main.c 67 KB

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