i40e_main.c 226 KB

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