main.c 52 KB

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