main.c 41 KB

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