main.c 48 KB

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