main.c 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720
  1. /*
  2. * Atheros AR9170 driver
  3. *
  4. * mac80211 interaction code
  5. *
  6. * Copyright 2008, Johannes Berg <johannes@sipsolutions.net>
  7. * Copyright 2009, Christian Lamparter <chunkeey@web.de>
  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/module.h>
  41. #include <linux/etherdevice.h>
  42. #include <net/mac80211.h>
  43. #include "ar9170.h"
  44. #include "hw.h"
  45. #include "cmd.h"
  46. static int modparam_nohwcrypt;
  47. module_param_named(nohwcrypt, modparam_nohwcrypt, bool, S_IRUGO);
  48. MODULE_PARM_DESC(nohwcrypt, "Disable hardware encryption.");
  49. static int modparam_ht;
  50. module_param_named(ht, modparam_ht, bool, S_IRUGO);
  51. MODULE_PARM_DESC(ht, "enable MPDU aggregation.");
  52. #define RATE(_bitrate, _hw_rate, _txpidx, _flags) { \
  53. .bitrate = (_bitrate), \
  54. .flags = (_flags), \
  55. .hw_value = (_hw_rate) | (_txpidx) << 4, \
  56. }
  57. static struct ieee80211_rate __ar9170_ratetable[] = {
  58. RATE(10, 0, 0, 0),
  59. RATE(20, 1, 1, IEEE80211_RATE_SHORT_PREAMBLE),
  60. RATE(55, 2, 2, IEEE80211_RATE_SHORT_PREAMBLE),
  61. RATE(110, 3, 3, IEEE80211_RATE_SHORT_PREAMBLE),
  62. RATE(60, 0xb, 0, 0),
  63. RATE(90, 0xf, 0, 0),
  64. RATE(120, 0xa, 0, 0),
  65. RATE(180, 0xe, 0, 0),
  66. RATE(240, 0x9, 0, 0),
  67. RATE(360, 0xd, 1, 0),
  68. RATE(480, 0x8, 2, 0),
  69. RATE(540, 0xc, 3, 0),
  70. };
  71. #undef RATE
  72. #define ar9170_g_ratetable (__ar9170_ratetable + 0)
  73. #define ar9170_g_ratetable_size 12
  74. #define ar9170_a_ratetable (__ar9170_ratetable + 4)
  75. #define ar9170_a_ratetable_size 8
  76. /*
  77. * NB: The hw_value is used as an index into the ar9170_phy_freq_params
  78. * array in phy.c so that we don't have to do frequency lookups!
  79. */
  80. #define CHAN(_freq, _idx) { \
  81. .center_freq = (_freq), \
  82. .hw_value = (_idx), \
  83. .max_power = 18, /* XXX */ \
  84. }
  85. static struct ieee80211_channel ar9170_2ghz_chantable[] = {
  86. CHAN(2412, 0),
  87. CHAN(2417, 1),
  88. CHAN(2422, 2),
  89. CHAN(2427, 3),
  90. CHAN(2432, 4),
  91. CHAN(2437, 5),
  92. CHAN(2442, 6),
  93. CHAN(2447, 7),
  94. CHAN(2452, 8),
  95. CHAN(2457, 9),
  96. CHAN(2462, 10),
  97. CHAN(2467, 11),
  98. CHAN(2472, 12),
  99. CHAN(2484, 13),
  100. };
  101. static struct ieee80211_channel ar9170_5ghz_chantable[] = {
  102. CHAN(4920, 14),
  103. CHAN(4940, 15),
  104. CHAN(4960, 16),
  105. CHAN(4980, 17),
  106. CHAN(5040, 18),
  107. CHAN(5060, 19),
  108. CHAN(5080, 20),
  109. CHAN(5180, 21),
  110. CHAN(5200, 22),
  111. CHAN(5220, 23),
  112. CHAN(5240, 24),
  113. CHAN(5260, 25),
  114. CHAN(5280, 26),
  115. CHAN(5300, 27),
  116. CHAN(5320, 28),
  117. CHAN(5500, 29),
  118. CHAN(5520, 30),
  119. CHAN(5540, 31),
  120. CHAN(5560, 32),
  121. CHAN(5580, 33),
  122. CHAN(5600, 34),
  123. CHAN(5620, 35),
  124. CHAN(5640, 36),
  125. CHAN(5660, 37),
  126. CHAN(5680, 38),
  127. CHAN(5700, 39),
  128. CHAN(5745, 40),
  129. CHAN(5765, 41),
  130. CHAN(5785, 42),
  131. CHAN(5805, 43),
  132. CHAN(5825, 44),
  133. CHAN(5170, 45),
  134. CHAN(5190, 46),
  135. CHAN(5210, 47),
  136. CHAN(5230, 48),
  137. };
  138. #undef CHAN
  139. #define AR9170_HT_CAP \
  140. { \
  141. .ht_supported = true, \
  142. .cap = IEEE80211_HT_CAP_MAX_AMSDU | \
  143. IEEE80211_HT_CAP_SUP_WIDTH_20_40 | \
  144. IEEE80211_HT_CAP_SGI_40 | \
  145. IEEE80211_HT_CAP_GRN_FLD | \
  146. IEEE80211_HT_CAP_DSSSCCK40 | \
  147. IEEE80211_HT_CAP_SM_PS, \
  148. .ampdu_factor = 3, \
  149. .ampdu_density = 6, \
  150. .mcs = { \
  151. .rx_mask = { 0xff, 0xff, 0, 0, 0x1, 0, 0, 0, 0, 0, }, \
  152. .rx_highest = cpu_to_le16(300), \
  153. .tx_params = IEEE80211_HT_MCS_TX_DEFINED, \
  154. }, \
  155. }
  156. static struct ieee80211_supported_band ar9170_band_2GHz = {
  157. .channels = ar9170_2ghz_chantable,
  158. .n_channels = ARRAY_SIZE(ar9170_2ghz_chantable),
  159. .bitrates = ar9170_g_ratetable,
  160. .n_bitrates = ar9170_g_ratetable_size,
  161. .ht_cap = AR9170_HT_CAP,
  162. };
  163. static struct ieee80211_supported_band ar9170_band_5GHz = {
  164. .channels = ar9170_5ghz_chantable,
  165. .n_channels = ARRAY_SIZE(ar9170_5ghz_chantable),
  166. .bitrates = ar9170_a_ratetable,
  167. .n_bitrates = ar9170_a_ratetable_size,
  168. .ht_cap = AR9170_HT_CAP,
  169. };
  170. static void ar9170_tx(struct ar9170 *ar);
  171. static bool ar9170_tx_ampdu(struct ar9170 *ar);
  172. static inline u16 ar9170_get_seq_h(struct ieee80211_hdr *hdr)
  173. {
  174. return le16_to_cpu(hdr->seq_ctrl) >> 4;
  175. }
  176. static inline u16 ar9170_get_seq(struct sk_buff *skb)
  177. {
  178. struct ar9170_tx_control *txc = (void *) skb->data;
  179. return ar9170_get_seq_h((void *) txc->frame_data);
  180. }
  181. static inline u16 ar9170_get_tid_h(struct ieee80211_hdr *hdr)
  182. {
  183. return (ieee80211_get_qos_ctl(hdr))[0] & IEEE80211_QOS_CTL_TID_MASK;
  184. }
  185. static inline u16 ar9170_get_tid(struct sk_buff *skb)
  186. {
  187. struct ar9170_tx_control *txc = (void *) skb->data;
  188. return ar9170_get_tid_h((struct ieee80211_hdr *) txc->frame_data);
  189. }
  190. #define GET_NEXT_SEQ(seq) ((seq + 1) & 0x0fff)
  191. #define GET_NEXT_SEQ_FROM_SKB(skb) (GET_NEXT_SEQ(ar9170_get_seq(skb)))
  192. #if (defined AR9170_QUEUE_DEBUG) || (defined AR9170_TXAGG_DEBUG)
  193. static void ar9170_print_txheader(struct ar9170 *ar, struct sk_buff *skb)
  194. {
  195. struct ar9170_tx_control *txc = (void *) skb->data;
  196. struct ieee80211_tx_info *txinfo = IEEE80211_SKB_CB(skb);
  197. struct ar9170_tx_info *arinfo = (void *) txinfo->rate_driver_data;
  198. struct ieee80211_hdr *hdr = (void *) txc->frame_data;
  199. printk(KERN_DEBUG "%s: => FRAME [skb:%p, q:%d, DA:[%pM] s:%d "
  200. "mac_ctrl:%04x, phy_ctrl:%08x, timeout:[%d ms]]\n",
  201. wiphy_name(ar->hw->wiphy), skb, skb_get_queue_mapping(skb),
  202. ieee80211_get_DA(hdr), ar9170_get_seq_h(hdr),
  203. le16_to_cpu(txc->mac_control), le32_to_cpu(txc->phy_control),
  204. jiffies_to_msecs(arinfo->timeout - jiffies));
  205. }
  206. static void __ar9170_dump_txqueue(struct ar9170 *ar,
  207. struct sk_buff_head *queue)
  208. {
  209. struct sk_buff *skb;
  210. int i = 0;
  211. printk(KERN_DEBUG "---[ cut here ]---\n");
  212. printk(KERN_DEBUG "%s: %d entries in queue.\n",
  213. wiphy_name(ar->hw->wiphy), skb_queue_len(queue));
  214. skb_queue_walk(queue, skb) {
  215. printk(KERN_DEBUG "index:%d => \n", i++);
  216. ar9170_print_txheader(ar, skb);
  217. }
  218. if (i != skb_queue_len(queue))
  219. printk(KERN_DEBUG "WARNING: queue frame counter "
  220. "mismatch %d != %d\n", skb_queue_len(queue), i);
  221. printk(KERN_DEBUG "---[ end ]---\n");
  222. }
  223. #endif /* AR9170_QUEUE_DEBUG || AR9170_TXAGG_DEBUG */
  224. #ifdef AR9170_QUEUE_DEBUG
  225. static void ar9170_dump_txqueue(struct ar9170 *ar,
  226. struct sk_buff_head *queue)
  227. {
  228. unsigned long flags;
  229. spin_lock_irqsave(&queue->lock, flags);
  230. __ar9170_dump_txqueue(ar, queue);
  231. spin_unlock_irqrestore(&queue->lock, flags);
  232. }
  233. #endif /* AR9170_QUEUE_DEBUG */
  234. #ifdef AR9170_QUEUE_STOP_DEBUG
  235. static void __ar9170_dump_txstats(struct ar9170 *ar)
  236. {
  237. int i;
  238. printk(KERN_DEBUG "%s: QoS queue stats\n",
  239. wiphy_name(ar->hw->wiphy));
  240. for (i = 0; i < __AR9170_NUM_TXQ; i++)
  241. printk(KERN_DEBUG "%s: queue:%d limit:%d len:%d waitack:%d "
  242. " stopped:%d\n", wiphy_name(ar->hw->wiphy), i,
  243. ar->tx_stats[i].limit, ar->tx_stats[i].len,
  244. skb_queue_len(&ar->tx_status[i]),
  245. ieee80211_queue_stopped(ar->hw, i));
  246. }
  247. #endif /* AR9170_QUEUE_STOP_DEBUG */
  248. #ifdef AR9170_TXAGG_DEBUG
  249. static void ar9170_dump_tx_status_ampdu(struct ar9170 *ar)
  250. {
  251. unsigned long flags;
  252. spin_lock_irqsave(&ar->tx_status_ampdu.lock, flags);
  253. printk(KERN_DEBUG "%s: A-MPDU tx_status queue => \n",
  254. wiphy_name(ar->hw->wiphy));
  255. __ar9170_dump_txqueue(ar, &ar->tx_status_ampdu);
  256. spin_unlock_irqrestore(&ar->tx_status_ampdu.lock, flags);
  257. }
  258. #endif /* AR9170_TXAGG_DEBUG */
  259. /* caller must guarantee exclusive access for _bin_ queue. */
  260. static void ar9170_recycle_expired(struct ar9170 *ar,
  261. struct sk_buff_head *queue,
  262. struct sk_buff_head *bin)
  263. {
  264. struct sk_buff *skb, *old = NULL;
  265. unsigned long flags;
  266. spin_lock_irqsave(&queue->lock, flags);
  267. while ((skb = skb_peek(queue))) {
  268. struct ieee80211_tx_info *txinfo;
  269. struct ar9170_tx_info *arinfo;
  270. txinfo = IEEE80211_SKB_CB(skb);
  271. arinfo = (void *) txinfo->rate_driver_data;
  272. if (time_is_before_jiffies(arinfo->timeout)) {
  273. #ifdef AR9170_QUEUE_DEBUG
  274. printk(KERN_DEBUG "%s: [%ld > %ld] frame expired => "
  275. "recycle \n", wiphy_name(ar->hw->wiphy),
  276. jiffies, arinfo->timeout);
  277. ar9170_print_txheader(ar, skb);
  278. #endif /* AR9170_QUEUE_DEBUG */
  279. __skb_unlink(skb, queue);
  280. __skb_queue_tail(bin, skb);
  281. } else {
  282. break;
  283. }
  284. if (unlikely(old == skb)) {
  285. /* bail out - queue is shot. */
  286. WARN_ON(1);
  287. break;
  288. }
  289. old = skb;
  290. }
  291. spin_unlock_irqrestore(&queue->lock, flags);
  292. }
  293. static void ar9170_tx_status(struct ar9170 *ar, struct sk_buff *skb,
  294. u16 tx_status)
  295. {
  296. struct ieee80211_tx_info *txinfo;
  297. unsigned int retries = 0;
  298. txinfo = IEEE80211_SKB_CB(skb);
  299. ieee80211_tx_info_clear_status(txinfo);
  300. switch (tx_status) {
  301. case AR9170_TX_STATUS_RETRY:
  302. retries = 2;
  303. case AR9170_TX_STATUS_COMPLETE:
  304. txinfo->flags |= IEEE80211_TX_STAT_ACK;
  305. break;
  306. case AR9170_TX_STATUS_FAILED:
  307. retries = ar->hw->conf.long_frame_max_tx_count;
  308. break;
  309. default:
  310. printk(KERN_ERR "%s: invalid tx_status response (%x).\n",
  311. wiphy_name(ar->hw->wiphy), tx_status);
  312. break;
  313. }
  314. txinfo->status.rates[0].count = retries + 1;
  315. skb_pull(skb, sizeof(struct ar9170_tx_control));
  316. ieee80211_tx_status_irqsafe(ar->hw, skb);
  317. }
  318. static void ar9170_tx_fake_ampdu_status(struct ar9170 *ar)
  319. {
  320. struct sk_buff_head success;
  321. struct sk_buff *skb;
  322. unsigned int i;
  323. unsigned long queue_bitmap = 0;
  324. skb_queue_head_init(&success);
  325. while (skb_queue_len(&ar->tx_status_ampdu) > AR9170_NUM_TX_STATUS)
  326. __skb_queue_tail(&success, skb_dequeue(&ar->tx_status_ampdu));
  327. ar9170_recycle_expired(ar, &ar->tx_status_ampdu, &success);
  328. #ifdef AR9170_TXAGG_DEBUG
  329. printk(KERN_DEBUG "%s: collected %d A-MPDU frames.\n",
  330. wiphy_name(ar->hw->wiphy), skb_queue_len(&success));
  331. __ar9170_dump_txqueue(ar, &success);
  332. #endif /* AR9170_TXAGG_DEBUG */
  333. while ((skb = __skb_dequeue(&success))) {
  334. struct ieee80211_tx_info *txinfo;
  335. queue_bitmap |= BIT(skb_get_queue_mapping(skb));
  336. txinfo = IEEE80211_SKB_CB(skb);
  337. ieee80211_tx_info_clear_status(txinfo);
  338. txinfo->flags |= IEEE80211_TX_STAT_ACK;
  339. txinfo->status.rates[0].count = 1;
  340. skb_pull(skb, sizeof(struct ar9170_tx_control));
  341. ieee80211_tx_status_irqsafe(ar->hw, skb);
  342. }
  343. for_each_bit(i, &queue_bitmap, BITS_PER_BYTE) {
  344. #ifdef AR9170_QUEUE_STOP_DEBUG
  345. printk(KERN_DEBUG "%s: wake queue %d\n",
  346. wiphy_name(ar->hw->wiphy), i);
  347. __ar9170_dump_txstats(ar);
  348. #endif /* AR9170_QUEUE_STOP_DEBUG */
  349. ieee80211_wake_queue(ar->hw, i);
  350. }
  351. if (queue_bitmap)
  352. ar9170_tx(ar);
  353. }
  354. static void ar9170_tx_ampdu_callback(struct ar9170 *ar, struct sk_buff *skb)
  355. {
  356. struct ieee80211_tx_info *txinfo = IEEE80211_SKB_CB(skb);
  357. struct ar9170_tx_info *arinfo = (void *) txinfo->rate_driver_data;
  358. arinfo->timeout = jiffies +
  359. msecs_to_jiffies(AR9170_BA_TIMEOUT);
  360. skb_queue_tail(&ar->tx_status_ampdu, skb);
  361. ar9170_tx_fake_ampdu_status(ar);
  362. if (atomic_dec_and_test(&ar->tx_ampdu_pending) &&
  363. !list_empty(&ar->tx_ampdu_list))
  364. ar9170_tx_ampdu(ar);
  365. }
  366. void ar9170_tx_callback(struct ar9170 *ar, struct sk_buff *skb)
  367. {
  368. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  369. struct ar9170_tx_info *arinfo = (void *) info->rate_driver_data;
  370. unsigned int queue = skb_get_queue_mapping(skb);
  371. unsigned long flags;
  372. spin_lock_irqsave(&ar->tx_stats_lock, flags);
  373. ar->tx_stats[queue].len--;
  374. if (ar->tx_stats[queue].len < AR9170_NUM_TX_LIMIT_SOFT) {
  375. #ifdef AR9170_QUEUE_STOP_DEBUG
  376. printk(KERN_DEBUG "%s: wake queue %d\n",
  377. wiphy_name(ar->hw->wiphy), queue);
  378. __ar9170_dump_txstats(ar);
  379. #endif /* AR9170_QUEUE_STOP_DEBUG */
  380. ieee80211_wake_queue(ar->hw, queue);
  381. }
  382. spin_unlock_irqrestore(&ar->tx_stats_lock, flags);
  383. if (info->flags & IEEE80211_TX_CTL_NO_ACK) {
  384. ar9170_tx_status(ar, skb, AR9170_TX_STATUS_FAILED);
  385. } else {
  386. if (info->flags & IEEE80211_TX_CTL_AMPDU) {
  387. ar9170_tx_ampdu_callback(ar, skb);
  388. } else {
  389. arinfo->timeout = jiffies +
  390. msecs_to_jiffies(AR9170_TX_TIMEOUT);
  391. skb_queue_tail(&ar->tx_status[queue], skb);
  392. }
  393. }
  394. if (!ar->tx_stats[queue].len &&
  395. !skb_queue_empty(&ar->tx_pending[queue])) {
  396. ar9170_tx(ar);
  397. }
  398. }
  399. static struct sk_buff *ar9170_get_queued_skb(struct ar9170 *ar,
  400. const u8 *mac,
  401. struct sk_buff_head *queue,
  402. const u32 rate)
  403. {
  404. unsigned long flags;
  405. struct sk_buff *skb;
  406. /*
  407. * Unfortunately, the firmware does not tell to which (queued) frame
  408. * this transmission status report belongs to.
  409. *
  410. * So we have to make risky guesses - with the scarce information
  411. * the firmware provided (-> destination MAC, and phy_control) -
  412. * and hope that we picked the right one...
  413. */
  414. spin_lock_irqsave(&queue->lock, flags);
  415. skb_queue_walk(queue, skb) {
  416. struct ar9170_tx_control *txc = (void *) skb->data;
  417. struct ieee80211_hdr *hdr = (void *) txc->frame_data;
  418. u32 r;
  419. if (mac && compare_ether_addr(ieee80211_get_DA(hdr), mac)) {
  420. #ifdef AR9170_QUEUE_DEBUG
  421. printk(KERN_DEBUG "%s: skip frame => DA %pM != %pM\n",
  422. wiphy_name(ar->hw->wiphy), mac,
  423. ieee80211_get_DA(hdr));
  424. ar9170_print_txheader(ar, skb);
  425. #endif /* AR9170_QUEUE_DEBUG */
  426. continue;
  427. }
  428. r = (le32_to_cpu(txc->phy_control) & AR9170_TX_PHY_MCS_MASK) >>
  429. AR9170_TX_PHY_MCS_SHIFT;
  430. if ((rate != AR9170_TX_INVALID_RATE) && (r != rate)) {
  431. #ifdef AR9170_QUEUE_DEBUG
  432. printk(KERN_DEBUG "%s: skip frame => rate %d != %d\n",
  433. wiphy_name(ar->hw->wiphy), rate, r);
  434. ar9170_print_txheader(ar, skb);
  435. #endif /* AR9170_QUEUE_DEBUG */
  436. continue;
  437. }
  438. __skb_unlink(skb, queue);
  439. spin_unlock_irqrestore(&queue->lock, flags);
  440. return skb;
  441. }
  442. #ifdef AR9170_QUEUE_DEBUG
  443. printk(KERN_ERR "%s: ESS:[%pM] does not have any "
  444. "outstanding frames in queue.\n",
  445. wiphy_name(ar->hw->wiphy), mac);
  446. __ar9170_dump_txqueue(ar, queue);
  447. #endif /* AR9170_QUEUE_DEBUG */
  448. spin_unlock_irqrestore(&queue->lock, flags);
  449. return NULL;
  450. }
  451. static void ar9170_handle_block_ack(struct ar9170 *ar, u16 count, u16 r)
  452. {
  453. struct sk_buff *skb;
  454. struct ieee80211_tx_info *txinfo;
  455. while (count) {
  456. skb = ar9170_get_queued_skb(ar, NULL, &ar->tx_status_ampdu, r);
  457. if (!skb)
  458. break;
  459. txinfo = IEEE80211_SKB_CB(skb);
  460. ieee80211_tx_info_clear_status(txinfo);
  461. /* FIXME: maybe more ? */
  462. txinfo->status.rates[0].count = 1;
  463. skb_pull(skb, sizeof(struct ar9170_tx_control));
  464. ieee80211_tx_status_irqsafe(ar->hw, skb);
  465. count--;
  466. }
  467. #ifdef AR9170_TXAGG_DEBUG
  468. if (count) {
  469. printk(KERN_DEBUG "%s: got %d more failed mpdus, but no more "
  470. "suitable frames left in tx_status queue.\n",
  471. wiphy_name(ar->hw->wiphy), count);
  472. ar9170_dump_tx_status_ampdu(ar);
  473. }
  474. #endif /* AR9170_TXAGG_DEBUG */
  475. }
  476. /*
  477. * This worker tries to keeps an maintain tx_status queues.
  478. * So we can guarantee that incoming tx_status reports are
  479. * actually for a pending frame.
  480. */
  481. static void ar9170_tx_janitor(struct work_struct *work)
  482. {
  483. struct ar9170 *ar = container_of(work, struct ar9170,
  484. tx_janitor.work);
  485. struct sk_buff_head waste;
  486. unsigned int i;
  487. bool resched = false;
  488. if (unlikely(!IS_STARTED(ar)))
  489. return ;
  490. skb_queue_head_init(&waste);
  491. for (i = 0; i < __AR9170_NUM_TXQ; i++) {
  492. #ifdef AR9170_QUEUE_DEBUG
  493. printk(KERN_DEBUG "%s: garbage collector scans queue:%d\n",
  494. wiphy_name(ar->hw->wiphy), i);
  495. ar9170_dump_txqueue(ar, &ar->tx_pending[i]);
  496. ar9170_dump_txqueue(ar, &ar->tx_status[i]);
  497. #endif /* AR9170_QUEUE_DEBUG */
  498. ar9170_recycle_expired(ar, &ar->tx_status[i], &waste);
  499. ar9170_recycle_expired(ar, &ar->tx_pending[i], &waste);
  500. skb_queue_purge(&waste);
  501. if (!skb_queue_empty(&ar->tx_status[i]) ||
  502. !skb_queue_empty(&ar->tx_pending[i]))
  503. resched = true;
  504. }
  505. ar9170_tx_fake_ampdu_status(ar);
  506. if (!resched)
  507. return;
  508. ieee80211_queue_delayed_work(ar->hw,
  509. &ar->tx_janitor,
  510. msecs_to_jiffies(AR9170_JANITOR_DELAY));
  511. }
  512. void ar9170_handle_command_response(struct ar9170 *ar, void *buf, u32 len)
  513. {
  514. struct ar9170_cmd_response *cmd = (void *) buf;
  515. if ((cmd->type & 0xc0) != 0xc0) {
  516. ar->callback_cmd(ar, len, buf);
  517. return;
  518. }
  519. /* hardware event handlers */
  520. switch (cmd->type) {
  521. case 0xc1: {
  522. /*
  523. * TX status notification:
  524. * bytes: 0c c1 XX YY M1 M2 M3 M4 M5 M6 R4 R3 R2 R1 S2 S1
  525. *
  526. * XX always 81
  527. * YY always 00
  528. * M1-M6 is the MAC address
  529. * R1-R4 is the transmit rate
  530. * S1-S2 is the transmit status
  531. */
  532. struct sk_buff *skb;
  533. u32 phy = le32_to_cpu(cmd->tx_status.rate);
  534. u32 q = (phy & AR9170_TX_PHY_QOS_MASK) >>
  535. AR9170_TX_PHY_QOS_SHIFT;
  536. #ifdef AR9170_QUEUE_DEBUG
  537. printk(KERN_DEBUG "%s: recv tx_status for %pM, p:%08x, q:%d\n",
  538. wiphy_name(ar->hw->wiphy), cmd->tx_status.dst, phy, q);
  539. #endif /* AR9170_QUEUE_DEBUG */
  540. skb = ar9170_get_queued_skb(ar, cmd->tx_status.dst,
  541. &ar->tx_status[q],
  542. AR9170_TX_INVALID_RATE);
  543. if (unlikely(!skb))
  544. return ;
  545. ar9170_tx_status(ar, skb, le16_to_cpu(cmd->tx_status.status));
  546. break;
  547. }
  548. case 0xc0:
  549. /*
  550. * pre-TBTT event
  551. */
  552. if (ar->vif && ar->vif->type == NL80211_IFTYPE_AP)
  553. ieee80211_queue_work(ar->hw, &ar->beacon_work);
  554. break;
  555. case 0xc2:
  556. /*
  557. * (IBSS) beacon send notification
  558. * bytes: 04 c2 XX YY B4 B3 B2 B1
  559. *
  560. * XX always 80
  561. * YY always 00
  562. * B1-B4 "should" be the number of send out beacons.
  563. */
  564. break;
  565. case 0xc3:
  566. /* End of Atim Window */
  567. break;
  568. case 0xc4:
  569. /* BlockACK bitmap */
  570. break;
  571. case 0xc5:
  572. /* BlockACK events */
  573. ar9170_handle_block_ack(ar,
  574. le16_to_cpu(cmd->ba_fail_cnt.failed),
  575. le16_to_cpu(cmd->ba_fail_cnt.rate));
  576. ar9170_tx_fake_ampdu_status(ar);
  577. break;
  578. case 0xc6:
  579. /* Watchdog Interrupt */
  580. break;
  581. case 0xc9:
  582. /* retransmission issue / SIFS/EIFS collision ?! */
  583. break;
  584. /* firmware debug */
  585. case 0xca:
  586. printk(KERN_DEBUG "ar9170 FW: %.*s\n", len - 4, (char *)buf + 4);
  587. break;
  588. case 0xcb:
  589. len -= 4;
  590. switch (len) {
  591. case 1:
  592. printk(KERN_DEBUG "ar9170 FW: u8: %#.2x\n",
  593. *((char *)buf + 4));
  594. break;
  595. case 2:
  596. printk(KERN_DEBUG "ar9170 FW: u8: %#.4x\n",
  597. le16_to_cpup((__le16 *)((char *)buf + 4)));
  598. break;
  599. case 4:
  600. printk(KERN_DEBUG "ar9170 FW: u8: %#.8x\n",
  601. le32_to_cpup((__le32 *)((char *)buf + 4)));
  602. break;
  603. case 8:
  604. printk(KERN_DEBUG "ar9170 FW: u8: %#.16lx\n",
  605. (unsigned long)le64_to_cpup(
  606. (__le64 *)((char *)buf + 4)));
  607. break;
  608. }
  609. break;
  610. case 0xcc:
  611. print_hex_dump_bytes("ar9170 FW:", DUMP_PREFIX_NONE,
  612. (char *)buf + 4, len - 4);
  613. break;
  614. default:
  615. printk(KERN_INFO "received unhandled event %x\n", cmd->type);
  616. print_hex_dump_bytes("dump:", DUMP_PREFIX_NONE, buf, len);
  617. break;
  618. }
  619. }
  620. static void ar9170_rx_reset_rx_mpdu(struct ar9170 *ar)
  621. {
  622. memset(&ar->rx_mpdu.plcp, 0, sizeof(struct ar9170_rx_head));
  623. ar->rx_mpdu.has_plcp = false;
  624. }
  625. int ar9170_nag_limiter(struct ar9170 *ar)
  626. {
  627. bool print_message;
  628. /*
  629. * we expect all sorts of errors in promiscuous mode.
  630. * don't bother with it, it's OK!
  631. */
  632. if (ar->sniffer_enabled)
  633. return false;
  634. /*
  635. * only go for frequent errors! The hardware tends to
  636. * do some stupid thing once in a while under load, in
  637. * noisy environments or just for fun!
  638. */
  639. if (time_before(jiffies, ar->bad_hw_nagger) && net_ratelimit())
  640. print_message = true;
  641. else
  642. print_message = false;
  643. /* reset threshold for "once in a while" */
  644. ar->bad_hw_nagger = jiffies + HZ / 4;
  645. return print_message;
  646. }
  647. static int ar9170_rx_mac_status(struct ar9170 *ar,
  648. struct ar9170_rx_head *head,
  649. struct ar9170_rx_macstatus *mac,
  650. struct ieee80211_rx_status *status)
  651. {
  652. u8 error, decrypt;
  653. BUILD_BUG_ON(sizeof(struct ar9170_rx_head) != 12);
  654. BUILD_BUG_ON(sizeof(struct ar9170_rx_macstatus) != 4);
  655. error = mac->error;
  656. if (error & AR9170_RX_ERROR_MMIC) {
  657. status->flag |= RX_FLAG_MMIC_ERROR;
  658. error &= ~AR9170_RX_ERROR_MMIC;
  659. }
  660. if (error & AR9170_RX_ERROR_PLCP) {
  661. status->flag |= RX_FLAG_FAILED_PLCP_CRC;
  662. error &= ~AR9170_RX_ERROR_PLCP;
  663. if (!(ar->filter_state & FIF_PLCPFAIL))
  664. return -EINVAL;
  665. }
  666. if (error & AR9170_RX_ERROR_FCS) {
  667. status->flag |= RX_FLAG_FAILED_FCS_CRC;
  668. error &= ~AR9170_RX_ERROR_FCS;
  669. if (!(ar->filter_state & FIF_FCSFAIL))
  670. return -EINVAL;
  671. }
  672. decrypt = ar9170_get_decrypt_type(mac);
  673. if (!(decrypt & AR9170_RX_ENC_SOFTWARE) &&
  674. decrypt != AR9170_ENC_ALG_NONE)
  675. status->flag |= RX_FLAG_DECRYPTED;
  676. /* ignore wrong RA errors */
  677. error &= ~AR9170_RX_ERROR_WRONG_RA;
  678. if (error & AR9170_RX_ERROR_DECRYPT) {
  679. error &= ~AR9170_RX_ERROR_DECRYPT;
  680. /*
  681. * Rx decryption is done in place,
  682. * the original data is lost anyway.
  683. */
  684. return -EINVAL;
  685. }
  686. /* drop any other error frames */
  687. if (unlikely(error)) {
  688. /* TODO: update netdevice's RX dropped/errors statistics */
  689. if (ar9170_nag_limiter(ar))
  690. printk(KERN_DEBUG "%s: received frame with "
  691. "suspicious error code (%#x).\n",
  692. wiphy_name(ar->hw->wiphy), error);
  693. return -EINVAL;
  694. }
  695. status->band = ar->channel->band;
  696. status->freq = ar->channel->center_freq;
  697. switch (mac->status & AR9170_RX_STATUS_MODULATION_MASK) {
  698. case AR9170_RX_STATUS_MODULATION_CCK:
  699. if (mac->status & AR9170_RX_STATUS_SHORT_PREAMBLE)
  700. status->flag |= RX_FLAG_SHORTPRE;
  701. switch (head->plcp[0]) {
  702. case 0x0a:
  703. status->rate_idx = 0;
  704. break;
  705. case 0x14:
  706. status->rate_idx = 1;
  707. break;
  708. case 0x37:
  709. status->rate_idx = 2;
  710. break;
  711. case 0x6e:
  712. status->rate_idx = 3;
  713. break;
  714. default:
  715. if (ar9170_nag_limiter(ar))
  716. printk(KERN_ERR "%s: invalid plcp cck rate "
  717. "(%x).\n", wiphy_name(ar->hw->wiphy),
  718. head->plcp[0]);
  719. return -EINVAL;
  720. }
  721. break;
  722. case AR9170_RX_STATUS_MODULATION_DUPOFDM:
  723. case AR9170_RX_STATUS_MODULATION_OFDM:
  724. switch (head->plcp[0] & 0xf) {
  725. case 0xb:
  726. status->rate_idx = 0;
  727. break;
  728. case 0xf:
  729. status->rate_idx = 1;
  730. break;
  731. case 0xa:
  732. status->rate_idx = 2;
  733. break;
  734. case 0xe:
  735. status->rate_idx = 3;
  736. break;
  737. case 0x9:
  738. status->rate_idx = 4;
  739. break;
  740. case 0xd:
  741. status->rate_idx = 5;
  742. break;
  743. case 0x8:
  744. status->rate_idx = 6;
  745. break;
  746. case 0xc:
  747. status->rate_idx = 7;
  748. break;
  749. default:
  750. if (ar9170_nag_limiter(ar))
  751. printk(KERN_ERR "%s: invalid plcp ofdm rate "
  752. "(%x).\n", wiphy_name(ar->hw->wiphy),
  753. head->plcp[0]);
  754. return -EINVAL;
  755. }
  756. if (status->band == IEEE80211_BAND_2GHZ)
  757. status->rate_idx += 4;
  758. break;
  759. case AR9170_RX_STATUS_MODULATION_HT:
  760. if (head->plcp[3] & 0x80)
  761. status->flag |= RX_FLAG_40MHZ;
  762. if (head->plcp[6] & 0x80)
  763. status->flag |= RX_FLAG_SHORT_GI;
  764. status->rate_idx = clamp(0, 75, head->plcp[6] & 0x7f);
  765. status->flag |= RX_FLAG_HT;
  766. break;
  767. default:
  768. if (ar9170_nag_limiter(ar))
  769. printk(KERN_ERR "%s: invalid modulation\n",
  770. wiphy_name(ar->hw->wiphy));
  771. return -EINVAL;
  772. }
  773. return 0;
  774. }
  775. static void ar9170_rx_phy_status(struct ar9170 *ar,
  776. struct ar9170_rx_phystatus *phy,
  777. struct ieee80211_rx_status *status)
  778. {
  779. int i;
  780. BUILD_BUG_ON(sizeof(struct ar9170_rx_phystatus) != 20);
  781. for (i = 0; i < 3; i++)
  782. if (phy->rssi[i] != 0x80)
  783. status->antenna |= BIT(i);
  784. /* post-process RSSI */
  785. for (i = 0; i < 7; i++)
  786. if (phy->rssi[i] & 0x80)
  787. phy->rssi[i] = ((phy->rssi[i] & 0x7f) + 1) & 0x7f;
  788. /* TODO: we could do something with phy_errors */
  789. status->signal = ar->noise[0] + phy->rssi_combined;
  790. status->noise = ar->noise[0];
  791. }
  792. static struct sk_buff *ar9170_rx_copy_data(u8 *buf, int len)
  793. {
  794. struct sk_buff *skb;
  795. int reserved = 0;
  796. struct ieee80211_hdr *hdr = (void *) buf;
  797. if (ieee80211_is_data_qos(hdr->frame_control)) {
  798. u8 *qc = ieee80211_get_qos_ctl(hdr);
  799. reserved += NET_IP_ALIGN;
  800. if (*qc & IEEE80211_QOS_CONTROL_A_MSDU_PRESENT)
  801. reserved += NET_IP_ALIGN;
  802. }
  803. if (ieee80211_has_a4(hdr->frame_control))
  804. reserved += NET_IP_ALIGN;
  805. reserved = 32 + (reserved & NET_IP_ALIGN);
  806. skb = dev_alloc_skb(len + reserved);
  807. if (likely(skb)) {
  808. skb_reserve(skb, reserved);
  809. memcpy(skb_put(skb, len), buf, len);
  810. }
  811. return skb;
  812. }
  813. /*
  814. * If the frame alignment is right (or the kernel has
  815. * CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS), and there
  816. * is only a single MPDU in the USB frame, then we could
  817. * submit to mac80211 the SKB directly. However, since
  818. * there may be multiple packets in one SKB in stream
  819. * mode, and we need to observe the proper ordering,
  820. * this is non-trivial.
  821. */
  822. static void ar9170_handle_mpdu(struct ar9170 *ar, u8 *buf, int len)
  823. {
  824. struct ar9170_rx_head *head;
  825. struct ar9170_rx_macstatus *mac;
  826. struct ar9170_rx_phystatus *phy = NULL;
  827. struct ieee80211_rx_status status;
  828. struct sk_buff *skb;
  829. int mpdu_len;
  830. if (unlikely(!IS_STARTED(ar) || len < (sizeof(*mac))))
  831. return ;
  832. /* Received MPDU */
  833. mpdu_len = len - sizeof(*mac);
  834. mac = (void *)(buf + mpdu_len);
  835. if (unlikely(mac->error & AR9170_RX_ERROR_FATAL)) {
  836. /* this frame is too damaged and can't be used - drop it */
  837. return ;
  838. }
  839. switch (mac->status & AR9170_RX_STATUS_MPDU_MASK) {
  840. case AR9170_RX_STATUS_MPDU_FIRST:
  841. /* first mpdu packet has the plcp header */
  842. if (likely(mpdu_len >= sizeof(struct ar9170_rx_head))) {
  843. head = (void *) buf;
  844. memcpy(&ar->rx_mpdu.plcp, (void *) buf,
  845. sizeof(struct ar9170_rx_head));
  846. mpdu_len -= sizeof(struct ar9170_rx_head);
  847. buf += sizeof(struct ar9170_rx_head);
  848. ar->rx_mpdu.has_plcp = true;
  849. } else {
  850. if (ar9170_nag_limiter(ar))
  851. printk(KERN_ERR "%s: plcp info is clipped.\n",
  852. wiphy_name(ar->hw->wiphy));
  853. return ;
  854. }
  855. break;
  856. case AR9170_RX_STATUS_MPDU_LAST:
  857. /* last mpdu has a extra tail with phy status information */
  858. if (likely(mpdu_len >= sizeof(struct ar9170_rx_phystatus))) {
  859. mpdu_len -= sizeof(struct ar9170_rx_phystatus);
  860. phy = (void *)(buf + mpdu_len);
  861. } else {
  862. if (ar9170_nag_limiter(ar))
  863. printk(KERN_ERR "%s: frame tail is clipped.\n",
  864. wiphy_name(ar->hw->wiphy));
  865. return ;
  866. }
  867. case AR9170_RX_STATUS_MPDU_MIDDLE:
  868. /* middle mpdus are just data */
  869. if (unlikely(!ar->rx_mpdu.has_plcp)) {
  870. if (!ar9170_nag_limiter(ar))
  871. return ;
  872. printk(KERN_ERR "%s: rx stream did not start "
  873. "with a first_mpdu frame tag.\n",
  874. wiphy_name(ar->hw->wiphy));
  875. return ;
  876. }
  877. head = &ar->rx_mpdu.plcp;
  878. break;
  879. case AR9170_RX_STATUS_MPDU_SINGLE:
  880. /* single mpdu - has plcp (head) and phy status (tail) */
  881. head = (void *) buf;
  882. mpdu_len -= sizeof(struct ar9170_rx_head);
  883. mpdu_len -= sizeof(struct ar9170_rx_phystatus);
  884. buf += sizeof(struct ar9170_rx_head);
  885. phy = (void *)(buf + mpdu_len);
  886. break;
  887. default:
  888. BUG_ON(1);
  889. break;
  890. }
  891. if (unlikely(mpdu_len < FCS_LEN))
  892. return ;
  893. memset(&status, 0, sizeof(status));
  894. if (unlikely(ar9170_rx_mac_status(ar, head, mac, &status)))
  895. return ;
  896. if (phy)
  897. ar9170_rx_phy_status(ar, phy, &status);
  898. skb = ar9170_rx_copy_data(buf, mpdu_len);
  899. if (likely(skb)) {
  900. memcpy(IEEE80211_SKB_RXCB(skb), &status, sizeof(status));
  901. ieee80211_rx_irqsafe(ar->hw, skb);
  902. }
  903. }
  904. void ar9170_rx(struct ar9170 *ar, struct sk_buff *skb)
  905. {
  906. unsigned int i, tlen, resplen, wlen = 0, clen = 0;
  907. u8 *tbuf, *respbuf;
  908. tbuf = skb->data;
  909. tlen = skb->len;
  910. while (tlen >= 4) {
  911. clen = tbuf[1] << 8 | tbuf[0];
  912. wlen = ALIGN(clen, 4);
  913. /* check if this is stream has a valid tag.*/
  914. if (tbuf[2] != 0 || tbuf[3] != 0x4e) {
  915. /*
  916. * TODO: handle the highly unlikely event that the
  917. * corrupted stream has the TAG at the right position.
  918. */
  919. /* check if the frame can be repaired. */
  920. if (!ar->rx_failover_missing) {
  921. /* this is no "short read". */
  922. if (ar9170_nag_limiter(ar)) {
  923. printk(KERN_ERR "%s: missing tag!\n",
  924. wiphy_name(ar->hw->wiphy));
  925. goto err_telluser;
  926. } else
  927. goto err_silent;
  928. }
  929. if (ar->rx_failover_missing > tlen) {
  930. if (ar9170_nag_limiter(ar)) {
  931. printk(KERN_ERR "%s: possible multi "
  932. "stream corruption!\n",
  933. wiphy_name(ar->hw->wiphy));
  934. goto err_telluser;
  935. } else
  936. goto err_silent;
  937. }
  938. memcpy(skb_put(ar->rx_failover, tlen), tbuf, tlen);
  939. ar->rx_failover_missing -= tlen;
  940. if (ar->rx_failover_missing <= 0) {
  941. /*
  942. * nested ar9170_rx call!
  943. * termination is guranteed, even when the
  944. * combined frame also have a element with
  945. * a bad tag.
  946. */
  947. ar->rx_failover_missing = 0;
  948. ar9170_rx(ar, ar->rx_failover);
  949. skb_reset_tail_pointer(ar->rx_failover);
  950. skb_trim(ar->rx_failover, 0);
  951. }
  952. return ;
  953. }
  954. /* check if stream is clipped */
  955. if (wlen > tlen - 4) {
  956. if (ar->rx_failover_missing) {
  957. /* TODO: handle double stream corruption. */
  958. if (ar9170_nag_limiter(ar)) {
  959. printk(KERN_ERR "%s: double rx stream "
  960. "corruption!\n",
  961. wiphy_name(ar->hw->wiphy));
  962. goto err_telluser;
  963. } else
  964. goto err_silent;
  965. }
  966. /*
  967. * save incomplete data set.
  968. * the firmware will resend the missing bits when
  969. * the rx - descriptor comes round again.
  970. */
  971. memcpy(skb_put(ar->rx_failover, tlen), tbuf, tlen);
  972. ar->rx_failover_missing = clen - tlen;
  973. return ;
  974. }
  975. resplen = clen;
  976. respbuf = tbuf + 4;
  977. tbuf += wlen + 4;
  978. tlen -= wlen + 4;
  979. i = 0;
  980. /* weird thing, but this is the same in the original driver */
  981. while (resplen > 2 && i < 12 &&
  982. respbuf[0] == 0xff && respbuf[1] == 0xff) {
  983. i += 2;
  984. resplen -= 2;
  985. respbuf += 2;
  986. }
  987. if (resplen < 4)
  988. continue;
  989. /* found the 6 * 0xffff marker? */
  990. if (i == 12)
  991. ar9170_handle_command_response(ar, respbuf, resplen);
  992. else
  993. ar9170_handle_mpdu(ar, respbuf, clen);
  994. }
  995. if (tlen) {
  996. if (net_ratelimit())
  997. printk(KERN_ERR "%s: %d bytes of unprocessed "
  998. "data left in rx stream!\n",
  999. wiphy_name(ar->hw->wiphy), tlen);
  1000. goto err_telluser;
  1001. }
  1002. return ;
  1003. err_telluser:
  1004. printk(KERN_ERR "%s: damaged RX stream data [want:%d, "
  1005. "data:%d, rx:%d, pending:%d ]\n",
  1006. wiphy_name(ar->hw->wiphy), clen, wlen, tlen,
  1007. ar->rx_failover_missing);
  1008. if (ar->rx_failover_missing)
  1009. print_hex_dump_bytes("rxbuf:", DUMP_PREFIX_OFFSET,
  1010. ar->rx_failover->data,
  1011. ar->rx_failover->len);
  1012. print_hex_dump_bytes("stream:", DUMP_PREFIX_OFFSET,
  1013. skb->data, skb->len);
  1014. printk(KERN_ERR "%s: please check your hardware and cables, if "
  1015. "you see this message frequently.\n",
  1016. wiphy_name(ar->hw->wiphy));
  1017. err_silent:
  1018. if (ar->rx_failover_missing) {
  1019. skb_reset_tail_pointer(ar->rx_failover);
  1020. skb_trim(ar->rx_failover, 0);
  1021. ar->rx_failover_missing = 0;
  1022. }
  1023. }
  1024. #define AR9170_FILL_QUEUE(queue, ai_fs, cwmin, cwmax, _txop) \
  1025. do { \
  1026. queue.aifs = ai_fs; \
  1027. queue.cw_min = cwmin; \
  1028. queue.cw_max = cwmax; \
  1029. queue.txop = _txop; \
  1030. } while (0)
  1031. static int ar9170_op_start(struct ieee80211_hw *hw)
  1032. {
  1033. struct ar9170 *ar = hw->priv;
  1034. int err, i;
  1035. mutex_lock(&ar->mutex);
  1036. /* reinitialize queues statistics */
  1037. memset(&ar->tx_stats, 0, sizeof(ar->tx_stats));
  1038. for (i = 0; i < __AR9170_NUM_TXQ; i++)
  1039. ar->tx_stats[i].limit = AR9170_TXQ_DEPTH;
  1040. /* reset QoS defaults */
  1041. AR9170_FILL_QUEUE(ar->edcf[0], 3, 15, 1023, 0); /* BEST EFFORT*/
  1042. AR9170_FILL_QUEUE(ar->edcf[1], 7, 15, 1023, 0); /* BACKGROUND */
  1043. AR9170_FILL_QUEUE(ar->edcf[2], 2, 7, 15, 94); /* VIDEO */
  1044. AR9170_FILL_QUEUE(ar->edcf[3], 2, 3, 7, 47); /* VOICE */
  1045. AR9170_FILL_QUEUE(ar->edcf[4], 2, 3, 7, 0); /* SPECIAL */
  1046. /* set sane AMPDU defaults */
  1047. ar->global_ampdu_density = 6;
  1048. ar->global_ampdu_factor = 3;
  1049. atomic_set(&ar->tx_ampdu_pending, 0);
  1050. ar->bad_hw_nagger = jiffies;
  1051. err = ar->open(ar);
  1052. if (err)
  1053. goto out;
  1054. err = ar9170_init_mac(ar);
  1055. if (err)
  1056. goto out;
  1057. err = ar9170_set_qos(ar);
  1058. if (err)
  1059. goto out;
  1060. err = ar9170_init_phy(ar, IEEE80211_BAND_2GHZ);
  1061. if (err)
  1062. goto out;
  1063. err = ar9170_init_rf(ar);
  1064. if (err)
  1065. goto out;
  1066. /* start DMA */
  1067. err = ar9170_write_reg(ar, 0x1c3d30, 0x100);
  1068. if (err)
  1069. goto out;
  1070. ar->state = AR9170_STARTED;
  1071. out:
  1072. mutex_unlock(&ar->mutex);
  1073. return err;
  1074. }
  1075. static void ar9170_op_stop(struct ieee80211_hw *hw)
  1076. {
  1077. struct ar9170 *ar = hw->priv;
  1078. unsigned int i;
  1079. if (IS_STARTED(ar))
  1080. ar->state = AR9170_IDLE;
  1081. cancel_delayed_work_sync(&ar->tx_janitor);
  1082. #ifdef CONFIG_AR9170_LEDS
  1083. cancel_delayed_work_sync(&ar->led_work);
  1084. #endif
  1085. cancel_work_sync(&ar->beacon_work);
  1086. mutex_lock(&ar->mutex);
  1087. if (IS_ACCEPTING_CMD(ar)) {
  1088. ar9170_set_leds_state(ar, 0);
  1089. /* stop DMA */
  1090. ar9170_write_reg(ar, 0x1c3d30, 0);
  1091. ar->stop(ar);
  1092. }
  1093. for (i = 0; i < __AR9170_NUM_TXQ; i++) {
  1094. skb_queue_purge(&ar->tx_pending[i]);
  1095. skb_queue_purge(&ar->tx_status[i]);
  1096. }
  1097. skb_queue_purge(&ar->tx_status_ampdu);
  1098. mutex_unlock(&ar->mutex);
  1099. }
  1100. static void ar9170_tx_indicate_immba(struct ar9170 *ar, struct sk_buff *skb)
  1101. {
  1102. struct ar9170_tx_control *txc = (void *) skb->data;
  1103. txc->mac_control |= cpu_to_le16(AR9170_TX_MAC_IMM_AMPDU);
  1104. }
  1105. static void ar9170_tx_copy_phy(struct ar9170 *ar, struct sk_buff *dst,
  1106. struct sk_buff *src)
  1107. {
  1108. struct ar9170_tx_control *dst_txc, *src_txc;
  1109. struct ieee80211_tx_info *dst_info, *src_info;
  1110. struct ar9170_tx_info *dst_arinfo, *src_arinfo;
  1111. src_txc = (void *) src->data;
  1112. src_info = IEEE80211_SKB_CB(src);
  1113. src_arinfo = (void *) src_info->rate_driver_data;
  1114. dst_txc = (void *) dst->data;
  1115. dst_info = IEEE80211_SKB_CB(dst);
  1116. dst_arinfo = (void *) dst_info->rate_driver_data;
  1117. dst_txc->phy_control = src_txc->phy_control;
  1118. /* same MCS for the whole aggregate */
  1119. memcpy(dst_info->driver_rates, src_info->driver_rates,
  1120. sizeof(dst_info->driver_rates));
  1121. }
  1122. static int ar9170_tx_prepare(struct ar9170 *ar, struct sk_buff *skb)
  1123. {
  1124. struct ieee80211_hdr *hdr;
  1125. struct ar9170_tx_control *txc;
  1126. struct ieee80211_tx_info *info;
  1127. struct ieee80211_tx_rate *txrate;
  1128. struct ar9170_tx_info *arinfo;
  1129. unsigned int queue = skb_get_queue_mapping(skb);
  1130. u16 keytype = 0;
  1131. u16 len, icv = 0;
  1132. BUILD_BUG_ON(sizeof(*arinfo) > sizeof(info->rate_driver_data));
  1133. hdr = (void *)skb->data;
  1134. info = IEEE80211_SKB_CB(skb);
  1135. len = skb->len;
  1136. txc = (void *)skb_push(skb, sizeof(*txc));
  1137. if (info->control.hw_key) {
  1138. icv = info->control.hw_key->icv_len;
  1139. switch (info->control.hw_key->alg) {
  1140. case ALG_WEP:
  1141. keytype = AR9170_TX_MAC_ENCR_RC4;
  1142. break;
  1143. case ALG_TKIP:
  1144. keytype = AR9170_TX_MAC_ENCR_RC4;
  1145. break;
  1146. case ALG_CCMP:
  1147. keytype = AR9170_TX_MAC_ENCR_AES;
  1148. break;
  1149. default:
  1150. WARN_ON(1);
  1151. goto err_out;
  1152. }
  1153. }
  1154. /* Length */
  1155. txc->length = cpu_to_le16(len + icv + 4);
  1156. txc->mac_control = cpu_to_le16(AR9170_TX_MAC_HW_DURATION |
  1157. AR9170_TX_MAC_BACKOFF);
  1158. txc->mac_control |= cpu_to_le16(ar9170_qos_hwmap[queue] <<
  1159. AR9170_TX_MAC_QOS_SHIFT);
  1160. txc->mac_control |= cpu_to_le16(keytype);
  1161. txc->phy_control = cpu_to_le32(0);
  1162. if (info->flags & IEEE80211_TX_CTL_NO_ACK)
  1163. txc->mac_control |= cpu_to_le16(AR9170_TX_MAC_NO_ACK);
  1164. txrate = &info->control.rates[0];
  1165. if (txrate->flags & IEEE80211_TX_RC_USE_CTS_PROTECT)
  1166. txc->mac_control |= cpu_to_le16(AR9170_TX_MAC_PROT_CTS);
  1167. else if (txrate->flags & IEEE80211_TX_RC_USE_RTS_CTS)
  1168. txc->mac_control |= cpu_to_le16(AR9170_TX_MAC_PROT_RTS);
  1169. arinfo = (void *)info->rate_driver_data;
  1170. arinfo->timeout = jiffies + msecs_to_jiffies(AR9170_QUEUE_TIMEOUT);
  1171. if (!(info->flags & IEEE80211_TX_CTL_NO_ACK) &&
  1172. (is_valid_ether_addr(ieee80211_get_DA(hdr)))) {
  1173. /*
  1174. * WARNING:
  1175. * Putting the QoS queue bits into an unexplored territory is
  1176. * certainly not elegant.
  1177. *
  1178. * In my defense: This idea provides a reasonable way to
  1179. * smuggle valuable information to the tx_status callback.
  1180. * Also, the idea behind this bit-abuse came straight from
  1181. * the original driver code.
  1182. */
  1183. txc->phy_control |=
  1184. cpu_to_le32(queue << AR9170_TX_PHY_QOS_SHIFT);
  1185. if (info->flags & IEEE80211_TX_CTL_AMPDU) {
  1186. if (unlikely(!info->control.sta))
  1187. goto err_out;
  1188. txc->mac_control |= cpu_to_le16(AR9170_TX_MAC_AGGR);
  1189. } else {
  1190. txc->mac_control |= cpu_to_le16(AR9170_TX_MAC_RATE_PROBE);
  1191. }
  1192. }
  1193. return 0;
  1194. err_out:
  1195. skb_pull(skb, sizeof(*txc));
  1196. return -EINVAL;
  1197. }
  1198. static void ar9170_tx_prepare_phy(struct ar9170 *ar, struct sk_buff *skb)
  1199. {
  1200. struct ar9170_tx_control *txc;
  1201. struct ieee80211_tx_info *info;
  1202. struct ieee80211_rate *rate = NULL;
  1203. struct ieee80211_tx_rate *txrate;
  1204. u32 power, chains;
  1205. txc = (void *) skb->data;
  1206. info = IEEE80211_SKB_CB(skb);
  1207. txrate = &info->control.rates[0];
  1208. if (txrate->flags & IEEE80211_TX_RC_GREEN_FIELD)
  1209. txc->phy_control |= cpu_to_le32(AR9170_TX_PHY_GREENFIELD);
  1210. if (txrate->flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
  1211. txc->phy_control |= cpu_to_le32(AR9170_TX_PHY_SHORT_PREAMBLE);
  1212. if (txrate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
  1213. txc->phy_control |= cpu_to_le32(AR9170_TX_PHY_BW_40MHZ);
  1214. /* this works because 40 MHz is 2 and dup is 3 */
  1215. if (txrate->flags & IEEE80211_TX_RC_DUP_DATA)
  1216. txc->phy_control |= cpu_to_le32(AR9170_TX_PHY_BW_40MHZ_DUP);
  1217. if (txrate->flags & IEEE80211_TX_RC_SHORT_GI)
  1218. txc->phy_control |= cpu_to_le32(AR9170_TX_PHY_SHORT_GI);
  1219. if (txrate->flags & IEEE80211_TX_RC_MCS) {
  1220. u32 r = txrate->idx;
  1221. u8 *txpower;
  1222. /* heavy clip control */
  1223. txc->phy_control |= cpu_to_le32((r & 0x7) << 7);
  1224. r <<= AR9170_TX_PHY_MCS_SHIFT;
  1225. BUG_ON(r & ~AR9170_TX_PHY_MCS_MASK);
  1226. txc->phy_control |= cpu_to_le32(r & AR9170_TX_PHY_MCS_MASK);
  1227. txc->phy_control |= cpu_to_le32(AR9170_TX_PHY_MOD_HT);
  1228. if (txrate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH) {
  1229. if (info->band == IEEE80211_BAND_5GHZ)
  1230. txpower = ar->power_5G_ht40;
  1231. else
  1232. txpower = ar->power_2G_ht40;
  1233. } else {
  1234. if (info->band == IEEE80211_BAND_5GHZ)
  1235. txpower = ar->power_5G_ht20;
  1236. else
  1237. txpower = ar->power_2G_ht20;
  1238. }
  1239. power = txpower[(txrate->idx) & 7];
  1240. } else {
  1241. u8 *txpower;
  1242. u32 mod;
  1243. u32 phyrate;
  1244. u8 idx = txrate->idx;
  1245. if (info->band != IEEE80211_BAND_2GHZ) {
  1246. idx += 4;
  1247. txpower = ar->power_5G_leg;
  1248. mod = AR9170_TX_PHY_MOD_OFDM;
  1249. } else {
  1250. if (idx < 4) {
  1251. txpower = ar->power_2G_cck;
  1252. mod = AR9170_TX_PHY_MOD_CCK;
  1253. } else {
  1254. mod = AR9170_TX_PHY_MOD_OFDM;
  1255. txpower = ar->power_2G_ofdm;
  1256. }
  1257. }
  1258. rate = &__ar9170_ratetable[idx];
  1259. phyrate = rate->hw_value & 0xF;
  1260. power = txpower[(rate->hw_value & 0x30) >> 4];
  1261. phyrate <<= AR9170_TX_PHY_MCS_SHIFT;
  1262. txc->phy_control |= cpu_to_le32(mod);
  1263. txc->phy_control |= cpu_to_le32(phyrate);
  1264. }
  1265. power <<= AR9170_TX_PHY_TX_PWR_SHIFT;
  1266. power &= AR9170_TX_PHY_TX_PWR_MASK;
  1267. txc->phy_control |= cpu_to_le32(power);
  1268. /* set TX chains */
  1269. if (ar->eeprom.tx_mask == 1) {
  1270. chains = AR9170_TX_PHY_TXCHAIN_1;
  1271. } else {
  1272. chains = AR9170_TX_PHY_TXCHAIN_2;
  1273. /* >= 36M legacy OFDM - use only one chain */
  1274. if (rate && rate->bitrate >= 360)
  1275. chains = AR9170_TX_PHY_TXCHAIN_1;
  1276. }
  1277. txc->phy_control |= cpu_to_le32(chains << AR9170_TX_PHY_TXCHAIN_SHIFT);
  1278. }
  1279. static bool ar9170_tx_ampdu(struct ar9170 *ar)
  1280. {
  1281. struct sk_buff_head agg;
  1282. struct ar9170_sta_tid *tid_info = NULL, *tmp;
  1283. struct sk_buff *skb, *first = NULL;
  1284. unsigned long flags, f2;
  1285. unsigned int i = 0;
  1286. u16 seq, queue, tmpssn;
  1287. bool run = false;
  1288. skb_queue_head_init(&agg);
  1289. spin_lock_irqsave(&ar->tx_ampdu_list_lock, flags);
  1290. if (list_empty(&ar->tx_ampdu_list)) {
  1291. #ifdef AR9170_TXAGG_DEBUG
  1292. printk(KERN_DEBUG "%s: aggregation list is empty.\n",
  1293. wiphy_name(ar->hw->wiphy));
  1294. #endif /* AR9170_TXAGG_DEBUG */
  1295. goto out_unlock;
  1296. }
  1297. list_for_each_entry_safe(tid_info, tmp, &ar->tx_ampdu_list, list) {
  1298. if (tid_info->state != AR9170_TID_STATE_COMPLETE) {
  1299. #ifdef AR9170_TXAGG_DEBUG
  1300. printk(KERN_DEBUG "%s: dangling aggregation entry!\n",
  1301. wiphy_name(ar->hw->wiphy));
  1302. #endif /* AR9170_TXAGG_DEBUG */
  1303. continue;
  1304. }
  1305. if (++i > 64) {
  1306. #ifdef AR9170_TXAGG_DEBUG
  1307. printk(KERN_DEBUG "%s: enough frames aggregated.\n",
  1308. wiphy_name(ar->hw->wiphy));
  1309. #endif /* AR9170_TXAGG_DEBUG */
  1310. break;
  1311. }
  1312. queue = TID_TO_WME_AC(tid_info->tid);
  1313. if (skb_queue_len(&ar->tx_pending[queue]) >=
  1314. AR9170_NUM_TX_AGG_MAX) {
  1315. #ifdef AR9170_TXAGG_DEBUG
  1316. printk(KERN_DEBUG "%s: queue %d full.\n",
  1317. wiphy_name(ar->hw->wiphy), queue);
  1318. #endif /* AR9170_TXAGG_DEBUG */
  1319. continue;
  1320. }
  1321. list_del_init(&tid_info->list);
  1322. spin_lock_irqsave(&tid_info->queue.lock, f2);
  1323. tmpssn = seq = tid_info->ssn;
  1324. first = skb_peek(&tid_info->queue);
  1325. if (likely(first))
  1326. tmpssn = ar9170_get_seq(first);
  1327. if (unlikely(tmpssn != seq)) {
  1328. #ifdef AR9170_TXAGG_DEBUG
  1329. printk(KERN_DEBUG "%s: ssn mismatch [%d != %d]\n.",
  1330. wiphy_name(ar->hw->wiphy), seq, tmpssn);
  1331. #endif /* AR9170_TXAGG_DEBUG */
  1332. tid_info->ssn = tmpssn;
  1333. }
  1334. #ifdef AR9170_TXAGG_DEBUG
  1335. printk(KERN_DEBUG "%s: generate A-MPDU for tid:%d ssn:%d with "
  1336. "%d queued frames.\n", wiphy_name(ar->hw->wiphy),
  1337. tid_info->tid, tid_info->ssn,
  1338. skb_queue_len(&tid_info->queue));
  1339. __ar9170_dump_txqueue(ar, &tid_info->queue);
  1340. #endif /* AR9170_TXAGG_DEBUG */
  1341. while ((skb = skb_peek(&tid_info->queue))) {
  1342. if (unlikely(ar9170_get_seq(skb) != seq))
  1343. break;
  1344. __skb_unlink(skb, &tid_info->queue);
  1345. tid_info->ssn = seq = GET_NEXT_SEQ(seq);
  1346. if (unlikely(skb_get_queue_mapping(skb) != queue)) {
  1347. #ifdef AR9170_TXAGG_DEBUG
  1348. printk(KERN_DEBUG "%s: tid:%d(q:%d) queue:%d "
  1349. "!match.\n", wiphy_name(ar->hw->wiphy),
  1350. tid_info->tid,
  1351. TID_TO_WME_AC(tid_info->tid),
  1352. skb_get_queue_mapping(skb));
  1353. #endif /* AR9170_TXAGG_DEBUG */
  1354. dev_kfree_skb_any(skb);
  1355. continue;
  1356. }
  1357. if (unlikely(first == skb)) {
  1358. ar9170_tx_prepare_phy(ar, skb);
  1359. __skb_queue_tail(&agg, skb);
  1360. first = skb;
  1361. } else {
  1362. ar9170_tx_copy_phy(ar, skb, first);
  1363. __skb_queue_tail(&agg, skb);
  1364. }
  1365. if (unlikely(skb_queue_len(&agg) ==
  1366. AR9170_NUM_TX_AGG_MAX))
  1367. break;
  1368. }
  1369. if (skb_queue_empty(&tid_info->queue))
  1370. tid_info->active = false;
  1371. else
  1372. list_add_tail(&tid_info->list,
  1373. &ar->tx_ampdu_list);
  1374. spin_unlock_irqrestore(&tid_info->queue.lock, f2);
  1375. if (unlikely(skb_queue_empty(&agg))) {
  1376. #ifdef AR9170_TXAGG_DEBUG
  1377. printk(KERN_DEBUG "%s: queued empty list!\n",
  1378. wiphy_name(ar->hw->wiphy));
  1379. #endif /* AR9170_TXAGG_DEBUG */
  1380. continue;
  1381. }
  1382. /*
  1383. * tell the FW/HW that this is the last frame,
  1384. * that way it will wait for the immediate block ack.
  1385. */
  1386. ar9170_tx_indicate_immba(ar, skb_peek_tail(&agg));
  1387. #ifdef AR9170_TXAGG_DEBUG
  1388. printk(KERN_DEBUG "%s: generated A-MPDU looks like this:\n",
  1389. wiphy_name(ar->hw->wiphy));
  1390. __ar9170_dump_txqueue(ar, &agg);
  1391. #endif /* AR9170_TXAGG_DEBUG */
  1392. spin_unlock_irqrestore(&ar->tx_ampdu_list_lock, flags);
  1393. spin_lock_irqsave(&ar->tx_pending[queue].lock, flags);
  1394. skb_queue_splice_tail_init(&agg, &ar->tx_pending[queue]);
  1395. spin_unlock_irqrestore(&ar->tx_pending[queue].lock, flags);
  1396. run = true;
  1397. spin_lock_irqsave(&ar->tx_ampdu_list_lock, flags);
  1398. }
  1399. out_unlock:
  1400. spin_unlock_irqrestore(&ar->tx_ampdu_list_lock, flags);
  1401. __skb_queue_purge(&agg);
  1402. return run;
  1403. }
  1404. static void ar9170_tx(struct ar9170 *ar)
  1405. {
  1406. struct sk_buff *skb;
  1407. unsigned long flags;
  1408. struct ieee80211_tx_info *info;
  1409. struct ar9170_tx_info *arinfo;
  1410. unsigned int i, frames, frames_failed, remaining_space;
  1411. int err;
  1412. bool schedule_garbagecollector = false;
  1413. BUILD_BUG_ON(sizeof(*arinfo) > sizeof(info->rate_driver_data));
  1414. if (unlikely(!IS_STARTED(ar)))
  1415. return ;
  1416. remaining_space = AR9170_TX_MAX_PENDING;
  1417. for (i = 0; i < __AR9170_NUM_TXQ; i++) {
  1418. spin_lock_irqsave(&ar->tx_stats_lock, flags);
  1419. frames = min(ar->tx_stats[i].limit - ar->tx_stats[i].len,
  1420. skb_queue_len(&ar->tx_pending[i]));
  1421. if (remaining_space < frames) {
  1422. #ifdef AR9170_QUEUE_DEBUG
  1423. printk(KERN_DEBUG "%s: tx quota reached queue:%d, "
  1424. "remaining slots:%d, needed:%d\n",
  1425. wiphy_name(ar->hw->wiphy), i, remaining_space,
  1426. frames);
  1427. #endif /* AR9170_QUEUE_DEBUG */
  1428. frames = remaining_space;
  1429. }
  1430. ar->tx_stats[i].len += frames;
  1431. ar->tx_stats[i].count += frames;
  1432. if (ar->tx_stats[i].len >= ar->tx_stats[i].limit) {
  1433. #ifdef AR9170_QUEUE_DEBUG
  1434. printk(KERN_DEBUG "%s: queue %d full\n",
  1435. wiphy_name(ar->hw->wiphy), i);
  1436. printk(KERN_DEBUG "%s: stuck frames: ===> \n",
  1437. wiphy_name(ar->hw->wiphy));
  1438. ar9170_dump_txqueue(ar, &ar->tx_pending[i]);
  1439. ar9170_dump_txqueue(ar, &ar->tx_status[i]);
  1440. #endif /* AR9170_QUEUE_DEBUG */
  1441. #ifdef AR9170_QUEUE_STOP_DEBUG
  1442. printk(KERN_DEBUG "%s: stop queue %d\n",
  1443. wiphy_name(ar->hw->wiphy), i);
  1444. __ar9170_dump_txstats(ar);
  1445. #endif /* AR9170_QUEUE_STOP_DEBUG */
  1446. ieee80211_stop_queue(ar->hw, i);
  1447. }
  1448. spin_unlock_irqrestore(&ar->tx_stats_lock, flags);
  1449. if (!frames)
  1450. continue;
  1451. frames_failed = 0;
  1452. while (frames) {
  1453. skb = skb_dequeue(&ar->tx_pending[i]);
  1454. if (unlikely(!skb)) {
  1455. frames_failed += frames;
  1456. frames = 0;
  1457. break;
  1458. }
  1459. info = IEEE80211_SKB_CB(skb);
  1460. arinfo = (void *) info->rate_driver_data;
  1461. /* TODO: cancel stuck frames */
  1462. arinfo->timeout = jiffies +
  1463. msecs_to_jiffies(AR9170_TX_TIMEOUT);
  1464. if (info->flags & IEEE80211_TX_CTL_AMPDU)
  1465. atomic_inc(&ar->tx_ampdu_pending);
  1466. #ifdef AR9170_QUEUE_DEBUG
  1467. printk(KERN_DEBUG "%s: send frame q:%d =>\n",
  1468. wiphy_name(ar->hw->wiphy), i);
  1469. ar9170_print_txheader(ar, skb);
  1470. #endif /* AR9170_QUEUE_DEBUG */
  1471. err = ar->tx(ar, skb);
  1472. if (unlikely(err)) {
  1473. if (info->flags & IEEE80211_TX_CTL_AMPDU)
  1474. atomic_dec(&ar->tx_ampdu_pending);
  1475. frames_failed++;
  1476. dev_kfree_skb_any(skb);
  1477. } else {
  1478. remaining_space--;
  1479. schedule_garbagecollector = true;
  1480. }
  1481. frames--;
  1482. }
  1483. #ifdef AR9170_QUEUE_DEBUG
  1484. printk(KERN_DEBUG "%s: ar9170_tx report for queue %d\n",
  1485. wiphy_name(ar->hw->wiphy), i);
  1486. printk(KERN_DEBUG "%s: unprocessed pending frames left:\n",
  1487. wiphy_name(ar->hw->wiphy));
  1488. ar9170_dump_txqueue(ar, &ar->tx_pending[i]);
  1489. #endif /* AR9170_QUEUE_DEBUG */
  1490. if (unlikely(frames_failed)) {
  1491. #ifdef AR9170_QUEUE_DEBUG
  1492. printk(KERN_DEBUG "%s: frames failed %d =>\n",
  1493. wiphy_name(ar->hw->wiphy), frames_failed);
  1494. #endif /* AR9170_QUEUE_DEBUG */
  1495. spin_lock_irqsave(&ar->tx_stats_lock, flags);
  1496. ar->tx_stats[i].len -= frames_failed;
  1497. ar->tx_stats[i].count -= frames_failed;
  1498. #ifdef AR9170_QUEUE_STOP_DEBUG
  1499. printk(KERN_DEBUG "%s: wake queue %d\n",
  1500. wiphy_name(ar->hw->wiphy), i);
  1501. __ar9170_dump_txstats(ar);
  1502. #endif /* AR9170_QUEUE_STOP_DEBUG */
  1503. ieee80211_wake_queue(ar->hw, i);
  1504. spin_unlock_irqrestore(&ar->tx_stats_lock, flags);
  1505. }
  1506. }
  1507. if (!schedule_garbagecollector)
  1508. return;
  1509. ieee80211_queue_delayed_work(ar->hw,
  1510. &ar->tx_janitor,
  1511. msecs_to_jiffies(AR9170_JANITOR_DELAY));
  1512. }
  1513. static bool ar9170_tx_ampdu_queue(struct ar9170 *ar, struct sk_buff *skb)
  1514. {
  1515. struct ieee80211_tx_info *txinfo;
  1516. struct ar9170_sta_info *sta_info;
  1517. struct ar9170_sta_tid *agg;
  1518. struct sk_buff *iter;
  1519. unsigned long flags, f2;
  1520. unsigned int max;
  1521. u16 tid, seq, qseq;
  1522. bool run = false, queue = false;
  1523. tid = ar9170_get_tid(skb);
  1524. seq = ar9170_get_seq(skb);
  1525. txinfo = IEEE80211_SKB_CB(skb);
  1526. sta_info = (void *) txinfo->control.sta->drv_priv;
  1527. agg = &sta_info->agg[tid];
  1528. max = sta_info->ampdu_max_len;
  1529. spin_lock_irqsave(&ar->tx_ampdu_list_lock, flags);
  1530. if (unlikely(agg->state != AR9170_TID_STATE_COMPLETE)) {
  1531. #ifdef AR9170_TXAGG_DEBUG
  1532. printk(KERN_DEBUG "%s: BlockACK session not fully initialized "
  1533. "for ESS:%pM tid:%d state:%d.\n",
  1534. wiphy_name(ar->hw->wiphy), agg->addr, agg->tid,
  1535. agg->state);
  1536. #endif /* AR9170_TXAGG_DEBUG */
  1537. goto err_unlock;
  1538. }
  1539. if (!agg->active) {
  1540. agg->active = true;
  1541. agg->ssn = seq;
  1542. queue = true;
  1543. }
  1544. /* check if seq is within the BA window */
  1545. if (unlikely(!BAW_WITHIN(agg->ssn, max, seq))) {
  1546. #ifdef AR9170_TXAGG_DEBUG
  1547. printk(KERN_DEBUG "%s: frame with tid:%d seq:%d does not "
  1548. "fit into BA window (%d - %d)\n",
  1549. wiphy_name(ar->hw->wiphy), tid, seq, agg->ssn,
  1550. (agg->ssn + max) & 0xfff);
  1551. #endif /* AR9170_TXAGG_DEBUG */
  1552. goto err_unlock;
  1553. }
  1554. spin_lock_irqsave(&agg->queue.lock, f2);
  1555. skb_queue_reverse_walk(&agg->queue, iter) {
  1556. qseq = ar9170_get_seq(iter);
  1557. if (GET_NEXT_SEQ(qseq) == seq) {
  1558. __skb_queue_after(&agg->queue, iter, skb);
  1559. goto queued;
  1560. }
  1561. }
  1562. __skb_queue_head(&agg->queue, skb);
  1563. queued:
  1564. spin_unlock_irqrestore(&agg->queue.lock, f2);
  1565. #ifdef AR9170_TXAGG_DEBUG
  1566. printk(KERN_DEBUG "%s: new aggregate %p queued.\n",
  1567. wiphy_name(ar->hw->wiphy), skb);
  1568. __ar9170_dump_txqueue(ar, &agg->queue);
  1569. #endif /* AR9170_TXAGG_DEBUG */
  1570. if (skb_queue_len(&agg->queue) >= AR9170_NUM_TX_AGG_MAX)
  1571. run = true;
  1572. if (queue)
  1573. list_add_tail(&agg->list, &ar->tx_ampdu_list);
  1574. spin_unlock_irqrestore(&ar->tx_ampdu_list_lock, flags);
  1575. return run;
  1576. err_unlock:
  1577. spin_unlock_irqrestore(&ar->tx_ampdu_list_lock, flags);
  1578. dev_kfree_skb_irq(skb);
  1579. return false;
  1580. }
  1581. int ar9170_op_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
  1582. {
  1583. struct ar9170 *ar = hw->priv;
  1584. struct ieee80211_tx_info *info;
  1585. if (unlikely(!IS_STARTED(ar)))
  1586. goto err_free;
  1587. if (unlikely(ar9170_tx_prepare(ar, skb)))
  1588. goto err_free;
  1589. info = IEEE80211_SKB_CB(skb);
  1590. if (info->flags & IEEE80211_TX_CTL_AMPDU) {
  1591. bool run = ar9170_tx_ampdu_queue(ar, skb);
  1592. if (run || !atomic_read(&ar->tx_ampdu_pending))
  1593. ar9170_tx_ampdu(ar);
  1594. } else {
  1595. unsigned int queue = skb_get_queue_mapping(skb);
  1596. ar9170_tx_prepare_phy(ar, skb);
  1597. skb_queue_tail(&ar->tx_pending[queue], skb);
  1598. }
  1599. ar9170_tx(ar);
  1600. return NETDEV_TX_OK;
  1601. err_free:
  1602. dev_kfree_skb_any(skb);
  1603. return NETDEV_TX_OK;
  1604. }
  1605. static int ar9170_op_add_interface(struct ieee80211_hw *hw,
  1606. struct ieee80211_vif *vif)
  1607. {
  1608. struct ar9170 *ar = hw->priv;
  1609. struct ath_common *common = &ar->common;
  1610. int err = 0;
  1611. mutex_lock(&ar->mutex);
  1612. if (ar->vif) {
  1613. err = -EBUSY;
  1614. goto unlock;
  1615. }
  1616. ar->vif = vif;
  1617. memcpy(common->macaddr, vif->addr, ETH_ALEN);
  1618. if (modparam_nohwcrypt || (ar->vif->type != NL80211_IFTYPE_STATION)) {
  1619. ar->rx_software_decryption = true;
  1620. ar->disable_offload = true;
  1621. }
  1622. ar->cur_filter = 0;
  1623. err = ar9170_update_frame_filter(ar, AR9170_MAC_REG_FTF_DEFAULTS);
  1624. if (err)
  1625. goto unlock;
  1626. err = ar9170_set_operating_mode(ar);
  1627. unlock:
  1628. mutex_unlock(&ar->mutex);
  1629. return err;
  1630. }
  1631. static void ar9170_op_remove_interface(struct ieee80211_hw *hw,
  1632. struct ieee80211_vif *vif)
  1633. {
  1634. struct ar9170 *ar = hw->priv;
  1635. mutex_lock(&ar->mutex);
  1636. ar->vif = NULL;
  1637. ar9170_update_frame_filter(ar, 0);
  1638. ar9170_set_beacon_timers(ar);
  1639. dev_kfree_skb(ar->beacon);
  1640. ar->beacon = NULL;
  1641. ar->sniffer_enabled = false;
  1642. ar->rx_software_decryption = false;
  1643. ar9170_set_operating_mode(ar);
  1644. mutex_unlock(&ar->mutex);
  1645. }
  1646. static int ar9170_op_config(struct ieee80211_hw *hw, u32 changed)
  1647. {
  1648. struct ar9170 *ar = hw->priv;
  1649. int err = 0;
  1650. mutex_lock(&ar->mutex);
  1651. if (changed & IEEE80211_CONF_CHANGE_LISTEN_INTERVAL) {
  1652. /* TODO */
  1653. err = 0;
  1654. }
  1655. if (changed & IEEE80211_CONF_CHANGE_PS) {
  1656. /* TODO */
  1657. err = 0;
  1658. }
  1659. if (changed & IEEE80211_CONF_CHANGE_POWER) {
  1660. /* TODO */
  1661. err = 0;
  1662. }
  1663. if (changed & IEEE80211_CONF_CHANGE_RETRY_LIMITS) {
  1664. /*
  1665. * is it long_frame_max_tx_count or short_frame_max_tx_count?
  1666. */
  1667. err = ar9170_set_hwretry_limit(ar,
  1668. ar->hw->conf.long_frame_max_tx_count);
  1669. if (err)
  1670. goto out;
  1671. }
  1672. if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
  1673. /* adjust slot time for 5 GHz */
  1674. err = ar9170_set_slot_time(ar);
  1675. if (err)
  1676. goto out;
  1677. err = ar9170_set_dyn_sifs_ack(ar);
  1678. if (err)
  1679. goto out;
  1680. err = ar9170_set_channel(ar, hw->conf.channel,
  1681. AR9170_RFI_NONE,
  1682. nl80211_to_ar9170(hw->conf.channel_type));
  1683. if (err)
  1684. goto out;
  1685. }
  1686. out:
  1687. mutex_unlock(&ar->mutex);
  1688. return err;
  1689. }
  1690. static u64 ar9170_op_prepare_multicast(struct ieee80211_hw *hw, int mc_count,
  1691. struct dev_addr_list *mclist)
  1692. {
  1693. u64 mchash;
  1694. int i;
  1695. /* always get broadcast frames */
  1696. mchash = 1ULL << (0xff >> 2);
  1697. for (i = 0; i < mc_count; i++) {
  1698. if (WARN_ON(!mclist))
  1699. break;
  1700. mchash |= 1ULL << (mclist->dmi_addr[5] >> 2);
  1701. mclist = mclist->next;
  1702. }
  1703. return mchash;
  1704. }
  1705. static void ar9170_op_configure_filter(struct ieee80211_hw *hw,
  1706. unsigned int changed_flags,
  1707. unsigned int *new_flags,
  1708. u64 multicast)
  1709. {
  1710. struct ar9170 *ar = hw->priv;
  1711. if (unlikely(!IS_ACCEPTING_CMD(ar)))
  1712. return ;
  1713. mutex_lock(&ar->mutex);
  1714. /* mask supported flags */
  1715. *new_flags &= FIF_ALLMULTI | FIF_CONTROL | FIF_BCN_PRBRESP_PROMISC |
  1716. FIF_PROMISC_IN_BSS | FIF_FCSFAIL | FIF_PLCPFAIL;
  1717. ar->filter_state = *new_flags;
  1718. /*
  1719. * We can support more by setting the sniffer bit and
  1720. * then checking the error flags, later.
  1721. */
  1722. if (changed_flags & FIF_ALLMULTI && *new_flags & FIF_ALLMULTI)
  1723. multicast = ~0ULL;
  1724. if (multicast != ar->cur_mc_hash)
  1725. ar9170_update_multicast(ar, multicast);
  1726. if (changed_flags & FIF_CONTROL) {
  1727. u32 filter = AR9170_MAC_REG_FTF_PSPOLL |
  1728. AR9170_MAC_REG_FTF_RTS |
  1729. AR9170_MAC_REG_FTF_CTS |
  1730. AR9170_MAC_REG_FTF_ACK |
  1731. AR9170_MAC_REG_FTF_CFE |
  1732. AR9170_MAC_REG_FTF_CFE_ACK;
  1733. if (*new_flags & FIF_CONTROL)
  1734. filter |= ar->cur_filter;
  1735. else
  1736. filter &= (~ar->cur_filter);
  1737. ar9170_update_frame_filter(ar, filter);
  1738. }
  1739. if (changed_flags & FIF_PROMISC_IN_BSS) {
  1740. ar->sniffer_enabled = ((*new_flags) & FIF_PROMISC_IN_BSS) != 0;
  1741. ar9170_set_operating_mode(ar);
  1742. }
  1743. mutex_unlock(&ar->mutex);
  1744. }
  1745. static void ar9170_op_bss_info_changed(struct ieee80211_hw *hw,
  1746. struct ieee80211_vif *vif,
  1747. struct ieee80211_bss_conf *bss_conf,
  1748. u32 changed)
  1749. {
  1750. struct ar9170 *ar = hw->priv;
  1751. struct ath_common *common = &ar->common;
  1752. int err = 0;
  1753. mutex_lock(&ar->mutex);
  1754. if (changed & BSS_CHANGED_BSSID) {
  1755. memcpy(common->curbssid, bss_conf->bssid, ETH_ALEN);
  1756. err = ar9170_set_operating_mode(ar);
  1757. if (err)
  1758. goto out;
  1759. }
  1760. if (changed & BSS_CHANGED_BEACON_ENABLED)
  1761. ar->enable_beacon = bss_conf->enable_beacon;
  1762. if (changed & BSS_CHANGED_BEACON) {
  1763. err = ar9170_update_beacon(ar);
  1764. if (err)
  1765. goto out;
  1766. }
  1767. if (changed & (BSS_CHANGED_BEACON_ENABLED | BSS_CHANGED_BEACON |
  1768. BSS_CHANGED_BEACON_INT)) {
  1769. err = ar9170_set_beacon_timers(ar);
  1770. if (err)
  1771. goto out;
  1772. }
  1773. if (changed & BSS_CHANGED_ASSOC) {
  1774. #ifndef CONFIG_AR9170_LEDS
  1775. /* enable assoc LED. */
  1776. err = ar9170_set_leds_state(ar, bss_conf->assoc ? 2 : 0);
  1777. #endif /* CONFIG_AR9170_LEDS */
  1778. }
  1779. if (changed & BSS_CHANGED_HT) {
  1780. /* TODO */
  1781. err = 0;
  1782. }
  1783. if (changed & BSS_CHANGED_ERP_SLOT) {
  1784. err = ar9170_set_slot_time(ar);
  1785. if (err)
  1786. goto out;
  1787. }
  1788. if (changed & BSS_CHANGED_BASIC_RATES) {
  1789. err = ar9170_set_basic_rates(ar);
  1790. if (err)
  1791. goto out;
  1792. }
  1793. out:
  1794. mutex_unlock(&ar->mutex);
  1795. }
  1796. static u64 ar9170_op_get_tsf(struct ieee80211_hw *hw)
  1797. {
  1798. struct ar9170 *ar = hw->priv;
  1799. int err;
  1800. u64 tsf;
  1801. #define NR 3
  1802. static const u32 addr[NR] = { AR9170_MAC_REG_TSF_H,
  1803. AR9170_MAC_REG_TSF_L,
  1804. AR9170_MAC_REG_TSF_H };
  1805. u32 val[NR];
  1806. int loops = 0;
  1807. mutex_lock(&ar->mutex);
  1808. while (loops++ < 10) {
  1809. err = ar9170_read_mreg(ar, NR, addr, val);
  1810. if (err || val[0] == val[2])
  1811. break;
  1812. }
  1813. mutex_unlock(&ar->mutex);
  1814. if (WARN_ON(err))
  1815. return 0;
  1816. tsf = val[0];
  1817. tsf = (tsf << 32) | val[1];
  1818. return tsf;
  1819. #undef NR
  1820. }
  1821. static int ar9170_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
  1822. struct ieee80211_vif *vif, struct ieee80211_sta *sta,
  1823. struct ieee80211_key_conf *key)
  1824. {
  1825. struct ar9170 *ar = hw->priv;
  1826. int err = 0, i;
  1827. u8 ktype;
  1828. if ((!ar->vif) || (ar->disable_offload))
  1829. return -EOPNOTSUPP;
  1830. switch (key->alg) {
  1831. case ALG_WEP:
  1832. if (key->keylen == WLAN_KEY_LEN_WEP40)
  1833. ktype = AR9170_ENC_ALG_WEP64;
  1834. else
  1835. ktype = AR9170_ENC_ALG_WEP128;
  1836. break;
  1837. case ALG_TKIP:
  1838. ktype = AR9170_ENC_ALG_TKIP;
  1839. break;
  1840. case ALG_CCMP:
  1841. ktype = AR9170_ENC_ALG_AESCCMP;
  1842. break;
  1843. default:
  1844. return -EOPNOTSUPP;
  1845. }
  1846. mutex_lock(&ar->mutex);
  1847. if (cmd == SET_KEY) {
  1848. if (unlikely(!IS_STARTED(ar))) {
  1849. err = -EOPNOTSUPP;
  1850. goto out;
  1851. }
  1852. /* group keys need all-zeroes address */
  1853. if (!(key->flags & IEEE80211_KEY_FLAG_PAIRWISE))
  1854. sta = NULL;
  1855. if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
  1856. for (i = 0; i < 64; i++)
  1857. if (!(ar->usedkeys & BIT(i)))
  1858. break;
  1859. if (i == 64) {
  1860. ar->rx_software_decryption = true;
  1861. ar9170_set_operating_mode(ar);
  1862. err = -ENOSPC;
  1863. goto out;
  1864. }
  1865. } else {
  1866. i = 64 + key->keyidx;
  1867. }
  1868. key->hw_key_idx = i;
  1869. err = ar9170_upload_key(ar, i, sta ? sta->addr : NULL, ktype, 0,
  1870. key->key, min_t(u8, 16, key->keylen));
  1871. if (err)
  1872. goto out;
  1873. if (key->alg == ALG_TKIP) {
  1874. err = ar9170_upload_key(ar, i, sta ? sta->addr : NULL,
  1875. ktype, 1, key->key + 16, 16);
  1876. if (err)
  1877. goto out;
  1878. /*
  1879. * hardware is not capable generating the MMIC
  1880. * for fragmented frames!
  1881. */
  1882. key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
  1883. }
  1884. if (i < 64)
  1885. ar->usedkeys |= BIT(i);
  1886. key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  1887. } else {
  1888. if (unlikely(!IS_STARTED(ar))) {
  1889. /* The device is gone... together with the key ;-) */
  1890. err = 0;
  1891. goto out;
  1892. }
  1893. err = ar9170_disable_key(ar, key->hw_key_idx);
  1894. if (err)
  1895. goto out;
  1896. if (key->hw_key_idx < 64) {
  1897. ar->usedkeys &= ~BIT(key->hw_key_idx);
  1898. } else {
  1899. err = ar9170_upload_key(ar, key->hw_key_idx, NULL,
  1900. AR9170_ENC_ALG_NONE, 0,
  1901. NULL, 0);
  1902. if (err)
  1903. goto out;
  1904. if (key->alg == ALG_TKIP) {
  1905. err = ar9170_upload_key(ar, key->hw_key_idx,
  1906. NULL,
  1907. AR9170_ENC_ALG_NONE, 1,
  1908. NULL, 0);
  1909. if (err)
  1910. goto out;
  1911. }
  1912. }
  1913. }
  1914. ar9170_regwrite_begin(ar);
  1915. ar9170_regwrite(AR9170_MAC_REG_ROLL_CALL_TBL_L, ar->usedkeys);
  1916. ar9170_regwrite(AR9170_MAC_REG_ROLL_CALL_TBL_H, ar->usedkeys >> 32);
  1917. ar9170_regwrite_finish();
  1918. err = ar9170_regwrite_result();
  1919. out:
  1920. mutex_unlock(&ar->mutex);
  1921. return err;
  1922. }
  1923. static void ar9170_sta_notify(struct ieee80211_hw *hw,
  1924. struct ieee80211_vif *vif,
  1925. enum sta_notify_cmd cmd,
  1926. struct ieee80211_sta *sta)
  1927. {
  1928. struct ar9170 *ar = hw->priv;
  1929. struct ar9170_sta_info *sta_info = (void *) sta->drv_priv;
  1930. unsigned int i;
  1931. switch (cmd) {
  1932. case STA_NOTIFY_ADD:
  1933. memset(sta_info, 0, sizeof(*sta_info));
  1934. if (!sta->ht_cap.ht_supported)
  1935. break;
  1936. if (sta->ht_cap.ampdu_density > ar->global_ampdu_density)
  1937. ar->global_ampdu_density = sta->ht_cap.ampdu_density;
  1938. if (sta->ht_cap.ampdu_factor < ar->global_ampdu_factor)
  1939. ar->global_ampdu_factor = sta->ht_cap.ampdu_factor;
  1940. for (i = 0; i < AR9170_NUM_TID; i++) {
  1941. sta_info->agg[i].state = AR9170_TID_STATE_SHUTDOWN;
  1942. sta_info->agg[i].active = false;
  1943. sta_info->agg[i].ssn = 0;
  1944. sta_info->agg[i].tid = i;
  1945. INIT_LIST_HEAD(&sta_info->agg[i].list);
  1946. skb_queue_head_init(&sta_info->agg[i].queue);
  1947. }
  1948. sta_info->ampdu_max_len = 1 << (3 + sta->ht_cap.ampdu_factor);
  1949. break;
  1950. case STA_NOTIFY_REMOVE:
  1951. if (!sta->ht_cap.ht_supported)
  1952. break;
  1953. for (i = 0; i < AR9170_NUM_TID; i++) {
  1954. sta_info->agg[i].state = AR9170_TID_STATE_INVALID;
  1955. skb_queue_purge(&sta_info->agg[i].queue);
  1956. }
  1957. break;
  1958. default:
  1959. break;
  1960. }
  1961. }
  1962. static int ar9170_get_stats(struct ieee80211_hw *hw,
  1963. struct ieee80211_low_level_stats *stats)
  1964. {
  1965. struct ar9170 *ar = hw->priv;
  1966. u32 val;
  1967. int err;
  1968. mutex_lock(&ar->mutex);
  1969. err = ar9170_read_reg(ar, AR9170_MAC_REG_TX_RETRY, &val);
  1970. ar->stats.dot11ACKFailureCount += val;
  1971. memcpy(stats, &ar->stats, sizeof(*stats));
  1972. mutex_unlock(&ar->mutex);
  1973. return 0;
  1974. }
  1975. static int ar9170_conf_tx(struct ieee80211_hw *hw, u16 queue,
  1976. const struct ieee80211_tx_queue_params *param)
  1977. {
  1978. struct ar9170 *ar = hw->priv;
  1979. int ret;
  1980. mutex_lock(&ar->mutex);
  1981. if (queue < __AR9170_NUM_TXQ) {
  1982. memcpy(&ar->edcf[ar9170_qos_hwmap[queue]],
  1983. param, sizeof(*param));
  1984. ret = ar9170_set_qos(ar);
  1985. } else {
  1986. ret = -EINVAL;
  1987. }
  1988. mutex_unlock(&ar->mutex);
  1989. return ret;
  1990. }
  1991. static int ar9170_ampdu_action(struct ieee80211_hw *hw,
  1992. struct ieee80211_vif *vif,
  1993. enum ieee80211_ampdu_mlme_action action,
  1994. struct ieee80211_sta *sta, u16 tid, u16 *ssn)
  1995. {
  1996. struct ar9170 *ar = hw->priv;
  1997. struct ar9170_sta_info *sta_info = (void *) sta->drv_priv;
  1998. struct ar9170_sta_tid *tid_info = &sta_info->agg[tid];
  1999. unsigned long flags;
  2000. if (!modparam_ht)
  2001. return -EOPNOTSUPP;
  2002. switch (action) {
  2003. case IEEE80211_AMPDU_TX_START:
  2004. spin_lock_irqsave(&ar->tx_ampdu_list_lock, flags);
  2005. if (tid_info->state != AR9170_TID_STATE_SHUTDOWN ||
  2006. !list_empty(&tid_info->list)) {
  2007. spin_unlock_irqrestore(&ar->tx_ampdu_list_lock, flags);
  2008. #ifdef AR9170_TXAGG_DEBUG
  2009. printk(KERN_INFO "%s: A-MPDU [ESS:[%pM] tid:[%d]] "
  2010. "is in a very bad state!\n",
  2011. wiphy_name(hw->wiphy), sta->addr, tid);
  2012. #endif /* AR9170_TXAGG_DEBUG */
  2013. return -EBUSY;
  2014. }
  2015. *ssn = tid_info->ssn;
  2016. tid_info->state = AR9170_TID_STATE_PROGRESS;
  2017. tid_info->active = false;
  2018. spin_unlock_irqrestore(&ar->tx_ampdu_list_lock, flags);
  2019. ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  2020. break;
  2021. case IEEE80211_AMPDU_TX_STOP:
  2022. spin_lock_irqsave(&ar->tx_ampdu_list_lock, flags);
  2023. tid_info->state = AR9170_TID_STATE_SHUTDOWN;
  2024. list_del_init(&tid_info->list);
  2025. tid_info->active = false;
  2026. skb_queue_purge(&tid_info->queue);
  2027. spin_unlock_irqrestore(&ar->tx_ampdu_list_lock, flags);
  2028. ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  2029. break;
  2030. case IEEE80211_AMPDU_TX_OPERATIONAL:
  2031. #ifdef AR9170_TXAGG_DEBUG
  2032. printk(KERN_INFO "%s: A-MPDU for %pM [tid:%d] Operational.\n",
  2033. wiphy_name(hw->wiphy), sta->addr, tid);
  2034. #endif /* AR9170_TXAGG_DEBUG */
  2035. spin_lock_irqsave(&ar->tx_ampdu_list_lock, flags);
  2036. sta_info->agg[tid].state = AR9170_TID_STATE_COMPLETE;
  2037. spin_unlock_irqrestore(&ar->tx_ampdu_list_lock, flags);
  2038. break;
  2039. case IEEE80211_AMPDU_RX_START:
  2040. case IEEE80211_AMPDU_RX_STOP:
  2041. /* Handled by firmware */
  2042. break;
  2043. default:
  2044. return -EOPNOTSUPP;
  2045. }
  2046. return 0;
  2047. }
  2048. static const struct ieee80211_ops ar9170_ops = {
  2049. .start = ar9170_op_start,
  2050. .stop = ar9170_op_stop,
  2051. .tx = ar9170_op_tx,
  2052. .add_interface = ar9170_op_add_interface,
  2053. .remove_interface = ar9170_op_remove_interface,
  2054. .config = ar9170_op_config,
  2055. .prepare_multicast = ar9170_op_prepare_multicast,
  2056. .configure_filter = ar9170_op_configure_filter,
  2057. .conf_tx = ar9170_conf_tx,
  2058. .bss_info_changed = ar9170_op_bss_info_changed,
  2059. .get_tsf = ar9170_op_get_tsf,
  2060. .set_key = ar9170_set_key,
  2061. .sta_notify = ar9170_sta_notify,
  2062. .get_stats = ar9170_get_stats,
  2063. .ampdu_action = ar9170_ampdu_action,
  2064. };
  2065. void *ar9170_alloc(size_t priv_size)
  2066. {
  2067. struct ieee80211_hw *hw;
  2068. struct ar9170 *ar;
  2069. struct sk_buff *skb;
  2070. int i;
  2071. /*
  2072. * this buffer is used for rx stream reconstruction.
  2073. * Under heavy load this device (or the transport layer?)
  2074. * tends to split the streams into seperate rx descriptors.
  2075. */
  2076. skb = __dev_alloc_skb(AR9170_MAX_RX_BUFFER_SIZE, GFP_KERNEL);
  2077. if (!skb)
  2078. goto err_nomem;
  2079. hw = ieee80211_alloc_hw(priv_size, &ar9170_ops);
  2080. if (!hw)
  2081. goto err_nomem;
  2082. ar = hw->priv;
  2083. ar->hw = hw;
  2084. ar->rx_failover = skb;
  2085. mutex_init(&ar->mutex);
  2086. spin_lock_init(&ar->cmdlock);
  2087. spin_lock_init(&ar->tx_stats_lock);
  2088. spin_lock_init(&ar->tx_ampdu_list_lock);
  2089. skb_queue_head_init(&ar->tx_status_ampdu);
  2090. for (i = 0; i < __AR9170_NUM_TXQ; i++) {
  2091. skb_queue_head_init(&ar->tx_status[i]);
  2092. skb_queue_head_init(&ar->tx_pending[i]);
  2093. }
  2094. ar9170_rx_reset_rx_mpdu(ar);
  2095. INIT_WORK(&ar->beacon_work, ar9170_new_beacon);
  2096. INIT_DELAYED_WORK(&ar->tx_janitor, ar9170_tx_janitor);
  2097. INIT_LIST_HEAD(&ar->tx_ampdu_list);
  2098. /* all hw supports 2.4 GHz, so set channel to 1 by default */
  2099. ar->channel = &ar9170_2ghz_chantable[0];
  2100. /* first part of wiphy init */
  2101. ar->hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
  2102. BIT(NL80211_IFTYPE_WDS) |
  2103. BIT(NL80211_IFTYPE_ADHOC);
  2104. ar->hw->flags |= IEEE80211_HW_RX_INCLUDES_FCS |
  2105. IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING |
  2106. IEEE80211_HW_SIGNAL_DBM |
  2107. IEEE80211_HW_NOISE_DBM;
  2108. if (modparam_ht) {
  2109. ar->hw->flags |= IEEE80211_HW_AMPDU_AGGREGATION;
  2110. } else {
  2111. ar9170_band_2GHz.ht_cap.ht_supported = false;
  2112. ar9170_band_5GHz.ht_cap.ht_supported = false;
  2113. }
  2114. ar->hw->queues = __AR9170_NUM_TXQ;
  2115. ar->hw->extra_tx_headroom = 8;
  2116. ar->hw->sta_data_size = sizeof(struct ar9170_sta_info);
  2117. ar->hw->max_rates = 1;
  2118. ar->hw->max_rate_tries = 3;
  2119. for (i = 0; i < ARRAY_SIZE(ar->noise); i++)
  2120. ar->noise[i] = -95; /* ATH_DEFAULT_NOISE_FLOOR */
  2121. return ar;
  2122. err_nomem:
  2123. kfree_skb(skb);
  2124. return ERR_PTR(-ENOMEM);
  2125. }
  2126. static int ar9170_read_eeprom(struct ar9170 *ar)
  2127. {
  2128. #define RW 8 /* number of words to read at once */
  2129. #define RB (sizeof(u32) * RW)
  2130. struct ath_regulatory *regulatory = &ar->common.regulatory;
  2131. u8 *eeprom = (void *)&ar->eeprom;
  2132. u8 *addr = ar->eeprom.mac_address;
  2133. __le32 offsets[RW];
  2134. unsigned int rx_streams, tx_streams, tx_params = 0;
  2135. int i, j, err, bands = 0;
  2136. BUILD_BUG_ON(sizeof(ar->eeprom) & 3);
  2137. BUILD_BUG_ON(RB > AR9170_MAX_CMD_LEN - 4);
  2138. #ifndef __CHECKER__
  2139. /* don't want to handle trailing remains */
  2140. BUILD_BUG_ON(sizeof(ar->eeprom) % RB);
  2141. #endif
  2142. for (i = 0; i < sizeof(ar->eeprom)/RB; i++) {
  2143. for (j = 0; j < RW; j++)
  2144. offsets[j] = cpu_to_le32(AR9170_EEPROM_START +
  2145. RB * i + 4 * j);
  2146. err = ar->exec_cmd(ar, AR9170_CMD_RREG,
  2147. RB, (u8 *) &offsets,
  2148. RB, eeprom + RB * i);
  2149. if (err)
  2150. return err;
  2151. }
  2152. #undef RW
  2153. #undef RB
  2154. if (ar->eeprom.length == cpu_to_le16(0xFFFF))
  2155. return -ENODATA;
  2156. if (ar->eeprom.operating_flags & AR9170_OPFLAG_2GHZ) {
  2157. ar->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &ar9170_band_2GHz;
  2158. bands++;
  2159. }
  2160. if (ar->eeprom.operating_flags & AR9170_OPFLAG_5GHZ) {
  2161. ar->hw->wiphy->bands[IEEE80211_BAND_5GHZ] = &ar9170_band_5GHz;
  2162. bands++;
  2163. }
  2164. rx_streams = hweight8(ar->eeprom.rx_mask);
  2165. tx_streams = hweight8(ar->eeprom.tx_mask);
  2166. if (rx_streams != tx_streams)
  2167. tx_params = IEEE80211_HT_MCS_TX_RX_DIFF;
  2168. if (tx_streams >= 1 && tx_streams <= IEEE80211_HT_MCS_TX_MAX_STREAMS)
  2169. tx_params = (tx_streams - 1) <<
  2170. IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT;
  2171. ar9170_band_2GHz.ht_cap.mcs.tx_params |= tx_params;
  2172. ar9170_band_5GHz.ht_cap.mcs.tx_params |= tx_params;
  2173. /*
  2174. * I measured this, a bandswitch takes roughly
  2175. * 135 ms and a frequency switch about 80.
  2176. *
  2177. * FIXME: measure these values again once EEPROM settings
  2178. * are used, that will influence them!
  2179. */
  2180. if (bands == 2)
  2181. ar->hw->channel_change_time = 135 * 1000;
  2182. else
  2183. ar->hw->channel_change_time = 80 * 1000;
  2184. regulatory->current_rd = le16_to_cpu(ar->eeprom.reg_domain[0]);
  2185. regulatory->current_rd_ext = le16_to_cpu(ar->eeprom.reg_domain[1]);
  2186. /* second part of wiphy init */
  2187. SET_IEEE80211_PERM_ADDR(ar->hw, addr);
  2188. return bands ? 0 : -EINVAL;
  2189. }
  2190. static int ar9170_reg_notifier(struct wiphy *wiphy,
  2191. struct regulatory_request *request)
  2192. {
  2193. struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
  2194. struct ar9170 *ar = hw->priv;
  2195. return ath_reg_notifier_apply(wiphy, request, &ar->common.regulatory);
  2196. }
  2197. int ar9170_register(struct ar9170 *ar, struct device *pdev)
  2198. {
  2199. struct ath_regulatory *regulatory = &ar->common.regulatory;
  2200. int err;
  2201. /* try to read EEPROM, init MAC addr */
  2202. err = ar9170_read_eeprom(ar);
  2203. if (err)
  2204. goto err_out;
  2205. err = ath_regd_init(regulatory, ar->hw->wiphy,
  2206. ar9170_reg_notifier);
  2207. if (err)
  2208. goto err_out;
  2209. err = ieee80211_register_hw(ar->hw);
  2210. if (err)
  2211. goto err_out;
  2212. if (!ath_is_world_regd(regulatory))
  2213. regulatory_hint(ar->hw->wiphy, regulatory->alpha2);
  2214. err = ar9170_init_leds(ar);
  2215. if (err)
  2216. goto err_unreg;
  2217. #ifdef CONFIG_AR9170_LEDS
  2218. err = ar9170_register_leds(ar);
  2219. if (err)
  2220. goto err_unreg;
  2221. #endif /* CONFIG_AR9170_LEDS */
  2222. dev_info(pdev, "Atheros AR9170 is registered as '%s'\n",
  2223. wiphy_name(ar->hw->wiphy));
  2224. return err;
  2225. err_unreg:
  2226. ieee80211_unregister_hw(ar->hw);
  2227. err_out:
  2228. return err;
  2229. }
  2230. void ar9170_unregister(struct ar9170 *ar)
  2231. {
  2232. #ifdef CONFIG_AR9170_LEDS
  2233. ar9170_unregister_leds(ar);
  2234. #endif /* CONFIG_AR9170_LEDS */
  2235. kfree_skb(ar->rx_failover);
  2236. ieee80211_unregister_hw(ar->hw);
  2237. mutex_destroy(&ar->mutex);
  2238. }