main.c 69 KB

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