mac.c 205 KB

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