main.c 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750
  1. /*
  2. * Copyright (c) 2008 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. #define ATH_PCI_VERSION "0.1"
  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 = 30, \
  32. }
  33. #define CHAN5G(_freq, _idx) { \
  34. .band = IEEE80211_BAND_5GHZ, \
  35. .center_freq = (_freq), \
  36. .hw_value = (_idx), \
  37. .max_power = 30, \
  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. 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, ATH_DBG_CONFIG, "Rate: %2dMbps, ratecode: %2d\n",
  209. rate[i].bitrate / 10, rate[i].hw_value);
  210. }
  211. }
  212. /*
  213. * Set/change channels. If the channel is really being changed, it's done
  214. * by reseting the chip. To accomplish this we must first cleanup any pending
  215. * DMA, then restart stuff.
  216. */
  217. static int ath_set_channel(struct ath_softc *sc, struct ath9k_channel *hchan)
  218. {
  219. struct ath_hw *ah = sc->sc_ah;
  220. bool fastcc = true, stopped;
  221. struct ieee80211_hw *hw = sc->hw;
  222. struct ieee80211_channel *channel = hw->conf.channel;
  223. int r;
  224. if (sc->sc_flags & SC_OP_INVALID)
  225. return -EIO;
  226. ath9k_ps_wakeup(sc);
  227. /*
  228. * This is only performed if the channel settings have
  229. * actually changed.
  230. *
  231. * To switch channels clear any pending DMA operations;
  232. * wait long enough for the RX fifo to drain, reset the
  233. * hardware at the new frequency, and then re-enable
  234. * the relevant bits of the h/w.
  235. */
  236. ath9k_hw_set_interrupts(ah, 0);
  237. ath_drain_all_txq(sc, false);
  238. stopped = ath_stoprecv(sc);
  239. /* XXX: do not flush receive queue here. We don't want
  240. * to flush data frames already in queue because of
  241. * changing channel. */
  242. if (!stopped || (sc->sc_flags & SC_OP_FULL_RESET))
  243. fastcc = false;
  244. DPRINTF(sc, ATH_DBG_CONFIG,
  245. "(%u MHz) -> (%u MHz), chanwidth: %d\n",
  246. sc->sc_ah->curchan->channel,
  247. channel->center_freq, sc->tx_chan_width);
  248. spin_lock_bh(&sc->sc_resetlock);
  249. r = ath9k_hw_reset(ah, hchan, fastcc);
  250. if (r) {
  251. DPRINTF(sc, ATH_DBG_FATAL,
  252. "Unable to reset channel (%u Mhz) "
  253. "reset status %u\n",
  254. channel->center_freq, r);
  255. spin_unlock_bh(&sc->sc_resetlock);
  256. return r;
  257. }
  258. spin_unlock_bh(&sc->sc_resetlock);
  259. sc->sc_flags &= ~SC_OP_CHAINMASK_UPDATE;
  260. sc->sc_flags &= ~SC_OP_FULL_RESET;
  261. if (ath_startrecv(sc) != 0) {
  262. DPRINTF(sc, ATH_DBG_FATAL,
  263. "Unable to restart recv logic\n");
  264. return -EIO;
  265. }
  266. ath_cache_conf_rate(sc, &hw->conf);
  267. ath_update_txpow(sc);
  268. ath9k_hw_set_interrupts(ah, sc->imask);
  269. ath9k_ps_restore(sc);
  270. return 0;
  271. }
  272. /*
  273. * This routine performs the periodic noise floor calibration function
  274. * that is used to adjust and optimize the chip performance. This
  275. * takes environmental changes (location, temperature) into account.
  276. * When the task is complete, it reschedules itself depending on the
  277. * appropriate interval that was calculated.
  278. */
  279. static void ath_ani_calibrate(unsigned long data)
  280. {
  281. struct ath_softc *sc = (struct ath_softc *)data;
  282. struct ath_hw *ah = sc->sc_ah;
  283. bool longcal = false;
  284. bool shortcal = false;
  285. bool aniflag = false;
  286. unsigned int timestamp = jiffies_to_msecs(jiffies);
  287. u32 cal_interval, short_cal_interval;
  288. short_cal_interval = (ah->opmode == NL80211_IFTYPE_AP) ?
  289. ATH_AP_SHORT_CALINTERVAL : ATH_STA_SHORT_CALINTERVAL;
  290. /*
  291. * don't calibrate when we're scanning.
  292. * we are most likely not on our home channel.
  293. */
  294. if (sc->rx.rxfilter & FIF_BCN_PRBRESP_PROMISC)
  295. goto set_timer;
  296. /* Long calibration runs independently of short calibration. */
  297. if ((timestamp - sc->ani.longcal_timer) >= ATH_LONG_CALINTERVAL) {
  298. longcal = true;
  299. DPRINTF(sc, ATH_DBG_ANI, "longcal @%lu\n", jiffies);
  300. sc->ani.longcal_timer = timestamp;
  301. }
  302. /* Short calibration applies only while caldone is false */
  303. if (!sc->ani.caldone) {
  304. if ((timestamp - sc->ani.shortcal_timer) >= short_cal_interval) {
  305. shortcal = true;
  306. DPRINTF(sc, ATH_DBG_ANI, "shortcal @%lu\n", jiffies);
  307. sc->ani.shortcal_timer = timestamp;
  308. sc->ani.resetcal_timer = timestamp;
  309. }
  310. } else {
  311. if ((timestamp - sc->ani.resetcal_timer) >=
  312. ATH_RESTART_CALINTERVAL) {
  313. sc->ani.caldone = ath9k_hw_reset_calvalid(ah);
  314. if (sc->ani.caldone)
  315. sc->ani.resetcal_timer = timestamp;
  316. }
  317. }
  318. /* Verify whether we must check ANI */
  319. if ((timestamp - sc->ani.checkani_timer) >= ATH_ANI_POLLINTERVAL) {
  320. aniflag = true;
  321. sc->ani.checkani_timer = timestamp;
  322. }
  323. /* Skip all processing if there's nothing to do. */
  324. if (longcal || shortcal || aniflag) {
  325. /* Call ANI routine if necessary */
  326. if (aniflag)
  327. ath9k_hw_ani_monitor(ah, &sc->nodestats, ah->curchan);
  328. /* Perform calibration if necessary */
  329. if (longcal || shortcal) {
  330. bool iscaldone = false;
  331. if (ath9k_hw_calibrate(ah, ah->curchan,
  332. sc->rx_chainmask, longcal,
  333. &iscaldone)) {
  334. if (longcal)
  335. sc->ani.noise_floor =
  336. ath9k_hw_getchan_noise(ah,
  337. ah->curchan);
  338. DPRINTF(sc, ATH_DBG_ANI,
  339. "calibrate chan %u/%x nf: %d\n",
  340. ah->curchan->channel,
  341. ah->curchan->channelFlags,
  342. sc->ani.noise_floor);
  343. } else {
  344. DPRINTF(sc, ATH_DBG_ANY,
  345. "calibrate chan %u/%x failed\n",
  346. ah->curchan->channel,
  347. ah->curchan->channelFlags);
  348. }
  349. sc->ani.caldone = iscaldone;
  350. }
  351. }
  352. set_timer:
  353. /*
  354. * Set timer interval based on previous results.
  355. * The interval must be the shortest necessary to satisfy ANI,
  356. * short calibration and long calibration.
  357. */
  358. cal_interval = ATH_LONG_CALINTERVAL;
  359. if (sc->sc_ah->config.enable_ani)
  360. cal_interval = min(cal_interval, (u32)ATH_ANI_POLLINTERVAL);
  361. if (!sc->ani.caldone)
  362. cal_interval = min(cal_interval, (u32)short_cal_interval);
  363. mod_timer(&sc->ani.timer, jiffies + msecs_to_jiffies(cal_interval));
  364. }
  365. /*
  366. * Update tx/rx chainmask. For legacy association,
  367. * hard code chainmask to 1x1, for 11n association, use
  368. * the chainmask configuration, for bt coexistence, use
  369. * the chainmask configuration even in legacy mode.
  370. */
  371. static void ath_update_chainmask(struct ath_softc *sc, int is_ht)
  372. {
  373. sc->sc_flags |= SC_OP_CHAINMASK_UPDATE;
  374. if (is_ht ||
  375. (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_BT_COEX)) {
  376. sc->tx_chainmask = sc->sc_ah->caps.tx_chainmask;
  377. sc->rx_chainmask = sc->sc_ah->caps.rx_chainmask;
  378. } else {
  379. sc->tx_chainmask = 1;
  380. sc->rx_chainmask = 1;
  381. }
  382. DPRINTF(sc, ATH_DBG_CONFIG, "tx chmask: %d, rx chmask: %d\n",
  383. sc->tx_chainmask, sc->rx_chainmask);
  384. }
  385. static void ath_node_attach(struct ath_softc *sc, struct ieee80211_sta *sta)
  386. {
  387. struct ath_node *an;
  388. an = (struct ath_node *)sta->drv_priv;
  389. if (sc->sc_flags & SC_OP_TXAGGR)
  390. ath_tx_node_init(sc, an);
  391. an->maxampdu = 1 << (IEEE80211_HTCAP_MAXRXAMPDU_FACTOR +
  392. sta->ht_cap.ampdu_factor);
  393. an->mpdudensity = parse_mpdudensity(sta->ht_cap.ampdu_density);
  394. }
  395. static void ath_node_detach(struct ath_softc *sc, struct ieee80211_sta *sta)
  396. {
  397. struct ath_node *an = (struct ath_node *)sta->drv_priv;
  398. if (sc->sc_flags & SC_OP_TXAGGR)
  399. ath_tx_node_cleanup(sc, an);
  400. }
  401. static void ath9k_tasklet(unsigned long data)
  402. {
  403. struct ath_softc *sc = (struct ath_softc *)data;
  404. u32 status = sc->intrstatus;
  405. if (status & ATH9K_INT_FATAL) {
  406. /* need a chip reset */
  407. ath_reset(sc, false);
  408. return;
  409. } else {
  410. if (status &
  411. (ATH9K_INT_RX | ATH9K_INT_RXEOL | ATH9K_INT_RXORN)) {
  412. spin_lock_bh(&sc->rx.rxflushlock);
  413. ath_rx_tasklet(sc, 0);
  414. spin_unlock_bh(&sc->rx.rxflushlock);
  415. }
  416. /* XXX: optimize this */
  417. if (status & ATH9K_INT_TX)
  418. ath_tx_tasklet(sc);
  419. }
  420. /* re-enable hardware interrupt */
  421. ath9k_hw_set_interrupts(sc->sc_ah, sc->imask);
  422. }
  423. irqreturn_t ath_isr(int irq, void *dev)
  424. {
  425. struct ath_softc *sc = dev;
  426. struct ath_hw *ah = sc->sc_ah;
  427. enum ath9k_int status;
  428. bool sched = false;
  429. do {
  430. if (sc->sc_flags & SC_OP_INVALID) {
  431. /*
  432. * The hardware is not ready/present, don't
  433. * touch anything. Note this can happen early
  434. * on if the IRQ is shared.
  435. */
  436. return IRQ_NONE;
  437. }
  438. if (!ath9k_hw_intrpend(ah)) { /* shared irq, not for us */
  439. return IRQ_NONE;
  440. }
  441. /*
  442. * Figure out the reason(s) for the interrupt. Note
  443. * that the hal returns a pseudo-ISR that may include
  444. * bits we haven't explicitly enabled so we mask the
  445. * value to insure we only process bits we requested.
  446. */
  447. ath9k_hw_getisr(ah, &status); /* NB: clears ISR too */
  448. status &= sc->imask; /* discard unasked-for bits */
  449. /*
  450. * If there are no status bits set, then this interrupt was not
  451. * for me (should have been caught above).
  452. */
  453. if (!status)
  454. return IRQ_NONE;
  455. sc->intrstatus = status;
  456. ath9k_ps_wakeup(sc);
  457. if (status & ATH9K_INT_FATAL) {
  458. /* need a chip reset */
  459. sched = true;
  460. } else if (status & ATH9K_INT_RXORN) {
  461. /* need a chip reset */
  462. sched = true;
  463. } else {
  464. if (status & ATH9K_INT_SWBA) {
  465. /* schedule a tasklet for beacon handling */
  466. tasklet_schedule(&sc->bcon_tasklet);
  467. }
  468. if (status & ATH9K_INT_RXEOL) {
  469. /*
  470. * NB: the hardware should re-read the link when
  471. * RXE bit is written, but it doesn't work
  472. * at least on older hardware revs.
  473. */
  474. sched = true;
  475. }
  476. if (status & ATH9K_INT_TXURN)
  477. /* bump tx trigger level */
  478. ath9k_hw_updatetxtriglevel(ah, true);
  479. /* XXX: optimize this */
  480. if (status & ATH9K_INT_RX)
  481. sched = true;
  482. if (status & ATH9K_INT_TX)
  483. sched = true;
  484. if (status & ATH9K_INT_BMISS)
  485. sched = true;
  486. /* carrier sense timeout */
  487. if (status & ATH9K_INT_CST)
  488. sched = true;
  489. if (status & ATH9K_INT_MIB) {
  490. /*
  491. * Disable interrupts until we service the MIB
  492. * interrupt; otherwise it will continue to
  493. * fire.
  494. */
  495. ath9k_hw_set_interrupts(ah, 0);
  496. /*
  497. * Let the hal handle the event. We assume
  498. * it will clear whatever condition caused
  499. * the interrupt.
  500. */
  501. ath9k_hw_procmibevent(ah, &sc->nodestats);
  502. ath9k_hw_set_interrupts(ah, sc->imask);
  503. }
  504. if (status & ATH9K_INT_TIM_TIMER) {
  505. if (!(ah->caps.hw_caps &
  506. ATH9K_HW_CAP_AUTOSLEEP)) {
  507. /* Clear RxAbort bit so that we can
  508. * receive frames */
  509. ath9k_hw_setpower(ah, ATH9K_PM_AWAKE);
  510. ath9k_hw_setrxabort(ah, 0);
  511. sched = true;
  512. sc->sc_flags |= SC_OP_WAIT_FOR_BEACON;
  513. }
  514. }
  515. if (status & ATH9K_INT_TSFOOR) {
  516. /* FIXME: Handle this interrupt for power save */
  517. sched = true;
  518. }
  519. }
  520. ath9k_ps_restore(sc);
  521. } while (0);
  522. ath_debug_stat_interrupt(sc, status);
  523. if (sched) {
  524. /* turn off every interrupt except SWBA */
  525. ath9k_hw_set_interrupts(ah, (sc->imask & ATH9K_INT_SWBA));
  526. tasklet_schedule(&sc->intr_tq);
  527. }
  528. return IRQ_HANDLED;
  529. }
  530. static u32 ath_get_extchanmode(struct ath_softc *sc,
  531. struct ieee80211_channel *chan,
  532. enum nl80211_channel_type channel_type)
  533. {
  534. u32 chanmode = 0;
  535. switch (chan->band) {
  536. case IEEE80211_BAND_2GHZ:
  537. switch(channel_type) {
  538. case NL80211_CHAN_NO_HT:
  539. case NL80211_CHAN_HT20:
  540. chanmode = CHANNEL_G_HT20;
  541. break;
  542. case NL80211_CHAN_HT40PLUS:
  543. chanmode = CHANNEL_G_HT40PLUS;
  544. break;
  545. case NL80211_CHAN_HT40MINUS:
  546. chanmode = CHANNEL_G_HT40MINUS;
  547. break;
  548. }
  549. break;
  550. case IEEE80211_BAND_5GHZ:
  551. switch(channel_type) {
  552. case NL80211_CHAN_NO_HT:
  553. case NL80211_CHAN_HT20:
  554. chanmode = CHANNEL_A_HT20;
  555. break;
  556. case NL80211_CHAN_HT40PLUS:
  557. chanmode = CHANNEL_A_HT40PLUS;
  558. break;
  559. case NL80211_CHAN_HT40MINUS:
  560. chanmode = CHANNEL_A_HT40MINUS;
  561. break;
  562. }
  563. break;
  564. default:
  565. break;
  566. }
  567. return chanmode;
  568. }
  569. static int ath_keyset(struct ath_softc *sc, u16 keyix,
  570. struct ath9k_keyval *hk, const u8 mac[ETH_ALEN])
  571. {
  572. bool status;
  573. status = ath9k_hw_set_keycache_entry(sc->sc_ah,
  574. keyix, hk, mac);
  575. return status != false;
  576. }
  577. static int ath_setkey_tkip(struct ath_softc *sc, u16 keyix, const u8 *key,
  578. struct ath9k_keyval *hk, const u8 *addr,
  579. bool authenticator)
  580. {
  581. const u8 *key_rxmic;
  582. const u8 *key_txmic;
  583. key_txmic = key + NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY;
  584. key_rxmic = key + NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY;
  585. if (addr == NULL) {
  586. /* Group key installation */
  587. if (authenticator) {
  588. memcpy(hk->kv_mic, key_txmic, sizeof(hk->kv_mic));
  589. memcpy(hk->kv_txmic, key_txmic, sizeof(hk->kv_mic));
  590. } else {
  591. memcpy(hk->kv_mic, key_rxmic, sizeof(hk->kv_mic));
  592. memcpy(hk->kv_txmic, key_rxmic, sizeof(hk->kv_mic));
  593. }
  594. return ath_keyset(sc, keyix, hk, addr);
  595. }
  596. if (!sc->splitmic) {
  597. /*
  598. * data key goes at first index,
  599. * the hal handles the MIC keys at index+64.
  600. */
  601. memcpy(hk->kv_mic, key_rxmic, sizeof(hk->kv_mic));
  602. memcpy(hk->kv_txmic, key_txmic, sizeof(hk->kv_txmic));
  603. return ath_keyset(sc, keyix, hk, addr);
  604. }
  605. /*
  606. * TX key goes at first index, RX key at +32.
  607. * The hal handles the MIC keys at index+64.
  608. */
  609. memcpy(hk->kv_mic, key_txmic, sizeof(hk->kv_mic));
  610. if (!ath_keyset(sc, keyix, hk, NULL)) {
  611. /* Txmic entry failed. No need to proceed further */
  612. DPRINTF(sc, ATH_DBG_KEYCACHE,
  613. "Setting TX MIC Key Failed\n");
  614. return 0;
  615. }
  616. memcpy(hk->kv_mic, key_rxmic, sizeof(hk->kv_mic));
  617. /* XXX delete tx key on failure? */
  618. return ath_keyset(sc, keyix + 32, hk, addr);
  619. }
  620. static int ath_reserve_key_cache_slot_tkip(struct ath_softc *sc)
  621. {
  622. int i;
  623. for (i = IEEE80211_WEP_NKID; i < sc->keymax / 2; i++) {
  624. if (test_bit(i, sc->keymap) ||
  625. test_bit(i + 64, sc->keymap))
  626. continue; /* At least one part of TKIP key allocated */
  627. if (sc->splitmic &&
  628. (test_bit(i + 32, sc->keymap) ||
  629. test_bit(i + 64 + 32, sc->keymap)))
  630. continue; /* At least one part of TKIP key allocated */
  631. /* Found a free slot for a TKIP key */
  632. return i;
  633. }
  634. return -1;
  635. }
  636. static int ath_reserve_key_cache_slot(struct ath_softc *sc)
  637. {
  638. int i;
  639. /* First, try to find slots that would not be available for TKIP. */
  640. if (sc->splitmic) {
  641. for (i = IEEE80211_WEP_NKID; i < sc->keymax / 4; i++) {
  642. if (!test_bit(i, sc->keymap) &&
  643. (test_bit(i + 32, sc->keymap) ||
  644. test_bit(i + 64, sc->keymap) ||
  645. test_bit(i + 64 + 32, sc->keymap)))
  646. return i;
  647. if (!test_bit(i + 32, sc->keymap) &&
  648. (test_bit(i, sc->keymap) ||
  649. test_bit(i + 64, sc->keymap) ||
  650. test_bit(i + 64 + 32, sc->keymap)))
  651. return i + 32;
  652. if (!test_bit(i + 64, sc->keymap) &&
  653. (test_bit(i , sc->keymap) ||
  654. test_bit(i + 32, sc->keymap) ||
  655. test_bit(i + 64 + 32, sc->keymap)))
  656. return i + 64;
  657. if (!test_bit(i + 64 + 32, sc->keymap) &&
  658. (test_bit(i, sc->keymap) ||
  659. test_bit(i + 32, sc->keymap) ||
  660. test_bit(i + 64, sc->keymap)))
  661. return i + 64 + 32;
  662. }
  663. } else {
  664. for (i = IEEE80211_WEP_NKID; i < sc->keymax / 2; i++) {
  665. if (!test_bit(i, sc->keymap) &&
  666. test_bit(i + 64, sc->keymap))
  667. return i;
  668. if (test_bit(i, sc->keymap) &&
  669. !test_bit(i + 64, sc->keymap))
  670. return i + 64;
  671. }
  672. }
  673. /* No partially used TKIP slots, pick any available slot */
  674. for (i = IEEE80211_WEP_NKID; i < sc->keymax; i++) {
  675. /* Do not allow slots that could be needed for TKIP group keys
  676. * to be used. This limitation could be removed if we know that
  677. * TKIP will not be used. */
  678. if (i >= 64 && i < 64 + IEEE80211_WEP_NKID)
  679. continue;
  680. if (sc->splitmic) {
  681. if (i >= 32 && i < 32 + IEEE80211_WEP_NKID)
  682. continue;
  683. if (i >= 64 + 32 && i < 64 + 32 + IEEE80211_WEP_NKID)
  684. continue;
  685. }
  686. if (!test_bit(i, sc->keymap))
  687. return i; /* Found a free slot for a key */
  688. }
  689. /* No free slot found */
  690. return -1;
  691. }
  692. static int ath_key_config(struct ath_softc *sc,
  693. struct ieee80211_vif *vif,
  694. struct ieee80211_sta *sta,
  695. struct ieee80211_key_conf *key)
  696. {
  697. struct ath9k_keyval hk;
  698. const u8 *mac = NULL;
  699. int ret = 0;
  700. int idx;
  701. memset(&hk, 0, sizeof(hk));
  702. switch (key->alg) {
  703. case ALG_WEP:
  704. hk.kv_type = ATH9K_CIPHER_WEP;
  705. break;
  706. case ALG_TKIP:
  707. hk.kv_type = ATH9K_CIPHER_TKIP;
  708. break;
  709. case ALG_CCMP:
  710. hk.kv_type = ATH9K_CIPHER_AES_CCM;
  711. break;
  712. default:
  713. return -EOPNOTSUPP;
  714. }
  715. hk.kv_len = key->keylen;
  716. memcpy(hk.kv_val, key->key, key->keylen);
  717. if (!(key->flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
  718. /* For now, use the default keys for broadcast keys. This may
  719. * need to change with virtual interfaces. */
  720. idx = key->keyidx;
  721. } else if (key->keyidx) {
  722. struct ieee80211_vif *vif;
  723. if (WARN_ON(!sta))
  724. return -EOPNOTSUPP;
  725. mac = sta->addr;
  726. vif = sc->vifs[0];
  727. if (vif->type != NL80211_IFTYPE_AP) {
  728. /* Only keyidx 0 should be used with unicast key, but
  729. * allow this for client mode for now. */
  730. idx = key->keyidx;
  731. } else
  732. return -EIO;
  733. } else {
  734. if (WARN_ON(!sta))
  735. return -EOPNOTSUPP;
  736. mac = sta->addr;
  737. if (key->alg == ALG_TKIP)
  738. idx = ath_reserve_key_cache_slot_tkip(sc);
  739. else
  740. idx = ath_reserve_key_cache_slot(sc);
  741. if (idx < 0)
  742. return -ENOSPC; /* no free key cache entries */
  743. }
  744. if (key->alg == ALG_TKIP)
  745. ret = ath_setkey_tkip(sc, idx, key->key, &hk, mac,
  746. vif->type == NL80211_IFTYPE_AP);
  747. else
  748. ret = ath_keyset(sc, idx, &hk, mac);
  749. if (!ret)
  750. return -EIO;
  751. set_bit(idx, sc->keymap);
  752. if (key->alg == ALG_TKIP) {
  753. set_bit(idx + 64, sc->keymap);
  754. if (sc->splitmic) {
  755. set_bit(idx + 32, sc->keymap);
  756. set_bit(idx + 64 + 32, sc->keymap);
  757. }
  758. }
  759. return idx;
  760. }
  761. static void ath_key_delete(struct ath_softc *sc, struct ieee80211_key_conf *key)
  762. {
  763. ath9k_hw_keyreset(sc->sc_ah, key->hw_key_idx);
  764. if (key->hw_key_idx < IEEE80211_WEP_NKID)
  765. return;
  766. clear_bit(key->hw_key_idx, sc->keymap);
  767. if (key->alg != ALG_TKIP)
  768. return;
  769. clear_bit(key->hw_key_idx + 64, sc->keymap);
  770. if (sc->splitmic) {
  771. clear_bit(key->hw_key_idx + 32, sc->keymap);
  772. clear_bit(key->hw_key_idx + 64 + 32, sc->keymap);
  773. }
  774. }
  775. static void setup_ht_cap(struct ath_softc *sc,
  776. struct ieee80211_sta_ht_cap *ht_info)
  777. {
  778. #define ATH9K_HT_CAP_MAXRXAMPDU_65536 0x3 /* 2 ^ 16 */
  779. #define ATH9K_HT_CAP_MPDUDENSITY_8 0x6 /* 8 usec */
  780. ht_info->ht_supported = true;
  781. ht_info->cap = IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
  782. IEEE80211_HT_CAP_SM_PS |
  783. IEEE80211_HT_CAP_SGI_40 |
  784. IEEE80211_HT_CAP_DSSSCCK40;
  785. ht_info->ampdu_factor = ATH9K_HT_CAP_MAXRXAMPDU_65536;
  786. ht_info->ampdu_density = ATH9K_HT_CAP_MPDUDENSITY_8;
  787. /* set up supported mcs set */
  788. memset(&ht_info->mcs, 0, sizeof(ht_info->mcs));
  789. switch(sc->rx_chainmask) {
  790. case 1:
  791. ht_info->mcs.rx_mask[0] = 0xff;
  792. break;
  793. case 3:
  794. case 5:
  795. case 7:
  796. default:
  797. ht_info->mcs.rx_mask[0] = 0xff;
  798. ht_info->mcs.rx_mask[1] = 0xff;
  799. break;
  800. }
  801. ht_info->mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
  802. }
  803. static void ath9k_bss_assoc_info(struct ath_softc *sc,
  804. struct ieee80211_vif *vif,
  805. struct ieee80211_bss_conf *bss_conf)
  806. {
  807. struct ath_vif *avp = (void *)vif->drv_priv;
  808. if (bss_conf->assoc) {
  809. DPRINTF(sc, ATH_DBG_CONFIG, "Bss Info ASSOC %d, bssid: %pM\n",
  810. bss_conf->aid, sc->curbssid);
  811. /* New association, store aid */
  812. if (avp->av_opmode == NL80211_IFTYPE_STATION) {
  813. sc->curaid = bss_conf->aid;
  814. ath9k_hw_write_associd(sc);
  815. }
  816. /* Configure the beacon */
  817. ath_beacon_config(sc, 0);
  818. sc->sc_flags |= SC_OP_BEACONS;
  819. /* Reset rssi stats */
  820. sc->nodestats.ns_avgbrssi = ATH_RSSI_DUMMY_MARKER;
  821. sc->nodestats.ns_avgrssi = ATH_RSSI_DUMMY_MARKER;
  822. sc->nodestats.ns_avgtxrssi = ATH_RSSI_DUMMY_MARKER;
  823. sc->nodestats.ns_avgtxrate = ATH_RATE_DUMMY_MARKER;
  824. /* Start ANI */
  825. mod_timer(&sc->ani.timer,
  826. jiffies + msecs_to_jiffies(ATH_ANI_POLLINTERVAL));
  827. } else {
  828. DPRINTF(sc, ATH_DBG_CONFIG, "Bss Info DISSOC\n");
  829. sc->curaid = 0;
  830. }
  831. }
  832. /********************************/
  833. /* LED functions */
  834. /********************************/
  835. static void ath_led_blink_work(struct work_struct *work)
  836. {
  837. struct ath_softc *sc = container_of(work, struct ath_softc,
  838. ath_led_blink_work.work);
  839. if (!(sc->sc_flags & SC_OP_LED_ASSOCIATED))
  840. return;
  841. ath9k_hw_set_gpio(sc->sc_ah, ATH_LED_PIN,
  842. (sc->sc_flags & SC_OP_LED_ON) ? 1 : 0);
  843. queue_delayed_work(sc->hw->workqueue, &sc->ath_led_blink_work,
  844. (sc->sc_flags & SC_OP_LED_ON) ?
  845. msecs_to_jiffies(sc->led_off_duration) :
  846. msecs_to_jiffies(sc->led_on_duration));
  847. sc->led_on_duration =
  848. max((ATH_LED_ON_DURATION_IDLE - sc->led_on_cnt), 25);
  849. sc->led_off_duration =
  850. max((ATH_LED_OFF_DURATION_IDLE - sc->led_off_cnt), 10);
  851. sc->led_on_cnt = sc->led_off_cnt = 0;
  852. if (sc->sc_flags & SC_OP_LED_ON)
  853. sc->sc_flags &= ~SC_OP_LED_ON;
  854. else
  855. sc->sc_flags |= SC_OP_LED_ON;
  856. }
  857. static void ath_led_brightness(struct led_classdev *led_cdev,
  858. enum led_brightness brightness)
  859. {
  860. struct ath_led *led = container_of(led_cdev, struct ath_led, led_cdev);
  861. struct ath_softc *sc = led->sc;
  862. switch (brightness) {
  863. case LED_OFF:
  864. if (led->led_type == ATH_LED_ASSOC ||
  865. led->led_type == ATH_LED_RADIO) {
  866. ath9k_hw_set_gpio(sc->sc_ah, ATH_LED_PIN,
  867. (led->led_type == ATH_LED_RADIO));
  868. sc->sc_flags &= ~SC_OP_LED_ASSOCIATED;
  869. if (led->led_type == ATH_LED_RADIO)
  870. sc->sc_flags &= ~SC_OP_LED_ON;
  871. } else {
  872. sc->led_off_cnt++;
  873. }
  874. break;
  875. case LED_FULL:
  876. if (led->led_type == ATH_LED_ASSOC) {
  877. sc->sc_flags |= SC_OP_LED_ASSOCIATED;
  878. queue_delayed_work(sc->hw->workqueue,
  879. &sc->ath_led_blink_work, 0);
  880. } else if (led->led_type == ATH_LED_RADIO) {
  881. ath9k_hw_set_gpio(sc->sc_ah, ATH_LED_PIN, 0);
  882. sc->sc_flags |= SC_OP_LED_ON;
  883. } else {
  884. sc->led_on_cnt++;
  885. }
  886. break;
  887. default:
  888. break;
  889. }
  890. }
  891. static int ath_register_led(struct ath_softc *sc, struct ath_led *led,
  892. char *trigger)
  893. {
  894. int ret;
  895. led->sc = sc;
  896. led->led_cdev.name = led->name;
  897. led->led_cdev.default_trigger = trigger;
  898. led->led_cdev.brightness_set = ath_led_brightness;
  899. ret = led_classdev_register(wiphy_dev(sc->hw->wiphy), &led->led_cdev);
  900. if (ret)
  901. DPRINTF(sc, ATH_DBG_FATAL,
  902. "Failed to register led:%s", led->name);
  903. else
  904. led->registered = 1;
  905. return ret;
  906. }
  907. static void ath_unregister_led(struct ath_led *led)
  908. {
  909. if (led->registered) {
  910. led_classdev_unregister(&led->led_cdev);
  911. led->registered = 0;
  912. }
  913. }
  914. static void ath_deinit_leds(struct ath_softc *sc)
  915. {
  916. cancel_delayed_work_sync(&sc->ath_led_blink_work);
  917. ath_unregister_led(&sc->assoc_led);
  918. sc->sc_flags &= ~SC_OP_LED_ASSOCIATED;
  919. ath_unregister_led(&sc->tx_led);
  920. ath_unregister_led(&sc->rx_led);
  921. ath_unregister_led(&sc->radio_led);
  922. ath9k_hw_set_gpio(sc->sc_ah, ATH_LED_PIN, 1);
  923. }
  924. static void ath_init_leds(struct ath_softc *sc)
  925. {
  926. char *trigger;
  927. int ret;
  928. /* Configure gpio 1 for output */
  929. ath9k_hw_cfg_output(sc->sc_ah, ATH_LED_PIN,
  930. AR_GPIO_OUTPUT_MUX_AS_OUTPUT);
  931. /* LED off, active low */
  932. ath9k_hw_set_gpio(sc->sc_ah, ATH_LED_PIN, 1);
  933. INIT_DELAYED_WORK(&sc->ath_led_blink_work, ath_led_blink_work);
  934. trigger = ieee80211_get_radio_led_name(sc->hw);
  935. snprintf(sc->radio_led.name, sizeof(sc->radio_led.name),
  936. "ath9k-%s::radio", wiphy_name(sc->hw->wiphy));
  937. ret = ath_register_led(sc, &sc->radio_led, trigger);
  938. sc->radio_led.led_type = ATH_LED_RADIO;
  939. if (ret)
  940. goto fail;
  941. trigger = ieee80211_get_assoc_led_name(sc->hw);
  942. snprintf(sc->assoc_led.name, sizeof(sc->assoc_led.name),
  943. "ath9k-%s::assoc", wiphy_name(sc->hw->wiphy));
  944. ret = ath_register_led(sc, &sc->assoc_led, trigger);
  945. sc->assoc_led.led_type = ATH_LED_ASSOC;
  946. if (ret)
  947. goto fail;
  948. trigger = ieee80211_get_tx_led_name(sc->hw);
  949. snprintf(sc->tx_led.name, sizeof(sc->tx_led.name),
  950. "ath9k-%s::tx", wiphy_name(sc->hw->wiphy));
  951. ret = ath_register_led(sc, &sc->tx_led, trigger);
  952. sc->tx_led.led_type = ATH_LED_TX;
  953. if (ret)
  954. goto fail;
  955. trigger = ieee80211_get_rx_led_name(sc->hw);
  956. snprintf(sc->rx_led.name, sizeof(sc->rx_led.name),
  957. "ath9k-%s::rx", wiphy_name(sc->hw->wiphy));
  958. ret = ath_register_led(sc, &sc->rx_led, trigger);
  959. sc->rx_led.led_type = ATH_LED_RX;
  960. if (ret)
  961. goto fail;
  962. return;
  963. fail:
  964. ath_deinit_leds(sc);
  965. }
  966. #if defined(CONFIG_RFKILL) || defined(CONFIG_RFKILL_MODULE)
  967. /*******************/
  968. /* Rfkill */
  969. /*******************/
  970. static void ath_radio_enable(struct ath_softc *sc)
  971. {
  972. struct ath_hw *ah = sc->sc_ah;
  973. struct ieee80211_channel *channel = sc->hw->conf.channel;
  974. int r;
  975. ath9k_ps_wakeup(sc);
  976. spin_lock_bh(&sc->sc_resetlock);
  977. r = ath9k_hw_reset(ah, ah->curchan, false);
  978. if (r) {
  979. DPRINTF(sc, ATH_DBG_FATAL,
  980. "Unable to reset channel %u (%uMhz) ",
  981. "reset status %u\n",
  982. channel->center_freq, r);
  983. }
  984. spin_unlock_bh(&sc->sc_resetlock);
  985. ath_update_txpow(sc);
  986. if (ath_startrecv(sc) != 0) {
  987. DPRINTF(sc, ATH_DBG_FATAL,
  988. "Unable to restart recv logic\n");
  989. return;
  990. }
  991. if (sc->sc_flags & SC_OP_BEACONS)
  992. ath_beacon_config(sc, ATH_IF_ID_ANY); /* restart beacons */
  993. /* Re-Enable interrupts */
  994. ath9k_hw_set_interrupts(ah, sc->imask);
  995. /* Enable LED */
  996. ath9k_hw_cfg_output(ah, ATH_LED_PIN,
  997. AR_GPIO_OUTPUT_MUX_AS_OUTPUT);
  998. ath9k_hw_set_gpio(ah, ATH_LED_PIN, 0);
  999. ieee80211_wake_queues(sc->hw);
  1000. ath9k_ps_restore(sc);
  1001. }
  1002. static void ath_radio_disable(struct ath_softc *sc)
  1003. {
  1004. struct ath_hw *ah = sc->sc_ah;
  1005. struct ieee80211_channel *channel = sc->hw->conf.channel;
  1006. int r;
  1007. ath9k_ps_wakeup(sc);
  1008. ieee80211_stop_queues(sc->hw);
  1009. /* Disable LED */
  1010. ath9k_hw_set_gpio(ah, ATH_LED_PIN, 1);
  1011. ath9k_hw_cfg_gpio_input(ah, ATH_LED_PIN);
  1012. /* Disable interrupts */
  1013. ath9k_hw_set_interrupts(ah, 0);
  1014. ath_drain_all_txq(sc, false); /* clear pending tx frames */
  1015. ath_stoprecv(sc); /* turn off frame recv */
  1016. ath_flushrecv(sc); /* flush recv queue */
  1017. spin_lock_bh(&sc->sc_resetlock);
  1018. r = ath9k_hw_reset(ah, ah->curchan, false);
  1019. if (r) {
  1020. DPRINTF(sc, ATH_DBG_FATAL,
  1021. "Unable to reset channel %u (%uMhz) "
  1022. "reset status %u\n",
  1023. channel->center_freq, r);
  1024. }
  1025. spin_unlock_bh(&sc->sc_resetlock);
  1026. ath9k_hw_phy_disable(ah);
  1027. ath9k_hw_setpower(ah, ATH9K_PM_FULL_SLEEP);
  1028. ath9k_ps_restore(sc);
  1029. }
  1030. static bool ath_is_rfkill_set(struct ath_softc *sc)
  1031. {
  1032. struct ath_hw *ah = sc->sc_ah;
  1033. return ath9k_hw_gpio_get(ah, ah->rfkill_gpio) ==
  1034. ah->rfkill_polarity;
  1035. }
  1036. /* h/w rfkill poll function */
  1037. static void ath_rfkill_poll(struct work_struct *work)
  1038. {
  1039. struct ath_softc *sc = container_of(work, struct ath_softc,
  1040. rf_kill.rfkill_poll.work);
  1041. bool radio_on;
  1042. if (sc->sc_flags & SC_OP_INVALID)
  1043. return;
  1044. radio_on = !ath_is_rfkill_set(sc);
  1045. /*
  1046. * enable/disable radio only when there is a
  1047. * state change in RF switch
  1048. */
  1049. if (radio_on == !!(sc->sc_flags & SC_OP_RFKILL_HW_BLOCKED)) {
  1050. enum rfkill_state state;
  1051. if (sc->sc_flags & SC_OP_RFKILL_SW_BLOCKED) {
  1052. state = radio_on ? RFKILL_STATE_SOFT_BLOCKED
  1053. : RFKILL_STATE_HARD_BLOCKED;
  1054. } else if (radio_on) {
  1055. ath_radio_enable(sc);
  1056. state = RFKILL_STATE_UNBLOCKED;
  1057. } else {
  1058. ath_radio_disable(sc);
  1059. state = RFKILL_STATE_HARD_BLOCKED;
  1060. }
  1061. if (state == RFKILL_STATE_HARD_BLOCKED)
  1062. sc->sc_flags |= SC_OP_RFKILL_HW_BLOCKED;
  1063. else
  1064. sc->sc_flags &= ~SC_OP_RFKILL_HW_BLOCKED;
  1065. rfkill_force_state(sc->rf_kill.rfkill, state);
  1066. }
  1067. queue_delayed_work(sc->hw->workqueue, &sc->rf_kill.rfkill_poll,
  1068. msecs_to_jiffies(ATH_RFKILL_POLL_INTERVAL));
  1069. }
  1070. /* s/w rfkill handler */
  1071. static int ath_sw_toggle_radio(void *data, enum rfkill_state state)
  1072. {
  1073. struct ath_softc *sc = data;
  1074. switch (state) {
  1075. case RFKILL_STATE_SOFT_BLOCKED:
  1076. if (!(sc->sc_flags & (SC_OP_RFKILL_HW_BLOCKED |
  1077. SC_OP_RFKILL_SW_BLOCKED)))
  1078. ath_radio_disable(sc);
  1079. sc->sc_flags |= SC_OP_RFKILL_SW_BLOCKED;
  1080. return 0;
  1081. case RFKILL_STATE_UNBLOCKED:
  1082. if ((sc->sc_flags & SC_OP_RFKILL_SW_BLOCKED)) {
  1083. sc->sc_flags &= ~SC_OP_RFKILL_SW_BLOCKED;
  1084. if (sc->sc_flags & SC_OP_RFKILL_HW_BLOCKED) {
  1085. DPRINTF(sc, ATH_DBG_FATAL, "Can't turn on the"
  1086. "radio as it is disabled by h/w\n");
  1087. return -EPERM;
  1088. }
  1089. ath_radio_enable(sc);
  1090. }
  1091. return 0;
  1092. default:
  1093. return -EINVAL;
  1094. }
  1095. }
  1096. /* Init s/w rfkill */
  1097. static int ath_init_sw_rfkill(struct ath_softc *sc)
  1098. {
  1099. sc->rf_kill.rfkill = rfkill_allocate(wiphy_dev(sc->hw->wiphy),
  1100. RFKILL_TYPE_WLAN);
  1101. if (!sc->rf_kill.rfkill) {
  1102. DPRINTF(sc, ATH_DBG_FATAL, "Failed to allocate rfkill\n");
  1103. return -ENOMEM;
  1104. }
  1105. snprintf(sc->rf_kill.rfkill_name, sizeof(sc->rf_kill.rfkill_name),
  1106. "ath9k-%s::rfkill", wiphy_name(sc->hw->wiphy));
  1107. sc->rf_kill.rfkill->name = sc->rf_kill.rfkill_name;
  1108. sc->rf_kill.rfkill->data = sc;
  1109. sc->rf_kill.rfkill->toggle_radio = ath_sw_toggle_radio;
  1110. sc->rf_kill.rfkill->state = RFKILL_STATE_UNBLOCKED;
  1111. sc->rf_kill.rfkill->user_claim_unsupported = 1;
  1112. return 0;
  1113. }
  1114. /* Deinitialize rfkill */
  1115. static void ath_deinit_rfkill(struct ath_softc *sc)
  1116. {
  1117. if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_RFSILENT)
  1118. cancel_delayed_work_sync(&sc->rf_kill.rfkill_poll);
  1119. if (sc->sc_flags & SC_OP_RFKILL_REGISTERED) {
  1120. rfkill_unregister(sc->rf_kill.rfkill);
  1121. sc->sc_flags &= ~SC_OP_RFKILL_REGISTERED;
  1122. sc->rf_kill.rfkill = NULL;
  1123. }
  1124. }
  1125. static int ath_start_rfkill_poll(struct ath_softc *sc)
  1126. {
  1127. if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_RFSILENT)
  1128. queue_delayed_work(sc->hw->workqueue,
  1129. &sc->rf_kill.rfkill_poll, 0);
  1130. if (!(sc->sc_flags & SC_OP_RFKILL_REGISTERED)) {
  1131. if (rfkill_register(sc->rf_kill.rfkill)) {
  1132. DPRINTF(sc, ATH_DBG_FATAL,
  1133. "Unable to register rfkill\n");
  1134. rfkill_free(sc->rf_kill.rfkill);
  1135. /* Deinitialize the device */
  1136. ath_cleanup(sc);
  1137. return -EIO;
  1138. } else {
  1139. sc->sc_flags |= SC_OP_RFKILL_REGISTERED;
  1140. }
  1141. }
  1142. return 0;
  1143. }
  1144. #endif /* CONFIG_RFKILL */
  1145. void ath_cleanup(struct ath_softc *sc)
  1146. {
  1147. ath_detach(sc);
  1148. free_irq(sc->irq, sc);
  1149. ath_bus_cleanup(sc);
  1150. ieee80211_free_hw(sc->hw);
  1151. }
  1152. void ath_detach(struct ath_softc *sc)
  1153. {
  1154. struct ieee80211_hw *hw = sc->hw;
  1155. int i = 0;
  1156. ath9k_ps_wakeup(sc);
  1157. DPRINTF(sc, ATH_DBG_CONFIG, "Detach ATH hw\n");
  1158. #if defined(CONFIG_RFKILL) || defined(CONFIG_RFKILL_MODULE)
  1159. ath_deinit_rfkill(sc);
  1160. #endif
  1161. ath_deinit_leds(sc);
  1162. ieee80211_unregister_hw(hw);
  1163. ath_rx_cleanup(sc);
  1164. ath_tx_cleanup(sc);
  1165. tasklet_kill(&sc->intr_tq);
  1166. tasklet_kill(&sc->bcon_tasklet);
  1167. if (!(sc->sc_flags & SC_OP_INVALID))
  1168. ath9k_hw_setpower(sc->sc_ah, ATH9K_PM_AWAKE);
  1169. /* cleanup tx queues */
  1170. for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++)
  1171. if (ATH_TXQ_SETUP(sc, i))
  1172. ath_tx_cleanupq(sc, &sc->tx.txq[i]);
  1173. ath9k_hw_detach(sc->sc_ah);
  1174. ath9k_exit_debug(sc);
  1175. ath9k_ps_restore(sc);
  1176. }
  1177. static int ath_init(u16 devid, struct ath_softc *sc)
  1178. {
  1179. struct ath_hw *ah = NULL;
  1180. int status;
  1181. int error = 0, i;
  1182. int csz = 0;
  1183. /* XXX: hardware will not be ready until ath_open() being called */
  1184. sc->sc_flags |= SC_OP_INVALID;
  1185. if (ath9k_init_debug(sc) < 0)
  1186. printk(KERN_ERR "Unable to create debugfs files\n");
  1187. spin_lock_init(&sc->sc_resetlock);
  1188. mutex_init(&sc->mutex);
  1189. tasklet_init(&sc->intr_tq, ath9k_tasklet, (unsigned long)sc);
  1190. tasklet_init(&sc->bcon_tasklet, ath9k_beacon_tasklet,
  1191. (unsigned long)sc);
  1192. /*
  1193. * Cache line size is used to size and align various
  1194. * structures used to communicate with the hardware.
  1195. */
  1196. ath_read_cachesize(sc, &csz);
  1197. /* XXX assert csz is non-zero */
  1198. sc->cachelsz = csz << 2; /* convert to bytes */
  1199. ah = ath9k_hw_attach(devid, sc, &status);
  1200. if (ah == NULL) {
  1201. DPRINTF(sc, ATH_DBG_FATAL,
  1202. "Unable to attach hardware; HAL status %d\n", status);
  1203. error = -ENXIO;
  1204. goto bad;
  1205. }
  1206. sc->sc_ah = ah;
  1207. /* Get the hardware key cache size. */
  1208. sc->keymax = ah->caps.keycache_size;
  1209. if (sc->keymax > ATH_KEYMAX) {
  1210. DPRINTF(sc, ATH_DBG_KEYCACHE,
  1211. "Warning, using only %u entries in %u key cache\n",
  1212. ATH_KEYMAX, sc->keymax);
  1213. sc->keymax = ATH_KEYMAX;
  1214. }
  1215. /*
  1216. * Reset the key cache since some parts do not
  1217. * reset the contents on initial power up.
  1218. */
  1219. for (i = 0; i < sc->keymax; i++)
  1220. ath9k_hw_keyreset(ah, (u16) i);
  1221. if (ath9k_regd_init(sc->sc_ah))
  1222. goto bad;
  1223. /* default to MONITOR mode */
  1224. sc->sc_ah->opmode = NL80211_IFTYPE_MONITOR;
  1225. /* Setup rate tables */
  1226. ath_rate_attach(sc);
  1227. ath_setup_rates(sc, IEEE80211_BAND_2GHZ);
  1228. ath_setup_rates(sc, IEEE80211_BAND_5GHZ);
  1229. /*
  1230. * Allocate hardware transmit queues: one queue for
  1231. * beacon frames and one data queue for each QoS
  1232. * priority. Note that the hal handles reseting
  1233. * these queues at the needed time.
  1234. */
  1235. sc->beacon.beaconq = ath_beaconq_setup(ah);
  1236. if (sc->beacon.beaconq == -1) {
  1237. DPRINTF(sc, ATH_DBG_FATAL,
  1238. "Unable to setup a beacon xmit queue\n");
  1239. error = -EIO;
  1240. goto bad2;
  1241. }
  1242. sc->beacon.cabq = ath_txq_setup(sc, ATH9K_TX_QUEUE_CAB, 0);
  1243. if (sc->beacon.cabq == NULL) {
  1244. DPRINTF(sc, ATH_DBG_FATAL,
  1245. "Unable to setup CAB xmit queue\n");
  1246. error = -EIO;
  1247. goto bad2;
  1248. }
  1249. sc->config.cabqReadytime = ATH_CABQ_READY_TIME;
  1250. ath_cabq_update(sc);
  1251. for (i = 0; i < ARRAY_SIZE(sc->tx.hwq_map); i++)
  1252. sc->tx.hwq_map[i] = -1;
  1253. /* Setup data queues */
  1254. /* NB: ensure BK queue is the lowest priority h/w queue */
  1255. if (!ath_tx_setup(sc, ATH9K_WME_AC_BK)) {
  1256. DPRINTF(sc, ATH_DBG_FATAL,
  1257. "Unable to setup xmit queue for BK traffic\n");
  1258. error = -EIO;
  1259. goto bad2;
  1260. }
  1261. if (!ath_tx_setup(sc, ATH9K_WME_AC_BE)) {
  1262. DPRINTF(sc, ATH_DBG_FATAL,
  1263. "Unable to setup xmit queue for BE traffic\n");
  1264. error = -EIO;
  1265. goto bad2;
  1266. }
  1267. if (!ath_tx_setup(sc, ATH9K_WME_AC_VI)) {
  1268. DPRINTF(sc, ATH_DBG_FATAL,
  1269. "Unable to setup xmit queue for VI traffic\n");
  1270. error = -EIO;
  1271. goto bad2;
  1272. }
  1273. if (!ath_tx_setup(sc, ATH9K_WME_AC_VO)) {
  1274. DPRINTF(sc, ATH_DBG_FATAL,
  1275. "Unable to setup xmit queue for VO traffic\n");
  1276. error = -EIO;
  1277. goto bad2;
  1278. }
  1279. /* Initializes the noise floor to a reasonable default value.
  1280. * Later on this will be updated during ANI processing. */
  1281. sc->ani.noise_floor = ATH_DEFAULT_NOISE_FLOOR;
  1282. setup_timer(&sc->ani.timer, ath_ani_calibrate, (unsigned long)sc);
  1283. if (ath9k_hw_getcapability(ah, ATH9K_CAP_CIPHER,
  1284. ATH9K_CIPHER_TKIP, NULL)) {
  1285. /*
  1286. * Whether we should enable h/w TKIP MIC.
  1287. * XXX: if we don't support WME TKIP MIC, then we wouldn't
  1288. * report WMM capable, so it's always safe to turn on
  1289. * TKIP MIC in this case.
  1290. */
  1291. ath9k_hw_setcapability(sc->sc_ah, ATH9K_CAP_TKIP_MIC,
  1292. 0, 1, NULL);
  1293. }
  1294. /*
  1295. * Check whether the separate key cache entries
  1296. * are required to handle both tx+rx MIC keys.
  1297. * With split mic keys the number of stations is limited
  1298. * to 27 otherwise 59.
  1299. */
  1300. if (ath9k_hw_getcapability(ah, ATH9K_CAP_CIPHER,
  1301. ATH9K_CIPHER_TKIP, NULL)
  1302. && ath9k_hw_getcapability(ah, ATH9K_CAP_CIPHER,
  1303. ATH9K_CIPHER_MIC, NULL)
  1304. && ath9k_hw_getcapability(ah, ATH9K_CAP_TKIP_SPLIT,
  1305. 0, NULL))
  1306. sc->splitmic = 1;
  1307. /* turn on mcast key search if possible */
  1308. if (!ath9k_hw_getcapability(ah, ATH9K_CAP_MCAST_KEYSRCH, 0, NULL))
  1309. (void)ath9k_hw_setcapability(ah, ATH9K_CAP_MCAST_KEYSRCH, 1,
  1310. 1, NULL);
  1311. sc->config.txpowlimit = ATH_TXPOWER_MAX;
  1312. /* 11n Capabilities */
  1313. if (ah->caps.hw_caps & ATH9K_HW_CAP_HT) {
  1314. sc->sc_flags |= SC_OP_TXAGGR;
  1315. sc->sc_flags |= SC_OP_RXAGGR;
  1316. }
  1317. sc->tx_chainmask = ah->caps.tx_chainmask;
  1318. sc->rx_chainmask = ah->caps.rx_chainmask;
  1319. ath9k_hw_setcapability(ah, ATH9K_CAP_DIVERSITY, 1, true, NULL);
  1320. sc->rx.defant = ath9k_hw_getdefantenna(ah);
  1321. if (ah->caps.hw_caps & ATH9K_HW_CAP_BSSIDMASK) {
  1322. memcpy(sc->bssidmask, ath_bcast_mac, ETH_ALEN);
  1323. ATH_SET_VIF_BSSID_MASK(sc->bssidmask);
  1324. ath9k_hw_setbssidmask(sc);
  1325. }
  1326. sc->beacon.slottime = ATH9K_SLOT_TIME_9; /* default to short slot time */
  1327. /* initialize beacon slots */
  1328. for (i = 0; i < ARRAY_SIZE(sc->beacon.bslot); i++)
  1329. sc->beacon.bslot[i] = ATH_IF_ID_ANY;
  1330. /* save MISC configurations */
  1331. sc->config.swBeaconProcess = 1;
  1332. /* setup channels and rates */
  1333. sc->sbands[IEEE80211_BAND_2GHZ].channels = ath9k_2ghz_chantable;
  1334. sc->sbands[IEEE80211_BAND_2GHZ].bitrates =
  1335. sc->rates[IEEE80211_BAND_2GHZ];
  1336. sc->sbands[IEEE80211_BAND_2GHZ].band = IEEE80211_BAND_2GHZ;
  1337. sc->sbands[IEEE80211_BAND_2GHZ].n_channels =
  1338. ARRAY_SIZE(ath9k_2ghz_chantable);
  1339. if (test_bit(ATH9K_MODE_11A, sc->sc_ah->caps.wireless_modes)) {
  1340. sc->sbands[IEEE80211_BAND_5GHZ].channels = ath9k_5ghz_chantable;
  1341. sc->sbands[IEEE80211_BAND_5GHZ].bitrates =
  1342. sc->rates[IEEE80211_BAND_5GHZ];
  1343. sc->sbands[IEEE80211_BAND_5GHZ].band = IEEE80211_BAND_5GHZ;
  1344. sc->sbands[IEEE80211_BAND_5GHZ].n_channels =
  1345. ARRAY_SIZE(ath9k_5ghz_chantable);
  1346. }
  1347. if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_BT_COEX)
  1348. ath9k_hw_btcoex_enable(sc->sc_ah);
  1349. return 0;
  1350. bad2:
  1351. /* cleanup tx queues */
  1352. for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++)
  1353. if (ATH_TXQ_SETUP(sc, i))
  1354. ath_tx_cleanupq(sc, &sc->tx.txq[i]);
  1355. bad:
  1356. if (ah)
  1357. ath9k_hw_detach(ah);
  1358. ath9k_exit_debug(sc);
  1359. return error;
  1360. }
  1361. int ath_attach(u16 devid, struct ath_softc *sc)
  1362. {
  1363. struct ieee80211_hw *hw = sc->hw;
  1364. const struct ieee80211_regdomain *regd;
  1365. int error = 0, i;
  1366. DPRINTF(sc, ATH_DBG_CONFIG, "Attach ATH hw\n");
  1367. error = ath_init(devid, sc);
  1368. if (error != 0)
  1369. return error;
  1370. /* get mac address from hardware and set in mac80211 */
  1371. SET_IEEE80211_PERM_ADDR(hw, sc->sc_ah->macaddr);
  1372. hw->flags = IEEE80211_HW_RX_INCLUDES_FCS |
  1373. IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING |
  1374. IEEE80211_HW_SIGNAL_DBM |
  1375. IEEE80211_HW_AMPDU_AGGREGATION |
  1376. IEEE80211_HW_SUPPORTS_PS |
  1377. IEEE80211_HW_PS_NULLFUNC_STACK;
  1378. if (AR_SREV_9160_10_OR_LATER(sc->sc_ah) || modparam_nohwcrypt)
  1379. hw->flags |= IEEE80211_HW_MFP_CAPABLE;
  1380. hw->wiphy->interface_modes =
  1381. BIT(NL80211_IFTYPE_AP) |
  1382. BIT(NL80211_IFTYPE_STATION) |
  1383. BIT(NL80211_IFTYPE_ADHOC);
  1384. hw->wiphy->reg_notifier = ath9k_reg_notifier;
  1385. hw->wiphy->strict_regulatory = true;
  1386. hw->queues = 4;
  1387. hw->max_rates = 4;
  1388. hw->channel_change_time = 5000;
  1389. hw->max_rate_tries = ATH_11N_TXMAXTRY;
  1390. hw->sta_data_size = sizeof(struct ath_node);
  1391. hw->vif_data_size = sizeof(struct ath_vif);
  1392. hw->rate_control_algorithm = "ath9k_rate_control";
  1393. if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_HT) {
  1394. setup_ht_cap(sc, &sc->sbands[IEEE80211_BAND_2GHZ].ht_cap);
  1395. if (test_bit(ATH9K_MODE_11A, sc->sc_ah->caps.wireless_modes))
  1396. setup_ht_cap(sc, &sc->sbands[IEEE80211_BAND_5GHZ].ht_cap);
  1397. }
  1398. hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &sc->sbands[IEEE80211_BAND_2GHZ];
  1399. if (test_bit(ATH9K_MODE_11A, sc->sc_ah->caps.wireless_modes))
  1400. hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
  1401. &sc->sbands[IEEE80211_BAND_5GHZ];
  1402. /* initialize tx/rx engine */
  1403. error = ath_tx_init(sc, ATH_TXBUF);
  1404. if (error != 0)
  1405. goto error_attach;
  1406. error = ath_rx_init(sc, ATH_RXBUF);
  1407. if (error != 0)
  1408. goto error_attach;
  1409. #if defined(CONFIG_RFKILL) || defined(CONFIG_RFKILL_MODULE)
  1410. /* Initialze h/w Rfkill */
  1411. if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_RFSILENT)
  1412. INIT_DELAYED_WORK(&sc->rf_kill.rfkill_poll, ath_rfkill_poll);
  1413. /* Initialize s/w rfkill */
  1414. error = ath_init_sw_rfkill(sc);
  1415. if (error)
  1416. goto error_attach;
  1417. #endif
  1418. if (ath9k_is_world_regd(sc->sc_ah)) {
  1419. /* Anything applied here (prior to wiphy registration) gets
  1420. * saved on the wiphy orig_* parameters */
  1421. regd = ath9k_world_regdomain(sc->sc_ah);
  1422. hw->wiphy->custom_regulatory = true;
  1423. hw->wiphy->strict_regulatory = false;
  1424. } else {
  1425. /* This gets applied in the case of the absense of CRDA,
  1426. * it's our own custom world regulatory domain, similar to
  1427. * cfg80211's but we enable passive scanning */
  1428. regd = ath9k_default_world_regdomain();
  1429. }
  1430. wiphy_apply_custom_regulatory(hw->wiphy, regd);
  1431. ath9k_reg_apply_radar_flags(hw->wiphy);
  1432. ath9k_reg_apply_world_flags(hw->wiphy, REGDOM_SET_BY_INIT);
  1433. error = ieee80211_register_hw(hw);
  1434. if (!ath9k_is_world_regd(sc->sc_ah)) {
  1435. error = regulatory_hint(hw->wiphy,
  1436. sc->sc_ah->regulatory.alpha2);
  1437. if (error)
  1438. goto error_attach;
  1439. }
  1440. /* Initialize LED control */
  1441. ath_init_leds(sc);
  1442. return 0;
  1443. error_attach:
  1444. /* cleanup tx queues */
  1445. for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++)
  1446. if (ATH_TXQ_SETUP(sc, i))
  1447. ath_tx_cleanupq(sc, &sc->tx.txq[i]);
  1448. ath9k_hw_detach(sc->sc_ah);
  1449. ath9k_exit_debug(sc);
  1450. return error;
  1451. }
  1452. int ath_reset(struct ath_softc *sc, bool retry_tx)
  1453. {
  1454. struct ath_hw *ah = sc->sc_ah;
  1455. struct ieee80211_hw *hw = sc->hw;
  1456. int r;
  1457. ath9k_hw_set_interrupts(ah, 0);
  1458. ath_drain_all_txq(sc, retry_tx);
  1459. ath_stoprecv(sc);
  1460. ath_flushrecv(sc);
  1461. spin_lock_bh(&sc->sc_resetlock);
  1462. r = ath9k_hw_reset(ah, sc->sc_ah->curchan, false);
  1463. if (r)
  1464. DPRINTF(sc, ATH_DBG_FATAL,
  1465. "Unable to reset hardware; reset status %u\n", r);
  1466. spin_unlock_bh(&sc->sc_resetlock);
  1467. if (ath_startrecv(sc) != 0)
  1468. DPRINTF(sc, ATH_DBG_FATAL, "Unable to start recv logic\n");
  1469. /*
  1470. * We may be doing a reset in response to a request
  1471. * that changes the channel so update any state that
  1472. * might change as a result.
  1473. */
  1474. ath_cache_conf_rate(sc, &hw->conf);
  1475. ath_update_txpow(sc);
  1476. if (sc->sc_flags & SC_OP_BEACONS)
  1477. ath_beacon_config(sc, ATH_IF_ID_ANY); /* restart beacons */
  1478. ath9k_hw_set_interrupts(ah, sc->imask);
  1479. if (retry_tx) {
  1480. int i;
  1481. for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++) {
  1482. if (ATH_TXQ_SETUP(sc, i)) {
  1483. spin_lock_bh(&sc->tx.txq[i].axq_lock);
  1484. ath_txq_schedule(sc, &sc->tx.txq[i]);
  1485. spin_unlock_bh(&sc->tx.txq[i].axq_lock);
  1486. }
  1487. }
  1488. }
  1489. return r;
  1490. }
  1491. /*
  1492. * This function will allocate both the DMA descriptor structure, and the
  1493. * buffers it contains. These are used to contain the descriptors used
  1494. * by the system.
  1495. */
  1496. int ath_descdma_setup(struct ath_softc *sc, struct ath_descdma *dd,
  1497. struct list_head *head, const char *name,
  1498. int nbuf, int ndesc)
  1499. {
  1500. #define DS2PHYS(_dd, _ds) \
  1501. ((_dd)->dd_desc_paddr + ((caddr_t)(_ds) - (caddr_t)(_dd)->dd_desc))
  1502. #define ATH_DESC_4KB_BOUND_CHECK(_daddr) ((((_daddr) & 0xFFF) > 0xF7F) ? 1 : 0)
  1503. #define ATH_DESC_4KB_BOUND_NUM_SKIPPED(_len) ((_len) / 4096)
  1504. struct ath_desc *ds;
  1505. struct ath_buf *bf;
  1506. int i, bsize, error;
  1507. DPRINTF(sc, ATH_DBG_CONFIG, "%s DMA: %u buffers %u desc/buf\n",
  1508. name, nbuf, ndesc);
  1509. /* ath_desc must be a multiple of DWORDs */
  1510. if ((sizeof(struct ath_desc) % 4) != 0) {
  1511. DPRINTF(sc, ATH_DBG_FATAL, "ath_desc not DWORD aligned\n");
  1512. ASSERT((sizeof(struct ath_desc) % 4) == 0);
  1513. error = -ENOMEM;
  1514. goto fail;
  1515. }
  1516. dd->dd_name = name;
  1517. dd->dd_desc_len = sizeof(struct ath_desc) * nbuf * ndesc;
  1518. /*
  1519. * Need additional DMA memory because we can't use
  1520. * descriptors that cross the 4K page boundary. Assume
  1521. * one skipped descriptor per 4K page.
  1522. */
  1523. if (!(sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_4KB_SPLITTRANS)) {
  1524. u32 ndesc_skipped =
  1525. ATH_DESC_4KB_BOUND_NUM_SKIPPED(dd->dd_desc_len);
  1526. u32 dma_len;
  1527. while (ndesc_skipped) {
  1528. dma_len = ndesc_skipped * sizeof(struct ath_desc);
  1529. dd->dd_desc_len += dma_len;
  1530. ndesc_skipped = ATH_DESC_4KB_BOUND_NUM_SKIPPED(dma_len);
  1531. };
  1532. }
  1533. /* allocate descriptors */
  1534. dd->dd_desc = dma_alloc_coherent(sc->dev, dd->dd_desc_len,
  1535. &dd->dd_desc_paddr, GFP_ATOMIC);
  1536. if (dd->dd_desc == NULL) {
  1537. error = -ENOMEM;
  1538. goto fail;
  1539. }
  1540. ds = dd->dd_desc;
  1541. DPRINTF(sc, ATH_DBG_CONFIG, "%s DMA map: %p (%u) -> %llx (%u)\n",
  1542. dd->dd_name, ds, (u32) dd->dd_desc_len,
  1543. ito64(dd->dd_desc_paddr), /*XXX*/(u32) dd->dd_desc_len);
  1544. /* allocate buffers */
  1545. bsize = sizeof(struct ath_buf) * nbuf;
  1546. bf = kmalloc(bsize, GFP_KERNEL);
  1547. if (bf == NULL) {
  1548. error = -ENOMEM;
  1549. goto fail2;
  1550. }
  1551. memset(bf, 0, bsize);
  1552. dd->dd_bufptr = bf;
  1553. INIT_LIST_HEAD(head);
  1554. for (i = 0; i < nbuf; i++, bf++, ds += ndesc) {
  1555. bf->bf_desc = ds;
  1556. bf->bf_daddr = DS2PHYS(dd, ds);
  1557. if (!(sc->sc_ah->caps.hw_caps &
  1558. ATH9K_HW_CAP_4KB_SPLITTRANS)) {
  1559. /*
  1560. * Skip descriptor addresses which can cause 4KB
  1561. * boundary crossing (addr + length) with a 32 dword
  1562. * descriptor fetch.
  1563. */
  1564. while (ATH_DESC_4KB_BOUND_CHECK(bf->bf_daddr)) {
  1565. ASSERT((caddr_t) bf->bf_desc <
  1566. ((caddr_t) dd->dd_desc +
  1567. dd->dd_desc_len));
  1568. ds += ndesc;
  1569. bf->bf_desc = ds;
  1570. bf->bf_daddr = DS2PHYS(dd, ds);
  1571. }
  1572. }
  1573. list_add_tail(&bf->list, head);
  1574. }
  1575. return 0;
  1576. fail2:
  1577. dma_free_coherent(sc->dev, dd->dd_desc_len, dd->dd_desc,
  1578. dd->dd_desc_paddr);
  1579. fail:
  1580. memset(dd, 0, sizeof(*dd));
  1581. return error;
  1582. #undef ATH_DESC_4KB_BOUND_CHECK
  1583. #undef ATH_DESC_4KB_BOUND_NUM_SKIPPED
  1584. #undef DS2PHYS
  1585. }
  1586. void ath_descdma_cleanup(struct ath_softc *sc,
  1587. struct ath_descdma *dd,
  1588. struct list_head *head)
  1589. {
  1590. dma_free_coherent(sc->dev, dd->dd_desc_len, dd->dd_desc,
  1591. dd->dd_desc_paddr);
  1592. INIT_LIST_HEAD(head);
  1593. kfree(dd->dd_bufptr);
  1594. memset(dd, 0, sizeof(*dd));
  1595. }
  1596. int ath_get_hal_qnum(u16 queue, struct ath_softc *sc)
  1597. {
  1598. int qnum;
  1599. switch (queue) {
  1600. case 0:
  1601. qnum = sc->tx.hwq_map[ATH9K_WME_AC_VO];
  1602. break;
  1603. case 1:
  1604. qnum = sc->tx.hwq_map[ATH9K_WME_AC_VI];
  1605. break;
  1606. case 2:
  1607. qnum = sc->tx.hwq_map[ATH9K_WME_AC_BE];
  1608. break;
  1609. case 3:
  1610. qnum = sc->tx.hwq_map[ATH9K_WME_AC_BK];
  1611. break;
  1612. default:
  1613. qnum = sc->tx.hwq_map[ATH9K_WME_AC_BE];
  1614. break;
  1615. }
  1616. return qnum;
  1617. }
  1618. int ath_get_mac80211_qnum(u32 queue, struct ath_softc *sc)
  1619. {
  1620. int qnum;
  1621. switch (queue) {
  1622. case ATH9K_WME_AC_VO:
  1623. qnum = 0;
  1624. break;
  1625. case ATH9K_WME_AC_VI:
  1626. qnum = 1;
  1627. break;
  1628. case ATH9K_WME_AC_BE:
  1629. qnum = 2;
  1630. break;
  1631. case ATH9K_WME_AC_BK:
  1632. qnum = 3;
  1633. break;
  1634. default:
  1635. qnum = -1;
  1636. break;
  1637. }
  1638. return qnum;
  1639. }
  1640. /* XXX: Remove me once we don't depend on ath9k_channel for all
  1641. * this redundant data */
  1642. static void ath9k_update_ichannel(struct ath_softc *sc,
  1643. struct ath9k_channel *ichan)
  1644. {
  1645. struct ieee80211_hw *hw = sc->hw;
  1646. struct ieee80211_channel *chan = hw->conf.channel;
  1647. struct ieee80211_conf *conf = &hw->conf;
  1648. ichan->channel = chan->center_freq;
  1649. ichan->chan = chan;
  1650. if (chan->band == IEEE80211_BAND_2GHZ) {
  1651. ichan->chanmode = CHANNEL_G;
  1652. ichan->channelFlags = CHANNEL_2GHZ | CHANNEL_OFDM;
  1653. } else {
  1654. ichan->chanmode = CHANNEL_A;
  1655. ichan->channelFlags = CHANNEL_5GHZ | CHANNEL_OFDM;
  1656. }
  1657. sc->tx_chan_width = ATH9K_HT_MACMODE_20;
  1658. if (conf_is_ht(conf)) {
  1659. if (conf_is_ht40(conf))
  1660. sc->tx_chan_width = ATH9K_HT_MACMODE_2040;
  1661. ichan->chanmode = ath_get_extchanmode(sc, chan,
  1662. conf->channel_type);
  1663. }
  1664. }
  1665. /**********************/
  1666. /* mac80211 callbacks */
  1667. /**********************/
  1668. static int ath9k_start(struct ieee80211_hw *hw)
  1669. {
  1670. struct ath_softc *sc = hw->priv;
  1671. struct ieee80211_channel *curchan = hw->conf.channel;
  1672. struct ath9k_channel *init_channel;
  1673. int r, pos;
  1674. DPRINTF(sc, ATH_DBG_CONFIG, "Starting driver with "
  1675. "initial channel: %d MHz\n", curchan->center_freq);
  1676. mutex_lock(&sc->mutex);
  1677. /* setup initial channel */
  1678. pos = curchan->hw_value;
  1679. init_channel = &sc->sc_ah->channels[pos];
  1680. ath9k_update_ichannel(sc, init_channel);
  1681. /* Reset SERDES registers */
  1682. ath9k_hw_configpcipowersave(sc->sc_ah, 0);
  1683. /*
  1684. * The basic interface to setting the hardware in a good
  1685. * state is ``reset''. On return the hardware is known to
  1686. * be powered up and with interrupts disabled. This must
  1687. * be followed by initialization of the appropriate bits
  1688. * and then setup of the interrupt mask.
  1689. */
  1690. spin_lock_bh(&sc->sc_resetlock);
  1691. r = ath9k_hw_reset(sc->sc_ah, init_channel, false);
  1692. if (r) {
  1693. DPRINTF(sc, ATH_DBG_FATAL,
  1694. "Unable to reset hardware; reset status %u "
  1695. "(freq %u MHz)\n", r,
  1696. curchan->center_freq);
  1697. spin_unlock_bh(&sc->sc_resetlock);
  1698. goto mutex_unlock;
  1699. }
  1700. spin_unlock_bh(&sc->sc_resetlock);
  1701. /*
  1702. * This is needed only to setup initial state
  1703. * but it's best done after a reset.
  1704. */
  1705. ath_update_txpow(sc);
  1706. /*
  1707. * Setup the hardware after reset:
  1708. * The receive engine is set going.
  1709. * Frame transmit is handled entirely
  1710. * in the frame output path; there's nothing to do
  1711. * here except setup the interrupt mask.
  1712. */
  1713. if (ath_startrecv(sc) != 0) {
  1714. DPRINTF(sc, ATH_DBG_FATAL,
  1715. "Unable to start recv logic\n");
  1716. r = -EIO;
  1717. goto mutex_unlock;
  1718. }
  1719. /* Setup our intr mask. */
  1720. sc->imask = ATH9K_INT_RX | ATH9K_INT_TX
  1721. | ATH9K_INT_RXEOL | ATH9K_INT_RXORN
  1722. | ATH9K_INT_FATAL | ATH9K_INT_GLOBAL;
  1723. if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_GTT)
  1724. sc->imask |= ATH9K_INT_GTT;
  1725. if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_HT)
  1726. sc->imask |= ATH9K_INT_CST;
  1727. ath_cache_conf_rate(sc, &hw->conf);
  1728. sc->sc_flags &= ~SC_OP_INVALID;
  1729. /* Disable BMISS interrupt when we're not associated */
  1730. sc->imask &= ~(ATH9K_INT_SWBA | ATH9K_INT_BMISS);
  1731. ath9k_hw_set_interrupts(sc->sc_ah, sc->imask);
  1732. ieee80211_wake_queues(sc->hw);
  1733. #if defined(CONFIG_RFKILL) || defined(CONFIG_RFKILL_MODULE)
  1734. r = ath_start_rfkill_poll(sc);
  1735. #endif
  1736. mutex_unlock:
  1737. mutex_unlock(&sc->mutex);
  1738. return r;
  1739. }
  1740. static int ath9k_tx(struct ieee80211_hw *hw,
  1741. struct sk_buff *skb)
  1742. {
  1743. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1744. struct ath_softc *sc = hw->priv;
  1745. struct ath_tx_control txctl;
  1746. int hdrlen, padsize;
  1747. memset(&txctl, 0, sizeof(struct ath_tx_control));
  1748. /*
  1749. * As a temporary workaround, assign seq# here; this will likely need
  1750. * to be cleaned up to work better with Beacon transmission and virtual
  1751. * BSSes.
  1752. */
  1753. if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
  1754. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  1755. if (info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT)
  1756. sc->tx.seq_no += 0x10;
  1757. hdr->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
  1758. hdr->seq_ctrl |= cpu_to_le16(sc->tx.seq_no);
  1759. }
  1760. /* Add the padding after the header if this is not already done */
  1761. hdrlen = ieee80211_get_hdrlen_from_skb(skb);
  1762. if (hdrlen & 3) {
  1763. padsize = hdrlen % 4;
  1764. if (skb_headroom(skb) < padsize)
  1765. return -1;
  1766. skb_push(skb, padsize);
  1767. memmove(skb->data, skb->data + padsize, hdrlen);
  1768. }
  1769. /* Check if a tx queue is available */
  1770. txctl.txq = ath_test_get_txq(sc, skb);
  1771. if (!txctl.txq)
  1772. goto exit;
  1773. DPRINTF(sc, ATH_DBG_XMIT, "transmitting packet, skb: %p\n", skb);
  1774. if (ath_tx_start(sc, skb, &txctl) != 0) {
  1775. DPRINTF(sc, ATH_DBG_XMIT, "TX failed\n");
  1776. goto exit;
  1777. }
  1778. return 0;
  1779. exit:
  1780. dev_kfree_skb_any(skb);
  1781. return 0;
  1782. }
  1783. static void ath9k_stop(struct ieee80211_hw *hw)
  1784. {
  1785. struct ath_softc *sc = hw->priv;
  1786. if (sc->sc_flags & SC_OP_INVALID) {
  1787. DPRINTF(sc, ATH_DBG_ANY, "Device not present\n");
  1788. return;
  1789. }
  1790. mutex_lock(&sc->mutex);
  1791. ieee80211_stop_queues(sc->hw);
  1792. /* make sure h/w will not generate any interrupt
  1793. * before setting the invalid flag. */
  1794. ath9k_hw_set_interrupts(sc->sc_ah, 0);
  1795. if (!(sc->sc_flags & SC_OP_INVALID)) {
  1796. ath_drain_all_txq(sc, false);
  1797. ath_stoprecv(sc);
  1798. ath9k_hw_phy_disable(sc->sc_ah);
  1799. } else
  1800. sc->rx.rxlink = NULL;
  1801. #if defined(CONFIG_RFKILL) || defined(CONFIG_RFKILL_MODULE)
  1802. if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_RFSILENT)
  1803. cancel_delayed_work_sync(&sc->rf_kill.rfkill_poll);
  1804. #endif
  1805. /* disable HAL and put h/w to sleep */
  1806. ath9k_hw_disable(sc->sc_ah);
  1807. ath9k_hw_configpcipowersave(sc->sc_ah, 1);
  1808. sc->sc_flags |= SC_OP_INVALID;
  1809. mutex_unlock(&sc->mutex);
  1810. DPRINTF(sc, ATH_DBG_CONFIG, "Driver halt\n");
  1811. }
  1812. static int ath9k_add_interface(struct ieee80211_hw *hw,
  1813. struct ieee80211_if_init_conf *conf)
  1814. {
  1815. struct ath_softc *sc = hw->priv;
  1816. struct ath_vif *avp = (void *)conf->vif->drv_priv;
  1817. enum nl80211_iftype ic_opmode = NL80211_IFTYPE_UNSPECIFIED;
  1818. /* Support only vif for now */
  1819. if (sc->nvifs)
  1820. return -ENOBUFS;
  1821. mutex_lock(&sc->mutex);
  1822. switch (conf->type) {
  1823. case NL80211_IFTYPE_STATION:
  1824. ic_opmode = NL80211_IFTYPE_STATION;
  1825. break;
  1826. case NL80211_IFTYPE_ADHOC:
  1827. ic_opmode = NL80211_IFTYPE_ADHOC;
  1828. break;
  1829. case NL80211_IFTYPE_AP:
  1830. ic_opmode = NL80211_IFTYPE_AP;
  1831. break;
  1832. default:
  1833. DPRINTF(sc, ATH_DBG_FATAL,
  1834. "Interface type %d not yet supported\n", conf->type);
  1835. mutex_unlock(&sc->mutex);
  1836. return -EOPNOTSUPP;
  1837. }
  1838. DPRINTF(sc, ATH_DBG_CONFIG, "Attach a VIF of type: %d\n", ic_opmode);
  1839. /* Set the VIF opmode */
  1840. avp->av_opmode = ic_opmode;
  1841. avp->av_bslot = -1;
  1842. if (ic_opmode == NL80211_IFTYPE_AP)
  1843. ath9k_hw_set_tsfadjust(sc->sc_ah, 1);
  1844. sc->vifs[0] = conf->vif;
  1845. sc->nvifs++;
  1846. /* Set the device opmode */
  1847. sc->sc_ah->opmode = ic_opmode;
  1848. /*
  1849. * Enable MIB interrupts when there are hardware phy counters.
  1850. * Note we only do this (at the moment) for station mode.
  1851. */
  1852. if ((conf->type == NL80211_IFTYPE_STATION) ||
  1853. (conf->type == NL80211_IFTYPE_ADHOC)) {
  1854. if (ath9k_hw_phycounters(sc->sc_ah))
  1855. sc->imask |= ATH9K_INT_MIB;
  1856. sc->imask |= ATH9K_INT_TSFOOR;
  1857. }
  1858. /*
  1859. * Some hardware processes the TIM IE and fires an
  1860. * interrupt when the TIM bit is set. For hardware
  1861. * that does, if not overridden by configuration,
  1862. * enable the TIM interrupt when operating as station.
  1863. */
  1864. if ((sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_ENHANCEDPM) &&
  1865. (conf->type == NL80211_IFTYPE_STATION) &&
  1866. !sc->config.swBeaconProcess)
  1867. sc->imask |= ATH9K_INT_TIM;
  1868. ath9k_hw_set_interrupts(sc->sc_ah, sc->imask);
  1869. if (conf->type == NL80211_IFTYPE_AP) {
  1870. /* TODO: is this a suitable place to start ANI for AP mode? */
  1871. /* Start ANI */
  1872. mod_timer(&sc->ani.timer,
  1873. jiffies + msecs_to_jiffies(ATH_ANI_POLLINTERVAL));
  1874. }
  1875. mutex_unlock(&sc->mutex);
  1876. return 0;
  1877. }
  1878. static void ath9k_remove_interface(struct ieee80211_hw *hw,
  1879. struct ieee80211_if_init_conf *conf)
  1880. {
  1881. struct ath_softc *sc = hw->priv;
  1882. struct ath_vif *avp = (void *)conf->vif->drv_priv;
  1883. DPRINTF(sc, ATH_DBG_CONFIG, "Detach Interface\n");
  1884. mutex_lock(&sc->mutex);
  1885. /* Stop ANI */
  1886. del_timer_sync(&sc->ani.timer);
  1887. /* Reclaim beacon resources */
  1888. if (sc->sc_ah->opmode == NL80211_IFTYPE_AP ||
  1889. sc->sc_ah->opmode == NL80211_IFTYPE_ADHOC) {
  1890. ath9k_hw_stoptxdma(sc->sc_ah, sc->beacon.beaconq);
  1891. ath_beacon_return(sc, avp);
  1892. }
  1893. sc->sc_flags &= ~SC_OP_BEACONS;
  1894. sc->vifs[0] = NULL;
  1895. sc->nvifs--;
  1896. mutex_unlock(&sc->mutex);
  1897. }
  1898. static int ath9k_config(struct ieee80211_hw *hw, u32 changed)
  1899. {
  1900. struct ath_softc *sc = hw->priv;
  1901. struct ieee80211_conf *conf = &hw->conf;
  1902. mutex_lock(&sc->mutex);
  1903. if (changed & IEEE80211_CONF_CHANGE_PS) {
  1904. if (conf->flags & IEEE80211_CONF_PS) {
  1905. if ((sc->imask & ATH9K_INT_TIM_TIMER) == 0) {
  1906. sc->imask |= ATH9K_INT_TIM_TIMER;
  1907. ath9k_hw_set_interrupts(sc->sc_ah,
  1908. sc->imask);
  1909. }
  1910. ath9k_hw_setrxabort(sc->sc_ah, 1);
  1911. ath9k_hw_setpower(sc->sc_ah, ATH9K_PM_NETWORK_SLEEP);
  1912. } else {
  1913. ath9k_hw_setpower(sc->sc_ah, ATH9K_PM_AWAKE);
  1914. ath9k_hw_setrxabort(sc->sc_ah, 0);
  1915. sc->sc_flags &= ~SC_OP_WAIT_FOR_BEACON;
  1916. if (sc->imask & ATH9K_INT_TIM_TIMER) {
  1917. sc->imask &= ~ATH9K_INT_TIM_TIMER;
  1918. ath9k_hw_set_interrupts(sc->sc_ah,
  1919. sc->imask);
  1920. }
  1921. }
  1922. }
  1923. if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
  1924. struct ieee80211_channel *curchan = hw->conf.channel;
  1925. int pos = curchan->hw_value;
  1926. DPRINTF(sc, ATH_DBG_CONFIG, "Set channel: %d MHz\n",
  1927. curchan->center_freq);
  1928. /* XXX: remove me eventualy */
  1929. ath9k_update_ichannel(sc, &sc->sc_ah->channels[pos]);
  1930. ath_update_chainmask(sc, conf_is_ht(conf));
  1931. if (ath_set_channel(sc, &sc->sc_ah->channels[pos]) < 0) {
  1932. DPRINTF(sc, ATH_DBG_FATAL, "Unable to set channel\n");
  1933. mutex_unlock(&sc->mutex);
  1934. return -EINVAL;
  1935. }
  1936. }
  1937. if (changed & IEEE80211_CONF_CHANGE_POWER)
  1938. sc->config.txpowlimit = 2 * conf->power_level;
  1939. mutex_unlock(&sc->mutex);
  1940. return 0;
  1941. }
  1942. static int ath9k_config_interface(struct ieee80211_hw *hw,
  1943. struct ieee80211_vif *vif,
  1944. struct ieee80211_if_conf *conf)
  1945. {
  1946. struct ath_softc *sc = hw->priv;
  1947. struct ath_hw *ah = sc->sc_ah;
  1948. struct ath_vif *avp = (void *)vif->drv_priv;
  1949. u32 rfilt = 0;
  1950. int error, i;
  1951. /* TODO: Need to decide which hw opmode to use for multi-interface
  1952. * cases */
  1953. if (vif->type == NL80211_IFTYPE_AP &&
  1954. ah->opmode != NL80211_IFTYPE_AP) {
  1955. ah->opmode = NL80211_IFTYPE_STATION;
  1956. ath9k_hw_setopmode(ah);
  1957. memcpy(sc->curbssid, sc->sc_ah->macaddr, ETH_ALEN);
  1958. sc->curaid = 0;
  1959. ath9k_hw_write_associd(sc);
  1960. /* Request full reset to get hw opmode changed properly */
  1961. sc->sc_flags |= SC_OP_FULL_RESET;
  1962. }
  1963. if ((conf->changed & IEEE80211_IFCC_BSSID) &&
  1964. !is_zero_ether_addr(conf->bssid)) {
  1965. switch (vif->type) {
  1966. case NL80211_IFTYPE_STATION:
  1967. case NL80211_IFTYPE_ADHOC:
  1968. /* Set BSSID */
  1969. memcpy(sc->curbssid, conf->bssid, ETH_ALEN);
  1970. sc->curaid = 0;
  1971. ath9k_hw_write_associd(sc);
  1972. /* Set aggregation protection mode parameters */
  1973. sc->config.ath_aggr_prot = 0;
  1974. DPRINTF(sc, ATH_DBG_CONFIG,
  1975. "RX filter 0x%x bssid %pM aid 0x%x\n",
  1976. rfilt, sc->curbssid, sc->curaid);
  1977. /* need to reconfigure the beacon */
  1978. sc->sc_flags &= ~SC_OP_BEACONS ;
  1979. break;
  1980. default:
  1981. break;
  1982. }
  1983. }
  1984. if ((vif->type == NL80211_IFTYPE_ADHOC) ||
  1985. (vif->type == NL80211_IFTYPE_AP)) {
  1986. if ((conf->changed & IEEE80211_IFCC_BEACON) ||
  1987. (conf->changed & IEEE80211_IFCC_BEACON_ENABLED &&
  1988. conf->enable_beacon)) {
  1989. /*
  1990. * Allocate and setup the beacon frame.
  1991. *
  1992. * Stop any previous beacon DMA. This may be
  1993. * necessary, for example, when an ibss merge
  1994. * causes reconfiguration; we may be called
  1995. * with beacon transmission active.
  1996. */
  1997. ath9k_hw_stoptxdma(sc->sc_ah, sc->beacon.beaconq);
  1998. error = ath_beacon_alloc(sc, 0);
  1999. if (error != 0)
  2000. return error;
  2001. ath_beacon_sync(sc, 0);
  2002. }
  2003. }
  2004. /* Check for WLAN_CAPABILITY_PRIVACY ? */
  2005. if ((avp->av_opmode != NL80211_IFTYPE_STATION)) {
  2006. for (i = 0; i < IEEE80211_WEP_NKID; i++)
  2007. if (ath9k_hw_keyisvalid(sc->sc_ah, (u16)i))
  2008. ath9k_hw_keysetmac(sc->sc_ah,
  2009. (u16)i,
  2010. sc->curbssid);
  2011. }
  2012. /* Only legacy IBSS for now */
  2013. if (vif->type == NL80211_IFTYPE_ADHOC)
  2014. ath_update_chainmask(sc, 0);
  2015. return 0;
  2016. }
  2017. #define SUPPORTED_FILTERS \
  2018. (FIF_PROMISC_IN_BSS | \
  2019. FIF_ALLMULTI | \
  2020. FIF_CONTROL | \
  2021. FIF_OTHER_BSS | \
  2022. FIF_BCN_PRBRESP_PROMISC | \
  2023. FIF_FCSFAIL)
  2024. /* FIXME: sc->sc_full_reset ? */
  2025. static void ath9k_configure_filter(struct ieee80211_hw *hw,
  2026. unsigned int changed_flags,
  2027. unsigned int *total_flags,
  2028. int mc_count,
  2029. struct dev_mc_list *mclist)
  2030. {
  2031. struct ath_softc *sc = hw->priv;
  2032. u32 rfilt;
  2033. changed_flags &= SUPPORTED_FILTERS;
  2034. *total_flags &= SUPPORTED_FILTERS;
  2035. sc->rx.rxfilter = *total_flags;
  2036. rfilt = ath_calcrxfilter(sc);
  2037. ath9k_hw_setrxfilter(sc->sc_ah, rfilt);
  2038. if (changed_flags & FIF_BCN_PRBRESP_PROMISC) {
  2039. if (*total_flags & FIF_BCN_PRBRESP_PROMISC) {
  2040. memcpy(sc->curbssid, ath_bcast_mac, ETH_ALEN);
  2041. sc->curaid = 0;
  2042. ath9k_hw_write_associd(sc);
  2043. }
  2044. }
  2045. DPRINTF(sc, ATH_DBG_CONFIG, "Set HW RX filter: 0x%x\n", sc->rx.rxfilter);
  2046. }
  2047. static void ath9k_sta_notify(struct ieee80211_hw *hw,
  2048. struct ieee80211_vif *vif,
  2049. enum sta_notify_cmd cmd,
  2050. struct ieee80211_sta *sta)
  2051. {
  2052. struct ath_softc *sc = hw->priv;
  2053. switch (cmd) {
  2054. case STA_NOTIFY_ADD:
  2055. ath_node_attach(sc, sta);
  2056. break;
  2057. case STA_NOTIFY_REMOVE:
  2058. ath_node_detach(sc, sta);
  2059. break;
  2060. default:
  2061. break;
  2062. }
  2063. }
  2064. static int ath9k_conf_tx(struct ieee80211_hw *hw, u16 queue,
  2065. const struct ieee80211_tx_queue_params *params)
  2066. {
  2067. struct ath_softc *sc = hw->priv;
  2068. struct ath9k_tx_queue_info qi;
  2069. int ret = 0, qnum;
  2070. if (queue >= WME_NUM_AC)
  2071. return 0;
  2072. mutex_lock(&sc->mutex);
  2073. qi.tqi_aifs = params->aifs;
  2074. qi.tqi_cwmin = params->cw_min;
  2075. qi.tqi_cwmax = params->cw_max;
  2076. qi.tqi_burstTime = params->txop;
  2077. qnum = ath_get_hal_qnum(queue, sc);
  2078. DPRINTF(sc, ATH_DBG_CONFIG,
  2079. "Configure tx [queue/halq] [%d/%d], "
  2080. "aifs: %d, cw_min: %d, cw_max: %d, txop: %d\n",
  2081. queue, qnum, params->aifs, params->cw_min,
  2082. params->cw_max, params->txop);
  2083. ret = ath_txq_update(sc, qnum, &qi);
  2084. if (ret)
  2085. DPRINTF(sc, ATH_DBG_FATAL, "TXQ Update failed\n");
  2086. mutex_unlock(&sc->mutex);
  2087. return ret;
  2088. }
  2089. static int ath9k_set_key(struct ieee80211_hw *hw,
  2090. enum set_key_cmd cmd,
  2091. struct ieee80211_vif *vif,
  2092. struct ieee80211_sta *sta,
  2093. struct ieee80211_key_conf *key)
  2094. {
  2095. struct ath_softc *sc = hw->priv;
  2096. int ret = 0;
  2097. if (modparam_nohwcrypt)
  2098. return -ENOSPC;
  2099. mutex_lock(&sc->mutex);
  2100. ath9k_ps_wakeup(sc);
  2101. DPRINTF(sc, ATH_DBG_KEYCACHE, "Set HW Key\n");
  2102. switch (cmd) {
  2103. case SET_KEY:
  2104. ret = ath_key_config(sc, vif, sta, key);
  2105. if (ret >= 0) {
  2106. key->hw_key_idx = ret;
  2107. /* push IV and Michael MIC generation to stack */
  2108. key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  2109. if (key->alg == ALG_TKIP)
  2110. key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
  2111. if (sc->sc_ah->sw_mgmt_crypto && key->alg == ALG_CCMP)
  2112. key->flags |= IEEE80211_KEY_FLAG_SW_MGMT;
  2113. ret = 0;
  2114. }
  2115. break;
  2116. case DISABLE_KEY:
  2117. ath_key_delete(sc, key);
  2118. break;
  2119. default:
  2120. ret = -EINVAL;
  2121. }
  2122. ath9k_ps_restore(sc);
  2123. mutex_unlock(&sc->mutex);
  2124. return ret;
  2125. }
  2126. static void ath9k_bss_info_changed(struct ieee80211_hw *hw,
  2127. struct ieee80211_vif *vif,
  2128. struct ieee80211_bss_conf *bss_conf,
  2129. u32 changed)
  2130. {
  2131. struct ath_softc *sc = hw->priv;
  2132. mutex_lock(&sc->mutex);
  2133. if (changed & BSS_CHANGED_ERP_PREAMBLE) {
  2134. DPRINTF(sc, ATH_DBG_CONFIG, "BSS Changed PREAMBLE %d\n",
  2135. bss_conf->use_short_preamble);
  2136. if (bss_conf->use_short_preamble)
  2137. sc->sc_flags |= SC_OP_PREAMBLE_SHORT;
  2138. else
  2139. sc->sc_flags &= ~SC_OP_PREAMBLE_SHORT;
  2140. }
  2141. if (changed & BSS_CHANGED_ERP_CTS_PROT) {
  2142. DPRINTF(sc, ATH_DBG_CONFIG, "BSS Changed CTS PROT %d\n",
  2143. bss_conf->use_cts_prot);
  2144. if (bss_conf->use_cts_prot &&
  2145. hw->conf.channel->band != IEEE80211_BAND_5GHZ)
  2146. sc->sc_flags |= SC_OP_PROTECT_ENABLE;
  2147. else
  2148. sc->sc_flags &= ~SC_OP_PROTECT_ENABLE;
  2149. }
  2150. if (changed & BSS_CHANGED_ASSOC) {
  2151. DPRINTF(sc, ATH_DBG_CONFIG, "BSS Changed ASSOC %d\n",
  2152. bss_conf->assoc);
  2153. ath9k_bss_assoc_info(sc, vif, bss_conf);
  2154. }
  2155. mutex_unlock(&sc->mutex);
  2156. }
  2157. static u64 ath9k_get_tsf(struct ieee80211_hw *hw)
  2158. {
  2159. u64 tsf;
  2160. struct ath_softc *sc = hw->priv;
  2161. mutex_lock(&sc->mutex);
  2162. tsf = ath9k_hw_gettsf64(sc->sc_ah);
  2163. mutex_unlock(&sc->mutex);
  2164. return tsf;
  2165. }
  2166. static void ath9k_set_tsf(struct ieee80211_hw *hw, u64 tsf)
  2167. {
  2168. struct ath_softc *sc = hw->priv;
  2169. mutex_lock(&sc->mutex);
  2170. ath9k_hw_settsf64(sc->sc_ah, tsf);
  2171. mutex_unlock(&sc->mutex);
  2172. }
  2173. static void ath9k_reset_tsf(struct ieee80211_hw *hw)
  2174. {
  2175. struct ath_softc *sc = hw->priv;
  2176. mutex_lock(&sc->mutex);
  2177. ath9k_hw_reset_tsf(sc->sc_ah);
  2178. mutex_unlock(&sc->mutex);
  2179. }
  2180. static int ath9k_ampdu_action(struct ieee80211_hw *hw,
  2181. enum ieee80211_ampdu_mlme_action action,
  2182. struct ieee80211_sta *sta,
  2183. u16 tid, u16 *ssn)
  2184. {
  2185. struct ath_softc *sc = hw->priv;
  2186. int ret = 0;
  2187. switch (action) {
  2188. case IEEE80211_AMPDU_RX_START:
  2189. if (!(sc->sc_flags & SC_OP_RXAGGR))
  2190. ret = -ENOTSUPP;
  2191. break;
  2192. case IEEE80211_AMPDU_RX_STOP:
  2193. break;
  2194. case IEEE80211_AMPDU_TX_START:
  2195. ret = ath_tx_aggr_start(sc, sta, tid, ssn);
  2196. if (ret < 0)
  2197. DPRINTF(sc, ATH_DBG_FATAL,
  2198. "Unable to start TX aggregation\n");
  2199. else
  2200. ieee80211_start_tx_ba_cb_irqsafe(hw, sta->addr, tid);
  2201. break;
  2202. case IEEE80211_AMPDU_TX_STOP:
  2203. ret = ath_tx_aggr_stop(sc, sta, tid);
  2204. if (ret < 0)
  2205. DPRINTF(sc, ATH_DBG_FATAL,
  2206. "Unable to stop TX aggregation\n");
  2207. ieee80211_stop_tx_ba_cb_irqsafe(hw, sta->addr, tid);
  2208. break;
  2209. case IEEE80211_AMPDU_TX_RESUME:
  2210. ath_tx_aggr_resume(sc, sta, tid);
  2211. break;
  2212. default:
  2213. DPRINTF(sc, ATH_DBG_FATAL, "Unknown AMPDU action\n");
  2214. }
  2215. return ret;
  2216. }
  2217. struct ieee80211_ops ath9k_ops = {
  2218. .tx = ath9k_tx,
  2219. .start = ath9k_start,
  2220. .stop = ath9k_stop,
  2221. .add_interface = ath9k_add_interface,
  2222. .remove_interface = ath9k_remove_interface,
  2223. .config = ath9k_config,
  2224. .config_interface = ath9k_config_interface,
  2225. .configure_filter = ath9k_configure_filter,
  2226. .sta_notify = ath9k_sta_notify,
  2227. .conf_tx = ath9k_conf_tx,
  2228. .bss_info_changed = ath9k_bss_info_changed,
  2229. .set_key = ath9k_set_key,
  2230. .get_tsf = ath9k_get_tsf,
  2231. .set_tsf = ath9k_set_tsf,
  2232. .reset_tsf = ath9k_reset_tsf,
  2233. .ampdu_action = ath9k_ampdu_action,
  2234. };
  2235. static struct {
  2236. u32 version;
  2237. const char * name;
  2238. } ath_mac_bb_names[] = {
  2239. { AR_SREV_VERSION_5416_PCI, "5416" },
  2240. { AR_SREV_VERSION_5416_PCIE, "5418" },
  2241. { AR_SREV_VERSION_9100, "9100" },
  2242. { AR_SREV_VERSION_9160, "9160" },
  2243. { AR_SREV_VERSION_9280, "9280" },
  2244. { AR_SREV_VERSION_9285, "9285" }
  2245. };
  2246. static struct {
  2247. u16 version;
  2248. const char * name;
  2249. } ath_rf_names[] = {
  2250. { 0, "5133" },
  2251. { AR_RAD5133_SREV_MAJOR, "5133" },
  2252. { AR_RAD5122_SREV_MAJOR, "5122" },
  2253. { AR_RAD2133_SREV_MAJOR, "2133" },
  2254. { AR_RAD2122_SREV_MAJOR, "2122" }
  2255. };
  2256. /*
  2257. * Return the MAC/BB name. "????" is returned if the MAC/BB is unknown.
  2258. */
  2259. const char *
  2260. ath_mac_bb_name(u32 mac_bb_version)
  2261. {
  2262. int i;
  2263. for (i=0; i<ARRAY_SIZE(ath_mac_bb_names); i++) {
  2264. if (ath_mac_bb_names[i].version == mac_bb_version) {
  2265. return ath_mac_bb_names[i].name;
  2266. }
  2267. }
  2268. return "????";
  2269. }
  2270. /*
  2271. * Return the RF name. "????" is returned if the RF is unknown.
  2272. */
  2273. const char *
  2274. ath_rf_name(u16 rf_version)
  2275. {
  2276. int i;
  2277. for (i=0; i<ARRAY_SIZE(ath_rf_names); i++) {
  2278. if (ath_rf_names[i].version == rf_version) {
  2279. return ath_rf_names[i].name;
  2280. }
  2281. }
  2282. return "????";
  2283. }
  2284. static int __init ath9k_init(void)
  2285. {
  2286. int error;
  2287. /* Register rate control algorithm */
  2288. error = ath_rate_control_register();
  2289. if (error != 0) {
  2290. printk(KERN_ERR
  2291. "ath9k: Unable to register rate control "
  2292. "algorithm: %d\n",
  2293. error);
  2294. goto err_out;
  2295. }
  2296. error = ath_pci_init();
  2297. if (error < 0) {
  2298. printk(KERN_ERR
  2299. "ath9k: No PCI devices found, driver not installed.\n");
  2300. error = -ENODEV;
  2301. goto err_rate_unregister;
  2302. }
  2303. error = ath_ahb_init();
  2304. if (error < 0) {
  2305. error = -ENODEV;
  2306. goto err_pci_exit;
  2307. }
  2308. return 0;
  2309. err_pci_exit:
  2310. ath_pci_exit();
  2311. err_rate_unregister:
  2312. ath_rate_control_unregister();
  2313. err_out:
  2314. return error;
  2315. }
  2316. module_init(ath9k_init);
  2317. static void __exit ath9k_exit(void)
  2318. {
  2319. ath_ahb_exit();
  2320. ath_pci_exit();
  2321. ath_rate_control_unregister();
  2322. printk(KERN_INFO "%s: Driver unloaded\n", dev_info);
  2323. }
  2324. module_exit(ath9k_exit);