main.c 64 KB

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