main.c 48 KB

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