main.c 53 KB

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