main.c 83 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284
  1. /*
  2. * Copyright (c) 2008-2009 Atheros Communications Inc.
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include <linux/nl80211.h>
  17. #include "ath9k.h"
  18. #include "btcoex.h"
  19. static char *dev_info = "ath9k";
  20. MODULE_AUTHOR("Atheros Communications");
  21. MODULE_DESCRIPTION("Support for Atheros 802.11n wireless LAN cards.");
  22. MODULE_SUPPORTED_DEVICE("Atheros 802.11n WLAN cards");
  23. MODULE_LICENSE("Dual BSD/GPL");
  24. static int modparam_nohwcrypt;
  25. module_param_named(nohwcrypt, modparam_nohwcrypt, int, 0444);
  26. MODULE_PARM_DESC(nohwcrypt, "Disable hardware encryption");
  27. /* We use the hw_value as an index into our private channel structure */
  28. #define CHAN2G(_freq, _idx) { \
  29. .center_freq = (_freq), \
  30. .hw_value = (_idx), \
  31. .max_power = 20, \
  32. }
  33. #define CHAN5G(_freq, _idx) { \
  34. .band = IEEE80211_BAND_5GHZ, \
  35. .center_freq = (_freq), \
  36. .hw_value = (_idx), \
  37. .max_power = 20, \
  38. }
  39. /* Some 2 GHz radios are actually tunable on 2312-2732
  40. * on 5 MHz steps, we support the channels which we know
  41. * we have calibration data for all cards though to make
  42. * this static */
  43. static struct ieee80211_channel ath9k_2ghz_chantable[] = {
  44. CHAN2G(2412, 0), /* Channel 1 */
  45. CHAN2G(2417, 1), /* Channel 2 */
  46. CHAN2G(2422, 2), /* Channel 3 */
  47. CHAN2G(2427, 3), /* Channel 4 */
  48. CHAN2G(2432, 4), /* Channel 5 */
  49. CHAN2G(2437, 5), /* Channel 6 */
  50. CHAN2G(2442, 6), /* Channel 7 */
  51. CHAN2G(2447, 7), /* Channel 8 */
  52. CHAN2G(2452, 8), /* Channel 9 */
  53. CHAN2G(2457, 9), /* Channel 10 */
  54. CHAN2G(2462, 10), /* Channel 11 */
  55. CHAN2G(2467, 11), /* Channel 12 */
  56. CHAN2G(2472, 12), /* Channel 13 */
  57. CHAN2G(2484, 13), /* Channel 14 */
  58. };
  59. /* Some 5 GHz radios are actually tunable on XXXX-YYYY
  60. * on 5 MHz steps, we support the channels which we know
  61. * we have calibration data for all cards though to make
  62. * this static */
  63. static struct ieee80211_channel ath9k_5ghz_chantable[] = {
  64. /* _We_ call this UNII 1 */
  65. CHAN5G(5180, 14), /* Channel 36 */
  66. CHAN5G(5200, 15), /* Channel 40 */
  67. CHAN5G(5220, 16), /* Channel 44 */
  68. CHAN5G(5240, 17), /* Channel 48 */
  69. /* _We_ call this UNII 2 */
  70. CHAN5G(5260, 18), /* Channel 52 */
  71. CHAN5G(5280, 19), /* Channel 56 */
  72. CHAN5G(5300, 20), /* Channel 60 */
  73. CHAN5G(5320, 21), /* Channel 64 */
  74. /* _We_ call this "Middle band" */
  75. CHAN5G(5500, 22), /* Channel 100 */
  76. CHAN5G(5520, 23), /* Channel 104 */
  77. CHAN5G(5540, 24), /* Channel 108 */
  78. CHAN5G(5560, 25), /* Channel 112 */
  79. CHAN5G(5580, 26), /* Channel 116 */
  80. CHAN5G(5600, 27), /* Channel 120 */
  81. CHAN5G(5620, 28), /* Channel 124 */
  82. CHAN5G(5640, 29), /* Channel 128 */
  83. CHAN5G(5660, 30), /* Channel 132 */
  84. CHAN5G(5680, 31), /* Channel 136 */
  85. CHAN5G(5700, 32), /* Channel 140 */
  86. /* _We_ call this UNII 3 */
  87. CHAN5G(5745, 33), /* Channel 149 */
  88. CHAN5G(5765, 34), /* Channel 153 */
  89. CHAN5G(5785, 35), /* Channel 157 */
  90. CHAN5G(5805, 36), /* Channel 161 */
  91. CHAN5G(5825, 37), /* Channel 165 */
  92. };
  93. static void ath_cache_conf_rate(struct ath_softc *sc,
  94. struct ieee80211_conf *conf)
  95. {
  96. switch (conf->channel->band) {
  97. case IEEE80211_BAND_2GHZ:
  98. if (conf_is_ht20(conf))
  99. sc->cur_rate_table =
  100. sc->hw_rate_table[ATH9K_MODE_11NG_HT20];
  101. else if (conf_is_ht40_minus(conf))
  102. sc->cur_rate_table =
  103. sc->hw_rate_table[ATH9K_MODE_11NG_HT40MINUS];
  104. else if (conf_is_ht40_plus(conf))
  105. sc->cur_rate_table =
  106. sc->hw_rate_table[ATH9K_MODE_11NG_HT40PLUS];
  107. else
  108. sc->cur_rate_table =
  109. sc->hw_rate_table[ATH9K_MODE_11G];
  110. break;
  111. case IEEE80211_BAND_5GHZ:
  112. if (conf_is_ht20(conf))
  113. sc->cur_rate_table =
  114. sc->hw_rate_table[ATH9K_MODE_11NA_HT20];
  115. else if (conf_is_ht40_minus(conf))
  116. sc->cur_rate_table =
  117. sc->hw_rate_table[ATH9K_MODE_11NA_HT40MINUS];
  118. else if (conf_is_ht40_plus(conf))
  119. sc->cur_rate_table =
  120. sc->hw_rate_table[ATH9K_MODE_11NA_HT40PLUS];
  121. else
  122. sc->cur_rate_table =
  123. sc->hw_rate_table[ATH9K_MODE_11A];
  124. break;
  125. default:
  126. BUG_ON(1);
  127. break;
  128. }
  129. }
  130. static void ath_update_txpow(struct ath_softc *sc)
  131. {
  132. struct ath_hw *ah = sc->sc_ah;
  133. u32 txpow;
  134. if (sc->curtxpow != sc->config.txpowlimit) {
  135. ath9k_hw_set_txpowerlimit(ah, sc->config.txpowlimit);
  136. /* read back in case value is clamped */
  137. ath9k_hw_getcapability(ah, ATH9K_CAP_TXPOW, 1, &txpow);
  138. sc->curtxpow = txpow;
  139. }
  140. }
  141. static u8 parse_mpdudensity(u8 mpdudensity)
  142. {
  143. /*
  144. * 802.11n D2.0 defined values for "Minimum MPDU Start Spacing":
  145. * 0 for no restriction
  146. * 1 for 1/4 us
  147. * 2 for 1/2 us
  148. * 3 for 1 us
  149. * 4 for 2 us
  150. * 5 for 4 us
  151. * 6 for 8 us
  152. * 7 for 16 us
  153. */
  154. switch (mpdudensity) {
  155. case 0:
  156. return 0;
  157. case 1:
  158. case 2:
  159. case 3:
  160. /* Our lower layer calculations limit our precision to
  161. 1 microsecond */
  162. return 1;
  163. case 4:
  164. return 2;
  165. case 5:
  166. return 4;
  167. case 6:
  168. return 8;
  169. case 7:
  170. return 16;
  171. default:
  172. return 0;
  173. }
  174. }
  175. static void ath_setup_rates(struct ath_softc *sc, enum ieee80211_band band)
  176. {
  177. const struct ath_rate_table *rate_table = NULL;
  178. struct ieee80211_supported_band *sband;
  179. struct ieee80211_rate *rate;
  180. int i, maxrates;
  181. switch (band) {
  182. case IEEE80211_BAND_2GHZ:
  183. rate_table = sc->hw_rate_table[ATH9K_MODE_11G];
  184. break;
  185. case IEEE80211_BAND_5GHZ:
  186. rate_table = sc->hw_rate_table[ATH9K_MODE_11A];
  187. break;
  188. default:
  189. break;
  190. }
  191. if (rate_table == NULL)
  192. return;
  193. sband = &sc->sbands[band];
  194. rate = sc->rates[band];
  195. if (rate_table->rate_cnt > ATH_RATE_MAX)
  196. maxrates = ATH_RATE_MAX;
  197. else
  198. maxrates = rate_table->rate_cnt;
  199. for (i = 0; i < maxrates; i++) {
  200. rate[i].bitrate = rate_table->info[i].ratekbps / 100;
  201. rate[i].hw_value = rate_table->info[i].ratecode;
  202. if (rate_table->info[i].short_preamble) {
  203. rate[i].hw_value_short = rate_table->info[i].ratecode |
  204. rate_table->info[i].short_preamble;
  205. rate[i].flags = IEEE80211_RATE_SHORT_PREAMBLE;
  206. }
  207. sband->n_bitrates++;
  208. ath_print(ath9k_hw_common(sc->sc_ah), ATH_DBG_CONFIG,
  209. "Rate: %2dMbps, ratecode: %2d\n",
  210. rate[i].bitrate / 10, rate[i].hw_value);
  211. }
  212. }
  213. static struct ath9k_channel *ath_get_curchannel(struct ath_softc *sc,
  214. struct ieee80211_hw *hw)
  215. {
  216. struct ieee80211_channel *curchan = hw->conf.channel;
  217. struct ath9k_channel *channel;
  218. u8 chan_idx;
  219. chan_idx = curchan->hw_value;
  220. channel = &sc->sc_ah->channels[chan_idx];
  221. ath9k_update_ichannel(sc, hw, channel);
  222. return channel;
  223. }
  224. static bool ath9k_setpower(struct ath_softc *sc, enum ath9k_power_mode mode)
  225. {
  226. unsigned long flags;
  227. bool ret;
  228. spin_lock_irqsave(&sc->sc_pm_lock, flags);
  229. ret = ath9k_hw_setpower(sc->sc_ah, mode);
  230. spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
  231. return ret;
  232. }
  233. void ath9k_ps_wakeup(struct ath_softc *sc)
  234. {
  235. unsigned long flags;
  236. spin_lock_irqsave(&sc->sc_pm_lock, flags);
  237. if (++sc->ps_usecount != 1)
  238. goto unlock;
  239. ath9k_hw_setpower(sc->sc_ah, ATH9K_PM_AWAKE);
  240. unlock:
  241. spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
  242. }
  243. void ath9k_ps_restore(struct ath_softc *sc)
  244. {
  245. unsigned long flags;
  246. spin_lock_irqsave(&sc->sc_pm_lock, flags);
  247. if (--sc->ps_usecount != 0)
  248. goto unlock;
  249. if (sc->ps_enabled &&
  250. !(sc->sc_flags & (SC_OP_WAIT_FOR_BEACON |
  251. SC_OP_WAIT_FOR_CAB |
  252. SC_OP_WAIT_FOR_PSPOLL_DATA |
  253. SC_OP_WAIT_FOR_TX_ACK)))
  254. ath9k_hw_setpower(sc->sc_ah, ATH9K_PM_NETWORK_SLEEP);
  255. unlock:
  256. spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
  257. }
  258. /*
  259. * Set/change channels. If the channel is really being changed, it's done
  260. * by reseting the chip. To accomplish this we must first cleanup any pending
  261. * DMA, then restart stuff.
  262. */
  263. int ath_set_channel(struct ath_softc *sc, struct ieee80211_hw *hw,
  264. struct ath9k_channel *hchan)
  265. {
  266. struct ath_hw *ah = sc->sc_ah;
  267. struct ath_common *common = ath9k_hw_common(ah);
  268. bool fastcc = true, stopped;
  269. struct ieee80211_channel *channel = hw->conf.channel;
  270. int r;
  271. if (sc->sc_flags & SC_OP_INVALID)
  272. return -EIO;
  273. ath9k_ps_wakeup(sc);
  274. /*
  275. * This is only performed if the channel settings have
  276. * actually changed.
  277. *
  278. * To switch channels clear any pending DMA operations;
  279. * wait long enough for the RX fifo to drain, reset the
  280. * hardware at the new frequency, and then re-enable
  281. * the relevant bits of the h/w.
  282. */
  283. ath9k_hw_set_interrupts(ah, 0);
  284. ath_drain_all_txq(sc, false);
  285. stopped = ath_stoprecv(sc);
  286. /* XXX: do not flush receive queue here. We don't want
  287. * to flush data frames already in queue because of
  288. * changing channel. */
  289. if (!stopped || (sc->sc_flags & SC_OP_FULL_RESET))
  290. fastcc = false;
  291. ath_print(common, ATH_DBG_CONFIG,
  292. "(%u MHz) -> (%u MHz), chanwidth: %d\n",
  293. sc->sc_ah->curchan->channel,
  294. channel->center_freq, sc->tx_chan_width);
  295. spin_lock_bh(&sc->sc_resetlock);
  296. r = ath9k_hw_reset(ah, hchan, fastcc);
  297. if (r) {
  298. ath_print(common, ATH_DBG_FATAL,
  299. "Unable to reset channel (%u Mhz) "
  300. "reset status %d\n",
  301. channel->center_freq, r);
  302. spin_unlock_bh(&sc->sc_resetlock);
  303. goto ps_restore;
  304. }
  305. spin_unlock_bh(&sc->sc_resetlock);
  306. sc->sc_flags &= ~SC_OP_FULL_RESET;
  307. if (ath_startrecv(sc) != 0) {
  308. ath_print(common, ATH_DBG_FATAL,
  309. "Unable to restart recv logic\n");
  310. r = -EIO;
  311. goto ps_restore;
  312. }
  313. ath_cache_conf_rate(sc, &hw->conf);
  314. ath_update_txpow(sc);
  315. ath9k_hw_set_interrupts(ah, sc->imask);
  316. ps_restore:
  317. ath9k_ps_restore(sc);
  318. return r;
  319. }
  320. /*
  321. * This routine performs the periodic noise floor calibration function
  322. * that is used to adjust and optimize the chip performance. This
  323. * takes environmental changes (location, temperature) into account.
  324. * When the task is complete, it reschedules itself depending on the
  325. * appropriate interval that was calculated.
  326. */
  327. static void ath_ani_calibrate(unsigned long data)
  328. {
  329. struct ath_softc *sc = (struct ath_softc *)data;
  330. struct ath_hw *ah = sc->sc_ah;
  331. struct ath_common *common = ath9k_hw_common(ah);
  332. bool longcal = false;
  333. bool shortcal = false;
  334. bool aniflag = false;
  335. unsigned int timestamp = jiffies_to_msecs(jiffies);
  336. u32 cal_interval, short_cal_interval;
  337. short_cal_interval = (ah->opmode == NL80211_IFTYPE_AP) ?
  338. ATH_AP_SHORT_CALINTERVAL : ATH_STA_SHORT_CALINTERVAL;
  339. /*
  340. * don't calibrate when we're scanning.
  341. * we are most likely not on our home channel.
  342. */
  343. spin_lock(&sc->ani_lock);
  344. if (sc->sc_flags & SC_OP_SCANNING)
  345. goto set_timer;
  346. /* Only calibrate if awake */
  347. if (sc->sc_ah->power_mode != ATH9K_PM_AWAKE)
  348. goto set_timer;
  349. ath9k_ps_wakeup(sc);
  350. /* Long calibration runs independently of short calibration. */
  351. if ((timestamp - sc->ani.longcal_timer) >= ATH_LONG_CALINTERVAL) {
  352. longcal = true;
  353. ath_print(common, ATH_DBG_ANI, "longcal @%lu\n", jiffies);
  354. sc->ani.longcal_timer = timestamp;
  355. }
  356. /* Short calibration applies only while caldone is false */
  357. if (!sc->ani.caldone) {
  358. if ((timestamp - sc->ani.shortcal_timer) >= short_cal_interval) {
  359. shortcal = true;
  360. ath_print(common, ATH_DBG_ANI,
  361. "shortcal @%lu\n", jiffies);
  362. sc->ani.shortcal_timer = timestamp;
  363. sc->ani.resetcal_timer = timestamp;
  364. }
  365. } else {
  366. if ((timestamp - sc->ani.resetcal_timer) >=
  367. ATH_RESTART_CALINTERVAL) {
  368. sc->ani.caldone = ath9k_hw_reset_calvalid(ah);
  369. if (sc->ani.caldone)
  370. sc->ani.resetcal_timer = timestamp;
  371. }
  372. }
  373. /* Verify whether we must check ANI */
  374. if ((timestamp - sc->ani.checkani_timer) >= ATH_ANI_POLLINTERVAL) {
  375. aniflag = true;
  376. sc->ani.checkani_timer = timestamp;
  377. }
  378. /* Skip all processing if there's nothing to do. */
  379. if (longcal || shortcal || aniflag) {
  380. /* Call ANI routine if necessary */
  381. if (aniflag)
  382. ath9k_hw_ani_monitor(ah, ah->curchan);
  383. /* Perform calibration if necessary */
  384. if (longcal || shortcal) {
  385. sc->ani.caldone =
  386. ath9k_hw_calibrate(ah,
  387. ah->curchan,
  388. common->rx_chainmask,
  389. longcal);
  390. if (longcal)
  391. sc->ani.noise_floor = ath9k_hw_getchan_noise(ah,
  392. ah->curchan);
  393. ath_print(common, ATH_DBG_ANI,
  394. " calibrate chan %u/%x nf: %d\n",
  395. ah->curchan->channel,
  396. ah->curchan->channelFlags,
  397. sc->ani.noise_floor);
  398. }
  399. }
  400. ath9k_ps_restore(sc);
  401. set_timer:
  402. spin_unlock(&sc->ani_lock);
  403. /*
  404. * Set timer interval based on previous results.
  405. * The interval must be the shortest necessary to satisfy ANI,
  406. * short calibration and long calibration.
  407. */
  408. cal_interval = ATH_LONG_CALINTERVAL;
  409. if (sc->sc_ah->config.enable_ani)
  410. cal_interval = min(cal_interval, (u32)ATH_ANI_POLLINTERVAL);
  411. if (!sc->ani.caldone)
  412. cal_interval = min(cal_interval, (u32)short_cal_interval);
  413. mod_timer(&sc->ani.timer, jiffies + msecs_to_jiffies(cal_interval));
  414. }
  415. static void ath_start_ani(struct ath_softc *sc)
  416. {
  417. unsigned long timestamp = jiffies_to_msecs(jiffies);
  418. sc->ani.longcal_timer = timestamp;
  419. sc->ani.shortcal_timer = timestamp;
  420. sc->ani.checkani_timer = timestamp;
  421. mod_timer(&sc->ani.timer,
  422. jiffies + msecs_to_jiffies(ATH_ANI_POLLINTERVAL));
  423. }
  424. /*
  425. * Update tx/rx chainmask. For legacy association,
  426. * hard code chainmask to 1x1, for 11n association, use
  427. * the chainmask configuration, for bt coexistence, use
  428. * the chainmask configuration even in legacy mode.
  429. */
  430. void ath_update_chainmask(struct ath_softc *sc, int is_ht)
  431. {
  432. struct ath_hw *ah = sc->sc_ah;
  433. struct ath_common *common = ath9k_hw_common(ah);
  434. if ((sc->sc_flags & SC_OP_SCANNING) || is_ht ||
  435. (ah->btcoex_hw.scheme != ATH_BTCOEX_CFG_NONE)) {
  436. common->tx_chainmask = ah->caps.tx_chainmask;
  437. common->rx_chainmask = ah->caps.rx_chainmask;
  438. } else {
  439. common->tx_chainmask = 1;
  440. common->rx_chainmask = 1;
  441. }
  442. ath_print(common, ATH_DBG_CONFIG,
  443. "tx chmask: %d, rx chmask: %d\n",
  444. common->tx_chainmask,
  445. common->rx_chainmask);
  446. }
  447. static void ath_node_attach(struct ath_softc *sc, struct ieee80211_sta *sta)
  448. {
  449. struct ath_node *an;
  450. an = (struct ath_node *)sta->drv_priv;
  451. if (sc->sc_flags & SC_OP_TXAGGR) {
  452. ath_tx_node_init(sc, an);
  453. an->maxampdu = 1 << (IEEE80211_HT_MAX_AMPDU_FACTOR +
  454. sta->ht_cap.ampdu_factor);
  455. an->mpdudensity = parse_mpdudensity(sta->ht_cap.ampdu_density);
  456. an->last_rssi = ATH_RSSI_DUMMY_MARKER;
  457. }
  458. }
  459. static void ath_node_detach(struct ath_softc *sc, struct ieee80211_sta *sta)
  460. {
  461. struct ath_node *an = (struct ath_node *)sta->drv_priv;
  462. if (sc->sc_flags & SC_OP_TXAGGR)
  463. ath_tx_node_cleanup(sc, an);
  464. }
  465. static void ath9k_tasklet(unsigned long data)
  466. {
  467. struct ath_softc *sc = (struct ath_softc *)data;
  468. struct ath_hw *ah = sc->sc_ah;
  469. struct ath_common *common = ath9k_hw_common(ah);
  470. u32 status = sc->intrstatus;
  471. ath9k_ps_wakeup(sc);
  472. if (status & ATH9K_INT_FATAL) {
  473. ath_reset(sc, false);
  474. ath9k_ps_restore(sc);
  475. return;
  476. }
  477. if (status & (ATH9K_INT_RX | ATH9K_INT_RXEOL | ATH9K_INT_RXORN)) {
  478. spin_lock_bh(&sc->rx.rxflushlock);
  479. ath_rx_tasklet(sc, 0);
  480. spin_unlock_bh(&sc->rx.rxflushlock);
  481. }
  482. if (status & ATH9K_INT_TX)
  483. ath_tx_tasklet(sc);
  484. if ((status & ATH9K_INT_TSFOOR) && sc->ps_enabled) {
  485. /*
  486. * TSF sync does not look correct; remain awake to sync with
  487. * the next Beacon.
  488. */
  489. ath_print(common, ATH_DBG_PS,
  490. "TSFOOR - Sync with next Beacon\n");
  491. sc->sc_flags |= SC_OP_WAIT_FOR_BEACON | SC_OP_BEACON_SYNC;
  492. }
  493. if (ah->btcoex_hw.scheme == ATH_BTCOEX_CFG_3WIRE)
  494. if (status & ATH9K_INT_GENTIMER)
  495. ath_gen_timer_isr(sc->sc_ah);
  496. /* re-enable hardware interrupt */
  497. ath9k_hw_set_interrupts(ah, sc->imask);
  498. ath9k_ps_restore(sc);
  499. }
  500. irqreturn_t ath_isr(int irq, void *dev)
  501. {
  502. #define SCHED_INTR ( \
  503. ATH9K_INT_FATAL | \
  504. ATH9K_INT_RXORN | \
  505. ATH9K_INT_RXEOL | \
  506. ATH9K_INT_RX | \
  507. ATH9K_INT_TX | \
  508. ATH9K_INT_BMISS | \
  509. ATH9K_INT_CST | \
  510. ATH9K_INT_TSFOOR | \
  511. ATH9K_INT_GENTIMER)
  512. struct ath_softc *sc = dev;
  513. struct ath_hw *ah = sc->sc_ah;
  514. enum ath9k_int status;
  515. bool sched = false;
  516. /*
  517. * The hardware is not ready/present, don't
  518. * touch anything. Note this can happen early
  519. * on if the IRQ is shared.
  520. */
  521. if (sc->sc_flags & SC_OP_INVALID)
  522. return IRQ_NONE;
  523. /* shared irq, not for us */
  524. if (!ath9k_hw_intrpend(ah))
  525. return IRQ_NONE;
  526. /*
  527. * Figure out the reason(s) for the interrupt. Note
  528. * that the hal returns a pseudo-ISR that may include
  529. * bits we haven't explicitly enabled so we mask the
  530. * value to insure we only process bits we requested.
  531. */
  532. ath9k_hw_getisr(ah, &status); /* NB: clears ISR too */
  533. status &= sc->imask; /* discard unasked-for bits */
  534. /*
  535. * If there are no status bits set, then this interrupt was not
  536. * for me (should have been caught above).
  537. */
  538. if (!status)
  539. return IRQ_NONE;
  540. /* Cache the status */
  541. sc->intrstatus = status;
  542. if (status & SCHED_INTR)
  543. sched = true;
  544. /*
  545. * If a FATAL or RXORN interrupt is received, we have to reset the
  546. * chip immediately.
  547. */
  548. if (status & (ATH9K_INT_FATAL | ATH9K_INT_RXORN))
  549. goto chip_reset;
  550. if (status & ATH9K_INT_SWBA)
  551. tasklet_schedule(&sc->bcon_tasklet);
  552. if (status & ATH9K_INT_TXURN)
  553. ath9k_hw_updatetxtriglevel(ah, true);
  554. if (status & ATH9K_INT_MIB) {
  555. /*
  556. * Disable interrupts until we service the MIB
  557. * interrupt; otherwise it will continue to
  558. * fire.
  559. */
  560. ath9k_hw_set_interrupts(ah, 0);
  561. /*
  562. * Let the hal handle the event. We assume
  563. * it will clear whatever condition caused
  564. * the interrupt.
  565. */
  566. ath9k_hw_procmibevent(ah);
  567. ath9k_hw_set_interrupts(ah, sc->imask);
  568. }
  569. if (!(ah->caps.hw_caps & ATH9K_HW_CAP_AUTOSLEEP))
  570. if (status & ATH9K_INT_TIM_TIMER) {
  571. /* Clear RxAbort bit so that we can
  572. * receive frames */
  573. ath9k_setpower(sc, ATH9K_PM_AWAKE);
  574. ath9k_hw_setrxabort(sc->sc_ah, 0);
  575. sc->sc_flags |= SC_OP_WAIT_FOR_BEACON;
  576. }
  577. chip_reset:
  578. ath_debug_stat_interrupt(sc, status);
  579. if (sched) {
  580. /* turn off every interrupt except SWBA */
  581. ath9k_hw_set_interrupts(ah, (sc->imask & ATH9K_INT_SWBA));
  582. tasklet_schedule(&sc->intr_tq);
  583. }
  584. return IRQ_HANDLED;
  585. #undef SCHED_INTR
  586. }
  587. static u32 ath_get_extchanmode(struct ath_softc *sc,
  588. struct ieee80211_channel *chan,
  589. enum nl80211_channel_type channel_type)
  590. {
  591. u32 chanmode = 0;
  592. switch (chan->band) {
  593. case IEEE80211_BAND_2GHZ:
  594. switch(channel_type) {
  595. case NL80211_CHAN_NO_HT:
  596. case NL80211_CHAN_HT20:
  597. chanmode = CHANNEL_G_HT20;
  598. break;
  599. case NL80211_CHAN_HT40PLUS:
  600. chanmode = CHANNEL_G_HT40PLUS;
  601. break;
  602. case NL80211_CHAN_HT40MINUS:
  603. chanmode = CHANNEL_G_HT40MINUS;
  604. break;
  605. }
  606. break;
  607. case IEEE80211_BAND_5GHZ:
  608. switch(channel_type) {
  609. case NL80211_CHAN_NO_HT:
  610. case NL80211_CHAN_HT20:
  611. chanmode = CHANNEL_A_HT20;
  612. break;
  613. case NL80211_CHAN_HT40PLUS:
  614. chanmode = CHANNEL_A_HT40PLUS;
  615. break;
  616. case NL80211_CHAN_HT40MINUS:
  617. chanmode = CHANNEL_A_HT40MINUS;
  618. break;
  619. }
  620. break;
  621. default:
  622. break;
  623. }
  624. return chanmode;
  625. }
  626. static int ath_setkey_tkip(struct ath_softc *sc, u16 keyix, const u8 *key,
  627. struct ath9k_keyval *hk, const u8 *addr,
  628. bool authenticator)
  629. {
  630. const u8 *key_rxmic;
  631. const u8 *key_txmic;
  632. key_txmic = key + NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY;
  633. key_rxmic = key + NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY;
  634. if (addr == NULL) {
  635. /*
  636. * Group key installation - only two key cache entries are used
  637. * regardless of splitmic capability since group key is only
  638. * used either for TX or RX.
  639. */
  640. if (authenticator) {
  641. memcpy(hk->kv_mic, key_txmic, sizeof(hk->kv_mic));
  642. memcpy(hk->kv_txmic, key_txmic, sizeof(hk->kv_mic));
  643. } else {
  644. memcpy(hk->kv_mic, key_rxmic, sizeof(hk->kv_mic));
  645. memcpy(hk->kv_txmic, key_rxmic, sizeof(hk->kv_mic));
  646. }
  647. return ath9k_hw_set_keycache_entry(sc->sc_ah, keyix, hk, addr);
  648. }
  649. if (!sc->splitmic) {
  650. /* TX and RX keys share the same key cache entry. */
  651. memcpy(hk->kv_mic, key_rxmic, sizeof(hk->kv_mic));
  652. memcpy(hk->kv_txmic, key_txmic, sizeof(hk->kv_txmic));
  653. return ath9k_hw_set_keycache_entry(sc->sc_ah, keyix, hk, addr);
  654. }
  655. /* Separate key cache entries for TX and RX */
  656. /* TX key goes at first index, RX key at +32. */
  657. memcpy(hk->kv_mic, key_txmic, sizeof(hk->kv_mic));
  658. if (!ath9k_hw_set_keycache_entry(sc->sc_ah, keyix, hk, NULL)) {
  659. /* TX MIC entry failed. No need to proceed further */
  660. ath_print(ath9k_hw_common(sc->sc_ah), ATH_DBG_FATAL,
  661. "Setting TX MIC Key Failed\n");
  662. return 0;
  663. }
  664. memcpy(hk->kv_mic, key_rxmic, sizeof(hk->kv_mic));
  665. /* XXX delete tx key on failure? */
  666. return ath9k_hw_set_keycache_entry(sc->sc_ah, keyix + 32, hk, addr);
  667. }
  668. static int ath_reserve_key_cache_slot_tkip(struct ath_softc *sc)
  669. {
  670. int i;
  671. for (i = IEEE80211_WEP_NKID; i < sc->keymax / 2; i++) {
  672. if (test_bit(i, sc->keymap) ||
  673. test_bit(i + 64, sc->keymap))
  674. continue; /* At least one part of TKIP key allocated */
  675. if (sc->splitmic &&
  676. (test_bit(i + 32, sc->keymap) ||
  677. test_bit(i + 64 + 32, sc->keymap)))
  678. continue; /* At least one part of TKIP key allocated */
  679. /* Found a free slot for a TKIP key */
  680. return i;
  681. }
  682. return -1;
  683. }
  684. static int ath_reserve_key_cache_slot(struct ath_softc *sc)
  685. {
  686. int i;
  687. /* First, try to find slots that would not be available for TKIP. */
  688. if (sc->splitmic) {
  689. for (i = IEEE80211_WEP_NKID; i < sc->keymax / 4; i++) {
  690. if (!test_bit(i, sc->keymap) &&
  691. (test_bit(i + 32, sc->keymap) ||
  692. test_bit(i + 64, sc->keymap) ||
  693. test_bit(i + 64 + 32, sc->keymap)))
  694. return i;
  695. if (!test_bit(i + 32, sc->keymap) &&
  696. (test_bit(i, sc->keymap) ||
  697. test_bit(i + 64, sc->keymap) ||
  698. test_bit(i + 64 + 32, sc->keymap)))
  699. return i + 32;
  700. if (!test_bit(i + 64, sc->keymap) &&
  701. (test_bit(i , sc->keymap) ||
  702. test_bit(i + 32, sc->keymap) ||
  703. test_bit(i + 64 + 32, sc->keymap)))
  704. return i + 64;
  705. if (!test_bit(i + 64 + 32, sc->keymap) &&
  706. (test_bit(i, sc->keymap) ||
  707. test_bit(i + 32, sc->keymap) ||
  708. test_bit(i + 64, sc->keymap)))
  709. return i + 64 + 32;
  710. }
  711. } else {
  712. for (i = IEEE80211_WEP_NKID; i < sc->keymax / 2; i++) {
  713. if (!test_bit(i, sc->keymap) &&
  714. test_bit(i + 64, sc->keymap))
  715. return i;
  716. if (test_bit(i, sc->keymap) &&
  717. !test_bit(i + 64, sc->keymap))
  718. return i + 64;
  719. }
  720. }
  721. /* No partially used TKIP slots, pick any available slot */
  722. for (i = IEEE80211_WEP_NKID; i < sc->keymax; i++) {
  723. /* Do not allow slots that could be needed for TKIP group keys
  724. * to be used. This limitation could be removed if we know that
  725. * TKIP will not be used. */
  726. if (i >= 64 && i < 64 + IEEE80211_WEP_NKID)
  727. continue;
  728. if (sc->splitmic) {
  729. if (i >= 32 && i < 32 + IEEE80211_WEP_NKID)
  730. continue;
  731. if (i >= 64 + 32 && i < 64 + 32 + IEEE80211_WEP_NKID)
  732. continue;
  733. }
  734. if (!test_bit(i, sc->keymap))
  735. return i; /* Found a free slot for a key */
  736. }
  737. /* No free slot found */
  738. return -1;
  739. }
  740. static int ath_key_config(struct ath_softc *sc,
  741. struct ieee80211_vif *vif,
  742. struct ieee80211_sta *sta,
  743. struct ieee80211_key_conf *key)
  744. {
  745. struct ath9k_keyval hk;
  746. const u8 *mac = NULL;
  747. int ret = 0;
  748. int idx;
  749. memset(&hk, 0, sizeof(hk));
  750. switch (key->alg) {
  751. case ALG_WEP:
  752. hk.kv_type = ATH9K_CIPHER_WEP;
  753. break;
  754. case ALG_TKIP:
  755. hk.kv_type = ATH9K_CIPHER_TKIP;
  756. break;
  757. case ALG_CCMP:
  758. hk.kv_type = ATH9K_CIPHER_AES_CCM;
  759. break;
  760. default:
  761. return -EOPNOTSUPP;
  762. }
  763. hk.kv_len = key->keylen;
  764. memcpy(hk.kv_val, key->key, key->keylen);
  765. if (!(key->flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
  766. /* For now, use the default keys for broadcast keys. This may
  767. * need to change with virtual interfaces. */
  768. idx = key->keyidx;
  769. } else if (key->keyidx) {
  770. if (WARN_ON(!sta))
  771. return -EOPNOTSUPP;
  772. mac = sta->addr;
  773. if (vif->type != NL80211_IFTYPE_AP) {
  774. /* Only keyidx 0 should be used with unicast key, but
  775. * allow this for client mode for now. */
  776. idx = key->keyidx;
  777. } else
  778. return -EIO;
  779. } else {
  780. if (WARN_ON(!sta))
  781. return -EOPNOTSUPP;
  782. mac = sta->addr;
  783. if (key->alg == ALG_TKIP)
  784. idx = ath_reserve_key_cache_slot_tkip(sc);
  785. else
  786. idx = ath_reserve_key_cache_slot(sc);
  787. if (idx < 0)
  788. return -ENOSPC; /* no free key cache entries */
  789. }
  790. if (key->alg == ALG_TKIP)
  791. ret = ath_setkey_tkip(sc, idx, key->key, &hk, mac,
  792. vif->type == NL80211_IFTYPE_AP);
  793. else
  794. ret = ath9k_hw_set_keycache_entry(sc->sc_ah, idx, &hk, mac);
  795. if (!ret)
  796. return -EIO;
  797. set_bit(idx, sc->keymap);
  798. if (key->alg == ALG_TKIP) {
  799. set_bit(idx + 64, sc->keymap);
  800. if (sc->splitmic) {
  801. set_bit(idx + 32, sc->keymap);
  802. set_bit(idx + 64 + 32, sc->keymap);
  803. }
  804. }
  805. return idx;
  806. }
  807. static void ath_key_delete(struct ath_softc *sc, struct ieee80211_key_conf *key)
  808. {
  809. ath9k_hw_keyreset(sc->sc_ah, key->hw_key_idx);
  810. if (key->hw_key_idx < IEEE80211_WEP_NKID)
  811. return;
  812. clear_bit(key->hw_key_idx, sc->keymap);
  813. if (key->alg != ALG_TKIP)
  814. return;
  815. clear_bit(key->hw_key_idx + 64, sc->keymap);
  816. if (sc->splitmic) {
  817. clear_bit(key->hw_key_idx + 32, sc->keymap);
  818. clear_bit(key->hw_key_idx + 64 + 32, sc->keymap);
  819. }
  820. }
  821. static void setup_ht_cap(struct ath_softc *sc,
  822. struct ieee80211_sta_ht_cap *ht_info)
  823. {
  824. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  825. u8 tx_streams, rx_streams;
  826. ht_info->ht_supported = true;
  827. ht_info->cap = IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
  828. IEEE80211_HT_CAP_SM_PS |
  829. IEEE80211_HT_CAP_SGI_40 |
  830. IEEE80211_HT_CAP_DSSSCCK40;
  831. ht_info->ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
  832. ht_info->ampdu_density = IEEE80211_HT_MPDU_DENSITY_8;
  833. /* set up supported mcs set */
  834. memset(&ht_info->mcs, 0, sizeof(ht_info->mcs));
  835. tx_streams = !(common->tx_chainmask & (common->tx_chainmask - 1)) ?
  836. 1 : 2;
  837. rx_streams = !(common->rx_chainmask & (common->rx_chainmask - 1)) ?
  838. 1 : 2;
  839. if (tx_streams != rx_streams) {
  840. ath_print(common, ATH_DBG_CONFIG,
  841. "TX streams %d, RX streams: %d\n",
  842. tx_streams, rx_streams);
  843. ht_info->mcs.tx_params |= IEEE80211_HT_MCS_TX_RX_DIFF;
  844. ht_info->mcs.tx_params |= ((tx_streams - 1) <<
  845. IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT);
  846. }
  847. ht_info->mcs.rx_mask[0] = 0xff;
  848. if (rx_streams >= 2)
  849. ht_info->mcs.rx_mask[1] = 0xff;
  850. ht_info->mcs.tx_params |= IEEE80211_HT_MCS_TX_DEFINED;
  851. }
  852. static void ath9k_bss_assoc_info(struct ath_softc *sc,
  853. struct ieee80211_vif *vif,
  854. struct ieee80211_bss_conf *bss_conf)
  855. {
  856. struct ath_hw *ah = sc->sc_ah;
  857. struct ath_common *common = ath9k_hw_common(ah);
  858. if (bss_conf->assoc) {
  859. ath_print(common, ATH_DBG_CONFIG,
  860. "Bss Info ASSOC %d, bssid: %pM\n",
  861. bss_conf->aid, common->curbssid);
  862. /* New association, store aid */
  863. common->curaid = bss_conf->aid;
  864. ath9k_hw_write_associd(ah);
  865. /*
  866. * Request a re-configuration of Beacon related timers
  867. * on the receipt of the first Beacon frame (i.e.,
  868. * after time sync with the AP).
  869. */
  870. sc->sc_flags |= SC_OP_BEACON_SYNC;
  871. /* Configure the beacon */
  872. ath_beacon_config(sc, vif);
  873. /* Reset rssi stats */
  874. sc->sc_ah->stats.avgbrssi = ATH_RSSI_DUMMY_MARKER;
  875. ath_start_ani(sc);
  876. } else {
  877. ath_print(common, ATH_DBG_CONFIG, "Bss Info DISASSOC\n");
  878. common->curaid = 0;
  879. /* Stop ANI */
  880. del_timer_sync(&sc->ani.timer);
  881. }
  882. }
  883. /********************************/
  884. /* LED functions */
  885. /********************************/
  886. static void ath_led_blink_work(struct work_struct *work)
  887. {
  888. struct ath_softc *sc = container_of(work, struct ath_softc,
  889. ath_led_blink_work.work);
  890. if (!(sc->sc_flags & SC_OP_LED_ASSOCIATED))
  891. return;
  892. if ((sc->led_on_duration == ATH_LED_ON_DURATION_IDLE) ||
  893. (sc->led_off_duration == ATH_LED_OFF_DURATION_IDLE))
  894. ath9k_hw_set_gpio(sc->sc_ah, sc->sc_ah->led_pin, 0);
  895. else
  896. ath9k_hw_set_gpio(sc->sc_ah, sc->sc_ah->led_pin,
  897. (sc->sc_flags & SC_OP_LED_ON) ? 1 : 0);
  898. ieee80211_queue_delayed_work(sc->hw,
  899. &sc->ath_led_blink_work,
  900. (sc->sc_flags & SC_OP_LED_ON) ?
  901. msecs_to_jiffies(sc->led_off_duration) :
  902. msecs_to_jiffies(sc->led_on_duration));
  903. sc->led_on_duration = sc->led_on_cnt ?
  904. max((ATH_LED_ON_DURATION_IDLE - sc->led_on_cnt), 25) :
  905. ATH_LED_ON_DURATION_IDLE;
  906. sc->led_off_duration = sc->led_off_cnt ?
  907. max((ATH_LED_OFF_DURATION_IDLE - sc->led_off_cnt), 10) :
  908. ATH_LED_OFF_DURATION_IDLE;
  909. sc->led_on_cnt = sc->led_off_cnt = 0;
  910. if (sc->sc_flags & SC_OP_LED_ON)
  911. sc->sc_flags &= ~SC_OP_LED_ON;
  912. else
  913. sc->sc_flags |= SC_OP_LED_ON;
  914. }
  915. static void ath_led_brightness(struct led_classdev *led_cdev,
  916. enum led_brightness brightness)
  917. {
  918. struct ath_led *led = container_of(led_cdev, struct ath_led, led_cdev);
  919. struct ath_softc *sc = led->sc;
  920. switch (brightness) {
  921. case LED_OFF:
  922. if (led->led_type == ATH_LED_ASSOC ||
  923. led->led_type == ATH_LED_RADIO) {
  924. ath9k_hw_set_gpio(sc->sc_ah, sc->sc_ah->led_pin,
  925. (led->led_type == ATH_LED_RADIO));
  926. sc->sc_flags &= ~SC_OP_LED_ASSOCIATED;
  927. if (led->led_type == ATH_LED_RADIO)
  928. sc->sc_flags &= ~SC_OP_LED_ON;
  929. } else {
  930. sc->led_off_cnt++;
  931. }
  932. break;
  933. case LED_FULL:
  934. if (led->led_type == ATH_LED_ASSOC) {
  935. sc->sc_flags |= SC_OP_LED_ASSOCIATED;
  936. ieee80211_queue_delayed_work(sc->hw,
  937. &sc->ath_led_blink_work, 0);
  938. } else if (led->led_type == ATH_LED_RADIO) {
  939. ath9k_hw_set_gpio(sc->sc_ah, sc->sc_ah->led_pin, 0);
  940. sc->sc_flags |= SC_OP_LED_ON;
  941. } else {
  942. sc->led_on_cnt++;
  943. }
  944. break;
  945. default:
  946. break;
  947. }
  948. }
  949. static int ath_register_led(struct ath_softc *sc, struct ath_led *led,
  950. char *trigger)
  951. {
  952. int ret;
  953. led->sc = sc;
  954. led->led_cdev.name = led->name;
  955. led->led_cdev.default_trigger = trigger;
  956. led->led_cdev.brightness_set = ath_led_brightness;
  957. ret = led_classdev_register(wiphy_dev(sc->hw->wiphy), &led->led_cdev);
  958. if (ret)
  959. ath_print(ath9k_hw_common(sc->sc_ah), ATH_DBG_FATAL,
  960. "Failed to register led:%s", led->name);
  961. else
  962. led->registered = 1;
  963. return ret;
  964. }
  965. static void ath_unregister_led(struct ath_led *led)
  966. {
  967. if (led->registered) {
  968. led_classdev_unregister(&led->led_cdev);
  969. led->registered = 0;
  970. }
  971. }
  972. static void ath_deinit_leds(struct ath_softc *sc)
  973. {
  974. ath_unregister_led(&sc->assoc_led);
  975. sc->sc_flags &= ~SC_OP_LED_ASSOCIATED;
  976. ath_unregister_led(&sc->tx_led);
  977. ath_unregister_led(&sc->rx_led);
  978. ath_unregister_led(&sc->radio_led);
  979. ath9k_hw_set_gpio(sc->sc_ah, sc->sc_ah->led_pin, 1);
  980. }
  981. static void ath_init_leds(struct ath_softc *sc)
  982. {
  983. char *trigger;
  984. int ret;
  985. if (AR_SREV_9287(sc->sc_ah))
  986. sc->sc_ah->led_pin = ATH_LED_PIN_9287;
  987. else
  988. sc->sc_ah->led_pin = ATH_LED_PIN_DEF;
  989. /* Configure gpio 1 for output */
  990. ath9k_hw_cfg_output(sc->sc_ah, sc->sc_ah->led_pin,
  991. AR_GPIO_OUTPUT_MUX_AS_OUTPUT);
  992. /* LED off, active low */
  993. ath9k_hw_set_gpio(sc->sc_ah, sc->sc_ah->led_pin, 1);
  994. INIT_DELAYED_WORK(&sc->ath_led_blink_work, ath_led_blink_work);
  995. trigger = ieee80211_get_radio_led_name(sc->hw);
  996. snprintf(sc->radio_led.name, sizeof(sc->radio_led.name),
  997. "ath9k-%s::radio", wiphy_name(sc->hw->wiphy));
  998. ret = ath_register_led(sc, &sc->radio_led, trigger);
  999. sc->radio_led.led_type = ATH_LED_RADIO;
  1000. if (ret)
  1001. goto fail;
  1002. trigger = ieee80211_get_assoc_led_name(sc->hw);
  1003. snprintf(sc->assoc_led.name, sizeof(sc->assoc_led.name),
  1004. "ath9k-%s::assoc", wiphy_name(sc->hw->wiphy));
  1005. ret = ath_register_led(sc, &sc->assoc_led, trigger);
  1006. sc->assoc_led.led_type = ATH_LED_ASSOC;
  1007. if (ret)
  1008. goto fail;
  1009. trigger = ieee80211_get_tx_led_name(sc->hw);
  1010. snprintf(sc->tx_led.name, sizeof(sc->tx_led.name),
  1011. "ath9k-%s::tx", wiphy_name(sc->hw->wiphy));
  1012. ret = ath_register_led(sc, &sc->tx_led, trigger);
  1013. sc->tx_led.led_type = ATH_LED_TX;
  1014. if (ret)
  1015. goto fail;
  1016. trigger = ieee80211_get_rx_led_name(sc->hw);
  1017. snprintf(sc->rx_led.name, sizeof(sc->rx_led.name),
  1018. "ath9k-%s::rx", wiphy_name(sc->hw->wiphy));
  1019. ret = ath_register_led(sc, &sc->rx_led, trigger);
  1020. sc->rx_led.led_type = ATH_LED_RX;
  1021. if (ret)
  1022. goto fail;
  1023. return;
  1024. fail:
  1025. cancel_delayed_work_sync(&sc->ath_led_blink_work);
  1026. ath_deinit_leds(sc);
  1027. }
  1028. void ath_radio_enable(struct ath_softc *sc)
  1029. {
  1030. struct ath_hw *ah = sc->sc_ah;
  1031. struct ath_common *common = ath9k_hw_common(ah);
  1032. struct ieee80211_channel *channel = sc->hw->conf.channel;
  1033. int r;
  1034. ath9k_ps_wakeup(sc);
  1035. ath9k_hw_configpcipowersave(ah, 0, 0);
  1036. if (!ah->curchan)
  1037. ah->curchan = ath_get_curchannel(sc, sc->hw);
  1038. spin_lock_bh(&sc->sc_resetlock);
  1039. r = ath9k_hw_reset(ah, ah->curchan, false);
  1040. if (r) {
  1041. ath_print(common, ATH_DBG_FATAL,
  1042. "Unable to reset channel %u (%uMhz) ",
  1043. "reset status %d\n",
  1044. channel->center_freq, r);
  1045. }
  1046. spin_unlock_bh(&sc->sc_resetlock);
  1047. ath_update_txpow(sc);
  1048. if (ath_startrecv(sc) != 0) {
  1049. ath_print(common, ATH_DBG_FATAL,
  1050. "Unable to restart recv logic\n");
  1051. return;
  1052. }
  1053. if (sc->sc_flags & SC_OP_BEACONS)
  1054. ath_beacon_config(sc, NULL); /* restart beacons */
  1055. /* Re-Enable interrupts */
  1056. ath9k_hw_set_interrupts(ah, sc->imask);
  1057. /* Enable LED */
  1058. ath9k_hw_cfg_output(ah, ah->led_pin,
  1059. AR_GPIO_OUTPUT_MUX_AS_OUTPUT);
  1060. ath9k_hw_set_gpio(ah, ah->led_pin, 0);
  1061. ieee80211_wake_queues(sc->hw);
  1062. ath9k_ps_restore(sc);
  1063. }
  1064. void ath_radio_disable(struct ath_softc *sc)
  1065. {
  1066. struct ath_hw *ah = sc->sc_ah;
  1067. struct ieee80211_channel *channel = sc->hw->conf.channel;
  1068. int r;
  1069. ath9k_ps_wakeup(sc);
  1070. ieee80211_stop_queues(sc->hw);
  1071. /* Disable LED */
  1072. ath9k_hw_set_gpio(ah, ah->led_pin, 1);
  1073. ath9k_hw_cfg_gpio_input(ah, ah->led_pin);
  1074. /* Disable interrupts */
  1075. ath9k_hw_set_interrupts(ah, 0);
  1076. ath_drain_all_txq(sc, false); /* clear pending tx frames */
  1077. ath_stoprecv(sc); /* turn off frame recv */
  1078. ath_flushrecv(sc); /* flush recv queue */
  1079. if (!ah->curchan)
  1080. ah->curchan = ath_get_curchannel(sc, sc->hw);
  1081. spin_lock_bh(&sc->sc_resetlock);
  1082. r = ath9k_hw_reset(ah, ah->curchan, false);
  1083. if (r) {
  1084. ath_print(ath9k_hw_common(sc->sc_ah), ATH_DBG_FATAL,
  1085. "Unable to reset channel %u (%uMhz) "
  1086. "reset status %d\n",
  1087. channel->center_freq, r);
  1088. }
  1089. spin_unlock_bh(&sc->sc_resetlock);
  1090. ath9k_hw_phy_disable(ah);
  1091. ath9k_hw_configpcipowersave(ah, 1, 1);
  1092. ath9k_ps_restore(sc);
  1093. ath9k_setpower(sc, ATH9K_PM_FULL_SLEEP);
  1094. }
  1095. /*******************/
  1096. /* Rfkill */
  1097. /*******************/
  1098. static bool ath_is_rfkill_set(struct ath_softc *sc)
  1099. {
  1100. struct ath_hw *ah = sc->sc_ah;
  1101. return ath9k_hw_gpio_get(ah, ah->rfkill_gpio) ==
  1102. ah->rfkill_polarity;
  1103. }
  1104. static void ath9k_rfkill_poll_state(struct ieee80211_hw *hw)
  1105. {
  1106. struct ath_wiphy *aphy = hw->priv;
  1107. struct ath_softc *sc = aphy->sc;
  1108. bool blocked = !!ath_is_rfkill_set(sc);
  1109. wiphy_rfkill_set_hw_state(hw->wiphy, blocked);
  1110. }
  1111. static void ath_start_rfkill_poll(struct ath_softc *sc)
  1112. {
  1113. struct ath_hw *ah = sc->sc_ah;
  1114. if (ah->caps.hw_caps & ATH9K_HW_CAP_RFSILENT)
  1115. wiphy_rfkill_start_polling(sc->hw->wiphy);
  1116. }
  1117. void ath_cleanup(struct ath_softc *sc)
  1118. {
  1119. ath_detach(sc);
  1120. free_irq(sc->irq, sc);
  1121. ath_bus_cleanup(sc);
  1122. kfree(sc->sec_wiphy);
  1123. ieee80211_free_hw(sc->hw);
  1124. }
  1125. void ath_detach(struct ath_softc *sc)
  1126. {
  1127. struct ieee80211_hw *hw = sc->hw;
  1128. struct ath_hw *ah = sc->sc_ah;
  1129. int i = 0;
  1130. ath9k_ps_wakeup(sc);
  1131. dev_dbg(sc->dev, "Detach ATH hw\n");
  1132. ath_deinit_leds(sc);
  1133. wiphy_rfkill_stop_polling(sc->hw->wiphy);
  1134. for (i = 0; i < sc->num_sec_wiphy; i++) {
  1135. struct ath_wiphy *aphy = sc->sec_wiphy[i];
  1136. if (aphy == NULL)
  1137. continue;
  1138. sc->sec_wiphy[i] = NULL;
  1139. ieee80211_unregister_hw(aphy->hw);
  1140. ieee80211_free_hw(aphy->hw);
  1141. }
  1142. ieee80211_unregister_hw(hw);
  1143. ath_rx_cleanup(sc);
  1144. ath_tx_cleanup(sc);
  1145. tasklet_kill(&sc->intr_tq);
  1146. tasklet_kill(&sc->bcon_tasklet);
  1147. if (!(sc->sc_flags & SC_OP_INVALID))
  1148. ath9k_setpower(sc, ATH9K_PM_AWAKE);
  1149. /* cleanup tx queues */
  1150. for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++)
  1151. if (ATH_TXQ_SETUP(sc, i))
  1152. ath_tx_cleanupq(sc, &sc->tx.txq[i]);
  1153. if ((sc->btcoex.no_stomp_timer) &&
  1154. ah->btcoex_hw.scheme == ATH_BTCOEX_CFG_3WIRE)
  1155. ath_gen_timer_free(ah, sc->btcoex.no_stomp_timer);
  1156. ath9k_hw_detach(ah);
  1157. ath9k_exit_debug(ah);
  1158. sc->sc_ah = NULL;
  1159. }
  1160. static int ath9k_reg_notifier(struct wiphy *wiphy,
  1161. struct regulatory_request *request)
  1162. {
  1163. struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
  1164. struct ath_wiphy *aphy = hw->priv;
  1165. struct ath_softc *sc = aphy->sc;
  1166. struct ath_regulatory *reg = ath9k_hw_regulatory(sc->sc_ah);
  1167. return ath_reg_notifier_apply(wiphy, request, reg);
  1168. }
  1169. /*
  1170. * Detects if there is any priority bt traffic
  1171. */
  1172. static void ath_detect_bt_priority(struct ath_softc *sc)
  1173. {
  1174. struct ath_btcoex *btcoex = &sc->btcoex;
  1175. struct ath_hw *ah = sc->sc_ah;
  1176. if (ath9k_hw_gpio_get(sc->sc_ah, ah->btcoex_hw.btpriority_gpio))
  1177. btcoex->bt_priority_cnt++;
  1178. if (time_after(jiffies, btcoex->bt_priority_time +
  1179. msecs_to_jiffies(ATH_BT_PRIORITY_TIME_THRESHOLD))) {
  1180. if (btcoex->bt_priority_cnt >= ATH_BT_CNT_THRESHOLD) {
  1181. ath_print(ath9k_hw_common(sc->sc_ah), ATH_DBG_BTCOEX,
  1182. "BT priority traffic detected");
  1183. sc->sc_flags |= SC_OP_BT_PRIORITY_DETECTED;
  1184. } else {
  1185. sc->sc_flags &= ~SC_OP_BT_PRIORITY_DETECTED;
  1186. }
  1187. btcoex->bt_priority_cnt = 0;
  1188. btcoex->bt_priority_time = jiffies;
  1189. }
  1190. }
  1191. /*
  1192. * Configures appropriate weight based on stomp type.
  1193. */
  1194. static void ath9k_btcoex_bt_stomp(struct ath_softc *sc,
  1195. enum ath_stomp_type stomp_type)
  1196. {
  1197. struct ath_hw *ah = sc->sc_ah;
  1198. switch (stomp_type) {
  1199. case ATH_BTCOEX_STOMP_ALL:
  1200. ath9k_hw_btcoex_set_weight(ah, AR_BT_COEX_WGHT,
  1201. AR_STOMP_ALL_WLAN_WGHT);
  1202. break;
  1203. case ATH_BTCOEX_STOMP_LOW:
  1204. ath9k_hw_btcoex_set_weight(ah, AR_BT_COEX_WGHT,
  1205. AR_STOMP_LOW_WLAN_WGHT);
  1206. break;
  1207. case ATH_BTCOEX_STOMP_NONE:
  1208. ath9k_hw_btcoex_set_weight(ah, AR_BT_COEX_WGHT,
  1209. AR_STOMP_NONE_WLAN_WGHT);
  1210. break;
  1211. default:
  1212. ath_print(ath9k_hw_common(ah), ATH_DBG_BTCOEX,
  1213. "Invalid Stomptype\n");
  1214. break;
  1215. }
  1216. ath9k_hw_btcoex_enable(ah);
  1217. }
  1218. static void ath9k_gen_timer_start(struct ath_hw *ah,
  1219. struct ath_gen_timer *timer,
  1220. u32 timer_next,
  1221. u32 timer_period)
  1222. {
  1223. ath9k_hw_gen_timer_start(ah, timer, timer_next, timer_period);
  1224. if ((ah->ah_sc->imask & ATH9K_INT_GENTIMER) == 0) {
  1225. ath9k_hw_set_interrupts(ah, 0);
  1226. ah->ah_sc->imask |= ATH9K_INT_GENTIMER;
  1227. ath9k_hw_set_interrupts(ah, ah->ah_sc->imask);
  1228. }
  1229. }
  1230. static void ath9k_gen_timer_stop(struct ath_hw *ah, struct ath_gen_timer *timer)
  1231. {
  1232. struct ath_gen_timer_table *timer_table = &ah->hw_gen_timers;
  1233. ath9k_hw_gen_timer_stop(ah, timer);
  1234. /* if no timer is enabled, turn off interrupt mask */
  1235. if (timer_table->timer_mask.val == 0) {
  1236. ath9k_hw_set_interrupts(ah, 0);
  1237. ah->ah_sc->imask &= ~ATH9K_INT_GENTIMER;
  1238. ath9k_hw_set_interrupts(ah, ah->ah_sc->imask);
  1239. }
  1240. }
  1241. /*
  1242. * This is the master bt coex timer which runs for every
  1243. * 45ms, bt traffic will be given priority during 55% of this
  1244. * period while wlan gets remaining 45%
  1245. */
  1246. static void ath_btcoex_period_timer(unsigned long data)
  1247. {
  1248. struct ath_softc *sc = (struct ath_softc *) data;
  1249. struct ath_hw *ah = sc->sc_ah;
  1250. struct ath_btcoex *btcoex = &sc->btcoex;
  1251. ath_detect_bt_priority(sc);
  1252. spin_lock_bh(&btcoex->btcoex_lock);
  1253. ath9k_btcoex_bt_stomp(sc, btcoex->bt_stomp_type);
  1254. spin_unlock_bh(&btcoex->btcoex_lock);
  1255. if (btcoex->btcoex_period != btcoex->btcoex_no_stomp) {
  1256. if (btcoex->hw_timer_enabled)
  1257. ath9k_gen_timer_stop(ah, btcoex->no_stomp_timer);
  1258. ath9k_gen_timer_start(ah,
  1259. btcoex->no_stomp_timer,
  1260. (ath9k_hw_gettsf32(ah) +
  1261. btcoex->btcoex_no_stomp),
  1262. btcoex->btcoex_no_stomp * 10);
  1263. btcoex->hw_timer_enabled = true;
  1264. }
  1265. mod_timer(&btcoex->period_timer, jiffies +
  1266. msecs_to_jiffies(ATH_BTCOEX_DEF_BT_PERIOD));
  1267. }
  1268. /*
  1269. * Generic tsf based hw timer which configures weight
  1270. * registers to time slice between wlan and bt traffic
  1271. */
  1272. static void ath_btcoex_no_stomp_timer(void *arg)
  1273. {
  1274. struct ath_softc *sc = (struct ath_softc *)arg;
  1275. struct ath_hw *ah = sc->sc_ah;
  1276. struct ath_btcoex *btcoex = &sc->btcoex;
  1277. ath_print(ath9k_hw_common(ah), ATH_DBG_BTCOEX,
  1278. "no stomp timer running \n");
  1279. spin_lock_bh(&btcoex->btcoex_lock);
  1280. if (btcoex->bt_stomp_type == ATH_BTCOEX_STOMP_LOW)
  1281. ath9k_btcoex_bt_stomp(sc, ATH_BTCOEX_STOMP_NONE);
  1282. else if (btcoex->bt_stomp_type == ATH_BTCOEX_STOMP_ALL)
  1283. ath9k_btcoex_bt_stomp(sc, ATH_BTCOEX_STOMP_LOW);
  1284. spin_unlock_bh(&btcoex->btcoex_lock);
  1285. }
  1286. static int ath_init_btcoex_timer(struct ath_softc *sc)
  1287. {
  1288. struct ath_btcoex *btcoex = &sc->btcoex;
  1289. btcoex->btcoex_period = ATH_BTCOEX_DEF_BT_PERIOD * 1000;
  1290. btcoex->btcoex_no_stomp = (100 - ATH_BTCOEX_DEF_DUTY_CYCLE) *
  1291. btcoex->btcoex_period / 100;
  1292. setup_timer(&btcoex->period_timer, ath_btcoex_period_timer,
  1293. (unsigned long) sc);
  1294. spin_lock_init(&btcoex->btcoex_lock);
  1295. btcoex->no_stomp_timer = ath_gen_timer_alloc(sc->sc_ah,
  1296. ath_btcoex_no_stomp_timer,
  1297. ath_btcoex_no_stomp_timer,
  1298. (void *) sc, AR_FIRST_NDP_TIMER);
  1299. if (!btcoex->no_stomp_timer)
  1300. return -ENOMEM;
  1301. return 0;
  1302. }
  1303. /*
  1304. * Read and write, they both share the same lock. We do this to serialize
  1305. * reads and writes on Atheros 802.11n PCI devices only. This is required
  1306. * as the FIFO on these devices can only accept sanely 2 requests. After
  1307. * that the device goes bananas. Serializing the reads/writes prevents this
  1308. * from happening.
  1309. */
  1310. static void ath9k_iowrite32(void *hw_priv, u32 val, u32 reg_offset)
  1311. {
  1312. struct ath_hw *ah = (struct ath_hw *) hw_priv;
  1313. if (ah->config.serialize_regmode == SER_REG_MODE_ON) {
  1314. unsigned long flags;
  1315. spin_lock_irqsave(&ah->ah_sc->sc_serial_rw, flags);
  1316. iowrite32(val, ah->ah_sc->mem + reg_offset);
  1317. spin_unlock_irqrestore(&ah->ah_sc->sc_serial_rw, flags);
  1318. } else
  1319. iowrite32(val, ah->ah_sc->mem + reg_offset);
  1320. }
  1321. static unsigned int ath9k_ioread32(void *hw_priv, u32 reg_offset)
  1322. {
  1323. struct ath_hw *ah = (struct ath_hw *) hw_priv;
  1324. u32 val;
  1325. if (ah->config.serialize_regmode == SER_REG_MODE_ON) {
  1326. unsigned long flags;
  1327. spin_lock_irqsave(&ah->ah_sc->sc_serial_rw, flags);
  1328. val = ioread32(ah->ah_sc->mem + reg_offset);
  1329. spin_unlock_irqrestore(&ah->ah_sc->sc_serial_rw, flags);
  1330. } else
  1331. val = ioread32(ah->ah_sc->mem + reg_offset);
  1332. return val;
  1333. }
  1334. static struct ath_ops ath9k_common_ops = {
  1335. .read = ath9k_ioread32,
  1336. .write = ath9k_iowrite32,
  1337. };
  1338. /*
  1339. * Initialize and fill ath_softc, ath_sofct is the
  1340. * "Software Carrier" struct. Historically it has existed
  1341. * to allow the separation between hardware specific
  1342. * variables (now in ath_hw) and driver specific variables.
  1343. */
  1344. static int ath_init_softc(u16 devid, struct ath_softc *sc, u16 subsysid)
  1345. {
  1346. struct ath_hw *ah = NULL;
  1347. struct ath_common *common;
  1348. int r = 0, i;
  1349. int csz = 0;
  1350. int qnum;
  1351. /* XXX: hardware will not be ready until ath_open() being called */
  1352. sc->sc_flags |= SC_OP_INVALID;
  1353. spin_lock_init(&sc->wiphy_lock);
  1354. spin_lock_init(&sc->sc_resetlock);
  1355. spin_lock_init(&sc->sc_serial_rw);
  1356. spin_lock_init(&sc->ani_lock);
  1357. spin_lock_init(&sc->sc_pm_lock);
  1358. mutex_init(&sc->mutex);
  1359. tasklet_init(&sc->intr_tq, ath9k_tasklet, (unsigned long)sc);
  1360. tasklet_init(&sc->bcon_tasklet, ath_beacon_tasklet,
  1361. (unsigned long)sc);
  1362. ah = kzalloc(sizeof(struct ath_hw), GFP_KERNEL);
  1363. if (!ah) {
  1364. r = -ENOMEM;
  1365. goto bad_no_ah;
  1366. }
  1367. ah->ah_sc = sc;
  1368. ah->hw_version.devid = devid;
  1369. ah->hw_version.subsysid = subsysid;
  1370. sc->sc_ah = ah;
  1371. common = ath9k_hw_common(ah);
  1372. common->ops = &ath9k_common_ops;
  1373. common->ah = ah;
  1374. common->hw = sc->hw;
  1375. /*
  1376. * Cache line size is used to size and align various
  1377. * structures used to communicate with the hardware.
  1378. */
  1379. ath_read_cachesize(sc, &csz);
  1380. /* XXX assert csz is non-zero */
  1381. common->cachelsz = csz << 2; /* convert to bytes */
  1382. if (ath9k_init_debug(ah) < 0)
  1383. dev_err(sc->dev, "Unable to create debugfs files\n");
  1384. r = ath9k_hw_init(ah);
  1385. if (r) {
  1386. ath_print(common, ATH_DBG_FATAL,
  1387. "Unable to initialize hardware; "
  1388. "initialization status: %d\n", r);
  1389. goto bad;
  1390. }
  1391. /* Get the hardware key cache size. */
  1392. sc->keymax = ah->caps.keycache_size;
  1393. if (sc->keymax > ATH_KEYMAX) {
  1394. ath_print(common, ATH_DBG_ANY,
  1395. "Warning, using only %u entries in %u key cache\n",
  1396. ATH_KEYMAX, sc->keymax);
  1397. sc->keymax = ATH_KEYMAX;
  1398. }
  1399. /*
  1400. * Reset the key cache since some parts do not
  1401. * reset the contents on initial power up.
  1402. */
  1403. for (i = 0; i < sc->keymax; i++)
  1404. ath9k_hw_keyreset(ah, (u16) i);
  1405. /* default to MONITOR mode */
  1406. sc->sc_ah->opmode = NL80211_IFTYPE_MONITOR;
  1407. /* Setup rate tables */
  1408. ath_rate_attach(sc);
  1409. ath_setup_rates(sc, IEEE80211_BAND_2GHZ);
  1410. ath_setup_rates(sc, IEEE80211_BAND_5GHZ);
  1411. /*
  1412. * Allocate hardware transmit queues: one queue for
  1413. * beacon frames and one data queue for each QoS
  1414. * priority. Note that the hal handles reseting
  1415. * these queues at the needed time.
  1416. */
  1417. sc->beacon.beaconq = ath_beaconq_setup(ah);
  1418. if (sc->beacon.beaconq == -1) {
  1419. ath_print(common, ATH_DBG_FATAL,
  1420. "Unable to setup a beacon xmit queue\n");
  1421. r = -EIO;
  1422. goto bad2;
  1423. }
  1424. sc->beacon.cabq = ath_txq_setup(sc, ATH9K_TX_QUEUE_CAB, 0);
  1425. if (sc->beacon.cabq == NULL) {
  1426. ath_print(common, ATH_DBG_FATAL,
  1427. "Unable to setup CAB xmit queue\n");
  1428. r = -EIO;
  1429. goto bad2;
  1430. }
  1431. sc->config.cabqReadytime = ATH_CABQ_READY_TIME;
  1432. ath_cabq_update(sc);
  1433. for (i = 0; i < ARRAY_SIZE(sc->tx.hwq_map); i++)
  1434. sc->tx.hwq_map[i] = -1;
  1435. /* Setup data queues */
  1436. /* NB: ensure BK queue is the lowest priority h/w queue */
  1437. if (!ath_tx_setup(sc, ATH9K_WME_AC_BK)) {
  1438. ath_print(common, ATH_DBG_FATAL,
  1439. "Unable to setup xmit queue for BK traffic\n");
  1440. r = -EIO;
  1441. goto bad2;
  1442. }
  1443. if (!ath_tx_setup(sc, ATH9K_WME_AC_BE)) {
  1444. ath_print(common, ATH_DBG_FATAL,
  1445. "Unable to setup xmit queue for BE traffic\n");
  1446. r = -EIO;
  1447. goto bad2;
  1448. }
  1449. if (!ath_tx_setup(sc, ATH9K_WME_AC_VI)) {
  1450. ath_print(common, ATH_DBG_FATAL,
  1451. "Unable to setup xmit queue for VI traffic\n");
  1452. r = -EIO;
  1453. goto bad2;
  1454. }
  1455. if (!ath_tx_setup(sc, ATH9K_WME_AC_VO)) {
  1456. ath_print(common, ATH_DBG_FATAL,
  1457. "Unable to setup xmit queue for VO traffic\n");
  1458. r = -EIO;
  1459. goto bad2;
  1460. }
  1461. /* Initializes the noise floor to a reasonable default value.
  1462. * Later on this will be updated during ANI processing. */
  1463. sc->ani.noise_floor = ATH_DEFAULT_NOISE_FLOOR;
  1464. setup_timer(&sc->ani.timer, ath_ani_calibrate, (unsigned long)sc);
  1465. if (ath9k_hw_getcapability(ah, ATH9K_CAP_CIPHER,
  1466. ATH9K_CIPHER_TKIP, NULL)) {
  1467. /*
  1468. * Whether we should enable h/w TKIP MIC.
  1469. * XXX: if we don't support WME TKIP MIC, then we wouldn't
  1470. * report WMM capable, so it's always safe to turn on
  1471. * TKIP MIC in this case.
  1472. */
  1473. ath9k_hw_setcapability(sc->sc_ah, ATH9K_CAP_TKIP_MIC,
  1474. 0, 1, NULL);
  1475. }
  1476. /*
  1477. * Check whether the separate key cache entries
  1478. * are required to handle both tx+rx MIC keys.
  1479. * With split mic keys the number of stations is limited
  1480. * to 27 otherwise 59.
  1481. */
  1482. if (ath9k_hw_getcapability(ah, ATH9K_CAP_CIPHER,
  1483. ATH9K_CIPHER_TKIP, NULL)
  1484. && ath9k_hw_getcapability(ah, ATH9K_CAP_CIPHER,
  1485. ATH9K_CIPHER_MIC, NULL)
  1486. && ath9k_hw_getcapability(ah, ATH9K_CAP_TKIP_SPLIT,
  1487. 0, NULL))
  1488. sc->splitmic = 1;
  1489. /* turn on mcast key search if possible */
  1490. if (!ath9k_hw_getcapability(ah, ATH9K_CAP_MCAST_KEYSRCH, 0, NULL))
  1491. (void)ath9k_hw_setcapability(ah, ATH9K_CAP_MCAST_KEYSRCH, 1,
  1492. 1, NULL);
  1493. sc->config.txpowlimit = ATH_TXPOWER_MAX;
  1494. /* 11n Capabilities */
  1495. if (ah->caps.hw_caps & ATH9K_HW_CAP_HT) {
  1496. sc->sc_flags |= SC_OP_TXAGGR;
  1497. sc->sc_flags |= SC_OP_RXAGGR;
  1498. }
  1499. common->tx_chainmask = ah->caps.tx_chainmask;
  1500. common->rx_chainmask = ah->caps.rx_chainmask;
  1501. ath9k_hw_setcapability(ah, ATH9K_CAP_DIVERSITY, 1, true, NULL);
  1502. sc->rx.defant = ath9k_hw_getdefantenna(ah);
  1503. if (ah->caps.hw_caps & ATH9K_HW_CAP_BSSIDMASK)
  1504. memcpy(common->bssidmask, ath_bcast_mac, ETH_ALEN);
  1505. sc->beacon.slottime = ATH9K_SLOT_TIME_9; /* default to short slot time */
  1506. /* initialize beacon slots */
  1507. for (i = 0; i < ARRAY_SIZE(sc->beacon.bslot); i++) {
  1508. sc->beacon.bslot[i] = NULL;
  1509. sc->beacon.bslot_aphy[i] = NULL;
  1510. }
  1511. /* setup channels and rates */
  1512. sc->sbands[IEEE80211_BAND_2GHZ].channels = ath9k_2ghz_chantable;
  1513. sc->sbands[IEEE80211_BAND_2GHZ].bitrates =
  1514. sc->rates[IEEE80211_BAND_2GHZ];
  1515. sc->sbands[IEEE80211_BAND_2GHZ].band = IEEE80211_BAND_2GHZ;
  1516. sc->sbands[IEEE80211_BAND_2GHZ].n_channels =
  1517. ARRAY_SIZE(ath9k_2ghz_chantable);
  1518. if (test_bit(ATH9K_MODE_11A, sc->sc_ah->caps.wireless_modes)) {
  1519. sc->sbands[IEEE80211_BAND_5GHZ].channels = ath9k_5ghz_chantable;
  1520. sc->sbands[IEEE80211_BAND_5GHZ].bitrates =
  1521. sc->rates[IEEE80211_BAND_5GHZ];
  1522. sc->sbands[IEEE80211_BAND_5GHZ].band = IEEE80211_BAND_5GHZ;
  1523. sc->sbands[IEEE80211_BAND_5GHZ].n_channels =
  1524. ARRAY_SIZE(ath9k_5ghz_chantable);
  1525. }
  1526. switch (ah->btcoex_hw.scheme) {
  1527. case ATH_BTCOEX_CFG_NONE:
  1528. break;
  1529. case ATH_BTCOEX_CFG_2WIRE:
  1530. ath9k_hw_btcoex_init_2wire(ah);
  1531. break;
  1532. case ATH_BTCOEX_CFG_3WIRE:
  1533. ath9k_hw_btcoex_init_3wire(ah);
  1534. r = ath_init_btcoex_timer(sc);
  1535. if (r)
  1536. goto bad2;
  1537. qnum = ath_tx_get_qnum(sc, ATH9K_TX_QUEUE_DATA, ATH9K_WME_AC_BE);
  1538. ath9k_hw_init_btcoex_hw(ah, qnum);
  1539. sc->btcoex.bt_stomp_type = ATH_BTCOEX_STOMP_LOW;
  1540. break;
  1541. default:
  1542. WARN_ON(1);
  1543. break;
  1544. }
  1545. return 0;
  1546. bad2:
  1547. /* cleanup tx queues */
  1548. for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++)
  1549. if (ATH_TXQ_SETUP(sc, i))
  1550. ath_tx_cleanupq(sc, &sc->tx.txq[i]);
  1551. bad:
  1552. ath9k_hw_detach(ah);
  1553. bad_no_ah:
  1554. ath9k_exit_debug(sc->sc_ah);
  1555. sc->sc_ah = NULL;
  1556. return r;
  1557. }
  1558. void ath_set_hw_capab(struct ath_softc *sc, struct ieee80211_hw *hw)
  1559. {
  1560. hw->flags = IEEE80211_HW_RX_INCLUDES_FCS |
  1561. IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING |
  1562. IEEE80211_HW_SIGNAL_DBM |
  1563. IEEE80211_HW_AMPDU_AGGREGATION |
  1564. IEEE80211_HW_SUPPORTS_PS |
  1565. IEEE80211_HW_PS_NULLFUNC_STACK |
  1566. IEEE80211_HW_SPECTRUM_MGMT;
  1567. if (AR_SREV_9160_10_OR_LATER(sc->sc_ah) || modparam_nohwcrypt)
  1568. hw->flags |= IEEE80211_HW_MFP_CAPABLE;
  1569. hw->wiphy->interface_modes =
  1570. BIT(NL80211_IFTYPE_AP) |
  1571. BIT(NL80211_IFTYPE_STATION) |
  1572. BIT(NL80211_IFTYPE_ADHOC) |
  1573. BIT(NL80211_IFTYPE_MESH_POINT);
  1574. hw->queues = 4;
  1575. hw->max_rates = 4;
  1576. hw->channel_change_time = 5000;
  1577. hw->max_listen_interval = 10;
  1578. /* Hardware supports 10 but we use 4 */
  1579. hw->max_rate_tries = 4;
  1580. hw->sta_data_size = sizeof(struct ath_node);
  1581. hw->vif_data_size = sizeof(struct ath_vif);
  1582. hw->rate_control_algorithm = "ath9k_rate_control";
  1583. hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
  1584. &sc->sbands[IEEE80211_BAND_2GHZ];
  1585. if (test_bit(ATH9K_MODE_11A, sc->sc_ah->caps.wireless_modes))
  1586. hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
  1587. &sc->sbands[IEEE80211_BAND_5GHZ];
  1588. }
  1589. /* Device driver core initialization */
  1590. int ath_init_device(u16 devid, struct ath_softc *sc, u16 subsysid)
  1591. {
  1592. struct ieee80211_hw *hw = sc->hw;
  1593. struct ath_common *common;
  1594. struct ath_hw *ah;
  1595. int error = 0, i;
  1596. struct ath_regulatory *reg;
  1597. dev_dbg(sc->dev, "Attach ATH hw\n");
  1598. error = ath_init_softc(devid, sc, subsysid);
  1599. if (error != 0)
  1600. return error;
  1601. ah = sc->sc_ah;
  1602. common = ath9k_hw_common(ah);
  1603. /* get mac address from hardware and set in mac80211 */
  1604. SET_IEEE80211_PERM_ADDR(hw, common->macaddr);
  1605. ath_set_hw_capab(sc, hw);
  1606. error = ath_regd_init(&common->regulatory, sc->hw->wiphy,
  1607. ath9k_reg_notifier);
  1608. if (error)
  1609. return error;
  1610. reg = &common->regulatory;
  1611. if (ah->caps.hw_caps & ATH9K_HW_CAP_HT) {
  1612. setup_ht_cap(sc, &sc->sbands[IEEE80211_BAND_2GHZ].ht_cap);
  1613. if (test_bit(ATH9K_MODE_11A, ah->caps.wireless_modes))
  1614. setup_ht_cap(sc, &sc->sbands[IEEE80211_BAND_5GHZ].ht_cap);
  1615. }
  1616. /* initialize tx/rx engine */
  1617. error = ath_tx_init(sc, ATH_TXBUF);
  1618. if (error != 0)
  1619. goto error_attach;
  1620. error = ath_rx_init(sc, ATH_RXBUF);
  1621. if (error != 0)
  1622. goto error_attach;
  1623. INIT_WORK(&sc->chan_work, ath9k_wiphy_chan_work);
  1624. INIT_DELAYED_WORK(&sc->wiphy_work, ath9k_wiphy_work);
  1625. sc->wiphy_scheduler_int = msecs_to_jiffies(500);
  1626. error = ieee80211_register_hw(hw);
  1627. if (!ath_is_world_regd(reg)) {
  1628. error = regulatory_hint(hw->wiphy, reg->alpha2);
  1629. if (error)
  1630. goto error_attach;
  1631. }
  1632. /* Initialize LED control */
  1633. ath_init_leds(sc);
  1634. ath_start_rfkill_poll(sc);
  1635. return 0;
  1636. error_attach:
  1637. /* cleanup tx queues */
  1638. for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++)
  1639. if (ATH_TXQ_SETUP(sc, i))
  1640. ath_tx_cleanupq(sc, &sc->tx.txq[i]);
  1641. ath9k_hw_detach(ah);
  1642. ath9k_exit_debug(ah);
  1643. sc->sc_ah = NULL;
  1644. return error;
  1645. }
  1646. int ath_reset(struct ath_softc *sc, bool retry_tx)
  1647. {
  1648. struct ath_hw *ah = sc->sc_ah;
  1649. struct ath_common *common = ath9k_hw_common(ah);
  1650. struct ieee80211_hw *hw = sc->hw;
  1651. int r;
  1652. ath9k_hw_set_interrupts(ah, 0);
  1653. ath_drain_all_txq(sc, retry_tx);
  1654. ath_stoprecv(sc);
  1655. ath_flushrecv(sc);
  1656. spin_lock_bh(&sc->sc_resetlock);
  1657. r = ath9k_hw_reset(ah, sc->sc_ah->curchan, false);
  1658. if (r)
  1659. ath_print(common, ATH_DBG_FATAL,
  1660. "Unable to reset hardware; reset status %d\n", r);
  1661. spin_unlock_bh(&sc->sc_resetlock);
  1662. if (ath_startrecv(sc) != 0)
  1663. ath_print(common, ATH_DBG_FATAL,
  1664. "Unable to start recv logic\n");
  1665. /*
  1666. * We may be doing a reset in response to a request
  1667. * that changes the channel so update any state that
  1668. * might change as a result.
  1669. */
  1670. ath_cache_conf_rate(sc, &hw->conf);
  1671. ath_update_txpow(sc);
  1672. if (sc->sc_flags & SC_OP_BEACONS)
  1673. ath_beacon_config(sc, NULL); /* restart beacons */
  1674. ath9k_hw_set_interrupts(ah, sc->imask);
  1675. if (retry_tx) {
  1676. int i;
  1677. for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++) {
  1678. if (ATH_TXQ_SETUP(sc, i)) {
  1679. spin_lock_bh(&sc->tx.txq[i].axq_lock);
  1680. ath_txq_schedule(sc, &sc->tx.txq[i]);
  1681. spin_unlock_bh(&sc->tx.txq[i].axq_lock);
  1682. }
  1683. }
  1684. }
  1685. return r;
  1686. }
  1687. /*
  1688. * This function will allocate both the DMA descriptor structure, and the
  1689. * buffers it contains. These are used to contain the descriptors used
  1690. * by the system.
  1691. */
  1692. int ath_descdma_setup(struct ath_softc *sc, struct ath_descdma *dd,
  1693. struct list_head *head, const char *name,
  1694. int nbuf, int ndesc)
  1695. {
  1696. #define DS2PHYS(_dd, _ds) \
  1697. ((_dd)->dd_desc_paddr + ((caddr_t)(_ds) - (caddr_t)(_dd)->dd_desc))
  1698. #define ATH_DESC_4KB_BOUND_CHECK(_daddr) ((((_daddr) & 0xFFF) > 0xF7F) ? 1 : 0)
  1699. #define ATH_DESC_4KB_BOUND_NUM_SKIPPED(_len) ((_len) / 4096)
  1700. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  1701. struct ath_desc *ds;
  1702. struct ath_buf *bf;
  1703. int i, bsize, error;
  1704. ath_print(common, ATH_DBG_CONFIG, "%s DMA: %u buffers %u desc/buf\n",
  1705. name, nbuf, ndesc);
  1706. INIT_LIST_HEAD(head);
  1707. /* ath_desc must be a multiple of DWORDs */
  1708. if ((sizeof(struct ath_desc) % 4) != 0) {
  1709. ath_print(common, ATH_DBG_FATAL,
  1710. "ath_desc not DWORD aligned\n");
  1711. ASSERT((sizeof(struct ath_desc) % 4) == 0);
  1712. error = -ENOMEM;
  1713. goto fail;
  1714. }
  1715. dd->dd_desc_len = sizeof(struct ath_desc) * nbuf * ndesc;
  1716. /*
  1717. * Need additional DMA memory because we can't use
  1718. * descriptors that cross the 4K page boundary. Assume
  1719. * one skipped descriptor per 4K page.
  1720. */
  1721. if (!(sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_4KB_SPLITTRANS)) {
  1722. u32 ndesc_skipped =
  1723. ATH_DESC_4KB_BOUND_NUM_SKIPPED(dd->dd_desc_len);
  1724. u32 dma_len;
  1725. while (ndesc_skipped) {
  1726. dma_len = ndesc_skipped * sizeof(struct ath_desc);
  1727. dd->dd_desc_len += dma_len;
  1728. ndesc_skipped = ATH_DESC_4KB_BOUND_NUM_SKIPPED(dma_len);
  1729. };
  1730. }
  1731. /* allocate descriptors */
  1732. dd->dd_desc = dma_alloc_coherent(sc->dev, dd->dd_desc_len,
  1733. &dd->dd_desc_paddr, GFP_KERNEL);
  1734. if (dd->dd_desc == NULL) {
  1735. error = -ENOMEM;
  1736. goto fail;
  1737. }
  1738. ds = dd->dd_desc;
  1739. ath_print(common, ATH_DBG_CONFIG, "%s DMA map: %p (%u) -> %llx (%u)\n",
  1740. name, ds, (u32) dd->dd_desc_len,
  1741. ito64(dd->dd_desc_paddr), /*XXX*/(u32) dd->dd_desc_len);
  1742. /* allocate buffers */
  1743. bsize = sizeof(struct ath_buf) * nbuf;
  1744. bf = kzalloc(bsize, GFP_KERNEL);
  1745. if (bf == NULL) {
  1746. error = -ENOMEM;
  1747. goto fail2;
  1748. }
  1749. dd->dd_bufptr = bf;
  1750. for (i = 0; i < nbuf; i++, bf++, ds += ndesc) {
  1751. bf->bf_desc = ds;
  1752. bf->bf_daddr = DS2PHYS(dd, ds);
  1753. if (!(sc->sc_ah->caps.hw_caps &
  1754. ATH9K_HW_CAP_4KB_SPLITTRANS)) {
  1755. /*
  1756. * Skip descriptor addresses which can cause 4KB
  1757. * boundary crossing (addr + length) with a 32 dword
  1758. * descriptor fetch.
  1759. */
  1760. while (ATH_DESC_4KB_BOUND_CHECK(bf->bf_daddr)) {
  1761. ASSERT((caddr_t) bf->bf_desc <
  1762. ((caddr_t) dd->dd_desc +
  1763. dd->dd_desc_len));
  1764. ds += ndesc;
  1765. bf->bf_desc = ds;
  1766. bf->bf_daddr = DS2PHYS(dd, ds);
  1767. }
  1768. }
  1769. list_add_tail(&bf->list, head);
  1770. }
  1771. return 0;
  1772. fail2:
  1773. dma_free_coherent(sc->dev, dd->dd_desc_len, dd->dd_desc,
  1774. dd->dd_desc_paddr);
  1775. fail:
  1776. memset(dd, 0, sizeof(*dd));
  1777. return error;
  1778. #undef ATH_DESC_4KB_BOUND_CHECK
  1779. #undef ATH_DESC_4KB_BOUND_NUM_SKIPPED
  1780. #undef DS2PHYS
  1781. }
  1782. void ath_descdma_cleanup(struct ath_softc *sc,
  1783. struct ath_descdma *dd,
  1784. struct list_head *head)
  1785. {
  1786. dma_free_coherent(sc->dev, dd->dd_desc_len, dd->dd_desc,
  1787. dd->dd_desc_paddr);
  1788. INIT_LIST_HEAD(head);
  1789. kfree(dd->dd_bufptr);
  1790. memset(dd, 0, sizeof(*dd));
  1791. }
  1792. int ath_get_hal_qnum(u16 queue, struct ath_softc *sc)
  1793. {
  1794. int qnum;
  1795. switch (queue) {
  1796. case 0:
  1797. qnum = sc->tx.hwq_map[ATH9K_WME_AC_VO];
  1798. break;
  1799. case 1:
  1800. qnum = sc->tx.hwq_map[ATH9K_WME_AC_VI];
  1801. break;
  1802. case 2:
  1803. qnum = sc->tx.hwq_map[ATH9K_WME_AC_BE];
  1804. break;
  1805. case 3:
  1806. qnum = sc->tx.hwq_map[ATH9K_WME_AC_BK];
  1807. break;
  1808. default:
  1809. qnum = sc->tx.hwq_map[ATH9K_WME_AC_BE];
  1810. break;
  1811. }
  1812. return qnum;
  1813. }
  1814. int ath_get_mac80211_qnum(u32 queue, struct ath_softc *sc)
  1815. {
  1816. int qnum;
  1817. switch (queue) {
  1818. case ATH9K_WME_AC_VO:
  1819. qnum = 0;
  1820. break;
  1821. case ATH9K_WME_AC_VI:
  1822. qnum = 1;
  1823. break;
  1824. case ATH9K_WME_AC_BE:
  1825. qnum = 2;
  1826. break;
  1827. case ATH9K_WME_AC_BK:
  1828. qnum = 3;
  1829. break;
  1830. default:
  1831. qnum = -1;
  1832. break;
  1833. }
  1834. return qnum;
  1835. }
  1836. /* XXX: Remove me once we don't depend on ath9k_channel for all
  1837. * this redundant data */
  1838. void ath9k_update_ichannel(struct ath_softc *sc, struct ieee80211_hw *hw,
  1839. struct ath9k_channel *ichan)
  1840. {
  1841. struct ieee80211_channel *chan = hw->conf.channel;
  1842. struct ieee80211_conf *conf = &hw->conf;
  1843. ichan->channel = chan->center_freq;
  1844. ichan->chan = chan;
  1845. if (chan->band == IEEE80211_BAND_2GHZ) {
  1846. ichan->chanmode = CHANNEL_G;
  1847. ichan->channelFlags = CHANNEL_2GHZ | CHANNEL_OFDM | CHANNEL_G;
  1848. } else {
  1849. ichan->chanmode = CHANNEL_A;
  1850. ichan->channelFlags = CHANNEL_5GHZ | CHANNEL_OFDM;
  1851. }
  1852. sc->tx_chan_width = ATH9K_HT_MACMODE_20;
  1853. if (conf_is_ht(conf)) {
  1854. if (conf_is_ht40(conf))
  1855. sc->tx_chan_width = ATH9K_HT_MACMODE_2040;
  1856. ichan->chanmode = ath_get_extchanmode(sc, chan,
  1857. conf->channel_type);
  1858. }
  1859. }
  1860. /**********************/
  1861. /* mac80211 callbacks */
  1862. /**********************/
  1863. /*
  1864. * (Re)start btcoex timers
  1865. */
  1866. static void ath9k_btcoex_timer_resume(struct ath_softc *sc)
  1867. {
  1868. struct ath_btcoex *btcoex = &sc->btcoex;
  1869. struct ath_hw *ah = sc->sc_ah;
  1870. ath_print(ath9k_hw_common(ah), ATH_DBG_BTCOEX,
  1871. "Starting btcoex timers");
  1872. /* make sure duty cycle timer is also stopped when resuming */
  1873. if (btcoex->hw_timer_enabled)
  1874. ath9k_gen_timer_stop(sc->sc_ah, btcoex->no_stomp_timer);
  1875. btcoex->bt_priority_cnt = 0;
  1876. btcoex->bt_priority_time = jiffies;
  1877. sc->sc_flags &= ~SC_OP_BT_PRIORITY_DETECTED;
  1878. mod_timer(&btcoex->period_timer, jiffies);
  1879. }
  1880. static int ath9k_start(struct ieee80211_hw *hw)
  1881. {
  1882. struct ath_wiphy *aphy = hw->priv;
  1883. struct ath_softc *sc = aphy->sc;
  1884. struct ath_hw *ah = sc->sc_ah;
  1885. struct ath_common *common = ath9k_hw_common(ah);
  1886. struct ieee80211_channel *curchan = hw->conf.channel;
  1887. struct ath9k_channel *init_channel;
  1888. int r;
  1889. ath_print(common, ATH_DBG_CONFIG,
  1890. "Starting driver with initial channel: %d MHz\n",
  1891. curchan->center_freq);
  1892. mutex_lock(&sc->mutex);
  1893. if (ath9k_wiphy_started(sc)) {
  1894. if (sc->chan_idx == curchan->hw_value) {
  1895. /*
  1896. * Already on the operational channel, the new wiphy
  1897. * can be marked active.
  1898. */
  1899. aphy->state = ATH_WIPHY_ACTIVE;
  1900. ieee80211_wake_queues(hw);
  1901. } else {
  1902. /*
  1903. * Another wiphy is on another channel, start the new
  1904. * wiphy in paused state.
  1905. */
  1906. aphy->state = ATH_WIPHY_PAUSED;
  1907. ieee80211_stop_queues(hw);
  1908. }
  1909. mutex_unlock(&sc->mutex);
  1910. return 0;
  1911. }
  1912. aphy->state = ATH_WIPHY_ACTIVE;
  1913. /* setup initial channel */
  1914. sc->chan_idx = curchan->hw_value;
  1915. init_channel = ath_get_curchannel(sc, hw);
  1916. /* Reset SERDES registers */
  1917. ath9k_hw_configpcipowersave(ah, 0, 0);
  1918. /*
  1919. * The basic interface to setting the hardware in a good
  1920. * state is ``reset''. On return the hardware is known to
  1921. * be powered up and with interrupts disabled. This must
  1922. * be followed by initialization of the appropriate bits
  1923. * and then setup of the interrupt mask.
  1924. */
  1925. spin_lock_bh(&sc->sc_resetlock);
  1926. r = ath9k_hw_reset(ah, init_channel, false);
  1927. if (r) {
  1928. ath_print(common, ATH_DBG_FATAL,
  1929. "Unable to reset hardware; reset status %d "
  1930. "(freq %u MHz)\n", r,
  1931. curchan->center_freq);
  1932. spin_unlock_bh(&sc->sc_resetlock);
  1933. goto mutex_unlock;
  1934. }
  1935. spin_unlock_bh(&sc->sc_resetlock);
  1936. /*
  1937. * This is needed only to setup initial state
  1938. * but it's best done after a reset.
  1939. */
  1940. ath_update_txpow(sc);
  1941. /*
  1942. * Setup the hardware after reset:
  1943. * The receive engine is set going.
  1944. * Frame transmit is handled entirely
  1945. * in the frame output path; there's nothing to do
  1946. * here except setup the interrupt mask.
  1947. */
  1948. if (ath_startrecv(sc) != 0) {
  1949. ath_print(common, ATH_DBG_FATAL,
  1950. "Unable to start recv logic\n");
  1951. r = -EIO;
  1952. goto mutex_unlock;
  1953. }
  1954. /* Setup our intr mask. */
  1955. sc->imask = ATH9K_INT_RX | ATH9K_INT_TX
  1956. | ATH9K_INT_RXEOL | ATH9K_INT_RXORN
  1957. | ATH9K_INT_FATAL | ATH9K_INT_GLOBAL;
  1958. if (ah->caps.hw_caps & ATH9K_HW_CAP_GTT)
  1959. sc->imask |= ATH9K_INT_GTT;
  1960. if (ah->caps.hw_caps & ATH9K_HW_CAP_HT)
  1961. sc->imask |= ATH9K_INT_CST;
  1962. ath_cache_conf_rate(sc, &hw->conf);
  1963. sc->sc_flags &= ~SC_OP_INVALID;
  1964. /* Disable BMISS interrupt when we're not associated */
  1965. sc->imask &= ~(ATH9K_INT_SWBA | ATH9K_INT_BMISS);
  1966. ath9k_hw_set_interrupts(ah, sc->imask);
  1967. ieee80211_wake_queues(hw);
  1968. ieee80211_queue_delayed_work(sc->hw, &sc->tx_complete_work, 0);
  1969. if ((ah->btcoex_hw.scheme != ATH_BTCOEX_CFG_NONE) &&
  1970. !ah->btcoex_hw.enabled) {
  1971. ath9k_hw_btcoex_set_weight(ah, AR_BT_COEX_WGHT,
  1972. AR_STOMP_LOW_WLAN_WGHT);
  1973. ath9k_hw_btcoex_enable(ah);
  1974. if (sc->bus_ops->bt_coex_prep)
  1975. sc->bus_ops->bt_coex_prep(sc);
  1976. if (ah->btcoex_hw.scheme == ATH_BTCOEX_CFG_3WIRE)
  1977. ath9k_btcoex_timer_resume(sc);
  1978. }
  1979. mutex_unlock:
  1980. mutex_unlock(&sc->mutex);
  1981. return r;
  1982. }
  1983. static int ath9k_tx(struct ieee80211_hw *hw,
  1984. struct sk_buff *skb)
  1985. {
  1986. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1987. struct ath_wiphy *aphy = hw->priv;
  1988. struct ath_softc *sc = aphy->sc;
  1989. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  1990. struct ath_tx_control txctl;
  1991. int hdrlen, padsize;
  1992. if (aphy->state != ATH_WIPHY_ACTIVE && aphy->state != ATH_WIPHY_SCAN) {
  1993. ath_print(common, ATH_DBG_XMIT,
  1994. "ath9k: %s: TX in unexpected wiphy state "
  1995. "%d\n", wiphy_name(hw->wiphy), aphy->state);
  1996. goto exit;
  1997. }
  1998. if (sc->ps_enabled) {
  1999. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  2000. /*
  2001. * mac80211 does not set PM field for normal data frames, so we
  2002. * need to update that based on the current PS mode.
  2003. */
  2004. if (ieee80211_is_data(hdr->frame_control) &&
  2005. !ieee80211_is_nullfunc(hdr->frame_control) &&
  2006. !ieee80211_has_pm(hdr->frame_control)) {
  2007. ath_print(common, ATH_DBG_PS, "Add PM=1 for a TX frame "
  2008. "while in PS mode\n");
  2009. hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
  2010. }
  2011. }
  2012. if (unlikely(sc->sc_ah->power_mode != ATH9K_PM_AWAKE)) {
  2013. /*
  2014. * We are using PS-Poll and mac80211 can request TX while in
  2015. * power save mode. Need to wake up hardware for the TX to be
  2016. * completed and if needed, also for RX of buffered frames.
  2017. */
  2018. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  2019. ath9k_ps_wakeup(sc);
  2020. ath9k_hw_setrxabort(sc->sc_ah, 0);
  2021. if (ieee80211_is_pspoll(hdr->frame_control)) {
  2022. ath_print(common, ATH_DBG_PS,
  2023. "Sending PS-Poll to pick a buffered frame\n");
  2024. sc->sc_flags |= SC_OP_WAIT_FOR_PSPOLL_DATA;
  2025. } else {
  2026. ath_print(common, ATH_DBG_PS,
  2027. "Wake up to complete TX\n");
  2028. sc->sc_flags |= SC_OP_WAIT_FOR_TX_ACK;
  2029. }
  2030. /*
  2031. * The actual restore operation will happen only after
  2032. * the sc_flags bit is cleared. We are just dropping
  2033. * the ps_usecount here.
  2034. */
  2035. ath9k_ps_restore(sc);
  2036. }
  2037. memset(&txctl, 0, sizeof(struct ath_tx_control));
  2038. /*
  2039. * As a temporary workaround, assign seq# here; this will likely need
  2040. * to be cleaned up to work better with Beacon transmission and virtual
  2041. * BSSes.
  2042. */
  2043. if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
  2044. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  2045. if (info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT)
  2046. sc->tx.seq_no += 0x10;
  2047. hdr->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
  2048. hdr->seq_ctrl |= cpu_to_le16(sc->tx.seq_no);
  2049. }
  2050. /* Add the padding after the header if this is not already done */
  2051. hdrlen = ieee80211_get_hdrlen_from_skb(skb);
  2052. if (hdrlen & 3) {
  2053. padsize = hdrlen % 4;
  2054. if (skb_headroom(skb) < padsize)
  2055. return -1;
  2056. skb_push(skb, padsize);
  2057. memmove(skb->data, skb->data + padsize, hdrlen);
  2058. }
  2059. /* Check if a tx queue is available */
  2060. txctl.txq = ath_test_get_txq(sc, skb);
  2061. if (!txctl.txq)
  2062. goto exit;
  2063. ath_print(common, ATH_DBG_XMIT, "transmitting packet, skb: %p\n", skb);
  2064. if (ath_tx_start(hw, skb, &txctl) != 0) {
  2065. ath_print(common, ATH_DBG_XMIT, "TX failed\n");
  2066. goto exit;
  2067. }
  2068. return 0;
  2069. exit:
  2070. dev_kfree_skb_any(skb);
  2071. return 0;
  2072. }
  2073. /*
  2074. * Pause btcoex timer and bt duty cycle timer
  2075. */
  2076. static void ath9k_btcoex_timer_pause(struct ath_softc *sc)
  2077. {
  2078. struct ath_btcoex *btcoex = &sc->btcoex;
  2079. struct ath_hw *ah = sc->sc_ah;
  2080. del_timer_sync(&btcoex->period_timer);
  2081. if (btcoex->hw_timer_enabled)
  2082. ath9k_gen_timer_stop(ah, btcoex->no_stomp_timer);
  2083. btcoex->hw_timer_enabled = false;
  2084. }
  2085. static void ath9k_stop(struct ieee80211_hw *hw)
  2086. {
  2087. struct ath_wiphy *aphy = hw->priv;
  2088. struct ath_softc *sc = aphy->sc;
  2089. struct ath_hw *ah = sc->sc_ah;
  2090. struct ath_common *common = ath9k_hw_common(ah);
  2091. mutex_lock(&sc->mutex);
  2092. aphy->state = ATH_WIPHY_INACTIVE;
  2093. cancel_delayed_work_sync(&sc->ath_led_blink_work);
  2094. cancel_delayed_work_sync(&sc->tx_complete_work);
  2095. if (!sc->num_sec_wiphy) {
  2096. cancel_delayed_work_sync(&sc->wiphy_work);
  2097. cancel_work_sync(&sc->chan_work);
  2098. }
  2099. if (sc->sc_flags & SC_OP_INVALID) {
  2100. ath_print(common, ATH_DBG_ANY, "Device not present\n");
  2101. mutex_unlock(&sc->mutex);
  2102. return;
  2103. }
  2104. if (ath9k_wiphy_started(sc)) {
  2105. mutex_unlock(&sc->mutex);
  2106. return; /* another wiphy still in use */
  2107. }
  2108. if (ah->btcoex_hw.enabled) {
  2109. ath9k_hw_btcoex_disable(ah);
  2110. if (ah->btcoex_hw.scheme == ATH_BTCOEX_CFG_3WIRE)
  2111. ath9k_btcoex_timer_pause(sc);
  2112. }
  2113. /* make sure h/w will not generate any interrupt
  2114. * before setting the invalid flag. */
  2115. ath9k_hw_set_interrupts(ah, 0);
  2116. if (!(sc->sc_flags & SC_OP_INVALID)) {
  2117. ath_drain_all_txq(sc, false);
  2118. ath_stoprecv(sc);
  2119. ath9k_hw_phy_disable(ah);
  2120. } else
  2121. sc->rx.rxlink = NULL;
  2122. /* disable HAL and put h/w to sleep */
  2123. ath9k_hw_disable(ah);
  2124. ath9k_hw_configpcipowersave(ah, 1, 1);
  2125. ath9k_setpower(sc, ATH9K_PM_FULL_SLEEP);
  2126. sc->sc_flags |= SC_OP_INVALID;
  2127. mutex_unlock(&sc->mutex);
  2128. ath_print(common, ATH_DBG_CONFIG, "Driver halt\n");
  2129. }
  2130. static int ath9k_add_interface(struct ieee80211_hw *hw,
  2131. struct ieee80211_if_init_conf *conf)
  2132. {
  2133. struct ath_wiphy *aphy = hw->priv;
  2134. struct ath_softc *sc = aphy->sc;
  2135. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  2136. struct ath_vif *avp = (void *)conf->vif->drv_priv;
  2137. enum nl80211_iftype ic_opmode = NL80211_IFTYPE_UNSPECIFIED;
  2138. int ret = 0;
  2139. mutex_lock(&sc->mutex);
  2140. if (!(sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_BSSIDMASK) &&
  2141. sc->nvifs > 0) {
  2142. ret = -ENOBUFS;
  2143. goto out;
  2144. }
  2145. switch (conf->type) {
  2146. case NL80211_IFTYPE_STATION:
  2147. ic_opmode = NL80211_IFTYPE_STATION;
  2148. break;
  2149. case NL80211_IFTYPE_ADHOC:
  2150. case NL80211_IFTYPE_AP:
  2151. case NL80211_IFTYPE_MESH_POINT:
  2152. if (sc->nbcnvifs >= ATH_BCBUF) {
  2153. ret = -ENOBUFS;
  2154. goto out;
  2155. }
  2156. ic_opmode = conf->type;
  2157. break;
  2158. default:
  2159. ath_print(common, ATH_DBG_FATAL,
  2160. "Interface type %d not yet supported\n", conf->type);
  2161. ret = -EOPNOTSUPP;
  2162. goto out;
  2163. }
  2164. ath_print(common, ATH_DBG_CONFIG,
  2165. "Attach a VIF of type: %d\n", ic_opmode);
  2166. /* Set the VIF opmode */
  2167. avp->av_opmode = ic_opmode;
  2168. avp->av_bslot = -1;
  2169. sc->nvifs++;
  2170. if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_BSSIDMASK)
  2171. ath9k_set_bssid_mask(hw);
  2172. if (sc->nvifs > 1)
  2173. goto out; /* skip global settings for secondary vif */
  2174. if (ic_opmode == NL80211_IFTYPE_AP) {
  2175. ath9k_hw_set_tsfadjust(sc->sc_ah, 1);
  2176. sc->sc_flags |= SC_OP_TSF_RESET;
  2177. }
  2178. /* Set the device opmode */
  2179. sc->sc_ah->opmode = ic_opmode;
  2180. /*
  2181. * Enable MIB interrupts when there are hardware phy counters.
  2182. * Note we only do this (at the moment) for station mode.
  2183. */
  2184. if ((conf->type == NL80211_IFTYPE_STATION) ||
  2185. (conf->type == NL80211_IFTYPE_ADHOC) ||
  2186. (conf->type == NL80211_IFTYPE_MESH_POINT)) {
  2187. sc->imask |= ATH9K_INT_MIB;
  2188. sc->imask |= ATH9K_INT_TSFOOR;
  2189. }
  2190. ath9k_hw_set_interrupts(sc->sc_ah, sc->imask);
  2191. if (conf->type == NL80211_IFTYPE_AP ||
  2192. conf->type == NL80211_IFTYPE_ADHOC ||
  2193. conf->type == NL80211_IFTYPE_MONITOR)
  2194. ath_start_ani(sc);
  2195. out:
  2196. mutex_unlock(&sc->mutex);
  2197. return ret;
  2198. }
  2199. static void ath9k_remove_interface(struct ieee80211_hw *hw,
  2200. struct ieee80211_if_init_conf *conf)
  2201. {
  2202. struct ath_wiphy *aphy = hw->priv;
  2203. struct ath_softc *sc = aphy->sc;
  2204. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  2205. struct ath_vif *avp = (void *)conf->vif->drv_priv;
  2206. int i;
  2207. ath_print(common, ATH_DBG_CONFIG, "Detach Interface\n");
  2208. mutex_lock(&sc->mutex);
  2209. /* Stop ANI */
  2210. del_timer_sync(&sc->ani.timer);
  2211. /* Reclaim beacon resources */
  2212. if ((sc->sc_ah->opmode == NL80211_IFTYPE_AP) ||
  2213. (sc->sc_ah->opmode == NL80211_IFTYPE_ADHOC) ||
  2214. (sc->sc_ah->opmode == NL80211_IFTYPE_MESH_POINT)) {
  2215. ath9k_hw_stoptxdma(sc->sc_ah, sc->beacon.beaconq);
  2216. ath_beacon_return(sc, avp);
  2217. }
  2218. sc->sc_flags &= ~SC_OP_BEACONS;
  2219. for (i = 0; i < ARRAY_SIZE(sc->beacon.bslot); i++) {
  2220. if (sc->beacon.bslot[i] == conf->vif) {
  2221. printk(KERN_DEBUG "%s: vif had allocated beacon "
  2222. "slot\n", __func__);
  2223. sc->beacon.bslot[i] = NULL;
  2224. sc->beacon.bslot_aphy[i] = NULL;
  2225. }
  2226. }
  2227. sc->nvifs--;
  2228. mutex_unlock(&sc->mutex);
  2229. }
  2230. static int ath9k_config(struct ieee80211_hw *hw, u32 changed)
  2231. {
  2232. struct ath_wiphy *aphy = hw->priv;
  2233. struct ath_softc *sc = aphy->sc;
  2234. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  2235. struct ieee80211_conf *conf = &hw->conf;
  2236. struct ath_hw *ah = sc->sc_ah;
  2237. bool all_wiphys_idle = false, disable_radio = false;
  2238. mutex_lock(&sc->mutex);
  2239. /* Leave this as the first check */
  2240. if (changed & IEEE80211_CONF_CHANGE_IDLE) {
  2241. spin_lock_bh(&sc->wiphy_lock);
  2242. all_wiphys_idle = ath9k_all_wiphys_idle(sc);
  2243. spin_unlock_bh(&sc->wiphy_lock);
  2244. if (conf->flags & IEEE80211_CONF_IDLE){
  2245. if (all_wiphys_idle)
  2246. disable_radio = true;
  2247. }
  2248. else if (all_wiphys_idle) {
  2249. ath_radio_enable(sc);
  2250. ath_print(common, ATH_DBG_CONFIG,
  2251. "not-idle: enabling radio\n");
  2252. }
  2253. }
  2254. if (changed & IEEE80211_CONF_CHANGE_PS) {
  2255. if (conf->flags & IEEE80211_CONF_PS) {
  2256. if (!(ah->caps.hw_caps &
  2257. ATH9K_HW_CAP_AUTOSLEEP)) {
  2258. if ((sc->imask & ATH9K_INT_TIM_TIMER) == 0) {
  2259. sc->imask |= ATH9K_INT_TIM_TIMER;
  2260. ath9k_hw_set_interrupts(sc->sc_ah,
  2261. sc->imask);
  2262. }
  2263. ath9k_hw_setrxabort(sc->sc_ah, 1);
  2264. }
  2265. sc->ps_enabled = true;
  2266. } else {
  2267. sc->ps_enabled = false;
  2268. ath9k_setpower(sc, ATH9K_PM_AWAKE);
  2269. if (!(ah->caps.hw_caps &
  2270. ATH9K_HW_CAP_AUTOSLEEP)) {
  2271. ath9k_hw_setrxabort(sc->sc_ah, 0);
  2272. sc->sc_flags &= ~(SC_OP_WAIT_FOR_BEACON |
  2273. SC_OP_WAIT_FOR_CAB |
  2274. SC_OP_WAIT_FOR_PSPOLL_DATA |
  2275. SC_OP_WAIT_FOR_TX_ACK);
  2276. if (sc->imask & ATH9K_INT_TIM_TIMER) {
  2277. sc->imask &= ~ATH9K_INT_TIM_TIMER;
  2278. ath9k_hw_set_interrupts(sc->sc_ah,
  2279. sc->imask);
  2280. }
  2281. }
  2282. }
  2283. }
  2284. if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
  2285. struct ieee80211_channel *curchan = hw->conf.channel;
  2286. int pos = curchan->hw_value;
  2287. aphy->chan_idx = pos;
  2288. aphy->chan_is_ht = conf_is_ht(conf);
  2289. if (aphy->state == ATH_WIPHY_SCAN ||
  2290. aphy->state == ATH_WIPHY_ACTIVE)
  2291. ath9k_wiphy_pause_all_forced(sc, aphy);
  2292. else {
  2293. /*
  2294. * Do not change operational channel based on a paused
  2295. * wiphy changes.
  2296. */
  2297. goto skip_chan_change;
  2298. }
  2299. ath_print(common, ATH_DBG_CONFIG, "Set channel: %d MHz\n",
  2300. curchan->center_freq);
  2301. /* XXX: remove me eventualy */
  2302. ath9k_update_ichannel(sc, hw, &sc->sc_ah->channels[pos]);
  2303. ath_update_chainmask(sc, conf_is_ht(conf));
  2304. if (ath_set_channel(sc, hw, &sc->sc_ah->channels[pos]) < 0) {
  2305. ath_print(common, ATH_DBG_FATAL,
  2306. "Unable to set channel\n");
  2307. mutex_unlock(&sc->mutex);
  2308. return -EINVAL;
  2309. }
  2310. }
  2311. skip_chan_change:
  2312. if (changed & IEEE80211_CONF_CHANGE_POWER)
  2313. sc->config.txpowlimit = 2 * conf->power_level;
  2314. if (disable_radio) {
  2315. ath_print(common, ATH_DBG_CONFIG, "idle: disabling radio\n");
  2316. ath_radio_disable(sc);
  2317. }
  2318. mutex_unlock(&sc->mutex);
  2319. return 0;
  2320. }
  2321. #define SUPPORTED_FILTERS \
  2322. (FIF_PROMISC_IN_BSS | \
  2323. FIF_ALLMULTI | \
  2324. FIF_CONTROL | \
  2325. FIF_PSPOLL | \
  2326. FIF_OTHER_BSS | \
  2327. FIF_BCN_PRBRESP_PROMISC | \
  2328. FIF_FCSFAIL)
  2329. /* FIXME: sc->sc_full_reset ? */
  2330. static void ath9k_configure_filter(struct ieee80211_hw *hw,
  2331. unsigned int changed_flags,
  2332. unsigned int *total_flags,
  2333. u64 multicast)
  2334. {
  2335. struct ath_wiphy *aphy = hw->priv;
  2336. struct ath_softc *sc = aphy->sc;
  2337. u32 rfilt;
  2338. changed_flags &= SUPPORTED_FILTERS;
  2339. *total_flags &= SUPPORTED_FILTERS;
  2340. sc->rx.rxfilter = *total_flags;
  2341. ath9k_ps_wakeup(sc);
  2342. rfilt = ath_calcrxfilter(sc);
  2343. ath9k_hw_setrxfilter(sc->sc_ah, rfilt);
  2344. ath9k_ps_restore(sc);
  2345. ath_print(ath9k_hw_common(sc->sc_ah), ATH_DBG_CONFIG,
  2346. "Set HW RX filter: 0x%x\n", rfilt);
  2347. }
  2348. static void ath9k_sta_notify(struct ieee80211_hw *hw,
  2349. struct ieee80211_vif *vif,
  2350. enum sta_notify_cmd cmd,
  2351. struct ieee80211_sta *sta)
  2352. {
  2353. struct ath_wiphy *aphy = hw->priv;
  2354. struct ath_softc *sc = aphy->sc;
  2355. switch (cmd) {
  2356. case STA_NOTIFY_ADD:
  2357. ath_node_attach(sc, sta);
  2358. break;
  2359. case STA_NOTIFY_REMOVE:
  2360. ath_node_detach(sc, sta);
  2361. break;
  2362. default:
  2363. break;
  2364. }
  2365. }
  2366. static int ath9k_conf_tx(struct ieee80211_hw *hw, u16 queue,
  2367. const struct ieee80211_tx_queue_params *params)
  2368. {
  2369. struct ath_wiphy *aphy = hw->priv;
  2370. struct ath_softc *sc = aphy->sc;
  2371. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  2372. struct ath9k_tx_queue_info qi;
  2373. int ret = 0, qnum;
  2374. if (queue >= WME_NUM_AC)
  2375. return 0;
  2376. mutex_lock(&sc->mutex);
  2377. memset(&qi, 0, sizeof(struct ath9k_tx_queue_info));
  2378. qi.tqi_aifs = params->aifs;
  2379. qi.tqi_cwmin = params->cw_min;
  2380. qi.tqi_cwmax = params->cw_max;
  2381. qi.tqi_burstTime = params->txop;
  2382. qnum = ath_get_hal_qnum(queue, sc);
  2383. ath_print(common, ATH_DBG_CONFIG,
  2384. "Configure tx [queue/halq] [%d/%d], "
  2385. "aifs: %d, cw_min: %d, cw_max: %d, txop: %d\n",
  2386. queue, qnum, params->aifs, params->cw_min,
  2387. params->cw_max, params->txop);
  2388. ret = ath_txq_update(sc, qnum, &qi);
  2389. if (ret)
  2390. ath_print(common, ATH_DBG_FATAL, "TXQ Update failed\n");
  2391. mutex_unlock(&sc->mutex);
  2392. return ret;
  2393. }
  2394. static int ath9k_set_key(struct ieee80211_hw *hw,
  2395. enum set_key_cmd cmd,
  2396. struct ieee80211_vif *vif,
  2397. struct ieee80211_sta *sta,
  2398. struct ieee80211_key_conf *key)
  2399. {
  2400. struct ath_wiphy *aphy = hw->priv;
  2401. struct ath_softc *sc = aphy->sc;
  2402. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  2403. int ret = 0;
  2404. if (modparam_nohwcrypt)
  2405. return -ENOSPC;
  2406. mutex_lock(&sc->mutex);
  2407. ath9k_ps_wakeup(sc);
  2408. ath_print(common, ATH_DBG_CONFIG, "Set HW Key\n");
  2409. switch (cmd) {
  2410. case SET_KEY:
  2411. ret = ath_key_config(sc, vif, sta, key);
  2412. if (ret >= 0) {
  2413. key->hw_key_idx = ret;
  2414. /* push IV and Michael MIC generation to stack */
  2415. key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  2416. if (key->alg == ALG_TKIP)
  2417. key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
  2418. if (sc->sc_ah->sw_mgmt_crypto && key->alg == ALG_CCMP)
  2419. key->flags |= IEEE80211_KEY_FLAG_SW_MGMT;
  2420. ret = 0;
  2421. }
  2422. break;
  2423. case DISABLE_KEY:
  2424. ath_key_delete(sc, key);
  2425. break;
  2426. default:
  2427. ret = -EINVAL;
  2428. }
  2429. ath9k_ps_restore(sc);
  2430. mutex_unlock(&sc->mutex);
  2431. return ret;
  2432. }
  2433. static void ath9k_bss_info_changed(struct ieee80211_hw *hw,
  2434. struct ieee80211_vif *vif,
  2435. struct ieee80211_bss_conf *bss_conf,
  2436. u32 changed)
  2437. {
  2438. struct ath_wiphy *aphy = hw->priv;
  2439. struct ath_softc *sc = aphy->sc;
  2440. struct ath_hw *ah = sc->sc_ah;
  2441. struct ath_common *common = ath9k_hw_common(ah);
  2442. struct ath_vif *avp = (void *)vif->drv_priv;
  2443. u32 rfilt = 0;
  2444. int error, i;
  2445. mutex_lock(&sc->mutex);
  2446. /*
  2447. * TODO: Need to decide which hw opmode to use for
  2448. * multi-interface cases
  2449. * XXX: This belongs into add_interface!
  2450. */
  2451. if (vif->type == NL80211_IFTYPE_AP &&
  2452. ah->opmode != NL80211_IFTYPE_AP) {
  2453. ah->opmode = NL80211_IFTYPE_STATION;
  2454. ath9k_hw_setopmode(ah);
  2455. memcpy(common->curbssid, common->macaddr, ETH_ALEN);
  2456. common->curaid = 0;
  2457. ath9k_hw_write_associd(ah);
  2458. /* Request full reset to get hw opmode changed properly */
  2459. sc->sc_flags |= SC_OP_FULL_RESET;
  2460. }
  2461. if ((changed & BSS_CHANGED_BSSID) &&
  2462. !is_zero_ether_addr(bss_conf->bssid)) {
  2463. switch (vif->type) {
  2464. case NL80211_IFTYPE_STATION:
  2465. case NL80211_IFTYPE_ADHOC:
  2466. case NL80211_IFTYPE_MESH_POINT:
  2467. /* Set BSSID */
  2468. memcpy(common->curbssid, bss_conf->bssid, ETH_ALEN);
  2469. memcpy(avp->bssid, bss_conf->bssid, ETH_ALEN);
  2470. common->curaid = 0;
  2471. ath9k_hw_write_associd(ah);
  2472. /* Set aggregation protection mode parameters */
  2473. sc->config.ath_aggr_prot = 0;
  2474. ath_print(common, ATH_DBG_CONFIG,
  2475. "RX filter 0x%x bssid %pM aid 0x%x\n",
  2476. rfilt, common->curbssid, common->curaid);
  2477. /* need to reconfigure the beacon */
  2478. sc->sc_flags &= ~SC_OP_BEACONS ;
  2479. break;
  2480. default:
  2481. break;
  2482. }
  2483. }
  2484. if ((vif->type == NL80211_IFTYPE_ADHOC) ||
  2485. (vif->type == NL80211_IFTYPE_AP) ||
  2486. (vif->type == NL80211_IFTYPE_MESH_POINT)) {
  2487. if ((changed & BSS_CHANGED_BEACON) ||
  2488. (changed & BSS_CHANGED_BEACON_ENABLED &&
  2489. bss_conf->enable_beacon)) {
  2490. /*
  2491. * Allocate and setup the beacon frame.
  2492. *
  2493. * Stop any previous beacon DMA. This may be
  2494. * necessary, for example, when an ibss merge
  2495. * causes reconfiguration; we may be called
  2496. * with beacon transmission active.
  2497. */
  2498. ath9k_hw_stoptxdma(sc->sc_ah, sc->beacon.beaconq);
  2499. error = ath_beacon_alloc(aphy, vif);
  2500. if (!error)
  2501. ath_beacon_config(sc, vif);
  2502. }
  2503. }
  2504. /* Check for WLAN_CAPABILITY_PRIVACY ? */
  2505. if ((avp->av_opmode != NL80211_IFTYPE_STATION)) {
  2506. for (i = 0; i < IEEE80211_WEP_NKID; i++)
  2507. if (ath9k_hw_keyisvalid(sc->sc_ah, (u16)i))
  2508. ath9k_hw_keysetmac(sc->sc_ah,
  2509. (u16)i,
  2510. common->curbssid);
  2511. }
  2512. /* Only legacy IBSS for now */
  2513. if (vif->type == NL80211_IFTYPE_ADHOC)
  2514. ath_update_chainmask(sc, 0);
  2515. if (changed & BSS_CHANGED_ERP_PREAMBLE) {
  2516. ath_print(common, ATH_DBG_CONFIG, "BSS Changed PREAMBLE %d\n",
  2517. bss_conf->use_short_preamble);
  2518. if (bss_conf->use_short_preamble)
  2519. sc->sc_flags |= SC_OP_PREAMBLE_SHORT;
  2520. else
  2521. sc->sc_flags &= ~SC_OP_PREAMBLE_SHORT;
  2522. }
  2523. if (changed & BSS_CHANGED_ERP_CTS_PROT) {
  2524. ath_print(common, ATH_DBG_CONFIG, "BSS Changed CTS PROT %d\n",
  2525. bss_conf->use_cts_prot);
  2526. if (bss_conf->use_cts_prot &&
  2527. hw->conf.channel->band != IEEE80211_BAND_5GHZ)
  2528. sc->sc_flags |= SC_OP_PROTECT_ENABLE;
  2529. else
  2530. sc->sc_flags &= ~SC_OP_PROTECT_ENABLE;
  2531. }
  2532. if (changed & BSS_CHANGED_ASSOC) {
  2533. ath_print(common, ATH_DBG_CONFIG, "BSS Changed ASSOC %d\n",
  2534. bss_conf->assoc);
  2535. ath9k_bss_assoc_info(sc, vif, bss_conf);
  2536. }
  2537. /*
  2538. * The HW TSF has to be reset when the beacon interval changes.
  2539. * We set the flag here, and ath_beacon_config_ap() would take this
  2540. * into account when it gets called through the subsequent
  2541. * config_interface() call - with IFCC_BEACON in the changed field.
  2542. */
  2543. if (changed & BSS_CHANGED_BEACON_INT) {
  2544. sc->sc_flags |= SC_OP_TSF_RESET;
  2545. sc->beacon_interval = bss_conf->beacon_int;
  2546. }
  2547. mutex_unlock(&sc->mutex);
  2548. }
  2549. static u64 ath9k_get_tsf(struct ieee80211_hw *hw)
  2550. {
  2551. u64 tsf;
  2552. struct ath_wiphy *aphy = hw->priv;
  2553. struct ath_softc *sc = aphy->sc;
  2554. mutex_lock(&sc->mutex);
  2555. tsf = ath9k_hw_gettsf64(sc->sc_ah);
  2556. mutex_unlock(&sc->mutex);
  2557. return tsf;
  2558. }
  2559. static void ath9k_set_tsf(struct ieee80211_hw *hw, u64 tsf)
  2560. {
  2561. struct ath_wiphy *aphy = hw->priv;
  2562. struct ath_softc *sc = aphy->sc;
  2563. mutex_lock(&sc->mutex);
  2564. ath9k_hw_settsf64(sc->sc_ah, tsf);
  2565. mutex_unlock(&sc->mutex);
  2566. }
  2567. static void ath9k_reset_tsf(struct ieee80211_hw *hw)
  2568. {
  2569. struct ath_wiphy *aphy = hw->priv;
  2570. struct ath_softc *sc = aphy->sc;
  2571. mutex_lock(&sc->mutex);
  2572. ath9k_ps_wakeup(sc);
  2573. ath9k_hw_reset_tsf(sc->sc_ah);
  2574. ath9k_ps_restore(sc);
  2575. mutex_unlock(&sc->mutex);
  2576. }
  2577. static int ath9k_ampdu_action(struct ieee80211_hw *hw,
  2578. enum ieee80211_ampdu_mlme_action action,
  2579. struct ieee80211_sta *sta,
  2580. u16 tid, u16 *ssn)
  2581. {
  2582. struct ath_wiphy *aphy = hw->priv;
  2583. struct ath_softc *sc = aphy->sc;
  2584. int ret = 0;
  2585. switch (action) {
  2586. case IEEE80211_AMPDU_RX_START:
  2587. if (!(sc->sc_flags & SC_OP_RXAGGR))
  2588. ret = -ENOTSUPP;
  2589. break;
  2590. case IEEE80211_AMPDU_RX_STOP:
  2591. break;
  2592. case IEEE80211_AMPDU_TX_START:
  2593. ath_tx_aggr_start(sc, sta, tid, ssn);
  2594. ieee80211_start_tx_ba_cb_irqsafe(hw, sta->addr, tid);
  2595. break;
  2596. case IEEE80211_AMPDU_TX_STOP:
  2597. ath_tx_aggr_stop(sc, sta, tid);
  2598. ieee80211_stop_tx_ba_cb_irqsafe(hw, sta->addr, tid);
  2599. break;
  2600. case IEEE80211_AMPDU_TX_OPERATIONAL:
  2601. ath_tx_aggr_resume(sc, sta, tid);
  2602. break;
  2603. default:
  2604. ath_print(ath9k_hw_common(sc->sc_ah), ATH_DBG_FATAL,
  2605. "Unknown AMPDU action\n");
  2606. }
  2607. return ret;
  2608. }
  2609. static void ath9k_sw_scan_start(struct ieee80211_hw *hw)
  2610. {
  2611. struct ath_wiphy *aphy = hw->priv;
  2612. struct ath_softc *sc = aphy->sc;
  2613. mutex_lock(&sc->mutex);
  2614. if (ath9k_wiphy_scanning(sc)) {
  2615. printk(KERN_DEBUG "ath9k: Two wiphys trying to scan at the "
  2616. "same time\n");
  2617. /*
  2618. * Do not allow the concurrent scanning state for now. This
  2619. * could be improved with scanning control moved into ath9k.
  2620. */
  2621. mutex_unlock(&sc->mutex);
  2622. return;
  2623. }
  2624. aphy->state = ATH_WIPHY_SCAN;
  2625. ath9k_wiphy_pause_all_forced(sc, aphy);
  2626. spin_lock_bh(&sc->ani_lock);
  2627. sc->sc_flags |= SC_OP_SCANNING;
  2628. spin_unlock_bh(&sc->ani_lock);
  2629. mutex_unlock(&sc->mutex);
  2630. }
  2631. static void ath9k_sw_scan_complete(struct ieee80211_hw *hw)
  2632. {
  2633. struct ath_wiphy *aphy = hw->priv;
  2634. struct ath_softc *sc = aphy->sc;
  2635. mutex_lock(&sc->mutex);
  2636. spin_lock_bh(&sc->ani_lock);
  2637. aphy->state = ATH_WIPHY_ACTIVE;
  2638. sc->sc_flags &= ~SC_OP_SCANNING;
  2639. sc->sc_flags |= SC_OP_FULL_RESET;
  2640. spin_unlock_bh(&sc->ani_lock);
  2641. ath_beacon_config(sc, NULL);
  2642. mutex_unlock(&sc->mutex);
  2643. }
  2644. struct ieee80211_ops ath9k_ops = {
  2645. .tx = ath9k_tx,
  2646. .start = ath9k_start,
  2647. .stop = ath9k_stop,
  2648. .add_interface = ath9k_add_interface,
  2649. .remove_interface = ath9k_remove_interface,
  2650. .config = ath9k_config,
  2651. .configure_filter = ath9k_configure_filter,
  2652. .sta_notify = ath9k_sta_notify,
  2653. .conf_tx = ath9k_conf_tx,
  2654. .bss_info_changed = ath9k_bss_info_changed,
  2655. .set_key = ath9k_set_key,
  2656. .get_tsf = ath9k_get_tsf,
  2657. .set_tsf = ath9k_set_tsf,
  2658. .reset_tsf = ath9k_reset_tsf,
  2659. .ampdu_action = ath9k_ampdu_action,
  2660. .sw_scan_start = ath9k_sw_scan_start,
  2661. .sw_scan_complete = ath9k_sw_scan_complete,
  2662. .rfkill_poll = ath9k_rfkill_poll_state,
  2663. };
  2664. static struct {
  2665. u32 version;
  2666. const char * name;
  2667. } ath_mac_bb_names[] = {
  2668. { AR_SREV_VERSION_5416_PCI, "5416" },
  2669. { AR_SREV_VERSION_5416_PCIE, "5418" },
  2670. { AR_SREV_VERSION_9100, "9100" },
  2671. { AR_SREV_VERSION_9160, "9160" },
  2672. { AR_SREV_VERSION_9280, "9280" },
  2673. { AR_SREV_VERSION_9285, "9285" },
  2674. { AR_SREV_VERSION_9287, "9287" }
  2675. };
  2676. static struct {
  2677. u16 version;
  2678. const char * name;
  2679. } ath_rf_names[] = {
  2680. { 0, "5133" },
  2681. { AR_RAD5133_SREV_MAJOR, "5133" },
  2682. { AR_RAD5122_SREV_MAJOR, "5122" },
  2683. { AR_RAD2133_SREV_MAJOR, "2133" },
  2684. { AR_RAD2122_SREV_MAJOR, "2122" }
  2685. };
  2686. /*
  2687. * Return the MAC/BB name. "????" is returned if the MAC/BB is unknown.
  2688. */
  2689. const char *
  2690. ath_mac_bb_name(u32 mac_bb_version)
  2691. {
  2692. int i;
  2693. for (i=0; i<ARRAY_SIZE(ath_mac_bb_names); i++) {
  2694. if (ath_mac_bb_names[i].version == mac_bb_version) {
  2695. return ath_mac_bb_names[i].name;
  2696. }
  2697. }
  2698. return "????";
  2699. }
  2700. /*
  2701. * Return the RF name. "????" is returned if the RF is unknown.
  2702. */
  2703. const char *
  2704. ath_rf_name(u16 rf_version)
  2705. {
  2706. int i;
  2707. for (i=0; i<ARRAY_SIZE(ath_rf_names); i++) {
  2708. if (ath_rf_names[i].version == rf_version) {
  2709. return ath_rf_names[i].name;
  2710. }
  2711. }
  2712. return "????";
  2713. }
  2714. static int __init ath9k_init(void)
  2715. {
  2716. int error;
  2717. /* Register rate control algorithm */
  2718. error = ath_rate_control_register();
  2719. if (error != 0) {
  2720. printk(KERN_ERR
  2721. "ath9k: Unable to register rate control "
  2722. "algorithm: %d\n",
  2723. error);
  2724. goto err_out;
  2725. }
  2726. error = ath9k_debug_create_root();
  2727. if (error) {
  2728. printk(KERN_ERR
  2729. "ath9k: Unable to create debugfs root: %d\n",
  2730. error);
  2731. goto err_rate_unregister;
  2732. }
  2733. error = ath_pci_init();
  2734. if (error < 0) {
  2735. printk(KERN_ERR
  2736. "ath9k: No PCI devices found, driver not installed.\n");
  2737. error = -ENODEV;
  2738. goto err_remove_root;
  2739. }
  2740. error = ath_ahb_init();
  2741. if (error < 0) {
  2742. error = -ENODEV;
  2743. goto err_pci_exit;
  2744. }
  2745. return 0;
  2746. err_pci_exit:
  2747. ath_pci_exit();
  2748. err_remove_root:
  2749. ath9k_debug_remove_root();
  2750. err_rate_unregister:
  2751. ath_rate_control_unregister();
  2752. err_out:
  2753. return error;
  2754. }
  2755. module_init(ath9k_init);
  2756. static void __exit ath9k_exit(void)
  2757. {
  2758. ath_ahb_exit();
  2759. ath_pci_exit();
  2760. ath9k_debug_remove_root();
  2761. ath_rate_control_unregister();
  2762. printk(KERN_INFO "%s: Driver unloaded\n", dev_info);
  2763. }
  2764. module_exit(ath9k_exit);