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