main.c 79 KB

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