i40e_main.c 399 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*******************************************************************************
  3. *
  4. * Intel Ethernet Controller XL710 Family Linux Driver
  5. * Copyright(c) 2013 - 2017 Intel Corporation.
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms and conditions of the GNU General Public License,
  9. * version 2, as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  14. * more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. * The full GNU General Public License is included in this distribution in
  20. * the file called "COPYING".
  21. *
  22. * Contact Information:
  23. * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
  24. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  25. *
  26. ******************************************************************************/
  27. #include <linux/etherdevice.h>
  28. #include <linux/of_net.h>
  29. #include <linux/pci.h>
  30. #include <linux/bpf.h>
  31. /* Local includes */
  32. #include "i40e.h"
  33. #include "i40e_diag.h"
  34. #include <net/udp_tunnel.h>
  35. /* All i40e tracepoints are defined by the include below, which
  36. * must be included exactly once across the whole kernel with
  37. * CREATE_TRACE_POINTS defined
  38. */
  39. #define CREATE_TRACE_POINTS
  40. #include "i40e_trace.h"
  41. const char i40e_driver_name[] = "i40e";
  42. static const char i40e_driver_string[] =
  43. "Intel(R) Ethernet Connection XL710 Network Driver";
  44. #define DRV_KERN "-k"
  45. #define DRV_VERSION_MAJOR 2
  46. #define DRV_VERSION_MINOR 3
  47. #define DRV_VERSION_BUILD 2
  48. #define DRV_VERSION __stringify(DRV_VERSION_MAJOR) "." \
  49. __stringify(DRV_VERSION_MINOR) "." \
  50. __stringify(DRV_VERSION_BUILD) DRV_KERN
  51. const char i40e_driver_version_str[] = DRV_VERSION;
  52. static const char i40e_copyright[] = "Copyright (c) 2013 - 2014 Intel Corporation.";
  53. /* a bit of forward declarations */
  54. static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi);
  55. static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired);
  56. static int i40e_add_vsi(struct i40e_vsi *vsi);
  57. static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi);
  58. static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit);
  59. static int i40e_setup_misc_vector(struct i40e_pf *pf);
  60. static void i40e_determine_queue_usage(struct i40e_pf *pf);
  61. static int i40e_setup_pf_filter_control(struct i40e_pf *pf);
  62. static void i40e_prep_for_reset(struct i40e_pf *pf, bool lock_acquired);
  63. static int i40e_reset(struct i40e_pf *pf);
  64. static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired);
  65. static void i40e_fdir_sb_setup(struct i40e_pf *pf);
  66. static int i40e_veb_get_bw_info(struct i40e_veb *veb);
  67. static int i40e_get_capabilities(struct i40e_pf *pf,
  68. enum i40e_admin_queue_opc list_type);
  69. /* i40e_pci_tbl - PCI Device ID Table
  70. *
  71. * Last entry must be all 0s
  72. *
  73. * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
  74. * Class, Class Mask, private data (not used) }
  75. */
  76. static const struct pci_device_id i40e_pci_tbl[] = {
  77. {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_XL710), 0},
  78. {PCI_VDEVICE(INTEL, I40E_DEV_ID_QEMU), 0},
  79. {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_B), 0},
  80. {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_C), 0},
  81. {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_A), 0},
  82. {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_B), 0},
  83. {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_C), 0},
  84. {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T), 0},
  85. {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T4), 0},
  86. {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_X722), 0},
  87. {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_X722), 0},
  88. {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_X722), 0},
  89. {PCI_VDEVICE(INTEL, I40E_DEV_ID_1G_BASE_T_X722), 0},
  90. {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_X722), 0},
  91. {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_I_X722), 0},
  92. {PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2), 0},
  93. {PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2_A), 0},
  94. {PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_B), 0},
  95. {PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_SFP28), 0},
  96. /* required last entry */
  97. {0, }
  98. };
  99. MODULE_DEVICE_TABLE(pci, i40e_pci_tbl);
  100. #define I40E_MAX_VF_COUNT 128
  101. static int debug = -1;
  102. module_param(debug, uint, 0);
  103. MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all), Debug mask (0x8XXXXXXX)");
  104. MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
  105. MODULE_DESCRIPTION("Intel(R) Ethernet Connection XL710 Network Driver");
  106. MODULE_LICENSE("GPL");
  107. MODULE_VERSION(DRV_VERSION);
  108. static struct workqueue_struct *i40e_wq;
  109. /**
  110. * i40e_allocate_dma_mem_d - OS specific memory alloc for shared code
  111. * @hw: pointer to the HW structure
  112. * @mem: ptr to mem struct to fill out
  113. * @size: size of memory requested
  114. * @alignment: what to align the allocation to
  115. **/
  116. int i40e_allocate_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem,
  117. u64 size, u32 alignment)
  118. {
  119. struct i40e_pf *pf = (struct i40e_pf *)hw->back;
  120. mem->size = ALIGN(size, alignment);
  121. mem->va = dma_zalloc_coherent(&pf->pdev->dev, mem->size,
  122. &mem->pa, GFP_KERNEL);
  123. if (!mem->va)
  124. return -ENOMEM;
  125. return 0;
  126. }
  127. /**
  128. * i40e_free_dma_mem_d - OS specific memory free for shared code
  129. * @hw: pointer to the HW structure
  130. * @mem: ptr to mem struct to free
  131. **/
  132. int i40e_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
  133. {
  134. struct i40e_pf *pf = (struct i40e_pf *)hw->back;
  135. dma_free_coherent(&pf->pdev->dev, mem->size, mem->va, mem->pa);
  136. mem->va = NULL;
  137. mem->pa = 0;
  138. mem->size = 0;
  139. return 0;
  140. }
  141. /**
  142. * i40e_allocate_virt_mem_d - OS specific memory alloc for shared code
  143. * @hw: pointer to the HW structure
  144. * @mem: ptr to mem struct to fill out
  145. * @size: size of memory requested
  146. **/
  147. int i40e_allocate_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem,
  148. u32 size)
  149. {
  150. mem->size = size;
  151. mem->va = kzalloc(size, GFP_KERNEL);
  152. if (!mem->va)
  153. return -ENOMEM;
  154. return 0;
  155. }
  156. /**
  157. * i40e_free_virt_mem_d - OS specific memory free for shared code
  158. * @hw: pointer to the HW structure
  159. * @mem: ptr to mem struct to free
  160. **/
  161. int i40e_free_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem)
  162. {
  163. /* it's ok to kfree a NULL pointer */
  164. kfree(mem->va);
  165. mem->va = NULL;
  166. mem->size = 0;
  167. return 0;
  168. }
  169. /**
  170. * i40e_get_lump - find a lump of free generic resource
  171. * @pf: board private structure
  172. * @pile: the pile of resource to search
  173. * @needed: the number of items needed
  174. * @id: an owner id to stick on the items assigned
  175. *
  176. * Returns the base item index of the lump, or negative for error
  177. *
  178. * The search_hint trick and lack of advanced fit-finding only work
  179. * because we're highly likely to have all the same size lump requests.
  180. * Linear search time and any fragmentation should be minimal.
  181. **/
  182. static int i40e_get_lump(struct i40e_pf *pf, struct i40e_lump_tracking *pile,
  183. u16 needed, u16 id)
  184. {
  185. int ret = -ENOMEM;
  186. int i, j;
  187. if (!pile || needed == 0 || id >= I40E_PILE_VALID_BIT) {
  188. dev_info(&pf->pdev->dev,
  189. "param err: pile=%s needed=%d id=0x%04x\n",
  190. pile ? "<valid>" : "<null>", needed, id);
  191. return -EINVAL;
  192. }
  193. /* start the linear search with an imperfect hint */
  194. i = pile->search_hint;
  195. while (i < pile->num_entries) {
  196. /* skip already allocated entries */
  197. if (pile->list[i] & I40E_PILE_VALID_BIT) {
  198. i++;
  199. continue;
  200. }
  201. /* do we have enough in this lump? */
  202. for (j = 0; (j < needed) && ((i+j) < pile->num_entries); j++) {
  203. if (pile->list[i+j] & I40E_PILE_VALID_BIT)
  204. break;
  205. }
  206. if (j == needed) {
  207. /* there was enough, so assign it to the requestor */
  208. for (j = 0; j < needed; j++)
  209. pile->list[i+j] = id | I40E_PILE_VALID_BIT;
  210. ret = i;
  211. pile->search_hint = i + j;
  212. break;
  213. }
  214. /* not enough, so skip over it and continue looking */
  215. i += j;
  216. }
  217. return ret;
  218. }
  219. /**
  220. * i40e_put_lump - return a lump of generic resource
  221. * @pile: the pile of resource to search
  222. * @index: the base item index
  223. * @id: the owner id of the items assigned
  224. *
  225. * Returns the count of items in the lump
  226. **/
  227. static int i40e_put_lump(struct i40e_lump_tracking *pile, u16 index, u16 id)
  228. {
  229. int valid_id = (id | I40E_PILE_VALID_BIT);
  230. int count = 0;
  231. int i;
  232. if (!pile || index >= pile->num_entries)
  233. return -EINVAL;
  234. for (i = index;
  235. i < pile->num_entries && pile->list[i] == valid_id;
  236. i++) {
  237. pile->list[i] = 0;
  238. count++;
  239. }
  240. if (count && index < pile->search_hint)
  241. pile->search_hint = index;
  242. return count;
  243. }
  244. /**
  245. * i40e_find_vsi_from_id - searches for the vsi with the given id
  246. * @pf - the pf structure to search for the vsi
  247. * @id - id of the vsi it is searching for
  248. **/
  249. struct i40e_vsi *i40e_find_vsi_from_id(struct i40e_pf *pf, u16 id)
  250. {
  251. int i;
  252. for (i = 0; i < pf->num_alloc_vsi; i++)
  253. if (pf->vsi[i] && (pf->vsi[i]->id == id))
  254. return pf->vsi[i];
  255. return NULL;
  256. }
  257. /**
  258. * i40e_service_event_schedule - Schedule the service task to wake up
  259. * @pf: board private structure
  260. *
  261. * If not already scheduled, this puts the task into the work queue
  262. **/
  263. void i40e_service_event_schedule(struct i40e_pf *pf)
  264. {
  265. if (!test_bit(__I40E_DOWN, pf->state) &&
  266. !test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
  267. queue_work(i40e_wq, &pf->service_task);
  268. }
  269. /**
  270. * i40e_tx_timeout - Respond to a Tx Hang
  271. * @netdev: network interface device structure
  272. *
  273. * If any port has noticed a Tx timeout, it is likely that the whole
  274. * device is munged, not just the one netdev port, so go for the full
  275. * reset.
  276. **/
  277. static void i40e_tx_timeout(struct net_device *netdev)
  278. {
  279. struct i40e_netdev_priv *np = netdev_priv(netdev);
  280. struct i40e_vsi *vsi = np->vsi;
  281. struct i40e_pf *pf = vsi->back;
  282. struct i40e_ring *tx_ring = NULL;
  283. unsigned int i, hung_queue = 0;
  284. u32 head, val;
  285. pf->tx_timeout_count++;
  286. /* find the stopped queue the same way the stack does */
  287. for (i = 0; i < netdev->num_tx_queues; i++) {
  288. struct netdev_queue *q;
  289. unsigned long trans_start;
  290. q = netdev_get_tx_queue(netdev, i);
  291. trans_start = q->trans_start;
  292. if (netif_xmit_stopped(q) &&
  293. time_after(jiffies,
  294. (trans_start + netdev->watchdog_timeo))) {
  295. hung_queue = i;
  296. break;
  297. }
  298. }
  299. if (i == netdev->num_tx_queues) {
  300. netdev_info(netdev, "tx_timeout: no netdev hung queue found\n");
  301. } else {
  302. /* now that we have an index, find the tx_ring struct */
  303. for (i = 0; i < vsi->num_queue_pairs; i++) {
  304. if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
  305. if (hung_queue ==
  306. vsi->tx_rings[i]->queue_index) {
  307. tx_ring = vsi->tx_rings[i];
  308. break;
  309. }
  310. }
  311. }
  312. }
  313. if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ*20)))
  314. pf->tx_timeout_recovery_level = 1; /* reset after some time */
  315. else if (time_before(jiffies,
  316. (pf->tx_timeout_last_recovery + netdev->watchdog_timeo)))
  317. return; /* don't do any new action before the next timeout */
  318. if (tx_ring) {
  319. head = i40e_get_head(tx_ring);
  320. /* Read interrupt register */
  321. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  322. val = rd32(&pf->hw,
  323. I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
  324. tx_ring->vsi->base_vector - 1));
  325. else
  326. val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
  327. netdev_info(netdev, "tx_timeout: VSI_seid: %d, Q %d, NTC: 0x%x, HWB: 0x%x, NTU: 0x%x, TAIL: 0x%x, INT: 0x%x\n",
  328. vsi->seid, hung_queue, tx_ring->next_to_clean,
  329. head, tx_ring->next_to_use,
  330. readl(tx_ring->tail), val);
  331. }
  332. pf->tx_timeout_last_recovery = jiffies;
  333. netdev_info(netdev, "tx_timeout recovery level %d, hung_queue %d\n",
  334. pf->tx_timeout_recovery_level, hung_queue);
  335. switch (pf->tx_timeout_recovery_level) {
  336. case 1:
  337. set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
  338. break;
  339. case 2:
  340. set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
  341. break;
  342. case 3:
  343. set_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
  344. break;
  345. default:
  346. netdev_err(netdev, "tx_timeout recovery unsuccessful\n");
  347. break;
  348. }
  349. i40e_service_event_schedule(pf);
  350. pf->tx_timeout_recovery_level++;
  351. }
  352. /**
  353. * i40e_get_vsi_stats_struct - Get System Network Statistics
  354. * @vsi: the VSI we care about
  355. *
  356. * Returns the address of the device statistics structure.
  357. * The statistics are actually updated from the service task.
  358. **/
  359. struct rtnl_link_stats64 *i40e_get_vsi_stats_struct(struct i40e_vsi *vsi)
  360. {
  361. return &vsi->net_stats;
  362. }
  363. /**
  364. * i40e_get_netdev_stats_struct_tx - populate stats from a Tx ring
  365. * @ring: Tx ring to get statistics from
  366. * @stats: statistics entry to be updated
  367. **/
  368. static void i40e_get_netdev_stats_struct_tx(struct i40e_ring *ring,
  369. struct rtnl_link_stats64 *stats)
  370. {
  371. u64 bytes, packets;
  372. unsigned int start;
  373. do {
  374. start = u64_stats_fetch_begin_irq(&ring->syncp);
  375. packets = ring->stats.packets;
  376. bytes = ring->stats.bytes;
  377. } while (u64_stats_fetch_retry_irq(&ring->syncp, start));
  378. stats->tx_packets += packets;
  379. stats->tx_bytes += bytes;
  380. }
  381. /**
  382. * i40e_get_netdev_stats_struct - Get statistics for netdev interface
  383. * @netdev: network interface device structure
  384. *
  385. * Returns the address of the device statistics structure.
  386. * The statistics are actually updated from the service task.
  387. **/
  388. static void i40e_get_netdev_stats_struct(struct net_device *netdev,
  389. struct rtnl_link_stats64 *stats)
  390. {
  391. struct i40e_netdev_priv *np = netdev_priv(netdev);
  392. struct i40e_ring *tx_ring, *rx_ring;
  393. struct i40e_vsi *vsi = np->vsi;
  394. struct rtnl_link_stats64 *vsi_stats = i40e_get_vsi_stats_struct(vsi);
  395. int i;
  396. if (test_bit(__I40E_VSI_DOWN, vsi->state))
  397. return;
  398. if (!vsi->tx_rings)
  399. return;
  400. rcu_read_lock();
  401. for (i = 0; i < vsi->num_queue_pairs; i++) {
  402. u64 bytes, packets;
  403. unsigned int start;
  404. tx_ring = READ_ONCE(vsi->tx_rings[i]);
  405. if (!tx_ring)
  406. continue;
  407. i40e_get_netdev_stats_struct_tx(tx_ring, stats);
  408. rx_ring = &tx_ring[1];
  409. do {
  410. start = u64_stats_fetch_begin_irq(&rx_ring->syncp);
  411. packets = rx_ring->stats.packets;
  412. bytes = rx_ring->stats.bytes;
  413. } while (u64_stats_fetch_retry_irq(&rx_ring->syncp, start));
  414. stats->rx_packets += packets;
  415. stats->rx_bytes += bytes;
  416. if (i40e_enabled_xdp_vsi(vsi))
  417. i40e_get_netdev_stats_struct_tx(&rx_ring[1], stats);
  418. }
  419. rcu_read_unlock();
  420. /* following stats updated by i40e_watchdog_subtask() */
  421. stats->multicast = vsi_stats->multicast;
  422. stats->tx_errors = vsi_stats->tx_errors;
  423. stats->tx_dropped = vsi_stats->tx_dropped;
  424. stats->rx_errors = vsi_stats->rx_errors;
  425. stats->rx_dropped = vsi_stats->rx_dropped;
  426. stats->rx_crc_errors = vsi_stats->rx_crc_errors;
  427. stats->rx_length_errors = vsi_stats->rx_length_errors;
  428. }
  429. /**
  430. * i40e_vsi_reset_stats - Resets all stats of the given vsi
  431. * @vsi: the VSI to have its stats reset
  432. **/
  433. void i40e_vsi_reset_stats(struct i40e_vsi *vsi)
  434. {
  435. struct rtnl_link_stats64 *ns;
  436. int i;
  437. if (!vsi)
  438. return;
  439. ns = i40e_get_vsi_stats_struct(vsi);
  440. memset(ns, 0, sizeof(*ns));
  441. memset(&vsi->net_stats_offsets, 0, sizeof(vsi->net_stats_offsets));
  442. memset(&vsi->eth_stats, 0, sizeof(vsi->eth_stats));
  443. memset(&vsi->eth_stats_offsets, 0, sizeof(vsi->eth_stats_offsets));
  444. if (vsi->rx_rings && vsi->rx_rings[0]) {
  445. for (i = 0; i < vsi->num_queue_pairs; i++) {
  446. memset(&vsi->rx_rings[i]->stats, 0,
  447. sizeof(vsi->rx_rings[i]->stats));
  448. memset(&vsi->rx_rings[i]->rx_stats, 0,
  449. sizeof(vsi->rx_rings[i]->rx_stats));
  450. memset(&vsi->tx_rings[i]->stats, 0,
  451. sizeof(vsi->tx_rings[i]->stats));
  452. memset(&vsi->tx_rings[i]->tx_stats, 0,
  453. sizeof(vsi->tx_rings[i]->tx_stats));
  454. }
  455. }
  456. vsi->stat_offsets_loaded = false;
  457. }
  458. /**
  459. * i40e_pf_reset_stats - Reset all of the stats for the given PF
  460. * @pf: the PF to be reset
  461. **/
  462. void i40e_pf_reset_stats(struct i40e_pf *pf)
  463. {
  464. int i;
  465. memset(&pf->stats, 0, sizeof(pf->stats));
  466. memset(&pf->stats_offsets, 0, sizeof(pf->stats_offsets));
  467. pf->stat_offsets_loaded = false;
  468. for (i = 0; i < I40E_MAX_VEB; i++) {
  469. if (pf->veb[i]) {
  470. memset(&pf->veb[i]->stats, 0,
  471. sizeof(pf->veb[i]->stats));
  472. memset(&pf->veb[i]->stats_offsets, 0,
  473. sizeof(pf->veb[i]->stats_offsets));
  474. pf->veb[i]->stat_offsets_loaded = false;
  475. }
  476. }
  477. pf->hw_csum_rx_error = 0;
  478. }
  479. /**
  480. * i40e_stat_update48 - read and update a 48 bit stat from the chip
  481. * @hw: ptr to the hardware info
  482. * @hireg: the high 32 bit reg to read
  483. * @loreg: the low 32 bit reg to read
  484. * @offset_loaded: has the initial offset been loaded yet
  485. * @offset: ptr to current offset value
  486. * @stat: ptr to the stat
  487. *
  488. * Since the device stats are not reset at PFReset, they likely will not
  489. * be zeroed when the driver starts. We'll save the first values read
  490. * and use them as offsets to be subtracted from the raw values in order
  491. * to report stats that count from zero. In the process, we also manage
  492. * the potential roll-over.
  493. **/
  494. static void i40e_stat_update48(struct i40e_hw *hw, u32 hireg, u32 loreg,
  495. bool offset_loaded, u64 *offset, u64 *stat)
  496. {
  497. u64 new_data;
  498. if (hw->device_id == I40E_DEV_ID_QEMU) {
  499. new_data = rd32(hw, loreg);
  500. new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
  501. } else {
  502. new_data = rd64(hw, loreg);
  503. }
  504. if (!offset_loaded)
  505. *offset = new_data;
  506. if (likely(new_data >= *offset))
  507. *stat = new_data - *offset;
  508. else
  509. *stat = (new_data + BIT_ULL(48)) - *offset;
  510. *stat &= 0xFFFFFFFFFFFFULL;
  511. }
  512. /**
  513. * i40e_stat_update32 - read and update a 32 bit stat from the chip
  514. * @hw: ptr to the hardware info
  515. * @reg: the hw reg to read
  516. * @offset_loaded: has the initial offset been loaded yet
  517. * @offset: ptr to current offset value
  518. * @stat: ptr to the stat
  519. **/
  520. static void i40e_stat_update32(struct i40e_hw *hw, u32 reg,
  521. bool offset_loaded, u64 *offset, u64 *stat)
  522. {
  523. u32 new_data;
  524. new_data = rd32(hw, reg);
  525. if (!offset_loaded)
  526. *offset = new_data;
  527. if (likely(new_data >= *offset))
  528. *stat = (u32)(new_data - *offset);
  529. else
  530. *stat = (u32)((new_data + BIT_ULL(32)) - *offset);
  531. }
  532. /**
  533. * i40e_stat_update_and_clear32 - read and clear hw reg, update a 32 bit stat
  534. * @hw: ptr to the hardware info
  535. * @reg: the hw reg to read and clear
  536. * @stat: ptr to the stat
  537. **/
  538. static void i40e_stat_update_and_clear32(struct i40e_hw *hw, u32 reg, u64 *stat)
  539. {
  540. u32 new_data = rd32(hw, reg);
  541. wr32(hw, reg, 1); /* must write a nonzero value to clear register */
  542. *stat += new_data;
  543. }
  544. /**
  545. * i40e_update_eth_stats - Update VSI-specific ethernet statistics counters.
  546. * @vsi: the VSI to be updated
  547. **/
  548. void i40e_update_eth_stats(struct i40e_vsi *vsi)
  549. {
  550. int stat_idx = le16_to_cpu(vsi->info.stat_counter_idx);
  551. struct i40e_pf *pf = vsi->back;
  552. struct i40e_hw *hw = &pf->hw;
  553. struct i40e_eth_stats *oes;
  554. struct i40e_eth_stats *es; /* device's eth stats */
  555. es = &vsi->eth_stats;
  556. oes = &vsi->eth_stats_offsets;
  557. /* Gather up the stats that the hw collects */
  558. i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
  559. vsi->stat_offsets_loaded,
  560. &oes->tx_errors, &es->tx_errors);
  561. i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx),
  562. vsi->stat_offsets_loaded,
  563. &oes->rx_discards, &es->rx_discards);
  564. i40e_stat_update32(hw, I40E_GLV_RUPP(stat_idx),
  565. vsi->stat_offsets_loaded,
  566. &oes->rx_unknown_protocol, &es->rx_unknown_protocol);
  567. i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
  568. vsi->stat_offsets_loaded,
  569. &oes->tx_errors, &es->tx_errors);
  570. i40e_stat_update48(hw, I40E_GLV_GORCH(stat_idx),
  571. I40E_GLV_GORCL(stat_idx),
  572. vsi->stat_offsets_loaded,
  573. &oes->rx_bytes, &es->rx_bytes);
  574. i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
  575. I40E_GLV_UPRCL(stat_idx),
  576. vsi->stat_offsets_loaded,
  577. &oes->rx_unicast, &es->rx_unicast);
  578. i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
  579. I40E_GLV_MPRCL(stat_idx),
  580. vsi->stat_offsets_loaded,
  581. &oes->rx_multicast, &es->rx_multicast);
  582. i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
  583. I40E_GLV_BPRCL(stat_idx),
  584. vsi->stat_offsets_loaded,
  585. &oes->rx_broadcast, &es->rx_broadcast);
  586. i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
  587. I40E_GLV_GOTCL(stat_idx),
  588. vsi->stat_offsets_loaded,
  589. &oes->tx_bytes, &es->tx_bytes);
  590. i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
  591. I40E_GLV_UPTCL(stat_idx),
  592. vsi->stat_offsets_loaded,
  593. &oes->tx_unicast, &es->tx_unicast);
  594. i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
  595. I40E_GLV_MPTCL(stat_idx),
  596. vsi->stat_offsets_loaded,
  597. &oes->tx_multicast, &es->tx_multicast);
  598. i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
  599. I40E_GLV_BPTCL(stat_idx),
  600. vsi->stat_offsets_loaded,
  601. &oes->tx_broadcast, &es->tx_broadcast);
  602. vsi->stat_offsets_loaded = true;
  603. }
  604. /**
  605. * i40e_update_veb_stats - Update Switch component statistics
  606. * @veb: the VEB being updated
  607. **/
  608. static void i40e_update_veb_stats(struct i40e_veb *veb)
  609. {
  610. struct i40e_pf *pf = veb->pf;
  611. struct i40e_hw *hw = &pf->hw;
  612. struct i40e_eth_stats *oes;
  613. struct i40e_eth_stats *es; /* device's eth stats */
  614. struct i40e_veb_tc_stats *veb_oes;
  615. struct i40e_veb_tc_stats *veb_es;
  616. int i, idx = 0;
  617. idx = veb->stats_idx;
  618. es = &veb->stats;
  619. oes = &veb->stats_offsets;
  620. veb_es = &veb->tc_stats;
  621. veb_oes = &veb->tc_stats_offsets;
  622. /* Gather up the stats that the hw collects */
  623. i40e_stat_update32(hw, I40E_GLSW_TDPC(idx),
  624. veb->stat_offsets_loaded,
  625. &oes->tx_discards, &es->tx_discards);
  626. if (hw->revision_id > 0)
  627. i40e_stat_update32(hw, I40E_GLSW_RUPP(idx),
  628. veb->stat_offsets_loaded,
  629. &oes->rx_unknown_protocol,
  630. &es->rx_unknown_protocol);
  631. i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx),
  632. veb->stat_offsets_loaded,
  633. &oes->rx_bytes, &es->rx_bytes);
  634. i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx),
  635. veb->stat_offsets_loaded,
  636. &oes->rx_unicast, &es->rx_unicast);
  637. i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx),
  638. veb->stat_offsets_loaded,
  639. &oes->rx_multicast, &es->rx_multicast);
  640. i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx),
  641. veb->stat_offsets_loaded,
  642. &oes->rx_broadcast, &es->rx_broadcast);
  643. i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx),
  644. veb->stat_offsets_loaded,
  645. &oes->tx_bytes, &es->tx_bytes);
  646. i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx),
  647. veb->stat_offsets_loaded,
  648. &oes->tx_unicast, &es->tx_unicast);
  649. i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx),
  650. veb->stat_offsets_loaded,
  651. &oes->tx_multicast, &es->tx_multicast);
  652. i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx),
  653. veb->stat_offsets_loaded,
  654. &oes->tx_broadcast, &es->tx_broadcast);
  655. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  656. i40e_stat_update48(hw, I40E_GLVEBTC_RPCH(i, idx),
  657. I40E_GLVEBTC_RPCL(i, idx),
  658. veb->stat_offsets_loaded,
  659. &veb_oes->tc_rx_packets[i],
  660. &veb_es->tc_rx_packets[i]);
  661. i40e_stat_update48(hw, I40E_GLVEBTC_RBCH(i, idx),
  662. I40E_GLVEBTC_RBCL(i, idx),
  663. veb->stat_offsets_loaded,
  664. &veb_oes->tc_rx_bytes[i],
  665. &veb_es->tc_rx_bytes[i]);
  666. i40e_stat_update48(hw, I40E_GLVEBTC_TPCH(i, idx),
  667. I40E_GLVEBTC_TPCL(i, idx),
  668. veb->stat_offsets_loaded,
  669. &veb_oes->tc_tx_packets[i],
  670. &veb_es->tc_tx_packets[i]);
  671. i40e_stat_update48(hw, I40E_GLVEBTC_TBCH(i, idx),
  672. I40E_GLVEBTC_TBCL(i, idx),
  673. veb->stat_offsets_loaded,
  674. &veb_oes->tc_tx_bytes[i],
  675. &veb_es->tc_tx_bytes[i]);
  676. }
  677. veb->stat_offsets_loaded = true;
  678. }
  679. /**
  680. * i40e_update_vsi_stats - Update the vsi statistics counters.
  681. * @vsi: the VSI to be updated
  682. *
  683. * There are a few instances where we store the same stat in a
  684. * couple of different structs. This is partly because we have
  685. * the netdev stats that need to be filled out, which is slightly
  686. * different from the "eth_stats" defined by the chip and used in
  687. * VF communications. We sort it out here.
  688. **/
  689. static void i40e_update_vsi_stats(struct i40e_vsi *vsi)
  690. {
  691. struct i40e_pf *pf = vsi->back;
  692. struct rtnl_link_stats64 *ons;
  693. struct rtnl_link_stats64 *ns; /* netdev stats */
  694. struct i40e_eth_stats *oes;
  695. struct i40e_eth_stats *es; /* device's eth stats */
  696. u32 tx_restart, tx_busy;
  697. struct i40e_ring *p;
  698. u32 rx_page, rx_buf;
  699. u64 bytes, packets;
  700. unsigned int start;
  701. u64 tx_linearize;
  702. u64 tx_force_wb;
  703. u64 rx_p, rx_b;
  704. u64 tx_p, tx_b;
  705. u16 q;
  706. if (test_bit(__I40E_VSI_DOWN, vsi->state) ||
  707. test_bit(__I40E_CONFIG_BUSY, pf->state))
  708. return;
  709. ns = i40e_get_vsi_stats_struct(vsi);
  710. ons = &vsi->net_stats_offsets;
  711. es = &vsi->eth_stats;
  712. oes = &vsi->eth_stats_offsets;
  713. /* Gather up the netdev and vsi stats that the driver collects
  714. * on the fly during packet processing
  715. */
  716. rx_b = rx_p = 0;
  717. tx_b = tx_p = 0;
  718. tx_restart = tx_busy = tx_linearize = tx_force_wb = 0;
  719. rx_page = 0;
  720. rx_buf = 0;
  721. rcu_read_lock();
  722. for (q = 0; q < vsi->num_queue_pairs; q++) {
  723. /* locate Tx ring */
  724. p = READ_ONCE(vsi->tx_rings[q]);
  725. do {
  726. start = u64_stats_fetch_begin_irq(&p->syncp);
  727. packets = p->stats.packets;
  728. bytes = p->stats.bytes;
  729. } while (u64_stats_fetch_retry_irq(&p->syncp, start));
  730. tx_b += bytes;
  731. tx_p += packets;
  732. tx_restart += p->tx_stats.restart_queue;
  733. tx_busy += p->tx_stats.tx_busy;
  734. tx_linearize += p->tx_stats.tx_linearize;
  735. tx_force_wb += p->tx_stats.tx_force_wb;
  736. /* Rx queue is part of the same block as Tx queue */
  737. p = &p[1];
  738. do {
  739. start = u64_stats_fetch_begin_irq(&p->syncp);
  740. packets = p->stats.packets;
  741. bytes = p->stats.bytes;
  742. } while (u64_stats_fetch_retry_irq(&p->syncp, start));
  743. rx_b += bytes;
  744. rx_p += packets;
  745. rx_buf += p->rx_stats.alloc_buff_failed;
  746. rx_page += p->rx_stats.alloc_page_failed;
  747. }
  748. rcu_read_unlock();
  749. vsi->tx_restart = tx_restart;
  750. vsi->tx_busy = tx_busy;
  751. vsi->tx_linearize = tx_linearize;
  752. vsi->tx_force_wb = tx_force_wb;
  753. vsi->rx_page_failed = rx_page;
  754. vsi->rx_buf_failed = rx_buf;
  755. ns->rx_packets = rx_p;
  756. ns->rx_bytes = rx_b;
  757. ns->tx_packets = tx_p;
  758. ns->tx_bytes = tx_b;
  759. /* update netdev stats from eth stats */
  760. i40e_update_eth_stats(vsi);
  761. ons->tx_errors = oes->tx_errors;
  762. ns->tx_errors = es->tx_errors;
  763. ons->multicast = oes->rx_multicast;
  764. ns->multicast = es->rx_multicast;
  765. ons->rx_dropped = oes->rx_discards;
  766. ns->rx_dropped = es->rx_discards;
  767. ons->tx_dropped = oes->tx_discards;
  768. ns->tx_dropped = es->tx_discards;
  769. /* pull in a couple PF stats if this is the main vsi */
  770. if (vsi == pf->vsi[pf->lan_vsi]) {
  771. ns->rx_crc_errors = pf->stats.crc_errors;
  772. ns->rx_errors = pf->stats.crc_errors + pf->stats.illegal_bytes;
  773. ns->rx_length_errors = pf->stats.rx_length_errors;
  774. }
  775. }
  776. /**
  777. * i40e_update_pf_stats - Update the PF statistics counters.
  778. * @pf: the PF to be updated
  779. **/
  780. static void i40e_update_pf_stats(struct i40e_pf *pf)
  781. {
  782. struct i40e_hw_port_stats *osd = &pf->stats_offsets;
  783. struct i40e_hw_port_stats *nsd = &pf->stats;
  784. struct i40e_hw *hw = &pf->hw;
  785. u32 val;
  786. int i;
  787. i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
  788. I40E_GLPRT_GORCL(hw->port),
  789. pf->stat_offsets_loaded,
  790. &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
  791. i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
  792. I40E_GLPRT_GOTCL(hw->port),
  793. pf->stat_offsets_loaded,
  794. &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
  795. i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
  796. pf->stat_offsets_loaded,
  797. &osd->eth.rx_discards,
  798. &nsd->eth.rx_discards);
  799. i40e_stat_update48(hw, I40E_GLPRT_UPRCH(hw->port),
  800. I40E_GLPRT_UPRCL(hw->port),
  801. pf->stat_offsets_loaded,
  802. &osd->eth.rx_unicast,
  803. &nsd->eth.rx_unicast);
  804. i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
  805. I40E_GLPRT_MPRCL(hw->port),
  806. pf->stat_offsets_loaded,
  807. &osd->eth.rx_multicast,
  808. &nsd->eth.rx_multicast);
  809. i40e_stat_update48(hw, I40E_GLPRT_BPRCH(hw->port),
  810. I40E_GLPRT_BPRCL(hw->port),
  811. pf->stat_offsets_loaded,
  812. &osd->eth.rx_broadcast,
  813. &nsd->eth.rx_broadcast);
  814. i40e_stat_update48(hw, I40E_GLPRT_UPTCH(hw->port),
  815. I40E_GLPRT_UPTCL(hw->port),
  816. pf->stat_offsets_loaded,
  817. &osd->eth.tx_unicast,
  818. &nsd->eth.tx_unicast);
  819. i40e_stat_update48(hw, I40E_GLPRT_MPTCH(hw->port),
  820. I40E_GLPRT_MPTCL(hw->port),
  821. pf->stat_offsets_loaded,
  822. &osd->eth.tx_multicast,
  823. &nsd->eth.tx_multicast);
  824. i40e_stat_update48(hw, I40E_GLPRT_BPTCH(hw->port),
  825. I40E_GLPRT_BPTCL(hw->port),
  826. pf->stat_offsets_loaded,
  827. &osd->eth.tx_broadcast,
  828. &nsd->eth.tx_broadcast);
  829. i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
  830. pf->stat_offsets_loaded,
  831. &osd->tx_dropped_link_down,
  832. &nsd->tx_dropped_link_down);
  833. i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
  834. pf->stat_offsets_loaded,
  835. &osd->crc_errors, &nsd->crc_errors);
  836. i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
  837. pf->stat_offsets_loaded,
  838. &osd->illegal_bytes, &nsd->illegal_bytes);
  839. i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
  840. pf->stat_offsets_loaded,
  841. &osd->mac_local_faults,
  842. &nsd->mac_local_faults);
  843. i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
  844. pf->stat_offsets_loaded,
  845. &osd->mac_remote_faults,
  846. &nsd->mac_remote_faults);
  847. i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
  848. pf->stat_offsets_loaded,
  849. &osd->rx_length_errors,
  850. &nsd->rx_length_errors);
  851. i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
  852. pf->stat_offsets_loaded,
  853. &osd->link_xon_rx, &nsd->link_xon_rx);
  854. i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
  855. pf->stat_offsets_loaded,
  856. &osd->link_xon_tx, &nsd->link_xon_tx);
  857. i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
  858. pf->stat_offsets_loaded,
  859. &osd->link_xoff_rx, &nsd->link_xoff_rx);
  860. i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
  861. pf->stat_offsets_loaded,
  862. &osd->link_xoff_tx, &nsd->link_xoff_tx);
  863. for (i = 0; i < 8; i++) {
  864. i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
  865. pf->stat_offsets_loaded,
  866. &osd->priority_xoff_rx[i],
  867. &nsd->priority_xoff_rx[i]);
  868. i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
  869. pf->stat_offsets_loaded,
  870. &osd->priority_xon_rx[i],
  871. &nsd->priority_xon_rx[i]);
  872. i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
  873. pf->stat_offsets_loaded,
  874. &osd->priority_xon_tx[i],
  875. &nsd->priority_xon_tx[i]);
  876. i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
  877. pf->stat_offsets_loaded,
  878. &osd->priority_xoff_tx[i],
  879. &nsd->priority_xoff_tx[i]);
  880. i40e_stat_update32(hw,
  881. I40E_GLPRT_RXON2OFFCNT(hw->port, i),
  882. pf->stat_offsets_loaded,
  883. &osd->priority_xon_2_xoff[i],
  884. &nsd->priority_xon_2_xoff[i]);
  885. }
  886. i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
  887. I40E_GLPRT_PRC64L(hw->port),
  888. pf->stat_offsets_loaded,
  889. &osd->rx_size_64, &nsd->rx_size_64);
  890. i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
  891. I40E_GLPRT_PRC127L(hw->port),
  892. pf->stat_offsets_loaded,
  893. &osd->rx_size_127, &nsd->rx_size_127);
  894. i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
  895. I40E_GLPRT_PRC255L(hw->port),
  896. pf->stat_offsets_loaded,
  897. &osd->rx_size_255, &nsd->rx_size_255);
  898. i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
  899. I40E_GLPRT_PRC511L(hw->port),
  900. pf->stat_offsets_loaded,
  901. &osd->rx_size_511, &nsd->rx_size_511);
  902. i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
  903. I40E_GLPRT_PRC1023L(hw->port),
  904. pf->stat_offsets_loaded,
  905. &osd->rx_size_1023, &nsd->rx_size_1023);
  906. i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
  907. I40E_GLPRT_PRC1522L(hw->port),
  908. pf->stat_offsets_loaded,
  909. &osd->rx_size_1522, &nsd->rx_size_1522);
  910. i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
  911. I40E_GLPRT_PRC9522L(hw->port),
  912. pf->stat_offsets_loaded,
  913. &osd->rx_size_big, &nsd->rx_size_big);
  914. i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
  915. I40E_GLPRT_PTC64L(hw->port),
  916. pf->stat_offsets_loaded,
  917. &osd->tx_size_64, &nsd->tx_size_64);
  918. i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
  919. I40E_GLPRT_PTC127L(hw->port),
  920. pf->stat_offsets_loaded,
  921. &osd->tx_size_127, &nsd->tx_size_127);
  922. i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
  923. I40E_GLPRT_PTC255L(hw->port),
  924. pf->stat_offsets_loaded,
  925. &osd->tx_size_255, &nsd->tx_size_255);
  926. i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
  927. I40E_GLPRT_PTC511L(hw->port),
  928. pf->stat_offsets_loaded,
  929. &osd->tx_size_511, &nsd->tx_size_511);
  930. i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
  931. I40E_GLPRT_PTC1023L(hw->port),
  932. pf->stat_offsets_loaded,
  933. &osd->tx_size_1023, &nsd->tx_size_1023);
  934. i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
  935. I40E_GLPRT_PTC1522L(hw->port),
  936. pf->stat_offsets_loaded,
  937. &osd->tx_size_1522, &nsd->tx_size_1522);
  938. i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
  939. I40E_GLPRT_PTC9522L(hw->port),
  940. pf->stat_offsets_loaded,
  941. &osd->tx_size_big, &nsd->tx_size_big);
  942. i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
  943. pf->stat_offsets_loaded,
  944. &osd->rx_undersize, &nsd->rx_undersize);
  945. i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
  946. pf->stat_offsets_loaded,
  947. &osd->rx_fragments, &nsd->rx_fragments);
  948. i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
  949. pf->stat_offsets_loaded,
  950. &osd->rx_oversize, &nsd->rx_oversize);
  951. i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
  952. pf->stat_offsets_loaded,
  953. &osd->rx_jabber, &nsd->rx_jabber);
  954. /* FDIR stats */
  955. i40e_stat_update_and_clear32(hw,
  956. I40E_GLQF_PCNT(I40E_FD_ATR_STAT_IDX(hw->pf_id)),
  957. &nsd->fd_atr_match);
  958. i40e_stat_update_and_clear32(hw,
  959. I40E_GLQF_PCNT(I40E_FD_SB_STAT_IDX(hw->pf_id)),
  960. &nsd->fd_sb_match);
  961. i40e_stat_update_and_clear32(hw,
  962. I40E_GLQF_PCNT(I40E_FD_ATR_TUNNEL_STAT_IDX(hw->pf_id)),
  963. &nsd->fd_atr_tunnel_match);
  964. val = rd32(hw, I40E_PRTPM_EEE_STAT);
  965. nsd->tx_lpi_status =
  966. (val & I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_MASK) >>
  967. I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_SHIFT;
  968. nsd->rx_lpi_status =
  969. (val & I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_MASK) >>
  970. I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_SHIFT;
  971. i40e_stat_update32(hw, I40E_PRTPM_TLPIC,
  972. pf->stat_offsets_loaded,
  973. &osd->tx_lpi_count, &nsd->tx_lpi_count);
  974. i40e_stat_update32(hw, I40E_PRTPM_RLPIC,
  975. pf->stat_offsets_loaded,
  976. &osd->rx_lpi_count, &nsd->rx_lpi_count);
  977. if (pf->flags & I40E_FLAG_FD_SB_ENABLED &&
  978. !test_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
  979. nsd->fd_sb_status = true;
  980. else
  981. nsd->fd_sb_status = false;
  982. if (pf->flags & I40E_FLAG_FD_ATR_ENABLED &&
  983. !test_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
  984. nsd->fd_atr_status = true;
  985. else
  986. nsd->fd_atr_status = false;
  987. pf->stat_offsets_loaded = true;
  988. }
  989. /**
  990. * i40e_update_stats - Update the various statistics counters.
  991. * @vsi: the VSI to be updated
  992. *
  993. * Update the various stats for this VSI and its related entities.
  994. **/
  995. void i40e_update_stats(struct i40e_vsi *vsi)
  996. {
  997. struct i40e_pf *pf = vsi->back;
  998. if (vsi == pf->vsi[pf->lan_vsi])
  999. i40e_update_pf_stats(pf);
  1000. i40e_update_vsi_stats(vsi);
  1001. }
  1002. /**
  1003. * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
  1004. * @vsi: the VSI to be searched
  1005. * @macaddr: the MAC address
  1006. * @vlan: the vlan
  1007. *
  1008. * Returns ptr to the filter object or NULL
  1009. **/
  1010. static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
  1011. const u8 *macaddr, s16 vlan)
  1012. {
  1013. struct i40e_mac_filter *f;
  1014. u64 key;
  1015. if (!vsi || !macaddr)
  1016. return NULL;
  1017. key = i40e_addr_to_hkey(macaddr);
  1018. hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
  1019. if ((ether_addr_equal(macaddr, f->macaddr)) &&
  1020. (vlan == f->vlan))
  1021. return f;
  1022. }
  1023. return NULL;
  1024. }
  1025. /**
  1026. * i40e_find_mac - Find a mac addr in the macvlan filters list
  1027. * @vsi: the VSI to be searched
  1028. * @macaddr: the MAC address we are searching for
  1029. *
  1030. * Returns the first filter with the provided MAC address or NULL if
  1031. * MAC address was not found
  1032. **/
  1033. struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, const u8 *macaddr)
  1034. {
  1035. struct i40e_mac_filter *f;
  1036. u64 key;
  1037. if (!vsi || !macaddr)
  1038. return NULL;
  1039. key = i40e_addr_to_hkey(macaddr);
  1040. hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
  1041. if ((ether_addr_equal(macaddr, f->macaddr)))
  1042. return f;
  1043. }
  1044. return NULL;
  1045. }
  1046. /**
  1047. * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
  1048. * @vsi: the VSI to be searched
  1049. *
  1050. * Returns true if VSI is in vlan mode or false otherwise
  1051. **/
  1052. bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
  1053. {
  1054. /* If we have a PVID, always operate in VLAN mode */
  1055. if (vsi->info.pvid)
  1056. return true;
  1057. /* We need to operate in VLAN mode whenever we have any filters with
  1058. * a VLAN other than I40E_VLAN_ALL. We could check the table each
  1059. * time, incurring search cost repeatedly. However, we can notice two
  1060. * things:
  1061. *
  1062. * 1) the only place where we can gain a VLAN filter is in
  1063. * i40e_add_filter.
  1064. *
  1065. * 2) the only place where filters are actually removed is in
  1066. * i40e_sync_filters_subtask.
  1067. *
  1068. * Thus, we can simply use a boolean value, has_vlan_filters which we
  1069. * will set to true when we add a VLAN filter in i40e_add_filter. Then
  1070. * we have to perform the full search after deleting filters in
  1071. * i40e_sync_filters_subtask, but we already have to search
  1072. * filters here and can perform the check at the same time. This
  1073. * results in avoiding embedding a loop for VLAN mode inside another
  1074. * loop over all the filters, and should maintain correctness as noted
  1075. * above.
  1076. */
  1077. return vsi->has_vlan_filter;
  1078. }
  1079. /**
  1080. * i40e_correct_mac_vlan_filters - Correct non-VLAN filters if necessary
  1081. * @vsi: the VSI to configure
  1082. * @tmp_add_list: list of filters ready to be added
  1083. * @tmp_del_list: list of filters ready to be deleted
  1084. * @vlan_filters: the number of active VLAN filters
  1085. *
  1086. * Update VLAN=0 and VLAN=-1 (I40E_VLAN_ANY) filters properly so that they
  1087. * behave as expected. If we have any active VLAN filters remaining or about
  1088. * to be added then we need to update non-VLAN filters to be marked as VLAN=0
  1089. * so that they only match against untagged traffic. If we no longer have any
  1090. * active VLAN filters, we need to make all non-VLAN filters marked as VLAN=-1
  1091. * so that they match against both tagged and untagged traffic. In this way,
  1092. * we ensure that we correctly receive the desired traffic. This ensures that
  1093. * when we have an active VLAN we will receive only untagged traffic and
  1094. * traffic matching active VLANs. If we have no active VLANs then we will
  1095. * operate in non-VLAN mode and receive all traffic, tagged or untagged.
  1096. *
  1097. * Finally, in a similar fashion, this function also corrects filters when
  1098. * there is an active PVID assigned to this VSI.
  1099. *
  1100. * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
  1101. *
  1102. * This function is only expected to be called from within
  1103. * i40e_sync_vsi_filters.
  1104. *
  1105. * NOTE: This function expects to be called while under the
  1106. * mac_filter_hash_lock
  1107. */
  1108. static int i40e_correct_mac_vlan_filters(struct i40e_vsi *vsi,
  1109. struct hlist_head *tmp_add_list,
  1110. struct hlist_head *tmp_del_list,
  1111. int vlan_filters)
  1112. {
  1113. s16 pvid = le16_to_cpu(vsi->info.pvid);
  1114. struct i40e_mac_filter *f, *add_head;
  1115. struct i40e_new_mac_filter *new;
  1116. struct hlist_node *h;
  1117. int bkt, new_vlan;
  1118. /* To determine if a particular filter needs to be replaced we
  1119. * have the three following conditions:
  1120. *
  1121. * a) if we have a PVID assigned, then all filters which are
  1122. * not marked as VLAN=PVID must be replaced with filters that
  1123. * are.
  1124. * b) otherwise, if we have any active VLANS, all filters
  1125. * which are marked as VLAN=-1 must be replaced with
  1126. * filters marked as VLAN=0
  1127. * c) finally, if we do not have any active VLANS, all filters
  1128. * which are marked as VLAN=0 must be replaced with filters
  1129. * marked as VLAN=-1
  1130. */
  1131. /* Update the filters about to be added in place */
  1132. hlist_for_each_entry(new, tmp_add_list, hlist) {
  1133. if (pvid && new->f->vlan != pvid)
  1134. new->f->vlan = pvid;
  1135. else if (vlan_filters && new->f->vlan == I40E_VLAN_ANY)
  1136. new->f->vlan = 0;
  1137. else if (!vlan_filters && new->f->vlan == 0)
  1138. new->f->vlan = I40E_VLAN_ANY;
  1139. }
  1140. /* Update the remaining active filters */
  1141. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  1142. /* Combine the checks for whether a filter needs to be changed
  1143. * and then determine the new VLAN inside the if block, in
  1144. * order to avoid duplicating code for adding the new filter
  1145. * then deleting the old filter.
  1146. */
  1147. if ((pvid && f->vlan != pvid) ||
  1148. (vlan_filters && f->vlan == I40E_VLAN_ANY) ||
  1149. (!vlan_filters && f->vlan == 0)) {
  1150. /* Determine the new vlan we will be adding */
  1151. if (pvid)
  1152. new_vlan = pvid;
  1153. else if (vlan_filters)
  1154. new_vlan = 0;
  1155. else
  1156. new_vlan = I40E_VLAN_ANY;
  1157. /* Create the new filter */
  1158. add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
  1159. if (!add_head)
  1160. return -ENOMEM;
  1161. /* Create a temporary i40e_new_mac_filter */
  1162. new = kzalloc(sizeof(*new), GFP_ATOMIC);
  1163. if (!new)
  1164. return -ENOMEM;
  1165. new->f = add_head;
  1166. new->state = add_head->state;
  1167. /* Add the new filter to the tmp list */
  1168. hlist_add_head(&new->hlist, tmp_add_list);
  1169. /* Put the original filter into the delete list */
  1170. f->state = I40E_FILTER_REMOVE;
  1171. hash_del(&f->hlist);
  1172. hlist_add_head(&f->hlist, tmp_del_list);
  1173. }
  1174. }
  1175. vsi->has_vlan_filter = !!vlan_filters;
  1176. return 0;
  1177. }
  1178. /**
  1179. * i40e_rm_default_mac_filter - Remove the default MAC filter set by NVM
  1180. * @vsi: the PF Main VSI - inappropriate for any other VSI
  1181. * @macaddr: the MAC address
  1182. *
  1183. * Remove whatever filter the firmware set up so the driver can manage
  1184. * its own filtering intelligently.
  1185. **/
  1186. static void i40e_rm_default_mac_filter(struct i40e_vsi *vsi, u8 *macaddr)
  1187. {
  1188. struct i40e_aqc_remove_macvlan_element_data element;
  1189. struct i40e_pf *pf = vsi->back;
  1190. /* Only appropriate for the PF main VSI */
  1191. if (vsi->type != I40E_VSI_MAIN)
  1192. return;
  1193. memset(&element, 0, sizeof(element));
  1194. ether_addr_copy(element.mac_addr, macaddr);
  1195. element.vlan_tag = 0;
  1196. /* Ignore error returns, some firmware does it this way... */
  1197. element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
  1198. i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
  1199. memset(&element, 0, sizeof(element));
  1200. ether_addr_copy(element.mac_addr, macaddr);
  1201. element.vlan_tag = 0;
  1202. /* ...and some firmware does it this way. */
  1203. element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH |
  1204. I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
  1205. i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
  1206. }
  1207. /**
  1208. * i40e_add_filter - Add a mac/vlan filter to the VSI
  1209. * @vsi: the VSI to be searched
  1210. * @macaddr: the MAC address
  1211. * @vlan: the vlan
  1212. *
  1213. * Returns ptr to the filter object or NULL when no memory available.
  1214. *
  1215. * NOTE: This function is expected to be called with mac_filter_hash_lock
  1216. * being held.
  1217. **/
  1218. struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
  1219. const u8 *macaddr, s16 vlan)
  1220. {
  1221. struct i40e_mac_filter *f;
  1222. u64 key;
  1223. if (!vsi || !macaddr)
  1224. return NULL;
  1225. f = i40e_find_filter(vsi, macaddr, vlan);
  1226. if (!f) {
  1227. f = kzalloc(sizeof(*f), GFP_ATOMIC);
  1228. if (!f)
  1229. return NULL;
  1230. /* Update the boolean indicating if we need to function in
  1231. * VLAN mode.
  1232. */
  1233. if (vlan >= 0)
  1234. vsi->has_vlan_filter = true;
  1235. ether_addr_copy(f->macaddr, macaddr);
  1236. f->vlan = vlan;
  1237. f->state = I40E_FILTER_NEW;
  1238. INIT_HLIST_NODE(&f->hlist);
  1239. key = i40e_addr_to_hkey(macaddr);
  1240. hash_add(vsi->mac_filter_hash, &f->hlist, key);
  1241. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  1242. set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
  1243. }
  1244. /* If we're asked to add a filter that has been marked for removal, it
  1245. * is safe to simply restore it to active state. __i40e_del_filter
  1246. * will have simply deleted any filters which were previously marked
  1247. * NEW or FAILED, so if it is currently marked REMOVE it must have
  1248. * previously been ACTIVE. Since we haven't yet run the sync filters
  1249. * task, just restore this filter to the ACTIVE state so that the
  1250. * sync task leaves it in place
  1251. */
  1252. if (f->state == I40E_FILTER_REMOVE)
  1253. f->state = I40E_FILTER_ACTIVE;
  1254. return f;
  1255. }
  1256. /**
  1257. * __i40e_del_filter - Remove a specific filter from the VSI
  1258. * @vsi: VSI to remove from
  1259. * @f: the filter to remove from the list
  1260. *
  1261. * This function should be called instead of i40e_del_filter only if you know
  1262. * the exact filter you will remove already, such as via i40e_find_filter or
  1263. * i40e_find_mac.
  1264. *
  1265. * NOTE: This function is expected to be called with mac_filter_hash_lock
  1266. * being held.
  1267. * ANOTHER NOTE: This function MUST be called from within the context of
  1268. * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
  1269. * instead of list_for_each_entry().
  1270. **/
  1271. void __i40e_del_filter(struct i40e_vsi *vsi, struct i40e_mac_filter *f)
  1272. {
  1273. if (!f)
  1274. return;
  1275. /* If the filter was never added to firmware then we can just delete it
  1276. * directly and we don't want to set the status to remove or else an
  1277. * admin queue command will unnecessarily fire.
  1278. */
  1279. if ((f->state == I40E_FILTER_FAILED) ||
  1280. (f->state == I40E_FILTER_NEW)) {
  1281. hash_del(&f->hlist);
  1282. kfree(f);
  1283. } else {
  1284. f->state = I40E_FILTER_REMOVE;
  1285. }
  1286. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  1287. set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->state);
  1288. }
  1289. /**
  1290. * i40e_del_filter - Remove a MAC/VLAN filter from the VSI
  1291. * @vsi: the VSI to be searched
  1292. * @macaddr: the MAC address
  1293. * @vlan: the VLAN
  1294. *
  1295. * NOTE: This function is expected to be called with mac_filter_hash_lock
  1296. * being held.
  1297. * ANOTHER NOTE: This function MUST be called from within the context of
  1298. * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
  1299. * instead of list_for_each_entry().
  1300. **/
  1301. void i40e_del_filter(struct i40e_vsi *vsi, const u8 *macaddr, s16 vlan)
  1302. {
  1303. struct i40e_mac_filter *f;
  1304. if (!vsi || !macaddr)
  1305. return;
  1306. f = i40e_find_filter(vsi, macaddr, vlan);
  1307. __i40e_del_filter(vsi, f);
  1308. }
  1309. /**
  1310. * i40e_add_mac_filter - Add a MAC filter for all active VLANs
  1311. * @vsi: the VSI to be searched
  1312. * @macaddr: the mac address to be filtered
  1313. *
  1314. * If we're not in VLAN mode, just add the filter to I40E_VLAN_ANY. Otherwise,
  1315. * go through all the macvlan filters and add a macvlan filter for each
  1316. * unique vlan that already exists. If a PVID has been assigned, instead only
  1317. * add the macaddr to that VLAN.
  1318. *
  1319. * Returns last filter added on success, else NULL
  1320. **/
  1321. struct i40e_mac_filter *i40e_add_mac_filter(struct i40e_vsi *vsi,
  1322. const u8 *macaddr)
  1323. {
  1324. struct i40e_mac_filter *f, *add = NULL;
  1325. struct hlist_node *h;
  1326. int bkt;
  1327. if (vsi->info.pvid)
  1328. return i40e_add_filter(vsi, macaddr,
  1329. le16_to_cpu(vsi->info.pvid));
  1330. if (!i40e_is_vsi_in_vlan(vsi))
  1331. return i40e_add_filter(vsi, macaddr, I40E_VLAN_ANY);
  1332. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  1333. if (f->state == I40E_FILTER_REMOVE)
  1334. continue;
  1335. add = i40e_add_filter(vsi, macaddr, f->vlan);
  1336. if (!add)
  1337. return NULL;
  1338. }
  1339. return add;
  1340. }
  1341. /**
  1342. * i40e_del_mac_filter - Remove a MAC filter from all VLANs
  1343. * @vsi: the VSI to be searched
  1344. * @macaddr: the mac address to be removed
  1345. *
  1346. * Removes a given MAC address from a VSI regardless of what VLAN it has been
  1347. * associated with.
  1348. *
  1349. * Returns 0 for success, or error
  1350. **/
  1351. int i40e_del_mac_filter(struct i40e_vsi *vsi, const u8 *macaddr)
  1352. {
  1353. struct i40e_mac_filter *f;
  1354. struct hlist_node *h;
  1355. bool found = false;
  1356. int bkt;
  1357. WARN(!spin_is_locked(&vsi->mac_filter_hash_lock),
  1358. "Missing mac_filter_hash_lock\n");
  1359. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  1360. if (ether_addr_equal(macaddr, f->macaddr)) {
  1361. __i40e_del_filter(vsi, f);
  1362. found = true;
  1363. }
  1364. }
  1365. if (found)
  1366. return 0;
  1367. else
  1368. return -ENOENT;
  1369. }
  1370. /**
  1371. * i40e_set_mac - NDO callback to set mac address
  1372. * @netdev: network interface device structure
  1373. * @p: pointer to an address structure
  1374. *
  1375. * Returns 0 on success, negative on failure
  1376. **/
  1377. static int i40e_set_mac(struct net_device *netdev, void *p)
  1378. {
  1379. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1380. struct i40e_vsi *vsi = np->vsi;
  1381. struct i40e_pf *pf = vsi->back;
  1382. struct i40e_hw *hw = &pf->hw;
  1383. struct sockaddr *addr = p;
  1384. if (!is_valid_ether_addr(addr->sa_data))
  1385. return -EADDRNOTAVAIL;
  1386. if (ether_addr_equal(netdev->dev_addr, addr->sa_data)) {
  1387. netdev_info(netdev, "already using mac address %pM\n",
  1388. addr->sa_data);
  1389. return 0;
  1390. }
  1391. if (test_bit(__I40E_VSI_DOWN, vsi->back->state) ||
  1392. test_bit(__I40E_RESET_RECOVERY_PENDING, vsi->back->state))
  1393. return -EADDRNOTAVAIL;
  1394. if (ether_addr_equal(hw->mac.addr, addr->sa_data))
  1395. netdev_info(netdev, "returning to hw mac address %pM\n",
  1396. hw->mac.addr);
  1397. else
  1398. netdev_info(netdev, "set new mac address %pM\n", addr->sa_data);
  1399. /* Copy the address first, so that we avoid a possible race with
  1400. * .set_rx_mode(). If we copy after changing the address in the filter
  1401. * list, we might open ourselves to a narrow race window where
  1402. * .set_rx_mode could delete our dev_addr filter and prevent traffic
  1403. * from passing.
  1404. */
  1405. ether_addr_copy(netdev->dev_addr, addr->sa_data);
  1406. spin_lock_bh(&vsi->mac_filter_hash_lock);
  1407. i40e_del_mac_filter(vsi, netdev->dev_addr);
  1408. i40e_add_mac_filter(vsi, addr->sa_data);
  1409. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  1410. if (vsi->type == I40E_VSI_MAIN) {
  1411. i40e_status ret;
  1412. ret = i40e_aq_mac_address_write(&vsi->back->hw,
  1413. I40E_AQC_WRITE_TYPE_LAA_WOL,
  1414. addr->sa_data, NULL);
  1415. if (ret)
  1416. netdev_info(netdev, "Ignoring error from firmware on LAA update, status %s, AQ ret %s\n",
  1417. i40e_stat_str(hw, ret),
  1418. i40e_aq_str(hw, hw->aq.asq_last_status));
  1419. }
  1420. /* schedule our worker thread which will take care of
  1421. * applying the new filter changes
  1422. */
  1423. i40e_service_event_schedule(vsi->back);
  1424. return 0;
  1425. }
  1426. /**
  1427. * i40e_config_rss_aq - Prepare for RSS using AQ commands
  1428. * @vsi: vsi structure
  1429. * @seed: RSS hash seed
  1430. **/
  1431. static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
  1432. u8 *lut, u16 lut_size)
  1433. {
  1434. struct i40e_pf *pf = vsi->back;
  1435. struct i40e_hw *hw = &pf->hw;
  1436. int ret = 0;
  1437. if (seed) {
  1438. struct i40e_aqc_get_set_rss_key_data *seed_dw =
  1439. (struct i40e_aqc_get_set_rss_key_data *)seed;
  1440. ret = i40e_aq_set_rss_key(hw, vsi->id, seed_dw);
  1441. if (ret) {
  1442. dev_info(&pf->pdev->dev,
  1443. "Cannot set RSS key, err %s aq_err %s\n",
  1444. i40e_stat_str(hw, ret),
  1445. i40e_aq_str(hw, hw->aq.asq_last_status));
  1446. return ret;
  1447. }
  1448. }
  1449. if (lut) {
  1450. bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
  1451. ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
  1452. if (ret) {
  1453. dev_info(&pf->pdev->dev,
  1454. "Cannot set RSS lut, err %s aq_err %s\n",
  1455. i40e_stat_str(hw, ret),
  1456. i40e_aq_str(hw, hw->aq.asq_last_status));
  1457. return ret;
  1458. }
  1459. }
  1460. return ret;
  1461. }
  1462. /**
  1463. * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
  1464. * @vsi: VSI structure
  1465. **/
  1466. static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
  1467. {
  1468. struct i40e_pf *pf = vsi->back;
  1469. u8 seed[I40E_HKEY_ARRAY_SIZE];
  1470. u8 *lut;
  1471. int ret;
  1472. if (!(pf->hw_features & I40E_HW_RSS_AQ_CAPABLE))
  1473. return 0;
  1474. if (!vsi->rss_size)
  1475. vsi->rss_size = min_t(int, pf->alloc_rss_size,
  1476. vsi->num_queue_pairs);
  1477. if (!vsi->rss_size)
  1478. return -EINVAL;
  1479. lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
  1480. if (!lut)
  1481. return -ENOMEM;
  1482. /* Use the user configured hash keys and lookup table if there is one,
  1483. * otherwise use default
  1484. */
  1485. if (vsi->rss_lut_user)
  1486. memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
  1487. else
  1488. i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
  1489. if (vsi->rss_hkey_user)
  1490. memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
  1491. else
  1492. netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
  1493. ret = i40e_config_rss_aq(vsi, seed, lut, vsi->rss_table_size);
  1494. kfree(lut);
  1495. return ret;
  1496. }
  1497. /**
  1498. * i40e_vsi_setup_queue_map_mqprio - Prepares mqprio based tc_config
  1499. * @vsi: the VSI being configured,
  1500. * @ctxt: VSI context structure
  1501. * @enabled_tc: number of traffic classes to enable
  1502. *
  1503. * Prepares VSI tc_config to have queue configurations based on MQPRIO options.
  1504. **/
  1505. static int i40e_vsi_setup_queue_map_mqprio(struct i40e_vsi *vsi,
  1506. struct i40e_vsi_context *ctxt,
  1507. u8 enabled_tc)
  1508. {
  1509. u16 qcount = 0, max_qcount, qmap, sections = 0;
  1510. int i, override_q, pow, num_qps, ret;
  1511. u8 netdev_tc = 0, offset = 0;
  1512. if (vsi->type != I40E_VSI_MAIN)
  1513. return -EINVAL;
  1514. sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
  1515. sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
  1516. vsi->tc_config.numtc = vsi->mqprio_qopt.qopt.num_tc;
  1517. vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
  1518. num_qps = vsi->mqprio_qopt.qopt.count[0];
  1519. /* find the next higher power-of-2 of num queue pairs */
  1520. pow = ilog2(num_qps);
  1521. if (!is_power_of_2(num_qps))
  1522. pow++;
  1523. qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
  1524. (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
  1525. /* Setup queue offset/count for all TCs for given VSI */
  1526. max_qcount = vsi->mqprio_qopt.qopt.count[0];
  1527. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  1528. /* See if the given TC is enabled for the given VSI */
  1529. if (vsi->tc_config.enabled_tc & BIT(i)) {
  1530. offset = vsi->mqprio_qopt.qopt.offset[i];
  1531. qcount = vsi->mqprio_qopt.qopt.count[i];
  1532. if (qcount > max_qcount)
  1533. max_qcount = qcount;
  1534. vsi->tc_config.tc_info[i].qoffset = offset;
  1535. vsi->tc_config.tc_info[i].qcount = qcount;
  1536. vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
  1537. } else {
  1538. /* TC is not enabled so set the offset to
  1539. * default queue and allocate one queue
  1540. * for the given TC.
  1541. */
  1542. vsi->tc_config.tc_info[i].qoffset = 0;
  1543. vsi->tc_config.tc_info[i].qcount = 1;
  1544. vsi->tc_config.tc_info[i].netdev_tc = 0;
  1545. }
  1546. }
  1547. /* Set actual Tx/Rx queue pairs */
  1548. vsi->num_queue_pairs = offset + qcount;
  1549. /* Setup queue TC[0].qmap for given VSI context */
  1550. ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
  1551. ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
  1552. ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
  1553. ctxt->info.valid_sections |= cpu_to_le16(sections);
  1554. /* Reconfigure RSS for main VSI with max queue count */
  1555. vsi->rss_size = max_qcount;
  1556. ret = i40e_vsi_config_rss(vsi);
  1557. if (ret) {
  1558. dev_info(&vsi->back->pdev->dev,
  1559. "Failed to reconfig rss for num_queues (%u)\n",
  1560. max_qcount);
  1561. return ret;
  1562. }
  1563. vsi->reconfig_rss = true;
  1564. dev_dbg(&vsi->back->pdev->dev,
  1565. "Reconfigured rss with num_queues (%u)\n", max_qcount);
  1566. /* Find queue count available for channel VSIs and starting offset
  1567. * for channel VSIs
  1568. */
  1569. override_q = vsi->mqprio_qopt.qopt.count[0];
  1570. if (override_q && override_q < vsi->num_queue_pairs) {
  1571. vsi->cnt_q_avail = vsi->num_queue_pairs - override_q;
  1572. vsi->next_base_queue = override_q;
  1573. }
  1574. return 0;
  1575. }
  1576. /**
  1577. * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
  1578. * @vsi: the VSI being setup
  1579. * @ctxt: VSI context structure
  1580. * @enabled_tc: Enabled TCs bitmap
  1581. * @is_add: True if called before Add VSI
  1582. *
  1583. * Setup VSI queue mapping for enabled traffic classes.
  1584. **/
  1585. static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
  1586. struct i40e_vsi_context *ctxt,
  1587. u8 enabled_tc,
  1588. bool is_add)
  1589. {
  1590. struct i40e_pf *pf = vsi->back;
  1591. u16 sections = 0;
  1592. u8 netdev_tc = 0;
  1593. u16 numtc = 1;
  1594. u16 qcount;
  1595. u8 offset;
  1596. u16 qmap;
  1597. int i;
  1598. u16 num_tc_qps = 0;
  1599. sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
  1600. offset = 0;
  1601. /* Number of queues per enabled TC */
  1602. num_tc_qps = vsi->alloc_queue_pairs;
  1603. if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
  1604. /* Find numtc from enabled TC bitmap */
  1605. for (i = 0, numtc = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  1606. if (enabled_tc & BIT(i)) /* TC is enabled */
  1607. numtc++;
  1608. }
  1609. if (!numtc) {
  1610. dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
  1611. numtc = 1;
  1612. }
  1613. num_tc_qps = num_tc_qps / numtc;
  1614. num_tc_qps = min_t(int, num_tc_qps,
  1615. i40e_pf_get_max_q_per_tc(pf));
  1616. }
  1617. vsi->tc_config.numtc = numtc;
  1618. vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
  1619. /* Do not allow use more TC queue pairs than MSI-X vectors exist */
  1620. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  1621. num_tc_qps = min_t(int, num_tc_qps, pf->num_lan_msix);
  1622. /* Setup queue offset/count for all TCs for given VSI */
  1623. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  1624. /* See if the given TC is enabled for the given VSI */
  1625. if (vsi->tc_config.enabled_tc & BIT(i)) {
  1626. /* TC is enabled */
  1627. int pow, num_qps;
  1628. switch (vsi->type) {
  1629. case I40E_VSI_MAIN:
  1630. if (!(pf->flags & (I40E_FLAG_FD_SB_ENABLED |
  1631. I40E_FLAG_FD_ATR_ENABLED)) ||
  1632. vsi->tc_config.enabled_tc != 1) {
  1633. qcount = min_t(int, pf->alloc_rss_size,
  1634. num_tc_qps);
  1635. break;
  1636. }
  1637. case I40E_VSI_FDIR:
  1638. case I40E_VSI_SRIOV:
  1639. case I40E_VSI_VMDQ2:
  1640. default:
  1641. qcount = num_tc_qps;
  1642. WARN_ON(i != 0);
  1643. break;
  1644. }
  1645. vsi->tc_config.tc_info[i].qoffset = offset;
  1646. vsi->tc_config.tc_info[i].qcount = qcount;
  1647. /* find the next higher power-of-2 of num queue pairs */
  1648. num_qps = qcount;
  1649. pow = 0;
  1650. while (num_qps && (BIT_ULL(pow) < qcount)) {
  1651. pow++;
  1652. num_qps >>= 1;
  1653. }
  1654. vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
  1655. qmap =
  1656. (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
  1657. (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
  1658. offset += qcount;
  1659. } else {
  1660. /* TC is not enabled so set the offset to
  1661. * default queue and allocate one queue
  1662. * for the given TC.
  1663. */
  1664. vsi->tc_config.tc_info[i].qoffset = 0;
  1665. vsi->tc_config.tc_info[i].qcount = 1;
  1666. vsi->tc_config.tc_info[i].netdev_tc = 0;
  1667. qmap = 0;
  1668. }
  1669. ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
  1670. }
  1671. /* Set actual Tx/Rx queue pairs */
  1672. vsi->num_queue_pairs = offset;
  1673. if ((vsi->type == I40E_VSI_MAIN) && (numtc == 1)) {
  1674. if (vsi->req_queue_pairs > 0)
  1675. vsi->num_queue_pairs = vsi->req_queue_pairs;
  1676. else if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  1677. vsi->num_queue_pairs = pf->num_lan_msix;
  1678. }
  1679. /* Scheduler section valid can only be set for ADD VSI */
  1680. if (is_add) {
  1681. sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
  1682. ctxt->info.up_enable_bits = enabled_tc;
  1683. }
  1684. if (vsi->type == I40E_VSI_SRIOV) {
  1685. ctxt->info.mapping_flags |=
  1686. cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
  1687. for (i = 0; i < vsi->num_queue_pairs; i++)
  1688. ctxt->info.queue_mapping[i] =
  1689. cpu_to_le16(vsi->base_queue + i);
  1690. } else {
  1691. ctxt->info.mapping_flags |=
  1692. cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
  1693. ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
  1694. }
  1695. ctxt->info.valid_sections |= cpu_to_le16(sections);
  1696. }
  1697. /**
  1698. * i40e_addr_sync - Callback for dev_(mc|uc)_sync to add address
  1699. * @netdev: the netdevice
  1700. * @addr: address to add
  1701. *
  1702. * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
  1703. * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
  1704. */
  1705. static int i40e_addr_sync(struct net_device *netdev, const u8 *addr)
  1706. {
  1707. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1708. struct i40e_vsi *vsi = np->vsi;
  1709. if (i40e_add_mac_filter(vsi, addr))
  1710. return 0;
  1711. else
  1712. return -ENOMEM;
  1713. }
  1714. /**
  1715. * i40e_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
  1716. * @netdev: the netdevice
  1717. * @addr: address to add
  1718. *
  1719. * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call
  1720. * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
  1721. */
  1722. static int i40e_addr_unsync(struct net_device *netdev, const u8 *addr)
  1723. {
  1724. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1725. struct i40e_vsi *vsi = np->vsi;
  1726. /* Under some circumstances, we might receive a request to delete
  1727. * our own device address from our uc list. Because we store the
  1728. * device address in the VSI's MAC/VLAN filter list, we need to ignore
  1729. * such requests and not delete our device address from this list.
  1730. */
  1731. if (ether_addr_equal(addr, netdev->dev_addr))
  1732. return 0;
  1733. i40e_del_mac_filter(vsi, addr);
  1734. return 0;
  1735. }
  1736. /**
  1737. * i40e_set_rx_mode - NDO callback to set the netdev filters
  1738. * @netdev: network interface device structure
  1739. **/
  1740. static void i40e_set_rx_mode(struct net_device *netdev)
  1741. {
  1742. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1743. struct i40e_vsi *vsi = np->vsi;
  1744. spin_lock_bh(&vsi->mac_filter_hash_lock);
  1745. __dev_uc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
  1746. __dev_mc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
  1747. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  1748. /* check for other flag changes */
  1749. if (vsi->current_netdev_flags != vsi->netdev->flags) {
  1750. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  1751. set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
  1752. }
  1753. }
  1754. /**
  1755. * i40e_undo_del_filter_entries - Undo the changes made to MAC filter entries
  1756. * @vsi: Pointer to VSI struct
  1757. * @from: Pointer to list which contains MAC filter entries - changes to
  1758. * those entries needs to be undone.
  1759. *
  1760. * MAC filter entries from this list were slated for deletion.
  1761. **/
  1762. static void i40e_undo_del_filter_entries(struct i40e_vsi *vsi,
  1763. struct hlist_head *from)
  1764. {
  1765. struct i40e_mac_filter *f;
  1766. struct hlist_node *h;
  1767. hlist_for_each_entry_safe(f, h, from, hlist) {
  1768. u64 key = i40e_addr_to_hkey(f->macaddr);
  1769. /* Move the element back into MAC filter list*/
  1770. hlist_del(&f->hlist);
  1771. hash_add(vsi->mac_filter_hash, &f->hlist, key);
  1772. }
  1773. }
  1774. /**
  1775. * i40e_undo_add_filter_entries - Undo the changes made to MAC filter entries
  1776. * @vsi: Pointer to vsi struct
  1777. * @from: Pointer to list which contains MAC filter entries - changes to
  1778. * those entries needs to be undone.
  1779. *
  1780. * MAC filter entries from this list were slated for addition.
  1781. **/
  1782. static void i40e_undo_add_filter_entries(struct i40e_vsi *vsi,
  1783. struct hlist_head *from)
  1784. {
  1785. struct i40e_new_mac_filter *new;
  1786. struct hlist_node *h;
  1787. hlist_for_each_entry_safe(new, h, from, hlist) {
  1788. /* We can simply free the wrapper structure */
  1789. hlist_del(&new->hlist);
  1790. kfree(new);
  1791. }
  1792. }
  1793. /**
  1794. * i40e_next_entry - Get the next non-broadcast filter from a list
  1795. * @next: pointer to filter in list
  1796. *
  1797. * Returns the next non-broadcast filter in the list. Required so that we
  1798. * ignore broadcast filters within the list, since these are not handled via
  1799. * the normal firmware update path.
  1800. */
  1801. static
  1802. struct i40e_new_mac_filter *i40e_next_filter(struct i40e_new_mac_filter *next)
  1803. {
  1804. hlist_for_each_entry_continue(next, hlist) {
  1805. if (!is_broadcast_ether_addr(next->f->macaddr))
  1806. return next;
  1807. }
  1808. return NULL;
  1809. }
  1810. /**
  1811. * i40e_update_filter_state - Update filter state based on return data
  1812. * from firmware
  1813. * @count: Number of filters added
  1814. * @add_list: return data from fw
  1815. * @head: pointer to first filter in current batch
  1816. *
  1817. * MAC filter entries from list were slated to be added to device. Returns
  1818. * number of successful filters. Note that 0 does NOT mean success!
  1819. **/
  1820. static int
  1821. i40e_update_filter_state(int count,
  1822. struct i40e_aqc_add_macvlan_element_data *add_list,
  1823. struct i40e_new_mac_filter *add_head)
  1824. {
  1825. int retval = 0;
  1826. int i;
  1827. for (i = 0; i < count; i++) {
  1828. /* Always check status of each filter. We don't need to check
  1829. * the firmware return status because we pre-set the filter
  1830. * status to I40E_AQC_MM_ERR_NO_RES when sending the filter
  1831. * request to the adminq. Thus, if it no longer matches then
  1832. * we know the filter is active.
  1833. */
  1834. if (add_list[i].match_method == I40E_AQC_MM_ERR_NO_RES) {
  1835. add_head->state = I40E_FILTER_FAILED;
  1836. } else {
  1837. add_head->state = I40E_FILTER_ACTIVE;
  1838. retval++;
  1839. }
  1840. add_head = i40e_next_filter(add_head);
  1841. if (!add_head)
  1842. break;
  1843. }
  1844. return retval;
  1845. }
  1846. /**
  1847. * i40e_aqc_del_filters - Request firmware to delete a set of filters
  1848. * @vsi: ptr to the VSI
  1849. * @vsi_name: name to display in messages
  1850. * @list: the list of filters to send to firmware
  1851. * @num_del: the number of filters to delete
  1852. * @retval: Set to -EIO on failure to delete
  1853. *
  1854. * Send a request to firmware via AdminQ to delete a set of filters. Uses
  1855. * *retval instead of a return value so that success does not force ret_val to
  1856. * be set to 0. This ensures that a sequence of calls to this function
  1857. * preserve the previous value of *retval on successful delete.
  1858. */
  1859. static
  1860. void i40e_aqc_del_filters(struct i40e_vsi *vsi, const char *vsi_name,
  1861. struct i40e_aqc_remove_macvlan_element_data *list,
  1862. int num_del, int *retval)
  1863. {
  1864. struct i40e_hw *hw = &vsi->back->hw;
  1865. i40e_status aq_ret;
  1866. int aq_err;
  1867. aq_ret = i40e_aq_remove_macvlan(hw, vsi->seid, list, num_del, NULL);
  1868. aq_err = hw->aq.asq_last_status;
  1869. /* Explicitly ignore and do not report when firmware returns ENOENT */
  1870. if (aq_ret && !(aq_err == I40E_AQ_RC_ENOENT)) {
  1871. *retval = -EIO;
  1872. dev_info(&vsi->back->pdev->dev,
  1873. "ignoring delete macvlan error on %s, err %s, aq_err %s\n",
  1874. vsi_name, i40e_stat_str(hw, aq_ret),
  1875. i40e_aq_str(hw, aq_err));
  1876. }
  1877. }
  1878. /**
  1879. * i40e_aqc_add_filters - Request firmware to add a set of filters
  1880. * @vsi: ptr to the VSI
  1881. * @vsi_name: name to display in messages
  1882. * @list: the list of filters to send to firmware
  1883. * @add_head: Position in the add hlist
  1884. * @num_add: the number of filters to add
  1885. *
  1886. * Send a request to firmware via AdminQ to add a chunk of filters. Will set
  1887. * __I40E_VSI_OVERFLOW_PROMISC bit in vsi->state if the firmware has run out of
  1888. * space for more filters.
  1889. */
  1890. static
  1891. void i40e_aqc_add_filters(struct i40e_vsi *vsi, const char *vsi_name,
  1892. struct i40e_aqc_add_macvlan_element_data *list,
  1893. struct i40e_new_mac_filter *add_head,
  1894. int num_add)
  1895. {
  1896. struct i40e_hw *hw = &vsi->back->hw;
  1897. int aq_err, fcnt;
  1898. i40e_aq_add_macvlan(hw, vsi->seid, list, num_add, NULL);
  1899. aq_err = hw->aq.asq_last_status;
  1900. fcnt = i40e_update_filter_state(num_add, list, add_head);
  1901. if (fcnt != num_add) {
  1902. set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
  1903. dev_warn(&vsi->back->pdev->dev,
  1904. "Error %s adding RX filters on %s, promiscuous mode forced on\n",
  1905. i40e_aq_str(hw, aq_err),
  1906. vsi_name);
  1907. }
  1908. }
  1909. /**
  1910. * i40e_aqc_broadcast_filter - Set promiscuous broadcast flags
  1911. * @vsi: pointer to the VSI
  1912. * @f: filter data
  1913. *
  1914. * This function sets or clears the promiscuous broadcast flags for VLAN
  1915. * filters in order to properly receive broadcast frames. Assumes that only
  1916. * broadcast filters are passed.
  1917. *
  1918. * Returns status indicating success or failure;
  1919. **/
  1920. static i40e_status
  1921. i40e_aqc_broadcast_filter(struct i40e_vsi *vsi, const char *vsi_name,
  1922. struct i40e_mac_filter *f)
  1923. {
  1924. bool enable = f->state == I40E_FILTER_NEW;
  1925. struct i40e_hw *hw = &vsi->back->hw;
  1926. i40e_status aq_ret;
  1927. if (f->vlan == I40E_VLAN_ANY) {
  1928. aq_ret = i40e_aq_set_vsi_broadcast(hw,
  1929. vsi->seid,
  1930. enable,
  1931. NULL);
  1932. } else {
  1933. aq_ret = i40e_aq_set_vsi_bc_promisc_on_vlan(hw,
  1934. vsi->seid,
  1935. enable,
  1936. f->vlan,
  1937. NULL);
  1938. }
  1939. if (aq_ret) {
  1940. set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
  1941. dev_warn(&vsi->back->pdev->dev,
  1942. "Error %s, forcing overflow promiscuous on %s\n",
  1943. i40e_aq_str(hw, hw->aq.asq_last_status),
  1944. vsi_name);
  1945. }
  1946. return aq_ret;
  1947. }
  1948. /**
  1949. * i40e_set_promiscuous - set promiscuous mode
  1950. * @pf: board private structure
  1951. * @promisc: promisc on or off
  1952. *
  1953. * There are different ways of setting promiscuous mode on a PF depending on
  1954. * what state/environment we're in. This identifies and sets it appropriately.
  1955. * Returns 0 on success.
  1956. **/
  1957. static int i40e_set_promiscuous(struct i40e_pf *pf, bool promisc)
  1958. {
  1959. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  1960. struct i40e_hw *hw = &pf->hw;
  1961. i40e_status aq_ret;
  1962. if (vsi->type == I40E_VSI_MAIN &&
  1963. pf->lan_veb != I40E_NO_VEB &&
  1964. !(pf->flags & I40E_FLAG_MFP_ENABLED)) {
  1965. /* set defport ON for Main VSI instead of true promisc
  1966. * this way we will get all unicast/multicast and VLAN
  1967. * promisc behavior but will not get VF or VMDq traffic
  1968. * replicated on the Main VSI.
  1969. */
  1970. if (promisc)
  1971. aq_ret = i40e_aq_set_default_vsi(hw,
  1972. vsi->seid,
  1973. NULL);
  1974. else
  1975. aq_ret = i40e_aq_clear_default_vsi(hw,
  1976. vsi->seid,
  1977. NULL);
  1978. if (aq_ret) {
  1979. dev_info(&pf->pdev->dev,
  1980. "Set default VSI failed, err %s, aq_err %s\n",
  1981. i40e_stat_str(hw, aq_ret),
  1982. i40e_aq_str(hw, hw->aq.asq_last_status));
  1983. }
  1984. } else {
  1985. aq_ret = i40e_aq_set_vsi_unicast_promiscuous(
  1986. hw,
  1987. vsi->seid,
  1988. promisc, NULL,
  1989. true);
  1990. if (aq_ret) {
  1991. dev_info(&pf->pdev->dev,
  1992. "set unicast promisc failed, err %s, aq_err %s\n",
  1993. i40e_stat_str(hw, aq_ret),
  1994. i40e_aq_str(hw, hw->aq.asq_last_status));
  1995. }
  1996. aq_ret = i40e_aq_set_vsi_multicast_promiscuous(
  1997. hw,
  1998. vsi->seid,
  1999. promisc, NULL);
  2000. if (aq_ret) {
  2001. dev_info(&pf->pdev->dev,
  2002. "set multicast promisc failed, err %s, aq_err %s\n",
  2003. i40e_stat_str(hw, aq_ret),
  2004. i40e_aq_str(hw, hw->aq.asq_last_status));
  2005. }
  2006. }
  2007. if (!aq_ret)
  2008. pf->cur_promisc = promisc;
  2009. return aq_ret;
  2010. }
  2011. /**
  2012. * i40e_sync_vsi_filters - Update the VSI filter list to the HW
  2013. * @vsi: ptr to the VSI
  2014. *
  2015. * Push any outstanding VSI filter changes through the AdminQ.
  2016. *
  2017. * Returns 0 or error value
  2018. **/
  2019. int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
  2020. {
  2021. struct hlist_head tmp_add_list, tmp_del_list;
  2022. struct i40e_mac_filter *f;
  2023. struct i40e_new_mac_filter *new, *add_head = NULL;
  2024. struct i40e_hw *hw = &vsi->back->hw;
  2025. bool old_overflow, new_overflow;
  2026. unsigned int failed_filters = 0;
  2027. unsigned int vlan_filters = 0;
  2028. char vsi_name[16] = "PF";
  2029. int filter_list_len = 0;
  2030. i40e_status aq_ret = 0;
  2031. u32 changed_flags = 0;
  2032. struct hlist_node *h;
  2033. struct i40e_pf *pf;
  2034. int num_add = 0;
  2035. int num_del = 0;
  2036. int retval = 0;
  2037. u16 cmd_flags;
  2038. int list_size;
  2039. int bkt;
  2040. /* empty array typed pointers, kcalloc later */
  2041. struct i40e_aqc_add_macvlan_element_data *add_list;
  2042. struct i40e_aqc_remove_macvlan_element_data *del_list;
  2043. while (test_and_set_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state))
  2044. usleep_range(1000, 2000);
  2045. pf = vsi->back;
  2046. old_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
  2047. if (vsi->netdev) {
  2048. changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
  2049. vsi->current_netdev_flags = vsi->netdev->flags;
  2050. }
  2051. INIT_HLIST_HEAD(&tmp_add_list);
  2052. INIT_HLIST_HEAD(&tmp_del_list);
  2053. if (vsi->type == I40E_VSI_SRIOV)
  2054. snprintf(vsi_name, sizeof(vsi_name) - 1, "VF %d", vsi->vf_id);
  2055. else if (vsi->type != I40E_VSI_MAIN)
  2056. snprintf(vsi_name, sizeof(vsi_name) - 1, "vsi %d", vsi->seid);
  2057. if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
  2058. vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
  2059. spin_lock_bh(&vsi->mac_filter_hash_lock);
  2060. /* Create a list of filters to delete. */
  2061. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  2062. if (f->state == I40E_FILTER_REMOVE) {
  2063. /* Move the element into temporary del_list */
  2064. hash_del(&f->hlist);
  2065. hlist_add_head(&f->hlist, &tmp_del_list);
  2066. /* Avoid counting removed filters */
  2067. continue;
  2068. }
  2069. if (f->state == I40E_FILTER_NEW) {
  2070. /* Create a temporary i40e_new_mac_filter */
  2071. new = kzalloc(sizeof(*new), GFP_ATOMIC);
  2072. if (!new)
  2073. goto err_no_memory_locked;
  2074. /* Store pointer to the real filter */
  2075. new->f = f;
  2076. new->state = f->state;
  2077. /* Add it to the hash list */
  2078. hlist_add_head(&new->hlist, &tmp_add_list);
  2079. }
  2080. /* Count the number of active (current and new) VLAN
  2081. * filters we have now. Does not count filters which
  2082. * are marked for deletion.
  2083. */
  2084. if (f->vlan > 0)
  2085. vlan_filters++;
  2086. }
  2087. retval = i40e_correct_mac_vlan_filters(vsi,
  2088. &tmp_add_list,
  2089. &tmp_del_list,
  2090. vlan_filters);
  2091. if (retval)
  2092. goto err_no_memory_locked;
  2093. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2094. }
  2095. /* Now process 'del_list' outside the lock */
  2096. if (!hlist_empty(&tmp_del_list)) {
  2097. filter_list_len = hw->aq.asq_buf_size /
  2098. sizeof(struct i40e_aqc_remove_macvlan_element_data);
  2099. list_size = filter_list_len *
  2100. sizeof(struct i40e_aqc_remove_macvlan_element_data);
  2101. del_list = kzalloc(list_size, GFP_ATOMIC);
  2102. if (!del_list)
  2103. goto err_no_memory;
  2104. hlist_for_each_entry_safe(f, h, &tmp_del_list, hlist) {
  2105. cmd_flags = 0;
  2106. /* handle broadcast filters by updating the broadcast
  2107. * promiscuous flag and release filter list.
  2108. */
  2109. if (is_broadcast_ether_addr(f->macaddr)) {
  2110. i40e_aqc_broadcast_filter(vsi, vsi_name, f);
  2111. hlist_del(&f->hlist);
  2112. kfree(f);
  2113. continue;
  2114. }
  2115. /* add to delete list */
  2116. ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
  2117. if (f->vlan == I40E_VLAN_ANY) {
  2118. del_list[num_del].vlan_tag = 0;
  2119. cmd_flags |= I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
  2120. } else {
  2121. del_list[num_del].vlan_tag =
  2122. cpu_to_le16((u16)(f->vlan));
  2123. }
  2124. cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
  2125. del_list[num_del].flags = cmd_flags;
  2126. num_del++;
  2127. /* flush a full buffer */
  2128. if (num_del == filter_list_len) {
  2129. i40e_aqc_del_filters(vsi, vsi_name, del_list,
  2130. num_del, &retval);
  2131. memset(del_list, 0, list_size);
  2132. num_del = 0;
  2133. }
  2134. /* Release memory for MAC filter entries which were
  2135. * synced up with HW.
  2136. */
  2137. hlist_del(&f->hlist);
  2138. kfree(f);
  2139. }
  2140. if (num_del) {
  2141. i40e_aqc_del_filters(vsi, vsi_name, del_list,
  2142. num_del, &retval);
  2143. }
  2144. kfree(del_list);
  2145. del_list = NULL;
  2146. }
  2147. if (!hlist_empty(&tmp_add_list)) {
  2148. /* Do all the adds now. */
  2149. filter_list_len = hw->aq.asq_buf_size /
  2150. sizeof(struct i40e_aqc_add_macvlan_element_data);
  2151. list_size = filter_list_len *
  2152. sizeof(struct i40e_aqc_add_macvlan_element_data);
  2153. add_list = kzalloc(list_size, GFP_ATOMIC);
  2154. if (!add_list)
  2155. goto err_no_memory;
  2156. num_add = 0;
  2157. hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
  2158. /* handle broadcast filters by updating the broadcast
  2159. * promiscuous flag instead of adding a MAC filter.
  2160. */
  2161. if (is_broadcast_ether_addr(new->f->macaddr)) {
  2162. if (i40e_aqc_broadcast_filter(vsi, vsi_name,
  2163. new->f))
  2164. new->state = I40E_FILTER_FAILED;
  2165. else
  2166. new->state = I40E_FILTER_ACTIVE;
  2167. continue;
  2168. }
  2169. /* add to add array */
  2170. if (num_add == 0)
  2171. add_head = new;
  2172. cmd_flags = 0;
  2173. ether_addr_copy(add_list[num_add].mac_addr,
  2174. new->f->macaddr);
  2175. if (new->f->vlan == I40E_VLAN_ANY) {
  2176. add_list[num_add].vlan_tag = 0;
  2177. cmd_flags |= I40E_AQC_MACVLAN_ADD_IGNORE_VLAN;
  2178. } else {
  2179. add_list[num_add].vlan_tag =
  2180. cpu_to_le16((u16)(new->f->vlan));
  2181. }
  2182. add_list[num_add].queue_number = 0;
  2183. /* set invalid match method for later detection */
  2184. add_list[num_add].match_method = I40E_AQC_MM_ERR_NO_RES;
  2185. cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
  2186. add_list[num_add].flags = cpu_to_le16(cmd_flags);
  2187. num_add++;
  2188. /* flush a full buffer */
  2189. if (num_add == filter_list_len) {
  2190. i40e_aqc_add_filters(vsi, vsi_name, add_list,
  2191. add_head, num_add);
  2192. memset(add_list, 0, list_size);
  2193. num_add = 0;
  2194. }
  2195. }
  2196. if (num_add) {
  2197. i40e_aqc_add_filters(vsi, vsi_name, add_list, add_head,
  2198. num_add);
  2199. }
  2200. /* Now move all of the filters from the temp add list back to
  2201. * the VSI's list.
  2202. */
  2203. spin_lock_bh(&vsi->mac_filter_hash_lock);
  2204. hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
  2205. /* Only update the state if we're still NEW */
  2206. if (new->f->state == I40E_FILTER_NEW)
  2207. new->f->state = new->state;
  2208. hlist_del(&new->hlist);
  2209. kfree(new);
  2210. }
  2211. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2212. kfree(add_list);
  2213. add_list = NULL;
  2214. }
  2215. /* Determine the number of active and failed filters. */
  2216. spin_lock_bh(&vsi->mac_filter_hash_lock);
  2217. vsi->active_filters = 0;
  2218. hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
  2219. if (f->state == I40E_FILTER_ACTIVE)
  2220. vsi->active_filters++;
  2221. else if (f->state == I40E_FILTER_FAILED)
  2222. failed_filters++;
  2223. }
  2224. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2225. /* Check if we are able to exit overflow promiscuous mode. We can
  2226. * safely exit if we didn't just enter, we no longer have any failed
  2227. * filters, and we have reduced filters below the threshold value.
  2228. */
  2229. if (old_overflow && !failed_filters &&
  2230. vsi->active_filters < vsi->promisc_threshold) {
  2231. dev_info(&pf->pdev->dev,
  2232. "filter logjam cleared on %s, leaving overflow promiscuous mode\n",
  2233. vsi_name);
  2234. clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
  2235. vsi->promisc_threshold = 0;
  2236. }
  2237. /* if the VF is not trusted do not do promisc */
  2238. if ((vsi->type == I40E_VSI_SRIOV) && !pf->vf[vsi->vf_id].trusted) {
  2239. clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
  2240. goto out;
  2241. }
  2242. new_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
  2243. /* If we are entering overflow promiscuous, we need to calculate a new
  2244. * threshold for when we are safe to exit
  2245. */
  2246. if (!old_overflow && new_overflow)
  2247. vsi->promisc_threshold = (vsi->active_filters * 3) / 4;
  2248. /* check for changes in promiscuous modes */
  2249. if (changed_flags & IFF_ALLMULTI) {
  2250. bool cur_multipromisc;
  2251. cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
  2252. aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
  2253. vsi->seid,
  2254. cur_multipromisc,
  2255. NULL);
  2256. if (aq_ret) {
  2257. retval = i40e_aq_rc_to_posix(aq_ret,
  2258. hw->aq.asq_last_status);
  2259. dev_info(&pf->pdev->dev,
  2260. "set multi promisc failed on %s, err %s aq_err %s\n",
  2261. vsi_name,
  2262. i40e_stat_str(hw, aq_ret),
  2263. i40e_aq_str(hw, hw->aq.asq_last_status));
  2264. }
  2265. }
  2266. if ((changed_flags & IFF_PROMISC) || old_overflow != new_overflow) {
  2267. bool cur_promisc;
  2268. cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
  2269. new_overflow);
  2270. aq_ret = i40e_set_promiscuous(pf, cur_promisc);
  2271. if (aq_ret) {
  2272. retval = i40e_aq_rc_to_posix(aq_ret,
  2273. hw->aq.asq_last_status);
  2274. dev_info(&pf->pdev->dev,
  2275. "Setting promiscuous %s failed on %s, err %s aq_err %s\n",
  2276. cur_promisc ? "on" : "off",
  2277. vsi_name,
  2278. i40e_stat_str(hw, aq_ret),
  2279. i40e_aq_str(hw, hw->aq.asq_last_status));
  2280. }
  2281. }
  2282. out:
  2283. /* if something went wrong then set the changed flag so we try again */
  2284. if (retval)
  2285. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  2286. clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
  2287. return retval;
  2288. err_no_memory:
  2289. /* Restore elements on the temporary add and delete lists */
  2290. spin_lock_bh(&vsi->mac_filter_hash_lock);
  2291. err_no_memory_locked:
  2292. i40e_undo_del_filter_entries(vsi, &tmp_del_list);
  2293. i40e_undo_add_filter_entries(vsi, &tmp_add_list);
  2294. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2295. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  2296. clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
  2297. return -ENOMEM;
  2298. }
  2299. /**
  2300. * i40e_sync_filters_subtask - Sync the VSI filter list with HW
  2301. * @pf: board private structure
  2302. **/
  2303. static void i40e_sync_filters_subtask(struct i40e_pf *pf)
  2304. {
  2305. int v;
  2306. if (!pf)
  2307. return;
  2308. if (!test_and_clear_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state))
  2309. return;
  2310. for (v = 0; v < pf->num_alloc_vsi; v++) {
  2311. if (pf->vsi[v] &&
  2312. (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED)) {
  2313. int ret = i40e_sync_vsi_filters(pf->vsi[v]);
  2314. if (ret) {
  2315. /* come back and try again later */
  2316. set_bit(__I40E_MACVLAN_SYNC_PENDING,
  2317. pf->state);
  2318. break;
  2319. }
  2320. }
  2321. }
  2322. }
  2323. /**
  2324. * i40e_max_xdp_frame_size - returns the maximum allowed frame size for XDP
  2325. * @vsi: the vsi
  2326. **/
  2327. static int i40e_max_xdp_frame_size(struct i40e_vsi *vsi)
  2328. {
  2329. if (PAGE_SIZE >= 8192 || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
  2330. return I40E_RXBUFFER_2048;
  2331. else
  2332. return I40E_RXBUFFER_3072;
  2333. }
  2334. /**
  2335. * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
  2336. * @netdev: network interface device structure
  2337. * @new_mtu: new value for maximum frame size
  2338. *
  2339. * Returns 0 on success, negative on failure
  2340. **/
  2341. static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
  2342. {
  2343. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2344. struct i40e_vsi *vsi = np->vsi;
  2345. struct i40e_pf *pf = vsi->back;
  2346. if (i40e_enabled_xdp_vsi(vsi)) {
  2347. int frame_size = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
  2348. if (frame_size > i40e_max_xdp_frame_size(vsi))
  2349. return -EINVAL;
  2350. }
  2351. netdev_info(netdev, "changing MTU from %d to %d\n",
  2352. netdev->mtu, new_mtu);
  2353. netdev->mtu = new_mtu;
  2354. if (netif_running(netdev))
  2355. i40e_vsi_reinit_locked(vsi);
  2356. set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
  2357. set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
  2358. return 0;
  2359. }
  2360. /**
  2361. * i40e_ioctl - Access the hwtstamp interface
  2362. * @netdev: network interface device structure
  2363. * @ifr: interface request data
  2364. * @cmd: ioctl command
  2365. **/
  2366. int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
  2367. {
  2368. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2369. struct i40e_pf *pf = np->vsi->back;
  2370. switch (cmd) {
  2371. case SIOCGHWTSTAMP:
  2372. return i40e_ptp_get_ts_config(pf, ifr);
  2373. case SIOCSHWTSTAMP:
  2374. return i40e_ptp_set_ts_config(pf, ifr);
  2375. default:
  2376. return -EOPNOTSUPP;
  2377. }
  2378. }
  2379. /**
  2380. * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
  2381. * @vsi: the vsi being adjusted
  2382. **/
  2383. void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
  2384. {
  2385. struct i40e_vsi_context ctxt;
  2386. i40e_status ret;
  2387. if ((vsi->info.valid_sections &
  2388. cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
  2389. ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
  2390. return; /* already enabled */
  2391. vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
  2392. vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
  2393. I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
  2394. ctxt.seid = vsi->seid;
  2395. ctxt.info = vsi->info;
  2396. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  2397. if (ret) {
  2398. dev_info(&vsi->back->pdev->dev,
  2399. "update vlan stripping failed, err %s aq_err %s\n",
  2400. i40e_stat_str(&vsi->back->hw, ret),
  2401. i40e_aq_str(&vsi->back->hw,
  2402. vsi->back->hw.aq.asq_last_status));
  2403. }
  2404. }
  2405. /**
  2406. * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
  2407. * @vsi: the vsi being adjusted
  2408. **/
  2409. void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
  2410. {
  2411. struct i40e_vsi_context ctxt;
  2412. i40e_status ret;
  2413. if ((vsi->info.valid_sections &
  2414. cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
  2415. ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
  2416. I40E_AQ_VSI_PVLAN_EMOD_MASK))
  2417. return; /* already disabled */
  2418. vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
  2419. vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
  2420. I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
  2421. ctxt.seid = vsi->seid;
  2422. ctxt.info = vsi->info;
  2423. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  2424. if (ret) {
  2425. dev_info(&vsi->back->pdev->dev,
  2426. "update vlan stripping failed, err %s aq_err %s\n",
  2427. i40e_stat_str(&vsi->back->hw, ret),
  2428. i40e_aq_str(&vsi->back->hw,
  2429. vsi->back->hw.aq.asq_last_status));
  2430. }
  2431. }
  2432. /**
  2433. * i40e_add_vlan_all_mac - Add a MAC/VLAN filter for each existing MAC address
  2434. * @vsi: the vsi being configured
  2435. * @vid: vlan id to be added (0 = untagged only , -1 = any)
  2436. *
  2437. * This is a helper function for adding a new MAC/VLAN filter with the
  2438. * specified VLAN for each existing MAC address already in the hash table.
  2439. * This function does *not* perform any accounting to update filters based on
  2440. * VLAN mode.
  2441. *
  2442. * NOTE: this function expects to be called while under the
  2443. * mac_filter_hash_lock
  2444. **/
  2445. int i40e_add_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
  2446. {
  2447. struct i40e_mac_filter *f, *add_f;
  2448. struct hlist_node *h;
  2449. int bkt;
  2450. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  2451. if (f->state == I40E_FILTER_REMOVE)
  2452. continue;
  2453. add_f = i40e_add_filter(vsi, f->macaddr, vid);
  2454. if (!add_f) {
  2455. dev_info(&vsi->back->pdev->dev,
  2456. "Could not add vlan filter %d for %pM\n",
  2457. vid, f->macaddr);
  2458. return -ENOMEM;
  2459. }
  2460. }
  2461. return 0;
  2462. }
  2463. /**
  2464. * i40e_vsi_add_vlan - Add VSI membership for given VLAN
  2465. * @vsi: the VSI being configured
  2466. * @vid: VLAN id to be added
  2467. **/
  2468. int i40e_vsi_add_vlan(struct i40e_vsi *vsi, u16 vid)
  2469. {
  2470. int err;
  2471. if (vsi->info.pvid)
  2472. return -EINVAL;
  2473. /* The network stack will attempt to add VID=0, with the intention to
  2474. * receive priority tagged packets with a VLAN of 0. Our HW receives
  2475. * these packets by default when configured to receive untagged
  2476. * packets, so we don't need to add a filter for this case.
  2477. * Additionally, HW interprets adding a VID=0 filter as meaning to
  2478. * receive *only* tagged traffic and stops receiving untagged traffic.
  2479. * Thus, we do not want to actually add a filter for VID=0
  2480. */
  2481. if (!vid)
  2482. return 0;
  2483. /* Locked once because all functions invoked below iterates list*/
  2484. spin_lock_bh(&vsi->mac_filter_hash_lock);
  2485. err = i40e_add_vlan_all_mac(vsi, vid);
  2486. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2487. if (err)
  2488. return err;
  2489. /* schedule our worker thread which will take care of
  2490. * applying the new filter changes
  2491. */
  2492. i40e_service_event_schedule(vsi->back);
  2493. return 0;
  2494. }
  2495. /**
  2496. * i40e_rm_vlan_all_mac - Remove MAC/VLAN pair for all MAC with the given VLAN
  2497. * @vsi: the vsi being configured
  2498. * @vid: vlan id to be removed (0 = untagged only , -1 = any)
  2499. *
  2500. * This function should be used to remove all VLAN filters which match the
  2501. * given VID. It does not schedule the service event and does not take the
  2502. * mac_filter_hash_lock so it may be combined with other operations under
  2503. * a single invocation of the mac_filter_hash_lock.
  2504. *
  2505. * NOTE: this function expects to be called while under the
  2506. * mac_filter_hash_lock
  2507. */
  2508. void i40e_rm_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
  2509. {
  2510. struct i40e_mac_filter *f;
  2511. struct hlist_node *h;
  2512. int bkt;
  2513. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  2514. if (f->vlan == vid)
  2515. __i40e_del_filter(vsi, f);
  2516. }
  2517. }
  2518. /**
  2519. * i40e_vsi_kill_vlan - Remove VSI membership for given VLAN
  2520. * @vsi: the VSI being configured
  2521. * @vid: VLAN id to be removed
  2522. **/
  2523. void i40e_vsi_kill_vlan(struct i40e_vsi *vsi, u16 vid)
  2524. {
  2525. if (!vid || vsi->info.pvid)
  2526. return;
  2527. spin_lock_bh(&vsi->mac_filter_hash_lock);
  2528. i40e_rm_vlan_all_mac(vsi, vid);
  2529. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2530. /* schedule our worker thread which will take care of
  2531. * applying the new filter changes
  2532. */
  2533. i40e_service_event_schedule(vsi->back);
  2534. }
  2535. /**
  2536. * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
  2537. * @netdev: network interface to be adjusted
  2538. * @vid: vlan id to be added
  2539. *
  2540. * net_device_ops implementation for adding vlan ids
  2541. **/
  2542. static int i40e_vlan_rx_add_vid(struct net_device *netdev,
  2543. __always_unused __be16 proto, u16 vid)
  2544. {
  2545. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2546. struct i40e_vsi *vsi = np->vsi;
  2547. int ret = 0;
  2548. if (vid >= VLAN_N_VID)
  2549. return -EINVAL;
  2550. ret = i40e_vsi_add_vlan(vsi, vid);
  2551. if (!ret)
  2552. set_bit(vid, vsi->active_vlans);
  2553. return ret;
  2554. }
  2555. /**
  2556. * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
  2557. * @netdev: network interface to be adjusted
  2558. * @vid: vlan id to be removed
  2559. *
  2560. * net_device_ops implementation for removing vlan ids
  2561. **/
  2562. static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
  2563. __always_unused __be16 proto, u16 vid)
  2564. {
  2565. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2566. struct i40e_vsi *vsi = np->vsi;
  2567. /* return code is ignored as there is nothing a user
  2568. * can do about failure to remove and a log message was
  2569. * already printed from the other function
  2570. */
  2571. i40e_vsi_kill_vlan(vsi, vid);
  2572. clear_bit(vid, vsi->active_vlans);
  2573. return 0;
  2574. }
  2575. /**
  2576. * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
  2577. * @vsi: the vsi being brought back up
  2578. **/
  2579. static void i40e_restore_vlan(struct i40e_vsi *vsi)
  2580. {
  2581. u16 vid;
  2582. if (!vsi->netdev)
  2583. return;
  2584. if (vsi->netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
  2585. i40e_vlan_stripping_enable(vsi);
  2586. else
  2587. i40e_vlan_stripping_disable(vsi);
  2588. for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
  2589. i40e_vlan_rx_add_vid(vsi->netdev, htons(ETH_P_8021Q),
  2590. vid);
  2591. }
  2592. /**
  2593. * i40e_vsi_add_pvid - Add pvid for the VSI
  2594. * @vsi: the vsi being adjusted
  2595. * @vid: the vlan id to set as a PVID
  2596. **/
  2597. int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
  2598. {
  2599. struct i40e_vsi_context ctxt;
  2600. i40e_status ret;
  2601. vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
  2602. vsi->info.pvid = cpu_to_le16(vid);
  2603. vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
  2604. I40E_AQ_VSI_PVLAN_INSERT_PVID |
  2605. I40E_AQ_VSI_PVLAN_EMOD_STR;
  2606. ctxt.seid = vsi->seid;
  2607. ctxt.info = vsi->info;
  2608. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  2609. if (ret) {
  2610. dev_info(&vsi->back->pdev->dev,
  2611. "add pvid failed, err %s aq_err %s\n",
  2612. i40e_stat_str(&vsi->back->hw, ret),
  2613. i40e_aq_str(&vsi->back->hw,
  2614. vsi->back->hw.aq.asq_last_status));
  2615. return -ENOENT;
  2616. }
  2617. return 0;
  2618. }
  2619. /**
  2620. * i40e_vsi_remove_pvid - Remove the pvid from the VSI
  2621. * @vsi: the vsi being adjusted
  2622. *
  2623. * Just use the vlan_rx_register() service to put it back to normal
  2624. **/
  2625. void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
  2626. {
  2627. i40e_vlan_stripping_disable(vsi);
  2628. vsi->info.pvid = 0;
  2629. }
  2630. /**
  2631. * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
  2632. * @vsi: ptr to the VSI
  2633. *
  2634. * If this function returns with an error, then it's possible one or
  2635. * more of the rings is populated (while the rest are not). It is the
  2636. * callers duty to clean those orphaned rings.
  2637. *
  2638. * Return 0 on success, negative on failure
  2639. **/
  2640. static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
  2641. {
  2642. int i, err = 0;
  2643. for (i = 0; i < vsi->num_queue_pairs && !err; i++)
  2644. err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
  2645. if (!i40e_enabled_xdp_vsi(vsi))
  2646. return err;
  2647. for (i = 0; i < vsi->num_queue_pairs && !err; i++)
  2648. err = i40e_setup_tx_descriptors(vsi->xdp_rings[i]);
  2649. return err;
  2650. }
  2651. /**
  2652. * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
  2653. * @vsi: ptr to the VSI
  2654. *
  2655. * Free VSI's transmit software resources
  2656. **/
  2657. static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
  2658. {
  2659. int i;
  2660. if (vsi->tx_rings) {
  2661. for (i = 0; i < vsi->num_queue_pairs; i++)
  2662. if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
  2663. i40e_free_tx_resources(vsi->tx_rings[i]);
  2664. }
  2665. if (vsi->xdp_rings) {
  2666. for (i = 0; i < vsi->num_queue_pairs; i++)
  2667. if (vsi->xdp_rings[i] && vsi->xdp_rings[i]->desc)
  2668. i40e_free_tx_resources(vsi->xdp_rings[i]);
  2669. }
  2670. }
  2671. /**
  2672. * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
  2673. * @vsi: ptr to the VSI
  2674. *
  2675. * If this function returns with an error, then it's possible one or
  2676. * more of the rings is populated (while the rest are not). It is the
  2677. * callers duty to clean those orphaned rings.
  2678. *
  2679. * Return 0 on success, negative on failure
  2680. **/
  2681. static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
  2682. {
  2683. int i, err = 0;
  2684. for (i = 0; i < vsi->num_queue_pairs && !err; i++)
  2685. err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
  2686. return err;
  2687. }
  2688. /**
  2689. * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
  2690. * @vsi: ptr to the VSI
  2691. *
  2692. * Free all receive software resources
  2693. **/
  2694. static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
  2695. {
  2696. int i;
  2697. if (!vsi->rx_rings)
  2698. return;
  2699. for (i = 0; i < vsi->num_queue_pairs; i++)
  2700. if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
  2701. i40e_free_rx_resources(vsi->rx_rings[i]);
  2702. }
  2703. /**
  2704. * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
  2705. * @ring: The Tx ring to configure
  2706. *
  2707. * This enables/disables XPS for a given Tx descriptor ring
  2708. * based on the TCs enabled for the VSI that ring belongs to.
  2709. **/
  2710. static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
  2711. {
  2712. int cpu;
  2713. if (!ring->q_vector || !ring->netdev || ring->ch)
  2714. return;
  2715. /* We only initialize XPS once, so as not to overwrite user settings */
  2716. if (test_and_set_bit(__I40E_TX_XPS_INIT_DONE, ring->state))
  2717. return;
  2718. cpu = cpumask_local_spread(ring->q_vector->v_idx, -1);
  2719. netif_set_xps_queue(ring->netdev, get_cpu_mask(cpu),
  2720. ring->queue_index);
  2721. }
  2722. /**
  2723. * i40e_configure_tx_ring - Configure a transmit ring context and rest
  2724. * @ring: The Tx ring to configure
  2725. *
  2726. * Configure the Tx descriptor ring in the HMC context.
  2727. **/
  2728. static int i40e_configure_tx_ring(struct i40e_ring *ring)
  2729. {
  2730. struct i40e_vsi *vsi = ring->vsi;
  2731. u16 pf_q = vsi->base_queue + ring->queue_index;
  2732. struct i40e_hw *hw = &vsi->back->hw;
  2733. struct i40e_hmc_obj_txq tx_ctx;
  2734. i40e_status err = 0;
  2735. u32 qtx_ctl = 0;
  2736. /* some ATR related tx ring init */
  2737. if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
  2738. ring->atr_sample_rate = vsi->back->atr_sample_rate;
  2739. ring->atr_count = 0;
  2740. } else {
  2741. ring->atr_sample_rate = 0;
  2742. }
  2743. /* configure XPS */
  2744. i40e_config_xps_tx_ring(ring);
  2745. /* clear the context structure first */
  2746. memset(&tx_ctx, 0, sizeof(tx_ctx));
  2747. tx_ctx.new_context = 1;
  2748. tx_ctx.base = (ring->dma / 128);
  2749. tx_ctx.qlen = ring->count;
  2750. tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
  2751. I40E_FLAG_FD_ATR_ENABLED));
  2752. tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
  2753. /* FDIR VSI tx ring can still use RS bit and writebacks */
  2754. if (vsi->type != I40E_VSI_FDIR)
  2755. tx_ctx.head_wb_ena = 1;
  2756. tx_ctx.head_wb_addr = ring->dma +
  2757. (ring->count * sizeof(struct i40e_tx_desc));
  2758. /* As part of VSI creation/update, FW allocates certain
  2759. * Tx arbitration queue sets for each TC enabled for
  2760. * the VSI. The FW returns the handles to these queue
  2761. * sets as part of the response buffer to Add VSI,
  2762. * Update VSI, etc. AQ commands. It is expected that
  2763. * these queue set handles be associated with the Tx
  2764. * queues by the driver as part of the TX queue context
  2765. * initialization. This has to be done regardless of
  2766. * DCB as by default everything is mapped to TC0.
  2767. */
  2768. if (ring->ch)
  2769. tx_ctx.rdylist =
  2770. le16_to_cpu(ring->ch->info.qs_handle[ring->dcb_tc]);
  2771. else
  2772. tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
  2773. tx_ctx.rdylist_act = 0;
  2774. /* clear the context in the HMC */
  2775. err = i40e_clear_lan_tx_queue_context(hw, pf_q);
  2776. if (err) {
  2777. dev_info(&vsi->back->pdev->dev,
  2778. "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
  2779. ring->queue_index, pf_q, err);
  2780. return -ENOMEM;
  2781. }
  2782. /* set the context in the HMC */
  2783. err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
  2784. if (err) {
  2785. dev_info(&vsi->back->pdev->dev,
  2786. "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
  2787. ring->queue_index, pf_q, err);
  2788. return -ENOMEM;
  2789. }
  2790. /* Now associate this queue with this PCI function */
  2791. if (ring->ch) {
  2792. if (ring->ch->type == I40E_VSI_VMDQ2)
  2793. qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
  2794. else
  2795. return -EINVAL;
  2796. qtx_ctl |= (ring->ch->vsi_number <<
  2797. I40E_QTX_CTL_VFVM_INDX_SHIFT) &
  2798. I40E_QTX_CTL_VFVM_INDX_MASK;
  2799. } else {
  2800. if (vsi->type == I40E_VSI_VMDQ2) {
  2801. qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
  2802. qtx_ctl |= ((vsi->id) << I40E_QTX_CTL_VFVM_INDX_SHIFT) &
  2803. I40E_QTX_CTL_VFVM_INDX_MASK;
  2804. } else {
  2805. qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
  2806. }
  2807. }
  2808. qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
  2809. I40E_QTX_CTL_PF_INDX_MASK);
  2810. wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
  2811. i40e_flush(hw);
  2812. /* cache tail off for easier writes later */
  2813. ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
  2814. return 0;
  2815. }
  2816. /**
  2817. * i40e_configure_rx_ring - Configure a receive ring context
  2818. * @ring: The Rx ring to configure
  2819. *
  2820. * Configure the Rx descriptor ring in the HMC context.
  2821. **/
  2822. static int i40e_configure_rx_ring(struct i40e_ring *ring)
  2823. {
  2824. struct i40e_vsi *vsi = ring->vsi;
  2825. u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
  2826. u16 pf_q = vsi->base_queue + ring->queue_index;
  2827. struct i40e_hw *hw = &vsi->back->hw;
  2828. struct i40e_hmc_obj_rxq rx_ctx;
  2829. i40e_status err = 0;
  2830. bitmap_zero(ring->state, __I40E_RING_STATE_NBITS);
  2831. /* clear the context structure first */
  2832. memset(&rx_ctx, 0, sizeof(rx_ctx));
  2833. ring->rx_buf_len = vsi->rx_buf_len;
  2834. rx_ctx.dbuff = DIV_ROUND_UP(ring->rx_buf_len,
  2835. BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
  2836. rx_ctx.base = (ring->dma / 128);
  2837. rx_ctx.qlen = ring->count;
  2838. /* use 32 byte descriptors */
  2839. rx_ctx.dsize = 1;
  2840. /* descriptor type is always zero
  2841. * rx_ctx.dtype = 0;
  2842. */
  2843. rx_ctx.hsplit_0 = 0;
  2844. rx_ctx.rxmax = min_t(u16, vsi->max_frame, chain_len * ring->rx_buf_len);
  2845. if (hw->revision_id == 0)
  2846. rx_ctx.lrxqthresh = 0;
  2847. else
  2848. rx_ctx.lrxqthresh = 1;
  2849. rx_ctx.crcstrip = 1;
  2850. rx_ctx.l2tsel = 1;
  2851. /* this controls whether VLAN is stripped from inner headers */
  2852. rx_ctx.showiv = 0;
  2853. /* set the prefena field to 1 because the manual says to */
  2854. rx_ctx.prefena = 1;
  2855. /* clear the context in the HMC */
  2856. err = i40e_clear_lan_rx_queue_context(hw, pf_q);
  2857. if (err) {
  2858. dev_info(&vsi->back->pdev->dev,
  2859. "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
  2860. ring->queue_index, pf_q, err);
  2861. return -ENOMEM;
  2862. }
  2863. /* set the context in the HMC */
  2864. err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
  2865. if (err) {
  2866. dev_info(&vsi->back->pdev->dev,
  2867. "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
  2868. ring->queue_index, pf_q, err);
  2869. return -ENOMEM;
  2870. }
  2871. /* configure Rx buffer alignment */
  2872. if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
  2873. clear_ring_build_skb_enabled(ring);
  2874. else
  2875. set_ring_build_skb_enabled(ring);
  2876. /* cache tail for quicker writes, and clear the reg before use */
  2877. ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
  2878. writel(0, ring->tail);
  2879. i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
  2880. return 0;
  2881. }
  2882. /**
  2883. * i40e_vsi_configure_tx - Configure the VSI for Tx
  2884. * @vsi: VSI structure describing this set of rings and resources
  2885. *
  2886. * Configure the Tx VSI for operation.
  2887. **/
  2888. static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
  2889. {
  2890. int err = 0;
  2891. u16 i;
  2892. for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
  2893. err = i40e_configure_tx_ring(vsi->tx_rings[i]);
  2894. if (!i40e_enabled_xdp_vsi(vsi))
  2895. return err;
  2896. for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
  2897. err = i40e_configure_tx_ring(vsi->xdp_rings[i]);
  2898. return err;
  2899. }
  2900. /**
  2901. * i40e_vsi_configure_rx - Configure the VSI for Rx
  2902. * @vsi: the VSI being configured
  2903. *
  2904. * Configure the Rx VSI for operation.
  2905. **/
  2906. static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
  2907. {
  2908. int err = 0;
  2909. u16 i;
  2910. if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX)) {
  2911. vsi->max_frame = I40E_MAX_RXBUFFER;
  2912. vsi->rx_buf_len = I40E_RXBUFFER_2048;
  2913. #if (PAGE_SIZE < 8192)
  2914. } else if (!I40E_2K_TOO_SMALL_WITH_PADDING &&
  2915. (vsi->netdev->mtu <= ETH_DATA_LEN)) {
  2916. vsi->max_frame = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
  2917. vsi->rx_buf_len = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
  2918. #endif
  2919. } else {
  2920. vsi->max_frame = I40E_MAX_RXBUFFER;
  2921. vsi->rx_buf_len = (PAGE_SIZE < 8192) ? I40E_RXBUFFER_3072 :
  2922. I40E_RXBUFFER_2048;
  2923. }
  2924. /* set up individual rings */
  2925. for (i = 0; i < vsi->num_queue_pairs && !err; i++)
  2926. err = i40e_configure_rx_ring(vsi->rx_rings[i]);
  2927. return err;
  2928. }
  2929. /**
  2930. * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
  2931. * @vsi: ptr to the VSI
  2932. **/
  2933. static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
  2934. {
  2935. struct i40e_ring *tx_ring, *rx_ring;
  2936. u16 qoffset, qcount;
  2937. int i, n;
  2938. if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
  2939. /* Reset the TC information */
  2940. for (i = 0; i < vsi->num_queue_pairs; i++) {
  2941. rx_ring = vsi->rx_rings[i];
  2942. tx_ring = vsi->tx_rings[i];
  2943. rx_ring->dcb_tc = 0;
  2944. tx_ring->dcb_tc = 0;
  2945. }
  2946. return;
  2947. }
  2948. for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
  2949. if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
  2950. continue;
  2951. qoffset = vsi->tc_config.tc_info[n].qoffset;
  2952. qcount = vsi->tc_config.tc_info[n].qcount;
  2953. for (i = qoffset; i < (qoffset + qcount); i++) {
  2954. rx_ring = vsi->rx_rings[i];
  2955. tx_ring = vsi->tx_rings[i];
  2956. rx_ring->dcb_tc = n;
  2957. tx_ring->dcb_tc = n;
  2958. }
  2959. }
  2960. }
  2961. /**
  2962. * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
  2963. * @vsi: ptr to the VSI
  2964. **/
  2965. static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
  2966. {
  2967. if (vsi->netdev)
  2968. i40e_set_rx_mode(vsi->netdev);
  2969. }
  2970. /**
  2971. * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
  2972. * @vsi: Pointer to the targeted VSI
  2973. *
  2974. * This function replays the hlist on the hw where all the SB Flow Director
  2975. * filters were saved.
  2976. **/
  2977. static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
  2978. {
  2979. struct i40e_fdir_filter *filter;
  2980. struct i40e_pf *pf = vsi->back;
  2981. struct hlist_node *node;
  2982. if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
  2983. return;
  2984. /* Reset FDir counters as we're replaying all existing filters */
  2985. pf->fd_tcp4_filter_cnt = 0;
  2986. pf->fd_udp4_filter_cnt = 0;
  2987. pf->fd_sctp4_filter_cnt = 0;
  2988. pf->fd_ip4_filter_cnt = 0;
  2989. hlist_for_each_entry_safe(filter, node,
  2990. &pf->fdir_filter_list, fdir_node) {
  2991. i40e_add_del_fdir(vsi, filter, true);
  2992. }
  2993. }
  2994. /**
  2995. * i40e_vsi_configure - Set up the VSI for action
  2996. * @vsi: the VSI being configured
  2997. **/
  2998. static int i40e_vsi_configure(struct i40e_vsi *vsi)
  2999. {
  3000. int err;
  3001. i40e_set_vsi_rx_mode(vsi);
  3002. i40e_restore_vlan(vsi);
  3003. i40e_vsi_config_dcb_rings(vsi);
  3004. err = i40e_vsi_configure_tx(vsi);
  3005. if (!err)
  3006. err = i40e_vsi_configure_rx(vsi);
  3007. return err;
  3008. }
  3009. /**
  3010. * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
  3011. * @vsi: the VSI being configured
  3012. **/
  3013. static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
  3014. {
  3015. bool has_xdp = i40e_enabled_xdp_vsi(vsi);
  3016. struct i40e_pf *pf = vsi->back;
  3017. struct i40e_hw *hw = &pf->hw;
  3018. u16 vector;
  3019. int i, q;
  3020. u32 qp;
  3021. /* The interrupt indexing is offset by 1 in the PFINT_ITRn
  3022. * and PFINT_LNKLSTn registers, e.g.:
  3023. * PFINT_ITRn[0..n-1] gets msix-1..msix-n (qpair interrupts)
  3024. */
  3025. qp = vsi->base_queue;
  3026. vector = vsi->base_vector;
  3027. for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
  3028. struct i40e_q_vector *q_vector = vsi->q_vectors[i];
  3029. q_vector->rx.next_update = jiffies + 1;
  3030. q_vector->rx.target_itr =
  3031. ITR_TO_REG(vsi->rx_rings[i]->itr_setting);
  3032. wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
  3033. q_vector->rx.target_itr);
  3034. q_vector->rx.current_itr = q_vector->rx.target_itr;
  3035. q_vector->tx.next_update = jiffies + 1;
  3036. q_vector->tx.target_itr =
  3037. ITR_TO_REG(vsi->tx_rings[i]->itr_setting);
  3038. wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
  3039. q_vector->tx.target_itr);
  3040. q_vector->tx.current_itr = q_vector->tx.target_itr;
  3041. wr32(hw, I40E_PFINT_RATEN(vector - 1),
  3042. i40e_intrl_usec_to_reg(vsi->int_rate_limit));
  3043. /* Linked list for the queuepairs assigned to this vector */
  3044. wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
  3045. for (q = 0; q < q_vector->num_ringpairs; q++) {
  3046. u32 nextqp = has_xdp ? qp + vsi->alloc_queue_pairs : qp;
  3047. u32 val;
  3048. val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
  3049. (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
  3050. (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
  3051. (nextqp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) |
  3052. (I40E_QUEUE_TYPE_TX <<
  3053. I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
  3054. wr32(hw, I40E_QINT_RQCTL(qp), val);
  3055. if (has_xdp) {
  3056. val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  3057. (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
  3058. (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
  3059. (qp << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
  3060. (I40E_QUEUE_TYPE_TX <<
  3061. I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
  3062. wr32(hw, I40E_QINT_TQCTL(nextqp), val);
  3063. }
  3064. val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  3065. (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
  3066. (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
  3067. ((qp + 1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
  3068. (I40E_QUEUE_TYPE_RX <<
  3069. I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
  3070. /* Terminate the linked list */
  3071. if (q == (q_vector->num_ringpairs - 1))
  3072. val |= (I40E_QUEUE_END_OF_LIST <<
  3073. I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
  3074. wr32(hw, I40E_QINT_TQCTL(qp), val);
  3075. qp++;
  3076. }
  3077. }
  3078. i40e_flush(hw);
  3079. }
  3080. /**
  3081. * i40e_enable_misc_int_causes - enable the non-queue interrupts
  3082. * @hw: ptr to the hardware info
  3083. **/
  3084. static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
  3085. {
  3086. struct i40e_hw *hw = &pf->hw;
  3087. u32 val;
  3088. /* clear things first */
  3089. wr32(hw, I40E_PFINT_ICR0_ENA, 0); /* disable all */
  3090. rd32(hw, I40E_PFINT_ICR0); /* read to clear */
  3091. val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK |
  3092. I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK |
  3093. I40E_PFINT_ICR0_ENA_GRST_MASK |
  3094. I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
  3095. I40E_PFINT_ICR0_ENA_GPIO_MASK |
  3096. I40E_PFINT_ICR0_ENA_HMC_ERR_MASK |
  3097. I40E_PFINT_ICR0_ENA_VFLR_MASK |
  3098. I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
  3099. if (pf->flags & I40E_FLAG_IWARP_ENABLED)
  3100. val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
  3101. if (pf->flags & I40E_FLAG_PTP)
  3102. val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
  3103. wr32(hw, I40E_PFINT_ICR0_ENA, val);
  3104. /* SW_ITR_IDX = 0, but don't change INTENA */
  3105. wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
  3106. I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
  3107. /* OTHER_ITR_IDX = 0 */
  3108. wr32(hw, I40E_PFINT_STAT_CTL0, 0);
  3109. }
  3110. /**
  3111. * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
  3112. * @vsi: the VSI being configured
  3113. **/
  3114. static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
  3115. {
  3116. u32 nextqp = i40e_enabled_xdp_vsi(vsi) ? vsi->alloc_queue_pairs : 0;
  3117. struct i40e_q_vector *q_vector = vsi->q_vectors[0];
  3118. struct i40e_pf *pf = vsi->back;
  3119. struct i40e_hw *hw = &pf->hw;
  3120. u32 val;
  3121. /* set the ITR configuration */
  3122. q_vector->rx.next_update = jiffies + 1;
  3123. q_vector->rx.target_itr = ITR_TO_REG(vsi->rx_rings[0]->itr_setting);
  3124. wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.target_itr);
  3125. q_vector->rx.current_itr = q_vector->rx.target_itr;
  3126. q_vector->tx.next_update = jiffies + 1;
  3127. q_vector->tx.target_itr = ITR_TO_REG(vsi->tx_rings[0]->itr_setting);
  3128. wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.target_itr);
  3129. q_vector->tx.current_itr = q_vector->tx.target_itr;
  3130. i40e_enable_misc_int_causes(pf);
  3131. /* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
  3132. wr32(hw, I40E_PFINT_LNKLST0, 0);
  3133. /* Associate the queue pair to the vector and enable the queue int */
  3134. val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
  3135. (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
  3136. (nextqp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
  3137. (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
  3138. wr32(hw, I40E_QINT_RQCTL(0), val);
  3139. if (i40e_enabled_xdp_vsi(vsi)) {
  3140. val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  3141. (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT)|
  3142. (I40E_QUEUE_TYPE_TX
  3143. << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
  3144. wr32(hw, I40E_QINT_TQCTL(nextqp), val);
  3145. }
  3146. val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  3147. (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
  3148. (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
  3149. wr32(hw, I40E_QINT_TQCTL(0), val);
  3150. i40e_flush(hw);
  3151. }
  3152. /**
  3153. * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
  3154. * @pf: board private structure
  3155. **/
  3156. void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
  3157. {
  3158. struct i40e_hw *hw = &pf->hw;
  3159. wr32(hw, I40E_PFINT_DYN_CTL0,
  3160. I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
  3161. i40e_flush(hw);
  3162. }
  3163. /**
  3164. * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
  3165. * @pf: board private structure
  3166. **/
  3167. void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf)
  3168. {
  3169. struct i40e_hw *hw = &pf->hw;
  3170. u32 val;
  3171. val = I40E_PFINT_DYN_CTL0_INTENA_MASK |
  3172. I40E_PFINT_DYN_CTL0_CLEARPBA_MASK |
  3173. (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
  3174. wr32(hw, I40E_PFINT_DYN_CTL0, val);
  3175. i40e_flush(hw);
  3176. }
  3177. /**
  3178. * i40e_msix_clean_rings - MSIX mode Interrupt Handler
  3179. * @irq: interrupt number
  3180. * @data: pointer to a q_vector
  3181. **/
  3182. static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
  3183. {
  3184. struct i40e_q_vector *q_vector = data;
  3185. if (!q_vector->tx.ring && !q_vector->rx.ring)
  3186. return IRQ_HANDLED;
  3187. napi_schedule_irqoff(&q_vector->napi);
  3188. return IRQ_HANDLED;
  3189. }
  3190. /**
  3191. * i40e_irq_affinity_notify - Callback for affinity changes
  3192. * @notify: context as to what irq was changed
  3193. * @mask: the new affinity mask
  3194. *
  3195. * This is a callback function used by the irq_set_affinity_notifier function
  3196. * so that we may register to receive changes to the irq affinity masks.
  3197. **/
  3198. static void i40e_irq_affinity_notify(struct irq_affinity_notify *notify,
  3199. const cpumask_t *mask)
  3200. {
  3201. struct i40e_q_vector *q_vector =
  3202. container_of(notify, struct i40e_q_vector, affinity_notify);
  3203. cpumask_copy(&q_vector->affinity_mask, mask);
  3204. }
  3205. /**
  3206. * i40e_irq_affinity_release - Callback for affinity notifier release
  3207. * @ref: internal core kernel usage
  3208. *
  3209. * This is a callback function used by the irq_set_affinity_notifier function
  3210. * to inform the current notification subscriber that they will no longer
  3211. * receive notifications.
  3212. **/
  3213. static void i40e_irq_affinity_release(struct kref *ref) {}
  3214. /**
  3215. * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
  3216. * @vsi: the VSI being configured
  3217. * @basename: name for the vector
  3218. *
  3219. * Allocates MSI-X vectors and requests interrupts from the kernel.
  3220. **/
  3221. static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
  3222. {
  3223. int q_vectors = vsi->num_q_vectors;
  3224. struct i40e_pf *pf = vsi->back;
  3225. int base = vsi->base_vector;
  3226. int rx_int_idx = 0;
  3227. int tx_int_idx = 0;
  3228. int vector, err;
  3229. int irq_num;
  3230. int cpu;
  3231. for (vector = 0; vector < q_vectors; vector++) {
  3232. struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
  3233. irq_num = pf->msix_entries[base + vector].vector;
  3234. if (q_vector->tx.ring && q_vector->rx.ring) {
  3235. snprintf(q_vector->name, sizeof(q_vector->name) - 1,
  3236. "%s-%s-%d", basename, "TxRx", rx_int_idx++);
  3237. tx_int_idx++;
  3238. } else if (q_vector->rx.ring) {
  3239. snprintf(q_vector->name, sizeof(q_vector->name) - 1,
  3240. "%s-%s-%d", basename, "rx", rx_int_idx++);
  3241. } else if (q_vector->tx.ring) {
  3242. snprintf(q_vector->name, sizeof(q_vector->name) - 1,
  3243. "%s-%s-%d", basename, "tx", tx_int_idx++);
  3244. } else {
  3245. /* skip this unused q_vector */
  3246. continue;
  3247. }
  3248. err = request_irq(irq_num,
  3249. vsi->irq_handler,
  3250. 0,
  3251. q_vector->name,
  3252. q_vector);
  3253. if (err) {
  3254. dev_info(&pf->pdev->dev,
  3255. "MSIX request_irq failed, error: %d\n", err);
  3256. goto free_queue_irqs;
  3257. }
  3258. /* register for affinity change notifications */
  3259. q_vector->affinity_notify.notify = i40e_irq_affinity_notify;
  3260. q_vector->affinity_notify.release = i40e_irq_affinity_release;
  3261. irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
  3262. /* Spread affinity hints out across online CPUs.
  3263. *
  3264. * get_cpu_mask returns a static constant mask with
  3265. * a permanent lifetime so it's ok to pass to
  3266. * irq_set_affinity_hint without making a copy.
  3267. */
  3268. cpu = cpumask_local_spread(q_vector->v_idx, -1);
  3269. irq_set_affinity_hint(irq_num, get_cpu_mask(cpu));
  3270. }
  3271. vsi->irqs_ready = true;
  3272. return 0;
  3273. free_queue_irqs:
  3274. while (vector) {
  3275. vector--;
  3276. irq_num = pf->msix_entries[base + vector].vector;
  3277. irq_set_affinity_notifier(irq_num, NULL);
  3278. irq_set_affinity_hint(irq_num, NULL);
  3279. free_irq(irq_num, &vsi->q_vectors[vector]);
  3280. }
  3281. return err;
  3282. }
  3283. /**
  3284. * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
  3285. * @vsi: the VSI being un-configured
  3286. **/
  3287. static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
  3288. {
  3289. struct i40e_pf *pf = vsi->back;
  3290. struct i40e_hw *hw = &pf->hw;
  3291. int base = vsi->base_vector;
  3292. int i;
  3293. /* disable interrupt causation from each queue */
  3294. for (i = 0; i < vsi->num_queue_pairs; i++) {
  3295. u32 val;
  3296. val = rd32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx));
  3297. val &= ~I40E_QINT_TQCTL_CAUSE_ENA_MASK;
  3298. wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), val);
  3299. val = rd32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx));
  3300. val &= ~I40E_QINT_RQCTL_CAUSE_ENA_MASK;
  3301. wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), val);
  3302. if (!i40e_enabled_xdp_vsi(vsi))
  3303. continue;
  3304. wr32(hw, I40E_QINT_TQCTL(vsi->xdp_rings[i]->reg_idx), 0);
  3305. }
  3306. /* disable each interrupt */
  3307. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  3308. for (i = vsi->base_vector;
  3309. i < (vsi->num_q_vectors + vsi->base_vector); i++)
  3310. wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
  3311. i40e_flush(hw);
  3312. for (i = 0; i < vsi->num_q_vectors; i++)
  3313. synchronize_irq(pf->msix_entries[i + base].vector);
  3314. } else {
  3315. /* Legacy and MSI mode - this stops all interrupt handling */
  3316. wr32(hw, I40E_PFINT_ICR0_ENA, 0);
  3317. wr32(hw, I40E_PFINT_DYN_CTL0, 0);
  3318. i40e_flush(hw);
  3319. synchronize_irq(pf->pdev->irq);
  3320. }
  3321. }
  3322. /**
  3323. * i40e_vsi_enable_irq - Enable IRQ for the given VSI
  3324. * @vsi: the VSI being configured
  3325. **/
  3326. static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
  3327. {
  3328. struct i40e_pf *pf = vsi->back;
  3329. int i;
  3330. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  3331. for (i = 0; i < vsi->num_q_vectors; i++)
  3332. i40e_irq_dynamic_enable(vsi, i);
  3333. } else {
  3334. i40e_irq_dynamic_enable_icr0(pf);
  3335. }
  3336. i40e_flush(&pf->hw);
  3337. return 0;
  3338. }
  3339. /**
  3340. * i40e_free_misc_vector - Free the vector that handles non-queue events
  3341. * @pf: board private structure
  3342. **/
  3343. static void i40e_free_misc_vector(struct i40e_pf *pf)
  3344. {
  3345. /* Disable ICR 0 */
  3346. wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
  3347. i40e_flush(&pf->hw);
  3348. if (pf->flags & I40E_FLAG_MSIX_ENABLED && pf->msix_entries) {
  3349. synchronize_irq(pf->msix_entries[0].vector);
  3350. free_irq(pf->msix_entries[0].vector, pf);
  3351. clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
  3352. }
  3353. }
  3354. /**
  3355. * i40e_intr - MSI/Legacy and non-queue interrupt handler
  3356. * @irq: interrupt number
  3357. * @data: pointer to a q_vector
  3358. *
  3359. * This is the handler used for all MSI/Legacy interrupts, and deals
  3360. * with both queue and non-queue interrupts. This is also used in
  3361. * MSIX mode to handle the non-queue interrupts.
  3362. **/
  3363. static irqreturn_t i40e_intr(int irq, void *data)
  3364. {
  3365. struct i40e_pf *pf = (struct i40e_pf *)data;
  3366. struct i40e_hw *hw = &pf->hw;
  3367. irqreturn_t ret = IRQ_NONE;
  3368. u32 icr0, icr0_remaining;
  3369. u32 val, ena_mask;
  3370. icr0 = rd32(hw, I40E_PFINT_ICR0);
  3371. ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
  3372. /* if sharing a legacy IRQ, we might get called w/o an intr pending */
  3373. if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
  3374. goto enable_intr;
  3375. /* if interrupt but no bits showing, must be SWINT */
  3376. if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
  3377. (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
  3378. pf->sw_int_count++;
  3379. if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
  3380. (icr0 & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
  3381. ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
  3382. dev_dbg(&pf->pdev->dev, "cleared PE_CRITERR\n");
  3383. set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
  3384. }
  3385. /* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
  3386. if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
  3387. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  3388. struct i40e_q_vector *q_vector = vsi->q_vectors[0];
  3389. /* We do not have a way to disarm Queue causes while leaving
  3390. * interrupt enabled for all other causes, ideally
  3391. * interrupt should be disabled while we are in NAPI but
  3392. * this is not a performance path and napi_schedule()
  3393. * can deal with rescheduling.
  3394. */
  3395. if (!test_bit(__I40E_DOWN, pf->state))
  3396. napi_schedule_irqoff(&q_vector->napi);
  3397. }
  3398. if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
  3399. ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
  3400. set_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
  3401. i40e_debug(&pf->hw, I40E_DEBUG_NVM, "AdminQ event\n");
  3402. }
  3403. if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
  3404. ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
  3405. set_bit(__I40E_MDD_EVENT_PENDING, pf->state);
  3406. }
  3407. if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
  3408. ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
  3409. set_bit(__I40E_VFLR_EVENT_PENDING, pf->state);
  3410. }
  3411. if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
  3412. if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
  3413. set_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
  3414. ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
  3415. val = rd32(hw, I40E_GLGEN_RSTAT);
  3416. val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
  3417. >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
  3418. if (val == I40E_RESET_CORER) {
  3419. pf->corer_count++;
  3420. } else if (val == I40E_RESET_GLOBR) {
  3421. pf->globr_count++;
  3422. } else if (val == I40E_RESET_EMPR) {
  3423. pf->empr_count++;
  3424. set_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state);
  3425. }
  3426. }
  3427. if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
  3428. icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
  3429. dev_info(&pf->pdev->dev, "HMC error interrupt\n");
  3430. dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
  3431. rd32(hw, I40E_PFHMC_ERRORINFO),
  3432. rd32(hw, I40E_PFHMC_ERRORDATA));
  3433. }
  3434. if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
  3435. u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
  3436. if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK) {
  3437. icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
  3438. i40e_ptp_tx_hwtstamp(pf);
  3439. }
  3440. }
  3441. /* If a critical error is pending we have no choice but to reset the
  3442. * device.
  3443. * Report and mask out any remaining unexpected interrupts.
  3444. */
  3445. icr0_remaining = icr0 & ena_mask;
  3446. if (icr0_remaining) {
  3447. dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
  3448. icr0_remaining);
  3449. if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
  3450. (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
  3451. (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
  3452. dev_info(&pf->pdev->dev, "device will be reset\n");
  3453. set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
  3454. i40e_service_event_schedule(pf);
  3455. }
  3456. ena_mask &= ~icr0_remaining;
  3457. }
  3458. ret = IRQ_HANDLED;
  3459. enable_intr:
  3460. /* re-enable interrupt causes */
  3461. wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
  3462. if (!test_bit(__I40E_DOWN, pf->state)) {
  3463. i40e_service_event_schedule(pf);
  3464. i40e_irq_dynamic_enable_icr0(pf);
  3465. }
  3466. return ret;
  3467. }
  3468. /**
  3469. * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
  3470. * @tx_ring: tx ring to clean
  3471. * @budget: how many cleans we're allowed
  3472. *
  3473. * Returns true if there's any budget left (e.g. the clean is finished)
  3474. **/
  3475. static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
  3476. {
  3477. struct i40e_vsi *vsi = tx_ring->vsi;
  3478. u16 i = tx_ring->next_to_clean;
  3479. struct i40e_tx_buffer *tx_buf;
  3480. struct i40e_tx_desc *tx_desc;
  3481. tx_buf = &tx_ring->tx_bi[i];
  3482. tx_desc = I40E_TX_DESC(tx_ring, i);
  3483. i -= tx_ring->count;
  3484. do {
  3485. struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
  3486. /* if next_to_watch is not set then there is no work pending */
  3487. if (!eop_desc)
  3488. break;
  3489. /* prevent any other reads prior to eop_desc */
  3490. smp_rmb();
  3491. /* if the descriptor isn't done, no work yet to do */
  3492. if (!(eop_desc->cmd_type_offset_bsz &
  3493. cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
  3494. break;
  3495. /* clear next_to_watch to prevent false hangs */
  3496. tx_buf->next_to_watch = NULL;
  3497. tx_desc->buffer_addr = 0;
  3498. tx_desc->cmd_type_offset_bsz = 0;
  3499. /* move past filter desc */
  3500. tx_buf++;
  3501. tx_desc++;
  3502. i++;
  3503. if (unlikely(!i)) {
  3504. i -= tx_ring->count;
  3505. tx_buf = tx_ring->tx_bi;
  3506. tx_desc = I40E_TX_DESC(tx_ring, 0);
  3507. }
  3508. /* unmap skb header data */
  3509. dma_unmap_single(tx_ring->dev,
  3510. dma_unmap_addr(tx_buf, dma),
  3511. dma_unmap_len(tx_buf, len),
  3512. DMA_TO_DEVICE);
  3513. if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
  3514. kfree(tx_buf->raw_buf);
  3515. tx_buf->raw_buf = NULL;
  3516. tx_buf->tx_flags = 0;
  3517. tx_buf->next_to_watch = NULL;
  3518. dma_unmap_len_set(tx_buf, len, 0);
  3519. tx_desc->buffer_addr = 0;
  3520. tx_desc->cmd_type_offset_bsz = 0;
  3521. /* move us past the eop_desc for start of next FD desc */
  3522. tx_buf++;
  3523. tx_desc++;
  3524. i++;
  3525. if (unlikely(!i)) {
  3526. i -= tx_ring->count;
  3527. tx_buf = tx_ring->tx_bi;
  3528. tx_desc = I40E_TX_DESC(tx_ring, 0);
  3529. }
  3530. /* update budget accounting */
  3531. budget--;
  3532. } while (likely(budget));
  3533. i += tx_ring->count;
  3534. tx_ring->next_to_clean = i;
  3535. if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED)
  3536. i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
  3537. return budget > 0;
  3538. }
  3539. /**
  3540. * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
  3541. * @irq: interrupt number
  3542. * @data: pointer to a q_vector
  3543. **/
  3544. static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
  3545. {
  3546. struct i40e_q_vector *q_vector = data;
  3547. struct i40e_vsi *vsi;
  3548. if (!q_vector->tx.ring)
  3549. return IRQ_HANDLED;
  3550. vsi = q_vector->tx.ring->vsi;
  3551. i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
  3552. return IRQ_HANDLED;
  3553. }
  3554. /**
  3555. * i40e_map_vector_to_qp - Assigns the queue pair to the vector
  3556. * @vsi: the VSI being configured
  3557. * @v_idx: vector index
  3558. * @qp_idx: queue pair index
  3559. **/
  3560. static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
  3561. {
  3562. struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
  3563. struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
  3564. struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
  3565. tx_ring->q_vector = q_vector;
  3566. tx_ring->next = q_vector->tx.ring;
  3567. q_vector->tx.ring = tx_ring;
  3568. q_vector->tx.count++;
  3569. /* Place XDP Tx ring in the same q_vector ring list as regular Tx */
  3570. if (i40e_enabled_xdp_vsi(vsi)) {
  3571. struct i40e_ring *xdp_ring = vsi->xdp_rings[qp_idx];
  3572. xdp_ring->q_vector = q_vector;
  3573. xdp_ring->next = q_vector->tx.ring;
  3574. q_vector->tx.ring = xdp_ring;
  3575. q_vector->tx.count++;
  3576. }
  3577. rx_ring->q_vector = q_vector;
  3578. rx_ring->next = q_vector->rx.ring;
  3579. q_vector->rx.ring = rx_ring;
  3580. q_vector->rx.count++;
  3581. }
  3582. /**
  3583. * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
  3584. * @vsi: the VSI being configured
  3585. *
  3586. * This function maps descriptor rings to the queue-specific vectors
  3587. * we were allotted through the MSI-X enabling code. Ideally, we'd have
  3588. * one vector per queue pair, but on a constrained vector budget, we
  3589. * group the queue pairs as "efficiently" as possible.
  3590. **/
  3591. static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
  3592. {
  3593. int qp_remaining = vsi->num_queue_pairs;
  3594. int q_vectors = vsi->num_q_vectors;
  3595. int num_ringpairs;
  3596. int v_start = 0;
  3597. int qp_idx = 0;
  3598. /* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
  3599. * group them so there are multiple queues per vector.
  3600. * It is also important to go through all the vectors available to be
  3601. * sure that if we don't use all the vectors, that the remaining vectors
  3602. * are cleared. This is especially important when decreasing the
  3603. * number of queues in use.
  3604. */
  3605. for (; v_start < q_vectors; v_start++) {
  3606. struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
  3607. num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
  3608. q_vector->num_ringpairs = num_ringpairs;
  3609. q_vector->reg_idx = q_vector->v_idx + vsi->base_vector - 1;
  3610. q_vector->rx.count = 0;
  3611. q_vector->tx.count = 0;
  3612. q_vector->rx.ring = NULL;
  3613. q_vector->tx.ring = NULL;
  3614. while (num_ringpairs--) {
  3615. i40e_map_vector_to_qp(vsi, v_start, qp_idx);
  3616. qp_idx++;
  3617. qp_remaining--;
  3618. }
  3619. }
  3620. }
  3621. /**
  3622. * i40e_vsi_request_irq - Request IRQ from the OS
  3623. * @vsi: the VSI being configured
  3624. * @basename: name for the vector
  3625. **/
  3626. static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
  3627. {
  3628. struct i40e_pf *pf = vsi->back;
  3629. int err;
  3630. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  3631. err = i40e_vsi_request_irq_msix(vsi, basename);
  3632. else if (pf->flags & I40E_FLAG_MSI_ENABLED)
  3633. err = request_irq(pf->pdev->irq, i40e_intr, 0,
  3634. pf->int_name, pf);
  3635. else
  3636. err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
  3637. pf->int_name, pf);
  3638. if (err)
  3639. dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
  3640. return err;
  3641. }
  3642. #ifdef CONFIG_NET_POLL_CONTROLLER
  3643. /**
  3644. * i40e_netpoll - A Polling 'interrupt' handler
  3645. * @netdev: network interface device structure
  3646. *
  3647. * This is used by netconsole to send skbs without having to re-enable
  3648. * interrupts. It's not called while the normal interrupt routine is executing.
  3649. **/
  3650. static void i40e_netpoll(struct net_device *netdev)
  3651. {
  3652. struct i40e_netdev_priv *np = netdev_priv(netdev);
  3653. struct i40e_vsi *vsi = np->vsi;
  3654. struct i40e_pf *pf = vsi->back;
  3655. int i;
  3656. /* if interface is down do nothing */
  3657. if (test_bit(__I40E_VSI_DOWN, vsi->state))
  3658. return;
  3659. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  3660. for (i = 0; i < vsi->num_q_vectors; i++)
  3661. i40e_msix_clean_rings(0, vsi->q_vectors[i]);
  3662. } else {
  3663. i40e_intr(pf->pdev->irq, netdev);
  3664. }
  3665. }
  3666. #endif
  3667. #define I40E_QTX_ENA_WAIT_COUNT 50
  3668. /**
  3669. * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
  3670. * @pf: the PF being configured
  3671. * @pf_q: the PF queue
  3672. * @enable: enable or disable state of the queue
  3673. *
  3674. * This routine will wait for the given Tx queue of the PF to reach the
  3675. * enabled or disabled state.
  3676. * Returns -ETIMEDOUT in case of failing to reach the requested state after
  3677. * multiple retries; else will return 0 in case of success.
  3678. **/
  3679. static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
  3680. {
  3681. int i;
  3682. u32 tx_reg;
  3683. for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
  3684. tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
  3685. if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
  3686. break;
  3687. usleep_range(10, 20);
  3688. }
  3689. if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
  3690. return -ETIMEDOUT;
  3691. return 0;
  3692. }
  3693. /**
  3694. * i40e_control_tx_q - Start or stop a particular Tx queue
  3695. * @pf: the PF structure
  3696. * @pf_q: the PF queue to configure
  3697. * @enable: start or stop the queue
  3698. *
  3699. * This function enables or disables a single queue. Note that any delay
  3700. * required after the operation is expected to be handled by the caller of
  3701. * this function.
  3702. **/
  3703. static void i40e_control_tx_q(struct i40e_pf *pf, int pf_q, bool enable)
  3704. {
  3705. struct i40e_hw *hw = &pf->hw;
  3706. u32 tx_reg;
  3707. int i;
  3708. /* warn the TX unit of coming changes */
  3709. i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
  3710. if (!enable)
  3711. usleep_range(10, 20);
  3712. for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
  3713. tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
  3714. if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
  3715. ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
  3716. break;
  3717. usleep_range(1000, 2000);
  3718. }
  3719. /* Skip if the queue is already in the requested state */
  3720. if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
  3721. return;
  3722. /* turn on/off the queue */
  3723. if (enable) {
  3724. wr32(hw, I40E_QTX_HEAD(pf_q), 0);
  3725. tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
  3726. } else {
  3727. tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
  3728. }
  3729. wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
  3730. }
  3731. /**
  3732. * i40e_control_wait_tx_q - Start/stop Tx queue and wait for completion
  3733. * @seid: VSI SEID
  3734. * @pf: the PF structure
  3735. * @pf_q: the PF queue to configure
  3736. * @is_xdp: true if the queue is used for XDP
  3737. * @enable: start or stop the queue
  3738. **/
  3739. static int i40e_control_wait_tx_q(int seid, struct i40e_pf *pf, int pf_q,
  3740. bool is_xdp, bool enable)
  3741. {
  3742. int ret;
  3743. i40e_control_tx_q(pf, pf_q, enable);
  3744. /* wait for the change to finish */
  3745. ret = i40e_pf_txq_wait(pf, pf_q, enable);
  3746. if (ret) {
  3747. dev_info(&pf->pdev->dev,
  3748. "VSI seid %d %sTx ring %d %sable timeout\n",
  3749. seid, (is_xdp ? "XDP " : ""), pf_q,
  3750. (enable ? "en" : "dis"));
  3751. }
  3752. return ret;
  3753. }
  3754. /**
  3755. * i40e_vsi_control_tx - Start or stop a VSI's rings
  3756. * @vsi: the VSI being configured
  3757. * @enable: start or stop the rings
  3758. **/
  3759. static int i40e_vsi_control_tx(struct i40e_vsi *vsi, bool enable)
  3760. {
  3761. struct i40e_pf *pf = vsi->back;
  3762. int i, pf_q, ret = 0;
  3763. pf_q = vsi->base_queue;
  3764. for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
  3765. ret = i40e_control_wait_tx_q(vsi->seid, pf,
  3766. pf_q,
  3767. false /*is xdp*/, enable);
  3768. if (ret)
  3769. break;
  3770. if (!i40e_enabled_xdp_vsi(vsi))
  3771. continue;
  3772. ret = i40e_control_wait_tx_q(vsi->seid, pf,
  3773. pf_q + vsi->alloc_queue_pairs,
  3774. true /*is xdp*/, enable);
  3775. if (ret)
  3776. break;
  3777. }
  3778. return ret;
  3779. }
  3780. /**
  3781. * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
  3782. * @pf: the PF being configured
  3783. * @pf_q: the PF queue
  3784. * @enable: enable or disable state of the queue
  3785. *
  3786. * This routine will wait for the given Rx queue of the PF to reach the
  3787. * enabled or disabled state.
  3788. * Returns -ETIMEDOUT in case of failing to reach the requested state after
  3789. * multiple retries; else will return 0 in case of success.
  3790. **/
  3791. static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
  3792. {
  3793. int i;
  3794. u32 rx_reg;
  3795. for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
  3796. rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
  3797. if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
  3798. break;
  3799. usleep_range(10, 20);
  3800. }
  3801. if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
  3802. return -ETIMEDOUT;
  3803. return 0;
  3804. }
  3805. /**
  3806. * i40e_control_rx_q - Start or stop a particular Rx queue
  3807. * @pf: the PF structure
  3808. * @pf_q: the PF queue to configure
  3809. * @enable: start or stop the queue
  3810. *
  3811. * This function enables or disables a single queue. Note that any delay
  3812. * required after the operation is expected to be handled by the caller of
  3813. * this function.
  3814. **/
  3815. static void i40e_control_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
  3816. {
  3817. struct i40e_hw *hw = &pf->hw;
  3818. u32 rx_reg;
  3819. int i;
  3820. for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
  3821. rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
  3822. if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
  3823. ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
  3824. break;
  3825. usleep_range(1000, 2000);
  3826. }
  3827. /* Skip if the queue is already in the requested state */
  3828. if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
  3829. return;
  3830. /* turn on/off the queue */
  3831. if (enable)
  3832. rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
  3833. else
  3834. rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
  3835. wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
  3836. }
  3837. /**
  3838. * i40e_vsi_control_rx - Start or stop a VSI's rings
  3839. * @vsi: the VSI being configured
  3840. * @enable: start or stop the rings
  3841. **/
  3842. static int i40e_vsi_control_rx(struct i40e_vsi *vsi, bool enable)
  3843. {
  3844. struct i40e_pf *pf = vsi->back;
  3845. int i, pf_q, ret = 0;
  3846. pf_q = vsi->base_queue;
  3847. for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
  3848. i40e_control_rx_q(pf, pf_q, enable);
  3849. /* wait for the change to finish */
  3850. ret = i40e_pf_rxq_wait(pf, pf_q, enable);
  3851. if (ret) {
  3852. dev_info(&pf->pdev->dev,
  3853. "VSI seid %d Rx ring %d %sable timeout\n",
  3854. vsi->seid, pf_q, (enable ? "en" : "dis"));
  3855. break;
  3856. }
  3857. }
  3858. /* Due to HW errata, on Rx disable only, the register can indicate done
  3859. * before it really is. Needs 50ms to be sure
  3860. */
  3861. if (!enable)
  3862. mdelay(50);
  3863. return ret;
  3864. }
  3865. /**
  3866. * i40e_vsi_start_rings - Start a VSI's rings
  3867. * @vsi: the VSI being configured
  3868. **/
  3869. int i40e_vsi_start_rings(struct i40e_vsi *vsi)
  3870. {
  3871. int ret = 0;
  3872. /* do rx first for enable and last for disable */
  3873. ret = i40e_vsi_control_rx(vsi, true);
  3874. if (ret)
  3875. return ret;
  3876. ret = i40e_vsi_control_tx(vsi, true);
  3877. return ret;
  3878. }
  3879. /**
  3880. * i40e_vsi_stop_rings - Stop a VSI's rings
  3881. * @vsi: the VSI being configured
  3882. **/
  3883. void i40e_vsi_stop_rings(struct i40e_vsi *vsi)
  3884. {
  3885. /* When port TX is suspended, don't wait */
  3886. if (test_bit(__I40E_PORT_SUSPENDED, vsi->back->state))
  3887. return i40e_vsi_stop_rings_no_wait(vsi);
  3888. /* do rx first for enable and last for disable
  3889. * Ignore return value, we need to shutdown whatever we can
  3890. */
  3891. i40e_vsi_control_tx(vsi, false);
  3892. i40e_vsi_control_rx(vsi, false);
  3893. }
  3894. /**
  3895. * i40e_vsi_stop_rings_no_wait - Stop a VSI's rings and do not delay
  3896. * @vsi: the VSI being shutdown
  3897. *
  3898. * This function stops all the rings for a VSI but does not delay to verify
  3899. * that rings have been disabled. It is expected that the caller is shutting
  3900. * down multiple VSIs at once and will delay together for all the VSIs after
  3901. * initiating the shutdown. This is particularly useful for shutting down lots
  3902. * of VFs together. Otherwise, a large delay can be incurred while configuring
  3903. * each VSI in serial.
  3904. **/
  3905. void i40e_vsi_stop_rings_no_wait(struct i40e_vsi *vsi)
  3906. {
  3907. struct i40e_pf *pf = vsi->back;
  3908. int i, pf_q;
  3909. pf_q = vsi->base_queue;
  3910. for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
  3911. i40e_control_tx_q(pf, pf_q, false);
  3912. i40e_control_rx_q(pf, pf_q, false);
  3913. }
  3914. }
  3915. /**
  3916. * i40e_vsi_free_irq - Free the irq association with the OS
  3917. * @vsi: the VSI being configured
  3918. **/
  3919. static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
  3920. {
  3921. struct i40e_pf *pf = vsi->back;
  3922. struct i40e_hw *hw = &pf->hw;
  3923. int base = vsi->base_vector;
  3924. u32 val, qp;
  3925. int i;
  3926. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  3927. if (!vsi->q_vectors)
  3928. return;
  3929. if (!vsi->irqs_ready)
  3930. return;
  3931. vsi->irqs_ready = false;
  3932. for (i = 0; i < vsi->num_q_vectors; i++) {
  3933. int irq_num;
  3934. u16 vector;
  3935. vector = i + base;
  3936. irq_num = pf->msix_entries[vector].vector;
  3937. /* free only the irqs that were actually requested */
  3938. if (!vsi->q_vectors[i] ||
  3939. !vsi->q_vectors[i]->num_ringpairs)
  3940. continue;
  3941. /* clear the affinity notifier in the IRQ descriptor */
  3942. irq_set_affinity_notifier(irq_num, NULL);
  3943. /* remove our suggested affinity mask for this IRQ */
  3944. irq_set_affinity_hint(irq_num, NULL);
  3945. synchronize_irq(irq_num);
  3946. free_irq(irq_num, vsi->q_vectors[i]);
  3947. /* Tear down the interrupt queue link list
  3948. *
  3949. * We know that they come in pairs and always
  3950. * the Rx first, then the Tx. To clear the
  3951. * link list, stick the EOL value into the
  3952. * next_q field of the registers.
  3953. */
  3954. val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
  3955. qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
  3956. >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
  3957. val |= I40E_QUEUE_END_OF_LIST
  3958. << I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
  3959. wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
  3960. while (qp != I40E_QUEUE_END_OF_LIST) {
  3961. u32 next;
  3962. val = rd32(hw, I40E_QINT_RQCTL(qp));
  3963. val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
  3964. I40E_QINT_RQCTL_MSIX0_INDX_MASK |
  3965. I40E_QINT_RQCTL_CAUSE_ENA_MASK |
  3966. I40E_QINT_RQCTL_INTEVENT_MASK);
  3967. val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
  3968. I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
  3969. wr32(hw, I40E_QINT_RQCTL(qp), val);
  3970. val = rd32(hw, I40E_QINT_TQCTL(qp));
  3971. next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
  3972. >> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
  3973. val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
  3974. I40E_QINT_TQCTL_MSIX0_INDX_MASK |
  3975. I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  3976. I40E_QINT_TQCTL_INTEVENT_MASK);
  3977. val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
  3978. I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
  3979. wr32(hw, I40E_QINT_TQCTL(qp), val);
  3980. qp = next;
  3981. }
  3982. }
  3983. } else {
  3984. free_irq(pf->pdev->irq, pf);
  3985. val = rd32(hw, I40E_PFINT_LNKLST0);
  3986. qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
  3987. >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
  3988. val |= I40E_QUEUE_END_OF_LIST
  3989. << I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
  3990. wr32(hw, I40E_PFINT_LNKLST0, val);
  3991. val = rd32(hw, I40E_QINT_RQCTL(qp));
  3992. val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
  3993. I40E_QINT_RQCTL_MSIX0_INDX_MASK |
  3994. I40E_QINT_RQCTL_CAUSE_ENA_MASK |
  3995. I40E_QINT_RQCTL_INTEVENT_MASK);
  3996. val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
  3997. I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
  3998. wr32(hw, I40E_QINT_RQCTL(qp), val);
  3999. val = rd32(hw, I40E_QINT_TQCTL(qp));
  4000. val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
  4001. I40E_QINT_TQCTL_MSIX0_INDX_MASK |
  4002. I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  4003. I40E_QINT_TQCTL_INTEVENT_MASK);
  4004. val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
  4005. I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
  4006. wr32(hw, I40E_QINT_TQCTL(qp), val);
  4007. }
  4008. }
  4009. /**
  4010. * i40e_free_q_vector - Free memory allocated for specific interrupt vector
  4011. * @vsi: the VSI being configured
  4012. * @v_idx: Index of vector to be freed
  4013. *
  4014. * This function frees the memory allocated to the q_vector. In addition if
  4015. * NAPI is enabled it will delete any references to the NAPI struct prior
  4016. * to freeing the q_vector.
  4017. **/
  4018. static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
  4019. {
  4020. struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
  4021. struct i40e_ring *ring;
  4022. if (!q_vector)
  4023. return;
  4024. /* disassociate q_vector from rings */
  4025. i40e_for_each_ring(ring, q_vector->tx)
  4026. ring->q_vector = NULL;
  4027. i40e_for_each_ring(ring, q_vector->rx)
  4028. ring->q_vector = NULL;
  4029. /* only VSI w/ an associated netdev is set up w/ NAPI */
  4030. if (vsi->netdev)
  4031. netif_napi_del(&q_vector->napi);
  4032. vsi->q_vectors[v_idx] = NULL;
  4033. kfree_rcu(q_vector, rcu);
  4034. }
  4035. /**
  4036. * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
  4037. * @vsi: the VSI being un-configured
  4038. *
  4039. * This frees the memory allocated to the q_vectors and
  4040. * deletes references to the NAPI struct.
  4041. **/
  4042. static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
  4043. {
  4044. int v_idx;
  4045. for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
  4046. i40e_free_q_vector(vsi, v_idx);
  4047. }
  4048. /**
  4049. * i40e_reset_interrupt_capability - Disable interrupt setup in OS
  4050. * @pf: board private structure
  4051. **/
  4052. static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
  4053. {
  4054. /* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
  4055. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  4056. pci_disable_msix(pf->pdev);
  4057. kfree(pf->msix_entries);
  4058. pf->msix_entries = NULL;
  4059. kfree(pf->irq_pile);
  4060. pf->irq_pile = NULL;
  4061. } else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
  4062. pci_disable_msi(pf->pdev);
  4063. }
  4064. pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
  4065. }
  4066. /**
  4067. * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
  4068. * @pf: board private structure
  4069. *
  4070. * We go through and clear interrupt specific resources and reset the structure
  4071. * to pre-load conditions
  4072. **/
  4073. static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
  4074. {
  4075. int i;
  4076. i40e_free_misc_vector(pf);
  4077. i40e_put_lump(pf->irq_pile, pf->iwarp_base_vector,
  4078. I40E_IWARP_IRQ_PILE_ID);
  4079. i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
  4080. for (i = 0; i < pf->num_alloc_vsi; i++)
  4081. if (pf->vsi[i])
  4082. i40e_vsi_free_q_vectors(pf->vsi[i]);
  4083. i40e_reset_interrupt_capability(pf);
  4084. }
  4085. /**
  4086. * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
  4087. * @vsi: the VSI being configured
  4088. **/
  4089. static void i40e_napi_enable_all(struct i40e_vsi *vsi)
  4090. {
  4091. int q_idx;
  4092. if (!vsi->netdev)
  4093. return;
  4094. for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
  4095. struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
  4096. if (q_vector->rx.ring || q_vector->tx.ring)
  4097. napi_enable(&q_vector->napi);
  4098. }
  4099. }
  4100. /**
  4101. * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
  4102. * @vsi: the VSI being configured
  4103. **/
  4104. static void i40e_napi_disable_all(struct i40e_vsi *vsi)
  4105. {
  4106. int q_idx;
  4107. if (!vsi->netdev)
  4108. return;
  4109. for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
  4110. struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
  4111. if (q_vector->rx.ring || q_vector->tx.ring)
  4112. napi_disable(&q_vector->napi);
  4113. }
  4114. }
  4115. /**
  4116. * i40e_vsi_close - Shut down a VSI
  4117. * @vsi: the vsi to be quelled
  4118. **/
  4119. static void i40e_vsi_close(struct i40e_vsi *vsi)
  4120. {
  4121. struct i40e_pf *pf = vsi->back;
  4122. if (!test_and_set_bit(__I40E_VSI_DOWN, vsi->state))
  4123. i40e_down(vsi);
  4124. i40e_vsi_free_irq(vsi);
  4125. i40e_vsi_free_tx_resources(vsi);
  4126. i40e_vsi_free_rx_resources(vsi);
  4127. vsi->current_netdev_flags = 0;
  4128. set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
  4129. if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
  4130. set_bit(__I40E_CLIENT_RESET, pf->state);
  4131. }
  4132. /**
  4133. * i40e_quiesce_vsi - Pause a given VSI
  4134. * @vsi: the VSI being paused
  4135. **/
  4136. static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
  4137. {
  4138. if (test_bit(__I40E_VSI_DOWN, vsi->state))
  4139. return;
  4140. set_bit(__I40E_VSI_NEEDS_RESTART, vsi->state);
  4141. if (vsi->netdev && netif_running(vsi->netdev))
  4142. vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
  4143. else
  4144. i40e_vsi_close(vsi);
  4145. }
  4146. /**
  4147. * i40e_unquiesce_vsi - Resume a given VSI
  4148. * @vsi: the VSI being resumed
  4149. **/
  4150. static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
  4151. {
  4152. if (!test_and_clear_bit(__I40E_VSI_NEEDS_RESTART, vsi->state))
  4153. return;
  4154. if (vsi->netdev && netif_running(vsi->netdev))
  4155. vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
  4156. else
  4157. i40e_vsi_open(vsi); /* this clears the DOWN bit */
  4158. }
  4159. /**
  4160. * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
  4161. * @pf: the PF
  4162. **/
  4163. static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
  4164. {
  4165. int v;
  4166. for (v = 0; v < pf->num_alloc_vsi; v++) {
  4167. if (pf->vsi[v])
  4168. i40e_quiesce_vsi(pf->vsi[v]);
  4169. }
  4170. }
  4171. /**
  4172. * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
  4173. * @pf: the PF
  4174. **/
  4175. static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
  4176. {
  4177. int v;
  4178. for (v = 0; v < pf->num_alloc_vsi; v++) {
  4179. if (pf->vsi[v])
  4180. i40e_unquiesce_vsi(pf->vsi[v]);
  4181. }
  4182. }
  4183. /**
  4184. * i40e_vsi_wait_queues_disabled - Wait for VSI's queues to be disabled
  4185. * @vsi: the VSI being configured
  4186. *
  4187. * Wait until all queues on a given VSI have been disabled.
  4188. **/
  4189. int i40e_vsi_wait_queues_disabled(struct i40e_vsi *vsi)
  4190. {
  4191. struct i40e_pf *pf = vsi->back;
  4192. int i, pf_q, ret;
  4193. pf_q = vsi->base_queue;
  4194. for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
  4195. /* Check and wait for the Tx queue */
  4196. ret = i40e_pf_txq_wait(pf, pf_q, false);
  4197. if (ret) {
  4198. dev_info(&pf->pdev->dev,
  4199. "VSI seid %d Tx ring %d disable timeout\n",
  4200. vsi->seid, pf_q);
  4201. return ret;
  4202. }
  4203. if (!i40e_enabled_xdp_vsi(vsi))
  4204. goto wait_rx;
  4205. /* Check and wait for the XDP Tx queue */
  4206. ret = i40e_pf_txq_wait(pf, pf_q + vsi->alloc_queue_pairs,
  4207. false);
  4208. if (ret) {
  4209. dev_info(&pf->pdev->dev,
  4210. "VSI seid %d XDP Tx ring %d disable timeout\n",
  4211. vsi->seid, pf_q);
  4212. return ret;
  4213. }
  4214. wait_rx:
  4215. /* Check and wait for the Rx queue */
  4216. ret = i40e_pf_rxq_wait(pf, pf_q, false);
  4217. if (ret) {
  4218. dev_info(&pf->pdev->dev,
  4219. "VSI seid %d Rx ring %d disable timeout\n",
  4220. vsi->seid, pf_q);
  4221. return ret;
  4222. }
  4223. }
  4224. return 0;
  4225. }
  4226. #ifdef CONFIG_I40E_DCB
  4227. /**
  4228. * i40e_pf_wait_queues_disabled - Wait for all queues of PF VSIs to be disabled
  4229. * @pf: the PF
  4230. *
  4231. * This function waits for the queues to be in disabled state for all the
  4232. * VSIs that are managed by this PF.
  4233. **/
  4234. static int i40e_pf_wait_queues_disabled(struct i40e_pf *pf)
  4235. {
  4236. int v, ret = 0;
  4237. for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
  4238. if (pf->vsi[v]) {
  4239. ret = i40e_vsi_wait_queues_disabled(pf->vsi[v]);
  4240. if (ret)
  4241. break;
  4242. }
  4243. }
  4244. return ret;
  4245. }
  4246. #endif
  4247. /**
  4248. * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
  4249. * @pf: pointer to PF
  4250. *
  4251. * Get TC map for ISCSI PF type that will include iSCSI TC
  4252. * and LAN TC.
  4253. **/
  4254. static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
  4255. {
  4256. struct i40e_dcb_app_priority_table app;
  4257. struct i40e_hw *hw = &pf->hw;
  4258. u8 enabled_tc = 1; /* TC0 is always enabled */
  4259. u8 tc, i;
  4260. /* Get the iSCSI APP TLV */
  4261. struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
  4262. for (i = 0; i < dcbcfg->numapps; i++) {
  4263. app = dcbcfg->app[i];
  4264. if (app.selector == I40E_APP_SEL_TCPIP &&
  4265. app.protocolid == I40E_APP_PROTOID_ISCSI) {
  4266. tc = dcbcfg->etscfg.prioritytable[app.priority];
  4267. enabled_tc |= BIT(tc);
  4268. break;
  4269. }
  4270. }
  4271. return enabled_tc;
  4272. }
  4273. /**
  4274. * i40e_dcb_get_num_tc - Get the number of TCs from DCBx config
  4275. * @dcbcfg: the corresponding DCBx configuration structure
  4276. *
  4277. * Return the number of TCs from given DCBx configuration
  4278. **/
  4279. static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
  4280. {
  4281. int i, tc_unused = 0;
  4282. u8 num_tc = 0;
  4283. u8 ret = 0;
  4284. /* Scan the ETS Config Priority Table to find
  4285. * traffic class enabled for a given priority
  4286. * and create a bitmask of enabled TCs
  4287. */
  4288. for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
  4289. num_tc |= BIT(dcbcfg->etscfg.prioritytable[i]);
  4290. /* Now scan the bitmask to check for
  4291. * contiguous TCs starting with TC0
  4292. */
  4293. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4294. if (num_tc & BIT(i)) {
  4295. if (!tc_unused) {
  4296. ret++;
  4297. } else {
  4298. pr_err("Non-contiguous TC - Disabling DCB\n");
  4299. return 1;
  4300. }
  4301. } else {
  4302. tc_unused = 1;
  4303. }
  4304. }
  4305. /* There is always at least TC0 */
  4306. if (!ret)
  4307. ret = 1;
  4308. return ret;
  4309. }
  4310. /**
  4311. * i40e_dcb_get_enabled_tc - Get enabled traffic classes
  4312. * @dcbcfg: the corresponding DCBx configuration structure
  4313. *
  4314. * Query the current DCB configuration and return the number of
  4315. * traffic classes enabled from the given DCBX config
  4316. **/
  4317. static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
  4318. {
  4319. u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
  4320. u8 enabled_tc = 1;
  4321. u8 i;
  4322. for (i = 0; i < num_tc; i++)
  4323. enabled_tc |= BIT(i);
  4324. return enabled_tc;
  4325. }
  4326. /**
  4327. * i40e_mqprio_get_enabled_tc - Get enabled traffic classes
  4328. * @pf: PF being queried
  4329. *
  4330. * Query the current MQPRIO configuration and return the number of
  4331. * traffic classes enabled.
  4332. **/
  4333. static u8 i40e_mqprio_get_enabled_tc(struct i40e_pf *pf)
  4334. {
  4335. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  4336. u8 num_tc = vsi->mqprio_qopt.qopt.num_tc;
  4337. u8 enabled_tc = 1, i;
  4338. for (i = 1; i < num_tc; i++)
  4339. enabled_tc |= BIT(i);
  4340. return enabled_tc;
  4341. }
  4342. /**
  4343. * i40e_pf_get_num_tc - Get enabled traffic classes for PF
  4344. * @pf: PF being queried
  4345. *
  4346. * Return number of traffic classes enabled for the given PF
  4347. **/
  4348. static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
  4349. {
  4350. struct i40e_hw *hw = &pf->hw;
  4351. u8 i, enabled_tc = 1;
  4352. u8 num_tc = 0;
  4353. struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
  4354. if (pf->flags & I40E_FLAG_TC_MQPRIO)
  4355. return pf->vsi[pf->lan_vsi]->mqprio_qopt.qopt.num_tc;
  4356. /* If neither MQPRIO nor DCB is enabled, then always use single TC */
  4357. if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
  4358. return 1;
  4359. /* SFP mode will be enabled for all TCs on port */
  4360. if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
  4361. return i40e_dcb_get_num_tc(dcbcfg);
  4362. /* MFP mode return count of enabled TCs for this PF */
  4363. if (pf->hw.func_caps.iscsi)
  4364. enabled_tc = i40e_get_iscsi_tc_map(pf);
  4365. else
  4366. return 1; /* Only TC0 */
  4367. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4368. if (enabled_tc & BIT(i))
  4369. num_tc++;
  4370. }
  4371. return num_tc;
  4372. }
  4373. /**
  4374. * i40e_pf_get_pf_tc_map - Get bitmap for enabled traffic classes
  4375. * @pf: PF being queried
  4376. *
  4377. * Return a bitmap for enabled traffic classes for this PF.
  4378. **/
  4379. static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
  4380. {
  4381. if (pf->flags & I40E_FLAG_TC_MQPRIO)
  4382. return i40e_mqprio_get_enabled_tc(pf);
  4383. /* If neither MQPRIO nor DCB is enabled for this PF then just return
  4384. * default TC
  4385. */
  4386. if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
  4387. return I40E_DEFAULT_TRAFFIC_CLASS;
  4388. /* SFP mode we want PF to be enabled for all TCs */
  4389. if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
  4390. return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
  4391. /* MFP enabled and iSCSI PF type */
  4392. if (pf->hw.func_caps.iscsi)
  4393. return i40e_get_iscsi_tc_map(pf);
  4394. else
  4395. return I40E_DEFAULT_TRAFFIC_CLASS;
  4396. }
  4397. /**
  4398. * i40e_vsi_get_bw_info - Query VSI BW Information
  4399. * @vsi: the VSI being queried
  4400. *
  4401. * Returns 0 on success, negative value on failure
  4402. **/
  4403. static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
  4404. {
  4405. struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
  4406. struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
  4407. struct i40e_pf *pf = vsi->back;
  4408. struct i40e_hw *hw = &pf->hw;
  4409. i40e_status ret;
  4410. u32 tc_bw_max;
  4411. int i;
  4412. /* Get the VSI level BW configuration */
  4413. ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
  4414. if (ret) {
  4415. dev_info(&pf->pdev->dev,
  4416. "couldn't get PF vsi bw config, err %s aq_err %s\n",
  4417. i40e_stat_str(&pf->hw, ret),
  4418. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  4419. return -EINVAL;
  4420. }
  4421. /* Get the VSI level BW configuration per TC */
  4422. ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
  4423. NULL);
  4424. if (ret) {
  4425. dev_info(&pf->pdev->dev,
  4426. "couldn't get PF vsi ets bw config, err %s aq_err %s\n",
  4427. i40e_stat_str(&pf->hw, ret),
  4428. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  4429. return -EINVAL;
  4430. }
  4431. if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
  4432. dev_info(&pf->pdev->dev,
  4433. "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
  4434. bw_config.tc_valid_bits,
  4435. bw_ets_config.tc_valid_bits);
  4436. /* Still continuing */
  4437. }
  4438. vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
  4439. vsi->bw_max_quanta = bw_config.max_bw;
  4440. tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
  4441. (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
  4442. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4443. vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
  4444. vsi->bw_ets_limit_credits[i] =
  4445. le16_to_cpu(bw_ets_config.credits[i]);
  4446. /* 3 bits out of 4 for each TC */
  4447. vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
  4448. }
  4449. return 0;
  4450. }
  4451. /**
  4452. * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
  4453. * @vsi: the VSI being configured
  4454. * @enabled_tc: TC bitmap
  4455. * @bw_credits: BW shared credits per TC
  4456. *
  4457. * Returns 0 on success, negative value on failure
  4458. **/
  4459. static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
  4460. u8 *bw_share)
  4461. {
  4462. struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
  4463. i40e_status ret;
  4464. int i;
  4465. if (vsi->back->flags & I40E_FLAG_TC_MQPRIO)
  4466. return 0;
  4467. if (!vsi->mqprio_qopt.qopt.hw) {
  4468. ret = i40e_set_bw_limit(vsi, vsi->seid, 0);
  4469. if (ret)
  4470. dev_info(&vsi->back->pdev->dev,
  4471. "Failed to reset tx rate for vsi->seid %u\n",
  4472. vsi->seid);
  4473. return ret;
  4474. }
  4475. bw_data.tc_valid_bits = enabled_tc;
  4476. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
  4477. bw_data.tc_bw_credits[i] = bw_share[i];
  4478. ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, vsi->seid, &bw_data,
  4479. NULL);
  4480. if (ret) {
  4481. dev_info(&vsi->back->pdev->dev,
  4482. "AQ command Config VSI BW allocation per TC failed = %d\n",
  4483. vsi->back->hw.aq.asq_last_status);
  4484. return -EINVAL;
  4485. }
  4486. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
  4487. vsi->info.qs_handle[i] = bw_data.qs_handles[i];
  4488. return 0;
  4489. }
  4490. /**
  4491. * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
  4492. * @vsi: the VSI being configured
  4493. * @enabled_tc: TC map to be enabled
  4494. *
  4495. **/
  4496. static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
  4497. {
  4498. struct net_device *netdev = vsi->netdev;
  4499. struct i40e_pf *pf = vsi->back;
  4500. struct i40e_hw *hw = &pf->hw;
  4501. u8 netdev_tc = 0;
  4502. int i;
  4503. struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
  4504. if (!netdev)
  4505. return;
  4506. if (!enabled_tc) {
  4507. netdev_reset_tc(netdev);
  4508. return;
  4509. }
  4510. /* Set up actual enabled TCs on the VSI */
  4511. if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
  4512. return;
  4513. /* set per TC queues for the VSI */
  4514. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4515. /* Only set TC queues for enabled tcs
  4516. *
  4517. * e.g. For a VSI that has TC0 and TC3 enabled the
  4518. * enabled_tc bitmap would be 0x00001001; the driver
  4519. * will set the numtc for netdev as 2 that will be
  4520. * referenced by the netdev layer as TC 0 and 1.
  4521. */
  4522. if (vsi->tc_config.enabled_tc & BIT(i))
  4523. netdev_set_tc_queue(netdev,
  4524. vsi->tc_config.tc_info[i].netdev_tc,
  4525. vsi->tc_config.tc_info[i].qcount,
  4526. vsi->tc_config.tc_info[i].qoffset);
  4527. }
  4528. if (pf->flags & I40E_FLAG_TC_MQPRIO)
  4529. return;
  4530. /* Assign UP2TC map for the VSI */
  4531. for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
  4532. /* Get the actual TC# for the UP */
  4533. u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
  4534. /* Get the mapped netdev TC# for the UP */
  4535. netdev_tc = vsi->tc_config.tc_info[ets_tc].netdev_tc;
  4536. netdev_set_prio_tc_map(netdev, i, netdev_tc);
  4537. }
  4538. }
  4539. /**
  4540. * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
  4541. * @vsi: the VSI being configured
  4542. * @ctxt: the ctxt buffer returned from AQ VSI update param command
  4543. **/
  4544. static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
  4545. struct i40e_vsi_context *ctxt)
  4546. {
  4547. /* copy just the sections touched not the entire info
  4548. * since not all sections are valid as returned by
  4549. * update vsi params
  4550. */
  4551. vsi->info.mapping_flags = ctxt->info.mapping_flags;
  4552. memcpy(&vsi->info.queue_mapping,
  4553. &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
  4554. memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
  4555. sizeof(vsi->info.tc_mapping));
  4556. }
  4557. /**
  4558. * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
  4559. * @vsi: VSI to be configured
  4560. * @enabled_tc: TC bitmap
  4561. *
  4562. * This configures a particular VSI for TCs that are mapped to the
  4563. * given TC bitmap. It uses default bandwidth share for TCs across
  4564. * VSIs to configure TC for a particular VSI.
  4565. *
  4566. * NOTE:
  4567. * It is expected that the VSI queues have been quisced before calling
  4568. * this function.
  4569. **/
  4570. static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
  4571. {
  4572. u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
  4573. struct i40e_pf *pf = vsi->back;
  4574. struct i40e_hw *hw = &pf->hw;
  4575. struct i40e_vsi_context ctxt;
  4576. int ret = 0;
  4577. int i;
  4578. /* Check if enabled_tc is same as existing or new TCs */
  4579. if (vsi->tc_config.enabled_tc == enabled_tc &&
  4580. vsi->mqprio_qopt.mode != TC_MQPRIO_MODE_CHANNEL)
  4581. return ret;
  4582. /* Enable ETS TCs with equal BW Share for now across all VSIs */
  4583. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4584. if (enabled_tc & BIT(i))
  4585. bw_share[i] = 1;
  4586. }
  4587. ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
  4588. if (ret) {
  4589. struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
  4590. dev_info(&pf->pdev->dev,
  4591. "Failed configuring TC map %d for VSI %d\n",
  4592. enabled_tc, vsi->seid);
  4593. ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid,
  4594. &bw_config, NULL);
  4595. if (ret) {
  4596. dev_info(&pf->pdev->dev,
  4597. "Failed querying vsi bw info, err %s aq_err %s\n",
  4598. i40e_stat_str(hw, ret),
  4599. i40e_aq_str(hw, hw->aq.asq_last_status));
  4600. goto out;
  4601. }
  4602. if ((bw_config.tc_valid_bits & enabled_tc) != enabled_tc) {
  4603. u8 valid_tc = bw_config.tc_valid_bits & enabled_tc;
  4604. if (!valid_tc)
  4605. valid_tc = bw_config.tc_valid_bits;
  4606. /* Always enable TC0, no matter what */
  4607. valid_tc |= 1;
  4608. dev_info(&pf->pdev->dev,
  4609. "Requested tc 0x%x, but FW reports 0x%x as valid. Attempting to use 0x%x.\n",
  4610. enabled_tc, bw_config.tc_valid_bits, valid_tc);
  4611. enabled_tc = valid_tc;
  4612. }
  4613. ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
  4614. if (ret) {
  4615. dev_err(&pf->pdev->dev,
  4616. "Unable to configure TC map %d for VSI %d\n",
  4617. enabled_tc, vsi->seid);
  4618. goto out;
  4619. }
  4620. }
  4621. /* Update Queue Pairs Mapping for currently enabled UPs */
  4622. ctxt.seid = vsi->seid;
  4623. ctxt.pf_num = vsi->back->hw.pf_id;
  4624. ctxt.vf_num = 0;
  4625. ctxt.uplink_seid = vsi->uplink_seid;
  4626. ctxt.info = vsi->info;
  4627. if (vsi->back->flags & I40E_FLAG_TC_MQPRIO) {
  4628. ret = i40e_vsi_setup_queue_map_mqprio(vsi, &ctxt, enabled_tc);
  4629. if (ret)
  4630. goto out;
  4631. } else {
  4632. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
  4633. }
  4634. /* On destroying the qdisc, reset vsi->rss_size, as number of enabled
  4635. * queues changed.
  4636. */
  4637. if (!vsi->mqprio_qopt.qopt.hw && vsi->reconfig_rss) {
  4638. vsi->rss_size = min_t(int, vsi->back->alloc_rss_size,
  4639. vsi->num_queue_pairs);
  4640. ret = i40e_vsi_config_rss(vsi);
  4641. if (ret) {
  4642. dev_info(&vsi->back->pdev->dev,
  4643. "Failed to reconfig rss for num_queues\n");
  4644. return ret;
  4645. }
  4646. vsi->reconfig_rss = false;
  4647. }
  4648. if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
  4649. ctxt.info.valid_sections |=
  4650. cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
  4651. ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
  4652. }
  4653. /* Update the VSI after updating the VSI queue-mapping
  4654. * information
  4655. */
  4656. ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
  4657. if (ret) {
  4658. dev_info(&pf->pdev->dev,
  4659. "Update vsi tc config failed, err %s aq_err %s\n",
  4660. i40e_stat_str(hw, ret),
  4661. i40e_aq_str(hw, hw->aq.asq_last_status));
  4662. goto out;
  4663. }
  4664. /* update the local VSI info with updated queue map */
  4665. i40e_vsi_update_queue_map(vsi, &ctxt);
  4666. vsi->info.valid_sections = 0;
  4667. /* Update current VSI BW information */
  4668. ret = i40e_vsi_get_bw_info(vsi);
  4669. if (ret) {
  4670. dev_info(&pf->pdev->dev,
  4671. "Failed updating vsi bw info, err %s aq_err %s\n",
  4672. i40e_stat_str(hw, ret),
  4673. i40e_aq_str(hw, hw->aq.asq_last_status));
  4674. goto out;
  4675. }
  4676. /* Update the netdev TC setup */
  4677. i40e_vsi_config_netdev_tc(vsi, enabled_tc);
  4678. out:
  4679. return ret;
  4680. }
  4681. /**
  4682. * i40e_get_link_speed - Returns link speed for the interface
  4683. * @vsi: VSI to be configured
  4684. *
  4685. **/
  4686. static int i40e_get_link_speed(struct i40e_vsi *vsi)
  4687. {
  4688. struct i40e_pf *pf = vsi->back;
  4689. switch (pf->hw.phy.link_info.link_speed) {
  4690. case I40E_LINK_SPEED_40GB:
  4691. return 40000;
  4692. case I40E_LINK_SPEED_25GB:
  4693. return 25000;
  4694. case I40E_LINK_SPEED_20GB:
  4695. return 20000;
  4696. case I40E_LINK_SPEED_10GB:
  4697. return 10000;
  4698. case I40E_LINK_SPEED_1GB:
  4699. return 1000;
  4700. default:
  4701. return -EINVAL;
  4702. }
  4703. }
  4704. /**
  4705. * i40e_set_bw_limit - setup BW limit for Tx traffic based on max_tx_rate
  4706. * @vsi: VSI to be configured
  4707. * @seid: seid of the channel/VSI
  4708. * @max_tx_rate: max TX rate to be configured as BW limit
  4709. *
  4710. * Helper function to set BW limit for a given VSI
  4711. **/
  4712. int i40e_set_bw_limit(struct i40e_vsi *vsi, u16 seid, u64 max_tx_rate)
  4713. {
  4714. struct i40e_pf *pf = vsi->back;
  4715. u64 credits = 0;
  4716. int speed = 0;
  4717. int ret = 0;
  4718. speed = i40e_get_link_speed(vsi);
  4719. if (max_tx_rate > speed) {
  4720. dev_err(&pf->pdev->dev,
  4721. "Invalid max tx rate %llu specified for VSI seid %d.",
  4722. max_tx_rate, seid);
  4723. return -EINVAL;
  4724. }
  4725. if (max_tx_rate && max_tx_rate < 50) {
  4726. dev_warn(&pf->pdev->dev,
  4727. "Setting max tx rate to minimum usable value of 50Mbps.\n");
  4728. max_tx_rate = 50;
  4729. }
  4730. /* Tx rate credits are in values of 50Mbps, 0 is disabled */
  4731. credits = max_tx_rate;
  4732. do_div(credits, I40E_BW_CREDIT_DIVISOR);
  4733. ret = i40e_aq_config_vsi_bw_limit(&pf->hw, seid, credits,
  4734. I40E_MAX_BW_INACTIVE_ACCUM, NULL);
  4735. if (ret)
  4736. dev_err(&pf->pdev->dev,
  4737. "Failed set tx rate (%llu Mbps) for vsi->seid %u, err %s aq_err %s\n",
  4738. max_tx_rate, seid, i40e_stat_str(&pf->hw, ret),
  4739. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  4740. return ret;
  4741. }
  4742. /**
  4743. * i40e_remove_queue_channels - Remove queue channels for the TCs
  4744. * @vsi: VSI to be configured
  4745. *
  4746. * Remove queue channels for the TCs
  4747. **/
  4748. static void i40e_remove_queue_channels(struct i40e_vsi *vsi)
  4749. {
  4750. enum i40e_admin_queue_err last_aq_status;
  4751. struct i40e_cloud_filter *cfilter;
  4752. struct i40e_channel *ch, *ch_tmp;
  4753. struct i40e_pf *pf = vsi->back;
  4754. struct hlist_node *node;
  4755. int ret, i;
  4756. /* Reset rss size that was stored when reconfiguring rss for
  4757. * channel VSIs with non-power-of-2 queue count.
  4758. */
  4759. vsi->current_rss_size = 0;
  4760. /* perform cleanup for channels if they exist */
  4761. if (list_empty(&vsi->ch_list))
  4762. return;
  4763. list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
  4764. struct i40e_vsi *p_vsi;
  4765. list_del(&ch->list);
  4766. p_vsi = ch->parent_vsi;
  4767. if (!p_vsi || !ch->initialized) {
  4768. kfree(ch);
  4769. continue;
  4770. }
  4771. /* Reset queue contexts */
  4772. for (i = 0; i < ch->num_queue_pairs; i++) {
  4773. struct i40e_ring *tx_ring, *rx_ring;
  4774. u16 pf_q;
  4775. pf_q = ch->base_queue + i;
  4776. tx_ring = vsi->tx_rings[pf_q];
  4777. tx_ring->ch = NULL;
  4778. rx_ring = vsi->rx_rings[pf_q];
  4779. rx_ring->ch = NULL;
  4780. }
  4781. /* Reset BW configured for this VSI via mqprio */
  4782. ret = i40e_set_bw_limit(vsi, ch->seid, 0);
  4783. if (ret)
  4784. dev_info(&vsi->back->pdev->dev,
  4785. "Failed to reset tx rate for ch->seid %u\n",
  4786. ch->seid);
  4787. /* delete cloud filters associated with this channel */
  4788. hlist_for_each_entry_safe(cfilter, node,
  4789. &pf->cloud_filter_list, cloud_node) {
  4790. if (cfilter->seid != ch->seid)
  4791. continue;
  4792. hash_del(&cfilter->cloud_node);
  4793. if (cfilter->dst_port)
  4794. ret = i40e_add_del_cloud_filter_big_buf(vsi,
  4795. cfilter,
  4796. false);
  4797. else
  4798. ret = i40e_add_del_cloud_filter(vsi, cfilter,
  4799. false);
  4800. last_aq_status = pf->hw.aq.asq_last_status;
  4801. if (ret)
  4802. dev_info(&pf->pdev->dev,
  4803. "Failed to delete cloud filter, err %s aq_err %s\n",
  4804. i40e_stat_str(&pf->hw, ret),
  4805. i40e_aq_str(&pf->hw, last_aq_status));
  4806. kfree(cfilter);
  4807. }
  4808. /* delete VSI from FW */
  4809. ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
  4810. NULL);
  4811. if (ret)
  4812. dev_err(&vsi->back->pdev->dev,
  4813. "unable to remove channel (%d) for parent VSI(%d)\n",
  4814. ch->seid, p_vsi->seid);
  4815. kfree(ch);
  4816. }
  4817. INIT_LIST_HEAD(&vsi->ch_list);
  4818. }
  4819. /**
  4820. * i40e_is_any_channel - channel exist or not
  4821. * @vsi: ptr to VSI to which channels are associated with
  4822. *
  4823. * Returns true or false if channel(s) exist for associated VSI or not
  4824. **/
  4825. static bool i40e_is_any_channel(struct i40e_vsi *vsi)
  4826. {
  4827. struct i40e_channel *ch, *ch_tmp;
  4828. list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
  4829. if (ch->initialized)
  4830. return true;
  4831. }
  4832. return false;
  4833. }
  4834. /**
  4835. * i40e_get_max_queues_for_channel
  4836. * @vsi: ptr to VSI to which channels are associated with
  4837. *
  4838. * Helper function which returns max value among the queue counts set on the
  4839. * channels/TCs created.
  4840. **/
  4841. static int i40e_get_max_queues_for_channel(struct i40e_vsi *vsi)
  4842. {
  4843. struct i40e_channel *ch, *ch_tmp;
  4844. int max = 0;
  4845. list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
  4846. if (!ch->initialized)
  4847. continue;
  4848. if (ch->num_queue_pairs > max)
  4849. max = ch->num_queue_pairs;
  4850. }
  4851. return max;
  4852. }
  4853. /**
  4854. * i40e_validate_num_queues - validate num_queues w.r.t channel
  4855. * @pf: ptr to PF device
  4856. * @num_queues: number of queues
  4857. * @vsi: the parent VSI
  4858. * @reconfig_rss: indicates should the RSS be reconfigured or not
  4859. *
  4860. * This function validates number of queues in the context of new channel
  4861. * which is being established and determines if RSS should be reconfigured
  4862. * or not for parent VSI.
  4863. **/
  4864. static int i40e_validate_num_queues(struct i40e_pf *pf, int num_queues,
  4865. struct i40e_vsi *vsi, bool *reconfig_rss)
  4866. {
  4867. int max_ch_queues;
  4868. if (!reconfig_rss)
  4869. return -EINVAL;
  4870. *reconfig_rss = false;
  4871. if (vsi->current_rss_size) {
  4872. if (num_queues > vsi->current_rss_size) {
  4873. dev_dbg(&pf->pdev->dev,
  4874. "Error: num_queues (%d) > vsi's current_size(%d)\n",
  4875. num_queues, vsi->current_rss_size);
  4876. return -EINVAL;
  4877. } else if ((num_queues < vsi->current_rss_size) &&
  4878. (!is_power_of_2(num_queues))) {
  4879. dev_dbg(&pf->pdev->dev,
  4880. "Error: num_queues (%d) < vsi's current_size(%d), but not power of 2\n",
  4881. num_queues, vsi->current_rss_size);
  4882. return -EINVAL;
  4883. }
  4884. }
  4885. if (!is_power_of_2(num_queues)) {
  4886. /* Find the max num_queues configured for channel if channel
  4887. * exist.
  4888. * if channel exist, then enforce 'num_queues' to be more than
  4889. * max ever queues configured for channel.
  4890. */
  4891. max_ch_queues = i40e_get_max_queues_for_channel(vsi);
  4892. if (num_queues < max_ch_queues) {
  4893. dev_dbg(&pf->pdev->dev,
  4894. "Error: num_queues (%d) < max queues configured for channel(%d)\n",
  4895. num_queues, max_ch_queues);
  4896. return -EINVAL;
  4897. }
  4898. *reconfig_rss = true;
  4899. }
  4900. return 0;
  4901. }
  4902. /**
  4903. * i40e_vsi_reconfig_rss - reconfig RSS based on specified rss_size
  4904. * @vsi: the VSI being setup
  4905. * @rss_size: size of RSS, accordingly LUT gets reprogrammed
  4906. *
  4907. * This function reconfigures RSS by reprogramming LUTs using 'rss_size'
  4908. **/
  4909. static int i40e_vsi_reconfig_rss(struct i40e_vsi *vsi, u16 rss_size)
  4910. {
  4911. struct i40e_pf *pf = vsi->back;
  4912. u8 seed[I40E_HKEY_ARRAY_SIZE];
  4913. struct i40e_hw *hw = &pf->hw;
  4914. int local_rss_size;
  4915. u8 *lut;
  4916. int ret;
  4917. if (!vsi->rss_size)
  4918. return -EINVAL;
  4919. if (rss_size > vsi->rss_size)
  4920. return -EINVAL;
  4921. local_rss_size = min_t(int, vsi->rss_size, rss_size);
  4922. lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
  4923. if (!lut)
  4924. return -ENOMEM;
  4925. /* Ignoring user configured lut if there is one */
  4926. i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, local_rss_size);
  4927. /* Use user configured hash key if there is one, otherwise
  4928. * use default.
  4929. */
  4930. if (vsi->rss_hkey_user)
  4931. memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
  4932. else
  4933. netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
  4934. ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
  4935. if (ret) {
  4936. dev_info(&pf->pdev->dev,
  4937. "Cannot set RSS lut, err %s aq_err %s\n",
  4938. i40e_stat_str(hw, ret),
  4939. i40e_aq_str(hw, hw->aq.asq_last_status));
  4940. kfree(lut);
  4941. return ret;
  4942. }
  4943. kfree(lut);
  4944. /* Do the update w.r.t. storing rss_size */
  4945. if (!vsi->orig_rss_size)
  4946. vsi->orig_rss_size = vsi->rss_size;
  4947. vsi->current_rss_size = local_rss_size;
  4948. return ret;
  4949. }
  4950. /**
  4951. * i40e_channel_setup_queue_map - Setup a channel queue map
  4952. * @pf: ptr to PF device
  4953. * @vsi: the VSI being setup
  4954. * @ctxt: VSI context structure
  4955. * @ch: ptr to channel structure
  4956. *
  4957. * Setup queue map for a specific channel
  4958. **/
  4959. static void i40e_channel_setup_queue_map(struct i40e_pf *pf,
  4960. struct i40e_vsi_context *ctxt,
  4961. struct i40e_channel *ch)
  4962. {
  4963. u16 qcount, qmap, sections = 0;
  4964. u8 offset = 0;
  4965. int pow;
  4966. sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
  4967. sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
  4968. qcount = min_t(int, ch->num_queue_pairs, pf->num_lan_msix);
  4969. ch->num_queue_pairs = qcount;
  4970. /* find the next higher power-of-2 of num queue pairs */
  4971. pow = ilog2(qcount);
  4972. if (!is_power_of_2(qcount))
  4973. pow++;
  4974. qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
  4975. (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
  4976. /* Setup queue TC[0].qmap for given VSI context */
  4977. ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
  4978. ctxt->info.up_enable_bits = 0x1; /* TC0 enabled */
  4979. ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
  4980. ctxt->info.queue_mapping[0] = cpu_to_le16(ch->base_queue);
  4981. ctxt->info.valid_sections |= cpu_to_le16(sections);
  4982. }
  4983. /**
  4984. * i40e_add_channel - add a channel by adding VSI
  4985. * @pf: ptr to PF device
  4986. * @uplink_seid: underlying HW switching element (VEB) ID
  4987. * @ch: ptr to channel structure
  4988. *
  4989. * Add a channel (VSI) using add_vsi and queue_map
  4990. **/
  4991. static int i40e_add_channel(struct i40e_pf *pf, u16 uplink_seid,
  4992. struct i40e_channel *ch)
  4993. {
  4994. struct i40e_hw *hw = &pf->hw;
  4995. struct i40e_vsi_context ctxt;
  4996. u8 enabled_tc = 0x1; /* TC0 enabled */
  4997. int ret;
  4998. if (ch->type != I40E_VSI_VMDQ2) {
  4999. dev_info(&pf->pdev->dev,
  5000. "add new vsi failed, ch->type %d\n", ch->type);
  5001. return -EINVAL;
  5002. }
  5003. memset(&ctxt, 0, sizeof(ctxt));
  5004. ctxt.pf_num = hw->pf_id;
  5005. ctxt.vf_num = 0;
  5006. ctxt.uplink_seid = uplink_seid;
  5007. ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
  5008. if (ch->type == I40E_VSI_VMDQ2)
  5009. ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
  5010. if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED) {
  5011. ctxt.info.valid_sections |=
  5012. cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  5013. ctxt.info.switch_id =
  5014. cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  5015. }
  5016. /* Set queue map for a given VSI context */
  5017. i40e_channel_setup_queue_map(pf, &ctxt, ch);
  5018. /* Now time to create VSI */
  5019. ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
  5020. if (ret) {
  5021. dev_info(&pf->pdev->dev,
  5022. "add new vsi failed, err %s aq_err %s\n",
  5023. i40e_stat_str(&pf->hw, ret),
  5024. i40e_aq_str(&pf->hw,
  5025. pf->hw.aq.asq_last_status));
  5026. return -ENOENT;
  5027. }
  5028. /* Success, update channel */
  5029. ch->enabled_tc = enabled_tc;
  5030. ch->seid = ctxt.seid;
  5031. ch->vsi_number = ctxt.vsi_number;
  5032. ch->stat_counter_idx = cpu_to_le16(ctxt.info.stat_counter_idx);
  5033. /* copy just the sections touched not the entire info
  5034. * since not all sections are valid as returned by
  5035. * update vsi params
  5036. */
  5037. ch->info.mapping_flags = ctxt.info.mapping_flags;
  5038. memcpy(&ch->info.queue_mapping,
  5039. &ctxt.info.queue_mapping, sizeof(ctxt.info.queue_mapping));
  5040. memcpy(&ch->info.tc_mapping, ctxt.info.tc_mapping,
  5041. sizeof(ctxt.info.tc_mapping));
  5042. return 0;
  5043. }
  5044. static int i40e_channel_config_bw(struct i40e_vsi *vsi, struct i40e_channel *ch,
  5045. u8 *bw_share)
  5046. {
  5047. struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
  5048. i40e_status ret;
  5049. int i;
  5050. bw_data.tc_valid_bits = ch->enabled_tc;
  5051. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
  5052. bw_data.tc_bw_credits[i] = bw_share[i];
  5053. ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, ch->seid,
  5054. &bw_data, NULL);
  5055. if (ret) {
  5056. dev_info(&vsi->back->pdev->dev,
  5057. "Config VSI BW allocation per TC failed, aq_err: %d for new_vsi->seid %u\n",
  5058. vsi->back->hw.aq.asq_last_status, ch->seid);
  5059. return -EINVAL;
  5060. }
  5061. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
  5062. ch->info.qs_handle[i] = bw_data.qs_handles[i];
  5063. return 0;
  5064. }
  5065. /**
  5066. * i40e_channel_config_tx_ring - config TX ring associated with new channel
  5067. * @pf: ptr to PF device
  5068. * @vsi: the VSI being setup
  5069. * @ch: ptr to channel structure
  5070. *
  5071. * Configure TX rings associated with channel (VSI) since queues are being
  5072. * from parent VSI.
  5073. **/
  5074. static int i40e_channel_config_tx_ring(struct i40e_pf *pf,
  5075. struct i40e_vsi *vsi,
  5076. struct i40e_channel *ch)
  5077. {
  5078. i40e_status ret;
  5079. int i;
  5080. u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
  5081. /* Enable ETS TCs with equal BW Share for now across all VSIs */
  5082. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  5083. if (ch->enabled_tc & BIT(i))
  5084. bw_share[i] = 1;
  5085. }
  5086. /* configure BW for new VSI */
  5087. ret = i40e_channel_config_bw(vsi, ch, bw_share);
  5088. if (ret) {
  5089. dev_info(&vsi->back->pdev->dev,
  5090. "Failed configuring TC map %d for channel (seid %u)\n",
  5091. ch->enabled_tc, ch->seid);
  5092. return ret;
  5093. }
  5094. for (i = 0; i < ch->num_queue_pairs; i++) {
  5095. struct i40e_ring *tx_ring, *rx_ring;
  5096. u16 pf_q;
  5097. pf_q = ch->base_queue + i;
  5098. /* Get to TX ring ptr of main VSI, for re-setup TX queue
  5099. * context
  5100. */
  5101. tx_ring = vsi->tx_rings[pf_q];
  5102. tx_ring->ch = ch;
  5103. /* Get the RX ring ptr */
  5104. rx_ring = vsi->rx_rings[pf_q];
  5105. rx_ring->ch = ch;
  5106. }
  5107. return 0;
  5108. }
  5109. /**
  5110. * i40e_setup_hw_channel - setup new channel
  5111. * @pf: ptr to PF device
  5112. * @vsi: the VSI being setup
  5113. * @ch: ptr to channel structure
  5114. * @uplink_seid: underlying HW switching element (VEB) ID
  5115. * @type: type of channel to be created (VMDq2/VF)
  5116. *
  5117. * Setup new channel (VSI) based on specified type (VMDq2/VF)
  5118. * and configures TX rings accordingly
  5119. **/
  5120. static inline int i40e_setup_hw_channel(struct i40e_pf *pf,
  5121. struct i40e_vsi *vsi,
  5122. struct i40e_channel *ch,
  5123. u16 uplink_seid, u8 type)
  5124. {
  5125. int ret;
  5126. ch->initialized = false;
  5127. ch->base_queue = vsi->next_base_queue;
  5128. ch->type = type;
  5129. /* Proceed with creation of channel (VMDq2) VSI */
  5130. ret = i40e_add_channel(pf, uplink_seid, ch);
  5131. if (ret) {
  5132. dev_info(&pf->pdev->dev,
  5133. "failed to add_channel using uplink_seid %u\n",
  5134. uplink_seid);
  5135. return ret;
  5136. }
  5137. /* Mark the successful creation of channel */
  5138. ch->initialized = true;
  5139. /* Reconfigure TX queues using QTX_CTL register */
  5140. ret = i40e_channel_config_tx_ring(pf, vsi, ch);
  5141. if (ret) {
  5142. dev_info(&pf->pdev->dev,
  5143. "failed to configure TX rings for channel %u\n",
  5144. ch->seid);
  5145. return ret;
  5146. }
  5147. /* update 'next_base_queue' */
  5148. vsi->next_base_queue = vsi->next_base_queue + ch->num_queue_pairs;
  5149. dev_dbg(&pf->pdev->dev,
  5150. "Added channel: vsi_seid %u, vsi_number %u, stat_counter_idx %u, num_queue_pairs %u, pf->next_base_queue %d\n",
  5151. ch->seid, ch->vsi_number, ch->stat_counter_idx,
  5152. ch->num_queue_pairs,
  5153. vsi->next_base_queue);
  5154. return ret;
  5155. }
  5156. /**
  5157. * i40e_setup_channel - setup new channel using uplink element
  5158. * @pf: ptr to PF device
  5159. * @type: type of channel to be created (VMDq2/VF)
  5160. * @uplink_seid: underlying HW switching element (VEB) ID
  5161. * @ch: ptr to channel structure
  5162. *
  5163. * Setup new channel (VSI) based on specified type (VMDq2/VF)
  5164. * and uplink switching element (uplink_seid)
  5165. **/
  5166. static bool i40e_setup_channel(struct i40e_pf *pf, struct i40e_vsi *vsi,
  5167. struct i40e_channel *ch)
  5168. {
  5169. u8 vsi_type;
  5170. u16 seid;
  5171. int ret;
  5172. if (vsi->type == I40E_VSI_MAIN) {
  5173. vsi_type = I40E_VSI_VMDQ2;
  5174. } else {
  5175. dev_err(&pf->pdev->dev, "unsupported parent vsi type(%d)\n",
  5176. vsi->type);
  5177. return false;
  5178. }
  5179. /* underlying switching element */
  5180. seid = pf->vsi[pf->lan_vsi]->uplink_seid;
  5181. /* create channel (VSI), configure TX rings */
  5182. ret = i40e_setup_hw_channel(pf, vsi, ch, seid, vsi_type);
  5183. if (ret) {
  5184. dev_err(&pf->pdev->dev, "failed to setup hw_channel\n");
  5185. return false;
  5186. }
  5187. return ch->initialized ? true : false;
  5188. }
  5189. /**
  5190. * i40e_validate_and_set_switch_mode - sets up switch mode correctly
  5191. * @vsi: ptr to VSI which has PF backing
  5192. *
  5193. * Sets up switch mode correctly if it needs to be changed and perform
  5194. * what are allowed modes.
  5195. **/
  5196. static int i40e_validate_and_set_switch_mode(struct i40e_vsi *vsi)
  5197. {
  5198. u8 mode;
  5199. struct i40e_pf *pf = vsi->back;
  5200. struct i40e_hw *hw = &pf->hw;
  5201. int ret;
  5202. ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_dev_capabilities);
  5203. if (ret)
  5204. return -EINVAL;
  5205. if (hw->dev_caps.switch_mode) {
  5206. /* if switch mode is set, support mode2 (non-tunneled for
  5207. * cloud filter) for now
  5208. */
  5209. u32 switch_mode = hw->dev_caps.switch_mode &
  5210. I40E_SWITCH_MODE_MASK;
  5211. if (switch_mode >= I40E_CLOUD_FILTER_MODE1) {
  5212. if (switch_mode == I40E_CLOUD_FILTER_MODE2)
  5213. return 0;
  5214. dev_err(&pf->pdev->dev,
  5215. "Invalid switch_mode (%d), only non-tunneled mode for cloud filter is supported\n",
  5216. hw->dev_caps.switch_mode);
  5217. return -EINVAL;
  5218. }
  5219. }
  5220. /* Set Bit 7 to be valid */
  5221. mode = I40E_AQ_SET_SWITCH_BIT7_VALID;
  5222. /* Set L4type for TCP support */
  5223. mode |= I40E_AQ_SET_SWITCH_L4_TYPE_TCP;
  5224. /* Set cloud filter mode */
  5225. mode |= I40E_AQ_SET_SWITCH_MODE_NON_TUNNEL;
  5226. /* Prep mode field for set_switch_config */
  5227. ret = i40e_aq_set_switch_config(hw, pf->last_sw_conf_flags,
  5228. pf->last_sw_conf_valid_flags,
  5229. mode, NULL);
  5230. if (ret && hw->aq.asq_last_status != I40E_AQ_RC_ESRCH)
  5231. dev_err(&pf->pdev->dev,
  5232. "couldn't set switch config bits, err %s aq_err %s\n",
  5233. i40e_stat_str(hw, ret),
  5234. i40e_aq_str(hw,
  5235. hw->aq.asq_last_status));
  5236. return ret;
  5237. }
  5238. /**
  5239. * i40e_create_queue_channel - function to create channel
  5240. * @vsi: VSI to be configured
  5241. * @ch: ptr to channel (it contains channel specific params)
  5242. *
  5243. * This function creates channel (VSI) using num_queues specified by user,
  5244. * reconfigs RSS if needed.
  5245. **/
  5246. int i40e_create_queue_channel(struct i40e_vsi *vsi,
  5247. struct i40e_channel *ch)
  5248. {
  5249. struct i40e_pf *pf = vsi->back;
  5250. bool reconfig_rss;
  5251. int err;
  5252. if (!ch)
  5253. return -EINVAL;
  5254. if (!ch->num_queue_pairs) {
  5255. dev_err(&pf->pdev->dev, "Invalid num_queues requested: %d\n",
  5256. ch->num_queue_pairs);
  5257. return -EINVAL;
  5258. }
  5259. /* validate user requested num_queues for channel */
  5260. err = i40e_validate_num_queues(pf, ch->num_queue_pairs, vsi,
  5261. &reconfig_rss);
  5262. if (err) {
  5263. dev_info(&pf->pdev->dev, "Failed to validate num_queues (%d)\n",
  5264. ch->num_queue_pairs);
  5265. return -EINVAL;
  5266. }
  5267. /* By default we are in VEPA mode, if this is the first VF/VMDq
  5268. * VSI to be added switch to VEB mode.
  5269. */
  5270. if ((!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) ||
  5271. (!i40e_is_any_channel(vsi))) {
  5272. if (!is_power_of_2(vsi->tc_config.tc_info[0].qcount)) {
  5273. dev_dbg(&pf->pdev->dev,
  5274. "Failed to create channel. Override queues (%u) not power of 2\n",
  5275. vsi->tc_config.tc_info[0].qcount);
  5276. return -EINVAL;
  5277. }
  5278. if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
  5279. pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
  5280. if (vsi->type == I40E_VSI_MAIN) {
  5281. if (pf->flags & I40E_FLAG_TC_MQPRIO)
  5282. i40e_do_reset(pf, I40E_PF_RESET_FLAG,
  5283. true);
  5284. else
  5285. i40e_do_reset_safe(pf,
  5286. I40E_PF_RESET_FLAG);
  5287. }
  5288. }
  5289. /* now onwards for main VSI, number of queues will be value
  5290. * of TC0's queue count
  5291. */
  5292. }
  5293. /* By this time, vsi->cnt_q_avail shall be set to non-zero and
  5294. * it should be more than num_queues
  5295. */
  5296. if (!vsi->cnt_q_avail || vsi->cnt_q_avail < ch->num_queue_pairs) {
  5297. dev_dbg(&pf->pdev->dev,
  5298. "Error: cnt_q_avail (%u) less than num_queues %d\n",
  5299. vsi->cnt_q_avail, ch->num_queue_pairs);
  5300. return -EINVAL;
  5301. }
  5302. /* reconfig_rss only if vsi type is MAIN_VSI */
  5303. if (reconfig_rss && (vsi->type == I40E_VSI_MAIN)) {
  5304. err = i40e_vsi_reconfig_rss(vsi, ch->num_queue_pairs);
  5305. if (err) {
  5306. dev_info(&pf->pdev->dev,
  5307. "Error: unable to reconfig rss for num_queues (%u)\n",
  5308. ch->num_queue_pairs);
  5309. return -EINVAL;
  5310. }
  5311. }
  5312. if (!i40e_setup_channel(pf, vsi, ch)) {
  5313. dev_info(&pf->pdev->dev, "Failed to setup channel\n");
  5314. return -EINVAL;
  5315. }
  5316. dev_info(&pf->pdev->dev,
  5317. "Setup channel (id:%u) utilizing num_queues %d\n",
  5318. ch->seid, ch->num_queue_pairs);
  5319. /* configure VSI for BW limit */
  5320. if (ch->max_tx_rate) {
  5321. u64 credits = ch->max_tx_rate;
  5322. if (i40e_set_bw_limit(vsi, ch->seid, ch->max_tx_rate))
  5323. return -EINVAL;
  5324. do_div(credits, I40E_BW_CREDIT_DIVISOR);
  5325. dev_dbg(&pf->pdev->dev,
  5326. "Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
  5327. ch->max_tx_rate,
  5328. credits,
  5329. ch->seid);
  5330. }
  5331. /* in case of VF, this will be main SRIOV VSI */
  5332. ch->parent_vsi = vsi;
  5333. /* and update main_vsi's count for queue_available to use */
  5334. vsi->cnt_q_avail -= ch->num_queue_pairs;
  5335. return 0;
  5336. }
  5337. /**
  5338. * i40e_configure_queue_channels - Add queue channel for the given TCs
  5339. * @vsi: VSI to be configured
  5340. *
  5341. * Configures queue channel mapping to the given TCs
  5342. **/
  5343. static int i40e_configure_queue_channels(struct i40e_vsi *vsi)
  5344. {
  5345. struct i40e_channel *ch;
  5346. u64 max_rate = 0;
  5347. int ret = 0, i;
  5348. /* Create app vsi with the TCs. Main VSI with TC0 is already set up */
  5349. vsi->tc_seid_map[0] = vsi->seid;
  5350. for (i = 1; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  5351. if (vsi->tc_config.enabled_tc & BIT(i)) {
  5352. ch = kzalloc(sizeof(*ch), GFP_KERNEL);
  5353. if (!ch) {
  5354. ret = -ENOMEM;
  5355. goto err_free;
  5356. }
  5357. INIT_LIST_HEAD(&ch->list);
  5358. ch->num_queue_pairs =
  5359. vsi->tc_config.tc_info[i].qcount;
  5360. ch->base_queue =
  5361. vsi->tc_config.tc_info[i].qoffset;
  5362. /* Bandwidth limit through tc interface is in bytes/s,
  5363. * change to Mbit/s
  5364. */
  5365. max_rate = vsi->mqprio_qopt.max_rate[i];
  5366. do_div(max_rate, I40E_BW_MBPS_DIVISOR);
  5367. ch->max_tx_rate = max_rate;
  5368. list_add_tail(&ch->list, &vsi->ch_list);
  5369. ret = i40e_create_queue_channel(vsi, ch);
  5370. if (ret) {
  5371. dev_err(&vsi->back->pdev->dev,
  5372. "Failed creating queue channel with TC%d: queues %d\n",
  5373. i, ch->num_queue_pairs);
  5374. goto err_free;
  5375. }
  5376. vsi->tc_seid_map[i] = ch->seid;
  5377. }
  5378. }
  5379. return ret;
  5380. err_free:
  5381. i40e_remove_queue_channels(vsi);
  5382. return ret;
  5383. }
  5384. /**
  5385. * i40e_veb_config_tc - Configure TCs for given VEB
  5386. * @veb: given VEB
  5387. * @enabled_tc: TC bitmap
  5388. *
  5389. * Configures given TC bitmap for VEB (switching) element
  5390. **/
  5391. int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
  5392. {
  5393. struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
  5394. struct i40e_pf *pf = veb->pf;
  5395. int ret = 0;
  5396. int i;
  5397. /* No TCs or already enabled TCs just return */
  5398. if (!enabled_tc || veb->enabled_tc == enabled_tc)
  5399. return ret;
  5400. bw_data.tc_valid_bits = enabled_tc;
  5401. /* bw_data.absolute_credits is not set (relative) */
  5402. /* Enable ETS TCs with equal BW Share for now */
  5403. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  5404. if (enabled_tc & BIT(i))
  5405. bw_data.tc_bw_share_credits[i] = 1;
  5406. }
  5407. ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
  5408. &bw_data, NULL);
  5409. if (ret) {
  5410. dev_info(&pf->pdev->dev,
  5411. "VEB bw config failed, err %s aq_err %s\n",
  5412. i40e_stat_str(&pf->hw, ret),
  5413. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  5414. goto out;
  5415. }
  5416. /* Update the BW information */
  5417. ret = i40e_veb_get_bw_info(veb);
  5418. if (ret) {
  5419. dev_info(&pf->pdev->dev,
  5420. "Failed getting veb bw config, err %s aq_err %s\n",
  5421. i40e_stat_str(&pf->hw, ret),
  5422. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  5423. }
  5424. out:
  5425. return ret;
  5426. }
  5427. #ifdef CONFIG_I40E_DCB
  5428. /**
  5429. * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
  5430. * @pf: PF struct
  5431. *
  5432. * Reconfigure VEB/VSIs on a given PF; it is assumed that
  5433. * the caller would've quiesce all the VSIs before calling
  5434. * this function
  5435. **/
  5436. static void i40e_dcb_reconfigure(struct i40e_pf *pf)
  5437. {
  5438. u8 tc_map = 0;
  5439. int ret;
  5440. u8 v;
  5441. /* Enable the TCs available on PF to all VEBs */
  5442. tc_map = i40e_pf_get_tc_map(pf);
  5443. for (v = 0; v < I40E_MAX_VEB; v++) {
  5444. if (!pf->veb[v])
  5445. continue;
  5446. ret = i40e_veb_config_tc(pf->veb[v], tc_map);
  5447. if (ret) {
  5448. dev_info(&pf->pdev->dev,
  5449. "Failed configuring TC for VEB seid=%d\n",
  5450. pf->veb[v]->seid);
  5451. /* Will try to configure as many components */
  5452. }
  5453. }
  5454. /* Update each VSI */
  5455. for (v = 0; v < pf->num_alloc_vsi; v++) {
  5456. if (!pf->vsi[v])
  5457. continue;
  5458. /* - Enable all TCs for the LAN VSI
  5459. * - For all others keep them at TC0 for now
  5460. */
  5461. if (v == pf->lan_vsi)
  5462. tc_map = i40e_pf_get_tc_map(pf);
  5463. else
  5464. tc_map = I40E_DEFAULT_TRAFFIC_CLASS;
  5465. ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
  5466. if (ret) {
  5467. dev_info(&pf->pdev->dev,
  5468. "Failed configuring TC for VSI seid=%d\n",
  5469. pf->vsi[v]->seid);
  5470. /* Will try to configure as many components */
  5471. } else {
  5472. /* Re-configure VSI vectors based on updated TC map */
  5473. i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
  5474. if (pf->vsi[v]->netdev)
  5475. i40e_dcbnl_set_all(pf->vsi[v]);
  5476. }
  5477. }
  5478. }
  5479. /**
  5480. * i40e_resume_port_tx - Resume port Tx
  5481. * @pf: PF struct
  5482. *
  5483. * Resume a port's Tx and issue a PF reset in case of failure to
  5484. * resume.
  5485. **/
  5486. static int i40e_resume_port_tx(struct i40e_pf *pf)
  5487. {
  5488. struct i40e_hw *hw = &pf->hw;
  5489. int ret;
  5490. ret = i40e_aq_resume_port_tx(hw, NULL);
  5491. if (ret) {
  5492. dev_info(&pf->pdev->dev,
  5493. "Resume Port Tx failed, err %s aq_err %s\n",
  5494. i40e_stat_str(&pf->hw, ret),
  5495. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  5496. /* Schedule PF reset to recover */
  5497. set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
  5498. i40e_service_event_schedule(pf);
  5499. }
  5500. return ret;
  5501. }
  5502. /**
  5503. * i40e_init_pf_dcb - Initialize DCB configuration
  5504. * @pf: PF being configured
  5505. *
  5506. * Query the current DCB configuration and cache it
  5507. * in the hardware structure
  5508. **/
  5509. static int i40e_init_pf_dcb(struct i40e_pf *pf)
  5510. {
  5511. struct i40e_hw *hw = &pf->hw;
  5512. int err = 0;
  5513. /* Do not enable DCB for SW1 and SW2 images even if the FW is capable
  5514. * Also do not enable DCBx if FW LLDP agent is disabled
  5515. */
  5516. if ((pf->hw_features & I40E_HW_NO_DCB_SUPPORT) ||
  5517. (pf->flags & I40E_FLAG_DISABLE_FW_LLDP))
  5518. goto out;
  5519. /* Get the initial DCB configuration */
  5520. err = i40e_init_dcb(hw);
  5521. if (!err) {
  5522. /* Device/Function is not DCBX capable */
  5523. if ((!hw->func_caps.dcb) ||
  5524. (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
  5525. dev_info(&pf->pdev->dev,
  5526. "DCBX offload is not supported or is disabled for this PF.\n");
  5527. } else {
  5528. /* When status is not DISABLED then DCBX in FW */
  5529. pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
  5530. DCB_CAP_DCBX_VER_IEEE;
  5531. pf->flags |= I40E_FLAG_DCB_CAPABLE;
  5532. /* Enable DCB tagging only when more than one TC
  5533. * or explicitly disable if only one TC
  5534. */
  5535. if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
  5536. pf->flags |= I40E_FLAG_DCB_ENABLED;
  5537. else
  5538. pf->flags &= ~I40E_FLAG_DCB_ENABLED;
  5539. dev_dbg(&pf->pdev->dev,
  5540. "DCBX offload is supported for this PF.\n");
  5541. }
  5542. } else if (pf->hw.aq.asq_last_status == I40E_AQ_RC_EPERM) {
  5543. dev_info(&pf->pdev->dev, "FW LLDP disabled for this PF.\n");
  5544. pf->flags |= I40E_FLAG_DISABLE_FW_LLDP;
  5545. } else {
  5546. dev_info(&pf->pdev->dev,
  5547. "Query for DCB configuration failed, err %s aq_err %s\n",
  5548. i40e_stat_str(&pf->hw, err),
  5549. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  5550. }
  5551. out:
  5552. return err;
  5553. }
  5554. #endif /* CONFIG_I40E_DCB */
  5555. #define SPEED_SIZE 14
  5556. #define FC_SIZE 8
  5557. /**
  5558. * i40e_print_link_message - print link up or down
  5559. * @vsi: the VSI for which link needs a message
  5560. */
  5561. void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
  5562. {
  5563. enum i40e_aq_link_speed new_speed;
  5564. struct i40e_pf *pf = vsi->back;
  5565. char *speed = "Unknown";
  5566. char *fc = "Unknown";
  5567. char *fec = "";
  5568. char *req_fec = "";
  5569. char *an = "";
  5570. new_speed = pf->hw.phy.link_info.link_speed;
  5571. if ((vsi->current_isup == isup) && (vsi->current_speed == new_speed))
  5572. return;
  5573. vsi->current_isup = isup;
  5574. vsi->current_speed = new_speed;
  5575. if (!isup) {
  5576. netdev_info(vsi->netdev, "NIC Link is Down\n");
  5577. return;
  5578. }
  5579. /* Warn user if link speed on NPAR enabled partition is not at
  5580. * least 10GB
  5581. */
  5582. if (pf->hw.func_caps.npar_enable &&
  5583. (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
  5584. pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
  5585. netdev_warn(vsi->netdev,
  5586. "The partition detected link speed that is less than 10Gbps\n");
  5587. switch (pf->hw.phy.link_info.link_speed) {
  5588. case I40E_LINK_SPEED_40GB:
  5589. speed = "40 G";
  5590. break;
  5591. case I40E_LINK_SPEED_20GB:
  5592. speed = "20 G";
  5593. break;
  5594. case I40E_LINK_SPEED_25GB:
  5595. speed = "25 G";
  5596. break;
  5597. case I40E_LINK_SPEED_10GB:
  5598. speed = "10 G";
  5599. break;
  5600. case I40E_LINK_SPEED_1GB:
  5601. speed = "1000 M";
  5602. break;
  5603. case I40E_LINK_SPEED_100MB:
  5604. speed = "100 M";
  5605. break;
  5606. default:
  5607. break;
  5608. }
  5609. switch (pf->hw.fc.current_mode) {
  5610. case I40E_FC_FULL:
  5611. fc = "RX/TX";
  5612. break;
  5613. case I40E_FC_TX_PAUSE:
  5614. fc = "TX";
  5615. break;
  5616. case I40E_FC_RX_PAUSE:
  5617. fc = "RX";
  5618. break;
  5619. default:
  5620. fc = "None";
  5621. break;
  5622. }
  5623. if (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_25GB) {
  5624. req_fec = ", Requested FEC: None";
  5625. fec = ", FEC: None";
  5626. an = ", Autoneg: False";
  5627. if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
  5628. an = ", Autoneg: True";
  5629. if (pf->hw.phy.link_info.fec_info &
  5630. I40E_AQ_CONFIG_FEC_KR_ENA)
  5631. fec = ", FEC: CL74 FC-FEC/BASE-R";
  5632. else if (pf->hw.phy.link_info.fec_info &
  5633. I40E_AQ_CONFIG_FEC_RS_ENA)
  5634. fec = ", FEC: CL108 RS-FEC";
  5635. /* 'CL108 RS-FEC' should be displayed when RS is requested, or
  5636. * both RS and FC are requested
  5637. */
  5638. if (vsi->back->hw.phy.link_info.req_fec_info &
  5639. (I40E_AQ_REQUEST_FEC_KR | I40E_AQ_REQUEST_FEC_RS)) {
  5640. if (vsi->back->hw.phy.link_info.req_fec_info &
  5641. I40E_AQ_REQUEST_FEC_RS)
  5642. req_fec = ", Requested FEC: CL108 RS-FEC";
  5643. else
  5644. req_fec = ", Requested FEC: CL74 FC-FEC/BASE-R";
  5645. }
  5646. }
  5647. netdev_info(vsi->netdev, "NIC Link is Up, %sbps Full Duplex%s%s%s, Flow Control: %s\n",
  5648. speed, req_fec, fec, an, fc);
  5649. }
  5650. /**
  5651. * i40e_up_complete - Finish the last steps of bringing up a connection
  5652. * @vsi: the VSI being configured
  5653. **/
  5654. static int i40e_up_complete(struct i40e_vsi *vsi)
  5655. {
  5656. struct i40e_pf *pf = vsi->back;
  5657. int err;
  5658. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  5659. i40e_vsi_configure_msix(vsi);
  5660. else
  5661. i40e_configure_msi_and_legacy(vsi);
  5662. /* start rings */
  5663. err = i40e_vsi_start_rings(vsi);
  5664. if (err)
  5665. return err;
  5666. clear_bit(__I40E_VSI_DOWN, vsi->state);
  5667. i40e_napi_enable_all(vsi);
  5668. i40e_vsi_enable_irq(vsi);
  5669. if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
  5670. (vsi->netdev)) {
  5671. i40e_print_link_message(vsi, true);
  5672. netif_tx_start_all_queues(vsi->netdev);
  5673. netif_carrier_on(vsi->netdev);
  5674. }
  5675. /* replay FDIR SB filters */
  5676. if (vsi->type == I40E_VSI_FDIR) {
  5677. /* reset fd counters */
  5678. pf->fd_add_err = 0;
  5679. pf->fd_atr_cnt = 0;
  5680. i40e_fdir_filter_restore(vsi);
  5681. }
  5682. /* On the next run of the service_task, notify any clients of the new
  5683. * opened netdev
  5684. */
  5685. set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
  5686. i40e_service_event_schedule(pf);
  5687. return 0;
  5688. }
  5689. /**
  5690. * i40e_vsi_reinit_locked - Reset the VSI
  5691. * @vsi: the VSI being configured
  5692. *
  5693. * Rebuild the ring structs after some configuration
  5694. * has changed, e.g. MTU size.
  5695. **/
  5696. static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
  5697. {
  5698. struct i40e_pf *pf = vsi->back;
  5699. WARN_ON(in_interrupt());
  5700. while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state))
  5701. usleep_range(1000, 2000);
  5702. i40e_down(vsi);
  5703. i40e_up(vsi);
  5704. clear_bit(__I40E_CONFIG_BUSY, pf->state);
  5705. }
  5706. /**
  5707. * i40e_up - Bring the connection back up after being down
  5708. * @vsi: the VSI being configured
  5709. **/
  5710. int i40e_up(struct i40e_vsi *vsi)
  5711. {
  5712. int err;
  5713. err = i40e_vsi_configure(vsi);
  5714. if (!err)
  5715. err = i40e_up_complete(vsi);
  5716. return err;
  5717. }
  5718. /**
  5719. * i40e_force_link_state - Force the link status
  5720. * @pf: board private structure
  5721. * @is_up: whether the link state should be forced up or down
  5722. **/
  5723. static i40e_status i40e_force_link_state(struct i40e_pf *pf, bool is_up)
  5724. {
  5725. struct i40e_aq_get_phy_abilities_resp abilities;
  5726. struct i40e_aq_set_phy_config config = {0};
  5727. struct i40e_hw *hw = &pf->hw;
  5728. i40e_status err;
  5729. u64 mask;
  5730. /* Get the current phy config */
  5731. err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities,
  5732. NULL);
  5733. if (err) {
  5734. dev_err(&pf->pdev->dev,
  5735. "failed to get phy cap., ret = %s last_status = %s\n",
  5736. i40e_stat_str(hw, err),
  5737. i40e_aq_str(hw, hw->aq.asq_last_status));
  5738. return err;
  5739. }
  5740. /* If link needs to go up, but was not forced to go down,
  5741. * no need for a flap
  5742. */
  5743. if (is_up && abilities.phy_type != 0)
  5744. return I40E_SUCCESS;
  5745. /* To force link we need to set bits for all supported PHY types,
  5746. * but there are now more than 32, so we need to split the bitmap
  5747. * across two fields.
  5748. */
  5749. mask = I40E_PHY_TYPES_BITMASK;
  5750. config.phy_type = is_up ? cpu_to_le32((u32)(mask & 0xffffffff)) : 0;
  5751. config.phy_type_ext = is_up ? (u8)((mask >> 32) & 0xff) : 0;
  5752. /* Copy the old settings, except of phy_type */
  5753. config.abilities = abilities.abilities;
  5754. config.link_speed = abilities.link_speed;
  5755. config.eee_capability = abilities.eee_capability;
  5756. config.eeer = abilities.eeer_val;
  5757. config.low_power_ctrl = abilities.d3_lpan;
  5758. err = i40e_aq_set_phy_config(hw, &config, NULL);
  5759. if (err) {
  5760. dev_err(&pf->pdev->dev,
  5761. "set phy config ret = %s last_status = %s\n",
  5762. i40e_stat_str(&pf->hw, err),
  5763. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  5764. return err;
  5765. }
  5766. /* Update the link info */
  5767. err = i40e_update_link_info(hw);
  5768. if (err) {
  5769. /* Wait a little bit (on 40G cards it sometimes takes a really
  5770. * long time for link to come back from the atomic reset)
  5771. * and try once more
  5772. */
  5773. msleep(1000);
  5774. i40e_update_link_info(hw);
  5775. }
  5776. i40e_aq_set_link_restart_an(hw, true, NULL);
  5777. return I40E_SUCCESS;
  5778. }
  5779. /**
  5780. * i40e_down - Shutdown the connection processing
  5781. * @vsi: the VSI being stopped
  5782. **/
  5783. void i40e_down(struct i40e_vsi *vsi)
  5784. {
  5785. int i;
  5786. /* It is assumed that the caller of this function
  5787. * sets the vsi->state __I40E_VSI_DOWN bit.
  5788. */
  5789. if (vsi->netdev) {
  5790. netif_carrier_off(vsi->netdev);
  5791. netif_tx_disable(vsi->netdev);
  5792. }
  5793. i40e_vsi_disable_irq(vsi);
  5794. i40e_vsi_stop_rings(vsi);
  5795. if (vsi->type == I40E_VSI_MAIN &&
  5796. vsi->back->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED)
  5797. i40e_force_link_state(vsi->back, false);
  5798. i40e_napi_disable_all(vsi);
  5799. for (i = 0; i < vsi->num_queue_pairs; i++) {
  5800. i40e_clean_tx_ring(vsi->tx_rings[i]);
  5801. if (i40e_enabled_xdp_vsi(vsi))
  5802. i40e_clean_tx_ring(vsi->xdp_rings[i]);
  5803. i40e_clean_rx_ring(vsi->rx_rings[i]);
  5804. }
  5805. }
  5806. /**
  5807. * i40e_validate_mqprio_qopt- validate queue mapping info
  5808. * @vsi: the VSI being configured
  5809. * @mqprio_qopt: queue parametrs
  5810. **/
  5811. static int i40e_validate_mqprio_qopt(struct i40e_vsi *vsi,
  5812. struct tc_mqprio_qopt_offload *mqprio_qopt)
  5813. {
  5814. u64 sum_max_rate = 0;
  5815. u64 max_rate = 0;
  5816. int i;
  5817. if (mqprio_qopt->qopt.offset[0] != 0 ||
  5818. mqprio_qopt->qopt.num_tc < 1 ||
  5819. mqprio_qopt->qopt.num_tc > I40E_MAX_TRAFFIC_CLASS)
  5820. return -EINVAL;
  5821. for (i = 0; ; i++) {
  5822. if (!mqprio_qopt->qopt.count[i])
  5823. return -EINVAL;
  5824. if (mqprio_qopt->min_rate[i]) {
  5825. dev_err(&vsi->back->pdev->dev,
  5826. "Invalid min tx rate (greater than 0) specified\n");
  5827. return -EINVAL;
  5828. }
  5829. max_rate = mqprio_qopt->max_rate[i];
  5830. do_div(max_rate, I40E_BW_MBPS_DIVISOR);
  5831. sum_max_rate += max_rate;
  5832. if (i >= mqprio_qopt->qopt.num_tc - 1)
  5833. break;
  5834. if (mqprio_qopt->qopt.offset[i + 1] !=
  5835. (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i]))
  5836. return -EINVAL;
  5837. }
  5838. if (vsi->num_queue_pairs <
  5839. (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i])) {
  5840. return -EINVAL;
  5841. }
  5842. if (sum_max_rate > i40e_get_link_speed(vsi)) {
  5843. dev_err(&vsi->back->pdev->dev,
  5844. "Invalid max tx rate specified\n");
  5845. return -EINVAL;
  5846. }
  5847. return 0;
  5848. }
  5849. /**
  5850. * i40e_vsi_set_default_tc_config - set default values for tc configuration
  5851. * @vsi: the VSI being configured
  5852. **/
  5853. static void i40e_vsi_set_default_tc_config(struct i40e_vsi *vsi)
  5854. {
  5855. u16 qcount;
  5856. int i;
  5857. /* Only TC0 is enabled */
  5858. vsi->tc_config.numtc = 1;
  5859. vsi->tc_config.enabled_tc = 1;
  5860. qcount = min_t(int, vsi->alloc_queue_pairs,
  5861. i40e_pf_get_max_q_per_tc(vsi->back));
  5862. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  5863. /* For the TC that is not enabled set the offset to to default
  5864. * queue and allocate one queue for the given TC.
  5865. */
  5866. vsi->tc_config.tc_info[i].qoffset = 0;
  5867. if (i == 0)
  5868. vsi->tc_config.tc_info[i].qcount = qcount;
  5869. else
  5870. vsi->tc_config.tc_info[i].qcount = 1;
  5871. vsi->tc_config.tc_info[i].netdev_tc = 0;
  5872. }
  5873. }
  5874. /**
  5875. * i40e_setup_tc - configure multiple traffic classes
  5876. * @netdev: net device to configure
  5877. * @type_data: tc offload data
  5878. **/
  5879. static int i40e_setup_tc(struct net_device *netdev, void *type_data)
  5880. {
  5881. struct tc_mqprio_qopt_offload *mqprio_qopt = type_data;
  5882. struct i40e_netdev_priv *np = netdev_priv(netdev);
  5883. struct i40e_vsi *vsi = np->vsi;
  5884. struct i40e_pf *pf = vsi->back;
  5885. u8 enabled_tc = 0, num_tc, hw;
  5886. bool need_reset = false;
  5887. int ret = -EINVAL;
  5888. u16 mode;
  5889. int i;
  5890. num_tc = mqprio_qopt->qopt.num_tc;
  5891. hw = mqprio_qopt->qopt.hw;
  5892. mode = mqprio_qopt->mode;
  5893. if (!hw) {
  5894. pf->flags &= ~I40E_FLAG_TC_MQPRIO;
  5895. memcpy(&vsi->mqprio_qopt, mqprio_qopt, sizeof(*mqprio_qopt));
  5896. goto config_tc;
  5897. }
  5898. /* Check if MFP enabled */
  5899. if (pf->flags & I40E_FLAG_MFP_ENABLED) {
  5900. netdev_info(netdev,
  5901. "Configuring TC not supported in MFP mode\n");
  5902. return ret;
  5903. }
  5904. switch (mode) {
  5905. case TC_MQPRIO_MODE_DCB:
  5906. pf->flags &= ~I40E_FLAG_TC_MQPRIO;
  5907. /* Check if DCB enabled to continue */
  5908. if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
  5909. netdev_info(netdev,
  5910. "DCB is not enabled for adapter\n");
  5911. return ret;
  5912. }
  5913. /* Check whether tc count is within enabled limit */
  5914. if (num_tc > i40e_pf_get_num_tc(pf)) {
  5915. netdev_info(netdev,
  5916. "TC count greater than enabled on link for adapter\n");
  5917. return ret;
  5918. }
  5919. break;
  5920. case TC_MQPRIO_MODE_CHANNEL:
  5921. if (pf->flags & I40E_FLAG_DCB_ENABLED) {
  5922. netdev_info(netdev,
  5923. "Full offload of TC Mqprio options is not supported when DCB is enabled\n");
  5924. return ret;
  5925. }
  5926. if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
  5927. return ret;
  5928. ret = i40e_validate_mqprio_qopt(vsi, mqprio_qopt);
  5929. if (ret)
  5930. return ret;
  5931. memcpy(&vsi->mqprio_qopt, mqprio_qopt,
  5932. sizeof(*mqprio_qopt));
  5933. pf->flags |= I40E_FLAG_TC_MQPRIO;
  5934. pf->flags &= ~I40E_FLAG_DCB_ENABLED;
  5935. break;
  5936. default:
  5937. return -EINVAL;
  5938. }
  5939. config_tc:
  5940. /* Generate TC map for number of tc requested */
  5941. for (i = 0; i < num_tc; i++)
  5942. enabled_tc |= BIT(i);
  5943. /* Requesting same TC configuration as already enabled */
  5944. if (enabled_tc == vsi->tc_config.enabled_tc &&
  5945. mode != TC_MQPRIO_MODE_CHANNEL)
  5946. return 0;
  5947. /* Quiesce VSI queues */
  5948. i40e_quiesce_vsi(vsi);
  5949. if (!hw && !(pf->flags & I40E_FLAG_TC_MQPRIO))
  5950. i40e_remove_queue_channels(vsi);
  5951. /* Configure VSI for enabled TCs */
  5952. ret = i40e_vsi_config_tc(vsi, enabled_tc);
  5953. if (ret) {
  5954. netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
  5955. vsi->seid);
  5956. need_reset = true;
  5957. goto exit;
  5958. }
  5959. if (pf->flags & I40E_FLAG_TC_MQPRIO) {
  5960. if (vsi->mqprio_qopt.max_rate[0]) {
  5961. u64 max_tx_rate = vsi->mqprio_qopt.max_rate[0];
  5962. do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
  5963. ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
  5964. if (!ret) {
  5965. u64 credits = max_tx_rate;
  5966. do_div(credits, I40E_BW_CREDIT_DIVISOR);
  5967. dev_dbg(&vsi->back->pdev->dev,
  5968. "Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
  5969. max_tx_rate,
  5970. credits,
  5971. vsi->seid);
  5972. } else {
  5973. need_reset = true;
  5974. goto exit;
  5975. }
  5976. }
  5977. ret = i40e_configure_queue_channels(vsi);
  5978. if (ret) {
  5979. netdev_info(netdev,
  5980. "Failed configuring queue channels\n");
  5981. need_reset = true;
  5982. goto exit;
  5983. }
  5984. }
  5985. exit:
  5986. /* Reset the configuration data to defaults, only TC0 is enabled */
  5987. if (need_reset) {
  5988. i40e_vsi_set_default_tc_config(vsi);
  5989. need_reset = false;
  5990. }
  5991. /* Unquiesce VSI */
  5992. i40e_unquiesce_vsi(vsi);
  5993. return ret;
  5994. }
  5995. /**
  5996. * i40e_set_cld_element - sets cloud filter element data
  5997. * @filter: cloud filter rule
  5998. * @cld: ptr to cloud filter element data
  5999. *
  6000. * This is helper function to copy data into cloud filter element
  6001. **/
  6002. static inline void
  6003. i40e_set_cld_element(struct i40e_cloud_filter *filter,
  6004. struct i40e_aqc_cloud_filters_element_data *cld)
  6005. {
  6006. int i, j;
  6007. u32 ipa;
  6008. memset(cld, 0, sizeof(*cld));
  6009. ether_addr_copy(cld->outer_mac, filter->dst_mac);
  6010. ether_addr_copy(cld->inner_mac, filter->src_mac);
  6011. if (filter->n_proto != ETH_P_IP && filter->n_proto != ETH_P_IPV6)
  6012. return;
  6013. if (filter->n_proto == ETH_P_IPV6) {
  6014. #define IPV6_MAX_INDEX (ARRAY_SIZE(filter->dst_ipv6) - 1)
  6015. for (i = 0, j = 0; i < ARRAY_SIZE(filter->dst_ipv6);
  6016. i++, j += 2) {
  6017. ipa = be32_to_cpu(filter->dst_ipv6[IPV6_MAX_INDEX - i]);
  6018. ipa = cpu_to_le32(ipa);
  6019. memcpy(&cld->ipaddr.raw_v6.data[j], &ipa, sizeof(ipa));
  6020. }
  6021. } else {
  6022. ipa = be32_to_cpu(filter->dst_ipv4);
  6023. memcpy(&cld->ipaddr.v4.data, &ipa, sizeof(ipa));
  6024. }
  6025. cld->inner_vlan = cpu_to_le16(ntohs(filter->vlan_id));
  6026. /* tenant_id is not supported by FW now, once the support is enabled
  6027. * fill the cld->tenant_id with cpu_to_le32(filter->tenant_id)
  6028. */
  6029. if (filter->tenant_id)
  6030. return;
  6031. }
  6032. /**
  6033. * i40e_add_del_cloud_filter - Add/del cloud filter
  6034. * @vsi: pointer to VSI
  6035. * @filter: cloud filter rule
  6036. * @add: if true, add, if false, delete
  6037. *
  6038. * Add or delete a cloud filter for a specific flow spec.
  6039. * Returns 0 if the filter were successfully added.
  6040. **/
  6041. int i40e_add_del_cloud_filter(struct i40e_vsi *vsi,
  6042. struct i40e_cloud_filter *filter, bool add)
  6043. {
  6044. struct i40e_aqc_cloud_filters_element_data cld_filter;
  6045. struct i40e_pf *pf = vsi->back;
  6046. int ret;
  6047. static const u16 flag_table[128] = {
  6048. [I40E_CLOUD_FILTER_FLAGS_OMAC] =
  6049. I40E_AQC_ADD_CLOUD_FILTER_OMAC,
  6050. [I40E_CLOUD_FILTER_FLAGS_IMAC] =
  6051. I40E_AQC_ADD_CLOUD_FILTER_IMAC,
  6052. [I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN] =
  6053. I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN,
  6054. [I40E_CLOUD_FILTER_FLAGS_IMAC_TEN_ID] =
  6055. I40E_AQC_ADD_CLOUD_FILTER_IMAC_TEN_ID,
  6056. [I40E_CLOUD_FILTER_FLAGS_OMAC_TEN_ID_IMAC] =
  6057. I40E_AQC_ADD_CLOUD_FILTER_OMAC_TEN_ID_IMAC,
  6058. [I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN_TEN_ID] =
  6059. I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN_TEN_ID,
  6060. [I40E_CLOUD_FILTER_FLAGS_IIP] =
  6061. I40E_AQC_ADD_CLOUD_FILTER_IIP,
  6062. };
  6063. if (filter->flags >= ARRAY_SIZE(flag_table))
  6064. return I40E_ERR_CONFIG;
  6065. /* copy element needed to add cloud filter from filter */
  6066. i40e_set_cld_element(filter, &cld_filter);
  6067. if (filter->tunnel_type != I40E_CLOUD_TNL_TYPE_NONE)
  6068. cld_filter.flags = cpu_to_le16(filter->tunnel_type <<
  6069. I40E_AQC_ADD_CLOUD_TNL_TYPE_SHIFT);
  6070. if (filter->n_proto == ETH_P_IPV6)
  6071. cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
  6072. I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
  6073. else
  6074. cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
  6075. I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
  6076. if (add)
  6077. ret = i40e_aq_add_cloud_filters(&pf->hw, filter->seid,
  6078. &cld_filter, 1);
  6079. else
  6080. ret = i40e_aq_rem_cloud_filters(&pf->hw, filter->seid,
  6081. &cld_filter, 1);
  6082. if (ret)
  6083. dev_dbg(&pf->pdev->dev,
  6084. "Failed to %s cloud filter using l4 port %u, err %d aq_err %d\n",
  6085. add ? "add" : "delete", filter->dst_port, ret,
  6086. pf->hw.aq.asq_last_status);
  6087. else
  6088. dev_info(&pf->pdev->dev,
  6089. "%s cloud filter for VSI: %d\n",
  6090. add ? "Added" : "Deleted", filter->seid);
  6091. return ret;
  6092. }
  6093. /**
  6094. * i40e_add_del_cloud_filter_big_buf - Add/del cloud filter using big_buf
  6095. * @vsi: pointer to VSI
  6096. * @filter: cloud filter rule
  6097. * @add: if true, add, if false, delete
  6098. *
  6099. * Add or delete a cloud filter for a specific flow spec using big buffer.
  6100. * Returns 0 if the filter were successfully added.
  6101. **/
  6102. int i40e_add_del_cloud_filter_big_buf(struct i40e_vsi *vsi,
  6103. struct i40e_cloud_filter *filter,
  6104. bool add)
  6105. {
  6106. struct i40e_aqc_cloud_filters_element_bb cld_filter;
  6107. struct i40e_pf *pf = vsi->back;
  6108. int ret;
  6109. /* Both (src/dst) valid mac_addr are not supported */
  6110. if ((is_valid_ether_addr(filter->dst_mac) &&
  6111. is_valid_ether_addr(filter->src_mac)) ||
  6112. (is_multicast_ether_addr(filter->dst_mac) &&
  6113. is_multicast_ether_addr(filter->src_mac)))
  6114. return -EOPNOTSUPP;
  6115. /* Big buffer cloud filter needs 'L4 port' to be non-zero. Also, UDP
  6116. * ports are not supported via big buffer now.
  6117. */
  6118. if (!filter->dst_port || filter->ip_proto == IPPROTO_UDP)
  6119. return -EOPNOTSUPP;
  6120. /* adding filter using src_port/src_ip is not supported at this stage */
  6121. if (filter->src_port || filter->src_ipv4 ||
  6122. !ipv6_addr_any(&filter->ip.v6.src_ip6))
  6123. return -EOPNOTSUPP;
  6124. /* copy element needed to add cloud filter from filter */
  6125. i40e_set_cld_element(filter, &cld_filter.element);
  6126. if (is_valid_ether_addr(filter->dst_mac) ||
  6127. is_valid_ether_addr(filter->src_mac) ||
  6128. is_multicast_ether_addr(filter->dst_mac) ||
  6129. is_multicast_ether_addr(filter->src_mac)) {
  6130. /* MAC + IP : unsupported mode */
  6131. if (filter->dst_ipv4)
  6132. return -EOPNOTSUPP;
  6133. /* since we validated that L4 port must be valid before
  6134. * we get here, start with respective "flags" value
  6135. * and update if vlan is present or not
  6136. */
  6137. cld_filter.element.flags =
  6138. cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_PORT);
  6139. if (filter->vlan_id) {
  6140. cld_filter.element.flags =
  6141. cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_VLAN_PORT);
  6142. }
  6143. } else if (filter->dst_ipv4 ||
  6144. !ipv6_addr_any(&filter->ip.v6.dst_ip6)) {
  6145. cld_filter.element.flags =
  6146. cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_IP_PORT);
  6147. if (filter->n_proto == ETH_P_IPV6)
  6148. cld_filter.element.flags |=
  6149. cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
  6150. else
  6151. cld_filter.element.flags |=
  6152. cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
  6153. } else {
  6154. dev_err(&pf->pdev->dev,
  6155. "either mac or ip has to be valid for cloud filter\n");
  6156. return -EINVAL;
  6157. }
  6158. /* Now copy L4 port in Byte 6..7 in general fields */
  6159. cld_filter.general_fields[I40E_AQC_ADD_CLOUD_FV_FLU_0X16_WORD0] =
  6160. be16_to_cpu(filter->dst_port);
  6161. if (add) {
  6162. /* Validate current device switch mode, change if necessary */
  6163. ret = i40e_validate_and_set_switch_mode(vsi);
  6164. if (ret) {
  6165. dev_err(&pf->pdev->dev,
  6166. "failed to set switch mode, ret %d\n",
  6167. ret);
  6168. return ret;
  6169. }
  6170. ret = i40e_aq_add_cloud_filters_bb(&pf->hw, filter->seid,
  6171. &cld_filter, 1);
  6172. } else {
  6173. ret = i40e_aq_rem_cloud_filters_bb(&pf->hw, filter->seid,
  6174. &cld_filter, 1);
  6175. }
  6176. if (ret)
  6177. dev_dbg(&pf->pdev->dev,
  6178. "Failed to %s cloud filter(big buffer) err %d aq_err %d\n",
  6179. add ? "add" : "delete", ret, pf->hw.aq.asq_last_status);
  6180. else
  6181. dev_info(&pf->pdev->dev,
  6182. "%s cloud filter for VSI: %d, L4 port: %d\n",
  6183. add ? "add" : "delete", filter->seid,
  6184. ntohs(filter->dst_port));
  6185. return ret;
  6186. }
  6187. /**
  6188. * i40e_parse_cls_flower - Parse tc flower filters provided by kernel
  6189. * @vsi: Pointer to VSI
  6190. * @cls_flower: Pointer to struct tc_cls_flower_offload
  6191. * @filter: Pointer to cloud filter structure
  6192. *
  6193. **/
  6194. static int i40e_parse_cls_flower(struct i40e_vsi *vsi,
  6195. struct tc_cls_flower_offload *f,
  6196. struct i40e_cloud_filter *filter)
  6197. {
  6198. u16 n_proto_mask = 0, n_proto_key = 0, addr_type = 0;
  6199. struct i40e_pf *pf = vsi->back;
  6200. u8 field_flags = 0;
  6201. if (f->dissector->used_keys &
  6202. ~(BIT(FLOW_DISSECTOR_KEY_CONTROL) |
  6203. BIT(FLOW_DISSECTOR_KEY_BASIC) |
  6204. BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
  6205. BIT(FLOW_DISSECTOR_KEY_VLAN) |
  6206. BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
  6207. BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
  6208. BIT(FLOW_DISSECTOR_KEY_PORTS) |
  6209. BIT(FLOW_DISSECTOR_KEY_ENC_KEYID))) {
  6210. dev_err(&pf->pdev->dev, "Unsupported key used: 0x%x\n",
  6211. f->dissector->used_keys);
  6212. return -EOPNOTSUPP;
  6213. }
  6214. if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
  6215. struct flow_dissector_key_keyid *key =
  6216. skb_flow_dissector_target(f->dissector,
  6217. FLOW_DISSECTOR_KEY_ENC_KEYID,
  6218. f->key);
  6219. struct flow_dissector_key_keyid *mask =
  6220. skb_flow_dissector_target(f->dissector,
  6221. FLOW_DISSECTOR_KEY_ENC_KEYID,
  6222. f->mask);
  6223. if (mask->keyid != 0)
  6224. field_flags |= I40E_CLOUD_FIELD_TEN_ID;
  6225. filter->tenant_id = be32_to_cpu(key->keyid);
  6226. }
  6227. if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_BASIC)) {
  6228. struct flow_dissector_key_basic *key =
  6229. skb_flow_dissector_target(f->dissector,
  6230. FLOW_DISSECTOR_KEY_BASIC,
  6231. f->key);
  6232. struct flow_dissector_key_basic *mask =
  6233. skb_flow_dissector_target(f->dissector,
  6234. FLOW_DISSECTOR_KEY_BASIC,
  6235. f->mask);
  6236. n_proto_key = ntohs(key->n_proto);
  6237. n_proto_mask = ntohs(mask->n_proto);
  6238. if (n_proto_key == ETH_P_ALL) {
  6239. n_proto_key = 0;
  6240. n_proto_mask = 0;
  6241. }
  6242. filter->n_proto = n_proto_key & n_proto_mask;
  6243. filter->ip_proto = key->ip_proto;
  6244. }
  6245. if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
  6246. struct flow_dissector_key_eth_addrs *key =
  6247. skb_flow_dissector_target(f->dissector,
  6248. FLOW_DISSECTOR_KEY_ETH_ADDRS,
  6249. f->key);
  6250. struct flow_dissector_key_eth_addrs *mask =
  6251. skb_flow_dissector_target(f->dissector,
  6252. FLOW_DISSECTOR_KEY_ETH_ADDRS,
  6253. f->mask);
  6254. /* use is_broadcast and is_zero to check for all 0xf or 0 */
  6255. if (!is_zero_ether_addr(mask->dst)) {
  6256. if (is_broadcast_ether_addr(mask->dst)) {
  6257. field_flags |= I40E_CLOUD_FIELD_OMAC;
  6258. } else {
  6259. dev_err(&pf->pdev->dev, "Bad ether dest mask %pM\n",
  6260. mask->dst);
  6261. return I40E_ERR_CONFIG;
  6262. }
  6263. }
  6264. if (!is_zero_ether_addr(mask->src)) {
  6265. if (is_broadcast_ether_addr(mask->src)) {
  6266. field_flags |= I40E_CLOUD_FIELD_IMAC;
  6267. } else {
  6268. dev_err(&pf->pdev->dev, "Bad ether src mask %pM\n",
  6269. mask->src);
  6270. return I40E_ERR_CONFIG;
  6271. }
  6272. }
  6273. ether_addr_copy(filter->dst_mac, key->dst);
  6274. ether_addr_copy(filter->src_mac, key->src);
  6275. }
  6276. if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_VLAN)) {
  6277. struct flow_dissector_key_vlan *key =
  6278. skb_flow_dissector_target(f->dissector,
  6279. FLOW_DISSECTOR_KEY_VLAN,
  6280. f->key);
  6281. struct flow_dissector_key_vlan *mask =
  6282. skb_flow_dissector_target(f->dissector,
  6283. FLOW_DISSECTOR_KEY_VLAN,
  6284. f->mask);
  6285. if (mask->vlan_id) {
  6286. if (mask->vlan_id == VLAN_VID_MASK) {
  6287. field_flags |= I40E_CLOUD_FIELD_IVLAN;
  6288. } else {
  6289. dev_err(&pf->pdev->dev, "Bad vlan mask 0x%04x\n",
  6290. mask->vlan_id);
  6291. return I40E_ERR_CONFIG;
  6292. }
  6293. }
  6294. filter->vlan_id = cpu_to_be16(key->vlan_id);
  6295. }
  6296. if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_CONTROL)) {
  6297. struct flow_dissector_key_control *key =
  6298. skb_flow_dissector_target(f->dissector,
  6299. FLOW_DISSECTOR_KEY_CONTROL,
  6300. f->key);
  6301. addr_type = key->addr_type;
  6302. }
  6303. if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
  6304. struct flow_dissector_key_ipv4_addrs *key =
  6305. skb_flow_dissector_target(f->dissector,
  6306. FLOW_DISSECTOR_KEY_IPV4_ADDRS,
  6307. f->key);
  6308. struct flow_dissector_key_ipv4_addrs *mask =
  6309. skb_flow_dissector_target(f->dissector,
  6310. FLOW_DISSECTOR_KEY_IPV4_ADDRS,
  6311. f->mask);
  6312. if (mask->dst) {
  6313. if (mask->dst == cpu_to_be32(0xffffffff)) {
  6314. field_flags |= I40E_CLOUD_FIELD_IIP;
  6315. } else {
  6316. mask->dst = be32_to_cpu(mask->dst);
  6317. dev_err(&pf->pdev->dev, "Bad ip dst mask %pI4\n",
  6318. &mask->dst);
  6319. return I40E_ERR_CONFIG;
  6320. }
  6321. }
  6322. if (mask->src) {
  6323. if (mask->src == cpu_to_be32(0xffffffff)) {
  6324. field_flags |= I40E_CLOUD_FIELD_IIP;
  6325. } else {
  6326. mask->src = be32_to_cpu(mask->src);
  6327. dev_err(&pf->pdev->dev, "Bad ip src mask %pI4\n",
  6328. &mask->src);
  6329. return I40E_ERR_CONFIG;
  6330. }
  6331. }
  6332. if (field_flags & I40E_CLOUD_FIELD_TEN_ID) {
  6333. dev_err(&pf->pdev->dev, "Tenant id not allowed for ip filter\n");
  6334. return I40E_ERR_CONFIG;
  6335. }
  6336. filter->dst_ipv4 = key->dst;
  6337. filter->src_ipv4 = key->src;
  6338. }
  6339. if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
  6340. struct flow_dissector_key_ipv6_addrs *key =
  6341. skb_flow_dissector_target(f->dissector,
  6342. FLOW_DISSECTOR_KEY_IPV6_ADDRS,
  6343. f->key);
  6344. struct flow_dissector_key_ipv6_addrs *mask =
  6345. skb_flow_dissector_target(f->dissector,
  6346. FLOW_DISSECTOR_KEY_IPV6_ADDRS,
  6347. f->mask);
  6348. /* src and dest IPV6 address should not be LOOPBACK
  6349. * (0:0:0:0:0:0:0:1), which can be represented as ::1
  6350. */
  6351. if (ipv6_addr_loopback(&key->dst) ||
  6352. ipv6_addr_loopback(&key->src)) {
  6353. dev_err(&pf->pdev->dev,
  6354. "Bad ipv6, addr is LOOPBACK\n");
  6355. return I40E_ERR_CONFIG;
  6356. }
  6357. if (!ipv6_addr_any(&mask->dst) || !ipv6_addr_any(&mask->src))
  6358. field_flags |= I40E_CLOUD_FIELD_IIP;
  6359. memcpy(&filter->src_ipv6, &key->src.s6_addr32,
  6360. sizeof(filter->src_ipv6));
  6361. memcpy(&filter->dst_ipv6, &key->dst.s6_addr32,
  6362. sizeof(filter->dst_ipv6));
  6363. }
  6364. if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_PORTS)) {
  6365. struct flow_dissector_key_ports *key =
  6366. skb_flow_dissector_target(f->dissector,
  6367. FLOW_DISSECTOR_KEY_PORTS,
  6368. f->key);
  6369. struct flow_dissector_key_ports *mask =
  6370. skb_flow_dissector_target(f->dissector,
  6371. FLOW_DISSECTOR_KEY_PORTS,
  6372. f->mask);
  6373. if (mask->src) {
  6374. if (mask->src == cpu_to_be16(0xffff)) {
  6375. field_flags |= I40E_CLOUD_FIELD_IIP;
  6376. } else {
  6377. dev_err(&pf->pdev->dev, "Bad src port mask 0x%04x\n",
  6378. be16_to_cpu(mask->src));
  6379. return I40E_ERR_CONFIG;
  6380. }
  6381. }
  6382. if (mask->dst) {
  6383. if (mask->dst == cpu_to_be16(0xffff)) {
  6384. field_flags |= I40E_CLOUD_FIELD_IIP;
  6385. } else {
  6386. dev_err(&pf->pdev->dev, "Bad dst port mask 0x%04x\n",
  6387. be16_to_cpu(mask->dst));
  6388. return I40E_ERR_CONFIG;
  6389. }
  6390. }
  6391. filter->dst_port = key->dst;
  6392. filter->src_port = key->src;
  6393. switch (filter->ip_proto) {
  6394. case IPPROTO_TCP:
  6395. case IPPROTO_UDP:
  6396. break;
  6397. default:
  6398. dev_err(&pf->pdev->dev,
  6399. "Only UDP and TCP transport are supported\n");
  6400. return -EINVAL;
  6401. }
  6402. }
  6403. filter->flags = field_flags;
  6404. return 0;
  6405. }
  6406. /**
  6407. * i40e_handle_tclass: Forward to a traffic class on the device
  6408. * @vsi: Pointer to VSI
  6409. * @tc: traffic class index on the device
  6410. * @filter: Pointer to cloud filter structure
  6411. *
  6412. **/
  6413. static int i40e_handle_tclass(struct i40e_vsi *vsi, u32 tc,
  6414. struct i40e_cloud_filter *filter)
  6415. {
  6416. struct i40e_channel *ch, *ch_tmp;
  6417. /* direct to a traffic class on the same device */
  6418. if (tc == 0) {
  6419. filter->seid = vsi->seid;
  6420. return 0;
  6421. } else if (vsi->tc_config.enabled_tc & BIT(tc)) {
  6422. if (!filter->dst_port) {
  6423. dev_err(&vsi->back->pdev->dev,
  6424. "Specify destination port to direct to traffic class that is not default\n");
  6425. return -EINVAL;
  6426. }
  6427. if (list_empty(&vsi->ch_list))
  6428. return -EINVAL;
  6429. list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list,
  6430. list) {
  6431. if (ch->seid == vsi->tc_seid_map[tc])
  6432. filter->seid = ch->seid;
  6433. }
  6434. return 0;
  6435. }
  6436. dev_err(&vsi->back->pdev->dev, "TC is not enabled\n");
  6437. return -EINVAL;
  6438. }
  6439. /**
  6440. * i40e_configure_clsflower - Configure tc flower filters
  6441. * @vsi: Pointer to VSI
  6442. * @cls_flower: Pointer to struct tc_cls_flower_offload
  6443. *
  6444. **/
  6445. static int i40e_configure_clsflower(struct i40e_vsi *vsi,
  6446. struct tc_cls_flower_offload *cls_flower)
  6447. {
  6448. int tc = tc_classid_to_hwtc(vsi->netdev, cls_flower->classid);
  6449. struct i40e_cloud_filter *filter = NULL;
  6450. struct i40e_pf *pf = vsi->back;
  6451. int err = 0;
  6452. if (tc < 0) {
  6453. dev_err(&vsi->back->pdev->dev, "Invalid traffic class\n");
  6454. return -EOPNOTSUPP;
  6455. }
  6456. if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
  6457. test_bit(__I40E_RESET_INTR_RECEIVED, pf->state))
  6458. return -EBUSY;
  6459. if (pf->fdir_pf_active_filters ||
  6460. (!hlist_empty(&pf->fdir_filter_list))) {
  6461. dev_err(&vsi->back->pdev->dev,
  6462. "Flow Director Sideband filters exists, turn ntuple off to configure cloud filters\n");
  6463. return -EINVAL;
  6464. }
  6465. if (vsi->back->flags & I40E_FLAG_FD_SB_ENABLED) {
  6466. dev_err(&vsi->back->pdev->dev,
  6467. "Disable Flow Director Sideband, configuring Cloud filters via tc-flower\n");
  6468. vsi->back->flags &= ~I40E_FLAG_FD_SB_ENABLED;
  6469. vsi->back->flags |= I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
  6470. }
  6471. filter = kzalloc(sizeof(*filter), GFP_KERNEL);
  6472. if (!filter)
  6473. return -ENOMEM;
  6474. filter->cookie = cls_flower->cookie;
  6475. err = i40e_parse_cls_flower(vsi, cls_flower, filter);
  6476. if (err < 0)
  6477. goto err;
  6478. err = i40e_handle_tclass(vsi, tc, filter);
  6479. if (err < 0)
  6480. goto err;
  6481. /* Add cloud filter */
  6482. if (filter->dst_port)
  6483. err = i40e_add_del_cloud_filter_big_buf(vsi, filter, true);
  6484. else
  6485. err = i40e_add_del_cloud_filter(vsi, filter, true);
  6486. if (err) {
  6487. dev_err(&pf->pdev->dev,
  6488. "Failed to add cloud filter, err %s\n",
  6489. i40e_stat_str(&pf->hw, err));
  6490. goto err;
  6491. }
  6492. /* add filter to the ordered list */
  6493. INIT_HLIST_NODE(&filter->cloud_node);
  6494. hlist_add_head(&filter->cloud_node, &pf->cloud_filter_list);
  6495. pf->num_cloud_filters++;
  6496. return err;
  6497. err:
  6498. kfree(filter);
  6499. return err;
  6500. }
  6501. /**
  6502. * i40e_find_cloud_filter - Find the could filter in the list
  6503. * @vsi: Pointer to VSI
  6504. * @cookie: filter specific cookie
  6505. *
  6506. **/
  6507. static struct i40e_cloud_filter *i40e_find_cloud_filter(struct i40e_vsi *vsi,
  6508. unsigned long *cookie)
  6509. {
  6510. struct i40e_cloud_filter *filter = NULL;
  6511. struct hlist_node *node2;
  6512. hlist_for_each_entry_safe(filter, node2,
  6513. &vsi->back->cloud_filter_list, cloud_node)
  6514. if (!memcmp(cookie, &filter->cookie, sizeof(filter->cookie)))
  6515. return filter;
  6516. return NULL;
  6517. }
  6518. /**
  6519. * i40e_delete_clsflower - Remove tc flower filters
  6520. * @vsi: Pointer to VSI
  6521. * @cls_flower: Pointer to struct tc_cls_flower_offload
  6522. *
  6523. **/
  6524. static int i40e_delete_clsflower(struct i40e_vsi *vsi,
  6525. struct tc_cls_flower_offload *cls_flower)
  6526. {
  6527. struct i40e_cloud_filter *filter = NULL;
  6528. struct i40e_pf *pf = vsi->back;
  6529. int err = 0;
  6530. filter = i40e_find_cloud_filter(vsi, &cls_flower->cookie);
  6531. if (!filter)
  6532. return -EINVAL;
  6533. hash_del(&filter->cloud_node);
  6534. if (filter->dst_port)
  6535. err = i40e_add_del_cloud_filter_big_buf(vsi, filter, false);
  6536. else
  6537. err = i40e_add_del_cloud_filter(vsi, filter, false);
  6538. kfree(filter);
  6539. if (err) {
  6540. dev_err(&pf->pdev->dev,
  6541. "Failed to delete cloud filter, err %s\n",
  6542. i40e_stat_str(&pf->hw, err));
  6543. return i40e_aq_rc_to_posix(err, pf->hw.aq.asq_last_status);
  6544. }
  6545. pf->num_cloud_filters--;
  6546. if (!pf->num_cloud_filters)
  6547. if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
  6548. !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
  6549. pf->flags |= I40E_FLAG_FD_SB_ENABLED;
  6550. pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
  6551. pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
  6552. }
  6553. return 0;
  6554. }
  6555. /**
  6556. * i40e_setup_tc_cls_flower - flower classifier offloads
  6557. * @netdev: net device to configure
  6558. * @type_data: offload data
  6559. **/
  6560. static int i40e_setup_tc_cls_flower(struct i40e_netdev_priv *np,
  6561. struct tc_cls_flower_offload *cls_flower)
  6562. {
  6563. struct i40e_vsi *vsi = np->vsi;
  6564. switch (cls_flower->command) {
  6565. case TC_CLSFLOWER_REPLACE:
  6566. return i40e_configure_clsflower(vsi, cls_flower);
  6567. case TC_CLSFLOWER_DESTROY:
  6568. return i40e_delete_clsflower(vsi, cls_flower);
  6569. case TC_CLSFLOWER_STATS:
  6570. return -EOPNOTSUPP;
  6571. default:
  6572. return -EINVAL;
  6573. }
  6574. }
  6575. static int i40e_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
  6576. void *cb_priv)
  6577. {
  6578. struct i40e_netdev_priv *np = cb_priv;
  6579. if (!tc_cls_can_offload_and_chain0(np->vsi->netdev, type_data))
  6580. return -EOPNOTSUPP;
  6581. switch (type) {
  6582. case TC_SETUP_CLSFLOWER:
  6583. return i40e_setup_tc_cls_flower(np, type_data);
  6584. default:
  6585. return -EOPNOTSUPP;
  6586. }
  6587. }
  6588. static int i40e_setup_tc_block(struct net_device *dev,
  6589. struct tc_block_offload *f)
  6590. {
  6591. struct i40e_netdev_priv *np = netdev_priv(dev);
  6592. if (f->binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
  6593. return -EOPNOTSUPP;
  6594. switch (f->command) {
  6595. case TC_BLOCK_BIND:
  6596. return tcf_block_cb_register(f->block, i40e_setup_tc_block_cb,
  6597. np, np);
  6598. case TC_BLOCK_UNBIND:
  6599. tcf_block_cb_unregister(f->block, i40e_setup_tc_block_cb, np);
  6600. return 0;
  6601. default:
  6602. return -EOPNOTSUPP;
  6603. }
  6604. }
  6605. static int __i40e_setup_tc(struct net_device *netdev, enum tc_setup_type type,
  6606. void *type_data)
  6607. {
  6608. switch (type) {
  6609. case TC_SETUP_QDISC_MQPRIO:
  6610. return i40e_setup_tc(netdev, type_data);
  6611. case TC_SETUP_BLOCK:
  6612. return i40e_setup_tc_block(netdev, type_data);
  6613. default:
  6614. return -EOPNOTSUPP;
  6615. }
  6616. }
  6617. /**
  6618. * i40e_open - Called when a network interface is made active
  6619. * @netdev: network interface device structure
  6620. *
  6621. * The open entry point is called when a network interface is made
  6622. * active by the system (IFF_UP). At this point all resources needed
  6623. * for transmit and receive operations are allocated, the interrupt
  6624. * handler is registered with the OS, the netdev watchdog subtask is
  6625. * enabled, and the stack is notified that the interface is ready.
  6626. *
  6627. * Returns 0 on success, negative value on failure
  6628. **/
  6629. int i40e_open(struct net_device *netdev)
  6630. {
  6631. struct i40e_netdev_priv *np = netdev_priv(netdev);
  6632. struct i40e_vsi *vsi = np->vsi;
  6633. struct i40e_pf *pf = vsi->back;
  6634. int err;
  6635. /* disallow open during test or if eeprom is broken */
  6636. if (test_bit(__I40E_TESTING, pf->state) ||
  6637. test_bit(__I40E_BAD_EEPROM, pf->state))
  6638. return -EBUSY;
  6639. netif_carrier_off(netdev);
  6640. if (i40e_force_link_state(pf, true))
  6641. return -EAGAIN;
  6642. err = i40e_vsi_open(vsi);
  6643. if (err)
  6644. return err;
  6645. /* configure global TSO hardware offload settings */
  6646. wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
  6647. TCP_FLAG_FIN) >> 16);
  6648. wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
  6649. TCP_FLAG_FIN |
  6650. TCP_FLAG_CWR) >> 16);
  6651. wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
  6652. udp_tunnel_get_rx_info(netdev);
  6653. return 0;
  6654. }
  6655. /**
  6656. * i40e_vsi_open -
  6657. * @vsi: the VSI to open
  6658. *
  6659. * Finish initialization of the VSI.
  6660. *
  6661. * Returns 0 on success, negative value on failure
  6662. *
  6663. * Note: expects to be called while under rtnl_lock()
  6664. **/
  6665. int i40e_vsi_open(struct i40e_vsi *vsi)
  6666. {
  6667. struct i40e_pf *pf = vsi->back;
  6668. char int_name[I40E_INT_NAME_STR_LEN];
  6669. int err;
  6670. /* allocate descriptors */
  6671. err = i40e_vsi_setup_tx_resources(vsi);
  6672. if (err)
  6673. goto err_setup_tx;
  6674. err = i40e_vsi_setup_rx_resources(vsi);
  6675. if (err)
  6676. goto err_setup_rx;
  6677. err = i40e_vsi_configure(vsi);
  6678. if (err)
  6679. goto err_setup_rx;
  6680. if (vsi->netdev) {
  6681. snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
  6682. dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
  6683. err = i40e_vsi_request_irq(vsi, int_name);
  6684. if (err)
  6685. goto err_setup_rx;
  6686. /* Notify the stack of the actual queue counts. */
  6687. err = netif_set_real_num_tx_queues(vsi->netdev,
  6688. vsi->num_queue_pairs);
  6689. if (err)
  6690. goto err_set_queues;
  6691. err = netif_set_real_num_rx_queues(vsi->netdev,
  6692. vsi->num_queue_pairs);
  6693. if (err)
  6694. goto err_set_queues;
  6695. } else if (vsi->type == I40E_VSI_FDIR) {
  6696. snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
  6697. dev_driver_string(&pf->pdev->dev),
  6698. dev_name(&pf->pdev->dev));
  6699. err = i40e_vsi_request_irq(vsi, int_name);
  6700. } else {
  6701. err = -EINVAL;
  6702. goto err_setup_rx;
  6703. }
  6704. err = i40e_up_complete(vsi);
  6705. if (err)
  6706. goto err_up_complete;
  6707. return 0;
  6708. err_up_complete:
  6709. i40e_down(vsi);
  6710. err_set_queues:
  6711. i40e_vsi_free_irq(vsi);
  6712. err_setup_rx:
  6713. i40e_vsi_free_rx_resources(vsi);
  6714. err_setup_tx:
  6715. i40e_vsi_free_tx_resources(vsi);
  6716. if (vsi == pf->vsi[pf->lan_vsi])
  6717. i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
  6718. return err;
  6719. }
  6720. /**
  6721. * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
  6722. * @pf: Pointer to PF
  6723. *
  6724. * This function destroys the hlist where all the Flow Director
  6725. * filters were saved.
  6726. **/
  6727. static void i40e_fdir_filter_exit(struct i40e_pf *pf)
  6728. {
  6729. struct i40e_fdir_filter *filter;
  6730. struct i40e_flex_pit *pit_entry, *tmp;
  6731. struct hlist_node *node2;
  6732. hlist_for_each_entry_safe(filter, node2,
  6733. &pf->fdir_filter_list, fdir_node) {
  6734. hlist_del(&filter->fdir_node);
  6735. kfree(filter);
  6736. }
  6737. list_for_each_entry_safe(pit_entry, tmp, &pf->l3_flex_pit_list, list) {
  6738. list_del(&pit_entry->list);
  6739. kfree(pit_entry);
  6740. }
  6741. INIT_LIST_HEAD(&pf->l3_flex_pit_list);
  6742. list_for_each_entry_safe(pit_entry, tmp, &pf->l4_flex_pit_list, list) {
  6743. list_del(&pit_entry->list);
  6744. kfree(pit_entry);
  6745. }
  6746. INIT_LIST_HEAD(&pf->l4_flex_pit_list);
  6747. pf->fdir_pf_active_filters = 0;
  6748. pf->fd_tcp4_filter_cnt = 0;
  6749. pf->fd_udp4_filter_cnt = 0;
  6750. pf->fd_sctp4_filter_cnt = 0;
  6751. pf->fd_ip4_filter_cnt = 0;
  6752. /* Reprogram the default input set for TCP/IPv4 */
  6753. i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
  6754. I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
  6755. I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
  6756. /* Reprogram the default input set for UDP/IPv4 */
  6757. i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_UDP,
  6758. I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
  6759. I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
  6760. /* Reprogram the default input set for SCTP/IPv4 */
  6761. i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_SCTP,
  6762. I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
  6763. I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
  6764. /* Reprogram the default input set for Other/IPv4 */
  6765. i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_OTHER,
  6766. I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
  6767. i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_FRAG_IPV4,
  6768. I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
  6769. }
  6770. /**
  6771. * i40e_cloud_filter_exit - Cleans up the cloud filters
  6772. * @pf: Pointer to PF
  6773. *
  6774. * This function destroys the hlist where all the cloud filters
  6775. * were saved.
  6776. **/
  6777. static void i40e_cloud_filter_exit(struct i40e_pf *pf)
  6778. {
  6779. struct i40e_cloud_filter *cfilter;
  6780. struct hlist_node *node;
  6781. hlist_for_each_entry_safe(cfilter, node,
  6782. &pf->cloud_filter_list, cloud_node) {
  6783. hlist_del(&cfilter->cloud_node);
  6784. kfree(cfilter);
  6785. }
  6786. pf->num_cloud_filters = 0;
  6787. if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
  6788. !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
  6789. pf->flags |= I40E_FLAG_FD_SB_ENABLED;
  6790. pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
  6791. pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
  6792. }
  6793. }
  6794. /**
  6795. * i40e_close - Disables a network interface
  6796. * @netdev: network interface device structure
  6797. *
  6798. * The close entry point is called when an interface is de-activated
  6799. * by the OS. The hardware is still under the driver's control, but
  6800. * this netdev interface is disabled.
  6801. *
  6802. * Returns 0, this is not allowed to fail
  6803. **/
  6804. int i40e_close(struct net_device *netdev)
  6805. {
  6806. struct i40e_netdev_priv *np = netdev_priv(netdev);
  6807. struct i40e_vsi *vsi = np->vsi;
  6808. i40e_vsi_close(vsi);
  6809. return 0;
  6810. }
  6811. /**
  6812. * i40e_do_reset - Start a PF or Core Reset sequence
  6813. * @pf: board private structure
  6814. * @reset_flags: which reset is requested
  6815. * @lock_acquired: indicates whether or not the lock has been acquired
  6816. * before this function was called.
  6817. *
  6818. * The essential difference in resets is that the PF Reset
  6819. * doesn't clear the packet buffers, doesn't reset the PE
  6820. * firmware, and doesn't bother the other PFs on the chip.
  6821. **/
  6822. void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags, bool lock_acquired)
  6823. {
  6824. u32 val;
  6825. WARN_ON(in_interrupt());
  6826. /* do the biggest reset indicated */
  6827. if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
  6828. /* Request a Global Reset
  6829. *
  6830. * This will start the chip's countdown to the actual full
  6831. * chip reset event, and a warning interrupt to be sent
  6832. * to all PFs, including the requestor. Our handler
  6833. * for the warning interrupt will deal with the shutdown
  6834. * and recovery of the switch setup.
  6835. */
  6836. dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
  6837. val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
  6838. val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
  6839. wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
  6840. } else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
  6841. /* Request a Core Reset
  6842. *
  6843. * Same as Global Reset, except does *not* include the MAC/PHY
  6844. */
  6845. dev_dbg(&pf->pdev->dev, "CoreR requested\n");
  6846. val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
  6847. val |= I40E_GLGEN_RTRIG_CORER_MASK;
  6848. wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
  6849. i40e_flush(&pf->hw);
  6850. } else if (reset_flags & I40E_PF_RESET_FLAG) {
  6851. /* Request a PF Reset
  6852. *
  6853. * Resets only the PF-specific registers
  6854. *
  6855. * This goes directly to the tear-down and rebuild of
  6856. * the switch, since we need to do all the recovery as
  6857. * for the Core Reset.
  6858. */
  6859. dev_dbg(&pf->pdev->dev, "PFR requested\n");
  6860. i40e_handle_reset_warning(pf, lock_acquired);
  6861. } else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
  6862. int v;
  6863. /* Find the VSI(s) that requested a re-init */
  6864. dev_info(&pf->pdev->dev,
  6865. "VSI reinit requested\n");
  6866. for (v = 0; v < pf->num_alloc_vsi; v++) {
  6867. struct i40e_vsi *vsi = pf->vsi[v];
  6868. if (vsi != NULL &&
  6869. test_and_clear_bit(__I40E_VSI_REINIT_REQUESTED,
  6870. vsi->state))
  6871. i40e_vsi_reinit_locked(pf->vsi[v]);
  6872. }
  6873. } else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
  6874. int v;
  6875. /* Find the VSI(s) that needs to be brought down */
  6876. dev_info(&pf->pdev->dev, "VSI down requested\n");
  6877. for (v = 0; v < pf->num_alloc_vsi; v++) {
  6878. struct i40e_vsi *vsi = pf->vsi[v];
  6879. if (vsi != NULL &&
  6880. test_and_clear_bit(__I40E_VSI_DOWN_REQUESTED,
  6881. vsi->state)) {
  6882. set_bit(__I40E_VSI_DOWN, vsi->state);
  6883. i40e_down(vsi);
  6884. }
  6885. }
  6886. } else {
  6887. dev_info(&pf->pdev->dev,
  6888. "bad reset request 0x%08x\n", reset_flags);
  6889. }
  6890. }
  6891. #ifdef CONFIG_I40E_DCB
  6892. /**
  6893. * i40e_dcb_need_reconfig - Check if DCB needs reconfig
  6894. * @pf: board private structure
  6895. * @old_cfg: current DCB config
  6896. * @new_cfg: new DCB config
  6897. **/
  6898. bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
  6899. struct i40e_dcbx_config *old_cfg,
  6900. struct i40e_dcbx_config *new_cfg)
  6901. {
  6902. bool need_reconfig = false;
  6903. /* Check if ETS configuration has changed */
  6904. if (memcmp(&new_cfg->etscfg,
  6905. &old_cfg->etscfg,
  6906. sizeof(new_cfg->etscfg))) {
  6907. /* If Priority Table has changed reconfig is needed */
  6908. if (memcmp(&new_cfg->etscfg.prioritytable,
  6909. &old_cfg->etscfg.prioritytable,
  6910. sizeof(new_cfg->etscfg.prioritytable))) {
  6911. need_reconfig = true;
  6912. dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
  6913. }
  6914. if (memcmp(&new_cfg->etscfg.tcbwtable,
  6915. &old_cfg->etscfg.tcbwtable,
  6916. sizeof(new_cfg->etscfg.tcbwtable)))
  6917. dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
  6918. if (memcmp(&new_cfg->etscfg.tsatable,
  6919. &old_cfg->etscfg.tsatable,
  6920. sizeof(new_cfg->etscfg.tsatable)))
  6921. dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
  6922. }
  6923. /* Check if PFC configuration has changed */
  6924. if (memcmp(&new_cfg->pfc,
  6925. &old_cfg->pfc,
  6926. sizeof(new_cfg->pfc))) {
  6927. need_reconfig = true;
  6928. dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
  6929. }
  6930. /* Check if APP Table has changed */
  6931. if (memcmp(&new_cfg->app,
  6932. &old_cfg->app,
  6933. sizeof(new_cfg->app))) {
  6934. need_reconfig = true;
  6935. dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
  6936. }
  6937. dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
  6938. return need_reconfig;
  6939. }
  6940. /**
  6941. * i40e_handle_lldp_event - Handle LLDP Change MIB event
  6942. * @pf: board private structure
  6943. * @e: event info posted on ARQ
  6944. **/
  6945. static int i40e_handle_lldp_event(struct i40e_pf *pf,
  6946. struct i40e_arq_event_info *e)
  6947. {
  6948. struct i40e_aqc_lldp_get_mib *mib =
  6949. (struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
  6950. struct i40e_hw *hw = &pf->hw;
  6951. struct i40e_dcbx_config tmp_dcbx_cfg;
  6952. bool need_reconfig = false;
  6953. int ret = 0;
  6954. u8 type;
  6955. /* Not DCB capable or capability disabled */
  6956. if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
  6957. return ret;
  6958. /* Ignore if event is not for Nearest Bridge */
  6959. type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
  6960. & I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
  6961. dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
  6962. if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
  6963. return ret;
  6964. /* Check MIB Type and return if event for Remote MIB update */
  6965. type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
  6966. dev_dbg(&pf->pdev->dev,
  6967. "LLDP event mib type %s\n", type ? "remote" : "local");
  6968. if (type == I40E_AQ_LLDP_MIB_REMOTE) {
  6969. /* Update the remote cached instance and return */
  6970. ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
  6971. I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
  6972. &hw->remote_dcbx_config);
  6973. goto exit;
  6974. }
  6975. /* Store the old configuration */
  6976. tmp_dcbx_cfg = hw->local_dcbx_config;
  6977. /* Reset the old DCBx configuration data */
  6978. memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
  6979. /* Get updated DCBX data from firmware */
  6980. ret = i40e_get_dcb_config(&pf->hw);
  6981. if (ret) {
  6982. dev_info(&pf->pdev->dev,
  6983. "Failed querying DCB configuration data from firmware, err %s aq_err %s\n",
  6984. i40e_stat_str(&pf->hw, ret),
  6985. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  6986. goto exit;
  6987. }
  6988. /* No change detected in DCBX configs */
  6989. if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
  6990. sizeof(tmp_dcbx_cfg))) {
  6991. dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
  6992. goto exit;
  6993. }
  6994. need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
  6995. &hw->local_dcbx_config);
  6996. i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
  6997. if (!need_reconfig)
  6998. goto exit;
  6999. /* Enable DCB tagging only when more than one TC */
  7000. if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
  7001. pf->flags |= I40E_FLAG_DCB_ENABLED;
  7002. else
  7003. pf->flags &= ~I40E_FLAG_DCB_ENABLED;
  7004. set_bit(__I40E_PORT_SUSPENDED, pf->state);
  7005. /* Reconfiguration needed quiesce all VSIs */
  7006. i40e_pf_quiesce_all_vsi(pf);
  7007. /* Changes in configuration update VEB/VSI */
  7008. i40e_dcb_reconfigure(pf);
  7009. ret = i40e_resume_port_tx(pf);
  7010. clear_bit(__I40E_PORT_SUSPENDED, pf->state);
  7011. /* In case of error no point in resuming VSIs */
  7012. if (ret)
  7013. goto exit;
  7014. /* Wait for the PF's queues to be disabled */
  7015. ret = i40e_pf_wait_queues_disabled(pf);
  7016. if (ret) {
  7017. /* Schedule PF reset to recover */
  7018. set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
  7019. i40e_service_event_schedule(pf);
  7020. } else {
  7021. i40e_pf_unquiesce_all_vsi(pf);
  7022. set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
  7023. set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
  7024. }
  7025. exit:
  7026. return ret;
  7027. }
  7028. #endif /* CONFIG_I40E_DCB */
  7029. /**
  7030. * i40e_do_reset_safe - Protected reset path for userland calls.
  7031. * @pf: board private structure
  7032. * @reset_flags: which reset is requested
  7033. *
  7034. **/
  7035. void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
  7036. {
  7037. rtnl_lock();
  7038. i40e_do_reset(pf, reset_flags, true);
  7039. rtnl_unlock();
  7040. }
  7041. /**
  7042. * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
  7043. * @pf: board private structure
  7044. * @e: event info posted on ARQ
  7045. *
  7046. * Handler for LAN Queue Overflow Event generated by the firmware for PF
  7047. * and VF queues
  7048. **/
  7049. static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
  7050. struct i40e_arq_event_info *e)
  7051. {
  7052. struct i40e_aqc_lan_overflow *data =
  7053. (struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
  7054. u32 queue = le32_to_cpu(data->prtdcb_rupto);
  7055. u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
  7056. struct i40e_hw *hw = &pf->hw;
  7057. struct i40e_vf *vf;
  7058. u16 vf_id;
  7059. dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
  7060. queue, qtx_ctl);
  7061. /* Queue belongs to VF, find the VF and issue VF reset */
  7062. if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
  7063. >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
  7064. vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
  7065. >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
  7066. vf_id -= hw->func_caps.vf_base_id;
  7067. vf = &pf->vf[vf_id];
  7068. i40e_vc_notify_vf_reset(vf);
  7069. /* Allow VF to process pending reset notification */
  7070. msleep(20);
  7071. i40e_reset_vf(vf, false);
  7072. }
  7073. }
  7074. /**
  7075. * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
  7076. * @pf: board private structure
  7077. **/
  7078. u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
  7079. {
  7080. u32 val, fcnt_prog;
  7081. val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
  7082. fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
  7083. return fcnt_prog;
  7084. }
  7085. /**
  7086. * i40e_get_current_fd_count - Get total FD filters programmed for this PF
  7087. * @pf: board private structure
  7088. **/
  7089. u32 i40e_get_current_fd_count(struct i40e_pf *pf)
  7090. {
  7091. u32 val, fcnt_prog;
  7092. val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
  7093. fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
  7094. ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
  7095. I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
  7096. return fcnt_prog;
  7097. }
  7098. /**
  7099. * i40e_get_global_fd_count - Get total FD filters programmed on device
  7100. * @pf: board private structure
  7101. **/
  7102. u32 i40e_get_global_fd_count(struct i40e_pf *pf)
  7103. {
  7104. u32 val, fcnt_prog;
  7105. val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
  7106. fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
  7107. ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >>
  7108. I40E_GLQF_FDCNT_0_BESTCNT_SHIFT);
  7109. return fcnt_prog;
  7110. }
  7111. /**
  7112. * i40e_reenable_fdir_sb - Restore FDir SB capability
  7113. * @pf: board private structure
  7114. **/
  7115. static void i40e_reenable_fdir_sb(struct i40e_pf *pf)
  7116. {
  7117. if (test_and_clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
  7118. if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
  7119. (I40E_DEBUG_FD & pf->hw.debug_mask))
  7120. dev_info(&pf->pdev->dev, "FD Sideband/ntuple is being enabled since we have space in the table now\n");
  7121. }
  7122. /**
  7123. * i40e_reenable_fdir_atr - Restore FDir ATR capability
  7124. * @pf: board private structure
  7125. **/
  7126. static void i40e_reenable_fdir_atr(struct i40e_pf *pf)
  7127. {
  7128. if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state)) {
  7129. /* ATR uses the same filtering logic as SB rules. It only
  7130. * functions properly if the input set mask is at the default
  7131. * settings. It is safe to restore the default input set
  7132. * because there are no active TCPv4 filter rules.
  7133. */
  7134. i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
  7135. I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
  7136. I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
  7137. if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
  7138. (I40E_DEBUG_FD & pf->hw.debug_mask))
  7139. dev_info(&pf->pdev->dev, "ATR is being enabled since we have space in the table and there are no conflicting ntuple rules\n");
  7140. }
  7141. }
  7142. /**
  7143. * i40e_delete_invalid_filter - Delete an invalid FDIR filter
  7144. * @pf: board private structure
  7145. * @filter: FDir filter to remove
  7146. */
  7147. static void i40e_delete_invalid_filter(struct i40e_pf *pf,
  7148. struct i40e_fdir_filter *filter)
  7149. {
  7150. /* Update counters */
  7151. pf->fdir_pf_active_filters--;
  7152. pf->fd_inv = 0;
  7153. switch (filter->flow_type) {
  7154. case TCP_V4_FLOW:
  7155. pf->fd_tcp4_filter_cnt--;
  7156. break;
  7157. case UDP_V4_FLOW:
  7158. pf->fd_udp4_filter_cnt--;
  7159. break;
  7160. case SCTP_V4_FLOW:
  7161. pf->fd_sctp4_filter_cnt--;
  7162. break;
  7163. case IP_USER_FLOW:
  7164. switch (filter->ip4_proto) {
  7165. case IPPROTO_TCP:
  7166. pf->fd_tcp4_filter_cnt--;
  7167. break;
  7168. case IPPROTO_UDP:
  7169. pf->fd_udp4_filter_cnt--;
  7170. break;
  7171. case IPPROTO_SCTP:
  7172. pf->fd_sctp4_filter_cnt--;
  7173. break;
  7174. case IPPROTO_IP:
  7175. pf->fd_ip4_filter_cnt--;
  7176. break;
  7177. }
  7178. break;
  7179. }
  7180. /* Remove the filter from the list and free memory */
  7181. hlist_del(&filter->fdir_node);
  7182. kfree(filter);
  7183. }
  7184. /**
  7185. * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
  7186. * @pf: board private structure
  7187. **/
  7188. void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
  7189. {
  7190. struct i40e_fdir_filter *filter;
  7191. u32 fcnt_prog, fcnt_avail;
  7192. struct hlist_node *node;
  7193. if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
  7194. return;
  7195. /* Check if we have enough room to re-enable FDir SB capability. */
  7196. fcnt_prog = i40e_get_global_fd_count(pf);
  7197. fcnt_avail = pf->fdir_pf_filter_count;
  7198. if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
  7199. (pf->fd_add_err == 0) ||
  7200. (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt))
  7201. i40e_reenable_fdir_sb(pf);
  7202. /* We should wait for even more space before re-enabling ATR.
  7203. * Additionally, we cannot enable ATR as long as we still have TCP SB
  7204. * rules active.
  7205. */
  7206. if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) &&
  7207. (pf->fd_tcp4_filter_cnt == 0))
  7208. i40e_reenable_fdir_atr(pf);
  7209. /* if hw had a problem adding a filter, delete it */
  7210. if (pf->fd_inv > 0) {
  7211. hlist_for_each_entry_safe(filter, node,
  7212. &pf->fdir_filter_list, fdir_node)
  7213. if (filter->fd_id == pf->fd_inv)
  7214. i40e_delete_invalid_filter(pf, filter);
  7215. }
  7216. }
  7217. #define I40E_MIN_FD_FLUSH_INTERVAL 10
  7218. #define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
  7219. /**
  7220. * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
  7221. * @pf: board private structure
  7222. **/
  7223. static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
  7224. {
  7225. unsigned long min_flush_time;
  7226. int flush_wait_retry = 50;
  7227. bool disable_atr = false;
  7228. int fd_room;
  7229. int reg;
  7230. if (!time_after(jiffies, pf->fd_flush_timestamp +
  7231. (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
  7232. return;
  7233. /* If the flush is happening too quick and we have mostly SB rules we
  7234. * should not re-enable ATR for some time.
  7235. */
  7236. min_flush_time = pf->fd_flush_timestamp +
  7237. (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
  7238. fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
  7239. if (!(time_after(jiffies, min_flush_time)) &&
  7240. (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
  7241. if (I40E_DEBUG_FD & pf->hw.debug_mask)
  7242. dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
  7243. disable_atr = true;
  7244. }
  7245. pf->fd_flush_timestamp = jiffies;
  7246. set_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
  7247. /* flush all filters */
  7248. wr32(&pf->hw, I40E_PFQF_CTL_1,
  7249. I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
  7250. i40e_flush(&pf->hw);
  7251. pf->fd_flush_cnt++;
  7252. pf->fd_add_err = 0;
  7253. do {
  7254. /* Check FD flush status every 5-6msec */
  7255. usleep_range(5000, 6000);
  7256. reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
  7257. if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
  7258. break;
  7259. } while (flush_wait_retry--);
  7260. if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
  7261. dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
  7262. } else {
  7263. /* replay sideband filters */
  7264. i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
  7265. if (!disable_atr && !pf->fd_tcp4_filter_cnt)
  7266. clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
  7267. clear_bit(__I40E_FD_FLUSH_REQUESTED, pf->state);
  7268. if (I40E_DEBUG_FD & pf->hw.debug_mask)
  7269. dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
  7270. }
  7271. }
  7272. /**
  7273. * i40e_get_current_atr_count - Get the count of total FD ATR filters programmed
  7274. * @pf: board private structure
  7275. **/
  7276. u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
  7277. {
  7278. return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
  7279. }
  7280. /* We can see up to 256 filter programming desc in transit if the filters are
  7281. * being applied really fast; before we see the first
  7282. * filter miss error on Rx queue 0. Accumulating enough error messages before
  7283. * reacting will make sure we don't cause flush too often.
  7284. */
  7285. #define I40E_MAX_FD_PROGRAM_ERROR 256
  7286. /**
  7287. * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
  7288. * @pf: board private structure
  7289. **/
  7290. static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
  7291. {
  7292. /* if interface is down do nothing */
  7293. if (test_bit(__I40E_DOWN, pf->state))
  7294. return;
  7295. if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
  7296. i40e_fdir_flush_and_replay(pf);
  7297. i40e_fdir_check_and_reenable(pf);
  7298. }
  7299. /**
  7300. * i40e_vsi_link_event - notify VSI of a link event
  7301. * @vsi: vsi to be notified
  7302. * @link_up: link up or down
  7303. **/
  7304. static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
  7305. {
  7306. if (!vsi || test_bit(__I40E_VSI_DOWN, vsi->state))
  7307. return;
  7308. switch (vsi->type) {
  7309. case I40E_VSI_MAIN:
  7310. if (!vsi->netdev || !vsi->netdev_registered)
  7311. break;
  7312. if (link_up) {
  7313. netif_carrier_on(vsi->netdev);
  7314. netif_tx_wake_all_queues(vsi->netdev);
  7315. } else {
  7316. netif_carrier_off(vsi->netdev);
  7317. netif_tx_stop_all_queues(vsi->netdev);
  7318. }
  7319. break;
  7320. case I40E_VSI_SRIOV:
  7321. case I40E_VSI_VMDQ2:
  7322. case I40E_VSI_CTRL:
  7323. case I40E_VSI_IWARP:
  7324. case I40E_VSI_MIRROR:
  7325. default:
  7326. /* there is no notification for other VSIs */
  7327. break;
  7328. }
  7329. }
  7330. /**
  7331. * i40e_veb_link_event - notify elements on the veb of a link event
  7332. * @veb: veb to be notified
  7333. * @link_up: link up or down
  7334. **/
  7335. static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
  7336. {
  7337. struct i40e_pf *pf;
  7338. int i;
  7339. if (!veb || !veb->pf)
  7340. return;
  7341. pf = veb->pf;
  7342. /* depth first... */
  7343. for (i = 0; i < I40E_MAX_VEB; i++)
  7344. if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
  7345. i40e_veb_link_event(pf->veb[i], link_up);
  7346. /* ... now the local VSIs */
  7347. for (i = 0; i < pf->num_alloc_vsi; i++)
  7348. if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
  7349. i40e_vsi_link_event(pf->vsi[i], link_up);
  7350. }
  7351. /**
  7352. * i40e_link_event - Update netif_carrier status
  7353. * @pf: board private structure
  7354. **/
  7355. static void i40e_link_event(struct i40e_pf *pf)
  7356. {
  7357. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  7358. u8 new_link_speed, old_link_speed;
  7359. i40e_status status;
  7360. bool new_link, old_link;
  7361. /* save off old link status information */
  7362. pf->hw.phy.link_info_old = pf->hw.phy.link_info;
  7363. /* set this to force the get_link_status call to refresh state */
  7364. pf->hw.phy.get_link_info = true;
  7365. old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
  7366. status = i40e_get_link_status(&pf->hw, &new_link);
  7367. /* On success, disable temp link polling */
  7368. if (status == I40E_SUCCESS) {
  7369. clear_bit(__I40E_TEMP_LINK_POLLING, pf->state);
  7370. } else {
  7371. /* Enable link polling temporarily until i40e_get_link_status
  7372. * returns I40E_SUCCESS
  7373. */
  7374. set_bit(__I40E_TEMP_LINK_POLLING, pf->state);
  7375. dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
  7376. status);
  7377. return;
  7378. }
  7379. old_link_speed = pf->hw.phy.link_info_old.link_speed;
  7380. new_link_speed = pf->hw.phy.link_info.link_speed;
  7381. if (new_link == old_link &&
  7382. new_link_speed == old_link_speed &&
  7383. (test_bit(__I40E_VSI_DOWN, vsi->state) ||
  7384. new_link == netif_carrier_ok(vsi->netdev)))
  7385. return;
  7386. i40e_print_link_message(vsi, new_link);
  7387. /* Notify the base of the switch tree connected to
  7388. * the link. Floating VEBs are not notified.
  7389. */
  7390. if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
  7391. i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
  7392. else
  7393. i40e_vsi_link_event(vsi, new_link);
  7394. if (pf->vf)
  7395. i40e_vc_notify_link_state(pf);
  7396. if (pf->flags & I40E_FLAG_PTP)
  7397. i40e_ptp_set_increment(pf);
  7398. }
  7399. /**
  7400. * i40e_watchdog_subtask - periodic checks not using event driven response
  7401. * @pf: board private structure
  7402. **/
  7403. static void i40e_watchdog_subtask(struct i40e_pf *pf)
  7404. {
  7405. int i;
  7406. /* if interface is down do nothing */
  7407. if (test_bit(__I40E_DOWN, pf->state) ||
  7408. test_bit(__I40E_CONFIG_BUSY, pf->state))
  7409. return;
  7410. /* make sure we don't do these things too often */
  7411. if (time_before(jiffies, (pf->service_timer_previous +
  7412. pf->service_timer_period)))
  7413. return;
  7414. pf->service_timer_previous = jiffies;
  7415. if ((pf->flags & I40E_FLAG_LINK_POLLING_ENABLED) ||
  7416. test_bit(__I40E_TEMP_LINK_POLLING, pf->state))
  7417. i40e_link_event(pf);
  7418. /* Update the stats for active netdevs so the network stack
  7419. * can look at updated numbers whenever it cares to
  7420. */
  7421. for (i = 0; i < pf->num_alloc_vsi; i++)
  7422. if (pf->vsi[i] && pf->vsi[i]->netdev)
  7423. i40e_update_stats(pf->vsi[i]);
  7424. if (pf->flags & I40E_FLAG_VEB_STATS_ENABLED) {
  7425. /* Update the stats for the active switching components */
  7426. for (i = 0; i < I40E_MAX_VEB; i++)
  7427. if (pf->veb[i])
  7428. i40e_update_veb_stats(pf->veb[i]);
  7429. }
  7430. i40e_ptp_rx_hang(pf);
  7431. i40e_ptp_tx_hang(pf);
  7432. }
  7433. /**
  7434. * i40e_reset_subtask - Set up for resetting the device and driver
  7435. * @pf: board private structure
  7436. **/
  7437. static void i40e_reset_subtask(struct i40e_pf *pf)
  7438. {
  7439. u32 reset_flags = 0;
  7440. if (test_bit(__I40E_REINIT_REQUESTED, pf->state)) {
  7441. reset_flags |= BIT(__I40E_REINIT_REQUESTED);
  7442. clear_bit(__I40E_REINIT_REQUESTED, pf->state);
  7443. }
  7444. if (test_bit(__I40E_PF_RESET_REQUESTED, pf->state)) {
  7445. reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
  7446. clear_bit(__I40E_PF_RESET_REQUESTED, pf->state);
  7447. }
  7448. if (test_bit(__I40E_CORE_RESET_REQUESTED, pf->state)) {
  7449. reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
  7450. clear_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
  7451. }
  7452. if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state)) {
  7453. reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
  7454. clear_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
  7455. }
  7456. if (test_bit(__I40E_DOWN_REQUESTED, pf->state)) {
  7457. reset_flags |= BIT(__I40E_DOWN_REQUESTED);
  7458. clear_bit(__I40E_DOWN_REQUESTED, pf->state);
  7459. }
  7460. /* If there's a recovery already waiting, it takes
  7461. * precedence before starting a new reset sequence.
  7462. */
  7463. if (test_bit(__I40E_RESET_INTR_RECEIVED, pf->state)) {
  7464. i40e_prep_for_reset(pf, false);
  7465. i40e_reset(pf);
  7466. i40e_rebuild(pf, false, false);
  7467. }
  7468. /* If we're already down or resetting, just bail */
  7469. if (reset_flags &&
  7470. !test_bit(__I40E_DOWN, pf->state) &&
  7471. !test_bit(__I40E_CONFIG_BUSY, pf->state)) {
  7472. i40e_do_reset(pf, reset_flags, false);
  7473. }
  7474. }
  7475. /**
  7476. * i40e_handle_link_event - Handle link event
  7477. * @pf: board private structure
  7478. * @e: event info posted on ARQ
  7479. **/
  7480. static void i40e_handle_link_event(struct i40e_pf *pf,
  7481. struct i40e_arq_event_info *e)
  7482. {
  7483. struct i40e_aqc_get_link_status *status =
  7484. (struct i40e_aqc_get_link_status *)&e->desc.params.raw;
  7485. /* Do a new status request to re-enable LSE reporting
  7486. * and load new status information into the hw struct
  7487. * This completely ignores any state information
  7488. * in the ARQ event info, instead choosing to always
  7489. * issue the AQ update link status command.
  7490. */
  7491. i40e_link_event(pf);
  7492. /* Check if module meets thermal requirements */
  7493. if (status->phy_type == I40E_PHY_TYPE_NOT_SUPPORTED_HIGH_TEMP) {
  7494. dev_err(&pf->pdev->dev,
  7495. "Rx/Tx is disabled on this device because the module does not meet thermal requirements.\n");
  7496. dev_err(&pf->pdev->dev,
  7497. "Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
  7498. } else {
  7499. /* check for unqualified module, if link is down, suppress
  7500. * the message if link was forced to be down.
  7501. */
  7502. if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
  7503. (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
  7504. (!(status->link_info & I40E_AQ_LINK_UP)) &&
  7505. (!(pf->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED))) {
  7506. dev_err(&pf->pdev->dev,
  7507. "Rx/Tx is disabled on this device because an unsupported SFP module type was detected.\n");
  7508. dev_err(&pf->pdev->dev,
  7509. "Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
  7510. }
  7511. }
  7512. }
  7513. /**
  7514. * i40e_clean_adminq_subtask - Clean the AdminQ rings
  7515. * @pf: board private structure
  7516. **/
  7517. static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
  7518. {
  7519. struct i40e_arq_event_info event;
  7520. struct i40e_hw *hw = &pf->hw;
  7521. u16 pending, i = 0;
  7522. i40e_status ret;
  7523. u16 opcode;
  7524. u32 oldval;
  7525. u32 val;
  7526. /* Do not run clean AQ when PF reset fails */
  7527. if (test_bit(__I40E_RESET_FAILED, pf->state))
  7528. return;
  7529. /* check for error indications */
  7530. val = rd32(&pf->hw, pf->hw.aq.arq.len);
  7531. oldval = val;
  7532. if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
  7533. if (hw->debug_mask & I40E_DEBUG_AQ)
  7534. dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
  7535. val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
  7536. }
  7537. if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
  7538. if (hw->debug_mask & I40E_DEBUG_AQ)
  7539. dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
  7540. val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
  7541. pf->arq_overflows++;
  7542. }
  7543. if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
  7544. if (hw->debug_mask & I40E_DEBUG_AQ)
  7545. dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
  7546. val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
  7547. }
  7548. if (oldval != val)
  7549. wr32(&pf->hw, pf->hw.aq.arq.len, val);
  7550. val = rd32(&pf->hw, pf->hw.aq.asq.len);
  7551. oldval = val;
  7552. if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
  7553. if (pf->hw.debug_mask & I40E_DEBUG_AQ)
  7554. dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
  7555. val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
  7556. }
  7557. if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
  7558. if (pf->hw.debug_mask & I40E_DEBUG_AQ)
  7559. dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
  7560. val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
  7561. }
  7562. if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
  7563. if (pf->hw.debug_mask & I40E_DEBUG_AQ)
  7564. dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
  7565. val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
  7566. }
  7567. if (oldval != val)
  7568. wr32(&pf->hw, pf->hw.aq.asq.len, val);
  7569. event.buf_len = I40E_MAX_AQ_BUF_SIZE;
  7570. event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
  7571. if (!event.msg_buf)
  7572. return;
  7573. do {
  7574. ret = i40e_clean_arq_element(hw, &event, &pending);
  7575. if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK)
  7576. break;
  7577. else if (ret) {
  7578. dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
  7579. break;
  7580. }
  7581. opcode = le16_to_cpu(event.desc.opcode);
  7582. switch (opcode) {
  7583. case i40e_aqc_opc_get_link_status:
  7584. i40e_handle_link_event(pf, &event);
  7585. break;
  7586. case i40e_aqc_opc_send_msg_to_pf:
  7587. ret = i40e_vc_process_vf_msg(pf,
  7588. le16_to_cpu(event.desc.retval),
  7589. le32_to_cpu(event.desc.cookie_high),
  7590. le32_to_cpu(event.desc.cookie_low),
  7591. event.msg_buf,
  7592. event.msg_len);
  7593. break;
  7594. case i40e_aqc_opc_lldp_update_mib:
  7595. dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
  7596. #ifdef CONFIG_I40E_DCB
  7597. rtnl_lock();
  7598. ret = i40e_handle_lldp_event(pf, &event);
  7599. rtnl_unlock();
  7600. #endif /* CONFIG_I40E_DCB */
  7601. break;
  7602. case i40e_aqc_opc_event_lan_overflow:
  7603. dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
  7604. i40e_handle_lan_overflow_event(pf, &event);
  7605. break;
  7606. case i40e_aqc_opc_send_msg_to_peer:
  7607. dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
  7608. break;
  7609. case i40e_aqc_opc_nvm_erase:
  7610. case i40e_aqc_opc_nvm_update:
  7611. case i40e_aqc_opc_oem_post_update:
  7612. i40e_debug(&pf->hw, I40E_DEBUG_NVM,
  7613. "ARQ NVM operation 0x%04x completed\n",
  7614. opcode);
  7615. break;
  7616. default:
  7617. dev_info(&pf->pdev->dev,
  7618. "ARQ: Unknown event 0x%04x ignored\n",
  7619. opcode);
  7620. break;
  7621. }
  7622. } while (i++ < pf->adminq_work_limit);
  7623. if (i < pf->adminq_work_limit)
  7624. clear_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
  7625. /* re-enable Admin queue interrupt cause */
  7626. val = rd32(hw, I40E_PFINT_ICR0_ENA);
  7627. val |= I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
  7628. wr32(hw, I40E_PFINT_ICR0_ENA, val);
  7629. i40e_flush(hw);
  7630. kfree(event.msg_buf);
  7631. }
  7632. /**
  7633. * i40e_verify_eeprom - make sure eeprom is good to use
  7634. * @pf: board private structure
  7635. **/
  7636. static void i40e_verify_eeprom(struct i40e_pf *pf)
  7637. {
  7638. int err;
  7639. err = i40e_diag_eeprom_test(&pf->hw);
  7640. if (err) {
  7641. /* retry in case of garbage read */
  7642. err = i40e_diag_eeprom_test(&pf->hw);
  7643. if (err) {
  7644. dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
  7645. err);
  7646. set_bit(__I40E_BAD_EEPROM, pf->state);
  7647. }
  7648. }
  7649. if (!err && test_bit(__I40E_BAD_EEPROM, pf->state)) {
  7650. dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
  7651. clear_bit(__I40E_BAD_EEPROM, pf->state);
  7652. }
  7653. }
  7654. /**
  7655. * i40e_enable_pf_switch_lb
  7656. * @pf: pointer to the PF structure
  7657. *
  7658. * enable switch loop back or die - no point in a return value
  7659. **/
  7660. static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
  7661. {
  7662. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  7663. struct i40e_vsi_context ctxt;
  7664. int ret;
  7665. ctxt.seid = pf->main_vsi_seid;
  7666. ctxt.pf_num = pf->hw.pf_id;
  7667. ctxt.vf_num = 0;
  7668. ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
  7669. if (ret) {
  7670. dev_info(&pf->pdev->dev,
  7671. "couldn't get PF vsi config, err %s aq_err %s\n",
  7672. i40e_stat_str(&pf->hw, ret),
  7673. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  7674. return;
  7675. }
  7676. ctxt.flags = I40E_AQ_VSI_TYPE_PF;
  7677. ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  7678. ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  7679. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  7680. if (ret) {
  7681. dev_info(&pf->pdev->dev,
  7682. "update vsi switch failed, err %s aq_err %s\n",
  7683. i40e_stat_str(&pf->hw, ret),
  7684. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  7685. }
  7686. }
  7687. /**
  7688. * i40e_disable_pf_switch_lb
  7689. * @pf: pointer to the PF structure
  7690. *
  7691. * disable switch loop back or die - no point in a return value
  7692. **/
  7693. static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
  7694. {
  7695. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  7696. struct i40e_vsi_context ctxt;
  7697. int ret;
  7698. ctxt.seid = pf->main_vsi_seid;
  7699. ctxt.pf_num = pf->hw.pf_id;
  7700. ctxt.vf_num = 0;
  7701. ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
  7702. if (ret) {
  7703. dev_info(&pf->pdev->dev,
  7704. "couldn't get PF vsi config, err %s aq_err %s\n",
  7705. i40e_stat_str(&pf->hw, ret),
  7706. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  7707. return;
  7708. }
  7709. ctxt.flags = I40E_AQ_VSI_TYPE_PF;
  7710. ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  7711. ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  7712. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  7713. if (ret) {
  7714. dev_info(&pf->pdev->dev,
  7715. "update vsi switch failed, err %s aq_err %s\n",
  7716. i40e_stat_str(&pf->hw, ret),
  7717. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  7718. }
  7719. }
  7720. /**
  7721. * i40e_config_bridge_mode - Configure the HW bridge mode
  7722. * @veb: pointer to the bridge instance
  7723. *
  7724. * Configure the loop back mode for the LAN VSI that is downlink to the
  7725. * specified HW bridge instance. It is expected this function is called
  7726. * when a new HW bridge is instantiated.
  7727. **/
  7728. static void i40e_config_bridge_mode(struct i40e_veb *veb)
  7729. {
  7730. struct i40e_pf *pf = veb->pf;
  7731. if (pf->hw.debug_mask & I40E_DEBUG_LAN)
  7732. dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
  7733. veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
  7734. if (veb->bridge_mode & BRIDGE_MODE_VEPA)
  7735. i40e_disable_pf_switch_lb(pf);
  7736. else
  7737. i40e_enable_pf_switch_lb(pf);
  7738. }
  7739. /**
  7740. * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
  7741. * @veb: pointer to the VEB instance
  7742. *
  7743. * This is a recursive function that first builds the attached VSIs then
  7744. * recurses in to build the next layer of VEB. We track the connections
  7745. * through our own index numbers because the seid's from the HW could
  7746. * change across the reset.
  7747. **/
  7748. static int i40e_reconstitute_veb(struct i40e_veb *veb)
  7749. {
  7750. struct i40e_vsi *ctl_vsi = NULL;
  7751. struct i40e_pf *pf = veb->pf;
  7752. int v, veb_idx;
  7753. int ret;
  7754. /* build VSI that owns this VEB, temporarily attached to base VEB */
  7755. for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
  7756. if (pf->vsi[v] &&
  7757. pf->vsi[v]->veb_idx == veb->idx &&
  7758. pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
  7759. ctl_vsi = pf->vsi[v];
  7760. break;
  7761. }
  7762. }
  7763. if (!ctl_vsi) {
  7764. dev_info(&pf->pdev->dev,
  7765. "missing owner VSI for veb_idx %d\n", veb->idx);
  7766. ret = -ENOENT;
  7767. goto end_reconstitute;
  7768. }
  7769. if (ctl_vsi != pf->vsi[pf->lan_vsi])
  7770. ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
  7771. ret = i40e_add_vsi(ctl_vsi);
  7772. if (ret) {
  7773. dev_info(&pf->pdev->dev,
  7774. "rebuild of veb_idx %d owner VSI failed: %d\n",
  7775. veb->idx, ret);
  7776. goto end_reconstitute;
  7777. }
  7778. i40e_vsi_reset_stats(ctl_vsi);
  7779. /* create the VEB in the switch and move the VSI onto the VEB */
  7780. ret = i40e_add_veb(veb, ctl_vsi);
  7781. if (ret)
  7782. goto end_reconstitute;
  7783. if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
  7784. veb->bridge_mode = BRIDGE_MODE_VEB;
  7785. else
  7786. veb->bridge_mode = BRIDGE_MODE_VEPA;
  7787. i40e_config_bridge_mode(veb);
  7788. /* create the remaining VSIs attached to this VEB */
  7789. for (v = 0; v < pf->num_alloc_vsi; v++) {
  7790. if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
  7791. continue;
  7792. if (pf->vsi[v]->veb_idx == veb->idx) {
  7793. struct i40e_vsi *vsi = pf->vsi[v];
  7794. vsi->uplink_seid = veb->seid;
  7795. ret = i40e_add_vsi(vsi);
  7796. if (ret) {
  7797. dev_info(&pf->pdev->dev,
  7798. "rebuild of vsi_idx %d failed: %d\n",
  7799. v, ret);
  7800. goto end_reconstitute;
  7801. }
  7802. i40e_vsi_reset_stats(vsi);
  7803. }
  7804. }
  7805. /* create any VEBs attached to this VEB - RECURSION */
  7806. for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
  7807. if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
  7808. pf->veb[veb_idx]->uplink_seid = veb->seid;
  7809. ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
  7810. if (ret)
  7811. break;
  7812. }
  7813. }
  7814. end_reconstitute:
  7815. return ret;
  7816. }
  7817. /**
  7818. * i40e_get_capabilities - get info about the HW
  7819. * @pf: the PF struct
  7820. **/
  7821. static int i40e_get_capabilities(struct i40e_pf *pf,
  7822. enum i40e_admin_queue_opc list_type)
  7823. {
  7824. struct i40e_aqc_list_capabilities_element_resp *cap_buf;
  7825. u16 data_size;
  7826. int buf_len;
  7827. int err;
  7828. buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
  7829. do {
  7830. cap_buf = kzalloc(buf_len, GFP_KERNEL);
  7831. if (!cap_buf)
  7832. return -ENOMEM;
  7833. /* this loads the data into the hw struct for us */
  7834. err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
  7835. &data_size, list_type,
  7836. NULL);
  7837. /* data loaded, buffer no longer needed */
  7838. kfree(cap_buf);
  7839. if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
  7840. /* retry with a larger buffer */
  7841. buf_len = data_size;
  7842. } else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK) {
  7843. dev_info(&pf->pdev->dev,
  7844. "capability discovery failed, err %s aq_err %s\n",
  7845. i40e_stat_str(&pf->hw, err),
  7846. i40e_aq_str(&pf->hw,
  7847. pf->hw.aq.asq_last_status));
  7848. return -ENODEV;
  7849. }
  7850. } while (err);
  7851. if (pf->hw.debug_mask & I40E_DEBUG_USER) {
  7852. if (list_type == i40e_aqc_opc_list_func_capabilities) {
  7853. dev_info(&pf->pdev->dev,
  7854. "pf=%d, num_vfs=%d, msix_pf=%d, msix_vf=%d, fd_g=%d, fd_b=%d, pf_max_q=%d num_vsi=%d\n",
  7855. pf->hw.pf_id, pf->hw.func_caps.num_vfs,
  7856. pf->hw.func_caps.num_msix_vectors,
  7857. pf->hw.func_caps.num_msix_vectors_vf,
  7858. pf->hw.func_caps.fd_filters_guaranteed,
  7859. pf->hw.func_caps.fd_filters_best_effort,
  7860. pf->hw.func_caps.num_tx_qp,
  7861. pf->hw.func_caps.num_vsis);
  7862. } else if (list_type == i40e_aqc_opc_list_dev_capabilities) {
  7863. dev_info(&pf->pdev->dev,
  7864. "switch_mode=0x%04x, function_valid=0x%08x\n",
  7865. pf->hw.dev_caps.switch_mode,
  7866. pf->hw.dev_caps.valid_functions);
  7867. dev_info(&pf->pdev->dev,
  7868. "SR-IOV=%d, num_vfs for all function=%u\n",
  7869. pf->hw.dev_caps.sr_iov_1_1,
  7870. pf->hw.dev_caps.num_vfs);
  7871. dev_info(&pf->pdev->dev,
  7872. "num_vsis=%u, num_rx:%u, num_tx=%u\n",
  7873. pf->hw.dev_caps.num_vsis,
  7874. pf->hw.dev_caps.num_rx_qp,
  7875. pf->hw.dev_caps.num_tx_qp);
  7876. }
  7877. }
  7878. if (list_type == i40e_aqc_opc_list_func_capabilities) {
  7879. #define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
  7880. + pf->hw.func_caps.num_vfs)
  7881. if (pf->hw.revision_id == 0 &&
  7882. pf->hw.func_caps.num_vsis < DEF_NUM_VSI) {
  7883. dev_info(&pf->pdev->dev,
  7884. "got num_vsis %d, setting num_vsis to %d\n",
  7885. pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
  7886. pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
  7887. }
  7888. }
  7889. return 0;
  7890. }
  7891. static int i40e_vsi_clear(struct i40e_vsi *vsi);
  7892. /**
  7893. * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
  7894. * @pf: board private structure
  7895. **/
  7896. static void i40e_fdir_sb_setup(struct i40e_pf *pf)
  7897. {
  7898. struct i40e_vsi *vsi;
  7899. /* quick workaround for an NVM issue that leaves a critical register
  7900. * uninitialized
  7901. */
  7902. if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
  7903. static const u32 hkey[] = {
  7904. 0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
  7905. 0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
  7906. 0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
  7907. 0x95b3a76d};
  7908. int i;
  7909. for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
  7910. wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
  7911. }
  7912. if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
  7913. return;
  7914. /* find existing VSI and see if it needs configuring */
  7915. vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
  7916. /* create a new VSI if none exists */
  7917. if (!vsi) {
  7918. vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
  7919. pf->vsi[pf->lan_vsi]->seid, 0);
  7920. if (!vsi) {
  7921. dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
  7922. pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
  7923. pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
  7924. return;
  7925. }
  7926. }
  7927. i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
  7928. }
  7929. /**
  7930. * i40e_fdir_teardown - release the Flow Director resources
  7931. * @pf: board private structure
  7932. **/
  7933. static void i40e_fdir_teardown(struct i40e_pf *pf)
  7934. {
  7935. struct i40e_vsi *vsi;
  7936. i40e_fdir_filter_exit(pf);
  7937. vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
  7938. if (vsi)
  7939. i40e_vsi_release(vsi);
  7940. }
  7941. /**
  7942. * i40e_rebuild_cloud_filters - Rebuilds cloud filters for VSIs
  7943. * @vsi: PF main vsi
  7944. * @seid: seid of main or channel VSIs
  7945. *
  7946. * Rebuilds cloud filters associated with main VSI and channel VSIs if they
  7947. * existed before reset
  7948. **/
  7949. static int i40e_rebuild_cloud_filters(struct i40e_vsi *vsi, u16 seid)
  7950. {
  7951. struct i40e_cloud_filter *cfilter;
  7952. struct i40e_pf *pf = vsi->back;
  7953. struct hlist_node *node;
  7954. i40e_status ret;
  7955. /* Add cloud filters back if they exist */
  7956. hlist_for_each_entry_safe(cfilter, node, &pf->cloud_filter_list,
  7957. cloud_node) {
  7958. if (cfilter->seid != seid)
  7959. continue;
  7960. if (cfilter->dst_port)
  7961. ret = i40e_add_del_cloud_filter_big_buf(vsi, cfilter,
  7962. true);
  7963. else
  7964. ret = i40e_add_del_cloud_filter(vsi, cfilter, true);
  7965. if (ret) {
  7966. dev_dbg(&pf->pdev->dev,
  7967. "Failed to rebuild cloud filter, err %s aq_err %s\n",
  7968. i40e_stat_str(&pf->hw, ret),
  7969. i40e_aq_str(&pf->hw,
  7970. pf->hw.aq.asq_last_status));
  7971. return ret;
  7972. }
  7973. }
  7974. return 0;
  7975. }
  7976. /**
  7977. * i40e_rebuild_channels - Rebuilds channel VSIs if they existed before reset
  7978. * @vsi: PF main vsi
  7979. *
  7980. * Rebuilds channel VSIs if they existed before reset
  7981. **/
  7982. static int i40e_rebuild_channels(struct i40e_vsi *vsi)
  7983. {
  7984. struct i40e_channel *ch, *ch_tmp;
  7985. i40e_status ret;
  7986. if (list_empty(&vsi->ch_list))
  7987. return 0;
  7988. list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
  7989. if (!ch->initialized)
  7990. break;
  7991. /* Proceed with creation of channel (VMDq2) VSI */
  7992. ret = i40e_add_channel(vsi->back, vsi->uplink_seid, ch);
  7993. if (ret) {
  7994. dev_info(&vsi->back->pdev->dev,
  7995. "failed to rebuild channels using uplink_seid %u\n",
  7996. vsi->uplink_seid);
  7997. return ret;
  7998. }
  7999. /* Reconfigure TX queues using QTX_CTL register */
  8000. ret = i40e_channel_config_tx_ring(vsi->back, vsi, ch);
  8001. if (ret) {
  8002. dev_info(&vsi->back->pdev->dev,
  8003. "failed to configure TX rings for channel %u\n",
  8004. ch->seid);
  8005. return ret;
  8006. }
  8007. /* update 'next_base_queue' */
  8008. vsi->next_base_queue = vsi->next_base_queue +
  8009. ch->num_queue_pairs;
  8010. if (ch->max_tx_rate) {
  8011. u64 credits = ch->max_tx_rate;
  8012. if (i40e_set_bw_limit(vsi, ch->seid,
  8013. ch->max_tx_rate))
  8014. return -EINVAL;
  8015. do_div(credits, I40E_BW_CREDIT_DIVISOR);
  8016. dev_dbg(&vsi->back->pdev->dev,
  8017. "Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
  8018. ch->max_tx_rate,
  8019. credits,
  8020. ch->seid);
  8021. }
  8022. ret = i40e_rebuild_cloud_filters(vsi, ch->seid);
  8023. if (ret) {
  8024. dev_dbg(&vsi->back->pdev->dev,
  8025. "Failed to rebuild cloud filters for channel VSI %u\n",
  8026. ch->seid);
  8027. return ret;
  8028. }
  8029. }
  8030. return 0;
  8031. }
  8032. /**
  8033. * i40e_prep_for_reset - prep for the core to reset
  8034. * @pf: board private structure
  8035. * @lock_acquired: indicates whether or not the lock has been acquired
  8036. * before this function was called.
  8037. *
  8038. * Close up the VFs and other things in prep for PF Reset.
  8039. **/
  8040. static void i40e_prep_for_reset(struct i40e_pf *pf, bool lock_acquired)
  8041. {
  8042. struct i40e_hw *hw = &pf->hw;
  8043. i40e_status ret = 0;
  8044. u32 v;
  8045. clear_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
  8046. if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
  8047. return;
  8048. if (i40e_check_asq_alive(&pf->hw))
  8049. i40e_vc_notify_reset(pf);
  8050. dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
  8051. /* quiesce the VSIs and their queues that are not already DOWN */
  8052. /* pf_quiesce_all_vsi modifies netdev structures -rtnl_lock needed */
  8053. if (!lock_acquired)
  8054. rtnl_lock();
  8055. i40e_pf_quiesce_all_vsi(pf);
  8056. if (!lock_acquired)
  8057. rtnl_unlock();
  8058. for (v = 0; v < pf->num_alloc_vsi; v++) {
  8059. if (pf->vsi[v])
  8060. pf->vsi[v]->seid = 0;
  8061. }
  8062. i40e_shutdown_adminq(&pf->hw);
  8063. /* call shutdown HMC */
  8064. if (hw->hmc.hmc_obj) {
  8065. ret = i40e_shutdown_lan_hmc(hw);
  8066. if (ret)
  8067. dev_warn(&pf->pdev->dev,
  8068. "shutdown_lan_hmc failed: %d\n", ret);
  8069. }
  8070. }
  8071. /**
  8072. * i40e_send_version - update firmware with driver version
  8073. * @pf: PF struct
  8074. */
  8075. static void i40e_send_version(struct i40e_pf *pf)
  8076. {
  8077. struct i40e_driver_version dv;
  8078. dv.major_version = DRV_VERSION_MAJOR;
  8079. dv.minor_version = DRV_VERSION_MINOR;
  8080. dv.build_version = DRV_VERSION_BUILD;
  8081. dv.subbuild_version = 0;
  8082. strlcpy(dv.driver_string, DRV_VERSION, sizeof(dv.driver_string));
  8083. i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
  8084. }
  8085. /**
  8086. * i40e_get_oem_version - get OEM specific version information
  8087. * @hw: pointer to the hardware structure
  8088. **/
  8089. static void i40e_get_oem_version(struct i40e_hw *hw)
  8090. {
  8091. u16 block_offset = 0xffff;
  8092. u16 block_length = 0;
  8093. u16 capabilities = 0;
  8094. u16 gen_snap = 0;
  8095. u16 release = 0;
  8096. #define I40E_SR_NVM_OEM_VERSION_PTR 0x1B
  8097. #define I40E_NVM_OEM_LENGTH_OFFSET 0x00
  8098. #define I40E_NVM_OEM_CAPABILITIES_OFFSET 0x01
  8099. #define I40E_NVM_OEM_GEN_OFFSET 0x02
  8100. #define I40E_NVM_OEM_RELEASE_OFFSET 0x03
  8101. #define I40E_NVM_OEM_CAPABILITIES_MASK 0x000F
  8102. #define I40E_NVM_OEM_LENGTH 3
  8103. /* Check if pointer to OEM version block is valid. */
  8104. i40e_read_nvm_word(hw, I40E_SR_NVM_OEM_VERSION_PTR, &block_offset);
  8105. if (block_offset == 0xffff)
  8106. return;
  8107. /* Check if OEM version block has correct length. */
  8108. i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_LENGTH_OFFSET,
  8109. &block_length);
  8110. if (block_length < I40E_NVM_OEM_LENGTH)
  8111. return;
  8112. /* Check if OEM version format is as expected. */
  8113. i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_CAPABILITIES_OFFSET,
  8114. &capabilities);
  8115. if ((capabilities & I40E_NVM_OEM_CAPABILITIES_MASK) != 0)
  8116. return;
  8117. i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_GEN_OFFSET,
  8118. &gen_snap);
  8119. i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_RELEASE_OFFSET,
  8120. &release);
  8121. hw->nvm.oem_ver = (gen_snap << I40E_OEM_SNAP_SHIFT) | release;
  8122. hw->nvm.eetrack = I40E_OEM_EETRACK_ID;
  8123. }
  8124. /**
  8125. * i40e_reset - wait for core reset to finish reset, reset pf if corer not seen
  8126. * @pf: board private structure
  8127. **/
  8128. static int i40e_reset(struct i40e_pf *pf)
  8129. {
  8130. struct i40e_hw *hw = &pf->hw;
  8131. i40e_status ret;
  8132. ret = i40e_pf_reset(hw);
  8133. if (ret) {
  8134. dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
  8135. set_bit(__I40E_RESET_FAILED, pf->state);
  8136. clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
  8137. } else {
  8138. pf->pfr_count++;
  8139. }
  8140. return ret;
  8141. }
  8142. /**
  8143. * i40e_rebuild - rebuild using a saved config
  8144. * @pf: board private structure
  8145. * @reinit: if the Main VSI needs to re-initialized.
  8146. * @lock_acquired: indicates whether or not the lock has been acquired
  8147. * before this function was called.
  8148. **/
  8149. static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired)
  8150. {
  8151. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  8152. struct i40e_hw *hw = &pf->hw;
  8153. u8 set_fc_aq_fail = 0;
  8154. i40e_status ret;
  8155. u32 val;
  8156. int v;
  8157. if (test_bit(__I40E_DOWN, pf->state))
  8158. goto clear_recovery;
  8159. dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
  8160. /* rebuild the basics for the AdminQ, HMC, and initial HW switch */
  8161. ret = i40e_init_adminq(&pf->hw);
  8162. if (ret) {
  8163. dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %s aq_err %s\n",
  8164. i40e_stat_str(&pf->hw, ret),
  8165. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  8166. goto clear_recovery;
  8167. }
  8168. i40e_get_oem_version(&pf->hw);
  8169. if (test_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state) &&
  8170. ((hw->aq.fw_maj_ver == 4 && hw->aq.fw_min_ver <= 33) ||
  8171. hw->aq.fw_maj_ver < 4) && hw->mac.type == I40E_MAC_XL710) {
  8172. /* The following delay is necessary for 4.33 firmware and older
  8173. * to recover after EMP reset. 200 ms should suffice but we
  8174. * put here 300 ms to be sure that FW is ready to operate
  8175. * after reset.
  8176. */
  8177. mdelay(300);
  8178. }
  8179. /* re-verify the eeprom if we just had an EMP reset */
  8180. if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state))
  8181. i40e_verify_eeprom(pf);
  8182. i40e_clear_pxe_mode(hw);
  8183. ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
  8184. if (ret)
  8185. goto end_core_reset;
  8186. ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
  8187. hw->func_caps.num_rx_qp, 0, 0);
  8188. if (ret) {
  8189. dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
  8190. goto end_core_reset;
  8191. }
  8192. ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
  8193. if (ret) {
  8194. dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
  8195. goto end_core_reset;
  8196. }
  8197. /* Enable FW to write a default DCB config on link-up */
  8198. i40e_aq_set_dcb_parameters(hw, true, NULL);
  8199. #ifdef CONFIG_I40E_DCB
  8200. ret = i40e_init_pf_dcb(pf);
  8201. if (ret) {
  8202. dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n", ret);
  8203. pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
  8204. /* Continue without DCB enabled */
  8205. }
  8206. #endif /* CONFIG_I40E_DCB */
  8207. /* do basic switch setup */
  8208. if (!lock_acquired)
  8209. rtnl_lock();
  8210. ret = i40e_setup_pf_switch(pf, reinit);
  8211. if (ret)
  8212. goto end_unlock;
  8213. /* The driver only wants link up/down and module qualification
  8214. * reports from firmware. Note the negative logic.
  8215. */
  8216. ret = i40e_aq_set_phy_int_mask(&pf->hw,
  8217. ~(I40E_AQ_EVENT_LINK_UPDOWN |
  8218. I40E_AQ_EVENT_MEDIA_NA |
  8219. I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
  8220. if (ret)
  8221. dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
  8222. i40e_stat_str(&pf->hw, ret),
  8223. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  8224. /* make sure our flow control settings are restored */
  8225. ret = i40e_set_fc(&pf->hw, &set_fc_aq_fail, true);
  8226. if (ret)
  8227. dev_dbg(&pf->pdev->dev, "setting flow control: ret = %s last_status = %s\n",
  8228. i40e_stat_str(&pf->hw, ret),
  8229. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  8230. /* Rebuild the VSIs and VEBs that existed before reset.
  8231. * They are still in our local switch element arrays, so only
  8232. * need to rebuild the switch model in the HW.
  8233. *
  8234. * If there were VEBs but the reconstitution failed, we'll try
  8235. * try to recover minimal use by getting the basic PF VSI working.
  8236. */
  8237. if (vsi->uplink_seid != pf->mac_seid) {
  8238. dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
  8239. /* find the one VEB connected to the MAC, and find orphans */
  8240. for (v = 0; v < I40E_MAX_VEB; v++) {
  8241. if (!pf->veb[v])
  8242. continue;
  8243. if (pf->veb[v]->uplink_seid == pf->mac_seid ||
  8244. pf->veb[v]->uplink_seid == 0) {
  8245. ret = i40e_reconstitute_veb(pf->veb[v]);
  8246. if (!ret)
  8247. continue;
  8248. /* If Main VEB failed, we're in deep doodoo,
  8249. * so give up rebuilding the switch and set up
  8250. * for minimal rebuild of PF VSI.
  8251. * If orphan failed, we'll report the error
  8252. * but try to keep going.
  8253. */
  8254. if (pf->veb[v]->uplink_seid == pf->mac_seid) {
  8255. dev_info(&pf->pdev->dev,
  8256. "rebuild of switch failed: %d, will try to set up simple PF connection\n",
  8257. ret);
  8258. vsi->uplink_seid = pf->mac_seid;
  8259. break;
  8260. } else if (pf->veb[v]->uplink_seid == 0) {
  8261. dev_info(&pf->pdev->dev,
  8262. "rebuild of orphan VEB failed: %d\n",
  8263. ret);
  8264. }
  8265. }
  8266. }
  8267. }
  8268. if (vsi->uplink_seid == pf->mac_seid) {
  8269. dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
  8270. /* no VEB, so rebuild only the Main VSI */
  8271. ret = i40e_add_vsi(vsi);
  8272. if (ret) {
  8273. dev_info(&pf->pdev->dev,
  8274. "rebuild of Main VSI failed: %d\n", ret);
  8275. goto end_unlock;
  8276. }
  8277. }
  8278. if (vsi->mqprio_qopt.max_rate[0]) {
  8279. u64 max_tx_rate = vsi->mqprio_qopt.max_rate[0];
  8280. u64 credits = 0;
  8281. do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
  8282. ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
  8283. if (ret)
  8284. goto end_unlock;
  8285. credits = max_tx_rate;
  8286. do_div(credits, I40E_BW_CREDIT_DIVISOR);
  8287. dev_dbg(&vsi->back->pdev->dev,
  8288. "Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
  8289. max_tx_rate,
  8290. credits,
  8291. vsi->seid);
  8292. }
  8293. ret = i40e_rebuild_cloud_filters(vsi, vsi->seid);
  8294. if (ret)
  8295. goto end_unlock;
  8296. /* PF Main VSI is rebuild by now, go ahead and rebuild channel VSIs
  8297. * for this main VSI if they exist
  8298. */
  8299. ret = i40e_rebuild_channels(vsi);
  8300. if (ret)
  8301. goto end_unlock;
  8302. /* Reconfigure hardware for allowing smaller MSS in the case
  8303. * of TSO, so that we avoid the MDD being fired and causing
  8304. * a reset in the case of small MSS+TSO.
  8305. */
  8306. #define I40E_REG_MSS 0x000E64DC
  8307. #define I40E_REG_MSS_MIN_MASK 0x3FF0000
  8308. #define I40E_64BYTE_MSS 0x400000
  8309. val = rd32(hw, I40E_REG_MSS);
  8310. if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
  8311. val &= ~I40E_REG_MSS_MIN_MASK;
  8312. val |= I40E_64BYTE_MSS;
  8313. wr32(hw, I40E_REG_MSS, val);
  8314. }
  8315. if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
  8316. msleep(75);
  8317. ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
  8318. if (ret)
  8319. dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
  8320. i40e_stat_str(&pf->hw, ret),
  8321. i40e_aq_str(&pf->hw,
  8322. pf->hw.aq.asq_last_status));
  8323. }
  8324. /* reinit the misc interrupt */
  8325. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  8326. ret = i40e_setup_misc_vector(pf);
  8327. /* Add a filter to drop all Flow control frames from any VSI from being
  8328. * transmitted. By doing so we stop a malicious VF from sending out
  8329. * PAUSE or PFC frames and potentially controlling traffic for other
  8330. * PF/VF VSIs.
  8331. * The FW can still send Flow control frames if enabled.
  8332. */
  8333. i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
  8334. pf->main_vsi_seid);
  8335. /* restart the VSIs that were rebuilt and running before the reset */
  8336. i40e_pf_unquiesce_all_vsi(pf);
  8337. /* Release the RTNL lock before we start resetting VFs */
  8338. if (!lock_acquired)
  8339. rtnl_unlock();
  8340. /* Restore promiscuous settings */
  8341. ret = i40e_set_promiscuous(pf, pf->cur_promisc);
  8342. if (ret)
  8343. dev_warn(&pf->pdev->dev,
  8344. "Failed to restore promiscuous setting: %s, err %s aq_err %s\n",
  8345. pf->cur_promisc ? "on" : "off",
  8346. i40e_stat_str(&pf->hw, ret),
  8347. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  8348. i40e_reset_all_vfs(pf, true);
  8349. /* tell the firmware that we're starting */
  8350. i40e_send_version(pf);
  8351. /* We've already released the lock, so don't do it again */
  8352. goto end_core_reset;
  8353. end_unlock:
  8354. if (!lock_acquired)
  8355. rtnl_unlock();
  8356. end_core_reset:
  8357. clear_bit(__I40E_RESET_FAILED, pf->state);
  8358. clear_recovery:
  8359. clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
  8360. }
  8361. /**
  8362. * i40e_reset_and_rebuild - reset and rebuild using a saved config
  8363. * @pf: board private structure
  8364. * @reinit: if the Main VSI needs to re-initialized.
  8365. * @lock_acquired: indicates whether or not the lock has been acquired
  8366. * before this function was called.
  8367. **/
  8368. static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
  8369. bool lock_acquired)
  8370. {
  8371. int ret;
  8372. /* Now we wait for GRST to settle out.
  8373. * We don't have to delete the VEBs or VSIs from the hw switch
  8374. * because the reset will make them disappear.
  8375. */
  8376. ret = i40e_reset(pf);
  8377. if (!ret)
  8378. i40e_rebuild(pf, reinit, lock_acquired);
  8379. }
  8380. /**
  8381. * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
  8382. * @pf: board private structure
  8383. *
  8384. * Close up the VFs and other things in prep for a Core Reset,
  8385. * then get ready to rebuild the world.
  8386. * @lock_acquired: indicates whether or not the lock has been acquired
  8387. * before this function was called.
  8388. **/
  8389. static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired)
  8390. {
  8391. i40e_prep_for_reset(pf, lock_acquired);
  8392. i40e_reset_and_rebuild(pf, false, lock_acquired);
  8393. }
  8394. /**
  8395. * i40e_handle_mdd_event
  8396. * @pf: pointer to the PF structure
  8397. *
  8398. * Called from the MDD irq handler to identify possibly malicious vfs
  8399. **/
  8400. static void i40e_handle_mdd_event(struct i40e_pf *pf)
  8401. {
  8402. struct i40e_hw *hw = &pf->hw;
  8403. bool mdd_detected = false;
  8404. bool pf_mdd_detected = false;
  8405. struct i40e_vf *vf;
  8406. u32 reg;
  8407. int i;
  8408. if (!test_bit(__I40E_MDD_EVENT_PENDING, pf->state))
  8409. return;
  8410. /* find what triggered the MDD event */
  8411. reg = rd32(hw, I40E_GL_MDET_TX);
  8412. if (reg & I40E_GL_MDET_TX_VALID_MASK) {
  8413. u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
  8414. I40E_GL_MDET_TX_PF_NUM_SHIFT;
  8415. u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
  8416. I40E_GL_MDET_TX_VF_NUM_SHIFT;
  8417. u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
  8418. I40E_GL_MDET_TX_EVENT_SHIFT;
  8419. u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
  8420. I40E_GL_MDET_TX_QUEUE_SHIFT) -
  8421. pf->hw.func_caps.base_queue;
  8422. if (netif_msg_tx_err(pf))
  8423. dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on TX queue %d PF number 0x%02x VF number 0x%02x\n",
  8424. event, queue, pf_num, vf_num);
  8425. wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
  8426. mdd_detected = true;
  8427. }
  8428. reg = rd32(hw, I40E_GL_MDET_RX);
  8429. if (reg & I40E_GL_MDET_RX_VALID_MASK) {
  8430. u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
  8431. I40E_GL_MDET_RX_FUNCTION_SHIFT;
  8432. u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
  8433. I40E_GL_MDET_RX_EVENT_SHIFT;
  8434. u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
  8435. I40E_GL_MDET_RX_QUEUE_SHIFT) -
  8436. pf->hw.func_caps.base_queue;
  8437. if (netif_msg_rx_err(pf))
  8438. dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
  8439. event, queue, func);
  8440. wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
  8441. mdd_detected = true;
  8442. }
  8443. if (mdd_detected) {
  8444. reg = rd32(hw, I40E_PF_MDET_TX);
  8445. if (reg & I40E_PF_MDET_TX_VALID_MASK) {
  8446. wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
  8447. dev_info(&pf->pdev->dev, "TX driver issue detected, PF reset issued\n");
  8448. pf_mdd_detected = true;
  8449. }
  8450. reg = rd32(hw, I40E_PF_MDET_RX);
  8451. if (reg & I40E_PF_MDET_RX_VALID_MASK) {
  8452. wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
  8453. dev_info(&pf->pdev->dev, "RX driver issue detected, PF reset issued\n");
  8454. pf_mdd_detected = true;
  8455. }
  8456. /* Queue belongs to the PF, initiate a reset */
  8457. if (pf_mdd_detected) {
  8458. set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
  8459. i40e_service_event_schedule(pf);
  8460. }
  8461. }
  8462. /* see if one of the VFs needs its hand slapped */
  8463. for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
  8464. vf = &(pf->vf[i]);
  8465. reg = rd32(hw, I40E_VP_MDET_TX(i));
  8466. if (reg & I40E_VP_MDET_TX_VALID_MASK) {
  8467. wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
  8468. vf->num_mdd_events++;
  8469. dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
  8470. i);
  8471. }
  8472. reg = rd32(hw, I40E_VP_MDET_RX(i));
  8473. if (reg & I40E_VP_MDET_RX_VALID_MASK) {
  8474. wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
  8475. vf->num_mdd_events++;
  8476. dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
  8477. i);
  8478. }
  8479. if (vf->num_mdd_events > I40E_DEFAULT_NUM_MDD_EVENTS_ALLOWED) {
  8480. dev_info(&pf->pdev->dev,
  8481. "Too many MDD events on VF %d, disabled\n", i);
  8482. dev_info(&pf->pdev->dev,
  8483. "Use PF Control I/F to re-enable the VF\n");
  8484. set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
  8485. }
  8486. }
  8487. /* re-enable mdd interrupt cause */
  8488. clear_bit(__I40E_MDD_EVENT_PENDING, pf->state);
  8489. reg = rd32(hw, I40E_PFINT_ICR0_ENA);
  8490. reg |= I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
  8491. wr32(hw, I40E_PFINT_ICR0_ENA, reg);
  8492. i40e_flush(hw);
  8493. }
  8494. static const char *i40e_tunnel_name(struct i40e_udp_port_config *port)
  8495. {
  8496. switch (port->type) {
  8497. case UDP_TUNNEL_TYPE_VXLAN:
  8498. return "vxlan";
  8499. case UDP_TUNNEL_TYPE_GENEVE:
  8500. return "geneve";
  8501. default:
  8502. return "unknown";
  8503. }
  8504. }
  8505. /**
  8506. * i40e_sync_udp_filters - Trigger a sync event for existing UDP filters
  8507. * @pf: board private structure
  8508. **/
  8509. static void i40e_sync_udp_filters(struct i40e_pf *pf)
  8510. {
  8511. int i;
  8512. /* loop through and set pending bit for all active UDP filters */
  8513. for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
  8514. if (pf->udp_ports[i].port)
  8515. pf->pending_udp_bitmap |= BIT_ULL(i);
  8516. }
  8517. set_bit(__I40E_UDP_FILTER_SYNC_PENDING, pf->state);
  8518. }
  8519. /**
  8520. * i40e_sync_udp_filters_subtask - Sync the VSI filter list with HW
  8521. * @pf: board private structure
  8522. **/
  8523. static void i40e_sync_udp_filters_subtask(struct i40e_pf *pf)
  8524. {
  8525. struct i40e_hw *hw = &pf->hw;
  8526. i40e_status ret;
  8527. u16 port;
  8528. int i;
  8529. if (!test_and_clear_bit(__I40E_UDP_FILTER_SYNC_PENDING, pf->state))
  8530. return;
  8531. for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
  8532. if (pf->pending_udp_bitmap & BIT_ULL(i)) {
  8533. pf->pending_udp_bitmap &= ~BIT_ULL(i);
  8534. port = pf->udp_ports[i].port;
  8535. if (port)
  8536. ret = i40e_aq_add_udp_tunnel(hw, port,
  8537. pf->udp_ports[i].type,
  8538. NULL, NULL);
  8539. else
  8540. ret = i40e_aq_del_udp_tunnel(hw, i, NULL);
  8541. if (ret) {
  8542. dev_info(&pf->pdev->dev,
  8543. "%s %s port %d, index %d failed, err %s aq_err %s\n",
  8544. i40e_tunnel_name(&pf->udp_ports[i]),
  8545. port ? "add" : "delete",
  8546. port, i,
  8547. i40e_stat_str(&pf->hw, ret),
  8548. i40e_aq_str(&pf->hw,
  8549. pf->hw.aq.asq_last_status));
  8550. pf->udp_ports[i].port = 0;
  8551. }
  8552. }
  8553. }
  8554. }
  8555. /**
  8556. * i40e_service_task - Run the driver's async subtasks
  8557. * @work: pointer to work_struct containing our data
  8558. **/
  8559. static void i40e_service_task(struct work_struct *work)
  8560. {
  8561. struct i40e_pf *pf = container_of(work,
  8562. struct i40e_pf,
  8563. service_task);
  8564. unsigned long start_time = jiffies;
  8565. /* don't bother with service tasks if a reset is in progress */
  8566. if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
  8567. return;
  8568. if (test_and_set_bit(__I40E_SERVICE_SCHED, pf->state))
  8569. return;
  8570. i40e_detect_recover_hung(pf->vsi[pf->lan_vsi]);
  8571. i40e_sync_filters_subtask(pf);
  8572. i40e_reset_subtask(pf);
  8573. i40e_handle_mdd_event(pf);
  8574. i40e_vc_process_vflr_event(pf);
  8575. i40e_watchdog_subtask(pf);
  8576. i40e_fdir_reinit_subtask(pf);
  8577. if (test_and_clear_bit(__I40E_CLIENT_RESET, pf->state)) {
  8578. /* Client subtask will reopen next time through. */
  8579. i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], true);
  8580. } else {
  8581. i40e_client_subtask(pf);
  8582. if (test_and_clear_bit(__I40E_CLIENT_L2_CHANGE,
  8583. pf->state))
  8584. i40e_notify_client_of_l2_param_changes(
  8585. pf->vsi[pf->lan_vsi]);
  8586. }
  8587. i40e_sync_filters_subtask(pf);
  8588. i40e_sync_udp_filters_subtask(pf);
  8589. i40e_clean_adminq_subtask(pf);
  8590. /* flush memory to make sure state is correct before next watchdog */
  8591. smp_mb__before_atomic();
  8592. clear_bit(__I40E_SERVICE_SCHED, pf->state);
  8593. /* If the tasks have taken longer than one timer cycle or there
  8594. * is more work to be done, reschedule the service task now
  8595. * rather than wait for the timer to tick again.
  8596. */
  8597. if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
  8598. test_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state) ||
  8599. test_bit(__I40E_MDD_EVENT_PENDING, pf->state) ||
  8600. test_bit(__I40E_VFLR_EVENT_PENDING, pf->state))
  8601. i40e_service_event_schedule(pf);
  8602. }
  8603. /**
  8604. * i40e_service_timer - timer callback
  8605. * @data: pointer to PF struct
  8606. **/
  8607. static void i40e_service_timer(struct timer_list *t)
  8608. {
  8609. struct i40e_pf *pf = from_timer(pf, t, service_timer);
  8610. mod_timer(&pf->service_timer,
  8611. round_jiffies(jiffies + pf->service_timer_period));
  8612. i40e_service_event_schedule(pf);
  8613. }
  8614. /**
  8615. * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
  8616. * @vsi: the VSI being configured
  8617. **/
  8618. static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
  8619. {
  8620. struct i40e_pf *pf = vsi->back;
  8621. switch (vsi->type) {
  8622. case I40E_VSI_MAIN:
  8623. vsi->alloc_queue_pairs = pf->num_lan_qps;
  8624. vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
  8625. I40E_REQ_DESCRIPTOR_MULTIPLE);
  8626. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  8627. vsi->num_q_vectors = pf->num_lan_msix;
  8628. else
  8629. vsi->num_q_vectors = 1;
  8630. break;
  8631. case I40E_VSI_FDIR:
  8632. vsi->alloc_queue_pairs = 1;
  8633. vsi->num_desc = ALIGN(I40E_FDIR_RING_COUNT,
  8634. I40E_REQ_DESCRIPTOR_MULTIPLE);
  8635. vsi->num_q_vectors = pf->num_fdsb_msix;
  8636. break;
  8637. case I40E_VSI_VMDQ2:
  8638. vsi->alloc_queue_pairs = pf->num_vmdq_qps;
  8639. vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
  8640. I40E_REQ_DESCRIPTOR_MULTIPLE);
  8641. vsi->num_q_vectors = pf->num_vmdq_msix;
  8642. break;
  8643. case I40E_VSI_SRIOV:
  8644. vsi->alloc_queue_pairs = pf->num_vf_qps;
  8645. vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
  8646. I40E_REQ_DESCRIPTOR_MULTIPLE);
  8647. break;
  8648. default:
  8649. WARN_ON(1);
  8650. return -ENODATA;
  8651. }
  8652. return 0;
  8653. }
  8654. /**
  8655. * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
  8656. * @vsi: VSI pointer
  8657. * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
  8658. *
  8659. * On error: returns error code (negative)
  8660. * On success: returns 0
  8661. **/
  8662. static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
  8663. {
  8664. struct i40e_ring **next_rings;
  8665. int size;
  8666. int ret = 0;
  8667. /* allocate memory for both Tx, XDP Tx and Rx ring pointers */
  8668. size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs *
  8669. (i40e_enabled_xdp_vsi(vsi) ? 3 : 2);
  8670. vsi->tx_rings = kzalloc(size, GFP_KERNEL);
  8671. if (!vsi->tx_rings)
  8672. return -ENOMEM;
  8673. next_rings = vsi->tx_rings + vsi->alloc_queue_pairs;
  8674. if (i40e_enabled_xdp_vsi(vsi)) {
  8675. vsi->xdp_rings = next_rings;
  8676. next_rings += vsi->alloc_queue_pairs;
  8677. }
  8678. vsi->rx_rings = next_rings;
  8679. if (alloc_qvectors) {
  8680. /* allocate memory for q_vector pointers */
  8681. size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
  8682. vsi->q_vectors = kzalloc(size, GFP_KERNEL);
  8683. if (!vsi->q_vectors) {
  8684. ret = -ENOMEM;
  8685. goto err_vectors;
  8686. }
  8687. }
  8688. return ret;
  8689. err_vectors:
  8690. kfree(vsi->tx_rings);
  8691. return ret;
  8692. }
  8693. /**
  8694. * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
  8695. * @pf: board private structure
  8696. * @type: type of VSI
  8697. *
  8698. * On error: returns error code (negative)
  8699. * On success: returns vsi index in PF (positive)
  8700. **/
  8701. static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
  8702. {
  8703. int ret = -ENODEV;
  8704. struct i40e_vsi *vsi;
  8705. int vsi_idx;
  8706. int i;
  8707. /* Need to protect the allocation of the VSIs at the PF level */
  8708. mutex_lock(&pf->switch_mutex);
  8709. /* VSI list may be fragmented if VSI creation/destruction has
  8710. * been happening. We can afford to do a quick scan to look
  8711. * for any free VSIs in the list.
  8712. *
  8713. * find next empty vsi slot, looping back around if necessary
  8714. */
  8715. i = pf->next_vsi;
  8716. while (i < pf->num_alloc_vsi && pf->vsi[i])
  8717. i++;
  8718. if (i >= pf->num_alloc_vsi) {
  8719. i = 0;
  8720. while (i < pf->next_vsi && pf->vsi[i])
  8721. i++;
  8722. }
  8723. if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
  8724. vsi_idx = i; /* Found one! */
  8725. } else {
  8726. ret = -ENODEV;
  8727. goto unlock_pf; /* out of VSI slots! */
  8728. }
  8729. pf->next_vsi = ++i;
  8730. vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
  8731. if (!vsi) {
  8732. ret = -ENOMEM;
  8733. goto unlock_pf;
  8734. }
  8735. vsi->type = type;
  8736. vsi->back = pf;
  8737. set_bit(__I40E_VSI_DOWN, vsi->state);
  8738. vsi->flags = 0;
  8739. vsi->idx = vsi_idx;
  8740. vsi->int_rate_limit = 0;
  8741. vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
  8742. pf->rss_table_size : 64;
  8743. vsi->netdev_registered = false;
  8744. vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
  8745. hash_init(vsi->mac_filter_hash);
  8746. vsi->irqs_ready = false;
  8747. ret = i40e_set_num_rings_in_vsi(vsi);
  8748. if (ret)
  8749. goto err_rings;
  8750. ret = i40e_vsi_alloc_arrays(vsi, true);
  8751. if (ret)
  8752. goto err_rings;
  8753. /* Setup default MSIX irq handler for VSI */
  8754. i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
  8755. /* Initialize VSI lock */
  8756. spin_lock_init(&vsi->mac_filter_hash_lock);
  8757. pf->vsi[vsi_idx] = vsi;
  8758. ret = vsi_idx;
  8759. goto unlock_pf;
  8760. err_rings:
  8761. pf->next_vsi = i - 1;
  8762. kfree(vsi);
  8763. unlock_pf:
  8764. mutex_unlock(&pf->switch_mutex);
  8765. return ret;
  8766. }
  8767. /**
  8768. * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
  8769. * @type: VSI pointer
  8770. * @free_qvectors: a bool to specify if q_vectors need to be freed.
  8771. *
  8772. * On error: returns error code (negative)
  8773. * On success: returns 0
  8774. **/
  8775. static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
  8776. {
  8777. /* free the ring and vector containers */
  8778. if (free_qvectors) {
  8779. kfree(vsi->q_vectors);
  8780. vsi->q_vectors = NULL;
  8781. }
  8782. kfree(vsi->tx_rings);
  8783. vsi->tx_rings = NULL;
  8784. vsi->rx_rings = NULL;
  8785. vsi->xdp_rings = NULL;
  8786. }
  8787. /**
  8788. * i40e_clear_rss_config_user - clear the user configured RSS hash keys
  8789. * and lookup table
  8790. * @vsi: Pointer to VSI structure
  8791. */
  8792. static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
  8793. {
  8794. if (!vsi)
  8795. return;
  8796. kfree(vsi->rss_hkey_user);
  8797. vsi->rss_hkey_user = NULL;
  8798. kfree(vsi->rss_lut_user);
  8799. vsi->rss_lut_user = NULL;
  8800. }
  8801. /**
  8802. * i40e_vsi_clear - Deallocate the VSI provided
  8803. * @vsi: the VSI being un-configured
  8804. **/
  8805. static int i40e_vsi_clear(struct i40e_vsi *vsi)
  8806. {
  8807. struct i40e_pf *pf;
  8808. if (!vsi)
  8809. return 0;
  8810. if (!vsi->back)
  8811. goto free_vsi;
  8812. pf = vsi->back;
  8813. mutex_lock(&pf->switch_mutex);
  8814. if (!pf->vsi[vsi->idx]) {
  8815. dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](type %d)\n",
  8816. vsi->idx, vsi->idx, vsi->type);
  8817. goto unlock_vsi;
  8818. }
  8819. if (pf->vsi[vsi->idx] != vsi) {
  8820. dev_err(&pf->pdev->dev,
  8821. "pf->vsi[%d](type %d) != vsi[%d](type %d): no free!\n",
  8822. pf->vsi[vsi->idx]->idx,
  8823. pf->vsi[vsi->idx]->type,
  8824. vsi->idx, vsi->type);
  8825. goto unlock_vsi;
  8826. }
  8827. /* updates the PF for this cleared vsi */
  8828. i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
  8829. i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
  8830. i40e_vsi_free_arrays(vsi, true);
  8831. i40e_clear_rss_config_user(vsi);
  8832. pf->vsi[vsi->idx] = NULL;
  8833. if (vsi->idx < pf->next_vsi)
  8834. pf->next_vsi = vsi->idx;
  8835. unlock_vsi:
  8836. mutex_unlock(&pf->switch_mutex);
  8837. free_vsi:
  8838. kfree(vsi);
  8839. return 0;
  8840. }
  8841. /**
  8842. * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
  8843. * @vsi: the VSI being cleaned
  8844. **/
  8845. static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
  8846. {
  8847. int i;
  8848. if (vsi->tx_rings && vsi->tx_rings[0]) {
  8849. for (i = 0; i < vsi->alloc_queue_pairs; i++) {
  8850. kfree_rcu(vsi->tx_rings[i], rcu);
  8851. vsi->tx_rings[i] = NULL;
  8852. vsi->rx_rings[i] = NULL;
  8853. if (vsi->xdp_rings)
  8854. vsi->xdp_rings[i] = NULL;
  8855. }
  8856. }
  8857. }
  8858. /**
  8859. * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
  8860. * @vsi: the VSI being configured
  8861. **/
  8862. static int i40e_alloc_rings(struct i40e_vsi *vsi)
  8863. {
  8864. int i, qpv = i40e_enabled_xdp_vsi(vsi) ? 3 : 2;
  8865. struct i40e_pf *pf = vsi->back;
  8866. struct i40e_ring *ring;
  8867. /* Set basic values in the rings to be used later during open() */
  8868. for (i = 0; i < vsi->alloc_queue_pairs; i++) {
  8869. /* allocate space for both Tx and Rx in one shot */
  8870. ring = kcalloc(qpv, sizeof(struct i40e_ring), GFP_KERNEL);
  8871. if (!ring)
  8872. goto err_out;
  8873. ring->queue_index = i;
  8874. ring->reg_idx = vsi->base_queue + i;
  8875. ring->ring_active = false;
  8876. ring->vsi = vsi;
  8877. ring->netdev = vsi->netdev;
  8878. ring->dev = &pf->pdev->dev;
  8879. ring->count = vsi->num_desc;
  8880. ring->size = 0;
  8881. ring->dcb_tc = 0;
  8882. if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
  8883. ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
  8884. ring->itr_setting = pf->tx_itr_default;
  8885. vsi->tx_rings[i] = ring++;
  8886. if (!i40e_enabled_xdp_vsi(vsi))
  8887. goto setup_rx;
  8888. ring->queue_index = vsi->alloc_queue_pairs + i;
  8889. ring->reg_idx = vsi->base_queue + ring->queue_index;
  8890. ring->ring_active = false;
  8891. ring->vsi = vsi;
  8892. ring->netdev = NULL;
  8893. ring->dev = &pf->pdev->dev;
  8894. ring->count = vsi->num_desc;
  8895. ring->size = 0;
  8896. ring->dcb_tc = 0;
  8897. if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
  8898. ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
  8899. set_ring_xdp(ring);
  8900. ring->itr_setting = pf->tx_itr_default;
  8901. vsi->xdp_rings[i] = ring++;
  8902. setup_rx:
  8903. ring->queue_index = i;
  8904. ring->reg_idx = vsi->base_queue + i;
  8905. ring->ring_active = false;
  8906. ring->vsi = vsi;
  8907. ring->netdev = vsi->netdev;
  8908. ring->dev = &pf->pdev->dev;
  8909. ring->count = vsi->num_desc;
  8910. ring->size = 0;
  8911. ring->dcb_tc = 0;
  8912. ring->itr_setting = pf->rx_itr_default;
  8913. vsi->rx_rings[i] = ring;
  8914. }
  8915. return 0;
  8916. err_out:
  8917. i40e_vsi_clear_rings(vsi);
  8918. return -ENOMEM;
  8919. }
  8920. /**
  8921. * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
  8922. * @pf: board private structure
  8923. * @vectors: the number of MSI-X vectors to request
  8924. *
  8925. * Returns the number of vectors reserved, or error
  8926. **/
  8927. static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
  8928. {
  8929. vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
  8930. I40E_MIN_MSIX, vectors);
  8931. if (vectors < 0) {
  8932. dev_info(&pf->pdev->dev,
  8933. "MSI-X vector reservation failed: %d\n", vectors);
  8934. vectors = 0;
  8935. }
  8936. return vectors;
  8937. }
  8938. /**
  8939. * i40e_init_msix - Setup the MSIX capability
  8940. * @pf: board private structure
  8941. *
  8942. * Work with the OS to set up the MSIX vectors needed.
  8943. *
  8944. * Returns the number of vectors reserved or negative on failure
  8945. **/
  8946. static int i40e_init_msix(struct i40e_pf *pf)
  8947. {
  8948. struct i40e_hw *hw = &pf->hw;
  8949. int cpus, extra_vectors;
  8950. int vectors_left;
  8951. int v_budget, i;
  8952. int v_actual;
  8953. int iwarp_requested = 0;
  8954. if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
  8955. return -ENODEV;
  8956. /* The number of vectors we'll request will be comprised of:
  8957. * - Add 1 for "other" cause for Admin Queue events, etc.
  8958. * - The number of LAN queue pairs
  8959. * - Queues being used for RSS.
  8960. * We don't need as many as max_rss_size vectors.
  8961. * use rss_size instead in the calculation since that
  8962. * is governed by number of cpus in the system.
  8963. * - assumes symmetric Tx/Rx pairing
  8964. * - The number of VMDq pairs
  8965. * - The CPU count within the NUMA node if iWARP is enabled
  8966. * Once we count this up, try the request.
  8967. *
  8968. * If we can't get what we want, we'll simplify to nearly nothing
  8969. * and try again. If that still fails, we punt.
  8970. */
  8971. vectors_left = hw->func_caps.num_msix_vectors;
  8972. v_budget = 0;
  8973. /* reserve one vector for miscellaneous handler */
  8974. if (vectors_left) {
  8975. v_budget++;
  8976. vectors_left--;
  8977. }
  8978. /* reserve some vectors for the main PF traffic queues. Initially we
  8979. * only reserve at most 50% of the available vectors, in the case that
  8980. * the number of online CPUs is large. This ensures that we can enable
  8981. * extra features as well. Once we've enabled the other features, we
  8982. * will use any remaining vectors to reach as close as we can to the
  8983. * number of online CPUs.
  8984. */
  8985. cpus = num_online_cpus();
  8986. pf->num_lan_msix = min_t(int, cpus, vectors_left / 2);
  8987. vectors_left -= pf->num_lan_msix;
  8988. /* reserve one vector for sideband flow director */
  8989. if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
  8990. if (vectors_left) {
  8991. pf->num_fdsb_msix = 1;
  8992. v_budget++;
  8993. vectors_left--;
  8994. } else {
  8995. pf->num_fdsb_msix = 0;
  8996. }
  8997. }
  8998. /* can we reserve enough for iWARP? */
  8999. if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
  9000. iwarp_requested = pf->num_iwarp_msix;
  9001. if (!vectors_left)
  9002. pf->num_iwarp_msix = 0;
  9003. else if (vectors_left < pf->num_iwarp_msix)
  9004. pf->num_iwarp_msix = 1;
  9005. v_budget += pf->num_iwarp_msix;
  9006. vectors_left -= pf->num_iwarp_msix;
  9007. }
  9008. /* any vectors left over go for VMDq support */
  9009. if (pf->flags & I40E_FLAG_VMDQ_ENABLED) {
  9010. int vmdq_vecs_wanted = pf->num_vmdq_vsis * pf->num_vmdq_qps;
  9011. int vmdq_vecs = min_t(int, vectors_left, vmdq_vecs_wanted);
  9012. if (!vectors_left) {
  9013. pf->num_vmdq_msix = 0;
  9014. pf->num_vmdq_qps = 0;
  9015. } else {
  9016. /* if we're short on vectors for what's desired, we limit
  9017. * the queues per vmdq. If this is still more than are
  9018. * available, the user will need to change the number of
  9019. * queues/vectors used by the PF later with the ethtool
  9020. * channels command
  9021. */
  9022. if (vmdq_vecs < vmdq_vecs_wanted)
  9023. pf->num_vmdq_qps = 1;
  9024. pf->num_vmdq_msix = pf->num_vmdq_qps;
  9025. v_budget += vmdq_vecs;
  9026. vectors_left -= vmdq_vecs;
  9027. }
  9028. }
  9029. /* On systems with a large number of SMP cores, we previously limited
  9030. * the number of vectors for num_lan_msix to be at most 50% of the
  9031. * available vectors, to allow for other features. Now, we add back
  9032. * the remaining vectors. However, we ensure that the total
  9033. * num_lan_msix will not exceed num_online_cpus(). To do this, we
  9034. * calculate the number of vectors we can add without going over the
  9035. * cap of CPUs. For systems with a small number of CPUs this will be
  9036. * zero.
  9037. */
  9038. extra_vectors = min_t(int, cpus - pf->num_lan_msix, vectors_left);
  9039. pf->num_lan_msix += extra_vectors;
  9040. vectors_left -= extra_vectors;
  9041. WARN(vectors_left < 0,
  9042. "Calculation of remaining vectors underflowed. This is an accounting bug when determining total MSI-X vectors.\n");
  9043. v_budget += pf->num_lan_msix;
  9044. pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
  9045. GFP_KERNEL);
  9046. if (!pf->msix_entries)
  9047. return -ENOMEM;
  9048. for (i = 0; i < v_budget; i++)
  9049. pf->msix_entries[i].entry = i;
  9050. v_actual = i40e_reserve_msix_vectors(pf, v_budget);
  9051. if (v_actual < I40E_MIN_MSIX) {
  9052. pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
  9053. kfree(pf->msix_entries);
  9054. pf->msix_entries = NULL;
  9055. pci_disable_msix(pf->pdev);
  9056. return -ENODEV;
  9057. } else if (v_actual == I40E_MIN_MSIX) {
  9058. /* Adjust for minimal MSIX use */
  9059. pf->num_vmdq_vsis = 0;
  9060. pf->num_vmdq_qps = 0;
  9061. pf->num_lan_qps = 1;
  9062. pf->num_lan_msix = 1;
  9063. } else if (v_actual != v_budget) {
  9064. /* If we have limited resources, we will start with no vectors
  9065. * for the special features and then allocate vectors to some
  9066. * of these features based on the policy and at the end disable
  9067. * the features that did not get any vectors.
  9068. */
  9069. int vec;
  9070. dev_info(&pf->pdev->dev,
  9071. "MSI-X vector limit reached with %d, wanted %d, attempting to redistribute vectors\n",
  9072. v_actual, v_budget);
  9073. /* reserve the misc vector */
  9074. vec = v_actual - 1;
  9075. /* Scale vector usage down */
  9076. pf->num_vmdq_msix = 1; /* force VMDqs to only one vector */
  9077. pf->num_vmdq_vsis = 1;
  9078. pf->num_vmdq_qps = 1;
  9079. /* partition out the remaining vectors */
  9080. switch (vec) {
  9081. case 2:
  9082. pf->num_lan_msix = 1;
  9083. break;
  9084. case 3:
  9085. if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
  9086. pf->num_lan_msix = 1;
  9087. pf->num_iwarp_msix = 1;
  9088. } else {
  9089. pf->num_lan_msix = 2;
  9090. }
  9091. break;
  9092. default:
  9093. if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
  9094. pf->num_iwarp_msix = min_t(int, (vec / 3),
  9095. iwarp_requested);
  9096. pf->num_vmdq_vsis = min_t(int, (vec / 3),
  9097. I40E_DEFAULT_NUM_VMDQ_VSI);
  9098. } else {
  9099. pf->num_vmdq_vsis = min_t(int, (vec / 2),
  9100. I40E_DEFAULT_NUM_VMDQ_VSI);
  9101. }
  9102. if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
  9103. pf->num_fdsb_msix = 1;
  9104. vec--;
  9105. }
  9106. pf->num_lan_msix = min_t(int,
  9107. (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
  9108. pf->num_lan_msix);
  9109. pf->num_lan_qps = pf->num_lan_msix;
  9110. break;
  9111. }
  9112. }
  9113. if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
  9114. (pf->num_fdsb_msix == 0)) {
  9115. dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n");
  9116. pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
  9117. pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
  9118. }
  9119. if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
  9120. (pf->num_vmdq_msix == 0)) {
  9121. dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
  9122. pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
  9123. }
  9124. if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
  9125. (pf->num_iwarp_msix == 0)) {
  9126. dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
  9127. pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
  9128. }
  9129. i40e_debug(&pf->hw, I40E_DEBUG_INIT,
  9130. "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n",
  9131. pf->num_lan_msix,
  9132. pf->num_vmdq_msix * pf->num_vmdq_vsis,
  9133. pf->num_fdsb_msix,
  9134. pf->num_iwarp_msix);
  9135. return v_actual;
  9136. }
  9137. /**
  9138. * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
  9139. * @vsi: the VSI being configured
  9140. * @v_idx: index of the vector in the vsi struct
  9141. * @cpu: cpu to be used on affinity_mask
  9142. *
  9143. * We allocate one q_vector. If allocation fails we return -ENOMEM.
  9144. **/
  9145. static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx, int cpu)
  9146. {
  9147. struct i40e_q_vector *q_vector;
  9148. /* allocate q_vector */
  9149. q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
  9150. if (!q_vector)
  9151. return -ENOMEM;
  9152. q_vector->vsi = vsi;
  9153. q_vector->v_idx = v_idx;
  9154. cpumask_copy(&q_vector->affinity_mask, cpu_possible_mask);
  9155. if (vsi->netdev)
  9156. netif_napi_add(vsi->netdev, &q_vector->napi,
  9157. i40e_napi_poll, NAPI_POLL_WEIGHT);
  9158. /* tie q_vector and vsi together */
  9159. vsi->q_vectors[v_idx] = q_vector;
  9160. return 0;
  9161. }
  9162. /**
  9163. * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
  9164. * @vsi: the VSI being configured
  9165. *
  9166. * We allocate one q_vector per queue interrupt. If allocation fails we
  9167. * return -ENOMEM.
  9168. **/
  9169. static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
  9170. {
  9171. struct i40e_pf *pf = vsi->back;
  9172. int err, v_idx, num_q_vectors, current_cpu;
  9173. /* if not MSIX, give the one vector only to the LAN VSI */
  9174. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  9175. num_q_vectors = vsi->num_q_vectors;
  9176. else if (vsi == pf->vsi[pf->lan_vsi])
  9177. num_q_vectors = 1;
  9178. else
  9179. return -EINVAL;
  9180. current_cpu = cpumask_first(cpu_online_mask);
  9181. for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
  9182. err = i40e_vsi_alloc_q_vector(vsi, v_idx, current_cpu);
  9183. if (err)
  9184. goto err_out;
  9185. current_cpu = cpumask_next(current_cpu, cpu_online_mask);
  9186. if (unlikely(current_cpu >= nr_cpu_ids))
  9187. current_cpu = cpumask_first(cpu_online_mask);
  9188. }
  9189. return 0;
  9190. err_out:
  9191. while (v_idx--)
  9192. i40e_free_q_vector(vsi, v_idx);
  9193. return err;
  9194. }
  9195. /**
  9196. * i40e_init_interrupt_scheme - Determine proper interrupt scheme
  9197. * @pf: board private structure to initialize
  9198. **/
  9199. static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
  9200. {
  9201. int vectors = 0;
  9202. ssize_t size;
  9203. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  9204. vectors = i40e_init_msix(pf);
  9205. if (vectors < 0) {
  9206. pf->flags &= ~(I40E_FLAG_MSIX_ENABLED |
  9207. I40E_FLAG_IWARP_ENABLED |
  9208. I40E_FLAG_RSS_ENABLED |
  9209. I40E_FLAG_DCB_CAPABLE |
  9210. I40E_FLAG_DCB_ENABLED |
  9211. I40E_FLAG_SRIOV_ENABLED |
  9212. I40E_FLAG_FD_SB_ENABLED |
  9213. I40E_FLAG_FD_ATR_ENABLED |
  9214. I40E_FLAG_VMDQ_ENABLED);
  9215. pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
  9216. /* rework the queue expectations without MSIX */
  9217. i40e_determine_queue_usage(pf);
  9218. }
  9219. }
  9220. if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
  9221. (pf->flags & I40E_FLAG_MSI_ENABLED)) {
  9222. dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
  9223. vectors = pci_enable_msi(pf->pdev);
  9224. if (vectors < 0) {
  9225. dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
  9226. vectors);
  9227. pf->flags &= ~I40E_FLAG_MSI_ENABLED;
  9228. }
  9229. vectors = 1; /* one MSI or Legacy vector */
  9230. }
  9231. if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
  9232. dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
  9233. /* set up vector assignment tracking */
  9234. size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
  9235. pf->irq_pile = kzalloc(size, GFP_KERNEL);
  9236. if (!pf->irq_pile)
  9237. return -ENOMEM;
  9238. pf->irq_pile->num_entries = vectors;
  9239. pf->irq_pile->search_hint = 0;
  9240. /* track first vector for misc interrupts, ignore return */
  9241. (void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
  9242. return 0;
  9243. }
  9244. /**
  9245. * i40e_restore_interrupt_scheme - Restore the interrupt scheme
  9246. * @pf: private board data structure
  9247. *
  9248. * Restore the interrupt scheme that was cleared when we suspended the
  9249. * device. This should be called during resume to re-allocate the q_vectors
  9250. * and reacquire IRQs.
  9251. */
  9252. static int i40e_restore_interrupt_scheme(struct i40e_pf *pf)
  9253. {
  9254. int err, i;
  9255. /* We cleared the MSI and MSI-X flags when disabling the old interrupt
  9256. * scheme. We need to re-enabled them here in order to attempt to
  9257. * re-acquire the MSI or MSI-X vectors
  9258. */
  9259. pf->flags |= (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
  9260. err = i40e_init_interrupt_scheme(pf);
  9261. if (err)
  9262. return err;
  9263. /* Now that we've re-acquired IRQs, we need to remap the vectors and
  9264. * rings together again.
  9265. */
  9266. for (i = 0; i < pf->num_alloc_vsi; i++) {
  9267. if (pf->vsi[i]) {
  9268. err = i40e_vsi_alloc_q_vectors(pf->vsi[i]);
  9269. if (err)
  9270. goto err_unwind;
  9271. i40e_vsi_map_rings_to_vectors(pf->vsi[i]);
  9272. }
  9273. }
  9274. err = i40e_setup_misc_vector(pf);
  9275. if (err)
  9276. goto err_unwind;
  9277. if (pf->flags & I40E_FLAG_IWARP_ENABLED)
  9278. i40e_client_update_msix_info(pf);
  9279. return 0;
  9280. err_unwind:
  9281. while (i--) {
  9282. if (pf->vsi[i])
  9283. i40e_vsi_free_q_vectors(pf->vsi[i]);
  9284. }
  9285. return err;
  9286. }
  9287. /**
  9288. * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
  9289. * @pf: board private structure
  9290. *
  9291. * This sets up the handler for MSIX 0, which is used to manage the
  9292. * non-queue interrupts, e.g. AdminQ and errors. This is not used
  9293. * when in MSI or Legacy interrupt mode.
  9294. **/
  9295. static int i40e_setup_misc_vector(struct i40e_pf *pf)
  9296. {
  9297. struct i40e_hw *hw = &pf->hw;
  9298. int err = 0;
  9299. /* Only request the IRQ once, the first time through. */
  9300. if (!test_and_set_bit(__I40E_MISC_IRQ_REQUESTED, pf->state)) {
  9301. err = request_irq(pf->msix_entries[0].vector,
  9302. i40e_intr, 0, pf->int_name, pf);
  9303. if (err) {
  9304. clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
  9305. dev_info(&pf->pdev->dev,
  9306. "request_irq for %s failed: %d\n",
  9307. pf->int_name, err);
  9308. return -EFAULT;
  9309. }
  9310. }
  9311. i40e_enable_misc_int_causes(pf);
  9312. /* associate no queues to the misc vector */
  9313. wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
  9314. wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K);
  9315. i40e_flush(hw);
  9316. i40e_irq_dynamic_enable_icr0(pf);
  9317. return err;
  9318. }
  9319. /**
  9320. * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
  9321. * @vsi: Pointer to vsi structure
  9322. * @seed: Buffter to store the hash keys
  9323. * @lut: Buffer to store the lookup table entries
  9324. * @lut_size: Size of buffer to store the lookup table entries
  9325. *
  9326. * Return 0 on success, negative on failure
  9327. */
  9328. static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
  9329. u8 *lut, u16 lut_size)
  9330. {
  9331. struct i40e_pf *pf = vsi->back;
  9332. struct i40e_hw *hw = &pf->hw;
  9333. int ret = 0;
  9334. if (seed) {
  9335. ret = i40e_aq_get_rss_key(hw, vsi->id,
  9336. (struct i40e_aqc_get_set_rss_key_data *)seed);
  9337. if (ret) {
  9338. dev_info(&pf->pdev->dev,
  9339. "Cannot get RSS key, err %s aq_err %s\n",
  9340. i40e_stat_str(&pf->hw, ret),
  9341. i40e_aq_str(&pf->hw,
  9342. pf->hw.aq.asq_last_status));
  9343. return ret;
  9344. }
  9345. }
  9346. if (lut) {
  9347. bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
  9348. ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
  9349. if (ret) {
  9350. dev_info(&pf->pdev->dev,
  9351. "Cannot get RSS lut, err %s aq_err %s\n",
  9352. i40e_stat_str(&pf->hw, ret),
  9353. i40e_aq_str(&pf->hw,
  9354. pf->hw.aq.asq_last_status));
  9355. return ret;
  9356. }
  9357. }
  9358. return ret;
  9359. }
  9360. /**
  9361. * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
  9362. * @vsi: Pointer to vsi structure
  9363. * @seed: RSS hash seed
  9364. * @lut: Lookup table
  9365. * @lut_size: Lookup table size
  9366. *
  9367. * Returns 0 on success, negative on failure
  9368. **/
  9369. static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
  9370. const u8 *lut, u16 lut_size)
  9371. {
  9372. struct i40e_pf *pf = vsi->back;
  9373. struct i40e_hw *hw = &pf->hw;
  9374. u16 vf_id = vsi->vf_id;
  9375. u8 i;
  9376. /* Fill out hash function seed */
  9377. if (seed) {
  9378. u32 *seed_dw = (u32 *)seed;
  9379. if (vsi->type == I40E_VSI_MAIN) {
  9380. for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
  9381. wr32(hw, I40E_PFQF_HKEY(i), seed_dw[i]);
  9382. } else if (vsi->type == I40E_VSI_SRIOV) {
  9383. for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
  9384. wr32(hw, I40E_VFQF_HKEY1(i, vf_id), seed_dw[i]);
  9385. } else {
  9386. dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
  9387. }
  9388. }
  9389. if (lut) {
  9390. u32 *lut_dw = (u32 *)lut;
  9391. if (vsi->type == I40E_VSI_MAIN) {
  9392. if (lut_size != I40E_HLUT_ARRAY_SIZE)
  9393. return -EINVAL;
  9394. for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
  9395. wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
  9396. } else if (vsi->type == I40E_VSI_SRIOV) {
  9397. if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
  9398. return -EINVAL;
  9399. for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
  9400. wr32(hw, I40E_VFQF_HLUT1(i, vf_id), lut_dw[i]);
  9401. } else {
  9402. dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
  9403. }
  9404. }
  9405. i40e_flush(hw);
  9406. return 0;
  9407. }
  9408. /**
  9409. * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
  9410. * @vsi: Pointer to VSI structure
  9411. * @seed: Buffer to store the keys
  9412. * @lut: Buffer to store the lookup table entries
  9413. * @lut_size: Size of buffer to store the lookup table entries
  9414. *
  9415. * Returns 0 on success, negative on failure
  9416. */
  9417. static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
  9418. u8 *lut, u16 lut_size)
  9419. {
  9420. struct i40e_pf *pf = vsi->back;
  9421. struct i40e_hw *hw = &pf->hw;
  9422. u16 i;
  9423. if (seed) {
  9424. u32 *seed_dw = (u32 *)seed;
  9425. for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
  9426. seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
  9427. }
  9428. if (lut) {
  9429. u32 *lut_dw = (u32 *)lut;
  9430. if (lut_size != I40E_HLUT_ARRAY_SIZE)
  9431. return -EINVAL;
  9432. for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
  9433. lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
  9434. }
  9435. return 0;
  9436. }
  9437. /**
  9438. * i40e_config_rss - Configure RSS keys and lut
  9439. * @vsi: Pointer to VSI structure
  9440. * @seed: RSS hash seed
  9441. * @lut: Lookup table
  9442. * @lut_size: Lookup table size
  9443. *
  9444. * Returns 0 on success, negative on failure
  9445. */
  9446. int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
  9447. {
  9448. struct i40e_pf *pf = vsi->back;
  9449. if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
  9450. return i40e_config_rss_aq(vsi, seed, lut, lut_size);
  9451. else
  9452. return i40e_config_rss_reg(vsi, seed, lut, lut_size);
  9453. }
  9454. /**
  9455. * i40e_get_rss - Get RSS keys and lut
  9456. * @vsi: Pointer to VSI structure
  9457. * @seed: Buffer to store the keys
  9458. * @lut: Buffer to store the lookup table entries
  9459. * lut_size: Size of buffer to store the lookup table entries
  9460. *
  9461. * Returns 0 on success, negative on failure
  9462. */
  9463. int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
  9464. {
  9465. struct i40e_pf *pf = vsi->back;
  9466. if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
  9467. return i40e_get_rss_aq(vsi, seed, lut, lut_size);
  9468. else
  9469. return i40e_get_rss_reg(vsi, seed, lut, lut_size);
  9470. }
  9471. /**
  9472. * i40e_fill_rss_lut - Fill the RSS lookup table with default values
  9473. * @pf: Pointer to board private structure
  9474. * @lut: Lookup table
  9475. * @rss_table_size: Lookup table size
  9476. * @rss_size: Range of queue number for hashing
  9477. */
  9478. void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
  9479. u16 rss_table_size, u16 rss_size)
  9480. {
  9481. u16 i;
  9482. for (i = 0; i < rss_table_size; i++)
  9483. lut[i] = i % rss_size;
  9484. }
  9485. /**
  9486. * i40e_pf_config_rss - Prepare for RSS if used
  9487. * @pf: board private structure
  9488. **/
  9489. static int i40e_pf_config_rss(struct i40e_pf *pf)
  9490. {
  9491. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  9492. u8 seed[I40E_HKEY_ARRAY_SIZE];
  9493. u8 *lut;
  9494. struct i40e_hw *hw = &pf->hw;
  9495. u32 reg_val;
  9496. u64 hena;
  9497. int ret;
  9498. /* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
  9499. hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
  9500. ((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
  9501. hena |= i40e_pf_get_default_rss_hena(pf);
  9502. i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
  9503. i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
  9504. /* Determine the RSS table size based on the hardware capabilities */
  9505. reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
  9506. reg_val = (pf->rss_table_size == 512) ?
  9507. (reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
  9508. (reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
  9509. i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
  9510. /* Determine the RSS size of the VSI */
  9511. if (!vsi->rss_size) {
  9512. u16 qcount;
  9513. /* If the firmware does something weird during VSI init, we
  9514. * could end up with zero TCs. Check for that to avoid
  9515. * divide-by-zero. It probably won't pass traffic, but it also
  9516. * won't panic.
  9517. */
  9518. qcount = vsi->num_queue_pairs /
  9519. (vsi->tc_config.numtc ? vsi->tc_config.numtc : 1);
  9520. vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
  9521. }
  9522. if (!vsi->rss_size)
  9523. return -EINVAL;
  9524. lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
  9525. if (!lut)
  9526. return -ENOMEM;
  9527. /* Use user configured lut if there is one, otherwise use default */
  9528. if (vsi->rss_lut_user)
  9529. memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
  9530. else
  9531. i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
  9532. /* Use user configured hash key if there is one, otherwise
  9533. * use default.
  9534. */
  9535. if (vsi->rss_hkey_user)
  9536. memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
  9537. else
  9538. netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
  9539. ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
  9540. kfree(lut);
  9541. return ret;
  9542. }
  9543. /**
  9544. * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
  9545. * @pf: board private structure
  9546. * @queue_count: the requested queue count for rss.
  9547. *
  9548. * returns 0 if rss is not enabled, if enabled returns the final rss queue
  9549. * count which may be different from the requested queue count.
  9550. * Note: expects to be called while under rtnl_lock()
  9551. **/
  9552. int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
  9553. {
  9554. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  9555. int new_rss_size;
  9556. if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
  9557. return 0;
  9558. new_rss_size = min_t(int, queue_count, pf->rss_size_max);
  9559. if (queue_count != vsi->num_queue_pairs) {
  9560. u16 qcount;
  9561. vsi->req_queue_pairs = queue_count;
  9562. i40e_prep_for_reset(pf, true);
  9563. pf->alloc_rss_size = new_rss_size;
  9564. i40e_reset_and_rebuild(pf, true, true);
  9565. /* Discard the user configured hash keys and lut, if less
  9566. * queues are enabled.
  9567. */
  9568. if (queue_count < vsi->rss_size) {
  9569. i40e_clear_rss_config_user(vsi);
  9570. dev_dbg(&pf->pdev->dev,
  9571. "discard user configured hash keys and lut\n");
  9572. }
  9573. /* Reset vsi->rss_size, as number of enabled queues changed */
  9574. qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
  9575. vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
  9576. i40e_pf_config_rss(pf);
  9577. }
  9578. dev_info(&pf->pdev->dev, "User requested queue count/HW max RSS count: %d/%d\n",
  9579. vsi->req_queue_pairs, pf->rss_size_max);
  9580. return pf->alloc_rss_size;
  9581. }
  9582. /**
  9583. * i40e_get_partition_bw_setting - Retrieve BW settings for this PF partition
  9584. * @pf: board private structure
  9585. **/
  9586. i40e_status i40e_get_partition_bw_setting(struct i40e_pf *pf)
  9587. {
  9588. i40e_status status;
  9589. bool min_valid, max_valid;
  9590. u32 max_bw, min_bw;
  9591. status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
  9592. &min_valid, &max_valid);
  9593. if (!status) {
  9594. if (min_valid)
  9595. pf->min_bw = min_bw;
  9596. if (max_valid)
  9597. pf->max_bw = max_bw;
  9598. }
  9599. return status;
  9600. }
  9601. /**
  9602. * i40e_set_partition_bw_setting - Set BW settings for this PF partition
  9603. * @pf: board private structure
  9604. **/
  9605. i40e_status i40e_set_partition_bw_setting(struct i40e_pf *pf)
  9606. {
  9607. struct i40e_aqc_configure_partition_bw_data bw_data;
  9608. i40e_status status;
  9609. /* Set the valid bit for this PF */
  9610. bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
  9611. bw_data.max_bw[pf->hw.pf_id] = pf->max_bw & I40E_ALT_BW_VALUE_MASK;
  9612. bw_data.min_bw[pf->hw.pf_id] = pf->min_bw & I40E_ALT_BW_VALUE_MASK;
  9613. /* Set the new bandwidths */
  9614. status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
  9615. return status;
  9616. }
  9617. /**
  9618. * i40e_commit_partition_bw_setting - Commit BW settings for this PF partition
  9619. * @pf: board private structure
  9620. **/
  9621. i40e_status i40e_commit_partition_bw_setting(struct i40e_pf *pf)
  9622. {
  9623. /* Commit temporary BW setting to permanent NVM image */
  9624. enum i40e_admin_queue_err last_aq_status;
  9625. i40e_status ret;
  9626. u16 nvm_word;
  9627. if (pf->hw.partition_id != 1) {
  9628. dev_info(&pf->pdev->dev,
  9629. "Commit BW only works on partition 1! This is partition %d",
  9630. pf->hw.partition_id);
  9631. ret = I40E_NOT_SUPPORTED;
  9632. goto bw_commit_out;
  9633. }
  9634. /* Acquire NVM for read access */
  9635. ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
  9636. last_aq_status = pf->hw.aq.asq_last_status;
  9637. if (ret) {
  9638. dev_info(&pf->pdev->dev,
  9639. "Cannot acquire NVM for read access, err %s aq_err %s\n",
  9640. i40e_stat_str(&pf->hw, ret),
  9641. i40e_aq_str(&pf->hw, last_aq_status));
  9642. goto bw_commit_out;
  9643. }
  9644. /* Read word 0x10 of NVM - SW compatibility word 1 */
  9645. ret = i40e_aq_read_nvm(&pf->hw,
  9646. I40E_SR_NVM_CONTROL_WORD,
  9647. 0x10, sizeof(nvm_word), &nvm_word,
  9648. false, NULL);
  9649. /* Save off last admin queue command status before releasing
  9650. * the NVM
  9651. */
  9652. last_aq_status = pf->hw.aq.asq_last_status;
  9653. i40e_release_nvm(&pf->hw);
  9654. if (ret) {
  9655. dev_info(&pf->pdev->dev, "NVM read error, err %s aq_err %s\n",
  9656. i40e_stat_str(&pf->hw, ret),
  9657. i40e_aq_str(&pf->hw, last_aq_status));
  9658. goto bw_commit_out;
  9659. }
  9660. /* Wait a bit for NVM release to complete */
  9661. msleep(50);
  9662. /* Acquire NVM for write access */
  9663. ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
  9664. last_aq_status = pf->hw.aq.asq_last_status;
  9665. if (ret) {
  9666. dev_info(&pf->pdev->dev,
  9667. "Cannot acquire NVM for write access, err %s aq_err %s\n",
  9668. i40e_stat_str(&pf->hw, ret),
  9669. i40e_aq_str(&pf->hw, last_aq_status));
  9670. goto bw_commit_out;
  9671. }
  9672. /* Write it back out unchanged to initiate update NVM,
  9673. * which will force a write of the shadow (alt) RAM to
  9674. * the NVM - thus storing the bandwidth values permanently.
  9675. */
  9676. ret = i40e_aq_update_nvm(&pf->hw,
  9677. I40E_SR_NVM_CONTROL_WORD,
  9678. 0x10, sizeof(nvm_word),
  9679. &nvm_word, true, 0, NULL);
  9680. /* Save off last admin queue command status before releasing
  9681. * the NVM
  9682. */
  9683. last_aq_status = pf->hw.aq.asq_last_status;
  9684. i40e_release_nvm(&pf->hw);
  9685. if (ret)
  9686. dev_info(&pf->pdev->dev,
  9687. "BW settings NOT SAVED, err %s aq_err %s\n",
  9688. i40e_stat_str(&pf->hw, ret),
  9689. i40e_aq_str(&pf->hw, last_aq_status));
  9690. bw_commit_out:
  9691. return ret;
  9692. }
  9693. /**
  9694. * i40e_sw_init - Initialize general software structures (struct i40e_pf)
  9695. * @pf: board private structure to initialize
  9696. *
  9697. * i40e_sw_init initializes the Adapter private data structure.
  9698. * Fields are initialized based on PCI device information and
  9699. * OS network device settings (MTU size).
  9700. **/
  9701. static int i40e_sw_init(struct i40e_pf *pf)
  9702. {
  9703. int err = 0;
  9704. int size;
  9705. /* Set default capability flags */
  9706. pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
  9707. I40E_FLAG_MSI_ENABLED |
  9708. I40E_FLAG_MSIX_ENABLED;
  9709. /* Set default ITR */
  9710. pf->rx_itr_default = I40E_ITR_RX_DEF;
  9711. pf->tx_itr_default = I40E_ITR_TX_DEF;
  9712. /* Depending on PF configurations, it is possible that the RSS
  9713. * maximum might end up larger than the available queues
  9714. */
  9715. pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
  9716. pf->alloc_rss_size = 1;
  9717. pf->rss_table_size = pf->hw.func_caps.rss_table_size;
  9718. pf->rss_size_max = min_t(int, pf->rss_size_max,
  9719. pf->hw.func_caps.num_tx_qp);
  9720. if (pf->hw.func_caps.rss) {
  9721. pf->flags |= I40E_FLAG_RSS_ENABLED;
  9722. pf->alloc_rss_size = min_t(int, pf->rss_size_max,
  9723. num_online_cpus());
  9724. }
  9725. /* MFP mode enabled */
  9726. if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
  9727. pf->flags |= I40E_FLAG_MFP_ENABLED;
  9728. dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
  9729. if (i40e_get_partition_bw_setting(pf)) {
  9730. dev_warn(&pf->pdev->dev,
  9731. "Could not get partition bw settings\n");
  9732. } else {
  9733. dev_info(&pf->pdev->dev,
  9734. "Partition BW Min = %8.8x, Max = %8.8x\n",
  9735. pf->min_bw, pf->max_bw);
  9736. /* nudge the Tx scheduler */
  9737. i40e_set_partition_bw_setting(pf);
  9738. }
  9739. }
  9740. if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
  9741. (pf->hw.func_caps.fd_filters_best_effort > 0)) {
  9742. pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
  9743. pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
  9744. if (pf->flags & I40E_FLAG_MFP_ENABLED &&
  9745. pf->hw.num_partitions > 1)
  9746. dev_info(&pf->pdev->dev,
  9747. "Flow Director Sideband mode Disabled in MFP mode\n");
  9748. else
  9749. pf->flags |= I40E_FLAG_FD_SB_ENABLED;
  9750. pf->fdir_pf_filter_count =
  9751. pf->hw.func_caps.fd_filters_guaranteed;
  9752. pf->hw.fdir_shared_filter_count =
  9753. pf->hw.func_caps.fd_filters_best_effort;
  9754. }
  9755. if (pf->hw.mac.type == I40E_MAC_X722) {
  9756. pf->hw_features |= (I40E_HW_RSS_AQ_CAPABLE |
  9757. I40E_HW_128_QP_RSS_CAPABLE |
  9758. I40E_HW_ATR_EVICT_CAPABLE |
  9759. I40E_HW_WB_ON_ITR_CAPABLE |
  9760. I40E_HW_MULTIPLE_TCP_UDP_RSS_PCTYPE |
  9761. I40E_HW_NO_PCI_LINK_CHECK |
  9762. I40E_HW_USE_SET_LLDP_MIB |
  9763. I40E_HW_GENEVE_OFFLOAD_CAPABLE |
  9764. I40E_HW_PTP_L4_CAPABLE |
  9765. I40E_HW_WOL_MC_MAGIC_PKT_WAKE |
  9766. I40E_HW_OUTER_UDP_CSUM_CAPABLE);
  9767. #define I40E_FDEVICT_PCTYPE_DEFAULT 0xc03
  9768. if (rd32(&pf->hw, I40E_GLQF_FDEVICTENA(1)) !=
  9769. I40E_FDEVICT_PCTYPE_DEFAULT) {
  9770. dev_warn(&pf->pdev->dev,
  9771. "FD EVICT PCTYPES are not right, disable FD HW EVICT\n");
  9772. pf->hw_features &= ~I40E_HW_ATR_EVICT_CAPABLE;
  9773. }
  9774. } else if ((pf->hw.aq.api_maj_ver > 1) ||
  9775. ((pf->hw.aq.api_maj_ver == 1) &&
  9776. (pf->hw.aq.api_min_ver > 4))) {
  9777. /* Supported in FW API version higher than 1.4 */
  9778. pf->hw_features |= I40E_HW_GENEVE_OFFLOAD_CAPABLE;
  9779. }
  9780. /* Enable HW ATR eviction if possible */
  9781. if (pf->hw_features & I40E_HW_ATR_EVICT_CAPABLE)
  9782. pf->flags |= I40E_FLAG_HW_ATR_EVICT_ENABLED;
  9783. if ((pf->hw.mac.type == I40E_MAC_XL710) &&
  9784. (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
  9785. (pf->hw.aq.fw_maj_ver < 4))) {
  9786. pf->hw_features |= I40E_HW_RESTART_AUTONEG;
  9787. /* No DCB support for FW < v4.33 */
  9788. pf->hw_features |= I40E_HW_NO_DCB_SUPPORT;
  9789. }
  9790. /* Disable FW LLDP if FW < v4.3 */
  9791. if ((pf->hw.mac.type == I40E_MAC_XL710) &&
  9792. (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
  9793. (pf->hw.aq.fw_maj_ver < 4)))
  9794. pf->hw_features |= I40E_HW_STOP_FW_LLDP;
  9795. /* Use the FW Set LLDP MIB API if FW > v4.40 */
  9796. if ((pf->hw.mac.type == I40E_MAC_XL710) &&
  9797. (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver >= 40)) ||
  9798. (pf->hw.aq.fw_maj_ver >= 5)))
  9799. pf->hw_features |= I40E_HW_USE_SET_LLDP_MIB;
  9800. /* Enable PTP L4 if FW > v6.0 */
  9801. if (pf->hw.mac.type == I40E_MAC_XL710 &&
  9802. pf->hw.aq.fw_maj_ver >= 6)
  9803. pf->hw_features |= I40E_HW_PTP_L4_CAPABLE;
  9804. if (pf->hw.func_caps.vmdq && num_online_cpus() != 1) {
  9805. pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
  9806. pf->flags |= I40E_FLAG_VMDQ_ENABLED;
  9807. pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
  9808. }
  9809. if (pf->hw.func_caps.iwarp && num_online_cpus() != 1) {
  9810. pf->flags |= I40E_FLAG_IWARP_ENABLED;
  9811. /* IWARP needs one extra vector for CQP just like MISC.*/
  9812. pf->num_iwarp_msix = (int)num_online_cpus() + 1;
  9813. }
  9814. /* Stopping the FW LLDP engine is only supported on the
  9815. * XL710 with a FW ver >= 1.7. Also, stopping FW LLDP
  9816. * engine is not supported if NPAR is functioning on this
  9817. * part
  9818. */
  9819. if (pf->hw.mac.type == I40E_MAC_XL710 &&
  9820. !pf->hw.func_caps.npar_enable &&
  9821. (pf->hw.aq.api_maj_ver > 1 ||
  9822. (pf->hw.aq.api_maj_ver == 1 && pf->hw.aq.api_min_ver > 6)))
  9823. pf->hw_features |= I40E_HW_STOPPABLE_FW_LLDP;
  9824. #ifdef CONFIG_PCI_IOV
  9825. if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
  9826. pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
  9827. pf->flags |= I40E_FLAG_SRIOV_ENABLED;
  9828. pf->num_req_vfs = min_t(int,
  9829. pf->hw.func_caps.num_vfs,
  9830. I40E_MAX_VF_COUNT);
  9831. }
  9832. #endif /* CONFIG_PCI_IOV */
  9833. pf->eeprom_version = 0xDEAD;
  9834. pf->lan_veb = I40E_NO_VEB;
  9835. pf->lan_vsi = I40E_NO_VSI;
  9836. /* By default FW has this off for performance reasons */
  9837. pf->flags &= ~I40E_FLAG_VEB_STATS_ENABLED;
  9838. /* set up queue assignment tracking */
  9839. size = sizeof(struct i40e_lump_tracking)
  9840. + (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
  9841. pf->qp_pile = kzalloc(size, GFP_KERNEL);
  9842. if (!pf->qp_pile) {
  9843. err = -ENOMEM;
  9844. goto sw_init_done;
  9845. }
  9846. pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
  9847. pf->qp_pile->search_hint = 0;
  9848. pf->tx_timeout_recovery_level = 1;
  9849. mutex_init(&pf->switch_mutex);
  9850. sw_init_done:
  9851. return err;
  9852. }
  9853. /**
  9854. * i40e_set_ntuple - set the ntuple feature flag and take action
  9855. * @pf: board private structure to initialize
  9856. * @features: the feature set that the stack is suggesting
  9857. *
  9858. * returns a bool to indicate if reset needs to happen
  9859. **/
  9860. bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
  9861. {
  9862. bool need_reset = false;
  9863. /* Check if Flow Director n-tuple support was enabled or disabled. If
  9864. * the state changed, we need to reset.
  9865. */
  9866. if (features & NETIF_F_NTUPLE) {
  9867. /* Enable filters and mark for reset */
  9868. if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
  9869. need_reset = true;
  9870. /* enable FD_SB only if there is MSI-X vector and no cloud
  9871. * filters exist
  9872. */
  9873. if (pf->num_fdsb_msix > 0 && !pf->num_cloud_filters) {
  9874. pf->flags |= I40E_FLAG_FD_SB_ENABLED;
  9875. pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
  9876. }
  9877. } else {
  9878. /* turn off filters, mark for reset and clear SW filter list */
  9879. if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
  9880. need_reset = true;
  9881. i40e_fdir_filter_exit(pf);
  9882. }
  9883. pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
  9884. clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state);
  9885. pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
  9886. /* reset fd counters */
  9887. pf->fd_add_err = 0;
  9888. pf->fd_atr_cnt = 0;
  9889. /* if ATR was auto disabled it can be re-enabled. */
  9890. if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
  9891. if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
  9892. (I40E_DEBUG_FD & pf->hw.debug_mask))
  9893. dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
  9894. }
  9895. return need_reset;
  9896. }
  9897. /**
  9898. * i40e_clear_rss_lut - clear the rx hash lookup table
  9899. * @vsi: the VSI being configured
  9900. **/
  9901. static void i40e_clear_rss_lut(struct i40e_vsi *vsi)
  9902. {
  9903. struct i40e_pf *pf = vsi->back;
  9904. struct i40e_hw *hw = &pf->hw;
  9905. u16 vf_id = vsi->vf_id;
  9906. u8 i;
  9907. if (vsi->type == I40E_VSI_MAIN) {
  9908. for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
  9909. wr32(hw, I40E_PFQF_HLUT(i), 0);
  9910. } else if (vsi->type == I40E_VSI_SRIOV) {
  9911. for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
  9912. i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0);
  9913. } else {
  9914. dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
  9915. }
  9916. }
  9917. /**
  9918. * i40e_set_features - set the netdev feature flags
  9919. * @netdev: ptr to the netdev being adjusted
  9920. * @features: the feature set that the stack is suggesting
  9921. * Note: expects to be called while under rtnl_lock()
  9922. **/
  9923. static int i40e_set_features(struct net_device *netdev,
  9924. netdev_features_t features)
  9925. {
  9926. struct i40e_netdev_priv *np = netdev_priv(netdev);
  9927. struct i40e_vsi *vsi = np->vsi;
  9928. struct i40e_pf *pf = vsi->back;
  9929. bool need_reset;
  9930. if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
  9931. i40e_pf_config_rss(pf);
  9932. else if (!(features & NETIF_F_RXHASH) &&
  9933. netdev->features & NETIF_F_RXHASH)
  9934. i40e_clear_rss_lut(vsi);
  9935. if (features & NETIF_F_HW_VLAN_CTAG_RX)
  9936. i40e_vlan_stripping_enable(vsi);
  9937. else
  9938. i40e_vlan_stripping_disable(vsi);
  9939. if (!(features & NETIF_F_HW_TC) && pf->num_cloud_filters) {
  9940. dev_err(&pf->pdev->dev,
  9941. "Offloaded tc filters active, can't turn hw_tc_offload off");
  9942. return -EINVAL;
  9943. }
  9944. need_reset = i40e_set_ntuple(pf, features);
  9945. if (need_reset)
  9946. i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
  9947. return 0;
  9948. }
  9949. /**
  9950. * i40e_get_udp_port_idx - Lookup a possibly offloaded for Rx UDP port
  9951. * @pf: board private structure
  9952. * @port: The UDP port to look up
  9953. *
  9954. * Returns the index number or I40E_MAX_PF_UDP_OFFLOAD_PORTS if port not found
  9955. **/
  9956. static u8 i40e_get_udp_port_idx(struct i40e_pf *pf, u16 port)
  9957. {
  9958. u8 i;
  9959. for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
  9960. if (pf->udp_ports[i].port == port)
  9961. return i;
  9962. }
  9963. return i;
  9964. }
  9965. /**
  9966. * i40e_udp_tunnel_add - Get notifications about UDP tunnel ports that come up
  9967. * @netdev: This physical port's netdev
  9968. * @ti: Tunnel endpoint information
  9969. **/
  9970. static void i40e_udp_tunnel_add(struct net_device *netdev,
  9971. struct udp_tunnel_info *ti)
  9972. {
  9973. struct i40e_netdev_priv *np = netdev_priv(netdev);
  9974. struct i40e_vsi *vsi = np->vsi;
  9975. struct i40e_pf *pf = vsi->back;
  9976. u16 port = ntohs(ti->port);
  9977. u8 next_idx;
  9978. u8 idx;
  9979. idx = i40e_get_udp_port_idx(pf, port);
  9980. /* Check if port already exists */
  9981. if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
  9982. netdev_info(netdev, "port %d already offloaded\n", port);
  9983. return;
  9984. }
  9985. /* Now check if there is space to add the new port */
  9986. next_idx = i40e_get_udp_port_idx(pf, 0);
  9987. if (next_idx == I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
  9988. netdev_info(netdev, "maximum number of offloaded UDP ports reached, not adding port %d\n",
  9989. port);
  9990. return;
  9991. }
  9992. switch (ti->type) {
  9993. case UDP_TUNNEL_TYPE_VXLAN:
  9994. pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_VXLAN;
  9995. break;
  9996. case UDP_TUNNEL_TYPE_GENEVE:
  9997. if (!(pf->hw_features & I40E_HW_GENEVE_OFFLOAD_CAPABLE))
  9998. return;
  9999. pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_NGE;
  10000. break;
  10001. default:
  10002. return;
  10003. }
  10004. /* New port: add it and mark its index in the bitmap */
  10005. pf->udp_ports[next_idx].port = port;
  10006. pf->pending_udp_bitmap |= BIT_ULL(next_idx);
  10007. set_bit(__I40E_UDP_FILTER_SYNC_PENDING, pf->state);
  10008. }
  10009. /**
  10010. * i40e_udp_tunnel_del - Get notifications about UDP tunnel ports that go away
  10011. * @netdev: This physical port's netdev
  10012. * @ti: Tunnel endpoint information
  10013. **/
  10014. static void i40e_udp_tunnel_del(struct net_device *netdev,
  10015. struct udp_tunnel_info *ti)
  10016. {
  10017. struct i40e_netdev_priv *np = netdev_priv(netdev);
  10018. struct i40e_vsi *vsi = np->vsi;
  10019. struct i40e_pf *pf = vsi->back;
  10020. u16 port = ntohs(ti->port);
  10021. u8 idx;
  10022. idx = i40e_get_udp_port_idx(pf, port);
  10023. /* Check if port already exists */
  10024. if (idx >= I40E_MAX_PF_UDP_OFFLOAD_PORTS)
  10025. goto not_found;
  10026. switch (ti->type) {
  10027. case UDP_TUNNEL_TYPE_VXLAN:
  10028. if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_VXLAN)
  10029. goto not_found;
  10030. break;
  10031. case UDP_TUNNEL_TYPE_GENEVE:
  10032. if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_NGE)
  10033. goto not_found;
  10034. break;
  10035. default:
  10036. goto not_found;
  10037. }
  10038. /* if port exists, set it to 0 (mark for deletion)
  10039. * and make it pending
  10040. */
  10041. pf->udp_ports[idx].port = 0;
  10042. pf->pending_udp_bitmap |= BIT_ULL(idx);
  10043. set_bit(__I40E_UDP_FILTER_SYNC_PENDING, pf->state);
  10044. return;
  10045. not_found:
  10046. netdev_warn(netdev, "UDP port %d was not found, not deleting\n",
  10047. port);
  10048. }
  10049. static int i40e_get_phys_port_id(struct net_device *netdev,
  10050. struct netdev_phys_item_id *ppid)
  10051. {
  10052. struct i40e_netdev_priv *np = netdev_priv(netdev);
  10053. struct i40e_pf *pf = np->vsi->back;
  10054. struct i40e_hw *hw = &pf->hw;
  10055. if (!(pf->hw_features & I40E_HW_PORT_ID_VALID))
  10056. return -EOPNOTSUPP;
  10057. ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
  10058. memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
  10059. return 0;
  10060. }
  10061. /**
  10062. * i40e_ndo_fdb_add - add an entry to the hardware database
  10063. * @ndm: the input from the stack
  10064. * @tb: pointer to array of nladdr (unused)
  10065. * @dev: the net device pointer
  10066. * @addr: the MAC address entry being added
  10067. * @flags: instructions from stack about fdb operation
  10068. */
  10069. static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
  10070. struct net_device *dev,
  10071. const unsigned char *addr, u16 vid,
  10072. u16 flags)
  10073. {
  10074. struct i40e_netdev_priv *np = netdev_priv(dev);
  10075. struct i40e_pf *pf = np->vsi->back;
  10076. int err = 0;
  10077. if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
  10078. return -EOPNOTSUPP;
  10079. if (vid) {
  10080. pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
  10081. return -EINVAL;
  10082. }
  10083. /* Hardware does not support aging addresses so if a
  10084. * ndm_state is given only allow permanent addresses
  10085. */
  10086. if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
  10087. netdev_info(dev, "FDB only supports static addresses\n");
  10088. return -EINVAL;
  10089. }
  10090. if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
  10091. err = dev_uc_add_excl(dev, addr);
  10092. else if (is_multicast_ether_addr(addr))
  10093. err = dev_mc_add_excl(dev, addr);
  10094. else
  10095. err = -EINVAL;
  10096. /* Only return duplicate errors if NLM_F_EXCL is set */
  10097. if (err == -EEXIST && !(flags & NLM_F_EXCL))
  10098. err = 0;
  10099. return err;
  10100. }
  10101. /**
  10102. * i40e_ndo_bridge_setlink - Set the hardware bridge mode
  10103. * @dev: the netdev being configured
  10104. * @nlh: RTNL message
  10105. *
  10106. * Inserts a new hardware bridge if not already created and
  10107. * enables the bridging mode requested (VEB or VEPA). If the
  10108. * hardware bridge has already been inserted and the request
  10109. * is to change the mode then that requires a PF reset to
  10110. * allow rebuild of the components with required hardware
  10111. * bridge mode enabled.
  10112. *
  10113. * Note: expects to be called while under rtnl_lock()
  10114. **/
  10115. static int i40e_ndo_bridge_setlink(struct net_device *dev,
  10116. struct nlmsghdr *nlh,
  10117. u16 flags)
  10118. {
  10119. struct i40e_netdev_priv *np = netdev_priv(dev);
  10120. struct i40e_vsi *vsi = np->vsi;
  10121. struct i40e_pf *pf = vsi->back;
  10122. struct i40e_veb *veb = NULL;
  10123. struct nlattr *attr, *br_spec;
  10124. int i, rem;
  10125. /* Only for PF VSI for now */
  10126. if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
  10127. return -EOPNOTSUPP;
  10128. /* Find the HW bridge for PF VSI */
  10129. for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
  10130. if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
  10131. veb = pf->veb[i];
  10132. }
  10133. br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
  10134. nla_for_each_nested(attr, br_spec, rem) {
  10135. __u16 mode;
  10136. if (nla_type(attr) != IFLA_BRIDGE_MODE)
  10137. continue;
  10138. mode = nla_get_u16(attr);
  10139. if ((mode != BRIDGE_MODE_VEPA) &&
  10140. (mode != BRIDGE_MODE_VEB))
  10141. return -EINVAL;
  10142. /* Insert a new HW bridge */
  10143. if (!veb) {
  10144. veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
  10145. vsi->tc_config.enabled_tc);
  10146. if (veb) {
  10147. veb->bridge_mode = mode;
  10148. i40e_config_bridge_mode(veb);
  10149. } else {
  10150. /* No Bridge HW offload available */
  10151. return -ENOENT;
  10152. }
  10153. break;
  10154. } else if (mode != veb->bridge_mode) {
  10155. /* Existing HW bridge but different mode needs reset */
  10156. veb->bridge_mode = mode;
  10157. /* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
  10158. if (mode == BRIDGE_MODE_VEB)
  10159. pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
  10160. else
  10161. pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
  10162. i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
  10163. break;
  10164. }
  10165. }
  10166. return 0;
  10167. }
  10168. /**
  10169. * i40e_ndo_bridge_getlink - Get the hardware bridge mode
  10170. * @skb: skb buff
  10171. * @pid: process id
  10172. * @seq: RTNL message seq #
  10173. * @dev: the netdev being configured
  10174. * @filter_mask: unused
  10175. * @nlflags: netlink flags passed in
  10176. *
  10177. * Return the mode in which the hardware bridge is operating in
  10178. * i.e VEB or VEPA.
  10179. **/
  10180. static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
  10181. struct net_device *dev,
  10182. u32 __always_unused filter_mask,
  10183. int nlflags)
  10184. {
  10185. struct i40e_netdev_priv *np = netdev_priv(dev);
  10186. struct i40e_vsi *vsi = np->vsi;
  10187. struct i40e_pf *pf = vsi->back;
  10188. struct i40e_veb *veb = NULL;
  10189. int i;
  10190. /* Only for PF VSI for now */
  10191. if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
  10192. return -EOPNOTSUPP;
  10193. /* Find the HW bridge for the PF VSI */
  10194. for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
  10195. if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
  10196. veb = pf->veb[i];
  10197. }
  10198. if (!veb)
  10199. return 0;
  10200. return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
  10201. 0, 0, nlflags, filter_mask, NULL);
  10202. }
  10203. /**
  10204. * i40e_features_check - Validate encapsulated packet conforms to limits
  10205. * @skb: skb buff
  10206. * @dev: This physical port's netdev
  10207. * @features: Offload features that the stack believes apply
  10208. **/
  10209. static netdev_features_t i40e_features_check(struct sk_buff *skb,
  10210. struct net_device *dev,
  10211. netdev_features_t features)
  10212. {
  10213. size_t len;
  10214. /* No point in doing any of this if neither checksum nor GSO are
  10215. * being requested for this frame. We can rule out both by just
  10216. * checking for CHECKSUM_PARTIAL
  10217. */
  10218. if (skb->ip_summed != CHECKSUM_PARTIAL)
  10219. return features;
  10220. /* We cannot support GSO if the MSS is going to be less than
  10221. * 64 bytes. If it is then we need to drop support for GSO.
  10222. */
  10223. if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
  10224. features &= ~NETIF_F_GSO_MASK;
  10225. /* MACLEN can support at most 63 words */
  10226. len = skb_network_header(skb) - skb->data;
  10227. if (len & ~(63 * 2))
  10228. goto out_err;
  10229. /* IPLEN and EIPLEN can support at most 127 dwords */
  10230. len = skb_transport_header(skb) - skb_network_header(skb);
  10231. if (len & ~(127 * 4))
  10232. goto out_err;
  10233. if (skb->encapsulation) {
  10234. /* L4TUNLEN can support 127 words */
  10235. len = skb_inner_network_header(skb) - skb_transport_header(skb);
  10236. if (len & ~(127 * 2))
  10237. goto out_err;
  10238. /* IPLEN can support at most 127 dwords */
  10239. len = skb_inner_transport_header(skb) -
  10240. skb_inner_network_header(skb);
  10241. if (len & ~(127 * 4))
  10242. goto out_err;
  10243. }
  10244. /* No need to validate L4LEN as TCP is the only protocol with a
  10245. * a flexible value and we support all possible values supported
  10246. * by TCP, which is at most 15 dwords
  10247. */
  10248. return features;
  10249. out_err:
  10250. return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
  10251. }
  10252. /**
  10253. * i40e_xdp_setup - add/remove an XDP program
  10254. * @vsi: VSI to changed
  10255. * @prog: XDP program
  10256. **/
  10257. static int i40e_xdp_setup(struct i40e_vsi *vsi,
  10258. struct bpf_prog *prog)
  10259. {
  10260. int frame_size = vsi->netdev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
  10261. struct i40e_pf *pf = vsi->back;
  10262. struct bpf_prog *old_prog;
  10263. bool need_reset;
  10264. int i;
  10265. /* Don't allow frames that span over multiple buffers */
  10266. if (frame_size > vsi->rx_buf_len)
  10267. return -EINVAL;
  10268. if (!i40e_enabled_xdp_vsi(vsi) && !prog)
  10269. return 0;
  10270. /* When turning XDP on->off/off->on we reset and rebuild the rings. */
  10271. need_reset = (i40e_enabled_xdp_vsi(vsi) != !!prog);
  10272. if (need_reset)
  10273. i40e_prep_for_reset(pf, true);
  10274. old_prog = xchg(&vsi->xdp_prog, prog);
  10275. if (need_reset)
  10276. i40e_reset_and_rebuild(pf, true, true);
  10277. for (i = 0; i < vsi->num_queue_pairs; i++)
  10278. WRITE_ONCE(vsi->rx_rings[i]->xdp_prog, vsi->xdp_prog);
  10279. if (old_prog)
  10280. bpf_prog_put(old_prog);
  10281. return 0;
  10282. }
  10283. /**
  10284. * i40e_xdp - implements ndo_bpf for i40e
  10285. * @dev: netdevice
  10286. * @xdp: XDP command
  10287. **/
  10288. static int i40e_xdp(struct net_device *dev,
  10289. struct netdev_bpf *xdp)
  10290. {
  10291. struct i40e_netdev_priv *np = netdev_priv(dev);
  10292. struct i40e_vsi *vsi = np->vsi;
  10293. if (vsi->type != I40E_VSI_MAIN)
  10294. return -EINVAL;
  10295. switch (xdp->command) {
  10296. case XDP_SETUP_PROG:
  10297. return i40e_xdp_setup(vsi, xdp->prog);
  10298. case XDP_QUERY_PROG:
  10299. xdp->prog_attached = i40e_enabled_xdp_vsi(vsi);
  10300. xdp->prog_id = vsi->xdp_prog ? vsi->xdp_prog->aux->id : 0;
  10301. return 0;
  10302. default:
  10303. return -EINVAL;
  10304. }
  10305. }
  10306. static const struct net_device_ops i40e_netdev_ops = {
  10307. .ndo_open = i40e_open,
  10308. .ndo_stop = i40e_close,
  10309. .ndo_start_xmit = i40e_lan_xmit_frame,
  10310. .ndo_get_stats64 = i40e_get_netdev_stats_struct,
  10311. .ndo_set_rx_mode = i40e_set_rx_mode,
  10312. .ndo_validate_addr = eth_validate_addr,
  10313. .ndo_set_mac_address = i40e_set_mac,
  10314. .ndo_change_mtu = i40e_change_mtu,
  10315. .ndo_do_ioctl = i40e_ioctl,
  10316. .ndo_tx_timeout = i40e_tx_timeout,
  10317. .ndo_vlan_rx_add_vid = i40e_vlan_rx_add_vid,
  10318. .ndo_vlan_rx_kill_vid = i40e_vlan_rx_kill_vid,
  10319. #ifdef CONFIG_NET_POLL_CONTROLLER
  10320. .ndo_poll_controller = i40e_netpoll,
  10321. #endif
  10322. .ndo_setup_tc = __i40e_setup_tc,
  10323. .ndo_set_features = i40e_set_features,
  10324. .ndo_set_vf_mac = i40e_ndo_set_vf_mac,
  10325. .ndo_set_vf_vlan = i40e_ndo_set_vf_port_vlan,
  10326. .ndo_set_vf_rate = i40e_ndo_set_vf_bw,
  10327. .ndo_get_vf_config = i40e_ndo_get_vf_config,
  10328. .ndo_set_vf_link_state = i40e_ndo_set_vf_link_state,
  10329. .ndo_set_vf_spoofchk = i40e_ndo_set_vf_spoofchk,
  10330. .ndo_set_vf_trust = i40e_ndo_set_vf_trust,
  10331. .ndo_udp_tunnel_add = i40e_udp_tunnel_add,
  10332. .ndo_udp_tunnel_del = i40e_udp_tunnel_del,
  10333. .ndo_get_phys_port_id = i40e_get_phys_port_id,
  10334. .ndo_fdb_add = i40e_ndo_fdb_add,
  10335. .ndo_features_check = i40e_features_check,
  10336. .ndo_bridge_getlink = i40e_ndo_bridge_getlink,
  10337. .ndo_bridge_setlink = i40e_ndo_bridge_setlink,
  10338. .ndo_bpf = i40e_xdp,
  10339. .ndo_xdp_xmit = i40e_xdp_xmit,
  10340. .ndo_xdp_flush = i40e_xdp_flush,
  10341. };
  10342. /**
  10343. * i40e_config_netdev - Setup the netdev flags
  10344. * @vsi: the VSI being configured
  10345. *
  10346. * Returns 0 on success, negative value on failure
  10347. **/
  10348. static int i40e_config_netdev(struct i40e_vsi *vsi)
  10349. {
  10350. struct i40e_pf *pf = vsi->back;
  10351. struct i40e_hw *hw = &pf->hw;
  10352. struct i40e_netdev_priv *np;
  10353. struct net_device *netdev;
  10354. u8 broadcast[ETH_ALEN];
  10355. u8 mac_addr[ETH_ALEN];
  10356. int etherdev_size;
  10357. netdev_features_t hw_enc_features;
  10358. netdev_features_t hw_features;
  10359. etherdev_size = sizeof(struct i40e_netdev_priv);
  10360. netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
  10361. if (!netdev)
  10362. return -ENOMEM;
  10363. vsi->netdev = netdev;
  10364. np = netdev_priv(netdev);
  10365. np->vsi = vsi;
  10366. hw_enc_features = NETIF_F_SG |
  10367. NETIF_F_IP_CSUM |
  10368. NETIF_F_IPV6_CSUM |
  10369. NETIF_F_HIGHDMA |
  10370. NETIF_F_SOFT_FEATURES |
  10371. NETIF_F_TSO |
  10372. NETIF_F_TSO_ECN |
  10373. NETIF_F_TSO6 |
  10374. NETIF_F_GSO_GRE |
  10375. NETIF_F_GSO_GRE_CSUM |
  10376. NETIF_F_GSO_PARTIAL |
  10377. NETIF_F_GSO_UDP_TUNNEL |
  10378. NETIF_F_GSO_UDP_TUNNEL_CSUM |
  10379. NETIF_F_SCTP_CRC |
  10380. NETIF_F_RXHASH |
  10381. NETIF_F_RXCSUM |
  10382. 0;
  10383. if (!(pf->hw_features & I40E_HW_OUTER_UDP_CSUM_CAPABLE))
  10384. netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
  10385. netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
  10386. netdev->hw_enc_features |= hw_enc_features;
  10387. /* record features VLANs can make use of */
  10388. netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
  10389. if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
  10390. netdev->hw_features |= NETIF_F_NTUPLE | NETIF_F_HW_TC;
  10391. hw_features = hw_enc_features |
  10392. NETIF_F_HW_VLAN_CTAG_TX |
  10393. NETIF_F_HW_VLAN_CTAG_RX;
  10394. netdev->hw_features |= hw_features;
  10395. netdev->features |= hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
  10396. netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
  10397. if (vsi->type == I40E_VSI_MAIN) {
  10398. SET_NETDEV_DEV(netdev, &pf->pdev->dev);
  10399. ether_addr_copy(mac_addr, hw->mac.perm_addr);
  10400. /* The following steps are necessary for two reasons. First,
  10401. * some older NVM configurations load a default MAC-VLAN
  10402. * filter that will accept any tagged packet, and we want to
  10403. * replace this with a normal filter. Additionally, it is
  10404. * possible our MAC address was provided by the platform using
  10405. * Open Firmware or similar.
  10406. *
  10407. * Thus, we need to remove the default filter and install one
  10408. * specific to the MAC address.
  10409. */
  10410. i40e_rm_default_mac_filter(vsi, mac_addr);
  10411. spin_lock_bh(&vsi->mac_filter_hash_lock);
  10412. i40e_add_mac_filter(vsi, mac_addr);
  10413. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  10414. } else {
  10415. /* Relate the VSI_VMDQ name to the VSI_MAIN name. Note that we
  10416. * are still limited by IFNAMSIZ, but we're adding 'v%d\0' to
  10417. * the end, which is 4 bytes long, so force truncation of the
  10418. * original name by IFNAMSIZ - 4
  10419. */
  10420. snprintf(netdev->name, IFNAMSIZ, "%.*sv%%d",
  10421. IFNAMSIZ - 4,
  10422. pf->vsi[pf->lan_vsi]->netdev->name);
  10423. random_ether_addr(mac_addr);
  10424. spin_lock_bh(&vsi->mac_filter_hash_lock);
  10425. i40e_add_mac_filter(vsi, mac_addr);
  10426. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  10427. }
  10428. /* Add the broadcast filter so that we initially will receive
  10429. * broadcast packets. Note that when a new VLAN is first added the
  10430. * driver will convert all filters marked I40E_VLAN_ANY into VLAN
  10431. * specific filters as part of transitioning into "vlan" operation.
  10432. * When more VLANs are added, the driver will copy each existing MAC
  10433. * filter and add it for the new VLAN.
  10434. *
  10435. * Broadcast filters are handled specially by
  10436. * i40e_sync_filters_subtask, as the driver must to set the broadcast
  10437. * promiscuous bit instead of adding this directly as a MAC/VLAN
  10438. * filter. The subtask will update the correct broadcast promiscuous
  10439. * bits as VLANs become active or inactive.
  10440. */
  10441. eth_broadcast_addr(broadcast);
  10442. spin_lock_bh(&vsi->mac_filter_hash_lock);
  10443. i40e_add_mac_filter(vsi, broadcast);
  10444. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  10445. ether_addr_copy(netdev->dev_addr, mac_addr);
  10446. ether_addr_copy(netdev->perm_addr, mac_addr);
  10447. netdev->priv_flags |= IFF_UNICAST_FLT;
  10448. netdev->priv_flags |= IFF_SUPP_NOFCS;
  10449. /* Setup netdev TC information */
  10450. i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
  10451. netdev->netdev_ops = &i40e_netdev_ops;
  10452. netdev->watchdog_timeo = 5 * HZ;
  10453. i40e_set_ethtool_ops(netdev);
  10454. /* MTU range: 68 - 9706 */
  10455. netdev->min_mtu = ETH_MIN_MTU;
  10456. netdev->max_mtu = I40E_MAX_RXBUFFER - I40E_PACKET_HDR_PAD;
  10457. return 0;
  10458. }
  10459. /**
  10460. * i40e_vsi_delete - Delete a VSI from the switch
  10461. * @vsi: the VSI being removed
  10462. *
  10463. * Returns 0 on success, negative value on failure
  10464. **/
  10465. static void i40e_vsi_delete(struct i40e_vsi *vsi)
  10466. {
  10467. /* remove default VSI is not allowed */
  10468. if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
  10469. return;
  10470. i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
  10471. }
  10472. /**
  10473. * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
  10474. * @vsi: the VSI being queried
  10475. *
  10476. * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
  10477. **/
  10478. int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
  10479. {
  10480. struct i40e_veb *veb;
  10481. struct i40e_pf *pf = vsi->back;
  10482. /* Uplink is not a bridge so default to VEB */
  10483. if (vsi->veb_idx == I40E_NO_VEB)
  10484. return 1;
  10485. veb = pf->veb[vsi->veb_idx];
  10486. if (!veb) {
  10487. dev_info(&pf->pdev->dev,
  10488. "There is no veb associated with the bridge\n");
  10489. return -ENOENT;
  10490. }
  10491. /* Uplink is a bridge in VEPA mode */
  10492. if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
  10493. return 0;
  10494. } else {
  10495. /* Uplink is a bridge in VEB mode */
  10496. return 1;
  10497. }
  10498. /* VEPA is now default bridge, so return 0 */
  10499. return 0;
  10500. }
  10501. /**
  10502. * i40e_add_vsi - Add a VSI to the switch
  10503. * @vsi: the VSI being configured
  10504. *
  10505. * This initializes a VSI context depending on the VSI type to be added and
  10506. * passes it down to the add_vsi aq command.
  10507. **/
  10508. static int i40e_add_vsi(struct i40e_vsi *vsi)
  10509. {
  10510. int ret = -ENODEV;
  10511. struct i40e_pf *pf = vsi->back;
  10512. struct i40e_hw *hw = &pf->hw;
  10513. struct i40e_vsi_context ctxt;
  10514. struct i40e_mac_filter *f;
  10515. struct hlist_node *h;
  10516. int bkt;
  10517. u8 enabled_tc = 0x1; /* TC0 enabled */
  10518. int f_count = 0;
  10519. memset(&ctxt, 0, sizeof(ctxt));
  10520. switch (vsi->type) {
  10521. case I40E_VSI_MAIN:
  10522. /* The PF's main VSI is already setup as part of the
  10523. * device initialization, so we'll not bother with
  10524. * the add_vsi call, but we will retrieve the current
  10525. * VSI context.
  10526. */
  10527. ctxt.seid = pf->main_vsi_seid;
  10528. ctxt.pf_num = pf->hw.pf_id;
  10529. ctxt.vf_num = 0;
  10530. ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
  10531. ctxt.flags = I40E_AQ_VSI_TYPE_PF;
  10532. if (ret) {
  10533. dev_info(&pf->pdev->dev,
  10534. "couldn't get PF vsi config, err %s aq_err %s\n",
  10535. i40e_stat_str(&pf->hw, ret),
  10536. i40e_aq_str(&pf->hw,
  10537. pf->hw.aq.asq_last_status));
  10538. return -ENOENT;
  10539. }
  10540. vsi->info = ctxt.info;
  10541. vsi->info.valid_sections = 0;
  10542. vsi->seid = ctxt.seid;
  10543. vsi->id = ctxt.vsi_number;
  10544. enabled_tc = i40e_pf_get_tc_map(pf);
  10545. /* Source pruning is enabled by default, so the flag is
  10546. * negative logic - if it's set, we need to fiddle with
  10547. * the VSI to disable source pruning.
  10548. */
  10549. if (pf->flags & I40E_FLAG_SOURCE_PRUNING_DISABLED) {
  10550. memset(&ctxt, 0, sizeof(ctxt));
  10551. ctxt.seid = pf->main_vsi_seid;
  10552. ctxt.pf_num = pf->hw.pf_id;
  10553. ctxt.vf_num = 0;
  10554. ctxt.info.valid_sections |=
  10555. cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  10556. ctxt.info.switch_id =
  10557. cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_LOCAL_LB);
  10558. ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
  10559. if (ret) {
  10560. dev_info(&pf->pdev->dev,
  10561. "update vsi failed, err %s aq_err %s\n",
  10562. i40e_stat_str(&pf->hw, ret),
  10563. i40e_aq_str(&pf->hw,
  10564. pf->hw.aq.asq_last_status));
  10565. ret = -ENOENT;
  10566. goto err;
  10567. }
  10568. }
  10569. /* MFP mode setup queue map and update VSI */
  10570. if ((pf->flags & I40E_FLAG_MFP_ENABLED) &&
  10571. !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
  10572. memset(&ctxt, 0, sizeof(ctxt));
  10573. ctxt.seid = pf->main_vsi_seid;
  10574. ctxt.pf_num = pf->hw.pf_id;
  10575. ctxt.vf_num = 0;
  10576. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
  10577. ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
  10578. if (ret) {
  10579. dev_info(&pf->pdev->dev,
  10580. "update vsi failed, err %s aq_err %s\n",
  10581. i40e_stat_str(&pf->hw, ret),
  10582. i40e_aq_str(&pf->hw,
  10583. pf->hw.aq.asq_last_status));
  10584. ret = -ENOENT;
  10585. goto err;
  10586. }
  10587. /* update the local VSI info queue map */
  10588. i40e_vsi_update_queue_map(vsi, &ctxt);
  10589. vsi->info.valid_sections = 0;
  10590. } else {
  10591. /* Default/Main VSI is only enabled for TC0
  10592. * reconfigure it to enable all TCs that are
  10593. * available on the port in SFP mode.
  10594. * For MFP case the iSCSI PF would use this
  10595. * flow to enable LAN+iSCSI TC.
  10596. */
  10597. ret = i40e_vsi_config_tc(vsi, enabled_tc);
  10598. if (ret) {
  10599. /* Single TC condition is not fatal,
  10600. * message and continue
  10601. */
  10602. dev_info(&pf->pdev->dev,
  10603. "failed to configure TCs for main VSI tc_map 0x%08x, err %s aq_err %s\n",
  10604. enabled_tc,
  10605. i40e_stat_str(&pf->hw, ret),
  10606. i40e_aq_str(&pf->hw,
  10607. pf->hw.aq.asq_last_status));
  10608. }
  10609. }
  10610. break;
  10611. case I40E_VSI_FDIR:
  10612. ctxt.pf_num = hw->pf_id;
  10613. ctxt.vf_num = 0;
  10614. ctxt.uplink_seid = vsi->uplink_seid;
  10615. ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
  10616. ctxt.flags = I40E_AQ_VSI_TYPE_PF;
  10617. if ((pf->flags & I40E_FLAG_VEB_MODE_ENABLED) &&
  10618. (i40e_is_vsi_uplink_mode_veb(vsi))) {
  10619. ctxt.info.valid_sections |=
  10620. cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  10621. ctxt.info.switch_id =
  10622. cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  10623. }
  10624. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
  10625. break;
  10626. case I40E_VSI_VMDQ2:
  10627. ctxt.pf_num = hw->pf_id;
  10628. ctxt.vf_num = 0;
  10629. ctxt.uplink_seid = vsi->uplink_seid;
  10630. ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
  10631. ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
  10632. /* This VSI is connected to VEB so the switch_id
  10633. * should be set to zero by default.
  10634. */
  10635. if (i40e_is_vsi_uplink_mode_veb(vsi)) {
  10636. ctxt.info.valid_sections |=
  10637. cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  10638. ctxt.info.switch_id =
  10639. cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  10640. }
  10641. /* Setup the VSI tx/rx queue map for TC0 only for now */
  10642. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
  10643. break;
  10644. case I40E_VSI_SRIOV:
  10645. ctxt.pf_num = hw->pf_id;
  10646. ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
  10647. ctxt.uplink_seid = vsi->uplink_seid;
  10648. ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
  10649. ctxt.flags = I40E_AQ_VSI_TYPE_VF;
  10650. /* This VSI is connected to VEB so the switch_id
  10651. * should be set to zero by default.
  10652. */
  10653. if (i40e_is_vsi_uplink_mode_veb(vsi)) {
  10654. ctxt.info.valid_sections |=
  10655. cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  10656. ctxt.info.switch_id =
  10657. cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  10658. }
  10659. if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
  10660. ctxt.info.valid_sections |=
  10661. cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
  10662. ctxt.info.queueing_opt_flags |=
  10663. (I40E_AQ_VSI_QUE_OPT_TCP_ENA |
  10664. I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI);
  10665. }
  10666. ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
  10667. ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
  10668. if (pf->vf[vsi->vf_id].spoofchk) {
  10669. ctxt.info.valid_sections |=
  10670. cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
  10671. ctxt.info.sec_flags |=
  10672. (I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
  10673. I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
  10674. }
  10675. /* Setup the VSI tx/rx queue map for TC0 only for now */
  10676. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
  10677. break;
  10678. case I40E_VSI_IWARP:
  10679. /* send down message to iWARP */
  10680. break;
  10681. default:
  10682. return -ENODEV;
  10683. }
  10684. if (vsi->type != I40E_VSI_MAIN) {
  10685. ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
  10686. if (ret) {
  10687. dev_info(&vsi->back->pdev->dev,
  10688. "add vsi failed, err %s aq_err %s\n",
  10689. i40e_stat_str(&pf->hw, ret),
  10690. i40e_aq_str(&pf->hw,
  10691. pf->hw.aq.asq_last_status));
  10692. ret = -ENOENT;
  10693. goto err;
  10694. }
  10695. vsi->info = ctxt.info;
  10696. vsi->info.valid_sections = 0;
  10697. vsi->seid = ctxt.seid;
  10698. vsi->id = ctxt.vsi_number;
  10699. }
  10700. vsi->active_filters = 0;
  10701. clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
  10702. spin_lock_bh(&vsi->mac_filter_hash_lock);
  10703. /* If macvlan filters already exist, force them to get loaded */
  10704. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  10705. f->state = I40E_FILTER_NEW;
  10706. f_count++;
  10707. }
  10708. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  10709. if (f_count) {
  10710. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  10711. set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
  10712. }
  10713. /* Update VSI BW information */
  10714. ret = i40e_vsi_get_bw_info(vsi);
  10715. if (ret) {
  10716. dev_info(&pf->pdev->dev,
  10717. "couldn't get vsi bw info, err %s aq_err %s\n",
  10718. i40e_stat_str(&pf->hw, ret),
  10719. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  10720. /* VSI is already added so not tearing that up */
  10721. ret = 0;
  10722. }
  10723. err:
  10724. return ret;
  10725. }
  10726. /**
  10727. * i40e_vsi_release - Delete a VSI and free its resources
  10728. * @vsi: the VSI being removed
  10729. *
  10730. * Returns 0 on success or < 0 on error
  10731. **/
  10732. int i40e_vsi_release(struct i40e_vsi *vsi)
  10733. {
  10734. struct i40e_mac_filter *f;
  10735. struct hlist_node *h;
  10736. struct i40e_veb *veb = NULL;
  10737. struct i40e_pf *pf;
  10738. u16 uplink_seid;
  10739. int i, n, bkt;
  10740. pf = vsi->back;
  10741. /* release of a VEB-owner or last VSI is not allowed */
  10742. if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
  10743. dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
  10744. vsi->seid, vsi->uplink_seid);
  10745. return -ENODEV;
  10746. }
  10747. if (vsi == pf->vsi[pf->lan_vsi] &&
  10748. !test_bit(__I40E_DOWN, pf->state)) {
  10749. dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
  10750. return -ENODEV;
  10751. }
  10752. uplink_seid = vsi->uplink_seid;
  10753. if (vsi->type != I40E_VSI_SRIOV) {
  10754. if (vsi->netdev_registered) {
  10755. vsi->netdev_registered = false;
  10756. if (vsi->netdev) {
  10757. /* results in a call to i40e_close() */
  10758. unregister_netdev(vsi->netdev);
  10759. }
  10760. } else {
  10761. i40e_vsi_close(vsi);
  10762. }
  10763. i40e_vsi_disable_irq(vsi);
  10764. }
  10765. spin_lock_bh(&vsi->mac_filter_hash_lock);
  10766. /* clear the sync flag on all filters */
  10767. if (vsi->netdev) {
  10768. __dev_uc_unsync(vsi->netdev, NULL);
  10769. __dev_mc_unsync(vsi->netdev, NULL);
  10770. }
  10771. /* make sure any remaining filters are marked for deletion */
  10772. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
  10773. __i40e_del_filter(vsi, f);
  10774. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  10775. i40e_sync_vsi_filters(vsi);
  10776. i40e_vsi_delete(vsi);
  10777. i40e_vsi_free_q_vectors(vsi);
  10778. if (vsi->netdev) {
  10779. free_netdev(vsi->netdev);
  10780. vsi->netdev = NULL;
  10781. }
  10782. i40e_vsi_clear_rings(vsi);
  10783. i40e_vsi_clear(vsi);
  10784. /* If this was the last thing on the VEB, except for the
  10785. * controlling VSI, remove the VEB, which puts the controlling
  10786. * VSI onto the next level down in the switch.
  10787. *
  10788. * Well, okay, there's one more exception here: don't remove
  10789. * the orphan VEBs yet. We'll wait for an explicit remove request
  10790. * from up the network stack.
  10791. */
  10792. for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
  10793. if (pf->vsi[i] &&
  10794. pf->vsi[i]->uplink_seid == uplink_seid &&
  10795. (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
  10796. n++; /* count the VSIs */
  10797. }
  10798. }
  10799. for (i = 0; i < I40E_MAX_VEB; i++) {
  10800. if (!pf->veb[i])
  10801. continue;
  10802. if (pf->veb[i]->uplink_seid == uplink_seid)
  10803. n++; /* count the VEBs */
  10804. if (pf->veb[i]->seid == uplink_seid)
  10805. veb = pf->veb[i];
  10806. }
  10807. if (n == 0 && veb && veb->uplink_seid != 0)
  10808. i40e_veb_release(veb);
  10809. return 0;
  10810. }
  10811. /**
  10812. * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
  10813. * @vsi: ptr to the VSI
  10814. *
  10815. * This should only be called after i40e_vsi_mem_alloc() which allocates the
  10816. * corresponding SW VSI structure and initializes num_queue_pairs for the
  10817. * newly allocated VSI.
  10818. *
  10819. * Returns 0 on success or negative on failure
  10820. **/
  10821. static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
  10822. {
  10823. int ret = -ENOENT;
  10824. struct i40e_pf *pf = vsi->back;
  10825. if (vsi->q_vectors[0]) {
  10826. dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
  10827. vsi->seid);
  10828. return -EEXIST;
  10829. }
  10830. if (vsi->base_vector) {
  10831. dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
  10832. vsi->seid, vsi->base_vector);
  10833. return -EEXIST;
  10834. }
  10835. ret = i40e_vsi_alloc_q_vectors(vsi);
  10836. if (ret) {
  10837. dev_info(&pf->pdev->dev,
  10838. "failed to allocate %d q_vector for VSI %d, ret=%d\n",
  10839. vsi->num_q_vectors, vsi->seid, ret);
  10840. vsi->num_q_vectors = 0;
  10841. goto vector_setup_out;
  10842. }
  10843. /* In Legacy mode, we do not have to get any other vector since we
  10844. * piggyback on the misc/ICR0 for queue interrupts.
  10845. */
  10846. if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
  10847. return ret;
  10848. if (vsi->num_q_vectors)
  10849. vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
  10850. vsi->num_q_vectors, vsi->idx);
  10851. if (vsi->base_vector < 0) {
  10852. dev_info(&pf->pdev->dev,
  10853. "failed to get tracking for %d vectors for VSI %d, err=%d\n",
  10854. vsi->num_q_vectors, vsi->seid, vsi->base_vector);
  10855. i40e_vsi_free_q_vectors(vsi);
  10856. ret = -ENOENT;
  10857. goto vector_setup_out;
  10858. }
  10859. vector_setup_out:
  10860. return ret;
  10861. }
  10862. /**
  10863. * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
  10864. * @vsi: pointer to the vsi.
  10865. *
  10866. * This re-allocates a vsi's queue resources.
  10867. *
  10868. * Returns pointer to the successfully allocated and configured VSI sw struct
  10869. * on success, otherwise returns NULL on failure.
  10870. **/
  10871. static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
  10872. {
  10873. u16 alloc_queue_pairs;
  10874. struct i40e_pf *pf;
  10875. u8 enabled_tc;
  10876. int ret;
  10877. if (!vsi)
  10878. return NULL;
  10879. pf = vsi->back;
  10880. i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
  10881. i40e_vsi_clear_rings(vsi);
  10882. i40e_vsi_free_arrays(vsi, false);
  10883. i40e_set_num_rings_in_vsi(vsi);
  10884. ret = i40e_vsi_alloc_arrays(vsi, false);
  10885. if (ret)
  10886. goto err_vsi;
  10887. alloc_queue_pairs = vsi->alloc_queue_pairs *
  10888. (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
  10889. ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
  10890. if (ret < 0) {
  10891. dev_info(&pf->pdev->dev,
  10892. "failed to get tracking for %d queues for VSI %d err %d\n",
  10893. alloc_queue_pairs, vsi->seid, ret);
  10894. goto err_vsi;
  10895. }
  10896. vsi->base_queue = ret;
  10897. /* Update the FW view of the VSI. Force a reset of TC and queue
  10898. * layout configurations.
  10899. */
  10900. enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
  10901. pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
  10902. pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
  10903. i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
  10904. if (vsi->type == I40E_VSI_MAIN)
  10905. i40e_rm_default_mac_filter(vsi, pf->hw.mac.perm_addr);
  10906. /* assign it some queues */
  10907. ret = i40e_alloc_rings(vsi);
  10908. if (ret)
  10909. goto err_rings;
  10910. /* map all of the rings to the q_vectors */
  10911. i40e_vsi_map_rings_to_vectors(vsi);
  10912. return vsi;
  10913. err_rings:
  10914. i40e_vsi_free_q_vectors(vsi);
  10915. if (vsi->netdev_registered) {
  10916. vsi->netdev_registered = false;
  10917. unregister_netdev(vsi->netdev);
  10918. free_netdev(vsi->netdev);
  10919. vsi->netdev = NULL;
  10920. }
  10921. i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
  10922. err_vsi:
  10923. i40e_vsi_clear(vsi);
  10924. return NULL;
  10925. }
  10926. /**
  10927. * i40e_vsi_setup - Set up a VSI by a given type
  10928. * @pf: board private structure
  10929. * @type: VSI type
  10930. * @uplink_seid: the switch element to link to
  10931. * @param1: usage depends upon VSI type. For VF types, indicates VF id
  10932. *
  10933. * This allocates the sw VSI structure and its queue resources, then add a VSI
  10934. * to the identified VEB.
  10935. *
  10936. * Returns pointer to the successfully allocated and configure VSI sw struct on
  10937. * success, otherwise returns NULL on failure.
  10938. **/
  10939. struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
  10940. u16 uplink_seid, u32 param1)
  10941. {
  10942. struct i40e_vsi *vsi = NULL;
  10943. struct i40e_veb *veb = NULL;
  10944. u16 alloc_queue_pairs;
  10945. int ret, i;
  10946. int v_idx;
  10947. /* The requested uplink_seid must be either
  10948. * - the PF's port seid
  10949. * no VEB is needed because this is the PF
  10950. * or this is a Flow Director special case VSI
  10951. * - seid of an existing VEB
  10952. * - seid of a VSI that owns an existing VEB
  10953. * - seid of a VSI that doesn't own a VEB
  10954. * a new VEB is created and the VSI becomes the owner
  10955. * - seid of the PF VSI, which is what creates the first VEB
  10956. * this is a special case of the previous
  10957. *
  10958. * Find which uplink_seid we were given and create a new VEB if needed
  10959. */
  10960. for (i = 0; i < I40E_MAX_VEB; i++) {
  10961. if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
  10962. veb = pf->veb[i];
  10963. break;
  10964. }
  10965. }
  10966. if (!veb && uplink_seid != pf->mac_seid) {
  10967. for (i = 0; i < pf->num_alloc_vsi; i++) {
  10968. if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
  10969. vsi = pf->vsi[i];
  10970. break;
  10971. }
  10972. }
  10973. if (!vsi) {
  10974. dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
  10975. uplink_seid);
  10976. return NULL;
  10977. }
  10978. if (vsi->uplink_seid == pf->mac_seid)
  10979. veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
  10980. vsi->tc_config.enabled_tc);
  10981. else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
  10982. veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
  10983. vsi->tc_config.enabled_tc);
  10984. if (veb) {
  10985. if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
  10986. dev_info(&vsi->back->pdev->dev,
  10987. "New VSI creation error, uplink seid of LAN VSI expected.\n");
  10988. return NULL;
  10989. }
  10990. /* We come up by default in VEPA mode if SRIOV is not
  10991. * already enabled, in which case we can't force VEPA
  10992. * mode.
  10993. */
  10994. if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
  10995. veb->bridge_mode = BRIDGE_MODE_VEPA;
  10996. pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
  10997. }
  10998. i40e_config_bridge_mode(veb);
  10999. }
  11000. for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
  11001. if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
  11002. veb = pf->veb[i];
  11003. }
  11004. if (!veb) {
  11005. dev_info(&pf->pdev->dev, "couldn't add VEB\n");
  11006. return NULL;
  11007. }
  11008. vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
  11009. uplink_seid = veb->seid;
  11010. }
  11011. /* get vsi sw struct */
  11012. v_idx = i40e_vsi_mem_alloc(pf, type);
  11013. if (v_idx < 0)
  11014. goto err_alloc;
  11015. vsi = pf->vsi[v_idx];
  11016. if (!vsi)
  11017. goto err_alloc;
  11018. vsi->type = type;
  11019. vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
  11020. if (type == I40E_VSI_MAIN)
  11021. pf->lan_vsi = v_idx;
  11022. else if (type == I40E_VSI_SRIOV)
  11023. vsi->vf_id = param1;
  11024. /* assign it some queues */
  11025. alloc_queue_pairs = vsi->alloc_queue_pairs *
  11026. (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
  11027. ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
  11028. if (ret < 0) {
  11029. dev_info(&pf->pdev->dev,
  11030. "failed to get tracking for %d queues for VSI %d err=%d\n",
  11031. alloc_queue_pairs, vsi->seid, ret);
  11032. goto err_vsi;
  11033. }
  11034. vsi->base_queue = ret;
  11035. /* get a VSI from the hardware */
  11036. vsi->uplink_seid = uplink_seid;
  11037. ret = i40e_add_vsi(vsi);
  11038. if (ret)
  11039. goto err_vsi;
  11040. switch (vsi->type) {
  11041. /* setup the netdev if needed */
  11042. case I40E_VSI_MAIN:
  11043. case I40E_VSI_VMDQ2:
  11044. ret = i40e_config_netdev(vsi);
  11045. if (ret)
  11046. goto err_netdev;
  11047. ret = register_netdev(vsi->netdev);
  11048. if (ret)
  11049. goto err_netdev;
  11050. vsi->netdev_registered = true;
  11051. netif_carrier_off(vsi->netdev);
  11052. #ifdef CONFIG_I40E_DCB
  11053. /* Setup DCB netlink interface */
  11054. i40e_dcbnl_setup(vsi);
  11055. #endif /* CONFIG_I40E_DCB */
  11056. /* fall through */
  11057. case I40E_VSI_FDIR:
  11058. /* set up vectors and rings if needed */
  11059. ret = i40e_vsi_setup_vectors(vsi);
  11060. if (ret)
  11061. goto err_msix;
  11062. ret = i40e_alloc_rings(vsi);
  11063. if (ret)
  11064. goto err_rings;
  11065. /* map all of the rings to the q_vectors */
  11066. i40e_vsi_map_rings_to_vectors(vsi);
  11067. i40e_vsi_reset_stats(vsi);
  11068. break;
  11069. default:
  11070. /* no netdev or rings for the other VSI types */
  11071. break;
  11072. }
  11073. if ((pf->hw_features & I40E_HW_RSS_AQ_CAPABLE) &&
  11074. (vsi->type == I40E_VSI_VMDQ2)) {
  11075. ret = i40e_vsi_config_rss(vsi);
  11076. }
  11077. return vsi;
  11078. err_rings:
  11079. i40e_vsi_free_q_vectors(vsi);
  11080. err_msix:
  11081. if (vsi->netdev_registered) {
  11082. vsi->netdev_registered = false;
  11083. unregister_netdev(vsi->netdev);
  11084. free_netdev(vsi->netdev);
  11085. vsi->netdev = NULL;
  11086. }
  11087. err_netdev:
  11088. i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
  11089. err_vsi:
  11090. i40e_vsi_clear(vsi);
  11091. err_alloc:
  11092. return NULL;
  11093. }
  11094. /**
  11095. * i40e_veb_get_bw_info - Query VEB BW information
  11096. * @veb: the veb to query
  11097. *
  11098. * Query the Tx scheduler BW configuration data for given VEB
  11099. **/
  11100. static int i40e_veb_get_bw_info(struct i40e_veb *veb)
  11101. {
  11102. struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
  11103. struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
  11104. struct i40e_pf *pf = veb->pf;
  11105. struct i40e_hw *hw = &pf->hw;
  11106. u32 tc_bw_max;
  11107. int ret = 0;
  11108. int i;
  11109. ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
  11110. &bw_data, NULL);
  11111. if (ret) {
  11112. dev_info(&pf->pdev->dev,
  11113. "query veb bw config failed, err %s aq_err %s\n",
  11114. i40e_stat_str(&pf->hw, ret),
  11115. i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
  11116. goto out;
  11117. }
  11118. ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
  11119. &ets_data, NULL);
  11120. if (ret) {
  11121. dev_info(&pf->pdev->dev,
  11122. "query veb bw ets config failed, err %s aq_err %s\n",
  11123. i40e_stat_str(&pf->hw, ret),
  11124. i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
  11125. goto out;
  11126. }
  11127. veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
  11128. veb->bw_max_quanta = ets_data.tc_bw_max;
  11129. veb->is_abs_credits = bw_data.absolute_credits_enable;
  11130. veb->enabled_tc = ets_data.tc_valid_bits;
  11131. tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
  11132. (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
  11133. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  11134. veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
  11135. veb->bw_tc_limit_credits[i] =
  11136. le16_to_cpu(bw_data.tc_bw_limits[i]);
  11137. veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
  11138. }
  11139. out:
  11140. return ret;
  11141. }
  11142. /**
  11143. * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
  11144. * @pf: board private structure
  11145. *
  11146. * On error: returns error code (negative)
  11147. * On success: returns vsi index in PF (positive)
  11148. **/
  11149. static int i40e_veb_mem_alloc(struct i40e_pf *pf)
  11150. {
  11151. int ret = -ENOENT;
  11152. struct i40e_veb *veb;
  11153. int i;
  11154. /* Need to protect the allocation of switch elements at the PF level */
  11155. mutex_lock(&pf->switch_mutex);
  11156. /* VEB list may be fragmented if VEB creation/destruction has
  11157. * been happening. We can afford to do a quick scan to look
  11158. * for any free slots in the list.
  11159. *
  11160. * find next empty veb slot, looping back around if necessary
  11161. */
  11162. i = 0;
  11163. while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
  11164. i++;
  11165. if (i >= I40E_MAX_VEB) {
  11166. ret = -ENOMEM;
  11167. goto err_alloc_veb; /* out of VEB slots! */
  11168. }
  11169. veb = kzalloc(sizeof(*veb), GFP_KERNEL);
  11170. if (!veb) {
  11171. ret = -ENOMEM;
  11172. goto err_alloc_veb;
  11173. }
  11174. veb->pf = pf;
  11175. veb->idx = i;
  11176. veb->enabled_tc = 1;
  11177. pf->veb[i] = veb;
  11178. ret = i;
  11179. err_alloc_veb:
  11180. mutex_unlock(&pf->switch_mutex);
  11181. return ret;
  11182. }
  11183. /**
  11184. * i40e_switch_branch_release - Delete a branch of the switch tree
  11185. * @branch: where to start deleting
  11186. *
  11187. * This uses recursion to find the tips of the branch to be
  11188. * removed, deleting until we get back to and can delete this VEB.
  11189. **/
  11190. static void i40e_switch_branch_release(struct i40e_veb *branch)
  11191. {
  11192. struct i40e_pf *pf = branch->pf;
  11193. u16 branch_seid = branch->seid;
  11194. u16 veb_idx = branch->idx;
  11195. int i;
  11196. /* release any VEBs on this VEB - RECURSION */
  11197. for (i = 0; i < I40E_MAX_VEB; i++) {
  11198. if (!pf->veb[i])
  11199. continue;
  11200. if (pf->veb[i]->uplink_seid == branch->seid)
  11201. i40e_switch_branch_release(pf->veb[i]);
  11202. }
  11203. /* Release the VSIs on this VEB, but not the owner VSI.
  11204. *
  11205. * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
  11206. * the VEB itself, so don't use (*branch) after this loop.
  11207. */
  11208. for (i = 0; i < pf->num_alloc_vsi; i++) {
  11209. if (!pf->vsi[i])
  11210. continue;
  11211. if (pf->vsi[i]->uplink_seid == branch_seid &&
  11212. (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
  11213. i40e_vsi_release(pf->vsi[i]);
  11214. }
  11215. }
  11216. /* There's one corner case where the VEB might not have been
  11217. * removed, so double check it here and remove it if needed.
  11218. * This case happens if the veb was created from the debugfs
  11219. * commands and no VSIs were added to it.
  11220. */
  11221. if (pf->veb[veb_idx])
  11222. i40e_veb_release(pf->veb[veb_idx]);
  11223. }
  11224. /**
  11225. * i40e_veb_clear - remove veb struct
  11226. * @veb: the veb to remove
  11227. **/
  11228. static void i40e_veb_clear(struct i40e_veb *veb)
  11229. {
  11230. if (!veb)
  11231. return;
  11232. if (veb->pf) {
  11233. struct i40e_pf *pf = veb->pf;
  11234. mutex_lock(&pf->switch_mutex);
  11235. if (pf->veb[veb->idx] == veb)
  11236. pf->veb[veb->idx] = NULL;
  11237. mutex_unlock(&pf->switch_mutex);
  11238. }
  11239. kfree(veb);
  11240. }
  11241. /**
  11242. * i40e_veb_release - Delete a VEB and free its resources
  11243. * @veb: the VEB being removed
  11244. **/
  11245. void i40e_veb_release(struct i40e_veb *veb)
  11246. {
  11247. struct i40e_vsi *vsi = NULL;
  11248. struct i40e_pf *pf;
  11249. int i, n = 0;
  11250. pf = veb->pf;
  11251. /* find the remaining VSI and check for extras */
  11252. for (i = 0; i < pf->num_alloc_vsi; i++) {
  11253. if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
  11254. n++;
  11255. vsi = pf->vsi[i];
  11256. }
  11257. }
  11258. if (n != 1) {
  11259. dev_info(&pf->pdev->dev,
  11260. "can't remove VEB %d with %d VSIs left\n",
  11261. veb->seid, n);
  11262. return;
  11263. }
  11264. /* move the remaining VSI to uplink veb */
  11265. vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
  11266. if (veb->uplink_seid) {
  11267. vsi->uplink_seid = veb->uplink_seid;
  11268. if (veb->uplink_seid == pf->mac_seid)
  11269. vsi->veb_idx = I40E_NO_VEB;
  11270. else
  11271. vsi->veb_idx = veb->veb_idx;
  11272. } else {
  11273. /* floating VEB */
  11274. vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
  11275. vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
  11276. }
  11277. i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
  11278. i40e_veb_clear(veb);
  11279. }
  11280. /**
  11281. * i40e_add_veb - create the VEB in the switch
  11282. * @veb: the VEB to be instantiated
  11283. * @vsi: the controlling VSI
  11284. **/
  11285. static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
  11286. {
  11287. struct i40e_pf *pf = veb->pf;
  11288. bool enable_stats = !!(pf->flags & I40E_FLAG_VEB_STATS_ENABLED);
  11289. int ret;
  11290. ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi->seid,
  11291. veb->enabled_tc, false,
  11292. &veb->seid, enable_stats, NULL);
  11293. /* get a VEB from the hardware */
  11294. if (ret) {
  11295. dev_info(&pf->pdev->dev,
  11296. "couldn't add VEB, err %s aq_err %s\n",
  11297. i40e_stat_str(&pf->hw, ret),
  11298. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  11299. return -EPERM;
  11300. }
  11301. /* get statistics counter */
  11302. ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
  11303. &veb->stats_idx, NULL, NULL, NULL);
  11304. if (ret) {
  11305. dev_info(&pf->pdev->dev,
  11306. "couldn't get VEB statistics idx, err %s aq_err %s\n",
  11307. i40e_stat_str(&pf->hw, ret),
  11308. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  11309. return -EPERM;
  11310. }
  11311. ret = i40e_veb_get_bw_info(veb);
  11312. if (ret) {
  11313. dev_info(&pf->pdev->dev,
  11314. "couldn't get VEB bw info, err %s aq_err %s\n",
  11315. i40e_stat_str(&pf->hw, ret),
  11316. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  11317. i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
  11318. return -ENOENT;
  11319. }
  11320. vsi->uplink_seid = veb->seid;
  11321. vsi->veb_idx = veb->idx;
  11322. vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
  11323. return 0;
  11324. }
  11325. /**
  11326. * i40e_veb_setup - Set up a VEB
  11327. * @pf: board private structure
  11328. * @flags: VEB setup flags
  11329. * @uplink_seid: the switch element to link to
  11330. * @vsi_seid: the initial VSI seid
  11331. * @enabled_tc: Enabled TC bit-map
  11332. *
  11333. * This allocates the sw VEB structure and links it into the switch
  11334. * It is possible and legal for this to be a duplicate of an already
  11335. * existing VEB. It is also possible for both uplink and vsi seids
  11336. * to be zero, in order to create a floating VEB.
  11337. *
  11338. * Returns pointer to the successfully allocated VEB sw struct on
  11339. * success, otherwise returns NULL on failure.
  11340. **/
  11341. struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
  11342. u16 uplink_seid, u16 vsi_seid,
  11343. u8 enabled_tc)
  11344. {
  11345. struct i40e_veb *veb, *uplink_veb = NULL;
  11346. int vsi_idx, veb_idx;
  11347. int ret;
  11348. /* if one seid is 0, the other must be 0 to create a floating relay */
  11349. if ((uplink_seid == 0 || vsi_seid == 0) &&
  11350. (uplink_seid + vsi_seid != 0)) {
  11351. dev_info(&pf->pdev->dev,
  11352. "one, not both seid's are 0: uplink=%d vsi=%d\n",
  11353. uplink_seid, vsi_seid);
  11354. return NULL;
  11355. }
  11356. /* make sure there is such a vsi and uplink */
  11357. for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
  11358. if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
  11359. break;
  11360. if (vsi_idx >= pf->num_alloc_vsi && vsi_seid != 0) {
  11361. dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
  11362. vsi_seid);
  11363. return NULL;
  11364. }
  11365. if (uplink_seid && uplink_seid != pf->mac_seid) {
  11366. for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
  11367. if (pf->veb[veb_idx] &&
  11368. pf->veb[veb_idx]->seid == uplink_seid) {
  11369. uplink_veb = pf->veb[veb_idx];
  11370. break;
  11371. }
  11372. }
  11373. if (!uplink_veb) {
  11374. dev_info(&pf->pdev->dev,
  11375. "uplink seid %d not found\n", uplink_seid);
  11376. return NULL;
  11377. }
  11378. }
  11379. /* get veb sw struct */
  11380. veb_idx = i40e_veb_mem_alloc(pf);
  11381. if (veb_idx < 0)
  11382. goto err_alloc;
  11383. veb = pf->veb[veb_idx];
  11384. veb->flags = flags;
  11385. veb->uplink_seid = uplink_seid;
  11386. veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
  11387. veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
  11388. /* create the VEB in the switch */
  11389. ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
  11390. if (ret)
  11391. goto err_veb;
  11392. if (vsi_idx == pf->lan_vsi)
  11393. pf->lan_veb = veb->idx;
  11394. return veb;
  11395. err_veb:
  11396. i40e_veb_clear(veb);
  11397. err_alloc:
  11398. return NULL;
  11399. }
  11400. /**
  11401. * i40e_setup_pf_switch_element - set PF vars based on switch type
  11402. * @pf: board private structure
  11403. * @ele: element we are building info from
  11404. * @num_reported: total number of elements
  11405. * @printconfig: should we print the contents
  11406. *
  11407. * helper function to assist in extracting a few useful SEID values.
  11408. **/
  11409. static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
  11410. struct i40e_aqc_switch_config_element_resp *ele,
  11411. u16 num_reported, bool printconfig)
  11412. {
  11413. u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
  11414. u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
  11415. u8 element_type = ele->element_type;
  11416. u16 seid = le16_to_cpu(ele->seid);
  11417. if (printconfig)
  11418. dev_info(&pf->pdev->dev,
  11419. "type=%d seid=%d uplink=%d downlink=%d\n",
  11420. element_type, seid, uplink_seid, downlink_seid);
  11421. switch (element_type) {
  11422. case I40E_SWITCH_ELEMENT_TYPE_MAC:
  11423. pf->mac_seid = seid;
  11424. break;
  11425. case I40E_SWITCH_ELEMENT_TYPE_VEB:
  11426. /* Main VEB? */
  11427. if (uplink_seid != pf->mac_seid)
  11428. break;
  11429. if (pf->lan_veb == I40E_NO_VEB) {
  11430. int v;
  11431. /* find existing or else empty VEB */
  11432. for (v = 0; v < I40E_MAX_VEB; v++) {
  11433. if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
  11434. pf->lan_veb = v;
  11435. break;
  11436. }
  11437. }
  11438. if (pf->lan_veb == I40E_NO_VEB) {
  11439. v = i40e_veb_mem_alloc(pf);
  11440. if (v < 0)
  11441. break;
  11442. pf->lan_veb = v;
  11443. }
  11444. }
  11445. pf->veb[pf->lan_veb]->seid = seid;
  11446. pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
  11447. pf->veb[pf->lan_veb]->pf = pf;
  11448. pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
  11449. break;
  11450. case I40E_SWITCH_ELEMENT_TYPE_VSI:
  11451. if (num_reported != 1)
  11452. break;
  11453. /* This is immediately after a reset so we can assume this is
  11454. * the PF's VSI
  11455. */
  11456. pf->mac_seid = uplink_seid;
  11457. pf->pf_seid = downlink_seid;
  11458. pf->main_vsi_seid = seid;
  11459. if (printconfig)
  11460. dev_info(&pf->pdev->dev,
  11461. "pf_seid=%d main_vsi_seid=%d\n",
  11462. pf->pf_seid, pf->main_vsi_seid);
  11463. break;
  11464. case I40E_SWITCH_ELEMENT_TYPE_PF:
  11465. case I40E_SWITCH_ELEMENT_TYPE_VF:
  11466. case I40E_SWITCH_ELEMENT_TYPE_EMP:
  11467. case I40E_SWITCH_ELEMENT_TYPE_BMC:
  11468. case I40E_SWITCH_ELEMENT_TYPE_PE:
  11469. case I40E_SWITCH_ELEMENT_TYPE_PA:
  11470. /* ignore these for now */
  11471. break;
  11472. default:
  11473. dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
  11474. element_type, seid);
  11475. break;
  11476. }
  11477. }
  11478. /**
  11479. * i40e_fetch_switch_configuration - Get switch config from firmware
  11480. * @pf: board private structure
  11481. * @printconfig: should we print the contents
  11482. *
  11483. * Get the current switch configuration from the device and
  11484. * extract a few useful SEID values.
  11485. **/
  11486. int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
  11487. {
  11488. struct i40e_aqc_get_switch_config_resp *sw_config;
  11489. u16 next_seid = 0;
  11490. int ret = 0;
  11491. u8 *aq_buf;
  11492. int i;
  11493. aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
  11494. if (!aq_buf)
  11495. return -ENOMEM;
  11496. sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
  11497. do {
  11498. u16 num_reported, num_total;
  11499. ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
  11500. I40E_AQ_LARGE_BUF,
  11501. &next_seid, NULL);
  11502. if (ret) {
  11503. dev_info(&pf->pdev->dev,
  11504. "get switch config failed err %s aq_err %s\n",
  11505. i40e_stat_str(&pf->hw, ret),
  11506. i40e_aq_str(&pf->hw,
  11507. pf->hw.aq.asq_last_status));
  11508. kfree(aq_buf);
  11509. return -ENOENT;
  11510. }
  11511. num_reported = le16_to_cpu(sw_config->header.num_reported);
  11512. num_total = le16_to_cpu(sw_config->header.num_total);
  11513. if (printconfig)
  11514. dev_info(&pf->pdev->dev,
  11515. "header: %d reported %d total\n",
  11516. num_reported, num_total);
  11517. for (i = 0; i < num_reported; i++) {
  11518. struct i40e_aqc_switch_config_element_resp *ele =
  11519. &sw_config->element[i];
  11520. i40e_setup_pf_switch_element(pf, ele, num_reported,
  11521. printconfig);
  11522. }
  11523. } while (next_seid != 0);
  11524. kfree(aq_buf);
  11525. return ret;
  11526. }
  11527. /**
  11528. * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
  11529. * @pf: board private structure
  11530. * @reinit: if the Main VSI needs to re-initialized.
  11531. *
  11532. * Returns 0 on success, negative value on failure
  11533. **/
  11534. static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit)
  11535. {
  11536. u16 flags = 0;
  11537. int ret;
  11538. /* find out what's out there already */
  11539. ret = i40e_fetch_switch_configuration(pf, false);
  11540. if (ret) {
  11541. dev_info(&pf->pdev->dev,
  11542. "couldn't fetch switch config, err %s aq_err %s\n",
  11543. i40e_stat_str(&pf->hw, ret),
  11544. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  11545. return ret;
  11546. }
  11547. i40e_pf_reset_stats(pf);
  11548. /* set the switch config bit for the whole device to
  11549. * support limited promisc or true promisc
  11550. * when user requests promisc. The default is limited
  11551. * promisc.
  11552. */
  11553. if ((pf->hw.pf_id == 0) &&
  11554. !(pf->flags & I40E_FLAG_TRUE_PROMISC_SUPPORT)) {
  11555. flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
  11556. pf->last_sw_conf_flags = flags;
  11557. }
  11558. if (pf->hw.pf_id == 0) {
  11559. u16 valid_flags;
  11560. valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
  11561. ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags, 0,
  11562. NULL);
  11563. if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) {
  11564. dev_info(&pf->pdev->dev,
  11565. "couldn't set switch config bits, err %s aq_err %s\n",
  11566. i40e_stat_str(&pf->hw, ret),
  11567. i40e_aq_str(&pf->hw,
  11568. pf->hw.aq.asq_last_status));
  11569. /* not a fatal problem, just keep going */
  11570. }
  11571. pf->last_sw_conf_valid_flags = valid_flags;
  11572. }
  11573. /* first time setup */
  11574. if (pf->lan_vsi == I40E_NO_VSI || reinit) {
  11575. struct i40e_vsi *vsi = NULL;
  11576. u16 uplink_seid;
  11577. /* Set up the PF VSI associated with the PF's main VSI
  11578. * that is already in the HW switch
  11579. */
  11580. if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
  11581. uplink_seid = pf->veb[pf->lan_veb]->seid;
  11582. else
  11583. uplink_seid = pf->mac_seid;
  11584. if (pf->lan_vsi == I40E_NO_VSI)
  11585. vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
  11586. else if (reinit)
  11587. vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
  11588. if (!vsi) {
  11589. dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
  11590. i40e_cloud_filter_exit(pf);
  11591. i40e_fdir_teardown(pf);
  11592. return -EAGAIN;
  11593. }
  11594. } else {
  11595. /* force a reset of TC and queue layout configurations */
  11596. u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
  11597. pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
  11598. pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
  11599. i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
  11600. }
  11601. i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
  11602. i40e_fdir_sb_setup(pf);
  11603. /* Setup static PF queue filter control settings */
  11604. ret = i40e_setup_pf_filter_control(pf);
  11605. if (ret) {
  11606. dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
  11607. ret);
  11608. /* Failure here should not stop continuing other steps */
  11609. }
  11610. /* enable RSS in the HW, even for only one queue, as the stack can use
  11611. * the hash
  11612. */
  11613. if ((pf->flags & I40E_FLAG_RSS_ENABLED))
  11614. i40e_pf_config_rss(pf);
  11615. /* fill in link information and enable LSE reporting */
  11616. i40e_link_event(pf);
  11617. /* Initialize user-specific link properties */
  11618. pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
  11619. I40E_AQ_AN_COMPLETED) ? true : false);
  11620. i40e_ptp_init(pf);
  11621. /* repopulate tunnel port filters */
  11622. i40e_sync_udp_filters(pf);
  11623. return ret;
  11624. }
  11625. /**
  11626. * i40e_determine_queue_usage - Work out queue distribution
  11627. * @pf: board private structure
  11628. **/
  11629. static void i40e_determine_queue_usage(struct i40e_pf *pf)
  11630. {
  11631. int queues_left;
  11632. int q_max;
  11633. pf->num_lan_qps = 0;
  11634. /* Find the max queues to be put into basic use. We'll always be
  11635. * using TC0, whether or not DCB is running, and TC0 will get the
  11636. * big RSS set.
  11637. */
  11638. queues_left = pf->hw.func_caps.num_tx_qp;
  11639. if ((queues_left == 1) ||
  11640. !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
  11641. /* one qp for PF, no queues for anything else */
  11642. queues_left = 0;
  11643. pf->alloc_rss_size = pf->num_lan_qps = 1;
  11644. /* make sure all the fancies are disabled */
  11645. pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
  11646. I40E_FLAG_IWARP_ENABLED |
  11647. I40E_FLAG_FD_SB_ENABLED |
  11648. I40E_FLAG_FD_ATR_ENABLED |
  11649. I40E_FLAG_DCB_CAPABLE |
  11650. I40E_FLAG_DCB_ENABLED |
  11651. I40E_FLAG_SRIOV_ENABLED |
  11652. I40E_FLAG_VMDQ_ENABLED);
  11653. pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
  11654. } else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
  11655. I40E_FLAG_FD_SB_ENABLED |
  11656. I40E_FLAG_FD_ATR_ENABLED |
  11657. I40E_FLAG_DCB_CAPABLE))) {
  11658. /* one qp for PF */
  11659. pf->alloc_rss_size = pf->num_lan_qps = 1;
  11660. queues_left -= pf->num_lan_qps;
  11661. pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
  11662. I40E_FLAG_IWARP_ENABLED |
  11663. I40E_FLAG_FD_SB_ENABLED |
  11664. I40E_FLAG_FD_ATR_ENABLED |
  11665. I40E_FLAG_DCB_ENABLED |
  11666. I40E_FLAG_VMDQ_ENABLED);
  11667. pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
  11668. } else {
  11669. /* Not enough queues for all TCs */
  11670. if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
  11671. (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
  11672. pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
  11673. I40E_FLAG_DCB_ENABLED);
  11674. dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
  11675. }
  11676. /* limit lan qps to the smaller of qps, cpus or msix */
  11677. q_max = max_t(int, pf->rss_size_max, num_online_cpus());
  11678. q_max = min_t(int, q_max, pf->hw.func_caps.num_tx_qp);
  11679. q_max = min_t(int, q_max, pf->hw.func_caps.num_msix_vectors);
  11680. pf->num_lan_qps = q_max;
  11681. queues_left -= pf->num_lan_qps;
  11682. }
  11683. if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
  11684. if (queues_left > 1) {
  11685. queues_left -= 1; /* save 1 queue for FD */
  11686. } else {
  11687. pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
  11688. pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
  11689. dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
  11690. }
  11691. }
  11692. if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
  11693. pf->num_vf_qps && pf->num_req_vfs && queues_left) {
  11694. pf->num_req_vfs = min_t(int, pf->num_req_vfs,
  11695. (queues_left / pf->num_vf_qps));
  11696. queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
  11697. }
  11698. if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
  11699. pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
  11700. pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
  11701. (queues_left / pf->num_vmdq_qps));
  11702. queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
  11703. }
  11704. pf->queues_left = queues_left;
  11705. dev_dbg(&pf->pdev->dev,
  11706. "qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
  11707. pf->hw.func_caps.num_tx_qp,
  11708. !!(pf->flags & I40E_FLAG_FD_SB_ENABLED),
  11709. pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
  11710. pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
  11711. queues_left);
  11712. }
  11713. /**
  11714. * i40e_setup_pf_filter_control - Setup PF static filter control
  11715. * @pf: PF to be setup
  11716. *
  11717. * i40e_setup_pf_filter_control sets up a PF's initial filter control
  11718. * settings. If PE/FCoE are enabled then it will also set the per PF
  11719. * based filter sizes required for them. It also enables Flow director,
  11720. * ethertype and macvlan type filter settings for the pf.
  11721. *
  11722. * Returns 0 on success, negative on failure
  11723. **/
  11724. static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
  11725. {
  11726. struct i40e_filter_control_settings *settings = &pf->filter_settings;
  11727. settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
  11728. /* Flow Director is enabled */
  11729. if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
  11730. settings->enable_fdir = true;
  11731. /* Ethtype and MACVLAN filters enabled for PF */
  11732. settings->enable_ethtype = true;
  11733. settings->enable_macvlan = true;
  11734. if (i40e_set_filter_control(&pf->hw, settings))
  11735. return -ENOENT;
  11736. return 0;
  11737. }
  11738. #define INFO_STRING_LEN 255
  11739. #define REMAIN(__x) (INFO_STRING_LEN - (__x))
  11740. static void i40e_print_features(struct i40e_pf *pf)
  11741. {
  11742. struct i40e_hw *hw = &pf->hw;
  11743. char *buf;
  11744. int i;
  11745. buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
  11746. if (!buf)
  11747. return;
  11748. i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
  11749. #ifdef CONFIG_PCI_IOV
  11750. i += snprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
  11751. #endif
  11752. i += snprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d",
  11753. pf->hw.func_caps.num_vsis,
  11754. pf->vsi[pf->lan_vsi]->num_queue_pairs);
  11755. if (pf->flags & I40E_FLAG_RSS_ENABLED)
  11756. i += snprintf(&buf[i], REMAIN(i), " RSS");
  11757. if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
  11758. i += snprintf(&buf[i], REMAIN(i), " FD_ATR");
  11759. if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
  11760. i += snprintf(&buf[i], REMAIN(i), " FD_SB");
  11761. i += snprintf(&buf[i], REMAIN(i), " NTUPLE");
  11762. }
  11763. if (pf->flags & I40E_FLAG_DCB_CAPABLE)
  11764. i += snprintf(&buf[i], REMAIN(i), " DCB");
  11765. i += snprintf(&buf[i], REMAIN(i), " VxLAN");
  11766. i += snprintf(&buf[i], REMAIN(i), " Geneve");
  11767. if (pf->flags & I40E_FLAG_PTP)
  11768. i += snprintf(&buf[i], REMAIN(i), " PTP");
  11769. if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
  11770. i += snprintf(&buf[i], REMAIN(i), " VEB");
  11771. else
  11772. i += snprintf(&buf[i], REMAIN(i), " VEPA");
  11773. dev_info(&pf->pdev->dev, "%s\n", buf);
  11774. kfree(buf);
  11775. WARN_ON(i > INFO_STRING_LEN);
  11776. }
  11777. /**
  11778. * i40e_get_platform_mac_addr - get platform-specific MAC address
  11779. * @pdev: PCI device information struct
  11780. * @pf: board private structure
  11781. *
  11782. * Look up the MAC address for the device. First we'll try
  11783. * eth_platform_get_mac_address, which will check Open Firmware, or arch
  11784. * specific fallback. Otherwise, we'll default to the stored value in
  11785. * firmware.
  11786. **/
  11787. static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
  11788. {
  11789. if (eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
  11790. i40e_get_mac_addr(&pf->hw, pf->hw.mac.addr);
  11791. }
  11792. /**
  11793. * i40e_probe - Device initialization routine
  11794. * @pdev: PCI device information struct
  11795. * @ent: entry in i40e_pci_tbl
  11796. *
  11797. * i40e_probe initializes a PF identified by a pci_dev structure.
  11798. * The OS initialization, configuring of the PF private structure,
  11799. * and a hardware reset occur.
  11800. *
  11801. * Returns 0 on success, negative on failure
  11802. **/
  11803. static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  11804. {
  11805. struct i40e_aq_get_phy_abilities_resp abilities;
  11806. struct i40e_pf *pf;
  11807. struct i40e_hw *hw;
  11808. static u16 pfs_found;
  11809. u16 wol_nvm_bits;
  11810. u16 link_status;
  11811. int err;
  11812. u32 val;
  11813. u32 i;
  11814. u8 set_fc_aq_fail;
  11815. err = pci_enable_device_mem(pdev);
  11816. if (err)
  11817. return err;
  11818. /* set up for high or low dma */
  11819. err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
  11820. if (err) {
  11821. err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  11822. if (err) {
  11823. dev_err(&pdev->dev,
  11824. "DMA configuration failed: 0x%x\n", err);
  11825. goto err_dma;
  11826. }
  11827. }
  11828. /* set up pci connections */
  11829. err = pci_request_mem_regions(pdev, i40e_driver_name);
  11830. if (err) {
  11831. dev_info(&pdev->dev,
  11832. "pci_request_selected_regions failed %d\n", err);
  11833. goto err_pci_reg;
  11834. }
  11835. pci_enable_pcie_error_reporting(pdev);
  11836. pci_set_master(pdev);
  11837. /* Now that we have a PCI connection, we need to do the
  11838. * low level device setup. This is primarily setting up
  11839. * the Admin Queue structures and then querying for the
  11840. * device's current profile information.
  11841. */
  11842. pf = kzalloc(sizeof(*pf), GFP_KERNEL);
  11843. if (!pf) {
  11844. err = -ENOMEM;
  11845. goto err_pf_alloc;
  11846. }
  11847. pf->next_vsi = 0;
  11848. pf->pdev = pdev;
  11849. set_bit(__I40E_DOWN, pf->state);
  11850. hw = &pf->hw;
  11851. hw->back = pf;
  11852. pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
  11853. I40E_MAX_CSR_SPACE);
  11854. hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
  11855. if (!hw->hw_addr) {
  11856. err = -EIO;
  11857. dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
  11858. (unsigned int)pci_resource_start(pdev, 0),
  11859. pf->ioremap_len, err);
  11860. goto err_ioremap;
  11861. }
  11862. hw->vendor_id = pdev->vendor;
  11863. hw->device_id = pdev->device;
  11864. pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
  11865. hw->subsystem_vendor_id = pdev->subsystem_vendor;
  11866. hw->subsystem_device_id = pdev->subsystem_device;
  11867. hw->bus.device = PCI_SLOT(pdev->devfn);
  11868. hw->bus.func = PCI_FUNC(pdev->devfn);
  11869. hw->bus.bus_id = pdev->bus->number;
  11870. pf->instance = pfs_found;
  11871. /* Select something other than the 802.1ad ethertype for the
  11872. * switch to use internally and drop on ingress.
  11873. */
  11874. hw->switch_tag = 0xffff;
  11875. hw->first_tag = ETH_P_8021AD;
  11876. hw->second_tag = ETH_P_8021Q;
  11877. INIT_LIST_HEAD(&pf->l3_flex_pit_list);
  11878. INIT_LIST_HEAD(&pf->l4_flex_pit_list);
  11879. /* set up the locks for the AQ, do this only once in probe
  11880. * and destroy them only once in remove
  11881. */
  11882. mutex_init(&hw->aq.asq_mutex);
  11883. mutex_init(&hw->aq.arq_mutex);
  11884. pf->msg_enable = netif_msg_init(debug,
  11885. NETIF_MSG_DRV |
  11886. NETIF_MSG_PROBE |
  11887. NETIF_MSG_LINK);
  11888. if (debug < -1)
  11889. pf->hw.debug_mask = debug;
  11890. /* do a special CORER for clearing PXE mode once at init */
  11891. if (hw->revision_id == 0 &&
  11892. (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
  11893. wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
  11894. i40e_flush(hw);
  11895. msleep(200);
  11896. pf->corer_count++;
  11897. i40e_clear_pxe_mode(hw);
  11898. }
  11899. /* Reset here to make sure all is clean and to define PF 'n' */
  11900. i40e_clear_hw(hw);
  11901. err = i40e_pf_reset(hw);
  11902. if (err) {
  11903. dev_info(&pdev->dev, "Initial pf_reset failed: %d\n", err);
  11904. goto err_pf_reset;
  11905. }
  11906. pf->pfr_count++;
  11907. hw->aq.num_arq_entries = I40E_AQ_LEN;
  11908. hw->aq.num_asq_entries = I40E_AQ_LEN;
  11909. hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
  11910. hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
  11911. pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
  11912. snprintf(pf->int_name, sizeof(pf->int_name) - 1,
  11913. "%s-%s:misc",
  11914. dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
  11915. err = i40e_init_shared_code(hw);
  11916. if (err) {
  11917. dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
  11918. err);
  11919. goto err_pf_reset;
  11920. }
  11921. /* set up a default setting for link flow control */
  11922. pf->hw.fc.requested_mode = I40E_FC_NONE;
  11923. err = i40e_init_adminq(hw);
  11924. if (err) {
  11925. if (err == I40E_ERR_FIRMWARE_API_VERSION)
  11926. dev_info(&pdev->dev,
  11927. "The driver for the device stopped because the NVM image is newer than expected. You must install the most recent version of the network driver.\n");
  11928. else
  11929. dev_info(&pdev->dev,
  11930. "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
  11931. goto err_pf_reset;
  11932. }
  11933. i40e_get_oem_version(hw);
  11934. /* provide nvm, fw, api versions */
  11935. dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s\n",
  11936. hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
  11937. hw->aq.api_maj_ver, hw->aq.api_min_ver,
  11938. i40e_nvm_version_str(hw));
  11939. if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
  11940. hw->aq.api_min_ver > I40E_FW_MINOR_VERSION(hw))
  11941. dev_info(&pdev->dev,
  11942. "The driver for the device detected a newer version of the NVM image than expected. Please install the most recent version of the network driver.\n");
  11943. else if (hw->aq.api_maj_ver == 1 && hw->aq.api_min_ver < 4)
  11944. dev_info(&pdev->dev,
  11945. "The driver for the device detected an older version of the NVM image than expected. Please update the NVM image.\n");
  11946. i40e_verify_eeprom(pf);
  11947. /* Rev 0 hardware was never productized */
  11948. if (hw->revision_id < 1)
  11949. dev_warn(&pdev->dev, "This device is a pre-production adapter/LOM. Please be aware there may be issues with your hardware. If you are experiencing problems please contact your Intel or hardware representative who provided you with this hardware.\n");
  11950. i40e_clear_pxe_mode(hw);
  11951. err = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
  11952. if (err)
  11953. goto err_adminq_setup;
  11954. err = i40e_sw_init(pf);
  11955. if (err) {
  11956. dev_info(&pdev->dev, "sw_init failed: %d\n", err);
  11957. goto err_sw_init;
  11958. }
  11959. err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
  11960. hw->func_caps.num_rx_qp, 0, 0);
  11961. if (err) {
  11962. dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
  11963. goto err_init_lan_hmc;
  11964. }
  11965. err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
  11966. if (err) {
  11967. dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
  11968. err = -ENOENT;
  11969. goto err_configure_lan_hmc;
  11970. }
  11971. /* Disable LLDP for NICs that have firmware versions lower than v4.3.
  11972. * Ignore error return codes because if it was already disabled via
  11973. * hardware settings this will fail
  11974. */
  11975. if (pf->hw_features & I40E_HW_STOP_FW_LLDP) {
  11976. dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
  11977. i40e_aq_stop_lldp(hw, true, NULL);
  11978. }
  11979. /* allow a platform config to override the HW addr */
  11980. i40e_get_platform_mac_addr(pdev, pf);
  11981. if (!is_valid_ether_addr(hw->mac.addr)) {
  11982. dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
  11983. err = -EIO;
  11984. goto err_mac_addr;
  11985. }
  11986. dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
  11987. ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
  11988. i40e_get_port_mac_addr(hw, hw->mac.port_addr);
  11989. if (is_valid_ether_addr(hw->mac.port_addr))
  11990. pf->hw_features |= I40E_HW_PORT_ID_VALID;
  11991. pci_set_drvdata(pdev, pf);
  11992. pci_save_state(pdev);
  11993. /* Enable FW to write default DCB config on link-up */
  11994. i40e_aq_set_dcb_parameters(hw, true, NULL);
  11995. #ifdef CONFIG_I40E_DCB
  11996. err = i40e_init_pf_dcb(pf);
  11997. if (err) {
  11998. dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
  11999. pf->flags &= ~(I40E_FLAG_DCB_CAPABLE | I40E_FLAG_DCB_ENABLED);
  12000. /* Continue without DCB enabled */
  12001. }
  12002. #endif /* CONFIG_I40E_DCB */
  12003. /* set up periodic task facility */
  12004. timer_setup(&pf->service_timer, i40e_service_timer, 0);
  12005. pf->service_timer_period = HZ;
  12006. INIT_WORK(&pf->service_task, i40e_service_task);
  12007. clear_bit(__I40E_SERVICE_SCHED, pf->state);
  12008. /* NVM bit on means WoL disabled for the port */
  12009. i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
  12010. if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
  12011. pf->wol_en = false;
  12012. else
  12013. pf->wol_en = true;
  12014. device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
  12015. /* set up the main switch operations */
  12016. i40e_determine_queue_usage(pf);
  12017. err = i40e_init_interrupt_scheme(pf);
  12018. if (err)
  12019. goto err_switch_setup;
  12020. /* The number of VSIs reported by the FW is the minimum guaranteed
  12021. * to us; HW supports far more and we share the remaining pool with
  12022. * the other PFs. We allocate space for more than the guarantee with
  12023. * the understanding that we might not get them all later.
  12024. */
  12025. if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
  12026. pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
  12027. else
  12028. pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
  12029. /* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
  12030. pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
  12031. GFP_KERNEL);
  12032. if (!pf->vsi) {
  12033. err = -ENOMEM;
  12034. goto err_switch_setup;
  12035. }
  12036. #ifdef CONFIG_PCI_IOV
  12037. /* prep for VF support */
  12038. if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
  12039. (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
  12040. !test_bit(__I40E_BAD_EEPROM, pf->state)) {
  12041. if (pci_num_vf(pdev))
  12042. pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
  12043. }
  12044. #endif
  12045. err = i40e_setup_pf_switch(pf, false);
  12046. if (err) {
  12047. dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
  12048. goto err_vsis;
  12049. }
  12050. INIT_LIST_HEAD(&pf->vsi[pf->lan_vsi]->ch_list);
  12051. /* Make sure flow control is set according to current settings */
  12052. err = i40e_set_fc(hw, &set_fc_aq_fail, true);
  12053. if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_GET)
  12054. dev_dbg(&pf->pdev->dev,
  12055. "Set fc with err %s aq_err %s on get_phy_cap\n",
  12056. i40e_stat_str(hw, err),
  12057. i40e_aq_str(hw, hw->aq.asq_last_status));
  12058. if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_SET)
  12059. dev_dbg(&pf->pdev->dev,
  12060. "Set fc with err %s aq_err %s on set_phy_config\n",
  12061. i40e_stat_str(hw, err),
  12062. i40e_aq_str(hw, hw->aq.asq_last_status));
  12063. if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_UPDATE)
  12064. dev_dbg(&pf->pdev->dev,
  12065. "Set fc with err %s aq_err %s on get_link_info\n",
  12066. i40e_stat_str(hw, err),
  12067. i40e_aq_str(hw, hw->aq.asq_last_status));
  12068. /* if FDIR VSI was set up, start it now */
  12069. for (i = 0; i < pf->num_alloc_vsi; i++) {
  12070. if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
  12071. i40e_vsi_open(pf->vsi[i]);
  12072. break;
  12073. }
  12074. }
  12075. /* The driver only wants link up/down and module qualification
  12076. * reports from firmware. Note the negative logic.
  12077. */
  12078. err = i40e_aq_set_phy_int_mask(&pf->hw,
  12079. ~(I40E_AQ_EVENT_LINK_UPDOWN |
  12080. I40E_AQ_EVENT_MEDIA_NA |
  12081. I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
  12082. if (err)
  12083. dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
  12084. i40e_stat_str(&pf->hw, err),
  12085. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  12086. /* Reconfigure hardware for allowing smaller MSS in the case
  12087. * of TSO, so that we avoid the MDD being fired and causing
  12088. * a reset in the case of small MSS+TSO.
  12089. */
  12090. val = rd32(hw, I40E_REG_MSS);
  12091. if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
  12092. val &= ~I40E_REG_MSS_MIN_MASK;
  12093. val |= I40E_64BYTE_MSS;
  12094. wr32(hw, I40E_REG_MSS, val);
  12095. }
  12096. if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
  12097. msleep(75);
  12098. err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
  12099. if (err)
  12100. dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
  12101. i40e_stat_str(&pf->hw, err),
  12102. i40e_aq_str(&pf->hw,
  12103. pf->hw.aq.asq_last_status));
  12104. }
  12105. /* The main driver is (mostly) up and happy. We need to set this state
  12106. * before setting up the misc vector or we get a race and the vector
  12107. * ends up disabled forever.
  12108. */
  12109. clear_bit(__I40E_DOWN, pf->state);
  12110. /* In case of MSIX we are going to setup the misc vector right here
  12111. * to handle admin queue events etc. In case of legacy and MSI
  12112. * the misc functionality and queue processing is combined in
  12113. * the same vector and that gets setup at open.
  12114. */
  12115. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  12116. err = i40e_setup_misc_vector(pf);
  12117. if (err) {
  12118. dev_info(&pdev->dev,
  12119. "setup of misc vector failed: %d\n", err);
  12120. goto err_vsis;
  12121. }
  12122. }
  12123. #ifdef CONFIG_PCI_IOV
  12124. /* prep for VF support */
  12125. if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
  12126. (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
  12127. !test_bit(__I40E_BAD_EEPROM, pf->state)) {
  12128. /* disable link interrupts for VFs */
  12129. val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
  12130. val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
  12131. wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
  12132. i40e_flush(hw);
  12133. if (pci_num_vf(pdev)) {
  12134. dev_info(&pdev->dev,
  12135. "Active VFs found, allocating resources.\n");
  12136. err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
  12137. if (err)
  12138. dev_info(&pdev->dev,
  12139. "Error %d allocating resources for existing VFs\n",
  12140. err);
  12141. }
  12142. }
  12143. #endif /* CONFIG_PCI_IOV */
  12144. if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
  12145. pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
  12146. pf->num_iwarp_msix,
  12147. I40E_IWARP_IRQ_PILE_ID);
  12148. if (pf->iwarp_base_vector < 0) {
  12149. dev_info(&pdev->dev,
  12150. "failed to get tracking for %d vectors for IWARP err=%d\n",
  12151. pf->num_iwarp_msix, pf->iwarp_base_vector);
  12152. pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
  12153. }
  12154. }
  12155. i40e_dbg_pf_init(pf);
  12156. /* tell the firmware that we're starting */
  12157. i40e_send_version(pf);
  12158. /* since everything's happy, start the service_task timer */
  12159. mod_timer(&pf->service_timer,
  12160. round_jiffies(jiffies + pf->service_timer_period));
  12161. /* add this PF to client device list and launch a client service task */
  12162. if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
  12163. err = i40e_lan_add_device(pf);
  12164. if (err)
  12165. dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
  12166. err);
  12167. }
  12168. #define PCI_SPEED_SIZE 8
  12169. #define PCI_WIDTH_SIZE 8
  12170. /* Devices on the IOSF bus do not have this information
  12171. * and will report PCI Gen 1 x 1 by default so don't bother
  12172. * checking them.
  12173. */
  12174. if (!(pf->hw_features & I40E_HW_NO_PCI_LINK_CHECK)) {
  12175. char speed[PCI_SPEED_SIZE] = "Unknown";
  12176. char width[PCI_WIDTH_SIZE] = "Unknown";
  12177. /* Get the negotiated link width and speed from PCI config
  12178. * space
  12179. */
  12180. pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
  12181. &link_status);
  12182. i40e_set_pci_config_data(hw, link_status);
  12183. switch (hw->bus.speed) {
  12184. case i40e_bus_speed_8000:
  12185. strncpy(speed, "8.0", PCI_SPEED_SIZE); break;
  12186. case i40e_bus_speed_5000:
  12187. strncpy(speed, "5.0", PCI_SPEED_SIZE); break;
  12188. case i40e_bus_speed_2500:
  12189. strncpy(speed, "2.5", PCI_SPEED_SIZE); break;
  12190. default:
  12191. break;
  12192. }
  12193. switch (hw->bus.width) {
  12194. case i40e_bus_width_pcie_x8:
  12195. strncpy(width, "8", PCI_WIDTH_SIZE); break;
  12196. case i40e_bus_width_pcie_x4:
  12197. strncpy(width, "4", PCI_WIDTH_SIZE); break;
  12198. case i40e_bus_width_pcie_x2:
  12199. strncpy(width, "2", PCI_WIDTH_SIZE); break;
  12200. case i40e_bus_width_pcie_x1:
  12201. strncpy(width, "1", PCI_WIDTH_SIZE); break;
  12202. default:
  12203. break;
  12204. }
  12205. dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
  12206. speed, width);
  12207. if (hw->bus.width < i40e_bus_width_pcie_x8 ||
  12208. hw->bus.speed < i40e_bus_speed_8000) {
  12209. dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
  12210. dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
  12211. }
  12212. }
  12213. /* get the requested speeds from the fw */
  12214. err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
  12215. if (err)
  12216. dev_dbg(&pf->pdev->dev, "get requested speeds ret = %s last_status = %s\n",
  12217. i40e_stat_str(&pf->hw, err),
  12218. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  12219. pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
  12220. /* get the supported phy types from the fw */
  12221. err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
  12222. if (err)
  12223. dev_dbg(&pf->pdev->dev, "get supported phy types ret = %s last_status = %s\n",
  12224. i40e_stat_str(&pf->hw, err),
  12225. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  12226. /* Add a filter to drop all Flow control frames from any VSI from being
  12227. * transmitted. By doing so we stop a malicious VF from sending out
  12228. * PAUSE or PFC frames and potentially controlling traffic for other
  12229. * PF/VF VSIs.
  12230. * The FW can still send Flow control frames if enabled.
  12231. */
  12232. i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
  12233. pf->main_vsi_seid);
  12234. if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
  12235. (pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
  12236. pf->hw_features |= I40E_HW_PHY_CONTROLS_LEDS;
  12237. if (pf->hw.device_id == I40E_DEV_ID_SFP_I_X722)
  12238. pf->hw_features |= I40E_HW_HAVE_CRT_RETIMER;
  12239. /* print a string summarizing features */
  12240. i40e_print_features(pf);
  12241. return 0;
  12242. /* Unwind what we've done if something failed in the setup */
  12243. err_vsis:
  12244. set_bit(__I40E_DOWN, pf->state);
  12245. i40e_clear_interrupt_scheme(pf);
  12246. kfree(pf->vsi);
  12247. err_switch_setup:
  12248. i40e_reset_interrupt_capability(pf);
  12249. del_timer_sync(&pf->service_timer);
  12250. err_mac_addr:
  12251. err_configure_lan_hmc:
  12252. (void)i40e_shutdown_lan_hmc(hw);
  12253. err_init_lan_hmc:
  12254. kfree(pf->qp_pile);
  12255. err_sw_init:
  12256. err_adminq_setup:
  12257. err_pf_reset:
  12258. iounmap(hw->hw_addr);
  12259. err_ioremap:
  12260. kfree(pf);
  12261. err_pf_alloc:
  12262. pci_disable_pcie_error_reporting(pdev);
  12263. pci_release_mem_regions(pdev);
  12264. err_pci_reg:
  12265. err_dma:
  12266. pci_disable_device(pdev);
  12267. return err;
  12268. }
  12269. /**
  12270. * i40e_remove - Device removal routine
  12271. * @pdev: PCI device information struct
  12272. *
  12273. * i40e_remove is called by the PCI subsystem to alert the driver
  12274. * that is should release a PCI device. This could be caused by a
  12275. * Hot-Plug event, or because the driver is going to be removed from
  12276. * memory.
  12277. **/
  12278. static void i40e_remove(struct pci_dev *pdev)
  12279. {
  12280. struct i40e_pf *pf = pci_get_drvdata(pdev);
  12281. struct i40e_hw *hw = &pf->hw;
  12282. i40e_status ret_code;
  12283. int i;
  12284. i40e_dbg_pf_exit(pf);
  12285. i40e_ptp_stop(pf);
  12286. /* Disable RSS in hw */
  12287. i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
  12288. i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
  12289. /* no more scheduling of any task */
  12290. set_bit(__I40E_SUSPENDED, pf->state);
  12291. set_bit(__I40E_DOWN, pf->state);
  12292. if (pf->service_timer.function)
  12293. del_timer_sync(&pf->service_timer);
  12294. if (pf->service_task.func)
  12295. cancel_work_sync(&pf->service_task);
  12296. /* Client close must be called explicitly here because the timer
  12297. * has been stopped.
  12298. */
  12299. i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
  12300. if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
  12301. i40e_free_vfs(pf);
  12302. pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
  12303. }
  12304. i40e_fdir_teardown(pf);
  12305. /* If there is a switch structure or any orphans, remove them.
  12306. * This will leave only the PF's VSI remaining.
  12307. */
  12308. for (i = 0; i < I40E_MAX_VEB; i++) {
  12309. if (!pf->veb[i])
  12310. continue;
  12311. if (pf->veb[i]->uplink_seid == pf->mac_seid ||
  12312. pf->veb[i]->uplink_seid == 0)
  12313. i40e_switch_branch_release(pf->veb[i]);
  12314. }
  12315. /* Now we can shutdown the PF's VSI, just before we kill
  12316. * adminq and hmc.
  12317. */
  12318. if (pf->vsi[pf->lan_vsi])
  12319. i40e_vsi_release(pf->vsi[pf->lan_vsi]);
  12320. i40e_cloud_filter_exit(pf);
  12321. /* remove attached clients */
  12322. if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
  12323. ret_code = i40e_lan_del_device(pf);
  12324. if (ret_code)
  12325. dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
  12326. ret_code);
  12327. }
  12328. /* shutdown and destroy the HMC */
  12329. if (hw->hmc.hmc_obj) {
  12330. ret_code = i40e_shutdown_lan_hmc(hw);
  12331. if (ret_code)
  12332. dev_warn(&pdev->dev,
  12333. "Failed to destroy the HMC resources: %d\n",
  12334. ret_code);
  12335. }
  12336. /* shutdown the adminq */
  12337. i40e_shutdown_adminq(hw);
  12338. /* destroy the locks only once, here */
  12339. mutex_destroy(&hw->aq.arq_mutex);
  12340. mutex_destroy(&hw->aq.asq_mutex);
  12341. /* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
  12342. i40e_clear_interrupt_scheme(pf);
  12343. for (i = 0; i < pf->num_alloc_vsi; i++) {
  12344. if (pf->vsi[i]) {
  12345. i40e_vsi_clear_rings(pf->vsi[i]);
  12346. i40e_vsi_clear(pf->vsi[i]);
  12347. pf->vsi[i] = NULL;
  12348. }
  12349. }
  12350. for (i = 0; i < I40E_MAX_VEB; i++) {
  12351. kfree(pf->veb[i]);
  12352. pf->veb[i] = NULL;
  12353. }
  12354. kfree(pf->qp_pile);
  12355. kfree(pf->vsi);
  12356. iounmap(hw->hw_addr);
  12357. kfree(pf);
  12358. pci_release_mem_regions(pdev);
  12359. pci_disable_pcie_error_reporting(pdev);
  12360. pci_disable_device(pdev);
  12361. }
  12362. /**
  12363. * i40e_pci_error_detected - warning that something funky happened in PCI land
  12364. * @pdev: PCI device information struct
  12365. *
  12366. * Called to warn that something happened and the error handling steps
  12367. * are in progress. Allows the driver to quiesce things, be ready for
  12368. * remediation.
  12369. **/
  12370. static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
  12371. enum pci_channel_state error)
  12372. {
  12373. struct i40e_pf *pf = pci_get_drvdata(pdev);
  12374. dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
  12375. if (!pf) {
  12376. dev_info(&pdev->dev,
  12377. "Cannot recover - error happened during device probe\n");
  12378. return PCI_ERS_RESULT_DISCONNECT;
  12379. }
  12380. /* shutdown all operations */
  12381. if (!test_bit(__I40E_SUSPENDED, pf->state))
  12382. i40e_prep_for_reset(pf, false);
  12383. /* Request a slot reset */
  12384. return PCI_ERS_RESULT_NEED_RESET;
  12385. }
  12386. /**
  12387. * i40e_pci_error_slot_reset - a PCI slot reset just happened
  12388. * @pdev: PCI device information struct
  12389. *
  12390. * Called to find if the driver can work with the device now that
  12391. * the pci slot has been reset. If a basic connection seems good
  12392. * (registers are readable and have sane content) then return a
  12393. * happy little PCI_ERS_RESULT_xxx.
  12394. **/
  12395. static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
  12396. {
  12397. struct i40e_pf *pf = pci_get_drvdata(pdev);
  12398. pci_ers_result_t result;
  12399. int err;
  12400. u32 reg;
  12401. dev_dbg(&pdev->dev, "%s\n", __func__);
  12402. if (pci_enable_device_mem(pdev)) {
  12403. dev_info(&pdev->dev,
  12404. "Cannot re-enable PCI device after reset.\n");
  12405. result = PCI_ERS_RESULT_DISCONNECT;
  12406. } else {
  12407. pci_set_master(pdev);
  12408. pci_restore_state(pdev);
  12409. pci_save_state(pdev);
  12410. pci_wake_from_d3(pdev, false);
  12411. reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
  12412. if (reg == 0)
  12413. result = PCI_ERS_RESULT_RECOVERED;
  12414. else
  12415. result = PCI_ERS_RESULT_DISCONNECT;
  12416. }
  12417. err = pci_cleanup_aer_uncorrect_error_status(pdev);
  12418. if (err) {
  12419. dev_info(&pdev->dev,
  12420. "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
  12421. err);
  12422. /* non-fatal, continue */
  12423. }
  12424. return result;
  12425. }
  12426. /**
  12427. * i40e_pci_error_reset_prepare - prepare device driver for pci reset
  12428. * @pdev: PCI device information struct
  12429. */
  12430. static void i40e_pci_error_reset_prepare(struct pci_dev *pdev)
  12431. {
  12432. struct i40e_pf *pf = pci_get_drvdata(pdev);
  12433. i40e_prep_for_reset(pf, false);
  12434. }
  12435. /**
  12436. * i40e_pci_error_reset_done - pci reset done, device driver reset can begin
  12437. * @pdev: PCI device information struct
  12438. */
  12439. static void i40e_pci_error_reset_done(struct pci_dev *pdev)
  12440. {
  12441. struct i40e_pf *pf = pci_get_drvdata(pdev);
  12442. i40e_reset_and_rebuild(pf, false, false);
  12443. }
  12444. /**
  12445. * i40e_pci_error_resume - restart operations after PCI error recovery
  12446. * @pdev: PCI device information struct
  12447. *
  12448. * Called to allow the driver to bring things back up after PCI error
  12449. * and/or reset recovery has finished.
  12450. **/
  12451. static void i40e_pci_error_resume(struct pci_dev *pdev)
  12452. {
  12453. struct i40e_pf *pf = pci_get_drvdata(pdev);
  12454. dev_dbg(&pdev->dev, "%s\n", __func__);
  12455. if (test_bit(__I40E_SUSPENDED, pf->state))
  12456. return;
  12457. i40e_handle_reset_warning(pf, false);
  12458. }
  12459. /**
  12460. * i40e_enable_mc_magic_wake - enable multicast magic packet wake up
  12461. * using the mac_address_write admin q function
  12462. * @pf: pointer to i40e_pf struct
  12463. **/
  12464. static void i40e_enable_mc_magic_wake(struct i40e_pf *pf)
  12465. {
  12466. struct i40e_hw *hw = &pf->hw;
  12467. i40e_status ret;
  12468. u8 mac_addr[6];
  12469. u16 flags = 0;
  12470. /* Get current MAC address in case it's an LAA */
  12471. if (pf->vsi[pf->lan_vsi] && pf->vsi[pf->lan_vsi]->netdev) {
  12472. ether_addr_copy(mac_addr,
  12473. pf->vsi[pf->lan_vsi]->netdev->dev_addr);
  12474. } else {
  12475. dev_err(&pf->pdev->dev,
  12476. "Failed to retrieve MAC address; using default\n");
  12477. ether_addr_copy(mac_addr, hw->mac.addr);
  12478. }
  12479. /* The FW expects the mac address write cmd to first be called with
  12480. * one of these flags before calling it again with the multicast
  12481. * enable flags.
  12482. */
  12483. flags = I40E_AQC_WRITE_TYPE_LAA_WOL;
  12484. if (hw->func_caps.flex10_enable && hw->partition_id != 1)
  12485. flags = I40E_AQC_WRITE_TYPE_LAA_ONLY;
  12486. ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
  12487. if (ret) {
  12488. dev_err(&pf->pdev->dev,
  12489. "Failed to update MAC address registers; cannot enable Multicast Magic packet wake up");
  12490. return;
  12491. }
  12492. flags = I40E_AQC_MC_MAG_EN
  12493. | I40E_AQC_WOL_PRESERVE_ON_PFR
  12494. | I40E_AQC_WRITE_TYPE_UPDATE_MC_MAG;
  12495. ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
  12496. if (ret)
  12497. dev_err(&pf->pdev->dev,
  12498. "Failed to enable Multicast Magic Packet wake up\n");
  12499. }
  12500. /**
  12501. * i40e_shutdown - PCI callback for shutting down
  12502. * @pdev: PCI device information struct
  12503. **/
  12504. static void i40e_shutdown(struct pci_dev *pdev)
  12505. {
  12506. struct i40e_pf *pf = pci_get_drvdata(pdev);
  12507. struct i40e_hw *hw = &pf->hw;
  12508. set_bit(__I40E_SUSPENDED, pf->state);
  12509. set_bit(__I40E_DOWN, pf->state);
  12510. rtnl_lock();
  12511. i40e_prep_for_reset(pf, true);
  12512. rtnl_unlock();
  12513. wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
  12514. wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
  12515. del_timer_sync(&pf->service_timer);
  12516. cancel_work_sync(&pf->service_task);
  12517. i40e_cloud_filter_exit(pf);
  12518. i40e_fdir_teardown(pf);
  12519. /* Client close must be called explicitly here because the timer
  12520. * has been stopped.
  12521. */
  12522. i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
  12523. if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
  12524. i40e_enable_mc_magic_wake(pf);
  12525. i40e_prep_for_reset(pf, false);
  12526. wr32(hw, I40E_PFPM_APM,
  12527. (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
  12528. wr32(hw, I40E_PFPM_WUFC,
  12529. (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
  12530. i40e_clear_interrupt_scheme(pf);
  12531. if (system_state == SYSTEM_POWER_OFF) {
  12532. pci_wake_from_d3(pdev, pf->wol_en);
  12533. pci_set_power_state(pdev, PCI_D3hot);
  12534. }
  12535. }
  12536. /**
  12537. * i40e_suspend - PM callback for moving to D3
  12538. * @dev: generic device information structure
  12539. **/
  12540. static int __maybe_unused i40e_suspend(struct device *dev)
  12541. {
  12542. struct pci_dev *pdev = to_pci_dev(dev);
  12543. struct i40e_pf *pf = pci_get_drvdata(pdev);
  12544. struct i40e_hw *hw = &pf->hw;
  12545. /* If we're already suspended, then there is nothing to do */
  12546. if (test_and_set_bit(__I40E_SUSPENDED, pf->state))
  12547. return 0;
  12548. set_bit(__I40E_DOWN, pf->state);
  12549. /* Ensure service task will not be running */
  12550. del_timer_sync(&pf->service_timer);
  12551. cancel_work_sync(&pf->service_task);
  12552. /* Client close must be called explicitly here because the timer
  12553. * has been stopped.
  12554. */
  12555. i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
  12556. if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
  12557. i40e_enable_mc_magic_wake(pf);
  12558. /* Since we're going to destroy queues during the
  12559. * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
  12560. * whole section
  12561. */
  12562. rtnl_lock();
  12563. i40e_prep_for_reset(pf, true);
  12564. wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
  12565. wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
  12566. /* Clear the interrupt scheme and release our IRQs so that the system
  12567. * can safely hibernate even when there are a large number of CPUs.
  12568. * Otherwise hibernation might fail when mapping all the vectors back
  12569. * to CPU0.
  12570. */
  12571. i40e_clear_interrupt_scheme(pf);
  12572. rtnl_unlock();
  12573. return 0;
  12574. }
  12575. /**
  12576. * i40e_resume - PM callback for waking up from D3
  12577. * @dev: generic device information structure
  12578. **/
  12579. static int __maybe_unused i40e_resume(struct device *dev)
  12580. {
  12581. struct pci_dev *pdev = to_pci_dev(dev);
  12582. struct i40e_pf *pf = pci_get_drvdata(pdev);
  12583. int err;
  12584. /* If we're not suspended, then there is nothing to do */
  12585. if (!test_bit(__I40E_SUSPENDED, pf->state))
  12586. return 0;
  12587. /* We need to hold the RTNL lock prior to restoring interrupt schemes,
  12588. * since we're going to be restoring queues
  12589. */
  12590. rtnl_lock();
  12591. /* We cleared the interrupt scheme when we suspended, so we need to
  12592. * restore it now to resume device functionality.
  12593. */
  12594. err = i40e_restore_interrupt_scheme(pf);
  12595. if (err) {
  12596. dev_err(&pdev->dev, "Cannot restore interrupt scheme: %d\n",
  12597. err);
  12598. }
  12599. clear_bit(__I40E_DOWN, pf->state);
  12600. i40e_reset_and_rebuild(pf, false, true);
  12601. rtnl_unlock();
  12602. /* Clear suspended state last after everything is recovered */
  12603. clear_bit(__I40E_SUSPENDED, pf->state);
  12604. /* Restart the service task */
  12605. mod_timer(&pf->service_timer,
  12606. round_jiffies(jiffies + pf->service_timer_period));
  12607. return 0;
  12608. }
  12609. static const struct pci_error_handlers i40e_err_handler = {
  12610. .error_detected = i40e_pci_error_detected,
  12611. .slot_reset = i40e_pci_error_slot_reset,
  12612. .reset_prepare = i40e_pci_error_reset_prepare,
  12613. .reset_done = i40e_pci_error_reset_done,
  12614. .resume = i40e_pci_error_resume,
  12615. };
  12616. static SIMPLE_DEV_PM_OPS(i40e_pm_ops, i40e_suspend, i40e_resume);
  12617. static struct pci_driver i40e_driver = {
  12618. .name = i40e_driver_name,
  12619. .id_table = i40e_pci_tbl,
  12620. .probe = i40e_probe,
  12621. .remove = i40e_remove,
  12622. .driver = {
  12623. .pm = &i40e_pm_ops,
  12624. },
  12625. .shutdown = i40e_shutdown,
  12626. .err_handler = &i40e_err_handler,
  12627. .sriov_configure = i40e_pci_sriov_configure,
  12628. };
  12629. /**
  12630. * i40e_init_module - Driver registration routine
  12631. *
  12632. * i40e_init_module is the first routine called when the driver is
  12633. * loaded. All it does is register with the PCI subsystem.
  12634. **/
  12635. static int __init i40e_init_module(void)
  12636. {
  12637. pr_info("%s: %s - version %s\n", i40e_driver_name,
  12638. i40e_driver_string, i40e_driver_version_str);
  12639. pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
  12640. /* There is no need to throttle the number of active tasks because
  12641. * each device limits its own task using a state bit for scheduling
  12642. * the service task, and the device tasks do not interfere with each
  12643. * other, so we don't set a max task limit. We must set WQ_MEM_RECLAIM
  12644. * since we need to be able to guarantee forward progress even under
  12645. * memory pressure.
  12646. */
  12647. i40e_wq = alloc_workqueue("%s", WQ_MEM_RECLAIM, 0, i40e_driver_name);
  12648. if (!i40e_wq) {
  12649. pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
  12650. return -ENOMEM;
  12651. }
  12652. i40e_dbg_init();
  12653. return pci_register_driver(&i40e_driver);
  12654. }
  12655. module_init(i40e_init_module);
  12656. /**
  12657. * i40e_exit_module - Driver exit cleanup routine
  12658. *
  12659. * i40e_exit_module is called just before the driver is removed
  12660. * from memory.
  12661. **/
  12662. static void __exit i40e_exit_module(void)
  12663. {
  12664. pci_unregister_driver(&i40e_driver);
  12665. destroy_workqueue(i40e_wq);
  12666. i40e_dbg_exit();
  12667. }
  12668. module_exit(i40e_exit_module);