tx.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711
  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/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 (unlikely(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. BUILD_BUG_ON(
  232. offsetof(struct ieee80211_tx_info, status.ack_signal) != 20);
  233. memset(&txinfo->status.ack_signal, 0,
  234. sizeof(struct ieee80211_tx_info) -
  235. offsetof(struct ieee80211_tx_info, status.ack_signal));
  236. if (atomic_read(&ar->tx_total_queued))
  237. ar->tx_schedule = true;
  238. if (txinfo->flags & IEEE80211_TX_CTL_AMPDU) {
  239. if (!atomic_read(&ar->tx_ampdu_upload))
  240. ar->tx_ampdu_schedule = true;
  241. if (txinfo->flags & IEEE80211_TX_STAT_AMPDU) {
  242. struct _carl9170_tx_superframe *super;
  243. super = (void *)skb->data;
  244. txinfo->status.ampdu_len = super->s.rix;
  245. txinfo->status.ampdu_ack_len = super->s.cnt;
  246. } else if ((txinfo->flags & IEEE80211_TX_STAT_ACK) &&
  247. !(txinfo->flags & IEEE80211_TX_CTL_REQ_TX_STATUS)) {
  248. /*
  249. * drop redundant tx_status reports:
  250. *
  251. * 1. ampdu_ack_len of the final tx_status does
  252. * include the feedback of this particular frame.
  253. *
  254. * 2. tx_status_irqsafe only queues up to 128
  255. * tx feedback reports and discards the rest.
  256. *
  257. * 3. minstrel_ht is picky, it only accepts
  258. * reports of frames with the TX_STATUS_AMPDU flag.
  259. *
  260. * 4. mac80211 is not particularly interested in
  261. * feedback either [CTL_REQ_TX_STATUS not set]
  262. */
  263. ieee80211_free_txskb(ar->hw, skb);
  264. return;
  265. } else {
  266. /*
  267. * Either the frame transmission has failed or
  268. * mac80211 requested tx status.
  269. */
  270. }
  271. }
  272. skb_pull(skb, sizeof(struct _carl9170_tx_superframe));
  273. ieee80211_tx_status_irqsafe(ar->hw, skb);
  274. }
  275. void carl9170_tx_get_skb(struct sk_buff *skb)
  276. {
  277. struct carl9170_tx_info *arinfo = (void *)
  278. (IEEE80211_SKB_CB(skb))->rate_driver_data;
  279. kref_get(&arinfo->ref);
  280. }
  281. int carl9170_tx_put_skb(struct sk_buff *skb)
  282. {
  283. struct carl9170_tx_info *arinfo = (void *)
  284. (IEEE80211_SKB_CB(skb))->rate_driver_data;
  285. return kref_put(&arinfo->ref, carl9170_tx_release);
  286. }
  287. /* Caller must hold the tid_info->lock & rcu_read_lock */
  288. static void carl9170_tx_shift_bm(struct ar9170 *ar,
  289. struct carl9170_sta_tid *tid_info, u16 seq)
  290. {
  291. u16 off;
  292. off = SEQ_DIFF(seq, tid_info->bsn);
  293. if (WARN_ON_ONCE(off >= CARL9170_BAW_BITS))
  294. return;
  295. /*
  296. * Sanity check. For each MPDU we set the bit in bitmap and
  297. * clear it once we received the tx_status.
  298. * But if the bit is already cleared then we've been bitten
  299. * by a bug.
  300. */
  301. WARN_ON_ONCE(!test_and_clear_bit(off, tid_info->bitmap));
  302. off = SEQ_DIFF(tid_info->snx, tid_info->bsn);
  303. if (WARN_ON_ONCE(off >= CARL9170_BAW_BITS))
  304. return;
  305. if (!bitmap_empty(tid_info->bitmap, off))
  306. off = find_first_bit(tid_info->bitmap, off);
  307. tid_info->bsn += off;
  308. tid_info->bsn &= 0x0fff;
  309. bitmap_shift_right(tid_info->bitmap, tid_info->bitmap,
  310. off, CARL9170_BAW_BITS);
  311. }
  312. static void carl9170_tx_status_process_ampdu(struct ar9170 *ar,
  313. struct sk_buff *skb, struct ieee80211_tx_info *txinfo)
  314. {
  315. struct _carl9170_tx_superframe *super = (void *) skb->data;
  316. struct ieee80211_hdr *hdr = (void *) super->frame_data;
  317. struct ieee80211_sta *sta;
  318. struct carl9170_sta_info *sta_info;
  319. struct carl9170_sta_tid *tid_info;
  320. u8 tid;
  321. if (!(txinfo->flags & IEEE80211_TX_CTL_AMPDU) ||
  322. txinfo->flags & IEEE80211_TX_CTL_INJECTED)
  323. return;
  324. rcu_read_lock();
  325. sta = __carl9170_get_tx_sta(ar, skb);
  326. if (unlikely(!sta))
  327. goto out_rcu;
  328. tid = get_tid_h(hdr);
  329. sta_info = (void *) sta->drv_priv;
  330. tid_info = rcu_dereference(sta_info->agg[tid]);
  331. if (!tid_info)
  332. goto out_rcu;
  333. spin_lock_bh(&tid_info->lock);
  334. if (likely(tid_info->state >= CARL9170_TID_STATE_IDLE))
  335. carl9170_tx_shift_bm(ar, tid_info, get_seq_h(hdr));
  336. if (sta_info->stats[tid].clear) {
  337. sta_info->stats[tid].clear = false;
  338. sta_info->stats[tid].req = false;
  339. sta_info->stats[tid].ampdu_len = 0;
  340. sta_info->stats[tid].ampdu_ack_len = 0;
  341. }
  342. sta_info->stats[tid].ampdu_len++;
  343. if (txinfo->status.rates[0].count == 1)
  344. sta_info->stats[tid].ampdu_ack_len++;
  345. if (!(txinfo->flags & IEEE80211_TX_STAT_ACK))
  346. sta_info->stats[tid].req = true;
  347. if (super->f.mac_control & cpu_to_le16(AR9170_TX_MAC_IMM_BA)) {
  348. super->s.rix = sta_info->stats[tid].ampdu_len;
  349. super->s.cnt = sta_info->stats[tid].ampdu_ack_len;
  350. txinfo->flags |= IEEE80211_TX_STAT_AMPDU;
  351. if (sta_info->stats[tid].req)
  352. txinfo->flags |= IEEE80211_TX_STAT_AMPDU_NO_BACK;
  353. sta_info->stats[tid].clear = true;
  354. }
  355. spin_unlock_bh(&tid_info->lock);
  356. out_rcu:
  357. rcu_read_unlock();
  358. }
  359. static void carl9170_tx_bar_status(struct ar9170 *ar, struct sk_buff *skb,
  360. struct ieee80211_tx_info *tx_info)
  361. {
  362. struct _carl9170_tx_superframe *super = (void *) skb->data;
  363. struct ieee80211_bar *bar = (void *) super->frame_data;
  364. /*
  365. * Unlike all other frames, the status report for BARs does
  366. * not directly come from the hardware as it is incapable of
  367. * matching a BA to a previously send BAR.
  368. * Instead the RX-path will scan for incoming BAs and set the
  369. * IEEE80211_TX_STAT_ACK if it sees one that was likely
  370. * caused by a BAR from us.
  371. */
  372. if (unlikely(ieee80211_is_back_req(bar->frame_control)) &&
  373. !(tx_info->flags & IEEE80211_TX_STAT_ACK)) {
  374. struct carl9170_bar_list_entry *entry;
  375. int queue = skb_get_queue_mapping(skb);
  376. rcu_read_lock();
  377. list_for_each_entry_rcu(entry, &ar->bar_list[queue], list) {
  378. if (entry->skb == skb) {
  379. spin_lock_bh(&ar->bar_list_lock[queue]);
  380. list_del_rcu(&entry->list);
  381. spin_unlock_bh(&ar->bar_list_lock[queue]);
  382. kfree_rcu(entry, head);
  383. goto out;
  384. }
  385. }
  386. WARN(1, "bar not found in %d - ra:%pM ta:%pM c:%x ssn:%x\n",
  387. queue, bar->ra, bar->ta, bar->control,
  388. bar->start_seq_num);
  389. out:
  390. rcu_read_unlock();
  391. }
  392. }
  393. void carl9170_tx_status(struct ar9170 *ar, struct sk_buff *skb,
  394. const bool success)
  395. {
  396. struct ieee80211_tx_info *txinfo;
  397. carl9170_tx_accounting_free(ar, skb);
  398. txinfo = IEEE80211_SKB_CB(skb);
  399. carl9170_tx_bar_status(ar, skb, txinfo);
  400. if (success)
  401. txinfo->flags |= IEEE80211_TX_STAT_ACK;
  402. else
  403. ar->tx_ack_failures++;
  404. if (txinfo->flags & IEEE80211_TX_CTL_AMPDU)
  405. carl9170_tx_status_process_ampdu(ar, skb, txinfo);
  406. carl9170_tx_ps_unblock(ar, skb);
  407. carl9170_tx_put_skb(skb);
  408. }
  409. /* This function may be called form any context */
  410. void carl9170_tx_callback(struct ar9170 *ar, struct sk_buff *skb)
  411. {
  412. struct ieee80211_tx_info *txinfo = IEEE80211_SKB_CB(skb);
  413. atomic_dec(&ar->tx_total_pending);
  414. if (txinfo->flags & IEEE80211_TX_CTL_AMPDU)
  415. atomic_dec(&ar->tx_ampdu_upload);
  416. if (carl9170_tx_put_skb(skb))
  417. tasklet_hi_schedule(&ar->usb_tasklet);
  418. }
  419. static struct sk_buff *carl9170_get_queued_skb(struct ar9170 *ar, u8 cookie,
  420. struct sk_buff_head *queue)
  421. {
  422. struct sk_buff *skb;
  423. spin_lock_bh(&queue->lock);
  424. skb_queue_walk(queue, skb) {
  425. struct _carl9170_tx_superframe *txc = (void *) skb->data;
  426. if (txc->s.cookie != cookie)
  427. continue;
  428. __skb_unlink(skb, queue);
  429. spin_unlock_bh(&queue->lock);
  430. carl9170_release_dev_space(ar, skb);
  431. return skb;
  432. }
  433. spin_unlock_bh(&queue->lock);
  434. return NULL;
  435. }
  436. static void carl9170_tx_fill_rateinfo(struct ar9170 *ar, unsigned int rix,
  437. unsigned int tries, struct ieee80211_tx_info *txinfo)
  438. {
  439. unsigned int i;
  440. for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
  441. if (txinfo->status.rates[i].idx < 0)
  442. break;
  443. if (i == rix) {
  444. txinfo->status.rates[i].count = tries;
  445. i++;
  446. break;
  447. }
  448. }
  449. for (; i < IEEE80211_TX_MAX_RATES; i++) {
  450. txinfo->status.rates[i].idx = -1;
  451. txinfo->status.rates[i].count = 0;
  452. }
  453. }
  454. static void carl9170_check_queue_stop_timeout(struct ar9170 *ar)
  455. {
  456. int i;
  457. struct sk_buff *skb;
  458. struct ieee80211_tx_info *txinfo;
  459. struct carl9170_tx_info *arinfo;
  460. bool restart = false;
  461. for (i = 0; i < ar->hw->queues; i++) {
  462. spin_lock_bh(&ar->tx_status[i].lock);
  463. skb = skb_peek(&ar->tx_status[i]);
  464. if (!skb)
  465. goto next;
  466. txinfo = IEEE80211_SKB_CB(skb);
  467. arinfo = (void *) txinfo->rate_driver_data;
  468. if (time_is_before_jiffies(arinfo->timeout +
  469. msecs_to_jiffies(CARL9170_QUEUE_STUCK_TIMEOUT)) == true)
  470. restart = true;
  471. next:
  472. spin_unlock_bh(&ar->tx_status[i].lock);
  473. }
  474. if (restart) {
  475. /*
  476. * At least one queue has been stuck for long enough.
  477. * Give the device a kick and hope it gets back to
  478. * work.
  479. *
  480. * possible reasons may include:
  481. * - frames got lost/corrupted (bad connection to the device)
  482. * - stalled rx processing/usb controller hiccups
  483. * - firmware errors/bugs
  484. * - every bug you can think of.
  485. * - all bugs you can't...
  486. * - ...
  487. */
  488. carl9170_restart(ar, CARL9170_RR_STUCK_TX);
  489. }
  490. }
  491. static void carl9170_tx_ampdu_timeout(struct ar9170 *ar)
  492. {
  493. struct carl9170_sta_tid *iter;
  494. struct sk_buff *skb;
  495. struct ieee80211_tx_info *txinfo;
  496. struct carl9170_tx_info *arinfo;
  497. struct ieee80211_sta *sta;
  498. rcu_read_lock();
  499. list_for_each_entry_rcu(iter, &ar->tx_ampdu_list, list) {
  500. if (iter->state < CARL9170_TID_STATE_IDLE)
  501. continue;
  502. spin_lock_bh(&iter->lock);
  503. skb = skb_peek(&iter->queue);
  504. if (!skb)
  505. goto unlock;
  506. txinfo = IEEE80211_SKB_CB(skb);
  507. arinfo = (void *)txinfo->rate_driver_data;
  508. if (time_is_after_jiffies(arinfo->timeout +
  509. msecs_to_jiffies(CARL9170_QUEUE_TIMEOUT)))
  510. goto unlock;
  511. sta = iter->sta;
  512. if (WARN_ON(!sta))
  513. goto unlock;
  514. ieee80211_stop_tx_ba_session(sta, iter->tid);
  515. unlock:
  516. spin_unlock_bh(&iter->lock);
  517. }
  518. rcu_read_unlock();
  519. }
  520. void carl9170_tx_janitor(struct work_struct *work)
  521. {
  522. struct ar9170 *ar = container_of(work, struct ar9170,
  523. tx_janitor.work);
  524. if (!IS_STARTED(ar))
  525. return;
  526. ar->tx_janitor_last_run = jiffies;
  527. carl9170_check_queue_stop_timeout(ar);
  528. carl9170_tx_ampdu_timeout(ar);
  529. if (!atomic_read(&ar->tx_total_queued))
  530. return;
  531. ieee80211_queue_delayed_work(ar->hw, &ar->tx_janitor,
  532. msecs_to_jiffies(CARL9170_TX_TIMEOUT));
  533. }
  534. static void __carl9170_tx_process_status(struct ar9170 *ar,
  535. const uint8_t cookie, const uint8_t info)
  536. {
  537. struct sk_buff *skb;
  538. struct ieee80211_tx_info *txinfo;
  539. unsigned int r, t, q;
  540. bool success = true;
  541. q = ar9170_qmap[info & CARL9170_TX_STATUS_QUEUE];
  542. skb = carl9170_get_queued_skb(ar, cookie, &ar->tx_status[q]);
  543. if (!skb) {
  544. /*
  545. * We have lost the race to another thread.
  546. */
  547. return ;
  548. }
  549. txinfo = IEEE80211_SKB_CB(skb);
  550. if (!(info & CARL9170_TX_STATUS_SUCCESS))
  551. success = false;
  552. r = (info & CARL9170_TX_STATUS_RIX) >> CARL9170_TX_STATUS_RIX_S;
  553. t = (info & CARL9170_TX_STATUS_TRIES) >> CARL9170_TX_STATUS_TRIES_S;
  554. carl9170_tx_fill_rateinfo(ar, r, t, txinfo);
  555. carl9170_tx_status(ar, skb, success);
  556. }
  557. void carl9170_tx_process_status(struct ar9170 *ar,
  558. const struct carl9170_rsp *cmd)
  559. {
  560. unsigned int i;
  561. for (i = 0; i < cmd->hdr.ext; i++) {
  562. if (WARN_ON(i > ((cmd->hdr.len / 2) + 1))) {
  563. print_hex_dump_bytes("UU:", DUMP_PREFIX_NONE,
  564. (void *) cmd, cmd->hdr.len + 4);
  565. break;
  566. }
  567. __carl9170_tx_process_status(ar, cmd->_tx_status[i].cookie,
  568. cmd->_tx_status[i].info);
  569. }
  570. }
  571. static void carl9170_tx_rate_tpc_chains(struct ar9170 *ar,
  572. struct ieee80211_tx_info *info, struct ieee80211_tx_rate *txrate,
  573. unsigned int *phyrate, unsigned int *tpc, unsigned int *chains)
  574. {
  575. struct ieee80211_rate *rate = NULL;
  576. u8 *txpower;
  577. unsigned int idx;
  578. idx = txrate->idx;
  579. *tpc = 0;
  580. *phyrate = 0;
  581. if (txrate->flags & IEEE80211_TX_RC_MCS) {
  582. if (txrate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH) {
  583. /* +1 dBm for HT40 */
  584. *tpc += 2;
  585. if (info->band == IEEE80211_BAND_2GHZ)
  586. txpower = ar->power_2G_ht40;
  587. else
  588. txpower = ar->power_5G_ht40;
  589. } else {
  590. if (info->band == IEEE80211_BAND_2GHZ)
  591. txpower = ar->power_2G_ht20;
  592. else
  593. txpower = ar->power_5G_ht20;
  594. }
  595. *phyrate = txrate->idx;
  596. *tpc += txpower[idx & 7];
  597. } else {
  598. if (info->band == IEEE80211_BAND_2GHZ) {
  599. if (idx < 4)
  600. txpower = ar->power_2G_cck;
  601. else
  602. txpower = ar->power_2G_ofdm;
  603. } else {
  604. txpower = ar->power_5G_leg;
  605. idx += 4;
  606. }
  607. rate = &__carl9170_ratetable[idx];
  608. *tpc += txpower[(rate->hw_value & 0x30) >> 4];
  609. *phyrate = rate->hw_value & 0xf;
  610. }
  611. if (ar->eeprom.tx_mask == 1) {
  612. *chains = AR9170_TX_PHY_TXCHAIN_1;
  613. } else {
  614. if (!(txrate->flags & IEEE80211_TX_RC_MCS) &&
  615. rate && rate->bitrate >= 360)
  616. *chains = AR9170_TX_PHY_TXCHAIN_1;
  617. else
  618. *chains = AR9170_TX_PHY_TXCHAIN_2;
  619. }
  620. *tpc = min_t(unsigned int, *tpc, ar->hw->conf.power_level * 2);
  621. }
  622. static __le32 carl9170_tx_physet(struct ar9170 *ar,
  623. struct ieee80211_tx_info *info, struct ieee80211_tx_rate *txrate)
  624. {
  625. unsigned int power = 0, chains = 0, phyrate = 0;
  626. __le32 tmp;
  627. tmp = cpu_to_le32(0);
  628. if (txrate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
  629. tmp |= cpu_to_le32(AR9170_TX_PHY_BW_40MHZ <<
  630. AR9170_TX_PHY_BW_S);
  631. /* this works because 40 MHz is 2 and dup is 3 */
  632. if (txrate->flags & IEEE80211_TX_RC_DUP_DATA)
  633. tmp |= cpu_to_le32(AR9170_TX_PHY_BW_40MHZ_DUP <<
  634. AR9170_TX_PHY_BW_S);
  635. if (txrate->flags & IEEE80211_TX_RC_SHORT_GI)
  636. tmp |= cpu_to_le32(AR9170_TX_PHY_SHORT_GI);
  637. if (txrate->flags & IEEE80211_TX_RC_MCS) {
  638. SET_VAL(AR9170_TX_PHY_MCS, phyrate, txrate->idx);
  639. /* heavy clip control */
  640. tmp |= cpu_to_le32((txrate->idx & 0x7) <<
  641. AR9170_TX_PHY_TX_HEAVY_CLIP_S);
  642. tmp |= cpu_to_le32(AR9170_TX_PHY_MOD_HT);
  643. /*
  644. * green field preamble does not work.
  645. *
  646. * if (txrate->flags & IEEE80211_TX_RC_GREEN_FIELD)
  647. * tmp |= cpu_to_le32(AR9170_TX_PHY_GREENFIELD);
  648. */
  649. } else {
  650. if (info->band == IEEE80211_BAND_2GHZ) {
  651. if (txrate->idx <= AR9170_TX_PHY_RATE_CCK_11M)
  652. tmp |= cpu_to_le32(AR9170_TX_PHY_MOD_CCK);
  653. else
  654. tmp |= cpu_to_le32(AR9170_TX_PHY_MOD_OFDM);
  655. } else {
  656. tmp |= cpu_to_le32(AR9170_TX_PHY_MOD_OFDM);
  657. }
  658. /*
  659. * short preamble seems to be broken too.
  660. *
  661. * if (txrate->flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
  662. * tmp |= cpu_to_le32(AR9170_TX_PHY_SHORT_PREAMBLE);
  663. */
  664. }
  665. carl9170_tx_rate_tpc_chains(ar, info, txrate,
  666. &phyrate, &power, &chains);
  667. tmp |= cpu_to_le32(SET_CONSTVAL(AR9170_TX_PHY_MCS, phyrate));
  668. tmp |= cpu_to_le32(SET_CONSTVAL(AR9170_TX_PHY_TX_PWR, power));
  669. tmp |= cpu_to_le32(SET_CONSTVAL(AR9170_TX_PHY_TXCHAIN, chains));
  670. return tmp;
  671. }
  672. static bool carl9170_tx_rts_check(struct ar9170 *ar,
  673. struct ieee80211_tx_rate *rate,
  674. bool ampdu, bool multi)
  675. {
  676. switch (ar->erp_mode) {
  677. case CARL9170_ERP_AUTO:
  678. if (ampdu)
  679. break;
  680. case CARL9170_ERP_MAC80211:
  681. if (!(rate->flags & IEEE80211_TX_RC_USE_RTS_CTS))
  682. break;
  683. case CARL9170_ERP_RTS:
  684. if (likely(!multi))
  685. return true;
  686. default:
  687. break;
  688. }
  689. return false;
  690. }
  691. static bool carl9170_tx_cts_check(struct ar9170 *ar,
  692. struct ieee80211_tx_rate *rate)
  693. {
  694. switch (ar->erp_mode) {
  695. case CARL9170_ERP_AUTO:
  696. case CARL9170_ERP_MAC80211:
  697. if (!(rate->flags & IEEE80211_TX_RC_USE_CTS_PROTECT))
  698. break;
  699. case CARL9170_ERP_CTS:
  700. return true;
  701. default:
  702. break;
  703. }
  704. return false;
  705. }
  706. static void carl9170_tx_get_rates(struct ar9170 *ar,
  707. struct ieee80211_vif *vif,
  708. struct ieee80211_sta *sta,
  709. struct sk_buff *skb)
  710. {
  711. struct ieee80211_tx_info *info;
  712. BUILD_BUG_ON(IEEE80211_TX_MAX_RATES < CARL9170_TX_MAX_RATES);
  713. BUILD_BUG_ON(IEEE80211_TX_MAX_RATES > IEEE80211_TX_RATE_TABLE_SIZE);
  714. info = IEEE80211_SKB_CB(skb);
  715. ieee80211_get_tx_rates(vif, sta, skb,
  716. info->control.rates,
  717. IEEE80211_TX_MAX_RATES);
  718. }
  719. static void carl9170_tx_apply_rateset(struct ar9170 *ar,
  720. struct ieee80211_tx_info *sinfo,
  721. struct sk_buff *skb)
  722. {
  723. struct ieee80211_tx_rate *txrate;
  724. struct ieee80211_tx_info *info;
  725. struct _carl9170_tx_superframe *txc = (void *) skb->data;
  726. int i;
  727. bool ampdu;
  728. bool no_ack;
  729. info = IEEE80211_SKB_CB(skb);
  730. ampdu = !!(info->flags & IEEE80211_TX_CTL_AMPDU);
  731. no_ack = !!(info->flags & IEEE80211_TX_CTL_NO_ACK);
  732. /* Set the rate control probe flag for all (sub-) frames.
  733. * This is because the TX_STATS_AMPDU flag is only set on
  734. * the last frame, so it has to be inherited.
  735. */
  736. info->flags |= (sinfo->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE);
  737. /* NOTE: For the first rate, the ERP & AMPDU flags are directly
  738. * taken from mac_control. For all fallback rate, the firmware
  739. * updates the mac_control flags from the rate info field.
  740. */
  741. for (i = 0; i < CARL9170_TX_MAX_RATES; i++) {
  742. __le32 phy_set;
  743. txrate = &sinfo->control.rates[i];
  744. if (txrate->idx < 0)
  745. break;
  746. phy_set = carl9170_tx_physet(ar, info, txrate);
  747. if (i == 0) {
  748. __le16 mac_tmp = cpu_to_le16(0);
  749. /* first rate - part of the hw's frame header */
  750. txc->f.phy_control = phy_set;
  751. if (ampdu && txrate->flags & IEEE80211_TX_RC_MCS)
  752. mac_tmp |= cpu_to_le16(AR9170_TX_MAC_AGGR);
  753. if (carl9170_tx_rts_check(ar, txrate, ampdu, no_ack))
  754. mac_tmp |= cpu_to_le16(AR9170_TX_MAC_PROT_RTS);
  755. else if (carl9170_tx_cts_check(ar, txrate))
  756. mac_tmp |= cpu_to_le16(AR9170_TX_MAC_PROT_CTS);
  757. txc->f.mac_control |= mac_tmp;
  758. } else {
  759. /* fallback rates are stored in the firmware's
  760. * retry rate set array.
  761. */
  762. txc->s.rr[i - 1] = phy_set;
  763. }
  764. SET_VAL(CARL9170_TX_SUPER_RI_TRIES, txc->s.ri[i],
  765. txrate->count);
  766. if (carl9170_tx_rts_check(ar, txrate, ampdu, no_ack))
  767. txc->s.ri[i] |= (AR9170_TX_MAC_PROT_RTS <<
  768. CARL9170_TX_SUPER_RI_ERP_PROT_S);
  769. else if (carl9170_tx_cts_check(ar, txrate))
  770. txc->s.ri[i] |= (AR9170_TX_MAC_PROT_CTS <<
  771. CARL9170_TX_SUPER_RI_ERP_PROT_S);
  772. if (ampdu && (txrate->flags & IEEE80211_TX_RC_MCS))
  773. txc->s.ri[i] |= CARL9170_TX_SUPER_RI_AMPDU;
  774. }
  775. }
  776. static int carl9170_tx_prepare(struct ar9170 *ar,
  777. struct ieee80211_sta *sta,
  778. struct sk_buff *skb)
  779. {
  780. struct ieee80211_hdr *hdr;
  781. struct _carl9170_tx_superframe *txc;
  782. struct carl9170_vif_info *cvif;
  783. struct ieee80211_tx_info *info;
  784. struct carl9170_tx_info *arinfo;
  785. unsigned int hw_queue;
  786. __le16 mac_tmp;
  787. u16 len;
  788. BUILD_BUG_ON(sizeof(*arinfo) > sizeof(info->rate_driver_data));
  789. BUILD_BUG_ON(sizeof(struct _carl9170_tx_superdesc) !=
  790. CARL9170_TX_SUPERDESC_LEN);
  791. BUILD_BUG_ON(sizeof(struct _ar9170_tx_hwdesc) !=
  792. AR9170_TX_HWDESC_LEN);
  793. BUILD_BUG_ON(AR9170_MAX_VIRTUAL_MAC >
  794. ((CARL9170_TX_SUPER_MISC_VIF_ID >>
  795. CARL9170_TX_SUPER_MISC_VIF_ID_S) + 1));
  796. hw_queue = ar9170_qmap[carl9170_get_queue(ar, skb)];
  797. hdr = (void *)skb->data;
  798. info = IEEE80211_SKB_CB(skb);
  799. len = skb->len;
  800. /*
  801. * Note: If the frame was sent through a monitor interface,
  802. * the ieee80211_vif pointer can be NULL.
  803. */
  804. if (likely(info->control.vif))
  805. cvif = (void *) info->control.vif->drv_priv;
  806. else
  807. cvif = NULL;
  808. txc = (void *)skb_push(skb, sizeof(*txc));
  809. memset(txc, 0, sizeof(*txc));
  810. SET_VAL(CARL9170_TX_SUPER_MISC_QUEUE, txc->s.misc, hw_queue);
  811. if (likely(cvif))
  812. SET_VAL(CARL9170_TX_SUPER_MISC_VIF_ID, txc->s.misc, cvif->id);
  813. if (unlikely(info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM))
  814. txc->s.misc |= CARL9170_TX_SUPER_MISC_CAB;
  815. if (unlikely(info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ))
  816. txc->s.misc |= CARL9170_TX_SUPER_MISC_ASSIGN_SEQ;
  817. if (unlikely(ieee80211_is_probe_resp(hdr->frame_control)))
  818. txc->s.misc |= CARL9170_TX_SUPER_MISC_FILL_IN_TSF;
  819. mac_tmp = cpu_to_le16(AR9170_TX_MAC_HW_DURATION |
  820. AR9170_TX_MAC_BACKOFF);
  821. mac_tmp |= cpu_to_le16((hw_queue << AR9170_TX_MAC_QOS_S) &
  822. AR9170_TX_MAC_QOS);
  823. if (unlikely(info->flags & IEEE80211_TX_CTL_NO_ACK))
  824. mac_tmp |= cpu_to_le16(AR9170_TX_MAC_NO_ACK);
  825. if (info->control.hw_key) {
  826. len += info->control.hw_key->icv_len;
  827. switch (info->control.hw_key->cipher) {
  828. case WLAN_CIPHER_SUITE_WEP40:
  829. case WLAN_CIPHER_SUITE_WEP104:
  830. case WLAN_CIPHER_SUITE_TKIP:
  831. mac_tmp |= cpu_to_le16(AR9170_TX_MAC_ENCR_RC4);
  832. break;
  833. case WLAN_CIPHER_SUITE_CCMP:
  834. mac_tmp |= cpu_to_le16(AR9170_TX_MAC_ENCR_AES);
  835. break;
  836. default:
  837. WARN_ON(1);
  838. goto err_out;
  839. }
  840. }
  841. if (info->flags & IEEE80211_TX_CTL_AMPDU) {
  842. unsigned int density, factor;
  843. if (unlikely(!sta || !cvif))
  844. goto err_out;
  845. factor = min_t(unsigned int, 1u, sta->ht_cap.ampdu_factor);
  846. density = sta->ht_cap.ampdu_density;
  847. if (density) {
  848. /*
  849. * Watch out!
  850. *
  851. * Otus uses slightly different density values than
  852. * those from the 802.11n spec.
  853. */
  854. density = max_t(unsigned int, density + 1, 7u);
  855. }
  856. SET_VAL(CARL9170_TX_SUPER_AMPDU_DENSITY,
  857. txc->s.ampdu_settings, density);
  858. SET_VAL(CARL9170_TX_SUPER_AMPDU_FACTOR,
  859. txc->s.ampdu_settings, factor);
  860. }
  861. txc->s.len = cpu_to_le16(skb->len);
  862. txc->f.length = cpu_to_le16(len + FCS_LEN);
  863. txc->f.mac_control = mac_tmp;
  864. arinfo = (void *)info->rate_driver_data;
  865. arinfo->timeout = jiffies;
  866. arinfo->ar = ar;
  867. kref_init(&arinfo->ref);
  868. return 0;
  869. err_out:
  870. skb_pull(skb, sizeof(*txc));
  871. return -EINVAL;
  872. }
  873. static void carl9170_set_immba(struct ar9170 *ar, struct sk_buff *skb)
  874. {
  875. struct _carl9170_tx_superframe *super;
  876. super = (void *) skb->data;
  877. super->f.mac_control |= cpu_to_le16(AR9170_TX_MAC_IMM_BA);
  878. }
  879. static void carl9170_set_ampdu_params(struct ar9170 *ar, struct sk_buff *skb)
  880. {
  881. struct _carl9170_tx_superframe *super;
  882. int tmp;
  883. super = (void *) skb->data;
  884. tmp = (super->s.ampdu_settings & CARL9170_TX_SUPER_AMPDU_DENSITY) <<
  885. CARL9170_TX_SUPER_AMPDU_DENSITY_S;
  886. /*
  887. * If you haven't noticed carl9170_tx_prepare has already filled
  888. * in all ampdu spacing & factor parameters.
  889. * Now it's the time to check whenever the settings have to be
  890. * updated by the firmware, or if everything is still the same.
  891. *
  892. * There's no sane way to handle different density values with
  893. * this hardware, so we may as well just do the compare in the
  894. * driver.
  895. */
  896. if (tmp != ar->current_density) {
  897. ar->current_density = tmp;
  898. super->s.ampdu_settings |=
  899. CARL9170_TX_SUPER_AMPDU_COMMIT_DENSITY;
  900. }
  901. tmp = (super->s.ampdu_settings & CARL9170_TX_SUPER_AMPDU_FACTOR) <<
  902. CARL9170_TX_SUPER_AMPDU_FACTOR_S;
  903. if (tmp != ar->current_factor) {
  904. ar->current_factor = tmp;
  905. super->s.ampdu_settings |=
  906. CARL9170_TX_SUPER_AMPDU_COMMIT_FACTOR;
  907. }
  908. }
  909. static void carl9170_tx_ampdu(struct ar9170 *ar)
  910. {
  911. struct sk_buff_head agg;
  912. struct carl9170_sta_tid *tid_info;
  913. struct sk_buff *skb, *first;
  914. struct ieee80211_tx_info *tx_info_first;
  915. unsigned int i = 0, done_ampdus = 0;
  916. u16 seq, queue, tmpssn;
  917. atomic_inc(&ar->tx_ampdu_scheduler);
  918. ar->tx_ampdu_schedule = false;
  919. if (atomic_read(&ar->tx_ampdu_upload))
  920. return;
  921. if (!ar->tx_ampdu_list_len)
  922. return;
  923. __skb_queue_head_init(&agg);
  924. rcu_read_lock();
  925. tid_info = rcu_dereference(ar->tx_ampdu_iter);
  926. if (WARN_ON_ONCE(!tid_info)) {
  927. rcu_read_unlock();
  928. return;
  929. }
  930. retry:
  931. list_for_each_entry_continue_rcu(tid_info, &ar->tx_ampdu_list, list) {
  932. i++;
  933. if (tid_info->state < CARL9170_TID_STATE_PROGRESS)
  934. continue;
  935. queue = TID_TO_WME_AC(tid_info->tid);
  936. spin_lock_bh(&tid_info->lock);
  937. if (tid_info->state != CARL9170_TID_STATE_XMIT)
  938. goto processed;
  939. tid_info->counter++;
  940. first = skb_peek(&tid_info->queue);
  941. tmpssn = carl9170_get_seq(first);
  942. seq = tid_info->snx;
  943. if (unlikely(tmpssn != seq)) {
  944. tid_info->state = CARL9170_TID_STATE_IDLE;
  945. goto processed;
  946. }
  947. tx_info_first = NULL;
  948. while ((skb = skb_peek(&tid_info->queue))) {
  949. /* strict 0, 1, ..., n - 1, n frame sequence order */
  950. if (unlikely(carl9170_get_seq(skb) != seq))
  951. break;
  952. /* don't upload more than AMPDU FACTOR allows. */
  953. if (unlikely(SEQ_DIFF(tid_info->snx, tid_info->bsn) >=
  954. (tid_info->max - 1)))
  955. break;
  956. if (!tx_info_first) {
  957. carl9170_tx_get_rates(ar, tid_info->vif,
  958. tid_info->sta, first);
  959. tx_info_first = IEEE80211_SKB_CB(first);
  960. }
  961. carl9170_tx_apply_rateset(ar, tx_info_first, skb);
  962. atomic_inc(&ar->tx_ampdu_upload);
  963. tid_info->snx = seq = SEQ_NEXT(seq);
  964. __skb_unlink(skb, &tid_info->queue);
  965. __skb_queue_tail(&agg, skb);
  966. if (skb_queue_len(&agg) >= CARL9170_NUM_TX_AGG_MAX)
  967. break;
  968. }
  969. if (skb_queue_empty(&tid_info->queue) ||
  970. carl9170_get_seq(skb_peek(&tid_info->queue)) !=
  971. tid_info->snx) {
  972. /* stop TID, if A-MPDU frames are still missing,
  973. * or whenever the queue is empty.
  974. */
  975. tid_info->state = CARL9170_TID_STATE_IDLE;
  976. }
  977. done_ampdus++;
  978. processed:
  979. spin_unlock_bh(&tid_info->lock);
  980. if (skb_queue_empty(&agg))
  981. continue;
  982. /* apply ampdu spacing & factor settings */
  983. carl9170_set_ampdu_params(ar, skb_peek(&agg));
  984. /* set aggregation push bit */
  985. carl9170_set_immba(ar, skb_peek_tail(&agg));
  986. spin_lock_bh(&ar->tx_pending[queue].lock);
  987. skb_queue_splice_tail_init(&agg, &ar->tx_pending[queue]);
  988. spin_unlock_bh(&ar->tx_pending[queue].lock);
  989. ar->tx_schedule = true;
  990. }
  991. if ((done_ampdus++ == 0) && (i++ == 0))
  992. goto retry;
  993. rcu_assign_pointer(ar->tx_ampdu_iter, tid_info);
  994. rcu_read_unlock();
  995. }
  996. static struct sk_buff *carl9170_tx_pick_skb(struct ar9170 *ar,
  997. struct sk_buff_head *queue)
  998. {
  999. struct sk_buff *skb;
  1000. struct ieee80211_tx_info *info;
  1001. struct carl9170_tx_info *arinfo;
  1002. BUILD_BUG_ON(sizeof(*arinfo) > sizeof(info->rate_driver_data));
  1003. spin_lock_bh(&queue->lock);
  1004. skb = skb_peek(queue);
  1005. if (unlikely(!skb))
  1006. goto err_unlock;
  1007. if (carl9170_alloc_dev_space(ar, skb))
  1008. goto err_unlock;
  1009. __skb_unlink(skb, queue);
  1010. spin_unlock_bh(&queue->lock);
  1011. info = IEEE80211_SKB_CB(skb);
  1012. arinfo = (void *) info->rate_driver_data;
  1013. arinfo->timeout = jiffies;
  1014. return skb;
  1015. err_unlock:
  1016. spin_unlock_bh(&queue->lock);
  1017. return NULL;
  1018. }
  1019. void carl9170_tx_drop(struct ar9170 *ar, struct sk_buff *skb)
  1020. {
  1021. struct _carl9170_tx_superframe *super;
  1022. uint8_t q = 0;
  1023. ar->tx_dropped++;
  1024. super = (void *)skb->data;
  1025. SET_VAL(CARL9170_TX_SUPER_MISC_QUEUE, q,
  1026. ar9170_qmap[carl9170_get_queue(ar, skb)]);
  1027. __carl9170_tx_process_status(ar, super->s.cookie, q);
  1028. }
  1029. static bool carl9170_tx_ps_drop(struct ar9170 *ar, struct sk_buff *skb)
  1030. {
  1031. struct ieee80211_sta *sta;
  1032. struct carl9170_sta_info *sta_info;
  1033. struct ieee80211_tx_info *tx_info;
  1034. rcu_read_lock();
  1035. sta = __carl9170_get_tx_sta(ar, skb);
  1036. if (!sta)
  1037. goto out_rcu;
  1038. sta_info = (void *) sta->drv_priv;
  1039. tx_info = IEEE80211_SKB_CB(skb);
  1040. if (unlikely(sta_info->sleeping) &&
  1041. !(tx_info->flags & (IEEE80211_TX_CTL_NO_PS_BUFFER |
  1042. IEEE80211_TX_CTL_CLEAR_PS_FILT))) {
  1043. rcu_read_unlock();
  1044. if (tx_info->flags & IEEE80211_TX_CTL_AMPDU)
  1045. atomic_dec(&ar->tx_ampdu_upload);
  1046. tx_info->flags |= IEEE80211_TX_STAT_TX_FILTERED;
  1047. carl9170_release_dev_space(ar, skb);
  1048. carl9170_tx_status(ar, skb, false);
  1049. return true;
  1050. }
  1051. out_rcu:
  1052. rcu_read_unlock();
  1053. return false;
  1054. }
  1055. static void carl9170_bar_check(struct ar9170 *ar, struct sk_buff *skb)
  1056. {
  1057. struct _carl9170_tx_superframe *super = (void *) skb->data;
  1058. struct ieee80211_bar *bar = (void *) super->frame_data;
  1059. if (unlikely(ieee80211_is_back_req(bar->frame_control)) &&
  1060. skb->len >= sizeof(struct ieee80211_bar)) {
  1061. struct carl9170_bar_list_entry *entry;
  1062. unsigned int queue = skb_get_queue_mapping(skb);
  1063. entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
  1064. if (!WARN_ON_ONCE(!entry)) {
  1065. entry->skb = skb;
  1066. spin_lock_bh(&ar->bar_list_lock[queue]);
  1067. list_add_tail_rcu(&entry->list, &ar->bar_list[queue]);
  1068. spin_unlock_bh(&ar->bar_list_lock[queue]);
  1069. }
  1070. }
  1071. }
  1072. static void carl9170_tx(struct ar9170 *ar)
  1073. {
  1074. struct sk_buff *skb;
  1075. unsigned int i, q;
  1076. bool schedule_garbagecollector = false;
  1077. ar->tx_schedule = false;
  1078. if (unlikely(!IS_STARTED(ar)))
  1079. return;
  1080. carl9170_usb_handle_tx_err(ar);
  1081. for (i = 0; i < ar->hw->queues; i++) {
  1082. while (!skb_queue_empty(&ar->tx_pending[i])) {
  1083. skb = carl9170_tx_pick_skb(ar, &ar->tx_pending[i]);
  1084. if (unlikely(!skb))
  1085. break;
  1086. if (unlikely(carl9170_tx_ps_drop(ar, skb)))
  1087. continue;
  1088. carl9170_bar_check(ar, skb);
  1089. atomic_inc(&ar->tx_total_pending);
  1090. q = __carl9170_get_queue(ar, i);
  1091. /*
  1092. * NB: tx_status[i] vs. tx_status[q],
  1093. * TODO: Move into pick_skb or alloc_dev_space.
  1094. */
  1095. skb_queue_tail(&ar->tx_status[q], skb);
  1096. /*
  1097. * increase ref count to "2".
  1098. * Ref counting is the easiest way to solve the
  1099. * race between the urb's completion routine:
  1100. * carl9170_tx_callback
  1101. * and wlan tx status functions:
  1102. * carl9170_tx_status/janitor.
  1103. */
  1104. carl9170_tx_get_skb(skb);
  1105. carl9170_usb_tx(ar, skb);
  1106. schedule_garbagecollector = true;
  1107. }
  1108. }
  1109. if (!schedule_garbagecollector)
  1110. return;
  1111. ieee80211_queue_delayed_work(ar->hw, &ar->tx_janitor,
  1112. msecs_to_jiffies(CARL9170_TX_TIMEOUT));
  1113. }
  1114. static bool carl9170_tx_ampdu_queue(struct ar9170 *ar,
  1115. struct ieee80211_sta *sta, struct sk_buff *skb,
  1116. struct ieee80211_tx_info *txinfo)
  1117. {
  1118. struct carl9170_sta_info *sta_info;
  1119. struct carl9170_sta_tid *agg;
  1120. struct sk_buff *iter;
  1121. u16 tid, seq, qseq, off;
  1122. bool run = false;
  1123. tid = carl9170_get_tid(skb);
  1124. seq = carl9170_get_seq(skb);
  1125. sta_info = (void *) sta->drv_priv;
  1126. rcu_read_lock();
  1127. agg = rcu_dereference(sta_info->agg[tid]);
  1128. if (!agg)
  1129. goto err_unlock_rcu;
  1130. spin_lock_bh(&agg->lock);
  1131. if (unlikely(agg->state < CARL9170_TID_STATE_IDLE))
  1132. goto err_unlock;
  1133. /* check if sequence is within the BA window */
  1134. if (unlikely(!BAW_WITHIN(agg->bsn, CARL9170_BAW_BITS, seq)))
  1135. goto err_unlock;
  1136. if (WARN_ON_ONCE(!BAW_WITHIN(agg->snx, CARL9170_BAW_BITS, seq)))
  1137. goto err_unlock;
  1138. off = SEQ_DIFF(seq, agg->bsn);
  1139. if (WARN_ON_ONCE(test_and_set_bit(off, agg->bitmap)))
  1140. goto err_unlock;
  1141. if (likely(BAW_WITHIN(agg->hsn, CARL9170_BAW_BITS, seq))) {
  1142. __skb_queue_tail(&agg->queue, skb);
  1143. agg->hsn = seq;
  1144. goto queued;
  1145. }
  1146. skb_queue_reverse_walk(&agg->queue, iter) {
  1147. qseq = carl9170_get_seq(iter);
  1148. if (BAW_WITHIN(qseq, CARL9170_BAW_BITS, seq)) {
  1149. __skb_queue_after(&agg->queue, iter, skb);
  1150. goto queued;
  1151. }
  1152. }
  1153. __skb_queue_head(&agg->queue, skb);
  1154. queued:
  1155. if (unlikely(agg->state != CARL9170_TID_STATE_XMIT)) {
  1156. if (agg->snx == carl9170_get_seq(skb_peek(&agg->queue))) {
  1157. agg->state = CARL9170_TID_STATE_XMIT;
  1158. run = true;
  1159. }
  1160. }
  1161. spin_unlock_bh(&agg->lock);
  1162. rcu_read_unlock();
  1163. return run;
  1164. err_unlock:
  1165. spin_unlock_bh(&agg->lock);
  1166. err_unlock_rcu:
  1167. rcu_read_unlock();
  1168. txinfo->flags &= ~IEEE80211_TX_CTL_AMPDU;
  1169. carl9170_tx_status(ar, skb, false);
  1170. ar->tx_dropped++;
  1171. return false;
  1172. }
  1173. void carl9170_op_tx(struct ieee80211_hw *hw,
  1174. struct ieee80211_tx_control *control,
  1175. struct sk_buff *skb)
  1176. {
  1177. struct ar9170 *ar = hw->priv;
  1178. struct ieee80211_tx_info *info;
  1179. struct ieee80211_sta *sta = control->sta;
  1180. struct ieee80211_vif *vif;
  1181. bool run;
  1182. if (unlikely(!IS_STARTED(ar)))
  1183. goto err_free;
  1184. info = IEEE80211_SKB_CB(skb);
  1185. vif = info->control.vif;
  1186. if (unlikely(carl9170_tx_prepare(ar, sta, skb)))
  1187. goto err_free;
  1188. carl9170_tx_accounting(ar, skb);
  1189. /*
  1190. * from now on, one has to use carl9170_tx_status to free
  1191. * all ressouces which are associated with the frame.
  1192. */
  1193. if (sta) {
  1194. struct carl9170_sta_info *stai = (void *) sta->drv_priv;
  1195. atomic_inc(&stai->pending_frames);
  1196. }
  1197. if (info->flags & IEEE80211_TX_CTL_AMPDU) {
  1198. /* to static code analyzers and reviewers:
  1199. * mac80211 guarantees that a valid "sta"
  1200. * reference is present, if a frame is to
  1201. * be part of an ampdu. Hence any extra
  1202. * sta == NULL checks are redundant in this
  1203. * special case.
  1204. */
  1205. run = carl9170_tx_ampdu_queue(ar, sta, skb, info);
  1206. if (run)
  1207. carl9170_tx_ampdu(ar);
  1208. } else {
  1209. unsigned int queue = skb_get_queue_mapping(skb);
  1210. carl9170_tx_get_rates(ar, vif, sta, skb);
  1211. carl9170_tx_apply_rateset(ar, info, skb);
  1212. skb_queue_tail(&ar->tx_pending[queue], skb);
  1213. }
  1214. carl9170_tx(ar);
  1215. return;
  1216. err_free:
  1217. ar->tx_dropped++;
  1218. ieee80211_free_txskb(ar->hw, skb);
  1219. }
  1220. void carl9170_tx_scheduler(struct ar9170 *ar)
  1221. {
  1222. if (ar->tx_ampdu_schedule)
  1223. carl9170_tx_ampdu(ar);
  1224. if (ar->tx_schedule)
  1225. carl9170_tx(ar);
  1226. }
  1227. /* caller has to take rcu_read_lock */
  1228. static struct carl9170_vif_info *carl9170_pick_beaconing_vif(struct ar9170 *ar)
  1229. {
  1230. struct carl9170_vif_info *cvif;
  1231. int i = 1;
  1232. /* The AR9170 hardware has no fancy beacon queue or some
  1233. * other scheduling mechanism. So, the driver has to make
  1234. * due by setting the two beacon timers (pretbtt and tbtt)
  1235. * once and then swapping the beacon address in the HW's
  1236. * register file each time the pretbtt fires.
  1237. */
  1238. cvif = rcu_dereference(ar->beacon_iter);
  1239. if (ar->vifs > 0 && cvif) {
  1240. do {
  1241. list_for_each_entry_continue_rcu(cvif, &ar->vif_list,
  1242. list) {
  1243. if (cvif->active && cvif->enable_beacon)
  1244. goto out;
  1245. }
  1246. } while (ar->beacon_enabled && i--);
  1247. }
  1248. out:
  1249. RCU_INIT_POINTER(ar->beacon_iter, cvif);
  1250. return cvif;
  1251. }
  1252. static bool carl9170_tx_beacon_physet(struct ar9170 *ar, struct sk_buff *skb,
  1253. u32 *ht1, u32 *plcp)
  1254. {
  1255. struct ieee80211_tx_info *txinfo;
  1256. struct ieee80211_tx_rate *rate;
  1257. unsigned int power, chains;
  1258. bool ht_rate;
  1259. txinfo = IEEE80211_SKB_CB(skb);
  1260. rate = &txinfo->control.rates[0];
  1261. ht_rate = !!(txinfo->control.rates[0].flags & IEEE80211_TX_RC_MCS);
  1262. carl9170_tx_rate_tpc_chains(ar, txinfo, rate, plcp, &power, &chains);
  1263. *ht1 = AR9170_MAC_BCN_HT1_TX_ANT0;
  1264. if (chains == AR9170_TX_PHY_TXCHAIN_2)
  1265. *ht1 |= AR9170_MAC_BCN_HT1_TX_ANT1;
  1266. SET_VAL(AR9170_MAC_BCN_HT1_PWR_CTRL, *ht1, 7);
  1267. SET_VAL(AR9170_MAC_BCN_HT1_TPC, *ht1, power);
  1268. SET_VAL(AR9170_MAC_BCN_HT1_CHAIN_MASK, *ht1, chains);
  1269. if (ht_rate) {
  1270. *ht1 |= AR9170_MAC_BCN_HT1_HT_EN;
  1271. if (rate->flags & IEEE80211_TX_RC_SHORT_GI)
  1272. *plcp |= AR9170_MAC_BCN_HT2_SGI;
  1273. if (rate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH) {
  1274. *ht1 |= AR9170_MAC_BCN_HT1_BWC_40M_SHARED;
  1275. *plcp |= AR9170_MAC_BCN_HT2_BW40;
  1276. } else if (rate->flags & IEEE80211_TX_RC_DUP_DATA) {
  1277. *ht1 |= AR9170_MAC_BCN_HT1_BWC_40M_DUP;
  1278. *plcp |= AR9170_MAC_BCN_HT2_BW40;
  1279. }
  1280. SET_VAL(AR9170_MAC_BCN_HT2_LEN, *plcp, skb->len + FCS_LEN);
  1281. } else {
  1282. if (*plcp <= AR9170_TX_PHY_RATE_CCK_11M)
  1283. *plcp |= ((skb->len + FCS_LEN) << (3 + 16)) + 0x0400;
  1284. else
  1285. *plcp |= ((skb->len + FCS_LEN) << 16) + 0x0010;
  1286. }
  1287. return ht_rate;
  1288. }
  1289. int carl9170_update_beacon(struct ar9170 *ar, const bool submit)
  1290. {
  1291. struct sk_buff *skb = NULL;
  1292. struct carl9170_vif_info *cvif;
  1293. __le32 *data, *old = NULL;
  1294. u32 word, ht1, plcp, off, addr, len;
  1295. int i = 0, err = 0;
  1296. bool ht_rate;
  1297. rcu_read_lock();
  1298. cvif = carl9170_pick_beaconing_vif(ar);
  1299. if (!cvif)
  1300. goto out_unlock;
  1301. skb = ieee80211_beacon_get_tim(ar->hw, carl9170_get_vif(cvif),
  1302. NULL, NULL);
  1303. if (!skb) {
  1304. err = -ENOMEM;
  1305. goto err_free;
  1306. }
  1307. spin_lock_bh(&ar->beacon_lock);
  1308. data = (__le32 *)skb->data;
  1309. if (cvif->beacon)
  1310. old = (__le32 *)cvif->beacon->data;
  1311. off = cvif->id * AR9170_MAC_BCN_LENGTH_MAX;
  1312. addr = ar->fw.beacon_addr + off;
  1313. len = roundup(skb->len + FCS_LEN, 4);
  1314. if ((off + len) > ar->fw.beacon_max_len) {
  1315. if (net_ratelimit()) {
  1316. wiphy_err(ar->hw->wiphy, "beacon does not "
  1317. "fit into device memory!\n");
  1318. }
  1319. err = -EINVAL;
  1320. goto err_unlock;
  1321. }
  1322. if (len > AR9170_MAC_BCN_LENGTH_MAX) {
  1323. if (net_ratelimit()) {
  1324. wiphy_err(ar->hw->wiphy, "no support for beacons "
  1325. "bigger than %d (yours:%d).\n",
  1326. AR9170_MAC_BCN_LENGTH_MAX, len);
  1327. }
  1328. err = -EMSGSIZE;
  1329. goto err_unlock;
  1330. }
  1331. ht_rate = carl9170_tx_beacon_physet(ar, skb, &ht1, &plcp);
  1332. carl9170_async_regwrite_begin(ar);
  1333. carl9170_async_regwrite(AR9170_MAC_REG_BCN_HT1, ht1);
  1334. if (ht_rate)
  1335. carl9170_async_regwrite(AR9170_MAC_REG_BCN_HT2, plcp);
  1336. else
  1337. carl9170_async_regwrite(AR9170_MAC_REG_BCN_PLCP, plcp);
  1338. for (i = 0; i < DIV_ROUND_UP(skb->len, 4); i++) {
  1339. /*
  1340. * XXX: This accesses beyond skb data for up
  1341. * to the last 3 bytes!!
  1342. */
  1343. if (old && (data[i] == old[i]))
  1344. continue;
  1345. word = le32_to_cpu(data[i]);
  1346. carl9170_async_regwrite(addr + 4 * i, word);
  1347. }
  1348. carl9170_async_regwrite_finish();
  1349. dev_kfree_skb_any(cvif->beacon);
  1350. cvif->beacon = NULL;
  1351. err = carl9170_async_regwrite_result();
  1352. if (!err)
  1353. cvif->beacon = skb;
  1354. spin_unlock_bh(&ar->beacon_lock);
  1355. if (err)
  1356. goto err_free;
  1357. if (submit) {
  1358. err = carl9170_bcn_ctrl(ar, cvif->id,
  1359. CARL9170_BCN_CTRL_CAB_TRIGGER,
  1360. addr, skb->len + FCS_LEN);
  1361. if (err)
  1362. goto err_free;
  1363. }
  1364. out_unlock:
  1365. rcu_read_unlock();
  1366. return 0;
  1367. err_unlock:
  1368. spin_unlock_bh(&ar->beacon_lock);
  1369. err_free:
  1370. rcu_read_unlock();
  1371. dev_kfree_skb_any(skb);
  1372. return err;
  1373. }