mac.c 201 KB

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