main.c 70 KB

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