i40e_main.c 238 KB

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