main.c 68 KB

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