main.c 83 KB

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