i40e_main.c 400 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright(c) 2013 - 2018 Intel Corporation. */
  3. #include <linux/etherdevice.h>
  4. #include <linux/of_net.h>
  5. #include <linux/pci.h>
  6. #include <linux/bpf.h>
  7. /* Local includes */
  8. #include "i40e.h"
  9. #include "i40e_diag.h"
  10. #include <net/udp_tunnel.h>
  11. /* All i40e tracepoints are defined by the include below, which
  12. * must be included exactly once across the whole kernel with
  13. * CREATE_TRACE_POINTS defined
  14. */
  15. #define CREATE_TRACE_POINTS
  16. #include "i40e_trace.h"
  17. const char i40e_driver_name[] = "i40e";
  18. static const char i40e_driver_string[] =
  19. "Intel(R) Ethernet Connection XL710 Network Driver";
  20. #define DRV_KERN "-k"
  21. #define DRV_VERSION_MAJOR 2
  22. #define DRV_VERSION_MINOR 3
  23. #define DRV_VERSION_BUILD 2
  24. #define DRV_VERSION __stringify(DRV_VERSION_MAJOR) "." \
  25. __stringify(DRV_VERSION_MINOR) "." \
  26. __stringify(DRV_VERSION_BUILD) DRV_KERN
  27. const char i40e_driver_version_str[] = DRV_VERSION;
  28. static const char i40e_copyright[] = "Copyright (c) 2013 - 2014 Intel Corporation.";
  29. /* a bit of forward declarations */
  30. static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi);
  31. static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired);
  32. static int i40e_add_vsi(struct i40e_vsi *vsi);
  33. static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi);
  34. static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit);
  35. static int i40e_setup_misc_vector(struct i40e_pf *pf);
  36. static void i40e_determine_queue_usage(struct i40e_pf *pf);
  37. static int i40e_setup_pf_filter_control(struct i40e_pf *pf);
  38. static void i40e_prep_for_reset(struct i40e_pf *pf, bool lock_acquired);
  39. static int i40e_reset(struct i40e_pf *pf);
  40. static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired);
  41. static void i40e_fdir_sb_setup(struct i40e_pf *pf);
  42. static int i40e_veb_get_bw_info(struct i40e_veb *veb);
  43. static int i40e_get_capabilities(struct i40e_pf *pf,
  44. enum i40e_admin_queue_opc list_type);
  45. /* i40e_pci_tbl - PCI Device ID Table
  46. *
  47. * Last entry must be all 0s
  48. *
  49. * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
  50. * Class, Class Mask, private data (not used) }
  51. */
  52. static const struct pci_device_id i40e_pci_tbl[] = {
  53. {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_XL710), 0},
  54. {PCI_VDEVICE(INTEL, I40E_DEV_ID_QEMU), 0},
  55. {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_B), 0},
  56. {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_C), 0},
  57. {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_A), 0},
  58. {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_B), 0},
  59. {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_C), 0},
  60. {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T), 0},
  61. {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T4), 0},
  62. {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_X722), 0},
  63. {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_X722), 0},
  64. {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_X722), 0},
  65. {PCI_VDEVICE(INTEL, I40E_DEV_ID_1G_BASE_T_X722), 0},
  66. {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_X722), 0},
  67. {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_I_X722), 0},
  68. {PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2), 0},
  69. {PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2_A), 0},
  70. {PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_B), 0},
  71. {PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_SFP28), 0},
  72. /* required last entry */
  73. {0, }
  74. };
  75. MODULE_DEVICE_TABLE(pci, i40e_pci_tbl);
  76. #define I40E_MAX_VF_COUNT 128
  77. static int debug = -1;
  78. module_param(debug, uint, 0);
  79. MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all), Debug mask (0x8XXXXXXX)");
  80. MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
  81. MODULE_DESCRIPTION("Intel(R) Ethernet Connection XL710 Network Driver");
  82. MODULE_LICENSE("GPL");
  83. MODULE_VERSION(DRV_VERSION);
  84. static struct workqueue_struct *i40e_wq;
  85. /**
  86. * i40e_allocate_dma_mem_d - OS specific memory alloc for shared code
  87. * @hw: pointer to the HW structure
  88. * @mem: ptr to mem struct to fill out
  89. * @size: size of memory requested
  90. * @alignment: what to align the allocation to
  91. **/
  92. int i40e_allocate_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem,
  93. u64 size, u32 alignment)
  94. {
  95. struct i40e_pf *pf = (struct i40e_pf *)hw->back;
  96. mem->size = ALIGN(size, alignment);
  97. mem->va = dma_zalloc_coherent(&pf->pdev->dev, mem->size,
  98. &mem->pa, GFP_KERNEL);
  99. if (!mem->va)
  100. return -ENOMEM;
  101. return 0;
  102. }
  103. /**
  104. * i40e_free_dma_mem_d - OS specific memory free for shared code
  105. * @hw: pointer to the HW structure
  106. * @mem: ptr to mem struct to free
  107. **/
  108. int i40e_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
  109. {
  110. struct i40e_pf *pf = (struct i40e_pf *)hw->back;
  111. dma_free_coherent(&pf->pdev->dev, mem->size, mem->va, mem->pa);
  112. mem->va = NULL;
  113. mem->pa = 0;
  114. mem->size = 0;
  115. return 0;
  116. }
  117. /**
  118. * i40e_allocate_virt_mem_d - OS specific memory alloc for shared code
  119. * @hw: pointer to the HW structure
  120. * @mem: ptr to mem struct to fill out
  121. * @size: size of memory requested
  122. **/
  123. int i40e_allocate_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem,
  124. u32 size)
  125. {
  126. mem->size = size;
  127. mem->va = kzalloc(size, GFP_KERNEL);
  128. if (!mem->va)
  129. return -ENOMEM;
  130. return 0;
  131. }
  132. /**
  133. * i40e_free_virt_mem_d - OS specific memory free for shared code
  134. * @hw: pointer to the HW structure
  135. * @mem: ptr to mem struct to free
  136. **/
  137. int i40e_free_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem)
  138. {
  139. /* it's ok to kfree a NULL pointer */
  140. kfree(mem->va);
  141. mem->va = NULL;
  142. mem->size = 0;
  143. return 0;
  144. }
  145. /**
  146. * i40e_get_lump - find a lump of free generic resource
  147. * @pf: board private structure
  148. * @pile: the pile of resource to search
  149. * @needed: the number of items needed
  150. * @id: an owner id to stick on the items assigned
  151. *
  152. * Returns the base item index of the lump, or negative for error
  153. *
  154. * The search_hint trick and lack of advanced fit-finding only work
  155. * because we're highly likely to have all the same size lump requests.
  156. * Linear search time and any fragmentation should be minimal.
  157. **/
  158. static int i40e_get_lump(struct i40e_pf *pf, struct i40e_lump_tracking *pile,
  159. u16 needed, u16 id)
  160. {
  161. int ret = -ENOMEM;
  162. int i, j;
  163. if (!pile || needed == 0 || id >= I40E_PILE_VALID_BIT) {
  164. dev_info(&pf->pdev->dev,
  165. "param err: pile=%s needed=%d id=0x%04x\n",
  166. pile ? "<valid>" : "<null>", needed, id);
  167. return -EINVAL;
  168. }
  169. /* start the linear search with an imperfect hint */
  170. i = pile->search_hint;
  171. while (i < pile->num_entries) {
  172. /* skip already allocated entries */
  173. if (pile->list[i] & I40E_PILE_VALID_BIT) {
  174. i++;
  175. continue;
  176. }
  177. /* do we have enough in this lump? */
  178. for (j = 0; (j < needed) && ((i+j) < pile->num_entries); j++) {
  179. if (pile->list[i+j] & I40E_PILE_VALID_BIT)
  180. break;
  181. }
  182. if (j == needed) {
  183. /* there was enough, so assign it to the requestor */
  184. for (j = 0; j < needed; j++)
  185. pile->list[i+j] = id | I40E_PILE_VALID_BIT;
  186. ret = i;
  187. pile->search_hint = i + j;
  188. break;
  189. }
  190. /* not enough, so skip over it and continue looking */
  191. i += j;
  192. }
  193. return ret;
  194. }
  195. /**
  196. * i40e_put_lump - return a lump of generic resource
  197. * @pile: the pile of resource to search
  198. * @index: the base item index
  199. * @id: the owner id of the items assigned
  200. *
  201. * Returns the count of items in the lump
  202. **/
  203. static int i40e_put_lump(struct i40e_lump_tracking *pile, u16 index, u16 id)
  204. {
  205. int valid_id = (id | I40E_PILE_VALID_BIT);
  206. int count = 0;
  207. int i;
  208. if (!pile || index >= pile->num_entries)
  209. return -EINVAL;
  210. for (i = index;
  211. i < pile->num_entries && pile->list[i] == valid_id;
  212. i++) {
  213. pile->list[i] = 0;
  214. count++;
  215. }
  216. if (count && index < pile->search_hint)
  217. pile->search_hint = index;
  218. return count;
  219. }
  220. /**
  221. * i40e_find_vsi_from_id - searches for the vsi with the given id
  222. * @pf: the pf structure to search for the vsi
  223. * @id: id of the vsi it is searching for
  224. **/
  225. struct i40e_vsi *i40e_find_vsi_from_id(struct i40e_pf *pf, u16 id)
  226. {
  227. int i;
  228. for (i = 0; i < pf->num_alloc_vsi; i++)
  229. if (pf->vsi[i] && (pf->vsi[i]->id == id))
  230. return pf->vsi[i];
  231. return NULL;
  232. }
  233. /**
  234. * i40e_service_event_schedule - Schedule the service task to wake up
  235. * @pf: board private structure
  236. *
  237. * If not already scheduled, this puts the task into the work queue
  238. **/
  239. void i40e_service_event_schedule(struct i40e_pf *pf)
  240. {
  241. if (!test_bit(__I40E_DOWN, pf->state) &&
  242. !test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
  243. queue_work(i40e_wq, &pf->service_task);
  244. }
  245. /**
  246. * i40e_tx_timeout - Respond to a Tx Hang
  247. * @netdev: network interface device structure
  248. *
  249. * If any port has noticed a Tx timeout, it is likely that the whole
  250. * device is munged, not just the one netdev port, so go for the full
  251. * reset.
  252. **/
  253. static void i40e_tx_timeout(struct net_device *netdev)
  254. {
  255. struct i40e_netdev_priv *np = netdev_priv(netdev);
  256. struct i40e_vsi *vsi = np->vsi;
  257. struct i40e_pf *pf = vsi->back;
  258. struct i40e_ring *tx_ring = NULL;
  259. unsigned int i, hung_queue = 0;
  260. u32 head, val;
  261. pf->tx_timeout_count++;
  262. /* find the stopped queue the same way the stack does */
  263. for (i = 0; i < netdev->num_tx_queues; i++) {
  264. struct netdev_queue *q;
  265. unsigned long trans_start;
  266. q = netdev_get_tx_queue(netdev, i);
  267. trans_start = q->trans_start;
  268. if (netif_xmit_stopped(q) &&
  269. time_after(jiffies,
  270. (trans_start + netdev->watchdog_timeo))) {
  271. hung_queue = i;
  272. break;
  273. }
  274. }
  275. if (i == netdev->num_tx_queues) {
  276. netdev_info(netdev, "tx_timeout: no netdev hung queue found\n");
  277. } else {
  278. /* now that we have an index, find the tx_ring struct */
  279. for (i = 0; i < vsi->num_queue_pairs; i++) {
  280. if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
  281. if (hung_queue ==
  282. vsi->tx_rings[i]->queue_index) {
  283. tx_ring = vsi->tx_rings[i];
  284. break;
  285. }
  286. }
  287. }
  288. }
  289. if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ*20)))
  290. pf->tx_timeout_recovery_level = 1; /* reset after some time */
  291. else if (time_before(jiffies,
  292. (pf->tx_timeout_last_recovery + netdev->watchdog_timeo)))
  293. return; /* don't do any new action before the next timeout */
  294. if (tx_ring) {
  295. head = i40e_get_head(tx_ring);
  296. /* Read interrupt register */
  297. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  298. val = rd32(&pf->hw,
  299. I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
  300. tx_ring->vsi->base_vector - 1));
  301. else
  302. val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
  303. 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",
  304. vsi->seid, hung_queue, tx_ring->next_to_clean,
  305. head, tx_ring->next_to_use,
  306. readl(tx_ring->tail), val);
  307. }
  308. pf->tx_timeout_last_recovery = jiffies;
  309. netdev_info(netdev, "tx_timeout recovery level %d, hung_queue %d\n",
  310. pf->tx_timeout_recovery_level, hung_queue);
  311. switch (pf->tx_timeout_recovery_level) {
  312. case 1:
  313. set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
  314. break;
  315. case 2:
  316. set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
  317. break;
  318. case 3:
  319. set_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
  320. break;
  321. default:
  322. netdev_err(netdev, "tx_timeout recovery unsuccessful\n");
  323. break;
  324. }
  325. i40e_service_event_schedule(pf);
  326. pf->tx_timeout_recovery_level++;
  327. }
  328. /**
  329. * i40e_get_vsi_stats_struct - Get System Network Statistics
  330. * @vsi: the VSI we care about
  331. *
  332. * Returns the address of the device statistics structure.
  333. * The statistics are actually updated from the service task.
  334. **/
  335. struct rtnl_link_stats64 *i40e_get_vsi_stats_struct(struct i40e_vsi *vsi)
  336. {
  337. return &vsi->net_stats;
  338. }
  339. /**
  340. * i40e_get_netdev_stats_struct_tx - populate stats from a Tx ring
  341. * @ring: Tx ring to get statistics from
  342. * @stats: statistics entry to be updated
  343. **/
  344. static void i40e_get_netdev_stats_struct_tx(struct i40e_ring *ring,
  345. struct rtnl_link_stats64 *stats)
  346. {
  347. u64 bytes, packets;
  348. unsigned int start;
  349. do {
  350. start = u64_stats_fetch_begin_irq(&ring->syncp);
  351. packets = ring->stats.packets;
  352. bytes = ring->stats.bytes;
  353. } while (u64_stats_fetch_retry_irq(&ring->syncp, start));
  354. stats->tx_packets += packets;
  355. stats->tx_bytes += bytes;
  356. }
  357. /**
  358. * i40e_get_netdev_stats_struct - Get statistics for netdev interface
  359. * @netdev: network interface device structure
  360. * @stats: data structure to store statistics
  361. *
  362. * Returns the address of the device statistics structure.
  363. * The statistics are actually updated from the service task.
  364. **/
  365. static void i40e_get_netdev_stats_struct(struct net_device *netdev,
  366. struct rtnl_link_stats64 *stats)
  367. {
  368. struct i40e_netdev_priv *np = netdev_priv(netdev);
  369. struct i40e_ring *tx_ring, *rx_ring;
  370. struct i40e_vsi *vsi = np->vsi;
  371. struct rtnl_link_stats64 *vsi_stats = i40e_get_vsi_stats_struct(vsi);
  372. int i;
  373. if (test_bit(__I40E_VSI_DOWN, vsi->state))
  374. return;
  375. if (!vsi->tx_rings)
  376. return;
  377. rcu_read_lock();
  378. for (i = 0; i < vsi->num_queue_pairs; i++) {
  379. u64 bytes, packets;
  380. unsigned int start;
  381. tx_ring = READ_ONCE(vsi->tx_rings[i]);
  382. if (!tx_ring)
  383. continue;
  384. i40e_get_netdev_stats_struct_tx(tx_ring, stats);
  385. rx_ring = &tx_ring[1];
  386. do {
  387. start = u64_stats_fetch_begin_irq(&rx_ring->syncp);
  388. packets = rx_ring->stats.packets;
  389. bytes = rx_ring->stats.bytes;
  390. } while (u64_stats_fetch_retry_irq(&rx_ring->syncp, start));
  391. stats->rx_packets += packets;
  392. stats->rx_bytes += bytes;
  393. if (i40e_enabled_xdp_vsi(vsi))
  394. i40e_get_netdev_stats_struct_tx(&rx_ring[1], stats);
  395. }
  396. rcu_read_unlock();
  397. /* following stats updated by i40e_watchdog_subtask() */
  398. stats->multicast = vsi_stats->multicast;
  399. stats->tx_errors = vsi_stats->tx_errors;
  400. stats->tx_dropped = vsi_stats->tx_dropped;
  401. stats->rx_errors = vsi_stats->rx_errors;
  402. stats->rx_dropped = vsi_stats->rx_dropped;
  403. stats->rx_crc_errors = vsi_stats->rx_crc_errors;
  404. stats->rx_length_errors = vsi_stats->rx_length_errors;
  405. }
  406. /**
  407. * i40e_vsi_reset_stats - Resets all stats of the given vsi
  408. * @vsi: the VSI to have its stats reset
  409. **/
  410. void i40e_vsi_reset_stats(struct i40e_vsi *vsi)
  411. {
  412. struct rtnl_link_stats64 *ns;
  413. int i;
  414. if (!vsi)
  415. return;
  416. ns = i40e_get_vsi_stats_struct(vsi);
  417. memset(ns, 0, sizeof(*ns));
  418. memset(&vsi->net_stats_offsets, 0, sizeof(vsi->net_stats_offsets));
  419. memset(&vsi->eth_stats, 0, sizeof(vsi->eth_stats));
  420. memset(&vsi->eth_stats_offsets, 0, sizeof(vsi->eth_stats_offsets));
  421. if (vsi->rx_rings && vsi->rx_rings[0]) {
  422. for (i = 0; i < vsi->num_queue_pairs; i++) {
  423. memset(&vsi->rx_rings[i]->stats, 0,
  424. sizeof(vsi->rx_rings[i]->stats));
  425. memset(&vsi->rx_rings[i]->rx_stats, 0,
  426. sizeof(vsi->rx_rings[i]->rx_stats));
  427. memset(&vsi->tx_rings[i]->stats, 0,
  428. sizeof(vsi->tx_rings[i]->stats));
  429. memset(&vsi->tx_rings[i]->tx_stats, 0,
  430. sizeof(vsi->tx_rings[i]->tx_stats));
  431. }
  432. }
  433. vsi->stat_offsets_loaded = false;
  434. }
  435. /**
  436. * i40e_pf_reset_stats - Reset all of the stats for the given PF
  437. * @pf: the PF to be reset
  438. **/
  439. void i40e_pf_reset_stats(struct i40e_pf *pf)
  440. {
  441. int i;
  442. memset(&pf->stats, 0, sizeof(pf->stats));
  443. memset(&pf->stats_offsets, 0, sizeof(pf->stats_offsets));
  444. pf->stat_offsets_loaded = false;
  445. for (i = 0; i < I40E_MAX_VEB; i++) {
  446. if (pf->veb[i]) {
  447. memset(&pf->veb[i]->stats, 0,
  448. sizeof(pf->veb[i]->stats));
  449. memset(&pf->veb[i]->stats_offsets, 0,
  450. sizeof(pf->veb[i]->stats_offsets));
  451. pf->veb[i]->stat_offsets_loaded = false;
  452. }
  453. }
  454. pf->hw_csum_rx_error = 0;
  455. }
  456. /**
  457. * i40e_stat_update48 - read and update a 48 bit stat from the chip
  458. * @hw: ptr to the hardware info
  459. * @hireg: the high 32 bit reg to read
  460. * @loreg: the low 32 bit reg to read
  461. * @offset_loaded: has the initial offset been loaded yet
  462. * @offset: ptr to current offset value
  463. * @stat: ptr to the stat
  464. *
  465. * Since the device stats are not reset at PFReset, they likely will not
  466. * be zeroed when the driver starts. We'll save the first values read
  467. * and use them as offsets to be subtracted from the raw values in order
  468. * to report stats that count from zero. In the process, we also manage
  469. * the potential roll-over.
  470. **/
  471. static void i40e_stat_update48(struct i40e_hw *hw, u32 hireg, u32 loreg,
  472. bool offset_loaded, u64 *offset, u64 *stat)
  473. {
  474. u64 new_data;
  475. if (hw->device_id == I40E_DEV_ID_QEMU) {
  476. new_data = rd32(hw, loreg);
  477. new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
  478. } else {
  479. new_data = rd64(hw, loreg);
  480. }
  481. if (!offset_loaded)
  482. *offset = new_data;
  483. if (likely(new_data >= *offset))
  484. *stat = new_data - *offset;
  485. else
  486. *stat = (new_data + BIT_ULL(48)) - *offset;
  487. *stat &= 0xFFFFFFFFFFFFULL;
  488. }
  489. /**
  490. * i40e_stat_update32 - read and update a 32 bit stat from the chip
  491. * @hw: ptr to the hardware info
  492. * @reg: the hw reg to read
  493. * @offset_loaded: has the initial offset been loaded yet
  494. * @offset: ptr to current offset value
  495. * @stat: ptr to the stat
  496. **/
  497. static void i40e_stat_update32(struct i40e_hw *hw, u32 reg,
  498. bool offset_loaded, u64 *offset, u64 *stat)
  499. {
  500. u32 new_data;
  501. new_data = rd32(hw, reg);
  502. if (!offset_loaded)
  503. *offset = new_data;
  504. if (likely(new_data >= *offset))
  505. *stat = (u32)(new_data - *offset);
  506. else
  507. *stat = (u32)((new_data + BIT_ULL(32)) - *offset);
  508. }
  509. /**
  510. * i40e_stat_update_and_clear32 - read and clear hw reg, update a 32 bit stat
  511. * @hw: ptr to the hardware info
  512. * @reg: the hw reg to read and clear
  513. * @stat: ptr to the stat
  514. **/
  515. static void i40e_stat_update_and_clear32(struct i40e_hw *hw, u32 reg, u64 *stat)
  516. {
  517. u32 new_data = rd32(hw, reg);
  518. wr32(hw, reg, 1); /* must write a nonzero value to clear register */
  519. *stat += new_data;
  520. }
  521. /**
  522. * i40e_update_eth_stats - Update VSI-specific ethernet statistics counters.
  523. * @vsi: the VSI to be updated
  524. **/
  525. void i40e_update_eth_stats(struct i40e_vsi *vsi)
  526. {
  527. int stat_idx = le16_to_cpu(vsi->info.stat_counter_idx);
  528. struct i40e_pf *pf = vsi->back;
  529. struct i40e_hw *hw = &pf->hw;
  530. struct i40e_eth_stats *oes;
  531. struct i40e_eth_stats *es; /* device's eth stats */
  532. es = &vsi->eth_stats;
  533. oes = &vsi->eth_stats_offsets;
  534. /* Gather up the stats that the hw collects */
  535. i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
  536. vsi->stat_offsets_loaded,
  537. &oes->tx_errors, &es->tx_errors);
  538. i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx),
  539. vsi->stat_offsets_loaded,
  540. &oes->rx_discards, &es->rx_discards);
  541. i40e_stat_update32(hw, I40E_GLV_RUPP(stat_idx),
  542. vsi->stat_offsets_loaded,
  543. &oes->rx_unknown_protocol, &es->rx_unknown_protocol);
  544. i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
  545. vsi->stat_offsets_loaded,
  546. &oes->tx_errors, &es->tx_errors);
  547. i40e_stat_update48(hw, I40E_GLV_GORCH(stat_idx),
  548. I40E_GLV_GORCL(stat_idx),
  549. vsi->stat_offsets_loaded,
  550. &oes->rx_bytes, &es->rx_bytes);
  551. i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
  552. I40E_GLV_UPRCL(stat_idx),
  553. vsi->stat_offsets_loaded,
  554. &oes->rx_unicast, &es->rx_unicast);
  555. i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
  556. I40E_GLV_MPRCL(stat_idx),
  557. vsi->stat_offsets_loaded,
  558. &oes->rx_multicast, &es->rx_multicast);
  559. i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
  560. I40E_GLV_BPRCL(stat_idx),
  561. vsi->stat_offsets_loaded,
  562. &oes->rx_broadcast, &es->rx_broadcast);
  563. i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
  564. I40E_GLV_GOTCL(stat_idx),
  565. vsi->stat_offsets_loaded,
  566. &oes->tx_bytes, &es->tx_bytes);
  567. i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
  568. I40E_GLV_UPTCL(stat_idx),
  569. vsi->stat_offsets_loaded,
  570. &oes->tx_unicast, &es->tx_unicast);
  571. i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
  572. I40E_GLV_MPTCL(stat_idx),
  573. vsi->stat_offsets_loaded,
  574. &oes->tx_multicast, &es->tx_multicast);
  575. i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
  576. I40E_GLV_BPTCL(stat_idx),
  577. vsi->stat_offsets_loaded,
  578. &oes->tx_broadcast, &es->tx_broadcast);
  579. vsi->stat_offsets_loaded = true;
  580. }
  581. /**
  582. * i40e_update_veb_stats - Update Switch component statistics
  583. * @veb: the VEB being updated
  584. **/
  585. static void i40e_update_veb_stats(struct i40e_veb *veb)
  586. {
  587. struct i40e_pf *pf = veb->pf;
  588. struct i40e_hw *hw = &pf->hw;
  589. struct i40e_eth_stats *oes;
  590. struct i40e_eth_stats *es; /* device's eth stats */
  591. struct i40e_veb_tc_stats *veb_oes;
  592. struct i40e_veb_tc_stats *veb_es;
  593. int i, idx = 0;
  594. idx = veb->stats_idx;
  595. es = &veb->stats;
  596. oes = &veb->stats_offsets;
  597. veb_es = &veb->tc_stats;
  598. veb_oes = &veb->tc_stats_offsets;
  599. /* Gather up the stats that the hw collects */
  600. i40e_stat_update32(hw, I40E_GLSW_TDPC(idx),
  601. veb->stat_offsets_loaded,
  602. &oes->tx_discards, &es->tx_discards);
  603. if (hw->revision_id > 0)
  604. i40e_stat_update32(hw, I40E_GLSW_RUPP(idx),
  605. veb->stat_offsets_loaded,
  606. &oes->rx_unknown_protocol,
  607. &es->rx_unknown_protocol);
  608. i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx),
  609. veb->stat_offsets_loaded,
  610. &oes->rx_bytes, &es->rx_bytes);
  611. i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx),
  612. veb->stat_offsets_loaded,
  613. &oes->rx_unicast, &es->rx_unicast);
  614. i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx),
  615. veb->stat_offsets_loaded,
  616. &oes->rx_multicast, &es->rx_multicast);
  617. i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx),
  618. veb->stat_offsets_loaded,
  619. &oes->rx_broadcast, &es->rx_broadcast);
  620. i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx),
  621. veb->stat_offsets_loaded,
  622. &oes->tx_bytes, &es->tx_bytes);
  623. i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx),
  624. veb->stat_offsets_loaded,
  625. &oes->tx_unicast, &es->tx_unicast);
  626. i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx),
  627. veb->stat_offsets_loaded,
  628. &oes->tx_multicast, &es->tx_multicast);
  629. i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx),
  630. veb->stat_offsets_loaded,
  631. &oes->tx_broadcast, &es->tx_broadcast);
  632. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  633. i40e_stat_update48(hw, I40E_GLVEBTC_RPCH(i, idx),
  634. I40E_GLVEBTC_RPCL(i, idx),
  635. veb->stat_offsets_loaded,
  636. &veb_oes->tc_rx_packets[i],
  637. &veb_es->tc_rx_packets[i]);
  638. i40e_stat_update48(hw, I40E_GLVEBTC_RBCH(i, idx),
  639. I40E_GLVEBTC_RBCL(i, idx),
  640. veb->stat_offsets_loaded,
  641. &veb_oes->tc_rx_bytes[i],
  642. &veb_es->tc_rx_bytes[i]);
  643. i40e_stat_update48(hw, I40E_GLVEBTC_TPCH(i, idx),
  644. I40E_GLVEBTC_TPCL(i, idx),
  645. veb->stat_offsets_loaded,
  646. &veb_oes->tc_tx_packets[i],
  647. &veb_es->tc_tx_packets[i]);
  648. i40e_stat_update48(hw, I40E_GLVEBTC_TBCH(i, idx),
  649. I40E_GLVEBTC_TBCL(i, idx),
  650. veb->stat_offsets_loaded,
  651. &veb_oes->tc_tx_bytes[i],
  652. &veb_es->tc_tx_bytes[i]);
  653. }
  654. veb->stat_offsets_loaded = true;
  655. }
  656. /**
  657. * i40e_update_vsi_stats - Update the vsi statistics counters.
  658. * @vsi: the VSI to be updated
  659. *
  660. * There are a few instances where we store the same stat in a
  661. * couple of different structs. This is partly because we have
  662. * the netdev stats that need to be filled out, which is slightly
  663. * different from the "eth_stats" defined by the chip and used in
  664. * VF communications. We sort it out here.
  665. **/
  666. static void i40e_update_vsi_stats(struct i40e_vsi *vsi)
  667. {
  668. struct i40e_pf *pf = vsi->back;
  669. struct rtnl_link_stats64 *ons;
  670. struct rtnl_link_stats64 *ns; /* netdev stats */
  671. struct i40e_eth_stats *oes;
  672. struct i40e_eth_stats *es; /* device's eth stats */
  673. u32 tx_restart, tx_busy;
  674. struct i40e_ring *p;
  675. u32 rx_page, rx_buf;
  676. u64 bytes, packets;
  677. unsigned int start;
  678. u64 tx_linearize;
  679. u64 tx_force_wb;
  680. u64 rx_p, rx_b;
  681. u64 tx_p, tx_b;
  682. u16 q;
  683. if (test_bit(__I40E_VSI_DOWN, vsi->state) ||
  684. test_bit(__I40E_CONFIG_BUSY, pf->state))
  685. return;
  686. ns = i40e_get_vsi_stats_struct(vsi);
  687. ons = &vsi->net_stats_offsets;
  688. es = &vsi->eth_stats;
  689. oes = &vsi->eth_stats_offsets;
  690. /* Gather up the netdev and vsi stats that the driver collects
  691. * on the fly during packet processing
  692. */
  693. rx_b = rx_p = 0;
  694. tx_b = tx_p = 0;
  695. tx_restart = tx_busy = tx_linearize = tx_force_wb = 0;
  696. rx_page = 0;
  697. rx_buf = 0;
  698. rcu_read_lock();
  699. for (q = 0; q < vsi->num_queue_pairs; q++) {
  700. /* locate Tx ring */
  701. p = READ_ONCE(vsi->tx_rings[q]);
  702. do {
  703. start = u64_stats_fetch_begin_irq(&p->syncp);
  704. packets = p->stats.packets;
  705. bytes = p->stats.bytes;
  706. } while (u64_stats_fetch_retry_irq(&p->syncp, start));
  707. tx_b += bytes;
  708. tx_p += packets;
  709. tx_restart += p->tx_stats.restart_queue;
  710. tx_busy += p->tx_stats.tx_busy;
  711. tx_linearize += p->tx_stats.tx_linearize;
  712. tx_force_wb += p->tx_stats.tx_force_wb;
  713. /* Rx queue is part of the same block as Tx queue */
  714. p = &p[1];
  715. do {
  716. start = u64_stats_fetch_begin_irq(&p->syncp);
  717. packets = p->stats.packets;
  718. bytes = p->stats.bytes;
  719. } while (u64_stats_fetch_retry_irq(&p->syncp, start));
  720. rx_b += bytes;
  721. rx_p += packets;
  722. rx_buf += p->rx_stats.alloc_buff_failed;
  723. rx_page += p->rx_stats.alloc_page_failed;
  724. }
  725. rcu_read_unlock();
  726. vsi->tx_restart = tx_restart;
  727. vsi->tx_busy = tx_busy;
  728. vsi->tx_linearize = tx_linearize;
  729. vsi->tx_force_wb = tx_force_wb;
  730. vsi->rx_page_failed = rx_page;
  731. vsi->rx_buf_failed = rx_buf;
  732. ns->rx_packets = rx_p;
  733. ns->rx_bytes = rx_b;
  734. ns->tx_packets = tx_p;
  735. ns->tx_bytes = tx_b;
  736. /* update netdev stats from eth stats */
  737. i40e_update_eth_stats(vsi);
  738. ons->tx_errors = oes->tx_errors;
  739. ns->tx_errors = es->tx_errors;
  740. ons->multicast = oes->rx_multicast;
  741. ns->multicast = es->rx_multicast;
  742. ons->rx_dropped = oes->rx_discards;
  743. ns->rx_dropped = es->rx_discards;
  744. ons->tx_dropped = oes->tx_discards;
  745. ns->tx_dropped = es->tx_discards;
  746. /* pull in a couple PF stats if this is the main vsi */
  747. if (vsi == pf->vsi[pf->lan_vsi]) {
  748. ns->rx_crc_errors = pf->stats.crc_errors;
  749. ns->rx_errors = pf->stats.crc_errors + pf->stats.illegal_bytes;
  750. ns->rx_length_errors = pf->stats.rx_length_errors;
  751. }
  752. }
  753. /**
  754. * i40e_update_pf_stats - Update the PF statistics counters.
  755. * @pf: the PF to be updated
  756. **/
  757. static void i40e_update_pf_stats(struct i40e_pf *pf)
  758. {
  759. struct i40e_hw_port_stats *osd = &pf->stats_offsets;
  760. struct i40e_hw_port_stats *nsd = &pf->stats;
  761. struct i40e_hw *hw = &pf->hw;
  762. u32 val;
  763. int i;
  764. i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
  765. I40E_GLPRT_GORCL(hw->port),
  766. pf->stat_offsets_loaded,
  767. &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
  768. i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
  769. I40E_GLPRT_GOTCL(hw->port),
  770. pf->stat_offsets_loaded,
  771. &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
  772. i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
  773. pf->stat_offsets_loaded,
  774. &osd->eth.rx_discards,
  775. &nsd->eth.rx_discards);
  776. i40e_stat_update48(hw, I40E_GLPRT_UPRCH(hw->port),
  777. I40E_GLPRT_UPRCL(hw->port),
  778. pf->stat_offsets_loaded,
  779. &osd->eth.rx_unicast,
  780. &nsd->eth.rx_unicast);
  781. i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
  782. I40E_GLPRT_MPRCL(hw->port),
  783. pf->stat_offsets_loaded,
  784. &osd->eth.rx_multicast,
  785. &nsd->eth.rx_multicast);
  786. i40e_stat_update48(hw, I40E_GLPRT_BPRCH(hw->port),
  787. I40E_GLPRT_BPRCL(hw->port),
  788. pf->stat_offsets_loaded,
  789. &osd->eth.rx_broadcast,
  790. &nsd->eth.rx_broadcast);
  791. i40e_stat_update48(hw, I40E_GLPRT_UPTCH(hw->port),
  792. I40E_GLPRT_UPTCL(hw->port),
  793. pf->stat_offsets_loaded,
  794. &osd->eth.tx_unicast,
  795. &nsd->eth.tx_unicast);
  796. i40e_stat_update48(hw, I40E_GLPRT_MPTCH(hw->port),
  797. I40E_GLPRT_MPTCL(hw->port),
  798. pf->stat_offsets_loaded,
  799. &osd->eth.tx_multicast,
  800. &nsd->eth.tx_multicast);
  801. i40e_stat_update48(hw, I40E_GLPRT_BPTCH(hw->port),
  802. I40E_GLPRT_BPTCL(hw->port),
  803. pf->stat_offsets_loaded,
  804. &osd->eth.tx_broadcast,
  805. &nsd->eth.tx_broadcast);
  806. i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
  807. pf->stat_offsets_loaded,
  808. &osd->tx_dropped_link_down,
  809. &nsd->tx_dropped_link_down);
  810. i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
  811. pf->stat_offsets_loaded,
  812. &osd->crc_errors, &nsd->crc_errors);
  813. i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
  814. pf->stat_offsets_loaded,
  815. &osd->illegal_bytes, &nsd->illegal_bytes);
  816. i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
  817. pf->stat_offsets_loaded,
  818. &osd->mac_local_faults,
  819. &nsd->mac_local_faults);
  820. i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
  821. pf->stat_offsets_loaded,
  822. &osd->mac_remote_faults,
  823. &nsd->mac_remote_faults);
  824. i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
  825. pf->stat_offsets_loaded,
  826. &osd->rx_length_errors,
  827. &nsd->rx_length_errors);
  828. i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
  829. pf->stat_offsets_loaded,
  830. &osd->link_xon_rx, &nsd->link_xon_rx);
  831. i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
  832. pf->stat_offsets_loaded,
  833. &osd->link_xon_tx, &nsd->link_xon_tx);
  834. i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
  835. pf->stat_offsets_loaded,
  836. &osd->link_xoff_rx, &nsd->link_xoff_rx);
  837. i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
  838. pf->stat_offsets_loaded,
  839. &osd->link_xoff_tx, &nsd->link_xoff_tx);
  840. for (i = 0; i < 8; i++) {
  841. i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
  842. pf->stat_offsets_loaded,
  843. &osd->priority_xoff_rx[i],
  844. &nsd->priority_xoff_rx[i]);
  845. i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
  846. pf->stat_offsets_loaded,
  847. &osd->priority_xon_rx[i],
  848. &nsd->priority_xon_rx[i]);
  849. i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
  850. pf->stat_offsets_loaded,
  851. &osd->priority_xon_tx[i],
  852. &nsd->priority_xon_tx[i]);
  853. i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
  854. pf->stat_offsets_loaded,
  855. &osd->priority_xoff_tx[i],
  856. &nsd->priority_xoff_tx[i]);
  857. i40e_stat_update32(hw,
  858. I40E_GLPRT_RXON2OFFCNT(hw->port, i),
  859. pf->stat_offsets_loaded,
  860. &osd->priority_xon_2_xoff[i],
  861. &nsd->priority_xon_2_xoff[i]);
  862. }
  863. i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
  864. I40E_GLPRT_PRC64L(hw->port),
  865. pf->stat_offsets_loaded,
  866. &osd->rx_size_64, &nsd->rx_size_64);
  867. i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
  868. I40E_GLPRT_PRC127L(hw->port),
  869. pf->stat_offsets_loaded,
  870. &osd->rx_size_127, &nsd->rx_size_127);
  871. i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
  872. I40E_GLPRT_PRC255L(hw->port),
  873. pf->stat_offsets_loaded,
  874. &osd->rx_size_255, &nsd->rx_size_255);
  875. i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
  876. I40E_GLPRT_PRC511L(hw->port),
  877. pf->stat_offsets_loaded,
  878. &osd->rx_size_511, &nsd->rx_size_511);
  879. i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
  880. I40E_GLPRT_PRC1023L(hw->port),
  881. pf->stat_offsets_loaded,
  882. &osd->rx_size_1023, &nsd->rx_size_1023);
  883. i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
  884. I40E_GLPRT_PRC1522L(hw->port),
  885. pf->stat_offsets_loaded,
  886. &osd->rx_size_1522, &nsd->rx_size_1522);
  887. i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
  888. I40E_GLPRT_PRC9522L(hw->port),
  889. pf->stat_offsets_loaded,
  890. &osd->rx_size_big, &nsd->rx_size_big);
  891. i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
  892. I40E_GLPRT_PTC64L(hw->port),
  893. pf->stat_offsets_loaded,
  894. &osd->tx_size_64, &nsd->tx_size_64);
  895. i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
  896. I40E_GLPRT_PTC127L(hw->port),
  897. pf->stat_offsets_loaded,
  898. &osd->tx_size_127, &nsd->tx_size_127);
  899. i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
  900. I40E_GLPRT_PTC255L(hw->port),
  901. pf->stat_offsets_loaded,
  902. &osd->tx_size_255, &nsd->tx_size_255);
  903. i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
  904. I40E_GLPRT_PTC511L(hw->port),
  905. pf->stat_offsets_loaded,
  906. &osd->tx_size_511, &nsd->tx_size_511);
  907. i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
  908. I40E_GLPRT_PTC1023L(hw->port),
  909. pf->stat_offsets_loaded,
  910. &osd->tx_size_1023, &nsd->tx_size_1023);
  911. i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
  912. I40E_GLPRT_PTC1522L(hw->port),
  913. pf->stat_offsets_loaded,
  914. &osd->tx_size_1522, &nsd->tx_size_1522);
  915. i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
  916. I40E_GLPRT_PTC9522L(hw->port),
  917. pf->stat_offsets_loaded,
  918. &osd->tx_size_big, &nsd->tx_size_big);
  919. i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
  920. pf->stat_offsets_loaded,
  921. &osd->rx_undersize, &nsd->rx_undersize);
  922. i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
  923. pf->stat_offsets_loaded,
  924. &osd->rx_fragments, &nsd->rx_fragments);
  925. i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
  926. pf->stat_offsets_loaded,
  927. &osd->rx_oversize, &nsd->rx_oversize);
  928. i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
  929. pf->stat_offsets_loaded,
  930. &osd->rx_jabber, &nsd->rx_jabber);
  931. /* FDIR stats */
  932. i40e_stat_update_and_clear32(hw,
  933. I40E_GLQF_PCNT(I40E_FD_ATR_STAT_IDX(hw->pf_id)),
  934. &nsd->fd_atr_match);
  935. i40e_stat_update_and_clear32(hw,
  936. I40E_GLQF_PCNT(I40E_FD_SB_STAT_IDX(hw->pf_id)),
  937. &nsd->fd_sb_match);
  938. i40e_stat_update_and_clear32(hw,
  939. I40E_GLQF_PCNT(I40E_FD_ATR_TUNNEL_STAT_IDX(hw->pf_id)),
  940. &nsd->fd_atr_tunnel_match);
  941. val = rd32(hw, I40E_PRTPM_EEE_STAT);
  942. nsd->tx_lpi_status =
  943. (val & I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_MASK) >>
  944. I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_SHIFT;
  945. nsd->rx_lpi_status =
  946. (val & I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_MASK) >>
  947. I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_SHIFT;
  948. i40e_stat_update32(hw, I40E_PRTPM_TLPIC,
  949. pf->stat_offsets_loaded,
  950. &osd->tx_lpi_count, &nsd->tx_lpi_count);
  951. i40e_stat_update32(hw, I40E_PRTPM_RLPIC,
  952. pf->stat_offsets_loaded,
  953. &osd->rx_lpi_count, &nsd->rx_lpi_count);
  954. if (pf->flags & I40E_FLAG_FD_SB_ENABLED &&
  955. !test_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
  956. nsd->fd_sb_status = true;
  957. else
  958. nsd->fd_sb_status = false;
  959. if (pf->flags & I40E_FLAG_FD_ATR_ENABLED &&
  960. !test_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
  961. nsd->fd_atr_status = true;
  962. else
  963. nsd->fd_atr_status = false;
  964. pf->stat_offsets_loaded = true;
  965. }
  966. /**
  967. * i40e_update_stats - Update the various statistics counters.
  968. * @vsi: the VSI to be updated
  969. *
  970. * Update the various stats for this VSI and its related entities.
  971. **/
  972. void i40e_update_stats(struct i40e_vsi *vsi)
  973. {
  974. struct i40e_pf *pf = vsi->back;
  975. if (vsi == pf->vsi[pf->lan_vsi])
  976. i40e_update_pf_stats(pf);
  977. i40e_update_vsi_stats(vsi);
  978. }
  979. /**
  980. * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
  981. * @vsi: the VSI to be searched
  982. * @macaddr: the MAC address
  983. * @vlan: the vlan
  984. *
  985. * Returns ptr to the filter object or NULL
  986. **/
  987. static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
  988. const u8 *macaddr, s16 vlan)
  989. {
  990. struct i40e_mac_filter *f;
  991. u64 key;
  992. if (!vsi || !macaddr)
  993. return NULL;
  994. key = i40e_addr_to_hkey(macaddr);
  995. hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
  996. if ((ether_addr_equal(macaddr, f->macaddr)) &&
  997. (vlan == f->vlan))
  998. return f;
  999. }
  1000. return NULL;
  1001. }
  1002. /**
  1003. * i40e_find_mac - Find a mac addr in the macvlan filters list
  1004. * @vsi: the VSI to be searched
  1005. * @macaddr: the MAC address we are searching for
  1006. *
  1007. * Returns the first filter with the provided MAC address or NULL if
  1008. * MAC address was not found
  1009. **/
  1010. struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, const u8 *macaddr)
  1011. {
  1012. struct i40e_mac_filter *f;
  1013. u64 key;
  1014. if (!vsi || !macaddr)
  1015. return NULL;
  1016. key = i40e_addr_to_hkey(macaddr);
  1017. hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
  1018. if ((ether_addr_equal(macaddr, f->macaddr)))
  1019. return f;
  1020. }
  1021. return NULL;
  1022. }
  1023. /**
  1024. * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
  1025. * @vsi: the VSI to be searched
  1026. *
  1027. * Returns true if VSI is in vlan mode or false otherwise
  1028. **/
  1029. bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
  1030. {
  1031. /* If we have a PVID, always operate in VLAN mode */
  1032. if (vsi->info.pvid)
  1033. return true;
  1034. /* We need to operate in VLAN mode whenever we have any filters with
  1035. * a VLAN other than I40E_VLAN_ALL. We could check the table each
  1036. * time, incurring search cost repeatedly. However, we can notice two
  1037. * things:
  1038. *
  1039. * 1) the only place where we can gain a VLAN filter is in
  1040. * i40e_add_filter.
  1041. *
  1042. * 2) the only place where filters are actually removed is in
  1043. * i40e_sync_filters_subtask.
  1044. *
  1045. * Thus, we can simply use a boolean value, has_vlan_filters which we
  1046. * will set to true when we add a VLAN filter in i40e_add_filter. Then
  1047. * we have to perform the full search after deleting filters in
  1048. * i40e_sync_filters_subtask, but we already have to search
  1049. * filters here and can perform the check at the same time. This
  1050. * results in avoiding embedding a loop for VLAN mode inside another
  1051. * loop over all the filters, and should maintain correctness as noted
  1052. * above.
  1053. */
  1054. return vsi->has_vlan_filter;
  1055. }
  1056. /**
  1057. * i40e_correct_mac_vlan_filters - Correct non-VLAN filters if necessary
  1058. * @vsi: the VSI to configure
  1059. * @tmp_add_list: list of filters ready to be added
  1060. * @tmp_del_list: list of filters ready to be deleted
  1061. * @vlan_filters: the number of active VLAN filters
  1062. *
  1063. * Update VLAN=0 and VLAN=-1 (I40E_VLAN_ANY) filters properly so that they
  1064. * behave as expected. If we have any active VLAN filters remaining or about
  1065. * to be added then we need to update non-VLAN filters to be marked as VLAN=0
  1066. * so that they only match against untagged traffic. If we no longer have any
  1067. * active VLAN filters, we need to make all non-VLAN filters marked as VLAN=-1
  1068. * so that they match against both tagged and untagged traffic. In this way,
  1069. * we ensure that we correctly receive the desired traffic. This ensures that
  1070. * when we have an active VLAN we will receive only untagged traffic and
  1071. * traffic matching active VLANs. If we have no active VLANs then we will
  1072. * operate in non-VLAN mode and receive all traffic, tagged or untagged.
  1073. *
  1074. * Finally, in a similar fashion, this function also corrects filters when
  1075. * there is an active PVID assigned to this VSI.
  1076. *
  1077. * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
  1078. *
  1079. * This function is only expected to be called from within
  1080. * i40e_sync_vsi_filters.
  1081. *
  1082. * NOTE: This function expects to be called while under the
  1083. * mac_filter_hash_lock
  1084. */
  1085. static int i40e_correct_mac_vlan_filters(struct i40e_vsi *vsi,
  1086. struct hlist_head *tmp_add_list,
  1087. struct hlist_head *tmp_del_list,
  1088. int vlan_filters)
  1089. {
  1090. s16 pvid = le16_to_cpu(vsi->info.pvid);
  1091. struct i40e_mac_filter *f, *add_head;
  1092. struct i40e_new_mac_filter *new;
  1093. struct hlist_node *h;
  1094. int bkt, new_vlan;
  1095. /* To determine if a particular filter needs to be replaced we
  1096. * have the three following conditions:
  1097. *
  1098. * a) if we have a PVID assigned, then all filters which are
  1099. * not marked as VLAN=PVID must be replaced with filters that
  1100. * are.
  1101. * b) otherwise, if we have any active VLANS, all filters
  1102. * which are marked as VLAN=-1 must be replaced with
  1103. * filters marked as VLAN=0
  1104. * c) finally, if we do not have any active VLANS, all filters
  1105. * which are marked as VLAN=0 must be replaced with filters
  1106. * marked as VLAN=-1
  1107. */
  1108. /* Update the filters about to be added in place */
  1109. hlist_for_each_entry(new, tmp_add_list, hlist) {
  1110. if (pvid && new->f->vlan != pvid)
  1111. new->f->vlan = pvid;
  1112. else if (vlan_filters && new->f->vlan == I40E_VLAN_ANY)
  1113. new->f->vlan = 0;
  1114. else if (!vlan_filters && new->f->vlan == 0)
  1115. new->f->vlan = I40E_VLAN_ANY;
  1116. }
  1117. /* Update the remaining active filters */
  1118. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  1119. /* Combine the checks for whether a filter needs to be changed
  1120. * and then determine the new VLAN inside the if block, in
  1121. * order to avoid duplicating code for adding the new filter
  1122. * then deleting the old filter.
  1123. */
  1124. if ((pvid && f->vlan != pvid) ||
  1125. (vlan_filters && f->vlan == I40E_VLAN_ANY) ||
  1126. (!vlan_filters && f->vlan == 0)) {
  1127. /* Determine the new vlan we will be adding */
  1128. if (pvid)
  1129. new_vlan = pvid;
  1130. else if (vlan_filters)
  1131. new_vlan = 0;
  1132. else
  1133. new_vlan = I40E_VLAN_ANY;
  1134. /* Create the new filter */
  1135. add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
  1136. if (!add_head)
  1137. return -ENOMEM;
  1138. /* Create a temporary i40e_new_mac_filter */
  1139. new = kzalloc(sizeof(*new), GFP_ATOMIC);
  1140. if (!new)
  1141. return -ENOMEM;
  1142. new->f = add_head;
  1143. new->state = add_head->state;
  1144. /* Add the new filter to the tmp list */
  1145. hlist_add_head(&new->hlist, tmp_add_list);
  1146. /* Put the original filter into the delete list */
  1147. f->state = I40E_FILTER_REMOVE;
  1148. hash_del(&f->hlist);
  1149. hlist_add_head(&f->hlist, tmp_del_list);
  1150. }
  1151. }
  1152. vsi->has_vlan_filter = !!vlan_filters;
  1153. return 0;
  1154. }
  1155. /**
  1156. * i40e_rm_default_mac_filter - Remove the default MAC filter set by NVM
  1157. * @vsi: the PF Main VSI - inappropriate for any other VSI
  1158. * @macaddr: the MAC address
  1159. *
  1160. * Remove whatever filter the firmware set up so the driver can manage
  1161. * its own filtering intelligently.
  1162. **/
  1163. static void i40e_rm_default_mac_filter(struct i40e_vsi *vsi, u8 *macaddr)
  1164. {
  1165. struct i40e_aqc_remove_macvlan_element_data element;
  1166. struct i40e_pf *pf = vsi->back;
  1167. /* Only appropriate for the PF main VSI */
  1168. if (vsi->type != I40E_VSI_MAIN)
  1169. return;
  1170. memset(&element, 0, sizeof(element));
  1171. ether_addr_copy(element.mac_addr, macaddr);
  1172. element.vlan_tag = 0;
  1173. /* Ignore error returns, some firmware does it this way... */
  1174. element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
  1175. i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
  1176. memset(&element, 0, sizeof(element));
  1177. ether_addr_copy(element.mac_addr, macaddr);
  1178. element.vlan_tag = 0;
  1179. /* ...and some firmware does it this way. */
  1180. element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH |
  1181. I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
  1182. i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
  1183. }
  1184. /**
  1185. * i40e_add_filter - Add a mac/vlan filter to the VSI
  1186. * @vsi: the VSI to be searched
  1187. * @macaddr: the MAC address
  1188. * @vlan: the vlan
  1189. *
  1190. * Returns ptr to the filter object or NULL when no memory available.
  1191. *
  1192. * NOTE: This function is expected to be called with mac_filter_hash_lock
  1193. * being held.
  1194. **/
  1195. struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
  1196. const u8 *macaddr, s16 vlan)
  1197. {
  1198. struct i40e_mac_filter *f;
  1199. u64 key;
  1200. if (!vsi || !macaddr)
  1201. return NULL;
  1202. f = i40e_find_filter(vsi, macaddr, vlan);
  1203. if (!f) {
  1204. f = kzalloc(sizeof(*f), GFP_ATOMIC);
  1205. if (!f)
  1206. return NULL;
  1207. /* Update the boolean indicating if we need to function in
  1208. * VLAN mode.
  1209. */
  1210. if (vlan >= 0)
  1211. vsi->has_vlan_filter = true;
  1212. ether_addr_copy(f->macaddr, macaddr);
  1213. f->vlan = vlan;
  1214. f->state = I40E_FILTER_NEW;
  1215. INIT_HLIST_NODE(&f->hlist);
  1216. key = i40e_addr_to_hkey(macaddr);
  1217. hash_add(vsi->mac_filter_hash, &f->hlist, key);
  1218. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  1219. set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
  1220. }
  1221. /* If we're asked to add a filter that has been marked for removal, it
  1222. * is safe to simply restore it to active state. __i40e_del_filter
  1223. * will have simply deleted any filters which were previously marked
  1224. * NEW or FAILED, so if it is currently marked REMOVE it must have
  1225. * previously been ACTIVE. Since we haven't yet run the sync filters
  1226. * task, just restore this filter to the ACTIVE state so that the
  1227. * sync task leaves it in place
  1228. */
  1229. if (f->state == I40E_FILTER_REMOVE)
  1230. f->state = I40E_FILTER_ACTIVE;
  1231. return f;
  1232. }
  1233. /**
  1234. * __i40e_del_filter - Remove a specific filter from the VSI
  1235. * @vsi: VSI to remove from
  1236. * @f: the filter to remove from the list
  1237. *
  1238. * This function should be called instead of i40e_del_filter only if you know
  1239. * the exact filter you will remove already, such as via i40e_find_filter or
  1240. * i40e_find_mac.
  1241. *
  1242. * NOTE: This function is expected to be called with mac_filter_hash_lock
  1243. * being held.
  1244. * ANOTHER NOTE: This function MUST be called from within the context of
  1245. * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
  1246. * instead of list_for_each_entry().
  1247. **/
  1248. void __i40e_del_filter(struct i40e_vsi *vsi, struct i40e_mac_filter *f)
  1249. {
  1250. if (!f)
  1251. return;
  1252. /* If the filter was never added to firmware then we can just delete it
  1253. * directly and we don't want to set the status to remove or else an
  1254. * admin queue command will unnecessarily fire.
  1255. */
  1256. if ((f->state == I40E_FILTER_FAILED) ||
  1257. (f->state == I40E_FILTER_NEW)) {
  1258. hash_del(&f->hlist);
  1259. kfree(f);
  1260. } else {
  1261. f->state = I40E_FILTER_REMOVE;
  1262. }
  1263. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  1264. set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->state);
  1265. }
  1266. /**
  1267. * i40e_del_filter - Remove a MAC/VLAN filter from the VSI
  1268. * @vsi: the VSI to be searched
  1269. * @macaddr: the MAC address
  1270. * @vlan: the VLAN
  1271. *
  1272. * NOTE: This function is expected to be called with mac_filter_hash_lock
  1273. * being held.
  1274. * ANOTHER NOTE: This function MUST be called from within the context of
  1275. * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
  1276. * instead of list_for_each_entry().
  1277. **/
  1278. void i40e_del_filter(struct i40e_vsi *vsi, const u8 *macaddr, s16 vlan)
  1279. {
  1280. struct i40e_mac_filter *f;
  1281. if (!vsi || !macaddr)
  1282. return;
  1283. f = i40e_find_filter(vsi, macaddr, vlan);
  1284. __i40e_del_filter(vsi, f);
  1285. }
  1286. /**
  1287. * i40e_add_mac_filter - Add a MAC filter for all active VLANs
  1288. * @vsi: the VSI to be searched
  1289. * @macaddr: the mac address to be filtered
  1290. *
  1291. * If we're not in VLAN mode, just add the filter to I40E_VLAN_ANY. Otherwise,
  1292. * go through all the macvlan filters and add a macvlan filter for each
  1293. * unique vlan that already exists. If a PVID has been assigned, instead only
  1294. * add the macaddr to that VLAN.
  1295. *
  1296. * Returns last filter added on success, else NULL
  1297. **/
  1298. struct i40e_mac_filter *i40e_add_mac_filter(struct i40e_vsi *vsi,
  1299. const u8 *macaddr)
  1300. {
  1301. struct i40e_mac_filter *f, *add = NULL;
  1302. struct hlist_node *h;
  1303. int bkt;
  1304. if (vsi->info.pvid)
  1305. return i40e_add_filter(vsi, macaddr,
  1306. le16_to_cpu(vsi->info.pvid));
  1307. if (!i40e_is_vsi_in_vlan(vsi))
  1308. return i40e_add_filter(vsi, macaddr, I40E_VLAN_ANY);
  1309. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  1310. if (f->state == I40E_FILTER_REMOVE)
  1311. continue;
  1312. add = i40e_add_filter(vsi, macaddr, f->vlan);
  1313. if (!add)
  1314. return NULL;
  1315. }
  1316. return add;
  1317. }
  1318. /**
  1319. * i40e_del_mac_filter - Remove a MAC filter from all VLANs
  1320. * @vsi: the VSI to be searched
  1321. * @macaddr: the mac address to be removed
  1322. *
  1323. * Removes a given MAC address from a VSI regardless of what VLAN it has been
  1324. * associated with.
  1325. *
  1326. * Returns 0 for success, or error
  1327. **/
  1328. int i40e_del_mac_filter(struct i40e_vsi *vsi, const u8 *macaddr)
  1329. {
  1330. struct i40e_mac_filter *f;
  1331. struct hlist_node *h;
  1332. bool found = false;
  1333. int bkt;
  1334. WARN(!spin_is_locked(&vsi->mac_filter_hash_lock),
  1335. "Missing mac_filter_hash_lock\n");
  1336. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  1337. if (ether_addr_equal(macaddr, f->macaddr)) {
  1338. __i40e_del_filter(vsi, f);
  1339. found = true;
  1340. }
  1341. }
  1342. if (found)
  1343. return 0;
  1344. else
  1345. return -ENOENT;
  1346. }
  1347. /**
  1348. * i40e_set_mac - NDO callback to set mac address
  1349. * @netdev: network interface device structure
  1350. * @p: pointer to an address structure
  1351. *
  1352. * Returns 0 on success, negative on failure
  1353. **/
  1354. static int i40e_set_mac(struct net_device *netdev, void *p)
  1355. {
  1356. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1357. struct i40e_vsi *vsi = np->vsi;
  1358. struct i40e_pf *pf = vsi->back;
  1359. struct i40e_hw *hw = &pf->hw;
  1360. struct sockaddr *addr = p;
  1361. if (!is_valid_ether_addr(addr->sa_data))
  1362. return -EADDRNOTAVAIL;
  1363. if (ether_addr_equal(netdev->dev_addr, addr->sa_data)) {
  1364. netdev_info(netdev, "already using mac address %pM\n",
  1365. addr->sa_data);
  1366. return 0;
  1367. }
  1368. if (test_bit(__I40E_VSI_DOWN, vsi->back->state) ||
  1369. test_bit(__I40E_RESET_RECOVERY_PENDING, vsi->back->state))
  1370. return -EADDRNOTAVAIL;
  1371. if (ether_addr_equal(hw->mac.addr, addr->sa_data))
  1372. netdev_info(netdev, "returning to hw mac address %pM\n",
  1373. hw->mac.addr);
  1374. else
  1375. netdev_info(netdev, "set new mac address %pM\n", addr->sa_data);
  1376. /* Copy the address first, so that we avoid a possible race with
  1377. * .set_rx_mode(). If we copy after changing the address in the filter
  1378. * list, we might open ourselves to a narrow race window where
  1379. * .set_rx_mode could delete our dev_addr filter and prevent traffic
  1380. * from passing.
  1381. */
  1382. ether_addr_copy(netdev->dev_addr, addr->sa_data);
  1383. spin_lock_bh(&vsi->mac_filter_hash_lock);
  1384. i40e_del_mac_filter(vsi, netdev->dev_addr);
  1385. i40e_add_mac_filter(vsi, addr->sa_data);
  1386. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  1387. if (vsi->type == I40E_VSI_MAIN) {
  1388. i40e_status ret;
  1389. ret = i40e_aq_mac_address_write(&vsi->back->hw,
  1390. I40E_AQC_WRITE_TYPE_LAA_WOL,
  1391. addr->sa_data, NULL);
  1392. if (ret)
  1393. netdev_info(netdev, "Ignoring error from firmware on LAA update, status %s, AQ ret %s\n",
  1394. i40e_stat_str(hw, ret),
  1395. i40e_aq_str(hw, hw->aq.asq_last_status));
  1396. }
  1397. /* schedule our worker thread which will take care of
  1398. * applying the new filter changes
  1399. */
  1400. i40e_service_event_schedule(vsi->back);
  1401. return 0;
  1402. }
  1403. /**
  1404. * i40e_config_rss_aq - Prepare for RSS using AQ commands
  1405. * @vsi: vsi structure
  1406. * @seed: RSS hash seed
  1407. **/
  1408. static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
  1409. u8 *lut, u16 lut_size)
  1410. {
  1411. struct i40e_pf *pf = vsi->back;
  1412. struct i40e_hw *hw = &pf->hw;
  1413. int ret = 0;
  1414. if (seed) {
  1415. struct i40e_aqc_get_set_rss_key_data *seed_dw =
  1416. (struct i40e_aqc_get_set_rss_key_data *)seed;
  1417. ret = i40e_aq_set_rss_key(hw, vsi->id, seed_dw);
  1418. if (ret) {
  1419. dev_info(&pf->pdev->dev,
  1420. "Cannot set RSS key, err %s aq_err %s\n",
  1421. i40e_stat_str(hw, ret),
  1422. i40e_aq_str(hw, hw->aq.asq_last_status));
  1423. return ret;
  1424. }
  1425. }
  1426. if (lut) {
  1427. bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
  1428. ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
  1429. if (ret) {
  1430. dev_info(&pf->pdev->dev,
  1431. "Cannot set RSS lut, err %s aq_err %s\n",
  1432. i40e_stat_str(hw, ret),
  1433. i40e_aq_str(hw, hw->aq.asq_last_status));
  1434. return ret;
  1435. }
  1436. }
  1437. return ret;
  1438. }
  1439. /**
  1440. * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
  1441. * @vsi: VSI structure
  1442. **/
  1443. static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
  1444. {
  1445. struct i40e_pf *pf = vsi->back;
  1446. u8 seed[I40E_HKEY_ARRAY_SIZE];
  1447. u8 *lut;
  1448. int ret;
  1449. if (!(pf->hw_features & I40E_HW_RSS_AQ_CAPABLE))
  1450. return 0;
  1451. if (!vsi->rss_size)
  1452. vsi->rss_size = min_t(int, pf->alloc_rss_size,
  1453. vsi->num_queue_pairs);
  1454. if (!vsi->rss_size)
  1455. return -EINVAL;
  1456. lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
  1457. if (!lut)
  1458. return -ENOMEM;
  1459. /* Use the user configured hash keys and lookup table if there is one,
  1460. * otherwise use default
  1461. */
  1462. if (vsi->rss_lut_user)
  1463. memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
  1464. else
  1465. i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
  1466. if (vsi->rss_hkey_user)
  1467. memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
  1468. else
  1469. netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
  1470. ret = i40e_config_rss_aq(vsi, seed, lut, vsi->rss_table_size);
  1471. kfree(lut);
  1472. return ret;
  1473. }
  1474. /**
  1475. * i40e_vsi_setup_queue_map_mqprio - Prepares mqprio based tc_config
  1476. * @vsi: the VSI being configured,
  1477. * @ctxt: VSI context structure
  1478. * @enabled_tc: number of traffic classes to enable
  1479. *
  1480. * Prepares VSI tc_config to have queue configurations based on MQPRIO options.
  1481. **/
  1482. static int i40e_vsi_setup_queue_map_mqprio(struct i40e_vsi *vsi,
  1483. struct i40e_vsi_context *ctxt,
  1484. u8 enabled_tc)
  1485. {
  1486. u16 qcount = 0, max_qcount, qmap, sections = 0;
  1487. int i, override_q, pow, num_qps, ret;
  1488. u8 netdev_tc = 0, offset = 0;
  1489. if (vsi->type != I40E_VSI_MAIN)
  1490. return -EINVAL;
  1491. sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
  1492. sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
  1493. vsi->tc_config.numtc = vsi->mqprio_qopt.qopt.num_tc;
  1494. vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
  1495. num_qps = vsi->mqprio_qopt.qopt.count[0];
  1496. /* find the next higher power-of-2 of num queue pairs */
  1497. pow = ilog2(num_qps);
  1498. if (!is_power_of_2(num_qps))
  1499. pow++;
  1500. qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
  1501. (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
  1502. /* Setup queue offset/count for all TCs for given VSI */
  1503. max_qcount = vsi->mqprio_qopt.qopt.count[0];
  1504. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  1505. /* See if the given TC is enabled for the given VSI */
  1506. if (vsi->tc_config.enabled_tc & BIT(i)) {
  1507. offset = vsi->mqprio_qopt.qopt.offset[i];
  1508. qcount = vsi->mqprio_qopt.qopt.count[i];
  1509. if (qcount > max_qcount)
  1510. max_qcount = qcount;
  1511. vsi->tc_config.tc_info[i].qoffset = offset;
  1512. vsi->tc_config.tc_info[i].qcount = qcount;
  1513. vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
  1514. } else {
  1515. /* TC is not enabled so set the offset to
  1516. * default queue and allocate one queue
  1517. * for the given TC.
  1518. */
  1519. vsi->tc_config.tc_info[i].qoffset = 0;
  1520. vsi->tc_config.tc_info[i].qcount = 1;
  1521. vsi->tc_config.tc_info[i].netdev_tc = 0;
  1522. }
  1523. }
  1524. /* Set actual Tx/Rx queue pairs */
  1525. vsi->num_queue_pairs = offset + qcount;
  1526. /* Setup queue TC[0].qmap for given VSI context */
  1527. ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
  1528. ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
  1529. ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
  1530. ctxt->info.valid_sections |= cpu_to_le16(sections);
  1531. /* Reconfigure RSS for main VSI with max queue count */
  1532. vsi->rss_size = max_qcount;
  1533. ret = i40e_vsi_config_rss(vsi);
  1534. if (ret) {
  1535. dev_info(&vsi->back->pdev->dev,
  1536. "Failed to reconfig rss for num_queues (%u)\n",
  1537. max_qcount);
  1538. return ret;
  1539. }
  1540. vsi->reconfig_rss = true;
  1541. dev_dbg(&vsi->back->pdev->dev,
  1542. "Reconfigured rss with num_queues (%u)\n", max_qcount);
  1543. /* Find queue count available for channel VSIs and starting offset
  1544. * for channel VSIs
  1545. */
  1546. override_q = vsi->mqprio_qopt.qopt.count[0];
  1547. if (override_q && override_q < vsi->num_queue_pairs) {
  1548. vsi->cnt_q_avail = vsi->num_queue_pairs - override_q;
  1549. vsi->next_base_queue = override_q;
  1550. }
  1551. return 0;
  1552. }
  1553. /**
  1554. * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
  1555. * @vsi: the VSI being setup
  1556. * @ctxt: VSI context structure
  1557. * @enabled_tc: Enabled TCs bitmap
  1558. * @is_add: True if called before Add VSI
  1559. *
  1560. * Setup VSI queue mapping for enabled traffic classes.
  1561. **/
  1562. static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
  1563. struct i40e_vsi_context *ctxt,
  1564. u8 enabled_tc,
  1565. bool is_add)
  1566. {
  1567. struct i40e_pf *pf = vsi->back;
  1568. u16 sections = 0;
  1569. u8 netdev_tc = 0;
  1570. u16 numtc = 1;
  1571. u16 qcount;
  1572. u8 offset;
  1573. u16 qmap;
  1574. int i;
  1575. u16 num_tc_qps = 0;
  1576. sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
  1577. offset = 0;
  1578. /* Number of queues per enabled TC */
  1579. num_tc_qps = vsi->alloc_queue_pairs;
  1580. if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
  1581. /* Find numtc from enabled TC bitmap */
  1582. for (i = 0, numtc = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  1583. if (enabled_tc & BIT(i)) /* TC is enabled */
  1584. numtc++;
  1585. }
  1586. if (!numtc) {
  1587. dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
  1588. numtc = 1;
  1589. }
  1590. num_tc_qps = num_tc_qps / numtc;
  1591. num_tc_qps = min_t(int, num_tc_qps,
  1592. i40e_pf_get_max_q_per_tc(pf));
  1593. }
  1594. vsi->tc_config.numtc = numtc;
  1595. vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
  1596. /* Do not allow use more TC queue pairs than MSI-X vectors exist */
  1597. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  1598. num_tc_qps = min_t(int, num_tc_qps, pf->num_lan_msix);
  1599. /* Setup queue offset/count for all TCs for given VSI */
  1600. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  1601. /* See if the given TC is enabled for the given VSI */
  1602. if (vsi->tc_config.enabled_tc & BIT(i)) {
  1603. /* TC is enabled */
  1604. int pow, num_qps;
  1605. switch (vsi->type) {
  1606. case I40E_VSI_MAIN:
  1607. if (!(pf->flags & (I40E_FLAG_FD_SB_ENABLED |
  1608. I40E_FLAG_FD_ATR_ENABLED)) ||
  1609. vsi->tc_config.enabled_tc != 1) {
  1610. qcount = min_t(int, pf->alloc_rss_size,
  1611. num_tc_qps);
  1612. break;
  1613. }
  1614. case I40E_VSI_FDIR:
  1615. case I40E_VSI_SRIOV:
  1616. case I40E_VSI_VMDQ2:
  1617. default:
  1618. qcount = num_tc_qps;
  1619. WARN_ON(i != 0);
  1620. break;
  1621. }
  1622. vsi->tc_config.tc_info[i].qoffset = offset;
  1623. vsi->tc_config.tc_info[i].qcount = qcount;
  1624. /* find the next higher power-of-2 of num queue pairs */
  1625. num_qps = qcount;
  1626. pow = 0;
  1627. while (num_qps && (BIT_ULL(pow) < qcount)) {
  1628. pow++;
  1629. num_qps >>= 1;
  1630. }
  1631. vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
  1632. qmap =
  1633. (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
  1634. (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
  1635. offset += qcount;
  1636. } else {
  1637. /* TC is not enabled so set the offset to
  1638. * default queue and allocate one queue
  1639. * for the given TC.
  1640. */
  1641. vsi->tc_config.tc_info[i].qoffset = 0;
  1642. vsi->tc_config.tc_info[i].qcount = 1;
  1643. vsi->tc_config.tc_info[i].netdev_tc = 0;
  1644. qmap = 0;
  1645. }
  1646. ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
  1647. }
  1648. /* Set actual Tx/Rx queue pairs */
  1649. vsi->num_queue_pairs = offset;
  1650. if ((vsi->type == I40E_VSI_MAIN) && (numtc == 1)) {
  1651. if (vsi->req_queue_pairs > 0)
  1652. vsi->num_queue_pairs = vsi->req_queue_pairs;
  1653. else if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  1654. vsi->num_queue_pairs = pf->num_lan_msix;
  1655. }
  1656. /* Scheduler section valid can only be set for ADD VSI */
  1657. if (is_add) {
  1658. sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
  1659. ctxt->info.up_enable_bits = enabled_tc;
  1660. }
  1661. if (vsi->type == I40E_VSI_SRIOV) {
  1662. ctxt->info.mapping_flags |=
  1663. cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
  1664. for (i = 0; i < vsi->num_queue_pairs; i++)
  1665. ctxt->info.queue_mapping[i] =
  1666. cpu_to_le16(vsi->base_queue + i);
  1667. } else {
  1668. ctxt->info.mapping_flags |=
  1669. cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
  1670. ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
  1671. }
  1672. ctxt->info.valid_sections |= cpu_to_le16(sections);
  1673. }
  1674. /**
  1675. * i40e_addr_sync - Callback for dev_(mc|uc)_sync to add address
  1676. * @netdev: the netdevice
  1677. * @addr: address to add
  1678. *
  1679. * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
  1680. * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
  1681. */
  1682. static int i40e_addr_sync(struct net_device *netdev, const u8 *addr)
  1683. {
  1684. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1685. struct i40e_vsi *vsi = np->vsi;
  1686. if (i40e_add_mac_filter(vsi, addr))
  1687. return 0;
  1688. else
  1689. return -ENOMEM;
  1690. }
  1691. /**
  1692. * i40e_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
  1693. * @netdev: the netdevice
  1694. * @addr: address to add
  1695. *
  1696. * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call
  1697. * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
  1698. */
  1699. static int i40e_addr_unsync(struct net_device *netdev, const u8 *addr)
  1700. {
  1701. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1702. struct i40e_vsi *vsi = np->vsi;
  1703. /* Under some circumstances, we might receive a request to delete
  1704. * our own device address from our uc list. Because we store the
  1705. * device address in the VSI's MAC/VLAN filter list, we need to ignore
  1706. * such requests and not delete our device address from this list.
  1707. */
  1708. if (ether_addr_equal(addr, netdev->dev_addr))
  1709. return 0;
  1710. i40e_del_mac_filter(vsi, addr);
  1711. return 0;
  1712. }
  1713. /**
  1714. * i40e_set_rx_mode - NDO callback to set the netdev filters
  1715. * @netdev: network interface device structure
  1716. **/
  1717. static void i40e_set_rx_mode(struct net_device *netdev)
  1718. {
  1719. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1720. struct i40e_vsi *vsi = np->vsi;
  1721. spin_lock_bh(&vsi->mac_filter_hash_lock);
  1722. __dev_uc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
  1723. __dev_mc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
  1724. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  1725. /* check for other flag changes */
  1726. if (vsi->current_netdev_flags != vsi->netdev->flags) {
  1727. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  1728. set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
  1729. }
  1730. }
  1731. /**
  1732. * i40e_undo_del_filter_entries - Undo the changes made to MAC filter entries
  1733. * @vsi: Pointer to VSI struct
  1734. * @from: Pointer to list which contains MAC filter entries - changes to
  1735. * those entries needs to be undone.
  1736. *
  1737. * MAC filter entries from this list were slated for deletion.
  1738. **/
  1739. static void i40e_undo_del_filter_entries(struct i40e_vsi *vsi,
  1740. struct hlist_head *from)
  1741. {
  1742. struct i40e_mac_filter *f;
  1743. struct hlist_node *h;
  1744. hlist_for_each_entry_safe(f, h, from, hlist) {
  1745. u64 key = i40e_addr_to_hkey(f->macaddr);
  1746. /* Move the element back into MAC filter list*/
  1747. hlist_del(&f->hlist);
  1748. hash_add(vsi->mac_filter_hash, &f->hlist, key);
  1749. }
  1750. }
  1751. /**
  1752. * i40e_undo_add_filter_entries - Undo the changes made to MAC filter entries
  1753. * @vsi: Pointer to vsi struct
  1754. * @from: Pointer to list which contains MAC filter entries - changes to
  1755. * those entries needs to be undone.
  1756. *
  1757. * MAC filter entries from this list were slated for addition.
  1758. **/
  1759. static void i40e_undo_add_filter_entries(struct i40e_vsi *vsi,
  1760. struct hlist_head *from)
  1761. {
  1762. struct i40e_new_mac_filter *new;
  1763. struct hlist_node *h;
  1764. hlist_for_each_entry_safe(new, h, from, hlist) {
  1765. /* We can simply free the wrapper structure */
  1766. hlist_del(&new->hlist);
  1767. kfree(new);
  1768. }
  1769. }
  1770. /**
  1771. * i40e_next_entry - Get the next non-broadcast filter from a list
  1772. * @next: pointer to filter in list
  1773. *
  1774. * Returns the next non-broadcast filter in the list. Required so that we
  1775. * ignore broadcast filters within the list, since these are not handled via
  1776. * the normal firmware update path.
  1777. */
  1778. static
  1779. struct i40e_new_mac_filter *i40e_next_filter(struct i40e_new_mac_filter *next)
  1780. {
  1781. hlist_for_each_entry_continue(next, hlist) {
  1782. if (!is_broadcast_ether_addr(next->f->macaddr))
  1783. return next;
  1784. }
  1785. return NULL;
  1786. }
  1787. /**
  1788. * i40e_update_filter_state - Update filter state based on return data
  1789. * from firmware
  1790. * @count: Number of filters added
  1791. * @add_list: return data from fw
  1792. * @add_head: pointer to first filter in current batch
  1793. *
  1794. * MAC filter entries from list were slated to be added to device. Returns
  1795. * number of successful filters. Note that 0 does NOT mean success!
  1796. **/
  1797. static int
  1798. i40e_update_filter_state(int count,
  1799. struct i40e_aqc_add_macvlan_element_data *add_list,
  1800. struct i40e_new_mac_filter *add_head)
  1801. {
  1802. int retval = 0;
  1803. int i;
  1804. for (i = 0; i < count; i++) {
  1805. /* Always check status of each filter. We don't need to check
  1806. * the firmware return status because we pre-set the filter
  1807. * status to I40E_AQC_MM_ERR_NO_RES when sending the filter
  1808. * request to the adminq. Thus, if it no longer matches then
  1809. * we know the filter is active.
  1810. */
  1811. if (add_list[i].match_method == I40E_AQC_MM_ERR_NO_RES) {
  1812. add_head->state = I40E_FILTER_FAILED;
  1813. } else {
  1814. add_head->state = I40E_FILTER_ACTIVE;
  1815. retval++;
  1816. }
  1817. add_head = i40e_next_filter(add_head);
  1818. if (!add_head)
  1819. break;
  1820. }
  1821. return retval;
  1822. }
  1823. /**
  1824. * i40e_aqc_del_filters - Request firmware to delete a set of filters
  1825. * @vsi: ptr to the VSI
  1826. * @vsi_name: name to display in messages
  1827. * @list: the list of filters to send to firmware
  1828. * @num_del: the number of filters to delete
  1829. * @retval: Set to -EIO on failure to delete
  1830. *
  1831. * Send a request to firmware via AdminQ to delete a set of filters. Uses
  1832. * *retval instead of a return value so that success does not force ret_val to
  1833. * be set to 0. This ensures that a sequence of calls to this function
  1834. * preserve the previous value of *retval on successful delete.
  1835. */
  1836. static
  1837. void i40e_aqc_del_filters(struct i40e_vsi *vsi, const char *vsi_name,
  1838. struct i40e_aqc_remove_macvlan_element_data *list,
  1839. int num_del, int *retval)
  1840. {
  1841. struct i40e_hw *hw = &vsi->back->hw;
  1842. i40e_status aq_ret;
  1843. int aq_err;
  1844. aq_ret = i40e_aq_remove_macvlan(hw, vsi->seid, list, num_del, NULL);
  1845. aq_err = hw->aq.asq_last_status;
  1846. /* Explicitly ignore and do not report when firmware returns ENOENT */
  1847. if (aq_ret && !(aq_err == I40E_AQ_RC_ENOENT)) {
  1848. *retval = -EIO;
  1849. dev_info(&vsi->back->pdev->dev,
  1850. "ignoring delete macvlan error on %s, err %s, aq_err %s\n",
  1851. vsi_name, i40e_stat_str(hw, aq_ret),
  1852. i40e_aq_str(hw, aq_err));
  1853. }
  1854. }
  1855. /**
  1856. * i40e_aqc_add_filters - Request firmware to add a set of filters
  1857. * @vsi: ptr to the VSI
  1858. * @vsi_name: name to display in messages
  1859. * @list: the list of filters to send to firmware
  1860. * @add_head: Position in the add hlist
  1861. * @num_add: the number of filters to add
  1862. *
  1863. * Send a request to firmware via AdminQ to add a chunk of filters. Will set
  1864. * __I40E_VSI_OVERFLOW_PROMISC bit in vsi->state if the firmware has run out of
  1865. * space for more filters.
  1866. */
  1867. static
  1868. void i40e_aqc_add_filters(struct i40e_vsi *vsi, const char *vsi_name,
  1869. struct i40e_aqc_add_macvlan_element_data *list,
  1870. struct i40e_new_mac_filter *add_head,
  1871. int num_add)
  1872. {
  1873. struct i40e_hw *hw = &vsi->back->hw;
  1874. int aq_err, fcnt;
  1875. i40e_aq_add_macvlan(hw, vsi->seid, list, num_add, NULL);
  1876. aq_err = hw->aq.asq_last_status;
  1877. fcnt = i40e_update_filter_state(num_add, list, add_head);
  1878. if (fcnt != num_add) {
  1879. set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
  1880. dev_warn(&vsi->back->pdev->dev,
  1881. "Error %s adding RX filters on %s, promiscuous mode forced on\n",
  1882. i40e_aq_str(hw, aq_err),
  1883. vsi_name);
  1884. }
  1885. }
  1886. /**
  1887. * i40e_aqc_broadcast_filter - Set promiscuous broadcast flags
  1888. * @vsi: pointer to the VSI
  1889. * @vsi_name: the VSI name
  1890. * @f: filter data
  1891. *
  1892. * This function sets or clears the promiscuous broadcast flags for VLAN
  1893. * filters in order to properly receive broadcast frames. Assumes that only
  1894. * broadcast filters are passed.
  1895. *
  1896. * Returns status indicating success or failure;
  1897. **/
  1898. static i40e_status
  1899. i40e_aqc_broadcast_filter(struct i40e_vsi *vsi, const char *vsi_name,
  1900. struct i40e_mac_filter *f)
  1901. {
  1902. bool enable = f->state == I40E_FILTER_NEW;
  1903. struct i40e_hw *hw = &vsi->back->hw;
  1904. i40e_status aq_ret;
  1905. if (f->vlan == I40E_VLAN_ANY) {
  1906. aq_ret = i40e_aq_set_vsi_broadcast(hw,
  1907. vsi->seid,
  1908. enable,
  1909. NULL);
  1910. } else {
  1911. aq_ret = i40e_aq_set_vsi_bc_promisc_on_vlan(hw,
  1912. vsi->seid,
  1913. enable,
  1914. f->vlan,
  1915. NULL);
  1916. }
  1917. if (aq_ret) {
  1918. set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
  1919. dev_warn(&vsi->back->pdev->dev,
  1920. "Error %s, forcing overflow promiscuous on %s\n",
  1921. i40e_aq_str(hw, hw->aq.asq_last_status),
  1922. vsi_name);
  1923. }
  1924. return aq_ret;
  1925. }
  1926. /**
  1927. * i40e_set_promiscuous - set promiscuous mode
  1928. * @pf: board private structure
  1929. * @promisc: promisc on or off
  1930. *
  1931. * There are different ways of setting promiscuous mode on a PF depending on
  1932. * what state/environment we're in. This identifies and sets it appropriately.
  1933. * Returns 0 on success.
  1934. **/
  1935. static int i40e_set_promiscuous(struct i40e_pf *pf, bool promisc)
  1936. {
  1937. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  1938. struct i40e_hw *hw = &pf->hw;
  1939. i40e_status aq_ret;
  1940. if (vsi->type == I40E_VSI_MAIN &&
  1941. pf->lan_veb != I40E_NO_VEB &&
  1942. !(pf->flags & I40E_FLAG_MFP_ENABLED)) {
  1943. /* set defport ON for Main VSI instead of true promisc
  1944. * this way we will get all unicast/multicast and VLAN
  1945. * promisc behavior but will not get VF or VMDq traffic
  1946. * replicated on the Main VSI.
  1947. */
  1948. if (promisc)
  1949. aq_ret = i40e_aq_set_default_vsi(hw,
  1950. vsi->seid,
  1951. NULL);
  1952. else
  1953. aq_ret = i40e_aq_clear_default_vsi(hw,
  1954. vsi->seid,
  1955. NULL);
  1956. if (aq_ret) {
  1957. dev_info(&pf->pdev->dev,
  1958. "Set default VSI failed, err %s, aq_err %s\n",
  1959. i40e_stat_str(hw, aq_ret),
  1960. i40e_aq_str(hw, hw->aq.asq_last_status));
  1961. }
  1962. } else {
  1963. aq_ret = i40e_aq_set_vsi_unicast_promiscuous(
  1964. hw,
  1965. vsi->seid,
  1966. promisc, NULL,
  1967. true);
  1968. if (aq_ret) {
  1969. dev_info(&pf->pdev->dev,
  1970. "set unicast promisc failed, err %s, aq_err %s\n",
  1971. i40e_stat_str(hw, aq_ret),
  1972. i40e_aq_str(hw, hw->aq.asq_last_status));
  1973. }
  1974. aq_ret = i40e_aq_set_vsi_multicast_promiscuous(
  1975. hw,
  1976. vsi->seid,
  1977. promisc, NULL);
  1978. if (aq_ret) {
  1979. dev_info(&pf->pdev->dev,
  1980. "set multicast promisc 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. }
  1985. if (!aq_ret)
  1986. pf->cur_promisc = promisc;
  1987. return aq_ret;
  1988. }
  1989. /**
  1990. * i40e_sync_vsi_filters - Update the VSI filter list to the HW
  1991. * @vsi: ptr to the VSI
  1992. *
  1993. * Push any outstanding VSI filter changes through the AdminQ.
  1994. *
  1995. * Returns 0 or error value
  1996. **/
  1997. int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
  1998. {
  1999. struct hlist_head tmp_add_list, tmp_del_list;
  2000. struct i40e_mac_filter *f;
  2001. struct i40e_new_mac_filter *new, *add_head = NULL;
  2002. struct i40e_hw *hw = &vsi->back->hw;
  2003. bool old_overflow, new_overflow;
  2004. unsigned int failed_filters = 0;
  2005. unsigned int vlan_filters = 0;
  2006. char vsi_name[16] = "PF";
  2007. int filter_list_len = 0;
  2008. i40e_status aq_ret = 0;
  2009. u32 changed_flags = 0;
  2010. struct hlist_node *h;
  2011. struct i40e_pf *pf;
  2012. int num_add = 0;
  2013. int num_del = 0;
  2014. int retval = 0;
  2015. u16 cmd_flags;
  2016. int list_size;
  2017. int bkt;
  2018. /* empty array typed pointers, kcalloc later */
  2019. struct i40e_aqc_add_macvlan_element_data *add_list;
  2020. struct i40e_aqc_remove_macvlan_element_data *del_list;
  2021. while (test_and_set_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state))
  2022. usleep_range(1000, 2000);
  2023. pf = vsi->back;
  2024. old_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
  2025. if (vsi->netdev) {
  2026. changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
  2027. vsi->current_netdev_flags = vsi->netdev->flags;
  2028. }
  2029. INIT_HLIST_HEAD(&tmp_add_list);
  2030. INIT_HLIST_HEAD(&tmp_del_list);
  2031. if (vsi->type == I40E_VSI_SRIOV)
  2032. snprintf(vsi_name, sizeof(vsi_name) - 1, "VF %d", vsi->vf_id);
  2033. else if (vsi->type != I40E_VSI_MAIN)
  2034. snprintf(vsi_name, sizeof(vsi_name) - 1, "vsi %d", vsi->seid);
  2035. if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
  2036. vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
  2037. spin_lock_bh(&vsi->mac_filter_hash_lock);
  2038. /* Create a list of filters to delete. */
  2039. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  2040. if (f->state == I40E_FILTER_REMOVE) {
  2041. /* Move the element into temporary del_list */
  2042. hash_del(&f->hlist);
  2043. hlist_add_head(&f->hlist, &tmp_del_list);
  2044. /* Avoid counting removed filters */
  2045. continue;
  2046. }
  2047. if (f->state == I40E_FILTER_NEW) {
  2048. /* Create a temporary i40e_new_mac_filter */
  2049. new = kzalloc(sizeof(*new), GFP_ATOMIC);
  2050. if (!new)
  2051. goto err_no_memory_locked;
  2052. /* Store pointer to the real filter */
  2053. new->f = f;
  2054. new->state = f->state;
  2055. /* Add it to the hash list */
  2056. hlist_add_head(&new->hlist, &tmp_add_list);
  2057. }
  2058. /* Count the number of active (current and new) VLAN
  2059. * filters we have now. Does not count filters which
  2060. * are marked for deletion.
  2061. */
  2062. if (f->vlan > 0)
  2063. vlan_filters++;
  2064. }
  2065. retval = i40e_correct_mac_vlan_filters(vsi,
  2066. &tmp_add_list,
  2067. &tmp_del_list,
  2068. vlan_filters);
  2069. if (retval)
  2070. goto err_no_memory_locked;
  2071. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2072. }
  2073. /* Now process 'del_list' outside the lock */
  2074. if (!hlist_empty(&tmp_del_list)) {
  2075. filter_list_len = hw->aq.asq_buf_size /
  2076. sizeof(struct i40e_aqc_remove_macvlan_element_data);
  2077. list_size = filter_list_len *
  2078. sizeof(struct i40e_aqc_remove_macvlan_element_data);
  2079. del_list = kzalloc(list_size, GFP_ATOMIC);
  2080. if (!del_list)
  2081. goto err_no_memory;
  2082. hlist_for_each_entry_safe(f, h, &tmp_del_list, hlist) {
  2083. cmd_flags = 0;
  2084. /* handle broadcast filters by updating the broadcast
  2085. * promiscuous flag and release filter list.
  2086. */
  2087. if (is_broadcast_ether_addr(f->macaddr)) {
  2088. i40e_aqc_broadcast_filter(vsi, vsi_name, f);
  2089. hlist_del(&f->hlist);
  2090. kfree(f);
  2091. continue;
  2092. }
  2093. /* add to delete list */
  2094. ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
  2095. if (f->vlan == I40E_VLAN_ANY) {
  2096. del_list[num_del].vlan_tag = 0;
  2097. cmd_flags |= I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
  2098. } else {
  2099. del_list[num_del].vlan_tag =
  2100. cpu_to_le16((u16)(f->vlan));
  2101. }
  2102. cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
  2103. del_list[num_del].flags = cmd_flags;
  2104. num_del++;
  2105. /* flush a full buffer */
  2106. if (num_del == filter_list_len) {
  2107. i40e_aqc_del_filters(vsi, vsi_name, del_list,
  2108. num_del, &retval);
  2109. memset(del_list, 0, list_size);
  2110. num_del = 0;
  2111. }
  2112. /* Release memory for MAC filter entries which were
  2113. * synced up with HW.
  2114. */
  2115. hlist_del(&f->hlist);
  2116. kfree(f);
  2117. }
  2118. if (num_del) {
  2119. i40e_aqc_del_filters(vsi, vsi_name, del_list,
  2120. num_del, &retval);
  2121. }
  2122. kfree(del_list);
  2123. del_list = NULL;
  2124. }
  2125. if (!hlist_empty(&tmp_add_list)) {
  2126. /* Do all the adds now. */
  2127. filter_list_len = hw->aq.asq_buf_size /
  2128. sizeof(struct i40e_aqc_add_macvlan_element_data);
  2129. list_size = filter_list_len *
  2130. sizeof(struct i40e_aqc_add_macvlan_element_data);
  2131. add_list = kzalloc(list_size, GFP_ATOMIC);
  2132. if (!add_list)
  2133. goto err_no_memory;
  2134. num_add = 0;
  2135. hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
  2136. /* handle broadcast filters by updating the broadcast
  2137. * promiscuous flag instead of adding a MAC filter.
  2138. */
  2139. if (is_broadcast_ether_addr(new->f->macaddr)) {
  2140. if (i40e_aqc_broadcast_filter(vsi, vsi_name,
  2141. new->f))
  2142. new->state = I40E_FILTER_FAILED;
  2143. else
  2144. new->state = I40E_FILTER_ACTIVE;
  2145. continue;
  2146. }
  2147. /* add to add array */
  2148. if (num_add == 0)
  2149. add_head = new;
  2150. cmd_flags = 0;
  2151. ether_addr_copy(add_list[num_add].mac_addr,
  2152. new->f->macaddr);
  2153. if (new->f->vlan == I40E_VLAN_ANY) {
  2154. add_list[num_add].vlan_tag = 0;
  2155. cmd_flags |= I40E_AQC_MACVLAN_ADD_IGNORE_VLAN;
  2156. } else {
  2157. add_list[num_add].vlan_tag =
  2158. cpu_to_le16((u16)(new->f->vlan));
  2159. }
  2160. add_list[num_add].queue_number = 0;
  2161. /* set invalid match method for later detection */
  2162. add_list[num_add].match_method = I40E_AQC_MM_ERR_NO_RES;
  2163. cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
  2164. add_list[num_add].flags = cpu_to_le16(cmd_flags);
  2165. num_add++;
  2166. /* flush a full buffer */
  2167. if (num_add == filter_list_len) {
  2168. i40e_aqc_add_filters(vsi, vsi_name, add_list,
  2169. add_head, num_add);
  2170. memset(add_list, 0, list_size);
  2171. num_add = 0;
  2172. }
  2173. }
  2174. if (num_add) {
  2175. i40e_aqc_add_filters(vsi, vsi_name, add_list, add_head,
  2176. num_add);
  2177. }
  2178. /* Now move all of the filters from the temp add list back to
  2179. * the VSI's list.
  2180. */
  2181. spin_lock_bh(&vsi->mac_filter_hash_lock);
  2182. hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
  2183. /* Only update the state if we're still NEW */
  2184. if (new->f->state == I40E_FILTER_NEW)
  2185. new->f->state = new->state;
  2186. hlist_del(&new->hlist);
  2187. kfree(new);
  2188. }
  2189. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2190. kfree(add_list);
  2191. add_list = NULL;
  2192. }
  2193. /* Determine the number of active and failed filters. */
  2194. spin_lock_bh(&vsi->mac_filter_hash_lock);
  2195. vsi->active_filters = 0;
  2196. hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
  2197. if (f->state == I40E_FILTER_ACTIVE)
  2198. vsi->active_filters++;
  2199. else if (f->state == I40E_FILTER_FAILED)
  2200. failed_filters++;
  2201. }
  2202. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2203. /* Check if we are able to exit overflow promiscuous mode. We can
  2204. * safely exit if we didn't just enter, we no longer have any failed
  2205. * filters, and we have reduced filters below the threshold value.
  2206. */
  2207. if (old_overflow && !failed_filters &&
  2208. vsi->active_filters < vsi->promisc_threshold) {
  2209. dev_info(&pf->pdev->dev,
  2210. "filter logjam cleared on %s, leaving overflow promiscuous mode\n",
  2211. vsi_name);
  2212. clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
  2213. vsi->promisc_threshold = 0;
  2214. }
  2215. /* if the VF is not trusted do not do promisc */
  2216. if ((vsi->type == I40E_VSI_SRIOV) && !pf->vf[vsi->vf_id].trusted) {
  2217. clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
  2218. goto out;
  2219. }
  2220. new_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
  2221. /* If we are entering overflow promiscuous, we need to calculate a new
  2222. * threshold for when we are safe to exit
  2223. */
  2224. if (!old_overflow && new_overflow)
  2225. vsi->promisc_threshold = (vsi->active_filters * 3) / 4;
  2226. /* check for changes in promiscuous modes */
  2227. if (changed_flags & IFF_ALLMULTI) {
  2228. bool cur_multipromisc;
  2229. cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
  2230. aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
  2231. vsi->seid,
  2232. cur_multipromisc,
  2233. NULL);
  2234. if (aq_ret) {
  2235. retval = i40e_aq_rc_to_posix(aq_ret,
  2236. hw->aq.asq_last_status);
  2237. dev_info(&pf->pdev->dev,
  2238. "set multi promisc failed on %s, err %s aq_err %s\n",
  2239. vsi_name,
  2240. i40e_stat_str(hw, aq_ret),
  2241. i40e_aq_str(hw, hw->aq.asq_last_status));
  2242. }
  2243. }
  2244. if ((changed_flags & IFF_PROMISC) || old_overflow != new_overflow) {
  2245. bool cur_promisc;
  2246. cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
  2247. new_overflow);
  2248. aq_ret = i40e_set_promiscuous(pf, cur_promisc);
  2249. if (aq_ret) {
  2250. retval = i40e_aq_rc_to_posix(aq_ret,
  2251. hw->aq.asq_last_status);
  2252. dev_info(&pf->pdev->dev,
  2253. "Setting promiscuous %s failed on %s, err %s aq_err %s\n",
  2254. cur_promisc ? "on" : "off",
  2255. vsi_name,
  2256. i40e_stat_str(hw, aq_ret),
  2257. i40e_aq_str(hw, hw->aq.asq_last_status));
  2258. }
  2259. }
  2260. out:
  2261. /* if something went wrong then set the changed flag so we try again */
  2262. if (retval)
  2263. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  2264. clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
  2265. return retval;
  2266. err_no_memory:
  2267. /* Restore elements on the temporary add and delete lists */
  2268. spin_lock_bh(&vsi->mac_filter_hash_lock);
  2269. err_no_memory_locked:
  2270. i40e_undo_del_filter_entries(vsi, &tmp_del_list);
  2271. i40e_undo_add_filter_entries(vsi, &tmp_add_list);
  2272. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2273. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  2274. clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
  2275. return -ENOMEM;
  2276. }
  2277. /**
  2278. * i40e_sync_filters_subtask - Sync the VSI filter list with HW
  2279. * @pf: board private structure
  2280. **/
  2281. static void i40e_sync_filters_subtask(struct i40e_pf *pf)
  2282. {
  2283. int v;
  2284. if (!pf)
  2285. return;
  2286. if (!test_and_clear_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state))
  2287. return;
  2288. for (v = 0; v < pf->num_alloc_vsi; v++) {
  2289. if (pf->vsi[v] &&
  2290. (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED)) {
  2291. int ret = i40e_sync_vsi_filters(pf->vsi[v]);
  2292. if (ret) {
  2293. /* come back and try again later */
  2294. set_bit(__I40E_MACVLAN_SYNC_PENDING,
  2295. pf->state);
  2296. break;
  2297. }
  2298. }
  2299. }
  2300. }
  2301. /**
  2302. * i40e_max_xdp_frame_size - returns the maximum allowed frame size for XDP
  2303. * @vsi: the vsi
  2304. **/
  2305. static int i40e_max_xdp_frame_size(struct i40e_vsi *vsi)
  2306. {
  2307. if (PAGE_SIZE >= 8192 || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
  2308. return I40E_RXBUFFER_2048;
  2309. else
  2310. return I40E_RXBUFFER_3072;
  2311. }
  2312. /**
  2313. * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
  2314. * @netdev: network interface device structure
  2315. * @new_mtu: new value for maximum frame size
  2316. *
  2317. * Returns 0 on success, negative on failure
  2318. **/
  2319. static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
  2320. {
  2321. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2322. struct i40e_vsi *vsi = np->vsi;
  2323. struct i40e_pf *pf = vsi->back;
  2324. if (i40e_enabled_xdp_vsi(vsi)) {
  2325. int frame_size = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
  2326. if (frame_size > i40e_max_xdp_frame_size(vsi))
  2327. return -EINVAL;
  2328. }
  2329. netdev_info(netdev, "changing MTU from %d to %d\n",
  2330. netdev->mtu, new_mtu);
  2331. netdev->mtu = new_mtu;
  2332. if (netif_running(netdev))
  2333. i40e_vsi_reinit_locked(vsi);
  2334. set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
  2335. set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
  2336. return 0;
  2337. }
  2338. /**
  2339. * i40e_ioctl - Access the hwtstamp interface
  2340. * @netdev: network interface device structure
  2341. * @ifr: interface request data
  2342. * @cmd: ioctl command
  2343. **/
  2344. int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
  2345. {
  2346. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2347. struct i40e_pf *pf = np->vsi->back;
  2348. switch (cmd) {
  2349. case SIOCGHWTSTAMP:
  2350. return i40e_ptp_get_ts_config(pf, ifr);
  2351. case SIOCSHWTSTAMP:
  2352. return i40e_ptp_set_ts_config(pf, ifr);
  2353. default:
  2354. return -EOPNOTSUPP;
  2355. }
  2356. }
  2357. /**
  2358. * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
  2359. * @vsi: the vsi being adjusted
  2360. **/
  2361. void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
  2362. {
  2363. struct i40e_vsi_context ctxt;
  2364. i40e_status ret;
  2365. if ((vsi->info.valid_sections &
  2366. cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
  2367. ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
  2368. return; /* already enabled */
  2369. vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
  2370. vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
  2371. I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
  2372. ctxt.seid = vsi->seid;
  2373. ctxt.info = vsi->info;
  2374. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  2375. if (ret) {
  2376. dev_info(&vsi->back->pdev->dev,
  2377. "update vlan stripping failed, err %s aq_err %s\n",
  2378. i40e_stat_str(&vsi->back->hw, ret),
  2379. i40e_aq_str(&vsi->back->hw,
  2380. vsi->back->hw.aq.asq_last_status));
  2381. }
  2382. }
  2383. /**
  2384. * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
  2385. * @vsi: the vsi being adjusted
  2386. **/
  2387. void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
  2388. {
  2389. struct i40e_vsi_context ctxt;
  2390. i40e_status ret;
  2391. if ((vsi->info.valid_sections &
  2392. cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
  2393. ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
  2394. I40E_AQ_VSI_PVLAN_EMOD_MASK))
  2395. return; /* already disabled */
  2396. vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
  2397. vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
  2398. I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
  2399. ctxt.seid = vsi->seid;
  2400. ctxt.info = vsi->info;
  2401. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  2402. if (ret) {
  2403. dev_info(&vsi->back->pdev->dev,
  2404. "update vlan stripping failed, err %s aq_err %s\n",
  2405. i40e_stat_str(&vsi->back->hw, ret),
  2406. i40e_aq_str(&vsi->back->hw,
  2407. vsi->back->hw.aq.asq_last_status));
  2408. }
  2409. }
  2410. /**
  2411. * i40e_add_vlan_all_mac - Add a MAC/VLAN filter for each existing MAC address
  2412. * @vsi: the vsi being configured
  2413. * @vid: vlan id to be added (0 = untagged only , -1 = any)
  2414. *
  2415. * This is a helper function for adding a new MAC/VLAN filter with the
  2416. * specified VLAN for each existing MAC address already in the hash table.
  2417. * This function does *not* perform any accounting to update filters based on
  2418. * VLAN mode.
  2419. *
  2420. * NOTE: this function expects to be called while under the
  2421. * mac_filter_hash_lock
  2422. **/
  2423. int i40e_add_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
  2424. {
  2425. struct i40e_mac_filter *f, *add_f;
  2426. struct hlist_node *h;
  2427. int bkt;
  2428. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  2429. if (f->state == I40E_FILTER_REMOVE)
  2430. continue;
  2431. add_f = i40e_add_filter(vsi, f->macaddr, vid);
  2432. if (!add_f) {
  2433. dev_info(&vsi->back->pdev->dev,
  2434. "Could not add vlan filter %d for %pM\n",
  2435. vid, f->macaddr);
  2436. return -ENOMEM;
  2437. }
  2438. }
  2439. return 0;
  2440. }
  2441. /**
  2442. * i40e_vsi_add_vlan - Add VSI membership for given VLAN
  2443. * @vsi: the VSI being configured
  2444. * @vid: VLAN id to be added
  2445. **/
  2446. int i40e_vsi_add_vlan(struct i40e_vsi *vsi, u16 vid)
  2447. {
  2448. int err;
  2449. if (vsi->info.pvid)
  2450. return -EINVAL;
  2451. /* The network stack will attempt to add VID=0, with the intention to
  2452. * receive priority tagged packets with a VLAN of 0. Our HW receives
  2453. * these packets by default when configured to receive untagged
  2454. * packets, so we don't need to add a filter for this case.
  2455. * Additionally, HW interprets adding a VID=0 filter as meaning to
  2456. * receive *only* tagged traffic and stops receiving untagged traffic.
  2457. * Thus, we do not want to actually add a filter for VID=0
  2458. */
  2459. if (!vid)
  2460. return 0;
  2461. /* Locked once because all functions invoked below iterates list*/
  2462. spin_lock_bh(&vsi->mac_filter_hash_lock);
  2463. err = i40e_add_vlan_all_mac(vsi, vid);
  2464. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2465. if (err)
  2466. return err;
  2467. /* schedule our worker thread which will take care of
  2468. * applying the new filter changes
  2469. */
  2470. i40e_service_event_schedule(vsi->back);
  2471. return 0;
  2472. }
  2473. /**
  2474. * i40e_rm_vlan_all_mac - Remove MAC/VLAN pair for all MAC with the given VLAN
  2475. * @vsi: the vsi being configured
  2476. * @vid: vlan id to be removed (0 = untagged only , -1 = any)
  2477. *
  2478. * This function should be used to remove all VLAN filters which match the
  2479. * given VID. It does not schedule the service event and does not take the
  2480. * mac_filter_hash_lock so it may be combined with other operations under
  2481. * a single invocation of the mac_filter_hash_lock.
  2482. *
  2483. * NOTE: this function expects to be called while under the
  2484. * mac_filter_hash_lock
  2485. */
  2486. void i40e_rm_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
  2487. {
  2488. struct i40e_mac_filter *f;
  2489. struct hlist_node *h;
  2490. int bkt;
  2491. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  2492. if (f->vlan == vid)
  2493. __i40e_del_filter(vsi, f);
  2494. }
  2495. }
  2496. /**
  2497. * i40e_vsi_kill_vlan - Remove VSI membership for given VLAN
  2498. * @vsi: the VSI being configured
  2499. * @vid: VLAN id to be removed
  2500. **/
  2501. void i40e_vsi_kill_vlan(struct i40e_vsi *vsi, u16 vid)
  2502. {
  2503. if (!vid || vsi->info.pvid)
  2504. return;
  2505. spin_lock_bh(&vsi->mac_filter_hash_lock);
  2506. i40e_rm_vlan_all_mac(vsi, vid);
  2507. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2508. /* schedule our worker thread which will take care of
  2509. * applying the new filter changes
  2510. */
  2511. i40e_service_event_schedule(vsi->back);
  2512. }
  2513. /**
  2514. * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
  2515. * @netdev: network interface to be adjusted
  2516. * @proto: unused protocol value
  2517. * @vid: vlan id to be added
  2518. *
  2519. * net_device_ops implementation for adding vlan ids
  2520. **/
  2521. static int i40e_vlan_rx_add_vid(struct net_device *netdev,
  2522. __always_unused __be16 proto, u16 vid)
  2523. {
  2524. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2525. struct i40e_vsi *vsi = np->vsi;
  2526. int ret = 0;
  2527. if (vid >= VLAN_N_VID)
  2528. return -EINVAL;
  2529. ret = i40e_vsi_add_vlan(vsi, vid);
  2530. if (!ret)
  2531. set_bit(vid, vsi->active_vlans);
  2532. return ret;
  2533. }
  2534. /**
  2535. * i40e_vlan_rx_add_vid_up - Add a vlan id filter to HW offload in UP path
  2536. * @netdev: network interface to be adjusted
  2537. * @proto: unused protocol value
  2538. * @vid: vlan id to be added
  2539. **/
  2540. static void i40e_vlan_rx_add_vid_up(struct net_device *netdev,
  2541. __always_unused __be16 proto, u16 vid)
  2542. {
  2543. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2544. struct i40e_vsi *vsi = np->vsi;
  2545. if (vid >= VLAN_N_VID)
  2546. return;
  2547. set_bit(vid, vsi->active_vlans);
  2548. }
  2549. /**
  2550. * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
  2551. * @netdev: network interface to be adjusted
  2552. * @proto: unused protocol value
  2553. * @vid: vlan id to be removed
  2554. *
  2555. * net_device_ops implementation for removing vlan ids
  2556. **/
  2557. static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
  2558. __always_unused __be16 proto, u16 vid)
  2559. {
  2560. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2561. struct i40e_vsi *vsi = np->vsi;
  2562. /* return code is ignored as there is nothing a user
  2563. * can do about failure to remove and a log message was
  2564. * already printed from the other function
  2565. */
  2566. i40e_vsi_kill_vlan(vsi, vid);
  2567. clear_bit(vid, vsi->active_vlans);
  2568. return 0;
  2569. }
  2570. /**
  2571. * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
  2572. * @vsi: the vsi being brought back up
  2573. **/
  2574. static void i40e_restore_vlan(struct i40e_vsi *vsi)
  2575. {
  2576. u16 vid;
  2577. if (!vsi->netdev)
  2578. return;
  2579. if (vsi->netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
  2580. i40e_vlan_stripping_enable(vsi);
  2581. else
  2582. i40e_vlan_stripping_disable(vsi);
  2583. for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
  2584. i40e_vlan_rx_add_vid_up(vsi->netdev, htons(ETH_P_8021Q),
  2585. vid);
  2586. }
  2587. /**
  2588. * i40e_vsi_add_pvid - Add pvid for the VSI
  2589. * @vsi: the vsi being adjusted
  2590. * @vid: the vlan id to set as a PVID
  2591. **/
  2592. int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
  2593. {
  2594. struct i40e_vsi_context ctxt;
  2595. i40e_status ret;
  2596. vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
  2597. vsi->info.pvid = cpu_to_le16(vid);
  2598. vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
  2599. I40E_AQ_VSI_PVLAN_INSERT_PVID |
  2600. I40E_AQ_VSI_PVLAN_EMOD_STR;
  2601. ctxt.seid = vsi->seid;
  2602. ctxt.info = vsi->info;
  2603. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  2604. if (ret) {
  2605. dev_info(&vsi->back->pdev->dev,
  2606. "add pvid failed, err %s aq_err %s\n",
  2607. i40e_stat_str(&vsi->back->hw, ret),
  2608. i40e_aq_str(&vsi->back->hw,
  2609. vsi->back->hw.aq.asq_last_status));
  2610. return -ENOENT;
  2611. }
  2612. return 0;
  2613. }
  2614. /**
  2615. * i40e_vsi_remove_pvid - Remove the pvid from the VSI
  2616. * @vsi: the vsi being adjusted
  2617. *
  2618. * Just use the vlan_rx_register() service to put it back to normal
  2619. **/
  2620. void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
  2621. {
  2622. i40e_vlan_stripping_disable(vsi);
  2623. vsi->info.pvid = 0;
  2624. }
  2625. /**
  2626. * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
  2627. * @vsi: ptr to the VSI
  2628. *
  2629. * If this function returns with an error, then it's possible one or
  2630. * more of the rings is populated (while the rest are not). It is the
  2631. * callers duty to clean those orphaned rings.
  2632. *
  2633. * Return 0 on success, negative on failure
  2634. **/
  2635. static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
  2636. {
  2637. int i, err = 0;
  2638. for (i = 0; i < vsi->num_queue_pairs && !err; i++)
  2639. err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
  2640. if (!i40e_enabled_xdp_vsi(vsi))
  2641. return err;
  2642. for (i = 0; i < vsi->num_queue_pairs && !err; i++)
  2643. err = i40e_setup_tx_descriptors(vsi->xdp_rings[i]);
  2644. return err;
  2645. }
  2646. /**
  2647. * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
  2648. * @vsi: ptr to the VSI
  2649. *
  2650. * Free VSI's transmit software resources
  2651. **/
  2652. static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
  2653. {
  2654. int i;
  2655. if (vsi->tx_rings) {
  2656. for (i = 0; i < vsi->num_queue_pairs; i++)
  2657. if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
  2658. i40e_free_tx_resources(vsi->tx_rings[i]);
  2659. }
  2660. if (vsi->xdp_rings) {
  2661. for (i = 0; i < vsi->num_queue_pairs; i++)
  2662. if (vsi->xdp_rings[i] && vsi->xdp_rings[i]->desc)
  2663. i40e_free_tx_resources(vsi->xdp_rings[i]);
  2664. }
  2665. }
  2666. /**
  2667. * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
  2668. * @vsi: ptr to the VSI
  2669. *
  2670. * If this function returns with an error, then it's possible one or
  2671. * more of the rings is populated (while the rest are not). It is the
  2672. * callers duty to clean those orphaned rings.
  2673. *
  2674. * Return 0 on success, negative on failure
  2675. **/
  2676. static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
  2677. {
  2678. int i, err = 0;
  2679. for (i = 0; i < vsi->num_queue_pairs && !err; i++)
  2680. err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
  2681. return err;
  2682. }
  2683. /**
  2684. * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
  2685. * @vsi: ptr to the VSI
  2686. *
  2687. * Free all receive software resources
  2688. **/
  2689. static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
  2690. {
  2691. int i;
  2692. if (!vsi->rx_rings)
  2693. return;
  2694. for (i = 0; i < vsi->num_queue_pairs; i++)
  2695. if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
  2696. i40e_free_rx_resources(vsi->rx_rings[i]);
  2697. }
  2698. /**
  2699. * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
  2700. * @ring: The Tx ring to configure
  2701. *
  2702. * This enables/disables XPS for a given Tx descriptor ring
  2703. * based on the TCs enabled for the VSI that ring belongs to.
  2704. **/
  2705. static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
  2706. {
  2707. int cpu;
  2708. if (!ring->q_vector || !ring->netdev || ring->ch)
  2709. return;
  2710. /* We only initialize XPS once, so as not to overwrite user settings */
  2711. if (test_and_set_bit(__I40E_TX_XPS_INIT_DONE, ring->state))
  2712. return;
  2713. cpu = cpumask_local_spread(ring->q_vector->v_idx, -1);
  2714. netif_set_xps_queue(ring->netdev, get_cpu_mask(cpu),
  2715. ring->queue_index);
  2716. }
  2717. /**
  2718. * i40e_configure_tx_ring - Configure a transmit ring context and rest
  2719. * @ring: The Tx ring to configure
  2720. *
  2721. * Configure the Tx descriptor ring in the HMC context.
  2722. **/
  2723. static int i40e_configure_tx_ring(struct i40e_ring *ring)
  2724. {
  2725. struct i40e_vsi *vsi = ring->vsi;
  2726. u16 pf_q = vsi->base_queue + ring->queue_index;
  2727. struct i40e_hw *hw = &vsi->back->hw;
  2728. struct i40e_hmc_obj_txq tx_ctx;
  2729. i40e_status err = 0;
  2730. u32 qtx_ctl = 0;
  2731. /* some ATR related tx ring init */
  2732. if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
  2733. ring->atr_sample_rate = vsi->back->atr_sample_rate;
  2734. ring->atr_count = 0;
  2735. } else {
  2736. ring->atr_sample_rate = 0;
  2737. }
  2738. /* configure XPS */
  2739. i40e_config_xps_tx_ring(ring);
  2740. /* clear the context structure first */
  2741. memset(&tx_ctx, 0, sizeof(tx_ctx));
  2742. tx_ctx.new_context = 1;
  2743. tx_ctx.base = (ring->dma / 128);
  2744. tx_ctx.qlen = ring->count;
  2745. tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
  2746. I40E_FLAG_FD_ATR_ENABLED));
  2747. tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
  2748. /* FDIR VSI tx ring can still use RS bit and writebacks */
  2749. if (vsi->type != I40E_VSI_FDIR)
  2750. tx_ctx.head_wb_ena = 1;
  2751. tx_ctx.head_wb_addr = ring->dma +
  2752. (ring->count * sizeof(struct i40e_tx_desc));
  2753. /* As part of VSI creation/update, FW allocates certain
  2754. * Tx arbitration queue sets for each TC enabled for
  2755. * the VSI. The FW returns the handles to these queue
  2756. * sets as part of the response buffer to Add VSI,
  2757. * Update VSI, etc. AQ commands. It is expected that
  2758. * these queue set handles be associated with the Tx
  2759. * queues by the driver as part of the TX queue context
  2760. * initialization. This has to be done regardless of
  2761. * DCB as by default everything is mapped to TC0.
  2762. */
  2763. if (ring->ch)
  2764. tx_ctx.rdylist =
  2765. le16_to_cpu(ring->ch->info.qs_handle[ring->dcb_tc]);
  2766. else
  2767. tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
  2768. tx_ctx.rdylist_act = 0;
  2769. /* clear the context in the HMC */
  2770. err = i40e_clear_lan_tx_queue_context(hw, pf_q);
  2771. if (err) {
  2772. dev_info(&vsi->back->pdev->dev,
  2773. "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
  2774. ring->queue_index, pf_q, err);
  2775. return -ENOMEM;
  2776. }
  2777. /* set the context in the HMC */
  2778. err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
  2779. if (err) {
  2780. dev_info(&vsi->back->pdev->dev,
  2781. "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
  2782. ring->queue_index, pf_q, err);
  2783. return -ENOMEM;
  2784. }
  2785. /* Now associate this queue with this PCI function */
  2786. if (ring->ch) {
  2787. if (ring->ch->type == I40E_VSI_VMDQ2)
  2788. qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
  2789. else
  2790. return -EINVAL;
  2791. qtx_ctl |= (ring->ch->vsi_number <<
  2792. I40E_QTX_CTL_VFVM_INDX_SHIFT) &
  2793. I40E_QTX_CTL_VFVM_INDX_MASK;
  2794. } else {
  2795. if (vsi->type == I40E_VSI_VMDQ2) {
  2796. qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
  2797. qtx_ctl |= ((vsi->id) << I40E_QTX_CTL_VFVM_INDX_SHIFT) &
  2798. I40E_QTX_CTL_VFVM_INDX_MASK;
  2799. } else {
  2800. qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
  2801. }
  2802. }
  2803. qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
  2804. I40E_QTX_CTL_PF_INDX_MASK);
  2805. wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
  2806. i40e_flush(hw);
  2807. /* cache tail off for easier writes later */
  2808. ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
  2809. return 0;
  2810. }
  2811. /**
  2812. * i40e_configure_rx_ring - Configure a receive ring context
  2813. * @ring: The Rx ring to configure
  2814. *
  2815. * Configure the Rx descriptor ring in the HMC context.
  2816. **/
  2817. static int i40e_configure_rx_ring(struct i40e_ring *ring)
  2818. {
  2819. struct i40e_vsi *vsi = ring->vsi;
  2820. u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
  2821. u16 pf_q = vsi->base_queue + ring->queue_index;
  2822. struct i40e_hw *hw = &vsi->back->hw;
  2823. struct i40e_hmc_obj_rxq rx_ctx;
  2824. i40e_status err = 0;
  2825. bitmap_zero(ring->state, __I40E_RING_STATE_NBITS);
  2826. /* clear the context structure first */
  2827. memset(&rx_ctx, 0, sizeof(rx_ctx));
  2828. ring->rx_buf_len = vsi->rx_buf_len;
  2829. rx_ctx.dbuff = DIV_ROUND_UP(ring->rx_buf_len,
  2830. BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
  2831. rx_ctx.base = (ring->dma / 128);
  2832. rx_ctx.qlen = ring->count;
  2833. /* use 32 byte descriptors */
  2834. rx_ctx.dsize = 1;
  2835. /* descriptor type is always zero
  2836. * rx_ctx.dtype = 0;
  2837. */
  2838. rx_ctx.hsplit_0 = 0;
  2839. rx_ctx.rxmax = min_t(u16, vsi->max_frame, chain_len * ring->rx_buf_len);
  2840. if (hw->revision_id == 0)
  2841. rx_ctx.lrxqthresh = 0;
  2842. else
  2843. rx_ctx.lrxqthresh = 1;
  2844. rx_ctx.crcstrip = 1;
  2845. rx_ctx.l2tsel = 1;
  2846. /* this controls whether VLAN is stripped from inner headers */
  2847. rx_ctx.showiv = 0;
  2848. /* set the prefena field to 1 because the manual says to */
  2849. rx_ctx.prefena = 1;
  2850. /* clear the context in the HMC */
  2851. err = i40e_clear_lan_rx_queue_context(hw, pf_q);
  2852. if (err) {
  2853. dev_info(&vsi->back->pdev->dev,
  2854. "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
  2855. ring->queue_index, pf_q, err);
  2856. return -ENOMEM;
  2857. }
  2858. /* set the context in the HMC */
  2859. err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
  2860. if (err) {
  2861. dev_info(&vsi->back->pdev->dev,
  2862. "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
  2863. ring->queue_index, pf_q, err);
  2864. return -ENOMEM;
  2865. }
  2866. /* configure Rx buffer alignment */
  2867. if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
  2868. clear_ring_build_skb_enabled(ring);
  2869. else
  2870. set_ring_build_skb_enabled(ring);
  2871. /* cache tail for quicker writes, and clear the reg before use */
  2872. ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
  2873. writel(0, ring->tail);
  2874. i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
  2875. return 0;
  2876. }
  2877. /**
  2878. * i40e_vsi_configure_tx - Configure the VSI for Tx
  2879. * @vsi: VSI structure describing this set of rings and resources
  2880. *
  2881. * Configure the Tx VSI for operation.
  2882. **/
  2883. static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
  2884. {
  2885. int err = 0;
  2886. u16 i;
  2887. for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
  2888. err = i40e_configure_tx_ring(vsi->tx_rings[i]);
  2889. if (!i40e_enabled_xdp_vsi(vsi))
  2890. return err;
  2891. for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
  2892. err = i40e_configure_tx_ring(vsi->xdp_rings[i]);
  2893. return err;
  2894. }
  2895. /**
  2896. * i40e_vsi_configure_rx - Configure the VSI for Rx
  2897. * @vsi: the VSI being configured
  2898. *
  2899. * Configure the Rx VSI for operation.
  2900. **/
  2901. static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
  2902. {
  2903. int err = 0;
  2904. u16 i;
  2905. if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX)) {
  2906. vsi->max_frame = I40E_MAX_RXBUFFER;
  2907. vsi->rx_buf_len = I40E_RXBUFFER_2048;
  2908. #if (PAGE_SIZE < 8192)
  2909. } else if (!I40E_2K_TOO_SMALL_WITH_PADDING &&
  2910. (vsi->netdev->mtu <= ETH_DATA_LEN)) {
  2911. vsi->max_frame = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
  2912. vsi->rx_buf_len = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
  2913. #endif
  2914. } else {
  2915. vsi->max_frame = I40E_MAX_RXBUFFER;
  2916. vsi->rx_buf_len = (PAGE_SIZE < 8192) ? I40E_RXBUFFER_3072 :
  2917. I40E_RXBUFFER_2048;
  2918. }
  2919. /* set up individual rings */
  2920. for (i = 0; i < vsi->num_queue_pairs && !err; i++)
  2921. err = i40e_configure_rx_ring(vsi->rx_rings[i]);
  2922. return err;
  2923. }
  2924. /**
  2925. * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
  2926. * @vsi: ptr to the VSI
  2927. **/
  2928. static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
  2929. {
  2930. struct i40e_ring *tx_ring, *rx_ring;
  2931. u16 qoffset, qcount;
  2932. int i, n;
  2933. if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
  2934. /* Reset the TC information */
  2935. for (i = 0; i < vsi->num_queue_pairs; i++) {
  2936. rx_ring = vsi->rx_rings[i];
  2937. tx_ring = vsi->tx_rings[i];
  2938. rx_ring->dcb_tc = 0;
  2939. tx_ring->dcb_tc = 0;
  2940. }
  2941. return;
  2942. }
  2943. for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
  2944. if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
  2945. continue;
  2946. qoffset = vsi->tc_config.tc_info[n].qoffset;
  2947. qcount = vsi->tc_config.tc_info[n].qcount;
  2948. for (i = qoffset; i < (qoffset + qcount); i++) {
  2949. rx_ring = vsi->rx_rings[i];
  2950. tx_ring = vsi->tx_rings[i];
  2951. rx_ring->dcb_tc = n;
  2952. tx_ring->dcb_tc = n;
  2953. }
  2954. }
  2955. }
  2956. /**
  2957. * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
  2958. * @vsi: ptr to the VSI
  2959. **/
  2960. static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
  2961. {
  2962. if (vsi->netdev)
  2963. i40e_set_rx_mode(vsi->netdev);
  2964. }
  2965. /**
  2966. * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
  2967. * @vsi: Pointer to the targeted VSI
  2968. *
  2969. * This function replays the hlist on the hw where all the SB Flow Director
  2970. * filters were saved.
  2971. **/
  2972. static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
  2973. {
  2974. struct i40e_fdir_filter *filter;
  2975. struct i40e_pf *pf = vsi->back;
  2976. struct hlist_node *node;
  2977. if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
  2978. return;
  2979. /* Reset FDir counters as we're replaying all existing filters */
  2980. pf->fd_tcp4_filter_cnt = 0;
  2981. pf->fd_udp4_filter_cnt = 0;
  2982. pf->fd_sctp4_filter_cnt = 0;
  2983. pf->fd_ip4_filter_cnt = 0;
  2984. hlist_for_each_entry_safe(filter, node,
  2985. &pf->fdir_filter_list, fdir_node) {
  2986. i40e_add_del_fdir(vsi, filter, true);
  2987. }
  2988. }
  2989. /**
  2990. * i40e_vsi_configure - Set up the VSI for action
  2991. * @vsi: the VSI being configured
  2992. **/
  2993. static int i40e_vsi_configure(struct i40e_vsi *vsi)
  2994. {
  2995. int err;
  2996. i40e_set_vsi_rx_mode(vsi);
  2997. i40e_restore_vlan(vsi);
  2998. i40e_vsi_config_dcb_rings(vsi);
  2999. err = i40e_vsi_configure_tx(vsi);
  3000. if (!err)
  3001. err = i40e_vsi_configure_rx(vsi);
  3002. return err;
  3003. }
  3004. /**
  3005. * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
  3006. * @vsi: the VSI being configured
  3007. **/
  3008. static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
  3009. {
  3010. bool has_xdp = i40e_enabled_xdp_vsi(vsi);
  3011. struct i40e_pf *pf = vsi->back;
  3012. struct i40e_hw *hw = &pf->hw;
  3013. u16 vector;
  3014. int i, q;
  3015. u32 qp;
  3016. /* The interrupt indexing is offset by 1 in the PFINT_ITRn
  3017. * and PFINT_LNKLSTn registers, e.g.:
  3018. * PFINT_ITRn[0..n-1] gets msix-1..msix-n (qpair interrupts)
  3019. */
  3020. qp = vsi->base_queue;
  3021. vector = vsi->base_vector;
  3022. for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
  3023. struct i40e_q_vector *q_vector = vsi->q_vectors[i];
  3024. q_vector->rx.next_update = jiffies + 1;
  3025. q_vector->rx.target_itr =
  3026. ITR_TO_REG(vsi->rx_rings[i]->itr_setting);
  3027. wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
  3028. q_vector->rx.target_itr);
  3029. q_vector->rx.current_itr = q_vector->rx.target_itr;
  3030. q_vector->tx.next_update = jiffies + 1;
  3031. q_vector->tx.target_itr =
  3032. ITR_TO_REG(vsi->tx_rings[i]->itr_setting);
  3033. wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
  3034. q_vector->tx.target_itr);
  3035. q_vector->tx.current_itr = q_vector->tx.target_itr;
  3036. wr32(hw, I40E_PFINT_RATEN(vector - 1),
  3037. i40e_intrl_usec_to_reg(vsi->int_rate_limit));
  3038. /* Linked list for the queuepairs assigned to this vector */
  3039. wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
  3040. for (q = 0; q < q_vector->num_ringpairs; q++) {
  3041. u32 nextqp = has_xdp ? qp + vsi->alloc_queue_pairs : qp;
  3042. u32 val;
  3043. val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
  3044. (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
  3045. (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
  3046. (nextqp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) |
  3047. (I40E_QUEUE_TYPE_TX <<
  3048. I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
  3049. wr32(hw, I40E_QINT_RQCTL(qp), val);
  3050. if (has_xdp) {
  3051. val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  3052. (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
  3053. (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
  3054. (qp << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
  3055. (I40E_QUEUE_TYPE_TX <<
  3056. I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
  3057. wr32(hw, I40E_QINT_TQCTL(nextqp), val);
  3058. }
  3059. val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  3060. (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
  3061. (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
  3062. ((qp + 1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
  3063. (I40E_QUEUE_TYPE_RX <<
  3064. I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
  3065. /* Terminate the linked list */
  3066. if (q == (q_vector->num_ringpairs - 1))
  3067. val |= (I40E_QUEUE_END_OF_LIST <<
  3068. I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
  3069. wr32(hw, I40E_QINT_TQCTL(qp), val);
  3070. qp++;
  3071. }
  3072. }
  3073. i40e_flush(hw);
  3074. }
  3075. /**
  3076. * i40e_enable_misc_int_causes - enable the non-queue interrupts
  3077. * @pf: pointer to private device data structure
  3078. **/
  3079. static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
  3080. {
  3081. struct i40e_hw *hw = &pf->hw;
  3082. u32 val;
  3083. /* clear things first */
  3084. wr32(hw, I40E_PFINT_ICR0_ENA, 0); /* disable all */
  3085. rd32(hw, I40E_PFINT_ICR0); /* read to clear */
  3086. val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK |
  3087. I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK |
  3088. I40E_PFINT_ICR0_ENA_GRST_MASK |
  3089. I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
  3090. I40E_PFINT_ICR0_ENA_GPIO_MASK |
  3091. I40E_PFINT_ICR0_ENA_HMC_ERR_MASK |
  3092. I40E_PFINT_ICR0_ENA_VFLR_MASK |
  3093. I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
  3094. if (pf->flags & I40E_FLAG_IWARP_ENABLED)
  3095. val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
  3096. if (pf->flags & I40E_FLAG_PTP)
  3097. val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
  3098. wr32(hw, I40E_PFINT_ICR0_ENA, val);
  3099. /* SW_ITR_IDX = 0, but don't change INTENA */
  3100. wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
  3101. I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
  3102. /* OTHER_ITR_IDX = 0 */
  3103. wr32(hw, I40E_PFINT_STAT_CTL0, 0);
  3104. }
  3105. /**
  3106. * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
  3107. * @vsi: the VSI being configured
  3108. **/
  3109. static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
  3110. {
  3111. u32 nextqp = i40e_enabled_xdp_vsi(vsi) ? vsi->alloc_queue_pairs : 0;
  3112. struct i40e_q_vector *q_vector = vsi->q_vectors[0];
  3113. struct i40e_pf *pf = vsi->back;
  3114. struct i40e_hw *hw = &pf->hw;
  3115. u32 val;
  3116. /* set the ITR configuration */
  3117. q_vector->rx.next_update = jiffies + 1;
  3118. q_vector->rx.target_itr = ITR_TO_REG(vsi->rx_rings[0]->itr_setting);
  3119. wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.target_itr);
  3120. q_vector->rx.current_itr = q_vector->rx.target_itr;
  3121. q_vector->tx.next_update = jiffies + 1;
  3122. q_vector->tx.target_itr = ITR_TO_REG(vsi->tx_rings[0]->itr_setting);
  3123. wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.target_itr);
  3124. q_vector->tx.current_itr = q_vector->tx.target_itr;
  3125. i40e_enable_misc_int_causes(pf);
  3126. /* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
  3127. wr32(hw, I40E_PFINT_LNKLST0, 0);
  3128. /* Associate the queue pair to the vector and enable the queue int */
  3129. val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
  3130. (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
  3131. (nextqp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
  3132. (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
  3133. wr32(hw, I40E_QINT_RQCTL(0), val);
  3134. if (i40e_enabled_xdp_vsi(vsi)) {
  3135. val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  3136. (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT)|
  3137. (I40E_QUEUE_TYPE_TX
  3138. << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
  3139. wr32(hw, I40E_QINT_TQCTL(nextqp), val);
  3140. }
  3141. val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  3142. (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
  3143. (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
  3144. wr32(hw, I40E_QINT_TQCTL(0), val);
  3145. i40e_flush(hw);
  3146. }
  3147. /**
  3148. * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
  3149. * @pf: board private structure
  3150. **/
  3151. void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
  3152. {
  3153. struct i40e_hw *hw = &pf->hw;
  3154. wr32(hw, I40E_PFINT_DYN_CTL0,
  3155. I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
  3156. i40e_flush(hw);
  3157. }
  3158. /**
  3159. * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
  3160. * @pf: board private structure
  3161. **/
  3162. void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf)
  3163. {
  3164. struct i40e_hw *hw = &pf->hw;
  3165. u32 val;
  3166. val = I40E_PFINT_DYN_CTL0_INTENA_MASK |
  3167. I40E_PFINT_DYN_CTL0_CLEARPBA_MASK |
  3168. (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
  3169. wr32(hw, I40E_PFINT_DYN_CTL0, val);
  3170. i40e_flush(hw);
  3171. }
  3172. /**
  3173. * i40e_msix_clean_rings - MSIX mode Interrupt Handler
  3174. * @irq: interrupt number
  3175. * @data: pointer to a q_vector
  3176. **/
  3177. static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
  3178. {
  3179. struct i40e_q_vector *q_vector = data;
  3180. if (!q_vector->tx.ring && !q_vector->rx.ring)
  3181. return IRQ_HANDLED;
  3182. napi_schedule_irqoff(&q_vector->napi);
  3183. return IRQ_HANDLED;
  3184. }
  3185. /**
  3186. * i40e_irq_affinity_notify - Callback for affinity changes
  3187. * @notify: context as to what irq was changed
  3188. * @mask: the new affinity mask
  3189. *
  3190. * This is a callback function used by the irq_set_affinity_notifier function
  3191. * so that we may register to receive changes to the irq affinity masks.
  3192. **/
  3193. static void i40e_irq_affinity_notify(struct irq_affinity_notify *notify,
  3194. const cpumask_t *mask)
  3195. {
  3196. struct i40e_q_vector *q_vector =
  3197. container_of(notify, struct i40e_q_vector, affinity_notify);
  3198. cpumask_copy(&q_vector->affinity_mask, mask);
  3199. }
  3200. /**
  3201. * i40e_irq_affinity_release - Callback for affinity notifier release
  3202. * @ref: internal core kernel usage
  3203. *
  3204. * This is a callback function used by the irq_set_affinity_notifier function
  3205. * to inform the current notification subscriber that they will no longer
  3206. * receive notifications.
  3207. **/
  3208. static void i40e_irq_affinity_release(struct kref *ref) {}
  3209. /**
  3210. * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
  3211. * @vsi: the VSI being configured
  3212. * @basename: name for the vector
  3213. *
  3214. * Allocates MSI-X vectors and requests interrupts from the kernel.
  3215. **/
  3216. static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
  3217. {
  3218. int q_vectors = vsi->num_q_vectors;
  3219. struct i40e_pf *pf = vsi->back;
  3220. int base = vsi->base_vector;
  3221. int rx_int_idx = 0;
  3222. int tx_int_idx = 0;
  3223. int vector, err;
  3224. int irq_num;
  3225. int cpu;
  3226. for (vector = 0; vector < q_vectors; vector++) {
  3227. struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
  3228. irq_num = pf->msix_entries[base + vector].vector;
  3229. if (q_vector->tx.ring && q_vector->rx.ring) {
  3230. snprintf(q_vector->name, sizeof(q_vector->name) - 1,
  3231. "%s-%s-%d", basename, "TxRx", rx_int_idx++);
  3232. tx_int_idx++;
  3233. } else if (q_vector->rx.ring) {
  3234. snprintf(q_vector->name, sizeof(q_vector->name) - 1,
  3235. "%s-%s-%d", basename, "rx", rx_int_idx++);
  3236. } else if (q_vector->tx.ring) {
  3237. snprintf(q_vector->name, sizeof(q_vector->name) - 1,
  3238. "%s-%s-%d", basename, "tx", tx_int_idx++);
  3239. } else {
  3240. /* skip this unused q_vector */
  3241. continue;
  3242. }
  3243. err = request_irq(irq_num,
  3244. vsi->irq_handler,
  3245. 0,
  3246. q_vector->name,
  3247. q_vector);
  3248. if (err) {
  3249. dev_info(&pf->pdev->dev,
  3250. "MSIX request_irq failed, error: %d\n", err);
  3251. goto free_queue_irqs;
  3252. }
  3253. /* register for affinity change notifications */
  3254. q_vector->affinity_notify.notify = i40e_irq_affinity_notify;
  3255. q_vector->affinity_notify.release = i40e_irq_affinity_release;
  3256. irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
  3257. /* Spread affinity hints out across online CPUs.
  3258. *
  3259. * get_cpu_mask returns a static constant mask with
  3260. * a permanent lifetime so it's ok to pass to
  3261. * irq_set_affinity_hint without making a copy.
  3262. */
  3263. cpu = cpumask_local_spread(q_vector->v_idx, -1);
  3264. irq_set_affinity_hint(irq_num, get_cpu_mask(cpu));
  3265. }
  3266. vsi->irqs_ready = true;
  3267. return 0;
  3268. free_queue_irqs:
  3269. while (vector) {
  3270. vector--;
  3271. irq_num = pf->msix_entries[base + vector].vector;
  3272. irq_set_affinity_notifier(irq_num, NULL);
  3273. irq_set_affinity_hint(irq_num, NULL);
  3274. free_irq(irq_num, &vsi->q_vectors[vector]);
  3275. }
  3276. return err;
  3277. }
  3278. /**
  3279. * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
  3280. * @vsi: the VSI being un-configured
  3281. **/
  3282. static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
  3283. {
  3284. struct i40e_pf *pf = vsi->back;
  3285. struct i40e_hw *hw = &pf->hw;
  3286. int base = vsi->base_vector;
  3287. int i;
  3288. /* disable interrupt causation from each queue */
  3289. for (i = 0; i < vsi->num_queue_pairs; i++) {
  3290. u32 val;
  3291. val = rd32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx));
  3292. val &= ~I40E_QINT_TQCTL_CAUSE_ENA_MASK;
  3293. wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), val);
  3294. val = rd32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx));
  3295. val &= ~I40E_QINT_RQCTL_CAUSE_ENA_MASK;
  3296. wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), val);
  3297. if (!i40e_enabled_xdp_vsi(vsi))
  3298. continue;
  3299. wr32(hw, I40E_QINT_TQCTL(vsi->xdp_rings[i]->reg_idx), 0);
  3300. }
  3301. /* disable each interrupt */
  3302. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  3303. for (i = vsi->base_vector;
  3304. i < (vsi->num_q_vectors + vsi->base_vector); i++)
  3305. wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
  3306. i40e_flush(hw);
  3307. for (i = 0; i < vsi->num_q_vectors; i++)
  3308. synchronize_irq(pf->msix_entries[i + base].vector);
  3309. } else {
  3310. /* Legacy and MSI mode - this stops all interrupt handling */
  3311. wr32(hw, I40E_PFINT_ICR0_ENA, 0);
  3312. wr32(hw, I40E_PFINT_DYN_CTL0, 0);
  3313. i40e_flush(hw);
  3314. synchronize_irq(pf->pdev->irq);
  3315. }
  3316. }
  3317. /**
  3318. * i40e_vsi_enable_irq - Enable IRQ for the given VSI
  3319. * @vsi: the VSI being configured
  3320. **/
  3321. static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
  3322. {
  3323. struct i40e_pf *pf = vsi->back;
  3324. int i;
  3325. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  3326. for (i = 0; i < vsi->num_q_vectors; i++)
  3327. i40e_irq_dynamic_enable(vsi, i);
  3328. } else {
  3329. i40e_irq_dynamic_enable_icr0(pf);
  3330. }
  3331. i40e_flush(&pf->hw);
  3332. return 0;
  3333. }
  3334. /**
  3335. * i40e_free_misc_vector - Free the vector that handles non-queue events
  3336. * @pf: board private structure
  3337. **/
  3338. static void i40e_free_misc_vector(struct i40e_pf *pf)
  3339. {
  3340. /* Disable ICR 0 */
  3341. wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
  3342. i40e_flush(&pf->hw);
  3343. if (pf->flags & I40E_FLAG_MSIX_ENABLED && pf->msix_entries) {
  3344. synchronize_irq(pf->msix_entries[0].vector);
  3345. free_irq(pf->msix_entries[0].vector, pf);
  3346. clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
  3347. }
  3348. }
  3349. /**
  3350. * i40e_intr - MSI/Legacy and non-queue interrupt handler
  3351. * @irq: interrupt number
  3352. * @data: pointer to a q_vector
  3353. *
  3354. * This is the handler used for all MSI/Legacy interrupts, and deals
  3355. * with both queue and non-queue interrupts. This is also used in
  3356. * MSIX mode to handle the non-queue interrupts.
  3357. **/
  3358. static irqreturn_t i40e_intr(int irq, void *data)
  3359. {
  3360. struct i40e_pf *pf = (struct i40e_pf *)data;
  3361. struct i40e_hw *hw = &pf->hw;
  3362. irqreturn_t ret = IRQ_NONE;
  3363. u32 icr0, icr0_remaining;
  3364. u32 val, ena_mask;
  3365. icr0 = rd32(hw, I40E_PFINT_ICR0);
  3366. ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
  3367. /* if sharing a legacy IRQ, we might get called w/o an intr pending */
  3368. if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
  3369. goto enable_intr;
  3370. /* if interrupt but no bits showing, must be SWINT */
  3371. if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
  3372. (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
  3373. pf->sw_int_count++;
  3374. if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
  3375. (icr0 & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
  3376. ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
  3377. dev_dbg(&pf->pdev->dev, "cleared PE_CRITERR\n");
  3378. set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
  3379. }
  3380. /* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
  3381. if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
  3382. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  3383. struct i40e_q_vector *q_vector = vsi->q_vectors[0];
  3384. /* We do not have a way to disarm Queue causes while leaving
  3385. * interrupt enabled for all other causes, ideally
  3386. * interrupt should be disabled while we are in NAPI but
  3387. * this is not a performance path and napi_schedule()
  3388. * can deal with rescheduling.
  3389. */
  3390. if (!test_bit(__I40E_DOWN, pf->state))
  3391. napi_schedule_irqoff(&q_vector->napi);
  3392. }
  3393. if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
  3394. ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
  3395. set_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
  3396. i40e_debug(&pf->hw, I40E_DEBUG_NVM, "AdminQ event\n");
  3397. }
  3398. if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
  3399. ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
  3400. set_bit(__I40E_MDD_EVENT_PENDING, pf->state);
  3401. }
  3402. if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
  3403. ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
  3404. set_bit(__I40E_VFLR_EVENT_PENDING, pf->state);
  3405. }
  3406. if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
  3407. if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
  3408. set_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
  3409. ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
  3410. val = rd32(hw, I40E_GLGEN_RSTAT);
  3411. val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
  3412. >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
  3413. if (val == I40E_RESET_CORER) {
  3414. pf->corer_count++;
  3415. } else if (val == I40E_RESET_GLOBR) {
  3416. pf->globr_count++;
  3417. } else if (val == I40E_RESET_EMPR) {
  3418. pf->empr_count++;
  3419. set_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state);
  3420. }
  3421. }
  3422. if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
  3423. icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
  3424. dev_info(&pf->pdev->dev, "HMC error interrupt\n");
  3425. dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
  3426. rd32(hw, I40E_PFHMC_ERRORINFO),
  3427. rd32(hw, I40E_PFHMC_ERRORDATA));
  3428. }
  3429. if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
  3430. u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
  3431. if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK) {
  3432. icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
  3433. i40e_ptp_tx_hwtstamp(pf);
  3434. }
  3435. }
  3436. /* If a critical error is pending we have no choice but to reset the
  3437. * device.
  3438. * Report and mask out any remaining unexpected interrupts.
  3439. */
  3440. icr0_remaining = icr0 & ena_mask;
  3441. if (icr0_remaining) {
  3442. dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
  3443. icr0_remaining);
  3444. if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
  3445. (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
  3446. (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
  3447. dev_info(&pf->pdev->dev, "device will be reset\n");
  3448. set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
  3449. i40e_service_event_schedule(pf);
  3450. }
  3451. ena_mask &= ~icr0_remaining;
  3452. }
  3453. ret = IRQ_HANDLED;
  3454. enable_intr:
  3455. /* re-enable interrupt causes */
  3456. wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
  3457. if (!test_bit(__I40E_DOWN, pf->state)) {
  3458. i40e_service_event_schedule(pf);
  3459. i40e_irq_dynamic_enable_icr0(pf);
  3460. }
  3461. return ret;
  3462. }
  3463. /**
  3464. * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
  3465. * @tx_ring: tx ring to clean
  3466. * @budget: how many cleans we're allowed
  3467. *
  3468. * Returns true if there's any budget left (e.g. the clean is finished)
  3469. **/
  3470. static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
  3471. {
  3472. struct i40e_vsi *vsi = tx_ring->vsi;
  3473. u16 i = tx_ring->next_to_clean;
  3474. struct i40e_tx_buffer *tx_buf;
  3475. struct i40e_tx_desc *tx_desc;
  3476. tx_buf = &tx_ring->tx_bi[i];
  3477. tx_desc = I40E_TX_DESC(tx_ring, i);
  3478. i -= tx_ring->count;
  3479. do {
  3480. struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
  3481. /* if next_to_watch is not set then there is no work pending */
  3482. if (!eop_desc)
  3483. break;
  3484. /* prevent any other reads prior to eop_desc */
  3485. smp_rmb();
  3486. /* if the descriptor isn't done, no work yet to do */
  3487. if (!(eop_desc->cmd_type_offset_bsz &
  3488. cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
  3489. break;
  3490. /* clear next_to_watch to prevent false hangs */
  3491. tx_buf->next_to_watch = NULL;
  3492. tx_desc->buffer_addr = 0;
  3493. tx_desc->cmd_type_offset_bsz = 0;
  3494. /* move past filter desc */
  3495. tx_buf++;
  3496. tx_desc++;
  3497. i++;
  3498. if (unlikely(!i)) {
  3499. i -= tx_ring->count;
  3500. tx_buf = tx_ring->tx_bi;
  3501. tx_desc = I40E_TX_DESC(tx_ring, 0);
  3502. }
  3503. /* unmap skb header data */
  3504. dma_unmap_single(tx_ring->dev,
  3505. dma_unmap_addr(tx_buf, dma),
  3506. dma_unmap_len(tx_buf, len),
  3507. DMA_TO_DEVICE);
  3508. if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
  3509. kfree(tx_buf->raw_buf);
  3510. tx_buf->raw_buf = NULL;
  3511. tx_buf->tx_flags = 0;
  3512. tx_buf->next_to_watch = NULL;
  3513. dma_unmap_len_set(tx_buf, len, 0);
  3514. tx_desc->buffer_addr = 0;
  3515. tx_desc->cmd_type_offset_bsz = 0;
  3516. /* move us past the eop_desc for start of next FD desc */
  3517. tx_buf++;
  3518. tx_desc++;
  3519. i++;
  3520. if (unlikely(!i)) {
  3521. i -= tx_ring->count;
  3522. tx_buf = tx_ring->tx_bi;
  3523. tx_desc = I40E_TX_DESC(tx_ring, 0);
  3524. }
  3525. /* update budget accounting */
  3526. budget--;
  3527. } while (likely(budget));
  3528. i += tx_ring->count;
  3529. tx_ring->next_to_clean = i;
  3530. if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED)
  3531. i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
  3532. return budget > 0;
  3533. }
  3534. /**
  3535. * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
  3536. * @irq: interrupt number
  3537. * @data: pointer to a q_vector
  3538. **/
  3539. static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
  3540. {
  3541. struct i40e_q_vector *q_vector = data;
  3542. struct i40e_vsi *vsi;
  3543. if (!q_vector->tx.ring)
  3544. return IRQ_HANDLED;
  3545. vsi = q_vector->tx.ring->vsi;
  3546. i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
  3547. return IRQ_HANDLED;
  3548. }
  3549. /**
  3550. * i40e_map_vector_to_qp - Assigns the queue pair to the vector
  3551. * @vsi: the VSI being configured
  3552. * @v_idx: vector index
  3553. * @qp_idx: queue pair index
  3554. **/
  3555. static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
  3556. {
  3557. struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
  3558. struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
  3559. struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
  3560. tx_ring->q_vector = q_vector;
  3561. tx_ring->next = q_vector->tx.ring;
  3562. q_vector->tx.ring = tx_ring;
  3563. q_vector->tx.count++;
  3564. /* Place XDP Tx ring in the same q_vector ring list as regular Tx */
  3565. if (i40e_enabled_xdp_vsi(vsi)) {
  3566. struct i40e_ring *xdp_ring = vsi->xdp_rings[qp_idx];
  3567. xdp_ring->q_vector = q_vector;
  3568. xdp_ring->next = q_vector->tx.ring;
  3569. q_vector->tx.ring = xdp_ring;
  3570. q_vector->tx.count++;
  3571. }
  3572. rx_ring->q_vector = q_vector;
  3573. rx_ring->next = q_vector->rx.ring;
  3574. q_vector->rx.ring = rx_ring;
  3575. q_vector->rx.count++;
  3576. }
  3577. /**
  3578. * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
  3579. * @vsi: the VSI being configured
  3580. *
  3581. * This function maps descriptor rings to the queue-specific vectors
  3582. * we were allotted through the MSI-X enabling code. Ideally, we'd have
  3583. * one vector per queue pair, but on a constrained vector budget, we
  3584. * group the queue pairs as "efficiently" as possible.
  3585. **/
  3586. static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
  3587. {
  3588. int qp_remaining = vsi->num_queue_pairs;
  3589. int q_vectors = vsi->num_q_vectors;
  3590. int num_ringpairs;
  3591. int v_start = 0;
  3592. int qp_idx = 0;
  3593. /* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
  3594. * group them so there are multiple queues per vector.
  3595. * It is also important to go through all the vectors available to be
  3596. * sure that if we don't use all the vectors, that the remaining vectors
  3597. * are cleared. This is especially important when decreasing the
  3598. * number of queues in use.
  3599. */
  3600. for (; v_start < q_vectors; v_start++) {
  3601. struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
  3602. num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
  3603. q_vector->num_ringpairs = num_ringpairs;
  3604. q_vector->reg_idx = q_vector->v_idx + vsi->base_vector - 1;
  3605. q_vector->rx.count = 0;
  3606. q_vector->tx.count = 0;
  3607. q_vector->rx.ring = NULL;
  3608. q_vector->tx.ring = NULL;
  3609. while (num_ringpairs--) {
  3610. i40e_map_vector_to_qp(vsi, v_start, qp_idx);
  3611. qp_idx++;
  3612. qp_remaining--;
  3613. }
  3614. }
  3615. }
  3616. /**
  3617. * i40e_vsi_request_irq - Request IRQ from the OS
  3618. * @vsi: the VSI being configured
  3619. * @basename: name for the vector
  3620. **/
  3621. static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
  3622. {
  3623. struct i40e_pf *pf = vsi->back;
  3624. int err;
  3625. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  3626. err = i40e_vsi_request_irq_msix(vsi, basename);
  3627. else if (pf->flags & I40E_FLAG_MSI_ENABLED)
  3628. err = request_irq(pf->pdev->irq, i40e_intr, 0,
  3629. pf->int_name, pf);
  3630. else
  3631. err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
  3632. pf->int_name, pf);
  3633. if (err)
  3634. dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
  3635. return err;
  3636. }
  3637. #ifdef CONFIG_NET_POLL_CONTROLLER
  3638. /**
  3639. * i40e_netpoll - A Polling 'interrupt' handler
  3640. * @netdev: network interface device structure
  3641. *
  3642. * This is used by netconsole to send skbs without having to re-enable
  3643. * interrupts. It's not called while the normal interrupt routine is executing.
  3644. **/
  3645. static void i40e_netpoll(struct net_device *netdev)
  3646. {
  3647. struct i40e_netdev_priv *np = netdev_priv(netdev);
  3648. struct i40e_vsi *vsi = np->vsi;
  3649. struct i40e_pf *pf = vsi->back;
  3650. int i;
  3651. /* if interface is down do nothing */
  3652. if (test_bit(__I40E_VSI_DOWN, vsi->state))
  3653. return;
  3654. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  3655. for (i = 0; i < vsi->num_q_vectors; i++)
  3656. i40e_msix_clean_rings(0, vsi->q_vectors[i]);
  3657. } else {
  3658. i40e_intr(pf->pdev->irq, netdev);
  3659. }
  3660. }
  3661. #endif
  3662. #define I40E_QTX_ENA_WAIT_COUNT 50
  3663. /**
  3664. * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
  3665. * @pf: the PF being configured
  3666. * @pf_q: the PF queue
  3667. * @enable: enable or disable state of the queue
  3668. *
  3669. * This routine will wait for the given Tx queue of the PF to reach the
  3670. * enabled or disabled state.
  3671. * Returns -ETIMEDOUT in case of failing to reach the requested state after
  3672. * multiple retries; else will return 0 in case of success.
  3673. **/
  3674. static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
  3675. {
  3676. int i;
  3677. u32 tx_reg;
  3678. for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
  3679. tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
  3680. if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
  3681. break;
  3682. usleep_range(10, 20);
  3683. }
  3684. if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
  3685. return -ETIMEDOUT;
  3686. return 0;
  3687. }
  3688. /**
  3689. * i40e_control_tx_q - Start or stop a particular Tx queue
  3690. * @pf: the PF structure
  3691. * @pf_q: the PF queue to configure
  3692. * @enable: start or stop the queue
  3693. *
  3694. * This function enables or disables a single queue. Note that any delay
  3695. * required after the operation is expected to be handled by the caller of
  3696. * this function.
  3697. **/
  3698. static void i40e_control_tx_q(struct i40e_pf *pf, int pf_q, bool enable)
  3699. {
  3700. struct i40e_hw *hw = &pf->hw;
  3701. u32 tx_reg;
  3702. int i;
  3703. /* warn the TX unit of coming changes */
  3704. i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
  3705. if (!enable)
  3706. usleep_range(10, 20);
  3707. for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
  3708. tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
  3709. if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
  3710. ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
  3711. break;
  3712. usleep_range(1000, 2000);
  3713. }
  3714. /* Skip if the queue is already in the requested state */
  3715. if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
  3716. return;
  3717. /* turn on/off the queue */
  3718. if (enable) {
  3719. wr32(hw, I40E_QTX_HEAD(pf_q), 0);
  3720. tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
  3721. } else {
  3722. tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
  3723. }
  3724. wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
  3725. }
  3726. /**
  3727. * i40e_control_wait_tx_q - Start/stop Tx queue and wait for completion
  3728. * @seid: VSI SEID
  3729. * @pf: the PF structure
  3730. * @pf_q: the PF queue to configure
  3731. * @is_xdp: true if the queue is used for XDP
  3732. * @enable: start or stop the queue
  3733. **/
  3734. int i40e_control_wait_tx_q(int seid, struct i40e_pf *pf, int pf_q,
  3735. bool is_xdp, bool enable)
  3736. {
  3737. int ret;
  3738. i40e_control_tx_q(pf, pf_q, enable);
  3739. /* wait for the change to finish */
  3740. ret = i40e_pf_txq_wait(pf, pf_q, enable);
  3741. if (ret) {
  3742. dev_info(&pf->pdev->dev,
  3743. "VSI seid %d %sTx ring %d %sable timeout\n",
  3744. seid, (is_xdp ? "XDP " : ""), pf_q,
  3745. (enable ? "en" : "dis"));
  3746. }
  3747. return ret;
  3748. }
  3749. /**
  3750. * i40e_vsi_control_tx - Start or stop a VSI's rings
  3751. * @vsi: the VSI being configured
  3752. * @enable: start or stop the rings
  3753. **/
  3754. static int i40e_vsi_control_tx(struct i40e_vsi *vsi, bool enable)
  3755. {
  3756. struct i40e_pf *pf = vsi->back;
  3757. int i, pf_q, ret = 0;
  3758. pf_q = vsi->base_queue;
  3759. for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
  3760. ret = i40e_control_wait_tx_q(vsi->seid, pf,
  3761. pf_q,
  3762. false /*is xdp*/, enable);
  3763. if (ret)
  3764. break;
  3765. if (!i40e_enabled_xdp_vsi(vsi))
  3766. continue;
  3767. ret = i40e_control_wait_tx_q(vsi->seid, pf,
  3768. pf_q + vsi->alloc_queue_pairs,
  3769. true /*is xdp*/, enable);
  3770. if (ret)
  3771. break;
  3772. }
  3773. return ret;
  3774. }
  3775. /**
  3776. * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
  3777. * @pf: the PF being configured
  3778. * @pf_q: the PF queue
  3779. * @enable: enable or disable state of the queue
  3780. *
  3781. * This routine will wait for the given Rx queue of the PF to reach the
  3782. * enabled or disabled state.
  3783. * Returns -ETIMEDOUT in case of failing to reach the requested state after
  3784. * multiple retries; else will return 0 in case of success.
  3785. **/
  3786. static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
  3787. {
  3788. int i;
  3789. u32 rx_reg;
  3790. for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
  3791. rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
  3792. if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
  3793. break;
  3794. usleep_range(10, 20);
  3795. }
  3796. if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
  3797. return -ETIMEDOUT;
  3798. return 0;
  3799. }
  3800. /**
  3801. * i40e_control_rx_q - Start or stop a particular Rx queue
  3802. * @pf: the PF structure
  3803. * @pf_q: the PF queue to configure
  3804. * @enable: start or stop the queue
  3805. *
  3806. * This function enables or disables a single queue. Note that
  3807. * any delay required after the operation is expected to be
  3808. * handled by the caller of this function.
  3809. **/
  3810. static void i40e_control_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
  3811. {
  3812. struct i40e_hw *hw = &pf->hw;
  3813. u32 rx_reg;
  3814. int i;
  3815. for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
  3816. rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
  3817. if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
  3818. ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
  3819. break;
  3820. usleep_range(1000, 2000);
  3821. }
  3822. /* Skip if the queue is already in the requested state */
  3823. if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
  3824. return;
  3825. /* turn on/off the queue */
  3826. if (enable)
  3827. rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
  3828. else
  3829. rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
  3830. wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
  3831. }
  3832. /**
  3833. * i40e_control_wait_rx_q
  3834. * @pf: the PF structure
  3835. * @pf_q: queue being configured
  3836. * @enable: start or stop the rings
  3837. *
  3838. * This function enables or disables a single queue along with waiting
  3839. * for the change to finish. The caller of this function should handle
  3840. * the delays needed in the case of disabling queues.
  3841. **/
  3842. int i40e_control_wait_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
  3843. {
  3844. int ret = 0;
  3845. i40e_control_rx_q(pf, pf_q, enable);
  3846. /* wait for the change to finish */
  3847. ret = i40e_pf_rxq_wait(pf, pf_q, enable);
  3848. if (ret)
  3849. return ret;
  3850. return ret;
  3851. }
  3852. /**
  3853. * i40e_vsi_control_rx - Start or stop a VSI's rings
  3854. * @vsi: the VSI being configured
  3855. * @enable: start or stop the rings
  3856. **/
  3857. static int i40e_vsi_control_rx(struct i40e_vsi *vsi, bool enable)
  3858. {
  3859. struct i40e_pf *pf = vsi->back;
  3860. int i, pf_q, ret = 0;
  3861. pf_q = vsi->base_queue;
  3862. for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
  3863. ret = i40e_control_wait_rx_q(pf, pf_q, enable);
  3864. if (ret) {
  3865. dev_info(&pf->pdev->dev,
  3866. "VSI seid %d Rx ring %d %sable timeout\n",
  3867. vsi->seid, pf_q, (enable ? "en" : "dis"));
  3868. break;
  3869. }
  3870. }
  3871. /* Due to HW errata, on Rx disable only, the register can indicate done
  3872. * before it really is. Needs 50ms to be sure
  3873. */
  3874. if (!enable)
  3875. mdelay(50);
  3876. return ret;
  3877. }
  3878. /**
  3879. * i40e_vsi_start_rings - Start a VSI's rings
  3880. * @vsi: the VSI being configured
  3881. **/
  3882. int i40e_vsi_start_rings(struct i40e_vsi *vsi)
  3883. {
  3884. int ret = 0;
  3885. /* do rx first for enable and last for disable */
  3886. ret = i40e_vsi_control_rx(vsi, true);
  3887. if (ret)
  3888. return ret;
  3889. ret = i40e_vsi_control_tx(vsi, true);
  3890. return ret;
  3891. }
  3892. /**
  3893. * i40e_vsi_stop_rings - Stop a VSI's rings
  3894. * @vsi: the VSI being configured
  3895. **/
  3896. void i40e_vsi_stop_rings(struct i40e_vsi *vsi)
  3897. {
  3898. /* When port TX is suspended, don't wait */
  3899. if (test_bit(__I40E_PORT_SUSPENDED, vsi->back->state))
  3900. return i40e_vsi_stop_rings_no_wait(vsi);
  3901. /* do rx first for enable and last for disable
  3902. * Ignore return value, we need to shutdown whatever we can
  3903. */
  3904. i40e_vsi_control_tx(vsi, false);
  3905. i40e_vsi_control_rx(vsi, false);
  3906. }
  3907. /**
  3908. * i40e_vsi_stop_rings_no_wait - Stop a VSI's rings and do not delay
  3909. * @vsi: the VSI being shutdown
  3910. *
  3911. * This function stops all the rings for a VSI but does not delay to verify
  3912. * that rings have been disabled. It is expected that the caller is shutting
  3913. * down multiple VSIs at once and will delay together for all the VSIs after
  3914. * initiating the shutdown. This is particularly useful for shutting down lots
  3915. * of VFs together. Otherwise, a large delay can be incurred while configuring
  3916. * each VSI in serial.
  3917. **/
  3918. void i40e_vsi_stop_rings_no_wait(struct i40e_vsi *vsi)
  3919. {
  3920. struct i40e_pf *pf = vsi->back;
  3921. int i, pf_q;
  3922. pf_q = vsi->base_queue;
  3923. for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
  3924. i40e_control_tx_q(pf, pf_q, false);
  3925. i40e_control_rx_q(pf, pf_q, false);
  3926. }
  3927. }
  3928. /**
  3929. * i40e_vsi_free_irq - Free the irq association with the OS
  3930. * @vsi: the VSI being configured
  3931. **/
  3932. static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
  3933. {
  3934. struct i40e_pf *pf = vsi->back;
  3935. struct i40e_hw *hw = &pf->hw;
  3936. int base = vsi->base_vector;
  3937. u32 val, qp;
  3938. int i;
  3939. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  3940. if (!vsi->q_vectors)
  3941. return;
  3942. if (!vsi->irqs_ready)
  3943. return;
  3944. vsi->irqs_ready = false;
  3945. for (i = 0; i < vsi->num_q_vectors; i++) {
  3946. int irq_num;
  3947. u16 vector;
  3948. vector = i + base;
  3949. irq_num = pf->msix_entries[vector].vector;
  3950. /* free only the irqs that were actually requested */
  3951. if (!vsi->q_vectors[i] ||
  3952. !vsi->q_vectors[i]->num_ringpairs)
  3953. continue;
  3954. /* clear the affinity notifier in the IRQ descriptor */
  3955. irq_set_affinity_notifier(irq_num, NULL);
  3956. /* remove our suggested affinity mask for this IRQ */
  3957. irq_set_affinity_hint(irq_num, NULL);
  3958. synchronize_irq(irq_num);
  3959. free_irq(irq_num, vsi->q_vectors[i]);
  3960. /* Tear down the interrupt queue link list
  3961. *
  3962. * We know that they come in pairs and always
  3963. * the Rx first, then the Tx. To clear the
  3964. * link list, stick the EOL value into the
  3965. * next_q field of the registers.
  3966. */
  3967. val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
  3968. qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
  3969. >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
  3970. val |= I40E_QUEUE_END_OF_LIST
  3971. << I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
  3972. wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
  3973. while (qp != I40E_QUEUE_END_OF_LIST) {
  3974. u32 next;
  3975. val = rd32(hw, I40E_QINT_RQCTL(qp));
  3976. val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
  3977. I40E_QINT_RQCTL_MSIX0_INDX_MASK |
  3978. I40E_QINT_RQCTL_CAUSE_ENA_MASK |
  3979. I40E_QINT_RQCTL_INTEVENT_MASK);
  3980. val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
  3981. I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
  3982. wr32(hw, I40E_QINT_RQCTL(qp), val);
  3983. val = rd32(hw, I40E_QINT_TQCTL(qp));
  3984. next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
  3985. >> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
  3986. val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
  3987. I40E_QINT_TQCTL_MSIX0_INDX_MASK |
  3988. I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  3989. I40E_QINT_TQCTL_INTEVENT_MASK);
  3990. val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
  3991. I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
  3992. wr32(hw, I40E_QINT_TQCTL(qp), val);
  3993. qp = next;
  3994. }
  3995. }
  3996. } else {
  3997. free_irq(pf->pdev->irq, pf);
  3998. val = rd32(hw, I40E_PFINT_LNKLST0);
  3999. qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
  4000. >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
  4001. val |= I40E_QUEUE_END_OF_LIST
  4002. << I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
  4003. wr32(hw, I40E_PFINT_LNKLST0, val);
  4004. val = rd32(hw, I40E_QINT_RQCTL(qp));
  4005. val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
  4006. I40E_QINT_RQCTL_MSIX0_INDX_MASK |
  4007. I40E_QINT_RQCTL_CAUSE_ENA_MASK |
  4008. I40E_QINT_RQCTL_INTEVENT_MASK);
  4009. val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
  4010. I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
  4011. wr32(hw, I40E_QINT_RQCTL(qp), val);
  4012. val = rd32(hw, I40E_QINT_TQCTL(qp));
  4013. val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
  4014. I40E_QINT_TQCTL_MSIX0_INDX_MASK |
  4015. I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  4016. I40E_QINT_TQCTL_INTEVENT_MASK);
  4017. val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
  4018. I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
  4019. wr32(hw, I40E_QINT_TQCTL(qp), val);
  4020. }
  4021. }
  4022. /**
  4023. * i40e_free_q_vector - Free memory allocated for specific interrupt vector
  4024. * @vsi: the VSI being configured
  4025. * @v_idx: Index of vector to be freed
  4026. *
  4027. * This function frees the memory allocated to the q_vector. In addition if
  4028. * NAPI is enabled it will delete any references to the NAPI struct prior
  4029. * to freeing the q_vector.
  4030. **/
  4031. static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
  4032. {
  4033. struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
  4034. struct i40e_ring *ring;
  4035. if (!q_vector)
  4036. return;
  4037. /* disassociate q_vector from rings */
  4038. i40e_for_each_ring(ring, q_vector->tx)
  4039. ring->q_vector = NULL;
  4040. i40e_for_each_ring(ring, q_vector->rx)
  4041. ring->q_vector = NULL;
  4042. /* only VSI w/ an associated netdev is set up w/ NAPI */
  4043. if (vsi->netdev)
  4044. netif_napi_del(&q_vector->napi);
  4045. vsi->q_vectors[v_idx] = NULL;
  4046. kfree_rcu(q_vector, rcu);
  4047. }
  4048. /**
  4049. * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
  4050. * @vsi: the VSI being un-configured
  4051. *
  4052. * This frees the memory allocated to the q_vectors and
  4053. * deletes references to the NAPI struct.
  4054. **/
  4055. static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
  4056. {
  4057. int v_idx;
  4058. for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
  4059. i40e_free_q_vector(vsi, v_idx);
  4060. }
  4061. /**
  4062. * i40e_reset_interrupt_capability - Disable interrupt setup in OS
  4063. * @pf: board private structure
  4064. **/
  4065. static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
  4066. {
  4067. /* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
  4068. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  4069. pci_disable_msix(pf->pdev);
  4070. kfree(pf->msix_entries);
  4071. pf->msix_entries = NULL;
  4072. kfree(pf->irq_pile);
  4073. pf->irq_pile = NULL;
  4074. } else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
  4075. pci_disable_msi(pf->pdev);
  4076. }
  4077. pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
  4078. }
  4079. /**
  4080. * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
  4081. * @pf: board private structure
  4082. *
  4083. * We go through and clear interrupt specific resources and reset the structure
  4084. * to pre-load conditions
  4085. **/
  4086. static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
  4087. {
  4088. int i;
  4089. i40e_free_misc_vector(pf);
  4090. i40e_put_lump(pf->irq_pile, pf->iwarp_base_vector,
  4091. I40E_IWARP_IRQ_PILE_ID);
  4092. i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
  4093. for (i = 0; i < pf->num_alloc_vsi; i++)
  4094. if (pf->vsi[i])
  4095. i40e_vsi_free_q_vectors(pf->vsi[i]);
  4096. i40e_reset_interrupt_capability(pf);
  4097. }
  4098. /**
  4099. * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
  4100. * @vsi: the VSI being configured
  4101. **/
  4102. static void i40e_napi_enable_all(struct i40e_vsi *vsi)
  4103. {
  4104. int q_idx;
  4105. if (!vsi->netdev)
  4106. return;
  4107. for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
  4108. struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
  4109. if (q_vector->rx.ring || q_vector->tx.ring)
  4110. napi_enable(&q_vector->napi);
  4111. }
  4112. }
  4113. /**
  4114. * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
  4115. * @vsi: the VSI being configured
  4116. **/
  4117. static void i40e_napi_disable_all(struct i40e_vsi *vsi)
  4118. {
  4119. int q_idx;
  4120. if (!vsi->netdev)
  4121. return;
  4122. for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
  4123. struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
  4124. if (q_vector->rx.ring || q_vector->tx.ring)
  4125. napi_disable(&q_vector->napi);
  4126. }
  4127. }
  4128. /**
  4129. * i40e_vsi_close - Shut down a VSI
  4130. * @vsi: the vsi to be quelled
  4131. **/
  4132. static void i40e_vsi_close(struct i40e_vsi *vsi)
  4133. {
  4134. struct i40e_pf *pf = vsi->back;
  4135. if (!test_and_set_bit(__I40E_VSI_DOWN, vsi->state))
  4136. i40e_down(vsi);
  4137. i40e_vsi_free_irq(vsi);
  4138. i40e_vsi_free_tx_resources(vsi);
  4139. i40e_vsi_free_rx_resources(vsi);
  4140. vsi->current_netdev_flags = 0;
  4141. set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
  4142. if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
  4143. set_bit(__I40E_CLIENT_RESET, pf->state);
  4144. }
  4145. /**
  4146. * i40e_quiesce_vsi - Pause a given VSI
  4147. * @vsi: the VSI being paused
  4148. **/
  4149. static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
  4150. {
  4151. if (test_bit(__I40E_VSI_DOWN, vsi->state))
  4152. return;
  4153. set_bit(__I40E_VSI_NEEDS_RESTART, vsi->state);
  4154. if (vsi->netdev && netif_running(vsi->netdev))
  4155. vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
  4156. else
  4157. i40e_vsi_close(vsi);
  4158. }
  4159. /**
  4160. * i40e_unquiesce_vsi - Resume a given VSI
  4161. * @vsi: the VSI being resumed
  4162. **/
  4163. static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
  4164. {
  4165. if (!test_and_clear_bit(__I40E_VSI_NEEDS_RESTART, vsi->state))
  4166. return;
  4167. if (vsi->netdev && netif_running(vsi->netdev))
  4168. vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
  4169. else
  4170. i40e_vsi_open(vsi); /* this clears the DOWN bit */
  4171. }
  4172. /**
  4173. * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
  4174. * @pf: the PF
  4175. **/
  4176. static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
  4177. {
  4178. int v;
  4179. for (v = 0; v < pf->num_alloc_vsi; v++) {
  4180. if (pf->vsi[v])
  4181. i40e_quiesce_vsi(pf->vsi[v]);
  4182. }
  4183. }
  4184. /**
  4185. * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
  4186. * @pf: the PF
  4187. **/
  4188. static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
  4189. {
  4190. int v;
  4191. for (v = 0; v < pf->num_alloc_vsi; v++) {
  4192. if (pf->vsi[v])
  4193. i40e_unquiesce_vsi(pf->vsi[v]);
  4194. }
  4195. }
  4196. /**
  4197. * i40e_vsi_wait_queues_disabled - Wait for VSI's queues to be disabled
  4198. * @vsi: the VSI being configured
  4199. *
  4200. * Wait until all queues on a given VSI have been disabled.
  4201. **/
  4202. int i40e_vsi_wait_queues_disabled(struct i40e_vsi *vsi)
  4203. {
  4204. struct i40e_pf *pf = vsi->back;
  4205. int i, pf_q, ret;
  4206. pf_q = vsi->base_queue;
  4207. for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
  4208. /* Check and wait for the Tx queue */
  4209. ret = i40e_pf_txq_wait(pf, pf_q, false);
  4210. if (ret) {
  4211. dev_info(&pf->pdev->dev,
  4212. "VSI seid %d Tx ring %d disable timeout\n",
  4213. vsi->seid, pf_q);
  4214. return ret;
  4215. }
  4216. if (!i40e_enabled_xdp_vsi(vsi))
  4217. goto wait_rx;
  4218. /* Check and wait for the XDP Tx queue */
  4219. ret = i40e_pf_txq_wait(pf, pf_q + vsi->alloc_queue_pairs,
  4220. false);
  4221. if (ret) {
  4222. dev_info(&pf->pdev->dev,
  4223. "VSI seid %d XDP Tx ring %d disable timeout\n",
  4224. vsi->seid, pf_q);
  4225. return ret;
  4226. }
  4227. wait_rx:
  4228. /* Check and wait for the Rx queue */
  4229. ret = i40e_pf_rxq_wait(pf, pf_q, false);
  4230. if (ret) {
  4231. dev_info(&pf->pdev->dev,
  4232. "VSI seid %d Rx ring %d disable timeout\n",
  4233. vsi->seid, pf_q);
  4234. return ret;
  4235. }
  4236. }
  4237. return 0;
  4238. }
  4239. #ifdef CONFIG_I40E_DCB
  4240. /**
  4241. * i40e_pf_wait_queues_disabled - Wait for all queues of PF VSIs to be disabled
  4242. * @pf: the PF
  4243. *
  4244. * This function waits for the queues to be in disabled state for all the
  4245. * VSIs that are managed by this PF.
  4246. **/
  4247. static int i40e_pf_wait_queues_disabled(struct i40e_pf *pf)
  4248. {
  4249. int v, ret = 0;
  4250. for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
  4251. if (pf->vsi[v]) {
  4252. ret = i40e_vsi_wait_queues_disabled(pf->vsi[v]);
  4253. if (ret)
  4254. break;
  4255. }
  4256. }
  4257. return ret;
  4258. }
  4259. #endif
  4260. /**
  4261. * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
  4262. * @pf: pointer to PF
  4263. *
  4264. * Get TC map for ISCSI PF type that will include iSCSI TC
  4265. * and LAN TC.
  4266. **/
  4267. static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
  4268. {
  4269. struct i40e_dcb_app_priority_table app;
  4270. struct i40e_hw *hw = &pf->hw;
  4271. u8 enabled_tc = 1; /* TC0 is always enabled */
  4272. u8 tc, i;
  4273. /* Get the iSCSI APP TLV */
  4274. struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
  4275. for (i = 0; i < dcbcfg->numapps; i++) {
  4276. app = dcbcfg->app[i];
  4277. if (app.selector == I40E_APP_SEL_TCPIP &&
  4278. app.protocolid == I40E_APP_PROTOID_ISCSI) {
  4279. tc = dcbcfg->etscfg.prioritytable[app.priority];
  4280. enabled_tc |= BIT(tc);
  4281. break;
  4282. }
  4283. }
  4284. return enabled_tc;
  4285. }
  4286. /**
  4287. * i40e_dcb_get_num_tc - Get the number of TCs from DCBx config
  4288. * @dcbcfg: the corresponding DCBx configuration structure
  4289. *
  4290. * Return the number of TCs from given DCBx configuration
  4291. **/
  4292. static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
  4293. {
  4294. int i, tc_unused = 0;
  4295. u8 num_tc = 0;
  4296. u8 ret = 0;
  4297. /* Scan the ETS Config Priority Table to find
  4298. * traffic class enabled for a given priority
  4299. * and create a bitmask of enabled TCs
  4300. */
  4301. for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
  4302. num_tc |= BIT(dcbcfg->etscfg.prioritytable[i]);
  4303. /* Now scan the bitmask to check for
  4304. * contiguous TCs starting with TC0
  4305. */
  4306. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4307. if (num_tc & BIT(i)) {
  4308. if (!tc_unused) {
  4309. ret++;
  4310. } else {
  4311. pr_err("Non-contiguous TC - Disabling DCB\n");
  4312. return 1;
  4313. }
  4314. } else {
  4315. tc_unused = 1;
  4316. }
  4317. }
  4318. /* There is always at least TC0 */
  4319. if (!ret)
  4320. ret = 1;
  4321. return ret;
  4322. }
  4323. /**
  4324. * i40e_dcb_get_enabled_tc - Get enabled traffic classes
  4325. * @dcbcfg: the corresponding DCBx configuration structure
  4326. *
  4327. * Query the current DCB configuration and return the number of
  4328. * traffic classes enabled from the given DCBX config
  4329. **/
  4330. static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
  4331. {
  4332. u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
  4333. u8 enabled_tc = 1;
  4334. u8 i;
  4335. for (i = 0; i < num_tc; i++)
  4336. enabled_tc |= BIT(i);
  4337. return enabled_tc;
  4338. }
  4339. /**
  4340. * i40e_mqprio_get_enabled_tc - Get enabled traffic classes
  4341. * @pf: PF being queried
  4342. *
  4343. * Query the current MQPRIO configuration and return the number of
  4344. * traffic classes enabled.
  4345. **/
  4346. static u8 i40e_mqprio_get_enabled_tc(struct i40e_pf *pf)
  4347. {
  4348. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  4349. u8 num_tc = vsi->mqprio_qopt.qopt.num_tc;
  4350. u8 enabled_tc = 1, i;
  4351. for (i = 1; i < num_tc; i++)
  4352. enabled_tc |= BIT(i);
  4353. return enabled_tc;
  4354. }
  4355. /**
  4356. * i40e_pf_get_num_tc - Get enabled traffic classes for PF
  4357. * @pf: PF being queried
  4358. *
  4359. * Return number of traffic classes enabled for the given PF
  4360. **/
  4361. static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
  4362. {
  4363. struct i40e_hw *hw = &pf->hw;
  4364. u8 i, enabled_tc = 1;
  4365. u8 num_tc = 0;
  4366. struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
  4367. if (pf->flags & I40E_FLAG_TC_MQPRIO)
  4368. return pf->vsi[pf->lan_vsi]->mqprio_qopt.qopt.num_tc;
  4369. /* If neither MQPRIO nor DCB is enabled, then always use single TC */
  4370. if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
  4371. return 1;
  4372. /* SFP mode will be enabled for all TCs on port */
  4373. if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
  4374. return i40e_dcb_get_num_tc(dcbcfg);
  4375. /* MFP mode return count of enabled TCs for this PF */
  4376. if (pf->hw.func_caps.iscsi)
  4377. enabled_tc = i40e_get_iscsi_tc_map(pf);
  4378. else
  4379. return 1; /* Only TC0 */
  4380. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4381. if (enabled_tc & BIT(i))
  4382. num_tc++;
  4383. }
  4384. return num_tc;
  4385. }
  4386. /**
  4387. * i40e_pf_get_pf_tc_map - Get bitmap for enabled traffic classes
  4388. * @pf: PF being queried
  4389. *
  4390. * Return a bitmap for enabled traffic classes for this PF.
  4391. **/
  4392. static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
  4393. {
  4394. if (pf->flags & I40E_FLAG_TC_MQPRIO)
  4395. return i40e_mqprio_get_enabled_tc(pf);
  4396. /* If neither MQPRIO nor DCB is enabled for this PF then just return
  4397. * default TC
  4398. */
  4399. if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
  4400. return I40E_DEFAULT_TRAFFIC_CLASS;
  4401. /* SFP mode we want PF to be enabled for all TCs */
  4402. if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
  4403. return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
  4404. /* MFP enabled and iSCSI PF type */
  4405. if (pf->hw.func_caps.iscsi)
  4406. return i40e_get_iscsi_tc_map(pf);
  4407. else
  4408. return I40E_DEFAULT_TRAFFIC_CLASS;
  4409. }
  4410. /**
  4411. * i40e_vsi_get_bw_info - Query VSI BW Information
  4412. * @vsi: the VSI being queried
  4413. *
  4414. * Returns 0 on success, negative value on failure
  4415. **/
  4416. static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
  4417. {
  4418. struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
  4419. struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
  4420. struct i40e_pf *pf = vsi->back;
  4421. struct i40e_hw *hw = &pf->hw;
  4422. i40e_status ret;
  4423. u32 tc_bw_max;
  4424. int i;
  4425. /* Get the VSI level BW configuration */
  4426. ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
  4427. if (ret) {
  4428. dev_info(&pf->pdev->dev,
  4429. "couldn't get PF vsi bw config, err %s aq_err %s\n",
  4430. i40e_stat_str(&pf->hw, ret),
  4431. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  4432. return -EINVAL;
  4433. }
  4434. /* Get the VSI level BW configuration per TC */
  4435. ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
  4436. NULL);
  4437. if (ret) {
  4438. dev_info(&pf->pdev->dev,
  4439. "couldn't get PF vsi ets bw config, err %s aq_err %s\n",
  4440. i40e_stat_str(&pf->hw, ret),
  4441. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  4442. return -EINVAL;
  4443. }
  4444. if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
  4445. dev_info(&pf->pdev->dev,
  4446. "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
  4447. bw_config.tc_valid_bits,
  4448. bw_ets_config.tc_valid_bits);
  4449. /* Still continuing */
  4450. }
  4451. vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
  4452. vsi->bw_max_quanta = bw_config.max_bw;
  4453. tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
  4454. (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
  4455. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4456. vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
  4457. vsi->bw_ets_limit_credits[i] =
  4458. le16_to_cpu(bw_ets_config.credits[i]);
  4459. /* 3 bits out of 4 for each TC */
  4460. vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
  4461. }
  4462. return 0;
  4463. }
  4464. /**
  4465. * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
  4466. * @vsi: the VSI being configured
  4467. * @enabled_tc: TC bitmap
  4468. * @bw_share: BW shared credits per TC
  4469. *
  4470. * Returns 0 on success, negative value on failure
  4471. **/
  4472. static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
  4473. u8 *bw_share)
  4474. {
  4475. struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
  4476. i40e_status ret;
  4477. int i;
  4478. if (vsi->back->flags & I40E_FLAG_TC_MQPRIO)
  4479. return 0;
  4480. if (!vsi->mqprio_qopt.qopt.hw) {
  4481. ret = i40e_set_bw_limit(vsi, vsi->seid, 0);
  4482. if (ret)
  4483. dev_info(&vsi->back->pdev->dev,
  4484. "Failed to reset tx rate for vsi->seid %u\n",
  4485. vsi->seid);
  4486. return ret;
  4487. }
  4488. bw_data.tc_valid_bits = enabled_tc;
  4489. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
  4490. bw_data.tc_bw_credits[i] = bw_share[i];
  4491. ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, vsi->seid, &bw_data,
  4492. NULL);
  4493. if (ret) {
  4494. dev_info(&vsi->back->pdev->dev,
  4495. "AQ command Config VSI BW allocation per TC failed = %d\n",
  4496. vsi->back->hw.aq.asq_last_status);
  4497. return -EINVAL;
  4498. }
  4499. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
  4500. vsi->info.qs_handle[i] = bw_data.qs_handles[i];
  4501. return 0;
  4502. }
  4503. /**
  4504. * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
  4505. * @vsi: the VSI being configured
  4506. * @enabled_tc: TC map to be enabled
  4507. *
  4508. **/
  4509. static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
  4510. {
  4511. struct net_device *netdev = vsi->netdev;
  4512. struct i40e_pf *pf = vsi->back;
  4513. struct i40e_hw *hw = &pf->hw;
  4514. u8 netdev_tc = 0;
  4515. int i;
  4516. struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
  4517. if (!netdev)
  4518. return;
  4519. if (!enabled_tc) {
  4520. netdev_reset_tc(netdev);
  4521. return;
  4522. }
  4523. /* Set up actual enabled TCs on the VSI */
  4524. if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
  4525. return;
  4526. /* set per TC queues for the VSI */
  4527. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4528. /* Only set TC queues for enabled tcs
  4529. *
  4530. * e.g. For a VSI that has TC0 and TC3 enabled the
  4531. * enabled_tc bitmap would be 0x00001001; the driver
  4532. * will set the numtc for netdev as 2 that will be
  4533. * referenced by the netdev layer as TC 0 and 1.
  4534. */
  4535. if (vsi->tc_config.enabled_tc & BIT(i))
  4536. netdev_set_tc_queue(netdev,
  4537. vsi->tc_config.tc_info[i].netdev_tc,
  4538. vsi->tc_config.tc_info[i].qcount,
  4539. vsi->tc_config.tc_info[i].qoffset);
  4540. }
  4541. if (pf->flags & I40E_FLAG_TC_MQPRIO)
  4542. return;
  4543. /* Assign UP2TC map for the VSI */
  4544. for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
  4545. /* Get the actual TC# for the UP */
  4546. u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
  4547. /* Get the mapped netdev TC# for the UP */
  4548. netdev_tc = vsi->tc_config.tc_info[ets_tc].netdev_tc;
  4549. netdev_set_prio_tc_map(netdev, i, netdev_tc);
  4550. }
  4551. }
  4552. /**
  4553. * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
  4554. * @vsi: the VSI being configured
  4555. * @ctxt: the ctxt buffer returned from AQ VSI update param command
  4556. **/
  4557. static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
  4558. struct i40e_vsi_context *ctxt)
  4559. {
  4560. /* copy just the sections touched not the entire info
  4561. * since not all sections are valid as returned by
  4562. * update vsi params
  4563. */
  4564. vsi->info.mapping_flags = ctxt->info.mapping_flags;
  4565. memcpy(&vsi->info.queue_mapping,
  4566. &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
  4567. memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
  4568. sizeof(vsi->info.tc_mapping));
  4569. }
  4570. /**
  4571. * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
  4572. * @vsi: VSI to be configured
  4573. * @enabled_tc: TC bitmap
  4574. *
  4575. * This configures a particular VSI for TCs that are mapped to the
  4576. * given TC bitmap. It uses default bandwidth share for TCs across
  4577. * VSIs to configure TC for a particular VSI.
  4578. *
  4579. * NOTE:
  4580. * It is expected that the VSI queues have been quisced before calling
  4581. * this function.
  4582. **/
  4583. static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
  4584. {
  4585. u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
  4586. struct i40e_pf *pf = vsi->back;
  4587. struct i40e_hw *hw = &pf->hw;
  4588. struct i40e_vsi_context ctxt;
  4589. int ret = 0;
  4590. int i;
  4591. /* Check if enabled_tc is same as existing or new TCs */
  4592. if (vsi->tc_config.enabled_tc == enabled_tc &&
  4593. vsi->mqprio_qopt.mode != TC_MQPRIO_MODE_CHANNEL)
  4594. return ret;
  4595. /* Enable ETS TCs with equal BW Share for now across all VSIs */
  4596. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4597. if (enabled_tc & BIT(i))
  4598. bw_share[i] = 1;
  4599. }
  4600. ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
  4601. if (ret) {
  4602. struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
  4603. dev_info(&pf->pdev->dev,
  4604. "Failed configuring TC map %d for VSI %d\n",
  4605. enabled_tc, vsi->seid);
  4606. ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid,
  4607. &bw_config, NULL);
  4608. if (ret) {
  4609. dev_info(&pf->pdev->dev,
  4610. "Failed querying vsi bw info, err %s aq_err %s\n",
  4611. i40e_stat_str(hw, ret),
  4612. i40e_aq_str(hw, hw->aq.asq_last_status));
  4613. goto out;
  4614. }
  4615. if ((bw_config.tc_valid_bits & enabled_tc) != enabled_tc) {
  4616. u8 valid_tc = bw_config.tc_valid_bits & enabled_tc;
  4617. if (!valid_tc)
  4618. valid_tc = bw_config.tc_valid_bits;
  4619. /* Always enable TC0, no matter what */
  4620. valid_tc |= 1;
  4621. dev_info(&pf->pdev->dev,
  4622. "Requested tc 0x%x, but FW reports 0x%x as valid. Attempting to use 0x%x.\n",
  4623. enabled_tc, bw_config.tc_valid_bits, valid_tc);
  4624. enabled_tc = valid_tc;
  4625. }
  4626. ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
  4627. if (ret) {
  4628. dev_err(&pf->pdev->dev,
  4629. "Unable to configure TC map %d for VSI %d\n",
  4630. enabled_tc, vsi->seid);
  4631. goto out;
  4632. }
  4633. }
  4634. /* Update Queue Pairs Mapping for currently enabled UPs */
  4635. ctxt.seid = vsi->seid;
  4636. ctxt.pf_num = vsi->back->hw.pf_id;
  4637. ctxt.vf_num = 0;
  4638. ctxt.uplink_seid = vsi->uplink_seid;
  4639. ctxt.info = vsi->info;
  4640. if (vsi->back->flags & I40E_FLAG_TC_MQPRIO) {
  4641. ret = i40e_vsi_setup_queue_map_mqprio(vsi, &ctxt, enabled_tc);
  4642. if (ret)
  4643. goto out;
  4644. } else {
  4645. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
  4646. }
  4647. /* On destroying the qdisc, reset vsi->rss_size, as number of enabled
  4648. * queues changed.
  4649. */
  4650. if (!vsi->mqprio_qopt.qopt.hw && vsi->reconfig_rss) {
  4651. vsi->rss_size = min_t(int, vsi->back->alloc_rss_size,
  4652. vsi->num_queue_pairs);
  4653. ret = i40e_vsi_config_rss(vsi);
  4654. if (ret) {
  4655. dev_info(&vsi->back->pdev->dev,
  4656. "Failed to reconfig rss for num_queues\n");
  4657. return ret;
  4658. }
  4659. vsi->reconfig_rss = false;
  4660. }
  4661. if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
  4662. ctxt.info.valid_sections |=
  4663. cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
  4664. ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
  4665. }
  4666. /* Update the VSI after updating the VSI queue-mapping
  4667. * information
  4668. */
  4669. ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
  4670. if (ret) {
  4671. dev_info(&pf->pdev->dev,
  4672. "Update vsi tc config failed, err %s aq_err %s\n",
  4673. i40e_stat_str(hw, ret),
  4674. i40e_aq_str(hw, hw->aq.asq_last_status));
  4675. goto out;
  4676. }
  4677. /* update the local VSI info with updated queue map */
  4678. i40e_vsi_update_queue_map(vsi, &ctxt);
  4679. vsi->info.valid_sections = 0;
  4680. /* Update current VSI BW information */
  4681. ret = i40e_vsi_get_bw_info(vsi);
  4682. if (ret) {
  4683. dev_info(&pf->pdev->dev,
  4684. "Failed updating vsi bw info, err %s aq_err %s\n",
  4685. i40e_stat_str(hw, ret),
  4686. i40e_aq_str(hw, hw->aq.asq_last_status));
  4687. goto out;
  4688. }
  4689. /* Update the netdev TC setup */
  4690. i40e_vsi_config_netdev_tc(vsi, enabled_tc);
  4691. out:
  4692. return ret;
  4693. }
  4694. /**
  4695. * i40e_get_link_speed - Returns link speed for the interface
  4696. * @vsi: VSI to be configured
  4697. *
  4698. **/
  4699. static int i40e_get_link_speed(struct i40e_vsi *vsi)
  4700. {
  4701. struct i40e_pf *pf = vsi->back;
  4702. switch (pf->hw.phy.link_info.link_speed) {
  4703. case I40E_LINK_SPEED_40GB:
  4704. return 40000;
  4705. case I40E_LINK_SPEED_25GB:
  4706. return 25000;
  4707. case I40E_LINK_SPEED_20GB:
  4708. return 20000;
  4709. case I40E_LINK_SPEED_10GB:
  4710. return 10000;
  4711. case I40E_LINK_SPEED_1GB:
  4712. return 1000;
  4713. default:
  4714. return -EINVAL;
  4715. }
  4716. }
  4717. /**
  4718. * i40e_set_bw_limit - setup BW limit for Tx traffic based on max_tx_rate
  4719. * @vsi: VSI to be configured
  4720. * @seid: seid of the channel/VSI
  4721. * @max_tx_rate: max TX rate to be configured as BW limit
  4722. *
  4723. * Helper function to set BW limit for a given VSI
  4724. **/
  4725. int i40e_set_bw_limit(struct i40e_vsi *vsi, u16 seid, u64 max_tx_rate)
  4726. {
  4727. struct i40e_pf *pf = vsi->back;
  4728. u64 credits = 0;
  4729. int speed = 0;
  4730. int ret = 0;
  4731. speed = i40e_get_link_speed(vsi);
  4732. if (max_tx_rate > speed) {
  4733. dev_err(&pf->pdev->dev,
  4734. "Invalid max tx rate %llu specified for VSI seid %d.",
  4735. max_tx_rate, seid);
  4736. return -EINVAL;
  4737. }
  4738. if (max_tx_rate && max_tx_rate < 50) {
  4739. dev_warn(&pf->pdev->dev,
  4740. "Setting max tx rate to minimum usable value of 50Mbps.\n");
  4741. max_tx_rate = 50;
  4742. }
  4743. /* Tx rate credits are in values of 50Mbps, 0 is disabled */
  4744. credits = max_tx_rate;
  4745. do_div(credits, I40E_BW_CREDIT_DIVISOR);
  4746. ret = i40e_aq_config_vsi_bw_limit(&pf->hw, seid, credits,
  4747. I40E_MAX_BW_INACTIVE_ACCUM, NULL);
  4748. if (ret)
  4749. dev_err(&pf->pdev->dev,
  4750. "Failed set tx rate (%llu Mbps) for vsi->seid %u, err %s aq_err %s\n",
  4751. max_tx_rate, seid, i40e_stat_str(&pf->hw, ret),
  4752. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  4753. return ret;
  4754. }
  4755. /**
  4756. * i40e_remove_queue_channels - Remove queue channels for the TCs
  4757. * @vsi: VSI to be configured
  4758. *
  4759. * Remove queue channels for the TCs
  4760. **/
  4761. static void i40e_remove_queue_channels(struct i40e_vsi *vsi)
  4762. {
  4763. enum i40e_admin_queue_err last_aq_status;
  4764. struct i40e_cloud_filter *cfilter;
  4765. struct i40e_channel *ch, *ch_tmp;
  4766. struct i40e_pf *pf = vsi->back;
  4767. struct hlist_node *node;
  4768. int ret, i;
  4769. /* Reset rss size that was stored when reconfiguring rss for
  4770. * channel VSIs with non-power-of-2 queue count.
  4771. */
  4772. vsi->current_rss_size = 0;
  4773. /* perform cleanup for channels if they exist */
  4774. if (list_empty(&vsi->ch_list))
  4775. return;
  4776. list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
  4777. struct i40e_vsi *p_vsi;
  4778. list_del(&ch->list);
  4779. p_vsi = ch->parent_vsi;
  4780. if (!p_vsi || !ch->initialized) {
  4781. kfree(ch);
  4782. continue;
  4783. }
  4784. /* Reset queue contexts */
  4785. for (i = 0; i < ch->num_queue_pairs; i++) {
  4786. struct i40e_ring *tx_ring, *rx_ring;
  4787. u16 pf_q;
  4788. pf_q = ch->base_queue + i;
  4789. tx_ring = vsi->tx_rings[pf_q];
  4790. tx_ring->ch = NULL;
  4791. rx_ring = vsi->rx_rings[pf_q];
  4792. rx_ring->ch = NULL;
  4793. }
  4794. /* Reset BW configured for this VSI via mqprio */
  4795. ret = i40e_set_bw_limit(vsi, ch->seid, 0);
  4796. if (ret)
  4797. dev_info(&vsi->back->pdev->dev,
  4798. "Failed to reset tx rate for ch->seid %u\n",
  4799. ch->seid);
  4800. /* delete cloud filters associated with this channel */
  4801. hlist_for_each_entry_safe(cfilter, node,
  4802. &pf->cloud_filter_list, cloud_node) {
  4803. if (cfilter->seid != ch->seid)
  4804. continue;
  4805. hash_del(&cfilter->cloud_node);
  4806. if (cfilter->dst_port)
  4807. ret = i40e_add_del_cloud_filter_big_buf(vsi,
  4808. cfilter,
  4809. false);
  4810. else
  4811. ret = i40e_add_del_cloud_filter(vsi, cfilter,
  4812. false);
  4813. last_aq_status = pf->hw.aq.asq_last_status;
  4814. if (ret)
  4815. dev_info(&pf->pdev->dev,
  4816. "Failed to delete cloud filter, err %s aq_err %s\n",
  4817. i40e_stat_str(&pf->hw, ret),
  4818. i40e_aq_str(&pf->hw, last_aq_status));
  4819. kfree(cfilter);
  4820. }
  4821. /* delete VSI from FW */
  4822. ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
  4823. NULL);
  4824. if (ret)
  4825. dev_err(&vsi->back->pdev->dev,
  4826. "unable to remove channel (%d) for parent VSI(%d)\n",
  4827. ch->seid, p_vsi->seid);
  4828. kfree(ch);
  4829. }
  4830. INIT_LIST_HEAD(&vsi->ch_list);
  4831. }
  4832. /**
  4833. * i40e_is_any_channel - channel exist or not
  4834. * @vsi: ptr to VSI to which channels are associated with
  4835. *
  4836. * Returns true or false if channel(s) exist for associated VSI or not
  4837. **/
  4838. static bool i40e_is_any_channel(struct i40e_vsi *vsi)
  4839. {
  4840. struct i40e_channel *ch, *ch_tmp;
  4841. list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
  4842. if (ch->initialized)
  4843. return true;
  4844. }
  4845. return false;
  4846. }
  4847. /**
  4848. * i40e_get_max_queues_for_channel
  4849. * @vsi: ptr to VSI to which channels are associated with
  4850. *
  4851. * Helper function which returns max value among the queue counts set on the
  4852. * channels/TCs created.
  4853. **/
  4854. static int i40e_get_max_queues_for_channel(struct i40e_vsi *vsi)
  4855. {
  4856. struct i40e_channel *ch, *ch_tmp;
  4857. int max = 0;
  4858. list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
  4859. if (!ch->initialized)
  4860. continue;
  4861. if (ch->num_queue_pairs > max)
  4862. max = ch->num_queue_pairs;
  4863. }
  4864. return max;
  4865. }
  4866. /**
  4867. * i40e_validate_num_queues - validate num_queues w.r.t channel
  4868. * @pf: ptr to PF device
  4869. * @num_queues: number of queues
  4870. * @vsi: the parent VSI
  4871. * @reconfig_rss: indicates should the RSS be reconfigured or not
  4872. *
  4873. * This function validates number of queues in the context of new channel
  4874. * which is being established and determines if RSS should be reconfigured
  4875. * or not for parent VSI.
  4876. **/
  4877. static int i40e_validate_num_queues(struct i40e_pf *pf, int num_queues,
  4878. struct i40e_vsi *vsi, bool *reconfig_rss)
  4879. {
  4880. int max_ch_queues;
  4881. if (!reconfig_rss)
  4882. return -EINVAL;
  4883. *reconfig_rss = false;
  4884. if (vsi->current_rss_size) {
  4885. if (num_queues > vsi->current_rss_size) {
  4886. dev_dbg(&pf->pdev->dev,
  4887. "Error: num_queues (%d) > vsi's current_size(%d)\n",
  4888. num_queues, vsi->current_rss_size);
  4889. return -EINVAL;
  4890. } else if ((num_queues < vsi->current_rss_size) &&
  4891. (!is_power_of_2(num_queues))) {
  4892. dev_dbg(&pf->pdev->dev,
  4893. "Error: num_queues (%d) < vsi's current_size(%d), but not power of 2\n",
  4894. num_queues, vsi->current_rss_size);
  4895. return -EINVAL;
  4896. }
  4897. }
  4898. if (!is_power_of_2(num_queues)) {
  4899. /* Find the max num_queues configured for channel if channel
  4900. * exist.
  4901. * if channel exist, then enforce 'num_queues' to be more than
  4902. * max ever queues configured for channel.
  4903. */
  4904. max_ch_queues = i40e_get_max_queues_for_channel(vsi);
  4905. if (num_queues < max_ch_queues) {
  4906. dev_dbg(&pf->pdev->dev,
  4907. "Error: num_queues (%d) < max queues configured for channel(%d)\n",
  4908. num_queues, max_ch_queues);
  4909. return -EINVAL;
  4910. }
  4911. *reconfig_rss = true;
  4912. }
  4913. return 0;
  4914. }
  4915. /**
  4916. * i40e_vsi_reconfig_rss - reconfig RSS based on specified rss_size
  4917. * @vsi: the VSI being setup
  4918. * @rss_size: size of RSS, accordingly LUT gets reprogrammed
  4919. *
  4920. * This function reconfigures RSS by reprogramming LUTs using 'rss_size'
  4921. **/
  4922. static int i40e_vsi_reconfig_rss(struct i40e_vsi *vsi, u16 rss_size)
  4923. {
  4924. struct i40e_pf *pf = vsi->back;
  4925. u8 seed[I40E_HKEY_ARRAY_SIZE];
  4926. struct i40e_hw *hw = &pf->hw;
  4927. int local_rss_size;
  4928. u8 *lut;
  4929. int ret;
  4930. if (!vsi->rss_size)
  4931. return -EINVAL;
  4932. if (rss_size > vsi->rss_size)
  4933. return -EINVAL;
  4934. local_rss_size = min_t(int, vsi->rss_size, rss_size);
  4935. lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
  4936. if (!lut)
  4937. return -ENOMEM;
  4938. /* Ignoring user configured lut if there is one */
  4939. i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, local_rss_size);
  4940. /* Use user configured hash key if there is one, otherwise
  4941. * use default.
  4942. */
  4943. if (vsi->rss_hkey_user)
  4944. memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
  4945. else
  4946. netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
  4947. ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
  4948. if (ret) {
  4949. dev_info(&pf->pdev->dev,
  4950. "Cannot set RSS lut, err %s aq_err %s\n",
  4951. i40e_stat_str(hw, ret),
  4952. i40e_aq_str(hw, hw->aq.asq_last_status));
  4953. kfree(lut);
  4954. return ret;
  4955. }
  4956. kfree(lut);
  4957. /* Do the update w.r.t. storing rss_size */
  4958. if (!vsi->orig_rss_size)
  4959. vsi->orig_rss_size = vsi->rss_size;
  4960. vsi->current_rss_size = local_rss_size;
  4961. return ret;
  4962. }
  4963. /**
  4964. * i40e_channel_setup_queue_map - Setup a channel queue map
  4965. * @pf: ptr to PF device
  4966. * @vsi: the VSI being setup
  4967. * @ctxt: VSI context structure
  4968. * @ch: ptr to channel structure
  4969. *
  4970. * Setup queue map for a specific channel
  4971. **/
  4972. static void i40e_channel_setup_queue_map(struct i40e_pf *pf,
  4973. struct i40e_vsi_context *ctxt,
  4974. struct i40e_channel *ch)
  4975. {
  4976. u16 qcount, qmap, sections = 0;
  4977. u8 offset = 0;
  4978. int pow;
  4979. sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
  4980. sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
  4981. qcount = min_t(int, ch->num_queue_pairs, pf->num_lan_msix);
  4982. ch->num_queue_pairs = qcount;
  4983. /* find the next higher power-of-2 of num queue pairs */
  4984. pow = ilog2(qcount);
  4985. if (!is_power_of_2(qcount))
  4986. pow++;
  4987. qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
  4988. (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
  4989. /* Setup queue TC[0].qmap for given VSI context */
  4990. ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
  4991. ctxt->info.up_enable_bits = 0x1; /* TC0 enabled */
  4992. ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
  4993. ctxt->info.queue_mapping[0] = cpu_to_le16(ch->base_queue);
  4994. ctxt->info.valid_sections |= cpu_to_le16(sections);
  4995. }
  4996. /**
  4997. * i40e_add_channel - add a channel by adding VSI
  4998. * @pf: ptr to PF device
  4999. * @uplink_seid: underlying HW switching element (VEB) ID
  5000. * @ch: ptr to channel structure
  5001. *
  5002. * Add a channel (VSI) using add_vsi and queue_map
  5003. **/
  5004. static int i40e_add_channel(struct i40e_pf *pf, u16 uplink_seid,
  5005. struct i40e_channel *ch)
  5006. {
  5007. struct i40e_hw *hw = &pf->hw;
  5008. struct i40e_vsi_context ctxt;
  5009. u8 enabled_tc = 0x1; /* TC0 enabled */
  5010. int ret;
  5011. if (ch->type != I40E_VSI_VMDQ2) {
  5012. dev_info(&pf->pdev->dev,
  5013. "add new vsi failed, ch->type %d\n", ch->type);
  5014. return -EINVAL;
  5015. }
  5016. memset(&ctxt, 0, sizeof(ctxt));
  5017. ctxt.pf_num = hw->pf_id;
  5018. ctxt.vf_num = 0;
  5019. ctxt.uplink_seid = uplink_seid;
  5020. ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
  5021. if (ch->type == I40E_VSI_VMDQ2)
  5022. ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
  5023. if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED) {
  5024. ctxt.info.valid_sections |=
  5025. cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  5026. ctxt.info.switch_id =
  5027. cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  5028. }
  5029. /* Set queue map for a given VSI context */
  5030. i40e_channel_setup_queue_map(pf, &ctxt, ch);
  5031. /* Now time to create VSI */
  5032. ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
  5033. if (ret) {
  5034. dev_info(&pf->pdev->dev,
  5035. "add new vsi failed, err %s aq_err %s\n",
  5036. i40e_stat_str(&pf->hw, ret),
  5037. i40e_aq_str(&pf->hw,
  5038. pf->hw.aq.asq_last_status));
  5039. return -ENOENT;
  5040. }
  5041. /* Success, update channel */
  5042. ch->enabled_tc = enabled_tc;
  5043. ch->seid = ctxt.seid;
  5044. ch->vsi_number = ctxt.vsi_number;
  5045. ch->stat_counter_idx = cpu_to_le16(ctxt.info.stat_counter_idx);
  5046. /* copy just the sections touched not the entire info
  5047. * since not all sections are valid as returned by
  5048. * update vsi params
  5049. */
  5050. ch->info.mapping_flags = ctxt.info.mapping_flags;
  5051. memcpy(&ch->info.queue_mapping,
  5052. &ctxt.info.queue_mapping, sizeof(ctxt.info.queue_mapping));
  5053. memcpy(&ch->info.tc_mapping, ctxt.info.tc_mapping,
  5054. sizeof(ctxt.info.tc_mapping));
  5055. return 0;
  5056. }
  5057. static int i40e_channel_config_bw(struct i40e_vsi *vsi, struct i40e_channel *ch,
  5058. u8 *bw_share)
  5059. {
  5060. struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
  5061. i40e_status ret;
  5062. int i;
  5063. bw_data.tc_valid_bits = ch->enabled_tc;
  5064. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
  5065. bw_data.tc_bw_credits[i] = bw_share[i];
  5066. ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, ch->seid,
  5067. &bw_data, NULL);
  5068. if (ret) {
  5069. dev_info(&vsi->back->pdev->dev,
  5070. "Config VSI BW allocation per TC failed, aq_err: %d for new_vsi->seid %u\n",
  5071. vsi->back->hw.aq.asq_last_status, ch->seid);
  5072. return -EINVAL;
  5073. }
  5074. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
  5075. ch->info.qs_handle[i] = bw_data.qs_handles[i];
  5076. return 0;
  5077. }
  5078. /**
  5079. * i40e_channel_config_tx_ring - config TX ring associated with new channel
  5080. * @pf: ptr to PF device
  5081. * @vsi: the VSI being setup
  5082. * @ch: ptr to channel structure
  5083. *
  5084. * Configure TX rings associated with channel (VSI) since queues are being
  5085. * from parent VSI.
  5086. **/
  5087. static int i40e_channel_config_tx_ring(struct i40e_pf *pf,
  5088. struct i40e_vsi *vsi,
  5089. struct i40e_channel *ch)
  5090. {
  5091. i40e_status ret;
  5092. int i;
  5093. u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
  5094. /* Enable ETS TCs with equal BW Share for now across all VSIs */
  5095. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  5096. if (ch->enabled_tc & BIT(i))
  5097. bw_share[i] = 1;
  5098. }
  5099. /* configure BW for new VSI */
  5100. ret = i40e_channel_config_bw(vsi, ch, bw_share);
  5101. if (ret) {
  5102. dev_info(&vsi->back->pdev->dev,
  5103. "Failed configuring TC map %d for channel (seid %u)\n",
  5104. ch->enabled_tc, ch->seid);
  5105. return ret;
  5106. }
  5107. for (i = 0; i < ch->num_queue_pairs; i++) {
  5108. struct i40e_ring *tx_ring, *rx_ring;
  5109. u16 pf_q;
  5110. pf_q = ch->base_queue + i;
  5111. /* Get to TX ring ptr of main VSI, for re-setup TX queue
  5112. * context
  5113. */
  5114. tx_ring = vsi->tx_rings[pf_q];
  5115. tx_ring->ch = ch;
  5116. /* Get the RX ring ptr */
  5117. rx_ring = vsi->rx_rings[pf_q];
  5118. rx_ring->ch = ch;
  5119. }
  5120. return 0;
  5121. }
  5122. /**
  5123. * i40e_setup_hw_channel - setup new channel
  5124. * @pf: ptr to PF device
  5125. * @vsi: the VSI being setup
  5126. * @ch: ptr to channel structure
  5127. * @uplink_seid: underlying HW switching element (VEB) ID
  5128. * @type: type of channel to be created (VMDq2/VF)
  5129. *
  5130. * Setup new channel (VSI) based on specified type (VMDq2/VF)
  5131. * and configures TX rings accordingly
  5132. **/
  5133. static inline int i40e_setup_hw_channel(struct i40e_pf *pf,
  5134. struct i40e_vsi *vsi,
  5135. struct i40e_channel *ch,
  5136. u16 uplink_seid, u8 type)
  5137. {
  5138. int ret;
  5139. ch->initialized = false;
  5140. ch->base_queue = vsi->next_base_queue;
  5141. ch->type = type;
  5142. /* Proceed with creation of channel (VMDq2) VSI */
  5143. ret = i40e_add_channel(pf, uplink_seid, ch);
  5144. if (ret) {
  5145. dev_info(&pf->pdev->dev,
  5146. "failed to add_channel using uplink_seid %u\n",
  5147. uplink_seid);
  5148. return ret;
  5149. }
  5150. /* Mark the successful creation of channel */
  5151. ch->initialized = true;
  5152. /* Reconfigure TX queues using QTX_CTL register */
  5153. ret = i40e_channel_config_tx_ring(pf, vsi, ch);
  5154. if (ret) {
  5155. dev_info(&pf->pdev->dev,
  5156. "failed to configure TX rings for channel %u\n",
  5157. ch->seid);
  5158. return ret;
  5159. }
  5160. /* update 'next_base_queue' */
  5161. vsi->next_base_queue = vsi->next_base_queue + ch->num_queue_pairs;
  5162. dev_dbg(&pf->pdev->dev,
  5163. "Added channel: vsi_seid %u, vsi_number %u, stat_counter_idx %u, num_queue_pairs %u, pf->next_base_queue %d\n",
  5164. ch->seid, ch->vsi_number, ch->stat_counter_idx,
  5165. ch->num_queue_pairs,
  5166. vsi->next_base_queue);
  5167. return ret;
  5168. }
  5169. /**
  5170. * i40e_setup_channel - setup new channel using uplink element
  5171. * @pf: ptr to PF device
  5172. * @type: type of channel to be created (VMDq2/VF)
  5173. * @uplink_seid: underlying HW switching element (VEB) ID
  5174. * @ch: ptr to channel structure
  5175. *
  5176. * Setup new channel (VSI) based on specified type (VMDq2/VF)
  5177. * and uplink switching element (uplink_seid)
  5178. **/
  5179. static bool i40e_setup_channel(struct i40e_pf *pf, struct i40e_vsi *vsi,
  5180. struct i40e_channel *ch)
  5181. {
  5182. u8 vsi_type;
  5183. u16 seid;
  5184. int ret;
  5185. if (vsi->type == I40E_VSI_MAIN) {
  5186. vsi_type = I40E_VSI_VMDQ2;
  5187. } else {
  5188. dev_err(&pf->pdev->dev, "unsupported parent vsi type(%d)\n",
  5189. vsi->type);
  5190. return false;
  5191. }
  5192. /* underlying switching element */
  5193. seid = pf->vsi[pf->lan_vsi]->uplink_seid;
  5194. /* create channel (VSI), configure TX rings */
  5195. ret = i40e_setup_hw_channel(pf, vsi, ch, seid, vsi_type);
  5196. if (ret) {
  5197. dev_err(&pf->pdev->dev, "failed to setup hw_channel\n");
  5198. return false;
  5199. }
  5200. return ch->initialized ? true : false;
  5201. }
  5202. /**
  5203. * i40e_validate_and_set_switch_mode - sets up switch mode correctly
  5204. * @vsi: ptr to VSI which has PF backing
  5205. *
  5206. * Sets up switch mode correctly if it needs to be changed and perform
  5207. * what are allowed modes.
  5208. **/
  5209. static int i40e_validate_and_set_switch_mode(struct i40e_vsi *vsi)
  5210. {
  5211. u8 mode;
  5212. struct i40e_pf *pf = vsi->back;
  5213. struct i40e_hw *hw = &pf->hw;
  5214. int ret;
  5215. ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_dev_capabilities);
  5216. if (ret)
  5217. return -EINVAL;
  5218. if (hw->dev_caps.switch_mode) {
  5219. /* if switch mode is set, support mode2 (non-tunneled for
  5220. * cloud filter) for now
  5221. */
  5222. u32 switch_mode = hw->dev_caps.switch_mode &
  5223. I40E_SWITCH_MODE_MASK;
  5224. if (switch_mode >= I40E_CLOUD_FILTER_MODE1) {
  5225. if (switch_mode == I40E_CLOUD_FILTER_MODE2)
  5226. return 0;
  5227. dev_err(&pf->pdev->dev,
  5228. "Invalid switch_mode (%d), only non-tunneled mode for cloud filter is supported\n",
  5229. hw->dev_caps.switch_mode);
  5230. return -EINVAL;
  5231. }
  5232. }
  5233. /* Set Bit 7 to be valid */
  5234. mode = I40E_AQ_SET_SWITCH_BIT7_VALID;
  5235. /* Set L4type for TCP support */
  5236. mode |= I40E_AQ_SET_SWITCH_L4_TYPE_TCP;
  5237. /* Set cloud filter mode */
  5238. mode |= I40E_AQ_SET_SWITCH_MODE_NON_TUNNEL;
  5239. /* Prep mode field for set_switch_config */
  5240. ret = i40e_aq_set_switch_config(hw, pf->last_sw_conf_flags,
  5241. pf->last_sw_conf_valid_flags,
  5242. mode, NULL);
  5243. if (ret && hw->aq.asq_last_status != I40E_AQ_RC_ESRCH)
  5244. dev_err(&pf->pdev->dev,
  5245. "couldn't set switch config bits, err %s aq_err %s\n",
  5246. i40e_stat_str(hw, ret),
  5247. i40e_aq_str(hw,
  5248. hw->aq.asq_last_status));
  5249. return ret;
  5250. }
  5251. /**
  5252. * i40e_create_queue_channel - function to create channel
  5253. * @vsi: VSI to be configured
  5254. * @ch: ptr to channel (it contains channel specific params)
  5255. *
  5256. * This function creates channel (VSI) using num_queues specified by user,
  5257. * reconfigs RSS if needed.
  5258. **/
  5259. int i40e_create_queue_channel(struct i40e_vsi *vsi,
  5260. struct i40e_channel *ch)
  5261. {
  5262. struct i40e_pf *pf = vsi->back;
  5263. bool reconfig_rss;
  5264. int err;
  5265. if (!ch)
  5266. return -EINVAL;
  5267. if (!ch->num_queue_pairs) {
  5268. dev_err(&pf->pdev->dev, "Invalid num_queues requested: %d\n",
  5269. ch->num_queue_pairs);
  5270. return -EINVAL;
  5271. }
  5272. /* validate user requested num_queues for channel */
  5273. err = i40e_validate_num_queues(pf, ch->num_queue_pairs, vsi,
  5274. &reconfig_rss);
  5275. if (err) {
  5276. dev_info(&pf->pdev->dev, "Failed to validate num_queues (%d)\n",
  5277. ch->num_queue_pairs);
  5278. return -EINVAL;
  5279. }
  5280. /* By default we are in VEPA mode, if this is the first VF/VMDq
  5281. * VSI to be added switch to VEB mode.
  5282. */
  5283. if ((!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) ||
  5284. (!i40e_is_any_channel(vsi))) {
  5285. if (!is_power_of_2(vsi->tc_config.tc_info[0].qcount)) {
  5286. dev_dbg(&pf->pdev->dev,
  5287. "Failed to create channel. Override queues (%u) not power of 2\n",
  5288. vsi->tc_config.tc_info[0].qcount);
  5289. return -EINVAL;
  5290. }
  5291. if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
  5292. pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
  5293. if (vsi->type == I40E_VSI_MAIN) {
  5294. if (pf->flags & I40E_FLAG_TC_MQPRIO)
  5295. i40e_do_reset(pf, I40E_PF_RESET_FLAG,
  5296. true);
  5297. else
  5298. i40e_do_reset_safe(pf,
  5299. I40E_PF_RESET_FLAG);
  5300. }
  5301. }
  5302. /* now onwards for main VSI, number of queues will be value
  5303. * of TC0's queue count
  5304. */
  5305. }
  5306. /* By this time, vsi->cnt_q_avail shall be set to non-zero and
  5307. * it should be more than num_queues
  5308. */
  5309. if (!vsi->cnt_q_avail || vsi->cnt_q_avail < ch->num_queue_pairs) {
  5310. dev_dbg(&pf->pdev->dev,
  5311. "Error: cnt_q_avail (%u) less than num_queues %d\n",
  5312. vsi->cnt_q_avail, ch->num_queue_pairs);
  5313. return -EINVAL;
  5314. }
  5315. /* reconfig_rss only if vsi type is MAIN_VSI */
  5316. if (reconfig_rss && (vsi->type == I40E_VSI_MAIN)) {
  5317. err = i40e_vsi_reconfig_rss(vsi, ch->num_queue_pairs);
  5318. if (err) {
  5319. dev_info(&pf->pdev->dev,
  5320. "Error: unable to reconfig rss for num_queues (%u)\n",
  5321. ch->num_queue_pairs);
  5322. return -EINVAL;
  5323. }
  5324. }
  5325. if (!i40e_setup_channel(pf, vsi, ch)) {
  5326. dev_info(&pf->pdev->dev, "Failed to setup channel\n");
  5327. return -EINVAL;
  5328. }
  5329. dev_info(&pf->pdev->dev,
  5330. "Setup channel (id:%u) utilizing num_queues %d\n",
  5331. ch->seid, ch->num_queue_pairs);
  5332. /* configure VSI for BW limit */
  5333. if (ch->max_tx_rate) {
  5334. u64 credits = ch->max_tx_rate;
  5335. if (i40e_set_bw_limit(vsi, ch->seid, ch->max_tx_rate))
  5336. return -EINVAL;
  5337. do_div(credits, I40E_BW_CREDIT_DIVISOR);
  5338. dev_dbg(&pf->pdev->dev,
  5339. "Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
  5340. ch->max_tx_rate,
  5341. credits,
  5342. ch->seid);
  5343. }
  5344. /* in case of VF, this will be main SRIOV VSI */
  5345. ch->parent_vsi = vsi;
  5346. /* and update main_vsi's count for queue_available to use */
  5347. vsi->cnt_q_avail -= ch->num_queue_pairs;
  5348. return 0;
  5349. }
  5350. /**
  5351. * i40e_configure_queue_channels - Add queue channel for the given TCs
  5352. * @vsi: VSI to be configured
  5353. *
  5354. * Configures queue channel mapping to the given TCs
  5355. **/
  5356. static int i40e_configure_queue_channels(struct i40e_vsi *vsi)
  5357. {
  5358. struct i40e_channel *ch;
  5359. u64 max_rate = 0;
  5360. int ret = 0, i;
  5361. /* Create app vsi with the TCs. Main VSI with TC0 is already set up */
  5362. vsi->tc_seid_map[0] = vsi->seid;
  5363. for (i = 1; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  5364. if (vsi->tc_config.enabled_tc & BIT(i)) {
  5365. ch = kzalloc(sizeof(*ch), GFP_KERNEL);
  5366. if (!ch) {
  5367. ret = -ENOMEM;
  5368. goto err_free;
  5369. }
  5370. INIT_LIST_HEAD(&ch->list);
  5371. ch->num_queue_pairs =
  5372. vsi->tc_config.tc_info[i].qcount;
  5373. ch->base_queue =
  5374. vsi->tc_config.tc_info[i].qoffset;
  5375. /* Bandwidth limit through tc interface is in bytes/s,
  5376. * change to Mbit/s
  5377. */
  5378. max_rate = vsi->mqprio_qopt.max_rate[i];
  5379. do_div(max_rate, I40E_BW_MBPS_DIVISOR);
  5380. ch->max_tx_rate = max_rate;
  5381. list_add_tail(&ch->list, &vsi->ch_list);
  5382. ret = i40e_create_queue_channel(vsi, ch);
  5383. if (ret) {
  5384. dev_err(&vsi->back->pdev->dev,
  5385. "Failed creating queue channel with TC%d: queues %d\n",
  5386. i, ch->num_queue_pairs);
  5387. goto err_free;
  5388. }
  5389. vsi->tc_seid_map[i] = ch->seid;
  5390. }
  5391. }
  5392. return ret;
  5393. err_free:
  5394. i40e_remove_queue_channels(vsi);
  5395. return ret;
  5396. }
  5397. /**
  5398. * i40e_veb_config_tc - Configure TCs for given VEB
  5399. * @veb: given VEB
  5400. * @enabled_tc: TC bitmap
  5401. *
  5402. * Configures given TC bitmap for VEB (switching) element
  5403. **/
  5404. int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
  5405. {
  5406. struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
  5407. struct i40e_pf *pf = veb->pf;
  5408. int ret = 0;
  5409. int i;
  5410. /* No TCs or already enabled TCs just return */
  5411. if (!enabled_tc || veb->enabled_tc == enabled_tc)
  5412. return ret;
  5413. bw_data.tc_valid_bits = enabled_tc;
  5414. /* bw_data.absolute_credits is not set (relative) */
  5415. /* Enable ETS TCs with equal BW Share for now */
  5416. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  5417. if (enabled_tc & BIT(i))
  5418. bw_data.tc_bw_share_credits[i] = 1;
  5419. }
  5420. ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
  5421. &bw_data, NULL);
  5422. if (ret) {
  5423. dev_info(&pf->pdev->dev,
  5424. "VEB bw config failed, err %s aq_err %s\n",
  5425. i40e_stat_str(&pf->hw, ret),
  5426. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  5427. goto out;
  5428. }
  5429. /* Update the BW information */
  5430. ret = i40e_veb_get_bw_info(veb);
  5431. if (ret) {
  5432. dev_info(&pf->pdev->dev,
  5433. "Failed getting veb bw config, err %s aq_err %s\n",
  5434. i40e_stat_str(&pf->hw, ret),
  5435. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  5436. }
  5437. out:
  5438. return ret;
  5439. }
  5440. #ifdef CONFIG_I40E_DCB
  5441. /**
  5442. * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
  5443. * @pf: PF struct
  5444. *
  5445. * Reconfigure VEB/VSIs on a given PF; it is assumed that
  5446. * the caller would've quiesce all the VSIs before calling
  5447. * this function
  5448. **/
  5449. static void i40e_dcb_reconfigure(struct i40e_pf *pf)
  5450. {
  5451. u8 tc_map = 0;
  5452. int ret;
  5453. u8 v;
  5454. /* Enable the TCs available on PF to all VEBs */
  5455. tc_map = i40e_pf_get_tc_map(pf);
  5456. for (v = 0; v < I40E_MAX_VEB; v++) {
  5457. if (!pf->veb[v])
  5458. continue;
  5459. ret = i40e_veb_config_tc(pf->veb[v], tc_map);
  5460. if (ret) {
  5461. dev_info(&pf->pdev->dev,
  5462. "Failed configuring TC for VEB seid=%d\n",
  5463. pf->veb[v]->seid);
  5464. /* Will try to configure as many components */
  5465. }
  5466. }
  5467. /* Update each VSI */
  5468. for (v = 0; v < pf->num_alloc_vsi; v++) {
  5469. if (!pf->vsi[v])
  5470. continue;
  5471. /* - Enable all TCs for the LAN VSI
  5472. * - For all others keep them at TC0 for now
  5473. */
  5474. if (v == pf->lan_vsi)
  5475. tc_map = i40e_pf_get_tc_map(pf);
  5476. else
  5477. tc_map = I40E_DEFAULT_TRAFFIC_CLASS;
  5478. ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
  5479. if (ret) {
  5480. dev_info(&pf->pdev->dev,
  5481. "Failed configuring TC for VSI seid=%d\n",
  5482. pf->vsi[v]->seid);
  5483. /* Will try to configure as many components */
  5484. } else {
  5485. /* Re-configure VSI vectors based on updated TC map */
  5486. i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
  5487. if (pf->vsi[v]->netdev)
  5488. i40e_dcbnl_set_all(pf->vsi[v]);
  5489. }
  5490. }
  5491. }
  5492. /**
  5493. * i40e_resume_port_tx - Resume port Tx
  5494. * @pf: PF struct
  5495. *
  5496. * Resume a port's Tx and issue a PF reset in case of failure to
  5497. * resume.
  5498. **/
  5499. static int i40e_resume_port_tx(struct i40e_pf *pf)
  5500. {
  5501. struct i40e_hw *hw = &pf->hw;
  5502. int ret;
  5503. ret = i40e_aq_resume_port_tx(hw, NULL);
  5504. if (ret) {
  5505. dev_info(&pf->pdev->dev,
  5506. "Resume Port Tx failed, err %s aq_err %s\n",
  5507. i40e_stat_str(&pf->hw, ret),
  5508. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  5509. /* Schedule PF reset to recover */
  5510. set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
  5511. i40e_service_event_schedule(pf);
  5512. }
  5513. return ret;
  5514. }
  5515. /**
  5516. * i40e_init_pf_dcb - Initialize DCB configuration
  5517. * @pf: PF being configured
  5518. *
  5519. * Query the current DCB configuration and cache it
  5520. * in the hardware structure
  5521. **/
  5522. static int i40e_init_pf_dcb(struct i40e_pf *pf)
  5523. {
  5524. struct i40e_hw *hw = &pf->hw;
  5525. int err = 0;
  5526. /* Do not enable DCB for SW1 and SW2 images even if the FW is capable
  5527. * Also do not enable DCBx if FW LLDP agent is disabled
  5528. */
  5529. if ((pf->hw_features & I40E_HW_NO_DCB_SUPPORT) ||
  5530. (pf->flags & I40E_FLAG_DISABLE_FW_LLDP))
  5531. goto out;
  5532. /* Get the initial DCB configuration */
  5533. err = i40e_init_dcb(hw);
  5534. if (!err) {
  5535. /* Device/Function is not DCBX capable */
  5536. if ((!hw->func_caps.dcb) ||
  5537. (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
  5538. dev_info(&pf->pdev->dev,
  5539. "DCBX offload is not supported or is disabled for this PF.\n");
  5540. } else {
  5541. /* When status is not DISABLED then DCBX in FW */
  5542. pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
  5543. DCB_CAP_DCBX_VER_IEEE;
  5544. pf->flags |= I40E_FLAG_DCB_CAPABLE;
  5545. /* Enable DCB tagging only when more than one TC
  5546. * or explicitly disable if only one TC
  5547. */
  5548. if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
  5549. pf->flags |= I40E_FLAG_DCB_ENABLED;
  5550. else
  5551. pf->flags &= ~I40E_FLAG_DCB_ENABLED;
  5552. dev_dbg(&pf->pdev->dev,
  5553. "DCBX offload is supported for this PF.\n");
  5554. }
  5555. } else if (pf->hw.aq.asq_last_status == I40E_AQ_RC_EPERM) {
  5556. dev_info(&pf->pdev->dev, "FW LLDP disabled for this PF.\n");
  5557. pf->flags |= I40E_FLAG_DISABLE_FW_LLDP;
  5558. } else {
  5559. dev_info(&pf->pdev->dev,
  5560. "Query for DCB configuration failed, err %s aq_err %s\n",
  5561. i40e_stat_str(&pf->hw, err),
  5562. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  5563. }
  5564. out:
  5565. return err;
  5566. }
  5567. #endif /* CONFIG_I40E_DCB */
  5568. #define SPEED_SIZE 14
  5569. #define FC_SIZE 8
  5570. /**
  5571. * i40e_print_link_message - print link up or down
  5572. * @vsi: the VSI for which link needs a message
  5573. * @isup: true of link is up, false otherwise
  5574. */
  5575. void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
  5576. {
  5577. enum i40e_aq_link_speed new_speed;
  5578. struct i40e_pf *pf = vsi->back;
  5579. char *speed = "Unknown";
  5580. char *fc = "Unknown";
  5581. char *fec = "";
  5582. char *req_fec = "";
  5583. char *an = "";
  5584. new_speed = pf->hw.phy.link_info.link_speed;
  5585. if ((vsi->current_isup == isup) && (vsi->current_speed == new_speed))
  5586. return;
  5587. vsi->current_isup = isup;
  5588. vsi->current_speed = new_speed;
  5589. if (!isup) {
  5590. netdev_info(vsi->netdev, "NIC Link is Down\n");
  5591. return;
  5592. }
  5593. /* Warn user if link speed on NPAR enabled partition is not at
  5594. * least 10GB
  5595. */
  5596. if (pf->hw.func_caps.npar_enable &&
  5597. (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
  5598. pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
  5599. netdev_warn(vsi->netdev,
  5600. "The partition detected link speed that is less than 10Gbps\n");
  5601. switch (pf->hw.phy.link_info.link_speed) {
  5602. case I40E_LINK_SPEED_40GB:
  5603. speed = "40 G";
  5604. break;
  5605. case I40E_LINK_SPEED_20GB:
  5606. speed = "20 G";
  5607. break;
  5608. case I40E_LINK_SPEED_25GB:
  5609. speed = "25 G";
  5610. break;
  5611. case I40E_LINK_SPEED_10GB:
  5612. speed = "10 G";
  5613. break;
  5614. case I40E_LINK_SPEED_1GB:
  5615. speed = "1000 M";
  5616. break;
  5617. case I40E_LINK_SPEED_100MB:
  5618. speed = "100 M";
  5619. break;
  5620. default:
  5621. break;
  5622. }
  5623. switch (pf->hw.fc.current_mode) {
  5624. case I40E_FC_FULL:
  5625. fc = "RX/TX";
  5626. break;
  5627. case I40E_FC_TX_PAUSE:
  5628. fc = "TX";
  5629. break;
  5630. case I40E_FC_RX_PAUSE:
  5631. fc = "RX";
  5632. break;
  5633. default:
  5634. fc = "None";
  5635. break;
  5636. }
  5637. if (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_25GB) {
  5638. req_fec = ", Requested FEC: None";
  5639. fec = ", FEC: None";
  5640. an = ", Autoneg: False";
  5641. if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
  5642. an = ", Autoneg: True";
  5643. if (pf->hw.phy.link_info.fec_info &
  5644. I40E_AQ_CONFIG_FEC_KR_ENA)
  5645. fec = ", FEC: CL74 FC-FEC/BASE-R";
  5646. else if (pf->hw.phy.link_info.fec_info &
  5647. I40E_AQ_CONFIG_FEC_RS_ENA)
  5648. fec = ", FEC: CL108 RS-FEC";
  5649. /* 'CL108 RS-FEC' should be displayed when RS is requested, or
  5650. * both RS and FC are requested
  5651. */
  5652. if (vsi->back->hw.phy.link_info.req_fec_info &
  5653. (I40E_AQ_REQUEST_FEC_KR | I40E_AQ_REQUEST_FEC_RS)) {
  5654. if (vsi->back->hw.phy.link_info.req_fec_info &
  5655. I40E_AQ_REQUEST_FEC_RS)
  5656. req_fec = ", Requested FEC: CL108 RS-FEC";
  5657. else
  5658. req_fec = ", Requested FEC: CL74 FC-FEC/BASE-R";
  5659. }
  5660. }
  5661. netdev_info(vsi->netdev, "NIC Link is Up, %sbps Full Duplex%s%s%s, Flow Control: %s\n",
  5662. speed, req_fec, fec, an, fc);
  5663. }
  5664. /**
  5665. * i40e_up_complete - Finish the last steps of bringing up a connection
  5666. * @vsi: the VSI being configured
  5667. **/
  5668. static int i40e_up_complete(struct i40e_vsi *vsi)
  5669. {
  5670. struct i40e_pf *pf = vsi->back;
  5671. int err;
  5672. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  5673. i40e_vsi_configure_msix(vsi);
  5674. else
  5675. i40e_configure_msi_and_legacy(vsi);
  5676. /* start rings */
  5677. err = i40e_vsi_start_rings(vsi);
  5678. if (err)
  5679. return err;
  5680. clear_bit(__I40E_VSI_DOWN, vsi->state);
  5681. i40e_napi_enable_all(vsi);
  5682. i40e_vsi_enable_irq(vsi);
  5683. if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
  5684. (vsi->netdev)) {
  5685. i40e_print_link_message(vsi, true);
  5686. netif_tx_start_all_queues(vsi->netdev);
  5687. netif_carrier_on(vsi->netdev);
  5688. }
  5689. /* replay FDIR SB filters */
  5690. if (vsi->type == I40E_VSI_FDIR) {
  5691. /* reset fd counters */
  5692. pf->fd_add_err = 0;
  5693. pf->fd_atr_cnt = 0;
  5694. i40e_fdir_filter_restore(vsi);
  5695. }
  5696. /* On the next run of the service_task, notify any clients of the new
  5697. * opened netdev
  5698. */
  5699. set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
  5700. i40e_service_event_schedule(pf);
  5701. return 0;
  5702. }
  5703. /**
  5704. * i40e_vsi_reinit_locked - Reset the VSI
  5705. * @vsi: the VSI being configured
  5706. *
  5707. * Rebuild the ring structs after some configuration
  5708. * has changed, e.g. MTU size.
  5709. **/
  5710. static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
  5711. {
  5712. struct i40e_pf *pf = vsi->back;
  5713. WARN_ON(in_interrupt());
  5714. while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state))
  5715. usleep_range(1000, 2000);
  5716. i40e_down(vsi);
  5717. i40e_up(vsi);
  5718. clear_bit(__I40E_CONFIG_BUSY, pf->state);
  5719. }
  5720. /**
  5721. * i40e_up - Bring the connection back up after being down
  5722. * @vsi: the VSI being configured
  5723. **/
  5724. int i40e_up(struct i40e_vsi *vsi)
  5725. {
  5726. int err;
  5727. err = i40e_vsi_configure(vsi);
  5728. if (!err)
  5729. err = i40e_up_complete(vsi);
  5730. return err;
  5731. }
  5732. /**
  5733. * i40e_force_link_state - Force the link status
  5734. * @pf: board private structure
  5735. * @is_up: whether the link state should be forced up or down
  5736. **/
  5737. static i40e_status i40e_force_link_state(struct i40e_pf *pf, bool is_up)
  5738. {
  5739. struct i40e_aq_get_phy_abilities_resp abilities;
  5740. struct i40e_aq_set_phy_config config = {0};
  5741. struct i40e_hw *hw = &pf->hw;
  5742. i40e_status err;
  5743. u64 mask;
  5744. /* Get the current phy config */
  5745. err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities,
  5746. NULL);
  5747. if (err) {
  5748. dev_err(&pf->pdev->dev,
  5749. "failed to get phy cap., ret = %s last_status = %s\n",
  5750. i40e_stat_str(hw, err),
  5751. i40e_aq_str(hw, hw->aq.asq_last_status));
  5752. return err;
  5753. }
  5754. /* If link needs to go up, but was not forced to go down,
  5755. * no need for a flap
  5756. */
  5757. if (is_up && abilities.phy_type != 0)
  5758. return I40E_SUCCESS;
  5759. /* To force link we need to set bits for all supported PHY types,
  5760. * but there are now more than 32, so we need to split the bitmap
  5761. * across two fields.
  5762. */
  5763. mask = I40E_PHY_TYPES_BITMASK;
  5764. config.phy_type = is_up ? cpu_to_le32((u32)(mask & 0xffffffff)) : 0;
  5765. config.phy_type_ext = is_up ? (u8)((mask >> 32) & 0xff) : 0;
  5766. /* Copy the old settings, except of phy_type */
  5767. config.abilities = abilities.abilities;
  5768. config.link_speed = abilities.link_speed;
  5769. config.eee_capability = abilities.eee_capability;
  5770. config.eeer = abilities.eeer_val;
  5771. config.low_power_ctrl = abilities.d3_lpan;
  5772. err = i40e_aq_set_phy_config(hw, &config, NULL);
  5773. if (err) {
  5774. dev_err(&pf->pdev->dev,
  5775. "set phy config ret = %s last_status = %s\n",
  5776. i40e_stat_str(&pf->hw, err),
  5777. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  5778. return err;
  5779. }
  5780. /* Update the link info */
  5781. err = i40e_update_link_info(hw);
  5782. if (err) {
  5783. /* Wait a little bit (on 40G cards it sometimes takes a really
  5784. * long time for link to come back from the atomic reset)
  5785. * and try once more
  5786. */
  5787. msleep(1000);
  5788. i40e_update_link_info(hw);
  5789. }
  5790. i40e_aq_set_link_restart_an(hw, true, NULL);
  5791. return I40E_SUCCESS;
  5792. }
  5793. /**
  5794. * i40e_down - Shutdown the connection processing
  5795. * @vsi: the VSI being stopped
  5796. **/
  5797. void i40e_down(struct i40e_vsi *vsi)
  5798. {
  5799. int i;
  5800. /* It is assumed that the caller of this function
  5801. * sets the vsi->state __I40E_VSI_DOWN bit.
  5802. */
  5803. if (vsi->netdev) {
  5804. netif_carrier_off(vsi->netdev);
  5805. netif_tx_disable(vsi->netdev);
  5806. }
  5807. i40e_vsi_disable_irq(vsi);
  5808. i40e_vsi_stop_rings(vsi);
  5809. if (vsi->type == I40E_VSI_MAIN &&
  5810. vsi->back->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED)
  5811. i40e_force_link_state(vsi->back, false);
  5812. i40e_napi_disable_all(vsi);
  5813. for (i = 0; i < vsi->num_queue_pairs; i++) {
  5814. i40e_clean_tx_ring(vsi->tx_rings[i]);
  5815. if (i40e_enabled_xdp_vsi(vsi))
  5816. i40e_clean_tx_ring(vsi->xdp_rings[i]);
  5817. i40e_clean_rx_ring(vsi->rx_rings[i]);
  5818. }
  5819. }
  5820. /**
  5821. * i40e_validate_mqprio_qopt- validate queue mapping info
  5822. * @vsi: the VSI being configured
  5823. * @mqprio_qopt: queue parametrs
  5824. **/
  5825. static int i40e_validate_mqprio_qopt(struct i40e_vsi *vsi,
  5826. struct tc_mqprio_qopt_offload *mqprio_qopt)
  5827. {
  5828. u64 sum_max_rate = 0;
  5829. u64 max_rate = 0;
  5830. int i;
  5831. if (mqprio_qopt->qopt.offset[0] != 0 ||
  5832. mqprio_qopt->qopt.num_tc < 1 ||
  5833. mqprio_qopt->qopt.num_tc > I40E_MAX_TRAFFIC_CLASS)
  5834. return -EINVAL;
  5835. for (i = 0; ; i++) {
  5836. if (!mqprio_qopt->qopt.count[i])
  5837. return -EINVAL;
  5838. if (mqprio_qopt->min_rate[i]) {
  5839. dev_err(&vsi->back->pdev->dev,
  5840. "Invalid min tx rate (greater than 0) specified\n");
  5841. return -EINVAL;
  5842. }
  5843. max_rate = mqprio_qopt->max_rate[i];
  5844. do_div(max_rate, I40E_BW_MBPS_DIVISOR);
  5845. sum_max_rate += max_rate;
  5846. if (i >= mqprio_qopt->qopt.num_tc - 1)
  5847. break;
  5848. if (mqprio_qopt->qopt.offset[i + 1] !=
  5849. (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i]))
  5850. return -EINVAL;
  5851. }
  5852. if (vsi->num_queue_pairs <
  5853. (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i])) {
  5854. return -EINVAL;
  5855. }
  5856. if (sum_max_rate > i40e_get_link_speed(vsi)) {
  5857. dev_err(&vsi->back->pdev->dev,
  5858. "Invalid max tx rate specified\n");
  5859. return -EINVAL;
  5860. }
  5861. return 0;
  5862. }
  5863. /**
  5864. * i40e_vsi_set_default_tc_config - set default values for tc configuration
  5865. * @vsi: the VSI being configured
  5866. **/
  5867. static void i40e_vsi_set_default_tc_config(struct i40e_vsi *vsi)
  5868. {
  5869. u16 qcount;
  5870. int i;
  5871. /* Only TC0 is enabled */
  5872. vsi->tc_config.numtc = 1;
  5873. vsi->tc_config.enabled_tc = 1;
  5874. qcount = min_t(int, vsi->alloc_queue_pairs,
  5875. i40e_pf_get_max_q_per_tc(vsi->back));
  5876. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  5877. /* For the TC that is not enabled set the offset to to default
  5878. * queue and allocate one queue for the given TC.
  5879. */
  5880. vsi->tc_config.tc_info[i].qoffset = 0;
  5881. if (i == 0)
  5882. vsi->tc_config.tc_info[i].qcount = qcount;
  5883. else
  5884. vsi->tc_config.tc_info[i].qcount = 1;
  5885. vsi->tc_config.tc_info[i].netdev_tc = 0;
  5886. }
  5887. }
  5888. /**
  5889. * i40e_setup_tc - configure multiple traffic classes
  5890. * @netdev: net device to configure
  5891. * @type_data: tc offload data
  5892. **/
  5893. static int i40e_setup_tc(struct net_device *netdev, void *type_data)
  5894. {
  5895. struct tc_mqprio_qopt_offload *mqprio_qopt = type_data;
  5896. struct i40e_netdev_priv *np = netdev_priv(netdev);
  5897. struct i40e_vsi *vsi = np->vsi;
  5898. struct i40e_pf *pf = vsi->back;
  5899. u8 enabled_tc = 0, num_tc, hw;
  5900. bool need_reset = false;
  5901. int ret = -EINVAL;
  5902. u16 mode;
  5903. int i;
  5904. num_tc = mqprio_qopt->qopt.num_tc;
  5905. hw = mqprio_qopt->qopt.hw;
  5906. mode = mqprio_qopt->mode;
  5907. if (!hw) {
  5908. pf->flags &= ~I40E_FLAG_TC_MQPRIO;
  5909. memcpy(&vsi->mqprio_qopt, mqprio_qopt, sizeof(*mqprio_qopt));
  5910. goto config_tc;
  5911. }
  5912. /* Check if MFP enabled */
  5913. if (pf->flags & I40E_FLAG_MFP_ENABLED) {
  5914. netdev_info(netdev,
  5915. "Configuring TC not supported in MFP mode\n");
  5916. return ret;
  5917. }
  5918. switch (mode) {
  5919. case TC_MQPRIO_MODE_DCB:
  5920. pf->flags &= ~I40E_FLAG_TC_MQPRIO;
  5921. /* Check if DCB enabled to continue */
  5922. if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
  5923. netdev_info(netdev,
  5924. "DCB is not enabled for adapter\n");
  5925. return ret;
  5926. }
  5927. /* Check whether tc count is within enabled limit */
  5928. if (num_tc > i40e_pf_get_num_tc(pf)) {
  5929. netdev_info(netdev,
  5930. "TC count greater than enabled on link for adapter\n");
  5931. return ret;
  5932. }
  5933. break;
  5934. case TC_MQPRIO_MODE_CHANNEL:
  5935. if (pf->flags & I40E_FLAG_DCB_ENABLED) {
  5936. netdev_info(netdev,
  5937. "Full offload of TC Mqprio options is not supported when DCB is enabled\n");
  5938. return ret;
  5939. }
  5940. if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
  5941. return ret;
  5942. ret = i40e_validate_mqprio_qopt(vsi, mqprio_qopt);
  5943. if (ret)
  5944. return ret;
  5945. memcpy(&vsi->mqprio_qopt, mqprio_qopt,
  5946. sizeof(*mqprio_qopt));
  5947. pf->flags |= I40E_FLAG_TC_MQPRIO;
  5948. pf->flags &= ~I40E_FLAG_DCB_ENABLED;
  5949. break;
  5950. default:
  5951. return -EINVAL;
  5952. }
  5953. config_tc:
  5954. /* Generate TC map for number of tc requested */
  5955. for (i = 0; i < num_tc; i++)
  5956. enabled_tc |= BIT(i);
  5957. /* Requesting same TC configuration as already enabled */
  5958. if (enabled_tc == vsi->tc_config.enabled_tc &&
  5959. mode != TC_MQPRIO_MODE_CHANNEL)
  5960. return 0;
  5961. /* Quiesce VSI queues */
  5962. i40e_quiesce_vsi(vsi);
  5963. if (!hw && !(pf->flags & I40E_FLAG_TC_MQPRIO))
  5964. i40e_remove_queue_channels(vsi);
  5965. /* Configure VSI for enabled TCs */
  5966. ret = i40e_vsi_config_tc(vsi, enabled_tc);
  5967. if (ret) {
  5968. netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
  5969. vsi->seid);
  5970. need_reset = true;
  5971. goto exit;
  5972. }
  5973. if (pf->flags & I40E_FLAG_TC_MQPRIO) {
  5974. if (vsi->mqprio_qopt.max_rate[0]) {
  5975. u64 max_tx_rate = vsi->mqprio_qopt.max_rate[0];
  5976. do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
  5977. ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
  5978. if (!ret) {
  5979. u64 credits = max_tx_rate;
  5980. do_div(credits, I40E_BW_CREDIT_DIVISOR);
  5981. dev_dbg(&vsi->back->pdev->dev,
  5982. "Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
  5983. max_tx_rate,
  5984. credits,
  5985. vsi->seid);
  5986. } else {
  5987. need_reset = true;
  5988. goto exit;
  5989. }
  5990. }
  5991. ret = i40e_configure_queue_channels(vsi);
  5992. if (ret) {
  5993. netdev_info(netdev,
  5994. "Failed configuring queue channels\n");
  5995. need_reset = true;
  5996. goto exit;
  5997. }
  5998. }
  5999. exit:
  6000. /* Reset the configuration data to defaults, only TC0 is enabled */
  6001. if (need_reset) {
  6002. i40e_vsi_set_default_tc_config(vsi);
  6003. need_reset = false;
  6004. }
  6005. /* Unquiesce VSI */
  6006. i40e_unquiesce_vsi(vsi);
  6007. return ret;
  6008. }
  6009. /**
  6010. * i40e_set_cld_element - sets cloud filter element data
  6011. * @filter: cloud filter rule
  6012. * @cld: ptr to cloud filter element data
  6013. *
  6014. * This is helper function to copy data into cloud filter element
  6015. **/
  6016. static inline void
  6017. i40e_set_cld_element(struct i40e_cloud_filter *filter,
  6018. struct i40e_aqc_cloud_filters_element_data *cld)
  6019. {
  6020. int i, j;
  6021. u32 ipa;
  6022. memset(cld, 0, sizeof(*cld));
  6023. ether_addr_copy(cld->outer_mac, filter->dst_mac);
  6024. ether_addr_copy(cld->inner_mac, filter->src_mac);
  6025. if (filter->n_proto != ETH_P_IP && filter->n_proto != ETH_P_IPV6)
  6026. return;
  6027. if (filter->n_proto == ETH_P_IPV6) {
  6028. #define IPV6_MAX_INDEX (ARRAY_SIZE(filter->dst_ipv6) - 1)
  6029. for (i = 0, j = 0; i < ARRAY_SIZE(filter->dst_ipv6);
  6030. i++, j += 2) {
  6031. ipa = be32_to_cpu(filter->dst_ipv6[IPV6_MAX_INDEX - i]);
  6032. ipa = cpu_to_le32(ipa);
  6033. memcpy(&cld->ipaddr.raw_v6.data[j], &ipa, sizeof(ipa));
  6034. }
  6035. } else {
  6036. ipa = be32_to_cpu(filter->dst_ipv4);
  6037. memcpy(&cld->ipaddr.v4.data, &ipa, sizeof(ipa));
  6038. }
  6039. cld->inner_vlan = cpu_to_le16(ntohs(filter->vlan_id));
  6040. /* tenant_id is not supported by FW now, once the support is enabled
  6041. * fill the cld->tenant_id with cpu_to_le32(filter->tenant_id)
  6042. */
  6043. if (filter->tenant_id)
  6044. return;
  6045. }
  6046. /**
  6047. * i40e_add_del_cloud_filter - Add/del cloud filter
  6048. * @vsi: pointer to VSI
  6049. * @filter: cloud filter rule
  6050. * @add: if true, add, if false, delete
  6051. *
  6052. * Add or delete a cloud filter for a specific flow spec.
  6053. * Returns 0 if the filter were successfully added.
  6054. **/
  6055. int i40e_add_del_cloud_filter(struct i40e_vsi *vsi,
  6056. struct i40e_cloud_filter *filter, bool add)
  6057. {
  6058. struct i40e_aqc_cloud_filters_element_data cld_filter;
  6059. struct i40e_pf *pf = vsi->back;
  6060. int ret;
  6061. static const u16 flag_table[128] = {
  6062. [I40E_CLOUD_FILTER_FLAGS_OMAC] =
  6063. I40E_AQC_ADD_CLOUD_FILTER_OMAC,
  6064. [I40E_CLOUD_FILTER_FLAGS_IMAC] =
  6065. I40E_AQC_ADD_CLOUD_FILTER_IMAC,
  6066. [I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN] =
  6067. I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN,
  6068. [I40E_CLOUD_FILTER_FLAGS_IMAC_TEN_ID] =
  6069. I40E_AQC_ADD_CLOUD_FILTER_IMAC_TEN_ID,
  6070. [I40E_CLOUD_FILTER_FLAGS_OMAC_TEN_ID_IMAC] =
  6071. I40E_AQC_ADD_CLOUD_FILTER_OMAC_TEN_ID_IMAC,
  6072. [I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN_TEN_ID] =
  6073. I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN_TEN_ID,
  6074. [I40E_CLOUD_FILTER_FLAGS_IIP] =
  6075. I40E_AQC_ADD_CLOUD_FILTER_IIP,
  6076. };
  6077. if (filter->flags >= ARRAY_SIZE(flag_table))
  6078. return I40E_ERR_CONFIG;
  6079. /* copy element needed to add cloud filter from filter */
  6080. i40e_set_cld_element(filter, &cld_filter);
  6081. if (filter->tunnel_type != I40E_CLOUD_TNL_TYPE_NONE)
  6082. cld_filter.flags = cpu_to_le16(filter->tunnel_type <<
  6083. I40E_AQC_ADD_CLOUD_TNL_TYPE_SHIFT);
  6084. if (filter->n_proto == ETH_P_IPV6)
  6085. cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
  6086. I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
  6087. else
  6088. cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
  6089. I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
  6090. if (add)
  6091. ret = i40e_aq_add_cloud_filters(&pf->hw, filter->seid,
  6092. &cld_filter, 1);
  6093. else
  6094. ret = i40e_aq_rem_cloud_filters(&pf->hw, filter->seid,
  6095. &cld_filter, 1);
  6096. if (ret)
  6097. dev_dbg(&pf->pdev->dev,
  6098. "Failed to %s cloud filter using l4 port %u, err %d aq_err %d\n",
  6099. add ? "add" : "delete", filter->dst_port, ret,
  6100. pf->hw.aq.asq_last_status);
  6101. else
  6102. dev_info(&pf->pdev->dev,
  6103. "%s cloud filter for VSI: %d\n",
  6104. add ? "Added" : "Deleted", filter->seid);
  6105. return ret;
  6106. }
  6107. /**
  6108. * i40e_add_del_cloud_filter_big_buf - Add/del cloud filter using big_buf
  6109. * @vsi: pointer to VSI
  6110. * @filter: cloud filter rule
  6111. * @add: if true, add, if false, delete
  6112. *
  6113. * Add or delete a cloud filter for a specific flow spec using big buffer.
  6114. * Returns 0 if the filter were successfully added.
  6115. **/
  6116. int i40e_add_del_cloud_filter_big_buf(struct i40e_vsi *vsi,
  6117. struct i40e_cloud_filter *filter,
  6118. bool add)
  6119. {
  6120. struct i40e_aqc_cloud_filters_element_bb cld_filter;
  6121. struct i40e_pf *pf = vsi->back;
  6122. int ret;
  6123. /* Both (src/dst) valid mac_addr are not supported */
  6124. if ((is_valid_ether_addr(filter->dst_mac) &&
  6125. is_valid_ether_addr(filter->src_mac)) ||
  6126. (is_multicast_ether_addr(filter->dst_mac) &&
  6127. is_multicast_ether_addr(filter->src_mac)))
  6128. return -EOPNOTSUPP;
  6129. /* Big buffer cloud filter needs 'L4 port' to be non-zero. Also, UDP
  6130. * ports are not supported via big buffer now.
  6131. */
  6132. if (!filter->dst_port || filter->ip_proto == IPPROTO_UDP)
  6133. return -EOPNOTSUPP;
  6134. /* adding filter using src_port/src_ip is not supported at this stage */
  6135. if (filter->src_port || filter->src_ipv4 ||
  6136. !ipv6_addr_any(&filter->ip.v6.src_ip6))
  6137. return -EOPNOTSUPP;
  6138. /* copy element needed to add cloud filter from filter */
  6139. i40e_set_cld_element(filter, &cld_filter.element);
  6140. if (is_valid_ether_addr(filter->dst_mac) ||
  6141. is_valid_ether_addr(filter->src_mac) ||
  6142. is_multicast_ether_addr(filter->dst_mac) ||
  6143. is_multicast_ether_addr(filter->src_mac)) {
  6144. /* MAC + IP : unsupported mode */
  6145. if (filter->dst_ipv4)
  6146. return -EOPNOTSUPP;
  6147. /* since we validated that L4 port must be valid before
  6148. * we get here, start with respective "flags" value
  6149. * and update if vlan is present or not
  6150. */
  6151. cld_filter.element.flags =
  6152. cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_PORT);
  6153. if (filter->vlan_id) {
  6154. cld_filter.element.flags =
  6155. cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_VLAN_PORT);
  6156. }
  6157. } else if (filter->dst_ipv4 ||
  6158. !ipv6_addr_any(&filter->ip.v6.dst_ip6)) {
  6159. cld_filter.element.flags =
  6160. cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_IP_PORT);
  6161. if (filter->n_proto == ETH_P_IPV6)
  6162. cld_filter.element.flags |=
  6163. cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
  6164. else
  6165. cld_filter.element.flags |=
  6166. cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
  6167. } else {
  6168. dev_err(&pf->pdev->dev,
  6169. "either mac or ip has to be valid for cloud filter\n");
  6170. return -EINVAL;
  6171. }
  6172. /* Now copy L4 port in Byte 6..7 in general fields */
  6173. cld_filter.general_fields[I40E_AQC_ADD_CLOUD_FV_FLU_0X16_WORD0] =
  6174. be16_to_cpu(filter->dst_port);
  6175. if (add) {
  6176. /* Validate current device switch mode, change if necessary */
  6177. ret = i40e_validate_and_set_switch_mode(vsi);
  6178. if (ret) {
  6179. dev_err(&pf->pdev->dev,
  6180. "failed to set switch mode, ret %d\n",
  6181. ret);
  6182. return ret;
  6183. }
  6184. ret = i40e_aq_add_cloud_filters_bb(&pf->hw, filter->seid,
  6185. &cld_filter, 1);
  6186. } else {
  6187. ret = i40e_aq_rem_cloud_filters_bb(&pf->hw, filter->seid,
  6188. &cld_filter, 1);
  6189. }
  6190. if (ret)
  6191. dev_dbg(&pf->pdev->dev,
  6192. "Failed to %s cloud filter(big buffer) err %d aq_err %d\n",
  6193. add ? "add" : "delete", ret, pf->hw.aq.asq_last_status);
  6194. else
  6195. dev_info(&pf->pdev->dev,
  6196. "%s cloud filter for VSI: %d, L4 port: %d\n",
  6197. add ? "add" : "delete", filter->seid,
  6198. ntohs(filter->dst_port));
  6199. return ret;
  6200. }
  6201. /**
  6202. * i40e_parse_cls_flower - Parse tc flower filters provided by kernel
  6203. * @vsi: Pointer to VSI
  6204. * @cls_flower: Pointer to struct tc_cls_flower_offload
  6205. * @filter: Pointer to cloud filter structure
  6206. *
  6207. **/
  6208. static int i40e_parse_cls_flower(struct i40e_vsi *vsi,
  6209. struct tc_cls_flower_offload *f,
  6210. struct i40e_cloud_filter *filter)
  6211. {
  6212. u16 n_proto_mask = 0, n_proto_key = 0, addr_type = 0;
  6213. struct i40e_pf *pf = vsi->back;
  6214. u8 field_flags = 0;
  6215. if (f->dissector->used_keys &
  6216. ~(BIT(FLOW_DISSECTOR_KEY_CONTROL) |
  6217. BIT(FLOW_DISSECTOR_KEY_BASIC) |
  6218. BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
  6219. BIT(FLOW_DISSECTOR_KEY_VLAN) |
  6220. BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
  6221. BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
  6222. BIT(FLOW_DISSECTOR_KEY_PORTS) |
  6223. BIT(FLOW_DISSECTOR_KEY_ENC_KEYID))) {
  6224. dev_err(&pf->pdev->dev, "Unsupported key used: 0x%x\n",
  6225. f->dissector->used_keys);
  6226. return -EOPNOTSUPP;
  6227. }
  6228. if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
  6229. struct flow_dissector_key_keyid *key =
  6230. skb_flow_dissector_target(f->dissector,
  6231. FLOW_DISSECTOR_KEY_ENC_KEYID,
  6232. f->key);
  6233. struct flow_dissector_key_keyid *mask =
  6234. skb_flow_dissector_target(f->dissector,
  6235. FLOW_DISSECTOR_KEY_ENC_KEYID,
  6236. f->mask);
  6237. if (mask->keyid != 0)
  6238. field_flags |= I40E_CLOUD_FIELD_TEN_ID;
  6239. filter->tenant_id = be32_to_cpu(key->keyid);
  6240. }
  6241. if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_BASIC)) {
  6242. struct flow_dissector_key_basic *key =
  6243. skb_flow_dissector_target(f->dissector,
  6244. FLOW_DISSECTOR_KEY_BASIC,
  6245. f->key);
  6246. struct flow_dissector_key_basic *mask =
  6247. skb_flow_dissector_target(f->dissector,
  6248. FLOW_DISSECTOR_KEY_BASIC,
  6249. f->mask);
  6250. n_proto_key = ntohs(key->n_proto);
  6251. n_proto_mask = ntohs(mask->n_proto);
  6252. if (n_proto_key == ETH_P_ALL) {
  6253. n_proto_key = 0;
  6254. n_proto_mask = 0;
  6255. }
  6256. filter->n_proto = n_proto_key & n_proto_mask;
  6257. filter->ip_proto = key->ip_proto;
  6258. }
  6259. if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
  6260. struct flow_dissector_key_eth_addrs *key =
  6261. skb_flow_dissector_target(f->dissector,
  6262. FLOW_DISSECTOR_KEY_ETH_ADDRS,
  6263. f->key);
  6264. struct flow_dissector_key_eth_addrs *mask =
  6265. skb_flow_dissector_target(f->dissector,
  6266. FLOW_DISSECTOR_KEY_ETH_ADDRS,
  6267. f->mask);
  6268. /* use is_broadcast and is_zero to check for all 0xf or 0 */
  6269. if (!is_zero_ether_addr(mask->dst)) {
  6270. if (is_broadcast_ether_addr(mask->dst)) {
  6271. field_flags |= I40E_CLOUD_FIELD_OMAC;
  6272. } else {
  6273. dev_err(&pf->pdev->dev, "Bad ether dest mask %pM\n",
  6274. mask->dst);
  6275. return I40E_ERR_CONFIG;
  6276. }
  6277. }
  6278. if (!is_zero_ether_addr(mask->src)) {
  6279. if (is_broadcast_ether_addr(mask->src)) {
  6280. field_flags |= I40E_CLOUD_FIELD_IMAC;
  6281. } else {
  6282. dev_err(&pf->pdev->dev, "Bad ether src mask %pM\n",
  6283. mask->src);
  6284. return I40E_ERR_CONFIG;
  6285. }
  6286. }
  6287. ether_addr_copy(filter->dst_mac, key->dst);
  6288. ether_addr_copy(filter->src_mac, key->src);
  6289. }
  6290. if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_VLAN)) {
  6291. struct flow_dissector_key_vlan *key =
  6292. skb_flow_dissector_target(f->dissector,
  6293. FLOW_DISSECTOR_KEY_VLAN,
  6294. f->key);
  6295. struct flow_dissector_key_vlan *mask =
  6296. skb_flow_dissector_target(f->dissector,
  6297. FLOW_DISSECTOR_KEY_VLAN,
  6298. f->mask);
  6299. if (mask->vlan_id) {
  6300. if (mask->vlan_id == VLAN_VID_MASK) {
  6301. field_flags |= I40E_CLOUD_FIELD_IVLAN;
  6302. } else {
  6303. dev_err(&pf->pdev->dev, "Bad vlan mask 0x%04x\n",
  6304. mask->vlan_id);
  6305. return I40E_ERR_CONFIG;
  6306. }
  6307. }
  6308. filter->vlan_id = cpu_to_be16(key->vlan_id);
  6309. }
  6310. if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_CONTROL)) {
  6311. struct flow_dissector_key_control *key =
  6312. skb_flow_dissector_target(f->dissector,
  6313. FLOW_DISSECTOR_KEY_CONTROL,
  6314. f->key);
  6315. addr_type = key->addr_type;
  6316. }
  6317. if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
  6318. struct flow_dissector_key_ipv4_addrs *key =
  6319. skb_flow_dissector_target(f->dissector,
  6320. FLOW_DISSECTOR_KEY_IPV4_ADDRS,
  6321. f->key);
  6322. struct flow_dissector_key_ipv4_addrs *mask =
  6323. skb_flow_dissector_target(f->dissector,
  6324. FLOW_DISSECTOR_KEY_IPV4_ADDRS,
  6325. f->mask);
  6326. if (mask->dst) {
  6327. if (mask->dst == cpu_to_be32(0xffffffff)) {
  6328. field_flags |= I40E_CLOUD_FIELD_IIP;
  6329. } else {
  6330. dev_err(&pf->pdev->dev, "Bad ip dst mask %pI4b\n",
  6331. &mask->dst);
  6332. return I40E_ERR_CONFIG;
  6333. }
  6334. }
  6335. if (mask->src) {
  6336. if (mask->src == cpu_to_be32(0xffffffff)) {
  6337. field_flags |= I40E_CLOUD_FIELD_IIP;
  6338. } else {
  6339. dev_err(&pf->pdev->dev, "Bad ip src mask %pI4b\n",
  6340. &mask->src);
  6341. return I40E_ERR_CONFIG;
  6342. }
  6343. }
  6344. if (field_flags & I40E_CLOUD_FIELD_TEN_ID) {
  6345. dev_err(&pf->pdev->dev, "Tenant id not allowed for ip filter\n");
  6346. return I40E_ERR_CONFIG;
  6347. }
  6348. filter->dst_ipv4 = key->dst;
  6349. filter->src_ipv4 = key->src;
  6350. }
  6351. if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
  6352. struct flow_dissector_key_ipv6_addrs *key =
  6353. skb_flow_dissector_target(f->dissector,
  6354. FLOW_DISSECTOR_KEY_IPV6_ADDRS,
  6355. f->key);
  6356. struct flow_dissector_key_ipv6_addrs *mask =
  6357. skb_flow_dissector_target(f->dissector,
  6358. FLOW_DISSECTOR_KEY_IPV6_ADDRS,
  6359. f->mask);
  6360. /* src and dest IPV6 address should not be LOOPBACK
  6361. * (0:0:0:0:0:0:0:1), which can be represented as ::1
  6362. */
  6363. if (ipv6_addr_loopback(&key->dst) ||
  6364. ipv6_addr_loopback(&key->src)) {
  6365. dev_err(&pf->pdev->dev,
  6366. "Bad ipv6, addr is LOOPBACK\n");
  6367. return I40E_ERR_CONFIG;
  6368. }
  6369. if (!ipv6_addr_any(&mask->dst) || !ipv6_addr_any(&mask->src))
  6370. field_flags |= I40E_CLOUD_FIELD_IIP;
  6371. memcpy(&filter->src_ipv6, &key->src.s6_addr32,
  6372. sizeof(filter->src_ipv6));
  6373. memcpy(&filter->dst_ipv6, &key->dst.s6_addr32,
  6374. sizeof(filter->dst_ipv6));
  6375. }
  6376. if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_PORTS)) {
  6377. struct flow_dissector_key_ports *key =
  6378. skb_flow_dissector_target(f->dissector,
  6379. FLOW_DISSECTOR_KEY_PORTS,
  6380. f->key);
  6381. struct flow_dissector_key_ports *mask =
  6382. skb_flow_dissector_target(f->dissector,
  6383. FLOW_DISSECTOR_KEY_PORTS,
  6384. f->mask);
  6385. if (mask->src) {
  6386. if (mask->src == cpu_to_be16(0xffff)) {
  6387. field_flags |= I40E_CLOUD_FIELD_IIP;
  6388. } else {
  6389. dev_err(&pf->pdev->dev, "Bad src port mask 0x%04x\n",
  6390. be16_to_cpu(mask->src));
  6391. return I40E_ERR_CONFIG;
  6392. }
  6393. }
  6394. if (mask->dst) {
  6395. if (mask->dst == cpu_to_be16(0xffff)) {
  6396. field_flags |= I40E_CLOUD_FIELD_IIP;
  6397. } else {
  6398. dev_err(&pf->pdev->dev, "Bad dst port mask 0x%04x\n",
  6399. be16_to_cpu(mask->dst));
  6400. return I40E_ERR_CONFIG;
  6401. }
  6402. }
  6403. filter->dst_port = key->dst;
  6404. filter->src_port = key->src;
  6405. switch (filter->ip_proto) {
  6406. case IPPROTO_TCP:
  6407. case IPPROTO_UDP:
  6408. break;
  6409. default:
  6410. dev_err(&pf->pdev->dev,
  6411. "Only UDP and TCP transport are supported\n");
  6412. return -EINVAL;
  6413. }
  6414. }
  6415. filter->flags = field_flags;
  6416. return 0;
  6417. }
  6418. /**
  6419. * i40e_handle_tclass: Forward to a traffic class on the device
  6420. * @vsi: Pointer to VSI
  6421. * @tc: traffic class index on the device
  6422. * @filter: Pointer to cloud filter structure
  6423. *
  6424. **/
  6425. static int i40e_handle_tclass(struct i40e_vsi *vsi, u32 tc,
  6426. struct i40e_cloud_filter *filter)
  6427. {
  6428. struct i40e_channel *ch, *ch_tmp;
  6429. /* direct to a traffic class on the same device */
  6430. if (tc == 0) {
  6431. filter->seid = vsi->seid;
  6432. return 0;
  6433. } else if (vsi->tc_config.enabled_tc & BIT(tc)) {
  6434. if (!filter->dst_port) {
  6435. dev_err(&vsi->back->pdev->dev,
  6436. "Specify destination port to direct to traffic class that is not default\n");
  6437. return -EINVAL;
  6438. }
  6439. if (list_empty(&vsi->ch_list))
  6440. return -EINVAL;
  6441. list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list,
  6442. list) {
  6443. if (ch->seid == vsi->tc_seid_map[tc])
  6444. filter->seid = ch->seid;
  6445. }
  6446. return 0;
  6447. }
  6448. dev_err(&vsi->back->pdev->dev, "TC is not enabled\n");
  6449. return -EINVAL;
  6450. }
  6451. /**
  6452. * i40e_configure_clsflower - Configure tc flower filters
  6453. * @vsi: Pointer to VSI
  6454. * @cls_flower: Pointer to struct tc_cls_flower_offload
  6455. *
  6456. **/
  6457. static int i40e_configure_clsflower(struct i40e_vsi *vsi,
  6458. struct tc_cls_flower_offload *cls_flower)
  6459. {
  6460. int tc = tc_classid_to_hwtc(vsi->netdev, cls_flower->classid);
  6461. struct i40e_cloud_filter *filter = NULL;
  6462. struct i40e_pf *pf = vsi->back;
  6463. int err = 0;
  6464. if (tc < 0) {
  6465. dev_err(&vsi->back->pdev->dev, "Invalid traffic class\n");
  6466. return -EOPNOTSUPP;
  6467. }
  6468. if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
  6469. test_bit(__I40E_RESET_INTR_RECEIVED, pf->state))
  6470. return -EBUSY;
  6471. if (pf->fdir_pf_active_filters ||
  6472. (!hlist_empty(&pf->fdir_filter_list))) {
  6473. dev_err(&vsi->back->pdev->dev,
  6474. "Flow Director Sideband filters exists, turn ntuple off to configure cloud filters\n");
  6475. return -EINVAL;
  6476. }
  6477. if (vsi->back->flags & I40E_FLAG_FD_SB_ENABLED) {
  6478. dev_err(&vsi->back->pdev->dev,
  6479. "Disable Flow Director Sideband, configuring Cloud filters via tc-flower\n");
  6480. vsi->back->flags &= ~I40E_FLAG_FD_SB_ENABLED;
  6481. vsi->back->flags |= I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
  6482. }
  6483. filter = kzalloc(sizeof(*filter), GFP_KERNEL);
  6484. if (!filter)
  6485. return -ENOMEM;
  6486. filter->cookie = cls_flower->cookie;
  6487. err = i40e_parse_cls_flower(vsi, cls_flower, filter);
  6488. if (err < 0)
  6489. goto err;
  6490. err = i40e_handle_tclass(vsi, tc, filter);
  6491. if (err < 0)
  6492. goto err;
  6493. /* Add cloud filter */
  6494. if (filter->dst_port)
  6495. err = i40e_add_del_cloud_filter_big_buf(vsi, filter, true);
  6496. else
  6497. err = i40e_add_del_cloud_filter(vsi, filter, true);
  6498. if (err) {
  6499. dev_err(&pf->pdev->dev,
  6500. "Failed to add cloud filter, err %s\n",
  6501. i40e_stat_str(&pf->hw, err));
  6502. goto err;
  6503. }
  6504. /* add filter to the ordered list */
  6505. INIT_HLIST_NODE(&filter->cloud_node);
  6506. hlist_add_head(&filter->cloud_node, &pf->cloud_filter_list);
  6507. pf->num_cloud_filters++;
  6508. return err;
  6509. err:
  6510. kfree(filter);
  6511. return err;
  6512. }
  6513. /**
  6514. * i40e_find_cloud_filter - Find the could filter in the list
  6515. * @vsi: Pointer to VSI
  6516. * @cookie: filter specific cookie
  6517. *
  6518. **/
  6519. static struct i40e_cloud_filter *i40e_find_cloud_filter(struct i40e_vsi *vsi,
  6520. unsigned long *cookie)
  6521. {
  6522. struct i40e_cloud_filter *filter = NULL;
  6523. struct hlist_node *node2;
  6524. hlist_for_each_entry_safe(filter, node2,
  6525. &vsi->back->cloud_filter_list, cloud_node)
  6526. if (!memcmp(cookie, &filter->cookie, sizeof(filter->cookie)))
  6527. return filter;
  6528. return NULL;
  6529. }
  6530. /**
  6531. * i40e_delete_clsflower - Remove tc flower filters
  6532. * @vsi: Pointer to VSI
  6533. * @cls_flower: Pointer to struct tc_cls_flower_offload
  6534. *
  6535. **/
  6536. static int i40e_delete_clsflower(struct i40e_vsi *vsi,
  6537. struct tc_cls_flower_offload *cls_flower)
  6538. {
  6539. struct i40e_cloud_filter *filter = NULL;
  6540. struct i40e_pf *pf = vsi->back;
  6541. int err = 0;
  6542. filter = i40e_find_cloud_filter(vsi, &cls_flower->cookie);
  6543. if (!filter)
  6544. return -EINVAL;
  6545. hash_del(&filter->cloud_node);
  6546. if (filter->dst_port)
  6547. err = i40e_add_del_cloud_filter_big_buf(vsi, filter, false);
  6548. else
  6549. err = i40e_add_del_cloud_filter(vsi, filter, false);
  6550. kfree(filter);
  6551. if (err) {
  6552. dev_err(&pf->pdev->dev,
  6553. "Failed to delete cloud filter, err %s\n",
  6554. i40e_stat_str(&pf->hw, err));
  6555. return i40e_aq_rc_to_posix(err, pf->hw.aq.asq_last_status);
  6556. }
  6557. pf->num_cloud_filters--;
  6558. if (!pf->num_cloud_filters)
  6559. if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
  6560. !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
  6561. pf->flags |= I40E_FLAG_FD_SB_ENABLED;
  6562. pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
  6563. pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
  6564. }
  6565. return 0;
  6566. }
  6567. /**
  6568. * i40e_setup_tc_cls_flower - flower classifier offloads
  6569. * @netdev: net device to configure
  6570. * @type_data: offload data
  6571. **/
  6572. static int i40e_setup_tc_cls_flower(struct i40e_netdev_priv *np,
  6573. struct tc_cls_flower_offload *cls_flower)
  6574. {
  6575. struct i40e_vsi *vsi = np->vsi;
  6576. switch (cls_flower->command) {
  6577. case TC_CLSFLOWER_REPLACE:
  6578. return i40e_configure_clsflower(vsi, cls_flower);
  6579. case TC_CLSFLOWER_DESTROY:
  6580. return i40e_delete_clsflower(vsi, cls_flower);
  6581. case TC_CLSFLOWER_STATS:
  6582. return -EOPNOTSUPP;
  6583. default:
  6584. return -EOPNOTSUPP;
  6585. }
  6586. }
  6587. static int i40e_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
  6588. void *cb_priv)
  6589. {
  6590. struct i40e_netdev_priv *np = cb_priv;
  6591. if (!tc_cls_can_offload_and_chain0(np->vsi->netdev, type_data))
  6592. return -EOPNOTSUPP;
  6593. switch (type) {
  6594. case TC_SETUP_CLSFLOWER:
  6595. return i40e_setup_tc_cls_flower(np, type_data);
  6596. default:
  6597. return -EOPNOTSUPP;
  6598. }
  6599. }
  6600. static int i40e_setup_tc_block(struct net_device *dev,
  6601. struct tc_block_offload *f)
  6602. {
  6603. struct i40e_netdev_priv *np = netdev_priv(dev);
  6604. if (f->binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
  6605. return -EOPNOTSUPP;
  6606. switch (f->command) {
  6607. case TC_BLOCK_BIND:
  6608. return tcf_block_cb_register(f->block, i40e_setup_tc_block_cb,
  6609. np, np, f->extack);
  6610. case TC_BLOCK_UNBIND:
  6611. tcf_block_cb_unregister(f->block, i40e_setup_tc_block_cb, np);
  6612. return 0;
  6613. default:
  6614. return -EOPNOTSUPP;
  6615. }
  6616. }
  6617. static int __i40e_setup_tc(struct net_device *netdev, enum tc_setup_type type,
  6618. void *type_data)
  6619. {
  6620. switch (type) {
  6621. case TC_SETUP_QDISC_MQPRIO:
  6622. return i40e_setup_tc(netdev, type_data);
  6623. case TC_SETUP_BLOCK:
  6624. return i40e_setup_tc_block(netdev, type_data);
  6625. default:
  6626. return -EOPNOTSUPP;
  6627. }
  6628. }
  6629. /**
  6630. * i40e_open - Called when a network interface is made active
  6631. * @netdev: network interface device structure
  6632. *
  6633. * The open entry point is called when a network interface is made
  6634. * active by the system (IFF_UP). At this point all resources needed
  6635. * for transmit and receive operations are allocated, the interrupt
  6636. * handler is registered with the OS, the netdev watchdog subtask is
  6637. * enabled, and the stack is notified that the interface is ready.
  6638. *
  6639. * Returns 0 on success, negative value on failure
  6640. **/
  6641. int i40e_open(struct net_device *netdev)
  6642. {
  6643. struct i40e_netdev_priv *np = netdev_priv(netdev);
  6644. struct i40e_vsi *vsi = np->vsi;
  6645. struct i40e_pf *pf = vsi->back;
  6646. int err;
  6647. /* disallow open during test or if eeprom is broken */
  6648. if (test_bit(__I40E_TESTING, pf->state) ||
  6649. test_bit(__I40E_BAD_EEPROM, pf->state))
  6650. return -EBUSY;
  6651. netif_carrier_off(netdev);
  6652. if (i40e_force_link_state(pf, true))
  6653. return -EAGAIN;
  6654. err = i40e_vsi_open(vsi);
  6655. if (err)
  6656. return err;
  6657. /* configure global TSO hardware offload settings */
  6658. wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
  6659. TCP_FLAG_FIN) >> 16);
  6660. wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
  6661. TCP_FLAG_FIN |
  6662. TCP_FLAG_CWR) >> 16);
  6663. wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
  6664. udp_tunnel_get_rx_info(netdev);
  6665. return 0;
  6666. }
  6667. /**
  6668. * i40e_vsi_open -
  6669. * @vsi: the VSI to open
  6670. *
  6671. * Finish initialization of the VSI.
  6672. *
  6673. * Returns 0 on success, negative value on failure
  6674. *
  6675. * Note: expects to be called while under rtnl_lock()
  6676. **/
  6677. int i40e_vsi_open(struct i40e_vsi *vsi)
  6678. {
  6679. struct i40e_pf *pf = vsi->back;
  6680. char int_name[I40E_INT_NAME_STR_LEN];
  6681. int err;
  6682. /* allocate descriptors */
  6683. err = i40e_vsi_setup_tx_resources(vsi);
  6684. if (err)
  6685. goto err_setup_tx;
  6686. err = i40e_vsi_setup_rx_resources(vsi);
  6687. if (err)
  6688. goto err_setup_rx;
  6689. err = i40e_vsi_configure(vsi);
  6690. if (err)
  6691. goto err_setup_rx;
  6692. if (vsi->netdev) {
  6693. snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
  6694. dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
  6695. err = i40e_vsi_request_irq(vsi, int_name);
  6696. if (err)
  6697. goto err_setup_rx;
  6698. /* Notify the stack of the actual queue counts. */
  6699. err = netif_set_real_num_tx_queues(vsi->netdev,
  6700. vsi->num_queue_pairs);
  6701. if (err)
  6702. goto err_set_queues;
  6703. err = netif_set_real_num_rx_queues(vsi->netdev,
  6704. vsi->num_queue_pairs);
  6705. if (err)
  6706. goto err_set_queues;
  6707. } else if (vsi->type == I40E_VSI_FDIR) {
  6708. snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
  6709. dev_driver_string(&pf->pdev->dev),
  6710. dev_name(&pf->pdev->dev));
  6711. err = i40e_vsi_request_irq(vsi, int_name);
  6712. } else {
  6713. err = -EINVAL;
  6714. goto err_setup_rx;
  6715. }
  6716. err = i40e_up_complete(vsi);
  6717. if (err)
  6718. goto err_up_complete;
  6719. return 0;
  6720. err_up_complete:
  6721. i40e_down(vsi);
  6722. err_set_queues:
  6723. i40e_vsi_free_irq(vsi);
  6724. err_setup_rx:
  6725. i40e_vsi_free_rx_resources(vsi);
  6726. err_setup_tx:
  6727. i40e_vsi_free_tx_resources(vsi);
  6728. if (vsi == pf->vsi[pf->lan_vsi])
  6729. i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
  6730. return err;
  6731. }
  6732. /**
  6733. * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
  6734. * @pf: Pointer to PF
  6735. *
  6736. * This function destroys the hlist where all the Flow Director
  6737. * filters were saved.
  6738. **/
  6739. static void i40e_fdir_filter_exit(struct i40e_pf *pf)
  6740. {
  6741. struct i40e_fdir_filter *filter;
  6742. struct i40e_flex_pit *pit_entry, *tmp;
  6743. struct hlist_node *node2;
  6744. hlist_for_each_entry_safe(filter, node2,
  6745. &pf->fdir_filter_list, fdir_node) {
  6746. hlist_del(&filter->fdir_node);
  6747. kfree(filter);
  6748. }
  6749. list_for_each_entry_safe(pit_entry, tmp, &pf->l3_flex_pit_list, list) {
  6750. list_del(&pit_entry->list);
  6751. kfree(pit_entry);
  6752. }
  6753. INIT_LIST_HEAD(&pf->l3_flex_pit_list);
  6754. list_for_each_entry_safe(pit_entry, tmp, &pf->l4_flex_pit_list, list) {
  6755. list_del(&pit_entry->list);
  6756. kfree(pit_entry);
  6757. }
  6758. INIT_LIST_HEAD(&pf->l4_flex_pit_list);
  6759. pf->fdir_pf_active_filters = 0;
  6760. pf->fd_tcp4_filter_cnt = 0;
  6761. pf->fd_udp4_filter_cnt = 0;
  6762. pf->fd_sctp4_filter_cnt = 0;
  6763. pf->fd_ip4_filter_cnt = 0;
  6764. /* Reprogram the default input set for TCP/IPv4 */
  6765. i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
  6766. I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
  6767. I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
  6768. /* Reprogram the default input set for UDP/IPv4 */
  6769. i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_UDP,
  6770. I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
  6771. I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
  6772. /* Reprogram the default input set for SCTP/IPv4 */
  6773. i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_SCTP,
  6774. I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
  6775. I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
  6776. /* Reprogram the default input set for Other/IPv4 */
  6777. i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_OTHER,
  6778. I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
  6779. i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_FRAG_IPV4,
  6780. I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
  6781. }
  6782. /**
  6783. * i40e_cloud_filter_exit - Cleans up the cloud filters
  6784. * @pf: Pointer to PF
  6785. *
  6786. * This function destroys the hlist where all the cloud filters
  6787. * were saved.
  6788. **/
  6789. static void i40e_cloud_filter_exit(struct i40e_pf *pf)
  6790. {
  6791. struct i40e_cloud_filter *cfilter;
  6792. struct hlist_node *node;
  6793. hlist_for_each_entry_safe(cfilter, node,
  6794. &pf->cloud_filter_list, cloud_node) {
  6795. hlist_del(&cfilter->cloud_node);
  6796. kfree(cfilter);
  6797. }
  6798. pf->num_cloud_filters = 0;
  6799. if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
  6800. !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
  6801. pf->flags |= I40E_FLAG_FD_SB_ENABLED;
  6802. pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
  6803. pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
  6804. }
  6805. }
  6806. /**
  6807. * i40e_close - Disables a network interface
  6808. * @netdev: network interface device structure
  6809. *
  6810. * The close entry point is called when an interface is de-activated
  6811. * by the OS. The hardware is still under the driver's control, but
  6812. * this netdev interface is disabled.
  6813. *
  6814. * Returns 0, this is not allowed to fail
  6815. **/
  6816. int i40e_close(struct net_device *netdev)
  6817. {
  6818. struct i40e_netdev_priv *np = netdev_priv(netdev);
  6819. struct i40e_vsi *vsi = np->vsi;
  6820. i40e_vsi_close(vsi);
  6821. return 0;
  6822. }
  6823. /**
  6824. * i40e_do_reset - Start a PF or Core Reset sequence
  6825. * @pf: board private structure
  6826. * @reset_flags: which reset is requested
  6827. * @lock_acquired: indicates whether or not the lock has been acquired
  6828. * before this function was called.
  6829. *
  6830. * The essential difference in resets is that the PF Reset
  6831. * doesn't clear the packet buffers, doesn't reset the PE
  6832. * firmware, and doesn't bother the other PFs on the chip.
  6833. **/
  6834. void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags, bool lock_acquired)
  6835. {
  6836. u32 val;
  6837. WARN_ON(in_interrupt());
  6838. /* do the biggest reset indicated */
  6839. if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
  6840. /* Request a Global Reset
  6841. *
  6842. * This will start the chip's countdown to the actual full
  6843. * chip reset event, and a warning interrupt to be sent
  6844. * to all PFs, including the requestor. Our handler
  6845. * for the warning interrupt will deal with the shutdown
  6846. * and recovery of the switch setup.
  6847. */
  6848. dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
  6849. val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
  6850. val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
  6851. wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
  6852. } else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
  6853. /* Request a Core Reset
  6854. *
  6855. * Same as Global Reset, except does *not* include the MAC/PHY
  6856. */
  6857. dev_dbg(&pf->pdev->dev, "CoreR requested\n");
  6858. val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
  6859. val |= I40E_GLGEN_RTRIG_CORER_MASK;
  6860. wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
  6861. i40e_flush(&pf->hw);
  6862. } else if (reset_flags & I40E_PF_RESET_FLAG) {
  6863. /* Request a PF Reset
  6864. *
  6865. * Resets only the PF-specific registers
  6866. *
  6867. * This goes directly to the tear-down and rebuild of
  6868. * the switch, since we need to do all the recovery as
  6869. * for the Core Reset.
  6870. */
  6871. dev_dbg(&pf->pdev->dev, "PFR requested\n");
  6872. i40e_handle_reset_warning(pf, lock_acquired);
  6873. } else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
  6874. int v;
  6875. /* Find the VSI(s) that requested a re-init */
  6876. dev_info(&pf->pdev->dev,
  6877. "VSI reinit requested\n");
  6878. for (v = 0; v < pf->num_alloc_vsi; v++) {
  6879. struct i40e_vsi *vsi = pf->vsi[v];
  6880. if (vsi != NULL &&
  6881. test_and_clear_bit(__I40E_VSI_REINIT_REQUESTED,
  6882. vsi->state))
  6883. i40e_vsi_reinit_locked(pf->vsi[v]);
  6884. }
  6885. } else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
  6886. int v;
  6887. /* Find the VSI(s) that needs to be brought down */
  6888. dev_info(&pf->pdev->dev, "VSI down requested\n");
  6889. for (v = 0; v < pf->num_alloc_vsi; v++) {
  6890. struct i40e_vsi *vsi = pf->vsi[v];
  6891. if (vsi != NULL &&
  6892. test_and_clear_bit(__I40E_VSI_DOWN_REQUESTED,
  6893. vsi->state)) {
  6894. set_bit(__I40E_VSI_DOWN, vsi->state);
  6895. i40e_down(vsi);
  6896. }
  6897. }
  6898. } else {
  6899. dev_info(&pf->pdev->dev,
  6900. "bad reset request 0x%08x\n", reset_flags);
  6901. }
  6902. }
  6903. #ifdef CONFIG_I40E_DCB
  6904. /**
  6905. * i40e_dcb_need_reconfig - Check if DCB needs reconfig
  6906. * @pf: board private structure
  6907. * @old_cfg: current DCB config
  6908. * @new_cfg: new DCB config
  6909. **/
  6910. bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
  6911. struct i40e_dcbx_config *old_cfg,
  6912. struct i40e_dcbx_config *new_cfg)
  6913. {
  6914. bool need_reconfig = false;
  6915. /* Check if ETS configuration has changed */
  6916. if (memcmp(&new_cfg->etscfg,
  6917. &old_cfg->etscfg,
  6918. sizeof(new_cfg->etscfg))) {
  6919. /* If Priority Table has changed reconfig is needed */
  6920. if (memcmp(&new_cfg->etscfg.prioritytable,
  6921. &old_cfg->etscfg.prioritytable,
  6922. sizeof(new_cfg->etscfg.prioritytable))) {
  6923. need_reconfig = true;
  6924. dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
  6925. }
  6926. if (memcmp(&new_cfg->etscfg.tcbwtable,
  6927. &old_cfg->etscfg.tcbwtable,
  6928. sizeof(new_cfg->etscfg.tcbwtable)))
  6929. dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
  6930. if (memcmp(&new_cfg->etscfg.tsatable,
  6931. &old_cfg->etscfg.tsatable,
  6932. sizeof(new_cfg->etscfg.tsatable)))
  6933. dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
  6934. }
  6935. /* Check if PFC configuration has changed */
  6936. if (memcmp(&new_cfg->pfc,
  6937. &old_cfg->pfc,
  6938. sizeof(new_cfg->pfc))) {
  6939. need_reconfig = true;
  6940. dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
  6941. }
  6942. /* Check if APP Table has changed */
  6943. if (memcmp(&new_cfg->app,
  6944. &old_cfg->app,
  6945. sizeof(new_cfg->app))) {
  6946. need_reconfig = true;
  6947. dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
  6948. }
  6949. dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
  6950. return need_reconfig;
  6951. }
  6952. /**
  6953. * i40e_handle_lldp_event - Handle LLDP Change MIB event
  6954. * @pf: board private structure
  6955. * @e: event info posted on ARQ
  6956. **/
  6957. static int i40e_handle_lldp_event(struct i40e_pf *pf,
  6958. struct i40e_arq_event_info *e)
  6959. {
  6960. struct i40e_aqc_lldp_get_mib *mib =
  6961. (struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
  6962. struct i40e_hw *hw = &pf->hw;
  6963. struct i40e_dcbx_config tmp_dcbx_cfg;
  6964. bool need_reconfig = false;
  6965. int ret = 0;
  6966. u8 type;
  6967. /* Not DCB capable or capability disabled */
  6968. if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
  6969. return ret;
  6970. /* Ignore if event is not for Nearest Bridge */
  6971. type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
  6972. & I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
  6973. dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
  6974. if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
  6975. return ret;
  6976. /* Check MIB Type and return if event for Remote MIB update */
  6977. type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
  6978. dev_dbg(&pf->pdev->dev,
  6979. "LLDP event mib type %s\n", type ? "remote" : "local");
  6980. if (type == I40E_AQ_LLDP_MIB_REMOTE) {
  6981. /* Update the remote cached instance and return */
  6982. ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
  6983. I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
  6984. &hw->remote_dcbx_config);
  6985. goto exit;
  6986. }
  6987. /* Store the old configuration */
  6988. tmp_dcbx_cfg = hw->local_dcbx_config;
  6989. /* Reset the old DCBx configuration data */
  6990. memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
  6991. /* Get updated DCBX data from firmware */
  6992. ret = i40e_get_dcb_config(&pf->hw);
  6993. if (ret) {
  6994. dev_info(&pf->pdev->dev,
  6995. "Failed querying DCB configuration data from firmware, err %s aq_err %s\n",
  6996. i40e_stat_str(&pf->hw, ret),
  6997. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  6998. goto exit;
  6999. }
  7000. /* No change detected in DCBX configs */
  7001. if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
  7002. sizeof(tmp_dcbx_cfg))) {
  7003. dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
  7004. goto exit;
  7005. }
  7006. need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
  7007. &hw->local_dcbx_config);
  7008. i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
  7009. if (!need_reconfig)
  7010. goto exit;
  7011. /* Enable DCB tagging only when more than one TC */
  7012. if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
  7013. pf->flags |= I40E_FLAG_DCB_ENABLED;
  7014. else
  7015. pf->flags &= ~I40E_FLAG_DCB_ENABLED;
  7016. set_bit(__I40E_PORT_SUSPENDED, pf->state);
  7017. /* Reconfiguration needed quiesce all VSIs */
  7018. i40e_pf_quiesce_all_vsi(pf);
  7019. /* Changes in configuration update VEB/VSI */
  7020. i40e_dcb_reconfigure(pf);
  7021. ret = i40e_resume_port_tx(pf);
  7022. clear_bit(__I40E_PORT_SUSPENDED, pf->state);
  7023. /* In case of error no point in resuming VSIs */
  7024. if (ret)
  7025. goto exit;
  7026. /* Wait for the PF's queues to be disabled */
  7027. ret = i40e_pf_wait_queues_disabled(pf);
  7028. if (ret) {
  7029. /* Schedule PF reset to recover */
  7030. set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
  7031. i40e_service_event_schedule(pf);
  7032. } else {
  7033. i40e_pf_unquiesce_all_vsi(pf);
  7034. set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
  7035. set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
  7036. }
  7037. exit:
  7038. return ret;
  7039. }
  7040. #endif /* CONFIG_I40E_DCB */
  7041. /**
  7042. * i40e_do_reset_safe - Protected reset path for userland calls.
  7043. * @pf: board private structure
  7044. * @reset_flags: which reset is requested
  7045. *
  7046. **/
  7047. void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
  7048. {
  7049. rtnl_lock();
  7050. i40e_do_reset(pf, reset_flags, true);
  7051. rtnl_unlock();
  7052. }
  7053. /**
  7054. * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
  7055. * @pf: board private structure
  7056. * @e: event info posted on ARQ
  7057. *
  7058. * Handler for LAN Queue Overflow Event generated by the firmware for PF
  7059. * and VF queues
  7060. **/
  7061. static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
  7062. struct i40e_arq_event_info *e)
  7063. {
  7064. struct i40e_aqc_lan_overflow *data =
  7065. (struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
  7066. u32 queue = le32_to_cpu(data->prtdcb_rupto);
  7067. u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
  7068. struct i40e_hw *hw = &pf->hw;
  7069. struct i40e_vf *vf;
  7070. u16 vf_id;
  7071. dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
  7072. queue, qtx_ctl);
  7073. /* Queue belongs to VF, find the VF and issue VF reset */
  7074. if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
  7075. >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
  7076. vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
  7077. >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
  7078. vf_id -= hw->func_caps.vf_base_id;
  7079. vf = &pf->vf[vf_id];
  7080. i40e_vc_notify_vf_reset(vf);
  7081. /* Allow VF to process pending reset notification */
  7082. msleep(20);
  7083. i40e_reset_vf(vf, false);
  7084. }
  7085. }
  7086. /**
  7087. * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
  7088. * @pf: board private structure
  7089. **/
  7090. u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
  7091. {
  7092. u32 val, fcnt_prog;
  7093. val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
  7094. fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
  7095. return fcnt_prog;
  7096. }
  7097. /**
  7098. * i40e_get_current_fd_count - Get total FD filters programmed for this PF
  7099. * @pf: board private structure
  7100. **/
  7101. u32 i40e_get_current_fd_count(struct i40e_pf *pf)
  7102. {
  7103. u32 val, fcnt_prog;
  7104. val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
  7105. fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
  7106. ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
  7107. I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
  7108. return fcnt_prog;
  7109. }
  7110. /**
  7111. * i40e_get_global_fd_count - Get total FD filters programmed on device
  7112. * @pf: board private structure
  7113. **/
  7114. u32 i40e_get_global_fd_count(struct i40e_pf *pf)
  7115. {
  7116. u32 val, fcnt_prog;
  7117. val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
  7118. fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
  7119. ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >>
  7120. I40E_GLQF_FDCNT_0_BESTCNT_SHIFT);
  7121. return fcnt_prog;
  7122. }
  7123. /**
  7124. * i40e_reenable_fdir_sb - Restore FDir SB capability
  7125. * @pf: board private structure
  7126. **/
  7127. static void i40e_reenable_fdir_sb(struct i40e_pf *pf)
  7128. {
  7129. if (test_and_clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
  7130. if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
  7131. (I40E_DEBUG_FD & pf->hw.debug_mask))
  7132. dev_info(&pf->pdev->dev, "FD Sideband/ntuple is being enabled since we have space in the table now\n");
  7133. }
  7134. /**
  7135. * i40e_reenable_fdir_atr - Restore FDir ATR capability
  7136. * @pf: board private structure
  7137. **/
  7138. static void i40e_reenable_fdir_atr(struct i40e_pf *pf)
  7139. {
  7140. if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state)) {
  7141. /* ATR uses the same filtering logic as SB rules. It only
  7142. * functions properly if the input set mask is at the default
  7143. * settings. It is safe to restore the default input set
  7144. * because there are no active TCPv4 filter rules.
  7145. */
  7146. i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
  7147. I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
  7148. I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
  7149. if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
  7150. (I40E_DEBUG_FD & pf->hw.debug_mask))
  7151. dev_info(&pf->pdev->dev, "ATR is being enabled since we have space in the table and there are no conflicting ntuple rules\n");
  7152. }
  7153. }
  7154. /**
  7155. * i40e_delete_invalid_filter - Delete an invalid FDIR filter
  7156. * @pf: board private structure
  7157. * @filter: FDir filter to remove
  7158. */
  7159. static void i40e_delete_invalid_filter(struct i40e_pf *pf,
  7160. struct i40e_fdir_filter *filter)
  7161. {
  7162. /* Update counters */
  7163. pf->fdir_pf_active_filters--;
  7164. pf->fd_inv = 0;
  7165. switch (filter->flow_type) {
  7166. case TCP_V4_FLOW:
  7167. pf->fd_tcp4_filter_cnt--;
  7168. break;
  7169. case UDP_V4_FLOW:
  7170. pf->fd_udp4_filter_cnt--;
  7171. break;
  7172. case SCTP_V4_FLOW:
  7173. pf->fd_sctp4_filter_cnt--;
  7174. break;
  7175. case IP_USER_FLOW:
  7176. switch (filter->ip4_proto) {
  7177. case IPPROTO_TCP:
  7178. pf->fd_tcp4_filter_cnt--;
  7179. break;
  7180. case IPPROTO_UDP:
  7181. pf->fd_udp4_filter_cnt--;
  7182. break;
  7183. case IPPROTO_SCTP:
  7184. pf->fd_sctp4_filter_cnt--;
  7185. break;
  7186. case IPPROTO_IP:
  7187. pf->fd_ip4_filter_cnt--;
  7188. break;
  7189. }
  7190. break;
  7191. }
  7192. /* Remove the filter from the list and free memory */
  7193. hlist_del(&filter->fdir_node);
  7194. kfree(filter);
  7195. }
  7196. /**
  7197. * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
  7198. * @pf: board private structure
  7199. **/
  7200. void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
  7201. {
  7202. struct i40e_fdir_filter *filter;
  7203. u32 fcnt_prog, fcnt_avail;
  7204. struct hlist_node *node;
  7205. if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
  7206. return;
  7207. /* Check if we have enough room to re-enable FDir SB capability. */
  7208. fcnt_prog = i40e_get_global_fd_count(pf);
  7209. fcnt_avail = pf->fdir_pf_filter_count;
  7210. if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
  7211. (pf->fd_add_err == 0) ||
  7212. (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt))
  7213. i40e_reenable_fdir_sb(pf);
  7214. /* We should wait for even more space before re-enabling ATR.
  7215. * Additionally, we cannot enable ATR as long as we still have TCP SB
  7216. * rules active.
  7217. */
  7218. if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) &&
  7219. (pf->fd_tcp4_filter_cnt == 0))
  7220. i40e_reenable_fdir_atr(pf);
  7221. /* if hw had a problem adding a filter, delete it */
  7222. if (pf->fd_inv > 0) {
  7223. hlist_for_each_entry_safe(filter, node,
  7224. &pf->fdir_filter_list, fdir_node)
  7225. if (filter->fd_id == pf->fd_inv)
  7226. i40e_delete_invalid_filter(pf, filter);
  7227. }
  7228. }
  7229. #define I40E_MIN_FD_FLUSH_INTERVAL 10
  7230. #define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
  7231. /**
  7232. * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
  7233. * @pf: board private structure
  7234. **/
  7235. static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
  7236. {
  7237. unsigned long min_flush_time;
  7238. int flush_wait_retry = 50;
  7239. bool disable_atr = false;
  7240. int fd_room;
  7241. int reg;
  7242. if (!time_after(jiffies, pf->fd_flush_timestamp +
  7243. (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
  7244. return;
  7245. /* If the flush is happening too quick and we have mostly SB rules we
  7246. * should not re-enable ATR for some time.
  7247. */
  7248. min_flush_time = pf->fd_flush_timestamp +
  7249. (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
  7250. fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
  7251. if (!(time_after(jiffies, min_flush_time)) &&
  7252. (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
  7253. if (I40E_DEBUG_FD & pf->hw.debug_mask)
  7254. dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
  7255. disable_atr = true;
  7256. }
  7257. pf->fd_flush_timestamp = jiffies;
  7258. set_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
  7259. /* flush all filters */
  7260. wr32(&pf->hw, I40E_PFQF_CTL_1,
  7261. I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
  7262. i40e_flush(&pf->hw);
  7263. pf->fd_flush_cnt++;
  7264. pf->fd_add_err = 0;
  7265. do {
  7266. /* Check FD flush status every 5-6msec */
  7267. usleep_range(5000, 6000);
  7268. reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
  7269. if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
  7270. break;
  7271. } while (flush_wait_retry--);
  7272. if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
  7273. dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
  7274. } else {
  7275. /* replay sideband filters */
  7276. i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
  7277. if (!disable_atr && !pf->fd_tcp4_filter_cnt)
  7278. clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
  7279. clear_bit(__I40E_FD_FLUSH_REQUESTED, pf->state);
  7280. if (I40E_DEBUG_FD & pf->hw.debug_mask)
  7281. dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
  7282. }
  7283. }
  7284. /**
  7285. * i40e_get_current_atr_count - Get the count of total FD ATR filters programmed
  7286. * @pf: board private structure
  7287. **/
  7288. u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
  7289. {
  7290. return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
  7291. }
  7292. /* We can see up to 256 filter programming desc in transit if the filters are
  7293. * being applied really fast; before we see the first
  7294. * filter miss error on Rx queue 0. Accumulating enough error messages before
  7295. * reacting will make sure we don't cause flush too often.
  7296. */
  7297. #define I40E_MAX_FD_PROGRAM_ERROR 256
  7298. /**
  7299. * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
  7300. * @pf: board private structure
  7301. **/
  7302. static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
  7303. {
  7304. /* if interface is down do nothing */
  7305. if (test_bit(__I40E_DOWN, pf->state))
  7306. return;
  7307. if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
  7308. i40e_fdir_flush_and_replay(pf);
  7309. i40e_fdir_check_and_reenable(pf);
  7310. }
  7311. /**
  7312. * i40e_vsi_link_event - notify VSI of a link event
  7313. * @vsi: vsi to be notified
  7314. * @link_up: link up or down
  7315. **/
  7316. static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
  7317. {
  7318. if (!vsi || test_bit(__I40E_VSI_DOWN, vsi->state))
  7319. return;
  7320. switch (vsi->type) {
  7321. case I40E_VSI_MAIN:
  7322. if (!vsi->netdev || !vsi->netdev_registered)
  7323. break;
  7324. if (link_up) {
  7325. netif_carrier_on(vsi->netdev);
  7326. netif_tx_wake_all_queues(vsi->netdev);
  7327. } else {
  7328. netif_carrier_off(vsi->netdev);
  7329. netif_tx_stop_all_queues(vsi->netdev);
  7330. }
  7331. break;
  7332. case I40E_VSI_SRIOV:
  7333. case I40E_VSI_VMDQ2:
  7334. case I40E_VSI_CTRL:
  7335. case I40E_VSI_IWARP:
  7336. case I40E_VSI_MIRROR:
  7337. default:
  7338. /* there is no notification for other VSIs */
  7339. break;
  7340. }
  7341. }
  7342. /**
  7343. * i40e_veb_link_event - notify elements on the veb of a link event
  7344. * @veb: veb to be notified
  7345. * @link_up: link up or down
  7346. **/
  7347. static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
  7348. {
  7349. struct i40e_pf *pf;
  7350. int i;
  7351. if (!veb || !veb->pf)
  7352. return;
  7353. pf = veb->pf;
  7354. /* depth first... */
  7355. for (i = 0; i < I40E_MAX_VEB; i++)
  7356. if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
  7357. i40e_veb_link_event(pf->veb[i], link_up);
  7358. /* ... now the local VSIs */
  7359. for (i = 0; i < pf->num_alloc_vsi; i++)
  7360. if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
  7361. i40e_vsi_link_event(pf->vsi[i], link_up);
  7362. }
  7363. /**
  7364. * i40e_link_event - Update netif_carrier status
  7365. * @pf: board private structure
  7366. **/
  7367. static void i40e_link_event(struct i40e_pf *pf)
  7368. {
  7369. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  7370. u8 new_link_speed, old_link_speed;
  7371. i40e_status status;
  7372. bool new_link, old_link;
  7373. /* save off old link status information */
  7374. pf->hw.phy.link_info_old = pf->hw.phy.link_info;
  7375. /* set this to force the get_link_status call to refresh state */
  7376. pf->hw.phy.get_link_info = true;
  7377. old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
  7378. status = i40e_get_link_status(&pf->hw, &new_link);
  7379. /* On success, disable temp link polling */
  7380. if (status == I40E_SUCCESS) {
  7381. clear_bit(__I40E_TEMP_LINK_POLLING, pf->state);
  7382. } else {
  7383. /* Enable link polling temporarily until i40e_get_link_status
  7384. * returns I40E_SUCCESS
  7385. */
  7386. set_bit(__I40E_TEMP_LINK_POLLING, pf->state);
  7387. dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
  7388. status);
  7389. return;
  7390. }
  7391. old_link_speed = pf->hw.phy.link_info_old.link_speed;
  7392. new_link_speed = pf->hw.phy.link_info.link_speed;
  7393. if (new_link == old_link &&
  7394. new_link_speed == old_link_speed &&
  7395. (test_bit(__I40E_VSI_DOWN, vsi->state) ||
  7396. new_link == netif_carrier_ok(vsi->netdev)))
  7397. return;
  7398. i40e_print_link_message(vsi, new_link);
  7399. /* Notify the base of the switch tree connected to
  7400. * the link. Floating VEBs are not notified.
  7401. */
  7402. if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
  7403. i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
  7404. else
  7405. i40e_vsi_link_event(vsi, new_link);
  7406. if (pf->vf)
  7407. i40e_vc_notify_link_state(pf);
  7408. if (pf->flags & I40E_FLAG_PTP)
  7409. i40e_ptp_set_increment(pf);
  7410. }
  7411. /**
  7412. * i40e_watchdog_subtask - periodic checks not using event driven response
  7413. * @pf: board private structure
  7414. **/
  7415. static void i40e_watchdog_subtask(struct i40e_pf *pf)
  7416. {
  7417. int i;
  7418. /* if interface is down do nothing */
  7419. if (test_bit(__I40E_DOWN, pf->state) ||
  7420. test_bit(__I40E_CONFIG_BUSY, pf->state))
  7421. return;
  7422. /* make sure we don't do these things too often */
  7423. if (time_before(jiffies, (pf->service_timer_previous +
  7424. pf->service_timer_period)))
  7425. return;
  7426. pf->service_timer_previous = jiffies;
  7427. if ((pf->flags & I40E_FLAG_LINK_POLLING_ENABLED) ||
  7428. test_bit(__I40E_TEMP_LINK_POLLING, pf->state))
  7429. i40e_link_event(pf);
  7430. /* Update the stats for active netdevs so the network stack
  7431. * can look at updated numbers whenever it cares to
  7432. */
  7433. for (i = 0; i < pf->num_alloc_vsi; i++)
  7434. if (pf->vsi[i] && pf->vsi[i]->netdev)
  7435. i40e_update_stats(pf->vsi[i]);
  7436. if (pf->flags & I40E_FLAG_VEB_STATS_ENABLED) {
  7437. /* Update the stats for the active switching components */
  7438. for (i = 0; i < I40E_MAX_VEB; i++)
  7439. if (pf->veb[i])
  7440. i40e_update_veb_stats(pf->veb[i]);
  7441. }
  7442. i40e_ptp_rx_hang(pf);
  7443. i40e_ptp_tx_hang(pf);
  7444. }
  7445. /**
  7446. * i40e_reset_subtask - Set up for resetting the device and driver
  7447. * @pf: board private structure
  7448. **/
  7449. static void i40e_reset_subtask(struct i40e_pf *pf)
  7450. {
  7451. u32 reset_flags = 0;
  7452. if (test_bit(__I40E_REINIT_REQUESTED, pf->state)) {
  7453. reset_flags |= BIT(__I40E_REINIT_REQUESTED);
  7454. clear_bit(__I40E_REINIT_REQUESTED, pf->state);
  7455. }
  7456. if (test_bit(__I40E_PF_RESET_REQUESTED, pf->state)) {
  7457. reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
  7458. clear_bit(__I40E_PF_RESET_REQUESTED, pf->state);
  7459. }
  7460. if (test_bit(__I40E_CORE_RESET_REQUESTED, pf->state)) {
  7461. reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
  7462. clear_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
  7463. }
  7464. if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state)) {
  7465. reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
  7466. clear_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
  7467. }
  7468. if (test_bit(__I40E_DOWN_REQUESTED, pf->state)) {
  7469. reset_flags |= BIT(__I40E_DOWN_REQUESTED);
  7470. clear_bit(__I40E_DOWN_REQUESTED, pf->state);
  7471. }
  7472. /* If there's a recovery already waiting, it takes
  7473. * precedence before starting a new reset sequence.
  7474. */
  7475. if (test_bit(__I40E_RESET_INTR_RECEIVED, pf->state)) {
  7476. i40e_prep_for_reset(pf, false);
  7477. i40e_reset(pf);
  7478. i40e_rebuild(pf, false, false);
  7479. }
  7480. /* If we're already down or resetting, just bail */
  7481. if (reset_flags &&
  7482. !test_bit(__I40E_DOWN, pf->state) &&
  7483. !test_bit(__I40E_CONFIG_BUSY, pf->state)) {
  7484. i40e_do_reset(pf, reset_flags, false);
  7485. }
  7486. }
  7487. /**
  7488. * i40e_handle_link_event - Handle link event
  7489. * @pf: board private structure
  7490. * @e: event info posted on ARQ
  7491. **/
  7492. static void i40e_handle_link_event(struct i40e_pf *pf,
  7493. struct i40e_arq_event_info *e)
  7494. {
  7495. struct i40e_aqc_get_link_status *status =
  7496. (struct i40e_aqc_get_link_status *)&e->desc.params.raw;
  7497. /* Do a new status request to re-enable LSE reporting
  7498. * and load new status information into the hw struct
  7499. * This completely ignores any state information
  7500. * in the ARQ event info, instead choosing to always
  7501. * issue the AQ update link status command.
  7502. */
  7503. i40e_link_event(pf);
  7504. /* Check if module meets thermal requirements */
  7505. if (status->phy_type == I40E_PHY_TYPE_NOT_SUPPORTED_HIGH_TEMP) {
  7506. dev_err(&pf->pdev->dev,
  7507. "Rx/Tx is disabled on this device because the module does not meet thermal requirements.\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. } else {
  7511. /* check for unqualified module, if link is down, suppress
  7512. * the message if link was forced to be down.
  7513. */
  7514. if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
  7515. (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
  7516. (!(status->link_info & I40E_AQ_LINK_UP)) &&
  7517. (!(pf->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED))) {
  7518. dev_err(&pf->pdev->dev,
  7519. "Rx/Tx is disabled on this device because an unsupported SFP module type was detected.\n");
  7520. dev_err(&pf->pdev->dev,
  7521. "Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
  7522. }
  7523. }
  7524. }
  7525. /**
  7526. * i40e_clean_adminq_subtask - Clean the AdminQ rings
  7527. * @pf: board private structure
  7528. **/
  7529. static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
  7530. {
  7531. struct i40e_arq_event_info event;
  7532. struct i40e_hw *hw = &pf->hw;
  7533. u16 pending, i = 0;
  7534. i40e_status ret;
  7535. u16 opcode;
  7536. u32 oldval;
  7537. u32 val;
  7538. /* Do not run clean AQ when PF reset fails */
  7539. if (test_bit(__I40E_RESET_FAILED, pf->state))
  7540. return;
  7541. /* check for error indications */
  7542. val = rd32(&pf->hw, pf->hw.aq.arq.len);
  7543. oldval = val;
  7544. if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
  7545. if (hw->debug_mask & I40E_DEBUG_AQ)
  7546. dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
  7547. val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
  7548. }
  7549. if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
  7550. if (hw->debug_mask & I40E_DEBUG_AQ)
  7551. dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
  7552. val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
  7553. pf->arq_overflows++;
  7554. }
  7555. if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
  7556. if (hw->debug_mask & I40E_DEBUG_AQ)
  7557. dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
  7558. val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
  7559. }
  7560. if (oldval != val)
  7561. wr32(&pf->hw, pf->hw.aq.arq.len, val);
  7562. val = rd32(&pf->hw, pf->hw.aq.asq.len);
  7563. oldval = val;
  7564. if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
  7565. if (pf->hw.debug_mask & I40E_DEBUG_AQ)
  7566. dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
  7567. val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
  7568. }
  7569. if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
  7570. if (pf->hw.debug_mask & I40E_DEBUG_AQ)
  7571. dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
  7572. val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
  7573. }
  7574. if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
  7575. if (pf->hw.debug_mask & I40E_DEBUG_AQ)
  7576. dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
  7577. val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
  7578. }
  7579. if (oldval != val)
  7580. wr32(&pf->hw, pf->hw.aq.asq.len, val);
  7581. event.buf_len = I40E_MAX_AQ_BUF_SIZE;
  7582. event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
  7583. if (!event.msg_buf)
  7584. return;
  7585. do {
  7586. ret = i40e_clean_arq_element(hw, &event, &pending);
  7587. if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK)
  7588. break;
  7589. else if (ret) {
  7590. dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
  7591. break;
  7592. }
  7593. opcode = le16_to_cpu(event.desc.opcode);
  7594. switch (opcode) {
  7595. case i40e_aqc_opc_get_link_status:
  7596. i40e_handle_link_event(pf, &event);
  7597. break;
  7598. case i40e_aqc_opc_send_msg_to_pf:
  7599. ret = i40e_vc_process_vf_msg(pf,
  7600. le16_to_cpu(event.desc.retval),
  7601. le32_to_cpu(event.desc.cookie_high),
  7602. le32_to_cpu(event.desc.cookie_low),
  7603. event.msg_buf,
  7604. event.msg_len);
  7605. break;
  7606. case i40e_aqc_opc_lldp_update_mib:
  7607. dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
  7608. #ifdef CONFIG_I40E_DCB
  7609. rtnl_lock();
  7610. ret = i40e_handle_lldp_event(pf, &event);
  7611. rtnl_unlock();
  7612. #endif /* CONFIG_I40E_DCB */
  7613. break;
  7614. case i40e_aqc_opc_event_lan_overflow:
  7615. dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
  7616. i40e_handle_lan_overflow_event(pf, &event);
  7617. break;
  7618. case i40e_aqc_opc_send_msg_to_peer:
  7619. dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
  7620. break;
  7621. case i40e_aqc_opc_nvm_erase:
  7622. case i40e_aqc_opc_nvm_update:
  7623. case i40e_aqc_opc_oem_post_update:
  7624. i40e_debug(&pf->hw, I40E_DEBUG_NVM,
  7625. "ARQ NVM operation 0x%04x completed\n",
  7626. opcode);
  7627. break;
  7628. default:
  7629. dev_info(&pf->pdev->dev,
  7630. "ARQ: Unknown event 0x%04x ignored\n",
  7631. opcode);
  7632. break;
  7633. }
  7634. } while (i++ < pf->adminq_work_limit);
  7635. if (i < pf->adminq_work_limit)
  7636. clear_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
  7637. /* re-enable Admin queue interrupt cause */
  7638. val = rd32(hw, I40E_PFINT_ICR0_ENA);
  7639. val |= I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
  7640. wr32(hw, I40E_PFINT_ICR0_ENA, val);
  7641. i40e_flush(hw);
  7642. kfree(event.msg_buf);
  7643. }
  7644. /**
  7645. * i40e_verify_eeprom - make sure eeprom is good to use
  7646. * @pf: board private structure
  7647. **/
  7648. static void i40e_verify_eeprom(struct i40e_pf *pf)
  7649. {
  7650. int err;
  7651. err = i40e_diag_eeprom_test(&pf->hw);
  7652. if (err) {
  7653. /* retry in case of garbage read */
  7654. err = i40e_diag_eeprom_test(&pf->hw);
  7655. if (err) {
  7656. dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
  7657. err);
  7658. set_bit(__I40E_BAD_EEPROM, pf->state);
  7659. }
  7660. }
  7661. if (!err && test_bit(__I40E_BAD_EEPROM, pf->state)) {
  7662. dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
  7663. clear_bit(__I40E_BAD_EEPROM, pf->state);
  7664. }
  7665. }
  7666. /**
  7667. * i40e_enable_pf_switch_lb
  7668. * @pf: pointer to the PF structure
  7669. *
  7670. * enable switch loop back or die - no point in a return value
  7671. **/
  7672. static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
  7673. {
  7674. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  7675. struct i40e_vsi_context ctxt;
  7676. int ret;
  7677. ctxt.seid = pf->main_vsi_seid;
  7678. ctxt.pf_num = pf->hw.pf_id;
  7679. ctxt.vf_num = 0;
  7680. ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
  7681. if (ret) {
  7682. dev_info(&pf->pdev->dev,
  7683. "couldn't get PF vsi config, err %s aq_err %s\n",
  7684. i40e_stat_str(&pf->hw, ret),
  7685. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  7686. return;
  7687. }
  7688. ctxt.flags = I40E_AQ_VSI_TYPE_PF;
  7689. ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  7690. ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  7691. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  7692. if (ret) {
  7693. dev_info(&pf->pdev->dev,
  7694. "update vsi switch failed, err %s aq_err %s\n",
  7695. i40e_stat_str(&pf->hw, ret),
  7696. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  7697. }
  7698. }
  7699. /**
  7700. * i40e_disable_pf_switch_lb
  7701. * @pf: pointer to the PF structure
  7702. *
  7703. * disable switch loop back or die - no point in a return value
  7704. **/
  7705. static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
  7706. {
  7707. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  7708. struct i40e_vsi_context ctxt;
  7709. int ret;
  7710. ctxt.seid = pf->main_vsi_seid;
  7711. ctxt.pf_num = pf->hw.pf_id;
  7712. ctxt.vf_num = 0;
  7713. ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
  7714. if (ret) {
  7715. dev_info(&pf->pdev->dev,
  7716. "couldn't get PF vsi config, err %s aq_err %s\n",
  7717. i40e_stat_str(&pf->hw, ret),
  7718. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  7719. return;
  7720. }
  7721. ctxt.flags = I40E_AQ_VSI_TYPE_PF;
  7722. ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  7723. ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  7724. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  7725. if (ret) {
  7726. dev_info(&pf->pdev->dev,
  7727. "update vsi switch failed, err %s aq_err %s\n",
  7728. i40e_stat_str(&pf->hw, ret),
  7729. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  7730. }
  7731. }
  7732. /**
  7733. * i40e_config_bridge_mode - Configure the HW bridge mode
  7734. * @veb: pointer to the bridge instance
  7735. *
  7736. * Configure the loop back mode for the LAN VSI that is downlink to the
  7737. * specified HW bridge instance. It is expected this function is called
  7738. * when a new HW bridge is instantiated.
  7739. **/
  7740. static void i40e_config_bridge_mode(struct i40e_veb *veb)
  7741. {
  7742. struct i40e_pf *pf = veb->pf;
  7743. if (pf->hw.debug_mask & I40E_DEBUG_LAN)
  7744. dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
  7745. veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
  7746. if (veb->bridge_mode & BRIDGE_MODE_VEPA)
  7747. i40e_disable_pf_switch_lb(pf);
  7748. else
  7749. i40e_enable_pf_switch_lb(pf);
  7750. }
  7751. /**
  7752. * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
  7753. * @veb: pointer to the VEB instance
  7754. *
  7755. * This is a recursive function that first builds the attached VSIs then
  7756. * recurses in to build the next layer of VEB. We track the connections
  7757. * through our own index numbers because the seid's from the HW could
  7758. * change across the reset.
  7759. **/
  7760. static int i40e_reconstitute_veb(struct i40e_veb *veb)
  7761. {
  7762. struct i40e_vsi *ctl_vsi = NULL;
  7763. struct i40e_pf *pf = veb->pf;
  7764. int v, veb_idx;
  7765. int ret;
  7766. /* build VSI that owns this VEB, temporarily attached to base VEB */
  7767. for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
  7768. if (pf->vsi[v] &&
  7769. pf->vsi[v]->veb_idx == veb->idx &&
  7770. pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
  7771. ctl_vsi = pf->vsi[v];
  7772. break;
  7773. }
  7774. }
  7775. if (!ctl_vsi) {
  7776. dev_info(&pf->pdev->dev,
  7777. "missing owner VSI for veb_idx %d\n", veb->idx);
  7778. ret = -ENOENT;
  7779. goto end_reconstitute;
  7780. }
  7781. if (ctl_vsi != pf->vsi[pf->lan_vsi])
  7782. ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
  7783. ret = i40e_add_vsi(ctl_vsi);
  7784. if (ret) {
  7785. dev_info(&pf->pdev->dev,
  7786. "rebuild of veb_idx %d owner VSI failed: %d\n",
  7787. veb->idx, ret);
  7788. goto end_reconstitute;
  7789. }
  7790. i40e_vsi_reset_stats(ctl_vsi);
  7791. /* create the VEB in the switch and move the VSI onto the VEB */
  7792. ret = i40e_add_veb(veb, ctl_vsi);
  7793. if (ret)
  7794. goto end_reconstitute;
  7795. if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
  7796. veb->bridge_mode = BRIDGE_MODE_VEB;
  7797. else
  7798. veb->bridge_mode = BRIDGE_MODE_VEPA;
  7799. i40e_config_bridge_mode(veb);
  7800. /* create the remaining VSIs attached to this VEB */
  7801. for (v = 0; v < pf->num_alloc_vsi; v++) {
  7802. if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
  7803. continue;
  7804. if (pf->vsi[v]->veb_idx == veb->idx) {
  7805. struct i40e_vsi *vsi = pf->vsi[v];
  7806. vsi->uplink_seid = veb->seid;
  7807. ret = i40e_add_vsi(vsi);
  7808. if (ret) {
  7809. dev_info(&pf->pdev->dev,
  7810. "rebuild of vsi_idx %d failed: %d\n",
  7811. v, ret);
  7812. goto end_reconstitute;
  7813. }
  7814. i40e_vsi_reset_stats(vsi);
  7815. }
  7816. }
  7817. /* create any VEBs attached to this VEB - RECURSION */
  7818. for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
  7819. if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
  7820. pf->veb[veb_idx]->uplink_seid = veb->seid;
  7821. ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
  7822. if (ret)
  7823. break;
  7824. }
  7825. }
  7826. end_reconstitute:
  7827. return ret;
  7828. }
  7829. /**
  7830. * i40e_get_capabilities - get info about the HW
  7831. * @pf: the PF struct
  7832. **/
  7833. static int i40e_get_capabilities(struct i40e_pf *pf,
  7834. enum i40e_admin_queue_opc list_type)
  7835. {
  7836. struct i40e_aqc_list_capabilities_element_resp *cap_buf;
  7837. u16 data_size;
  7838. int buf_len;
  7839. int err;
  7840. buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
  7841. do {
  7842. cap_buf = kzalloc(buf_len, GFP_KERNEL);
  7843. if (!cap_buf)
  7844. return -ENOMEM;
  7845. /* this loads the data into the hw struct for us */
  7846. err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
  7847. &data_size, list_type,
  7848. NULL);
  7849. /* data loaded, buffer no longer needed */
  7850. kfree(cap_buf);
  7851. if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
  7852. /* retry with a larger buffer */
  7853. buf_len = data_size;
  7854. } else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK) {
  7855. dev_info(&pf->pdev->dev,
  7856. "capability discovery failed, err %s aq_err %s\n",
  7857. i40e_stat_str(&pf->hw, err),
  7858. i40e_aq_str(&pf->hw,
  7859. pf->hw.aq.asq_last_status));
  7860. return -ENODEV;
  7861. }
  7862. } while (err);
  7863. if (pf->hw.debug_mask & I40E_DEBUG_USER) {
  7864. if (list_type == i40e_aqc_opc_list_func_capabilities) {
  7865. dev_info(&pf->pdev->dev,
  7866. "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",
  7867. pf->hw.pf_id, pf->hw.func_caps.num_vfs,
  7868. pf->hw.func_caps.num_msix_vectors,
  7869. pf->hw.func_caps.num_msix_vectors_vf,
  7870. pf->hw.func_caps.fd_filters_guaranteed,
  7871. pf->hw.func_caps.fd_filters_best_effort,
  7872. pf->hw.func_caps.num_tx_qp,
  7873. pf->hw.func_caps.num_vsis);
  7874. } else if (list_type == i40e_aqc_opc_list_dev_capabilities) {
  7875. dev_info(&pf->pdev->dev,
  7876. "switch_mode=0x%04x, function_valid=0x%08x\n",
  7877. pf->hw.dev_caps.switch_mode,
  7878. pf->hw.dev_caps.valid_functions);
  7879. dev_info(&pf->pdev->dev,
  7880. "SR-IOV=%d, num_vfs for all function=%u\n",
  7881. pf->hw.dev_caps.sr_iov_1_1,
  7882. pf->hw.dev_caps.num_vfs);
  7883. dev_info(&pf->pdev->dev,
  7884. "num_vsis=%u, num_rx:%u, num_tx=%u\n",
  7885. pf->hw.dev_caps.num_vsis,
  7886. pf->hw.dev_caps.num_rx_qp,
  7887. pf->hw.dev_caps.num_tx_qp);
  7888. }
  7889. }
  7890. if (list_type == i40e_aqc_opc_list_func_capabilities) {
  7891. #define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
  7892. + pf->hw.func_caps.num_vfs)
  7893. if (pf->hw.revision_id == 0 &&
  7894. pf->hw.func_caps.num_vsis < DEF_NUM_VSI) {
  7895. dev_info(&pf->pdev->dev,
  7896. "got num_vsis %d, setting num_vsis to %d\n",
  7897. pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
  7898. pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
  7899. }
  7900. }
  7901. return 0;
  7902. }
  7903. static int i40e_vsi_clear(struct i40e_vsi *vsi);
  7904. /**
  7905. * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
  7906. * @pf: board private structure
  7907. **/
  7908. static void i40e_fdir_sb_setup(struct i40e_pf *pf)
  7909. {
  7910. struct i40e_vsi *vsi;
  7911. /* quick workaround for an NVM issue that leaves a critical register
  7912. * uninitialized
  7913. */
  7914. if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
  7915. static const u32 hkey[] = {
  7916. 0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
  7917. 0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
  7918. 0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
  7919. 0x95b3a76d};
  7920. int i;
  7921. for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
  7922. wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
  7923. }
  7924. if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
  7925. return;
  7926. /* find existing VSI and see if it needs configuring */
  7927. vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
  7928. /* create a new VSI if none exists */
  7929. if (!vsi) {
  7930. vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
  7931. pf->vsi[pf->lan_vsi]->seid, 0);
  7932. if (!vsi) {
  7933. dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
  7934. pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
  7935. pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
  7936. return;
  7937. }
  7938. }
  7939. i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
  7940. }
  7941. /**
  7942. * i40e_fdir_teardown - release the Flow Director resources
  7943. * @pf: board private structure
  7944. **/
  7945. static void i40e_fdir_teardown(struct i40e_pf *pf)
  7946. {
  7947. struct i40e_vsi *vsi;
  7948. i40e_fdir_filter_exit(pf);
  7949. vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
  7950. if (vsi)
  7951. i40e_vsi_release(vsi);
  7952. }
  7953. /**
  7954. * i40e_rebuild_cloud_filters - Rebuilds cloud filters for VSIs
  7955. * @vsi: PF main vsi
  7956. * @seid: seid of main or channel VSIs
  7957. *
  7958. * Rebuilds cloud filters associated with main VSI and channel VSIs if they
  7959. * existed before reset
  7960. **/
  7961. static int i40e_rebuild_cloud_filters(struct i40e_vsi *vsi, u16 seid)
  7962. {
  7963. struct i40e_cloud_filter *cfilter;
  7964. struct i40e_pf *pf = vsi->back;
  7965. struct hlist_node *node;
  7966. i40e_status ret;
  7967. /* Add cloud filters back if they exist */
  7968. hlist_for_each_entry_safe(cfilter, node, &pf->cloud_filter_list,
  7969. cloud_node) {
  7970. if (cfilter->seid != seid)
  7971. continue;
  7972. if (cfilter->dst_port)
  7973. ret = i40e_add_del_cloud_filter_big_buf(vsi, cfilter,
  7974. true);
  7975. else
  7976. ret = i40e_add_del_cloud_filter(vsi, cfilter, true);
  7977. if (ret) {
  7978. dev_dbg(&pf->pdev->dev,
  7979. "Failed to rebuild cloud filter, err %s aq_err %s\n",
  7980. i40e_stat_str(&pf->hw, ret),
  7981. i40e_aq_str(&pf->hw,
  7982. pf->hw.aq.asq_last_status));
  7983. return ret;
  7984. }
  7985. }
  7986. return 0;
  7987. }
  7988. /**
  7989. * i40e_rebuild_channels - Rebuilds channel VSIs if they existed before reset
  7990. * @vsi: PF main vsi
  7991. *
  7992. * Rebuilds channel VSIs if they existed before reset
  7993. **/
  7994. static int i40e_rebuild_channels(struct i40e_vsi *vsi)
  7995. {
  7996. struct i40e_channel *ch, *ch_tmp;
  7997. i40e_status ret;
  7998. if (list_empty(&vsi->ch_list))
  7999. return 0;
  8000. list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
  8001. if (!ch->initialized)
  8002. break;
  8003. /* Proceed with creation of channel (VMDq2) VSI */
  8004. ret = i40e_add_channel(vsi->back, vsi->uplink_seid, ch);
  8005. if (ret) {
  8006. dev_info(&vsi->back->pdev->dev,
  8007. "failed to rebuild channels using uplink_seid %u\n",
  8008. vsi->uplink_seid);
  8009. return ret;
  8010. }
  8011. /* Reconfigure TX queues using QTX_CTL register */
  8012. ret = i40e_channel_config_tx_ring(vsi->back, vsi, ch);
  8013. if (ret) {
  8014. dev_info(&vsi->back->pdev->dev,
  8015. "failed to configure TX rings for channel %u\n",
  8016. ch->seid);
  8017. return ret;
  8018. }
  8019. /* update 'next_base_queue' */
  8020. vsi->next_base_queue = vsi->next_base_queue +
  8021. ch->num_queue_pairs;
  8022. if (ch->max_tx_rate) {
  8023. u64 credits = ch->max_tx_rate;
  8024. if (i40e_set_bw_limit(vsi, ch->seid,
  8025. ch->max_tx_rate))
  8026. return -EINVAL;
  8027. do_div(credits, I40E_BW_CREDIT_DIVISOR);
  8028. dev_dbg(&vsi->back->pdev->dev,
  8029. "Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
  8030. ch->max_tx_rate,
  8031. credits,
  8032. ch->seid);
  8033. }
  8034. ret = i40e_rebuild_cloud_filters(vsi, ch->seid);
  8035. if (ret) {
  8036. dev_dbg(&vsi->back->pdev->dev,
  8037. "Failed to rebuild cloud filters for channel VSI %u\n",
  8038. ch->seid);
  8039. return ret;
  8040. }
  8041. }
  8042. return 0;
  8043. }
  8044. /**
  8045. * i40e_prep_for_reset - prep for the core to reset
  8046. * @pf: board private structure
  8047. * @lock_acquired: indicates whether or not the lock has been acquired
  8048. * before this function was called.
  8049. *
  8050. * Close up the VFs and other things in prep for PF Reset.
  8051. **/
  8052. static void i40e_prep_for_reset(struct i40e_pf *pf, bool lock_acquired)
  8053. {
  8054. struct i40e_hw *hw = &pf->hw;
  8055. i40e_status ret = 0;
  8056. u32 v;
  8057. clear_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
  8058. if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
  8059. return;
  8060. if (i40e_check_asq_alive(&pf->hw))
  8061. i40e_vc_notify_reset(pf);
  8062. dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
  8063. /* quiesce the VSIs and their queues that are not already DOWN */
  8064. /* pf_quiesce_all_vsi modifies netdev structures -rtnl_lock needed */
  8065. if (!lock_acquired)
  8066. rtnl_lock();
  8067. i40e_pf_quiesce_all_vsi(pf);
  8068. if (!lock_acquired)
  8069. rtnl_unlock();
  8070. for (v = 0; v < pf->num_alloc_vsi; v++) {
  8071. if (pf->vsi[v])
  8072. pf->vsi[v]->seid = 0;
  8073. }
  8074. i40e_shutdown_adminq(&pf->hw);
  8075. /* call shutdown HMC */
  8076. if (hw->hmc.hmc_obj) {
  8077. ret = i40e_shutdown_lan_hmc(hw);
  8078. if (ret)
  8079. dev_warn(&pf->pdev->dev,
  8080. "shutdown_lan_hmc failed: %d\n", ret);
  8081. }
  8082. }
  8083. /**
  8084. * i40e_send_version - update firmware with driver version
  8085. * @pf: PF struct
  8086. */
  8087. static void i40e_send_version(struct i40e_pf *pf)
  8088. {
  8089. struct i40e_driver_version dv;
  8090. dv.major_version = DRV_VERSION_MAJOR;
  8091. dv.minor_version = DRV_VERSION_MINOR;
  8092. dv.build_version = DRV_VERSION_BUILD;
  8093. dv.subbuild_version = 0;
  8094. strlcpy(dv.driver_string, DRV_VERSION, sizeof(dv.driver_string));
  8095. i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
  8096. }
  8097. /**
  8098. * i40e_get_oem_version - get OEM specific version information
  8099. * @hw: pointer to the hardware structure
  8100. **/
  8101. static void i40e_get_oem_version(struct i40e_hw *hw)
  8102. {
  8103. u16 block_offset = 0xffff;
  8104. u16 block_length = 0;
  8105. u16 capabilities = 0;
  8106. u16 gen_snap = 0;
  8107. u16 release = 0;
  8108. #define I40E_SR_NVM_OEM_VERSION_PTR 0x1B
  8109. #define I40E_NVM_OEM_LENGTH_OFFSET 0x00
  8110. #define I40E_NVM_OEM_CAPABILITIES_OFFSET 0x01
  8111. #define I40E_NVM_OEM_GEN_OFFSET 0x02
  8112. #define I40E_NVM_OEM_RELEASE_OFFSET 0x03
  8113. #define I40E_NVM_OEM_CAPABILITIES_MASK 0x000F
  8114. #define I40E_NVM_OEM_LENGTH 3
  8115. /* Check if pointer to OEM version block is valid. */
  8116. i40e_read_nvm_word(hw, I40E_SR_NVM_OEM_VERSION_PTR, &block_offset);
  8117. if (block_offset == 0xffff)
  8118. return;
  8119. /* Check if OEM version block has correct length. */
  8120. i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_LENGTH_OFFSET,
  8121. &block_length);
  8122. if (block_length < I40E_NVM_OEM_LENGTH)
  8123. return;
  8124. /* Check if OEM version format is as expected. */
  8125. i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_CAPABILITIES_OFFSET,
  8126. &capabilities);
  8127. if ((capabilities & I40E_NVM_OEM_CAPABILITIES_MASK) != 0)
  8128. return;
  8129. i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_GEN_OFFSET,
  8130. &gen_snap);
  8131. i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_RELEASE_OFFSET,
  8132. &release);
  8133. hw->nvm.oem_ver = (gen_snap << I40E_OEM_SNAP_SHIFT) | release;
  8134. hw->nvm.eetrack = I40E_OEM_EETRACK_ID;
  8135. }
  8136. /**
  8137. * i40e_reset - wait for core reset to finish reset, reset pf if corer not seen
  8138. * @pf: board private structure
  8139. **/
  8140. static int i40e_reset(struct i40e_pf *pf)
  8141. {
  8142. struct i40e_hw *hw = &pf->hw;
  8143. i40e_status ret;
  8144. ret = i40e_pf_reset(hw);
  8145. if (ret) {
  8146. dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
  8147. set_bit(__I40E_RESET_FAILED, pf->state);
  8148. clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
  8149. } else {
  8150. pf->pfr_count++;
  8151. }
  8152. return ret;
  8153. }
  8154. /**
  8155. * i40e_rebuild - rebuild using a saved config
  8156. * @pf: board private structure
  8157. * @reinit: if the Main VSI needs to re-initialized.
  8158. * @lock_acquired: indicates whether or not the lock has been acquired
  8159. * before this function was called.
  8160. **/
  8161. static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired)
  8162. {
  8163. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  8164. struct i40e_hw *hw = &pf->hw;
  8165. u8 set_fc_aq_fail = 0;
  8166. i40e_status ret;
  8167. u32 val;
  8168. int v;
  8169. if (test_bit(__I40E_DOWN, pf->state))
  8170. goto clear_recovery;
  8171. dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
  8172. /* rebuild the basics for the AdminQ, HMC, and initial HW switch */
  8173. ret = i40e_init_adminq(&pf->hw);
  8174. if (ret) {
  8175. dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %s aq_err %s\n",
  8176. i40e_stat_str(&pf->hw, ret),
  8177. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  8178. goto clear_recovery;
  8179. }
  8180. i40e_get_oem_version(&pf->hw);
  8181. if (test_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state) &&
  8182. ((hw->aq.fw_maj_ver == 4 && hw->aq.fw_min_ver <= 33) ||
  8183. hw->aq.fw_maj_ver < 4) && hw->mac.type == I40E_MAC_XL710) {
  8184. /* The following delay is necessary for 4.33 firmware and older
  8185. * to recover after EMP reset. 200 ms should suffice but we
  8186. * put here 300 ms to be sure that FW is ready to operate
  8187. * after reset.
  8188. */
  8189. mdelay(300);
  8190. }
  8191. /* re-verify the eeprom if we just had an EMP reset */
  8192. if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state))
  8193. i40e_verify_eeprom(pf);
  8194. i40e_clear_pxe_mode(hw);
  8195. ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
  8196. if (ret)
  8197. goto end_core_reset;
  8198. ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
  8199. hw->func_caps.num_rx_qp, 0, 0);
  8200. if (ret) {
  8201. dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
  8202. goto end_core_reset;
  8203. }
  8204. ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
  8205. if (ret) {
  8206. dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
  8207. goto end_core_reset;
  8208. }
  8209. /* Enable FW to write a default DCB config on link-up */
  8210. i40e_aq_set_dcb_parameters(hw, true, NULL);
  8211. #ifdef CONFIG_I40E_DCB
  8212. ret = i40e_init_pf_dcb(pf);
  8213. if (ret) {
  8214. dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n", ret);
  8215. pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
  8216. /* Continue without DCB enabled */
  8217. }
  8218. #endif /* CONFIG_I40E_DCB */
  8219. /* do basic switch setup */
  8220. if (!lock_acquired)
  8221. rtnl_lock();
  8222. ret = i40e_setup_pf_switch(pf, reinit);
  8223. if (ret)
  8224. goto end_unlock;
  8225. /* The driver only wants link up/down and module qualification
  8226. * reports from firmware. Note the negative logic.
  8227. */
  8228. ret = i40e_aq_set_phy_int_mask(&pf->hw,
  8229. ~(I40E_AQ_EVENT_LINK_UPDOWN |
  8230. I40E_AQ_EVENT_MEDIA_NA |
  8231. I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
  8232. if (ret)
  8233. dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
  8234. i40e_stat_str(&pf->hw, ret),
  8235. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  8236. /* make sure our flow control settings are restored */
  8237. ret = i40e_set_fc(&pf->hw, &set_fc_aq_fail, true);
  8238. if (ret)
  8239. dev_dbg(&pf->pdev->dev, "setting flow control: ret = %s last_status = %s\n",
  8240. i40e_stat_str(&pf->hw, ret),
  8241. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  8242. /* Rebuild the VSIs and VEBs that existed before reset.
  8243. * They are still in our local switch element arrays, so only
  8244. * need to rebuild the switch model in the HW.
  8245. *
  8246. * If there were VEBs but the reconstitution failed, we'll try
  8247. * try to recover minimal use by getting the basic PF VSI working.
  8248. */
  8249. if (vsi->uplink_seid != pf->mac_seid) {
  8250. dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
  8251. /* find the one VEB connected to the MAC, and find orphans */
  8252. for (v = 0; v < I40E_MAX_VEB; v++) {
  8253. if (!pf->veb[v])
  8254. continue;
  8255. if (pf->veb[v]->uplink_seid == pf->mac_seid ||
  8256. pf->veb[v]->uplink_seid == 0) {
  8257. ret = i40e_reconstitute_veb(pf->veb[v]);
  8258. if (!ret)
  8259. continue;
  8260. /* If Main VEB failed, we're in deep doodoo,
  8261. * so give up rebuilding the switch and set up
  8262. * for minimal rebuild of PF VSI.
  8263. * If orphan failed, we'll report the error
  8264. * but try to keep going.
  8265. */
  8266. if (pf->veb[v]->uplink_seid == pf->mac_seid) {
  8267. dev_info(&pf->pdev->dev,
  8268. "rebuild of switch failed: %d, will try to set up simple PF connection\n",
  8269. ret);
  8270. vsi->uplink_seid = pf->mac_seid;
  8271. break;
  8272. } else if (pf->veb[v]->uplink_seid == 0) {
  8273. dev_info(&pf->pdev->dev,
  8274. "rebuild of orphan VEB failed: %d\n",
  8275. ret);
  8276. }
  8277. }
  8278. }
  8279. }
  8280. if (vsi->uplink_seid == pf->mac_seid) {
  8281. dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
  8282. /* no VEB, so rebuild only the Main VSI */
  8283. ret = i40e_add_vsi(vsi);
  8284. if (ret) {
  8285. dev_info(&pf->pdev->dev,
  8286. "rebuild of Main VSI failed: %d\n", ret);
  8287. goto end_unlock;
  8288. }
  8289. }
  8290. if (vsi->mqprio_qopt.max_rate[0]) {
  8291. u64 max_tx_rate = vsi->mqprio_qopt.max_rate[0];
  8292. u64 credits = 0;
  8293. do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
  8294. ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
  8295. if (ret)
  8296. goto end_unlock;
  8297. credits = max_tx_rate;
  8298. do_div(credits, I40E_BW_CREDIT_DIVISOR);
  8299. dev_dbg(&vsi->back->pdev->dev,
  8300. "Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
  8301. max_tx_rate,
  8302. credits,
  8303. vsi->seid);
  8304. }
  8305. ret = i40e_rebuild_cloud_filters(vsi, vsi->seid);
  8306. if (ret)
  8307. goto end_unlock;
  8308. /* PF Main VSI is rebuild by now, go ahead and rebuild channel VSIs
  8309. * for this main VSI if they exist
  8310. */
  8311. ret = i40e_rebuild_channels(vsi);
  8312. if (ret)
  8313. goto end_unlock;
  8314. /* Reconfigure hardware for allowing smaller MSS in the case
  8315. * of TSO, so that we avoid the MDD being fired and causing
  8316. * a reset in the case of small MSS+TSO.
  8317. */
  8318. #define I40E_REG_MSS 0x000E64DC
  8319. #define I40E_REG_MSS_MIN_MASK 0x3FF0000
  8320. #define I40E_64BYTE_MSS 0x400000
  8321. val = rd32(hw, I40E_REG_MSS);
  8322. if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
  8323. val &= ~I40E_REG_MSS_MIN_MASK;
  8324. val |= I40E_64BYTE_MSS;
  8325. wr32(hw, I40E_REG_MSS, val);
  8326. }
  8327. if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
  8328. msleep(75);
  8329. ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
  8330. if (ret)
  8331. dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
  8332. i40e_stat_str(&pf->hw, ret),
  8333. i40e_aq_str(&pf->hw,
  8334. pf->hw.aq.asq_last_status));
  8335. }
  8336. /* reinit the misc interrupt */
  8337. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  8338. ret = i40e_setup_misc_vector(pf);
  8339. /* Add a filter to drop all Flow control frames from any VSI from being
  8340. * transmitted. By doing so we stop a malicious VF from sending out
  8341. * PAUSE or PFC frames and potentially controlling traffic for other
  8342. * PF/VF VSIs.
  8343. * The FW can still send Flow control frames if enabled.
  8344. */
  8345. i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
  8346. pf->main_vsi_seid);
  8347. /* restart the VSIs that were rebuilt and running before the reset */
  8348. i40e_pf_unquiesce_all_vsi(pf);
  8349. /* Release the RTNL lock before we start resetting VFs */
  8350. if (!lock_acquired)
  8351. rtnl_unlock();
  8352. /* Restore promiscuous settings */
  8353. ret = i40e_set_promiscuous(pf, pf->cur_promisc);
  8354. if (ret)
  8355. dev_warn(&pf->pdev->dev,
  8356. "Failed to restore promiscuous setting: %s, err %s aq_err %s\n",
  8357. pf->cur_promisc ? "on" : "off",
  8358. i40e_stat_str(&pf->hw, ret),
  8359. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  8360. i40e_reset_all_vfs(pf, true);
  8361. /* tell the firmware that we're starting */
  8362. i40e_send_version(pf);
  8363. /* We've already released the lock, so don't do it again */
  8364. goto end_core_reset;
  8365. end_unlock:
  8366. if (!lock_acquired)
  8367. rtnl_unlock();
  8368. end_core_reset:
  8369. clear_bit(__I40E_RESET_FAILED, pf->state);
  8370. clear_recovery:
  8371. clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
  8372. }
  8373. /**
  8374. * i40e_reset_and_rebuild - reset and rebuild using a saved config
  8375. * @pf: board private structure
  8376. * @reinit: if the Main VSI needs to re-initialized.
  8377. * @lock_acquired: indicates whether or not the lock has been acquired
  8378. * before this function was called.
  8379. **/
  8380. static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
  8381. bool lock_acquired)
  8382. {
  8383. int ret;
  8384. /* Now we wait for GRST to settle out.
  8385. * We don't have to delete the VEBs or VSIs from the hw switch
  8386. * because the reset will make them disappear.
  8387. */
  8388. ret = i40e_reset(pf);
  8389. if (!ret)
  8390. i40e_rebuild(pf, reinit, lock_acquired);
  8391. }
  8392. /**
  8393. * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
  8394. * @pf: board private structure
  8395. *
  8396. * Close up the VFs and other things in prep for a Core Reset,
  8397. * then get ready to rebuild the world.
  8398. * @lock_acquired: indicates whether or not the lock has been acquired
  8399. * before this function was called.
  8400. **/
  8401. static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired)
  8402. {
  8403. i40e_prep_for_reset(pf, lock_acquired);
  8404. i40e_reset_and_rebuild(pf, false, lock_acquired);
  8405. }
  8406. /**
  8407. * i40e_handle_mdd_event
  8408. * @pf: pointer to the PF structure
  8409. *
  8410. * Called from the MDD irq handler to identify possibly malicious vfs
  8411. **/
  8412. static void i40e_handle_mdd_event(struct i40e_pf *pf)
  8413. {
  8414. struct i40e_hw *hw = &pf->hw;
  8415. bool mdd_detected = false;
  8416. bool pf_mdd_detected = false;
  8417. struct i40e_vf *vf;
  8418. u32 reg;
  8419. int i;
  8420. if (!test_bit(__I40E_MDD_EVENT_PENDING, pf->state))
  8421. return;
  8422. /* find what triggered the MDD event */
  8423. reg = rd32(hw, I40E_GL_MDET_TX);
  8424. if (reg & I40E_GL_MDET_TX_VALID_MASK) {
  8425. u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
  8426. I40E_GL_MDET_TX_PF_NUM_SHIFT;
  8427. u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
  8428. I40E_GL_MDET_TX_VF_NUM_SHIFT;
  8429. u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
  8430. I40E_GL_MDET_TX_EVENT_SHIFT;
  8431. u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
  8432. I40E_GL_MDET_TX_QUEUE_SHIFT) -
  8433. pf->hw.func_caps.base_queue;
  8434. if (netif_msg_tx_err(pf))
  8435. dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on TX queue %d PF number 0x%02x VF number 0x%02x\n",
  8436. event, queue, pf_num, vf_num);
  8437. wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
  8438. mdd_detected = true;
  8439. }
  8440. reg = rd32(hw, I40E_GL_MDET_RX);
  8441. if (reg & I40E_GL_MDET_RX_VALID_MASK) {
  8442. u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
  8443. I40E_GL_MDET_RX_FUNCTION_SHIFT;
  8444. u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
  8445. I40E_GL_MDET_RX_EVENT_SHIFT;
  8446. u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
  8447. I40E_GL_MDET_RX_QUEUE_SHIFT) -
  8448. pf->hw.func_caps.base_queue;
  8449. if (netif_msg_rx_err(pf))
  8450. dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
  8451. event, queue, func);
  8452. wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
  8453. mdd_detected = true;
  8454. }
  8455. if (mdd_detected) {
  8456. reg = rd32(hw, I40E_PF_MDET_TX);
  8457. if (reg & I40E_PF_MDET_TX_VALID_MASK) {
  8458. wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
  8459. dev_info(&pf->pdev->dev, "TX driver issue detected, PF reset issued\n");
  8460. pf_mdd_detected = true;
  8461. }
  8462. reg = rd32(hw, I40E_PF_MDET_RX);
  8463. if (reg & I40E_PF_MDET_RX_VALID_MASK) {
  8464. wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
  8465. dev_info(&pf->pdev->dev, "RX driver issue detected, PF reset issued\n");
  8466. pf_mdd_detected = true;
  8467. }
  8468. /* Queue belongs to the PF, initiate a reset */
  8469. if (pf_mdd_detected) {
  8470. set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
  8471. i40e_service_event_schedule(pf);
  8472. }
  8473. }
  8474. /* see if one of the VFs needs its hand slapped */
  8475. for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
  8476. vf = &(pf->vf[i]);
  8477. reg = rd32(hw, I40E_VP_MDET_TX(i));
  8478. if (reg & I40E_VP_MDET_TX_VALID_MASK) {
  8479. wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
  8480. vf->num_mdd_events++;
  8481. dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
  8482. i);
  8483. }
  8484. reg = rd32(hw, I40E_VP_MDET_RX(i));
  8485. if (reg & I40E_VP_MDET_RX_VALID_MASK) {
  8486. wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
  8487. vf->num_mdd_events++;
  8488. dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
  8489. i);
  8490. }
  8491. if (vf->num_mdd_events > I40E_DEFAULT_NUM_MDD_EVENTS_ALLOWED) {
  8492. dev_info(&pf->pdev->dev,
  8493. "Too many MDD events on VF %d, disabled\n", i);
  8494. dev_info(&pf->pdev->dev,
  8495. "Use PF Control I/F to re-enable the VF\n");
  8496. set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
  8497. }
  8498. }
  8499. /* re-enable mdd interrupt cause */
  8500. clear_bit(__I40E_MDD_EVENT_PENDING, pf->state);
  8501. reg = rd32(hw, I40E_PFINT_ICR0_ENA);
  8502. reg |= I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
  8503. wr32(hw, I40E_PFINT_ICR0_ENA, reg);
  8504. i40e_flush(hw);
  8505. }
  8506. static const char *i40e_tunnel_name(u8 type)
  8507. {
  8508. switch (type) {
  8509. case UDP_TUNNEL_TYPE_VXLAN:
  8510. return "vxlan";
  8511. case UDP_TUNNEL_TYPE_GENEVE:
  8512. return "geneve";
  8513. default:
  8514. return "unknown";
  8515. }
  8516. }
  8517. /**
  8518. * i40e_sync_udp_filters - Trigger a sync event for existing UDP filters
  8519. * @pf: board private structure
  8520. **/
  8521. static void i40e_sync_udp_filters(struct i40e_pf *pf)
  8522. {
  8523. int i;
  8524. /* loop through and set pending bit for all active UDP filters */
  8525. for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
  8526. if (pf->udp_ports[i].port)
  8527. pf->pending_udp_bitmap |= BIT_ULL(i);
  8528. }
  8529. set_bit(__I40E_UDP_FILTER_SYNC_PENDING, pf->state);
  8530. }
  8531. /**
  8532. * i40e_sync_udp_filters_subtask - Sync the VSI filter list with HW
  8533. * @pf: board private structure
  8534. **/
  8535. static void i40e_sync_udp_filters_subtask(struct i40e_pf *pf)
  8536. {
  8537. struct i40e_hw *hw = &pf->hw;
  8538. u8 filter_index, type;
  8539. u16 port;
  8540. int i;
  8541. if (!test_and_clear_bit(__I40E_UDP_FILTER_SYNC_PENDING, pf->state))
  8542. return;
  8543. /* acquire RTNL to maintain state of flags and port requests */
  8544. rtnl_lock();
  8545. for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
  8546. if (pf->pending_udp_bitmap & BIT_ULL(i)) {
  8547. struct i40e_udp_port_config *udp_port;
  8548. i40e_status ret = 0;
  8549. udp_port = &pf->udp_ports[i];
  8550. pf->pending_udp_bitmap &= ~BIT_ULL(i);
  8551. port = READ_ONCE(udp_port->port);
  8552. type = READ_ONCE(udp_port->type);
  8553. filter_index = READ_ONCE(udp_port->filter_index);
  8554. /* release RTNL while we wait on AQ command */
  8555. rtnl_unlock();
  8556. if (port)
  8557. ret = i40e_aq_add_udp_tunnel(hw, port,
  8558. type,
  8559. &filter_index,
  8560. NULL);
  8561. else if (filter_index != I40E_UDP_PORT_INDEX_UNUSED)
  8562. ret = i40e_aq_del_udp_tunnel(hw, filter_index,
  8563. NULL);
  8564. /* reacquire RTNL so we can update filter_index */
  8565. rtnl_lock();
  8566. if (ret) {
  8567. dev_info(&pf->pdev->dev,
  8568. "%s %s port %d, index %d failed, err %s aq_err %s\n",
  8569. i40e_tunnel_name(type),
  8570. port ? "add" : "delete",
  8571. port,
  8572. filter_index,
  8573. i40e_stat_str(&pf->hw, ret),
  8574. i40e_aq_str(&pf->hw,
  8575. pf->hw.aq.asq_last_status));
  8576. if (port) {
  8577. /* failed to add, just reset port,
  8578. * drop pending bit for any deletion
  8579. */
  8580. udp_port->port = 0;
  8581. pf->pending_udp_bitmap &= ~BIT_ULL(i);
  8582. }
  8583. } else if (port) {
  8584. /* record filter index on success */
  8585. udp_port->filter_index = filter_index;
  8586. }
  8587. }
  8588. }
  8589. rtnl_unlock();
  8590. }
  8591. /**
  8592. * i40e_service_task - Run the driver's async subtasks
  8593. * @work: pointer to work_struct containing our data
  8594. **/
  8595. static void i40e_service_task(struct work_struct *work)
  8596. {
  8597. struct i40e_pf *pf = container_of(work,
  8598. struct i40e_pf,
  8599. service_task);
  8600. unsigned long start_time = jiffies;
  8601. /* don't bother with service tasks if a reset is in progress */
  8602. if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
  8603. return;
  8604. if (test_and_set_bit(__I40E_SERVICE_SCHED, pf->state))
  8605. return;
  8606. i40e_detect_recover_hung(pf->vsi[pf->lan_vsi]);
  8607. i40e_sync_filters_subtask(pf);
  8608. i40e_reset_subtask(pf);
  8609. i40e_handle_mdd_event(pf);
  8610. i40e_vc_process_vflr_event(pf);
  8611. i40e_watchdog_subtask(pf);
  8612. i40e_fdir_reinit_subtask(pf);
  8613. if (test_and_clear_bit(__I40E_CLIENT_RESET, pf->state)) {
  8614. /* Client subtask will reopen next time through. */
  8615. i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], true);
  8616. } else {
  8617. i40e_client_subtask(pf);
  8618. if (test_and_clear_bit(__I40E_CLIENT_L2_CHANGE,
  8619. pf->state))
  8620. i40e_notify_client_of_l2_param_changes(
  8621. pf->vsi[pf->lan_vsi]);
  8622. }
  8623. i40e_sync_filters_subtask(pf);
  8624. i40e_sync_udp_filters_subtask(pf);
  8625. i40e_clean_adminq_subtask(pf);
  8626. /* flush memory to make sure state is correct before next watchdog */
  8627. smp_mb__before_atomic();
  8628. clear_bit(__I40E_SERVICE_SCHED, pf->state);
  8629. /* If the tasks have taken longer than one timer cycle or there
  8630. * is more work to be done, reschedule the service task now
  8631. * rather than wait for the timer to tick again.
  8632. */
  8633. if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
  8634. test_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state) ||
  8635. test_bit(__I40E_MDD_EVENT_PENDING, pf->state) ||
  8636. test_bit(__I40E_VFLR_EVENT_PENDING, pf->state))
  8637. i40e_service_event_schedule(pf);
  8638. }
  8639. /**
  8640. * i40e_service_timer - timer callback
  8641. * @data: pointer to PF struct
  8642. **/
  8643. static void i40e_service_timer(struct timer_list *t)
  8644. {
  8645. struct i40e_pf *pf = from_timer(pf, t, service_timer);
  8646. mod_timer(&pf->service_timer,
  8647. round_jiffies(jiffies + pf->service_timer_period));
  8648. i40e_service_event_schedule(pf);
  8649. }
  8650. /**
  8651. * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
  8652. * @vsi: the VSI being configured
  8653. **/
  8654. static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
  8655. {
  8656. struct i40e_pf *pf = vsi->back;
  8657. switch (vsi->type) {
  8658. case I40E_VSI_MAIN:
  8659. vsi->alloc_queue_pairs = pf->num_lan_qps;
  8660. vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
  8661. I40E_REQ_DESCRIPTOR_MULTIPLE);
  8662. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  8663. vsi->num_q_vectors = pf->num_lan_msix;
  8664. else
  8665. vsi->num_q_vectors = 1;
  8666. break;
  8667. case I40E_VSI_FDIR:
  8668. vsi->alloc_queue_pairs = 1;
  8669. vsi->num_desc = ALIGN(I40E_FDIR_RING_COUNT,
  8670. I40E_REQ_DESCRIPTOR_MULTIPLE);
  8671. vsi->num_q_vectors = pf->num_fdsb_msix;
  8672. break;
  8673. case I40E_VSI_VMDQ2:
  8674. vsi->alloc_queue_pairs = pf->num_vmdq_qps;
  8675. vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
  8676. I40E_REQ_DESCRIPTOR_MULTIPLE);
  8677. vsi->num_q_vectors = pf->num_vmdq_msix;
  8678. break;
  8679. case I40E_VSI_SRIOV:
  8680. vsi->alloc_queue_pairs = pf->num_vf_qps;
  8681. vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
  8682. I40E_REQ_DESCRIPTOR_MULTIPLE);
  8683. break;
  8684. default:
  8685. WARN_ON(1);
  8686. return -ENODATA;
  8687. }
  8688. return 0;
  8689. }
  8690. /**
  8691. * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
  8692. * @vsi: VSI pointer
  8693. * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
  8694. *
  8695. * On error: returns error code (negative)
  8696. * On success: returns 0
  8697. **/
  8698. static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
  8699. {
  8700. struct i40e_ring **next_rings;
  8701. int size;
  8702. int ret = 0;
  8703. /* allocate memory for both Tx, XDP Tx and Rx ring pointers */
  8704. size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs *
  8705. (i40e_enabled_xdp_vsi(vsi) ? 3 : 2);
  8706. vsi->tx_rings = kzalloc(size, GFP_KERNEL);
  8707. if (!vsi->tx_rings)
  8708. return -ENOMEM;
  8709. next_rings = vsi->tx_rings + vsi->alloc_queue_pairs;
  8710. if (i40e_enabled_xdp_vsi(vsi)) {
  8711. vsi->xdp_rings = next_rings;
  8712. next_rings += vsi->alloc_queue_pairs;
  8713. }
  8714. vsi->rx_rings = next_rings;
  8715. if (alloc_qvectors) {
  8716. /* allocate memory for q_vector pointers */
  8717. size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
  8718. vsi->q_vectors = kzalloc(size, GFP_KERNEL);
  8719. if (!vsi->q_vectors) {
  8720. ret = -ENOMEM;
  8721. goto err_vectors;
  8722. }
  8723. }
  8724. return ret;
  8725. err_vectors:
  8726. kfree(vsi->tx_rings);
  8727. return ret;
  8728. }
  8729. /**
  8730. * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
  8731. * @pf: board private structure
  8732. * @type: type of VSI
  8733. *
  8734. * On error: returns error code (negative)
  8735. * On success: returns vsi index in PF (positive)
  8736. **/
  8737. static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
  8738. {
  8739. int ret = -ENODEV;
  8740. struct i40e_vsi *vsi;
  8741. int vsi_idx;
  8742. int i;
  8743. /* Need to protect the allocation of the VSIs at the PF level */
  8744. mutex_lock(&pf->switch_mutex);
  8745. /* VSI list may be fragmented if VSI creation/destruction has
  8746. * been happening. We can afford to do a quick scan to look
  8747. * for any free VSIs in the list.
  8748. *
  8749. * find next empty vsi slot, looping back around if necessary
  8750. */
  8751. i = pf->next_vsi;
  8752. while (i < pf->num_alloc_vsi && pf->vsi[i])
  8753. i++;
  8754. if (i >= pf->num_alloc_vsi) {
  8755. i = 0;
  8756. while (i < pf->next_vsi && pf->vsi[i])
  8757. i++;
  8758. }
  8759. if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
  8760. vsi_idx = i; /* Found one! */
  8761. } else {
  8762. ret = -ENODEV;
  8763. goto unlock_pf; /* out of VSI slots! */
  8764. }
  8765. pf->next_vsi = ++i;
  8766. vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
  8767. if (!vsi) {
  8768. ret = -ENOMEM;
  8769. goto unlock_pf;
  8770. }
  8771. vsi->type = type;
  8772. vsi->back = pf;
  8773. set_bit(__I40E_VSI_DOWN, vsi->state);
  8774. vsi->flags = 0;
  8775. vsi->idx = vsi_idx;
  8776. vsi->int_rate_limit = 0;
  8777. vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
  8778. pf->rss_table_size : 64;
  8779. vsi->netdev_registered = false;
  8780. vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
  8781. hash_init(vsi->mac_filter_hash);
  8782. vsi->irqs_ready = false;
  8783. ret = i40e_set_num_rings_in_vsi(vsi);
  8784. if (ret)
  8785. goto err_rings;
  8786. ret = i40e_vsi_alloc_arrays(vsi, true);
  8787. if (ret)
  8788. goto err_rings;
  8789. /* Setup default MSIX irq handler for VSI */
  8790. i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
  8791. /* Initialize VSI lock */
  8792. spin_lock_init(&vsi->mac_filter_hash_lock);
  8793. pf->vsi[vsi_idx] = vsi;
  8794. ret = vsi_idx;
  8795. goto unlock_pf;
  8796. err_rings:
  8797. pf->next_vsi = i - 1;
  8798. kfree(vsi);
  8799. unlock_pf:
  8800. mutex_unlock(&pf->switch_mutex);
  8801. return ret;
  8802. }
  8803. /**
  8804. * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
  8805. * @vsi: VSI pointer
  8806. * @free_qvectors: a bool to specify if q_vectors need to be freed.
  8807. *
  8808. * On error: returns error code (negative)
  8809. * On success: returns 0
  8810. **/
  8811. static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
  8812. {
  8813. /* free the ring and vector containers */
  8814. if (free_qvectors) {
  8815. kfree(vsi->q_vectors);
  8816. vsi->q_vectors = NULL;
  8817. }
  8818. kfree(vsi->tx_rings);
  8819. vsi->tx_rings = NULL;
  8820. vsi->rx_rings = NULL;
  8821. vsi->xdp_rings = NULL;
  8822. }
  8823. /**
  8824. * i40e_clear_rss_config_user - clear the user configured RSS hash keys
  8825. * and lookup table
  8826. * @vsi: Pointer to VSI structure
  8827. */
  8828. static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
  8829. {
  8830. if (!vsi)
  8831. return;
  8832. kfree(vsi->rss_hkey_user);
  8833. vsi->rss_hkey_user = NULL;
  8834. kfree(vsi->rss_lut_user);
  8835. vsi->rss_lut_user = NULL;
  8836. }
  8837. /**
  8838. * i40e_vsi_clear - Deallocate the VSI provided
  8839. * @vsi: the VSI being un-configured
  8840. **/
  8841. static int i40e_vsi_clear(struct i40e_vsi *vsi)
  8842. {
  8843. struct i40e_pf *pf;
  8844. if (!vsi)
  8845. return 0;
  8846. if (!vsi->back)
  8847. goto free_vsi;
  8848. pf = vsi->back;
  8849. mutex_lock(&pf->switch_mutex);
  8850. if (!pf->vsi[vsi->idx]) {
  8851. dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](type %d)\n",
  8852. vsi->idx, vsi->idx, vsi->type);
  8853. goto unlock_vsi;
  8854. }
  8855. if (pf->vsi[vsi->idx] != vsi) {
  8856. dev_err(&pf->pdev->dev,
  8857. "pf->vsi[%d](type %d) != vsi[%d](type %d): no free!\n",
  8858. pf->vsi[vsi->idx]->idx,
  8859. pf->vsi[vsi->idx]->type,
  8860. vsi->idx, vsi->type);
  8861. goto unlock_vsi;
  8862. }
  8863. /* updates the PF for this cleared vsi */
  8864. i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
  8865. i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
  8866. i40e_vsi_free_arrays(vsi, true);
  8867. i40e_clear_rss_config_user(vsi);
  8868. pf->vsi[vsi->idx] = NULL;
  8869. if (vsi->idx < pf->next_vsi)
  8870. pf->next_vsi = vsi->idx;
  8871. unlock_vsi:
  8872. mutex_unlock(&pf->switch_mutex);
  8873. free_vsi:
  8874. kfree(vsi);
  8875. return 0;
  8876. }
  8877. /**
  8878. * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
  8879. * @vsi: the VSI being cleaned
  8880. **/
  8881. static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
  8882. {
  8883. int i;
  8884. if (vsi->tx_rings && vsi->tx_rings[0]) {
  8885. for (i = 0; i < vsi->alloc_queue_pairs; i++) {
  8886. kfree_rcu(vsi->tx_rings[i], rcu);
  8887. vsi->tx_rings[i] = NULL;
  8888. vsi->rx_rings[i] = NULL;
  8889. if (vsi->xdp_rings)
  8890. vsi->xdp_rings[i] = NULL;
  8891. }
  8892. }
  8893. }
  8894. /**
  8895. * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
  8896. * @vsi: the VSI being configured
  8897. **/
  8898. static int i40e_alloc_rings(struct i40e_vsi *vsi)
  8899. {
  8900. int i, qpv = i40e_enabled_xdp_vsi(vsi) ? 3 : 2;
  8901. struct i40e_pf *pf = vsi->back;
  8902. struct i40e_ring *ring;
  8903. /* Set basic values in the rings to be used later during open() */
  8904. for (i = 0; i < vsi->alloc_queue_pairs; i++) {
  8905. /* allocate space for both Tx and Rx in one shot */
  8906. ring = kcalloc(qpv, sizeof(struct i40e_ring), GFP_KERNEL);
  8907. if (!ring)
  8908. goto err_out;
  8909. ring->queue_index = i;
  8910. ring->reg_idx = vsi->base_queue + i;
  8911. ring->ring_active = false;
  8912. ring->vsi = vsi;
  8913. ring->netdev = vsi->netdev;
  8914. ring->dev = &pf->pdev->dev;
  8915. ring->count = vsi->num_desc;
  8916. ring->size = 0;
  8917. ring->dcb_tc = 0;
  8918. if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
  8919. ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
  8920. ring->itr_setting = pf->tx_itr_default;
  8921. vsi->tx_rings[i] = ring++;
  8922. if (!i40e_enabled_xdp_vsi(vsi))
  8923. goto setup_rx;
  8924. ring->queue_index = vsi->alloc_queue_pairs + i;
  8925. ring->reg_idx = vsi->base_queue + ring->queue_index;
  8926. ring->ring_active = false;
  8927. ring->vsi = vsi;
  8928. ring->netdev = NULL;
  8929. ring->dev = &pf->pdev->dev;
  8930. ring->count = vsi->num_desc;
  8931. ring->size = 0;
  8932. ring->dcb_tc = 0;
  8933. if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
  8934. ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
  8935. set_ring_xdp(ring);
  8936. ring->itr_setting = pf->tx_itr_default;
  8937. vsi->xdp_rings[i] = ring++;
  8938. setup_rx:
  8939. ring->queue_index = i;
  8940. ring->reg_idx = vsi->base_queue + i;
  8941. ring->ring_active = false;
  8942. ring->vsi = vsi;
  8943. ring->netdev = vsi->netdev;
  8944. ring->dev = &pf->pdev->dev;
  8945. ring->count = vsi->num_desc;
  8946. ring->size = 0;
  8947. ring->dcb_tc = 0;
  8948. ring->itr_setting = pf->rx_itr_default;
  8949. vsi->rx_rings[i] = ring;
  8950. }
  8951. return 0;
  8952. err_out:
  8953. i40e_vsi_clear_rings(vsi);
  8954. return -ENOMEM;
  8955. }
  8956. /**
  8957. * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
  8958. * @pf: board private structure
  8959. * @vectors: the number of MSI-X vectors to request
  8960. *
  8961. * Returns the number of vectors reserved, or error
  8962. **/
  8963. static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
  8964. {
  8965. vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
  8966. I40E_MIN_MSIX, vectors);
  8967. if (vectors < 0) {
  8968. dev_info(&pf->pdev->dev,
  8969. "MSI-X vector reservation failed: %d\n", vectors);
  8970. vectors = 0;
  8971. }
  8972. return vectors;
  8973. }
  8974. /**
  8975. * i40e_init_msix - Setup the MSIX capability
  8976. * @pf: board private structure
  8977. *
  8978. * Work with the OS to set up the MSIX vectors needed.
  8979. *
  8980. * Returns the number of vectors reserved or negative on failure
  8981. **/
  8982. static int i40e_init_msix(struct i40e_pf *pf)
  8983. {
  8984. struct i40e_hw *hw = &pf->hw;
  8985. int cpus, extra_vectors;
  8986. int vectors_left;
  8987. int v_budget, i;
  8988. int v_actual;
  8989. int iwarp_requested = 0;
  8990. if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
  8991. return -ENODEV;
  8992. /* The number of vectors we'll request will be comprised of:
  8993. * - Add 1 for "other" cause for Admin Queue events, etc.
  8994. * - The number of LAN queue pairs
  8995. * - Queues being used for RSS.
  8996. * We don't need as many as max_rss_size vectors.
  8997. * use rss_size instead in the calculation since that
  8998. * is governed by number of cpus in the system.
  8999. * - assumes symmetric Tx/Rx pairing
  9000. * - The number of VMDq pairs
  9001. * - The CPU count within the NUMA node if iWARP is enabled
  9002. * Once we count this up, try the request.
  9003. *
  9004. * If we can't get what we want, we'll simplify to nearly nothing
  9005. * and try again. If that still fails, we punt.
  9006. */
  9007. vectors_left = hw->func_caps.num_msix_vectors;
  9008. v_budget = 0;
  9009. /* reserve one vector for miscellaneous handler */
  9010. if (vectors_left) {
  9011. v_budget++;
  9012. vectors_left--;
  9013. }
  9014. /* reserve some vectors for the main PF traffic queues. Initially we
  9015. * only reserve at most 50% of the available vectors, in the case that
  9016. * the number of online CPUs is large. This ensures that we can enable
  9017. * extra features as well. Once we've enabled the other features, we
  9018. * will use any remaining vectors to reach as close as we can to the
  9019. * number of online CPUs.
  9020. */
  9021. cpus = num_online_cpus();
  9022. pf->num_lan_msix = min_t(int, cpus, vectors_left / 2);
  9023. vectors_left -= pf->num_lan_msix;
  9024. /* reserve one vector for sideband flow director */
  9025. if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
  9026. if (vectors_left) {
  9027. pf->num_fdsb_msix = 1;
  9028. v_budget++;
  9029. vectors_left--;
  9030. } else {
  9031. pf->num_fdsb_msix = 0;
  9032. }
  9033. }
  9034. /* can we reserve enough for iWARP? */
  9035. if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
  9036. iwarp_requested = pf->num_iwarp_msix;
  9037. if (!vectors_left)
  9038. pf->num_iwarp_msix = 0;
  9039. else if (vectors_left < pf->num_iwarp_msix)
  9040. pf->num_iwarp_msix = 1;
  9041. v_budget += pf->num_iwarp_msix;
  9042. vectors_left -= pf->num_iwarp_msix;
  9043. }
  9044. /* any vectors left over go for VMDq support */
  9045. if (pf->flags & I40E_FLAG_VMDQ_ENABLED) {
  9046. if (!vectors_left) {
  9047. pf->num_vmdq_msix = 0;
  9048. pf->num_vmdq_qps = 0;
  9049. } else {
  9050. int vmdq_vecs_wanted =
  9051. pf->num_vmdq_vsis * pf->num_vmdq_qps;
  9052. int vmdq_vecs =
  9053. min_t(int, vectors_left, vmdq_vecs_wanted);
  9054. /* if we're short on vectors for what's desired, we limit
  9055. * the queues per vmdq. If this is still more than are
  9056. * available, the user will need to change the number of
  9057. * queues/vectors used by the PF later with the ethtool
  9058. * channels command
  9059. */
  9060. if (vectors_left < vmdq_vecs_wanted) {
  9061. pf->num_vmdq_qps = 1;
  9062. vmdq_vecs_wanted = pf->num_vmdq_vsis;
  9063. vmdq_vecs = min_t(int,
  9064. vectors_left,
  9065. vmdq_vecs_wanted);
  9066. }
  9067. pf->num_vmdq_msix = pf->num_vmdq_qps;
  9068. v_budget += vmdq_vecs;
  9069. vectors_left -= vmdq_vecs;
  9070. }
  9071. }
  9072. /* On systems with a large number of SMP cores, we previously limited
  9073. * the number of vectors for num_lan_msix to be at most 50% of the
  9074. * available vectors, to allow for other features. Now, we add back
  9075. * the remaining vectors. However, we ensure that the total
  9076. * num_lan_msix will not exceed num_online_cpus(). To do this, we
  9077. * calculate the number of vectors we can add without going over the
  9078. * cap of CPUs. For systems with a small number of CPUs this will be
  9079. * zero.
  9080. */
  9081. extra_vectors = min_t(int, cpus - pf->num_lan_msix, vectors_left);
  9082. pf->num_lan_msix += extra_vectors;
  9083. vectors_left -= extra_vectors;
  9084. WARN(vectors_left < 0,
  9085. "Calculation of remaining vectors underflowed. This is an accounting bug when determining total MSI-X vectors.\n");
  9086. v_budget += pf->num_lan_msix;
  9087. pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
  9088. GFP_KERNEL);
  9089. if (!pf->msix_entries)
  9090. return -ENOMEM;
  9091. for (i = 0; i < v_budget; i++)
  9092. pf->msix_entries[i].entry = i;
  9093. v_actual = i40e_reserve_msix_vectors(pf, v_budget);
  9094. if (v_actual < I40E_MIN_MSIX) {
  9095. pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
  9096. kfree(pf->msix_entries);
  9097. pf->msix_entries = NULL;
  9098. pci_disable_msix(pf->pdev);
  9099. return -ENODEV;
  9100. } else if (v_actual == I40E_MIN_MSIX) {
  9101. /* Adjust for minimal MSIX use */
  9102. pf->num_vmdq_vsis = 0;
  9103. pf->num_vmdq_qps = 0;
  9104. pf->num_lan_qps = 1;
  9105. pf->num_lan_msix = 1;
  9106. } else if (v_actual != v_budget) {
  9107. /* If we have limited resources, we will start with no vectors
  9108. * for the special features and then allocate vectors to some
  9109. * of these features based on the policy and at the end disable
  9110. * the features that did not get any vectors.
  9111. */
  9112. int vec;
  9113. dev_info(&pf->pdev->dev,
  9114. "MSI-X vector limit reached with %d, wanted %d, attempting to redistribute vectors\n",
  9115. v_actual, v_budget);
  9116. /* reserve the misc vector */
  9117. vec = v_actual - 1;
  9118. /* Scale vector usage down */
  9119. pf->num_vmdq_msix = 1; /* force VMDqs to only one vector */
  9120. pf->num_vmdq_vsis = 1;
  9121. pf->num_vmdq_qps = 1;
  9122. /* partition out the remaining vectors */
  9123. switch (vec) {
  9124. case 2:
  9125. pf->num_lan_msix = 1;
  9126. break;
  9127. case 3:
  9128. if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
  9129. pf->num_lan_msix = 1;
  9130. pf->num_iwarp_msix = 1;
  9131. } else {
  9132. pf->num_lan_msix = 2;
  9133. }
  9134. break;
  9135. default:
  9136. if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
  9137. pf->num_iwarp_msix = min_t(int, (vec / 3),
  9138. iwarp_requested);
  9139. pf->num_vmdq_vsis = min_t(int, (vec / 3),
  9140. I40E_DEFAULT_NUM_VMDQ_VSI);
  9141. } else {
  9142. pf->num_vmdq_vsis = min_t(int, (vec / 2),
  9143. I40E_DEFAULT_NUM_VMDQ_VSI);
  9144. }
  9145. if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
  9146. pf->num_fdsb_msix = 1;
  9147. vec--;
  9148. }
  9149. pf->num_lan_msix = min_t(int,
  9150. (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
  9151. pf->num_lan_msix);
  9152. pf->num_lan_qps = pf->num_lan_msix;
  9153. break;
  9154. }
  9155. }
  9156. if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
  9157. (pf->num_fdsb_msix == 0)) {
  9158. dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n");
  9159. pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
  9160. pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
  9161. }
  9162. if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
  9163. (pf->num_vmdq_msix == 0)) {
  9164. dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
  9165. pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
  9166. }
  9167. if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
  9168. (pf->num_iwarp_msix == 0)) {
  9169. dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
  9170. pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
  9171. }
  9172. i40e_debug(&pf->hw, I40E_DEBUG_INIT,
  9173. "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n",
  9174. pf->num_lan_msix,
  9175. pf->num_vmdq_msix * pf->num_vmdq_vsis,
  9176. pf->num_fdsb_msix,
  9177. pf->num_iwarp_msix);
  9178. return v_actual;
  9179. }
  9180. /**
  9181. * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
  9182. * @vsi: the VSI being configured
  9183. * @v_idx: index of the vector in the vsi struct
  9184. * @cpu: cpu to be used on affinity_mask
  9185. *
  9186. * We allocate one q_vector. If allocation fails we return -ENOMEM.
  9187. **/
  9188. static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx, int cpu)
  9189. {
  9190. struct i40e_q_vector *q_vector;
  9191. /* allocate q_vector */
  9192. q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
  9193. if (!q_vector)
  9194. return -ENOMEM;
  9195. q_vector->vsi = vsi;
  9196. q_vector->v_idx = v_idx;
  9197. cpumask_copy(&q_vector->affinity_mask, cpu_possible_mask);
  9198. if (vsi->netdev)
  9199. netif_napi_add(vsi->netdev, &q_vector->napi,
  9200. i40e_napi_poll, NAPI_POLL_WEIGHT);
  9201. /* tie q_vector and vsi together */
  9202. vsi->q_vectors[v_idx] = q_vector;
  9203. return 0;
  9204. }
  9205. /**
  9206. * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
  9207. * @vsi: the VSI being configured
  9208. *
  9209. * We allocate one q_vector per queue interrupt. If allocation fails we
  9210. * return -ENOMEM.
  9211. **/
  9212. static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
  9213. {
  9214. struct i40e_pf *pf = vsi->back;
  9215. int err, v_idx, num_q_vectors, current_cpu;
  9216. /* if not MSIX, give the one vector only to the LAN VSI */
  9217. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  9218. num_q_vectors = vsi->num_q_vectors;
  9219. else if (vsi == pf->vsi[pf->lan_vsi])
  9220. num_q_vectors = 1;
  9221. else
  9222. return -EINVAL;
  9223. current_cpu = cpumask_first(cpu_online_mask);
  9224. for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
  9225. err = i40e_vsi_alloc_q_vector(vsi, v_idx, current_cpu);
  9226. if (err)
  9227. goto err_out;
  9228. current_cpu = cpumask_next(current_cpu, cpu_online_mask);
  9229. if (unlikely(current_cpu >= nr_cpu_ids))
  9230. current_cpu = cpumask_first(cpu_online_mask);
  9231. }
  9232. return 0;
  9233. err_out:
  9234. while (v_idx--)
  9235. i40e_free_q_vector(vsi, v_idx);
  9236. return err;
  9237. }
  9238. /**
  9239. * i40e_init_interrupt_scheme - Determine proper interrupt scheme
  9240. * @pf: board private structure to initialize
  9241. **/
  9242. static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
  9243. {
  9244. int vectors = 0;
  9245. ssize_t size;
  9246. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  9247. vectors = i40e_init_msix(pf);
  9248. if (vectors < 0) {
  9249. pf->flags &= ~(I40E_FLAG_MSIX_ENABLED |
  9250. I40E_FLAG_IWARP_ENABLED |
  9251. I40E_FLAG_RSS_ENABLED |
  9252. I40E_FLAG_DCB_CAPABLE |
  9253. I40E_FLAG_DCB_ENABLED |
  9254. I40E_FLAG_SRIOV_ENABLED |
  9255. I40E_FLAG_FD_SB_ENABLED |
  9256. I40E_FLAG_FD_ATR_ENABLED |
  9257. I40E_FLAG_VMDQ_ENABLED);
  9258. pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
  9259. /* rework the queue expectations without MSIX */
  9260. i40e_determine_queue_usage(pf);
  9261. }
  9262. }
  9263. if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
  9264. (pf->flags & I40E_FLAG_MSI_ENABLED)) {
  9265. dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
  9266. vectors = pci_enable_msi(pf->pdev);
  9267. if (vectors < 0) {
  9268. dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
  9269. vectors);
  9270. pf->flags &= ~I40E_FLAG_MSI_ENABLED;
  9271. }
  9272. vectors = 1; /* one MSI or Legacy vector */
  9273. }
  9274. if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
  9275. dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
  9276. /* set up vector assignment tracking */
  9277. size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
  9278. pf->irq_pile = kzalloc(size, GFP_KERNEL);
  9279. if (!pf->irq_pile)
  9280. return -ENOMEM;
  9281. pf->irq_pile->num_entries = vectors;
  9282. pf->irq_pile->search_hint = 0;
  9283. /* track first vector for misc interrupts, ignore return */
  9284. (void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
  9285. return 0;
  9286. }
  9287. /**
  9288. * i40e_restore_interrupt_scheme - Restore the interrupt scheme
  9289. * @pf: private board data structure
  9290. *
  9291. * Restore the interrupt scheme that was cleared when we suspended the
  9292. * device. This should be called during resume to re-allocate the q_vectors
  9293. * and reacquire IRQs.
  9294. */
  9295. static int i40e_restore_interrupt_scheme(struct i40e_pf *pf)
  9296. {
  9297. int err, i;
  9298. /* We cleared the MSI and MSI-X flags when disabling the old interrupt
  9299. * scheme. We need to re-enabled them here in order to attempt to
  9300. * re-acquire the MSI or MSI-X vectors
  9301. */
  9302. pf->flags |= (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
  9303. err = i40e_init_interrupt_scheme(pf);
  9304. if (err)
  9305. return err;
  9306. /* Now that we've re-acquired IRQs, we need to remap the vectors and
  9307. * rings together again.
  9308. */
  9309. for (i = 0; i < pf->num_alloc_vsi; i++) {
  9310. if (pf->vsi[i]) {
  9311. err = i40e_vsi_alloc_q_vectors(pf->vsi[i]);
  9312. if (err)
  9313. goto err_unwind;
  9314. i40e_vsi_map_rings_to_vectors(pf->vsi[i]);
  9315. }
  9316. }
  9317. err = i40e_setup_misc_vector(pf);
  9318. if (err)
  9319. goto err_unwind;
  9320. if (pf->flags & I40E_FLAG_IWARP_ENABLED)
  9321. i40e_client_update_msix_info(pf);
  9322. return 0;
  9323. err_unwind:
  9324. while (i--) {
  9325. if (pf->vsi[i])
  9326. i40e_vsi_free_q_vectors(pf->vsi[i]);
  9327. }
  9328. return err;
  9329. }
  9330. /**
  9331. * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
  9332. * @pf: board private structure
  9333. *
  9334. * This sets up the handler for MSIX 0, which is used to manage the
  9335. * non-queue interrupts, e.g. AdminQ and errors. This is not used
  9336. * when in MSI or Legacy interrupt mode.
  9337. **/
  9338. static int i40e_setup_misc_vector(struct i40e_pf *pf)
  9339. {
  9340. struct i40e_hw *hw = &pf->hw;
  9341. int err = 0;
  9342. /* Only request the IRQ once, the first time through. */
  9343. if (!test_and_set_bit(__I40E_MISC_IRQ_REQUESTED, pf->state)) {
  9344. err = request_irq(pf->msix_entries[0].vector,
  9345. i40e_intr, 0, pf->int_name, pf);
  9346. if (err) {
  9347. clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
  9348. dev_info(&pf->pdev->dev,
  9349. "request_irq for %s failed: %d\n",
  9350. pf->int_name, err);
  9351. return -EFAULT;
  9352. }
  9353. }
  9354. i40e_enable_misc_int_causes(pf);
  9355. /* associate no queues to the misc vector */
  9356. wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
  9357. wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K);
  9358. i40e_flush(hw);
  9359. i40e_irq_dynamic_enable_icr0(pf);
  9360. return err;
  9361. }
  9362. /**
  9363. * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
  9364. * @vsi: Pointer to vsi structure
  9365. * @seed: Buffter to store the hash keys
  9366. * @lut: Buffer to store the lookup table entries
  9367. * @lut_size: Size of buffer to store the lookup table entries
  9368. *
  9369. * Return 0 on success, negative on failure
  9370. */
  9371. static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
  9372. u8 *lut, u16 lut_size)
  9373. {
  9374. struct i40e_pf *pf = vsi->back;
  9375. struct i40e_hw *hw = &pf->hw;
  9376. int ret = 0;
  9377. if (seed) {
  9378. ret = i40e_aq_get_rss_key(hw, vsi->id,
  9379. (struct i40e_aqc_get_set_rss_key_data *)seed);
  9380. if (ret) {
  9381. dev_info(&pf->pdev->dev,
  9382. "Cannot get RSS key, err %s aq_err %s\n",
  9383. i40e_stat_str(&pf->hw, ret),
  9384. i40e_aq_str(&pf->hw,
  9385. pf->hw.aq.asq_last_status));
  9386. return ret;
  9387. }
  9388. }
  9389. if (lut) {
  9390. bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
  9391. ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
  9392. if (ret) {
  9393. dev_info(&pf->pdev->dev,
  9394. "Cannot get RSS lut, err %s aq_err %s\n",
  9395. i40e_stat_str(&pf->hw, ret),
  9396. i40e_aq_str(&pf->hw,
  9397. pf->hw.aq.asq_last_status));
  9398. return ret;
  9399. }
  9400. }
  9401. return ret;
  9402. }
  9403. /**
  9404. * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
  9405. * @vsi: Pointer to vsi structure
  9406. * @seed: RSS hash seed
  9407. * @lut: Lookup table
  9408. * @lut_size: Lookup table size
  9409. *
  9410. * Returns 0 on success, negative on failure
  9411. **/
  9412. static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
  9413. const u8 *lut, u16 lut_size)
  9414. {
  9415. struct i40e_pf *pf = vsi->back;
  9416. struct i40e_hw *hw = &pf->hw;
  9417. u16 vf_id = vsi->vf_id;
  9418. u8 i;
  9419. /* Fill out hash function seed */
  9420. if (seed) {
  9421. u32 *seed_dw = (u32 *)seed;
  9422. if (vsi->type == I40E_VSI_MAIN) {
  9423. for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
  9424. wr32(hw, I40E_PFQF_HKEY(i), seed_dw[i]);
  9425. } else if (vsi->type == I40E_VSI_SRIOV) {
  9426. for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
  9427. wr32(hw, I40E_VFQF_HKEY1(i, vf_id), seed_dw[i]);
  9428. } else {
  9429. dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
  9430. }
  9431. }
  9432. if (lut) {
  9433. u32 *lut_dw = (u32 *)lut;
  9434. if (vsi->type == I40E_VSI_MAIN) {
  9435. if (lut_size != I40E_HLUT_ARRAY_SIZE)
  9436. return -EINVAL;
  9437. for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
  9438. wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
  9439. } else if (vsi->type == I40E_VSI_SRIOV) {
  9440. if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
  9441. return -EINVAL;
  9442. for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
  9443. wr32(hw, I40E_VFQF_HLUT1(i, vf_id), lut_dw[i]);
  9444. } else {
  9445. dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
  9446. }
  9447. }
  9448. i40e_flush(hw);
  9449. return 0;
  9450. }
  9451. /**
  9452. * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
  9453. * @vsi: Pointer to VSI structure
  9454. * @seed: Buffer to store the keys
  9455. * @lut: Buffer to store the lookup table entries
  9456. * @lut_size: Size of buffer to store the lookup table entries
  9457. *
  9458. * Returns 0 on success, negative on failure
  9459. */
  9460. static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
  9461. u8 *lut, u16 lut_size)
  9462. {
  9463. struct i40e_pf *pf = vsi->back;
  9464. struct i40e_hw *hw = &pf->hw;
  9465. u16 i;
  9466. if (seed) {
  9467. u32 *seed_dw = (u32 *)seed;
  9468. for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
  9469. seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
  9470. }
  9471. if (lut) {
  9472. u32 *lut_dw = (u32 *)lut;
  9473. if (lut_size != I40E_HLUT_ARRAY_SIZE)
  9474. return -EINVAL;
  9475. for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
  9476. lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
  9477. }
  9478. return 0;
  9479. }
  9480. /**
  9481. * i40e_config_rss - Configure RSS keys and lut
  9482. * @vsi: Pointer to VSI structure
  9483. * @seed: RSS hash seed
  9484. * @lut: Lookup table
  9485. * @lut_size: Lookup table size
  9486. *
  9487. * Returns 0 on success, negative on failure
  9488. */
  9489. int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
  9490. {
  9491. struct i40e_pf *pf = vsi->back;
  9492. if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
  9493. return i40e_config_rss_aq(vsi, seed, lut, lut_size);
  9494. else
  9495. return i40e_config_rss_reg(vsi, seed, lut, lut_size);
  9496. }
  9497. /**
  9498. * i40e_get_rss - Get RSS keys and lut
  9499. * @vsi: Pointer to VSI structure
  9500. * @seed: Buffer to store the keys
  9501. * @lut: Buffer to store the lookup table entries
  9502. * @lut_size: Size of buffer to store the lookup table entries
  9503. *
  9504. * Returns 0 on success, negative on failure
  9505. */
  9506. int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
  9507. {
  9508. struct i40e_pf *pf = vsi->back;
  9509. if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
  9510. return i40e_get_rss_aq(vsi, seed, lut, lut_size);
  9511. else
  9512. return i40e_get_rss_reg(vsi, seed, lut, lut_size);
  9513. }
  9514. /**
  9515. * i40e_fill_rss_lut - Fill the RSS lookup table with default values
  9516. * @pf: Pointer to board private structure
  9517. * @lut: Lookup table
  9518. * @rss_table_size: Lookup table size
  9519. * @rss_size: Range of queue number for hashing
  9520. */
  9521. void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
  9522. u16 rss_table_size, u16 rss_size)
  9523. {
  9524. u16 i;
  9525. for (i = 0; i < rss_table_size; i++)
  9526. lut[i] = i % rss_size;
  9527. }
  9528. /**
  9529. * i40e_pf_config_rss - Prepare for RSS if used
  9530. * @pf: board private structure
  9531. **/
  9532. static int i40e_pf_config_rss(struct i40e_pf *pf)
  9533. {
  9534. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  9535. u8 seed[I40E_HKEY_ARRAY_SIZE];
  9536. u8 *lut;
  9537. struct i40e_hw *hw = &pf->hw;
  9538. u32 reg_val;
  9539. u64 hena;
  9540. int ret;
  9541. /* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
  9542. hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
  9543. ((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
  9544. hena |= i40e_pf_get_default_rss_hena(pf);
  9545. i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
  9546. i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
  9547. /* Determine the RSS table size based on the hardware capabilities */
  9548. reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
  9549. reg_val = (pf->rss_table_size == 512) ?
  9550. (reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
  9551. (reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
  9552. i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
  9553. /* Determine the RSS size of the VSI */
  9554. if (!vsi->rss_size) {
  9555. u16 qcount;
  9556. /* If the firmware does something weird during VSI init, we
  9557. * could end up with zero TCs. Check for that to avoid
  9558. * divide-by-zero. It probably won't pass traffic, but it also
  9559. * won't panic.
  9560. */
  9561. qcount = vsi->num_queue_pairs /
  9562. (vsi->tc_config.numtc ? vsi->tc_config.numtc : 1);
  9563. vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
  9564. }
  9565. if (!vsi->rss_size)
  9566. return -EINVAL;
  9567. lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
  9568. if (!lut)
  9569. return -ENOMEM;
  9570. /* Use user configured lut if there is one, otherwise use default */
  9571. if (vsi->rss_lut_user)
  9572. memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
  9573. else
  9574. i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
  9575. /* Use user configured hash key if there is one, otherwise
  9576. * use default.
  9577. */
  9578. if (vsi->rss_hkey_user)
  9579. memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
  9580. else
  9581. netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
  9582. ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
  9583. kfree(lut);
  9584. return ret;
  9585. }
  9586. /**
  9587. * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
  9588. * @pf: board private structure
  9589. * @queue_count: the requested queue count for rss.
  9590. *
  9591. * returns 0 if rss is not enabled, if enabled returns the final rss queue
  9592. * count which may be different from the requested queue count.
  9593. * Note: expects to be called while under rtnl_lock()
  9594. **/
  9595. int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
  9596. {
  9597. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  9598. int new_rss_size;
  9599. if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
  9600. return 0;
  9601. new_rss_size = min_t(int, queue_count, pf->rss_size_max);
  9602. if (queue_count != vsi->num_queue_pairs) {
  9603. u16 qcount;
  9604. vsi->req_queue_pairs = queue_count;
  9605. i40e_prep_for_reset(pf, true);
  9606. pf->alloc_rss_size = new_rss_size;
  9607. i40e_reset_and_rebuild(pf, true, true);
  9608. /* Discard the user configured hash keys and lut, if less
  9609. * queues are enabled.
  9610. */
  9611. if (queue_count < vsi->rss_size) {
  9612. i40e_clear_rss_config_user(vsi);
  9613. dev_dbg(&pf->pdev->dev,
  9614. "discard user configured hash keys and lut\n");
  9615. }
  9616. /* Reset vsi->rss_size, as number of enabled queues changed */
  9617. qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
  9618. vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
  9619. i40e_pf_config_rss(pf);
  9620. }
  9621. dev_info(&pf->pdev->dev, "User requested queue count/HW max RSS count: %d/%d\n",
  9622. vsi->req_queue_pairs, pf->rss_size_max);
  9623. return pf->alloc_rss_size;
  9624. }
  9625. /**
  9626. * i40e_get_partition_bw_setting - Retrieve BW settings for this PF partition
  9627. * @pf: board private structure
  9628. **/
  9629. i40e_status i40e_get_partition_bw_setting(struct i40e_pf *pf)
  9630. {
  9631. i40e_status status;
  9632. bool min_valid, max_valid;
  9633. u32 max_bw, min_bw;
  9634. status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
  9635. &min_valid, &max_valid);
  9636. if (!status) {
  9637. if (min_valid)
  9638. pf->min_bw = min_bw;
  9639. if (max_valid)
  9640. pf->max_bw = max_bw;
  9641. }
  9642. return status;
  9643. }
  9644. /**
  9645. * i40e_set_partition_bw_setting - Set BW settings for this PF partition
  9646. * @pf: board private structure
  9647. **/
  9648. i40e_status i40e_set_partition_bw_setting(struct i40e_pf *pf)
  9649. {
  9650. struct i40e_aqc_configure_partition_bw_data bw_data;
  9651. i40e_status status;
  9652. /* Set the valid bit for this PF */
  9653. bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
  9654. bw_data.max_bw[pf->hw.pf_id] = pf->max_bw & I40E_ALT_BW_VALUE_MASK;
  9655. bw_data.min_bw[pf->hw.pf_id] = pf->min_bw & I40E_ALT_BW_VALUE_MASK;
  9656. /* Set the new bandwidths */
  9657. status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
  9658. return status;
  9659. }
  9660. /**
  9661. * i40e_commit_partition_bw_setting - Commit BW settings for this PF partition
  9662. * @pf: board private structure
  9663. **/
  9664. i40e_status i40e_commit_partition_bw_setting(struct i40e_pf *pf)
  9665. {
  9666. /* Commit temporary BW setting to permanent NVM image */
  9667. enum i40e_admin_queue_err last_aq_status;
  9668. i40e_status ret;
  9669. u16 nvm_word;
  9670. if (pf->hw.partition_id != 1) {
  9671. dev_info(&pf->pdev->dev,
  9672. "Commit BW only works on partition 1! This is partition %d",
  9673. pf->hw.partition_id);
  9674. ret = I40E_NOT_SUPPORTED;
  9675. goto bw_commit_out;
  9676. }
  9677. /* Acquire NVM for read access */
  9678. ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
  9679. last_aq_status = pf->hw.aq.asq_last_status;
  9680. if (ret) {
  9681. dev_info(&pf->pdev->dev,
  9682. "Cannot acquire NVM for read access, err %s aq_err %s\n",
  9683. i40e_stat_str(&pf->hw, ret),
  9684. i40e_aq_str(&pf->hw, last_aq_status));
  9685. goto bw_commit_out;
  9686. }
  9687. /* Read word 0x10 of NVM - SW compatibility word 1 */
  9688. ret = i40e_aq_read_nvm(&pf->hw,
  9689. I40E_SR_NVM_CONTROL_WORD,
  9690. 0x10, sizeof(nvm_word), &nvm_word,
  9691. false, NULL);
  9692. /* Save off last admin queue command status before releasing
  9693. * the NVM
  9694. */
  9695. last_aq_status = pf->hw.aq.asq_last_status;
  9696. i40e_release_nvm(&pf->hw);
  9697. if (ret) {
  9698. dev_info(&pf->pdev->dev, "NVM read error, err %s aq_err %s\n",
  9699. i40e_stat_str(&pf->hw, ret),
  9700. i40e_aq_str(&pf->hw, last_aq_status));
  9701. goto bw_commit_out;
  9702. }
  9703. /* Wait a bit for NVM release to complete */
  9704. msleep(50);
  9705. /* Acquire NVM for write access */
  9706. ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
  9707. last_aq_status = pf->hw.aq.asq_last_status;
  9708. if (ret) {
  9709. dev_info(&pf->pdev->dev,
  9710. "Cannot acquire NVM for write access, err %s aq_err %s\n",
  9711. i40e_stat_str(&pf->hw, ret),
  9712. i40e_aq_str(&pf->hw, last_aq_status));
  9713. goto bw_commit_out;
  9714. }
  9715. /* Write it back out unchanged to initiate update NVM,
  9716. * which will force a write of the shadow (alt) RAM to
  9717. * the NVM - thus storing the bandwidth values permanently.
  9718. */
  9719. ret = i40e_aq_update_nvm(&pf->hw,
  9720. I40E_SR_NVM_CONTROL_WORD,
  9721. 0x10, sizeof(nvm_word),
  9722. &nvm_word, true, 0, NULL);
  9723. /* Save off last admin queue command status before releasing
  9724. * the NVM
  9725. */
  9726. last_aq_status = pf->hw.aq.asq_last_status;
  9727. i40e_release_nvm(&pf->hw);
  9728. if (ret)
  9729. dev_info(&pf->pdev->dev,
  9730. "BW settings NOT SAVED, err %s aq_err %s\n",
  9731. i40e_stat_str(&pf->hw, ret),
  9732. i40e_aq_str(&pf->hw, last_aq_status));
  9733. bw_commit_out:
  9734. return ret;
  9735. }
  9736. /**
  9737. * i40e_sw_init - Initialize general software structures (struct i40e_pf)
  9738. * @pf: board private structure to initialize
  9739. *
  9740. * i40e_sw_init initializes the Adapter private data structure.
  9741. * Fields are initialized based on PCI device information and
  9742. * OS network device settings (MTU size).
  9743. **/
  9744. static int i40e_sw_init(struct i40e_pf *pf)
  9745. {
  9746. int err = 0;
  9747. int size;
  9748. /* Set default capability flags */
  9749. pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
  9750. I40E_FLAG_MSI_ENABLED |
  9751. I40E_FLAG_MSIX_ENABLED;
  9752. /* Set default ITR */
  9753. pf->rx_itr_default = I40E_ITR_RX_DEF;
  9754. pf->tx_itr_default = I40E_ITR_TX_DEF;
  9755. /* Depending on PF configurations, it is possible that the RSS
  9756. * maximum might end up larger than the available queues
  9757. */
  9758. pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
  9759. pf->alloc_rss_size = 1;
  9760. pf->rss_table_size = pf->hw.func_caps.rss_table_size;
  9761. pf->rss_size_max = min_t(int, pf->rss_size_max,
  9762. pf->hw.func_caps.num_tx_qp);
  9763. if (pf->hw.func_caps.rss) {
  9764. pf->flags |= I40E_FLAG_RSS_ENABLED;
  9765. pf->alloc_rss_size = min_t(int, pf->rss_size_max,
  9766. num_online_cpus());
  9767. }
  9768. /* MFP mode enabled */
  9769. if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
  9770. pf->flags |= I40E_FLAG_MFP_ENABLED;
  9771. dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
  9772. if (i40e_get_partition_bw_setting(pf)) {
  9773. dev_warn(&pf->pdev->dev,
  9774. "Could not get partition bw settings\n");
  9775. } else {
  9776. dev_info(&pf->pdev->dev,
  9777. "Partition BW Min = %8.8x, Max = %8.8x\n",
  9778. pf->min_bw, pf->max_bw);
  9779. /* nudge the Tx scheduler */
  9780. i40e_set_partition_bw_setting(pf);
  9781. }
  9782. }
  9783. if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
  9784. (pf->hw.func_caps.fd_filters_best_effort > 0)) {
  9785. pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
  9786. pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
  9787. if (pf->flags & I40E_FLAG_MFP_ENABLED &&
  9788. pf->hw.num_partitions > 1)
  9789. dev_info(&pf->pdev->dev,
  9790. "Flow Director Sideband mode Disabled in MFP mode\n");
  9791. else
  9792. pf->flags |= I40E_FLAG_FD_SB_ENABLED;
  9793. pf->fdir_pf_filter_count =
  9794. pf->hw.func_caps.fd_filters_guaranteed;
  9795. pf->hw.fdir_shared_filter_count =
  9796. pf->hw.func_caps.fd_filters_best_effort;
  9797. }
  9798. if (pf->hw.mac.type == I40E_MAC_X722) {
  9799. pf->hw_features |= (I40E_HW_RSS_AQ_CAPABLE |
  9800. I40E_HW_128_QP_RSS_CAPABLE |
  9801. I40E_HW_ATR_EVICT_CAPABLE |
  9802. I40E_HW_WB_ON_ITR_CAPABLE |
  9803. I40E_HW_MULTIPLE_TCP_UDP_RSS_PCTYPE |
  9804. I40E_HW_NO_PCI_LINK_CHECK |
  9805. I40E_HW_USE_SET_LLDP_MIB |
  9806. I40E_HW_GENEVE_OFFLOAD_CAPABLE |
  9807. I40E_HW_PTP_L4_CAPABLE |
  9808. I40E_HW_WOL_MC_MAGIC_PKT_WAKE |
  9809. I40E_HW_OUTER_UDP_CSUM_CAPABLE);
  9810. #define I40E_FDEVICT_PCTYPE_DEFAULT 0xc03
  9811. if (rd32(&pf->hw, I40E_GLQF_FDEVICTENA(1)) !=
  9812. I40E_FDEVICT_PCTYPE_DEFAULT) {
  9813. dev_warn(&pf->pdev->dev,
  9814. "FD EVICT PCTYPES are not right, disable FD HW EVICT\n");
  9815. pf->hw_features &= ~I40E_HW_ATR_EVICT_CAPABLE;
  9816. }
  9817. } else if ((pf->hw.aq.api_maj_ver > 1) ||
  9818. ((pf->hw.aq.api_maj_ver == 1) &&
  9819. (pf->hw.aq.api_min_ver > 4))) {
  9820. /* Supported in FW API version higher than 1.4 */
  9821. pf->hw_features |= I40E_HW_GENEVE_OFFLOAD_CAPABLE;
  9822. }
  9823. /* Enable HW ATR eviction if possible */
  9824. if (pf->hw_features & I40E_HW_ATR_EVICT_CAPABLE)
  9825. pf->flags |= I40E_FLAG_HW_ATR_EVICT_ENABLED;
  9826. if ((pf->hw.mac.type == I40E_MAC_XL710) &&
  9827. (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
  9828. (pf->hw.aq.fw_maj_ver < 4))) {
  9829. pf->hw_features |= I40E_HW_RESTART_AUTONEG;
  9830. /* No DCB support for FW < v4.33 */
  9831. pf->hw_features |= I40E_HW_NO_DCB_SUPPORT;
  9832. }
  9833. /* Disable FW LLDP if FW < v4.3 */
  9834. if ((pf->hw.mac.type == I40E_MAC_XL710) &&
  9835. (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
  9836. (pf->hw.aq.fw_maj_ver < 4)))
  9837. pf->hw_features |= I40E_HW_STOP_FW_LLDP;
  9838. /* Use the FW Set LLDP MIB API if FW > v4.40 */
  9839. if ((pf->hw.mac.type == I40E_MAC_XL710) &&
  9840. (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver >= 40)) ||
  9841. (pf->hw.aq.fw_maj_ver >= 5)))
  9842. pf->hw_features |= I40E_HW_USE_SET_LLDP_MIB;
  9843. /* Enable PTP L4 if FW > v6.0 */
  9844. if (pf->hw.mac.type == I40E_MAC_XL710 &&
  9845. pf->hw.aq.fw_maj_ver >= 6)
  9846. pf->hw_features |= I40E_HW_PTP_L4_CAPABLE;
  9847. if (pf->hw.func_caps.vmdq && num_online_cpus() != 1) {
  9848. pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
  9849. pf->flags |= I40E_FLAG_VMDQ_ENABLED;
  9850. pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
  9851. }
  9852. if (pf->hw.func_caps.iwarp && num_online_cpus() != 1) {
  9853. pf->flags |= I40E_FLAG_IWARP_ENABLED;
  9854. /* IWARP needs one extra vector for CQP just like MISC.*/
  9855. pf->num_iwarp_msix = (int)num_online_cpus() + 1;
  9856. }
  9857. /* Stopping the FW LLDP engine is only supported on the
  9858. * XL710 with a FW ver >= 1.7. Also, stopping FW LLDP
  9859. * engine is not supported if NPAR is functioning on this
  9860. * part
  9861. */
  9862. if (pf->hw.mac.type == I40E_MAC_XL710 &&
  9863. !pf->hw.func_caps.npar_enable &&
  9864. (pf->hw.aq.api_maj_ver > 1 ||
  9865. (pf->hw.aq.api_maj_ver == 1 && pf->hw.aq.api_min_ver > 6)))
  9866. pf->hw_features |= I40E_HW_STOPPABLE_FW_LLDP;
  9867. #ifdef CONFIG_PCI_IOV
  9868. if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
  9869. pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
  9870. pf->flags |= I40E_FLAG_SRIOV_ENABLED;
  9871. pf->num_req_vfs = min_t(int,
  9872. pf->hw.func_caps.num_vfs,
  9873. I40E_MAX_VF_COUNT);
  9874. }
  9875. #endif /* CONFIG_PCI_IOV */
  9876. pf->eeprom_version = 0xDEAD;
  9877. pf->lan_veb = I40E_NO_VEB;
  9878. pf->lan_vsi = I40E_NO_VSI;
  9879. /* By default FW has this off for performance reasons */
  9880. pf->flags &= ~I40E_FLAG_VEB_STATS_ENABLED;
  9881. /* set up queue assignment tracking */
  9882. size = sizeof(struct i40e_lump_tracking)
  9883. + (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
  9884. pf->qp_pile = kzalloc(size, GFP_KERNEL);
  9885. if (!pf->qp_pile) {
  9886. err = -ENOMEM;
  9887. goto sw_init_done;
  9888. }
  9889. pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
  9890. pf->qp_pile->search_hint = 0;
  9891. pf->tx_timeout_recovery_level = 1;
  9892. mutex_init(&pf->switch_mutex);
  9893. sw_init_done:
  9894. return err;
  9895. }
  9896. /**
  9897. * i40e_set_ntuple - set the ntuple feature flag and take action
  9898. * @pf: board private structure to initialize
  9899. * @features: the feature set that the stack is suggesting
  9900. *
  9901. * returns a bool to indicate if reset needs to happen
  9902. **/
  9903. bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
  9904. {
  9905. bool need_reset = false;
  9906. /* Check if Flow Director n-tuple support was enabled or disabled. If
  9907. * the state changed, we need to reset.
  9908. */
  9909. if (features & NETIF_F_NTUPLE) {
  9910. /* Enable filters and mark for reset */
  9911. if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
  9912. need_reset = true;
  9913. /* enable FD_SB only if there is MSI-X vector and no cloud
  9914. * filters exist
  9915. */
  9916. if (pf->num_fdsb_msix > 0 && !pf->num_cloud_filters) {
  9917. pf->flags |= I40E_FLAG_FD_SB_ENABLED;
  9918. pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
  9919. }
  9920. } else {
  9921. /* turn off filters, mark for reset and clear SW filter list */
  9922. if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
  9923. need_reset = true;
  9924. i40e_fdir_filter_exit(pf);
  9925. }
  9926. pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
  9927. clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state);
  9928. pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
  9929. /* reset fd counters */
  9930. pf->fd_add_err = 0;
  9931. pf->fd_atr_cnt = 0;
  9932. /* if ATR was auto disabled it can be re-enabled. */
  9933. if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
  9934. if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
  9935. (I40E_DEBUG_FD & pf->hw.debug_mask))
  9936. dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
  9937. }
  9938. return need_reset;
  9939. }
  9940. /**
  9941. * i40e_clear_rss_lut - clear the rx hash lookup table
  9942. * @vsi: the VSI being configured
  9943. **/
  9944. static void i40e_clear_rss_lut(struct i40e_vsi *vsi)
  9945. {
  9946. struct i40e_pf *pf = vsi->back;
  9947. struct i40e_hw *hw = &pf->hw;
  9948. u16 vf_id = vsi->vf_id;
  9949. u8 i;
  9950. if (vsi->type == I40E_VSI_MAIN) {
  9951. for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
  9952. wr32(hw, I40E_PFQF_HLUT(i), 0);
  9953. } else if (vsi->type == I40E_VSI_SRIOV) {
  9954. for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
  9955. i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0);
  9956. } else {
  9957. dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
  9958. }
  9959. }
  9960. /**
  9961. * i40e_set_features - set the netdev feature flags
  9962. * @netdev: ptr to the netdev being adjusted
  9963. * @features: the feature set that the stack is suggesting
  9964. * Note: expects to be called while under rtnl_lock()
  9965. **/
  9966. static int i40e_set_features(struct net_device *netdev,
  9967. netdev_features_t features)
  9968. {
  9969. struct i40e_netdev_priv *np = netdev_priv(netdev);
  9970. struct i40e_vsi *vsi = np->vsi;
  9971. struct i40e_pf *pf = vsi->back;
  9972. bool need_reset;
  9973. if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
  9974. i40e_pf_config_rss(pf);
  9975. else if (!(features & NETIF_F_RXHASH) &&
  9976. netdev->features & NETIF_F_RXHASH)
  9977. i40e_clear_rss_lut(vsi);
  9978. if (features & NETIF_F_HW_VLAN_CTAG_RX)
  9979. i40e_vlan_stripping_enable(vsi);
  9980. else
  9981. i40e_vlan_stripping_disable(vsi);
  9982. if (!(features & NETIF_F_HW_TC) && pf->num_cloud_filters) {
  9983. dev_err(&pf->pdev->dev,
  9984. "Offloaded tc filters active, can't turn hw_tc_offload off");
  9985. return -EINVAL;
  9986. }
  9987. need_reset = i40e_set_ntuple(pf, features);
  9988. if (need_reset)
  9989. i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
  9990. return 0;
  9991. }
  9992. /**
  9993. * i40e_get_udp_port_idx - Lookup a possibly offloaded for Rx UDP port
  9994. * @pf: board private structure
  9995. * @port: The UDP port to look up
  9996. *
  9997. * Returns the index number or I40E_MAX_PF_UDP_OFFLOAD_PORTS if port not found
  9998. **/
  9999. static u8 i40e_get_udp_port_idx(struct i40e_pf *pf, u16 port)
  10000. {
  10001. u8 i;
  10002. for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
  10003. /* Do not report ports with pending deletions as
  10004. * being available.
  10005. */
  10006. if (!port && (pf->pending_udp_bitmap & BIT_ULL(i)))
  10007. continue;
  10008. if (pf->udp_ports[i].port == port)
  10009. return i;
  10010. }
  10011. return i;
  10012. }
  10013. /**
  10014. * i40e_udp_tunnel_add - Get notifications about UDP tunnel ports that come up
  10015. * @netdev: This physical port's netdev
  10016. * @ti: Tunnel endpoint information
  10017. **/
  10018. static void i40e_udp_tunnel_add(struct net_device *netdev,
  10019. struct udp_tunnel_info *ti)
  10020. {
  10021. struct i40e_netdev_priv *np = netdev_priv(netdev);
  10022. struct i40e_vsi *vsi = np->vsi;
  10023. struct i40e_pf *pf = vsi->back;
  10024. u16 port = ntohs(ti->port);
  10025. u8 next_idx;
  10026. u8 idx;
  10027. idx = i40e_get_udp_port_idx(pf, port);
  10028. /* Check if port already exists */
  10029. if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
  10030. netdev_info(netdev, "port %d already offloaded\n", port);
  10031. return;
  10032. }
  10033. /* Now check if there is space to add the new port */
  10034. next_idx = i40e_get_udp_port_idx(pf, 0);
  10035. if (next_idx == I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
  10036. netdev_info(netdev, "maximum number of offloaded UDP ports reached, not adding port %d\n",
  10037. port);
  10038. return;
  10039. }
  10040. switch (ti->type) {
  10041. case UDP_TUNNEL_TYPE_VXLAN:
  10042. pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_VXLAN;
  10043. break;
  10044. case UDP_TUNNEL_TYPE_GENEVE:
  10045. if (!(pf->hw_features & I40E_HW_GENEVE_OFFLOAD_CAPABLE))
  10046. return;
  10047. pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_NGE;
  10048. break;
  10049. default:
  10050. return;
  10051. }
  10052. /* New port: add it and mark its index in the bitmap */
  10053. pf->udp_ports[next_idx].port = port;
  10054. pf->udp_ports[next_idx].filter_index = I40E_UDP_PORT_INDEX_UNUSED;
  10055. pf->pending_udp_bitmap |= BIT_ULL(next_idx);
  10056. set_bit(__I40E_UDP_FILTER_SYNC_PENDING, pf->state);
  10057. }
  10058. /**
  10059. * i40e_udp_tunnel_del - Get notifications about UDP tunnel ports that go away
  10060. * @netdev: This physical port's netdev
  10061. * @ti: Tunnel endpoint information
  10062. **/
  10063. static void i40e_udp_tunnel_del(struct net_device *netdev,
  10064. struct udp_tunnel_info *ti)
  10065. {
  10066. struct i40e_netdev_priv *np = netdev_priv(netdev);
  10067. struct i40e_vsi *vsi = np->vsi;
  10068. struct i40e_pf *pf = vsi->back;
  10069. u16 port = ntohs(ti->port);
  10070. u8 idx;
  10071. idx = i40e_get_udp_port_idx(pf, port);
  10072. /* Check if port already exists */
  10073. if (idx >= I40E_MAX_PF_UDP_OFFLOAD_PORTS)
  10074. goto not_found;
  10075. switch (ti->type) {
  10076. case UDP_TUNNEL_TYPE_VXLAN:
  10077. if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_VXLAN)
  10078. goto not_found;
  10079. break;
  10080. case UDP_TUNNEL_TYPE_GENEVE:
  10081. if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_NGE)
  10082. goto not_found;
  10083. break;
  10084. default:
  10085. goto not_found;
  10086. }
  10087. /* if port exists, set it to 0 (mark for deletion)
  10088. * and make it pending
  10089. */
  10090. pf->udp_ports[idx].port = 0;
  10091. /* Toggle pending bit instead of setting it. This way if we are
  10092. * deleting a port that has yet to be added we just clear the pending
  10093. * bit and don't have to worry about it.
  10094. */
  10095. pf->pending_udp_bitmap ^= BIT_ULL(idx);
  10096. set_bit(__I40E_UDP_FILTER_SYNC_PENDING, pf->state);
  10097. return;
  10098. not_found:
  10099. netdev_warn(netdev, "UDP port %d was not found, not deleting\n",
  10100. port);
  10101. }
  10102. static int i40e_get_phys_port_id(struct net_device *netdev,
  10103. struct netdev_phys_item_id *ppid)
  10104. {
  10105. struct i40e_netdev_priv *np = netdev_priv(netdev);
  10106. struct i40e_pf *pf = np->vsi->back;
  10107. struct i40e_hw *hw = &pf->hw;
  10108. if (!(pf->hw_features & I40E_HW_PORT_ID_VALID))
  10109. return -EOPNOTSUPP;
  10110. ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
  10111. memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
  10112. return 0;
  10113. }
  10114. /**
  10115. * i40e_ndo_fdb_add - add an entry to the hardware database
  10116. * @ndm: the input from the stack
  10117. * @tb: pointer to array of nladdr (unused)
  10118. * @dev: the net device pointer
  10119. * @addr: the MAC address entry being added
  10120. * @vid: VLAN ID
  10121. * @flags: instructions from stack about fdb operation
  10122. */
  10123. static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
  10124. struct net_device *dev,
  10125. const unsigned char *addr, u16 vid,
  10126. u16 flags)
  10127. {
  10128. struct i40e_netdev_priv *np = netdev_priv(dev);
  10129. struct i40e_pf *pf = np->vsi->back;
  10130. int err = 0;
  10131. if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
  10132. return -EOPNOTSUPP;
  10133. if (vid) {
  10134. pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
  10135. return -EINVAL;
  10136. }
  10137. /* Hardware does not support aging addresses so if a
  10138. * ndm_state is given only allow permanent addresses
  10139. */
  10140. if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
  10141. netdev_info(dev, "FDB only supports static addresses\n");
  10142. return -EINVAL;
  10143. }
  10144. if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
  10145. err = dev_uc_add_excl(dev, addr);
  10146. else if (is_multicast_ether_addr(addr))
  10147. err = dev_mc_add_excl(dev, addr);
  10148. else
  10149. err = -EINVAL;
  10150. /* Only return duplicate errors if NLM_F_EXCL is set */
  10151. if (err == -EEXIST && !(flags & NLM_F_EXCL))
  10152. err = 0;
  10153. return err;
  10154. }
  10155. /**
  10156. * i40e_ndo_bridge_setlink - Set the hardware bridge mode
  10157. * @dev: the netdev being configured
  10158. * @nlh: RTNL message
  10159. * @flags: bridge flags
  10160. *
  10161. * Inserts a new hardware bridge if not already created and
  10162. * enables the bridging mode requested (VEB or VEPA). If the
  10163. * hardware bridge has already been inserted and the request
  10164. * is to change the mode then that requires a PF reset to
  10165. * allow rebuild of the components with required hardware
  10166. * bridge mode enabled.
  10167. *
  10168. * Note: expects to be called while under rtnl_lock()
  10169. **/
  10170. static int i40e_ndo_bridge_setlink(struct net_device *dev,
  10171. struct nlmsghdr *nlh,
  10172. u16 flags)
  10173. {
  10174. struct i40e_netdev_priv *np = netdev_priv(dev);
  10175. struct i40e_vsi *vsi = np->vsi;
  10176. struct i40e_pf *pf = vsi->back;
  10177. struct i40e_veb *veb = NULL;
  10178. struct nlattr *attr, *br_spec;
  10179. int i, rem;
  10180. /* Only for PF VSI for now */
  10181. if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
  10182. return -EOPNOTSUPP;
  10183. /* Find the HW bridge for PF VSI */
  10184. for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
  10185. if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
  10186. veb = pf->veb[i];
  10187. }
  10188. br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
  10189. nla_for_each_nested(attr, br_spec, rem) {
  10190. __u16 mode;
  10191. if (nla_type(attr) != IFLA_BRIDGE_MODE)
  10192. continue;
  10193. mode = nla_get_u16(attr);
  10194. if ((mode != BRIDGE_MODE_VEPA) &&
  10195. (mode != BRIDGE_MODE_VEB))
  10196. return -EINVAL;
  10197. /* Insert a new HW bridge */
  10198. if (!veb) {
  10199. veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
  10200. vsi->tc_config.enabled_tc);
  10201. if (veb) {
  10202. veb->bridge_mode = mode;
  10203. i40e_config_bridge_mode(veb);
  10204. } else {
  10205. /* No Bridge HW offload available */
  10206. return -ENOENT;
  10207. }
  10208. break;
  10209. } else if (mode != veb->bridge_mode) {
  10210. /* Existing HW bridge but different mode needs reset */
  10211. veb->bridge_mode = mode;
  10212. /* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
  10213. if (mode == BRIDGE_MODE_VEB)
  10214. pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
  10215. else
  10216. pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
  10217. i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
  10218. break;
  10219. }
  10220. }
  10221. return 0;
  10222. }
  10223. /**
  10224. * i40e_ndo_bridge_getlink - Get the hardware bridge mode
  10225. * @skb: skb buff
  10226. * @pid: process id
  10227. * @seq: RTNL message seq #
  10228. * @dev: the netdev being configured
  10229. * @filter_mask: unused
  10230. * @nlflags: netlink flags passed in
  10231. *
  10232. * Return the mode in which the hardware bridge is operating in
  10233. * i.e VEB or VEPA.
  10234. **/
  10235. static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
  10236. struct net_device *dev,
  10237. u32 __always_unused filter_mask,
  10238. int nlflags)
  10239. {
  10240. struct i40e_netdev_priv *np = netdev_priv(dev);
  10241. struct i40e_vsi *vsi = np->vsi;
  10242. struct i40e_pf *pf = vsi->back;
  10243. struct i40e_veb *veb = NULL;
  10244. int i;
  10245. /* Only for PF VSI for now */
  10246. if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
  10247. return -EOPNOTSUPP;
  10248. /* Find the HW bridge for the PF VSI */
  10249. for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
  10250. if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
  10251. veb = pf->veb[i];
  10252. }
  10253. if (!veb)
  10254. return 0;
  10255. return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
  10256. 0, 0, nlflags, filter_mask, NULL);
  10257. }
  10258. /**
  10259. * i40e_features_check - Validate encapsulated packet conforms to limits
  10260. * @skb: skb buff
  10261. * @dev: This physical port's netdev
  10262. * @features: Offload features that the stack believes apply
  10263. **/
  10264. static netdev_features_t i40e_features_check(struct sk_buff *skb,
  10265. struct net_device *dev,
  10266. netdev_features_t features)
  10267. {
  10268. size_t len;
  10269. /* No point in doing any of this if neither checksum nor GSO are
  10270. * being requested for this frame. We can rule out both by just
  10271. * checking for CHECKSUM_PARTIAL
  10272. */
  10273. if (skb->ip_summed != CHECKSUM_PARTIAL)
  10274. return features;
  10275. /* We cannot support GSO if the MSS is going to be less than
  10276. * 64 bytes. If it is then we need to drop support for GSO.
  10277. */
  10278. if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
  10279. features &= ~NETIF_F_GSO_MASK;
  10280. /* MACLEN can support at most 63 words */
  10281. len = skb_network_header(skb) - skb->data;
  10282. if (len & ~(63 * 2))
  10283. goto out_err;
  10284. /* IPLEN and EIPLEN can support at most 127 dwords */
  10285. len = skb_transport_header(skb) - skb_network_header(skb);
  10286. if (len & ~(127 * 4))
  10287. goto out_err;
  10288. if (skb->encapsulation) {
  10289. /* L4TUNLEN can support 127 words */
  10290. len = skb_inner_network_header(skb) - skb_transport_header(skb);
  10291. if (len & ~(127 * 2))
  10292. goto out_err;
  10293. /* IPLEN can support at most 127 dwords */
  10294. len = skb_inner_transport_header(skb) -
  10295. skb_inner_network_header(skb);
  10296. if (len & ~(127 * 4))
  10297. goto out_err;
  10298. }
  10299. /* No need to validate L4LEN as TCP is the only protocol with a
  10300. * a flexible value and we support all possible values supported
  10301. * by TCP, which is at most 15 dwords
  10302. */
  10303. return features;
  10304. out_err:
  10305. return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
  10306. }
  10307. /**
  10308. * i40e_xdp_setup - add/remove an XDP program
  10309. * @vsi: VSI to changed
  10310. * @prog: XDP program
  10311. **/
  10312. static int i40e_xdp_setup(struct i40e_vsi *vsi,
  10313. struct bpf_prog *prog)
  10314. {
  10315. int frame_size = vsi->netdev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
  10316. struct i40e_pf *pf = vsi->back;
  10317. struct bpf_prog *old_prog;
  10318. bool need_reset;
  10319. int i;
  10320. /* Don't allow frames that span over multiple buffers */
  10321. if (frame_size > vsi->rx_buf_len)
  10322. return -EINVAL;
  10323. if (!i40e_enabled_xdp_vsi(vsi) && !prog)
  10324. return 0;
  10325. /* When turning XDP on->off/off->on we reset and rebuild the rings. */
  10326. need_reset = (i40e_enabled_xdp_vsi(vsi) != !!prog);
  10327. if (need_reset)
  10328. i40e_prep_for_reset(pf, true);
  10329. old_prog = xchg(&vsi->xdp_prog, prog);
  10330. if (need_reset)
  10331. i40e_reset_and_rebuild(pf, true, true);
  10332. for (i = 0; i < vsi->num_queue_pairs; i++)
  10333. WRITE_ONCE(vsi->rx_rings[i]->xdp_prog, vsi->xdp_prog);
  10334. if (old_prog)
  10335. bpf_prog_put(old_prog);
  10336. return 0;
  10337. }
  10338. /**
  10339. * i40e_xdp - implements ndo_bpf for i40e
  10340. * @dev: netdevice
  10341. * @xdp: XDP command
  10342. **/
  10343. static int i40e_xdp(struct net_device *dev,
  10344. struct netdev_bpf *xdp)
  10345. {
  10346. struct i40e_netdev_priv *np = netdev_priv(dev);
  10347. struct i40e_vsi *vsi = np->vsi;
  10348. if (vsi->type != I40E_VSI_MAIN)
  10349. return -EINVAL;
  10350. switch (xdp->command) {
  10351. case XDP_SETUP_PROG:
  10352. return i40e_xdp_setup(vsi, xdp->prog);
  10353. case XDP_QUERY_PROG:
  10354. xdp->prog_id = vsi->xdp_prog ? vsi->xdp_prog->aux->id : 0;
  10355. return 0;
  10356. default:
  10357. return -EINVAL;
  10358. }
  10359. }
  10360. static const struct net_device_ops i40e_netdev_ops = {
  10361. .ndo_open = i40e_open,
  10362. .ndo_stop = i40e_close,
  10363. .ndo_start_xmit = i40e_lan_xmit_frame,
  10364. .ndo_get_stats64 = i40e_get_netdev_stats_struct,
  10365. .ndo_set_rx_mode = i40e_set_rx_mode,
  10366. .ndo_validate_addr = eth_validate_addr,
  10367. .ndo_set_mac_address = i40e_set_mac,
  10368. .ndo_change_mtu = i40e_change_mtu,
  10369. .ndo_do_ioctl = i40e_ioctl,
  10370. .ndo_tx_timeout = i40e_tx_timeout,
  10371. .ndo_vlan_rx_add_vid = i40e_vlan_rx_add_vid,
  10372. .ndo_vlan_rx_kill_vid = i40e_vlan_rx_kill_vid,
  10373. #ifdef CONFIG_NET_POLL_CONTROLLER
  10374. .ndo_poll_controller = i40e_netpoll,
  10375. #endif
  10376. .ndo_setup_tc = __i40e_setup_tc,
  10377. .ndo_set_features = i40e_set_features,
  10378. .ndo_set_vf_mac = i40e_ndo_set_vf_mac,
  10379. .ndo_set_vf_vlan = i40e_ndo_set_vf_port_vlan,
  10380. .ndo_set_vf_rate = i40e_ndo_set_vf_bw,
  10381. .ndo_get_vf_config = i40e_ndo_get_vf_config,
  10382. .ndo_set_vf_link_state = i40e_ndo_set_vf_link_state,
  10383. .ndo_set_vf_spoofchk = i40e_ndo_set_vf_spoofchk,
  10384. .ndo_set_vf_trust = i40e_ndo_set_vf_trust,
  10385. .ndo_udp_tunnel_add = i40e_udp_tunnel_add,
  10386. .ndo_udp_tunnel_del = i40e_udp_tunnel_del,
  10387. .ndo_get_phys_port_id = i40e_get_phys_port_id,
  10388. .ndo_fdb_add = i40e_ndo_fdb_add,
  10389. .ndo_features_check = i40e_features_check,
  10390. .ndo_bridge_getlink = i40e_ndo_bridge_getlink,
  10391. .ndo_bridge_setlink = i40e_ndo_bridge_setlink,
  10392. .ndo_bpf = i40e_xdp,
  10393. .ndo_xdp_xmit = i40e_xdp_xmit,
  10394. };
  10395. /**
  10396. * i40e_config_netdev - Setup the netdev flags
  10397. * @vsi: the VSI being configured
  10398. *
  10399. * Returns 0 on success, negative value on failure
  10400. **/
  10401. static int i40e_config_netdev(struct i40e_vsi *vsi)
  10402. {
  10403. struct i40e_pf *pf = vsi->back;
  10404. struct i40e_hw *hw = &pf->hw;
  10405. struct i40e_netdev_priv *np;
  10406. struct net_device *netdev;
  10407. u8 broadcast[ETH_ALEN];
  10408. u8 mac_addr[ETH_ALEN];
  10409. int etherdev_size;
  10410. netdev_features_t hw_enc_features;
  10411. netdev_features_t hw_features;
  10412. etherdev_size = sizeof(struct i40e_netdev_priv);
  10413. netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
  10414. if (!netdev)
  10415. return -ENOMEM;
  10416. vsi->netdev = netdev;
  10417. np = netdev_priv(netdev);
  10418. np->vsi = vsi;
  10419. hw_enc_features = NETIF_F_SG |
  10420. NETIF_F_IP_CSUM |
  10421. NETIF_F_IPV6_CSUM |
  10422. NETIF_F_HIGHDMA |
  10423. NETIF_F_SOFT_FEATURES |
  10424. NETIF_F_TSO |
  10425. NETIF_F_TSO_ECN |
  10426. NETIF_F_TSO6 |
  10427. NETIF_F_GSO_GRE |
  10428. NETIF_F_GSO_GRE_CSUM |
  10429. NETIF_F_GSO_PARTIAL |
  10430. NETIF_F_GSO_UDP_TUNNEL |
  10431. NETIF_F_GSO_UDP_TUNNEL_CSUM |
  10432. NETIF_F_SCTP_CRC |
  10433. NETIF_F_RXHASH |
  10434. NETIF_F_RXCSUM |
  10435. 0;
  10436. if (!(pf->hw_features & I40E_HW_OUTER_UDP_CSUM_CAPABLE))
  10437. netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
  10438. netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
  10439. netdev->hw_enc_features |= hw_enc_features;
  10440. /* record features VLANs can make use of */
  10441. netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
  10442. if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
  10443. netdev->hw_features |= NETIF_F_NTUPLE | NETIF_F_HW_TC;
  10444. hw_features = hw_enc_features |
  10445. NETIF_F_HW_VLAN_CTAG_TX |
  10446. NETIF_F_HW_VLAN_CTAG_RX;
  10447. netdev->hw_features |= hw_features;
  10448. netdev->features |= hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
  10449. netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
  10450. if (vsi->type == I40E_VSI_MAIN) {
  10451. SET_NETDEV_DEV(netdev, &pf->pdev->dev);
  10452. ether_addr_copy(mac_addr, hw->mac.perm_addr);
  10453. /* The following steps are necessary for two reasons. First,
  10454. * some older NVM configurations load a default MAC-VLAN
  10455. * filter that will accept any tagged packet, and we want to
  10456. * replace this with a normal filter. Additionally, it is
  10457. * possible our MAC address was provided by the platform using
  10458. * Open Firmware or similar.
  10459. *
  10460. * Thus, we need to remove the default filter and install one
  10461. * specific to the MAC address.
  10462. */
  10463. i40e_rm_default_mac_filter(vsi, mac_addr);
  10464. spin_lock_bh(&vsi->mac_filter_hash_lock);
  10465. i40e_add_mac_filter(vsi, mac_addr);
  10466. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  10467. } else {
  10468. /* Relate the VSI_VMDQ name to the VSI_MAIN name. Note that we
  10469. * are still limited by IFNAMSIZ, but we're adding 'v%d\0' to
  10470. * the end, which is 4 bytes long, so force truncation of the
  10471. * original name by IFNAMSIZ - 4
  10472. */
  10473. snprintf(netdev->name, IFNAMSIZ, "%.*sv%%d",
  10474. IFNAMSIZ - 4,
  10475. pf->vsi[pf->lan_vsi]->netdev->name);
  10476. eth_random_addr(mac_addr);
  10477. spin_lock_bh(&vsi->mac_filter_hash_lock);
  10478. i40e_add_mac_filter(vsi, mac_addr);
  10479. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  10480. }
  10481. /* Add the broadcast filter so that we initially will receive
  10482. * broadcast packets. Note that when a new VLAN is first added the
  10483. * driver will convert all filters marked I40E_VLAN_ANY into VLAN
  10484. * specific filters as part of transitioning into "vlan" operation.
  10485. * When more VLANs are added, the driver will copy each existing MAC
  10486. * filter and add it for the new VLAN.
  10487. *
  10488. * Broadcast filters are handled specially by
  10489. * i40e_sync_filters_subtask, as the driver must to set the broadcast
  10490. * promiscuous bit instead of adding this directly as a MAC/VLAN
  10491. * filter. The subtask will update the correct broadcast promiscuous
  10492. * bits as VLANs become active or inactive.
  10493. */
  10494. eth_broadcast_addr(broadcast);
  10495. spin_lock_bh(&vsi->mac_filter_hash_lock);
  10496. i40e_add_mac_filter(vsi, broadcast);
  10497. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  10498. ether_addr_copy(netdev->dev_addr, mac_addr);
  10499. ether_addr_copy(netdev->perm_addr, mac_addr);
  10500. netdev->priv_flags |= IFF_UNICAST_FLT;
  10501. netdev->priv_flags |= IFF_SUPP_NOFCS;
  10502. /* Setup netdev TC information */
  10503. i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
  10504. netdev->netdev_ops = &i40e_netdev_ops;
  10505. netdev->watchdog_timeo = 5 * HZ;
  10506. i40e_set_ethtool_ops(netdev);
  10507. /* MTU range: 68 - 9706 */
  10508. netdev->min_mtu = ETH_MIN_MTU;
  10509. netdev->max_mtu = I40E_MAX_RXBUFFER - I40E_PACKET_HDR_PAD;
  10510. return 0;
  10511. }
  10512. /**
  10513. * i40e_vsi_delete - Delete a VSI from the switch
  10514. * @vsi: the VSI being removed
  10515. *
  10516. * Returns 0 on success, negative value on failure
  10517. **/
  10518. static void i40e_vsi_delete(struct i40e_vsi *vsi)
  10519. {
  10520. /* remove default VSI is not allowed */
  10521. if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
  10522. return;
  10523. i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
  10524. }
  10525. /**
  10526. * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
  10527. * @vsi: the VSI being queried
  10528. *
  10529. * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
  10530. **/
  10531. int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
  10532. {
  10533. struct i40e_veb *veb;
  10534. struct i40e_pf *pf = vsi->back;
  10535. /* Uplink is not a bridge so default to VEB */
  10536. if (vsi->veb_idx == I40E_NO_VEB)
  10537. return 1;
  10538. veb = pf->veb[vsi->veb_idx];
  10539. if (!veb) {
  10540. dev_info(&pf->pdev->dev,
  10541. "There is no veb associated with the bridge\n");
  10542. return -ENOENT;
  10543. }
  10544. /* Uplink is a bridge in VEPA mode */
  10545. if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
  10546. return 0;
  10547. } else {
  10548. /* Uplink is a bridge in VEB mode */
  10549. return 1;
  10550. }
  10551. /* VEPA is now default bridge, so return 0 */
  10552. return 0;
  10553. }
  10554. /**
  10555. * i40e_add_vsi - Add a VSI to the switch
  10556. * @vsi: the VSI being configured
  10557. *
  10558. * This initializes a VSI context depending on the VSI type to be added and
  10559. * passes it down to the add_vsi aq command.
  10560. **/
  10561. static int i40e_add_vsi(struct i40e_vsi *vsi)
  10562. {
  10563. int ret = -ENODEV;
  10564. struct i40e_pf *pf = vsi->back;
  10565. struct i40e_hw *hw = &pf->hw;
  10566. struct i40e_vsi_context ctxt;
  10567. struct i40e_mac_filter *f;
  10568. struct hlist_node *h;
  10569. int bkt;
  10570. u8 enabled_tc = 0x1; /* TC0 enabled */
  10571. int f_count = 0;
  10572. memset(&ctxt, 0, sizeof(ctxt));
  10573. switch (vsi->type) {
  10574. case I40E_VSI_MAIN:
  10575. /* The PF's main VSI is already setup as part of the
  10576. * device initialization, so we'll not bother with
  10577. * the add_vsi call, but we will retrieve the current
  10578. * VSI context.
  10579. */
  10580. ctxt.seid = pf->main_vsi_seid;
  10581. ctxt.pf_num = pf->hw.pf_id;
  10582. ctxt.vf_num = 0;
  10583. ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
  10584. ctxt.flags = I40E_AQ_VSI_TYPE_PF;
  10585. if (ret) {
  10586. dev_info(&pf->pdev->dev,
  10587. "couldn't get PF vsi config, err %s aq_err %s\n",
  10588. i40e_stat_str(&pf->hw, ret),
  10589. i40e_aq_str(&pf->hw,
  10590. pf->hw.aq.asq_last_status));
  10591. return -ENOENT;
  10592. }
  10593. vsi->info = ctxt.info;
  10594. vsi->info.valid_sections = 0;
  10595. vsi->seid = ctxt.seid;
  10596. vsi->id = ctxt.vsi_number;
  10597. enabled_tc = i40e_pf_get_tc_map(pf);
  10598. /* Source pruning is enabled by default, so the flag is
  10599. * negative logic - if it's set, we need to fiddle with
  10600. * the VSI to disable source pruning.
  10601. */
  10602. if (pf->flags & I40E_FLAG_SOURCE_PRUNING_DISABLED) {
  10603. memset(&ctxt, 0, sizeof(ctxt));
  10604. ctxt.seid = pf->main_vsi_seid;
  10605. ctxt.pf_num = pf->hw.pf_id;
  10606. ctxt.vf_num = 0;
  10607. ctxt.info.valid_sections |=
  10608. cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  10609. ctxt.info.switch_id =
  10610. cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_LOCAL_LB);
  10611. ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
  10612. if (ret) {
  10613. dev_info(&pf->pdev->dev,
  10614. "update vsi failed, err %s aq_err %s\n",
  10615. i40e_stat_str(&pf->hw, ret),
  10616. i40e_aq_str(&pf->hw,
  10617. pf->hw.aq.asq_last_status));
  10618. ret = -ENOENT;
  10619. goto err;
  10620. }
  10621. }
  10622. /* MFP mode setup queue map and update VSI */
  10623. if ((pf->flags & I40E_FLAG_MFP_ENABLED) &&
  10624. !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
  10625. memset(&ctxt, 0, sizeof(ctxt));
  10626. ctxt.seid = pf->main_vsi_seid;
  10627. ctxt.pf_num = pf->hw.pf_id;
  10628. ctxt.vf_num = 0;
  10629. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
  10630. ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
  10631. if (ret) {
  10632. dev_info(&pf->pdev->dev,
  10633. "update vsi failed, err %s aq_err %s\n",
  10634. i40e_stat_str(&pf->hw, ret),
  10635. i40e_aq_str(&pf->hw,
  10636. pf->hw.aq.asq_last_status));
  10637. ret = -ENOENT;
  10638. goto err;
  10639. }
  10640. /* update the local VSI info queue map */
  10641. i40e_vsi_update_queue_map(vsi, &ctxt);
  10642. vsi->info.valid_sections = 0;
  10643. } else {
  10644. /* Default/Main VSI is only enabled for TC0
  10645. * reconfigure it to enable all TCs that are
  10646. * available on the port in SFP mode.
  10647. * For MFP case the iSCSI PF would use this
  10648. * flow to enable LAN+iSCSI TC.
  10649. */
  10650. ret = i40e_vsi_config_tc(vsi, enabled_tc);
  10651. if (ret) {
  10652. /* Single TC condition is not fatal,
  10653. * message and continue
  10654. */
  10655. dev_info(&pf->pdev->dev,
  10656. "failed to configure TCs for main VSI tc_map 0x%08x, err %s aq_err %s\n",
  10657. enabled_tc,
  10658. i40e_stat_str(&pf->hw, ret),
  10659. i40e_aq_str(&pf->hw,
  10660. pf->hw.aq.asq_last_status));
  10661. }
  10662. }
  10663. break;
  10664. case I40E_VSI_FDIR:
  10665. ctxt.pf_num = hw->pf_id;
  10666. ctxt.vf_num = 0;
  10667. ctxt.uplink_seid = vsi->uplink_seid;
  10668. ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
  10669. ctxt.flags = I40E_AQ_VSI_TYPE_PF;
  10670. if ((pf->flags & I40E_FLAG_VEB_MODE_ENABLED) &&
  10671. (i40e_is_vsi_uplink_mode_veb(vsi))) {
  10672. ctxt.info.valid_sections |=
  10673. cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  10674. ctxt.info.switch_id =
  10675. cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  10676. }
  10677. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
  10678. break;
  10679. case I40E_VSI_VMDQ2:
  10680. ctxt.pf_num = hw->pf_id;
  10681. ctxt.vf_num = 0;
  10682. ctxt.uplink_seid = vsi->uplink_seid;
  10683. ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
  10684. ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
  10685. /* This VSI is connected to VEB so the switch_id
  10686. * should be set to zero by default.
  10687. */
  10688. if (i40e_is_vsi_uplink_mode_veb(vsi)) {
  10689. ctxt.info.valid_sections |=
  10690. cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  10691. ctxt.info.switch_id =
  10692. cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  10693. }
  10694. /* Setup the VSI tx/rx queue map for TC0 only for now */
  10695. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
  10696. break;
  10697. case I40E_VSI_SRIOV:
  10698. ctxt.pf_num = hw->pf_id;
  10699. ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
  10700. ctxt.uplink_seid = vsi->uplink_seid;
  10701. ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
  10702. ctxt.flags = I40E_AQ_VSI_TYPE_VF;
  10703. /* This VSI is connected to VEB so the switch_id
  10704. * should be set to zero by default.
  10705. */
  10706. if (i40e_is_vsi_uplink_mode_veb(vsi)) {
  10707. ctxt.info.valid_sections |=
  10708. cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  10709. ctxt.info.switch_id =
  10710. cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  10711. }
  10712. if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
  10713. ctxt.info.valid_sections |=
  10714. cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
  10715. ctxt.info.queueing_opt_flags |=
  10716. (I40E_AQ_VSI_QUE_OPT_TCP_ENA |
  10717. I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI);
  10718. }
  10719. ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
  10720. ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
  10721. if (pf->vf[vsi->vf_id].spoofchk) {
  10722. ctxt.info.valid_sections |=
  10723. cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
  10724. ctxt.info.sec_flags |=
  10725. (I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
  10726. I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
  10727. }
  10728. /* Setup the VSI tx/rx queue map for TC0 only for now */
  10729. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
  10730. break;
  10731. case I40E_VSI_IWARP:
  10732. /* send down message to iWARP */
  10733. break;
  10734. default:
  10735. return -ENODEV;
  10736. }
  10737. if (vsi->type != I40E_VSI_MAIN) {
  10738. ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
  10739. if (ret) {
  10740. dev_info(&vsi->back->pdev->dev,
  10741. "add vsi failed, err %s aq_err %s\n",
  10742. i40e_stat_str(&pf->hw, ret),
  10743. i40e_aq_str(&pf->hw,
  10744. pf->hw.aq.asq_last_status));
  10745. ret = -ENOENT;
  10746. goto err;
  10747. }
  10748. vsi->info = ctxt.info;
  10749. vsi->info.valid_sections = 0;
  10750. vsi->seid = ctxt.seid;
  10751. vsi->id = ctxt.vsi_number;
  10752. }
  10753. vsi->active_filters = 0;
  10754. clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
  10755. spin_lock_bh(&vsi->mac_filter_hash_lock);
  10756. /* If macvlan filters already exist, force them to get loaded */
  10757. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  10758. f->state = I40E_FILTER_NEW;
  10759. f_count++;
  10760. }
  10761. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  10762. if (f_count) {
  10763. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  10764. set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
  10765. }
  10766. /* Update VSI BW information */
  10767. ret = i40e_vsi_get_bw_info(vsi);
  10768. if (ret) {
  10769. dev_info(&pf->pdev->dev,
  10770. "couldn't get vsi bw info, err %s aq_err %s\n",
  10771. i40e_stat_str(&pf->hw, ret),
  10772. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  10773. /* VSI is already added so not tearing that up */
  10774. ret = 0;
  10775. }
  10776. err:
  10777. return ret;
  10778. }
  10779. /**
  10780. * i40e_vsi_release - Delete a VSI and free its resources
  10781. * @vsi: the VSI being removed
  10782. *
  10783. * Returns 0 on success or < 0 on error
  10784. **/
  10785. int i40e_vsi_release(struct i40e_vsi *vsi)
  10786. {
  10787. struct i40e_mac_filter *f;
  10788. struct hlist_node *h;
  10789. struct i40e_veb *veb = NULL;
  10790. struct i40e_pf *pf;
  10791. u16 uplink_seid;
  10792. int i, n, bkt;
  10793. pf = vsi->back;
  10794. /* release of a VEB-owner or last VSI is not allowed */
  10795. if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
  10796. dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
  10797. vsi->seid, vsi->uplink_seid);
  10798. return -ENODEV;
  10799. }
  10800. if (vsi == pf->vsi[pf->lan_vsi] &&
  10801. !test_bit(__I40E_DOWN, pf->state)) {
  10802. dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
  10803. return -ENODEV;
  10804. }
  10805. uplink_seid = vsi->uplink_seid;
  10806. if (vsi->type != I40E_VSI_SRIOV) {
  10807. if (vsi->netdev_registered) {
  10808. vsi->netdev_registered = false;
  10809. if (vsi->netdev) {
  10810. /* results in a call to i40e_close() */
  10811. unregister_netdev(vsi->netdev);
  10812. }
  10813. } else {
  10814. i40e_vsi_close(vsi);
  10815. }
  10816. i40e_vsi_disable_irq(vsi);
  10817. }
  10818. spin_lock_bh(&vsi->mac_filter_hash_lock);
  10819. /* clear the sync flag on all filters */
  10820. if (vsi->netdev) {
  10821. __dev_uc_unsync(vsi->netdev, NULL);
  10822. __dev_mc_unsync(vsi->netdev, NULL);
  10823. }
  10824. /* make sure any remaining filters are marked for deletion */
  10825. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
  10826. __i40e_del_filter(vsi, f);
  10827. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  10828. i40e_sync_vsi_filters(vsi);
  10829. i40e_vsi_delete(vsi);
  10830. i40e_vsi_free_q_vectors(vsi);
  10831. if (vsi->netdev) {
  10832. free_netdev(vsi->netdev);
  10833. vsi->netdev = NULL;
  10834. }
  10835. i40e_vsi_clear_rings(vsi);
  10836. i40e_vsi_clear(vsi);
  10837. /* If this was the last thing on the VEB, except for the
  10838. * controlling VSI, remove the VEB, which puts the controlling
  10839. * VSI onto the next level down in the switch.
  10840. *
  10841. * Well, okay, there's one more exception here: don't remove
  10842. * the orphan VEBs yet. We'll wait for an explicit remove request
  10843. * from up the network stack.
  10844. */
  10845. for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
  10846. if (pf->vsi[i] &&
  10847. pf->vsi[i]->uplink_seid == uplink_seid &&
  10848. (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
  10849. n++; /* count the VSIs */
  10850. }
  10851. }
  10852. for (i = 0; i < I40E_MAX_VEB; i++) {
  10853. if (!pf->veb[i])
  10854. continue;
  10855. if (pf->veb[i]->uplink_seid == uplink_seid)
  10856. n++; /* count the VEBs */
  10857. if (pf->veb[i]->seid == uplink_seid)
  10858. veb = pf->veb[i];
  10859. }
  10860. if (n == 0 && veb && veb->uplink_seid != 0)
  10861. i40e_veb_release(veb);
  10862. return 0;
  10863. }
  10864. /**
  10865. * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
  10866. * @vsi: ptr to the VSI
  10867. *
  10868. * This should only be called after i40e_vsi_mem_alloc() which allocates the
  10869. * corresponding SW VSI structure and initializes num_queue_pairs for the
  10870. * newly allocated VSI.
  10871. *
  10872. * Returns 0 on success or negative on failure
  10873. **/
  10874. static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
  10875. {
  10876. int ret = -ENOENT;
  10877. struct i40e_pf *pf = vsi->back;
  10878. if (vsi->q_vectors[0]) {
  10879. dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
  10880. vsi->seid);
  10881. return -EEXIST;
  10882. }
  10883. if (vsi->base_vector) {
  10884. dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
  10885. vsi->seid, vsi->base_vector);
  10886. return -EEXIST;
  10887. }
  10888. ret = i40e_vsi_alloc_q_vectors(vsi);
  10889. if (ret) {
  10890. dev_info(&pf->pdev->dev,
  10891. "failed to allocate %d q_vector for VSI %d, ret=%d\n",
  10892. vsi->num_q_vectors, vsi->seid, ret);
  10893. vsi->num_q_vectors = 0;
  10894. goto vector_setup_out;
  10895. }
  10896. /* In Legacy mode, we do not have to get any other vector since we
  10897. * piggyback on the misc/ICR0 for queue interrupts.
  10898. */
  10899. if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
  10900. return ret;
  10901. if (vsi->num_q_vectors)
  10902. vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
  10903. vsi->num_q_vectors, vsi->idx);
  10904. if (vsi->base_vector < 0) {
  10905. dev_info(&pf->pdev->dev,
  10906. "failed to get tracking for %d vectors for VSI %d, err=%d\n",
  10907. vsi->num_q_vectors, vsi->seid, vsi->base_vector);
  10908. i40e_vsi_free_q_vectors(vsi);
  10909. ret = -ENOENT;
  10910. goto vector_setup_out;
  10911. }
  10912. vector_setup_out:
  10913. return ret;
  10914. }
  10915. /**
  10916. * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
  10917. * @vsi: pointer to the vsi.
  10918. *
  10919. * This re-allocates a vsi's queue resources.
  10920. *
  10921. * Returns pointer to the successfully allocated and configured VSI sw struct
  10922. * on success, otherwise returns NULL on failure.
  10923. **/
  10924. static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
  10925. {
  10926. u16 alloc_queue_pairs;
  10927. struct i40e_pf *pf;
  10928. u8 enabled_tc;
  10929. int ret;
  10930. if (!vsi)
  10931. return NULL;
  10932. pf = vsi->back;
  10933. i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
  10934. i40e_vsi_clear_rings(vsi);
  10935. i40e_vsi_free_arrays(vsi, false);
  10936. i40e_set_num_rings_in_vsi(vsi);
  10937. ret = i40e_vsi_alloc_arrays(vsi, false);
  10938. if (ret)
  10939. goto err_vsi;
  10940. alloc_queue_pairs = vsi->alloc_queue_pairs *
  10941. (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
  10942. ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
  10943. if (ret < 0) {
  10944. dev_info(&pf->pdev->dev,
  10945. "failed to get tracking for %d queues for VSI %d err %d\n",
  10946. alloc_queue_pairs, vsi->seid, ret);
  10947. goto err_vsi;
  10948. }
  10949. vsi->base_queue = ret;
  10950. /* Update the FW view of the VSI. Force a reset of TC and queue
  10951. * layout configurations.
  10952. */
  10953. enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
  10954. pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
  10955. pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
  10956. i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
  10957. if (vsi->type == I40E_VSI_MAIN)
  10958. i40e_rm_default_mac_filter(vsi, pf->hw.mac.perm_addr);
  10959. /* assign it some queues */
  10960. ret = i40e_alloc_rings(vsi);
  10961. if (ret)
  10962. goto err_rings;
  10963. /* map all of the rings to the q_vectors */
  10964. i40e_vsi_map_rings_to_vectors(vsi);
  10965. return vsi;
  10966. err_rings:
  10967. i40e_vsi_free_q_vectors(vsi);
  10968. if (vsi->netdev_registered) {
  10969. vsi->netdev_registered = false;
  10970. unregister_netdev(vsi->netdev);
  10971. free_netdev(vsi->netdev);
  10972. vsi->netdev = NULL;
  10973. }
  10974. i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
  10975. err_vsi:
  10976. i40e_vsi_clear(vsi);
  10977. return NULL;
  10978. }
  10979. /**
  10980. * i40e_vsi_setup - Set up a VSI by a given type
  10981. * @pf: board private structure
  10982. * @type: VSI type
  10983. * @uplink_seid: the switch element to link to
  10984. * @param1: usage depends upon VSI type. For VF types, indicates VF id
  10985. *
  10986. * This allocates the sw VSI structure and its queue resources, then add a VSI
  10987. * to the identified VEB.
  10988. *
  10989. * Returns pointer to the successfully allocated and configure VSI sw struct on
  10990. * success, otherwise returns NULL on failure.
  10991. **/
  10992. struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
  10993. u16 uplink_seid, u32 param1)
  10994. {
  10995. struct i40e_vsi *vsi = NULL;
  10996. struct i40e_veb *veb = NULL;
  10997. u16 alloc_queue_pairs;
  10998. int ret, i;
  10999. int v_idx;
  11000. /* The requested uplink_seid must be either
  11001. * - the PF's port seid
  11002. * no VEB is needed because this is the PF
  11003. * or this is a Flow Director special case VSI
  11004. * - seid of an existing VEB
  11005. * - seid of a VSI that owns an existing VEB
  11006. * - seid of a VSI that doesn't own a VEB
  11007. * a new VEB is created and the VSI becomes the owner
  11008. * - seid of the PF VSI, which is what creates the first VEB
  11009. * this is a special case of the previous
  11010. *
  11011. * Find which uplink_seid we were given and create a new VEB if needed
  11012. */
  11013. for (i = 0; i < I40E_MAX_VEB; i++) {
  11014. if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
  11015. veb = pf->veb[i];
  11016. break;
  11017. }
  11018. }
  11019. if (!veb && uplink_seid != pf->mac_seid) {
  11020. for (i = 0; i < pf->num_alloc_vsi; i++) {
  11021. if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
  11022. vsi = pf->vsi[i];
  11023. break;
  11024. }
  11025. }
  11026. if (!vsi) {
  11027. dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
  11028. uplink_seid);
  11029. return NULL;
  11030. }
  11031. if (vsi->uplink_seid == pf->mac_seid)
  11032. veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
  11033. vsi->tc_config.enabled_tc);
  11034. else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
  11035. veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
  11036. vsi->tc_config.enabled_tc);
  11037. if (veb) {
  11038. if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
  11039. dev_info(&vsi->back->pdev->dev,
  11040. "New VSI creation error, uplink seid of LAN VSI expected.\n");
  11041. return NULL;
  11042. }
  11043. /* We come up by default in VEPA mode if SRIOV is not
  11044. * already enabled, in which case we can't force VEPA
  11045. * mode.
  11046. */
  11047. if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
  11048. veb->bridge_mode = BRIDGE_MODE_VEPA;
  11049. pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
  11050. }
  11051. i40e_config_bridge_mode(veb);
  11052. }
  11053. for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
  11054. if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
  11055. veb = pf->veb[i];
  11056. }
  11057. if (!veb) {
  11058. dev_info(&pf->pdev->dev, "couldn't add VEB\n");
  11059. return NULL;
  11060. }
  11061. vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
  11062. uplink_seid = veb->seid;
  11063. }
  11064. /* get vsi sw struct */
  11065. v_idx = i40e_vsi_mem_alloc(pf, type);
  11066. if (v_idx < 0)
  11067. goto err_alloc;
  11068. vsi = pf->vsi[v_idx];
  11069. if (!vsi)
  11070. goto err_alloc;
  11071. vsi->type = type;
  11072. vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
  11073. if (type == I40E_VSI_MAIN)
  11074. pf->lan_vsi = v_idx;
  11075. else if (type == I40E_VSI_SRIOV)
  11076. vsi->vf_id = param1;
  11077. /* assign it some queues */
  11078. alloc_queue_pairs = vsi->alloc_queue_pairs *
  11079. (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
  11080. ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
  11081. if (ret < 0) {
  11082. dev_info(&pf->pdev->dev,
  11083. "failed to get tracking for %d queues for VSI %d err=%d\n",
  11084. alloc_queue_pairs, vsi->seid, ret);
  11085. goto err_vsi;
  11086. }
  11087. vsi->base_queue = ret;
  11088. /* get a VSI from the hardware */
  11089. vsi->uplink_seid = uplink_seid;
  11090. ret = i40e_add_vsi(vsi);
  11091. if (ret)
  11092. goto err_vsi;
  11093. switch (vsi->type) {
  11094. /* setup the netdev if needed */
  11095. case I40E_VSI_MAIN:
  11096. case I40E_VSI_VMDQ2:
  11097. ret = i40e_config_netdev(vsi);
  11098. if (ret)
  11099. goto err_netdev;
  11100. ret = register_netdev(vsi->netdev);
  11101. if (ret)
  11102. goto err_netdev;
  11103. vsi->netdev_registered = true;
  11104. netif_carrier_off(vsi->netdev);
  11105. #ifdef CONFIG_I40E_DCB
  11106. /* Setup DCB netlink interface */
  11107. i40e_dcbnl_setup(vsi);
  11108. #endif /* CONFIG_I40E_DCB */
  11109. /* fall through */
  11110. case I40E_VSI_FDIR:
  11111. /* set up vectors and rings if needed */
  11112. ret = i40e_vsi_setup_vectors(vsi);
  11113. if (ret)
  11114. goto err_msix;
  11115. ret = i40e_alloc_rings(vsi);
  11116. if (ret)
  11117. goto err_rings;
  11118. /* map all of the rings to the q_vectors */
  11119. i40e_vsi_map_rings_to_vectors(vsi);
  11120. i40e_vsi_reset_stats(vsi);
  11121. break;
  11122. default:
  11123. /* no netdev or rings for the other VSI types */
  11124. break;
  11125. }
  11126. if ((pf->hw_features & I40E_HW_RSS_AQ_CAPABLE) &&
  11127. (vsi->type == I40E_VSI_VMDQ2)) {
  11128. ret = i40e_vsi_config_rss(vsi);
  11129. }
  11130. return vsi;
  11131. err_rings:
  11132. i40e_vsi_free_q_vectors(vsi);
  11133. err_msix:
  11134. if (vsi->netdev_registered) {
  11135. vsi->netdev_registered = false;
  11136. unregister_netdev(vsi->netdev);
  11137. free_netdev(vsi->netdev);
  11138. vsi->netdev = NULL;
  11139. }
  11140. err_netdev:
  11141. i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
  11142. err_vsi:
  11143. i40e_vsi_clear(vsi);
  11144. err_alloc:
  11145. return NULL;
  11146. }
  11147. /**
  11148. * i40e_veb_get_bw_info - Query VEB BW information
  11149. * @veb: the veb to query
  11150. *
  11151. * Query the Tx scheduler BW configuration data for given VEB
  11152. **/
  11153. static int i40e_veb_get_bw_info(struct i40e_veb *veb)
  11154. {
  11155. struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
  11156. struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
  11157. struct i40e_pf *pf = veb->pf;
  11158. struct i40e_hw *hw = &pf->hw;
  11159. u32 tc_bw_max;
  11160. int ret = 0;
  11161. int i;
  11162. ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
  11163. &bw_data, NULL);
  11164. if (ret) {
  11165. dev_info(&pf->pdev->dev,
  11166. "query veb bw config failed, err %s aq_err %s\n",
  11167. i40e_stat_str(&pf->hw, ret),
  11168. i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
  11169. goto out;
  11170. }
  11171. ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
  11172. &ets_data, NULL);
  11173. if (ret) {
  11174. dev_info(&pf->pdev->dev,
  11175. "query veb bw ets config failed, err %s aq_err %s\n",
  11176. i40e_stat_str(&pf->hw, ret),
  11177. i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
  11178. goto out;
  11179. }
  11180. veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
  11181. veb->bw_max_quanta = ets_data.tc_bw_max;
  11182. veb->is_abs_credits = bw_data.absolute_credits_enable;
  11183. veb->enabled_tc = ets_data.tc_valid_bits;
  11184. tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
  11185. (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
  11186. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  11187. veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
  11188. veb->bw_tc_limit_credits[i] =
  11189. le16_to_cpu(bw_data.tc_bw_limits[i]);
  11190. veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
  11191. }
  11192. out:
  11193. return ret;
  11194. }
  11195. /**
  11196. * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
  11197. * @pf: board private structure
  11198. *
  11199. * On error: returns error code (negative)
  11200. * On success: returns vsi index in PF (positive)
  11201. **/
  11202. static int i40e_veb_mem_alloc(struct i40e_pf *pf)
  11203. {
  11204. int ret = -ENOENT;
  11205. struct i40e_veb *veb;
  11206. int i;
  11207. /* Need to protect the allocation of switch elements at the PF level */
  11208. mutex_lock(&pf->switch_mutex);
  11209. /* VEB list may be fragmented if VEB creation/destruction has
  11210. * been happening. We can afford to do a quick scan to look
  11211. * for any free slots in the list.
  11212. *
  11213. * find next empty veb slot, looping back around if necessary
  11214. */
  11215. i = 0;
  11216. while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
  11217. i++;
  11218. if (i >= I40E_MAX_VEB) {
  11219. ret = -ENOMEM;
  11220. goto err_alloc_veb; /* out of VEB slots! */
  11221. }
  11222. veb = kzalloc(sizeof(*veb), GFP_KERNEL);
  11223. if (!veb) {
  11224. ret = -ENOMEM;
  11225. goto err_alloc_veb;
  11226. }
  11227. veb->pf = pf;
  11228. veb->idx = i;
  11229. veb->enabled_tc = 1;
  11230. pf->veb[i] = veb;
  11231. ret = i;
  11232. err_alloc_veb:
  11233. mutex_unlock(&pf->switch_mutex);
  11234. return ret;
  11235. }
  11236. /**
  11237. * i40e_switch_branch_release - Delete a branch of the switch tree
  11238. * @branch: where to start deleting
  11239. *
  11240. * This uses recursion to find the tips of the branch to be
  11241. * removed, deleting until we get back to and can delete this VEB.
  11242. **/
  11243. static void i40e_switch_branch_release(struct i40e_veb *branch)
  11244. {
  11245. struct i40e_pf *pf = branch->pf;
  11246. u16 branch_seid = branch->seid;
  11247. u16 veb_idx = branch->idx;
  11248. int i;
  11249. /* release any VEBs on this VEB - RECURSION */
  11250. for (i = 0; i < I40E_MAX_VEB; i++) {
  11251. if (!pf->veb[i])
  11252. continue;
  11253. if (pf->veb[i]->uplink_seid == branch->seid)
  11254. i40e_switch_branch_release(pf->veb[i]);
  11255. }
  11256. /* Release the VSIs on this VEB, but not the owner VSI.
  11257. *
  11258. * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
  11259. * the VEB itself, so don't use (*branch) after this loop.
  11260. */
  11261. for (i = 0; i < pf->num_alloc_vsi; i++) {
  11262. if (!pf->vsi[i])
  11263. continue;
  11264. if (pf->vsi[i]->uplink_seid == branch_seid &&
  11265. (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
  11266. i40e_vsi_release(pf->vsi[i]);
  11267. }
  11268. }
  11269. /* There's one corner case where the VEB might not have been
  11270. * removed, so double check it here and remove it if needed.
  11271. * This case happens if the veb was created from the debugfs
  11272. * commands and no VSIs were added to it.
  11273. */
  11274. if (pf->veb[veb_idx])
  11275. i40e_veb_release(pf->veb[veb_idx]);
  11276. }
  11277. /**
  11278. * i40e_veb_clear - remove veb struct
  11279. * @veb: the veb to remove
  11280. **/
  11281. static void i40e_veb_clear(struct i40e_veb *veb)
  11282. {
  11283. if (!veb)
  11284. return;
  11285. if (veb->pf) {
  11286. struct i40e_pf *pf = veb->pf;
  11287. mutex_lock(&pf->switch_mutex);
  11288. if (pf->veb[veb->idx] == veb)
  11289. pf->veb[veb->idx] = NULL;
  11290. mutex_unlock(&pf->switch_mutex);
  11291. }
  11292. kfree(veb);
  11293. }
  11294. /**
  11295. * i40e_veb_release - Delete a VEB and free its resources
  11296. * @veb: the VEB being removed
  11297. **/
  11298. void i40e_veb_release(struct i40e_veb *veb)
  11299. {
  11300. struct i40e_vsi *vsi = NULL;
  11301. struct i40e_pf *pf;
  11302. int i, n = 0;
  11303. pf = veb->pf;
  11304. /* find the remaining VSI and check for extras */
  11305. for (i = 0; i < pf->num_alloc_vsi; i++) {
  11306. if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
  11307. n++;
  11308. vsi = pf->vsi[i];
  11309. }
  11310. }
  11311. if (n != 1) {
  11312. dev_info(&pf->pdev->dev,
  11313. "can't remove VEB %d with %d VSIs left\n",
  11314. veb->seid, n);
  11315. return;
  11316. }
  11317. /* move the remaining VSI to uplink veb */
  11318. vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
  11319. if (veb->uplink_seid) {
  11320. vsi->uplink_seid = veb->uplink_seid;
  11321. if (veb->uplink_seid == pf->mac_seid)
  11322. vsi->veb_idx = I40E_NO_VEB;
  11323. else
  11324. vsi->veb_idx = veb->veb_idx;
  11325. } else {
  11326. /* floating VEB */
  11327. vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
  11328. vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
  11329. }
  11330. i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
  11331. i40e_veb_clear(veb);
  11332. }
  11333. /**
  11334. * i40e_add_veb - create the VEB in the switch
  11335. * @veb: the VEB to be instantiated
  11336. * @vsi: the controlling VSI
  11337. **/
  11338. static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
  11339. {
  11340. struct i40e_pf *pf = veb->pf;
  11341. bool enable_stats = !!(pf->flags & I40E_FLAG_VEB_STATS_ENABLED);
  11342. int ret;
  11343. ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi->seid,
  11344. veb->enabled_tc, false,
  11345. &veb->seid, enable_stats, NULL);
  11346. /* get a VEB from the hardware */
  11347. if (ret) {
  11348. dev_info(&pf->pdev->dev,
  11349. "couldn't add VEB, err %s aq_err %s\n",
  11350. i40e_stat_str(&pf->hw, ret),
  11351. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  11352. return -EPERM;
  11353. }
  11354. /* get statistics counter */
  11355. ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
  11356. &veb->stats_idx, NULL, NULL, NULL);
  11357. if (ret) {
  11358. dev_info(&pf->pdev->dev,
  11359. "couldn't get VEB statistics idx, err %s aq_err %s\n",
  11360. i40e_stat_str(&pf->hw, ret),
  11361. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  11362. return -EPERM;
  11363. }
  11364. ret = i40e_veb_get_bw_info(veb);
  11365. if (ret) {
  11366. dev_info(&pf->pdev->dev,
  11367. "couldn't get VEB bw info, err %s aq_err %s\n",
  11368. i40e_stat_str(&pf->hw, ret),
  11369. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  11370. i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
  11371. return -ENOENT;
  11372. }
  11373. vsi->uplink_seid = veb->seid;
  11374. vsi->veb_idx = veb->idx;
  11375. vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
  11376. return 0;
  11377. }
  11378. /**
  11379. * i40e_veb_setup - Set up a VEB
  11380. * @pf: board private structure
  11381. * @flags: VEB setup flags
  11382. * @uplink_seid: the switch element to link to
  11383. * @vsi_seid: the initial VSI seid
  11384. * @enabled_tc: Enabled TC bit-map
  11385. *
  11386. * This allocates the sw VEB structure and links it into the switch
  11387. * It is possible and legal for this to be a duplicate of an already
  11388. * existing VEB. It is also possible for both uplink and vsi seids
  11389. * to be zero, in order to create a floating VEB.
  11390. *
  11391. * Returns pointer to the successfully allocated VEB sw struct on
  11392. * success, otherwise returns NULL on failure.
  11393. **/
  11394. struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
  11395. u16 uplink_seid, u16 vsi_seid,
  11396. u8 enabled_tc)
  11397. {
  11398. struct i40e_veb *veb, *uplink_veb = NULL;
  11399. int vsi_idx, veb_idx;
  11400. int ret;
  11401. /* if one seid is 0, the other must be 0 to create a floating relay */
  11402. if ((uplink_seid == 0 || vsi_seid == 0) &&
  11403. (uplink_seid + vsi_seid != 0)) {
  11404. dev_info(&pf->pdev->dev,
  11405. "one, not both seid's are 0: uplink=%d vsi=%d\n",
  11406. uplink_seid, vsi_seid);
  11407. return NULL;
  11408. }
  11409. /* make sure there is such a vsi and uplink */
  11410. for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
  11411. if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
  11412. break;
  11413. if (vsi_idx >= pf->num_alloc_vsi && vsi_seid != 0) {
  11414. dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
  11415. vsi_seid);
  11416. return NULL;
  11417. }
  11418. if (uplink_seid && uplink_seid != pf->mac_seid) {
  11419. for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
  11420. if (pf->veb[veb_idx] &&
  11421. pf->veb[veb_idx]->seid == uplink_seid) {
  11422. uplink_veb = pf->veb[veb_idx];
  11423. break;
  11424. }
  11425. }
  11426. if (!uplink_veb) {
  11427. dev_info(&pf->pdev->dev,
  11428. "uplink seid %d not found\n", uplink_seid);
  11429. return NULL;
  11430. }
  11431. }
  11432. /* get veb sw struct */
  11433. veb_idx = i40e_veb_mem_alloc(pf);
  11434. if (veb_idx < 0)
  11435. goto err_alloc;
  11436. veb = pf->veb[veb_idx];
  11437. veb->flags = flags;
  11438. veb->uplink_seid = uplink_seid;
  11439. veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
  11440. veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
  11441. /* create the VEB in the switch */
  11442. ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
  11443. if (ret)
  11444. goto err_veb;
  11445. if (vsi_idx == pf->lan_vsi)
  11446. pf->lan_veb = veb->idx;
  11447. return veb;
  11448. err_veb:
  11449. i40e_veb_clear(veb);
  11450. err_alloc:
  11451. return NULL;
  11452. }
  11453. /**
  11454. * i40e_setup_pf_switch_element - set PF vars based on switch type
  11455. * @pf: board private structure
  11456. * @ele: element we are building info from
  11457. * @num_reported: total number of elements
  11458. * @printconfig: should we print the contents
  11459. *
  11460. * helper function to assist in extracting a few useful SEID values.
  11461. **/
  11462. static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
  11463. struct i40e_aqc_switch_config_element_resp *ele,
  11464. u16 num_reported, bool printconfig)
  11465. {
  11466. u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
  11467. u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
  11468. u8 element_type = ele->element_type;
  11469. u16 seid = le16_to_cpu(ele->seid);
  11470. if (printconfig)
  11471. dev_info(&pf->pdev->dev,
  11472. "type=%d seid=%d uplink=%d downlink=%d\n",
  11473. element_type, seid, uplink_seid, downlink_seid);
  11474. switch (element_type) {
  11475. case I40E_SWITCH_ELEMENT_TYPE_MAC:
  11476. pf->mac_seid = seid;
  11477. break;
  11478. case I40E_SWITCH_ELEMENT_TYPE_VEB:
  11479. /* Main VEB? */
  11480. if (uplink_seid != pf->mac_seid)
  11481. break;
  11482. if (pf->lan_veb == I40E_NO_VEB) {
  11483. int v;
  11484. /* find existing or else empty VEB */
  11485. for (v = 0; v < I40E_MAX_VEB; v++) {
  11486. if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
  11487. pf->lan_veb = v;
  11488. break;
  11489. }
  11490. }
  11491. if (pf->lan_veb == I40E_NO_VEB) {
  11492. v = i40e_veb_mem_alloc(pf);
  11493. if (v < 0)
  11494. break;
  11495. pf->lan_veb = v;
  11496. }
  11497. }
  11498. pf->veb[pf->lan_veb]->seid = seid;
  11499. pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
  11500. pf->veb[pf->lan_veb]->pf = pf;
  11501. pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
  11502. break;
  11503. case I40E_SWITCH_ELEMENT_TYPE_VSI:
  11504. if (num_reported != 1)
  11505. break;
  11506. /* This is immediately after a reset so we can assume this is
  11507. * the PF's VSI
  11508. */
  11509. pf->mac_seid = uplink_seid;
  11510. pf->pf_seid = downlink_seid;
  11511. pf->main_vsi_seid = seid;
  11512. if (printconfig)
  11513. dev_info(&pf->pdev->dev,
  11514. "pf_seid=%d main_vsi_seid=%d\n",
  11515. pf->pf_seid, pf->main_vsi_seid);
  11516. break;
  11517. case I40E_SWITCH_ELEMENT_TYPE_PF:
  11518. case I40E_SWITCH_ELEMENT_TYPE_VF:
  11519. case I40E_SWITCH_ELEMENT_TYPE_EMP:
  11520. case I40E_SWITCH_ELEMENT_TYPE_BMC:
  11521. case I40E_SWITCH_ELEMENT_TYPE_PE:
  11522. case I40E_SWITCH_ELEMENT_TYPE_PA:
  11523. /* ignore these for now */
  11524. break;
  11525. default:
  11526. dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
  11527. element_type, seid);
  11528. break;
  11529. }
  11530. }
  11531. /**
  11532. * i40e_fetch_switch_configuration - Get switch config from firmware
  11533. * @pf: board private structure
  11534. * @printconfig: should we print the contents
  11535. *
  11536. * Get the current switch configuration from the device and
  11537. * extract a few useful SEID values.
  11538. **/
  11539. int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
  11540. {
  11541. struct i40e_aqc_get_switch_config_resp *sw_config;
  11542. u16 next_seid = 0;
  11543. int ret = 0;
  11544. u8 *aq_buf;
  11545. int i;
  11546. aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
  11547. if (!aq_buf)
  11548. return -ENOMEM;
  11549. sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
  11550. do {
  11551. u16 num_reported, num_total;
  11552. ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
  11553. I40E_AQ_LARGE_BUF,
  11554. &next_seid, NULL);
  11555. if (ret) {
  11556. dev_info(&pf->pdev->dev,
  11557. "get switch config failed err %s aq_err %s\n",
  11558. i40e_stat_str(&pf->hw, ret),
  11559. i40e_aq_str(&pf->hw,
  11560. pf->hw.aq.asq_last_status));
  11561. kfree(aq_buf);
  11562. return -ENOENT;
  11563. }
  11564. num_reported = le16_to_cpu(sw_config->header.num_reported);
  11565. num_total = le16_to_cpu(sw_config->header.num_total);
  11566. if (printconfig)
  11567. dev_info(&pf->pdev->dev,
  11568. "header: %d reported %d total\n",
  11569. num_reported, num_total);
  11570. for (i = 0; i < num_reported; i++) {
  11571. struct i40e_aqc_switch_config_element_resp *ele =
  11572. &sw_config->element[i];
  11573. i40e_setup_pf_switch_element(pf, ele, num_reported,
  11574. printconfig);
  11575. }
  11576. } while (next_seid != 0);
  11577. kfree(aq_buf);
  11578. return ret;
  11579. }
  11580. /**
  11581. * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
  11582. * @pf: board private structure
  11583. * @reinit: if the Main VSI needs to re-initialized.
  11584. *
  11585. * Returns 0 on success, negative value on failure
  11586. **/
  11587. static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit)
  11588. {
  11589. u16 flags = 0;
  11590. int ret;
  11591. /* find out what's out there already */
  11592. ret = i40e_fetch_switch_configuration(pf, false);
  11593. if (ret) {
  11594. dev_info(&pf->pdev->dev,
  11595. "couldn't fetch switch config, err %s aq_err %s\n",
  11596. i40e_stat_str(&pf->hw, ret),
  11597. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  11598. return ret;
  11599. }
  11600. i40e_pf_reset_stats(pf);
  11601. /* set the switch config bit for the whole device to
  11602. * support limited promisc or true promisc
  11603. * when user requests promisc. The default is limited
  11604. * promisc.
  11605. */
  11606. if ((pf->hw.pf_id == 0) &&
  11607. !(pf->flags & I40E_FLAG_TRUE_PROMISC_SUPPORT)) {
  11608. flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
  11609. pf->last_sw_conf_flags = flags;
  11610. }
  11611. if (pf->hw.pf_id == 0) {
  11612. u16 valid_flags;
  11613. valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
  11614. ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags, 0,
  11615. NULL);
  11616. if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) {
  11617. dev_info(&pf->pdev->dev,
  11618. "couldn't set switch config bits, err %s aq_err %s\n",
  11619. i40e_stat_str(&pf->hw, ret),
  11620. i40e_aq_str(&pf->hw,
  11621. pf->hw.aq.asq_last_status));
  11622. /* not a fatal problem, just keep going */
  11623. }
  11624. pf->last_sw_conf_valid_flags = valid_flags;
  11625. }
  11626. /* first time setup */
  11627. if (pf->lan_vsi == I40E_NO_VSI || reinit) {
  11628. struct i40e_vsi *vsi = NULL;
  11629. u16 uplink_seid;
  11630. /* Set up the PF VSI associated with the PF's main VSI
  11631. * that is already in the HW switch
  11632. */
  11633. if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
  11634. uplink_seid = pf->veb[pf->lan_veb]->seid;
  11635. else
  11636. uplink_seid = pf->mac_seid;
  11637. if (pf->lan_vsi == I40E_NO_VSI)
  11638. vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
  11639. else if (reinit)
  11640. vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
  11641. if (!vsi) {
  11642. dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
  11643. i40e_cloud_filter_exit(pf);
  11644. i40e_fdir_teardown(pf);
  11645. return -EAGAIN;
  11646. }
  11647. } else {
  11648. /* force a reset of TC and queue layout configurations */
  11649. u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
  11650. pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
  11651. pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
  11652. i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
  11653. }
  11654. i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
  11655. i40e_fdir_sb_setup(pf);
  11656. /* Setup static PF queue filter control settings */
  11657. ret = i40e_setup_pf_filter_control(pf);
  11658. if (ret) {
  11659. dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
  11660. ret);
  11661. /* Failure here should not stop continuing other steps */
  11662. }
  11663. /* enable RSS in the HW, even for only one queue, as the stack can use
  11664. * the hash
  11665. */
  11666. if ((pf->flags & I40E_FLAG_RSS_ENABLED))
  11667. i40e_pf_config_rss(pf);
  11668. /* fill in link information and enable LSE reporting */
  11669. i40e_link_event(pf);
  11670. /* Initialize user-specific link properties */
  11671. pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
  11672. I40E_AQ_AN_COMPLETED) ? true : false);
  11673. i40e_ptp_init(pf);
  11674. /* repopulate tunnel port filters */
  11675. i40e_sync_udp_filters(pf);
  11676. return ret;
  11677. }
  11678. /**
  11679. * i40e_determine_queue_usage - Work out queue distribution
  11680. * @pf: board private structure
  11681. **/
  11682. static void i40e_determine_queue_usage(struct i40e_pf *pf)
  11683. {
  11684. int queues_left;
  11685. int q_max;
  11686. pf->num_lan_qps = 0;
  11687. /* Find the max queues to be put into basic use. We'll always be
  11688. * using TC0, whether or not DCB is running, and TC0 will get the
  11689. * big RSS set.
  11690. */
  11691. queues_left = pf->hw.func_caps.num_tx_qp;
  11692. if ((queues_left == 1) ||
  11693. !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
  11694. /* one qp for PF, no queues for anything else */
  11695. queues_left = 0;
  11696. pf->alloc_rss_size = pf->num_lan_qps = 1;
  11697. /* make sure all the fancies are disabled */
  11698. pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
  11699. I40E_FLAG_IWARP_ENABLED |
  11700. I40E_FLAG_FD_SB_ENABLED |
  11701. I40E_FLAG_FD_ATR_ENABLED |
  11702. I40E_FLAG_DCB_CAPABLE |
  11703. I40E_FLAG_DCB_ENABLED |
  11704. I40E_FLAG_SRIOV_ENABLED |
  11705. I40E_FLAG_VMDQ_ENABLED);
  11706. pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
  11707. } else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
  11708. I40E_FLAG_FD_SB_ENABLED |
  11709. I40E_FLAG_FD_ATR_ENABLED |
  11710. I40E_FLAG_DCB_CAPABLE))) {
  11711. /* one qp for PF */
  11712. pf->alloc_rss_size = pf->num_lan_qps = 1;
  11713. queues_left -= pf->num_lan_qps;
  11714. pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
  11715. I40E_FLAG_IWARP_ENABLED |
  11716. I40E_FLAG_FD_SB_ENABLED |
  11717. I40E_FLAG_FD_ATR_ENABLED |
  11718. I40E_FLAG_DCB_ENABLED |
  11719. I40E_FLAG_VMDQ_ENABLED);
  11720. pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
  11721. } else {
  11722. /* Not enough queues for all TCs */
  11723. if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
  11724. (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
  11725. pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
  11726. I40E_FLAG_DCB_ENABLED);
  11727. dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
  11728. }
  11729. /* limit lan qps to the smaller of qps, cpus or msix */
  11730. q_max = max_t(int, pf->rss_size_max, num_online_cpus());
  11731. q_max = min_t(int, q_max, pf->hw.func_caps.num_tx_qp);
  11732. q_max = min_t(int, q_max, pf->hw.func_caps.num_msix_vectors);
  11733. pf->num_lan_qps = q_max;
  11734. queues_left -= pf->num_lan_qps;
  11735. }
  11736. if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
  11737. if (queues_left > 1) {
  11738. queues_left -= 1; /* save 1 queue for FD */
  11739. } else {
  11740. pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
  11741. pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
  11742. dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
  11743. }
  11744. }
  11745. if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
  11746. pf->num_vf_qps && pf->num_req_vfs && queues_left) {
  11747. pf->num_req_vfs = min_t(int, pf->num_req_vfs,
  11748. (queues_left / pf->num_vf_qps));
  11749. queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
  11750. }
  11751. if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
  11752. pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
  11753. pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
  11754. (queues_left / pf->num_vmdq_qps));
  11755. queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
  11756. }
  11757. pf->queues_left = queues_left;
  11758. dev_dbg(&pf->pdev->dev,
  11759. "qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
  11760. pf->hw.func_caps.num_tx_qp,
  11761. !!(pf->flags & I40E_FLAG_FD_SB_ENABLED),
  11762. pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
  11763. pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
  11764. queues_left);
  11765. }
  11766. /**
  11767. * i40e_setup_pf_filter_control - Setup PF static filter control
  11768. * @pf: PF to be setup
  11769. *
  11770. * i40e_setup_pf_filter_control sets up a PF's initial filter control
  11771. * settings. If PE/FCoE are enabled then it will also set the per PF
  11772. * based filter sizes required for them. It also enables Flow director,
  11773. * ethertype and macvlan type filter settings for the pf.
  11774. *
  11775. * Returns 0 on success, negative on failure
  11776. **/
  11777. static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
  11778. {
  11779. struct i40e_filter_control_settings *settings = &pf->filter_settings;
  11780. settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
  11781. /* Flow Director is enabled */
  11782. if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
  11783. settings->enable_fdir = true;
  11784. /* Ethtype and MACVLAN filters enabled for PF */
  11785. settings->enable_ethtype = true;
  11786. settings->enable_macvlan = true;
  11787. if (i40e_set_filter_control(&pf->hw, settings))
  11788. return -ENOENT;
  11789. return 0;
  11790. }
  11791. #define INFO_STRING_LEN 255
  11792. #define REMAIN(__x) (INFO_STRING_LEN - (__x))
  11793. static void i40e_print_features(struct i40e_pf *pf)
  11794. {
  11795. struct i40e_hw *hw = &pf->hw;
  11796. char *buf;
  11797. int i;
  11798. buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
  11799. if (!buf)
  11800. return;
  11801. i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
  11802. #ifdef CONFIG_PCI_IOV
  11803. i += snprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
  11804. #endif
  11805. i += snprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d",
  11806. pf->hw.func_caps.num_vsis,
  11807. pf->vsi[pf->lan_vsi]->num_queue_pairs);
  11808. if (pf->flags & I40E_FLAG_RSS_ENABLED)
  11809. i += snprintf(&buf[i], REMAIN(i), " RSS");
  11810. if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
  11811. i += snprintf(&buf[i], REMAIN(i), " FD_ATR");
  11812. if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
  11813. i += snprintf(&buf[i], REMAIN(i), " FD_SB");
  11814. i += snprintf(&buf[i], REMAIN(i), " NTUPLE");
  11815. }
  11816. if (pf->flags & I40E_FLAG_DCB_CAPABLE)
  11817. i += snprintf(&buf[i], REMAIN(i), " DCB");
  11818. i += snprintf(&buf[i], REMAIN(i), " VxLAN");
  11819. i += snprintf(&buf[i], REMAIN(i), " Geneve");
  11820. if (pf->flags & I40E_FLAG_PTP)
  11821. i += snprintf(&buf[i], REMAIN(i), " PTP");
  11822. if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
  11823. i += snprintf(&buf[i], REMAIN(i), " VEB");
  11824. else
  11825. i += snprintf(&buf[i], REMAIN(i), " VEPA");
  11826. dev_info(&pf->pdev->dev, "%s\n", buf);
  11827. kfree(buf);
  11828. WARN_ON(i > INFO_STRING_LEN);
  11829. }
  11830. /**
  11831. * i40e_get_platform_mac_addr - get platform-specific MAC address
  11832. * @pdev: PCI device information struct
  11833. * @pf: board private structure
  11834. *
  11835. * Look up the MAC address for the device. First we'll try
  11836. * eth_platform_get_mac_address, which will check Open Firmware, or arch
  11837. * specific fallback. Otherwise, we'll default to the stored value in
  11838. * firmware.
  11839. **/
  11840. static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
  11841. {
  11842. if (eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
  11843. i40e_get_mac_addr(&pf->hw, pf->hw.mac.addr);
  11844. }
  11845. /**
  11846. * i40e_probe - Device initialization routine
  11847. * @pdev: PCI device information struct
  11848. * @ent: entry in i40e_pci_tbl
  11849. *
  11850. * i40e_probe initializes a PF identified by a pci_dev structure.
  11851. * The OS initialization, configuring of the PF private structure,
  11852. * and a hardware reset occur.
  11853. *
  11854. * Returns 0 on success, negative on failure
  11855. **/
  11856. static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  11857. {
  11858. struct i40e_aq_get_phy_abilities_resp abilities;
  11859. struct i40e_pf *pf;
  11860. struct i40e_hw *hw;
  11861. static u16 pfs_found;
  11862. u16 wol_nvm_bits;
  11863. u16 link_status;
  11864. int err;
  11865. u32 val;
  11866. u32 i;
  11867. u8 set_fc_aq_fail;
  11868. err = pci_enable_device_mem(pdev);
  11869. if (err)
  11870. return err;
  11871. /* set up for high or low dma */
  11872. err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
  11873. if (err) {
  11874. err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  11875. if (err) {
  11876. dev_err(&pdev->dev,
  11877. "DMA configuration failed: 0x%x\n", err);
  11878. goto err_dma;
  11879. }
  11880. }
  11881. /* set up pci connections */
  11882. err = pci_request_mem_regions(pdev, i40e_driver_name);
  11883. if (err) {
  11884. dev_info(&pdev->dev,
  11885. "pci_request_selected_regions failed %d\n", err);
  11886. goto err_pci_reg;
  11887. }
  11888. pci_enable_pcie_error_reporting(pdev);
  11889. pci_set_master(pdev);
  11890. /* Now that we have a PCI connection, we need to do the
  11891. * low level device setup. This is primarily setting up
  11892. * the Admin Queue structures and then querying for the
  11893. * device's current profile information.
  11894. */
  11895. pf = kzalloc(sizeof(*pf), GFP_KERNEL);
  11896. if (!pf) {
  11897. err = -ENOMEM;
  11898. goto err_pf_alloc;
  11899. }
  11900. pf->next_vsi = 0;
  11901. pf->pdev = pdev;
  11902. set_bit(__I40E_DOWN, pf->state);
  11903. hw = &pf->hw;
  11904. hw->back = pf;
  11905. pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
  11906. I40E_MAX_CSR_SPACE);
  11907. hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
  11908. if (!hw->hw_addr) {
  11909. err = -EIO;
  11910. dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
  11911. (unsigned int)pci_resource_start(pdev, 0),
  11912. pf->ioremap_len, err);
  11913. goto err_ioremap;
  11914. }
  11915. hw->vendor_id = pdev->vendor;
  11916. hw->device_id = pdev->device;
  11917. pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
  11918. hw->subsystem_vendor_id = pdev->subsystem_vendor;
  11919. hw->subsystem_device_id = pdev->subsystem_device;
  11920. hw->bus.device = PCI_SLOT(pdev->devfn);
  11921. hw->bus.func = PCI_FUNC(pdev->devfn);
  11922. hw->bus.bus_id = pdev->bus->number;
  11923. pf->instance = pfs_found;
  11924. /* Select something other than the 802.1ad ethertype for the
  11925. * switch to use internally and drop on ingress.
  11926. */
  11927. hw->switch_tag = 0xffff;
  11928. hw->first_tag = ETH_P_8021AD;
  11929. hw->second_tag = ETH_P_8021Q;
  11930. INIT_LIST_HEAD(&pf->l3_flex_pit_list);
  11931. INIT_LIST_HEAD(&pf->l4_flex_pit_list);
  11932. /* set up the locks for the AQ, do this only once in probe
  11933. * and destroy them only once in remove
  11934. */
  11935. mutex_init(&hw->aq.asq_mutex);
  11936. mutex_init(&hw->aq.arq_mutex);
  11937. pf->msg_enable = netif_msg_init(debug,
  11938. NETIF_MSG_DRV |
  11939. NETIF_MSG_PROBE |
  11940. NETIF_MSG_LINK);
  11941. if (debug < -1)
  11942. pf->hw.debug_mask = debug;
  11943. /* do a special CORER for clearing PXE mode once at init */
  11944. if (hw->revision_id == 0 &&
  11945. (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
  11946. wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
  11947. i40e_flush(hw);
  11948. msleep(200);
  11949. pf->corer_count++;
  11950. i40e_clear_pxe_mode(hw);
  11951. }
  11952. /* Reset here to make sure all is clean and to define PF 'n' */
  11953. i40e_clear_hw(hw);
  11954. err = i40e_pf_reset(hw);
  11955. if (err) {
  11956. dev_info(&pdev->dev, "Initial pf_reset failed: %d\n", err);
  11957. goto err_pf_reset;
  11958. }
  11959. pf->pfr_count++;
  11960. hw->aq.num_arq_entries = I40E_AQ_LEN;
  11961. hw->aq.num_asq_entries = I40E_AQ_LEN;
  11962. hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
  11963. hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
  11964. pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
  11965. snprintf(pf->int_name, sizeof(pf->int_name) - 1,
  11966. "%s-%s:misc",
  11967. dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
  11968. err = i40e_init_shared_code(hw);
  11969. if (err) {
  11970. dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
  11971. err);
  11972. goto err_pf_reset;
  11973. }
  11974. /* set up a default setting for link flow control */
  11975. pf->hw.fc.requested_mode = I40E_FC_NONE;
  11976. err = i40e_init_adminq(hw);
  11977. if (err) {
  11978. if (err == I40E_ERR_FIRMWARE_API_VERSION)
  11979. dev_info(&pdev->dev,
  11980. "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");
  11981. else
  11982. dev_info(&pdev->dev,
  11983. "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
  11984. goto err_pf_reset;
  11985. }
  11986. i40e_get_oem_version(hw);
  11987. /* provide nvm, fw, api versions */
  11988. dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s\n",
  11989. hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
  11990. hw->aq.api_maj_ver, hw->aq.api_min_ver,
  11991. i40e_nvm_version_str(hw));
  11992. if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
  11993. hw->aq.api_min_ver > I40E_FW_MINOR_VERSION(hw))
  11994. dev_info(&pdev->dev,
  11995. "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");
  11996. else if (hw->aq.api_maj_ver == 1 && hw->aq.api_min_ver < 4)
  11997. dev_info(&pdev->dev,
  11998. "The driver for the device detected an older version of the NVM image than expected. Please update the NVM image.\n");
  11999. i40e_verify_eeprom(pf);
  12000. /* Rev 0 hardware was never productized */
  12001. if (hw->revision_id < 1)
  12002. 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");
  12003. i40e_clear_pxe_mode(hw);
  12004. err = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
  12005. if (err)
  12006. goto err_adminq_setup;
  12007. err = i40e_sw_init(pf);
  12008. if (err) {
  12009. dev_info(&pdev->dev, "sw_init failed: %d\n", err);
  12010. goto err_sw_init;
  12011. }
  12012. err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
  12013. hw->func_caps.num_rx_qp, 0, 0);
  12014. if (err) {
  12015. dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
  12016. goto err_init_lan_hmc;
  12017. }
  12018. err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
  12019. if (err) {
  12020. dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
  12021. err = -ENOENT;
  12022. goto err_configure_lan_hmc;
  12023. }
  12024. /* Disable LLDP for NICs that have firmware versions lower than v4.3.
  12025. * Ignore error return codes because if it was already disabled via
  12026. * hardware settings this will fail
  12027. */
  12028. if (pf->hw_features & I40E_HW_STOP_FW_LLDP) {
  12029. dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
  12030. i40e_aq_stop_lldp(hw, true, NULL);
  12031. }
  12032. /* allow a platform config to override the HW addr */
  12033. i40e_get_platform_mac_addr(pdev, pf);
  12034. if (!is_valid_ether_addr(hw->mac.addr)) {
  12035. dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
  12036. err = -EIO;
  12037. goto err_mac_addr;
  12038. }
  12039. dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
  12040. ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
  12041. i40e_get_port_mac_addr(hw, hw->mac.port_addr);
  12042. if (is_valid_ether_addr(hw->mac.port_addr))
  12043. pf->hw_features |= I40E_HW_PORT_ID_VALID;
  12044. pci_set_drvdata(pdev, pf);
  12045. pci_save_state(pdev);
  12046. /* Enable FW to write default DCB config on link-up */
  12047. i40e_aq_set_dcb_parameters(hw, true, NULL);
  12048. #ifdef CONFIG_I40E_DCB
  12049. err = i40e_init_pf_dcb(pf);
  12050. if (err) {
  12051. dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
  12052. pf->flags &= ~(I40E_FLAG_DCB_CAPABLE | I40E_FLAG_DCB_ENABLED);
  12053. /* Continue without DCB enabled */
  12054. }
  12055. #endif /* CONFIG_I40E_DCB */
  12056. /* set up periodic task facility */
  12057. timer_setup(&pf->service_timer, i40e_service_timer, 0);
  12058. pf->service_timer_period = HZ;
  12059. INIT_WORK(&pf->service_task, i40e_service_task);
  12060. clear_bit(__I40E_SERVICE_SCHED, pf->state);
  12061. /* NVM bit on means WoL disabled for the port */
  12062. i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
  12063. if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
  12064. pf->wol_en = false;
  12065. else
  12066. pf->wol_en = true;
  12067. device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
  12068. /* set up the main switch operations */
  12069. i40e_determine_queue_usage(pf);
  12070. err = i40e_init_interrupt_scheme(pf);
  12071. if (err)
  12072. goto err_switch_setup;
  12073. /* The number of VSIs reported by the FW is the minimum guaranteed
  12074. * to us; HW supports far more and we share the remaining pool with
  12075. * the other PFs. We allocate space for more than the guarantee with
  12076. * the understanding that we might not get them all later.
  12077. */
  12078. if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
  12079. pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
  12080. else
  12081. pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
  12082. /* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
  12083. pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
  12084. GFP_KERNEL);
  12085. if (!pf->vsi) {
  12086. err = -ENOMEM;
  12087. goto err_switch_setup;
  12088. }
  12089. #ifdef CONFIG_PCI_IOV
  12090. /* prep for VF support */
  12091. if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
  12092. (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
  12093. !test_bit(__I40E_BAD_EEPROM, pf->state)) {
  12094. if (pci_num_vf(pdev))
  12095. pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
  12096. }
  12097. #endif
  12098. err = i40e_setup_pf_switch(pf, false);
  12099. if (err) {
  12100. dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
  12101. goto err_vsis;
  12102. }
  12103. INIT_LIST_HEAD(&pf->vsi[pf->lan_vsi]->ch_list);
  12104. /* Make sure flow control is set according to current settings */
  12105. err = i40e_set_fc(hw, &set_fc_aq_fail, true);
  12106. if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_GET)
  12107. dev_dbg(&pf->pdev->dev,
  12108. "Set fc with err %s aq_err %s on get_phy_cap\n",
  12109. i40e_stat_str(hw, err),
  12110. i40e_aq_str(hw, hw->aq.asq_last_status));
  12111. if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_SET)
  12112. dev_dbg(&pf->pdev->dev,
  12113. "Set fc with err %s aq_err %s on set_phy_config\n",
  12114. i40e_stat_str(hw, err),
  12115. i40e_aq_str(hw, hw->aq.asq_last_status));
  12116. if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_UPDATE)
  12117. dev_dbg(&pf->pdev->dev,
  12118. "Set fc with err %s aq_err %s on get_link_info\n",
  12119. i40e_stat_str(hw, err),
  12120. i40e_aq_str(hw, hw->aq.asq_last_status));
  12121. /* if FDIR VSI was set up, start it now */
  12122. for (i = 0; i < pf->num_alloc_vsi; i++) {
  12123. if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
  12124. i40e_vsi_open(pf->vsi[i]);
  12125. break;
  12126. }
  12127. }
  12128. /* The driver only wants link up/down and module qualification
  12129. * reports from firmware. Note the negative logic.
  12130. */
  12131. err = i40e_aq_set_phy_int_mask(&pf->hw,
  12132. ~(I40E_AQ_EVENT_LINK_UPDOWN |
  12133. I40E_AQ_EVENT_MEDIA_NA |
  12134. I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
  12135. if (err)
  12136. dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
  12137. i40e_stat_str(&pf->hw, err),
  12138. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  12139. /* Reconfigure hardware for allowing smaller MSS in the case
  12140. * of TSO, so that we avoid the MDD being fired and causing
  12141. * a reset in the case of small MSS+TSO.
  12142. */
  12143. val = rd32(hw, I40E_REG_MSS);
  12144. if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
  12145. val &= ~I40E_REG_MSS_MIN_MASK;
  12146. val |= I40E_64BYTE_MSS;
  12147. wr32(hw, I40E_REG_MSS, val);
  12148. }
  12149. if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
  12150. msleep(75);
  12151. err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
  12152. if (err)
  12153. dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
  12154. i40e_stat_str(&pf->hw, err),
  12155. i40e_aq_str(&pf->hw,
  12156. pf->hw.aq.asq_last_status));
  12157. }
  12158. /* The main driver is (mostly) up and happy. We need to set this state
  12159. * before setting up the misc vector or we get a race and the vector
  12160. * ends up disabled forever.
  12161. */
  12162. clear_bit(__I40E_DOWN, pf->state);
  12163. /* In case of MSIX we are going to setup the misc vector right here
  12164. * to handle admin queue events etc. In case of legacy and MSI
  12165. * the misc functionality and queue processing is combined in
  12166. * the same vector and that gets setup at open.
  12167. */
  12168. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  12169. err = i40e_setup_misc_vector(pf);
  12170. if (err) {
  12171. dev_info(&pdev->dev,
  12172. "setup of misc vector failed: %d\n", err);
  12173. goto err_vsis;
  12174. }
  12175. }
  12176. #ifdef CONFIG_PCI_IOV
  12177. /* prep for VF support */
  12178. if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
  12179. (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
  12180. !test_bit(__I40E_BAD_EEPROM, pf->state)) {
  12181. /* disable link interrupts for VFs */
  12182. val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
  12183. val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
  12184. wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
  12185. i40e_flush(hw);
  12186. if (pci_num_vf(pdev)) {
  12187. dev_info(&pdev->dev,
  12188. "Active VFs found, allocating resources.\n");
  12189. err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
  12190. if (err)
  12191. dev_info(&pdev->dev,
  12192. "Error %d allocating resources for existing VFs\n",
  12193. err);
  12194. }
  12195. }
  12196. #endif /* CONFIG_PCI_IOV */
  12197. if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
  12198. pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
  12199. pf->num_iwarp_msix,
  12200. I40E_IWARP_IRQ_PILE_ID);
  12201. if (pf->iwarp_base_vector < 0) {
  12202. dev_info(&pdev->dev,
  12203. "failed to get tracking for %d vectors for IWARP err=%d\n",
  12204. pf->num_iwarp_msix, pf->iwarp_base_vector);
  12205. pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
  12206. }
  12207. }
  12208. i40e_dbg_pf_init(pf);
  12209. /* tell the firmware that we're starting */
  12210. i40e_send_version(pf);
  12211. /* since everything's happy, start the service_task timer */
  12212. mod_timer(&pf->service_timer,
  12213. round_jiffies(jiffies + pf->service_timer_period));
  12214. /* add this PF to client device list and launch a client service task */
  12215. if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
  12216. err = i40e_lan_add_device(pf);
  12217. if (err)
  12218. dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
  12219. err);
  12220. }
  12221. #define PCI_SPEED_SIZE 8
  12222. #define PCI_WIDTH_SIZE 8
  12223. /* Devices on the IOSF bus do not have this information
  12224. * and will report PCI Gen 1 x 1 by default so don't bother
  12225. * checking them.
  12226. */
  12227. if (!(pf->hw_features & I40E_HW_NO_PCI_LINK_CHECK)) {
  12228. char speed[PCI_SPEED_SIZE] = "Unknown";
  12229. char width[PCI_WIDTH_SIZE] = "Unknown";
  12230. /* Get the negotiated link width and speed from PCI config
  12231. * space
  12232. */
  12233. pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
  12234. &link_status);
  12235. i40e_set_pci_config_data(hw, link_status);
  12236. switch (hw->bus.speed) {
  12237. case i40e_bus_speed_8000:
  12238. strncpy(speed, "8.0", PCI_SPEED_SIZE); break;
  12239. case i40e_bus_speed_5000:
  12240. strncpy(speed, "5.0", PCI_SPEED_SIZE); break;
  12241. case i40e_bus_speed_2500:
  12242. strncpy(speed, "2.5", PCI_SPEED_SIZE); break;
  12243. default:
  12244. break;
  12245. }
  12246. switch (hw->bus.width) {
  12247. case i40e_bus_width_pcie_x8:
  12248. strncpy(width, "8", PCI_WIDTH_SIZE); break;
  12249. case i40e_bus_width_pcie_x4:
  12250. strncpy(width, "4", PCI_WIDTH_SIZE); break;
  12251. case i40e_bus_width_pcie_x2:
  12252. strncpy(width, "2", PCI_WIDTH_SIZE); break;
  12253. case i40e_bus_width_pcie_x1:
  12254. strncpy(width, "1", PCI_WIDTH_SIZE); break;
  12255. default:
  12256. break;
  12257. }
  12258. dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
  12259. speed, width);
  12260. if (hw->bus.width < i40e_bus_width_pcie_x8 ||
  12261. hw->bus.speed < i40e_bus_speed_8000) {
  12262. dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
  12263. dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
  12264. }
  12265. }
  12266. /* get the requested speeds from the fw */
  12267. err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
  12268. if (err)
  12269. dev_dbg(&pf->pdev->dev, "get requested speeds ret = %s last_status = %s\n",
  12270. i40e_stat_str(&pf->hw, err),
  12271. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  12272. pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
  12273. /* get the supported phy types from the fw */
  12274. err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
  12275. if (err)
  12276. dev_dbg(&pf->pdev->dev, "get supported phy types ret = %s last_status = %s\n",
  12277. i40e_stat_str(&pf->hw, err),
  12278. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  12279. /* Add a filter to drop all Flow control frames from any VSI from being
  12280. * transmitted. By doing so we stop a malicious VF from sending out
  12281. * PAUSE or PFC frames and potentially controlling traffic for other
  12282. * PF/VF VSIs.
  12283. * The FW can still send Flow control frames if enabled.
  12284. */
  12285. i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
  12286. pf->main_vsi_seid);
  12287. if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
  12288. (pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
  12289. pf->hw_features |= I40E_HW_PHY_CONTROLS_LEDS;
  12290. if (pf->hw.device_id == I40E_DEV_ID_SFP_I_X722)
  12291. pf->hw_features |= I40E_HW_HAVE_CRT_RETIMER;
  12292. /* print a string summarizing features */
  12293. i40e_print_features(pf);
  12294. return 0;
  12295. /* Unwind what we've done if something failed in the setup */
  12296. err_vsis:
  12297. set_bit(__I40E_DOWN, pf->state);
  12298. i40e_clear_interrupt_scheme(pf);
  12299. kfree(pf->vsi);
  12300. err_switch_setup:
  12301. i40e_reset_interrupt_capability(pf);
  12302. del_timer_sync(&pf->service_timer);
  12303. err_mac_addr:
  12304. err_configure_lan_hmc:
  12305. (void)i40e_shutdown_lan_hmc(hw);
  12306. err_init_lan_hmc:
  12307. kfree(pf->qp_pile);
  12308. err_sw_init:
  12309. err_adminq_setup:
  12310. err_pf_reset:
  12311. iounmap(hw->hw_addr);
  12312. err_ioremap:
  12313. kfree(pf);
  12314. err_pf_alloc:
  12315. pci_disable_pcie_error_reporting(pdev);
  12316. pci_release_mem_regions(pdev);
  12317. err_pci_reg:
  12318. err_dma:
  12319. pci_disable_device(pdev);
  12320. return err;
  12321. }
  12322. /**
  12323. * i40e_remove - Device removal routine
  12324. * @pdev: PCI device information struct
  12325. *
  12326. * i40e_remove is called by the PCI subsystem to alert the driver
  12327. * that is should release a PCI device. This could be caused by a
  12328. * Hot-Plug event, or because the driver is going to be removed from
  12329. * memory.
  12330. **/
  12331. static void i40e_remove(struct pci_dev *pdev)
  12332. {
  12333. struct i40e_pf *pf = pci_get_drvdata(pdev);
  12334. struct i40e_hw *hw = &pf->hw;
  12335. i40e_status ret_code;
  12336. int i;
  12337. i40e_dbg_pf_exit(pf);
  12338. i40e_ptp_stop(pf);
  12339. /* Disable RSS in hw */
  12340. i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
  12341. i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
  12342. /* no more scheduling of any task */
  12343. set_bit(__I40E_SUSPENDED, pf->state);
  12344. set_bit(__I40E_DOWN, pf->state);
  12345. if (pf->service_timer.function)
  12346. del_timer_sync(&pf->service_timer);
  12347. if (pf->service_task.func)
  12348. cancel_work_sync(&pf->service_task);
  12349. /* Client close must be called explicitly here because the timer
  12350. * has been stopped.
  12351. */
  12352. i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
  12353. if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
  12354. i40e_free_vfs(pf);
  12355. pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
  12356. }
  12357. i40e_fdir_teardown(pf);
  12358. /* If there is a switch structure or any orphans, remove them.
  12359. * This will leave only the PF's VSI remaining.
  12360. */
  12361. for (i = 0; i < I40E_MAX_VEB; i++) {
  12362. if (!pf->veb[i])
  12363. continue;
  12364. if (pf->veb[i]->uplink_seid == pf->mac_seid ||
  12365. pf->veb[i]->uplink_seid == 0)
  12366. i40e_switch_branch_release(pf->veb[i]);
  12367. }
  12368. /* Now we can shutdown the PF's VSI, just before we kill
  12369. * adminq and hmc.
  12370. */
  12371. if (pf->vsi[pf->lan_vsi])
  12372. i40e_vsi_release(pf->vsi[pf->lan_vsi]);
  12373. i40e_cloud_filter_exit(pf);
  12374. /* remove attached clients */
  12375. if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
  12376. ret_code = i40e_lan_del_device(pf);
  12377. if (ret_code)
  12378. dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
  12379. ret_code);
  12380. }
  12381. /* shutdown and destroy the HMC */
  12382. if (hw->hmc.hmc_obj) {
  12383. ret_code = i40e_shutdown_lan_hmc(hw);
  12384. if (ret_code)
  12385. dev_warn(&pdev->dev,
  12386. "Failed to destroy the HMC resources: %d\n",
  12387. ret_code);
  12388. }
  12389. /* shutdown the adminq */
  12390. i40e_shutdown_adminq(hw);
  12391. /* destroy the locks only once, here */
  12392. mutex_destroy(&hw->aq.arq_mutex);
  12393. mutex_destroy(&hw->aq.asq_mutex);
  12394. /* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
  12395. i40e_clear_interrupt_scheme(pf);
  12396. for (i = 0; i < pf->num_alloc_vsi; i++) {
  12397. if (pf->vsi[i]) {
  12398. i40e_vsi_clear_rings(pf->vsi[i]);
  12399. i40e_vsi_clear(pf->vsi[i]);
  12400. pf->vsi[i] = NULL;
  12401. }
  12402. }
  12403. for (i = 0; i < I40E_MAX_VEB; i++) {
  12404. kfree(pf->veb[i]);
  12405. pf->veb[i] = NULL;
  12406. }
  12407. kfree(pf->qp_pile);
  12408. kfree(pf->vsi);
  12409. iounmap(hw->hw_addr);
  12410. kfree(pf);
  12411. pci_release_mem_regions(pdev);
  12412. pci_disable_pcie_error_reporting(pdev);
  12413. pci_disable_device(pdev);
  12414. }
  12415. /**
  12416. * i40e_pci_error_detected - warning that something funky happened in PCI land
  12417. * @pdev: PCI device information struct
  12418. * @error: the type of PCI error
  12419. *
  12420. * Called to warn that something happened and the error handling steps
  12421. * are in progress. Allows the driver to quiesce things, be ready for
  12422. * remediation.
  12423. **/
  12424. static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
  12425. enum pci_channel_state error)
  12426. {
  12427. struct i40e_pf *pf = pci_get_drvdata(pdev);
  12428. dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
  12429. if (!pf) {
  12430. dev_info(&pdev->dev,
  12431. "Cannot recover - error happened during device probe\n");
  12432. return PCI_ERS_RESULT_DISCONNECT;
  12433. }
  12434. /* shutdown all operations */
  12435. if (!test_bit(__I40E_SUSPENDED, pf->state))
  12436. i40e_prep_for_reset(pf, false);
  12437. /* Request a slot reset */
  12438. return PCI_ERS_RESULT_NEED_RESET;
  12439. }
  12440. /**
  12441. * i40e_pci_error_slot_reset - a PCI slot reset just happened
  12442. * @pdev: PCI device information struct
  12443. *
  12444. * Called to find if the driver can work with the device now that
  12445. * the pci slot has been reset. If a basic connection seems good
  12446. * (registers are readable and have sane content) then return a
  12447. * happy little PCI_ERS_RESULT_xxx.
  12448. **/
  12449. static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
  12450. {
  12451. struct i40e_pf *pf = pci_get_drvdata(pdev);
  12452. pci_ers_result_t result;
  12453. int err;
  12454. u32 reg;
  12455. dev_dbg(&pdev->dev, "%s\n", __func__);
  12456. if (pci_enable_device_mem(pdev)) {
  12457. dev_info(&pdev->dev,
  12458. "Cannot re-enable PCI device after reset.\n");
  12459. result = PCI_ERS_RESULT_DISCONNECT;
  12460. } else {
  12461. pci_set_master(pdev);
  12462. pci_restore_state(pdev);
  12463. pci_save_state(pdev);
  12464. pci_wake_from_d3(pdev, false);
  12465. reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
  12466. if (reg == 0)
  12467. result = PCI_ERS_RESULT_RECOVERED;
  12468. else
  12469. result = PCI_ERS_RESULT_DISCONNECT;
  12470. }
  12471. err = pci_cleanup_aer_uncorrect_error_status(pdev);
  12472. if (err) {
  12473. dev_info(&pdev->dev,
  12474. "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
  12475. err);
  12476. /* non-fatal, continue */
  12477. }
  12478. return result;
  12479. }
  12480. /**
  12481. * i40e_pci_error_reset_prepare - prepare device driver for pci reset
  12482. * @pdev: PCI device information struct
  12483. */
  12484. static void i40e_pci_error_reset_prepare(struct pci_dev *pdev)
  12485. {
  12486. struct i40e_pf *pf = pci_get_drvdata(pdev);
  12487. i40e_prep_for_reset(pf, false);
  12488. }
  12489. /**
  12490. * i40e_pci_error_reset_done - pci reset done, device driver reset can begin
  12491. * @pdev: PCI device information struct
  12492. */
  12493. static void i40e_pci_error_reset_done(struct pci_dev *pdev)
  12494. {
  12495. struct i40e_pf *pf = pci_get_drvdata(pdev);
  12496. i40e_reset_and_rebuild(pf, false, false);
  12497. }
  12498. /**
  12499. * i40e_pci_error_resume - restart operations after PCI error recovery
  12500. * @pdev: PCI device information struct
  12501. *
  12502. * Called to allow the driver to bring things back up after PCI error
  12503. * and/or reset recovery has finished.
  12504. **/
  12505. static void i40e_pci_error_resume(struct pci_dev *pdev)
  12506. {
  12507. struct i40e_pf *pf = pci_get_drvdata(pdev);
  12508. dev_dbg(&pdev->dev, "%s\n", __func__);
  12509. if (test_bit(__I40E_SUSPENDED, pf->state))
  12510. return;
  12511. i40e_handle_reset_warning(pf, false);
  12512. }
  12513. /**
  12514. * i40e_enable_mc_magic_wake - enable multicast magic packet wake up
  12515. * using the mac_address_write admin q function
  12516. * @pf: pointer to i40e_pf struct
  12517. **/
  12518. static void i40e_enable_mc_magic_wake(struct i40e_pf *pf)
  12519. {
  12520. struct i40e_hw *hw = &pf->hw;
  12521. i40e_status ret;
  12522. u8 mac_addr[6];
  12523. u16 flags = 0;
  12524. /* Get current MAC address in case it's an LAA */
  12525. if (pf->vsi[pf->lan_vsi] && pf->vsi[pf->lan_vsi]->netdev) {
  12526. ether_addr_copy(mac_addr,
  12527. pf->vsi[pf->lan_vsi]->netdev->dev_addr);
  12528. } else {
  12529. dev_err(&pf->pdev->dev,
  12530. "Failed to retrieve MAC address; using default\n");
  12531. ether_addr_copy(mac_addr, hw->mac.addr);
  12532. }
  12533. /* The FW expects the mac address write cmd to first be called with
  12534. * one of these flags before calling it again with the multicast
  12535. * enable flags.
  12536. */
  12537. flags = I40E_AQC_WRITE_TYPE_LAA_WOL;
  12538. if (hw->func_caps.flex10_enable && hw->partition_id != 1)
  12539. flags = I40E_AQC_WRITE_TYPE_LAA_ONLY;
  12540. ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
  12541. if (ret) {
  12542. dev_err(&pf->pdev->dev,
  12543. "Failed to update MAC address registers; cannot enable Multicast Magic packet wake up");
  12544. return;
  12545. }
  12546. flags = I40E_AQC_MC_MAG_EN
  12547. | I40E_AQC_WOL_PRESERVE_ON_PFR
  12548. | I40E_AQC_WRITE_TYPE_UPDATE_MC_MAG;
  12549. ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
  12550. if (ret)
  12551. dev_err(&pf->pdev->dev,
  12552. "Failed to enable Multicast Magic Packet wake up\n");
  12553. }
  12554. /**
  12555. * i40e_shutdown - PCI callback for shutting down
  12556. * @pdev: PCI device information struct
  12557. **/
  12558. static void i40e_shutdown(struct pci_dev *pdev)
  12559. {
  12560. struct i40e_pf *pf = pci_get_drvdata(pdev);
  12561. struct i40e_hw *hw = &pf->hw;
  12562. set_bit(__I40E_SUSPENDED, pf->state);
  12563. set_bit(__I40E_DOWN, pf->state);
  12564. rtnl_lock();
  12565. i40e_prep_for_reset(pf, true);
  12566. rtnl_unlock();
  12567. wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
  12568. wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
  12569. del_timer_sync(&pf->service_timer);
  12570. cancel_work_sync(&pf->service_task);
  12571. i40e_cloud_filter_exit(pf);
  12572. i40e_fdir_teardown(pf);
  12573. /* Client close must be called explicitly here because the timer
  12574. * has been stopped.
  12575. */
  12576. i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
  12577. if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
  12578. i40e_enable_mc_magic_wake(pf);
  12579. i40e_prep_for_reset(pf, false);
  12580. wr32(hw, I40E_PFPM_APM,
  12581. (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
  12582. wr32(hw, I40E_PFPM_WUFC,
  12583. (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
  12584. i40e_clear_interrupt_scheme(pf);
  12585. if (system_state == SYSTEM_POWER_OFF) {
  12586. pci_wake_from_d3(pdev, pf->wol_en);
  12587. pci_set_power_state(pdev, PCI_D3hot);
  12588. }
  12589. }
  12590. /**
  12591. * i40e_suspend - PM callback for moving to D3
  12592. * @dev: generic device information structure
  12593. **/
  12594. static int __maybe_unused i40e_suspend(struct device *dev)
  12595. {
  12596. struct pci_dev *pdev = to_pci_dev(dev);
  12597. struct i40e_pf *pf = pci_get_drvdata(pdev);
  12598. struct i40e_hw *hw = &pf->hw;
  12599. /* If we're already suspended, then there is nothing to do */
  12600. if (test_and_set_bit(__I40E_SUSPENDED, pf->state))
  12601. return 0;
  12602. set_bit(__I40E_DOWN, pf->state);
  12603. /* Ensure service task will not be running */
  12604. del_timer_sync(&pf->service_timer);
  12605. cancel_work_sync(&pf->service_task);
  12606. /* Client close must be called explicitly here because the timer
  12607. * has been stopped.
  12608. */
  12609. i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
  12610. if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
  12611. i40e_enable_mc_magic_wake(pf);
  12612. /* Since we're going to destroy queues during the
  12613. * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
  12614. * whole section
  12615. */
  12616. rtnl_lock();
  12617. i40e_prep_for_reset(pf, true);
  12618. wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
  12619. wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
  12620. /* Clear the interrupt scheme and release our IRQs so that the system
  12621. * can safely hibernate even when there are a large number of CPUs.
  12622. * Otherwise hibernation might fail when mapping all the vectors back
  12623. * to CPU0.
  12624. */
  12625. i40e_clear_interrupt_scheme(pf);
  12626. rtnl_unlock();
  12627. return 0;
  12628. }
  12629. /**
  12630. * i40e_resume - PM callback for waking up from D3
  12631. * @dev: generic device information structure
  12632. **/
  12633. static int __maybe_unused i40e_resume(struct device *dev)
  12634. {
  12635. struct pci_dev *pdev = to_pci_dev(dev);
  12636. struct i40e_pf *pf = pci_get_drvdata(pdev);
  12637. int err;
  12638. /* If we're not suspended, then there is nothing to do */
  12639. if (!test_bit(__I40E_SUSPENDED, pf->state))
  12640. return 0;
  12641. /* We need to hold the RTNL lock prior to restoring interrupt schemes,
  12642. * since we're going to be restoring queues
  12643. */
  12644. rtnl_lock();
  12645. /* We cleared the interrupt scheme when we suspended, so we need to
  12646. * restore it now to resume device functionality.
  12647. */
  12648. err = i40e_restore_interrupt_scheme(pf);
  12649. if (err) {
  12650. dev_err(&pdev->dev, "Cannot restore interrupt scheme: %d\n",
  12651. err);
  12652. }
  12653. clear_bit(__I40E_DOWN, pf->state);
  12654. i40e_reset_and_rebuild(pf, false, true);
  12655. rtnl_unlock();
  12656. /* Clear suspended state last after everything is recovered */
  12657. clear_bit(__I40E_SUSPENDED, pf->state);
  12658. /* Restart the service task */
  12659. mod_timer(&pf->service_timer,
  12660. round_jiffies(jiffies + pf->service_timer_period));
  12661. return 0;
  12662. }
  12663. static const struct pci_error_handlers i40e_err_handler = {
  12664. .error_detected = i40e_pci_error_detected,
  12665. .slot_reset = i40e_pci_error_slot_reset,
  12666. .reset_prepare = i40e_pci_error_reset_prepare,
  12667. .reset_done = i40e_pci_error_reset_done,
  12668. .resume = i40e_pci_error_resume,
  12669. };
  12670. static SIMPLE_DEV_PM_OPS(i40e_pm_ops, i40e_suspend, i40e_resume);
  12671. static struct pci_driver i40e_driver = {
  12672. .name = i40e_driver_name,
  12673. .id_table = i40e_pci_tbl,
  12674. .probe = i40e_probe,
  12675. .remove = i40e_remove,
  12676. .driver = {
  12677. .pm = &i40e_pm_ops,
  12678. },
  12679. .shutdown = i40e_shutdown,
  12680. .err_handler = &i40e_err_handler,
  12681. .sriov_configure = i40e_pci_sriov_configure,
  12682. };
  12683. /**
  12684. * i40e_init_module - Driver registration routine
  12685. *
  12686. * i40e_init_module is the first routine called when the driver is
  12687. * loaded. All it does is register with the PCI subsystem.
  12688. **/
  12689. static int __init i40e_init_module(void)
  12690. {
  12691. pr_info("%s: %s - version %s\n", i40e_driver_name,
  12692. i40e_driver_string, i40e_driver_version_str);
  12693. pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
  12694. /* There is no need to throttle the number of active tasks because
  12695. * each device limits its own task using a state bit for scheduling
  12696. * the service task, and the device tasks do not interfere with each
  12697. * other, so we don't set a max task limit. We must set WQ_MEM_RECLAIM
  12698. * since we need to be able to guarantee forward progress even under
  12699. * memory pressure.
  12700. */
  12701. i40e_wq = alloc_workqueue("%s", WQ_MEM_RECLAIM, 0, i40e_driver_name);
  12702. if (!i40e_wq) {
  12703. pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
  12704. return -ENOMEM;
  12705. }
  12706. i40e_dbg_init();
  12707. return pci_register_driver(&i40e_driver);
  12708. }
  12709. module_init(i40e_init_module);
  12710. /**
  12711. * i40e_exit_module - Driver exit cleanup routine
  12712. *
  12713. * i40e_exit_module is called just before the driver is removed
  12714. * from memory.
  12715. **/
  12716. static void __exit i40e_exit_module(void)
  12717. {
  12718. pci_unregister_driver(&i40e_driver);
  12719. destroy_workqueue(i40e_wq);
  12720. i40e_dbg_exit();
  12721. }
  12722. module_exit(i40e_exit_module);