mac.c 117 KB

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