main.c 84 KB

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