main.c 68 KB

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