main.c 81 KB

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