main.c 47 KB

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