mac.c 138 KB

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