main.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210
  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. ieee80211_rx_irqsafe(ar->hw, skb, &status);
  788. }
  789. void ar9170_rx(struct ar9170 *ar, struct sk_buff *skb)
  790. {
  791. unsigned int i, tlen, resplen, wlen = 0, clen = 0;
  792. u8 *tbuf, *respbuf;
  793. tbuf = skb->data;
  794. tlen = skb->len;
  795. while (tlen >= 4) {
  796. clen = tbuf[1] << 8 | tbuf[0];
  797. wlen = ALIGN(clen, 4);
  798. /* check if this is stream has a valid tag.*/
  799. if (tbuf[2] != 0 || tbuf[3] != 0x4e) {
  800. /*
  801. * TODO: handle the highly unlikely event that the
  802. * corrupted stream has the TAG at the right position.
  803. */
  804. /* check if the frame can be repaired. */
  805. if (!ar->rx_failover_missing) {
  806. /* this is no "short read". */
  807. if (ar9170_nag_limiter(ar)) {
  808. printk(KERN_ERR "%s: missing tag!\n",
  809. wiphy_name(ar->hw->wiphy));
  810. goto err_telluser;
  811. } else
  812. goto err_silent;
  813. }
  814. if (ar->rx_failover_missing > tlen) {
  815. if (ar9170_nag_limiter(ar)) {
  816. printk(KERN_ERR "%s: possible multi "
  817. "stream corruption!\n",
  818. wiphy_name(ar->hw->wiphy));
  819. goto err_telluser;
  820. } else
  821. goto err_silent;
  822. }
  823. memcpy(skb_put(ar->rx_failover, tlen), tbuf, tlen);
  824. ar->rx_failover_missing -= tlen;
  825. if (ar->rx_failover_missing <= 0) {
  826. /*
  827. * nested ar9170_rx call!
  828. * termination is guranteed, even when the
  829. * combined frame also have a element with
  830. * a bad tag.
  831. */
  832. ar->rx_failover_missing = 0;
  833. ar9170_rx(ar, ar->rx_failover);
  834. skb_reset_tail_pointer(ar->rx_failover);
  835. skb_trim(ar->rx_failover, 0);
  836. }
  837. return ;
  838. }
  839. /* check if stream is clipped */
  840. if (wlen > tlen - 4) {
  841. if (ar->rx_failover_missing) {
  842. /* TODO: handle double stream corruption. */
  843. if (ar9170_nag_limiter(ar)) {
  844. printk(KERN_ERR "%s: double rx stream "
  845. "corruption!\n",
  846. wiphy_name(ar->hw->wiphy));
  847. goto err_telluser;
  848. } else
  849. goto err_silent;
  850. }
  851. /*
  852. * save incomplete data set.
  853. * the firmware will resend the missing bits when
  854. * the rx - descriptor comes round again.
  855. */
  856. memcpy(skb_put(ar->rx_failover, tlen), tbuf, tlen);
  857. ar->rx_failover_missing = clen - tlen;
  858. return ;
  859. }
  860. resplen = clen;
  861. respbuf = tbuf + 4;
  862. tbuf += wlen + 4;
  863. tlen -= wlen + 4;
  864. i = 0;
  865. /* weird thing, but this is the same in the original driver */
  866. while (resplen > 2 && i < 12 &&
  867. respbuf[0] == 0xff && respbuf[1] == 0xff) {
  868. i += 2;
  869. resplen -= 2;
  870. respbuf += 2;
  871. }
  872. if (resplen < 4)
  873. continue;
  874. /* found the 6 * 0xffff marker? */
  875. if (i == 12)
  876. ar9170_handle_command_response(ar, respbuf, resplen);
  877. else
  878. ar9170_handle_mpdu(ar, respbuf, clen);
  879. }
  880. if (tlen) {
  881. if (net_ratelimit())
  882. printk(KERN_ERR "%s: %d bytes of unprocessed "
  883. "data left in rx stream!\n",
  884. wiphy_name(ar->hw->wiphy), tlen);
  885. goto err_telluser;
  886. }
  887. return ;
  888. err_telluser:
  889. printk(KERN_ERR "%s: damaged RX stream data [want:%d, "
  890. "data:%d, rx:%d, pending:%d ]\n",
  891. wiphy_name(ar->hw->wiphy), clen, wlen, tlen,
  892. ar->rx_failover_missing);
  893. if (ar->rx_failover_missing)
  894. print_hex_dump_bytes("rxbuf:", DUMP_PREFIX_OFFSET,
  895. ar->rx_failover->data,
  896. ar->rx_failover->len);
  897. print_hex_dump_bytes("stream:", DUMP_PREFIX_OFFSET,
  898. skb->data, skb->len);
  899. printk(KERN_ERR "%s: please check your hardware and cables, if "
  900. "you see this message frequently.\n",
  901. wiphy_name(ar->hw->wiphy));
  902. err_silent:
  903. if (ar->rx_failover_missing) {
  904. skb_reset_tail_pointer(ar->rx_failover);
  905. skb_trim(ar->rx_failover, 0);
  906. ar->rx_failover_missing = 0;
  907. }
  908. }
  909. #define AR9170_FILL_QUEUE(queue, ai_fs, cwmin, cwmax, _txop) \
  910. do { \
  911. queue.aifs = ai_fs; \
  912. queue.cw_min = cwmin; \
  913. queue.cw_max = cwmax; \
  914. queue.txop = _txop; \
  915. } while (0)
  916. static int ar9170_op_start(struct ieee80211_hw *hw)
  917. {
  918. struct ar9170 *ar = hw->priv;
  919. int err, i;
  920. mutex_lock(&ar->mutex);
  921. ar->filter_changed = 0;
  922. /* reinitialize queues statistics */
  923. memset(&ar->tx_stats, 0, sizeof(ar->tx_stats));
  924. for (i = 0; i < __AR9170_NUM_TXQ; i++)
  925. ar->tx_stats[i].limit = AR9170_TXQ_DEPTH;
  926. /* reset QoS defaults */
  927. AR9170_FILL_QUEUE(ar->edcf[0], 3, 15, 1023, 0); /* BEST EFFORT*/
  928. AR9170_FILL_QUEUE(ar->edcf[1], 7, 15, 1023, 0); /* BACKGROUND */
  929. AR9170_FILL_QUEUE(ar->edcf[2], 2, 7, 15, 94); /* VIDEO */
  930. AR9170_FILL_QUEUE(ar->edcf[3], 2, 3, 7, 47); /* VOICE */
  931. AR9170_FILL_QUEUE(ar->edcf[4], 2, 3, 7, 0); /* SPECIAL */
  932. ar->bad_hw_nagger = jiffies;
  933. err = ar->open(ar);
  934. if (err)
  935. goto out;
  936. err = ar9170_init_mac(ar);
  937. if (err)
  938. goto out;
  939. err = ar9170_set_qos(ar);
  940. if (err)
  941. goto out;
  942. err = ar9170_init_phy(ar, IEEE80211_BAND_2GHZ);
  943. if (err)
  944. goto out;
  945. err = ar9170_init_rf(ar);
  946. if (err)
  947. goto out;
  948. /* start DMA */
  949. err = ar9170_write_reg(ar, 0x1c3d30, 0x100);
  950. if (err)
  951. goto out;
  952. ar->state = AR9170_STARTED;
  953. out:
  954. mutex_unlock(&ar->mutex);
  955. return err;
  956. }
  957. static void ar9170_op_stop(struct ieee80211_hw *hw)
  958. {
  959. struct ar9170 *ar = hw->priv;
  960. unsigned int i;
  961. if (IS_STARTED(ar))
  962. ar->state = AR9170_IDLE;
  963. flush_workqueue(ar->hw->workqueue);
  964. cancel_delayed_work_sync(&ar->tx_janitor);
  965. cancel_work_sync(&ar->filter_config_work);
  966. cancel_work_sync(&ar->beacon_work);
  967. mutex_lock(&ar->mutex);
  968. if (IS_ACCEPTING_CMD(ar)) {
  969. ar9170_set_leds_state(ar, 0);
  970. /* stop DMA */
  971. ar9170_write_reg(ar, 0x1c3d30, 0);
  972. ar->stop(ar);
  973. }
  974. for (i = 0; i < __AR9170_NUM_TXQ; i++) {
  975. skb_queue_purge(&ar->tx_pending[i]);
  976. skb_queue_purge(&ar->tx_status[i]);
  977. }
  978. mutex_unlock(&ar->mutex);
  979. }
  980. static int ar9170_tx_prepare(struct ar9170 *ar, struct sk_buff *skb)
  981. {
  982. struct ieee80211_hdr *hdr;
  983. struct ar9170_tx_control *txc;
  984. struct ieee80211_tx_info *info;
  985. struct ieee80211_tx_rate *txrate;
  986. struct ar9170_tx_info *arinfo;
  987. unsigned int queue = skb_get_queue_mapping(skb);
  988. u16 keytype = 0;
  989. u16 len, icv = 0;
  990. BUILD_BUG_ON(sizeof(*arinfo) > sizeof(info->rate_driver_data));
  991. hdr = (void *)skb->data;
  992. info = IEEE80211_SKB_CB(skb);
  993. len = skb->len;
  994. txc = (void *)skb_push(skb, sizeof(*txc));
  995. if (info->control.hw_key) {
  996. icv = info->control.hw_key->icv_len;
  997. switch (info->control.hw_key->alg) {
  998. case ALG_WEP:
  999. keytype = AR9170_TX_MAC_ENCR_RC4;
  1000. break;
  1001. case ALG_TKIP:
  1002. keytype = AR9170_TX_MAC_ENCR_RC4;
  1003. break;
  1004. case ALG_CCMP:
  1005. keytype = AR9170_TX_MAC_ENCR_AES;
  1006. break;
  1007. default:
  1008. WARN_ON(1);
  1009. goto err_out;
  1010. }
  1011. }
  1012. /* Length */
  1013. txc->length = cpu_to_le16(len + icv + 4);
  1014. txc->mac_control = cpu_to_le16(AR9170_TX_MAC_HW_DURATION |
  1015. AR9170_TX_MAC_BACKOFF);
  1016. txc->mac_control |= cpu_to_le16(ar9170_qos_hwmap[queue] <<
  1017. AR9170_TX_MAC_QOS_SHIFT);
  1018. txc->mac_control |= cpu_to_le16(keytype);
  1019. txc->phy_control = cpu_to_le32(0);
  1020. if (info->flags & IEEE80211_TX_CTL_NO_ACK)
  1021. txc->mac_control |= cpu_to_le16(AR9170_TX_MAC_NO_ACK);
  1022. txrate = &info->control.rates[0];
  1023. if (txrate->flags & IEEE80211_TX_RC_USE_CTS_PROTECT)
  1024. txc->mac_control |= cpu_to_le16(AR9170_TX_MAC_PROT_CTS);
  1025. else if (txrate->flags & IEEE80211_TX_RC_USE_RTS_CTS)
  1026. txc->mac_control |= cpu_to_le16(AR9170_TX_MAC_PROT_RTS);
  1027. arinfo = (void *)info->rate_driver_data;
  1028. arinfo->timeout = jiffies + msecs_to_jiffies(AR9170_QUEUE_TIMEOUT);
  1029. if (!(info->flags & IEEE80211_TX_CTL_NO_ACK) &&
  1030. (is_valid_ether_addr(ieee80211_get_DA(hdr)))) {
  1031. if (info->flags & IEEE80211_TX_CTL_AMPDU) {
  1032. if (unlikely(!info->control.sta))
  1033. goto err_out;
  1034. txc->mac_control |= cpu_to_le16(AR9170_TX_MAC_AGGR);
  1035. arinfo->flags = AR9170_TX_FLAG_BLOCK_ACK;
  1036. goto out;
  1037. }
  1038. txc->mac_control |= cpu_to_le16(AR9170_TX_MAC_RATE_PROBE);
  1039. /*
  1040. * WARNING:
  1041. * Putting the QoS queue bits into an unexplored territory is
  1042. * certainly not elegant.
  1043. *
  1044. * In my defense: This idea provides a reasonable way to
  1045. * smuggle valuable information to the tx_status callback.
  1046. * Also, the idea behind this bit-abuse came straight from
  1047. * the original driver code.
  1048. */
  1049. txc->phy_control |=
  1050. cpu_to_le32(queue << AR9170_TX_PHY_QOS_SHIFT);
  1051. arinfo->flags = AR9170_TX_FLAG_WAIT_FOR_ACK;
  1052. } else {
  1053. arinfo->flags = AR9170_TX_FLAG_NO_ACK;
  1054. }
  1055. out:
  1056. return 0;
  1057. err_out:
  1058. skb_pull(skb, sizeof(*txc));
  1059. return -EINVAL;
  1060. }
  1061. static void ar9170_tx_prepare_phy(struct ar9170 *ar, struct sk_buff *skb)
  1062. {
  1063. struct ar9170_tx_control *txc;
  1064. struct ieee80211_tx_info *info;
  1065. struct ieee80211_rate *rate = NULL;
  1066. struct ieee80211_tx_rate *txrate;
  1067. u32 power, chains;
  1068. txc = (void *) skb->data;
  1069. info = IEEE80211_SKB_CB(skb);
  1070. txrate = &info->control.rates[0];
  1071. if (txrate->flags & IEEE80211_TX_RC_GREEN_FIELD)
  1072. txc->phy_control |= cpu_to_le32(AR9170_TX_PHY_GREENFIELD);
  1073. if (txrate->flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE)
  1074. txc->phy_control |= cpu_to_le32(AR9170_TX_PHY_SHORT_PREAMBLE);
  1075. if (txrate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
  1076. txc->phy_control |= cpu_to_le32(AR9170_TX_PHY_BW_40MHZ);
  1077. /* this works because 40 MHz is 2 and dup is 3 */
  1078. if (txrate->flags & IEEE80211_TX_RC_DUP_DATA)
  1079. txc->phy_control |= cpu_to_le32(AR9170_TX_PHY_BW_40MHZ_DUP);
  1080. if (txrate->flags & IEEE80211_TX_RC_SHORT_GI)
  1081. txc->phy_control |= cpu_to_le32(AR9170_TX_PHY_SHORT_GI);
  1082. if (txrate->flags & IEEE80211_TX_RC_MCS) {
  1083. u32 r = txrate->idx;
  1084. u8 *txpower;
  1085. /* heavy clip control */
  1086. txc->phy_control |= cpu_to_le32((r & 0x7) << 7);
  1087. r <<= AR9170_TX_PHY_MCS_SHIFT;
  1088. BUG_ON(r & ~AR9170_TX_PHY_MCS_MASK);
  1089. txc->phy_control |= cpu_to_le32(r & AR9170_TX_PHY_MCS_MASK);
  1090. txc->phy_control |= cpu_to_le32(AR9170_TX_PHY_MOD_HT);
  1091. if (txrate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH) {
  1092. if (info->band == IEEE80211_BAND_5GHZ)
  1093. txpower = ar->power_5G_ht40;
  1094. else
  1095. txpower = ar->power_2G_ht40;
  1096. } else {
  1097. if (info->band == IEEE80211_BAND_5GHZ)
  1098. txpower = ar->power_5G_ht20;
  1099. else
  1100. txpower = ar->power_2G_ht20;
  1101. }
  1102. power = txpower[(txrate->idx) & 7];
  1103. } else {
  1104. u8 *txpower;
  1105. u32 mod;
  1106. u32 phyrate;
  1107. u8 idx = txrate->idx;
  1108. if (info->band != IEEE80211_BAND_2GHZ) {
  1109. idx += 4;
  1110. txpower = ar->power_5G_leg;
  1111. mod = AR9170_TX_PHY_MOD_OFDM;
  1112. } else {
  1113. if (idx < 4) {
  1114. txpower = ar->power_2G_cck;
  1115. mod = AR9170_TX_PHY_MOD_CCK;
  1116. } else {
  1117. mod = AR9170_TX_PHY_MOD_OFDM;
  1118. txpower = ar->power_2G_ofdm;
  1119. }
  1120. }
  1121. rate = &__ar9170_ratetable[idx];
  1122. phyrate = rate->hw_value & 0xF;
  1123. power = txpower[(rate->hw_value & 0x30) >> 4];
  1124. phyrate <<= AR9170_TX_PHY_MCS_SHIFT;
  1125. txc->phy_control |= cpu_to_le32(mod);
  1126. txc->phy_control |= cpu_to_le32(phyrate);
  1127. }
  1128. power <<= AR9170_TX_PHY_TX_PWR_SHIFT;
  1129. power &= AR9170_TX_PHY_TX_PWR_MASK;
  1130. txc->phy_control |= cpu_to_le32(power);
  1131. /* set TX chains */
  1132. if (ar->eeprom.tx_mask == 1) {
  1133. chains = AR9170_TX_PHY_TXCHAIN_1;
  1134. } else {
  1135. chains = AR9170_TX_PHY_TXCHAIN_2;
  1136. /* >= 36M legacy OFDM - use only one chain */
  1137. if (rate && rate->bitrate >= 360)
  1138. chains = AR9170_TX_PHY_TXCHAIN_1;
  1139. }
  1140. txc->phy_control |= cpu_to_le32(chains << AR9170_TX_PHY_TXCHAIN_SHIFT);
  1141. }
  1142. static void ar9170_tx(struct ar9170 *ar)
  1143. {
  1144. struct sk_buff *skb;
  1145. unsigned long flags;
  1146. struct ieee80211_tx_info *info;
  1147. struct ar9170_tx_info *arinfo;
  1148. unsigned int i, frames, frames_failed, remaining_space;
  1149. int err;
  1150. bool schedule_garbagecollector = false;
  1151. BUILD_BUG_ON(sizeof(*arinfo) > sizeof(info->rate_driver_data));
  1152. if (unlikely(!IS_STARTED(ar)))
  1153. return ;
  1154. remaining_space = AR9170_TX_MAX_PENDING;
  1155. for (i = 0; i < __AR9170_NUM_TXQ; i++) {
  1156. spin_lock_irqsave(&ar->tx_stats_lock, flags);
  1157. if (ar->tx_stats[i].len >= ar->tx_stats[i].limit) {
  1158. #ifdef AR9170_QUEUE_DEBUG
  1159. printk(KERN_DEBUG "%s: queue %d full\n",
  1160. wiphy_name(ar->hw->wiphy), i);
  1161. __ar9170_dump_txstats(ar);
  1162. printk(KERN_DEBUG "stuck frames: ===> \n");
  1163. ar9170_dump_txqueue(ar, &ar->tx_pending[i]);
  1164. ar9170_dump_txqueue(ar, &ar->tx_status[i]);
  1165. #endif /* AR9170_QUEUE_DEBUG */
  1166. ieee80211_stop_queue(ar->hw, i);
  1167. spin_unlock_irqrestore(&ar->tx_stats_lock, flags);
  1168. continue;
  1169. }
  1170. frames = min(ar->tx_stats[i].limit - ar->tx_stats[i].len,
  1171. skb_queue_len(&ar->tx_pending[i]));
  1172. if (remaining_space < frames) {
  1173. #ifdef AR9170_QUEUE_DEBUG
  1174. printk(KERN_DEBUG "%s: tx quota reached queue:%d, "
  1175. "remaining slots:%d, needed:%d\n",
  1176. wiphy_name(ar->hw->wiphy), i, remaining_space,
  1177. frames);
  1178. ar9170_dump_txstats(ar);
  1179. #endif /* AR9170_QUEUE_DEBUG */
  1180. frames = remaining_space;
  1181. }
  1182. ar->tx_stats[i].len += frames;
  1183. ar->tx_stats[i].count += frames;
  1184. spin_unlock_irqrestore(&ar->tx_stats_lock, flags);
  1185. if (!frames)
  1186. continue;
  1187. frames_failed = 0;
  1188. while (frames) {
  1189. skb = skb_dequeue(&ar->tx_pending[i]);
  1190. if (unlikely(!skb)) {
  1191. frames_failed += frames;
  1192. frames = 0;
  1193. break;
  1194. }
  1195. info = IEEE80211_SKB_CB(skb);
  1196. arinfo = (void *) info->rate_driver_data;
  1197. /* TODO: cancel stuck frames */
  1198. arinfo->timeout = jiffies +
  1199. msecs_to_jiffies(AR9170_TX_TIMEOUT);
  1200. #ifdef AR9170_QUEUE_DEBUG
  1201. printk(KERN_DEBUG "%s: send frame q:%d =>\n",
  1202. wiphy_name(ar->hw->wiphy), i);
  1203. ar9170_print_txheader(ar, skb);
  1204. #endif /* AR9170_QUEUE_DEBUG */
  1205. err = ar->tx(ar, skb);
  1206. if (unlikely(err)) {
  1207. frames_failed++;
  1208. dev_kfree_skb_any(skb);
  1209. } else {
  1210. remaining_space--;
  1211. schedule_garbagecollector = true;
  1212. }
  1213. frames--;
  1214. }
  1215. #ifdef AR9170_QUEUE_DEBUG
  1216. printk(KERN_DEBUG "%s: ar9170_tx report for queue %d\n",
  1217. wiphy_name(ar->hw->wiphy), i);
  1218. printk(KERN_DEBUG "%s: unprocessed pending frames left:\n",
  1219. wiphy_name(ar->hw->wiphy));
  1220. ar9170_dump_txqueue(ar, &ar->tx_pending[i]);
  1221. #endif /* AR9170_QUEUE_DEBUG */
  1222. if (unlikely(frames_failed)) {
  1223. #ifdef AR9170_QUEUE_DEBUG
  1224. printk(KERN_DEBUG "%s: frames failed =>\n",
  1225. wiphy_name(ar->hw->wiphy), frames_failed);
  1226. #endif /* AR9170_QUEUE_DEBUG */
  1227. spin_lock_irqsave(&ar->tx_stats_lock, flags);
  1228. ar->tx_stats[i].len -= frames_failed;
  1229. ar->tx_stats[i].count -= frames_failed;
  1230. ieee80211_wake_queue(ar->hw, i);
  1231. spin_unlock_irqrestore(&ar->tx_stats_lock, flags);
  1232. }
  1233. }
  1234. if (schedule_garbagecollector)
  1235. queue_delayed_work(ar->hw->workqueue,
  1236. &ar->tx_janitor,
  1237. msecs_to_jiffies(AR9170_JANITOR_DELAY));
  1238. }
  1239. int ar9170_op_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
  1240. {
  1241. struct ar9170 *ar = hw->priv;
  1242. struct ieee80211_tx_info *info;
  1243. if (unlikely(!IS_STARTED(ar)))
  1244. goto err_free;
  1245. if (unlikely(ar9170_tx_prepare(ar, skb)))
  1246. goto err_free;
  1247. info = IEEE80211_SKB_CB(skb);
  1248. if (info->flags & IEEE80211_TX_CTL_AMPDU) {
  1249. /* drop frame, we do not allow TX A-MPDU aggregation yet. */
  1250. goto err_free;
  1251. } else {
  1252. unsigned int queue = skb_get_queue_mapping(skb);
  1253. ar9170_tx_prepare_phy(ar, skb);
  1254. skb_queue_tail(&ar->tx_pending[queue], skb);
  1255. }
  1256. ar9170_tx(ar);
  1257. return NETDEV_TX_OK;
  1258. err_free:
  1259. dev_kfree_skb_any(skb);
  1260. return NETDEV_TX_OK;
  1261. }
  1262. static int ar9170_op_add_interface(struct ieee80211_hw *hw,
  1263. struct ieee80211_if_init_conf *conf)
  1264. {
  1265. struct ar9170 *ar = hw->priv;
  1266. int err = 0;
  1267. mutex_lock(&ar->mutex);
  1268. if (ar->vif) {
  1269. err = -EBUSY;
  1270. goto unlock;
  1271. }
  1272. ar->vif = conf->vif;
  1273. memcpy(ar->mac_addr, conf->mac_addr, ETH_ALEN);
  1274. if (modparam_nohwcrypt || (ar->vif->type != NL80211_IFTYPE_STATION)) {
  1275. ar->rx_software_decryption = true;
  1276. ar->disable_offload = true;
  1277. }
  1278. ar->cur_filter = 0;
  1279. ar->want_filter = AR9170_MAC_REG_FTF_DEFAULTS;
  1280. err = ar9170_update_frame_filter(ar);
  1281. if (err)
  1282. goto unlock;
  1283. err = ar9170_set_operating_mode(ar);
  1284. unlock:
  1285. mutex_unlock(&ar->mutex);
  1286. return err;
  1287. }
  1288. static void ar9170_op_remove_interface(struct ieee80211_hw *hw,
  1289. struct ieee80211_if_init_conf *conf)
  1290. {
  1291. struct ar9170 *ar = hw->priv;
  1292. mutex_lock(&ar->mutex);
  1293. ar->vif = NULL;
  1294. ar->want_filter = 0;
  1295. ar9170_update_frame_filter(ar);
  1296. ar9170_set_beacon_timers(ar);
  1297. dev_kfree_skb(ar->beacon);
  1298. ar->beacon = NULL;
  1299. ar->sniffer_enabled = false;
  1300. ar->rx_software_decryption = false;
  1301. ar9170_set_operating_mode(ar);
  1302. mutex_unlock(&ar->mutex);
  1303. }
  1304. static int ar9170_op_config(struct ieee80211_hw *hw, u32 changed)
  1305. {
  1306. struct ar9170 *ar = hw->priv;
  1307. int err = 0;
  1308. mutex_lock(&ar->mutex);
  1309. if (changed & IEEE80211_CONF_CHANGE_LISTEN_INTERVAL) {
  1310. /* TODO */
  1311. err = 0;
  1312. }
  1313. if (changed & IEEE80211_CONF_CHANGE_PS) {
  1314. /* TODO */
  1315. err = 0;
  1316. }
  1317. if (changed & IEEE80211_CONF_CHANGE_POWER) {
  1318. /* TODO */
  1319. err = 0;
  1320. }
  1321. if (changed & IEEE80211_CONF_CHANGE_RETRY_LIMITS) {
  1322. /*
  1323. * is it long_frame_max_tx_count or short_frame_max_tx_count?
  1324. */
  1325. err = ar9170_set_hwretry_limit(ar,
  1326. ar->hw->conf.long_frame_max_tx_count);
  1327. if (err)
  1328. goto out;
  1329. }
  1330. if (changed & BSS_CHANGED_BEACON_INT) {
  1331. err = ar9170_set_beacon_timers(ar);
  1332. if (err)
  1333. goto out;
  1334. }
  1335. if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
  1336. /* adjust slot time for 5 GHz */
  1337. err = ar9170_set_slot_time(ar);
  1338. if (err)
  1339. goto out;
  1340. err = ar9170_set_dyn_sifs_ack(ar);
  1341. if (err)
  1342. goto out;
  1343. err = ar9170_set_channel(ar, hw->conf.channel,
  1344. AR9170_RFI_NONE,
  1345. nl80211_to_ar9170(hw->conf.channel_type));
  1346. if (err)
  1347. goto out;
  1348. }
  1349. out:
  1350. mutex_unlock(&ar->mutex);
  1351. return err;
  1352. }
  1353. static void ar9170_set_filters(struct work_struct *work)
  1354. {
  1355. struct ar9170 *ar = container_of(work, struct ar9170,
  1356. filter_config_work);
  1357. int err;
  1358. if (unlikely(!IS_STARTED(ar)))
  1359. return ;
  1360. mutex_lock(&ar->mutex);
  1361. if (test_and_clear_bit(AR9170_FILTER_CHANGED_MODE,
  1362. &ar->filter_changed)) {
  1363. err = ar9170_set_operating_mode(ar);
  1364. if (err)
  1365. goto unlock;
  1366. }
  1367. if (test_and_clear_bit(AR9170_FILTER_CHANGED_MULTICAST,
  1368. &ar->filter_changed)) {
  1369. err = ar9170_update_multicast(ar);
  1370. if (err)
  1371. goto unlock;
  1372. }
  1373. if (test_and_clear_bit(AR9170_FILTER_CHANGED_FRAMEFILTER,
  1374. &ar->filter_changed)) {
  1375. err = ar9170_update_frame_filter(ar);
  1376. if (err)
  1377. goto unlock;
  1378. }
  1379. unlock:
  1380. mutex_unlock(&ar->mutex);
  1381. }
  1382. static void ar9170_op_configure_filter(struct ieee80211_hw *hw,
  1383. unsigned int changed_flags,
  1384. unsigned int *new_flags,
  1385. int mc_count, struct dev_mc_list *mclist)
  1386. {
  1387. struct ar9170 *ar = hw->priv;
  1388. /* mask supported flags */
  1389. *new_flags &= FIF_ALLMULTI | FIF_CONTROL | FIF_BCN_PRBRESP_PROMISC |
  1390. FIF_PROMISC_IN_BSS | FIF_FCSFAIL | FIF_PLCPFAIL;
  1391. ar->filter_state = *new_flags;
  1392. /*
  1393. * We can support more by setting the sniffer bit and
  1394. * then checking the error flags, later.
  1395. */
  1396. if (changed_flags & FIF_ALLMULTI) {
  1397. if (*new_flags & FIF_ALLMULTI) {
  1398. ar->want_mc_hash = ~0ULL;
  1399. } else {
  1400. u64 mchash;
  1401. int i;
  1402. /* always get broadcast frames */
  1403. mchash = 1ULL << (0xff >> 2);
  1404. for (i = 0; i < mc_count; i++) {
  1405. if (WARN_ON(!mclist))
  1406. break;
  1407. mchash |= 1ULL << (mclist->dmi_addr[5] >> 2);
  1408. mclist = mclist->next;
  1409. }
  1410. ar->want_mc_hash = mchash;
  1411. }
  1412. set_bit(AR9170_FILTER_CHANGED_MULTICAST, &ar->filter_changed);
  1413. }
  1414. if (changed_flags & FIF_CONTROL) {
  1415. u32 filter = AR9170_MAC_REG_FTF_PSPOLL |
  1416. AR9170_MAC_REG_FTF_RTS |
  1417. AR9170_MAC_REG_FTF_CTS |
  1418. AR9170_MAC_REG_FTF_ACK |
  1419. AR9170_MAC_REG_FTF_CFE |
  1420. AR9170_MAC_REG_FTF_CFE_ACK;
  1421. if (*new_flags & FIF_CONTROL)
  1422. ar->want_filter = ar->cur_filter | filter;
  1423. else
  1424. ar->want_filter = ar->cur_filter & ~filter;
  1425. set_bit(AR9170_FILTER_CHANGED_FRAMEFILTER,
  1426. &ar->filter_changed);
  1427. }
  1428. if (changed_flags & FIF_PROMISC_IN_BSS) {
  1429. ar->sniffer_enabled = ((*new_flags) & FIF_PROMISC_IN_BSS) != 0;
  1430. set_bit(AR9170_FILTER_CHANGED_MODE,
  1431. &ar->filter_changed);
  1432. }
  1433. if (likely(IS_STARTED(ar)))
  1434. queue_work(ar->hw->workqueue, &ar->filter_config_work);
  1435. }
  1436. static void ar9170_op_bss_info_changed(struct ieee80211_hw *hw,
  1437. struct ieee80211_vif *vif,
  1438. struct ieee80211_bss_conf *bss_conf,
  1439. u32 changed)
  1440. {
  1441. struct ar9170 *ar = hw->priv;
  1442. int err = 0;
  1443. mutex_lock(&ar->mutex);
  1444. if (changed & BSS_CHANGED_BSSID) {
  1445. memcpy(ar->bssid, bss_conf->bssid, ETH_ALEN);
  1446. err = ar9170_set_operating_mode(ar);
  1447. if (err)
  1448. goto out;
  1449. }
  1450. if (changed & (BSS_CHANGED_BEACON | BSS_CHANGED_BEACON_ENABLED)) {
  1451. err = ar9170_update_beacon(ar);
  1452. if (err)
  1453. goto out;
  1454. err = ar9170_set_beacon_timers(ar);
  1455. if (err)
  1456. goto out;
  1457. }
  1458. if (changed & BSS_CHANGED_ASSOC) {
  1459. #ifndef CONFIG_AR9170_LEDS
  1460. /* enable assoc LED. */
  1461. err = ar9170_set_leds_state(ar, bss_conf->assoc ? 2 : 0);
  1462. #endif /* CONFIG_AR9170_LEDS */
  1463. }
  1464. if (changed & BSS_CHANGED_BEACON_INT) {
  1465. err = ar9170_set_beacon_timers(ar);
  1466. if (err)
  1467. goto out;
  1468. }
  1469. if (changed & BSS_CHANGED_HT) {
  1470. /* TODO */
  1471. err = 0;
  1472. }
  1473. if (changed & BSS_CHANGED_ERP_SLOT) {
  1474. err = ar9170_set_slot_time(ar);
  1475. if (err)
  1476. goto out;
  1477. }
  1478. if (changed & BSS_CHANGED_BASIC_RATES) {
  1479. err = ar9170_set_basic_rates(ar);
  1480. if (err)
  1481. goto out;
  1482. }
  1483. out:
  1484. mutex_unlock(&ar->mutex);
  1485. }
  1486. static u64 ar9170_op_get_tsf(struct ieee80211_hw *hw)
  1487. {
  1488. struct ar9170 *ar = hw->priv;
  1489. int err;
  1490. u32 tsf_low;
  1491. u32 tsf_high;
  1492. u64 tsf;
  1493. mutex_lock(&ar->mutex);
  1494. err = ar9170_read_reg(ar, AR9170_MAC_REG_TSF_L, &tsf_low);
  1495. if (!err)
  1496. err = ar9170_read_reg(ar, AR9170_MAC_REG_TSF_H, &tsf_high);
  1497. mutex_unlock(&ar->mutex);
  1498. if (WARN_ON(err))
  1499. return 0;
  1500. tsf = tsf_high;
  1501. tsf = (tsf << 32) | tsf_low;
  1502. return tsf;
  1503. }
  1504. static int ar9170_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
  1505. struct ieee80211_vif *vif, struct ieee80211_sta *sta,
  1506. struct ieee80211_key_conf *key)
  1507. {
  1508. struct ar9170 *ar = hw->priv;
  1509. int err = 0, i;
  1510. u8 ktype;
  1511. if ((!ar->vif) || (ar->disable_offload))
  1512. return -EOPNOTSUPP;
  1513. switch (key->alg) {
  1514. case ALG_WEP:
  1515. if (key->keylen == WLAN_KEY_LEN_WEP40)
  1516. ktype = AR9170_ENC_ALG_WEP64;
  1517. else
  1518. ktype = AR9170_ENC_ALG_WEP128;
  1519. break;
  1520. case ALG_TKIP:
  1521. ktype = AR9170_ENC_ALG_TKIP;
  1522. break;
  1523. case ALG_CCMP:
  1524. ktype = AR9170_ENC_ALG_AESCCMP;
  1525. break;
  1526. default:
  1527. return -EOPNOTSUPP;
  1528. }
  1529. mutex_lock(&ar->mutex);
  1530. if (cmd == SET_KEY) {
  1531. if (unlikely(!IS_STARTED(ar))) {
  1532. err = -EOPNOTSUPP;
  1533. goto out;
  1534. }
  1535. /* group keys need all-zeroes address */
  1536. if (!(key->flags & IEEE80211_KEY_FLAG_PAIRWISE))
  1537. sta = NULL;
  1538. if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
  1539. for (i = 0; i < 64; i++)
  1540. if (!(ar->usedkeys & BIT(i)))
  1541. break;
  1542. if (i == 64) {
  1543. ar->rx_software_decryption = true;
  1544. ar9170_set_operating_mode(ar);
  1545. err = -ENOSPC;
  1546. goto out;
  1547. }
  1548. } else {
  1549. i = 64 + key->keyidx;
  1550. }
  1551. key->hw_key_idx = i;
  1552. err = ar9170_upload_key(ar, i, sta ? sta->addr : NULL, ktype, 0,
  1553. key->key, min_t(u8, 16, key->keylen));
  1554. if (err)
  1555. goto out;
  1556. if (key->alg == ALG_TKIP) {
  1557. err = ar9170_upload_key(ar, i, sta ? sta->addr : NULL,
  1558. ktype, 1, key->key + 16, 16);
  1559. if (err)
  1560. goto out;
  1561. /*
  1562. * hardware is not capable generating the MMIC
  1563. * for fragmented frames!
  1564. */
  1565. key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
  1566. }
  1567. if (i < 64)
  1568. ar->usedkeys |= BIT(i);
  1569. key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  1570. } else {
  1571. if (unlikely(!IS_STARTED(ar))) {
  1572. /* The device is gone... together with the key ;-) */
  1573. err = 0;
  1574. goto out;
  1575. }
  1576. err = ar9170_disable_key(ar, key->hw_key_idx);
  1577. if (err)
  1578. goto out;
  1579. if (key->hw_key_idx < 64) {
  1580. ar->usedkeys &= ~BIT(key->hw_key_idx);
  1581. } else {
  1582. err = ar9170_upload_key(ar, key->hw_key_idx, NULL,
  1583. AR9170_ENC_ALG_NONE, 0,
  1584. NULL, 0);
  1585. if (err)
  1586. goto out;
  1587. if (key->alg == ALG_TKIP) {
  1588. err = ar9170_upload_key(ar, key->hw_key_idx,
  1589. NULL,
  1590. AR9170_ENC_ALG_NONE, 1,
  1591. NULL, 0);
  1592. if (err)
  1593. goto out;
  1594. }
  1595. }
  1596. }
  1597. ar9170_regwrite_begin(ar);
  1598. ar9170_regwrite(AR9170_MAC_REG_ROLL_CALL_TBL_L, ar->usedkeys);
  1599. ar9170_regwrite(AR9170_MAC_REG_ROLL_CALL_TBL_H, ar->usedkeys >> 32);
  1600. ar9170_regwrite_finish();
  1601. err = ar9170_regwrite_result();
  1602. out:
  1603. mutex_unlock(&ar->mutex);
  1604. return err;
  1605. }
  1606. static void ar9170_sta_notify(struct ieee80211_hw *hw,
  1607. struct ieee80211_vif *vif,
  1608. enum sta_notify_cmd cmd,
  1609. struct ieee80211_sta *sta)
  1610. {
  1611. }
  1612. static int ar9170_get_stats(struct ieee80211_hw *hw,
  1613. struct ieee80211_low_level_stats *stats)
  1614. {
  1615. struct ar9170 *ar = hw->priv;
  1616. u32 val;
  1617. int err;
  1618. mutex_lock(&ar->mutex);
  1619. err = ar9170_read_reg(ar, AR9170_MAC_REG_TX_RETRY, &val);
  1620. ar->stats.dot11ACKFailureCount += val;
  1621. memcpy(stats, &ar->stats, sizeof(*stats));
  1622. mutex_unlock(&ar->mutex);
  1623. return 0;
  1624. }
  1625. static int ar9170_get_tx_stats(struct ieee80211_hw *hw,
  1626. struct ieee80211_tx_queue_stats *tx_stats)
  1627. {
  1628. struct ar9170 *ar = hw->priv;
  1629. spin_lock_bh(&ar->tx_stats_lock);
  1630. memcpy(tx_stats, ar->tx_stats, sizeof(tx_stats[0]) * hw->queues);
  1631. spin_unlock_bh(&ar->tx_stats_lock);
  1632. return 0;
  1633. }
  1634. static int ar9170_conf_tx(struct ieee80211_hw *hw, u16 queue,
  1635. const struct ieee80211_tx_queue_params *param)
  1636. {
  1637. struct ar9170 *ar = hw->priv;
  1638. int ret;
  1639. mutex_lock(&ar->mutex);
  1640. if ((param) && !(queue > __AR9170_NUM_TXQ)) {
  1641. memcpy(&ar->edcf[ar9170_qos_hwmap[queue]],
  1642. param, sizeof(*param));
  1643. ret = ar9170_set_qos(ar);
  1644. } else
  1645. ret = -EINVAL;
  1646. mutex_unlock(&ar->mutex);
  1647. return ret;
  1648. }
  1649. static int ar9170_ampdu_action(struct ieee80211_hw *hw,
  1650. enum ieee80211_ampdu_mlme_action action,
  1651. struct ieee80211_sta *sta, u16 tid, u16 *ssn)
  1652. {
  1653. switch (action) {
  1654. case IEEE80211_AMPDU_RX_START:
  1655. case IEEE80211_AMPDU_RX_STOP:
  1656. /*
  1657. * Something goes wrong -- RX locks up
  1658. * after a while of receiving aggregated
  1659. * frames -- not enabling for now.
  1660. */
  1661. return -EOPNOTSUPP;
  1662. default:
  1663. return -EOPNOTSUPP;
  1664. }
  1665. }
  1666. static const struct ieee80211_ops ar9170_ops = {
  1667. .start = ar9170_op_start,
  1668. .stop = ar9170_op_stop,
  1669. .tx = ar9170_op_tx,
  1670. .add_interface = ar9170_op_add_interface,
  1671. .remove_interface = ar9170_op_remove_interface,
  1672. .config = ar9170_op_config,
  1673. .configure_filter = ar9170_op_configure_filter,
  1674. .conf_tx = ar9170_conf_tx,
  1675. .bss_info_changed = ar9170_op_bss_info_changed,
  1676. .get_tsf = ar9170_op_get_tsf,
  1677. .set_key = ar9170_set_key,
  1678. .sta_notify = ar9170_sta_notify,
  1679. .get_stats = ar9170_get_stats,
  1680. .get_tx_stats = ar9170_get_tx_stats,
  1681. .ampdu_action = ar9170_ampdu_action,
  1682. };
  1683. void *ar9170_alloc(size_t priv_size)
  1684. {
  1685. struct ieee80211_hw *hw;
  1686. struct ar9170 *ar;
  1687. struct sk_buff *skb;
  1688. int i;
  1689. /*
  1690. * this buffer is used for rx stream reconstruction.
  1691. * Under heavy load this device (or the transport layer?)
  1692. * tends to split the streams into seperate rx descriptors.
  1693. */
  1694. skb = __dev_alloc_skb(AR9170_MAX_RX_BUFFER_SIZE, GFP_KERNEL);
  1695. if (!skb)
  1696. goto err_nomem;
  1697. hw = ieee80211_alloc_hw(priv_size, &ar9170_ops);
  1698. if (!hw)
  1699. goto err_nomem;
  1700. ar = hw->priv;
  1701. ar->hw = hw;
  1702. ar->rx_failover = skb;
  1703. mutex_init(&ar->mutex);
  1704. spin_lock_init(&ar->cmdlock);
  1705. spin_lock_init(&ar->tx_stats_lock);
  1706. for (i = 0; i < __AR9170_NUM_TXQ; i++) {
  1707. skb_queue_head_init(&ar->tx_status[i]);
  1708. skb_queue_head_init(&ar->tx_pending[i]);
  1709. }
  1710. ar9170_rx_reset_rx_mpdu(ar);
  1711. INIT_WORK(&ar->filter_config_work, ar9170_set_filters);
  1712. INIT_WORK(&ar->beacon_work, ar9170_new_beacon);
  1713. INIT_DELAYED_WORK(&ar->tx_janitor, ar9170_tx_janitor);
  1714. /* all hw supports 2.4 GHz, so set channel to 1 by default */
  1715. ar->channel = &ar9170_2ghz_chantable[0];
  1716. /* first part of wiphy init */
  1717. ar->hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
  1718. BIT(NL80211_IFTYPE_WDS) |
  1719. BIT(NL80211_IFTYPE_ADHOC);
  1720. ar->hw->flags |= IEEE80211_HW_RX_INCLUDES_FCS |
  1721. IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING |
  1722. IEEE80211_HW_SIGNAL_DBM |
  1723. IEEE80211_HW_NOISE_DBM;
  1724. ar->hw->queues = __AR9170_NUM_TXQ;
  1725. ar->hw->extra_tx_headroom = 8;
  1726. ar->hw->sta_data_size = sizeof(struct ar9170_sta_info);
  1727. ar->hw->max_rates = 1;
  1728. ar->hw->max_rate_tries = 3;
  1729. for (i = 0; i < ARRAY_SIZE(ar->noise); i++)
  1730. ar->noise[i] = -95; /* ATH_DEFAULT_NOISE_FLOOR */
  1731. return ar;
  1732. err_nomem:
  1733. kfree_skb(skb);
  1734. return ERR_PTR(-ENOMEM);
  1735. }
  1736. static int ar9170_read_eeprom(struct ar9170 *ar)
  1737. {
  1738. #define RW 8 /* number of words to read at once */
  1739. #define RB (sizeof(u32) * RW)
  1740. DECLARE_MAC_BUF(mbuf);
  1741. u8 *eeprom = (void *)&ar->eeprom;
  1742. u8 *addr = ar->eeprom.mac_address;
  1743. __le32 offsets[RW];
  1744. int i, j, err, bands = 0;
  1745. BUILD_BUG_ON(sizeof(ar->eeprom) & 3);
  1746. BUILD_BUG_ON(RB > AR9170_MAX_CMD_LEN - 4);
  1747. #ifndef __CHECKER__
  1748. /* don't want to handle trailing remains */
  1749. BUILD_BUG_ON(sizeof(ar->eeprom) % RB);
  1750. #endif
  1751. for (i = 0; i < sizeof(ar->eeprom)/RB; i++) {
  1752. for (j = 0; j < RW; j++)
  1753. offsets[j] = cpu_to_le32(AR9170_EEPROM_START +
  1754. RB * i + 4 * j);
  1755. err = ar->exec_cmd(ar, AR9170_CMD_RREG,
  1756. RB, (u8 *) &offsets,
  1757. RB, eeprom + RB * i);
  1758. if (err)
  1759. return err;
  1760. }
  1761. #undef RW
  1762. #undef RB
  1763. if (ar->eeprom.length == cpu_to_le16(0xFFFF))
  1764. return -ENODATA;
  1765. if (ar->eeprom.operating_flags & AR9170_OPFLAG_2GHZ) {
  1766. ar->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &ar9170_band_2GHz;
  1767. bands++;
  1768. }
  1769. if (ar->eeprom.operating_flags & AR9170_OPFLAG_5GHZ) {
  1770. ar->hw->wiphy->bands[IEEE80211_BAND_5GHZ] = &ar9170_band_5GHz;
  1771. bands++;
  1772. }
  1773. /*
  1774. * I measured this, a bandswitch takes roughly
  1775. * 135 ms and a frequency switch about 80.
  1776. *
  1777. * FIXME: measure these values again once EEPROM settings
  1778. * are used, that will influence them!
  1779. */
  1780. if (bands == 2)
  1781. ar->hw->channel_change_time = 135 * 1000;
  1782. else
  1783. ar->hw->channel_change_time = 80 * 1000;
  1784. ar->regulatory.current_rd = le16_to_cpu(ar->eeprom.reg_domain[0]);
  1785. ar->regulatory.current_rd_ext = le16_to_cpu(ar->eeprom.reg_domain[1]);
  1786. /* second part of wiphy init */
  1787. SET_IEEE80211_PERM_ADDR(ar->hw, addr);
  1788. return bands ? 0 : -EINVAL;
  1789. }
  1790. static int ar9170_reg_notifier(struct wiphy *wiphy,
  1791. struct regulatory_request *request)
  1792. {
  1793. struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
  1794. struct ar9170 *ar = hw->priv;
  1795. return ath_reg_notifier_apply(wiphy, request, &ar->regulatory);
  1796. }
  1797. int ar9170_register(struct ar9170 *ar, struct device *pdev)
  1798. {
  1799. int err;
  1800. /* try to read EEPROM, init MAC addr */
  1801. err = ar9170_read_eeprom(ar);
  1802. if (err)
  1803. goto err_out;
  1804. err = ath_regd_init(&ar->regulatory, ar->hw->wiphy,
  1805. ar9170_reg_notifier);
  1806. if (err)
  1807. goto err_out;
  1808. err = ieee80211_register_hw(ar->hw);
  1809. if (err)
  1810. goto err_out;
  1811. if (!ath_is_world_regd(&ar->regulatory))
  1812. regulatory_hint(ar->hw->wiphy, ar->regulatory.alpha2);
  1813. err = ar9170_init_leds(ar);
  1814. if (err)
  1815. goto err_unreg;
  1816. #ifdef CONFIG_AR9170_LEDS
  1817. err = ar9170_register_leds(ar);
  1818. if (err)
  1819. goto err_unreg;
  1820. #endif /* CONFIG_AR9170_LEDS */
  1821. dev_info(pdev, "Atheros AR9170 is registered as '%s'\n",
  1822. wiphy_name(ar->hw->wiphy));
  1823. return err;
  1824. err_unreg:
  1825. ieee80211_unregister_hw(ar->hw);
  1826. err_out:
  1827. return err;
  1828. }
  1829. void ar9170_unregister(struct ar9170 *ar)
  1830. {
  1831. #ifdef CONFIG_AR9170_LEDS
  1832. ar9170_unregister_leds(ar);
  1833. #endif /* CONFIG_AR9170_LEDS */
  1834. kfree_skb(ar->rx_failover);
  1835. ieee80211_unregister_hw(ar->hw);
  1836. mutex_destroy(&ar->mutex);
  1837. }