main.c 79 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161
  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. if (bss_conf->assoc) {
  839. DPRINTF(sc->sc_ah, ATH_DBG_CONFIG, "Bss Info ASSOC %d, bssid: %pM\n",
  840. bss_conf->aid, sc->curbssid);
  841. /* New association, store aid */
  842. sc->curaid = bss_conf->aid;
  843. ath9k_hw_write_associd(sc);
  844. /*
  845. * Request a re-configuration of Beacon related timers
  846. * on the receipt of the first Beacon frame (i.e.,
  847. * after time sync with the AP).
  848. */
  849. sc->sc_flags |= SC_OP_BEACON_SYNC;
  850. /* Configure the beacon */
  851. ath_beacon_config(sc, vif);
  852. /* Reset rssi stats */
  853. sc->sc_ah->stats.avgbrssi = ATH_RSSI_DUMMY_MARKER;
  854. ath_start_ani(sc);
  855. } else {
  856. DPRINTF(sc->sc_ah, ATH_DBG_CONFIG, "Bss Info DISASSOC\n");
  857. sc->curaid = 0;
  858. /* Stop ANI */
  859. del_timer_sync(&sc->ani.timer);
  860. }
  861. }
  862. /********************************/
  863. /* LED functions */
  864. /********************************/
  865. static void ath_led_blink_work(struct work_struct *work)
  866. {
  867. struct ath_softc *sc = container_of(work, struct ath_softc,
  868. ath_led_blink_work.work);
  869. if (!(sc->sc_flags & SC_OP_LED_ASSOCIATED))
  870. return;
  871. if ((sc->led_on_duration == ATH_LED_ON_DURATION_IDLE) ||
  872. (sc->led_off_duration == ATH_LED_OFF_DURATION_IDLE))
  873. ath9k_hw_set_gpio(sc->sc_ah, sc->sc_ah->led_pin, 0);
  874. else
  875. ath9k_hw_set_gpio(sc->sc_ah, sc->sc_ah->led_pin,
  876. (sc->sc_flags & SC_OP_LED_ON) ? 1 : 0);
  877. ieee80211_queue_delayed_work(sc->hw,
  878. &sc->ath_led_blink_work,
  879. (sc->sc_flags & SC_OP_LED_ON) ?
  880. msecs_to_jiffies(sc->led_off_duration) :
  881. msecs_to_jiffies(sc->led_on_duration));
  882. sc->led_on_duration = sc->led_on_cnt ?
  883. max((ATH_LED_ON_DURATION_IDLE - sc->led_on_cnt), 25) :
  884. ATH_LED_ON_DURATION_IDLE;
  885. sc->led_off_duration = sc->led_off_cnt ?
  886. max((ATH_LED_OFF_DURATION_IDLE - sc->led_off_cnt), 10) :
  887. ATH_LED_OFF_DURATION_IDLE;
  888. sc->led_on_cnt = sc->led_off_cnt = 0;
  889. if (sc->sc_flags & SC_OP_LED_ON)
  890. sc->sc_flags &= ~SC_OP_LED_ON;
  891. else
  892. sc->sc_flags |= SC_OP_LED_ON;
  893. }
  894. static void ath_led_brightness(struct led_classdev *led_cdev,
  895. enum led_brightness brightness)
  896. {
  897. struct ath_led *led = container_of(led_cdev, struct ath_led, led_cdev);
  898. struct ath_softc *sc = led->sc;
  899. switch (brightness) {
  900. case LED_OFF:
  901. if (led->led_type == ATH_LED_ASSOC ||
  902. led->led_type == ATH_LED_RADIO) {
  903. ath9k_hw_set_gpio(sc->sc_ah, sc->sc_ah->led_pin,
  904. (led->led_type == ATH_LED_RADIO));
  905. sc->sc_flags &= ~SC_OP_LED_ASSOCIATED;
  906. if (led->led_type == ATH_LED_RADIO)
  907. sc->sc_flags &= ~SC_OP_LED_ON;
  908. } else {
  909. sc->led_off_cnt++;
  910. }
  911. break;
  912. case LED_FULL:
  913. if (led->led_type == ATH_LED_ASSOC) {
  914. sc->sc_flags |= SC_OP_LED_ASSOCIATED;
  915. ieee80211_queue_delayed_work(sc->hw,
  916. &sc->ath_led_blink_work, 0);
  917. } else if (led->led_type == ATH_LED_RADIO) {
  918. ath9k_hw_set_gpio(sc->sc_ah, sc->sc_ah->led_pin, 0);
  919. sc->sc_flags |= SC_OP_LED_ON;
  920. } else {
  921. sc->led_on_cnt++;
  922. }
  923. break;
  924. default:
  925. break;
  926. }
  927. }
  928. static int ath_register_led(struct ath_softc *sc, struct ath_led *led,
  929. char *trigger)
  930. {
  931. int ret;
  932. led->sc = sc;
  933. led->led_cdev.name = led->name;
  934. led->led_cdev.default_trigger = trigger;
  935. led->led_cdev.brightness_set = ath_led_brightness;
  936. ret = led_classdev_register(wiphy_dev(sc->hw->wiphy), &led->led_cdev);
  937. if (ret)
  938. DPRINTF(sc->sc_ah, ATH_DBG_FATAL,
  939. "Failed to register led:%s", led->name);
  940. else
  941. led->registered = 1;
  942. return ret;
  943. }
  944. static void ath_unregister_led(struct ath_led *led)
  945. {
  946. if (led->registered) {
  947. led_classdev_unregister(&led->led_cdev);
  948. led->registered = 0;
  949. }
  950. }
  951. static void ath_deinit_leds(struct ath_softc *sc)
  952. {
  953. ath_unregister_led(&sc->assoc_led);
  954. sc->sc_flags &= ~SC_OP_LED_ASSOCIATED;
  955. ath_unregister_led(&sc->tx_led);
  956. ath_unregister_led(&sc->rx_led);
  957. ath_unregister_led(&sc->radio_led);
  958. ath9k_hw_set_gpio(sc->sc_ah, sc->sc_ah->led_pin, 1);
  959. }
  960. static void ath_init_leds(struct ath_softc *sc)
  961. {
  962. char *trigger;
  963. int ret;
  964. if (AR_SREV_9287(sc->sc_ah))
  965. sc->sc_ah->led_pin = ATH_LED_PIN_9287;
  966. else
  967. sc->sc_ah->led_pin = ATH_LED_PIN_DEF;
  968. /* Configure gpio 1 for output */
  969. ath9k_hw_cfg_output(sc->sc_ah, sc->sc_ah->led_pin,
  970. AR_GPIO_OUTPUT_MUX_AS_OUTPUT);
  971. /* LED off, active low */
  972. ath9k_hw_set_gpio(sc->sc_ah, sc->sc_ah->led_pin, 1);
  973. INIT_DELAYED_WORK(&sc->ath_led_blink_work, ath_led_blink_work);
  974. trigger = ieee80211_get_radio_led_name(sc->hw);
  975. snprintf(sc->radio_led.name, sizeof(sc->radio_led.name),
  976. "ath9k-%s::radio", wiphy_name(sc->hw->wiphy));
  977. ret = ath_register_led(sc, &sc->radio_led, trigger);
  978. sc->radio_led.led_type = ATH_LED_RADIO;
  979. if (ret)
  980. goto fail;
  981. trigger = ieee80211_get_assoc_led_name(sc->hw);
  982. snprintf(sc->assoc_led.name, sizeof(sc->assoc_led.name),
  983. "ath9k-%s::assoc", wiphy_name(sc->hw->wiphy));
  984. ret = ath_register_led(sc, &sc->assoc_led, trigger);
  985. sc->assoc_led.led_type = ATH_LED_ASSOC;
  986. if (ret)
  987. goto fail;
  988. trigger = ieee80211_get_tx_led_name(sc->hw);
  989. snprintf(sc->tx_led.name, sizeof(sc->tx_led.name),
  990. "ath9k-%s::tx", wiphy_name(sc->hw->wiphy));
  991. ret = ath_register_led(sc, &sc->tx_led, trigger);
  992. sc->tx_led.led_type = ATH_LED_TX;
  993. if (ret)
  994. goto fail;
  995. trigger = ieee80211_get_rx_led_name(sc->hw);
  996. snprintf(sc->rx_led.name, sizeof(sc->rx_led.name),
  997. "ath9k-%s::rx", wiphy_name(sc->hw->wiphy));
  998. ret = ath_register_led(sc, &sc->rx_led, trigger);
  999. sc->rx_led.led_type = ATH_LED_RX;
  1000. if (ret)
  1001. goto fail;
  1002. return;
  1003. fail:
  1004. cancel_delayed_work_sync(&sc->ath_led_blink_work);
  1005. ath_deinit_leds(sc);
  1006. }
  1007. void ath_radio_enable(struct ath_softc *sc)
  1008. {
  1009. struct ath_hw *ah = sc->sc_ah;
  1010. struct ieee80211_channel *channel = sc->hw->conf.channel;
  1011. int r;
  1012. ath9k_ps_wakeup(sc);
  1013. ath9k_hw_configpcipowersave(ah, 0, 0);
  1014. if (!ah->curchan)
  1015. ah->curchan = ath_get_curchannel(sc, sc->hw);
  1016. spin_lock_bh(&sc->sc_resetlock);
  1017. r = ath9k_hw_reset(ah, ah->curchan, false);
  1018. if (r) {
  1019. DPRINTF(sc->sc_ah, ATH_DBG_FATAL,
  1020. "Unable to reset channel %u (%uMhz) ",
  1021. "reset status %d\n",
  1022. channel->center_freq, r);
  1023. }
  1024. spin_unlock_bh(&sc->sc_resetlock);
  1025. ath_update_txpow(sc);
  1026. if (ath_startrecv(sc) != 0) {
  1027. DPRINTF(sc->sc_ah, ATH_DBG_FATAL,
  1028. "Unable to restart recv logic\n");
  1029. return;
  1030. }
  1031. if (sc->sc_flags & SC_OP_BEACONS)
  1032. ath_beacon_config(sc, NULL); /* restart beacons */
  1033. /* Re-Enable interrupts */
  1034. ath9k_hw_set_interrupts(ah, sc->imask);
  1035. /* Enable LED */
  1036. ath9k_hw_cfg_output(ah, ah->led_pin,
  1037. AR_GPIO_OUTPUT_MUX_AS_OUTPUT);
  1038. ath9k_hw_set_gpio(ah, ah->led_pin, 0);
  1039. ieee80211_wake_queues(sc->hw);
  1040. ath9k_ps_restore(sc);
  1041. }
  1042. void ath_radio_disable(struct ath_softc *sc)
  1043. {
  1044. struct ath_hw *ah = sc->sc_ah;
  1045. struct ieee80211_channel *channel = sc->hw->conf.channel;
  1046. int r;
  1047. ath9k_ps_wakeup(sc);
  1048. ieee80211_stop_queues(sc->hw);
  1049. /* Disable LED */
  1050. ath9k_hw_set_gpio(ah, ah->led_pin, 1);
  1051. ath9k_hw_cfg_gpio_input(ah, ah->led_pin);
  1052. /* Disable interrupts */
  1053. ath9k_hw_set_interrupts(ah, 0);
  1054. ath_drain_all_txq(sc, false); /* clear pending tx frames */
  1055. ath_stoprecv(sc); /* turn off frame recv */
  1056. ath_flushrecv(sc); /* flush recv queue */
  1057. if (!ah->curchan)
  1058. ah->curchan = ath_get_curchannel(sc, sc->hw);
  1059. spin_lock_bh(&sc->sc_resetlock);
  1060. r = ath9k_hw_reset(ah, ah->curchan, false);
  1061. if (r) {
  1062. DPRINTF(sc->sc_ah, ATH_DBG_FATAL,
  1063. "Unable to reset channel %u (%uMhz) "
  1064. "reset status %d\n",
  1065. channel->center_freq, r);
  1066. }
  1067. spin_unlock_bh(&sc->sc_resetlock);
  1068. ath9k_hw_phy_disable(ah);
  1069. ath9k_hw_configpcipowersave(ah, 1, 1);
  1070. ath9k_ps_restore(sc);
  1071. ath9k_setpower(sc, ATH9K_PM_FULL_SLEEP);
  1072. }
  1073. /*******************/
  1074. /* Rfkill */
  1075. /*******************/
  1076. static bool ath_is_rfkill_set(struct ath_softc *sc)
  1077. {
  1078. struct ath_hw *ah = sc->sc_ah;
  1079. return ath9k_hw_gpio_get(ah, ah->rfkill_gpio) ==
  1080. ah->rfkill_polarity;
  1081. }
  1082. static void ath9k_rfkill_poll_state(struct ieee80211_hw *hw)
  1083. {
  1084. struct ath_wiphy *aphy = hw->priv;
  1085. struct ath_softc *sc = aphy->sc;
  1086. bool blocked = !!ath_is_rfkill_set(sc);
  1087. wiphy_rfkill_set_hw_state(hw->wiphy, blocked);
  1088. }
  1089. static void ath_start_rfkill_poll(struct ath_softc *sc)
  1090. {
  1091. struct ath_hw *ah = sc->sc_ah;
  1092. if (ah->caps.hw_caps & ATH9K_HW_CAP_RFSILENT)
  1093. wiphy_rfkill_start_polling(sc->hw->wiphy);
  1094. }
  1095. void ath_cleanup(struct ath_softc *sc)
  1096. {
  1097. ath_detach(sc);
  1098. free_irq(sc->irq, sc);
  1099. ath_bus_cleanup(sc);
  1100. kfree(sc->sec_wiphy);
  1101. ieee80211_free_hw(sc->hw);
  1102. }
  1103. void ath_detach(struct ath_softc *sc)
  1104. {
  1105. struct ieee80211_hw *hw = sc->hw;
  1106. struct ath_hw *ah = sc->sc_ah;
  1107. int i = 0;
  1108. ath9k_ps_wakeup(sc);
  1109. dev_dbg(sc->dev, "Detach ATH hw\n");
  1110. ath_deinit_leds(sc);
  1111. wiphy_rfkill_stop_polling(sc->hw->wiphy);
  1112. for (i = 0; i < sc->num_sec_wiphy; i++) {
  1113. struct ath_wiphy *aphy = sc->sec_wiphy[i];
  1114. if (aphy == NULL)
  1115. continue;
  1116. sc->sec_wiphy[i] = NULL;
  1117. ieee80211_unregister_hw(aphy->hw);
  1118. ieee80211_free_hw(aphy->hw);
  1119. }
  1120. ieee80211_unregister_hw(hw);
  1121. ath_rx_cleanup(sc);
  1122. ath_tx_cleanup(sc);
  1123. tasklet_kill(&sc->intr_tq);
  1124. tasklet_kill(&sc->bcon_tasklet);
  1125. if (!(sc->sc_flags & SC_OP_INVALID))
  1126. ath9k_setpower(sc, ATH9K_PM_AWAKE);
  1127. /* cleanup tx queues */
  1128. for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++)
  1129. if (ATH_TXQ_SETUP(sc, i))
  1130. ath_tx_cleanupq(sc, &sc->tx.txq[i]);
  1131. if ((sc->btcoex.no_stomp_timer) &&
  1132. ah->btcoex_hw.scheme == ATH_BTCOEX_CFG_3WIRE)
  1133. ath_gen_timer_free(ah, sc->btcoex.no_stomp_timer);
  1134. ath9k_hw_detach(ah);
  1135. ath9k_exit_debug(ah);
  1136. sc->sc_ah = NULL;
  1137. }
  1138. static int ath9k_reg_notifier(struct wiphy *wiphy,
  1139. struct regulatory_request *request)
  1140. {
  1141. struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
  1142. struct ath_wiphy *aphy = hw->priv;
  1143. struct ath_softc *sc = aphy->sc;
  1144. struct ath_regulatory *reg = &sc->common.regulatory;
  1145. return ath_reg_notifier_apply(wiphy, request, reg);
  1146. }
  1147. /*
  1148. * Detects if there is any priority bt traffic
  1149. */
  1150. static void ath_detect_bt_priority(struct ath_softc *sc)
  1151. {
  1152. struct ath_btcoex *btcoex = &sc->btcoex;
  1153. struct ath_hw *ah = sc->sc_ah;
  1154. if (ath9k_hw_gpio_get(sc->sc_ah, ah->btcoex_hw.btpriority_gpio))
  1155. btcoex->bt_priority_cnt++;
  1156. if (time_after(jiffies, btcoex->bt_priority_time +
  1157. msecs_to_jiffies(ATH_BT_PRIORITY_TIME_THRESHOLD))) {
  1158. if (btcoex->bt_priority_cnt >= ATH_BT_CNT_THRESHOLD) {
  1159. DPRINTF(sc->sc_ah, ATH_DBG_BTCOEX,
  1160. "BT priority traffic detected");
  1161. sc->sc_flags |= SC_OP_BT_PRIORITY_DETECTED;
  1162. } else {
  1163. sc->sc_flags &= ~SC_OP_BT_PRIORITY_DETECTED;
  1164. }
  1165. btcoex->bt_priority_cnt = 0;
  1166. btcoex->bt_priority_time = jiffies;
  1167. }
  1168. }
  1169. /*
  1170. * Configures appropriate weight based on stomp type.
  1171. */
  1172. static void ath9k_btcoex_bt_stomp(struct ath_softc *sc,
  1173. enum ath_stomp_type stomp_type)
  1174. {
  1175. struct ath_hw *ah = sc->sc_ah;
  1176. switch (stomp_type) {
  1177. case ATH_BTCOEX_STOMP_ALL:
  1178. ath9k_hw_btcoex_set_weight(ah, AR_BT_COEX_WGHT,
  1179. AR_STOMP_ALL_WLAN_WGHT);
  1180. break;
  1181. case ATH_BTCOEX_STOMP_LOW:
  1182. ath9k_hw_btcoex_set_weight(ah, AR_BT_COEX_WGHT,
  1183. AR_STOMP_LOW_WLAN_WGHT);
  1184. break;
  1185. case ATH_BTCOEX_STOMP_NONE:
  1186. ath9k_hw_btcoex_set_weight(ah, AR_BT_COEX_WGHT,
  1187. AR_STOMP_NONE_WLAN_WGHT);
  1188. break;
  1189. default:
  1190. DPRINTF(ah, ATH_DBG_BTCOEX, "Invalid Stomptype\n");
  1191. break;
  1192. }
  1193. ath9k_hw_btcoex_enable(ah);
  1194. }
  1195. /*
  1196. * This is the master bt coex timer which runs for every
  1197. * 45ms, bt traffic will be given priority during 55% of this
  1198. * period while wlan gets remaining 45%
  1199. */
  1200. static void ath_btcoex_period_timer(unsigned long data)
  1201. {
  1202. struct ath_softc *sc = (struct ath_softc *) data;
  1203. struct ath_hw *ah = sc->sc_ah;
  1204. struct ath_btcoex *btcoex = &sc->btcoex;
  1205. ath_detect_bt_priority(sc);
  1206. spin_lock_bh(&btcoex->btcoex_lock);
  1207. ath9k_btcoex_bt_stomp(sc, btcoex->bt_stomp_type);
  1208. spin_unlock_bh(&btcoex->btcoex_lock);
  1209. if (btcoex->btcoex_period != btcoex->btcoex_no_stomp) {
  1210. if (btcoex->hw_timer_enabled)
  1211. ath_gen_timer_stop(ah, btcoex->no_stomp_timer);
  1212. ath_gen_timer_start(ah,
  1213. btcoex->no_stomp_timer,
  1214. (ath9k_hw_gettsf32(ah) +
  1215. btcoex->btcoex_no_stomp),
  1216. btcoex->btcoex_no_stomp * 10);
  1217. btcoex->hw_timer_enabled = true;
  1218. }
  1219. mod_timer(&btcoex->period_timer, jiffies +
  1220. msecs_to_jiffies(ATH_BTCOEX_DEF_BT_PERIOD));
  1221. }
  1222. /*
  1223. * Generic tsf based hw timer which configures weight
  1224. * registers to time slice between wlan and bt traffic
  1225. */
  1226. static void ath_btcoex_no_stomp_timer(void *arg)
  1227. {
  1228. struct ath_softc *sc = (struct ath_softc *)arg;
  1229. struct ath_hw *ah = sc->sc_ah;
  1230. struct ath_btcoex *btcoex = &sc->btcoex;
  1231. DPRINTF(ah, ATH_DBG_BTCOEX, "no stomp timer running \n");
  1232. spin_lock_bh(&btcoex->btcoex_lock);
  1233. if (btcoex->bt_stomp_type == ATH_BTCOEX_STOMP_LOW)
  1234. ath9k_btcoex_bt_stomp(sc, ATH_BTCOEX_STOMP_NONE);
  1235. else if (btcoex->bt_stomp_type == ATH_BTCOEX_STOMP_ALL)
  1236. ath9k_btcoex_bt_stomp(sc, ATH_BTCOEX_STOMP_LOW);
  1237. spin_unlock_bh(&btcoex->btcoex_lock);
  1238. }
  1239. static int ath_init_btcoex_timer(struct ath_softc *sc)
  1240. {
  1241. struct ath_btcoex *btcoex = &sc->btcoex;
  1242. btcoex->btcoex_period = ATH_BTCOEX_DEF_BT_PERIOD * 1000;
  1243. btcoex->btcoex_no_stomp = (100 - ATH_BTCOEX_DEF_DUTY_CYCLE) *
  1244. btcoex->btcoex_period / 100;
  1245. setup_timer(&btcoex->period_timer, ath_btcoex_period_timer,
  1246. (unsigned long) sc);
  1247. spin_lock_init(&btcoex->btcoex_lock);
  1248. btcoex->no_stomp_timer = ath_gen_timer_alloc(sc->sc_ah,
  1249. ath_btcoex_no_stomp_timer,
  1250. ath_btcoex_no_stomp_timer,
  1251. (void *) sc, AR_FIRST_NDP_TIMER);
  1252. if (!btcoex->no_stomp_timer)
  1253. return -ENOMEM;
  1254. return 0;
  1255. }
  1256. /*
  1257. * Initialize and fill ath_softc, ath_sofct is the
  1258. * "Software Carrier" struct. Historically it has existed
  1259. * to allow the separation between hardware specific
  1260. * variables (now in ath_hw) and driver specific variables.
  1261. */
  1262. static int ath_init_softc(u16 devid, struct ath_softc *sc, u16 subsysid)
  1263. {
  1264. struct ath_hw *ah = NULL;
  1265. int r = 0, i;
  1266. int csz = 0;
  1267. int qnum;
  1268. /* XXX: hardware will not be ready until ath_open() being called */
  1269. sc->sc_flags |= SC_OP_INVALID;
  1270. spin_lock_init(&sc->wiphy_lock);
  1271. spin_lock_init(&sc->sc_resetlock);
  1272. spin_lock_init(&sc->sc_serial_rw);
  1273. spin_lock_init(&sc->ani_lock);
  1274. spin_lock_init(&sc->sc_pm_lock);
  1275. mutex_init(&sc->mutex);
  1276. tasklet_init(&sc->intr_tq, ath9k_tasklet, (unsigned long)sc);
  1277. tasklet_init(&sc->bcon_tasklet, ath_beacon_tasklet,
  1278. (unsigned long)sc);
  1279. /*
  1280. * Cache line size is used to size and align various
  1281. * structures used to communicate with the hardware.
  1282. */
  1283. ath_read_cachesize(sc, &csz);
  1284. /* XXX assert csz is non-zero */
  1285. sc->common.cachelsz = csz << 2; /* convert to bytes */
  1286. ah = kzalloc(sizeof(struct ath_hw), GFP_KERNEL);
  1287. if (!ah) {
  1288. r = -ENOMEM;
  1289. goto bad_no_ah;
  1290. }
  1291. ah->ah_sc = sc;
  1292. ah->hw_version.devid = devid;
  1293. ah->hw_version.subsysid = subsysid;
  1294. sc->sc_ah = ah;
  1295. if (ath9k_init_debug(ah) < 0)
  1296. dev_err(sc->dev, "Unable to create debugfs files\n");
  1297. r = ath9k_hw_init(ah);
  1298. if (r) {
  1299. DPRINTF(ah, ATH_DBG_FATAL,
  1300. "Unable to initialize hardware; "
  1301. "initialization status: %d\n", r);
  1302. goto bad;
  1303. }
  1304. /* Get the hardware key cache size. */
  1305. sc->keymax = ah->caps.keycache_size;
  1306. if (sc->keymax > ATH_KEYMAX) {
  1307. DPRINTF(ah, ATH_DBG_ANY,
  1308. "Warning, using only %u entries in %u key cache\n",
  1309. ATH_KEYMAX, sc->keymax);
  1310. sc->keymax = ATH_KEYMAX;
  1311. }
  1312. /*
  1313. * Reset the key cache since some parts do not
  1314. * reset the contents on initial power up.
  1315. */
  1316. for (i = 0; i < sc->keymax; i++)
  1317. ath9k_hw_keyreset(ah, (u16) i);
  1318. /* default to MONITOR mode */
  1319. sc->sc_ah->opmode = NL80211_IFTYPE_MONITOR;
  1320. /* Setup rate tables */
  1321. ath_rate_attach(sc);
  1322. ath_setup_rates(sc, IEEE80211_BAND_2GHZ);
  1323. ath_setup_rates(sc, IEEE80211_BAND_5GHZ);
  1324. /*
  1325. * Allocate hardware transmit queues: one queue for
  1326. * beacon frames and one data queue for each QoS
  1327. * priority. Note that the hal handles reseting
  1328. * these queues at the needed time.
  1329. */
  1330. sc->beacon.beaconq = ath_beaconq_setup(ah);
  1331. if (sc->beacon.beaconq == -1) {
  1332. DPRINTF(ah, ATH_DBG_FATAL,
  1333. "Unable to setup a beacon xmit queue\n");
  1334. r = -EIO;
  1335. goto bad2;
  1336. }
  1337. sc->beacon.cabq = ath_txq_setup(sc, ATH9K_TX_QUEUE_CAB, 0);
  1338. if (sc->beacon.cabq == NULL) {
  1339. DPRINTF(ah, ATH_DBG_FATAL,
  1340. "Unable to setup CAB xmit queue\n");
  1341. r = -EIO;
  1342. goto bad2;
  1343. }
  1344. sc->config.cabqReadytime = ATH_CABQ_READY_TIME;
  1345. ath_cabq_update(sc);
  1346. for (i = 0; i < ARRAY_SIZE(sc->tx.hwq_map); i++)
  1347. sc->tx.hwq_map[i] = -1;
  1348. /* Setup data queues */
  1349. /* NB: ensure BK queue is the lowest priority h/w queue */
  1350. if (!ath_tx_setup(sc, ATH9K_WME_AC_BK)) {
  1351. DPRINTF(ah, ATH_DBG_FATAL,
  1352. "Unable to setup xmit queue for BK traffic\n");
  1353. r = -EIO;
  1354. goto bad2;
  1355. }
  1356. if (!ath_tx_setup(sc, ATH9K_WME_AC_BE)) {
  1357. DPRINTF(ah, ATH_DBG_FATAL,
  1358. "Unable to setup xmit queue for BE traffic\n");
  1359. r = -EIO;
  1360. goto bad2;
  1361. }
  1362. if (!ath_tx_setup(sc, ATH9K_WME_AC_VI)) {
  1363. DPRINTF(ah, ATH_DBG_FATAL,
  1364. "Unable to setup xmit queue for VI traffic\n");
  1365. r = -EIO;
  1366. goto bad2;
  1367. }
  1368. if (!ath_tx_setup(sc, ATH9K_WME_AC_VO)) {
  1369. DPRINTF(ah, ATH_DBG_FATAL,
  1370. "Unable to setup xmit queue for VO traffic\n");
  1371. r = -EIO;
  1372. goto bad2;
  1373. }
  1374. /* Initializes the noise floor to a reasonable default value.
  1375. * Later on this will be updated during ANI processing. */
  1376. sc->ani.noise_floor = ATH_DEFAULT_NOISE_FLOOR;
  1377. setup_timer(&sc->ani.timer, ath_ani_calibrate, (unsigned long)sc);
  1378. if (ath9k_hw_getcapability(ah, ATH9K_CAP_CIPHER,
  1379. ATH9K_CIPHER_TKIP, NULL)) {
  1380. /*
  1381. * Whether we should enable h/w TKIP MIC.
  1382. * XXX: if we don't support WME TKIP MIC, then we wouldn't
  1383. * report WMM capable, so it's always safe to turn on
  1384. * TKIP MIC in this case.
  1385. */
  1386. ath9k_hw_setcapability(sc->sc_ah, ATH9K_CAP_TKIP_MIC,
  1387. 0, 1, NULL);
  1388. }
  1389. /*
  1390. * Check whether the separate key cache entries
  1391. * are required to handle both tx+rx MIC keys.
  1392. * With split mic keys the number of stations is limited
  1393. * to 27 otherwise 59.
  1394. */
  1395. if (ath9k_hw_getcapability(ah, ATH9K_CAP_CIPHER,
  1396. ATH9K_CIPHER_TKIP, NULL)
  1397. && ath9k_hw_getcapability(ah, ATH9K_CAP_CIPHER,
  1398. ATH9K_CIPHER_MIC, NULL)
  1399. && ath9k_hw_getcapability(ah, ATH9K_CAP_TKIP_SPLIT,
  1400. 0, NULL))
  1401. sc->splitmic = 1;
  1402. /* turn on mcast key search if possible */
  1403. if (!ath9k_hw_getcapability(ah, ATH9K_CAP_MCAST_KEYSRCH, 0, NULL))
  1404. (void)ath9k_hw_setcapability(ah, ATH9K_CAP_MCAST_KEYSRCH, 1,
  1405. 1, NULL);
  1406. sc->config.txpowlimit = ATH_TXPOWER_MAX;
  1407. /* 11n Capabilities */
  1408. if (ah->caps.hw_caps & ATH9K_HW_CAP_HT) {
  1409. sc->sc_flags |= SC_OP_TXAGGR;
  1410. sc->sc_flags |= SC_OP_RXAGGR;
  1411. }
  1412. sc->tx_chainmask = ah->caps.tx_chainmask;
  1413. sc->rx_chainmask = ah->caps.rx_chainmask;
  1414. ath9k_hw_setcapability(ah, ATH9K_CAP_DIVERSITY, 1, true, NULL);
  1415. sc->rx.defant = ath9k_hw_getdefantenna(ah);
  1416. if (ah->caps.hw_caps & ATH9K_HW_CAP_BSSIDMASK)
  1417. memcpy(sc->bssidmask, ath_bcast_mac, ETH_ALEN);
  1418. sc->beacon.slottime = ATH9K_SLOT_TIME_9; /* default to short slot time */
  1419. /* initialize beacon slots */
  1420. for (i = 0; i < ARRAY_SIZE(sc->beacon.bslot); i++) {
  1421. sc->beacon.bslot[i] = NULL;
  1422. sc->beacon.bslot_aphy[i] = NULL;
  1423. }
  1424. /* setup channels and rates */
  1425. sc->sbands[IEEE80211_BAND_2GHZ].channels = ath9k_2ghz_chantable;
  1426. sc->sbands[IEEE80211_BAND_2GHZ].bitrates =
  1427. sc->rates[IEEE80211_BAND_2GHZ];
  1428. sc->sbands[IEEE80211_BAND_2GHZ].band = IEEE80211_BAND_2GHZ;
  1429. sc->sbands[IEEE80211_BAND_2GHZ].n_channels =
  1430. ARRAY_SIZE(ath9k_2ghz_chantable);
  1431. if (test_bit(ATH9K_MODE_11A, sc->sc_ah->caps.wireless_modes)) {
  1432. sc->sbands[IEEE80211_BAND_5GHZ].channels = ath9k_5ghz_chantable;
  1433. sc->sbands[IEEE80211_BAND_5GHZ].bitrates =
  1434. sc->rates[IEEE80211_BAND_5GHZ];
  1435. sc->sbands[IEEE80211_BAND_5GHZ].band = IEEE80211_BAND_5GHZ;
  1436. sc->sbands[IEEE80211_BAND_5GHZ].n_channels =
  1437. ARRAY_SIZE(ath9k_5ghz_chantable);
  1438. }
  1439. switch (ah->btcoex_hw.scheme) {
  1440. case ATH_BTCOEX_CFG_NONE:
  1441. break;
  1442. case ATH_BTCOEX_CFG_2WIRE:
  1443. ath9k_hw_btcoex_init_2wire(ah);
  1444. break;
  1445. case ATH_BTCOEX_CFG_3WIRE:
  1446. ath9k_hw_btcoex_init_3wire(ah);
  1447. r = ath_init_btcoex_timer(sc);
  1448. if (r)
  1449. goto bad2;
  1450. qnum = ath_tx_get_qnum(sc, ATH9K_TX_QUEUE_DATA, ATH9K_WME_AC_BE);
  1451. ath9k_hw_init_btcoex_hw(ah, qnum);
  1452. sc->btcoex.bt_stomp_type = ATH_BTCOEX_STOMP_LOW;
  1453. break;
  1454. default:
  1455. WARN_ON(1);
  1456. break;
  1457. }
  1458. return 0;
  1459. bad2:
  1460. /* cleanup tx queues */
  1461. for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++)
  1462. if (ATH_TXQ_SETUP(sc, i))
  1463. ath_tx_cleanupq(sc, &sc->tx.txq[i]);
  1464. bad:
  1465. ath9k_hw_detach(ah);
  1466. bad_no_ah:
  1467. ath9k_exit_debug(sc->sc_ah);
  1468. sc->sc_ah = NULL;
  1469. return r;
  1470. }
  1471. void ath_set_hw_capab(struct ath_softc *sc, struct ieee80211_hw *hw)
  1472. {
  1473. hw->flags = IEEE80211_HW_RX_INCLUDES_FCS |
  1474. IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING |
  1475. IEEE80211_HW_SIGNAL_DBM |
  1476. IEEE80211_HW_AMPDU_AGGREGATION |
  1477. IEEE80211_HW_SUPPORTS_PS |
  1478. IEEE80211_HW_PS_NULLFUNC_STACK |
  1479. IEEE80211_HW_SPECTRUM_MGMT;
  1480. if (AR_SREV_9160_10_OR_LATER(sc->sc_ah) || modparam_nohwcrypt)
  1481. hw->flags |= IEEE80211_HW_MFP_CAPABLE;
  1482. hw->wiphy->interface_modes =
  1483. BIT(NL80211_IFTYPE_AP) |
  1484. BIT(NL80211_IFTYPE_STATION) |
  1485. BIT(NL80211_IFTYPE_ADHOC) |
  1486. BIT(NL80211_IFTYPE_MESH_POINT);
  1487. hw->queues = 4;
  1488. hw->max_rates = 4;
  1489. hw->channel_change_time = 5000;
  1490. hw->max_listen_interval = 10;
  1491. /* Hardware supports 10 but we use 4 */
  1492. hw->max_rate_tries = 4;
  1493. hw->sta_data_size = sizeof(struct ath_node);
  1494. hw->vif_data_size = sizeof(struct ath_vif);
  1495. hw->rate_control_algorithm = "ath9k_rate_control";
  1496. hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
  1497. &sc->sbands[IEEE80211_BAND_2GHZ];
  1498. if (test_bit(ATH9K_MODE_11A, sc->sc_ah->caps.wireless_modes))
  1499. hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
  1500. &sc->sbands[IEEE80211_BAND_5GHZ];
  1501. }
  1502. /* Device driver core initialization */
  1503. int ath_init_device(u16 devid, struct ath_softc *sc, u16 subsysid)
  1504. {
  1505. struct ieee80211_hw *hw = sc->hw;
  1506. struct ath_hw *ah;
  1507. int error = 0, i;
  1508. struct ath_regulatory *reg;
  1509. dev_dbg(sc->dev, "Attach ATH hw\n");
  1510. error = ath_init_softc(devid, sc, subsysid);
  1511. if (error != 0)
  1512. return error;
  1513. ah = sc->sc_ah;
  1514. /* get mac address from hardware and set in mac80211 */
  1515. SET_IEEE80211_PERM_ADDR(hw, ah->macaddr);
  1516. ath_set_hw_capab(sc, hw);
  1517. error = ath_regd_init(&sc->common.regulatory, sc->hw->wiphy,
  1518. ath9k_reg_notifier);
  1519. if (error)
  1520. return error;
  1521. reg = &sc->common.regulatory;
  1522. if (ah->caps.hw_caps & ATH9K_HW_CAP_HT) {
  1523. setup_ht_cap(sc, &sc->sbands[IEEE80211_BAND_2GHZ].ht_cap);
  1524. if (test_bit(ATH9K_MODE_11A, ah->caps.wireless_modes))
  1525. setup_ht_cap(sc, &sc->sbands[IEEE80211_BAND_5GHZ].ht_cap);
  1526. }
  1527. /* initialize tx/rx engine */
  1528. error = ath_tx_init(sc, ATH_TXBUF);
  1529. if (error != 0)
  1530. goto error_attach;
  1531. error = ath_rx_init(sc, ATH_RXBUF);
  1532. if (error != 0)
  1533. goto error_attach;
  1534. INIT_WORK(&sc->chan_work, ath9k_wiphy_chan_work);
  1535. INIT_DELAYED_WORK(&sc->wiphy_work, ath9k_wiphy_work);
  1536. sc->wiphy_scheduler_int = msecs_to_jiffies(500);
  1537. error = ieee80211_register_hw(hw);
  1538. if (!ath_is_world_regd(reg)) {
  1539. error = regulatory_hint(hw->wiphy, reg->alpha2);
  1540. if (error)
  1541. goto error_attach;
  1542. }
  1543. /* Initialize LED control */
  1544. ath_init_leds(sc);
  1545. ath_start_rfkill_poll(sc);
  1546. return 0;
  1547. error_attach:
  1548. /* cleanup tx queues */
  1549. for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++)
  1550. if (ATH_TXQ_SETUP(sc, i))
  1551. ath_tx_cleanupq(sc, &sc->tx.txq[i]);
  1552. ath9k_hw_detach(ah);
  1553. ath9k_exit_debug(ah);
  1554. sc->sc_ah = NULL;
  1555. return error;
  1556. }
  1557. int ath_reset(struct ath_softc *sc, bool retry_tx)
  1558. {
  1559. struct ath_hw *ah = sc->sc_ah;
  1560. struct ieee80211_hw *hw = sc->hw;
  1561. int r;
  1562. ath9k_hw_set_interrupts(ah, 0);
  1563. ath_drain_all_txq(sc, retry_tx);
  1564. ath_stoprecv(sc);
  1565. ath_flushrecv(sc);
  1566. spin_lock_bh(&sc->sc_resetlock);
  1567. r = ath9k_hw_reset(ah, sc->sc_ah->curchan, false);
  1568. if (r)
  1569. DPRINTF(ah, ATH_DBG_FATAL,
  1570. "Unable to reset hardware; reset status %d\n", r);
  1571. spin_unlock_bh(&sc->sc_resetlock);
  1572. if (ath_startrecv(sc) != 0)
  1573. DPRINTF(ah, ATH_DBG_FATAL, "Unable to start recv logic\n");
  1574. /*
  1575. * We may be doing a reset in response to a request
  1576. * that changes the channel so update any state that
  1577. * might change as a result.
  1578. */
  1579. ath_cache_conf_rate(sc, &hw->conf);
  1580. ath_update_txpow(sc);
  1581. if (sc->sc_flags & SC_OP_BEACONS)
  1582. ath_beacon_config(sc, NULL); /* restart beacons */
  1583. ath9k_hw_set_interrupts(ah, sc->imask);
  1584. if (retry_tx) {
  1585. int i;
  1586. for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++) {
  1587. if (ATH_TXQ_SETUP(sc, i)) {
  1588. spin_lock_bh(&sc->tx.txq[i].axq_lock);
  1589. ath_txq_schedule(sc, &sc->tx.txq[i]);
  1590. spin_unlock_bh(&sc->tx.txq[i].axq_lock);
  1591. }
  1592. }
  1593. }
  1594. return r;
  1595. }
  1596. /*
  1597. * This function will allocate both the DMA descriptor structure, and the
  1598. * buffers it contains. These are used to contain the descriptors used
  1599. * by the system.
  1600. */
  1601. int ath_descdma_setup(struct ath_softc *sc, struct ath_descdma *dd,
  1602. struct list_head *head, const char *name,
  1603. int nbuf, int ndesc)
  1604. {
  1605. #define DS2PHYS(_dd, _ds) \
  1606. ((_dd)->dd_desc_paddr + ((caddr_t)(_ds) - (caddr_t)(_dd)->dd_desc))
  1607. #define ATH_DESC_4KB_BOUND_CHECK(_daddr) ((((_daddr) & 0xFFF) > 0xF7F) ? 1 : 0)
  1608. #define ATH_DESC_4KB_BOUND_NUM_SKIPPED(_len) ((_len) / 4096)
  1609. struct ath_desc *ds;
  1610. struct ath_buf *bf;
  1611. int i, bsize, error;
  1612. DPRINTF(sc->sc_ah, ATH_DBG_CONFIG, "%s DMA: %u buffers %u desc/buf\n",
  1613. name, nbuf, ndesc);
  1614. INIT_LIST_HEAD(head);
  1615. /* ath_desc must be a multiple of DWORDs */
  1616. if ((sizeof(struct ath_desc) % 4) != 0) {
  1617. DPRINTF(sc->sc_ah, ATH_DBG_FATAL, "ath_desc not DWORD aligned\n");
  1618. ASSERT((sizeof(struct ath_desc) % 4) == 0);
  1619. error = -ENOMEM;
  1620. goto fail;
  1621. }
  1622. dd->dd_desc_len = sizeof(struct ath_desc) * nbuf * ndesc;
  1623. /*
  1624. * Need additional DMA memory because we can't use
  1625. * descriptors that cross the 4K page boundary. Assume
  1626. * one skipped descriptor per 4K page.
  1627. */
  1628. if (!(sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_4KB_SPLITTRANS)) {
  1629. u32 ndesc_skipped =
  1630. ATH_DESC_4KB_BOUND_NUM_SKIPPED(dd->dd_desc_len);
  1631. u32 dma_len;
  1632. while (ndesc_skipped) {
  1633. dma_len = ndesc_skipped * sizeof(struct ath_desc);
  1634. dd->dd_desc_len += dma_len;
  1635. ndesc_skipped = ATH_DESC_4KB_BOUND_NUM_SKIPPED(dma_len);
  1636. };
  1637. }
  1638. /* allocate descriptors */
  1639. dd->dd_desc = dma_alloc_coherent(sc->dev, dd->dd_desc_len,
  1640. &dd->dd_desc_paddr, GFP_KERNEL);
  1641. if (dd->dd_desc == NULL) {
  1642. error = -ENOMEM;
  1643. goto fail;
  1644. }
  1645. ds = dd->dd_desc;
  1646. DPRINTF(sc->sc_ah, ATH_DBG_CONFIG, "%s DMA map: %p (%u) -> %llx (%u)\n",
  1647. name, ds, (u32) dd->dd_desc_len,
  1648. ito64(dd->dd_desc_paddr), /*XXX*/(u32) dd->dd_desc_len);
  1649. /* allocate buffers */
  1650. bsize = sizeof(struct ath_buf) * nbuf;
  1651. bf = kzalloc(bsize, GFP_KERNEL);
  1652. if (bf == NULL) {
  1653. error = -ENOMEM;
  1654. goto fail2;
  1655. }
  1656. dd->dd_bufptr = bf;
  1657. for (i = 0; i < nbuf; i++, bf++, ds += ndesc) {
  1658. bf->bf_desc = ds;
  1659. bf->bf_daddr = DS2PHYS(dd, ds);
  1660. if (!(sc->sc_ah->caps.hw_caps &
  1661. ATH9K_HW_CAP_4KB_SPLITTRANS)) {
  1662. /*
  1663. * Skip descriptor addresses which can cause 4KB
  1664. * boundary crossing (addr + length) with a 32 dword
  1665. * descriptor fetch.
  1666. */
  1667. while (ATH_DESC_4KB_BOUND_CHECK(bf->bf_daddr)) {
  1668. ASSERT((caddr_t) bf->bf_desc <
  1669. ((caddr_t) dd->dd_desc +
  1670. dd->dd_desc_len));
  1671. ds += ndesc;
  1672. bf->bf_desc = ds;
  1673. bf->bf_daddr = DS2PHYS(dd, ds);
  1674. }
  1675. }
  1676. list_add_tail(&bf->list, head);
  1677. }
  1678. return 0;
  1679. fail2:
  1680. dma_free_coherent(sc->dev, dd->dd_desc_len, dd->dd_desc,
  1681. dd->dd_desc_paddr);
  1682. fail:
  1683. memset(dd, 0, sizeof(*dd));
  1684. return error;
  1685. #undef ATH_DESC_4KB_BOUND_CHECK
  1686. #undef ATH_DESC_4KB_BOUND_NUM_SKIPPED
  1687. #undef DS2PHYS
  1688. }
  1689. void ath_descdma_cleanup(struct ath_softc *sc,
  1690. struct ath_descdma *dd,
  1691. struct list_head *head)
  1692. {
  1693. dma_free_coherent(sc->dev, dd->dd_desc_len, dd->dd_desc,
  1694. dd->dd_desc_paddr);
  1695. INIT_LIST_HEAD(head);
  1696. kfree(dd->dd_bufptr);
  1697. memset(dd, 0, sizeof(*dd));
  1698. }
  1699. int ath_get_hal_qnum(u16 queue, struct ath_softc *sc)
  1700. {
  1701. int qnum;
  1702. switch (queue) {
  1703. case 0:
  1704. qnum = sc->tx.hwq_map[ATH9K_WME_AC_VO];
  1705. break;
  1706. case 1:
  1707. qnum = sc->tx.hwq_map[ATH9K_WME_AC_VI];
  1708. break;
  1709. case 2:
  1710. qnum = sc->tx.hwq_map[ATH9K_WME_AC_BE];
  1711. break;
  1712. case 3:
  1713. qnum = sc->tx.hwq_map[ATH9K_WME_AC_BK];
  1714. break;
  1715. default:
  1716. qnum = sc->tx.hwq_map[ATH9K_WME_AC_BE];
  1717. break;
  1718. }
  1719. return qnum;
  1720. }
  1721. int ath_get_mac80211_qnum(u32 queue, struct ath_softc *sc)
  1722. {
  1723. int qnum;
  1724. switch (queue) {
  1725. case ATH9K_WME_AC_VO:
  1726. qnum = 0;
  1727. break;
  1728. case ATH9K_WME_AC_VI:
  1729. qnum = 1;
  1730. break;
  1731. case ATH9K_WME_AC_BE:
  1732. qnum = 2;
  1733. break;
  1734. case ATH9K_WME_AC_BK:
  1735. qnum = 3;
  1736. break;
  1737. default:
  1738. qnum = -1;
  1739. break;
  1740. }
  1741. return qnum;
  1742. }
  1743. /* XXX: Remove me once we don't depend on ath9k_channel for all
  1744. * this redundant data */
  1745. void ath9k_update_ichannel(struct ath_softc *sc, struct ieee80211_hw *hw,
  1746. struct ath9k_channel *ichan)
  1747. {
  1748. struct ieee80211_channel *chan = hw->conf.channel;
  1749. struct ieee80211_conf *conf = &hw->conf;
  1750. ichan->channel = chan->center_freq;
  1751. ichan->chan = chan;
  1752. if (chan->band == IEEE80211_BAND_2GHZ) {
  1753. ichan->chanmode = CHANNEL_G;
  1754. ichan->channelFlags = CHANNEL_2GHZ | CHANNEL_OFDM | CHANNEL_G;
  1755. } else {
  1756. ichan->chanmode = CHANNEL_A;
  1757. ichan->channelFlags = CHANNEL_5GHZ | CHANNEL_OFDM;
  1758. }
  1759. sc->tx_chan_width = ATH9K_HT_MACMODE_20;
  1760. if (conf_is_ht(conf)) {
  1761. if (conf_is_ht40(conf))
  1762. sc->tx_chan_width = ATH9K_HT_MACMODE_2040;
  1763. ichan->chanmode = ath_get_extchanmode(sc, chan,
  1764. conf->channel_type);
  1765. }
  1766. }
  1767. /**********************/
  1768. /* mac80211 callbacks */
  1769. /**********************/
  1770. /*
  1771. * (Re)start btcoex timers
  1772. */
  1773. static void ath9k_btcoex_timer_resume(struct ath_softc *sc)
  1774. {
  1775. struct ath_btcoex *btcoex = &sc->btcoex;
  1776. struct ath_hw *ah = sc->sc_ah;
  1777. DPRINTF(ah, ATH_DBG_BTCOEX, "Starting btcoex timers");
  1778. /* make sure duty cycle timer is also stopped when resuming */
  1779. if (btcoex->hw_timer_enabled)
  1780. ath_gen_timer_stop(sc->sc_ah, btcoex->no_stomp_timer);
  1781. btcoex->bt_priority_cnt = 0;
  1782. btcoex->bt_priority_time = jiffies;
  1783. sc->sc_flags &= ~SC_OP_BT_PRIORITY_DETECTED;
  1784. mod_timer(&btcoex->period_timer, jiffies);
  1785. }
  1786. static int ath9k_start(struct ieee80211_hw *hw)
  1787. {
  1788. struct ath_wiphy *aphy = hw->priv;
  1789. struct ath_softc *sc = aphy->sc;
  1790. struct ath_hw *ah = sc->sc_ah;
  1791. struct ieee80211_channel *curchan = hw->conf.channel;
  1792. struct ath9k_channel *init_channel;
  1793. int r;
  1794. DPRINTF(ah, ATH_DBG_CONFIG, "Starting driver with "
  1795. "initial channel: %d MHz\n", curchan->center_freq);
  1796. mutex_lock(&sc->mutex);
  1797. if (ath9k_wiphy_started(sc)) {
  1798. if (sc->chan_idx == curchan->hw_value) {
  1799. /*
  1800. * Already on the operational channel, the new wiphy
  1801. * can be marked active.
  1802. */
  1803. aphy->state = ATH_WIPHY_ACTIVE;
  1804. ieee80211_wake_queues(hw);
  1805. } else {
  1806. /*
  1807. * Another wiphy is on another channel, start the new
  1808. * wiphy in paused state.
  1809. */
  1810. aphy->state = ATH_WIPHY_PAUSED;
  1811. ieee80211_stop_queues(hw);
  1812. }
  1813. mutex_unlock(&sc->mutex);
  1814. return 0;
  1815. }
  1816. aphy->state = ATH_WIPHY_ACTIVE;
  1817. /* setup initial channel */
  1818. sc->chan_idx = curchan->hw_value;
  1819. init_channel = ath_get_curchannel(sc, hw);
  1820. /* Reset SERDES registers */
  1821. ath9k_hw_configpcipowersave(ah, 0, 0);
  1822. /*
  1823. * The basic interface to setting the hardware in a good
  1824. * state is ``reset''. On return the hardware is known to
  1825. * be powered up and with interrupts disabled. This must
  1826. * be followed by initialization of the appropriate bits
  1827. * and then setup of the interrupt mask.
  1828. */
  1829. spin_lock_bh(&sc->sc_resetlock);
  1830. r = ath9k_hw_reset(ah, init_channel, false);
  1831. if (r) {
  1832. DPRINTF(ah, ATH_DBG_FATAL,
  1833. "Unable to reset hardware; reset status %d "
  1834. "(freq %u MHz)\n", r,
  1835. curchan->center_freq);
  1836. spin_unlock_bh(&sc->sc_resetlock);
  1837. goto mutex_unlock;
  1838. }
  1839. spin_unlock_bh(&sc->sc_resetlock);
  1840. /*
  1841. * This is needed only to setup initial state
  1842. * but it's best done after a reset.
  1843. */
  1844. ath_update_txpow(sc);
  1845. /*
  1846. * Setup the hardware after reset:
  1847. * The receive engine is set going.
  1848. * Frame transmit is handled entirely
  1849. * in the frame output path; there's nothing to do
  1850. * here except setup the interrupt mask.
  1851. */
  1852. if (ath_startrecv(sc) != 0) {
  1853. DPRINTF(ah, ATH_DBG_FATAL, "Unable to start recv logic\n");
  1854. r = -EIO;
  1855. goto mutex_unlock;
  1856. }
  1857. /* Setup our intr mask. */
  1858. sc->imask = ATH9K_INT_RX | ATH9K_INT_TX
  1859. | ATH9K_INT_RXEOL | ATH9K_INT_RXORN
  1860. | ATH9K_INT_FATAL | ATH9K_INT_GLOBAL;
  1861. if (ah->caps.hw_caps & ATH9K_HW_CAP_GTT)
  1862. sc->imask |= ATH9K_INT_GTT;
  1863. if (ah->caps.hw_caps & ATH9K_HW_CAP_HT)
  1864. sc->imask |= ATH9K_INT_CST;
  1865. ath_cache_conf_rate(sc, &hw->conf);
  1866. sc->sc_flags &= ~SC_OP_INVALID;
  1867. /* Disable BMISS interrupt when we're not associated */
  1868. sc->imask &= ~(ATH9K_INT_SWBA | ATH9K_INT_BMISS);
  1869. ath9k_hw_set_interrupts(ah, sc->imask);
  1870. ieee80211_wake_queues(hw);
  1871. ieee80211_queue_delayed_work(sc->hw, &sc->tx_complete_work, 0);
  1872. if ((ah->btcoex_hw.scheme != ATH_BTCOEX_CFG_NONE) &&
  1873. !ah->btcoex_hw.enabled) {
  1874. ath9k_hw_btcoex_set_weight(ah, AR_BT_COEX_WGHT,
  1875. AR_STOMP_LOW_WLAN_WGHT);
  1876. ath9k_hw_btcoex_enable(ah);
  1877. ath_pcie_aspm_disable(sc);
  1878. if (ah->btcoex_hw.scheme == ATH_BTCOEX_CFG_3WIRE)
  1879. ath9k_btcoex_timer_resume(sc);
  1880. }
  1881. mutex_unlock:
  1882. mutex_unlock(&sc->mutex);
  1883. return r;
  1884. }
  1885. static int ath9k_tx(struct ieee80211_hw *hw,
  1886. struct sk_buff *skb)
  1887. {
  1888. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1889. struct ath_wiphy *aphy = hw->priv;
  1890. struct ath_softc *sc = aphy->sc;
  1891. struct ath_tx_control txctl;
  1892. int hdrlen, padsize;
  1893. if (aphy->state != ATH_WIPHY_ACTIVE && aphy->state != ATH_WIPHY_SCAN) {
  1894. printk(KERN_DEBUG "ath9k: %s: TX in unexpected wiphy state "
  1895. "%d\n", wiphy_name(hw->wiphy), aphy->state);
  1896. goto exit;
  1897. }
  1898. if (sc->ps_enabled) {
  1899. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  1900. /*
  1901. * mac80211 does not set PM field for normal data frames, so we
  1902. * need to update that based on the current PS mode.
  1903. */
  1904. if (ieee80211_is_data(hdr->frame_control) &&
  1905. !ieee80211_is_nullfunc(hdr->frame_control) &&
  1906. !ieee80211_has_pm(hdr->frame_control)) {
  1907. DPRINTF(sc->sc_ah, ATH_DBG_PS, "Add PM=1 for a TX frame "
  1908. "while in PS mode\n");
  1909. hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
  1910. }
  1911. }
  1912. if (unlikely(sc->sc_ah->power_mode != ATH9K_PM_AWAKE)) {
  1913. /*
  1914. * We are using PS-Poll and mac80211 can request TX while in
  1915. * power save mode. Need to wake up hardware for the TX to be
  1916. * completed and if needed, also for RX of buffered frames.
  1917. */
  1918. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  1919. ath9k_ps_wakeup(sc);
  1920. ath9k_hw_setrxabort(sc->sc_ah, 0);
  1921. if (ieee80211_is_pspoll(hdr->frame_control)) {
  1922. DPRINTF(sc->sc_ah, ATH_DBG_PS, "Sending PS-Poll to pick a "
  1923. "buffered frame\n");
  1924. sc->sc_flags |= SC_OP_WAIT_FOR_PSPOLL_DATA;
  1925. } else {
  1926. DPRINTF(sc->sc_ah, ATH_DBG_PS, "Wake up to complete TX\n");
  1927. sc->sc_flags |= SC_OP_WAIT_FOR_TX_ACK;
  1928. }
  1929. /*
  1930. * The actual restore operation will happen only after
  1931. * the sc_flags bit is cleared. We are just dropping
  1932. * the ps_usecount here.
  1933. */
  1934. ath9k_ps_restore(sc);
  1935. }
  1936. memset(&txctl, 0, sizeof(struct ath_tx_control));
  1937. /*
  1938. * As a temporary workaround, assign seq# here; this will likely need
  1939. * to be cleaned up to work better with Beacon transmission and virtual
  1940. * BSSes.
  1941. */
  1942. if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
  1943. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  1944. if (info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT)
  1945. sc->tx.seq_no += 0x10;
  1946. hdr->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
  1947. hdr->seq_ctrl |= cpu_to_le16(sc->tx.seq_no);
  1948. }
  1949. /* Add the padding after the header if this is not already done */
  1950. hdrlen = ieee80211_get_hdrlen_from_skb(skb);
  1951. if (hdrlen & 3) {
  1952. padsize = hdrlen % 4;
  1953. if (skb_headroom(skb) < padsize)
  1954. return -1;
  1955. skb_push(skb, padsize);
  1956. memmove(skb->data, skb->data + padsize, hdrlen);
  1957. }
  1958. /* Check if a tx queue is available */
  1959. txctl.txq = ath_test_get_txq(sc, skb);
  1960. if (!txctl.txq)
  1961. goto exit;
  1962. DPRINTF(sc->sc_ah, ATH_DBG_XMIT, "transmitting packet, skb: %p\n", skb);
  1963. if (ath_tx_start(hw, skb, &txctl) != 0) {
  1964. DPRINTF(sc->sc_ah, ATH_DBG_XMIT, "TX failed\n");
  1965. goto exit;
  1966. }
  1967. return 0;
  1968. exit:
  1969. dev_kfree_skb_any(skb);
  1970. return 0;
  1971. }
  1972. /*
  1973. * Pause btcoex timer and bt duty cycle timer
  1974. */
  1975. static void ath9k_btcoex_timer_pause(struct ath_softc *sc)
  1976. {
  1977. struct ath_btcoex *btcoex = &sc->btcoex;
  1978. struct ath_hw *ah = sc->sc_ah;
  1979. del_timer_sync(&btcoex->period_timer);
  1980. if (btcoex->hw_timer_enabled)
  1981. ath_gen_timer_stop(ah, btcoex->no_stomp_timer);
  1982. btcoex->hw_timer_enabled = false;
  1983. }
  1984. static void ath9k_stop(struct ieee80211_hw *hw)
  1985. {
  1986. struct ath_wiphy *aphy = hw->priv;
  1987. struct ath_softc *sc = aphy->sc;
  1988. struct ath_hw *ah = sc->sc_ah;
  1989. mutex_lock(&sc->mutex);
  1990. aphy->state = ATH_WIPHY_INACTIVE;
  1991. cancel_delayed_work_sync(&sc->ath_led_blink_work);
  1992. cancel_delayed_work_sync(&sc->tx_complete_work);
  1993. if (!sc->num_sec_wiphy) {
  1994. cancel_delayed_work_sync(&sc->wiphy_work);
  1995. cancel_work_sync(&sc->chan_work);
  1996. }
  1997. if (sc->sc_flags & SC_OP_INVALID) {
  1998. DPRINTF(ah, ATH_DBG_ANY, "Device not present\n");
  1999. mutex_unlock(&sc->mutex);
  2000. return;
  2001. }
  2002. if (ath9k_wiphy_started(sc)) {
  2003. mutex_unlock(&sc->mutex);
  2004. return; /* another wiphy still in use */
  2005. }
  2006. if (ah->btcoex_hw.enabled) {
  2007. ath9k_hw_btcoex_disable(ah);
  2008. if (ah->btcoex_hw.scheme == ATH_BTCOEX_CFG_3WIRE)
  2009. ath9k_btcoex_timer_pause(sc);
  2010. }
  2011. /* make sure h/w will not generate any interrupt
  2012. * before setting the invalid flag. */
  2013. ath9k_hw_set_interrupts(ah, 0);
  2014. if (!(sc->sc_flags & SC_OP_INVALID)) {
  2015. ath_drain_all_txq(sc, false);
  2016. ath_stoprecv(sc);
  2017. ath9k_hw_phy_disable(ah);
  2018. } else
  2019. sc->rx.rxlink = NULL;
  2020. /* disable HAL and put h/w to sleep */
  2021. ath9k_hw_disable(ah);
  2022. ath9k_hw_configpcipowersave(ah, 1, 1);
  2023. ath9k_setpower(sc, ATH9K_PM_FULL_SLEEP);
  2024. sc->sc_flags |= SC_OP_INVALID;
  2025. mutex_unlock(&sc->mutex);
  2026. DPRINTF(ah, ATH_DBG_CONFIG, "Driver halt\n");
  2027. }
  2028. static int ath9k_add_interface(struct ieee80211_hw *hw,
  2029. struct ieee80211_if_init_conf *conf)
  2030. {
  2031. struct ath_wiphy *aphy = hw->priv;
  2032. struct ath_softc *sc = aphy->sc;
  2033. struct ath_vif *avp = (void *)conf->vif->drv_priv;
  2034. enum nl80211_iftype ic_opmode = NL80211_IFTYPE_UNSPECIFIED;
  2035. int ret = 0;
  2036. mutex_lock(&sc->mutex);
  2037. if (!(sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_BSSIDMASK) &&
  2038. sc->nvifs > 0) {
  2039. ret = -ENOBUFS;
  2040. goto out;
  2041. }
  2042. switch (conf->type) {
  2043. case NL80211_IFTYPE_STATION:
  2044. ic_opmode = NL80211_IFTYPE_STATION;
  2045. break;
  2046. case NL80211_IFTYPE_ADHOC:
  2047. case NL80211_IFTYPE_AP:
  2048. case NL80211_IFTYPE_MESH_POINT:
  2049. if (sc->nbcnvifs >= ATH_BCBUF) {
  2050. ret = -ENOBUFS;
  2051. goto out;
  2052. }
  2053. ic_opmode = conf->type;
  2054. break;
  2055. default:
  2056. DPRINTF(sc->sc_ah, ATH_DBG_FATAL,
  2057. "Interface type %d not yet supported\n", conf->type);
  2058. ret = -EOPNOTSUPP;
  2059. goto out;
  2060. }
  2061. DPRINTF(sc->sc_ah, ATH_DBG_CONFIG, "Attach a VIF of type: %d\n", ic_opmode);
  2062. /* Set the VIF opmode */
  2063. avp->av_opmode = ic_opmode;
  2064. avp->av_bslot = -1;
  2065. sc->nvifs++;
  2066. if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_BSSIDMASK)
  2067. ath9k_set_bssid_mask(hw);
  2068. if (sc->nvifs > 1)
  2069. goto out; /* skip global settings for secondary vif */
  2070. if (ic_opmode == NL80211_IFTYPE_AP) {
  2071. ath9k_hw_set_tsfadjust(sc->sc_ah, 1);
  2072. sc->sc_flags |= SC_OP_TSF_RESET;
  2073. }
  2074. /* Set the device opmode */
  2075. sc->sc_ah->opmode = ic_opmode;
  2076. /*
  2077. * Enable MIB interrupts when there are hardware phy counters.
  2078. * Note we only do this (at the moment) for station mode.
  2079. */
  2080. if ((conf->type == NL80211_IFTYPE_STATION) ||
  2081. (conf->type == NL80211_IFTYPE_ADHOC) ||
  2082. (conf->type == NL80211_IFTYPE_MESH_POINT)) {
  2083. sc->imask |= ATH9K_INT_MIB;
  2084. sc->imask |= ATH9K_INT_TSFOOR;
  2085. }
  2086. ath9k_hw_set_interrupts(sc->sc_ah, sc->imask);
  2087. if (conf->type == NL80211_IFTYPE_AP ||
  2088. conf->type == NL80211_IFTYPE_ADHOC ||
  2089. conf->type == NL80211_IFTYPE_MONITOR)
  2090. ath_start_ani(sc);
  2091. out:
  2092. mutex_unlock(&sc->mutex);
  2093. return ret;
  2094. }
  2095. static void ath9k_remove_interface(struct ieee80211_hw *hw,
  2096. struct ieee80211_if_init_conf *conf)
  2097. {
  2098. struct ath_wiphy *aphy = hw->priv;
  2099. struct ath_softc *sc = aphy->sc;
  2100. struct ath_vif *avp = (void *)conf->vif->drv_priv;
  2101. int i;
  2102. DPRINTF(sc->sc_ah, ATH_DBG_CONFIG, "Detach Interface\n");
  2103. mutex_lock(&sc->mutex);
  2104. /* Stop ANI */
  2105. del_timer_sync(&sc->ani.timer);
  2106. /* Reclaim beacon resources */
  2107. if ((sc->sc_ah->opmode == NL80211_IFTYPE_AP) ||
  2108. (sc->sc_ah->opmode == NL80211_IFTYPE_ADHOC) ||
  2109. (sc->sc_ah->opmode == NL80211_IFTYPE_MESH_POINT)) {
  2110. ath9k_hw_stoptxdma(sc->sc_ah, sc->beacon.beaconq);
  2111. ath_beacon_return(sc, avp);
  2112. }
  2113. sc->sc_flags &= ~SC_OP_BEACONS;
  2114. for (i = 0; i < ARRAY_SIZE(sc->beacon.bslot); i++) {
  2115. if (sc->beacon.bslot[i] == conf->vif) {
  2116. printk(KERN_DEBUG "%s: vif had allocated beacon "
  2117. "slot\n", __func__);
  2118. sc->beacon.bslot[i] = NULL;
  2119. sc->beacon.bslot_aphy[i] = NULL;
  2120. }
  2121. }
  2122. sc->nvifs--;
  2123. mutex_unlock(&sc->mutex);
  2124. }
  2125. static int ath9k_config(struct ieee80211_hw *hw, u32 changed)
  2126. {
  2127. struct ath_wiphy *aphy = hw->priv;
  2128. struct ath_softc *sc = aphy->sc;
  2129. struct ieee80211_conf *conf = &hw->conf;
  2130. struct ath_hw *ah = sc->sc_ah;
  2131. bool all_wiphys_idle = false, disable_radio = false;
  2132. mutex_lock(&sc->mutex);
  2133. /* Leave this as the first check */
  2134. if (changed & IEEE80211_CONF_CHANGE_IDLE) {
  2135. spin_lock_bh(&sc->wiphy_lock);
  2136. all_wiphys_idle = ath9k_all_wiphys_idle(sc);
  2137. spin_unlock_bh(&sc->wiphy_lock);
  2138. if (conf->flags & IEEE80211_CONF_IDLE){
  2139. if (all_wiphys_idle)
  2140. disable_radio = true;
  2141. }
  2142. else if (all_wiphys_idle) {
  2143. ath_radio_enable(sc);
  2144. DPRINTF(sc->sc_ah, ATH_DBG_CONFIG,
  2145. "not-idle: enabling radio\n");
  2146. }
  2147. }
  2148. if (changed & IEEE80211_CONF_CHANGE_PS) {
  2149. if (conf->flags & IEEE80211_CONF_PS) {
  2150. if (!(ah->caps.hw_caps &
  2151. ATH9K_HW_CAP_AUTOSLEEP)) {
  2152. if ((sc->imask & ATH9K_INT_TIM_TIMER) == 0) {
  2153. sc->imask |= ATH9K_INT_TIM_TIMER;
  2154. ath9k_hw_set_interrupts(sc->sc_ah,
  2155. sc->imask);
  2156. }
  2157. ath9k_hw_setrxabort(sc->sc_ah, 1);
  2158. }
  2159. sc->ps_enabled = true;
  2160. } else {
  2161. sc->ps_enabled = false;
  2162. ath9k_setpower(sc, ATH9K_PM_AWAKE);
  2163. if (!(ah->caps.hw_caps &
  2164. ATH9K_HW_CAP_AUTOSLEEP)) {
  2165. ath9k_hw_setrxabort(sc->sc_ah, 0);
  2166. sc->sc_flags &= ~(SC_OP_WAIT_FOR_BEACON |
  2167. SC_OP_WAIT_FOR_CAB |
  2168. SC_OP_WAIT_FOR_PSPOLL_DATA |
  2169. SC_OP_WAIT_FOR_TX_ACK);
  2170. if (sc->imask & ATH9K_INT_TIM_TIMER) {
  2171. sc->imask &= ~ATH9K_INT_TIM_TIMER;
  2172. ath9k_hw_set_interrupts(sc->sc_ah,
  2173. sc->imask);
  2174. }
  2175. }
  2176. }
  2177. }
  2178. if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
  2179. struct ieee80211_channel *curchan = hw->conf.channel;
  2180. int pos = curchan->hw_value;
  2181. aphy->chan_idx = pos;
  2182. aphy->chan_is_ht = conf_is_ht(conf);
  2183. if (aphy->state == ATH_WIPHY_SCAN ||
  2184. aphy->state == ATH_WIPHY_ACTIVE)
  2185. ath9k_wiphy_pause_all_forced(sc, aphy);
  2186. else {
  2187. /*
  2188. * Do not change operational channel based on a paused
  2189. * wiphy changes.
  2190. */
  2191. goto skip_chan_change;
  2192. }
  2193. DPRINTF(sc->sc_ah, ATH_DBG_CONFIG, "Set channel: %d MHz\n",
  2194. curchan->center_freq);
  2195. /* XXX: remove me eventualy */
  2196. ath9k_update_ichannel(sc, hw, &sc->sc_ah->channels[pos]);
  2197. ath_update_chainmask(sc, conf_is_ht(conf));
  2198. if (ath_set_channel(sc, hw, &sc->sc_ah->channels[pos]) < 0) {
  2199. DPRINTF(sc->sc_ah, ATH_DBG_FATAL, "Unable to set channel\n");
  2200. mutex_unlock(&sc->mutex);
  2201. return -EINVAL;
  2202. }
  2203. }
  2204. skip_chan_change:
  2205. if (changed & IEEE80211_CONF_CHANGE_POWER)
  2206. sc->config.txpowlimit = 2 * conf->power_level;
  2207. if (disable_radio) {
  2208. DPRINTF(sc->sc_ah, ATH_DBG_CONFIG, "idle: disabling radio\n");
  2209. ath_radio_disable(sc);
  2210. }
  2211. mutex_unlock(&sc->mutex);
  2212. return 0;
  2213. }
  2214. #define SUPPORTED_FILTERS \
  2215. (FIF_PROMISC_IN_BSS | \
  2216. FIF_ALLMULTI | \
  2217. FIF_CONTROL | \
  2218. FIF_PSPOLL | \
  2219. FIF_OTHER_BSS | \
  2220. FIF_BCN_PRBRESP_PROMISC | \
  2221. FIF_FCSFAIL)
  2222. /* FIXME: sc->sc_full_reset ? */
  2223. static void ath9k_configure_filter(struct ieee80211_hw *hw,
  2224. unsigned int changed_flags,
  2225. unsigned int *total_flags,
  2226. u64 multicast)
  2227. {
  2228. struct ath_wiphy *aphy = hw->priv;
  2229. struct ath_softc *sc = aphy->sc;
  2230. u32 rfilt;
  2231. changed_flags &= SUPPORTED_FILTERS;
  2232. *total_flags &= SUPPORTED_FILTERS;
  2233. sc->rx.rxfilter = *total_flags;
  2234. ath9k_ps_wakeup(sc);
  2235. rfilt = ath_calcrxfilter(sc);
  2236. ath9k_hw_setrxfilter(sc->sc_ah, rfilt);
  2237. ath9k_ps_restore(sc);
  2238. DPRINTF(sc->sc_ah, ATH_DBG_CONFIG, "Set HW RX filter: 0x%x\n", rfilt);
  2239. }
  2240. static void ath9k_sta_notify(struct ieee80211_hw *hw,
  2241. struct ieee80211_vif *vif,
  2242. enum sta_notify_cmd cmd,
  2243. struct ieee80211_sta *sta)
  2244. {
  2245. struct ath_wiphy *aphy = hw->priv;
  2246. struct ath_softc *sc = aphy->sc;
  2247. switch (cmd) {
  2248. case STA_NOTIFY_ADD:
  2249. ath_node_attach(sc, sta);
  2250. break;
  2251. case STA_NOTIFY_REMOVE:
  2252. ath_node_detach(sc, sta);
  2253. break;
  2254. default:
  2255. break;
  2256. }
  2257. }
  2258. static int ath9k_conf_tx(struct ieee80211_hw *hw, u16 queue,
  2259. const struct ieee80211_tx_queue_params *params)
  2260. {
  2261. struct ath_wiphy *aphy = hw->priv;
  2262. struct ath_softc *sc = aphy->sc;
  2263. struct ath9k_tx_queue_info qi;
  2264. int ret = 0, qnum;
  2265. if (queue >= WME_NUM_AC)
  2266. return 0;
  2267. mutex_lock(&sc->mutex);
  2268. memset(&qi, 0, sizeof(struct ath9k_tx_queue_info));
  2269. qi.tqi_aifs = params->aifs;
  2270. qi.tqi_cwmin = params->cw_min;
  2271. qi.tqi_cwmax = params->cw_max;
  2272. qi.tqi_burstTime = params->txop;
  2273. qnum = ath_get_hal_qnum(queue, sc);
  2274. DPRINTF(sc->sc_ah, ATH_DBG_CONFIG,
  2275. "Configure tx [queue/halq] [%d/%d], "
  2276. "aifs: %d, cw_min: %d, cw_max: %d, txop: %d\n",
  2277. queue, qnum, params->aifs, params->cw_min,
  2278. params->cw_max, params->txop);
  2279. ret = ath_txq_update(sc, qnum, &qi);
  2280. if (ret)
  2281. DPRINTF(sc->sc_ah, ATH_DBG_FATAL, "TXQ Update failed\n");
  2282. mutex_unlock(&sc->mutex);
  2283. return ret;
  2284. }
  2285. static int ath9k_set_key(struct ieee80211_hw *hw,
  2286. enum set_key_cmd cmd,
  2287. struct ieee80211_vif *vif,
  2288. struct ieee80211_sta *sta,
  2289. struct ieee80211_key_conf *key)
  2290. {
  2291. struct ath_wiphy *aphy = hw->priv;
  2292. struct ath_softc *sc = aphy->sc;
  2293. int ret = 0;
  2294. if (modparam_nohwcrypt)
  2295. return -ENOSPC;
  2296. mutex_lock(&sc->mutex);
  2297. ath9k_ps_wakeup(sc);
  2298. DPRINTF(sc->sc_ah, ATH_DBG_CONFIG, "Set HW Key\n");
  2299. switch (cmd) {
  2300. case SET_KEY:
  2301. ret = ath_key_config(sc, vif, sta, key);
  2302. if (ret >= 0) {
  2303. key->hw_key_idx = ret;
  2304. /* push IV and Michael MIC generation to stack */
  2305. key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  2306. if (key->alg == ALG_TKIP)
  2307. key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
  2308. if (sc->sc_ah->sw_mgmt_crypto && key->alg == ALG_CCMP)
  2309. key->flags |= IEEE80211_KEY_FLAG_SW_MGMT;
  2310. ret = 0;
  2311. }
  2312. break;
  2313. case DISABLE_KEY:
  2314. ath_key_delete(sc, key);
  2315. break;
  2316. default:
  2317. ret = -EINVAL;
  2318. }
  2319. ath9k_ps_restore(sc);
  2320. mutex_unlock(&sc->mutex);
  2321. return ret;
  2322. }
  2323. static void ath9k_bss_info_changed(struct ieee80211_hw *hw,
  2324. struct ieee80211_vif *vif,
  2325. struct ieee80211_bss_conf *bss_conf,
  2326. u32 changed)
  2327. {
  2328. struct ath_wiphy *aphy = hw->priv;
  2329. struct ath_softc *sc = aphy->sc;
  2330. struct ath_hw *ah = sc->sc_ah;
  2331. struct ath_vif *avp = (void *)vif->drv_priv;
  2332. u32 rfilt = 0;
  2333. int error, i;
  2334. mutex_lock(&sc->mutex);
  2335. /*
  2336. * TODO: Need to decide which hw opmode to use for
  2337. * multi-interface cases
  2338. * XXX: This belongs into add_interface!
  2339. */
  2340. if (vif->type == NL80211_IFTYPE_AP &&
  2341. ah->opmode != NL80211_IFTYPE_AP) {
  2342. ah->opmode = NL80211_IFTYPE_STATION;
  2343. ath9k_hw_setopmode(ah);
  2344. memcpy(sc->curbssid, sc->sc_ah->macaddr, ETH_ALEN);
  2345. sc->curaid = 0;
  2346. ath9k_hw_write_associd(sc);
  2347. /* Request full reset to get hw opmode changed properly */
  2348. sc->sc_flags |= SC_OP_FULL_RESET;
  2349. }
  2350. if ((changed & BSS_CHANGED_BSSID) &&
  2351. !is_zero_ether_addr(bss_conf->bssid)) {
  2352. switch (vif->type) {
  2353. case NL80211_IFTYPE_STATION:
  2354. case NL80211_IFTYPE_ADHOC:
  2355. case NL80211_IFTYPE_MESH_POINT:
  2356. /* Set BSSID */
  2357. memcpy(sc->curbssid, bss_conf->bssid, ETH_ALEN);
  2358. memcpy(avp->bssid, bss_conf->bssid, ETH_ALEN);
  2359. sc->curaid = 0;
  2360. ath9k_hw_write_associd(sc);
  2361. /* Set aggregation protection mode parameters */
  2362. sc->config.ath_aggr_prot = 0;
  2363. DPRINTF(sc->sc_ah, ATH_DBG_CONFIG,
  2364. "RX filter 0x%x bssid %pM aid 0x%x\n",
  2365. rfilt, sc->curbssid, sc->curaid);
  2366. /* need to reconfigure the beacon */
  2367. sc->sc_flags &= ~SC_OP_BEACONS ;
  2368. break;
  2369. default:
  2370. break;
  2371. }
  2372. }
  2373. if ((vif->type == NL80211_IFTYPE_ADHOC) ||
  2374. (vif->type == NL80211_IFTYPE_AP) ||
  2375. (vif->type == NL80211_IFTYPE_MESH_POINT)) {
  2376. if ((changed & BSS_CHANGED_BEACON) ||
  2377. (changed & BSS_CHANGED_BEACON_ENABLED &&
  2378. bss_conf->enable_beacon)) {
  2379. /*
  2380. * Allocate and setup the beacon frame.
  2381. *
  2382. * Stop any previous beacon DMA. This may be
  2383. * necessary, for example, when an ibss merge
  2384. * causes reconfiguration; we may be called
  2385. * with beacon transmission active.
  2386. */
  2387. ath9k_hw_stoptxdma(sc->sc_ah, sc->beacon.beaconq);
  2388. error = ath_beacon_alloc(aphy, vif);
  2389. if (!error)
  2390. ath_beacon_config(sc, vif);
  2391. }
  2392. }
  2393. /* Check for WLAN_CAPABILITY_PRIVACY ? */
  2394. if ((avp->av_opmode != NL80211_IFTYPE_STATION)) {
  2395. for (i = 0; i < IEEE80211_WEP_NKID; i++)
  2396. if (ath9k_hw_keyisvalid(sc->sc_ah, (u16)i))
  2397. ath9k_hw_keysetmac(sc->sc_ah,
  2398. (u16)i,
  2399. sc->curbssid);
  2400. }
  2401. /* Only legacy IBSS for now */
  2402. if (vif->type == NL80211_IFTYPE_ADHOC)
  2403. ath_update_chainmask(sc, 0);
  2404. if (changed & BSS_CHANGED_ERP_PREAMBLE) {
  2405. DPRINTF(sc->sc_ah, ATH_DBG_CONFIG, "BSS Changed PREAMBLE %d\n",
  2406. bss_conf->use_short_preamble);
  2407. if (bss_conf->use_short_preamble)
  2408. sc->sc_flags |= SC_OP_PREAMBLE_SHORT;
  2409. else
  2410. sc->sc_flags &= ~SC_OP_PREAMBLE_SHORT;
  2411. }
  2412. if (changed & BSS_CHANGED_ERP_CTS_PROT) {
  2413. DPRINTF(sc->sc_ah, ATH_DBG_CONFIG, "BSS Changed CTS PROT %d\n",
  2414. bss_conf->use_cts_prot);
  2415. if (bss_conf->use_cts_prot &&
  2416. hw->conf.channel->band != IEEE80211_BAND_5GHZ)
  2417. sc->sc_flags |= SC_OP_PROTECT_ENABLE;
  2418. else
  2419. sc->sc_flags &= ~SC_OP_PROTECT_ENABLE;
  2420. }
  2421. if (changed & BSS_CHANGED_ASSOC) {
  2422. DPRINTF(sc->sc_ah, ATH_DBG_CONFIG, "BSS Changed ASSOC %d\n",
  2423. bss_conf->assoc);
  2424. ath9k_bss_assoc_info(sc, vif, bss_conf);
  2425. }
  2426. /*
  2427. * The HW TSF has to be reset when the beacon interval changes.
  2428. * We set the flag here, and ath_beacon_config_ap() would take this
  2429. * into account when it gets called through the subsequent
  2430. * config_interface() call - with IFCC_BEACON in the changed field.
  2431. */
  2432. if (changed & BSS_CHANGED_BEACON_INT) {
  2433. sc->sc_flags |= SC_OP_TSF_RESET;
  2434. sc->beacon_interval = bss_conf->beacon_int;
  2435. }
  2436. mutex_unlock(&sc->mutex);
  2437. }
  2438. static u64 ath9k_get_tsf(struct ieee80211_hw *hw)
  2439. {
  2440. u64 tsf;
  2441. struct ath_wiphy *aphy = hw->priv;
  2442. struct ath_softc *sc = aphy->sc;
  2443. mutex_lock(&sc->mutex);
  2444. tsf = ath9k_hw_gettsf64(sc->sc_ah);
  2445. mutex_unlock(&sc->mutex);
  2446. return tsf;
  2447. }
  2448. static void ath9k_set_tsf(struct ieee80211_hw *hw, u64 tsf)
  2449. {
  2450. struct ath_wiphy *aphy = hw->priv;
  2451. struct ath_softc *sc = aphy->sc;
  2452. mutex_lock(&sc->mutex);
  2453. ath9k_hw_settsf64(sc->sc_ah, tsf);
  2454. mutex_unlock(&sc->mutex);
  2455. }
  2456. static void ath9k_reset_tsf(struct ieee80211_hw *hw)
  2457. {
  2458. struct ath_wiphy *aphy = hw->priv;
  2459. struct ath_softc *sc = aphy->sc;
  2460. mutex_lock(&sc->mutex);
  2461. ath9k_ps_wakeup(sc);
  2462. ath9k_hw_reset_tsf(sc->sc_ah);
  2463. ath9k_ps_restore(sc);
  2464. mutex_unlock(&sc->mutex);
  2465. }
  2466. static int ath9k_ampdu_action(struct ieee80211_hw *hw,
  2467. enum ieee80211_ampdu_mlme_action action,
  2468. struct ieee80211_sta *sta,
  2469. u16 tid, u16 *ssn)
  2470. {
  2471. struct ath_wiphy *aphy = hw->priv;
  2472. struct ath_softc *sc = aphy->sc;
  2473. int ret = 0;
  2474. switch (action) {
  2475. case IEEE80211_AMPDU_RX_START:
  2476. if (!(sc->sc_flags & SC_OP_RXAGGR))
  2477. ret = -ENOTSUPP;
  2478. break;
  2479. case IEEE80211_AMPDU_RX_STOP:
  2480. break;
  2481. case IEEE80211_AMPDU_TX_START:
  2482. ath_tx_aggr_start(sc, sta, tid, ssn);
  2483. ieee80211_start_tx_ba_cb_irqsafe(hw, sta->addr, tid);
  2484. break;
  2485. case IEEE80211_AMPDU_TX_STOP:
  2486. ath_tx_aggr_stop(sc, sta, tid);
  2487. ieee80211_stop_tx_ba_cb_irqsafe(hw, sta->addr, tid);
  2488. break;
  2489. case IEEE80211_AMPDU_TX_OPERATIONAL:
  2490. ath_tx_aggr_resume(sc, sta, tid);
  2491. break;
  2492. default:
  2493. DPRINTF(sc->sc_ah, ATH_DBG_FATAL, "Unknown AMPDU action\n");
  2494. }
  2495. return ret;
  2496. }
  2497. static void ath9k_sw_scan_start(struct ieee80211_hw *hw)
  2498. {
  2499. struct ath_wiphy *aphy = hw->priv;
  2500. struct ath_softc *sc = aphy->sc;
  2501. mutex_lock(&sc->mutex);
  2502. if (ath9k_wiphy_scanning(sc)) {
  2503. printk(KERN_DEBUG "ath9k: Two wiphys trying to scan at the "
  2504. "same time\n");
  2505. /*
  2506. * Do not allow the concurrent scanning state for now. This
  2507. * could be improved with scanning control moved into ath9k.
  2508. */
  2509. mutex_unlock(&sc->mutex);
  2510. return;
  2511. }
  2512. aphy->state = ATH_WIPHY_SCAN;
  2513. ath9k_wiphy_pause_all_forced(sc, aphy);
  2514. spin_lock_bh(&sc->ani_lock);
  2515. sc->sc_flags |= SC_OP_SCANNING;
  2516. spin_unlock_bh(&sc->ani_lock);
  2517. mutex_unlock(&sc->mutex);
  2518. }
  2519. static void ath9k_sw_scan_complete(struct ieee80211_hw *hw)
  2520. {
  2521. struct ath_wiphy *aphy = hw->priv;
  2522. struct ath_softc *sc = aphy->sc;
  2523. mutex_lock(&sc->mutex);
  2524. spin_lock_bh(&sc->ani_lock);
  2525. aphy->state = ATH_WIPHY_ACTIVE;
  2526. sc->sc_flags &= ~SC_OP_SCANNING;
  2527. sc->sc_flags |= SC_OP_FULL_RESET;
  2528. spin_unlock_bh(&sc->ani_lock);
  2529. ath_beacon_config(sc, NULL);
  2530. mutex_unlock(&sc->mutex);
  2531. }
  2532. struct ieee80211_ops ath9k_ops = {
  2533. .tx = ath9k_tx,
  2534. .start = ath9k_start,
  2535. .stop = ath9k_stop,
  2536. .add_interface = ath9k_add_interface,
  2537. .remove_interface = ath9k_remove_interface,
  2538. .config = ath9k_config,
  2539. .configure_filter = ath9k_configure_filter,
  2540. .sta_notify = ath9k_sta_notify,
  2541. .conf_tx = ath9k_conf_tx,
  2542. .bss_info_changed = ath9k_bss_info_changed,
  2543. .set_key = ath9k_set_key,
  2544. .get_tsf = ath9k_get_tsf,
  2545. .set_tsf = ath9k_set_tsf,
  2546. .reset_tsf = ath9k_reset_tsf,
  2547. .ampdu_action = ath9k_ampdu_action,
  2548. .sw_scan_start = ath9k_sw_scan_start,
  2549. .sw_scan_complete = ath9k_sw_scan_complete,
  2550. .rfkill_poll = ath9k_rfkill_poll_state,
  2551. };
  2552. static struct {
  2553. u32 version;
  2554. const char * name;
  2555. } ath_mac_bb_names[] = {
  2556. { AR_SREV_VERSION_5416_PCI, "5416" },
  2557. { AR_SREV_VERSION_5416_PCIE, "5418" },
  2558. { AR_SREV_VERSION_9100, "9100" },
  2559. { AR_SREV_VERSION_9160, "9160" },
  2560. { AR_SREV_VERSION_9280, "9280" },
  2561. { AR_SREV_VERSION_9285, "9285" },
  2562. { AR_SREV_VERSION_9287, "9287" }
  2563. };
  2564. static struct {
  2565. u16 version;
  2566. const char * name;
  2567. } ath_rf_names[] = {
  2568. { 0, "5133" },
  2569. { AR_RAD5133_SREV_MAJOR, "5133" },
  2570. { AR_RAD5122_SREV_MAJOR, "5122" },
  2571. { AR_RAD2133_SREV_MAJOR, "2133" },
  2572. { AR_RAD2122_SREV_MAJOR, "2122" }
  2573. };
  2574. /*
  2575. * Return the MAC/BB name. "????" is returned if the MAC/BB is unknown.
  2576. */
  2577. const char *
  2578. ath_mac_bb_name(u32 mac_bb_version)
  2579. {
  2580. int i;
  2581. for (i=0; i<ARRAY_SIZE(ath_mac_bb_names); i++) {
  2582. if (ath_mac_bb_names[i].version == mac_bb_version) {
  2583. return ath_mac_bb_names[i].name;
  2584. }
  2585. }
  2586. return "????";
  2587. }
  2588. /*
  2589. * Return the RF name. "????" is returned if the RF is unknown.
  2590. */
  2591. const char *
  2592. ath_rf_name(u16 rf_version)
  2593. {
  2594. int i;
  2595. for (i=0; i<ARRAY_SIZE(ath_rf_names); i++) {
  2596. if (ath_rf_names[i].version == rf_version) {
  2597. return ath_rf_names[i].name;
  2598. }
  2599. }
  2600. return "????";
  2601. }
  2602. static int __init ath9k_init(void)
  2603. {
  2604. int error;
  2605. /* Register rate control algorithm */
  2606. error = ath_rate_control_register();
  2607. if (error != 0) {
  2608. printk(KERN_ERR
  2609. "ath9k: Unable to register rate control "
  2610. "algorithm: %d\n",
  2611. error);
  2612. goto err_out;
  2613. }
  2614. error = ath9k_debug_create_root();
  2615. if (error) {
  2616. printk(KERN_ERR
  2617. "ath9k: Unable to create debugfs root: %d\n",
  2618. error);
  2619. goto err_rate_unregister;
  2620. }
  2621. error = ath_pci_init();
  2622. if (error < 0) {
  2623. printk(KERN_ERR
  2624. "ath9k: No PCI devices found, driver not installed.\n");
  2625. error = -ENODEV;
  2626. goto err_remove_root;
  2627. }
  2628. error = ath_ahb_init();
  2629. if (error < 0) {
  2630. error = -ENODEV;
  2631. goto err_pci_exit;
  2632. }
  2633. return 0;
  2634. err_pci_exit:
  2635. ath_pci_exit();
  2636. err_remove_root:
  2637. ath9k_debug_remove_root();
  2638. err_rate_unregister:
  2639. ath_rate_control_unregister();
  2640. err_out:
  2641. return error;
  2642. }
  2643. module_init(ath9k_init);
  2644. static void __exit ath9k_exit(void)
  2645. {
  2646. ath_ahb_exit();
  2647. ath_pci_exit();
  2648. ath9k_debug_remove_root();
  2649. ath_rate_control_unregister();
  2650. printk(KERN_INFO "%s: Driver unloaded\n", dev_info);
  2651. }
  2652. module_exit(ath9k_exit);