main.c 47 KB

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