tx.c 43 KB

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