main.c 68 KB

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