main.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190
  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. wiphy_debug(ar->hw->wiphy,
  186. "=> FRAME [skb:%p, q:%d, DA:[%pM] s:%d "
  187. "mac_ctrl:%04x, phy_ctrl:%08x, timeout:[%d ms]]\n",
  188. skb, skb_get_queue_mapping(skb),
  189. ieee80211_get_DA(hdr), ar9170_get_seq_h(hdr),
  190. le16_to_cpu(txc->mac_control), le32_to_cpu(txc->phy_control),
  191. jiffies_to_msecs(arinfo->timeout - jiffies));
  192. }
  193. static void __ar9170_dump_txqueue(struct ar9170 *ar,
  194. struct sk_buff_head *queue)
  195. {
  196. struct sk_buff *skb;
  197. int i = 0;
  198. printk(KERN_DEBUG "---[ cut here ]---\n");
  199. wiphy_debug(ar->hw->wiphy, "%d entries in queue.\n",
  200. skb_queue_len(queue));
  201. skb_queue_walk(queue, skb) {
  202. printk(KERN_DEBUG "index:%d =>\n", i++);
  203. ar9170_print_txheader(ar, skb);
  204. }
  205. if (i != skb_queue_len(queue))
  206. printk(KERN_DEBUG "WARNING: queue frame counter "
  207. "mismatch %d != %d\n", skb_queue_len(queue), i);
  208. printk(KERN_DEBUG "---[ end ]---\n");
  209. }
  210. #endif /* AR9170_QUEUE_DEBUG */
  211. #ifdef AR9170_QUEUE_DEBUG
  212. static void ar9170_dump_txqueue(struct ar9170 *ar,
  213. struct sk_buff_head *queue)
  214. {
  215. unsigned long flags;
  216. spin_lock_irqsave(&queue->lock, flags);
  217. __ar9170_dump_txqueue(ar, queue);
  218. spin_unlock_irqrestore(&queue->lock, flags);
  219. }
  220. #endif /* AR9170_QUEUE_DEBUG */
  221. #ifdef AR9170_QUEUE_STOP_DEBUG
  222. static void __ar9170_dump_txstats(struct ar9170 *ar)
  223. {
  224. int i;
  225. wiphy_debug(ar->hw->wiphy, "QoS queue stats\n");
  226. for (i = 0; i < __AR9170_NUM_TXQ; i++)
  227. wiphy_debug(ar->hw->wiphy,
  228. "queue:%d limit:%d len:%d waitack:%d stopped:%d\n",
  229. i, 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. wiphy_debug(ar->hw->wiphy,
  250. "[%ld > %ld] frame expired => recycle\n",
  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. wiphy_err(ar->hw->wiphy,
  286. "invalid tx_status response (%x)\n", 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. wiphy_debug(ar->hw->wiphy, "wake queue %d\n", queue);
  304. __ar9170_dump_txstats(ar);
  305. #endif /* AR9170_QUEUE_STOP_DEBUG */
  306. ieee80211_wake_queue(ar->hw, queue);
  307. }
  308. spin_unlock_irqrestore(&ar->tx_stats_lock, flags);
  309. if (info->flags & IEEE80211_TX_CTL_NO_ACK) {
  310. ar9170_tx_status(ar, skb, AR9170_TX_STATUS_FAILED);
  311. } else {
  312. arinfo->timeout = jiffies +
  313. msecs_to_jiffies(AR9170_TX_TIMEOUT);
  314. skb_queue_tail(&ar->tx_status[queue], skb);
  315. }
  316. if (!ar->tx_stats[queue].len &&
  317. !skb_queue_empty(&ar->tx_pending[queue])) {
  318. ar9170_tx(ar);
  319. }
  320. }
  321. static struct sk_buff *ar9170_get_queued_skb(struct ar9170 *ar,
  322. const u8 *mac,
  323. struct sk_buff_head *queue,
  324. const u32 rate)
  325. {
  326. unsigned long flags;
  327. struct sk_buff *skb;
  328. /*
  329. * Unfortunately, the firmware does not tell to which (queued) frame
  330. * this transmission status report belongs to.
  331. *
  332. * So we have to make risky guesses - with the scarce information
  333. * the firmware provided (-> destination MAC, and phy_control) -
  334. * and hope that we picked the right one...
  335. */
  336. spin_lock_irqsave(&queue->lock, flags);
  337. skb_queue_walk(queue, skb) {
  338. struct ar9170_tx_control *txc = (void *) skb->data;
  339. struct ieee80211_hdr *hdr = (void *) txc->frame_data;
  340. u32 r;
  341. if (mac && compare_ether_addr(ieee80211_get_DA(hdr), mac)) {
  342. #ifdef AR9170_QUEUE_DEBUG
  343. wiphy_debug(ar->hw->wiphy,
  344. "skip frame => DA %pM != %pM\n",
  345. mac, ieee80211_get_DA(hdr));
  346. ar9170_print_txheader(ar, skb);
  347. #endif /* AR9170_QUEUE_DEBUG */
  348. continue;
  349. }
  350. r = (le32_to_cpu(txc->phy_control) & AR9170_TX_PHY_MCS_MASK) >>
  351. AR9170_TX_PHY_MCS_SHIFT;
  352. if ((rate != AR9170_TX_INVALID_RATE) && (r != rate)) {
  353. #ifdef AR9170_QUEUE_DEBUG
  354. wiphy_debug(ar->hw->wiphy,
  355. "skip frame => rate %d != %d\n", rate, r);
  356. ar9170_print_txheader(ar, skb);
  357. #endif /* AR9170_QUEUE_DEBUG */
  358. continue;
  359. }
  360. __skb_unlink(skb, queue);
  361. spin_unlock_irqrestore(&queue->lock, flags);
  362. return skb;
  363. }
  364. #ifdef AR9170_QUEUE_DEBUG
  365. wiphy_err(ar->hw->wiphy,
  366. "ESS:[%pM] does not have any outstanding frames in queue.\n",
  367. mac);
  368. __ar9170_dump_txqueue(ar, queue);
  369. #endif /* AR9170_QUEUE_DEBUG */
  370. spin_unlock_irqrestore(&queue->lock, flags);
  371. return NULL;
  372. }
  373. /*
  374. * This worker tries to keeps an maintain tx_status queues.
  375. * So we can guarantee that incoming tx_status reports are
  376. * actually for a pending frame.
  377. */
  378. static void ar9170_tx_janitor(struct work_struct *work)
  379. {
  380. struct ar9170 *ar = container_of(work, struct ar9170,
  381. tx_janitor.work);
  382. struct sk_buff_head waste;
  383. unsigned int i;
  384. bool resched = false;
  385. if (unlikely(!IS_STARTED(ar)))
  386. return ;
  387. skb_queue_head_init(&waste);
  388. for (i = 0; i < __AR9170_NUM_TXQ; i++) {
  389. #ifdef AR9170_QUEUE_DEBUG
  390. wiphy_debug(ar->hw->wiphy, "garbage collector scans queue:%d\n",
  391. i);
  392. ar9170_dump_txqueue(ar, &ar->tx_pending[i]);
  393. ar9170_dump_txqueue(ar, &ar->tx_status[i]);
  394. #endif /* AR9170_QUEUE_DEBUG */
  395. ar9170_recycle_expired(ar, &ar->tx_status[i], &waste);
  396. ar9170_recycle_expired(ar, &ar->tx_pending[i], &waste);
  397. skb_queue_purge(&waste);
  398. if (!skb_queue_empty(&ar->tx_status[i]) ||
  399. !skb_queue_empty(&ar->tx_pending[i]))
  400. resched = true;
  401. }
  402. if (!resched)
  403. return;
  404. ieee80211_queue_delayed_work(ar->hw,
  405. &ar->tx_janitor,
  406. msecs_to_jiffies(AR9170_JANITOR_DELAY));
  407. }
  408. void ar9170_handle_command_response(struct ar9170 *ar, void *buf, u32 len)
  409. {
  410. struct ar9170_cmd_response *cmd = (void *) buf;
  411. if ((cmd->type & 0xc0) != 0xc0) {
  412. ar->callback_cmd(ar, len, buf);
  413. return;
  414. }
  415. /* hardware event handlers */
  416. switch (cmd->type) {
  417. case 0xc1: {
  418. /*
  419. * TX status notification:
  420. * bytes: 0c c1 XX YY M1 M2 M3 M4 M5 M6 R4 R3 R2 R1 S2 S1
  421. *
  422. * XX always 81
  423. * YY always 00
  424. * M1-M6 is the MAC address
  425. * R1-R4 is the transmit rate
  426. * S1-S2 is the transmit status
  427. */
  428. struct sk_buff *skb;
  429. u32 phy = le32_to_cpu(cmd->tx_status.rate);
  430. u32 q = (phy & AR9170_TX_PHY_QOS_MASK) >>
  431. AR9170_TX_PHY_QOS_SHIFT;
  432. #ifdef AR9170_QUEUE_DEBUG
  433. wiphy_debug(ar->hw->wiphy,
  434. "recv tx_status for %pm, p:%08x, q:%d\n",
  435. 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. pr_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. wiphy_debug(ar->hw->wiphy,
  585. "received frame with suspicious error code (%#x).\n",
  586. 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. wiphy_err(ar->hw->wiphy,
  611. "invalid plcp cck rate (%x).\n",
  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. wiphy_err(ar->hw->wiphy,
  646. "invalid plcp ofdm rate (%x).\n",
  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. wiphy_err(ar->hw->wiphy, "invalid modulation\n");
  664. return -EINVAL;
  665. }
  666. return 0;
  667. }
  668. static void ar9170_rx_phy_status(struct ar9170 *ar,
  669. struct ar9170_rx_phystatus *phy,
  670. struct ieee80211_rx_status *status)
  671. {
  672. int i;
  673. BUILD_BUG_ON(sizeof(struct ar9170_rx_phystatus) != 20);
  674. for (i = 0; i < 3; i++)
  675. if (phy->rssi[i] != 0x80)
  676. status->antenna |= BIT(i);
  677. /* post-process RSSI */
  678. for (i = 0; i < 7; i++)
  679. if (phy->rssi[i] & 0x80)
  680. phy->rssi[i] = ((phy->rssi[i] & 0x7f) + 1) & 0x7f;
  681. /* TODO: we could do something with phy_errors */
  682. status->signal = ar->noise[0] + phy->rssi_combined;
  683. }
  684. static struct sk_buff *ar9170_rx_copy_data(u8 *buf, int len)
  685. {
  686. struct sk_buff *skb;
  687. int reserved = 0;
  688. struct ieee80211_hdr *hdr = (void *) buf;
  689. if (ieee80211_is_data_qos(hdr->frame_control)) {
  690. u8 *qc = ieee80211_get_qos_ctl(hdr);
  691. reserved += NET_IP_ALIGN;
  692. if (*qc & IEEE80211_QOS_CONTROL_A_MSDU_PRESENT)
  693. reserved += NET_IP_ALIGN;
  694. }
  695. if (ieee80211_has_a4(hdr->frame_control))
  696. reserved += NET_IP_ALIGN;
  697. reserved = 32 + (reserved & NET_IP_ALIGN);
  698. skb = dev_alloc_skb(len + reserved);
  699. if (likely(skb)) {
  700. skb_reserve(skb, reserved);
  701. memcpy(skb_put(skb, len), buf, len);
  702. }
  703. return skb;
  704. }
  705. /*
  706. * If the frame alignment is right (or the kernel has
  707. * CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS), and there
  708. * is only a single MPDU in the USB frame, then we could
  709. * submit to mac80211 the SKB directly. However, since
  710. * there may be multiple packets in one SKB in stream
  711. * mode, and we need to observe the proper ordering,
  712. * this is non-trivial.
  713. */
  714. static void ar9170_handle_mpdu(struct ar9170 *ar, u8 *buf, int len)
  715. {
  716. struct ar9170_rx_head *head;
  717. struct ar9170_rx_macstatus *mac;
  718. struct ar9170_rx_phystatus *phy = NULL;
  719. struct ieee80211_rx_status status;
  720. struct sk_buff *skb;
  721. int mpdu_len;
  722. if (unlikely(!IS_STARTED(ar) || len < (sizeof(*mac))))
  723. return ;
  724. /* Received MPDU */
  725. mpdu_len = len - sizeof(*mac);
  726. mac = (void *)(buf + mpdu_len);
  727. if (unlikely(mac->error & AR9170_RX_ERROR_FATAL)) {
  728. /* this frame is too damaged and can't be used - drop it */
  729. return ;
  730. }
  731. switch (mac->status & AR9170_RX_STATUS_MPDU_MASK) {
  732. case AR9170_RX_STATUS_MPDU_FIRST:
  733. /* first mpdu packet has the plcp header */
  734. if (likely(mpdu_len >= sizeof(struct ar9170_rx_head))) {
  735. head = (void *) buf;
  736. memcpy(&ar->rx_mpdu.plcp, (void *) buf,
  737. sizeof(struct ar9170_rx_head));
  738. mpdu_len -= sizeof(struct ar9170_rx_head);
  739. buf += sizeof(struct ar9170_rx_head);
  740. ar->rx_mpdu.has_plcp = true;
  741. } else {
  742. if (ar9170_nag_limiter(ar))
  743. wiphy_err(ar->hw->wiphy,
  744. "plcp info is clipped.\n");
  745. return ;
  746. }
  747. break;
  748. case AR9170_RX_STATUS_MPDU_LAST:
  749. /* last mpdu has a extra tail with phy status information */
  750. if (likely(mpdu_len >= sizeof(struct ar9170_rx_phystatus))) {
  751. mpdu_len -= sizeof(struct ar9170_rx_phystatus);
  752. phy = (void *)(buf + mpdu_len);
  753. } else {
  754. if (ar9170_nag_limiter(ar))
  755. wiphy_err(ar->hw->wiphy,
  756. "frame tail is clipped.\n");
  757. return ;
  758. }
  759. case AR9170_RX_STATUS_MPDU_MIDDLE:
  760. /* middle mpdus are just data */
  761. if (unlikely(!ar->rx_mpdu.has_plcp)) {
  762. if (!ar9170_nag_limiter(ar))
  763. return ;
  764. wiphy_err(ar->hw->wiphy,
  765. "rx stream did not start with a first_mpdu frame tag.\n");
  766. return ;
  767. }
  768. head = &ar->rx_mpdu.plcp;
  769. break;
  770. case AR9170_RX_STATUS_MPDU_SINGLE:
  771. /* single mpdu - has plcp (head) and phy status (tail) */
  772. head = (void *) buf;
  773. mpdu_len -= sizeof(struct ar9170_rx_head);
  774. mpdu_len -= sizeof(struct ar9170_rx_phystatus);
  775. buf += sizeof(struct ar9170_rx_head);
  776. phy = (void *)(buf + mpdu_len);
  777. break;
  778. default:
  779. BUG_ON(1);
  780. break;
  781. }
  782. if (unlikely(mpdu_len < FCS_LEN))
  783. return ;
  784. memset(&status, 0, sizeof(status));
  785. if (unlikely(ar9170_rx_mac_status(ar, head, mac, &status)))
  786. return ;
  787. if (phy)
  788. ar9170_rx_phy_status(ar, phy, &status);
  789. skb = ar9170_rx_copy_data(buf, mpdu_len);
  790. if (likely(skb)) {
  791. memcpy(IEEE80211_SKB_RXCB(skb), &status, sizeof(status));
  792. ieee80211_rx_irqsafe(ar->hw, skb);
  793. }
  794. }
  795. void ar9170_rx(struct ar9170 *ar, struct sk_buff *skb)
  796. {
  797. unsigned int i, tlen, resplen, wlen = 0, clen = 0;
  798. u8 *tbuf, *respbuf;
  799. tbuf = skb->data;
  800. tlen = skb->len;
  801. while (tlen >= 4) {
  802. clen = tbuf[1] << 8 | tbuf[0];
  803. wlen = ALIGN(clen, 4);
  804. /* check if this is stream has a valid tag.*/
  805. if (tbuf[2] != 0 || tbuf[3] != 0x4e) {
  806. /*
  807. * TODO: handle the highly unlikely event that the
  808. * corrupted stream has the TAG at the right position.
  809. */
  810. /* check if the frame can be repaired. */
  811. if (!ar->rx_failover_missing) {
  812. /* this is no "short read". */
  813. if (ar9170_nag_limiter(ar)) {
  814. wiphy_err(ar->hw->wiphy,
  815. "missing tag!\n");
  816. goto err_telluser;
  817. } else
  818. goto err_silent;
  819. }
  820. if (ar->rx_failover_missing > tlen) {
  821. if (ar9170_nag_limiter(ar)) {
  822. wiphy_err(ar->hw->wiphy,
  823. "possible multi stream corruption!\n");
  824. goto err_telluser;
  825. } else
  826. goto err_silent;
  827. }
  828. memcpy(skb_put(ar->rx_failover, tlen), tbuf, tlen);
  829. ar->rx_failover_missing -= tlen;
  830. if (ar->rx_failover_missing <= 0) {
  831. /*
  832. * nested ar9170_rx call!
  833. * termination is guranteed, even when the
  834. * combined frame also have a element with
  835. * a bad tag.
  836. */
  837. ar->rx_failover_missing = 0;
  838. ar9170_rx(ar, ar->rx_failover);
  839. skb_reset_tail_pointer(ar->rx_failover);
  840. skb_trim(ar->rx_failover, 0);
  841. }
  842. return ;
  843. }
  844. /* check if stream is clipped */
  845. if (wlen > tlen - 4) {
  846. if (ar->rx_failover_missing) {
  847. /* TODO: handle double stream corruption. */
  848. if (ar9170_nag_limiter(ar)) {
  849. wiphy_err(ar->hw->wiphy,
  850. "double rx stream corruption!\n");
  851. goto err_telluser;
  852. } else
  853. goto err_silent;
  854. }
  855. /*
  856. * save incomplete data set.
  857. * the firmware will resend the missing bits when
  858. * the rx - descriptor comes round again.
  859. */
  860. memcpy(skb_put(ar->rx_failover, tlen), tbuf, tlen);
  861. ar->rx_failover_missing = clen - tlen;
  862. return ;
  863. }
  864. resplen = clen;
  865. respbuf = tbuf + 4;
  866. tbuf += wlen + 4;
  867. tlen -= wlen + 4;
  868. i = 0;
  869. /* weird thing, but this is the same in the original driver */
  870. while (resplen > 2 && i < 12 &&
  871. respbuf[0] == 0xff && respbuf[1] == 0xff) {
  872. i += 2;
  873. resplen -= 2;
  874. respbuf += 2;
  875. }
  876. if (resplen < 4)
  877. continue;
  878. /* found the 6 * 0xffff marker? */
  879. if (i == 12)
  880. ar9170_handle_command_response(ar, respbuf, resplen);
  881. else
  882. ar9170_handle_mpdu(ar, respbuf, clen);
  883. }
  884. if (tlen) {
  885. if (net_ratelimit())
  886. wiphy_err(ar->hw->wiphy,
  887. "%d bytes of unprocessed data left in rx stream!\n",
  888. tlen);
  889. goto err_telluser;
  890. }
  891. return ;
  892. err_telluser:
  893. wiphy_err(ar->hw->wiphy,
  894. "damaged RX stream data [want:%d, data:%d, rx:%d, pending:%d ]\n",
  895. clen, wlen, tlen, ar->rx_failover_missing);
  896. if (ar->rx_failover_missing)
  897. print_hex_dump_bytes("rxbuf:", DUMP_PREFIX_OFFSET,
  898. ar->rx_failover->data,
  899. ar->rx_failover->len);
  900. print_hex_dump_bytes("stream:", DUMP_PREFIX_OFFSET,
  901. skb->data, skb->len);
  902. wiphy_err(ar->hw->wiphy,
  903. "If you see this message frequently, please check your hardware and cables.\n");
  904. err_silent:
  905. if (ar->rx_failover_missing) {
  906. skb_reset_tail_pointer(ar->rx_failover);
  907. skb_trim(ar->rx_failover, 0);
  908. ar->rx_failover_missing = 0;
  909. }
  910. }
  911. #define AR9170_FILL_QUEUE(queue, ai_fs, cwmin, cwmax, _txop) \
  912. do { \
  913. queue.aifs = ai_fs; \
  914. queue.cw_min = cwmin; \
  915. queue.cw_max = cwmax; \
  916. queue.txop = _txop; \
  917. } while (0)
  918. static int ar9170_op_start(struct ieee80211_hw *hw)
  919. {
  920. struct ar9170 *ar = hw->priv;
  921. int err, i;
  922. mutex_lock(&ar->mutex);
  923. /* reinitialize queues statistics */
  924. memset(&ar->tx_stats, 0, sizeof(ar->tx_stats));
  925. for (i = 0; i < __AR9170_NUM_TXQ; i++)
  926. ar->tx_stats[i].limit = AR9170_TXQ_DEPTH;
  927. /* reset QoS defaults */
  928. AR9170_FILL_QUEUE(ar->edcf[0], 3, 15, 1023, 0); /* BEST EFFORT*/
  929. AR9170_FILL_QUEUE(ar->edcf[1], 7, 15, 1023, 0); /* BACKGROUND */
  930. AR9170_FILL_QUEUE(ar->edcf[2], 2, 7, 15, 94); /* VIDEO */
  931. AR9170_FILL_QUEUE(ar->edcf[3], 2, 3, 7, 47); /* VOICE */
  932. AR9170_FILL_QUEUE(ar->edcf[4], 2, 3, 7, 0); /* SPECIAL */
  933. /* set sane AMPDU defaults */
  934. ar->global_ampdu_density = 6;
  935. ar->global_ampdu_factor = 3;
  936. ar->bad_hw_nagger = jiffies;
  937. err = ar->open(ar);
  938. if (err)
  939. goto out;
  940. err = ar9170_init_mac(ar);
  941. if (err)
  942. goto out;
  943. err = ar9170_set_qos(ar);
  944. if (err)
  945. goto out;
  946. err = ar9170_init_phy(ar, IEEE80211_BAND_2GHZ);
  947. if (err)
  948. goto out;
  949. err = ar9170_init_rf(ar);
  950. if (err)
  951. goto out;
  952. /* start DMA */
  953. err = ar9170_write_reg(ar, 0x1c3d30, 0x100);
  954. if (err)
  955. goto out;
  956. ar->state = AR9170_STARTED;
  957. out:
  958. mutex_unlock(&ar->mutex);
  959. return err;
  960. }
  961. static void ar9170_op_stop(struct ieee80211_hw *hw)
  962. {
  963. struct ar9170 *ar = hw->priv;
  964. unsigned int i;
  965. if (IS_STARTED(ar))
  966. ar->state = AR9170_IDLE;
  967. cancel_delayed_work_sync(&ar->tx_janitor);
  968. #ifdef CONFIG_AR9170_LEDS
  969. cancel_delayed_work_sync(&ar->led_work);
  970. #endif
  971. cancel_work_sync(&ar->beacon_work);
  972. mutex_lock(&ar->mutex);
  973. if (IS_ACCEPTING_CMD(ar)) {
  974. ar9170_set_leds_state(ar, 0);
  975. /* stop DMA */
  976. ar9170_write_reg(ar, 0x1c3d30, 0);
  977. ar->stop(ar);
  978. }
  979. for (i = 0; i < __AR9170_NUM_TXQ; i++) {
  980. skb_queue_purge(&ar->tx_pending[i]);
  981. skb_queue_purge(&ar->tx_status[i]);
  982. }
  983. mutex_unlock(&ar->mutex);
  984. }
  985. static int ar9170_tx_prepare(struct ar9170 *ar, struct sk_buff *skb)
  986. {
  987. struct ieee80211_hdr *hdr;
  988. struct ar9170_tx_control *txc;
  989. struct ieee80211_tx_info *info;
  990. struct ieee80211_tx_rate *txrate;
  991. struct ar9170_tx_info *arinfo;
  992. unsigned int queue = skb_get_queue_mapping(skb);
  993. u16 keytype = 0;
  994. u16 len, icv = 0;
  995. BUILD_BUG_ON(sizeof(*arinfo) > sizeof(info->rate_driver_data));
  996. hdr = (void *)skb->data;
  997. info = IEEE80211_SKB_CB(skb);
  998. len = skb->len;
  999. txc = (void *)skb_push(skb, sizeof(*txc));
  1000. if (info->control.hw_key) {
  1001. icv = info->control.hw_key->icv_len;
  1002. switch (info->control.hw_key->cipher) {
  1003. case WLAN_CIPHER_SUITE_WEP40:
  1004. case WLAN_CIPHER_SUITE_WEP104:
  1005. case WLAN_CIPHER_SUITE_TKIP:
  1006. keytype = AR9170_TX_MAC_ENCR_RC4;
  1007. break;
  1008. case WLAN_CIPHER_SUITE_CCMP:
  1009. keytype = AR9170_TX_MAC_ENCR_AES;
  1010. break;
  1011. default:
  1012. WARN_ON(1);
  1013. goto err_out;
  1014. }
  1015. }
  1016. /* Length */
  1017. txc->length = cpu_to_le16(len + icv + 4);
  1018. txc->mac_control = cpu_to_le16(AR9170_TX_MAC_HW_DURATION |
  1019. AR9170_TX_MAC_BACKOFF);
  1020. txc->mac_control |= cpu_to_le16(ar9170_qos_hwmap[queue] <<
  1021. AR9170_TX_MAC_QOS_SHIFT);
  1022. txc->mac_control |= cpu_to_le16(keytype);
  1023. txc->phy_control = cpu_to_le32(0);
  1024. if (info->flags & IEEE80211_TX_CTL_NO_ACK)
  1025. txc->mac_control |= cpu_to_le16(AR9170_TX_MAC_NO_ACK);
  1026. txrate = &info->control.rates[0];
  1027. if (txrate->flags & IEEE80211_TX_RC_USE_CTS_PROTECT)
  1028. txc->mac_control |= cpu_to_le16(AR9170_TX_MAC_PROT_CTS);
  1029. else if (txrate->flags & IEEE80211_TX_RC_USE_RTS_CTS)
  1030. txc->mac_control |= cpu_to_le16(AR9170_TX_MAC_PROT_RTS);
  1031. arinfo = (void *)info->rate_driver_data;
  1032. arinfo->timeout = jiffies + msecs_to_jiffies(AR9170_QUEUE_TIMEOUT);
  1033. if (!(info->flags & IEEE80211_TX_CTL_NO_ACK) &&
  1034. (is_valid_ether_addr(ieee80211_get_DA(hdr)))) {
  1035. /*
  1036. * WARNING:
  1037. * Putting the QoS queue bits into an unexplored territory is
  1038. * certainly not elegant.
  1039. *
  1040. * In my defense: This idea provides a reasonable way to
  1041. * smuggle valuable information to the tx_status callback.
  1042. * Also, the idea behind this bit-abuse came straight from
  1043. * the original driver code.
  1044. */
  1045. txc->phy_control |=
  1046. cpu_to_le32(queue << AR9170_TX_PHY_QOS_SHIFT);
  1047. txc->mac_control |= cpu_to_le16(AR9170_TX_MAC_RATE_PROBE);
  1048. }
  1049. return 0;
  1050. err_out:
  1051. skb_pull(skb, sizeof(*txc));
  1052. return -EINVAL;
  1053. }
  1054. static void ar9170_tx_prepare_phy(struct ar9170 *ar, struct sk_buff *skb)
  1055. {
  1056. struct ar9170_tx_control *txc;
  1057. struct ieee80211_tx_info *info;
  1058. struct ieee80211_rate *rate = NULL;
  1059. struct ieee80211_tx_rate *txrate;
  1060. u32 power, chains;
  1061. txc = (void *) skb->data;
  1062. info = IEEE80211_SKB_CB(skb);
  1063. txrate = &info->control.rates[0];
  1064. if (txrate->flags & IEEE80211_TX_RC_GREEN_FIELD)
  1065. txc->phy_control |= cpu_to_le32(AR9170_TX_PHY_GREENFIELD);
  1066. if (txrate->flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
  1067. txc->phy_control |= cpu_to_le32(AR9170_TX_PHY_SHORT_PREAMBLE);
  1068. if (txrate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
  1069. txc->phy_control |= cpu_to_le32(AR9170_TX_PHY_BW_40MHZ);
  1070. /* this works because 40 MHz is 2 and dup is 3 */
  1071. if (txrate->flags & IEEE80211_TX_RC_DUP_DATA)
  1072. txc->phy_control |= cpu_to_le32(AR9170_TX_PHY_BW_40MHZ_DUP);
  1073. if (txrate->flags & IEEE80211_TX_RC_SHORT_GI)
  1074. txc->phy_control |= cpu_to_le32(AR9170_TX_PHY_SHORT_GI);
  1075. if (txrate->flags & IEEE80211_TX_RC_MCS) {
  1076. u32 r = txrate->idx;
  1077. u8 *txpower;
  1078. /* heavy clip control */
  1079. txc->phy_control |= cpu_to_le32((r & 0x7) << 7);
  1080. r <<= AR9170_TX_PHY_MCS_SHIFT;
  1081. BUG_ON(r & ~AR9170_TX_PHY_MCS_MASK);
  1082. txc->phy_control |= cpu_to_le32(r & AR9170_TX_PHY_MCS_MASK);
  1083. txc->phy_control |= cpu_to_le32(AR9170_TX_PHY_MOD_HT);
  1084. if (txrate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH) {
  1085. if (info->band == IEEE80211_BAND_5GHZ)
  1086. txpower = ar->power_5G_ht40;
  1087. else
  1088. txpower = ar->power_2G_ht40;
  1089. } else {
  1090. if (info->band == IEEE80211_BAND_5GHZ)
  1091. txpower = ar->power_5G_ht20;
  1092. else
  1093. txpower = ar->power_2G_ht20;
  1094. }
  1095. power = txpower[(txrate->idx) & 7];
  1096. } else {
  1097. u8 *txpower;
  1098. u32 mod;
  1099. u32 phyrate;
  1100. u8 idx = txrate->idx;
  1101. if (info->band != IEEE80211_BAND_2GHZ) {
  1102. idx += 4;
  1103. txpower = ar->power_5G_leg;
  1104. mod = AR9170_TX_PHY_MOD_OFDM;
  1105. } else {
  1106. if (idx < 4) {
  1107. txpower = ar->power_2G_cck;
  1108. mod = AR9170_TX_PHY_MOD_CCK;
  1109. } else {
  1110. mod = AR9170_TX_PHY_MOD_OFDM;
  1111. txpower = ar->power_2G_ofdm;
  1112. }
  1113. }
  1114. rate = &__ar9170_ratetable[idx];
  1115. phyrate = rate->hw_value & 0xF;
  1116. power = txpower[(rate->hw_value & 0x30) >> 4];
  1117. phyrate <<= AR9170_TX_PHY_MCS_SHIFT;
  1118. txc->phy_control |= cpu_to_le32(mod);
  1119. txc->phy_control |= cpu_to_le32(phyrate);
  1120. }
  1121. power <<= AR9170_TX_PHY_TX_PWR_SHIFT;
  1122. power &= AR9170_TX_PHY_TX_PWR_MASK;
  1123. txc->phy_control |= cpu_to_le32(power);
  1124. /* set TX chains */
  1125. if (ar->eeprom.tx_mask == 1) {
  1126. chains = AR9170_TX_PHY_TXCHAIN_1;
  1127. } else {
  1128. chains = AR9170_TX_PHY_TXCHAIN_2;
  1129. /* >= 36M legacy OFDM - use only one chain */
  1130. if (rate && rate->bitrate >= 360)
  1131. chains = AR9170_TX_PHY_TXCHAIN_1;
  1132. }
  1133. txc->phy_control |= cpu_to_le32(chains << AR9170_TX_PHY_TXCHAIN_SHIFT);
  1134. }
  1135. static void ar9170_tx(struct ar9170 *ar)
  1136. {
  1137. struct sk_buff *skb;
  1138. unsigned long flags;
  1139. struct ieee80211_tx_info *info;
  1140. struct ar9170_tx_info *arinfo;
  1141. unsigned int i, frames, frames_failed, remaining_space;
  1142. int err;
  1143. bool schedule_garbagecollector = false;
  1144. BUILD_BUG_ON(sizeof(*arinfo) > sizeof(info->rate_driver_data));
  1145. if (unlikely(!IS_STARTED(ar)))
  1146. return ;
  1147. remaining_space = AR9170_TX_MAX_PENDING;
  1148. for (i = 0; i < __AR9170_NUM_TXQ; i++) {
  1149. spin_lock_irqsave(&ar->tx_stats_lock, flags);
  1150. frames = min(ar->tx_stats[i].limit - ar->tx_stats[i].len,
  1151. skb_queue_len(&ar->tx_pending[i]));
  1152. if (remaining_space < frames) {
  1153. #ifdef AR9170_QUEUE_DEBUG
  1154. wiphy_debug(ar->hw->wiphy,
  1155. "tx quota reached queue:%d, "
  1156. "remaining slots:%d, needed:%d\n",
  1157. i, remaining_space, frames);
  1158. #endif /* AR9170_QUEUE_DEBUG */
  1159. frames = remaining_space;
  1160. }
  1161. ar->tx_stats[i].len += frames;
  1162. ar->tx_stats[i].count += frames;
  1163. if (ar->tx_stats[i].len >= ar->tx_stats[i].limit) {
  1164. #ifdef AR9170_QUEUE_DEBUG
  1165. wiphy_debug(ar->hw->wiphy, "queue %d full\n", i);
  1166. wiphy_debug(ar->hw->wiphy, "stuck frames: ===>\n");
  1167. ar9170_dump_txqueue(ar, &ar->tx_pending[i]);
  1168. ar9170_dump_txqueue(ar, &ar->tx_status[i]);
  1169. #endif /* AR9170_QUEUE_DEBUG */
  1170. #ifdef AR9170_QUEUE_STOP_DEBUG
  1171. wiphy_debug(ar->hw->wiphy, "stop queue %d\n", i);
  1172. __ar9170_dump_txstats(ar);
  1173. #endif /* AR9170_QUEUE_STOP_DEBUG */
  1174. ieee80211_stop_queue(ar->hw, i);
  1175. }
  1176. spin_unlock_irqrestore(&ar->tx_stats_lock, flags);
  1177. if (!frames)
  1178. continue;
  1179. frames_failed = 0;
  1180. while (frames) {
  1181. skb = skb_dequeue(&ar->tx_pending[i]);
  1182. if (unlikely(!skb)) {
  1183. frames_failed += frames;
  1184. frames = 0;
  1185. break;
  1186. }
  1187. info = IEEE80211_SKB_CB(skb);
  1188. arinfo = (void *) info->rate_driver_data;
  1189. /* TODO: cancel stuck frames */
  1190. arinfo->timeout = jiffies +
  1191. msecs_to_jiffies(AR9170_TX_TIMEOUT);
  1192. #ifdef AR9170_QUEUE_DEBUG
  1193. wiphy_debug(ar->hw->wiphy, "send frame q:%d =>\n", i);
  1194. ar9170_print_txheader(ar, skb);
  1195. #endif /* AR9170_QUEUE_DEBUG */
  1196. err = ar->tx(ar, skb);
  1197. if (unlikely(err)) {
  1198. frames_failed++;
  1199. dev_kfree_skb_any(skb);
  1200. } else {
  1201. remaining_space--;
  1202. schedule_garbagecollector = true;
  1203. }
  1204. frames--;
  1205. }
  1206. #ifdef AR9170_QUEUE_DEBUG
  1207. wiphy_debug(ar->hw->wiphy,
  1208. "ar9170_tx report for queue %d\n", i);
  1209. wiphy_debug(ar->hw->wiphy,
  1210. "unprocessed pending frames left:\n");
  1211. ar9170_dump_txqueue(ar, &ar->tx_pending[i]);
  1212. #endif /* AR9170_QUEUE_DEBUG */
  1213. if (unlikely(frames_failed)) {
  1214. #ifdef AR9170_QUEUE_DEBUG
  1215. wiphy_debug(ar->hw->wiphy,
  1216. "frames failed %d =>\n", frames_failed);
  1217. #endif /* AR9170_QUEUE_DEBUG */
  1218. spin_lock_irqsave(&ar->tx_stats_lock, flags);
  1219. ar->tx_stats[i].len -= frames_failed;
  1220. ar->tx_stats[i].count -= frames_failed;
  1221. #ifdef AR9170_QUEUE_STOP_DEBUG
  1222. wiphy_debug(ar->hw->wiphy, "wake queue %d\n", i);
  1223. __ar9170_dump_txstats(ar);
  1224. #endif /* AR9170_QUEUE_STOP_DEBUG */
  1225. ieee80211_wake_queue(ar->hw, i);
  1226. spin_unlock_irqrestore(&ar->tx_stats_lock, flags);
  1227. }
  1228. }
  1229. if (!schedule_garbagecollector)
  1230. return;
  1231. ieee80211_queue_delayed_work(ar->hw,
  1232. &ar->tx_janitor,
  1233. msecs_to_jiffies(AR9170_JANITOR_DELAY));
  1234. }
  1235. int ar9170_op_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
  1236. {
  1237. struct ar9170 *ar = hw->priv;
  1238. struct ieee80211_tx_info *info;
  1239. unsigned int queue;
  1240. if (unlikely(!IS_STARTED(ar)))
  1241. goto err_free;
  1242. if (unlikely(ar9170_tx_prepare(ar, skb)))
  1243. goto err_free;
  1244. queue = skb_get_queue_mapping(skb);
  1245. info = IEEE80211_SKB_CB(skb);
  1246. ar9170_tx_prepare_phy(ar, skb);
  1247. skb_queue_tail(&ar->tx_pending[queue], skb);
  1248. ar9170_tx(ar);
  1249. return NETDEV_TX_OK;
  1250. err_free:
  1251. dev_kfree_skb_any(skb);
  1252. return NETDEV_TX_OK;
  1253. }
  1254. static int ar9170_op_add_interface(struct ieee80211_hw *hw,
  1255. struct ieee80211_vif *vif)
  1256. {
  1257. struct ar9170 *ar = hw->priv;
  1258. struct ath_common *common = &ar->common;
  1259. int err = 0;
  1260. mutex_lock(&ar->mutex);
  1261. if (ar->vif) {
  1262. err = -EBUSY;
  1263. goto unlock;
  1264. }
  1265. ar->vif = vif;
  1266. memcpy(common->macaddr, vif->addr, ETH_ALEN);
  1267. if (modparam_nohwcrypt || (ar->vif->type != NL80211_IFTYPE_STATION)) {
  1268. ar->rx_software_decryption = true;
  1269. ar->disable_offload = true;
  1270. }
  1271. ar->cur_filter = 0;
  1272. err = ar9170_update_frame_filter(ar, AR9170_MAC_REG_FTF_DEFAULTS);
  1273. if (err)
  1274. goto unlock;
  1275. err = ar9170_set_operating_mode(ar);
  1276. unlock:
  1277. mutex_unlock(&ar->mutex);
  1278. return err;
  1279. }
  1280. static void ar9170_op_remove_interface(struct ieee80211_hw *hw,
  1281. struct ieee80211_vif *vif)
  1282. {
  1283. struct ar9170 *ar = hw->priv;
  1284. mutex_lock(&ar->mutex);
  1285. ar->vif = NULL;
  1286. ar9170_update_frame_filter(ar, 0);
  1287. ar9170_set_beacon_timers(ar);
  1288. dev_kfree_skb(ar->beacon);
  1289. ar->beacon = NULL;
  1290. ar->sniffer_enabled = false;
  1291. ar->rx_software_decryption = false;
  1292. ar9170_set_operating_mode(ar);
  1293. mutex_unlock(&ar->mutex);
  1294. }
  1295. static int ar9170_op_config(struct ieee80211_hw *hw, u32 changed)
  1296. {
  1297. struct ar9170 *ar = hw->priv;
  1298. int err = 0;
  1299. mutex_lock(&ar->mutex);
  1300. if (changed & IEEE80211_CONF_CHANGE_LISTEN_INTERVAL) {
  1301. /* TODO */
  1302. err = 0;
  1303. }
  1304. if (changed & IEEE80211_CONF_CHANGE_PS) {
  1305. /* TODO */
  1306. err = 0;
  1307. }
  1308. if (changed & IEEE80211_CONF_CHANGE_POWER) {
  1309. /* TODO */
  1310. err = 0;
  1311. }
  1312. if (changed & IEEE80211_CONF_CHANGE_RETRY_LIMITS) {
  1313. /*
  1314. * is it long_frame_max_tx_count or short_frame_max_tx_count?
  1315. */
  1316. err = ar9170_set_hwretry_limit(ar,
  1317. ar->hw->conf.long_frame_max_tx_count);
  1318. if (err)
  1319. goto out;
  1320. }
  1321. if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
  1322. /* adjust slot time for 5 GHz */
  1323. err = ar9170_set_slot_time(ar);
  1324. if (err)
  1325. goto out;
  1326. err = ar9170_set_dyn_sifs_ack(ar);
  1327. if (err)
  1328. goto out;
  1329. err = ar9170_set_channel(ar, hw->conf.channel,
  1330. AR9170_RFI_NONE,
  1331. nl80211_to_ar9170(hw->conf.channel_type));
  1332. if (err)
  1333. goto out;
  1334. }
  1335. out:
  1336. mutex_unlock(&ar->mutex);
  1337. return err;
  1338. }
  1339. static u64 ar9170_op_prepare_multicast(struct ieee80211_hw *hw,
  1340. struct netdev_hw_addr_list *mc_list)
  1341. {
  1342. u64 mchash;
  1343. struct netdev_hw_addr *ha;
  1344. /* always get broadcast frames */
  1345. mchash = 1ULL << (0xff >> 2);
  1346. netdev_hw_addr_list_for_each(ha, mc_list)
  1347. mchash |= 1ULL << (ha->addr[5] >> 2);
  1348. return mchash;
  1349. }
  1350. static void ar9170_op_configure_filter(struct ieee80211_hw *hw,
  1351. unsigned int changed_flags,
  1352. unsigned int *new_flags,
  1353. u64 multicast)
  1354. {
  1355. struct ar9170 *ar = hw->priv;
  1356. if (unlikely(!IS_ACCEPTING_CMD(ar)))
  1357. return ;
  1358. mutex_lock(&ar->mutex);
  1359. /* mask supported flags */
  1360. *new_flags &= FIF_ALLMULTI | FIF_CONTROL | FIF_BCN_PRBRESP_PROMISC |
  1361. FIF_PROMISC_IN_BSS | FIF_FCSFAIL | FIF_PLCPFAIL;
  1362. ar->filter_state = *new_flags;
  1363. /*
  1364. * We can support more by setting the sniffer bit and
  1365. * then checking the error flags, later.
  1366. */
  1367. if (changed_flags & FIF_ALLMULTI && *new_flags & FIF_ALLMULTI)
  1368. multicast = ~0ULL;
  1369. if (multicast != ar->cur_mc_hash)
  1370. ar9170_update_multicast(ar, multicast);
  1371. if (changed_flags & FIF_CONTROL) {
  1372. u32 filter = AR9170_MAC_REG_FTF_PSPOLL |
  1373. AR9170_MAC_REG_FTF_RTS |
  1374. AR9170_MAC_REG_FTF_CTS |
  1375. AR9170_MAC_REG_FTF_ACK |
  1376. AR9170_MAC_REG_FTF_CFE |
  1377. AR9170_MAC_REG_FTF_CFE_ACK;
  1378. if (*new_flags & FIF_CONTROL)
  1379. filter |= ar->cur_filter;
  1380. else
  1381. filter &= (~ar->cur_filter);
  1382. ar9170_update_frame_filter(ar, filter);
  1383. }
  1384. if (changed_flags & FIF_PROMISC_IN_BSS) {
  1385. ar->sniffer_enabled = ((*new_flags) & FIF_PROMISC_IN_BSS) != 0;
  1386. ar9170_set_operating_mode(ar);
  1387. }
  1388. mutex_unlock(&ar->mutex);
  1389. }
  1390. static void ar9170_op_bss_info_changed(struct ieee80211_hw *hw,
  1391. struct ieee80211_vif *vif,
  1392. struct ieee80211_bss_conf *bss_conf,
  1393. u32 changed)
  1394. {
  1395. struct ar9170 *ar = hw->priv;
  1396. struct ath_common *common = &ar->common;
  1397. int err = 0;
  1398. mutex_lock(&ar->mutex);
  1399. if (changed & BSS_CHANGED_BSSID) {
  1400. memcpy(common->curbssid, bss_conf->bssid, ETH_ALEN);
  1401. err = ar9170_set_operating_mode(ar);
  1402. if (err)
  1403. goto out;
  1404. }
  1405. if (changed & BSS_CHANGED_BEACON_ENABLED)
  1406. ar->enable_beacon = bss_conf->enable_beacon;
  1407. if (changed & BSS_CHANGED_BEACON) {
  1408. err = ar9170_update_beacon(ar);
  1409. if (err)
  1410. goto out;
  1411. }
  1412. if (changed & (BSS_CHANGED_BEACON_ENABLED | BSS_CHANGED_BEACON |
  1413. BSS_CHANGED_BEACON_INT)) {
  1414. err = ar9170_set_beacon_timers(ar);
  1415. if (err)
  1416. goto out;
  1417. }
  1418. if (changed & BSS_CHANGED_ASSOC) {
  1419. #ifndef CONFIG_AR9170_LEDS
  1420. /* enable assoc LED. */
  1421. err = ar9170_set_leds_state(ar, bss_conf->assoc ? 2 : 0);
  1422. #endif /* CONFIG_AR9170_LEDS */
  1423. }
  1424. if (changed & BSS_CHANGED_HT) {
  1425. /* TODO */
  1426. err = 0;
  1427. }
  1428. if (changed & BSS_CHANGED_ERP_SLOT) {
  1429. err = ar9170_set_slot_time(ar);
  1430. if (err)
  1431. goto out;
  1432. }
  1433. if (changed & BSS_CHANGED_BASIC_RATES) {
  1434. err = ar9170_set_basic_rates(ar);
  1435. if (err)
  1436. goto out;
  1437. }
  1438. out:
  1439. mutex_unlock(&ar->mutex);
  1440. }
  1441. static u64 ar9170_op_get_tsf(struct ieee80211_hw *hw)
  1442. {
  1443. struct ar9170 *ar = hw->priv;
  1444. int err;
  1445. u64 tsf;
  1446. #define NR 3
  1447. static const u32 addr[NR] = { AR9170_MAC_REG_TSF_H,
  1448. AR9170_MAC_REG_TSF_L,
  1449. AR9170_MAC_REG_TSF_H };
  1450. u32 val[NR];
  1451. int loops = 0;
  1452. mutex_lock(&ar->mutex);
  1453. while (loops++ < 10) {
  1454. err = ar9170_read_mreg(ar, NR, addr, val);
  1455. if (err || val[0] == val[2])
  1456. break;
  1457. }
  1458. mutex_unlock(&ar->mutex);
  1459. if (WARN_ON(err))
  1460. return 0;
  1461. tsf = val[0];
  1462. tsf = (tsf << 32) | val[1];
  1463. return tsf;
  1464. #undef NR
  1465. }
  1466. static int ar9170_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
  1467. struct ieee80211_vif *vif, struct ieee80211_sta *sta,
  1468. struct ieee80211_key_conf *key)
  1469. {
  1470. struct ar9170 *ar = hw->priv;
  1471. int err = 0, i;
  1472. u8 ktype;
  1473. if ((!ar->vif) || (ar->disable_offload))
  1474. return -EOPNOTSUPP;
  1475. switch (key->cipher) {
  1476. case WLAN_CIPHER_SUITE_WEP40:
  1477. ktype = AR9170_ENC_ALG_WEP64;
  1478. break;
  1479. case WLAN_CIPHER_SUITE_WEP104:
  1480. ktype = AR9170_ENC_ALG_WEP128;
  1481. break;
  1482. case WLAN_CIPHER_SUITE_TKIP:
  1483. ktype = AR9170_ENC_ALG_TKIP;
  1484. break;
  1485. case WLAN_CIPHER_SUITE_CCMP:
  1486. ktype = AR9170_ENC_ALG_AESCCMP;
  1487. break;
  1488. default:
  1489. return -EOPNOTSUPP;
  1490. }
  1491. mutex_lock(&ar->mutex);
  1492. if (cmd == SET_KEY) {
  1493. if (unlikely(!IS_STARTED(ar))) {
  1494. err = -EOPNOTSUPP;
  1495. goto out;
  1496. }
  1497. /* group keys need all-zeroes address */
  1498. if (!(key->flags & IEEE80211_KEY_FLAG_PAIRWISE))
  1499. sta = NULL;
  1500. if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
  1501. for (i = 0; i < 64; i++)
  1502. if (!(ar->usedkeys & BIT(i)))
  1503. break;
  1504. if (i == 64) {
  1505. ar->rx_software_decryption = true;
  1506. ar9170_set_operating_mode(ar);
  1507. err = -ENOSPC;
  1508. goto out;
  1509. }
  1510. } else {
  1511. i = 64 + key->keyidx;
  1512. }
  1513. key->hw_key_idx = i;
  1514. err = ar9170_upload_key(ar, i, sta ? sta->addr : NULL, ktype, 0,
  1515. key->key, min_t(u8, 16, key->keylen));
  1516. if (err)
  1517. goto out;
  1518. if (key->cipher == WLAN_CIPHER_SUITE_TKIP) {
  1519. err = ar9170_upload_key(ar, i, sta ? sta->addr : NULL,
  1520. ktype, 1, key->key + 16, 16);
  1521. if (err)
  1522. goto out;
  1523. /*
  1524. * hardware is not capable generating the MMIC
  1525. * for fragmented frames!
  1526. */
  1527. key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
  1528. }
  1529. if (i < 64)
  1530. ar->usedkeys |= BIT(i);
  1531. key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  1532. } else {
  1533. if (unlikely(!IS_STARTED(ar))) {
  1534. /* The device is gone... together with the key ;-) */
  1535. err = 0;
  1536. goto out;
  1537. }
  1538. err = ar9170_disable_key(ar, key->hw_key_idx);
  1539. if (err)
  1540. goto out;
  1541. if (key->hw_key_idx < 64) {
  1542. ar->usedkeys &= ~BIT(key->hw_key_idx);
  1543. } else {
  1544. err = ar9170_upload_key(ar, key->hw_key_idx, NULL,
  1545. AR9170_ENC_ALG_NONE, 0,
  1546. NULL, 0);
  1547. if (err)
  1548. goto out;
  1549. if (key->cipher == WLAN_CIPHER_SUITE_TKIP) {
  1550. err = ar9170_upload_key(ar, key->hw_key_idx,
  1551. NULL,
  1552. AR9170_ENC_ALG_NONE, 1,
  1553. NULL, 0);
  1554. if (err)
  1555. goto out;
  1556. }
  1557. }
  1558. }
  1559. ar9170_regwrite_begin(ar);
  1560. ar9170_regwrite(AR9170_MAC_REG_ROLL_CALL_TBL_L, ar->usedkeys);
  1561. ar9170_regwrite(AR9170_MAC_REG_ROLL_CALL_TBL_H, ar->usedkeys >> 32);
  1562. ar9170_regwrite_finish();
  1563. err = ar9170_regwrite_result();
  1564. out:
  1565. mutex_unlock(&ar->mutex);
  1566. return err;
  1567. }
  1568. static int ar9170_get_stats(struct ieee80211_hw *hw,
  1569. struct ieee80211_low_level_stats *stats)
  1570. {
  1571. struct ar9170 *ar = hw->priv;
  1572. u32 val;
  1573. int err;
  1574. mutex_lock(&ar->mutex);
  1575. err = ar9170_read_reg(ar, AR9170_MAC_REG_TX_RETRY, &val);
  1576. ar->stats.dot11ACKFailureCount += val;
  1577. memcpy(stats, &ar->stats, sizeof(*stats));
  1578. mutex_unlock(&ar->mutex);
  1579. return 0;
  1580. }
  1581. static int ar9170_get_survey(struct ieee80211_hw *hw, int idx,
  1582. struct survey_info *survey)
  1583. {
  1584. struct ar9170 *ar = hw->priv;
  1585. struct ieee80211_conf *conf = &hw->conf;
  1586. if (idx != 0)
  1587. return -ENOENT;
  1588. /* TODO: update noise value, e.g. call ar9170_set_channel */
  1589. survey->channel = conf->channel;
  1590. survey->filled = SURVEY_INFO_NOISE_DBM;
  1591. survey->noise = ar->noise[0];
  1592. return 0;
  1593. }
  1594. static int ar9170_conf_tx(struct ieee80211_hw *hw, u16 queue,
  1595. const struct ieee80211_tx_queue_params *param)
  1596. {
  1597. struct ar9170 *ar = hw->priv;
  1598. int ret;
  1599. mutex_lock(&ar->mutex);
  1600. if (queue < __AR9170_NUM_TXQ) {
  1601. memcpy(&ar->edcf[ar9170_qos_hwmap[queue]],
  1602. param, sizeof(*param));
  1603. ret = ar9170_set_qos(ar);
  1604. } else {
  1605. ret = -EINVAL;
  1606. }
  1607. mutex_unlock(&ar->mutex);
  1608. return ret;
  1609. }
  1610. static int ar9170_ampdu_action(struct ieee80211_hw *hw,
  1611. struct ieee80211_vif *vif,
  1612. enum ieee80211_ampdu_mlme_action action,
  1613. struct ieee80211_sta *sta, u16 tid, u16 *ssn)
  1614. {
  1615. switch (action) {
  1616. case IEEE80211_AMPDU_RX_START:
  1617. case IEEE80211_AMPDU_RX_STOP:
  1618. /* Handled by firmware */
  1619. break;
  1620. default:
  1621. return -EOPNOTSUPP;
  1622. }
  1623. return 0;
  1624. }
  1625. static const struct ieee80211_ops ar9170_ops = {
  1626. .start = ar9170_op_start,
  1627. .stop = ar9170_op_stop,
  1628. .tx = ar9170_op_tx,
  1629. .add_interface = ar9170_op_add_interface,
  1630. .remove_interface = ar9170_op_remove_interface,
  1631. .config = ar9170_op_config,
  1632. .prepare_multicast = ar9170_op_prepare_multicast,
  1633. .configure_filter = ar9170_op_configure_filter,
  1634. .conf_tx = ar9170_conf_tx,
  1635. .bss_info_changed = ar9170_op_bss_info_changed,
  1636. .get_tsf = ar9170_op_get_tsf,
  1637. .set_key = ar9170_set_key,
  1638. .get_stats = ar9170_get_stats,
  1639. .get_survey = ar9170_get_survey,
  1640. .ampdu_action = ar9170_ampdu_action,
  1641. };
  1642. void *ar9170_alloc(size_t priv_size)
  1643. {
  1644. struct ieee80211_hw *hw;
  1645. struct ar9170 *ar;
  1646. struct sk_buff *skb;
  1647. int i;
  1648. /*
  1649. * this buffer is used for rx stream reconstruction.
  1650. * Under heavy load this device (or the transport layer?)
  1651. * tends to split the streams into separate rx descriptors.
  1652. */
  1653. skb = __dev_alloc_skb(AR9170_RX_STREAM_MAX_SIZE, GFP_KERNEL);
  1654. if (!skb)
  1655. goto err_nomem;
  1656. hw = ieee80211_alloc_hw(priv_size, &ar9170_ops);
  1657. if (!hw)
  1658. goto err_nomem;
  1659. ar = hw->priv;
  1660. ar->hw = hw;
  1661. ar->rx_failover = skb;
  1662. mutex_init(&ar->mutex);
  1663. spin_lock_init(&ar->cmdlock);
  1664. spin_lock_init(&ar->tx_stats_lock);
  1665. for (i = 0; i < __AR9170_NUM_TXQ; i++) {
  1666. skb_queue_head_init(&ar->tx_status[i]);
  1667. skb_queue_head_init(&ar->tx_pending[i]);
  1668. }
  1669. ar9170_rx_reset_rx_mpdu(ar);
  1670. INIT_WORK(&ar->beacon_work, ar9170_new_beacon);
  1671. INIT_DELAYED_WORK(&ar->tx_janitor, ar9170_tx_janitor);
  1672. /* all hw supports 2.4 GHz, so set channel to 1 by default */
  1673. ar->channel = &ar9170_2ghz_chantable[0];
  1674. /* first part of wiphy init */
  1675. ar->hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
  1676. BIT(NL80211_IFTYPE_WDS) |
  1677. BIT(NL80211_IFTYPE_ADHOC);
  1678. ar->hw->flags |= IEEE80211_HW_RX_INCLUDES_FCS |
  1679. IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING |
  1680. IEEE80211_HW_SIGNAL_DBM;
  1681. ar->hw->queues = __AR9170_NUM_TXQ;
  1682. ar->hw->extra_tx_headroom = 8;
  1683. ar->hw->max_rates = 1;
  1684. ar->hw->max_rate_tries = 3;
  1685. for (i = 0; i < ARRAY_SIZE(ar->noise); i++)
  1686. ar->noise[i] = -95; /* ATH_DEFAULT_NOISE_FLOOR */
  1687. return ar;
  1688. err_nomem:
  1689. kfree_skb(skb);
  1690. return ERR_PTR(-ENOMEM);
  1691. }
  1692. static int ar9170_read_eeprom(struct ar9170 *ar)
  1693. {
  1694. #define RW 8 /* number of words to read at once */
  1695. #define RB (sizeof(u32) * RW)
  1696. struct ath_regulatory *regulatory = &ar->common.regulatory;
  1697. u8 *eeprom = (void *)&ar->eeprom;
  1698. u8 *addr = ar->eeprom.mac_address;
  1699. __le32 offsets[RW];
  1700. unsigned int rx_streams, tx_streams, tx_params = 0;
  1701. int i, j, err, bands = 0;
  1702. BUILD_BUG_ON(sizeof(ar->eeprom) & 3);
  1703. BUILD_BUG_ON(RB > AR9170_MAX_CMD_LEN - 4);
  1704. #ifndef __CHECKER__
  1705. /* don't want to handle trailing remains */
  1706. BUILD_BUG_ON(sizeof(ar->eeprom) % RB);
  1707. #endif
  1708. for (i = 0; i < sizeof(ar->eeprom)/RB; i++) {
  1709. for (j = 0; j < RW; j++)
  1710. offsets[j] = cpu_to_le32(AR9170_EEPROM_START +
  1711. RB * i + 4 * j);
  1712. err = ar->exec_cmd(ar, AR9170_CMD_RREG,
  1713. RB, (u8 *) &offsets,
  1714. RB, eeprom + RB * i);
  1715. if (err)
  1716. return err;
  1717. }
  1718. #undef RW
  1719. #undef RB
  1720. if (ar->eeprom.length == cpu_to_le16(0xFFFF))
  1721. return -ENODATA;
  1722. if (ar->eeprom.operating_flags & AR9170_OPFLAG_2GHZ) {
  1723. ar->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &ar9170_band_2GHz;
  1724. bands++;
  1725. }
  1726. if (ar->eeprom.operating_flags & AR9170_OPFLAG_5GHZ) {
  1727. ar->hw->wiphy->bands[IEEE80211_BAND_5GHZ] = &ar9170_band_5GHz;
  1728. bands++;
  1729. }
  1730. rx_streams = hweight8(ar->eeprom.rx_mask);
  1731. tx_streams = hweight8(ar->eeprom.tx_mask);
  1732. if (rx_streams != tx_streams)
  1733. tx_params = IEEE80211_HT_MCS_TX_RX_DIFF;
  1734. if (tx_streams >= 1 && tx_streams <= IEEE80211_HT_MCS_TX_MAX_STREAMS)
  1735. tx_params = (tx_streams - 1) <<
  1736. IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT;
  1737. ar9170_band_2GHz.ht_cap.mcs.tx_params |= tx_params;
  1738. ar9170_band_5GHz.ht_cap.mcs.tx_params |= tx_params;
  1739. /*
  1740. * I measured this, a bandswitch takes roughly
  1741. * 135 ms and a frequency switch about 80.
  1742. *
  1743. * FIXME: measure these values again once EEPROM settings
  1744. * are used, that will influence them!
  1745. */
  1746. if (bands == 2)
  1747. ar->hw->channel_change_time = 135 * 1000;
  1748. else
  1749. ar->hw->channel_change_time = 80 * 1000;
  1750. regulatory->current_rd = le16_to_cpu(ar->eeprom.reg_domain[0]);
  1751. regulatory->current_rd_ext = le16_to_cpu(ar->eeprom.reg_domain[1]);
  1752. /* second part of wiphy init */
  1753. SET_IEEE80211_PERM_ADDR(ar->hw, addr);
  1754. return bands ? 0 : -EINVAL;
  1755. }
  1756. static int ar9170_reg_notifier(struct wiphy *wiphy,
  1757. struct regulatory_request *request)
  1758. {
  1759. struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
  1760. struct ar9170 *ar = hw->priv;
  1761. return ath_reg_notifier_apply(wiphy, request, &ar->common.regulatory);
  1762. }
  1763. int ar9170_register(struct ar9170 *ar, struct device *pdev)
  1764. {
  1765. struct ath_regulatory *regulatory = &ar->common.regulatory;
  1766. int err;
  1767. /* try to read EEPROM, init MAC addr */
  1768. err = ar9170_read_eeprom(ar);
  1769. if (err)
  1770. goto err_out;
  1771. err = ath_regd_init(regulatory, ar->hw->wiphy,
  1772. ar9170_reg_notifier);
  1773. if (err)
  1774. goto err_out;
  1775. err = ieee80211_register_hw(ar->hw);
  1776. if (err)
  1777. goto err_out;
  1778. if (!ath_is_world_regd(regulatory))
  1779. regulatory_hint(ar->hw->wiphy, regulatory->alpha2);
  1780. err = ar9170_init_leds(ar);
  1781. if (err)
  1782. goto err_unreg;
  1783. #ifdef CONFIG_AR9170_LEDS
  1784. err = ar9170_register_leds(ar);
  1785. if (err)
  1786. goto err_unreg;
  1787. #endif /* CONFIG_AR9170_LEDS */
  1788. dev_info(pdev, "Atheros AR9170 is registered as '%s'\n",
  1789. wiphy_name(ar->hw->wiphy));
  1790. ar->registered = true;
  1791. return 0;
  1792. err_unreg:
  1793. ieee80211_unregister_hw(ar->hw);
  1794. err_out:
  1795. return err;
  1796. }
  1797. void ar9170_unregister(struct ar9170 *ar)
  1798. {
  1799. if (ar->registered) {
  1800. #ifdef CONFIG_AR9170_LEDS
  1801. ar9170_unregister_leds(ar);
  1802. #endif /* CONFIG_AR9170_LEDS */
  1803. ieee80211_unregister_hw(ar->hw);
  1804. }
  1805. kfree_skb(ar->rx_failover);
  1806. mutex_destroy(&ar->mutex);
  1807. }