i40e_main.c 235 KB

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