main.c 52 KB

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