mac.c 121 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929
  1. /*
  2. * Copyright (c) 2005-2011 Atheros Communications Inc.
  3. * Copyright (c) 2011-2013 Qualcomm Atheros, Inc.
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  14. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  15. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. #include "mac.h"
  18. #include <net/mac80211.h>
  19. #include <linux/etherdevice.h>
  20. #include "hif.h"
  21. #include "core.h"
  22. #include "debug.h"
  23. #include "wmi.h"
  24. #include "htt.h"
  25. #include "txrx.h"
  26. #include "testmode.h"
  27. /**********/
  28. /* Crypto */
  29. /**********/
  30. static int ath10k_send_key(struct ath10k_vif *arvif,
  31. struct ieee80211_key_conf *key,
  32. enum set_key_cmd cmd,
  33. const u8 *macaddr)
  34. {
  35. struct ath10k *ar = arvif->ar;
  36. struct wmi_vdev_install_key_arg arg = {
  37. .vdev_id = arvif->vdev_id,
  38. .key_idx = key->keyidx,
  39. .key_len = key->keylen,
  40. .key_data = key->key,
  41. .macaddr = macaddr,
  42. };
  43. lockdep_assert_held(&arvif->ar->conf_mutex);
  44. if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
  45. arg.key_flags = WMI_KEY_PAIRWISE;
  46. else
  47. arg.key_flags = WMI_KEY_GROUP;
  48. switch (key->cipher) {
  49. case WLAN_CIPHER_SUITE_CCMP:
  50. arg.key_cipher = WMI_CIPHER_AES_CCM;
  51. if (arvif->vdev_type == WMI_VDEV_TYPE_AP)
  52. key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV_MGMT;
  53. else
  54. key->flags |= IEEE80211_KEY_FLAG_SW_MGMT_TX;
  55. break;
  56. case WLAN_CIPHER_SUITE_TKIP:
  57. arg.key_cipher = WMI_CIPHER_TKIP;
  58. arg.key_txmic_len = 8;
  59. arg.key_rxmic_len = 8;
  60. break;
  61. case WLAN_CIPHER_SUITE_WEP40:
  62. case WLAN_CIPHER_SUITE_WEP104:
  63. arg.key_cipher = WMI_CIPHER_WEP;
  64. /* AP/IBSS mode requires self-key to be groupwise
  65. * Otherwise pairwise key must be set */
  66. if (memcmp(macaddr, arvif->vif->addr, ETH_ALEN))
  67. arg.key_flags = WMI_KEY_PAIRWISE;
  68. break;
  69. default:
  70. ath10k_warn(ar, "cipher %d is not supported\n", key->cipher);
  71. return -EOPNOTSUPP;
  72. }
  73. if (cmd == DISABLE_KEY) {
  74. arg.key_cipher = WMI_CIPHER_NONE;
  75. arg.key_data = NULL;
  76. }
  77. return ath10k_wmi_vdev_install_key(arvif->ar, &arg);
  78. }
  79. static int ath10k_install_key(struct ath10k_vif *arvif,
  80. struct ieee80211_key_conf *key,
  81. enum set_key_cmd cmd,
  82. const u8 *macaddr)
  83. {
  84. struct ath10k *ar = arvif->ar;
  85. int ret;
  86. lockdep_assert_held(&ar->conf_mutex);
  87. reinit_completion(&ar->install_key_done);
  88. ret = ath10k_send_key(arvif, key, cmd, macaddr);
  89. if (ret)
  90. return ret;
  91. ret = wait_for_completion_timeout(&ar->install_key_done, 3*HZ);
  92. if (ret == 0)
  93. return -ETIMEDOUT;
  94. return 0;
  95. }
  96. static int ath10k_install_peer_wep_keys(struct ath10k_vif *arvif,
  97. const u8 *addr)
  98. {
  99. struct ath10k *ar = arvif->ar;
  100. struct ath10k_peer *peer;
  101. int ret;
  102. int i;
  103. lockdep_assert_held(&ar->conf_mutex);
  104. spin_lock_bh(&ar->data_lock);
  105. peer = ath10k_peer_find(ar, arvif->vdev_id, addr);
  106. spin_unlock_bh(&ar->data_lock);
  107. if (!peer)
  108. return -ENOENT;
  109. for (i = 0; i < ARRAY_SIZE(arvif->wep_keys); i++) {
  110. if (arvif->wep_keys[i] == NULL)
  111. continue;
  112. ret = ath10k_install_key(arvif, arvif->wep_keys[i], SET_KEY,
  113. addr);
  114. if (ret)
  115. return ret;
  116. peer->keys[i] = arvif->wep_keys[i];
  117. }
  118. return 0;
  119. }
  120. static int ath10k_clear_peer_keys(struct ath10k_vif *arvif,
  121. const u8 *addr)
  122. {
  123. struct ath10k *ar = arvif->ar;
  124. struct ath10k_peer *peer;
  125. int first_errno = 0;
  126. int ret;
  127. int i;
  128. lockdep_assert_held(&ar->conf_mutex);
  129. spin_lock_bh(&ar->data_lock);
  130. peer = ath10k_peer_find(ar, arvif->vdev_id, addr);
  131. spin_unlock_bh(&ar->data_lock);
  132. if (!peer)
  133. return -ENOENT;
  134. for (i = 0; i < ARRAY_SIZE(peer->keys); i++) {
  135. if (peer->keys[i] == NULL)
  136. continue;
  137. ret = ath10k_install_key(arvif, peer->keys[i],
  138. DISABLE_KEY, addr);
  139. if (ret && first_errno == 0)
  140. first_errno = ret;
  141. if (ret)
  142. ath10k_warn(ar, "failed to remove peer wep key %d: %d\n",
  143. i, ret);
  144. peer->keys[i] = NULL;
  145. }
  146. return first_errno;
  147. }
  148. static int ath10k_clear_vdev_key(struct ath10k_vif *arvif,
  149. struct ieee80211_key_conf *key)
  150. {
  151. struct ath10k *ar = arvif->ar;
  152. struct ath10k_peer *peer;
  153. u8 addr[ETH_ALEN];
  154. int first_errno = 0;
  155. int ret;
  156. int i;
  157. lockdep_assert_held(&ar->conf_mutex);
  158. for (;;) {
  159. /* since ath10k_install_key we can't hold data_lock all the
  160. * time, so we try to remove the keys incrementally */
  161. spin_lock_bh(&ar->data_lock);
  162. i = 0;
  163. list_for_each_entry(peer, &ar->peers, list) {
  164. for (i = 0; i < ARRAY_SIZE(peer->keys); i++) {
  165. if (peer->keys[i] == key) {
  166. ether_addr_copy(addr, peer->addr);
  167. peer->keys[i] = NULL;
  168. break;
  169. }
  170. }
  171. if (i < ARRAY_SIZE(peer->keys))
  172. break;
  173. }
  174. spin_unlock_bh(&ar->data_lock);
  175. if (i == ARRAY_SIZE(peer->keys))
  176. break;
  177. ret = ath10k_install_key(arvif, key, DISABLE_KEY, addr);
  178. if (ret && first_errno == 0)
  179. first_errno = ret;
  180. if (ret)
  181. ath10k_warn(ar, "failed to remove key for %pM: %d\n",
  182. addr, ret);
  183. }
  184. return first_errno;
  185. }
  186. /*********************/
  187. /* General utilities */
  188. /*********************/
  189. static inline enum wmi_phy_mode
  190. chan_to_phymode(const struct cfg80211_chan_def *chandef)
  191. {
  192. enum wmi_phy_mode phymode = MODE_UNKNOWN;
  193. switch (chandef->chan->band) {
  194. case IEEE80211_BAND_2GHZ:
  195. switch (chandef->width) {
  196. case NL80211_CHAN_WIDTH_20_NOHT:
  197. phymode = MODE_11G;
  198. break;
  199. case NL80211_CHAN_WIDTH_20:
  200. phymode = MODE_11NG_HT20;
  201. break;
  202. case NL80211_CHAN_WIDTH_40:
  203. phymode = MODE_11NG_HT40;
  204. break;
  205. case NL80211_CHAN_WIDTH_5:
  206. case NL80211_CHAN_WIDTH_10:
  207. case NL80211_CHAN_WIDTH_80:
  208. case NL80211_CHAN_WIDTH_80P80:
  209. case NL80211_CHAN_WIDTH_160:
  210. phymode = MODE_UNKNOWN;
  211. break;
  212. }
  213. break;
  214. case IEEE80211_BAND_5GHZ:
  215. switch (chandef->width) {
  216. case NL80211_CHAN_WIDTH_20_NOHT:
  217. phymode = MODE_11A;
  218. break;
  219. case NL80211_CHAN_WIDTH_20:
  220. phymode = MODE_11NA_HT20;
  221. break;
  222. case NL80211_CHAN_WIDTH_40:
  223. phymode = MODE_11NA_HT40;
  224. break;
  225. case NL80211_CHAN_WIDTH_80:
  226. phymode = MODE_11AC_VHT80;
  227. break;
  228. case NL80211_CHAN_WIDTH_5:
  229. case NL80211_CHAN_WIDTH_10:
  230. case NL80211_CHAN_WIDTH_80P80:
  231. case NL80211_CHAN_WIDTH_160:
  232. phymode = MODE_UNKNOWN;
  233. break;
  234. }
  235. break;
  236. default:
  237. break;
  238. }
  239. WARN_ON(phymode == MODE_UNKNOWN);
  240. return phymode;
  241. }
  242. static u8 ath10k_parse_mpdudensity(u8 mpdudensity)
  243. {
  244. /*
  245. * 802.11n D2.0 defined values for "Minimum MPDU Start Spacing":
  246. * 0 for no restriction
  247. * 1 for 1/4 us
  248. * 2 for 1/2 us
  249. * 3 for 1 us
  250. * 4 for 2 us
  251. * 5 for 4 us
  252. * 6 for 8 us
  253. * 7 for 16 us
  254. */
  255. switch (mpdudensity) {
  256. case 0:
  257. return 0;
  258. case 1:
  259. case 2:
  260. case 3:
  261. /* Our lower layer calculations limit our precision to
  262. 1 microsecond */
  263. return 1;
  264. case 4:
  265. return 2;
  266. case 5:
  267. return 4;
  268. case 6:
  269. return 8;
  270. case 7:
  271. return 16;
  272. default:
  273. return 0;
  274. }
  275. }
  276. static int ath10k_peer_create(struct ath10k *ar, u32 vdev_id, const u8 *addr)
  277. {
  278. int ret;
  279. lockdep_assert_held(&ar->conf_mutex);
  280. ret = ath10k_wmi_peer_create(ar, vdev_id, addr);
  281. if (ret) {
  282. ath10k_warn(ar, "failed to create wmi peer %pM on vdev %i: %i\n",
  283. addr, vdev_id, ret);
  284. return ret;
  285. }
  286. ret = ath10k_wait_for_peer_created(ar, vdev_id, addr);
  287. if (ret) {
  288. ath10k_warn(ar, "failed to wait for created wmi peer %pM on vdev %i: %i\n",
  289. addr, vdev_id, ret);
  290. return ret;
  291. }
  292. spin_lock_bh(&ar->data_lock);
  293. ar->num_peers++;
  294. spin_unlock_bh(&ar->data_lock);
  295. return 0;
  296. }
  297. static int ath10k_mac_set_kickout(struct ath10k_vif *arvif)
  298. {
  299. struct ath10k *ar = arvif->ar;
  300. u32 param;
  301. int ret;
  302. param = ar->wmi.pdev_param->sta_kickout_th;
  303. ret = ath10k_wmi_pdev_set_param(ar, param,
  304. ATH10K_KICKOUT_THRESHOLD);
  305. if (ret) {
  306. ath10k_warn(ar, "failed to set kickout threshold on vdev %i: %d\n",
  307. arvif->vdev_id, ret);
  308. return ret;
  309. }
  310. param = ar->wmi.vdev_param->ap_keepalive_min_idle_inactive_time_secs;
  311. ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, param,
  312. ATH10K_KEEPALIVE_MIN_IDLE);
  313. if (ret) {
  314. ath10k_warn(ar, "failed to set keepalive minimum idle time on vdev %i: %d\n",
  315. arvif->vdev_id, ret);
  316. return ret;
  317. }
  318. param = ar->wmi.vdev_param->ap_keepalive_max_idle_inactive_time_secs;
  319. ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, param,
  320. ATH10K_KEEPALIVE_MAX_IDLE);
  321. if (ret) {
  322. ath10k_warn(ar, "failed to set keepalive maximum idle time on vdev %i: %d\n",
  323. arvif->vdev_id, ret);
  324. return ret;
  325. }
  326. param = ar->wmi.vdev_param->ap_keepalive_max_unresponsive_time_secs;
  327. ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, param,
  328. ATH10K_KEEPALIVE_MAX_UNRESPONSIVE);
  329. if (ret) {
  330. ath10k_warn(ar, "failed to set keepalive maximum unresponsive time on vdev %i: %d\n",
  331. arvif->vdev_id, ret);
  332. return ret;
  333. }
  334. return 0;
  335. }
  336. static int ath10k_mac_set_rts(struct ath10k_vif *arvif, u32 value)
  337. {
  338. struct ath10k *ar = arvif->ar;
  339. u32 vdev_param;
  340. if (value != 0xFFFFFFFF)
  341. value = min_t(u32, arvif->ar->hw->wiphy->rts_threshold,
  342. ATH10K_RTS_MAX);
  343. vdev_param = ar->wmi.vdev_param->rts_threshold;
  344. return ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param, value);
  345. }
  346. static int ath10k_mac_set_frag(struct ath10k_vif *arvif, u32 value)
  347. {
  348. struct ath10k *ar = arvif->ar;
  349. u32 vdev_param;
  350. if (value != 0xFFFFFFFF)
  351. value = clamp_t(u32, arvif->ar->hw->wiphy->frag_threshold,
  352. ATH10K_FRAGMT_THRESHOLD_MIN,
  353. ATH10K_FRAGMT_THRESHOLD_MAX);
  354. vdev_param = ar->wmi.vdev_param->fragmentation_threshold;
  355. return ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param, value);
  356. }
  357. static int ath10k_peer_delete(struct ath10k *ar, u32 vdev_id, const u8 *addr)
  358. {
  359. int ret;
  360. lockdep_assert_held(&ar->conf_mutex);
  361. ret = ath10k_wmi_peer_delete(ar, vdev_id, addr);
  362. if (ret)
  363. return ret;
  364. ret = ath10k_wait_for_peer_deleted(ar, vdev_id, addr);
  365. if (ret)
  366. return ret;
  367. spin_lock_bh(&ar->data_lock);
  368. ar->num_peers--;
  369. spin_unlock_bh(&ar->data_lock);
  370. return 0;
  371. }
  372. static void ath10k_peer_cleanup(struct ath10k *ar, u32 vdev_id)
  373. {
  374. struct ath10k_peer *peer, *tmp;
  375. lockdep_assert_held(&ar->conf_mutex);
  376. spin_lock_bh(&ar->data_lock);
  377. list_for_each_entry_safe(peer, tmp, &ar->peers, list) {
  378. if (peer->vdev_id != vdev_id)
  379. continue;
  380. ath10k_warn(ar, "removing stale peer %pM from vdev_id %d\n",
  381. peer->addr, vdev_id);
  382. list_del(&peer->list);
  383. kfree(peer);
  384. ar->num_peers--;
  385. }
  386. spin_unlock_bh(&ar->data_lock);
  387. }
  388. static void ath10k_peer_cleanup_all(struct ath10k *ar)
  389. {
  390. struct ath10k_peer *peer, *tmp;
  391. lockdep_assert_held(&ar->conf_mutex);
  392. spin_lock_bh(&ar->data_lock);
  393. list_for_each_entry_safe(peer, tmp, &ar->peers, list) {
  394. list_del(&peer->list);
  395. kfree(peer);
  396. }
  397. ar->num_peers = 0;
  398. spin_unlock_bh(&ar->data_lock);
  399. }
  400. /************************/
  401. /* Interface management */
  402. /************************/
  403. static inline int ath10k_vdev_setup_sync(struct ath10k *ar)
  404. {
  405. int ret;
  406. lockdep_assert_held(&ar->conf_mutex);
  407. ret = wait_for_completion_timeout(&ar->vdev_setup_done,
  408. ATH10K_VDEV_SETUP_TIMEOUT_HZ);
  409. if (ret == 0)
  410. return -ETIMEDOUT;
  411. return 0;
  412. }
  413. static int ath10k_monitor_vdev_start(struct ath10k *ar, int vdev_id)
  414. {
  415. struct cfg80211_chan_def *chandef = &ar->chandef;
  416. struct ieee80211_channel *channel = chandef->chan;
  417. struct wmi_vdev_start_request_arg arg = {};
  418. int ret = 0;
  419. lockdep_assert_held(&ar->conf_mutex);
  420. arg.vdev_id = vdev_id;
  421. arg.channel.freq = channel->center_freq;
  422. arg.channel.band_center_freq1 = chandef->center_freq1;
  423. /* TODO setup this dynamically, what in case we
  424. don't have any vifs? */
  425. arg.channel.mode = chan_to_phymode(chandef);
  426. arg.channel.chan_radar =
  427. !!(channel->flags & IEEE80211_CHAN_RADAR);
  428. arg.channel.min_power = 0;
  429. arg.channel.max_power = channel->max_power * 2;
  430. arg.channel.max_reg_power = channel->max_reg_power * 2;
  431. arg.channel.max_antenna_gain = channel->max_antenna_gain * 2;
  432. ret = ath10k_wmi_vdev_start(ar, &arg);
  433. if (ret) {
  434. ath10k_warn(ar, "failed to request monitor vdev %i start: %d\n",
  435. vdev_id, ret);
  436. return ret;
  437. }
  438. ret = ath10k_vdev_setup_sync(ar);
  439. if (ret) {
  440. ath10k_warn(ar, "failed to synchronize setup for monitor vdev %i: %d\n",
  441. vdev_id, ret);
  442. return ret;
  443. }
  444. ret = ath10k_wmi_vdev_up(ar, vdev_id, 0, ar->mac_addr);
  445. if (ret) {
  446. ath10k_warn(ar, "failed to put up monitor vdev %i: %d\n",
  447. vdev_id, ret);
  448. goto vdev_stop;
  449. }
  450. ar->monitor_vdev_id = vdev_id;
  451. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor vdev %i started\n",
  452. ar->monitor_vdev_id);
  453. return 0;
  454. vdev_stop:
  455. ret = ath10k_wmi_vdev_stop(ar, ar->monitor_vdev_id);
  456. if (ret)
  457. ath10k_warn(ar, "failed to stop monitor vdev %i after start failure: %d\n",
  458. ar->monitor_vdev_id, ret);
  459. return ret;
  460. }
  461. static int ath10k_monitor_vdev_stop(struct ath10k *ar)
  462. {
  463. int ret = 0;
  464. lockdep_assert_held(&ar->conf_mutex);
  465. ret = ath10k_wmi_vdev_down(ar, ar->monitor_vdev_id);
  466. if (ret)
  467. ath10k_warn(ar, "failed to put down monitor vdev %i: %d\n",
  468. ar->monitor_vdev_id, ret);
  469. ret = ath10k_wmi_vdev_stop(ar, ar->monitor_vdev_id);
  470. if (ret)
  471. ath10k_warn(ar, "failed to to request monitor vdev %i stop: %d\n",
  472. ar->monitor_vdev_id, ret);
  473. ret = ath10k_vdev_setup_sync(ar);
  474. if (ret)
  475. ath10k_warn(ar, "failed to synchronise monitor vdev %i: %d\n",
  476. ar->monitor_vdev_id, ret);
  477. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor vdev %i stopped\n",
  478. ar->monitor_vdev_id);
  479. return ret;
  480. }
  481. static int ath10k_monitor_vdev_create(struct ath10k *ar)
  482. {
  483. int bit, ret = 0;
  484. lockdep_assert_held(&ar->conf_mutex);
  485. if (ar->free_vdev_map == 0) {
  486. ath10k_warn(ar, "failed to find free vdev id for monitor vdev\n");
  487. return -ENOMEM;
  488. }
  489. bit = ffs(ar->free_vdev_map);
  490. ar->monitor_vdev_id = bit - 1;
  491. ret = ath10k_wmi_vdev_create(ar, ar->monitor_vdev_id,
  492. WMI_VDEV_TYPE_MONITOR,
  493. 0, ar->mac_addr);
  494. if (ret) {
  495. ath10k_warn(ar, "failed to request monitor vdev %i creation: %d\n",
  496. ar->monitor_vdev_id, ret);
  497. return ret;
  498. }
  499. ar->free_vdev_map &= ~(1 << ar->monitor_vdev_id);
  500. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor vdev %d created\n",
  501. ar->monitor_vdev_id);
  502. return 0;
  503. }
  504. static int ath10k_monitor_vdev_delete(struct ath10k *ar)
  505. {
  506. int ret = 0;
  507. lockdep_assert_held(&ar->conf_mutex);
  508. ret = ath10k_wmi_vdev_delete(ar, ar->monitor_vdev_id);
  509. if (ret) {
  510. ath10k_warn(ar, "failed to request wmi monitor vdev %i removal: %d\n",
  511. ar->monitor_vdev_id, ret);
  512. return ret;
  513. }
  514. ar->free_vdev_map |= 1 << ar->monitor_vdev_id;
  515. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor vdev %d deleted\n",
  516. ar->monitor_vdev_id);
  517. return ret;
  518. }
  519. static int ath10k_monitor_start(struct ath10k *ar)
  520. {
  521. int ret;
  522. lockdep_assert_held(&ar->conf_mutex);
  523. ret = ath10k_monitor_vdev_create(ar);
  524. if (ret) {
  525. ath10k_warn(ar, "failed to create monitor vdev: %d\n", ret);
  526. return ret;
  527. }
  528. ret = ath10k_monitor_vdev_start(ar, ar->monitor_vdev_id);
  529. if (ret) {
  530. ath10k_warn(ar, "failed to start monitor vdev: %d\n", ret);
  531. ath10k_monitor_vdev_delete(ar);
  532. return ret;
  533. }
  534. ar->monitor_started = true;
  535. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor started\n");
  536. return 0;
  537. }
  538. static int ath10k_monitor_stop(struct ath10k *ar)
  539. {
  540. int ret;
  541. lockdep_assert_held(&ar->conf_mutex);
  542. ret = ath10k_monitor_vdev_stop(ar);
  543. if (ret) {
  544. ath10k_warn(ar, "failed to stop monitor vdev: %d\n", ret);
  545. return ret;
  546. }
  547. ret = ath10k_monitor_vdev_delete(ar);
  548. if (ret) {
  549. ath10k_warn(ar, "failed to delete monitor vdev: %d\n", ret);
  550. return ret;
  551. }
  552. ar->monitor_started = false;
  553. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor stopped\n");
  554. return 0;
  555. }
  556. static int ath10k_monitor_recalc(struct ath10k *ar)
  557. {
  558. bool should_start;
  559. lockdep_assert_held(&ar->conf_mutex);
  560. should_start = ar->monitor ||
  561. ar->filter_flags & FIF_PROMISC_IN_BSS ||
  562. test_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
  563. ath10k_dbg(ar, ATH10K_DBG_MAC,
  564. "mac monitor recalc started? %d should? %d\n",
  565. ar->monitor_started, should_start);
  566. if (should_start == ar->monitor_started)
  567. return 0;
  568. if (should_start)
  569. return ath10k_monitor_start(ar);
  570. return ath10k_monitor_stop(ar);
  571. }
  572. static int ath10k_recalc_rtscts_prot(struct ath10k_vif *arvif)
  573. {
  574. struct ath10k *ar = arvif->ar;
  575. u32 vdev_param, rts_cts = 0;
  576. lockdep_assert_held(&ar->conf_mutex);
  577. vdev_param = ar->wmi.vdev_param->enable_rtscts;
  578. if (arvif->use_cts_prot || arvif->num_legacy_stations > 0)
  579. rts_cts |= SM(WMI_RTSCTS_ENABLED, WMI_RTSCTS_SET);
  580. if (arvif->num_legacy_stations > 0)
  581. rts_cts |= SM(WMI_RTSCTS_ACROSS_SW_RETRIES,
  582. WMI_RTSCTS_PROFILE);
  583. return ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
  584. rts_cts);
  585. }
  586. static int ath10k_start_cac(struct ath10k *ar)
  587. {
  588. int ret;
  589. lockdep_assert_held(&ar->conf_mutex);
  590. set_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
  591. ret = ath10k_monitor_recalc(ar);
  592. if (ret) {
  593. ath10k_warn(ar, "failed to start monitor (cac): %d\n", ret);
  594. clear_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
  595. return ret;
  596. }
  597. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac cac start monitor vdev %d\n",
  598. ar->monitor_vdev_id);
  599. return 0;
  600. }
  601. static int ath10k_stop_cac(struct ath10k *ar)
  602. {
  603. lockdep_assert_held(&ar->conf_mutex);
  604. /* CAC is not running - do nothing */
  605. if (!test_bit(ATH10K_CAC_RUNNING, &ar->dev_flags))
  606. return 0;
  607. clear_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
  608. ath10k_monitor_stop(ar);
  609. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac cac finished\n");
  610. return 0;
  611. }
  612. static void ath10k_recalc_radar_detection(struct ath10k *ar)
  613. {
  614. int ret;
  615. lockdep_assert_held(&ar->conf_mutex);
  616. ath10k_stop_cac(ar);
  617. if (!ar->radar_enabled)
  618. return;
  619. if (ar->num_started_vdevs > 0)
  620. return;
  621. ret = ath10k_start_cac(ar);
  622. if (ret) {
  623. /*
  624. * Not possible to start CAC on current channel so starting
  625. * radiation is not allowed, make this channel DFS_UNAVAILABLE
  626. * by indicating that radar was detected.
  627. */
  628. ath10k_warn(ar, "failed to start CAC: %d\n", ret);
  629. ieee80211_radar_detected(ar->hw);
  630. }
  631. }
  632. static int ath10k_vdev_start_restart(struct ath10k_vif *arvif, bool restart)
  633. {
  634. struct ath10k *ar = arvif->ar;
  635. struct cfg80211_chan_def *chandef = &ar->chandef;
  636. struct wmi_vdev_start_request_arg arg = {};
  637. int ret = 0;
  638. lockdep_assert_held(&ar->conf_mutex);
  639. reinit_completion(&ar->vdev_setup_done);
  640. arg.vdev_id = arvif->vdev_id;
  641. arg.dtim_period = arvif->dtim_period;
  642. arg.bcn_intval = arvif->beacon_interval;
  643. arg.channel.freq = chandef->chan->center_freq;
  644. arg.channel.band_center_freq1 = chandef->center_freq1;
  645. arg.channel.mode = chan_to_phymode(chandef);
  646. arg.channel.min_power = 0;
  647. arg.channel.max_power = chandef->chan->max_power * 2;
  648. arg.channel.max_reg_power = chandef->chan->max_reg_power * 2;
  649. arg.channel.max_antenna_gain = chandef->chan->max_antenna_gain * 2;
  650. if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
  651. arg.ssid = arvif->u.ap.ssid;
  652. arg.ssid_len = arvif->u.ap.ssid_len;
  653. arg.hidden_ssid = arvif->u.ap.hidden_ssid;
  654. /* For now allow DFS for AP mode */
  655. arg.channel.chan_radar =
  656. !!(chandef->chan->flags & IEEE80211_CHAN_RADAR);
  657. } else if (arvif->vdev_type == WMI_VDEV_TYPE_IBSS) {
  658. arg.ssid = arvif->vif->bss_conf.ssid;
  659. arg.ssid_len = arvif->vif->bss_conf.ssid_len;
  660. }
  661. ath10k_dbg(ar, ATH10K_DBG_MAC,
  662. "mac vdev %d start center_freq %d phymode %s\n",
  663. arg.vdev_id, arg.channel.freq,
  664. ath10k_wmi_phymode_str(arg.channel.mode));
  665. if (restart)
  666. ret = ath10k_wmi_vdev_restart(ar, &arg);
  667. else
  668. ret = ath10k_wmi_vdev_start(ar, &arg);
  669. if (ret) {
  670. ath10k_warn(ar, "failed to start WMI vdev %i: %d\n",
  671. arg.vdev_id, ret);
  672. return ret;
  673. }
  674. ret = ath10k_vdev_setup_sync(ar);
  675. if (ret) {
  676. ath10k_warn(ar, "failed to synchronise setup for vdev %i: %d\n",
  677. arg.vdev_id, ret);
  678. return ret;
  679. }
  680. ar->num_started_vdevs++;
  681. ath10k_recalc_radar_detection(ar);
  682. return ret;
  683. }
  684. static int ath10k_vdev_start(struct ath10k_vif *arvif)
  685. {
  686. return ath10k_vdev_start_restart(arvif, false);
  687. }
  688. static int ath10k_vdev_restart(struct ath10k_vif *arvif)
  689. {
  690. return ath10k_vdev_start_restart(arvif, true);
  691. }
  692. static int ath10k_vdev_stop(struct ath10k_vif *arvif)
  693. {
  694. struct ath10k *ar = arvif->ar;
  695. int ret;
  696. lockdep_assert_held(&ar->conf_mutex);
  697. reinit_completion(&ar->vdev_setup_done);
  698. ret = ath10k_wmi_vdev_stop(ar, arvif->vdev_id);
  699. if (ret) {
  700. ath10k_warn(ar, "failed to stop WMI vdev %i: %d\n",
  701. arvif->vdev_id, ret);
  702. return ret;
  703. }
  704. ret = ath10k_vdev_setup_sync(ar);
  705. if (ret) {
  706. ath10k_warn(ar, "failed to syncronise setup for vdev %i: %d\n",
  707. arvif->vdev_id, ret);
  708. return ret;
  709. }
  710. WARN_ON(ar->num_started_vdevs == 0);
  711. if (ar->num_started_vdevs != 0) {
  712. ar->num_started_vdevs--;
  713. ath10k_recalc_radar_detection(ar);
  714. }
  715. return ret;
  716. }
  717. static void ath10k_control_beaconing(struct ath10k_vif *arvif,
  718. struct ieee80211_bss_conf *info)
  719. {
  720. struct ath10k *ar = arvif->ar;
  721. int ret = 0;
  722. lockdep_assert_held(&arvif->ar->conf_mutex);
  723. if (!info->enable_beacon) {
  724. ath10k_vdev_stop(arvif);
  725. arvif->is_started = false;
  726. arvif->is_up = false;
  727. spin_lock_bh(&arvif->ar->data_lock);
  728. if (arvif->beacon) {
  729. dma_unmap_single(arvif->ar->dev,
  730. ATH10K_SKB_CB(arvif->beacon)->paddr,
  731. arvif->beacon->len, DMA_TO_DEVICE);
  732. dev_kfree_skb_any(arvif->beacon);
  733. arvif->beacon = NULL;
  734. arvif->beacon_sent = false;
  735. }
  736. spin_unlock_bh(&arvif->ar->data_lock);
  737. return;
  738. }
  739. arvif->tx_seq_no = 0x1000;
  740. ret = ath10k_vdev_start(arvif);
  741. if (ret)
  742. return;
  743. arvif->aid = 0;
  744. ether_addr_copy(arvif->bssid, info->bssid);
  745. ret = ath10k_wmi_vdev_up(arvif->ar, arvif->vdev_id, arvif->aid,
  746. arvif->bssid);
  747. if (ret) {
  748. ath10k_warn(ar, "failed to bring up vdev %d: %i\n",
  749. arvif->vdev_id, ret);
  750. ath10k_vdev_stop(arvif);
  751. return;
  752. }
  753. arvif->is_started = true;
  754. arvif->is_up = true;
  755. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d up\n", arvif->vdev_id);
  756. }
  757. static void ath10k_control_ibss(struct ath10k_vif *arvif,
  758. struct ieee80211_bss_conf *info,
  759. const u8 self_peer[ETH_ALEN])
  760. {
  761. struct ath10k *ar = arvif->ar;
  762. u32 vdev_param;
  763. int ret = 0;
  764. lockdep_assert_held(&arvif->ar->conf_mutex);
  765. if (!info->ibss_joined) {
  766. ret = ath10k_peer_delete(arvif->ar, arvif->vdev_id, self_peer);
  767. if (ret)
  768. ath10k_warn(ar, "failed to delete IBSS self peer %pM for vdev %d: %d\n",
  769. self_peer, arvif->vdev_id, ret);
  770. if (is_zero_ether_addr(arvif->bssid))
  771. return;
  772. ret = ath10k_peer_delete(arvif->ar, arvif->vdev_id,
  773. arvif->bssid);
  774. if (ret) {
  775. ath10k_warn(ar, "failed to delete IBSS BSSID peer %pM for vdev %d: %d\n",
  776. arvif->bssid, arvif->vdev_id, ret);
  777. return;
  778. }
  779. memset(arvif->bssid, 0, ETH_ALEN);
  780. return;
  781. }
  782. ret = ath10k_peer_create(arvif->ar, arvif->vdev_id, self_peer);
  783. if (ret) {
  784. ath10k_warn(ar, "failed to create IBSS self peer %pM for vdev %d: %d\n",
  785. self_peer, arvif->vdev_id, ret);
  786. return;
  787. }
  788. vdev_param = arvif->ar->wmi.vdev_param->atim_window;
  789. ret = ath10k_wmi_vdev_set_param(arvif->ar, arvif->vdev_id, vdev_param,
  790. ATH10K_DEFAULT_ATIM);
  791. if (ret)
  792. ath10k_warn(ar, "failed to set IBSS ATIM for vdev %d: %d\n",
  793. arvif->vdev_id, ret);
  794. }
  795. /*
  796. * Review this when mac80211 gains per-interface powersave support.
  797. */
  798. static int ath10k_mac_vif_setup_ps(struct ath10k_vif *arvif)
  799. {
  800. struct ath10k *ar = arvif->ar;
  801. struct ieee80211_conf *conf = &ar->hw->conf;
  802. enum wmi_sta_powersave_param param;
  803. enum wmi_sta_ps_mode psmode;
  804. int ret;
  805. lockdep_assert_held(&arvif->ar->conf_mutex);
  806. if (arvif->vif->type != NL80211_IFTYPE_STATION)
  807. return 0;
  808. if (conf->flags & IEEE80211_CONF_PS) {
  809. psmode = WMI_STA_PS_MODE_ENABLED;
  810. param = WMI_STA_PS_PARAM_INACTIVITY_TIME;
  811. ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id, param,
  812. conf->dynamic_ps_timeout);
  813. if (ret) {
  814. ath10k_warn(ar, "failed to set inactivity time for vdev %d: %i\n",
  815. arvif->vdev_id, ret);
  816. return ret;
  817. }
  818. } else {
  819. psmode = WMI_STA_PS_MODE_DISABLED;
  820. }
  821. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d psmode %s\n",
  822. arvif->vdev_id, psmode ? "enable" : "disable");
  823. ret = ath10k_wmi_set_psmode(ar, arvif->vdev_id, psmode);
  824. if (ret) {
  825. ath10k_warn(ar, "failed to set PS Mode %d for vdev %d: %d\n",
  826. psmode, arvif->vdev_id, ret);
  827. return ret;
  828. }
  829. return 0;
  830. }
  831. /**********************/
  832. /* Station management */
  833. /**********************/
  834. static void ath10k_peer_assoc_h_basic(struct ath10k *ar,
  835. struct ath10k_vif *arvif,
  836. struct ieee80211_sta *sta,
  837. struct ieee80211_bss_conf *bss_conf,
  838. struct wmi_peer_assoc_complete_arg *arg)
  839. {
  840. lockdep_assert_held(&ar->conf_mutex);
  841. ether_addr_copy(arg->addr, sta->addr);
  842. arg->vdev_id = arvif->vdev_id;
  843. arg->peer_aid = sta->aid;
  844. arg->peer_flags |= WMI_PEER_AUTH;
  845. if (arvif->vdev_type == WMI_VDEV_TYPE_STA)
  846. /*
  847. * Seems FW have problems with Power Save in STA
  848. * mode when we setup this parameter to high (eg. 5).
  849. * Often we see that FW don't send NULL (with clean P flags)
  850. * frame even there is info about buffered frames in beacons.
  851. * Sometimes we have to wait more than 10 seconds before FW
  852. * will wakeup. Often sending one ping from AP to our device
  853. * just fail (more than 50%).
  854. *
  855. * Seems setting this FW parameter to 1 couse FW
  856. * will check every beacon and will wakup immediately
  857. * after detection buffered data.
  858. */
  859. arg->peer_listen_intval = 1;
  860. else
  861. arg->peer_listen_intval = ar->hw->conf.listen_interval;
  862. arg->peer_num_spatial_streams = 1;
  863. /*
  864. * The assoc capabilities are available only in managed mode.
  865. */
  866. if (arvif->vdev_type == WMI_VDEV_TYPE_STA && bss_conf)
  867. arg->peer_caps = bss_conf->assoc_capability;
  868. }
  869. static void ath10k_peer_assoc_h_crypto(struct ath10k *ar,
  870. struct ath10k_vif *arvif,
  871. struct wmi_peer_assoc_complete_arg *arg)
  872. {
  873. struct ieee80211_vif *vif = arvif->vif;
  874. struct ieee80211_bss_conf *info = &vif->bss_conf;
  875. struct cfg80211_bss *bss;
  876. const u8 *rsnie = NULL;
  877. const u8 *wpaie = NULL;
  878. lockdep_assert_held(&ar->conf_mutex);
  879. bss = cfg80211_get_bss(ar->hw->wiphy, ar->hw->conf.chandef.chan,
  880. info->bssid, NULL, 0, 0, 0);
  881. if (bss) {
  882. const struct cfg80211_bss_ies *ies;
  883. rcu_read_lock();
  884. rsnie = ieee80211_bss_get_ie(bss, WLAN_EID_RSN);
  885. ies = rcu_dereference(bss->ies);
  886. wpaie = cfg80211_find_vendor_ie(WLAN_OUI_MICROSOFT,
  887. WLAN_OUI_TYPE_MICROSOFT_WPA,
  888. ies->data,
  889. ies->len);
  890. rcu_read_unlock();
  891. cfg80211_put_bss(ar->hw->wiphy, bss);
  892. }
  893. /* FIXME: base on RSN IE/WPA IE is a correct idea? */
  894. if (rsnie || wpaie) {
  895. ath10k_dbg(ar, ATH10K_DBG_WMI, "%s: rsn ie found\n", __func__);
  896. arg->peer_flags |= WMI_PEER_NEED_PTK_4_WAY;
  897. }
  898. if (wpaie) {
  899. ath10k_dbg(ar, ATH10K_DBG_WMI, "%s: wpa ie found\n", __func__);
  900. arg->peer_flags |= WMI_PEER_NEED_GTK_2_WAY;
  901. }
  902. }
  903. static void ath10k_peer_assoc_h_rates(struct ath10k *ar,
  904. struct ieee80211_sta *sta,
  905. struct wmi_peer_assoc_complete_arg *arg)
  906. {
  907. struct wmi_rate_set_arg *rateset = &arg->peer_legacy_rates;
  908. const struct ieee80211_supported_band *sband;
  909. const struct ieee80211_rate *rates;
  910. u32 ratemask;
  911. int i;
  912. lockdep_assert_held(&ar->conf_mutex);
  913. sband = ar->hw->wiphy->bands[ar->hw->conf.chandef.chan->band];
  914. ratemask = sta->supp_rates[ar->hw->conf.chandef.chan->band];
  915. rates = sband->bitrates;
  916. rateset->num_rates = 0;
  917. for (i = 0; i < 32; i++, ratemask >>= 1, rates++) {
  918. if (!(ratemask & 1))
  919. continue;
  920. rateset->rates[rateset->num_rates] = rates->hw_value;
  921. rateset->num_rates++;
  922. }
  923. }
  924. static void ath10k_peer_assoc_h_ht(struct ath10k *ar,
  925. struct ieee80211_sta *sta,
  926. struct wmi_peer_assoc_complete_arg *arg)
  927. {
  928. const struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
  929. int i, n;
  930. u32 stbc;
  931. lockdep_assert_held(&ar->conf_mutex);
  932. if (!ht_cap->ht_supported)
  933. return;
  934. arg->peer_flags |= WMI_PEER_HT;
  935. arg->peer_max_mpdu = (1 << (IEEE80211_HT_MAX_AMPDU_FACTOR +
  936. ht_cap->ampdu_factor)) - 1;
  937. arg->peer_mpdu_density =
  938. ath10k_parse_mpdudensity(ht_cap->ampdu_density);
  939. arg->peer_ht_caps = ht_cap->cap;
  940. arg->peer_rate_caps |= WMI_RC_HT_FLAG;
  941. if (ht_cap->cap & IEEE80211_HT_CAP_LDPC_CODING)
  942. arg->peer_flags |= WMI_PEER_LDPC;
  943. if (sta->bandwidth >= IEEE80211_STA_RX_BW_40) {
  944. arg->peer_flags |= WMI_PEER_40MHZ;
  945. arg->peer_rate_caps |= WMI_RC_CW40_FLAG;
  946. }
  947. if (ht_cap->cap & IEEE80211_HT_CAP_SGI_20)
  948. arg->peer_rate_caps |= WMI_RC_SGI_FLAG;
  949. if (ht_cap->cap & IEEE80211_HT_CAP_SGI_40)
  950. arg->peer_rate_caps |= WMI_RC_SGI_FLAG;
  951. if (ht_cap->cap & IEEE80211_HT_CAP_TX_STBC) {
  952. arg->peer_rate_caps |= WMI_RC_TX_STBC_FLAG;
  953. arg->peer_flags |= WMI_PEER_STBC;
  954. }
  955. if (ht_cap->cap & IEEE80211_HT_CAP_RX_STBC) {
  956. stbc = ht_cap->cap & IEEE80211_HT_CAP_RX_STBC;
  957. stbc = stbc >> IEEE80211_HT_CAP_RX_STBC_SHIFT;
  958. stbc = stbc << WMI_RC_RX_STBC_FLAG_S;
  959. arg->peer_rate_caps |= stbc;
  960. arg->peer_flags |= WMI_PEER_STBC;
  961. }
  962. if (ht_cap->mcs.rx_mask[1] && ht_cap->mcs.rx_mask[2])
  963. arg->peer_rate_caps |= WMI_RC_TS_FLAG;
  964. else if (ht_cap->mcs.rx_mask[1])
  965. arg->peer_rate_caps |= WMI_RC_DS_FLAG;
  966. for (i = 0, n = 0; i < IEEE80211_HT_MCS_MASK_LEN*8; i++)
  967. if (ht_cap->mcs.rx_mask[i/8] & (1 << i%8))
  968. arg->peer_ht_rates.rates[n++] = i;
  969. /*
  970. * This is a workaround for HT-enabled STAs which break the spec
  971. * and have no HT capabilities RX mask (no HT RX MCS map).
  972. *
  973. * As per spec, in section 20.3.5 Modulation and coding scheme (MCS),
  974. * MCS 0 through 7 are mandatory in 20MHz with 800 ns GI at all STAs.
  975. *
  976. * Firmware asserts if such situation occurs.
  977. */
  978. if (n == 0) {
  979. arg->peer_ht_rates.num_rates = 8;
  980. for (i = 0; i < arg->peer_ht_rates.num_rates; i++)
  981. arg->peer_ht_rates.rates[i] = i;
  982. } else {
  983. arg->peer_ht_rates.num_rates = n;
  984. arg->peer_num_spatial_streams = sta->rx_nss;
  985. }
  986. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac ht peer %pM mcs cnt %d nss %d\n",
  987. arg->addr,
  988. arg->peer_ht_rates.num_rates,
  989. arg->peer_num_spatial_streams);
  990. }
  991. static int ath10k_peer_assoc_qos_ap(struct ath10k *ar,
  992. struct ath10k_vif *arvif,
  993. struct ieee80211_sta *sta)
  994. {
  995. u32 uapsd = 0;
  996. u32 max_sp = 0;
  997. int ret = 0;
  998. lockdep_assert_held(&ar->conf_mutex);
  999. if (sta->wme && sta->uapsd_queues) {
  1000. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac uapsd_queues 0x%x max_sp %d\n",
  1001. sta->uapsd_queues, sta->max_sp);
  1002. if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
  1003. uapsd |= WMI_AP_PS_UAPSD_AC3_DELIVERY_EN |
  1004. WMI_AP_PS_UAPSD_AC3_TRIGGER_EN;
  1005. if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
  1006. uapsd |= WMI_AP_PS_UAPSD_AC2_DELIVERY_EN |
  1007. WMI_AP_PS_UAPSD_AC2_TRIGGER_EN;
  1008. if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
  1009. uapsd |= WMI_AP_PS_UAPSD_AC1_DELIVERY_EN |
  1010. WMI_AP_PS_UAPSD_AC1_TRIGGER_EN;
  1011. if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
  1012. uapsd |= WMI_AP_PS_UAPSD_AC0_DELIVERY_EN |
  1013. WMI_AP_PS_UAPSD_AC0_TRIGGER_EN;
  1014. if (sta->max_sp < MAX_WMI_AP_PS_PEER_PARAM_MAX_SP)
  1015. max_sp = sta->max_sp;
  1016. ret = ath10k_wmi_set_ap_ps_param(ar, arvif->vdev_id,
  1017. sta->addr,
  1018. WMI_AP_PS_PEER_PARAM_UAPSD,
  1019. uapsd);
  1020. if (ret) {
  1021. ath10k_warn(ar, "failed to set ap ps peer param uapsd for vdev %i: %d\n",
  1022. arvif->vdev_id, ret);
  1023. return ret;
  1024. }
  1025. ret = ath10k_wmi_set_ap_ps_param(ar, arvif->vdev_id,
  1026. sta->addr,
  1027. WMI_AP_PS_PEER_PARAM_MAX_SP,
  1028. max_sp);
  1029. if (ret) {
  1030. ath10k_warn(ar, "failed to set ap ps peer param max sp for vdev %i: %d\n",
  1031. arvif->vdev_id, ret);
  1032. return ret;
  1033. }
  1034. /* TODO setup this based on STA listen interval and
  1035. beacon interval. Currently we don't know
  1036. sta->listen_interval - mac80211 patch required.
  1037. Currently use 10 seconds */
  1038. ret = ath10k_wmi_set_ap_ps_param(ar, arvif->vdev_id, sta->addr,
  1039. WMI_AP_PS_PEER_PARAM_AGEOUT_TIME,
  1040. 10);
  1041. if (ret) {
  1042. ath10k_warn(ar, "failed to set ap ps peer param ageout time for vdev %i: %d\n",
  1043. arvif->vdev_id, ret);
  1044. return ret;
  1045. }
  1046. }
  1047. return 0;
  1048. }
  1049. static void ath10k_peer_assoc_h_vht(struct ath10k *ar,
  1050. struct ieee80211_sta *sta,
  1051. struct wmi_peer_assoc_complete_arg *arg)
  1052. {
  1053. const struct ieee80211_sta_vht_cap *vht_cap = &sta->vht_cap;
  1054. u8 ampdu_factor;
  1055. if (!vht_cap->vht_supported)
  1056. return;
  1057. arg->peer_flags |= WMI_PEER_VHT;
  1058. arg->peer_vht_caps = vht_cap->cap;
  1059. ampdu_factor = (vht_cap->cap &
  1060. IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK) >>
  1061. IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
  1062. /* Workaround: Some Netgear/Linksys 11ac APs set Rx A-MPDU factor to
  1063. * zero in VHT IE. Using it would result in degraded throughput.
  1064. * arg->peer_max_mpdu at this point contains HT max_mpdu so keep
  1065. * it if VHT max_mpdu is smaller. */
  1066. arg->peer_max_mpdu = max(arg->peer_max_mpdu,
  1067. (1U << (IEEE80211_HT_MAX_AMPDU_FACTOR +
  1068. ampdu_factor)) - 1);
  1069. if (sta->bandwidth == IEEE80211_STA_RX_BW_80)
  1070. arg->peer_flags |= WMI_PEER_80MHZ;
  1071. arg->peer_vht_rates.rx_max_rate =
  1072. __le16_to_cpu(vht_cap->vht_mcs.rx_highest);
  1073. arg->peer_vht_rates.rx_mcs_set =
  1074. __le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map);
  1075. arg->peer_vht_rates.tx_max_rate =
  1076. __le16_to_cpu(vht_cap->vht_mcs.tx_highest);
  1077. arg->peer_vht_rates.tx_mcs_set =
  1078. __le16_to_cpu(vht_cap->vht_mcs.tx_mcs_map);
  1079. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vht peer %pM max_mpdu %d flags 0x%x\n",
  1080. sta->addr, arg->peer_max_mpdu, arg->peer_flags);
  1081. }
  1082. static void ath10k_peer_assoc_h_qos(struct ath10k *ar,
  1083. struct ath10k_vif *arvif,
  1084. struct ieee80211_sta *sta,
  1085. struct ieee80211_bss_conf *bss_conf,
  1086. struct wmi_peer_assoc_complete_arg *arg)
  1087. {
  1088. switch (arvif->vdev_type) {
  1089. case WMI_VDEV_TYPE_AP:
  1090. if (sta->wme)
  1091. arg->peer_flags |= WMI_PEER_QOS;
  1092. if (sta->wme && sta->uapsd_queues) {
  1093. arg->peer_flags |= WMI_PEER_APSD;
  1094. arg->peer_rate_caps |= WMI_RC_UAPSD_FLAG;
  1095. }
  1096. break;
  1097. case WMI_VDEV_TYPE_STA:
  1098. if (bss_conf->qos)
  1099. arg->peer_flags |= WMI_PEER_QOS;
  1100. break;
  1101. default:
  1102. break;
  1103. }
  1104. }
  1105. static void ath10k_peer_assoc_h_phymode(struct ath10k *ar,
  1106. struct ath10k_vif *arvif,
  1107. struct ieee80211_sta *sta,
  1108. struct wmi_peer_assoc_complete_arg *arg)
  1109. {
  1110. enum wmi_phy_mode phymode = MODE_UNKNOWN;
  1111. switch (ar->hw->conf.chandef.chan->band) {
  1112. case IEEE80211_BAND_2GHZ:
  1113. if (sta->ht_cap.ht_supported) {
  1114. if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
  1115. phymode = MODE_11NG_HT40;
  1116. else
  1117. phymode = MODE_11NG_HT20;
  1118. } else {
  1119. phymode = MODE_11G;
  1120. }
  1121. break;
  1122. case IEEE80211_BAND_5GHZ:
  1123. /*
  1124. * Check VHT first.
  1125. */
  1126. if (sta->vht_cap.vht_supported) {
  1127. if (sta->bandwidth == IEEE80211_STA_RX_BW_80)
  1128. phymode = MODE_11AC_VHT80;
  1129. else if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
  1130. phymode = MODE_11AC_VHT40;
  1131. else if (sta->bandwidth == IEEE80211_STA_RX_BW_20)
  1132. phymode = MODE_11AC_VHT20;
  1133. } else if (sta->ht_cap.ht_supported) {
  1134. if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
  1135. phymode = MODE_11NA_HT40;
  1136. else
  1137. phymode = MODE_11NA_HT20;
  1138. } else {
  1139. phymode = MODE_11A;
  1140. }
  1141. break;
  1142. default:
  1143. break;
  1144. }
  1145. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac peer %pM phymode %s\n",
  1146. sta->addr, ath10k_wmi_phymode_str(phymode));
  1147. arg->peer_phymode = phymode;
  1148. WARN_ON(phymode == MODE_UNKNOWN);
  1149. }
  1150. static int ath10k_peer_assoc_prepare(struct ath10k *ar,
  1151. struct ath10k_vif *arvif,
  1152. struct ieee80211_sta *sta,
  1153. struct ieee80211_bss_conf *bss_conf,
  1154. struct wmi_peer_assoc_complete_arg *arg)
  1155. {
  1156. lockdep_assert_held(&ar->conf_mutex);
  1157. memset(arg, 0, sizeof(*arg));
  1158. ath10k_peer_assoc_h_basic(ar, arvif, sta, bss_conf, arg);
  1159. ath10k_peer_assoc_h_crypto(ar, arvif, arg);
  1160. ath10k_peer_assoc_h_rates(ar, sta, arg);
  1161. ath10k_peer_assoc_h_ht(ar, sta, arg);
  1162. ath10k_peer_assoc_h_vht(ar, sta, arg);
  1163. ath10k_peer_assoc_h_qos(ar, arvif, sta, bss_conf, arg);
  1164. ath10k_peer_assoc_h_phymode(ar, arvif, sta, arg);
  1165. return 0;
  1166. }
  1167. static const u32 ath10k_smps_map[] = {
  1168. [WLAN_HT_CAP_SM_PS_STATIC] = WMI_PEER_SMPS_STATIC,
  1169. [WLAN_HT_CAP_SM_PS_DYNAMIC] = WMI_PEER_SMPS_DYNAMIC,
  1170. [WLAN_HT_CAP_SM_PS_INVALID] = WMI_PEER_SMPS_PS_NONE,
  1171. [WLAN_HT_CAP_SM_PS_DISABLED] = WMI_PEER_SMPS_PS_NONE,
  1172. };
  1173. static int ath10k_setup_peer_smps(struct ath10k *ar, struct ath10k_vif *arvif,
  1174. const u8 *addr,
  1175. const struct ieee80211_sta_ht_cap *ht_cap)
  1176. {
  1177. int smps;
  1178. if (!ht_cap->ht_supported)
  1179. return 0;
  1180. smps = ht_cap->cap & IEEE80211_HT_CAP_SM_PS;
  1181. smps >>= IEEE80211_HT_CAP_SM_PS_SHIFT;
  1182. if (smps >= ARRAY_SIZE(ath10k_smps_map))
  1183. return -EINVAL;
  1184. return ath10k_wmi_peer_set_param(ar, arvif->vdev_id, addr,
  1185. WMI_PEER_SMPS_STATE,
  1186. ath10k_smps_map[smps]);
  1187. }
  1188. /* can be called only in mac80211 callbacks due to `key_count` usage */
  1189. static void ath10k_bss_assoc(struct ieee80211_hw *hw,
  1190. struct ieee80211_vif *vif,
  1191. struct ieee80211_bss_conf *bss_conf)
  1192. {
  1193. struct ath10k *ar = hw->priv;
  1194. struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
  1195. struct ieee80211_sta_ht_cap ht_cap;
  1196. struct wmi_peer_assoc_complete_arg peer_arg;
  1197. struct ieee80211_sta *ap_sta;
  1198. int ret;
  1199. lockdep_assert_held(&ar->conf_mutex);
  1200. rcu_read_lock();
  1201. ap_sta = ieee80211_find_sta(vif, bss_conf->bssid);
  1202. if (!ap_sta) {
  1203. ath10k_warn(ar, "failed to find station entry for bss %pM vdev %i\n",
  1204. bss_conf->bssid, arvif->vdev_id);
  1205. rcu_read_unlock();
  1206. return;
  1207. }
  1208. /* ap_sta must be accessed only within rcu section which must be left
  1209. * before calling ath10k_setup_peer_smps() which might sleep. */
  1210. ht_cap = ap_sta->ht_cap;
  1211. ret = ath10k_peer_assoc_prepare(ar, arvif, ap_sta,
  1212. bss_conf, &peer_arg);
  1213. if (ret) {
  1214. ath10k_warn(ar, "failed to prepare peer assoc for %pM vdev %i: %d\n",
  1215. bss_conf->bssid, arvif->vdev_id, ret);
  1216. rcu_read_unlock();
  1217. return;
  1218. }
  1219. rcu_read_unlock();
  1220. ret = ath10k_wmi_peer_assoc(ar, &peer_arg);
  1221. if (ret) {
  1222. ath10k_warn(ar, "failed to run peer assoc for %pM vdev %i: %d\n",
  1223. bss_conf->bssid, arvif->vdev_id, ret);
  1224. return;
  1225. }
  1226. ret = ath10k_setup_peer_smps(ar, arvif, bss_conf->bssid, &ht_cap);
  1227. if (ret) {
  1228. ath10k_warn(ar, "failed to setup peer SMPS for vdev %i: %d\n",
  1229. arvif->vdev_id, ret);
  1230. return;
  1231. }
  1232. ath10k_dbg(ar, ATH10K_DBG_MAC,
  1233. "mac vdev %d up (associated) bssid %pM aid %d\n",
  1234. arvif->vdev_id, bss_conf->bssid, bss_conf->aid);
  1235. arvif->aid = bss_conf->aid;
  1236. ether_addr_copy(arvif->bssid, bss_conf->bssid);
  1237. ret = ath10k_wmi_vdev_up(ar, arvif->vdev_id, arvif->aid, arvif->bssid);
  1238. if (ret) {
  1239. ath10k_warn(ar, "failed to set vdev %d up: %d\n",
  1240. arvif->vdev_id, ret);
  1241. return;
  1242. }
  1243. arvif->is_up = true;
  1244. }
  1245. /*
  1246. * FIXME: flush TIDs
  1247. */
  1248. static void ath10k_bss_disassoc(struct ieee80211_hw *hw,
  1249. struct ieee80211_vif *vif)
  1250. {
  1251. struct ath10k *ar = hw->priv;
  1252. struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
  1253. int ret;
  1254. lockdep_assert_held(&ar->conf_mutex);
  1255. /*
  1256. * For some reason, calling VDEV-DOWN before VDEV-STOP
  1257. * makes the FW to send frames via HTT after disassociation.
  1258. * No idea why this happens, even though VDEV-DOWN is supposed
  1259. * to be analogous to link down, so just stop the VDEV.
  1260. */
  1261. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d stop (disassociated\n",
  1262. arvif->vdev_id);
  1263. /* FIXME: check return value */
  1264. ret = ath10k_vdev_stop(arvif);
  1265. /*
  1266. * If we don't call VDEV-DOWN after VDEV-STOP FW will remain active and
  1267. * report beacons from previously associated network through HTT.
  1268. * This in turn would spam mac80211 WARN_ON if we bring down all
  1269. * interfaces as it expects there is no rx when no interface is
  1270. * running.
  1271. */
  1272. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d down\n", arvif->vdev_id);
  1273. /* FIXME: why don't we print error if wmi call fails? */
  1274. ret = ath10k_wmi_vdev_down(ar, arvif->vdev_id);
  1275. arvif->def_wep_key_idx = 0;
  1276. arvif->is_started = false;
  1277. arvif->is_up = false;
  1278. }
  1279. static int ath10k_station_assoc(struct ath10k *ar, struct ath10k_vif *arvif,
  1280. struct ieee80211_sta *sta, bool reassoc)
  1281. {
  1282. struct wmi_peer_assoc_complete_arg peer_arg;
  1283. int ret = 0;
  1284. lockdep_assert_held(&ar->conf_mutex);
  1285. ret = ath10k_peer_assoc_prepare(ar, arvif, sta, NULL, &peer_arg);
  1286. if (ret) {
  1287. ath10k_warn(ar, "failed to prepare WMI peer assoc for %pM vdev %i: %i\n",
  1288. sta->addr, arvif->vdev_id, ret);
  1289. return ret;
  1290. }
  1291. peer_arg.peer_reassoc = reassoc;
  1292. ret = ath10k_wmi_peer_assoc(ar, &peer_arg);
  1293. if (ret) {
  1294. ath10k_warn(ar, "failed to run peer assoc for STA %pM vdev %i: %d\n",
  1295. sta->addr, arvif->vdev_id, ret);
  1296. return ret;
  1297. }
  1298. ret = ath10k_setup_peer_smps(ar, arvif, sta->addr, &sta->ht_cap);
  1299. if (ret) {
  1300. ath10k_warn(ar, "failed to setup peer SMPS for vdev %d: %d\n",
  1301. arvif->vdev_id, ret);
  1302. return ret;
  1303. }
  1304. if (!sta->wme && !reassoc) {
  1305. arvif->num_legacy_stations++;
  1306. ret = ath10k_recalc_rtscts_prot(arvif);
  1307. if (ret) {
  1308. ath10k_warn(ar, "failed to recalculate rts/cts prot for vdev %d: %d\n",
  1309. arvif->vdev_id, ret);
  1310. return ret;
  1311. }
  1312. }
  1313. ret = ath10k_install_peer_wep_keys(arvif, sta->addr);
  1314. if (ret) {
  1315. ath10k_warn(ar, "failed to install peer wep keys for vdev %i: %d\n",
  1316. arvif->vdev_id, ret);
  1317. return ret;
  1318. }
  1319. ret = ath10k_peer_assoc_qos_ap(ar, arvif, sta);
  1320. if (ret) {
  1321. ath10k_warn(ar, "failed to set qos params for STA %pM for vdev %i: %d\n",
  1322. sta->addr, arvif->vdev_id, ret);
  1323. return ret;
  1324. }
  1325. return ret;
  1326. }
  1327. static int ath10k_station_disassoc(struct ath10k *ar, struct ath10k_vif *arvif,
  1328. struct ieee80211_sta *sta)
  1329. {
  1330. int ret = 0;
  1331. lockdep_assert_held(&ar->conf_mutex);
  1332. if (!sta->wme) {
  1333. arvif->num_legacy_stations--;
  1334. ret = ath10k_recalc_rtscts_prot(arvif);
  1335. if (ret) {
  1336. ath10k_warn(ar, "failed to recalculate rts/cts prot for vdev %d: %d\n",
  1337. arvif->vdev_id, ret);
  1338. return ret;
  1339. }
  1340. }
  1341. ret = ath10k_clear_peer_keys(arvif, sta->addr);
  1342. if (ret) {
  1343. ath10k_warn(ar, "failed to clear all peer wep keys for vdev %i: %d\n",
  1344. arvif->vdev_id, ret);
  1345. return ret;
  1346. }
  1347. return ret;
  1348. }
  1349. /**************/
  1350. /* Regulatory */
  1351. /**************/
  1352. static int ath10k_update_channel_list(struct ath10k *ar)
  1353. {
  1354. struct ieee80211_hw *hw = ar->hw;
  1355. struct ieee80211_supported_band **bands;
  1356. enum ieee80211_band band;
  1357. struct ieee80211_channel *channel;
  1358. struct wmi_scan_chan_list_arg arg = {0};
  1359. struct wmi_channel_arg *ch;
  1360. bool passive;
  1361. int len;
  1362. int ret;
  1363. int i;
  1364. lockdep_assert_held(&ar->conf_mutex);
  1365. bands = hw->wiphy->bands;
  1366. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  1367. if (!bands[band])
  1368. continue;
  1369. for (i = 0; i < bands[band]->n_channels; i++) {
  1370. if (bands[band]->channels[i].flags &
  1371. IEEE80211_CHAN_DISABLED)
  1372. continue;
  1373. arg.n_channels++;
  1374. }
  1375. }
  1376. len = sizeof(struct wmi_channel_arg) * arg.n_channels;
  1377. arg.channels = kzalloc(len, GFP_KERNEL);
  1378. if (!arg.channels)
  1379. return -ENOMEM;
  1380. ch = arg.channels;
  1381. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  1382. if (!bands[band])
  1383. continue;
  1384. for (i = 0; i < bands[band]->n_channels; i++) {
  1385. channel = &bands[band]->channels[i];
  1386. if (channel->flags & IEEE80211_CHAN_DISABLED)
  1387. continue;
  1388. ch->allow_ht = true;
  1389. /* FIXME: when should we really allow VHT? */
  1390. ch->allow_vht = true;
  1391. ch->allow_ibss =
  1392. !(channel->flags & IEEE80211_CHAN_NO_IR);
  1393. ch->ht40plus =
  1394. !(channel->flags & IEEE80211_CHAN_NO_HT40PLUS);
  1395. ch->chan_radar =
  1396. !!(channel->flags & IEEE80211_CHAN_RADAR);
  1397. passive = channel->flags & IEEE80211_CHAN_NO_IR;
  1398. ch->passive = passive;
  1399. ch->freq = channel->center_freq;
  1400. ch->min_power = 0;
  1401. ch->max_power = channel->max_power * 2;
  1402. ch->max_reg_power = channel->max_reg_power * 2;
  1403. ch->max_antenna_gain = channel->max_antenna_gain * 2;
  1404. ch->reg_class_id = 0; /* FIXME */
  1405. /* FIXME: why use only legacy modes, why not any
  1406. * HT/VHT modes? Would that even make any
  1407. * difference? */
  1408. if (channel->band == IEEE80211_BAND_2GHZ)
  1409. ch->mode = MODE_11G;
  1410. else
  1411. ch->mode = MODE_11A;
  1412. if (WARN_ON_ONCE(ch->mode == MODE_UNKNOWN))
  1413. continue;
  1414. ath10k_dbg(ar, ATH10K_DBG_WMI,
  1415. "mac channel [%zd/%d] freq %d maxpower %d regpower %d antenna %d mode %d\n",
  1416. ch - arg.channels, arg.n_channels,
  1417. ch->freq, ch->max_power, ch->max_reg_power,
  1418. ch->max_antenna_gain, ch->mode);
  1419. ch++;
  1420. }
  1421. }
  1422. ret = ath10k_wmi_scan_chan_list(ar, &arg);
  1423. kfree(arg.channels);
  1424. return ret;
  1425. }
  1426. static enum wmi_dfs_region
  1427. ath10k_mac_get_dfs_region(enum nl80211_dfs_regions dfs_region)
  1428. {
  1429. switch (dfs_region) {
  1430. case NL80211_DFS_UNSET:
  1431. return WMI_UNINIT_DFS_DOMAIN;
  1432. case NL80211_DFS_FCC:
  1433. return WMI_FCC_DFS_DOMAIN;
  1434. case NL80211_DFS_ETSI:
  1435. return WMI_ETSI_DFS_DOMAIN;
  1436. case NL80211_DFS_JP:
  1437. return WMI_MKK4_DFS_DOMAIN;
  1438. }
  1439. return WMI_UNINIT_DFS_DOMAIN;
  1440. }
  1441. static void ath10k_regd_update(struct ath10k *ar)
  1442. {
  1443. struct reg_dmn_pair_mapping *regpair;
  1444. int ret;
  1445. enum wmi_dfs_region wmi_dfs_reg;
  1446. enum nl80211_dfs_regions nl_dfs_reg;
  1447. lockdep_assert_held(&ar->conf_mutex);
  1448. ret = ath10k_update_channel_list(ar);
  1449. if (ret)
  1450. ath10k_warn(ar, "failed to update channel list: %d\n", ret);
  1451. regpair = ar->ath_common.regulatory.regpair;
  1452. if (config_enabled(CONFIG_ATH10K_DFS_CERTIFIED) && ar->dfs_detector) {
  1453. nl_dfs_reg = ar->dfs_detector->region;
  1454. wmi_dfs_reg = ath10k_mac_get_dfs_region(nl_dfs_reg);
  1455. } else {
  1456. wmi_dfs_reg = WMI_UNINIT_DFS_DOMAIN;
  1457. }
  1458. /* Target allows setting up per-band regdomain but ath_common provides
  1459. * a combined one only */
  1460. ret = ath10k_wmi_pdev_set_regdomain(ar,
  1461. regpair->reg_domain,
  1462. regpair->reg_domain, /* 2ghz */
  1463. regpair->reg_domain, /* 5ghz */
  1464. regpair->reg_2ghz_ctl,
  1465. regpair->reg_5ghz_ctl,
  1466. wmi_dfs_reg);
  1467. if (ret)
  1468. ath10k_warn(ar, "failed to set pdev regdomain: %d\n", ret);
  1469. }
  1470. static void ath10k_reg_notifier(struct wiphy *wiphy,
  1471. struct regulatory_request *request)
  1472. {
  1473. struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
  1474. struct ath10k *ar = hw->priv;
  1475. bool result;
  1476. ath_reg_notifier_apply(wiphy, request, &ar->ath_common.regulatory);
  1477. if (config_enabled(CONFIG_ATH10K_DFS_CERTIFIED) && ar->dfs_detector) {
  1478. ath10k_dbg(ar, ATH10K_DBG_REGULATORY, "dfs region 0x%x\n",
  1479. request->dfs_region);
  1480. result = ar->dfs_detector->set_dfs_domain(ar->dfs_detector,
  1481. request->dfs_region);
  1482. if (!result)
  1483. ath10k_warn(ar, "DFS region 0x%X not supported, will trigger radar for every pulse\n",
  1484. request->dfs_region);
  1485. }
  1486. mutex_lock(&ar->conf_mutex);
  1487. if (ar->state == ATH10K_STATE_ON)
  1488. ath10k_regd_update(ar);
  1489. mutex_unlock(&ar->conf_mutex);
  1490. }
  1491. /***************/
  1492. /* TX handlers */
  1493. /***************/
  1494. static u8 ath10k_tx_h_get_tid(struct ieee80211_hdr *hdr)
  1495. {
  1496. if (ieee80211_is_mgmt(hdr->frame_control))
  1497. return HTT_DATA_TX_EXT_TID_MGMT;
  1498. if (!ieee80211_is_data_qos(hdr->frame_control))
  1499. return HTT_DATA_TX_EXT_TID_NON_QOS_MCAST_BCAST;
  1500. if (!is_unicast_ether_addr(ieee80211_get_DA(hdr)))
  1501. return HTT_DATA_TX_EXT_TID_NON_QOS_MCAST_BCAST;
  1502. return ieee80211_get_qos_ctl(hdr)[0] & IEEE80211_QOS_CTL_TID_MASK;
  1503. }
  1504. static u8 ath10k_tx_h_get_vdev_id(struct ath10k *ar, struct ieee80211_vif *vif)
  1505. {
  1506. if (vif)
  1507. return ath10k_vif_to_arvif(vif)->vdev_id;
  1508. if (ar->monitor_started)
  1509. return ar->monitor_vdev_id;
  1510. ath10k_warn(ar, "failed to resolve vdev id\n");
  1511. return 0;
  1512. }
  1513. /* HTT Tx uses Native Wifi tx mode which expects 802.11 frames without QoS
  1514. * Control in the header.
  1515. */
  1516. static void ath10k_tx_h_nwifi(struct ieee80211_hw *hw, struct sk_buff *skb)
  1517. {
  1518. struct ieee80211_hdr *hdr = (void *)skb->data;
  1519. struct ath10k_skb_cb *cb = ATH10K_SKB_CB(skb);
  1520. u8 *qos_ctl;
  1521. if (!ieee80211_is_data_qos(hdr->frame_control))
  1522. return;
  1523. qos_ctl = ieee80211_get_qos_ctl(hdr);
  1524. memmove(skb->data + IEEE80211_QOS_CTL_LEN,
  1525. skb->data, (void *)qos_ctl - (void *)skb->data);
  1526. skb_pull(skb, IEEE80211_QOS_CTL_LEN);
  1527. /* Fw/Hw generates a corrupted QoS Control Field for QoS NullFunc
  1528. * frames. Powersave is handled by the fw/hw so QoS NyllFunc frames are
  1529. * used only for CQM purposes (e.g. hostapd station keepalive ping) so
  1530. * it is safe to downgrade to NullFunc.
  1531. */
  1532. if (ieee80211_is_qos_nullfunc(hdr->frame_control)) {
  1533. hdr->frame_control &= ~__cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
  1534. cb->htt.tid = HTT_DATA_TX_EXT_TID_NON_QOS_MCAST_BCAST;
  1535. }
  1536. }
  1537. static void ath10k_tx_wep_key_work(struct work_struct *work)
  1538. {
  1539. struct ath10k_vif *arvif = container_of(work, struct ath10k_vif,
  1540. wep_key_work);
  1541. struct ath10k *ar = arvif->ar;
  1542. int ret, keyidx = arvif->def_wep_key_newidx;
  1543. mutex_lock(&arvif->ar->conf_mutex);
  1544. if (arvif->ar->state != ATH10K_STATE_ON)
  1545. goto unlock;
  1546. if (arvif->def_wep_key_idx == keyidx)
  1547. goto unlock;
  1548. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d set keyidx %d\n",
  1549. arvif->vdev_id, keyidx);
  1550. ret = ath10k_wmi_vdev_set_param(arvif->ar,
  1551. arvif->vdev_id,
  1552. arvif->ar->wmi.vdev_param->def_keyid,
  1553. keyidx);
  1554. if (ret) {
  1555. ath10k_warn(ar, "failed to update wep key index for vdev %d: %d\n",
  1556. arvif->vdev_id,
  1557. ret);
  1558. goto unlock;
  1559. }
  1560. arvif->def_wep_key_idx = keyidx;
  1561. unlock:
  1562. mutex_unlock(&arvif->ar->conf_mutex);
  1563. }
  1564. static void ath10k_tx_h_update_wep_key(struct ieee80211_vif *vif,
  1565. struct ieee80211_key_conf *key,
  1566. struct sk_buff *skb)
  1567. {
  1568. struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
  1569. struct ath10k *ar = arvif->ar;
  1570. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  1571. if (!ieee80211_has_protected(hdr->frame_control))
  1572. return;
  1573. if (!key)
  1574. return;
  1575. if (key->cipher != WLAN_CIPHER_SUITE_WEP40 &&
  1576. key->cipher != WLAN_CIPHER_SUITE_WEP104)
  1577. return;
  1578. if (key->keyidx == arvif->def_wep_key_idx)
  1579. return;
  1580. /* FIXME: Most likely a few frames will be TXed with an old key. Simply
  1581. * queueing frames until key index is updated is not an option because
  1582. * sk_buff may need more processing to be done, e.g. offchannel */
  1583. arvif->def_wep_key_newidx = key->keyidx;
  1584. ieee80211_queue_work(ar->hw, &arvif->wep_key_work);
  1585. }
  1586. static void ath10k_tx_h_add_p2p_noa_ie(struct ath10k *ar,
  1587. struct ieee80211_vif *vif,
  1588. struct sk_buff *skb)
  1589. {
  1590. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  1591. struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
  1592. /* This is case only for P2P_GO */
  1593. if (arvif->vdev_type != WMI_VDEV_TYPE_AP ||
  1594. arvif->vdev_subtype != WMI_VDEV_SUBTYPE_P2P_GO)
  1595. return;
  1596. if (unlikely(ieee80211_is_probe_resp(hdr->frame_control))) {
  1597. spin_lock_bh(&ar->data_lock);
  1598. if (arvif->u.ap.noa_data)
  1599. if (!pskb_expand_head(skb, 0, arvif->u.ap.noa_len,
  1600. GFP_ATOMIC))
  1601. memcpy(skb_put(skb, arvif->u.ap.noa_len),
  1602. arvif->u.ap.noa_data,
  1603. arvif->u.ap.noa_len);
  1604. spin_unlock_bh(&ar->data_lock);
  1605. }
  1606. }
  1607. static void ath10k_tx_htt(struct ath10k *ar, struct sk_buff *skb)
  1608. {
  1609. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  1610. int ret = 0;
  1611. if (ar->htt.target_version_major >= 3) {
  1612. /* Since HTT 3.0 there is no separate mgmt tx command */
  1613. ret = ath10k_htt_tx(&ar->htt, skb);
  1614. goto exit;
  1615. }
  1616. if (ieee80211_is_mgmt(hdr->frame_control)) {
  1617. if (test_bit(ATH10K_FW_FEATURE_HAS_WMI_MGMT_TX,
  1618. ar->fw_features)) {
  1619. if (skb_queue_len(&ar->wmi_mgmt_tx_queue) >=
  1620. ATH10K_MAX_NUM_MGMT_PENDING) {
  1621. ath10k_warn(ar, "reached WMI management transmit queue limit\n");
  1622. ret = -EBUSY;
  1623. goto exit;
  1624. }
  1625. skb_queue_tail(&ar->wmi_mgmt_tx_queue, skb);
  1626. ieee80211_queue_work(ar->hw, &ar->wmi_mgmt_tx_work);
  1627. } else {
  1628. ret = ath10k_htt_mgmt_tx(&ar->htt, skb);
  1629. }
  1630. } else if (!test_bit(ATH10K_FW_FEATURE_HAS_WMI_MGMT_TX,
  1631. ar->fw_features) &&
  1632. ieee80211_is_nullfunc(hdr->frame_control)) {
  1633. /* FW does not report tx status properly for NullFunc frames
  1634. * unless they are sent through mgmt tx path. mac80211 sends
  1635. * those frames when it detects link/beacon loss and depends
  1636. * on the tx status to be correct. */
  1637. ret = ath10k_htt_mgmt_tx(&ar->htt, skb);
  1638. } else {
  1639. ret = ath10k_htt_tx(&ar->htt, skb);
  1640. }
  1641. exit:
  1642. if (ret) {
  1643. ath10k_warn(ar, "failed to transmit packet, dropping: %d\n",
  1644. ret);
  1645. ieee80211_free_txskb(ar->hw, skb);
  1646. }
  1647. }
  1648. void ath10k_offchan_tx_purge(struct ath10k *ar)
  1649. {
  1650. struct sk_buff *skb;
  1651. for (;;) {
  1652. skb = skb_dequeue(&ar->offchan_tx_queue);
  1653. if (!skb)
  1654. break;
  1655. ieee80211_free_txskb(ar->hw, skb);
  1656. }
  1657. }
  1658. void ath10k_offchan_tx_work(struct work_struct *work)
  1659. {
  1660. struct ath10k *ar = container_of(work, struct ath10k, offchan_tx_work);
  1661. struct ath10k_peer *peer;
  1662. struct ieee80211_hdr *hdr;
  1663. struct sk_buff *skb;
  1664. const u8 *peer_addr;
  1665. int vdev_id;
  1666. int ret;
  1667. /* FW requirement: We must create a peer before FW will send out
  1668. * an offchannel frame. Otherwise the frame will be stuck and
  1669. * never transmitted. We delete the peer upon tx completion.
  1670. * It is unlikely that a peer for offchannel tx will already be
  1671. * present. However it may be in some rare cases so account for that.
  1672. * Otherwise we might remove a legitimate peer and break stuff. */
  1673. for (;;) {
  1674. skb = skb_dequeue(&ar->offchan_tx_queue);
  1675. if (!skb)
  1676. break;
  1677. mutex_lock(&ar->conf_mutex);
  1678. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac offchannel skb %p\n",
  1679. skb);
  1680. hdr = (struct ieee80211_hdr *)skb->data;
  1681. peer_addr = ieee80211_get_DA(hdr);
  1682. vdev_id = ATH10K_SKB_CB(skb)->vdev_id;
  1683. spin_lock_bh(&ar->data_lock);
  1684. peer = ath10k_peer_find(ar, vdev_id, peer_addr);
  1685. spin_unlock_bh(&ar->data_lock);
  1686. if (peer)
  1687. /* FIXME: should this use ath10k_warn()? */
  1688. ath10k_dbg(ar, ATH10K_DBG_MAC, "peer %pM on vdev %d already present\n",
  1689. peer_addr, vdev_id);
  1690. if (!peer) {
  1691. ret = ath10k_peer_create(ar, vdev_id, peer_addr);
  1692. if (ret)
  1693. ath10k_warn(ar, "failed to create peer %pM on vdev %d: %d\n",
  1694. peer_addr, vdev_id, ret);
  1695. }
  1696. spin_lock_bh(&ar->data_lock);
  1697. reinit_completion(&ar->offchan_tx_completed);
  1698. ar->offchan_tx_skb = skb;
  1699. spin_unlock_bh(&ar->data_lock);
  1700. ath10k_tx_htt(ar, skb);
  1701. ret = wait_for_completion_timeout(&ar->offchan_tx_completed,
  1702. 3 * HZ);
  1703. if (ret <= 0)
  1704. ath10k_warn(ar, "timed out waiting for offchannel skb %p\n",
  1705. skb);
  1706. if (!peer) {
  1707. ret = ath10k_peer_delete(ar, vdev_id, peer_addr);
  1708. if (ret)
  1709. ath10k_warn(ar, "failed to delete peer %pM on vdev %d: %d\n",
  1710. peer_addr, vdev_id, ret);
  1711. }
  1712. mutex_unlock(&ar->conf_mutex);
  1713. }
  1714. }
  1715. void ath10k_mgmt_over_wmi_tx_purge(struct ath10k *ar)
  1716. {
  1717. struct sk_buff *skb;
  1718. for (;;) {
  1719. skb = skb_dequeue(&ar->wmi_mgmt_tx_queue);
  1720. if (!skb)
  1721. break;
  1722. ieee80211_free_txskb(ar->hw, skb);
  1723. }
  1724. }
  1725. void ath10k_mgmt_over_wmi_tx_work(struct work_struct *work)
  1726. {
  1727. struct ath10k *ar = container_of(work, struct ath10k, wmi_mgmt_tx_work);
  1728. struct sk_buff *skb;
  1729. int ret;
  1730. for (;;) {
  1731. skb = skb_dequeue(&ar->wmi_mgmt_tx_queue);
  1732. if (!skb)
  1733. break;
  1734. ret = ath10k_wmi_mgmt_tx(ar, skb);
  1735. if (ret) {
  1736. ath10k_warn(ar, "failed to transmit management frame via WMI: %d\n",
  1737. ret);
  1738. ieee80211_free_txskb(ar->hw, skb);
  1739. }
  1740. }
  1741. }
  1742. /************/
  1743. /* Scanning */
  1744. /************/
  1745. void __ath10k_scan_finish(struct ath10k *ar)
  1746. {
  1747. lockdep_assert_held(&ar->data_lock);
  1748. switch (ar->scan.state) {
  1749. case ATH10K_SCAN_IDLE:
  1750. break;
  1751. case ATH10K_SCAN_RUNNING:
  1752. case ATH10K_SCAN_ABORTING:
  1753. if (ar->scan.is_roc)
  1754. ieee80211_remain_on_channel_expired(ar->hw);
  1755. else
  1756. ieee80211_scan_completed(ar->hw,
  1757. (ar->scan.state ==
  1758. ATH10K_SCAN_ABORTING));
  1759. /* fall through */
  1760. case ATH10K_SCAN_STARTING:
  1761. ar->scan.state = ATH10K_SCAN_IDLE;
  1762. ar->scan_channel = NULL;
  1763. ath10k_offchan_tx_purge(ar);
  1764. cancel_delayed_work(&ar->scan.timeout);
  1765. complete_all(&ar->scan.completed);
  1766. break;
  1767. }
  1768. }
  1769. void ath10k_scan_finish(struct ath10k *ar)
  1770. {
  1771. spin_lock_bh(&ar->data_lock);
  1772. __ath10k_scan_finish(ar);
  1773. spin_unlock_bh(&ar->data_lock);
  1774. }
  1775. static int ath10k_scan_stop(struct ath10k *ar)
  1776. {
  1777. struct wmi_stop_scan_arg arg = {
  1778. .req_id = 1, /* FIXME */
  1779. .req_type = WMI_SCAN_STOP_ONE,
  1780. .u.scan_id = ATH10K_SCAN_ID,
  1781. };
  1782. int ret;
  1783. lockdep_assert_held(&ar->conf_mutex);
  1784. ret = ath10k_wmi_stop_scan(ar, &arg);
  1785. if (ret) {
  1786. ath10k_warn(ar, "failed to stop wmi scan: %d\n", ret);
  1787. goto out;
  1788. }
  1789. ret = wait_for_completion_timeout(&ar->scan.completed, 3*HZ);
  1790. if (ret == 0) {
  1791. ath10k_warn(ar, "failed to receive scan abortion completion: timed out\n");
  1792. ret = -ETIMEDOUT;
  1793. } else if (ret > 0) {
  1794. ret = 0;
  1795. }
  1796. out:
  1797. /* Scan state should be updated upon scan completion but in case
  1798. * firmware fails to deliver the event (for whatever reason) it is
  1799. * desired to clean up scan state anyway. Firmware may have just
  1800. * dropped the scan completion event delivery due to transport pipe
  1801. * being overflown with data and/or it can recover on its own before
  1802. * next scan request is submitted.
  1803. */
  1804. spin_lock_bh(&ar->data_lock);
  1805. if (ar->scan.state != ATH10K_SCAN_IDLE)
  1806. __ath10k_scan_finish(ar);
  1807. spin_unlock_bh(&ar->data_lock);
  1808. return ret;
  1809. }
  1810. static void ath10k_scan_abort(struct ath10k *ar)
  1811. {
  1812. int ret;
  1813. lockdep_assert_held(&ar->conf_mutex);
  1814. spin_lock_bh(&ar->data_lock);
  1815. switch (ar->scan.state) {
  1816. case ATH10K_SCAN_IDLE:
  1817. /* This can happen if timeout worker kicked in and called
  1818. * abortion while scan completion was being processed.
  1819. */
  1820. break;
  1821. case ATH10K_SCAN_STARTING:
  1822. case ATH10K_SCAN_ABORTING:
  1823. ath10k_warn(ar, "refusing scan abortion due to invalid scan state: %s (%d)\n",
  1824. ath10k_scan_state_str(ar->scan.state),
  1825. ar->scan.state);
  1826. break;
  1827. case ATH10K_SCAN_RUNNING:
  1828. ar->scan.state = ATH10K_SCAN_ABORTING;
  1829. spin_unlock_bh(&ar->data_lock);
  1830. ret = ath10k_scan_stop(ar);
  1831. if (ret)
  1832. ath10k_warn(ar, "failed to abort scan: %d\n", ret);
  1833. spin_lock_bh(&ar->data_lock);
  1834. break;
  1835. }
  1836. spin_unlock_bh(&ar->data_lock);
  1837. }
  1838. void ath10k_scan_timeout_work(struct work_struct *work)
  1839. {
  1840. struct ath10k *ar = container_of(work, struct ath10k,
  1841. scan.timeout.work);
  1842. mutex_lock(&ar->conf_mutex);
  1843. ath10k_scan_abort(ar);
  1844. mutex_unlock(&ar->conf_mutex);
  1845. }
  1846. static int ath10k_start_scan(struct ath10k *ar,
  1847. const struct wmi_start_scan_arg *arg)
  1848. {
  1849. int ret;
  1850. lockdep_assert_held(&ar->conf_mutex);
  1851. ret = ath10k_wmi_start_scan(ar, arg);
  1852. if (ret)
  1853. return ret;
  1854. ret = wait_for_completion_timeout(&ar->scan.started, 1*HZ);
  1855. if (ret == 0) {
  1856. ret = ath10k_scan_stop(ar);
  1857. if (ret)
  1858. ath10k_warn(ar, "failed to stop scan: %d\n", ret);
  1859. return -ETIMEDOUT;
  1860. }
  1861. /* Add a 200ms margin to account for event/command processing */
  1862. ieee80211_queue_delayed_work(ar->hw, &ar->scan.timeout,
  1863. msecs_to_jiffies(arg->max_scan_time+200));
  1864. return 0;
  1865. }
  1866. /**********************/
  1867. /* mac80211 callbacks */
  1868. /**********************/
  1869. static void ath10k_tx(struct ieee80211_hw *hw,
  1870. struct ieee80211_tx_control *control,
  1871. struct sk_buff *skb)
  1872. {
  1873. struct ath10k *ar = hw->priv;
  1874. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1875. struct ieee80211_vif *vif = info->control.vif;
  1876. struct ieee80211_key_conf *key = info->control.hw_key;
  1877. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  1878. /* We should disable CCK RATE due to P2P */
  1879. if (info->flags & IEEE80211_TX_CTL_NO_CCK_RATE)
  1880. ath10k_dbg(ar, ATH10K_DBG_MAC, "IEEE80211_TX_CTL_NO_CCK_RATE\n");
  1881. ATH10K_SKB_CB(skb)->htt.is_offchan = false;
  1882. ATH10K_SKB_CB(skb)->htt.tid = ath10k_tx_h_get_tid(hdr);
  1883. ATH10K_SKB_CB(skb)->vdev_id = ath10k_tx_h_get_vdev_id(ar, vif);
  1884. /* it makes no sense to process injected frames like that */
  1885. if (vif && vif->type != NL80211_IFTYPE_MONITOR) {
  1886. ath10k_tx_h_nwifi(hw, skb);
  1887. ath10k_tx_h_update_wep_key(vif, key, skb);
  1888. ath10k_tx_h_add_p2p_noa_ie(ar, vif, skb);
  1889. ath10k_tx_h_seq_no(vif, skb);
  1890. }
  1891. if (info->flags & IEEE80211_TX_CTL_TX_OFFCHAN) {
  1892. spin_lock_bh(&ar->data_lock);
  1893. ATH10K_SKB_CB(skb)->htt.is_offchan = true;
  1894. ATH10K_SKB_CB(skb)->vdev_id = ar->scan.vdev_id;
  1895. spin_unlock_bh(&ar->data_lock);
  1896. ath10k_dbg(ar, ATH10K_DBG_MAC, "queued offchannel skb %p\n",
  1897. skb);
  1898. skb_queue_tail(&ar->offchan_tx_queue, skb);
  1899. ieee80211_queue_work(hw, &ar->offchan_tx_work);
  1900. return;
  1901. }
  1902. ath10k_tx_htt(ar, skb);
  1903. }
  1904. /* Must not be called with conf_mutex held as workers can use that also. */
  1905. static void ath10k_drain_tx(struct ath10k *ar)
  1906. {
  1907. /* make sure rcu-protected mac80211 tx path itself is drained */
  1908. synchronize_net();
  1909. ath10k_offchan_tx_purge(ar);
  1910. ath10k_mgmt_over_wmi_tx_purge(ar);
  1911. cancel_work_sync(&ar->offchan_tx_work);
  1912. cancel_work_sync(&ar->wmi_mgmt_tx_work);
  1913. }
  1914. void ath10k_halt(struct ath10k *ar)
  1915. {
  1916. struct ath10k_vif *arvif;
  1917. lockdep_assert_held(&ar->conf_mutex);
  1918. clear_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
  1919. ar->filter_flags = 0;
  1920. ar->monitor = false;
  1921. if (ar->monitor_started)
  1922. ath10k_monitor_stop(ar);
  1923. ar->monitor_started = false;
  1924. ath10k_scan_finish(ar);
  1925. ath10k_peer_cleanup_all(ar);
  1926. ath10k_core_stop(ar);
  1927. ath10k_hif_power_down(ar);
  1928. spin_lock_bh(&ar->data_lock);
  1929. list_for_each_entry(arvif, &ar->arvifs, list) {
  1930. if (!arvif->beacon)
  1931. continue;
  1932. dma_unmap_single(arvif->ar->dev,
  1933. ATH10K_SKB_CB(arvif->beacon)->paddr,
  1934. arvif->beacon->len, DMA_TO_DEVICE);
  1935. dev_kfree_skb_any(arvif->beacon);
  1936. arvif->beacon = NULL;
  1937. }
  1938. spin_unlock_bh(&ar->data_lock);
  1939. }
  1940. static int ath10k_get_antenna(struct ieee80211_hw *hw, u32 *tx_ant, u32 *rx_ant)
  1941. {
  1942. struct ath10k *ar = hw->priv;
  1943. mutex_lock(&ar->conf_mutex);
  1944. if (ar->cfg_tx_chainmask) {
  1945. *tx_ant = ar->cfg_tx_chainmask;
  1946. *rx_ant = ar->cfg_rx_chainmask;
  1947. } else {
  1948. *tx_ant = ar->supp_tx_chainmask;
  1949. *rx_ant = ar->supp_rx_chainmask;
  1950. }
  1951. mutex_unlock(&ar->conf_mutex);
  1952. return 0;
  1953. }
  1954. static int __ath10k_set_antenna(struct ath10k *ar, u32 tx_ant, u32 rx_ant)
  1955. {
  1956. int ret;
  1957. lockdep_assert_held(&ar->conf_mutex);
  1958. ar->cfg_tx_chainmask = tx_ant;
  1959. ar->cfg_rx_chainmask = rx_ant;
  1960. if ((ar->state != ATH10K_STATE_ON) &&
  1961. (ar->state != ATH10K_STATE_RESTARTED))
  1962. return 0;
  1963. ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->tx_chain_mask,
  1964. tx_ant);
  1965. if (ret) {
  1966. ath10k_warn(ar, "failed to set tx-chainmask: %d, req 0x%x\n",
  1967. ret, tx_ant);
  1968. return ret;
  1969. }
  1970. ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->rx_chain_mask,
  1971. rx_ant);
  1972. if (ret) {
  1973. ath10k_warn(ar, "failed to set rx-chainmask: %d, req 0x%x\n",
  1974. ret, rx_ant);
  1975. return ret;
  1976. }
  1977. return 0;
  1978. }
  1979. static int ath10k_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
  1980. {
  1981. struct ath10k *ar = hw->priv;
  1982. int ret;
  1983. mutex_lock(&ar->conf_mutex);
  1984. ret = __ath10k_set_antenna(ar, tx_ant, rx_ant);
  1985. mutex_unlock(&ar->conf_mutex);
  1986. return ret;
  1987. }
  1988. static int ath10k_start(struct ieee80211_hw *hw)
  1989. {
  1990. struct ath10k *ar = hw->priv;
  1991. int ret = 0;
  1992. /*
  1993. * This makes sense only when restarting hw. It is harmless to call
  1994. * uncoditionally. This is necessary to make sure no HTT/WMI tx
  1995. * commands will be submitted while restarting.
  1996. */
  1997. ath10k_drain_tx(ar);
  1998. mutex_lock(&ar->conf_mutex);
  1999. switch (ar->state) {
  2000. case ATH10K_STATE_OFF:
  2001. ar->state = ATH10K_STATE_ON;
  2002. break;
  2003. case ATH10K_STATE_RESTARTING:
  2004. ath10k_halt(ar);
  2005. ar->state = ATH10K_STATE_RESTARTED;
  2006. break;
  2007. case ATH10K_STATE_ON:
  2008. case ATH10K_STATE_RESTARTED:
  2009. case ATH10K_STATE_WEDGED:
  2010. WARN_ON(1);
  2011. ret = -EINVAL;
  2012. goto err;
  2013. case ATH10K_STATE_UTF:
  2014. ret = -EBUSY;
  2015. goto err;
  2016. }
  2017. ret = ath10k_hif_power_up(ar);
  2018. if (ret) {
  2019. ath10k_err(ar, "Could not init hif: %d\n", ret);
  2020. goto err_off;
  2021. }
  2022. ret = ath10k_core_start(ar, ATH10K_FIRMWARE_MODE_NORMAL);
  2023. if (ret) {
  2024. ath10k_err(ar, "Could not init core: %d\n", ret);
  2025. goto err_power_down;
  2026. }
  2027. ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->pmf_qos, 1);
  2028. if (ret) {
  2029. ath10k_warn(ar, "failed to enable PMF QOS: %d\n", ret);
  2030. goto err_core_stop;
  2031. }
  2032. ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->dynamic_bw, 1);
  2033. if (ret) {
  2034. ath10k_warn(ar, "failed to enable dynamic BW: %d\n", ret);
  2035. goto err_core_stop;
  2036. }
  2037. if (ar->cfg_tx_chainmask)
  2038. __ath10k_set_antenna(ar, ar->cfg_tx_chainmask,
  2039. ar->cfg_rx_chainmask);
  2040. /*
  2041. * By default FW set ARP frames ac to voice (6). In that case ARP
  2042. * exchange is not working properly for UAPSD enabled AP. ARP requests
  2043. * which arrives with access category 0 are processed by network stack
  2044. * and send back with access category 0, but FW changes access category
  2045. * to 6. Set ARP frames access category to best effort (0) solves
  2046. * this problem.
  2047. */
  2048. ret = ath10k_wmi_pdev_set_param(ar,
  2049. ar->wmi.pdev_param->arp_ac_override, 0);
  2050. if (ret) {
  2051. ath10k_warn(ar, "failed to set arp ac override parameter: %d\n",
  2052. ret);
  2053. goto err_core_stop;
  2054. }
  2055. ar->num_started_vdevs = 0;
  2056. ath10k_regd_update(ar);
  2057. ath10k_spectral_start(ar);
  2058. mutex_unlock(&ar->conf_mutex);
  2059. return 0;
  2060. err_core_stop:
  2061. ath10k_core_stop(ar);
  2062. err_power_down:
  2063. ath10k_hif_power_down(ar);
  2064. err_off:
  2065. ar->state = ATH10K_STATE_OFF;
  2066. err:
  2067. mutex_unlock(&ar->conf_mutex);
  2068. return ret;
  2069. }
  2070. static void ath10k_stop(struct ieee80211_hw *hw)
  2071. {
  2072. struct ath10k *ar = hw->priv;
  2073. ath10k_drain_tx(ar);
  2074. mutex_lock(&ar->conf_mutex);
  2075. if (ar->state != ATH10K_STATE_OFF) {
  2076. ath10k_halt(ar);
  2077. ar->state = ATH10K_STATE_OFF;
  2078. }
  2079. mutex_unlock(&ar->conf_mutex);
  2080. cancel_delayed_work_sync(&ar->scan.timeout);
  2081. cancel_work_sync(&ar->restart_work);
  2082. }
  2083. static int ath10k_config_ps(struct ath10k *ar)
  2084. {
  2085. struct ath10k_vif *arvif;
  2086. int ret = 0;
  2087. lockdep_assert_held(&ar->conf_mutex);
  2088. list_for_each_entry(arvif, &ar->arvifs, list) {
  2089. ret = ath10k_mac_vif_setup_ps(arvif);
  2090. if (ret) {
  2091. ath10k_warn(ar, "failed to setup powersave: %d\n", ret);
  2092. break;
  2093. }
  2094. }
  2095. return ret;
  2096. }
  2097. static const char *chandef_get_width(enum nl80211_chan_width width)
  2098. {
  2099. switch (width) {
  2100. case NL80211_CHAN_WIDTH_20_NOHT:
  2101. return "20 (noht)";
  2102. case NL80211_CHAN_WIDTH_20:
  2103. return "20";
  2104. case NL80211_CHAN_WIDTH_40:
  2105. return "40";
  2106. case NL80211_CHAN_WIDTH_80:
  2107. return "80";
  2108. case NL80211_CHAN_WIDTH_80P80:
  2109. return "80+80";
  2110. case NL80211_CHAN_WIDTH_160:
  2111. return "160";
  2112. case NL80211_CHAN_WIDTH_5:
  2113. return "5";
  2114. case NL80211_CHAN_WIDTH_10:
  2115. return "10";
  2116. }
  2117. return "?";
  2118. }
  2119. static void ath10k_config_chan(struct ath10k *ar)
  2120. {
  2121. struct ath10k_vif *arvif;
  2122. int ret;
  2123. lockdep_assert_held(&ar->conf_mutex);
  2124. ath10k_dbg(ar, ATH10K_DBG_MAC,
  2125. "mac config channel to %dMHz (cf1 %dMHz cf2 %dMHz width %s)\n",
  2126. ar->chandef.chan->center_freq,
  2127. ar->chandef.center_freq1,
  2128. ar->chandef.center_freq2,
  2129. chandef_get_width(ar->chandef.width));
  2130. /* First stop monitor interface. Some FW versions crash if there's a
  2131. * lone monitor interface. */
  2132. if (ar->monitor_started)
  2133. ath10k_monitor_stop(ar);
  2134. list_for_each_entry(arvif, &ar->arvifs, list) {
  2135. if (!arvif->is_started)
  2136. continue;
  2137. if (!arvif->is_up)
  2138. continue;
  2139. if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR)
  2140. continue;
  2141. ret = ath10k_wmi_vdev_down(ar, arvif->vdev_id);
  2142. if (ret) {
  2143. ath10k_warn(ar, "failed to down vdev %d: %d\n",
  2144. arvif->vdev_id, ret);
  2145. continue;
  2146. }
  2147. }
  2148. /* all vdevs are downed now - attempt to restart and re-up them */
  2149. list_for_each_entry(arvif, &ar->arvifs, list) {
  2150. if (!arvif->is_started)
  2151. continue;
  2152. if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR)
  2153. continue;
  2154. ret = ath10k_vdev_restart(arvif);
  2155. if (ret) {
  2156. ath10k_warn(ar, "failed to restart vdev %d: %d\n",
  2157. arvif->vdev_id, ret);
  2158. continue;
  2159. }
  2160. if (!arvif->is_up)
  2161. continue;
  2162. ret = ath10k_wmi_vdev_up(arvif->ar, arvif->vdev_id, arvif->aid,
  2163. arvif->bssid);
  2164. if (ret) {
  2165. ath10k_warn(ar, "failed to bring vdev up %d: %d\n",
  2166. arvif->vdev_id, ret);
  2167. continue;
  2168. }
  2169. }
  2170. ath10k_monitor_recalc(ar);
  2171. }
  2172. static int ath10k_config(struct ieee80211_hw *hw, u32 changed)
  2173. {
  2174. struct ath10k *ar = hw->priv;
  2175. struct ieee80211_conf *conf = &hw->conf;
  2176. int ret = 0;
  2177. u32 param;
  2178. mutex_lock(&ar->conf_mutex);
  2179. if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
  2180. ath10k_dbg(ar, ATH10K_DBG_MAC,
  2181. "mac config channel %dMHz flags 0x%x radar %d\n",
  2182. conf->chandef.chan->center_freq,
  2183. conf->chandef.chan->flags,
  2184. conf->radar_enabled);
  2185. spin_lock_bh(&ar->data_lock);
  2186. ar->rx_channel = conf->chandef.chan;
  2187. spin_unlock_bh(&ar->data_lock);
  2188. ar->radar_enabled = conf->radar_enabled;
  2189. ath10k_recalc_radar_detection(ar);
  2190. if (!cfg80211_chandef_identical(&ar->chandef, &conf->chandef)) {
  2191. ar->chandef = conf->chandef;
  2192. ath10k_config_chan(ar);
  2193. }
  2194. }
  2195. if (changed & IEEE80211_CONF_CHANGE_POWER) {
  2196. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac config power %d\n",
  2197. hw->conf.power_level);
  2198. param = ar->wmi.pdev_param->txpower_limit2g;
  2199. ret = ath10k_wmi_pdev_set_param(ar, param,
  2200. hw->conf.power_level * 2);
  2201. if (ret)
  2202. ath10k_warn(ar, "failed to set 2g txpower %d: %d\n",
  2203. hw->conf.power_level, ret);
  2204. param = ar->wmi.pdev_param->txpower_limit5g;
  2205. ret = ath10k_wmi_pdev_set_param(ar, param,
  2206. hw->conf.power_level * 2);
  2207. if (ret)
  2208. ath10k_warn(ar, "failed to set 5g txpower %d: %d\n",
  2209. hw->conf.power_level, ret);
  2210. }
  2211. if (changed & IEEE80211_CONF_CHANGE_PS)
  2212. ath10k_config_ps(ar);
  2213. if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
  2214. ar->monitor = conf->flags & IEEE80211_CONF_MONITOR;
  2215. ret = ath10k_monitor_recalc(ar);
  2216. if (ret)
  2217. ath10k_warn(ar, "failed to recalc monitor: %d\n", ret);
  2218. }
  2219. mutex_unlock(&ar->conf_mutex);
  2220. return ret;
  2221. }
  2222. /*
  2223. * TODO:
  2224. * Figure out how to handle WMI_VDEV_SUBTYPE_P2P_DEVICE,
  2225. * because we will send mgmt frames without CCK. This requirement
  2226. * for P2P_FIND/GO_NEG should be handled by checking CCK flag
  2227. * in the TX packet.
  2228. */
  2229. static int ath10k_add_interface(struct ieee80211_hw *hw,
  2230. struct ieee80211_vif *vif)
  2231. {
  2232. struct ath10k *ar = hw->priv;
  2233. struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
  2234. enum wmi_sta_powersave_param param;
  2235. int ret = 0;
  2236. u32 value;
  2237. int bit;
  2238. u32 vdev_param;
  2239. mutex_lock(&ar->conf_mutex);
  2240. memset(arvif, 0, sizeof(*arvif));
  2241. arvif->ar = ar;
  2242. arvif->vif = vif;
  2243. INIT_WORK(&arvif->wep_key_work, ath10k_tx_wep_key_work);
  2244. INIT_LIST_HEAD(&arvif->list);
  2245. if (ar->free_vdev_map == 0) {
  2246. ath10k_warn(ar, "Free vdev map is empty, no more interfaces allowed.\n");
  2247. ret = -EBUSY;
  2248. goto err;
  2249. }
  2250. bit = ffs(ar->free_vdev_map);
  2251. arvif->vdev_id = bit - 1;
  2252. arvif->vdev_subtype = WMI_VDEV_SUBTYPE_NONE;
  2253. if (ar->p2p)
  2254. arvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_DEVICE;
  2255. switch (vif->type) {
  2256. case NL80211_IFTYPE_UNSPECIFIED:
  2257. case NL80211_IFTYPE_STATION:
  2258. arvif->vdev_type = WMI_VDEV_TYPE_STA;
  2259. if (vif->p2p)
  2260. arvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_CLIENT;
  2261. break;
  2262. case NL80211_IFTYPE_ADHOC:
  2263. arvif->vdev_type = WMI_VDEV_TYPE_IBSS;
  2264. break;
  2265. case NL80211_IFTYPE_AP:
  2266. arvif->vdev_type = WMI_VDEV_TYPE_AP;
  2267. if (vif->p2p)
  2268. arvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_GO;
  2269. break;
  2270. case NL80211_IFTYPE_MONITOR:
  2271. arvif->vdev_type = WMI_VDEV_TYPE_MONITOR;
  2272. break;
  2273. default:
  2274. WARN_ON(1);
  2275. break;
  2276. }
  2277. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev create %d (add interface) type %d subtype %d\n",
  2278. arvif->vdev_id, arvif->vdev_type, arvif->vdev_subtype);
  2279. ret = ath10k_wmi_vdev_create(ar, arvif->vdev_id, arvif->vdev_type,
  2280. arvif->vdev_subtype, vif->addr);
  2281. if (ret) {
  2282. ath10k_warn(ar, "failed to create WMI vdev %i: %d\n",
  2283. arvif->vdev_id, ret);
  2284. goto err;
  2285. }
  2286. ar->free_vdev_map &= ~(1 << arvif->vdev_id);
  2287. list_add(&arvif->list, &ar->arvifs);
  2288. vdev_param = ar->wmi.vdev_param->def_keyid;
  2289. ret = ath10k_wmi_vdev_set_param(ar, 0, vdev_param,
  2290. arvif->def_wep_key_idx);
  2291. if (ret) {
  2292. ath10k_warn(ar, "failed to set vdev %i default key id: %d\n",
  2293. arvif->vdev_id, ret);
  2294. goto err_vdev_delete;
  2295. }
  2296. vdev_param = ar->wmi.vdev_param->tx_encap_type;
  2297. ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
  2298. ATH10K_HW_TXRX_NATIVE_WIFI);
  2299. /* 10.X firmware does not support this VDEV parameter. Do not warn */
  2300. if (ret && ret != -EOPNOTSUPP) {
  2301. ath10k_warn(ar, "failed to set vdev %i TX encapsulation: %d\n",
  2302. arvif->vdev_id, ret);
  2303. goto err_vdev_delete;
  2304. }
  2305. if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
  2306. ret = ath10k_peer_create(ar, arvif->vdev_id, vif->addr);
  2307. if (ret) {
  2308. ath10k_warn(ar, "failed to create vdev %i peer for AP: %d\n",
  2309. arvif->vdev_id, ret);
  2310. goto err_vdev_delete;
  2311. }
  2312. ret = ath10k_mac_set_kickout(arvif);
  2313. if (ret) {
  2314. ath10k_warn(ar, "failed to set vdev %i kickout parameters: %d\n",
  2315. arvif->vdev_id, ret);
  2316. goto err_peer_delete;
  2317. }
  2318. }
  2319. if (arvif->vdev_type == WMI_VDEV_TYPE_STA) {
  2320. param = WMI_STA_PS_PARAM_RX_WAKE_POLICY;
  2321. value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
  2322. ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
  2323. param, value);
  2324. if (ret) {
  2325. ath10k_warn(ar, "failed to set vdev %i RX wake policy: %d\n",
  2326. arvif->vdev_id, ret);
  2327. goto err_peer_delete;
  2328. }
  2329. param = WMI_STA_PS_PARAM_TX_WAKE_THRESHOLD;
  2330. value = WMI_STA_PS_TX_WAKE_THRESHOLD_ALWAYS;
  2331. ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
  2332. param, value);
  2333. if (ret) {
  2334. ath10k_warn(ar, "failed to set vdev %i TX wake thresh: %d\n",
  2335. arvif->vdev_id, ret);
  2336. goto err_peer_delete;
  2337. }
  2338. param = WMI_STA_PS_PARAM_PSPOLL_COUNT;
  2339. value = WMI_STA_PS_PSPOLL_COUNT_NO_MAX;
  2340. ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
  2341. param, value);
  2342. if (ret) {
  2343. ath10k_warn(ar, "failed to set vdev %i PSPOLL count: %d\n",
  2344. arvif->vdev_id, ret);
  2345. goto err_peer_delete;
  2346. }
  2347. }
  2348. ret = ath10k_mac_set_rts(arvif, ar->hw->wiphy->rts_threshold);
  2349. if (ret) {
  2350. ath10k_warn(ar, "failed to set rts threshold for vdev %d: %d\n",
  2351. arvif->vdev_id, ret);
  2352. goto err_peer_delete;
  2353. }
  2354. ret = ath10k_mac_set_frag(arvif, ar->hw->wiphy->frag_threshold);
  2355. if (ret) {
  2356. ath10k_warn(ar, "failed to set frag threshold for vdev %d: %d\n",
  2357. arvif->vdev_id, ret);
  2358. goto err_peer_delete;
  2359. }
  2360. mutex_unlock(&ar->conf_mutex);
  2361. return 0;
  2362. err_peer_delete:
  2363. if (arvif->vdev_type == WMI_VDEV_TYPE_AP)
  2364. ath10k_wmi_peer_delete(ar, arvif->vdev_id, vif->addr);
  2365. err_vdev_delete:
  2366. ath10k_wmi_vdev_delete(ar, arvif->vdev_id);
  2367. ar->free_vdev_map |= 1 << arvif->vdev_id;
  2368. list_del(&arvif->list);
  2369. err:
  2370. mutex_unlock(&ar->conf_mutex);
  2371. return ret;
  2372. }
  2373. static void ath10k_remove_interface(struct ieee80211_hw *hw,
  2374. struct ieee80211_vif *vif)
  2375. {
  2376. struct ath10k *ar = hw->priv;
  2377. struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
  2378. int ret;
  2379. mutex_lock(&ar->conf_mutex);
  2380. cancel_work_sync(&arvif->wep_key_work);
  2381. spin_lock_bh(&ar->data_lock);
  2382. if (arvif->beacon) {
  2383. dma_unmap_single(arvif->ar->dev,
  2384. ATH10K_SKB_CB(arvif->beacon)->paddr,
  2385. arvif->beacon->len, DMA_TO_DEVICE);
  2386. dev_kfree_skb_any(arvif->beacon);
  2387. arvif->beacon = NULL;
  2388. }
  2389. spin_unlock_bh(&ar->data_lock);
  2390. ret = ath10k_spectral_vif_stop(arvif);
  2391. if (ret)
  2392. ath10k_warn(ar, "failed to stop spectral for vdev %i: %d\n",
  2393. arvif->vdev_id, ret);
  2394. ar->free_vdev_map |= 1 << arvif->vdev_id;
  2395. list_del(&arvif->list);
  2396. if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
  2397. ret = ath10k_peer_delete(arvif->ar, arvif->vdev_id, vif->addr);
  2398. if (ret)
  2399. ath10k_warn(ar, "failed to remove peer for AP vdev %i: %d\n",
  2400. arvif->vdev_id, ret);
  2401. kfree(arvif->u.ap.noa_data);
  2402. }
  2403. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %i delete (remove interface)\n",
  2404. arvif->vdev_id);
  2405. ret = ath10k_wmi_vdev_delete(ar, arvif->vdev_id);
  2406. if (ret)
  2407. ath10k_warn(ar, "failed to delete WMI vdev %i: %d\n",
  2408. arvif->vdev_id, ret);
  2409. ath10k_peer_cleanup(ar, arvif->vdev_id);
  2410. mutex_unlock(&ar->conf_mutex);
  2411. }
  2412. /*
  2413. * FIXME: Has to be verified.
  2414. */
  2415. #define SUPPORTED_FILTERS \
  2416. (FIF_PROMISC_IN_BSS | \
  2417. FIF_ALLMULTI | \
  2418. FIF_CONTROL | \
  2419. FIF_PSPOLL | \
  2420. FIF_OTHER_BSS | \
  2421. FIF_BCN_PRBRESP_PROMISC | \
  2422. FIF_PROBE_REQ | \
  2423. FIF_FCSFAIL)
  2424. static void ath10k_configure_filter(struct ieee80211_hw *hw,
  2425. unsigned int changed_flags,
  2426. unsigned int *total_flags,
  2427. u64 multicast)
  2428. {
  2429. struct ath10k *ar = hw->priv;
  2430. int ret;
  2431. mutex_lock(&ar->conf_mutex);
  2432. changed_flags &= SUPPORTED_FILTERS;
  2433. *total_flags &= SUPPORTED_FILTERS;
  2434. ar->filter_flags = *total_flags;
  2435. ret = ath10k_monitor_recalc(ar);
  2436. if (ret)
  2437. ath10k_warn(ar, "failed to recalc montior: %d\n", ret);
  2438. mutex_unlock(&ar->conf_mutex);
  2439. }
  2440. static void ath10k_bss_info_changed(struct ieee80211_hw *hw,
  2441. struct ieee80211_vif *vif,
  2442. struct ieee80211_bss_conf *info,
  2443. u32 changed)
  2444. {
  2445. struct ath10k *ar = hw->priv;
  2446. struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
  2447. int ret = 0;
  2448. u32 vdev_param, pdev_param, slottime, preamble;
  2449. mutex_lock(&ar->conf_mutex);
  2450. if (changed & BSS_CHANGED_IBSS)
  2451. ath10k_control_ibss(arvif, info, vif->addr);
  2452. if (changed & BSS_CHANGED_BEACON_INT) {
  2453. arvif->beacon_interval = info->beacon_int;
  2454. vdev_param = ar->wmi.vdev_param->beacon_interval;
  2455. ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
  2456. arvif->beacon_interval);
  2457. ath10k_dbg(ar, ATH10K_DBG_MAC,
  2458. "mac vdev %d beacon_interval %d\n",
  2459. arvif->vdev_id, arvif->beacon_interval);
  2460. if (ret)
  2461. ath10k_warn(ar, "failed to set beacon interval for vdev %d: %i\n",
  2462. arvif->vdev_id, ret);
  2463. }
  2464. if (changed & BSS_CHANGED_BEACON) {
  2465. ath10k_dbg(ar, ATH10K_DBG_MAC,
  2466. "vdev %d set beacon tx mode to staggered\n",
  2467. arvif->vdev_id);
  2468. pdev_param = ar->wmi.pdev_param->beacon_tx_mode;
  2469. ret = ath10k_wmi_pdev_set_param(ar, pdev_param,
  2470. WMI_BEACON_STAGGERED_MODE);
  2471. if (ret)
  2472. ath10k_warn(ar, "failed to set beacon mode for vdev %d: %i\n",
  2473. arvif->vdev_id, ret);
  2474. }
  2475. if (changed & BSS_CHANGED_BEACON_INFO) {
  2476. arvif->dtim_period = info->dtim_period;
  2477. ath10k_dbg(ar, ATH10K_DBG_MAC,
  2478. "mac vdev %d dtim_period %d\n",
  2479. arvif->vdev_id, arvif->dtim_period);
  2480. vdev_param = ar->wmi.vdev_param->dtim_period;
  2481. ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
  2482. arvif->dtim_period);
  2483. if (ret)
  2484. ath10k_warn(ar, "failed to set dtim period for vdev %d: %i\n",
  2485. arvif->vdev_id, ret);
  2486. }
  2487. if (changed & BSS_CHANGED_SSID &&
  2488. vif->type == NL80211_IFTYPE_AP) {
  2489. arvif->u.ap.ssid_len = info->ssid_len;
  2490. if (info->ssid_len)
  2491. memcpy(arvif->u.ap.ssid, info->ssid, info->ssid_len);
  2492. arvif->u.ap.hidden_ssid = info->hidden_ssid;
  2493. }
  2494. /*
  2495. * Firmware manages AP self-peer internally so make sure to not create
  2496. * it in driver. Otherwise AP self-peer deletion may timeout later.
  2497. */
  2498. if (changed & BSS_CHANGED_BSSID &&
  2499. vif->type != NL80211_IFTYPE_AP) {
  2500. if (!is_zero_ether_addr(info->bssid)) {
  2501. ath10k_dbg(ar, ATH10K_DBG_MAC,
  2502. "mac vdev %d create peer %pM\n",
  2503. arvif->vdev_id, info->bssid);
  2504. ret = ath10k_peer_create(ar, arvif->vdev_id,
  2505. info->bssid);
  2506. if (ret)
  2507. ath10k_warn(ar, "failed to add peer %pM for vdev %d when changing bssid: %i\n",
  2508. info->bssid, arvif->vdev_id, ret);
  2509. if (vif->type == NL80211_IFTYPE_STATION) {
  2510. /*
  2511. * this is never erased as we it for crypto key
  2512. * clearing; this is FW requirement
  2513. */
  2514. ether_addr_copy(arvif->bssid, info->bssid);
  2515. ath10k_dbg(ar, ATH10K_DBG_MAC,
  2516. "mac vdev %d start %pM\n",
  2517. arvif->vdev_id, info->bssid);
  2518. ret = ath10k_vdev_start(arvif);
  2519. if (ret) {
  2520. ath10k_warn(ar, "failed to start vdev %i: %d\n",
  2521. arvif->vdev_id, ret);
  2522. goto exit;
  2523. }
  2524. arvif->is_started = true;
  2525. }
  2526. /*
  2527. * Mac80211 does not keep IBSS bssid when leaving IBSS,
  2528. * so driver need to store it. It is needed when leaving
  2529. * IBSS in order to remove BSSID peer.
  2530. */
  2531. if (vif->type == NL80211_IFTYPE_ADHOC)
  2532. memcpy(arvif->bssid, info->bssid,
  2533. ETH_ALEN);
  2534. }
  2535. }
  2536. if (changed & BSS_CHANGED_BEACON_ENABLED)
  2537. ath10k_control_beaconing(arvif, info);
  2538. if (changed & BSS_CHANGED_ERP_CTS_PROT) {
  2539. arvif->use_cts_prot = info->use_cts_prot;
  2540. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d cts_prot %d\n",
  2541. arvif->vdev_id, info->use_cts_prot);
  2542. ret = ath10k_recalc_rtscts_prot(arvif);
  2543. if (ret)
  2544. ath10k_warn(ar, "failed to recalculate rts/cts prot for vdev %d: %d\n",
  2545. arvif->vdev_id, ret);
  2546. }
  2547. if (changed & BSS_CHANGED_ERP_SLOT) {
  2548. if (info->use_short_slot)
  2549. slottime = WMI_VDEV_SLOT_TIME_SHORT; /* 9us */
  2550. else
  2551. slottime = WMI_VDEV_SLOT_TIME_LONG; /* 20us */
  2552. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d slot_time %d\n",
  2553. arvif->vdev_id, slottime);
  2554. vdev_param = ar->wmi.vdev_param->slot_time;
  2555. ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
  2556. slottime);
  2557. if (ret)
  2558. ath10k_warn(ar, "failed to set erp slot for vdev %d: %i\n",
  2559. arvif->vdev_id, ret);
  2560. }
  2561. if (changed & BSS_CHANGED_ERP_PREAMBLE) {
  2562. if (info->use_short_preamble)
  2563. preamble = WMI_VDEV_PREAMBLE_SHORT;
  2564. else
  2565. preamble = WMI_VDEV_PREAMBLE_LONG;
  2566. ath10k_dbg(ar, ATH10K_DBG_MAC,
  2567. "mac vdev %d preamble %dn",
  2568. arvif->vdev_id, preamble);
  2569. vdev_param = ar->wmi.vdev_param->preamble;
  2570. ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
  2571. preamble);
  2572. if (ret)
  2573. ath10k_warn(ar, "failed to set preamble for vdev %d: %i\n",
  2574. arvif->vdev_id, ret);
  2575. }
  2576. if (changed & BSS_CHANGED_ASSOC) {
  2577. if (info->assoc) {
  2578. /* Workaround: Make sure monitor vdev is not running
  2579. * when associating to prevent some firmware revisions
  2580. * (e.g. 10.1 and 10.2) from crashing.
  2581. */
  2582. if (ar->monitor_started)
  2583. ath10k_monitor_stop(ar);
  2584. ath10k_bss_assoc(hw, vif, info);
  2585. ath10k_monitor_recalc(ar);
  2586. }
  2587. }
  2588. exit:
  2589. mutex_unlock(&ar->conf_mutex);
  2590. }
  2591. static int ath10k_hw_scan(struct ieee80211_hw *hw,
  2592. struct ieee80211_vif *vif,
  2593. struct ieee80211_scan_request *hw_req)
  2594. {
  2595. struct ath10k *ar = hw->priv;
  2596. struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
  2597. struct cfg80211_scan_request *req = &hw_req->req;
  2598. struct wmi_start_scan_arg arg;
  2599. int ret = 0;
  2600. int i;
  2601. mutex_lock(&ar->conf_mutex);
  2602. spin_lock_bh(&ar->data_lock);
  2603. switch (ar->scan.state) {
  2604. case ATH10K_SCAN_IDLE:
  2605. reinit_completion(&ar->scan.started);
  2606. reinit_completion(&ar->scan.completed);
  2607. ar->scan.state = ATH10K_SCAN_STARTING;
  2608. ar->scan.is_roc = false;
  2609. ar->scan.vdev_id = arvif->vdev_id;
  2610. ret = 0;
  2611. break;
  2612. case ATH10K_SCAN_STARTING:
  2613. case ATH10K_SCAN_RUNNING:
  2614. case ATH10K_SCAN_ABORTING:
  2615. ret = -EBUSY;
  2616. break;
  2617. }
  2618. spin_unlock_bh(&ar->data_lock);
  2619. if (ret)
  2620. goto exit;
  2621. memset(&arg, 0, sizeof(arg));
  2622. ath10k_wmi_start_scan_init(ar, &arg);
  2623. arg.vdev_id = arvif->vdev_id;
  2624. arg.scan_id = ATH10K_SCAN_ID;
  2625. if (!req->no_cck)
  2626. arg.scan_ctrl_flags |= WMI_SCAN_ADD_CCK_RATES;
  2627. if (req->ie_len) {
  2628. arg.ie_len = req->ie_len;
  2629. memcpy(arg.ie, req->ie, arg.ie_len);
  2630. }
  2631. if (req->n_ssids) {
  2632. arg.n_ssids = req->n_ssids;
  2633. for (i = 0; i < arg.n_ssids; i++) {
  2634. arg.ssids[i].len = req->ssids[i].ssid_len;
  2635. arg.ssids[i].ssid = req->ssids[i].ssid;
  2636. }
  2637. } else {
  2638. arg.scan_ctrl_flags |= WMI_SCAN_FLAG_PASSIVE;
  2639. }
  2640. if (req->n_channels) {
  2641. arg.n_channels = req->n_channels;
  2642. for (i = 0; i < arg.n_channels; i++)
  2643. arg.channels[i] = req->channels[i]->center_freq;
  2644. }
  2645. ret = ath10k_start_scan(ar, &arg);
  2646. if (ret) {
  2647. ath10k_warn(ar, "failed to start hw scan: %d\n", ret);
  2648. spin_lock_bh(&ar->data_lock);
  2649. ar->scan.state = ATH10K_SCAN_IDLE;
  2650. spin_unlock_bh(&ar->data_lock);
  2651. }
  2652. exit:
  2653. mutex_unlock(&ar->conf_mutex);
  2654. return ret;
  2655. }
  2656. static void ath10k_cancel_hw_scan(struct ieee80211_hw *hw,
  2657. struct ieee80211_vif *vif)
  2658. {
  2659. struct ath10k *ar = hw->priv;
  2660. mutex_lock(&ar->conf_mutex);
  2661. cancel_delayed_work_sync(&ar->scan.timeout);
  2662. ath10k_scan_abort(ar);
  2663. mutex_unlock(&ar->conf_mutex);
  2664. }
  2665. static void ath10k_set_key_h_def_keyidx(struct ath10k *ar,
  2666. struct ath10k_vif *arvif,
  2667. enum set_key_cmd cmd,
  2668. struct ieee80211_key_conf *key)
  2669. {
  2670. u32 vdev_param = arvif->ar->wmi.vdev_param->def_keyid;
  2671. int ret;
  2672. /* 10.1 firmware branch requires default key index to be set to group
  2673. * key index after installing it. Otherwise FW/HW Txes corrupted
  2674. * frames with multi-vif APs. This is not required for main firmware
  2675. * branch (e.g. 636).
  2676. *
  2677. * FIXME: This has been tested only in AP. It remains unknown if this
  2678. * is required for multi-vif STA interfaces on 10.1 */
  2679. if (arvif->vdev_type != WMI_VDEV_TYPE_AP)
  2680. return;
  2681. if (key->cipher == WLAN_CIPHER_SUITE_WEP40)
  2682. return;
  2683. if (key->cipher == WLAN_CIPHER_SUITE_WEP104)
  2684. return;
  2685. if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
  2686. return;
  2687. if (cmd != SET_KEY)
  2688. return;
  2689. ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
  2690. key->keyidx);
  2691. if (ret)
  2692. ath10k_warn(ar, "failed to set vdev %i group key as default key: %d\n",
  2693. arvif->vdev_id, ret);
  2694. }
  2695. static int ath10k_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
  2696. struct ieee80211_vif *vif, struct ieee80211_sta *sta,
  2697. struct ieee80211_key_conf *key)
  2698. {
  2699. struct ath10k *ar = hw->priv;
  2700. struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
  2701. struct ath10k_peer *peer;
  2702. const u8 *peer_addr;
  2703. bool is_wep = key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
  2704. key->cipher == WLAN_CIPHER_SUITE_WEP104;
  2705. int ret = 0;
  2706. if (key->keyidx > WMI_MAX_KEY_INDEX)
  2707. return -ENOSPC;
  2708. mutex_lock(&ar->conf_mutex);
  2709. if (sta)
  2710. peer_addr = sta->addr;
  2711. else if (arvif->vdev_type == WMI_VDEV_TYPE_STA)
  2712. peer_addr = vif->bss_conf.bssid;
  2713. else
  2714. peer_addr = vif->addr;
  2715. key->hw_key_idx = key->keyidx;
  2716. /* the peer should not disappear in mid-way (unless FW goes awry) since
  2717. * we already hold conf_mutex. we just make sure its there now. */
  2718. spin_lock_bh(&ar->data_lock);
  2719. peer = ath10k_peer_find(ar, arvif->vdev_id, peer_addr);
  2720. spin_unlock_bh(&ar->data_lock);
  2721. if (!peer) {
  2722. if (cmd == SET_KEY) {
  2723. ath10k_warn(ar, "failed to install key for non-existent peer %pM\n",
  2724. peer_addr);
  2725. ret = -EOPNOTSUPP;
  2726. goto exit;
  2727. } else {
  2728. /* if the peer doesn't exist there is no key to disable
  2729. * anymore */
  2730. goto exit;
  2731. }
  2732. }
  2733. if (is_wep) {
  2734. if (cmd == SET_KEY)
  2735. arvif->wep_keys[key->keyidx] = key;
  2736. else
  2737. arvif->wep_keys[key->keyidx] = NULL;
  2738. if (cmd == DISABLE_KEY)
  2739. ath10k_clear_vdev_key(arvif, key);
  2740. }
  2741. ret = ath10k_install_key(arvif, key, cmd, peer_addr);
  2742. if (ret) {
  2743. ath10k_warn(ar, "failed to install key for vdev %i peer %pM: %d\n",
  2744. arvif->vdev_id, peer_addr, ret);
  2745. goto exit;
  2746. }
  2747. ath10k_set_key_h_def_keyidx(ar, arvif, cmd, key);
  2748. spin_lock_bh(&ar->data_lock);
  2749. peer = ath10k_peer_find(ar, arvif->vdev_id, peer_addr);
  2750. if (peer && cmd == SET_KEY)
  2751. peer->keys[key->keyidx] = key;
  2752. else if (peer && cmd == DISABLE_KEY)
  2753. peer->keys[key->keyidx] = NULL;
  2754. else if (peer == NULL)
  2755. /* impossible unless FW goes crazy */
  2756. ath10k_warn(ar, "Peer %pM disappeared!\n", peer_addr);
  2757. spin_unlock_bh(&ar->data_lock);
  2758. exit:
  2759. mutex_unlock(&ar->conf_mutex);
  2760. return ret;
  2761. }
  2762. static void ath10k_sta_rc_update_wk(struct work_struct *wk)
  2763. {
  2764. struct ath10k *ar;
  2765. struct ath10k_vif *arvif;
  2766. struct ath10k_sta *arsta;
  2767. struct ieee80211_sta *sta;
  2768. u32 changed, bw, nss, smps;
  2769. int err;
  2770. arsta = container_of(wk, struct ath10k_sta, update_wk);
  2771. sta = container_of((void *)arsta, struct ieee80211_sta, drv_priv);
  2772. arvif = arsta->arvif;
  2773. ar = arvif->ar;
  2774. spin_lock_bh(&ar->data_lock);
  2775. changed = arsta->changed;
  2776. arsta->changed = 0;
  2777. bw = arsta->bw;
  2778. nss = arsta->nss;
  2779. smps = arsta->smps;
  2780. spin_unlock_bh(&ar->data_lock);
  2781. mutex_lock(&ar->conf_mutex);
  2782. if (changed & IEEE80211_RC_BW_CHANGED) {
  2783. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac update sta %pM peer bw %d\n",
  2784. sta->addr, bw);
  2785. err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
  2786. WMI_PEER_CHAN_WIDTH, bw);
  2787. if (err)
  2788. ath10k_warn(ar, "failed to update STA %pM peer bw %d: %d\n",
  2789. sta->addr, bw, err);
  2790. }
  2791. if (changed & IEEE80211_RC_NSS_CHANGED) {
  2792. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac update sta %pM nss %d\n",
  2793. sta->addr, nss);
  2794. err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
  2795. WMI_PEER_NSS, nss);
  2796. if (err)
  2797. ath10k_warn(ar, "failed to update STA %pM nss %d: %d\n",
  2798. sta->addr, nss, err);
  2799. }
  2800. if (changed & IEEE80211_RC_SMPS_CHANGED) {
  2801. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac update sta %pM smps %d\n",
  2802. sta->addr, smps);
  2803. err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
  2804. WMI_PEER_SMPS_STATE, smps);
  2805. if (err)
  2806. ath10k_warn(ar, "failed to update STA %pM smps %d: %d\n",
  2807. sta->addr, smps, err);
  2808. }
  2809. if (changed & IEEE80211_RC_SUPP_RATES_CHANGED) {
  2810. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac update sta %pM supp rates\n",
  2811. sta->addr);
  2812. err = ath10k_station_assoc(ar, arvif, sta, true);
  2813. if (err)
  2814. ath10k_warn(ar, "failed to reassociate station: %pM\n",
  2815. sta->addr);
  2816. }
  2817. mutex_unlock(&ar->conf_mutex);
  2818. }
  2819. static int ath10k_sta_state(struct ieee80211_hw *hw,
  2820. struct ieee80211_vif *vif,
  2821. struct ieee80211_sta *sta,
  2822. enum ieee80211_sta_state old_state,
  2823. enum ieee80211_sta_state new_state)
  2824. {
  2825. struct ath10k *ar = hw->priv;
  2826. struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
  2827. struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
  2828. int max_num_peers;
  2829. int ret = 0;
  2830. if (old_state == IEEE80211_STA_NOTEXIST &&
  2831. new_state == IEEE80211_STA_NONE) {
  2832. memset(arsta, 0, sizeof(*arsta));
  2833. arsta->arvif = arvif;
  2834. INIT_WORK(&arsta->update_wk, ath10k_sta_rc_update_wk);
  2835. }
  2836. /* cancel must be done outside the mutex to avoid deadlock */
  2837. if ((old_state == IEEE80211_STA_NONE &&
  2838. new_state == IEEE80211_STA_NOTEXIST))
  2839. cancel_work_sync(&arsta->update_wk);
  2840. mutex_lock(&ar->conf_mutex);
  2841. if (old_state == IEEE80211_STA_NOTEXIST &&
  2842. new_state == IEEE80211_STA_NONE &&
  2843. vif->type != NL80211_IFTYPE_STATION) {
  2844. /*
  2845. * New station addition.
  2846. */
  2847. if (test_bit(ATH10K_FW_FEATURE_WMI_10X, ar->fw_features))
  2848. max_num_peers = TARGET_10X_NUM_PEERS_MAX - 1;
  2849. else
  2850. max_num_peers = TARGET_NUM_PEERS;
  2851. if (ar->num_peers >= max_num_peers) {
  2852. ath10k_warn(ar, "number of peers exceeded: peers number %d (max peers %d)\n",
  2853. ar->num_peers, max_num_peers);
  2854. ret = -ENOBUFS;
  2855. goto exit;
  2856. }
  2857. ath10k_dbg(ar, ATH10K_DBG_MAC,
  2858. "mac vdev %d peer create %pM (new sta) num_peers %d\n",
  2859. arvif->vdev_id, sta->addr, ar->num_peers);
  2860. ret = ath10k_peer_create(ar, arvif->vdev_id, sta->addr);
  2861. if (ret)
  2862. ath10k_warn(ar, "failed to add peer %pM for vdev %d when adding a new sta: %i\n",
  2863. sta->addr, arvif->vdev_id, ret);
  2864. } else if ((old_state == IEEE80211_STA_NONE &&
  2865. new_state == IEEE80211_STA_NOTEXIST)) {
  2866. /*
  2867. * Existing station deletion.
  2868. */
  2869. ath10k_dbg(ar, ATH10K_DBG_MAC,
  2870. "mac vdev %d peer delete %pM (sta gone)\n",
  2871. arvif->vdev_id, sta->addr);
  2872. ret = ath10k_peer_delete(ar, arvif->vdev_id, sta->addr);
  2873. if (ret)
  2874. ath10k_warn(ar, "failed to delete peer %pM for vdev %d: %i\n",
  2875. sta->addr, arvif->vdev_id, ret);
  2876. if (vif->type == NL80211_IFTYPE_STATION)
  2877. ath10k_bss_disassoc(hw, vif);
  2878. } else if (old_state == IEEE80211_STA_AUTH &&
  2879. new_state == IEEE80211_STA_ASSOC &&
  2880. (vif->type == NL80211_IFTYPE_AP ||
  2881. vif->type == NL80211_IFTYPE_ADHOC)) {
  2882. /*
  2883. * New association.
  2884. */
  2885. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac sta %pM associated\n",
  2886. sta->addr);
  2887. ret = ath10k_station_assoc(ar, arvif, sta, false);
  2888. if (ret)
  2889. ath10k_warn(ar, "failed to associate station %pM for vdev %i: %i\n",
  2890. sta->addr, arvif->vdev_id, ret);
  2891. } else if (old_state == IEEE80211_STA_ASSOC &&
  2892. new_state == IEEE80211_STA_AUTH &&
  2893. (vif->type == NL80211_IFTYPE_AP ||
  2894. vif->type == NL80211_IFTYPE_ADHOC)) {
  2895. /*
  2896. * Disassociation.
  2897. */
  2898. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac sta %pM disassociated\n",
  2899. sta->addr);
  2900. ret = ath10k_station_disassoc(ar, arvif, sta);
  2901. if (ret)
  2902. ath10k_warn(ar, "failed to disassociate station: %pM vdev %i: %i\n",
  2903. sta->addr, arvif->vdev_id, ret);
  2904. }
  2905. exit:
  2906. mutex_unlock(&ar->conf_mutex);
  2907. return ret;
  2908. }
  2909. static int ath10k_conf_tx_uapsd(struct ath10k *ar, struct ieee80211_vif *vif,
  2910. u16 ac, bool enable)
  2911. {
  2912. struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
  2913. u32 value = 0;
  2914. int ret = 0;
  2915. lockdep_assert_held(&ar->conf_mutex);
  2916. if (arvif->vdev_type != WMI_VDEV_TYPE_STA)
  2917. return 0;
  2918. switch (ac) {
  2919. case IEEE80211_AC_VO:
  2920. value = WMI_STA_PS_UAPSD_AC3_DELIVERY_EN |
  2921. WMI_STA_PS_UAPSD_AC3_TRIGGER_EN;
  2922. break;
  2923. case IEEE80211_AC_VI:
  2924. value = WMI_STA_PS_UAPSD_AC2_DELIVERY_EN |
  2925. WMI_STA_PS_UAPSD_AC2_TRIGGER_EN;
  2926. break;
  2927. case IEEE80211_AC_BE:
  2928. value = WMI_STA_PS_UAPSD_AC1_DELIVERY_EN |
  2929. WMI_STA_PS_UAPSD_AC1_TRIGGER_EN;
  2930. break;
  2931. case IEEE80211_AC_BK:
  2932. value = WMI_STA_PS_UAPSD_AC0_DELIVERY_EN |
  2933. WMI_STA_PS_UAPSD_AC0_TRIGGER_EN;
  2934. break;
  2935. }
  2936. if (enable)
  2937. arvif->u.sta.uapsd |= value;
  2938. else
  2939. arvif->u.sta.uapsd &= ~value;
  2940. ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
  2941. WMI_STA_PS_PARAM_UAPSD,
  2942. arvif->u.sta.uapsd);
  2943. if (ret) {
  2944. ath10k_warn(ar, "failed to set uapsd params: %d\n", ret);
  2945. goto exit;
  2946. }
  2947. if (arvif->u.sta.uapsd)
  2948. value = WMI_STA_PS_RX_WAKE_POLICY_POLL_UAPSD;
  2949. else
  2950. value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
  2951. ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
  2952. WMI_STA_PS_PARAM_RX_WAKE_POLICY,
  2953. value);
  2954. if (ret)
  2955. ath10k_warn(ar, "failed to set rx wake param: %d\n", ret);
  2956. exit:
  2957. return ret;
  2958. }
  2959. static int ath10k_conf_tx(struct ieee80211_hw *hw,
  2960. struct ieee80211_vif *vif, u16 ac,
  2961. const struct ieee80211_tx_queue_params *params)
  2962. {
  2963. struct ath10k *ar = hw->priv;
  2964. struct wmi_wmm_params_arg *p = NULL;
  2965. int ret;
  2966. mutex_lock(&ar->conf_mutex);
  2967. switch (ac) {
  2968. case IEEE80211_AC_VO:
  2969. p = &ar->wmm_params.ac_vo;
  2970. break;
  2971. case IEEE80211_AC_VI:
  2972. p = &ar->wmm_params.ac_vi;
  2973. break;
  2974. case IEEE80211_AC_BE:
  2975. p = &ar->wmm_params.ac_be;
  2976. break;
  2977. case IEEE80211_AC_BK:
  2978. p = &ar->wmm_params.ac_bk;
  2979. break;
  2980. }
  2981. if (WARN_ON(!p)) {
  2982. ret = -EINVAL;
  2983. goto exit;
  2984. }
  2985. p->cwmin = params->cw_min;
  2986. p->cwmax = params->cw_max;
  2987. p->aifs = params->aifs;
  2988. /*
  2989. * The channel time duration programmed in the HW is in absolute
  2990. * microseconds, while mac80211 gives the txop in units of
  2991. * 32 microseconds.
  2992. */
  2993. p->txop = params->txop * 32;
  2994. /* FIXME: FW accepts wmm params per hw, not per vif */
  2995. ret = ath10k_wmi_pdev_set_wmm_params(ar, &ar->wmm_params);
  2996. if (ret) {
  2997. ath10k_warn(ar, "failed to set wmm params: %d\n", ret);
  2998. goto exit;
  2999. }
  3000. ret = ath10k_conf_tx_uapsd(ar, vif, ac, params->uapsd);
  3001. if (ret)
  3002. ath10k_warn(ar, "failed to set sta uapsd: %d\n", ret);
  3003. exit:
  3004. mutex_unlock(&ar->conf_mutex);
  3005. return ret;
  3006. }
  3007. #define ATH10K_ROC_TIMEOUT_HZ (2*HZ)
  3008. static int ath10k_remain_on_channel(struct ieee80211_hw *hw,
  3009. struct ieee80211_vif *vif,
  3010. struct ieee80211_channel *chan,
  3011. int duration,
  3012. enum ieee80211_roc_type type)
  3013. {
  3014. struct ath10k *ar = hw->priv;
  3015. struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
  3016. struct wmi_start_scan_arg arg;
  3017. int ret = 0;
  3018. mutex_lock(&ar->conf_mutex);
  3019. spin_lock_bh(&ar->data_lock);
  3020. switch (ar->scan.state) {
  3021. case ATH10K_SCAN_IDLE:
  3022. reinit_completion(&ar->scan.started);
  3023. reinit_completion(&ar->scan.completed);
  3024. reinit_completion(&ar->scan.on_channel);
  3025. ar->scan.state = ATH10K_SCAN_STARTING;
  3026. ar->scan.is_roc = true;
  3027. ar->scan.vdev_id = arvif->vdev_id;
  3028. ar->scan.roc_freq = chan->center_freq;
  3029. ret = 0;
  3030. break;
  3031. case ATH10K_SCAN_STARTING:
  3032. case ATH10K_SCAN_RUNNING:
  3033. case ATH10K_SCAN_ABORTING:
  3034. ret = -EBUSY;
  3035. break;
  3036. }
  3037. spin_unlock_bh(&ar->data_lock);
  3038. if (ret)
  3039. goto exit;
  3040. memset(&arg, 0, sizeof(arg));
  3041. ath10k_wmi_start_scan_init(ar, &arg);
  3042. arg.vdev_id = arvif->vdev_id;
  3043. arg.scan_id = ATH10K_SCAN_ID;
  3044. arg.n_channels = 1;
  3045. arg.channels[0] = chan->center_freq;
  3046. arg.dwell_time_active = duration;
  3047. arg.dwell_time_passive = duration;
  3048. arg.max_scan_time = 2 * duration;
  3049. arg.scan_ctrl_flags |= WMI_SCAN_FLAG_PASSIVE;
  3050. arg.scan_ctrl_flags |= WMI_SCAN_FILTER_PROBE_REQ;
  3051. ret = ath10k_start_scan(ar, &arg);
  3052. if (ret) {
  3053. ath10k_warn(ar, "failed to start roc scan: %d\n", ret);
  3054. spin_lock_bh(&ar->data_lock);
  3055. ar->scan.state = ATH10K_SCAN_IDLE;
  3056. spin_unlock_bh(&ar->data_lock);
  3057. goto exit;
  3058. }
  3059. ret = wait_for_completion_timeout(&ar->scan.on_channel, 3*HZ);
  3060. if (ret == 0) {
  3061. ath10k_warn(ar, "failed to switch to channel for roc scan\n");
  3062. ret = ath10k_scan_stop(ar);
  3063. if (ret)
  3064. ath10k_warn(ar, "failed to stop scan: %d\n", ret);
  3065. ret = -ETIMEDOUT;
  3066. goto exit;
  3067. }
  3068. ret = 0;
  3069. exit:
  3070. mutex_unlock(&ar->conf_mutex);
  3071. return ret;
  3072. }
  3073. static int ath10k_cancel_remain_on_channel(struct ieee80211_hw *hw)
  3074. {
  3075. struct ath10k *ar = hw->priv;
  3076. mutex_lock(&ar->conf_mutex);
  3077. cancel_delayed_work_sync(&ar->scan.timeout);
  3078. ath10k_scan_abort(ar);
  3079. mutex_unlock(&ar->conf_mutex);
  3080. return 0;
  3081. }
  3082. /*
  3083. * Both RTS and Fragmentation threshold are interface-specific
  3084. * in ath10k, but device-specific in mac80211.
  3085. */
  3086. static int ath10k_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
  3087. {
  3088. struct ath10k *ar = hw->priv;
  3089. struct ath10k_vif *arvif;
  3090. int ret = 0;
  3091. mutex_lock(&ar->conf_mutex);
  3092. list_for_each_entry(arvif, &ar->arvifs, list) {
  3093. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d rts threshold %d\n",
  3094. arvif->vdev_id, value);
  3095. ret = ath10k_mac_set_rts(arvif, value);
  3096. if (ret) {
  3097. ath10k_warn(ar, "failed to set rts threshold for vdev %d: %d\n",
  3098. arvif->vdev_id, ret);
  3099. break;
  3100. }
  3101. }
  3102. mutex_unlock(&ar->conf_mutex);
  3103. return ret;
  3104. }
  3105. static int ath10k_set_frag_threshold(struct ieee80211_hw *hw, u32 value)
  3106. {
  3107. struct ath10k *ar = hw->priv;
  3108. struct ath10k_vif *arvif;
  3109. int ret = 0;
  3110. mutex_lock(&ar->conf_mutex);
  3111. list_for_each_entry(arvif, &ar->arvifs, list) {
  3112. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d fragmentation threshold %d\n",
  3113. arvif->vdev_id, value);
  3114. ret = ath10k_mac_set_rts(arvif, value);
  3115. if (ret) {
  3116. ath10k_warn(ar, "failed to set fragmentation threshold for vdev %d: %d\n",
  3117. arvif->vdev_id, ret);
  3118. break;
  3119. }
  3120. }
  3121. mutex_unlock(&ar->conf_mutex);
  3122. return ret;
  3123. }
  3124. static void ath10k_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  3125. u32 queues, bool drop)
  3126. {
  3127. struct ath10k *ar = hw->priv;
  3128. bool skip;
  3129. int ret;
  3130. /* mac80211 doesn't care if we really xmit queued frames or not
  3131. * we'll collect those frames either way if we stop/delete vdevs */
  3132. if (drop)
  3133. return;
  3134. mutex_lock(&ar->conf_mutex);
  3135. if (ar->state == ATH10K_STATE_WEDGED)
  3136. goto skip;
  3137. ret = wait_event_timeout(ar->htt.empty_tx_wq, ({
  3138. bool empty;
  3139. spin_lock_bh(&ar->htt.tx_lock);
  3140. empty = (ar->htt.num_pending_tx == 0);
  3141. spin_unlock_bh(&ar->htt.tx_lock);
  3142. skip = (ar->state == ATH10K_STATE_WEDGED);
  3143. (empty || skip);
  3144. }), ATH10K_FLUSH_TIMEOUT_HZ);
  3145. if (ret <= 0 || skip)
  3146. ath10k_warn(ar, "failed to flush transmit queue (skip %i ar-state %i): %i\n",
  3147. skip, ar->state, ret);
  3148. skip:
  3149. mutex_unlock(&ar->conf_mutex);
  3150. }
  3151. /* TODO: Implement this function properly
  3152. * For now it is needed to reply to Probe Requests in IBSS mode.
  3153. * Propably we need this information from FW.
  3154. */
  3155. static int ath10k_tx_last_beacon(struct ieee80211_hw *hw)
  3156. {
  3157. return 1;
  3158. }
  3159. #ifdef CONFIG_PM
  3160. static int ath10k_suspend(struct ieee80211_hw *hw,
  3161. struct cfg80211_wowlan *wowlan)
  3162. {
  3163. struct ath10k *ar = hw->priv;
  3164. int ret;
  3165. mutex_lock(&ar->conf_mutex);
  3166. ret = ath10k_wait_for_suspend(ar, WMI_PDEV_SUSPEND);
  3167. if (ret) {
  3168. if (ret == -ETIMEDOUT)
  3169. goto resume;
  3170. ret = 1;
  3171. goto exit;
  3172. }
  3173. ret = ath10k_hif_suspend(ar);
  3174. if (ret) {
  3175. ath10k_warn(ar, "failed to suspend hif: %d\n", ret);
  3176. goto resume;
  3177. }
  3178. ret = 0;
  3179. goto exit;
  3180. resume:
  3181. ret = ath10k_wmi_pdev_resume_target(ar);
  3182. if (ret)
  3183. ath10k_warn(ar, "failed to resume target: %d\n", ret);
  3184. ret = 1;
  3185. exit:
  3186. mutex_unlock(&ar->conf_mutex);
  3187. return ret;
  3188. }
  3189. static int ath10k_resume(struct ieee80211_hw *hw)
  3190. {
  3191. struct ath10k *ar = hw->priv;
  3192. int ret;
  3193. mutex_lock(&ar->conf_mutex);
  3194. ret = ath10k_hif_resume(ar);
  3195. if (ret) {
  3196. ath10k_warn(ar, "failed to resume hif: %d\n", ret);
  3197. ret = 1;
  3198. goto exit;
  3199. }
  3200. ret = ath10k_wmi_pdev_resume_target(ar);
  3201. if (ret) {
  3202. ath10k_warn(ar, "failed to resume target: %d\n", ret);
  3203. ret = 1;
  3204. goto exit;
  3205. }
  3206. ret = 0;
  3207. exit:
  3208. mutex_unlock(&ar->conf_mutex);
  3209. return ret;
  3210. }
  3211. #endif
  3212. static void ath10k_restart_complete(struct ieee80211_hw *hw)
  3213. {
  3214. struct ath10k *ar = hw->priv;
  3215. mutex_lock(&ar->conf_mutex);
  3216. /* If device failed to restart it will be in a different state, e.g.
  3217. * ATH10K_STATE_WEDGED */
  3218. if (ar->state == ATH10K_STATE_RESTARTED) {
  3219. ath10k_info(ar, "device successfully recovered\n");
  3220. ar->state = ATH10K_STATE_ON;
  3221. }
  3222. mutex_unlock(&ar->conf_mutex);
  3223. }
  3224. static int ath10k_get_survey(struct ieee80211_hw *hw, int idx,
  3225. struct survey_info *survey)
  3226. {
  3227. struct ath10k *ar = hw->priv;
  3228. struct ieee80211_supported_band *sband;
  3229. struct survey_info *ar_survey = &ar->survey[idx];
  3230. int ret = 0;
  3231. mutex_lock(&ar->conf_mutex);
  3232. sband = hw->wiphy->bands[IEEE80211_BAND_2GHZ];
  3233. if (sband && idx >= sband->n_channels) {
  3234. idx -= sband->n_channels;
  3235. sband = NULL;
  3236. }
  3237. if (!sband)
  3238. sband = hw->wiphy->bands[IEEE80211_BAND_5GHZ];
  3239. if (!sband || idx >= sband->n_channels) {
  3240. ret = -ENOENT;
  3241. goto exit;
  3242. }
  3243. spin_lock_bh(&ar->data_lock);
  3244. memcpy(survey, ar_survey, sizeof(*survey));
  3245. spin_unlock_bh(&ar->data_lock);
  3246. survey->channel = &sband->channels[idx];
  3247. exit:
  3248. mutex_unlock(&ar->conf_mutex);
  3249. return ret;
  3250. }
  3251. /* Helper table for legacy fixed_rate/bitrate_mask */
  3252. static const u8 cck_ofdm_rate[] = {
  3253. /* CCK */
  3254. 3, /* 1Mbps */
  3255. 2, /* 2Mbps */
  3256. 1, /* 5.5Mbps */
  3257. 0, /* 11Mbps */
  3258. /* OFDM */
  3259. 3, /* 6Mbps */
  3260. 7, /* 9Mbps */
  3261. 2, /* 12Mbps */
  3262. 6, /* 18Mbps */
  3263. 1, /* 24Mbps */
  3264. 5, /* 36Mbps */
  3265. 0, /* 48Mbps */
  3266. 4, /* 54Mbps */
  3267. };
  3268. /* Check if only one bit set */
  3269. static int ath10k_check_single_mask(u32 mask)
  3270. {
  3271. int bit;
  3272. bit = ffs(mask);
  3273. if (!bit)
  3274. return 0;
  3275. mask &= ~BIT(bit - 1);
  3276. if (mask)
  3277. return 2;
  3278. return 1;
  3279. }
  3280. static bool
  3281. ath10k_default_bitrate_mask(struct ath10k *ar,
  3282. enum ieee80211_band band,
  3283. const struct cfg80211_bitrate_mask *mask)
  3284. {
  3285. u32 legacy = 0x00ff;
  3286. u8 ht = 0xff, i;
  3287. u16 vht = 0x3ff;
  3288. switch (band) {
  3289. case IEEE80211_BAND_2GHZ:
  3290. legacy = 0x00fff;
  3291. vht = 0;
  3292. break;
  3293. case IEEE80211_BAND_5GHZ:
  3294. break;
  3295. default:
  3296. return false;
  3297. }
  3298. if (mask->control[band].legacy != legacy)
  3299. return false;
  3300. for (i = 0; i < ar->num_rf_chains; i++)
  3301. if (mask->control[band].ht_mcs[i] != ht)
  3302. return false;
  3303. for (i = 0; i < ar->num_rf_chains; i++)
  3304. if (mask->control[band].vht_mcs[i] != vht)
  3305. return false;
  3306. return true;
  3307. }
  3308. static bool
  3309. ath10k_bitrate_mask_nss(const struct cfg80211_bitrate_mask *mask,
  3310. enum ieee80211_band band,
  3311. u8 *fixed_nss)
  3312. {
  3313. int ht_nss = 0, vht_nss = 0, i;
  3314. /* check legacy */
  3315. if (ath10k_check_single_mask(mask->control[band].legacy))
  3316. return false;
  3317. /* check HT */
  3318. for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++) {
  3319. if (mask->control[band].ht_mcs[i] == 0xff)
  3320. continue;
  3321. else if (mask->control[band].ht_mcs[i] == 0x00)
  3322. break;
  3323. return false;
  3324. }
  3325. ht_nss = i;
  3326. /* check VHT */
  3327. for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
  3328. if (mask->control[band].vht_mcs[i] == 0x03ff)
  3329. continue;
  3330. else if (mask->control[band].vht_mcs[i] == 0x0000)
  3331. break;
  3332. return false;
  3333. }
  3334. vht_nss = i;
  3335. if (ht_nss > 0 && vht_nss > 0)
  3336. return false;
  3337. if (ht_nss)
  3338. *fixed_nss = ht_nss;
  3339. else if (vht_nss)
  3340. *fixed_nss = vht_nss;
  3341. else
  3342. return false;
  3343. return true;
  3344. }
  3345. static bool
  3346. ath10k_bitrate_mask_correct(const struct cfg80211_bitrate_mask *mask,
  3347. enum ieee80211_band band,
  3348. enum wmi_rate_preamble *preamble)
  3349. {
  3350. int legacy = 0, ht = 0, vht = 0, i;
  3351. *preamble = WMI_RATE_PREAMBLE_OFDM;
  3352. /* check legacy */
  3353. legacy = ath10k_check_single_mask(mask->control[band].legacy);
  3354. if (legacy > 1)
  3355. return false;
  3356. /* check HT */
  3357. for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
  3358. ht += ath10k_check_single_mask(mask->control[band].ht_mcs[i]);
  3359. if (ht > 1)
  3360. return false;
  3361. /* check VHT */
  3362. for (i = 0; i < NL80211_VHT_NSS_MAX; i++)
  3363. vht += ath10k_check_single_mask(mask->control[band].vht_mcs[i]);
  3364. if (vht > 1)
  3365. return false;
  3366. /* Currently we support only one fixed_rate */
  3367. if ((legacy + ht + vht) != 1)
  3368. return false;
  3369. if (ht)
  3370. *preamble = WMI_RATE_PREAMBLE_HT;
  3371. else if (vht)
  3372. *preamble = WMI_RATE_PREAMBLE_VHT;
  3373. return true;
  3374. }
  3375. static bool
  3376. ath10k_bitrate_mask_rate(struct ath10k *ar,
  3377. const struct cfg80211_bitrate_mask *mask,
  3378. enum ieee80211_band band,
  3379. u8 *fixed_rate,
  3380. u8 *fixed_nss)
  3381. {
  3382. u8 rate = 0, pream = 0, nss = 0, i;
  3383. enum wmi_rate_preamble preamble;
  3384. /* Check if single rate correct */
  3385. if (!ath10k_bitrate_mask_correct(mask, band, &preamble))
  3386. return false;
  3387. pream = preamble;
  3388. switch (preamble) {
  3389. case WMI_RATE_PREAMBLE_CCK:
  3390. case WMI_RATE_PREAMBLE_OFDM:
  3391. i = ffs(mask->control[band].legacy) - 1;
  3392. if (band == IEEE80211_BAND_2GHZ && i < 4)
  3393. pream = WMI_RATE_PREAMBLE_CCK;
  3394. if (band == IEEE80211_BAND_5GHZ)
  3395. i += 4;
  3396. if (i >= ARRAY_SIZE(cck_ofdm_rate))
  3397. return false;
  3398. rate = cck_ofdm_rate[i];
  3399. break;
  3400. case WMI_RATE_PREAMBLE_HT:
  3401. for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
  3402. if (mask->control[band].ht_mcs[i])
  3403. break;
  3404. if (i == IEEE80211_HT_MCS_MASK_LEN)
  3405. return false;
  3406. rate = ffs(mask->control[band].ht_mcs[i]) - 1;
  3407. nss = i;
  3408. break;
  3409. case WMI_RATE_PREAMBLE_VHT:
  3410. for (i = 0; i < NL80211_VHT_NSS_MAX; i++)
  3411. if (mask->control[band].vht_mcs[i])
  3412. break;
  3413. if (i == NL80211_VHT_NSS_MAX)
  3414. return false;
  3415. rate = ffs(mask->control[band].vht_mcs[i]) - 1;
  3416. nss = i;
  3417. break;
  3418. }
  3419. *fixed_nss = nss + 1;
  3420. nss <<= 4;
  3421. pream <<= 6;
  3422. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac fixed rate pream 0x%02x nss 0x%02x rate 0x%02x\n",
  3423. pream, nss, rate);
  3424. *fixed_rate = pream | nss | rate;
  3425. return true;
  3426. }
  3427. static bool ath10k_get_fixed_rate_nss(struct ath10k *ar,
  3428. const struct cfg80211_bitrate_mask *mask,
  3429. enum ieee80211_band band,
  3430. u8 *fixed_rate,
  3431. u8 *fixed_nss)
  3432. {
  3433. /* First check full NSS mask, if we can simply limit NSS */
  3434. if (ath10k_bitrate_mask_nss(mask, band, fixed_nss))
  3435. return true;
  3436. /* Next Check single rate is set */
  3437. return ath10k_bitrate_mask_rate(ar, mask, band, fixed_rate, fixed_nss);
  3438. }
  3439. static int ath10k_set_fixed_rate_param(struct ath10k_vif *arvif,
  3440. u8 fixed_rate,
  3441. u8 fixed_nss,
  3442. u8 force_sgi)
  3443. {
  3444. struct ath10k *ar = arvif->ar;
  3445. u32 vdev_param;
  3446. int ret = 0;
  3447. mutex_lock(&ar->conf_mutex);
  3448. if (arvif->fixed_rate == fixed_rate &&
  3449. arvif->fixed_nss == fixed_nss &&
  3450. arvif->force_sgi == force_sgi)
  3451. goto exit;
  3452. if (fixed_rate == WMI_FIXED_RATE_NONE)
  3453. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac disable fixed bitrate mask\n");
  3454. if (force_sgi)
  3455. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac force sgi\n");
  3456. vdev_param = ar->wmi.vdev_param->fixed_rate;
  3457. ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id,
  3458. vdev_param, fixed_rate);
  3459. if (ret) {
  3460. ath10k_warn(ar, "failed to set fixed rate param 0x%02x: %d\n",
  3461. fixed_rate, ret);
  3462. ret = -EINVAL;
  3463. goto exit;
  3464. }
  3465. arvif->fixed_rate = fixed_rate;
  3466. vdev_param = ar->wmi.vdev_param->nss;
  3467. ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id,
  3468. vdev_param, fixed_nss);
  3469. if (ret) {
  3470. ath10k_warn(ar, "failed to set fixed nss param %d: %d\n",
  3471. fixed_nss, ret);
  3472. ret = -EINVAL;
  3473. goto exit;
  3474. }
  3475. arvif->fixed_nss = fixed_nss;
  3476. vdev_param = ar->wmi.vdev_param->sgi;
  3477. ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
  3478. force_sgi);
  3479. if (ret) {
  3480. ath10k_warn(ar, "failed to set sgi param %d: %d\n",
  3481. force_sgi, ret);
  3482. ret = -EINVAL;
  3483. goto exit;
  3484. }
  3485. arvif->force_sgi = force_sgi;
  3486. exit:
  3487. mutex_unlock(&ar->conf_mutex);
  3488. return ret;
  3489. }
  3490. static int ath10k_set_bitrate_mask(struct ieee80211_hw *hw,
  3491. struct ieee80211_vif *vif,
  3492. const struct cfg80211_bitrate_mask *mask)
  3493. {
  3494. struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
  3495. struct ath10k *ar = arvif->ar;
  3496. enum ieee80211_band band = ar->hw->conf.chandef.chan->band;
  3497. u8 fixed_rate = WMI_FIXED_RATE_NONE;
  3498. u8 fixed_nss = ar->num_rf_chains;
  3499. u8 force_sgi;
  3500. force_sgi = mask->control[band].gi;
  3501. if (force_sgi == NL80211_TXRATE_FORCE_LGI)
  3502. return -EINVAL;
  3503. if (!ath10k_default_bitrate_mask(ar, band, mask)) {
  3504. if (!ath10k_get_fixed_rate_nss(ar, mask, band,
  3505. &fixed_rate,
  3506. &fixed_nss))
  3507. return -EINVAL;
  3508. }
  3509. if (fixed_rate == WMI_FIXED_RATE_NONE && force_sgi) {
  3510. ath10k_warn(ar, "failed to force SGI usage for default rate settings\n");
  3511. return -EINVAL;
  3512. }
  3513. return ath10k_set_fixed_rate_param(arvif, fixed_rate,
  3514. fixed_nss, force_sgi);
  3515. }
  3516. static void ath10k_sta_rc_update(struct ieee80211_hw *hw,
  3517. struct ieee80211_vif *vif,
  3518. struct ieee80211_sta *sta,
  3519. u32 changed)
  3520. {
  3521. struct ath10k *ar = hw->priv;
  3522. struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
  3523. u32 bw, smps;
  3524. spin_lock_bh(&ar->data_lock);
  3525. ath10k_dbg(ar, ATH10K_DBG_MAC,
  3526. "mac sta rc update for %pM changed %08x bw %d nss %d smps %d\n",
  3527. sta->addr, changed, sta->bandwidth, sta->rx_nss,
  3528. sta->smps_mode);
  3529. if (changed & IEEE80211_RC_BW_CHANGED) {
  3530. bw = WMI_PEER_CHWIDTH_20MHZ;
  3531. switch (sta->bandwidth) {
  3532. case IEEE80211_STA_RX_BW_20:
  3533. bw = WMI_PEER_CHWIDTH_20MHZ;
  3534. break;
  3535. case IEEE80211_STA_RX_BW_40:
  3536. bw = WMI_PEER_CHWIDTH_40MHZ;
  3537. break;
  3538. case IEEE80211_STA_RX_BW_80:
  3539. bw = WMI_PEER_CHWIDTH_80MHZ;
  3540. break;
  3541. case IEEE80211_STA_RX_BW_160:
  3542. ath10k_warn(ar, "Invalid bandwith %d in rc update for %pM\n",
  3543. sta->bandwidth, sta->addr);
  3544. bw = WMI_PEER_CHWIDTH_20MHZ;
  3545. break;
  3546. }
  3547. arsta->bw = bw;
  3548. }
  3549. if (changed & IEEE80211_RC_NSS_CHANGED)
  3550. arsta->nss = sta->rx_nss;
  3551. if (changed & IEEE80211_RC_SMPS_CHANGED) {
  3552. smps = WMI_PEER_SMPS_PS_NONE;
  3553. switch (sta->smps_mode) {
  3554. case IEEE80211_SMPS_AUTOMATIC:
  3555. case IEEE80211_SMPS_OFF:
  3556. smps = WMI_PEER_SMPS_PS_NONE;
  3557. break;
  3558. case IEEE80211_SMPS_STATIC:
  3559. smps = WMI_PEER_SMPS_STATIC;
  3560. break;
  3561. case IEEE80211_SMPS_DYNAMIC:
  3562. smps = WMI_PEER_SMPS_DYNAMIC;
  3563. break;
  3564. case IEEE80211_SMPS_NUM_MODES:
  3565. ath10k_warn(ar, "Invalid smps %d in sta rc update for %pM\n",
  3566. sta->smps_mode, sta->addr);
  3567. smps = WMI_PEER_SMPS_PS_NONE;
  3568. break;
  3569. }
  3570. arsta->smps = smps;
  3571. }
  3572. arsta->changed |= changed;
  3573. spin_unlock_bh(&ar->data_lock);
  3574. ieee80211_queue_work(hw, &arsta->update_wk);
  3575. }
  3576. static u64 ath10k_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
  3577. {
  3578. /*
  3579. * FIXME: Return 0 for time being. Need to figure out whether FW
  3580. * has the API to fetch 64-bit local TSF
  3581. */
  3582. return 0;
  3583. }
  3584. static int ath10k_ampdu_action(struct ieee80211_hw *hw,
  3585. struct ieee80211_vif *vif,
  3586. enum ieee80211_ampdu_mlme_action action,
  3587. struct ieee80211_sta *sta, u16 tid, u16 *ssn,
  3588. u8 buf_size)
  3589. {
  3590. struct ath10k *ar = hw->priv;
  3591. struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
  3592. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac ampdu vdev_id %i sta %pM tid %hu action %d\n",
  3593. arvif->vdev_id, sta->addr, tid, action);
  3594. switch (action) {
  3595. case IEEE80211_AMPDU_RX_START:
  3596. case IEEE80211_AMPDU_RX_STOP:
  3597. /* HTT AddBa/DelBa events trigger mac80211 Rx BA session
  3598. * creation/removal. Do we need to verify this?
  3599. */
  3600. return 0;
  3601. case IEEE80211_AMPDU_TX_START:
  3602. case IEEE80211_AMPDU_TX_STOP_CONT:
  3603. case IEEE80211_AMPDU_TX_STOP_FLUSH:
  3604. case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
  3605. case IEEE80211_AMPDU_TX_OPERATIONAL:
  3606. /* Firmware offloads Tx aggregation entirely so deny mac80211
  3607. * Tx aggregation requests.
  3608. */
  3609. return -EOPNOTSUPP;
  3610. }
  3611. return -EINVAL;
  3612. }
  3613. static const struct ieee80211_ops ath10k_ops = {
  3614. .tx = ath10k_tx,
  3615. .start = ath10k_start,
  3616. .stop = ath10k_stop,
  3617. .config = ath10k_config,
  3618. .add_interface = ath10k_add_interface,
  3619. .remove_interface = ath10k_remove_interface,
  3620. .configure_filter = ath10k_configure_filter,
  3621. .bss_info_changed = ath10k_bss_info_changed,
  3622. .hw_scan = ath10k_hw_scan,
  3623. .cancel_hw_scan = ath10k_cancel_hw_scan,
  3624. .set_key = ath10k_set_key,
  3625. .sta_state = ath10k_sta_state,
  3626. .conf_tx = ath10k_conf_tx,
  3627. .remain_on_channel = ath10k_remain_on_channel,
  3628. .cancel_remain_on_channel = ath10k_cancel_remain_on_channel,
  3629. .set_rts_threshold = ath10k_set_rts_threshold,
  3630. .set_frag_threshold = ath10k_set_frag_threshold,
  3631. .flush = ath10k_flush,
  3632. .tx_last_beacon = ath10k_tx_last_beacon,
  3633. .set_antenna = ath10k_set_antenna,
  3634. .get_antenna = ath10k_get_antenna,
  3635. .restart_complete = ath10k_restart_complete,
  3636. .get_survey = ath10k_get_survey,
  3637. .set_bitrate_mask = ath10k_set_bitrate_mask,
  3638. .sta_rc_update = ath10k_sta_rc_update,
  3639. .get_tsf = ath10k_get_tsf,
  3640. .ampdu_action = ath10k_ampdu_action,
  3641. CFG80211_TESTMODE_CMD(ath10k_tm_cmd)
  3642. #ifdef CONFIG_PM
  3643. .suspend = ath10k_suspend,
  3644. .resume = ath10k_resume,
  3645. #endif
  3646. };
  3647. #define RATETAB_ENT(_rate, _rateid, _flags) { \
  3648. .bitrate = (_rate), \
  3649. .flags = (_flags), \
  3650. .hw_value = (_rateid), \
  3651. }
  3652. #define CHAN2G(_channel, _freq, _flags) { \
  3653. .band = IEEE80211_BAND_2GHZ, \
  3654. .hw_value = (_channel), \
  3655. .center_freq = (_freq), \
  3656. .flags = (_flags), \
  3657. .max_antenna_gain = 0, \
  3658. .max_power = 30, \
  3659. }
  3660. #define CHAN5G(_channel, _freq, _flags) { \
  3661. .band = IEEE80211_BAND_5GHZ, \
  3662. .hw_value = (_channel), \
  3663. .center_freq = (_freq), \
  3664. .flags = (_flags), \
  3665. .max_antenna_gain = 0, \
  3666. .max_power = 30, \
  3667. }
  3668. static const struct ieee80211_channel ath10k_2ghz_channels[] = {
  3669. CHAN2G(1, 2412, 0),
  3670. CHAN2G(2, 2417, 0),
  3671. CHAN2G(3, 2422, 0),
  3672. CHAN2G(4, 2427, 0),
  3673. CHAN2G(5, 2432, 0),
  3674. CHAN2G(6, 2437, 0),
  3675. CHAN2G(7, 2442, 0),
  3676. CHAN2G(8, 2447, 0),
  3677. CHAN2G(9, 2452, 0),
  3678. CHAN2G(10, 2457, 0),
  3679. CHAN2G(11, 2462, 0),
  3680. CHAN2G(12, 2467, 0),
  3681. CHAN2G(13, 2472, 0),
  3682. CHAN2G(14, 2484, 0),
  3683. };
  3684. static const struct ieee80211_channel ath10k_5ghz_channels[] = {
  3685. CHAN5G(36, 5180, 0),
  3686. CHAN5G(40, 5200, 0),
  3687. CHAN5G(44, 5220, 0),
  3688. CHAN5G(48, 5240, 0),
  3689. CHAN5G(52, 5260, 0),
  3690. CHAN5G(56, 5280, 0),
  3691. CHAN5G(60, 5300, 0),
  3692. CHAN5G(64, 5320, 0),
  3693. CHAN5G(100, 5500, 0),
  3694. CHAN5G(104, 5520, 0),
  3695. CHAN5G(108, 5540, 0),
  3696. CHAN5G(112, 5560, 0),
  3697. CHAN5G(116, 5580, 0),
  3698. CHAN5G(120, 5600, 0),
  3699. CHAN5G(124, 5620, 0),
  3700. CHAN5G(128, 5640, 0),
  3701. CHAN5G(132, 5660, 0),
  3702. CHAN5G(136, 5680, 0),
  3703. CHAN5G(140, 5700, 0),
  3704. CHAN5G(149, 5745, 0),
  3705. CHAN5G(153, 5765, 0),
  3706. CHAN5G(157, 5785, 0),
  3707. CHAN5G(161, 5805, 0),
  3708. CHAN5G(165, 5825, 0),
  3709. };
  3710. static struct ieee80211_rate ath10k_rates[] = {
  3711. /* CCK */
  3712. RATETAB_ENT(10, 0x82, 0),
  3713. RATETAB_ENT(20, 0x84, 0),
  3714. RATETAB_ENT(55, 0x8b, 0),
  3715. RATETAB_ENT(110, 0x96, 0),
  3716. /* OFDM */
  3717. RATETAB_ENT(60, 0x0c, 0),
  3718. RATETAB_ENT(90, 0x12, 0),
  3719. RATETAB_ENT(120, 0x18, 0),
  3720. RATETAB_ENT(180, 0x24, 0),
  3721. RATETAB_ENT(240, 0x30, 0),
  3722. RATETAB_ENT(360, 0x48, 0),
  3723. RATETAB_ENT(480, 0x60, 0),
  3724. RATETAB_ENT(540, 0x6c, 0),
  3725. };
  3726. #define ath10k_a_rates (ath10k_rates + 4)
  3727. #define ath10k_a_rates_size (ARRAY_SIZE(ath10k_rates) - 4)
  3728. #define ath10k_g_rates (ath10k_rates + 0)
  3729. #define ath10k_g_rates_size (ARRAY_SIZE(ath10k_rates))
  3730. struct ath10k *ath10k_mac_create(size_t priv_size)
  3731. {
  3732. struct ieee80211_hw *hw;
  3733. struct ath10k *ar;
  3734. hw = ieee80211_alloc_hw(sizeof(struct ath10k) + priv_size, &ath10k_ops);
  3735. if (!hw)
  3736. return NULL;
  3737. ar = hw->priv;
  3738. ar->hw = hw;
  3739. return ar;
  3740. }
  3741. void ath10k_mac_destroy(struct ath10k *ar)
  3742. {
  3743. ieee80211_free_hw(ar->hw);
  3744. }
  3745. static const struct ieee80211_iface_limit ath10k_if_limits[] = {
  3746. {
  3747. .max = 8,
  3748. .types = BIT(NL80211_IFTYPE_STATION)
  3749. | BIT(NL80211_IFTYPE_P2P_CLIENT)
  3750. },
  3751. {
  3752. .max = 3,
  3753. .types = BIT(NL80211_IFTYPE_P2P_GO)
  3754. },
  3755. {
  3756. .max = 7,
  3757. .types = BIT(NL80211_IFTYPE_AP)
  3758. },
  3759. };
  3760. static const struct ieee80211_iface_limit ath10k_10x_if_limits[] = {
  3761. {
  3762. .max = 8,
  3763. .types = BIT(NL80211_IFTYPE_AP)
  3764. },
  3765. };
  3766. static const struct ieee80211_iface_combination ath10k_if_comb[] = {
  3767. {
  3768. .limits = ath10k_if_limits,
  3769. .n_limits = ARRAY_SIZE(ath10k_if_limits),
  3770. .max_interfaces = 8,
  3771. .num_different_channels = 1,
  3772. .beacon_int_infra_match = true,
  3773. },
  3774. };
  3775. static const struct ieee80211_iface_combination ath10k_10x_if_comb[] = {
  3776. {
  3777. .limits = ath10k_10x_if_limits,
  3778. .n_limits = ARRAY_SIZE(ath10k_10x_if_limits),
  3779. .max_interfaces = 8,
  3780. .num_different_channels = 1,
  3781. .beacon_int_infra_match = true,
  3782. #ifdef CONFIG_ATH10K_DFS_CERTIFIED
  3783. .radar_detect_widths = BIT(NL80211_CHAN_WIDTH_20_NOHT) |
  3784. BIT(NL80211_CHAN_WIDTH_20) |
  3785. BIT(NL80211_CHAN_WIDTH_40) |
  3786. BIT(NL80211_CHAN_WIDTH_80),
  3787. #endif
  3788. },
  3789. };
  3790. static struct ieee80211_sta_vht_cap ath10k_create_vht_cap(struct ath10k *ar)
  3791. {
  3792. struct ieee80211_sta_vht_cap vht_cap = {0};
  3793. u16 mcs_map;
  3794. int i;
  3795. vht_cap.vht_supported = 1;
  3796. vht_cap.cap = ar->vht_cap_info;
  3797. mcs_map = 0;
  3798. for (i = 0; i < 8; i++) {
  3799. if (i < ar->num_rf_chains)
  3800. mcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i*2);
  3801. else
  3802. mcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i*2);
  3803. }
  3804. vht_cap.vht_mcs.rx_mcs_map = cpu_to_le16(mcs_map);
  3805. vht_cap.vht_mcs.tx_mcs_map = cpu_to_le16(mcs_map);
  3806. return vht_cap;
  3807. }
  3808. static struct ieee80211_sta_ht_cap ath10k_get_ht_cap(struct ath10k *ar)
  3809. {
  3810. int i;
  3811. struct ieee80211_sta_ht_cap ht_cap = {0};
  3812. if (!(ar->ht_cap_info & WMI_HT_CAP_ENABLED))
  3813. return ht_cap;
  3814. ht_cap.ht_supported = 1;
  3815. ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
  3816. ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_8;
  3817. ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
  3818. ht_cap.cap |= IEEE80211_HT_CAP_DSSSCCK40;
  3819. ht_cap.cap |= WLAN_HT_CAP_SM_PS_STATIC << IEEE80211_HT_CAP_SM_PS_SHIFT;
  3820. if (ar->ht_cap_info & WMI_HT_CAP_HT20_SGI)
  3821. ht_cap.cap |= IEEE80211_HT_CAP_SGI_20;
  3822. if (ar->ht_cap_info & WMI_HT_CAP_HT40_SGI)
  3823. ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
  3824. if (ar->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS) {
  3825. u32 smps;
  3826. smps = WLAN_HT_CAP_SM_PS_DYNAMIC;
  3827. smps <<= IEEE80211_HT_CAP_SM_PS_SHIFT;
  3828. ht_cap.cap |= smps;
  3829. }
  3830. if (ar->ht_cap_info & WMI_HT_CAP_TX_STBC)
  3831. ht_cap.cap |= IEEE80211_HT_CAP_TX_STBC;
  3832. if (ar->ht_cap_info & WMI_HT_CAP_RX_STBC) {
  3833. u32 stbc;
  3834. stbc = ar->ht_cap_info;
  3835. stbc &= WMI_HT_CAP_RX_STBC;
  3836. stbc >>= WMI_HT_CAP_RX_STBC_MASK_SHIFT;
  3837. stbc <<= IEEE80211_HT_CAP_RX_STBC_SHIFT;
  3838. stbc &= IEEE80211_HT_CAP_RX_STBC;
  3839. ht_cap.cap |= stbc;
  3840. }
  3841. if (ar->ht_cap_info & WMI_HT_CAP_LDPC)
  3842. ht_cap.cap |= IEEE80211_HT_CAP_LDPC_CODING;
  3843. if (ar->ht_cap_info & WMI_HT_CAP_L_SIG_TXOP_PROT)
  3844. ht_cap.cap |= IEEE80211_HT_CAP_LSIG_TXOP_PROT;
  3845. /* max AMSDU is implicitly taken from vht_cap_info */
  3846. if (ar->vht_cap_info & WMI_VHT_CAP_MAX_MPDU_LEN_MASK)
  3847. ht_cap.cap |= IEEE80211_HT_CAP_MAX_AMSDU;
  3848. for (i = 0; i < ar->num_rf_chains; i++)
  3849. ht_cap.mcs.rx_mask[i] = 0xFF;
  3850. ht_cap.mcs.tx_params |= IEEE80211_HT_MCS_TX_DEFINED;
  3851. return ht_cap;
  3852. }
  3853. static void ath10k_get_arvif_iter(void *data, u8 *mac,
  3854. struct ieee80211_vif *vif)
  3855. {
  3856. struct ath10k_vif_iter *arvif_iter = data;
  3857. struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
  3858. if (arvif->vdev_id == arvif_iter->vdev_id)
  3859. arvif_iter->arvif = arvif;
  3860. }
  3861. struct ath10k_vif *ath10k_get_arvif(struct ath10k *ar, u32 vdev_id)
  3862. {
  3863. struct ath10k_vif_iter arvif_iter;
  3864. u32 flags;
  3865. memset(&arvif_iter, 0, sizeof(struct ath10k_vif_iter));
  3866. arvif_iter.vdev_id = vdev_id;
  3867. flags = IEEE80211_IFACE_ITER_RESUME_ALL;
  3868. ieee80211_iterate_active_interfaces_atomic(ar->hw,
  3869. flags,
  3870. ath10k_get_arvif_iter,
  3871. &arvif_iter);
  3872. if (!arvif_iter.arvif) {
  3873. ath10k_warn(ar, "No VIF found for vdev %d\n", vdev_id);
  3874. return NULL;
  3875. }
  3876. return arvif_iter.arvif;
  3877. }
  3878. int ath10k_mac_register(struct ath10k *ar)
  3879. {
  3880. struct ieee80211_supported_band *band;
  3881. struct ieee80211_sta_vht_cap vht_cap;
  3882. struct ieee80211_sta_ht_cap ht_cap;
  3883. void *channels;
  3884. int ret;
  3885. SET_IEEE80211_PERM_ADDR(ar->hw, ar->mac_addr);
  3886. SET_IEEE80211_DEV(ar->hw, ar->dev);
  3887. ht_cap = ath10k_get_ht_cap(ar);
  3888. vht_cap = ath10k_create_vht_cap(ar);
  3889. if (ar->phy_capability & WHAL_WLAN_11G_CAPABILITY) {
  3890. channels = kmemdup(ath10k_2ghz_channels,
  3891. sizeof(ath10k_2ghz_channels),
  3892. GFP_KERNEL);
  3893. if (!channels) {
  3894. ret = -ENOMEM;
  3895. goto err_free;
  3896. }
  3897. band = &ar->mac.sbands[IEEE80211_BAND_2GHZ];
  3898. band->n_channels = ARRAY_SIZE(ath10k_2ghz_channels);
  3899. band->channels = channels;
  3900. band->n_bitrates = ath10k_g_rates_size;
  3901. band->bitrates = ath10k_g_rates;
  3902. band->ht_cap = ht_cap;
  3903. /* vht is not supported in 2.4 GHz */
  3904. ar->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = band;
  3905. }
  3906. if (ar->phy_capability & WHAL_WLAN_11A_CAPABILITY) {
  3907. channels = kmemdup(ath10k_5ghz_channels,
  3908. sizeof(ath10k_5ghz_channels),
  3909. GFP_KERNEL);
  3910. if (!channels) {
  3911. ret = -ENOMEM;
  3912. goto err_free;
  3913. }
  3914. band = &ar->mac.sbands[IEEE80211_BAND_5GHZ];
  3915. band->n_channels = ARRAY_SIZE(ath10k_5ghz_channels);
  3916. band->channels = channels;
  3917. band->n_bitrates = ath10k_a_rates_size;
  3918. band->bitrates = ath10k_a_rates;
  3919. band->ht_cap = ht_cap;
  3920. band->vht_cap = vht_cap;
  3921. ar->hw->wiphy->bands[IEEE80211_BAND_5GHZ] = band;
  3922. }
  3923. ar->hw->wiphy->interface_modes =
  3924. BIT(NL80211_IFTYPE_STATION) |
  3925. BIT(NL80211_IFTYPE_AP);
  3926. if (test_bit(ATH10K_FW_FEATURE_WMI_10X, ar->fw_features)) {
  3927. /* TODO: Have to deal with 2x2 chips if/when the come out. */
  3928. ar->supp_tx_chainmask = TARGET_10X_TX_CHAIN_MASK;
  3929. ar->supp_rx_chainmask = TARGET_10X_RX_CHAIN_MASK;
  3930. } else {
  3931. ar->supp_tx_chainmask = TARGET_TX_CHAIN_MASK;
  3932. ar->supp_rx_chainmask = TARGET_RX_CHAIN_MASK;
  3933. }
  3934. ar->hw->wiphy->available_antennas_rx = ar->supp_rx_chainmask;
  3935. ar->hw->wiphy->available_antennas_tx = ar->supp_tx_chainmask;
  3936. if (!test_bit(ATH10K_FW_FEATURE_NO_P2P, ar->fw_features))
  3937. ar->hw->wiphy->interface_modes |=
  3938. BIT(NL80211_IFTYPE_P2P_CLIENT) |
  3939. BIT(NL80211_IFTYPE_P2P_GO);
  3940. ar->hw->flags = IEEE80211_HW_SIGNAL_DBM |
  3941. IEEE80211_HW_SUPPORTS_PS |
  3942. IEEE80211_HW_SUPPORTS_DYNAMIC_PS |
  3943. IEEE80211_HW_SUPPORTS_UAPSD |
  3944. IEEE80211_HW_MFP_CAPABLE |
  3945. IEEE80211_HW_REPORTS_TX_ACK_STATUS |
  3946. IEEE80211_HW_HAS_RATE_CONTROL |
  3947. IEEE80211_HW_AP_LINK_PS |
  3948. IEEE80211_HW_SPECTRUM_MGMT;
  3949. /* MSDU can have HTT TX fragment pushed in front. The additional 4
  3950. * bytes is used for padding/alignment if necessary. */
  3951. ar->hw->extra_tx_headroom += sizeof(struct htt_data_tx_desc_frag)*2 + 4;
  3952. ar->hw->wiphy->features |= NL80211_FEATURE_STATIC_SMPS;
  3953. if (ar->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS)
  3954. ar->hw->wiphy->features |= NL80211_FEATURE_DYNAMIC_SMPS;
  3955. if (ar->ht_cap_info & WMI_HT_CAP_ENABLED) {
  3956. ar->hw->flags |= IEEE80211_HW_AMPDU_AGGREGATION;
  3957. ar->hw->flags |= IEEE80211_HW_TX_AMPDU_SETUP_IN_HW;
  3958. }
  3959. ar->hw->wiphy->max_scan_ssids = WLAN_SCAN_PARAMS_MAX_SSID;
  3960. ar->hw->wiphy->max_scan_ie_len = WLAN_SCAN_PARAMS_MAX_IE_LEN;
  3961. ar->hw->vif_data_size = sizeof(struct ath10k_vif);
  3962. ar->hw->sta_data_size = sizeof(struct ath10k_sta);
  3963. ar->hw->max_listen_interval = ATH10K_MAX_HW_LISTEN_INTERVAL;
  3964. ar->hw->wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
  3965. ar->hw->wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH;
  3966. ar->hw->wiphy->max_remain_on_channel_duration = 5000;
  3967. ar->hw->wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
  3968. /*
  3969. * on LL hardware queues are managed entirely by the FW
  3970. * so we only advertise to mac we can do the queues thing
  3971. */
  3972. ar->hw->queues = 4;
  3973. if (test_bit(ATH10K_FW_FEATURE_WMI_10X, ar->fw_features)) {
  3974. ar->hw->wiphy->iface_combinations = ath10k_10x_if_comb;
  3975. ar->hw->wiphy->n_iface_combinations =
  3976. ARRAY_SIZE(ath10k_10x_if_comb);
  3977. } else {
  3978. ar->hw->wiphy->iface_combinations = ath10k_if_comb;
  3979. ar->hw->wiphy->n_iface_combinations =
  3980. ARRAY_SIZE(ath10k_if_comb);
  3981. ar->hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_ADHOC);
  3982. }
  3983. ar->hw->netdev_features = NETIF_F_HW_CSUM;
  3984. if (config_enabled(CONFIG_ATH10K_DFS_CERTIFIED)) {
  3985. /* Init ath dfs pattern detector */
  3986. ar->ath_common.debug_mask = ATH_DBG_DFS;
  3987. ar->dfs_detector = dfs_pattern_detector_init(&ar->ath_common,
  3988. NL80211_DFS_UNSET);
  3989. if (!ar->dfs_detector)
  3990. ath10k_warn(ar, "failed to initialise DFS pattern detector\n");
  3991. }
  3992. ret = ath_regd_init(&ar->ath_common.regulatory, ar->hw->wiphy,
  3993. ath10k_reg_notifier);
  3994. if (ret) {
  3995. ath10k_err(ar, "failed to initialise regulatory: %i\n", ret);
  3996. goto err_free;
  3997. }
  3998. ret = ieee80211_register_hw(ar->hw);
  3999. if (ret) {
  4000. ath10k_err(ar, "failed to register ieee80211: %d\n", ret);
  4001. goto err_free;
  4002. }
  4003. if (!ath_is_world_regd(&ar->ath_common.regulatory)) {
  4004. ret = regulatory_hint(ar->hw->wiphy,
  4005. ar->ath_common.regulatory.alpha2);
  4006. if (ret)
  4007. goto err_unregister;
  4008. }
  4009. return 0;
  4010. err_unregister:
  4011. ieee80211_unregister_hw(ar->hw);
  4012. err_free:
  4013. kfree(ar->mac.sbands[IEEE80211_BAND_2GHZ].channels);
  4014. kfree(ar->mac.sbands[IEEE80211_BAND_5GHZ].channels);
  4015. return ret;
  4016. }
  4017. void ath10k_mac_unregister(struct ath10k *ar)
  4018. {
  4019. ieee80211_unregister_hw(ar->hw);
  4020. if (config_enabled(CONFIG_ATH10K_DFS_CERTIFIED) && ar->dfs_detector)
  4021. ar->dfs_detector->exit(ar->dfs_detector);
  4022. kfree(ar->mac.sbands[IEEE80211_BAND_2GHZ].channels);
  4023. kfree(ar->mac.sbands[IEEE80211_BAND_5GHZ].channels);
  4024. SET_IEEE80211_DEV(ar->hw, NULL);
  4025. }