mac.c 185 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395
  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-tlv.h"
  29. #include "wmi-ops.h"
  30. #include "wow.h"
  31. /*********/
  32. /* Rates */
  33. /*********/
  34. static struct ieee80211_rate ath10k_rates[] = {
  35. { .bitrate = 10,
  36. .hw_value = ATH10K_HW_RATE_CCK_LP_1M },
  37. { .bitrate = 20,
  38. .hw_value = ATH10K_HW_RATE_CCK_LP_2M,
  39. .hw_value_short = ATH10K_HW_RATE_CCK_SP_2M,
  40. .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  41. { .bitrate = 55,
  42. .hw_value = ATH10K_HW_RATE_CCK_LP_5_5M,
  43. .hw_value_short = ATH10K_HW_RATE_CCK_SP_5_5M,
  44. .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  45. { .bitrate = 110,
  46. .hw_value = ATH10K_HW_RATE_CCK_LP_11M,
  47. .hw_value_short = ATH10K_HW_RATE_CCK_SP_11M,
  48. .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  49. { .bitrate = 60, .hw_value = ATH10K_HW_RATE_OFDM_6M },
  50. { .bitrate = 90, .hw_value = ATH10K_HW_RATE_OFDM_9M },
  51. { .bitrate = 120, .hw_value = ATH10K_HW_RATE_OFDM_12M },
  52. { .bitrate = 180, .hw_value = ATH10K_HW_RATE_OFDM_18M },
  53. { .bitrate = 240, .hw_value = ATH10K_HW_RATE_OFDM_24M },
  54. { .bitrate = 360, .hw_value = ATH10K_HW_RATE_OFDM_36M },
  55. { .bitrate = 480, .hw_value = ATH10K_HW_RATE_OFDM_48M },
  56. { .bitrate = 540, .hw_value = ATH10K_HW_RATE_OFDM_54M },
  57. };
  58. #define ATH10K_MAC_FIRST_OFDM_RATE_IDX 4
  59. #define ath10k_a_rates (ath10k_rates + ATH10K_MAC_FIRST_OFDM_RATE_IDX)
  60. #define ath10k_a_rates_size (ARRAY_SIZE(ath10k_rates) - \
  61. ATH10K_MAC_FIRST_OFDM_RATE_IDX)
  62. #define ath10k_g_rates (ath10k_rates + 0)
  63. #define ath10k_g_rates_size (ARRAY_SIZE(ath10k_rates))
  64. static bool ath10k_mac_bitrate_is_cck(int bitrate)
  65. {
  66. switch (bitrate) {
  67. case 10:
  68. case 20:
  69. case 55:
  70. case 110:
  71. return true;
  72. }
  73. return false;
  74. }
  75. static u8 ath10k_mac_bitrate_to_rate(int bitrate)
  76. {
  77. return DIV_ROUND_UP(bitrate, 5) |
  78. (ath10k_mac_bitrate_is_cck(bitrate) ? BIT(7) : 0);
  79. }
  80. u8 ath10k_mac_hw_rate_to_idx(const struct ieee80211_supported_band *sband,
  81. u8 hw_rate, bool cck)
  82. {
  83. const struct ieee80211_rate *rate;
  84. int i;
  85. for (i = 0; i < sband->n_bitrates; i++) {
  86. rate = &sband->bitrates[i];
  87. if (ath10k_mac_bitrate_is_cck(rate->bitrate) != cck)
  88. continue;
  89. if (rate->hw_value == hw_rate)
  90. return i;
  91. else if (rate->flags & IEEE80211_RATE_SHORT_PREAMBLE &&
  92. rate->hw_value_short == hw_rate)
  93. return i;
  94. }
  95. return 0;
  96. }
  97. u8 ath10k_mac_bitrate_to_idx(const struct ieee80211_supported_band *sband,
  98. u32 bitrate)
  99. {
  100. int i;
  101. for (i = 0; i < sband->n_bitrates; i++)
  102. if (sband->bitrates[i].bitrate == bitrate)
  103. return i;
  104. return 0;
  105. }
  106. static int ath10k_mac_get_max_vht_mcs_map(u16 mcs_map, int nss)
  107. {
  108. switch ((mcs_map >> (2 * nss)) & 0x3) {
  109. case IEEE80211_VHT_MCS_SUPPORT_0_7: return BIT(8) - 1;
  110. case IEEE80211_VHT_MCS_SUPPORT_0_8: return BIT(9) - 1;
  111. case IEEE80211_VHT_MCS_SUPPORT_0_9: return BIT(10) - 1;
  112. }
  113. return 0;
  114. }
  115. static u32
  116. ath10k_mac_max_ht_nss(const u8 ht_mcs_mask[IEEE80211_HT_MCS_MASK_LEN])
  117. {
  118. int nss;
  119. for (nss = IEEE80211_HT_MCS_MASK_LEN - 1; nss >= 0; nss--)
  120. if (ht_mcs_mask[nss])
  121. return nss + 1;
  122. return 1;
  123. }
  124. static u32
  125. ath10k_mac_max_vht_nss(const u16 vht_mcs_mask[NL80211_VHT_NSS_MAX])
  126. {
  127. int nss;
  128. for (nss = NL80211_VHT_NSS_MAX - 1; nss >= 0; nss--)
  129. if (vht_mcs_mask[nss])
  130. return nss + 1;
  131. return 1;
  132. }
  133. /**********/
  134. /* Crypto */
  135. /**********/
  136. static int ath10k_send_key(struct ath10k_vif *arvif,
  137. struct ieee80211_key_conf *key,
  138. enum set_key_cmd cmd,
  139. const u8 *macaddr, u32 flags)
  140. {
  141. struct ath10k *ar = arvif->ar;
  142. struct wmi_vdev_install_key_arg arg = {
  143. .vdev_id = arvif->vdev_id,
  144. .key_idx = key->keyidx,
  145. .key_len = key->keylen,
  146. .key_data = key->key,
  147. .key_flags = flags,
  148. .macaddr = macaddr,
  149. };
  150. lockdep_assert_held(&arvif->ar->conf_mutex);
  151. switch (key->cipher) {
  152. case WLAN_CIPHER_SUITE_CCMP:
  153. arg.key_cipher = WMI_CIPHER_AES_CCM;
  154. key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV_MGMT;
  155. break;
  156. case WLAN_CIPHER_SUITE_TKIP:
  157. arg.key_cipher = WMI_CIPHER_TKIP;
  158. arg.key_txmic_len = 8;
  159. arg.key_rxmic_len = 8;
  160. break;
  161. case WLAN_CIPHER_SUITE_WEP40:
  162. case WLAN_CIPHER_SUITE_WEP104:
  163. arg.key_cipher = WMI_CIPHER_WEP;
  164. break;
  165. case WLAN_CIPHER_SUITE_AES_CMAC:
  166. WARN_ON(1);
  167. return -EINVAL;
  168. default:
  169. ath10k_warn(ar, "cipher %d is not supported\n", key->cipher);
  170. return -EOPNOTSUPP;
  171. }
  172. if (test_bit(ATH10K_FLAG_RAW_MODE, &ar->dev_flags))
  173. key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  174. if (cmd == DISABLE_KEY) {
  175. arg.key_cipher = WMI_CIPHER_NONE;
  176. arg.key_data = NULL;
  177. }
  178. return ath10k_wmi_vdev_install_key(arvif->ar, &arg);
  179. }
  180. static int ath10k_install_key(struct ath10k_vif *arvif,
  181. struct ieee80211_key_conf *key,
  182. enum set_key_cmd cmd,
  183. const u8 *macaddr, u32 flags)
  184. {
  185. struct ath10k *ar = arvif->ar;
  186. int ret;
  187. unsigned long time_left;
  188. lockdep_assert_held(&ar->conf_mutex);
  189. reinit_completion(&ar->install_key_done);
  190. if (arvif->nohwcrypt)
  191. return 1;
  192. ret = ath10k_send_key(arvif, key, cmd, macaddr, flags);
  193. if (ret)
  194. return ret;
  195. time_left = wait_for_completion_timeout(&ar->install_key_done, 3 * HZ);
  196. if (time_left == 0)
  197. return -ETIMEDOUT;
  198. return 0;
  199. }
  200. static int ath10k_install_peer_wep_keys(struct ath10k_vif *arvif,
  201. const u8 *addr)
  202. {
  203. struct ath10k *ar = arvif->ar;
  204. struct ath10k_peer *peer;
  205. int ret;
  206. int i;
  207. u32 flags;
  208. lockdep_assert_held(&ar->conf_mutex);
  209. if (WARN_ON(arvif->vif->type != NL80211_IFTYPE_AP &&
  210. arvif->vif->type != NL80211_IFTYPE_ADHOC))
  211. return -EINVAL;
  212. spin_lock_bh(&ar->data_lock);
  213. peer = ath10k_peer_find(ar, arvif->vdev_id, addr);
  214. spin_unlock_bh(&ar->data_lock);
  215. if (!peer)
  216. return -ENOENT;
  217. for (i = 0; i < ARRAY_SIZE(arvif->wep_keys); i++) {
  218. if (arvif->wep_keys[i] == NULL)
  219. continue;
  220. switch (arvif->vif->type) {
  221. case NL80211_IFTYPE_AP:
  222. flags = WMI_KEY_PAIRWISE;
  223. if (arvif->def_wep_key_idx == i)
  224. flags |= WMI_KEY_TX_USAGE;
  225. ret = ath10k_install_key(arvif, arvif->wep_keys[i],
  226. SET_KEY, addr, flags);
  227. if (ret < 0)
  228. return ret;
  229. break;
  230. case NL80211_IFTYPE_ADHOC:
  231. ret = ath10k_install_key(arvif, arvif->wep_keys[i],
  232. SET_KEY, addr,
  233. WMI_KEY_PAIRWISE);
  234. if (ret < 0)
  235. return ret;
  236. ret = ath10k_install_key(arvif, arvif->wep_keys[i],
  237. SET_KEY, addr, WMI_KEY_GROUP);
  238. if (ret < 0)
  239. return ret;
  240. break;
  241. default:
  242. WARN_ON(1);
  243. return -EINVAL;
  244. }
  245. spin_lock_bh(&ar->data_lock);
  246. peer->keys[i] = arvif->wep_keys[i];
  247. spin_unlock_bh(&ar->data_lock);
  248. }
  249. /* In some cases (notably with static WEP IBSS with multiple keys)
  250. * multicast Tx becomes broken. Both pairwise and groupwise keys are
  251. * installed already. Using WMI_KEY_TX_USAGE in different combinations
  252. * didn't seem help. Using def_keyid vdev parameter seems to be
  253. * effective so use that.
  254. *
  255. * FIXME: Revisit. Perhaps this can be done in a less hacky way.
  256. */
  257. if (arvif->vif->type != NL80211_IFTYPE_ADHOC)
  258. return 0;
  259. if (arvif->def_wep_key_idx == -1)
  260. return 0;
  261. ret = ath10k_wmi_vdev_set_param(arvif->ar,
  262. arvif->vdev_id,
  263. arvif->ar->wmi.vdev_param->def_keyid,
  264. arvif->def_wep_key_idx);
  265. if (ret) {
  266. ath10k_warn(ar, "failed to re-set def wpa key idxon vdev %i: %d\n",
  267. arvif->vdev_id, ret);
  268. return ret;
  269. }
  270. return 0;
  271. }
  272. static int ath10k_clear_peer_keys(struct ath10k_vif *arvif,
  273. const u8 *addr)
  274. {
  275. struct ath10k *ar = arvif->ar;
  276. struct ath10k_peer *peer;
  277. int first_errno = 0;
  278. int ret;
  279. int i;
  280. u32 flags = 0;
  281. lockdep_assert_held(&ar->conf_mutex);
  282. spin_lock_bh(&ar->data_lock);
  283. peer = ath10k_peer_find(ar, arvif->vdev_id, addr);
  284. spin_unlock_bh(&ar->data_lock);
  285. if (!peer)
  286. return -ENOENT;
  287. for (i = 0; i < ARRAY_SIZE(peer->keys); i++) {
  288. if (peer->keys[i] == NULL)
  289. continue;
  290. /* key flags are not required to delete the key */
  291. ret = ath10k_install_key(arvif, peer->keys[i],
  292. DISABLE_KEY, addr, flags);
  293. if (ret < 0 && first_errno == 0)
  294. first_errno = ret;
  295. if (ret < 0)
  296. ath10k_warn(ar, "failed to remove peer wep key %d: %d\n",
  297. i, ret);
  298. spin_lock_bh(&ar->data_lock);
  299. peer->keys[i] = NULL;
  300. spin_unlock_bh(&ar->data_lock);
  301. }
  302. return first_errno;
  303. }
  304. bool ath10k_mac_is_peer_wep_key_set(struct ath10k *ar, const u8 *addr,
  305. u8 keyidx)
  306. {
  307. struct ath10k_peer *peer;
  308. int i;
  309. lockdep_assert_held(&ar->data_lock);
  310. /* We don't know which vdev this peer belongs to,
  311. * since WMI doesn't give us that information.
  312. *
  313. * FIXME: multi-bss needs to be handled.
  314. */
  315. peer = ath10k_peer_find(ar, 0, addr);
  316. if (!peer)
  317. return false;
  318. for (i = 0; i < ARRAY_SIZE(peer->keys); i++) {
  319. if (peer->keys[i] && peer->keys[i]->keyidx == keyidx)
  320. return true;
  321. }
  322. return false;
  323. }
  324. static int ath10k_clear_vdev_key(struct ath10k_vif *arvif,
  325. struct ieee80211_key_conf *key)
  326. {
  327. struct ath10k *ar = arvif->ar;
  328. struct ath10k_peer *peer;
  329. u8 addr[ETH_ALEN];
  330. int first_errno = 0;
  331. int ret;
  332. int i;
  333. u32 flags = 0;
  334. lockdep_assert_held(&ar->conf_mutex);
  335. for (;;) {
  336. /* since ath10k_install_key we can't hold data_lock all the
  337. * time, so we try to remove the keys incrementally */
  338. spin_lock_bh(&ar->data_lock);
  339. i = 0;
  340. list_for_each_entry(peer, &ar->peers, list) {
  341. for (i = 0; i < ARRAY_SIZE(peer->keys); i++) {
  342. if (peer->keys[i] == key) {
  343. ether_addr_copy(addr, peer->addr);
  344. peer->keys[i] = NULL;
  345. break;
  346. }
  347. }
  348. if (i < ARRAY_SIZE(peer->keys))
  349. break;
  350. }
  351. spin_unlock_bh(&ar->data_lock);
  352. if (i == ARRAY_SIZE(peer->keys))
  353. break;
  354. /* key flags are not required to delete the key */
  355. ret = ath10k_install_key(arvif, key, DISABLE_KEY, addr, flags);
  356. if (ret < 0 && first_errno == 0)
  357. first_errno = ret;
  358. if (ret)
  359. ath10k_warn(ar, "failed to remove key for %pM: %d\n",
  360. addr, ret);
  361. }
  362. return first_errno;
  363. }
  364. static int ath10k_mac_vif_update_wep_key(struct ath10k_vif *arvif,
  365. struct ieee80211_key_conf *key)
  366. {
  367. struct ath10k *ar = arvif->ar;
  368. struct ath10k_peer *peer;
  369. int ret;
  370. lockdep_assert_held(&ar->conf_mutex);
  371. list_for_each_entry(peer, &ar->peers, list) {
  372. if (!memcmp(peer->addr, arvif->vif->addr, ETH_ALEN))
  373. continue;
  374. if (!memcmp(peer->addr, arvif->bssid, ETH_ALEN))
  375. continue;
  376. if (peer->keys[key->keyidx] == key)
  377. continue;
  378. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vif vdev %i update key %i needs update\n",
  379. arvif->vdev_id, key->keyidx);
  380. ret = ath10k_install_peer_wep_keys(arvif, peer->addr);
  381. if (ret) {
  382. ath10k_warn(ar, "failed to update wep keys on vdev %i for peer %pM: %d\n",
  383. arvif->vdev_id, peer->addr, ret);
  384. return ret;
  385. }
  386. }
  387. return 0;
  388. }
  389. /*********************/
  390. /* General utilities */
  391. /*********************/
  392. static inline enum wmi_phy_mode
  393. chan_to_phymode(const struct cfg80211_chan_def *chandef)
  394. {
  395. enum wmi_phy_mode phymode = MODE_UNKNOWN;
  396. switch (chandef->chan->band) {
  397. case IEEE80211_BAND_2GHZ:
  398. switch (chandef->width) {
  399. case NL80211_CHAN_WIDTH_20_NOHT:
  400. if (chandef->chan->flags & IEEE80211_CHAN_NO_OFDM)
  401. phymode = MODE_11B;
  402. else
  403. phymode = MODE_11G;
  404. break;
  405. case NL80211_CHAN_WIDTH_20:
  406. phymode = MODE_11NG_HT20;
  407. break;
  408. case NL80211_CHAN_WIDTH_40:
  409. phymode = MODE_11NG_HT40;
  410. break;
  411. case NL80211_CHAN_WIDTH_5:
  412. case NL80211_CHAN_WIDTH_10:
  413. case NL80211_CHAN_WIDTH_80:
  414. case NL80211_CHAN_WIDTH_80P80:
  415. case NL80211_CHAN_WIDTH_160:
  416. phymode = MODE_UNKNOWN;
  417. break;
  418. }
  419. break;
  420. case IEEE80211_BAND_5GHZ:
  421. switch (chandef->width) {
  422. case NL80211_CHAN_WIDTH_20_NOHT:
  423. phymode = MODE_11A;
  424. break;
  425. case NL80211_CHAN_WIDTH_20:
  426. phymode = MODE_11NA_HT20;
  427. break;
  428. case NL80211_CHAN_WIDTH_40:
  429. phymode = MODE_11NA_HT40;
  430. break;
  431. case NL80211_CHAN_WIDTH_80:
  432. phymode = MODE_11AC_VHT80;
  433. break;
  434. case NL80211_CHAN_WIDTH_5:
  435. case NL80211_CHAN_WIDTH_10:
  436. case NL80211_CHAN_WIDTH_80P80:
  437. case NL80211_CHAN_WIDTH_160:
  438. phymode = MODE_UNKNOWN;
  439. break;
  440. }
  441. break;
  442. default:
  443. break;
  444. }
  445. WARN_ON(phymode == MODE_UNKNOWN);
  446. return phymode;
  447. }
  448. static u8 ath10k_parse_mpdudensity(u8 mpdudensity)
  449. {
  450. /*
  451. * 802.11n D2.0 defined values for "Minimum MPDU Start Spacing":
  452. * 0 for no restriction
  453. * 1 for 1/4 us
  454. * 2 for 1/2 us
  455. * 3 for 1 us
  456. * 4 for 2 us
  457. * 5 for 4 us
  458. * 6 for 8 us
  459. * 7 for 16 us
  460. */
  461. switch (mpdudensity) {
  462. case 0:
  463. return 0;
  464. case 1:
  465. case 2:
  466. case 3:
  467. /* Our lower layer calculations limit our precision to
  468. 1 microsecond */
  469. return 1;
  470. case 4:
  471. return 2;
  472. case 5:
  473. return 4;
  474. case 6:
  475. return 8;
  476. case 7:
  477. return 16;
  478. default:
  479. return 0;
  480. }
  481. }
  482. int ath10k_mac_vif_chan(struct ieee80211_vif *vif,
  483. struct cfg80211_chan_def *def)
  484. {
  485. struct ieee80211_chanctx_conf *conf;
  486. rcu_read_lock();
  487. conf = rcu_dereference(vif->chanctx_conf);
  488. if (!conf) {
  489. rcu_read_unlock();
  490. return -ENOENT;
  491. }
  492. *def = conf->def;
  493. rcu_read_unlock();
  494. return 0;
  495. }
  496. static void ath10k_mac_num_chanctxs_iter(struct ieee80211_hw *hw,
  497. struct ieee80211_chanctx_conf *conf,
  498. void *data)
  499. {
  500. int *num = data;
  501. (*num)++;
  502. }
  503. static int ath10k_mac_num_chanctxs(struct ath10k *ar)
  504. {
  505. int num = 0;
  506. ieee80211_iter_chan_contexts_atomic(ar->hw,
  507. ath10k_mac_num_chanctxs_iter,
  508. &num);
  509. return num;
  510. }
  511. static void
  512. ath10k_mac_get_any_chandef_iter(struct ieee80211_hw *hw,
  513. struct ieee80211_chanctx_conf *conf,
  514. void *data)
  515. {
  516. struct cfg80211_chan_def **def = data;
  517. *def = &conf->def;
  518. }
  519. static int ath10k_peer_create(struct ath10k *ar, u32 vdev_id, const u8 *addr,
  520. enum wmi_peer_type peer_type)
  521. {
  522. struct ath10k_vif *arvif;
  523. int num_peers = 0;
  524. int ret;
  525. lockdep_assert_held(&ar->conf_mutex);
  526. num_peers = ar->num_peers;
  527. /* Each vdev consumes a peer entry as well */
  528. list_for_each_entry(arvif, &ar->arvifs, list)
  529. num_peers++;
  530. if (num_peers >= ar->max_num_peers)
  531. return -ENOBUFS;
  532. ret = ath10k_wmi_peer_create(ar, vdev_id, addr, peer_type);
  533. if (ret) {
  534. ath10k_warn(ar, "failed to create wmi peer %pM on vdev %i: %i\n",
  535. addr, vdev_id, ret);
  536. return ret;
  537. }
  538. ret = ath10k_wait_for_peer_created(ar, vdev_id, addr);
  539. if (ret) {
  540. ath10k_warn(ar, "failed to wait for created wmi peer %pM on vdev %i: %i\n",
  541. addr, vdev_id, ret);
  542. return ret;
  543. }
  544. ar->num_peers++;
  545. return 0;
  546. }
  547. static int ath10k_mac_set_kickout(struct ath10k_vif *arvif)
  548. {
  549. struct ath10k *ar = arvif->ar;
  550. u32 param;
  551. int ret;
  552. param = ar->wmi.pdev_param->sta_kickout_th;
  553. ret = ath10k_wmi_pdev_set_param(ar, param,
  554. ATH10K_KICKOUT_THRESHOLD);
  555. if (ret) {
  556. ath10k_warn(ar, "failed to set kickout threshold on vdev %i: %d\n",
  557. arvif->vdev_id, ret);
  558. return ret;
  559. }
  560. param = ar->wmi.vdev_param->ap_keepalive_min_idle_inactive_time_secs;
  561. ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, param,
  562. ATH10K_KEEPALIVE_MIN_IDLE);
  563. if (ret) {
  564. ath10k_warn(ar, "failed to set keepalive minimum idle time on vdev %i: %d\n",
  565. arvif->vdev_id, ret);
  566. return ret;
  567. }
  568. param = ar->wmi.vdev_param->ap_keepalive_max_idle_inactive_time_secs;
  569. ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, param,
  570. ATH10K_KEEPALIVE_MAX_IDLE);
  571. if (ret) {
  572. ath10k_warn(ar, "failed to set keepalive maximum idle time on vdev %i: %d\n",
  573. arvif->vdev_id, ret);
  574. return ret;
  575. }
  576. param = ar->wmi.vdev_param->ap_keepalive_max_unresponsive_time_secs;
  577. ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, param,
  578. ATH10K_KEEPALIVE_MAX_UNRESPONSIVE);
  579. if (ret) {
  580. ath10k_warn(ar, "failed to set keepalive maximum unresponsive time on vdev %i: %d\n",
  581. arvif->vdev_id, ret);
  582. return ret;
  583. }
  584. return 0;
  585. }
  586. static int ath10k_mac_set_rts(struct ath10k_vif *arvif, u32 value)
  587. {
  588. struct ath10k *ar = arvif->ar;
  589. u32 vdev_param;
  590. vdev_param = ar->wmi.vdev_param->rts_threshold;
  591. return ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param, value);
  592. }
  593. static int ath10k_peer_delete(struct ath10k *ar, u32 vdev_id, const u8 *addr)
  594. {
  595. int ret;
  596. lockdep_assert_held(&ar->conf_mutex);
  597. ret = ath10k_wmi_peer_delete(ar, vdev_id, addr);
  598. if (ret)
  599. return ret;
  600. ret = ath10k_wait_for_peer_deleted(ar, vdev_id, addr);
  601. if (ret)
  602. return ret;
  603. ar->num_peers--;
  604. return 0;
  605. }
  606. static void ath10k_peer_cleanup(struct ath10k *ar, u32 vdev_id)
  607. {
  608. struct ath10k_peer *peer, *tmp;
  609. lockdep_assert_held(&ar->conf_mutex);
  610. spin_lock_bh(&ar->data_lock);
  611. list_for_each_entry_safe(peer, tmp, &ar->peers, list) {
  612. if (peer->vdev_id != vdev_id)
  613. continue;
  614. ath10k_warn(ar, "removing stale peer %pM from vdev_id %d\n",
  615. peer->addr, vdev_id);
  616. list_del(&peer->list);
  617. kfree(peer);
  618. ar->num_peers--;
  619. }
  620. spin_unlock_bh(&ar->data_lock);
  621. }
  622. static void ath10k_peer_cleanup_all(struct ath10k *ar)
  623. {
  624. struct ath10k_peer *peer, *tmp;
  625. lockdep_assert_held(&ar->conf_mutex);
  626. spin_lock_bh(&ar->data_lock);
  627. list_for_each_entry_safe(peer, tmp, &ar->peers, list) {
  628. list_del(&peer->list);
  629. kfree(peer);
  630. }
  631. spin_unlock_bh(&ar->data_lock);
  632. ar->num_peers = 0;
  633. ar->num_stations = 0;
  634. }
  635. static int ath10k_mac_tdls_peer_update(struct ath10k *ar, u32 vdev_id,
  636. struct ieee80211_sta *sta,
  637. enum wmi_tdls_peer_state state)
  638. {
  639. int ret;
  640. struct wmi_tdls_peer_update_cmd_arg arg = {};
  641. struct wmi_tdls_peer_capab_arg cap = {};
  642. struct wmi_channel_arg chan_arg = {};
  643. lockdep_assert_held(&ar->conf_mutex);
  644. arg.vdev_id = vdev_id;
  645. arg.peer_state = state;
  646. ether_addr_copy(arg.addr, sta->addr);
  647. cap.peer_max_sp = sta->max_sp;
  648. cap.peer_uapsd_queues = sta->uapsd_queues;
  649. if (state == WMI_TDLS_PEER_STATE_CONNECTED &&
  650. !sta->tdls_initiator)
  651. cap.is_peer_responder = 1;
  652. ret = ath10k_wmi_tdls_peer_update(ar, &arg, &cap, &chan_arg);
  653. if (ret) {
  654. ath10k_warn(ar, "failed to update tdls peer %pM on vdev %i: %i\n",
  655. arg.addr, vdev_id, ret);
  656. return ret;
  657. }
  658. return 0;
  659. }
  660. /************************/
  661. /* Interface management */
  662. /************************/
  663. void ath10k_mac_vif_beacon_free(struct ath10k_vif *arvif)
  664. {
  665. struct ath10k *ar = arvif->ar;
  666. lockdep_assert_held(&ar->data_lock);
  667. if (!arvif->beacon)
  668. return;
  669. if (!arvif->beacon_buf)
  670. dma_unmap_single(ar->dev, ATH10K_SKB_CB(arvif->beacon)->paddr,
  671. arvif->beacon->len, DMA_TO_DEVICE);
  672. if (WARN_ON(arvif->beacon_state != ATH10K_BEACON_SCHEDULED &&
  673. arvif->beacon_state != ATH10K_BEACON_SENT))
  674. return;
  675. dev_kfree_skb_any(arvif->beacon);
  676. arvif->beacon = NULL;
  677. arvif->beacon_state = ATH10K_BEACON_SCHEDULED;
  678. }
  679. static void ath10k_mac_vif_beacon_cleanup(struct ath10k_vif *arvif)
  680. {
  681. struct ath10k *ar = arvif->ar;
  682. lockdep_assert_held(&ar->data_lock);
  683. ath10k_mac_vif_beacon_free(arvif);
  684. if (arvif->beacon_buf) {
  685. dma_free_coherent(ar->dev, IEEE80211_MAX_FRAME_LEN,
  686. arvif->beacon_buf, arvif->beacon_paddr);
  687. arvif->beacon_buf = NULL;
  688. }
  689. }
  690. static inline int ath10k_vdev_setup_sync(struct ath10k *ar)
  691. {
  692. unsigned long time_left;
  693. lockdep_assert_held(&ar->conf_mutex);
  694. if (test_bit(ATH10K_FLAG_CRASH_FLUSH, &ar->dev_flags))
  695. return -ESHUTDOWN;
  696. time_left = wait_for_completion_timeout(&ar->vdev_setup_done,
  697. ATH10K_VDEV_SETUP_TIMEOUT_HZ);
  698. if (time_left == 0)
  699. return -ETIMEDOUT;
  700. return 0;
  701. }
  702. static int ath10k_monitor_vdev_start(struct ath10k *ar, int vdev_id)
  703. {
  704. struct cfg80211_chan_def *chandef = NULL;
  705. struct ieee80211_channel *channel = NULL;
  706. struct wmi_vdev_start_request_arg arg = {};
  707. int ret = 0;
  708. lockdep_assert_held(&ar->conf_mutex);
  709. ieee80211_iter_chan_contexts_atomic(ar->hw,
  710. ath10k_mac_get_any_chandef_iter,
  711. &chandef);
  712. if (WARN_ON_ONCE(!chandef))
  713. return -ENOENT;
  714. channel = chandef->chan;
  715. arg.vdev_id = vdev_id;
  716. arg.channel.freq = channel->center_freq;
  717. arg.channel.band_center_freq1 = chandef->center_freq1;
  718. /* TODO setup this dynamically, what in case we
  719. don't have any vifs? */
  720. arg.channel.mode = chan_to_phymode(chandef);
  721. arg.channel.chan_radar =
  722. !!(channel->flags & IEEE80211_CHAN_RADAR);
  723. arg.channel.min_power = 0;
  724. arg.channel.max_power = channel->max_power * 2;
  725. arg.channel.max_reg_power = channel->max_reg_power * 2;
  726. arg.channel.max_antenna_gain = channel->max_antenna_gain * 2;
  727. reinit_completion(&ar->vdev_setup_done);
  728. ret = ath10k_wmi_vdev_start(ar, &arg);
  729. if (ret) {
  730. ath10k_warn(ar, "failed to request monitor vdev %i start: %d\n",
  731. vdev_id, ret);
  732. return ret;
  733. }
  734. ret = ath10k_vdev_setup_sync(ar);
  735. if (ret) {
  736. ath10k_warn(ar, "failed to synchronize setup for monitor vdev %i start: %d\n",
  737. vdev_id, ret);
  738. return ret;
  739. }
  740. ret = ath10k_wmi_vdev_up(ar, vdev_id, 0, ar->mac_addr);
  741. if (ret) {
  742. ath10k_warn(ar, "failed to put up monitor vdev %i: %d\n",
  743. vdev_id, ret);
  744. goto vdev_stop;
  745. }
  746. ar->monitor_vdev_id = vdev_id;
  747. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor vdev %i started\n",
  748. ar->monitor_vdev_id);
  749. return 0;
  750. vdev_stop:
  751. ret = ath10k_wmi_vdev_stop(ar, ar->monitor_vdev_id);
  752. if (ret)
  753. ath10k_warn(ar, "failed to stop monitor vdev %i after start failure: %d\n",
  754. ar->monitor_vdev_id, ret);
  755. return ret;
  756. }
  757. static int ath10k_monitor_vdev_stop(struct ath10k *ar)
  758. {
  759. int ret = 0;
  760. lockdep_assert_held(&ar->conf_mutex);
  761. ret = ath10k_wmi_vdev_down(ar, ar->monitor_vdev_id);
  762. if (ret)
  763. ath10k_warn(ar, "failed to put down monitor vdev %i: %d\n",
  764. ar->monitor_vdev_id, ret);
  765. reinit_completion(&ar->vdev_setup_done);
  766. ret = ath10k_wmi_vdev_stop(ar, ar->monitor_vdev_id);
  767. if (ret)
  768. ath10k_warn(ar, "failed to to request monitor vdev %i stop: %d\n",
  769. ar->monitor_vdev_id, ret);
  770. ret = ath10k_vdev_setup_sync(ar);
  771. if (ret)
  772. ath10k_warn(ar, "failed to synchronize monitor vdev %i stop: %d\n",
  773. ar->monitor_vdev_id, ret);
  774. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor vdev %i stopped\n",
  775. ar->monitor_vdev_id);
  776. return ret;
  777. }
  778. static int ath10k_monitor_vdev_create(struct ath10k *ar)
  779. {
  780. int bit, ret = 0;
  781. lockdep_assert_held(&ar->conf_mutex);
  782. if (ar->free_vdev_map == 0) {
  783. ath10k_warn(ar, "failed to find free vdev id for monitor vdev\n");
  784. return -ENOMEM;
  785. }
  786. bit = __ffs64(ar->free_vdev_map);
  787. ar->monitor_vdev_id = bit;
  788. ret = ath10k_wmi_vdev_create(ar, ar->monitor_vdev_id,
  789. WMI_VDEV_TYPE_MONITOR,
  790. 0, ar->mac_addr);
  791. if (ret) {
  792. ath10k_warn(ar, "failed to request monitor vdev %i creation: %d\n",
  793. ar->monitor_vdev_id, ret);
  794. return ret;
  795. }
  796. ar->free_vdev_map &= ~(1LL << ar->monitor_vdev_id);
  797. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor vdev %d created\n",
  798. ar->monitor_vdev_id);
  799. return 0;
  800. }
  801. static int ath10k_monitor_vdev_delete(struct ath10k *ar)
  802. {
  803. int ret = 0;
  804. lockdep_assert_held(&ar->conf_mutex);
  805. ret = ath10k_wmi_vdev_delete(ar, ar->monitor_vdev_id);
  806. if (ret) {
  807. ath10k_warn(ar, "failed to request wmi monitor vdev %i removal: %d\n",
  808. ar->monitor_vdev_id, ret);
  809. return ret;
  810. }
  811. ar->free_vdev_map |= 1LL << ar->monitor_vdev_id;
  812. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor vdev %d deleted\n",
  813. ar->monitor_vdev_id);
  814. return ret;
  815. }
  816. static int ath10k_monitor_start(struct ath10k *ar)
  817. {
  818. int ret;
  819. lockdep_assert_held(&ar->conf_mutex);
  820. ret = ath10k_monitor_vdev_create(ar);
  821. if (ret) {
  822. ath10k_warn(ar, "failed to create monitor vdev: %d\n", ret);
  823. return ret;
  824. }
  825. ret = ath10k_monitor_vdev_start(ar, ar->monitor_vdev_id);
  826. if (ret) {
  827. ath10k_warn(ar, "failed to start monitor vdev: %d\n", ret);
  828. ath10k_monitor_vdev_delete(ar);
  829. return ret;
  830. }
  831. ar->monitor_started = true;
  832. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor started\n");
  833. return 0;
  834. }
  835. static int ath10k_monitor_stop(struct ath10k *ar)
  836. {
  837. int ret;
  838. lockdep_assert_held(&ar->conf_mutex);
  839. ret = ath10k_monitor_vdev_stop(ar);
  840. if (ret) {
  841. ath10k_warn(ar, "failed to stop monitor vdev: %d\n", ret);
  842. return ret;
  843. }
  844. ret = ath10k_monitor_vdev_delete(ar);
  845. if (ret) {
  846. ath10k_warn(ar, "failed to delete monitor vdev: %d\n", ret);
  847. return ret;
  848. }
  849. ar->monitor_started = false;
  850. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor stopped\n");
  851. return 0;
  852. }
  853. static bool ath10k_mac_monitor_vdev_is_needed(struct ath10k *ar)
  854. {
  855. int num_ctx;
  856. /* At least one chanctx is required to derive a channel to start
  857. * monitor vdev on.
  858. */
  859. num_ctx = ath10k_mac_num_chanctxs(ar);
  860. if (num_ctx == 0)
  861. return false;
  862. /* If there's already an existing special monitor interface then don't
  863. * bother creating another monitor vdev.
  864. */
  865. if (ar->monitor_arvif)
  866. return false;
  867. return ar->monitor ||
  868. ar->filter_flags & FIF_OTHER_BSS ||
  869. test_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
  870. }
  871. static bool ath10k_mac_monitor_vdev_is_allowed(struct ath10k *ar)
  872. {
  873. int num_ctx;
  874. num_ctx = ath10k_mac_num_chanctxs(ar);
  875. /* FIXME: Current interface combinations and cfg80211/mac80211 code
  876. * shouldn't allow this but make sure to prevent handling the following
  877. * case anyway since multi-channel DFS hasn't been tested at all.
  878. */
  879. if (test_bit(ATH10K_CAC_RUNNING, &ar->dev_flags) && num_ctx > 1)
  880. return false;
  881. return true;
  882. }
  883. static int ath10k_monitor_recalc(struct ath10k *ar)
  884. {
  885. bool needed;
  886. bool allowed;
  887. int ret;
  888. lockdep_assert_held(&ar->conf_mutex);
  889. needed = ath10k_mac_monitor_vdev_is_needed(ar);
  890. allowed = ath10k_mac_monitor_vdev_is_allowed(ar);
  891. ath10k_dbg(ar, ATH10K_DBG_MAC,
  892. "mac monitor recalc started? %d needed? %d allowed? %d\n",
  893. ar->monitor_started, needed, allowed);
  894. if (WARN_ON(needed && !allowed)) {
  895. if (ar->monitor_started) {
  896. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor stopping disallowed monitor\n");
  897. ret = ath10k_monitor_stop(ar);
  898. if (ret)
  899. ath10k_warn(ar, "failed to stop disallowed monitor: %d\n",
  900. ret);
  901. /* not serious */
  902. }
  903. return -EPERM;
  904. }
  905. if (needed == ar->monitor_started)
  906. return 0;
  907. if (needed)
  908. return ath10k_monitor_start(ar);
  909. else
  910. return ath10k_monitor_stop(ar);
  911. }
  912. static int ath10k_recalc_rtscts_prot(struct ath10k_vif *arvif)
  913. {
  914. struct ath10k *ar = arvif->ar;
  915. u32 vdev_param, rts_cts = 0;
  916. lockdep_assert_held(&ar->conf_mutex);
  917. vdev_param = ar->wmi.vdev_param->enable_rtscts;
  918. rts_cts |= SM(WMI_RTSCTS_ENABLED, WMI_RTSCTS_SET);
  919. if (arvif->num_legacy_stations > 0)
  920. rts_cts |= SM(WMI_RTSCTS_ACROSS_SW_RETRIES,
  921. WMI_RTSCTS_PROFILE);
  922. else
  923. rts_cts |= SM(WMI_RTSCTS_FOR_SECOND_RATESERIES,
  924. WMI_RTSCTS_PROFILE);
  925. return ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
  926. rts_cts);
  927. }
  928. static int ath10k_start_cac(struct ath10k *ar)
  929. {
  930. int ret;
  931. lockdep_assert_held(&ar->conf_mutex);
  932. set_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
  933. ret = ath10k_monitor_recalc(ar);
  934. if (ret) {
  935. ath10k_warn(ar, "failed to start monitor (cac): %d\n", ret);
  936. clear_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
  937. return ret;
  938. }
  939. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac cac start monitor vdev %d\n",
  940. ar->monitor_vdev_id);
  941. return 0;
  942. }
  943. static int ath10k_stop_cac(struct ath10k *ar)
  944. {
  945. lockdep_assert_held(&ar->conf_mutex);
  946. /* CAC is not running - do nothing */
  947. if (!test_bit(ATH10K_CAC_RUNNING, &ar->dev_flags))
  948. return 0;
  949. clear_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
  950. ath10k_monitor_stop(ar);
  951. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac cac finished\n");
  952. return 0;
  953. }
  954. static void ath10k_mac_has_radar_iter(struct ieee80211_hw *hw,
  955. struct ieee80211_chanctx_conf *conf,
  956. void *data)
  957. {
  958. bool *ret = data;
  959. if (!*ret && conf->radar_enabled)
  960. *ret = true;
  961. }
  962. static bool ath10k_mac_has_radar_enabled(struct ath10k *ar)
  963. {
  964. bool has_radar = false;
  965. ieee80211_iter_chan_contexts_atomic(ar->hw,
  966. ath10k_mac_has_radar_iter,
  967. &has_radar);
  968. return has_radar;
  969. }
  970. static void ath10k_recalc_radar_detection(struct ath10k *ar)
  971. {
  972. int ret;
  973. lockdep_assert_held(&ar->conf_mutex);
  974. ath10k_stop_cac(ar);
  975. if (!ath10k_mac_has_radar_enabled(ar))
  976. return;
  977. if (ar->num_started_vdevs > 0)
  978. return;
  979. ret = ath10k_start_cac(ar);
  980. if (ret) {
  981. /*
  982. * Not possible to start CAC on current channel so starting
  983. * radiation is not allowed, make this channel DFS_UNAVAILABLE
  984. * by indicating that radar was detected.
  985. */
  986. ath10k_warn(ar, "failed to start CAC: %d\n", ret);
  987. ieee80211_radar_detected(ar->hw);
  988. }
  989. }
  990. static int ath10k_vdev_stop(struct ath10k_vif *arvif)
  991. {
  992. struct ath10k *ar = arvif->ar;
  993. int ret;
  994. lockdep_assert_held(&ar->conf_mutex);
  995. reinit_completion(&ar->vdev_setup_done);
  996. ret = ath10k_wmi_vdev_stop(ar, arvif->vdev_id);
  997. if (ret) {
  998. ath10k_warn(ar, "failed to stop WMI vdev %i: %d\n",
  999. arvif->vdev_id, ret);
  1000. return ret;
  1001. }
  1002. ret = ath10k_vdev_setup_sync(ar);
  1003. if (ret) {
  1004. ath10k_warn(ar, "failed to syncronise setup for vdev %i: %d\n",
  1005. arvif->vdev_id, ret);
  1006. return ret;
  1007. }
  1008. WARN_ON(ar->num_started_vdevs == 0);
  1009. if (ar->num_started_vdevs != 0) {
  1010. ar->num_started_vdevs--;
  1011. ath10k_recalc_radar_detection(ar);
  1012. }
  1013. return ret;
  1014. }
  1015. static int ath10k_vdev_start_restart(struct ath10k_vif *arvif,
  1016. const struct cfg80211_chan_def *chandef,
  1017. bool restart)
  1018. {
  1019. struct ath10k *ar = arvif->ar;
  1020. struct wmi_vdev_start_request_arg arg = {};
  1021. int ret = 0;
  1022. lockdep_assert_held(&ar->conf_mutex);
  1023. reinit_completion(&ar->vdev_setup_done);
  1024. arg.vdev_id = arvif->vdev_id;
  1025. arg.dtim_period = arvif->dtim_period;
  1026. arg.bcn_intval = arvif->beacon_interval;
  1027. arg.channel.freq = chandef->chan->center_freq;
  1028. arg.channel.band_center_freq1 = chandef->center_freq1;
  1029. arg.channel.mode = chan_to_phymode(chandef);
  1030. arg.channel.min_power = 0;
  1031. arg.channel.max_power = chandef->chan->max_power * 2;
  1032. arg.channel.max_reg_power = chandef->chan->max_reg_power * 2;
  1033. arg.channel.max_antenna_gain = chandef->chan->max_antenna_gain * 2;
  1034. if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
  1035. arg.ssid = arvif->u.ap.ssid;
  1036. arg.ssid_len = arvif->u.ap.ssid_len;
  1037. arg.hidden_ssid = arvif->u.ap.hidden_ssid;
  1038. /* For now allow DFS for AP mode */
  1039. arg.channel.chan_radar =
  1040. !!(chandef->chan->flags & IEEE80211_CHAN_RADAR);
  1041. } else if (arvif->vdev_type == WMI_VDEV_TYPE_IBSS) {
  1042. arg.ssid = arvif->vif->bss_conf.ssid;
  1043. arg.ssid_len = arvif->vif->bss_conf.ssid_len;
  1044. }
  1045. ath10k_dbg(ar, ATH10K_DBG_MAC,
  1046. "mac vdev %d start center_freq %d phymode %s\n",
  1047. arg.vdev_id, arg.channel.freq,
  1048. ath10k_wmi_phymode_str(arg.channel.mode));
  1049. if (restart)
  1050. ret = ath10k_wmi_vdev_restart(ar, &arg);
  1051. else
  1052. ret = ath10k_wmi_vdev_start(ar, &arg);
  1053. if (ret) {
  1054. ath10k_warn(ar, "failed to start WMI vdev %i: %d\n",
  1055. arg.vdev_id, ret);
  1056. return ret;
  1057. }
  1058. ret = ath10k_vdev_setup_sync(ar);
  1059. if (ret) {
  1060. ath10k_warn(ar,
  1061. "failed to synchronize setup for vdev %i restart %d: %d\n",
  1062. arg.vdev_id, restart, ret);
  1063. return ret;
  1064. }
  1065. ar->num_started_vdevs++;
  1066. ath10k_recalc_radar_detection(ar);
  1067. return ret;
  1068. }
  1069. static int ath10k_vdev_start(struct ath10k_vif *arvif,
  1070. const struct cfg80211_chan_def *def)
  1071. {
  1072. return ath10k_vdev_start_restart(arvif, def, false);
  1073. }
  1074. static int ath10k_vdev_restart(struct ath10k_vif *arvif,
  1075. const struct cfg80211_chan_def *def)
  1076. {
  1077. return ath10k_vdev_start_restart(arvif, def, true);
  1078. }
  1079. static int ath10k_mac_setup_bcn_p2p_ie(struct ath10k_vif *arvif,
  1080. struct sk_buff *bcn)
  1081. {
  1082. struct ath10k *ar = arvif->ar;
  1083. struct ieee80211_mgmt *mgmt;
  1084. const u8 *p2p_ie;
  1085. int ret;
  1086. if (arvif->vdev_type != WMI_VDEV_TYPE_AP)
  1087. return 0;
  1088. if (arvif->vdev_subtype != WMI_VDEV_SUBTYPE_P2P_GO)
  1089. return 0;
  1090. mgmt = (void *)bcn->data;
  1091. p2p_ie = cfg80211_find_vendor_ie(WLAN_OUI_WFA, WLAN_OUI_TYPE_WFA_P2P,
  1092. mgmt->u.beacon.variable,
  1093. bcn->len - (mgmt->u.beacon.variable -
  1094. bcn->data));
  1095. if (!p2p_ie)
  1096. return -ENOENT;
  1097. ret = ath10k_wmi_p2p_go_bcn_ie(ar, arvif->vdev_id, p2p_ie);
  1098. if (ret) {
  1099. ath10k_warn(ar, "failed to submit p2p go bcn ie for vdev %i: %d\n",
  1100. arvif->vdev_id, ret);
  1101. return ret;
  1102. }
  1103. return 0;
  1104. }
  1105. static int ath10k_mac_remove_vendor_ie(struct sk_buff *skb, unsigned int oui,
  1106. u8 oui_type, size_t ie_offset)
  1107. {
  1108. size_t len;
  1109. const u8 *next;
  1110. const u8 *end;
  1111. u8 *ie;
  1112. if (WARN_ON(skb->len < ie_offset))
  1113. return -EINVAL;
  1114. ie = (u8 *)cfg80211_find_vendor_ie(oui, oui_type,
  1115. skb->data + ie_offset,
  1116. skb->len - ie_offset);
  1117. if (!ie)
  1118. return -ENOENT;
  1119. len = ie[1] + 2;
  1120. end = skb->data + skb->len;
  1121. next = ie + len;
  1122. if (WARN_ON(next > end))
  1123. return -EINVAL;
  1124. memmove(ie, next, end - next);
  1125. skb_trim(skb, skb->len - len);
  1126. return 0;
  1127. }
  1128. static int ath10k_mac_setup_bcn_tmpl(struct ath10k_vif *arvif)
  1129. {
  1130. struct ath10k *ar = arvif->ar;
  1131. struct ieee80211_hw *hw = ar->hw;
  1132. struct ieee80211_vif *vif = arvif->vif;
  1133. struct ieee80211_mutable_offsets offs = {};
  1134. struct sk_buff *bcn;
  1135. int ret;
  1136. if (!test_bit(WMI_SERVICE_BEACON_OFFLOAD, ar->wmi.svc_map))
  1137. return 0;
  1138. if (arvif->vdev_type != WMI_VDEV_TYPE_AP &&
  1139. arvif->vdev_type != WMI_VDEV_TYPE_IBSS)
  1140. return 0;
  1141. bcn = ieee80211_beacon_get_template(hw, vif, &offs);
  1142. if (!bcn) {
  1143. ath10k_warn(ar, "failed to get beacon template from mac80211\n");
  1144. return -EPERM;
  1145. }
  1146. ret = ath10k_mac_setup_bcn_p2p_ie(arvif, bcn);
  1147. if (ret) {
  1148. ath10k_warn(ar, "failed to setup p2p go bcn ie: %d\n", ret);
  1149. kfree_skb(bcn);
  1150. return ret;
  1151. }
  1152. /* P2P IE is inserted by firmware automatically (as configured above)
  1153. * so remove it from the base beacon template to avoid duplicate P2P
  1154. * IEs in beacon frames.
  1155. */
  1156. ath10k_mac_remove_vendor_ie(bcn, WLAN_OUI_WFA, WLAN_OUI_TYPE_WFA_P2P,
  1157. offsetof(struct ieee80211_mgmt,
  1158. u.beacon.variable));
  1159. ret = ath10k_wmi_bcn_tmpl(ar, arvif->vdev_id, offs.tim_offset, bcn, 0,
  1160. 0, NULL, 0);
  1161. kfree_skb(bcn);
  1162. if (ret) {
  1163. ath10k_warn(ar, "failed to submit beacon template command: %d\n",
  1164. ret);
  1165. return ret;
  1166. }
  1167. return 0;
  1168. }
  1169. static int ath10k_mac_setup_prb_tmpl(struct ath10k_vif *arvif)
  1170. {
  1171. struct ath10k *ar = arvif->ar;
  1172. struct ieee80211_hw *hw = ar->hw;
  1173. struct ieee80211_vif *vif = arvif->vif;
  1174. struct sk_buff *prb;
  1175. int ret;
  1176. if (!test_bit(WMI_SERVICE_BEACON_OFFLOAD, ar->wmi.svc_map))
  1177. return 0;
  1178. if (arvif->vdev_type != WMI_VDEV_TYPE_AP)
  1179. return 0;
  1180. prb = ieee80211_proberesp_get(hw, vif);
  1181. if (!prb) {
  1182. ath10k_warn(ar, "failed to get probe resp template from mac80211\n");
  1183. return -EPERM;
  1184. }
  1185. ret = ath10k_wmi_prb_tmpl(ar, arvif->vdev_id, prb);
  1186. kfree_skb(prb);
  1187. if (ret) {
  1188. ath10k_warn(ar, "failed to submit probe resp template command: %d\n",
  1189. ret);
  1190. return ret;
  1191. }
  1192. return 0;
  1193. }
  1194. static int ath10k_mac_vif_fix_hidden_ssid(struct ath10k_vif *arvif)
  1195. {
  1196. struct ath10k *ar = arvif->ar;
  1197. struct cfg80211_chan_def def;
  1198. int ret;
  1199. /* When originally vdev is started during assign_vif_chanctx() some
  1200. * information is missing, notably SSID. Firmware revisions with beacon
  1201. * offloading require the SSID to be provided during vdev (re)start to
  1202. * handle hidden SSID properly.
  1203. *
  1204. * Vdev restart must be done after vdev has been both started and
  1205. * upped. Otherwise some firmware revisions (at least 10.2) fail to
  1206. * deliver vdev restart response event causing timeouts during vdev
  1207. * syncing in ath10k.
  1208. *
  1209. * Note: The vdev down/up and template reinstallation could be skipped
  1210. * since only wmi-tlv firmware are known to have beacon offload and
  1211. * wmi-tlv doesn't seem to misbehave like 10.2 wrt vdev restart
  1212. * response delivery. It's probably more robust to keep it as is.
  1213. */
  1214. if (!test_bit(WMI_SERVICE_BEACON_OFFLOAD, ar->wmi.svc_map))
  1215. return 0;
  1216. if (WARN_ON(!arvif->is_started))
  1217. return -EINVAL;
  1218. if (WARN_ON(!arvif->is_up))
  1219. return -EINVAL;
  1220. if (WARN_ON(ath10k_mac_vif_chan(arvif->vif, &def)))
  1221. return -EINVAL;
  1222. ret = ath10k_wmi_vdev_down(ar, arvif->vdev_id);
  1223. if (ret) {
  1224. ath10k_warn(ar, "failed to bring down ap vdev %i: %d\n",
  1225. arvif->vdev_id, ret);
  1226. return ret;
  1227. }
  1228. /* Vdev down reset beacon & presp templates. Reinstall them. Otherwise
  1229. * firmware will crash upon vdev up.
  1230. */
  1231. ret = ath10k_mac_setup_bcn_tmpl(arvif);
  1232. if (ret) {
  1233. ath10k_warn(ar, "failed to update beacon template: %d\n", ret);
  1234. return ret;
  1235. }
  1236. ret = ath10k_mac_setup_prb_tmpl(arvif);
  1237. if (ret) {
  1238. ath10k_warn(ar, "failed to update presp template: %d\n", ret);
  1239. return ret;
  1240. }
  1241. ret = ath10k_vdev_restart(arvif, &def);
  1242. if (ret) {
  1243. ath10k_warn(ar, "failed to restart ap vdev %i: %d\n",
  1244. arvif->vdev_id, ret);
  1245. return ret;
  1246. }
  1247. ret = ath10k_wmi_vdev_up(arvif->ar, arvif->vdev_id, arvif->aid,
  1248. arvif->bssid);
  1249. if (ret) {
  1250. ath10k_warn(ar, "failed to bring up ap vdev %i: %d\n",
  1251. arvif->vdev_id, ret);
  1252. return ret;
  1253. }
  1254. return 0;
  1255. }
  1256. static void ath10k_control_beaconing(struct ath10k_vif *arvif,
  1257. struct ieee80211_bss_conf *info)
  1258. {
  1259. struct ath10k *ar = arvif->ar;
  1260. int ret = 0;
  1261. lockdep_assert_held(&arvif->ar->conf_mutex);
  1262. if (!info->enable_beacon) {
  1263. ret = ath10k_wmi_vdev_down(ar, arvif->vdev_id);
  1264. if (ret)
  1265. ath10k_warn(ar, "failed to down vdev_id %i: %d\n",
  1266. arvif->vdev_id, ret);
  1267. arvif->is_up = false;
  1268. spin_lock_bh(&arvif->ar->data_lock);
  1269. ath10k_mac_vif_beacon_free(arvif);
  1270. spin_unlock_bh(&arvif->ar->data_lock);
  1271. return;
  1272. }
  1273. arvif->tx_seq_no = 0x1000;
  1274. arvif->aid = 0;
  1275. ether_addr_copy(arvif->bssid, info->bssid);
  1276. ret = ath10k_wmi_vdev_up(arvif->ar, arvif->vdev_id, arvif->aid,
  1277. arvif->bssid);
  1278. if (ret) {
  1279. ath10k_warn(ar, "failed to bring up vdev %d: %i\n",
  1280. arvif->vdev_id, ret);
  1281. return;
  1282. }
  1283. arvif->is_up = true;
  1284. ret = ath10k_mac_vif_fix_hidden_ssid(arvif);
  1285. if (ret) {
  1286. ath10k_warn(ar, "failed to fix hidden ssid for vdev %i, expect trouble: %d\n",
  1287. arvif->vdev_id, ret);
  1288. return;
  1289. }
  1290. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d up\n", arvif->vdev_id);
  1291. }
  1292. static void ath10k_control_ibss(struct ath10k_vif *arvif,
  1293. struct ieee80211_bss_conf *info,
  1294. const u8 self_peer[ETH_ALEN])
  1295. {
  1296. struct ath10k *ar = arvif->ar;
  1297. u32 vdev_param;
  1298. int ret = 0;
  1299. lockdep_assert_held(&arvif->ar->conf_mutex);
  1300. if (!info->ibss_joined) {
  1301. if (is_zero_ether_addr(arvif->bssid))
  1302. return;
  1303. eth_zero_addr(arvif->bssid);
  1304. return;
  1305. }
  1306. vdev_param = arvif->ar->wmi.vdev_param->atim_window;
  1307. ret = ath10k_wmi_vdev_set_param(arvif->ar, arvif->vdev_id, vdev_param,
  1308. ATH10K_DEFAULT_ATIM);
  1309. if (ret)
  1310. ath10k_warn(ar, "failed to set IBSS ATIM for vdev %d: %d\n",
  1311. arvif->vdev_id, ret);
  1312. }
  1313. static int ath10k_mac_vif_recalc_ps_wake_threshold(struct ath10k_vif *arvif)
  1314. {
  1315. struct ath10k *ar = arvif->ar;
  1316. u32 param;
  1317. u32 value;
  1318. int ret;
  1319. lockdep_assert_held(&arvif->ar->conf_mutex);
  1320. if (arvif->u.sta.uapsd)
  1321. value = WMI_STA_PS_TX_WAKE_THRESHOLD_NEVER;
  1322. else
  1323. value = WMI_STA_PS_TX_WAKE_THRESHOLD_ALWAYS;
  1324. param = WMI_STA_PS_PARAM_TX_WAKE_THRESHOLD;
  1325. ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id, param, value);
  1326. if (ret) {
  1327. ath10k_warn(ar, "failed to submit ps wake threshold %u on vdev %i: %d\n",
  1328. value, arvif->vdev_id, ret);
  1329. return ret;
  1330. }
  1331. return 0;
  1332. }
  1333. static int ath10k_mac_vif_recalc_ps_poll_count(struct ath10k_vif *arvif)
  1334. {
  1335. struct ath10k *ar = arvif->ar;
  1336. u32 param;
  1337. u32 value;
  1338. int ret;
  1339. lockdep_assert_held(&arvif->ar->conf_mutex);
  1340. if (arvif->u.sta.uapsd)
  1341. value = WMI_STA_PS_PSPOLL_COUNT_UAPSD;
  1342. else
  1343. value = WMI_STA_PS_PSPOLL_COUNT_NO_MAX;
  1344. param = WMI_STA_PS_PARAM_PSPOLL_COUNT;
  1345. ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
  1346. param, value);
  1347. if (ret) {
  1348. ath10k_warn(ar, "failed to submit ps poll count %u on vdev %i: %d\n",
  1349. value, arvif->vdev_id, ret);
  1350. return ret;
  1351. }
  1352. return 0;
  1353. }
  1354. static int ath10k_mac_num_vifs_started(struct ath10k *ar)
  1355. {
  1356. struct ath10k_vif *arvif;
  1357. int num = 0;
  1358. lockdep_assert_held(&ar->conf_mutex);
  1359. list_for_each_entry(arvif, &ar->arvifs, list)
  1360. if (arvif->is_started)
  1361. num++;
  1362. return num;
  1363. }
  1364. static int ath10k_mac_vif_setup_ps(struct ath10k_vif *arvif)
  1365. {
  1366. struct ath10k *ar = arvif->ar;
  1367. struct ieee80211_vif *vif = arvif->vif;
  1368. struct ieee80211_conf *conf = &ar->hw->conf;
  1369. enum wmi_sta_powersave_param param;
  1370. enum wmi_sta_ps_mode psmode;
  1371. int ret;
  1372. int ps_timeout;
  1373. bool enable_ps;
  1374. lockdep_assert_held(&arvif->ar->conf_mutex);
  1375. if (arvif->vif->type != NL80211_IFTYPE_STATION)
  1376. return 0;
  1377. enable_ps = arvif->ps;
  1378. if (enable_ps && ath10k_mac_num_vifs_started(ar) > 1 &&
  1379. !test_bit(ATH10K_FW_FEATURE_MULTI_VIF_PS_SUPPORT,
  1380. ar->fw_features)) {
  1381. ath10k_warn(ar, "refusing to enable ps on vdev %i: not supported by fw\n",
  1382. arvif->vdev_id);
  1383. enable_ps = false;
  1384. }
  1385. if (!arvif->is_started) {
  1386. /* mac80211 can update vif powersave state while disconnected.
  1387. * Firmware doesn't behave nicely and consumes more power than
  1388. * necessary if PS is disabled on a non-started vdev. Hence
  1389. * force-enable PS for non-running vdevs.
  1390. */
  1391. psmode = WMI_STA_PS_MODE_ENABLED;
  1392. } else if (enable_ps) {
  1393. psmode = WMI_STA_PS_MODE_ENABLED;
  1394. param = WMI_STA_PS_PARAM_INACTIVITY_TIME;
  1395. ps_timeout = conf->dynamic_ps_timeout;
  1396. if (ps_timeout == 0) {
  1397. /* Firmware doesn't like 0 */
  1398. ps_timeout = ieee80211_tu_to_usec(
  1399. vif->bss_conf.beacon_int) / 1000;
  1400. }
  1401. ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id, param,
  1402. ps_timeout);
  1403. if (ret) {
  1404. ath10k_warn(ar, "failed to set inactivity time for vdev %d: %i\n",
  1405. arvif->vdev_id, ret);
  1406. return ret;
  1407. }
  1408. } else {
  1409. psmode = WMI_STA_PS_MODE_DISABLED;
  1410. }
  1411. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d psmode %s\n",
  1412. arvif->vdev_id, psmode ? "enable" : "disable");
  1413. ret = ath10k_wmi_set_psmode(ar, arvif->vdev_id, psmode);
  1414. if (ret) {
  1415. ath10k_warn(ar, "failed to set PS Mode %d for vdev %d: %d\n",
  1416. psmode, arvif->vdev_id, ret);
  1417. return ret;
  1418. }
  1419. return 0;
  1420. }
  1421. static int ath10k_mac_vif_disable_keepalive(struct ath10k_vif *arvif)
  1422. {
  1423. struct ath10k *ar = arvif->ar;
  1424. struct wmi_sta_keepalive_arg arg = {};
  1425. int ret;
  1426. lockdep_assert_held(&arvif->ar->conf_mutex);
  1427. if (arvif->vdev_type != WMI_VDEV_TYPE_STA)
  1428. return 0;
  1429. if (!test_bit(WMI_SERVICE_STA_KEEP_ALIVE, ar->wmi.svc_map))
  1430. return 0;
  1431. /* Some firmware revisions have a bug and ignore the `enabled` field.
  1432. * Instead use the interval to disable the keepalive.
  1433. */
  1434. arg.vdev_id = arvif->vdev_id;
  1435. arg.enabled = 1;
  1436. arg.method = WMI_STA_KEEPALIVE_METHOD_NULL_FRAME;
  1437. arg.interval = WMI_STA_KEEPALIVE_INTERVAL_DISABLE;
  1438. ret = ath10k_wmi_sta_keepalive(ar, &arg);
  1439. if (ret) {
  1440. ath10k_warn(ar, "failed to submit keepalive on vdev %i: %d\n",
  1441. arvif->vdev_id, ret);
  1442. return ret;
  1443. }
  1444. return 0;
  1445. }
  1446. static void ath10k_mac_vif_ap_csa_count_down(struct ath10k_vif *arvif)
  1447. {
  1448. struct ath10k *ar = arvif->ar;
  1449. struct ieee80211_vif *vif = arvif->vif;
  1450. int ret;
  1451. lockdep_assert_held(&arvif->ar->conf_mutex);
  1452. if (WARN_ON(!test_bit(WMI_SERVICE_BEACON_OFFLOAD, ar->wmi.svc_map)))
  1453. return;
  1454. if (arvif->vdev_type != WMI_VDEV_TYPE_AP)
  1455. return;
  1456. if (!vif->csa_active)
  1457. return;
  1458. if (!arvif->is_up)
  1459. return;
  1460. if (!ieee80211_csa_is_complete(vif)) {
  1461. ieee80211_csa_update_counter(vif);
  1462. ret = ath10k_mac_setup_bcn_tmpl(arvif);
  1463. if (ret)
  1464. ath10k_warn(ar, "failed to update bcn tmpl during csa: %d\n",
  1465. ret);
  1466. ret = ath10k_mac_setup_prb_tmpl(arvif);
  1467. if (ret)
  1468. ath10k_warn(ar, "failed to update prb tmpl during csa: %d\n",
  1469. ret);
  1470. } else {
  1471. ieee80211_csa_finish(vif);
  1472. }
  1473. }
  1474. static void ath10k_mac_vif_ap_csa_work(struct work_struct *work)
  1475. {
  1476. struct ath10k_vif *arvif = container_of(work, struct ath10k_vif,
  1477. ap_csa_work);
  1478. struct ath10k *ar = arvif->ar;
  1479. mutex_lock(&ar->conf_mutex);
  1480. ath10k_mac_vif_ap_csa_count_down(arvif);
  1481. mutex_unlock(&ar->conf_mutex);
  1482. }
  1483. static void ath10k_mac_handle_beacon_iter(void *data, u8 *mac,
  1484. struct ieee80211_vif *vif)
  1485. {
  1486. struct sk_buff *skb = data;
  1487. struct ieee80211_mgmt *mgmt = (void *)skb->data;
  1488. struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
  1489. if (vif->type != NL80211_IFTYPE_STATION)
  1490. return;
  1491. if (!ether_addr_equal(mgmt->bssid, vif->bss_conf.bssid))
  1492. return;
  1493. cancel_delayed_work(&arvif->connection_loss_work);
  1494. }
  1495. void ath10k_mac_handle_beacon(struct ath10k *ar, struct sk_buff *skb)
  1496. {
  1497. ieee80211_iterate_active_interfaces_atomic(ar->hw,
  1498. IEEE80211_IFACE_ITER_NORMAL,
  1499. ath10k_mac_handle_beacon_iter,
  1500. skb);
  1501. }
  1502. static void ath10k_mac_handle_beacon_miss_iter(void *data, u8 *mac,
  1503. struct ieee80211_vif *vif)
  1504. {
  1505. u32 *vdev_id = data;
  1506. struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
  1507. struct ath10k *ar = arvif->ar;
  1508. struct ieee80211_hw *hw = ar->hw;
  1509. if (arvif->vdev_id != *vdev_id)
  1510. return;
  1511. if (!arvif->is_up)
  1512. return;
  1513. ieee80211_beacon_loss(vif);
  1514. /* Firmware doesn't report beacon loss events repeatedly. If AP probe
  1515. * (done by mac80211) succeeds but beacons do not resume then it
  1516. * doesn't make sense to continue operation. Queue connection loss work
  1517. * which can be cancelled when beacon is received.
  1518. */
  1519. ieee80211_queue_delayed_work(hw, &arvif->connection_loss_work,
  1520. ATH10K_CONNECTION_LOSS_HZ);
  1521. }
  1522. void ath10k_mac_handle_beacon_miss(struct ath10k *ar, u32 vdev_id)
  1523. {
  1524. ieee80211_iterate_active_interfaces_atomic(ar->hw,
  1525. IEEE80211_IFACE_ITER_NORMAL,
  1526. ath10k_mac_handle_beacon_miss_iter,
  1527. &vdev_id);
  1528. }
  1529. static void ath10k_mac_vif_sta_connection_loss_work(struct work_struct *work)
  1530. {
  1531. struct ath10k_vif *arvif = container_of(work, struct ath10k_vif,
  1532. connection_loss_work.work);
  1533. struct ieee80211_vif *vif = arvif->vif;
  1534. if (!arvif->is_up)
  1535. return;
  1536. ieee80211_connection_loss(vif);
  1537. }
  1538. /**********************/
  1539. /* Station management */
  1540. /**********************/
  1541. static u32 ath10k_peer_assoc_h_listen_intval(struct ath10k *ar,
  1542. struct ieee80211_vif *vif)
  1543. {
  1544. /* Some firmware revisions have unstable STA powersave when listen
  1545. * interval is set too high (e.g. 5). The symptoms are firmware doesn't
  1546. * generate NullFunc frames properly even if buffered frames have been
  1547. * indicated in Beacon TIM. Firmware would seldom wake up to pull
  1548. * buffered frames. Often pinging the device from AP would simply fail.
  1549. *
  1550. * As a workaround set it to 1.
  1551. */
  1552. if (vif->type == NL80211_IFTYPE_STATION)
  1553. return 1;
  1554. return ar->hw->conf.listen_interval;
  1555. }
  1556. static void ath10k_peer_assoc_h_basic(struct ath10k *ar,
  1557. struct ieee80211_vif *vif,
  1558. struct ieee80211_sta *sta,
  1559. struct wmi_peer_assoc_complete_arg *arg)
  1560. {
  1561. struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
  1562. u32 aid;
  1563. lockdep_assert_held(&ar->conf_mutex);
  1564. if (vif->type == NL80211_IFTYPE_STATION)
  1565. aid = vif->bss_conf.aid;
  1566. else
  1567. aid = sta->aid;
  1568. ether_addr_copy(arg->addr, sta->addr);
  1569. arg->vdev_id = arvif->vdev_id;
  1570. arg->peer_aid = aid;
  1571. arg->peer_flags |= arvif->ar->wmi.peer_flags->auth;
  1572. arg->peer_listen_intval = ath10k_peer_assoc_h_listen_intval(ar, vif);
  1573. arg->peer_num_spatial_streams = 1;
  1574. arg->peer_caps = vif->bss_conf.assoc_capability;
  1575. }
  1576. static void ath10k_peer_assoc_h_crypto(struct ath10k *ar,
  1577. struct ieee80211_vif *vif,
  1578. struct ieee80211_sta *sta,
  1579. struct wmi_peer_assoc_complete_arg *arg)
  1580. {
  1581. struct ieee80211_bss_conf *info = &vif->bss_conf;
  1582. struct cfg80211_chan_def def;
  1583. struct cfg80211_bss *bss;
  1584. const u8 *rsnie = NULL;
  1585. const u8 *wpaie = NULL;
  1586. lockdep_assert_held(&ar->conf_mutex);
  1587. if (WARN_ON(ath10k_mac_vif_chan(vif, &def)))
  1588. return;
  1589. bss = cfg80211_get_bss(ar->hw->wiphy, def.chan, info->bssid, NULL, 0,
  1590. IEEE80211_BSS_TYPE_ANY, IEEE80211_PRIVACY_ANY);
  1591. if (bss) {
  1592. const struct cfg80211_bss_ies *ies;
  1593. rcu_read_lock();
  1594. rsnie = ieee80211_bss_get_ie(bss, WLAN_EID_RSN);
  1595. ies = rcu_dereference(bss->ies);
  1596. wpaie = cfg80211_find_vendor_ie(WLAN_OUI_MICROSOFT,
  1597. WLAN_OUI_TYPE_MICROSOFT_WPA,
  1598. ies->data,
  1599. ies->len);
  1600. rcu_read_unlock();
  1601. cfg80211_put_bss(ar->hw->wiphy, bss);
  1602. }
  1603. /* FIXME: base on RSN IE/WPA IE is a correct idea? */
  1604. if (rsnie || wpaie) {
  1605. ath10k_dbg(ar, ATH10K_DBG_WMI, "%s: rsn ie found\n", __func__);
  1606. arg->peer_flags |= ar->wmi.peer_flags->need_ptk_4_way;
  1607. }
  1608. if (wpaie) {
  1609. ath10k_dbg(ar, ATH10K_DBG_WMI, "%s: wpa ie found\n", __func__);
  1610. arg->peer_flags |= ar->wmi.peer_flags->need_gtk_2_way;
  1611. }
  1612. if (sta->mfp &&
  1613. test_bit(ATH10K_FW_FEATURE_MFP_SUPPORT, ar->fw_features)) {
  1614. arg->peer_flags |= ar->wmi.peer_flags->pmf;
  1615. }
  1616. }
  1617. static void ath10k_peer_assoc_h_rates(struct ath10k *ar,
  1618. struct ieee80211_vif *vif,
  1619. struct ieee80211_sta *sta,
  1620. struct wmi_peer_assoc_complete_arg *arg)
  1621. {
  1622. struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
  1623. struct wmi_rate_set_arg *rateset = &arg->peer_legacy_rates;
  1624. struct cfg80211_chan_def def;
  1625. const struct ieee80211_supported_band *sband;
  1626. const struct ieee80211_rate *rates;
  1627. enum ieee80211_band band;
  1628. u32 ratemask;
  1629. u8 rate;
  1630. int i;
  1631. lockdep_assert_held(&ar->conf_mutex);
  1632. if (WARN_ON(ath10k_mac_vif_chan(vif, &def)))
  1633. return;
  1634. band = def.chan->band;
  1635. sband = ar->hw->wiphy->bands[band];
  1636. ratemask = sta->supp_rates[band];
  1637. ratemask &= arvif->bitrate_mask.control[band].legacy;
  1638. rates = sband->bitrates;
  1639. rateset->num_rates = 0;
  1640. for (i = 0; i < 32; i++, ratemask >>= 1, rates++) {
  1641. if (!(ratemask & 1))
  1642. continue;
  1643. rate = ath10k_mac_bitrate_to_rate(rates->bitrate);
  1644. rateset->rates[rateset->num_rates] = rate;
  1645. rateset->num_rates++;
  1646. }
  1647. }
  1648. static bool
  1649. ath10k_peer_assoc_h_ht_masked(const u8 ht_mcs_mask[IEEE80211_HT_MCS_MASK_LEN])
  1650. {
  1651. int nss;
  1652. for (nss = 0; nss < IEEE80211_HT_MCS_MASK_LEN; nss++)
  1653. if (ht_mcs_mask[nss])
  1654. return false;
  1655. return true;
  1656. }
  1657. static bool
  1658. ath10k_peer_assoc_h_vht_masked(const u16 vht_mcs_mask[NL80211_VHT_NSS_MAX])
  1659. {
  1660. int nss;
  1661. for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++)
  1662. if (vht_mcs_mask[nss])
  1663. return false;
  1664. return true;
  1665. }
  1666. static void ath10k_peer_assoc_h_ht(struct ath10k *ar,
  1667. struct ieee80211_vif *vif,
  1668. struct ieee80211_sta *sta,
  1669. struct wmi_peer_assoc_complete_arg *arg)
  1670. {
  1671. const struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
  1672. struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
  1673. struct cfg80211_chan_def def;
  1674. enum ieee80211_band band;
  1675. const u8 *ht_mcs_mask;
  1676. const u16 *vht_mcs_mask;
  1677. int i, n;
  1678. u8 max_nss;
  1679. u32 stbc;
  1680. lockdep_assert_held(&ar->conf_mutex);
  1681. if (WARN_ON(ath10k_mac_vif_chan(vif, &def)))
  1682. return;
  1683. if (!ht_cap->ht_supported)
  1684. return;
  1685. band = def.chan->band;
  1686. ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
  1687. vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
  1688. if (ath10k_peer_assoc_h_ht_masked(ht_mcs_mask) &&
  1689. ath10k_peer_assoc_h_vht_masked(vht_mcs_mask))
  1690. return;
  1691. arg->peer_flags |= ar->wmi.peer_flags->ht;
  1692. arg->peer_max_mpdu = (1 << (IEEE80211_HT_MAX_AMPDU_FACTOR +
  1693. ht_cap->ampdu_factor)) - 1;
  1694. arg->peer_mpdu_density =
  1695. ath10k_parse_mpdudensity(ht_cap->ampdu_density);
  1696. arg->peer_ht_caps = ht_cap->cap;
  1697. arg->peer_rate_caps |= WMI_RC_HT_FLAG;
  1698. if (ht_cap->cap & IEEE80211_HT_CAP_LDPC_CODING)
  1699. arg->peer_flags |= ar->wmi.peer_flags->ldbc;
  1700. if (sta->bandwidth >= IEEE80211_STA_RX_BW_40) {
  1701. arg->peer_flags |= ar->wmi.peer_flags->bw40;
  1702. arg->peer_rate_caps |= WMI_RC_CW40_FLAG;
  1703. }
  1704. if (arvif->bitrate_mask.control[band].gi != NL80211_TXRATE_FORCE_LGI) {
  1705. if (ht_cap->cap & IEEE80211_HT_CAP_SGI_20)
  1706. arg->peer_rate_caps |= WMI_RC_SGI_FLAG;
  1707. if (ht_cap->cap & IEEE80211_HT_CAP_SGI_40)
  1708. arg->peer_rate_caps |= WMI_RC_SGI_FLAG;
  1709. }
  1710. if (ht_cap->cap & IEEE80211_HT_CAP_TX_STBC) {
  1711. arg->peer_rate_caps |= WMI_RC_TX_STBC_FLAG;
  1712. arg->peer_flags |= ar->wmi.peer_flags->stbc;
  1713. }
  1714. if (ht_cap->cap & IEEE80211_HT_CAP_RX_STBC) {
  1715. stbc = ht_cap->cap & IEEE80211_HT_CAP_RX_STBC;
  1716. stbc = stbc >> IEEE80211_HT_CAP_RX_STBC_SHIFT;
  1717. stbc = stbc << WMI_RC_RX_STBC_FLAG_S;
  1718. arg->peer_rate_caps |= stbc;
  1719. arg->peer_flags |= ar->wmi.peer_flags->stbc;
  1720. }
  1721. if (ht_cap->mcs.rx_mask[1] && ht_cap->mcs.rx_mask[2])
  1722. arg->peer_rate_caps |= WMI_RC_TS_FLAG;
  1723. else if (ht_cap->mcs.rx_mask[1])
  1724. arg->peer_rate_caps |= WMI_RC_DS_FLAG;
  1725. for (i = 0, n = 0, max_nss = 0; i < IEEE80211_HT_MCS_MASK_LEN * 8; i++)
  1726. if ((ht_cap->mcs.rx_mask[i / 8] & BIT(i % 8)) &&
  1727. (ht_mcs_mask[i / 8] & BIT(i % 8))) {
  1728. max_nss = (i / 8) + 1;
  1729. arg->peer_ht_rates.rates[n++] = i;
  1730. }
  1731. /*
  1732. * This is a workaround for HT-enabled STAs which break the spec
  1733. * and have no HT capabilities RX mask (no HT RX MCS map).
  1734. *
  1735. * As per spec, in section 20.3.5 Modulation and coding scheme (MCS),
  1736. * MCS 0 through 7 are mandatory in 20MHz with 800 ns GI at all STAs.
  1737. *
  1738. * Firmware asserts if such situation occurs.
  1739. */
  1740. if (n == 0) {
  1741. arg->peer_ht_rates.num_rates = 8;
  1742. for (i = 0; i < arg->peer_ht_rates.num_rates; i++)
  1743. arg->peer_ht_rates.rates[i] = i;
  1744. } else {
  1745. arg->peer_ht_rates.num_rates = n;
  1746. arg->peer_num_spatial_streams = min(sta->rx_nss, max_nss);
  1747. }
  1748. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac ht peer %pM mcs cnt %d nss %d\n",
  1749. arg->addr,
  1750. arg->peer_ht_rates.num_rates,
  1751. arg->peer_num_spatial_streams);
  1752. }
  1753. static int ath10k_peer_assoc_qos_ap(struct ath10k *ar,
  1754. struct ath10k_vif *arvif,
  1755. struct ieee80211_sta *sta)
  1756. {
  1757. u32 uapsd = 0;
  1758. u32 max_sp = 0;
  1759. int ret = 0;
  1760. lockdep_assert_held(&ar->conf_mutex);
  1761. if (sta->wme && sta->uapsd_queues) {
  1762. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac uapsd_queues 0x%x max_sp %d\n",
  1763. sta->uapsd_queues, sta->max_sp);
  1764. if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
  1765. uapsd |= WMI_AP_PS_UAPSD_AC3_DELIVERY_EN |
  1766. WMI_AP_PS_UAPSD_AC3_TRIGGER_EN;
  1767. if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
  1768. uapsd |= WMI_AP_PS_UAPSD_AC2_DELIVERY_EN |
  1769. WMI_AP_PS_UAPSD_AC2_TRIGGER_EN;
  1770. if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
  1771. uapsd |= WMI_AP_PS_UAPSD_AC1_DELIVERY_EN |
  1772. WMI_AP_PS_UAPSD_AC1_TRIGGER_EN;
  1773. if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
  1774. uapsd |= WMI_AP_PS_UAPSD_AC0_DELIVERY_EN |
  1775. WMI_AP_PS_UAPSD_AC0_TRIGGER_EN;
  1776. if (sta->max_sp < MAX_WMI_AP_PS_PEER_PARAM_MAX_SP)
  1777. max_sp = sta->max_sp;
  1778. ret = ath10k_wmi_set_ap_ps_param(ar, arvif->vdev_id,
  1779. sta->addr,
  1780. WMI_AP_PS_PEER_PARAM_UAPSD,
  1781. uapsd);
  1782. if (ret) {
  1783. ath10k_warn(ar, "failed to set ap ps peer param uapsd for vdev %i: %d\n",
  1784. arvif->vdev_id, ret);
  1785. return ret;
  1786. }
  1787. ret = ath10k_wmi_set_ap_ps_param(ar, arvif->vdev_id,
  1788. sta->addr,
  1789. WMI_AP_PS_PEER_PARAM_MAX_SP,
  1790. max_sp);
  1791. if (ret) {
  1792. ath10k_warn(ar, "failed to set ap ps peer param max sp for vdev %i: %d\n",
  1793. arvif->vdev_id, ret);
  1794. return ret;
  1795. }
  1796. /* TODO setup this based on STA listen interval and
  1797. beacon interval. Currently we don't know
  1798. sta->listen_interval - mac80211 patch required.
  1799. Currently use 10 seconds */
  1800. ret = ath10k_wmi_set_ap_ps_param(ar, arvif->vdev_id, sta->addr,
  1801. WMI_AP_PS_PEER_PARAM_AGEOUT_TIME,
  1802. 10);
  1803. if (ret) {
  1804. ath10k_warn(ar, "failed to set ap ps peer param ageout time for vdev %i: %d\n",
  1805. arvif->vdev_id, ret);
  1806. return ret;
  1807. }
  1808. }
  1809. return 0;
  1810. }
  1811. static u16
  1812. ath10k_peer_assoc_h_vht_limit(u16 tx_mcs_set,
  1813. const u16 vht_mcs_limit[NL80211_VHT_NSS_MAX])
  1814. {
  1815. int idx_limit;
  1816. int nss;
  1817. u16 mcs_map;
  1818. u16 mcs;
  1819. for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++) {
  1820. mcs_map = ath10k_mac_get_max_vht_mcs_map(tx_mcs_set, nss) &
  1821. vht_mcs_limit[nss];
  1822. if (mcs_map)
  1823. idx_limit = fls(mcs_map) - 1;
  1824. else
  1825. idx_limit = -1;
  1826. switch (idx_limit) {
  1827. case 0: /* fall through */
  1828. case 1: /* fall through */
  1829. case 2: /* fall through */
  1830. case 3: /* fall through */
  1831. case 4: /* fall through */
  1832. case 5: /* fall through */
  1833. case 6: /* fall through */
  1834. default:
  1835. /* see ath10k_mac_can_set_bitrate_mask() */
  1836. WARN_ON(1);
  1837. /* fall through */
  1838. case -1:
  1839. mcs = IEEE80211_VHT_MCS_NOT_SUPPORTED;
  1840. break;
  1841. case 7:
  1842. mcs = IEEE80211_VHT_MCS_SUPPORT_0_7;
  1843. break;
  1844. case 8:
  1845. mcs = IEEE80211_VHT_MCS_SUPPORT_0_8;
  1846. break;
  1847. case 9:
  1848. mcs = IEEE80211_VHT_MCS_SUPPORT_0_9;
  1849. break;
  1850. }
  1851. tx_mcs_set &= ~(0x3 << (nss * 2));
  1852. tx_mcs_set |= mcs << (nss * 2);
  1853. }
  1854. return tx_mcs_set;
  1855. }
  1856. static void ath10k_peer_assoc_h_vht(struct ath10k *ar,
  1857. struct ieee80211_vif *vif,
  1858. struct ieee80211_sta *sta,
  1859. struct wmi_peer_assoc_complete_arg *arg)
  1860. {
  1861. const struct ieee80211_sta_vht_cap *vht_cap = &sta->vht_cap;
  1862. struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
  1863. struct cfg80211_chan_def def;
  1864. enum ieee80211_band band;
  1865. const u16 *vht_mcs_mask;
  1866. u8 ampdu_factor;
  1867. if (WARN_ON(ath10k_mac_vif_chan(vif, &def)))
  1868. return;
  1869. if (!vht_cap->vht_supported)
  1870. return;
  1871. band = def.chan->band;
  1872. vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
  1873. if (ath10k_peer_assoc_h_vht_masked(vht_mcs_mask))
  1874. return;
  1875. arg->peer_flags |= ar->wmi.peer_flags->vht;
  1876. if (def.chan->band == IEEE80211_BAND_2GHZ)
  1877. arg->peer_flags |= ar->wmi.peer_flags->vht_2g;
  1878. arg->peer_vht_caps = vht_cap->cap;
  1879. ampdu_factor = (vht_cap->cap &
  1880. IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK) >>
  1881. IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
  1882. /* Workaround: Some Netgear/Linksys 11ac APs set Rx A-MPDU factor to
  1883. * zero in VHT IE. Using it would result in degraded throughput.
  1884. * arg->peer_max_mpdu at this point contains HT max_mpdu so keep
  1885. * it if VHT max_mpdu is smaller. */
  1886. arg->peer_max_mpdu = max(arg->peer_max_mpdu,
  1887. (1U << (IEEE80211_HT_MAX_AMPDU_FACTOR +
  1888. ampdu_factor)) - 1);
  1889. if (sta->bandwidth == IEEE80211_STA_RX_BW_80)
  1890. arg->peer_flags |= ar->wmi.peer_flags->bw80;
  1891. arg->peer_vht_rates.rx_max_rate =
  1892. __le16_to_cpu(vht_cap->vht_mcs.rx_highest);
  1893. arg->peer_vht_rates.rx_mcs_set =
  1894. __le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map);
  1895. arg->peer_vht_rates.tx_max_rate =
  1896. __le16_to_cpu(vht_cap->vht_mcs.tx_highest);
  1897. arg->peer_vht_rates.tx_mcs_set = ath10k_peer_assoc_h_vht_limit(
  1898. __le16_to_cpu(vht_cap->vht_mcs.tx_mcs_map), vht_mcs_mask);
  1899. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vht peer %pM max_mpdu %d flags 0x%x\n",
  1900. sta->addr, arg->peer_max_mpdu, arg->peer_flags);
  1901. }
  1902. static void ath10k_peer_assoc_h_qos(struct ath10k *ar,
  1903. struct ieee80211_vif *vif,
  1904. struct ieee80211_sta *sta,
  1905. struct wmi_peer_assoc_complete_arg *arg)
  1906. {
  1907. struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
  1908. switch (arvif->vdev_type) {
  1909. case WMI_VDEV_TYPE_AP:
  1910. if (sta->wme)
  1911. arg->peer_flags |= arvif->ar->wmi.peer_flags->qos;
  1912. if (sta->wme && sta->uapsd_queues) {
  1913. arg->peer_flags |= arvif->ar->wmi.peer_flags->apsd;
  1914. arg->peer_rate_caps |= WMI_RC_UAPSD_FLAG;
  1915. }
  1916. break;
  1917. case WMI_VDEV_TYPE_STA:
  1918. if (vif->bss_conf.qos)
  1919. arg->peer_flags |= arvif->ar->wmi.peer_flags->qos;
  1920. break;
  1921. case WMI_VDEV_TYPE_IBSS:
  1922. if (sta->wme)
  1923. arg->peer_flags |= arvif->ar->wmi.peer_flags->qos;
  1924. break;
  1925. default:
  1926. break;
  1927. }
  1928. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac peer %pM qos %d\n",
  1929. sta->addr, !!(arg->peer_flags &
  1930. arvif->ar->wmi.peer_flags->qos));
  1931. }
  1932. static bool ath10k_mac_sta_has_ofdm_only(struct ieee80211_sta *sta)
  1933. {
  1934. return sta->supp_rates[IEEE80211_BAND_2GHZ] >>
  1935. ATH10K_MAC_FIRST_OFDM_RATE_IDX;
  1936. }
  1937. static void ath10k_peer_assoc_h_phymode(struct ath10k *ar,
  1938. struct ieee80211_vif *vif,
  1939. struct ieee80211_sta *sta,
  1940. struct wmi_peer_assoc_complete_arg *arg)
  1941. {
  1942. struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
  1943. struct cfg80211_chan_def def;
  1944. enum ieee80211_band band;
  1945. const u8 *ht_mcs_mask;
  1946. const u16 *vht_mcs_mask;
  1947. enum wmi_phy_mode phymode = MODE_UNKNOWN;
  1948. if (WARN_ON(ath10k_mac_vif_chan(vif, &def)))
  1949. return;
  1950. band = def.chan->band;
  1951. ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
  1952. vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
  1953. switch (band) {
  1954. case IEEE80211_BAND_2GHZ:
  1955. if (sta->vht_cap.vht_supported &&
  1956. !ath10k_peer_assoc_h_vht_masked(vht_mcs_mask)) {
  1957. if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
  1958. phymode = MODE_11AC_VHT40;
  1959. else
  1960. phymode = MODE_11AC_VHT20;
  1961. } else if (sta->ht_cap.ht_supported &&
  1962. !ath10k_peer_assoc_h_ht_masked(ht_mcs_mask)) {
  1963. if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
  1964. phymode = MODE_11NG_HT40;
  1965. else
  1966. phymode = MODE_11NG_HT20;
  1967. } else if (ath10k_mac_sta_has_ofdm_only(sta)) {
  1968. phymode = MODE_11G;
  1969. } else {
  1970. phymode = MODE_11B;
  1971. }
  1972. break;
  1973. case IEEE80211_BAND_5GHZ:
  1974. /*
  1975. * Check VHT first.
  1976. */
  1977. if (sta->vht_cap.vht_supported &&
  1978. !ath10k_peer_assoc_h_vht_masked(vht_mcs_mask)) {
  1979. if (sta->bandwidth == IEEE80211_STA_RX_BW_80)
  1980. phymode = MODE_11AC_VHT80;
  1981. else if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
  1982. phymode = MODE_11AC_VHT40;
  1983. else if (sta->bandwidth == IEEE80211_STA_RX_BW_20)
  1984. phymode = MODE_11AC_VHT20;
  1985. } else if (sta->ht_cap.ht_supported &&
  1986. !ath10k_peer_assoc_h_ht_masked(ht_mcs_mask)) {
  1987. if (sta->bandwidth >= IEEE80211_STA_RX_BW_40)
  1988. phymode = MODE_11NA_HT40;
  1989. else
  1990. phymode = MODE_11NA_HT20;
  1991. } else {
  1992. phymode = MODE_11A;
  1993. }
  1994. break;
  1995. default:
  1996. break;
  1997. }
  1998. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac peer %pM phymode %s\n",
  1999. sta->addr, ath10k_wmi_phymode_str(phymode));
  2000. arg->peer_phymode = phymode;
  2001. WARN_ON(phymode == MODE_UNKNOWN);
  2002. }
  2003. static int ath10k_peer_assoc_prepare(struct ath10k *ar,
  2004. struct ieee80211_vif *vif,
  2005. struct ieee80211_sta *sta,
  2006. struct wmi_peer_assoc_complete_arg *arg)
  2007. {
  2008. lockdep_assert_held(&ar->conf_mutex);
  2009. memset(arg, 0, sizeof(*arg));
  2010. ath10k_peer_assoc_h_basic(ar, vif, sta, arg);
  2011. ath10k_peer_assoc_h_crypto(ar, vif, sta, arg);
  2012. ath10k_peer_assoc_h_rates(ar, vif, sta, arg);
  2013. ath10k_peer_assoc_h_ht(ar, vif, sta, arg);
  2014. ath10k_peer_assoc_h_vht(ar, vif, sta, arg);
  2015. ath10k_peer_assoc_h_qos(ar, vif, sta, arg);
  2016. ath10k_peer_assoc_h_phymode(ar, vif, sta, arg);
  2017. return 0;
  2018. }
  2019. static const u32 ath10k_smps_map[] = {
  2020. [WLAN_HT_CAP_SM_PS_STATIC] = WMI_PEER_SMPS_STATIC,
  2021. [WLAN_HT_CAP_SM_PS_DYNAMIC] = WMI_PEER_SMPS_DYNAMIC,
  2022. [WLAN_HT_CAP_SM_PS_INVALID] = WMI_PEER_SMPS_PS_NONE,
  2023. [WLAN_HT_CAP_SM_PS_DISABLED] = WMI_PEER_SMPS_PS_NONE,
  2024. };
  2025. static int ath10k_setup_peer_smps(struct ath10k *ar, struct ath10k_vif *arvif,
  2026. const u8 *addr,
  2027. const struct ieee80211_sta_ht_cap *ht_cap)
  2028. {
  2029. int smps;
  2030. if (!ht_cap->ht_supported)
  2031. return 0;
  2032. smps = ht_cap->cap & IEEE80211_HT_CAP_SM_PS;
  2033. smps >>= IEEE80211_HT_CAP_SM_PS_SHIFT;
  2034. if (smps >= ARRAY_SIZE(ath10k_smps_map))
  2035. return -EINVAL;
  2036. return ath10k_wmi_peer_set_param(ar, arvif->vdev_id, addr,
  2037. WMI_PEER_SMPS_STATE,
  2038. ath10k_smps_map[smps]);
  2039. }
  2040. static int ath10k_mac_vif_recalc_txbf(struct ath10k *ar,
  2041. struct ieee80211_vif *vif,
  2042. struct ieee80211_sta_vht_cap vht_cap)
  2043. {
  2044. struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
  2045. int ret;
  2046. u32 param;
  2047. u32 value;
  2048. if (ath10k_wmi_get_txbf_conf_scheme(ar) != WMI_TXBF_CONF_AFTER_ASSOC)
  2049. return 0;
  2050. if (!(ar->vht_cap_info &
  2051. (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
  2052. IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE |
  2053. IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE |
  2054. IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE)))
  2055. return 0;
  2056. param = ar->wmi.vdev_param->txbf;
  2057. value = 0;
  2058. if (WARN_ON(param == WMI_VDEV_PARAM_UNSUPPORTED))
  2059. return 0;
  2060. /* The following logic is correct. If a remote STA advertises support
  2061. * for being a beamformer then we should enable us being a beamformee.
  2062. */
  2063. if (ar->vht_cap_info &
  2064. (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
  2065. IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE)) {
  2066. if (vht_cap.cap & IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)
  2067. value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFEE;
  2068. if (vht_cap.cap & IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE)
  2069. value |= WMI_VDEV_PARAM_TXBF_MU_TX_BFEE;
  2070. }
  2071. if (ar->vht_cap_info &
  2072. (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE |
  2073. IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE)) {
  2074. if (vht_cap.cap & IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE)
  2075. value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFER;
  2076. if (vht_cap.cap & IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE)
  2077. value |= WMI_VDEV_PARAM_TXBF_MU_TX_BFER;
  2078. }
  2079. if (value & WMI_VDEV_PARAM_TXBF_MU_TX_BFEE)
  2080. value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFEE;
  2081. if (value & WMI_VDEV_PARAM_TXBF_MU_TX_BFER)
  2082. value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFER;
  2083. ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, param, value);
  2084. if (ret) {
  2085. ath10k_warn(ar, "failed to submit vdev param txbf 0x%x: %d\n",
  2086. value, ret);
  2087. return ret;
  2088. }
  2089. return 0;
  2090. }
  2091. /* can be called only in mac80211 callbacks due to `key_count` usage */
  2092. static void ath10k_bss_assoc(struct ieee80211_hw *hw,
  2093. struct ieee80211_vif *vif,
  2094. struct ieee80211_bss_conf *bss_conf)
  2095. {
  2096. struct ath10k *ar = hw->priv;
  2097. struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
  2098. struct ieee80211_sta_ht_cap ht_cap;
  2099. struct ieee80211_sta_vht_cap vht_cap;
  2100. struct wmi_peer_assoc_complete_arg peer_arg;
  2101. struct ieee80211_sta *ap_sta;
  2102. int ret;
  2103. lockdep_assert_held(&ar->conf_mutex);
  2104. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %i assoc bssid %pM aid %d\n",
  2105. arvif->vdev_id, arvif->bssid, arvif->aid);
  2106. rcu_read_lock();
  2107. ap_sta = ieee80211_find_sta(vif, bss_conf->bssid);
  2108. if (!ap_sta) {
  2109. ath10k_warn(ar, "failed to find station entry for bss %pM vdev %i\n",
  2110. bss_conf->bssid, arvif->vdev_id);
  2111. rcu_read_unlock();
  2112. return;
  2113. }
  2114. /* ap_sta must be accessed only within rcu section which must be left
  2115. * before calling ath10k_setup_peer_smps() which might sleep. */
  2116. ht_cap = ap_sta->ht_cap;
  2117. vht_cap = ap_sta->vht_cap;
  2118. ret = ath10k_peer_assoc_prepare(ar, vif, ap_sta, &peer_arg);
  2119. if (ret) {
  2120. ath10k_warn(ar, "failed to prepare peer assoc for %pM vdev %i: %d\n",
  2121. bss_conf->bssid, arvif->vdev_id, ret);
  2122. rcu_read_unlock();
  2123. return;
  2124. }
  2125. rcu_read_unlock();
  2126. ret = ath10k_wmi_peer_assoc(ar, &peer_arg);
  2127. if (ret) {
  2128. ath10k_warn(ar, "failed to run peer assoc for %pM vdev %i: %d\n",
  2129. bss_conf->bssid, arvif->vdev_id, ret);
  2130. return;
  2131. }
  2132. ret = ath10k_setup_peer_smps(ar, arvif, bss_conf->bssid, &ht_cap);
  2133. if (ret) {
  2134. ath10k_warn(ar, "failed to setup peer SMPS for vdev %i: %d\n",
  2135. arvif->vdev_id, ret);
  2136. return;
  2137. }
  2138. ret = ath10k_mac_vif_recalc_txbf(ar, vif, vht_cap);
  2139. if (ret) {
  2140. ath10k_warn(ar, "failed to recalc txbf for vdev %i on bss %pM: %d\n",
  2141. arvif->vdev_id, bss_conf->bssid, ret);
  2142. return;
  2143. }
  2144. ath10k_dbg(ar, ATH10K_DBG_MAC,
  2145. "mac vdev %d up (associated) bssid %pM aid %d\n",
  2146. arvif->vdev_id, bss_conf->bssid, bss_conf->aid);
  2147. WARN_ON(arvif->is_up);
  2148. arvif->aid = bss_conf->aid;
  2149. ether_addr_copy(arvif->bssid, bss_conf->bssid);
  2150. ret = ath10k_wmi_vdev_up(ar, arvif->vdev_id, arvif->aid, arvif->bssid);
  2151. if (ret) {
  2152. ath10k_warn(ar, "failed to set vdev %d up: %d\n",
  2153. arvif->vdev_id, ret);
  2154. return;
  2155. }
  2156. arvif->is_up = true;
  2157. /* Workaround: Some firmware revisions (tested with qca6174
  2158. * WLAN.RM.2.0-00073) have buggy powersave state machine and must be
  2159. * poked with peer param command.
  2160. */
  2161. ret = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, arvif->bssid,
  2162. WMI_PEER_DUMMY_VAR, 1);
  2163. if (ret) {
  2164. ath10k_warn(ar, "failed to poke peer %pM param for ps workaround on vdev %i: %d\n",
  2165. arvif->bssid, arvif->vdev_id, ret);
  2166. return;
  2167. }
  2168. }
  2169. static void ath10k_bss_disassoc(struct ieee80211_hw *hw,
  2170. struct ieee80211_vif *vif)
  2171. {
  2172. struct ath10k *ar = hw->priv;
  2173. struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
  2174. struct ieee80211_sta_vht_cap vht_cap = {};
  2175. int ret;
  2176. lockdep_assert_held(&ar->conf_mutex);
  2177. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %i disassoc bssid %pM\n",
  2178. arvif->vdev_id, arvif->bssid);
  2179. ret = ath10k_wmi_vdev_down(ar, arvif->vdev_id);
  2180. if (ret)
  2181. ath10k_warn(ar, "faield to down vdev %i: %d\n",
  2182. arvif->vdev_id, ret);
  2183. arvif->def_wep_key_idx = -1;
  2184. ret = ath10k_mac_vif_recalc_txbf(ar, vif, vht_cap);
  2185. if (ret) {
  2186. ath10k_warn(ar, "failed to recalc txbf for vdev %i: %d\n",
  2187. arvif->vdev_id, ret);
  2188. return;
  2189. }
  2190. arvif->is_up = false;
  2191. cancel_delayed_work_sync(&arvif->connection_loss_work);
  2192. }
  2193. static int ath10k_station_assoc(struct ath10k *ar,
  2194. struct ieee80211_vif *vif,
  2195. struct ieee80211_sta *sta,
  2196. bool reassoc)
  2197. {
  2198. struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
  2199. struct wmi_peer_assoc_complete_arg peer_arg;
  2200. int ret = 0;
  2201. lockdep_assert_held(&ar->conf_mutex);
  2202. ret = ath10k_peer_assoc_prepare(ar, vif, sta, &peer_arg);
  2203. if (ret) {
  2204. ath10k_warn(ar, "failed to prepare WMI peer assoc for %pM vdev %i: %i\n",
  2205. sta->addr, arvif->vdev_id, ret);
  2206. return ret;
  2207. }
  2208. ret = ath10k_wmi_peer_assoc(ar, &peer_arg);
  2209. if (ret) {
  2210. ath10k_warn(ar, "failed to run peer assoc for STA %pM vdev %i: %d\n",
  2211. sta->addr, arvif->vdev_id, ret);
  2212. return ret;
  2213. }
  2214. /* Re-assoc is run only to update supported rates for given station. It
  2215. * doesn't make much sense to reconfigure the peer completely.
  2216. */
  2217. if (!reassoc) {
  2218. ret = ath10k_setup_peer_smps(ar, arvif, sta->addr,
  2219. &sta->ht_cap);
  2220. if (ret) {
  2221. ath10k_warn(ar, "failed to setup peer SMPS for vdev %d: %d\n",
  2222. arvif->vdev_id, ret);
  2223. return ret;
  2224. }
  2225. ret = ath10k_peer_assoc_qos_ap(ar, arvif, sta);
  2226. if (ret) {
  2227. ath10k_warn(ar, "failed to set qos params for STA %pM for vdev %i: %d\n",
  2228. sta->addr, arvif->vdev_id, ret);
  2229. return ret;
  2230. }
  2231. if (!sta->wme) {
  2232. arvif->num_legacy_stations++;
  2233. ret = ath10k_recalc_rtscts_prot(arvif);
  2234. if (ret) {
  2235. ath10k_warn(ar, "failed to recalculate rts/cts prot for vdev %d: %d\n",
  2236. arvif->vdev_id, ret);
  2237. return ret;
  2238. }
  2239. }
  2240. /* Plumb cached keys only for static WEP */
  2241. if (arvif->def_wep_key_idx != -1) {
  2242. ret = ath10k_install_peer_wep_keys(arvif, sta->addr);
  2243. if (ret) {
  2244. ath10k_warn(ar, "failed to install peer wep keys for vdev %i: %d\n",
  2245. arvif->vdev_id, ret);
  2246. return ret;
  2247. }
  2248. }
  2249. }
  2250. return ret;
  2251. }
  2252. static int ath10k_station_disassoc(struct ath10k *ar,
  2253. struct ieee80211_vif *vif,
  2254. struct ieee80211_sta *sta)
  2255. {
  2256. struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
  2257. int ret = 0;
  2258. lockdep_assert_held(&ar->conf_mutex);
  2259. if (!sta->wme) {
  2260. arvif->num_legacy_stations--;
  2261. ret = ath10k_recalc_rtscts_prot(arvif);
  2262. if (ret) {
  2263. ath10k_warn(ar, "failed to recalculate rts/cts prot for vdev %d: %d\n",
  2264. arvif->vdev_id, ret);
  2265. return ret;
  2266. }
  2267. }
  2268. ret = ath10k_clear_peer_keys(arvif, sta->addr);
  2269. if (ret) {
  2270. ath10k_warn(ar, "failed to clear all peer wep keys for vdev %i: %d\n",
  2271. arvif->vdev_id, ret);
  2272. return ret;
  2273. }
  2274. return ret;
  2275. }
  2276. /**************/
  2277. /* Regulatory */
  2278. /**************/
  2279. static int ath10k_update_channel_list(struct ath10k *ar)
  2280. {
  2281. struct ieee80211_hw *hw = ar->hw;
  2282. struct ieee80211_supported_band **bands;
  2283. enum ieee80211_band band;
  2284. struct ieee80211_channel *channel;
  2285. struct wmi_scan_chan_list_arg arg = {0};
  2286. struct wmi_channel_arg *ch;
  2287. bool passive;
  2288. int len;
  2289. int ret;
  2290. int i;
  2291. lockdep_assert_held(&ar->conf_mutex);
  2292. bands = hw->wiphy->bands;
  2293. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  2294. if (!bands[band])
  2295. continue;
  2296. for (i = 0; i < bands[band]->n_channels; i++) {
  2297. if (bands[band]->channels[i].flags &
  2298. IEEE80211_CHAN_DISABLED)
  2299. continue;
  2300. arg.n_channels++;
  2301. }
  2302. }
  2303. len = sizeof(struct wmi_channel_arg) * arg.n_channels;
  2304. arg.channels = kzalloc(len, GFP_KERNEL);
  2305. if (!arg.channels)
  2306. return -ENOMEM;
  2307. ch = arg.channels;
  2308. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  2309. if (!bands[band])
  2310. continue;
  2311. for (i = 0; i < bands[band]->n_channels; i++) {
  2312. channel = &bands[band]->channels[i];
  2313. if (channel->flags & IEEE80211_CHAN_DISABLED)
  2314. continue;
  2315. ch->allow_ht = true;
  2316. /* FIXME: when should we really allow VHT? */
  2317. ch->allow_vht = true;
  2318. ch->allow_ibss =
  2319. !(channel->flags & IEEE80211_CHAN_NO_IR);
  2320. ch->ht40plus =
  2321. !(channel->flags & IEEE80211_CHAN_NO_HT40PLUS);
  2322. ch->chan_radar =
  2323. !!(channel->flags & IEEE80211_CHAN_RADAR);
  2324. passive = channel->flags & IEEE80211_CHAN_NO_IR;
  2325. ch->passive = passive;
  2326. ch->freq = channel->center_freq;
  2327. ch->band_center_freq1 = channel->center_freq;
  2328. ch->min_power = 0;
  2329. ch->max_power = channel->max_power * 2;
  2330. ch->max_reg_power = channel->max_reg_power * 2;
  2331. ch->max_antenna_gain = channel->max_antenna_gain * 2;
  2332. ch->reg_class_id = 0; /* FIXME */
  2333. /* FIXME: why use only legacy modes, why not any
  2334. * HT/VHT modes? Would that even make any
  2335. * difference? */
  2336. if (channel->band == IEEE80211_BAND_2GHZ)
  2337. ch->mode = MODE_11G;
  2338. else
  2339. ch->mode = MODE_11A;
  2340. if (WARN_ON_ONCE(ch->mode == MODE_UNKNOWN))
  2341. continue;
  2342. ath10k_dbg(ar, ATH10K_DBG_WMI,
  2343. "mac channel [%zd/%d] freq %d maxpower %d regpower %d antenna %d mode %d\n",
  2344. ch - arg.channels, arg.n_channels,
  2345. ch->freq, ch->max_power, ch->max_reg_power,
  2346. ch->max_antenna_gain, ch->mode);
  2347. ch++;
  2348. }
  2349. }
  2350. ret = ath10k_wmi_scan_chan_list(ar, &arg);
  2351. kfree(arg.channels);
  2352. return ret;
  2353. }
  2354. static enum wmi_dfs_region
  2355. ath10k_mac_get_dfs_region(enum nl80211_dfs_regions dfs_region)
  2356. {
  2357. switch (dfs_region) {
  2358. case NL80211_DFS_UNSET:
  2359. return WMI_UNINIT_DFS_DOMAIN;
  2360. case NL80211_DFS_FCC:
  2361. return WMI_FCC_DFS_DOMAIN;
  2362. case NL80211_DFS_ETSI:
  2363. return WMI_ETSI_DFS_DOMAIN;
  2364. case NL80211_DFS_JP:
  2365. return WMI_MKK4_DFS_DOMAIN;
  2366. }
  2367. return WMI_UNINIT_DFS_DOMAIN;
  2368. }
  2369. static void ath10k_regd_update(struct ath10k *ar)
  2370. {
  2371. struct reg_dmn_pair_mapping *regpair;
  2372. int ret;
  2373. enum wmi_dfs_region wmi_dfs_reg;
  2374. enum nl80211_dfs_regions nl_dfs_reg;
  2375. lockdep_assert_held(&ar->conf_mutex);
  2376. ret = ath10k_update_channel_list(ar);
  2377. if (ret)
  2378. ath10k_warn(ar, "failed to update channel list: %d\n", ret);
  2379. regpair = ar->ath_common.regulatory.regpair;
  2380. if (config_enabled(CONFIG_ATH10K_DFS_CERTIFIED) && ar->dfs_detector) {
  2381. nl_dfs_reg = ar->dfs_detector->region;
  2382. wmi_dfs_reg = ath10k_mac_get_dfs_region(nl_dfs_reg);
  2383. } else {
  2384. wmi_dfs_reg = WMI_UNINIT_DFS_DOMAIN;
  2385. }
  2386. /* Target allows setting up per-band regdomain but ath_common provides
  2387. * a combined one only */
  2388. ret = ath10k_wmi_pdev_set_regdomain(ar,
  2389. regpair->reg_domain,
  2390. regpair->reg_domain, /* 2ghz */
  2391. regpair->reg_domain, /* 5ghz */
  2392. regpair->reg_2ghz_ctl,
  2393. regpair->reg_5ghz_ctl,
  2394. wmi_dfs_reg);
  2395. if (ret)
  2396. ath10k_warn(ar, "failed to set pdev regdomain: %d\n", ret);
  2397. }
  2398. static void ath10k_reg_notifier(struct wiphy *wiphy,
  2399. struct regulatory_request *request)
  2400. {
  2401. struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
  2402. struct ath10k *ar = hw->priv;
  2403. bool result;
  2404. ath_reg_notifier_apply(wiphy, request, &ar->ath_common.regulatory);
  2405. if (config_enabled(CONFIG_ATH10K_DFS_CERTIFIED) && ar->dfs_detector) {
  2406. ath10k_dbg(ar, ATH10K_DBG_REGULATORY, "dfs region 0x%x\n",
  2407. request->dfs_region);
  2408. result = ar->dfs_detector->set_dfs_domain(ar->dfs_detector,
  2409. request->dfs_region);
  2410. if (!result)
  2411. ath10k_warn(ar, "DFS region 0x%X not supported, will trigger radar for every pulse\n",
  2412. request->dfs_region);
  2413. }
  2414. mutex_lock(&ar->conf_mutex);
  2415. if (ar->state == ATH10K_STATE_ON)
  2416. ath10k_regd_update(ar);
  2417. mutex_unlock(&ar->conf_mutex);
  2418. }
  2419. /***************/
  2420. /* TX handlers */
  2421. /***************/
  2422. void ath10k_mac_tx_lock(struct ath10k *ar, int reason)
  2423. {
  2424. lockdep_assert_held(&ar->htt.tx_lock);
  2425. WARN_ON(reason >= ATH10K_TX_PAUSE_MAX);
  2426. ar->tx_paused |= BIT(reason);
  2427. ieee80211_stop_queues(ar->hw);
  2428. }
  2429. static void ath10k_mac_tx_unlock_iter(void *data, u8 *mac,
  2430. struct ieee80211_vif *vif)
  2431. {
  2432. struct ath10k *ar = data;
  2433. struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
  2434. if (arvif->tx_paused)
  2435. return;
  2436. ieee80211_wake_queue(ar->hw, arvif->vdev_id);
  2437. }
  2438. void ath10k_mac_tx_unlock(struct ath10k *ar, int reason)
  2439. {
  2440. lockdep_assert_held(&ar->htt.tx_lock);
  2441. WARN_ON(reason >= ATH10K_TX_PAUSE_MAX);
  2442. ar->tx_paused &= ~BIT(reason);
  2443. if (ar->tx_paused)
  2444. return;
  2445. ieee80211_iterate_active_interfaces_atomic(ar->hw,
  2446. IEEE80211_IFACE_ITER_RESUME_ALL,
  2447. ath10k_mac_tx_unlock_iter,
  2448. ar);
  2449. ieee80211_wake_queue(ar->hw, ar->hw->offchannel_tx_hw_queue);
  2450. }
  2451. void ath10k_mac_vif_tx_lock(struct ath10k_vif *arvif, int reason)
  2452. {
  2453. struct ath10k *ar = arvif->ar;
  2454. lockdep_assert_held(&ar->htt.tx_lock);
  2455. WARN_ON(reason >= BITS_PER_LONG);
  2456. arvif->tx_paused |= BIT(reason);
  2457. ieee80211_stop_queue(ar->hw, arvif->vdev_id);
  2458. }
  2459. void ath10k_mac_vif_tx_unlock(struct ath10k_vif *arvif, int reason)
  2460. {
  2461. struct ath10k *ar = arvif->ar;
  2462. lockdep_assert_held(&ar->htt.tx_lock);
  2463. WARN_ON(reason >= BITS_PER_LONG);
  2464. arvif->tx_paused &= ~BIT(reason);
  2465. if (ar->tx_paused)
  2466. return;
  2467. if (arvif->tx_paused)
  2468. return;
  2469. ieee80211_wake_queue(ar->hw, arvif->vdev_id);
  2470. }
  2471. static void ath10k_mac_vif_handle_tx_pause(struct ath10k_vif *arvif,
  2472. enum wmi_tlv_tx_pause_id pause_id,
  2473. enum wmi_tlv_tx_pause_action action)
  2474. {
  2475. struct ath10k *ar = arvif->ar;
  2476. lockdep_assert_held(&ar->htt.tx_lock);
  2477. switch (action) {
  2478. case WMI_TLV_TX_PAUSE_ACTION_STOP:
  2479. ath10k_mac_vif_tx_lock(arvif, pause_id);
  2480. break;
  2481. case WMI_TLV_TX_PAUSE_ACTION_WAKE:
  2482. ath10k_mac_vif_tx_unlock(arvif, pause_id);
  2483. break;
  2484. default:
  2485. ath10k_warn(ar, "received unknown tx pause action %d on vdev %i, ignoring\n",
  2486. action, arvif->vdev_id);
  2487. break;
  2488. }
  2489. }
  2490. struct ath10k_mac_tx_pause {
  2491. u32 vdev_id;
  2492. enum wmi_tlv_tx_pause_id pause_id;
  2493. enum wmi_tlv_tx_pause_action action;
  2494. };
  2495. static void ath10k_mac_handle_tx_pause_iter(void *data, u8 *mac,
  2496. struct ieee80211_vif *vif)
  2497. {
  2498. struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
  2499. struct ath10k_mac_tx_pause *arg = data;
  2500. if (arvif->vdev_id != arg->vdev_id)
  2501. return;
  2502. ath10k_mac_vif_handle_tx_pause(arvif, arg->pause_id, arg->action);
  2503. }
  2504. void ath10k_mac_handle_tx_pause_vdev(struct ath10k *ar, u32 vdev_id,
  2505. enum wmi_tlv_tx_pause_id pause_id,
  2506. enum wmi_tlv_tx_pause_action action)
  2507. {
  2508. struct ath10k_mac_tx_pause arg = {
  2509. .vdev_id = vdev_id,
  2510. .pause_id = pause_id,
  2511. .action = action,
  2512. };
  2513. spin_lock_bh(&ar->htt.tx_lock);
  2514. ieee80211_iterate_active_interfaces_atomic(ar->hw,
  2515. IEEE80211_IFACE_ITER_RESUME_ALL,
  2516. ath10k_mac_handle_tx_pause_iter,
  2517. &arg);
  2518. spin_unlock_bh(&ar->htt.tx_lock);
  2519. }
  2520. static u8 ath10k_tx_h_get_tid(struct ieee80211_hdr *hdr)
  2521. {
  2522. if (ieee80211_is_mgmt(hdr->frame_control))
  2523. return HTT_DATA_TX_EXT_TID_MGMT;
  2524. if (!ieee80211_is_data_qos(hdr->frame_control))
  2525. return HTT_DATA_TX_EXT_TID_NON_QOS_MCAST_BCAST;
  2526. if (!is_unicast_ether_addr(ieee80211_get_DA(hdr)))
  2527. return HTT_DATA_TX_EXT_TID_NON_QOS_MCAST_BCAST;
  2528. return ieee80211_get_qos_ctl(hdr)[0] & IEEE80211_QOS_CTL_TID_MASK;
  2529. }
  2530. static u8 ath10k_tx_h_get_vdev_id(struct ath10k *ar, struct ieee80211_vif *vif)
  2531. {
  2532. if (vif)
  2533. return ath10k_vif_to_arvif(vif)->vdev_id;
  2534. if (ar->monitor_started)
  2535. return ar->monitor_vdev_id;
  2536. ath10k_warn(ar, "failed to resolve vdev id\n");
  2537. return 0;
  2538. }
  2539. static enum ath10k_hw_txrx_mode
  2540. ath10k_tx_h_get_txmode(struct ath10k *ar, struct ieee80211_vif *vif,
  2541. struct ieee80211_sta *sta, struct sk_buff *skb)
  2542. {
  2543. const struct ieee80211_hdr *hdr = (void *)skb->data;
  2544. __le16 fc = hdr->frame_control;
  2545. if (!vif || vif->type == NL80211_IFTYPE_MONITOR)
  2546. return ATH10K_HW_TXRX_RAW;
  2547. if (ieee80211_is_mgmt(fc))
  2548. return ATH10K_HW_TXRX_MGMT;
  2549. /* Workaround:
  2550. *
  2551. * NullFunc frames are mostly used to ping if a client or AP are still
  2552. * reachable and responsive. This implies tx status reports must be
  2553. * accurate - otherwise either mac80211 or userspace (e.g. hostapd) can
  2554. * come to a conclusion that the other end disappeared and tear down
  2555. * BSS connection or it can never disconnect from BSS/client (which is
  2556. * the case).
  2557. *
  2558. * Firmware with HTT older than 3.0 delivers incorrect tx status for
  2559. * NullFunc frames to driver. However there's a HTT Mgmt Tx command
  2560. * which seems to deliver correct tx reports for NullFunc frames. The
  2561. * downside of using it is it ignores client powersave state so it can
  2562. * end up disconnecting sleeping clients in AP mode. It should fix STA
  2563. * mode though because AP don't sleep.
  2564. */
  2565. if (ar->htt.target_version_major < 3 &&
  2566. (ieee80211_is_nullfunc(fc) || ieee80211_is_qos_nullfunc(fc)) &&
  2567. !test_bit(ATH10K_FW_FEATURE_HAS_WMI_MGMT_TX, ar->fw_features))
  2568. return ATH10K_HW_TXRX_MGMT;
  2569. /* Workaround:
  2570. *
  2571. * Some wmi-tlv firmwares for qca6174 have broken Tx key selection for
  2572. * NativeWifi txmode - it selects AP key instead of peer key. It seems
  2573. * to work with Ethernet txmode so use it.
  2574. *
  2575. * FIXME: Check if raw mode works with TDLS.
  2576. */
  2577. if (ieee80211_is_data_present(fc) && sta && sta->tdls)
  2578. return ATH10K_HW_TXRX_ETHERNET;
  2579. if (test_bit(ATH10K_FLAG_RAW_MODE, &ar->dev_flags))
  2580. return ATH10K_HW_TXRX_RAW;
  2581. return ATH10K_HW_TXRX_NATIVE_WIFI;
  2582. }
  2583. static bool ath10k_tx_h_use_hwcrypto(struct ieee80211_vif *vif,
  2584. struct sk_buff *skb)
  2585. {
  2586. const struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  2587. const struct ieee80211_hdr *hdr = (void *)skb->data;
  2588. const u32 mask = IEEE80211_TX_INTFL_DONT_ENCRYPT |
  2589. IEEE80211_TX_CTL_INJECTED;
  2590. if (!ieee80211_has_protected(hdr->frame_control))
  2591. return false;
  2592. if ((info->flags & mask) == mask)
  2593. return false;
  2594. if (vif)
  2595. return !ath10k_vif_to_arvif(vif)->nohwcrypt;
  2596. return true;
  2597. }
  2598. /* HTT Tx uses Native Wifi tx mode which expects 802.11 frames without QoS
  2599. * Control in the header.
  2600. */
  2601. static void ath10k_tx_h_nwifi(struct ieee80211_hw *hw, struct sk_buff *skb)
  2602. {
  2603. struct ieee80211_hdr *hdr = (void *)skb->data;
  2604. struct ath10k_skb_cb *cb = ATH10K_SKB_CB(skb);
  2605. u8 *qos_ctl;
  2606. if (!ieee80211_is_data_qos(hdr->frame_control))
  2607. return;
  2608. qos_ctl = ieee80211_get_qos_ctl(hdr);
  2609. memmove(skb->data + IEEE80211_QOS_CTL_LEN,
  2610. skb->data, (void *)qos_ctl - (void *)skb->data);
  2611. skb_pull(skb, IEEE80211_QOS_CTL_LEN);
  2612. /* Some firmware revisions don't handle sending QoS NullFunc well.
  2613. * These frames are mainly used for CQM purposes so it doesn't really
  2614. * matter whether QoS NullFunc or NullFunc are sent.
  2615. */
  2616. hdr = (void *)skb->data;
  2617. if (ieee80211_is_qos_nullfunc(hdr->frame_control))
  2618. cb->htt.tid = HTT_DATA_TX_EXT_TID_NON_QOS_MCAST_BCAST;
  2619. hdr->frame_control &= ~__cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
  2620. }
  2621. static void ath10k_tx_h_8023(struct sk_buff *skb)
  2622. {
  2623. struct ieee80211_hdr *hdr;
  2624. struct rfc1042_hdr *rfc1042;
  2625. struct ethhdr *eth;
  2626. size_t hdrlen;
  2627. u8 da[ETH_ALEN];
  2628. u8 sa[ETH_ALEN];
  2629. __be16 type;
  2630. hdr = (void *)skb->data;
  2631. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  2632. rfc1042 = (void *)skb->data + hdrlen;
  2633. ether_addr_copy(da, ieee80211_get_DA(hdr));
  2634. ether_addr_copy(sa, ieee80211_get_SA(hdr));
  2635. type = rfc1042->snap_type;
  2636. skb_pull(skb, hdrlen + sizeof(*rfc1042));
  2637. skb_push(skb, sizeof(*eth));
  2638. eth = (void *)skb->data;
  2639. ether_addr_copy(eth->h_dest, da);
  2640. ether_addr_copy(eth->h_source, sa);
  2641. eth->h_proto = type;
  2642. }
  2643. static void ath10k_tx_h_add_p2p_noa_ie(struct ath10k *ar,
  2644. struct ieee80211_vif *vif,
  2645. struct sk_buff *skb)
  2646. {
  2647. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  2648. struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
  2649. /* This is case only for P2P_GO */
  2650. if (arvif->vdev_type != WMI_VDEV_TYPE_AP ||
  2651. arvif->vdev_subtype != WMI_VDEV_SUBTYPE_P2P_GO)
  2652. return;
  2653. if (unlikely(ieee80211_is_probe_resp(hdr->frame_control))) {
  2654. spin_lock_bh(&ar->data_lock);
  2655. if (arvif->u.ap.noa_data)
  2656. if (!pskb_expand_head(skb, 0, arvif->u.ap.noa_len,
  2657. GFP_ATOMIC))
  2658. memcpy(skb_put(skb, arvif->u.ap.noa_len),
  2659. arvif->u.ap.noa_data,
  2660. arvif->u.ap.noa_len);
  2661. spin_unlock_bh(&ar->data_lock);
  2662. }
  2663. }
  2664. bool ath10k_mac_tx_frm_has_freq(struct ath10k *ar)
  2665. {
  2666. /* FIXME: Not really sure since when the behaviour changed. At some
  2667. * point new firmware stopped requiring creation of peer entries for
  2668. * offchannel tx (and actually creating them causes issues with wmi-htc
  2669. * tx credit replenishment and reliability). Assuming it's at least 3.4
  2670. * because that's when the `freq` was introduced to TX_FRM HTT command.
  2671. */
  2672. return (ar->htt.target_version_major >= 3 &&
  2673. ar->htt.target_version_minor >= 4 &&
  2674. ar->htt.op_version == ATH10K_FW_HTT_OP_VERSION_TLV);
  2675. }
  2676. static int ath10k_mac_tx_wmi_mgmt(struct ath10k *ar, struct sk_buff *skb)
  2677. {
  2678. struct sk_buff_head *q = &ar->wmi_mgmt_tx_queue;
  2679. int ret = 0;
  2680. spin_lock_bh(&ar->data_lock);
  2681. if (skb_queue_len(q) == ATH10K_MAX_NUM_MGMT_PENDING) {
  2682. ath10k_warn(ar, "wmi mgmt tx queue is full\n");
  2683. ret = -ENOSPC;
  2684. goto unlock;
  2685. }
  2686. __skb_queue_tail(q, skb);
  2687. ieee80211_queue_work(ar->hw, &ar->wmi_mgmt_tx_work);
  2688. unlock:
  2689. spin_unlock_bh(&ar->data_lock);
  2690. return ret;
  2691. }
  2692. static void ath10k_mac_tx(struct ath10k *ar, struct sk_buff *skb)
  2693. {
  2694. struct ath10k_skb_cb *cb = ATH10K_SKB_CB(skb);
  2695. struct ath10k_htt *htt = &ar->htt;
  2696. int ret = 0;
  2697. switch (cb->txmode) {
  2698. case ATH10K_HW_TXRX_RAW:
  2699. case ATH10K_HW_TXRX_NATIVE_WIFI:
  2700. case ATH10K_HW_TXRX_ETHERNET:
  2701. ret = ath10k_htt_tx(htt, skb);
  2702. break;
  2703. case ATH10K_HW_TXRX_MGMT:
  2704. if (test_bit(ATH10K_FW_FEATURE_HAS_WMI_MGMT_TX,
  2705. ar->fw_features))
  2706. ret = ath10k_mac_tx_wmi_mgmt(ar, skb);
  2707. else if (ar->htt.target_version_major >= 3)
  2708. ret = ath10k_htt_tx(htt, skb);
  2709. else
  2710. ret = ath10k_htt_mgmt_tx(htt, skb);
  2711. break;
  2712. }
  2713. if (ret) {
  2714. ath10k_warn(ar, "failed to transmit packet, dropping: %d\n",
  2715. ret);
  2716. ieee80211_free_txskb(ar->hw, skb);
  2717. }
  2718. }
  2719. void ath10k_offchan_tx_purge(struct ath10k *ar)
  2720. {
  2721. struct sk_buff *skb;
  2722. for (;;) {
  2723. skb = skb_dequeue(&ar->offchan_tx_queue);
  2724. if (!skb)
  2725. break;
  2726. ieee80211_free_txskb(ar->hw, skb);
  2727. }
  2728. }
  2729. void ath10k_offchan_tx_work(struct work_struct *work)
  2730. {
  2731. struct ath10k *ar = container_of(work, struct ath10k, offchan_tx_work);
  2732. struct ath10k_peer *peer;
  2733. struct ieee80211_hdr *hdr;
  2734. struct sk_buff *skb;
  2735. const u8 *peer_addr;
  2736. int vdev_id;
  2737. int ret;
  2738. unsigned long time_left;
  2739. bool tmp_peer_created = false;
  2740. /* FW requirement: We must create a peer before FW will send out
  2741. * an offchannel frame. Otherwise the frame will be stuck and
  2742. * never transmitted. We delete the peer upon tx completion.
  2743. * It is unlikely that a peer for offchannel tx will already be
  2744. * present. However it may be in some rare cases so account for that.
  2745. * Otherwise we might remove a legitimate peer and break stuff. */
  2746. for (;;) {
  2747. skb = skb_dequeue(&ar->offchan_tx_queue);
  2748. if (!skb)
  2749. break;
  2750. mutex_lock(&ar->conf_mutex);
  2751. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac offchannel skb %p\n",
  2752. skb);
  2753. hdr = (struct ieee80211_hdr *)skb->data;
  2754. peer_addr = ieee80211_get_DA(hdr);
  2755. vdev_id = ATH10K_SKB_CB(skb)->vdev_id;
  2756. spin_lock_bh(&ar->data_lock);
  2757. peer = ath10k_peer_find(ar, vdev_id, peer_addr);
  2758. spin_unlock_bh(&ar->data_lock);
  2759. if (peer)
  2760. /* FIXME: should this use ath10k_warn()? */
  2761. ath10k_dbg(ar, ATH10K_DBG_MAC, "peer %pM on vdev %d already present\n",
  2762. peer_addr, vdev_id);
  2763. if (!peer) {
  2764. ret = ath10k_peer_create(ar, vdev_id, peer_addr,
  2765. WMI_PEER_TYPE_DEFAULT);
  2766. if (ret)
  2767. ath10k_warn(ar, "failed to create peer %pM on vdev %d: %d\n",
  2768. peer_addr, vdev_id, ret);
  2769. tmp_peer_created = (ret == 0);
  2770. }
  2771. spin_lock_bh(&ar->data_lock);
  2772. reinit_completion(&ar->offchan_tx_completed);
  2773. ar->offchan_tx_skb = skb;
  2774. spin_unlock_bh(&ar->data_lock);
  2775. ath10k_mac_tx(ar, skb);
  2776. time_left =
  2777. wait_for_completion_timeout(&ar->offchan_tx_completed, 3 * HZ);
  2778. if (time_left == 0)
  2779. ath10k_warn(ar, "timed out waiting for offchannel skb %p\n",
  2780. skb);
  2781. if (!peer && tmp_peer_created) {
  2782. ret = ath10k_peer_delete(ar, vdev_id, peer_addr);
  2783. if (ret)
  2784. ath10k_warn(ar, "failed to delete peer %pM on vdev %d: %d\n",
  2785. peer_addr, vdev_id, ret);
  2786. }
  2787. mutex_unlock(&ar->conf_mutex);
  2788. }
  2789. }
  2790. void ath10k_mgmt_over_wmi_tx_purge(struct ath10k *ar)
  2791. {
  2792. struct sk_buff *skb;
  2793. for (;;) {
  2794. skb = skb_dequeue(&ar->wmi_mgmt_tx_queue);
  2795. if (!skb)
  2796. break;
  2797. ieee80211_free_txskb(ar->hw, skb);
  2798. }
  2799. }
  2800. void ath10k_mgmt_over_wmi_tx_work(struct work_struct *work)
  2801. {
  2802. struct ath10k *ar = container_of(work, struct ath10k, wmi_mgmt_tx_work);
  2803. struct sk_buff *skb;
  2804. int ret;
  2805. for (;;) {
  2806. skb = skb_dequeue(&ar->wmi_mgmt_tx_queue);
  2807. if (!skb)
  2808. break;
  2809. ret = ath10k_wmi_mgmt_tx(ar, skb);
  2810. if (ret) {
  2811. ath10k_warn(ar, "failed to transmit management frame via WMI: %d\n",
  2812. ret);
  2813. ieee80211_free_txskb(ar->hw, skb);
  2814. }
  2815. }
  2816. }
  2817. /************/
  2818. /* Scanning */
  2819. /************/
  2820. void __ath10k_scan_finish(struct ath10k *ar)
  2821. {
  2822. lockdep_assert_held(&ar->data_lock);
  2823. switch (ar->scan.state) {
  2824. case ATH10K_SCAN_IDLE:
  2825. break;
  2826. case ATH10K_SCAN_RUNNING:
  2827. case ATH10K_SCAN_ABORTING:
  2828. if (!ar->scan.is_roc)
  2829. ieee80211_scan_completed(ar->hw,
  2830. (ar->scan.state ==
  2831. ATH10K_SCAN_ABORTING));
  2832. else if (ar->scan.roc_notify)
  2833. ieee80211_remain_on_channel_expired(ar->hw);
  2834. /* fall through */
  2835. case ATH10K_SCAN_STARTING:
  2836. ar->scan.state = ATH10K_SCAN_IDLE;
  2837. ar->scan_channel = NULL;
  2838. ath10k_offchan_tx_purge(ar);
  2839. cancel_delayed_work(&ar->scan.timeout);
  2840. complete_all(&ar->scan.completed);
  2841. break;
  2842. }
  2843. }
  2844. void ath10k_scan_finish(struct ath10k *ar)
  2845. {
  2846. spin_lock_bh(&ar->data_lock);
  2847. __ath10k_scan_finish(ar);
  2848. spin_unlock_bh(&ar->data_lock);
  2849. }
  2850. static int ath10k_scan_stop(struct ath10k *ar)
  2851. {
  2852. struct wmi_stop_scan_arg arg = {
  2853. .req_id = 1, /* FIXME */
  2854. .req_type = WMI_SCAN_STOP_ONE,
  2855. .u.scan_id = ATH10K_SCAN_ID,
  2856. };
  2857. int ret;
  2858. lockdep_assert_held(&ar->conf_mutex);
  2859. ret = ath10k_wmi_stop_scan(ar, &arg);
  2860. if (ret) {
  2861. ath10k_warn(ar, "failed to stop wmi scan: %d\n", ret);
  2862. goto out;
  2863. }
  2864. ret = wait_for_completion_timeout(&ar->scan.completed, 3*HZ);
  2865. if (ret == 0) {
  2866. ath10k_warn(ar, "failed to receive scan abortion completion: timed out\n");
  2867. ret = -ETIMEDOUT;
  2868. } else if (ret > 0) {
  2869. ret = 0;
  2870. }
  2871. out:
  2872. /* Scan state should be updated upon scan completion but in case
  2873. * firmware fails to deliver the event (for whatever reason) it is
  2874. * desired to clean up scan state anyway. Firmware may have just
  2875. * dropped the scan completion event delivery due to transport pipe
  2876. * being overflown with data and/or it can recover on its own before
  2877. * next scan request is submitted.
  2878. */
  2879. spin_lock_bh(&ar->data_lock);
  2880. if (ar->scan.state != ATH10K_SCAN_IDLE)
  2881. __ath10k_scan_finish(ar);
  2882. spin_unlock_bh(&ar->data_lock);
  2883. return ret;
  2884. }
  2885. static void ath10k_scan_abort(struct ath10k *ar)
  2886. {
  2887. int ret;
  2888. lockdep_assert_held(&ar->conf_mutex);
  2889. spin_lock_bh(&ar->data_lock);
  2890. switch (ar->scan.state) {
  2891. case ATH10K_SCAN_IDLE:
  2892. /* This can happen if timeout worker kicked in and called
  2893. * abortion while scan completion was being processed.
  2894. */
  2895. break;
  2896. case ATH10K_SCAN_STARTING:
  2897. case ATH10K_SCAN_ABORTING:
  2898. ath10k_warn(ar, "refusing scan abortion due to invalid scan state: %s (%d)\n",
  2899. ath10k_scan_state_str(ar->scan.state),
  2900. ar->scan.state);
  2901. break;
  2902. case ATH10K_SCAN_RUNNING:
  2903. ar->scan.state = ATH10K_SCAN_ABORTING;
  2904. spin_unlock_bh(&ar->data_lock);
  2905. ret = ath10k_scan_stop(ar);
  2906. if (ret)
  2907. ath10k_warn(ar, "failed to abort scan: %d\n", ret);
  2908. spin_lock_bh(&ar->data_lock);
  2909. break;
  2910. }
  2911. spin_unlock_bh(&ar->data_lock);
  2912. }
  2913. void ath10k_scan_timeout_work(struct work_struct *work)
  2914. {
  2915. struct ath10k *ar = container_of(work, struct ath10k,
  2916. scan.timeout.work);
  2917. mutex_lock(&ar->conf_mutex);
  2918. ath10k_scan_abort(ar);
  2919. mutex_unlock(&ar->conf_mutex);
  2920. }
  2921. static int ath10k_start_scan(struct ath10k *ar,
  2922. const struct wmi_start_scan_arg *arg)
  2923. {
  2924. int ret;
  2925. lockdep_assert_held(&ar->conf_mutex);
  2926. ret = ath10k_wmi_start_scan(ar, arg);
  2927. if (ret)
  2928. return ret;
  2929. ret = wait_for_completion_timeout(&ar->scan.started, 1*HZ);
  2930. if (ret == 0) {
  2931. ret = ath10k_scan_stop(ar);
  2932. if (ret)
  2933. ath10k_warn(ar, "failed to stop scan: %d\n", ret);
  2934. return -ETIMEDOUT;
  2935. }
  2936. /* If we failed to start the scan, return error code at
  2937. * this point. This is probably due to some issue in the
  2938. * firmware, but no need to wedge the driver due to that...
  2939. */
  2940. spin_lock_bh(&ar->data_lock);
  2941. if (ar->scan.state == ATH10K_SCAN_IDLE) {
  2942. spin_unlock_bh(&ar->data_lock);
  2943. return -EINVAL;
  2944. }
  2945. spin_unlock_bh(&ar->data_lock);
  2946. return 0;
  2947. }
  2948. /**********************/
  2949. /* mac80211 callbacks */
  2950. /**********************/
  2951. static void ath10k_tx(struct ieee80211_hw *hw,
  2952. struct ieee80211_tx_control *control,
  2953. struct sk_buff *skb)
  2954. {
  2955. struct ath10k *ar = hw->priv;
  2956. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  2957. struct ieee80211_vif *vif = info->control.vif;
  2958. struct ieee80211_sta *sta = control->sta;
  2959. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  2960. /* We should disable CCK RATE due to P2P */
  2961. if (info->flags & IEEE80211_TX_CTL_NO_CCK_RATE)
  2962. ath10k_dbg(ar, ATH10K_DBG_MAC, "IEEE80211_TX_CTL_NO_CCK_RATE\n");
  2963. ATH10K_SKB_CB(skb)->htt.is_offchan = false;
  2964. ATH10K_SKB_CB(skb)->htt.freq = 0;
  2965. ATH10K_SKB_CB(skb)->htt.tid = ath10k_tx_h_get_tid(hdr);
  2966. ATH10K_SKB_CB(skb)->htt.nohwcrypt = !ath10k_tx_h_use_hwcrypto(vif, skb);
  2967. ATH10K_SKB_CB(skb)->vdev_id = ath10k_tx_h_get_vdev_id(ar, vif);
  2968. ATH10K_SKB_CB(skb)->txmode = ath10k_tx_h_get_txmode(ar, vif, sta, skb);
  2969. switch (ATH10K_SKB_CB(skb)->txmode) {
  2970. case ATH10K_HW_TXRX_MGMT:
  2971. case ATH10K_HW_TXRX_NATIVE_WIFI:
  2972. ath10k_tx_h_nwifi(hw, skb);
  2973. ath10k_tx_h_add_p2p_noa_ie(ar, vif, skb);
  2974. ath10k_tx_h_seq_no(vif, skb);
  2975. break;
  2976. case ATH10K_HW_TXRX_ETHERNET:
  2977. ath10k_tx_h_8023(skb);
  2978. break;
  2979. case ATH10K_HW_TXRX_RAW:
  2980. if (!test_bit(ATH10K_FLAG_RAW_MODE, &ar->dev_flags)) {
  2981. WARN_ON_ONCE(1);
  2982. ieee80211_free_txskb(hw, skb);
  2983. return;
  2984. }
  2985. }
  2986. if (info->flags & IEEE80211_TX_CTL_TX_OFFCHAN) {
  2987. spin_lock_bh(&ar->data_lock);
  2988. ATH10K_SKB_CB(skb)->htt.freq = ar->scan.roc_freq;
  2989. ATH10K_SKB_CB(skb)->vdev_id = ar->scan.vdev_id;
  2990. spin_unlock_bh(&ar->data_lock);
  2991. if (!ath10k_mac_tx_frm_has_freq(ar)) {
  2992. ATH10K_SKB_CB(skb)->htt.freq = 0;
  2993. ATH10K_SKB_CB(skb)->htt.is_offchan = true;
  2994. ath10k_dbg(ar, ATH10K_DBG_MAC, "queued offchannel skb %p\n",
  2995. skb);
  2996. skb_queue_tail(&ar->offchan_tx_queue, skb);
  2997. ieee80211_queue_work(hw, &ar->offchan_tx_work);
  2998. return;
  2999. }
  3000. }
  3001. ath10k_mac_tx(ar, skb);
  3002. }
  3003. /* Must not be called with conf_mutex held as workers can use that also. */
  3004. void ath10k_drain_tx(struct ath10k *ar)
  3005. {
  3006. /* make sure rcu-protected mac80211 tx path itself is drained */
  3007. synchronize_net();
  3008. ath10k_offchan_tx_purge(ar);
  3009. ath10k_mgmt_over_wmi_tx_purge(ar);
  3010. cancel_work_sync(&ar->offchan_tx_work);
  3011. cancel_work_sync(&ar->wmi_mgmt_tx_work);
  3012. }
  3013. void ath10k_halt(struct ath10k *ar)
  3014. {
  3015. struct ath10k_vif *arvif;
  3016. lockdep_assert_held(&ar->conf_mutex);
  3017. clear_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
  3018. ar->filter_flags = 0;
  3019. ar->monitor = false;
  3020. ar->monitor_arvif = NULL;
  3021. if (ar->monitor_started)
  3022. ath10k_monitor_stop(ar);
  3023. ar->monitor_started = false;
  3024. ar->tx_paused = 0;
  3025. ath10k_scan_finish(ar);
  3026. ath10k_peer_cleanup_all(ar);
  3027. ath10k_core_stop(ar);
  3028. ath10k_hif_power_down(ar);
  3029. spin_lock_bh(&ar->data_lock);
  3030. list_for_each_entry(arvif, &ar->arvifs, list)
  3031. ath10k_mac_vif_beacon_cleanup(arvif);
  3032. spin_unlock_bh(&ar->data_lock);
  3033. }
  3034. static int ath10k_get_antenna(struct ieee80211_hw *hw, u32 *tx_ant, u32 *rx_ant)
  3035. {
  3036. struct ath10k *ar = hw->priv;
  3037. mutex_lock(&ar->conf_mutex);
  3038. *tx_ant = ar->cfg_tx_chainmask;
  3039. *rx_ant = ar->cfg_rx_chainmask;
  3040. mutex_unlock(&ar->conf_mutex);
  3041. return 0;
  3042. }
  3043. static void ath10k_check_chain_mask(struct ath10k *ar, u32 cm, const char *dbg)
  3044. {
  3045. /* It is not clear that allowing gaps in chainmask
  3046. * is helpful. Probably it will not do what user
  3047. * is hoping for, so warn in that case.
  3048. */
  3049. if (cm == 15 || cm == 7 || cm == 3 || cm == 1 || cm == 0)
  3050. return;
  3051. ath10k_warn(ar, "mac %s antenna chainmask may be invalid: 0x%x. Suggested values: 15, 7, 3, 1 or 0.\n",
  3052. dbg, cm);
  3053. }
  3054. static int ath10k_mac_get_vht_cap_bf_sts(struct ath10k *ar)
  3055. {
  3056. int nsts = ar->vht_cap_info;
  3057. nsts &= IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK;
  3058. nsts >>= IEEE80211_VHT_CAP_BEAMFORMEE_STS_SHIFT;
  3059. /* If firmware does not deliver to host number of space-time
  3060. * streams supported, assume it support up to 4 BF STS and return
  3061. * the value for VHT CAP: nsts-1)
  3062. */
  3063. if (nsts == 0)
  3064. return 3;
  3065. return nsts;
  3066. }
  3067. static int ath10k_mac_get_vht_cap_bf_sound_dim(struct ath10k *ar)
  3068. {
  3069. int sound_dim = ar->vht_cap_info;
  3070. sound_dim &= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK;
  3071. sound_dim >>= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_SHIFT;
  3072. /* If the sounding dimension is not advertised by the firmware,
  3073. * let's use a default value of 1
  3074. */
  3075. if (sound_dim == 0)
  3076. return 1;
  3077. return sound_dim;
  3078. }
  3079. static struct ieee80211_sta_vht_cap ath10k_create_vht_cap(struct ath10k *ar)
  3080. {
  3081. struct ieee80211_sta_vht_cap vht_cap = {0};
  3082. u16 mcs_map;
  3083. u32 val;
  3084. int i;
  3085. vht_cap.vht_supported = 1;
  3086. vht_cap.cap = ar->vht_cap_info;
  3087. if (ar->vht_cap_info & (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
  3088. IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE)) {
  3089. val = ath10k_mac_get_vht_cap_bf_sts(ar);
  3090. val <<= IEEE80211_VHT_CAP_BEAMFORMEE_STS_SHIFT;
  3091. val &= IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK;
  3092. vht_cap.cap |= val;
  3093. }
  3094. if (ar->vht_cap_info & (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE |
  3095. IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE)) {
  3096. val = ath10k_mac_get_vht_cap_bf_sound_dim(ar);
  3097. val <<= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_SHIFT;
  3098. val &= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK;
  3099. vht_cap.cap |= val;
  3100. }
  3101. mcs_map = 0;
  3102. for (i = 0; i < 8; i++) {
  3103. if ((i < ar->num_rf_chains) && (ar->cfg_tx_chainmask & BIT(i)))
  3104. mcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i * 2);
  3105. else
  3106. mcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i * 2);
  3107. }
  3108. vht_cap.vht_mcs.rx_mcs_map = cpu_to_le16(mcs_map);
  3109. vht_cap.vht_mcs.tx_mcs_map = cpu_to_le16(mcs_map);
  3110. return vht_cap;
  3111. }
  3112. static struct ieee80211_sta_ht_cap ath10k_get_ht_cap(struct ath10k *ar)
  3113. {
  3114. int i;
  3115. struct ieee80211_sta_ht_cap ht_cap = {0};
  3116. if (!(ar->ht_cap_info & WMI_HT_CAP_ENABLED))
  3117. return ht_cap;
  3118. ht_cap.ht_supported = 1;
  3119. ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
  3120. ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_8;
  3121. ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
  3122. ht_cap.cap |= IEEE80211_HT_CAP_DSSSCCK40;
  3123. ht_cap.cap |= WLAN_HT_CAP_SM_PS_STATIC << IEEE80211_HT_CAP_SM_PS_SHIFT;
  3124. if (ar->ht_cap_info & WMI_HT_CAP_HT20_SGI)
  3125. ht_cap.cap |= IEEE80211_HT_CAP_SGI_20;
  3126. if (ar->ht_cap_info & WMI_HT_CAP_HT40_SGI)
  3127. ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
  3128. if (ar->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS) {
  3129. u32 smps;
  3130. smps = WLAN_HT_CAP_SM_PS_DYNAMIC;
  3131. smps <<= IEEE80211_HT_CAP_SM_PS_SHIFT;
  3132. ht_cap.cap |= smps;
  3133. }
  3134. if (ar->ht_cap_info & WMI_HT_CAP_TX_STBC)
  3135. ht_cap.cap |= IEEE80211_HT_CAP_TX_STBC;
  3136. if (ar->ht_cap_info & WMI_HT_CAP_RX_STBC) {
  3137. u32 stbc;
  3138. stbc = ar->ht_cap_info;
  3139. stbc &= WMI_HT_CAP_RX_STBC;
  3140. stbc >>= WMI_HT_CAP_RX_STBC_MASK_SHIFT;
  3141. stbc <<= IEEE80211_HT_CAP_RX_STBC_SHIFT;
  3142. stbc &= IEEE80211_HT_CAP_RX_STBC;
  3143. ht_cap.cap |= stbc;
  3144. }
  3145. if (ar->ht_cap_info & WMI_HT_CAP_LDPC)
  3146. ht_cap.cap |= IEEE80211_HT_CAP_LDPC_CODING;
  3147. if (ar->ht_cap_info & WMI_HT_CAP_L_SIG_TXOP_PROT)
  3148. ht_cap.cap |= IEEE80211_HT_CAP_LSIG_TXOP_PROT;
  3149. /* max AMSDU is implicitly taken from vht_cap_info */
  3150. if (ar->vht_cap_info & WMI_VHT_CAP_MAX_MPDU_LEN_MASK)
  3151. ht_cap.cap |= IEEE80211_HT_CAP_MAX_AMSDU;
  3152. for (i = 0; i < ar->num_rf_chains; i++) {
  3153. if (ar->cfg_rx_chainmask & BIT(i))
  3154. ht_cap.mcs.rx_mask[i] = 0xFF;
  3155. }
  3156. ht_cap.mcs.tx_params |= IEEE80211_HT_MCS_TX_DEFINED;
  3157. return ht_cap;
  3158. }
  3159. static void ath10k_mac_setup_ht_vht_cap(struct ath10k *ar)
  3160. {
  3161. struct ieee80211_supported_band *band;
  3162. struct ieee80211_sta_vht_cap vht_cap;
  3163. struct ieee80211_sta_ht_cap ht_cap;
  3164. ht_cap = ath10k_get_ht_cap(ar);
  3165. vht_cap = ath10k_create_vht_cap(ar);
  3166. if (ar->phy_capability & WHAL_WLAN_11G_CAPABILITY) {
  3167. band = &ar->mac.sbands[IEEE80211_BAND_2GHZ];
  3168. band->ht_cap = ht_cap;
  3169. /* Enable the VHT support at 2.4 GHz */
  3170. band->vht_cap = vht_cap;
  3171. }
  3172. if (ar->phy_capability & WHAL_WLAN_11A_CAPABILITY) {
  3173. band = &ar->mac.sbands[IEEE80211_BAND_5GHZ];
  3174. band->ht_cap = ht_cap;
  3175. band->vht_cap = vht_cap;
  3176. }
  3177. }
  3178. static int __ath10k_set_antenna(struct ath10k *ar, u32 tx_ant, u32 rx_ant)
  3179. {
  3180. int ret;
  3181. lockdep_assert_held(&ar->conf_mutex);
  3182. ath10k_check_chain_mask(ar, tx_ant, "tx");
  3183. ath10k_check_chain_mask(ar, rx_ant, "rx");
  3184. ar->cfg_tx_chainmask = tx_ant;
  3185. ar->cfg_rx_chainmask = rx_ant;
  3186. if ((ar->state != ATH10K_STATE_ON) &&
  3187. (ar->state != ATH10K_STATE_RESTARTED))
  3188. return 0;
  3189. ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->tx_chain_mask,
  3190. tx_ant);
  3191. if (ret) {
  3192. ath10k_warn(ar, "failed to set tx-chainmask: %d, req 0x%x\n",
  3193. ret, tx_ant);
  3194. return ret;
  3195. }
  3196. ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->rx_chain_mask,
  3197. rx_ant);
  3198. if (ret) {
  3199. ath10k_warn(ar, "failed to set rx-chainmask: %d, req 0x%x\n",
  3200. ret, rx_ant);
  3201. return ret;
  3202. }
  3203. /* Reload HT/VHT capability */
  3204. ath10k_mac_setup_ht_vht_cap(ar);
  3205. return 0;
  3206. }
  3207. static int ath10k_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
  3208. {
  3209. struct ath10k *ar = hw->priv;
  3210. int ret;
  3211. mutex_lock(&ar->conf_mutex);
  3212. ret = __ath10k_set_antenna(ar, tx_ant, rx_ant);
  3213. mutex_unlock(&ar->conf_mutex);
  3214. return ret;
  3215. }
  3216. static int ath10k_start(struct ieee80211_hw *hw)
  3217. {
  3218. struct ath10k *ar = hw->priv;
  3219. u32 burst_enable;
  3220. int ret = 0;
  3221. /*
  3222. * This makes sense only when restarting hw. It is harmless to call
  3223. * uncoditionally. This is necessary to make sure no HTT/WMI tx
  3224. * commands will be submitted while restarting.
  3225. */
  3226. ath10k_drain_tx(ar);
  3227. mutex_lock(&ar->conf_mutex);
  3228. switch (ar->state) {
  3229. case ATH10K_STATE_OFF:
  3230. ar->state = ATH10K_STATE_ON;
  3231. break;
  3232. case ATH10K_STATE_RESTARTING:
  3233. ath10k_halt(ar);
  3234. ar->state = ATH10K_STATE_RESTARTED;
  3235. break;
  3236. case ATH10K_STATE_ON:
  3237. case ATH10K_STATE_RESTARTED:
  3238. case ATH10K_STATE_WEDGED:
  3239. WARN_ON(1);
  3240. ret = -EINVAL;
  3241. goto err;
  3242. case ATH10K_STATE_UTF:
  3243. ret = -EBUSY;
  3244. goto err;
  3245. }
  3246. ret = ath10k_hif_power_up(ar);
  3247. if (ret) {
  3248. ath10k_err(ar, "Could not init hif: %d\n", ret);
  3249. goto err_off;
  3250. }
  3251. ret = ath10k_core_start(ar, ATH10K_FIRMWARE_MODE_NORMAL);
  3252. if (ret) {
  3253. ath10k_err(ar, "Could not init core: %d\n", ret);
  3254. goto err_power_down;
  3255. }
  3256. ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->pmf_qos, 1);
  3257. if (ret) {
  3258. ath10k_warn(ar, "failed to enable PMF QOS: %d\n", ret);
  3259. goto err_core_stop;
  3260. }
  3261. ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->dynamic_bw, 1);
  3262. if (ret) {
  3263. ath10k_warn(ar, "failed to enable dynamic BW: %d\n", ret);
  3264. goto err_core_stop;
  3265. }
  3266. if (test_bit(WMI_SERVICE_ADAPTIVE_OCS, ar->wmi.svc_map)) {
  3267. ret = ath10k_wmi_adaptive_qcs(ar, true);
  3268. if (ret) {
  3269. ath10k_warn(ar, "failed to enable adaptive qcs: %d\n",
  3270. ret);
  3271. goto err_core_stop;
  3272. }
  3273. }
  3274. if (test_bit(WMI_SERVICE_BURST, ar->wmi.svc_map)) {
  3275. burst_enable = ar->wmi.pdev_param->burst_enable;
  3276. ret = ath10k_wmi_pdev_set_param(ar, burst_enable, 0);
  3277. if (ret) {
  3278. ath10k_warn(ar, "failed to disable burst: %d\n", ret);
  3279. goto err_core_stop;
  3280. }
  3281. }
  3282. __ath10k_set_antenna(ar, ar->cfg_tx_chainmask, ar->cfg_rx_chainmask);
  3283. /*
  3284. * By default FW set ARP frames ac to voice (6). In that case ARP
  3285. * exchange is not working properly for UAPSD enabled AP. ARP requests
  3286. * which arrives with access category 0 are processed by network stack
  3287. * and send back with access category 0, but FW changes access category
  3288. * to 6. Set ARP frames access category to best effort (0) solves
  3289. * this problem.
  3290. */
  3291. ret = ath10k_wmi_pdev_set_param(ar,
  3292. ar->wmi.pdev_param->arp_ac_override, 0);
  3293. if (ret) {
  3294. ath10k_warn(ar, "failed to set arp ac override parameter: %d\n",
  3295. ret);
  3296. goto err_core_stop;
  3297. }
  3298. if (test_bit(ATH10K_FW_FEATURE_SUPPORTS_ADAPTIVE_CCA,
  3299. ar->fw_features)) {
  3300. ret = ath10k_wmi_pdev_enable_adaptive_cca(ar, 1,
  3301. WMI_CCA_DETECT_LEVEL_AUTO,
  3302. WMI_CCA_DETECT_MARGIN_AUTO);
  3303. if (ret) {
  3304. ath10k_warn(ar, "failed to enable adaptive cca: %d\n",
  3305. ret);
  3306. goto err_core_stop;
  3307. }
  3308. }
  3309. ret = ath10k_wmi_pdev_set_param(ar,
  3310. ar->wmi.pdev_param->ani_enable, 1);
  3311. if (ret) {
  3312. ath10k_warn(ar, "failed to enable ani by default: %d\n",
  3313. ret);
  3314. goto err_core_stop;
  3315. }
  3316. ar->ani_enabled = true;
  3317. ar->num_started_vdevs = 0;
  3318. ath10k_regd_update(ar);
  3319. ath10k_spectral_start(ar);
  3320. ath10k_thermal_set_throttling(ar);
  3321. mutex_unlock(&ar->conf_mutex);
  3322. return 0;
  3323. err_core_stop:
  3324. ath10k_core_stop(ar);
  3325. err_power_down:
  3326. ath10k_hif_power_down(ar);
  3327. err_off:
  3328. ar->state = ATH10K_STATE_OFF;
  3329. err:
  3330. mutex_unlock(&ar->conf_mutex);
  3331. return ret;
  3332. }
  3333. static void ath10k_stop(struct ieee80211_hw *hw)
  3334. {
  3335. struct ath10k *ar = hw->priv;
  3336. ath10k_drain_tx(ar);
  3337. mutex_lock(&ar->conf_mutex);
  3338. if (ar->state != ATH10K_STATE_OFF) {
  3339. ath10k_halt(ar);
  3340. ar->state = ATH10K_STATE_OFF;
  3341. }
  3342. mutex_unlock(&ar->conf_mutex);
  3343. cancel_delayed_work_sync(&ar->scan.timeout);
  3344. cancel_work_sync(&ar->restart_work);
  3345. }
  3346. static int ath10k_config_ps(struct ath10k *ar)
  3347. {
  3348. struct ath10k_vif *arvif;
  3349. int ret = 0;
  3350. lockdep_assert_held(&ar->conf_mutex);
  3351. list_for_each_entry(arvif, &ar->arvifs, list) {
  3352. ret = ath10k_mac_vif_setup_ps(arvif);
  3353. if (ret) {
  3354. ath10k_warn(ar, "failed to setup powersave: %d\n", ret);
  3355. break;
  3356. }
  3357. }
  3358. return ret;
  3359. }
  3360. static int ath10k_mac_txpower_setup(struct ath10k *ar, int txpower)
  3361. {
  3362. int ret;
  3363. u32 param;
  3364. lockdep_assert_held(&ar->conf_mutex);
  3365. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac txpower %d\n", txpower);
  3366. param = ar->wmi.pdev_param->txpower_limit2g;
  3367. ret = ath10k_wmi_pdev_set_param(ar, param, txpower * 2);
  3368. if (ret) {
  3369. ath10k_warn(ar, "failed to set 2g txpower %d: %d\n",
  3370. txpower, ret);
  3371. return ret;
  3372. }
  3373. param = ar->wmi.pdev_param->txpower_limit5g;
  3374. ret = ath10k_wmi_pdev_set_param(ar, param, txpower * 2);
  3375. if (ret) {
  3376. ath10k_warn(ar, "failed to set 5g txpower %d: %d\n",
  3377. txpower, ret);
  3378. return ret;
  3379. }
  3380. return 0;
  3381. }
  3382. static int ath10k_mac_txpower_recalc(struct ath10k *ar)
  3383. {
  3384. struct ath10k_vif *arvif;
  3385. int ret, txpower = -1;
  3386. lockdep_assert_held(&ar->conf_mutex);
  3387. list_for_each_entry(arvif, &ar->arvifs, list) {
  3388. WARN_ON(arvif->txpower < 0);
  3389. if (txpower == -1)
  3390. txpower = arvif->txpower;
  3391. else
  3392. txpower = min(txpower, arvif->txpower);
  3393. }
  3394. if (WARN_ON(txpower == -1))
  3395. return -EINVAL;
  3396. ret = ath10k_mac_txpower_setup(ar, txpower);
  3397. if (ret) {
  3398. ath10k_warn(ar, "failed to setup tx power %d: %d\n",
  3399. txpower, ret);
  3400. return ret;
  3401. }
  3402. return 0;
  3403. }
  3404. static int ath10k_config(struct ieee80211_hw *hw, u32 changed)
  3405. {
  3406. struct ath10k *ar = hw->priv;
  3407. struct ieee80211_conf *conf = &hw->conf;
  3408. int ret = 0;
  3409. mutex_lock(&ar->conf_mutex);
  3410. if (changed & IEEE80211_CONF_CHANGE_PS)
  3411. ath10k_config_ps(ar);
  3412. if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
  3413. ar->monitor = conf->flags & IEEE80211_CONF_MONITOR;
  3414. ret = ath10k_monitor_recalc(ar);
  3415. if (ret)
  3416. ath10k_warn(ar, "failed to recalc monitor: %d\n", ret);
  3417. }
  3418. mutex_unlock(&ar->conf_mutex);
  3419. return ret;
  3420. }
  3421. static u32 get_nss_from_chainmask(u16 chain_mask)
  3422. {
  3423. if ((chain_mask & 0x15) == 0x15)
  3424. return 4;
  3425. else if ((chain_mask & 0x7) == 0x7)
  3426. return 3;
  3427. else if ((chain_mask & 0x3) == 0x3)
  3428. return 2;
  3429. return 1;
  3430. }
  3431. static int ath10k_mac_set_txbf_conf(struct ath10k_vif *arvif)
  3432. {
  3433. u32 value = 0;
  3434. struct ath10k *ar = arvif->ar;
  3435. int nsts;
  3436. int sound_dim;
  3437. if (ath10k_wmi_get_txbf_conf_scheme(ar) != WMI_TXBF_CONF_BEFORE_ASSOC)
  3438. return 0;
  3439. nsts = ath10k_mac_get_vht_cap_bf_sts(ar);
  3440. if (ar->vht_cap_info & (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
  3441. IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE))
  3442. value |= SM(nsts, WMI_TXBF_STS_CAP_OFFSET);
  3443. sound_dim = ath10k_mac_get_vht_cap_bf_sound_dim(ar);
  3444. if (ar->vht_cap_info & (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE |
  3445. IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE))
  3446. value |= SM(sound_dim, WMI_BF_SOUND_DIM_OFFSET);
  3447. if (!value)
  3448. return 0;
  3449. if (ar->vht_cap_info & IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)
  3450. value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFER;
  3451. if (ar->vht_cap_info & IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE)
  3452. value |= (WMI_VDEV_PARAM_TXBF_MU_TX_BFER |
  3453. WMI_VDEV_PARAM_TXBF_SU_TX_BFER);
  3454. if (ar->vht_cap_info & IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE)
  3455. value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFEE;
  3456. if (ar->vht_cap_info & IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE)
  3457. value |= (WMI_VDEV_PARAM_TXBF_MU_TX_BFEE |
  3458. WMI_VDEV_PARAM_TXBF_SU_TX_BFEE);
  3459. return ath10k_wmi_vdev_set_param(ar, arvif->vdev_id,
  3460. ar->wmi.vdev_param->txbf, value);
  3461. }
  3462. /*
  3463. * TODO:
  3464. * Figure out how to handle WMI_VDEV_SUBTYPE_P2P_DEVICE,
  3465. * because we will send mgmt frames without CCK. This requirement
  3466. * for P2P_FIND/GO_NEG should be handled by checking CCK flag
  3467. * in the TX packet.
  3468. */
  3469. static int ath10k_add_interface(struct ieee80211_hw *hw,
  3470. struct ieee80211_vif *vif)
  3471. {
  3472. struct ath10k *ar = hw->priv;
  3473. struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
  3474. enum wmi_sta_powersave_param param;
  3475. int ret = 0;
  3476. u32 value;
  3477. int bit;
  3478. int i;
  3479. u32 vdev_param;
  3480. vif->driver_flags |= IEEE80211_VIF_SUPPORTS_UAPSD;
  3481. mutex_lock(&ar->conf_mutex);
  3482. memset(arvif, 0, sizeof(*arvif));
  3483. arvif->ar = ar;
  3484. arvif->vif = vif;
  3485. INIT_LIST_HEAD(&arvif->list);
  3486. INIT_WORK(&arvif->ap_csa_work, ath10k_mac_vif_ap_csa_work);
  3487. INIT_DELAYED_WORK(&arvif->connection_loss_work,
  3488. ath10k_mac_vif_sta_connection_loss_work);
  3489. for (i = 0; i < ARRAY_SIZE(arvif->bitrate_mask.control); i++) {
  3490. arvif->bitrate_mask.control[i].legacy = 0xffffffff;
  3491. memset(arvif->bitrate_mask.control[i].ht_mcs, 0xff,
  3492. sizeof(arvif->bitrate_mask.control[i].ht_mcs));
  3493. memset(arvif->bitrate_mask.control[i].vht_mcs, 0xff,
  3494. sizeof(arvif->bitrate_mask.control[i].vht_mcs));
  3495. }
  3496. if (ar->num_peers >= ar->max_num_peers) {
  3497. ath10k_warn(ar, "refusing vdev creation due to insufficient peer entry resources in firmware\n");
  3498. ret = -ENOBUFS;
  3499. goto err;
  3500. }
  3501. if (ar->free_vdev_map == 0) {
  3502. ath10k_warn(ar, "Free vdev map is empty, no more interfaces allowed.\n");
  3503. ret = -EBUSY;
  3504. goto err;
  3505. }
  3506. bit = __ffs64(ar->free_vdev_map);
  3507. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac create vdev %i map %llx\n",
  3508. bit, ar->free_vdev_map);
  3509. arvif->vdev_id = bit;
  3510. arvif->vdev_subtype = WMI_VDEV_SUBTYPE_NONE;
  3511. switch (vif->type) {
  3512. case NL80211_IFTYPE_P2P_DEVICE:
  3513. arvif->vdev_type = WMI_VDEV_TYPE_STA;
  3514. arvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_DEVICE;
  3515. break;
  3516. case NL80211_IFTYPE_UNSPECIFIED:
  3517. case NL80211_IFTYPE_STATION:
  3518. arvif->vdev_type = WMI_VDEV_TYPE_STA;
  3519. if (vif->p2p)
  3520. arvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_CLIENT;
  3521. break;
  3522. case NL80211_IFTYPE_ADHOC:
  3523. arvif->vdev_type = WMI_VDEV_TYPE_IBSS;
  3524. break;
  3525. case NL80211_IFTYPE_MESH_POINT:
  3526. if (!test_bit(ATH10K_FLAG_RAW_MODE, &ar->dev_flags)) {
  3527. ret = -EINVAL;
  3528. ath10k_warn(ar, "must load driver with rawmode=1 to add mesh interfaces\n");
  3529. goto err;
  3530. }
  3531. arvif->vdev_type = WMI_VDEV_TYPE_AP;
  3532. break;
  3533. case NL80211_IFTYPE_AP:
  3534. arvif->vdev_type = WMI_VDEV_TYPE_AP;
  3535. if (vif->p2p)
  3536. arvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_GO;
  3537. break;
  3538. case NL80211_IFTYPE_MONITOR:
  3539. arvif->vdev_type = WMI_VDEV_TYPE_MONITOR;
  3540. break;
  3541. default:
  3542. WARN_ON(1);
  3543. break;
  3544. }
  3545. /* Using vdev_id as queue number will make it very easy to do per-vif
  3546. * tx queue locking. This shouldn't wrap due to interface combinations
  3547. * but do a modulo for correctness sake and prevent using offchannel tx
  3548. * queues for regular vif tx.
  3549. */
  3550. vif->cab_queue = arvif->vdev_id % (IEEE80211_MAX_QUEUES - 1);
  3551. for (i = 0; i < ARRAY_SIZE(vif->hw_queue); i++)
  3552. vif->hw_queue[i] = arvif->vdev_id % (IEEE80211_MAX_QUEUES - 1);
  3553. /* Some firmware revisions don't wait for beacon tx completion before
  3554. * sending another SWBA event. This could lead to hardware using old
  3555. * (freed) beacon data in some cases, e.g. tx credit starvation
  3556. * combined with missed TBTT. This is very very rare.
  3557. *
  3558. * On non-IOMMU-enabled hosts this could be a possible security issue
  3559. * because hw could beacon some random data on the air. On
  3560. * IOMMU-enabled hosts DMAR faults would occur in most cases and target
  3561. * device would crash.
  3562. *
  3563. * Since there are no beacon tx completions (implicit nor explicit)
  3564. * propagated to host the only workaround for this is to allocate a
  3565. * DMA-coherent buffer for a lifetime of a vif and use it for all
  3566. * beacon tx commands. Worst case for this approach is some beacons may
  3567. * become corrupted, e.g. have garbled IEs or out-of-date TIM bitmap.
  3568. */
  3569. if (vif->type == NL80211_IFTYPE_ADHOC ||
  3570. vif->type == NL80211_IFTYPE_MESH_POINT ||
  3571. vif->type == NL80211_IFTYPE_AP) {
  3572. arvif->beacon_buf = dma_zalloc_coherent(ar->dev,
  3573. IEEE80211_MAX_FRAME_LEN,
  3574. &arvif->beacon_paddr,
  3575. GFP_ATOMIC);
  3576. if (!arvif->beacon_buf) {
  3577. ret = -ENOMEM;
  3578. ath10k_warn(ar, "failed to allocate beacon buffer: %d\n",
  3579. ret);
  3580. goto err;
  3581. }
  3582. }
  3583. if (test_bit(ATH10K_FLAG_HW_CRYPTO_DISABLED, &ar->dev_flags))
  3584. arvif->nohwcrypt = true;
  3585. if (arvif->nohwcrypt &&
  3586. !test_bit(ATH10K_FLAG_RAW_MODE, &ar->dev_flags)) {
  3587. ath10k_warn(ar, "cryptmode module param needed for sw crypto\n");
  3588. goto err;
  3589. }
  3590. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev create %d (add interface) type %d subtype %d bcnmode %s\n",
  3591. arvif->vdev_id, arvif->vdev_type, arvif->vdev_subtype,
  3592. arvif->beacon_buf ? "single-buf" : "per-skb");
  3593. ret = ath10k_wmi_vdev_create(ar, arvif->vdev_id, arvif->vdev_type,
  3594. arvif->vdev_subtype, vif->addr);
  3595. if (ret) {
  3596. ath10k_warn(ar, "failed to create WMI vdev %i: %d\n",
  3597. arvif->vdev_id, ret);
  3598. goto err;
  3599. }
  3600. ar->free_vdev_map &= ~(1LL << arvif->vdev_id);
  3601. list_add(&arvif->list, &ar->arvifs);
  3602. /* It makes no sense to have firmware do keepalives. mac80211 already
  3603. * takes care of this with idle connection polling.
  3604. */
  3605. ret = ath10k_mac_vif_disable_keepalive(arvif);
  3606. if (ret) {
  3607. ath10k_warn(ar, "failed to disable keepalive on vdev %i: %d\n",
  3608. arvif->vdev_id, ret);
  3609. goto err_vdev_delete;
  3610. }
  3611. arvif->def_wep_key_idx = -1;
  3612. vdev_param = ar->wmi.vdev_param->tx_encap_type;
  3613. ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
  3614. ATH10K_HW_TXRX_NATIVE_WIFI);
  3615. /* 10.X firmware does not support this VDEV parameter. Do not warn */
  3616. if (ret && ret != -EOPNOTSUPP) {
  3617. ath10k_warn(ar, "failed to set vdev %i TX encapsulation: %d\n",
  3618. arvif->vdev_id, ret);
  3619. goto err_vdev_delete;
  3620. }
  3621. if (ar->cfg_tx_chainmask) {
  3622. u16 nss = get_nss_from_chainmask(ar->cfg_tx_chainmask);
  3623. vdev_param = ar->wmi.vdev_param->nss;
  3624. ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
  3625. nss);
  3626. if (ret) {
  3627. ath10k_warn(ar, "failed to set vdev %i chainmask 0x%x, nss %i: %d\n",
  3628. arvif->vdev_id, ar->cfg_tx_chainmask, nss,
  3629. ret);
  3630. goto err_vdev_delete;
  3631. }
  3632. }
  3633. if (arvif->vdev_type == WMI_VDEV_TYPE_AP ||
  3634. arvif->vdev_type == WMI_VDEV_TYPE_IBSS) {
  3635. ret = ath10k_peer_create(ar, arvif->vdev_id, vif->addr,
  3636. WMI_PEER_TYPE_DEFAULT);
  3637. if (ret) {
  3638. ath10k_warn(ar, "failed to create vdev %i peer for AP/IBSS: %d\n",
  3639. arvif->vdev_id, ret);
  3640. goto err_vdev_delete;
  3641. }
  3642. }
  3643. if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
  3644. ret = ath10k_mac_set_kickout(arvif);
  3645. if (ret) {
  3646. ath10k_warn(ar, "failed to set vdev %i kickout parameters: %d\n",
  3647. arvif->vdev_id, ret);
  3648. goto err_peer_delete;
  3649. }
  3650. }
  3651. if (arvif->vdev_type == WMI_VDEV_TYPE_STA) {
  3652. param = WMI_STA_PS_PARAM_RX_WAKE_POLICY;
  3653. value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
  3654. ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
  3655. param, value);
  3656. if (ret) {
  3657. ath10k_warn(ar, "failed to set vdev %i RX wake policy: %d\n",
  3658. arvif->vdev_id, ret);
  3659. goto err_peer_delete;
  3660. }
  3661. ret = ath10k_mac_vif_recalc_ps_wake_threshold(arvif);
  3662. if (ret) {
  3663. ath10k_warn(ar, "failed to recalc ps wake threshold on vdev %i: %d\n",
  3664. arvif->vdev_id, ret);
  3665. goto err_peer_delete;
  3666. }
  3667. ret = ath10k_mac_vif_recalc_ps_poll_count(arvif);
  3668. if (ret) {
  3669. ath10k_warn(ar, "failed to recalc ps poll count on vdev %i: %d\n",
  3670. arvif->vdev_id, ret);
  3671. goto err_peer_delete;
  3672. }
  3673. }
  3674. ret = ath10k_mac_set_txbf_conf(arvif);
  3675. if (ret) {
  3676. ath10k_warn(ar, "failed to set txbf for vdev %d: %d\n",
  3677. arvif->vdev_id, ret);
  3678. goto err_peer_delete;
  3679. }
  3680. ret = ath10k_mac_set_rts(arvif, ar->hw->wiphy->rts_threshold);
  3681. if (ret) {
  3682. ath10k_warn(ar, "failed to set rts threshold for vdev %d: %d\n",
  3683. arvif->vdev_id, ret);
  3684. goto err_peer_delete;
  3685. }
  3686. arvif->txpower = vif->bss_conf.txpower;
  3687. ret = ath10k_mac_txpower_recalc(ar);
  3688. if (ret) {
  3689. ath10k_warn(ar, "failed to recalc tx power: %d\n", ret);
  3690. goto err_peer_delete;
  3691. }
  3692. if (vif->type == NL80211_IFTYPE_MONITOR) {
  3693. ar->monitor_arvif = arvif;
  3694. ret = ath10k_monitor_recalc(ar);
  3695. if (ret) {
  3696. ath10k_warn(ar, "failed to recalc monitor: %d\n", ret);
  3697. goto err_peer_delete;
  3698. }
  3699. }
  3700. spin_lock_bh(&ar->htt.tx_lock);
  3701. if (!ar->tx_paused)
  3702. ieee80211_wake_queue(ar->hw, arvif->vdev_id);
  3703. spin_unlock_bh(&ar->htt.tx_lock);
  3704. mutex_unlock(&ar->conf_mutex);
  3705. return 0;
  3706. err_peer_delete:
  3707. if (arvif->vdev_type == WMI_VDEV_TYPE_AP ||
  3708. arvif->vdev_type == WMI_VDEV_TYPE_IBSS)
  3709. ath10k_wmi_peer_delete(ar, arvif->vdev_id, vif->addr);
  3710. err_vdev_delete:
  3711. ath10k_wmi_vdev_delete(ar, arvif->vdev_id);
  3712. ar->free_vdev_map |= 1LL << arvif->vdev_id;
  3713. list_del(&arvif->list);
  3714. err:
  3715. if (arvif->beacon_buf) {
  3716. dma_free_coherent(ar->dev, IEEE80211_MAX_FRAME_LEN,
  3717. arvif->beacon_buf, arvif->beacon_paddr);
  3718. arvif->beacon_buf = NULL;
  3719. }
  3720. mutex_unlock(&ar->conf_mutex);
  3721. return ret;
  3722. }
  3723. static void ath10k_mac_vif_tx_unlock_all(struct ath10k_vif *arvif)
  3724. {
  3725. int i;
  3726. for (i = 0; i < BITS_PER_LONG; i++)
  3727. ath10k_mac_vif_tx_unlock(arvif, i);
  3728. }
  3729. static void ath10k_remove_interface(struct ieee80211_hw *hw,
  3730. struct ieee80211_vif *vif)
  3731. {
  3732. struct ath10k *ar = hw->priv;
  3733. struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
  3734. int ret;
  3735. cancel_work_sync(&arvif->ap_csa_work);
  3736. cancel_delayed_work_sync(&arvif->connection_loss_work);
  3737. mutex_lock(&ar->conf_mutex);
  3738. spin_lock_bh(&ar->data_lock);
  3739. ath10k_mac_vif_beacon_cleanup(arvif);
  3740. spin_unlock_bh(&ar->data_lock);
  3741. ret = ath10k_spectral_vif_stop(arvif);
  3742. if (ret)
  3743. ath10k_warn(ar, "failed to stop spectral for vdev %i: %d\n",
  3744. arvif->vdev_id, ret);
  3745. ar->free_vdev_map |= 1LL << arvif->vdev_id;
  3746. list_del(&arvif->list);
  3747. if (arvif->vdev_type == WMI_VDEV_TYPE_AP ||
  3748. arvif->vdev_type == WMI_VDEV_TYPE_IBSS) {
  3749. ret = ath10k_wmi_peer_delete(arvif->ar, arvif->vdev_id,
  3750. vif->addr);
  3751. if (ret)
  3752. ath10k_warn(ar, "failed to submit AP/IBSS self-peer removal on vdev %i: %d\n",
  3753. arvif->vdev_id, ret);
  3754. kfree(arvif->u.ap.noa_data);
  3755. }
  3756. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %i delete (remove interface)\n",
  3757. arvif->vdev_id);
  3758. ret = ath10k_wmi_vdev_delete(ar, arvif->vdev_id);
  3759. if (ret)
  3760. ath10k_warn(ar, "failed to delete WMI vdev %i: %d\n",
  3761. arvif->vdev_id, ret);
  3762. /* Some firmware revisions don't notify host about self-peer removal
  3763. * until after associated vdev is deleted.
  3764. */
  3765. if (arvif->vdev_type == WMI_VDEV_TYPE_AP ||
  3766. arvif->vdev_type == WMI_VDEV_TYPE_IBSS) {
  3767. ret = ath10k_wait_for_peer_deleted(ar, arvif->vdev_id,
  3768. vif->addr);
  3769. if (ret)
  3770. ath10k_warn(ar, "failed to remove AP self-peer on vdev %i: %d\n",
  3771. arvif->vdev_id, ret);
  3772. spin_lock_bh(&ar->data_lock);
  3773. ar->num_peers--;
  3774. spin_unlock_bh(&ar->data_lock);
  3775. }
  3776. ath10k_peer_cleanup(ar, arvif->vdev_id);
  3777. if (vif->type == NL80211_IFTYPE_MONITOR) {
  3778. ar->monitor_arvif = NULL;
  3779. ret = ath10k_monitor_recalc(ar);
  3780. if (ret)
  3781. ath10k_warn(ar, "failed to recalc monitor: %d\n", ret);
  3782. }
  3783. spin_lock_bh(&ar->htt.tx_lock);
  3784. ath10k_mac_vif_tx_unlock_all(arvif);
  3785. spin_unlock_bh(&ar->htt.tx_lock);
  3786. mutex_unlock(&ar->conf_mutex);
  3787. }
  3788. /*
  3789. * FIXME: Has to be verified.
  3790. */
  3791. #define SUPPORTED_FILTERS \
  3792. (FIF_ALLMULTI | \
  3793. FIF_CONTROL | \
  3794. FIF_PSPOLL | \
  3795. FIF_OTHER_BSS | \
  3796. FIF_BCN_PRBRESP_PROMISC | \
  3797. FIF_PROBE_REQ | \
  3798. FIF_FCSFAIL)
  3799. static void ath10k_configure_filter(struct ieee80211_hw *hw,
  3800. unsigned int changed_flags,
  3801. unsigned int *total_flags,
  3802. u64 multicast)
  3803. {
  3804. struct ath10k *ar = hw->priv;
  3805. int ret;
  3806. mutex_lock(&ar->conf_mutex);
  3807. changed_flags &= SUPPORTED_FILTERS;
  3808. *total_flags &= SUPPORTED_FILTERS;
  3809. ar->filter_flags = *total_flags;
  3810. ret = ath10k_monitor_recalc(ar);
  3811. if (ret)
  3812. ath10k_warn(ar, "failed to recalc montior: %d\n", ret);
  3813. mutex_unlock(&ar->conf_mutex);
  3814. }
  3815. static void ath10k_bss_info_changed(struct ieee80211_hw *hw,
  3816. struct ieee80211_vif *vif,
  3817. struct ieee80211_bss_conf *info,
  3818. u32 changed)
  3819. {
  3820. struct ath10k *ar = hw->priv;
  3821. struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
  3822. int ret = 0;
  3823. u32 vdev_param, pdev_param, slottime, preamble;
  3824. mutex_lock(&ar->conf_mutex);
  3825. if (changed & BSS_CHANGED_IBSS)
  3826. ath10k_control_ibss(arvif, info, vif->addr);
  3827. if (changed & BSS_CHANGED_BEACON_INT) {
  3828. arvif->beacon_interval = info->beacon_int;
  3829. vdev_param = ar->wmi.vdev_param->beacon_interval;
  3830. ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
  3831. arvif->beacon_interval);
  3832. ath10k_dbg(ar, ATH10K_DBG_MAC,
  3833. "mac vdev %d beacon_interval %d\n",
  3834. arvif->vdev_id, arvif->beacon_interval);
  3835. if (ret)
  3836. ath10k_warn(ar, "failed to set beacon interval for vdev %d: %i\n",
  3837. arvif->vdev_id, ret);
  3838. }
  3839. if (changed & BSS_CHANGED_BEACON) {
  3840. ath10k_dbg(ar, ATH10K_DBG_MAC,
  3841. "vdev %d set beacon tx mode to staggered\n",
  3842. arvif->vdev_id);
  3843. pdev_param = ar->wmi.pdev_param->beacon_tx_mode;
  3844. ret = ath10k_wmi_pdev_set_param(ar, pdev_param,
  3845. WMI_BEACON_STAGGERED_MODE);
  3846. if (ret)
  3847. ath10k_warn(ar, "failed to set beacon mode for vdev %d: %i\n",
  3848. arvif->vdev_id, ret);
  3849. ret = ath10k_mac_setup_bcn_tmpl(arvif);
  3850. if (ret)
  3851. ath10k_warn(ar, "failed to update beacon template: %d\n",
  3852. ret);
  3853. if (ieee80211_vif_is_mesh(vif)) {
  3854. /* mesh doesn't use SSID but firmware needs it */
  3855. strncpy(arvif->u.ap.ssid, "mesh",
  3856. sizeof(arvif->u.ap.ssid));
  3857. arvif->u.ap.ssid_len = 4;
  3858. }
  3859. }
  3860. if (changed & BSS_CHANGED_AP_PROBE_RESP) {
  3861. ret = ath10k_mac_setup_prb_tmpl(arvif);
  3862. if (ret)
  3863. ath10k_warn(ar, "failed to setup probe resp template on vdev %i: %d\n",
  3864. arvif->vdev_id, ret);
  3865. }
  3866. if (changed & (BSS_CHANGED_BEACON_INFO | BSS_CHANGED_BEACON)) {
  3867. arvif->dtim_period = info->dtim_period;
  3868. ath10k_dbg(ar, ATH10K_DBG_MAC,
  3869. "mac vdev %d dtim_period %d\n",
  3870. arvif->vdev_id, arvif->dtim_period);
  3871. vdev_param = ar->wmi.vdev_param->dtim_period;
  3872. ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
  3873. arvif->dtim_period);
  3874. if (ret)
  3875. ath10k_warn(ar, "failed to set dtim period for vdev %d: %i\n",
  3876. arvif->vdev_id, ret);
  3877. }
  3878. if (changed & BSS_CHANGED_SSID &&
  3879. vif->type == NL80211_IFTYPE_AP) {
  3880. arvif->u.ap.ssid_len = info->ssid_len;
  3881. if (info->ssid_len)
  3882. memcpy(arvif->u.ap.ssid, info->ssid, info->ssid_len);
  3883. arvif->u.ap.hidden_ssid = info->hidden_ssid;
  3884. }
  3885. if (changed & BSS_CHANGED_BSSID && !is_zero_ether_addr(info->bssid))
  3886. ether_addr_copy(arvif->bssid, info->bssid);
  3887. if (changed & BSS_CHANGED_BEACON_ENABLED)
  3888. ath10k_control_beaconing(arvif, info);
  3889. if (changed & BSS_CHANGED_ERP_CTS_PROT) {
  3890. arvif->use_cts_prot = info->use_cts_prot;
  3891. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d cts_prot %d\n",
  3892. arvif->vdev_id, info->use_cts_prot);
  3893. ret = ath10k_recalc_rtscts_prot(arvif);
  3894. if (ret)
  3895. ath10k_warn(ar, "failed to recalculate rts/cts prot for vdev %d: %d\n",
  3896. arvif->vdev_id, ret);
  3897. vdev_param = ar->wmi.vdev_param->protection_mode;
  3898. ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
  3899. info->use_cts_prot ? 1 : 0);
  3900. if (ret)
  3901. ath10k_warn(ar, "failed to set protection mode %d on vdev %i: %d\n",
  3902. info->use_cts_prot, arvif->vdev_id, ret);
  3903. }
  3904. if (changed & BSS_CHANGED_ERP_SLOT) {
  3905. if (info->use_short_slot)
  3906. slottime = WMI_VDEV_SLOT_TIME_SHORT; /* 9us */
  3907. else
  3908. slottime = WMI_VDEV_SLOT_TIME_LONG; /* 20us */
  3909. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d slot_time %d\n",
  3910. arvif->vdev_id, slottime);
  3911. vdev_param = ar->wmi.vdev_param->slot_time;
  3912. ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
  3913. slottime);
  3914. if (ret)
  3915. ath10k_warn(ar, "failed to set erp slot for vdev %d: %i\n",
  3916. arvif->vdev_id, ret);
  3917. }
  3918. if (changed & BSS_CHANGED_ERP_PREAMBLE) {
  3919. if (info->use_short_preamble)
  3920. preamble = WMI_VDEV_PREAMBLE_SHORT;
  3921. else
  3922. preamble = WMI_VDEV_PREAMBLE_LONG;
  3923. ath10k_dbg(ar, ATH10K_DBG_MAC,
  3924. "mac vdev %d preamble %dn",
  3925. arvif->vdev_id, preamble);
  3926. vdev_param = ar->wmi.vdev_param->preamble;
  3927. ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
  3928. preamble);
  3929. if (ret)
  3930. ath10k_warn(ar, "failed to set preamble for vdev %d: %i\n",
  3931. arvif->vdev_id, ret);
  3932. }
  3933. if (changed & BSS_CHANGED_ASSOC) {
  3934. if (info->assoc) {
  3935. /* Workaround: Make sure monitor vdev is not running
  3936. * when associating to prevent some firmware revisions
  3937. * (e.g. 10.1 and 10.2) from crashing.
  3938. */
  3939. if (ar->monitor_started)
  3940. ath10k_monitor_stop(ar);
  3941. ath10k_bss_assoc(hw, vif, info);
  3942. ath10k_monitor_recalc(ar);
  3943. } else {
  3944. ath10k_bss_disassoc(hw, vif);
  3945. }
  3946. }
  3947. if (changed & BSS_CHANGED_TXPOWER) {
  3948. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev_id %i txpower %d\n",
  3949. arvif->vdev_id, info->txpower);
  3950. arvif->txpower = info->txpower;
  3951. ret = ath10k_mac_txpower_recalc(ar);
  3952. if (ret)
  3953. ath10k_warn(ar, "failed to recalc tx power: %d\n", ret);
  3954. }
  3955. if (changed & BSS_CHANGED_PS) {
  3956. arvif->ps = vif->bss_conf.ps;
  3957. ret = ath10k_config_ps(ar);
  3958. if (ret)
  3959. ath10k_warn(ar, "failed to setup ps on vdev %i: %d\n",
  3960. arvif->vdev_id, ret);
  3961. }
  3962. mutex_unlock(&ar->conf_mutex);
  3963. }
  3964. static int ath10k_hw_scan(struct ieee80211_hw *hw,
  3965. struct ieee80211_vif *vif,
  3966. struct ieee80211_scan_request *hw_req)
  3967. {
  3968. struct ath10k *ar = hw->priv;
  3969. struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
  3970. struct cfg80211_scan_request *req = &hw_req->req;
  3971. struct wmi_start_scan_arg arg;
  3972. int ret = 0;
  3973. int i;
  3974. mutex_lock(&ar->conf_mutex);
  3975. spin_lock_bh(&ar->data_lock);
  3976. switch (ar->scan.state) {
  3977. case ATH10K_SCAN_IDLE:
  3978. reinit_completion(&ar->scan.started);
  3979. reinit_completion(&ar->scan.completed);
  3980. ar->scan.state = ATH10K_SCAN_STARTING;
  3981. ar->scan.is_roc = false;
  3982. ar->scan.vdev_id = arvif->vdev_id;
  3983. ret = 0;
  3984. break;
  3985. case ATH10K_SCAN_STARTING:
  3986. case ATH10K_SCAN_RUNNING:
  3987. case ATH10K_SCAN_ABORTING:
  3988. ret = -EBUSY;
  3989. break;
  3990. }
  3991. spin_unlock_bh(&ar->data_lock);
  3992. if (ret)
  3993. goto exit;
  3994. memset(&arg, 0, sizeof(arg));
  3995. ath10k_wmi_start_scan_init(ar, &arg);
  3996. arg.vdev_id = arvif->vdev_id;
  3997. arg.scan_id = ATH10K_SCAN_ID;
  3998. if (req->ie_len) {
  3999. arg.ie_len = req->ie_len;
  4000. memcpy(arg.ie, req->ie, arg.ie_len);
  4001. }
  4002. if (req->n_ssids) {
  4003. arg.n_ssids = req->n_ssids;
  4004. for (i = 0; i < arg.n_ssids; i++) {
  4005. arg.ssids[i].len = req->ssids[i].ssid_len;
  4006. arg.ssids[i].ssid = req->ssids[i].ssid;
  4007. }
  4008. } else {
  4009. arg.scan_ctrl_flags |= WMI_SCAN_FLAG_PASSIVE;
  4010. }
  4011. if (req->n_channels) {
  4012. arg.n_channels = req->n_channels;
  4013. for (i = 0; i < arg.n_channels; i++)
  4014. arg.channels[i] = req->channels[i]->center_freq;
  4015. }
  4016. ret = ath10k_start_scan(ar, &arg);
  4017. if (ret) {
  4018. ath10k_warn(ar, "failed to start hw scan: %d\n", ret);
  4019. spin_lock_bh(&ar->data_lock);
  4020. ar->scan.state = ATH10K_SCAN_IDLE;
  4021. spin_unlock_bh(&ar->data_lock);
  4022. }
  4023. /* Add a 200ms margin to account for event/command processing */
  4024. ieee80211_queue_delayed_work(ar->hw, &ar->scan.timeout,
  4025. msecs_to_jiffies(arg.max_scan_time +
  4026. 200));
  4027. exit:
  4028. mutex_unlock(&ar->conf_mutex);
  4029. return ret;
  4030. }
  4031. static void ath10k_cancel_hw_scan(struct ieee80211_hw *hw,
  4032. struct ieee80211_vif *vif)
  4033. {
  4034. struct ath10k *ar = hw->priv;
  4035. mutex_lock(&ar->conf_mutex);
  4036. ath10k_scan_abort(ar);
  4037. mutex_unlock(&ar->conf_mutex);
  4038. cancel_delayed_work_sync(&ar->scan.timeout);
  4039. }
  4040. static void ath10k_set_key_h_def_keyidx(struct ath10k *ar,
  4041. struct ath10k_vif *arvif,
  4042. enum set_key_cmd cmd,
  4043. struct ieee80211_key_conf *key)
  4044. {
  4045. u32 vdev_param = arvif->ar->wmi.vdev_param->def_keyid;
  4046. int ret;
  4047. /* 10.1 firmware branch requires default key index to be set to group
  4048. * key index after installing it. Otherwise FW/HW Txes corrupted
  4049. * frames with multi-vif APs. This is not required for main firmware
  4050. * branch (e.g. 636).
  4051. *
  4052. * This is also needed for 636 fw for IBSS-RSN to work more reliably.
  4053. *
  4054. * FIXME: It remains unknown if this is required for multi-vif STA
  4055. * interfaces on 10.1.
  4056. */
  4057. if (arvif->vdev_type != WMI_VDEV_TYPE_AP &&
  4058. arvif->vdev_type != WMI_VDEV_TYPE_IBSS)
  4059. return;
  4060. if (key->cipher == WLAN_CIPHER_SUITE_WEP40)
  4061. return;
  4062. if (key->cipher == WLAN_CIPHER_SUITE_WEP104)
  4063. return;
  4064. if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
  4065. return;
  4066. if (cmd != SET_KEY)
  4067. return;
  4068. ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
  4069. key->keyidx);
  4070. if (ret)
  4071. ath10k_warn(ar, "failed to set vdev %i group key as default key: %d\n",
  4072. arvif->vdev_id, ret);
  4073. }
  4074. static int ath10k_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
  4075. struct ieee80211_vif *vif, struct ieee80211_sta *sta,
  4076. struct ieee80211_key_conf *key)
  4077. {
  4078. struct ath10k *ar = hw->priv;
  4079. struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
  4080. struct ath10k_peer *peer;
  4081. const u8 *peer_addr;
  4082. bool is_wep = key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
  4083. key->cipher == WLAN_CIPHER_SUITE_WEP104;
  4084. int ret = 0;
  4085. int ret2;
  4086. u32 flags = 0;
  4087. u32 flags2;
  4088. /* this one needs to be done in software */
  4089. if (key->cipher == WLAN_CIPHER_SUITE_AES_CMAC)
  4090. return 1;
  4091. if (arvif->nohwcrypt)
  4092. return 1;
  4093. if (key->keyidx > WMI_MAX_KEY_INDEX)
  4094. return -ENOSPC;
  4095. mutex_lock(&ar->conf_mutex);
  4096. if (sta)
  4097. peer_addr = sta->addr;
  4098. else if (arvif->vdev_type == WMI_VDEV_TYPE_STA)
  4099. peer_addr = vif->bss_conf.bssid;
  4100. else
  4101. peer_addr = vif->addr;
  4102. key->hw_key_idx = key->keyidx;
  4103. if (is_wep) {
  4104. if (cmd == SET_KEY)
  4105. arvif->wep_keys[key->keyidx] = key;
  4106. else
  4107. arvif->wep_keys[key->keyidx] = NULL;
  4108. }
  4109. /* the peer should not disappear in mid-way (unless FW goes awry) since
  4110. * we already hold conf_mutex. we just make sure its there now. */
  4111. spin_lock_bh(&ar->data_lock);
  4112. peer = ath10k_peer_find(ar, arvif->vdev_id, peer_addr);
  4113. spin_unlock_bh(&ar->data_lock);
  4114. if (!peer) {
  4115. if (cmd == SET_KEY) {
  4116. ath10k_warn(ar, "failed to install key for non-existent peer %pM\n",
  4117. peer_addr);
  4118. ret = -EOPNOTSUPP;
  4119. goto exit;
  4120. } else {
  4121. /* if the peer doesn't exist there is no key to disable
  4122. * anymore */
  4123. goto exit;
  4124. }
  4125. }
  4126. if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
  4127. flags |= WMI_KEY_PAIRWISE;
  4128. else
  4129. flags |= WMI_KEY_GROUP;
  4130. if (is_wep) {
  4131. if (cmd == DISABLE_KEY)
  4132. ath10k_clear_vdev_key(arvif, key);
  4133. /* When WEP keys are uploaded it's possible that there are
  4134. * stations associated already (e.g. when merging) without any
  4135. * keys. Static WEP needs an explicit per-peer key upload.
  4136. */
  4137. if (vif->type == NL80211_IFTYPE_ADHOC &&
  4138. cmd == SET_KEY)
  4139. ath10k_mac_vif_update_wep_key(arvif, key);
  4140. /* 802.1x never sets the def_wep_key_idx so each set_key()
  4141. * call changes default tx key.
  4142. *
  4143. * Static WEP sets def_wep_key_idx via .set_default_unicast_key
  4144. * after first set_key().
  4145. */
  4146. if (cmd == SET_KEY && arvif->def_wep_key_idx == -1)
  4147. flags |= WMI_KEY_TX_USAGE;
  4148. }
  4149. ret = ath10k_install_key(arvif, key, cmd, peer_addr, flags);
  4150. if (ret) {
  4151. WARN_ON(ret > 0);
  4152. ath10k_warn(ar, "failed to install key for vdev %i peer %pM: %d\n",
  4153. arvif->vdev_id, peer_addr, ret);
  4154. goto exit;
  4155. }
  4156. /* mac80211 sets static WEP keys as groupwise while firmware requires
  4157. * them to be installed twice as both pairwise and groupwise.
  4158. */
  4159. if (is_wep && !sta && vif->type == NL80211_IFTYPE_STATION) {
  4160. flags2 = flags;
  4161. flags2 &= ~WMI_KEY_GROUP;
  4162. flags2 |= WMI_KEY_PAIRWISE;
  4163. ret = ath10k_install_key(arvif, key, cmd, peer_addr, flags2);
  4164. if (ret) {
  4165. WARN_ON(ret > 0);
  4166. ath10k_warn(ar, "failed to install (ucast) key for vdev %i peer %pM: %d\n",
  4167. arvif->vdev_id, peer_addr, ret);
  4168. ret2 = ath10k_install_key(arvif, key, DISABLE_KEY,
  4169. peer_addr, flags);
  4170. if (ret2) {
  4171. WARN_ON(ret2 > 0);
  4172. ath10k_warn(ar, "failed to disable (mcast) key for vdev %i peer %pM: %d\n",
  4173. arvif->vdev_id, peer_addr, ret2);
  4174. }
  4175. goto exit;
  4176. }
  4177. }
  4178. ath10k_set_key_h_def_keyidx(ar, arvif, cmd, key);
  4179. spin_lock_bh(&ar->data_lock);
  4180. peer = ath10k_peer_find(ar, arvif->vdev_id, peer_addr);
  4181. if (peer && cmd == SET_KEY)
  4182. peer->keys[key->keyidx] = key;
  4183. else if (peer && cmd == DISABLE_KEY)
  4184. peer->keys[key->keyidx] = NULL;
  4185. else if (peer == NULL)
  4186. /* impossible unless FW goes crazy */
  4187. ath10k_warn(ar, "Peer %pM disappeared!\n", peer_addr);
  4188. spin_unlock_bh(&ar->data_lock);
  4189. exit:
  4190. mutex_unlock(&ar->conf_mutex);
  4191. return ret;
  4192. }
  4193. static void ath10k_set_default_unicast_key(struct ieee80211_hw *hw,
  4194. struct ieee80211_vif *vif,
  4195. int keyidx)
  4196. {
  4197. struct ath10k *ar = hw->priv;
  4198. struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
  4199. int ret;
  4200. mutex_lock(&arvif->ar->conf_mutex);
  4201. if (arvif->ar->state != ATH10K_STATE_ON)
  4202. goto unlock;
  4203. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d set keyidx %d\n",
  4204. arvif->vdev_id, keyidx);
  4205. ret = ath10k_wmi_vdev_set_param(arvif->ar,
  4206. arvif->vdev_id,
  4207. arvif->ar->wmi.vdev_param->def_keyid,
  4208. keyidx);
  4209. if (ret) {
  4210. ath10k_warn(ar, "failed to update wep key index for vdev %d: %d\n",
  4211. arvif->vdev_id,
  4212. ret);
  4213. goto unlock;
  4214. }
  4215. arvif->def_wep_key_idx = keyidx;
  4216. unlock:
  4217. mutex_unlock(&arvif->ar->conf_mutex);
  4218. }
  4219. static void ath10k_sta_rc_update_wk(struct work_struct *wk)
  4220. {
  4221. struct ath10k *ar;
  4222. struct ath10k_vif *arvif;
  4223. struct ath10k_sta *arsta;
  4224. struct ieee80211_sta *sta;
  4225. struct cfg80211_chan_def def;
  4226. enum ieee80211_band band;
  4227. const u8 *ht_mcs_mask;
  4228. const u16 *vht_mcs_mask;
  4229. u32 changed, bw, nss, smps;
  4230. int err;
  4231. arsta = container_of(wk, struct ath10k_sta, update_wk);
  4232. sta = container_of((void *)arsta, struct ieee80211_sta, drv_priv);
  4233. arvif = arsta->arvif;
  4234. ar = arvif->ar;
  4235. if (WARN_ON(ath10k_mac_vif_chan(arvif->vif, &def)))
  4236. return;
  4237. band = def.chan->band;
  4238. ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
  4239. vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
  4240. spin_lock_bh(&ar->data_lock);
  4241. changed = arsta->changed;
  4242. arsta->changed = 0;
  4243. bw = arsta->bw;
  4244. nss = arsta->nss;
  4245. smps = arsta->smps;
  4246. spin_unlock_bh(&ar->data_lock);
  4247. mutex_lock(&ar->conf_mutex);
  4248. nss = max_t(u32, 1, nss);
  4249. nss = min(nss, max(ath10k_mac_max_ht_nss(ht_mcs_mask),
  4250. ath10k_mac_max_vht_nss(vht_mcs_mask)));
  4251. if (changed & IEEE80211_RC_BW_CHANGED) {
  4252. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac update sta %pM peer bw %d\n",
  4253. sta->addr, bw);
  4254. err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
  4255. WMI_PEER_CHAN_WIDTH, bw);
  4256. if (err)
  4257. ath10k_warn(ar, "failed to update STA %pM peer bw %d: %d\n",
  4258. sta->addr, bw, err);
  4259. }
  4260. if (changed & IEEE80211_RC_NSS_CHANGED) {
  4261. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac update sta %pM nss %d\n",
  4262. sta->addr, nss);
  4263. err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
  4264. WMI_PEER_NSS, nss);
  4265. if (err)
  4266. ath10k_warn(ar, "failed to update STA %pM nss %d: %d\n",
  4267. sta->addr, nss, err);
  4268. }
  4269. if (changed & IEEE80211_RC_SMPS_CHANGED) {
  4270. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac update sta %pM smps %d\n",
  4271. sta->addr, smps);
  4272. err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
  4273. WMI_PEER_SMPS_STATE, smps);
  4274. if (err)
  4275. ath10k_warn(ar, "failed to update STA %pM smps %d: %d\n",
  4276. sta->addr, smps, err);
  4277. }
  4278. if (changed & IEEE80211_RC_SUPP_RATES_CHANGED ||
  4279. changed & IEEE80211_RC_NSS_CHANGED) {
  4280. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac update sta %pM supp rates/nss\n",
  4281. sta->addr);
  4282. err = ath10k_station_assoc(ar, arvif->vif, sta, true);
  4283. if (err)
  4284. ath10k_warn(ar, "failed to reassociate station: %pM\n",
  4285. sta->addr);
  4286. }
  4287. mutex_unlock(&ar->conf_mutex);
  4288. }
  4289. static int ath10k_mac_inc_num_stations(struct ath10k_vif *arvif,
  4290. struct ieee80211_sta *sta)
  4291. {
  4292. struct ath10k *ar = arvif->ar;
  4293. lockdep_assert_held(&ar->conf_mutex);
  4294. if (arvif->vdev_type == WMI_VDEV_TYPE_STA && !sta->tdls)
  4295. return 0;
  4296. if (ar->num_stations >= ar->max_num_stations)
  4297. return -ENOBUFS;
  4298. ar->num_stations++;
  4299. return 0;
  4300. }
  4301. static void ath10k_mac_dec_num_stations(struct ath10k_vif *arvif,
  4302. struct ieee80211_sta *sta)
  4303. {
  4304. struct ath10k *ar = arvif->ar;
  4305. lockdep_assert_held(&ar->conf_mutex);
  4306. if (arvif->vdev_type == WMI_VDEV_TYPE_STA && !sta->tdls)
  4307. return;
  4308. ar->num_stations--;
  4309. }
  4310. struct ath10k_mac_tdls_iter_data {
  4311. u32 num_tdls_stations;
  4312. struct ieee80211_vif *curr_vif;
  4313. };
  4314. static void ath10k_mac_tdls_vif_stations_count_iter(void *data,
  4315. struct ieee80211_sta *sta)
  4316. {
  4317. struct ath10k_mac_tdls_iter_data *iter_data = data;
  4318. struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
  4319. struct ieee80211_vif *sta_vif = arsta->arvif->vif;
  4320. if (sta->tdls && sta_vif == iter_data->curr_vif)
  4321. iter_data->num_tdls_stations++;
  4322. }
  4323. static int ath10k_mac_tdls_vif_stations_count(struct ieee80211_hw *hw,
  4324. struct ieee80211_vif *vif)
  4325. {
  4326. struct ath10k_mac_tdls_iter_data data = {};
  4327. data.curr_vif = vif;
  4328. ieee80211_iterate_stations_atomic(hw,
  4329. ath10k_mac_tdls_vif_stations_count_iter,
  4330. &data);
  4331. return data.num_tdls_stations;
  4332. }
  4333. static void ath10k_mac_tdls_vifs_count_iter(void *data, u8 *mac,
  4334. struct ieee80211_vif *vif)
  4335. {
  4336. struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
  4337. int *num_tdls_vifs = data;
  4338. if (vif->type != NL80211_IFTYPE_STATION)
  4339. return;
  4340. if (ath10k_mac_tdls_vif_stations_count(arvif->ar->hw, vif) > 0)
  4341. (*num_tdls_vifs)++;
  4342. }
  4343. static int ath10k_mac_tdls_vifs_count(struct ieee80211_hw *hw)
  4344. {
  4345. int num_tdls_vifs = 0;
  4346. ieee80211_iterate_active_interfaces_atomic(hw,
  4347. IEEE80211_IFACE_ITER_NORMAL,
  4348. ath10k_mac_tdls_vifs_count_iter,
  4349. &num_tdls_vifs);
  4350. return num_tdls_vifs;
  4351. }
  4352. static int ath10k_sta_state(struct ieee80211_hw *hw,
  4353. struct ieee80211_vif *vif,
  4354. struct ieee80211_sta *sta,
  4355. enum ieee80211_sta_state old_state,
  4356. enum ieee80211_sta_state new_state)
  4357. {
  4358. struct ath10k *ar = hw->priv;
  4359. struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
  4360. struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
  4361. int ret = 0;
  4362. if (old_state == IEEE80211_STA_NOTEXIST &&
  4363. new_state == IEEE80211_STA_NONE) {
  4364. memset(arsta, 0, sizeof(*arsta));
  4365. arsta->arvif = arvif;
  4366. INIT_WORK(&arsta->update_wk, ath10k_sta_rc_update_wk);
  4367. }
  4368. /* cancel must be done outside the mutex to avoid deadlock */
  4369. if ((old_state == IEEE80211_STA_NONE &&
  4370. new_state == IEEE80211_STA_NOTEXIST))
  4371. cancel_work_sync(&arsta->update_wk);
  4372. mutex_lock(&ar->conf_mutex);
  4373. if (old_state == IEEE80211_STA_NOTEXIST &&
  4374. new_state == IEEE80211_STA_NONE) {
  4375. /*
  4376. * New station addition.
  4377. */
  4378. enum wmi_peer_type peer_type = WMI_PEER_TYPE_DEFAULT;
  4379. u32 num_tdls_stations;
  4380. u32 num_tdls_vifs;
  4381. ath10k_dbg(ar, ATH10K_DBG_MAC,
  4382. "mac vdev %d peer create %pM (new sta) sta %d / %d peer %d / %d\n",
  4383. arvif->vdev_id, sta->addr,
  4384. ar->num_stations + 1, ar->max_num_stations,
  4385. ar->num_peers + 1, ar->max_num_peers);
  4386. ret = ath10k_mac_inc_num_stations(arvif, sta);
  4387. if (ret) {
  4388. ath10k_warn(ar, "refusing to associate station: too many connected already (%d)\n",
  4389. ar->max_num_stations);
  4390. goto exit;
  4391. }
  4392. if (sta->tdls)
  4393. peer_type = WMI_PEER_TYPE_TDLS;
  4394. ret = ath10k_peer_create(ar, arvif->vdev_id, sta->addr,
  4395. peer_type);
  4396. if (ret) {
  4397. ath10k_warn(ar, "failed to add peer %pM for vdev %d when adding a new sta: %i\n",
  4398. sta->addr, arvif->vdev_id, ret);
  4399. ath10k_mac_dec_num_stations(arvif, sta);
  4400. goto exit;
  4401. }
  4402. if (!sta->tdls)
  4403. goto exit;
  4404. num_tdls_stations = ath10k_mac_tdls_vif_stations_count(hw, vif);
  4405. num_tdls_vifs = ath10k_mac_tdls_vifs_count(hw);
  4406. if (num_tdls_vifs >= ar->max_num_tdls_vdevs &&
  4407. num_tdls_stations == 0) {
  4408. ath10k_warn(ar, "vdev %i exceeded maximum number of tdls vdevs %i\n",
  4409. arvif->vdev_id, ar->max_num_tdls_vdevs);
  4410. ath10k_peer_delete(ar, arvif->vdev_id, sta->addr);
  4411. ath10k_mac_dec_num_stations(arvif, sta);
  4412. ret = -ENOBUFS;
  4413. goto exit;
  4414. }
  4415. if (num_tdls_stations == 0) {
  4416. /* This is the first tdls peer in current vif */
  4417. enum wmi_tdls_state state = WMI_TDLS_ENABLE_ACTIVE;
  4418. ret = ath10k_wmi_update_fw_tdls_state(ar, arvif->vdev_id,
  4419. state);
  4420. if (ret) {
  4421. ath10k_warn(ar, "failed to update fw tdls state on vdev %i: %i\n",
  4422. arvif->vdev_id, ret);
  4423. ath10k_peer_delete(ar, arvif->vdev_id,
  4424. sta->addr);
  4425. ath10k_mac_dec_num_stations(arvif, sta);
  4426. goto exit;
  4427. }
  4428. }
  4429. ret = ath10k_mac_tdls_peer_update(ar, arvif->vdev_id, sta,
  4430. WMI_TDLS_PEER_STATE_PEERING);
  4431. if (ret) {
  4432. ath10k_warn(ar,
  4433. "failed to update tdls peer %pM for vdev %d when adding a new sta: %i\n",
  4434. sta->addr, arvif->vdev_id, ret);
  4435. ath10k_peer_delete(ar, arvif->vdev_id, sta->addr);
  4436. ath10k_mac_dec_num_stations(arvif, sta);
  4437. if (num_tdls_stations != 0)
  4438. goto exit;
  4439. ath10k_wmi_update_fw_tdls_state(ar, arvif->vdev_id,
  4440. WMI_TDLS_DISABLE);
  4441. }
  4442. } else if ((old_state == IEEE80211_STA_NONE &&
  4443. new_state == IEEE80211_STA_NOTEXIST)) {
  4444. /*
  4445. * Existing station deletion.
  4446. */
  4447. ath10k_dbg(ar, ATH10K_DBG_MAC,
  4448. "mac vdev %d peer delete %pM (sta gone)\n",
  4449. arvif->vdev_id, sta->addr);
  4450. ret = ath10k_peer_delete(ar, arvif->vdev_id, sta->addr);
  4451. if (ret)
  4452. ath10k_warn(ar, "failed to delete peer %pM for vdev %d: %i\n",
  4453. sta->addr, arvif->vdev_id, ret);
  4454. ath10k_mac_dec_num_stations(arvif, sta);
  4455. if (!sta->tdls)
  4456. goto exit;
  4457. if (ath10k_mac_tdls_vif_stations_count(hw, vif))
  4458. goto exit;
  4459. /* This was the last tdls peer in current vif */
  4460. ret = ath10k_wmi_update_fw_tdls_state(ar, arvif->vdev_id,
  4461. WMI_TDLS_DISABLE);
  4462. if (ret) {
  4463. ath10k_warn(ar, "failed to update fw tdls state on vdev %i: %i\n",
  4464. arvif->vdev_id, ret);
  4465. }
  4466. } else if (old_state == IEEE80211_STA_AUTH &&
  4467. new_state == IEEE80211_STA_ASSOC &&
  4468. (vif->type == NL80211_IFTYPE_AP ||
  4469. vif->type == NL80211_IFTYPE_MESH_POINT ||
  4470. vif->type == NL80211_IFTYPE_ADHOC)) {
  4471. /*
  4472. * New association.
  4473. */
  4474. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac sta %pM associated\n",
  4475. sta->addr);
  4476. ret = ath10k_station_assoc(ar, vif, sta, false);
  4477. if (ret)
  4478. ath10k_warn(ar, "failed to associate station %pM for vdev %i: %i\n",
  4479. sta->addr, arvif->vdev_id, ret);
  4480. } else if (old_state == IEEE80211_STA_ASSOC &&
  4481. new_state == IEEE80211_STA_AUTHORIZED &&
  4482. sta->tdls) {
  4483. /*
  4484. * Tdls station authorized.
  4485. */
  4486. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac tdls sta %pM authorized\n",
  4487. sta->addr);
  4488. ret = ath10k_station_assoc(ar, vif, sta, false);
  4489. if (ret) {
  4490. ath10k_warn(ar, "failed to associate tdls station %pM for vdev %i: %i\n",
  4491. sta->addr, arvif->vdev_id, ret);
  4492. goto exit;
  4493. }
  4494. ret = ath10k_mac_tdls_peer_update(ar, arvif->vdev_id, sta,
  4495. WMI_TDLS_PEER_STATE_CONNECTED);
  4496. if (ret)
  4497. ath10k_warn(ar, "failed to update tdls peer %pM for vdev %i: %i\n",
  4498. sta->addr, arvif->vdev_id, ret);
  4499. } else if (old_state == IEEE80211_STA_ASSOC &&
  4500. new_state == IEEE80211_STA_AUTH &&
  4501. (vif->type == NL80211_IFTYPE_AP ||
  4502. vif->type == NL80211_IFTYPE_MESH_POINT ||
  4503. vif->type == NL80211_IFTYPE_ADHOC)) {
  4504. /*
  4505. * Disassociation.
  4506. */
  4507. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac sta %pM disassociated\n",
  4508. sta->addr);
  4509. ret = ath10k_station_disassoc(ar, vif, sta);
  4510. if (ret)
  4511. ath10k_warn(ar, "failed to disassociate station: %pM vdev %i: %i\n",
  4512. sta->addr, arvif->vdev_id, ret);
  4513. }
  4514. exit:
  4515. mutex_unlock(&ar->conf_mutex);
  4516. return ret;
  4517. }
  4518. static int ath10k_conf_tx_uapsd(struct ath10k *ar, struct ieee80211_vif *vif,
  4519. u16 ac, bool enable)
  4520. {
  4521. struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
  4522. struct wmi_sta_uapsd_auto_trig_arg arg = {};
  4523. u32 prio = 0, acc = 0;
  4524. u32 value = 0;
  4525. int ret = 0;
  4526. lockdep_assert_held(&ar->conf_mutex);
  4527. if (arvif->vdev_type != WMI_VDEV_TYPE_STA)
  4528. return 0;
  4529. switch (ac) {
  4530. case IEEE80211_AC_VO:
  4531. value = WMI_STA_PS_UAPSD_AC3_DELIVERY_EN |
  4532. WMI_STA_PS_UAPSD_AC3_TRIGGER_EN;
  4533. prio = 7;
  4534. acc = 3;
  4535. break;
  4536. case IEEE80211_AC_VI:
  4537. value = WMI_STA_PS_UAPSD_AC2_DELIVERY_EN |
  4538. WMI_STA_PS_UAPSD_AC2_TRIGGER_EN;
  4539. prio = 5;
  4540. acc = 2;
  4541. break;
  4542. case IEEE80211_AC_BE:
  4543. value = WMI_STA_PS_UAPSD_AC1_DELIVERY_EN |
  4544. WMI_STA_PS_UAPSD_AC1_TRIGGER_EN;
  4545. prio = 2;
  4546. acc = 1;
  4547. break;
  4548. case IEEE80211_AC_BK:
  4549. value = WMI_STA_PS_UAPSD_AC0_DELIVERY_EN |
  4550. WMI_STA_PS_UAPSD_AC0_TRIGGER_EN;
  4551. prio = 0;
  4552. acc = 0;
  4553. break;
  4554. }
  4555. if (enable)
  4556. arvif->u.sta.uapsd |= value;
  4557. else
  4558. arvif->u.sta.uapsd &= ~value;
  4559. ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
  4560. WMI_STA_PS_PARAM_UAPSD,
  4561. arvif->u.sta.uapsd);
  4562. if (ret) {
  4563. ath10k_warn(ar, "failed to set uapsd params: %d\n", ret);
  4564. goto exit;
  4565. }
  4566. if (arvif->u.sta.uapsd)
  4567. value = WMI_STA_PS_RX_WAKE_POLICY_POLL_UAPSD;
  4568. else
  4569. value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
  4570. ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
  4571. WMI_STA_PS_PARAM_RX_WAKE_POLICY,
  4572. value);
  4573. if (ret)
  4574. ath10k_warn(ar, "failed to set rx wake param: %d\n", ret);
  4575. ret = ath10k_mac_vif_recalc_ps_wake_threshold(arvif);
  4576. if (ret) {
  4577. ath10k_warn(ar, "failed to recalc ps wake threshold on vdev %i: %d\n",
  4578. arvif->vdev_id, ret);
  4579. return ret;
  4580. }
  4581. ret = ath10k_mac_vif_recalc_ps_poll_count(arvif);
  4582. if (ret) {
  4583. ath10k_warn(ar, "failed to recalc ps poll count on vdev %i: %d\n",
  4584. arvif->vdev_id, ret);
  4585. return ret;
  4586. }
  4587. if (test_bit(WMI_SERVICE_STA_UAPSD_BASIC_AUTO_TRIG, ar->wmi.svc_map) ||
  4588. test_bit(WMI_SERVICE_STA_UAPSD_VAR_AUTO_TRIG, ar->wmi.svc_map)) {
  4589. /* Only userspace can make an educated decision when to send
  4590. * trigger frame. The following effectively disables u-UAPSD
  4591. * autotrigger in firmware (which is enabled by default
  4592. * provided the autotrigger service is available).
  4593. */
  4594. arg.wmm_ac = acc;
  4595. arg.user_priority = prio;
  4596. arg.service_interval = 0;
  4597. arg.suspend_interval = WMI_STA_UAPSD_MAX_INTERVAL_MSEC;
  4598. arg.delay_interval = WMI_STA_UAPSD_MAX_INTERVAL_MSEC;
  4599. ret = ath10k_wmi_vdev_sta_uapsd(ar, arvif->vdev_id,
  4600. arvif->bssid, &arg, 1);
  4601. if (ret) {
  4602. ath10k_warn(ar, "failed to set uapsd auto trigger %d\n",
  4603. ret);
  4604. return ret;
  4605. }
  4606. }
  4607. exit:
  4608. return ret;
  4609. }
  4610. static int ath10k_conf_tx(struct ieee80211_hw *hw,
  4611. struct ieee80211_vif *vif, u16 ac,
  4612. const struct ieee80211_tx_queue_params *params)
  4613. {
  4614. struct ath10k *ar = hw->priv;
  4615. struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
  4616. struct wmi_wmm_params_arg *p = NULL;
  4617. int ret;
  4618. mutex_lock(&ar->conf_mutex);
  4619. switch (ac) {
  4620. case IEEE80211_AC_VO:
  4621. p = &arvif->wmm_params.ac_vo;
  4622. break;
  4623. case IEEE80211_AC_VI:
  4624. p = &arvif->wmm_params.ac_vi;
  4625. break;
  4626. case IEEE80211_AC_BE:
  4627. p = &arvif->wmm_params.ac_be;
  4628. break;
  4629. case IEEE80211_AC_BK:
  4630. p = &arvif->wmm_params.ac_bk;
  4631. break;
  4632. }
  4633. if (WARN_ON(!p)) {
  4634. ret = -EINVAL;
  4635. goto exit;
  4636. }
  4637. p->cwmin = params->cw_min;
  4638. p->cwmax = params->cw_max;
  4639. p->aifs = params->aifs;
  4640. /*
  4641. * The channel time duration programmed in the HW is in absolute
  4642. * microseconds, while mac80211 gives the txop in units of
  4643. * 32 microseconds.
  4644. */
  4645. p->txop = params->txop * 32;
  4646. if (ar->wmi.ops->gen_vdev_wmm_conf) {
  4647. ret = ath10k_wmi_vdev_wmm_conf(ar, arvif->vdev_id,
  4648. &arvif->wmm_params);
  4649. if (ret) {
  4650. ath10k_warn(ar, "failed to set vdev wmm params on vdev %i: %d\n",
  4651. arvif->vdev_id, ret);
  4652. goto exit;
  4653. }
  4654. } else {
  4655. /* This won't work well with multi-interface cases but it's
  4656. * better than nothing.
  4657. */
  4658. ret = ath10k_wmi_pdev_set_wmm_params(ar, &arvif->wmm_params);
  4659. if (ret) {
  4660. ath10k_warn(ar, "failed to set wmm params: %d\n", ret);
  4661. goto exit;
  4662. }
  4663. }
  4664. ret = ath10k_conf_tx_uapsd(ar, vif, ac, params->uapsd);
  4665. if (ret)
  4666. ath10k_warn(ar, "failed to set sta uapsd: %d\n", ret);
  4667. exit:
  4668. mutex_unlock(&ar->conf_mutex);
  4669. return ret;
  4670. }
  4671. #define ATH10K_ROC_TIMEOUT_HZ (2*HZ)
  4672. static int ath10k_remain_on_channel(struct ieee80211_hw *hw,
  4673. struct ieee80211_vif *vif,
  4674. struct ieee80211_channel *chan,
  4675. int duration,
  4676. enum ieee80211_roc_type type)
  4677. {
  4678. struct ath10k *ar = hw->priv;
  4679. struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
  4680. struct wmi_start_scan_arg arg;
  4681. int ret = 0;
  4682. u32 scan_time_msec;
  4683. mutex_lock(&ar->conf_mutex);
  4684. spin_lock_bh(&ar->data_lock);
  4685. switch (ar->scan.state) {
  4686. case ATH10K_SCAN_IDLE:
  4687. reinit_completion(&ar->scan.started);
  4688. reinit_completion(&ar->scan.completed);
  4689. reinit_completion(&ar->scan.on_channel);
  4690. ar->scan.state = ATH10K_SCAN_STARTING;
  4691. ar->scan.is_roc = true;
  4692. ar->scan.vdev_id = arvif->vdev_id;
  4693. ar->scan.roc_freq = chan->center_freq;
  4694. ar->scan.roc_notify = true;
  4695. ret = 0;
  4696. break;
  4697. case ATH10K_SCAN_STARTING:
  4698. case ATH10K_SCAN_RUNNING:
  4699. case ATH10K_SCAN_ABORTING:
  4700. ret = -EBUSY;
  4701. break;
  4702. }
  4703. spin_unlock_bh(&ar->data_lock);
  4704. if (ret)
  4705. goto exit;
  4706. scan_time_msec = ar->hw->wiphy->max_remain_on_channel_duration * 2;
  4707. memset(&arg, 0, sizeof(arg));
  4708. ath10k_wmi_start_scan_init(ar, &arg);
  4709. arg.vdev_id = arvif->vdev_id;
  4710. arg.scan_id = ATH10K_SCAN_ID;
  4711. arg.n_channels = 1;
  4712. arg.channels[0] = chan->center_freq;
  4713. arg.dwell_time_active = scan_time_msec;
  4714. arg.dwell_time_passive = scan_time_msec;
  4715. arg.max_scan_time = scan_time_msec;
  4716. arg.scan_ctrl_flags |= WMI_SCAN_FLAG_PASSIVE;
  4717. arg.scan_ctrl_flags |= WMI_SCAN_FILTER_PROBE_REQ;
  4718. arg.burst_duration_ms = duration;
  4719. ret = ath10k_start_scan(ar, &arg);
  4720. if (ret) {
  4721. ath10k_warn(ar, "failed to start roc scan: %d\n", ret);
  4722. spin_lock_bh(&ar->data_lock);
  4723. ar->scan.state = ATH10K_SCAN_IDLE;
  4724. spin_unlock_bh(&ar->data_lock);
  4725. goto exit;
  4726. }
  4727. ret = wait_for_completion_timeout(&ar->scan.on_channel, 3*HZ);
  4728. if (ret == 0) {
  4729. ath10k_warn(ar, "failed to switch to channel for roc scan\n");
  4730. ret = ath10k_scan_stop(ar);
  4731. if (ret)
  4732. ath10k_warn(ar, "failed to stop scan: %d\n", ret);
  4733. ret = -ETIMEDOUT;
  4734. goto exit;
  4735. }
  4736. ieee80211_queue_delayed_work(ar->hw, &ar->scan.timeout,
  4737. msecs_to_jiffies(duration));
  4738. ret = 0;
  4739. exit:
  4740. mutex_unlock(&ar->conf_mutex);
  4741. return ret;
  4742. }
  4743. static int ath10k_cancel_remain_on_channel(struct ieee80211_hw *hw)
  4744. {
  4745. struct ath10k *ar = hw->priv;
  4746. mutex_lock(&ar->conf_mutex);
  4747. spin_lock_bh(&ar->data_lock);
  4748. ar->scan.roc_notify = false;
  4749. spin_unlock_bh(&ar->data_lock);
  4750. ath10k_scan_abort(ar);
  4751. mutex_unlock(&ar->conf_mutex);
  4752. cancel_delayed_work_sync(&ar->scan.timeout);
  4753. return 0;
  4754. }
  4755. /*
  4756. * Both RTS and Fragmentation threshold are interface-specific
  4757. * in ath10k, but device-specific in mac80211.
  4758. */
  4759. static int ath10k_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
  4760. {
  4761. struct ath10k *ar = hw->priv;
  4762. struct ath10k_vif *arvif;
  4763. int ret = 0;
  4764. mutex_lock(&ar->conf_mutex);
  4765. list_for_each_entry(arvif, &ar->arvifs, list) {
  4766. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d rts threshold %d\n",
  4767. arvif->vdev_id, value);
  4768. ret = ath10k_mac_set_rts(arvif, value);
  4769. if (ret) {
  4770. ath10k_warn(ar, "failed to set rts threshold for vdev %d: %d\n",
  4771. arvif->vdev_id, ret);
  4772. break;
  4773. }
  4774. }
  4775. mutex_unlock(&ar->conf_mutex);
  4776. return ret;
  4777. }
  4778. static int ath10k_mac_op_set_frag_threshold(struct ieee80211_hw *hw, u32 value)
  4779. {
  4780. /* Even though there's a WMI enum for fragmentation threshold no known
  4781. * firmware actually implements it. Moreover it is not possible to rely
  4782. * frame fragmentation to mac80211 because firmware clears the "more
  4783. * fragments" bit in frame control making it impossible for remote
  4784. * devices to reassemble frames.
  4785. *
  4786. * Hence implement a dummy callback just to say fragmentation isn't
  4787. * supported. This effectively prevents mac80211 from doing frame
  4788. * fragmentation in software.
  4789. */
  4790. return -EOPNOTSUPP;
  4791. }
  4792. static void ath10k_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  4793. u32 queues, bool drop)
  4794. {
  4795. struct ath10k *ar = hw->priv;
  4796. bool skip;
  4797. long time_left;
  4798. /* mac80211 doesn't care if we really xmit queued frames or not
  4799. * we'll collect those frames either way if we stop/delete vdevs */
  4800. if (drop)
  4801. return;
  4802. mutex_lock(&ar->conf_mutex);
  4803. if (ar->state == ATH10K_STATE_WEDGED)
  4804. goto skip;
  4805. time_left = wait_event_timeout(ar->htt.empty_tx_wq, ({
  4806. bool empty;
  4807. spin_lock_bh(&ar->htt.tx_lock);
  4808. empty = (ar->htt.num_pending_tx == 0);
  4809. spin_unlock_bh(&ar->htt.tx_lock);
  4810. skip = (ar->state == ATH10K_STATE_WEDGED) ||
  4811. test_bit(ATH10K_FLAG_CRASH_FLUSH,
  4812. &ar->dev_flags);
  4813. (empty || skip);
  4814. }), ATH10K_FLUSH_TIMEOUT_HZ);
  4815. if (time_left == 0 || skip)
  4816. ath10k_warn(ar, "failed to flush transmit queue (skip %i ar-state %i): %ld\n",
  4817. skip, ar->state, time_left);
  4818. skip:
  4819. mutex_unlock(&ar->conf_mutex);
  4820. }
  4821. /* TODO: Implement this function properly
  4822. * For now it is needed to reply to Probe Requests in IBSS mode.
  4823. * Propably we need this information from FW.
  4824. */
  4825. static int ath10k_tx_last_beacon(struct ieee80211_hw *hw)
  4826. {
  4827. return 1;
  4828. }
  4829. static void ath10k_reconfig_complete(struct ieee80211_hw *hw,
  4830. enum ieee80211_reconfig_type reconfig_type)
  4831. {
  4832. struct ath10k *ar = hw->priv;
  4833. if (reconfig_type != IEEE80211_RECONFIG_TYPE_RESTART)
  4834. return;
  4835. mutex_lock(&ar->conf_mutex);
  4836. /* If device failed to restart it will be in a different state, e.g.
  4837. * ATH10K_STATE_WEDGED */
  4838. if (ar->state == ATH10K_STATE_RESTARTED) {
  4839. ath10k_info(ar, "device successfully recovered\n");
  4840. ar->state = ATH10K_STATE_ON;
  4841. ieee80211_wake_queues(ar->hw);
  4842. }
  4843. mutex_unlock(&ar->conf_mutex);
  4844. }
  4845. static int ath10k_get_survey(struct ieee80211_hw *hw, int idx,
  4846. struct survey_info *survey)
  4847. {
  4848. struct ath10k *ar = hw->priv;
  4849. struct ieee80211_supported_band *sband;
  4850. struct survey_info *ar_survey = &ar->survey[idx];
  4851. int ret = 0;
  4852. mutex_lock(&ar->conf_mutex);
  4853. sband = hw->wiphy->bands[IEEE80211_BAND_2GHZ];
  4854. if (sband && idx >= sband->n_channels) {
  4855. idx -= sband->n_channels;
  4856. sband = NULL;
  4857. }
  4858. if (!sband)
  4859. sband = hw->wiphy->bands[IEEE80211_BAND_5GHZ];
  4860. if (!sband || idx >= sband->n_channels) {
  4861. ret = -ENOENT;
  4862. goto exit;
  4863. }
  4864. spin_lock_bh(&ar->data_lock);
  4865. memcpy(survey, ar_survey, sizeof(*survey));
  4866. spin_unlock_bh(&ar->data_lock);
  4867. survey->channel = &sband->channels[idx];
  4868. if (ar->rx_channel == survey->channel)
  4869. survey->filled |= SURVEY_INFO_IN_USE;
  4870. exit:
  4871. mutex_unlock(&ar->conf_mutex);
  4872. return ret;
  4873. }
  4874. static bool
  4875. ath10k_mac_bitrate_mask_has_single_rate(struct ath10k *ar,
  4876. enum ieee80211_band band,
  4877. const struct cfg80211_bitrate_mask *mask)
  4878. {
  4879. int num_rates = 0;
  4880. int i;
  4881. num_rates += hweight32(mask->control[band].legacy);
  4882. for (i = 0; i < ARRAY_SIZE(mask->control[band].ht_mcs); i++)
  4883. num_rates += hweight8(mask->control[band].ht_mcs[i]);
  4884. for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++)
  4885. num_rates += hweight16(mask->control[band].vht_mcs[i]);
  4886. return num_rates == 1;
  4887. }
  4888. static bool
  4889. ath10k_mac_bitrate_mask_get_single_nss(struct ath10k *ar,
  4890. enum ieee80211_band band,
  4891. const struct cfg80211_bitrate_mask *mask,
  4892. int *nss)
  4893. {
  4894. struct ieee80211_supported_band *sband = &ar->mac.sbands[band];
  4895. u16 vht_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
  4896. u8 ht_nss_mask = 0;
  4897. u8 vht_nss_mask = 0;
  4898. int i;
  4899. if (mask->control[band].legacy)
  4900. return false;
  4901. for (i = 0; i < ARRAY_SIZE(mask->control[band].ht_mcs); i++) {
  4902. if (mask->control[band].ht_mcs[i] == 0)
  4903. continue;
  4904. else if (mask->control[band].ht_mcs[i] ==
  4905. sband->ht_cap.mcs.rx_mask[i])
  4906. ht_nss_mask |= BIT(i);
  4907. else
  4908. return false;
  4909. }
  4910. for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++) {
  4911. if (mask->control[band].vht_mcs[i] == 0)
  4912. continue;
  4913. else if (mask->control[band].vht_mcs[i] ==
  4914. ath10k_mac_get_max_vht_mcs_map(vht_mcs_map, i))
  4915. vht_nss_mask |= BIT(i);
  4916. else
  4917. return false;
  4918. }
  4919. if (ht_nss_mask != vht_nss_mask)
  4920. return false;
  4921. if (ht_nss_mask == 0)
  4922. return false;
  4923. if (BIT(fls(ht_nss_mask)) - 1 != ht_nss_mask)
  4924. return false;
  4925. *nss = fls(ht_nss_mask);
  4926. return true;
  4927. }
  4928. static int
  4929. ath10k_mac_bitrate_mask_get_single_rate(struct ath10k *ar,
  4930. enum ieee80211_band band,
  4931. const struct cfg80211_bitrate_mask *mask,
  4932. u8 *rate, u8 *nss)
  4933. {
  4934. struct ieee80211_supported_band *sband = &ar->mac.sbands[band];
  4935. int rate_idx;
  4936. int i;
  4937. u16 bitrate;
  4938. u8 preamble;
  4939. u8 hw_rate;
  4940. if (hweight32(mask->control[band].legacy) == 1) {
  4941. rate_idx = ffs(mask->control[band].legacy) - 1;
  4942. hw_rate = sband->bitrates[rate_idx].hw_value;
  4943. bitrate = sband->bitrates[rate_idx].bitrate;
  4944. if (ath10k_mac_bitrate_is_cck(bitrate))
  4945. preamble = WMI_RATE_PREAMBLE_CCK;
  4946. else
  4947. preamble = WMI_RATE_PREAMBLE_OFDM;
  4948. *nss = 1;
  4949. *rate = preamble << 6 |
  4950. (*nss - 1) << 4 |
  4951. hw_rate << 0;
  4952. return 0;
  4953. }
  4954. for (i = 0; i < ARRAY_SIZE(mask->control[band].ht_mcs); i++) {
  4955. if (hweight8(mask->control[band].ht_mcs[i]) == 1) {
  4956. *nss = i + 1;
  4957. *rate = WMI_RATE_PREAMBLE_HT << 6 |
  4958. (*nss - 1) << 4 |
  4959. (ffs(mask->control[band].ht_mcs[i]) - 1);
  4960. return 0;
  4961. }
  4962. }
  4963. for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++) {
  4964. if (hweight16(mask->control[band].vht_mcs[i]) == 1) {
  4965. *nss = i + 1;
  4966. *rate = WMI_RATE_PREAMBLE_VHT << 6 |
  4967. (*nss - 1) << 4 |
  4968. (ffs(mask->control[band].vht_mcs[i]) - 1);
  4969. return 0;
  4970. }
  4971. }
  4972. return -EINVAL;
  4973. }
  4974. static int ath10k_mac_set_fixed_rate_params(struct ath10k_vif *arvif,
  4975. u8 rate, u8 nss, u8 sgi, u8 ldpc)
  4976. {
  4977. struct ath10k *ar = arvif->ar;
  4978. u32 vdev_param;
  4979. int ret;
  4980. lockdep_assert_held(&ar->conf_mutex);
  4981. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac set fixed rate params vdev %i rate 0x%02hhx nss %hhu sgi %hhu\n",
  4982. arvif->vdev_id, rate, nss, sgi);
  4983. vdev_param = ar->wmi.vdev_param->fixed_rate;
  4984. ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param, rate);
  4985. if (ret) {
  4986. ath10k_warn(ar, "failed to set fixed rate param 0x%02x: %d\n",
  4987. rate, ret);
  4988. return ret;
  4989. }
  4990. vdev_param = ar->wmi.vdev_param->nss;
  4991. ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param, nss);
  4992. if (ret) {
  4993. ath10k_warn(ar, "failed to set nss param %d: %d\n", nss, ret);
  4994. return ret;
  4995. }
  4996. vdev_param = ar->wmi.vdev_param->sgi;
  4997. ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param, sgi);
  4998. if (ret) {
  4999. ath10k_warn(ar, "failed to set sgi param %d: %d\n", sgi, ret);
  5000. return ret;
  5001. }
  5002. vdev_param = ar->wmi.vdev_param->ldpc;
  5003. ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param, ldpc);
  5004. if (ret) {
  5005. ath10k_warn(ar, "failed to set ldpc param %d: %d\n", ldpc, ret);
  5006. return ret;
  5007. }
  5008. return 0;
  5009. }
  5010. static bool
  5011. ath10k_mac_can_set_bitrate_mask(struct ath10k *ar,
  5012. enum ieee80211_band band,
  5013. const struct cfg80211_bitrate_mask *mask)
  5014. {
  5015. int i;
  5016. u16 vht_mcs;
  5017. /* Due to firmware limitation in WMI_PEER_ASSOC_CMDID it is impossible
  5018. * to express all VHT MCS rate masks. Effectively only the following
  5019. * ranges can be used: none, 0-7, 0-8 and 0-9.
  5020. */
  5021. for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
  5022. vht_mcs = mask->control[band].vht_mcs[i];
  5023. switch (vht_mcs) {
  5024. case 0:
  5025. case BIT(8) - 1:
  5026. case BIT(9) - 1:
  5027. case BIT(10) - 1:
  5028. break;
  5029. default:
  5030. ath10k_warn(ar, "refusing bitrate mask with missing 0-7 VHT MCS rates\n");
  5031. return false;
  5032. }
  5033. }
  5034. return true;
  5035. }
  5036. static void ath10k_mac_set_bitrate_mask_iter(void *data,
  5037. struct ieee80211_sta *sta)
  5038. {
  5039. struct ath10k_vif *arvif = data;
  5040. struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
  5041. struct ath10k *ar = arvif->ar;
  5042. if (arsta->arvif != arvif)
  5043. return;
  5044. spin_lock_bh(&ar->data_lock);
  5045. arsta->changed |= IEEE80211_RC_SUPP_RATES_CHANGED;
  5046. spin_unlock_bh(&ar->data_lock);
  5047. ieee80211_queue_work(ar->hw, &arsta->update_wk);
  5048. }
  5049. static int ath10k_mac_op_set_bitrate_mask(struct ieee80211_hw *hw,
  5050. struct ieee80211_vif *vif,
  5051. const struct cfg80211_bitrate_mask *mask)
  5052. {
  5053. struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
  5054. struct cfg80211_chan_def def;
  5055. struct ath10k *ar = arvif->ar;
  5056. enum ieee80211_band band;
  5057. const u8 *ht_mcs_mask;
  5058. const u16 *vht_mcs_mask;
  5059. u8 rate;
  5060. u8 nss;
  5061. u8 sgi;
  5062. u8 ldpc;
  5063. int single_nss;
  5064. int ret;
  5065. if (ath10k_mac_vif_chan(vif, &def))
  5066. return -EPERM;
  5067. band = def.chan->band;
  5068. ht_mcs_mask = mask->control[band].ht_mcs;
  5069. vht_mcs_mask = mask->control[band].vht_mcs;
  5070. ldpc = !!(ar->ht_cap_info & WMI_HT_CAP_LDPC);
  5071. sgi = mask->control[band].gi;
  5072. if (sgi == NL80211_TXRATE_FORCE_LGI)
  5073. return -EINVAL;
  5074. if (ath10k_mac_bitrate_mask_has_single_rate(ar, band, mask)) {
  5075. ret = ath10k_mac_bitrate_mask_get_single_rate(ar, band, mask,
  5076. &rate, &nss);
  5077. if (ret) {
  5078. ath10k_warn(ar, "failed to get single rate for vdev %i: %d\n",
  5079. arvif->vdev_id, ret);
  5080. return ret;
  5081. }
  5082. } else if (ath10k_mac_bitrate_mask_get_single_nss(ar, band, mask,
  5083. &single_nss)) {
  5084. rate = WMI_FIXED_RATE_NONE;
  5085. nss = single_nss;
  5086. } else {
  5087. rate = WMI_FIXED_RATE_NONE;
  5088. nss = min(ar->num_rf_chains,
  5089. max(ath10k_mac_max_ht_nss(ht_mcs_mask),
  5090. ath10k_mac_max_vht_nss(vht_mcs_mask)));
  5091. if (!ath10k_mac_can_set_bitrate_mask(ar, band, mask))
  5092. return -EINVAL;
  5093. mutex_lock(&ar->conf_mutex);
  5094. arvif->bitrate_mask = *mask;
  5095. ieee80211_iterate_stations_atomic(ar->hw,
  5096. ath10k_mac_set_bitrate_mask_iter,
  5097. arvif);
  5098. mutex_unlock(&ar->conf_mutex);
  5099. }
  5100. mutex_lock(&ar->conf_mutex);
  5101. ret = ath10k_mac_set_fixed_rate_params(arvif, rate, nss, sgi, ldpc);
  5102. if (ret) {
  5103. ath10k_warn(ar, "failed to set fixed rate params on vdev %i: %d\n",
  5104. arvif->vdev_id, ret);
  5105. goto exit;
  5106. }
  5107. exit:
  5108. mutex_unlock(&ar->conf_mutex);
  5109. return ret;
  5110. }
  5111. static void ath10k_sta_rc_update(struct ieee80211_hw *hw,
  5112. struct ieee80211_vif *vif,
  5113. struct ieee80211_sta *sta,
  5114. u32 changed)
  5115. {
  5116. struct ath10k *ar = hw->priv;
  5117. struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
  5118. u32 bw, smps;
  5119. spin_lock_bh(&ar->data_lock);
  5120. ath10k_dbg(ar, ATH10K_DBG_MAC,
  5121. "mac sta rc update for %pM changed %08x bw %d nss %d smps %d\n",
  5122. sta->addr, changed, sta->bandwidth, sta->rx_nss,
  5123. sta->smps_mode);
  5124. if (changed & IEEE80211_RC_BW_CHANGED) {
  5125. bw = WMI_PEER_CHWIDTH_20MHZ;
  5126. switch (sta->bandwidth) {
  5127. case IEEE80211_STA_RX_BW_20:
  5128. bw = WMI_PEER_CHWIDTH_20MHZ;
  5129. break;
  5130. case IEEE80211_STA_RX_BW_40:
  5131. bw = WMI_PEER_CHWIDTH_40MHZ;
  5132. break;
  5133. case IEEE80211_STA_RX_BW_80:
  5134. bw = WMI_PEER_CHWIDTH_80MHZ;
  5135. break;
  5136. case IEEE80211_STA_RX_BW_160:
  5137. ath10k_warn(ar, "Invalid bandwidth %d in rc update for %pM\n",
  5138. sta->bandwidth, sta->addr);
  5139. bw = WMI_PEER_CHWIDTH_20MHZ;
  5140. break;
  5141. }
  5142. arsta->bw = bw;
  5143. }
  5144. if (changed & IEEE80211_RC_NSS_CHANGED)
  5145. arsta->nss = sta->rx_nss;
  5146. if (changed & IEEE80211_RC_SMPS_CHANGED) {
  5147. smps = WMI_PEER_SMPS_PS_NONE;
  5148. switch (sta->smps_mode) {
  5149. case IEEE80211_SMPS_AUTOMATIC:
  5150. case IEEE80211_SMPS_OFF:
  5151. smps = WMI_PEER_SMPS_PS_NONE;
  5152. break;
  5153. case IEEE80211_SMPS_STATIC:
  5154. smps = WMI_PEER_SMPS_STATIC;
  5155. break;
  5156. case IEEE80211_SMPS_DYNAMIC:
  5157. smps = WMI_PEER_SMPS_DYNAMIC;
  5158. break;
  5159. case IEEE80211_SMPS_NUM_MODES:
  5160. ath10k_warn(ar, "Invalid smps %d in sta rc update for %pM\n",
  5161. sta->smps_mode, sta->addr);
  5162. smps = WMI_PEER_SMPS_PS_NONE;
  5163. break;
  5164. }
  5165. arsta->smps = smps;
  5166. }
  5167. arsta->changed |= changed;
  5168. spin_unlock_bh(&ar->data_lock);
  5169. ieee80211_queue_work(hw, &arsta->update_wk);
  5170. }
  5171. static u64 ath10k_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
  5172. {
  5173. /*
  5174. * FIXME: Return 0 for time being. Need to figure out whether FW
  5175. * has the API to fetch 64-bit local TSF
  5176. */
  5177. return 0;
  5178. }
  5179. static int ath10k_ampdu_action(struct ieee80211_hw *hw,
  5180. struct ieee80211_vif *vif,
  5181. enum ieee80211_ampdu_mlme_action action,
  5182. struct ieee80211_sta *sta, u16 tid, u16 *ssn,
  5183. u8 buf_size, bool amsdu)
  5184. {
  5185. struct ath10k *ar = hw->priv;
  5186. struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
  5187. ath10k_dbg(ar, ATH10K_DBG_MAC, "mac ampdu vdev_id %i sta %pM tid %hu action %d\n",
  5188. arvif->vdev_id, sta->addr, tid, action);
  5189. switch (action) {
  5190. case IEEE80211_AMPDU_RX_START:
  5191. case IEEE80211_AMPDU_RX_STOP:
  5192. /* HTT AddBa/DelBa events trigger mac80211 Rx BA session
  5193. * creation/removal. Do we need to verify this?
  5194. */
  5195. return 0;
  5196. case IEEE80211_AMPDU_TX_START:
  5197. case IEEE80211_AMPDU_TX_STOP_CONT:
  5198. case IEEE80211_AMPDU_TX_STOP_FLUSH:
  5199. case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
  5200. case IEEE80211_AMPDU_TX_OPERATIONAL:
  5201. /* Firmware offloads Tx aggregation entirely so deny mac80211
  5202. * Tx aggregation requests.
  5203. */
  5204. return -EOPNOTSUPP;
  5205. }
  5206. return -EINVAL;
  5207. }
  5208. static void
  5209. ath10k_mac_update_rx_channel(struct ath10k *ar,
  5210. struct ieee80211_chanctx_conf *ctx,
  5211. struct ieee80211_vif_chanctx_switch *vifs,
  5212. int n_vifs)
  5213. {
  5214. struct cfg80211_chan_def *def = NULL;
  5215. /* Both locks are required because ar->rx_channel is modified. This
  5216. * allows readers to hold either lock.
  5217. */
  5218. lockdep_assert_held(&ar->conf_mutex);
  5219. lockdep_assert_held(&ar->data_lock);
  5220. WARN_ON(ctx && vifs);
  5221. WARN_ON(vifs && n_vifs != 1);
  5222. /* FIXME: Sort of an optimization and a workaround. Peers and vifs are
  5223. * on a linked list now. Doing a lookup peer -> vif -> chanctx for each
  5224. * ppdu on Rx may reduce performance on low-end systems. It should be
  5225. * possible to make tables/hashmaps to speed the lookup up (be vary of
  5226. * cpu data cache lines though regarding sizes) but to keep the initial
  5227. * implementation simple and less intrusive fallback to the slow lookup
  5228. * only for multi-channel cases. Single-channel cases will remain to
  5229. * use the old channel derival and thus performance should not be
  5230. * affected much.
  5231. */
  5232. rcu_read_lock();
  5233. if (!ctx && ath10k_mac_num_chanctxs(ar) == 1) {
  5234. ieee80211_iter_chan_contexts_atomic(ar->hw,
  5235. ath10k_mac_get_any_chandef_iter,
  5236. &def);
  5237. if (vifs)
  5238. def = &vifs[0].new_ctx->def;
  5239. ar->rx_channel = def->chan;
  5240. } else if (ctx && ath10k_mac_num_chanctxs(ar) == 0) {
  5241. ar->rx_channel = ctx->def.chan;
  5242. } else {
  5243. ar->rx_channel = NULL;
  5244. }
  5245. rcu_read_unlock();
  5246. }
  5247. static void
  5248. ath10k_mac_update_vif_chan(struct ath10k *ar,
  5249. struct ieee80211_vif_chanctx_switch *vifs,
  5250. int n_vifs)
  5251. {
  5252. struct ath10k_vif *arvif;
  5253. int ret;
  5254. int i;
  5255. lockdep_assert_held(&ar->conf_mutex);
  5256. /* First stop monitor interface. Some FW versions crash if there's a
  5257. * lone monitor interface.
  5258. */
  5259. if (ar->monitor_started)
  5260. ath10k_monitor_stop(ar);
  5261. for (i = 0; i < n_vifs; i++) {
  5262. arvif = ath10k_vif_to_arvif(vifs[i].vif);
  5263. ath10k_dbg(ar, ATH10K_DBG_MAC,
  5264. "mac chanctx switch vdev_id %i freq %hu->%hu width %d->%d\n",
  5265. arvif->vdev_id,
  5266. vifs[i].old_ctx->def.chan->center_freq,
  5267. vifs[i].new_ctx->def.chan->center_freq,
  5268. vifs[i].old_ctx->def.width,
  5269. vifs[i].new_ctx->def.width);
  5270. if (WARN_ON(!arvif->is_started))
  5271. continue;
  5272. if (WARN_ON(!arvif->is_up))
  5273. continue;
  5274. ret = ath10k_wmi_vdev_down(ar, arvif->vdev_id);
  5275. if (ret) {
  5276. ath10k_warn(ar, "failed to down vdev %d: %d\n",
  5277. arvif->vdev_id, ret);
  5278. continue;
  5279. }
  5280. }
  5281. /* All relevant vdevs are downed and associated channel resources
  5282. * should be available for the channel switch now.
  5283. */
  5284. spin_lock_bh(&ar->data_lock);
  5285. ath10k_mac_update_rx_channel(ar, NULL, vifs, n_vifs);
  5286. spin_unlock_bh(&ar->data_lock);
  5287. for (i = 0; i < n_vifs; i++) {
  5288. arvif = ath10k_vif_to_arvif(vifs[i].vif);
  5289. if (WARN_ON(!arvif->is_started))
  5290. continue;
  5291. if (WARN_ON(!arvif->is_up))
  5292. continue;
  5293. ret = ath10k_mac_setup_bcn_tmpl(arvif);
  5294. if (ret)
  5295. ath10k_warn(ar, "failed to update bcn tmpl during csa: %d\n",
  5296. ret);
  5297. ret = ath10k_mac_setup_prb_tmpl(arvif);
  5298. if (ret)
  5299. ath10k_warn(ar, "failed to update prb tmpl during csa: %d\n",
  5300. ret);
  5301. ret = ath10k_vdev_restart(arvif, &vifs[i].new_ctx->def);
  5302. if (ret) {
  5303. ath10k_warn(ar, "failed to restart vdev %d: %d\n",
  5304. arvif->vdev_id, ret);
  5305. continue;
  5306. }
  5307. ret = ath10k_wmi_vdev_up(arvif->ar, arvif->vdev_id, arvif->aid,
  5308. arvif->bssid);
  5309. if (ret) {
  5310. ath10k_warn(ar, "failed to bring vdev up %d: %d\n",
  5311. arvif->vdev_id, ret);
  5312. continue;
  5313. }
  5314. }
  5315. ath10k_monitor_recalc(ar);
  5316. }
  5317. static int
  5318. ath10k_mac_op_add_chanctx(struct ieee80211_hw *hw,
  5319. struct ieee80211_chanctx_conf *ctx)
  5320. {
  5321. struct ath10k *ar = hw->priv;
  5322. ath10k_dbg(ar, ATH10K_DBG_MAC,
  5323. "mac chanctx add freq %hu width %d ptr %p\n",
  5324. ctx->def.chan->center_freq, ctx->def.width, ctx);
  5325. mutex_lock(&ar->conf_mutex);
  5326. spin_lock_bh(&ar->data_lock);
  5327. ath10k_mac_update_rx_channel(ar, ctx, NULL, 0);
  5328. spin_unlock_bh(&ar->data_lock);
  5329. ath10k_recalc_radar_detection(ar);
  5330. ath10k_monitor_recalc(ar);
  5331. mutex_unlock(&ar->conf_mutex);
  5332. return 0;
  5333. }
  5334. static void
  5335. ath10k_mac_op_remove_chanctx(struct ieee80211_hw *hw,
  5336. struct ieee80211_chanctx_conf *ctx)
  5337. {
  5338. struct ath10k *ar = hw->priv;
  5339. ath10k_dbg(ar, ATH10K_DBG_MAC,
  5340. "mac chanctx remove freq %hu width %d ptr %p\n",
  5341. ctx->def.chan->center_freq, ctx->def.width, ctx);
  5342. mutex_lock(&ar->conf_mutex);
  5343. spin_lock_bh(&ar->data_lock);
  5344. ath10k_mac_update_rx_channel(ar, NULL, NULL, 0);
  5345. spin_unlock_bh(&ar->data_lock);
  5346. ath10k_recalc_radar_detection(ar);
  5347. ath10k_monitor_recalc(ar);
  5348. mutex_unlock(&ar->conf_mutex);
  5349. }
  5350. struct ath10k_mac_change_chanctx_arg {
  5351. struct ieee80211_chanctx_conf *ctx;
  5352. struct ieee80211_vif_chanctx_switch *vifs;
  5353. int n_vifs;
  5354. int next_vif;
  5355. };
  5356. static void
  5357. ath10k_mac_change_chanctx_cnt_iter(void *data, u8 *mac,
  5358. struct ieee80211_vif *vif)
  5359. {
  5360. struct ath10k_mac_change_chanctx_arg *arg = data;
  5361. if (rcu_access_pointer(vif->chanctx_conf) != arg->ctx)
  5362. return;
  5363. arg->n_vifs++;
  5364. }
  5365. static void
  5366. ath10k_mac_change_chanctx_fill_iter(void *data, u8 *mac,
  5367. struct ieee80211_vif *vif)
  5368. {
  5369. struct ath10k_mac_change_chanctx_arg *arg = data;
  5370. struct ieee80211_chanctx_conf *ctx;
  5371. ctx = rcu_access_pointer(vif->chanctx_conf);
  5372. if (ctx != arg->ctx)
  5373. return;
  5374. if (WARN_ON(arg->next_vif == arg->n_vifs))
  5375. return;
  5376. arg->vifs[arg->next_vif].vif = vif;
  5377. arg->vifs[arg->next_vif].old_ctx = ctx;
  5378. arg->vifs[arg->next_vif].new_ctx = ctx;
  5379. arg->next_vif++;
  5380. }
  5381. static void
  5382. ath10k_mac_op_change_chanctx(struct ieee80211_hw *hw,
  5383. struct ieee80211_chanctx_conf *ctx,
  5384. u32 changed)
  5385. {
  5386. struct ath10k *ar = hw->priv;
  5387. struct ath10k_mac_change_chanctx_arg arg = { .ctx = ctx };
  5388. mutex_lock(&ar->conf_mutex);
  5389. ath10k_dbg(ar, ATH10K_DBG_MAC,
  5390. "mac chanctx change freq %hu width %d ptr %p changed %x\n",
  5391. ctx->def.chan->center_freq, ctx->def.width, ctx, changed);
  5392. /* This shouldn't really happen because channel switching should use
  5393. * switch_vif_chanctx().
  5394. */
  5395. if (WARN_ON(changed & IEEE80211_CHANCTX_CHANGE_CHANNEL))
  5396. goto unlock;
  5397. if (changed & IEEE80211_CHANCTX_CHANGE_WIDTH) {
  5398. ieee80211_iterate_active_interfaces_atomic(
  5399. hw,
  5400. IEEE80211_IFACE_ITER_NORMAL,
  5401. ath10k_mac_change_chanctx_cnt_iter,
  5402. &arg);
  5403. if (arg.n_vifs == 0)
  5404. goto radar;
  5405. arg.vifs = kcalloc(arg.n_vifs, sizeof(arg.vifs[0]),
  5406. GFP_KERNEL);
  5407. if (!arg.vifs)
  5408. goto radar;
  5409. ieee80211_iterate_active_interfaces_atomic(
  5410. hw,
  5411. IEEE80211_IFACE_ITER_NORMAL,
  5412. ath10k_mac_change_chanctx_fill_iter,
  5413. &arg);
  5414. ath10k_mac_update_vif_chan(ar, arg.vifs, arg.n_vifs);
  5415. kfree(arg.vifs);
  5416. }
  5417. radar:
  5418. ath10k_recalc_radar_detection(ar);
  5419. /* FIXME: How to configure Rx chains properly? */
  5420. /* No other actions are actually necessary. Firmware maintains channel
  5421. * definitions per vdev internally and there's no host-side channel
  5422. * context abstraction to configure, e.g. channel width.
  5423. */
  5424. unlock:
  5425. mutex_unlock(&ar->conf_mutex);
  5426. }
  5427. static int
  5428. ath10k_mac_op_assign_vif_chanctx(struct ieee80211_hw *hw,
  5429. struct ieee80211_vif *vif,
  5430. struct ieee80211_chanctx_conf *ctx)
  5431. {
  5432. struct ath10k *ar = hw->priv;
  5433. struct ath10k_vif *arvif = (void *)vif->drv_priv;
  5434. int ret;
  5435. mutex_lock(&ar->conf_mutex);
  5436. ath10k_dbg(ar, ATH10K_DBG_MAC,
  5437. "mac chanctx assign ptr %p vdev_id %i\n",
  5438. ctx, arvif->vdev_id);
  5439. if (WARN_ON(arvif->is_started)) {
  5440. mutex_unlock(&ar->conf_mutex);
  5441. return -EBUSY;
  5442. }
  5443. ret = ath10k_vdev_start(arvif, &ctx->def);
  5444. if (ret) {
  5445. ath10k_warn(ar, "failed to start vdev %i addr %pM on freq %d: %d\n",
  5446. arvif->vdev_id, vif->addr,
  5447. ctx->def.chan->center_freq, ret);
  5448. goto err;
  5449. }
  5450. arvif->is_started = true;
  5451. ret = ath10k_mac_vif_setup_ps(arvif);
  5452. if (ret) {
  5453. ath10k_warn(ar, "failed to update vdev %i ps: %d\n",
  5454. arvif->vdev_id, ret);
  5455. goto err_stop;
  5456. }
  5457. if (vif->type == NL80211_IFTYPE_MONITOR) {
  5458. ret = ath10k_wmi_vdev_up(ar, arvif->vdev_id, 0, vif->addr);
  5459. if (ret) {
  5460. ath10k_warn(ar, "failed to up monitor vdev %i: %d\n",
  5461. arvif->vdev_id, ret);
  5462. goto err_stop;
  5463. }
  5464. arvif->is_up = true;
  5465. }
  5466. mutex_unlock(&ar->conf_mutex);
  5467. return 0;
  5468. err_stop:
  5469. ath10k_vdev_stop(arvif);
  5470. arvif->is_started = false;
  5471. ath10k_mac_vif_setup_ps(arvif);
  5472. err:
  5473. mutex_unlock(&ar->conf_mutex);
  5474. return ret;
  5475. }
  5476. static void
  5477. ath10k_mac_op_unassign_vif_chanctx(struct ieee80211_hw *hw,
  5478. struct ieee80211_vif *vif,
  5479. struct ieee80211_chanctx_conf *ctx)
  5480. {
  5481. struct ath10k *ar = hw->priv;
  5482. struct ath10k_vif *arvif = (void *)vif->drv_priv;
  5483. int ret;
  5484. mutex_lock(&ar->conf_mutex);
  5485. ath10k_dbg(ar, ATH10K_DBG_MAC,
  5486. "mac chanctx unassign ptr %p vdev_id %i\n",
  5487. ctx, arvif->vdev_id);
  5488. WARN_ON(!arvif->is_started);
  5489. if (vif->type == NL80211_IFTYPE_MONITOR) {
  5490. WARN_ON(!arvif->is_up);
  5491. ret = ath10k_wmi_vdev_down(ar, arvif->vdev_id);
  5492. if (ret)
  5493. ath10k_warn(ar, "failed to down monitor vdev %i: %d\n",
  5494. arvif->vdev_id, ret);
  5495. arvif->is_up = false;
  5496. }
  5497. ret = ath10k_vdev_stop(arvif);
  5498. if (ret)
  5499. ath10k_warn(ar, "failed to stop vdev %i: %d\n",
  5500. arvif->vdev_id, ret);
  5501. arvif->is_started = false;
  5502. mutex_unlock(&ar->conf_mutex);
  5503. }
  5504. static int
  5505. ath10k_mac_op_switch_vif_chanctx(struct ieee80211_hw *hw,
  5506. struct ieee80211_vif_chanctx_switch *vifs,
  5507. int n_vifs,
  5508. enum ieee80211_chanctx_switch_mode mode)
  5509. {
  5510. struct ath10k *ar = hw->priv;
  5511. mutex_lock(&ar->conf_mutex);
  5512. ath10k_dbg(ar, ATH10K_DBG_MAC,
  5513. "mac chanctx switch n_vifs %d mode %d\n",
  5514. n_vifs, mode);
  5515. ath10k_mac_update_vif_chan(ar, vifs, n_vifs);
  5516. mutex_unlock(&ar->conf_mutex);
  5517. return 0;
  5518. }
  5519. static const struct ieee80211_ops ath10k_ops = {
  5520. .tx = ath10k_tx,
  5521. .start = ath10k_start,
  5522. .stop = ath10k_stop,
  5523. .config = ath10k_config,
  5524. .add_interface = ath10k_add_interface,
  5525. .remove_interface = ath10k_remove_interface,
  5526. .configure_filter = ath10k_configure_filter,
  5527. .bss_info_changed = ath10k_bss_info_changed,
  5528. .hw_scan = ath10k_hw_scan,
  5529. .cancel_hw_scan = ath10k_cancel_hw_scan,
  5530. .set_key = ath10k_set_key,
  5531. .set_default_unicast_key = ath10k_set_default_unicast_key,
  5532. .sta_state = ath10k_sta_state,
  5533. .conf_tx = ath10k_conf_tx,
  5534. .remain_on_channel = ath10k_remain_on_channel,
  5535. .cancel_remain_on_channel = ath10k_cancel_remain_on_channel,
  5536. .set_rts_threshold = ath10k_set_rts_threshold,
  5537. .set_frag_threshold = ath10k_mac_op_set_frag_threshold,
  5538. .flush = ath10k_flush,
  5539. .tx_last_beacon = ath10k_tx_last_beacon,
  5540. .set_antenna = ath10k_set_antenna,
  5541. .get_antenna = ath10k_get_antenna,
  5542. .reconfig_complete = ath10k_reconfig_complete,
  5543. .get_survey = ath10k_get_survey,
  5544. .set_bitrate_mask = ath10k_mac_op_set_bitrate_mask,
  5545. .sta_rc_update = ath10k_sta_rc_update,
  5546. .get_tsf = ath10k_get_tsf,
  5547. .ampdu_action = ath10k_ampdu_action,
  5548. .get_et_sset_count = ath10k_debug_get_et_sset_count,
  5549. .get_et_stats = ath10k_debug_get_et_stats,
  5550. .get_et_strings = ath10k_debug_get_et_strings,
  5551. .add_chanctx = ath10k_mac_op_add_chanctx,
  5552. .remove_chanctx = ath10k_mac_op_remove_chanctx,
  5553. .change_chanctx = ath10k_mac_op_change_chanctx,
  5554. .assign_vif_chanctx = ath10k_mac_op_assign_vif_chanctx,
  5555. .unassign_vif_chanctx = ath10k_mac_op_unassign_vif_chanctx,
  5556. .switch_vif_chanctx = ath10k_mac_op_switch_vif_chanctx,
  5557. CFG80211_TESTMODE_CMD(ath10k_tm_cmd)
  5558. #ifdef CONFIG_PM
  5559. .suspend = ath10k_wow_op_suspend,
  5560. .resume = ath10k_wow_op_resume,
  5561. #endif
  5562. #ifdef CONFIG_MAC80211_DEBUGFS
  5563. .sta_add_debugfs = ath10k_sta_add_debugfs,
  5564. #endif
  5565. };
  5566. #define CHAN2G(_channel, _freq, _flags) { \
  5567. .band = IEEE80211_BAND_2GHZ, \
  5568. .hw_value = (_channel), \
  5569. .center_freq = (_freq), \
  5570. .flags = (_flags), \
  5571. .max_antenna_gain = 0, \
  5572. .max_power = 30, \
  5573. }
  5574. #define CHAN5G(_channel, _freq, _flags) { \
  5575. .band = IEEE80211_BAND_5GHZ, \
  5576. .hw_value = (_channel), \
  5577. .center_freq = (_freq), \
  5578. .flags = (_flags), \
  5579. .max_antenna_gain = 0, \
  5580. .max_power = 30, \
  5581. }
  5582. static const struct ieee80211_channel ath10k_2ghz_channels[] = {
  5583. CHAN2G(1, 2412, 0),
  5584. CHAN2G(2, 2417, 0),
  5585. CHAN2G(3, 2422, 0),
  5586. CHAN2G(4, 2427, 0),
  5587. CHAN2G(5, 2432, 0),
  5588. CHAN2G(6, 2437, 0),
  5589. CHAN2G(7, 2442, 0),
  5590. CHAN2G(8, 2447, 0),
  5591. CHAN2G(9, 2452, 0),
  5592. CHAN2G(10, 2457, 0),
  5593. CHAN2G(11, 2462, 0),
  5594. CHAN2G(12, 2467, 0),
  5595. CHAN2G(13, 2472, 0),
  5596. CHAN2G(14, 2484, 0),
  5597. };
  5598. static const struct ieee80211_channel ath10k_5ghz_channels[] = {
  5599. CHAN5G(36, 5180, 0),
  5600. CHAN5G(40, 5200, 0),
  5601. CHAN5G(44, 5220, 0),
  5602. CHAN5G(48, 5240, 0),
  5603. CHAN5G(52, 5260, 0),
  5604. CHAN5G(56, 5280, 0),
  5605. CHAN5G(60, 5300, 0),
  5606. CHAN5G(64, 5320, 0),
  5607. CHAN5G(100, 5500, 0),
  5608. CHAN5G(104, 5520, 0),
  5609. CHAN5G(108, 5540, 0),
  5610. CHAN5G(112, 5560, 0),
  5611. CHAN5G(116, 5580, 0),
  5612. CHAN5G(120, 5600, 0),
  5613. CHAN5G(124, 5620, 0),
  5614. CHAN5G(128, 5640, 0),
  5615. CHAN5G(132, 5660, 0),
  5616. CHAN5G(136, 5680, 0),
  5617. CHAN5G(140, 5700, 0),
  5618. CHAN5G(144, 5720, 0),
  5619. CHAN5G(149, 5745, 0),
  5620. CHAN5G(153, 5765, 0),
  5621. CHAN5G(157, 5785, 0),
  5622. CHAN5G(161, 5805, 0),
  5623. CHAN5G(165, 5825, 0),
  5624. };
  5625. struct ath10k *ath10k_mac_create(size_t priv_size)
  5626. {
  5627. struct ieee80211_hw *hw;
  5628. struct ath10k *ar;
  5629. hw = ieee80211_alloc_hw(sizeof(struct ath10k) + priv_size, &ath10k_ops);
  5630. if (!hw)
  5631. return NULL;
  5632. ar = hw->priv;
  5633. ar->hw = hw;
  5634. return ar;
  5635. }
  5636. void ath10k_mac_destroy(struct ath10k *ar)
  5637. {
  5638. ieee80211_free_hw(ar->hw);
  5639. }
  5640. static const struct ieee80211_iface_limit ath10k_if_limits[] = {
  5641. {
  5642. .max = 8,
  5643. .types = BIT(NL80211_IFTYPE_STATION)
  5644. | BIT(NL80211_IFTYPE_P2P_CLIENT)
  5645. },
  5646. {
  5647. .max = 3,
  5648. .types = BIT(NL80211_IFTYPE_P2P_GO)
  5649. },
  5650. {
  5651. .max = 1,
  5652. .types = BIT(NL80211_IFTYPE_P2P_DEVICE)
  5653. },
  5654. {
  5655. .max = 7,
  5656. .types = BIT(NL80211_IFTYPE_AP)
  5657. #ifdef CONFIG_MAC80211_MESH
  5658. | BIT(NL80211_IFTYPE_MESH_POINT)
  5659. #endif
  5660. },
  5661. };
  5662. static const struct ieee80211_iface_limit ath10k_10x_if_limits[] = {
  5663. {
  5664. .max = 8,
  5665. .types = BIT(NL80211_IFTYPE_AP)
  5666. #ifdef CONFIG_MAC80211_MESH
  5667. | BIT(NL80211_IFTYPE_MESH_POINT)
  5668. #endif
  5669. },
  5670. };
  5671. static const struct ieee80211_iface_combination ath10k_if_comb[] = {
  5672. {
  5673. .limits = ath10k_if_limits,
  5674. .n_limits = ARRAY_SIZE(ath10k_if_limits),
  5675. .max_interfaces = 8,
  5676. .num_different_channels = 1,
  5677. .beacon_int_infra_match = true,
  5678. },
  5679. };
  5680. static const struct ieee80211_iface_combination ath10k_10x_if_comb[] = {
  5681. {
  5682. .limits = ath10k_10x_if_limits,
  5683. .n_limits = ARRAY_SIZE(ath10k_10x_if_limits),
  5684. .max_interfaces = 8,
  5685. .num_different_channels = 1,
  5686. .beacon_int_infra_match = true,
  5687. #ifdef CONFIG_ATH10K_DFS_CERTIFIED
  5688. .radar_detect_widths = BIT(NL80211_CHAN_WIDTH_20_NOHT) |
  5689. BIT(NL80211_CHAN_WIDTH_20) |
  5690. BIT(NL80211_CHAN_WIDTH_40) |
  5691. BIT(NL80211_CHAN_WIDTH_80),
  5692. #endif
  5693. },
  5694. };
  5695. static const struct ieee80211_iface_limit ath10k_tlv_if_limit[] = {
  5696. {
  5697. .max = 2,
  5698. .types = BIT(NL80211_IFTYPE_STATION),
  5699. },
  5700. {
  5701. .max = 2,
  5702. .types = BIT(NL80211_IFTYPE_AP) |
  5703. #ifdef CONFIG_MAC80211_MESH
  5704. BIT(NL80211_IFTYPE_MESH_POINT) |
  5705. #endif
  5706. BIT(NL80211_IFTYPE_P2P_CLIENT) |
  5707. BIT(NL80211_IFTYPE_P2P_GO),
  5708. },
  5709. {
  5710. .max = 1,
  5711. .types = BIT(NL80211_IFTYPE_P2P_DEVICE),
  5712. },
  5713. };
  5714. static const struct ieee80211_iface_limit ath10k_tlv_qcs_if_limit[] = {
  5715. {
  5716. .max = 2,
  5717. .types = BIT(NL80211_IFTYPE_STATION),
  5718. },
  5719. {
  5720. .max = 2,
  5721. .types = BIT(NL80211_IFTYPE_P2P_CLIENT),
  5722. },
  5723. {
  5724. .max = 1,
  5725. .types = BIT(NL80211_IFTYPE_AP) |
  5726. #ifdef CONFIG_MAC80211_MESH
  5727. BIT(NL80211_IFTYPE_MESH_POINT) |
  5728. #endif
  5729. BIT(NL80211_IFTYPE_P2P_GO),
  5730. },
  5731. {
  5732. .max = 1,
  5733. .types = BIT(NL80211_IFTYPE_P2P_DEVICE),
  5734. },
  5735. };
  5736. static const struct ieee80211_iface_limit ath10k_tlv_if_limit_ibss[] = {
  5737. {
  5738. .max = 1,
  5739. .types = BIT(NL80211_IFTYPE_STATION),
  5740. },
  5741. {
  5742. .max = 1,
  5743. .types = BIT(NL80211_IFTYPE_ADHOC),
  5744. },
  5745. };
  5746. /* FIXME: This is not thouroughly tested. These combinations may over- or
  5747. * underestimate hw/fw capabilities.
  5748. */
  5749. static struct ieee80211_iface_combination ath10k_tlv_if_comb[] = {
  5750. {
  5751. .limits = ath10k_tlv_if_limit,
  5752. .num_different_channels = 1,
  5753. .max_interfaces = 4,
  5754. .n_limits = ARRAY_SIZE(ath10k_tlv_if_limit),
  5755. },
  5756. {
  5757. .limits = ath10k_tlv_if_limit_ibss,
  5758. .num_different_channels = 1,
  5759. .max_interfaces = 2,
  5760. .n_limits = ARRAY_SIZE(ath10k_tlv_if_limit_ibss),
  5761. },
  5762. };
  5763. static struct ieee80211_iface_combination ath10k_tlv_qcs_if_comb[] = {
  5764. {
  5765. .limits = ath10k_tlv_if_limit,
  5766. .num_different_channels = 1,
  5767. .max_interfaces = 4,
  5768. .n_limits = ARRAY_SIZE(ath10k_tlv_if_limit),
  5769. },
  5770. {
  5771. .limits = ath10k_tlv_qcs_if_limit,
  5772. .num_different_channels = 2,
  5773. .max_interfaces = 4,
  5774. .n_limits = ARRAY_SIZE(ath10k_tlv_qcs_if_limit),
  5775. },
  5776. {
  5777. .limits = ath10k_tlv_if_limit_ibss,
  5778. .num_different_channels = 1,
  5779. .max_interfaces = 2,
  5780. .n_limits = ARRAY_SIZE(ath10k_tlv_if_limit_ibss),
  5781. },
  5782. };
  5783. static const struct ieee80211_iface_limit ath10k_10_4_if_limits[] = {
  5784. {
  5785. .max = 1,
  5786. .types = BIT(NL80211_IFTYPE_STATION),
  5787. },
  5788. {
  5789. .max = 16,
  5790. .types = BIT(NL80211_IFTYPE_AP)
  5791. #ifdef CONFIG_MAC80211_MESH
  5792. | BIT(NL80211_IFTYPE_MESH_POINT)
  5793. #endif
  5794. },
  5795. };
  5796. static const struct ieee80211_iface_combination ath10k_10_4_if_comb[] = {
  5797. {
  5798. .limits = ath10k_10_4_if_limits,
  5799. .n_limits = ARRAY_SIZE(ath10k_10_4_if_limits),
  5800. .max_interfaces = 16,
  5801. .num_different_channels = 1,
  5802. .beacon_int_infra_match = true,
  5803. #ifdef CONFIG_ATH10K_DFS_CERTIFIED
  5804. .radar_detect_widths = BIT(NL80211_CHAN_WIDTH_20_NOHT) |
  5805. BIT(NL80211_CHAN_WIDTH_20) |
  5806. BIT(NL80211_CHAN_WIDTH_40) |
  5807. BIT(NL80211_CHAN_WIDTH_80),
  5808. #endif
  5809. },
  5810. };
  5811. static void ath10k_get_arvif_iter(void *data, u8 *mac,
  5812. struct ieee80211_vif *vif)
  5813. {
  5814. struct ath10k_vif_iter *arvif_iter = data;
  5815. struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
  5816. if (arvif->vdev_id == arvif_iter->vdev_id)
  5817. arvif_iter->arvif = arvif;
  5818. }
  5819. struct ath10k_vif *ath10k_get_arvif(struct ath10k *ar, u32 vdev_id)
  5820. {
  5821. struct ath10k_vif_iter arvif_iter;
  5822. u32 flags;
  5823. memset(&arvif_iter, 0, sizeof(struct ath10k_vif_iter));
  5824. arvif_iter.vdev_id = vdev_id;
  5825. flags = IEEE80211_IFACE_ITER_RESUME_ALL;
  5826. ieee80211_iterate_active_interfaces_atomic(ar->hw,
  5827. flags,
  5828. ath10k_get_arvif_iter,
  5829. &arvif_iter);
  5830. if (!arvif_iter.arvif) {
  5831. ath10k_warn(ar, "No VIF found for vdev %d\n", vdev_id);
  5832. return NULL;
  5833. }
  5834. return arvif_iter.arvif;
  5835. }
  5836. int ath10k_mac_register(struct ath10k *ar)
  5837. {
  5838. static const u32 cipher_suites[] = {
  5839. WLAN_CIPHER_SUITE_WEP40,
  5840. WLAN_CIPHER_SUITE_WEP104,
  5841. WLAN_CIPHER_SUITE_TKIP,
  5842. WLAN_CIPHER_SUITE_CCMP,
  5843. WLAN_CIPHER_SUITE_AES_CMAC,
  5844. };
  5845. struct ieee80211_supported_band *band;
  5846. void *channels;
  5847. int ret;
  5848. SET_IEEE80211_PERM_ADDR(ar->hw, ar->mac_addr);
  5849. SET_IEEE80211_DEV(ar->hw, ar->dev);
  5850. BUILD_BUG_ON((ARRAY_SIZE(ath10k_2ghz_channels) +
  5851. ARRAY_SIZE(ath10k_5ghz_channels)) !=
  5852. ATH10K_NUM_CHANS);
  5853. if (ar->phy_capability & WHAL_WLAN_11G_CAPABILITY) {
  5854. channels = kmemdup(ath10k_2ghz_channels,
  5855. sizeof(ath10k_2ghz_channels),
  5856. GFP_KERNEL);
  5857. if (!channels) {
  5858. ret = -ENOMEM;
  5859. goto err_free;
  5860. }
  5861. band = &ar->mac.sbands[IEEE80211_BAND_2GHZ];
  5862. band->n_channels = ARRAY_SIZE(ath10k_2ghz_channels);
  5863. band->channels = channels;
  5864. band->n_bitrates = ath10k_g_rates_size;
  5865. band->bitrates = ath10k_g_rates;
  5866. ar->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = band;
  5867. }
  5868. if (ar->phy_capability & WHAL_WLAN_11A_CAPABILITY) {
  5869. channels = kmemdup(ath10k_5ghz_channels,
  5870. sizeof(ath10k_5ghz_channels),
  5871. GFP_KERNEL);
  5872. if (!channels) {
  5873. ret = -ENOMEM;
  5874. goto err_free;
  5875. }
  5876. band = &ar->mac.sbands[IEEE80211_BAND_5GHZ];
  5877. band->n_channels = ARRAY_SIZE(ath10k_5ghz_channels);
  5878. band->channels = channels;
  5879. band->n_bitrates = ath10k_a_rates_size;
  5880. band->bitrates = ath10k_a_rates;
  5881. ar->hw->wiphy->bands[IEEE80211_BAND_5GHZ] = band;
  5882. }
  5883. ath10k_mac_setup_ht_vht_cap(ar);
  5884. ar->hw->wiphy->interface_modes =
  5885. BIT(NL80211_IFTYPE_STATION) |
  5886. BIT(NL80211_IFTYPE_AP) |
  5887. BIT(NL80211_IFTYPE_MESH_POINT);
  5888. ar->hw->wiphy->available_antennas_rx = ar->cfg_rx_chainmask;
  5889. ar->hw->wiphy->available_antennas_tx = ar->cfg_tx_chainmask;
  5890. if (!test_bit(ATH10K_FW_FEATURE_NO_P2P, ar->fw_features))
  5891. ar->hw->wiphy->interface_modes |=
  5892. BIT(NL80211_IFTYPE_P2P_DEVICE) |
  5893. BIT(NL80211_IFTYPE_P2P_CLIENT) |
  5894. BIT(NL80211_IFTYPE_P2P_GO);
  5895. ieee80211_hw_set(ar->hw, SIGNAL_DBM);
  5896. ieee80211_hw_set(ar->hw, SUPPORTS_PS);
  5897. ieee80211_hw_set(ar->hw, SUPPORTS_DYNAMIC_PS);
  5898. ieee80211_hw_set(ar->hw, MFP_CAPABLE);
  5899. ieee80211_hw_set(ar->hw, REPORTS_TX_ACK_STATUS);
  5900. ieee80211_hw_set(ar->hw, HAS_RATE_CONTROL);
  5901. ieee80211_hw_set(ar->hw, AP_LINK_PS);
  5902. ieee80211_hw_set(ar->hw, SPECTRUM_MGMT);
  5903. ieee80211_hw_set(ar->hw, SUPPORT_FAST_XMIT);
  5904. ieee80211_hw_set(ar->hw, CONNECTION_MONITOR);
  5905. ieee80211_hw_set(ar->hw, SUPPORTS_PER_STA_GTK);
  5906. ieee80211_hw_set(ar->hw, WANT_MONITOR_VIF);
  5907. ieee80211_hw_set(ar->hw, CHANCTX_STA_CSA);
  5908. ieee80211_hw_set(ar->hw, QUEUE_CONTROL);
  5909. if (!test_bit(ATH10K_FLAG_RAW_MODE, &ar->dev_flags))
  5910. ieee80211_hw_set(ar->hw, SW_CRYPTO_CONTROL);
  5911. ar->hw->wiphy->features |= NL80211_FEATURE_STATIC_SMPS;
  5912. ar->hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
  5913. if (ar->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS)
  5914. ar->hw->wiphy->features |= NL80211_FEATURE_DYNAMIC_SMPS;
  5915. if (ar->ht_cap_info & WMI_HT_CAP_ENABLED) {
  5916. ieee80211_hw_set(ar->hw, AMPDU_AGGREGATION);
  5917. ieee80211_hw_set(ar->hw, TX_AMPDU_SETUP_IN_HW);
  5918. }
  5919. ar->hw->wiphy->max_scan_ssids = WLAN_SCAN_PARAMS_MAX_SSID;
  5920. ar->hw->wiphy->max_scan_ie_len = WLAN_SCAN_PARAMS_MAX_IE_LEN;
  5921. ar->hw->vif_data_size = sizeof(struct ath10k_vif);
  5922. ar->hw->sta_data_size = sizeof(struct ath10k_sta);
  5923. ar->hw->max_listen_interval = ATH10K_MAX_HW_LISTEN_INTERVAL;
  5924. if (test_bit(WMI_SERVICE_BEACON_OFFLOAD, ar->wmi.svc_map)) {
  5925. ar->hw->wiphy->flags |= WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD;
  5926. /* Firmware delivers WPS/P2P Probe Requests frames to driver so
  5927. * that userspace (e.g. wpa_supplicant/hostapd) can generate
  5928. * correct Probe Responses. This is more of a hack advert..
  5929. */
  5930. ar->hw->wiphy->probe_resp_offload |=
  5931. NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS |
  5932. NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2 |
  5933. NL80211_PROBE_RESP_OFFLOAD_SUPPORT_P2P;
  5934. }
  5935. if (test_bit(WMI_SERVICE_TDLS, ar->wmi.svc_map))
  5936. ar->hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_TDLS;
  5937. ar->hw->wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
  5938. ar->hw->wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH;
  5939. ar->hw->wiphy->max_remain_on_channel_duration = 5000;
  5940. ar->hw->wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
  5941. ar->hw->wiphy->features |= NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE;
  5942. ar->hw->wiphy->max_ap_assoc_sta = ar->max_num_stations;
  5943. ret = ath10k_wow_init(ar);
  5944. if (ret) {
  5945. ath10k_warn(ar, "failed to init wow: %d\n", ret);
  5946. goto err_free;
  5947. }
  5948. wiphy_ext_feature_set(ar->hw->wiphy, NL80211_EXT_FEATURE_VHT_IBSS);
  5949. /*
  5950. * on LL hardware queues are managed entirely by the FW
  5951. * so we only advertise to mac we can do the queues thing
  5952. */
  5953. ar->hw->queues = IEEE80211_MAX_QUEUES;
  5954. /* vdev_ids are used as hw queue numbers. Make sure offchan tx queue is
  5955. * something that vdev_ids can't reach so that we don't stop the queue
  5956. * accidentally.
  5957. */
  5958. ar->hw->offchannel_tx_hw_queue = IEEE80211_MAX_QUEUES - 1;
  5959. switch (ar->wmi.op_version) {
  5960. case ATH10K_FW_WMI_OP_VERSION_MAIN:
  5961. ar->hw->wiphy->iface_combinations = ath10k_if_comb;
  5962. ar->hw->wiphy->n_iface_combinations =
  5963. ARRAY_SIZE(ath10k_if_comb);
  5964. ar->hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_ADHOC);
  5965. break;
  5966. case ATH10K_FW_WMI_OP_VERSION_TLV:
  5967. if (test_bit(WMI_SERVICE_ADAPTIVE_OCS, ar->wmi.svc_map)) {
  5968. ar->hw->wiphy->iface_combinations =
  5969. ath10k_tlv_qcs_if_comb;
  5970. ar->hw->wiphy->n_iface_combinations =
  5971. ARRAY_SIZE(ath10k_tlv_qcs_if_comb);
  5972. } else {
  5973. ar->hw->wiphy->iface_combinations = ath10k_tlv_if_comb;
  5974. ar->hw->wiphy->n_iface_combinations =
  5975. ARRAY_SIZE(ath10k_tlv_if_comb);
  5976. }
  5977. ar->hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_ADHOC);
  5978. break;
  5979. case ATH10K_FW_WMI_OP_VERSION_10_1:
  5980. case ATH10K_FW_WMI_OP_VERSION_10_2:
  5981. case ATH10K_FW_WMI_OP_VERSION_10_2_4:
  5982. ar->hw->wiphy->iface_combinations = ath10k_10x_if_comb;
  5983. ar->hw->wiphy->n_iface_combinations =
  5984. ARRAY_SIZE(ath10k_10x_if_comb);
  5985. break;
  5986. case ATH10K_FW_WMI_OP_VERSION_10_4:
  5987. ar->hw->wiphy->iface_combinations = ath10k_10_4_if_comb;
  5988. ar->hw->wiphy->n_iface_combinations =
  5989. ARRAY_SIZE(ath10k_10_4_if_comb);
  5990. break;
  5991. case ATH10K_FW_WMI_OP_VERSION_UNSET:
  5992. case ATH10K_FW_WMI_OP_VERSION_MAX:
  5993. WARN_ON(1);
  5994. ret = -EINVAL;
  5995. goto err_free;
  5996. }
  5997. if (!test_bit(ATH10K_FLAG_RAW_MODE, &ar->dev_flags))
  5998. ar->hw->netdev_features = NETIF_F_HW_CSUM;
  5999. if (config_enabled(CONFIG_ATH10K_DFS_CERTIFIED)) {
  6000. /* Init ath dfs pattern detector */
  6001. ar->ath_common.debug_mask = ATH_DBG_DFS;
  6002. ar->dfs_detector = dfs_pattern_detector_init(&ar->ath_common,
  6003. NL80211_DFS_UNSET);
  6004. if (!ar->dfs_detector)
  6005. ath10k_warn(ar, "failed to initialise DFS pattern detector\n");
  6006. }
  6007. ret = ath_regd_init(&ar->ath_common.regulatory, ar->hw->wiphy,
  6008. ath10k_reg_notifier);
  6009. if (ret) {
  6010. ath10k_err(ar, "failed to initialise regulatory: %i\n", ret);
  6011. goto err_dfs_detector_exit;
  6012. }
  6013. ar->hw->wiphy->cipher_suites = cipher_suites;
  6014. ar->hw->wiphy->n_cipher_suites = ARRAY_SIZE(cipher_suites);
  6015. ret = ieee80211_register_hw(ar->hw);
  6016. if (ret) {
  6017. ath10k_err(ar, "failed to register ieee80211: %d\n", ret);
  6018. goto err_dfs_detector_exit;
  6019. }
  6020. if (!ath_is_world_regd(&ar->ath_common.regulatory)) {
  6021. ret = regulatory_hint(ar->hw->wiphy,
  6022. ar->ath_common.regulatory.alpha2);
  6023. if (ret)
  6024. goto err_unregister;
  6025. }
  6026. return 0;
  6027. err_unregister:
  6028. ieee80211_unregister_hw(ar->hw);
  6029. err_dfs_detector_exit:
  6030. if (config_enabled(CONFIG_ATH10K_DFS_CERTIFIED) && ar->dfs_detector)
  6031. ar->dfs_detector->exit(ar->dfs_detector);
  6032. err_free:
  6033. kfree(ar->mac.sbands[IEEE80211_BAND_2GHZ].channels);
  6034. kfree(ar->mac.sbands[IEEE80211_BAND_5GHZ].channels);
  6035. SET_IEEE80211_DEV(ar->hw, NULL);
  6036. return ret;
  6037. }
  6038. void ath10k_mac_unregister(struct ath10k *ar)
  6039. {
  6040. ieee80211_unregister_hw(ar->hw);
  6041. if (config_enabled(CONFIG_ATH10K_DFS_CERTIFIED) && ar->dfs_detector)
  6042. ar->dfs_detector->exit(ar->dfs_detector);
  6043. kfree(ar->mac.sbands[IEEE80211_BAND_2GHZ].channels);
  6044. kfree(ar->mac.sbands[IEEE80211_BAND_5GHZ].channels);
  6045. SET_IEEE80211_DEV(ar->hw, NULL);
  6046. }