mac.c 217 KB

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