i40e_main.c 329 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898
  1. /*******************************************************************************
  2. *
  3. * Intel Ethernet Controller XL710 Family Linux Driver
  4. * Copyright(c) 2013 - 2016 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. #include <linux/etherdevice.h>
  27. #include <linux/of_net.h>
  28. #include <linux/pci.h>
  29. /* Local includes */
  30. #include "i40e.h"
  31. #include "i40e_diag.h"
  32. #include <net/udp_tunnel.h>
  33. /* All i40e tracepoints are defined by the include below, which
  34. * must be included exactly once across the whole kernel with
  35. * CREATE_TRACE_POINTS defined
  36. */
  37. #define CREATE_TRACE_POINTS
  38. #include "i40e_trace.h"
  39. const char i40e_driver_name[] = "i40e";
  40. static const char i40e_driver_string[] =
  41. "Intel(R) Ethernet Connection XL710 Network Driver";
  42. #define DRV_KERN "-k"
  43. #define DRV_VERSION_MAJOR 2
  44. #define DRV_VERSION_MINOR 1
  45. #define DRV_VERSION_BUILD 7
  46. #define DRV_VERSION __stringify(DRV_VERSION_MAJOR) "." \
  47. __stringify(DRV_VERSION_MINOR) "." \
  48. __stringify(DRV_VERSION_BUILD) DRV_KERN
  49. const char i40e_driver_version_str[] = DRV_VERSION;
  50. static const char i40e_copyright[] = "Copyright (c) 2013 - 2014 Intel Corporation.";
  51. /* a bit of forward declarations */
  52. static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi);
  53. static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired);
  54. static int i40e_add_vsi(struct i40e_vsi *vsi);
  55. static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi);
  56. static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit);
  57. static int i40e_setup_misc_vector(struct i40e_pf *pf);
  58. static void i40e_determine_queue_usage(struct i40e_pf *pf);
  59. static int i40e_setup_pf_filter_control(struct i40e_pf *pf);
  60. static void i40e_prep_for_reset(struct i40e_pf *pf, bool lock_acquired);
  61. static int i40e_reset(struct i40e_pf *pf);
  62. static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired);
  63. static void i40e_fdir_sb_setup(struct i40e_pf *pf);
  64. static int i40e_veb_get_bw_info(struct i40e_veb *veb);
  65. /* i40e_pci_tbl - PCI Device ID Table
  66. *
  67. * Last entry must be all 0s
  68. *
  69. * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
  70. * Class, Class Mask, private data (not used) }
  71. */
  72. static const struct pci_device_id i40e_pci_tbl[] = {
  73. {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_XL710), 0},
  74. {PCI_VDEVICE(INTEL, I40E_DEV_ID_QEMU), 0},
  75. {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_B), 0},
  76. {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_C), 0},
  77. {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_A), 0},
  78. {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_B), 0},
  79. {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_C), 0},
  80. {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T), 0},
  81. {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T4), 0},
  82. {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_X722), 0},
  83. {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_X722), 0},
  84. {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_X722), 0},
  85. {PCI_VDEVICE(INTEL, I40E_DEV_ID_1G_BASE_T_X722), 0},
  86. {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_X722), 0},
  87. {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_I_X722), 0},
  88. {PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2), 0},
  89. {PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2_A), 0},
  90. {PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_B), 0},
  91. {PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_SFP28), 0},
  92. /* required last entry */
  93. {0, }
  94. };
  95. MODULE_DEVICE_TABLE(pci, i40e_pci_tbl);
  96. #define I40E_MAX_VF_COUNT 128
  97. static int debug = -1;
  98. module_param(debug, uint, 0);
  99. MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all), Debug mask (0x8XXXXXXX)");
  100. MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
  101. MODULE_DESCRIPTION("Intel(R) Ethernet Connection XL710 Network Driver");
  102. MODULE_LICENSE("GPL");
  103. MODULE_VERSION(DRV_VERSION);
  104. static struct workqueue_struct *i40e_wq;
  105. /**
  106. * i40e_allocate_dma_mem_d - OS specific memory alloc for shared code
  107. * @hw: pointer to the HW structure
  108. * @mem: ptr to mem struct to fill out
  109. * @size: size of memory requested
  110. * @alignment: what to align the allocation to
  111. **/
  112. int i40e_allocate_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem,
  113. u64 size, u32 alignment)
  114. {
  115. struct i40e_pf *pf = (struct i40e_pf *)hw->back;
  116. mem->size = ALIGN(size, alignment);
  117. mem->va = dma_zalloc_coherent(&pf->pdev->dev, mem->size,
  118. &mem->pa, GFP_KERNEL);
  119. if (!mem->va)
  120. return -ENOMEM;
  121. return 0;
  122. }
  123. /**
  124. * i40e_free_dma_mem_d - OS specific memory free for shared code
  125. * @hw: pointer to the HW structure
  126. * @mem: ptr to mem struct to free
  127. **/
  128. int i40e_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
  129. {
  130. struct i40e_pf *pf = (struct i40e_pf *)hw->back;
  131. dma_free_coherent(&pf->pdev->dev, mem->size, mem->va, mem->pa);
  132. mem->va = NULL;
  133. mem->pa = 0;
  134. mem->size = 0;
  135. return 0;
  136. }
  137. /**
  138. * i40e_allocate_virt_mem_d - OS specific memory alloc for shared code
  139. * @hw: pointer to the HW structure
  140. * @mem: ptr to mem struct to fill out
  141. * @size: size of memory requested
  142. **/
  143. int i40e_allocate_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem,
  144. u32 size)
  145. {
  146. mem->size = size;
  147. mem->va = kzalloc(size, GFP_KERNEL);
  148. if (!mem->va)
  149. return -ENOMEM;
  150. return 0;
  151. }
  152. /**
  153. * i40e_free_virt_mem_d - OS specific memory free for shared code
  154. * @hw: pointer to the HW structure
  155. * @mem: ptr to mem struct to free
  156. **/
  157. int i40e_free_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem)
  158. {
  159. /* it's ok to kfree a NULL pointer */
  160. kfree(mem->va);
  161. mem->va = NULL;
  162. mem->size = 0;
  163. return 0;
  164. }
  165. /**
  166. * i40e_get_lump - find a lump of free generic resource
  167. * @pf: board private structure
  168. * @pile: the pile of resource to search
  169. * @needed: the number of items needed
  170. * @id: an owner id to stick on the items assigned
  171. *
  172. * Returns the base item index of the lump, or negative for error
  173. *
  174. * The search_hint trick and lack of advanced fit-finding only work
  175. * because we're highly likely to have all the same size lump requests.
  176. * Linear search time and any fragmentation should be minimal.
  177. **/
  178. static int i40e_get_lump(struct i40e_pf *pf, struct i40e_lump_tracking *pile,
  179. u16 needed, u16 id)
  180. {
  181. int ret = -ENOMEM;
  182. int i, j;
  183. if (!pile || needed == 0 || id >= I40E_PILE_VALID_BIT) {
  184. dev_info(&pf->pdev->dev,
  185. "param err: pile=%p needed=%d id=0x%04x\n",
  186. pile, needed, id);
  187. return -EINVAL;
  188. }
  189. /* start the linear search with an imperfect hint */
  190. i = pile->search_hint;
  191. while (i < pile->num_entries) {
  192. /* skip already allocated entries */
  193. if (pile->list[i] & I40E_PILE_VALID_BIT) {
  194. i++;
  195. continue;
  196. }
  197. /* do we have enough in this lump? */
  198. for (j = 0; (j < needed) && ((i+j) < pile->num_entries); j++) {
  199. if (pile->list[i+j] & I40E_PILE_VALID_BIT)
  200. break;
  201. }
  202. if (j == needed) {
  203. /* there was enough, so assign it to the requestor */
  204. for (j = 0; j < needed; j++)
  205. pile->list[i+j] = id | I40E_PILE_VALID_BIT;
  206. ret = i;
  207. pile->search_hint = i + j;
  208. break;
  209. }
  210. /* not enough, so skip over it and continue looking */
  211. i += j;
  212. }
  213. return ret;
  214. }
  215. /**
  216. * i40e_put_lump - return a lump of generic resource
  217. * @pile: the pile of resource to search
  218. * @index: the base item index
  219. * @id: the owner id of the items assigned
  220. *
  221. * Returns the count of items in the lump
  222. **/
  223. static int i40e_put_lump(struct i40e_lump_tracking *pile, u16 index, u16 id)
  224. {
  225. int valid_id = (id | I40E_PILE_VALID_BIT);
  226. int count = 0;
  227. int i;
  228. if (!pile || index >= pile->num_entries)
  229. return -EINVAL;
  230. for (i = index;
  231. i < pile->num_entries && pile->list[i] == valid_id;
  232. i++) {
  233. pile->list[i] = 0;
  234. count++;
  235. }
  236. if (count && index < pile->search_hint)
  237. pile->search_hint = index;
  238. return count;
  239. }
  240. /**
  241. * i40e_find_vsi_from_id - searches for the vsi with the given id
  242. * @pf - the pf structure to search for the vsi
  243. * @id - id of the vsi it is searching for
  244. **/
  245. struct i40e_vsi *i40e_find_vsi_from_id(struct i40e_pf *pf, u16 id)
  246. {
  247. int i;
  248. for (i = 0; i < pf->num_alloc_vsi; i++)
  249. if (pf->vsi[i] && (pf->vsi[i]->id == id))
  250. return pf->vsi[i];
  251. return NULL;
  252. }
  253. /**
  254. * i40e_service_event_schedule - Schedule the service task to wake up
  255. * @pf: board private structure
  256. *
  257. * If not already scheduled, this puts the task into the work queue
  258. **/
  259. void i40e_service_event_schedule(struct i40e_pf *pf)
  260. {
  261. if (!test_bit(__I40E_DOWN, &pf->state) &&
  262. !test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
  263. queue_work(i40e_wq, &pf->service_task);
  264. }
  265. /**
  266. * i40e_tx_timeout - Respond to a Tx Hang
  267. * @netdev: network interface device structure
  268. *
  269. * If any port has noticed a Tx timeout, it is likely that the whole
  270. * device is munged, not just the one netdev port, so go for the full
  271. * reset.
  272. **/
  273. static void i40e_tx_timeout(struct net_device *netdev)
  274. {
  275. struct i40e_netdev_priv *np = netdev_priv(netdev);
  276. struct i40e_vsi *vsi = np->vsi;
  277. struct i40e_pf *pf = vsi->back;
  278. struct i40e_ring *tx_ring = NULL;
  279. unsigned int i, hung_queue = 0;
  280. u32 head, val;
  281. pf->tx_timeout_count++;
  282. /* find the stopped queue the same way the stack does */
  283. for (i = 0; i < netdev->num_tx_queues; i++) {
  284. struct netdev_queue *q;
  285. unsigned long trans_start;
  286. q = netdev_get_tx_queue(netdev, i);
  287. trans_start = q->trans_start;
  288. if (netif_xmit_stopped(q) &&
  289. time_after(jiffies,
  290. (trans_start + netdev->watchdog_timeo))) {
  291. hung_queue = i;
  292. break;
  293. }
  294. }
  295. if (i == netdev->num_tx_queues) {
  296. netdev_info(netdev, "tx_timeout: no netdev hung queue found\n");
  297. } else {
  298. /* now that we have an index, find the tx_ring struct */
  299. for (i = 0; i < vsi->num_queue_pairs; i++) {
  300. if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
  301. if (hung_queue ==
  302. vsi->tx_rings[i]->queue_index) {
  303. tx_ring = vsi->tx_rings[i];
  304. break;
  305. }
  306. }
  307. }
  308. }
  309. if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ*20)))
  310. pf->tx_timeout_recovery_level = 1; /* reset after some time */
  311. else if (time_before(jiffies,
  312. (pf->tx_timeout_last_recovery + netdev->watchdog_timeo)))
  313. return; /* don't do any new action before the next timeout */
  314. if (tx_ring) {
  315. head = i40e_get_head(tx_ring);
  316. /* Read interrupt register */
  317. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  318. val = rd32(&pf->hw,
  319. I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
  320. tx_ring->vsi->base_vector - 1));
  321. else
  322. val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
  323. netdev_info(netdev, "tx_timeout: VSI_seid: %d, Q %d, NTC: 0x%x, HWB: 0x%x, NTU: 0x%x, TAIL: 0x%x, INT: 0x%x\n",
  324. vsi->seid, hung_queue, tx_ring->next_to_clean,
  325. head, tx_ring->next_to_use,
  326. readl(tx_ring->tail), val);
  327. }
  328. pf->tx_timeout_last_recovery = jiffies;
  329. netdev_info(netdev, "tx_timeout recovery level %d, hung_queue %d\n",
  330. pf->tx_timeout_recovery_level, hung_queue);
  331. switch (pf->tx_timeout_recovery_level) {
  332. case 1:
  333. set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
  334. break;
  335. case 2:
  336. set_bit(__I40E_CORE_RESET_REQUESTED, &pf->state);
  337. break;
  338. case 3:
  339. set_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state);
  340. break;
  341. default:
  342. netdev_err(netdev, "tx_timeout recovery unsuccessful\n");
  343. break;
  344. }
  345. i40e_service_event_schedule(pf);
  346. pf->tx_timeout_recovery_level++;
  347. }
  348. /**
  349. * i40e_get_vsi_stats_struct - Get System Network Statistics
  350. * @vsi: the VSI we care about
  351. *
  352. * Returns the address of the device statistics structure.
  353. * The statistics are actually updated from the service task.
  354. **/
  355. struct rtnl_link_stats64 *i40e_get_vsi_stats_struct(struct i40e_vsi *vsi)
  356. {
  357. return &vsi->net_stats;
  358. }
  359. /**
  360. * i40e_get_netdev_stats_struct - Get statistics for netdev interface
  361. * @netdev: network interface device structure
  362. *
  363. * Returns the address of the device statistics structure.
  364. * The statistics are actually updated from the service task.
  365. **/
  366. static void i40e_get_netdev_stats_struct(struct net_device *netdev,
  367. struct rtnl_link_stats64 *stats)
  368. {
  369. struct i40e_netdev_priv *np = netdev_priv(netdev);
  370. struct i40e_ring *tx_ring, *rx_ring;
  371. struct i40e_vsi *vsi = np->vsi;
  372. struct rtnl_link_stats64 *vsi_stats = i40e_get_vsi_stats_struct(vsi);
  373. int i;
  374. if (test_bit(__I40E_DOWN, &vsi->state))
  375. return;
  376. if (!vsi->tx_rings)
  377. return;
  378. rcu_read_lock();
  379. for (i = 0; i < vsi->num_queue_pairs; i++) {
  380. u64 bytes, packets;
  381. unsigned int start;
  382. tx_ring = ACCESS_ONCE(vsi->tx_rings[i]);
  383. if (!tx_ring)
  384. continue;
  385. do {
  386. start = u64_stats_fetch_begin_irq(&tx_ring->syncp);
  387. packets = tx_ring->stats.packets;
  388. bytes = tx_ring->stats.bytes;
  389. } while (u64_stats_fetch_retry_irq(&tx_ring->syncp, start));
  390. stats->tx_packets += packets;
  391. stats->tx_bytes += bytes;
  392. rx_ring = &tx_ring[1];
  393. do {
  394. start = u64_stats_fetch_begin_irq(&rx_ring->syncp);
  395. packets = rx_ring->stats.packets;
  396. bytes = rx_ring->stats.bytes;
  397. } while (u64_stats_fetch_retry_irq(&rx_ring->syncp, start));
  398. stats->rx_packets += packets;
  399. stats->rx_bytes += bytes;
  400. }
  401. rcu_read_unlock();
  402. /* following stats updated by i40e_watchdog_subtask() */
  403. stats->multicast = vsi_stats->multicast;
  404. stats->tx_errors = vsi_stats->tx_errors;
  405. stats->tx_dropped = vsi_stats->tx_dropped;
  406. stats->rx_errors = vsi_stats->rx_errors;
  407. stats->rx_dropped = vsi_stats->rx_dropped;
  408. stats->rx_crc_errors = vsi_stats->rx_crc_errors;
  409. stats->rx_length_errors = vsi_stats->rx_length_errors;
  410. }
  411. /**
  412. * i40e_vsi_reset_stats - Resets all stats of the given vsi
  413. * @vsi: the VSI to have its stats reset
  414. **/
  415. void i40e_vsi_reset_stats(struct i40e_vsi *vsi)
  416. {
  417. struct rtnl_link_stats64 *ns;
  418. int i;
  419. if (!vsi)
  420. return;
  421. ns = i40e_get_vsi_stats_struct(vsi);
  422. memset(ns, 0, sizeof(*ns));
  423. memset(&vsi->net_stats_offsets, 0, sizeof(vsi->net_stats_offsets));
  424. memset(&vsi->eth_stats, 0, sizeof(vsi->eth_stats));
  425. memset(&vsi->eth_stats_offsets, 0, sizeof(vsi->eth_stats_offsets));
  426. if (vsi->rx_rings && vsi->rx_rings[0]) {
  427. for (i = 0; i < vsi->num_queue_pairs; i++) {
  428. memset(&vsi->rx_rings[i]->stats, 0,
  429. sizeof(vsi->rx_rings[i]->stats));
  430. memset(&vsi->rx_rings[i]->rx_stats, 0,
  431. sizeof(vsi->rx_rings[i]->rx_stats));
  432. memset(&vsi->tx_rings[i]->stats, 0,
  433. sizeof(vsi->tx_rings[i]->stats));
  434. memset(&vsi->tx_rings[i]->tx_stats, 0,
  435. sizeof(vsi->tx_rings[i]->tx_stats));
  436. }
  437. }
  438. vsi->stat_offsets_loaded = false;
  439. }
  440. /**
  441. * i40e_pf_reset_stats - Reset all of the stats for the given PF
  442. * @pf: the PF to be reset
  443. **/
  444. void i40e_pf_reset_stats(struct i40e_pf *pf)
  445. {
  446. int i;
  447. memset(&pf->stats, 0, sizeof(pf->stats));
  448. memset(&pf->stats_offsets, 0, sizeof(pf->stats_offsets));
  449. pf->stat_offsets_loaded = false;
  450. for (i = 0; i < I40E_MAX_VEB; i++) {
  451. if (pf->veb[i]) {
  452. memset(&pf->veb[i]->stats, 0,
  453. sizeof(pf->veb[i]->stats));
  454. memset(&pf->veb[i]->stats_offsets, 0,
  455. sizeof(pf->veb[i]->stats_offsets));
  456. pf->veb[i]->stat_offsets_loaded = false;
  457. }
  458. }
  459. pf->hw_csum_rx_error = 0;
  460. }
  461. /**
  462. * i40e_stat_update48 - read and update a 48 bit stat from the chip
  463. * @hw: ptr to the hardware info
  464. * @hireg: the high 32 bit reg to read
  465. * @loreg: the low 32 bit reg to read
  466. * @offset_loaded: has the initial offset been loaded yet
  467. * @offset: ptr to current offset value
  468. * @stat: ptr to the stat
  469. *
  470. * Since the device stats are not reset at PFReset, they likely will not
  471. * be zeroed when the driver starts. We'll save the first values read
  472. * and use them as offsets to be subtracted from the raw values in order
  473. * to report stats that count from zero. In the process, we also manage
  474. * the potential roll-over.
  475. **/
  476. static void i40e_stat_update48(struct i40e_hw *hw, u32 hireg, u32 loreg,
  477. bool offset_loaded, u64 *offset, u64 *stat)
  478. {
  479. u64 new_data;
  480. if (hw->device_id == I40E_DEV_ID_QEMU) {
  481. new_data = rd32(hw, loreg);
  482. new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
  483. } else {
  484. new_data = rd64(hw, loreg);
  485. }
  486. if (!offset_loaded)
  487. *offset = new_data;
  488. if (likely(new_data >= *offset))
  489. *stat = new_data - *offset;
  490. else
  491. *stat = (new_data + BIT_ULL(48)) - *offset;
  492. *stat &= 0xFFFFFFFFFFFFULL;
  493. }
  494. /**
  495. * i40e_stat_update32 - read and update a 32 bit stat from the chip
  496. * @hw: ptr to the hardware info
  497. * @reg: the hw reg to read
  498. * @offset_loaded: has the initial offset been loaded yet
  499. * @offset: ptr to current offset value
  500. * @stat: ptr to the stat
  501. **/
  502. static void i40e_stat_update32(struct i40e_hw *hw, u32 reg,
  503. bool offset_loaded, u64 *offset, u64 *stat)
  504. {
  505. u32 new_data;
  506. new_data = rd32(hw, reg);
  507. if (!offset_loaded)
  508. *offset = new_data;
  509. if (likely(new_data >= *offset))
  510. *stat = (u32)(new_data - *offset);
  511. else
  512. *stat = (u32)((new_data + BIT_ULL(32)) - *offset);
  513. }
  514. /**
  515. * i40e_update_eth_stats - Update VSI-specific ethernet statistics counters.
  516. * @vsi: the VSI to be updated
  517. **/
  518. void i40e_update_eth_stats(struct i40e_vsi *vsi)
  519. {
  520. int stat_idx = le16_to_cpu(vsi->info.stat_counter_idx);
  521. struct i40e_pf *pf = vsi->back;
  522. struct i40e_hw *hw = &pf->hw;
  523. struct i40e_eth_stats *oes;
  524. struct i40e_eth_stats *es; /* device's eth stats */
  525. es = &vsi->eth_stats;
  526. oes = &vsi->eth_stats_offsets;
  527. /* Gather up the stats that the hw collects */
  528. i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
  529. vsi->stat_offsets_loaded,
  530. &oes->tx_errors, &es->tx_errors);
  531. i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx),
  532. vsi->stat_offsets_loaded,
  533. &oes->rx_discards, &es->rx_discards);
  534. i40e_stat_update32(hw, I40E_GLV_RUPP(stat_idx),
  535. vsi->stat_offsets_loaded,
  536. &oes->rx_unknown_protocol, &es->rx_unknown_protocol);
  537. i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
  538. vsi->stat_offsets_loaded,
  539. &oes->tx_errors, &es->tx_errors);
  540. i40e_stat_update48(hw, I40E_GLV_GORCH(stat_idx),
  541. I40E_GLV_GORCL(stat_idx),
  542. vsi->stat_offsets_loaded,
  543. &oes->rx_bytes, &es->rx_bytes);
  544. i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
  545. I40E_GLV_UPRCL(stat_idx),
  546. vsi->stat_offsets_loaded,
  547. &oes->rx_unicast, &es->rx_unicast);
  548. i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
  549. I40E_GLV_MPRCL(stat_idx),
  550. vsi->stat_offsets_loaded,
  551. &oes->rx_multicast, &es->rx_multicast);
  552. i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
  553. I40E_GLV_BPRCL(stat_idx),
  554. vsi->stat_offsets_loaded,
  555. &oes->rx_broadcast, &es->rx_broadcast);
  556. i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
  557. I40E_GLV_GOTCL(stat_idx),
  558. vsi->stat_offsets_loaded,
  559. &oes->tx_bytes, &es->tx_bytes);
  560. i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
  561. I40E_GLV_UPTCL(stat_idx),
  562. vsi->stat_offsets_loaded,
  563. &oes->tx_unicast, &es->tx_unicast);
  564. i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
  565. I40E_GLV_MPTCL(stat_idx),
  566. vsi->stat_offsets_loaded,
  567. &oes->tx_multicast, &es->tx_multicast);
  568. i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
  569. I40E_GLV_BPTCL(stat_idx),
  570. vsi->stat_offsets_loaded,
  571. &oes->tx_broadcast, &es->tx_broadcast);
  572. vsi->stat_offsets_loaded = true;
  573. }
  574. /**
  575. * i40e_update_veb_stats - Update Switch component statistics
  576. * @veb: the VEB being updated
  577. **/
  578. static void i40e_update_veb_stats(struct i40e_veb *veb)
  579. {
  580. struct i40e_pf *pf = veb->pf;
  581. struct i40e_hw *hw = &pf->hw;
  582. struct i40e_eth_stats *oes;
  583. struct i40e_eth_stats *es; /* device's eth stats */
  584. struct i40e_veb_tc_stats *veb_oes;
  585. struct i40e_veb_tc_stats *veb_es;
  586. int i, idx = 0;
  587. idx = veb->stats_idx;
  588. es = &veb->stats;
  589. oes = &veb->stats_offsets;
  590. veb_es = &veb->tc_stats;
  591. veb_oes = &veb->tc_stats_offsets;
  592. /* Gather up the stats that the hw collects */
  593. i40e_stat_update32(hw, I40E_GLSW_TDPC(idx),
  594. veb->stat_offsets_loaded,
  595. &oes->tx_discards, &es->tx_discards);
  596. if (hw->revision_id > 0)
  597. i40e_stat_update32(hw, I40E_GLSW_RUPP(idx),
  598. veb->stat_offsets_loaded,
  599. &oes->rx_unknown_protocol,
  600. &es->rx_unknown_protocol);
  601. i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx),
  602. veb->stat_offsets_loaded,
  603. &oes->rx_bytes, &es->rx_bytes);
  604. i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx),
  605. veb->stat_offsets_loaded,
  606. &oes->rx_unicast, &es->rx_unicast);
  607. i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx),
  608. veb->stat_offsets_loaded,
  609. &oes->rx_multicast, &es->rx_multicast);
  610. i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx),
  611. veb->stat_offsets_loaded,
  612. &oes->rx_broadcast, &es->rx_broadcast);
  613. i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx),
  614. veb->stat_offsets_loaded,
  615. &oes->tx_bytes, &es->tx_bytes);
  616. i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx),
  617. veb->stat_offsets_loaded,
  618. &oes->tx_unicast, &es->tx_unicast);
  619. i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx),
  620. veb->stat_offsets_loaded,
  621. &oes->tx_multicast, &es->tx_multicast);
  622. i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx),
  623. veb->stat_offsets_loaded,
  624. &oes->tx_broadcast, &es->tx_broadcast);
  625. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  626. i40e_stat_update48(hw, I40E_GLVEBTC_RPCH(i, idx),
  627. I40E_GLVEBTC_RPCL(i, idx),
  628. veb->stat_offsets_loaded,
  629. &veb_oes->tc_rx_packets[i],
  630. &veb_es->tc_rx_packets[i]);
  631. i40e_stat_update48(hw, I40E_GLVEBTC_RBCH(i, idx),
  632. I40E_GLVEBTC_RBCL(i, idx),
  633. veb->stat_offsets_loaded,
  634. &veb_oes->tc_rx_bytes[i],
  635. &veb_es->tc_rx_bytes[i]);
  636. i40e_stat_update48(hw, I40E_GLVEBTC_TPCH(i, idx),
  637. I40E_GLVEBTC_TPCL(i, idx),
  638. veb->stat_offsets_loaded,
  639. &veb_oes->tc_tx_packets[i],
  640. &veb_es->tc_tx_packets[i]);
  641. i40e_stat_update48(hw, I40E_GLVEBTC_TBCH(i, idx),
  642. I40E_GLVEBTC_TBCL(i, idx),
  643. veb->stat_offsets_loaded,
  644. &veb_oes->tc_tx_bytes[i],
  645. &veb_es->tc_tx_bytes[i]);
  646. }
  647. veb->stat_offsets_loaded = true;
  648. }
  649. /**
  650. * i40e_update_vsi_stats - Update the vsi statistics counters.
  651. * @vsi: the VSI to be updated
  652. *
  653. * There are a few instances where we store the same stat in a
  654. * couple of different structs. This is partly because we have
  655. * the netdev stats that need to be filled out, which is slightly
  656. * different from the "eth_stats" defined by the chip and used in
  657. * VF communications. We sort it out here.
  658. **/
  659. static void i40e_update_vsi_stats(struct i40e_vsi *vsi)
  660. {
  661. struct i40e_pf *pf = vsi->back;
  662. struct rtnl_link_stats64 *ons;
  663. struct rtnl_link_stats64 *ns; /* netdev stats */
  664. struct i40e_eth_stats *oes;
  665. struct i40e_eth_stats *es; /* device's eth stats */
  666. u32 tx_restart, tx_busy;
  667. struct i40e_ring *p;
  668. u32 rx_page, rx_buf;
  669. u64 bytes, packets;
  670. unsigned int start;
  671. u64 tx_linearize;
  672. u64 tx_force_wb;
  673. u64 rx_p, rx_b;
  674. u64 tx_p, tx_b;
  675. u16 q;
  676. if (test_bit(__I40E_DOWN, &vsi->state) ||
  677. test_bit(__I40E_CONFIG_BUSY, &pf->state))
  678. return;
  679. ns = i40e_get_vsi_stats_struct(vsi);
  680. ons = &vsi->net_stats_offsets;
  681. es = &vsi->eth_stats;
  682. oes = &vsi->eth_stats_offsets;
  683. /* Gather up the netdev and vsi stats that the driver collects
  684. * on the fly during packet processing
  685. */
  686. rx_b = rx_p = 0;
  687. tx_b = tx_p = 0;
  688. tx_restart = tx_busy = tx_linearize = tx_force_wb = 0;
  689. rx_page = 0;
  690. rx_buf = 0;
  691. rcu_read_lock();
  692. for (q = 0; q < vsi->num_queue_pairs; q++) {
  693. /* locate Tx ring */
  694. p = ACCESS_ONCE(vsi->tx_rings[q]);
  695. do {
  696. start = u64_stats_fetch_begin_irq(&p->syncp);
  697. packets = p->stats.packets;
  698. bytes = p->stats.bytes;
  699. } while (u64_stats_fetch_retry_irq(&p->syncp, start));
  700. tx_b += bytes;
  701. tx_p += packets;
  702. tx_restart += p->tx_stats.restart_queue;
  703. tx_busy += p->tx_stats.tx_busy;
  704. tx_linearize += p->tx_stats.tx_linearize;
  705. tx_force_wb += p->tx_stats.tx_force_wb;
  706. /* Rx queue is part of the same block as Tx queue */
  707. p = &p[1];
  708. do {
  709. start = u64_stats_fetch_begin_irq(&p->syncp);
  710. packets = p->stats.packets;
  711. bytes = p->stats.bytes;
  712. } while (u64_stats_fetch_retry_irq(&p->syncp, start));
  713. rx_b += bytes;
  714. rx_p += packets;
  715. rx_buf += p->rx_stats.alloc_buff_failed;
  716. rx_page += p->rx_stats.alloc_page_failed;
  717. }
  718. rcu_read_unlock();
  719. vsi->tx_restart = tx_restart;
  720. vsi->tx_busy = tx_busy;
  721. vsi->tx_linearize = tx_linearize;
  722. vsi->tx_force_wb = tx_force_wb;
  723. vsi->rx_page_failed = rx_page;
  724. vsi->rx_buf_failed = rx_buf;
  725. ns->rx_packets = rx_p;
  726. ns->rx_bytes = rx_b;
  727. ns->tx_packets = tx_p;
  728. ns->tx_bytes = tx_b;
  729. /* update netdev stats from eth stats */
  730. i40e_update_eth_stats(vsi);
  731. ons->tx_errors = oes->tx_errors;
  732. ns->tx_errors = es->tx_errors;
  733. ons->multicast = oes->rx_multicast;
  734. ns->multicast = es->rx_multicast;
  735. ons->rx_dropped = oes->rx_discards;
  736. ns->rx_dropped = es->rx_discards;
  737. ons->tx_dropped = oes->tx_discards;
  738. ns->tx_dropped = es->tx_discards;
  739. /* pull in a couple PF stats if this is the main vsi */
  740. if (vsi == pf->vsi[pf->lan_vsi]) {
  741. ns->rx_crc_errors = pf->stats.crc_errors;
  742. ns->rx_errors = pf->stats.crc_errors + pf->stats.illegal_bytes;
  743. ns->rx_length_errors = pf->stats.rx_length_errors;
  744. }
  745. }
  746. /**
  747. * i40e_update_pf_stats - Update the PF statistics counters.
  748. * @pf: the PF to be updated
  749. **/
  750. static void i40e_update_pf_stats(struct i40e_pf *pf)
  751. {
  752. struct i40e_hw_port_stats *osd = &pf->stats_offsets;
  753. struct i40e_hw_port_stats *nsd = &pf->stats;
  754. struct i40e_hw *hw = &pf->hw;
  755. u32 val;
  756. int i;
  757. i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
  758. I40E_GLPRT_GORCL(hw->port),
  759. pf->stat_offsets_loaded,
  760. &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
  761. i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
  762. I40E_GLPRT_GOTCL(hw->port),
  763. pf->stat_offsets_loaded,
  764. &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
  765. i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
  766. pf->stat_offsets_loaded,
  767. &osd->eth.rx_discards,
  768. &nsd->eth.rx_discards);
  769. i40e_stat_update48(hw, I40E_GLPRT_UPRCH(hw->port),
  770. I40E_GLPRT_UPRCL(hw->port),
  771. pf->stat_offsets_loaded,
  772. &osd->eth.rx_unicast,
  773. &nsd->eth.rx_unicast);
  774. i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
  775. I40E_GLPRT_MPRCL(hw->port),
  776. pf->stat_offsets_loaded,
  777. &osd->eth.rx_multicast,
  778. &nsd->eth.rx_multicast);
  779. i40e_stat_update48(hw, I40E_GLPRT_BPRCH(hw->port),
  780. I40E_GLPRT_BPRCL(hw->port),
  781. pf->stat_offsets_loaded,
  782. &osd->eth.rx_broadcast,
  783. &nsd->eth.rx_broadcast);
  784. i40e_stat_update48(hw, I40E_GLPRT_UPTCH(hw->port),
  785. I40E_GLPRT_UPTCL(hw->port),
  786. pf->stat_offsets_loaded,
  787. &osd->eth.tx_unicast,
  788. &nsd->eth.tx_unicast);
  789. i40e_stat_update48(hw, I40E_GLPRT_MPTCH(hw->port),
  790. I40E_GLPRT_MPTCL(hw->port),
  791. pf->stat_offsets_loaded,
  792. &osd->eth.tx_multicast,
  793. &nsd->eth.tx_multicast);
  794. i40e_stat_update48(hw, I40E_GLPRT_BPTCH(hw->port),
  795. I40E_GLPRT_BPTCL(hw->port),
  796. pf->stat_offsets_loaded,
  797. &osd->eth.tx_broadcast,
  798. &nsd->eth.tx_broadcast);
  799. i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
  800. pf->stat_offsets_loaded,
  801. &osd->tx_dropped_link_down,
  802. &nsd->tx_dropped_link_down);
  803. i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
  804. pf->stat_offsets_loaded,
  805. &osd->crc_errors, &nsd->crc_errors);
  806. i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
  807. pf->stat_offsets_loaded,
  808. &osd->illegal_bytes, &nsd->illegal_bytes);
  809. i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
  810. pf->stat_offsets_loaded,
  811. &osd->mac_local_faults,
  812. &nsd->mac_local_faults);
  813. i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
  814. pf->stat_offsets_loaded,
  815. &osd->mac_remote_faults,
  816. &nsd->mac_remote_faults);
  817. i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
  818. pf->stat_offsets_loaded,
  819. &osd->rx_length_errors,
  820. &nsd->rx_length_errors);
  821. i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
  822. pf->stat_offsets_loaded,
  823. &osd->link_xon_rx, &nsd->link_xon_rx);
  824. i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
  825. pf->stat_offsets_loaded,
  826. &osd->link_xon_tx, &nsd->link_xon_tx);
  827. i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
  828. pf->stat_offsets_loaded,
  829. &osd->link_xoff_rx, &nsd->link_xoff_rx);
  830. i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
  831. pf->stat_offsets_loaded,
  832. &osd->link_xoff_tx, &nsd->link_xoff_tx);
  833. for (i = 0; i < 8; i++) {
  834. i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
  835. pf->stat_offsets_loaded,
  836. &osd->priority_xoff_rx[i],
  837. &nsd->priority_xoff_rx[i]);
  838. i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
  839. pf->stat_offsets_loaded,
  840. &osd->priority_xon_rx[i],
  841. &nsd->priority_xon_rx[i]);
  842. i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
  843. pf->stat_offsets_loaded,
  844. &osd->priority_xon_tx[i],
  845. &nsd->priority_xon_tx[i]);
  846. i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
  847. pf->stat_offsets_loaded,
  848. &osd->priority_xoff_tx[i],
  849. &nsd->priority_xoff_tx[i]);
  850. i40e_stat_update32(hw,
  851. I40E_GLPRT_RXON2OFFCNT(hw->port, i),
  852. pf->stat_offsets_loaded,
  853. &osd->priority_xon_2_xoff[i],
  854. &nsd->priority_xon_2_xoff[i]);
  855. }
  856. i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
  857. I40E_GLPRT_PRC64L(hw->port),
  858. pf->stat_offsets_loaded,
  859. &osd->rx_size_64, &nsd->rx_size_64);
  860. i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
  861. I40E_GLPRT_PRC127L(hw->port),
  862. pf->stat_offsets_loaded,
  863. &osd->rx_size_127, &nsd->rx_size_127);
  864. i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
  865. I40E_GLPRT_PRC255L(hw->port),
  866. pf->stat_offsets_loaded,
  867. &osd->rx_size_255, &nsd->rx_size_255);
  868. i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
  869. I40E_GLPRT_PRC511L(hw->port),
  870. pf->stat_offsets_loaded,
  871. &osd->rx_size_511, &nsd->rx_size_511);
  872. i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
  873. I40E_GLPRT_PRC1023L(hw->port),
  874. pf->stat_offsets_loaded,
  875. &osd->rx_size_1023, &nsd->rx_size_1023);
  876. i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
  877. I40E_GLPRT_PRC1522L(hw->port),
  878. pf->stat_offsets_loaded,
  879. &osd->rx_size_1522, &nsd->rx_size_1522);
  880. i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
  881. I40E_GLPRT_PRC9522L(hw->port),
  882. pf->stat_offsets_loaded,
  883. &osd->rx_size_big, &nsd->rx_size_big);
  884. i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
  885. I40E_GLPRT_PTC64L(hw->port),
  886. pf->stat_offsets_loaded,
  887. &osd->tx_size_64, &nsd->tx_size_64);
  888. i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
  889. I40E_GLPRT_PTC127L(hw->port),
  890. pf->stat_offsets_loaded,
  891. &osd->tx_size_127, &nsd->tx_size_127);
  892. i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
  893. I40E_GLPRT_PTC255L(hw->port),
  894. pf->stat_offsets_loaded,
  895. &osd->tx_size_255, &nsd->tx_size_255);
  896. i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
  897. I40E_GLPRT_PTC511L(hw->port),
  898. pf->stat_offsets_loaded,
  899. &osd->tx_size_511, &nsd->tx_size_511);
  900. i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
  901. I40E_GLPRT_PTC1023L(hw->port),
  902. pf->stat_offsets_loaded,
  903. &osd->tx_size_1023, &nsd->tx_size_1023);
  904. i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
  905. I40E_GLPRT_PTC1522L(hw->port),
  906. pf->stat_offsets_loaded,
  907. &osd->tx_size_1522, &nsd->tx_size_1522);
  908. i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
  909. I40E_GLPRT_PTC9522L(hw->port),
  910. pf->stat_offsets_loaded,
  911. &osd->tx_size_big, &nsd->tx_size_big);
  912. i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
  913. pf->stat_offsets_loaded,
  914. &osd->rx_undersize, &nsd->rx_undersize);
  915. i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
  916. pf->stat_offsets_loaded,
  917. &osd->rx_fragments, &nsd->rx_fragments);
  918. i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
  919. pf->stat_offsets_loaded,
  920. &osd->rx_oversize, &nsd->rx_oversize);
  921. i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
  922. pf->stat_offsets_loaded,
  923. &osd->rx_jabber, &nsd->rx_jabber);
  924. /* FDIR stats */
  925. i40e_stat_update32(hw,
  926. I40E_GLQF_PCNT(I40E_FD_ATR_STAT_IDX(pf->hw.pf_id)),
  927. pf->stat_offsets_loaded,
  928. &osd->fd_atr_match, &nsd->fd_atr_match);
  929. i40e_stat_update32(hw,
  930. I40E_GLQF_PCNT(I40E_FD_SB_STAT_IDX(pf->hw.pf_id)),
  931. pf->stat_offsets_loaded,
  932. &osd->fd_sb_match, &nsd->fd_sb_match);
  933. i40e_stat_update32(hw,
  934. I40E_GLQF_PCNT(I40E_FD_ATR_TUNNEL_STAT_IDX(pf->hw.pf_id)),
  935. pf->stat_offsets_loaded,
  936. &osd->fd_atr_tunnel_match, &nsd->fd_atr_tunnel_match);
  937. val = rd32(hw, I40E_PRTPM_EEE_STAT);
  938. nsd->tx_lpi_status =
  939. (val & I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_MASK) >>
  940. I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_SHIFT;
  941. nsd->rx_lpi_status =
  942. (val & I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_MASK) >>
  943. I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_SHIFT;
  944. i40e_stat_update32(hw, I40E_PRTPM_TLPIC,
  945. pf->stat_offsets_loaded,
  946. &osd->tx_lpi_count, &nsd->tx_lpi_count);
  947. i40e_stat_update32(hw, I40E_PRTPM_RLPIC,
  948. pf->stat_offsets_loaded,
  949. &osd->rx_lpi_count, &nsd->rx_lpi_count);
  950. if (pf->flags & I40E_FLAG_FD_SB_ENABLED &&
  951. !(pf->hw_disabled_flags & I40E_FLAG_FD_SB_ENABLED))
  952. nsd->fd_sb_status = true;
  953. else
  954. nsd->fd_sb_status = false;
  955. if (pf->flags & I40E_FLAG_FD_ATR_ENABLED &&
  956. !(pf->hw_disabled_flags & I40E_FLAG_FD_ATR_ENABLED))
  957. nsd->fd_atr_status = true;
  958. else
  959. nsd->fd_atr_status = false;
  960. pf->stat_offsets_loaded = true;
  961. }
  962. /**
  963. * i40e_update_stats - Update the various statistics counters.
  964. * @vsi: the VSI to be updated
  965. *
  966. * Update the various stats for this VSI and its related entities.
  967. **/
  968. void i40e_update_stats(struct i40e_vsi *vsi)
  969. {
  970. struct i40e_pf *pf = vsi->back;
  971. if (vsi == pf->vsi[pf->lan_vsi])
  972. i40e_update_pf_stats(pf);
  973. i40e_update_vsi_stats(vsi);
  974. }
  975. /**
  976. * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
  977. * @vsi: the VSI to be searched
  978. * @macaddr: the MAC address
  979. * @vlan: the vlan
  980. *
  981. * Returns ptr to the filter object or NULL
  982. **/
  983. static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
  984. const u8 *macaddr, s16 vlan)
  985. {
  986. struct i40e_mac_filter *f;
  987. u64 key;
  988. if (!vsi || !macaddr)
  989. return NULL;
  990. key = i40e_addr_to_hkey(macaddr);
  991. hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
  992. if ((ether_addr_equal(macaddr, f->macaddr)) &&
  993. (vlan == f->vlan))
  994. return f;
  995. }
  996. return NULL;
  997. }
  998. /**
  999. * i40e_find_mac - Find a mac addr in the macvlan filters list
  1000. * @vsi: the VSI to be searched
  1001. * @macaddr: the MAC address we are searching for
  1002. *
  1003. * Returns the first filter with the provided MAC address or NULL if
  1004. * MAC address was not found
  1005. **/
  1006. struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, const u8 *macaddr)
  1007. {
  1008. struct i40e_mac_filter *f;
  1009. u64 key;
  1010. if (!vsi || !macaddr)
  1011. return NULL;
  1012. key = i40e_addr_to_hkey(macaddr);
  1013. hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
  1014. if ((ether_addr_equal(macaddr, f->macaddr)))
  1015. return f;
  1016. }
  1017. return NULL;
  1018. }
  1019. /**
  1020. * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
  1021. * @vsi: the VSI to be searched
  1022. *
  1023. * Returns true if VSI is in vlan mode or false otherwise
  1024. **/
  1025. bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
  1026. {
  1027. /* If we have a PVID, always operate in VLAN mode */
  1028. if (vsi->info.pvid)
  1029. return true;
  1030. /* We need to operate in VLAN mode whenever we have any filters with
  1031. * a VLAN other than I40E_VLAN_ALL. We could check the table each
  1032. * time, incurring search cost repeatedly. However, we can notice two
  1033. * things:
  1034. *
  1035. * 1) the only place where we can gain a VLAN filter is in
  1036. * i40e_add_filter.
  1037. *
  1038. * 2) the only place where filters are actually removed is in
  1039. * i40e_sync_filters_subtask.
  1040. *
  1041. * Thus, we can simply use a boolean value, has_vlan_filters which we
  1042. * will set to true when we add a VLAN filter in i40e_add_filter. Then
  1043. * we have to perform the full search after deleting filters in
  1044. * i40e_sync_filters_subtask, but we already have to search
  1045. * filters here and can perform the check at the same time. This
  1046. * results in avoiding embedding a loop for VLAN mode inside another
  1047. * loop over all the filters, and should maintain correctness as noted
  1048. * above.
  1049. */
  1050. return vsi->has_vlan_filter;
  1051. }
  1052. /**
  1053. * i40e_correct_mac_vlan_filters - Correct non-VLAN filters if necessary
  1054. * @vsi: the VSI to configure
  1055. * @tmp_add_list: list of filters ready to be added
  1056. * @tmp_del_list: list of filters ready to be deleted
  1057. * @vlan_filters: the number of active VLAN filters
  1058. *
  1059. * Update VLAN=0 and VLAN=-1 (I40E_VLAN_ANY) filters properly so that they
  1060. * behave as expected. If we have any active VLAN filters remaining or about
  1061. * to be added then we need to update non-VLAN filters to be marked as VLAN=0
  1062. * so that they only match against untagged traffic. If we no longer have any
  1063. * active VLAN filters, we need to make all non-VLAN filters marked as VLAN=-1
  1064. * so that they match against both tagged and untagged traffic. In this way,
  1065. * we ensure that we correctly receive the desired traffic. This ensures that
  1066. * when we have an active VLAN we will receive only untagged traffic and
  1067. * traffic matching active VLANs. If we have no active VLANs then we will
  1068. * operate in non-VLAN mode and receive all traffic, tagged or untagged.
  1069. *
  1070. * Finally, in a similar fashion, this function also corrects filters when
  1071. * there is an active PVID assigned to this VSI.
  1072. *
  1073. * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
  1074. *
  1075. * This function is only expected to be called from within
  1076. * i40e_sync_vsi_filters.
  1077. *
  1078. * NOTE: This function expects to be called while under the
  1079. * mac_filter_hash_lock
  1080. */
  1081. static int i40e_correct_mac_vlan_filters(struct i40e_vsi *vsi,
  1082. struct hlist_head *tmp_add_list,
  1083. struct hlist_head *tmp_del_list,
  1084. int vlan_filters)
  1085. {
  1086. s16 pvid = le16_to_cpu(vsi->info.pvid);
  1087. struct i40e_mac_filter *f, *add_head;
  1088. struct i40e_new_mac_filter *new;
  1089. struct hlist_node *h;
  1090. int bkt, new_vlan;
  1091. /* To determine if a particular filter needs to be replaced we
  1092. * have the three following conditions:
  1093. *
  1094. * a) if we have a PVID assigned, then all filters which are
  1095. * not marked as VLAN=PVID must be replaced with filters that
  1096. * are.
  1097. * b) otherwise, if we have any active VLANS, all filters
  1098. * which are marked as VLAN=-1 must be replaced with
  1099. * filters marked as VLAN=0
  1100. * c) finally, if we do not have any active VLANS, all filters
  1101. * which are marked as VLAN=0 must be replaced with filters
  1102. * marked as VLAN=-1
  1103. */
  1104. /* Update the filters about to be added in place */
  1105. hlist_for_each_entry(new, tmp_add_list, hlist) {
  1106. if (pvid && new->f->vlan != pvid)
  1107. new->f->vlan = pvid;
  1108. else if (vlan_filters && new->f->vlan == I40E_VLAN_ANY)
  1109. new->f->vlan = 0;
  1110. else if (!vlan_filters && new->f->vlan == 0)
  1111. new->f->vlan = I40E_VLAN_ANY;
  1112. }
  1113. /* Update the remaining active filters */
  1114. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  1115. /* Combine the checks for whether a filter needs to be changed
  1116. * and then determine the new VLAN inside the if block, in
  1117. * order to avoid duplicating code for adding the new filter
  1118. * then deleting the old filter.
  1119. */
  1120. if ((pvid && f->vlan != pvid) ||
  1121. (vlan_filters && f->vlan == I40E_VLAN_ANY) ||
  1122. (!vlan_filters && f->vlan == 0)) {
  1123. /* Determine the new vlan we will be adding */
  1124. if (pvid)
  1125. new_vlan = pvid;
  1126. else if (vlan_filters)
  1127. new_vlan = 0;
  1128. else
  1129. new_vlan = I40E_VLAN_ANY;
  1130. /* Create the new filter */
  1131. add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
  1132. if (!add_head)
  1133. return -ENOMEM;
  1134. /* Create a temporary i40e_new_mac_filter */
  1135. new = kzalloc(sizeof(*new), GFP_ATOMIC);
  1136. if (!new)
  1137. return -ENOMEM;
  1138. new->f = add_head;
  1139. new->state = add_head->state;
  1140. /* Add the new filter to the tmp list */
  1141. hlist_add_head(&new->hlist, tmp_add_list);
  1142. /* Put the original filter into the delete list */
  1143. f->state = I40E_FILTER_REMOVE;
  1144. hash_del(&f->hlist);
  1145. hlist_add_head(&f->hlist, tmp_del_list);
  1146. }
  1147. }
  1148. vsi->has_vlan_filter = !!vlan_filters;
  1149. return 0;
  1150. }
  1151. /**
  1152. * i40e_rm_default_mac_filter - Remove the default MAC filter set by NVM
  1153. * @vsi: the PF Main VSI - inappropriate for any other VSI
  1154. * @macaddr: the MAC address
  1155. *
  1156. * Remove whatever filter the firmware set up so the driver can manage
  1157. * its own filtering intelligently.
  1158. **/
  1159. static void i40e_rm_default_mac_filter(struct i40e_vsi *vsi, u8 *macaddr)
  1160. {
  1161. struct i40e_aqc_remove_macvlan_element_data element;
  1162. struct i40e_pf *pf = vsi->back;
  1163. /* Only appropriate for the PF main VSI */
  1164. if (vsi->type != I40E_VSI_MAIN)
  1165. return;
  1166. memset(&element, 0, sizeof(element));
  1167. ether_addr_copy(element.mac_addr, macaddr);
  1168. element.vlan_tag = 0;
  1169. /* Ignore error returns, some firmware does it this way... */
  1170. element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
  1171. i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
  1172. memset(&element, 0, sizeof(element));
  1173. ether_addr_copy(element.mac_addr, macaddr);
  1174. element.vlan_tag = 0;
  1175. /* ...and some firmware does it this way. */
  1176. element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH |
  1177. I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
  1178. i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
  1179. }
  1180. /**
  1181. * i40e_add_filter - Add a mac/vlan filter to the VSI
  1182. * @vsi: the VSI to be searched
  1183. * @macaddr: the MAC address
  1184. * @vlan: the vlan
  1185. *
  1186. * Returns ptr to the filter object or NULL when no memory available.
  1187. *
  1188. * NOTE: This function is expected to be called with mac_filter_hash_lock
  1189. * being held.
  1190. **/
  1191. struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
  1192. const u8 *macaddr, s16 vlan)
  1193. {
  1194. struct i40e_mac_filter *f;
  1195. u64 key;
  1196. if (!vsi || !macaddr)
  1197. return NULL;
  1198. f = i40e_find_filter(vsi, macaddr, vlan);
  1199. if (!f) {
  1200. f = kzalloc(sizeof(*f), GFP_ATOMIC);
  1201. if (!f)
  1202. return NULL;
  1203. /* Update the boolean indicating if we need to function in
  1204. * VLAN mode.
  1205. */
  1206. if (vlan >= 0)
  1207. vsi->has_vlan_filter = true;
  1208. ether_addr_copy(f->macaddr, macaddr);
  1209. f->vlan = vlan;
  1210. /* If we're in overflow promisc mode, set the state directly
  1211. * to failed, so we don't bother to try sending the filter
  1212. * to the hardware.
  1213. */
  1214. if (test_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state))
  1215. f->state = I40E_FILTER_FAILED;
  1216. else
  1217. f->state = I40E_FILTER_NEW;
  1218. INIT_HLIST_NODE(&f->hlist);
  1219. key = i40e_addr_to_hkey(macaddr);
  1220. hash_add(vsi->mac_filter_hash, &f->hlist, key);
  1221. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  1222. vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
  1223. }
  1224. /* If we're asked to add a filter that has been marked for removal, it
  1225. * is safe to simply restore it to active state. __i40e_del_filter
  1226. * will have simply deleted any filters which were previously marked
  1227. * NEW or FAILED, so if it is currently marked REMOVE it must have
  1228. * previously been ACTIVE. Since we haven't yet run the sync filters
  1229. * task, just restore this filter to the ACTIVE state so that the
  1230. * sync task leaves it in place
  1231. */
  1232. if (f->state == I40E_FILTER_REMOVE)
  1233. f->state = I40E_FILTER_ACTIVE;
  1234. return f;
  1235. }
  1236. /**
  1237. * __i40e_del_filter - Remove a specific filter from the VSI
  1238. * @vsi: VSI to remove from
  1239. * @f: the filter to remove from the list
  1240. *
  1241. * This function should be called instead of i40e_del_filter only if you know
  1242. * the exact filter you will remove already, such as via i40e_find_filter or
  1243. * i40e_find_mac.
  1244. *
  1245. * NOTE: This function is expected to be called with mac_filter_hash_lock
  1246. * being held.
  1247. * ANOTHER NOTE: This function MUST be called from within the context of
  1248. * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
  1249. * instead of list_for_each_entry().
  1250. **/
  1251. void __i40e_del_filter(struct i40e_vsi *vsi, struct i40e_mac_filter *f)
  1252. {
  1253. if (!f)
  1254. return;
  1255. /* If the filter was never added to firmware then we can just delete it
  1256. * directly and we don't want to set the status to remove or else an
  1257. * admin queue command will unnecessarily fire.
  1258. */
  1259. if ((f->state == I40E_FILTER_FAILED) ||
  1260. (f->state == I40E_FILTER_NEW)) {
  1261. hash_del(&f->hlist);
  1262. kfree(f);
  1263. } else {
  1264. f->state = I40E_FILTER_REMOVE;
  1265. }
  1266. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  1267. vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
  1268. }
  1269. /**
  1270. * i40e_del_filter - Remove a MAC/VLAN filter from the VSI
  1271. * @vsi: the VSI to be searched
  1272. * @macaddr: the MAC address
  1273. * @vlan: the VLAN
  1274. *
  1275. * NOTE: This function is expected to be called with mac_filter_hash_lock
  1276. * being held.
  1277. * ANOTHER NOTE: This function MUST be called from within the context of
  1278. * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
  1279. * instead of list_for_each_entry().
  1280. **/
  1281. void i40e_del_filter(struct i40e_vsi *vsi, const u8 *macaddr, s16 vlan)
  1282. {
  1283. struct i40e_mac_filter *f;
  1284. if (!vsi || !macaddr)
  1285. return;
  1286. f = i40e_find_filter(vsi, macaddr, vlan);
  1287. __i40e_del_filter(vsi, f);
  1288. }
  1289. /**
  1290. * i40e_add_mac_filter - Add a MAC filter for all active VLANs
  1291. * @vsi: the VSI to be searched
  1292. * @macaddr: the mac address to be filtered
  1293. *
  1294. * If we're not in VLAN mode, just add the filter to I40E_VLAN_ANY. Otherwise,
  1295. * go through all the macvlan filters and add a macvlan filter for each
  1296. * unique vlan that already exists. If a PVID has been assigned, instead only
  1297. * add the macaddr to that VLAN.
  1298. *
  1299. * Returns last filter added on success, else NULL
  1300. **/
  1301. struct i40e_mac_filter *i40e_add_mac_filter(struct i40e_vsi *vsi,
  1302. const u8 *macaddr)
  1303. {
  1304. struct i40e_mac_filter *f, *add = NULL;
  1305. struct hlist_node *h;
  1306. int bkt;
  1307. if (vsi->info.pvid)
  1308. return i40e_add_filter(vsi, macaddr,
  1309. le16_to_cpu(vsi->info.pvid));
  1310. if (!i40e_is_vsi_in_vlan(vsi))
  1311. return i40e_add_filter(vsi, macaddr, I40E_VLAN_ANY);
  1312. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  1313. if (f->state == I40E_FILTER_REMOVE)
  1314. continue;
  1315. add = i40e_add_filter(vsi, macaddr, f->vlan);
  1316. if (!add)
  1317. return NULL;
  1318. }
  1319. return add;
  1320. }
  1321. /**
  1322. * i40e_del_mac_filter - Remove a MAC filter from all VLANs
  1323. * @vsi: the VSI to be searched
  1324. * @macaddr: the mac address to be removed
  1325. *
  1326. * Removes a given MAC address from a VSI regardless of what VLAN it has been
  1327. * associated with.
  1328. *
  1329. * Returns 0 for success, or error
  1330. **/
  1331. int i40e_del_mac_filter(struct i40e_vsi *vsi, const u8 *macaddr)
  1332. {
  1333. struct i40e_mac_filter *f;
  1334. struct hlist_node *h;
  1335. bool found = false;
  1336. int bkt;
  1337. WARN(!spin_is_locked(&vsi->mac_filter_hash_lock),
  1338. "Missing mac_filter_hash_lock\n");
  1339. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  1340. if (ether_addr_equal(macaddr, f->macaddr)) {
  1341. __i40e_del_filter(vsi, f);
  1342. found = true;
  1343. }
  1344. }
  1345. if (found)
  1346. return 0;
  1347. else
  1348. return -ENOENT;
  1349. }
  1350. /**
  1351. * i40e_set_mac - NDO callback to set mac address
  1352. * @netdev: network interface device structure
  1353. * @p: pointer to an address structure
  1354. *
  1355. * Returns 0 on success, negative on failure
  1356. **/
  1357. static int i40e_set_mac(struct net_device *netdev, void *p)
  1358. {
  1359. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1360. struct i40e_vsi *vsi = np->vsi;
  1361. struct i40e_pf *pf = vsi->back;
  1362. struct i40e_hw *hw = &pf->hw;
  1363. struct sockaddr *addr = p;
  1364. if (!is_valid_ether_addr(addr->sa_data))
  1365. return -EADDRNOTAVAIL;
  1366. if (ether_addr_equal(netdev->dev_addr, addr->sa_data)) {
  1367. netdev_info(netdev, "already using mac address %pM\n",
  1368. addr->sa_data);
  1369. return 0;
  1370. }
  1371. if (test_bit(__I40E_DOWN, &vsi->back->state) ||
  1372. test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
  1373. return -EADDRNOTAVAIL;
  1374. if (ether_addr_equal(hw->mac.addr, addr->sa_data))
  1375. netdev_info(netdev, "returning to hw mac address %pM\n",
  1376. hw->mac.addr);
  1377. else
  1378. netdev_info(netdev, "set new mac address %pM\n", addr->sa_data);
  1379. spin_lock_bh(&vsi->mac_filter_hash_lock);
  1380. i40e_del_mac_filter(vsi, netdev->dev_addr);
  1381. i40e_add_mac_filter(vsi, addr->sa_data);
  1382. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  1383. ether_addr_copy(netdev->dev_addr, addr->sa_data);
  1384. if (vsi->type == I40E_VSI_MAIN) {
  1385. i40e_status ret;
  1386. ret = i40e_aq_mac_address_write(&vsi->back->hw,
  1387. I40E_AQC_WRITE_TYPE_LAA_WOL,
  1388. addr->sa_data, NULL);
  1389. if (ret)
  1390. netdev_info(netdev, "Ignoring error from firmware on LAA update, status %s, AQ ret %s\n",
  1391. i40e_stat_str(hw, ret),
  1392. i40e_aq_str(hw, hw->aq.asq_last_status));
  1393. }
  1394. /* schedule our worker thread which will take care of
  1395. * applying the new filter changes
  1396. */
  1397. i40e_service_event_schedule(vsi->back);
  1398. return 0;
  1399. }
  1400. /**
  1401. * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
  1402. * @vsi: the VSI being setup
  1403. * @ctxt: VSI context structure
  1404. * @enabled_tc: Enabled TCs bitmap
  1405. * @is_add: True if called before Add VSI
  1406. *
  1407. * Setup VSI queue mapping for enabled traffic classes.
  1408. **/
  1409. static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
  1410. struct i40e_vsi_context *ctxt,
  1411. u8 enabled_tc,
  1412. bool is_add)
  1413. {
  1414. struct i40e_pf *pf = vsi->back;
  1415. u16 sections = 0;
  1416. u8 netdev_tc = 0;
  1417. u16 numtc = 0;
  1418. u16 qcount;
  1419. u8 offset;
  1420. u16 qmap;
  1421. int i;
  1422. u16 num_tc_qps = 0;
  1423. sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
  1424. offset = 0;
  1425. if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
  1426. /* Find numtc from enabled TC bitmap */
  1427. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  1428. if (enabled_tc & BIT(i)) /* TC is enabled */
  1429. numtc++;
  1430. }
  1431. if (!numtc) {
  1432. dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
  1433. numtc = 1;
  1434. }
  1435. } else {
  1436. /* At least TC0 is enabled in case of non-DCB case */
  1437. numtc = 1;
  1438. }
  1439. vsi->tc_config.numtc = numtc;
  1440. vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
  1441. /* Number of queues per enabled TC */
  1442. qcount = vsi->alloc_queue_pairs;
  1443. num_tc_qps = qcount / numtc;
  1444. num_tc_qps = min_t(int, num_tc_qps, i40e_pf_get_max_q_per_tc(pf));
  1445. /* Setup queue offset/count for all TCs for given VSI */
  1446. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  1447. /* See if the given TC is enabled for the given VSI */
  1448. if (vsi->tc_config.enabled_tc & BIT(i)) {
  1449. /* TC is enabled */
  1450. int pow, num_qps;
  1451. switch (vsi->type) {
  1452. case I40E_VSI_MAIN:
  1453. qcount = min_t(int, pf->alloc_rss_size,
  1454. num_tc_qps);
  1455. break;
  1456. case I40E_VSI_FDIR:
  1457. case I40E_VSI_SRIOV:
  1458. case I40E_VSI_VMDQ2:
  1459. default:
  1460. qcount = num_tc_qps;
  1461. WARN_ON(i != 0);
  1462. break;
  1463. }
  1464. vsi->tc_config.tc_info[i].qoffset = offset;
  1465. vsi->tc_config.tc_info[i].qcount = qcount;
  1466. /* find the next higher power-of-2 of num queue pairs */
  1467. num_qps = qcount;
  1468. pow = 0;
  1469. while (num_qps && (BIT_ULL(pow) < qcount)) {
  1470. pow++;
  1471. num_qps >>= 1;
  1472. }
  1473. vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
  1474. qmap =
  1475. (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
  1476. (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
  1477. offset += qcount;
  1478. } else {
  1479. /* TC is not enabled so set the offset to
  1480. * default queue and allocate one queue
  1481. * for the given TC.
  1482. */
  1483. vsi->tc_config.tc_info[i].qoffset = 0;
  1484. vsi->tc_config.tc_info[i].qcount = 1;
  1485. vsi->tc_config.tc_info[i].netdev_tc = 0;
  1486. qmap = 0;
  1487. }
  1488. ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
  1489. }
  1490. /* Set actual Tx/Rx queue pairs */
  1491. vsi->num_queue_pairs = offset;
  1492. if ((vsi->type == I40E_VSI_MAIN) && (numtc == 1)) {
  1493. if (vsi->req_queue_pairs > 0)
  1494. vsi->num_queue_pairs = vsi->req_queue_pairs;
  1495. else if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  1496. vsi->num_queue_pairs = pf->num_lan_msix;
  1497. }
  1498. /* Scheduler section valid can only be set for ADD VSI */
  1499. if (is_add) {
  1500. sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
  1501. ctxt->info.up_enable_bits = enabled_tc;
  1502. }
  1503. if (vsi->type == I40E_VSI_SRIOV) {
  1504. ctxt->info.mapping_flags |=
  1505. cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
  1506. for (i = 0; i < vsi->num_queue_pairs; i++)
  1507. ctxt->info.queue_mapping[i] =
  1508. cpu_to_le16(vsi->base_queue + i);
  1509. } else {
  1510. ctxt->info.mapping_flags |=
  1511. cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
  1512. ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
  1513. }
  1514. ctxt->info.valid_sections |= cpu_to_le16(sections);
  1515. }
  1516. /**
  1517. * i40e_addr_sync - Callback for dev_(mc|uc)_sync to add address
  1518. * @netdev: the netdevice
  1519. * @addr: address to add
  1520. *
  1521. * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
  1522. * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
  1523. */
  1524. static int i40e_addr_sync(struct net_device *netdev, const u8 *addr)
  1525. {
  1526. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1527. struct i40e_vsi *vsi = np->vsi;
  1528. if (i40e_add_mac_filter(vsi, addr))
  1529. return 0;
  1530. else
  1531. return -ENOMEM;
  1532. }
  1533. /**
  1534. * i40e_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
  1535. * @netdev: the netdevice
  1536. * @addr: address to add
  1537. *
  1538. * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call
  1539. * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
  1540. */
  1541. static int i40e_addr_unsync(struct net_device *netdev, const u8 *addr)
  1542. {
  1543. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1544. struct i40e_vsi *vsi = np->vsi;
  1545. i40e_del_mac_filter(vsi, addr);
  1546. return 0;
  1547. }
  1548. /**
  1549. * i40e_set_rx_mode - NDO callback to set the netdev filters
  1550. * @netdev: network interface device structure
  1551. **/
  1552. static void i40e_set_rx_mode(struct net_device *netdev)
  1553. {
  1554. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1555. struct i40e_vsi *vsi = np->vsi;
  1556. spin_lock_bh(&vsi->mac_filter_hash_lock);
  1557. __dev_uc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
  1558. __dev_mc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
  1559. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  1560. /* check for other flag changes */
  1561. if (vsi->current_netdev_flags != vsi->netdev->flags) {
  1562. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  1563. vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
  1564. }
  1565. /* schedule our worker thread which will take care of
  1566. * applying the new filter changes
  1567. */
  1568. i40e_service_event_schedule(vsi->back);
  1569. }
  1570. /**
  1571. * i40e_undo_del_filter_entries - Undo the changes made to MAC filter entries
  1572. * @vsi: Pointer to VSI struct
  1573. * @from: Pointer to list which contains MAC filter entries - changes to
  1574. * those entries needs to be undone.
  1575. *
  1576. * MAC filter entries from this list were slated for deletion.
  1577. **/
  1578. static void i40e_undo_del_filter_entries(struct i40e_vsi *vsi,
  1579. struct hlist_head *from)
  1580. {
  1581. struct i40e_mac_filter *f;
  1582. struct hlist_node *h;
  1583. hlist_for_each_entry_safe(f, h, from, hlist) {
  1584. u64 key = i40e_addr_to_hkey(f->macaddr);
  1585. /* Move the element back into MAC filter list*/
  1586. hlist_del(&f->hlist);
  1587. hash_add(vsi->mac_filter_hash, &f->hlist, key);
  1588. }
  1589. }
  1590. /**
  1591. * i40e_undo_add_filter_entries - Undo the changes made to MAC filter entries
  1592. * @vsi: Pointer to vsi struct
  1593. * @from: Pointer to list which contains MAC filter entries - changes to
  1594. * those entries needs to be undone.
  1595. *
  1596. * MAC filter entries from this list were slated for addition.
  1597. **/
  1598. static void i40e_undo_add_filter_entries(struct i40e_vsi *vsi,
  1599. struct hlist_head *from)
  1600. {
  1601. struct i40e_new_mac_filter *new;
  1602. struct hlist_node *h;
  1603. hlist_for_each_entry_safe(new, h, from, hlist) {
  1604. /* We can simply free the wrapper structure */
  1605. hlist_del(&new->hlist);
  1606. kfree(new);
  1607. }
  1608. }
  1609. /**
  1610. * i40e_next_entry - Get the next non-broadcast filter from a list
  1611. * @next: pointer to filter in list
  1612. *
  1613. * Returns the next non-broadcast filter in the list. Required so that we
  1614. * ignore broadcast filters within the list, since these are not handled via
  1615. * the normal firmware update path.
  1616. */
  1617. static
  1618. struct i40e_new_mac_filter *i40e_next_filter(struct i40e_new_mac_filter *next)
  1619. {
  1620. hlist_for_each_entry_continue(next, hlist) {
  1621. if (!is_broadcast_ether_addr(next->f->macaddr))
  1622. return next;
  1623. }
  1624. return NULL;
  1625. }
  1626. /**
  1627. * i40e_update_filter_state - Update filter state based on return data
  1628. * from firmware
  1629. * @count: Number of filters added
  1630. * @add_list: return data from fw
  1631. * @head: pointer to first filter in current batch
  1632. *
  1633. * MAC filter entries from list were slated to be added to device. Returns
  1634. * number of successful filters. Note that 0 does NOT mean success!
  1635. **/
  1636. static int
  1637. i40e_update_filter_state(int count,
  1638. struct i40e_aqc_add_macvlan_element_data *add_list,
  1639. struct i40e_new_mac_filter *add_head)
  1640. {
  1641. int retval = 0;
  1642. int i;
  1643. for (i = 0; i < count; i++) {
  1644. /* Always check status of each filter. We don't need to check
  1645. * the firmware return status because we pre-set the filter
  1646. * status to I40E_AQC_MM_ERR_NO_RES when sending the filter
  1647. * request to the adminq. Thus, if it no longer matches then
  1648. * we know the filter is active.
  1649. */
  1650. if (add_list[i].match_method == I40E_AQC_MM_ERR_NO_RES) {
  1651. add_head->state = I40E_FILTER_FAILED;
  1652. } else {
  1653. add_head->state = I40E_FILTER_ACTIVE;
  1654. retval++;
  1655. }
  1656. add_head = i40e_next_filter(add_head);
  1657. if (!add_head)
  1658. break;
  1659. }
  1660. return retval;
  1661. }
  1662. /**
  1663. * i40e_aqc_del_filters - Request firmware to delete a set of filters
  1664. * @vsi: ptr to the VSI
  1665. * @vsi_name: name to display in messages
  1666. * @list: the list of filters to send to firmware
  1667. * @num_del: the number of filters to delete
  1668. * @retval: Set to -EIO on failure to delete
  1669. *
  1670. * Send a request to firmware via AdminQ to delete a set of filters. Uses
  1671. * *retval instead of a return value so that success does not force ret_val to
  1672. * be set to 0. This ensures that a sequence of calls to this function
  1673. * preserve the previous value of *retval on successful delete.
  1674. */
  1675. static
  1676. void i40e_aqc_del_filters(struct i40e_vsi *vsi, const char *vsi_name,
  1677. struct i40e_aqc_remove_macvlan_element_data *list,
  1678. int num_del, int *retval)
  1679. {
  1680. struct i40e_hw *hw = &vsi->back->hw;
  1681. i40e_status aq_ret;
  1682. int aq_err;
  1683. aq_ret = i40e_aq_remove_macvlan(hw, vsi->seid, list, num_del, NULL);
  1684. aq_err = hw->aq.asq_last_status;
  1685. /* Explicitly ignore and do not report when firmware returns ENOENT */
  1686. if (aq_ret && !(aq_err == I40E_AQ_RC_ENOENT)) {
  1687. *retval = -EIO;
  1688. dev_info(&vsi->back->pdev->dev,
  1689. "ignoring delete macvlan error on %s, err %s, aq_err %s\n",
  1690. vsi_name, i40e_stat_str(hw, aq_ret),
  1691. i40e_aq_str(hw, aq_err));
  1692. }
  1693. }
  1694. /**
  1695. * i40e_aqc_add_filters - Request firmware to add a set of filters
  1696. * @vsi: ptr to the VSI
  1697. * @vsi_name: name to display in messages
  1698. * @list: the list of filters to send to firmware
  1699. * @add_head: Position in the add hlist
  1700. * @num_add: the number of filters to add
  1701. * @promisc_change: set to true on exit if promiscuous mode was forced on
  1702. *
  1703. * Send a request to firmware via AdminQ to add a chunk of filters. Will set
  1704. * promisc_changed to true if the firmware has run out of space for more
  1705. * filters.
  1706. */
  1707. static
  1708. void i40e_aqc_add_filters(struct i40e_vsi *vsi, const char *vsi_name,
  1709. struct i40e_aqc_add_macvlan_element_data *list,
  1710. struct i40e_new_mac_filter *add_head,
  1711. int num_add, bool *promisc_changed)
  1712. {
  1713. struct i40e_hw *hw = &vsi->back->hw;
  1714. int aq_err, fcnt;
  1715. i40e_aq_add_macvlan(hw, vsi->seid, list, num_add, NULL);
  1716. aq_err = hw->aq.asq_last_status;
  1717. fcnt = i40e_update_filter_state(num_add, list, add_head);
  1718. if (fcnt != num_add) {
  1719. *promisc_changed = true;
  1720. set_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state);
  1721. dev_warn(&vsi->back->pdev->dev,
  1722. "Error %s adding RX filters on %s, promiscuous mode forced on\n",
  1723. i40e_aq_str(hw, aq_err),
  1724. vsi_name);
  1725. }
  1726. }
  1727. /**
  1728. * i40e_aqc_broadcast_filter - Set promiscuous broadcast flags
  1729. * @vsi: pointer to the VSI
  1730. * @f: filter data
  1731. *
  1732. * This function sets or clears the promiscuous broadcast flags for VLAN
  1733. * filters in order to properly receive broadcast frames. Assumes that only
  1734. * broadcast filters are passed.
  1735. *
  1736. * Returns status indicating success or failure;
  1737. **/
  1738. static i40e_status
  1739. i40e_aqc_broadcast_filter(struct i40e_vsi *vsi, const char *vsi_name,
  1740. struct i40e_mac_filter *f)
  1741. {
  1742. bool enable = f->state == I40E_FILTER_NEW;
  1743. struct i40e_hw *hw = &vsi->back->hw;
  1744. i40e_status aq_ret;
  1745. if (f->vlan == I40E_VLAN_ANY) {
  1746. aq_ret = i40e_aq_set_vsi_broadcast(hw,
  1747. vsi->seid,
  1748. enable,
  1749. NULL);
  1750. } else {
  1751. aq_ret = i40e_aq_set_vsi_bc_promisc_on_vlan(hw,
  1752. vsi->seid,
  1753. enable,
  1754. f->vlan,
  1755. NULL);
  1756. }
  1757. if (aq_ret)
  1758. dev_warn(&vsi->back->pdev->dev,
  1759. "Error %s setting broadcast promiscuous mode on %s\n",
  1760. i40e_aq_str(hw, hw->aq.asq_last_status),
  1761. vsi_name);
  1762. return aq_ret;
  1763. }
  1764. /**
  1765. * i40e_sync_vsi_filters - Update the VSI filter list to the HW
  1766. * @vsi: ptr to the VSI
  1767. *
  1768. * Push any outstanding VSI filter changes through the AdminQ.
  1769. *
  1770. * Returns 0 or error value
  1771. **/
  1772. int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
  1773. {
  1774. struct hlist_head tmp_add_list, tmp_del_list;
  1775. struct i40e_mac_filter *f;
  1776. struct i40e_new_mac_filter *new, *add_head = NULL;
  1777. struct i40e_hw *hw = &vsi->back->hw;
  1778. unsigned int failed_filters = 0;
  1779. unsigned int vlan_filters = 0;
  1780. bool promisc_changed = false;
  1781. char vsi_name[16] = "PF";
  1782. int filter_list_len = 0;
  1783. i40e_status aq_ret = 0;
  1784. u32 changed_flags = 0;
  1785. struct hlist_node *h;
  1786. struct i40e_pf *pf;
  1787. int num_add = 0;
  1788. int num_del = 0;
  1789. int retval = 0;
  1790. u16 cmd_flags;
  1791. int list_size;
  1792. int bkt;
  1793. /* empty array typed pointers, kcalloc later */
  1794. struct i40e_aqc_add_macvlan_element_data *add_list;
  1795. struct i40e_aqc_remove_macvlan_element_data *del_list;
  1796. while (test_and_set_bit(__I40E_CONFIG_BUSY, &vsi->state))
  1797. usleep_range(1000, 2000);
  1798. pf = vsi->back;
  1799. if (vsi->netdev) {
  1800. changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
  1801. vsi->current_netdev_flags = vsi->netdev->flags;
  1802. }
  1803. INIT_HLIST_HEAD(&tmp_add_list);
  1804. INIT_HLIST_HEAD(&tmp_del_list);
  1805. if (vsi->type == I40E_VSI_SRIOV)
  1806. snprintf(vsi_name, sizeof(vsi_name) - 1, "VF %d", vsi->vf_id);
  1807. else if (vsi->type != I40E_VSI_MAIN)
  1808. snprintf(vsi_name, sizeof(vsi_name) - 1, "vsi %d", vsi->seid);
  1809. if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
  1810. vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
  1811. spin_lock_bh(&vsi->mac_filter_hash_lock);
  1812. /* Create a list of filters to delete. */
  1813. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  1814. if (f->state == I40E_FILTER_REMOVE) {
  1815. /* Move the element into temporary del_list */
  1816. hash_del(&f->hlist);
  1817. hlist_add_head(&f->hlist, &tmp_del_list);
  1818. /* Avoid counting removed filters */
  1819. continue;
  1820. }
  1821. if (f->state == I40E_FILTER_NEW) {
  1822. /* Create a temporary i40e_new_mac_filter */
  1823. new = kzalloc(sizeof(*new), GFP_ATOMIC);
  1824. if (!new)
  1825. goto err_no_memory_locked;
  1826. /* Store pointer to the real filter */
  1827. new->f = f;
  1828. new->state = f->state;
  1829. /* Add it to the hash list */
  1830. hlist_add_head(&new->hlist, &tmp_add_list);
  1831. }
  1832. /* Count the number of active (current and new) VLAN
  1833. * filters we have now. Does not count filters which
  1834. * are marked for deletion.
  1835. */
  1836. if (f->vlan > 0)
  1837. vlan_filters++;
  1838. }
  1839. retval = i40e_correct_mac_vlan_filters(vsi,
  1840. &tmp_add_list,
  1841. &tmp_del_list,
  1842. vlan_filters);
  1843. if (retval)
  1844. goto err_no_memory_locked;
  1845. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  1846. }
  1847. /* Now process 'del_list' outside the lock */
  1848. if (!hlist_empty(&tmp_del_list)) {
  1849. filter_list_len = hw->aq.asq_buf_size /
  1850. sizeof(struct i40e_aqc_remove_macvlan_element_data);
  1851. list_size = filter_list_len *
  1852. sizeof(struct i40e_aqc_remove_macvlan_element_data);
  1853. del_list = kzalloc(list_size, GFP_ATOMIC);
  1854. if (!del_list)
  1855. goto err_no_memory;
  1856. hlist_for_each_entry_safe(f, h, &tmp_del_list, hlist) {
  1857. cmd_flags = 0;
  1858. /* handle broadcast filters by updating the broadcast
  1859. * promiscuous flag and release filter list.
  1860. */
  1861. if (is_broadcast_ether_addr(f->macaddr)) {
  1862. i40e_aqc_broadcast_filter(vsi, vsi_name, f);
  1863. hlist_del(&f->hlist);
  1864. kfree(f);
  1865. continue;
  1866. }
  1867. /* add to delete list */
  1868. ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
  1869. if (f->vlan == I40E_VLAN_ANY) {
  1870. del_list[num_del].vlan_tag = 0;
  1871. cmd_flags |= I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
  1872. } else {
  1873. del_list[num_del].vlan_tag =
  1874. cpu_to_le16((u16)(f->vlan));
  1875. }
  1876. cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
  1877. del_list[num_del].flags = cmd_flags;
  1878. num_del++;
  1879. /* flush a full buffer */
  1880. if (num_del == filter_list_len) {
  1881. i40e_aqc_del_filters(vsi, vsi_name, del_list,
  1882. num_del, &retval);
  1883. memset(del_list, 0, list_size);
  1884. num_del = 0;
  1885. }
  1886. /* Release memory for MAC filter entries which were
  1887. * synced up with HW.
  1888. */
  1889. hlist_del(&f->hlist);
  1890. kfree(f);
  1891. }
  1892. if (num_del) {
  1893. i40e_aqc_del_filters(vsi, vsi_name, del_list,
  1894. num_del, &retval);
  1895. }
  1896. kfree(del_list);
  1897. del_list = NULL;
  1898. }
  1899. if (!hlist_empty(&tmp_add_list)) {
  1900. /* Do all the adds now. */
  1901. filter_list_len = hw->aq.asq_buf_size /
  1902. sizeof(struct i40e_aqc_add_macvlan_element_data);
  1903. list_size = filter_list_len *
  1904. sizeof(struct i40e_aqc_add_macvlan_element_data);
  1905. add_list = kzalloc(list_size, GFP_ATOMIC);
  1906. if (!add_list)
  1907. goto err_no_memory;
  1908. num_add = 0;
  1909. hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
  1910. if (test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
  1911. &vsi->state)) {
  1912. new->state = I40E_FILTER_FAILED;
  1913. continue;
  1914. }
  1915. /* handle broadcast filters by updating the broadcast
  1916. * promiscuous flag instead of adding a MAC filter.
  1917. */
  1918. if (is_broadcast_ether_addr(new->f->macaddr)) {
  1919. if (i40e_aqc_broadcast_filter(vsi, vsi_name,
  1920. new->f))
  1921. new->state = I40E_FILTER_FAILED;
  1922. else
  1923. new->state = I40E_FILTER_ACTIVE;
  1924. continue;
  1925. }
  1926. /* add to add array */
  1927. if (num_add == 0)
  1928. add_head = new;
  1929. cmd_flags = 0;
  1930. ether_addr_copy(add_list[num_add].mac_addr,
  1931. new->f->macaddr);
  1932. if (new->f->vlan == I40E_VLAN_ANY) {
  1933. add_list[num_add].vlan_tag = 0;
  1934. cmd_flags |= I40E_AQC_MACVLAN_ADD_IGNORE_VLAN;
  1935. } else {
  1936. add_list[num_add].vlan_tag =
  1937. cpu_to_le16((u16)(new->f->vlan));
  1938. }
  1939. add_list[num_add].queue_number = 0;
  1940. /* set invalid match method for later detection */
  1941. add_list[num_add].match_method = I40E_AQC_MM_ERR_NO_RES;
  1942. cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
  1943. add_list[num_add].flags = cpu_to_le16(cmd_flags);
  1944. num_add++;
  1945. /* flush a full buffer */
  1946. if (num_add == filter_list_len) {
  1947. i40e_aqc_add_filters(vsi, vsi_name, add_list,
  1948. add_head, num_add,
  1949. &promisc_changed);
  1950. memset(add_list, 0, list_size);
  1951. num_add = 0;
  1952. }
  1953. }
  1954. if (num_add) {
  1955. i40e_aqc_add_filters(vsi, vsi_name, add_list, add_head,
  1956. num_add, &promisc_changed);
  1957. }
  1958. /* Now move all of the filters from the temp add list back to
  1959. * the VSI's list.
  1960. */
  1961. spin_lock_bh(&vsi->mac_filter_hash_lock);
  1962. hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
  1963. /* Only update the state if we're still NEW */
  1964. if (new->f->state == I40E_FILTER_NEW)
  1965. new->f->state = new->state;
  1966. hlist_del(&new->hlist);
  1967. kfree(new);
  1968. }
  1969. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  1970. kfree(add_list);
  1971. add_list = NULL;
  1972. }
  1973. /* Determine the number of active and failed filters. */
  1974. spin_lock_bh(&vsi->mac_filter_hash_lock);
  1975. vsi->active_filters = 0;
  1976. hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
  1977. if (f->state == I40E_FILTER_ACTIVE)
  1978. vsi->active_filters++;
  1979. else if (f->state == I40E_FILTER_FAILED)
  1980. failed_filters++;
  1981. }
  1982. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  1983. /* If promiscuous mode has changed, we need to calculate a new
  1984. * threshold for when we are safe to exit
  1985. */
  1986. if (promisc_changed)
  1987. vsi->promisc_threshold = (vsi->active_filters * 3) / 4;
  1988. /* Check if we are able to exit overflow promiscuous mode. We can
  1989. * safely exit if we didn't just enter, we no longer have any failed
  1990. * filters, and we have reduced filters below the threshold value.
  1991. */
  1992. if (test_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state) &&
  1993. !promisc_changed && !failed_filters &&
  1994. (vsi->active_filters < vsi->promisc_threshold)) {
  1995. dev_info(&pf->pdev->dev,
  1996. "filter logjam cleared on %s, leaving overflow promiscuous mode\n",
  1997. vsi_name);
  1998. clear_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state);
  1999. promisc_changed = true;
  2000. vsi->promisc_threshold = 0;
  2001. }
  2002. /* if the VF is not trusted do not do promisc */
  2003. if ((vsi->type == I40E_VSI_SRIOV) && !pf->vf[vsi->vf_id].trusted) {
  2004. clear_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state);
  2005. goto out;
  2006. }
  2007. /* check for changes in promiscuous modes */
  2008. if (changed_flags & IFF_ALLMULTI) {
  2009. bool cur_multipromisc;
  2010. cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
  2011. aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
  2012. vsi->seid,
  2013. cur_multipromisc,
  2014. NULL);
  2015. if (aq_ret) {
  2016. retval = i40e_aq_rc_to_posix(aq_ret,
  2017. hw->aq.asq_last_status);
  2018. dev_info(&pf->pdev->dev,
  2019. "set multi promisc failed on %s, err %s aq_err %s\n",
  2020. vsi_name,
  2021. i40e_stat_str(hw, aq_ret),
  2022. i40e_aq_str(hw, hw->aq.asq_last_status));
  2023. }
  2024. }
  2025. if ((changed_flags & IFF_PROMISC) ||
  2026. (promisc_changed &&
  2027. test_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state))) {
  2028. bool cur_promisc;
  2029. cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
  2030. test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
  2031. &vsi->state));
  2032. if ((vsi->type == I40E_VSI_MAIN) &&
  2033. (pf->lan_veb != I40E_NO_VEB) &&
  2034. !(pf->flags & I40E_FLAG_MFP_ENABLED)) {
  2035. /* set defport ON for Main VSI instead of true promisc
  2036. * this way we will get all unicast/multicast and VLAN
  2037. * promisc behavior but will not get VF or VMDq traffic
  2038. * replicated on the Main VSI.
  2039. */
  2040. if (pf->cur_promisc != cur_promisc) {
  2041. pf->cur_promisc = cur_promisc;
  2042. if (cur_promisc)
  2043. aq_ret =
  2044. i40e_aq_set_default_vsi(hw,
  2045. vsi->seid,
  2046. NULL);
  2047. else
  2048. aq_ret =
  2049. i40e_aq_clear_default_vsi(hw,
  2050. vsi->seid,
  2051. NULL);
  2052. if (aq_ret) {
  2053. retval = i40e_aq_rc_to_posix(aq_ret,
  2054. hw->aq.asq_last_status);
  2055. dev_info(&pf->pdev->dev,
  2056. "Set default VSI failed on %s, err %s, aq_err %s\n",
  2057. vsi_name,
  2058. i40e_stat_str(hw, aq_ret),
  2059. i40e_aq_str(hw,
  2060. hw->aq.asq_last_status));
  2061. }
  2062. }
  2063. } else {
  2064. aq_ret = i40e_aq_set_vsi_unicast_promiscuous(
  2065. hw,
  2066. vsi->seid,
  2067. cur_promisc, NULL,
  2068. true);
  2069. if (aq_ret) {
  2070. retval =
  2071. i40e_aq_rc_to_posix(aq_ret,
  2072. hw->aq.asq_last_status);
  2073. dev_info(&pf->pdev->dev,
  2074. "set unicast promisc failed on %s, err %s, aq_err %s\n",
  2075. vsi_name,
  2076. i40e_stat_str(hw, aq_ret),
  2077. i40e_aq_str(hw,
  2078. hw->aq.asq_last_status));
  2079. }
  2080. aq_ret = i40e_aq_set_vsi_multicast_promiscuous(
  2081. hw,
  2082. vsi->seid,
  2083. cur_promisc, NULL);
  2084. if (aq_ret) {
  2085. retval =
  2086. i40e_aq_rc_to_posix(aq_ret,
  2087. hw->aq.asq_last_status);
  2088. dev_info(&pf->pdev->dev,
  2089. "set multicast promisc failed on %s, err %s, aq_err %s\n",
  2090. vsi_name,
  2091. i40e_stat_str(hw, aq_ret),
  2092. i40e_aq_str(hw,
  2093. hw->aq.asq_last_status));
  2094. }
  2095. }
  2096. aq_ret = i40e_aq_set_vsi_broadcast(&vsi->back->hw,
  2097. vsi->seid,
  2098. cur_promisc, NULL);
  2099. if (aq_ret) {
  2100. retval = i40e_aq_rc_to_posix(aq_ret,
  2101. pf->hw.aq.asq_last_status);
  2102. dev_info(&pf->pdev->dev,
  2103. "set brdcast promisc failed, err %s, aq_err %s\n",
  2104. i40e_stat_str(hw, aq_ret),
  2105. i40e_aq_str(hw,
  2106. hw->aq.asq_last_status));
  2107. }
  2108. }
  2109. out:
  2110. /* if something went wrong then set the changed flag so we try again */
  2111. if (retval)
  2112. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  2113. clear_bit(__I40E_CONFIG_BUSY, &vsi->state);
  2114. return retval;
  2115. err_no_memory:
  2116. /* Restore elements on the temporary add and delete lists */
  2117. spin_lock_bh(&vsi->mac_filter_hash_lock);
  2118. err_no_memory_locked:
  2119. i40e_undo_del_filter_entries(vsi, &tmp_del_list);
  2120. i40e_undo_add_filter_entries(vsi, &tmp_add_list);
  2121. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2122. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  2123. clear_bit(__I40E_CONFIG_BUSY, &vsi->state);
  2124. return -ENOMEM;
  2125. }
  2126. /**
  2127. * i40e_sync_filters_subtask - Sync the VSI filter list with HW
  2128. * @pf: board private structure
  2129. **/
  2130. static void i40e_sync_filters_subtask(struct i40e_pf *pf)
  2131. {
  2132. int v;
  2133. if (!pf || !(pf->flags & I40E_FLAG_FILTER_SYNC))
  2134. return;
  2135. pf->flags &= ~I40E_FLAG_FILTER_SYNC;
  2136. for (v = 0; v < pf->num_alloc_vsi; v++) {
  2137. if (pf->vsi[v] &&
  2138. (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED)) {
  2139. int ret = i40e_sync_vsi_filters(pf->vsi[v]);
  2140. if (ret) {
  2141. /* come back and try again later */
  2142. pf->flags |= I40E_FLAG_FILTER_SYNC;
  2143. break;
  2144. }
  2145. }
  2146. }
  2147. }
  2148. /**
  2149. * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
  2150. * @netdev: network interface device structure
  2151. * @new_mtu: new value for maximum frame size
  2152. *
  2153. * Returns 0 on success, negative on failure
  2154. **/
  2155. static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
  2156. {
  2157. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2158. struct i40e_vsi *vsi = np->vsi;
  2159. struct i40e_pf *pf = vsi->back;
  2160. netdev_info(netdev, "changing MTU from %d to %d\n",
  2161. netdev->mtu, new_mtu);
  2162. netdev->mtu = new_mtu;
  2163. if (netif_running(netdev))
  2164. i40e_vsi_reinit_locked(vsi);
  2165. pf->flags |= (I40E_FLAG_SERVICE_CLIENT_REQUESTED |
  2166. I40E_FLAG_CLIENT_L2_CHANGE);
  2167. return 0;
  2168. }
  2169. /**
  2170. * i40e_ioctl - Access the hwtstamp interface
  2171. * @netdev: network interface device structure
  2172. * @ifr: interface request data
  2173. * @cmd: ioctl command
  2174. **/
  2175. int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
  2176. {
  2177. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2178. struct i40e_pf *pf = np->vsi->back;
  2179. switch (cmd) {
  2180. case SIOCGHWTSTAMP:
  2181. return i40e_ptp_get_ts_config(pf, ifr);
  2182. case SIOCSHWTSTAMP:
  2183. return i40e_ptp_set_ts_config(pf, ifr);
  2184. default:
  2185. return -EOPNOTSUPP;
  2186. }
  2187. }
  2188. /**
  2189. * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
  2190. * @vsi: the vsi being adjusted
  2191. **/
  2192. void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
  2193. {
  2194. struct i40e_vsi_context ctxt;
  2195. i40e_status ret;
  2196. if ((vsi->info.valid_sections &
  2197. cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
  2198. ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
  2199. return; /* already enabled */
  2200. vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
  2201. vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
  2202. I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
  2203. ctxt.seid = vsi->seid;
  2204. ctxt.info = vsi->info;
  2205. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  2206. if (ret) {
  2207. dev_info(&vsi->back->pdev->dev,
  2208. "update vlan stripping failed, err %s aq_err %s\n",
  2209. i40e_stat_str(&vsi->back->hw, ret),
  2210. i40e_aq_str(&vsi->back->hw,
  2211. vsi->back->hw.aq.asq_last_status));
  2212. }
  2213. }
  2214. /**
  2215. * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
  2216. * @vsi: the vsi being adjusted
  2217. **/
  2218. void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
  2219. {
  2220. struct i40e_vsi_context ctxt;
  2221. i40e_status ret;
  2222. if ((vsi->info.valid_sections &
  2223. cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
  2224. ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
  2225. I40E_AQ_VSI_PVLAN_EMOD_MASK))
  2226. return; /* already disabled */
  2227. vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
  2228. vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
  2229. I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
  2230. ctxt.seid = vsi->seid;
  2231. ctxt.info = vsi->info;
  2232. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  2233. if (ret) {
  2234. dev_info(&vsi->back->pdev->dev,
  2235. "update vlan stripping failed, err %s aq_err %s\n",
  2236. i40e_stat_str(&vsi->back->hw, ret),
  2237. i40e_aq_str(&vsi->back->hw,
  2238. vsi->back->hw.aq.asq_last_status));
  2239. }
  2240. }
  2241. /**
  2242. * i40e_vlan_rx_register - Setup or shutdown vlan offload
  2243. * @netdev: network interface to be adjusted
  2244. * @features: netdev features to test if VLAN offload is enabled or not
  2245. **/
  2246. static void i40e_vlan_rx_register(struct net_device *netdev, u32 features)
  2247. {
  2248. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2249. struct i40e_vsi *vsi = np->vsi;
  2250. if (features & NETIF_F_HW_VLAN_CTAG_RX)
  2251. i40e_vlan_stripping_enable(vsi);
  2252. else
  2253. i40e_vlan_stripping_disable(vsi);
  2254. }
  2255. /**
  2256. * i40e_add_vlan_all_mac - Add a MAC/VLAN filter for each existing MAC address
  2257. * @vsi: the vsi being configured
  2258. * @vid: vlan id to be added (0 = untagged only , -1 = any)
  2259. *
  2260. * This is a helper function for adding a new MAC/VLAN filter with the
  2261. * specified VLAN for each existing MAC address already in the hash table.
  2262. * This function does *not* perform any accounting to update filters based on
  2263. * VLAN mode.
  2264. *
  2265. * NOTE: this function expects to be called while under the
  2266. * mac_filter_hash_lock
  2267. **/
  2268. int i40e_add_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
  2269. {
  2270. struct i40e_mac_filter *f, *add_f;
  2271. struct hlist_node *h;
  2272. int bkt;
  2273. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  2274. if (f->state == I40E_FILTER_REMOVE)
  2275. continue;
  2276. add_f = i40e_add_filter(vsi, f->macaddr, vid);
  2277. if (!add_f) {
  2278. dev_info(&vsi->back->pdev->dev,
  2279. "Could not add vlan filter %d for %pM\n",
  2280. vid, f->macaddr);
  2281. return -ENOMEM;
  2282. }
  2283. }
  2284. return 0;
  2285. }
  2286. /**
  2287. * i40e_vsi_add_vlan - Add VSI membership for given VLAN
  2288. * @vsi: the VSI being configured
  2289. * @vid: VLAN id to be added
  2290. **/
  2291. int i40e_vsi_add_vlan(struct i40e_vsi *vsi, u16 vid)
  2292. {
  2293. int err;
  2294. if (!vid || vsi->info.pvid)
  2295. return -EINVAL;
  2296. /* Locked once because all functions invoked below iterates list*/
  2297. spin_lock_bh(&vsi->mac_filter_hash_lock);
  2298. err = i40e_add_vlan_all_mac(vsi, vid);
  2299. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2300. if (err)
  2301. return err;
  2302. /* schedule our worker thread which will take care of
  2303. * applying the new filter changes
  2304. */
  2305. i40e_service_event_schedule(vsi->back);
  2306. return 0;
  2307. }
  2308. /**
  2309. * i40e_rm_vlan_all_mac - Remove MAC/VLAN pair for all MAC with the given VLAN
  2310. * @vsi: the vsi being configured
  2311. * @vid: vlan id to be removed (0 = untagged only , -1 = any)
  2312. *
  2313. * This function should be used to remove all VLAN filters which match the
  2314. * given VID. It does not schedule the service event and does not take the
  2315. * mac_filter_hash_lock so it may be combined with other operations under
  2316. * a single invocation of the mac_filter_hash_lock.
  2317. *
  2318. * NOTE: this function expects to be called while under the
  2319. * mac_filter_hash_lock
  2320. */
  2321. void i40e_rm_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
  2322. {
  2323. struct i40e_mac_filter *f;
  2324. struct hlist_node *h;
  2325. int bkt;
  2326. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  2327. if (f->vlan == vid)
  2328. __i40e_del_filter(vsi, f);
  2329. }
  2330. }
  2331. /**
  2332. * i40e_vsi_kill_vlan - Remove VSI membership for given VLAN
  2333. * @vsi: the VSI being configured
  2334. * @vid: VLAN id to be removed
  2335. **/
  2336. void i40e_vsi_kill_vlan(struct i40e_vsi *vsi, u16 vid)
  2337. {
  2338. if (!vid || vsi->info.pvid)
  2339. return;
  2340. spin_lock_bh(&vsi->mac_filter_hash_lock);
  2341. i40e_rm_vlan_all_mac(vsi, vid);
  2342. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2343. /* schedule our worker thread which will take care of
  2344. * applying the new filter changes
  2345. */
  2346. i40e_service_event_schedule(vsi->back);
  2347. }
  2348. /**
  2349. * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
  2350. * @netdev: network interface to be adjusted
  2351. * @vid: vlan id to be added
  2352. *
  2353. * net_device_ops implementation for adding vlan ids
  2354. **/
  2355. static int i40e_vlan_rx_add_vid(struct net_device *netdev,
  2356. __always_unused __be16 proto, u16 vid)
  2357. {
  2358. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2359. struct i40e_vsi *vsi = np->vsi;
  2360. int ret = 0;
  2361. if (vid >= VLAN_N_VID)
  2362. return -EINVAL;
  2363. /* If the network stack called us with vid = 0 then
  2364. * it is asking to receive priority tagged packets with
  2365. * vlan id 0. Our HW receives them by default when configured
  2366. * to receive untagged packets so there is no need to add an
  2367. * extra filter for vlan 0 tagged packets.
  2368. */
  2369. if (vid)
  2370. ret = i40e_vsi_add_vlan(vsi, vid);
  2371. if (!ret)
  2372. set_bit(vid, vsi->active_vlans);
  2373. return ret;
  2374. }
  2375. /**
  2376. * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
  2377. * @netdev: network interface to be adjusted
  2378. * @vid: vlan id to be removed
  2379. *
  2380. * net_device_ops implementation for removing vlan ids
  2381. **/
  2382. static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
  2383. __always_unused __be16 proto, u16 vid)
  2384. {
  2385. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2386. struct i40e_vsi *vsi = np->vsi;
  2387. /* return code is ignored as there is nothing a user
  2388. * can do about failure to remove and a log message was
  2389. * already printed from the other function
  2390. */
  2391. i40e_vsi_kill_vlan(vsi, vid);
  2392. clear_bit(vid, vsi->active_vlans);
  2393. return 0;
  2394. }
  2395. /**
  2396. * i40e_macaddr_init - explicitly write the mac address filters
  2397. *
  2398. * @vsi: pointer to the vsi
  2399. * @macaddr: the MAC address
  2400. *
  2401. * This is needed when the macaddr has been obtained by other
  2402. * means than the default, e.g., from Open Firmware or IDPROM.
  2403. * Returns 0 on success, negative on failure
  2404. **/
  2405. static int i40e_macaddr_init(struct i40e_vsi *vsi, u8 *macaddr)
  2406. {
  2407. int ret;
  2408. struct i40e_aqc_add_macvlan_element_data element;
  2409. ret = i40e_aq_mac_address_write(&vsi->back->hw,
  2410. I40E_AQC_WRITE_TYPE_LAA_WOL,
  2411. macaddr, NULL);
  2412. if (ret) {
  2413. dev_info(&vsi->back->pdev->dev,
  2414. "Addr change for VSI failed: %d\n", ret);
  2415. return -EADDRNOTAVAIL;
  2416. }
  2417. memset(&element, 0, sizeof(element));
  2418. ether_addr_copy(element.mac_addr, macaddr);
  2419. element.flags = cpu_to_le16(I40E_AQC_MACVLAN_ADD_PERFECT_MATCH);
  2420. ret = i40e_aq_add_macvlan(&vsi->back->hw, vsi->seid, &element, 1, NULL);
  2421. if (ret) {
  2422. dev_info(&vsi->back->pdev->dev,
  2423. "add filter failed err %s aq_err %s\n",
  2424. i40e_stat_str(&vsi->back->hw, ret),
  2425. i40e_aq_str(&vsi->back->hw,
  2426. vsi->back->hw.aq.asq_last_status));
  2427. }
  2428. return ret;
  2429. }
  2430. /**
  2431. * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
  2432. * @vsi: the vsi being brought back up
  2433. **/
  2434. static void i40e_restore_vlan(struct i40e_vsi *vsi)
  2435. {
  2436. u16 vid;
  2437. if (!vsi->netdev)
  2438. return;
  2439. i40e_vlan_rx_register(vsi->netdev, vsi->netdev->features);
  2440. for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
  2441. i40e_vlan_rx_add_vid(vsi->netdev, htons(ETH_P_8021Q),
  2442. vid);
  2443. }
  2444. /**
  2445. * i40e_vsi_add_pvid - Add pvid for the VSI
  2446. * @vsi: the vsi being adjusted
  2447. * @vid: the vlan id to set as a PVID
  2448. **/
  2449. int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
  2450. {
  2451. struct i40e_vsi_context ctxt;
  2452. i40e_status ret;
  2453. vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
  2454. vsi->info.pvid = cpu_to_le16(vid);
  2455. vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
  2456. I40E_AQ_VSI_PVLAN_INSERT_PVID |
  2457. I40E_AQ_VSI_PVLAN_EMOD_STR;
  2458. ctxt.seid = vsi->seid;
  2459. ctxt.info = vsi->info;
  2460. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  2461. if (ret) {
  2462. dev_info(&vsi->back->pdev->dev,
  2463. "add pvid failed, err %s aq_err %s\n",
  2464. i40e_stat_str(&vsi->back->hw, ret),
  2465. i40e_aq_str(&vsi->back->hw,
  2466. vsi->back->hw.aq.asq_last_status));
  2467. return -ENOENT;
  2468. }
  2469. return 0;
  2470. }
  2471. /**
  2472. * i40e_vsi_remove_pvid - Remove the pvid from the VSI
  2473. * @vsi: the vsi being adjusted
  2474. *
  2475. * Just use the vlan_rx_register() service to put it back to normal
  2476. **/
  2477. void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
  2478. {
  2479. i40e_vlan_stripping_disable(vsi);
  2480. vsi->info.pvid = 0;
  2481. }
  2482. /**
  2483. * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
  2484. * @vsi: ptr to the VSI
  2485. *
  2486. * If this function returns with an error, then it's possible one or
  2487. * more of the rings is populated (while the rest are not). It is the
  2488. * callers duty to clean those orphaned rings.
  2489. *
  2490. * Return 0 on success, negative on failure
  2491. **/
  2492. static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
  2493. {
  2494. int i, err = 0;
  2495. for (i = 0; i < vsi->num_queue_pairs && !err; i++)
  2496. err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
  2497. return err;
  2498. }
  2499. /**
  2500. * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
  2501. * @vsi: ptr to the VSI
  2502. *
  2503. * Free VSI's transmit software resources
  2504. **/
  2505. static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
  2506. {
  2507. int i;
  2508. if (!vsi->tx_rings)
  2509. return;
  2510. for (i = 0; i < vsi->num_queue_pairs; i++)
  2511. if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
  2512. i40e_free_tx_resources(vsi->tx_rings[i]);
  2513. }
  2514. /**
  2515. * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
  2516. * @vsi: ptr to the VSI
  2517. *
  2518. * If this function returns with an error, then it's possible one or
  2519. * more of the rings is populated (while the rest are not). It is the
  2520. * callers duty to clean those orphaned rings.
  2521. *
  2522. * Return 0 on success, negative on failure
  2523. **/
  2524. static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
  2525. {
  2526. int i, err = 0;
  2527. for (i = 0; i < vsi->num_queue_pairs && !err; i++)
  2528. err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
  2529. return err;
  2530. }
  2531. /**
  2532. * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
  2533. * @vsi: ptr to the VSI
  2534. *
  2535. * Free all receive software resources
  2536. **/
  2537. static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
  2538. {
  2539. int i;
  2540. if (!vsi->rx_rings)
  2541. return;
  2542. for (i = 0; i < vsi->num_queue_pairs; i++)
  2543. if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
  2544. i40e_free_rx_resources(vsi->rx_rings[i]);
  2545. }
  2546. /**
  2547. * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
  2548. * @ring: The Tx ring to configure
  2549. *
  2550. * This enables/disables XPS for a given Tx descriptor ring
  2551. * based on the TCs enabled for the VSI that ring belongs to.
  2552. **/
  2553. static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
  2554. {
  2555. struct i40e_vsi *vsi = ring->vsi;
  2556. cpumask_var_t mask;
  2557. if (!ring->q_vector || !ring->netdev)
  2558. return;
  2559. /* Single TC mode enable XPS */
  2560. if (vsi->tc_config.numtc <= 1) {
  2561. if (!test_and_set_bit(__I40E_TX_XPS_INIT_DONE, &ring->state))
  2562. netif_set_xps_queue(ring->netdev,
  2563. &ring->q_vector->affinity_mask,
  2564. ring->queue_index);
  2565. } else if (alloc_cpumask_var(&mask, GFP_KERNEL)) {
  2566. /* Disable XPS to allow selection based on TC */
  2567. bitmap_zero(cpumask_bits(mask), nr_cpumask_bits);
  2568. netif_set_xps_queue(ring->netdev, mask, ring->queue_index);
  2569. free_cpumask_var(mask);
  2570. }
  2571. /* schedule our worker thread which will take care of
  2572. * applying the new filter changes
  2573. */
  2574. i40e_service_event_schedule(vsi->back);
  2575. }
  2576. /**
  2577. * i40e_configure_tx_ring - Configure a transmit ring context and rest
  2578. * @ring: The Tx ring to configure
  2579. *
  2580. * Configure the Tx descriptor ring in the HMC context.
  2581. **/
  2582. static int i40e_configure_tx_ring(struct i40e_ring *ring)
  2583. {
  2584. struct i40e_vsi *vsi = ring->vsi;
  2585. u16 pf_q = vsi->base_queue + ring->queue_index;
  2586. struct i40e_hw *hw = &vsi->back->hw;
  2587. struct i40e_hmc_obj_txq tx_ctx;
  2588. i40e_status err = 0;
  2589. u32 qtx_ctl = 0;
  2590. /* some ATR related tx ring init */
  2591. if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
  2592. ring->atr_sample_rate = vsi->back->atr_sample_rate;
  2593. ring->atr_count = 0;
  2594. } else {
  2595. ring->atr_sample_rate = 0;
  2596. }
  2597. /* configure XPS */
  2598. i40e_config_xps_tx_ring(ring);
  2599. /* clear the context structure first */
  2600. memset(&tx_ctx, 0, sizeof(tx_ctx));
  2601. tx_ctx.new_context = 1;
  2602. tx_ctx.base = (ring->dma / 128);
  2603. tx_ctx.qlen = ring->count;
  2604. tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
  2605. I40E_FLAG_FD_ATR_ENABLED));
  2606. tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
  2607. /* FDIR VSI tx ring can still use RS bit and writebacks */
  2608. if (vsi->type != I40E_VSI_FDIR)
  2609. tx_ctx.head_wb_ena = 1;
  2610. tx_ctx.head_wb_addr = ring->dma +
  2611. (ring->count * sizeof(struct i40e_tx_desc));
  2612. /* As part of VSI creation/update, FW allocates certain
  2613. * Tx arbitration queue sets for each TC enabled for
  2614. * the VSI. The FW returns the handles to these queue
  2615. * sets as part of the response buffer to Add VSI,
  2616. * Update VSI, etc. AQ commands. It is expected that
  2617. * these queue set handles be associated with the Tx
  2618. * queues by the driver as part of the TX queue context
  2619. * initialization. This has to be done regardless of
  2620. * DCB as by default everything is mapped to TC0.
  2621. */
  2622. tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
  2623. tx_ctx.rdylist_act = 0;
  2624. /* clear the context in the HMC */
  2625. err = i40e_clear_lan_tx_queue_context(hw, pf_q);
  2626. if (err) {
  2627. dev_info(&vsi->back->pdev->dev,
  2628. "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
  2629. ring->queue_index, pf_q, err);
  2630. return -ENOMEM;
  2631. }
  2632. /* set the context in the HMC */
  2633. err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
  2634. if (err) {
  2635. dev_info(&vsi->back->pdev->dev,
  2636. "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
  2637. ring->queue_index, pf_q, err);
  2638. return -ENOMEM;
  2639. }
  2640. /* Now associate this queue with this PCI function */
  2641. if (vsi->type == I40E_VSI_VMDQ2) {
  2642. qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
  2643. qtx_ctl |= ((vsi->id) << I40E_QTX_CTL_VFVM_INDX_SHIFT) &
  2644. I40E_QTX_CTL_VFVM_INDX_MASK;
  2645. } else {
  2646. qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
  2647. }
  2648. qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
  2649. I40E_QTX_CTL_PF_INDX_MASK);
  2650. wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
  2651. i40e_flush(hw);
  2652. /* cache tail off for easier writes later */
  2653. ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
  2654. return 0;
  2655. }
  2656. /**
  2657. * i40e_configure_rx_ring - Configure a receive ring context
  2658. * @ring: The Rx ring to configure
  2659. *
  2660. * Configure the Rx descriptor ring in the HMC context.
  2661. **/
  2662. static int i40e_configure_rx_ring(struct i40e_ring *ring)
  2663. {
  2664. struct i40e_vsi *vsi = ring->vsi;
  2665. u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
  2666. u16 pf_q = vsi->base_queue + ring->queue_index;
  2667. struct i40e_hw *hw = &vsi->back->hw;
  2668. struct i40e_hmc_obj_rxq rx_ctx;
  2669. i40e_status err = 0;
  2670. ring->state = 0;
  2671. /* clear the context structure first */
  2672. memset(&rx_ctx, 0, sizeof(rx_ctx));
  2673. ring->rx_buf_len = vsi->rx_buf_len;
  2674. rx_ctx.dbuff = DIV_ROUND_UP(ring->rx_buf_len,
  2675. BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
  2676. rx_ctx.base = (ring->dma / 128);
  2677. rx_ctx.qlen = ring->count;
  2678. /* use 32 byte descriptors */
  2679. rx_ctx.dsize = 1;
  2680. /* descriptor type is always zero
  2681. * rx_ctx.dtype = 0;
  2682. */
  2683. rx_ctx.hsplit_0 = 0;
  2684. rx_ctx.rxmax = min_t(u16, vsi->max_frame, chain_len * ring->rx_buf_len);
  2685. if (hw->revision_id == 0)
  2686. rx_ctx.lrxqthresh = 0;
  2687. else
  2688. rx_ctx.lrxqthresh = 2;
  2689. rx_ctx.crcstrip = 1;
  2690. rx_ctx.l2tsel = 1;
  2691. /* this controls whether VLAN is stripped from inner headers */
  2692. rx_ctx.showiv = 0;
  2693. /* set the prefena field to 1 because the manual says to */
  2694. rx_ctx.prefena = 1;
  2695. /* clear the context in the HMC */
  2696. err = i40e_clear_lan_rx_queue_context(hw, pf_q);
  2697. if (err) {
  2698. dev_info(&vsi->back->pdev->dev,
  2699. "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
  2700. ring->queue_index, pf_q, err);
  2701. return -ENOMEM;
  2702. }
  2703. /* set the context in the HMC */
  2704. err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
  2705. if (err) {
  2706. dev_info(&vsi->back->pdev->dev,
  2707. "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
  2708. ring->queue_index, pf_q, err);
  2709. return -ENOMEM;
  2710. }
  2711. /* configure Rx buffer alignment */
  2712. if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
  2713. clear_ring_build_skb_enabled(ring);
  2714. else
  2715. set_ring_build_skb_enabled(ring);
  2716. /* cache tail for quicker writes, and clear the reg before use */
  2717. ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
  2718. writel(0, ring->tail);
  2719. i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
  2720. return 0;
  2721. }
  2722. /**
  2723. * i40e_vsi_configure_tx - Configure the VSI for Tx
  2724. * @vsi: VSI structure describing this set of rings and resources
  2725. *
  2726. * Configure the Tx VSI for operation.
  2727. **/
  2728. static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
  2729. {
  2730. int err = 0;
  2731. u16 i;
  2732. for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
  2733. err = i40e_configure_tx_ring(vsi->tx_rings[i]);
  2734. return err;
  2735. }
  2736. /**
  2737. * i40e_vsi_configure_rx - Configure the VSI for Rx
  2738. * @vsi: the VSI being configured
  2739. *
  2740. * Configure the Rx VSI for operation.
  2741. **/
  2742. static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
  2743. {
  2744. int err = 0;
  2745. u16 i;
  2746. if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX)) {
  2747. vsi->max_frame = I40E_MAX_RXBUFFER;
  2748. vsi->rx_buf_len = I40E_RXBUFFER_2048;
  2749. #if (PAGE_SIZE < 8192)
  2750. } else if (!I40E_2K_TOO_SMALL_WITH_PADDING &&
  2751. (vsi->netdev->mtu <= ETH_DATA_LEN)) {
  2752. vsi->max_frame = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
  2753. vsi->rx_buf_len = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
  2754. #endif
  2755. } else {
  2756. vsi->max_frame = I40E_MAX_RXBUFFER;
  2757. vsi->rx_buf_len = (PAGE_SIZE < 8192) ? I40E_RXBUFFER_3072 :
  2758. I40E_RXBUFFER_2048;
  2759. }
  2760. /* set up individual rings */
  2761. for (i = 0; i < vsi->num_queue_pairs && !err; i++)
  2762. err = i40e_configure_rx_ring(vsi->rx_rings[i]);
  2763. return err;
  2764. }
  2765. /**
  2766. * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
  2767. * @vsi: ptr to the VSI
  2768. **/
  2769. static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
  2770. {
  2771. struct i40e_ring *tx_ring, *rx_ring;
  2772. u16 qoffset, qcount;
  2773. int i, n;
  2774. if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
  2775. /* Reset the TC information */
  2776. for (i = 0; i < vsi->num_queue_pairs; i++) {
  2777. rx_ring = vsi->rx_rings[i];
  2778. tx_ring = vsi->tx_rings[i];
  2779. rx_ring->dcb_tc = 0;
  2780. tx_ring->dcb_tc = 0;
  2781. }
  2782. }
  2783. for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
  2784. if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
  2785. continue;
  2786. qoffset = vsi->tc_config.tc_info[n].qoffset;
  2787. qcount = vsi->tc_config.tc_info[n].qcount;
  2788. for (i = qoffset; i < (qoffset + qcount); i++) {
  2789. rx_ring = vsi->rx_rings[i];
  2790. tx_ring = vsi->tx_rings[i];
  2791. rx_ring->dcb_tc = n;
  2792. tx_ring->dcb_tc = n;
  2793. }
  2794. }
  2795. }
  2796. /**
  2797. * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
  2798. * @vsi: ptr to the VSI
  2799. **/
  2800. static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
  2801. {
  2802. struct i40e_pf *pf = vsi->back;
  2803. int err;
  2804. if (vsi->netdev)
  2805. i40e_set_rx_mode(vsi->netdev);
  2806. if (!!(pf->flags & I40E_FLAG_PF_MAC)) {
  2807. err = i40e_macaddr_init(vsi, pf->hw.mac.addr);
  2808. if (err) {
  2809. dev_warn(&pf->pdev->dev,
  2810. "could not set up macaddr; err %d\n", err);
  2811. }
  2812. }
  2813. }
  2814. /**
  2815. * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
  2816. * @vsi: Pointer to the targeted VSI
  2817. *
  2818. * This function replays the hlist on the hw where all the SB Flow Director
  2819. * filters were saved.
  2820. **/
  2821. static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
  2822. {
  2823. struct i40e_fdir_filter *filter;
  2824. struct i40e_pf *pf = vsi->back;
  2825. struct hlist_node *node;
  2826. if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
  2827. return;
  2828. /* Reset FDir counters as we're replaying all existing filters */
  2829. pf->fd_tcp4_filter_cnt = 0;
  2830. pf->fd_udp4_filter_cnt = 0;
  2831. pf->fd_sctp4_filter_cnt = 0;
  2832. pf->fd_ip4_filter_cnt = 0;
  2833. hlist_for_each_entry_safe(filter, node,
  2834. &pf->fdir_filter_list, fdir_node) {
  2835. i40e_add_del_fdir(vsi, filter, true);
  2836. }
  2837. }
  2838. /**
  2839. * i40e_vsi_configure - Set up the VSI for action
  2840. * @vsi: the VSI being configured
  2841. **/
  2842. static int i40e_vsi_configure(struct i40e_vsi *vsi)
  2843. {
  2844. int err;
  2845. i40e_set_vsi_rx_mode(vsi);
  2846. i40e_restore_vlan(vsi);
  2847. i40e_vsi_config_dcb_rings(vsi);
  2848. err = i40e_vsi_configure_tx(vsi);
  2849. if (!err)
  2850. err = i40e_vsi_configure_rx(vsi);
  2851. return err;
  2852. }
  2853. /**
  2854. * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
  2855. * @vsi: the VSI being configured
  2856. **/
  2857. static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
  2858. {
  2859. struct i40e_pf *pf = vsi->back;
  2860. struct i40e_hw *hw = &pf->hw;
  2861. u16 vector;
  2862. int i, q;
  2863. u32 qp;
  2864. /* The interrupt indexing is offset by 1 in the PFINT_ITRn
  2865. * and PFINT_LNKLSTn registers, e.g.:
  2866. * PFINT_ITRn[0..n-1] gets msix-1..msix-n (qpair interrupts)
  2867. */
  2868. qp = vsi->base_queue;
  2869. vector = vsi->base_vector;
  2870. for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
  2871. struct i40e_q_vector *q_vector = vsi->q_vectors[i];
  2872. q_vector->itr_countdown = ITR_COUNTDOWN_START;
  2873. q_vector->rx.itr = ITR_TO_REG(vsi->rx_rings[i]->rx_itr_setting);
  2874. q_vector->rx.latency_range = I40E_LOW_LATENCY;
  2875. wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
  2876. q_vector->rx.itr);
  2877. q_vector->tx.itr = ITR_TO_REG(vsi->tx_rings[i]->tx_itr_setting);
  2878. q_vector->tx.latency_range = I40E_LOW_LATENCY;
  2879. wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
  2880. q_vector->tx.itr);
  2881. wr32(hw, I40E_PFINT_RATEN(vector - 1),
  2882. i40e_intrl_usec_to_reg(vsi->int_rate_limit));
  2883. /* Linked list for the queuepairs assigned to this vector */
  2884. wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
  2885. for (q = 0; q < q_vector->num_ringpairs; q++) {
  2886. u32 val;
  2887. val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
  2888. (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
  2889. (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
  2890. (qp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
  2891. (I40E_QUEUE_TYPE_TX
  2892. << I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
  2893. wr32(hw, I40E_QINT_RQCTL(qp), val);
  2894. val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  2895. (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
  2896. (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
  2897. ((qp+1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT)|
  2898. (I40E_QUEUE_TYPE_RX
  2899. << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
  2900. /* Terminate the linked list */
  2901. if (q == (q_vector->num_ringpairs - 1))
  2902. val |= (I40E_QUEUE_END_OF_LIST
  2903. << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
  2904. wr32(hw, I40E_QINT_TQCTL(qp), val);
  2905. qp++;
  2906. }
  2907. }
  2908. i40e_flush(hw);
  2909. }
  2910. /**
  2911. * i40e_enable_misc_int_causes - enable the non-queue interrupts
  2912. * @hw: ptr to the hardware info
  2913. **/
  2914. static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
  2915. {
  2916. struct i40e_hw *hw = &pf->hw;
  2917. u32 val;
  2918. /* clear things first */
  2919. wr32(hw, I40E_PFINT_ICR0_ENA, 0); /* disable all */
  2920. rd32(hw, I40E_PFINT_ICR0); /* read to clear */
  2921. val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK |
  2922. I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK |
  2923. I40E_PFINT_ICR0_ENA_GRST_MASK |
  2924. I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
  2925. I40E_PFINT_ICR0_ENA_GPIO_MASK |
  2926. I40E_PFINT_ICR0_ENA_HMC_ERR_MASK |
  2927. I40E_PFINT_ICR0_ENA_VFLR_MASK |
  2928. I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
  2929. if (pf->flags & I40E_FLAG_IWARP_ENABLED)
  2930. val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
  2931. if (pf->flags & I40E_FLAG_PTP)
  2932. val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
  2933. wr32(hw, I40E_PFINT_ICR0_ENA, val);
  2934. /* SW_ITR_IDX = 0, but don't change INTENA */
  2935. wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
  2936. I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
  2937. /* OTHER_ITR_IDX = 0 */
  2938. wr32(hw, I40E_PFINT_STAT_CTL0, 0);
  2939. }
  2940. /**
  2941. * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
  2942. * @vsi: the VSI being configured
  2943. **/
  2944. static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
  2945. {
  2946. struct i40e_q_vector *q_vector = vsi->q_vectors[0];
  2947. struct i40e_pf *pf = vsi->back;
  2948. struct i40e_hw *hw = &pf->hw;
  2949. u32 val;
  2950. /* set the ITR configuration */
  2951. q_vector->itr_countdown = ITR_COUNTDOWN_START;
  2952. q_vector->rx.itr = ITR_TO_REG(vsi->rx_rings[0]->rx_itr_setting);
  2953. q_vector->rx.latency_range = I40E_LOW_LATENCY;
  2954. wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.itr);
  2955. q_vector->tx.itr = ITR_TO_REG(vsi->tx_rings[0]->tx_itr_setting);
  2956. q_vector->tx.latency_range = I40E_LOW_LATENCY;
  2957. wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.itr);
  2958. i40e_enable_misc_int_causes(pf);
  2959. /* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
  2960. wr32(hw, I40E_PFINT_LNKLST0, 0);
  2961. /* Associate the queue pair to the vector and enable the queue int */
  2962. val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
  2963. (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
  2964. (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
  2965. wr32(hw, I40E_QINT_RQCTL(0), val);
  2966. val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  2967. (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
  2968. (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
  2969. wr32(hw, I40E_QINT_TQCTL(0), val);
  2970. i40e_flush(hw);
  2971. }
  2972. /**
  2973. * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
  2974. * @pf: board private structure
  2975. **/
  2976. void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
  2977. {
  2978. struct i40e_hw *hw = &pf->hw;
  2979. wr32(hw, I40E_PFINT_DYN_CTL0,
  2980. I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
  2981. i40e_flush(hw);
  2982. }
  2983. /**
  2984. * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
  2985. * @pf: board private structure
  2986. * @clearpba: true when all pending interrupt events should be cleared
  2987. **/
  2988. void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf, bool clearpba)
  2989. {
  2990. struct i40e_hw *hw = &pf->hw;
  2991. u32 val;
  2992. val = I40E_PFINT_DYN_CTL0_INTENA_MASK |
  2993. (clearpba ? I40E_PFINT_DYN_CTL0_CLEARPBA_MASK : 0) |
  2994. (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
  2995. wr32(hw, I40E_PFINT_DYN_CTL0, val);
  2996. i40e_flush(hw);
  2997. }
  2998. /**
  2999. * i40e_msix_clean_rings - MSIX mode Interrupt Handler
  3000. * @irq: interrupt number
  3001. * @data: pointer to a q_vector
  3002. **/
  3003. static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
  3004. {
  3005. struct i40e_q_vector *q_vector = data;
  3006. if (!q_vector->tx.ring && !q_vector->rx.ring)
  3007. return IRQ_HANDLED;
  3008. napi_schedule_irqoff(&q_vector->napi);
  3009. return IRQ_HANDLED;
  3010. }
  3011. /**
  3012. * i40e_irq_affinity_notify - Callback for affinity changes
  3013. * @notify: context as to what irq was changed
  3014. * @mask: the new affinity mask
  3015. *
  3016. * This is a callback function used by the irq_set_affinity_notifier function
  3017. * so that we may register to receive changes to the irq affinity masks.
  3018. **/
  3019. static void i40e_irq_affinity_notify(struct irq_affinity_notify *notify,
  3020. const cpumask_t *mask)
  3021. {
  3022. struct i40e_q_vector *q_vector =
  3023. container_of(notify, struct i40e_q_vector, affinity_notify);
  3024. q_vector->affinity_mask = *mask;
  3025. }
  3026. /**
  3027. * i40e_irq_affinity_release - Callback for affinity notifier release
  3028. * @ref: internal core kernel usage
  3029. *
  3030. * This is a callback function used by the irq_set_affinity_notifier function
  3031. * to inform the current notification subscriber that they will no longer
  3032. * receive notifications.
  3033. **/
  3034. static void i40e_irq_affinity_release(struct kref *ref) {}
  3035. /**
  3036. * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
  3037. * @vsi: the VSI being configured
  3038. * @basename: name for the vector
  3039. *
  3040. * Allocates MSI-X vectors and requests interrupts from the kernel.
  3041. **/
  3042. static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
  3043. {
  3044. int q_vectors = vsi->num_q_vectors;
  3045. struct i40e_pf *pf = vsi->back;
  3046. int base = vsi->base_vector;
  3047. int rx_int_idx = 0;
  3048. int tx_int_idx = 0;
  3049. int vector, err;
  3050. int irq_num;
  3051. for (vector = 0; vector < q_vectors; vector++) {
  3052. struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
  3053. irq_num = pf->msix_entries[base + vector].vector;
  3054. if (q_vector->tx.ring && q_vector->rx.ring) {
  3055. snprintf(q_vector->name, sizeof(q_vector->name) - 1,
  3056. "%s-%s-%d", basename, "TxRx", rx_int_idx++);
  3057. tx_int_idx++;
  3058. } else if (q_vector->rx.ring) {
  3059. snprintf(q_vector->name, sizeof(q_vector->name) - 1,
  3060. "%s-%s-%d", basename, "rx", rx_int_idx++);
  3061. } else if (q_vector->tx.ring) {
  3062. snprintf(q_vector->name, sizeof(q_vector->name) - 1,
  3063. "%s-%s-%d", basename, "tx", tx_int_idx++);
  3064. } else {
  3065. /* skip this unused q_vector */
  3066. continue;
  3067. }
  3068. err = request_irq(irq_num,
  3069. vsi->irq_handler,
  3070. 0,
  3071. q_vector->name,
  3072. q_vector);
  3073. if (err) {
  3074. dev_info(&pf->pdev->dev,
  3075. "MSIX request_irq failed, error: %d\n", err);
  3076. goto free_queue_irqs;
  3077. }
  3078. /* register for affinity change notifications */
  3079. q_vector->affinity_notify.notify = i40e_irq_affinity_notify;
  3080. q_vector->affinity_notify.release = i40e_irq_affinity_release;
  3081. irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
  3082. /* assign the mask for this irq */
  3083. irq_set_affinity_hint(irq_num, &q_vector->affinity_mask);
  3084. }
  3085. vsi->irqs_ready = true;
  3086. return 0;
  3087. free_queue_irqs:
  3088. while (vector) {
  3089. vector--;
  3090. irq_num = pf->msix_entries[base + vector].vector;
  3091. irq_set_affinity_notifier(irq_num, NULL);
  3092. irq_set_affinity_hint(irq_num, NULL);
  3093. free_irq(irq_num, &vsi->q_vectors[vector]);
  3094. }
  3095. return err;
  3096. }
  3097. /**
  3098. * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
  3099. * @vsi: the VSI being un-configured
  3100. **/
  3101. static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
  3102. {
  3103. struct i40e_pf *pf = vsi->back;
  3104. struct i40e_hw *hw = &pf->hw;
  3105. int base = vsi->base_vector;
  3106. int i;
  3107. for (i = 0; i < vsi->num_queue_pairs; i++) {
  3108. wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), 0);
  3109. wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), 0);
  3110. }
  3111. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  3112. for (i = vsi->base_vector;
  3113. i < (vsi->num_q_vectors + vsi->base_vector); i++)
  3114. wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
  3115. i40e_flush(hw);
  3116. for (i = 0; i < vsi->num_q_vectors; i++)
  3117. synchronize_irq(pf->msix_entries[i + base].vector);
  3118. } else {
  3119. /* Legacy and MSI mode - this stops all interrupt handling */
  3120. wr32(hw, I40E_PFINT_ICR0_ENA, 0);
  3121. wr32(hw, I40E_PFINT_DYN_CTL0, 0);
  3122. i40e_flush(hw);
  3123. synchronize_irq(pf->pdev->irq);
  3124. }
  3125. }
  3126. /**
  3127. * i40e_vsi_enable_irq - Enable IRQ for the given VSI
  3128. * @vsi: the VSI being configured
  3129. **/
  3130. static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
  3131. {
  3132. struct i40e_pf *pf = vsi->back;
  3133. int i;
  3134. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  3135. for (i = 0; i < vsi->num_q_vectors; i++)
  3136. i40e_irq_dynamic_enable(vsi, i);
  3137. } else {
  3138. i40e_irq_dynamic_enable_icr0(pf, true);
  3139. }
  3140. i40e_flush(&pf->hw);
  3141. return 0;
  3142. }
  3143. /**
  3144. * i40e_stop_misc_vector - Stop the vector that handles non-queue events
  3145. * @pf: board private structure
  3146. **/
  3147. static void i40e_stop_misc_vector(struct i40e_pf *pf)
  3148. {
  3149. /* Disable ICR 0 */
  3150. wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
  3151. i40e_flush(&pf->hw);
  3152. }
  3153. /**
  3154. * i40e_intr - MSI/Legacy and non-queue interrupt handler
  3155. * @irq: interrupt number
  3156. * @data: pointer to a q_vector
  3157. *
  3158. * This is the handler used for all MSI/Legacy interrupts, and deals
  3159. * with both queue and non-queue interrupts. This is also used in
  3160. * MSIX mode to handle the non-queue interrupts.
  3161. **/
  3162. static irqreturn_t i40e_intr(int irq, void *data)
  3163. {
  3164. struct i40e_pf *pf = (struct i40e_pf *)data;
  3165. struct i40e_hw *hw = &pf->hw;
  3166. irqreturn_t ret = IRQ_NONE;
  3167. u32 icr0, icr0_remaining;
  3168. u32 val, ena_mask;
  3169. icr0 = rd32(hw, I40E_PFINT_ICR0);
  3170. ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
  3171. /* if sharing a legacy IRQ, we might get called w/o an intr pending */
  3172. if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
  3173. goto enable_intr;
  3174. /* if interrupt but no bits showing, must be SWINT */
  3175. if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
  3176. (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
  3177. pf->sw_int_count++;
  3178. if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
  3179. (ena_mask & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
  3180. ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
  3181. icr0 &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
  3182. dev_dbg(&pf->pdev->dev, "cleared PE_CRITERR\n");
  3183. }
  3184. /* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
  3185. if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
  3186. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  3187. struct i40e_q_vector *q_vector = vsi->q_vectors[0];
  3188. /* We do not have a way to disarm Queue causes while leaving
  3189. * interrupt enabled for all other causes, ideally
  3190. * interrupt should be disabled while we are in NAPI but
  3191. * this is not a performance path and napi_schedule()
  3192. * can deal with rescheduling.
  3193. */
  3194. if (!test_bit(__I40E_DOWN, &pf->state))
  3195. napi_schedule_irqoff(&q_vector->napi);
  3196. }
  3197. if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
  3198. ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
  3199. set_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
  3200. i40e_debug(&pf->hw, I40E_DEBUG_NVM, "AdminQ event\n");
  3201. }
  3202. if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
  3203. ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
  3204. set_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
  3205. }
  3206. if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
  3207. ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
  3208. set_bit(__I40E_VFLR_EVENT_PENDING, &pf->state);
  3209. }
  3210. if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
  3211. if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
  3212. set_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
  3213. ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
  3214. val = rd32(hw, I40E_GLGEN_RSTAT);
  3215. val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
  3216. >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
  3217. if (val == I40E_RESET_CORER) {
  3218. pf->corer_count++;
  3219. } else if (val == I40E_RESET_GLOBR) {
  3220. pf->globr_count++;
  3221. } else if (val == I40E_RESET_EMPR) {
  3222. pf->empr_count++;
  3223. set_bit(__I40E_EMP_RESET_INTR_RECEIVED, &pf->state);
  3224. }
  3225. }
  3226. if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
  3227. icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
  3228. dev_info(&pf->pdev->dev, "HMC error interrupt\n");
  3229. dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
  3230. rd32(hw, I40E_PFHMC_ERRORINFO),
  3231. rd32(hw, I40E_PFHMC_ERRORDATA));
  3232. }
  3233. if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
  3234. u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
  3235. if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK) {
  3236. icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
  3237. i40e_ptp_tx_hwtstamp(pf);
  3238. }
  3239. }
  3240. /* If a critical error is pending we have no choice but to reset the
  3241. * device.
  3242. * Report and mask out any remaining unexpected interrupts.
  3243. */
  3244. icr0_remaining = icr0 & ena_mask;
  3245. if (icr0_remaining) {
  3246. dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
  3247. icr0_remaining);
  3248. if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
  3249. (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
  3250. (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
  3251. dev_info(&pf->pdev->dev, "device will be reset\n");
  3252. set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
  3253. i40e_service_event_schedule(pf);
  3254. }
  3255. ena_mask &= ~icr0_remaining;
  3256. }
  3257. ret = IRQ_HANDLED;
  3258. enable_intr:
  3259. /* re-enable interrupt causes */
  3260. wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
  3261. if (!test_bit(__I40E_DOWN, &pf->state)) {
  3262. i40e_service_event_schedule(pf);
  3263. i40e_irq_dynamic_enable_icr0(pf, false);
  3264. }
  3265. return ret;
  3266. }
  3267. /**
  3268. * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
  3269. * @tx_ring: tx ring to clean
  3270. * @budget: how many cleans we're allowed
  3271. *
  3272. * Returns true if there's any budget left (e.g. the clean is finished)
  3273. **/
  3274. static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
  3275. {
  3276. struct i40e_vsi *vsi = tx_ring->vsi;
  3277. u16 i = tx_ring->next_to_clean;
  3278. struct i40e_tx_buffer *tx_buf;
  3279. struct i40e_tx_desc *tx_desc;
  3280. tx_buf = &tx_ring->tx_bi[i];
  3281. tx_desc = I40E_TX_DESC(tx_ring, i);
  3282. i -= tx_ring->count;
  3283. do {
  3284. struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
  3285. /* if next_to_watch is not set then there is no work pending */
  3286. if (!eop_desc)
  3287. break;
  3288. /* prevent any other reads prior to eop_desc */
  3289. read_barrier_depends();
  3290. /* if the descriptor isn't done, no work yet to do */
  3291. if (!(eop_desc->cmd_type_offset_bsz &
  3292. cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
  3293. break;
  3294. /* clear next_to_watch to prevent false hangs */
  3295. tx_buf->next_to_watch = NULL;
  3296. tx_desc->buffer_addr = 0;
  3297. tx_desc->cmd_type_offset_bsz = 0;
  3298. /* move past filter desc */
  3299. tx_buf++;
  3300. tx_desc++;
  3301. i++;
  3302. if (unlikely(!i)) {
  3303. i -= tx_ring->count;
  3304. tx_buf = tx_ring->tx_bi;
  3305. tx_desc = I40E_TX_DESC(tx_ring, 0);
  3306. }
  3307. /* unmap skb header data */
  3308. dma_unmap_single(tx_ring->dev,
  3309. dma_unmap_addr(tx_buf, dma),
  3310. dma_unmap_len(tx_buf, len),
  3311. DMA_TO_DEVICE);
  3312. if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
  3313. kfree(tx_buf->raw_buf);
  3314. tx_buf->raw_buf = NULL;
  3315. tx_buf->tx_flags = 0;
  3316. tx_buf->next_to_watch = NULL;
  3317. dma_unmap_len_set(tx_buf, len, 0);
  3318. tx_desc->buffer_addr = 0;
  3319. tx_desc->cmd_type_offset_bsz = 0;
  3320. /* move us past the eop_desc for start of next FD desc */
  3321. tx_buf++;
  3322. tx_desc++;
  3323. i++;
  3324. if (unlikely(!i)) {
  3325. i -= tx_ring->count;
  3326. tx_buf = tx_ring->tx_bi;
  3327. tx_desc = I40E_TX_DESC(tx_ring, 0);
  3328. }
  3329. /* update budget accounting */
  3330. budget--;
  3331. } while (likely(budget));
  3332. i += tx_ring->count;
  3333. tx_ring->next_to_clean = i;
  3334. if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED)
  3335. i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
  3336. return budget > 0;
  3337. }
  3338. /**
  3339. * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
  3340. * @irq: interrupt number
  3341. * @data: pointer to a q_vector
  3342. **/
  3343. static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
  3344. {
  3345. struct i40e_q_vector *q_vector = data;
  3346. struct i40e_vsi *vsi;
  3347. if (!q_vector->tx.ring)
  3348. return IRQ_HANDLED;
  3349. vsi = q_vector->tx.ring->vsi;
  3350. i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
  3351. return IRQ_HANDLED;
  3352. }
  3353. /**
  3354. * i40e_map_vector_to_qp - Assigns the queue pair to the vector
  3355. * @vsi: the VSI being configured
  3356. * @v_idx: vector index
  3357. * @qp_idx: queue pair index
  3358. **/
  3359. static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
  3360. {
  3361. struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
  3362. struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
  3363. struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
  3364. tx_ring->q_vector = q_vector;
  3365. tx_ring->next = q_vector->tx.ring;
  3366. q_vector->tx.ring = tx_ring;
  3367. q_vector->tx.count++;
  3368. rx_ring->q_vector = q_vector;
  3369. rx_ring->next = q_vector->rx.ring;
  3370. q_vector->rx.ring = rx_ring;
  3371. q_vector->rx.count++;
  3372. }
  3373. /**
  3374. * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
  3375. * @vsi: the VSI being configured
  3376. *
  3377. * This function maps descriptor rings to the queue-specific vectors
  3378. * we were allotted through the MSI-X enabling code. Ideally, we'd have
  3379. * one vector per queue pair, but on a constrained vector budget, we
  3380. * group the queue pairs as "efficiently" as possible.
  3381. **/
  3382. static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
  3383. {
  3384. int qp_remaining = vsi->num_queue_pairs;
  3385. int q_vectors = vsi->num_q_vectors;
  3386. int num_ringpairs;
  3387. int v_start = 0;
  3388. int qp_idx = 0;
  3389. /* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
  3390. * group them so there are multiple queues per vector.
  3391. * It is also important to go through all the vectors available to be
  3392. * sure that if we don't use all the vectors, that the remaining vectors
  3393. * are cleared. This is especially important when decreasing the
  3394. * number of queues in use.
  3395. */
  3396. for (; v_start < q_vectors; v_start++) {
  3397. struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
  3398. num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
  3399. q_vector->num_ringpairs = num_ringpairs;
  3400. q_vector->rx.count = 0;
  3401. q_vector->tx.count = 0;
  3402. q_vector->rx.ring = NULL;
  3403. q_vector->tx.ring = NULL;
  3404. while (num_ringpairs--) {
  3405. i40e_map_vector_to_qp(vsi, v_start, qp_idx);
  3406. qp_idx++;
  3407. qp_remaining--;
  3408. }
  3409. }
  3410. }
  3411. /**
  3412. * i40e_vsi_request_irq - Request IRQ from the OS
  3413. * @vsi: the VSI being configured
  3414. * @basename: name for the vector
  3415. **/
  3416. static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
  3417. {
  3418. struct i40e_pf *pf = vsi->back;
  3419. int err;
  3420. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  3421. err = i40e_vsi_request_irq_msix(vsi, basename);
  3422. else if (pf->flags & I40E_FLAG_MSI_ENABLED)
  3423. err = request_irq(pf->pdev->irq, i40e_intr, 0,
  3424. pf->int_name, pf);
  3425. else
  3426. err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
  3427. pf->int_name, pf);
  3428. if (err)
  3429. dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
  3430. return err;
  3431. }
  3432. #ifdef CONFIG_NET_POLL_CONTROLLER
  3433. /**
  3434. * i40e_netpoll - A Polling 'interrupt' handler
  3435. * @netdev: network interface device structure
  3436. *
  3437. * This is used by netconsole to send skbs without having to re-enable
  3438. * interrupts. It's not called while the normal interrupt routine is executing.
  3439. **/
  3440. static void i40e_netpoll(struct net_device *netdev)
  3441. {
  3442. struct i40e_netdev_priv *np = netdev_priv(netdev);
  3443. struct i40e_vsi *vsi = np->vsi;
  3444. struct i40e_pf *pf = vsi->back;
  3445. int i;
  3446. /* if interface is down do nothing */
  3447. if (test_bit(__I40E_DOWN, &vsi->state))
  3448. return;
  3449. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  3450. for (i = 0; i < vsi->num_q_vectors; i++)
  3451. i40e_msix_clean_rings(0, vsi->q_vectors[i]);
  3452. } else {
  3453. i40e_intr(pf->pdev->irq, netdev);
  3454. }
  3455. }
  3456. #endif
  3457. #define I40E_QTX_ENA_WAIT_COUNT 50
  3458. /**
  3459. * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
  3460. * @pf: the PF being configured
  3461. * @pf_q: the PF queue
  3462. * @enable: enable or disable state of the queue
  3463. *
  3464. * This routine will wait for the given Tx queue of the PF to reach the
  3465. * enabled or disabled state.
  3466. * Returns -ETIMEDOUT in case of failing to reach the requested state after
  3467. * multiple retries; else will return 0 in case of success.
  3468. **/
  3469. static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
  3470. {
  3471. int i;
  3472. u32 tx_reg;
  3473. for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
  3474. tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
  3475. if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
  3476. break;
  3477. usleep_range(10, 20);
  3478. }
  3479. if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
  3480. return -ETIMEDOUT;
  3481. return 0;
  3482. }
  3483. /**
  3484. * i40e_control_tx_q - Start or stop a particular Tx queue
  3485. * @pf: the PF structure
  3486. * @pf_q: the PF queue to configure
  3487. * @enable: start or stop the queue
  3488. *
  3489. * This function enables or disables a single queue. Note that any delay
  3490. * required after the operation is expected to be handled by the caller of
  3491. * this function.
  3492. **/
  3493. static void i40e_control_tx_q(struct i40e_pf *pf, int pf_q, bool enable)
  3494. {
  3495. struct i40e_hw *hw = &pf->hw;
  3496. u32 tx_reg;
  3497. int i;
  3498. /* warn the TX unit of coming changes */
  3499. i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
  3500. if (!enable)
  3501. usleep_range(10, 20);
  3502. for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
  3503. tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
  3504. if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
  3505. ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
  3506. break;
  3507. usleep_range(1000, 2000);
  3508. }
  3509. /* Skip if the queue is already in the requested state */
  3510. if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
  3511. return;
  3512. /* turn on/off the queue */
  3513. if (enable) {
  3514. wr32(hw, I40E_QTX_HEAD(pf_q), 0);
  3515. tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
  3516. } else {
  3517. tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
  3518. }
  3519. wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
  3520. }
  3521. /**
  3522. * i40e_vsi_control_tx - Start or stop a VSI's rings
  3523. * @vsi: the VSI being configured
  3524. * @enable: start or stop the rings
  3525. **/
  3526. static int i40e_vsi_control_tx(struct i40e_vsi *vsi, bool enable)
  3527. {
  3528. struct i40e_pf *pf = vsi->back;
  3529. int i, pf_q, ret = 0;
  3530. pf_q = vsi->base_queue;
  3531. for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
  3532. i40e_control_tx_q(pf, pf_q, enable);
  3533. /* wait for the change to finish */
  3534. ret = i40e_pf_txq_wait(pf, pf_q, enable);
  3535. if (ret) {
  3536. dev_info(&pf->pdev->dev,
  3537. "VSI seid %d Tx ring %d %sable timeout\n",
  3538. vsi->seid, pf_q, (enable ? "en" : "dis"));
  3539. break;
  3540. }
  3541. }
  3542. return ret;
  3543. }
  3544. /**
  3545. * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
  3546. * @pf: the PF being configured
  3547. * @pf_q: the PF queue
  3548. * @enable: enable or disable state of the queue
  3549. *
  3550. * This routine will wait for the given Rx queue of the PF to reach the
  3551. * enabled or disabled state.
  3552. * Returns -ETIMEDOUT in case of failing to reach the requested state after
  3553. * multiple retries; else will return 0 in case of success.
  3554. **/
  3555. static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
  3556. {
  3557. int i;
  3558. u32 rx_reg;
  3559. for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
  3560. rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
  3561. if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
  3562. break;
  3563. usleep_range(10, 20);
  3564. }
  3565. if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
  3566. return -ETIMEDOUT;
  3567. return 0;
  3568. }
  3569. /**
  3570. * i40e_control_rx_q - Start or stop a particular Rx queue
  3571. * @pf: the PF structure
  3572. * @pf_q: the PF queue to configure
  3573. * @enable: start or stop the queue
  3574. *
  3575. * This function enables or disables a single queue. Note that any delay
  3576. * required after the operation is expected to be handled by the caller of
  3577. * this function.
  3578. **/
  3579. static void i40e_control_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
  3580. {
  3581. struct i40e_hw *hw = &pf->hw;
  3582. u32 rx_reg;
  3583. int i;
  3584. for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
  3585. rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
  3586. if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
  3587. ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
  3588. break;
  3589. usleep_range(1000, 2000);
  3590. }
  3591. /* Skip if the queue is already in the requested state */
  3592. if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
  3593. return;
  3594. /* turn on/off the queue */
  3595. if (enable)
  3596. rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
  3597. else
  3598. rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
  3599. wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
  3600. }
  3601. /**
  3602. * i40e_vsi_control_rx - Start or stop a VSI's rings
  3603. * @vsi: the VSI being configured
  3604. * @enable: start or stop the rings
  3605. **/
  3606. static int i40e_vsi_control_rx(struct i40e_vsi *vsi, bool enable)
  3607. {
  3608. struct i40e_pf *pf = vsi->back;
  3609. int i, pf_q, ret = 0;
  3610. pf_q = vsi->base_queue;
  3611. for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
  3612. i40e_control_rx_q(pf, pf_q, enable);
  3613. /* wait for the change to finish */
  3614. ret = i40e_pf_rxq_wait(pf, pf_q, enable);
  3615. if (ret) {
  3616. dev_info(&pf->pdev->dev,
  3617. "VSI seid %d Rx ring %d %sable timeout\n",
  3618. vsi->seid, pf_q, (enable ? "en" : "dis"));
  3619. break;
  3620. }
  3621. }
  3622. /* Due to HW errata, on Rx disable only, the register can indicate done
  3623. * before it really is. Needs 50ms to be sure
  3624. */
  3625. if (!enable)
  3626. mdelay(50);
  3627. return ret;
  3628. }
  3629. /**
  3630. * i40e_vsi_start_rings - Start a VSI's rings
  3631. * @vsi: the VSI being configured
  3632. **/
  3633. int i40e_vsi_start_rings(struct i40e_vsi *vsi)
  3634. {
  3635. int ret = 0;
  3636. /* do rx first for enable and last for disable */
  3637. ret = i40e_vsi_control_rx(vsi, true);
  3638. if (ret)
  3639. return ret;
  3640. ret = i40e_vsi_control_tx(vsi, true);
  3641. return ret;
  3642. }
  3643. /**
  3644. * i40e_vsi_stop_rings - Stop a VSI's rings
  3645. * @vsi: the VSI being configured
  3646. **/
  3647. void i40e_vsi_stop_rings(struct i40e_vsi *vsi)
  3648. {
  3649. /* When port TX is suspended, don't wait */
  3650. if (test_bit(__I40E_PORT_SUSPENDED, &vsi->back->state))
  3651. return i40e_vsi_stop_rings_no_wait(vsi);
  3652. /* do rx first for enable and last for disable
  3653. * Ignore return value, we need to shutdown whatever we can
  3654. */
  3655. i40e_vsi_control_tx(vsi, false);
  3656. i40e_vsi_control_rx(vsi, false);
  3657. }
  3658. /**
  3659. * i40e_vsi_stop_rings_no_wait - Stop a VSI's rings and do not delay
  3660. * @vsi: the VSI being shutdown
  3661. *
  3662. * This function stops all the rings for a VSI but does not delay to verify
  3663. * that rings have been disabled. It is expected that the caller is shutting
  3664. * down multiple VSIs at once and will delay together for all the VSIs after
  3665. * initiating the shutdown. This is particularly useful for shutting down lots
  3666. * of VFs together. Otherwise, a large delay can be incurred while configuring
  3667. * each VSI in serial.
  3668. **/
  3669. void i40e_vsi_stop_rings_no_wait(struct i40e_vsi *vsi)
  3670. {
  3671. struct i40e_pf *pf = vsi->back;
  3672. int i, pf_q;
  3673. pf_q = vsi->base_queue;
  3674. for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
  3675. i40e_control_tx_q(pf, pf_q, false);
  3676. i40e_control_rx_q(pf, pf_q, false);
  3677. }
  3678. }
  3679. /**
  3680. * i40e_vsi_free_irq - Free the irq association with the OS
  3681. * @vsi: the VSI being configured
  3682. **/
  3683. static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
  3684. {
  3685. struct i40e_pf *pf = vsi->back;
  3686. struct i40e_hw *hw = &pf->hw;
  3687. int base = vsi->base_vector;
  3688. u32 val, qp;
  3689. int i;
  3690. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  3691. if (!vsi->q_vectors)
  3692. return;
  3693. if (!vsi->irqs_ready)
  3694. return;
  3695. vsi->irqs_ready = false;
  3696. for (i = 0; i < vsi->num_q_vectors; i++) {
  3697. int irq_num;
  3698. u16 vector;
  3699. vector = i + base;
  3700. irq_num = pf->msix_entries[vector].vector;
  3701. /* free only the irqs that were actually requested */
  3702. if (!vsi->q_vectors[i] ||
  3703. !vsi->q_vectors[i]->num_ringpairs)
  3704. continue;
  3705. /* clear the affinity notifier in the IRQ descriptor */
  3706. irq_set_affinity_notifier(irq_num, NULL);
  3707. /* clear the affinity_mask in the IRQ descriptor */
  3708. irq_set_affinity_hint(irq_num, NULL);
  3709. synchronize_irq(irq_num);
  3710. free_irq(irq_num, vsi->q_vectors[i]);
  3711. /* Tear down the interrupt queue link list
  3712. *
  3713. * We know that they come in pairs and always
  3714. * the Rx first, then the Tx. To clear the
  3715. * link list, stick the EOL value into the
  3716. * next_q field of the registers.
  3717. */
  3718. val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
  3719. qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
  3720. >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
  3721. val |= I40E_QUEUE_END_OF_LIST
  3722. << I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
  3723. wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
  3724. while (qp != I40E_QUEUE_END_OF_LIST) {
  3725. u32 next;
  3726. val = rd32(hw, I40E_QINT_RQCTL(qp));
  3727. val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
  3728. I40E_QINT_RQCTL_MSIX0_INDX_MASK |
  3729. I40E_QINT_RQCTL_CAUSE_ENA_MASK |
  3730. I40E_QINT_RQCTL_INTEVENT_MASK);
  3731. val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
  3732. I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
  3733. wr32(hw, I40E_QINT_RQCTL(qp), val);
  3734. val = rd32(hw, I40E_QINT_TQCTL(qp));
  3735. next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
  3736. >> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
  3737. val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
  3738. I40E_QINT_TQCTL_MSIX0_INDX_MASK |
  3739. I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  3740. I40E_QINT_TQCTL_INTEVENT_MASK);
  3741. val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
  3742. I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
  3743. wr32(hw, I40E_QINT_TQCTL(qp), val);
  3744. qp = next;
  3745. }
  3746. }
  3747. } else {
  3748. free_irq(pf->pdev->irq, pf);
  3749. val = rd32(hw, I40E_PFINT_LNKLST0);
  3750. qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
  3751. >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
  3752. val |= I40E_QUEUE_END_OF_LIST
  3753. << I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
  3754. wr32(hw, I40E_PFINT_LNKLST0, val);
  3755. val = rd32(hw, I40E_QINT_RQCTL(qp));
  3756. val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
  3757. I40E_QINT_RQCTL_MSIX0_INDX_MASK |
  3758. I40E_QINT_RQCTL_CAUSE_ENA_MASK |
  3759. I40E_QINT_RQCTL_INTEVENT_MASK);
  3760. val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
  3761. I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
  3762. wr32(hw, I40E_QINT_RQCTL(qp), val);
  3763. val = rd32(hw, I40E_QINT_TQCTL(qp));
  3764. val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
  3765. I40E_QINT_TQCTL_MSIX0_INDX_MASK |
  3766. I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  3767. I40E_QINT_TQCTL_INTEVENT_MASK);
  3768. val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
  3769. I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
  3770. wr32(hw, I40E_QINT_TQCTL(qp), val);
  3771. }
  3772. }
  3773. /**
  3774. * i40e_free_q_vector - Free memory allocated for specific interrupt vector
  3775. * @vsi: the VSI being configured
  3776. * @v_idx: Index of vector to be freed
  3777. *
  3778. * This function frees the memory allocated to the q_vector. In addition if
  3779. * NAPI is enabled it will delete any references to the NAPI struct prior
  3780. * to freeing the q_vector.
  3781. **/
  3782. static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
  3783. {
  3784. struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
  3785. struct i40e_ring *ring;
  3786. if (!q_vector)
  3787. return;
  3788. /* disassociate q_vector from rings */
  3789. i40e_for_each_ring(ring, q_vector->tx)
  3790. ring->q_vector = NULL;
  3791. i40e_for_each_ring(ring, q_vector->rx)
  3792. ring->q_vector = NULL;
  3793. /* only VSI w/ an associated netdev is set up w/ NAPI */
  3794. if (vsi->netdev)
  3795. netif_napi_del(&q_vector->napi);
  3796. vsi->q_vectors[v_idx] = NULL;
  3797. kfree_rcu(q_vector, rcu);
  3798. }
  3799. /**
  3800. * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
  3801. * @vsi: the VSI being un-configured
  3802. *
  3803. * This frees the memory allocated to the q_vectors and
  3804. * deletes references to the NAPI struct.
  3805. **/
  3806. static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
  3807. {
  3808. int v_idx;
  3809. for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
  3810. i40e_free_q_vector(vsi, v_idx);
  3811. }
  3812. /**
  3813. * i40e_reset_interrupt_capability - Disable interrupt setup in OS
  3814. * @pf: board private structure
  3815. **/
  3816. static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
  3817. {
  3818. /* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
  3819. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  3820. pci_disable_msix(pf->pdev);
  3821. kfree(pf->msix_entries);
  3822. pf->msix_entries = NULL;
  3823. kfree(pf->irq_pile);
  3824. pf->irq_pile = NULL;
  3825. } else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
  3826. pci_disable_msi(pf->pdev);
  3827. }
  3828. pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
  3829. }
  3830. /**
  3831. * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
  3832. * @pf: board private structure
  3833. *
  3834. * We go through and clear interrupt specific resources and reset the structure
  3835. * to pre-load conditions
  3836. **/
  3837. static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
  3838. {
  3839. int i;
  3840. i40e_stop_misc_vector(pf);
  3841. if (pf->flags & I40E_FLAG_MSIX_ENABLED && pf->msix_entries) {
  3842. synchronize_irq(pf->msix_entries[0].vector);
  3843. free_irq(pf->msix_entries[0].vector, pf);
  3844. }
  3845. i40e_put_lump(pf->irq_pile, pf->iwarp_base_vector,
  3846. I40E_IWARP_IRQ_PILE_ID);
  3847. i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
  3848. for (i = 0; i < pf->num_alloc_vsi; i++)
  3849. if (pf->vsi[i])
  3850. i40e_vsi_free_q_vectors(pf->vsi[i]);
  3851. i40e_reset_interrupt_capability(pf);
  3852. }
  3853. /**
  3854. * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
  3855. * @vsi: the VSI being configured
  3856. **/
  3857. static void i40e_napi_enable_all(struct i40e_vsi *vsi)
  3858. {
  3859. int q_idx;
  3860. if (!vsi->netdev)
  3861. return;
  3862. for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
  3863. struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
  3864. if (q_vector->rx.ring || q_vector->tx.ring)
  3865. napi_enable(&q_vector->napi);
  3866. }
  3867. }
  3868. /**
  3869. * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
  3870. * @vsi: the VSI being configured
  3871. **/
  3872. static void i40e_napi_disable_all(struct i40e_vsi *vsi)
  3873. {
  3874. int q_idx;
  3875. if (!vsi->netdev)
  3876. return;
  3877. for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
  3878. struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
  3879. if (q_vector->rx.ring || q_vector->tx.ring)
  3880. napi_disable(&q_vector->napi);
  3881. }
  3882. }
  3883. /**
  3884. * i40e_vsi_close - Shut down a VSI
  3885. * @vsi: the vsi to be quelled
  3886. **/
  3887. static void i40e_vsi_close(struct i40e_vsi *vsi)
  3888. {
  3889. struct i40e_pf *pf = vsi->back;
  3890. if (!test_and_set_bit(__I40E_DOWN, &vsi->state))
  3891. i40e_down(vsi);
  3892. i40e_vsi_free_irq(vsi);
  3893. i40e_vsi_free_tx_resources(vsi);
  3894. i40e_vsi_free_rx_resources(vsi);
  3895. vsi->current_netdev_flags = 0;
  3896. pf->flags |= I40E_FLAG_SERVICE_CLIENT_REQUESTED;
  3897. if (test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
  3898. pf->flags |= I40E_FLAG_CLIENT_RESET;
  3899. }
  3900. /**
  3901. * i40e_quiesce_vsi - Pause a given VSI
  3902. * @vsi: the VSI being paused
  3903. **/
  3904. static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
  3905. {
  3906. if (test_bit(__I40E_DOWN, &vsi->state))
  3907. return;
  3908. set_bit(__I40E_NEEDS_RESTART, &vsi->state);
  3909. if (vsi->netdev && netif_running(vsi->netdev))
  3910. vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
  3911. else
  3912. i40e_vsi_close(vsi);
  3913. }
  3914. /**
  3915. * i40e_unquiesce_vsi - Resume a given VSI
  3916. * @vsi: the VSI being resumed
  3917. **/
  3918. static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
  3919. {
  3920. if (!test_bit(__I40E_NEEDS_RESTART, &vsi->state))
  3921. return;
  3922. clear_bit(__I40E_NEEDS_RESTART, &vsi->state);
  3923. if (vsi->netdev && netif_running(vsi->netdev))
  3924. vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
  3925. else
  3926. i40e_vsi_open(vsi); /* this clears the DOWN bit */
  3927. }
  3928. /**
  3929. * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
  3930. * @pf: the PF
  3931. **/
  3932. static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
  3933. {
  3934. int v;
  3935. for (v = 0; v < pf->num_alloc_vsi; v++) {
  3936. if (pf->vsi[v])
  3937. i40e_quiesce_vsi(pf->vsi[v]);
  3938. }
  3939. }
  3940. /**
  3941. * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
  3942. * @pf: the PF
  3943. **/
  3944. static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
  3945. {
  3946. int v;
  3947. for (v = 0; v < pf->num_alloc_vsi; v++) {
  3948. if (pf->vsi[v])
  3949. i40e_unquiesce_vsi(pf->vsi[v]);
  3950. }
  3951. }
  3952. /**
  3953. * i40e_vsi_wait_queues_disabled - Wait for VSI's queues to be disabled
  3954. * @vsi: the VSI being configured
  3955. *
  3956. * Wait until all queues on a given VSI have been disabled.
  3957. **/
  3958. int i40e_vsi_wait_queues_disabled(struct i40e_vsi *vsi)
  3959. {
  3960. struct i40e_pf *pf = vsi->back;
  3961. int i, pf_q, ret;
  3962. pf_q = vsi->base_queue;
  3963. for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
  3964. /* Check and wait for the Tx queue */
  3965. ret = i40e_pf_txq_wait(pf, pf_q, false);
  3966. if (ret) {
  3967. dev_info(&pf->pdev->dev,
  3968. "VSI seid %d Tx ring %d disable timeout\n",
  3969. vsi->seid, pf_q);
  3970. return ret;
  3971. }
  3972. /* Check and wait for the Tx queue */
  3973. ret = i40e_pf_rxq_wait(pf, pf_q, false);
  3974. if (ret) {
  3975. dev_info(&pf->pdev->dev,
  3976. "VSI seid %d Rx ring %d disable timeout\n",
  3977. vsi->seid, pf_q);
  3978. return ret;
  3979. }
  3980. }
  3981. return 0;
  3982. }
  3983. #ifdef CONFIG_I40E_DCB
  3984. /**
  3985. * i40e_pf_wait_queues_disabled - Wait for all queues of PF VSIs to be disabled
  3986. * @pf: the PF
  3987. *
  3988. * This function waits for the queues to be in disabled state for all the
  3989. * VSIs that are managed by this PF.
  3990. **/
  3991. static int i40e_pf_wait_queues_disabled(struct i40e_pf *pf)
  3992. {
  3993. int v, ret = 0;
  3994. for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
  3995. if (pf->vsi[v]) {
  3996. ret = i40e_vsi_wait_queues_disabled(pf->vsi[v]);
  3997. if (ret)
  3998. break;
  3999. }
  4000. }
  4001. return ret;
  4002. }
  4003. #endif
  4004. /**
  4005. * i40e_detect_recover_hung_queue - Function to detect and recover hung_queue
  4006. * @q_idx: TX queue number
  4007. * @vsi: Pointer to VSI struct
  4008. *
  4009. * This function checks specified queue for given VSI. Detects hung condition.
  4010. * We proactively detect hung TX queues by checking if interrupts are disabled
  4011. * but there are pending descriptors. If it appears hung, attempt to recover
  4012. * by triggering a SW interrupt.
  4013. **/
  4014. static void i40e_detect_recover_hung_queue(int q_idx, struct i40e_vsi *vsi)
  4015. {
  4016. struct i40e_ring *tx_ring = NULL;
  4017. struct i40e_pf *pf;
  4018. u32 val, tx_pending;
  4019. int i;
  4020. pf = vsi->back;
  4021. /* now that we have an index, find the tx_ring struct */
  4022. for (i = 0; i < vsi->num_queue_pairs; i++) {
  4023. if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
  4024. if (q_idx == vsi->tx_rings[i]->queue_index) {
  4025. tx_ring = vsi->tx_rings[i];
  4026. break;
  4027. }
  4028. }
  4029. }
  4030. if (!tx_ring)
  4031. return;
  4032. /* Read interrupt register */
  4033. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  4034. val = rd32(&pf->hw,
  4035. I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
  4036. tx_ring->vsi->base_vector - 1));
  4037. else
  4038. val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
  4039. tx_pending = i40e_get_tx_pending(tx_ring);
  4040. /* Interrupts are disabled and TX pending is non-zero,
  4041. * trigger the SW interrupt (don't wait). Worst case
  4042. * there will be one extra interrupt which may result
  4043. * into not cleaning any queues because queues are cleaned.
  4044. */
  4045. if (tx_pending && (!(val & I40E_PFINT_DYN_CTLN_INTENA_MASK)))
  4046. i40e_force_wb(vsi, tx_ring->q_vector);
  4047. }
  4048. /**
  4049. * i40e_detect_recover_hung - Function to detect and recover hung_queues
  4050. * @pf: pointer to PF struct
  4051. *
  4052. * LAN VSI has netdev and netdev has TX queues. This function is to check
  4053. * each of those TX queues if they are hung, trigger recovery by issuing
  4054. * SW interrupt.
  4055. **/
  4056. static void i40e_detect_recover_hung(struct i40e_pf *pf)
  4057. {
  4058. struct net_device *netdev;
  4059. struct i40e_vsi *vsi;
  4060. int i;
  4061. /* Only for LAN VSI */
  4062. vsi = pf->vsi[pf->lan_vsi];
  4063. if (!vsi)
  4064. return;
  4065. /* Make sure, VSI state is not DOWN/RECOVERY_PENDING */
  4066. if (test_bit(__I40E_DOWN, &vsi->back->state) ||
  4067. test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
  4068. return;
  4069. /* Make sure type is MAIN VSI */
  4070. if (vsi->type != I40E_VSI_MAIN)
  4071. return;
  4072. netdev = vsi->netdev;
  4073. if (!netdev)
  4074. return;
  4075. /* Bail out if netif_carrier is not OK */
  4076. if (!netif_carrier_ok(netdev))
  4077. return;
  4078. /* Go thru' TX queues for netdev */
  4079. for (i = 0; i < netdev->num_tx_queues; i++) {
  4080. struct netdev_queue *q;
  4081. q = netdev_get_tx_queue(netdev, i);
  4082. if (q)
  4083. i40e_detect_recover_hung_queue(i, vsi);
  4084. }
  4085. }
  4086. /**
  4087. * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
  4088. * @pf: pointer to PF
  4089. *
  4090. * Get TC map for ISCSI PF type that will include iSCSI TC
  4091. * and LAN TC.
  4092. **/
  4093. static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
  4094. {
  4095. struct i40e_dcb_app_priority_table app;
  4096. struct i40e_hw *hw = &pf->hw;
  4097. u8 enabled_tc = 1; /* TC0 is always enabled */
  4098. u8 tc, i;
  4099. /* Get the iSCSI APP TLV */
  4100. struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
  4101. for (i = 0; i < dcbcfg->numapps; i++) {
  4102. app = dcbcfg->app[i];
  4103. if (app.selector == I40E_APP_SEL_TCPIP &&
  4104. app.protocolid == I40E_APP_PROTOID_ISCSI) {
  4105. tc = dcbcfg->etscfg.prioritytable[app.priority];
  4106. enabled_tc |= BIT(tc);
  4107. break;
  4108. }
  4109. }
  4110. return enabled_tc;
  4111. }
  4112. /**
  4113. * i40e_dcb_get_num_tc - Get the number of TCs from DCBx config
  4114. * @dcbcfg: the corresponding DCBx configuration structure
  4115. *
  4116. * Return the number of TCs from given DCBx configuration
  4117. **/
  4118. static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
  4119. {
  4120. int i, tc_unused = 0;
  4121. u8 num_tc = 0;
  4122. u8 ret = 0;
  4123. /* Scan the ETS Config Priority Table to find
  4124. * traffic class enabled for a given priority
  4125. * and create a bitmask of enabled TCs
  4126. */
  4127. for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
  4128. num_tc |= BIT(dcbcfg->etscfg.prioritytable[i]);
  4129. /* Now scan the bitmask to check for
  4130. * contiguous TCs starting with TC0
  4131. */
  4132. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4133. if (num_tc & BIT(i)) {
  4134. if (!tc_unused) {
  4135. ret++;
  4136. } else {
  4137. pr_err("Non-contiguous TC - Disabling DCB\n");
  4138. return 1;
  4139. }
  4140. } else {
  4141. tc_unused = 1;
  4142. }
  4143. }
  4144. /* There is always at least TC0 */
  4145. if (!ret)
  4146. ret = 1;
  4147. return ret;
  4148. }
  4149. /**
  4150. * i40e_dcb_get_enabled_tc - Get enabled traffic classes
  4151. * @dcbcfg: the corresponding DCBx configuration structure
  4152. *
  4153. * Query the current DCB configuration and return the number of
  4154. * traffic classes enabled from the given DCBX config
  4155. **/
  4156. static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
  4157. {
  4158. u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
  4159. u8 enabled_tc = 1;
  4160. u8 i;
  4161. for (i = 0; i < num_tc; i++)
  4162. enabled_tc |= BIT(i);
  4163. return enabled_tc;
  4164. }
  4165. /**
  4166. * i40e_pf_get_num_tc - Get enabled traffic classes for PF
  4167. * @pf: PF being queried
  4168. *
  4169. * Return number of traffic classes enabled for the given PF
  4170. **/
  4171. static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
  4172. {
  4173. struct i40e_hw *hw = &pf->hw;
  4174. u8 i, enabled_tc = 1;
  4175. u8 num_tc = 0;
  4176. struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
  4177. /* If DCB is not enabled then always in single TC */
  4178. if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
  4179. return 1;
  4180. /* SFP mode will be enabled for all TCs on port */
  4181. if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
  4182. return i40e_dcb_get_num_tc(dcbcfg);
  4183. /* MFP mode return count of enabled TCs for this PF */
  4184. if (pf->hw.func_caps.iscsi)
  4185. enabled_tc = i40e_get_iscsi_tc_map(pf);
  4186. else
  4187. return 1; /* Only TC0 */
  4188. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4189. if (enabled_tc & BIT(i))
  4190. num_tc++;
  4191. }
  4192. return num_tc;
  4193. }
  4194. /**
  4195. * i40e_pf_get_pf_tc_map - Get bitmap for enabled traffic classes
  4196. * @pf: PF being queried
  4197. *
  4198. * Return a bitmap for enabled traffic classes for this PF.
  4199. **/
  4200. static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
  4201. {
  4202. /* If DCB is not enabled for this PF then just return default TC */
  4203. if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
  4204. return I40E_DEFAULT_TRAFFIC_CLASS;
  4205. /* SFP mode we want PF to be enabled for all TCs */
  4206. if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
  4207. return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
  4208. /* MFP enabled and iSCSI PF type */
  4209. if (pf->hw.func_caps.iscsi)
  4210. return i40e_get_iscsi_tc_map(pf);
  4211. else
  4212. return I40E_DEFAULT_TRAFFIC_CLASS;
  4213. }
  4214. /**
  4215. * i40e_vsi_get_bw_info - Query VSI BW Information
  4216. * @vsi: the VSI being queried
  4217. *
  4218. * Returns 0 on success, negative value on failure
  4219. **/
  4220. static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
  4221. {
  4222. struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
  4223. struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
  4224. struct i40e_pf *pf = vsi->back;
  4225. struct i40e_hw *hw = &pf->hw;
  4226. i40e_status ret;
  4227. u32 tc_bw_max;
  4228. int i;
  4229. /* Get the VSI level BW configuration */
  4230. ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
  4231. if (ret) {
  4232. dev_info(&pf->pdev->dev,
  4233. "couldn't get PF vsi bw config, err %s aq_err %s\n",
  4234. i40e_stat_str(&pf->hw, ret),
  4235. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  4236. return -EINVAL;
  4237. }
  4238. /* Get the VSI level BW configuration per TC */
  4239. ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
  4240. NULL);
  4241. if (ret) {
  4242. dev_info(&pf->pdev->dev,
  4243. "couldn't get PF vsi ets bw config, err %s aq_err %s\n",
  4244. i40e_stat_str(&pf->hw, ret),
  4245. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  4246. return -EINVAL;
  4247. }
  4248. if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
  4249. dev_info(&pf->pdev->dev,
  4250. "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
  4251. bw_config.tc_valid_bits,
  4252. bw_ets_config.tc_valid_bits);
  4253. /* Still continuing */
  4254. }
  4255. vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
  4256. vsi->bw_max_quanta = bw_config.max_bw;
  4257. tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
  4258. (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
  4259. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4260. vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
  4261. vsi->bw_ets_limit_credits[i] =
  4262. le16_to_cpu(bw_ets_config.credits[i]);
  4263. /* 3 bits out of 4 for each TC */
  4264. vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
  4265. }
  4266. return 0;
  4267. }
  4268. /**
  4269. * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
  4270. * @vsi: the VSI being configured
  4271. * @enabled_tc: TC bitmap
  4272. * @bw_credits: BW shared credits per TC
  4273. *
  4274. * Returns 0 on success, negative value on failure
  4275. **/
  4276. static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
  4277. u8 *bw_share)
  4278. {
  4279. struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
  4280. i40e_status ret;
  4281. int i;
  4282. bw_data.tc_valid_bits = enabled_tc;
  4283. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
  4284. bw_data.tc_bw_credits[i] = bw_share[i];
  4285. ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, vsi->seid, &bw_data,
  4286. NULL);
  4287. if (ret) {
  4288. dev_info(&vsi->back->pdev->dev,
  4289. "AQ command Config VSI BW allocation per TC failed = %d\n",
  4290. vsi->back->hw.aq.asq_last_status);
  4291. return -EINVAL;
  4292. }
  4293. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
  4294. vsi->info.qs_handle[i] = bw_data.qs_handles[i];
  4295. return 0;
  4296. }
  4297. /**
  4298. * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
  4299. * @vsi: the VSI being configured
  4300. * @enabled_tc: TC map to be enabled
  4301. *
  4302. **/
  4303. static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
  4304. {
  4305. struct net_device *netdev = vsi->netdev;
  4306. struct i40e_pf *pf = vsi->back;
  4307. struct i40e_hw *hw = &pf->hw;
  4308. u8 netdev_tc = 0;
  4309. int i;
  4310. struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
  4311. if (!netdev)
  4312. return;
  4313. if (!enabled_tc) {
  4314. netdev_reset_tc(netdev);
  4315. return;
  4316. }
  4317. /* Set up actual enabled TCs on the VSI */
  4318. if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
  4319. return;
  4320. /* set per TC queues for the VSI */
  4321. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4322. /* Only set TC queues for enabled tcs
  4323. *
  4324. * e.g. For a VSI that has TC0 and TC3 enabled the
  4325. * enabled_tc bitmap would be 0x00001001; the driver
  4326. * will set the numtc for netdev as 2 that will be
  4327. * referenced by the netdev layer as TC 0 and 1.
  4328. */
  4329. if (vsi->tc_config.enabled_tc & BIT(i))
  4330. netdev_set_tc_queue(netdev,
  4331. vsi->tc_config.tc_info[i].netdev_tc,
  4332. vsi->tc_config.tc_info[i].qcount,
  4333. vsi->tc_config.tc_info[i].qoffset);
  4334. }
  4335. /* Assign UP2TC map for the VSI */
  4336. for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
  4337. /* Get the actual TC# for the UP */
  4338. u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
  4339. /* Get the mapped netdev TC# for the UP */
  4340. netdev_tc = vsi->tc_config.tc_info[ets_tc].netdev_tc;
  4341. netdev_set_prio_tc_map(netdev, i, netdev_tc);
  4342. }
  4343. }
  4344. /**
  4345. * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
  4346. * @vsi: the VSI being configured
  4347. * @ctxt: the ctxt buffer returned from AQ VSI update param command
  4348. **/
  4349. static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
  4350. struct i40e_vsi_context *ctxt)
  4351. {
  4352. /* copy just the sections touched not the entire info
  4353. * since not all sections are valid as returned by
  4354. * update vsi params
  4355. */
  4356. vsi->info.mapping_flags = ctxt->info.mapping_flags;
  4357. memcpy(&vsi->info.queue_mapping,
  4358. &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
  4359. memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
  4360. sizeof(vsi->info.tc_mapping));
  4361. }
  4362. /**
  4363. * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
  4364. * @vsi: VSI to be configured
  4365. * @enabled_tc: TC bitmap
  4366. *
  4367. * This configures a particular VSI for TCs that are mapped to the
  4368. * given TC bitmap. It uses default bandwidth share for TCs across
  4369. * VSIs to configure TC for a particular VSI.
  4370. *
  4371. * NOTE:
  4372. * It is expected that the VSI queues have been quisced before calling
  4373. * this function.
  4374. **/
  4375. static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
  4376. {
  4377. u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
  4378. struct i40e_vsi_context ctxt;
  4379. int ret = 0;
  4380. int i;
  4381. /* Check if enabled_tc is same as existing or new TCs */
  4382. if (vsi->tc_config.enabled_tc == enabled_tc)
  4383. return ret;
  4384. /* Enable ETS TCs with equal BW Share for now across all VSIs */
  4385. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4386. if (enabled_tc & BIT(i))
  4387. bw_share[i] = 1;
  4388. }
  4389. ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
  4390. if (ret) {
  4391. dev_info(&vsi->back->pdev->dev,
  4392. "Failed configuring TC map %d for VSI %d\n",
  4393. enabled_tc, vsi->seid);
  4394. goto out;
  4395. }
  4396. /* Update Queue Pairs Mapping for currently enabled UPs */
  4397. ctxt.seid = vsi->seid;
  4398. ctxt.pf_num = vsi->back->hw.pf_id;
  4399. ctxt.vf_num = 0;
  4400. ctxt.uplink_seid = vsi->uplink_seid;
  4401. ctxt.info = vsi->info;
  4402. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
  4403. if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
  4404. ctxt.info.valid_sections |=
  4405. cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
  4406. ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
  4407. }
  4408. /* Update the VSI after updating the VSI queue-mapping information */
  4409. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  4410. if (ret) {
  4411. dev_info(&vsi->back->pdev->dev,
  4412. "Update vsi tc config failed, err %s aq_err %s\n",
  4413. i40e_stat_str(&vsi->back->hw, ret),
  4414. i40e_aq_str(&vsi->back->hw,
  4415. vsi->back->hw.aq.asq_last_status));
  4416. goto out;
  4417. }
  4418. /* update the local VSI info with updated queue map */
  4419. i40e_vsi_update_queue_map(vsi, &ctxt);
  4420. vsi->info.valid_sections = 0;
  4421. /* Update current VSI BW information */
  4422. ret = i40e_vsi_get_bw_info(vsi);
  4423. if (ret) {
  4424. dev_info(&vsi->back->pdev->dev,
  4425. "Failed updating vsi bw info, err %s aq_err %s\n",
  4426. i40e_stat_str(&vsi->back->hw, ret),
  4427. i40e_aq_str(&vsi->back->hw,
  4428. vsi->back->hw.aq.asq_last_status));
  4429. goto out;
  4430. }
  4431. /* Update the netdev TC setup */
  4432. i40e_vsi_config_netdev_tc(vsi, enabled_tc);
  4433. out:
  4434. return ret;
  4435. }
  4436. /**
  4437. * i40e_veb_config_tc - Configure TCs for given VEB
  4438. * @veb: given VEB
  4439. * @enabled_tc: TC bitmap
  4440. *
  4441. * Configures given TC bitmap for VEB (switching) element
  4442. **/
  4443. int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
  4444. {
  4445. struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
  4446. struct i40e_pf *pf = veb->pf;
  4447. int ret = 0;
  4448. int i;
  4449. /* No TCs or already enabled TCs just return */
  4450. if (!enabled_tc || veb->enabled_tc == enabled_tc)
  4451. return ret;
  4452. bw_data.tc_valid_bits = enabled_tc;
  4453. /* bw_data.absolute_credits is not set (relative) */
  4454. /* Enable ETS TCs with equal BW Share for now */
  4455. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4456. if (enabled_tc & BIT(i))
  4457. bw_data.tc_bw_share_credits[i] = 1;
  4458. }
  4459. ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
  4460. &bw_data, NULL);
  4461. if (ret) {
  4462. dev_info(&pf->pdev->dev,
  4463. "VEB bw config failed, err %s aq_err %s\n",
  4464. i40e_stat_str(&pf->hw, ret),
  4465. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  4466. goto out;
  4467. }
  4468. /* Update the BW information */
  4469. ret = i40e_veb_get_bw_info(veb);
  4470. if (ret) {
  4471. dev_info(&pf->pdev->dev,
  4472. "Failed getting veb bw config, err %s aq_err %s\n",
  4473. i40e_stat_str(&pf->hw, ret),
  4474. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  4475. }
  4476. out:
  4477. return ret;
  4478. }
  4479. #ifdef CONFIG_I40E_DCB
  4480. /**
  4481. * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
  4482. * @pf: PF struct
  4483. *
  4484. * Reconfigure VEB/VSIs on a given PF; it is assumed that
  4485. * the caller would've quiesce all the VSIs before calling
  4486. * this function
  4487. **/
  4488. static void i40e_dcb_reconfigure(struct i40e_pf *pf)
  4489. {
  4490. u8 tc_map = 0;
  4491. int ret;
  4492. u8 v;
  4493. /* Enable the TCs available on PF to all VEBs */
  4494. tc_map = i40e_pf_get_tc_map(pf);
  4495. for (v = 0; v < I40E_MAX_VEB; v++) {
  4496. if (!pf->veb[v])
  4497. continue;
  4498. ret = i40e_veb_config_tc(pf->veb[v], tc_map);
  4499. if (ret) {
  4500. dev_info(&pf->pdev->dev,
  4501. "Failed configuring TC for VEB seid=%d\n",
  4502. pf->veb[v]->seid);
  4503. /* Will try to configure as many components */
  4504. }
  4505. }
  4506. /* Update each VSI */
  4507. for (v = 0; v < pf->num_alloc_vsi; v++) {
  4508. if (!pf->vsi[v])
  4509. continue;
  4510. /* - Enable all TCs for the LAN VSI
  4511. * - For all others keep them at TC0 for now
  4512. */
  4513. if (v == pf->lan_vsi)
  4514. tc_map = i40e_pf_get_tc_map(pf);
  4515. else
  4516. tc_map = I40E_DEFAULT_TRAFFIC_CLASS;
  4517. ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
  4518. if (ret) {
  4519. dev_info(&pf->pdev->dev,
  4520. "Failed configuring TC for VSI seid=%d\n",
  4521. pf->vsi[v]->seid);
  4522. /* Will try to configure as many components */
  4523. } else {
  4524. /* Re-configure VSI vectors based on updated TC map */
  4525. i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
  4526. if (pf->vsi[v]->netdev)
  4527. i40e_dcbnl_set_all(pf->vsi[v]);
  4528. }
  4529. }
  4530. }
  4531. /**
  4532. * i40e_resume_port_tx - Resume port Tx
  4533. * @pf: PF struct
  4534. *
  4535. * Resume a port's Tx and issue a PF reset in case of failure to
  4536. * resume.
  4537. **/
  4538. static int i40e_resume_port_tx(struct i40e_pf *pf)
  4539. {
  4540. struct i40e_hw *hw = &pf->hw;
  4541. int ret;
  4542. ret = i40e_aq_resume_port_tx(hw, NULL);
  4543. if (ret) {
  4544. dev_info(&pf->pdev->dev,
  4545. "Resume Port Tx failed, err %s aq_err %s\n",
  4546. i40e_stat_str(&pf->hw, ret),
  4547. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  4548. /* Schedule PF reset to recover */
  4549. set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
  4550. i40e_service_event_schedule(pf);
  4551. }
  4552. return ret;
  4553. }
  4554. /**
  4555. * i40e_init_pf_dcb - Initialize DCB configuration
  4556. * @pf: PF being configured
  4557. *
  4558. * Query the current DCB configuration and cache it
  4559. * in the hardware structure
  4560. **/
  4561. static int i40e_init_pf_dcb(struct i40e_pf *pf)
  4562. {
  4563. struct i40e_hw *hw = &pf->hw;
  4564. int err = 0;
  4565. /* Do not enable DCB for SW1 and SW2 images even if the FW is capable */
  4566. if (pf->flags & I40E_FLAG_NO_DCB_SUPPORT)
  4567. goto out;
  4568. /* Get the initial DCB configuration */
  4569. err = i40e_init_dcb(hw);
  4570. if (!err) {
  4571. /* Device/Function is not DCBX capable */
  4572. if ((!hw->func_caps.dcb) ||
  4573. (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
  4574. dev_info(&pf->pdev->dev,
  4575. "DCBX offload is not supported or is disabled for this PF.\n");
  4576. } else {
  4577. /* When status is not DISABLED then DCBX in FW */
  4578. pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
  4579. DCB_CAP_DCBX_VER_IEEE;
  4580. pf->flags |= I40E_FLAG_DCB_CAPABLE;
  4581. /* Enable DCB tagging only when more than one TC
  4582. * or explicitly disable if only one TC
  4583. */
  4584. if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
  4585. pf->flags |= I40E_FLAG_DCB_ENABLED;
  4586. else
  4587. pf->flags &= ~I40E_FLAG_DCB_ENABLED;
  4588. dev_dbg(&pf->pdev->dev,
  4589. "DCBX offload is supported for this PF.\n");
  4590. }
  4591. } else {
  4592. dev_info(&pf->pdev->dev,
  4593. "Query for DCB configuration failed, err %s aq_err %s\n",
  4594. i40e_stat_str(&pf->hw, err),
  4595. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  4596. }
  4597. out:
  4598. return err;
  4599. }
  4600. #endif /* CONFIG_I40E_DCB */
  4601. #define SPEED_SIZE 14
  4602. #define FC_SIZE 8
  4603. /**
  4604. * i40e_print_link_message - print link up or down
  4605. * @vsi: the VSI for which link needs a message
  4606. */
  4607. void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
  4608. {
  4609. enum i40e_aq_link_speed new_speed;
  4610. char *speed = "Unknown";
  4611. char *fc = "Unknown";
  4612. char *fec = "";
  4613. char *an = "";
  4614. new_speed = vsi->back->hw.phy.link_info.link_speed;
  4615. if ((vsi->current_isup == isup) && (vsi->current_speed == new_speed))
  4616. return;
  4617. vsi->current_isup = isup;
  4618. vsi->current_speed = new_speed;
  4619. if (!isup) {
  4620. netdev_info(vsi->netdev, "NIC Link is Down\n");
  4621. return;
  4622. }
  4623. /* Warn user if link speed on NPAR enabled partition is not at
  4624. * least 10GB
  4625. */
  4626. if (vsi->back->hw.func_caps.npar_enable &&
  4627. (vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
  4628. vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
  4629. netdev_warn(vsi->netdev,
  4630. "The partition detected link speed that is less than 10Gbps\n");
  4631. switch (vsi->back->hw.phy.link_info.link_speed) {
  4632. case I40E_LINK_SPEED_40GB:
  4633. speed = "40 G";
  4634. break;
  4635. case I40E_LINK_SPEED_20GB:
  4636. speed = "20 G";
  4637. break;
  4638. case I40E_LINK_SPEED_25GB:
  4639. speed = "25 G";
  4640. break;
  4641. case I40E_LINK_SPEED_10GB:
  4642. speed = "10 G";
  4643. break;
  4644. case I40E_LINK_SPEED_1GB:
  4645. speed = "1000 M";
  4646. break;
  4647. case I40E_LINK_SPEED_100MB:
  4648. speed = "100 M";
  4649. break;
  4650. default:
  4651. break;
  4652. }
  4653. switch (vsi->back->hw.fc.current_mode) {
  4654. case I40E_FC_FULL:
  4655. fc = "RX/TX";
  4656. break;
  4657. case I40E_FC_TX_PAUSE:
  4658. fc = "TX";
  4659. break;
  4660. case I40E_FC_RX_PAUSE:
  4661. fc = "RX";
  4662. break;
  4663. default:
  4664. fc = "None";
  4665. break;
  4666. }
  4667. if (vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_25GB) {
  4668. fec = ", FEC: None";
  4669. an = ", Autoneg: False";
  4670. if (vsi->back->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
  4671. an = ", Autoneg: True";
  4672. if (vsi->back->hw.phy.link_info.fec_info &
  4673. I40E_AQ_CONFIG_FEC_KR_ENA)
  4674. fec = ", FEC: CL74 FC-FEC/BASE-R";
  4675. else if (vsi->back->hw.phy.link_info.fec_info &
  4676. I40E_AQ_CONFIG_FEC_RS_ENA)
  4677. fec = ", FEC: CL108 RS-FEC";
  4678. }
  4679. netdev_info(vsi->netdev, "NIC Link is Up, %sbps Full Duplex%s%s, Flow Control: %s\n",
  4680. speed, fec, an, fc);
  4681. }
  4682. /**
  4683. * i40e_up_complete - Finish the last steps of bringing up a connection
  4684. * @vsi: the VSI being configured
  4685. **/
  4686. static int i40e_up_complete(struct i40e_vsi *vsi)
  4687. {
  4688. struct i40e_pf *pf = vsi->back;
  4689. int err;
  4690. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  4691. i40e_vsi_configure_msix(vsi);
  4692. else
  4693. i40e_configure_msi_and_legacy(vsi);
  4694. /* start rings */
  4695. err = i40e_vsi_start_rings(vsi);
  4696. if (err)
  4697. return err;
  4698. clear_bit(__I40E_DOWN, &vsi->state);
  4699. i40e_napi_enable_all(vsi);
  4700. i40e_vsi_enable_irq(vsi);
  4701. if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
  4702. (vsi->netdev)) {
  4703. i40e_print_link_message(vsi, true);
  4704. netif_tx_start_all_queues(vsi->netdev);
  4705. netif_carrier_on(vsi->netdev);
  4706. } else if (vsi->netdev) {
  4707. i40e_print_link_message(vsi, false);
  4708. /* need to check for qualified module here*/
  4709. if ((pf->hw.phy.link_info.link_info &
  4710. I40E_AQ_MEDIA_AVAILABLE) &&
  4711. (!(pf->hw.phy.link_info.an_info &
  4712. I40E_AQ_QUALIFIED_MODULE)))
  4713. netdev_err(vsi->netdev,
  4714. "the driver failed to link because an unqualified module was detected.");
  4715. }
  4716. /* replay FDIR SB filters */
  4717. if (vsi->type == I40E_VSI_FDIR) {
  4718. /* reset fd counters */
  4719. pf->fd_add_err = 0;
  4720. pf->fd_atr_cnt = 0;
  4721. i40e_fdir_filter_restore(vsi);
  4722. }
  4723. /* On the next run of the service_task, notify any clients of the new
  4724. * opened netdev
  4725. */
  4726. pf->flags |= I40E_FLAG_SERVICE_CLIENT_REQUESTED;
  4727. i40e_service_event_schedule(pf);
  4728. return 0;
  4729. }
  4730. /**
  4731. * i40e_vsi_reinit_locked - Reset the VSI
  4732. * @vsi: the VSI being configured
  4733. *
  4734. * Rebuild the ring structs after some configuration
  4735. * has changed, e.g. MTU size.
  4736. **/
  4737. static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
  4738. {
  4739. struct i40e_pf *pf = vsi->back;
  4740. WARN_ON(in_interrupt());
  4741. while (test_and_set_bit(__I40E_CONFIG_BUSY, &pf->state))
  4742. usleep_range(1000, 2000);
  4743. i40e_down(vsi);
  4744. i40e_up(vsi);
  4745. clear_bit(__I40E_CONFIG_BUSY, &pf->state);
  4746. }
  4747. /**
  4748. * i40e_up - Bring the connection back up after being down
  4749. * @vsi: the VSI being configured
  4750. **/
  4751. int i40e_up(struct i40e_vsi *vsi)
  4752. {
  4753. int err;
  4754. err = i40e_vsi_configure(vsi);
  4755. if (!err)
  4756. err = i40e_up_complete(vsi);
  4757. return err;
  4758. }
  4759. /**
  4760. * i40e_down - Shutdown the connection processing
  4761. * @vsi: the VSI being stopped
  4762. **/
  4763. void i40e_down(struct i40e_vsi *vsi)
  4764. {
  4765. int i;
  4766. /* It is assumed that the caller of this function
  4767. * sets the vsi->state __I40E_DOWN bit.
  4768. */
  4769. if (vsi->netdev) {
  4770. netif_carrier_off(vsi->netdev);
  4771. netif_tx_disable(vsi->netdev);
  4772. }
  4773. i40e_vsi_disable_irq(vsi);
  4774. i40e_vsi_stop_rings(vsi);
  4775. i40e_napi_disable_all(vsi);
  4776. for (i = 0; i < vsi->num_queue_pairs; i++) {
  4777. i40e_clean_tx_ring(vsi->tx_rings[i]);
  4778. i40e_clean_rx_ring(vsi->rx_rings[i]);
  4779. }
  4780. }
  4781. /**
  4782. * i40e_setup_tc - configure multiple traffic classes
  4783. * @netdev: net device to configure
  4784. * @tc: number of traffic classes to enable
  4785. **/
  4786. static int i40e_setup_tc(struct net_device *netdev, u8 tc)
  4787. {
  4788. struct i40e_netdev_priv *np = netdev_priv(netdev);
  4789. struct i40e_vsi *vsi = np->vsi;
  4790. struct i40e_pf *pf = vsi->back;
  4791. u8 enabled_tc = 0;
  4792. int ret = -EINVAL;
  4793. int i;
  4794. /* Check if DCB enabled to continue */
  4795. if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
  4796. netdev_info(netdev, "DCB is not enabled for adapter\n");
  4797. goto exit;
  4798. }
  4799. /* Check if MFP enabled */
  4800. if (pf->flags & I40E_FLAG_MFP_ENABLED) {
  4801. netdev_info(netdev, "Configuring TC not supported in MFP mode\n");
  4802. goto exit;
  4803. }
  4804. /* Check whether tc count is within enabled limit */
  4805. if (tc > i40e_pf_get_num_tc(pf)) {
  4806. netdev_info(netdev, "TC count greater than enabled on link for adapter\n");
  4807. goto exit;
  4808. }
  4809. /* Generate TC map for number of tc requested */
  4810. for (i = 0; i < tc; i++)
  4811. enabled_tc |= BIT(i);
  4812. /* Requesting same TC configuration as already enabled */
  4813. if (enabled_tc == vsi->tc_config.enabled_tc)
  4814. return 0;
  4815. /* Quiesce VSI queues */
  4816. i40e_quiesce_vsi(vsi);
  4817. /* Configure VSI for enabled TCs */
  4818. ret = i40e_vsi_config_tc(vsi, enabled_tc);
  4819. if (ret) {
  4820. netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
  4821. vsi->seid);
  4822. goto exit;
  4823. }
  4824. /* Unquiesce VSI */
  4825. i40e_unquiesce_vsi(vsi);
  4826. exit:
  4827. return ret;
  4828. }
  4829. static int __i40e_setup_tc(struct net_device *netdev, u32 handle, __be16 proto,
  4830. struct tc_to_netdev *tc)
  4831. {
  4832. if (tc->type != TC_SETUP_MQPRIO)
  4833. return -EINVAL;
  4834. tc->mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
  4835. return i40e_setup_tc(netdev, tc->mqprio->num_tc);
  4836. }
  4837. /**
  4838. * i40e_open - Called when a network interface is made active
  4839. * @netdev: network interface device structure
  4840. *
  4841. * The open entry point is called when a network interface is made
  4842. * active by the system (IFF_UP). At this point all resources needed
  4843. * for transmit and receive operations are allocated, the interrupt
  4844. * handler is registered with the OS, the netdev watchdog subtask is
  4845. * enabled, and the stack is notified that the interface is ready.
  4846. *
  4847. * Returns 0 on success, negative value on failure
  4848. **/
  4849. int i40e_open(struct net_device *netdev)
  4850. {
  4851. struct i40e_netdev_priv *np = netdev_priv(netdev);
  4852. struct i40e_vsi *vsi = np->vsi;
  4853. struct i40e_pf *pf = vsi->back;
  4854. int err;
  4855. /* disallow open during test or if eeprom is broken */
  4856. if (test_bit(__I40E_TESTING, &pf->state) ||
  4857. test_bit(__I40E_BAD_EEPROM, &pf->state))
  4858. return -EBUSY;
  4859. netif_carrier_off(netdev);
  4860. err = i40e_vsi_open(vsi);
  4861. if (err)
  4862. return err;
  4863. /* configure global TSO hardware offload settings */
  4864. wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
  4865. TCP_FLAG_FIN) >> 16);
  4866. wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
  4867. TCP_FLAG_FIN |
  4868. TCP_FLAG_CWR) >> 16);
  4869. wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
  4870. udp_tunnel_get_rx_info(netdev);
  4871. return 0;
  4872. }
  4873. /**
  4874. * i40e_vsi_open -
  4875. * @vsi: the VSI to open
  4876. *
  4877. * Finish initialization of the VSI.
  4878. *
  4879. * Returns 0 on success, negative value on failure
  4880. *
  4881. * Note: expects to be called while under rtnl_lock()
  4882. **/
  4883. int i40e_vsi_open(struct i40e_vsi *vsi)
  4884. {
  4885. struct i40e_pf *pf = vsi->back;
  4886. char int_name[I40E_INT_NAME_STR_LEN];
  4887. int err;
  4888. /* allocate descriptors */
  4889. err = i40e_vsi_setup_tx_resources(vsi);
  4890. if (err)
  4891. goto err_setup_tx;
  4892. err = i40e_vsi_setup_rx_resources(vsi);
  4893. if (err)
  4894. goto err_setup_rx;
  4895. err = i40e_vsi_configure(vsi);
  4896. if (err)
  4897. goto err_setup_rx;
  4898. if (vsi->netdev) {
  4899. snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
  4900. dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
  4901. err = i40e_vsi_request_irq(vsi, int_name);
  4902. if (err)
  4903. goto err_setup_rx;
  4904. /* Notify the stack of the actual queue counts. */
  4905. err = netif_set_real_num_tx_queues(vsi->netdev,
  4906. vsi->num_queue_pairs);
  4907. if (err)
  4908. goto err_set_queues;
  4909. err = netif_set_real_num_rx_queues(vsi->netdev,
  4910. vsi->num_queue_pairs);
  4911. if (err)
  4912. goto err_set_queues;
  4913. } else if (vsi->type == I40E_VSI_FDIR) {
  4914. snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
  4915. dev_driver_string(&pf->pdev->dev),
  4916. dev_name(&pf->pdev->dev));
  4917. err = i40e_vsi_request_irq(vsi, int_name);
  4918. } else {
  4919. err = -EINVAL;
  4920. goto err_setup_rx;
  4921. }
  4922. err = i40e_up_complete(vsi);
  4923. if (err)
  4924. goto err_up_complete;
  4925. return 0;
  4926. err_up_complete:
  4927. i40e_down(vsi);
  4928. err_set_queues:
  4929. i40e_vsi_free_irq(vsi);
  4930. err_setup_rx:
  4931. i40e_vsi_free_rx_resources(vsi);
  4932. err_setup_tx:
  4933. i40e_vsi_free_tx_resources(vsi);
  4934. if (vsi == pf->vsi[pf->lan_vsi])
  4935. i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED), true);
  4936. return err;
  4937. }
  4938. /**
  4939. * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
  4940. * @pf: Pointer to PF
  4941. *
  4942. * This function destroys the hlist where all the Flow Director
  4943. * filters were saved.
  4944. **/
  4945. static void i40e_fdir_filter_exit(struct i40e_pf *pf)
  4946. {
  4947. struct i40e_fdir_filter *filter;
  4948. struct i40e_flex_pit *pit_entry, *tmp;
  4949. struct hlist_node *node2;
  4950. hlist_for_each_entry_safe(filter, node2,
  4951. &pf->fdir_filter_list, fdir_node) {
  4952. hlist_del(&filter->fdir_node);
  4953. kfree(filter);
  4954. }
  4955. list_for_each_entry_safe(pit_entry, tmp, &pf->l3_flex_pit_list, list) {
  4956. list_del(&pit_entry->list);
  4957. kfree(pit_entry);
  4958. }
  4959. INIT_LIST_HEAD(&pf->l3_flex_pit_list);
  4960. list_for_each_entry_safe(pit_entry, tmp, &pf->l4_flex_pit_list, list) {
  4961. list_del(&pit_entry->list);
  4962. kfree(pit_entry);
  4963. }
  4964. INIT_LIST_HEAD(&pf->l4_flex_pit_list);
  4965. pf->fdir_pf_active_filters = 0;
  4966. pf->fd_tcp4_filter_cnt = 0;
  4967. pf->fd_udp4_filter_cnt = 0;
  4968. pf->fd_sctp4_filter_cnt = 0;
  4969. pf->fd_ip4_filter_cnt = 0;
  4970. /* Reprogram the default input set for TCP/IPv4 */
  4971. i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
  4972. I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
  4973. I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
  4974. /* Reprogram the default input set for UDP/IPv4 */
  4975. i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_UDP,
  4976. I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
  4977. I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
  4978. /* Reprogram the default input set for SCTP/IPv4 */
  4979. i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_SCTP,
  4980. I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
  4981. I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
  4982. /* Reprogram the default input set for Other/IPv4 */
  4983. i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_OTHER,
  4984. I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
  4985. }
  4986. /**
  4987. * i40e_close - Disables a network interface
  4988. * @netdev: network interface device structure
  4989. *
  4990. * The close entry point is called when an interface is de-activated
  4991. * by the OS. The hardware is still under the driver's control, but
  4992. * this netdev interface is disabled.
  4993. *
  4994. * Returns 0, this is not allowed to fail
  4995. **/
  4996. int i40e_close(struct net_device *netdev)
  4997. {
  4998. struct i40e_netdev_priv *np = netdev_priv(netdev);
  4999. struct i40e_vsi *vsi = np->vsi;
  5000. i40e_vsi_close(vsi);
  5001. return 0;
  5002. }
  5003. /**
  5004. * i40e_do_reset - Start a PF or Core Reset sequence
  5005. * @pf: board private structure
  5006. * @reset_flags: which reset is requested
  5007. * @lock_acquired: indicates whether or not the lock has been acquired
  5008. * before this function was called.
  5009. *
  5010. * The essential difference in resets is that the PF Reset
  5011. * doesn't clear the packet buffers, doesn't reset the PE
  5012. * firmware, and doesn't bother the other PFs on the chip.
  5013. **/
  5014. void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags, bool lock_acquired)
  5015. {
  5016. u32 val;
  5017. WARN_ON(in_interrupt());
  5018. /* do the biggest reset indicated */
  5019. if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
  5020. /* Request a Global Reset
  5021. *
  5022. * This will start the chip's countdown to the actual full
  5023. * chip reset event, and a warning interrupt to be sent
  5024. * to all PFs, including the requestor. Our handler
  5025. * for the warning interrupt will deal with the shutdown
  5026. * and recovery of the switch setup.
  5027. */
  5028. dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
  5029. val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
  5030. val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
  5031. wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
  5032. } else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
  5033. /* Request a Core Reset
  5034. *
  5035. * Same as Global Reset, except does *not* include the MAC/PHY
  5036. */
  5037. dev_dbg(&pf->pdev->dev, "CoreR requested\n");
  5038. val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
  5039. val |= I40E_GLGEN_RTRIG_CORER_MASK;
  5040. wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
  5041. i40e_flush(&pf->hw);
  5042. } else if (reset_flags & BIT_ULL(__I40E_PF_RESET_REQUESTED)) {
  5043. /* Request a PF Reset
  5044. *
  5045. * Resets only the PF-specific registers
  5046. *
  5047. * This goes directly to the tear-down and rebuild of
  5048. * the switch, since we need to do all the recovery as
  5049. * for the Core Reset.
  5050. */
  5051. dev_dbg(&pf->pdev->dev, "PFR requested\n");
  5052. i40e_handle_reset_warning(pf, lock_acquired);
  5053. } else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
  5054. int v;
  5055. /* Find the VSI(s) that requested a re-init */
  5056. dev_info(&pf->pdev->dev,
  5057. "VSI reinit requested\n");
  5058. for (v = 0; v < pf->num_alloc_vsi; v++) {
  5059. struct i40e_vsi *vsi = pf->vsi[v];
  5060. if (vsi != NULL &&
  5061. test_bit(__I40E_REINIT_REQUESTED, &vsi->state)) {
  5062. i40e_vsi_reinit_locked(pf->vsi[v]);
  5063. clear_bit(__I40E_REINIT_REQUESTED, &vsi->state);
  5064. }
  5065. }
  5066. } else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
  5067. int v;
  5068. /* Find the VSI(s) that needs to be brought down */
  5069. dev_info(&pf->pdev->dev, "VSI down requested\n");
  5070. for (v = 0; v < pf->num_alloc_vsi; v++) {
  5071. struct i40e_vsi *vsi = pf->vsi[v];
  5072. if (vsi != NULL &&
  5073. test_bit(__I40E_DOWN_REQUESTED, &vsi->state)) {
  5074. set_bit(__I40E_DOWN, &vsi->state);
  5075. i40e_down(vsi);
  5076. clear_bit(__I40E_DOWN_REQUESTED, &vsi->state);
  5077. }
  5078. }
  5079. } else {
  5080. dev_info(&pf->pdev->dev,
  5081. "bad reset request 0x%08x\n", reset_flags);
  5082. }
  5083. }
  5084. #ifdef CONFIG_I40E_DCB
  5085. /**
  5086. * i40e_dcb_need_reconfig - Check if DCB needs reconfig
  5087. * @pf: board private structure
  5088. * @old_cfg: current DCB config
  5089. * @new_cfg: new DCB config
  5090. **/
  5091. bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
  5092. struct i40e_dcbx_config *old_cfg,
  5093. struct i40e_dcbx_config *new_cfg)
  5094. {
  5095. bool need_reconfig = false;
  5096. /* Check if ETS configuration has changed */
  5097. if (memcmp(&new_cfg->etscfg,
  5098. &old_cfg->etscfg,
  5099. sizeof(new_cfg->etscfg))) {
  5100. /* If Priority Table has changed reconfig is needed */
  5101. if (memcmp(&new_cfg->etscfg.prioritytable,
  5102. &old_cfg->etscfg.prioritytable,
  5103. sizeof(new_cfg->etscfg.prioritytable))) {
  5104. need_reconfig = true;
  5105. dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
  5106. }
  5107. if (memcmp(&new_cfg->etscfg.tcbwtable,
  5108. &old_cfg->etscfg.tcbwtable,
  5109. sizeof(new_cfg->etscfg.tcbwtable)))
  5110. dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
  5111. if (memcmp(&new_cfg->etscfg.tsatable,
  5112. &old_cfg->etscfg.tsatable,
  5113. sizeof(new_cfg->etscfg.tsatable)))
  5114. dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
  5115. }
  5116. /* Check if PFC configuration has changed */
  5117. if (memcmp(&new_cfg->pfc,
  5118. &old_cfg->pfc,
  5119. sizeof(new_cfg->pfc))) {
  5120. need_reconfig = true;
  5121. dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
  5122. }
  5123. /* Check if APP Table has changed */
  5124. if (memcmp(&new_cfg->app,
  5125. &old_cfg->app,
  5126. sizeof(new_cfg->app))) {
  5127. need_reconfig = true;
  5128. dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
  5129. }
  5130. dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
  5131. return need_reconfig;
  5132. }
  5133. /**
  5134. * i40e_handle_lldp_event - Handle LLDP Change MIB event
  5135. * @pf: board private structure
  5136. * @e: event info posted on ARQ
  5137. **/
  5138. static int i40e_handle_lldp_event(struct i40e_pf *pf,
  5139. struct i40e_arq_event_info *e)
  5140. {
  5141. struct i40e_aqc_lldp_get_mib *mib =
  5142. (struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
  5143. struct i40e_hw *hw = &pf->hw;
  5144. struct i40e_dcbx_config tmp_dcbx_cfg;
  5145. bool need_reconfig = false;
  5146. int ret = 0;
  5147. u8 type;
  5148. /* Not DCB capable or capability disabled */
  5149. if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
  5150. return ret;
  5151. /* Ignore if event is not for Nearest Bridge */
  5152. type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
  5153. & I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
  5154. dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
  5155. if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
  5156. return ret;
  5157. /* Check MIB Type and return if event for Remote MIB update */
  5158. type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
  5159. dev_dbg(&pf->pdev->dev,
  5160. "LLDP event mib type %s\n", type ? "remote" : "local");
  5161. if (type == I40E_AQ_LLDP_MIB_REMOTE) {
  5162. /* Update the remote cached instance and return */
  5163. ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
  5164. I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
  5165. &hw->remote_dcbx_config);
  5166. goto exit;
  5167. }
  5168. /* Store the old configuration */
  5169. tmp_dcbx_cfg = hw->local_dcbx_config;
  5170. /* Reset the old DCBx configuration data */
  5171. memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
  5172. /* Get updated DCBX data from firmware */
  5173. ret = i40e_get_dcb_config(&pf->hw);
  5174. if (ret) {
  5175. dev_info(&pf->pdev->dev,
  5176. "Failed querying DCB configuration data from firmware, err %s aq_err %s\n",
  5177. i40e_stat_str(&pf->hw, ret),
  5178. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  5179. goto exit;
  5180. }
  5181. /* No change detected in DCBX configs */
  5182. if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
  5183. sizeof(tmp_dcbx_cfg))) {
  5184. dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
  5185. goto exit;
  5186. }
  5187. need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
  5188. &hw->local_dcbx_config);
  5189. i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
  5190. if (!need_reconfig)
  5191. goto exit;
  5192. /* Enable DCB tagging only when more than one TC */
  5193. if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
  5194. pf->flags |= I40E_FLAG_DCB_ENABLED;
  5195. else
  5196. pf->flags &= ~I40E_FLAG_DCB_ENABLED;
  5197. set_bit(__I40E_PORT_SUSPENDED, &pf->state);
  5198. /* Reconfiguration needed quiesce all VSIs */
  5199. i40e_pf_quiesce_all_vsi(pf);
  5200. /* Changes in configuration update VEB/VSI */
  5201. i40e_dcb_reconfigure(pf);
  5202. ret = i40e_resume_port_tx(pf);
  5203. clear_bit(__I40E_PORT_SUSPENDED, &pf->state);
  5204. /* In case of error no point in resuming VSIs */
  5205. if (ret)
  5206. goto exit;
  5207. /* Wait for the PF's queues to be disabled */
  5208. ret = i40e_pf_wait_queues_disabled(pf);
  5209. if (ret) {
  5210. /* Schedule PF reset to recover */
  5211. set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
  5212. i40e_service_event_schedule(pf);
  5213. } else {
  5214. i40e_pf_unquiesce_all_vsi(pf);
  5215. pf->flags |= (I40E_FLAG_SERVICE_CLIENT_REQUESTED |
  5216. I40E_FLAG_CLIENT_L2_CHANGE);
  5217. }
  5218. exit:
  5219. return ret;
  5220. }
  5221. #endif /* CONFIG_I40E_DCB */
  5222. /**
  5223. * i40e_do_reset_safe - Protected reset path for userland calls.
  5224. * @pf: board private structure
  5225. * @reset_flags: which reset is requested
  5226. *
  5227. **/
  5228. void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
  5229. {
  5230. rtnl_lock();
  5231. i40e_do_reset(pf, reset_flags, true);
  5232. rtnl_unlock();
  5233. }
  5234. /**
  5235. * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
  5236. * @pf: board private structure
  5237. * @e: event info posted on ARQ
  5238. *
  5239. * Handler for LAN Queue Overflow Event generated by the firmware for PF
  5240. * and VF queues
  5241. **/
  5242. static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
  5243. struct i40e_arq_event_info *e)
  5244. {
  5245. struct i40e_aqc_lan_overflow *data =
  5246. (struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
  5247. u32 queue = le32_to_cpu(data->prtdcb_rupto);
  5248. u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
  5249. struct i40e_hw *hw = &pf->hw;
  5250. struct i40e_vf *vf;
  5251. u16 vf_id;
  5252. dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
  5253. queue, qtx_ctl);
  5254. /* Queue belongs to VF, find the VF and issue VF reset */
  5255. if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
  5256. >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
  5257. vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
  5258. >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
  5259. vf_id -= hw->func_caps.vf_base_id;
  5260. vf = &pf->vf[vf_id];
  5261. i40e_vc_notify_vf_reset(vf);
  5262. /* Allow VF to process pending reset notification */
  5263. msleep(20);
  5264. i40e_reset_vf(vf, false);
  5265. }
  5266. }
  5267. /**
  5268. * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
  5269. * @pf: board private structure
  5270. **/
  5271. u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
  5272. {
  5273. u32 val, fcnt_prog;
  5274. val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
  5275. fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
  5276. return fcnt_prog;
  5277. }
  5278. /**
  5279. * i40e_get_current_fd_count - Get total FD filters programmed for this PF
  5280. * @pf: board private structure
  5281. **/
  5282. u32 i40e_get_current_fd_count(struct i40e_pf *pf)
  5283. {
  5284. u32 val, fcnt_prog;
  5285. val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
  5286. fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
  5287. ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
  5288. I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
  5289. return fcnt_prog;
  5290. }
  5291. /**
  5292. * i40e_get_global_fd_count - Get total FD filters programmed on device
  5293. * @pf: board private structure
  5294. **/
  5295. u32 i40e_get_global_fd_count(struct i40e_pf *pf)
  5296. {
  5297. u32 val, fcnt_prog;
  5298. val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
  5299. fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
  5300. ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >>
  5301. I40E_GLQF_FDCNT_0_BESTCNT_SHIFT);
  5302. return fcnt_prog;
  5303. }
  5304. /**
  5305. * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
  5306. * @pf: board private structure
  5307. **/
  5308. void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
  5309. {
  5310. struct i40e_fdir_filter *filter;
  5311. u32 fcnt_prog, fcnt_avail;
  5312. struct hlist_node *node;
  5313. if (test_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state))
  5314. return;
  5315. /* Check if, FD SB or ATR was auto disabled and if there is enough room
  5316. * to re-enable
  5317. */
  5318. fcnt_prog = i40e_get_global_fd_count(pf);
  5319. fcnt_avail = pf->fdir_pf_filter_count;
  5320. if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
  5321. (pf->fd_add_err == 0) ||
  5322. (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt)) {
  5323. if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
  5324. (pf->hw_disabled_flags & I40E_FLAG_FD_SB_ENABLED)) {
  5325. pf->hw_disabled_flags &= ~I40E_FLAG_FD_SB_ENABLED;
  5326. if (I40E_DEBUG_FD & pf->hw.debug_mask)
  5327. dev_info(&pf->pdev->dev, "FD Sideband/ntuple is being enabled since we have space in the table now\n");
  5328. }
  5329. }
  5330. /* Wait for some more space to be available to turn on ATR. We also
  5331. * must check that no existing ntuple rules for TCP are in effect
  5332. */
  5333. if (fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM * 2)) {
  5334. if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
  5335. (pf->hw_disabled_flags & I40E_FLAG_FD_ATR_ENABLED) &&
  5336. (pf->fd_tcp4_filter_cnt == 0)) {
  5337. pf->hw_disabled_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
  5338. if (I40E_DEBUG_FD & pf->hw.debug_mask)
  5339. dev_info(&pf->pdev->dev, "ATR is being enabled since we have space in the table and there are no conflicting ntuple rules\n");
  5340. }
  5341. }
  5342. /* if hw had a problem adding a filter, delete it */
  5343. if (pf->fd_inv > 0) {
  5344. hlist_for_each_entry_safe(filter, node,
  5345. &pf->fdir_filter_list, fdir_node) {
  5346. if (filter->fd_id == pf->fd_inv) {
  5347. hlist_del(&filter->fdir_node);
  5348. kfree(filter);
  5349. pf->fdir_pf_active_filters--;
  5350. }
  5351. }
  5352. }
  5353. }
  5354. #define I40E_MIN_FD_FLUSH_INTERVAL 10
  5355. #define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
  5356. /**
  5357. * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
  5358. * @pf: board private structure
  5359. **/
  5360. static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
  5361. {
  5362. unsigned long min_flush_time;
  5363. int flush_wait_retry = 50;
  5364. bool disable_atr = false;
  5365. int fd_room;
  5366. int reg;
  5367. if (!time_after(jiffies, pf->fd_flush_timestamp +
  5368. (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
  5369. return;
  5370. /* If the flush is happening too quick and we have mostly SB rules we
  5371. * should not re-enable ATR for some time.
  5372. */
  5373. min_flush_time = pf->fd_flush_timestamp +
  5374. (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
  5375. fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
  5376. if (!(time_after(jiffies, min_flush_time)) &&
  5377. (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
  5378. if (I40E_DEBUG_FD & pf->hw.debug_mask)
  5379. dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
  5380. disable_atr = true;
  5381. }
  5382. pf->fd_flush_timestamp = jiffies;
  5383. pf->hw_disabled_flags |= I40E_FLAG_FD_ATR_ENABLED;
  5384. /* flush all filters */
  5385. wr32(&pf->hw, I40E_PFQF_CTL_1,
  5386. I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
  5387. i40e_flush(&pf->hw);
  5388. pf->fd_flush_cnt++;
  5389. pf->fd_add_err = 0;
  5390. do {
  5391. /* Check FD flush status every 5-6msec */
  5392. usleep_range(5000, 6000);
  5393. reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
  5394. if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
  5395. break;
  5396. } while (flush_wait_retry--);
  5397. if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
  5398. dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
  5399. } else {
  5400. /* replay sideband filters */
  5401. i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
  5402. if (!disable_atr && !pf->fd_tcp4_filter_cnt)
  5403. pf->hw_disabled_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
  5404. clear_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state);
  5405. if (I40E_DEBUG_FD & pf->hw.debug_mask)
  5406. dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
  5407. }
  5408. }
  5409. /**
  5410. * i40e_get_current_atr_count - Get the count of total FD ATR filters programmed
  5411. * @pf: board private structure
  5412. **/
  5413. u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
  5414. {
  5415. return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
  5416. }
  5417. /* We can see up to 256 filter programming desc in transit if the filters are
  5418. * being applied really fast; before we see the first
  5419. * filter miss error on Rx queue 0. Accumulating enough error messages before
  5420. * reacting will make sure we don't cause flush too often.
  5421. */
  5422. #define I40E_MAX_FD_PROGRAM_ERROR 256
  5423. /**
  5424. * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
  5425. * @pf: board private structure
  5426. **/
  5427. static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
  5428. {
  5429. /* if interface is down do nothing */
  5430. if (test_bit(__I40E_DOWN, &pf->state))
  5431. return;
  5432. if (test_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state))
  5433. i40e_fdir_flush_and_replay(pf);
  5434. i40e_fdir_check_and_reenable(pf);
  5435. }
  5436. /**
  5437. * i40e_vsi_link_event - notify VSI of a link event
  5438. * @vsi: vsi to be notified
  5439. * @link_up: link up or down
  5440. **/
  5441. static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
  5442. {
  5443. if (!vsi || test_bit(__I40E_DOWN, &vsi->state))
  5444. return;
  5445. switch (vsi->type) {
  5446. case I40E_VSI_MAIN:
  5447. if (!vsi->netdev || !vsi->netdev_registered)
  5448. break;
  5449. if (link_up) {
  5450. netif_carrier_on(vsi->netdev);
  5451. netif_tx_wake_all_queues(vsi->netdev);
  5452. } else {
  5453. netif_carrier_off(vsi->netdev);
  5454. netif_tx_stop_all_queues(vsi->netdev);
  5455. }
  5456. break;
  5457. case I40E_VSI_SRIOV:
  5458. case I40E_VSI_VMDQ2:
  5459. case I40E_VSI_CTRL:
  5460. case I40E_VSI_IWARP:
  5461. case I40E_VSI_MIRROR:
  5462. default:
  5463. /* there is no notification for other VSIs */
  5464. break;
  5465. }
  5466. }
  5467. /**
  5468. * i40e_veb_link_event - notify elements on the veb of a link event
  5469. * @veb: veb to be notified
  5470. * @link_up: link up or down
  5471. **/
  5472. static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
  5473. {
  5474. struct i40e_pf *pf;
  5475. int i;
  5476. if (!veb || !veb->pf)
  5477. return;
  5478. pf = veb->pf;
  5479. /* depth first... */
  5480. for (i = 0; i < I40E_MAX_VEB; i++)
  5481. if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
  5482. i40e_veb_link_event(pf->veb[i], link_up);
  5483. /* ... now the local VSIs */
  5484. for (i = 0; i < pf->num_alloc_vsi; i++)
  5485. if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
  5486. i40e_vsi_link_event(pf->vsi[i], link_up);
  5487. }
  5488. /**
  5489. * i40e_link_event - Update netif_carrier status
  5490. * @pf: board private structure
  5491. **/
  5492. static void i40e_link_event(struct i40e_pf *pf)
  5493. {
  5494. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  5495. u8 new_link_speed, old_link_speed;
  5496. i40e_status status;
  5497. bool new_link, old_link;
  5498. /* save off old link status information */
  5499. pf->hw.phy.link_info_old = pf->hw.phy.link_info;
  5500. /* set this to force the get_link_status call to refresh state */
  5501. pf->hw.phy.get_link_info = true;
  5502. old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
  5503. status = i40e_get_link_status(&pf->hw, &new_link);
  5504. /* On success, disable temp link polling */
  5505. if (status == I40E_SUCCESS) {
  5506. if (pf->flags & I40E_FLAG_TEMP_LINK_POLLING)
  5507. pf->flags &= ~I40E_FLAG_TEMP_LINK_POLLING;
  5508. } else {
  5509. /* Enable link polling temporarily until i40e_get_link_status
  5510. * returns I40E_SUCCESS
  5511. */
  5512. pf->flags |= I40E_FLAG_TEMP_LINK_POLLING;
  5513. dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
  5514. status);
  5515. return;
  5516. }
  5517. old_link_speed = pf->hw.phy.link_info_old.link_speed;
  5518. new_link_speed = pf->hw.phy.link_info.link_speed;
  5519. if (new_link == old_link &&
  5520. new_link_speed == old_link_speed &&
  5521. (test_bit(__I40E_DOWN, &vsi->state) ||
  5522. new_link == netif_carrier_ok(vsi->netdev)))
  5523. return;
  5524. if (!test_bit(__I40E_DOWN, &vsi->state))
  5525. i40e_print_link_message(vsi, new_link);
  5526. /* Notify the base of the switch tree connected to
  5527. * the link. Floating VEBs are not notified.
  5528. */
  5529. if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
  5530. i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
  5531. else
  5532. i40e_vsi_link_event(vsi, new_link);
  5533. if (pf->vf)
  5534. i40e_vc_notify_link_state(pf);
  5535. if (pf->flags & I40E_FLAG_PTP)
  5536. i40e_ptp_set_increment(pf);
  5537. }
  5538. /**
  5539. * i40e_watchdog_subtask - periodic checks not using event driven response
  5540. * @pf: board private structure
  5541. **/
  5542. static void i40e_watchdog_subtask(struct i40e_pf *pf)
  5543. {
  5544. int i;
  5545. /* if interface is down do nothing */
  5546. if (test_bit(__I40E_DOWN, &pf->state) ||
  5547. test_bit(__I40E_CONFIG_BUSY, &pf->state))
  5548. return;
  5549. /* make sure we don't do these things too often */
  5550. if (time_before(jiffies, (pf->service_timer_previous +
  5551. pf->service_timer_period)))
  5552. return;
  5553. pf->service_timer_previous = jiffies;
  5554. if ((pf->flags & I40E_FLAG_LINK_POLLING_ENABLED) ||
  5555. (pf->flags & I40E_FLAG_TEMP_LINK_POLLING))
  5556. i40e_link_event(pf);
  5557. /* Update the stats for active netdevs so the network stack
  5558. * can look at updated numbers whenever it cares to
  5559. */
  5560. for (i = 0; i < pf->num_alloc_vsi; i++)
  5561. if (pf->vsi[i] && pf->vsi[i]->netdev)
  5562. i40e_update_stats(pf->vsi[i]);
  5563. if (pf->flags & I40E_FLAG_VEB_STATS_ENABLED) {
  5564. /* Update the stats for the active switching components */
  5565. for (i = 0; i < I40E_MAX_VEB; i++)
  5566. if (pf->veb[i])
  5567. i40e_update_veb_stats(pf->veb[i]);
  5568. }
  5569. i40e_ptp_rx_hang(pf->vsi[pf->lan_vsi]);
  5570. }
  5571. /**
  5572. * i40e_reset_subtask - Set up for resetting the device and driver
  5573. * @pf: board private structure
  5574. **/
  5575. static void i40e_reset_subtask(struct i40e_pf *pf)
  5576. {
  5577. u32 reset_flags = 0;
  5578. if (test_bit(__I40E_REINIT_REQUESTED, &pf->state)) {
  5579. reset_flags |= BIT(__I40E_REINIT_REQUESTED);
  5580. clear_bit(__I40E_REINIT_REQUESTED, &pf->state);
  5581. }
  5582. if (test_bit(__I40E_PF_RESET_REQUESTED, &pf->state)) {
  5583. reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
  5584. clear_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
  5585. }
  5586. if (test_bit(__I40E_CORE_RESET_REQUESTED, &pf->state)) {
  5587. reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
  5588. clear_bit(__I40E_CORE_RESET_REQUESTED, &pf->state);
  5589. }
  5590. if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state)) {
  5591. reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
  5592. clear_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state);
  5593. }
  5594. if (test_bit(__I40E_DOWN_REQUESTED, &pf->state)) {
  5595. reset_flags |= BIT(__I40E_DOWN_REQUESTED);
  5596. clear_bit(__I40E_DOWN_REQUESTED, &pf->state);
  5597. }
  5598. /* If there's a recovery already waiting, it takes
  5599. * precedence before starting a new reset sequence.
  5600. */
  5601. if (test_bit(__I40E_RESET_INTR_RECEIVED, &pf->state)) {
  5602. i40e_prep_for_reset(pf, false);
  5603. i40e_reset(pf);
  5604. i40e_rebuild(pf, false, false);
  5605. }
  5606. /* If we're already down or resetting, just bail */
  5607. if (reset_flags &&
  5608. !test_bit(__I40E_DOWN, &pf->state) &&
  5609. !test_bit(__I40E_CONFIG_BUSY, &pf->state)) {
  5610. rtnl_lock();
  5611. i40e_do_reset(pf, reset_flags, true);
  5612. rtnl_unlock();
  5613. }
  5614. }
  5615. /**
  5616. * i40e_handle_link_event - Handle link event
  5617. * @pf: board private structure
  5618. * @e: event info posted on ARQ
  5619. **/
  5620. static void i40e_handle_link_event(struct i40e_pf *pf,
  5621. struct i40e_arq_event_info *e)
  5622. {
  5623. struct i40e_aqc_get_link_status *status =
  5624. (struct i40e_aqc_get_link_status *)&e->desc.params.raw;
  5625. /* Do a new status request to re-enable LSE reporting
  5626. * and load new status information into the hw struct
  5627. * This completely ignores any state information
  5628. * in the ARQ event info, instead choosing to always
  5629. * issue the AQ update link status command.
  5630. */
  5631. i40e_link_event(pf);
  5632. /* check for unqualified module, if link is down */
  5633. if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
  5634. (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
  5635. (!(status->link_info & I40E_AQ_LINK_UP)))
  5636. dev_err(&pf->pdev->dev,
  5637. "The driver failed to link because an unqualified module was detected.\n");
  5638. }
  5639. /**
  5640. * i40e_clean_adminq_subtask - Clean the AdminQ rings
  5641. * @pf: board private structure
  5642. **/
  5643. static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
  5644. {
  5645. struct i40e_arq_event_info event;
  5646. struct i40e_hw *hw = &pf->hw;
  5647. u16 pending, i = 0;
  5648. i40e_status ret;
  5649. u16 opcode;
  5650. u32 oldval;
  5651. u32 val;
  5652. /* Do not run clean AQ when PF reset fails */
  5653. if (test_bit(__I40E_RESET_FAILED, &pf->state))
  5654. return;
  5655. /* check for error indications */
  5656. val = rd32(&pf->hw, pf->hw.aq.arq.len);
  5657. oldval = val;
  5658. if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
  5659. if (hw->debug_mask & I40E_DEBUG_AQ)
  5660. dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
  5661. val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
  5662. }
  5663. if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
  5664. if (hw->debug_mask & I40E_DEBUG_AQ)
  5665. dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
  5666. val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
  5667. pf->arq_overflows++;
  5668. }
  5669. if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
  5670. if (hw->debug_mask & I40E_DEBUG_AQ)
  5671. dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
  5672. val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
  5673. }
  5674. if (oldval != val)
  5675. wr32(&pf->hw, pf->hw.aq.arq.len, val);
  5676. val = rd32(&pf->hw, pf->hw.aq.asq.len);
  5677. oldval = val;
  5678. if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
  5679. if (pf->hw.debug_mask & I40E_DEBUG_AQ)
  5680. dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
  5681. val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
  5682. }
  5683. if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
  5684. if (pf->hw.debug_mask & I40E_DEBUG_AQ)
  5685. dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
  5686. val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
  5687. }
  5688. if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
  5689. if (pf->hw.debug_mask & I40E_DEBUG_AQ)
  5690. dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
  5691. val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
  5692. }
  5693. if (oldval != val)
  5694. wr32(&pf->hw, pf->hw.aq.asq.len, val);
  5695. event.buf_len = I40E_MAX_AQ_BUF_SIZE;
  5696. event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
  5697. if (!event.msg_buf)
  5698. return;
  5699. do {
  5700. ret = i40e_clean_arq_element(hw, &event, &pending);
  5701. if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK)
  5702. break;
  5703. else if (ret) {
  5704. dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
  5705. break;
  5706. }
  5707. opcode = le16_to_cpu(event.desc.opcode);
  5708. switch (opcode) {
  5709. case i40e_aqc_opc_get_link_status:
  5710. i40e_handle_link_event(pf, &event);
  5711. break;
  5712. case i40e_aqc_opc_send_msg_to_pf:
  5713. ret = i40e_vc_process_vf_msg(pf,
  5714. le16_to_cpu(event.desc.retval),
  5715. le32_to_cpu(event.desc.cookie_high),
  5716. le32_to_cpu(event.desc.cookie_low),
  5717. event.msg_buf,
  5718. event.msg_len);
  5719. break;
  5720. case i40e_aqc_opc_lldp_update_mib:
  5721. dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
  5722. #ifdef CONFIG_I40E_DCB
  5723. rtnl_lock();
  5724. ret = i40e_handle_lldp_event(pf, &event);
  5725. rtnl_unlock();
  5726. #endif /* CONFIG_I40E_DCB */
  5727. break;
  5728. case i40e_aqc_opc_event_lan_overflow:
  5729. dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
  5730. i40e_handle_lan_overflow_event(pf, &event);
  5731. break;
  5732. case i40e_aqc_opc_send_msg_to_peer:
  5733. dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
  5734. break;
  5735. case i40e_aqc_opc_nvm_erase:
  5736. case i40e_aqc_opc_nvm_update:
  5737. case i40e_aqc_opc_oem_post_update:
  5738. i40e_debug(&pf->hw, I40E_DEBUG_NVM,
  5739. "ARQ NVM operation 0x%04x completed\n",
  5740. opcode);
  5741. break;
  5742. default:
  5743. dev_info(&pf->pdev->dev,
  5744. "ARQ: Unknown event 0x%04x ignored\n",
  5745. opcode);
  5746. break;
  5747. }
  5748. } while (i++ < pf->adminq_work_limit);
  5749. if (i < pf->adminq_work_limit)
  5750. clear_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
  5751. /* re-enable Admin queue interrupt cause */
  5752. val = rd32(hw, I40E_PFINT_ICR0_ENA);
  5753. val |= I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
  5754. wr32(hw, I40E_PFINT_ICR0_ENA, val);
  5755. i40e_flush(hw);
  5756. kfree(event.msg_buf);
  5757. }
  5758. /**
  5759. * i40e_verify_eeprom - make sure eeprom is good to use
  5760. * @pf: board private structure
  5761. **/
  5762. static void i40e_verify_eeprom(struct i40e_pf *pf)
  5763. {
  5764. int err;
  5765. err = i40e_diag_eeprom_test(&pf->hw);
  5766. if (err) {
  5767. /* retry in case of garbage read */
  5768. err = i40e_diag_eeprom_test(&pf->hw);
  5769. if (err) {
  5770. dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
  5771. err);
  5772. set_bit(__I40E_BAD_EEPROM, &pf->state);
  5773. }
  5774. }
  5775. if (!err && test_bit(__I40E_BAD_EEPROM, &pf->state)) {
  5776. dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
  5777. clear_bit(__I40E_BAD_EEPROM, &pf->state);
  5778. }
  5779. }
  5780. /**
  5781. * i40e_enable_pf_switch_lb
  5782. * @pf: pointer to the PF structure
  5783. *
  5784. * enable switch loop back or die - no point in a return value
  5785. **/
  5786. static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
  5787. {
  5788. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  5789. struct i40e_vsi_context ctxt;
  5790. int ret;
  5791. ctxt.seid = pf->main_vsi_seid;
  5792. ctxt.pf_num = pf->hw.pf_id;
  5793. ctxt.vf_num = 0;
  5794. ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
  5795. if (ret) {
  5796. dev_info(&pf->pdev->dev,
  5797. "couldn't get PF vsi config, err %s aq_err %s\n",
  5798. i40e_stat_str(&pf->hw, ret),
  5799. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  5800. return;
  5801. }
  5802. ctxt.flags = I40E_AQ_VSI_TYPE_PF;
  5803. ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  5804. ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  5805. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  5806. if (ret) {
  5807. dev_info(&pf->pdev->dev,
  5808. "update vsi switch failed, err %s aq_err %s\n",
  5809. i40e_stat_str(&pf->hw, ret),
  5810. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  5811. }
  5812. }
  5813. /**
  5814. * i40e_disable_pf_switch_lb
  5815. * @pf: pointer to the PF structure
  5816. *
  5817. * disable switch loop back or die - no point in a return value
  5818. **/
  5819. static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
  5820. {
  5821. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  5822. struct i40e_vsi_context ctxt;
  5823. int ret;
  5824. ctxt.seid = pf->main_vsi_seid;
  5825. ctxt.pf_num = pf->hw.pf_id;
  5826. ctxt.vf_num = 0;
  5827. ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
  5828. if (ret) {
  5829. dev_info(&pf->pdev->dev,
  5830. "couldn't get PF vsi config, err %s aq_err %s\n",
  5831. i40e_stat_str(&pf->hw, ret),
  5832. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  5833. return;
  5834. }
  5835. ctxt.flags = I40E_AQ_VSI_TYPE_PF;
  5836. ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  5837. ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  5838. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  5839. if (ret) {
  5840. dev_info(&pf->pdev->dev,
  5841. "update vsi switch failed, err %s aq_err %s\n",
  5842. i40e_stat_str(&pf->hw, ret),
  5843. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  5844. }
  5845. }
  5846. /**
  5847. * i40e_config_bridge_mode - Configure the HW bridge mode
  5848. * @veb: pointer to the bridge instance
  5849. *
  5850. * Configure the loop back mode for the LAN VSI that is downlink to the
  5851. * specified HW bridge instance. It is expected this function is called
  5852. * when a new HW bridge is instantiated.
  5853. **/
  5854. static void i40e_config_bridge_mode(struct i40e_veb *veb)
  5855. {
  5856. struct i40e_pf *pf = veb->pf;
  5857. if (pf->hw.debug_mask & I40E_DEBUG_LAN)
  5858. dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
  5859. veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
  5860. if (veb->bridge_mode & BRIDGE_MODE_VEPA)
  5861. i40e_disable_pf_switch_lb(pf);
  5862. else
  5863. i40e_enable_pf_switch_lb(pf);
  5864. }
  5865. /**
  5866. * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
  5867. * @veb: pointer to the VEB instance
  5868. *
  5869. * This is a recursive function that first builds the attached VSIs then
  5870. * recurses in to build the next layer of VEB. We track the connections
  5871. * through our own index numbers because the seid's from the HW could
  5872. * change across the reset.
  5873. **/
  5874. static int i40e_reconstitute_veb(struct i40e_veb *veb)
  5875. {
  5876. struct i40e_vsi *ctl_vsi = NULL;
  5877. struct i40e_pf *pf = veb->pf;
  5878. int v, veb_idx;
  5879. int ret;
  5880. /* build VSI that owns this VEB, temporarily attached to base VEB */
  5881. for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
  5882. if (pf->vsi[v] &&
  5883. pf->vsi[v]->veb_idx == veb->idx &&
  5884. pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
  5885. ctl_vsi = pf->vsi[v];
  5886. break;
  5887. }
  5888. }
  5889. if (!ctl_vsi) {
  5890. dev_info(&pf->pdev->dev,
  5891. "missing owner VSI for veb_idx %d\n", veb->idx);
  5892. ret = -ENOENT;
  5893. goto end_reconstitute;
  5894. }
  5895. if (ctl_vsi != pf->vsi[pf->lan_vsi])
  5896. ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
  5897. ret = i40e_add_vsi(ctl_vsi);
  5898. if (ret) {
  5899. dev_info(&pf->pdev->dev,
  5900. "rebuild of veb_idx %d owner VSI failed: %d\n",
  5901. veb->idx, ret);
  5902. goto end_reconstitute;
  5903. }
  5904. i40e_vsi_reset_stats(ctl_vsi);
  5905. /* create the VEB in the switch and move the VSI onto the VEB */
  5906. ret = i40e_add_veb(veb, ctl_vsi);
  5907. if (ret)
  5908. goto end_reconstitute;
  5909. if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
  5910. veb->bridge_mode = BRIDGE_MODE_VEB;
  5911. else
  5912. veb->bridge_mode = BRIDGE_MODE_VEPA;
  5913. i40e_config_bridge_mode(veb);
  5914. /* create the remaining VSIs attached to this VEB */
  5915. for (v = 0; v < pf->num_alloc_vsi; v++) {
  5916. if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
  5917. continue;
  5918. if (pf->vsi[v]->veb_idx == veb->idx) {
  5919. struct i40e_vsi *vsi = pf->vsi[v];
  5920. vsi->uplink_seid = veb->seid;
  5921. ret = i40e_add_vsi(vsi);
  5922. if (ret) {
  5923. dev_info(&pf->pdev->dev,
  5924. "rebuild of vsi_idx %d failed: %d\n",
  5925. v, ret);
  5926. goto end_reconstitute;
  5927. }
  5928. i40e_vsi_reset_stats(vsi);
  5929. }
  5930. }
  5931. /* create any VEBs attached to this VEB - RECURSION */
  5932. for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
  5933. if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
  5934. pf->veb[veb_idx]->uplink_seid = veb->seid;
  5935. ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
  5936. if (ret)
  5937. break;
  5938. }
  5939. }
  5940. end_reconstitute:
  5941. return ret;
  5942. }
  5943. /**
  5944. * i40e_get_capabilities - get info about the HW
  5945. * @pf: the PF struct
  5946. **/
  5947. static int i40e_get_capabilities(struct i40e_pf *pf)
  5948. {
  5949. struct i40e_aqc_list_capabilities_element_resp *cap_buf;
  5950. u16 data_size;
  5951. int buf_len;
  5952. int err;
  5953. buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
  5954. do {
  5955. cap_buf = kzalloc(buf_len, GFP_KERNEL);
  5956. if (!cap_buf)
  5957. return -ENOMEM;
  5958. /* this loads the data into the hw struct for us */
  5959. err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
  5960. &data_size,
  5961. i40e_aqc_opc_list_func_capabilities,
  5962. NULL);
  5963. /* data loaded, buffer no longer needed */
  5964. kfree(cap_buf);
  5965. if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
  5966. /* retry with a larger buffer */
  5967. buf_len = data_size;
  5968. } else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK) {
  5969. dev_info(&pf->pdev->dev,
  5970. "capability discovery failed, err %s aq_err %s\n",
  5971. i40e_stat_str(&pf->hw, err),
  5972. i40e_aq_str(&pf->hw,
  5973. pf->hw.aq.asq_last_status));
  5974. return -ENODEV;
  5975. }
  5976. } while (err);
  5977. if (pf->hw.debug_mask & I40E_DEBUG_USER)
  5978. dev_info(&pf->pdev->dev,
  5979. "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",
  5980. pf->hw.pf_id, pf->hw.func_caps.num_vfs,
  5981. pf->hw.func_caps.num_msix_vectors,
  5982. pf->hw.func_caps.num_msix_vectors_vf,
  5983. pf->hw.func_caps.fd_filters_guaranteed,
  5984. pf->hw.func_caps.fd_filters_best_effort,
  5985. pf->hw.func_caps.num_tx_qp,
  5986. pf->hw.func_caps.num_vsis);
  5987. #define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
  5988. + pf->hw.func_caps.num_vfs)
  5989. if (pf->hw.revision_id == 0 && (DEF_NUM_VSI > pf->hw.func_caps.num_vsis)) {
  5990. dev_info(&pf->pdev->dev,
  5991. "got num_vsis %d, setting num_vsis to %d\n",
  5992. pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
  5993. pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
  5994. }
  5995. return 0;
  5996. }
  5997. static int i40e_vsi_clear(struct i40e_vsi *vsi);
  5998. /**
  5999. * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
  6000. * @pf: board private structure
  6001. **/
  6002. static void i40e_fdir_sb_setup(struct i40e_pf *pf)
  6003. {
  6004. struct i40e_vsi *vsi;
  6005. /* quick workaround for an NVM issue that leaves a critical register
  6006. * uninitialized
  6007. */
  6008. if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
  6009. static const u32 hkey[] = {
  6010. 0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
  6011. 0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
  6012. 0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
  6013. 0x95b3a76d};
  6014. int i;
  6015. for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
  6016. wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
  6017. }
  6018. if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
  6019. return;
  6020. /* find existing VSI and see if it needs configuring */
  6021. vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
  6022. /* create a new VSI if none exists */
  6023. if (!vsi) {
  6024. vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
  6025. pf->vsi[pf->lan_vsi]->seid, 0);
  6026. if (!vsi) {
  6027. dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
  6028. pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
  6029. return;
  6030. }
  6031. }
  6032. i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
  6033. }
  6034. /**
  6035. * i40e_fdir_teardown - release the Flow Director resources
  6036. * @pf: board private structure
  6037. **/
  6038. static void i40e_fdir_teardown(struct i40e_pf *pf)
  6039. {
  6040. struct i40e_vsi *vsi;
  6041. i40e_fdir_filter_exit(pf);
  6042. vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
  6043. if (vsi)
  6044. i40e_vsi_release(vsi);
  6045. }
  6046. /**
  6047. * i40e_prep_for_reset - prep for the core to reset
  6048. * @pf: board private structure
  6049. * @lock_acquired: indicates whether or not the lock has been acquired
  6050. * before this function was called.
  6051. *
  6052. * Close up the VFs and other things in prep for PF Reset.
  6053. **/
  6054. static void i40e_prep_for_reset(struct i40e_pf *pf, bool lock_acquired)
  6055. {
  6056. struct i40e_hw *hw = &pf->hw;
  6057. i40e_status ret = 0;
  6058. u32 v;
  6059. clear_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
  6060. if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
  6061. return;
  6062. if (i40e_check_asq_alive(&pf->hw))
  6063. i40e_vc_notify_reset(pf);
  6064. dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
  6065. /* quiesce the VSIs and their queues that are not already DOWN */
  6066. /* pf_quiesce_all_vsi modifies netdev structures -rtnl_lock needed */
  6067. if (!lock_acquired)
  6068. rtnl_lock();
  6069. i40e_pf_quiesce_all_vsi(pf);
  6070. if (!lock_acquired)
  6071. rtnl_unlock();
  6072. for (v = 0; v < pf->num_alloc_vsi; v++) {
  6073. if (pf->vsi[v])
  6074. pf->vsi[v]->seid = 0;
  6075. }
  6076. i40e_shutdown_adminq(&pf->hw);
  6077. /* call shutdown HMC */
  6078. if (hw->hmc.hmc_obj) {
  6079. ret = i40e_shutdown_lan_hmc(hw);
  6080. if (ret)
  6081. dev_warn(&pf->pdev->dev,
  6082. "shutdown_lan_hmc failed: %d\n", ret);
  6083. }
  6084. }
  6085. /**
  6086. * i40e_send_version - update firmware with driver version
  6087. * @pf: PF struct
  6088. */
  6089. static void i40e_send_version(struct i40e_pf *pf)
  6090. {
  6091. struct i40e_driver_version dv;
  6092. dv.major_version = DRV_VERSION_MAJOR;
  6093. dv.minor_version = DRV_VERSION_MINOR;
  6094. dv.build_version = DRV_VERSION_BUILD;
  6095. dv.subbuild_version = 0;
  6096. strlcpy(dv.driver_string, DRV_VERSION, sizeof(dv.driver_string));
  6097. i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
  6098. }
  6099. /**
  6100. * i40e_reset - wait for core reset to finish reset, reset pf if corer not seen
  6101. * @pf: board private structure
  6102. **/
  6103. static int i40e_reset(struct i40e_pf *pf)
  6104. {
  6105. struct i40e_hw *hw = &pf->hw;
  6106. i40e_status ret;
  6107. ret = i40e_pf_reset(hw);
  6108. if (ret) {
  6109. dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
  6110. set_bit(__I40E_RESET_FAILED, &pf->state);
  6111. clear_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state);
  6112. } else {
  6113. pf->pfr_count++;
  6114. }
  6115. return ret;
  6116. }
  6117. /**
  6118. * i40e_rebuild - rebuild using a saved config
  6119. * @pf: board private structure
  6120. * @reinit: if the Main VSI needs to re-initialized.
  6121. * @lock_acquired: indicates whether or not the lock has been acquired
  6122. * before this function was called.
  6123. **/
  6124. static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired)
  6125. {
  6126. struct i40e_hw *hw = &pf->hw;
  6127. u8 set_fc_aq_fail = 0;
  6128. i40e_status ret;
  6129. u32 val;
  6130. int v;
  6131. if (test_bit(__I40E_DOWN, &pf->state))
  6132. goto clear_recovery;
  6133. dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
  6134. /* rebuild the basics for the AdminQ, HMC, and initial HW switch */
  6135. ret = i40e_init_adminq(&pf->hw);
  6136. if (ret) {
  6137. dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %s aq_err %s\n",
  6138. i40e_stat_str(&pf->hw, ret),
  6139. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  6140. goto clear_recovery;
  6141. }
  6142. /* re-verify the eeprom if we just had an EMP reset */
  6143. if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, &pf->state))
  6144. i40e_verify_eeprom(pf);
  6145. i40e_clear_pxe_mode(hw);
  6146. ret = i40e_get_capabilities(pf);
  6147. if (ret)
  6148. goto end_core_reset;
  6149. ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
  6150. hw->func_caps.num_rx_qp, 0, 0);
  6151. if (ret) {
  6152. dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
  6153. goto end_core_reset;
  6154. }
  6155. ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
  6156. if (ret) {
  6157. dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
  6158. goto end_core_reset;
  6159. }
  6160. #ifdef CONFIG_I40E_DCB
  6161. ret = i40e_init_pf_dcb(pf);
  6162. if (ret) {
  6163. dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n", ret);
  6164. pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
  6165. /* Continue without DCB enabled */
  6166. }
  6167. #endif /* CONFIG_I40E_DCB */
  6168. /* do basic switch setup */
  6169. if (!lock_acquired)
  6170. rtnl_lock();
  6171. ret = i40e_setup_pf_switch(pf, reinit);
  6172. if (ret)
  6173. goto end_unlock;
  6174. /* The driver only wants link up/down and module qualification
  6175. * reports from firmware. Note the negative logic.
  6176. */
  6177. ret = i40e_aq_set_phy_int_mask(&pf->hw,
  6178. ~(I40E_AQ_EVENT_LINK_UPDOWN |
  6179. I40E_AQ_EVENT_MEDIA_NA |
  6180. I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
  6181. if (ret)
  6182. dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
  6183. i40e_stat_str(&pf->hw, ret),
  6184. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  6185. /* make sure our flow control settings are restored */
  6186. ret = i40e_set_fc(&pf->hw, &set_fc_aq_fail, true);
  6187. if (ret)
  6188. dev_dbg(&pf->pdev->dev, "setting flow control: ret = %s last_status = %s\n",
  6189. i40e_stat_str(&pf->hw, ret),
  6190. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  6191. /* Rebuild the VSIs and VEBs that existed before reset.
  6192. * They are still in our local switch element arrays, so only
  6193. * need to rebuild the switch model in the HW.
  6194. *
  6195. * If there were VEBs but the reconstitution failed, we'll try
  6196. * try to recover minimal use by getting the basic PF VSI working.
  6197. */
  6198. if (pf->vsi[pf->lan_vsi]->uplink_seid != pf->mac_seid) {
  6199. dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
  6200. /* find the one VEB connected to the MAC, and find orphans */
  6201. for (v = 0; v < I40E_MAX_VEB; v++) {
  6202. if (!pf->veb[v])
  6203. continue;
  6204. if (pf->veb[v]->uplink_seid == pf->mac_seid ||
  6205. pf->veb[v]->uplink_seid == 0) {
  6206. ret = i40e_reconstitute_veb(pf->veb[v]);
  6207. if (!ret)
  6208. continue;
  6209. /* If Main VEB failed, we're in deep doodoo,
  6210. * so give up rebuilding the switch and set up
  6211. * for minimal rebuild of PF VSI.
  6212. * If orphan failed, we'll report the error
  6213. * but try to keep going.
  6214. */
  6215. if (pf->veb[v]->uplink_seid == pf->mac_seid) {
  6216. dev_info(&pf->pdev->dev,
  6217. "rebuild of switch failed: %d, will try to set up simple PF connection\n",
  6218. ret);
  6219. pf->vsi[pf->lan_vsi]->uplink_seid
  6220. = pf->mac_seid;
  6221. break;
  6222. } else if (pf->veb[v]->uplink_seid == 0) {
  6223. dev_info(&pf->pdev->dev,
  6224. "rebuild of orphan VEB failed: %d\n",
  6225. ret);
  6226. }
  6227. }
  6228. }
  6229. }
  6230. if (pf->vsi[pf->lan_vsi]->uplink_seid == pf->mac_seid) {
  6231. dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
  6232. /* no VEB, so rebuild only the Main VSI */
  6233. ret = i40e_add_vsi(pf->vsi[pf->lan_vsi]);
  6234. if (ret) {
  6235. dev_info(&pf->pdev->dev,
  6236. "rebuild of Main VSI failed: %d\n", ret);
  6237. goto end_unlock;
  6238. }
  6239. }
  6240. /* Reconfigure hardware for allowing smaller MSS in the case
  6241. * of TSO, so that we avoid the MDD being fired and causing
  6242. * a reset in the case of small MSS+TSO.
  6243. */
  6244. #define I40E_REG_MSS 0x000E64DC
  6245. #define I40E_REG_MSS_MIN_MASK 0x3FF0000
  6246. #define I40E_64BYTE_MSS 0x400000
  6247. val = rd32(hw, I40E_REG_MSS);
  6248. if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
  6249. val &= ~I40E_REG_MSS_MIN_MASK;
  6250. val |= I40E_64BYTE_MSS;
  6251. wr32(hw, I40E_REG_MSS, val);
  6252. }
  6253. if (pf->flags & I40E_FLAG_RESTART_AUTONEG) {
  6254. msleep(75);
  6255. ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
  6256. if (ret)
  6257. dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
  6258. i40e_stat_str(&pf->hw, ret),
  6259. i40e_aq_str(&pf->hw,
  6260. pf->hw.aq.asq_last_status));
  6261. }
  6262. /* reinit the misc interrupt */
  6263. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  6264. ret = i40e_setup_misc_vector(pf);
  6265. /* Add a filter to drop all Flow control frames from any VSI from being
  6266. * transmitted. By doing so we stop a malicious VF from sending out
  6267. * PAUSE or PFC frames and potentially controlling traffic for other
  6268. * PF/VF VSIs.
  6269. * The FW can still send Flow control frames if enabled.
  6270. */
  6271. i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
  6272. pf->main_vsi_seid);
  6273. /* restart the VSIs that were rebuilt and running before the reset */
  6274. i40e_pf_unquiesce_all_vsi(pf);
  6275. /* Release the RTNL lock before we start resetting VFs */
  6276. if (!lock_acquired)
  6277. rtnl_unlock();
  6278. i40e_reset_all_vfs(pf, true);
  6279. /* tell the firmware that we're starting */
  6280. i40e_send_version(pf);
  6281. /* We've already released the lock, so don't do it again */
  6282. goto end_core_reset;
  6283. end_unlock:
  6284. if (!lock_acquired)
  6285. rtnl_unlock();
  6286. end_core_reset:
  6287. clear_bit(__I40E_RESET_FAILED, &pf->state);
  6288. clear_recovery:
  6289. clear_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state);
  6290. }
  6291. /**
  6292. * i40e_reset_and_rebuild - reset and rebuild using a saved config
  6293. * @pf: board private structure
  6294. * @reinit: if the Main VSI needs to re-initialized.
  6295. * @lock_acquired: indicates whether or not the lock has been acquired
  6296. * before this function was called.
  6297. **/
  6298. static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
  6299. bool lock_acquired)
  6300. {
  6301. int ret;
  6302. /* Now we wait for GRST to settle out.
  6303. * We don't have to delete the VEBs or VSIs from the hw switch
  6304. * because the reset will make them disappear.
  6305. */
  6306. ret = i40e_reset(pf);
  6307. if (!ret)
  6308. i40e_rebuild(pf, reinit, lock_acquired);
  6309. }
  6310. /**
  6311. * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
  6312. * @pf: board private structure
  6313. *
  6314. * Close up the VFs and other things in prep for a Core Reset,
  6315. * then get ready to rebuild the world.
  6316. * @lock_acquired: indicates whether or not the lock has been acquired
  6317. * before this function was called.
  6318. **/
  6319. static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired)
  6320. {
  6321. i40e_prep_for_reset(pf, lock_acquired);
  6322. i40e_reset_and_rebuild(pf, false, lock_acquired);
  6323. }
  6324. /**
  6325. * i40e_handle_mdd_event
  6326. * @pf: pointer to the PF structure
  6327. *
  6328. * Called from the MDD irq handler to identify possibly malicious vfs
  6329. **/
  6330. static void i40e_handle_mdd_event(struct i40e_pf *pf)
  6331. {
  6332. struct i40e_hw *hw = &pf->hw;
  6333. bool mdd_detected = false;
  6334. bool pf_mdd_detected = false;
  6335. struct i40e_vf *vf;
  6336. u32 reg;
  6337. int i;
  6338. if (!test_bit(__I40E_MDD_EVENT_PENDING, &pf->state))
  6339. return;
  6340. /* find what triggered the MDD event */
  6341. reg = rd32(hw, I40E_GL_MDET_TX);
  6342. if (reg & I40E_GL_MDET_TX_VALID_MASK) {
  6343. u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
  6344. I40E_GL_MDET_TX_PF_NUM_SHIFT;
  6345. u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
  6346. I40E_GL_MDET_TX_VF_NUM_SHIFT;
  6347. u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
  6348. I40E_GL_MDET_TX_EVENT_SHIFT;
  6349. u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
  6350. I40E_GL_MDET_TX_QUEUE_SHIFT) -
  6351. pf->hw.func_caps.base_queue;
  6352. if (netif_msg_tx_err(pf))
  6353. dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on TX queue %d PF number 0x%02x VF number 0x%02x\n",
  6354. event, queue, pf_num, vf_num);
  6355. wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
  6356. mdd_detected = true;
  6357. }
  6358. reg = rd32(hw, I40E_GL_MDET_RX);
  6359. if (reg & I40E_GL_MDET_RX_VALID_MASK) {
  6360. u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
  6361. I40E_GL_MDET_RX_FUNCTION_SHIFT;
  6362. u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
  6363. I40E_GL_MDET_RX_EVENT_SHIFT;
  6364. u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
  6365. I40E_GL_MDET_RX_QUEUE_SHIFT) -
  6366. pf->hw.func_caps.base_queue;
  6367. if (netif_msg_rx_err(pf))
  6368. dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
  6369. event, queue, func);
  6370. wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
  6371. mdd_detected = true;
  6372. }
  6373. if (mdd_detected) {
  6374. reg = rd32(hw, I40E_PF_MDET_TX);
  6375. if (reg & I40E_PF_MDET_TX_VALID_MASK) {
  6376. wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
  6377. dev_info(&pf->pdev->dev, "TX driver issue detected, PF reset issued\n");
  6378. pf_mdd_detected = true;
  6379. }
  6380. reg = rd32(hw, I40E_PF_MDET_RX);
  6381. if (reg & I40E_PF_MDET_RX_VALID_MASK) {
  6382. wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
  6383. dev_info(&pf->pdev->dev, "RX driver issue detected, PF reset issued\n");
  6384. pf_mdd_detected = true;
  6385. }
  6386. /* Queue belongs to the PF, initiate a reset */
  6387. if (pf_mdd_detected) {
  6388. set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
  6389. i40e_service_event_schedule(pf);
  6390. }
  6391. }
  6392. /* see if one of the VFs needs its hand slapped */
  6393. for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
  6394. vf = &(pf->vf[i]);
  6395. reg = rd32(hw, I40E_VP_MDET_TX(i));
  6396. if (reg & I40E_VP_MDET_TX_VALID_MASK) {
  6397. wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
  6398. vf->num_mdd_events++;
  6399. dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
  6400. i);
  6401. }
  6402. reg = rd32(hw, I40E_VP_MDET_RX(i));
  6403. if (reg & I40E_VP_MDET_RX_VALID_MASK) {
  6404. wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
  6405. vf->num_mdd_events++;
  6406. dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
  6407. i);
  6408. }
  6409. if (vf->num_mdd_events > I40E_DEFAULT_NUM_MDD_EVENTS_ALLOWED) {
  6410. dev_info(&pf->pdev->dev,
  6411. "Too many MDD events on VF %d, disabled\n", i);
  6412. dev_info(&pf->pdev->dev,
  6413. "Use PF Control I/F to re-enable the VF\n");
  6414. set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
  6415. }
  6416. }
  6417. /* re-enable mdd interrupt cause */
  6418. clear_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
  6419. reg = rd32(hw, I40E_PFINT_ICR0_ENA);
  6420. reg |= I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
  6421. wr32(hw, I40E_PFINT_ICR0_ENA, reg);
  6422. i40e_flush(hw);
  6423. }
  6424. /**
  6425. * i40e_sync_udp_filters_subtask - Sync the VSI filter list with HW
  6426. * @pf: board private structure
  6427. **/
  6428. static void i40e_sync_udp_filters_subtask(struct i40e_pf *pf)
  6429. {
  6430. struct i40e_hw *hw = &pf->hw;
  6431. i40e_status ret;
  6432. u16 port;
  6433. int i;
  6434. if (!(pf->flags & I40E_FLAG_UDP_FILTER_SYNC))
  6435. return;
  6436. pf->flags &= ~I40E_FLAG_UDP_FILTER_SYNC;
  6437. for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
  6438. if (pf->pending_udp_bitmap & BIT_ULL(i)) {
  6439. pf->pending_udp_bitmap &= ~BIT_ULL(i);
  6440. port = pf->udp_ports[i].port;
  6441. if (port)
  6442. ret = i40e_aq_add_udp_tunnel(hw, port,
  6443. pf->udp_ports[i].type,
  6444. NULL, NULL);
  6445. else
  6446. ret = i40e_aq_del_udp_tunnel(hw, i, NULL);
  6447. if (ret) {
  6448. dev_dbg(&pf->pdev->dev,
  6449. "%s %s port %d, index %d failed, err %s aq_err %s\n",
  6450. pf->udp_ports[i].type ? "vxlan" : "geneve",
  6451. port ? "add" : "delete",
  6452. port, i,
  6453. i40e_stat_str(&pf->hw, ret),
  6454. i40e_aq_str(&pf->hw,
  6455. pf->hw.aq.asq_last_status));
  6456. pf->udp_ports[i].port = 0;
  6457. }
  6458. }
  6459. }
  6460. }
  6461. /**
  6462. * i40e_service_task - Run the driver's async subtasks
  6463. * @work: pointer to work_struct containing our data
  6464. **/
  6465. static void i40e_service_task(struct work_struct *work)
  6466. {
  6467. struct i40e_pf *pf = container_of(work,
  6468. struct i40e_pf,
  6469. service_task);
  6470. unsigned long start_time = jiffies;
  6471. /* don't bother with service tasks if a reset is in progress */
  6472. if (test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
  6473. return;
  6474. }
  6475. if (test_and_set_bit(__I40E_SERVICE_SCHED, &pf->state))
  6476. return;
  6477. i40e_detect_recover_hung(pf);
  6478. i40e_sync_filters_subtask(pf);
  6479. i40e_reset_subtask(pf);
  6480. i40e_handle_mdd_event(pf);
  6481. i40e_vc_process_vflr_event(pf);
  6482. i40e_watchdog_subtask(pf);
  6483. i40e_fdir_reinit_subtask(pf);
  6484. if (pf->flags & I40E_FLAG_CLIENT_RESET) {
  6485. /* Client subtask will reopen next time through. */
  6486. i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], true);
  6487. pf->flags &= ~I40E_FLAG_CLIENT_RESET;
  6488. } else {
  6489. i40e_client_subtask(pf);
  6490. if (pf->flags & I40E_FLAG_CLIENT_L2_CHANGE) {
  6491. i40e_notify_client_of_l2_param_changes(
  6492. pf->vsi[pf->lan_vsi]);
  6493. pf->flags &= ~I40E_FLAG_CLIENT_L2_CHANGE;
  6494. }
  6495. }
  6496. i40e_sync_filters_subtask(pf);
  6497. i40e_sync_udp_filters_subtask(pf);
  6498. i40e_clean_adminq_subtask(pf);
  6499. /* flush memory to make sure state is correct before next watchdog */
  6500. smp_mb__before_atomic();
  6501. clear_bit(__I40E_SERVICE_SCHED, &pf->state);
  6502. /* If the tasks have taken longer than one timer cycle or there
  6503. * is more work to be done, reschedule the service task now
  6504. * rather than wait for the timer to tick again.
  6505. */
  6506. if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
  6507. test_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state) ||
  6508. test_bit(__I40E_MDD_EVENT_PENDING, &pf->state) ||
  6509. test_bit(__I40E_VFLR_EVENT_PENDING, &pf->state))
  6510. i40e_service_event_schedule(pf);
  6511. }
  6512. /**
  6513. * i40e_service_timer - timer callback
  6514. * @data: pointer to PF struct
  6515. **/
  6516. static void i40e_service_timer(unsigned long data)
  6517. {
  6518. struct i40e_pf *pf = (struct i40e_pf *)data;
  6519. mod_timer(&pf->service_timer,
  6520. round_jiffies(jiffies + pf->service_timer_period));
  6521. i40e_service_event_schedule(pf);
  6522. }
  6523. /**
  6524. * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
  6525. * @vsi: the VSI being configured
  6526. **/
  6527. static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
  6528. {
  6529. struct i40e_pf *pf = vsi->back;
  6530. switch (vsi->type) {
  6531. case I40E_VSI_MAIN:
  6532. vsi->alloc_queue_pairs = pf->num_lan_qps;
  6533. vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
  6534. I40E_REQ_DESCRIPTOR_MULTIPLE);
  6535. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  6536. vsi->num_q_vectors = pf->num_lan_msix;
  6537. else
  6538. vsi->num_q_vectors = 1;
  6539. break;
  6540. case I40E_VSI_FDIR:
  6541. vsi->alloc_queue_pairs = 1;
  6542. vsi->num_desc = ALIGN(I40E_FDIR_RING_COUNT,
  6543. I40E_REQ_DESCRIPTOR_MULTIPLE);
  6544. vsi->num_q_vectors = pf->num_fdsb_msix;
  6545. break;
  6546. case I40E_VSI_VMDQ2:
  6547. vsi->alloc_queue_pairs = pf->num_vmdq_qps;
  6548. vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
  6549. I40E_REQ_DESCRIPTOR_MULTIPLE);
  6550. vsi->num_q_vectors = pf->num_vmdq_msix;
  6551. break;
  6552. case I40E_VSI_SRIOV:
  6553. vsi->alloc_queue_pairs = pf->num_vf_qps;
  6554. vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
  6555. I40E_REQ_DESCRIPTOR_MULTIPLE);
  6556. break;
  6557. default:
  6558. WARN_ON(1);
  6559. return -ENODATA;
  6560. }
  6561. return 0;
  6562. }
  6563. /**
  6564. * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
  6565. * @type: VSI pointer
  6566. * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
  6567. *
  6568. * On error: returns error code (negative)
  6569. * On success: returns 0
  6570. **/
  6571. static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
  6572. {
  6573. int size;
  6574. int ret = 0;
  6575. /* allocate memory for both Tx and Rx ring pointers */
  6576. size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs * 2;
  6577. vsi->tx_rings = kzalloc(size, GFP_KERNEL);
  6578. if (!vsi->tx_rings)
  6579. return -ENOMEM;
  6580. vsi->rx_rings = &vsi->tx_rings[vsi->alloc_queue_pairs];
  6581. if (alloc_qvectors) {
  6582. /* allocate memory for q_vector pointers */
  6583. size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
  6584. vsi->q_vectors = kzalloc(size, GFP_KERNEL);
  6585. if (!vsi->q_vectors) {
  6586. ret = -ENOMEM;
  6587. goto err_vectors;
  6588. }
  6589. }
  6590. return ret;
  6591. err_vectors:
  6592. kfree(vsi->tx_rings);
  6593. return ret;
  6594. }
  6595. /**
  6596. * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
  6597. * @pf: board private structure
  6598. * @type: type of VSI
  6599. *
  6600. * On error: returns error code (negative)
  6601. * On success: returns vsi index in PF (positive)
  6602. **/
  6603. static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
  6604. {
  6605. int ret = -ENODEV;
  6606. struct i40e_vsi *vsi;
  6607. int vsi_idx;
  6608. int i;
  6609. /* Need to protect the allocation of the VSIs at the PF level */
  6610. mutex_lock(&pf->switch_mutex);
  6611. /* VSI list may be fragmented if VSI creation/destruction has
  6612. * been happening. We can afford to do a quick scan to look
  6613. * for any free VSIs in the list.
  6614. *
  6615. * find next empty vsi slot, looping back around if necessary
  6616. */
  6617. i = pf->next_vsi;
  6618. while (i < pf->num_alloc_vsi && pf->vsi[i])
  6619. i++;
  6620. if (i >= pf->num_alloc_vsi) {
  6621. i = 0;
  6622. while (i < pf->next_vsi && pf->vsi[i])
  6623. i++;
  6624. }
  6625. if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
  6626. vsi_idx = i; /* Found one! */
  6627. } else {
  6628. ret = -ENODEV;
  6629. goto unlock_pf; /* out of VSI slots! */
  6630. }
  6631. pf->next_vsi = ++i;
  6632. vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
  6633. if (!vsi) {
  6634. ret = -ENOMEM;
  6635. goto unlock_pf;
  6636. }
  6637. vsi->type = type;
  6638. vsi->back = pf;
  6639. set_bit(__I40E_DOWN, &vsi->state);
  6640. vsi->flags = 0;
  6641. vsi->idx = vsi_idx;
  6642. vsi->int_rate_limit = 0;
  6643. vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
  6644. pf->rss_table_size : 64;
  6645. vsi->netdev_registered = false;
  6646. vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
  6647. hash_init(vsi->mac_filter_hash);
  6648. vsi->irqs_ready = false;
  6649. ret = i40e_set_num_rings_in_vsi(vsi);
  6650. if (ret)
  6651. goto err_rings;
  6652. ret = i40e_vsi_alloc_arrays(vsi, true);
  6653. if (ret)
  6654. goto err_rings;
  6655. /* Setup default MSIX irq handler for VSI */
  6656. i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
  6657. /* Initialize VSI lock */
  6658. spin_lock_init(&vsi->mac_filter_hash_lock);
  6659. pf->vsi[vsi_idx] = vsi;
  6660. ret = vsi_idx;
  6661. goto unlock_pf;
  6662. err_rings:
  6663. pf->next_vsi = i - 1;
  6664. kfree(vsi);
  6665. unlock_pf:
  6666. mutex_unlock(&pf->switch_mutex);
  6667. return ret;
  6668. }
  6669. /**
  6670. * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
  6671. * @type: VSI pointer
  6672. * @free_qvectors: a bool to specify if q_vectors need to be freed.
  6673. *
  6674. * On error: returns error code (negative)
  6675. * On success: returns 0
  6676. **/
  6677. static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
  6678. {
  6679. /* free the ring and vector containers */
  6680. if (free_qvectors) {
  6681. kfree(vsi->q_vectors);
  6682. vsi->q_vectors = NULL;
  6683. }
  6684. kfree(vsi->tx_rings);
  6685. vsi->tx_rings = NULL;
  6686. vsi->rx_rings = NULL;
  6687. }
  6688. /**
  6689. * i40e_clear_rss_config_user - clear the user configured RSS hash keys
  6690. * and lookup table
  6691. * @vsi: Pointer to VSI structure
  6692. */
  6693. static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
  6694. {
  6695. if (!vsi)
  6696. return;
  6697. kfree(vsi->rss_hkey_user);
  6698. vsi->rss_hkey_user = NULL;
  6699. kfree(vsi->rss_lut_user);
  6700. vsi->rss_lut_user = NULL;
  6701. }
  6702. /**
  6703. * i40e_vsi_clear - Deallocate the VSI provided
  6704. * @vsi: the VSI being un-configured
  6705. **/
  6706. static int i40e_vsi_clear(struct i40e_vsi *vsi)
  6707. {
  6708. struct i40e_pf *pf;
  6709. if (!vsi)
  6710. return 0;
  6711. if (!vsi->back)
  6712. goto free_vsi;
  6713. pf = vsi->back;
  6714. mutex_lock(&pf->switch_mutex);
  6715. if (!pf->vsi[vsi->idx]) {
  6716. dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](%p,type %d)\n",
  6717. vsi->idx, vsi->idx, vsi, vsi->type);
  6718. goto unlock_vsi;
  6719. }
  6720. if (pf->vsi[vsi->idx] != vsi) {
  6721. dev_err(&pf->pdev->dev,
  6722. "pf->vsi[%d](%p, type %d) != vsi[%d](%p,type %d): no free!\n",
  6723. pf->vsi[vsi->idx]->idx,
  6724. pf->vsi[vsi->idx],
  6725. pf->vsi[vsi->idx]->type,
  6726. vsi->idx, vsi, vsi->type);
  6727. goto unlock_vsi;
  6728. }
  6729. /* updates the PF for this cleared vsi */
  6730. i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
  6731. i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
  6732. i40e_vsi_free_arrays(vsi, true);
  6733. i40e_clear_rss_config_user(vsi);
  6734. pf->vsi[vsi->idx] = NULL;
  6735. if (vsi->idx < pf->next_vsi)
  6736. pf->next_vsi = vsi->idx;
  6737. unlock_vsi:
  6738. mutex_unlock(&pf->switch_mutex);
  6739. free_vsi:
  6740. kfree(vsi);
  6741. return 0;
  6742. }
  6743. /**
  6744. * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
  6745. * @vsi: the VSI being cleaned
  6746. **/
  6747. static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
  6748. {
  6749. int i;
  6750. if (vsi->tx_rings && vsi->tx_rings[0]) {
  6751. for (i = 0; i < vsi->alloc_queue_pairs; i++) {
  6752. kfree_rcu(vsi->tx_rings[i], rcu);
  6753. vsi->tx_rings[i] = NULL;
  6754. vsi->rx_rings[i] = NULL;
  6755. }
  6756. }
  6757. }
  6758. /**
  6759. * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
  6760. * @vsi: the VSI being configured
  6761. **/
  6762. static int i40e_alloc_rings(struct i40e_vsi *vsi)
  6763. {
  6764. struct i40e_ring *tx_ring, *rx_ring;
  6765. struct i40e_pf *pf = vsi->back;
  6766. int i;
  6767. /* Set basic values in the rings to be used later during open() */
  6768. for (i = 0; i < vsi->alloc_queue_pairs; i++) {
  6769. /* allocate space for both Tx and Rx in one shot */
  6770. tx_ring = kzalloc(sizeof(struct i40e_ring) * 2, GFP_KERNEL);
  6771. if (!tx_ring)
  6772. goto err_out;
  6773. tx_ring->queue_index = i;
  6774. tx_ring->reg_idx = vsi->base_queue + i;
  6775. tx_ring->ring_active = false;
  6776. tx_ring->vsi = vsi;
  6777. tx_ring->netdev = vsi->netdev;
  6778. tx_ring->dev = &pf->pdev->dev;
  6779. tx_ring->count = vsi->num_desc;
  6780. tx_ring->size = 0;
  6781. tx_ring->dcb_tc = 0;
  6782. if (vsi->back->flags & I40E_FLAG_WB_ON_ITR_CAPABLE)
  6783. tx_ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
  6784. tx_ring->tx_itr_setting = pf->tx_itr_default;
  6785. vsi->tx_rings[i] = tx_ring;
  6786. rx_ring = &tx_ring[1];
  6787. rx_ring->queue_index = i;
  6788. rx_ring->reg_idx = vsi->base_queue + i;
  6789. rx_ring->ring_active = false;
  6790. rx_ring->vsi = vsi;
  6791. rx_ring->netdev = vsi->netdev;
  6792. rx_ring->dev = &pf->pdev->dev;
  6793. rx_ring->count = vsi->num_desc;
  6794. rx_ring->size = 0;
  6795. rx_ring->dcb_tc = 0;
  6796. rx_ring->rx_itr_setting = pf->rx_itr_default;
  6797. vsi->rx_rings[i] = rx_ring;
  6798. }
  6799. return 0;
  6800. err_out:
  6801. i40e_vsi_clear_rings(vsi);
  6802. return -ENOMEM;
  6803. }
  6804. /**
  6805. * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
  6806. * @pf: board private structure
  6807. * @vectors: the number of MSI-X vectors to request
  6808. *
  6809. * Returns the number of vectors reserved, or error
  6810. **/
  6811. static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
  6812. {
  6813. vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
  6814. I40E_MIN_MSIX, vectors);
  6815. if (vectors < 0) {
  6816. dev_info(&pf->pdev->dev,
  6817. "MSI-X vector reservation failed: %d\n", vectors);
  6818. vectors = 0;
  6819. }
  6820. return vectors;
  6821. }
  6822. /**
  6823. * i40e_init_msix - Setup the MSIX capability
  6824. * @pf: board private structure
  6825. *
  6826. * Work with the OS to set up the MSIX vectors needed.
  6827. *
  6828. * Returns the number of vectors reserved or negative on failure
  6829. **/
  6830. static int i40e_init_msix(struct i40e_pf *pf)
  6831. {
  6832. struct i40e_hw *hw = &pf->hw;
  6833. int cpus, extra_vectors;
  6834. int vectors_left;
  6835. int v_budget, i;
  6836. int v_actual;
  6837. int iwarp_requested = 0;
  6838. if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
  6839. return -ENODEV;
  6840. /* The number of vectors we'll request will be comprised of:
  6841. * - Add 1 for "other" cause for Admin Queue events, etc.
  6842. * - The number of LAN queue pairs
  6843. * - Queues being used for RSS.
  6844. * We don't need as many as max_rss_size vectors.
  6845. * use rss_size instead in the calculation since that
  6846. * is governed by number of cpus in the system.
  6847. * - assumes symmetric Tx/Rx pairing
  6848. * - The number of VMDq pairs
  6849. * - The CPU count within the NUMA node if iWARP is enabled
  6850. * Once we count this up, try the request.
  6851. *
  6852. * If we can't get what we want, we'll simplify to nearly nothing
  6853. * and try again. If that still fails, we punt.
  6854. */
  6855. vectors_left = hw->func_caps.num_msix_vectors;
  6856. v_budget = 0;
  6857. /* reserve one vector for miscellaneous handler */
  6858. if (vectors_left) {
  6859. v_budget++;
  6860. vectors_left--;
  6861. }
  6862. /* reserve some vectors for the main PF traffic queues. Initially we
  6863. * only reserve at most 50% of the available vectors, in the case that
  6864. * the number of online CPUs is large. This ensures that we can enable
  6865. * extra features as well. Once we've enabled the other features, we
  6866. * will use any remaining vectors to reach as close as we can to the
  6867. * number of online CPUs.
  6868. */
  6869. cpus = num_online_cpus();
  6870. pf->num_lan_msix = min_t(int, cpus, vectors_left / 2);
  6871. vectors_left -= pf->num_lan_msix;
  6872. /* reserve one vector for sideband flow director */
  6873. if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
  6874. if (vectors_left) {
  6875. pf->num_fdsb_msix = 1;
  6876. v_budget++;
  6877. vectors_left--;
  6878. } else {
  6879. pf->num_fdsb_msix = 0;
  6880. }
  6881. }
  6882. /* can we reserve enough for iWARP? */
  6883. if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
  6884. iwarp_requested = pf->num_iwarp_msix;
  6885. if (!vectors_left)
  6886. pf->num_iwarp_msix = 0;
  6887. else if (vectors_left < pf->num_iwarp_msix)
  6888. pf->num_iwarp_msix = 1;
  6889. v_budget += pf->num_iwarp_msix;
  6890. vectors_left -= pf->num_iwarp_msix;
  6891. }
  6892. /* any vectors left over go for VMDq support */
  6893. if (pf->flags & I40E_FLAG_VMDQ_ENABLED) {
  6894. int vmdq_vecs_wanted = pf->num_vmdq_vsis * pf->num_vmdq_qps;
  6895. int vmdq_vecs = min_t(int, vectors_left, vmdq_vecs_wanted);
  6896. if (!vectors_left) {
  6897. pf->num_vmdq_msix = 0;
  6898. pf->num_vmdq_qps = 0;
  6899. } else {
  6900. /* if we're short on vectors for what's desired, we limit
  6901. * the queues per vmdq. If this is still more than are
  6902. * available, the user will need to change the number of
  6903. * queues/vectors used by the PF later with the ethtool
  6904. * channels command
  6905. */
  6906. if (vmdq_vecs < vmdq_vecs_wanted)
  6907. pf->num_vmdq_qps = 1;
  6908. pf->num_vmdq_msix = pf->num_vmdq_qps;
  6909. v_budget += vmdq_vecs;
  6910. vectors_left -= vmdq_vecs;
  6911. }
  6912. }
  6913. /* On systems with a large number of SMP cores, we previously limited
  6914. * the number of vectors for num_lan_msix to be at most 50% of the
  6915. * available vectors, to allow for other features. Now, we add back
  6916. * the remaining vectors. However, we ensure that the total
  6917. * num_lan_msix will not exceed num_online_cpus(). To do this, we
  6918. * calculate the number of vectors we can add without going over the
  6919. * cap of CPUs. For systems with a small number of CPUs this will be
  6920. * zero.
  6921. */
  6922. extra_vectors = min_t(int, cpus - pf->num_lan_msix, vectors_left);
  6923. pf->num_lan_msix += extra_vectors;
  6924. vectors_left -= extra_vectors;
  6925. WARN(vectors_left < 0,
  6926. "Calculation of remaining vectors underflowed. This is an accounting bug when determining total MSI-X vectors.\n");
  6927. v_budget += pf->num_lan_msix;
  6928. pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
  6929. GFP_KERNEL);
  6930. if (!pf->msix_entries)
  6931. return -ENOMEM;
  6932. for (i = 0; i < v_budget; i++)
  6933. pf->msix_entries[i].entry = i;
  6934. v_actual = i40e_reserve_msix_vectors(pf, v_budget);
  6935. if (v_actual < I40E_MIN_MSIX) {
  6936. pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
  6937. kfree(pf->msix_entries);
  6938. pf->msix_entries = NULL;
  6939. pci_disable_msix(pf->pdev);
  6940. return -ENODEV;
  6941. } else if (v_actual == I40E_MIN_MSIX) {
  6942. /* Adjust for minimal MSIX use */
  6943. pf->num_vmdq_vsis = 0;
  6944. pf->num_vmdq_qps = 0;
  6945. pf->num_lan_qps = 1;
  6946. pf->num_lan_msix = 1;
  6947. } else if (!vectors_left) {
  6948. /* If we have limited resources, we will start with no vectors
  6949. * for the special features and then allocate vectors to some
  6950. * of these features based on the policy and at the end disable
  6951. * the features that did not get any vectors.
  6952. */
  6953. int vec;
  6954. dev_info(&pf->pdev->dev,
  6955. "MSI-X vector limit reached, attempting to redistribute vectors\n");
  6956. /* reserve the misc vector */
  6957. vec = v_actual - 1;
  6958. /* Scale vector usage down */
  6959. pf->num_vmdq_msix = 1; /* force VMDqs to only one vector */
  6960. pf->num_vmdq_vsis = 1;
  6961. pf->num_vmdq_qps = 1;
  6962. /* partition out the remaining vectors */
  6963. switch (vec) {
  6964. case 2:
  6965. pf->num_lan_msix = 1;
  6966. break;
  6967. case 3:
  6968. if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
  6969. pf->num_lan_msix = 1;
  6970. pf->num_iwarp_msix = 1;
  6971. } else {
  6972. pf->num_lan_msix = 2;
  6973. }
  6974. break;
  6975. default:
  6976. if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
  6977. pf->num_iwarp_msix = min_t(int, (vec / 3),
  6978. iwarp_requested);
  6979. pf->num_vmdq_vsis = min_t(int, (vec / 3),
  6980. I40E_DEFAULT_NUM_VMDQ_VSI);
  6981. } else {
  6982. pf->num_vmdq_vsis = min_t(int, (vec / 2),
  6983. I40E_DEFAULT_NUM_VMDQ_VSI);
  6984. }
  6985. if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
  6986. pf->num_fdsb_msix = 1;
  6987. vec--;
  6988. }
  6989. pf->num_lan_msix = min_t(int,
  6990. (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
  6991. pf->num_lan_msix);
  6992. pf->num_lan_qps = pf->num_lan_msix;
  6993. break;
  6994. }
  6995. }
  6996. if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
  6997. (pf->num_fdsb_msix == 0)) {
  6998. dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n");
  6999. pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
  7000. }
  7001. if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
  7002. (pf->num_vmdq_msix == 0)) {
  7003. dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
  7004. pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
  7005. }
  7006. if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
  7007. (pf->num_iwarp_msix == 0)) {
  7008. dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
  7009. pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
  7010. }
  7011. i40e_debug(&pf->hw, I40E_DEBUG_INIT,
  7012. "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n",
  7013. pf->num_lan_msix,
  7014. pf->num_vmdq_msix * pf->num_vmdq_vsis,
  7015. pf->num_fdsb_msix,
  7016. pf->num_iwarp_msix);
  7017. return v_actual;
  7018. }
  7019. /**
  7020. * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
  7021. * @vsi: the VSI being configured
  7022. * @v_idx: index of the vector in the vsi struct
  7023. * @cpu: cpu to be used on affinity_mask
  7024. *
  7025. * We allocate one q_vector. If allocation fails we return -ENOMEM.
  7026. **/
  7027. static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx, int cpu)
  7028. {
  7029. struct i40e_q_vector *q_vector;
  7030. /* allocate q_vector */
  7031. q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
  7032. if (!q_vector)
  7033. return -ENOMEM;
  7034. q_vector->vsi = vsi;
  7035. q_vector->v_idx = v_idx;
  7036. cpumask_set_cpu(cpu, &q_vector->affinity_mask);
  7037. if (vsi->netdev)
  7038. netif_napi_add(vsi->netdev, &q_vector->napi,
  7039. i40e_napi_poll, NAPI_POLL_WEIGHT);
  7040. q_vector->rx.latency_range = I40E_LOW_LATENCY;
  7041. q_vector->tx.latency_range = I40E_LOW_LATENCY;
  7042. /* tie q_vector and vsi together */
  7043. vsi->q_vectors[v_idx] = q_vector;
  7044. return 0;
  7045. }
  7046. /**
  7047. * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
  7048. * @vsi: the VSI being configured
  7049. *
  7050. * We allocate one q_vector per queue interrupt. If allocation fails we
  7051. * return -ENOMEM.
  7052. **/
  7053. static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
  7054. {
  7055. struct i40e_pf *pf = vsi->back;
  7056. int err, v_idx, num_q_vectors, current_cpu;
  7057. /* if not MSIX, give the one vector only to the LAN VSI */
  7058. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  7059. num_q_vectors = vsi->num_q_vectors;
  7060. else if (vsi == pf->vsi[pf->lan_vsi])
  7061. num_q_vectors = 1;
  7062. else
  7063. return -EINVAL;
  7064. current_cpu = cpumask_first(cpu_online_mask);
  7065. for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
  7066. err = i40e_vsi_alloc_q_vector(vsi, v_idx, current_cpu);
  7067. if (err)
  7068. goto err_out;
  7069. current_cpu = cpumask_next(current_cpu, cpu_online_mask);
  7070. if (unlikely(current_cpu >= nr_cpu_ids))
  7071. current_cpu = cpumask_first(cpu_online_mask);
  7072. }
  7073. return 0;
  7074. err_out:
  7075. while (v_idx--)
  7076. i40e_free_q_vector(vsi, v_idx);
  7077. return err;
  7078. }
  7079. /**
  7080. * i40e_init_interrupt_scheme - Determine proper interrupt scheme
  7081. * @pf: board private structure to initialize
  7082. **/
  7083. static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
  7084. {
  7085. int vectors = 0;
  7086. ssize_t size;
  7087. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  7088. vectors = i40e_init_msix(pf);
  7089. if (vectors < 0) {
  7090. pf->flags &= ~(I40E_FLAG_MSIX_ENABLED |
  7091. I40E_FLAG_IWARP_ENABLED |
  7092. I40E_FLAG_RSS_ENABLED |
  7093. I40E_FLAG_DCB_CAPABLE |
  7094. I40E_FLAG_DCB_ENABLED |
  7095. I40E_FLAG_SRIOV_ENABLED |
  7096. I40E_FLAG_FD_SB_ENABLED |
  7097. I40E_FLAG_FD_ATR_ENABLED |
  7098. I40E_FLAG_VMDQ_ENABLED);
  7099. /* rework the queue expectations without MSIX */
  7100. i40e_determine_queue_usage(pf);
  7101. }
  7102. }
  7103. if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
  7104. (pf->flags & I40E_FLAG_MSI_ENABLED)) {
  7105. dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
  7106. vectors = pci_enable_msi(pf->pdev);
  7107. if (vectors < 0) {
  7108. dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
  7109. vectors);
  7110. pf->flags &= ~I40E_FLAG_MSI_ENABLED;
  7111. }
  7112. vectors = 1; /* one MSI or Legacy vector */
  7113. }
  7114. if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
  7115. dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
  7116. /* set up vector assignment tracking */
  7117. size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
  7118. pf->irq_pile = kzalloc(size, GFP_KERNEL);
  7119. if (!pf->irq_pile) {
  7120. dev_err(&pf->pdev->dev, "error allocating irq_pile memory\n");
  7121. return -ENOMEM;
  7122. }
  7123. pf->irq_pile->num_entries = vectors;
  7124. pf->irq_pile->search_hint = 0;
  7125. /* track first vector for misc interrupts, ignore return */
  7126. (void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
  7127. return 0;
  7128. }
  7129. /**
  7130. * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
  7131. * @pf: board private structure
  7132. *
  7133. * This sets up the handler for MSIX 0, which is used to manage the
  7134. * non-queue interrupts, e.g. AdminQ and errors. This is not used
  7135. * when in MSI or Legacy interrupt mode.
  7136. **/
  7137. static int i40e_setup_misc_vector(struct i40e_pf *pf)
  7138. {
  7139. struct i40e_hw *hw = &pf->hw;
  7140. int err = 0;
  7141. /* Only request the irq if this is the first time through, and
  7142. * not when we're rebuilding after a Reset
  7143. */
  7144. if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
  7145. err = request_irq(pf->msix_entries[0].vector,
  7146. i40e_intr, 0, pf->int_name, pf);
  7147. if (err) {
  7148. dev_info(&pf->pdev->dev,
  7149. "request_irq for %s failed: %d\n",
  7150. pf->int_name, err);
  7151. return -EFAULT;
  7152. }
  7153. }
  7154. i40e_enable_misc_int_causes(pf);
  7155. /* associate no queues to the misc vector */
  7156. wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
  7157. wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K);
  7158. i40e_flush(hw);
  7159. i40e_irq_dynamic_enable_icr0(pf, true);
  7160. return err;
  7161. }
  7162. /**
  7163. * i40e_config_rss_aq - Prepare for RSS using AQ commands
  7164. * @vsi: vsi structure
  7165. * @seed: RSS hash seed
  7166. **/
  7167. static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
  7168. u8 *lut, u16 lut_size)
  7169. {
  7170. struct i40e_pf *pf = vsi->back;
  7171. struct i40e_hw *hw = &pf->hw;
  7172. int ret = 0;
  7173. if (seed) {
  7174. struct i40e_aqc_get_set_rss_key_data *seed_dw =
  7175. (struct i40e_aqc_get_set_rss_key_data *)seed;
  7176. ret = i40e_aq_set_rss_key(hw, vsi->id, seed_dw);
  7177. if (ret) {
  7178. dev_info(&pf->pdev->dev,
  7179. "Cannot set RSS key, err %s aq_err %s\n",
  7180. i40e_stat_str(hw, ret),
  7181. i40e_aq_str(hw, hw->aq.asq_last_status));
  7182. return ret;
  7183. }
  7184. }
  7185. if (lut) {
  7186. bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
  7187. ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
  7188. if (ret) {
  7189. dev_info(&pf->pdev->dev,
  7190. "Cannot set RSS lut, err %s aq_err %s\n",
  7191. i40e_stat_str(hw, ret),
  7192. i40e_aq_str(hw, hw->aq.asq_last_status));
  7193. return ret;
  7194. }
  7195. }
  7196. return ret;
  7197. }
  7198. /**
  7199. * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
  7200. * @vsi: Pointer to vsi structure
  7201. * @seed: Buffter to store the hash keys
  7202. * @lut: Buffer to store the lookup table entries
  7203. * @lut_size: Size of buffer to store the lookup table entries
  7204. *
  7205. * Return 0 on success, negative on failure
  7206. */
  7207. static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
  7208. u8 *lut, u16 lut_size)
  7209. {
  7210. struct i40e_pf *pf = vsi->back;
  7211. struct i40e_hw *hw = &pf->hw;
  7212. int ret = 0;
  7213. if (seed) {
  7214. ret = i40e_aq_get_rss_key(hw, vsi->id,
  7215. (struct i40e_aqc_get_set_rss_key_data *)seed);
  7216. if (ret) {
  7217. dev_info(&pf->pdev->dev,
  7218. "Cannot get RSS key, err %s aq_err %s\n",
  7219. i40e_stat_str(&pf->hw, ret),
  7220. i40e_aq_str(&pf->hw,
  7221. pf->hw.aq.asq_last_status));
  7222. return ret;
  7223. }
  7224. }
  7225. if (lut) {
  7226. bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
  7227. ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
  7228. if (ret) {
  7229. dev_info(&pf->pdev->dev,
  7230. "Cannot get RSS lut, err %s aq_err %s\n",
  7231. i40e_stat_str(&pf->hw, ret),
  7232. i40e_aq_str(&pf->hw,
  7233. pf->hw.aq.asq_last_status));
  7234. return ret;
  7235. }
  7236. }
  7237. return ret;
  7238. }
  7239. /**
  7240. * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
  7241. * @vsi: VSI structure
  7242. **/
  7243. static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
  7244. {
  7245. u8 seed[I40E_HKEY_ARRAY_SIZE];
  7246. struct i40e_pf *pf = vsi->back;
  7247. u8 *lut;
  7248. int ret;
  7249. if (!(pf->flags & I40E_FLAG_RSS_AQ_CAPABLE))
  7250. return 0;
  7251. if (!vsi->rss_size)
  7252. vsi->rss_size = min_t(int, pf->alloc_rss_size,
  7253. vsi->num_queue_pairs);
  7254. if (!vsi->rss_size)
  7255. return -EINVAL;
  7256. lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
  7257. if (!lut)
  7258. return -ENOMEM;
  7259. /* Use the user configured hash keys and lookup table if there is one,
  7260. * otherwise use default
  7261. */
  7262. if (vsi->rss_lut_user)
  7263. memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
  7264. else
  7265. i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
  7266. if (vsi->rss_hkey_user)
  7267. memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
  7268. else
  7269. netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
  7270. ret = i40e_config_rss_aq(vsi, seed, lut, vsi->rss_table_size);
  7271. kfree(lut);
  7272. return ret;
  7273. }
  7274. /**
  7275. * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
  7276. * @vsi: Pointer to vsi structure
  7277. * @seed: RSS hash seed
  7278. * @lut: Lookup table
  7279. * @lut_size: Lookup table size
  7280. *
  7281. * Returns 0 on success, negative on failure
  7282. **/
  7283. static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
  7284. const u8 *lut, u16 lut_size)
  7285. {
  7286. struct i40e_pf *pf = vsi->back;
  7287. struct i40e_hw *hw = &pf->hw;
  7288. u16 vf_id = vsi->vf_id;
  7289. u8 i;
  7290. /* Fill out hash function seed */
  7291. if (seed) {
  7292. u32 *seed_dw = (u32 *)seed;
  7293. if (vsi->type == I40E_VSI_MAIN) {
  7294. for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
  7295. wr32(hw, I40E_PFQF_HKEY(i), seed_dw[i]);
  7296. } else if (vsi->type == I40E_VSI_SRIOV) {
  7297. for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
  7298. wr32(hw, I40E_VFQF_HKEY1(i, vf_id), seed_dw[i]);
  7299. } else {
  7300. dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
  7301. }
  7302. }
  7303. if (lut) {
  7304. u32 *lut_dw = (u32 *)lut;
  7305. if (vsi->type == I40E_VSI_MAIN) {
  7306. if (lut_size != I40E_HLUT_ARRAY_SIZE)
  7307. return -EINVAL;
  7308. for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
  7309. wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
  7310. } else if (vsi->type == I40E_VSI_SRIOV) {
  7311. if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
  7312. return -EINVAL;
  7313. for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
  7314. wr32(hw, I40E_VFQF_HLUT1(i, vf_id), lut_dw[i]);
  7315. } else {
  7316. dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
  7317. }
  7318. }
  7319. i40e_flush(hw);
  7320. return 0;
  7321. }
  7322. /**
  7323. * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
  7324. * @vsi: Pointer to VSI structure
  7325. * @seed: Buffer to store the keys
  7326. * @lut: Buffer to store the lookup table entries
  7327. * @lut_size: Size of buffer to store the lookup table entries
  7328. *
  7329. * Returns 0 on success, negative on failure
  7330. */
  7331. static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
  7332. u8 *lut, u16 lut_size)
  7333. {
  7334. struct i40e_pf *pf = vsi->back;
  7335. struct i40e_hw *hw = &pf->hw;
  7336. u16 i;
  7337. if (seed) {
  7338. u32 *seed_dw = (u32 *)seed;
  7339. for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
  7340. seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
  7341. }
  7342. if (lut) {
  7343. u32 *lut_dw = (u32 *)lut;
  7344. if (lut_size != I40E_HLUT_ARRAY_SIZE)
  7345. return -EINVAL;
  7346. for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
  7347. lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
  7348. }
  7349. return 0;
  7350. }
  7351. /**
  7352. * i40e_config_rss - Configure RSS keys and lut
  7353. * @vsi: Pointer to VSI structure
  7354. * @seed: RSS hash seed
  7355. * @lut: Lookup table
  7356. * @lut_size: Lookup table size
  7357. *
  7358. * Returns 0 on success, negative on failure
  7359. */
  7360. int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
  7361. {
  7362. struct i40e_pf *pf = vsi->back;
  7363. if (pf->flags & I40E_FLAG_RSS_AQ_CAPABLE)
  7364. return i40e_config_rss_aq(vsi, seed, lut, lut_size);
  7365. else
  7366. return i40e_config_rss_reg(vsi, seed, lut, lut_size);
  7367. }
  7368. /**
  7369. * i40e_get_rss - Get RSS keys and lut
  7370. * @vsi: Pointer to VSI structure
  7371. * @seed: Buffer to store the keys
  7372. * @lut: Buffer to store the lookup table entries
  7373. * lut_size: Size of buffer to store the lookup table entries
  7374. *
  7375. * Returns 0 on success, negative on failure
  7376. */
  7377. int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
  7378. {
  7379. struct i40e_pf *pf = vsi->back;
  7380. if (pf->flags & I40E_FLAG_RSS_AQ_CAPABLE)
  7381. return i40e_get_rss_aq(vsi, seed, lut, lut_size);
  7382. else
  7383. return i40e_get_rss_reg(vsi, seed, lut, lut_size);
  7384. }
  7385. /**
  7386. * i40e_fill_rss_lut - Fill the RSS lookup table with default values
  7387. * @pf: Pointer to board private structure
  7388. * @lut: Lookup table
  7389. * @rss_table_size: Lookup table size
  7390. * @rss_size: Range of queue number for hashing
  7391. */
  7392. void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
  7393. u16 rss_table_size, u16 rss_size)
  7394. {
  7395. u16 i;
  7396. for (i = 0; i < rss_table_size; i++)
  7397. lut[i] = i % rss_size;
  7398. }
  7399. /**
  7400. * i40e_pf_config_rss - Prepare for RSS if used
  7401. * @pf: board private structure
  7402. **/
  7403. static int i40e_pf_config_rss(struct i40e_pf *pf)
  7404. {
  7405. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  7406. u8 seed[I40E_HKEY_ARRAY_SIZE];
  7407. u8 *lut;
  7408. struct i40e_hw *hw = &pf->hw;
  7409. u32 reg_val;
  7410. u64 hena;
  7411. int ret;
  7412. /* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
  7413. hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
  7414. ((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
  7415. hena |= i40e_pf_get_default_rss_hena(pf);
  7416. i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
  7417. i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
  7418. /* Determine the RSS table size based on the hardware capabilities */
  7419. reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
  7420. reg_val = (pf->rss_table_size == 512) ?
  7421. (reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
  7422. (reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
  7423. i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
  7424. /* Determine the RSS size of the VSI */
  7425. if (!vsi->rss_size) {
  7426. u16 qcount;
  7427. qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
  7428. vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
  7429. }
  7430. if (!vsi->rss_size)
  7431. return -EINVAL;
  7432. lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
  7433. if (!lut)
  7434. return -ENOMEM;
  7435. /* Use user configured lut if there is one, otherwise use default */
  7436. if (vsi->rss_lut_user)
  7437. memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
  7438. else
  7439. i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
  7440. /* Use user configured hash key if there is one, otherwise
  7441. * use default.
  7442. */
  7443. if (vsi->rss_hkey_user)
  7444. memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
  7445. else
  7446. netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
  7447. ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
  7448. kfree(lut);
  7449. return ret;
  7450. }
  7451. /**
  7452. * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
  7453. * @pf: board private structure
  7454. * @queue_count: the requested queue count for rss.
  7455. *
  7456. * returns 0 if rss is not enabled, if enabled returns the final rss queue
  7457. * count which may be different from the requested queue count.
  7458. * Note: expects to be called while under rtnl_lock()
  7459. **/
  7460. int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
  7461. {
  7462. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  7463. int new_rss_size;
  7464. if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
  7465. return 0;
  7466. new_rss_size = min_t(int, queue_count, pf->rss_size_max);
  7467. if (queue_count != vsi->num_queue_pairs) {
  7468. u16 qcount;
  7469. vsi->req_queue_pairs = queue_count;
  7470. i40e_prep_for_reset(pf, true);
  7471. pf->alloc_rss_size = new_rss_size;
  7472. i40e_reset_and_rebuild(pf, true, true);
  7473. /* Discard the user configured hash keys and lut, if less
  7474. * queues are enabled.
  7475. */
  7476. if (queue_count < vsi->rss_size) {
  7477. i40e_clear_rss_config_user(vsi);
  7478. dev_dbg(&pf->pdev->dev,
  7479. "discard user configured hash keys and lut\n");
  7480. }
  7481. /* Reset vsi->rss_size, as number of enabled queues changed */
  7482. qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
  7483. vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
  7484. i40e_pf_config_rss(pf);
  7485. }
  7486. dev_info(&pf->pdev->dev, "User requested queue count/HW max RSS count: %d/%d\n",
  7487. vsi->req_queue_pairs, pf->rss_size_max);
  7488. return pf->alloc_rss_size;
  7489. }
  7490. /**
  7491. * i40e_get_npar_bw_setting - Retrieve BW settings for this PF partition
  7492. * @pf: board private structure
  7493. **/
  7494. i40e_status i40e_get_npar_bw_setting(struct i40e_pf *pf)
  7495. {
  7496. i40e_status status;
  7497. bool min_valid, max_valid;
  7498. u32 max_bw, min_bw;
  7499. status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
  7500. &min_valid, &max_valid);
  7501. if (!status) {
  7502. if (min_valid)
  7503. pf->npar_min_bw = min_bw;
  7504. if (max_valid)
  7505. pf->npar_max_bw = max_bw;
  7506. }
  7507. return status;
  7508. }
  7509. /**
  7510. * i40e_set_npar_bw_setting - Set BW settings for this PF partition
  7511. * @pf: board private structure
  7512. **/
  7513. i40e_status i40e_set_npar_bw_setting(struct i40e_pf *pf)
  7514. {
  7515. struct i40e_aqc_configure_partition_bw_data bw_data;
  7516. i40e_status status;
  7517. /* Set the valid bit for this PF */
  7518. bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
  7519. bw_data.max_bw[pf->hw.pf_id] = pf->npar_max_bw & I40E_ALT_BW_VALUE_MASK;
  7520. bw_data.min_bw[pf->hw.pf_id] = pf->npar_min_bw & I40E_ALT_BW_VALUE_MASK;
  7521. /* Set the new bandwidths */
  7522. status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
  7523. return status;
  7524. }
  7525. /**
  7526. * i40e_commit_npar_bw_setting - Commit BW settings for this PF partition
  7527. * @pf: board private structure
  7528. **/
  7529. i40e_status i40e_commit_npar_bw_setting(struct i40e_pf *pf)
  7530. {
  7531. /* Commit temporary BW setting to permanent NVM image */
  7532. enum i40e_admin_queue_err last_aq_status;
  7533. i40e_status ret;
  7534. u16 nvm_word;
  7535. if (pf->hw.partition_id != 1) {
  7536. dev_info(&pf->pdev->dev,
  7537. "Commit BW only works on partition 1! This is partition %d",
  7538. pf->hw.partition_id);
  7539. ret = I40E_NOT_SUPPORTED;
  7540. goto bw_commit_out;
  7541. }
  7542. /* Acquire NVM for read access */
  7543. ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
  7544. last_aq_status = pf->hw.aq.asq_last_status;
  7545. if (ret) {
  7546. dev_info(&pf->pdev->dev,
  7547. "Cannot acquire NVM for read access, err %s aq_err %s\n",
  7548. i40e_stat_str(&pf->hw, ret),
  7549. i40e_aq_str(&pf->hw, last_aq_status));
  7550. goto bw_commit_out;
  7551. }
  7552. /* Read word 0x10 of NVM - SW compatibility word 1 */
  7553. ret = i40e_aq_read_nvm(&pf->hw,
  7554. I40E_SR_NVM_CONTROL_WORD,
  7555. 0x10, sizeof(nvm_word), &nvm_word,
  7556. false, NULL);
  7557. /* Save off last admin queue command status before releasing
  7558. * the NVM
  7559. */
  7560. last_aq_status = pf->hw.aq.asq_last_status;
  7561. i40e_release_nvm(&pf->hw);
  7562. if (ret) {
  7563. dev_info(&pf->pdev->dev, "NVM read error, err %s aq_err %s\n",
  7564. i40e_stat_str(&pf->hw, ret),
  7565. i40e_aq_str(&pf->hw, last_aq_status));
  7566. goto bw_commit_out;
  7567. }
  7568. /* Wait a bit for NVM release to complete */
  7569. msleep(50);
  7570. /* Acquire NVM for write access */
  7571. ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
  7572. last_aq_status = pf->hw.aq.asq_last_status;
  7573. if (ret) {
  7574. dev_info(&pf->pdev->dev,
  7575. "Cannot acquire NVM for write access, err %s aq_err %s\n",
  7576. i40e_stat_str(&pf->hw, ret),
  7577. i40e_aq_str(&pf->hw, last_aq_status));
  7578. goto bw_commit_out;
  7579. }
  7580. /* Write it back out unchanged to initiate update NVM,
  7581. * which will force a write of the shadow (alt) RAM to
  7582. * the NVM - thus storing the bandwidth values permanently.
  7583. */
  7584. ret = i40e_aq_update_nvm(&pf->hw,
  7585. I40E_SR_NVM_CONTROL_WORD,
  7586. 0x10, sizeof(nvm_word),
  7587. &nvm_word, true, NULL);
  7588. /* Save off last admin queue command status before releasing
  7589. * the NVM
  7590. */
  7591. last_aq_status = pf->hw.aq.asq_last_status;
  7592. i40e_release_nvm(&pf->hw);
  7593. if (ret)
  7594. dev_info(&pf->pdev->dev,
  7595. "BW settings NOT SAVED, err %s aq_err %s\n",
  7596. i40e_stat_str(&pf->hw, ret),
  7597. i40e_aq_str(&pf->hw, last_aq_status));
  7598. bw_commit_out:
  7599. return ret;
  7600. }
  7601. /**
  7602. * i40e_sw_init - Initialize general software structures (struct i40e_pf)
  7603. * @pf: board private structure to initialize
  7604. *
  7605. * i40e_sw_init initializes the Adapter private data structure.
  7606. * Fields are initialized based on PCI device information and
  7607. * OS network device settings (MTU size).
  7608. **/
  7609. static int i40e_sw_init(struct i40e_pf *pf)
  7610. {
  7611. int err = 0;
  7612. int size;
  7613. /* Set default capability flags */
  7614. pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
  7615. I40E_FLAG_MSI_ENABLED |
  7616. I40E_FLAG_MSIX_ENABLED;
  7617. /* Set default ITR */
  7618. pf->rx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_RX_DEF;
  7619. pf->tx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_TX_DEF;
  7620. /* Depending on PF configurations, it is possible that the RSS
  7621. * maximum might end up larger than the available queues
  7622. */
  7623. pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
  7624. pf->alloc_rss_size = 1;
  7625. pf->rss_table_size = pf->hw.func_caps.rss_table_size;
  7626. pf->rss_size_max = min_t(int, pf->rss_size_max,
  7627. pf->hw.func_caps.num_tx_qp);
  7628. if (pf->hw.func_caps.rss) {
  7629. pf->flags |= I40E_FLAG_RSS_ENABLED;
  7630. pf->alloc_rss_size = min_t(int, pf->rss_size_max,
  7631. num_online_cpus());
  7632. }
  7633. /* MFP mode enabled */
  7634. if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
  7635. pf->flags |= I40E_FLAG_MFP_ENABLED;
  7636. dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
  7637. if (i40e_get_npar_bw_setting(pf))
  7638. dev_warn(&pf->pdev->dev,
  7639. "Could not get NPAR bw settings\n");
  7640. else
  7641. dev_info(&pf->pdev->dev,
  7642. "Min BW = %8.8x, Max BW = %8.8x\n",
  7643. pf->npar_min_bw, pf->npar_max_bw);
  7644. }
  7645. /* FW/NVM is not yet fixed in this regard */
  7646. if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
  7647. (pf->hw.func_caps.fd_filters_best_effort > 0)) {
  7648. pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
  7649. pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
  7650. if (pf->flags & I40E_FLAG_MFP_ENABLED &&
  7651. pf->hw.num_partitions > 1)
  7652. dev_info(&pf->pdev->dev,
  7653. "Flow Director Sideband mode Disabled in MFP mode\n");
  7654. else
  7655. pf->flags |= I40E_FLAG_FD_SB_ENABLED;
  7656. pf->fdir_pf_filter_count =
  7657. pf->hw.func_caps.fd_filters_guaranteed;
  7658. pf->hw.fdir_shared_filter_count =
  7659. pf->hw.func_caps.fd_filters_best_effort;
  7660. }
  7661. if ((pf->hw.mac.type == I40E_MAC_XL710) &&
  7662. (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
  7663. (pf->hw.aq.fw_maj_ver < 4))) {
  7664. pf->flags |= I40E_FLAG_RESTART_AUTONEG;
  7665. /* No DCB support for FW < v4.33 */
  7666. pf->flags |= I40E_FLAG_NO_DCB_SUPPORT;
  7667. }
  7668. /* Disable FW LLDP if FW < v4.3 */
  7669. if ((pf->hw.mac.type == I40E_MAC_XL710) &&
  7670. (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
  7671. (pf->hw.aq.fw_maj_ver < 4)))
  7672. pf->flags |= I40E_FLAG_STOP_FW_LLDP;
  7673. /* Use the FW Set LLDP MIB API if FW > v4.40 */
  7674. if ((pf->hw.mac.type == I40E_MAC_XL710) &&
  7675. (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver >= 40)) ||
  7676. (pf->hw.aq.fw_maj_ver >= 5)))
  7677. pf->flags |= I40E_FLAG_USE_SET_LLDP_MIB;
  7678. if (pf->hw.func_caps.vmdq) {
  7679. pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
  7680. pf->flags |= I40E_FLAG_VMDQ_ENABLED;
  7681. pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
  7682. }
  7683. if (pf->hw.func_caps.iwarp) {
  7684. pf->flags |= I40E_FLAG_IWARP_ENABLED;
  7685. /* IWARP needs one extra vector for CQP just like MISC.*/
  7686. pf->num_iwarp_msix = (int)num_online_cpus() + 1;
  7687. }
  7688. #ifdef CONFIG_PCI_IOV
  7689. if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
  7690. pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
  7691. pf->flags |= I40E_FLAG_SRIOV_ENABLED;
  7692. pf->num_req_vfs = min_t(int,
  7693. pf->hw.func_caps.num_vfs,
  7694. I40E_MAX_VF_COUNT);
  7695. }
  7696. #endif /* CONFIG_PCI_IOV */
  7697. if (pf->hw.mac.type == I40E_MAC_X722) {
  7698. pf->flags |= I40E_FLAG_RSS_AQ_CAPABLE
  7699. | I40E_FLAG_128_QP_RSS_CAPABLE
  7700. | I40E_FLAG_HW_ATR_EVICT_CAPABLE
  7701. | I40E_FLAG_OUTER_UDP_CSUM_CAPABLE
  7702. | I40E_FLAG_WB_ON_ITR_CAPABLE
  7703. | I40E_FLAG_MULTIPLE_TCP_UDP_RSS_PCTYPE
  7704. | I40E_FLAG_NO_PCI_LINK_CHECK
  7705. | I40E_FLAG_USE_SET_LLDP_MIB
  7706. | I40E_FLAG_GENEVE_OFFLOAD_CAPABLE
  7707. | I40E_FLAG_PTP_L4_CAPABLE
  7708. | I40E_FLAG_WOL_MC_MAGIC_PKT_WAKE;
  7709. } else if ((pf->hw.aq.api_maj_ver > 1) ||
  7710. ((pf->hw.aq.api_maj_ver == 1) &&
  7711. (pf->hw.aq.api_min_ver > 4))) {
  7712. /* Supported in FW API version higher than 1.4 */
  7713. pf->flags |= I40E_FLAG_GENEVE_OFFLOAD_CAPABLE;
  7714. pf->hw_disabled_flags = I40E_FLAG_HW_ATR_EVICT_CAPABLE;
  7715. } else {
  7716. pf->hw_disabled_flags = I40E_FLAG_HW_ATR_EVICT_CAPABLE;
  7717. }
  7718. pf->eeprom_version = 0xDEAD;
  7719. pf->lan_veb = I40E_NO_VEB;
  7720. pf->lan_vsi = I40E_NO_VSI;
  7721. /* By default FW has this off for performance reasons */
  7722. pf->flags &= ~I40E_FLAG_VEB_STATS_ENABLED;
  7723. /* set up queue assignment tracking */
  7724. size = sizeof(struct i40e_lump_tracking)
  7725. + (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
  7726. pf->qp_pile = kzalloc(size, GFP_KERNEL);
  7727. if (!pf->qp_pile) {
  7728. err = -ENOMEM;
  7729. goto sw_init_done;
  7730. }
  7731. pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
  7732. pf->qp_pile->search_hint = 0;
  7733. pf->tx_timeout_recovery_level = 1;
  7734. mutex_init(&pf->switch_mutex);
  7735. /* If NPAR is enabled nudge the Tx scheduler */
  7736. if (pf->hw.func_caps.npar_enable && (!i40e_get_npar_bw_setting(pf)))
  7737. i40e_set_npar_bw_setting(pf);
  7738. sw_init_done:
  7739. return err;
  7740. }
  7741. /**
  7742. * i40e_set_ntuple - set the ntuple feature flag and take action
  7743. * @pf: board private structure to initialize
  7744. * @features: the feature set that the stack is suggesting
  7745. *
  7746. * returns a bool to indicate if reset needs to happen
  7747. **/
  7748. bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
  7749. {
  7750. bool need_reset = false;
  7751. /* Check if Flow Director n-tuple support was enabled or disabled. If
  7752. * the state changed, we need to reset.
  7753. */
  7754. if (features & NETIF_F_NTUPLE) {
  7755. /* Enable filters and mark for reset */
  7756. if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
  7757. need_reset = true;
  7758. /* enable FD_SB only if there is MSI-X vector */
  7759. if (pf->num_fdsb_msix > 0)
  7760. pf->flags |= I40E_FLAG_FD_SB_ENABLED;
  7761. } else {
  7762. /* turn off filters, mark for reset and clear SW filter list */
  7763. if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
  7764. need_reset = true;
  7765. i40e_fdir_filter_exit(pf);
  7766. }
  7767. pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
  7768. pf->hw_disabled_flags &= ~I40E_FLAG_FD_SB_ENABLED;
  7769. /* reset fd counters */
  7770. pf->fd_add_err = 0;
  7771. pf->fd_atr_cnt = 0;
  7772. /* if ATR was auto disabled it can be re-enabled. */
  7773. if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
  7774. (pf->hw_disabled_flags & I40E_FLAG_FD_ATR_ENABLED)) {
  7775. pf->hw_disabled_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
  7776. if (I40E_DEBUG_FD & pf->hw.debug_mask)
  7777. dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
  7778. }
  7779. }
  7780. return need_reset;
  7781. }
  7782. /**
  7783. * i40e_clear_rss_lut - clear the rx hash lookup table
  7784. * @vsi: the VSI being configured
  7785. **/
  7786. static void i40e_clear_rss_lut(struct i40e_vsi *vsi)
  7787. {
  7788. struct i40e_pf *pf = vsi->back;
  7789. struct i40e_hw *hw = &pf->hw;
  7790. u16 vf_id = vsi->vf_id;
  7791. u8 i;
  7792. if (vsi->type == I40E_VSI_MAIN) {
  7793. for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
  7794. wr32(hw, I40E_PFQF_HLUT(i), 0);
  7795. } else if (vsi->type == I40E_VSI_SRIOV) {
  7796. for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
  7797. i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0);
  7798. } else {
  7799. dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
  7800. }
  7801. }
  7802. /**
  7803. * i40e_set_features - set the netdev feature flags
  7804. * @netdev: ptr to the netdev being adjusted
  7805. * @features: the feature set that the stack is suggesting
  7806. * Note: expects to be called while under rtnl_lock()
  7807. **/
  7808. static int i40e_set_features(struct net_device *netdev,
  7809. netdev_features_t features)
  7810. {
  7811. struct i40e_netdev_priv *np = netdev_priv(netdev);
  7812. struct i40e_vsi *vsi = np->vsi;
  7813. struct i40e_pf *pf = vsi->back;
  7814. bool need_reset;
  7815. if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
  7816. i40e_pf_config_rss(pf);
  7817. else if (!(features & NETIF_F_RXHASH) &&
  7818. netdev->features & NETIF_F_RXHASH)
  7819. i40e_clear_rss_lut(vsi);
  7820. if (features & NETIF_F_HW_VLAN_CTAG_RX)
  7821. i40e_vlan_stripping_enable(vsi);
  7822. else
  7823. i40e_vlan_stripping_disable(vsi);
  7824. need_reset = i40e_set_ntuple(pf, features);
  7825. if (need_reset)
  7826. i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED), true);
  7827. return 0;
  7828. }
  7829. /**
  7830. * i40e_get_udp_port_idx - Lookup a possibly offloaded for Rx UDP port
  7831. * @pf: board private structure
  7832. * @port: The UDP port to look up
  7833. *
  7834. * Returns the index number or I40E_MAX_PF_UDP_OFFLOAD_PORTS if port not found
  7835. **/
  7836. static u8 i40e_get_udp_port_idx(struct i40e_pf *pf, u16 port)
  7837. {
  7838. u8 i;
  7839. for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
  7840. if (pf->udp_ports[i].port == port)
  7841. return i;
  7842. }
  7843. return i;
  7844. }
  7845. /**
  7846. * i40e_udp_tunnel_add - Get notifications about UDP tunnel ports that come up
  7847. * @netdev: This physical port's netdev
  7848. * @ti: Tunnel endpoint information
  7849. **/
  7850. static void i40e_udp_tunnel_add(struct net_device *netdev,
  7851. struct udp_tunnel_info *ti)
  7852. {
  7853. struct i40e_netdev_priv *np = netdev_priv(netdev);
  7854. struct i40e_vsi *vsi = np->vsi;
  7855. struct i40e_pf *pf = vsi->back;
  7856. u16 port = ntohs(ti->port);
  7857. u8 next_idx;
  7858. u8 idx;
  7859. idx = i40e_get_udp_port_idx(pf, port);
  7860. /* Check if port already exists */
  7861. if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
  7862. netdev_info(netdev, "port %d already offloaded\n", port);
  7863. return;
  7864. }
  7865. /* Now check if there is space to add the new port */
  7866. next_idx = i40e_get_udp_port_idx(pf, 0);
  7867. if (next_idx == I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
  7868. netdev_info(netdev, "maximum number of offloaded UDP ports reached, not adding port %d\n",
  7869. port);
  7870. return;
  7871. }
  7872. switch (ti->type) {
  7873. case UDP_TUNNEL_TYPE_VXLAN:
  7874. pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_VXLAN;
  7875. break;
  7876. case UDP_TUNNEL_TYPE_GENEVE:
  7877. if (!(pf->flags & I40E_FLAG_GENEVE_OFFLOAD_CAPABLE))
  7878. return;
  7879. pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_NGE;
  7880. break;
  7881. default:
  7882. return;
  7883. }
  7884. /* New port: add it and mark its index in the bitmap */
  7885. pf->udp_ports[next_idx].port = port;
  7886. pf->pending_udp_bitmap |= BIT_ULL(next_idx);
  7887. pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
  7888. }
  7889. /**
  7890. * i40e_udp_tunnel_del - Get notifications about UDP tunnel ports that go away
  7891. * @netdev: This physical port's netdev
  7892. * @ti: Tunnel endpoint information
  7893. **/
  7894. static void i40e_udp_tunnel_del(struct net_device *netdev,
  7895. struct udp_tunnel_info *ti)
  7896. {
  7897. struct i40e_netdev_priv *np = netdev_priv(netdev);
  7898. struct i40e_vsi *vsi = np->vsi;
  7899. struct i40e_pf *pf = vsi->back;
  7900. u16 port = ntohs(ti->port);
  7901. u8 idx;
  7902. idx = i40e_get_udp_port_idx(pf, port);
  7903. /* Check if port already exists */
  7904. if (idx >= I40E_MAX_PF_UDP_OFFLOAD_PORTS)
  7905. goto not_found;
  7906. switch (ti->type) {
  7907. case UDP_TUNNEL_TYPE_VXLAN:
  7908. if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_VXLAN)
  7909. goto not_found;
  7910. break;
  7911. case UDP_TUNNEL_TYPE_GENEVE:
  7912. if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_NGE)
  7913. goto not_found;
  7914. break;
  7915. default:
  7916. goto not_found;
  7917. }
  7918. /* if port exists, set it to 0 (mark for deletion)
  7919. * and make it pending
  7920. */
  7921. pf->udp_ports[idx].port = 0;
  7922. pf->pending_udp_bitmap |= BIT_ULL(idx);
  7923. pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
  7924. return;
  7925. not_found:
  7926. netdev_warn(netdev, "UDP port %d was not found, not deleting\n",
  7927. port);
  7928. }
  7929. static int i40e_get_phys_port_id(struct net_device *netdev,
  7930. struct netdev_phys_item_id *ppid)
  7931. {
  7932. struct i40e_netdev_priv *np = netdev_priv(netdev);
  7933. struct i40e_pf *pf = np->vsi->back;
  7934. struct i40e_hw *hw = &pf->hw;
  7935. if (!(pf->flags & I40E_FLAG_PORT_ID_VALID))
  7936. return -EOPNOTSUPP;
  7937. ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
  7938. memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
  7939. return 0;
  7940. }
  7941. /**
  7942. * i40e_ndo_fdb_add - add an entry to the hardware database
  7943. * @ndm: the input from the stack
  7944. * @tb: pointer to array of nladdr (unused)
  7945. * @dev: the net device pointer
  7946. * @addr: the MAC address entry being added
  7947. * @flags: instructions from stack about fdb operation
  7948. */
  7949. static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
  7950. struct net_device *dev,
  7951. const unsigned char *addr, u16 vid,
  7952. u16 flags)
  7953. {
  7954. struct i40e_netdev_priv *np = netdev_priv(dev);
  7955. struct i40e_pf *pf = np->vsi->back;
  7956. int err = 0;
  7957. if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
  7958. return -EOPNOTSUPP;
  7959. if (vid) {
  7960. pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
  7961. return -EINVAL;
  7962. }
  7963. /* Hardware does not support aging addresses so if a
  7964. * ndm_state is given only allow permanent addresses
  7965. */
  7966. if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
  7967. netdev_info(dev, "FDB only supports static addresses\n");
  7968. return -EINVAL;
  7969. }
  7970. if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
  7971. err = dev_uc_add_excl(dev, addr);
  7972. else if (is_multicast_ether_addr(addr))
  7973. err = dev_mc_add_excl(dev, addr);
  7974. else
  7975. err = -EINVAL;
  7976. /* Only return duplicate errors if NLM_F_EXCL is set */
  7977. if (err == -EEXIST && !(flags & NLM_F_EXCL))
  7978. err = 0;
  7979. return err;
  7980. }
  7981. /**
  7982. * i40e_ndo_bridge_setlink - Set the hardware bridge mode
  7983. * @dev: the netdev being configured
  7984. * @nlh: RTNL message
  7985. *
  7986. * Inserts a new hardware bridge if not already created and
  7987. * enables the bridging mode requested (VEB or VEPA). If the
  7988. * hardware bridge has already been inserted and the request
  7989. * is to change the mode then that requires a PF reset to
  7990. * allow rebuild of the components with required hardware
  7991. * bridge mode enabled.
  7992. *
  7993. * Note: expects to be called while under rtnl_lock()
  7994. **/
  7995. static int i40e_ndo_bridge_setlink(struct net_device *dev,
  7996. struct nlmsghdr *nlh,
  7997. u16 flags)
  7998. {
  7999. struct i40e_netdev_priv *np = netdev_priv(dev);
  8000. struct i40e_vsi *vsi = np->vsi;
  8001. struct i40e_pf *pf = vsi->back;
  8002. struct i40e_veb *veb = NULL;
  8003. struct nlattr *attr, *br_spec;
  8004. int i, rem;
  8005. /* Only for PF VSI for now */
  8006. if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
  8007. return -EOPNOTSUPP;
  8008. /* Find the HW bridge for PF VSI */
  8009. for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
  8010. if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
  8011. veb = pf->veb[i];
  8012. }
  8013. br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
  8014. nla_for_each_nested(attr, br_spec, rem) {
  8015. __u16 mode;
  8016. if (nla_type(attr) != IFLA_BRIDGE_MODE)
  8017. continue;
  8018. mode = nla_get_u16(attr);
  8019. if ((mode != BRIDGE_MODE_VEPA) &&
  8020. (mode != BRIDGE_MODE_VEB))
  8021. return -EINVAL;
  8022. /* Insert a new HW bridge */
  8023. if (!veb) {
  8024. veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
  8025. vsi->tc_config.enabled_tc);
  8026. if (veb) {
  8027. veb->bridge_mode = mode;
  8028. i40e_config_bridge_mode(veb);
  8029. } else {
  8030. /* No Bridge HW offload available */
  8031. return -ENOENT;
  8032. }
  8033. break;
  8034. } else if (mode != veb->bridge_mode) {
  8035. /* Existing HW bridge but different mode needs reset */
  8036. veb->bridge_mode = mode;
  8037. /* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
  8038. if (mode == BRIDGE_MODE_VEB)
  8039. pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
  8040. else
  8041. pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
  8042. i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED),
  8043. true);
  8044. break;
  8045. }
  8046. }
  8047. return 0;
  8048. }
  8049. /**
  8050. * i40e_ndo_bridge_getlink - Get the hardware bridge mode
  8051. * @skb: skb buff
  8052. * @pid: process id
  8053. * @seq: RTNL message seq #
  8054. * @dev: the netdev being configured
  8055. * @filter_mask: unused
  8056. * @nlflags: netlink flags passed in
  8057. *
  8058. * Return the mode in which the hardware bridge is operating in
  8059. * i.e VEB or VEPA.
  8060. **/
  8061. static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
  8062. struct net_device *dev,
  8063. u32 __always_unused filter_mask,
  8064. int nlflags)
  8065. {
  8066. struct i40e_netdev_priv *np = netdev_priv(dev);
  8067. struct i40e_vsi *vsi = np->vsi;
  8068. struct i40e_pf *pf = vsi->back;
  8069. struct i40e_veb *veb = NULL;
  8070. int i;
  8071. /* Only for PF VSI for now */
  8072. if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
  8073. return -EOPNOTSUPP;
  8074. /* Find the HW bridge for the PF VSI */
  8075. for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
  8076. if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
  8077. veb = pf->veb[i];
  8078. }
  8079. if (!veb)
  8080. return 0;
  8081. return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
  8082. 0, 0, nlflags, filter_mask, NULL);
  8083. }
  8084. /**
  8085. * i40e_features_check - Validate encapsulated packet conforms to limits
  8086. * @skb: skb buff
  8087. * @dev: This physical port's netdev
  8088. * @features: Offload features that the stack believes apply
  8089. **/
  8090. static netdev_features_t i40e_features_check(struct sk_buff *skb,
  8091. struct net_device *dev,
  8092. netdev_features_t features)
  8093. {
  8094. size_t len;
  8095. /* No point in doing any of this if neither checksum nor GSO are
  8096. * being requested for this frame. We can rule out both by just
  8097. * checking for CHECKSUM_PARTIAL
  8098. */
  8099. if (skb->ip_summed != CHECKSUM_PARTIAL)
  8100. return features;
  8101. /* We cannot support GSO if the MSS is going to be less than
  8102. * 64 bytes. If it is then we need to drop support for GSO.
  8103. */
  8104. if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
  8105. features &= ~NETIF_F_GSO_MASK;
  8106. /* MACLEN can support at most 63 words */
  8107. len = skb_network_header(skb) - skb->data;
  8108. if (len & ~(63 * 2))
  8109. goto out_err;
  8110. /* IPLEN and EIPLEN can support at most 127 dwords */
  8111. len = skb_transport_header(skb) - skb_network_header(skb);
  8112. if (len & ~(127 * 4))
  8113. goto out_err;
  8114. if (skb->encapsulation) {
  8115. /* L4TUNLEN can support 127 words */
  8116. len = skb_inner_network_header(skb) - skb_transport_header(skb);
  8117. if (len & ~(127 * 2))
  8118. goto out_err;
  8119. /* IPLEN can support at most 127 dwords */
  8120. len = skb_inner_transport_header(skb) -
  8121. skb_inner_network_header(skb);
  8122. if (len & ~(127 * 4))
  8123. goto out_err;
  8124. }
  8125. /* No need to validate L4LEN as TCP is the only protocol with a
  8126. * a flexible value and we support all possible values supported
  8127. * by TCP, which is at most 15 dwords
  8128. */
  8129. return features;
  8130. out_err:
  8131. return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
  8132. }
  8133. static const struct net_device_ops i40e_netdev_ops = {
  8134. .ndo_open = i40e_open,
  8135. .ndo_stop = i40e_close,
  8136. .ndo_start_xmit = i40e_lan_xmit_frame,
  8137. .ndo_get_stats64 = i40e_get_netdev_stats_struct,
  8138. .ndo_set_rx_mode = i40e_set_rx_mode,
  8139. .ndo_validate_addr = eth_validate_addr,
  8140. .ndo_set_mac_address = i40e_set_mac,
  8141. .ndo_change_mtu = i40e_change_mtu,
  8142. .ndo_do_ioctl = i40e_ioctl,
  8143. .ndo_tx_timeout = i40e_tx_timeout,
  8144. .ndo_vlan_rx_add_vid = i40e_vlan_rx_add_vid,
  8145. .ndo_vlan_rx_kill_vid = i40e_vlan_rx_kill_vid,
  8146. #ifdef CONFIG_NET_POLL_CONTROLLER
  8147. .ndo_poll_controller = i40e_netpoll,
  8148. #endif
  8149. .ndo_setup_tc = __i40e_setup_tc,
  8150. .ndo_set_features = i40e_set_features,
  8151. .ndo_set_vf_mac = i40e_ndo_set_vf_mac,
  8152. .ndo_set_vf_vlan = i40e_ndo_set_vf_port_vlan,
  8153. .ndo_set_vf_rate = i40e_ndo_set_vf_bw,
  8154. .ndo_get_vf_config = i40e_ndo_get_vf_config,
  8155. .ndo_set_vf_link_state = i40e_ndo_set_vf_link_state,
  8156. .ndo_set_vf_spoofchk = i40e_ndo_set_vf_spoofchk,
  8157. .ndo_set_vf_trust = i40e_ndo_set_vf_trust,
  8158. .ndo_udp_tunnel_add = i40e_udp_tunnel_add,
  8159. .ndo_udp_tunnel_del = i40e_udp_tunnel_del,
  8160. .ndo_get_phys_port_id = i40e_get_phys_port_id,
  8161. .ndo_fdb_add = i40e_ndo_fdb_add,
  8162. .ndo_features_check = i40e_features_check,
  8163. .ndo_bridge_getlink = i40e_ndo_bridge_getlink,
  8164. .ndo_bridge_setlink = i40e_ndo_bridge_setlink,
  8165. };
  8166. /**
  8167. * i40e_config_netdev - Setup the netdev flags
  8168. * @vsi: the VSI being configured
  8169. *
  8170. * Returns 0 on success, negative value on failure
  8171. **/
  8172. static int i40e_config_netdev(struct i40e_vsi *vsi)
  8173. {
  8174. struct i40e_pf *pf = vsi->back;
  8175. struct i40e_hw *hw = &pf->hw;
  8176. struct i40e_netdev_priv *np;
  8177. struct net_device *netdev;
  8178. u8 broadcast[ETH_ALEN];
  8179. u8 mac_addr[ETH_ALEN];
  8180. int etherdev_size;
  8181. netdev_features_t hw_enc_features;
  8182. netdev_features_t hw_features;
  8183. etherdev_size = sizeof(struct i40e_netdev_priv);
  8184. netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
  8185. if (!netdev)
  8186. return -ENOMEM;
  8187. vsi->netdev = netdev;
  8188. np = netdev_priv(netdev);
  8189. np->vsi = vsi;
  8190. hw_enc_features = NETIF_F_SG |
  8191. NETIF_F_IP_CSUM |
  8192. NETIF_F_IPV6_CSUM |
  8193. NETIF_F_HIGHDMA |
  8194. NETIF_F_SOFT_FEATURES |
  8195. NETIF_F_TSO |
  8196. NETIF_F_TSO_ECN |
  8197. NETIF_F_TSO6 |
  8198. NETIF_F_GSO_GRE |
  8199. NETIF_F_GSO_GRE_CSUM |
  8200. NETIF_F_GSO_PARTIAL |
  8201. NETIF_F_GSO_UDP_TUNNEL |
  8202. NETIF_F_GSO_UDP_TUNNEL_CSUM |
  8203. NETIF_F_SCTP_CRC |
  8204. NETIF_F_RXHASH |
  8205. NETIF_F_RXCSUM |
  8206. 0;
  8207. if (!(pf->flags & I40E_FLAG_OUTER_UDP_CSUM_CAPABLE))
  8208. netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
  8209. netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
  8210. netdev->hw_enc_features |= hw_enc_features;
  8211. /* record features VLANs can make use of */
  8212. netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
  8213. if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
  8214. netdev->hw_features |= NETIF_F_NTUPLE;
  8215. hw_features = hw_enc_features |
  8216. NETIF_F_HW_VLAN_CTAG_TX |
  8217. NETIF_F_HW_VLAN_CTAG_RX;
  8218. netdev->hw_features |= hw_features;
  8219. netdev->features |= hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
  8220. netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
  8221. if (vsi->type == I40E_VSI_MAIN) {
  8222. SET_NETDEV_DEV(netdev, &pf->pdev->dev);
  8223. ether_addr_copy(mac_addr, hw->mac.perm_addr);
  8224. /* The following steps are necessary for two reasons. First,
  8225. * some older NVM configurations load a default MAC-VLAN
  8226. * filter that will accept any tagged packet, and we want to
  8227. * replace this with a normal filter. Additionally, it is
  8228. * possible our MAC address was provided by the platform using
  8229. * Open Firmware or similar.
  8230. *
  8231. * Thus, we need to remove the default filter and install one
  8232. * specific to the MAC address.
  8233. */
  8234. i40e_rm_default_mac_filter(vsi, mac_addr);
  8235. spin_lock_bh(&vsi->mac_filter_hash_lock);
  8236. i40e_add_mac_filter(vsi, mac_addr);
  8237. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  8238. } else {
  8239. /* relate the VSI_VMDQ name to the VSI_MAIN name */
  8240. snprintf(netdev->name, IFNAMSIZ, "%sv%%d",
  8241. pf->vsi[pf->lan_vsi]->netdev->name);
  8242. random_ether_addr(mac_addr);
  8243. spin_lock_bh(&vsi->mac_filter_hash_lock);
  8244. i40e_add_mac_filter(vsi, mac_addr);
  8245. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  8246. }
  8247. /* Add the broadcast filter so that we initially will receive
  8248. * broadcast packets. Note that when a new VLAN is first added the
  8249. * driver will convert all filters marked I40E_VLAN_ANY into VLAN
  8250. * specific filters as part of transitioning into "vlan" operation.
  8251. * When more VLANs are added, the driver will copy each existing MAC
  8252. * filter and add it for the new VLAN.
  8253. *
  8254. * Broadcast filters are handled specially by
  8255. * i40e_sync_filters_subtask, as the driver must to set the broadcast
  8256. * promiscuous bit instead of adding this directly as a MAC/VLAN
  8257. * filter. The subtask will update the correct broadcast promiscuous
  8258. * bits as VLANs become active or inactive.
  8259. */
  8260. eth_broadcast_addr(broadcast);
  8261. spin_lock_bh(&vsi->mac_filter_hash_lock);
  8262. i40e_add_mac_filter(vsi, broadcast);
  8263. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  8264. ether_addr_copy(netdev->dev_addr, mac_addr);
  8265. ether_addr_copy(netdev->perm_addr, mac_addr);
  8266. netdev->priv_flags |= IFF_UNICAST_FLT;
  8267. netdev->priv_flags |= IFF_SUPP_NOFCS;
  8268. /* Setup netdev TC information */
  8269. i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
  8270. netdev->netdev_ops = &i40e_netdev_ops;
  8271. netdev->watchdog_timeo = 5 * HZ;
  8272. i40e_set_ethtool_ops(netdev);
  8273. /* MTU range: 68 - 9706 */
  8274. netdev->min_mtu = ETH_MIN_MTU;
  8275. netdev->max_mtu = I40E_MAX_RXBUFFER -
  8276. (ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
  8277. return 0;
  8278. }
  8279. /**
  8280. * i40e_vsi_delete - Delete a VSI from the switch
  8281. * @vsi: the VSI being removed
  8282. *
  8283. * Returns 0 on success, negative value on failure
  8284. **/
  8285. static void i40e_vsi_delete(struct i40e_vsi *vsi)
  8286. {
  8287. /* remove default VSI is not allowed */
  8288. if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
  8289. return;
  8290. i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
  8291. }
  8292. /**
  8293. * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
  8294. * @vsi: the VSI being queried
  8295. *
  8296. * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
  8297. **/
  8298. int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
  8299. {
  8300. struct i40e_veb *veb;
  8301. struct i40e_pf *pf = vsi->back;
  8302. /* Uplink is not a bridge so default to VEB */
  8303. if (vsi->veb_idx == I40E_NO_VEB)
  8304. return 1;
  8305. veb = pf->veb[vsi->veb_idx];
  8306. if (!veb) {
  8307. dev_info(&pf->pdev->dev,
  8308. "There is no veb associated with the bridge\n");
  8309. return -ENOENT;
  8310. }
  8311. /* Uplink is a bridge in VEPA mode */
  8312. if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
  8313. return 0;
  8314. } else {
  8315. /* Uplink is a bridge in VEB mode */
  8316. return 1;
  8317. }
  8318. /* VEPA is now default bridge, so return 0 */
  8319. return 0;
  8320. }
  8321. /**
  8322. * i40e_add_vsi - Add a VSI to the switch
  8323. * @vsi: the VSI being configured
  8324. *
  8325. * This initializes a VSI context depending on the VSI type to be added and
  8326. * passes it down to the add_vsi aq command.
  8327. **/
  8328. static int i40e_add_vsi(struct i40e_vsi *vsi)
  8329. {
  8330. int ret = -ENODEV;
  8331. struct i40e_pf *pf = vsi->back;
  8332. struct i40e_hw *hw = &pf->hw;
  8333. struct i40e_vsi_context ctxt;
  8334. struct i40e_mac_filter *f;
  8335. struct hlist_node *h;
  8336. int bkt;
  8337. u8 enabled_tc = 0x1; /* TC0 enabled */
  8338. int f_count = 0;
  8339. memset(&ctxt, 0, sizeof(ctxt));
  8340. switch (vsi->type) {
  8341. case I40E_VSI_MAIN:
  8342. /* The PF's main VSI is already setup as part of the
  8343. * device initialization, so we'll not bother with
  8344. * the add_vsi call, but we will retrieve the current
  8345. * VSI context.
  8346. */
  8347. ctxt.seid = pf->main_vsi_seid;
  8348. ctxt.pf_num = pf->hw.pf_id;
  8349. ctxt.vf_num = 0;
  8350. ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
  8351. ctxt.flags = I40E_AQ_VSI_TYPE_PF;
  8352. if (ret) {
  8353. dev_info(&pf->pdev->dev,
  8354. "couldn't get PF vsi config, err %s aq_err %s\n",
  8355. i40e_stat_str(&pf->hw, ret),
  8356. i40e_aq_str(&pf->hw,
  8357. pf->hw.aq.asq_last_status));
  8358. return -ENOENT;
  8359. }
  8360. vsi->info = ctxt.info;
  8361. vsi->info.valid_sections = 0;
  8362. vsi->seid = ctxt.seid;
  8363. vsi->id = ctxt.vsi_number;
  8364. enabled_tc = i40e_pf_get_tc_map(pf);
  8365. /* MFP mode setup queue map and update VSI */
  8366. if ((pf->flags & I40E_FLAG_MFP_ENABLED) &&
  8367. !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
  8368. memset(&ctxt, 0, sizeof(ctxt));
  8369. ctxt.seid = pf->main_vsi_seid;
  8370. ctxt.pf_num = pf->hw.pf_id;
  8371. ctxt.vf_num = 0;
  8372. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
  8373. ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
  8374. if (ret) {
  8375. dev_info(&pf->pdev->dev,
  8376. "update vsi failed, err %s aq_err %s\n",
  8377. i40e_stat_str(&pf->hw, ret),
  8378. i40e_aq_str(&pf->hw,
  8379. pf->hw.aq.asq_last_status));
  8380. ret = -ENOENT;
  8381. goto err;
  8382. }
  8383. /* update the local VSI info queue map */
  8384. i40e_vsi_update_queue_map(vsi, &ctxt);
  8385. vsi->info.valid_sections = 0;
  8386. } else {
  8387. /* Default/Main VSI is only enabled for TC0
  8388. * reconfigure it to enable all TCs that are
  8389. * available on the port in SFP mode.
  8390. * For MFP case the iSCSI PF would use this
  8391. * flow to enable LAN+iSCSI TC.
  8392. */
  8393. ret = i40e_vsi_config_tc(vsi, enabled_tc);
  8394. if (ret) {
  8395. dev_info(&pf->pdev->dev,
  8396. "failed to configure TCs for main VSI tc_map 0x%08x, err %s aq_err %s\n",
  8397. enabled_tc,
  8398. i40e_stat_str(&pf->hw, ret),
  8399. i40e_aq_str(&pf->hw,
  8400. pf->hw.aq.asq_last_status));
  8401. ret = -ENOENT;
  8402. }
  8403. }
  8404. break;
  8405. case I40E_VSI_FDIR:
  8406. ctxt.pf_num = hw->pf_id;
  8407. ctxt.vf_num = 0;
  8408. ctxt.uplink_seid = vsi->uplink_seid;
  8409. ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
  8410. ctxt.flags = I40E_AQ_VSI_TYPE_PF;
  8411. if ((pf->flags & I40E_FLAG_VEB_MODE_ENABLED) &&
  8412. (i40e_is_vsi_uplink_mode_veb(vsi))) {
  8413. ctxt.info.valid_sections |=
  8414. cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  8415. ctxt.info.switch_id =
  8416. cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  8417. }
  8418. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
  8419. break;
  8420. case I40E_VSI_VMDQ2:
  8421. ctxt.pf_num = hw->pf_id;
  8422. ctxt.vf_num = 0;
  8423. ctxt.uplink_seid = vsi->uplink_seid;
  8424. ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
  8425. ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
  8426. /* This VSI is connected to VEB so the switch_id
  8427. * should be set to zero by default.
  8428. */
  8429. if (i40e_is_vsi_uplink_mode_veb(vsi)) {
  8430. ctxt.info.valid_sections |=
  8431. cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  8432. ctxt.info.switch_id =
  8433. cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  8434. }
  8435. /* Setup the VSI tx/rx queue map for TC0 only for now */
  8436. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
  8437. break;
  8438. case I40E_VSI_SRIOV:
  8439. ctxt.pf_num = hw->pf_id;
  8440. ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
  8441. ctxt.uplink_seid = vsi->uplink_seid;
  8442. ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
  8443. ctxt.flags = I40E_AQ_VSI_TYPE_VF;
  8444. /* This VSI is connected to VEB so the switch_id
  8445. * should be set to zero by default.
  8446. */
  8447. if (i40e_is_vsi_uplink_mode_veb(vsi)) {
  8448. ctxt.info.valid_sections |=
  8449. cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  8450. ctxt.info.switch_id =
  8451. cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  8452. }
  8453. if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
  8454. ctxt.info.valid_sections |=
  8455. cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
  8456. ctxt.info.queueing_opt_flags |=
  8457. (I40E_AQ_VSI_QUE_OPT_TCP_ENA |
  8458. I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI);
  8459. }
  8460. ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
  8461. ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
  8462. if (pf->vf[vsi->vf_id].spoofchk) {
  8463. ctxt.info.valid_sections |=
  8464. cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
  8465. ctxt.info.sec_flags |=
  8466. (I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
  8467. I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
  8468. }
  8469. /* Setup the VSI tx/rx queue map for TC0 only for now */
  8470. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
  8471. break;
  8472. case I40E_VSI_IWARP:
  8473. /* send down message to iWARP */
  8474. break;
  8475. default:
  8476. return -ENODEV;
  8477. }
  8478. if (vsi->type != I40E_VSI_MAIN) {
  8479. ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
  8480. if (ret) {
  8481. dev_info(&vsi->back->pdev->dev,
  8482. "add vsi failed, err %s aq_err %s\n",
  8483. i40e_stat_str(&pf->hw, ret),
  8484. i40e_aq_str(&pf->hw,
  8485. pf->hw.aq.asq_last_status));
  8486. ret = -ENOENT;
  8487. goto err;
  8488. }
  8489. vsi->info = ctxt.info;
  8490. vsi->info.valid_sections = 0;
  8491. vsi->seid = ctxt.seid;
  8492. vsi->id = ctxt.vsi_number;
  8493. }
  8494. vsi->active_filters = 0;
  8495. clear_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state);
  8496. spin_lock_bh(&vsi->mac_filter_hash_lock);
  8497. /* If macvlan filters already exist, force them to get loaded */
  8498. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  8499. f->state = I40E_FILTER_NEW;
  8500. f_count++;
  8501. }
  8502. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  8503. if (f_count) {
  8504. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  8505. pf->flags |= I40E_FLAG_FILTER_SYNC;
  8506. }
  8507. /* Update VSI BW information */
  8508. ret = i40e_vsi_get_bw_info(vsi);
  8509. if (ret) {
  8510. dev_info(&pf->pdev->dev,
  8511. "couldn't get vsi bw info, err %s aq_err %s\n",
  8512. i40e_stat_str(&pf->hw, ret),
  8513. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  8514. /* VSI is already added so not tearing that up */
  8515. ret = 0;
  8516. }
  8517. err:
  8518. return ret;
  8519. }
  8520. /**
  8521. * i40e_vsi_release - Delete a VSI and free its resources
  8522. * @vsi: the VSI being removed
  8523. *
  8524. * Returns 0 on success or < 0 on error
  8525. **/
  8526. int i40e_vsi_release(struct i40e_vsi *vsi)
  8527. {
  8528. struct i40e_mac_filter *f;
  8529. struct hlist_node *h;
  8530. struct i40e_veb *veb = NULL;
  8531. struct i40e_pf *pf;
  8532. u16 uplink_seid;
  8533. int i, n, bkt;
  8534. pf = vsi->back;
  8535. /* release of a VEB-owner or last VSI is not allowed */
  8536. if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
  8537. dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
  8538. vsi->seid, vsi->uplink_seid);
  8539. return -ENODEV;
  8540. }
  8541. if (vsi == pf->vsi[pf->lan_vsi] &&
  8542. !test_bit(__I40E_DOWN, &pf->state)) {
  8543. dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
  8544. return -ENODEV;
  8545. }
  8546. uplink_seid = vsi->uplink_seid;
  8547. if (vsi->type != I40E_VSI_SRIOV) {
  8548. if (vsi->netdev_registered) {
  8549. vsi->netdev_registered = false;
  8550. if (vsi->netdev) {
  8551. /* results in a call to i40e_close() */
  8552. unregister_netdev(vsi->netdev);
  8553. }
  8554. } else {
  8555. i40e_vsi_close(vsi);
  8556. }
  8557. i40e_vsi_disable_irq(vsi);
  8558. }
  8559. spin_lock_bh(&vsi->mac_filter_hash_lock);
  8560. /* clear the sync flag on all filters */
  8561. if (vsi->netdev) {
  8562. __dev_uc_unsync(vsi->netdev, NULL);
  8563. __dev_mc_unsync(vsi->netdev, NULL);
  8564. }
  8565. /* make sure any remaining filters are marked for deletion */
  8566. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
  8567. __i40e_del_filter(vsi, f);
  8568. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  8569. i40e_sync_vsi_filters(vsi);
  8570. i40e_vsi_delete(vsi);
  8571. i40e_vsi_free_q_vectors(vsi);
  8572. if (vsi->netdev) {
  8573. free_netdev(vsi->netdev);
  8574. vsi->netdev = NULL;
  8575. }
  8576. i40e_vsi_clear_rings(vsi);
  8577. i40e_vsi_clear(vsi);
  8578. /* If this was the last thing on the VEB, except for the
  8579. * controlling VSI, remove the VEB, which puts the controlling
  8580. * VSI onto the next level down in the switch.
  8581. *
  8582. * Well, okay, there's one more exception here: don't remove
  8583. * the orphan VEBs yet. We'll wait for an explicit remove request
  8584. * from up the network stack.
  8585. */
  8586. for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
  8587. if (pf->vsi[i] &&
  8588. pf->vsi[i]->uplink_seid == uplink_seid &&
  8589. (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
  8590. n++; /* count the VSIs */
  8591. }
  8592. }
  8593. for (i = 0; i < I40E_MAX_VEB; i++) {
  8594. if (!pf->veb[i])
  8595. continue;
  8596. if (pf->veb[i]->uplink_seid == uplink_seid)
  8597. n++; /* count the VEBs */
  8598. if (pf->veb[i]->seid == uplink_seid)
  8599. veb = pf->veb[i];
  8600. }
  8601. if (n == 0 && veb && veb->uplink_seid != 0)
  8602. i40e_veb_release(veb);
  8603. return 0;
  8604. }
  8605. /**
  8606. * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
  8607. * @vsi: ptr to the VSI
  8608. *
  8609. * This should only be called after i40e_vsi_mem_alloc() which allocates the
  8610. * corresponding SW VSI structure and initializes num_queue_pairs for the
  8611. * newly allocated VSI.
  8612. *
  8613. * Returns 0 on success or negative on failure
  8614. **/
  8615. static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
  8616. {
  8617. int ret = -ENOENT;
  8618. struct i40e_pf *pf = vsi->back;
  8619. if (vsi->q_vectors[0]) {
  8620. dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
  8621. vsi->seid);
  8622. return -EEXIST;
  8623. }
  8624. if (vsi->base_vector) {
  8625. dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
  8626. vsi->seid, vsi->base_vector);
  8627. return -EEXIST;
  8628. }
  8629. ret = i40e_vsi_alloc_q_vectors(vsi);
  8630. if (ret) {
  8631. dev_info(&pf->pdev->dev,
  8632. "failed to allocate %d q_vector for VSI %d, ret=%d\n",
  8633. vsi->num_q_vectors, vsi->seid, ret);
  8634. vsi->num_q_vectors = 0;
  8635. goto vector_setup_out;
  8636. }
  8637. /* In Legacy mode, we do not have to get any other vector since we
  8638. * piggyback on the misc/ICR0 for queue interrupts.
  8639. */
  8640. if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
  8641. return ret;
  8642. if (vsi->num_q_vectors)
  8643. vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
  8644. vsi->num_q_vectors, vsi->idx);
  8645. if (vsi->base_vector < 0) {
  8646. dev_info(&pf->pdev->dev,
  8647. "failed to get tracking for %d vectors for VSI %d, err=%d\n",
  8648. vsi->num_q_vectors, vsi->seid, vsi->base_vector);
  8649. i40e_vsi_free_q_vectors(vsi);
  8650. ret = -ENOENT;
  8651. goto vector_setup_out;
  8652. }
  8653. vector_setup_out:
  8654. return ret;
  8655. }
  8656. /**
  8657. * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
  8658. * @vsi: pointer to the vsi.
  8659. *
  8660. * This re-allocates a vsi's queue resources.
  8661. *
  8662. * Returns pointer to the successfully allocated and configured VSI sw struct
  8663. * on success, otherwise returns NULL on failure.
  8664. **/
  8665. static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
  8666. {
  8667. struct i40e_pf *pf;
  8668. u8 enabled_tc;
  8669. int ret;
  8670. if (!vsi)
  8671. return NULL;
  8672. pf = vsi->back;
  8673. i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
  8674. i40e_vsi_clear_rings(vsi);
  8675. i40e_vsi_free_arrays(vsi, false);
  8676. i40e_set_num_rings_in_vsi(vsi);
  8677. ret = i40e_vsi_alloc_arrays(vsi, false);
  8678. if (ret)
  8679. goto err_vsi;
  8680. ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs, vsi->idx);
  8681. if (ret < 0) {
  8682. dev_info(&pf->pdev->dev,
  8683. "failed to get tracking for %d queues for VSI %d err %d\n",
  8684. vsi->alloc_queue_pairs, vsi->seid, ret);
  8685. goto err_vsi;
  8686. }
  8687. vsi->base_queue = ret;
  8688. /* Update the FW view of the VSI. Force a reset of TC and queue
  8689. * layout configurations.
  8690. */
  8691. enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
  8692. pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
  8693. pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
  8694. i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
  8695. if (vsi->type == I40E_VSI_MAIN)
  8696. i40e_rm_default_mac_filter(vsi, pf->hw.mac.perm_addr);
  8697. /* assign it some queues */
  8698. ret = i40e_alloc_rings(vsi);
  8699. if (ret)
  8700. goto err_rings;
  8701. /* map all of the rings to the q_vectors */
  8702. i40e_vsi_map_rings_to_vectors(vsi);
  8703. return vsi;
  8704. err_rings:
  8705. i40e_vsi_free_q_vectors(vsi);
  8706. if (vsi->netdev_registered) {
  8707. vsi->netdev_registered = false;
  8708. unregister_netdev(vsi->netdev);
  8709. free_netdev(vsi->netdev);
  8710. vsi->netdev = NULL;
  8711. }
  8712. i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
  8713. err_vsi:
  8714. i40e_vsi_clear(vsi);
  8715. return NULL;
  8716. }
  8717. /**
  8718. * i40e_vsi_setup - Set up a VSI by a given type
  8719. * @pf: board private structure
  8720. * @type: VSI type
  8721. * @uplink_seid: the switch element to link to
  8722. * @param1: usage depends upon VSI type. For VF types, indicates VF id
  8723. *
  8724. * This allocates the sw VSI structure and its queue resources, then add a VSI
  8725. * to the identified VEB.
  8726. *
  8727. * Returns pointer to the successfully allocated and configure VSI sw struct on
  8728. * success, otherwise returns NULL on failure.
  8729. **/
  8730. struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
  8731. u16 uplink_seid, u32 param1)
  8732. {
  8733. struct i40e_vsi *vsi = NULL;
  8734. struct i40e_veb *veb = NULL;
  8735. int ret, i;
  8736. int v_idx;
  8737. /* The requested uplink_seid must be either
  8738. * - the PF's port seid
  8739. * no VEB is needed because this is the PF
  8740. * or this is a Flow Director special case VSI
  8741. * - seid of an existing VEB
  8742. * - seid of a VSI that owns an existing VEB
  8743. * - seid of a VSI that doesn't own a VEB
  8744. * a new VEB is created and the VSI becomes the owner
  8745. * - seid of the PF VSI, which is what creates the first VEB
  8746. * this is a special case of the previous
  8747. *
  8748. * Find which uplink_seid we were given and create a new VEB if needed
  8749. */
  8750. for (i = 0; i < I40E_MAX_VEB; i++) {
  8751. if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
  8752. veb = pf->veb[i];
  8753. break;
  8754. }
  8755. }
  8756. if (!veb && uplink_seid != pf->mac_seid) {
  8757. for (i = 0; i < pf->num_alloc_vsi; i++) {
  8758. if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
  8759. vsi = pf->vsi[i];
  8760. break;
  8761. }
  8762. }
  8763. if (!vsi) {
  8764. dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
  8765. uplink_seid);
  8766. return NULL;
  8767. }
  8768. if (vsi->uplink_seid == pf->mac_seid)
  8769. veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
  8770. vsi->tc_config.enabled_tc);
  8771. else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
  8772. veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
  8773. vsi->tc_config.enabled_tc);
  8774. if (veb) {
  8775. if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
  8776. dev_info(&vsi->back->pdev->dev,
  8777. "New VSI creation error, uplink seid of LAN VSI expected.\n");
  8778. return NULL;
  8779. }
  8780. /* We come up by default in VEPA mode if SRIOV is not
  8781. * already enabled, in which case we can't force VEPA
  8782. * mode.
  8783. */
  8784. if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
  8785. veb->bridge_mode = BRIDGE_MODE_VEPA;
  8786. pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
  8787. }
  8788. i40e_config_bridge_mode(veb);
  8789. }
  8790. for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
  8791. if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
  8792. veb = pf->veb[i];
  8793. }
  8794. if (!veb) {
  8795. dev_info(&pf->pdev->dev, "couldn't add VEB\n");
  8796. return NULL;
  8797. }
  8798. vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
  8799. uplink_seid = veb->seid;
  8800. }
  8801. /* get vsi sw struct */
  8802. v_idx = i40e_vsi_mem_alloc(pf, type);
  8803. if (v_idx < 0)
  8804. goto err_alloc;
  8805. vsi = pf->vsi[v_idx];
  8806. if (!vsi)
  8807. goto err_alloc;
  8808. vsi->type = type;
  8809. vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
  8810. if (type == I40E_VSI_MAIN)
  8811. pf->lan_vsi = v_idx;
  8812. else if (type == I40E_VSI_SRIOV)
  8813. vsi->vf_id = param1;
  8814. /* assign it some queues */
  8815. ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs,
  8816. vsi->idx);
  8817. if (ret < 0) {
  8818. dev_info(&pf->pdev->dev,
  8819. "failed to get tracking for %d queues for VSI %d err=%d\n",
  8820. vsi->alloc_queue_pairs, vsi->seid, ret);
  8821. goto err_vsi;
  8822. }
  8823. vsi->base_queue = ret;
  8824. /* get a VSI from the hardware */
  8825. vsi->uplink_seid = uplink_seid;
  8826. ret = i40e_add_vsi(vsi);
  8827. if (ret)
  8828. goto err_vsi;
  8829. switch (vsi->type) {
  8830. /* setup the netdev if needed */
  8831. case I40E_VSI_MAIN:
  8832. /* Apply relevant filters if a platform-specific mac
  8833. * address was selected.
  8834. */
  8835. if (!!(pf->flags & I40E_FLAG_PF_MAC)) {
  8836. ret = i40e_macaddr_init(vsi, pf->hw.mac.addr);
  8837. if (ret) {
  8838. dev_warn(&pf->pdev->dev,
  8839. "could not set up macaddr; err %d\n",
  8840. ret);
  8841. }
  8842. }
  8843. case I40E_VSI_VMDQ2:
  8844. ret = i40e_config_netdev(vsi);
  8845. if (ret)
  8846. goto err_netdev;
  8847. ret = register_netdev(vsi->netdev);
  8848. if (ret)
  8849. goto err_netdev;
  8850. vsi->netdev_registered = true;
  8851. netif_carrier_off(vsi->netdev);
  8852. #ifdef CONFIG_I40E_DCB
  8853. /* Setup DCB netlink interface */
  8854. i40e_dcbnl_setup(vsi);
  8855. #endif /* CONFIG_I40E_DCB */
  8856. /* fall through */
  8857. case I40E_VSI_FDIR:
  8858. /* set up vectors and rings if needed */
  8859. ret = i40e_vsi_setup_vectors(vsi);
  8860. if (ret)
  8861. goto err_msix;
  8862. ret = i40e_alloc_rings(vsi);
  8863. if (ret)
  8864. goto err_rings;
  8865. /* map all of the rings to the q_vectors */
  8866. i40e_vsi_map_rings_to_vectors(vsi);
  8867. i40e_vsi_reset_stats(vsi);
  8868. break;
  8869. default:
  8870. /* no netdev or rings for the other VSI types */
  8871. break;
  8872. }
  8873. if ((pf->flags & I40E_FLAG_RSS_AQ_CAPABLE) &&
  8874. (vsi->type == I40E_VSI_VMDQ2)) {
  8875. ret = i40e_vsi_config_rss(vsi);
  8876. }
  8877. return vsi;
  8878. err_rings:
  8879. i40e_vsi_free_q_vectors(vsi);
  8880. err_msix:
  8881. if (vsi->netdev_registered) {
  8882. vsi->netdev_registered = false;
  8883. unregister_netdev(vsi->netdev);
  8884. free_netdev(vsi->netdev);
  8885. vsi->netdev = NULL;
  8886. }
  8887. err_netdev:
  8888. i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
  8889. err_vsi:
  8890. i40e_vsi_clear(vsi);
  8891. err_alloc:
  8892. return NULL;
  8893. }
  8894. /**
  8895. * i40e_veb_get_bw_info - Query VEB BW information
  8896. * @veb: the veb to query
  8897. *
  8898. * Query the Tx scheduler BW configuration data for given VEB
  8899. **/
  8900. static int i40e_veb_get_bw_info(struct i40e_veb *veb)
  8901. {
  8902. struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
  8903. struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
  8904. struct i40e_pf *pf = veb->pf;
  8905. struct i40e_hw *hw = &pf->hw;
  8906. u32 tc_bw_max;
  8907. int ret = 0;
  8908. int i;
  8909. ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
  8910. &bw_data, NULL);
  8911. if (ret) {
  8912. dev_info(&pf->pdev->dev,
  8913. "query veb bw config failed, err %s aq_err %s\n",
  8914. i40e_stat_str(&pf->hw, ret),
  8915. i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
  8916. goto out;
  8917. }
  8918. ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
  8919. &ets_data, NULL);
  8920. if (ret) {
  8921. dev_info(&pf->pdev->dev,
  8922. "query veb bw ets config failed, err %s aq_err %s\n",
  8923. i40e_stat_str(&pf->hw, ret),
  8924. i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
  8925. goto out;
  8926. }
  8927. veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
  8928. veb->bw_max_quanta = ets_data.tc_bw_max;
  8929. veb->is_abs_credits = bw_data.absolute_credits_enable;
  8930. veb->enabled_tc = ets_data.tc_valid_bits;
  8931. tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
  8932. (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
  8933. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  8934. veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
  8935. veb->bw_tc_limit_credits[i] =
  8936. le16_to_cpu(bw_data.tc_bw_limits[i]);
  8937. veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
  8938. }
  8939. out:
  8940. return ret;
  8941. }
  8942. /**
  8943. * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
  8944. * @pf: board private structure
  8945. *
  8946. * On error: returns error code (negative)
  8947. * On success: returns vsi index in PF (positive)
  8948. **/
  8949. static int i40e_veb_mem_alloc(struct i40e_pf *pf)
  8950. {
  8951. int ret = -ENOENT;
  8952. struct i40e_veb *veb;
  8953. int i;
  8954. /* Need to protect the allocation of switch elements at the PF level */
  8955. mutex_lock(&pf->switch_mutex);
  8956. /* VEB list may be fragmented if VEB creation/destruction has
  8957. * been happening. We can afford to do a quick scan to look
  8958. * for any free slots in the list.
  8959. *
  8960. * find next empty veb slot, looping back around if necessary
  8961. */
  8962. i = 0;
  8963. while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
  8964. i++;
  8965. if (i >= I40E_MAX_VEB) {
  8966. ret = -ENOMEM;
  8967. goto err_alloc_veb; /* out of VEB slots! */
  8968. }
  8969. veb = kzalloc(sizeof(*veb), GFP_KERNEL);
  8970. if (!veb) {
  8971. ret = -ENOMEM;
  8972. goto err_alloc_veb;
  8973. }
  8974. veb->pf = pf;
  8975. veb->idx = i;
  8976. veb->enabled_tc = 1;
  8977. pf->veb[i] = veb;
  8978. ret = i;
  8979. err_alloc_veb:
  8980. mutex_unlock(&pf->switch_mutex);
  8981. return ret;
  8982. }
  8983. /**
  8984. * i40e_switch_branch_release - Delete a branch of the switch tree
  8985. * @branch: where to start deleting
  8986. *
  8987. * This uses recursion to find the tips of the branch to be
  8988. * removed, deleting until we get back to and can delete this VEB.
  8989. **/
  8990. static void i40e_switch_branch_release(struct i40e_veb *branch)
  8991. {
  8992. struct i40e_pf *pf = branch->pf;
  8993. u16 branch_seid = branch->seid;
  8994. u16 veb_idx = branch->idx;
  8995. int i;
  8996. /* release any VEBs on this VEB - RECURSION */
  8997. for (i = 0; i < I40E_MAX_VEB; i++) {
  8998. if (!pf->veb[i])
  8999. continue;
  9000. if (pf->veb[i]->uplink_seid == branch->seid)
  9001. i40e_switch_branch_release(pf->veb[i]);
  9002. }
  9003. /* Release the VSIs on this VEB, but not the owner VSI.
  9004. *
  9005. * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
  9006. * the VEB itself, so don't use (*branch) after this loop.
  9007. */
  9008. for (i = 0; i < pf->num_alloc_vsi; i++) {
  9009. if (!pf->vsi[i])
  9010. continue;
  9011. if (pf->vsi[i]->uplink_seid == branch_seid &&
  9012. (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
  9013. i40e_vsi_release(pf->vsi[i]);
  9014. }
  9015. }
  9016. /* There's one corner case where the VEB might not have been
  9017. * removed, so double check it here and remove it if needed.
  9018. * This case happens if the veb was created from the debugfs
  9019. * commands and no VSIs were added to it.
  9020. */
  9021. if (pf->veb[veb_idx])
  9022. i40e_veb_release(pf->veb[veb_idx]);
  9023. }
  9024. /**
  9025. * i40e_veb_clear - remove veb struct
  9026. * @veb: the veb to remove
  9027. **/
  9028. static void i40e_veb_clear(struct i40e_veb *veb)
  9029. {
  9030. if (!veb)
  9031. return;
  9032. if (veb->pf) {
  9033. struct i40e_pf *pf = veb->pf;
  9034. mutex_lock(&pf->switch_mutex);
  9035. if (pf->veb[veb->idx] == veb)
  9036. pf->veb[veb->idx] = NULL;
  9037. mutex_unlock(&pf->switch_mutex);
  9038. }
  9039. kfree(veb);
  9040. }
  9041. /**
  9042. * i40e_veb_release - Delete a VEB and free its resources
  9043. * @veb: the VEB being removed
  9044. **/
  9045. void i40e_veb_release(struct i40e_veb *veb)
  9046. {
  9047. struct i40e_vsi *vsi = NULL;
  9048. struct i40e_pf *pf;
  9049. int i, n = 0;
  9050. pf = veb->pf;
  9051. /* find the remaining VSI and check for extras */
  9052. for (i = 0; i < pf->num_alloc_vsi; i++) {
  9053. if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
  9054. n++;
  9055. vsi = pf->vsi[i];
  9056. }
  9057. }
  9058. if (n != 1) {
  9059. dev_info(&pf->pdev->dev,
  9060. "can't remove VEB %d with %d VSIs left\n",
  9061. veb->seid, n);
  9062. return;
  9063. }
  9064. /* move the remaining VSI to uplink veb */
  9065. vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
  9066. if (veb->uplink_seid) {
  9067. vsi->uplink_seid = veb->uplink_seid;
  9068. if (veb->uplink_seid == pf->mac_seid)
  9069. vsi->veb_idx = I40E_NO_VEB;
  9070. else
  9071. vsi->veb_idx = veb->veb_idx;
  9072. } else {
  9073. /* floating VEB */
  9074. vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
  9075. vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
  9076. }
  9077. i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
  9078. i40e_veb_clear(veb);
  9079. }
  9080. /**
  9081. * i40e_add_veb - create the VEB in the switch
  9082. * @veb: the VEB to be instantiated
  9083. * @vsi: the controlling VSI
  9084. **/
  9085. static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
  9086. {
  9087. struct i40e_pf *pf = veb->pf;
  9088. bool enable_stats = !!(pf->flags & I40E_FLAG_VEB_STATS_ENABLED);
  9089. int ret;
  9090. ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi->seid,
  9091. veb->enabled_tc, false,
  9092. &veb->seid, enable_stats, NULL);
  9093. /* get a VEB from the hardware */
  9094. if (ret) {
  9095. dev_info(&pf->pdev->dev,
  9096. "couldn't add VEB, err %s aq_err %s\n",
  9097. i40e_stat_str(&pf->hw, ret),
  9098. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  9099. return -EPERM;
  9100. }
  9101. /* get statistics counter */
  9102. ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
  9103. &veb->stats_idx, NULL, NULL, NULL);
  9104. if (ret) {
  9105. dev_info(&pf->pdev->dev,
  9106. "couldn't get VEB statistics idx, err %s aq_err %s\n",
  9107. i40e_stat_str(&pf->hw, ret),
  9108. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  9109. return -EPERM;
  9110. }
  9111. ret = i40e_veb_get_bw_info(veb);
  9112. if (ret) {
  9113. dev_info(&pf->pdev->dev,
  9114. "couldn't get VEB bw info, err %s aq_err %s\n",
  9115. i40e_stat_str(&pf->hw, ret),
  9116. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  9117. i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
  9118. return -ENOENT;
  9119. }
  9120. vsi->uplink_seid = veb->seid;
  9121. vsi->veb_idx = veb->idx;
  9122. vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
  9123. return 0;
  9124. }
  9125. /**
  9126. * i40e_veb_setup - Set up a VEB
  9127. * @pf: board private structure
  9128. * @flags: VEB setup flags
  9129. * @uplink_seid: the switch element to link to
  9130. * @vsi_seid: the initial VSI seid
  9131. * @enabled_tc: Enabled TC bit-map
  9132. *
  9133. * This allocates the sw VEB structure and links it into the switch
  9134. * It is possible and legal for this to be a duplicate of an already
  9135. * existing VEB. It is also possible for both uplink and vsi seids
  9136. * to be zero, in order to create a floating VEB.
  9137. *
  9138. * Returns pointer to the successfully allocated VEB sw struct on
  9139. * success, otherwise returns NULL on failure.
  9140. **/
  9141. struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
  9142. u16 uplink_seid, u16 vsi_seid,
  9143. u8 enabled_tc)
  9144. {
  9145. struct i40e_veb *veb, *uplink_veb = NULL;
  9146. int vsi_idx, veb_idx;
  9147. int ret;
  9148. /* if one seid is 0, the other must be 0 to create a floating relay */
  9149. if ((uplink_seid == 0 || vsi_seid == 0) &&
  9150. (uplink_seid + vsi_seid != 0)) {
  9151. dev_info(&pf->pdev->dev,
  9152. "one, not both seid's are 0: uplink=%d vsi=%d\n",
  9153. uplink_seid, vsi_seid);
  9154. return NULL;
  9155. }
  9156. /* make sure there is such a vsi and uplink */
  9157. for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
  9158. if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
  9159. break;
  9160. if (vsi_idx >= pf->num_alloc_vsi && vsi_seid != 0) {
  9161. dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
  9162. vsi_seid);
  9163. return NULL;
  9164. }
  9165. if (uplink_seid && uplink_seid != pf->mac_seid) {
  9166. for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
  9167. if (pf->veb[veb_idx] &&
  9168. pf->veb[veb_idx]->seid == uplink_seid) {
  9169. uplink_veb = pf->veb[veb_idx];
  9170. break;
  9171. }
  9172. }
  9173. if (!uplink_veb) {
  9174. dev_info(&pf->pdev->dev,
  9175. "uplink seid %d not found\n", uplink_seid);
  9176. return NULL;
  9177. }
  9178. }
  9179. /* get veb sw struct */
  9180. veb_idx = i40e_veb_mem_alloc(pf);
  9181. if (veb_idx < 0)
  9182. goto err_alloc;
  9183. veb = pf->veb[veb_idx];
  9184. veb->flags = flags;
  9185. veb->uplink_seid = uplink_seid;
  9186. veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
  9187. veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
  9188. /* create the VEB in the switch */
  9189. ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
  9190. if (ret)
  9191. goto err_veb;
  9192. if (vsi_idx == pf->lan_vsi)
  9193. pf->lan_veb = veb->idx;
  9194. return veb;
  9195. err_veb:
  9196. i40e_veb_clear(veb);
  9197. err_alloc:
  9198. return NULL;
  9199. }
  9200. /**
  9201. * i40e_setup_pf_switch_element - set PF vars based on switch type
  9202. * @pf: board private structure
  9203. * @ele: element we are building info from
  9204. * @num_reported: total number of elements
  9205. * @printconfig: should we print the contents
  9206. *
  9207. * helper function to assist in extracting a few useful SEID values.
  9208. **/
  9209. static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
  9210. struct i40e_aqc_switch_config_element_resp *ele,
  9211. u16 num_reported, bool printconfig)
  9212. {
  9213. u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
  9214. u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
  9215. u8 element_type = ele->element_type;
  9216. u16 seid = le16_to_cpu(ele->seid);
  9217. if (printconfig)
  9218. dev_info(&pf->pdev->dev,
  9219. "type=%d seid=%d uplink=%d downlink=%d\n",
  9220. element_type, seid, uplink_seid, downlink_seid);
  9221. switch (element_type) {
  9222. case I40E_SWITCH_ELEMENT_TYPE_MAC:
  9223. pf->mac_seid = seid;
  9224. break;
  9225. case I40E_SWITCH_ELEMENT_TYPE_VEB:
  9226. /* Main VEB? */
  9227. if (uplink_seid != pf->mac_seid)
  9228. break;
  9229. if (pf->lan_veb == I40E_NO_VEB) {
  9230. int v;
  9231. /* find existing or else empty VEB */
  9232. for (v = 0; v < I40E_MAX_VEB; v++) {
  9233. if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
  9234. pf->lan_veb = v;
  9235. break;
  9236. }
  9237. }
  9238. if (pf->lan_veb == I40E_NO_VEB) {
  9239. v = i40e_veb_mem_alloc(pf);
  9240. if (v < 0)
  9241. break;
  9242. pf->lan_veb = v;
  9243. }
  9244. }
  9245. pf->veb[pf->lan_veb]->seid = seid;
  9246. pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
  9247. pf->veb[pf->lan_veb]->pf = pf;
  9248. pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
  9249. break;
  9250. case I40E_SWITCH_ELEMENT_TYPE_VSI:
  9251. if (num_reported != 1)
  9252. break;
  9253. /* This is immediately after a reset so we can assume this is
  9254. * the PF's VSI
  9255. */
  9256. pf->mac_seid = uplink_seid;
  9257. pf->pf_seid = downlink_seid;
  9258. pf->main_vsi_seid = seid;
  9259. if (printconfig)
  9260. dev_info(&pf->pdev->dev,
  9261. "pf_seid=%d main_vsi_seid=%d\n",
  9262. pf->pf_seid, pf->main_vsi_seid);
  9263. break;
  9264. case I40E_SWITCH_ELEMENT_TYPE_PF:
  9265. case I40E_SWITCH_ELEMENT_TYPE_VF:
  9266. case I40E_SWITCH_ELEMENT_TYPE_EMP:
  9267. case I40E_SWITCH_ELEMENT_TYPE_BMC:
  9268. case I40E_SWITCH_ELEMENT_TYPE_PE:
  9269. case I40E_SWITCH_ELEMENT_TYPE_PA:
  9270. /* ignore these for now */
  9271. break;
  9272. default:
  9273. dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
  9274. element_type, seid);
  9275. break;
  9276. }
  9277. }
  9278. /**
  9279. * i40e_fetch_switch_configuration - Get switch config from firmware
  9280. * @pf: board private structure
  9281. * @printconfig: should we print the contents
  9282. *
  9283. * Get the current switch configuration from the device and
  9284. * extract a few useful SEID values.
  9285. **/
  9286. int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
  9287. {
  9288. struct i40e_aqc_get_switch_config_resp *sw_config;
  9289. u16 next_seid = 0;
  9290. int ret = 0;
  9291. u8 *aq_buf;
  9292. int i;
  9293. aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
  9294. if (!aq_buf)
  9295. return -ENOMEM;
  9296. sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
  9297. do {
  9298. u16 num_reported, num_total;
  9299. ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
  9300. I40E_AQ_LARGE_BUF,
  9301. &next_seid, NULL);
  9302. if (ret) {
  9303. dev_info(&pf->pdev->dev,
  9304. "get switch config failed err %s aq_err %s\n",
  9305. i40e_stat_str(&pf->hw, ret),
  9306. i40e_aq_str(&pf->hw,
  9307. pf->hw.aq.asq_last_status));
  9308. kfree(aq_buf);
  9309. return -ENOENT;
  9310. }
  9311. num_reported = le16_to_cpu(sw_config->header.num_reported);
  9312. num_total = le16_to_cpu(sw_config->header.num_total);
  9313. if (printconfig)
  9314. dev_info(&pf->pdev->dev,
  9315. "header: %d reported %d total\n",
  9316. num_reported, num_total);
  9317. for (i = 0; i < num_reported; i++) {
  9318. struct i40e_aqc_switch_config_element_resp *ele =
  9319. &sw_config->element[i];
  9320. i40e_setup_pf_switch_element(pf, ele, num_reported,
  9321. printconfig);
  9322. }
  9323. } while (next_seid != 0);
  9324. kfree(aq_buf);
  9325. return ret;
  9326. }
  9327. /**
  9328. * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
  9329. * @pf: board private structure
  9330. * @reinit: if the Main VSI needs to re-initialized.
  9331. *
  9332. * Returns 0 on success, negative value on failure
  9333. **/
  9334. static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit)
  9335. {
  9336. u16 flags = 0;
  9337. int ret;
  9338. /* find out what's out there already */
  9339. ret = i40e_fetch_switch_configuration(pf, false);
  9340. if (ret) {
  9341. dev_info(&pf->pdev->dev,
  9342. "couldn't fetch switch config, err %s aq_err %s\n",
  9343. i40e_stat_str(&pf->hw, ret),
  9344. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  9345. return ret;
  9346. }
  9347. i40e_pf_reset_stats(pf);
  9348. /* set the switch config bit for the whole device to
  9349. * support limited promisc or true promisc
  9350. * when user requests promisc. The default is limited
  9351. * promisc.
  9352. */
  9353. if ((pf->hw.pf_id == 0) &&
  9354. !(pf->flags & I40E_FLAG_TRUE_PROMISC_SUPPORT))
  9355. flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
  9356. if (pf->hw.pf_id == 0) {
  9357. u16 valid_flags;
  9358. valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
  9359. ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags,
  9360. NULL);
  9361. if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) {
  9362. dev_info(&pf->pdev->dev,
  9363. "couldn't set switch config bits, err %s aq_err %s\n",
  9364. i40e_stat_str(&pf->hw, ret),
  9365. i40e_aq_str(&pf->hw,
  9366. pf->hw.aq.asq_last_status));
  9367. /* not a fatal problem, just keep going */
  9368. }
  9369. }
  9370. /* first time setup */
  9371. if (pf->lan_vsi == I40E_NO_VSI || reinit) {
  9372. struct i40e_vsi *vsi = NULL;
  9373. u16 uplink_seid;
  9374. /* Set up the PF VSI associated with the PF's main VSI
  9375. * that is already in the HW switch
  9376. */
  9377. if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
  9378. uplink_seid = pf->veb[pf->lan_veb]->seid;
  9379. else
  9380. uplink_seid = pf->mac_seid;
  9381. if (pf->lan_vsi == I40E_NO_VSI)
  9382. vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
  9383. else if (reinit)
  9384. vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
  9385. if (!vsi) {
  9386. dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
  9387. i40e_fdir_teardown(pf);
  9388. return -EAGAIN;
  9389. }
  9390. } else {
  9391. /* force a reset of TC and queue layout configurations */
  9392. u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
  9393. pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
  9394. pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
  9395. i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
  9396. }
  9397. i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
  9398. i40e_fdir_sb_setup(pf);
  9399. /* Setup static PF queue filter control settings */
  9400. ret = i40e_setup_pf_filter_control(pf);
  9401. if (ret) {
  9402. dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
  9403. ret);
  9404. /* Failure here should not stop continuing other steps */
  9405. }
  9406. /* enable RSS in the HW, even for only one queue, as the stack can use
  9407. * the hash
  9408. */
  9409. if ((pf->flags & I40E_FLAG_RSS_ENABLED))
  9410. i40e_pf_config_rss(pf);
  9411. /* fill in link information and enable LSE reporting */
  9412. i40e_link_event(pf);
  9413. /* Initialize user-specific link properties */
  9414. pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
  9415. I40E_AQ_AN_COMPLETED) ? true : false);
  9416. i40e_ptp_init(pf);
  9417. return ret;
  9418. }
  9419. /**
  9420. * i40e_determine_queue_usage - Work out queue distribution
  9421. * @pf: board private structure
  9422. **/
  9423. static void i40e_determine_queue_usage(struct i40e_pf *pf)
  9424. {
  9425. int queues_left;
  9426. pf->num_lan_qps = 0;
  9427. /* Find the max queues to be put into basic use. We'll always be
  9428. * using TC0, whether or not DCB is running, and TC0 will get the
  9429. * big RSS set.
  9430. */
  9431. queues_left = pf->hw.func_caps.num_tx_qp;
  9432. if ((queues_left == 1) ||
  9433. !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
  9434. /* one qp for PF, no queues for anything else */
  9435. queues_left = 0;
  9436. pf->alloc_rss_size = pf->num_lan_qps = 1;
  9437. /* make sure all the fancies are disabled */
  9438. pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
  9439. I40E_FLAG_IWARP_ENABLED |
  9440. I40E_FLAG_FD_SB_ENABLED |
  9441. I40E_FLAG_FD_ATR_ENABLED |
  9442. I40E_FLAG_DCB_CAPABLE |
  9443. I40E_FLAG_DCB_ENABLED |
  9444. I40E_FLAG_SRIOV_ENABLED |
  9445. I40E_FLAG_VMDQ_ENABLED);
  9446. } else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
  9447. I40E_FLAG_FD_SB_ENABLED |
  9448. I40E_FLAG_FD_ATR_ENABLED |
  9449. I40E_FLAG_DCB_CAPABLE))) {
  9450. /* one qp for PF */
  9451. pf->alloc_rss_size = pf->num_lan_qps = 1;
  9452. queues_left -= pf->num_lan_qps;
  9453. pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
  9454. I40E_FLAG_IWARP_ENABLED |
  9455. I40E_FLAG_FD_SB_ENABLED |
  9456. I40E_FLAG_FD_ATR_ENABLED |
  9457. I40E_FLAG_DCB_ENABLED |
  9458. I40E_FLAG_VMDQ_ENABLED);
  9459. } else {
  9460. /* Not enough queues for all TCs */
  9461. if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
  9462. (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
  9463. pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
  9464. I40E_FLAG_DCB_ENABLED);
  9465. dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
  9466. }
  9467. pf->num_lan_qps = max_t(int, pf->rss_size_max,
  9468. num_online_cpus());
  9469. pf->num_lan_qps = min_t(int, pf->num_lan_qps,
  9470. pf->hw.func_caps.num_tx_qp);
  9471. queues_left -= pf->num_lan_qps;
  9472. }
  9473. if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
  9474. if (queues_left > 1) {
  9475. queues_left -= 1; /* save 1 queue for FD */
  9476. } else {
  9477. pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
  9478. dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
  9479. }
  9480. }
  9481. if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
  9482. pf->num_vf_qps && pf->num_req_vfs && queues_left) {
  9483. pf->num_req_vfs = min_t(int, pf->num_req_vfs,
  9484. (queues_left / pf->num_vf_qps));
  9485. queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
  9486. }
  9487. if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
  9488. pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
  9489. pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
  9490. (queues_left / pf->num_vmdq_qps));
  9491. queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
  9492. }
  9493. pf->queues_left = queues_left;
  9494. dev_dbg(&pf->pdev->dev,
  9495. "qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
  9496. pf->hw.func_caps.num_tx_qp,
  9497. !!(pf->flags & I40E_FLAG_FD_SB_ENABLED),
  9498. pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
  9499. pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
  9500. queues_left);
  9501. }
  9502. /**
  9503. * i40e_setup_pf_filter_control - Setup PF static filter control
  9504. * @pf: PF to be setup
  9505. *
  9506. * i40e_setup_pf_filter_control sets up a PF's initial filter control
  9507. * settings. If PE/FCoE are enabled then it will also set the per PF
  9508. * based filter sizes required for them. It also enables Flow director,
  9509. * ethertype and macvlan type filter settings for the pf.
  9510. *
  9511. * Returns 0 on success, negative on failure
  9512. **/
  9513. static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
  9514. {
  9515. struct i40e_filter_control_settings *settings = &pf->filter_settings;
  9516. settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
  9517. /* Flow Director is enabled */
  9518. if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
  9519. settings->enable_fdir = true;
  9520. /* Ethtype and MACVLAN filters enabled for PF */
  9521. settings->enable_ethtype = true;
  9522. settings->enable_macvlan = true;
  9523. if (i40e_set_filter_control(&pf->hw, settings))
  9524. return -ENOENT;
  9525. return 0;
  9526. }
  9527. #define INFO_STRING_LEN 255
  9528. #define REMAIN(__x) (INFO_STRING_LEN - (__x))
  9529. static void i40e_print_features(struct i40e_pf *pf)
  9530. {
  9531. struct i40e_hw *hw = &pf->hw;
  9532. char *buf;
  9533. int i;
  9534. buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
  9535. if (!buf)
  9536. return;
  9537. i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
  9538. #ifdef CONFIG_PCI_IOV
  9539. i += snprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
  9540. #endif
  9541. i += snprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d",
  9542. pf->hw.func_caps.num_vsis,
  9543. pf->vsi[pf->lan_vsi]->num_queue_pairs);
  9544. if (pf->flags & I40E_FLAG_RSS_ENABLED)
  9545. i += snprintf(&buf[i], REMAIN(i), " RSS");
  9546. if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
  9547. i += snprintf(&buf[i], REMAIN(i), " FD_ATR");
  9548. if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
  9549. i += snprintf(&buf[i], REMAIN(i), " FD_SB");
  9550. i += snprintf(&buf[i], REMAIN(i), " NTUPLE");
  9551. }
  9552. if (pf->flags & I40E_FLAG_DCB_CAPABLE)
  9553. i += snprintf(&buf[i], REMAIN(i), " DCB");
  9554. i += snprintf(&buf[i], REMAIN(i), " VxLAN");
  9555. i += snprintf(&buf[i], REMAIN(i), " Geneve");
  9556. if (pf->flags & I40E_FLAG_PTP)
  9557. i += snprintf(&buf[i], REMAIN(i), " PTP");
  9558. if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
  9559. i += snprintf(&buf[i], REMAIN(i), " VEB");
  9560. else
  9561. i += snprintf(&buf[i], REMAIN(i), " VEPA");
  9562. dev_info(&pf->pdev->dev, "%s\n", buf);
  9563. kfree(buf);
  9564. WARN_ON(i > INFO_STRING_LEN);
  9565. }
  9566. /**
  9567. * i40e_get_platform_mac_addr - get platform-specific MAC address
  9568. * @pdev: PCI device information struct
  9569. * @pf: board private structure
  9570. *
  9571. * Look up the MAC address for the device. First we'll try
  9572. * eth_platform_get_mac_address, which will check Open Firmware, or arch
  9573. * specific fallback. Otherwise, we'll default to the stored value in
  9574. * firmware.
  9575. **/
  9576. static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
  9577. {
  9578. if (eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
  9579. i40e_get_mac_addr(&pf->hw, pf->hw.mac.addr);
  9580. }
  9581. /**
  9582. * i40e_probe - Device initialization routine
  9583. * @pdev: PCI device information struct
  9584. * @ent: entry in i40e_pci_tbl
  9585. *
  9586. * i40e_probe initializes a PF identified by a pci_dev structure.
  9587. * The OS initialization, configuring of the PF private structure,
  9588. * and a hardware reset occur.
  9589. *
  9590. * Returns 0 on success, negative on failure
  9591. **/
  9592. static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  9593. {
  9594. struct i40e_aq_get_phy_abilities_resp abilities;
  9595. struct i40e_pf *pf;
  9596. struct i40e_hw *hw;
  9597. static u16 pfs_found;
  9598. u16 wol_nvm_bits;
  9599. u16 link_status;
  9600. int err;
  9601. u32 val;
  9602. u32 i;
  9603. u8 set_fc_aq_fail;
  9604. err = pci_enable_device_mem(pdev);
  9605. if (err)
  9606. return err;
  9607. /* set up for high or low dma */
  9608. err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
  9609. if (err) {
  9610. err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  9611. if (err) {
  9612. dev_err(&pdev->dev,
  9613. "DMA configuration failed: 0x%x\n", err);
  9614. goto err_dma;
  9615. }
  9616. }
  9617. /* set up pci connections */
  9618. err = pci_request_mem_regions(pdev, i40e_driver_name);
  9619. if (err) {
  9620. dev_info(&pdev->dev,
  9621. "pci_request_selected_regions failed %d\n", err);
  9622. goto err_pci_reg;
  9623. }
  9624. pci_enable_pcie_error_reporting(pdev);
  9625. pci_set_master(pdev);
  9626. /* Now that we have a PCI connection, we need to do the
  9627. * low level device setup. This is primarily setting up
  9628. * the Admin Queue structures and then querying for the
  9629. * device's current profile information.
  9630. */
  9631. pf = kzalloc(sizeof(*pf), GFP_KERNEL);
  9632. if (!pf) {
  9633. err = -ENOMEM;
  9634. goto err_pf_alloc;
  9635. }
  9636. pf->next_vsi = 0;
  9637. pf->pdev = pdev;
  9638. set_bit(__I40E_DOWN, &pf->state);
  9639. hw = &pf->hw;
  9640. hw->back = pf;
  9641. pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
  9642. I40E_MAX_CSR_SPACE);
  9643. hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
  9644. if (!hw->hw_addr) {
  9645. err = -EIO;
  9646. dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
  9647. (unsigned int)pci_resource_start(pdev, 0),
  9648. pf->ioremap_len, err);
  9649. goto err_ioremap;
  9650. }
  9651. hw->vendor_id = pdev->vendor;
  9652. hw->device_id = pdev->device;
  9653. pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
  9654. hw->subsystem_vendor_id = pdev->subsystem_vendor;
  9655. hw->subsystem_device_id = pdev->subsystem_device;
  9656. hw->bus.device = PCI_SLOT(pdev->devfn);
  9657. hw->bus.func = PCI_FUNC(pdev->devfn);
  9658. hw->bus.bus_id = pdev->bus->number;
  9659. pf->instance = pfs_found;
  9660. INIT_LIST_HEAD(&pf->l3_flex_pit_list);
  9661. INIT_LIST_HEAD(&pf->l4_flex_pit_list);
  9662. /* set up the locks for the AQ, do this only once in probe
  9663. * and destroy them only once in remove
  9664. */
  9665. mutex_init(&hw->aq.asq_mutex);
  9666. mutex_init(&hw->aq.arq_mutex);
  9667. pf->msg_enable = netif_msg_init(debug,
  9668. NETIF_MSG_DRV |
  9669. NETIF_MSG_PROBE |
  9670. NETIF_MSG_LINK);
  9671. if (debug < -1)
  9672. pf->hw.debug_mask = debug;
  9673. /* do a special CORER for clearing PXE mode once at init */
  9674. if (hw->revision_id == 0 &&
  9675. (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
  9676. wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
  9677. i40e_flush(hw);
  9678. msleep(200);
  9679. pf->corer_count++;
  9680. i40e_clear_pxe_mode(hw);
  9681. }
  9682. /* Reset here to make sure all is clean and to define PF 'n' */
  9683. i40e_clear_hw(hw);
  9684. err = i40e_pf_reset(hw);
  9685. if (err) {
  9686. dev_info(&pdev->dev, "Initial pf_reset failed: %d\n", err);
  9687. goto err_pf_reset;
  9688. }
  9689. pf->pfr_count++;
  9690. hw->aq.num_arq_entries = I40E_AQ_LEN;
  9691. hw->aq.num_asq_entries = I40E_AQ_LEN;
  9692. hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
  9693. hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
  9694. pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
  9695. snprintf(pf->int_name, sizeof(pf->int_name) - 1,
  9696. "%s-%s:misc",
  9697. dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
  9698. err = i40e_init_shared_code(hw);
  9699. if (err) {
  9700. dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
  9701. err);
  9702. goto err_pf_reset;
  9703. }
  9704. /* set up a default setting for link flow control */
  9705. pf->hw.fc.requested_mode = I40E_FC_NONE;
  9706. err = i40e_init_adminq(hw);
  9707. if (err) {
  9708. if (err == I40E_ERR_FIRMWARE_API_VERSION)
  9709. dev_info(&pdev->dev,
  9710. "The driver for the device stopped because the NVM image is newer than expected. You must install the most recent version of the network driver.\n");
  9711. else
  9712. dev_info(&pdev->dev,
  9713. "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
  9714. goto err_pf_reset;
  9715. }
  9716. /* provide nvm, fw, api versions */
  9717. dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s\n",
  9718. hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
  9719. hw->aq.api_maj_ver, hw->aq.api_min_ver,
  9720. i40e_nvm_version_str(hw));
  9721. if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
  9722. hw->aq.api_min_ver > I40E_FW_API_VERSION_MINOR)
  9723. dev_info(&pdev->dev,
  9724. "The driver for the device detected a newer version of the NVM image than expected. Please install the most recent version of the network driver.\n");
  9725. else if (hw->aq.api_maj_ver < I40E_FW_API_VERSION_MAJOR ||
  9726. hw->aq.api_min_ver < (I40E_FW_API_VERSION_MINOR - 1))
  9727. dev_info(&pdev->dev,
  9728. "The driver for the device detected an older version of the NVM image than expected. Please update the NVM image.\n");
  9729. i40e_verify_eeprom(pf);
  9730. /* Rev 0 hardware was never productized */
  9731. if (hw->revision_id < 1)
  9732. dev_warn(&pdev->dev, "This device is a pre-production adapter/LOM. Please be aware there may be issues with your hardware. If you are experiencing problems please contact your Intel or hardware representative who provided you with this hardware.\n");
  9733. i40e_clear_pxe_mode(hw);
  9734. err = i40e_get_capabilities(pf);
  9735. if (err)
  9736. goto err_adminq_setup;
  9737. err = i40e_sw_init(pf);
  9738. if (err) {
  9739. dev_info(&pdev->dev, "sw_init failed: %d\n", err);
  9740. goto err_sw_init;
  9741. }
  9742. err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
  9743. hw->func_caps.num_rx_qp, 0, 0);
  9744. if (err) {
  9745. dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
  9746. goto err_init_lan_hmc;
  9747. }
  9748. err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
  9749. if (err) {
  9750. dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
  9751. err = -ENOENT;
  9752. goto err_configure_lan_hmc;
  9753. }
  9754. /* Disable LLDP for NICs that have firmware versions lower than v4.3.
  9755. * Ignore error return codes because if it was already disabled via
  9756. * hardware settings this will fail
  9757. */
  9758. if (pf->flags & I40E_FLAG_STOP_FW_LLDP) {
  9759. dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
  9760. i40e_aq_stop_lldp(hw, true, NULL);
  9761. }
  9762. /* allow a platform config to override the HW addr */
  9763. i40e_get_platform_mac_addr(pdev, pf);
  9764. if (!is_valid_ether_addr(hw->mac.addr)) {
  9765. dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
  9766. err = -EIO;
  9767. goto err_mac_addr;
  9768. }
  9769. dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
  9770. ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
  9771. i40e_get_port_mac_addr(hw, hw->mac.port_addr);
  9772. if (is_valid_ether_addr(hw->mac.port_addr))
  9773. pf->flags |= I40E_FLAG_PORT_ID_VALID;
  9774. pci_set_drvdata(pdev, pf);
  9775. pci_save_state(pdev);
  9776. #ifdef CONFIG_I40E_DCB
  9777. err = i40e_init_pf_dcb(pf);
  9778. if (err) {
  9779. dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
  9780. pf->flags &= ~(I40E_FLAG_DCB_CAPABLE | I40E_FLAG_DCB_ENABLED);
  9781. /* Continue without DCB enabled */
  9782. }
  9783. #endif /* CONFIG_I40E_DCB */
  9784. /* set up periodic task facility */
  9785. setup_timer(&pf->service_timer, i40e_service_timer, (unsigned long)pf);
  9786. pf->service_timer_period = HZ;
  9787. INIT_WORK(&pf->service_task, i40e_service_task);
  9788. clear_bit(__I40E_SERVICE_SCHED, &pf->state);
  9789. /* NVM bit on means WoL disabled for the port */
  9790. i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
  9791. if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
  9792. pf->wol_en = false;
  9793. else
  9794. pf->wol_en = true;
  9795. device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
  9796. /* set up the main switch operations */
  9797. i40e_determine_queue_usage(pf);
  9798. err = i40e_init_interrupt_scheme(pf);
  9799. if (err)
  9800. goto err_switch_setup;
  9801. /* The number of VSIs reported by the FW is the minimum guaranteed
  9802. * to us; HW supports far more and we share the remaining pool with
  9803. * the other PFs. We allocate space for more than the guarantee with
  9804. * the understanding that we might not get them all later.
  9805. */
  9806. if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
  9807. pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
  9808. else
  9809. pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
  9810. /* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
  9811. pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
  9812. GFP_KERNEL);
  9813. if (!pf->vsi) {
  9814. err = -ENOMEM;
  9815. goto err_switch_setup;
  9816. }
  9817. #ifdef CONFIG_PCI_IOV
  9818. /* prep for VF support */
  9819. if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
  9820. (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
  9821. !test_bit(__I40E_BAD_EEPROM, &pf->state)) {
  9822. if (pci_num_vf(pdev))
  9823. pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
  9824. }
  9825. #endif
  9826. err = i40e_setup_pf_switch(pf, false);
  9827. if (err) {
  9828. dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
  9829. goto err_vsis;
  9830. }
  9831. /* Make sure flow control is set according to current settings */
  9832. err = i40e_set_fc(hw, &set_fc_aq_fail, true);
  9833. if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_GET)
  9834. dev_dbg(&pf->pdev->dev,
  9835. "Set fc with err %s aq_err %s on get_phy_cap\n",
  9836. i40e_stat_str(hw, err),
  9837. i40e_aq_str(hw, hw->aq.asq_last_status));
  9838. if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_SET)
  9839. dev_dbg(&pf->pdev->dev,
  9840. "Set fc with err %s aq_err %s on set_phy_config\n",
  9841. i40e_stat_str(hw, err),
  9842. i40e_aq_str(hw, hw->aq.asq_last_status));
  9843. if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_UPDATE)
  9844. dev_dbg(&pf->pdev->dev,
  9845. "Set fc with err %s aq_err %s on get_link_info\n",
  9846. i40e_stat_str(hw, err),
  9847. i40e_aq_str(hw, hw->aq.asq_last_status));
  9848. /* if FDIR VSI was set up, start it now */
  9849. for (i = 0; i < pf->num_alloc_vsi; i++) {
  9850. if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
  9851. i40e_vsi_open(pf->vsi[i]);
  9852. break;
  9853. }
  9854. }
  9855. /* The driver only wants link up/down and module qualification
  9856. * reports from firmware. Note the negative logic.
  9857. */
  9858. err = i40e_aq_set_phy_int_mask(&pf->hw,
  9859. ~(I40E_AQ_EVENT_LINK_UPDOWN |
  9860. I40E_AQ_EVENT_MEDIA_NA |
  9861. I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
  9862. if (err)
  9863. dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
  9864. i40e_stat_str(&pf->hw, err),
  9865. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  9866. /* Reconfigure hardware for allowing smaller MSS in the case
  9867. * of TSO, so that we avoid the MDD being fired and causing
  9868. * a reset in the case of small MSS+TSO.
  9869. */
  9870. val = rd32(hw, I40E_REG_MSS);
  9871. if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
  9872. val &= ~I40E_REG_MSS_MIN_MASK;
  9873. val |= I40E_64BYTE_MSS;
  9874. wr32(hw, I40E_REG_MSS, val);
  9875. }
  9876. if (pf->flags & I40E_FLAG_RESTART_AUTONEG) {
  9877. msleep(75);
  9878. err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
  9879. if (err)
  9880. dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
  9881. i40e_stat_str(&pf->hw, err),
  9882. i40e_aq_str(&pf->hw,
  9883. pf->hw.aq.asq_last_status));
  9884. }
  9885. /* The main driver is (mostly) up and happy. We need to set this state
  9886. * before setting up the misc vector or we get a race and the vector
  9887. * ends up disabled forever.
  9888. */
  9889. clear_bit(__I40E_DOWN, &pf->state);
  9890. /* In case of MSIX we are going to setup the misc vector right here
  9891. * to handle admin queue events etc. In case of legacy and MSI
  9892. * the misc functionality and queue processing is combined in
  9893. * the same vector and that gets setup at open.
  9894. */
  9895. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  9896. err = i40e_setup_misc_vector(pf);
  9897. if (err) {
  9898. dev_info(&pdev->dev,
  9899. "setup of misc vector failed: %d\n", err);
  9900. goto err_vsis;
  9901. }
  9902. }
  9903. #ifdef CONFIG_PCI_IOV
  9904. /* prep for VF support */
  9905. if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
  9906. (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
  9907. !test_bit(__I40E_BAD_EEPROM, &pf->state)) {
  9908. /* disable link interrupts for VFs */
  9909. val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
  9910. val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
  9911. wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
  9912. i40e_flush(hw);
  9913. if (pci_num_vf(pdev)) {
  9914. dev_info(&pdev->dev,
  9915. "Active VFs found, allocating resources.\n");
  9916. err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
  9917. if (err)
  9918. dev_info(&pdev->dev,
  9919. "Error %d allocating resources for existing VFs\n",
  9920. err);
  9921. }
  9922. }
  9923. #endif /* CONFIG_PCI_IOV */
  9924. if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
  9925. pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
  9926. pf->num_iwarp_msix,
  9927. I40E_IWARP_IRQ_PILE_ID);
  9928. if (pf->iwarp_base_vector < 0) {
  9929. dev_info(&pdev->dev,
  9930. "failed to get tracking for %d vectors for IWARP err=%d\n",
  9931. pf->num_iwarp_msix, pf->iwarp_base_vector);
  9932. pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
  9933. }
  9934. }
  9935. i40e_dbg_pf_init(pf);
  9936. /* tell the firmware that we're starting */
  9937. i40e_send_version(pf);
  9938. /* since everything's happy, start the service_task timer */
  9939. mod_timer(&pf->service_timer,
  9940. round_jiffies(jiffies + pf->service_timer_period));
  9941. /* add this PF to client device list and launch a client service task */
  9942. if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
  9943. err = i40e_lan_add_device(pf);
  9944. if (err)
  9945. dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
  9946. err);
  9947. }
  9948. #define PCI_SPEED_SIZE 8
  9949. #define PCI_WIDTH_SIZE 8
  9950. /* Devices on the IOSF bus do not have this information
  9951. * and will report PCI Gen 1 x 1 by default so don't bother
  9952. * checking them.
  9953. */
  9954. if (!(pf->flags & I40E_FLAG_NO_PCI_LINK_CHECK)) {
  9955. char speed[PCI_SPEED_SIZE] = "Unknown";
  9956. char width[PCI_WIDTH_SIZE] = "Unknown";
  9957. /* Get the negotiated link width and speed from PCI config
  9958. * space
  9959. */
  9960. pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
  9961. &link_status);
  9962. i40e_set_pci_config_data(hw, link_status);
  9963. switch (hw->bus.speed) {
  9964. case i40e_bus_speed_8000:
  9965. strncpy(speed, "8.0", PCI_SPEED_SIZE); break;
  9966. case i40e_bus_speed_5000:
  9967. strncpy(speed, "5.0", PCI_SPEED_SIZE); break;
  9968. case i40e_bus_speed_2500:
  9969. strncpy(speed, "2.5", PCI_SPEED_SIZE); break;
  9970. default:
  9971. break;
  9972. }
  9973. switch (hw->bus.width) {
  9974. case i40e_bus_width_pcie_x8:
  9975. strncpy(width, "8", PCI_WIDTH_SIZE); break;
  9976. case i40e_bus_width_pcie_x4:
  9977. strncpy(width, "4", PCI_WIDTH_SIZE); break;
  9978. case i40e_bus_width_pcie_x2:
  9979. strncpy(width, "2", PCI_WIDTH_SIZE); break;
  9980. case i40e_bus_width_pcie_x1:
  9981. strncpy(width, "1", PCI_WIDTH_SIZE); break;
  9982. default:
  9983. break;
  9984. }
  9985. dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
  9986. speed, width);
  9987. if (hw->bus.width < i40e_bus_width_pcie_x8 ||
  9988. hw->bus.speed < i40e_bus_speed_8000) {
  9989. dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
  9990. dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
  9991. }
  9992. }
  9993. /* get the requested speeds from the fw */
  9994. err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
  9995. if (err)
  9996. dev_dbg(&pf->pdev->dev, "get requested speeds ret = %s last_status = %s\n",
  9997. i40e_stat_str(&pf->hw, err),
  9998. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  9999. pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
  10000. /* get the supported phy types from the fw */
  10001. err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
  10002. if (err)
  10003. dev_dbg(&pf->pdev->dev, "get supported phy types ret = %s last_status = %s\n",
  10004. i40e_stat_str(&pf->hw, err),
  10005. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  10006. /* Add a filter to drop all Flow control frames from any VSI from being
  10007. * transmitted. By doing so we stop a malicious VF from sending out
  10008. * PAUSE or PFC frames and potentially controlling traffic for other
  10009. * PF/VF VSIs.
  10010. * The FW can still send Flow control frames if enabled.
  10011. */
  10012. i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
  10013. pf->main_vsi_seid);
  10014. if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
  10015. (pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
  10016. pf->flags |= I40E_FLAG_PHY_CONTROLS_LEDS;
  10017. if (pf->hw.device_id == I40E_DEV_ID_SFP_I_X722)
  10018. pf->flags |= I40E_FLAG_HAVE_CRT_RETIMER;
  10019. /* print a string summarizing features */
  10020. i40e_print_features(pf);
  10021. return 0;
  10022. /* Unwind what we've done if something failed in the setup */
  10023. err_vsis:
  10024. set_bit(__I40E_DOWN, &pf->state);
  10025. i40e_clear_interrupt_scheme(pf);
  10026. kfree(pf->vsi);
  10027. err_switch_setup:
  10028. i40e_reset_interrupt_capability(pf);
  10029. del_timer_sync(&pf->service_timer);
  10030. err_mac_addr:
  10031. err_configure_lan_hmc:
  10032. (void)i40e_shutdown_lan_hmc(hw);
  10033. err_init_lan_hmc:
  10034. kfree(pf->qp_pile);
  10035. err_sw_init:
  10036. err_adminq_setup:
  10037. err_pf_reset:
  10038. iounmap(hw->hw_addr);
  10039. err_ioremap:
  10040. kfree(pf);
  10041. err_pf_alloc:
  10042. pci_disable_pcie_error_reporting(pdev);
  10043. pci_release_mem_regions(pdev);
  10044. err_pci_reg:
  10045. err_dma:
  10046. pci_disable_device(pdev);
  10047. return err;
  10048. }
  10049. /**
  10050. * i40e_remove - Device removal routine
  10051. * @pdev: PCI device information struct
  10052. *
  10053. * i40e_remove is called by the PCI subsystem to alert the driver
  10054. * that is should release a PCI device. This could be caused by a
  10055. * Hot-Plug event, or because the driver is going to be removed from
  10056. * memory.
  10057. **/
  10058. static void i40e_remove(struct pci_dev *pdev)
  10059. {
  10060. struct i40e_pf *pf = pci_get_drvdata(pdev);
  10061. struct i40e_hw *hw = &pf->hw;
  10062. i40e_status ret_code;
  10063. int i;
  10064. i40e_dbg_pf_exit(pf);
  10065. i40e_ptp_stop(pf);
  10066. /* Disable RSS in hw */
  10067. i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
  10068. i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
  10069. /* no more scheduling of any task */
  10070. set_bit(__I40E_SUSPENDED, &pf->state);
  10071. set_bit(__I40E_DOWN, &pf->state);
  10072. if (pf->service_timer.data)
  10073. del_timer_sync(&pf->service_timer);
  10074. if (pf->service_task.func)
  10075. cancel_work_sync(&pf->service_task);
  10076. /* Client close must be called explicitly here because the timer
  10077. * has been stopped.
  10078. */
  10079. i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
  10080. if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
  10081. i40e_free_vfs(pf);
  10082. pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
  10083. }
  10084. i40e_fdir_teardown(pf);
  10085. /* If there is a switch structure or any orphans, remove them.
  10086. * This will leave only the PF's VSI remaining.
  10087. */
  10088. for (i = 0; i < I40E_MAX_VEB; i++) {
  10089. if (!pf->veb[i])
  10090. continue;
  10091. if (pf->veb[i]->uplink_seid == pf->mac_seid ||
  10092. pf->veb[i]->uplink_seid == 0)
  10093. i40e_switch_branch_release(pf->veb[i]);
  10094. }
  10095. /* Now we can shutdown the PF's VSI, just before we kill
  10096. * adminq and hmc.
  10097. */
  10098. if (pf->vsi[pf->lan_vsi])
  10099. i40e_vsi_release(pf->vsi[pf->lan_vsi]);
  10100. /* remove attached clients */
  10101. if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
  10102. ret_code = i40e_lan_del_device(pf);
  10103. if (ret_code)
  10104. dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
  10105. ret_code);
  10106. }
  10107. /* shutdown and destroy the HMC */
  10108. if (hw->hmc.hmc_obj) {
  10109. ret_code = i40e_shutdown_lan_hmc(hw);
  10110. if (ret_code)
  10111. dev_warn(&pdev->dev,
  10112. "Failed to destroy the HMC resources: %d\n",
  10113. ret_code);
  10114. }
  10115. /* shutdown the adminq */
  10116. i40e_shutdown_adminq(hw);
  10117. /* destroy the locks only once, here */
  10118. mutex_destroy(&hw->aq.arq_mutex);
  10119. mutex_destroy(&hw->aq.asq_mutex);
  10120. /* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
  10121. i40e_clear_interrupt_scheme(pf);
  10122. for (i = 0; i < pf->num_alloc_vsi; i++) {
  10123. if (pf->vsi[i]) {
  10124. i40e_vsi_clear_rings(pf->vsi[i]);
  10125. i40e_vsi_clear(pf->vsi[i]);
  10126. pf->vsi[i] = NULL;
  10127. }
  10128. }
  10129. for (i = 0; i < I40E_MAX_VEB; i++) {
  10130. kfree(pf->veb[i]);
  10131. pf->veb[i] = NULL;
  10132. }
  10133. kfree(pf->qp_pile);
  10134. kfree(pf->vsi);
  10135. iounmap(hw->hw_addr);
  10136. kfree(pf);
  10137. pci_release_mem_regions(pdev);
  10138. pci_disable_pcie_error_reporting(pdev);
  10139. pci_disable_device(pdev);
  10140. }
  10141. /**
  10142. * i40e_pci_error_detected - warning that something funky happened in PCI land
  10143. * @pdev: PCI device information struct
  10144. *
  10145. * Called to warn that something happened and the error handling steps
  10146. * are in progress. Allows the driver to quiesce things, be ready for
  10147. * remediation.
  10148. **/
  10149. static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
  10150. enum pci_channel_state error)
  10151. {
  10152. struct i40e_pf *pf = pci_get_drvdata(pdev);
  10153. dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
  10154. if (!pf) {
  10155. dev_info(&pdev->dev,
  10156. "Cannot recover - error happened during device probe\n");
  10157. return PCI_ERS_RESULT_DISCONNECT;
  10158. }
  10159. /* shutdown all operations */
  10160. if (!test_bit(__I40E_SUSPENDED, &pf->state)) {
  10161. rtnl_lock();
  10162. i40e_prep_for_reset(pf, true);
  10163. rtnl_unlock();
  10164. }
  10165. /* Request a slot reset */
  10166. return PCI_ERS_RESULT_NEED_RESET;
  10167. }
  10168. /**
  10169. * i40e_pci_error_slot_reset - a PCI slot reset just happened
  10170. * @pdev: PCI device information struct
  10171. *
  10172. * Called to find if the driver can work with the device now that
  10173. * the pci slot has been reset. If a basic connection seems good
  10174. * (registers are readable and have sane content) then return a
  10175. * happy little PCI_ERS_RESULT_xxx.
  10176. **/
  10177. static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
  10178. {
  10179. struct i40e_pf *pf = pci_get_drvdata(pdev);
  10180. pci_ers_result_t result;
  10181. int err;
  10182. u32 reg;
  10183. dev_dbg(&pdev->dev, "%s\n", __func__);
  10184. if (pci_enable_device_mem(pdev)) {
  10185. dev_info(&pdev->dev,
  10186. "Cannot re-enable PCI device after reset.\n");
  10187. result = PCI_ERS_RESULT_DISCONNECT;
  10188. } else {
  10189. pci_set_master(pdev);
  10190. pci_restore_state(pdev);
  10191. pci_save_state(pdev);
  10192. pci_wake_from_d3(pdev, false);
  10193. reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
  10194. if (reg == 0)
  10195. result = PCI_ERS_RESULT_RECOVERED;
  10196. else
  10197. result = PCI_ERS_RESULT_DISCONNECT;
  10198. }
  10199. err = pci_cleanup_aer_uncorrect_error_status(pdev);
  10200. if (err) {
  10201. dev_info(&pdev->dev,
  10202. "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
  10203. err);
  10204. /* non-fatal, continue */
  10205. }
  10206. return result;
  10207. }
  10208. /**
  10209. * i40e_pci_error_resume - restart operations after PCI error recovery
  10210. * @pdev: PCI device information struct
  10211. *
  10212. * Called to allow the driver to bring things back up after PCI error
  10213. * and/or reset recovery has finished.
  10214. **/
  10215. static void i40e_pci_error_resume(struct pci_dev *pdev)
  10216. {
  10217. struct i40e_pf *pf = pci_get_drvdata(pdev);
  10218. dev_dbg(&pdev->dev, "%s\n", __func__);
  10219. if (test_bit(__I40E_SUSPENDED, &pf->state))
  10220. return;
  10221. rtnl_lock();
  10222. i40e_handle_reset_warning(pf, true);
  10223. rtnl_unlock();
  10224. }
  10225. /**
  10226. * i40e_enable_mc_magic_wake - enable multicast magic packet wake up
  10227. * using the mac_address_write admin q function
  10228. * @pf: pointer to i40e_pf struct
  10229. **/
  10230. static void i40e_enable_mc_magic_wake(struct i40e_pf *pf)
  10231. {
  10232. struct i40e_hw *hw = &pf->hw;
  10233. i40e_status ret;
  10234. u8 mac_addr[6];
  10235. u16 flags = 0;
  10236. /* Get current MAC address in case it's an LAA */
  10237. if (pf->vsi[pf->lan_vsi] && pf->vsi[pf->lan_vsi]->netdev) {
  10238. ether_addr_copy(mac_addr,
  10239. pf->vsi[pf->lan_vsi]->netdev->dev_addr);
  10240. } else {
  10241. dev_err(&pf->pdev->dev,
  10242. "Failed to retrieve MAC address; using default\n");
  10243. ether_addr_copy(mac_addr, hw->mac.addr);
  10244. }
  10245. /* The FW expects the mac address write cmd to first be called with
  10246. * one of these flags before calling it again with the multicast
  10247. * enable flags.
  10248. */
  10249. flags = I40E_AQC_WRITE_TYPE_LAA_WOL;
  10250. if (hw->func_caps.flex10_enable && hw->partition_id != 1)
  10251. flags = I40E_AQC_WRITE_TYPE_LAA_ONLY;
  10252. ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
  10253. if (ret) {
  10254. dev_err(&pf->pdev->dev,
  10255. "Failed to update MAC address registers; cannot enable Multicast Magic packet wake up");
  10256. return;
  10257. }
  10258. flags = I40E_AQC_MC_MAG_EN
  10259. | I40E_AQC_WOL_PRESERVE_ON_PFR
  10260. | I40E_AQC_WRITE_TYPE_UPDATE_MC_MAG;
  10261. ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
  10262. if (ret)
  10263. dev_err(&pf->pdev->dev,
  10264. "Failed to enable Multicast Magic Packet wake up\n");
  10265. }
  10266. /**
  10267. * i40e_shutdown - PCI callback for shutting down
  10268. * @pdev: PCI device information struct
  10269. **/
  10270. static void i40e_shutdown(struct pci_dev *pdev)
  10271. {
  10272. struct i40e_pf *pf = pci_get_drvdata(pdev);
  10273. struct i40e_hw *hw = &pf->hw;
  10274. set_bit(__I40E_SUSPENDED, &pf->state);
  10275. set_bit(__I40E_DOWN, &pf->state);
  10276. rtnl_lock();
  10277. i40e_prep_for_reset(pf, true);
  10278. rtnl_unlock();
  10279. wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
  10280. wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
  10281. del_timer_sync(&pf->service_timer);
  10282. cancel_work_sync(&pf->service_task);
  10283. i40e_fdir_teardown(pf);
  10284. /* Client close must be called explicitly here because the timer
  10285. * has been stopped.
  10286. */
  10287. i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
  10288. if (pf->wol_en && (pf->flags & I40E_FLAG_WOL_MC_MAGIC_PKT_WAKE))
  10289. i40e_enable_mc_magic_wake(pf);
  10290. rtnl_lock();
  10291. i40e_prep_for_reset(pf, true);
  10292. rtnl_unlock();
  10293. wr32(hw, I40E_PFPM_APM,
  10294. (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
  10295. wr32(hw, I40E_PFPM_WUFC,
  10296. (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
  10297. i40e_clear_interrupt_scheme(pf);
  10298. if (system_state == SYSTEM_POWER_OFF) {
  10299. pci_wake_from_d3(pdev, pf->wol_en);
  10300. pci_set_power_state(pdev, PCI_D3hot);
  10301. }
  10302. }
  10303. #ifdef CONFIG_PM
  10304. /**
  10305. * i40e_suspend - PCI callback for moving to D3
  10306. * @pdev: PCI device information struct
  10307. **/
  10308. static int i40e_suspend(struct pci_dev *pdev, pm_message_t state)
  10309. {
  10310. struct i40e_pf *pf = pci_get_drvdata(pdev);
  10311. struct i40e_hw *hw = &pf->hw;
  10312. int retval = 0;
  10313. set_bit(__I40E_SUSPENDED, &pf->state);
  10314. set_bit(__I40E_DOWN, &pf->state);
  10315. if (pf->wol_en && (pf->flags & I40E_FLAG_WOL_MC_MAGIC_PKT_WAKE))
  10316. i40e_enable_mc_magic_wake(pf);
  10317. rtnl_lock();
  10318. i40e_prep_for_reset(pf, true);
  10319. rtnl_unlock();
  10320. wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
  10321. wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
  10322. i40e_stop_misc_vector(pf);
  10323. retval = pci_save_state(pdev);
  10324. if (retval)
  10325. return retval;
  10326. pci_wake_from_d3(pdev, pf->wol_en);
  10327. pci_set_power_state(pdev, PCI_D3hot);
  10328. return retval;
  10329. }
  10330. /**
  10331. * i40e_resume - PCI callback for waking up from D3
  10332. * @pdev: PCI device information struct
  10333. **/
  10334. static int i40e_resume(struct pci_dev *pdev)
  10335. {
  10336. struct i40e_pf *pf = pci_get_drvdata(pdev);
  10337. u32 err;
  10338. pci_set_power_state(pdev, PCI_D0);
  10339. pci_restore_state(pdev);
  10340. /* pci_restore_state() clears dev->state_saves, so
  10341. * call pci_save_state() again to restore it.
  10342. */
  10343. pci_save_state(pdev);
  10344. err = pci_enable_device_mem(pdev);
  10345. if (err) {
  10346. dev_err(&pdev->dev, "Cannot enable PCI device from suspend\n");
  10347. return err;
  10348. }
  10349. pci_set_master(pdev);
  10350. /* no wakeup events while running */
  10351. pci_wake_from_d3(pdev, false);
  10352. /* handling the reset will rebuild the device state */
  10353. if (test_and_clear_bit(__I40E_SUSPENDED, &pf->state)) {
  10354. clear_bit(__I40E_DOWN, &pf->state);
  10355. rtnl_lock();
  10356. i40e_reset_and_rebuild(pf, false, true);
  10357. rtnl_unlock();
  10358. }
  10359. return 0;
  10360. }
  10361. #endif
  10362. static const struct pci_error_handlers i40e_err_handler = {
  10363. .error_detected = i40e_pci_error_detected,
  10364. .slot_reset = i40e_pci_error_slot_reset,
  10365. .resume = i40e_pci_error_resume,
  10366. };
  10367. static struct pci_driver i40e_driver = {
  10368. .name = i40e_driver_name,
  10369. .id_table = i40e_pci_tbl,
  10370. .probe = i40e_probe,
  10371. .remove = i40e_remove,
  10372. #ifdef CONFIG_PM
  10373. .suspend = i40e_suspend,
  10374. .resume = i40e_resume,
  10375. #endif
  10376. .shutdown = i40e_shutdown,
  10377. .err_handler = &i40e_err_handler,
  10378. .sriov_configure = i40e_pci_sriov_configure,
  10379. };
  10380. /**
  10381. * i40e_init_module - Driver registration routine
  10382. *
  10383. * i40e_init_module is the first routine called when the driver is
  10384. * loaded. All it does is register with the PCI subsystem.
  10385. **/
  10386. static int __init i40e_init_module(void)
  10387. {
  10388. pr_info("%s: %s - version %s\n", i40e_driver_name,
  10389. i40e_driver_string, i40e_driver_version_str);
  10390. pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
  10391. /* we will see if single thread per module is enough for now,
  10392. * it can't be any worse than using the system workqueue which
  10393. * was already single threaded
  10394. */
  10395. i40e_wq = alloc_workqueue("%s", WQ_UNBOUND | WQ_MEM_RECLAIM, 1,
  10396. i40e_driver_name);
  10397. if (!i40e_wq) {
  10398. pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
  10399. return -ENOMEM;
  10400. }
  10401. i40e_dbg_init();
  10402. return pci_register_driver(&i40e_driver);
  10403. }
  10404. module_init(i40e_init_module);
  10405. /**
  10406. * i40e_exit_module - Driver exit cleanup routine
  10407. *
  10408. * i40e_exit_module is called just before the driver is removed
  10409. * from memory.
  10410. **/
  10411. static void __exit i40e_exit_module(void)
  10412. {
  10413. pci_unregister_driver(&i40e_driver);
  10414. destroy_workqueue(i40e_wq);
  10415. i40e_dbg_exit();
  10416. }
  10417. module_exit(i40e_exit_module);