i40e_main.c 235 KB

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