tx.c 42 KB

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