i40e_main.c 247 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213
  1. /*******************************************************************************
  2. *
  3. * Intel Ethernet Controller XL710 Family Linux Driver
  4. * Copyright(c) 2013 - 2014 Intel Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * The full GNU General Public License is included in this distribution in
  19. * the file called "COPYING".
  20. *
  21. * Contact Information:
  22. * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
  23. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  24. *
  25. ******************************************************************************/
  26. /* Local includes */
  27. #include "i40e.h"
  28. #include "i40e_diag.h"
  29. #ifdef CONFIG_I40E_VXLAN
  30. #include <net/vxlan.h>
  31. #endif
  32. const char i40e_driver_name[] = "i40e";
  33. static const char i40e_driver_string[] =
  34. "Intel(R) Ethernet Connection XL710 Network Driver";
  35. #define DRV_KERN "-k"
  36. #define DRV_VERSION_MAJOR 0
  37. #define DRV_VERSION_MINOR 4
  38. #define DRV_VERSION_BUILD 13
  39. #define DRV_VERSION __stringify(DRV_VERSION_MAJOR) "." \
  40. __stringify(DRV_VERSION_MINOR) "." \
  41. __stringify(DRV_VERSION_BUILD) DRV_KERN
  42. const char i40e_driver_version_str[] = DRV_VERSION;
  43. static const char i40e_copyright[] = "Copyright (c) 2013 - 2014 Intel Corporation.";
  44. /* a bit of forward declarations */
  45. static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi);
  46. static void i40e_handle_reset_warning(struct i40e_pf *pf);
  47. static int i40e_add_vsi(struct i40e_vsi *vsi);
  48. static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi);
  49. static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit);
  50. static int i40e_setup_misc_vector(struct i40e_pf *pf);
  51. static void i40e_determine_queue_usage(struct i40e_pf *pf);
  52. static int i40e_setup_pf_filter_control(struct i40e_pf *pf);
  53. static void i40e_fdir_sb_setup(struct i40e_pf *pf);
  54. static int i40e_veb_get_bw_info(struct i40e_veb *veb);
  55. /* i40e_pci_tbl - PCI Device ID Table
  56. *
  57. * Last entry must be all 0s
  58. *
  59. * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
  60. * Class, Class Mask, private data (not used) }
  61. */
  62. static DEFINE_PCI_DEVICE_TABLE(i40e_pci_tbl) = {
  63. {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_XL710), 0},
  64. {PCI_VDEVICE(INTEL, I40E_DEV_ID_QEMU), 0},
  65. {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_A), 0},
  66. {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_B), 0},
  67. {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_C), 0},
  68. {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_A), 0},
  69. {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_B), 0},
  70. {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_C), 0},
  71. /* required last entry */
  72. {0, }
  73. };
  74. MODULE_DEVICE_TABLE(pci, i40e_pci_tbl);
  75. #define I40E_MAX_VF_COUNT 128
  76. static int debug = -1;
  77. module_param(debug, int, 0);
  78. MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
  79. MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
  80. MODULE_DESCRIPTION("Intel(R) Ethernet Connection XL710 Network Driver");
  81. MODULE_LICENSE("GPL");
  82. MODULE_VERSION(DRV_VERSION);
  83. /**
  84. * i40e_allocate_dma_mem_d - OS specific memory alloc for shared code
  85. * @hw: pointer to the HW structure
  86. * @mem: ptr to mem struct to fill out
  87. * @size: size of memory requested
  88. * @alignment: what to align the allocation to
  89. **/
  90. int i40e_allocate_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem,
  91. u64 size, u32 alignment)
  92. {
  93. struct i40e_pf *pf = (struct i40e_pf *)hw->back;
  94. mem->size = ALIGN(size, alignment);
  95. mem->va = dma_zalloc_coherent(&pf->pdev->dev, mem->size,
  96. &mem->pa, GFP_KERNEL);
  97. if (!mem->va)
  98. return -ENOMEM;
  99. return 0;
  100. }
  101. /**
  102. * i40e_free_dma_mem_d - OS specific memory free for shared code
  103. * @hw: pointer to the HW structure
  104. * @mem: ptr to mem struct to free
  105. **/
  106. int i40e_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
  107. {
  108. struct i40e_pf *pf = (struct i40e_pf *)hw->back;
  109. dma_free_coherent(&pf->pdev->dev, mem->size, mem->va, mem->pa);
  110. mem->va = NULL;
  111. mem->pa = 0;
  112. mem->size = 0;
  113. return 0;
  114. }
  115. /**
  116. * i40e_allocate_virt_mem_d - OS specific memory alloc for shared code
  117. * @hw: pointer to the HW structure
  118. * @mem: ptr to mem struct to fill out
  119. * @size: size of memory requested
  120. **/
  121. int i40e_allocate_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem,
  122. u32 size)
  123. {
  124. mem->size = size;
  125. mem->va = kzalloc(size, GFP_KERNEL);
  126. if (!mem->va)
  127. return -ENOMEM;
  128. return 0;
  129. }
  130. /**
  131. * i40e_free_virt_mem_d - OS specific memory free for shared code
  132. * @hw: pointer to the HW structure
  133. * @mem: ptr to mem struct to free
  134. **/
  135. int i40e_free_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem)
  136. {
  137. /* it's ok to kfree a NULL pointer */
  138. kfree(mem->va);
  139. mem->va = NULL;
  140. mem->size = 0;
  141. return 0;
  142. }
  143. /**
  144. * i40e_get_lump - find a lump of free generic resource
  145. * @pf: board private structure
  146. * @pile: the pile of resource to search
  147. * @needed: the number of items needed
  148. * @id: an owner id to stick on the items assigned
  149. *
  150. * Returns the base item index of the lump, or negative for error
  151. *
  152. * The search_hint trick and lack of advanced fit-finding only work
  153. * because we're highly likely to have all the same size lump requests.
  154. * Linear search time and any fragmentation should be minimal.
  155. **/
  156. static int i40e_get_lump(struct i40e_pf *pf, struct i40e_lump_tracking *pile,
  157. u16 needed, u16 id)
  158. {
  159. int ret = -ENOMEM;
  160. int i, j;
  161. if (!pile || needed == 0 || id >= I40E_PILE_VALID_BIT) {
  162. dev_info(&pf->pdev->dev,
  163. "param err: pile=%p needed=%d id=0x%04x\n",
  164. pile, needed, id);
  165. return -EINVAL;
  166. }
  167. /* start the linear search with an imperfect hint */
  168. i = pile->search_hint;
  169. while (i < pile->num_entries) {
  170. /* skip already allocated entries */
  171. if (pile->list[i] & I40E_PILE_VALID_BIT) {
  172. i++;
  173. continue;
  174. }
  175. /* do we have enough in this lump? */
  176. for (j = 0; (j < needed) && ((i+j) < pile->num_entries); j++) {
  177. if (pile->list[i+j] & I40E_PILE_VALID_BIT)
  178. break;
  179. }
  180. if (j == needed) {
  181. /* there was enough, so assign it to the requestor */
  182. for (j = 0; j < needed; j++)
  183. pile->list[i+j] = id | I40E_PILE_VALID_BIT;
  184. ret = i;
  185. pile->search_hint = i + j;
  186. break;
  187. } else {
  188. /* not enough, so skip over it and continue looking */
  189. i += j;
  190. }
  191. }
  192. return ret;
  193. }
  194. /**
  195. * i40e_put_lump - return a lump of generic resource
  196. * @pile: the pile of resource to search
  197. * @index: the base item index
  198. * @id: the owner id of the items assigned
  199. *
  200. * Returns the count of items in the lump
  201. **/
  202. static int i40e_put_lump(struct i40e_lump_tracking *pile, u16 index, u16 id)
  203. {
  204. int valid_id = (id | I40E_PILE_VALID_BIT);
  205. int count = 0;
  206. int i;
  207. if (!pile || index >= pile->num_entries)
  208. return -EINVAL;
  209. for (i = index;
  210. i < pile->num_entries && pile->list[i] == valid_id;
  211. i++) {
  212. pile->list[i] = 0;
  213. count++;
  214. }
  215. if (count && index < pile->search_hint)
  216. pile->search_hint = index;
  217. return count;
  218. }
  219. /**
  220. * i40e_service_event_schedule - Schedule the service task to wake up
  221. * @pf: board private structure
  222. *
  223. * If not already scheduled, this puts the task into the work queue
  224. **/
  225. static void i40e_service_event_schedule(struct i40e_pf *pf)
  226. {
  227. if (!test_bit(__I40E_DOWN, &pf->state) &&
  228. !test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state) &&
  229. !test_and_set_bit(__I40E_SERVICE_SCHED, &pf->state))
  230. schedule_work(&pf->service_task);
  231. }
  232. /**
  233. * i40e_tx_timeout - Respond to a Tx Hang
  234. * @netdev: network interface device structure
  235. *
  236. * If any port has noticed a Tx timeout, it is likely that the whole
  237. * device is munged, not just the one netdev port, so go for the full
  238. * reset.
  239. **/
  240. static void i40e_tx_timeout(struct net_device *netdev)
  241. {
  242. struct i40e_netdev_priv *np = netdev_priv(netdev);
  243. struct i40e_vsi *vsi = np->vsi;
  244. struct i40e_pf *pf = vsi->back;
  245. pf->tx_timeout_count++;
  246. if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ*20)))
  247. pf->tx_timeout_recovery_level = 0;
  248. pf->tx_timeout_last_recovery = jiffies;
  249. netdev_info(netdev, "tx_timeout recovery level %d\n",
  250. pf->tx_timeout_recovery_level);
  251. switch (pf->tx_timeout_recovery_level) {
  252. case 0:
  253. /* disable and re-enable queues for the VSI */
  254. if (in_interrupt()) {
  255. set_bit(__I40E_REINIT_REQUESTED, &pf->state);
  256. set_bit(__I40E_REINIT_REQUESTED, &vsi->state);
  257. } else {
  258. i40e_vsi_reinit_locked(vsi);
  259. }
  260. break;
  261. case 1:
  262. set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
  263. break;
  264. case 2:
  265. set_bit(__I40E_CORE_RESET_REQUESTED, &pf->state);
  266. break;
  267. case 3:
  268. set_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state);
  269. break;
  270. default:
  271. netdev_err(netdev, "tx_timeout recovery unsuccessful\n");
  272. set_bit(__I40E_DOWN_REQUESTED, &pf->state);
  273. set_bit(__I40E_DOWN_REQUESTED, &vsi->state);
  274. break;
  275. }
  276. i40e_service_event_schedule(pf);
  277. pf->tx_timeout_recovery_level++;
  278. }
  279. /**
  280. * i40e_release_rx_desc - Store the new tail and head values
  281. * @rx_ring: ring to bump
  282. * @val: new head index
  283. **/
  284. static inline void i40e_release_rx_desc(struct i40e_ring *rx_ring, u32 val)
  285. {
  286. rx_ring->next_to_use = val;
  287. /* Force memory writes to complete before letting h/w
  288. * know there are new descriptors to fetch. (Only
  289. * applicable for weak-ordered memory model archs,
  290. * such as IA-64).
  291. */
  292. wmb();
  293. writel(val, rx_ring->tail);
  294. }
  295. /**
  296. * i40e_get_vsi_stats_struct - Get System Network Statistics
  297. * @vsi: the VSI we care about
  298. *
  299. * Returns the address of the device statistics structure.
  300. * The statistics are actually updated from the service task.
  301. **/
  302. struct rtnl_link_stats64 *i40e_get_vsi_stats_struct(struct i40e_vsi *vsi)
  303. {
  304. return &vsi->net_stats;
  305. }
  306. /**
  307. * i40e_get_netdev_stats_struct - Get statistics for netdev interface
  308. * @netdev: network interface device structure
  309. *
  310. * Returns the address of the device statistics structure.
  311. * The statistics are actually updated from the service task.
  312. **/
  313. static struct rtnl_link_stats64 *i40e_get_netdev_stats_struct(
  314. struct net_device *netdev,
  315. struct rtnl_link_stats64 *stats)
  316. {
  317. struct i40e_netdev_priv *np = netdev_priv(netdev);
  318. struct i40e_ring *tx_ring, *rx_ring;
  319. struct i40e_vsi *vsi = np->vsi;
  320. struct rtnl_link_stats64 *vsi_stats = i40e_get_vsi_stats_struct(vsi);
  321. int i;
  322. if (test_bit(__I40E_DOWN, &vsi->state))
  323. return stats;
  324. if (!vsi->tx_rings)
  325. return stats;
  326. rcu_read_lock();
  327. for (i = 0; i < vsi->num_queue_pairs; i++) {
  328. u64 bytes, packets;
  329. unsigned int start;
  330. tx_ring = ACCESS_ONCE(vsi->tx_rings[i]);
  331. if (!tx_ring)
  332. continue;
  333. do {
  334. start = u64_stats_fetch_begin_irq(&tx_ring->syncp);
  335. packets = tx_ring->stats.packets;
  336. bytes = tx_ring->stats.bytes;
  337. } while (u64_stats_fetch_retry_irq(&tx_ring->syncp, start));
  338. stats->tx_packets += packets;
  339. stats->tx_bytes += bytes;
  340. rx_ring = &tx_ring[1];
  341. do {
  342. start = u64_stats_fetch_begin_irq(&rx_ring->syncp);
  343. packets = rx_ring->stats.packets;
  344. bytes = rx_ring->stats.bytes;
  345. } while (u64_stats_fetch_retry_irq(&rx_ring->syncp, start));
  346. stats->rx_packets += packets;
  347. stats->rx_bytes += bytes;
  348. }
  349. rcu_read_unlock();
  350. /* following stats updated by i40e_watchdog_subtask() */
  351. stats->multicast = vsi_stats->multicast;
  352. stats->tx_errors = vsi_stats->tx_errors;
  353. stats->tx_dropped = vsi_stats->tx_dropped;
  354. stats->rx_errors = vsi_stats->rx_errors;
  355. stats->rx_crc_errors = vsi_stats->rx_crc_errors;
  356. stats->rx_length_errors = vsi_stats->rx_length_errors;
  357. return stats;
  358. }
  359. /**
  360. * i40e_vsi_reset_stats - Resets all stats of the given vsi
  361. * @vsi: the VSI to have its stats reset
  362. **/
  363. void i40e_vsi_reset_stats(struct i40e_vsi *vsi)
  364. {
  365. struct rtnl_link_stats64 *ns;
  366. int i;
  367. if (!vsi)
  368. return;
  369. ns = i40e_get_vsi_stats_struct(vsi);
  370. memset(ns, 0, sizeof(*ns));
  371. memset(&vsi->net_stats_offsets, 0, sizeof(vsi->net_stats_offsets));
  372. memset(&vsi->eth_stats, 0, sizeof(vsi->eth_stats));
  373. memset(&vsi->eth_stats_offsets, 0, sizeof(vsi->eth_stats_offsets));
  374. if (vsi->rx_rings && vsi->rx_rings[0]) {
  375. for (i = 0; i < vsi->num_queue_pairs; i++) {
  376. memset(&vsi->rx_rings[i]->stats, 0 ,
  377. sizeof(vsi->rx_rings[i]->stats));
  378. memset(&vsi->rx_rings[i]->rx_stats, 0 ,
  379. sizeof(vsi->rx_rings[i]->rx_stats));
  380. memset(&vsi->tx_rings[i]->stats, 0 ,
  381. sizeof(vsi->tx_rings[i]->stats));
  382. memset(&vsi->tx_rings[i]->tx_stats, 0,
  383. sizeof(vsi->tx_rings[i]->tx_stats));
  384. }
  385. }
  386. vsi->stat_offsets_loaded = false;
  387. }
  388. /**
  389. * i40e_pf_reset_stats - Reset all of the stats for the given pf
  390. * @pf: the PF to be reset
  391. **/
  392. void i40e_pf_reset_stats(struct i40e_pf *pf)
  393. {
  394. int i;
  395. memset(&pf->stats, 0, sizeof(pf->stats));
  396. memset(&pf->stats_offsets, 0, sizeof(pf->stats_offsets));
  397. pf->stat_offsets_loaded = false;
  398. for (i = 0; i < I40E_MAX_VEB; i++) {
  399. if (pf->veb[i]) {
  400. memset(&pf->veb[i]->stats, 0,
  401. sizeof(pf->veb[i]->stats));
  402. memset(&pf->veb[i]->stats_offsets, 0,
  403. sizeof(pf->veb[i]->stats_offsets));
  404. pf->veb[i]->stat_offsets_loaded = false;
  405. }
  406. }
  407. }
  408. /**
  409. * i40e_stat_update48 - read and update a 48 bit stat from the chip
  410. * @hw: ptr to the hardware info
  411. * @hireg: the high 32 bit reg to read
  412. * @loreg: the low 32 bit reg to read
  413. * @offset_loaded: has the initial offset been loaded yet
  414. * @offset: ptr to current offset value
  415. * @stat: ptr to the stat
  416. *
  417. * Since the device stats are not reset at PFReset, they likely will not
  418. * be zeroed when the driver starts. We'll save the first values read
  419. * and use them as offsets to be subtracted from the raw values in order
  420. * to report stats that count from zero. In the process, we also manage
  421. * the potential roll-over.
  422. **/
  423. static void i40e_stat_update48(struct i40e_hw *hw, u32 hireg, u32 loreg,
  424. bool offset_loaded, u64 *offset, u64 *stat)
  425. {
  426. u64 new_data;
  427. if (hw->device_id == I40E_DEV_ID_QEMU) {
  428. new_data = rd32(hw, loreg);
  429. new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
  430. } else {
  431. new_data = rd64(hw, loreg);
  432. }
  433. if (!offset_loaded)
  434. *offset = new_data;
  435. if (likely(new_data >= *offset))
  436. *stat = new_data - *offset;
  437. else
  438. *stat = (new_data + ((u64)1 << 48)) - *offset;
  439. *stat &= 0xFFFFFFFFFFFFULL;
  440. }
  441. /**
  442. * i40e_stat_update32 - read and update a 32 bit stat from the chip
  443. * @hw: ptr to the hardware info
  444. * @reg: the hw reg to read
  445. * @offset_loaded: has the initial offset been loaded yet
  446. * @offset: ptr to current offset value
  447. * @stat: ptr to the stat
  448. **/
  449. static void i40e_stat_update32(struct i40e_hw *hw, u32 reg,
  450. bool offset_loaded, u64 *offset, u64 *stat)
  451. {
  452. u32 new_data;
  453. new_data = rd32(hw, reg);
  454. if (!offset_loaded)
  455. *offset = new_data;
  456. if (likely(new_data >= *offset))
  457. *stat = (u32)(new_data - *offset);
  458. else
  459. *stat = (u32)((new_data + ((u64)1 << 32)) - *offset);
  460. }
  461. /**
  462. * i40e_update_eth_stats - Update VSI-specific ethernet statistics counters.
  463. * @vsi: the VSI to be updated
  464. **/
  465. void i40e_update_eth_stats(struct i40e_vsi *vsi)
  466. {
  467. int stat_idx = le16_to_cpu(vsi->info.stat_counter_idx);
  468. struct i40e_pf *pf = vsi->back;
  469. struct i40e_hw *hw = &pf->hw;
  470. struct i40e_eth_stats *oes;
  471. struct i40e_eth_stats *es; /* device's eth stats */
  472. es = &vsi->eth_stats;
  473. oes = &vsi->eth_stats_offsets;
  474. /* Gather up the stats that the hw collects */
  475. i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
  476. vsi->stat_offsets_loaded,
  477. &oes->tx_errors, &es->tx_errors);
  478. i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx),
  479. vsi->stat_offsets_loaded,
  480. &oes->rx_discards, &es->rx_discards);
  481. i40e_stat_update32(hw, I40E_GLV_RUPP(stat_idx),
  482. vsi->stat_offsets_loaded,
  483. &oes->rx_unknown_protocol, &es->rx_unknown_protocol);
  484. i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
  485. vsi->stat_offsets_loaded,
  486. &oes->tx_errors, &es->tx_errors);
  487. i40e_stat_update48(hw, I40E_GLV_GORCH(stat_idx),
  488. I40E_GLV_GORCL(stat_idx),
  489. vsi->stat_offsets_loaded,
  490. &oes->rx_bytes, &es->rx_bytes);
  491. i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
  492. I40E_GLV_UPRCL(stat_idx),
  493. vsi->stat_offsets_loaded,
  494. &oes->rx_unicast, &es->rx_unicast);
  495. i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
  496. I40E_GLV_MPRCL(stat_idx),
  497. vsi->stat_offsets_loaded,
  498. &oes->rx_multicast, &es->rx_multicast);
  499. i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
  500. I40E_GLV_BPRCL(stat_idx),
  501. vsi->stat_offsets_loaded,
  502. &oes->rx_broadcast, &es->rx_broadcast);
  503. i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
  504. I40E_GLV_GOTCL(stat_idx),
  505. vsi->stat_offsets_loaded,
  506. &oes->tx_bytes, &es->tx_bytes);
  507. i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
  508. I40E_GLV_UPTCL(stat_idx),
  509. vsi->stat_offsets_loaded,
  510. &oes->tx_unicast, &es->tx_unicast);
  511. i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
  512. I40E_GLV_MPTCL(stat_idx),
  513. vsi->stat_offsets_loaded,
  514. &oes->tx_multicast, &es->tx_multicast);
  515. i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
  516. I40E_GLV_BPTCL(stat_idx),
  517. vsi->stat_offsets_loaded,
  518. &oes->tx_broadcast, &es->tx_broadcast);
  519. vsi->stat_offsets_loaded = true;
  520. }
  521. /**
  522. * i40e_update_veb_stats - Update Switch component statistics
  523. * @veb: the VEB being updated
  524. **/
  525. static void i40e_update_veb_stats(struct i40e_veb *veb)
  526. {
  527. struct i40e_pf *pf = veb->pf;
  528. struct i40e_hw *hw = &pf->hw;
  529. struct i40e_eth_stats *oes;
  530. struct i40e_eth_stats *es; /* device's eth stats */
  531. int idx = 0;
  532. idx = veb->stats_idx;
  533. es = &veb->stats;
  534. oes = &veb->stats_offsets;
  535. /* Gather up the stats that the hw collects */
  536. i40e_stat_update32(hw, I40E_GLSW_TDPC(idx),
  537. veb->stat_offsets_loaded,
  538. &oes->tx_discards, &es->tx_discards);
  539. if (hw->revision_id > 0)
  540. i40e_stat_update32(hw, I40E_GLSW_RUPP(idx),
  541. veb->stat_offsets_loaded,
  542. &oes->rx_unknown_protocol,
  543. &es->rx_unknown_protocol);
  544. i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx),
  545. veb->stat_offsets_loaded,
  546. &oes->rx_bytes, &es->rx_bytes);
  547. i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx),
  548. veb->stat_offsets_loaded,
  549. &oes->rx_unicast, &es->rx_unicast);
  550. i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx),
  551. veb->stat_offsets_loaded,
  552. &oes->rx_multicast, &es->rx_multicast);
  553. i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx),
  554. veb->stat_offsets_loaded,
  555. &oes->rx_broadcast, &es->rx_broadcast);
  556. i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx),
  557. veb->stat_offsets_loaded,
  558. &oes->tx_bytes, &es->tx_bytes);
  559. i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx),
  560. veb->stat_offsets_loaded,
  561. &oes->tx_unicast, &es->tx_unicast);
  562. i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx),
  563. veb->stat_offsets_loaded,
  564. &oes->tx_multicast, &es->tx_multicast);
  565. i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx),
  566. veb->stat_offsets_loaded,
  567. &oes->tx_broadcast, &es->tx_broadcast);
  568. veb->stat_offsets_loaded = true;
  569. }
  570. /**
  571. * i40e_update_link_xoff_rx - Update XOFF received in link flow control mode
  572. * @pf: the corresponding PF
  573. *
  574. * Update the Rx XOFF counter (PAUSE frames) in link flow control mode
  575. **/
  576. static void i40e_update_link_xoff_rx(struct i40e_pf *pf)
  577. {
  578. struct i40e_hw_port_stats *osd = &pf->stats_offsets;
  579. struct i40e_hw_port_stats *nsd = &pf->stats;
  580. struct i40e_hw *hw = &pf->hw;
  581. u64 xoff = 0;
  582. u16 i, v;
  583. if ((hw->fc.current_mode != I40E_FC_FULL) &&
  584. (hw->fc.current_mode != I40E_FC_RX_PAUSE))
  585. return;
  586. xoff = nsd->link_xoff_rx;
  587. i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
  588. pf->stat_offsets_loaded,
  589. &osd->link_xoff_rx, &nsd->link_xoff_rx);
  590. /* No new LFC xoff rx */
  591. if (!(nsd->link_xoff_rx - xoff))
  592. return;
  593. /* Clear the __I40E_HANG_CHECK_ARMED bit for all Tx rings */
  594. for (v = 0; v < pf->num_alloc_vsi; v++) {
  595. struct i40e_vsi *vsi = pf->vsi[v];
  596. if (!vsi || !vsi->tx_rings[0])
  597. continue;
  598. for (i = 0; i < vsi->num_queue_pairs; i++) {
  599. struct i40e_ring *ring = vsi->tx_rings[i];
  600. clear_bit(__I40E_HANG_CHECK_ARMED, &ring->state);
  601. }
  602. }
  603. }
  604. /**
  605. * i40e_update_prio_xoff_rx - Update XOFF received in PFC mode
  606. * @pf: the corresponding PF
  607. *
  608. * Update the Rx XOFF counter (PAUSE frames) in PFC mode
  609. **/
  610. static void i40e_update_prio_xoff_rx(struct i40e_pf *pf)
  611. {
  612. struct i40e_hw_port_stats *osd = &pf->stats_offsets;
  613. struct i40e_hw_port_stats *nsd = &pf->stats;
  614. bool xoff[I40E_MAX_TRAFFIC_CLASS] = {false};
  615. struct i40e_dcbx_config *dcb_cfg;
  616. struct i40e_hw *hw = &pf->hw;
  617. u16 i, v;
  618. u8 tc;
  619. dcb_cfg = &hw->local_dcbx_config;
  620. /* See if DCB enabled with PFC TC */
  621. if (!(pf->flags & I40E_FLAG_DCB_ENABLED) ||
  622. !(dcb_cfg->pfc.pfcenable)) {
  623. i40e_update_link_xoff_rx(pf);
  624. return;
  625. }
  626. for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
  627. u64 prio_xoff = nsd->priority_xoff_rx[i];
  628. i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
  629. pf->stat_offsets_loaded,
  630. &osd->priority_xoff_rx[i],
  631. &nsd->priority_xoff_rx[i]);
  632. /* No new PFC xoff rx */
  633. if (!(nsd->priority_xoff_rx[i] - prio_xoff))
  634. continue;
  635. /* Get the TC for given priority */
  636. tc = dcb_cfg->etscfg.prioritytable[i];
  637. xoff[tc] = true;
  638. }
  639. /* Clear the __I40E_HANG_CHECK_ARMED bit for Tx rings */
  640. for (v = 0; v < pf->num_alloc_vsi; v++) {
  641. struct i40e_vsi *vsi = pf->vsi[v];
  642. if (!vsi || !vsi->tx_rings[0])
  643. continue;
  644. for (i = 0; i < vsi->num_queue_pairs; i++) {
  645. struct i40e_ring *ring = vsi->tx_rings[i];
  646. tc = ring->dcb_tc;
  647. if (xoff[tc])
  648. clear_bit(__I40E_HANG_CHECK_ARMED,
  649. &ring->state);
  650. }
  651. }
  652. }
  653. /**
  654. * i40e_update_vsi_stats - Update the vsi statistics counters.
  655. * @vsi: the VSI to be updated
  656. *
  657. * There are a few instances where we store the same stat in a
  658. * couple of different structs. This is partly because we have
  659. * the netdev stats that need to be filled out, which is slightly
  660. * different from the "eth_stats" defined by the chip and used in
  661. * VF communications. We sort it out here.
  662. **/
  663. static void i40e_update_vsi_stats(struct i40e_vsi *vsi)
  664. {
  665. struct i40e_pf *pf = vsi->back;
  666. struct rtnl_link_stats64 *ons;
  667. struct rtnl_link_stats64 *ns; /* netdev stats */
  668. struct i40e_eth_stats *oes;
  669. struct i40e_eth_stats *es; /* device's eth stats */
  670. u32 tx_restart, tx_busy;
  671. u32 rx_page, rx_buf;
  672. u64 rx_p, rx_b;
  673. u64 tx_p, tx_b;
  674. u16 q;
  675. if (test_bit(__I40E_DOWN, &vsi->state) ||
  676. test_bit(__I40E_CONFIG_BUSY, &pf->state))
  677. return;
  678. ns = i40e_get_vsi_stats_struct(vsi);
  679. ons = &vsi->net_stats_offsets;
  680. es = &vsi->eth_stats;
  681. oes = &vsi->eth_stats_offsets;
  682. /* Gather up the netdev and vsi stats that the driver collects
  683. * on the fly during packet processing
  684. */
  685. rx_b = rx_p = 0;
  686. tx_b = tx_p = 0;
  687. tx_restart = tx_busy = 0;
  688. rx_page = 0;
  689. rx_buf = 0;
  690. rcu_read_lock();
  691. for (q = 0; q < vsi->num_queue_pairs; q++) {
  692. struct i40e_ring *p;
  693. u64 bytes, packets;
  694. unsigned int start;
  695. /* locate Tx ring */
  696. p = ACCESS_ONCE(vsi->tx_rings[q]);
  697. do {
  698. start = u64_stats_fetch_begin_irq(&p->syncp);
  699. packets = p->stats.packets;
  700. bytes = p->stats.bytes;
  701. } while (u64_stats_fetch_retry_irq(&p->syncp, start));
  702. tx_b += bytes;
  703. tx_p += packets;
  704. tx_restart += p->tx_stats.restart_queue;
  705. tx_busy += p->tx_stats.tx_busy;
  706. /* Rx queue is part of the same block as Tx queue */
  707. p = &p[1];
  708. do {
  709. start = u64_stats_fetch_begin_irq(&p->syncp);
  710. packets = p->stats.packets;
  711. bytes = p->stats.bytes;
  712. } while (u64_stats_fetch_retry_irq(&p->syncp, start));
  713. rx_b += bytes;
  714. rx_p += packets;
  715. rx_buf += p->rx_stats.alloc_buff_failed;
  716. rx_page += p->rx_stats.alloc_page_failed;
  717. }
  718. rcu_read_unlock();
  719. vsi->tx_restart = tx_restart;
  720. vsi->tx_busy = tx_busy;
  721. vsi->rx_page_failed = rx_page;
  722. vsi->rx_buf_failed = rx_buf;
  723. ns->rx_packets = rx_p;
  724. ns->rx_bytes = rx_b;
  725. ns->tx_packets = tx_p;
  726. ns->tx_bytes = tx_b;
  727. /* update netdev stats from eth stats */
  728. i40e_update_eth_stats(vsi);
  729. ons->tx_errors = oes->tx_errors;
  730. ns->tx_errors = es->tx_errors;
  731. ons->multicast = oes->rx_multicast;
  732. ns->multicast = es->rx_multicast;
  733. ons->rx_dropped = oes->rx_discards;
  734. ns->rx_dropped = es->rx_discards;
  735. ons->tx_dropped = oes->tx_discards;
  736. ns->tx_dropped = es->tx_discards;
  737. /* pull in a couple PF stats if this is the main vsi */
  738. if (vsi == pf->vsi[pf->lan_vsi]) {
  739. ns->rx_crc_errors = pf->stats.crc_errors;
  740. ns->rx_errors = pf->stats.crc_errors + pf->stats.illegal_bytes;
  741. ns->rx_length_errors = pf->stats.rx_length_errors;
  742. }
  743. }
  744. /**
  745. * i40e_update_pf_stats - Update the pf statistics counters.
  746. * @pf: the PF to be updated
  747. **/
  748. static void i40e_update_pf_stats(struct i40e_pf *pf)
  749. {
  750. struct i40e_hw_port_stats *osd = &pf->stats_offsets;
  751. struct i40e_hw_port_stats *nsd = &pf->stats;
  752. struct i40e_hw *hw = &pf->hw;
  753. u32 val;
  754. int i;
  755. i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
  756. I40E_GLPRT_GORCL(hw->port),
  757. pf->stat_offsets_loaded,
  758. &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
  759. i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
  760. I40E_GLPRT_GOTCL(hw->port),
  761. pf->stat_offsets_loaded,
  762. &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
  763. i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
  764. pf->stat_offsets_loaded,
  765. &osd->eth.rx_discards,
  766. &nsd->eth.rx_discards);
  767. i40e_stat_update32(hw, I40E_GLPRT_TDPC(hw->port),
  768. pf->stat_offsets_loaded,
  769. &osd->eth.tx_discards,
  770. &nsd->eth.tx_discards);
  771. i40e_stat_update48(hw, I40E_GLPRT_UPRCH(hw->port),
  772. I40E_GLPRT_UPRCL(hw->port),
  773. pf->stat_offsets_loaded,
  774. &osd->eth.rx_unicast,
  775. &nsd->eth.rx_unicast);
  776. i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
  777. I40E_GLPRT_MPRCL(hw->port),
  778. pf->stat_offsets_loaded,
  779. &osd->eth.rx_multicast,
  780. &nsd->eth.rx_multicast);
  781. i40e_stat_update48(hw, I40E_GLPRT_BPRCH(hw->port),
  782. I40E_GLPRT_BPRCL(hw->port),
  783. pf->stat_offsets_loaded,
  784. &osd->eth.rx_broadcast,
  785. &nsd->eth.rx_broadcast);
  786. i40e_stat_update48(hw, I40E_GLPRT_UPTCH(hw->port),
  787. I40E_GLPRT_UPTCL(hw->port),
  788. pf->stat_offsets_loaded,
  789. &osd->eth.tx_unicast,
  790. &nsd->eth.tx_unicast);
  791. i40e_stat_update48(hw, I40E_GLPRT_MPTCH(hw->port),
  792. I40E_GLPRT_MPTCL(hw->port),
  793. pf->stat_offsets_loaded,
  794. &osd->eth.tx_multicast,
  795. &nsd->eth.tx_multicast);
  796. i40e_stat_update48(hw, I40E_GLPRT_BPTCH(hw->port),
  797. I40E_GLPRT_BPTCL(hw->port),
  798. pf->stat_offsets_loaded,
  799. &osd->eth.tx_broadcast,
  800. &nsd->eth.tx_broadcast);
  801. i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
  802. pf->stat_offsets_loaded,
  803. &osd->tx_dropped_link_down,
  804. &nsd->tx_dropped_link_down);
  805. i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
  806. pf->stat_offsets_loaded,
  807. &osd->crc_errors, &nsd->crc_errors);
  808. i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
  809. pf->stat_offsets_loaded,
  810. &osd->illegal_bytes, &nsd->illegal_bytes);
  811. i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
  812. pf->stat_offsets_loaded,
  813. &osd->mac_local_faults,
  814. &nsd->mac_local_faults);
  815. i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
  816. pf->stat_offsets_loaded,
  817. &osd->mac_remote_faults,
  818. &nsd->mac_remote_faults);
  819. i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
  820. pf->stat_offsets_loaded,
  821. &osd->rx_length_errors,
  822. &nsd->rx_length_errors);
  823. i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
  824. pf->stat_offsets_loaded,
  825. &osd->link_xon_rx, &nsd->link_xon_rx);
  826. i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
  827. pf->stat_offsets_loaded,
  828. &osd->link_xon_tx, &nsd->link_xon_tx);
  829. i40e_update_prio_xoff_rx(pf); /* handles I40E_GLPRT_LXOFFRXC */
  830. i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
  831. pf->stat_offsets_loaded,
  832. &osd->link_xoff_tx, &nsd->link_xoff_tx);
  833. for (i = 0; i < 8; i++) {
  834. i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
  835. pf->stat_offsets_loaded,
  836. &osd->priority_xon_rx[i],
  837. &nsd->priority_xon_rx[i]);
  838. i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
  839. pf->stat_offsets_loaded,
  840. &osd->priority_xon_tx[i],
  841. &nsd->priority_xon_tx[i]);
  842. i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
  843. pf->stat_offsets_loaded,
  844. &osd->priority_xoff_tx[i],
  845. &nsd->priority_xoff_tx[i]);
  846. i40e_stat_update32(hw,
  847. I40E_GLPRT_RXON2OFFCNT(hw->port, i),
  848. pf->stat_offsets_loaded,
  849. &osd->priority_xon_2_xoff[i],
  850. &nsd->priority_xon_2_xoff[i]);
  851. }
  852. i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
  853. I40E_GLPRT_PRC64L(hw->port),
  854. pf->stat_offsets_loaded,
  855. &osd->rx_size_64, &nsd->rx_size_64);
  856. i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
  857. I40E_GLPRT_PRC127L(hw->port),
  858. pf->stat_offsets_loaded,
  859. &osd->rx_size_127, &nsd->rx_size_127);
  860. i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
  861. I40E_GLPRT_PRC255L(hw->port),
  862. pf->stat_offsets_loaded,
  863. &osd->rx_size_255, &nsd->rx_size_255);
  864. i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
  865. I40E_GLPRT_PRC511L(hw->port),
  866. pf->stat_offsets_loaded,
  867. &osd->rx_size_511, &nsd->rx_size_511);
  868. i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
  869. I40E_GLPRT_PRC1023L(hw->port),
  870. pf->stat_offsets_loaded,
  871. &osd->rx_size_1023, &nsd->rx_size_1023);
  872. i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
  873. I40E_GLPRT_PRC1522L(hw->port),
  874. pf->stat_offsets_loaded,
  875. &osd->rx_size_1522, &nsd->rx_size_1522);
  876. i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
  877. I40E_GLPRT_PRC9522L(hw->port),
  878. pf->stat_offsets_loaded,
  879. &osd->rx_size_big, &nsd->rx_size_big);
  880. i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
  881. I40E_GLPRT_PTC64L(hw->port),
  882. pf->stat_offsets_loaded,
  883. &osd->tx_size_64, &nsd->tx_size_64);
  884. i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
  885. I40E_GLPRT_PTC127L(hw->port),
  886. pf->stat_offsets_loaded,
  887. &osd->tx_size_127, &nsd->tx_size_127);
  888. i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
  889. I40E_GLPRT_PTC255L(hw->port),
  890. pf->stat_offsets_loaded,
  891. &osd->tx_size_255, &nsd->tx_size_255);
  892. i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
  893. I40E_GLPRT_PTC511L(hw->port),
  894. pf->stat_offsets_loaded,
  895. &osd->tx_size_511, &nsd->tx_size_511);
  896. i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
  897. I40E_GLPRT_PTC1023L(hw->port),
  898. pf->stat_offsets_loaded,
  899. &osd->tx_size_1023, &nsd->tx_size_1023);
  900. i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
  901. I40E_GLPRT_PTC1522L(hw->port),
  902. pf->stat_offsets_loaded,
  903. &osd->tx_size_1522, &nsd->tx_size_1522);
  904. i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
  905. I40E_GLPRT_PTC9522L(hw->port),
  906. pf->stat_offsets_loaded,
  907. &osd->tx_size_big, &nsd->tx_size_big);
  908. i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
  909. pf->stat_offsets_loaded,
  910. &osd->rx_undersize, &nsd->rx_undersize);
  911. i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
  912. pf->stat_offsets_loaded,
  913. &osd->rx_fragments, &nsd->rx_fragments);
  914. i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
  915. pf->stat_offsets_loaded,
  916. &osd->rx_oversize, &nsd->rx_oversize);
  917. i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
  918. pf->stat_offsets_loaded,
  919. &osd->rx_jabber, &nsd->rx_jabber);
  920. /* FDIR stats */
  921. i40e_stat_update32(hw, I40E_GLQF_PCNT(pf->fd_atr_cnt_idx),
  922. pf->stat_offsets_loaded,
  923. &osd->fd_atr_match, &nsd->fd_atr_match);
  924. i40e_stat_update32(hw, I40E_GLQF_PCNT(pf->fd_sb_cnt_idx),
  925. pf->stat_offsets_loaded,
  926. &osd->fd_sb_match, &nsd->fd_sb_match);
  927. val = rd32(hw, I40E_PRTPM_EEE_STAT);
  928. nsd->tx_lpi_status =
  929. (val & I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_MASK) >>
  930. I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_SHIFT;
  931. nsd->rx_lpi_status =
  932. (val & I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_MASK) >>
  933. I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_SHIFT;
  934. i40e_stat_update32(hw, I40E_PRTPM_TLPIC,
  935. pf->stat_offsets_loaded,
  936. &osd->tx_lpi_count, &nsd->tx_lpi_count);
  937. i40e_stat_update32(hw, I40E_PRTPM_RLPIC,
  938. pf->stat_offsets_loaded,
  939. &osd->rx_lpi_count, &nsd->rx_lpi_count);
  940. pf->stat_offsets_loaded = true;
  941. }
  942. /**
  943. * i40e_update_stats - Update the various statistics counters.
  944. * @vsi: the VSI to be updated
  945. *
  946. * Update the various stats for this VSI and its related entities.
  947. **/
  948. void i40e_update_stats(struct i40e_vsi *vsi)
  949. {
  950. struct i40e_pf *pf = vsi->back;
  951. if (vsi == pf->vsi[pf->lan_vsi])
  952. i40e_update_pf_stats(pf);
  953. i40e_update_vsi_stats(vsi);
  954. }
  955. /**
  956. * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
  957. * @vsi: the VSI to be searched
  958. * @macaddr: the MAC address
  959. * @vlan: the vlan
  960. * @is_vf: make sure its a vf filter, else doesn't matter
  961. * @is_netdev: make sure its a netdev filter, else doesn't matter
  962. *
  963. * Returns ptr to the filter object or NULL
  964. **/
  965. static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
  966. u8 *macaddr, s16 vlan,
  967. bool is_vf, bool is_netdev)
  968. {
  969. struct i40e_mac_filter *f;
  970. if (!vsi || !macaddr)
  971. return NULL;
  972. list_for_each_entry(f, &vsi->mac_filter_list, list) {
  973. if ((ether_addr_equal(macaddr, f->macaddr)) &&
  974. (vlan == f->vlan) &&
  975. (!is_vf || f->is_vf) &&
  976. (!is_netdev || f->is_netdev))
  977. return f;
  978. }
  979. return NULL;
  980. }
  981. /**
  982. * i40e_find_mac - Find a mac addr in the macvlan filters list
  983. * @vsi: the VSI to be searched
  984. * @macaddr: the MAC address we are searching for
  985. * @is_vf: make sure its a vf filter, else doesn't matter
  986. * @is_netdev: make sure its a netdev filter, else doesn't matter
  987. *
  988. * Returns the first filter with the provided MAC address or NULL if
  989. * MAC address was not found
  990. **/
  991. struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, u8 *macaddr,
  992. bool is_vf, bool is_netdev)
  993. {
  994. struct i40e_mac_filter *f;
  995. if (!vsi || !macaddr)
  996. return NULL;
  997. list_for_each_entry(f, &vsi->mac_filter_list, list) {
  998. if ((ether_addr_equal(macaddr, f->macaddr)) &&
  999. (!is_vf || f->is_vf) &&
  1000. (!is_netdev || f->is_netdev))
  1001. return f;
  1002. }
  1003. return NULL;
  1004. }
  1005. /**
  1006. * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
  1007. * @vsi: the VSI to be searched
  1008. *
  1009. * Returns true if VSI is in vlan mode or false otherwise
  1010. **/
  1011. bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
  1012. {
  1013. struct i40e_mac_filter *f;
  1014. /* Only -1 for all the filters denotes not in vlan mode
  1015. * so we have to go through all the list in order to make sure
  1016. */
  1017. list_for_each_entry(f, &vsi->mac_filter_list, list) {
  1018. if (f->vlan >= 0)
  1019. return true;
  1020. }
  1021. return false;
  1022. }
  1023. /**
  1024. * i40e_put_mac_in_vlan - Make macvlan filters from macaddrs and vlans
  1025. * @vsi: the VSI to be searched
  1026. * @macaddr: the mac address to be filtered
  1027. * @is_vf: true if it is a vf
  1028. * @is_netdev: true if it is a netdev
  1029. *
  1030. * Goes through all the macvlan filters and adds a
  1031. * macvlan filter for each unique vlan that already exists
  1032. *
  1033. * Returns first filter found on success, else NULL
  1034. **/
  1035. struct i40e_mac_filter *i40e_put_mac_in_vlan(struct i40e_vsi *vsi, u8 *macaddr,
  1036. bool is_vf, bool is_netdev)
  1037. {
  1038. struct i40e_mac_filter *f;
  1039. list_for_each_entry(f, &vsi->mac_filter_list, list) {
  1040. if (!i40e_find_filter(vsi, macaddr, f->vlan,
  1041. is_vf, is_netdev)) {
  1042. if (!i40e_add_filter(vsi, macaddr, f->vlan,
  1043. is_vf, is_netdev))
  1044. return NULL;
  1045. }
  1046. }
  1047. return list_first_entry_or_null(&vsi->mac_filter_list,
  1048. struct i40e_mac_filter, list);
  1049. }
  1050. /**
  1051. * i40e_rm_default_mac_filter - Remove the default MAC filter set by NVM
  1052. * @vsi: the PF Main VSI - inappropriate for any other VSI
  1053. * @macaddr: the MAC address
  1054. **/
  1055. static void i40e_rm_default_mac_filter(struct i40e_vsi *vsi, u8 *macaddr)
  1056. {
  1057. struct i40e_aqc_remove_macvlan_element_data element;
  1058. struct i40e_pf *pf = vsi->back;
  1059. i40e_status aq_ret;
  1060. /* Only appropriate for the PF main VSI */
  1061. if (vsi->type != I40E_VSI_MAIN)
  1062. return;
  1063. ether_addr_copy(element.mac_addr, macaddr);
  1064. element.vlan_tag = 0;
  1065. element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH |
  1066. I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
  1067. aq_ret = i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
  1068. if (aq_ret)
  1069. dev_err(&pf->pdev->dev, "Could not remove default MAC-VLAN\n");
  1070. }
  1071. /**
  1072. * i40e_add_filter - Add a mac/vlan filter to the VSI
  1073. * @vsi: the VSI to be searched
  1074. * @macaddr: the MAC address
  1075. * @vlan: the vlan
  1076. * @is_vf: make sure its a vf filter, else doesn't matter
  1077. * @is_netdev: make sure its a netdev filter, else doesn't matter
  1078. *
  1079. * Returns ptr to the filter object or NULL when no memory available.
  1080. **/
  1081. struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
  1082. u8 *macaddr, s16 vlan,
  1083. bool is_vf, bool is_netdev)
  1084. {
  1085. struct i40e_mac_filter *f;
  1086. if (!vsi || !macaddr)
  1087. return NULL;
  1088. f = i40e_find_filter(vsi, macaddr, vlan, is_vf, is_netdev);
  1089. if (!f) {
  1090. f = kzalloc(sizeof(*f), GFP_ATOMIC);
  1091. if (!f)
  1092. goto add_filter_out;
  1093. ether_addr_copy(f->macaddr, macaddr);
  1094. f->vlan = vlan;
  1095. f->changed = true;
  1096. INIT_LIST_HEAD(&f->list);
  1097. list_add(&f->list, &vsi->mac_filter_list);
  1098. }
  1099. /* increment counter and add a new flag if needed */
  1100. if (is_vf) {
  1101. if (!f->is_vf) {
  1102. f->is_vf = true;
  1103. f->counter++;
  1104. }
  1105. } else if (is_netdev) {
  1106. if (!f->is_netdev) {
  1107. f->is_netdev = true;
  1108. f->counter++;
  1109. }
  1110. } else {
  1111. f->counter++;
  1112. }
  1113. /* changed tells sync_filters_subtask to
  1114. * push the filter down to the firmware
  1115. */
  1116. if (f->changed) {
  1117. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  1118. vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
  1119. }
  1120. add_filter_out:
  1121. return f;
  1122. }
  1123. /**
  1124. * i40e_del_filter - Remove a mac/vlan filter from the VSI
  1125. * @vsi: the VSI to be searched
  1126. * @macaddr: the MAC address
  1127. * @vlan: the vlan
  1128. * @is_vf: make sure it's a vf filter, else doesn't matter
  1129. * @is_netdev: make sure it's a netdev filter, else doesn't matter
  1130. **/
  1131. void i40e_del_filter(struct i40e_vsi *vsi,
  1132. u8 *macaddr, s16 vlan,
  1133. bool is_vf, bool is_netdev)
  1134. {
  1135. struct i40e_mac_filter *f;
  1136. if (!vsi || !macaddr)
  1137. return;
  1138. f = i40e_find_filter(vsi, macaddr, vlan, is_vf, is_netdev);
  1139. if (!f || f->counter == 0)
  1140. return;
  1141. if (is_vf) {
  1142. if (f->is_vf) {
  1143. f->is_vf = false;
  1144. f->counter--;
  1145. }
  1146. } else if (is_netdev) {
  1147. if (f->is_netdev) {
  1148. f->is_netdev = false;
  1149. f->counter--;
  1150. }
  1151. } else {
  1152. /* make sure we don't remove a filter in use by vf or netdev */
  1153. int min_f = 0;
  1154. min_f += (f->is_vf ? 1 : 0);
  1155. min_f += (f->is_netdev ? 1 : 0);
  1156. if (f->counter > min_f)
  1157. f->counter--;
  1158. }
  1159. /* counter == 0 tells sync_filters_subtask to
  1160. * remove the filter from the firmware's list
  1161. */
  1162. if (f->counter == 0) {
  1163. f->changed = true;
  1164. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  1165. vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
  1166. }
  1167. }
  1168. /**
  1169. * i40e_set_mac - NDO callback to set mac address
  1170. * @netdev: network interface device structure
  1171. * @p: pointer to an address structure
  1172. *
  1173. * Returns 0 on success, negative on failure
  1174. **/
  1175. static int i40e_set_mac(struct net_device *netdev, void *p)
  1176. {
  1177. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1178. struct i40e_vsi *vsi = np->vsi;
  1179. struct sockaddr *addr = p;
  1180. struct i40e_mac_filter *f;
  1181. if (!is_valid_ether_addr(addr->sa_data))
  1182. return -EADDRNOTAVAIL;
  1183. netdev_info(netdev, "set mac address=%pM\n", addr->sa_data);
  1184. if (test_bit(__I40E_DOWN, &vsi->back->state) ||
  1185. test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
  1186. return -EADDRNOTAVAIL;
  1187. if (vsi->type == I40E_VSI_MAIN) {
  1188. i40e_status ret;
  1189. ret = i40e_aq_mac_address_write(&vsi->back->hw,
  1190. I40E_AQC_WRITE_TYPE_LAA_WOL,
  1191. addr->sa_data, NULL);
  1192. if (ret) {
  1193. netdev_info(netdev,
  1194. "Addr change for Main VSI failed: %d\n",
  1195. ret);
  1196. return -EADDRNOTAVAIL;
  1197. }
  1198. }
  1199. if (!i40e_find_mac(vsi, addr->sa_data, false, true)) {
  1200. /* In order to be sure to not drop any packets, add the
  1201. * new address first then delete the old one.
  1202. */
  1203. f = i40e_add_filter(vsi, addr->sa_data, I40E_VLAN_ANY,
  1204. false, false);
  1205. if (!f)
  1206. return -ENOMEM;
  1207. i40e_sync_vsi_filters(vsi);
  1208. i40e_del_filter(vsi, netdev->dev_addr, I40E_VLAN_ANY,
  1209. false, false);
  1210. i40e_sync_vsi_filters(vsi);
  1211. }
  1212. if (!ether_addr_equal(netdev->dev_addr, addr->sa_data))
  1213. ether_addr_copy(netdev->dev_addr, addr->sa_data);
  1214. return 0;
  1215. }
  1216. /**
  1217. * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
  1218. * @vsi: the VSI being setup
  1219. * @ctxt: VSI context structure
  1220. * @enabled_tc: Enabled TCs bitmap
  1221. * @is_add: True if called before Add VSI
  1222. *
  1223. * Setup VSI queue mapping for enabled traffic classes.
  1224. **/
  1225. static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
  1226. struct i40e_vsi_context *ctxt,
  1227. u8 enabled_tc,
  1228. bool is_add)
  1229. {
  1230. struct i40e_pf *pf = vsi->back;
  1231. u16 sections = 0;
  1232. u8 netdev_tc = 0;
  1233. u16 numtc = 0;
  1234. u16 qcount;
  1235. u8 offset;
  1236. u16 qmap;
  1237. int i;
  1238. u16 num_tc_qps = 0;
  1239. sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
  1240. offset = 0;
  1241. if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
  1242. /* Find numtc from enabled TC bitmap */
  1243. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  1244. if (enabled_tc & (1 << i)) /* TC is enabled */
  1245. numtc++;
  1246. }
  1247. if (!numtc) {
  1248. dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
  1249. numtc = 1;
  1250. }
  1251. } else {
  1252. /* At least TC0 is enabled in case of non-DCB case */
  1253. numtc = 1;
  1254. }
  1255. vsi->tc_config.numtc = numtc;
  1256. vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
  1257. /* Number of queues per enabled TC */
  1258. num_tc_qps = vsi->alloc_queue_pairs/numtc;
  1259. num_tc_qps = min_t(int, num_tc_qps, I40E_MAX_QUEUES_PER_TC);
  1260. /* Setup queue offset/count for all TCs for given VSI */
  1261. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  1262. /* See if the given TC is enabled for the given VSI */
  1263. if (vsi->tc_config.enabled_tc & (1 << i)) { /* TC is enabled */
  1264. int pow, num_qps;
  1265. switch (vsi->type) {
  1266. case I40E_VSI_MAIN:
  1267. qcount = min_t(int, pf->rss_size, num_tc_qps);
  1268. break;
  1269. case I40E_VSI_FDIR:
  1270. case I40E_VSI_SRIOV:
  1271. case I40E_VSI_VMDQ2:
  1272. default:
  1273. qcount = num_tc_qps;
  1274. WARN_ON(i != 0);
  1275. break;
  1276. }
  1277. vsi->tc_config.tc_info[i].qoffset = offset;
  1278. vsi->tc_config.tc_info[i].qcount = qcount;
  1279. /* find the power-of-2 of the number of queue pairs */
  1280. num_qps = qcount;
  1281. pow = 0;
  1282. while (num_qps && ((1 << pow) < qcount)) {
  1283. pow++;
  1284. num_qps >>= 1;
  1285. }
  1286. vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
  1287. qmap =
  1288. (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
  1289. (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
  1290. offset += qcount;
  1291. } else {
  1292. /* TC is not enabled so set the offset to
  1293. * default queue and allocate one queue
  1294. * for the given TC.
  1295. */
  1296. vsi->tc_config.tc_info[i].qoffset = 0;
  1297. vsi->tc_config.tc_info[i].qcount = 1;
  1298. vsi->tc_config.tc_info[i].netdev_tc = 0;
  1299. qmap = 0;
  1300. }
  1301. ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
  1302. }
  1303. /* Set actual Tx/Rx queue pairs */
  1304. vsi->num_queue_pairs = offset;
  1305. /* Scheduler section valid can only be set for ADD VSI */
  1306. if (is_add) {
  1307. sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
  1308. ctxt->info.up_enable_bits = enabled_tc;
  1309. }
  1310. if (vsi->type == I40E_VSI_SRIOV) {
  1311. ctxt->info.mapping_flags |=
  1312. cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
  1313. for (i = 0; i < vsi->num_queue_pairs; i++)
  1314. ctxt->info.queue_mapping[i] =
  1315. cpu_to_le16(vsi->base_queue + i);
  1316. } else {
  1317. ctxt->info.mapping_flags |=
  1318. cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
  1319. ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
  1320. }
  1321. ctxt->info.valid_sections |= cpu_to_le16(sections);
  1322. }
  1323. /**
  1324. * i40e_set_rx_mode - NDO callback to set the netdev filters
  1325. * @netdev: network interface device structure
  1326. **/
  1327. static void i40e_set_rx_mode(struct net_device *netdev)
  1328. {
  1329. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1330. struct i40e_mac_filter *f, *ftmp;
  1331. struct i40e_vsi *vsi = np->vsi;
  1332. struct netdev_hw_addr *uca;
  1333. struct netdev_hw_addr *mca;
  1334. struct netdev_hw_addr *ha;
  1335. /* add addr if not already in the filter list */
  1336. netdev_for_each_uc_addr(uca, netdev) {
  1337. if (!i40e_find_mac(vsi, uca->addr, false, true)) {
  1338. if (i40e_is_vsi_in_vlan(vsi))
  1339. i40e_put_mac_in_vlan(vsi, uca->addr,
  1340. false, true);
  1341. else
  1342. i40e_add_filter(vsi, uca->addr, I40E_VLAN_ANY,
  1343. false, true);
  1344. }
  1345. }
  1346. netdev_for_each_mc_addr(mca, netdev) {
  1347. if (!i40e_find_mac(vsi, mca->addr, false, true)) {
  1348. if (i40e_is_vsi_in_vlan(vsi))
  1349. i40e_put_mac_in_vlan(vsi, mca->addr,
  1350. false, true);
  1351. else
  1352. i40e_add_filter(vsi, mca->addr, I40E_VLAN_ANY,
  1353. false, true);
  1354. }
  1355. }
  1356. /* remove filter if not in netdev list */
  1357. list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
  1358. bool found = false;
  1359. if (!f->is_netdev)
  1360. continue;
  1361. if (is_multicast_ether_addr(f->macaddr)) {
  1362. netdev_for_each_mc_addr(mca, netdev) {
  1363. if (ether_addr_equal(mca->addr, f->macaddr)) {
  1364. found = true;
  1365. break;
  1366. }
  1367. }
  1368. } else {
  1369. netdev_for_each_uc_addr(uca, netdev) {
  1370. if (ether_addr_equal(uca->addr, f->macaddr)) {
  1371. found = true;
  1372. break;
  1373. }
  1374. }
  1375. for_each_dev_addr(netdev, ha) {
  1376. if (ether_addr_equal(ha->addr, f->macaddr)) {
  1377. found = true;
  1378. break;
  1379. }
  1380. }
  1381. }
  1382. if (!found)
  1383. i40e_del_filter(
  1384. vsi, f->macaddr, I40E_VLAN_ANY, false, true);
  1385. }
  1386. /* check for other flag changes */
  1387. if (vsi->current_netdev_flags != vsi->netdev->flags) {
  1388. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  1389. vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
  1390. }
  1391. }
  1392. /**
  1393. * i40e_sync_vsi_filters - Update the VSI filter list to the HW
  1394. * @vsi: ptr to the VSI
  1395. *
  1396. * Push any outstanding VSI filter changes through the AdminQ.
  1397. *
  1398. * Returns 0 or error value
  1399. **/
  1400. int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
  1401. {
  1402. struct i40e_mac_filter *f, *ftmp;
  1403. bool promisc_forced_on = false;
  1404. bool add_happened = false;
  1405. int filter_list_len = 0;
  1406. u32 changed_flags = 0;
  1407. i40e_status aq_ret = 0;
  1408. struct i40e_pf *pf;
  1409. int num_add = 0;
  1410. int num_del = 0;
  1411. u16 cmd_flags;
  1412. /* empty array typed pointers, kcalloc later */
  1413. struct i40e_aqc_add_macvlan_element_data *add_list;
  1414. struct i40e_aqc_remove_macvlan_element_data *del_list;
  1415. while (test_and_set_bit(__I40E_CONFIG_BUSY, &vsi->state))
  1416. usleep_range(1000, 2000);
  1417. pf = vsi->back;
  1418. if (vsi->netdev) {
  1419. changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
  1420. vsi->current_netdev_flags = vsi->netdev->flags;
  1421. }
  1422. if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
  1423. vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
  1424. filter_list_len = pf->hw.aq.asq_buf_size /
  1425. sizeof(struct i40e_aqc_remove_macvlan_element_data);
  1426. del_list = kcalloc(filter_list_len,
  1427. sizeof(struct i40e_aqc_remove_macvlan_element_data),
  1428. GFP_KERNEL);
  1429. if (!del_list)
  1430. return -ENOMEM;
  1431. list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
  1432. if (!f->changed)
  1433. continue;
  1434. if (f->counter != 0)
  1435. continue;
  1436. f->changed = false;
  1437. cmd_flags = 0;
  1438. /* add to delete list */
  1439. ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
  1440. del_list[num_del].vlan_tag =
  1441. cpu_to_le16((u16)(f->vlan ==
  1442. I40E_VLAN_ANY ? 0 : f->vlan));
  1443. cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
  1444. del_list[num_del].flags = cmd_flags;
  1445. num_del++;
  1446. /* unlink from filter list */
  1447. list_del(&f->list);
  1448. kfree(f);
  1449. /* flush a full buffer */
  1450. if (num_del == filter_list_len) {
  1451. aq_ret = i40e_aq_remove_macvlan(&pf->hw,
  1452. vsi->seid, del_list, num_del,
  1453. NULL);
  1454. num_del = 0;
  1455. memset(del_list, 0, sizeof(*del_list));
  1456. if (aq_ret &&
  1457. pf->hw.aq.asq_last_status !=
  1458. I40E_AQ_RC_ENOENT)
  1459. dev_info(&pf->pdev->dev,
  1460. "ignoring delete macvlan error, err %d, aq_err %d while flushing a full buffer\n",
  1461. aq_ret,
  1462. pf->hw.aq.asq_last_status);
  1463. }
  1464. }
  1465. if (num_del) {
  1466. aq_ret = i40e_aq_remove_macvlan(&pf->hw, vsi->seid,
  1467. del_list, num_del, NULL);
  1468. num_del = 0;
  1469. if (aq_ret &&
  1470. pf->hw.aq.asq_last_status != I40E_AQ_RC_ENOENT)
  1471. dev_info(&pf->pdev->dev,
  1472. "ignoring delete macvlan error, err %d, aq_err %d\n",
  1473. aq_ret, pf->hw.aq.asq_last_status);
  1474. }
  1475. kfree(del_list);
  1476. del_list = NULL;
  1477. /* do all the adds now */
  1478. filter_list_len = pf->hw.aq.asq_buf_size /
  1479. sizeof(struct i40e_aqc_add_macvlan_element_data),
  1480. add_list = kcalloc(filter_list_len,
  1481. sizeof(struct i40e_aqc_add_macvlan_element_data),
  1482. GFP_KERNEL);
  1483. if (!add_list)
  1484. return -ENOMEM;
  1485. list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
  1486. if (!f->changed)
  1487. continue;
  1488. if (f->counter == 0)
  1489. continue;
  1490. f->changed = false;
  1491. add_happened = true;
  1492. cmd_flags = 0;
  1493. /* add to add array */
  1494. ether_addr_copy(add_list[num_add].mac_addr, f->macaddr);
  1495. add_list[num_add].vlan_tag =
  1496. cpu_to_le16(
  1497. (u16)(f->vlan == I40E_VLAN_ANY ? 0 : f->vlan));
  1498. add_list[num_add].queue_number = 0;
  1499. cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
  1500. add_list[num_add].flags = cpu_to_le16(cmd_flags);
  1501. num_add++;
  1502. /* flush a full buffer */
  1503. if (num_add == filter_list_len) {
  1504. aq_ret = i40e_aq_add_macvlan(&pf->hw, vsi->seid,
  1505. add_list, num_add,
  1506. NULL);
  1507. num_add = 0;
  1508. if (aq_ret)
  1509. break;
  1510. memset(add_list, 0, sizeof(*add_list));
  1511. }
  1512. }
  1513. if (num_add) {
  1514. aq_ret = i40e_aq_add_macvlan(&pf->hw, vsi->seid,
  1515. add_list, num_add, NULL);
  1516. num_add = 0;
  1517. }
  1518. kfree(add_list);
  1519. add_list = NULL;
  1520. if (add_happened && (!aq_ret)) {
  1521. /* do nothing */;
  1522. } else if (add_happened && (aq_ret)) {
  1523. dev_info(&pf->pdev->dev,
  1524. "add filter failed, err %d, aq_err %d\n",
  1525. aq_ret, pf->hw.aq.asq_last_status);
  1526. if ((pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOSPC) &&
  1527. !test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
  1528. &vsi->state)) {
  1529. promisc_forced_on = true;
  1530. set_bit(__I40E_FILTER_OVERFLOW_PROMISC,
  1531. &vsi->state);
  1532. dev_info(&pf->pdev->dev, "promiscuous mode forced on\n");
  1533. }
  1534. }
  1535. }
  1536. /* check for changes in promiscuous modes */
  1537. if (changed_flags & IFF_ALLMULTI) {
  1538. bool cur_multipromisc;
  1539. cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
  1540. aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
  1541. vsi->seid,
  1542. cur_multipromisc,
  1543. NULL);
  1544. if (aq_ret)
  1545. dev_info(&pf->pdev->dev,
  1546. "set multi promisc failed, err %d, aq_err %d\n",
  1547. aq_ret, pf->hw.aq.asq_last_status);
  1548. }
  1549. if ((changed_flags & IFF_PROMISC) || promisc_forced_on) {
  1550. bool cur_promisc;
  1551. cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
  1552. test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
  1553. &vsi->state));
  1554. aq_ret = i40e_aq_set_vsi_unicast_promiscuous(&vsi->back->hw,
  1555. vsi->seid,
  1556. cur_promisc, NULL);
  1557. if (aq_ret)
  1558. dev_info(&pf->pdev->dev,
  1559. "set uni promisc failed, err %d, aq_err %d\n",
  1560. aq_ret, pf->hw.aq.asq_last_status);
  1561. aq_ret = i40e_aq_set_vsi_broadcast(&vsi->back->hw,
  1562. vsi->seid,
  1563. cur_promisc, NULL);
  1564. if (aq_ret)
  1565. dev_info(&pf->pdev->dev,
  1566. "set brdcast promisc failed, err %d, aq_err %d\n",
  1567. aq_ret, pf->hw.aq.asq_last_status);
  1568. }
  1569. clear_bit(__I40E_CONFIG_BUSY, &vsi->state);
  1570. return 0;
  1571. }
  1572. /**
  1573. * i40e_sync_filters_subtask - Sync the VSI filter list with HW
  1574. * @pf: board private structure
  1575. **/
  1576. static void i40e_sync_filters_subtask(struct i40e_pf *pf)
  1577. {
  1578. int v;
  1579. if (!pf || !(pf->flags & I40E_FLAG_FILTER_SYNC))
  1580. return;
  1581. pf->flags &= ~I40E_FLAG_FILTER_SYNC;
  1582. for (v = 0; v < pf->num_alloc_vsi; v++) {
  1583. if (pf->vsi[v] &&
  1584. (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED))
  1585. i40e_sync_vsi_filters(pf->vsi[v]);
  1586. }
  1587. }
  1588. /**
  1589. * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
  1590. * @netdev: network interface device structure
  1591. * @new_mtu: new value for maximum frame size
  1592. *
  1593. * Returns 0 on success, negative on failure
  1594. **/
  1595. static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
  1596. {
  1597. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1598. int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
  1599. struct i40e_vsi *vsi = np->vsi;
  1600. /* MTU < 68 is an error and causes problems on some kernels */
  1601. if ((new_mtu < 68) || (max_frame > I40E_MAX_RXBUFFER))
  1602. return -EINVAL;
  1603. netdev_info(netdev, "changing MTU from %d to %d\n",
  1604. netdev->mtu, new_mtu);
  1605. netdev->mtu = new_mtu;
  1606. if (netif_running(netdev))
  1607. i40e_vsi_reinit_locked(vsi);
  1608. return 0;
  1609. }
  1610. /**
  1611. * i40e_ioctl - Access the hwtstamp interface
  1612. * @netdev: network interface device structure
  1613. * @ifr: interface request data
  1614. * @cmd: ioctl command
  1615. **/
  1616. int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
  1617. {
  1618. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1619. struct i40e_pf *pf = np->vsi->back;
  1620. switch (cmd) {
  1621. case SIOCGHWTSTAMP:
  1622. return i40e_ptp_get_ts_config(pf, ifr);
  1623. case SIOCSHWTSTAMP:
  1624. return i40e_ptp_set_ts_config(pf, ifr);
  1625. default:
  1626. return -EOPNOTSUPP;
  1627. }
  1628. }
  1629. /**
  1630. * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
  1631. * @vsi: the vsi being adjusted
  1632. **/
  1633. void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
  1634. {
  1635. struct i40e_vsi_context ctxt;
  1636. i40e_status ret;
  1637. if ((vsi->info.valid_sections &
  1638. cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
  1639. ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
  1640. return; /* already enabled */
  1641. vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
  1642. vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
  1643. I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
  1644. ctxt.seid = vsi->seid;
  1645. memcpy(&ctxt.info, &vsi->info, sizeof(vsi->info));
  1646. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  1647. if (ret) {
  1648. dev_info(&vsi->back->pdev->dev,
  1649. "%s: update vsi failed, aq_err=%d\n",
  1650. __func__, vsi->back->hw.aq.asq_last_status);
  1651. }
  1652. }
  1653. /**
  1654. * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
  1655. * @vsi: the vsi being adjusted
  1656. **/
  1657. void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
  1658. {
  1659. struct i40e_vsi_context ctxt;
  1660. i40e_status ret;
  1661. if ((vsi->info.valid_sections &
  1662. cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
  1663. ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
  1664. I40E_AQ_VSI_PVLAN_EMOD_MASK))
  1665. return; /* already disabled */
  1666. vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
  1667. vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
  1668. I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
  1669. ctxt.seid = vsi->seid;
  1670. memcpy(&ctxt.info, &vsi->info, sizeof(vsi->info));
  1671. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  1672. if (ret) {
  1673. dev_info(&vsi->back->pdev->dev,
  1674. "%s: update vsi failed, aq_err=%d\n",
  1675. __func__, vsi->back->hw.aq.asq_last_status);
  1676. }
  1677. }
  1678. /**
  1679. * i40e_vlan_rx_register - Setup or shutdown vlan offload
  1680. * @netdev: network interface to be adjusted
  1681. * @features: netdev features to test if VLAN offload is enabled or not
  1682. **/
  1683. static void i40e_vlan_rx_register(struct net_device *netdev, u32 features)
  1684. {
  1685. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1686. struct i40e_vsi *vsi = np->vsi;
  1687. if (features & NETIF_F_HW_VLAN_CTAG_RX)
  1688. i40e_vlan_stripping_enable(vsi);
  1689. else
  1690. i40e_vlan_stripping_disable(vsi);
  1691. }
  1692. /**
  1693. * i40e_vsi_add_vlan - Add vsi membership for given vlan
  1694. * @vsi: the vsi being configured
  1695. * @vid: vlan id to be added (0 = untagged only , -1 = any)
  1696. **/
  1697. int i40e_vsi_add_vlan(struct i40e_vsi *vsi, s16 vid)
  1698. {
  1699. struct i40e_mac_filter *f, *add_f;
  1700. bool is_netdev, is_vf;
  1701. is_vf = (vsi->type == I40E_VSI_SRIOV);
  1702. is_netdev = !!(vsi->netdev);
  1703. if (is_netdev) {
  1704. add_f = i40e_add_filter(vsi, vsi->netdev->dev_addr, vid,
  1705. is_vf, is_netdev);
  1706. if (!add_f) {
  1707. dev_info(&vsi->back->pdev->dev,
  1708. "Could not add vlan filter %d for %pM\n",
  1709. vid, vsi->netdev->dev_addr);
  1710. return -ENOMEM;
  1711. }
  1712. }
  1713. list_for_each_entry(f, &vsi->mac_filter_list, list) {
  1714. add_f = i40e_add_filter(vsi, f->macaddr, vid, is_vf, is_netdev);
  1715. if (!add_f) {
  1716. dev_info(&vsi->back->pdev->dev,
  1717. "Could not add vlan filter %d for %pM\n",
  1718. vid, f->macaddr);
  1719. return -ENOMEM;
  1720. }
  1721. }
  1722. /* Now if we add a vlan tag, make sure to check if it is the first
  1723. * tag (i.e. a "tag" -1 does exist) and if so replace the -1 "tag"
  1724. * with 0, so we now accept untagged and specified tagged traffic
  1725. * (and not any taged and untagged)
  1726. */
  1727. if (vid > 0) {
  1728. if (is_netdev && i40e_find_filter(vsi, vsi->netdev->dev_addr,
  1729. I40E_VLAN_ANY,
  1730. is_vf, is_netdev)) {
  1731. i40e_del_filter(vsi, vsi->netdev->dev_addr,
  1732. I40E_VLAN_ANY, is_vf, is_netdev);
  1733. add_f = i40e_add_filter(vsi, vsi->netdev->dev_addr, 0,
  1734. is_vf, is_netdev);
  1735. if (!add_f) {
  1736. dev_info(&vsi->back->pdev->dev,
  1737. "Could not add filter 0 for %pM\n",
  1738. vsi->netdev->dev_addr);
  1739. return -ENOMEM;
  1740. }
  1741. }
  1742. }
  1743. /* Do not assume that I40E_VLAN_ANY should be reset to VLAN 0 */
  1744. if (vid > 0 && !vsi->info.pvid) {
  1745. list_for_each_entry(f, &vsi->mac_filter_list, list) {
  1746. if (i40e_find_filter(vsi, f->macaddr, I40E_VLAN_ANY,
  1747. is_vf, is_netdev)) {
  1748. i40e_del_filter(vsi, f->macaddr, I40E_VLAN_ANY,
  1749. is_vf, is_netdev);
  1750. add_f = i40e_add_filter(vsi, f->macaddr,
  1751. 0, is_vf, is_netdev);
  1752. if (!add_f) {
  1753. dev_info(&vsi->back->pdev->dev,
  1754. "Could not add filter 0 for %pM\n",
  1755. f->macaddr);
  1756. return -ENOMEM;
  1757. }
  1758. }
  1759. }
  1760. }
  1761. if (test_bit(__I40E_DOWN, &vsi->back->state) ||
  1762. test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
  1763. return 0;
  1764. return i40e_sync_vsi_filters(vsi);
  1765. }
  1766. /**
  1767. * i40e_vsi_kill_vlan - Remove vsi membership for given vlan
  1768. * @vsi: the vsi being configured
  1769. * @vid: vlan id to be removed (0 = untagged only , -1 = any)
  1770. *
  1771. * Return: 0 on success or negative otherwise
  1772. **/
  1773. int i40e_vsi_kill_vlan(struct i40e_vsi *vsi, s16 vid)
  1774. {
  1775. struct net_device *netdev = vsi->netdev;
  1776. struct i40e_mac_filter *f, *add_f;
  1777. bool is_vf, is_netdev;
  1778. int filter_count = 0;
  1779. is_vf = (vsi->type == I40E_VSI_SRIOV);
  1780. is_netdev = !!(netdev);
  1781. if (is_netdev)
  1782. i40e_del_filter(vsi, netdev->dev_addr, vid, is_vf, is_netdev);
  1783. list_for_each_entry(f, &vsi->mac_filter_list, list)
  1784. i40e_del_filter(vsi, f->macaddr, vid, is_vf, is_netdev);
  1785. /* go through all the filters for this VSI and if there is only
  1786. * vid == 0 it means there are no other filters, so vid 0 must
  1787. * be replaced with -1. This signifies that we should from now
  1788. * on accept any traffic (with any tag present, or untagged)
  1789. */
  1790. list_for_each_entry(f, &vsi->mac_filter_list, list) {
  1791. if (is_netdev) {
  1792. if (f->vlan &&
  1793. ether_addr_equal(netdev->dev_addr, f->macaddr))
  1794. filter_count++;
  1795. }
  1796. if (f->vlan)
  1797. filter_count++;
  1798. }
  1799. if (!filter_count && is_netdev) {
  1800. i40e_del_filter(vsi, netdev->dev_addr, 0, is_vf, is_netdev);
  1801. f = i40e_add_filter(vsi, netdev->dev_addr, I40E_VLAN_ANY,
  1802. is_vf, is_netdev);
  1803. if (!f) {
  1804. dev_info(&vsi->back->pdev->dev,
  1805. "Could not add filter %d for %pM\n",
  1806. I40E_VLAN_ANY, netdev->dev_addr);
  1807. return -ENOMEM;
  1808. }
  1809. }
  1810. if (!filter_count) {
  1811. list_for_each_entry(f, &vsi->mac_filter_list, list) {
  1812. i40e_del_filter(vsi, f->macaddr, 0, is_vf, is_netdev);
  1813. add_f = i40e_add_filter(vsi, f->macaddr, I40E_VLAN_ANY,
  1814. is_vf, is_netdev);
  1815. if (!add_f) {
  1816. dev_info(&vsi->back->pdev->dev,
  1817. "Could not add filter %d for %pM\n",
  1818. I40E_VLAN_ANY, f->macaddr);
  1819. return -ENOMEM;
  1820. }
  1821. }
  1822. }
  1823. if (test_bit(__I40E_DOWN, &vsi->back->state) ||
  1824. test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
  1825. return 0;
  1826. return i40e_sync_vsi_filters(vsi);
  1827. }
  1828. /**
  1829. * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
  1830. * @netdev: network interface to be adjusted
  1831. * @vid: vlan id to be added
  1832. *
  1833. * net_device_ops implementation for adding vlan ids
  1834. **/
  1835. static int i40e_vlan_rx_add_vid(struct net_device *netdev,
  1836. __always_unused __be16 proto, u16 vid)
  1837. {
  1838. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1839. struct i40e_vsi *vsi = np->vsi;
  1840. int ret = 0;
  1841. if (vid > 4095)
  1842. return -EINVAL;
  1843. netdev_info(netdev, "adding %pM vid=%d\n", netdev->dev_addr, vid);
  1844. /* If the network stack called us with vid = 0 then
  1845. * it is asking to receive priority tagged packets with
  1846. * vlan id 0. Our HW receives them by default when configured
  1847. * to receive untagged packets so there is no need to add an
  1848. * extra filter for vlan 0 tagged packets.
  1849. */
  1850. if (vid)
  1851. ret = i40e_vsi_add_vlan(vsi, vid);
  1852. if (!ret && (vid < VLAN_N_VID))
  1853. set_bit(vid, vsi->active_vlans);
  1854. return ret;
  1855. }
  1856. /**
  1857. * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
  1858. * @netdev: network interface to be adjusted
  1859. * @vid: vlan id to be removed
  1860. *
  1861. * net_device_ops implementation for removing vlan ids
  1862. **/
  1863. static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
  1864. __always_unused __be16 proto, u16 vid)
  1865. {
  1866. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1867. struct i40e_vsi *vsi = np->vsi;
  1868. netdev_info(netdev, "removing %pM vid=%d\n", netdev->dev_addr, vid);
  1869. /* return code is ignored as there is nothing a user
  1870. * can do about failure to remove and a log message was
  1871. * already printed from the other function
  1872. */
  1873. i40e_vsi_kill_vlan(vsi, vid);
  1874. clear_bit(vid, vsi->active_vlans);
  1875. return 0;
  1876. }
  1877. /**
  1878. * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
  1879. * @vsi: the vsi being brought back up
  1880. **/
  1881. static void i40e_restore_vlan(struct i40e_vsi *vsi)
  1882. {
  1883. u16 vid;
  1884. if (!vsi->netdev)
  1885. return;
  1886. i40e_vlan_rx_register(vsi->netdev, vsi->netdev->features);
  1887. for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
  1888. i40e_vlan_rx_add_vid(vsi->netdev, htons(ETH_P_8021Q),
  1889. vid);
  1890. }
  1891. /**
  1892. * i40e_vsi_add_pvid - Add pvid for the VSI
  1893. * @vsi: the vsi being adjusted
  1894. * @vid: the vlan id to set as a PVID
  1895. **/
  1896. int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
  1897. {
  1898. struct i40e_vsi_context ctxt;
  1899. i40e_status aq_ret;
  1900. vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
  1901. vsi->info.pvid = cpu_to_le16(vid);
  1902. vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
  1903. I40E_AQ_VSI_PVLAN_INSERT_PVID |
  1904. I40E_AQ_VSI_PVLAN_EMOD_STR;
  1905. ctxt.seid = vsi->seid;
  1906. memcpy(&ctxt.info, &vsi->info, sizeof(vsi->info));
  1907. aq_ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  1908. if (aq_ret) {
  1909. dev_info(&vsi->back->pdev->dev,
  1910. "%s: update vsi failed, aq_err=%d\n",
  1911. __func__, vsi->back->hw.aq.asq_last_status);
  1912. return -ENOENT;
  1913. }
  1914. return 0;
  1915. }
  1916. /**
  1917. * i40e_vsi_remove_pvid - Remove the pvid from the VSI
  1918. * @vsi: the vsi being adjusted
  1919. *
  1920. * Just use the vlan_rx_register() service to put it back to normal
  1921. **/
  1922. void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
  1923. {
  1924. i40e_vlan_stripping_disable(vsi);
  1925. vsi->info.pvid = 0;
  1926. }
  1927. /**
  1928. * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
  1929. * @vsi: ptr to the VSI
  1930. *
  1931. * If this function returns with an error, then it's possible one or
  1932. * more of the rings is populated (while the rest are not). It is the
  1933. * callers duty to clean those orphaned rings.
  1934. *
  1935. * Return 0 on success, negative on failure
  1936. **/
  1937. static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
  1938. {
  1939. int i, err = 0;
  1940. for (i = 0; i < vsi->num_queue_pairs && !err; i++)
  1941. err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
  1942. return err;
  1943. }
  1944. /**
  1945. * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
  1946. * @vsi: ptr to the VSI
  1947. *
  1948. * Free VSI's transmit software resources
  1949. **/
  1950. static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
  1951. {
  1952. int i;
  1953. if (!vsi->tx_rings)
  1954. return;
  1955. for (i = 0; i < vsi->num_queue_pairs; i++)
  1956. if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
  1957. i40e_free_tx_resources(vsi->tx_rings[i]);
  1958. }
  1959. /**
  1960. * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
  1961. * @vsi: ptr to the VSI
  1962. *
  1963. * If this function returns with an error, then it's possible one or
  1964. * more of the rings is populated (while the rest are not). It is the
  1965. * callers duty to clean those orphaned rings.
  1966. *
  1967. * Return 0 on success, negative on failure
  1968. **/
  1969. static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
  1970. {
  1971. int i, err = 0;
  1972. for (i = 0; i < vsi->num_queue_pairs && !err; i++)
  1973. err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
  1974. return err;
  1975. }
  1976. /**
  1977. * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
  1978. * @vsi: ptr to the VSI
  1979. *
  1980. * Free all receive software resources
  1981. **/
  1982. static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
  1983. {
  1984. int i;
  1985. if (!vsi->rx_rings)
  1986. return;
  1987. for (i = 0; i < vsi->num_queue_pairs; i++)
  1988. if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
  1989. i40e_free_rx_resources(vsi->rx_rings[i]);
  1990. }
  1991. /**
  1992. * i40e_configure_tx_ring - Configure a transmit ring context and rest
  1993. * @ring: The Tx ring to configure
  1994. *
  1995. * Configure the Tx descriptor ring in the HMC context.
  1996. **/
  1997. static int i40e_configure_tx_ring(struct i40e_ring *ring)
  1998. {
  1999. struct i40e_vsi *vsi = ring->vsi;
  2000. u16 pf_q = vsi->base_queue + ring->queue_index;
  2001. struct i40e_hw *hw = &vsi->back->hw;
  2002. struct i40e_hmc_obj_txq tx_ctx;
  2003. i40e_status err = 0;
  2004. u32 qtx_ctl = 0;
  2005. /* some ATR related tx ring init */
  2006. if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
  2007. ring->atr_sample_rate = vsi->back->atr_sample_rate;
  2008. ring->atr_count = 0;
  2009. } else {
  2010. ring->atr_sample_rate = 0;
  2011. }
  2012. /* initialize XPS */
  2013. if (ring->q_vector && ring->netdev &&
  2014. vsi->tc_config.numtc <= 1 &&
  2015. !test_and_set_bit(__I40E_TX_XPS_INIT_DONE, &ring->state))
  2016. netif_set_xps_queue(ring->netdev,
  2017. &ring->q_vector->affinity_mask,
  2018. ring->queue_index);
  2019. /* clear the context structure first */
  2020. memset(&tx_ctx, 0, sizeof(tx_ctx));
  2021. tx_ctx.new_context = 1;
  2022. tx_ctx.base = (ring->dma / 128);
  2023. tx_ctx.qlen = ring->count;
  2024. tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
  2025. I40E_FLAG_FD_ATR_ENABLED));
  2026. tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
  2027. /* FDIR VSI tx ring can still use RS bit and writebacks */
  2028. if (vsi->type != I40E_VSI_FDIR)
  2029. tx_ctx.head_wb_ena = 1;
  2030. tx_ctx.head_wb_addr = ring->dma +
  2031. (ring->count * sizeof(struct i40e_tx_desc));
  2032. /* As part of VSI creation/update, FW allocates certain
  2033. * Tx arbitration queue sets for each TC enabled for
  2034. * the VSI. The FW returns the handles to these queue
  2035. * sets as part of the response buffer to Add VSI,
  2036. * Update VSI, etc. AQ commands. It is expected that
  2037. * these queue set handles be associated with the Tx
  2038. * queues by the driver as part of the TX queue context
  2039. * initialization. This has to be done regardless of
  2040. * DCB as by default everything is mapped to TC0.
  2041. */
  2042. tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
  2043. tx_ctx.rdylist_act = 0;
  2044. /* clear the context in the HMC */
  2045. err = i40e_clear_lan_tx_queue_context(hw, pf_q);
  2046. if (err) {
  2047. dev_info(&vsi->back->pdev->dev,
  2048. "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
  2049. ring->queue_index, pf_q, err);
  2050. return -ENOMEM;
  2051. }
  2052. /* set the context in the HMC */
  2053. err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
  2054. if (err) {
  2055. dev_info(&vsi->back->pdev->dev,
  2056. "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
  2057. ring->queue_index, pf_q, err);
  2058. return -ENOMEM;
  2059. }
  2060. /* Now associate this queue with this PCI function */
  2061. if (vsi->type == I40E_VSI_VMDQ2)
  2062. qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
  2063. else
  2064. qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
  2065. qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
  2066. I40E_QTX_CTL_PF_INDX_MASK);
  2067. wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
  2068. i40e_flush(hw);
  2069. clear_bit(__I40E_HANG_CHECK_ARMED, &ring->state);
  2070. /* cache tail off for easier writes later */
  2071. ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
  2072. return 0;
  2073. }
  2074. /**
  2075. * i40e_configure_rx_ring - Configure a receive ring context
  2076. * @ring: The Rx ring to configure
  2077. *
  2078. * Configure the Rx descriptor ring in the HMC context.
  2079. **/
  2080. static int i40e_configure_rx_ring(struct i40e_ring *ring)
  2081. {
  2082. struct i40e_vsi *vsi = ring->vsi;
  2083. u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
  2084. u16 pf_q = vsi->base_queue + ring->queue_index;
  2085. struct i40e_hw *hw = &vsi->back->hw;
  2086. struct i40e_hmc_obj_rxq rx_ctx;
  2087. i40e_status err = 0;
  2088. ring->state = 0;
  2089. /* clear the context structure first */
  2090. memset(&rx_ctx, 0, sizeof(rx_ctx));
  2091. ring->rx_buf_len = vsi->rx_buf_len;
  2092. ring->rx_hdr_len = vsi->rx_hdr_len;
  2093. rx_ctx.dbuff = ring->rx_buf_len >> I40E_RXQ_CTX_DBUFF_SHIFT;
  2094. rx_ctx.hbuff = ring->rx_hdr_len >> I40E_RXQ_CTX_HBUFF_SHIFT;
  2095. rx_ctx.base = (ring->dma / 128);
  2096. rx_ctx.qlen = ring->count;
  2097. if (vsi->back->flags & I40E_FLAG_16BYTE_RX_DESC_ENABLED) {
  2098. set_ring_16byte_desc_enabled(ring);
  2099. rx_ctx.dsize = 0;
  2100. } else {
  2101. rx_ctx.dsize = 1;
  2102. }
  2103. rx_ctx.dtype = vsi->dtype;
  2104. if (vsi->dtype) {
  2105. set_ring_ps_enabled(ring);
  2106. rx_ctx.hsplit_0 = I40E_RX_SPLIT_L2 |
  2107. I40E_RX_SPLIT_IP |
  2108. I40E_RX_SPLIT_TCP_UDP |
  2109. I40E_RX_SPLIT_SCTP;
  2110. } else {
  2111. rx_ctx.hsplit_0 = 0;
  2112. }
  2113. rx_ctx.rxmax = min_t(u16, vsi->max_frame,
  2114. (chain_len * ring->rx_buf_len));
  2115. rx_ctx.tphrdesc_ena = 1;
  2116. rx_ctx.tphwdesc_ena = 1;
  2117. rx_ctx.tphdata_ena = 1;
  2118. rx_ctx.tphhead_ena = 1;
  2119. if (hw->revision_id == 0)
  2120. rx_ctx.lrxqthresh = 0;
  2121. else
  2122. rx_ctx.lrxqthresh = 2;
  2123. rx_ctx.crcstrip = 1;
  2124. rx_ctx.l2tsel = 1;
  2125. rx_ctx.showiv = 1;
  2126. /* set the prefena field to 1 because the manual says to */
  2127. rx_ctx.prefena = 1;
  2128. /* clear the context in the HMC */
  2129. err = i40e_clear_lan_rx_queue_context(hw, pf_q);
  2130. if (err) {
  2131. dev_info(&vsi->back->pdev->dev,
  2132. "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
  2133. ring->queue_index, pf_q, err);
  2134. return -ENOMEM;
  2135. }
  2136. /* set the context in the HMC */
  2137. err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
  2138. if (err) {
  2139. dev_info(&vsi->back->pdev->dev,
  2140. "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
  2141. ring->queue_index, pf_q, err);
  2142. return -ENOMEM;
  2143. }
  2144. /* cache tail for quicker writes, and clear the reg before use */
  2145. ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
  2146. writel(0, ring->tail);
  2147. i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
  2148. return 0;
  2149. }
  2150. /**
  2151. * i40e_vsi_configure_tx - Configure the VSI for Tx
  2152. * @vsi: VSI structure describing this set of rings and resources
  2153. *
  2154. * Configure the Tx VSI for operation.
  2155. **/
  2156. static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
  2157. {
  2158. int err = 0;
  2159. u16 i;
  2160. for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
  2161. err = i40e_configure_tx_ring(vsi->tx_rings[i]);
  2162. return err;
  2163. }
  2164. /**
  2165. * i40e_vsi_configure_rx - Configure the VSI for Rx
  2166. * @vsi: the VSI being configured
  2167. *
  2168. * Configure the Rx VSI for operation.
  2169. **/
  2170. static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
  2171. {
  2172. int err = 0;
  2173. u16 i;
  2174. if (vsi->netdev && (vsi->netdev->mtu > ETH_DATA_LEN))
  2175. vsi->max_frame = vsi->netdev->mtu + ETH_HLEN
  2176. + ETH_FCS_LEN + VLAN_HLEN;
  2177. else
  2178. vsi->max_frame = I40E_RXBUFFER_2048;
  2179. /* figure out correct receive buffer length */
  2180. switch (vsi->back->flags & (I40E_FLAG_RX_1BUF_ENABLED |
  2181. I40E_FLAG_RX_PS_ENABLED)) {
  2182. case I40E_FLAG_RX_1BUF_ENABLED:
  2183. vsi->rx_hdr_len = 0;
  2184. vsi->rx_buf_len = vsi->max_frame;
  2185. vsi->dtype = I40E_RX_DTYPE_NO_SPLIT;
  2186. break;
  2187. case I40E_FLAG_RX_PS_ENABLED:
  2188. vsi->rx_hdr_len = I40E_RX_HDR_SIZE;
  2189. vsi->rx_buf_len = I40E_RXBUFFER_2048;
  2190. vsi->dtype = I40E_RX_DTYPE_HEADER_SPLIT;
  2191. break;
  2192. default:
  2193. vsi->rx_hdr_len = I40E_RX_HDR_SIZE;
  2194. vsi->rx_buf_len = I40E_RXBUFFER_2048;
  2195. vsi->dtype = I40E_RX_DTYPE_SPLIT_ALWAYS;
  2196. break;
  2197. }
  2198. /* round up for the chip's needs */
  2199. vsi->rx_hdr_len = ALIGN(vsi->rx_hdr_len,
  2200. (1 << I40E_RXQ_CTX_HBUFF_SHIFT));
  2201. vsi->rx_buf_len = ALIGN(vsi->rx_buf_len,
  2202. (1 << I40E_RXQ_CTX_DBUFF_SHIFT));
  2203. /* set up individual rings */
  2204. for (i = 0; i < vsi->num_queue_pairs && !err; i++)
  2205. err = i40e_configure_rx_ring(vsi->rx_rings[i]);
  2206. return err;
  2207. }
  2208. /**
  2209. * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
  2210. * @vsi: ptr to the VSI
  2211. **/
  2212. static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
  2213. {
  2214. struct i40e_ring *tx_ring, *rx_ring;
  2215. u16 qoffset, qcount;
  2216. int i, n;
  2217. if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED))
  2218. return;
  2219. for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
  2220. if (!(vsi->tc_config.enabled_tc & (1 << n)))
  2221. continue;
  2222. qoffset = vsi->tc_config.tc_info[n].qoffset;
  2223. qcount = vsi->tc_config.tc_info[n].qcount;
  2224. for (i = qoffset; i < (qoffset + qcount); i++) {
  2225. rx_ring = vsi->rx_rings[i];
  2226. tx_ring = vsi->tx_rings[i];
  2227. rx_ring->dcb_tc = n;
  2228. tx_ring->dcb_tc = n;
  2229. }
  2230. }
  2231. }
  2232. /**
  2233. * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
  2234. * @vsi: ptr to the VSI
  2235. **/
  2236. static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
  2237. {
  2238. if (vsi->netdev)
  2239. i40e_set_rx_mode(vsi->netdev);
  2240. }
  2241. /**
  2242. * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
  2243. * @vsi: Pointer to the targeted VSI
  2244. *
  2245. * This function replays the hlist on the hw where all the SB Flow Director
  2246. * filters were saved.
  2247. **/
  2248. static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
  2249. {
  2250. struct i40e_fdir_filter *filter;
  2251. struct i40e_pf *pf = vsi->back;
  2252. struct hlist_node *node;
  2253. if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
  2254. return;
  2255. hlist_for_each_entry_safe(filter, node,
  2256. &pf->fdir_filter_list, fdir_node) {
  2257. i40e_add_del_fdir(vsi, filter, true);
  2258. }
  2259. }
  2260. /**
  2261. * i40e_vsi_configure - Set up the VSI for action
  2262. * @vsi: the VSI being configured
  2263. **/
  2264. static int i40e_vsi_configure(struct i40e_vsi *vsi)
  2265. {
  2266. int err;
  2267. i40e_set_vsi_rx_mode(vsi);
  2268. i40e_restore_vlan(vsi);
  2269. i40e_vsi_config_dcb_rings(vsi);
  2270. err = i40e_vsi_configure_tx(vsi);
  2271. if (!err)
  2272. err = i40e_vsi_configure_rx(vsi);
  2273. return err;
  2274. }
  2275. /**
  2276. * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
  2277. * @vsi: the VSI being configured
  2278. **/
  2279. static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
  2280. {
  2281. struct i40e_pf *pf = vsi->back;
  2282. struct i40e_q_vector *q_vector;
  2283. struct i40e_hw *hw = &pf->hw;
  2284. u16 vector;
  2285. int i, q;
  2286. u32 val;
  2287. u32 qp;
  2288. /* The interrupt indexing is offset by 1 in the PFINT_ITRn
  2289. * and PFINT_LNKLSTn registers, e.g.:
  2290. * PFINT_ITRn[0..n-1] gets msix-1..msix-n (qpair interrupts)
  2291. */
  2292. qp = vsi->base_queue;
  2293. vector = vsi->base_vector;
  2294. for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
  2295. q_vector = vsi->q_vectors[i];
  2296. q_vector->rx.itr = ITR_TO_REG(vsi->rx_itr_setting);
  2297. q_vector->rx.latency_range = I40E_LOW_LATENCY;
  2298. wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
  2299. q_vector->rx.itr);
  2300. q_vector->tx.itr = ITR_TO_REG(vsi->tx_itr_setting);
  2301. q_vector->tx.latency_range = I40E_LOW_LATENCY;
  2302. wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
  2303. q_vector->tx.itr);
  2304. /* Linked list for the queuepairs assigned to this vector */
  2305. wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
  2306. for (q = 0; q < q_vector->num_ringpairs; q++) {
  2307. val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
  2308. (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
  2309. (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
  2310. (qp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
  2311. (I40E_QUEUE_TYPE_TX
  2312. << I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
  2313. wr32(hw, I40E_QINT_RQCTL(qp), val);
  2314. val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  2315. (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
  2316. (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
  2317. ((qp+1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT)|
  2318. (I40E_QUEUE_TYPE_RX
  2319. << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
  2320. /* Terminate the linked list */
  2321. if (q == (q_vector->num_ringpairs - 1))
  2322. val |= (I40E_QUEUE_END_OF_LIST
  2323. << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
  2324. wr32(hw, I40E_QINT_TQCTL(qp), val);
  2325. qp++;
  2326. }
  2327. }
  2328. i40e_flush(hw);
  2329. }
  2330. /**
  2331. * i40e_enable_misc_int_causes - enable the non-queue interrupts
  2332. * @hw: ptr to the hardware info
  2333. **/
  2334. static void i40e_enable_misc_int_causes(struct i40e_hw *hw)
  2335. {
  2336. u32 val;
  2337. /* clear things first */
  2338. wr32(hw, I40E_PFINT_ICR0_ENA, 0); /* disable all */
  2339. rd32(hw, I40E_PFINT_ICR0); /* read to clear */
  2340. val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK |
  2341. I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK |
  2342. I40E_PFINT_ICR0_ENA_GRST_MASK |
  2343. I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
  2344. I40E_PFINT_ICR0_ENA_GPIO_MASK |
  2345. I40E_PFINT_ICR0_ENA_TIMESYNC_MASK |
  2346. I40E_PFINT_ICR0_ENA_HMC_ERR_MASK |
  2347. I40E_PFINT_ICR0_ENA_VFLR_MASK |
  2348. I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
  2349. wr32(hw, I40E_PFINT_ICR0_ENA, val);
  2350. /* SW_ITR_IDX = 0, but don't change INTENA */
  2351. wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
  2352. I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
  2353. /* OTHER_ITR_IDX = 0 */
  2354. wr32(hw, I40E_PFINT_STAT_CTL0, 0);
  2355. }
  2356. /**
  2357. * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
  2358. * @vsi: the VSI being configured
  2359. **/
  2360. static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
  2361. {
  2362. struct i40e_q_vector *q_vector = vsi->q_vectors[0];
  2363. struct i40e_pf *pf = vsi->back;
  2364. struct i40e_hw *hw = &pf->hw;
  2365. u32 val;
  2366. /* set the ITR configuration */
  2367. q_vector->rx.itr = ITR_TO_REG(vsi->rx_itr_setting);
  2368. q_vector->rx.latency_range = I40E_LOW_LATENCY;
  2369. wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.itr);
  2370. q_vector->tx.itr = ITR_TO_REG(vsi->tx_itr_setting);
  2371. q_vector->tx.latency_range = I40E_LOW_LATENCY;
  2372. wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.itr);
  2373. i40e_enable_misc_int_causes(hw);
  2374. /* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
  2375. wr32(hw, I40E_PFINT_LNKLST0, 0);
  2376. /* Associate the queue pair to the vector and enable the queue int */
  2377. val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
  2378. (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
  2379. (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
  2380. wr32(hw, I40E_QINT_RQCTL(0), val);
  2381. val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  2382. (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
  2383. (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
  2384. wr32(hw, I40E_QINT_TQCTL(0), val);
  2385. i40e_flush(hw);
  2386. }
  2387. /**
  2388. * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
  2389. * @pf: board private structure
  2390. **/
  2391. void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
  2392. {
  2393. struct i40e_hw *hw = &pf->hw;
  2394. wr32(hw, I40E_PFINT_DYN_CTL0,
  2395. I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
  2396. i40e_flush(hw);
  2397. }
  2398. /**
  2399. * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
  2400. * @pf: board private structure
  2401. **/
  2402. void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf)
  2403. {
  2404. struct i40e_hw *hw = &pf->hw;
  2405. u32 val;
  2406. val = I40E_PFINT_DYN_CTL0_INTENA_MASK |
  2407. I40E_PFINT_DYN_CTL0_CLEARPBA_MASK |
  2408. (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
  2409. wr32(hw, I40E_PFINT_DYN_CTL0, val);
  2410. i40e_flush(hw);
  2411. }
  2412. /**
  2413. * i40e_irq_dynamic_enable - Enable default interrupt generation settings
  2414. * @vsi: pointer to a vsi
  2415. * @vector: enable a particular Hw Interrupt vector
  2416. **/
  2417. void i40e_irq_dynamic_enable(struct i40e_vsi *vsi, int vector)
  2418. {
  2419. struct i40e_pf *pf = vsi->back;
  2420. struct i40e_hw *hw = &pf->hw;
  2421. u32 val;
  2422. val = I40E_PFINT_DYN_CTLN_INTENA_MASK |
  2423. I40E_PFINT_DYN_CTLN_CLEARPBA_MASK |
  2424. (I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
  2425. wr32(hw, I40E_PFINT_DYN_CTLN(vector - 1), val);
  2426. /* skip the flush */
  2427. }
  2428. /**
  2429. * i40e_irq_dynamic_disable - Disable default interrupt generation settings
  2430. * @vsi: pointer to a vsi
  2431. * @vector: enable a particular Hw Interrupt vector
  2432. **/
  2433. void i40e_irq_dynamic_disable(struct i40e_vsi *vsi, int vector)
  2434. {
  2435. struct i40e_pf *pf = vsi->back;
  2436. struct i40e_hw *hw = &pf->hw;
  2437. u32 val;
  2438. val = I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT;
  2439. wr32(hw, I40E_PFINT_DYN_CTLN(vector - 1), val);
  2440. i40e_flush(hw);
  2441. }
  2442. /**
  2443. * i40e_msix_clean_rings - MSIX mode Interrupt Handler
  2444. * @irq: interrupt number
  2445. * @data: pointer to a q_vector
  2446. **/
  2447. static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
  2448. {
  2449. struct i40e_q_vector *q_vector = data;
  2450. if (!q_vector->tx.ring && !q_vector->rx.ring)
  2451. return IRQ_HANDLED;
  2452. napi_schedule(&q_vector->napi);
  2453. return IRQ_HANDLED;
  2454. }
  2455. /**
  2456. * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
  2457. * @vsi: the VSI being configured
  2458. * @basename: name for the vector
  2459. *
  2460. * Allocates MSI-X vectors and requests interrupts from the kernel.
  2461. **/
  2462. static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
  2463. {
  2464. int q_vectors = vsi->num_q_vectors;
  2465. struct i40e_pf *pf = vsi->back;
  2466. int base = vsi->base_vector;
  2467. int rx_int_idx = 0;
  2468. int tx_int_idx = 0;
  2469. int vector, err;
  2470. for (vector = 0; vector < q_vectors; vector++) {
  2471. struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
  2472. if (q_vector->tx.ring && q_vector->rx.ring) {
  2473. snprintf(q_vector->name, sizeof(q_vector->name) - 1,
  2474. "%s-%s-%d", basename, "TxRx", rx_int_idx++);
  2475. tx_int_idx++;
  2476. } else if (q_vector->rx.ring) {
  2477. snprintf(q_vector->name, sizeof(q_vector->name) - 1,
  2478. "%s-%s-%d", basename, "rx", rx_int_idx++);
  2479. } else if (q_vector->tx.ring) {
  2480. snprintf(q_vector->name, sizeof(q_vector->name) - 1,
  2481. "%s-%s-%d", basename, "tx", tx_int_idx++);
  2482. } else {
  2483. /* skip this unused q_vector */
  2484. continue;
  2485. }
  2486. err = request_irq(pf->msix_entries[base + vector].vector,
  2487. vsi->irq_handler,
  2488. 0,
  2489. q_vector->name,
  2490. q_vector);
  2491. if (err) {
  2492. dev_info(&pf->pdev->dev,
  2493. "%s: request_irq failed, error: %d\n",
  2494. __func__, err);
  2495. goto free_queue_irqs;
  2496. }
  2497. /* assign the mask for this irq */
  2498. irq_set_affinity_hint(pf->msix_entries[base + vector].vector,
  2499. &q_vector->affinity_mask);
  2500. }
  2501. vsi->irqs_ready = true;
  2502. return 0;
  2503. free_queue_irqs:
  2504. while (vector) {
  2505. vector--;
  2506. irq_set_affinity_hint(pf->msix_entries[base + vector].vector,
  2507. NULL);
  2508. free_irq(pf->msix_entries[base + vector].vector,
  2509. &(vsi->q_vectors[vector]));
  2510. }
  2511. return err;
  2512. }
  2513. /**
  2514. * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
  2515. * @vsi: the VSI being un-configured
  2516. **/
  2517. static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
  2518. {
  2519. struct i40e_pf *pf = vsi->back;
  2520. struct i40e_hw *hw = &pf->hw;
  2521. int base = vsi->base_vector;
  2522. int i;
  2523. for (i = 0; i < vsi->num_queue_pairs; i++) {
  2524. wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), 0);
  2525. wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), 0);
  2526. }
  2527. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  2528. for (i = vsi->base_vector;
  2529. i < (vsi->num_q_vectors + vsi->base_vector); i++)
  2530. wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
  2531. i40e_flush(hw);
  2532. for (i = 0; i < vsi->num_q_vectors; i++)
  2533. synchronize_irq(pf->msix_entries[i + base].vector);
  2534. } else {
  2535. /* Legacy and MSI mode - this stops all interrupt handling */
  2536. wr32(hw, I40E_PFINT_ICR0_ENA, 0);
  2537. wr32(hw, I40E_PFINT_DYN_CTL0, 0);
  2538. i40e_flush(hw);
  2539. synchronize_irq(pf->pdev->irq);
  2540. }
  2541. }
  2542. /**
  2543. * i40e_vsi_enable_irq - Enable IRQ for the given VSI
  2544. * @vsi: the VSI being configured
  2545. **/
  2546. static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
  2547. {
  2548. struct i40e_pf *pf = vsi->back;
  2549. int i;
  2550. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  2551. for (i = vsi->base_vector;
  2552. i < (vsi->num_q_vectors + vsi->base_vector); i++)
  2553. i40e_irq_dynamic_enable(vsi, i);
  2554. } else {
  2555. i40e_irq_dynamic_enable_icr0(pf);
  2556. }
  2557. i40e_flush(&pf->hw);
  2558. return 0;
  2559. }
  2560. /**
  2561. * i40e_stop_misc_vector - Stop the vector that handles non-queue events
  2562. * @pf: board private structure
  2563. **/
  2564. static void i40e_stop_misc_vector(struct i40e_pf *pf)
  2565. {
  2566. /* Disable ICR 0 */
  2567. wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
  2568. i40e_flush(&pf->hw);
  2569. }
  2570. /**
  2571. * i40e_intr - MSI/Legacy and non-queue interrupt handler
  2572. * @irq: interrupt number
  2573. * @data: pointer to a q_vector
  2574. *
  2575. * This is the handler used for all MSI/Legacy interrupts, and deals
  2576. * with both queue and non-queue interrupts. This is also used in
  2577. * MSIX mode to handle the non-queue interrupts.
  2578. **/
  2579. static irqreturn_t i40e_intr(int irq, void *data)
  2580. {
  2581. struct i40e_pf *pf = (struct i40e_pf *)data;
  2582. struct i40e_hw *hw = &pf->hw;
  2583. irqreturn_t ret = IRQ_NONE;
  2584. u32 icr0, icr0_remaining;
  2585. u32 val, ena_mask;
  2586. icr0 = rd32(hw, I40E_PFINT_ICR0);
  2587. ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
  2588. /* if sharing a legacy IRQ, we might get called w/o an intr pending */
  2589. if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
  2590. goto enable_intr;
  2591. /* if interrupt but no bits showing, must be SWINT */
  2592. if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
  2593. (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
  2594. pf->sw_int_count++;
  2595. /* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
  2596. if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
  2597. /* temporarily disable queue cause for NAPI processing */
  2598. u32 qval = rd32(hw, I40E_QINT_RQCTL(0));
  2599. qval &= ~I40E_QINT_RQCTL_CAUSE_ENA_MASK;
  2600. wr32(hw, I40E_QINT_RQCTL(0), qval);
  2601. qval = rd32(hw, I40E_QINT_TQCTL(0));
  2602. qval &= ~I40E_QINT_TQCTL_CAUSE_ENA_MASK;
  2603. wr32(hw, I40E_QINT_TQCTL(0), qval);
  2604. if (!test_bit(__I40E_DOWN, &pf->state))
  2605. napi_schedule(&pf->vsi[pf->lan_vsi]->q_vectors[0]->napi);
  2606. }
  2607. if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
  2608. ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
  2609. set_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
  2610. }
  2611. if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
  2612. ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
  2613. set_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
  2614. }
  2615. if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
  2616. ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
  2617. set_bit(__I40E_VFLR_EVENT_PENDING, &pf->state);
  2618. }
  2619. if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
  2620. if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
  2621. set_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
  2622. ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
  2623. val = rd32(hw, I40E_GLGEN_RSTAT);
  2624. val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
  2625. >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
  2626. if (val == I40E_RESET_CORER) {
  2627. pf->corer_count++;
  2628. } else if (val == I40E_RESET_GLOBR) {
  2629. pf->globr_count++;
  2630. } else if (val == I40E_RESET_EMPR) {
  2631. pf->empr_count++;
  2632. set_bit(__I40E_EMP_RESET_REQUESTED, &pf->state);
  2633. }
  2634. }
  2635. if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
  2636. icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
  2637. dev_info(&pf->pdev->dev, "HMC error interrupt\n");
  2638. }
  2639. if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
  2640. u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
  2641. if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK) {
  2642. icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
  2643. i40e_ptp_tx_hwtstamp(pf);
  2644. }
  2645. }
  2646. /* If a critical error is pending we have no choice but to reset the
  2647. * device.
  2648. * Report and mask out any remaining unexpected interrupts.
  2649. */
  2650. icr0_remaining = icr0 & ena_mask;
  2651. if (icr0_remaining) {
  2652. dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
  2653. icr0_remaining);
  2654. if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
  2655. (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
  2656. (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
  2657. dev_info(&pf->pdev->dev, "device will be reset\n");
  2658. set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
  2659. i40e_service_event_schedule(pf);
  2660. }
  2661. ena_mask &= ~icr0_remaining;
  2662. }
  2663. ret = IRQ_HANDLED;
  2664. enable_intr:
  2665. /* re-enable interrupt causes */
  2666. wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
  2667. if (!test_bit(__I40E_DOWN, &pf->state)) {
  2668. i40e_service_event_schedule(pf);
  2669. i40e_irq_dynamic_enable_icr0(pf);
  2670. }
  2671. return ret;
  2672. }
  2673. /**
  2674. * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
  2675. * @tx_ring: tx ring to clean
  2676. * @budget: how many cleans we're allowed
  2677. *
  2678. * Returns true if there's any budget left (e.g. the clean is finished)
  2679. **/
  2680. static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
  2681. {
  2682. struct i40e_vsi *vsi = tx_ring->vsi;
  2683. u16 i = tx_ring->next_to_clean;
  2684. struct i40e_tx_buffer *tx_buf;
  2685. struct i40e_tx_desc *tx_desc;
  2686. tx_buf = &tx_ring->tx_bi[i];
  2687. tx_desc = I40E_TX_DESC(tx_ring, i);
  2688. i -= tx_ring->count;
  2689. do {
  2690. struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
  2691. /* if next_to_watch is not set then there is no work pending */
  2692. if (!eop_desc)
  2693. break;
  2694. /* prevent any other reads prior to eop_desc */
  2695. read_barrier_depends();
  2696. /* if the descriptor isn't done, no work yet to do */
  2697. if (!(eop_desc->cmd_type_offset_bsz &
  2698. cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
  2699. break;
  2700. /* clear next_to_watch to prevent false hangs */
  2701. tx_buf->next_to_watch = NULL;
  2702. /* unmap skb header data */
  2703. dma_unmap_single(tx_ring->dev,
  2704. dma_unmap_addr(tx_buf, dma),
  2705. dma_unmap_len(tx_buf, len),
  2706. DMA_TO_DEVICE);
  2707. dma_unmap_len_set(tx_buf, len, 0);
  2708. /* move to the next desc and buffer to clean */
  2709. tx_buf++;
  2710. tx_desc++;
  2711. i++;
  2712. if (unlikely(!i)) {
  2713. i -= tx_ring->count;
  2714. tx_buf = tx_ring->tx_bi;
  2715. tx_desc = I40E_TX_DESC(tx_ring, 0);
  2716. }
  2717. /* update budget accounting */
  2718. budget--;
  2719. } while (likely(budget));
  2720. i += tx_ring->count;
  2721. tx_ring->next_to_clean = i;
  2722. if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED) {
  2723. i40e_irq_dynamic_enable(vsi,
  2724. tx_ring->q_vector->v_idx + vsi->base_vector);
  2725. }
  2726. return budget > 0;
  2727. }
  2728. /**
  2729. * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
  2730. * @irq: interrupt number
  2731. * @data: pointer to a q_vector
  2732. **/
  2733. static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
  2734. {
  2735. struct i40e_q_vector *q_vector = data;
  2736. struct i40e_vsi *vsi;
  2737. if (!q_vector->tx.ring)
  2738. return IRQ_HANDLED;
  2739. vsi = q_vector->tx.ring->vsi;
  2740. i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
  2741. return IRQ_HANDLED;
  2742. }
  2743. /**
  2744. * i40e_map_vector_to_qp - Assigns the queue pair to the vector
  2745. * @vsi: the VSI being configured
  2746. * @v_idx: vector index
  2747. * @qp_idx: queue pair index
  2748. **/
  2749. static void map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
  2750. {
  2751. struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
  2752. struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
  2753. struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
  2754. tx_ring->q_vector = q_vector;
  2755. tx_ring->next = q_vector->tx.ring;
  2756. q_vector->tx.ring = tx_ring;
  2757. q_vector->tx.count++;
  2758. rx_ring->q_vector = q_vector;
  2759. rx_ring->next = q_vector->rx.ring;
  2760. q_vector->rx.ring = rx_ring;
  2761. q_vector->rx.count++;
  2762. }
  2763. /**
  2764. * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
  2765. * @vsi: the VSI being configured
  2766. *
  2767. * This function maps descriptor rings to the queue-specific vectors
  2768. * we were allotted through the MSI-X enabling code. Ideally, we'd have
  2769. * one vector per queue pair, but on a constrained vector budget, we
  2770. * group the queue pairs as "efficiently" as possible.
  2771. **/
  2772. static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
  2773. {
  2774. int qp_remaining = vsi->num_queue_pairs;
  2775. int q_vectors = vsi->num_q_vectors;
  2776. int num_ringpairs;
  2777. int v_start = 0;
  2778. int qp_idx = 0;
  2779. /* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
  2780. * group them so there are multiple queues per vector.
  2781. * It is also important to go through all the vectors available to be
  2782. * sure that if we don't use all the vectors, that the remaining vectors
  2783. * are cleared. This is especially important when decreasing the
  2784. * number of queues in use.
  2785. */
  2786. for (; v_start < q_vectors; v_start++) {
  2787. struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
  2788. num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
  2789. q_vector->num_ringpairs = num_ringpairs;
  2790. q_vector->rx.count = 0;
  2791. q_vector->tx.count = 0;
  2792. q_vector->rx.ring = NULL;
  2793. q_vector->tx.ring = NULL;
  2794. while (num_ringpairs--) {
  2795. map_vector_to_qp(vsi, v_start, qp_idx);
  2796. qp_idx++;
  2797. qp_remaining--;
  2798. }
  2799. }
  2800. }
  2801. /**
  2802. * i40e_vsi_request_irq - Request IRQ from the OS
  2803. * @vsi: the VSI being configured
  2804. * @basename: name for the vector
  2805. **/
  2806. static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
  2807. {
  2808. struct i40e_pf *pf = vsi->back;
  2809. int err;
  2810. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  2811. err = i40e_vsi_request_irq_msix(vsi, basename);
  2812. else if (pf->flags & I40E_FLAG_MSI_ENABLED)
  2813. err = request_irq(pf->pdev->irq, i40e_intr, 0,
  2814. pf->misc_int_name, pf);
  2815. else
  2816. err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
  2817. pf->misc_int_name, pf);
  2818. if (err)
  2819. dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
  2820. return err;
  2821. }
  2822. #ifdef CONFIG_NET_POLL_CONTROLLER
  2823. /**
  2824. * i40e_netpoll - A Polling 'interrupt'handler
  2825. * @netdev: network interface device structure
  2826. *
  2827. * This is used by netconsole to send skbs without having to re-enable
  2828. * interrupts. It's not called while the normal interrupt routine is executing.
  2829. **/
  2830. static void i40e_netpoll(struct net_device *netdev)
  2831. {
  2832. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2833. struct i40e_vsi *vsi = np->vsi;
  2834. struct i40e_pf *pf = vsi->back;
  2835. int i;
  2836. /* if interface is down do nothing */
  2837. if (test_bit(__I40E_DOWN, &vsi->state))
  2838. return;
  2839. pf->flags |= I40E_FLAG_IN_NETPOLL;
  2840. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  2841. for (i = 0; i < vsi->num_q_vectors; i++)
  2842. i40e_msix_clean_rings(0, vsi->q_vectors[i]);
  2843. } else {
  2844. i40e_intr(pf->pdev->irq, netdev);
  2845. }
  2846. pf->flags &= ~I40E_FLAG_IN_NETPOLL;
  2847. }
  2848. #endif
  2849. /**
  2850. * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
  2851. * @pf: the PF being configured
  2852. * @pf_q: the PF queue
  2853. * @enable: enable or disable state of the queue
  2854. *
  2855. * This routine will wait for the given Tx queue of the PF to reach the
  2856. * enabled or disabled state.
  2857. * Returns -ETIMEDOUT in case of failing to reach the requested state after
  2858. * multiple retries; else will return 0 in case of success.
  2859. **/
  2860. static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
  2861. {
  2862. int i;
  2863. u32 tx_reg;
  2864. for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
  2865. tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
  2866. if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
  2867. break;
  2868. udelay(10);
  2869. }
  2870. if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
  2871. return -ETIMEDOUT;
  2872. return 0;
  2873. }
  2874. /**
  2875. * i40e_vsi_control_tx - Start or stop a VSI's rings
  2876. * @vsi: the VSI being configured
  2877. * @enable: start or stop the rings
  2878. **/
  2879. static int i40e_vsi_control_tx(struct i40e_vsi *vsi, bool enable)
  2880. {
  2881. struct i40e_pf *pf = vsi->back;
  2882. struct i40e_hw *hw = &pf->hw;
  2883. int i, j, pf_q, ret = 0;
  2884. u32 tx_reg;
  2885. pf_q = vsi->base_queue;
  2886. for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
  2887. /* warn the TX unit of coming changes */
  2888. i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
  2889. if (!enable)
  2890. udelay(10);
  2891. for (j = 0; j < 50; j++) {
  2892. tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
  2893. if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
  2894. ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
  2895. break;
  2896. usleep_range(1000, 2000);
  2897. }
  2898. /* Skip if the queue is already in the requested state */
  2899. if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
  2900. continue;
  2901. /* turn on/off the queue */
  2902. if (enable) {
  2903. wr32(hw, I40E_QTX_HEAD(pf_q), 0);
  2904. tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
  2905. } else {
  2906. tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
  2907. }
  2908. wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
  2909. /* wait for the change to finish */
  2910. ret = i40e_pf_txq_wait(pf, pf_q, enable);
  2911. if (ret) {
  2912. dev_info(&pf->pdev->dev,
  2913. "%s: VSI seid %d Tx ring %d %sable timeout\n",
  2914. __func__, vsi->seid, pf_q,
  2915. (enable ? "en" : "dis"));
  2916. break;
  2917. }
  2918. }
  2919. if (hw->revision_id == 0)
  2920. mdelay(50);
  2921. return ret;
  2922. }
  2923. /**
  2924. * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
  2925. * @pf: the PF being configured
  2926. * @pf_q: the PF queue
  2927. * @enable: enable or disable state of the queue
  2928. *
  2929. * This routine will wait for the given Rx queue of the PF to reach the
  2930. * enabled or disabled state.
  2931. * Returns -ETIMEDOUT in case of failing to reach the requested state after
  2932. * multiple retries; else will return 0 in case of success.
  2933. **/
  2934. static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
  2935. {
  2936. int i;
  2937. u32 rx_reg;
  2938. for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
  2939. rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
  2940. if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
  2941. break;
  2942. udelay(10);
  2943. }
  2944. if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
  2945. return -ETIMEDOUT;
  2946. return 0;
  2947. }
  2948. /**
  2949. * i40e_vsi_control_rx - Start or stop a VSI's rings
  2950. * @vsi: the VSI being configured
  2951. * @enable: start or stop the rings
  2952. **/
  2953. static int i40e_vsi_control_rx(struct i40e_vsi *vsi, bool enable)
  2954. {
  2955. struct i40e_pf *pf = vsi->back;
  2956. struct i40e_hw *hw = &pf->hw;
  2957. int i, j, pf_q, ret = 0;
  2958. u32 rx_reg;
  2959. pf_q = vsi->base_queue;
  2960. for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
  2961. for (j = 0; j < 50; j++) {
  2962. rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
  2963. if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
  2964. ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
  2965. break;
  2966. usleep_range(1000, 2000);
  2967. }
  2968. /* Skip if the queue is already in the requested state */
  2969. if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
  2970. continue;
  2971. /* turn on/off the queue */
  2972. if (enable)
  2973. rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
  2974. else
  2975. rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
  2976. wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
  2977. /* wait for the change to finish */
  2978. ret = i40e_pf_rxq_wait(pf, pf_q, enable);
  2979. if (ret) {
  2980. dev_info(&pf->pdev->dev,
  2981. "%s: VSI seid %d Rx ring %d %sable timeout\n",
  2982. __func__, vsi->seid, pf_q,
  2983. (enable ? "en" : "dis"));
  2984. break;
  2985. }
  2986. }
  2987. return ret;
  2988. }
  2989. /**
  2990. * i40e_vsi_control_rings - Start or stop a VSI's rings
  2991. * @vsi: the VSI being configured
  2992. * @enable: start or stop the rings
  2993. **/
  2994. int i40e_vsi_control_rings(struct i40e_vsi *vsi, bool request)
  2995. {
  2996. int ret = 0;
  2997. /* do rx first for enable and last for disable */
  2998. if (request) {
  2999. ret = i40e_vsi_control_rx(vsi, request);
  3000. if (ret)
  3001. return ret;
  3002. ret = i40e_vsi_control_tx(vsi, request);
  3003. } else {
  3004. /* Ignore return value, we need to shutdown whatever we can */
  3005. i40e_vsi_control_tx(vsi, request);
  3006. i40e_vsi_control_rx(vsi, request);
  3007. }
  3008. return ret;
  3009. }
  3010. /**
  3011. * i40e_vsi_free_irq - Free the irq association with the OS
  3012. * @vsi: the VSI being configured
  3013. **/
  3014. static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
  3015. {
  3016. struct i40e_pf *pf = vsi->back;
  3017. struct i40e_hw *hw = &pf->hw;
  3018. int base = vsi->base_vector;
  3019. u32 val, qp;
  3020. int i;
  3021. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  3022. if (!vsi->q_vectors)
  3023. return;
  3024. if (!vsi->irqs_ready)
  3025. return;
  3026. vsi->irqs_ready = false;
  3027. for (i = 0; i < vsi->num_q_vectors; i++) {
  3028. u16 vector = i + base;
  3029. /* free only the irqs that were actually requested */
  3030. if (!vsi->q_vectors[i] ||
  3031. !vsi->q_vectors[i]->num_ringpairs)
  3032. continue;
  3033. /* clear the affinity_mask in the IRQ descriptor */
  3034. irq_set_affinity_hint(pf->msix_entries[vector].vector,
  3035. NULL);
  3036. free_irq(pf->msix_entries[vector].vector,
  3037. vsi->q_vectors[i]);
  3038. /* Tear down the interrupt queue link list
  3039. *
  3040. * We know that they come in pairs and always
  3041. * the Rx first, then the Tx. To clear the
  3042. * link list, stick the EOL value into the
  3043. * next_q field of the registers.
  3044. */
  3045. val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
  3046. qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
  3047. >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
  3048. val |= I40E_QUEUE_END_OF_LIST
  3049. << I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
  3050. wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
  3051. while (qp != I40E_QUEUE_END_OF_LIST) {
  3052. u32 next;
  3053. val = rd32(hw, I40E_QINT_RQCTL(qp));
  3054. val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
  3055. I40E_QINT_RQCTL_MSIX0_INDX_MASK |
  3056. I40E_QINT_RQCTL_CAUSE_ENA_MASK |
  3057. I40E_QINT_RQCTL_INTEVENT_MASK);
  3058. val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
  3059. I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
  3060. wr32(hw, I40E_QINT_RQCTL(qp), val);
  3061. val = rd32(hw, I40E_QINT_TQCTL(qp));
  3062. next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
  3063. >> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
  3064. val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
  3065. I40E_QINT_TQCTL_MSIX0_INDX_MASK |
  3066. I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  3067. I40E_QINT_TQCTL_INTEVENT_MASK);
  3068. val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
  3069. I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
  3070. wr32(hw, I40E_QINT_TQCTL(qp), val);
  3071. qp = next;
  3072. }
  3073. }
  3074. } else {
  3075. free_irq(pf->pdev->irq, pf);
  3076. val = rd32(hw, I40E_PFINT_LNKLST0);
  3077. qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
  3078. >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
  3079. val |= I40E_QUEUE_END_OF_LIST
  3080. << I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
  3081. wr32(hw, I40E_PFINT_LNKLST0, val);
  3082. val = rd32(hw, I40E_QINT_RQCTL(qp));
  3083. val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
  3084. I40E_QINT_RQCTL_MSIX0_INDX_MASK |
  3085. I40E_QINT_RQCTL_CAUSE_ENA_MASK |
  3086. I40E_QINT_RQCTL_INTEVENT_MASK);
  3087. val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
  3088. I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
  3089. wr32(hw, I40E_QINT_RQCTL(qp), val);
  3090. val = rd32(hw, I40E_QINT_TQCTL(qp));
  3091. val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
  3092. I40E_QINT_TQCTL_MSIX0_INDX_MASK |
  3093. I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  3094. I40E_QINT_TQCTL_INTEVENT_MASK);
  3095. val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
  3096. I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
  3097. wr32(hw, I40E_QINT_TQCTL(qp), val);
  3098. }
  3099. }
  3100. /**
  3101. * i40e_free_q_vector - Free memory allocated for specific interrupt vector
  3102. * @vsi: the VSI being configured
  3103. * @v_idx: Index of vector to be freed
  3104. *
  3105. * This function frees the memory allocated to the q_vector. In addition if
  3106. * NAPI is enabled it will delete any references to the NAPI struct prior
  3107. * to freeing the q_vector.
  3108. **/
  3109. static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
  3110. {
  3111. struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
  3112. struct i40e_ring *ring;
  3113. if (!q_vector)
  3114. return;
  3115. /* disassociate q_vector from rings */
  3116. i40e_for_each_ring(ring, q_vector->tx)
  3117. ring->q_vector = NULL;
  3118. i40e_for_each_ring(ring, q_vector->rx)
  3119. ring->q_vector = NULL;
  3120. /* only VSI w/ an associated netdev is set up w/ NAPI */
  3121. if (vsi->netdev)
  3122. netif_napi_del(&q_vector->napi);
  3123. vsi->q_vectors[v_idx] = NULL;
  3124. kfree_rcu(q_vector, rcu);
  3125. }
  3126. /**
  3127. * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
  3128. * @vsi: the VSI being un-configured
  3129. *
  3130. * This frees the memory allocated to the q_vectors and
  3131. * deletes references to the NAPI struct.
  3132. **/
  3133. static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
  3134. {
  3135. int v_idx;
  3136. for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
  3137. i40e_free_q_vector(vsi, v_idx);
  3138. }
  3139. /**
  3140. * i40e_reset_interrupt_capability - Disable interrupt setup in OS
  3141. * @pf: board private structure
  3142. **/
  3143. static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
  3144. {
  3145. /* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
  3146. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  3147. pci_disable_msix(pf->pdev);
  3148. kfree(pf->msix_entries);
  3149. pf->msix_entries = NULL;
  3150. } else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
  3151. pci_disable_msi(pf->pdev);
  3152. }
  3153. pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
  3154. }
  3155. /**
  3156. * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
  3157. * @pf: board private structure
  3158. *
  3159. * We go through and clear interrupt specific resources and reset the structure
  3160. * to pre-load conditions
  3161. **/
  3162. static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
  3163. {
  3164. int i;
  3165. i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
  3166. for (i = 0; i < pf->num_alloc_vsi; i++)
  3167. if (pf->vsi[i])
  3168. i40e_vsi_free_q_vectors(pf->vsi[i]);
  3169. i40e_reset_interrupt_capability(pf);
  3170. }
  3171. /**
  3172. * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
  3173. * @vsi: the VSI being configured
  3174. **/
  3175. static void i40e_napi_enable_all(struct i40e_vsi *vsi)
  3176. {
  3177. int q_idx;
  3178. if (!vsi->netdev)
  3179. return;
  3180. for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
  3181. napi_enable(&vsi->q_vectors[q_idx]->napi);
  3182. }
  3183. /**
  3184. * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
  3185. * @vsi: the VSI being configured
  3186. **/
  3187. static void i40e_napi_disable_all(struct i40e_vsi *vsi)
  3188. {
  3189. int q_idx;
  3190. if (!vsi->netdev)
  3191. return;
  3192. for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
  3193. napi_disable(&vsi->q_vectors[q_idx]->napi);
  3194. }
  3195. /**
  3196. * i40e_vsi_close - Shut down a VSI
  3197. * @vsi: the vsi to be quelled
  3198. **/
  3199. static void i40e_vsi_close(struct i40e_vsi *vsi)
  3200. {
  3201. if (!test_and_set_bit(__I40E_DOWN, &vsi->state))
  3202. i40e_down(vsi);
  3203. i40e_vsi_free_irq(vsi);
  3204. i40e_vsi_free_tx_resources(vsi);
  3205. i40e_vsi_free_rx_resources(vsi);
  3206. }
  3207. /**
  3208. * i40e_quiesce_vsi - Pause a given VSI
  3209. * @vsi: the VSI being paused
  3210. **/
  3211. static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
  3212. {
  3213. if (test_bit(__I40E_DOWN, &vsi->state))
  3214. return;
  3215. set_bit(__I40E_NEEDS_RESTART, &vsi->state);
  3216. if (vsi->netdev && netif_running(vsi->netdev)) {
  3217. vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
  3218. } else {
  3219. i40e_vsi_close(vsi);
  3220. }
  3221. }
  3222. /**
  3223. * i40e_unquiesce_vsi - Resume a given VSI
  3224. * @vsi: the VSI being resumed
  3225. **/
  3226. static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
  3227. {
  3228. if (!test_bit(__I40E_NEEDS_RESTART, &vsi->state))
  3229. return;
  3230. clear_bit(__I40E_NEEDS_RESTART, &vsi->state);
  3231. if (vsi->netdev && netif_running(vsi->netdev))
  3232. vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
  3233. else
  3234. i40e_vsi_open(vsi); /* this clears the DOWN bit */
  3235. }
  3236. /**
  3237. * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
  3238. * @pf: the PF
  3239. **/
  3240. static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
  3241. {
  3242. int v;
  3243. for (v = 0; v < pf->num_alloc_vsi; v++) {
  3244. if (pf->vsi[v])
  3245. i40e_quiesce_vsi(pf->vsi[v]);
  3246. }
  3247. }
  3248. /**
  3249. * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
  3250. * @pf: the PF
  3251. **/
  3252. static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
  3253. {
  3254. int v;
  3255. for (v = 0; v < pf->num_alloc_vsi; v++) {
  3256. if (pf->vsi[v])
  3257. i40e_unquiesce_vsi(pf->vsi[v]);
  3258. }
  3259. }
  3260. /**
  3261. * i40e_dcb_get_num_tc - Get the number of TCs from DCBx config
  3262. * @dcbcfg: the corresponding DCBx configuration structure
  3263. *
  3264. * Return the number of TCs from given DCBx configuration
  3265. **/
  3266. static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
  3267. {
  3268. u8 num_tc = 0;
  3269. int i;
  3270. /* Scan the ETS Config Priority Table to find
  3271. * traffic class enabled for a given priority
  3272. * and use the traffic class index to get the
  3273. * number of traffic classes enabled
  3274. */
  3275. for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
  3276. if (dcbcfg->etscfg.prioritytable[i] > num_tc)
  3277. num_tc = dcbcfg->etscfg.prioritytable[i];
  3278. }
  3279. /* Traffic class index starts from zero so
  3280. * increment to return the actual count
  3281. */
  3282. return num_tc + 1;
  3283. }
  3284. /**
  3285. * i40e_dcb_get_enabled_tc - Get enabled traffic classes
  3286. * @dcbcfg: the corresponding DCBx configuration structure
  3287. *
  3288. * Query the current DCB configuration and return the number of
  3289. * traffic classes enabled from the given DCBX config
  3290. **/
  3291. static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
  3292. {
  3293. u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
  3294. u8 enabled_tc = 1;
  3295. u8 i;
  3296. for (i = 0; i < num_tc; i++)
  3297. enabled_tc |= 1 << i;
  3298. return enabled_tc;
  3299. }
  3300. /**
  3301. * i40e_pf_get_num_tc - Get enabled traffic classes for PF
  3302. * @pf: PF being queried
  3303. *
  3304. * Return number of traffic classes enabled for the given PF
  3305. **/
  3306. static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
  3307. {
  3308. struct i40e_hw *hw = &pf->hw;
  3309. u8 i, enabled_tc;
  3310. u8 num_tc = 0;
  3311. struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
  3312. /* If DCB is not enabled then always in single TC */
  3313. if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
  3314. return 1;
  3315. /* MFP mode return count of enabled TCs for this PF */
  3316. if (pf->flags & I40E_FLAG_MFP_ENABLED) {
  3317. enabled_tc = pf->hw.func_caps.enabled_tcmap;
  3318. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  3319. if (enabled_tc & (1 << i))
  3320. num_tc++;
  3321. }
  3322. return num_tc;
  3323. }
  3324. /* SFP mode will be enabled for all TCs on port */
  3325. return i40e_dcb_get_num_tc(dcbcfg);
  3326. }
  3327. /**
  3328. * i40e_pf_get_default_tc - Get bitmap for first enabled TC
  3329. * @pf: PF being queried
  3330. *
  3331. * Return a bitmap for first enabled traffic class for this PF.
  3332. **/
  3333. static u8 i40e_pf_get_default_tc(struct i40e_pf *pf)
  3334. {
  3335. u8 enabled_tc = pf->hw.func_caps.enabled_tcmap;
  3336. u8 i = 0;
  3337. if (!enabled_tc)
  3338. return 0x1; /* TC0 */
  3339. /* Find the first enabled TC */
  3340. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  3341. if (enabled_tc & (1 << i))
  3342. break;
  3343. }
  3344. return 1 << i;
  3345. }
  3346. /**
  3347. * i40e_pf_get_pf_tc_map - Get bitmap for enabled traffic classes
  3348. * @pf: PF being queried
  3349. *
  3350. * Return a bitmap for enabled traffic classes for this PF.
  3351. **/
  3352. static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
  3353. {
  3354. /* If DCB is not enabled for this PF then just return default TC */
  3355. if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
  3356. return i40e_pf_get_default_tc(pf);
  3357. /* MFP mode will have enabled TCs set by FW */
  3358. if (pf->flags & I40E_FLAG_MFP_ENABLED)
  3359. return pf->hw.func_caps.enabled_tcmap;
  3360. /* SFP mode we want PF to be enabled for all TCs */
  3361. return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
  3362. }
  3363. /**
  3364. * i40e_vsi_get_bw_info - Query VSI BW Information
  3365. * @vsi: the VSI being queried
  3366. *
  3367. * Returns 0 on success, negative value on failure
  3368. **/
  3369. static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
  3370. {
  3371. struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
  3372. struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
  3373. struct i40e_pf *pf = vsi->back;
  3374. struct i40e_hw *hw = &pf->hw;
  3375. i40e_status aq_ret;
  3376. u32 tc_bw_max;
  3377. int i;
  3378. /* Get the VSI level BW configuration */
  3379. aq_ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
  3380. if (aq_ret) {
  3381. dev_info(&pf->pdev->dev,
  3382. "couldn't get pf vsi bw config, err %d, aq_err %d\n",
  3383. aq_ret, pf->hw.aq.asq_last_status);
  3384. return -EINVAL;
  3385. }
  3386. /* Get the VSI level BW configuration per TC */
  3387. aq_ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
  3388. NULL);
  3389. if (aq_ret) {
  3390. dev_info(&pf->pdev->dev,
  3391. "couldn't get pf vsi ets bw config, err %d, aq_err %d\n",
  3392. aq_ret, pf->hw.aq.asq_last_status);
  3393. return -EINVAL;
  3394. }
  3395. if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
  3396. dev_info(&pf->pdev->dev,
  3397. "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
  3398. bw_config.tc_valid_bits,
  3399. bw_ets_config.tc_valid_bits);
  3400. /* Still continuing */
  3401. }
  3402. vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
  3403. vsi->bw_max_quanta = bw_config.max_bw;
  3404. tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
  3405. (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
  3406. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  3407. vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
  3408. vsi->bw_ets_limit_credits[i] =
  3409. le16_to_cpu(bw_ets_config.credits[i]);
  3410. /* 3 bits out of 4 for each TC */
  3411. vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
  3412. }
  3413. return 0;
  3414. }
  3415. /**
  3416. * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
  3417. * @vsi: the VSI being configured
  3418. * @enabled_tc: TC bitmap
  3419. * @bw_credits: BW shared credits per TC
  3420. *
  3421. * Returns 0 on success, negative value on failure
  3422. **/
  3423. static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
  3424. u8 *bw_share)
  3425. {
  3426. struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
  3427. i40e_status aq_ret;
  3428. int i;
  3429. bw_data.tc_valid_bits = enabled_tc;
  3430. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
  3431. bw_data.tc_bw_credits[i] = bw_share[i];
  3432. aq_ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, vsi->seid, &bw_data,
  3433. NULL);
  3434. if (aq_ret) {
  3435. dev_info(&vsi->back->pdev->dev,
  3436. "AQ command Config VSI BW allocation per TC failed = %d\n",
  3437. vsi->back->hw.aq.asq_last_status);
  3438. return -EINVAL;
  3439. }
  3440. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
  3441. vsi->info.qs_handle[i] = bw_data.qs_handles[i];
  3442. return 0;
  3443. }
  3444. /**
  3445. * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
  3446. * @vsi: the VSI being configured
  3447. * @enabled_tc: TC map to be enabled
  3448. *
  3449. **/
  3450. static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
  3451. {
  3452. struct net_device *netdev = vsi->netdev;
  3453. struct i40e_pf *pf = vsi->back;
  3454. struct i40e_hw *hw = &pf->hw;
  3455. u8 netdev_tc = 0;
  3456. int i;
  3457. struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
  3458. if (!netdev)
  3459. return;
  3460. if (!enabled_tc) {
  3461. netdev_reset_tc(netdev);
  3462. return;
  3463. }
  3464. /* Set up actual enabled TCs on the VSI */
  3465. if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
  3466. return;
  3467. /* set per TC queues for the VSI */
  3468. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  3469. /* Only set TC queues for enabled tcs
  3470. *
  3471. * e.g. For a VSI that has TC0 and TC3 enabled the
  3472. * enabled_tc bitmap would be 0x00001001; the driver
  3473. * will set the numtc for netdev as 2 that will be
  3474. * referenced by the netdev layer as TC 0 and 1.
  3475. */
  3476. if (vsi->tc_config.enabled_tc & (1 << i))
  3477. netdev_set_tc_queue(netdev,
  3478. vsi->tc_config.tc_info[i].netdev_tc,
  3479. vsi->tc_config.tc_info[i].qcount,
  3480. vsi->tc_config.tc_info[i].qoffset);
  3481. }
  3482. /* Assign UP2TC map for the VSI */
  3483. for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
  3484. /* Get the actual TC# for the UP */
  3485. u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
  3486. /* Get the mapped netdev TC# for the UP */
  3487. netdev_tc = vsi->tc_config.tc_info[ets_tc].netdev_tc;
  3488. netdev_set_prio_tc_map(netdev, i, netdev_tc);
  3489. }
  3490. }
  3491. /**
  3492. * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
  3493. * @vsi: the VSI being configured
  3494. * @ctxt: the ctxt buffer returned from AQ VSI update param command
  3495. **/
  3496. static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
  3497. struct i40e_vsi_context *ctxt)
  3498. {
  3499. /* copy just the sections touched not the entire info
  3500. * since not all sections are valid as returned by
  3501. * update vsi params
  3502. */
  3503. vsi->info.mapping_flags = ctxt->info.mapping_flags;
  3504. memcpy(&vsi->info.queue_mapping,
  3505. &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
  3506. memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
  3507. sizeof(vsi->info.tc_mapping));
  3508. }
  3509. /**
  3510. * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
  3511. * @vsi: VSI to be configured
  3512. * @enabled_tc: TC bitmap
  3513. *
  3514. * This configures a particular VSI for TCs that are mapped to the
  3515. * given TC bitmap. It uses default bandwidth share for TCs across
  3516. * VSIs to configure TC for a particular VSI.
  3517. *
  3518. * NOTE:
  3519. * It is expected that the VSI queues have been quisced before calling
  3520. * this function.
  3521. **/
  3522. static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
  3523. {
  3524. u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
  3525. struct i40e_vsi_context ctxt;
  3526. int ret = 0;
  3527. int i;
  3528. /* Check if enabled_tc is same as existing or new TCs */
  3529. if (vsi->tc_config.enabled_tc == enabled_tc)
  3530. return ret;
  3531. /* Enable ETS TCs with equal BW Share for now across all VSIs */
  3532. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  3533. if (enabled_tc & (1 << i))
  3534. bw_share[i] = 1;
  3535. }
  3536. ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
  3537. if (ret) {
  3538. dev_info(&vsi->back->pdev->dev,
  3539. "Failed configuring TC map %d for VSI %d\n",
  3540. enabled_tc, vsi->seid);
  3541. goto out;
  3542. }
  3543. /* Update Queue Pairs Mapping for currently enabled UPs */
  3544. ctxt.seid = vsi->seid;
  3545. ctxt.pf_num = vsi->back->hw.pf_id;
  3546. ctxt.vf_num = 0;
  3547. ctxt.uplink_seid = vsi->uplink_seid;
  3548. memcpy(&ctxt.info, &vsi->info, sizeof(vsi->info));
  3549. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
  3550. /* Update the VSI after updating the VSI queue-mapping information */
  3551. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  3552. if (ret) {
  3553. dev_info(&vsi->back->pdev->dev,
  3554. "update vsi failed, aq_err=%d\n",
  3555. vsi->back->hw.aq.asq_last_status);
  3556. goto out;
  3557. }
  3558. /* update the local VSI info with updated queue map */
  3559. i40e_vsi_update_queue_map(vsi, &ctxt);
  3560. vsi->info.valid_sections = 0;
  3561. /* Update current VSI BW information */
  3562. ret = i40e_vsi_get_bw_info(vsi);
  3563. if (ret) {
  3564. dev_info(&vsi->back->pdev->dev,
  3565. "Failed updating vsi bw info, aq_err=%d\n",
  3566. vsi->back->hw.aq.asq_last_status);
  3567. goto out;
  3568. }
  3569. /* Update the netdev TC setup */
  3570. i40e_vsi_config_netdev_tc(vsi, enabled_tc);
  3571. out:
  3572. return ret;
  3573. }
  3574. /**
  3575. * i40e_veb_config_tc - Configure TCs for given VEB
  3576. * @veb: given VEB
  3577. * @enabled_tc: TC bitmap
  3578. *
  3579. * Configures given TC bitmap for VEB (switching) element
  3580. **/
  3581. int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
  3582. {
  3583. struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
  3584. struct i40e_pf *pf = veb->pf;
  3585. int ret = 0;
  3586. int i;
  3587. /* No TCs or already enabled TCs just return */
  3588. if (!enabled_tc || veb->enabled_tc == enabled_tc)
  3589. return ret;
  3590. bw_data.tc_valid_bits = enabled_tc;
  3591. /* bw_data.absolute_credits is not set (relative) */
  3592. /* Enable ETS TCs with equal BW Share for now */
  3593. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  3594. if (enabled_tc & (1 << i))
  3595. bw_data.tc_bw_share_credits[i] = 1;
  3596. }
  3597. ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
  3598. &bw_data, NULL);
  3599. if (ret) {
  3600. dev_info(&pf->pdev->dev,
  3601. "veb bw config failed, aq_err=%d\n",
  3602. pf->hw.aq.asq_last_status);
  3603. goto out;
  3604. }
  3605. /* Update the BW information */
  3606. ret = i40e_veb_get_bw_info(veb);
  3607. if (ret) {
  3608. dev_info(&pf->pdev->dev,
  3609. "Failed getting veb bw config, aq_err=%d\n",
  3610. pf->hw.aq.asq_last_status);
  3611. }
  3612. out:
  3613. return ret;
  3614. }
  3615. #ifdef CONFIG_I40E_DCB
  3616. /**
  3617. * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
  3618. * @pf: PF struct
  3619. *
  3620. * Reconfigure VEB/VSIs on a given PF; it is assumed that
  3621. * the caller would've quiesce all the VSIs before calling
  3622. * this function
  3623. **/
  3624. static void i40e_dcb_reconfigure(struct i40e_pf *pf)
  3625. {
  3626. u8 tc_map = 0;
  3627. int ret;
  3628. u8 v;
  3629. /* Enable the TCs available on PF to all VEBs */
  3630. tc_map = i40e_pf_get_tc_map(pf);
  3631. for (v = 0; v < I40E_MAX_VEB; v++) {
  3632. if (!pf->veb[v])
  3633. continue;
  3634. ret = i40e_veb_config_tc(pf->veb[v], tc_map);
  3635. if (ret) {
  3636. dev_info(&pf->pdev->dev,
  3637. "Failed configuring TC for VEB seid=%d\n",
  3638. pf->veb[v]->seid);
  3639. /* Will try to configure as many components */
  3640. }
  3641. }
  3642. /* Update each VSI */
  3643. for (v = 0; v < pf->num_alloc_vsi; v++) {
  3644. if (!pf->vsi[v])
  3645. continue;
  3646. /* - Enable all TCs for the LAN VSI
  3647. * - For all others keep them at TC0 for now
  3648. */
  3649. if (v == pf->lan_vsi)
  3650. tc_map = i40e_pf_get_tc_map(pf);
  3651. else
  3652. tc_map = i40e_pf_get_default_tc(pf);
  3653. ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
  3654. if (ret) {
  3655. dev_info(&pf->pdev->dev,
  3656. "Failed configuring TC for VSI seid=%d\n",
  3657. pf->vsi[v]->seid);
  3658. /* Will try to configure as many components */
  3659. } else {
  3660. /* Re-configure VSI vectors based on updated TC map */
  3661. i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
  3662. if (pf->vsi[v]->netdev)
  3663. i40e_dcbnl_set_all(pf->vsi[v]);
  3664. }
  3665. }
  3666. }
  3667. /**
  3668. * i40e_init_pf_dcb - Initialize DCB configuration
  3669. * @pf: PF being configured
  3670. *
  3671. * Query the current DCB configuration and cache it
  3672. * in the hardware structure
  3673. **/
  3674. static int i40e_init_pf_dcb(struct i40e_pf *pf)
  3675. {
  3676. struct i40e_hw *hw = &pf->hw;
  3677. int err = 0;
  3678. if (pf->hw.func_caps.npar_enable)
  3679. goto out;
  3680. /* Get the initial DCB configuration */
  3681. err = i40e_init_dcb(hw);
  3682. if (!err) {
  3683. /* Device/Function is not DCBX capable */
  3684. if ((!hw->func_caps.dcb) ||
  3685. (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
  3686. dev_info(&pf->pdev->dev,
  3687. "DCBX offload is not supported or is disabled for this PF.\n");
  3688. if (pf->flags & I40E_FLAG_MFP_ENABLED)
  3689. goto out;
  3690. } else {
  3691. /* When status is not DISABLED then DCBX in FW */
  3692. pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
  3693. DCB_CAP_DCBX_VER_IEEE;
  3694. pf->flags |= I40E_FLAG_DCB_CAPABLE;
  3695. /* Enable DCB tagging only when more than one TC */
  3696. if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
  3697. pf->flags |= I40E_FLAG_DCB_ENABLED;
  3698. }
  3699. } else {
  3700. dev_info(&pf->pdev->dev, "AQ Querying DCB configuration failed: %d\n",
  3701. pf->hw.aq.asq_last_status);
  3702. }
  3703. out:
  3704. return err;
  3705. }
  3706. #endif /* CONFIG_I40E_DCB */
  3707. #define SPEED_SIZE 14
  3708. #define FC_SIZE 8
  3709. /**
  3710. * i40e_print_link_message - print link up or down
  3711. * @vsi: the VSI for which link needs a message
  3712. */
  3713. static void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
  3714. {
  3715. char speed[SPEED_SIZE] = "Unknown";
  3716. char fc[FC_SIZE] = "RX/TX";
  3717. if (!isup) {
  3718. netdev_info(vsi->netdev, "NIC Link is Down\n");
  3719. return;
  3720. }
  3721. switch (vsi->back->hw.phy.link_info.link_speed) {
  3722. case I40E_LINK_SPEED_40GB:
  3723. strncpy(speed, "40 Gbps", SPEED_SIZE);
  3724. break;
  3725. case I40E_LINK_SPEED_10GB:
  3726. strncpy(speed, "10 Gbps", SPEED_SIZE);
  3727. break;
  3728. case I40E_LINK_SPEED_1GB:
  3729. strncpy(speed, "1000 Mbps", SPEED_SIZE);
  3730. break;
  3731. default:
  3732. break;
  3733. }
  3734. switch (vsi->back->hw.fc.current_mode) {
  3735. case I40E_FC_FULL:
  3736. strncpy(fc, "RX/TX", FC_SIZE);
  3737. break;
  3738. case I40E_FC_TX_PAUSE:
  3739. strncpy(fc, "TX", FC_SIZE);
  3740. break;
  3741. case I40E_FC_RX_PAUSE:
  3742. strncpy(fc, "RX", FC_SIZE);
  3743. break;
  3744. default:
  3745. strncpy(fc, "None", FC_SIZE);
  3746. break;
  3747. }
  3748. netdev_info(vsi->netdev, "NIC Link is Up %s Full Duplex, Flow Control: %s\n",
  3749. speed, fc);
  3750. }
  3751. /**
  3752. * i40e_up_complete - Finish the last steps of bringing up a connection
  3753. * @vsi: the VSI being configured
  3754. **/
  3755. static int i40e_up_complete(struct i40e_vsi *vsi)
  3756. {
  3757. struct i40e_pf *pf = vsi->back;
  3758. int err;
  3759. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  3760. i40e_vsi_configure_msix(vsi);
  3761. else
  3762. i40e_configure_msi_and_legacy(vsi);
  3763. /* start rings */
  3764. err = i40e_vsi_control_rings(vsi, true);
  3765. if (err)
  3766. return err;
  3767. clear_bit(__I40E_DOWN, &vsi->state);
  3768. i40e_napi_enable_all(vsi);
  3769. i40e_vsi_enable_irq(vsi);
  3770. if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
  3771. (vsi->netdev)) {
  3772. i40e_print_link_message(vsi, true);
  3773. netif_tx_start_all_queues(vsi->netdev);
  3774. netif_carrier_on(vsi->netdev);
  3775. } else if (vsi->netdev) {
  3776. i40e_print_link_message(vsi, false);
  3777. }
  3778. /* replay FDIR SB filters */
  3779. if (vsi->type == I40E_VSI_FDIR)
  3780. i40e_fdir_filter_restore(vsi);
  3781. i40e_service_event_schedule(pf);
  3782. return 0;
  3783. }
  3784. /**
  3785. * i40e_vsi_reinit_locked - Reset the VSI
  3786. * @vsi: the VSI being configured
  3787. *
  3788. * Rebuild the ring structs after some configuration
  3789. * has changed, e.g. MTU size.
  3790. **/
  3791. static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
  3792. {
  3793. struct i40e_pf *pf = vsi->back;
  3794. WARN_ON(in_interrupt());
  3795. while (test_and_set_bit(__I40E_CONFIG_BUSY, &pf->state))
  3796. usleep_range(1000, 2000);
  3797. i40e_down(vsi);
  3798. /* Give a VF some time to respond to the reset. The
  3799. * two second wait is based upon the watchdog cycle in
  3800. * the VF driver.
  3801. */
  3802. if (vsi->type == I40E_VSI_SRIOV)
  3803. msleep(2000);
  3804. i40e_up(vsi);
  3805. clear_bit(__I40E_CONFIG_BUSY, &pf->state);
  3806. }
  3807. /**
  3808. * i40e_up - Bring the connection back up after being down
  3809. * @vsi: the VSI being configured
  3810. **/
  3811. int i40e_up(struct i40e_vsi *vsi)
  3812. {
  3813. int err;
  3814. err = i40e_vsi_configure(vsi);
  3815. if (!err)
  3816. err = i40e_up_complete(vsi);
  3817. return err;
  3818. }
  3819. /**
  3820. * i40e_down - Shutdown the connection processing
  3821. * @vsi: the VSI being stopped
  3822. **/
  3823. void i40e_down(struct i40e_vsi *vsi)
  3824. {
  3825. int i;
  3826. /* It is assumed that the caller of this function
  3827. * sets the vsi->state __I40E_DOWN bit.
  3828. */
  3829. if (vsi->netdev) {
  3830. netif_carrier_off(vsi->netdev);
  3831. netif_tx_disable(vsi->netdev);
  3832. }
  3833. i40e_vsi_disable_irq(vsi);
  3834. i40e_vsi_control_rings(vsi, false);
  3835. i40e_napi_disable_all(vsi);
  3836. for (i = 0; i < vsi->num_queue_pairs; i++) {
  3837. i40e_clean_tx_ring(vsi->tx_rings[i]);
  3838. i40e_clean_rx_ring(vsi->rx_rings[i]);
  3839. }
  3840. }
  3841. /**
  3842. * i40e_setup_tc - configure multiple traffic classes
  3843. * @netdev: net device to configure
  3844. * @tc: number of traffic classes to enable
  3845. **/
  3846. static int i40e_setup_tc(struct net_device *netdev, u8 tc)
  3847. {
  3848. struct i40e_netdev_priv *np = netdev_priv(netdev);
  3849. struct i40e_vsi *vsi = np->vsi;
  3850. struct i40e_pf *pf = vsi->back;
  3851. u8 enabled_tc = 0;
  3852. int ret = -EINVAL;
  3853. int i;
  3854. /* Check if DCB enabled to continue */
  3855. if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
  3856. netdev_info(netdev, "DCB is not enabled for adapter\n");
  3857. goto exit;
  3858. }
  3859. /* Check if MFP enabled */
  3860. if (pf->flags & I40E_FLAG_MFP_ENABLED) {
  3861. netdev_info(netdev, "Configuring TC not supported in MFP mode\n");
  3862. goto exit;
  3863. }
  3864. /* Check whether tc count is within enabled limit */
  3865. if (tc > i40e_pf_get_num_tc(pf)) {
  3866. netdev_info(netdev, "TC count greater than enabled on link for adapter\n");
  3867. goto exit;
  3868. }
  3869. /* Generate TC map for number of tc requested */
  3870. for (i = 0; i < tc; i++)
  3871. enabled_tc |= (1 << i);
  3872. /* Requesting same TC configuration as already enabled */
  3873. if (enabled_tc == vsi->tc_config.enabled_tc)
  3874. return 0;
  3875. /* Quiesce VSI queues */
  3876. i40e_quiesce_vsi(vsi);
  3877. /* Configure VSI for enabled TCs */
  3878. ret = i40e_vsi_config_tc(vsi, enabled_tc);
  3879. if (ret) {
  3880. netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
  3881. vsi->seid);
  3882. goto exit;
  3883. }
  3884. /* Unquiesce VSI */
  3885. i40e_unquiesce_vsi(vsi);
  3886. exit:
  3887. return ret;
  3888. }
  3889. /**
  3890. * i40e_open - Called when a network interface is made active
  3891. * @netdev: network interface device structure
  3892. *
  3893. * The open entry point is called when a network interface is made
  3894. * active by the system (IFF_UP). At this point all resources needed
  3895. * for transmit and receive operations are allocated, the interrupt
  3896. * handler is registered with the OS, the netdev watchdog subtask is
  3897. * enabled, and the stack is notified that the interface is ready.
  3898. *
  3899. * Returns 0 on success, negative value on failure
  3900. **/
  3901. static int i40e_open(struct net_device *netdev)
  3902. {
  3903. struct i40e_netdev_priv *np = netdev_priv(netdev);
  3904. struct i40e_vsi *vsi = np->vsi;
  3905. struct i40e_pf *pf = vsi->back;
  3906. int err;
  3907. /* disallow open during test or if eeprom is broken */
  3908. if (test_bit(__I40E_TESTING, &pf->state) ||
  3909. test_bit(__I40E_BAD_EEPROM, &pf->state))
  3910. return -EBUSY;
  3911. netif_carrier_off(netdev);
  3912. err = i40e_vsi_open(vsi);
  3913. if (err)
  3914. return err;
  3915. /* configure global TSO hardware offload settings */
  3916. wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
  3917. TCP_FLAG_FIN) >> 16);
  3918. wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
  3919. TCP_FLAG_FIN |
  3920. TCP_FLAG_CWR) >> 16);
  3921. wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
  3922. #ifdef CONFIG_I40E_VXLAN
  3923. vxlan_get_rx_port(netdev);
  3924. #endif
  3925. return 0;
  3926. }
  3927. /**
  3928. * i40e_vsi_open -
  3929. * @vsi: the VSI to open
  3930. *
  3931. * Finish initialization of the VSI.
  3932. *
  3933. * Returns 0 on success, negative value on failure
  3934. **/
  3935. int i40e_vsi_open(struct i40e_vsi *vsi)
  3936. {
  3937. struct i40e_pf *pf = vsi->back;
  3938. char int_name[IFNAMSIZ];
  3939. int err;
  3940. /* allocate descriptors */
  3941. err = i40e_vsi_setup_tx_resources(vsi);
  3942. if (err)
  3943. goto err_setup_tx;
  3944. err = i40e_vsi_setup_rx_resources(vsi);
  3945. if (err)
  3946. goto err_setup_rx;
  3947. err = i40e_vsi_configure(vsi);
  3948. if (err)
  3949. goto err_setup_rx;
  3950. if (vsi->netdev) {
  3951. snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
  3952. dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
  3953. err = i40e_vsi_request_irq(vsi, int_name);
  3954. if (err)
  3955. goto err_setup_rx;
  3956. /* Notify the stack of the actual queue counts. */
  3957. err = netif_set_real_num_tx_queues(vsi->netdev,
  3958. vsi->num_queue_pairs);
  3959. if (err)
  3960. goto err_set_queues;
  3961. err = netif_set_real_num_rx_queues(vsi->netdev,
  3962. vsi->num_queue_pairs);
  3963. if (err)
  3964. goto err_set_queues;
  3965. } else if (vsi->type == I40E_VSI_FDIR) {
  3966. snprintf(int_name, sizeof(int_name) - 1, "%s-fdir",
  3967. dev_driver_string(&pf->pdev->dev));
  3968. err = i40e_vsi_request_irq(vsi, int_name);
  3969. } else {
  3970. err = -EINVAL;
  3971. goto err_setup_rx;
  3972. }
  3973. err = i40e_up_complete(vsi);
  3974. if (err)
  3975. goto err_up_complete;
  3976. return 0;
  3977. err_up_complete:
  3978. i40e_down(vsi);
  3979. err_set_queues:
  3980. i40e_vsi_free_irq(vsi);
  3981. err_setup_rx:
  3982. i40e_vsi_free_rx_resources(vsi);
  3983. err_setup_tx:
  3984. i40e_vsi_free_tx_resources(vsi);
  3985. if (vsi == pf->vsi[pf->lan_vsi])
  3986. i40e_do_reset(pf, (1 << __I40E_PF_RESET_REQUESTED));
  3987. return err;
  3988. }
  3989. /**
  3990. * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
  3991. * @pf: Pointer to pf
  3992. *
  3993. * This function destroys the hlist where all the Flow Director
  3994. * filters were saved.
  3995. **/
  3996. static void i40e_fdir_filter_exit(struct i40e_pf *pf)
  3997. {
  3998. struct i40e_fdir_filter *filter;
  3999. struct hlist_node *node2;
  4000. hlist_for_each_entry_safe(filter, node2,
  4001. &pf->fdir_filter_list, fdir_node) {
  4002. hlist_del(&filter->fdir_node);
  4003. kfree(filter);
  4004. }
  4005. pf->fdir_pf_active_filters = 0;
  4006. }
  4007. /**
  4008. * i40e_close - Disables a network interface
  4009. * @netdev: network interface device structure
  4010. *
  4011. * The close entry point is called when an interface is de-activated
  4012. * by the OS. The hardware is still under the driver's control, but
  4013. * this netdev interface is disabled.
  4014. *
  4015. * Returns 0, this is not allowed to fail
  4016. **/
  4017. static int i40e_close(struct net_device *netdev)
  4018. {
  4019. struct i40e_netdev_priv *np = netdev_priv(netdev);
  4020. struct i40e_vsi *vsi = np->vsi;
  4021. i40e_vsi_close(vsi);
  4022. return 0;
  4023. }
  4024. /**
  4025. * i40e_do_reset - Start a PF or Core Reset sequence
  4026. * @pf: board private structure
  4027. * @reset_flags: which reset is requested
  4028. *
  4029. * The essential difference in resets is that the PF Reset
  4030. * doesn't clear the packet buffers, doesn't reset the PE
  4031. * firmware, and doesn't bother the other PFs on the chip.
  4032. **/
  4033. void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags)
  4034. {
  4035. u32 val;
  4036. WARN_ON(in_interrupt());
  4037. if (i40e_check_asq_alive(&pf->hw))
  4038. i40e_vc_notify_reset(pf);
  4039. /* do the biggest reset indicated */
  4040. if (reset_flags & (1 << __I40E_GLOBAL_RESET_REQUESTED)) {
  4041. /* Request a Global Reset
  4042. *
  4043. * This will start the chip's countdown to the actual full
  4044. * chip reset event, and a warning interrupt to be sent
  4045. * to all PFs, including the requestor. Our handler
  4046. * for the warning interrupt will deal with the shutdown
  4047. * and recovery of the switch setup.
  4048. */
  4049. dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
  4050. val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
  4051. val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
  4052. wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
  4053. } else if (reset_flags & (1 << __I40E_CORE_RESET_REQUESTED)) {
  4054. /* Request a Core Reset
  4055. *
  4056. * Same as Global Reset, except does *not* include the MAC/PHY
  4057. */
  4058. dev_dbg(&pf->pdev->dev, "CoreR requested\n");
  4059. val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
  4060. val |= I40E_GLGEN_RTRIG_CORER_MASK;
  4061. wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
  4062. i40e_flush(&pf->hw);
  4063. } else if (reset_flags & (1 << __I40E_EMP_RESET_REQUESTED)) {
  4064. /* Request a Firmware Reset
  4065. *
  4066. * Same as Global reset, plus restarting the
  4067. * embedded firmware engine.
  4068. */
  4069. /* enable EMP Reset */
  4070. val = rd32(&pf->hw, I40E_GLGEN_RSTENA_EMP);
  4071. val |= I40E_GLGEN_RSTENA_EMP_EMP_RST_ENA_MASK;
  4072. wr32(&pf->hw, I40E_GLGEN_RSTENA_EMP, val);
  4073. /* force the reset */
  4074. val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
  4075. val |= I40E_GLGEN_RTRIG_EMPFWR_MASK;
  4076. wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
  4077. i40e_flush(&pf->hw);
  4078. } else if (reset_flags & (1 << __I40E_PF_RESET_REQUESTED)) {
  4079. /* Request a PF Reset
  4080. *
  4081. * Resets only the PF-specific registers
  4082. *
  4083. * This goes directly to the tear-down and rebuild of
  4084. * the switch, since we need to do all the recovery as
  4085. * for the Core Reset.
  4086. */
  4087. dev_dbg(&pf->pdev->dev, "PFR requested\n");
  4088. i40e_handle_reset_warning(pf);
  4089. } else if (reset_flags & (1 << __I40E_REINIT_REQUESTED)) {
  4090. int v;
  4091. /* Find the VSI(s) that requested a re-init */
  4092. dev_info(&pf->pdev->dev,
  4093. "VSI reinit requested\n");
  4094. for (v = 0; v < pf->num_alloc_vsi; v++) {
  4095. struct i40e_vsi *vsi = pf->vsi[v];
  4096. if (vsi != NULL &&
  4097. test_bit(__I40E_REINIT_REQUESTED, &vsi->state)) {
  4098. i40e_vsi_reinit_locked(pf->vsi[v]);
  4099. clear_bit(__I40E_REINIT_REQUESTED, &vsi->state);
  4100. }
  4101. }
  4102. /* no further action needed, so return now */
  4103. return;
  4104. } else if (reset_flags & (1 << __I40E_DOWN_REQUESTED)) {
  4105. int v;
  4106. /* Find the VSI(s) that needs to be brought down */
  4107. dev_info(&pf->pdev->dev, "VSI down requested\n");
  4108. for (v = 0; v < pf->num_alloc_vsi; v++) {
  4109. struct i40e_vsi *vsi = pf->vsi[v];
  4110. if (vsi != NULL &&
  4111. test_bit(__I40E_DOWN_REQUESTED, &vsi->state)) {
  4112. set_bit(__I40E_DOWN, &vsi->state);
  4113. i40e_down(vsi);
  4114. clear_bit(__I40E_DOWN_REQUESTED, &vsi->state);
  4115. }
  4116. }
  4117. /* no further action needed, so return now */
  4118. return;
  4119. } else {
  4120. dev_info(&pf->pdev->dev,
  4121. "bad reset request 0x%08x\n", reset_flags);
  4122. return;
  4123. }
  4124. }
  4125. #ifdef CONFIG_I40E_DCB
  4126. /**
  4127. * i40e_dcb_need_reconfig - Check if DCB needs reconfig
  4128. * @pf: board private structure
  4129. * @old_cfg: current DCB config
  4130. * @new_cfg: new DCB config
  4131. **/
  4132. bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
  4133. struct i40e_dcbx_config *old_cfg,
  4134. struct i40e_dcbx_config *new_cfg)
  4135. {
  4136. bool need_reconfig = false;
  4137. /* Check if ETS configuration has changed */
  4138. if (memcmp(&new_cfg->etscfg,
  4139. &old_cfg->etscfg,
  4140. sizeof(new_cfg->etscfg))) {
  4141. /* If Priority Table has changed reconfig is needed */
  4142. if (memcmp(&new_cfg->etscfg.prioritytable,
  4143. &old_cfg->etscfg.prioritytable,
  4144. sizeof(new_cfg->etscfg.prioritytable))) {
  4145. need_reconfig = true;
  4146. dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
  4147. }
  4148. if (memcmp(&new_cfg->etscfg.tcbwtable,
  4149. &old_cfg->etscfg.tcbwtable,
  4150. sizeof(new_cfg->etscfg.tcbwtable)))
  4151. dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
  4152. if (memcmp(&new_cfg->etscfg.tsatable,
  4153. &old_cfg->etscfg.tsatable,
  4154. sizeof(new_cfg->etscfg.tsatable)))
  4155. dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
  4156. }
  4157. /* Check if PFC configuration has changed */
  4158. if (memcmp(&new_cfg->pfc,
  4159. &old_cfg->pfc,
  4160. sizeof(new_cfg->pfc))) {
  4161. need_reconfig = true;
  4162. dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
  4163. }
  4164. /* Check if APP Table has changed */
  4165. if (memcmp(&new_cfg->app,
  4166. &old_cfg->app,
  4167. sizeof(new_cfg->app))) {
  4168. need_reconfig = true;
  4169. dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
  4170. }
  4171. return need_reconfig;
  4172. }
  4173. /**
  4174. * i40e_handle_lldp_event - Handle LLDP Change MIB event
  4175. * @pf: board private structure
  4176. * @e: event info posted on ARQ
  4177. **/
  4178. static int i40e_handle_lldp_event(struct i40e_pf *pf,
  4179. struct i40e_arq_event_info *e)
  4180. {
  4181. struct i40e_aqc_lldp_get_mib *mib =
  4182. (struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
  4183. struct i40e_hw *hw = &pf->hw;
  4184. struct i40e_dcbx_config *dcbx_cfg = &hw->local_dcbx_config;
  4185. struct i40e_dcbx_config tmp_dcbx_cfg;
  4186. bool need_reconfig = false;
  4187. int ret = 0;
  4188. u8 type;
  4189. /* Not DCB capable or capability disabled */
  4190. if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
  4191. return ret;
  4192. /* Ignore if event is not for Nearest Bridge */
  4193. type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
  4194. & I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
  4195. if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
  4196. return ret;
  4197. /* Check MIB Type and return if event for Remote MIB update */
  4198. type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
  4199. if (type == I40E_AQ_LLDP_MIB_REMOTE) {
  4200. /* Update the remote cached instance and return */
  4201. ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
  4202. I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
  4203. &hw->remote_dcbx_config);
  4204. goto exit;
  4205. }
  4206. /* Convert/store the DCBX data from LLDPDU temporarily */
  4207. memset(&tmp_dcbx_cfg, 0, sizeof(tmp_dcbx_cfg));
  4208. ret = i40e_lldp_to_dcb_config(e->msg_buf, &tmp_dcbx_cfg);
  4209. if (ret) {
  4210. /* Error in LLDPDU parsing return */
  4211. dev_info(&pf->pdev->dev, "Failed parsing LLDPDU from event buffer\n");
  4212. goto exit;
  4213. }
  4214. /* No change detected in DCBX configs */
  4215. if (!memcmp(&tmp_dcbx_cfg, dcbx_cfg, sizeof(tmp_dcbx_cfg))) {
  4216. dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
  4217. goto exit;
  4218. }
  4219. need_reconfig = i40e_dcb_need_reconfig(pf, dcbx_cfg, &tmp_dcbx_cfg);
  4220. i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg);
  4221. /* Overwrite the new configuration */
  4222. *dcbx_cfg = tmp_dcbx_cfg;
  4223. if (!need_reconfig)
  4224. goto exit;
  4225. /* Enable DCB tagging only when more than one TC */
  4226. if (i40e_dcb_get_num_tc(dcbx_cfg) > 1)
  4227. pf->flags |= I40E_FLAG_DCB_ENABLED;
  4228. else
  4229. pf->flags &= ~I40E_FLAG_DCB_ENABLED;
  4230. /* Reconfiguration needed quiesce all VSIs */
  4231. i40e_pf_quiesce_all_vsi(pf);
  4232. /* Changes in configuration update VEB/VSI */
  4233. i40e_dcb_reconfigure(pf);
  4234. i40e_pf_unquiesce_all_vsi(pf);
  4235. exit:
  4236. return ret;
  4237. }
  4238. #endif /* CONFIG_I40E_DCB */
  4239. /**
  4240. * i40e_do_reset_safe - Protected reset path for userland calls.
  4241. * @pf: board private structure
  4242. * @reset_flags: which reset is requested
  4243. *
  4244. **/
  4245. void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
  4246. {
  4247. rtnl_lock();
  4248. i40e_do_reset(pf, reset_flags);
  4249. rtnl_unlock();
  4250. }
  4251. /**
  4252. * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
  4253. * @pf: board private structure
  4254. * @e: event info posted on ARQ
  4255. *
  4256. * Handler for LAN Queue Overflow Event generated by the firmware for PF
  4257. * and VF queues
  4258. **/
  4259. static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
  4260. struct i40e_arq_event_info *e)
  4261. {
  4262. struct i40e_aqc_lan_overflow *data =
  4263. (struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
  4264. u32 queue = le32_to_cpu(data->prtdcb_rupto);
  4265. u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
  4266. struct i40e_hw *hw = &pf->hw;
  4267. struct i40e_vf *vf;
  4268. u16 vf_id;
  4269. dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
  4270. queue, qtx_ctl);
  4271. /* Queue belongs to VF, find the VF and issue VF reset */
  4272. if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
  4273. >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
  4274. vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
  4275. >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
  4276. vf_id -= hw->func_caps.vf_base_id;
  4277. vf = &pf->vf[vf_id];
  4278. i40e_vc_notify_vf_reset(vf);
  4279. /* Allow VF to process pending reset notification */
  4280. msleep(20);
  4281. i40e_reset_vf(vf, false);
  4282. }
  4283. }
  4284. /**
  4285. * i40e_service_event_complete - Finish up the service event
  4286. * @pf: board private structure
  4287. **/
  4288. static void i40e_service_event_complete(struct i40e_pf *pf)
  4289. {
  4290. BUG_ON(!test_bit(__I40E_SERVICE_SCHED, &pf->state));
  4291. /* flush memory to make sure state is correct before next watchog */
  4292. smp_mb__before_atomic();
  4293. clear_bit(__I40E_SERVICE_SCHED, &pf->state);
  4294. }
  4295. /**
  4296. * i40e_get_current_fd_count - Get the count of FD filters programmed in the HW
  4297. * @pf: board private structure
  4298. **/
  4299. int i40e_get_current_fd_count(struct i40e_pf *pf)
  4300. {
  4301. int val, fcnt_prog;
  4302. val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
  4303. fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
  4304. ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
  4305. I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
  4306. return fcnt_prog;
  4307. }
  4308. /**
  4309. * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
  4310. * @pf: board private structure
  4311. **/
  4312. void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
  4313. {
  4314. u32 fcnt_prog, fcnt_avail;
  4315. /* Check if, FD SB or ATR was auto disabled and if there is enough room
  4316. * to re-enable
  4317. */
  4318. if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
  4319. (pf->flags & I40E_FLAG_FD_SB_ENABLED))
  4320. return;
  4321. fcnt_prog = i40e_get_current_fd_count(pf);
  4322. fcnt_avail = i40e_get_fd_cnt_all(pf);
  4323. if (fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) {
  4324. if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
  4325. (pf->auto_disable_flags & I40E_FLAG_FD_SB_ENABLED)) {
  4326. pf->auto_disable_flags &= ~I40E_FLAG_FD_SB_ENABLED;
  4327. dev_info(&pf->pdev->dev, "FD Sideband/ntuple is being enabled since we have space in the table now\n");
  4328. }
  4329. }
  4330. /* Wait for some more space to be available to turn on ATR */
  4331. if (fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM * 2)) {
  4332. if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
  4333. (pf->auto_disable_flags & I40E_FLAG_FD_ATR_ENABLED)) {
  4334. pf->auto_disable_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
  4335. dev_info(&pf->pdev->dev, "ATR is being enabled since we have space in the table now\n");
  4336. }
  4337. }
  4338. }
  4339. /**
  4340. * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
  4341. * @pf: board private structure
  4342. **/
  4343. static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
  4344. {
  4345. if (!(pf->flags & I40E_FLAG_FDIR_REQUIRES_REINIT))
  4346. return;
  4347. /* if interface is down do nothing */
  4348. if (test_bit(__I40E_DOWN, &pf->state))
  4349. return;
  4350. i40e_fdir_check_and_reenable(pf);
  4351. if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
  4352. (pf->flags & I40E_FLAG_FD_SB_ENABLED))
  4353. pf->flags &= ~I40E_FLAG_FDIR_REQUIRES_REINIT;
  4354. }
  4355. /**
  4356. * i40e_vsi_link_event - notify VSI of a link event
  4357. * @vsi: vsi to be notified
  4358. * @link_up: link up or down
  4359. **/
  4360. static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
  4361. {
  4362. if (!vsi)
  4363. return;
  4364. switch (vsi->type) {
  4365. case I40E_VSI_MAIN:
  4366. if (!vsi->netdev || !vsi->netdev_registered)
  4367. break;
  4368. if (link_up) {
  4369. netif_carrier_on(vsi->netdev);
  4370. netif_tx_wake_all_queues(vsi->netdev);
  4371. } else {
  4372. netif_carrier_off(vsi->netdev);
  4373. netif_tx_stop_all_queues(vsi->netdev);
  4374. }
  4375. break;
  4376. case I40E_VSI_SRIOV:
  4377. break;
  4378. case I40E_VSI_VMDQ2:
  4379. case I40E_VSI_CTRL:
  4380. case I40E_VSI_MIRROR:
  4381. default:
  4382. /* there is no notification for other VSIs */
  4383. break;
  4384. }
  4385. }
  4386. /**
  4387. * i40e_veb_link_event - notify elements on the veb of a link event
  4388. * @veb: veb to be notified
  4389. * @link_up: link up or down
  4390. **/
  4391. static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
  4392. {
  4393. struct i40e_pf *pf;
  4394. int i;
  4395. if (!veb || !veb->pf)
  4396. return;
  4397. pf = veb->pf;
  4398. /* depth first... */
  4399. for (i = 0; i < I40E_MAX_VEB; i++)
  4400. if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
  4401. i40e_veb_link_event(pf->veb[i], link_up);
  4402. /* ... now the local VSIs */
  4403. for (i = 0; i < pf->num_alloc_vsi; i++)
  4404. if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
  4405. i40e_vsi_link_event(pf->vsi[i], link_up);
  4406. }
  4407. /**
  4408. * i40e_link_event - Update netif_carrier status
  4409. * @pf: board private structure
  4410. **/
  4411. static void i40e_link_event(struct i40e_pf *pf)
  4412. {
  4413. bool new_link, old_link;
  4414. new_link = (pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP);
  4415. old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
  4416. if (new_link == old_link)
  4417. return;
  4418. if (!test_bit(__I40E_DOWN, &pf->vsi[pf->lan_vsi]->state))
  4419. i40e_print_link_message(pf->vsi[pf->lan_vsi], new_link);
  4420. /* Notify the base of the switch tree connected to
  4421. * the link. Floating VEBs are not notified.
  4422. */
  4423. if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
  4424. i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
  4425. else
  4426. i40e_vsi_link_event(pf->vsi[pf->lan_vsi], new_link);
  4427. if (pf->vf)
  4428. i40e_vc_notify_link_state(pf);
  4429. if (pf->flags & I40E_FLAG_PTP)
  4430. i40e_ptp_set_increment(pf);
  4431. }
  4432. /**
  4433. * i40e_check_hang_subtask - Check for hung queues and dropped interrupts
  4434. * @pf: board private structure
  4435. *
  4436. * Set the per-queue flags to request a check for stuck queues in the irq
  4437. * clean functions, then force interrupts to be sure the irq clean is called.
  4438. **/
  4439. static void i40e_check_hang_subtask(struct i40e_pf *pf)
  4440. {
  4441. int i, v;
  4442. /* If we're down or resetting, just bail */
  4443. if (test_bit(__I40E_CONFIG_BUSY, &pf->state))
  4444. return;
  4445. /* for each VSI/netdev
  4446. * for each Tx queue
  4447. * set the check flag
  4448. * for each q_vector
  4449. * force an interrupt
  4450. */
  4451. for (v = 0; v < pf->num_alloc_vsi; v++) {
  4452. struct i40e_vsi *vsi = pf->vsi[v];
  4453. int armed = 0;
  4454. if (!pf->vsi[v] ||
  4455. test_bit(__I40E_DOWN, &vsi->state) ||
  4456. (vsi->netdev && !netif_carrier_ok(vsi->netdev)))
  4457. continue;
  4458. for (i = 0; i < vsi->num_queue_pairs; i++) {
  4459. set_check_for_tx_hang(vsi->tx_rings[i]);
  4460. if (test_bit(__I40E_HANG_CHECK_ARMED,
  4461. &vsi->tx_rings[i]->state))
  4462. armed++;
  4463. }
  4464. if (armed) {
  4465. if (!(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
  4466. wr32(&vsi->back->hw, I40E_PFINT_DYN_CTL0,
  4467. (I40E_PFINT_DYN_CTL0_INTENA_MASK |
  4468. I40E_PFINT_DYN_CTL0_SWINT_TRIG_MASK));
  4469. } else {
  4470. u16 vec = vsi->base_vector - 1;
  4471. u32 val = (I40E_PFINT_DYN_CTLN_INTENA_MASK |
  4472. I40E_PFINT_DYN_CTLN_SWINT_TRIG_MASK);
  4473. for (i = 0; i < vsi->num_q_vectors; i++, vec++)
  4474. wr32(&vsi->back->hw,
  4475. I40E_PFINT_DYN_CTLN(vec), val);
  4476. }
  4477. i40e_flush(&vsi->back->hw);
  4478. }
  4479. }
  4480. }
  4481. /**
  4482. * i40e_watchdog_subtask - Check and bring link up
  4483. * @pf: board private structure
  4484. **/
  4485. static void i40e_watchdog_subtask(struct i40e_pf *pf)
  4486. {
  4487. int i;
  4488. /* if interface is down do nothing */
  4489. if (test_bit(__I40E_DOWN, &pf->state) ||
  4490. test_bit(__I40E_CONFIG_BUSY, &pf->state))
  4491. return;
  4492. /* Update the stats for active netdevs so the network stack
  4493. * can look at updated numbers whenever it cares to
  4494. */
  4495. for (i = 0; i < pf->num_alloc_vsi; i++)
  4496. if (pf->vsi[i] && pf->vsi[i]->netdev)
  4497. i40e_update_stats(pf->vsi[i]);
  4498. /* Update the stats for the active switching components */
  4499. for (i = 0; i < I40E_MAX_VEB; i++)
  4500. if (pf->veb[i])
  4501. i40e_update_veb_stats(pf->veb[i]);
  4502. i40e_ptp_rx_hang(pf->vsi[pf->lan_vsi]);
  4503. }
  4504. /**
  4505. * i40e_reset_subtask - Set up for resetting the device and driver
  4506. * @pf: board private structure
  4507. **/
  4508. static void i40e_reset_subtask(struct i40e_pf *pf)
  4509. {
  4510. u32 reset_flags = 0;
  4511. rtnl_lock();
  4512. if (test_bit(__I40E_REINIT_REQUESTED, &pf->state)) {
  4513. reset_flags |= (1 << __I40E_REINIT_REQUESTED);
  4514. clear_bit(__I40E_REINIT_REQUESTED, &pf->state);
  4515. }
  4516. if (test_bit(__I40E_PF_RESET_REQUESTED, &pf->state)) {
  4517. reset_flags |= (1 << __I40E_PF_RESET_REQUESTED);
  4518. clear_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
  4519. }
  4520. if (test_bit(__I40E_CORE_RESET_REQUESTED, &pf->state)) {
  4521. reset_flags |= (1 << __I40E_CORE_RESET_REQUESTED);
  4522. clear_bit(__I40E_CORE_RESET_REQUESTED, &pf->state);
  4523. }
  4524. if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state)) {
  4525. reset_flags |= (1 << __I40E_GLOBAL_RESET_REQUESTED);
  4526. clear_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state);
  4527. }
  4528. if (test_bit(__I40E_DOWN_REQUESTED, &pf->state)) {
  4529. reset_flags |= (1 << __I40E_DOWN_REQUESTED);
  4530. clear_bit(__I40E_DOWN_REQUESTED, &pf->state);
  4531. }
  4532. /* If there's a recovery already waiting, it takes
  4533. * precedence before starting a new reset sequence.
  4534. */
  4535. if (test_bit(__I40E_RESET_INTR_RECEIVED, &pf->state)) {
  4536. i40e_handle_reset_warning(pf);
  4537. goto unlock;
  4538. }
  4539. /* If we're already down or resetting, just bail */
  4540. if (reset_flags &&
  4541. !test_bit(__I40E_DOWN, &pf->state) &&
  4542. !test_bit(__I40E_CONFIG_BUSY, &pf->state))
  4543. i40e_do_reset(pf, reset_flags);
  4544. unlock:
  4545. rtnl_unlock();
  4546. }
  4547. /**
  4548. * i40e_handle_link_event - Handle link event
  4549. * @pf: board private structure
  4550. * @e: event info posted on ARQ
  4551. **/
  4552. static void i40e_handle_link_event(struct i40e_pf *pf,
  4553. struct i40e_arq_event_info *e)
  4554. {
  4555. struct i40e_hw *hw = &pf->hw;
  4556. struct i40e_aqc_get_link_status *status =
  4557. (struct i40e_aqc_get_link_status *)&e->desc.params.raw;
  4558. struct i40e_link_status *hw_link_info = &hw->phy.link_info;
  4559. /* save off old link status information */
  4560. memcpy(&pf->hw.phy.link_info_old, hw_link_info,
  4561. sizeof(pf->hw.phy.link_info_old));
  4562. /* update link status */
  4563. hw_link_info->phy_type = (enum i40e_aq_phy_type)status->phy_type;
  4564. hw_link_info->link_speed = (enum i40e_aq_link_speed)status->link_speed;
  4565. hw_link_info->link_info = status->link_info;
  4566. hw_link_info->an_info = status->an_info;
  4567. hw_link_info->ext_info = status->ext_info;
  4568. hw_link_info->lse_enable =
  4569. le16_to_cpu(status->command_flags) &
  4570. I40E_AQ_LSE_ENABLE;
  4571. /* process the event */
  4572. i40e_link_event(pf);
  4573. /* Do a new status request to re-enable LSE reporting
  4574. * and load new status information into the hw struct,
  4575. * then see if the status changed while processing the
  4576. * initial event.
  4577. */
  4578. i40e_aq_get_link_info(&pf->hw, true, NULL, NULL);
  4579. i40e_link_event(pf);
  4580. }
  4581. /**
  4582. * i40e_clean_adminq_subtask - Clean the AdminQ rings
  4583. * @pf: board private structure
  4584. **/
  4585. static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
  4586. {
  4587. struct i40e_arq_event_info event;
  4588. struct i40e_hw *hw = &pf->hw;
  4589. u16 pending, i = 0;
  4590. i40e_status ret;
  4591. u16 opcode;
  4592. u32 oldval;
  4593. u32 val;
  4594. if (!test_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state))
  4595. return;
  4596. /* check for error indications */
  4597. val = rd32(&pf->hw, pf->hw.aq.arq.len);
  4598. oldval = val;
  4599. if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
  4600. dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
  4601. val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
  4602. }
  4603. if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
  4604. dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
  4605. val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
  4606. }
  4607. if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
  4608. dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
  4609. val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
  4610. }
  4611. if (oldval != val)
  4612. wr32(&pf->hw, pf->hw.aq.arq.len, val);
  4613. val = rd32(&pf->hw, pf->hw.aq.asq.len);
  4614. oldval = val;
  4615. if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
  4616. dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
  4617. val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
  4618. }
  4619. if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
  4620. dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
  4621. val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
  4622. }
  4623. if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
  4624. dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
  4625. val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
  4626. }
  4627. if (oldval != val)
  4628. wr32(&pf->hw, pf->hw.aq.asq.len, val);
  4629. event.msg_size = I40E_MAX_AQ_BUF_SIZE;
  4630. event.msg_buf = kzalloc(event.msg_size, GFP_KERNEL);
  4631. if (!event.msg_buf)
  4632. return;
  4633. do {
  4634. event.msg_size = I40E_MAX_AQ_BUF_SIZE; /* reinit each time */
  4635. ret = i40e_clean_arq_element(hw, &event, &pending);
  4636. if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK) {
  4637. dev_info(&pf->pdev->dev, "No ARQ event found\n");
  4638. break;
  4639. } else if (ret) {
  4640. dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
  4641. break;
  4642. }
  4643. opcode = le16_to_cpu(event.desc.opcode);
  4644. switch (opcode) {
  4645. case i40e_aqc_opc_get_link_status:
  4646. i40e_handle_link_event(pf, &event);
  4647. break;
  4648. case i40e_aqc_opc_send_msg_to_pf:
  4649. ret = i40e_vc_process_vf_msg(pf,
  4650. le16_to_cpu(event.desc.retval),
  4651. le32_to_cpu(event.desc.cookie_high),
  4652. le32_to_cpu(event.desc.cookie_low),
  4653. event.msg_buf,
  4654. event.msg_size);
  4655. break;
  4656. case i40e_aqc_opc_lldp_update_mib:
  4657. dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
  4658. #ifdef CONFIG_I40E_DCB
  4659. rtnl_lock();
  4660. ret = i40e_handle_lldp_event(pf, &event);
  4661. rtnl_unlock();
  4662. #endif /* CONFIG_I40E_DCB */
  4663. break;
  4664. case i40e_aqc_opc_event_lan_overflow:
  4665. dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
  4666. i40e_handle_lan_overflow_event(pf, &event);
  4667. break;
  4668. case i40e_aqc_opc_send_msg_to_peer:
  4669. dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
  4670. break;
  4671. default:
  4672. dev_info(&pf->pdev->dev,
  4673. "ARQ Error: Unknown event 0x%04x received\n",
  4674. opcode);
  4675. break;
  4676. }
  4677. } while (pending && (i++ < pf->adminq_work_limit));
  4678. clear_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
  4679. /* re-enable Admin queue interrupt cause */
  4680. val = rd32(hw, I40E_PFINT_ICR0_ENA);
  4681. val |= I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
  4682. wr32(hw, I40E_PFINT_ICR0_ENA, val);
  4683. i40e_flush(hw);
  4684. kfree(event.msg_buf);
  4685. }
  4686. /**
  4687. * i40e_verify_eeprom - make sure eeprom is good to use
  4688. * @pf: board private structure
  4689. **/
  4690. static void i40e_verify_eeprom(struct i40e_pf *pf)
  4691. {
  4692. int err;
  4693. err = i40e_diag_eeprom_test(&pf->hw);
  4694. if (err) {
  4695. /* retry in case of garbage read */
  4696. err = i40e_diag_eeprom_test(&pf->hw);
  4697. if (err) {
  4698. dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
  4699. err);
  4700. set_bit(__I40E_BAD_EEPROM, &pf->state);
  4701. }
  4702. }
  4703. if (!err && test_bit(__I40E_BAD_EEPROM, &pf->state)) {
  4704. dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
  4705. clear_bit(__I40E_BAD_EEPROM, &pf->state);
  4706. }
  4707. }
  4708. /**
  4709. * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
  4710. * @veb: pointer to the VEB instance
  4711. *
  4712. * This is a recursive function that first builds the attached VSIs then
  4713. * recurses in to build the next layer of VEB. We track the connections
  4714. * through our own index numbers because the seid's from the HW could
  4715. * change across the reset.
  4716. **/
  4717. static int i40e_reconstitute_veb(struct i40e_veb *veb)
  4718. {
  4719. struct i40e_vsi *ctl_vsi = NULL;
  4720. struct i40e_pf *pf = veb->pf;
  4721. int v, veb_idx;
  4722. int ret;
  4723. /* build VSI that owns this VEB, temporarily attached to base VEB */
  4724. for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
  4725. if (pf->vsi[v] &&
  4726. pf->vsi[v]->veb_idx == veb->idx &&
  4727. pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
  4728. ctl_vsi = pf->vsi[v];
  4729. break;
  4730. }
  4731. }
  4732. if (!ctl_vsi) {
  4733. dev_info(&pf->pdev->dev,
  4734. "missing owner VSI for veb_idx %d\n", veb->idx);
  4735. ret = -ENOENT;
  4736. goto end_reconstitute;
  4737. }
  4738. if (ctl_vsi != pf->vsi[pf->lan_vsi])
  4739. ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
  4740. ret = i40e_add_vsi(ctl_vsi);
  4741. if (ret) {
  4742. dev_info(&pf->pdev->dev,
  4743. "rebuild of owner VSI failed: %d\n", ret);
  4744. goto end_reconstitute;
  4745. }
  4746. i40e_vsi_reset_stats(ctl_vsi);
  4747. /* create the VEB in the switch and move the VSI onto the VEB */
  4748. ret = i40e_add_veb(veb, ctl_vsi);
  4749. if (ret)
  4750. goto end_reconstitute;
  4751. /* create the remaining VSIs attached to this VEB */
  4752. for (v = 0; v < pf->num_alloc_vsi; v++) {
  4753. if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
  4754. continue;
  4755. if (pf->vsi[v]->veb_idx == veb->idx) {
  4756. struct i40e_vsi *vsi = pf->vsi[v];
  4757. vsi->uplink_seid = veb->seid;
  4758. ret = i40e_add_vsi(vsi);
  4759. if (ret) {
  4760. dev_info(&pf->pdev->dev,
  4761. "rebuild of vsi_idx %d failed: %d\n",
  4762. v, ret);
  4763. goto end_reconstitute;
  4764. }
  4765. i40e_vsi_reset_stats(vsi);
  4766. }
  4767. }
  4768. /* create any VEBs attached to this VEB - RECURSION */
  4769. for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
  4770. if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
  4771. pf->veb[veb_idx]->uplink_seid = veb->seid;
  4772. ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
  4773. if (ret)
  4774. break;
  4775. }
  4776. }
  4777. end_reconstitute:
  4778. return ret;
  4779. }
  4780. /**
  4781. * i40e_get_capabilities - get info about the HW
  4782. * @pf: the PF struct
  4783. **/
  4784. static int i40e_get_capabilities(struct i40e_pf *pf)
  4785. {
  4786. struct i40e_aqc_list_capabilities_element_resp *cap_buf;
  4787. u16 data_size;
  4788. int buf_len;
  4789. int err;
  4790. buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
  4791. do {
  4792. cap_buf = kzalloc(buf_len, GFP_KERNEL);
  4793. if (!cap_buf)
  4794. return -ENOMEM;
  4795. /* this loads the data into the hw struct for us */
  4796. err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
  4797. &data_size,
  4798. i40e_aqc_opc_list_func_capabilities,
  4799. NULL);
  4800. /* data loaded, buffer no longer needed */
  4801. kfree(cap_buf);
  4802. if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
  4803. /* retry with a larger buffer */
  4804. buf_len = data_size;
  4805. } else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK) {
  4806. dev_info(&pf->pdev->dev,
  4807. "capability discovery failed: aq=%d\n",
  4808. pf->hw.aq.asq_last_status);
  4809. return -ENODEV;
  4810. }
  4811. } while (err);
  4812. if (((pf->hw.aq.fw_maj_ver == 2) && (pf->hw.aq.fw_min_ver < 22)) ||
  4813. (pf->hw.aq.fw_maj_ver < 2)) {
  4814. pf->hw.func_caps.num_msix_vectors++;
  4815. pf->hw.func_caps.num_msix_vectors_vf++;
  4816. }
  4817. if (pf->hw.debug_mask & I40E_DEBUG_USER)
  4818. dev_info(&pf->pdev->dev,
  4819. "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",
  4820. pf->hw.pf_id, pf->hw.func_caps.num_vfs,
  4821. pf->hw.func_caps.num_msix_vectors,
  4822. pf->hw.func_caps.num_msix_vectors_vf,
  4823. pf->hw.func_caps.fd_filters_guaranteed,
  4824. pf->hw.func_caps.fd_filters_best_effort,
  4825. pf->hw.func_caps.num_tx_qp,
  4826. pf->hw.func_caps.num_vsis);
  4827. #define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
  4828. + pf->hw.func_caps.num_vfs)
  4829. if (pf->hw.revision_id == 0 && (DEF_NUM_VSI > pf->hw.func_caps.num_vsis)) {
  4830. dev_info(&pf->pdev->dev,
  4831. "got num_vsis %d, setting num_vsis to %d\n",
  4832. pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
  4833. pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
  4834. }
  4835. return 0;
  4836. }
  4837. static int i40e_vsi_clear(struct i40e_vsi *vsi);
  4838. /**
  4839. * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
  4840. * @pf: board private structure
  4841. **/
  4842. static void i40e_fdir_sb_setup(struct i40e_pf *pf)
  4843. {
  4844. struct i40e_vsi *vsi;
  4845. int i;
  4846. /* quick workaround for an NVM issue that leaves a critical register
  4847. * uninitialized
  4848. */
  4849. if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
  4850. static const u32 hkey[] = {
  4851. 0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
  4852. 0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
  4853. 0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
  4854. 0x95b3a76d};
  4855. for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
  4856. wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
  4857. }
  4858. if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
  4859. return;
  4860. /* find existing VSI and see if it needs configuring */
  4861. vsi = NULL;
  4862. for (i = 0; i < pf->num_alloc_vsi; i++) {
  4863. if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
  4864. vsi = pf->vsi[i];
  4865. break;
  4866. }
  4867. }
  4868. /* create a new VSI if none exists */
  4869. if (!vsi) {
  4870. vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
  4871. pf->vsi[pf->lan_vsi]->seid, 0);
  4872. if (!vsi) {
  4873. dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
  4874. pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
  4875. return;
  4876. }
  4877. }
  4878. i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
  4879. }
  4880. /**
  4881. * i40e_fdir_teardown - release the Flow Director resources
  4882. * @pf: board private structure
  4883. **/
  4884. static void i40e_fdir_teardown(struct i40e_pf *pf)
  4885. {
  4886. int i;
  4887. i40e_fdir_filter_exit(pf);
  4888. for (i = 0; i < pf->num_alloc_vsi; i++) {
  4889. if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
  4890. i40e_vsi_release(pf->vsi[i]);
  4891. break;
  4892. }
  4893. }
  4894. }
  4895. /**
  4896. * i40e_prep_for_reset - prep for the core to reset
  4897. * @pf: board private structure
  4898. *
  4899. * Close up the VFs and other things in prep for pf Reset.
  4900. **/
  4901. static void i40e_prep_for_reset(struct i40e_pf *pf)
  4902. {
  4903. struct i40e_hw *hw = &pf->hw;
  4904. i40e_status ret = 0;
  4905. u32 v;
  4906. clear_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
  4907. if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
  4908. return;
  4909. dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
  4910. /* quiesce the VSIs and their queues that are not already DOWN */
  4911. i40e_pf_quiesce_all_vsi(pf);
  4912. for (v = 0; v < pf->num_alloc_vsi; v++) {
  4913. if (pf->vsi[v])
  4914. pf->vsi[v]->seid = 0;
  4915. }
  4916. i40e_shutdown_adminq(&pf->hw);
  4917. /* call shutdown HMC */
  4918. if (hw->hmc.hmc_obj) {
  4919. ret = i40e_shutdown_lan_hmc(hw);
  4920. if (ret)
  4921. dev_warn(&pf->pdev->dev,
  4922. "shutdown_lan_hmc failed: %d\n", ret);
  4923. }
  4924. }
  4925. /**
  4926. * i40e_send_version - update firmware with driver version
  4927. * @pf: PF struct
  4928. */
  4929. static void i40e_send_version(struct i40e_pf *pf)
  4930. {
  4931. struct i40e_driver_version dv;
  4932. dv.major_version = DRV_VERSION_MAJOR;
  4933. dv.minor_version = DRV_VERSION_MINOR;
  4934. dv.build_version = DRV_VERSION_BUILD;
  4935. dv.subbuild_version = 0;
  4936. strncpy(dv.driver_string, DRV_VERSION, sizeof(dv.driver_string));
  4937. i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
  4938. }
  4939. /**
  4940. * i40e_reset_and_rebuild - reset and rebuild using a saved config
  4941. * @pf: board private structure
  4942. * @reinit: if the Main VSI needs to re-initialized.
  4943. **/
  4944. static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit)
  4945. {
  4946. struct i40e_hw *hw = &pf->hw;
  4947. i40e_status ret;
  4948. u32 v;
  4949. /* Now we wait for GRST to settle out.
  4950. * We don't have to delete the VEBs or VSIs from the hw switch
  4951. * because the reset will make them disappear.
  4952. */
  4953. ret = i40e_pf_reset(hw);
  4954. if (ret) {
  4955. dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
  4956. goto end_core_reset;
  4957. }
  4958. pf->pfr_count++;
  4959. if (test_bit(__I40E_DOWN, &pf->state))
  4960. goto end_core_reset;
  4961. dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
  4962. /* rebuild the basics for the AdminQ, HMC, and initial HW switch */
  4963. ret = i40e_init_adminq(&pf->hw);
  4964. if (ret) {
  4965. dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, %d\n", ret);
  4966. goto end_core_reset;
  4967. }
  4968. /* re-verify the eeprom if we just had an EMP reset */
  4969. if (test_bit(__I40E_EMP_RESET_REQUESTED, &pf->state)) {
  4970. clear_bit(__I40E_EMP_RESET_REQUESTED, &pf->state);
  4971. i40e_verify_eeprom(pf);
  4972. }
  4973. i40e_clear_pxe_mode(hw);
  4974. ret = i40e_get_capabilities(pf);
  4975. if (ret) {
  4976. dev_info(&pf->pdev->dev, "i40e_get_capabilities failed, %d\n",
  4977. ret);
  4978. goto end_core_reset;
  4979. }
  4980. ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
  4981. hw->func_caps.num_rx_qp,
  4982. pf->fcoe_hmc_cntx_num, pf->fcoe_hmc_filt_num);
  4983. if (ret) {
  4984. dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
  4985. goto end_core_reset;
  4986. }
  4987. ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
  4988. if (ret) {
  4989. dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
  4990. goto end_core_reset;
  4991. }
  4992. #ifdef CONFIG_I40E_DCB
  4993. ret = i40e_init_pf_dcb(pf);
  4994. if (ret) {
  4995. dev_info(&pf->pdev->dev, "init_pf_dcb failed: %d\n", ret);
  4996. goto end_core_reset;
  4997. }
  4998. #endif /* CONFIG_I40E_DCB */
  4999. /* do basic switch setup */
  5000. ret = i40e_setup_pf_switch(pf, reinit);
  5001. if (ret)
  5002. goto end_core_reset;
  5003. /* Rebuild the VSIs and VEBs that existed before reset.
  5004. * They are still in our local switch element arrays, so only
  5005. * need to rebuild the switch model in the HW.
  5006. *
  5007. * If there were VEBs but the reconstitution failed, we'll try
  5008. * try to recover minimal use by getting the basic PF VSI working.
  5009. */
  5010. if (pf->vsi[pf->lan_vsi]->uplink_seid != pf->mac_seid) {
  5011. dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
  5012. /* find the one VEB connected to the MAC, and find orphans */
  5013. for (v = 0; v < I40E_MAX_VEB; v++) {
  5014. if (!pf->veb[v])
  5015. continue;
  5016. if (pf->veb[v]->uplink_seid == pf->mac_seid ||
  5017. pf->veb[v]->uplink_seid == 0) {
  5018. ret = i40e_reconstitute_veb(pf->veb[v]);
  5019. if (!ret)
  5020. continue;
  5021. /* If Main VEB failed, we're in deep doodoo,
  5022. * so give up rebuilding the switch and set up
  5023. * for minimal rebuild of PF VSI.
  5024. * If orphan failed, we'll report the error
  5025. * but try to keep going.
  5026. */
  5027. if (pf->veb[v]->uplink_seid == pf->mac_seid) {
  5028. dev_info(&pf->pdev->dev,
  5029. "rebuild of switch failed: %d, will try to set up simple PF connection\n",
  5030. ret);
  5031. pf->vsi[pf->lan_vsi]->uplink_seid
  5032. = pf->mac_seid;
  5033. break;
  5034. } else if (pf->veb[v]->uplink_seid == 0) {
  5035. dev_info(&pf->pdev->dev,
  5036. "rebuild of orphan VEB failed: %d\n",
  5037. ret);
  5038. }
  5039. }
  5040. }
  5041. }
  5042. if (pf->vsi[pf->lan_vsi]->uplink_seid == pf->mac_seid) {
  5043. dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
  5044. /* no VEB, so rebuild only the Main VSI */
  5045. ret = i40e_add_vsi(pf->vsi[pf->lan_vsi]);
  5046. if (ret) {
  5047. dev_info(&pf->pdev->dev,
  5048. "rebuild of Main VSI failed: %d\n", ret);
  5049. goto end_core_reset;
  5050. }
  5051. }
  5052. /* reinit the misc interrupt */
  5053. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  5054. ret = i40e_setup_misc_vector(pf);
  5055. /* restart the VSIs that were rebuilt and running before the reset */
  5056. i40e_pf_unquiesce_all_vsi(pf);
  5057. if (pf->num_alloc_vfs) {
  5058. for (v = 0; v < pf->num_alloc_vfs; v++)
  5059. i40e_reset_vf(&pf->vf[v], true);
  5060. }
  5061. /* tell the firmware that we're starting */
  5062. i40e_send_version(pf);
  5063. end_core_reset:
  5064. clear_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state);
  5065. }
  5066. /**
  5067. * i40e_handle_reset_warning - prep for the pf to reset, reset and rebuild
  5068. * @pf: board private structure
  5069. *
  5070. * Close up the VFs and other things in prep for a Core Reset,
  5071. * then get ready to rebuild the world.
  5072. **/
  5073. static void i40e_handle_reset_warning(struct i40e_pf *pf)
  5074. {
  5075. i40e_prep_for_reset(pf);
  5076. i40e_reset_and_rebuild(pf, false);
  5077. }
  5078. /**
  5079. * i40e_handle_mdd_event
  5080. * @pf: pointer to the pf structure
  5081. *
  5082. * Called from the MDD irq handler to identify possibly malicious vfs
  5083. **/
  5084. static void i40e_handle_mdd_event(struct i40e_pf *pf)
  5085. {
  5086. struct i40e_hw *hw = &pf->hw;
  5087. bool mdd_detected = false;
  5088. bool pf_mdd_detected = false;
  5089. struct i40e_vf *vf;
  5090. u32 reg;
  5091. int i;
  5092. if (!test_bit(__I40E_MDD_EVENT_PENDING, &pf->state))
  5093. return;
  5094. /* find what triggered the MDD event */
  5095. reg = rd32(hw, I40E_GL_MDET_TX);
  5096. if (reg & I40E_GL_MDET_TX_VALID_MASK) {
  5097. u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
  5098. I40E_GL_MDET_TX_PF_NUM_SHIFT;
  5099. u8 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
  5100. I40E_GL_MDET_TX_VF_NUM_SHIFT;
  5101. u8 event = (reg & I40E_GL_MDET_TX_EVENT_SHIFT) >>
  5102. I40E_GL_MDET_TX_EVENT_SHIFT;
  5103. u8 queue = (reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
  5104. I40E_GL_MDET_TX_QUEUE_SHIFT;
  5105. dev_info(&pf->pdev->dev,
  5106. "Malicious Driver Detection event 0x%02x on TX queue %d pf number 0x%02x vf number 0x%02x\n",
  5107. event, queue, pf_num, vf_num);
  5108. wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
  5109. mdd_detected = true;
  5110. }
  5111. reg = rd32(hw, I40E_GL_MDET_RX);
  5112. if (reg & I40E_GL_MDET_RX_VALID_MASK) {
  5113. u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
  5114. I40E_GL_MDET_RX_FUNCTION_SHIFT;
  5115. u8 event = (reg & I40E_GL_MDET_RX_EVENT_SHIFT) >>
  5116. I40E_GL_MDET_RX_EVENT_SHIFT;
  5117. u8 queue = (reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
  5118. I40E_GL_MDET_RX_QUEUE_SHIFT;
  5119. dev_info(&pf->pdev->dev,
  5120. "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
  5121. event, queue, func);
  5122. wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
  5123. mdd_detected = true;
  5124. }
  5125. if (mdd_detected) {
  5126. reg = rd32(hw, I40E_PF_MDET_TX);
  5127. if (reg & I40E_PF_MDET_TX_VALID_MASK) {
  5128. wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
  5129. dev_info(&pf->pdev->dev,
  5130. "MDD TX event is for this function 0x%08x, requesting PF reset.\n",
  5131. reg);
  5132. pf_mdd_detected = true;
  5133. }
  5134. reg = rd32(hw, I40E_PF_MDET_RX);
  5135. if (reg & I40E_PF_MDET_RX_VALID_MASK) {
  5136. wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
  5137. dev_info(&pf->pdev->dev,
  5138. "MDD RX event is for this function 0x%08x, requesting PF reset.\n",
  5139. reg);
  5140. pf_mdd_detected = true;
  5141. }
  5142. /* Queue belongs to the PF, initiate a reset */
  5143. if (pf_mdd_detected) {
  5144. set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
  5145. i40e_service_event_schedule(pf);
  5146. }
  5147. }
  5148. /* see if one of the VFs needs its hand slapped */
  5149. for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
  5150. vf = &(pf->vf[i]);
  5151. reg = rd32(hw, I40E_VP_MDET_TX(i));
  5152. if (reg & I40E_VP_MDET_TX_VALID_MASK) {
  5153. wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
  5154. vf->num_mdd_events++;
  5155. dev_info(&pf->pdev->dev, "MDD TX event on VF %d\n", i);
  5156. }
  5157. reg = rd32(hw, I40E_VP_MDET_RX(i));
  5158. if (reg & I40E_VP_MDET_RX_VALID_MASK) {
  5159. wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
  5160. vf->num_mdd_events++;
  5161. dev_info(&pf->pdev->dev, "MDD RX event on VF %d\n", i);
  5162. }
  5163. if (vf->num_mdd_events > I40E_DEFAULT_NUM_MDD_EVENTS_ALLOWED) {
  5164. dev_info(&pf->pdev->dev,
  5165. "Too many MDD events on VF %d, disabled\n", i);
  5166. dev_info(&pf->pdev->dev,
  5167. "Use PF Control I/F to re-enable the VF\n");
  5168. set_bit(I40E_VF_STAT_DISABLED, &vf->vf_states);
  5169. }
  5170. }
  5171. /* re-enable mdd interrupt cause */
  5172. clear_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
  5173. reg = rd32(hw, I40E_PFINT_ICR0_ENA);
  5174. reg |= I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
  5175. wr32(hw, I40E_PFINT_ICR0_ENA, reg);
  5176. i40e_flush(hw);
  5177. }
  5178. #ifdef CONFIG_I40E_VXLAN
  5179. /**
  5180. * i40e_sync_vxlan_filters_subtask - Sync the VSI filter list with HW
  5181. * @pf: board private structure
  5182. **/
  5183. static void i40e_sync_vxlan_filters_subtask(struct i40e_pf *pf)
  5184. {
  5185. struct i40e_hw *hw = &pf->hw;
  5186. i40e_status ret;
  5187. u8 filter_index;
  5188. __be16 port;
  5189. int i;
  5190. if (!(pf->flags & I40E_FLAG_VXLAN_FILTER_SYNC))
  5191. return;
  5192. pf->flags &= ~I40E_FLAG_VXLAN_FILTER_SYNC;
  5193. for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
  5194. if (pf->pending_vxlan_bitmap & (1 << i)) {
  5195. pf->pending_vxlan_bitmap &= ~(1 << i);
  5196. port = pf->vxlan_ports[i];
  5197. ret = port ?
  5198. i40e_aq_add_udp_tunnel(hw, ntohs(port),
  5199. I40E_AQC_TUNNEL_TYPE_VXLAN,
  5200. &filter_index, NULL)
  5201. : i40e_aq_del_udp_tunnel(hw, i, NULL);
  5202. if (ret) {
  5203. dev_info(&pf->pdev->dev, "Failed to execute AQ command for %s port %d with index %d\n",
  5204. port ? "adding" : "deleting",
  5205. ntohs(port), port ? i : i);
  5206. pf->vxlan_ports[i] = 0;
  5207. } else {
  5208. dev_info(&pf->pdev->dev, "%s port %d with AQ command with index %d\n",
  5209. port ? "Added" : "Deleted",
  5210. ntohs(port), port ? i : filter_index);
  5211. }
  5212. }
  5213. }
  5214. }
  5215. #endif
  5216. /**
  5217. * i40e_service_task - Run the driver's async subtasks
  5218. * @work: pointer to work_struct containing our data
  5219. **/
  5220. static void i40e_service_task(struct work_struct *work)
  5221. {
  5222. struct i40e_pf *pf = container_of(work,
  5223. struct i40e_pf,
  5224. service_task);
  5225. unsigned long start_time = jiffies;
  5226. /* don't bother with service tasks if a reset is in progress */
  5227. if (test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
  5228. i40e_service_event_complete(pf);
  5229. return;
  5230. }
  5231. i40e_reset_subtask(pf);
  5232. i40e_handle_mdd_event(pf);
  5233. i40e_vc_process_vflr_event(pf);
  5234. i40e_watchdog_subtask(pf);
  5235. i40e_fdir_reinit_subtask(pf);
  5236. i40e_check_hang_subtask(pf);
  5237. i40e_sync_filters_subtask(pf);
  5238. #ifdef CONFIG_I40E_VXLAN
  5239. i40e_sync_vxlan_filters_subtask(pf);
  5240. #endif
  5241. i40e_clean_adminq_subtask(pf);
  5242. i40e_service_event_complete(pf);
  5243. /* If the tasks have taken longer than one timer cycle or there
  5244. * is more work to be done, reschedule the service task now
  5245. * rather than wait for the timer to tick again.
  5246. */
  5247. if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
  5248. test_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state) ||
  5249. test_bit(__I40E_MDD_EVENT_PENDING, &pf->state) ||
  5250. test_bit(__I40E_VFLR_EVENT_PENDING, &pf->state))
  5251. i40e_service_event_schedule(pf);
  5252. }
  5253. /**
  5254. * i40e_service_timer - timer callback
  5255. * @data: pointer to PF struct
  5256. **/
  5257. static void i40e_service_timer(unsigned long data)
  5258. {
  5259. struct i40e_pf *pf = (struct i40e_pf *)data;
  5260. mod_timer(&pf->service_timer,
  5261. round_jiffies(jiffies + pf->service_timer_period));
  5262. i40e_service_event_schedule(pf);
  5263. }
  5264. /**
  5265. * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
  5266. * @vsi: the VSI being configured
  5267. **/
  5268. static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
  5269. {
  5270. struct i40e_pf *pf = vsi->back;
  5271. switch (vsi->type) {
  5272. case I40E_VSI_MAIN:
  5273. vsi->alloc_queue_pairs = pf->num_lan_qps;
  5274. vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
  5275. I40E_REQ_DESCRIPTOR_MULTIPLE);
  5276. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  5277. vsi->num_q_vectors = pf->num_lan_msix;
  5278. else
  5279. vsi->num_q_vectors = 1;
  5280. break;
  5281. case I40E_VSI_FDIR:
  5282. vsi->alloc_queue_pairs = 1;
  5283. vsi->num_desc = ALIGN(I40E_FDIR_RING_COUNT,
  5284. I40E_REQ_DESCRIPTOR_MULTIPLE);
  5285. vsi->num_q_vectors = 1;
  5286. break;
  5287. case I40E_VSI_VMDQ2:
  5288. vsi->alloc_queue_pairs = pf->num_vmdq_qps;
  5289. vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
  5290. I40E_REQ_DESCRIPTOR_MULTIPLE);
  5291. vsi->num_q_vectors = pf->num_vmdq_msix;
  5292. break;
  5293. case I40E_VSI_SRIOV:
  5294. vsi->alloc_queue_pairs = pf->num_vf_qps;
  5295. vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
  5296. I40E_REQ_DESCRIPTOR_MULTIPLE);
  5297. break;
  5298. default:
  5299. WARN_ON(1);
  5300. return -ENODATA;
  5301. }
  5302. return 0;
  5303. }
  5304. /**
  5305. * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
  5306. * @type: VSI pointer
  5307. * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
  5308. *
  5309. * On error: returns error code (negative)
  5310. * On success: returns 0
  5311. **/
  5312. static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
  5313. {
  5314. int size;
  5315. int ret = 0;
  5316. /* allocate memory for both Tx and Rx ring pointers */
  5317. size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs * 2;
  5318. vsi->tx_rings = kzalloc(size, GFP_KERNEL);
  5319. if (!vsi->tx_rings)
  5320. return -ENOMEM;
  5321. vsi->rx_rings = &vsi->tx_rings[vsi->alloc_queue_pairs];
  5322. if (alloc_qvectors) {
  5323. /* allocate memory for q_vector pointers */
  5324. size = sizeof(struct i40e_q_vectors *) * vsi->num_q_vectors;
  5325. vsi->q_vectors = kzalloc(size, GFP_KERNEL);
  5326. if (!vsi->q_vectors) {
  5327. ret = -ENOMEM;
  5328. goto err_vectors;
  5329. }
  5330. }
  5331. return ret;
  5332. err_vectors:
  5333. kfree(vsi->tx_rings);
  5334. return ret;
  5335. }
  5336. /**
  5337. * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
  5338. * @pf: board private structure
  5339. * @type: type of VSI
  5340. *
  5341. * On error: returns error code (negative)
  5342. * On success: returns vsi index in PF (positive)
  5343. **/
  5344. static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
  5345. {
  5346. int ret = -ENODEV;
  5347. struct i40e_vsi *vsi;
  5348. int vsi_idx;
  5349. int i;
  5350. /* Need to protect the allocation of the VSIs at the PF level */
  5351. mutex_lock(&pf->switch_mutex);
  5352. /* VSI list may be fragmented if VSI creation/destruction has
  5353. * been happening. We can afford to do a quick scan to look
  5354. * for any free VSIs in the list.
  5355. *
  5356. * find next empty vsi slot, looping back around if necessary
  5357. */
  5358. i = pf->next_vsi;
  5359. while (i < pf->num_alloc_vsi && pf->vsi[i])
  5360. i++;
  5361. if (i >= pf->num_alloc_vsi) {
  5362. i = 0;
  5363. while (i < pf->next_vsi && pf->vsi[i])
  5364. i++;
  5365. }
  5366. if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
  5367. vsi_idx = i; /* Found one! */
  5368. } else {
  5369. ret = -ENODEV;
  5370. goto unlock_pf; /* out of VSI slots! */
  5371. }
  5372. pf->next_vsi = ++i;
  5373. vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
  5374. if (!vsi) {
  5375. ret = -ENOMEM;
  5376. goto unlock_pf;
  5377. }
  5378. vsi->type = type;
  5379. vsi->back = pf;
  5380. set_bit(__I40E_DOWN, &vsi->state);
  5381. vsi->flags = 0;
  5382. vsi->idx = vsi_idx;
  5383. vsi->rx_itr_setting = pf->rx_itr_default;
  5384. vsi->tx_itr_setting = pf->tx_itr_default;
  5385. vsi->netdev_registered = false;
  5386. vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
  5387. INIT_LIST_HEAD(&vsi->mac_filter_list);
  5388. vsi->irqs_ready = false;
  5389. ret = i40e_set_num_rings_in_vsi(vsi);
  5390. if (ret)
  5391. goto err_rings;
  5392. ret = i40e_vsi_alloc_arrays(vsi, true);
  5393. if (ret)
  5394. goto err_rings;
  5395. /* Setup default MSIX irq handler for VSI */
  5396. i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
  5397. pf->vsi[vsi_idx] = vsi;
  5398. ret = vsi_idx;
  5399. goto unlock_pf;
  5400. err_rings:
  5401. pf->next_vsi = i - 1;
  5402. kfree(vsi);
  5403. unlock_pf:
  5404. mutex_unlock(&pf->switch_mutex);
  5405. return ret;
  5406. }
  5407. /**
  5408. * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
  5409. * @type: VSI pointer
  5410. * @free_qvectors: a bool to specify if q_vectors need to be freed.
  5411. *
  5412. * On error: returns error code (negative)
  5413. * On success: returns 0
  5414. **/
  5415. static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
  5416. {
  5417. /* free the ring and vector containers */
  5418. if (free_qvectors) {
  5419. kfree(vsi->q_vectors);
  5420. vsi->q_vectors = NULL;
  5421. }
  5422. kfree(vsi->tx_rings);
  5423. vsi->tx_rings = NULL;
  5424. vsi->rx_rings = NULL;
  5425. }
  5426. /**
  5427. * i40e_vsi_clear - Deallocate the VSI provided
  5428. * @vsi: the VSI being un-configured
  5429. **/
  5430. static int i40e_vsi_clear(struct i40e_vsi *vsi)
  5431. {
  5432. struct i40e_pf *pf;
  5433. if (!vsi)
  5434. return 0;
  5435. if (!vsi->back)
  5436. goto free_vsi;
  5437. pf = vsi->back;
  5438. mutex_lock(&pf->switch_mutex);
  5439. if (!pf->vsi[vsi->idx]) {
  5440. dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](%p,type %d)\n",
  5441. vsi->idx, vsi->idx, vsi, vsi->type);
  5442. goto unlock_vsi;
  5443. }
  5444. if (pf->vsi[vsi->idx] != vsi) {
  5445. dev_err(&pf->pdev->dev,
  5446. "pf->vsi[%d](%p, type %d) != vsi[%d](%p,type %d): no free!\n",
  5447. pf->vsi[vsi->idx]->idx,
  5448. pf->vsi[vsi->idx],
  5449. pf->vsi[vsi->idx]->type,
  5450. vsi->idx, vsi, vsi->type);
  5451. goto unlock_vsi;
  5452. }
  5453. /* updates the pf for this cleared vsi */
  5454. i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
  5455. i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
  5456. i40e_vsi_free_arrays(vsi, true);
  5457. pf->vsi[vsi->idx] = NULL;
  5458. if (vsi->idx < pf->next_vsi)
  5459. pf->next_vsi = vsi->idx;
  5460. unlock_vsi:
  5461. mutex_unlock(&pf->switch_mutex);
  5462. free_vsi:
  5463. kfree(vsi);
  5464. return 0;
  5465. }
  5466. /**
  5467. * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
  5468. * @vsi: the VSI being cleaned
  5469. **/
  5470. static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
  5471. {
  5472. int i;
  5473. if (vsi->tx_rings && vsi->tx_rings[0]) {
  5474. for (i = 0; i < vsi->alloc_queue_pairs; i++) {
  5475. kfree_rcu(vsi->tx_rings[i], rcu);
  5476. vsi->tx_rings[i] = NULL;
  5477. vsi->rx_rings[i] = NULL;
  5478. }
  5479. }
  5480. }
  5481. /**
  5482. * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
  5483. * @vsi: the VSI being configured
  5484. **/
  5485. static int i40e_alloc_rings(struct i40e_vsi *vsi)
  5486. {
  5487. struct i40e_ring *tx_ring, *rx_ring;
  5488. struct i40e_pf *pf = vsi->back;
  5489. int i;
  5490. /* Set basic values in the rings to be used later during open() */
  5491. for (i = 0; i < vsi->alloc_queue_pairs; i++) {
  5492. /* allocate space for both Tx and Rx in one shot */
  5493. tx_ring = kzalloc(sizeof(struct i40e_ring) * 2, GFP_KERNEL);
  5494. if (!tx_ring)
  5495. goto err_out;
  5496. tx_ring->queue_index = i;
  5497. tx_ring->reg_idx = vsi->base_queue + i;
  5498. tx_ring->ring_active = false;
  5499. tx_ring->vsi = vsi;
  5500. tx_ring->netdev = vsi->netdev;
  5501. tx_ring->dev = &pf->pdev->dev;
  5502. tx_ring->count = vsi->num_desc;
  5503. tx_ring->size = 0;
  5504. tx_ring->dcb_tc = 0;
  5505. vsi->tx_rings[i] = tx_ring;
  5506. rx_ring = &tx_ring[1];
  5507. rx_ring->queue_index = i;
  5508. rx_ring->reg_idx = vsi->base_queue + i;
  5509. rx_ring->ring_active = false;
  5510. rx_ring->vsi = vsi;
  5511. rx_ring->netdev = vsi->netdev;
  5512. rx_ring->dev = &pf->pdev->dev;
  5513. rx_ring->count = vsi->num_desc;
  5514. rx_ring->size = 0;
  5515. rx_ring->dcb_tc = 0;
  5516. if (pf->flags & I40E_FLAG_16BYTE_RX_DESC_ENABLED)
  5517. set_ring_16byte_desc_enabled(rx_ring);
  5518. else
  5519. clear_ring_16byte_desc_enabled(rx_ring);
  5520. vsi->rx_rings[i] = rx_ring;
  5521. }
  5522. return 0;
  5523. err_out:
  5524. i40e_vsi_clear_rings(vsi);
  5525. return -ENOMEM;
  5526. }
  5527. /**
  5528. * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
  5529. * @pf: board private structure
  5530. * @vectors: the number of MSI-X vectors to request
  5531. *
  5532. * Returns the number of vectors reserved, or error
  5533. **/
  5534. static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
  5535. {
  5536. vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
  5537. I40E_MIN_MSIX, vectors);
  5538. if (vectors < 0) {
  5539. dev_info(&pf->pdev->dev,
  5540. "MSI-X vector reservation failed: %d\n", vectors);
  5541. vectors = 0;
  5542. }
  5543. return vectors;
  5544. }
  5545. /**
  5546. * i40e_init_msix - Setup the MSIX capability
  5547. * @pf: board private structure
  5548. *
  5549. * Work with the OS to set up the MSIX vectors needed.
  5550. *
  5551. * Returns 0 on success, negative on failure
  5552. **/
  5553. static int i40e_init_msix(struct i40e_pf *pf)
  5554. {
  5555. i40e_status err = 0;
  5556. struct i40e_hw *hw = &pf->hw;
  5557. int v_budget, i;
  5558. int vec;
  5559. if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
  5560. return -ENODEV;
  5561. /* The number of vectors we'll request will be comprised of:
  5562. * - Add 1 for "other" cause for Admin Queue events, etc.
  5563. * - The number of LAN queue pairs
  5564. * - Queues being used for RSS.
  5565. * We don't need as many as max_rss_size vectors.
  5566. * use rss_size instead in the calculation since that
  5567. * is governed by number of cpus in the system.
  5568. * - assumes symmetric Tx/Rx pairing
  5569. * - The number of VMDq pairs
  5570. * Once we count this up, try the request.
  5571. *
  5572. * If we can't get what we want, we'll simplify to nearly nothing
  5573. * and try again. If that still fails, we punt.
  5574. */
  5575. pf->num_lan_msix = pf->num_lan_qps - (pf->rss_size_max - pf->rss_size);
  5576. pf->num_vmdq_msix = pf->num_vmdq_qps;
  5577. v_budget = 1 + pf->num_lan_msix;
  5578. v_budget += (pf->num_vmdq_vsis * pf->num_vmdq_msix);
  5579. if (pf->flags & I40E_FLAG_FD_SB_ENABLED)
  5580. v_budget++;
  5581. /* Scale down if necessary, and the rings will share vectors */
  5582. v_budget = min_t(int, v_budget, hw->func_caps.num_msix_vectors);
  5583. pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
  5584. GFP_KERNEL);
  5585. if (!pf->msix_entries)
  5586. return -ENOMEM;
  5587. for (i = 0; i < v_budget; i++)
  5588. pf->msix_entries[i].entry = i;
  5589. vec = i40e_reserve_msix_vectors(pf, v_budget);
  5590. if (vec != v_budget) {
  5591. /* If we have limited resources, we will start with no vectors
  5592. * for the special features and then allocate vectors to some
  5593. * of these features based on the policy and at the end disable
  5594. * the features that did not get any vectors.
  5595. */
  5596. pf->num_vmdq_msix = 0;
  5597. }
  5598. if (vec < I40E_MIN_MSIX) {
  5599. pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
  5600. kfree(pf->msix_entries);
  5601. pf->msix_entries = NULL;
  5602. return -ENODEV;
  5603. } else if (vec == I40E_MIN_MSIX) {
  5604. /* Adjust for minimal MSIX use */
  5605. pf->num_vmdq_vsis = 0;
  5606. pf->num_vmdq_qps = 0;
  5607. pf->num_lan_qps = 1;
  5608. pf->num_lan_msix = 1;
  5609. } else if (vec != v_budget) {
  5610. /* reserve the misc vector */
  5611. vec--;
  5612. /* Scale vector usage down */
  5613. pf->num_vmdq_msix = 1; /* force VMDqs to only one vector */
  5614. pf->num_vmdq_vsis = 1;
  5615. /* partition out the remaining vectors */
  5616. switch (vec) {
  5617. case 2:
  5618. pf->num_lan_msix = 1;
  5619. break;
  5620. case 3:
  5621. pf->num_lan_msix = 2;
  5622. break;
  5623. default:
  5624. pf->num_lan_msix = min_t(int, (vec / 2),
  5625. pf->num_lan_qps);
  5626. pf->num_vmdq_vsis = min_t(int, (vec - pf->num_lan_msix),
  5627. I40E_DEFAULT_NUM_VMDQ_VSI);
  5628. break;
  5629. }
  5630. }
  5631. if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
  5632. (pf->num_vmdq_msix == 0)) {
  5633. dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
  5634. pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
  5635. }
  5636. return err;
  5637. }
  5638. /**
  5639. * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
  5640. * @vsi: the VSI being configured
  5641. * @v_idx: index of the vector in the vsi struct
  5642. *
  5643. * We allocate one q_vector. If allocation fails we return -ENOMEM.
  5644. **/
  5645. static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx)
  5646. {
  5647. struct i40e_q_vector *q_vector;
  5648. /* allocate q_vector */
  5649. q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
  5650. if (!q_vector)
  5651. return -ENOMEM;
  5652. q_vector->vsi = vsi;
  5653. q_vector->v_idx = v_idx;
  5654. cpumask_set_cpu(v_idx, &q_vector->affinity_mask);
  5655. if (vsi->netdev)
  5656. netif_napi_add(vsi->netdev, &q_vector->napi,
  5657. i40e_napi_poll, NAPI_POLL_WEIGHT);
  5658. q_vector->rx.latency_range = I40E_LOW_LATENCY;
  5659. q_vector->tx.latency_range = I40E_LOW_LATENCY;
  5660. /* tie q_vector and vsi together */
  5661. vsi->q_vectors[v_idx] = q_vector;
  5662. return 0;
  5663. }
  5664. /**
  5665. * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
  5666. * @vsi: the VSI being configured
  5667. *
  5668. * We allocate one q_vector per queue interrupt. If allocation fails we
  5669. * return -ENOMEM.
  5670. **/
  5671. static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
  5672. {
  5673. struct i40e_pf *pf = vsi->back;
  5674. int v_idx, num_q_vectors;
  5675. int err;
  5676. /* if not MSIX, give the one vector only to the LAN VSI */
  5677. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  5678. num_q_vectors = vsi->num_q_vectors;
  5679. else if (vsi == pf->vsi[pf->lan_vsi])
  5680. num_q_vectors = 1;
  5681. else
  5682. return -EINVAL;
  5683. for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
  5684. err = i40e_vsi_alloc_q_vector(vsi, v_idx);
  5685. if (err)
  5686. goto err_out;
  5687. }
  5688. return 0;
  5689. err_out:
  5690. while (v_idx--)
  5691. i40e_free_q_vector(vsi, v_idx);
  5692. return err;
  5693. }
  5694. /**
  5695. * i40e_init_interrupt_scheme - Determine proper interrupt scheme
  5696. * @pf: board private structure to initialize
  5697. **/
  5698. static void i40e_init_interrupt_scheme(struct i40e_pf *pf)
  5699. {
  5700. int err = 0;
  5701. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  5702. err = i40e_init_msix(pf);
  5703. if (err) {
  5704. pf->flags &= ~(I40E_FLAG_MSIX_ENABLED |
  5705. I40E_FLAG_RSS_ENABLED |
  5706. I40E_FLAG_DCB_CAPABLE |
  5707. I40E_FLAG_SRIOV_ENABLED |
  5708. I40E_FLAG_FD_SB_ENABLED |
  5709. I40E_FLAG_FD_ATR_ENABLED |
  5710. I40E_FLAG_VMDQ_ENABLED);
  5711. /* rework the queue expectations without MSIX */
  5712. i40e_determine_queue_usage(pf);
  5713. }
  5714. }
  5715. if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
  5716. (pf->flags & I40E_FLAG_MSI_ENABLED)) {
  5717. dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
  5718. err = pci_enable_msi(pf->pdev);
  5719. if (err) {
  5720. dev_info(&pf->pdev->dev, "MSI init failed - %d\n", err);
  5721. pf->flags &= ~I40E_FLAG_MSI_ENABLED;
  5722. }
  5723. }
  5724. if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
  5725. dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
  5726. /* track first vector for misc interrupts */
  5727. err = i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT-1);
  5728. }
  5729. /**
  5730. * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
  5731. * @pf: board private structure
  5732. *
  5733. * This sets up the handler for MSIX 0, which is used to manage the
  5734. * non-queue interrupts, e.g. AdminQ and errors. This is not used
  5735. * when in MSI or Legacy interrupt mode.
  5736. **/
  5737. static int i40e_setup_misc_vector(struct i40e_pf *pf)
  5738. {
  5739. struct i40e_hw *hw = &pf->hw;
  5740. int err = 0;
  5741. /* Only request the irq if this is the first time through, and
  5742. * not when we're rebuilding after a Reset
  5743. */
  5744. if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
  5745. err = request_irq(pf->msix_entries[0].vector,
  5746. i40e_intr, 0, pf->misc_int_name, pf);
  5747. if (err) {
  5748. dev_info(&pf->pdev->dev,
  5749. "request_irq for %s failed: %d\n",
  5750. pf->misc_int_name, err);
  5751. return -EFAULT;
  5752. }
  5753. }
  5754. i40e_enable_misc_int_causes(hw);
  5755. /* associate no queues to the misc vector */
  5756. wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
  5757. wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K);
  5758. i40e_flush(hw);
  5759. i40e_irq_dynamic_enable_icr0(pf);
  5760. return err;
  5761. }
  5762. /**
  5763. * i40e_config_rss - Prepare for RSS if used
  5764. * @pf: board private structure
  5765. **/
  5766. static int i40e_config_rss(struct i40e_pf *pf)
  5767. {
  5768. /* Set of random keys generated using kernel random number generator */
  5769. static const u32 seed[I40E_PFQF_HKEY_MAX_INDEX + 1] = {0x41b01687,
  5770. 0x183cfd8c, 0xce880440, 0x580cbc3c, 0x35897377,
  5771. 0x328b25e1, 0x4fa98922, 0xb7d90c14, 0xd5bad70d,
  5772. 0xcd15a2c1, 0xe8580225, 0x4a1e9d11, 0xfe5731be};
  5773. struct i40e_hw *hw = &pf->hw;
  5774. u32 lut = 0;
  5775. int i, j;
  5776. u64 hena;
  5777. u32 reg_val;
  5778. /* Fill out hash function seed */
  5779. for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
  5780. wr32(hw, I40E_PFQF_HKEY(i), seed[i]);
  5781. /* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
  5782. hena = (u64)rd32(hw, I40E_PFQF_HENA(0)) |
  5783. ((u64)rd32(hw, I40E_PFQF_HENA(1)) << 32);
  5784. hena |= I40E_DEFAULT_RSS_HENA;
  5785. wr32(hw, I40E_PFQF_HENA(0), (u32)hena);
  5786. wr32(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
  5787. /* Check capability and Set table size and register per hw expectation*/
  5788. reg_val = rd32(hw, I40E_PFQF_CTL_0);
  5789. if (hw->func_caps.rss_table_size == 512) {
  5790. reg_val |= I40E_PFQF_CTL_0_HASHLUTSIZE_512;
  5791. pf->rss_table_size = 512;
  5792. } else {
  5793. pf->rss_table_size = 128;
  5794. reg_val &= ~I40E_PFQF_CTL_0_HASHLUTSIZE_512;
  5795. }
  5796. wr32(hw, I40E_PFQF_CTL_0, reg_val);
  5797. /* Populate the LUT with max no. of queues in round robin fashion */
  5798. for (i = 0, j = 0; i < pf->rss_table_size; i++, j++) {
  5799. /* The assumption is that lan qp count will be the highest
  5800. * qp count for any PF VSI that needs RSS.
  5801. * If multiple VSIs need RSS support, all the qp counts
  5802. * for those VSIs should be a power of 2 for RSS to work.
  5803. * If LAN VSI is the only consumer for RSS then this requirement
  5804. * is not necessary.
  5805. */
  5806. if (j == pf->rss_size)
  5807. j = 0;
  5808. /* lut = 4-byte sliding window of 4 lut entries */
  5809. lut = (lut << 8) | (j &
  5810. ((0x1 << pf->hw.func_caps.rss_table_entry_width) - 1));
  5811. /* On i = 3, we have 4 entries in lut; write to the register */
  5812. if ((i & 3) == 3)
  5813. wr32(hw, I40E_PFQF_HLUT(i >> 2), lut);
  5814. }
  5815. i40e_flush(hw);
  5816. return 0;
  5817. }
  5818. /**
  5819. * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
  5820. * @pf: board private structure
  5821. * @queue_count: the requested queue count for rss.
  5822. *
  5823. * returns 0 if rss is not enabled, if enabled returns the final rss queue
  5824. * count which may be different from the requested queue count.
  5825. **/
  5826. int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
  5827. {
  5828. if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
  5829. return 0;
  5830. queue_count = min_t(int, queue_count, pf->rss_size_max);
  5831. if (queue_count != pf->rss_size) {
  5832. i40e_prep_for_reset(pf);
  5833. pf->rss_size = queue_count;
  5834. i40e_reset_and_rebuild(pf, true);
  5835. i40e_config_rss(pf);
  5836. }
  5837. dev_info(&pf->pdev->dev, "RSS count: %d\n", pf->rss_size);
  5838. return pf->rss_size;
  5839. }
  5840. /**
  5841. * i40e_sw_init - Initialize general software structures (struct i40e_pf)
  5842. * @pf: board private structure to initialize
  5843. *
  5844. * i40e_sw_init initializes the Adapter private data structure.
  5845. * Fields are initialized based on PCI device information and
  5846. * OS network device settings (MTU size).
  5847. **/
  5848. static int i40e_sw_init(struct i40e_pf *pf)
  5849. {
  5850. int err = 0;
  5851. int size;
  5852. pf->msg_enable = netif_msg_init(I40E_DEFAULT_MSG_ENABLE,
  5853. (NETIF_MSG_DRV|NETIF_MSG_PROBE|NETIF_MSG_LINK));
  5854. pf->hw.debug_mask = pf->msg_enable | I40E_DEBUG_DIAG;
  5855. if (debug != -1 && debug != I40E_DEFAULT_MSG_ENABLE) {
  5856. if (I40E_DEBUG_USER & debug)
  5857. pf->hw.debug_mask = debug;
  5858. pf->msg_enable = netif_msg_init((debug & ~I40E_DEBUG_USER),
  5859. I40E_DEFAULT_MSG_ENABLE);
  5860. }
  5861. /* Set default capability flags */
  5862. pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
  5863. I40E_FLAG_MSI_ENABLED |
  5864. I40E_FLAG_MSIX_ENABLED |
  5865. I40E_FLAG_RX_1BUF_ENABLED;
  5866. /* Set default ITR */
  5867. pf->rx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_RX_DEF;
  5868. pf->tx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_TX_DEF;
  5869. /* Depending on PF configurations, it is possible that the RSS
  5870. * maximum might end up larger than the available queues
  5871. */
  5872. pf->rss_size_max = 0x1 << pf->hw.func_caps.rss_table_entry_width;
  5873. pf->rss_size_max = min_t(int, pf->rss_size_max,
  5874. pf->hw.func_caps.num_tx_qp);
  5875. if (pf->hw.func_caps.rss) {
  5876. pf->flags |= I40E_FLAG_RSS_ENABLED;
  5877. pf->rss_size = min_t(int, pf->rss_size_max, num_online_cpus());
  5878. } else {
  5879. pf->rss_size = 1;
  5880. }
  5881. /* MFP mode enabled */
  5882. if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.mfp_mode_1) {
  5883. pf->flags |= I40E_FLAG_MFP_ENABLED;
  5884. dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
  5885. }
  5886. /* FW/NVM is not yet fixed in this regard */
  5887. if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
  5888. (pf->hw.func_caps.fd_filters_best_effort > 0)) {
  5889. pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
  5890. pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
  5891. /* Setup a counter for fd_atr per pf */
  5892. pf->fd_atr_cnt_idx = I40E_FD_ATR_STAT_IDX(pf->hw.pf_id);
  5893. if (!(pf->flags & I40E_FLAG_MFP_ENABLED)) {
  5894. pf->flags |= I40E_FLAG_FD_SB_ENABLED;
  5895. /* Setup a counter for fd_sb per pf */
  5896. pf->fd_sb_cnt_idx = I40E_FD_SB_STAT_IDX(pf->hw.pf_id);
  5897. } else {
  5898. dev_info(&pf->pdev->dev,
  5899. "Flow Director Sideband mode Disabled in MFP mode\n");
  5900. }
  5901. pf->fdir_pf_filter_count =
  5902. pf->hw.func_caps.fd_filters_guaranteed;
  5903. pf->hw.fdir_shared_filter_count =
  5904. pf->hw.func_caps.fd_filters_best_effort;
  5905. }
  5906. if (pf->hw.func_caps.vmdq) {
  5907. pf->flags |= I40E_FLAG_VMDQ_ENABLED;
  5908. pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
  5909. pf->num_vmdq_qps = I40E_DEFAULT_QUEUES_PER_VMDQ;
  5910. }
  5911. #ifdef CONFIG_PCI_IOV
  5912. if (pf->hw.func_caps.num_vfs) {
  5913. pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
  5914. pf->flags |= I40E_FLAG_SRIOV_ENABLED;
  5915. pf->num_req_vfs = min_t(int,
  5916. pf->hw.func_caps.num_vfs,
  5917. I40E_MAX_VF_COUNT);
  5918. }
  5919. #endif /* CONFIG_PCI_IOV */
  5920. pf->eeprom_version = 0xDEAD;
  5921. pf->lan_veb = I40E_NO_VEB;
  5922. pf->lan_vsi = I40E_NO_VSI;
  5923. /* set up queue assignment tracking */
  5924. size = sizeof(struct i40e_lump_tracking)
  5925. + (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
  5926. pf->qp_pile = kzalloc(size, GFP_KERNEL);
  5927. if (!pf->qp_pile) {
  5928. err = -ENOMEM;
  5929. goto sw_init_done;
  5930. }
  5931. pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
  5932. pf->qp_pile->search_hint = 0;
  5933. /* set up vector assignment tracking */
  5934. size = sizeof(struct i40e_lump_tracking)
  5935. + (sizeof(u16) * pf->hw.func_caps.num_msix_vectors);
  5936. pf->irq_pile = kzalloc(size, GFP_KERNEL);
  5937. if (!pf->irq_pile) {
  5938. kfree(pf->qp_pile);
  5939. err = -ENOMEM;
  5940. goto sw_init_done;
  5941. }
  5942. pf->irq_pile->num_entries = pf->hw.func_caps.num_msix_vectors;
  5943. pf->irq_pile->search_hint = 0;
  5944. mutex_init(&pf->switch_mutex);
  5945. sw_init_done:
  5946. return err;
  5947. }
  5948. /**
  5949. * i40e_set_ntuple - set the ntuple feature flag and take action
  5950. * @pf: board private structure to initialize
  5951. * @features: the feature set that the stack is suggesting
  5952. *
  5953. * returns a bool to indicate if reset needs to happen
  5954. **/
  5955. bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
  5956. {
  5957. bool need_reset = false;
  5958. /* Check if Flow Director n-tuple support was enabled or disabled. If
  5959. * the state changed, we need to reset.
  5960. */
  5961. if (features & NETIF_F_NTUPLE) {
  5962. /* Enable filters and mark for reset */
  5963. if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
  5964. need_reset = true;
  5965. pf->flags |= I40E_FLAG_FD_SB_ENABLED;
  5966. } else {
  5967. /* turn off filters, mark for reset and clear SW filter list */
  5968. if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
  5969. need_reset = true;
  5970. i40e_fdir_filter_exit(pf);
  5971. }
  5972. pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
  5973. /* if ATR was disabled it can be re-enabled. */
  5974. if (!(pf->flags & I40E_FLAG_FD_ATR_ENABLED))
  5975. pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
  5976. }
  5977. return need_reset;
  5978. }
  5979. /**
  5980. * i40e_set_features - set the netdev feature flags
  5981. * @netdev: ptr to the netdev being adjusted
  5982. * @features: the feature set that the stack is suggesting
  5983. **/
  5984. static int i40e_set_features(struct net_device *netdev,
  5985. netdev_features_t features)
  5986. {
  5987. struct i40e_netdev_priv *np = netdev_priv(netdev);
  5988. struct i40e_vsi *vsi = np->vsi;
  5989. struct i40e_pf *pf = vsi->back;
  5990. bool need_reset;
  5991. if (features & NETIF_F_HW_VLAN_CTAG_RX)
  5992. i40e_vlan_stripping_enable(vsi);
  5993. else
  5994. i40e_vlan_stripping_disable(vsi);
  5995. need_reset = i40e_set_ntuple(pf, features);
  5996. if (need_reset)
  5997. i40e_do_reset(pf, (1 << __I40E_PF_RESET_REQUESTED));
  5998. return 0;
  5999. }
  6000. #ifdef CONFIG_I40E_VXLAN
  6001. /**
  6002. * i40e_get_vxlan_port_idx - Lookup a possibly offloaded for Rx UDP port
  6003. * @pf: board private structure
  6004. * @port: The UDP port to look up
  6005. *
  6006. * Returns the index number or I40E_MAX_PF_UDP_OFFLOAD_PORTS if port not found
  6007. **/
  6008. static u8 i40e_get_vxlan_port_idx(struct i40e_pf *pf, __be16 port)
  6009. {
  6010. u8 i;
  6011. for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
  6012. if (pf->vxlan_ports[i] == port)
  6013. return i;
  6014. }
  6015. return i;
  6016. }
  6017. /**
  6018. * i40e_add_vxlan_port - Get notifications about VXLAN ports that come up
  6019. * @netdev: This physical port's netdev
  6020. * @sa_family: Socket Family that VXLAN is notifying us about
  6021. * @port: New UDP port number that VXLAN started listening to
  6022. **/
  6023. static void i40e_add_vxlan_port(struct net_device *netdev,
  6024. sa_family_t sa_family, __be16 port)
  6025. {
  6026. struct i40e_netdev_priv *np = netdev_priv(netdev);
  6027. struct i40e_vsi *vsi = np->vsi;
  6028. struct i40e_pf *pf = vsi->back;
  6029. u8 next_idx;
  6030. u8 idx;
  6031. if (sa_family == AF_INET6)
  6032. return;
  6033. idx = i40e_get_vxlan_port_idx(pf, port);
  6034. /* Check if port already exists */
  6035. if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
  6036. netdev_info(netdev, "Port %d already offloaded\n", ntohs(port));
  6037. return;
  6038. }
  6039. /* Now check if there is space to add the new port */
  6040. next_idx = i40e_get_vxlan_port_idx(pf, 0);
  6041. if (next_idx == I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
  6042. netdev_info(netdev, "Maximum number of UDP ports reached, not adding port %d\n",
  6043. ntohs(port));
  6044. return;
  6045. }
  6046. /* New port: add it and mark its index in the bitmap */
  6047. pf->vxlan_ports[next_idx] = port;
  6048. pf->pending_vxlan_bitmap |= (1 << next_idx);
  6049. pf->flags |= I40E_FLAG_VXLAN_FILTER_SYNC;
  6050. }
  6051. /**
  6052. * i40e_del_vxlan_port - Get notifications about VXLAN ports that go away
  6053. * @netdev: This physical port's netdev
  6054. * @sa_family: Socket Family that VXLAN is notifying us about
  6055. * @port: UDP port number that VXLAN stopped listening to
  6056. **/
  6057. static void i40e_del_vxlan_port(struct net_device *netdev,
  6058. sa_family_t sa_family, __be16 port)
  6059. {
  6060. struct i40e_netdev_priv *np = netdev_priv(netdev);
  6061. struct i40e_vsi *vsi = np->vsi;
  6062. struct i40e_pf *pf = vsi->back;
  6063. u8 idx;
  6064. if (sa_family == AF_INET6)
  6065. return;
  6066. idx = i40e_get_vxlan_port_idx(pf, port);
  6067. /* Check if port already exists */
  6068. if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
  6069. /* if port exists, set it to 0 (mark for deletion)
  6070. * and make it pending
  6071. */
  6072. pf->vxlan_ports[idx] = 0;
  6073. pf->pending_vxlan_bitmap |= (1 << idx);
  6074. pf->flags |= I40E_FLAG_VXLAN_FILTER_SYNC;
  6075. } else {
  6076. netdev_warn(netdev, "Port %d was not found, not deleting\n",
  6077. ntohs(port));
  6078. }
  6079. }
  6080. #endif
  6081. #ifdef HAVE_FDB_OPS
  6082. #ifdef USE_CONST_DEV_UC_CHAR
  6083. static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
  6084. struct net_device *dev,
  6085. const unsigned char *addr,
  6086. u16 flags)
  6087. #else
  6088. static int i40e_ndo_fdb_add(struct ndmsg *ndm,
  6089. struct net_device *dev,
  6090. unsigned char *addr,
  6091. u16 flags)
  6092. #endif
  6093. {
  6094. struct i40e_netdev_priv *np = netdev_priv(dev);
  6095. struct i40e_pf *pf = np->vsi->back;
  6096. int err = 0;
  6097. if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
  6098. return -EOPNOTSUPP;
  6099. /* Hardware does not support aging addresses so if a
  6100. * ndm_state is given only allow permanent addresses
  6101. */
  6102. if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
  6103. netdev_info(dev, "FDB only supports static addresses\n");
  6104. return -EINVAL;
  6105. }
  6106. if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
  6107. err = dev_uc_add_excl(dev, addr);
  6108. else if (is_multicast_ether_addr(addr))
  6109. err = dev_mc_add_excl(dev, addr);
  6110. else
  6111. err = -EINVAL;
  6112. /* Only return duplicate errors if NLM_F_EXCL is set */
  6113. if (err == -EEXIST && !(flags & NLM_F_EXCL))
  6114. err = 0;
  6115. return err;
  6116. }
  6117. #ifndef USE_DEFAULT_FDB_DEL_DUMP
  6118. #ifdef USE_CONST_DEV_UC_CHAR
  6119. static int i40e_ndo_fdb_del(struct ndmsg *ndm,
  6120. struct net_device *dev,
  6121. const unsigned char *addr)
  6122. #else
  6123. static int i40e_ndo_fdb_del(struct ndmsg *ndm,
  6124. struct net_device *dev,
  6125. unsigned char *addr)
  6126. #endif
  6127. {
  6128. struct i40e_netdev_priv *np = netdev_priv(dev);
  6129. struct i40e_pf *pf = np->vsi->back;
  6130. int err = -EOPNOTSUPP;
  6131. if (ndm->ndm_state & NUD_PERMANENT) {
  6132. netdev_info(dev, "FDB only supports static addresses\n");
  6133. return -EINVAL;
  6134. }
  6135. if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
  6136. if (is_unicast_ether_addr(addr))
  6137. err = dev_uc_del(dev, addr);
  6138. else if (is_multicast_ether_addr(addr))
  6139. err = dev_mc_del(dev, addr);
  6140. else
  6141. err = -EINVAL;
  6142. }
  6143. return err;
  6144. }
  6145. static int i40e_ndo_fdb_dump(struct sk_buff *skb,
  6146. struct netlink_callback *cb,
  6147. struct net_device *dev,
  6148. int idx)
  6149. {
  6150. struct i40e_netdev_priv *np = netdev_priv(dev);
  6151. struct i40e_pf *pf = np->vsi->back;
  6152. if (pf->flags & I40E_FLAG_SRIOV_ENABLED)
  6153. idx = ndo_dflt_fdb_dump(skb, cb, dev, idx);
  6154. return idx;
  6155. }
  6156. #endif /* USE_DEFAULT_FDB_DEL_DUMP */
  6157. #endif /* HAVE_FDB_OPS */
  6158. static const struct net_device_ops i40e_netdev_ops = {
  6159. .ndo_open = i40e_open,
  6160. .ndo_stop = i40e_close,
  6161. .ndo_start_xmit = i40e_lan_xmit_frame,
  6162. .ndo_get_stats64 = i40e_get_netdev_stats_struct,
  6163. .ndo_set_rx_mode = i40e_set_rx_mode,
  6164. .ndo_validate_addr = eth_validate_addr,
  6165. .ndo_set_mac_address = i40e_set_mac,
  6166. .ndo_change_mtu = i40e_change_mtu,
  6167. .ndo_do_ioctl = i40e_ioctl,
  6168. .ndo_tx_timeout = i40e_tx_timeout,
  6169. .ndo_vlan_rx_add_vid = i40e_vlan_rx_add_vid,
  6170. .ndo_vlan_rx_kill_vid = i40e_vlan_rx_kill_vid,
  6171. #ifdef CONFIG_NET_POLL_CONTROLLER
  6172. .ndo_poll_controller = i40e_netpoll,
  6173. #endif
  6174. .ndo_setup_tc = i40e_setup_tc,
  6175. .ndo_set_features = i40e_set_features,
  6176. .ndo_set_vf_mac = i40e_ndo_set_vf_mac,
  6177. .ndo_set_vf_vlan = i40e_ndo_set_vf_port_vlan,
  6178. .ndo_set_vf_rate = i40e_ndo_set_vf_bw,
  6179. .ndo_get_vf_config = i40e_ndo_get_vf_config,
  6180. .ndo_set_vf_link_state = i40e_ndo_set_vf_link_state,
  6181. .ndo_set_vf_spoofchk = i40e_ndo_set_vf_spoofck,
  6182. #ifdef CONFIG_I40E_VXLAN
  6183. .ndo_add_vxlan_port = i40e_add_vxlan_port,
  6184. .ndo_del_vxlan_port = i40e_del_vxlan_port,
  6185. #endif
  6186. #ifdef HAVE_FDB_OPS
  6187. .ndo_fdb_add = i40e_ndo_fdb_add,
  6188. #ifndef USE_DEFAULT_FDB_DEL_DUMP
  6189. .ndo_fdb_del = i40e_ndo_fdb_del,
  6190. .ndo_fdb_dump = i40e_ndo_fdb_dump,
  6191. #endif
  6192. #endif
  6193. };
  6194. /**
  6195. * i40e_config_netdev - Setup the netdev flags
  6196. * @vsi: the VSI being configured
  6197. *
  6198. * Returns 0 on success, negative value on failure
  6199. **/
  6200. static int i40e_config_netdev(struct i40e_vsi *vsi)
  6201. {
  6202. u8 brdcast[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
  6203. struct i40e_pf *pf = vsi->back;
  6204. struct i40e_hw *hw = &pf->hw;
  6205. struct i40e_netdev_priv *np;
  6206. struct net_device *netdev;
  6207. u8 mac_addr[ETH_ALEN];
  6208. int etherdev_size;
  6209. etherdev_size = sizeof(struct i40e_netdev_priv);
  6210. netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
  6211. if (!netdev)
  6212. return -ENOMEM;
  6213. vsi->netdev = netdev;
  6214. np = netdev_priv(netdev);
  6215. np->vsi = vsi;
  6216. netdev->hw_enc_features |= NETIF_F_IP_CSUM |
  6217. NETIF_F_GSO_UDP_TUNNEL |
  6218. NETIF_F_TSO;
  6219. netdev->features = NETIF_F_SG |
  6220. NETIF_F_IP_CSUM |
  6221. NETIF_F_SCTP_CSUM |
  6222. NETIF_F_HIGHDMA |
  6223. NETIF_F_GSO_UDP_TUNNEL |
  6224. NETIF_F_HW_VLAN_CTAG_TX |
  6225. NETIF_F_HW_VLAN_CTAG_RX |
  6226. NETIF_F_HW_VLAN_CTAG_FILTER |
  6227. NETIF_F_IPV6_CSUM |
  6228. NETIF_F_TSO |
  6229. NETIF_F_TSO_ECN |
  6230. NETIF_F_TSO6 |
  6231. NETIF_F_RXCSUM |
  6232. NETIF_F_RXHASH |
  6233. 0;
  6234. if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
  6235. netdev->features |= NETIF_F_NTUPLE;
  6236. /* copy netdev features into list of user selectable features */
  6237. netdev->hw_features |= netdev->features;
  6238. if (vsi->type == I40E_VSI_MAIN) {
  6239. SET_NETDEV_DEV(netdev, &pf->pdev->dev);
  6240. ether_addr_copy(mac_addr, hw->mac.perm_addr);
  6241. /* The following two steps are necessary to prevent reception
  6242. * of tagged packets - by default the NVM loads a MAC-VLAN
  6243. * filter that will accept any tagged packet. This is to
  6244. * prevent that during normal operations until a specific
  6245. * VLAN tag filter has been set.
  6246. */
  6247. i40e_rm_default_mac_filter(vsi, mac_addr);
  6248. i40e_add_filter(vsi, mac_addr, I40E_VLAN_ANY, false, true);
  6249. } else {
  6250. /* relate the VSI_VMDQ name to the VSI_MAIN name */
  6251. snprintf(netdev->name, IFNAMSIZ, "%sv%%d",
  6252. pf->vsi[pf->lan_vsi]->netdev->name);
  6253. random_ether_addr(mac_addr);
  6254. i40e_add_filter(vsi, mac_addr, I40E_VLAN_ANY, false, false);
  6255. }
  6256. i40e_add_filter(vsi, brdcast, I40E_VLAN_ANY, false, false);
  6257. ether_addr_copy(netdev->dev_addr, mac_addr);
  6258. ether_addr_copy(netdev->perm_addr, mac_addr);
  6259. /* vlan gets same features (except vlan offload)
  6260. * after any tweaks for specific VSI types
  6261. */
  6262. netdev->vlan_features = netdev->features & ~(NETIF_F_HW_VLAN_CTAG_TX |
  6263. NETIF_F_HW_VLAN_CTAG_RX |
  6264. NETIF_F_HW_VLAN_CTAG_FILTER);
  6265. netdev->priv_flags |= IFF_UNICAST_FLT;
  6266. netdev->priv_flags |= IFF_SUPP_NOFCS;
  6267. /* Setup netdev TC information */
  6268. i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
  6269. netdev->netdev_ops = &i40e_netdev_ops;
  6270. netdev->watchdog_timeo = 5 * HZ;
  6271. i40e_set_ethtool_ops(netdev);
  6272. return 0;
  6273. }
  6274. /**
  6275. * i40e_vsi_delete - Delete a VSI from the switch
  6276. * @vsi: the VSI being removed
  6277. *
  6278. * Returns 0 on success, negative value on failure
  6279. **/
  6280. static void i40e_vsi_delete(struct i40e_vsi *vsi)
  6281. {
  6282. /* remove default VSI is not allowed */
  6283. if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
  6284. return;
  6285. i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
  6286. }
  6287. /**
  6288. * i40e_add_vsi - Add a VSI to the switch
  6289. * @vsi: the VSI being configured
  6290. *
  6291. * This initializes a VSI context depending on the VSI type to be added and
  6292. * passes it down to the add_vsi aq command.
  6293. **/
  6294. static int i40e_add_vsi(struct i40e_vsi *vsi)
  6295. {
  6296. int ret = -ENODEV;
  6297. struct i40e_mac_filter *f, *ftmp;
  6298. struct i40e_pf *pf = vsi->back;
  6299. struct i40e_hw *hw = &pf->hw;
  6300. struct i40e_vsi_context ctxt;
  6301. u8 enabled_tc = 0x1; /* TC0 enabled */
  6302. int f_count = 0;
  6303. memset(&ctxt, 0, sizeof(ctxt));
  6304. switch (vsi->type) {
  6305. case I40E_VSI_MAIN:
  6306. /* The PF's main VSI is already setup as part of the
  6307. * device initialization, so we'll not bother with
  6308. * the add_vsi call, but we will retrieve the current
  6309. * VSI context.
  6310. */
  6311. ctxt.seid = pf->main_vsi_seid;
  6312. ctxt.pf_num = pf->hw.pf_id;
  6313. ctxt.vf_num = 0;
  6314. ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
  6315. ctxt.flags = I40E_AQ_VSI_TYPE_PF;
  6316. if (ret) {
  6317. dev_info(&pf->pdev->dev,
  6318. "couldn't get pf vsi config, err %d, aq_err %d\n",
  6319. ret, pf->hw.aq.asq_last_status);
  6320. return -ENOENT;
  6321. }
  6322. memcpy(&vsi->info, &ctxt.info, sizeof(ctxt.info));
  6323. vsi->info.valid_sections = 0;
  6324. vsi->seid = ctxt.seid;
  6325. vsi->id = ctxt.vsi_number;
  6326. enabled_tc = i40e_pf_get_tc_map(pf);
  6327. /* MFP mode setup queue map and update VSI */
  6328. if (pf->flags & I40E_FLAG_MFP_ENABLED) {
  6329. memset(&ctxt, 0, sizeof(ctxt));
  6330. ctxt.seid = pf->main_vsi_seid;
  6331. ctxt.pf_num = pf->hw.pf_id;
  6332. ctxt.vf_num = 0;
  6333. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
  6334. ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
  6335. if (ret) {
  6336. dev_info(&pf->pdev->dev,
  6337. "update vsi failed, aq_err=%d\n",
  6338. pf->hw.aq.asq_last_status);
  6339. ret = -ENOENT;
  6340. goto err;
  6341. }
  6342. /* update the local VSI info queue map */
  6343. i40e_vsi_update_queue_map(vsi, &ctxt);
  6344. vsi->info.valid_sections = 0;
  6345. } else {
  6346. /* Default/Main VSI is only enabled for TC0
  6347. * reconfigure it to enable all TCs that are
  6348. * available on the port in SFP mode.
  6349. */
  6350. ret = i40e_vsi_config_tc(vsi, enabled_tc);
  6351. if (ret) {
  6352. dev_info(&pf->pdev->dev,
  6353. "failed to configure TCs for main VSI tc_map 0x%08x, err %d, aq_err %d\n",
  6354. enabled_tc, ret,
  6355. pf->hw.aq.asq_last_status);
  6356. ret = -ENOENT;
  6357. }
  6358. }
  6359. break;
  6360. case I40E_VSI_FDIR:
  6361. ctxt.pf_num = hw->pf_id;
  6362. ctxt.vf_num = 0;
  6363. ctxt.uplink_seid = vsi->uplink_seid;
  6364. ctxt.connection_type = 0x1; /* regular data port */
  6365. ctxt.flags = I40E_AQ_VSI_TYPE_PF;
  6366. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
  6367. break;
  6368. case I40E_VSI_VMDQ2:
  6369. ctxt.pf_num = hw->pf_id;
  6370. ctxt.vf_num = 0;
  6371. ctxt.uplink_seid = vsi->uplink_seid;
  6372. ctxt.connection_type = 0x1; /* regular data port */
  6373. ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
  6374. ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  6375. /* This VSI is connected to VEB so the switch_id
  6376. * should be set to zero by default.
  6377. */
  6378. ctxt.info.switch_id = 0;
  6379. ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_LOCAL_LB);
  6380. ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  6381. /* Setup the VSI tx/rx queue map for TC0 only for now */
  6382. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
  6383. break;
  6384. case I40E_VSI_SRIOV:
  6385. ctxt.pf_num = hw->pf_id;
  6386. ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
  6387. ctxt.uplink_seid = vsi->uplink_seid;
  6388. ctxt.connection_type = 0x1; /* regular data port */
  6389. ctxt.flags = I40E_AQ_VSI_TYPE_VF;
  6390. ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  6391. /* This VSI is connected to VEB so the switch_id
  6392. * should be set to zero by default.
  6393. */
  6394. ctxt.info.switch_id = cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  6395. ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
  6396. ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
  6397. if (pf->vf[vsi->vf_id].spoofchk) {
  6398. ctxt.info.valid_sections |=
  6399. cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
  6400. ctxt.info.sec_flags |=
  6401. (I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
  6402. I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
  6403. }
  6404. /* Setup the VSI tx/rx queue map for TC0 only for now */
  6405. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
  6406. break;
  6407. default:
  6408. return -ENODEV;
  6409. }
  6410. if (vsi->type != I40E_VSI_MAIN) {
  6411. ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
  6412. if (ret) {
  6413. dev_info(&vsi->back->pdev->dev,
  6414. "add vsi failed, aq_err=%d\n",
  6415. vsi->back->hw.aq.asq_last_status);
  6416. ret = -ENOENT;
  6417. goto err;
  6418. }
  6419. memcpy(&vsi->info, &ctxt.info, sizeof(ctxt.info));
  6420. vsi->info.valid_sections = 0;
  6421. vsi->seid = ctxt.seid;
  6422. vsi->id = ctxt.vsi_number;
  6423. }
  6424. /* If macvlan filters already exist, force them to get loaded */
  6425. list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
  6426. f->changed = true;
  6427. f_count++;
  6428. }
  6429. if (f_count) {
  6430. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  6431. pf->flags |= I40E_FLAG_FILTER_SYNC;
  6432. }
  6433. /* Update VSI BW information */
  6434. ret = i40e_vsi_get_bw_info(vsi);
  6435. if (ret) {
  6436. dev_info(&pf->pdev->dev,
  6437. "couldn't get vsi bw info, err %d, aq_err %d\n",
  6438. ret, pf->hw.aq.asq_last_status);
  6439. /* VSI is already added so not tearing that up */
  6440. ret = 0;
  6441. }
  6442. err:
  6443. return ret;
  6444. }
  6445. /**
  6446. * i40e_vsi_release - Delete a VSI and free its resources
  6447. * @vsi: the VSI being removed
  6448. *
  6449. * Returns 0 on success or < 0 on error
  6450. **/
  6451. int i40e_vsi_release(struct i40e_vsi *vsi)
  6452. {
  6453. struct i40e_mac_filter *f, *ftmp;
  6454. struct i40e_veb *veb = NULL;
  6455. struct i40e_pf *pf;
  6456. u16 uplink_seid;
  6457. int i, n;
  6458. pf = vsi->back;
  6459. /* release of a VEB-owner or last VSI is not allowed */
  6460. if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
  6461. dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
  6462. vsi->seid, vsi->uplink_seid);
  6463. return -ENODEV;
  6464. }
  6465. if (vsi == pf->vsi[pf->lan_vsi] &&
  6466. !test_bit(__I40E_DOWN, &pf->state)) {
  6467. dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
  6468. return -ENODEV;
  6469. }
  6470. uplink_seid = vsi->uplink_seid;
  6471. if (vsi->type != I40E_VSI_SRIOV) {
  6472. if (vsi->netdev_registered) {
  6473. vsi->netdev_registered = false;
  6474. if (vsi->netdev) {
  6475. /* results in a call to i40e_close() */
  6476. unregister_netdev(vsi->netdev);
  6477. }
  6478. } else {
  6479. i40e_vsi_close(vsi);
  6480. }
  6481. i40e_vsi_disable_irq(vsi);
  6482. }
  6483. list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list)
  6484. i40e_del_filter(vsi, f->macaddr, f->vlan,
  6485. f->is_vf, f->is_netdev);
  6486. i40e_sync_vsi_filters(vsi);
  6487. i40e_vsi_delete(vsi);
  6488. i40e_vsi_free_q_vectors(vsi);
  6489. if (vsi->netdev) {
  6490. free_netdev(vsi->netdev);
  6491. vsi->netdev = NULL;
  6492. }
  6493. i40e_vsi_clear_rings(vsi);
  6494. i40e_vsi_clear(vsi);
  6495. /* If this was the last thing on the VEB, except for the
  6496. * controlling VSI, remove the VEB, which puts the controlling
  6497. * VSI onto the next level down in the switch.
  6498. *
  6499. * Well, okay, there's one more exception here: don't remove
  6500. * the orphan VEBs yet. We'll wait for an explicit remove request
  6501. * from up the network stack.
  6502. */
  6503. for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
  6504. if (pf->vsi[i] &&
  6505. pf->vsi[i]->uplink_seid == uplink_seid &&
  6506. (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
  6507. n++; /* count the VSIs */
  6508. }
  6509. }
  6510. for (i = 0; i < I40E_MAX_VEB; i++) {
  6511. if (!pf->veb[i])
  6512. continue;
  6513. if (pf->veb[i]->uplink_seid == uplink_seid)
  6514. n++; /* count the VEBs */
  6515. if (pf->veb[i]->seid == uplink_seid)
  6516. veb = pf->veb[i];
  6517. }
  6518. if (n == 0 && veb && veb->uplink_seid != 0)
  6519. i40e_veb_release(veb);
  6520. return 0;
  6521. }
  6522. /**
  6523. * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
  6524. * @vsi: ptr to the VSI
  6525. *
  6526. * This should only be called after i40e_vsi_mem_alloc() which allocates the
  6527. * corresponding SW VSI structure and initializes num_queue_pairs for the
  6528. * newly allocated VSI.
  6529. *
  6530. * Returns 0 on success or negative on failure
  6531. **/
  6532. static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
  6533. {
  6534. int ret = -ENOENT;
  6535. struct i40e_pf *pf = vsi->back;
  6536. if (vsi->q_vectors[0]) {
  6537. dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
  6538. vsi->seid);
  6539. return -EEXIST;
  6540. }
  6541. if (vsi->base_vector) {
  6542. dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
  6543. vsi->seid, vsi->base_vector);
  6544. return -EEXIST;
  6545. }
  6546. ret = i40e_vsi_alloc_q_vectors(vsi);
  6547. if (ret) {
  6548. dev_info(&pf->pdev->dev,
  6549. "failed to allocate %d q_vector for VSI %d, ret=%d\n",
  6550. vsi->num_q_vectors, vsi->seid, ret);
  6551. vsi->num_q_vectors = 0;
  6552. goto vector_setup_out;
  6553. }
  6554. if (vsi->num_q_vectors)
  6555. vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
  6556. vsi->num_q_vectors, vsi->idx);
  6557. if (vsi->base_vector < 0) {
  6558. dev_info(&pf->pdev->dev,
  6559. "failed to get queue tracking for VSI %d, err=%d\n",
  6560. vsi->seid, vsi->base_vector);
  6561. i40e_vsi_free_q_vectors(vsi);
  6562. ret = -ENOENT;
  6563. goto vector_setup_out;
  6564. }
  6565. vector_setup_out:
  6566. return ret;
  6567. }
  6568. /**
  6569. * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
  6570. * @vsi: pointer to the vsi.
  6571. *
  6572. * This re-allocates a vsi's queue resources.
  6573. *
  6574. * Returns pointer to the successfully allocated and configured VSI sw struct
  6575. * on success, otherwise returns NULL on failure.
  6576. **/
  6577. static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
  6578. {
  6579. struct i40e_pf *pf = vsi->back;
  6580. u8 enabled_tc;
  6581. int ret;
  6582. i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
  6583. i40e_vsi_clear_rings(vsi);
  6584. i40e_vsi_free_arrays(vsi, false);
  6585. i40e_set_num_rings_in_vsi(vsi);
  6586. ret = i40e_vsi_alloc_arrays(vsi, false);
  6587. if (ret)
  6588. goto err_vsi;
  6589. ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs, vsi->idx);
  6590. if (ret < 0) {
  6591. dev_info(&pf->pdev->dev, "VSI %d get_lump failed %d\n",
  6592. vsi->seid, ret);
  6593. goto err_vsi;
  6594. }
  6595. vsi->base_queue = ret;
  6596. /* Update the FW view of the VSI. Force a reset of TC and queue
  6597. * layout configurations.
  6598. */
  6599. enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
  6600. pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
  6601. pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
  6602. i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
  6603. /* assign it some queues */
  6604. ret = i40e_alloc_rings(vsi);
  6605. if (ret)
  6606. goto err_rings;
  6607. /* map all of the rings to the q_vectors */
  6608. i40e_vsi_map_rings_to_vectors(vsi);
  6609. return vsi;
  6610. err_rings:
  6611. i40e_vsi_free_q_vectors(vsi);
  6612. if (vsi->netdev_registered) {
  6613. vsi->netdev_registered = false;
  6614. unregister_netdev(vsi->netdev);
  6615. free_netdev(vsi->netdev);
  6616. vsi->netdev = NULL;
  6617. }
  6618. i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
  6619. err_vsi:
  6620. i40e_vsi_clear(vsi);
  6621. return NULL;
  6622. }
  6623. /**
  6624. * i40e_vsi_setup - Set up a VSI by a given type
  6625. * @pf: board private structure
  6626. * @type: VSI type
  6627. * @uplink_seid: the switch element to link to
  6628. * @param1: usage depends upon VSI type. For VF types, indicates VF id
  6629. *
  6630. * This allocates the sw VSI structure and its queue resources, then add a VSI
  6631. * to the identified VEB.
  6632. *
  6633. * Returns pointer to the successfully allocated and configure VSI sw struct on
  6634. * success, otherwise returns NULL on failure.
  6635. **/
  6636. struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
  6637. u16 uplink_seid, u32 param1)
  6638. {
  6639. struct i40e_vsi *vsi = NULL;
  6640. struct i40e_veb *veb = NULL;
  6641. int ret, i;
  6642. int v_idx;
  6643. /* The requested uplink_seid must be either
  6644. * - the PF's port seid
  6645. * no VEB is needed because this is the PF
  6646. * or this is a Flow Director special case VSI
  6647. * - seid of an existing VEB
  6648. * - seid of a VSI that owns an existing VEB
  6649. * - seid of a VSI that doesn't own a VEB
  6650. * a new VEB is created and the VSI becomes the owner
  6651. * - seid of the PF VSI, which is what creates the first VEB
  6652. * this is a special case of the previous
  6653. *
  6654. * Find which uplink_seid we were given and create a new VEB if needed
  6655. */
  6656. for (i = 0; i < I40E_MAX_VEB; i++) {
  6657. if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
  6658. veb = pf->veb[i];
  6659. break;
  6660. }
  6661. }
  6662. if (!veb && uplink_seid != pf->mac_seid) {
  6663. for (i = 0; i < pf->num_alloc_vsi; i++) {
  6664. if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
  6665. vsi = pf->vsi[i];
  6666. break;
  6667. }
  6668. }
  6669. if (!vsi) {
  6670. dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
  6671. uplink_seid);
  6672. return NULL;
  6673. }
  6674. if (vsi->uplink_seid == pf->mac_seid)
  6675. veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
  6676. vsi->tc_config.enabled_tc);
  6677. else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
  6678. veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
  6679. vsi->tc_config.enabled_tc);
  6680. for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
  6681. if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
  6682. veb = pf->veb[i];
  6683. }
  6684. if (!veb) {
  6685. dev_info(&pf->pdev->dev, "couldn't add VEB\n");
  6686. return NULL;
  6687. }
  6688. vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
  6689. uplink_seid = veb->seid;
  6690. }
  6691. /* get vsi sw struct */
  6692. v_idx = i40e_vsi_mem_alloc(pf, type);
  6693. if (v_idx < 0)
  6694. goto err_alloc;
  6695. vsi = pf->vsi[v_idx];
  6696. if (!vsi)
  6697. goto err_alloc;
  6698. vsi->type = type;
  6699. vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
  6700. if (type == I40E_VSI_MAIN)
  6701. pf->lan_vsi = v_idx;
  6702. else if (type == I40E_VSI_SRIOV)
  6703. vsi->vf_id = param1;
  6704. /* assign it some queues */
  6705. ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs,
  6706. vsi->idx);
  6707. if (ret < 0) {
  6708. dev_info(&pf->pdev->dev, "VSI %d get_lump failed %d\n",
  6709. vsi->seid, ret);
  6710. goto err_vsi;
  6711. }
  6712. vsi->base_queue = ret;
  6713. /* get a VSI from the hardware */
  6714. vsi->uplink_seid = uplink_seid;
  6715. ret = i40e_add_vsi(vsi);
  6716. if (ret)
  6717. goto err_vsi;
  6718. switch (vsi->type) {
  6719. /* setup the netdev if needed */
  6720. case I40E_VSI_MAIN:
  6721. case I40E_VSI_VMDQ2:
  6722. ret = i40e_config_netdev(vsi);
  6723. if (ret)
  6724. goto err_netdev;
  6725. ret = register_netdev(vsi->netdev);
  6726. if (ret)
  6727. goto err_netdev;
  6728. vsi->netdev_registered = true;
  6729. netif_carrier_off(vsi->netdev);
  6730. #ifdef CONFIG_I40E_DCB
  6731. /* Setup DCB netlink interface */
  6732. i40e_dcbnl_setup(vsi);
  6733. #endif /* CONFIG_I40E_DCB */
  6734. /* fall through */
  6735. case I40E_VSI_FDIR:
  6736. /* set up vectors and rings if needed */
  6737. ret = i40e_vsi_setup_vectors(vsi);
  6738. if (ret)
  6739. goto err_msix;
  6740. ret = i40e_alloc_rings(vsi);
  6741. if (ret)
  6742. goto err_rings;
  6743. /* map all of the rings to the q_vectors */
  6744. i40e_vsi_map_rings_to_vectors(vsi);
  6745. i40e_vsi_reset_stats(vsi);
  6746. break;
  6747. default:
  6748. /* no netdev or rings for the other VSI types */
  6749. break;
  6750. }
  6751. return vsi;
  6752. err_rings:
  6753. i40e_vsi_free_q_vectors(vsi);
  6754. err_msix:
  6755. if (vsi->netdev_registered) {
  6756. vsi->netdev_registered = false;
  6757. unregister_netdev(vsi->netdev);
  6758. free_netdev(vsi->netdev);
  6759. vsi->netdev = NULL;
  6760. }
  6761. err_netdev:
  6762. i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
  6763. err_vsi:
  6764. i40e_vsi_clear(vsi);
  6765. err_alloc:
  6766. return NULL;
  6767. }
  6768. /**
  6769. * i40e_veb_get_bw_info - Query VEB BW information
  6770. * @veb: the veb to query
  6771. *
  6772. * Query the Tx scheduler BW configuration data for given VEB
  6773. **/
  6774. static int i40e_veb_get_bw_info(struct i40e_veb *veb)
  6775. {
  6776. struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
  6777. struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
  6778. struct i40e_pf *pf = veb->pf;
  6779. struct i40e_hw *hw = &pf->hw;
  6780. u32 tc_bw_max;
  6781. int ret = 0;
  6782. int i;
  6783. ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
  6784. &bw_data, NULL);
  6785. if (ret) {
  6786. dev_info(&pf->pdev->dev,
  6787. "query veb bw config failed, aq_err=%d\n",
  6788. hw->aq.asq_last_status);
  6789. goto out;
  6790. }
  6791. ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
  6792. &ets_data, NULL);
  6793. if (ret) {
  6794. dev_info(&pf->pdev->dev,
  6795. "query veb bw ets config failed, aq_err=%d\n",
  6796. hw->aq.asq_last_status);
  6797. goto out;
  6798. }
  6799. veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
  6800. veb->bw_max_quanta = ets_data.tc_bw_max;
  6801. veb->is_abs_credits = bw_data.absolute_credits_enable;
  6802. tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
  6803. (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
  6804. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  6805. veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
  6806. veb->bw_tc_limit_credits[i] =
  6807. le16_to_cpu(bw_data.tc_bw_limits[i]);
  6808. veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
  6809. }
  6810. out:
  6811. return ret;
  6812. }
  6813. /**
  6814. * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
  6815. * @pf: board private structure
  6816. *
  6817. * On error: returns error code (negative)
  6818. * On success: returns vsi index in PF (positive)
  6819. **/
  6820. static int i40e_veb_mem_alloc(struct i40e_pf *pf)
  6821. {
  6822. int ret = -ENOENT;
  6823. struct i40e_veb *veb;
  6824. int i;
  6825. /* Need to protect the allocation of switch elements at the PF level */
  6826. mutex_lock(&pf->switch_mutex);
  6827. /* VEB list may be fragmented if VEB creation/destruction has
  6828. * been happening. We can afford to do a quick scan to look
  6829. * for any free slots in the list.
  6830. *
  6831. * find next empty veb slot, looping back around if necessary
  6832. */
  6833. i = 0;
  6834. while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
  6835. i++;
  6836. if (i >= I40E_MAX_VEB) {
  6837. ret = -ENOMEM;
  6838. goto err_alloc_veb; /* out of VEB slots! */
  6839. }
  6840. veb = kzalloc(sizeof(*veb), GFP_KERNEL);
  6841. if (!veb) {
  6842. ret = -ENOMEM;
  6843. goto err_alloc_veb;
  6844. }
  6845. veb->pf = pf;
  6846. veb->idx = i;
  6847. veb->enabled_tc = 1;
  6848. pf->veb[i] = veb;
  6849. ret = i;
  6850. err_alloc_veb:
  6851. mutex_unlock(&pf->switch_mutex);
  6852. return ret;
  6853. }
  6854. /**
  6855. * i40e_switch_branch_release - Delete a branch of the switch tree
  6856. * @branch: where to start deleting
  6857. *
  6858. * This uses recursion to find the tips of the branch to be
  6859. * removed, deleting until we get back to and can delete this VEB.
  6860. **/
  6861. static void i40e_switch_branch_release(struct i40e_veb *branch)
  6862. {
  6863. struct i40e_pf *pf = branch->pf;
  6864. u16 branch_seid = branch->seid;
  6865. u16 veb_idx = branch->idx;
  6866. int i;
  6867. /* release any VEBs on this VEB - RECURSION */
  6868. for (i = 0; i < I40E_MAX_VEB; i++) {
  6869. if (!pf->veb[i])
  6870. continue;
  6871. if (pf->veb[i]->uplink_seid == branch->seid)
  6872. i40e_switch_branch_release(pf->veb[i]);
  6873. }
  6874. /* Release the VSIs on this VEB, but not the owner VSI.
  6875. *
  6876. * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
  6877. * the VEB itself, so don't use (*branch) after this loop.
  6878. */
  6879. for (i = 0; i < pf->num_alloc_vsi; i++) {
  6880. if (!pf->vsi[i])
  6881. continue;
  6882. if (pf->vsi[i]->uplink_seid == branch_seid &&
  6883. (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
  6884. i40e_vsi_release(pf->vsi[i]);
  6885. }
  6886. }
  6887. /* There's one corner case where the VEB might not have been
  6888. * removed, so double check it here and remove it if needed.
  6889. * This case happens if the veb was created from the debugfs
  6890. * commands and no VSIs were added to it.
  6891. */
  6892. if (pf->veb[veb_idx])
  6893. i40e_veb_release(pf->veb[veb_idx]);
  6894. }
  6895. /**
  6896. * i40e_veb_clear - remove veb struct
  6897. * @veb: the veb to remove
  6898. **/
  6899. static void i40e_veb_clear(struct i40e_veb *veb)
  6900. {
  6901. if (!veb)
  6902. return;
  6903. if (veb->pf) {
  6904. struct i40e_pf *pf = veb->pf;
  6905. mutex_lock(&pf->switch_mutex);
  6906. if (pf->veb[veb->idx] == veb)
  6907. pf->veb[veb->idx] = NULL;
  6908. mutex_unlock(&pf->switch_mutex);
  6909. }
  6910. kfree(veb);
  6911. }
  6912. /**
  6913. * i40e_veb_release - Delete a VEB and free its resources
  6914. * @veb: the VEB being removed
  6915. **/
  6916. void i40e_veb_release(struct i40e_veb *veb)
  6917. {
  6918. struct i40e_vsi *vsi = NULL;
  6919. struct i40e_pf *pf;
  6920. int i, n = 0;
  6921. pf = veb->pf;
  6922. /* find the remaining VSI and check for extras */
  6923. for (i = 0; i < pf->num_alloc_vsi; i++) {
  6924. if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
  6925. n++;
  6926. vsi = pf->vsi[i];
  6927. }
  6928. }
  6929. if (n != 1) {
  6930. dev_info(&pf->pdev->dev,
  6931. "can't remove VEB %d with %d VSIs left\n",
  6932. veb->seid, n);
  6933. return;
  6934. }
  6935. /* move the remaining VSI to uplink veb */
  6936. vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
  6937. if (veb->uplink_seid) {
  6938. vsi->uplink_seid = veb->uplink_seid;
  6939. if (veb->uplink_seid == pf->mac_seid)
  6940. vsi->veb_idx = I40E_NO_VEB;
  6941. else
  6942. vsi->veb_idx = veb->veb_idx;
  6943. } else {
  6944. /* floating VEB */
  6945. vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
  6946. vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
  6947. }
  6948. i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
  6949. i40e_veb_clear(veb);
  6950. }
  6951. /**
  6952. * i40e_add_veb - create the VEB in the switch
  6953. * @veb: the VEB to be instantiated
  6954. * @vsi: the controlling VSI
  6955. **/
  6956. static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
  6957. {
  6958. bool is_default = false;
  6959. bool is_cloud = false;
  6960. int ret;
  6961. /* get a VEB from the hardware */
  6962. ret = i40e_aq_add_veb(&veb->pf->hw, veb->uplink_seid, vsi->seid,
  6963. veb->enabled_tc, is_default,
  6964. is_cloud, &veb->seid, NULL);
  6965. if (ret) {
  6966. dev_info(&veb->pf->pdev->dev,
  6967. "couldn't add VEB, err %d, aq_err %d\n",
  6968. ret, veb->pf->hw.aq.asq_last_status);
  6969. return -EPERM;
  6970. }
  6971. /* get statistics counter */
  6972. ret = i40e_aq_get_veb_parameters(&veb->pf->hw, veb->seid, NULL, NULL,
  6973. &veb->stats_idx, NULL, NULL, NULL);
  6974. if (ret) {
  6975. dev_info(&veb->pf->pdev->dev,
  6976. "couldn't get VEB statistics idx, err %d, aq_err %d\n",
  6977. ret, veb->pf->hw.aq.asq_last_status);
  6978. return -EPERM;
  6979. }
  6980. ret = i40e_veb_get_bw_info(veb);
  6981. if (ret) {
  6982. dev_info(&veb->pf->pdev->dev,
  6983. "couldn't get VEB bw info, err %d, aq_err %d\n",
  6984. ret, veb->pf->hw.aq.asq_last_status);
  6985. i40e_aq_delete_element(&veb->pf->hw, veb->seid, NULL);
  6986. return -ENOENT;
  6987. }
  6988. vsi->uplink_seid = veb->seid;
  6989. vsi->veb_idx = veb->idx;
  6990. vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
  6991. return 0;
  6992. }
  6993. /**
  6994. * i40e_veb_setup - Set up a VEB
  6995. * @pf: board private structure
  6996. * @flags: VEB setup flags
  6997. * @uplink_seid: the switch element to link to
  6998. * @vsi_seid: the initial VSI seid
  6999. * @enabled_tc: Enabled TC bit-map
  7000. *
  7001. * This allocates the sw VEB structure and links it into the switch
  7002. * It is possible and legal for this to be a duplicate of an already
  7003. * existing VEB. It is also possible for both uplink and vsi seids
  7004. * to be zero, in order to create a floating VEB.
  7005. *
  7006. * Returns pointer to the successfully allocated VEB sw struct on
  7007. * success, otherwise returns NULL on failure.
  7008. **/
  7009. struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
  7010. u16 uplink_seid, u16 vsi_seid,
  7011. u8 enabled_tc)
  7012. {
  7013. struct i40e_veb *veb, *uplink_veb = NULL;
  7014. int vsi_idx, veb_idx;
  7015. int ret;
  7016. /* if one seid is 0, the other must be 0 to create a floating relay */
  7017. if ((uplink_seid == 0 || vsi_seid == 0) &&
  7018. (uplink_seid + vsi_seid != 0)) {
  7019. dev_info(&pf->pdev->dev,
  7020. "one, not both seid's are 0: uplink=%d vsi=%d\n",
  7021. uplink_seid, vsi_seid);
  7022. return NULL;
  7023. }
  7024. /* make sure there is such a vsi and uplink */
  7025. for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
  7026. if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
  7027. break;
  7028. if (vsi_idx >= pf->num_alloc_vsi && vsi_seid != 0) {
  7029. dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
  7030. vsi_seid);
  7031. return NULL;
  7032. }
  7033. if (uplink_seid && uplink_seid != pf->mac_seid) {
  7034. for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
  7035. if (pf->veb[veb_idx] &&
  7036. pf->veb[veb_idx]->seid == uplink_seid) {
  7037. uplink_veb = pf->veb[veb_idx];
  7038. break;
  7039. }
  7040. }
  7041. if (!uplink_veb) {
  7042. dev_info(&pf->pdev->dev,
  7043. "uplink seid %d not found\n", uplink_seid);
  7044. return NULL;
  7045. }
  7046. }
  7047. /* get veb sw struct */
  7048. veb_idx = i40e_veb_mem_alloc(pf);
  7049. if (veb_idx < 0)
  7050. goto err_alloc;
  7051. veb = pf->veb[veb_idx];
  7052. veb->flags = flags;
  7053. veb->uplink_seid = uplink_seid;
  7054. veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
  7055. veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
  7056. /* create the VEB in the switch */
  7057. ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
  7058. if (ret)
  7059. goto err_veb;
  7060. if (vsi_idx == pf->lan_vsi)
  7061. pf->lan_veb = veb->idx;
  7062. return veb;
  7063. err_veb:
  7064. i40e_veb_clear(veb);
  7065. err_alloc:
  7066. return NULL;
  7067. }
  7068. /**
  7069. * i40e_setup_pf_switch_element - set pf vars based on switch type
  7070. * @pf: board private structure
  7071. * @ele: element we are building info from
  7072. * @num_reported: total number of elements
  7073. * @printconfig: should we print the contents
  7074. *
  7075. * helper function to assist in extracting a few useful SEID values.
  7076. **/
  7077. static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
  7078. struct i40e_aqc_switch_config_element_resp *ele,
  7079. u16 num_reported, bool printconfig)
  7080. {
  7081. u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
  7082. u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
  7083. u8 element_type = ele->element_type;
  7084. u16 seid = le16_to_cpu(ele->seid);
  7085. if (printconfig)
  7086. dev_info(&pf->pdev->dev,
  7087. "type=%d seid=%d uplink=%d downlink=%d\n",
  7088. element_type, seid, uplink_seid, downlink_seid);
  7089. switch (element_type) {
  7090. case I40E_SWITCH_ELEMENT_TYPE_MAC:
  7091. pf->mac_seid = seid;
  7092. break;
  7093. case I40E_SWITCH_ELEMENT_TYPE_VEB:
  7094. /* Main VEB? */
  7095. if (uplink_seid != pf->mac_seid)
  7096. break;
  7097. if (pf->lan_veb == I40E_NO_VEB) {
  7098. int v;
  7099. /* find existing or else empty VEB */
  7100. for (v = 0; v < I40E_MAX_VEB; v++) {
  7101. if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
  7102. pf->lan_veb = v;
  7103. break;
  7104. }
  7105. }
  7106. if (pf->lan_veb == I40E_NO_VEB) {
  7107. v = i40e_veb_mem_alloc(pf);
  7108. if (v < 0)
  7109. break;
  7110. pf->lan_veb = v;
  7111. }
  7112. }
  7113. pf->veb[pf->lan_veb]->seid = seid;
  7114. pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
  7115. pf->veb[pf->lan_veb]->pf = pf;
  7116. pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
  7117. break;
  7118. case I40E_SWITCH_ELEMENT_TYPE_VSI:
  7119. if (num_reported != 1)
  7120. break;
  7121. /* This is immediately after a reset so we can assume this is
  7122. * the PF's VSI
  7123. */
  7124. pf->mac_seid = uplink_seid;
  7125. pf->pf_seid = downlink_seid;
  7126. pf->main_vsi_seid = seid;
  7127. if (printconfig)
  7128. dev_info(&pf->pdev->dev,
  7129. "pf_seid=%d main_vsi_seid=%d\n",
  7130. pf->pf_seid, pf->main_vsi_seid);
  7131. break;
  7132. case I40E_SWITCH_ELEMENT_TYPE_PF:
  7133. case I40E_SWITCH_ELEMENT_TYPE_VF:
  7134. case I40E_SWITCH_ELEMENT_TYPE_EMP:
  7135. case I40E_SWITCH_ELEMENT_TYPE_BMC:
  7136. case I40E_SWITCH_ELEMENT_TYPE_PE:
  7137. case I40E_SWITCH_ELEMENT_TYPE_PA:
  7138. /* ignore these for now */
  7139. break;
  7140. default:
  7141. dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
  7142. element_type, seid);
  7143. break;
  7144. }
  7145. }
  7146. /**
  7147. * i40e_fetch_switch_configuration - Get switch config from firmware
  7148. * @pf: board private structure
  7149. * @printconfig: should we print the contents
  7150. *
  7151. * Get the current switch configuration from the device and
  7152. * extract a few useful SEID values.
  7153. **/
  7154. int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
  7155. {
  7156. struct i40e_aqc_get_switch_config_resp *sw_config;
  7157. u16 next_seid = 0;
  7158. int ret = 0;
  7159. u8 *aq_buf;
  7160. int i;
  7161. aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
  7162. if (!aq_buf)
  7163. return -ENOMEM;
  7164. sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
  7165. do {
  7166. u16 num_reported, num_total;
  7167. ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
  7168. I40E_AQ_LARGE_BUF,
  7169. &next_seid, NULL);
  7170. if (ret) {
  7171. dev_info(&pf->pdev->dev,
  7172. "get switch config failed %d aq_err=%x\n",
  7173. ret, pf->hw.aq.asq_last_status);
  7174. kfree(aq_buf);
  7175. return -ENOENT;
  7176. }
  7177. num_reported = le16_to_cpu(sw_config->header.num_reported);
  7178. num_total = le16_to_cpu(sw_config->header.num_total);
  7179. if (printconfig)
  7180. dev_info(&pf->pdev->dev,
  7181. "header: %d reported %d total\n",
  7182. num_reported, num_total);
  7183. for (i = 0; i < num_reported; i++) {
  7184. struct i40e_aqc_switch_config_element_resp *ele =
  7185. &sw_config->element[i];
  7186. i40e_setup_pf_switch_element(pf, ele, num_reported,
  7187. printconfig);
  7188. }
  7189. } while (next_seid != 0);
  7190. kfree(aq_buf);
  7191. return ret;
  7192. }
  7193. /**
  7194. * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
  7195. * @pf: board private structure
  7196. * @reinit: if the Main VSI needs to re-initialized.
  7197. *
  7198. * Returns 0 on success, negative value on failure
  7199. **/
  7200. static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit)
  7201. {
  7202. u32 rxfc = 0, txfc = 0, rxfc_reg;
  7203. int ret;
  7204. /* find out what's out there already */
  7205. ret = i40e_fetch_switch_configuration(pf, false);
  7206. if (ret) {
  7207. dev_info(&pf->pdev->dev,
  7208. "couldn't fetch switch config, err %d, aq_err %d\n",
  7209. ret, pf->hw.aq.asq_last_status);
  7210. return ret;
  7211. }
  7212. i40e_pf_reset_stats(pf);
  7213. /* first time setup */
  7214. if (pf->lan_vsi == I40E_NO_VSI || reinit) {
  7215. struct i40e_vsi *vsi = NULL;
  7216. u16 uplink_seid;
  7217. /* Set up the PF VSI associated with the PF's main VSI
  7218. * that is already in the HW switch
  7219. */
  7220. if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
  7221. uplink_seid = pf->veb[pf->lan_veb]->seid;
  7222. else
  7223. uplink_seid = pf->mac_seid;
  7224. if (pf->lan_vsi == I40E_NO_VSI)
  7225. vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
  7226. else if (reinit)
  7227. vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
  7228. if (!vsi) {
  7229. dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
  7230. i40e_fdir_teardown(pf);
  7231. return -EAGAIN;
  7232. }
  7233. } else {
  7234. /* force a reset of TC and queue layout configurations */
  7235. u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
  7236. pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
  7237. pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
  7238. i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
  7239. }
  7240. i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
  7241. i40e_fdir_sb_setup(pf);
  7242. /* Setup static PF queue filter control settings */
  7243. ret = i40e_setup_pf_filter_control(pf);
  7244. if (ret) {
  7245. dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
  7246. ret);
  7247. /* Failure here should not stop continuing other steps */
  7248. }
  7249. /* enable RSS in the HW, even for only one queue, as the stack can use
  7250. * the hash
  7251. */
  7252. if ((pf->flags & I40E_FLAG_RSS_ENABLED))
  7253. i40e_config_rss(pf);
  7254. /* fill in link information and enable LSE reporting */
  7255. i40e_aq_get_link_info(&pf->hw, true, NULL, NULL);
  7256. i40e_link_event(pf);
  7257. /* Initialize user-specific link properties */
  7258. pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
  7259. I40E_AQ_AN_COMPLETED) ? true : false);
  7260. /* requested_mode is set in probe or by ethtool */
  7261. if (!pf->fc_autoneg_status)
  7262. goto no_autoneg;
  7263. if ((pf->hw.phy.link_info.an_info & I40E_AQ_LINK_PAUSE_TX) &&
  7264. (pf->hw.phy.link_info.an_info & I40E_AQ_LINK_PAUSE_RX))
  7265. pf->hw.fc.current_mode = I40E_FC_FULL;
  7266. else if (pf->hw.phy.link_info.an_info & I40E_AQ_LINK_PAUSE_TX)
  7267. pf->hw.fc.current_mode = I40E_FC_TX_PAUSE;
  7268. else if (pf->hw.phy.link_info.an_info & I40E_AQ_LINK_PAUSE_RX)
  7269. pf->hw.fc.current_mode = I40E_FC_RX_PAUSE;
  7270. else
  7271. pf->hw.fc.current_mode = I40E_FC_NONE;
  7272. /* sync the flow control settings with the auto-neg values */
  7273. switch (pf->hw.fc.current_mode) {
  7274. case I40E_FC_FULL:
  7275. txfc = 1;
  7276. rxfc = 1;
  7277. break;
  7278. case I40E_FC_TX_PAUSE:
  7279. txfc = 1;
  7280. rxfc = 0;
  7281. break;
  7282. case I40E_FC_RX_PAUSE:
  7283. txfc = 0;
  7284. rxfc = 1;
  7285. break;
  7286. case I40E_FC_NONE:
  7287. case I40E_FC_DEFAULT:
  7288. txfc = 0;
  7289. rxfc = 0;
  7290. break;
  7291. case I40E_FC_PFC:
  7292. /* TBD */
  7293. break;
  7294. /* no default case, we have to handle all possibilities here */
  7295. }
  7296. wr32(&pf->hw, I40E_PRTDCB_FCCFG, txfc << I40E_PRTDCB_FCCFG_TFCE_SHIFT);
  7297. rxfc_reg = rd32(&pf->hw, I40E_PRTDCB_MFLCN) &
  7298. ~I40E_PRTDCB_MFLCN_RFCE_MASK;
  7299. rxfc_reg |= (rxfc << I40E_PRTDCB_MFLCN_RFCE_SHIFT);
  7300. wr32(&pf->hw, I40E_PRTDCB_MFLCN, rxfc_reg);
  7301. goto fc_complete;
  7302. no_autoneg:
  7303. /* disable L2 flow control, user can turn it on if they wish */
  7304. wr32(&pf->hw, I40E_PRTDCB_FCCFG, 0);
  7305. wr32(&pf->hw, I40E_PRTDCB_MFLCN, rd32(&pf->hw, I40E_PRTDCB_MFLCN) &
  7306. ~I40E_PRTDCB_MFLCN_RFCE_MASK);
  7307. fc_complete:
  7308. i40e_ptp_init(pf);
  7309. return ret;
  7310. }
  7311. /**
  7312. * i40e_determine_queue_usage - Work out queue distribution
  7313. * @pf: board private structure
  7314. **/
  7315. static void i40e_determine_queue_usage(struct i40e_pf *pf)
  7316. {
  7317. int queues_left;
  7318. pf->num_lan_qps = 0;
  7319. /* Find the max queues to be put into basic use. We'll always be
  7320. * using TC0, whether or not DCB is running, and TC0 will get the
  7321. * big RSS set.
  7322. */
  7323. queues_left = pf->hw.func_caps.num_tx_qp;
  7324. if ((queues_left == 1) ||
  7325. !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
  7326. /* one qp for PF, no queues for anything else */
  7327. queues_left = 0;
  7328. pf->rss_size = pf->num_lan_qps = 1;
  7329. /* make sure all the fancies are disabled */
  7330. pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
  7331. I40E_FLAG_FD_SB_ENABLED |
  7332. I40E_FLAG_FD_ATR_ENABLED |
  7333. I40E_FLAG_DCB_CAPABLE |
  7334. I40E_FLAG_SRIOV_ENABLED |
  7335. I40E_FLAG_VMDQ_ENABLED);
  7336. } else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
  7337. I40E_FLAG_FD_SB_ENABLED |
  7338. I40E_FLAG_FD_ATR_ENABLED |
  7339. I40E_FLAG_DCB_CAPABLE))) {
  7340. /* one qp for PF */
  7341. pf->rss_size = pf->num_lan_qps = 1;
  7342. queues_left -= pf->num_lan_qps;
  7343. pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
  7344. I40E_FLAG_FD_SB_ENABLED |
  7345. I40E_FLAG_FD_ATR_ENABLED |
  7346. I40E_FLAG_DCB_ENABLED |
  7347. I40E_FLAG_VMDQ_ENABLED);
  7348. } else {
  7349. /* Not enough queues for all TCs */
  7350. if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
  7351. (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
  7352. pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
  7353. dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
  7354. }
  7355. pf->num_lan_qps = pf->rss_size_max;
  7356. queues_left -= pf->num_lan_qps;
  7357. }
  7358. if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
  7359. if (queues_left > 1) {
  7360. queues_left -= 1; /* save 1 queue for FD */
  7361. } else {
  7362. pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
  7363. dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
  7364. }
  7365. }
  7366. if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
  7367. pf->num_vf_qps && pf->num_req_vfs && queues_left) {
  7368. pf->num_req_vfs = min_t(int, pf->num_req_vfs,
  7369. (queues_left / pf->num_vf_qps));
  7370. queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
  7371. }
  7372. if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
  7373. pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
  7374. pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
  7375. (queues_left / pf->num_vmdq_qps));
  7376. queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
  7377. }
  7378. pf->queues_left = queues_left;
  7379. }
  7380. /**
  7381. * i40e_setup_pf_filter_control - Setup PF static filter control
  7382. * @pf: PF to be setup
  7383. *
  7384. * i40e_setup_pf_filter_control sets up a pf's initial filter control
  7385. * settings. If PE/FCoE are enabled then it will also set the per PF
  7386. * based filter sizes required for them. It also enables Flow director,
  7387. * ethertype and macvlan type filter settings for the pf.
  7388. *
  7389. * Returns 0 on success, negative on failure
  7390. **/
  7391. static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
  7392. {
  7393. struct i40e_filter_control_settings *settings = &pf->filter_settings;
  7394. settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
  7395. /* Flow Director is enabled */
  7396. if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
  7397. settings->enable_fdir = true;
  7398. /* Ethtype and MACVLAN filters enabled for PF */
  7399. settings->enable_ethtype = true;
  7400. settings->enable_macvlan = true;
  7401. if (i40e_set_filter_control(&pf->hw, settings))
  7402. return -ENOENT;
  7403. return 0;
  7404. }
  7405. #define INFO_STRING_LEN 255
  7406. static void i40e_print_features(struct i40e_pf *pf)
  7407. {
  7408. struct i40e_hw *hw = &pf->hw;
  7409. char *buf, *string;
  7410. string = kzalloc(INFO_STRING_LEN, GFP_KERNEL);
  7411. if (!string) {
  7412. dev_err(&pf->pdev->dev, "Features string allocation failed\n");
  7413. return;
  7414. }
  7415. buf = string;
  7416. buf += sprintf(string, "Features: PF-id[%d] ", hw->pf_id);
  7417. #ifdef CONFIG_PCI_IOV
  7418. buf += sprintf(buf, "VFs: %d ", pf->num_req_vfs);
  7419. #endif
  7420. buf += sprintf(buf, "VSIs: %d QP: %d ", pf->hw.func_caps.num_vsis,
  7421. pf->vsi[pf->lan_vsi]->num_queue_pairs);
  7422. if (pf->flags & I40E_FLAG_RSS_ENABLED)
  7423. buf += sprintf(buf, "RSS ");
  7424. if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
  7425. buf += sprintf(buf, "FD_ATR ");
  7426. if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
  7427. buf += sprintf(buf, "FD_SB ");
  7428. buf += sprintf(buf, "NTUPLE ");
  7429. }
  7430. if (pf->flags & I40E_FLAG_DCB_CAPABLE)
  7431. buf += sprintf(buf, "DCB ");
  7432. if (pf->flags & I40E_FLAG_PTP)
  7433. buf += sprintf(buf, "PTP ");
  7434. BUG_ON(buf > (string + INFO_STRING_LEN));
  7435. dev_info(&pf->pdev->dev, "%s\n", string);
  7436. kfree(string);
  7437. }
  7438. /**
  7439. * i40e_probe - Device initialization routine
  7440. * @pdev: PCI device information struct
  7441. * @ent: entry in i40e_pci_tbl
  7442. *
  7443. * i40e_probe initializes a pf identified by a pci_dev structure.
  7444. * The OS initialization, configuring of the pf private structure,
  7445. * and a hardware reset occur.
  7446. *
  7447. * Returns 0 on success, negative on failure
  7448. **/
  7449. static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  7450. {
  7451. struct i40e_pf *pf;
  7452. struct i40e_hw *hw;
  7453. static u16 pfs_found;
  7454. u16 link_status;
  7455. int err = 0;
  7456. u32 len;
  7457. u32 i;
  7458. err = pci_enable_device_mem(pdev);
  7459. if (err)
  7460. return err;
  7461. /* set up for high or low dma */
  7462. err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
  7463. if (err) {
  7464. err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  7465. if (err) {
  7466. dev_err(&pdev->dev,
  7467. "DMA configuration failed: 0x%x\n", err);
  7468. goto err_dma;
  7469. }
  7470. }
  7471. /* set up pci connections */
  7472. err = pci_request_selected_regions(pdev, pci_select_bars(pdev,
  7473. IORESOURCE_MEM), i40e_driver_name);
  7474. if (err) {
  7475. dev_info(&pdev->dev,
  7476. "pci_request_selected_regions failed %d\n", err);
  7477. goto err_pci_reg;
  7478. }
  7479. pci_enable_pcie_error_reporting(pdev);
  7480. pci_set_master(pdev);
  7481. /* Now that we have a PCI connection, we need to do the
  7482. * low level device setup. This is primarily setting up
  7483. * the Admin Queue structures and then querying for the
  7484. * device's current profile information.
  7485. */
  7486. pf = kzalloc(sizeof(*pf), GFP_KERNEL);
  7487. if (!pf) {
  7488. err = -ENOMEM;
  7489. goto err_pf_alloc;
  7490. }
  7491. pf->next_vsi = 0;
  7492. pf->pdev = pdev;
  7493. set_bit(__I40E_DOWN, &pf->state);
  7494. hw = &pf->hw;
  7495. hw->back = pf;
  7496. hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
  7497. pci_resource_len(pdev, 0));
  7498. if (!hw->hw_addr) {
  7499. err = -EIO;
  7500. dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
  7501. (unsigned int)pci_resource_start(pdev, 0),
  7502. (unsigned int)pci_resource_len(pdev, 0), err);
  7503. goto err_ioremap;
  7504. }
  7505. hw->vendor_id = pdev->vendor;
  7506. hw->device_id = pdev->device;
  7507. pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
  7508. hw->subsystem_vendor_id = pdev->subsystem_vendor;
  7509. hw->subsystem_device_id = pdev->subsystem_device;
  7510. hw->bus.device = PCI_SLOT(pdev->devfn);
  7511. hw->bus.func = PCI_FUNC(pdev->devfn);
  7512. pf->instance = pfs_found;
  7513. /* do a special CORER for clearing PXE mode once at init */
  7514. if (hw->revision_id == 0 &&
  7515. (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
  7516. wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
  7517. i40e_flush(hw);
  7518. msleep(200);
  7519. pf->corer_count++;
  7520. i40e_clear_pxe_mode(hw);
  7521. }
  7522. /* Reset here to make sure all is clean and to define PF 'n' */
  7523. err = i40e_pf_reset(hw);
  7524. if (err) {
  7525. dev_info(&pdev->dev, "Initial pf_reset failed: %d\n", err);
  7526. goto err_pf_reset;
  7527. }
  7528. pf->pfr_count++;
  7529. hw->aq.num_arq_entries = I40E_AQ_LEN;
  7530. hw->aq.num_asq_entries = I40E_AQ_LEN;
  7531. hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
  7532. hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
  7533. pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
  7534. snprintf(pf->misc_int_name, sizeof(pf->misc_int_name) - 1,
  7535. "%s-pf%d:misc",
  7536. dev_driver_string(&pf->pdev->dev), pf->hw.pf_id);
  7537. err = i40e_init_shared_code(hw);
  7538. if (err) {
  7539. dev_info(&pdev->dev, "init_shared_code failed: %d\n", err);
  7540. goto err_pf_reset;
  7541. }
  7542. /* set up a default setting for link flow control */
  7543. pf->hw.fc.requested_mode = I40E_FC_NONE;
  7544. err = i40e_init_adminq(hw);
  7545. dev_info(&pdev->dev, "%s\n", i40e_fw_version_str(hw));
  7546. if (err) {
  7547. dev_info(&pdev->dev,
  7548. "init_adminq failed: %d expecting API %02x.%02x\n",
  7549. err,
  7550. I40E_FW_API_VERSION_MAJOR, I40E_FW_API_VERSION_MINOR);
  7551. goto err_pf_reset;
  7552. }
  7553. if (hw->aq.api_min_ver > I40E_FW_API_VERSION_MINOR)
  7554. dev_info(&pdev->dev,
  7555. "Note: FW API version %02x.%02x newer than expected %02x.%02x, recommend driver update.\n",
  7556. hw->aq.api_maj_ver, hw->aq.api_min_ver,
  7557. I40E_FW_API_VERSION_MAJOR, I40E_FW_API_VERSION_MINOR);
  7558. if (hw->aq.api_maj_ver < I40E_FW_API_VERSION_MAJOR ||
  7559. hw->aq.api_min_ver < (I40E_FW_API_VERSION_MINOR-1))
  7560. dev_info(&pdev->dev,
  7561. "Note: FW API version %02x.%02x older than expected %02x.%02x, recommend nvm update.\n",
  7562. hw->aq.api_maj_ver, hw->aq.api_min_ver,
  7563. I40E_FW_API_VERSION_MAJOR, I40E_FW_API_VERSION_MINOR);
  7564. i40e_verify_eeprom(pf);
  7565. /* Rev 0 hardware was never productized */
  7566. if (hw->revision_id < 1)
  7567. 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");
  7568. i40e_clear_pxe_mode(hw);
  7569. err = i40e_get_capabilities(pf);
  7570. if (err)
  7571. goto err_adminq_setup;
  7572. err = i40e_sw_init(pf);
  7573. if (err) {
  7574. dev_info(&pdev->dev, "sw_init failed: %d\n", err);
  7575. goto err_sw_init;
  7576. }
  7577. err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
  7578. hw->func_caps.num_rx_qp,
  7579. pf->fcoe_hmc_cntx_num, pf->fcoe_hmc_filt_num);
  7580. if (err) {
  7581. dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
  7582. goto err_init_lan_hmc;
  7583. }
  7584. err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
  7585. if (err) {
  7586. dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
  7587. err = -ENOENT;
  7588. goto err_configure_lan_hmc;
  7589. }
  7590. i40e_get_mac_addr(hw, hw->mac.addr);
  7591. if (!is_valid_ether_addr(hw->mac.addr)) {
  7592. dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
  7593. err = -EIO;
  7594. goto err_mac_addr;
  7595. }
  7596. dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
  7597. ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
  7598. pci_set_drvdata(pdev, pf);
  7599. pci_save_state(pdev);
  7600. #ifdef CONFIG_I40E_DCB
  7601. err = i40e_init_pf_dcb(pf);
  7602. if (err) {
  7603. dev_info(&pdev->dev, "init_pf_dcb failed: %d\n", err);
  7604. pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
  7605. /* Continue without DCB enabled */
  7606. }
  7607. #endif /* CONFIG_I40E_DCB */
  7608. /* set up periodic task facility */
  7609. setup_timer(&pf->service_timer, i40e_service_timer, (unsigned long)pf);
  7610. pf->service_timer_period = HZ;
  7611. INIT_WORK(&pf->service_task, i40e_service_task);
  7612. clear_bit(__I40E_SERVICE_SCHED, &pf->state);
  7613. pf->flags |= I40E_FLAG_NEED_LINK_UPDATE;
  7614. pf->link_check_timeout = jiffies;
  7615. /* WoL defaults to disabled */
  7616. pf->wol_en = false;
  7617. device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
  7618. /* set up the main switch operations */
  7619. i40e_determine_queue_usage(pf);
  7620. i40e_init_interrupt_scheme(pf);
  7621. /* The number of VSIs reported by the FW is the minimum guaranteed
  7622. * to us; HW supports far more and we share the remaining pool with
  7623. * the other PFs. We allocate space for more than the guarantee with
  7624. * the understanding that we might not get them all later.
  7625. */
  7626. if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
  7627. pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
  7628. else
  7629. pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
  7630. /* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
  7631. len = sizeof(struct i40e_vsi *) * pf->num_alloc_vsi;
  7632. pf->vsi = kzalloc(len, GFP_KERNEL);
  7633. if (!pf->vsi) {
  7634. err = -ENOMEM;
  7635. goto err_switch_setup;
  7636. }
  7637. err = i40e_setup_pf_switch(pf, false);
  7638. if (err) {
  7639. dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
  7640. goto err_vsis;
  7641. }
  7642. /* if FDIR VSI was set up, start it now */
  7643. for (i = 0; i < pf->num_alloc_vsi; i++) {
  7644. if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
  7645. i40e_vsi_open(pf->vsi[i]);
  7646. break;
  7647. }
  7648. }
  7649. /* The main driver is (mostly) up and happy. We need to set this state
  7650. * before setting up the misc vector or we get a race and the vector
  7651. * ends up disabled forever.
  7652. */
  7653. clear_bit(__I40E_DOWN, &pf->state);
  7654. /* In case of MSIX we are going to setup the misc vector right here
  7655. * to handle admin queue events etc. In case of legacy and MSI
  7656. * the misc functionality and queue processing is combined in
  7657. * the same vector and that gets setup at open.
  7658. */
  7659. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  7660. err = i40e_setup_misc_vector(pf);
  7661. if (err) {
  7662. dev_info(&pdev->dev,
  7663. "setup of misc vector failed: %d\n", err);
  7664. goto err_vsis;
  7665. }
  7666. }
  7667. #ifdef CONFIG_PCI_IOV
  7668. /* prep for VF support */
  7669. if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
  7670. (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
  7671. !test_bit(__I40E_BAD_EEPROM, &pf->state)) {
  7672. u32 val;
  7673. /* disable link interrupts for VFs */
  7674. val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
  7675. val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
  7676. wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
  7677. i40e_flush(hw);
  7678. if (pci_num_vf(pdev)) {
  7679. dev_info(&pdev->dev,
  7680. "Active VFs found, allocating resources.\n");
  7681. err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
  7682. if (err)
  7683. dev_info(&pdev->dev,
  7684. "Error %d allocating resources for existing VFs\n",
  7685. err);
  7686. }
  7687. }
  7688. #endif /* CONFIG_PCI_IOV */
  7689. pfs_found++;
  7690. i40e_dbg_pf_init(pf);
  7691. /* tell the firmware that we're starting */
  7692. i40e_send_version(pf);
  7693. /* since everything's happy, start the service_task timer */
  7694. mod_timer(&pf->service_timer,
  7695. round_jiffies(jiffies + pf->service_timer_period));
  7696. /* Get the negotiated link width and speed from PCI config space */
  7697. pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA, &link_status);
  7698. i40e_set_pci_config_data(hw, link_status);
  7699. dev_info(&pdev->dev, "PCI-Express: %s %s\n",
  7700. (hw->bus.speed == i40e_bus_speed_8000 ? "Speed 8.0GT/s" :
  7701. hw->bus.speed == i40e_bus_speed_5000 ? "Speed 5.0GT/s" :
  7702. hw->bus.speed == i40e_bus_speed_2500 ? "Speed 2.5GT/s" :
  7703. "Unknown"),
  7704. (hw->bus.width == i40e_bus_width_pcie_x8 ? "Width x8" :
  7705. hw->bus.width == i40e_bus_width_pcie_x4 ? "Width x4" :
  7706. hw->bus.width == i40e_bus_width_pcie_x2 ? "Width x2" :
  7707. hw->bus.width == i40e_bus_width_pcie_x1 ? "Width x1" :
  7708. "Unknown"));
  7709. if (hw->bus.width < i40e_bus_width_pcie_x8 ||
  7710. hw->bus.speed < i40e_bus_speed_8000) {
  7711. dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
  7712. dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
  7713. }
  7714. /* print a string summarizing features */
  7715. i40e_print_features(pf);
  7716. return 0;
  7717. /* Unwind what we've done if something failed in the setup */
  7718. err_vsis:
  7719. set_bit(__I40E_DOWN, &pf->state);
  7720. i40e_clear_interrupt_scheme(pf);
  7721. kfree(pf->vsi);
  7722. err_switch_setup:
  7723. i40e_reset_interrupt_capability(pf);
  7724. del_timer_sync(&pf->service_timer);
  7725. err_mac_addr:
  7726. err_configure_lan_hmc:
  7727. (void)i40e_shutdown_lan_hmc(hw);
  7728. err_init_lan_hmc:
  7729. kfree(pf->qp_pile);
  7730. kfree(pf->irq_pile);
  7731. err_sw_init:
  7732. err_adminq_setup:
  7733. (void)i40e_shutdown_adminq(hw);
  7734. err_pf_reset:
  7735. iounmap(hw->hw_addr);
  7736. err_ioremap:
  7737. kfree(pf);
  7738. err_pf_alloc:
  7739. pci_disable_pcie_error_reporting(pdev);
  7740. pci_release_selected_regions(pdev,
  7741. pci_select_bars(pdev, IORESOURCE_MEM));
  7742. err_pci_reg:
  7743. err_dma:
  7744. pci_disable_device(pdev);
  7745. return err;
  7746. }
  7747. /**
  7748. * i40e_remove - Device removal routine
  7749. * @pdev: PCI device information struct
  7750. *
  7751. * i40e_remove is called by the PCI subsystem to alert the driver
  7752. * that is should release a PCI device. This could be caused by a
  7753. * Hot-Plug event, or because the driver is going to be removed from
  7754. * memory.
  7755. **/
  7756. static void i40e_remove(struct pci_dev *pdev)
  7757. {
  7758. struct i40e_pf *pf = pci_get_drvdata(pdev);
  7759. i40e_status ret_code;
  7760. u32 reg;
  7761. int i;
  7762. i40e_dbg_pf_exit(pf);
  7763. i40e_ptp_stop(pf);
  7764. /* no more scheduling of any task */
  7765. set_bit(__I40E_DOWN, &pf->state);
  7766. del_timer_sync(&pf->service_timer);
  7767. cancel_work_sync(&pf->service_task);
  7768. if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
  7769. i40e_free_vfs(pf);
  7770. pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
  7771. }
  7772. i40e_fdir_teardown(pf);
  7773. /* If there is a switch structure or any orphans, remove them.
  7774. * This will leave only the PF's VSI remaining.
  7775. */
  7776. for (i = 0; i < I40E_MAX_VEB; i++) {
  7777. if (!pf->veb[i])
  7778. continue;
  7779. if (pf->veb[i]->uplink_seid == pf->mac_seid ||
  7780. pf->veb[i]->uplink_seid == 0)
  7781. i40e_switch_branch_release(pf->veb[i]);
  7782. }
  7783. /* Now we can shutdown the PF's VSI, just before we kill
  7784. * adminq and hmc.
  7785. */
  7786. if (pf->vsi[pf->lan_vsi])
  7787. i40e_vsi_release(pf->vsi[pf->lan_vsi]);
  7788. i40e_stop_misc_vector(pf);
  7789. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  7790. synchronize_irq(pf->msix_entries[0].vector);
  7791. free_irq(pf->msix_entries[0].vector, pf);
  7792. }
  7793. /* shutdown and destroy the HMC */
  7794. if (pf->hw.hmc.hmc_obj) {
  7795. ret_code = i40e_shutdown_lan_hmc(&pf->hw);
  7796. if (ret_code)
  7797. dev_warn(&pdev->dev,
  7798. "Failed to destroy the HMC resources: %d\n",
  7799. ret_code);
  7800. }
  7801. /* shutdown the adminq */
  7802. ret_code = i40e_shutdown_adminq(&pf->hw);
  7803. if (ret_code)
  7804. dev_warn(&pdev->dev,
  7805. "Failed to destroy the Admin Queue resources: %d\n",
  7806. ret_code);
  7807. /* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
  7808. i40e_clear_interrupt_scheme(pf);
  7809. for (i = 0; i < pf->num_alloc_vsi; i++) {
  7810. if (pf->vsi[i]) {
  7811. i40e_vsi_clear_rings(pf->vsi[i]);
  7812. i40e_vsi_clear(pf->vsi[i]);
  7813. pf->vsi[i] = NULL;
  7814. }
  7815. }
  7816. for (i = 0; i < I40E_MAX_VEB; i++) {
  7817. kfree(pf->veb[i]);
  7818. pf->veb[i] = NULL;
  7819. }
  7820. kfree(pf->qp_pile);
  7821. kfree(pf->irq_pile);
  7822. kfree(pf->vsi);
  7823. /* force a PF reset to clean anything leftover */
  7824. reg = rd32(&pf->hw, I40E_PFGEN_CTRL);
  7825. wr32(&pf->hw, I40E_PFGEN_CTRL, (reg | I40E_PFGEN_CTRL_PFSWR_MASK));
  7826. i40e_flush(&pf->hw);
  7827. iounmap(pf->hw.hw_addr);
  7828. kfree(pf);
  7829. pci_release_selected_regions(pdev,
  7830. pci_select_bars(pdev, IORESOURCE_MEM));
  7831. pci_disable_pcie_error_reporting(pdev);
  7832. pci_disable_device(pdev);
  7833. }
  7834. /**
  7835. * i40e_pci_error_detected - warning that something funky happened in PCI land
  7836. * @pdev: PCI device information struct
  7837. *
  7838. * Called to warn that something happened and the error handling steps
  7839. * are in progress. Allows the driver to quiesce things, be ready for
  7840. * remediation.
  7841. **/
  7842. static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
  7843. enum pci_channel_state error)
  7844. {
  7845. struct i40e_pf *pf = pci_get_drvdata(pdev);
  7846. dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
  7847. /* shutdown all operations */
  7848. if (!test_bit(__I40E_SUSPENDED, &pf->state)) {
  7849. rtnl_lock();
  7850. i40e_prep_for_reset(pf);
  7851. rtnl_unlock();
  7852. }
  7853. /* Request a slot reset */
  7854. return PCI_ERS_RESULT_NEED_RESET;
  7855. }
  7856. /**
  7857. * i40e_pci_error_slot_reset - a PCI slot reset just happened
  7858. * @pdev: PCI device information struct
  7859. *
  7860. * Called to find if the driver can work with the device now that
  7861. * the pci slot has been reset. If a basic connection seems good
  7862. * (registers are readable and have sane content) then return a
  7863. * happy little PCI_ERS_RESULT_xxx.
  7864. **/
  7865. static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
  7866. {
  7867. struct i40e_pf *pf = pci_get_drvdata(pdev);
  7868. pci_ers_result_t result;
  7869. int err;
  7870. u32 reg;
  7871. dev_info(&pdev->dev, "%s\n", __func__);
  7872. if (pci_enable_device_mem(pdev)) {
  7873. dev_info(&pdev->dev,
  7874. "Cannot re-enable PCI device after reset.\n");
  7875. result = PCI_ERS_RESULT_DISCONNECT;
  7876. } else {
  7877. pci_set_master(pdev);
  7878. pci_restore_state(pdev);
  7879. pci_save_state(pdev);
  7880. pci_wake_from_d3(pdev, false);
  7881. reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
  7882. if (reg == 0)
  7883. result = PCI_ERS_RESULT_RECOVERED;
  7884. else
  7885. result = PCI_ERS_RESULT_DISCONNECT;
  7886. }
  7887. err = pci_cleanup_aer_uncorrect_error_status(pdev);
  7888. if (err) {
  7889. dev_info(&pdev->dev,
  7890. "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
  7891. err);
  7892. /* non-fatal, continue */
  7893. }
  7894. return result;
  7895. }
  7896. /**
  7897. * i40e_pci_error_resume - restart operations after PCI error recovery
  7898. * @pdev: PCI device information struct
  7899. *
  7900. * Called to allow the driver to bring things back up after PCI error
  7901. * and/or reset recovery has finished.
  7902. **/
  7903. static void i40e_pci_error_resume(struct pci_dev *pdev)
  7904. {
  7905. struct i40e_pf *pf = pci_get_drvdata(pdev);
  7906. dev_info(&pdev->dev, "%s\n", __func__);
  7907. if (test_bit(__I40E_SUSPENDED, &pf->state))
  7908. return;
  7909. rtnl_lock();
  7910. i40e_handle_reset_warning(pf);
  7911. rtnl_lock();
  7912. }
  7913. /**
  7914. * i40e_shutdown - PCI callback for shutting down
  7915. * @pdev: PCI device information struct
  7916. **/
  7917. static void i40e_shutdown(struct pci_dev *pdev)
  7918. {
  7919. struct i40e_pf *pf = pci_get_drvdata(pdev);
  7920. struct i40e_hw *hw = &pf->hw;
  7921. set_bit(__I40E_SUSPENDED, &pf->state);
  7922. set_bit(__I40E_DOWN, &pf->state);
  7923. rtnl_lock();
  7924. i40e_prep_for_reset(pf);
  7925. rtnl_unlock();
  7926. wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
  7927. wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
  7928. if (system_state == SYSTEM_POWER_OFF) {
  7929. pci_wake_from_d3(pdev, pf->wol_en);
  7930. pci_set_power_state(pdev, PCI_D3hot);
  7931. }
  7932. }
  7933. #ifdef CONFIG_PM
  7934. /**
  7935. * i40e_suspend - PCI callback for moving to D3
  7936. * @pdev: PCI device information struct
  7937. **/
  7938. static int i40e_suspend(struct pci_dev *pdev, pm_message_t state)
  7939. {
  7940. struct i40e_pf *pf = pci_get_drvdata(pdev);
  7941. struct i40e_hw *hw = &pf->hw;
  7942. set_bit(__I40E_SUSPENDED, &pf->state);
  7943. set_bit(__I40E_DOWN, &pf->state);
  7944. rtnl_lock();
  7945. i40e_prep_for_reset(pf);
  7946. rtnl_unlock();
  7947. wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
  7948. wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
  7949. pci_wake_from_d3(pdev, pf->wol_en);
  7950. pci_set_power_state(pdev, PCI_D3hot);
  7951. return 0;
  7952. }
  7953. /**
  7954. * i40e_resume - PCI callback for waking up from D3
  7955. * @pdev: PCI device information struct
  7956. **/
  7957. static int i40e_resume(struct pci_dev *pdev)
  7958. {
  7959. struct i40e_pf *pf = pci_get_drvdata(pdev);
  7960. u32 err;
  7961. pci_set_power_state(pdev, PCI_D0);
  7962. pci_restore_state(pdev);
  7963. /* pci_restore_state() clears dev->state_saves, so
  7964. * call pci_save_state() again to restore it.
  7965. */
  7966. pci_save_state(pdev);
  7967. err = pci_enable_device_mem(pdev);
  7968. if (err) {
  7969. dev_err(&pdev->dev,
  7970. "%s: Cannot enable PCI device from suspend\n",
  7971. __func__);
  7972. return err;
  7973. }
  7974. pci_set_master(pdev);
  7975. /* no wakeup events while running */
  7976. pci_wake_from_d3(pdev, false);
  7977. /* handling the reset will rebuild the device state */
  7978. if (test_and_clear_bit(__I40E_SUSPENDED, &pf->state)) {
  7979. clear_bit(__I40E_DOWN, &pf->state);
  7980. rtnl_lock();
  7981. i40e_reset_and_rebuild(pf, false);
  7982. rtnl_unlock();
  7983. }
  7984. return 0;
  7985. }
  7986. #endif
  7987. static const struct pci_error_handlers i40e_err_handler = {
  7988. .error_detected = i40e_pci_error_detected,
  7989. .slot_reset = i40e_pci_error_slot_reset,
  7990. .resume = i40e_pci_error_resume,
  7991. };
  7992. static struct pci_driver i40e_driver = {
  7993. .name = i40e_driver_name,
  7994. .id_table = i40e_pci_tbl,
  7995. .probe = i40e_probe,
  7996. .remove = i40e_remove,
  7997. #ifdef CONFIG_PM
  7998. .suspend = i40e_suspend,
  7999. .resume = i40e_resume,
  8000. #endif
  8001. .shutdown = i40e_shutdown,
  8002. .err_handler = &i40e_err_handler,
  8003. .sriov_configure = i40e_pci_sriov_configure,
  8004. };
  8005. /**
  8006. * i40e_init_module - Driver registration routine
  8007. *
  8008. * i40e_init_module is the first routine called when the driver is
  8009. * loaded. All it does is register with the PCI subsystem.
  8010. **/
  8011. static int __init i40e_init_module(void)
  8012. {
  8013. pr_info("%s: %s - version %s\n", i40e_driver_name,
  8014. i40e_driver_string, i40e_driver_version_str);
  8015. pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
  8016. i40e_dbg_init();
  8017. return pci_register_driver(&i40e_driver);
  8018. }
  8019. module_init(i40e_init_module);
  8020. /**
  8021. * i40e_exit_module - Driver exit cleanup routine
  8022. *
  8023. * i40e_exit_module is called just before the driver is removed
  8024. * from memory.
  8025. **/
  8026. static void __exit i40e_exit_module(void)
  8027. {
  8028. pci_unregister_driver(&i40e_driver);
  8029. i40e_dbg_exit();
  8030. }
  8031. module_exit(i40e_exit_module);