mac.c 177 KB

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