main.c 68 KB

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