main.c 70 KB

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