main.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671
  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. static struct ieee80211_supported_band ar9170_band_2GHz = {
  137. .channels = ar9170_2ghz_chantable,
  138. .n_channels = ARRAY_SIZE(ar9170_2ghz_chantable),
  139. .bitrates = ar9170_g_ratetable,
  140. .n_bitrates = ar9170_g_ratetable_size,
  141. };
  142. #ifdef AR9170_QUEUE_DEBUG
  143. /*
  144. * In case some wants works with AR9170's crazy tx_status queueing techniques.
  145. * He might need this rather useful probing function.
  146. *
  147. * NOTE: caller must hold the queue's spinlock!
  148. */
  149. static void ar9170_print_txheader(struct ar9170 *ar, struct sk_buff *skb)
  150. {
  151. struct ar9170_tx_control *txc = (void *) skb->data;
  152. struct ieee80211_hdr *hdr = (void *)txc->frame_data;
  153. printk(KERN_DEBUG "%s: => FRAME [skb:%p, queue:%d, DA:[%pM] "
  154. "mac_control:%04x, phy_control:%08x]\n",
  155. wiphy_name(ar->hw->wiphy), skb, skb_get_queue_mapping(skb),
  156. ieee80211_get_DA(hdr), le16_to_cpu(txc->mac_control),
  157. le32_to_cpu(txc->phy_control));
  158. }
  159. static void ar9170_dump_station_tx_status_queue(struct ar9170 *ar,
  160. struct sk_buff_head *queue)
  161. {
  162. struct sk_buff *skb;
  163. int i = 0;
  164. printk(KERN_DEBUG "---[ cut here ]---\n");
  165. printk(KERN_DEBUG "%s: %d entries in tx_status queue.\n",
  166. wiphy_name(ar->hw->wiphy), skb_queue_len(queue));
  167. skb_queue_walk(queue, skb) {
  168. struct ar9170_tx_control *txc = (void *) skb->data;
  169. struct ieee80211_hdr *hdr = (void *)txc->frame_data;
  170. printk(KERN_DEBUG "index:%d => \n", i);
  171. ar9170_print_txheader(ar, skb);
  172. }
  173. printk(KERN_DEBUG "---[ end ]---\n");
  174. }
  175. #endif /* AR9170_QUEUE_DEBUG */
  176. static struct ieee80211_supported_band ar9170_band_5GHz = {
  177. .channels = ar9170_5ghz_chantable,
  178. .n_channels = ARRAY_SIZE(ar9170_5ghz_chantable),
  179. .bitrates = ar9170_a_ratetable,
  180. .n_bitrates = ar9170_a_ratetable_size,
  181. };
  182. void ar9170_handle_tx_status(struct ar9170 *ar, struct sk_buff *skb,
  183. bool valid_status, u16 tx_status)
  184. {
  185. struct ieee80211_tx_info *txinfo;
  186. unsigned int retries = 0, queue = skb_get_queue_mapping(skb);
  187. unsigned long flags;
  188. spin_lock_irqsave(&ar->tx_stats_lock, flags);
  189. ar->tx_stats[queue].len--;
  190. if (ieee80211_queue_stopped(ar->hw, queue))
  191. ieee80211_wake_queue(ar->hw, queue);
  192. spin_unlock_irqrestore(&ar->tx_stats_lock, flags);
  193. txinfo = IEEE80211_SKB_CB(skb);
  194. ieee80211_tx_info_clear_status(txinfo);
  195. switch (tx_status) {
  196. case AR9170_TX_STATUS_RETRY:
  197. retries = 2;
  198. case AR9170_TX_STATUS_COMPLETE:
  199. txinfo->flags |= IEEE80211_TX_STAT_ACK;
  200. break;
  201. case AR9170_TX_STATUS_FAILED:
  202. retries = ar->hw->conf.long_frame_max_tx_count;
  203. break;
  204. default:
  205. printk(KERN_ERR "%s: invalid tx_status response (%x).\n",
  206. wiphy_name(ar->hw->wiphy), tx_status);
  207. break;
  208. }
  209. if (valid_status)
  210. txinfo->status.rates[0].count = retries + 1;
  211. skb_pull(skb, sizeof(struct ar9170_tx_control));
  212. ieee80211_tx_status_irqsafe(ar->hw, skb);
  213. }
  214. static struct sk_buff *ar9170_find_skb_in_queue(struct ar9170 *ar,
  215. const u8 *mac,
  216. const u32 queue,
  217. struct sk_buff_head *q)
  218. {
  219. unsigned long flags;
  220. struct sk_buff *skb;
  221. spin_lock_irqsave(&q->lock, flags);
  222. skb_queue_walk(q, skb) {
  223. struct ar9170_tx_control *txc = (void *) skb->data;
  224. struct ieee80211_hdr *hdr = (void *) txc->frame_data;
  225. u32 txc_queue = (le32_to_cpu(txc->phy_control) &
  226. AR9170_TX_PHY_QOS_MASK) >>
  227. AR9170_TX_PHY_QOS_SHIFT;
  228. if ((queue != txc_queue) ||
  229. (compare_ether_addr(ieee80211_get_DA(hdr), mac)))
  230. continue;
  231. __skb_unlink(skb, q);
  232. spin_unlock_irqrestore(&q->lock, flags);
  233. return skb;
  234. }
  235. spin_unlock_irqrestore(&q->lock, flags);
  236. return NULL;
  237. }
  238. static struct sk_buff *ar9170_find_queued_skb(struct ar9170 *ar, const u8 *mac,
  239. const u32 queue)
  240. {
  241. struct ieee80211_sta *sta;
  242. struct sk_buff *skb;
  243. /*
  244. * Unfortunately, the firmware does not tell to which (queued) frame
  245. * this transmission status report belongs to.
  246. *
  247. * So we have to make risky guesses - with the scarce information
  248. * the firmware provided (-> destination MAC, and phy_control) -
  249. * and hope that we picked the right one...
  250. */
  251. rcu_read_lock();
  252. sta = ieee80211_find_sta(ar->hw, mac);
  253. if (likely(sta)) {
  254. struct ar9170_sta_info *sta_priv = (void *) sta->drv_priv;
  255. skb = skb_dequeue(&sta_priv->tx_status[queue]);
  256. rcu_read_unlock();
  257. if (likely(skb))
  258. return skb;
  259. } else
  260. rcu_read_unlock();
  261. /* scan the waste queue for candidates */
  262. skb = ar9170_find_skb_in_queue(ar, mac, queue,
  263. &ar->global_tx_status_waste);
  264. if (!skb) {
  265. /* so it still _must_ be in the global list. */
  266. skb = ar9170_find_skb_in_queue(ar, mac, queue,
  267. &ar->global_tx_status);
  268. }
  269. #ifdef AR9170_QUEUE_DEBUG
  270. if (unlikely((!skb) && net_ratelimit())) {
  271. printk(KERN_ERR "%s: ESS:[%pM] does not have any "
  272. "outstanding frames in this queue (%d).\n",
  273. wiphy_name(ar->hw->wiphy), mac, queue);
  274. }
  275. #endif /* AR9170_QUEUE_DEBUG */
  276. return skb;
  277. }
  278. /*
  279. * This worker tries to keep the global tx_status queue empty.
  280. * So we can guarantee that incoming tx_status reports for
  281. * unregistered stations are always synced with the actual
  282. * frame - which we think - belongs to.
  283. */
  284. static void ar9170_tx_status_janitor(struct work_struct *work)
  285. {
  286. struct ar9170 *ar = container_of(work, struct ar9170,
  287. tx_status_janitor.work);
  288. struct sk_buff *skb;
  289. if (unlikely(!IS_STARTED(ar)))
  290. return ;
  291. mutex_lock(&ar->mutex);
  292. /* recycle the garbage back to mac80211... one by one. */
  293. while ((skb = skb_dequeue(&ar->global_tx_status_waste))) {
  294. #ifdef AR9170_QUEUE_DEBUG
  295. printk(KERN_DEBUG "%s: dispose queued frame =>\n",
  296. wiphy_name(ar->hw->wiphy));
  297. ar9170_print_txheader(ar, skb);
  298. #endif /* AR9170_QUEUE_DEBUG */
  299. ar9170_handle_tx_status(ar, skb, false,
  300. AR9170_TX_STATUS_FAILED);
  301. }
  302. while ((skb = skb_dequeue(&ar->global_tx_status))) {
  303. #ifdef AR9170_QUEUE_DEBUG
  304. printk(KERN_DEBUG "%s: moving frame into waste queue =>\n",
  305. wiphy_name(ar->hw->wiphy));
  306. ar9170_print_txheader(ar, skb);
  307. #endif /* AR9170_QUEUE_DEBUG */
  308. skb_queue_tail(&ar->global_tx_status_waste, skb);
  309. }
  310. /* recall the janitor in 100ms - if there's garbage in the can. */
  311. if (skb_queue_len(&ar->global_tx_status_waste) > 0)
  312. queue_delayed_work(ar->hw->workqueue, &ar->tx_status_janitor,
  313. msecs_to_jiffies(100));
  314. mutex_unlock(&ar->mutex);
  315. }
  316. static void ar9170_handle_command_response(struct ar9170 *ar,
  317. void *buf, u32 len)
  318. {
  319. struct ar9170_cmd_response *cmd = (void *) buf;
  320. if ((cmd->type & 0xc0) != 0xc0) {
  321. ar->callback_cmd(ar, len, buf);
  322. return;
  323. }
  324. /* hardware event handlers */
  325. switch (cmd->type) {
  326. case 0xc1: {
  327. /*
  328. * TX status notification:
  329. * bytes: 0c c1 XX YY M1 M2 M3 M4 M5 M6 R4 R3 R2 R1 S2 S1
  330. *
  331. * XX always 81
  332. * YY always 00
  333. * M1-M6 is the MAC address
  334. * R1-R4 is the transmit rate
  335. * S1-S2 is the transmit status
  336. */
  337. struct sk_buff *skb;
  338. u32 queue = (le32_to_cpu(cmd->tx_status.rate) &
  339. AR9170_TX_PHY_QOS_MASK) >> AR9170_TX_PHY_QOS_SHIFT;
  340. skb = ar9170_find_queued_skb(ar, cmd->tx_status.dst, queue);
  341. if (unlikely(!skb))
  342. return ;
  343. ar9170_handle_tx_status(ar, skb, true,
  344. le16_to_cpu(cmd->tx_status.status));
  345. break;
  346. }
  347. case 0xc0:
  348. /*
  349. * pre-TBTT event
  350. */
  351. if (ar->vif && ar->vif->type == NL80211_IFTYPE_AP)
  352. queue_work(ar->hw->workqueue, &ar->beacon_work);
  353. break;
  354. case 0xc2:
  355. /*
  356. * (IBSS) beacon send notification
  357. * bytes: 04 c2 XX YY B4 B3 B2 B1
  358. *
  359. * XX always 80
  360. * YY always 00
  361. * B1-B4 "should" be the number of send out beacons.
  362. */
  363. break;
  364. case 0xc3:
  365. /* End of Atim Window */
  366. break;
  367. case 0xc4:
  368. case 0xc5:
  369. /* BlockACK events */
  370. break;
  371. case 0xc6:
  372. /* Watchdog Interrupt */
  373. break;
  374. case 0xc9:
  375. /* retransmission issue / SIFS/EIFS collision ?! */
  376. break;
  377. default:
  378. printk(KERN_INFO "received unhandled event %x\n", cmd->type);
  379. print_hex_dump_bytes("dump:", DUMP_PREFIX_NONE, buf, len);
  380. break;
  381. }
  382. }
  383. /*
  384. * If the frame alignment is right (or the kernel has
  385. * CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS), and there
  386. * is only a single MPDU in the USB frame, then we can
  387. * submit to mac80211 the SKB directly. However, since
  388. * there may be multiple packets in one SKB in stream
  389. * mode, and we need to observe the proper ordering,
  390. * this is non-trivial.
  391. */
  392. static void ar9170_handle_mpdu(struct ar9170 *ar, u8 *buf, int len)
  393. {
  394. struct sk_buff *skb;
  395. struct ar9170_rx_head *head = (void *)buf;
  396. struct ar9170_rx_tail *tail;
  397. struct ieee80211_rx_status status;
  398. int mpdu_len, i;
  399. u8 error, antennas = 0, decrypt;
  400. __le16 fc;
  401. int reserved;
  402. if (unlikely(!IS_STARTED(ar)))
  403. return ;
  404. /* Received MPDU */
  405. mpdu_len = len;
  406. mpdu_len -= sizeof(struct ar9170_rx_head);
  407. mpdu_len -= sizeof(struct ar9170_rx_tail);
  408. BUILD_BUG_ON(sizeof(struct ar9170_rx_head) != 12);
  409. BUILD_BUG_ON(sizeof(struct ar9170_rx_tail) != 24);
  410. if (mpdu_len <= FCS_LEN)
  411. return;
  412. tail = (void *)(buf + sizeof(struct ar9170_rx_head) + mpdu_len);
  413. for (i = 0; i < 3; i++)
  414. if (tail->rssi[i] != 0x80)
  415. antennas |= BIT(i);
  416. /* post-process RSSI */
  417. for (i = 0; i < 7; i++)
  418. if (tail->rssi[i] & 0x80)
  419. tail->rssi[i] = ((tail->rssi[i] & 0x7f) + 1) & 0x7f;
  420. memset(&status, 0, sizeof(status));
  421. status.band = ar->channel->band;
  422. status.freq = ar->channel->center_freq;
  423. status.signal = ar->noise[0] + tail->rssi_combined;
  424. status.noise = ar->noise[0];
  425. status.antenna = antennas;
  426. switch (tail->status & AR9170_RX_STATUS_MODULATION_MASK) {
  427. case AR9170_RX_STATUS_MODULATION_CCK:
  428. if (tail->status & AR9170_RX_STATUS_SHORT_PREAMBLE)
  429. status.flag |= RX_FLAG_SHORTPRE;
  430. switch (head->plcp[0]) {
  431. case 0x0a:
  432. status.rate_idx = 0;
  433. break;
  434. case 0x14:
  435. status.rate_idx = 1;
  436. break;
  437. case 0x37:
  438. status.rate_idx = 2;
  439. break;
  440. case 0x6e:
  441. status.rate_idx = 3;
  442. break;
  443. default:
  444. if ((!ar->sniffer_enabled) && (net_ratelimit()))
  445. printk(KERN_ERR "%s: invalid plcp cck rate "
  446. "(%x).\n", wiphy_name(ar->hw->wiphy),
  447. head->plcp[0]);
  448. return;
  449. }
  450. break;
  451. case AR9170_RX_STATUS_MODULATION_OFDM:
  452. switch (head->plcp[0] & 0xF) {
  453. case 0xB:
  454. status.rate_idx = 0;
  455. break;
  456. case 0xF:
  457. status.rate_idx = 1;
  458. break;
  459. case 0xA:
  460. status.rate_idx = 2;
  461. break;
  462. case 0xE:
  463. status.rate_idx = 3;
  464. break;
  465. case 0x9:
  466. status.rate_idx = 4;
  467. break;
  468. case 0xD:
  469. status.rate_idx = 5;
  470. break;
  471. case 0x8:
  472. status.rate_idx = 6;
  473. break;
  474. case 0xC:
  475. status.rate_idx = 7;
  476. break;
  477. default:
  478. if ((!ar->sniffer_enabled) && (net_ratelimit()))
  479. printk(KERN_ERR "%s: invalid plcp ofdm rate "
  480. "(%x).\n", wiphy_name(ar->hw->wiphy),
  481. head->plcp[0]);
  482. return;
  483. }
  484. if (status.band == IEEE80211_BAND_2GHZ)
  485. status.rate_idx += 4;
  486. break;
  487. case AR9170_RX_STATUS_MODULATION_HT:
  488. case AR9170_RX_STATUS_MODULATION_DUPOFDM:
  489. /* XXX */
  490. if (net_ratelimit())
  491. printk(KERN_ERR "%s: invalid modulation\n",
  492. wiphy_name(ar->hw->wiphy));
  493. return;
  494. }
  495. error = tail->error;
  496. if (error & AR9170_RX_ERROR_MMIC) {
  497. status.flag |= RX_FLAG_MMIC_ERROR;
  498. error &= ~AR9170_RX_ERROR_MMIC;
  499. }
  500. if (error & AR9170_RX_ERROR_PLCP) {
  501. status.flag |= RX_FLAG_FAILED_PLCP_CRC;
  502. error &= ~AR9170_RX_ERROR_PLCP;
  503. }
  504. if (error & AR9170_RX_ERROR_FCS) {
  505. status.flag |= RX_FLAG_FAILED_FCS_CRC;
  506. error &= ~AR9170_RX_ERROR_FCS;
  507. }
  508. decrypt = ar9170_get_decrypt_type(tail);
  509. if (!(decrypt & AR9170_RX_ENC_SOFTWARE) &&
  510. decrypt != AR9170_ENC_ALG_NONE)
  511. status.flag |= RX_FLAG_DECRYPTED;
  512. /* ignore wrong RA errors */
  513. error &= ~AR9170_RX_ERROR_WRONG_RA;
  514. if (error & AR9170_RX_ERROR_DECRYPT) {
  515. error &= ~AR9170_RX_ERROR_DECRYPT;
  516. /*
  517. * Rx decryption is done in place,
  518. * the original data is lost anyway.
  519. */
  520. return ;
  521. }
  522. /* drop any other error frames */
  523. if ((error) && (net_ratelimit())) {
  524. printk(KERN_DEBUG "%s: errors: %#x\n",
  525. wiphy_name(ar->hw->wiphy), error);
  526. return;
  527. }
  528. buf += sizeof(struct ar9170_rx_head);
  529. fc = *(__le16 *)buf;
  530. if (ieee80211_is_data_qos(fc) ^ ieee80211_has_a4(fc))
  531. reserved = 32 + 2;
  532. else
  533. reserved = 32;
  534. skb = dev_alloc_skb(mpdu_len + reserved);
  535. if (!skb)
  536. return;
  537. skb_reserve(skb, reserved);
  538. memcpy(skb_put(skb, mpdu_len), buf, mpdu_len);
  539. ieee80211_rx_irqsafe(ar->hw, skb, &status);
  540. }
  541. void ar9170_rx(struct ar9170 *ar, struct sk_buff *skb)
  542. {
  543. unsigned int i, tlen, resplen;
  544. u8 *tbuf, *respbuf;
  545. tbuf = skb->data;
  546. tlen = skb->len;
  547. while (tlen >= 4) {
  548. int clen = tbuf[1] << 8 | tbuf[0];
  549. int wlen = (clen + 3) & ~3;
  550. /*
  551. * parse stream (if any)
  552. */
  553. if (tbuf[2] != 0 || tbuf[3] != 0x4e) {
  554. printk(KERN_ERR "%s: missing tag!\n",
  555. wiphy_name(ar->hw->wiphy));
  556. return ;
  557. }
  558. if (wlen > tlen - 4) {
  559. printk(KERN_ERR "%s: invalid RX (%d, %d, %d)\n",
  560. wiphy_name(ar->hw->wiphy), clen, wlen, tlen);
  561. print_hex_dump(KERN_DEBUG, "data: ",
  562. DUMP_PREFIX_OFFSET,
  563. 16, 1, tbuf, tlen, true);
  564. return ;
  565. }
  566. resplen = clen;
  567. respbuf = tbuf + 4;
  568. tbuf += wlen + 4;
  569. tlen -= wlen + 4;
  570. i = 0;
  571. /* weird thing, but this is the same in the original driver */
  572. while (resplen > 2 && i < 12 &&
  573. respbuf[0] == 0xff && respbuf[1] == 0xff) {
  574. i += 2;
  575. resplen -= 2;
  576. respbuf += 2;
  577. }
  578. if (resplen < 4)
  579. continue;
  580. /* found the 6 * 0xffff marker? */
  581. if (i == 12)
  582. ar9170_handle_command_response(ar, respbuf, resplen);
  583. else
  584. ar9170_handle_mpdu(ar, respbuf, resplen);
  585. }
  586. if (tlen)
  587. printk(KERN_ERR "%s: buffer remains!\n",
  588. wiphy_name(ar->hw->wiphy));
  589. }
  590. #define AR9170_FILL_QUEUE(queue, ai_fs, cwmin, cwmax, _txop) \
  591. do { \
  592. queue.aifs = ai_fs; \
  593. queue.cw_min = cwmin; \
  594. queue.cw_max = cwmax; \
  595. queue.txop = _txop; \
  596. } while (0)
  597. static int ar9170_op_start(struct ieee80211_hw *hw)
  598. {
  599. struct ar9170 *ar = hw->priv;
  600. int err, i;
  601. mutex_lock(&ar->mutex);
  602. /* reinitialize queues statistics */
  603. memset(&ar->tx_stats, 0, sizeof(ar->tx_stats));
  604. for (i = 0; i < ARRAY_SIZE(ar->tx_stats); i++)
  605. ar->tx_stats[i].limit = 8;
  606. /* reset QoS defaults */
  607. AR9170_FILL_QUEUE(ar->edcf[0], 3, 15, 1023, 0); /* BEST EFFORT*/
  608. AR9170_FILL_QUEUE(ar->edcf[1], 7, 15, 1023, 0); /* BACKGROUND */
  609. AR9170_FILL_QUEUE(ar->edcf[2], 2, 7, 15, 94); /* VIDEO */
  610. AR9170_FILL_QUEUE(ar->edcf[3], 2, 3, 7, 47); /* VOICE */
  611. AR9170_FILL_QUEUE(ar->edcf[4], 2, 3, 7, 0); /* SPECIAL */
  612. err = ar->open(ar);
  613. if (err)
  614. goto out;
  615. err = ar9170_init_mac(ar);
  616. if (err)
  617. goto out;
  618. err = ar9170_set_qos(ar);
  619. if (err)
  620. goto out;
  621. err = ar9170_init_phy(ar, IEEE80211_BAND_2GHZ);
  622. if (err)
  623. goto out;
  624. err = ar9170_init_rf(ar);
  625. if (err)
  626. goto out;
  627. /* start DMA */
  628. err = ar9170_write_reg(ar, 0x1c3d30, 0x100);
  629. if (err)
  630. goto out;
  631. ar->state = AR9170_STARTED;
  632. out:
  633. mutex_unlock(&ar->mutex);
  634. return err;
  635. }
  636. static void ar9170_op_stop(struct ieee80211_hw *hw)
  637. {
  638. struct ar9170 *ar = hw->priv;
  639. if (IS_STARTED(ar))
  640. ar->state = AR9170_IDLE;
  641. mutex_lock(&ar->mutex);
  642. cancel_delayed_work_sync(&ar->tx_status_janitor);
  643. cancel_work_sync(&ar->filter_config_work);
  644. cancel_work_sync(&ar->beacon_work);
  645. skb_queue_purge(&ar->global_tx_status_waste);
  646. skb_queue_purge(&ar->global_tx_status);
  647. if (IS_ACCEPTING_CMD(ar)) {
  648. ar9170_set_leds_state(ar, 0);
  649. /* stop DMA */
  650. ar9170_write_reg(ar, 0x1c3d30, 0);
  651. ar->stop(ar);
  652. }
  653. mutex_unlock(&ar->mutex);
  654. }
  655. int ar9170_op_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
  656. {
  657. struct ar9170 *ar = hw->priv;
  658. struct ieee80211_hdr *hdr;
  659. struct ar9170_tx_control *txc;
  660. struct ieee80211_tx_info *info;
  661. struct ieee80211_rate *rate = NULL;
  662. struct ieee80211_tx_rate *txrate;
  663. unsigned int queue = skb_get_queue_mapping(skb);
  664. unsigned long flags = 0;
  665. struct ar9170_sta_info *sta_info = NULL;
  666. u32 power, chains;
  667. u16 keytype = 0;
  668. u16 len, icv = 0;
  669. int err;
  670. bool tx_status;
  671. if (unlikely(!IS_STARTED(ar)))
  672. goto err_free;
  673. hdr = (void *)skb->data;
  674. info = IEEE80211_SKB_CB(skb);
  675. len = skb->len;
  676. spin_lock_irqsave(&ar->tx_stats_lock, flags);
  677. if (ar->tx_stats[queue].limit < ar->tx_stats[queue].len) {
  678. spin_unlock_irqrestore(&ar->tx_stats_lock, flags);
  679. return NETDEV_TX_OK;
  680. }
  681. ar->tx_stats[queue].len++;
  682. ar->tx_stats[queue].count++;
  683. if (ar->tx_stats[queue].limit == ar->tx_stats[queue].len)
  684. ieee80211_stop_queue(hw, queue);
  685. spin_unlock_irqrestore(&ar->tx_stats_lock, flags);
  686. txc = (void *)skb_push(skb, sizeof(*txc));
  687. tx_status = (((info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE) != 0) ||
  688. ((info->flags & IEEE80211_TX_CTL_REQ_TX_STATUS) != 0));
  689. if (info->control.hw_key) {
  690. icv = info->control.hw_key->icv_len;
  691. switch (info->control.hw_key->alg) {
  692. case ALG_WEP:
  693. keytype = AR9170_TX_MAC_ENCR_RC4;
  694. break;
  695. case ALG_TKIP:
  696. keytype = AR9170_TX_MAC_ENCR_RC4;
  697. break;
  698. case ALG_CCMP:
  699. keytype = AR9170_TX_MAC_ENCR_AES;
  700. break;
  701. default:
  702. WARN_ON(1);
  703. goto err_dequeue;
  704. }
  705. }
  706. /* Length */
  707. txc->length = cpu_to_le16(len + icv + 4);
  708. txc->mac_control = cpu_to_le16(AR9170_TX_MAC_HW_DURATION |
  709. AR9170_TX_MAC_BACKOFF);
  710. txc->mac_control |= cpu_to_le16(ar9170_qos_hwmap[queue] <<
  711. AR9170_TX_MAC_QOS_SHIFT);
  712. txc->mac_control |= cpu_to_le16(keytype);
  713. txc->phy_control = cpu_to_le32(0);
  714. if (info->flags & IEEE80211_TX_CTL_NO_ACK)
  715. txc->mac_control |= cpu_to_le16(AR9170_TX_MAC_NO_ACK);
  716. if (info->flags & IEEE80211_TX_CTL_AMPDU)
  717. txc->mac_control |= cpu_to_le16(AR9170_TX_MAC_AGGR);
  718. txrate = &info->control.rates[0];
  719. if (txrate->flags & IEEE80211_TX_RC_USE_CTS_PROTECT)
  720. txc->mac_control |= cpu_to_le16(AR9170_TX_MAC_PROT_CTS);
  721. else if (txrate->flags & IEEE80211_TX_RC_USE_RTS_CTS)
  722. txc->mac_control |= cpu_to_le16(AR9170_TX_MAC_PROT_RTS);
  723. if (txrate->flags & IEEE80211_TX_RC_GREEN_FIELD)
  724. txc->phy_control |= cpu_to_le32(AR9170_TX_PHY_GREENFIELD);
  725. if (txrate->flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
  726. txc->phy_control |= cpu_to_le32(AR9170_TX_PHY_SHORT_PREAMBLE);
  727. if (txrate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
  728. txc->phy_control |= cpu_to_le32(AR9170_TX_PHY_BW_40MHZ);
  729. /* this works because 40 MHz is 2 and dup is 3 */
  730. if (txrate->flags & IEEE80211_TX_RC_DUP_DATA)
  731. txc->phy_control |= cpu_to_le32(AR9170_TX_PHY_BW_40MHZ_DUP);
  732. if (txrate->flags & IEEE80211_TX_RC_SHORT_GI)
  733. txc->phy_control |= cpu_to_le32(AR9170_TX_PHY_SHORT_GI);
  734. if (txrate->flags & IEEE80211_TX_RC_MCS) {
  735. u32 r = txrate->idx;
  736. u8 *txpower;
  737. r <<= AR9170_TX_PHY_MCS_SHIFT;
  738. if (WARN_ON(r & ~AR9170_TX_PHY_MCS_MASK))
  739. goto err_dequeue;
  740. txc->phy_control |= cpu_to_le32(r & AR9170_TX_PHY_MCS_MASK);
  741. txc->phy_control |= cpu_to_le32(AR9170_TX_PHY_MOD_HT);
  742. if (txrate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH) {
  743. if (info->band == IEEE80211_BAND_5GHZ)
  744. txpower = ar->power_5G_ht40;
  745. else
  746. txpower = ar->power_2G_ht40;
  747. } else {
  748. if (info->band == IEEE80211_BAND_5GHZ)
  749. txpower = ar->power_5G_ht20;
  750. else
  751. txpower = ar->power_2G_ht20;
  752. }
  753. power = txpower[(txrate->idx) & 7];
  754. } else {
  755. u8 *txpower;
  756. u32 mod;
  757. u32 phyrate;
  758. u8 idx = txrate->idx;
  759. if (info->band != IEEE80211_BAND_2GHZ) {
  760. idx += 4;
  761. txpower = ar->power_5G_leg;
  762. mod = AR9170_TX_PHY_MOD_OFDM;
  763. } else {
  764. if (idx < 4) {
  765. txpower = ar->power_2G_cck;
  766. mod = AR9170_TX_PHY_MOD_CCK;
  767. } else {
  768. mod = AR9170_TX_PHY_MOD_OFDM;
  769. txpower = ar->power_2G_ofdm;
  770. }
  771. }
  772. rate = &__ar9170_ratetable[idx];
  773. phyrate = rate->hw_value & 0xF;
  774. power = txpower[(rate->hw_value & 0x30) >> 4];
  775. phyrate <<= AR9170_TX_PHY_MCS_SHIFT;
  776. txc->phy_control |= cpu_to_le32(mod);
  777. txc->phy_control |= cpu_to_le32(phyrate);
  778. }
  779. power <<= AR9170_TX_PHY_TX_PWR_SHIFT;
  780. power &= AR9170_TX_PHY_TX_PWR_MASK;
  781. txc->phy_control |= cpu_to_le32(power);
  782. /* set TX chains */
  783. if (ar->eeprom.tx_mask == 1) {
  784. chains = AR9170_TX_PHY_TXCHAIN_1;
  785. } else {
  786. chains = AR9170_TX_PHY_TXCHAIN_2;
  787. /* >= 36M legacy OFDM - use only one chain */
  788. if (rate && rate->bitrate >= 360)
  789. chains = AR9170_TX_PHY_TXCHAIN_1;
  790. }
  791. txc->phy_control |= cpu_to_le32(chains << AR9170_TX_PHY_TXCHAIN_SHIFT);
  792. if (tx_status) {
  793. txc->mac_control |= cpu_to_le16(AR9170_TX_MAC_RATE_PROBE);
  794. /*
  795. * WARNING:
  796. * Putting the QoS queue bits into an unexplored territory is
  797. * certainly not elegant.
  798. *
  799. * In my defense: This idea provides a reasonable way to
  800. * smuggle valuable information to the tx_status callback.
  801. * Also, the idea behind this bit-abuse came straight from
  802. * the original driver code.
  803. */
  804. txc->phy_control |=
  805. cpu_to_le32(queue << AR9170_TX_PHY_QOS_SHIFT);
  806. if (info->control.sta) {
  807. sta_info = (void *) info->control.sta->drv_priv;
  808. skb_queue_tail(&sta_info->tx_status[queue], skb);
  809. } else {
  810. skb_queue_tail(&ar->global_tx_status, skb);
  811. queue_delayed_work(ar->hw->workqueue,
  812. &ar->tx_status_janitor,
  813. msecs_to_jiffies(100));
  814. }
  815. }
  816. err = ar->tx(ar, skb, tx_status, 0);
  817. if (unlikely(tx_status && err)) {
  818. if (info->control.sta)
  819. skb_unlink(skb, &sta_info->tx_status[queue]);
  820. else
  821. skb_unlink(skb, &ar->global_tx_status);
  822. }
  823. return NETDEV_TX_OK;
  824. err_dequeue:
  825. spin_lock_irqsave(&ar->tx_stats_lock, flags);
  826. ar->tx_stats[queue].len--;
  827. ar->tx_stats[queue].count--;
  828. spin_unlock_irqrestore(&ar->tx_stats_lock, flags);
  829. err_free:
  830. dev_kfree_skb(skb);
  831. return NETDEV_TX_OK;
  832. }
  833. static int ar9170_op_add_interface(struct ieee80211_hw *hw,
  834. struct ieee80211_if_init_conf *conf)
  835. {
  836. struct ar9170 *ar = hw->priv;
  837. int err = 0;
  838. mutex_lock(&ar->mutex);
  839. if (ar->vif) {
  840. err = -EBUSY;
  841. goto unlock;
  842. }
  843. ar->vif = conf->vif;
  844. memcpy(ar->mac_addr, conf->mac_addr, ETH_ALEN);
  845. if (modparam_nohwcrypt || (ar->vif->type != NL80211_IFTYPE_STATION)) {
  846. ar->rx_software_decryption = true;
  847. ar->disable_offload = true;
  848. }
  849. ar->cur_filter = 0;
  850. ar->want_filter = AR9170_MAC_REG_FTF_DEFAULTS;
  851. err = ar9170_update_frame_filter(ar);
  852. if (err)
  853. goto unlock;
  854. err = ar9170_set_operating_mode(ar);
  855. unlock:
  856. mutex_unlock(&ar->mutex);
  857. return err;
  858. }
  859. static void ar9170_op_remove_interface(struct ieee80211_hw *hw,
  860. struct ieee80211_if_init_conf *conf)
  861. {
  862. struct ar9170 *ar = hw->priv;
  863. mutex_lock(&ar->mutex);
  864. ar->vif = NULL;
  865. ar->want_filter = 0;
  866. ar9170_update_frame_filter(ar);
  867. ar9170_set_beacon_timers(ar);
  868. dev_kfree_skb(ar->beacon);
  869. ar->beacon = NULL;
  870. ar->sniffer_enabled = false;
  871. ar->rx_software_decryption = false;
  872. ar9170_set_operating_mode(ar);
  873. mutex_unlock(&ar->mutex);
  874. }
  875. static int ar9170_op_config(struct ieee80211_hw *hw, u32 changed)
  876. {
  877. struct ar9170 *ar = hw->priv;
  878. int err = 0;
  879. mutex_lock(&ar->mutex);
  880. if (changed & IEEE80211_CONF_CHANGE_RADIO_ENABLED) {
  881. /* TODO */
  882. err = 0;
  883. }
  884. if (changed & IEEE80211_CONF_CHANGE_LISTEN_INTERVAL) {
  885. /* TODO */
  886. err = 0;
  887. }
  888. if (changed & IEEE80211_CONF_CHANGE_PS) {
  889. /* TODO */
  890. err = 0;
  891. }
  892. if (changed & IEEE80211_CONF_CHANGE_POWER) {
  893. /* TODO */
  894. err = 0;
  895. }
  896. if (changed & IEEE80211_CONF_CHANGE_RETRY_LIMITS) {
  897. /*
  898. * is it long_frame_max_tx_count or short_frame_max_tx_count?
  899. */
  900. err = ar9170_set_hwretry_limit(ar,
  901. ar->hw->conf.long_frame_max_tx_count);
  902. if (err)
  903. goto out;
  904. }
  905. if (changed & IEEE80211_CONF_CHANGE_BEACON_INTERVAL) {
  906. err = ar9170_set_beacon_timers(ar);
  907. if (err)
  908. goto out;
  909. }
  910. if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
  911. err = ar9170_set_channel(ar, hw->conf.channel,
  912. AR9170_RFI_NONE, AR9170_BW_20);
  913. if (err)
  914. goto out;
  915. /* adjust slot time for 5 GHz */
  916. if (hw->conf.channel->band == IEEE80211_BAND_5GHZ)
  917. err = ar9170_write_reg(ar, AR9170_MAC_REG_SLOT_TIME,
  918. 9 << 10);
  919. }
  920. out:
  921. mutex_unlock(&ar->mutex);
  922. return err;
  923. }
  924. static int ar9170_op_config_interface(struct ieee80211_hw *hw,
  925. struct ieee80211_vif *vif,
  926. struct ieee80211_if_conf *conf)
  927. {
  928. struct ar9170 *ar = hw->priv;
  929. int err = 0;
  930. mutex_lock(&ar->mutex);
  931. if (conf->changed & IEEE80211_IFCC_BSSID) {
  932. memcpy(ar->bssid, conf->bssid, ETH_ALEN);
  933. err = ar9170_set_operating_mode(ar);
  934. }
  935. if (conf->changed & IEEE80211_IFCC_BEACON) {
  936. err = ar9170_update_beacon(ar);
  937. if (err)
  938. goto out;
  939. err = ar9170_set_beacon_timers(ar);
  940. }
  941. out:
  942. mutex_unlock(&ar->mutex);
  943. return err;
  944. }
  945. static void ar9170_set_filters(struct work_struct *work)
  946. {
  947. struct ar9170 *ar = container_of(work, struct ar9170,
  948. filter_config_work);
  949. int err;
  950. mutex_lock(&ar->mutex);
  951. if (unlikely(!IS_STARTED(ar)))
  952. goto unlock;
  953. if (ar->filter_changed & AR9170_FILTER_CHANGED_PROMISC) {
  954. err = ar9170_set_operating_mode(ar);
  955. if (err)
  956. goto unlock;
  957. }
  958. if (ar->filter_changed & AR9170_FILTER_CHANGED_MULTICAST) {
  959. err = ar9170_update_multicast(ar);
  960. if (err)
  961. goto unlock;
  962. }
  963. if (ar->filter_changed & AR9170_FILTER_CHANGED_FRAMEFILTER)
  964. err = ar9170_update_frame_filter(ar);
  965. unlock:
  966. mutex_unlock(&ar->mutex);
  967. }
  968. static void ar9170_op_configure_filter(struct ieee80211_hw *hw,
  969. unsigned int changed_flags,
  970. unsigned int *new_flags,
  971. int mc_count, struct dev_mc_list *mclist)
  972. {
  973. struct ar9170 *ar = hw->priv;
  974. /* mask supported flags */
  975. *new_flags &= FIF_ALLMULTI | FIF_CONTROL | FIF_BCN_PRBRESP_PROMISC |
  976. FIF_PROMISC_IN_BSS;
  977. /*
  978. * We can support more by setting the sniffer bit and
  979. * then checking the error flags, later.
  980. */
  981. if (changed_flags & FIF_ALLMULTI) {
  982. if (*new_flags & FIF_ALLMULTI) {
  983. ar->want_mc_hash = ~0ULL;
  984. } else {
  985. u64 mchash;
  986. int i;
  987. /* always get broadcast frames */
  988. mchash = 1ULL << (0xff>>2);
  989. for (i = 0; i < mc_count; i++) {
  990. if (WARN_ON(!mclist))
  991. break;
  992. mchash |= 1ULL << (mclist->dmi_addr[5] >> 2);
  993. mclist = mclist->next;
  994. }
  995. ar->want_mc_hash = mchash;
  996. }
  997. ar->filter_changed |= AR9170_FILTER_CHANGED_MULTICAST;
  998. }
  999. if (changed_flags & FIF_CONTROL) {
  1000. u32 filter = AR9170_MAC_REG_FTF_PSPOLL |
  1001. AR9170_MAC_REG_FTF_RTS |
  1002. AR9170_MAC_REG_FTF_CTS |
  1003. AR9170_MAC_REG_FTF_ACK |
  1004. AR9170_MAC_REG_FTF_CFE |
  1005. AR9170_MAC_REG_FTF_CFE_ACK;
  1006. if (*new_flags & FIF_CONTROL)
  1007. ar->want_filter = ar->cur_filter | filter;
  1008. else
  1009. ar->want_filter = ar->cur_filter & ~filter;
  1010. ar->filter_changed |= AR9170_FILTER_CHANGED_FRAMEFILTER;
  1011. }
  1012. if (changed_flags & FIF_PROMISC_IN_BSS) {
  1013. ar->sniffer_enabled = ((*new_flags) & FIF_PROMISC_IN_BSS) != 0;
  1014. ar->filter_changed |= AR9170_FILTER_CHANGED_PROMISC;
  1015. }
  1016. if (likely(IS_STARTED(ar)))
  1017. queue_work(ar->hw->workqueue, &ar->filter_config_work);
  1018. }
  1019. static void ar9170_op_bss_info_changed(struct ieee80211_hw *hw,
  1020. struct ieee80211_vif *vif,
  1021. struct ieee80211_bss_conf *bss_conf,
  1022. u32 changed)
  1023. {
  1024. struct ar9170 *ar = hw->priv;
  1025. int err = 0;
  1026. mutex_lock(&ar->mutex);
  1027. ar9170_regwrite_begin(ar);
  1028. if (changed & BSS_CHANGED_ASSOC) {
  1029. ar->state = bss_conf->assoc ? AR9170_ASSOCIATED : ar->state;
  1030. #ifndef CONFIG_AR9170_LEDS
  1031. /* enable assoc LED. */
  1032. err = ar9170_set_leds_state(ar, bss_conf->assoc ? 2 : 0);
  1033. #endif /* CONFIG_AR9170_LEDS */
  1034. }
  1035. if (changed & BSS_CHANGED_HT) {
  1036. /* TODO */
  1037. err = 0;
  1038. }
  1039. if (changed & BSS_CHANGED_ERP_SLOT) {
  1040. u32 slottime = 20;
  1041. if (bss_conf->use_short_slot)
  1042. slottime = 9;
  1043. ar9170_regwrite(AR9170_MAC_REG_SLOT_TIME, slottime << 10);
  1044. }
  1045. if (changed & BSS_CHANGED_BASIC_RATES) {
  1046. u32 cck, ofdm;
  1047. if (hw->conf.channel->band == IEEE80211_BAND_5GHZ) {
  1048. ofdm = bss_conf->basic_rates;
  1049. cck = 0;
  1050. } else {
  1051. /* four cck rates */
  1052. cck = bss_conf->basic_rates & 0xf;
  1053. ofdm = bss_conf->basic_rates >> 4;
  1054. }
  1055. ar9170_regwrite(AR9170_MAC_REG_BASIC_RATE,
  1056. ofdm << 8 | cck);
  1057. }
  1058. ar9170_regwrite_finish();
  1059. err = ar9170_regwrite_result();
  1060. mutex_unlock(&ar->mutex);
  1061. }
  1062. static u64 ar9170_op_get_tsf(struct ieee80211_hw *hw)
  1063. {
  1064. struct ar9170 *ar = hw->priv;
  1065. int err;
  1066. u32 tsf_low;
  1067. u32 tsf_high;
  1068. u64 tsf;
  1069. mutex_lock(&ar->mutex);
  1070. err = ar9170_read_reg(ar, AR9170_MAC_REG_TSF_L, &tsf_low);
  1071. if (!err)
  1072. err = ar9170_read_reg(ar, AR9170_MAC_REG_TSF_H, &tsf_high);
  1073. mutex_unlock(&ar->mutex);
  1074. if (WARN_ON(err))
  1075. return 0;
  1076. tsf = tsf_high;
  1077. tsf = (tsf << 32) | tsf_low;
  1078. return tsf;
  1079. }
  1080. static int ar9170_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
  1081. struct ieee80211_vif *vif, struct ieee80211_sta *sta,
  1082. struct ieee80211_key_conf *key)
  1083. {
  1084. struct ar9170 *ar = hw->priv;
  1085. int err = 0, i;
  1086. u8 ktype;
  1087. if ((!ar->vif) || (ar->disable_offload))
  1088. return -EOPNOTSUPP;
  1089. switch (key->alg) {
  1090. case ALG_WEP:
  1091. if (key->keylen == LEN_WEP40)
  1092. ktype = AR9170_ENC_ALG_WEP64;
  1093. else
  1094. ktype = AR9170_ENC_ALG_WEP128;
  1095. break;
  1096. case ALG_TKIP:
  1097. ktype = AR9170_ENC_ALG_TKIP;
  1098. break;
  1099. case ALG_CCMP:
  1100. ktype = AR9170_ENC_ALG_AESCCMP;
  1101. break;
  1102. default:
  1103. return -EOPNOTSUPP;
  1104. }
  1105. mutex_lock(&ar->mutex);
  1106. if (cmd == SET_KEY) {
  1107. if (unlikely(!IS_STARTED(ar))) {
  1108. err = -EOPNOTSUPP;
  1109. goto out;
  1110. }
  1111. /* group keys need all-zeroes address */
  1112. if (!(key->flags & IEEE80211_KEY_FLAG_PAIRWISE))
  1113. sta = NULL;
  1114. if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
  1115. for (i = 0; i < 64; i++)
  1116. if (!(ar->usedkeys & BIT(i)))
  1117. break;
  1118. if (i == 64) {
  1119. ar->rx_software_decryption = true;
  1120. ar9170_set_operating_mode(ar);
  1121. err = -ENOSPC;
  1122. goto out;
  1123. }
  1124. } else {
  1125. i = 64 + key->keyidx;
  1126. }
  1127. key->hw_key_idx = i;
  1128. err = ar9170_upload_key(ar, i, sta ? sta->addr : NULL, ktype, 0,
  1129. key->key, min_t(u8, 16, key->keylen));
  1130. if (err)
  1131. goto out;
  1132. if (key->alg == ALG_TKIP) {
  1133. err = ar9170_upload_key(ar, i, sta ? sta->addr : NULL,
  1134. ktype, 1, key->key + 16, 16);
  1135. if (err)
  1136. goto out;
  1137. /*
  1138. * hardware is not capable generating the MMIC
  1139. * for fragmented frames!
  1140. */
  1141. key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
  1142. }
  1143. if (i < 64)
  1144. ar->usedkeys |= BIT(i);
  1145. key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  1146. } else {
  1147. if (unlikely(!IS_STARTED(ar))) {
  1148. /* The device is gone... together with the key ;-) */
  1149. err = 0;
  1150. goto out;
  1151. }
  1152. err = ar9170_disable_key(ar, key->hw_key_idx);
  1153. if (err)
  1154. goto out;
  1155. if (key->hw_key_idx < 64) {
  1156. ar->usedkeys &= ~BIT(key->hw_key_idx);
  1157. } else {
  1158. err = ar9170_upload_key(ar, key->hw_key_idx, NULL,
  1159. AR9170_ENC_ALG_NONE, 0,
  1160. NULL, 0);
  1161. if (err)
  1162. goto out;
  1163. if (key->alg == ALG_TKIP) {
  1164. err = ar9170_upload_key(ar, key->hw_key_idx,
  1165. NULL,
  1166. AR9170_ENC_ALG_NONE, 1,
  1167. NULL, 0);
  1168. if (err)
  1169. goto out;
  1170. }
  1171. }
  1172. }
  1173. ar9170_regwrite_begin(ar);
  1174. ar9170_regwrite(AR9170_MAC_REG_ROLL_CALL_TBL_L, ar->usedkeys);
  1175. ar9170_regwrite(AR9170_MAC_REG_ROLL_CALL_TBL_H, ar->usedkeys >> 32);
  1176. ar9170_regwrite_finish();
  1177. err = ar9170_regwrite_result();
  1178. out:
  1179. mutex_unlock(&ar->mutex);
  1180. return err;
  1181. }
  1182. static void ar9170_sta_notify(struct ieee80211_hw *hw,
  1183. struct ieee80211_vif *vif,
  1184. enum sta_notify_cmd cmd,
  1185. struct ieee80211_sta *sta)
  1186. {
  1187. struct ar9170 *ar = hw->priv;
  1188. struct ar9170_sta_info *info = (void *) sta->drv_priv;
  1189. struct sk_buff *skb;
  1190. unsigned int i;
  1191. switch (cmd) {
  1192. case STA_NOTIFY_ADD:
  1193. for (i = 0; i < ar->hw->queues; i++)
  1194. skb_queue_head_init(&info->tx_status[i]);
  1195. break;
  1196. case STA_NOTIFY_REMOVE:
  1197. /*
  1198. * transfer all outstanding frames that need a tx_status
  1199. * reports to the global tx_status queue
  1200. */
  1201. for (i = 0; i < ar->hw->queues; i++) {
  1202. while ((skb = skb_dequeue(&info->tx_status[i]))) {
  1203. #ifdef AR9170_QUEUE_DEBUG
  1204. printk(KERN_DEBUG "%s: queueing frame in "
  1205. "global tx_status queue =>\n",
  1206. wiphy_name(ar->hw->wiphy));
  1207. ar9170_print_txheader(ar, skb);
  1208. #endif /* AR9170_QUEUE_DEBUG */
  1209. skb_queue_tail(&ar->global_tx_status, skb);
  1210. }
  1211. }
  1212. queue_delayed_work(ar->hw->workqueue, &ar->tx_status_janitor,
  1213. msecs_to_jiffies(100));
  1214. break;
  1215. default:
  1216. break;
  1217. }
  1218. }
  1219. static int ar9170_get_stats(struct ieee80211_hw *hw,
  1220. struct ieee80211_low_level_stats *stats)
  1221. {
  1222. struct ar9170 *ar = hw->priv;
  1223. u32 val;
  1224. int err;
  1225. mutex_lock(&ar->mutex);
  1226. err = ar9170_read_reg(ar, AR9170_MAC_REG_TX_RETRY, &val);
  1227. ar->stats.dot11ACKFailureCount += val;
  1228. memcpy(stats, &ar->stats, sizeof(*stats));
  1229. mutex_unlock(&ar->mutex);
  1230. return 0;
  1231. }
  1232. static int ar9170_get_tx_stats(struct ieee80211_hw *hw,
  1233. struct ieee80211_tx_queue_stats *tx_stats)
  1234. {
  1235. struct ar9170 *ar = hw->priv;
  1236. spin_lock_bh(&ar->tx_stats_lock);
  1237. memcpy(tx_stats, ar->tx_stats, sizeof(tx_stats[0]) * hw->queues);
  1238. spin_unlock_bh(&ar->tx_stats_lock);
  1239. return 0;
  1240. }
  1241. static int ar9170_conf_tx(struct ieee80211_hw *hw, u16 queue,
  1242. const struct ieee80211_tx_queue_params *param)
  1243. {
  1244. struct ar9170 *ar = hw->priv;
  1245. int ret;
  1246. mutex_lock(&ar->mutex);
  1247. if ((param) && !(queue > ar->hw->queues)) {
  1248. memcpy(&ar->edcf[ar9170_qos_hwmap[queue]],
  1249. param, sizeof(*param));
  1250. ret = ar9170_set_qos(ar);
  1251. } else
  1252. ret = -EINVAL;
  1253. mutex_unlock(&ar->mutex);
  1254. return ret;
  1255. }
  1256. static const struct ieee80211_ops ar9170_ops = {
  1257. .start = ar9170_op_start,
  1258. .stop = ar9170_op_stop,
  1259. .tx = ar9170_op_tx,
  1260. .add_interface = ar9170_op_add_interface,
  1261. .remove_interface = ar9170_op_remove_interface,
  1262. .config = ar9170_op_config,
  1263. .config_interface = ar9170_op_config_interface,
  1264. .configure_filter = ar9170_op_configure_filter,
  1265. .conf_tx = ar9170_conf_tx,
  1266. .bss_info_changed = ar9170_op_bss_info_changed,
  1267. .get_tsf = ar9170_op_get_tsf,
  1268. .set_key = ar9170_set_key,
  1269. .sta_notify = ar9170_sta_notify,
  1270. .get_stats = ar9170_get_stats,
  1271. .get_tx_stats = ar9170_get_tx_stats,
  1272. };
  1273. void *ar9170_alloc(size_t priv_size)
  1274. {
  1275. struct ieee80211_hw *hw;
  1276. struct ar9170 *ar;
  1277. int i;
  1278. hw = ieee80211_alloc_hw(priv_size, &ar9170_ops);
  1279. if (!hw)
  1280. return ERR_PTR(-ENOMEM);
  1281. ar = hw->priv;
  1282. ar->hw = hw;
  1283. mutex_init(&ar->mutex);
  1284. spin_lock_init(&ar->cmdlock);
  1285. spin_lock_init(&ar->tx_stats_lock);
  1286. skb_queue_head_init(&ar->global_tx_status);
  1287. skb_queue_head_init(&ar->global_tx_status_waste);
  1288. INIT_WORK(&ar->filter_config_work, ar9170_set_filters);
  1289. INIT_WORK(&ar->beacon_work, ar9170_new_beacon);
  1290. INIT_DELAYED_WORK(&ar->tx_status_janitor, ar9170_tx_status_janitor);
  1291. /* all hw supports 2.4 GHz, so set channel to 1 by default */
  1292. ar->channel = &ar9170_2ghz_chantable[0];
  1293. /* first part of wiphy init */
  1294. ar->hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
  1295. BIT(NL80211_IFTYPE_WDS) |
  1296. BIT(NL80211_IFTYPE_ADHOC);
  1297. ar->hw->flags |= IEEE80211_HW_RX_INCLUDES_FCS |
  1298. IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING |
  1299. IEEE80211_HW_SIGNAL_DBM |
  1300. IEEE80211_HW_NOISE_DBM;
  1301. ar->hw->queues = __AR9170_NUM_TXQ;
  1302. ar->hw->extra_tx_headroom = 8;
  1303. ar->hw->sta_data_size = sizeof(struct ar9170_sta_info);
  1304. ar->hw->max_rates = 1;
  1305. ar->hw->max_rate_tries = 3;
  1306. for (i = 0; i < ARRAY_SIZE(ar->noise); i++)
  1307. ar->noise[i] = -95; /* ATH_DEFAULT_NOISE_FLOOR */
  1308. return ar;
  1309. }
  1310. static int ar9170_read_eeprom(struct ar9170 *ar)
  1311. {
  1312. #define RW 8 /* number of words to read at once */
  1313. #define RB (sizeof(u32) * RW)
  1314. DECLARE_MAC_BUF(mbuf);
  1315. u8 *eeprom = (void *)&ar->eeprom;
  1316. u8 *addr = ar->eeprom.mac_address;
  1317. __le32 offsets[RW];
  1318. int i, j, err, bands = 0;
  1319. BUILD_BUG_ON(sizeof(ar->eeprom) & 3);
  1320. BUILD_BUG_ON(RB > AR9170_MAX_CMD_LEN - 4);
  1321. #ifndef __CHECKER__
  1322. /* don't want to handle trailing remains */
  1323. BUILD_BUG_ON(sizeof(ar->eeprom) % RB);
  1324. #endif
  1325. for (i = 0; i < sizeof(ar->eeprom)/RB; i++) {
  1326. for (j = 0; j < RW; j++)
  1327. offsets[j] = cpu_to_le32(AR9170_EEPROM_START +
  1328. RB * i + 4 * j);
  1329. err = ar->exec_cmd(ar, AR9170_CMD_RREG,
  1330. RB, (u8 *) &offsets,
  1331. RB, eeprom + RB * i);
  1332. if (err)
  1333. return err;
  1334. }
  1335. #undef RW
  1336. #undef RB
  1337. if (ar->eeprom.length == cpu_to_le16(0xFFFF))
  1338. return -ENODATA;
  1339. if (ar->eeprom.operating_flags & AR9170_OPFLAG_2GHZ) {
  1340. ar->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &ar9170_band_2GHz;
  1341. bands++;
  1342. }
  1343. if (ar->eeprom.operating_flags & AR9170_OPFLAG_5GHZ) {
  1344. ar->hw->wiphy->bands[IEEE80211_BAND_5GHZ] = &ar9170_band_5GHz;
  1345. bands++;
  1346. }
  1347. /*
  1348. * I measured this, a bandswitch takes roughly
  1349. * 135 ms and a frequency switch about 80.
  1350. *
  1351. * FIXME: measure these values again once EEPROM settings
  1352. * are used, that will influence them!
  1353. */
  1354. if (bands == 2)
  1355. ar->hw->channel_change_time = 135 * 1000;
  1356. else
  1357. ar->hw->channel_change_time = 80 * 1000;
  1358. /* second part of wiphy init */
  1359. SET_IEEE80211_PERM_ADDR(ar->hw, addr);
  1360. return bands ? 0 : -EINVAL;
  1361. }
  1362. int ar9170_register(struct ar9170 *ar, struct device *pdev)
  1363. {
  1364. int err;
  1365. /* try to read EEPROM, init MAC addr */
  1366. err = ar9170_read_eeprom(ar);
  1367. if (err)
  1368. goto err_out;
  1369. err = ieee80211_register_hw(ar->hw);
  1370. if (err)
  1371. goto err_out;
  1372. err = ar9170_init_leds(ar);
  1373. if (err)
  1374. goto err_unreg;
  1375. #ifdef CONFIG_AR9170_LEDS
  1376. err = ar9170_register_leds(ar);
  1377. if (err)
  1378. goto err_unreg;
  1379. #endif /* CONFIG_AR9170_LEDS */
  1380. dev_info(pdev, "Atheros AR9170 is registered as '%s'\n",
  1381. wiphy_name(ar->hw->wiphy));
  1382. return err;
  1383. err_unreg:
  1384. ieee80211_unregister_hw(ar->hw);
  1385. err_out:
  1386. return err;
  1387. }
  1388. void ar9170_unregister(struct ar9170 *ar)
  1389. {
  1390. #ifdef CONFIG_AR9170_LEDS
  1391. ar9170_unregister_leds(ar);
  1392. #endif /* CONFIG_AR9170_LEDS */
  1393. ieee80211_unregister_hw(ar->hw);
  1394. mutex_destroy(&ar->mutex);
  1395. }