mac.c 211 KB

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