i40e_main.c 325 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778
  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. const char i40e_driver_name[] = "i40e";
  34. static const char i40e_driver_string[] =
  35. "Intel(R) Ethernet Connection XL710 Network Driver";
  36. #define DRV_KERN "-k"
  37. #define DRV_VERSION_MAJOR 2
  38. #define DRV_VERSION_MINOR 1
  39. #define DRV_VERSION_BUILD 7
  40. #define DRV_VERSION __stringify(DRV_VERSION_MAJOR) "." \
  41. __stringify(DRV_VERSION_MINOR) "." \
  42. __stringify(DRV_VERSION_BUILD) DRV_KERN
  43. const char i40e_driver_version_str[] = DRV_VERSION;
  44. static const char i40e_copyright[] = "Copyright (c) 2013 - 2014 Intel Corporation.";
  45. /* a bit of forward declarations */
  46. static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi);
  47. static void i40e_handle_reset_warning(struct i40e_pf *pf);
  48. static int i40e_add_vsi(struct i40e_vsi *vsi);
  49. static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi);
  50. static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit);
  51. static int i40e_setup_misc_vector(struct i40e_pf *pf);
  52. static void i40e_determine_queue_usage(struct i40e_pf *pf);
  53. static int i40e_setup_pf_filter_control(struct i40e_pf *pf);
  54. static void i40e_fdir_sb_setup(struct i40e_pf *pf);
  55. static int i40e_veb_get_bw_info(struct i40e_veb *veb);
  56. /* i40e_pci_tbl - PCI Device ID Table
  57. *
  58. * Last entry must be all 0s
  59. *
  60. * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
  61. * Class, Class Mask, private data (not used) }
  62. */
  63. static const struct pci_device_id i40e_pci_tbl[] = {
  64. {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_XL710), 0},
  65. {PCI_VDEVICE(INTEL, I40E_DEV_ID_QEMU), 0},
  66. {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_B), 0},
  67. {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_C), 0},
  68. {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_A), 0},
  69. {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_B), 0},
  70. {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_C), 0},
  71. {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T), 0},
  72. {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T4), 0},
  73. {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_X722), 0},
  74. {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_X722), 0},
  75. {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_X722), 0},
  76. {PCI_VDEVICE(INTEL, I40E_DEV_ID_1G_BASE_T_X722), 0},
  77. {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_X722), 0},
  78. {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_I_X722), 0},
  79. {PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2), 0},
  80. {PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2_A), 0},
  81. {PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_B), 0},
  82. {PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_SFP28), 0},
  83. /* required last entry */
  84. {0, }
  85. };
  86. MODULE_DEVICE_TABLE(pci, i40e_pci_tbl);
  87. #define I40E_MAX_VF_COUNT 128
  88. static int debug = -1;
  89. module_param(debug, uint, 0);
  90. MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all), Debug mask (0x8XXXXXXX)");
  91. MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
  92. MODULE_DESCRIPTION("Intel(R) Ethernet Connection XL710 Network Driver");
  93. MODULE_LICENSE("GPL");
  94. MODULE_VERSION(DRV_VERSION);
  95. static struct workqueue_struct *i40e_wq;
  96. /**
  97. * i40e_allocate_dma_mem_d - OS specific memory alloc for shared code
  98. * @hw: pointer to the HW structure
  99. * @mem: ptr to mem struct to fill out
  100. * @size: size of memory requested
  101. * @alignment: what to align the allocation to
  102. **/
  103. int i40e_allocate_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem,
  104. u64 size, u32 alignment)
  105. {
  106. struct i40e_pf *pf = (struct i40e_pf *)hw->back;
  107. mem->size = ALIGN(size, alignment);
  108. mem->va = dma_zalloc_coherent(&pf->pdev->dev, mem->size,
  109. &mem->pa, GFP_KERNEL);
  110. if (!mem->va)
  111. return -ENOMEM;
  112. return 0;
  113. }
  114. /**
  115. * i40e_free_dma_mem_d - OS specific memory free for shared code
  116. * @hw: pointer to the HW structure
  117. * @mem: ptr to mem struct to free
  118. **/
  119. int i40e_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
  120. {
  121. struct i40e_pf *pf = (struct i40e_pf *)hw->back;
  122. dma_free_coherent(&pf->pdev->dev, mem->size, mem->va, mem->pa);
  123. mem->va = NULL;
  124. mem->pa = 0;
  125. mem->size = 0;
  126. return 0;
  127. }
  128. /**
  129. * i40e_allocate_virt_mem_d - OS specific memory alloc for shared code
  130. * @hw: pointer to the HW structure
  131. * @mem: ptr to mem struct to fill out
  132. * @size: size of memory requested
  133. **/
  134. int i40e_allocate_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem,
  135. u32 size)
  136. {
  137. mem->size = size;
  138. mem->va = kzalloc(size, GFP_KERNEL);
  139. if (!mem->va)
  140. return -ENOMEM;
  141. return 0;
  142. }
  143. /**
  144. * i40e_free_virt_mem_d - OS specific memory free for shared code
  145. * @hw: pointer to the HW structure
  146. * @mem: ptr to mem struct to free
  147. **/
  148. int i40e_free_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem)
  149. {
  150. /* it's ok to kfree a NULL pointer */
  151. kfree(mem->va);
  152. mem->va = NULL;
  153. mem->size = 0;
  154. return 0;
  155. }
  156. /**
  157. * i40e_get_lump - find a lump of free generic resource
  158. * @pf: board private structure
  159. * @pile: the pile of resource to search
  160. * @needed: the number of items needed
  161. * @id: an owner id to stick on the items assigned
  162. *
  163. * Returns the base item index of the lump, or negative for error
  164. *
  165. * The search_hint trick and lack of advanced fit-finding only work
  166. * because we're highly likely to have all the same size lump requests.
  167. * Linear search time and any fragmentation should be minimal.
  168. **/
  169. static int i40e_get_lump(struct i40e_pf *pf, struct i40e_lump_tracking *pile,
  170. u16 needed, u16 id)
  171. {
  172. int ret = -ENOMEM;
  173. int i, j;
  174. if (!pile || needed == 0 || id >= I40E_PILE_VALID_BIT) {
  175. dev_info(&pf->pdev->dev,
  176. "param err: pile=%p needed=%d id=0x%04x\n",
  177. pile, needed, id);
  178. return -EINVAL;
  179. }
  180. /* start the linear search with an imperfect hint */
  181. i = pile->search_hint;
  182. while (i < pile->num_entries) {
  183. /* skip already allocated entries */
  184. if (pile->list[i] & I40E_PILE_VALID_BIT) {
  185. i++;
  186. continue;
  187. }
  188. /* do we have enough in this lump? */
  189. for (j = 0; (j < needed) && ((i+j) < pile->num_entries); j++) {
  190. if (pile->list[i+j] & I40E_PILE_VALID_BIT)
  191. break;
  192. }
  193. if (j == needed) {
  194. /* there was enough, so assign it to the requestor */
  195. for (j = 0; j < needed; j++)
  196. pile->list[i+j] = id | I40E_PILE_VALID_BIT;
  197. ret = i;
  198. pile->search_hint = i + j;
  199. break;
  200. }
  201. /* not enough, so skip over it and continue looking */
  202. i += j;
  203. }
  204. return ret;
  205. }
  206. /**
  207. * i40e_put_lump - return a lump of generic resource
  208. * @pile: the pile of resource to search
  209. * @index: the base item index
  210. * @id: the owner id of the items assigned
  211. *
  212. * Returns the count of items in the lump
  213. **/
  214. static int i40e_put_lump(struct i40e_lump_tracking *pile, u16 index, u16 id)
  215. {
  216. int valid_id = (id | I40E_PILE_VALID_BIT);
  217. int count = 0;
  218. int i;
  219. if (!pile || index >= pile->num_entries)
  220. return -EINVAL;
  221. for (i = index;
  222. i < pile->num_entries && pile->list[i] == valid_id;
  223. i++) {
  224. pile->list[i] = 0;
  225. count++;
  226. }
  227. if (count && index < pile->search_hint)
  228. pile->search_hint = index;
  229. return count;
  230. }
  231. /**
  232. * i40e_find_vsi_from_id - searches for the vsi with the given id
  233. * @pf - the pf structure to search for the vsi
  234. * @id - id of the vsi it is searching for
  235. **/
  236. struct i40e_vsi *i40e_find_vsi_from_id(struct i40e_pf *pf, u16 id)
  237. {
  238. int i;
  239. for (i = 0; i < pf->num_alloc_vsi; i++)
  240. if (pf->vsi[i] && (pf->vsi[i]->id == id))
  241. return pf->vsi[i];
  242. return NULL;
  243. }
  244. /**
  245. * i40e_service_event_schedule - Schedule the service task to wake up
  246. * @pf: board private structure
  247. *
  248. * If not already scheduled, this puts the task into the work queue
  249. **/
  250. void i40e_service_event_schedule(struct i40e_pf *pf)
  251. {
  252. if (!test_bit(__I40E_DOWN, &pf->state) &&
  253. !test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
  254. queue_work(i40e_wq, &pf->service_task);
  255. }
  256. /**
  257. * i40e_tx_timeout - Respond to a Tx Hang
  258. * @netdev: network interface device structure
  259. *
  260. * If any port has noticed a Tx timeout, it is likely that the whole
  261. * device is munged, not just the one netdev port, so go for the full
  262. * reset.
  263. **/
  264. static void i40e_tx_timeout(struct net_device *netdev)
  265. {
  266. struct i40e_netdev_priv *np = netdev_priv(netdev);
  267. struct i40e_vsi *vsi = np->vsi;
  268. struct i40e_pf *pf = vsi->back;
  269. struct i40e_ring *tx_ring = NULL;
  270. unsigned int i, hung_queue = 0;
  271. u32 head, val;
  272. pf->tx_timeout_count++;
  273. /* find the stopped queue the same way the stack does */
  274. for (i = 0; i < netdev->num_tx_queues; i++) {
  275. struct netdev_queue *q;
  276. unsigned long trans_start;
  277. q = netdev_get_tx_queue(netdev, i);
  278. trans_start = q->trans_start;
  279. if (netif_xmit_stopped(q) &&
  280. time_after(jiffies,
  281. (trans_start + netdev->watchdog_timeo))) {
  282. hung_queue = i;
  283. break;
  284. }
  285. }
  286. if (i == netdev->num_tx_queues) {
  287. netdev_info(netdev, "tx_timeout: no netdev hung queue found\n");
  288. } else {
  289. /* now that we have an index, find the tx_ring struct */
  290. for (i = 0; i < vsi->num_queue_pairs; i++) {
  291. if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
  292. if (hung_queue ==
  293. vsi->tx_rings[i]->queue_index) {
  294. tx_ring = vsi->tx_rings[i];
  295. break;
  296. }
  297. }
  298. }
  299. }
  300. if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ*20)))
  301. pf->tx_timeout_recovery_level = 1; /* reset after some time */
  302. else if (time_before(jiffies,
  303. (pf->tx_timeout_last_recovery + netdev->watchdog_timeo)))
  304. return; /* don't do any new action before the next timeout */
  305. if (tx_ring) {
  306. head = i40e_get_head(tx_ring);
  307. /* Read interrupt register */
  308. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  309. val = rd32(&pf->hw,
  310. I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
  311. tx_ring->vsi->base_vector - 1));
  312. else
  313. val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
  314. 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",
  315. vsi->seid, hung_queue, tx_ring->next_to_clean,
  316. head, tx_ring->next_to_use,
  317. readl(tx_ring->tail), val);
  318. }
  319. pf->tx_timeout_last_recovery = jiffies;
  320. netdev_info(netdev, "tx_timeout recovery level %d, hung_queue %d\n",
  321. pf->tx_timeout_recovery_level, hung_queue);
  322. switch (pf->tx_timeout_recovery_level) {
  323. case 1:
  324. set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
  325. break;
  326. case 2:
  327. set_bit(__I40E_CORE_RESET_REQUESTED, &pf->state);
  328. break;
  329. case 3:
  330. set_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state);
  331. break;
  332. default:
  333. netdev_err(netdev, "tx_timeout recovery unsuccessful\n");
  334. break;
  335. }
  336. i40e_service_event_schedule(pf);
  337. pf->tx_timeout_recovery_level++;
  338. }
  339. /**
  340. * i40e_get_vsi_stats_struct - Get System Network Statistics
  341. * @vsi: the VSI we care about
  342. *
  343. * Returns the address of the device statistics structure.
  344. * The statistics are actually updated from the service task.
  345. **/
  346. struct rtnl_link_stats64 *i40e_get_vsi_stats_struct(struct i40e_vsi *vsi)
  347. {
  348. return &vsi->net_stats;
  349. }
  350. /**
  351. * i40e_get_netdev_stats_struct - Get statistics for netdev interface
  352. * @netdev: network interface device structure
  353. *
  354. * Returns the address of the device statistics structure.
  355. * The statistics are actually updated from the service task.
  356. **/
  357. static void i40e_get_netdev_stats_struct(struct net_device *netdev,
  358. struct rtnl_link_stats64 *stats)
  359. {
  360. struct i40e_netdev_priv *np = netdev_priv(netdev);
  361. struct i40e_ring *tx_ring, *rx_ring;
  362. struct i40e_vsi *vsi = np->vsi;
  363. struct rtnl_link_stats64 *vsi_stats = i40e_get_vsi_stats_struct(vsi);
  364. int i;
  365. if (test_bit(__I40E_DOWN, &vsi->state))
  366. return;
  367. if (!vsi->tx_rings)
  368. return;
  369. rcu_read_lock();
  370. for (i = 0; i < vsi->num_queue_pairs; i++) {
  371. u64 bytes, packets;
  372. unsigned int start;
  373. tx_ring = ACCESS_ONCE(vsi->tx_rings[i]);
  374. if (!tx_ring)
  375. continue;
  376. do {
  377. start = u64_stats_fetch_begin_irq(&tx_ring->syncp);
  378. packets = tx_ring->stats.packets;
  379. bytes = tx_ring->stats.bytes;
  380. } while (u64_stats_fetch_retry_irq(&tx_ring->syncp, start));
  381. stats->tx_packets += packets;
  382. stats->tx_bytes += bytes;
  383. rx_ring = &tx_ring[1];
  384. do {
  385. start = u64_stats_fetch_begin_irq(&rx_ring->syncp);
  386. packets = rx_ring->stats.packets;
  387. bytes = rx_ring->stats.bytes;
  388. } while (u64_stats_fetch_retry_irq(&rx_ring->syncp, start));
  389. stats->rx_packets += packets;
  390. stats->rx_bytes += bytes;
  391. }
  392. rcu_read_unlock();
  393. /* following stats updated by i40e_watchdog_subtask() */
  394. stats->multicast = vsi_stats->multicast;
  395. stats->tx_errors = vsi_stats->tx_errors;
  396. stats->tx_dropped = vsi_stats->tx_dropped;
  397. stats->rx_errors = vsi_stats->rx_errors;
  398. stats->rx_dropped = vsi_stats->rx_dropped;
  399. stats->rx_crc_errors = vsi_stats->rx_crc_errors;
  400. stats->rx_length_errors = vsi_stats->rx_length_errors;
  401. }
  402. /**
  403. * i40e_vsi_reset_stats - Resets all stats of the given vsi
  404. * @vsi: the VSI to have its stats reset
  405. **/
  406. void i40e_vsi_reset_stats(struct i40e_vsi *vsi)
  407. {
  408. struct rtnl_link_stats64 *ns;
  409. int i;
  410. if (!vsi)
  411. return;
  412. ns = i40e_get_vsi_stats_struct(vsi);
  413. memset(ns, 0, sizeof(*ns));
  414. memset(&vsi->net_stats_offsets, 0, sizeof(vsi->net_stats_offsets));
  415. memset(&vsi->eth_stats, 0, sizeof(vsi->eth_stats));
  416. memset(&vsi->eth_stats_offsets, 0, sizeof(vsi->eth_stats_offsets));
  417. if (vsi->rx_rings && vsi->rx_rings[0]) {
  418. for (i = 0; i < vsi->num_queue_pairs; i++) {
  419. memset(&vsi->rx_rings[i]->stats, 0,
  420. sizeof(vsi->rx_rings[i]->stats));
  421. memset(&vsi->rx_rings[i]->rx_stats, 0,
  422. sizeof(vsi->rx_rings[i]->rx_stats));
  423. memset(&vsi->tx_rings[i]->stats, 0,
  424. sizeof(vsi->tx_rings[i]->stats));
  425. memset(&vsi->tx_rings[i]->tx_stats, 0,
  426. sizeof(vsi->tx_rings[i]->tx_stats));
  427. }
  428. }
  429. vsi->stat_offsets_loaded = false;
  430. }
  431. /**
  432. * i40e_pf_reset_stats - Reset all of the stats for the given PF
  433. * @pf: the PF to be reset
  434. **/
  435. void i40e_pf_reset_stats(struct i40e_pf *pf)
  436. {
  437. int i;
  438. memset(&pf->stats, 0, sizeof(pf->stats));
  439. memset(&pf->stats_offsets, 0, sizeof(pf->stats_offsets));
  440. pf->stat_offsets_loaded = false;
  441. for (i = 0; i < I40E_MAX_VEB; i++) {
  442. if (pf->veb[i]) {
  443. memset(&pf->veb[i]->stats, 0,
  444. sizeof(pf->veb[i]->stats));
  445. memset(&pf->veb[i]->stats_offsets, 0,
  446. sizeof(pf->veb[i]->stats_offsets));
  447. pf->veb[i]->stat_offsets_loaded = false;
  448. }
  449. }
  450. pf->hw_csum_rx_error = 0;
  451. }
  452. /**
  453. * i40e_stat_update48 - read and update a 48 bit stat from the chip
  454. * @hw: ptr to the hardware info
  455. * @hireg: the high 32 bit reg to read
  456. * @loreg: the low 32 bit reg to read
  457. * @offset_loaded: has the initial offset been loaded yet
  458. * @offset: ptr to current offset value
  459. * @stat: ptr to the stat
  460. *
  461. * Since the device stats are not reset at PFReset, they likely will not
  462. * be zeroed when the driver starts. We'll save the first values read
  463. * and use them as offsets to be subtracted from the raw values in order
  464. * to report stats that count from zero. In the process, we also manage
  465. * the potential roll-over.
  466. **/
  467. static void i40e_stat_update48(struct i40e_hw *hw, u32 hireg, u32 loreg,
  468. bool offset_loaded, u64 *offset, u64 *stat)
  469. {
  470. u64 new_data;
  471. if (hw->device_id == I40E_DEV_ID_QEMU) {
  472. new_data = rd32(hw, loreg);
  473. new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
  474. } else {
  475. new_data = rd64(hw, loreg);
  476. }
  477. if (!offset_loaded)
  478. *offset = new_data;
  479. if (likely(new_data >= *offset))
  480. *stat = new_data - *offset;
  481. else
  482. *stat = (new_data + BIT_ULL(48)) - *offset;
  483. *stat &= 0xFFFFFFFFFFFFULL;
  484. }
  485. /**
  486. * i40e_stat_update32 - read and update a 32 bit stat from the chip
  487. * @hw: ptr to the hardware info
  488. * @reg: the hw reg to read
  489. * @offset_loaded: has the initial offset been loaded yet
  490. * @offset: ptr to current offset value
  491. * @stat: ptr to the stat
  492. **/
  493. static void i40e_stat_update32(struct i40e_hw *hw, u32 reg,
  494. bool offset_loaded, u64 *offset, u64 *stat)
  495. {
  496. u32 new_data;
  497. new_data = rd32(hw, reg);
  498. if (!offset_loaded)
  499. *offset = new_data;
  500. if (likely(new_data >= *offset))
  501. *stat = (u32)(new_data - *offset);
  502. else
  503. *stat = (u32)((new_data + BIT_ULL(32)) - *offset);
  504. }
  505. /**
  506. * i40e_update_eth_stats - Update VSI-specific ethernet statistics counters.
  507. * @vsi: the VSI to be updated
  508. **/
  509. void i40e_update_eth_stats(struct i40e_vsi *vsi)
  510. {
  511. int stat_idx = le16_to_cpu(vsi->info.stat_counter_idx);
  512. struct i40e_pf *pf = vsi->back;
  513. struct i40e_hw *hw = &pf->hw;
  514. struct i40e_eth_stats *oes;
  515. struct i40e_eth_stats *es; /* device's eth stats */
  516. es = &vsi->eth_stats;
  517. oes = &vsi->eth_stats_offsets;
  518. /* Gather up the stats that the hw collects */
  519. i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
  520. vsi->stat_offsets_loaded,
  521. &oes->tx_errors, &es->tx_errors);
  522. i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx),
  523. vsi->stat_offsets_loaded,
  524. &oes->rx_discards, &es->rx_discards);
  525. i40e_stat_update32(hw, I40E_GLV_RUPP(stat_idx),
  526. vsi->stat_offsets_loaded,
  527. &oes->rx_unknown_protocol, &es->rx_unknown_protocol);
  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_update48(hw, I40E_GLV_GORCH(stat_idx),
  532. I40E_GLV_GORCL(stat_idx),
  533. vsi->stat_offsets_loaded,
  534. &oes->rx_bytes, &es->rx_bytes);
  535. i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
  536. I40E_GLV_UPRCL(stat_idx),
  537. vsi->stat_offsets_loaded,
  538. &oes->rx_unicast, &es->rx_unicast);
  539. i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
  540. I40E_GLV_MPRCL(stat_idx),
  541. vsi->stat_offsets_loaded,
  542. &oes->rx_multicast, &es->rx_multicast);
  543. i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
  544. I40E_GLV_BPRCL(stat_idx),
  545. vsi->stat_offsets_loaded,
  546. &oes->rx_broadcast, &es->rx_broadcast);
  547. i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
  548. I40E_GLV_GOTCL(stat_idx),
  549. vsi->stat_offsets_loaded,
  550. &oes->tx_bytes, &es->tx_bytes);
  551. i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
  552. I40E_GLV_UPTCL(stat_idx),
  553. vsi->stat_offsets_loaded,
  554. &oes->tx_unicast, &es->tx_unicast);
  555. i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
  556. I40E_GLV_MPTCL(stat_idx),
  557. vsi->stat_offsets_loaded,
  558. &oes->tx_multicast, &es->tx_multicast);
  559. i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
  560. I40E_GLV_BPTCL(stat_idx),
  561. vsi->stat_offsets_loaded,
  562. &oes->tx_broadcast, &es->tx_broadcast);
  563. vsi->stat_offsets_loaded = true;
  564. }
  565. /**
  566. * i40e_update_veb_stats - Update Switch component statistics
  567. * @veb: the VEB being updated
  568. **/
  569. static void i40e_update_veb_stats(struct i40e_veb *veb)
  570. {
  571. struct i40e_pf *pf = veb->pf;
  572. struct i40e_hw *hw = &pf->hw;
  573. struct i40e_eth_stats *oes;
  574. struct i40e_eth_stats *es; /* device's eth stats */
  575. struct i40e_veb_tc_stats *veb_oes;
  576. struct i40e_veb_tc_stats *veb_es;
  577. int i, idx = 0;
  578. idx = veb->stats_idx;
  579. es = &veb->stats;
  580. oes = &veb->stats_offsets;
  581. veb_es = &veb->tc_stats;
  582. veb_oes = &veb->tc_stats_offsets;
  583. /* Gather up the stats that the hw collects */
  584. i40e_stat_update32(hw, I40E_GLSW_TDPC(idx),
  585. veb->stat_offsets_loaded,
  586. &oes->tx_discards, &es->tx_discards);
  587. if (hw->revision_id > 0)
  588. i40e_stat_update32(hw, I40E_GLSW_RUPP(idx),
  589. veb->stat_offsets_loaded,
  590. &oes->rx_unknown_protocol,
  591. &es->rx_unknown_protocol);
  592. i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx),
  593. veb->stat_offsets_loaded,
  594. &oes->rx_bytes, &es->rx_bytes);
  595. i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx),
  596. veb->stat_offsets_loaded,
  597. &oes->rx_unicast, &es->rx_unicast);
  598. i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx),
  599. veb->stat_offsets_loaded,
  600. &oes->rx_multicast, &es->rx_multicast);
  601. i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx),
  602. veb->stat_offsets_loaded,
  603. &oes->rx_broadcast, &es->rx_broadcast);
  604. i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx),
  605. veb->stat_offsets_loaded,
  606. &oes->tx_bytes, &es->tx_bytes);
  607. i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx),
  608. veb->stat_offsets_loaded,
  609. &oes->tx_unicast, &es->tx_unicast);
  610. i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx),
  611. veb->stat_offsets_loaded,
  612. &oes->tx_multicast, &es->tx_multicast);
  613. i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx),
  614. veb->stat_offsets_loaded,
  615. &oes->tx_broadcast, &es->tx_broadcast);
  616. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  617. i40e_stat_update48(hw, I40E_GLVEBTC_RPCH(i, idx),
  618. I40E_GLVEBTC_RPCL(i, idx),
  619. veb->stat_offsets_loaded,
  620. &veb_oes->tc_rx_packets[i],
  621. &veb_es->tc_rx_packets[i]);
  622. i40e_stat_update48(hw, I40E_GLVEBTC_RBCH(i, idx),
  623. I40E_GLVEBTC_RBCL(i, idx),
  624. veb->stat_offsets_loaded,
  625. &veb_oes->tc_rx_bytes[i],
  626. &veb_es->tc_rx_bytes[i]);
  627. i40e_stat_update48(hw, I40E_GLVEBTC_TPCH(i, idx),
  628. I40E_GLVEBTC_TPCL(i, idx),
  629. veb->stat_offsets_loaded,
  630. &veb_oes->tc_tx_packets[i],
  631. &veb_es->tc_tx_packets[i]);
  632. i40e_stat_update48(hw, I40E_GLVEBTC_TBCH(i, idx),
  633. I40E_GLVEBTC_TBCL(i, idx),
  634. veb->stat_offsets_loaded,
  635. &veb_oes->tc_tx_bytes[i],
  636. &veb_es->tc_tx_bytes[i]);
  637. }
  638. veb->stat_offsets_loaded = true;
  639. }
  640. /**
  641. * i40e_update_vsi_stats - Update the vsi statistics counters.
  642. * @vsi: the VSI to be updated
  643. *
  644. * There are a few instances where we store the same stat in a
  645. * couple of different structs. This is partly because we have
  646. * the netdev stats that need to be filled out, which is slightly
  647. * different from the "eth_stats" defined by the chip and used in
  648. * VF communications. We sort it out here.
  649. **/
  650. static void i40e_update_vsi_stats(struct i40e_vsi *vsi)
  651. {
  652. struct i40e_pf *pf = vsi->back;
  653. struct rtnl_link_stats64 *ons;
  654. struct rtnl_link_stats64 *ns; /* netdev stats */
  655. struct i40e_eth_stats *oes;
  656. struct i40e_eth_stats *es; /* device's eth stats */
  657. u32 tx_restart, tx_busy;
  658. u64 tx_lost_interrupt;
  659. struct i40e_ring *p;
  660. u32 rx_page, rx_buf;
  661. u64 bytes, packets;
  662. unsigned int start;
  663. u64 tx_linearize;
  664. u64 tx_force_wb;
  665. u64 rx_p, rx_b;
  666. u64 tx_p, tx_b;
  667. u16 q;
  668. if (test_bit(__I40E_DOWN, &vsi->state) ||
  669. test_bit(__I40E_CONFIG_BUSY, &pf->state))
  670. return;
  671. ns = i40e_get_vsi_stats_struct(vsi);
  672. ons = &vsi->net_stats_offsets;
  673. es = &vsi->eth_stats;
  674. oes = &vsi->eth_stats_offsets;
  675. /* Gather up the netdev and vsi stats that the driver collects
  676. * on the fly during packet processing
  677. */
  678. rx_b = rx_p = 0;
  679. tx_b = tx_p = 0;
  680. tx_restart = tx_busy = tx_linearize = tx_force_wb = 0;
  681. tx_lost_interrupt = 0;
  682. rx_page = 0;
  683. rx_buf = 0;
  684. rcu_read_lock();
  685. for (q = 0; q < vsi->num_queue_pairs; q++) {
  686. /* locate Tx ring */
  687. p = ACCESS_ONCE(vsi->tx_rings[q]);
  688. do {
  689. start = u64_stats_fetch_begin_irq(&p->syncp);
  690. packets = p->stats.packets;
  691. bytes = p->stats.bytes;
  692. } while (u64_stats_fetch_retry_irq(&p->syncp, start));
  693. tx_b += bytes;
  694. tx_p += packets;
  695. tx_restart += p->tx_stats.restart_queue;
  696. tx_busy += p->tx_stats.tx_busy;
  697. tx_linearize += p->tx_stats.tx_linearize;
  698. tx_force_wb += p->tx_stats.tx_force_wb;
  699. tx_lost_interrupt += p->tx_stats.tx_lost_interrupt;
  700. /* Rx queue is part of the same block as Tx queue */
  701. p = &p[1];
  702. do {
  703. start = u64_stats_fetch_begin_irq(&p->syncp);
  704. packets = p->stats.packets;
  705. bytes = p->stats.bytes;
  706. } while (u64_stats_fetch_retry_irq(&p->syncp, start));
  707. rx_b += bytes;
  708. rx_p += packets;
  709. rx_buf += p->rx_stats.alloc_buff_failed;
  710. rx_page += p->rx_stats.alloc_page_failed;
  711. }
  712. rcu_read_unlock();
  713. vsi->tx_restart = tx_restart;
  714. vsi->tx_busy = tx_busy;
  715. vsi->tx_linearize = tx_linearize;
  716. vsi->tx_force_wb = tx_force_wb;
  717. vsi->tx_lost_interrupt = tx_lost_interrupt;
  718. vsi->rx_page_failed = rx_page;
  719. vsi->rx_buf_failed = rx_buf;
  720. ns->rx_packets = rx_p;
  721. ns->rx_bytes = rx_b;
  722. ns->tx_packets = tx_p;
  723. ns->tx_bytes = tx_b;
  724. /* update netdev stats from eth stats */
  725. i40e_update_eth_stats(vsi);
  726. ons->tx_errors = oes->tx_errors;
  727. ns->tx_errors = es->tx_errors;
  728. ons->multicast = oes->rx_multicast;
  729. ns->multicast = es->rx_multicast;
  730. ons->rx_dropped = oes->rx_discards;
  731. ns->rx_dropped = es->rx_discards;
  732. ons->tx_dropped = oes->tx_discards;
  733. ns->tx_dropped = es->tx_discards;
  734. /* pull in a couple PF stats if this is the main vsi */
  735. if (vsi == pf->vsi[pf->lan_vsi]) {
  736. ns->rx_crc_errors = pf->stats.crc_errors;
  737. ns->rx_errors = pf->stats.crc_errors + pf->stats.illegal_bytes;
  738. ns->rx_length_errors = pf->stats.rx_length_errors;
  739. }
  740. }
  741. /**
  742. * i40e_update_pf_stats - Update the PF statistics counters.
  743. * @pf: the PF to be updated
  744. **/
  745. static void i40e_update_pf_stats(struct i40e_pf *pf)
  746. {
  747. struct i40e_hw_port_stats *osd = &pf->stats_offsets;
  748. struct i40e_hw_port_stats *nsd = &pf->stats;
  749. struct i40e_hw *hw = &pf->hw;
  750. u32 val;
  751. int i;
  752. i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
  753. I40E_GLPRT_GORCL(hw->port),
  754. pf->stat_offsets_loaded,
  755. &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
  756. i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
  757. I40E_GLPRT_GOTCL(hw->port),
  758. pf->stat_offsets_loaded,
  759. &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
  760. i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
  761. pf->stat_offsets_loaded,
  762. &osd->eth.rx_discards,
  763. &nsd->eth.rx_discards);
  764. i40e_stat_update48(hw, I40E_GLPRT_UPRCH(hw->port),
  765. I40E_GLPRT_UPRCL(hw->port),
  766. pf->stat_offsets_loaded,
  767. &osd->eth.rx_unicast,
  768. &nsd->eth.rx_unicast);
  769. i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
  770. I40E_GLPRT_MPRCL(hw->port),
  771. pf->stat_offsets_loaded,
  772. &osd->eth.rx_multicast,
  773. &nsd->eth.rx_multicast);
  774. i40e_stat_update48(hw, I40E_GLPRT_BPRCH(hw->port),
  775. I40E_GLPRT_BPRCL(hw->port),
  776. pf->stat_offsets_loaded,
  777. &osd->eth.rx_broadcast,
  778. &nsd->eth.rx_broadcast);
  779. i40e_stat_update48(hw, I40E_GLPRT_UPTCH(hw->port),
  780. I40E_GLPRT_UPTCL(hw->port),
  781. pf->stat_offsets_loaded,
  782. &osd->eth.tx_unicast,
  783. &nsd->eth.tx_unicast);
  784. i40e_stat_update48(hw, I40E_GLPRT_MPTCH(hw->port),
  785. I40E_GLPRT_MPTCL(hw->port),
  786. pf->stat_offsets_loaded,
  787. &osd->eth.tx_multicast,
  788. &nsd->eth.tx_multicast);
  789. i40e_stat_update48(hw, I40E_GLPRT_BPTCH(hw->port),
  790. I40E_GLPRT_BPTCL(hw->port),
  791. pf->stat_offsets_loaded,
  792. &osd->eth.tx_broadcast,
  793. &nsd->eth.tx_broadcast);
  794. i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
  795. pf->stat_offsets_loaded,
  796. &osd->tx_dropped_link_down,
  797. &nsd->tx_dropped_link_down);
  798. i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
  799. pf->stat_offsets_loaded,
  800. &osd->crc_errors, &nsd->crc_errors);
  801. i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
  802. pf->stat_offsets_loaded,
  803. &osd->illegal_bytes, &nsd->illegal_bytes);
  804. i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
  805. pf->stat_offsets_loaded,
  806. &osd->mac_local_faults,
  807. &nsd->mac_local_faults);
  808. i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
  809. pf->stat_offsets_loaded,
  810. &osd->mac_remote_faults,
  811. &nsd->mac_remote_faults);
  812. i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
  813. pf->stat_offsets_loaded,
  814. &osd->rx_length_errors,
  815. &nsd->rx_length_errors);
  816. i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
  817. pf->stat_offsets_loaded,
  818. &osd->link_xon_rx, &nsd->link_xon_rx);
  819. i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
  820. pf->stat_offsets_loaded,
  821. &osd->link_xon_tx, &nsd->link_xon_tx);
  822. i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
  823. pf->stat_offsets_loaded,
  824. &osd->link_xoff_rx, &nsd->link_xoff_rx);
  825. i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
  826. pf->stat_offsets_loaded,
  827. &osd->link_xoff_tx, &nsd->link_xoff_tx);
  828. for (i = 0; i < 8; i++) {
  829. i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
  830. pf->stat_offsets_loaded,
  831. &osd->priority_xoff_rx[i],
  832. &nsd->priority_xoff_rx[i]);
  833. i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
  834. pf->stat_offsets_loaded,
  835. &osd->priority_xon_rx[i],
  836. &nsd->priority_xon_rx[i]);
  837. i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
  838. pf->stat_offsets_loaded,
  839. &osd->priority_xon_tx[i],
  840. &nsd->priority_xon_tx[i]);
  841. i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
  842. pf->stat_offsets_loaded,
  843. &osd->priority_xoff_tx[i],
  844. &nsd->priority_xoff_tx[i]);
  845. i40e_stat_update32(hw,
  846. I40E_GLPRT_RXON2OFFCNT(hw->port, i),
  847. pf->stat_offsets_loaded,
  848. &osd->priority_xon_2_xoff[i],
  849. &nsd->priority_xon_2_xoff[i]);
  850. }
  851. i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
  852. I40E_GLPRT_PRC64L(hw->port),
  853. pf->stat_offsets_loaded,
  854. &osd->rx_size_64, &nsd->rx_size_64);
  855. i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
  856. I40E_GLPRT_PRC127L(hw->port),
  857. pf->stat_offsets_loaded,
  858. &osd->rx_size_127, &nsd->rx_size_127);
  859. i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
  860. I40E_GLPRT_PRC255L(hw->port),
  861. pf->stat_offsets_loaded,
  862. &osd->rx_size_255, &nsd->rx_size_255);
  863. i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
  864. I40E_GLPRT_PRC511L(hw->port),
  865. pf->stat_offsets_loaded,
  866. &osd->rx_size_511, &nsd->rx_size_511);
  867. i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
  868. I40E_GLPRT_PRC1023L(hw->port),
  869. pf->stat_offsets_loaded,
  870. &osd->rx_size_1023, &nsd->rx_size_1023);
  871. i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
  872. I40E_GLPRT_PRC1522L(hw->port),
  873. pf->stat_offsets_loaded,
  874. &osd->rx_size_1522, &nsd->rx_size_1522);
  875. i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
  876. I40E_GLPRT_PRC9522L(hw->port),
  877. pf->stat_offsets_loaded,
  878. &osd->rx_size_big, &nsd->rx_size_big);
  879. i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
  880. I40E_GLPRT_PTC64L(hw->port),
  881. pf->stat_offsets_loaded,
  882. &osd->tx_size_64, &nsd->tx_size_64);
  883. i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
  884. I40E_GLPRT_PTC127L(hw->port),
  885. pf->stat_offsets_loaded,
  886. &osd->tx_size_127, &nsd->tx_size_127);
  887. i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
  888. I40E_GLPRT_PTC255L(hw->port),
  889. pf->stat_offsets_loaded,
  890. &osd->tx_size_255, &nsd->tx_size_255);
  891. i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
  892. I40E_GLPRT_PTC511L(hw->port),
  893. pf->stat_offsets_loaded,
  894. &osd->tx_size_511, &nsd->tx_size_511);
  895. i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
  896. I40E_GLPRT_PTC1023L(hw->port),
  897. pf->stat_offsets_loaded,
  898. &osd->tx_size_1023, &nsd->tx_size_1023);
  899. i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
  900. I40E_GLPRT_PTC1522L(hw->port),
  901. pf->stat_offsets_loaded,
  902. &osd->tx_size_1522, &nsd->tx_size_1522);
  903. i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
  904. I40E_GLPRT_PTC9522L(hw->port),
  905. pf->stat_offsets_loaded,
  906. &osd->tx_size_big, &nsd->tx_size_big);
  907. i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
  908. pf->stat_offsets_loaded,
  909. &osd->rx_undersize, &nsd->rx_undersize);
  910. i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
  911. pf->stat_offsets_loaded,
  912. &osd->rx_fragments, &nsd->rx_fragments);
  913. i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
  914. pf->stat_offsets_loaded,
  915. &osd->rx_oversize, &nsd->rx_oversize);
  916. i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
  917. pf->stat_offsets_loaded,
  918. &osd->rx_jabber, &nsd->rx_jabber);
  919. /* FDIR stats */
  920. i40e_stat_update32(hw,
  921. I40E_GLQF_PCNT(I40E_FD_ATR_STAT_IDX(pf->hw.pf_id)),
  922. pf->stat_offsets_loaded,
  923. &osd->fd_atr_match, &nsd->fd_atr_match);
  924. i40e_stat_update32(hw,
  925. I40E_GLQF_PCNT(I40E_FD_SB_STAT_IDX(pf->hw.pf_id)),
  926. pf->stat_offsets_loaded,
  927. &osd->fd_sb_match, &nsd->fd_sb_match);
  928. i40e_stat_update32(hw,
  929. I40E_GLQF_PCNT(I40E_FD_ATR_TUNNEL_STAT_IDX(pf->hw.pf_id)),
  930. pf->stat_offsets_loaded,
  931. &osd->fd_atr_tunnel_match, &nsd->fd_atr_tunnel_match);
  932. val = rd32(hw, I40E_PRTPM_EEE_STAT);
  933. nsd->tx_lpi_status =
  934. (val & I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_MASK) >>
  935. I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_SHIFT;
  936. nsd->rx_lpi_status =
  937. (val & I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_MASK) >>
  938. I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_SHIFT;
  939. i40e_stat_update32(hw, I40E_PRTPM_TLPIC,
  940. pf->stat_offsets_loaded,
  941. &osd->tx_lpi_count, &nsd->tx_lpi_count);
  942. i40e_stat_update32(hw, I40E_PRTPM_RLPIC,
  943. pf->stat_offsets_loaded,
  944. &osd->rx_lpi_count, &nsd->rx_lpi_count);
  945. if (pf->flags & I40E_FLAG_FD_SB_ENABLED &&
  946. !(pf->hw_disabled_flags & I40E_FLAG_FD_SB_ENABLED))
  947. nsd->fd_sb_status = true;
  948. else
  949. nsd->fd_sb_status = false;
  950. if (pf->flags & I40E_FLAG_FD_ATR_ENABLED &&
  951. !(pf->hw_disabled_flags & I40E_FLAG_FD_ATR_ENABLED))
  952. nsd->fd_atr_status = true;
  953. else
  954. nsd->fd_atr_status = false;
  955. pf->stat_offsets_loaded = true;
  956. }
  957. /**
  958. * i40e_update_stats - Update the various statistics counters.
  959. * @vsi: the VSI to be updated
  960. *
  961. * Update the various stats for this VSI and its related entities.
  962. **/
  963. void i40e_update_stats(struct i40e_vsi *vsi)
  964. {
  965. struct i40e_pf *pf = vsi->back;
  966. if (vsi == pf->vsi[pf->lan_vsi])
  967. i40e_update_pf_stats(pf);
  968. i40e_update_vsi_stats(vsi);
  969. }
  970. /**
  971. * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
  972. * @vsi: the VSI to be searched
  973. * @macaddr: the MAC address
  974. * @vlan: the vlan
  975. *
  976. * Returns ptr to the filter object or NULL
  977. **/
  978. static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
  979. const u8 *macaddr, s16 vlan)
  980. {
  981. struct i40e_mac_filter *f;
  982. u64 key;
  983. if (!vsi || !macaddr)
  984. return NULL;
  985. key = i40e_addr_to_hkey(macaddr);
  986. hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
  987. if ((ether_addr_equal(macaddr, f->macaddr)) &&
  988. (vlan == f->vlan))
  989. return f;
  990. }
  991. return NULL;
  992. }
  993. /**
  994. * i40e_find_mac - Find a mac addr in the macvlan filters list
  995. * @vsi: the VSI to be searched
  996. * @macaddr: the MAC address we are searching for
  997. *
  998. * Returns the first filter with the provided MAC address or NULL if
  999. * MAC address was not found
  1000. **/
  1001. struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, const u8 *macaddr)
  1002. {
  1003. struct i40e_mac_filter *f;
  1004. u64 key;
  1005. if (!vsi || !macaddr)
  1006. return NULL;
  1007. key = i40e_addr_to_hkey(macaddr);
  1008. hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
  1009. if ((ether_addr_equal(macaddr, f->macaddr)))
  1010. return f;
  1011. }
  1012. return NULL;
  1013. }
  1014. /**
  1015. * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
  1016. * @vsi: the VSI to be searched
  1017. *
  1018. * Returns true if VSI is in vlan mode or false otherwise
  1019. **/
  1020. bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
  1021. {
  1022. /* If we have a PVID, always operate in VLAN mode */
  1023. if (vsi->info.pvid)
  1024. return true;
  1025. /* We need to operate in VLAN mode whenever we have any filters with
  1026. * a VLAN other than I40E_VLAN_ALL. We could check the table each
  1027. * time, incurring search cost repeatedly. However, we can notice two
  1028. * things:
  1029. *
  1030. * 1) the only place where we can gain a VLAN filter is in
  1031. * i40e_add_filter.
  1032. *
  1033. * 2) the only place where filters are actually removed is in
  1034. * i40e_sync_filters_subtask.
  1035. *
  1036. * Thus, we can simply use a boolean value, has_vlan_filters which we
  1037. * will set to true when we add a VLAN filter in i40e_add_filter. Then
  1038. * we have to perform the full search after deleting filters in
  1039. * i40e_sync_filters_subtask, but we already have to search
  1040. * filters here and can perform the check at the same time. This
  1041. * results in avoiding embedding a loop for VLAN mode inside another
  1042. * loop over all the filters, and should maintain correctness as noted
  1043. * above.
  1044. */
  1045. return vsi->has_vlan_filter;
  1046. }
  1047. /**
  1048. * i40e_correct_mac_vlan_filters - Correct non-VLAN filters if necessary
  1049. * @vsi: the VSI to configure
  1050. * @tmp_add_list: list of filters ready to be added
  1051. * @tmp_del_list: list of filters ready to be deleted
  1052. * @vlan_filters: the number of active VLAN filters
  1053. *
  1054. * Update VLAN=0 and VLAN=-1 (I40E_VLAN_ANY) filters properly so that they
  1055. * behave as expected. If we have any active VLAN filters remaining or about
  1056. * to be added then we need to update non-VLAN filters to be marked as VLAN=0
  1057. * so that they only match against untagged traffic. If we no longer have any
  1058. * active VLAN filters, we need to make all non-VLAN filters marked as VLAN=-1
  1059. * so that they match against both tagged and untagged traffic. In this way,
  1060. * we ensure that we correctly receive the desired traffic. This ensures that
  1061. * when we have an active VLAN we will receive only untagged traffic and
  1062. * traffic matching active VLANs. If we have no active VLANs then we will
  1063. * operate in non-VLAN mode and receive all traffic, tagged or untagged.
  1064. *
  1065. * Finally, in a similar fashion, this function also corrects filters when
  1066. * there is an active PVID assigned to this VSI.
  1067. *
  1068. * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
  1069. *
  1070. * This function is only expected to be called from within
  1071. * i40e_sync_vsi_filters.
  1072. *
  1073. * NOTE: This function expects to be called while under the
  1074. * mac_filter_hash_lock
  1075. */
  1076. static int i40e_correct_mac_vlan_filters(struct i40e_vsi *vsi,
  1077. struct hlist_head *tmp_add_list,
  1078. struct hlist_head *tmp_del_list,
  1079. int vlan_filters)
  1080. {
  1081. s16 pvid = le16_to_cpu(vsi->info.pvid);
  1082. struct i40e_mac_filter *f, *add_head;
  1083. struct i40e_new_mac_filter *new;
  1084. struct hlist_node *h;
  1085. int bkt, new_vlan;
  1086. /* To determine if a particular filter needs to be replaced we
  1087. * have the three following conditions:
  1088. *
  1089. * a) if we have a PVID assigned, then all filters which are
  1090. * not marked as VLAN=PVID must be replaced with filters that
  1091. * are.
  1092. * b) otherwise, if we have any active VLANS, all filters
  1093. * which are marked as VLAN=-1 must be replaced with
  1094. * filters marked as VLAN=0
  1095. * c) finally, if we do not have any active VLANS, all filters
  1096. * which are marked as VLAN=0 must be replaced with filters
  1097. * marked as VLAN=-1
  1098. */
  1099. /* Update the filters about to be added in place */
  1100. hlist_for_each_entry(new, tmp_add_list, hlist) {
  1101. if (pvid && new->f->vlan != pvid)
  1102. new->f->vlan = pvid;
  1103. else if (vlan_filters && new->f->vlan == I40E_VLAN_ANY)
  1104. new->f->vlan = 0;
  1105. else if (!vlan_filters && new->f->vlan == 0)
  1106. new->f->vlan = I40E_VLAN_ANY;
  1107. }
  1108. /* Update the remaining active filters */
  1109. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  1110. /* Combine the checks for whether a filter needs to be changed
  1111. * and then determine the new VLAN inside the if block, in
  1112. * order to avoid duplicating code for adding the new filter
  1113. * then deleting the old filter.
  1114. */
  1115. if ((pvid && f->vlan != pvid) ||
  1116. (vlan_filters && f->vlan == I40E_VLAN_ANY) ||
  1117. (!vlan_filters && f->vlan == 0)) {
  1118. /* Determine the new vlan we will be adding */
  1119. if (pvid)
  1120. new_vlan = pvid;
  1121. else if (vlan_filters)
  1122. new_vlan = 0;
  1123. else
  1124. new_vlan = I40E_VLAN_ANY;
  1125. /* Create the new filter */
  1126. add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
  1127. if (!add_head)
  1128. return -ENOMEM;
  1129. /* Create a temporary i40e_new_mac_filter */
  1130. new = kzalloc(sizeof(*new), GFP_ATOMIC);
  1131. if (!new)
  1132. return -ENOMEM;
  1133. new->f = add_head;
  1134. new->state = add_head->state;
  1135. /* Add the new filter to the tmp list */
  1136. hlist_add_head(&new->hlist, tmp_add_list);
  1137. /* Put the original filter into the delete list */
  1138. f->state = I40E_FILTER_REMOVE;
  1139. hash_del(&f->hlist);
  1140. hlist_add_head(&f->hlist, tmp_del_list);
  1141. }
  1142. }
  1143. vsi->has_vlan_filter = !!vlan_filters;
  1144. return 0;
  1145. }
  1146. /**
  1147. * i40e_rm_default_mac_filter - Remove the default MAC filter set by NVM
  1148. * @vsi: the PF Main VSI - inappropriate for any other VSI
  1149. * @macaddr: the MAC address
  1150. *
  1151. * Remove whatever filter the firmware set up so the driver can manage
  1152. * its own filtering intelligently.
  1153. **/
  1154. static void i40e_rm_default_mac_filter(struct i40e_vsi *vsi, u8 *macaddr)
  1155. {
  1156. struct i40e_aqc_remove_macvlan_element_data element;
  1157. struct i40e_pf *pf = vsi->back;
  1158. /* Only appropriate for the PF main VSI */
  1159. if (vsi->type != I40E_VSI_MAIN)
  1160. return;
  1161. memset(&element, 0, sizeof(element));
  1162. ether_addr_copy(element.mac_addr, macaddr);
  1163. element.vlan_tag = 0;
  1164. /* Ignore error returns, some firmware does it this way... */
  1165. element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
  1166. i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
  1167. memset(&element, 0, sizeof(element));
  1168. ether_addr_copy(element.mac_addr, macaddr);
  1169. element.vlan_tag = 0;
  1170. /* ...and some firmware does it this way. */
  1171. element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH |
  1172. I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
  1173. i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
  1174. }
  1175. /**
  1176. * i40e_add_filter - Add a mac/vlan filter to the VSI
  1177. * @vsi: the VSI to be searched
  1178. * @macaddr: the MAC address
  1179. * @vlan: the vlan
  1180. *
  1181. * Returns ptr to the filter object or NULL when no memory available.
  1182. *
  1183. * NOTE: This function is expected to be called with mac_filter_hash_lock
  1184. * being held.
  1185. **/
  1186. struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
  1187. const u8 *macaddr, s16 vlan)
  1188. {
  1189. struct i40e_mac_filter *f;
  1190. u64 key;
  1191. if (!vsi || !macaddr)
  1192. return NULL;
  1193. f = i40e_find_filter(vsi, macaddr, vlan);
  1194. if (!f) {
  1195. f = kzalloc(sizeof(*f), GFP_ATOMIC);
  1196. if (!f)
  1197. return NULL;
  1198. /* Update the boolean indicating if we need to function in
  1199. * VLAN mode.
  1200. */
  1201. if (vlan >= 0)
  1202. vsi->has_vlan_filter = true;
  1203. ether_addr_copy(f->macaddr, macaddr);
  1204. f->vlan = vlan;
  1205. /* If we're in overflow promisc mode, set the state directly
  1206. * to failed, so we don't bother to try sending the filter
  1207. * to the hardware.
  1208. */
  1209. if (test_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state))
  1210. f->state = I40E_FILTER_FAILED;
  1211. else
  1212. f->state = I40E_FILTER_NEW;
  1213. INIT_HLIST_NODE(&f->hlist);
  1214. key = i40e_addr_to_hkey(macaddr);
  1215. hash_add(vsi->mac_filter_hash, &f->hlist, key);
  1216. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  1217. vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
  1218. }
  1219. /* If we're asked to add a filter that has been marked for removal, it
  1220. * is safe to simply restore it to active state. __i40e_del_filter
  1221. * will have simply deleted any filters which were previously marked
  1222. * NEW or FAILED, so if it is currently marked REMOVE it must have
  1223. * previously been ACTIVE. Since we haven't yet run the sync filters
  1224. * task, just restore this filter to the ACTIVE state so that the
  1225. * sync task leaves it in place
  1226. */
  1227. if (f->state == I40E_FILTER_REMOVE)
  1228. f->state = I40E_FILTER_ACTIVE;
  1229. return f;
  1230. }
  1231. /**
  1232. * __i40e_del_filter - Remove a specific filter from the VSI
  1233. * @vsi: VSI to remove from
  1234. * @f: the filter to remove from the list
  1235. *
  1236. * This function should be called instead of i40e_del_filter only if you know
  1237. * the exact filter you will remove already, such as via i40e_find_filter or
  1238. * i40e_find_mac.
  1239. *
  1240. * NOTE: This function is expected to be called with mac_filter_hash_lock
  1241. * being held.
  1242. * ANOTHER NOTE: This function MUST be called from within the context of
  1243. * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
  1244. * instead of list_for_each_entry().
  1245. **/
  1246. void __i40e_del_filter(struct i40e_vsi *vsi, struct i40e_mac_filter *f)
  1247. {
  1248. if (!f)
  1249. return;
  1250. /* If the filter was never added to firmware then we can just delete it
  1251. * directly and we don't want to set the status to remove or else an
  1252. * admin queue command will unnecessarily fire.
  1253. */
  1254. if ((f->state == I40E_FILTER_FAILED) ||
  1255. (f->state == I40E_FILTER_NEW)) {
  1256. hash_del(&f->hlist);
  1257. kfree(f);
  1258. } else {
  1259. f->state = I40E_FILTER_REMOVE;
  1260. }
  1261. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  1262. vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
  1263. }
  1264. /**
  1265. * i40e_del_filter - Remove a MAC/VLAN filter from the VSI
  1266. * @vsi: the VSI to be searched
  1267. * @macaddr: the MAC address
  1268. * @vlan: the VLAN
  1269. *
  1270. * NOTE: This function is expected to be called with mac_filter_hash_lock
  1271. * being held.
  1272. * ANOTHER NOTE: This function MUST be called from within the context of
  1273. * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
  1274. * instead of list_for_each_entry().
  1275. **/
  1276. void i40e_del_filter(struct i40e_vsi *vsi, const u8 *macaddr, s16 vlan)
  1277. {
  1278. struct i40e_mac_filter *f;
  1279. if (!vsi || !macaddr)
  1280. return;
  1281. f = i40e_find_filter(vsi, macaddr, vlan);
  1282. __i40e_del_filter(vsi, f);
  1283. }
  1284. /**
  1285. * i40e_add_mac_filter - Add a MAC filter for all active VLANs
  1286. * @vsi: the VSI to be searched
  1287. * @macaddr: the mac address to be filtered
  1288. *
  1289. * If we're not in VLAN mode, just add the filter to I40E_VLAN_ANY. Otherwise,
  1290. * go through all the macvlan filters and add a macvlan filter for each
  1291. * unique vlan that already exists. If a PVID has been assigned, instead only
  1292. * add the macaddr to that VLAN.
  1293. *
  1294. * Returns last filter added on success, else NULL
  1295. **/
  1296. struct i40e_mac_filter *i40e_add_mac_filter(struct i40e_vsi *vsi,
  1297. const u8 *macaddr)
  1298. {
  1299. struct i40e_mac_filter *f, *add = NULL;
  1300. struct hlist_node *h;
  1301. int bkt;
  1302. if (vsi->info.pvid)
  1303. return i40e_add_filter(vsi, macaddr,
  1304. le16_to_cpu(vsi->info.pvid));
  1305. if (!i40e_is_vsi_in_vlan(vsi))
  1306. return i40e_add_filter(vsi, macaddr, I40E_VLAN_ANY);
  1307. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  1308. if (f->state == I40E_FILTER_REMOVE)
  1309. continue;
  1310. add = i40e_add_filter(vsi, macaddr, f->vlan);
  1311. if (!add)
  1312. return NULL;
  1313. }
  1314. return add;
  1315. }
  1316. /**
  1317. * i40e_del_mac_filter - Remove a MAC filter from all VLANs
  1318. * @vsi: the VSI to be searched
  1319. * @macaddr: the mac address to be removed
  1320. *
  1321. * Removes a given MAC address from a VSI regardless of what VLAN it has been
  1322. * associated with.
  1323. *
  1324. * Returns 0 for success, or error
  1325. **/
  1326. int i40e_del_mac_filter(struct i40e_vsi *vsi, const u8 *macaddr)
  1327. {
  1328. struct i40e_mac_filter *f;
  1329. struct hlist_node *h;
  1330. bool found = false;
  1331. int bkt;
  1332. WARN(!spin_is_locked(&vsi->mac_filter_hash_lock),
  1333. "Missing mac_filter_hash_lock\n");
  1334. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  1335. if (ether_addr_equal(macaddr, f->macaddr)) {
  1336. __i40e_del_filter(vsi, f);
  1337. found = true;
  1338. }
  1339. }
  1340. if (found)
  1341. return 0;
  1342. else
  1343. return -ENOENT;
  1344. }
  1345. /**
  1346. * i40e_set_mac - NDO callback to set mac address
  1347. * @netdev: network interface device structure
  1348. * @p: pointer to an address structure
  1349. *
  1350. * Returns 0 on success, negative on failure
  1351. **/
  1352. static int i40e_set_mac(struct net_device *netdev, void *p)
  1353. {
  1354. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1355. struct i40e_vsi *vsi = np->vsi;
  1356. struct i40e_pf *pf = vsi->back;
  1357. struct i40e_hw *hw = &pf->hw;
  1358. struct sockaddr *addr = p;
  1359. if (!is_valid_ether_addr(addr->sa_data))
  1360. return -EADDRNOTAVAIL;
  1361. if (ether_addr_equal(netdev->dev_addr, addr->sa_data)) {
  1362. netdev_info(netdev, "already using mac address %pM\n",
  1363. addr->sa_data);
  1364. return 0;
  1365. }
  1366. if (test_bit(__I40E_DOWN, &vsi->back->state) ||
  1367. test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
  1368. return -EADDRNOTAVAIL;
  1369. if (ether_addr_equal(hw->mac.addr, addr->sa_data))
  1370. netdev_info(netdev, "returning to hw mac address %pM\n",
  1371. hw->mac.addr);
  1372. else
  1373. netdev_info(netdev, "set new mac address %pM\n", addr->sa_data);
  1374. spin_lock_bh(&vsi->mac_filter_hash_lock);
  1375. i40e_del_mac_filter(vsi, netdev->dev_addr);
  1376. i40e_add_mac_filter(vsi, addr->sa_data);
  1377. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  1378. ether_addr_copy(netdev->dev_addr, addr->sa_data);
  1379. if (vsi->type == I40E_VSI_MAIN) {
  1380. i40e_status ret;
  1381. ret = i40e_aq_mac_address_write(&vsi->back->hw,
  1382. I40E_AQC_WRITE_TYPE_LAA_WOL,
  1383. addr->sa_data, NULL);
  1384. if (ret)
  1385. netdev_info(netdev, "Ignoring error from firmware on LAA update, status %s, AQ ret %s\n",
  1386. i40e_stat_str(hw, ret),
  1387. i40e_aq_str(hw, hw->aq.asq_last_status));
  1388. }
  1389. /* schedule our worker thread which will take care of
  1390. * applying the new filter changes
  1391. */
  1392. i40e_service_event_schedule(vsi->back);
  1393. return 0;
  1394. }
  1395. /**
  1396. * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
  1397. * @vsi: the VSI being setup
  1398. * @ctxt: VSI context structure
  1399. * @enabled_tc: Enabled TCs bitmap
  1400. * @is_add: True if called before Add VSI
  1401. *
  1402. * Setup VSI queue mapping for enabled traffic classes.
  1403. **/
  1404. static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
  1405. struct i40e_vsi_context *ctxt,
  1406. u8 enabled_tc,
  1407. bool is_add)
  1408. {
  1409. struct i40e_pf *pf = vsi->back;
  1410. u16 sections = 0;
  1411. u8 netdev_tc = 0;
  1412. u16 numtc = 0;
  1413. u16 qcount;
  1414. u8 offset;
  1415. u16 qmap;
  1416. int i;
  1417. u16 num_tc_qps = 0;
  1418. sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
  1419. offset = 0;
  1420. if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
  1421. /* Find numtc from enabled TC bitmap */
  1422. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  1423. if (enabled_tc & BIT(i)) /* TC is enabled */
  1424. numtc++;
  1425. }
  1426. if (!numtc) {
  1427. dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
  1428. numtc = 1;
  1429. }
  1430. } else {
  1431. /* At least TC0 is enabled in case of non-DCB case */
  1432. numtc = 1;
  1433. }
  1434. vsi->tc_config.numtc = numtc;
  1435. vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
  1436. /* Number of queues per enabled TC */
  1437. qcount = vsi->alloc_queue_pairs;
  1438. num_tc_qps = qcount / numtc;
  1439. num_tc_qps = min_t(int, num_tc_qps, i40e_pf_get_max_q_per_tc(pf));
  1440. /* Setup queue offset/count for all TCs for given VSI */
  1441. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  1442. /* See if the given TC is enabled for the given VSI */
  1443. if (vsi->tc_config.enabled_tc & BIT(i)) {
  1444. /* TC is enabled */
  1445. int pow, num_qps;
  1446. switch (vsi->type) {
  1447. case I40E_VSI_MAIN:
  1448. qcount = min_t(int, pf->alloc_rss_size,
  1449. num_tc_qps);
  1450. break;
  1451. case I40E_VSI_FDIR:
  1452. case I40E_VSI_SRIOV:
  1453. case I40E_VSI_VMDQ2:
  1454. default:
  1455. qcount = num_tc_qps;
  1456. WARN_ON(i != 0);
  1457. break;
  1458. }
  1459. vsi->tc_config.tc_info[i].qoffset = offset;
  1460. vsi->tc_config.tc_info[i].qcount = qcount;
  1461. /* find the next higher power-of-2 of num queue pairs */
  1462. num_qps = qcount;
  1463. pow = 0;
  1464. while (num_qps && (BIT_ULL(pow) < qcount)) {
  1465. pow++;
  1466. num_qps >>= 1;
  1467. }
  1468. vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
  1469. qmap =
  1470. (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
  1471. (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
  1472. offset += qcount;
  1473. } else {
  1474. /* TC is not enabled so set the offset to
  1475. * default queue and allocate one queue
  1476. * for the given TC.
  1477. */
  1478. vsi->tc_config.tc_info[i].qoffset = 0;
  1479. vsi->tc_config.tc_info[i].qcount = 1;
  1480. vsi->tc_config.tc_info[i].netdev_tc = 0;
  1481. qmap = 0;
  1482. }
  1483. ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
  1484. }
  1485. /* Set actual Tx/Rx queue pairs */
  1486. vsi->num_queue_pairs = offset;
  1487. if ((vsi->type == I40E_VSI_MAIN) && (numtc == 1)) {
  1488. if (vsi->req_queue_pairs > 0)
  1489. vsi->num_queue_pairs = vsi->req_queue_pairs;
  1490. else if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  1491. vsi->num_queue_pairs = pf->num_lan_msix;
  1492. }
  1493. /* Scheduler section valid can only be set for ADD VSI */
  1494. if (is_add) {
  1495. sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
  1496. ctxt->info.up_enable_bits = enabled_tc;
  1497. }
  1498. if (vsi->type == I40E_VSI_SRIOV) {
  1499. ctxt->info.mapping_flags |=
  1500. cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
  1501. for (i = 0; i < vsi->num_queue_pairs; i++)
  1502. ctxt->info.queue_mapping[i] =
  1503. cpu_to_le16(vsi->base_queue + i);
  1504. } else {
  1505. ctxt->info.mapping_flags |=
  1506. cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
  1507. ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
  1508. }
  1509. ctxt->info.valid_sections |= cpu_to_le16(sections);
  1510. }
  1511. /**
  1512. * i40e_addr_sync - Callback for dev_(mc|uc)_sync to add address
  1513. * @netdev: the netdevice
  1514. * @addr: address to add
  1515. *
  1516. * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
  1517. * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
  1518. */
  1519. static int i40e_addr_sync(struct net_device *netdev, const u8 *addr)
  1520. {
  1521. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1522. struct i40e_vsi *vsi = np->vsi;
  1523. if (i40e_add_mac_filter(vsi, addr))
  1524. return 0;
  1525. else
  1526. return -ENOMEM;
  1527. }
  1528. /**
  1529. * i40e_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
  1530. * @netdev: the netdevice
  1531. * @addr: address to add
  1532. *
  1533. * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call
  1534. * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
  1535. */
  1536. static int i40e_addr_unsync(struct net_device *netdev, const u8 *addr)
  1537. {
  1538. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1539. struct i40e_vsi *vsi = np->vsi;
  1540. i40e_del_mac_filter(vsi, addr);
  1541. return 0;
  1542. }
  1543. /**
  1544. * i40e_set_rx_mode - NDO callback to set the netdev filters
  1545. * @netdev: network interface device structure
  1546. **/
  1547. static void i40e_set_rx_mode(struct net_device *netdev)
  1548. {
  1549. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1550. struct i40e_vsi *vsi = np->vsi;
  1551. spin_lock_bh(&vsi->mac_filter_hash_lock);
  1552. __dev_uc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
  1553. __dev_mc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
  1554. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  1555. /* check for other flag changes */
  1556. if (vsi->current_netdev_flags != vsi->netdev->flags) {
  1557. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  1558. vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
  1559. }
  1560. /* schedule our worker thread which will take care of
  1561. * applying the new filter changes
  1562. */
  1563. i40e_service_event_schedule(vsi->back);
  1564. }
  1565. /**
  1566. * i40e_undo_del_filter_entries - Undo the changes made to MAC filter entries
  1567. * @vsi: Pointer to VSI struct
  1568. * @from: Pointer to list which contains MAC filter entries - changes to
  1569. * those entries needs to be undone.
  1570. *
  1571. * MAC filter entries from this list were slated for deletion.
  1572. **/
  1573. static void i40e_undo_del_filter_entries(struct i40e_vsi *vsi,
  1574. struct hlist_head *from)
  1575. {
  1576. struct i40e_mac_filter *f;
  1577. struct hlist_node *h;
  1578. hlist_for_each_entry_safe(f, h, from, hlist) {
  1579. u64 key = i40e_addr_to_hkey(f->macaddr);
  1580. /* Move the element back into MAC filter list*/
  1581. hlist_del(&f->hlist);
  1582. hash_add(vsi->mac_filter_hash, &f->hlist, key);
  1583. }
  1584. }
  1585. /**
  1586. * i40e_undo_add_filter_entries - Undo the changes made to MAC filter entries
  1587. * @vsi: Pointer to vsi struct
  1588. * @from: Pointer to list which contains MAC filter entries - changes to
  1589. * those entries needs to be undone.
  1590. *
  1591. * MAC filter entries from this list were slated for addition.
  1592. **/
  1593. static void i40e_undo_add_filter_entries(struct i40e_vsi *vsi,
  1594. struct hlist_head *from)
  1595. {
  1596. struct i40e_new_mac_filter *new;
  1597. struct hlist_node *h;
  1598. hlist_for_each_entry_safe(new, h, from, hlist) {
  1599. /* We can simply free the wrapper structure */
  1600. hlist_del(&new->hlist);
  1601. kfree(new);
  1602. }
  1603. }
  1604. /**
  1605. * i40e_next_entry - Get the next non-broadcast filter from a list
  1606. * @next: pointer to filter in list
  1607. *
  1608. * Returns the next non-broadcast filter in the list. Required so that we
  1609. * ignore broadcast filters within the list, since these are not handled via
  1610. * the normal firmware update path.
  1611. */
  1612. static
  1613. struct i40e_new_mac_filter *i40e_next_filter(struct i40e_new_mac_filter *next)
  1614. {
  1615. hlist_for_each_entry_continue(next, hlist) {
  1616. if (!is_broadcast_ether_addr(next->f->macaddr))
  1617. return next;
  1618. }
  1619. return NULL;
  1620. }
  1621. /**
  1622. * i40e_update_filter_state - Update filter state based on return data
  1623. * from firmware
  1624. * @count: Number of filters added
  1625. * @add_list: return data from fw
  1626. * @head: pointer to first filter in current batch
  1627. *
  1628. * MAC filter entries from list were slated to be added to device. Returns
  1629. * number of successful filters. Note that 0 does NOT mean success!
  1630. **/
  1631. static int
  1632. i40e_update_filter_state(int count,
  1633. struct i40e_aqc_add_macvlan_element_data *add_list,
  1634. struct i40e_new_mac_filter *add_head)
  1635. {
  1636. int retval = 0;
  1637. int i;
  1638. for (i = 0; i < count; i++) {
  1639. /* Always check status of each filter. We don't need to check
  1640. * the firmware return status because we pre-set the filter
  1641. * status to I40E_AQC_MM_ERR_NO_RES when sending the filter
  1642. * request to the adminq. Thus, if it no longer matches then
  1643. * we know the filter is active.
  1644. */
  1645. if (add_list[i].match_method == I40E_AQC_MM_ERR_NO_RES) {
  1646. add_head->state = I40E_FILTER_FAILED;
  1647. } else {
  1648. add_head->state = I40E_FILTER_ACTIVE;
  1649. retval++;
  1650. }
  1651. add_head = i40e_next_filter(add_head);
  1652. if (!add_head)
  1653. break;
  1654. }
  1655. return retval;
  1656. }
  1657. /**
  1658. * i40e_aqc_del_filters - Request firmware to delete a set of filters
  1659. * @vsi: ptr to the VSI
  1660. * @vsi_name: name to display in messages
  1661. * @list: the list of filters to send to firmware
  1662. * @num_del: the number of filters to delete
  1663. * @retval: Set to -EIO on failure to delete
  1664. *
  1665. * Send a request to firmware via AdminQ to delete a set of filters. Uses
  1666. * *retval instead of a return value so that success does not force ret_val to
  1667. * be set to 0. This ensures that a sequence of calls to this function
  1668. * preserve the previous value of *retval on successful delete.
  1669. */
  1670. static
  1671. void i40e_aqc_del_filters(struct i40e_vsi *vsi, const char *vsi_name,
  1672. struct i40e_aqc_remove_macvlan_element_data *list,
  1673. int num_del, int *retval)
  1674. {
  1675. struct i40e_hw *hw = &vsi->back->hw;
  1676. i40e_status aq_ret;
  1677. int aq_err;
  1678. aq_ret = i40e_aq_remove_macvlan(hw, vsi->seid, list, num_del, NULL);
  1679. aq_err = hw->aq.asq_last_status;
  1680. /* Explicitly ignore and do not report when firmware returns ENOENT */
  1681. if (aq_ret && !(aq_err == I40E_AQ_RC_ENOENT)) {
  1682. *retval = -EIO;
  1683. dev_info(&vsi->back->pdev->dev,
  1684. "ignoring delete macvlan error on %s, err %s, aq_err %s\n",
  1685. vsi_name, i40e_stat_str(hw, aq_ret),
  1686. i40e_aq_str(hw, aq_err));
  1687. }
  1688. }
  1689. /**
  1690. * i40e_aqc_add_filters - Request firmware to add a set of filters
  1691. * @vsi: ptr to the VSI
  1692. * @vsi_name: name to display in messages
  1693. * @list: the list of filters to send to firmware
  1694. * @add_head: Position in the add hlist
  1695. * @num_add: the number of filters to add
  1696. * @promisc_change: set to true on exit if promiscuous mode was forced on
  1697. *
  1698. * Send a request to firmware via AdminQ to add a chunk of filters. Will set
  1699. * promisc_changed to true if the firmware has run out of space for more
  1700. * filters.
  1701. */
  1702. static
  1703. void i40e_aqc_add_filters(struct i40e_vsi *vsi, const char *vsi_name,
  1704. struct i40e_aqc_add_macvlan_element_data *list,
  1705. struct i40e_new_mac_filter *add_head,
  1706. int num_add, bool *promisc_changed)
  1707. {
  1708. struct i40e_hw *hw = &vsi->back->hw;
  1709. int aq_err, fcnt;
  1710. i40e_aq_add_macvlan(hw, vsi->seid, list, num_add, NULL);
  1711. aq_err = hw->aq.asq_last_status;
  1712. fcnt = i40e_update_filter_state(num_add, list, add_head);
  1713. if (fcnt != num_add) {
  1714. *promisc_changed = true;
  1715. set_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state);
  1716. dev_warn(&vsi->back->pdev->dev,
  1717. "Error %s adding RX filters on %s, promiscuous mode forced on\n",
  1718. i40e_aq_str(hw, aq_err),
  1719. vsi_name);
  1720. }
  1721. }
  1722. /**
  1723. * i40e_aqc_broadcast_filter - Set promiscuous broadcast flags
  1724. * @vsi: pointer to the VSI
  1725. * @f: filter data
  1726. *
  1727. * This function sets or clears the promiscuous broadcast flags for VLAN
  1728. * filters in order to properly receive broadcast frames. Assumes that only
  1729. * broadcast filters are passed.
  1730. *
  1731. * Returns status indicating success or failure;
  1732. **/
  1733. static i40e_status
  1734. i40e_aqc_broadcast_filter(struct i40e_vsi *vsi, const char *vsi_name,
  1735. struct i40e_mac_filter *f)
  1736. {
  1737. bool enable = f->state == I40E_FILTER_NEW;
  1738. struct i40e_hw *hw = &vsi->back->hw;
  1739. i40e_status aq_ret;
  1740. if (f->vlan == I40E_VLAN_ANY) {
  1741. aq_ret = i40e_aq_set_vsi_broadcast(hw,
  1742. vsi->seid,
  1743. enable,
  1744. NULL);
  1745. } else {
  1746. aq_ret = i40e_aq_set_vsi_bc_promisc_on_vlan(hw,
  1747. vsi->seid,
  1748. enable,
  1749. f->vlan,
  1750. NULL);
  1751. }
  1752. if (aq_ret)
  1753. dev_warn(&vsi->back->pdev->dev,
  1754. "Error %s setting broadcast promiscuous mode on %s\n",
  1755. i40e_aq_str(hw, hw->aq.asq_last_status),
  1756. vsi_name);
  1757. return aq_ret;
  1758. }
  1759. /**
  1760. * i40e_sync_vsi_filters - Update the VSI filter list to the HW
  1761. * @vsi: ptr to the VSI
  1762. *
  1763. * Push any outstanding VSI filter changes through the AdminQ.
  1764. *
  1765. * Returns 0 or error value
  1766. **/
  1767. int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
  1768. {
  1769. struct hlist_head tmp_add_list, tmp_del_list;
  1770. struct i40e_mac_filter *f;
  1771. struct i40e_new_mac_filter *new, *add_head = NULL;
  1772. struct i40e_hw *hw = &vsi->back->hw;
  1773. unsigned int failed_filters = 0;
  1774. unsigned int vlan_filters = 0;
  1775. bool promisc_changed = false;
  1776. char vsi_name[16] = "PF";
  1777. int filter_list_len = 0;
  1778. i40e_status aq_ret = 0;
  1779. u32 changed_flags = 0;
  1780. struct hlist_node *h;
  1781. struct i40e_pf *pf;
  1782. int num_add = 0;
  1783. int num_del = 0;
  1784. int retval = 0;
  1785. u16 cmd_flags;
  1786. int list_size;
  1787. int bkt;
  1788. /* empty array typed pointers, kcalloc later */
  1789. struct i40e_aqc_add_macvlan_element_data *add_list;
  1790. struct i40e_aqc_remove_macvlan_element_data *del_list;
  1791. while (test_and_set_bit(__I40E_CONFIG_BUSY, &vsi->state))
  1792. usleep_range(1000, 2000);
  1793. pf = vsi->back;
  1794. if (vsi->netdev) {
  1795. changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
  1796. vsi->current_netdev_flags = vsi->netdev->flags;
  1797. }
  1798. INIT_HLIST_HEAD(&tmp_add_list);
  1799. INIT_HLIST_HEAD(&tmp_del_list);
  1800. if (vsi->type == I40E_VSI_SRIOV)
  1801. snprintf(vsi_name, sizeof(vsi_name) - 1, "VF %d", vsi->vf_id);
  1802. else if (vsi->type != I40E_VSI_MAIN)
  1803. snprintf(vsi_name, sizeof(vsi_name) - 1, "vsi %d", vsi->seid);
  1804. if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
  1805. vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
  1806. spin_lock_bh(&vsi->mac_filter_hash_lock);
  1807. /* Create a list of filters to delete. */
  1808. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  1809. if (f->state == I40E_FILTER_REMOVE) {
  1810. /* Move the element into temporary del_list */
  1811. hash_del(&f->hlist);
  1812. hlist_add_head(&f->hlist, &tmp_del_list);
  1813. /* Avoid counting removed filters */
  1814. continue;
  1815. }
  1816. if (f->state == I40E_FILTER_NEW) {
  1817. /* Create a temporary i40e_new_mac_filter */
  1818. new = kzalloc(sizeof(*new), GFP_ATOMIC);
  1819. if (!new)
  1820. goto err_no_memory_locked;
  1821. /* Store pointer to the real filter */
  1822. new->f = f;
  1823. new->state = f->state;
  1824. /* Add it to the hash list */
  1825. hlist_add_head(&new->hlist, &tmp_add_list);
  1826. }
  1827. /* Count the number of active (current and new) VLAN
  1828. * filters we have now. Does not count filters which
  1829. * are marked for deletion.
  1830. */
  1831. if (f->vlan > 0)
  1832. vlan_filters++;
  1833. }
  1834. retval = i40e_correct_mac_vlan_filters(vsi,
  1835. &tmp_add_list,
  1836. &tmp_del_list,
  1837. vlan_filters);
  1838. if (retval)
  1839. goto err_no_memory_locked;
  1840. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  1841. }
  1842. /* Now process 'del_list' outside the lock */
  1843. if (!hlist_empty(&tmp_del_list)) {
  1844. filter_list_len = hw->aq.asq_buf_size /
  1845. sizeof(struct i40e_aqc_remove_macvlan_element_data);
  1846. list_size = filter_list_len *
  1847. sizeof(struct i40e_aqc_remove_macvlan_element_data);
  1848. del_list = kzalloc(list_size, GFP_ATOMIC);
  1849. if (!del_list)
  1850. goto err_no_memory;
  1851. hlist_for_each_entry_safe(f, h, &tmp_del_list, hlist) {
  1852. cmd_flags = 0;
  1853. /* handle broadcast filters by updating the broadcast
  1854. * promiscuous flag and release filter list.
  1855. */
  1856. if (is_broadcast_ether_addr(f->macaddr)) {
  1857. i40e_aqc_broadcast_filter(vsi, vsi_name, f);
  1858. hlist_del(&f->hlist);
  1859. kfree(f);
  1860. continue;
  1861. }
  1862. /* add to delete list */
  1863. ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
  1864. if (f->vlan == I40E_VLAN_ANY) {
  1865. del_list[num_del].vlan_tag = 0;
  1866. cmd_flags |= I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
  1867. } else {
  1868. del_list[num_del].vlan_tag =
  1869. cpu_to_le16((u16)(f->vlan));
  1870. }
  1871. cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
  1872. del_list[num_del].flags = cmd_flags;
  1873. num_del++;
  1874. /* flush a full buffer */
  1875. if (num_del == filter_list_len) {
  1876. i40e_aqc_del_filters(vsi, vsi_name, del_list,
  1877. num_del, &retval);
  1878. memset(del_list, 0, list_size);
  1879. num_del = 0;
  1880. }
  1881. /* Release memory for MAC filter entries which were
  1882. * synced up with HW.
  1883. */
  1884. hlist_del(&f->hlist);
  1885. kfree(f);
  1886. }
  1887. if (num_del) {
  1888. i40e_aqc_del_filters(vsi, vsi_name, del_list,
  1889. num_del, &retval);
  1890. }
  1891. kfree(del_list);
  1892. del_list = NULL;
  1893. }
  1894. if (!hlist_empty(&tmp_add_list)) {
  1895. /* Do all the adds now. */
  1896. filter_list_len = hw->aq.asq_buf_size /
  1897. sizeof(struct i40e_aqc_add_macvlan_element_data);
  1898. list_size = filter_list_len *
  1899. sizeof(struct i40e_aqc_add_macvlan_element_data);
  1900. add_list = kzalloc(list_size, GFP_ATOMIC);
  1901. if (!add_list)
  1902. goto err_no_memory;
  1903. num_add = 0;
  1904. hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
  1905. if (test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
  1906. &vsi->state)) {
  1907. new->state = I40E_FILTER_FAILED;
  1908. continue;
  1909. }
  1910. /* handle broadcast filters by updating the broadcast
  1911. * promiscuous flag instead of adding a MAC filter.
  1912. */
  1913. if (is_broadcast_ether_addr(new->f->macaddr)) {
  1914. if (i40e_aqc_broadcast_filter(vsi, vsi_name,
  1915. new->f))
  1916. new->state = I40E_FILTER_FAILED;
  1917. else
  1918. new->state = I40E_FILTER_ACTIVE;
  1919. continue;
  1920. }
  1921. /* add to add array */
  1922. if (num_add == 0)
  1923. add_head = new;
  1924. cmd_flags = 0;
  1925. ether_addr_copy(add_list[num_add].mac_addr,
  1926. new->f->macaddr);
  1927. if (new->f->vlan == I40E_VLAN_ANY) {
  1928. add_list[num_add].vlan_tag = 0;
  1929. cmd_flags |= I40E_AQC_MACVLAN_ADD_IGNORE_VLAN;
  1930. } else {
  1931. add_list[num_add].vlan_tag =
  1932. cpu_to_le16((u16)(new->f->vlan));
  1933. }
  1934. add_list[num_add].queue_number = 0;
  1935. /* set invalid match method for later detection */
  1936. add_list[num_add].match_method = I40E_AQC_MM_ERR_NO_RES;
  1937. cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
  1938. add_list[num_add].flags = cpu_to_le16(cmd_flags);
  1939. num_add++;
  1940. /* flush a full buffer */
  1941. if (num_add == filter_list_len) {
  1942. i40e_aqc_add_filters(vsi, vsi_name, add_list,
  1943. add_head, num_add,
  1944. &promisc_changed);
  1945. memset(add_list, 0, list_size);
  1946. num_add = 0;
  1947. }
  1948. }
  1949. if (num_add) {
  1950. i40e_aqc_add_filters(vsi, vsi_name, add_list, add_head,
  1951. num_add, &promisc_changed);
  1952. }
  1953. /* Now move all of the filters from the temp add list back to
  1954. * the VSI's list.
  1955. */
  1956. spin_lock_bh(&vsi->mac_filter_hash_lock);
  1957. hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
  1958. /* Only update the state if we're still NEW */
  1959. if (new->f->state == I40E_FILTER_NEW)
  1960. new->f->state = new->state;
  1961. hlist_del(&new->hlist);
  1962. kfree(new);
  1963. }
  1964. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  1965. kfree(add_list);
  1966. add_list = NULL;
  1967. }
  1968. /* Determine the number of active and failed filters. */
  1969. spin_lock_bh(&vsi->mac_filter_hash_lock);
  1970. vsi->active_filters = 0;
  1971. hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
  1972. if (f->state == I40E_FILTER_ACTIVE)
  1973. vsi->active_filters++;
  1974. else if (f->state == I40E_FILTER_FAILED)
  1975. failed_filters++;
  1976. }
  1977. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  1978. /* If promiscuous mode has changed, we need to calculate a new
  1979. * threshold for when we are safe to exit
  1980. */
  1981. if (promisc_changed)
  1982. vsi->promisc_threshold = (vsi->active_filters * 3) / 4;
  1983. /* Check if we are able to exit overflow promiscuous mode. We can
  1984. * safely exit if we didn't just enter, we no longer have any failed
  1985. * filters, and we have reduced filters below the threshold value.
  1986. */
  1987. if (test_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state) &&
  1988. !promisc_changed && !failed_filters &&
  1989. (vsi->active_filters < vsi->promisc_threshold)) {
  1990. dev_info(&pf->pdev->dev,
  1991. "filter logjam cleared on %s, leaving overflow promiscuous mode\n",
  1992. vsi_name);
  1993. clear_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state);
  1994. promisc_changed = true;
  1995. vsi->promisc_threshold = 0;
  1996. }
  1997. /* if the VF is not trusted do not do promisc */
  1998. if ((vsi->type == I40E_VSI_SRIOV) && !pf->vf[vsi->vf_id].trusted) {
  1999. clear_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state);
  2000. goto out;
  2001. }
  2002. /* check for changes in promiscuous modes */
  2003. if (changed_flags & IFF_ALLMULTI) {
  2004. bool cur_multipromisc;
  2005. cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
  2006. aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
  2007. vsi->seid,
  2008. cur_multipromisc,
  2009. NULL);
  2010. if (aq_ret) {
  2011. retval = i40e_aq_rc_to_posix(aq_ret,
  2012. hw->aq.asq_last_status);
  2013. dev_info(&pf->pdev->dev,
  2014. "set multi promisc failed on %s, err %s aq_err %s\n",
  2015. vsi_name,
  2016. i40e_stat_str(hw, aq_ret),
  2017. i40e_aq_str(hw, hw->aq.asq_last_status));
  2018. }
  2019. }
  2020. if ((changed_flags & IFF_PROMISC) ||
  2021. (promisc_changed &&
  2022. test_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state))) {
  2023. bool cur_promisc;
  2024. cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
  2025. test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
  2026. &vsi->state));
  2027. if ((vsi->type == I40E_VSI_MAIN) &&
  2028. (pf->lan_veb != I40E_NO_VEB) &&
  2029. !(pf->flags & I40E_FLAG_MFP_ENABLED)) {
  2030. /* set defport ON for Main VSI instead of true promisc
  2031. * this way we will get all unicast/multicast and VLAN
  2032. * promisc behavior but will not get VF or VMDq traffic
  2033. * replicated on the Main VSI.
  2034. */
  2035. if (pf->cur_promisc != cur_promisc) {
  2036. pf->cur_promisc = cur_promisc;
  2037. if (cur_promisc)
  2038. aq_ret =
  2039. i40e_aq_set_default_vsi(hw,
  2040. vsi->seid,
  2041. NULL);
  2042. else
  2043. aq_ret =
  2044. i40e_aq_clear_default_vsi(hw,
  2045. vsi->seid,
  2046. NULL);
  2047. if (aq_ret) {
  2048. retval = i40e_aq_rc_to_posix(aq_ret,
  2049. hw->aq.asq_last_status);
  2050. dev_info(&pf->pdev->dev,
  2051. "Set default VSI failed on %s, err %s, aq_err %s\n",
  2052. vsi_name,
  2053. i40e_stat_str(hw, aq_ret),
  2054. i40e_aq_str(hw,
  2055. hw->aq.asq_last_status));
  2056. }
  2057. }
  2058. } else {
  2059. aq_ret = i40e_aq_set_vsi_unicast_promiscuous(
  2060. hw,
  2061. vsi->seid,
  2062. cur_promisc, NULL,
  2063. true);
  2064. if (aq_ret) {
  2065. retval =
  2066. i40e_aq_rc_to_posix(aq_ret,
  2067. hw->aq.asq_last_status);
  2068. dev_info(&pf->pdev->dev,
  2069. "set unicast promisc failed on %s, err %s, aq_err %s\n",
  2070. vsi_name,
  2071. i40e_stat_str(hw, aq_ret),
  2072. i40e_aq_str(hw,
  2073. hw->aq.asq_last_status));
  2074. }
  2075. aq_ret = i40e_aq_set_vsi_multicast_promiscuous(
  2076. hw,
  2077. vsi->seid,
  2078. cur_promisc, NULL);
  2079. if (aq_ret) {
  2080. retval =
  2081. i40e_aq_rc_to_posix(aq_ret,
  2082. hw->aq.asq_last_status);
  2083. dev_info(&pf->pdev->dev,
  2084. "set multicast promisc failed on %s, err %s, aq_err %s\n",
  2085. vsi_name,
  2086. i40e_stat_str(hw, aq_ret),
  2087. i40e_aq_str(hw,
  2088. hw->aq.asq_last_status));
  2089. }
  2090. }
  2091. aq_ret = i40e_aq_set_vsi_broadcast(&vsi->back->hw,
  2092. vsi->seid,
  2093. cur_promisc, NULL);
  2094. if (aq_ret) {
  2095. retval = i40e_aq_rc_to_posix(aq_ret,
  2096. pf->hw.aq.asq_last_status);
  2097. dev_info(&pf->pdev->dev,
  2098. "set brdcast promisc failed, err %s, aq_err %s\n",
  2099. i40e_stat_str(hw, aq_ret),
  2100. i40e_aq_str(hw,
  2101. hw->aq.asq_last_status));
  2102. }
  2103. }
  2104. out:
  2105. /* if something went wrong then set the changed flag so we try again */
  2106. if (retval)
  2107. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  2108. clear_bit(__I40E_CONFIG_BUSY, &vsi->state);
  2109. return retval;
  2110. err_no_memory:
  2111. /* Restore elements on the temporary add and delete lists */
  2112. spin_lock_bh(&vsi->mac_filter_hash_lock);
  2113. err_no_memory_locked:
  2114. i40e_undo_del_filter_entries(vsi, &tmp_del_list);
  2115. i40e_undo_add_filter_entries(vsi, &tmp_add_list);
  2116. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2117. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  2118. clear_bit(__I40E_CONFIG_BUSY, &vsi->state);
  2119. return -ENOMEM;
  2120. }
  2121. /**
  2122. * i40e_sync_filters_subtask - Sync the VSI filter list with HW
  2123. * @pf: board private structure
  2124. **/
  2125. static void i40e_sync_filters_subtask(struct i40e_pf *pf)
  2126. {
  2127. int v;
  2128. if (!pf || !(pf->flags & I40E_FLAG_FILTER_SYNC))
  2129. return;
  2130. pf->flags &= ~I40E_FLAG_FILTER_SYNC;
  2131. for (v = 0; v < pf->num_alloc_vsi; v++) {
  2132. if (pf->vsi[v] &&
  2133. (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED)) {
  2134. int ret = i40e_sync_vsi_filters(pf->vsi[v]);
  2135. if (ret) {
  2136. /* come back and try again later */
  2137. pf->flags |= I40E_FLAG_FILTER_SYNC;
  2138. break;
  2139. }
  2140. }
  2141. }
  2142. }
  2143. /**
  2144. * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
  2145. * @netdev: network interface device structure
  2146. * @new_mtu: new value for maximum frame size
  2147. *
  2148. * Returns 0 on success, negative on failure
  2149. **/
  2150. static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
  2151. {
  2152. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2153. struct i40e_vsi *vsi = np->vsi;
  2154. struct i40e_pf *pf = vsi->back;
  2155. netdev_info(netdev, "changing MTU from %d to %d\n",
  2156. netdev->mtu, new_mtu);
  2157. netdev->mtu = new_mtu;
  2158. if (netif_running(netdev))
  2159. i40e_vsi_reinit_locked(vsi);
  2160. pf->flags |= (I40E_FLAG_SERVICE_CLIENT_REQUESTED |
  2161. I40E_FLAG_CLIENT_L2_CHANGE);
  2162. return 0;
  2163. }
  2164. /**
  2165. * i40e_ioctl - Access the hwtstamp interface
  2166. * @netdev: network interface device structure
  2167. * @ifr: interface request data
  2168. * @cmd: ioctl command
  2169. **/
  2170. int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
  2171. {
  2172. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2173. struct i40e_pf *pf = np->vsi->back;
  2174. switch (cmd) {
  2175. case SIOCGHWTSTAMP:
  2176. return i40e_ptp_get_ts_config(pf, ifr);
  2177. case SIOCSHWTSTAMP:
  2178. return i40e_ptp_set_ts_config(pf, ifr);
  2179. default:
  2180. return -EOPNOTSUPP;
  2181. }
  2182. }
  2183. /**
  2184. * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
  2185. * @vsi: the vsi being adjusted
  2186. **/
  2187. void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
  2188. {
  2189. struct i40e_vsi_context ctxt;
  2190. i40e_status ret;
  2191. if ((vsi->info.valid_sections &
  2192. cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
  2193. ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
  2194. return; /* already enabled */
  2195. vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
  2196. vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
  2197. I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
  2198. ctxt.seid = vsi->seid;
  2199. ctxt.info = vsi->info;
  2200. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  2201. if (ret) {
  2202. dev_info(&vsi->back->pdev->dev,
  2203. "update vlan stripping failed, err %s aq_err %s\n",
  2204. i40e_stat_str(&vsi->back->hw, ret),
  2205. i40e_aq_str(&vsi->back->hw,
  2206. vsi->back->hw.aq.asq_last_status));
  2207. }
  2208. }
  2209. /**
  2210. * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
  2211. * @vsi: the vsi being adjusted
  2212. **/
  2213. void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
  2214. {
  2215. struct i40e_vsi_context ctxt;
  2216. i40e_status ret;
  2217. if ((vsi->info.valid_sections &
  2218. cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
  2219. ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
  2220. I40E_AQ_VSI_PVLAN_EMOD_MASK))
  2221. return; /* already disabled */
  2222. vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
  2223. vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
  2224. I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
  2225. ctxt.seid = vsi->seid;
  2226. ctxt.info = vsi->info;
  2227. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  2228. if (ret) {
  2229. dev_info(&vsi->back->pdev->dev,
  2230. "update vlan stripping failed, err %s aq_err %s\n",
  2231. i40e_stat_str(&vsi->back->hw, ret),
  2232. i40e_aq_str(&vsi->back->hw,
  2233. vsi->back->hw.aq.asq_last_status));
  2234. }
  2235. }
  2236. /**
  2237. * i40e_vlan_rx_register - Setup or shutdown vlan offload
  2238. * @netdev: network interface to be adjusted
  2239. * @features: netdev features to test if VLAN offload is enabled or not
  2240. **/
  2241. static void i40e_vlan_rx_register(struct net_device *netdev, u32 features)
  2242. {
  2243. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2244. struct i40e_vsi *vsi = np->vsi;
  2245. if (features & NETIF_F_HW_VLAN_CTAG_RX)
  2246. i40e_vlan_stripping_enable(vsi);
  2247. else
  2248. i40e_vlan_stripping_disable(vsi);
  2249. }
  2250. /**
  2251. * i40e_add_vlan_all_mac - Add a MAC/VLAN filter for each existing MAC address
  2252. * @vsi: the vsi being configured
  2253. * @vid: vlan id to be added (0 = untagged only , -1 = any)
  2254. *
  2255. * This is a helper function for adding a new MAC/VLAN filter with the
  2256. * specified VLAN for each existing MAC address already in the hash table.
  2257. * This function does *not* perform any accounting to update filters based on
  2258. * VLAN mode.
  2259. *
  2260. * NOTE: this function expects to be called while under the
  2261. * mac_filter_hash_lock
  2262. **/
  2263. int i40e_add_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
  2264. {
  2265. struct i40e_mac_filter *f, *add_f;
  2266. struct hlist_node *h;
  2267. int bkt;
  2268. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  2269. if (f->state == I40E_FILTER_REMOVE)
  2270. continue;
  2271. add_f = i40e_add_filter(vsi, f->macaddr, vid);
  2272. if (!add_f) {
  2273. dev_info(&vsi->back->pdev->dev,
  2274. "Could not add vlan filter %d for %pM\n",
  2275. vid, f->macaddr);
  2276. return -ENOMEM;
  2277. }
  2278. }
  2279. return 0;
  2280. }
  2281. /**
  2282. * i40e_vsi_add_vlan - Add VSI membership for given VLAN
  2283. * @vsi: the VSI being configured
  2284. * @vid: VLAN id to be added
  2285. **/
  2286. int i40e_vsi_add_vlan(struct i40e_vsi *vsi, u16 vid)
  2287. {
  2288. int err;
  2289. if (!vid || vsi->info.pvid)
  2290. return -EINVAL;
  2291. /* Locked once because all functions invoked below iterates list*/
  2292. spin_lock_bh(&vsi->mac_filter_hash_lock);
  2293. err = i40e_add_vlan_all_mac(vsi, vid);
  2294. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2295. if (err)
  2296. return err;
  2297. /* schedule our worker thread which will take care of
  2298. * applying the new filter changes
  2299. */
  2300. i40e_service_event_schedule(vsi->back);
  2301. return 0;
  2302. }
  2303. /**
  2304. * i40e_rm_vlan_all_mac - Remove MAC/VLAN pair for all MAC with the given VLAN
  2305. * @vsi: the vsi being configured
  2306. * @vid: vlan id to be removed (0 = untagged only , -1 = any)
  2307. *
  2308. * This function should be used to remove all VLAN filters which match the
  2309. * given VID. It does not schedule the service event and does not take the
  2310. * mac_filter_hash_lock so it may be combined with other operations under
  2311. * a single invocation of the mac_filter_hash_lock.
  2312. *
  2313. * NOTE: this function expects to be called while under the
  2314. * mac_filter_hash_lock
  2315. */
  2316. void i40e_rm_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
  2317. {
  2318. struct i40e_mac_filter *f;
  2319. struct hlist_node *h;
  2320. int bkt;
  2321. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  2322. if (f->vlan == vid)
  2323. __i40e_del_filter(vsi, f);
  2324. }
  2325. }
  2326. /**
  2327. * i40e_vsi_kill_vlan - Remove VSI membership for given VLAN
  2328. * @vsi: the VSI being configured
  2329. * @vid: VLAN id to be removed
  2330. **/
  2331. void i40e_vsi_kill_vlan(struct i40e_vsi *vsi, u16 vid)
  2332. {
  2333. if (!vid || vsi->info.pvid)
  2334. return;
  2335. spin_lock_bh(&vsi->mac_filter_hash_lock);
  2336. i40e_rm_vlan_all_mac(vsi, vid);
  2337. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2338. /* schedule our worker thread which will take care of
  2339. * applying the new filter changes
  2340. */
  2341. i40e_service_event_schedule(vsi->back);
  2342. }
  2343. /**
  2344. * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
  2345. * @netdev: network interface to be adjusted
  2346. * @vid: vlan id to be added
  2347. *
  2348. * net_device_ops implementation for adding vlan ids
  2349. **/
  2350. static int i40e_vlan_rx_add_vid(struct net_device *netdev,
  2351. __always_unused __be16 proto, u16 vid)
  2352. {
  2353. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2354. struct i40e_vsi *vsi = np->vsi;
  2355. int ret = 0;
  2356. if (vid >= VLAN_N_VID)
  2357. return -EINVAL;
  2358. /* If the network stack called us with vid = 0 then
  2359. * it is asking to receive priority tagged packets with
  2360. * vlan id 0. Our HW receives them by default when configured
  2361. * to receive untagged packets so there is no need to add an
  2362. * extra filter for vlan 0 tagged packets.
  2363. */
  2364. if (vid)
  2365. ret = i40e_vsi_add_vlan(vsi, vid);
  2366. if (!ret)
  2367. set_bit(vid, vsi->active_vlans);
  2368. return ret;
  2369. }
  2370. /**
  2371. * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
  2372. * @netdev: network interface to be adjusted
  2373. * @vid: vlan id to be removed
  2374. *
  2375. * net_device_ops implementation for removing vlan ids
  2376. **/
  2377. static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
  2378. __always_unused __be16 proto, u16 vid)
  2379. {
  2380. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2381. struct i40e_vsi *vsi = np->vsi;
  2382. /* return code is ignored as there is nothing a user
  2383. * can do about failure to remove and a log message was
  2384. * already printed from the other function
  2385. */
  2386. i40e_vsi_kill_vlan(vsi, vid);
  2387. clear_bit(vid, vsi->active_vlans);
  2388. return 0;
  2389. }
  2390. /**
  2391. * i40e_macaddr_init - explicitly write the mac address filters
  2392. *
  2393. * @vsi: pointer to the vsi
  2394. * @macaddr: the MAC address
  2395. *
  2396. * This is needed when the macaddr has been obtained by other
  2397. * means than the default, e.g., from Open Firmware or IDPROM.
  2398. * Returns 0 on success, negative on failure
  2399. **/
  2400. static int i40e_macaddr_init(struct i40e_vsi *vsi, u8 *macaddr)
  2401. {
  2402. int ret;
  2403. struct i40e_aqc_add_macvlan_element_data element;
  2404. ret = i40e_aq_mac_address_write(&vsi->back->hw,
  2405. I40E_AQC_WRITE_TYPE_LAA_WOL,
  2406. macaddr, NULL);
  2407. if (ret) {
  2408. dev_info(&vsi->back->pdev->dev,
  2409. "Addr change for VSI failed: %d\n", ret);
  2410. return -EADDRNOTAVAIL;
  2411. }
  2412. memset(&element, 0, sizeof(element));
  2413. ether_addr_copy(element.mac_addr, macaddr);
  2414. element.flags = cpu_to_le16(I40E_AQC_MACVLAN_ADD_PERFECT_MATCH);
  2415. ret = i40e_aq_add_macvlan(&vsi->back->hw, vsi->seid, &element, 1, NULL);
  2416. if (ret) {
  2417. dev_info(&vsi->back->pdev->dev,
  2418. "add filter failed err %s aq_err %s\n",
  2419. i40e_stat_str(&vsi->back->hw, ret),
  2420. i40e_aq_str(&vsi->back->hw,
  2421. vsi->back->hw.aq.asq_last_status));
  2422. }
  2423. return ret;
  2424. }
  2425. /**
  2426. * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
  2427. * @vsi: the vsi being brought back up
  2428. **/
  2429. static void i40e_restore_vlan(struct i40e_vsi *vsi)
  2430. {
  2431. u16 vid;
  2432. if (!vsi->netdev)
  2433. return;
  2434. i40e_vlan_rx_register(vsi->netdev, vsi->netdev->features);
  2435. for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
  2436. i40e_vlan_rx_add_vid(vsi->netdev, htons(ETH_P_8021Q),
  2437. vid);
  2438. }
  2439. /**
  2440. * i40e_vsi_add_pvid - Add pvid for the VSI
  2441. * @vsi: the vsi being adjusted
  2442. * @vid: the vlan id to set as a PVID
  2443. **/
  2444. int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
  2445. {
  2446. struct i40e_vsi_context ctxt;
  2447. i40e_status ret;
  2448. vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
  2449. vsi->info.pvid = cpu_to_le16(vid);
  2450. vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
  2451. I40E_AQ_VSI_PVLAN_INSERT_PVID |
  2452. I40E_AQ_VSI_PVLAN_EMOD_STR;
  2453. ctxt.seid = vsi->seid;
  2454. ctxt.info = vsi->info;
  2455. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  2456. if (ret) {
  2457. dev_info(&vsi->back->pdev->dev,
  2458. "add pvid failed, err %s aq_err %s\n",
  2459. i40e_stat_str(&vsi->back->hw, ret),
  2460. i40e_aq_str(&vsi->back->hw,
  2461. vsi->back->hw.aq.asq_last_status));
  2462. return -ENOENT;
  2463. }
  2464. return 0;
  2465. }
  2466. /**
  2467. * i40e_vsi_remove_pvid - Remove the pvid from the VSI
  2468. * @vsi: the vsi being adjusted
  2469. *
  2470. * Just use the vlan_rx_register() service to put it back to normal
  2471. **/
  2472. void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
  2473. {
  2474. i40e_vlan_stripping_disable(vsi);
  2475. vsi->info.pvid = 0;
  2476. }
  2477. /**
  2478. * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
  2479. * @vsi: ptr to the VSI
  2480. *
  2481. * If this function returns with an error, then it's possible one or
  2482. * more of the rings is populated (while the rest are not). It is the
  2483. * callers duty to clean those orphaned rings.
  2484. *
  2485. * Return 0 on success, negative on failure
  2486. **/
  2487. static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
  2488. {
  2489. int i, err = 0;
  2490. for (i = 0; i < vsi->num_queue_pairs && !err; i++)
  2491. err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
  2492. return err;
  2493. }
  2494. /**
  2495. * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
  2496. * @vsi: ptr to the VSI
  2497. *
  2498. * Free VSI's transmit software resources
  2499. **/
  2500. static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
  2501. {
  2502. int i;
  2503. if (!vsi->tx_rings)
  2504. return;
  2505. for (i = 0; i < vsi->num_queue_pairs; i++)
  2506. if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
  2507. i40e_free_tx_resources(vsi->tx_rings[i]);
  2508. }
  2509. /**
  2510. * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
  2511. * @vsi: ptr to the VSI
  2512. *
  2513. * If this function returns with an error, then it's possible one or
  2514. * more of the rings is populated (while the rest are not). It is the
  2515. * callers duty to clean those orphaned rings.
  2516. *
  2517. * Return 0 on success, negative on failure
  2518. **/
  2519. static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
  2520. {
  2521. int i, err = 0;
  2522. for (i = 0; i < vsi->num_queue_pairs && !err; i++)
  2523. err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
  2524. return err;
  2525. }
  2526. /**
  2527. * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
  2528. * @vsi: ptr to the VSI
  2529. *
  2530. * Free all receive software resources
  2531. **/
  2532. static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
  2533. {
  2534. int i;
  2535. if (!vsi->rx_rings)
  2536. return;
  2537. for (i = 0; i < vsi->num_queue_pairs; i++)
  2538. if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
  2539. i40e_free_rx_resources(vsi->rx_rings[i]);
  2540. }
  2541. /**
  2542. * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
  2543. * @ring: The Tx ring to configure
  2544. *
  2545. * This enables/disables XPS for a given Tx descriptor ring
  2546. * based on the TCs enabled for the VSI that ring belongs to.
  2547. **/
  2548. static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
  2549. {
  2550. struct i40e_vsi *vsi = ring->vsi;
  2551. cpumask_var_t mask;
  2552. if (!ring->q_vector || !ring->netdev)
  2553. return;
  2554. /* Single TC mode enable XPS */
  2555. if (vsi->tc_config.numtc <= 1) {
  2556. if (!test_and_set_bit(__I40E_TX_XPS_INIT_DONE, &ring->state))
  2557. netif_set_xps_queue(ring->netdev,
  2558. &ring->q_vector->affinity_mask,
  2559. ring->queue_index);
  2560. } else if (alloc_cpumask_var(&mask, GFP_KERNEL)) {
  2561. /* Disable XPS to allow selection based on TC */
  2562. bitmap_zero(cpumask_bits(mask), nr_cpumask_bits);
  2563. netif_set_xps_queue(ring->netdev, mask, ring->queue_index);
  2564. free_cpumask_var(mask);
  2565. }
  2566. /* schedule our worker thread which will take care of
  2567. * applying the new filter changes
  2568. */
  2569. i40e_service_event_schedule(vsi->back);
  2570. }
  2571. /**
  2572. * i40e_configure_tx_ring - Configure a transmit ring context and rest
  2573. * @ring: The Tx ring to configure
  2574. *
  2575. * Configure the Tx descriptor ring in the HMC context.
  2576. **/
  2577. static int i40e_configure_tx_ring(struct i40e_ring *ring)
  2578. {
  2579. struct i40e_vsi *vsi = ring->vsi;
  2580. u16 pf_q = vsi->base_queue + ring->queue_index;
  2581. struct i40e_hw *hw = &vsi->back->hw;
  2582. struct i40e_hmc_obj_txq tx_ctx;
  2583. i40e_status err = 0;
  2584. u32 qtx_ctl = 0;
  2585. /* some ATR related tx ring init */
  2586. if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
  2587. ring->atr_sample_rate = vsi->back->atr_sample_rate;
  2588. ring->atr_count = 0;
  2589. } else {
  2590. ring->atr_sample_rate = 0;
  2591. }
  2592. /* configure XPS */
  2593. i40e_config_xps_tx_ring(ring);
  2594. /* clear the context structure first */
  2595. memset(&tx_ctx, 0, sizeof(tx_ctx));
  2596. tx_ctx.new_context = 1;
  2597. tx_ctx.base = (ring->dma / 128);
  2598. tx_ctx.qlen = ring->count;
  2599. tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
  2600. I40E_FLAG_FD_ATR_ENABLED));
  2601. tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
  2602. /* FDIR VSI tx ring can still use RS bit and writebacks */
  2603. if (vsi->type != I40E_VSI_FDIR)
  2604. tx_ctx.head_wb_ena = 1;
  2605. tx_ctx.head_wb_addr = ring->dma +
  2606. (ring->count * sizeof(struct i40e_tx_desc));
  2607. /* As part of VSI creation/update, FW allocates certain
  2608. * Tx arbitration queue sets for each TC enabled for
  2609. * the VSI. The FW returns the handles to these queue
  2610. * sets as part of the response buffer to Add VSI,
  2611. * Update VSI, etc. AQ commands. It is expected that
  2612. * these queue set handles be associated with the Tx
  2613. * queues by the driver as part of the TX queue context
  2614. * initialization. This has to be done regardless of
  2615. * DCB as by default everything is mapped to TC0.
  2616. */
  2617. tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
  2618. tx_ctx.rdylist_act = 0;
  2619. /* clear the context in the HMC */
  2620. err = i40e_clear_lan_tx_queue_context(hw, pf_q);
  2621. if (err) {
  2622. dev_info(&vsi->back->pdev->dev,
  2623. "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
  2624. ring->queue_index, pf_q, err);
  2625. return -ENOMEM;
  2626. }
  2627. /* set the context in the HMC */
  2628. err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
  2629. if (err) {
  2630. dev_info(&vsi->back->pdev->dev,
  2631. "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
  2632. ring->queue_index, pf_q, err);
  2633. return -ENOMEM;
  2634. }
  2635. /* Now associate this queue with this PCI function */
  2636. if (vsi->type == I40E_VSI_VMDQ2) {
  2637. qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
  2638. qtx_ctl |= ((vsi->id) << I40E_QTX_CTL_VFVM_INDX_SHIFT) &
  2639. I40E_QTX_CTL_VFVM_INDX_MASK;
  2640. } else {
  2641. qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
  2642. }
  2643. qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
  2644. I40E_QTX_CTL_PF_INDX_MASK);
  2645. wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
  2646. i40e_flush(hw);
  2647. /* cache tail off for easier writes later */
  2648. ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
  2649. return 0;
  2650. }
  2651. /**
  2652. * i40e_configure_rx_ring - Configure a receive ring context
  2653. * @ring: The Rx ring to configure
  2654. *
  2655. * Configure the Rx descriptor ring in the HMC context.
  2656. **/
  2657. static int i40e_configure_rx_ring(struct i40e_ring *ring)
  2658. {
  2659. struct i40e_vsi *vsi = ring->vsi;
  2660. u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
  2661. u16 pf_q = vsi->base_queue + ring->queue_index;
  2662. struct i40e_hw *hw = &vsi->back->hw;
  2663. struct i40e_hmc_obj_rxq rx_ctx;
  2664. i40e_status err = 0;
  2665. ring->state = 0;
  2666. /* clear the context structure first */
  2667. memset(&rx_ctx, 0, sizeof(rx_ctx));
  2668. ring->rx_buf_len = vsi->rx_buf_len;
  2669. rx_ctx.dbuff = ring->rx_buf_len >> I40E_RXQ_CTX_DBUFF_SHIFT;
  2670. rx_ctx.base = (ring->dma / 128);
  2671. rx_ctx.qlen = ring->count;
  2672. /* use 32 byte descriptors */
  2673. rx_ctx.dsize = 1;
  2674. /* descriptor type is always zero
  2675. * rx_ctx.dtype = 0;
  2676. */
  2677. rx_ctx.hsplit_0 = 0;
  2678. rx_ctx.rxmax = min_t(u16, vsi->max_frame, chain_len * ring->rx_buf_len);
  2679. if (hw->revision_id == 0)
  2680. rx_ctx.lrxqthresh = 0;
  2681. else
  2682. rx_ctx.lrxqthresh = 2;
  2683. rx_ctx.crcstrip = 1;
  2684. rx_ctx.l2tsel = 1;
  2685. /* this controls whether VLAN is stripped from inner headers */
  2686. rx_ctx.showiv = 0;
  2687. /* set the prefena field to 1 because the manual says to */
  2688. rx_ctx.prefena = 1;
  2689. /* clear the context in the HMC */
  2690. err = i40e_clear_lan_rx_queue_context(hw, pf_q);
  2691. if (err) {
  2692. dev_info(&vsi->back->pdev->dev,
  2693. "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
  2694. ring->queue_index, pf_q, err);
  2695. return -ENOMEM;
  2696. }
  2697. /* set the context in the HMC */
  2698. err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
  2699. if (err) {
  2700. dev_info(&vsi->back->pdev->dev,
  2701. "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
  2702. ring->queue_index, pf_q, err);
  2703. return -ENOMEM;
  2704. }
  2705. /* cache tail for quicker writes, and clear the reg before use */
  2706. ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
  2707. writel(0, ring->tail);
  2708. i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
  2709. return 0;
  2710. }
  2711. /**
  2712. * i40e_vsi_configure_tx - Configure the VSI for Tx
  2713. * @vsi: VSI structure describing this set of rings and resources
  2714. *
  2715. * Configure the Tx VSI for operation.
  2716. **/
  2717. static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
  2718. {
  2719. int err = 0;
  2720. u16 i;
  2721. for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
  2722. err = i40e_configure_tx_ring(vsi->tx_rings[i]);
  2723. return err;
  2724. }
  2725. /**
  2726. * i40e_vsi_configure_rx - Configure the VSI for Rx
  2727. * @vsi: the VSI being configured
  2728. *
  2729. * Configure the Rx VSI for operation.
  2730. **/
  2731. static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
  2732. {
  2733. int err = 0;
  2734. u16 i;
  2735. if (vsi->netdev && (vsi->netdev->mtu > ETH_DATA_LEN))
  2736. vsi->max_frame = vsi->netdev->mtu + ETH_HLEN
  2737. + ETH_FCS_LEN + VLAN_HLEN;
  2738. else
  2739. vsi->max_frame = I40E_RXBUFFER_2048;
  2740. vsi->rx_buf_len = I40E_RXBUFFER_2048;
  2741. /* round up for the chip's needs */
  2742. vsi->rx_buf_len = ALIGN(vsi->rx_buf_len,
  2743. BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
  2744. /* set up individual rings */
  2745. for (i = 0; i < vsi->num_queue_pairs && !err; i++)
  2746. err = i40e_configure_rx_ring(vsi->rx_rings[i]);
  2747. return err;
  2748. }
  2749. /**
  2750. * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
  2751. * @vsi: ptr to the VSI
  2752. **/
  2753. static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
  2754. {
  2755. struct i40e_ring *tx_ring, *rx_ring;
  2756. u16 qoffset, qcount;
  2757. int i, n;
  2758. if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
  2759. /* Reset the TC information */
  2760. for (i = 0; i < vsi->num_queue_pairs; i++) {
  2761. rx_ring = vsi->rx_rings[i];
  2762. tx_ring = vsi->tx_rings[i];
  2763. rx_ring->dcb_tc = 0;
  2764. tx_ring->dcb_tc = 0;
  2765. }
  2766. }
  2767. for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
  2768. if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
  2769. continue;
  2770. qoffset = vsi->tc_config.tc_info[n].qoffset;
  2771. qcount = vsi->tc_config.tc_info[n].qcount;
  2772. for (i = qoffset; i < (qoffset + qcount); i++) {
  2773. rx_ring = vsi->rx_rings[i];
  2774. tx_ring = vsi->tx_rings[i];
  2775. rx_ring->dcb_tc = n;
  2776. tx_ring->dcb_tc = n;
  2777. }
  2778. }
  2779. }
  2780. /**
  2781. * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
  2782. * @vsi: ptr to the VSI
  2783. **/
  2784. static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
  2785. {
  2786. struct i40e_pf *pf = vsi->back;
  2787. int err;
  2788. if (vsi->netdev)
  2789. i40e_set_rx_mode(vsi->netdev);
  2790. if (!!(pf->flags & I40E_FLAG_PF_MAC)) {
  2791. err = i40e_macaddr_init(vsi, pf->hw.mac.addr);
  2792. if (err) {
  2793. dev_warn(&pf->pdev->dev,
  2794. "could not set up macaddr; err %d\n", err);
  2795. }
  2796. }
  2797. }
  2798. /**
  2799. * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
  2800. * @vsi: Pointer to the targeted VSI
  2801. *
  2802. * This function replays the hlist on the hw where all the SB Flow Director
  2803. * filters were saved.
  2804. **/
  2805. static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
  2806. {
  2807. struct i40e_fdir_filter *filter;
  2808. struct i40e_pf *pf = vsi->back;
  2809. struct hlist_node *node;
  2810. if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
  2811. return;
  2812. /* Reset FDir counters as we're replaying all existing filters */
  2813. pf->fd_tcp4_filter_cnt = 0;
  2814. pf->fd_udp4_filter_cnt = 0;
  2815. pf->fd_sctp4_filter_cnt = 0;
  2816. pf->fd_ip4_filter_cnt = 0;
  2817. hlist_for_each_entry_safe(filter, node,
  2818. &pf->fdir_filter_list, fdir_node) {
  2819. i40e_add_del_fdir(vsi, filter, true);
  2820. }
  2821. }
  2822. /**
  2823. * i40e_vsi_configure - Set up the VSI for action
  2824. * @vsi: the VSI being configured
  2825. **/
  2826. static int i40e_vsi_configure(struct i40e_vsi *vsi)
  2827. {
  2828. int err;
  2829. i40e_set_vsi_rx_mode(vsi);
  2830. i40e_restore_vlan(vsi);
  2831. i40e_vsi_config_dcb_rings(vsi);
  2832. err = i40e_vsi_configure_tx(vsi);
  2833. if (!err)
  2834. err = i40e_vsi_configure_rx(vsi);
  2835. return err;
  2836. }
  2837. /**
  2838. * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
  2839. * @vsi: the VSI being configured
  2840. **/
  2841. static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
  2842. {
  2843. struct i40e_pf *pf = vsi->back;
  2844. struct i40e_hw *hw = &pf->hw;
  2845. u16 vector;
  2846. int i, q;
  2847. u32 qp;
  2848. /* The interrupt indexing is offset by 1 in the PFINT_ITRn
  2849. * and PFINT_LNKLSTn registers, e.g.:
  2850. * PFINT_ITRn[0..n-1] gets msix-1..msix-n (qpair interrupts)
  2851. */
  2852. qp = vsi->base_queue;
  2853. vector = vsi->base_vector;
  2854. for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
  2855. struct i40e_q_vector *q_vector = vsi->q_vectors[i];
  2856. q_vector->itr_countdown = ITR_COUNTDOWN_START;
  2857. q_vector->rx.itr = ITR_TO_REG(vsi->rx_rings[i]->rx_itr_setting);
  2858. q_vector->rx.latency_range = I40E_LOW_LATENCY;
  2859. wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
  2860. q_vector->rx.itr);
  2861. q_vector->tx.itr = ITR_TO_REG(vsi->tx_rings[i]->tx_itr_setting);
  2862. q_vector->tx.latency_range = I40E_LOW_LATENCY;
  2863. wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
  2864. q_vector->tx.itr);
  2865. wr32(hw, I40E_PFINT_RATEN(vector - 1),
  2866. i40e_intrl_usec_to_reg(vsi->int_rate_limit));
  2867. /* Linked list for the queuepairs assigned to this vector */
  2868. wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
  2869. for (q = 0; q < q_vector->num_ringpairs; q++) {
  2870. u32 val;
  2871. val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
  2872. (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
  2873. (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
  2874. (qp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
  2875. (I40E_QUEUE_TYPE_TX
  2876. << I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
  2877. wr32(hw, I40E_QINT_RQCTL(qp), val);
  2878. val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  2879. (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
  2880. (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
  2881. ((qp+1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT)|
  2882. (I40E_QUEUE_TYPE_RX
  2883. << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
  2884. /* Terminate the linked list */
  2885. if (q == (q_vector->num_ringpairs - 1))
  2886. val |= (I40E_QUEUE_END_OF_LIST
  2887. << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
  2888. wr32(hw, I40E_QINT_TQCTL(qp), val);
  2889. qp++;
  2890. }
  2891. }
  2892. i40e_flush(hw);
  2893. }
  2894. /**
  2895. * i40e_enable_misc_int_causes - enable the non-queue interrupts
  2896. * @hw: ptr to the hardware info
  2897. **/
  2898. static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
  2899. {
  2900. struct i40e_hw *hw = &pf->hw;
  2901. u32 val;
  2902. /* clear things first */
  2903. wr32(hw, I40E_PFINT_ICR0_ENA, 0); /* disable all */
  2904. rd32(hw, I40E_PFINT_ICR0); /* read to clear */
  2905. val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK |
  2906. I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK |
  2907. I40E_PFINT_ICR0_ENA_GRST_MASK |
  2908. I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
  2909. I40E_PFINT_ICR0_ENA_GPIO_MASK |
  2910. I40E_PFINT_ICR0_ENA_HMC_ERR_MASK |
  2911. I40E_PFINT_ICR0_ENA_VFLR_MASK |
  2912. I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
  2913. if (pf->flags & I40E_FLAG_IWARP_ENABLED)
  2914. val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
  2915. if (pf->flags & I40E_FLAG_PTP)
  2916. val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
  2917. wr32(hw, I40E_PFINT_ICR0_ENA, val);
  2918. /* SW_ITR_IDX = 0, but don't change INTENA */
  2919. wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
  2920. I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
  2921. /* OTHER_ITR_IDX = 0 */
  2922. wr32(hw, I40E_PFINT_STAT_CTL0, 0);
  2923. }
  2924. /**
  2925. * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
  2926. * @vsi: the VSI being configured
  2927. **/
  2928. static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
  2929. {
  2930. struct i40e_q_vector *q_vector = vsi->q_vectors[0];
  2931. struct i40e_pf *pf = vsi->back;
  2932. struct i40e_hw *hw = &pf->hw;
  2933. u32 val;
  2934. /* set the ITR configuration */
  2935. q_vector->itr_countdown = ITR_COUNTDOWN_START;
  2936. q_vector->rx.itr = ITR_TO_REG(vsi->rx_rings[0]->rx_itr_setting);
  2937. q_vector->rx.latency_range = I40E_LOW_LATENCY;
  2938. wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.itr);
  2939. q_vector->tx.itr = ITR_TO_REG(vsi->tx_rings[0]->tx_itr_setting);
  2940. q_vector->tx.latency_range = I40E_LOW_LATENCY;
  2941. wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.itr);
  2942. i40e_enable_misc_int_causes(pf);
  2943. /* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
  2944. wr32(hw, I40E_PFINT_LNKLST0, 0);
  2945. /* Associate the queue pair to the vector and enable the queue int */
  2946. val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
  2947. (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
  2948. (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
  2949. wr32(hw, I40E_QINT_RQCTL(0), val);
  2950. val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  2951. (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
  2952. (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
  2953. wr32(hw, I40E_QINT_TQCTL(0), val);
  2954. i40e_flush(hw);
  2955. }
  2956. /**
  2957. * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
  2958. * @pf: board private structure
  2959. **/
  2960. void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
  2961. {
  2962. struct i40e_hw *hw = &pf->hw;
  2963. wr32(hw, I40E_PFINT_DYN_CTL0,
  2964. I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
  2965. i40e_flush(hw);
  2966. }
  2967. /**
  2968. * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
  2969. * @pf: board private structure
  2970. * @clearpba: true when all pending interrupt events should be cleared
  2971. **/
  2972. void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf, bool clearpba)
  2973. {
  2974. struct i40e_hw *hw = &pf->hw;
  2975. u32 val;
  2976. val = I40E_PFINT_DYN_CTL0_INTENA_MASK |
  2977. (clearpba ? I40E_PFINT_DYN_CTL0_CLEARPBA_MASK : 0) |
  2978. (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
  2979. wr32(hw, I40E_PFINT_DYN_CTL0, val);
  2980. i40e_flush(hw);
  2981. }
  2982. /**
  2983. * i40e_msix_clean_rings - MSIX mode Interrupt Handler
  2984. * @irq: interrupt number
  2985. * @data: pointer to a q_vector
  2986. **/
  2987. static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
  2988. {
  2989. struct i40e_q_vector *q_vector = data;
  2990. if (!q_vector->tx.ring && !q_vector->rx.ring)
  2991. return IRQ_HANDLED;
  2992. napi_schedule_irqoff(&q_vector->napi);
  2993. return IRQ_HANDLED;
  2994. }
  2995. /**
  2996. * i40e_irq_affinity_notify - Callback for affinity changes
  2997. * @notify: context as to what irq was changed
  2998. * @mask: the new affinity mask
  2999. *
  3000. * This is a callback function used by the irq_set_affinity_notifier function
  3001. * so that we may register to receive changes to the irq affinity masks.
  3002. **/
  3003. static void i40e_irq_affinity_notify(struct irq_affinity_notify *notify,
  3004. const cpumask_t *mask)
  3005. {
  3006. struct i40e_q_vector *q_vector =
  3007. container_of(notify, struct i40e_q_vector, affinity_notify);
  3008. q_vector->affinity_mask = *mask;
  3009. }
  3010. /**
  3011. * i40e_irq_affinity_release - Callback for affinity notifier release
  3012. * @ref: internal core kernel usage
  3013. *
  3014. * This is a callback function used by the irq_set_affinity_notifier function
  3015. * to inform the current notification subscriber that they will no longer
  3016. * receive notifications.
  3017. **/
  3018. static void i40e_irq_affinity_release(struct kref *ref) {}
  3019. /**
  3020. * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
  3021. * @vsi: the VSI being configured
  3022. * @basename: name for the vector
  3023. *
  3024. * Allocates MSI-X vectors and requests interrupts from the kernel.
  3025. **/
  3026. static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
  3027. {
  3028. int q_vectors = vsi->num_q_vectors;
  3029. struct i40e_pf *pf = vsi->back;
  3030. int base = vsi->base_vector;
  3031. int rx_int_idx = 0;
  3032. int tx_int_idx = 0;
  3033. int vector, err;
  3034. int irq_num;
  3035. for (vector = 0; vector < q_vectors; vector++) {
  3036. struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
  3037. irq_num = pf->msix_entries[base + vector].vector;
  3038. if (q_vector->tx.ring && q_vector->rx.ring) {
  3039. snprintf(q_vector->name, sizeof(q_vector->name) - 1,
  3040. "%s-%s-%d", basename, "TxRx", rx_int_idx++);
  3041. tx_int_idx++;
  3042. } else if (q_vector->rx.ring) {
  3043. snprintf(q_vector->name, sizeof(q_vector->name) - 1,
  3044. "%s-%s-%d", basename, "rx", rx_int_idx++);
  3045. } else if (q_vector->tx.ring) {
  3046. snprintf(q_vector->name, sizeof(q_vector->name) - 1,
  3047. "%s-%s-%d", basename, "tx", tx_int_idx++);
  3048. } else {
  3049. /* skip this unused q_vector */
  3050. continue;
  3051. }
  3052. err = request_irq(irq_num,
  3053. vsi->irq_handler,
  3054. 0,
  3055. q_vector->name,
  3056. q_vector);
  3057. if (err) {
  3058. dev_info(&pf->pdev->dev,
  3059. "MSIX request_irq failed, error: %d\n", err);
  3060. goto free_queue_irqs;
  3061. }
  3062. /* register for affinity change notifications */
  3063. q_vector->affinity_notify.notify = i40e_irq_affinity_notify;
  3064. q_vector->affinity_notify.release = i40e_irq_affinity_release;
  3065. irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
  3066. /* assign the mask for this irq */
  3067. irq_set_affinity_hint(irq_num, &q_vector->affinity_mask);
  3068. }
  3069. vsi->irqs_ready = true;
  3070. return 0;
  3071. free_queue_irqs:
  3072. while (vector) {
  3073. vector--;
  3074. irq_num = pf->msix_entries[base + vector].vector;
  3075. irq_set_affinity_notifier(irq_num, NULL);
  3076. irq_set_affinity_hint(irq_num, NULL);
  3077. free_irq(irq_num, &vsi->q_vectors[vector]);
  3078. }
  3079. return err;
  3080. }
  3081. /**
  3082. * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
  3083. * @vsi: the VSI being un-configured
  3084. **/
  3085. static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
  3086. {
  3087. struct i40e_pf *pf = vsi->back;
  3088. struct i40e_hw *hw = &pf->hw;
  3089. int base = vsi->base_vector;
  3090. int i;
  3091. for (i = 0; i < vsi->num_queue_pairs; i++) {
  3092. wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), 0);
  3093. wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), 0);
  3094. }
  3095. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  3096. for (i = vsi->base_vector;
  3097. i < (vsi->num_q_vectors + vsi->base_vector); i++)
  3098. wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
  3099. i40e_flush(hw);
  3100. for (i = 0; i < vsi->num_q_vectors; i++)
  3101. synchronize_irq(pf->msix_entries[i + base].vector);
  3102. } else {
  3103. /* Legacy and MSI mode - this stops all interrupt handling */
  3104. wr32(hw, I40E_PFINT_ICR0_ENA, 0);
  3105. wr32(hw, I40E_PFINT_DYN_CTL0, 0);
  3106. i40e_flush(hw);
  3107. synchronize_irq(pf->pdev->irq);
  3108. }
  3109. }
  3110. /**
  3111. * i40e_vsi_enable_irq - Enable IRQ for the given VSI
  3112. * @vsi: the VSI being configured
  3113. **/
  3114. static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
  3115. {
  3116. struct i40e_pf *pf = vsi->back;
  3117. int i;
  3118. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  3119. for (i = 0; i < vsi->num_q_vectors; i++)
  3120. i40e_irq_dynamic_enable(vsi, i);
  3121. } else {
  3122. i40e_irq_dynamic_enable_icr0(pf, true);
  3123. }
  3124. i40e_flush(&pf->hw);
  3125. return 0;
  3126. }
  3127. /**
  3128. * i40e_stop_misc_vector - Stop the vector that handles non-queue events
  3129. * @pf: board private structure
  3130. **/
  3131. static void i40e_stop_misc_vector(struct i40e_pf *pf)
  3132. {
  3133. /* Disable ICR 0 */
  3134. wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
  3135. i40e_flush(&pf->hw);
  3136. }
  3137. /**
  3138. * i40e_intr - MSI/Legacy and non-queue interrupt handler
  3139. * @irq: interrupt number
  3140. * @data: pointer to a q_vector
  3141. *
  3142. * This is the handler used for all MSI/Legacy interrupts, and deals
  3143. * with both queue and non-queue interrupts. This is also used in
  3144. * MSIX mode to handle the non-queue interrupts.
  3145. **/
  3146. static irqreturn_t i40e_intr(int irq, void *data)
  3147. {
  3148. struct i40e_pf *pf = (struct i40e_pf *)data;
  3149. struct i40e_hw *hw = &pf->hw;
  3150. irqreturn_t ret = IRQ_NONE;
  3151. u32 icr0, icr0_remaining;
  3152. u32 val, ena_mask;
  3153. icr0 = rd32(hw, I40E_PFINT_ICR0);
  3154. ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
  3155. /* if sharing a legacy IRQ, we might get called w/o an intr pending */
  3156. if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
  3157. goto enable_intr;
  3158. /* if interrupt but no bits showing, must be SWINT */
  3159. if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
  3160. (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
  3161. pf->sw_int_count++;
  3162. if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
  3163. (ena_mask & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
  3164. ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
  3165. icr0 &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
  3166. dev_dbg(&pf->pdev->dev, "cleared PE_CRITERR\n");
  3167. }
  3168. /* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
  3169. if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
  3170. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  3171. struct i40e_q_vector *q_vector = vsi->q_vectors[0];
  3172. /* We do not have a way to disarm Queue causes while leaving
  3173. * interrupt enabled for all other causes, ideally
  3174. * interrupt should be disabled while we are in NAPI but
  3175. * this is not a performance path and napi_schedule()
  3176. * can deal with rescheduling.
  3177. */
  3178. if (!test_bit(__I40E_DOWN, &pf->state))
  3179. napi_schedule_irqoff(&q_vector->napi);
  3180. }
  3181. if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
  3182. ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
  3183. set_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
  3184. i40e_debug(&pf->hw, I40E_DEBUG_NVM, "AdminQ event\n");
  3185. }
  3186. if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
  3187. ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
  3188. set_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
  3189. }
  3190. if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
  3191. ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
  3192. set_bit(__I40E_VFLR_EVENT_PENDING, &pf->state);
  3193. }
  3194. if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
  3195. if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
  3196. set_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
  3197. ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
  3198. val = rd32(hw, I40E_GLGEN_RSTAT);
  3199. val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
  3200. >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
  3201. if (val == I40E_RESET_CORER) {
  3202. pf->corer_count++;
  3203. } else if (val == I40E_RESET_GLOBR) {
  3204. pf->globr_count++;
  3205. } else if (val == I40E_RESET_EMPR) {
  3206. pf->empr_count++;
  3207. set_bit(__I40E_EMP_RESET_INTR_RECEIVED, &pf->state);
  3208. }
  3209. }
  3210. if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
  3211. icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
  3212. dev_info(&pf->pdev->dev, "HMC error interrupt\n");
  3213. dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
  3214. rd32(hw, I40E_PFHMC_ERRORINFO),
  3215. rd32(hw, I40E_PFHMC_ERRORDATA));
  3216. }
  3217. if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
  3218. u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
  3219. if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK) {
  3220. icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
  3221. i40e_ptp_tx_hwtstamp(pf);
  3222. }
  3223. }
  3224. /* If a critical error is pending we have no choice but to reset the
  3225. * device.
  3226. * Report and mask out any remaining unexpected interrupts.
  3227. */
  3228. icr0_remaining = icr0 & ena_mask;
  3229. if (icr0_remaining) {
  3230. dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
  3231. icr0_remaining);
  3232. if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
  3233. (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
  3234. (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
  3235. dev_info(&pf->pdev->dev, "device will be reset\n");
  3236. set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
  3237. i40e_service_event_schedule(pf);
  3238. }
  3239. ena_mask &= ~icr0_remaining;
  3240. }
  3241. ret = IRQ_HANDLED;
  3242. enable_intr:
  3243. /* re-enable interrupt causes */
  3244. wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
  3245. if (!test_bit(__I40E_DOWN, &pf->state)) {
  3246. i40e_service_event_schedule(pf);
  3247. i40e_irq_dynamic_enable_icr0(pf, false);
  3248. }
  3249. return ret;
  3250. }
  3251. /**
  3252. * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
  3253. * @tx_ring: tx ring to clean
  3254. * @budget: how many cleans we're allowed
  3255. *
  3256. * Returns true if there's any budget left (e.g. the clean is finished)
  3257. **/
  3258. static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
  3259. {
  3260. struct i40e_vsi *vsi = tx_ring->vsi;
  3261. u16 i = tx_ring->next_to_clean;
  3262. struct i40e_tx_buffer *tx_buf;
  3263. struct i40e_tx_desc *tx_desc;
  3264. tx_buf = &tx_ring->tx_bi[i];
  3265. tx_desc = I40E_TX_DESC(tx_ring, i);
  3266. i -= tx_ring->count;
  3267. do {
  3268. struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
  3269. /* if next_to_watch is not set then there is no work pending */
  3270. if (!eop_desc)
  3271. break;
  3272. /* prevent any other reads prior to eop_desc */
  3273. read_barrier_depends();
  3274. /* if the descriptor isn't done, no work yet to do */
  3275. if (!(eop_desc->cmd_type_offset_bsz &
  3276. cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
  3277. break;
  3278. /* clear next_to_watch to prevent false hangs */
  3279. tx_buf->next_to_watch = NULL;
  3280. tx_desc->buffer_addr = 0;
  3281. tx_desc->cmd_type_offset_bsz = 0;
  3282. /* move past filter desc */
  3283. tx_buf++;
  3284. tx_desc++;
  3285. i++;
  3286. if (unlikely(!i)) {
  3287. i -= tx_ring->count;
  3288. tx_buf = tx_ring->tx_bi;
  3289. tx_desc = I40E_TX_DESC(tx_ring, 0);
  3290. }
  3291. /* unmap skb header data */
  3292. dma_unmap_single(tx_ring->dev,
  3293. dma_unmap_addr(tx_buf, dma),
  3294. dma_unmap_len(tx_buf, len),
  3295. DMA_TO_DEVICE);
  3296. if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
  3297. kfree(tx_buf->raw_buf);
  3298. tx_buf->raw_buf = NULL;
  3299. tx_buf->tx_flags = 0;
  3300. tx_buf->next_to_watch = NULL;
  3301. dma_unmap_len_set(tx_buf, len, 0);
  3302. tx_desc->buffer_addr = 0;
  3303. tx_desc->cmd_type_offset_bsz = 0;
  3304. /* move us past the eop_desc for start of next FD desc */
  3305. tx_buf++;
  3306. tx_desc++;
  3307. i++;
  3308. if (unlikely(!i)) {
  3309. i -= tx_ring->count;
  3310. tx_buf = tx_ring->tx_bi;
  3311. tx_desc = I40E_TX_DESC(tx_ring, 0);
  3312. }
  3313. /* update budget accounting */
  3314. budget--;
  3315. } while (likely(budget));
  3316. i += tx_ring->count;
  3317. tx_ring->next_to_clean = i;
  3318. if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED)
  3319. i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
  3320. return budget > 0;
  3321. }
  3322. /**
  3323. * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
  3324. * @irq: interrupt number
  3325. * @data: pointer to a q_vector
  3326. **/
  3327. static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
  3328. {
  3329. struct i40e_q_vector *q_vector = data;
  3330. struct i40e_vsi *vsi;
  3331. if (!q_vector->tx.ring)
  3332. return IRQ_HANDLED;
  3333. vsi = q_vector->tx.ring->vsi;
  3334. i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
  3335. return IRQ_HANDLED;
  3336. }
  3337. /**
  3338. * i40e_map_vector_to_qp - Assigns the queue pair to the vector
  3339. * @vsi: the VSI being configured
  3340. * @v_idx: vector index
  3341. * @qp_idx: queue pair index
  3342. **/
  3343. static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
  3344. {
  3345. struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
  3346. struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
  3347. struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
  3348. tx_ring->q_vector = q_vector;
  3349. tx_ring->next = q_vector->tx.ring;
  3350. q_vector->tx.ring = tx_ring;
  3351. q_vector->tx.count++;
  3352. rx_ring->q_vector = q_vector;
  3353. rx_ring->next = q_vector->rx.ring;
  3354. q_vector->rx.ring = rx_ring;
  3355. q_vector->rx.count++;
  3356. }
  3357. /**
  3358. * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
  3359. * @vsi: the VSI being configured
  3360. *
  3361. * This function maps descriptor rings to the queue-specific vectors
  3362. * we were allotted through the MSI-X enabling code. Ideally, we'd have
  3363. * one vector per queue pair, but on a constrained vector budget, we
  3364. * group the queue pairs as "efficiently" as possible.
  3365. **/
  3366. static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
  3367. {
  3368. int qp_remaining = vsi->num_queue_pairs;
  3369. int q_vectors = vsi->num_q_vectors;
  3370. int num_ringpairs;
  3371. int v_start = 0;
  3372. int qp_idx = 0;
  3373. /* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
  3374. * group them so there are multiple queues per vector.
  3375. * It is also important to go through all the vectors available to be
  3376. * sure that if we don't use all the vectors, that the remaining vectors
  3377. * are cleared. This is especially important when decreasing the
  3378. * number of queues in use.
  3379. */
  3380. for (; v_start < q_vectors; v_start++) {
  3381. struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
  3382. num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
  3383. q_vector->num_ringpairs = num_ringpairs;
  3384. q_vector->rx.count = 0;
  3385. q_vector->tx.count = 0;
  3386. q_vector->rx.ring = NULL;
  3387. q_vector->tx.ring = NULL;
  3388. while (num_ringpairs--) {
  3389. i40e_map_vector_to_qp(vsi, v_start, qp_idx);
  3390. qp_idx++;
  3391. qp_remaining--;
  3392. }
  3393. }
  3394. }
  3395. /**
  3396. * i40e_vsi_request_irq - Request IRQ from the OS
  3397. * @vsi: the VSI being configured
  3398. * @basename: name for the vector
  3399. **/
  3400. static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
  3401. {
  3402. struct i40e_pf *pf = vsi->back;
  3403. int err;
  3404. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  3405. err = i40e_vsi_request_irq_msix(vsi, basename);
  3406. else if (pf->flags & I40E_FLAG_MSI_ENABLED)
  3407. err = request_irq(pf->pdev->irq, i40e_intr, 0,
  3408. pf->int_name, pf);
  3409. else
  3410. err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
  3411. pf->int_name, pf);
  3412. if (err)
  3413. dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
  3414. return err;
  3415. }
  3416. #ifdef CONFIG_NET_POLL_CONTROLLER
  3417. /**
  3418. * i40e_netpoll - A Polling 'interrupt' handler
  3419. * @netdev: network interface device structure
  3420. *
  3421. * This is used by netconsole to send skbs without having to re-enable
  3422. * interrupts. It's not called while the normal interrupt routine is executing.
  3423. **/
  3424. static void i40e_netpoll(struct net_device *netdev)
  3425. {
  3426. struct i40e_netdev_priv *np = netdev_priv(netdev);
  3427. struct i40e_vsi *vsi = np->vsi;
  3428. struct i40e_pf *pf = vsi->back;
  3429. int i;
  3430. /* if interface is down do nothing */
  3431. if (test_bit(__I40E_DOWN, &vsi->state))
  3432. return;
  3433. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  3434. for (i = 0; i < vsi->num_q_vectors; i++)
  3435. i40e_msix_clean_rings(0, vsi->q_vectors[i]);
  3436. } else {
  3437. i40e_intr(pf->pdev->irq, netdev);
  3438. }
  3439. }
  3440. #endif
  3441. /**
  3442. * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
  3443. * @pf: the PF being configured
  3444. * @pf_q: the PF queue
  3445. * @enable: enable or disable state of the queue
  3446. *
  3447. * This routine will wait for the given Tx queue of the PF to reach the
  3448. * enabled or disabled state.
  3449. * Returns -ETIMEDOUT in case of failing to reach the requested state after
  3450. * multiple retries; else will return 0 in case of success.
  3451. **/
  3452. static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
  3453. {
  3454. int i;
  3455. u32 tx_reg;
  3456. for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
  3457. tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
  3458. if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
  3459. break;
  3460. usleep_range(10, 20);
  3461. }
  3462. if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
  3463. return -ETIMEDOUT;
  3464. return 0;
  3465. }
  3466. /**
  3467. * i40e_vsi_control_tx - Start or stop a VSI's rings
  3468. * @vsi: the VSI being configured
  3469. * @enable: start or stop the rings
  3470. **/
  3471. static int i40e_vsi_control_tx(struct i40e_vsi *vsi, bool enable)
  3472. {
  3473. struct i40e_pf *pf = vsi->back;
  3474. struct i40e_hw *hw = &pf->hw;
  3475. int i, j, pf_q, ret = 0;
  3476. u32 tx_reg;
  3477. pf_q = vsi->base_queue;
  3478. for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
  3479. /* warn the TX unit of coming changes */
  3480. i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
  3481. if (!enable)
  3482. usleep_range(10, 20);
  3483. for (j = 0; j < 50; j++) {
  3484. tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
  3485. if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
  3486. ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
  3487. break;
  3488. usleep_range(1000, 2000);
  3489. }
  3490. /* Skip if the queue is already in the requested state */
  3491. if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
  3492. continue;
  3493. /* turn on/off the queue */
  3494. if (enable) {
  3495. wr32(hw, I40E_QTX_HEAD(pf_q), 0);
  3496. tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
  3497. } else {
  3498. tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
  3499. }
  3500. wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
  3501. /* No waiting for the Tx queue to disable */
  3502. if (!enable && test_bit(__I40E_PORT_TX_SUSPENDED, &pf->state))
  3503. continue;
  3504. /* wait for the change to finish */
  3505. ret = i40e_pf_txq_wait(pf, pf_q, enable);
  3506. if (ret) {
  3507. dev_info(&pf->pdev->dev,
  3508. "VSI seid %d Tx ring %d %sable timeout\n",
  3509. vsi->seid, pf_q, (enable ? "en" : "dis"));
  3510. break;
  3511. }
  3512. }
  3513. return ret;
  3514. }
  3515. /**
  3516. * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
  3517. * @pf: the PF being configured
  3518. * @pf_q: the PF queue
  3519. * @enable: enable or disable state of the queue
  3520. *
  3521. * This routine will wait for the given Rx queue of the PF to reach the
  3522. * enabled or disabled state.
  3523. * Returns -ETIMEDOUT in case of failing to reach the requested state after
  3524. * multiple retries; else will return 0 in case of success.
  3525. **/
  3526. static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
  3527. {
  3528. int i;
  3529. u32 rx_reg;
  3530. for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
  3531. rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
  3532. if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
  3533. break;
  3534. usleep_range(10, 20);
  3535. }
  3536. if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
  3537. return -ETIMEDOUT;
  3538. return 0;
  3539. }
  3540. /**
  3541. * i40e_vsi_control_rx - Start or stop a VSI's rings
  3542. * @vsi: the VSI being configured
  3543. * @enable: start or stop the rings
  3544. **/
  3545. static int i40e_vsi_control_rx(struct i40e_vsi *vsi, bool enable)
  3546. {
  3547. struct i40e_pf *pf = vsi->back;
  3548. struct i40e_hw *hw = &pf->hw;
  3549. int i, j, pf_q, ret = 0;
  3550. u32 rx_reg;
  3551. pf_q = vsi->base_queue;
  3552. for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
  3553. for (j = 0; j < 50; j++) {
  3554. rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
  3555. if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
  3556. ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
  3557. break;
  3558. usleep_range(1000, 2000);
  3559. }
  3560. /* Skip if the queue is already in the requested state */
  3561. if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
  3562. continue;
  3563. /* turn on/off the queue */
  3564. if (enable)
  3565. rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
  3566. else
  3567. rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
  3568. wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
  3569. /* No waiting for the Tx queue to disable */
  3570. if (!enable && test_bit(__I40E_PORT_TX_SUSPENDED, &pf->state))
  3571. continue;
  3572. /* wait for the change to finish */
  3573. ret = i40e_pf_rxq_wait(pf, pf_q, enable);
  3574. if (ret) {
  3575. dev_info(&pf->pdev->dev,
  3576. "VSI seid %d Rx ring %d %sable timeout\n",
  3577. vsi->seid, pf_q, (enable ? "en" : "dis"));
  3578. break;
  3579. }
  3580. }
  3581. return ret;
  3582. }
  3583. /**
  3584. * i40e_vsi_start_rings - Start a VSI's rings
  3585. * @vsi: the VSI being configured
  3586. **/
  3587. int i40e_vsi_start_rings(struct i40e_vsi *vsi)
  3588. {
  3589. int ret = 0;
  3590. /* do rx first for enable and last for disable */
  3591. ret = i40e_vsi_control_rx(vsi, true);
  3592. if (ret)
  3593. return ret;
  3594. ret = i40e_vsi_control_tx(vsi, true);
  3595. return ret;
  3596. }
  3597. /**
  3598. * i40e_vsi_stop_rings - Stop a VSI's rings
  3599. * @vsi: the VSI being configured
  3600. **/
  3601. void i40e_vsi_stop_rings(struct i40e_vsi *vsi)
  3602. {
  3603. /* do rx first for enable and last for disable
  3604. * Ignore return value, we need to shutdown whatever we can
  3605. */
  3606. i40e_vsi_control_tx(vsi, false);
  3607. i40e_vsi_control_rx(vsi, false);
  3608. }
  3609. /**
  3610. * i40e_vsi_free_irq - Free the irq association with the OS
  3611. * @vsi: the VSI being configured
  3612. **/
  3613. static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
  3614. {
  3615. struct i40e_pf *pf = vsi->back;
  3616. struct i40e_hw *hw = &pf->hw;
  3617. int base = vsi->base_vector;
  3618. u32 val, qp;
  3619. int i;
  3620. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  3621. if (!vsi->q_vectors)
  3622. return;
  3623. if (!vsi->irqs_ready)
  3624. return;
  3625. vsi->irqs_ready = false;
  3626. for (i = 0; i < vsi->num_q_vectors; i++) {
  3627. int irq_num;
  3628. u16 vector;
  3629. vector = i + base;
  3630. irq_num = pf->msix_entries[vector].vector;
  3631. /* free only the irqs that were actually requested */
  3632. if (!vsi->q_vectors[i] ||
  3633. !vsi->q_vectors[i]->num_ringpairs)
  3634. continue;
  3635. /* clear the affinity notifier in the IRQ descriptor */
  3636. irq_set_affinity_notifier(irq_num, NULL);
  3637. /* clear the affinity_mask in the IRQ descriptor */
  3638. irq_set_affinity_hint(irq_num, NULL);
  3639. synchronize_irq(irq_num);
  3640. free_irq(irq_num, vsi->q_vectors[i]);
  3641. /* Tear down the interrupt queue link list
  3642. *
  3643. * We know that they come in pairs and always
  3644. * the Rx first, then the Tx. To clear the
  3645. * link list, stick the EOL value into the
  3646. * next_q field of the registers.
  3647. */
  3648. val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
  3649. qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
  3650. >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
  3651. val |= I40E_QUEUE_END_OF_LIST
  3652. << I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
  3653. wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
  3654. while (qp != I40E_QUEUE_END_OF_LIST) {
  3655. u32 next;
  3656. val = rd32(hw, I40E_QINT_RQCTL(qp));
  3657. val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
  3658. I40E_QINT_RQCTL_MSIX0_INDX_MASK |
  3659. I40E_QINT_RQCTL_CAUSE_ENA_MASK |
  3660. I40E_QINT_RQCTL_INTEVENT_MASK);
  3661. val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
  3662. I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
  3663. wr32(hw, I40E_QINT_RQCTL(qp), val);
  3664. val = rd32(hw, I40E_QINT_TQCTL(qp));
  3665. next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
  3666. >> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
  3667. val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
  3668. I40E_QINT_TQCTL_MSIX0_INDX_MASK |
  3669. I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  3670. I40E_QINT_TQCTL_INTEVENT_MASK);
  3671. val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
  3672. I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
  3673. wr32(hw, I40E_QINT_TQCTL(qp), val);
  3674. qp = next;
  3675. }
  3676. }
  3677. } else {
  3678. free_irq(pf->pdev->irq, pf);
  3679. val = rd32(hw, I40E_PFINT_LNKLST0);
  3680. qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
  3681. >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
  3682. val |= I40E_QUEUE_END_OF_LIST
  3683. << I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
  3684. wr32(hw, I40E_PFINT_LNKLST0, val);
  3685. val = rd32(hw, I40E_QINT_RQCTL(qp));
  3686. val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
  3687. I40E_QINT_RQCTL_MSIX0_INDX_MASK |
  3688. I40E_QINT_RQCTL_CAUSE_ENA_MASK |
  3689. I40E_QINT_RQCTL_INTEVENT_MASK);
  3690. val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
  3691. I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
  3692. wr32(hw, I40E_QINT_RQCTL(qp), val);
  3693. val = rd32(hw, I40E_QINT_TQCTL(qp));
  3694. val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
  3695. I40E_QINT_TQCTL_MSIX0_INDX_MASK |
  3696. I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  3697. I40E_QINT_TQCTL_INTEVENT_MASK);
  3698. val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
  3699. I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
  3700. wr32(hw, I40E_QINT_TQCTL(qp), val);
  3701. }
  3702. }
  3703. /**
  3704. * i40e_free_q_vector - Free memory allocated for specific interrupt vector
  3705. * @vsi: the VSI being configured
  3706. * @v_idx: Index of vector to be freed
  3707. *
  3708. * This function frees the memory allocated to the q_vector. In addition if
  3709. * NAPI is enabled it will delete any references to the NAPI struct prior
  3710. * to freeing the q_vector.
  3711. **/
  3712. static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
  3713. {
  3714. struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
  3715. struct i40e_ring *ring;
  3716. if (!q_vector)
  3717. return;
  3718. /* disassociate q_vector from rings */
  3719. i40e_for_each_ring(ring, q_vector->tx)
  3720. ring->q_vector = NULL;
  3721. i40e_for_each_ring(ring, q_vector->rx)
  3722. ring->q_vector = NULL;
  3723. /* only VSI w/ an associated netdev is set up w/ NAPI */
  3724. if (vsi->netdev)
  3725. netif_napi_del(&q_vector->napi);
  3726. vsi->q_vectors[v_idx] = NULL;
  3727. kfree_rcu(q_vector, rcu);
  3728. }
  3729. /**
  3730. * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
  3731. * @vsi: the VSI being un-configured
  3732. *
  3733. * This frees the memory allocated to the q_vectors and
  3734. * deletes references to the NAPI struct.
  3735. **/
  3736. static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
  3737. {
  3738. int v_idx;
  3739. for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
  3740. i40e_free_q_vector(vsi, v_idx);
  3741. }
  3742. /**
  3743. * i40e_reset_interrupt_capability - Disable interrupt setup in OS
  3744. * @pf: board private structure
  3745. **/
  3746. static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
  3747. {
  3748. /* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
  3749. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  3750. pci_disable_msix(pf->pdev);
  3751. kfree(pf->msix_entries);
  3752. pf->msix_entries = NULL;
  3753. kfree(pf->irq_pile);
  3754. pf->irq_pile = NULL;
  3755. } else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
  3756. pci_disable_msi(pf->pdev);
  3757. }
  3758. pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
  3759. }
  3760. /**
  3761. * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
  3762. * @pf: board private structure
  3763. *
  3764. * We go through and clear interrupt specific resources and reset the structure
  3765. * to pre-load conditions
  3766. **/
  3767. static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
  3768. {
  3769. int i;
  3770. i40e_stop_misc_vector(pf);
  3771. if (pf->flags & I40E_FLAG_MSIX_ENABLED && pf->msix_entries) {
  3772. synchronize_irq(pf->msix_entries[0].vector);
  3773. free_irq(pf->msix_entries[0].vector, pf);
  3774. }
  3775. i40e_put_lump(pf->irq_pile, pf->iwarp_base_vector,
  3776. I40E_IWARP_IRQ_PILE_ID);
  3777. i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
  3778. for (i = 0; i < pf->num_alloc_vsi; i++)
  3779. if (pf->vsi[i])
  3780. i40e_vsi_free_q_vectors(pf->vsi[i]);
  3781. i40e_reset_interrupt_capability(pf);
  3782. }
  3783. /**
  3784. * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
  3785. * @vsi: the VSI being configured
  3786. **/
  3787. static void i40e_napi_enable_all(struct i40e_vsi *vsi)
  3788. {
  3789. int q_idx;
  3790. if (!vsi->netdev)
  3791. return;
  3792. for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
  3793. napi_enable(&vsi->q_vectors[q_idx]->napi);
  3794. }
  3795. /**
  3796. * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
  3797. * @vsi: the VSI being configured
  3798. **/
  3799. static void i40e_napi_disable_all(struct i40e_vsi *vsi)
  3800. {
  3801. int q_idx;
  3802. if (!vsi->netdev)
  3803. return;
  3804. for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
  3805. napi_disable(&vsi->q_vectors[q_idx]->napi);
  3806. }
  3807. /**
  3808. * i40e_vsi_close - Shut down a VSI
  3809. * @vsi: the vsi to be quelled
  3810. **/
  3811. static void i40e_vsi_close(struct i40e_vsi *vsi)
  3812. {
  3813. struct i40e_pf *pf = vsi->back;
  3814. if (!test_and_set_bit(__I40E_DOWN, &vsi->state))
  3815. i40e_down(vsi);
  3816. i40e_vsi_free_irq(vsi);
  3817. i40e_vsi_free_tx_resources(vsi);
  3818. i40e_vsi_free_rx_resources(vsi);
  3819. vsi->current_netdev_flags = 0;
  3820. pf->flags |= I40E_FLAG_SERVICE_CLIENT_REQUESTED;
  3821. if (test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
  3822. pf->flags |= I40E_FLAG_CLIENT_RESET;
  3823. }
  3824. /**
  3825. * i40e_quiesce_vsi - Pause a given VSI
  3826. * @vsi: the VSI being paused
  3827. **/
  3828. static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
  3829. {
  3830. if (test_bit(__I40E_DOWN, &vsi->state))
  3831. return;
  3832. set_bit(__I40E_NEEDS_RESTART, &vsi->state);
  3833. if (vsi->netdev && netif_running(vsi->netdev))
  3834. vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
  3835. else
  3836. i40e_vsi_close(vsi);
  3837. }
  3838. /**
  3839. * i40e_unquiesce_vsi - Resume a given VSI
  3840. * @vsi: the VSI being resumed
  3841. **/
  3842. static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
  3843. {
  3844. if (!test_bit(__I40E_NEEDS_RESTART, &vsi->state))
  3845. return;
  3846. clear_bit(__I40E_NEEDS_RESTART, &vsi->state);
  3847. if (vsi->netdev && netif_running(vsi->netdev))
  3848. vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
  3849. else
  3850. i40e_vsi_open(vsi); /* this clears the DOWN bit */
  3851. }
  3852. /**
  3853. * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
  3854. * @pf: the PF
  3855. **/
  3856. static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
  3857. {
  3858. int v;
  3859. for (v = 0; v < pf->num_alloc_vsi; v++) {
  3860. if (pf->vsi[v])
  3861. i40e_quiesce_vsi(pf->vsi[v]);
  3862. }
  3863. }
  3864. /**
  3865. * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
  3866. * @pf: the PF
  3867. **/
  3868. static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
  3869. {
  3870. int v;
  3871. for (v = 0; v < pf->num_alloc_vsi; v++) {
  3872. if (pf->vsi[v])
  3873. i40e_unquiesce_vsi(pf->vsi[v]);
  3874. }
  3875. }
  3876. #ifdef CONFIG_I40E_DCB
  3877. /**
  3878. * i40e_vsi_wait_queues_disabled - Wait for VSI's queues to be disabled
  3879. * @vsi: the VSI being configured
  3880. *
  3881. * This function waits for the given VSI's queues to be disabled.
  3882. **/
  3883. static int i40e_vsi_wait_queues_disabled(struct i40e_vsi *vsi)
  3884. {
  3885. struct i40e_pf *pf = vsi->back;
  3886. int i, pf_q, ret;
  3887. pf_q = vsi->base_queue;
  3888. for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
  3889. /* Check and wait for the disable status of the queue */
  3890. ret = i40e_pf_txq_wait(pf, pf_q, false);
  3891. if (ret) {
  3892. dev_info(&pf->pdev->dev,
  3893. "VSI seid %d Tx ring %d disable timeout\n",
  3894. vsi->seid, pf_q);
  3895. return ret;
  3896. }
  3897. }
  3898. pf_q = vsi->base_queue;
  3899. for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
  3900. /* Check and wait for the disable status of the queue */
  3901. ret = i40e_pf_rxq_wait(pf, pf_q, false);
  3902. if (ret) {
  3903. dev_info(&pf->pdev->dev,
  3904. "VSI seid %d Rx ring %d disable timeout\n",
  3905. vsi->seid, pf_q);
  3906. return ret;
  3907. }
  3908. }
  3909. return 0;
  3910. }
  3911. /**
  3912. * i40e_pf_wait_queues_disabled - Wait for all queues of PF VSIs to be disabled
  3913. * @pf: the PF
  3914. *
  3915. * This function waits for the queues to be in disabled state for all the
  3916. * VSIs that are managed by this PF.
  3917. **/
  3918. static int i40e_pf_wait_queues_disabled(struct i40e_pf *pf)
  3919. {
  3920. int v, ret = 0;
  3921. for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
  3922. if (pf->vsi[v]) {
  3923. ret = i40e_vsi_wait_queues_disabled(pf->vsi[v]);
  3924. if (ret)
  3925. break;
  3926. }
  3927. }
  3928. return ret;
  3929. }
  3930. #endif
  3931. /**
  3932. * i40e_detect_recover_hung_queue - Function to detect and recover hung_queue
  3933. * @q_idx: TX queue number
  3934. * @vsi: Pointer to VSI struct
  3935. *
  3936. * This function checks specified queue for given VSI. Detects hung condition.
  3937. * Sets hung bit since it is two step process. Before next run of service task
  3938. * if napi_poll runs, it reset 'hung' bit for respective q_vector. If not,
  3939. * hung condition remain unchanged and during subsequent run, this function
  3940. * issues SW interrupt to recover from hung condition.
  3941. **/
  3942. static void i40e_detect_recover_hung_queue(int q_idx, struct i40e_vsi *vsi)
  3943. {
  3944. struct i40e_ring *tx_ring = NULL;
  3945. struct i40e_pf *pf;
  3946. u32 head, val, tx_pending_hw;
  3947. int i;
  3948. pf = vsi->back;
  3949. /* now that we have an index, find the tx_ring struct */
  3950. for (i = 0; i < vsi->num_queue_pairs; i++) {
  3951. if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
  3952. if (q_idx == vsi->tx_rings[i]->queue_index) {
  3953. tx_ring = vsi->tx_rings[i];
  3954. break;
  3955. }
  3956. }
  3957. }
  3958. if (!tx_ring)
  3959. return;
  3960. /* Read interrupt register */
  3961. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  3962. val = rd32(&pf->hw,
  3963. I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
  3964. tx_ring->vsi->base_vector - 1));
  3965. else
  3966. val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
  3967. head = i40e_get_head(tx_ring);
  3968. tx_pending_hw = i40e_get_tx_pending(tx_ring, false);
  3969. /* HW is done executing descriptors, updated HEAD write back,
  3970. * but SW hasn't processed those descriptors. If interrupt is
  3971. * not generated from this point ON, it could result into
  3972. * dev_watchdog detecting timeout on those netdev_queue,
  3973. * hence proactively trigger SW interrupt.
  3974. */
  3975. if (tx_pending_hw && (!(val & I40E_PFINT_DYN_CTLN_INTENA_MASK))) {
  3976. /* NAPI Poll didn't run and clear since it was set */
  3977. if (test_and_clear_bit(I40E_Q_VECTOR_HUNG_DETECT,
  3978. &tx_ring->q_vector->hung_detected)) {
  3979. netdev_info(vsi->netdev, "VSI_seid %d, Hung TX queue %d, tx_pending_hw: %d, NTC:0x%x, HWB: 0x%x, NTU: 0x%x, TAIL: 0x%x\n",
  3980. vsi->seid, q_idx, tx_pending_hw,
  3981. tx_ring->next_to_clean, head,
  3982. tx_ring->next_to_use,
  3983. readl(tx_ring->tail));
  3984. netdev_info(vsi->netdev, "VSI_seid %d, Issuing force_wb for TX queue %d, Interrupt Reg: 0x%x\n",
  3985. vsi->seid, q_idx, val);
  3986. i40e_force_wb(vsi, tx_ring->q_vector);
  3987. } else {
  3988. /* First Chance - detected possible hung */
  3989. set_bit(I40E_Q_VECTOR_HUNG_DETECT,
  3990. &tx_ring->q_vector->hung_detected);
  3991. }
  3992. }
  3993. /* This is the case where we have interrupts missing,
  3994. * so the tx_pending in HW will most likely be 0, but we
  3995. * will have tx_pending in SW since the WB happened but the
  3996. * interrupt got lost.
  3997. */
  3998. if ((!tx_pending_hw) && i40e_get_tx_pending(tx_ring, true) &&
  3999. (!(val & I40E_PFINT_DYN_CTLN_INTENA_MASK))) {
  4000. local_bh_disable();
  4001. if (napi_reschedule(&tx_ring->q_vector->napi))
  4002. tx_ring->tx_stats.tx_lost_interrupt++;
  4003. local_bh_enable();
  4004. }
  4005. }
  4006. /**
  4007. * i40e_detect_recover_hung - Function to detect and recover hung_queues
  4008. * @pf: pointer to PF struct
  4009. *
  4010. * LAN VSI has netdev and netdev has TX queues. This function is to check
  4011. * each of those TX queues if they are hung, trigger recovery by issuing
  4012. * SW interrupt.
  4013. **/
  4014. static void i40e_detect_recover_hung(struct i40e_pf *pf)
  4015. {
  4016. struct net_device *netdev;
  4017. struct i40e_vsi *vsi;
  4018. int i;
  4019. /* Only for LAN VSI */
  4020. vsi = pf->vsi[pf->lan_vsi];
  4021. if (!vsi)
  4022. return;
  4023. /* Make sure, VSI state is not DOWN/RECOVERY_PENDING */
  4024. if (test_bit(__I40E_DOWN, &vsi->back->state) ||
  4025. test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
  4026. return;
  4027. /* Make sure type is MAIN VSI */
  4028. if (vsi->type != I40E_VSI_MAIN)
  4029. return;
  4030. netdev = vsi->netdev;
  4031. if (!netdev)
  4032. return;
  4033. /* Bail out if netif_carrier is not OK */
  4034. if (!netif_carrier_ok(netdev))
  4035. return;
  4036. /* Go thru' TX queues for netdev */
  4037. for (i = 0; i < netdev->num_tx_queues; i++) {
  4038. struct netdev_queue *q;
  4039. q = netdev_get_tx_queue(netdev, i);
  4040. if (q)
  4041. i40e_detect_recover_hung_queue(i, vsi);
  4042. }
  4043. }
  4044. /**
  4045. * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
  4046. * @pf: pointer to PF
  4047. *
  4048. * Get TC map for ISCSI PF type that will include iSCSI TC
  4049. * and LAN TC.
  4050. **/
  4051. static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
  4052. {
  4053. struct i40e_dcb_app_priority_table app;
  4054. struct i40e_hw *hw = &pf->hw;
  4055. u8 enabled_tc = 1; /* TC0 is always enabled */
  4056. u8 tc, i;
  4057. /* Get the iSCSI APP TLV */
  4058. struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
  4059. for (i = 0; i < dcbcfg->numapps; i++) {
  4060. app = dcbcfg->app[i];
  4061. if (app.selector == I40E_APP_SEL_TCPIP &&
  4062. app.protocolid == I40E_APP_PROTOID_ISCSI) {
  4063. tc = dcbcfg->etscfg.prioritytable[app.priority];
  4064. enabled_tc |= BIT(tc);
  4065. break;
  4066. }
  4067. }
  4068. return enabled_tc;
  4069. }
  4070. /**
  4071. * i40e_dcb_get_num_tc - Get the number of TCs from DCBx config
  4072. * @dcbcfg: the corresponding DCBx configuration structure
  4073. *
  4074. * Return the number of TCs from given DCBx configuration
  4075. **/
  4076. static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
  4077. {
  4078. int i, tc_unused = 0;
  4079. u8 num_tc = 0;
  4080. u8 ret = 0;
  4081. /* Scan the ETS Config Priority Table to find
  4082. * traffic class enabled for a given priority
  4083. * and create a bitmask of enabled TCs
  4084. */
  4085. for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
  4086. num_tc |= BIT(dcbcfg->etscfg.prioritytable[i]);
  4087. /* Now scan the bitmask to check for
  4088. * contiguous TCs starting with TC0
  4089. */
  4090. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4091. if (num_tc & BIT(i)) {
  4092. if (!tc_unused) {
  4093. ret++;
  4094. } else {
  4095. pr_err("Non-contiguous TC - Disabling DCB\n");
  4096. return 1;
  4097. }
  4098. } else {
  4099. tc_unused = 1;
  4100. }
  4101. }
  4102. /* There is always at least TC0 */
  4103. if (!ret)
  4104. ret = 1;
  4105. return ret;
  4106. }
  4107. /**
  4108. * i40e_dcb_get_enabled_tc - Get enabled traffic classes
  4109. * @dcbcfg: the corresponding DCBx configuration structure
  4110. *
  4111. * Query the current DCB configuration and return the number of
  4112. * traffic classes enabled from the given DCBX config
  4113. **/
  4114. static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
  4115. {
  4116. u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
  4117. u8 enabled_tc = 1;
  4118. u8 i;
  4119. for (i = 0; i < num_tc; i++)
  4120. enabled_tc |= BIT(i);
  4121. return enabled_tc;
  4122. }
  4123. /**
  4124. * i40e_pf_get_num_tc - Get enabled traffic classes for PF
  4125. * @pf: PF being queried
  4126. *
  4127. * Return number of traffic classes enabled for the given PF
  4128. **/
  4129. static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
  4130. {
  4131. struct i40e_hw *hw = &pf->hw;
  4132. u8 i, enabled_tc = 1;
  4133. u8 num_tc = 0;
  4134. struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
  4135. /* If DCB is not enabled then always in single TC */
  4136. if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
  4137. return 1;
  4138. /* SFP mode will be enabled for all TCs on port */
  4139. if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
  4140. return i40e_dcb_get_num_tc(dcbcfg);
  4141. /* MFP mode return count of enabled TCs for this PF */
  4142. if (pf->hw.func_caps.iscsi)
  4143. enabled_tc = i40e_get_iscsi_tc_map(pf);
  4144. else
  4145. return 1; /* Only TC0 */
  4146. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4147. if (enabled_tc & BIT(i))
  4148. num_tc++;
  4149. }
  4150. return num_tc;
  4151. }
  4152. /**
  4153. * i40e_pf_get_pf_tc_map - Get bitmap for enabled traffic classes
  4154. * @pf: PF being queried
  4155. *
  4156. * Return a bitmap for enabled traffic classes for this PF.
  4157. **/
  4158. static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
  4159. {
  4160. /* If DCB is not enabled for this PF then just return default TC */
  4161. if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
  4162. return I40E_DEFAULT_TRAFFIC_CLASS;
  4163. /* SFP mode we want PF to be enabled for all TCs */
  4164. if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
  4165. return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
  4166. /* MFP enabled and iSCSI PF type */
  4167. if (pf->hw.func_caps.iscsi)
  4168. return i40e_get_iscsi_tc_map(pf);
  4169. else
  4170. return I40E_DEFAULT_TRAFFIC_CLASS;
  4171. }
  4172. /**
  4173. * i40e_vsi_get_bw_info - Query VSI BW Information
  4174. * @vsi: the VSI being queried
  4175. *
  4176. * Returns 0 on success, negative value on failure
  4177. **/
  4178. static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
  4179. {
  4180. struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
  4181. struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
  4182. struct i40e_pf *pf = vsi->back;
  4183. struct i40e_hw *hw = &pf->hw;
  4184. i40e_status ret;
  4185. u32 tc_bw_max;
  4186. int i;
  4187. /* Get the VSI level BW configuration */
  4188. ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
  4189. if (ret) {
  4190. dev_info(&pf->pdev->dev,
  4191. "couldn't get PF vsi bw config, err %s aq_err %s\n",
  4192. i40e_stat_str(&pf->hw, ret),
  4193. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  4194. return -EINVAL;
  4195. }
  4196. /* Get the VSI level BW configuration per TC */
  4197. ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
  4198. NULL);
  4199. if (ret) {
  4200. dev_info(&pf->pdev->dev,
  4201. "couldn't get PF vsi ets bw config, err %s aq_err %s\n",
  4202. i40e_stat_str(&pf->hw, ret),
  4203. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  4204. return -EINVAL;
  4205. }
  4206. if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
  4207. dev_info(&pf->pdev->dev,
  4208. "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
  4209. bw_config.tc_valid_bits,
  4210. bw_ets_config.tc_valid_bits);
  4211. /* Still continuing */
  4212. }
  4213. vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
  4214. vsi->bw_max_quanta = bw_config.max_bw;
  4215. tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
  4216. (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
  4217. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4218. vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
  4219. vsi->bw_ets_limit_credits[i] =
  4220. le16_to_cpu(bw_ets_config.credits[i]);
  4221. /* 3 bits out of 4 for each TC */
  4222. vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
  4223. }
  4224. return 0;
  4225. }
  4226. /**
  4227. * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
  4228. * @vsi: the VSI being configured
  4229. * @enabled_tc: TC bitmap
  4230. * @bw_credits: BW shared credits per TC
  4231. *
  4232. * Returns 0 on success, negative value on failure
  4233. **/
  4234. static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
  4235. u8 *bw_share)
  4236. {
  4237. struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
  4238. i40e_status ret;
  4239. int i;
  4240. bw_data.tc_valid_bits = enabled_tc;
  4241. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
  4242. bw_data.tc_bw_credits[i] = bw_share[i];
  4243. ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, vsi->seid, &bw_data,
  4244. NULL);
  4245. if (ret) {
  4246. dev_info(&vsi->back->pdev->dev,
  4247. "AQ command Config VSI BW allocation per TC failed = %d\n",
  4248. vsi->back->hw.aq.asq_last_status);
  4249. return -EINVAL;
  4250. }
  4251. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
  4252. vsi->info.qs_handle[i] = bw_data.qs_handles[i];
  4253. return 0;
  4254. }
  4255. /**
  4256. * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
  4257. * @vsi: the VSI being configured
  4258. * @enabled_tc: TC map to be enabled
  4259. *
  4260. **/
  4261. static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
  4262. {
  4263. struct net_device *netdev = vsi->netdev;
  4264. struct i40e_pf *pf = vsi->back;
  4265. struct i40e_hw *hw = &pf->hw;
  4266. u8 netdev_tc = 0;
  4267. int i;
  4268. struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
  4269. if (!netdev)
  4270. return;
  4271. if (!enabled_tc) {
  4272. netdev_reset_tc(netdev);
  4273. return;
  4274. }
  4275. /* Set up actual enabled TCs on the VSI */
  4276. if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
  4277. return;
  4278. /* set per TC queues for the VSI */
  4279. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4280. /* Only set TC queues for enabled tcs
  4281. *
  4282. * e.g. For a VSI that has TC0 and TC3 enabled the
  4283. * enabled_tc bitmap would be 0x00001001; the driver
  4284. * will set the numtc for netdev as 2 that will be
  4285. * referenced by the netdev layer as TC 0 and 1.
  4286. */
  4287. if (vsi->tc_config.enabled_tc & BIT(i))
  4288. netdev_set_tc_queue(netdev,
  4289. vsi->tc_config.tc_info[i].netdev_tc,
  4290. vsi->tc_config.tc_info[i].qcount,
  4291. vsi->tc_config.tc_info[i].qoffset);
  4292. }
  4293. /* Assign UP2TC map for the VSI */
  4294. for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
  4295. /* Get the actual TC# for the UP */
  4296. u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
  4297. /* Get the mapped netdev TC# for the UP */
  4298. netdev_tc = vsi->tc_config.tc_info[ets_tc].netdev_tc;
  4299. netdev_set_prio_tc_map(netdev, i, netdev_tc);
  4300. }
  4301. }
  4302. /**
  4303. * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
  4304. * @vsi: the VSI being configured
  4305. * @ctxt: the ctxt buffer returned from AQ VSI update param command
  4306. **/
  4307. static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
  4308. struct i40e_vsi_context *ctxt)
  4309. {
  4310. /* copy just the sections touched not the entire info
  4311. * since not all sections are valid as returned by
  4312. * update vsi params
  4313. */
  4314. vsi->info.mapping_flags = ctxt->info.mapping_flags;
  4315. memcpy(&vsi->info.queue_mapping,
  4316. &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
  4317. memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
  4318. sizeof(vsi->info.tc_mapping));
  4319. }
  4320. /**
  4321. * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
  4322. * @vsi: VSI to be configured
  4323. * @enabled_tc: TC bitmap
  4324. *
  4325. * This configures a particular VSI for TCs that are mapped to the
  4326. * given TC bitmap. It uses default bandwidth share for TCs across
  4327. * VSIs to configure TC for a particular VSI.
  4328. *
  4329. * NOTE:
  4330. * It is expected that the VSI queues have been quisced before calling
  4331. * this function.
  4332. **/
  4333. static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
  4334. {
  4335. u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
  4336. struct i40e_vsi_context ctxt;
  4337. int ret = 0;
  4338. int i;
  4339. /* Check if enabled_tc is same as existing or new TCs */
  4340. if (vsi->tc_config.enabled_tc == enabled_tc)
  4341. return ret;
  4342. /* Enable ETS TCs with equal BW Share for now across all VSIs */
  4343. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4344. if (enabled_tc & BIT(i))
  4345. bw_share[i] = 1;
  4346. }
  4347. ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
  4348. if (ret) {
  4349. dev_info(&vsi->back->pdev->dev,
  4350. "Failed configuring TC map %d for VSI %d\n",
  4351. enabled_tc, vsi->seid);
  4352. goto out;
  4353. }
  4354. /* Update Queue Pairs Mapping for currently enabled UPs */
  4355. ctxt.seid = vsi->seid;
  4356. ctxt.pf_num = vsi->back->hw.pf_id;
  4357. ctxt.vf_num = 0;
  4358. ctxt.uplink_seid = vsi->uplink_seid;
  4359. ctxt.info = vsi->info;
  4360. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
  4361. if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
  4362. ctxt.info.valid_sections |=
  4363. cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
  4364. ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
  4365. }
  4366. /* Update the VSI after updating the VSI queue-mapping information */
  4367. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  4368. if (ret) {
  4369. dev_info(&vsi->back->pdev->dev,
  4370. "Update vsi tc config failed, err %s aq_err %s\n",
  4371. i40e_stat_str(&vsi->back->hw, ret),
  4372. i40e_aq_str(&vsi->back->hw,
  4373. vsi->back->hw.aq.asq_last_status));
  4374. goto out;
  4375. }
  4376. /* update the local VSI info with updated queue map */
  4377. i40e_vsi_update_queue_map(vsi, &ctxt);
  4378. vsi->info.valid_sections = 0;
  4379. /* Update current VSI BW information */
  4380. ret = i40e_vsi_get_bw_info(vsi);
  4381. if (ret) {
  4382. dev_info(&vsi->back->pdev->dev,
  4383. "Failed updating vsi bw info, err %s aq_err %s\n",
  4384. i40e_stat_str(&vsi->back->hw, ret),
  4385. i40e_aq_str(&vsi->back->hw,
  4386. vsi->back->hw.aq.asq_last_status));
  4387. goto out;
  4388. }
  4389. /* Update the netdev TC setup */
  4390. i40e_vsi_config_netdev_tc(vsi, enabled_tc);
  4391. out:
  4392. return ret;
  4393. }
  4394. /**
  4395. * i40e_veb_config_tc - Configure TCs for given VEB
  4396. * @veb: given VEB
  4397. * @enabled_tc: TC bitmap
  4398. *
  4399. * Configures given TC bitmap for VEB (switching) element
  4400. **/
  4401. int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
  4402. {
  4403. struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
  4404. struct i40e_pf *pf = veb->pf;
  4405. int ret = 0;
  4406. int i;
  4407. /* No TCs or already enabled TCs just return */
  4408. if (!enabled_tc || veb->enabled_tc == enabled_tc)
  4409. return ret;
  4410. bw_data.tc_valid_bits = enabled_tc;
  4411. /* bw_data.absolute_credits is not set (relative) */
  4412. /* Enable ETS TCs with equal BW Share for now */
  4413. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4414. if (enabled_tc & BIT(i))
  4415. bw_data.tc_bw_share_credits[i] = 1;
  4416. }
  4417. ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
  4418. &bw_data, NULL);
  4419. if (ret) {
  4420. dev_info(&pf->pdev->dev,
  4421. "VEB bw config failed, err %s aq_err %s\n",
  4422. i40e_stat_str(&pf->hw, ret),
  4423. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  4424. goto out;
  4425. }
  4426. /* Update the BW information */
  4427. ret = i40e_veb_get_bw_info(veb);
  4428. if (ret) {
  4429. dev_info(&pf->pdev->dev,
  4430. "Failed getting veb bw config, err %s aq_err %s\n",
  4431. i40e_stat_str(&pf->hw, ret),
  4432. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  4433. }
  4434. out:
  4435. return ret;
  4436. }
  4437. #ifdef CONFIG_I40E_DCB
  4438. /**
  4439. * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
  4440. * @pf: PF struct
  4441. *
  4442. * Reconfigure VEB/VSIs on a given PF; it is assumed that
  4443. * the caller would've quiesce all the VSIs before calling
  4444. * this function
  4445. **/
  4446. static void i40e_dcb_reconfigure(struct i40e_pf *pf)
  4447. {
  4448. u8 tc_map = 0;
  4449. int ret;
  4450. u8 v;
  4451. /* Enable the TCs available on PF to all VEBs */
  4452. tc_map = i40e_pf_get_tc_map(pf);
  4453. for (v = 0; v < I40E_MAX_VEB; v++) {
  4454. if (!pf->veb[v])
  4455. continue;
  4456. ret = i40e_veb_config_tc(pf->veb[v], tc_map);
  4457. if (ret) {
  4458. dev_info(&pf->pdev->dev,
  4459. "Failed configuring TC for VEB seid=%d\n",
  4460. pf->veb[v]->seid);
  4461. /* Will try to configure as many components */
  4462. }
  4463. }
  4464. /* Update each VSI */
  4465. for (v = 0; v < pf->num_alloc_vsi; v++) {
  4466. if (!pf->vsi[v])
  4467. continue;
  4468. /* - Enable all TCs for the LAN VSI
  4469. * - For all others keep them at TC0 for now
  4470. */
  4471. if (v == pf->lan_vsi)
  4472. tc_map = i40e_pf_get_tc_map(pf);
  4473. else
  4474. tc_map = I40E_DEFAULT_TRAFFIC_CLASS;
  4475. ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
  4476. if (ret) {
  4477. dev_info(&pf->pdev->dev,
  4478. "Failed configuring TC for VSI seid=%d\n",
  4479. pf->vsi[v]->seid);
  4480. /* Will try to configure as many components */
  4481. } else {
  4482. /* Re-configure VSI vectors based on updated TC map */
  4483. i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
  4484. if (pf->vsi[v]->netdev)
  4485. i40e_dcbnl_set_all(pf->vsi[v]);
  4486. }
  4487. }
  4488. }
  4489. /**
  4490. * i40e_resume_port_tx - Resume port Tx
  4491. * @pf: PF struct
  4492. *
  4493. * Resume a port's Tx and issue a PF reset in case of failure to
  4494. * resume.
  4495. **/
  4496. static int i40e_resume_port_tx(struct i40e_pf *pf)
  4497. {
  4498. struct i40e_hw *hw = &pf->hw;
  4499. int ret;
  4500. ret = i40e_aq_resume_port_tx(hw, NULL);
  4501. if (ret) {
  4502. dev_info(&pf->pdev->dev,
  4503. "Resume Port Tx failed, err %s aq_err %s\n",
  4504. i40e_stat_str(&pf->hw, ret),
  4505. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  4506. /* Schedule PF reset to recover */
  4507. set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
  4508. i40e_service_event_schedule(pf);
  4509. }
  4510. return ret;
  4511. }
  4512. /**
  4513. * i40e_init_pf_dcb - Initialize DCB configuration
  4514. * @pf: PF being configured
  4515. *
  4516. * Query the current DCB configuration and cache it
  4517. * in the hardware structure
  4518. **/
  4519. static int i40e_init_pf_dcb(struct i40e_pf *pf)
  4520. {
  4521. struct i40e_hw *hw = &pf->hw;
  4522. int err = 0;
  4523. /* Do not enable DCB for SW1 and SW2 images even if the FW is capable */
  4524. if (pf->flags & I40E_FLAG_NO_DCB_SUPPORT)
  4525. goto out;
  4526. /* Get the initial DCB configuration */
  4527. err = i40e_init_dcb(hw);
  4528. if (!err) {
  4529. /* Device/Function is not DCBX capable */
  4530. if ((!hw->func_caps.dcb) ||
  4531. (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
  4532. dev_info(&pf->pdev->dev,
  4533. "DCBX offload is not supported or is disabled for this PF.\n");
  4534. if (pf->flags & I40E_FLAG_MFP_ENABLED)
  4535. goto out;
  4536. } else {
  4537. /* When status is not DISABLED then DCBX in FW */
  4538. pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
  4539. DCB_CAP_DCBX_VER_IEEE;
  4540. pf->flags |= I40E_FLAG_DCB_CAPABLE;
  4541. /* Enable DCB tagging only when more than one TC
  4542. * or explicitly disable if only one TC
  4543. */
  4544. if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
  4545. pf->flags |= I40E_FLAG_DCB_ENABLED;
  4546. else
  4547. pf->flags &= ~I40E_FLAG_DCB_ENABLED;
  4548. dev_dbg(&pf->pdev->dev,
  4549. "DCBX offload is supported for this PF.\n");
  4550. }
  4551. } else {
  4552. dev_info(&pf->pdev->dev,
  4553. "Query for DCB configuration failed, err %s aq_err %s\n",
  4554. i40e_stat_str(&pf->hw, err),
  4555. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  4556. }
  4557. out:
  4558. return err;
  4559. }
  4560. #endif /* CONFIG_I40E_DCB */
  4561. #define SPEED_SIZE 14
  4562. #define FC_SIZE 8
  4563. /**
  4564. * i40e_print_link_message - print link up or down
  4565. * @vsi: the VSI for which link needs a message
  4566. */
  4567. void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
  4568. {
  4569. enum i40e_aq_link_speed new_speed;
  4570. char *speed = "Unknown";
  4571. char *fc = "Unknown";
  4572. char *fec = "";
  4573. char *an = "";
  4574. new_speed = vsi->back->hw.phy.link_info.link_speed;
  4575. if ((vsi->current_isup == isup) && (vsi->current_speed == new_speed))
  4576. return;
  4577. vsi->current_isup = isup;
  4578. vsi->current_speed = new_speed;
  4579. if (!isup) {
  4580. netdev_info(vsi->netdev, "NIC Link is Down\n");
  4581. return;
  4582. }
  4583. /* Warn user if link speed on NPAR enabled partition is not at
  4584. * least 10GB
  4585. */
  4586. if (vsi->back->hw.func_caps.npar_enable &&
  4587. (vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
  4588. vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
  4589. netdev_warn(vsi->netdev,
  4590. "The partition detected link speed that is less than 10Gbps\n");
  4591. switch (vsi->back->hw.phy.link_info.link_speed) {
  4592. case I40E_LINK_SPEED_40GB:
  4593. speed = "40 G";
  4594. break;
  4595. case I40E_LINK_SPEED_20GB:
  4596. speed = "20 G";
  4597. break;
  4598. case I40E_LINK_SPEED_25GB:
  4599. speed = "25 G";
  4600. break;
  4601. case I40E_LINK_SPEED_10GB:
  4602. speed = "10 G";
  4603. break;
  4604. case I40E_LINK_SPEED_1GB:
  4605. speed = "1000 M";
  4606. break;
  4607. case I40E_LINK_SPEED_100MB:
  4608. speed = "100 M";
  4609. break;
  4610. default:
  4611. break;
  4612. }
  4613. switch (vsi->back->hw.fc.current_mode) {
  4614. case I40E_FC_FULL:
  4615. fc = "RX/TX";
  4616. break;
  4617. case I40E_FC_TX_PAUSE:
  4618. fc = "TX";
  4619. break;
  4620. case I40E_FC_RX_PAUSE:
  4621. fc = "RX";
  4622. break;
  4623. default:
  4624. fc = "None";
  4625. break;
  4626. }
  4627. if (vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_25GB) {
  4628. fec = ", FEC: None";
  4629. an = ", Autoneg: False";
  4630. if (vsi->back->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
  4631. an = ", Autoneg: True";
  4632. if (vsi->back->hw.phy.link_info.fec_info &
  4633. I40E_AQ_CONFIG_FEC_KR_ENA)
  4634. fec = ", FEC: CL74 FC-FEC/BASE-R";
  4635. else if (vsi->back->hw.phy.link_info.fec_info &
  4636. I40E_AQ_CONFIG_FEC_RS_ENA)
  4637. fec = ", FEC: CL108 RS-FEC";
  4638. }
  4639. netdev_info(vsi->netdev, "NIC Link is Up, %sbps Full Duplex%s%s, Flow Control: %s\n",
  4640. speed, fec, an, fc);
  4641. }
  4642. /**
  4643. * i40e_up_complete - Finish the last steps of bringing up a connection
  4644. * @vsi: the VSI being configured
  4645. **/
  4646. static int i40e_up_complete(struct i40e_vsi *vsi)
  4647. {
  4648. struct i40e_pf *pf = vsi->back;
  4649. int err;
  4650. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  4651. i40e_vsi_configure_msix(vsi);
  4652. else
  4653. i40e_configure_msi_and_legacy(vsi);
  4654. /* start rings */
  4655. err = i40e_vsi_start_rings(vsi);
  4656. if (err)
  4657. return err;
  4658. clear_bit(__I40E_DOWN, &vsi->state);
  4659. i40e_napi_enable_all(vsi);
  4660. i40e_vsi_enable_irq(vsi);
  4661. if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
  4662. (vsi->netdev)) {
  4663. i40e_print_link_message(vsi, true);
  4664. netif_tx_start_all_queues(vsi->netdev);
  4665. netif_carrier_on(vsi->netdev);
  4666. } else if (vsi->netdev) {
  4667. i40e_print_link_message(vsi, false);
  4668. /* need to check for qualified module here*/
  4669. if ((pf->hw.phy.link_info.link_info &
  4670. I40E_AQ_MEDIA_AVAILABLE) &&
  4671. (!(pf->hw.phy.link_info.an_info &
  4672. I40E_AQ_QUALIFIED_MODULE)))
  4673. netdev_err(vsi->netdev,
  4674. "the driver failed to link because an unqualified module was detected.");
  4675. }
  4676. /* replay FDIR SB filters */
  4677. if (vsi->type == I40E_VSI_FDIR) {
  4678. /* reset fd counters */
  4679. pf->fd_add_err = 0;
  4680. pf->fd_atr_cnt = 0;
  4681. i40e_fdir_filter_restore(vsi);
  4682. }
  4683. /* On the next run of the service_task, notify any clients of the new
  4684. * opened netdev
  4685. */
  4686. pf->flags |= I40E_FLAG_SERVICE_CLIENT_REQUESTED;
  4687. i40e_service_event_schedule(pf);
  4688. return 0;
  4689. }
  4690. /**
  4691. * i40e_vsi_reinit_locked - Reset the VSI
  4692. * @vsi: the VSI being configured
  4693. *
  4694. * Rebuild the ring structs after some configuration
  4695. * has changed, e.g. MTU size.
  4696. **/
  4697. static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
  4698. {
  4699. struct i40e_pf *pf = vsi->back;
  4700. WARN_ON(in_interrupt());
  4701. while (test_and_set_bit(__I40E_CONFIG_BUSY, &pf->state))
  4702. usleep_range(1000, 2000);
  4703. i40e_down(vsi);
  4704. i40e_up(vsi);
  4705. clear_bit(__I40E_CONFIG_BUSY, &pf->state);
  4706. }
  4707. /**
  4708. * i40e_up - Bring the connection back up after being down
  4709. * @vsi: the VSI being configured
  4710. **/
  4711. int i40e_up(struct i40e_vsi *vsi)
  4712. {
  4713. int err;
  4714. err = i40e_vsi_configure(vsi);
  4715. if (!err)
  4716. err = i40e_up_complete(vsi);
  4717. return err;
  4718. }
  4719. /**
  4720. * i40e_down - Shutdown the connection processing
  4721. * @vsi: the VSI being stopped
  4722. **/
  4723. void i40e_down(struct i40e_vsi *vsi)
  4724. {
  4725. int i;
  4726. /* It is assumed that the caller of this function
  4727. * sets the vsi->state __I40E_DOWN bit.
  4728. */
  4729. if (vsi->netdev) {
  4730. netif_carrier_off(vsi->netdev);
  4731. netif_tx_disable(vsi->netdev);
  4732. }
  4733. i40e_vsi_disable_irq(vsi);
  4734. i40e_vsi_stop_rings(vsi);
  4735. i40e_napi_disable_all(vsi);
  4736. for (i = 0; i < vsi->num_queue_pairs; i++) {
  4737. i40e_clean_tx_ring(vsi->tx_rings[i]);
  4738. i40e_clean_rx_ring(vsi->rx_rings[i]);
  4739. }
  4740. }
  4741. /**
  4742. * i40e_setup_tc - configure multiple traffic classes
  4743. * @netdev: net device to configure
  4744. * @tc: number of traffic classes to enable
  4745. **/
  4746. static int i40e_setup_tc(struct net_device *netdev, u8 tc)
  4747. {
  4748. struct i40e_netdev_priv *np = netdev_priv(netdev);
  4749. struct i40e_vsi *vsi = np->vsi;
  4750. struct i40e_pf *pf = vsi->back;
  4751. u8 enabled_tc = 0;
  4752. int ret = -EINVAL;
  4753. int i;
  4754. /* Check if DCB enabled to continue */
  4755. if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
  4756. netdev_info(netdev, "DCB is not enabled for adapter\n");
  4757. goto exit;
  4758. }
  4759. /* Check if MFP enabled */
  4760. if (pf->flags & I40E_FLAG_MFP_ENABLED) {
  4761. netdev_info(netdev, "Configuring TC not supported in MFP mode\n");
  4762. goto exit;
  4763. }
  4764. /* Check whether tc count is within enabled limit */
  4765. if (tc > i40e_pf_get_num_tc(pf)) {
  4766. netdev_info(netdev, "TC count greater than enabled on link for adapter\n");
  4767. goto exit;
  4768. }
  4769. /* Generate TC map for number of tc requested */
  4770. for (i = 0; i < tc; i++)
  4771. enabled_tc |= BIT(i);
  4772. /* Requesting same TC configuration as already enabled */
  4773. if (enabled_tc == vsi->tc_config.enabled_tc)
  4774. return 0;
  4775. /* Quiesce VSI queues */
  4776. i40e_quiesce_vsi(vsi);
  4777. /* Configure VSI for enabled TCs */
  4778. ret = i40e_vsi_config_tc(vsi, enabled_tc);
  4779. if (ret) {
  4780. netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
  4781. vsi->seid);
  4782. goto exit;
  4783. }
  4784. /* Unquiesce VSI */
  4785. i40e_unquiesce_vsi(vsi);
  4786. exit:
  4787. return ret;
  4788. }
  4789. static int __i40e_setup_tc(struct net_device *netdev, u32 handle, __be16 proto,
  4790. struct tc_to_netdev *tc)
  4791. {
  4792. if (tc->type != TC_SETUP_MQPRIO)
  4793. return -EINVAL;
  4794. tc->mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
  4795. return i40e_setup_tc(netdev, tc->mqprio->num_tc);
  4796. }
  4797. /**
  4798. * i40e_open - Called when a network interface is made active
  4799. * @netdev: network interface device structure
  4800. *
  4801. * The open entry point is called when a network interface is made
  4802. * active by the system (IFF_UP). At this point all resources needed
  4803. * for transmit and receive operations are allocated, the interrupt
  4804. * handler is registered with the OS, the netdev watchdog subtask is
  4805. * enabled, and the stack is notified that the interface is ready.
  4806. *
  4807. * Returns 0 on success, negative value on failure
  4808. **/
  4809. int i40e_open(struct net_device *netdev)
  4810. {
  4811. struct i40e_netdev_priv *np = netdev_priv(netdev);
  4812. struct i40e_vsi *vsi = np->vsi;
  4813. struct i40e_pf *pf = vsi->back;
  4814. int err;
  4815. /* disallow open during test or if eeprom is broken */
  4816. if (test_bit(__I40E_TESTING, &pf->state) ||
  4817. test_bit(__I40E_BAD_EEPROM, &pf->state))
  4818. return -EBUSY;
  4819. netif_carrier_off(netdev);
  4820. err = i40e_vsi_open(vsi);
  4821. if (err)
  4822. return err;
  4823. /* configure global TSO hardware offload settings */
  4824. wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
  4825. TCP_FLAG_FIN) >> 16);
  4826. wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
  4827. TCP_FLAG_FIN |
  4828. TCP_FLAG_CWR) >> 16);
  4829. wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
  4830. udp_tunnel_get_rx_info(netdev);
  4831. return 0;
  4832. }
  4833. /**
  4834. * i40e_vsi_open -
  4835. * @vsi: the VSI to open
  4836. *
  4837. * Finish initialization of the VSI.
  4838. *
  4839. * Returns 0 on success, negative value on failure
  4840. **/
  4841. int i40e_vsi_open(struct i40e_vsi *vsi)
  4842. {
  4843. struct i40e_pf *pf = vsi->back;
  4844. char int_name[I40E_INT_NAME_STR_LEN];
  4845. int err;
  4846. /* allocate descriptors */
  4847. err = i40e_vsi_setup_tx_resources(vsi);
  4848. if (err)
  4849. goto err_setup_tx;
  4850. err = i40e_vsi_setup_rx_resources(vsi);
  4851. if (err)
  4852. goto err_setup_rx;
  4853. err = i40e_vsi_configure(vsi);
  4854. if (err)
  4855. goto err_setup_rx;
  4856. if (vsi->netdev) {
  4857. snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
  4858. dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
  4859. err = i40e_vsi_request_irq(vsi, int_name);
  4860. if (err)
  4861. goto err_setup_rx;
  4862. /* Notify the stack of the actual queue counts. */
  4863. err = netif_set_real_num_tx_queues(vsi->netdev,
  4864. vsi->num_queue_pairs);
  4865. if (err)
  4866. goto err_set_queues;
  4867. err = netif_set_real_num_rx_queues(vsi->netdev,
  4868. vsi->num_queue_pairs);
  4869. if (err)
  4870. goto err_set_queues;
  4871. } else if (vsi->type == I40E_VSI_FDIR) {
  4872. snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
  4873. dev_driver_string(&pf->pdev->dev),
  4874. dev_name(&pf->pdev->dev));
  4875. err = i40e_vsi_request_irq(vsi, int_name);
  4876. } else {
  4877. err = -EINVAL;
  4878. goto err_setup_rx;
  4879. }
  4880. err = i40e_up_complete(vsi);
  4881. if (err)
  4882. goto err_up_complete;
  4883. return 0;
  4884. err_up_complete:
  4885. i40e_down(vsi);
  4886. err_set_queues:
  4887. i40e_vsi_free_irq(vsi);
  4888. err_setup_rx:
  4889. i40e_vsi_free_rx_resources(vsi);
  4890. err_setup_tx:
  4891. i40e_vsi_free_tx_resources(vsi);
  4892. if (vsi == pf->vsi[pf->lan_vsi])
  4893. i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
  4894. return err;
  4895. }
  4896. /**
  4897. * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
  4898. * @pf: Pointer to PF
  4899. *
  4900. * This function destroys the hlist where all the Flow Director
  4901. * filters were saved.
  4902. **/
  4903. static void i40e_fdir_filter_exit(struct i40e_pf *pf)
  4904. {
  4905. struct i40e_fdir_filter *filter;
  4906. struct i40e_flex_pit *pit_entry, *tmp;
  4907. struct hlist_node *node2;
  4908. hlist_for_each_entry_safe(filter, node2,
  4909. &pf->fdir_filter_list, fdir_node) {
  4910. hlist_del(&filter->fdir_node);
  4911. kfree(filter);
  4912. }
  4913. list_for_each_entry_safe(pit_entry, tmp, &pf->l3_flex_pit_list, list) {
  4914. list_del(&pit_entry->list);
  4915. kfree(pit_entry);
  4916. }
  4917. INIT_LIST_HEAD(&pf->l3_flex_pit_list);
  4918. list_for_each_entry_safe(pit_entry, tmp, &pf->l4_flex_pit_list, list) {
  4919. list_del(&pit_entry->list);
  4920. kfree(pit_entry);
  4921. }
  4922. INIT_LIST_HEAD(&pf->l4_flex_pit_list);
  4923. pf->fdir_pf_active_filters = 0;
  4924. pf->fd_tcp4_filter_cnt = 0;
  4925. pf->fd_udp4_filter_cnt = 0;
  4926. pf->fd_sctp4_filter_cnt = 0;
  4927. pf->fd_ip4_filter_cnt = 0;
  4928. /* Reprogram the default input set for TCP/IPv4 */
  4929. i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
  4930. I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
  4931. I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
  4932. /* Reprogram the default input set for UDP/IPv4 */
  4933. i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_UDP,
  4934. I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
  4935. I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
  4936. /* Reprogram the default input set for SCTP/IPv4 */
  4937. i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_SCTP,
  4938. I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
  4939. I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
  4940. /* Reprogram the default input set for Other/IPv4 */
  4941. i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_OTHER,
  4942. I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
  4943. }
  4944. /**
  4945. * i40e_close - Disables a network interface
  4946. * @netdev: network interface device structure
  4947. *
  4948. * The close entry point is called when an interface is de-activated
  4949. * by the OS. The hardware is still under the driver's control, but
  4950. * this netdev interface is disabled.
  4951. *
  4952. * Returns 0, this is not allowed to fail
  4953. **/
  4954. int i40e_close(struct net_device *netdev)
  4955. {
  4956. struct i40e_netdev_priv *np = netdev_priv(netdev);
  4957. struct i40e_vsi *vsi = np->vsi;
  4958. i40e_vsi_close(vsi);
  4959. return 0;
  4960. }
  4961. /**
  4962. * i40e_do_reset - Start a PF or Core Reset sequence
  4963. * @pf: board private structure
  4964. * @reset_flags: which reset is requested
  4965. *
  4966. * The essential difference in resets is that the PF Reset
  4967. * doesn't clear the packet buffers, doesn't reset the PE
  4968. * firmware, and doesn't bother the other PFs on the chip.
  4969. **/
  4970. void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags)
  4971. {
  4972. u32 val;
  4973. WARN_ON(in_interrupt());
  4974. /* do the biggest reset indicated */
  4975. if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
  4976. /* Request a Global Reset
  4977. *
  4978. * This will start the chip's countdown to the actual full
  4979. * chip reset event, and a warning interrupt to be sent
  4980. * to all PFs, including the requestor. Our handler
  4981. * for the warning interrupt will deal with the shutdown
  4982. * and recovery of the switch setup.
  4983. */
  4984. dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
  4985. val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
  4986. val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
  4987. wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
  4988. } else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
  4989. /* Request a Core Reset
  4990. *
  4991. * Same as Global Reset, except does *not* include the MAC/PHY
  4992. */
  4993. dev_dbg(&pf->pdev->dev, "CoreR requested\n");
  4994. val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
  4995. val |= I40E_GLGEN_RTRIG_CORER_MASK;
  4996. wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
  4997. i40e_flush(&pf->hw);
  4998. } else if (reset_flags & BIT_ULL(__I40E_PF_RESET_REQUESTED)) {
  4999. /* Request a PF Reset
  5000. *
  5001. * Resets only the PF-specific registers
  5002. *
  5003. * This goes directly to the tear-down and rebuild of
  5004. * the switch, since we need to do all the recovery as
  5005. * for the Core Reset.
  5006. */
  5007. dev_dbg(&pf->pdev->dev, "PFR requested\n");
  5008. i40e_handle_reset_warning(pf);
  5009. } else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
  5010. int v;
  5011. /* Find the VSI(s) that requested a re-init */
  5012. dev_info(&pf->pdev->dev,
  5013. "VSI reinit requested\n");
  5014. for (v = 0; v < pf->num_alloc_vsi; v++) {
  5015. struct i40e_vsi *vsi = pf->vsi[v];
  5016. if (vsi != NULL &&
  5017. test_bit(__I40E_REINIT_REQUESTED, &vsi->state)) {
  5018. i40e_vsi_reinit_locked(pf->vsi[v]);
  5019. clear_bit(__I40E_REINIT_REQUESTED, &vsi->state);
  5020. }
  5021. }
  5022. } else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
  5023. int v;
  5024. /* Find the VSI(s) that needs to be brought down */
  5025. dev_info(&pf->pdev->dev, "VSI down requested\n");
  5026. for (v = 0; v < pf->num_alloc_vsi; v++) {
  5027. struct i40e_vsi *vsi = pf->vsi[v];
  5028. if (vsi != NULL &&
  5029. test_bit(__I40E_DOWN_REQUESTED, &vsi->state)) {
  5030. set_bit(__I40E_DOWN, &vsi->state);
  5031. i40e_down(vsi);
  5032. clear_bit(__I40E_DOWN_REQUESTED, &vsi->state);
  5033. }
  5034. }
  5035. } else {
  5036. dev_info(&pf->pdev->dev,
  5037. "bad reset request 0x%08x\n", reset_flags);
  5038. }
  5039. }
  5040. #ifdef CONFIG_I40E_DCB
  5041. /**
  5042. * i40e_dcb_need_reconfig - Check if DCB needs reconfig
  5043. * @pf: board private structure
  5044. * @old_cfg: current DCB config
  5045. * @new_cfg: new DCB config
  5046. **/
  5047. bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
  5048. struct i40e_dcbx_config *old_cfg,
  5049. struct i40e_dcbx_config *new_cfg)
  5050. {
  5051. bool need_reconfig = false;
  5052. /* Check if ETS configuration has changed */
  5053. if (memcmp(&new_cfg->etscfg,
  5054. &old_cfg->etscfg,
  5055. sizeof(new_cfg->etscfg))) {
  5056. /* If Priority Table has changed reconfig is needed */
  5057. if (memcmp(&new_cfg->etscfg.prioritytable,
  5058. &old_cfg->etscfg.prioritytable,
  5059. sizeof(new_cfg->etscfg.prioritytable))) {
  5060. need_reconfig = true;
  5061. dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
  5062. }
  5063. if (memcmp(&new_cfg->etscfg.tcbwtable,
  5064. &old_cfg->etscfg.tcbwtable,
  5065. sizeof(new_cfg->etscfg.tcbwtable)))
  5066. dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
  5067. if (memcmp(&new_cfg->etscfg.tsatable,
  5068. &old_cfg->etscfg.tsatable,
  5069. sizeof(new_cfg->etscfg.tsatable)))
  5070. dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
  5071. }
  5072. /* Check if PFC configuration has changed */
  5073. if (memcmp(&new_cfg->pfc,
  5074. &old_cfg->pfc,
  5075. sizeof(new_cfg->pfc))) {
  5076. need_reconfig = true;
  5077. dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
  5078. }
  5079. /* Check if APP Table has changed */
  5080. if (memcmp(&new_cfg->app,
  5081. &old_cfg->app,
  5082. sizeof(new_cfg->app))) {
  5083. need_reconfig = true;
  5084. dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
  5085. }
  5086. dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
  5087. return need_reconfig;
  5088. }
  5089. /**
  5090. * i40e_handle_lldp_event - Handle LLDP Change MIB event
  5091. * @pf: board private structure
  5092. * @e: event info posted on ARQ
  5093. **/
  5094. static int i40e_handle_lldp_event(struct i40e_pf *pf,
  5095. struct i40e_arq_event_info *e)
  5096. {
  5097. struct i40e_aqc_lldp_get_mib *mib =
  5098. (struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
  5099. struct i40e_hw *hw = &pf->hw;
  5100. struct i40e_dcbx_config tmp_dcbx_cfg;
  5101. bool need_reconfig = false;
  5102. int ret = 0;
  5103. u8 type;
  5104. /* Not DCB capable or capability disabled */
  5105. if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
  5106. return ret;
  5107. /* Ignore if event is not for Nearest Bridge */
  5108. type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
  5109. & I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
  5110. dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
  5111. if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
  5112. return ret;
  5113. /* Check MIB Type and return if event for Remote MIB update */
  5114. type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
  5115. dev_dbg(&pf->pdev->dev,
  5116. "LLDP event mib type %s\n", type ? "remote" : "local");
  5117. if (type == I40E_AQ_LLDP_MIB_REMOTE) {
  5118. /* Update the remote cached instance and return */
  5119. ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
  5120. I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
  5121. &hw->remote_dcbx_config);
  5122. goto exit;
  5123. }
  5124. /* Store the old configuration */
  5125. tmp_dcbx_cfg = hw->local_dcbx_config;
  5126. /* Reset the old DCBx configuration data */
  5127. memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
  5128. /* Get updated DCBX data from firmware */
  5129. ret = i40e_get_dcb_config(&pf->hw);
  5130. if (ret) {
  5131. dev_info(&pf->pdev->dev,
  5132. "Failed querying DCB configuration data from firmware, err %s aq_err %s\n",
  5133. i40e_stat_str(&pf->hw, ret),
  5134. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  5135. goto exit;
  5136. }
  5137. /* No change detected in DCBX configs */
  5138. if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
  5139. sizeof(tmp_dcbx_cfg))) {
  5140. dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
  5141. goto exit;
  5142. }
  5143. need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
  5144. &hw->local_dcbx_config);
  5145. i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
  5146. if (!need_reconfig)
  5147. goto exit;
  5148. /* Enable DCB tagging only when more than one TC */
  5149. if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
  5150. pf->flags |= I40E_FLAG_DCB_ENABLED;
  5151. else
  5152. pf->flags &= ~I40E_FLAG_DCB_ENABLED;
  5153. set_bit(__I40E_PORT_TX_SUSPENDED, &pf->state);
  5154. /* Reconfiguration needed quiesce all VSIs */
  5155. i40e_pf_quiesce_all_vsi(pf);
  5156. /* Changes in configuration update VEB/VSI */
  5157. i40e_dcb_reconfigure(pf);
  5158. ret = i40e_resume_port_tx(pf);
  5159. clear_bit(__I40E_PORT_TX_SUSPENDED, &pf->state);
  5160. /* In case of error no point in resuming VSIs */
  5161. if (ret)
  5162. goto exit;
  5163. /* Wait for the PF's queues to be disabled */
  5164. ret = i40e_pf_wait_queues_disabled(pf);
  5165. if (ret) {
  5166. /* Schedule PF reset to recover */
  5167. set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
  5168. i40e_service_event_schedule(pf);
  5169. } else {
  5170. i40e_pf_unquiesce_all_vsi(pf);
  5171. pf->flags |= (I40E_FLAG_SERVICE_CLIENT_REQUESTED |
  5172. I40E_FLAG_CLIENT_L2_CHANGE);
  5173. }
  5174. exit:
  5175. return ret;
  5176. }
  5177. #endif /* CONFIG_I40E_DCB */
  5178. /**
  5179. * i40e_do_reset_safe - Protected reset path for userland calls.
  5180. * @pf: board private structure
  5181. * @reset_flags: which reset is requested
  5182. *
  5183. **/
  5184. void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
  5185. {
  5186. rtnl_lock();
  5187. i40e_do_reset(pf, reset_flags);
  5188. rtnl_unlock();
  5189. }
  5190. /**
  5191. * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
  5192. * @pf: board private structure
  5193. * @e: event info posted on ARQ
  5194. *
  5195. * Handler for LAN Queue Overflow Event generated by the firmware for PF
  5196. * and VF queues
  5197. **/
  5198. static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
  5199. struct i40e_arq_event_info *e)
  5200. {
  5201. struct i40e_aqc_lan_overflow *data =
  5202. (struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
  5203. u32 queue = le32_to_cpu(data->prtdcb_rupto);
  5204. u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
  5205. struct i40e_hw *hw = &pf->hw;
  5206. struct i40e_vf *vf;
  5207. u16 vf_id;
  5208. dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
  5209. queue, qtx_ctl);
  5210. /* Queue belongs to VF, find the VF and issue VF reset */
  5211. if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
  5212. >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
  5213. vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
  5214. >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
  5215. vf_id -= hw->func_caps.vf_base_id;
  5216. vf = &pf->vf[vf_id];
  5217. i40e_vc_notify_vf_reset(vf);
  5218. /* Allow VF to process pending reset notification */
  5219. msleep(20);
  5220. i40e_reset_vf(vf, false);
  5221. }
  5222. }
  5223. /**
  5224. * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
  5225. * @pf: board private structure
  5226. **/
  5227. u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
  5228. {
  5229. u32 val, fcnt_prog;
  5230. val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
  5231. fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
  5232. return fcnt_prog;
  5233. }
  5234. /**
  5235. * i40e_get_current_fd_count - Get total FD filters programmed for this PF
  5236. * @pf: board private structure
  5237. **/
  5238. u32 i40e_get_current_fd_count(struct i40e_pf *pf)
  5239. {
  5240. u32 val, fcnt_prog;
  5241. val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
  5242. fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
  5243. ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
  5244. I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
  5245. return fcnt_prog;
  5246. }
  5247. /**
  5248. * i40e_get_global_fd_count - Get total FD filters programmed on device
  5249. * @pf: board private structure
  5250. **/
  5251. u32 i40e_get_global_fd_count(struct i40e_pf *pf)
  5252. {
  5253. u32 val, fcnt_prog;
  5254. val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
  5255. fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
  5256. ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >>
  5257. I40E_GLQF_FDCNT_0_BESTCNT_SHIFT);
  5258. return fcnt_prog;
  5259. }
  5260. /**
  5261. * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
  5262. * @pf: board private structure
  5263. **/
  5264. void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
  5265. {
  5266. struct i40e_fdir_filter *filter;
  5267. u32 fcnt_prog, fcnt_avail;
  5268. struct hlist_node *node;
  5269. if (test_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state))
  5270. return;
  5271. /* Check if, FD SB or ATR was auto disabled and if there is enough room
  5272. * to re-enable
  5273. */
  5274. fcnt_prog = i40e_get_global_fd_count(pf);
  5275. fcnt_avail = pf->fdir_pf_filter_count;
  5276. if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
  5277. (pf->fd_add_err == 0) ||
  5278. (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt)) {
  5279. if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
  5280. (pf->hw_disabled_flags & I40E_FLAG_FD_SB_ENABLED)) {
  5281. pf->hw_disabled_flags &= ~I40E_FLAG_FD_SB_ENABLED;
  5282. if (I40E_DEBUG_FD & pf->hw.debug_mask)
  5283. dev_info(&pf->pdev->dev, "FD Sideband/ntuple is being enabled since we have space in the table now\n");
  5284. }
  5285. }
  5286. /* Wait for some more space to be available to turn on ATR. We also
  5287. * must check that no existing ntuple rules for TCP are in effect
  5288. */
  5289. if (fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM * 2)) {
  5290. if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
  5291. (pf->hw_disabled_flags & I40E_FLAG_FD_ATR_ENABLED) &&
  5292. (pf->fd_tcp4_filter_cnt == 0)) {
  5293. pf->hw_disabled_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
  5294. if (I40E_DEBUG_FD & pf->hw.debug_mask)
  5295. dev_info(&pf->pdev->dev, "ATR is being enabled since we have space in the table and there are no conflicting ntuple rules\n");
  5296. }
  5297. }
  5298. /* if hw had a problem adding a filter, delete it */
  5299. if (pf->fd_inv > 0) {
  5300. hlist_for_each_entry_safe(filter, node,
  5301. &pf->fdir_filter_list, fdir_node) {
  5302. if (filter->fd_id == pf->fd_inv) {
  5303. hlist_del(&filter->fdir_node);
  5304. kfree(filter);
  5305. pf->fdir_pf_active_filters--;
  5306. }
  5307. }
  5308. }
  5309. }
  5310. #define I40E_MIN_FD_FLUSH_INTERVAL 10
  5311. #define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
  5312. /**
  5313. * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
  5314. * @pf: board private structure
  5315. **/
  5316. static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
  5317. {
  5318. unsigned long min_flush_time;
  5319. int flush_wait_retry = 50;
  5320. bool disable_atr = false;
  5321. int fd_room;
  5322. int reg;
  5323. if (!time_after(jiffies, pf->fd_flush_timestamp +
  5324. (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
  5325. return;
  5326. /* If the flush is happening too quick and we have mostly SB rules we
  5327. * should not re-enable ATR for some time.
  5328. */
  5329. min_flush_time = pf->fd_flush_timestamp +
  5330. (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
  5331. fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
  5332. if (!(time_after(jiffies, min_flush_time)) &&
  5333. (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
  5334. if (I40E_DEBUG_FD & pf->hw.debug_mask)
  5335. dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
  5336. disable_atr = true;
  5337. }
  5338. pf->fd_flush_timestamp = jiffies;
  5339. pf->hw_disabled_flags |= I40E_FLAG_FD_ATR_ENABLED;
  5340. /* flush all filters */
  5341. wr32(&pf->hw, I40E_PFQF_CTL_1,
  5342. I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
  5343. i40e_flush(&pf->hw);
  5344. pf->fd_flush_cnt++;
  5345. pf->fd_add_err = 0;
  5346. do {
  5347. /* Check FD flush status every 5-6msec */
  5348. usleep_range(5000, 6000);
  5349. reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
  5350. if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
  5351. break;
  5352. } while (flush_wait_retry--);
  5353. if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
  5354. dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
  5355. } else {
  5356. /* replay sideband filters */
  5357. i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
  5358. if (!disable_atr && !pf->fd_tcp4_filter_cnt)
  5359. pf->hw_disabled_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
  5360. clear_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state);
  5361. if (I40E_DEBUG_FD & pf->hw.debug_mask)
  5362. dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
  5363. }
  5364. }
  5365. /**
  5366. * i40e_get_current_atr_count - Get the count of total FD ATR filters programmed
  5367. * @pf: board private structure
  5368. **/
  5369. u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
  5370. {
  5371. return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
  5372. }
  5373. /* We can see up to 256 filter programming desc in transit if the filters are
  5374. * being applied really fast; before we see the first
  5375. * filter miss error on Rx queue 0. Accumulating enough error messages before
  5376. * reacting will make sure we don't cause flush too often.
  5377. */
  5378. #define I40E_MAX_FD_PROGRAM_ERROR 256
  5379. /**
  5380. * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
  5381. * @pf: board private structure
  5382. **/
  5383. static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
  5384. {
  5385. /* if interface is down do nothing */
  5386. if (test_bit(__I40E_DOWN, &pf->state))
  5387. return;
  5388. if (test_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state))
  5389. i40e_fdir_flush_and_replay(pf);
  5390. i40e_fdir_check_and_reenable(pf);
  5391. }
  5392. /**
  5393. * i40e_vsi_link_event - notify VSI of a link event
  5394. * @vsi: vsi to be notified
  5395. * @link_up: link up or down
  5396. **/
  5397. static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
  5398. {
  5399. if (!vsi || test_bit(__I40E_DOWN, &vsi->state))
  5400. return;
  5401. switch (vsi->type) {
  5402. case I40E_VSI_MAIN:
  5403. if (!vsi->netdev || !vsi->netdev_registered)
  5404. break;
  5405. if (link_up) {
  5406. netif_carrier_on(vsi->netdev);
  5407. netif_tx_wake_all_queues(vsi->netdev);
  5408. } else {
  5409. netif_carrier_off(vsi->netdev);
  5410. netif_tx_stop_all_queues(vsi->netdev);
  5411. }
  5412. break;
  5413. case I40E_VSI_SRIOV:
  5414. case I40E_VSI_VMDQ2:
  5415. case I40E_VSI_CTRL:
  5416. case I40E_VSI_IWARP:
  5417. case I40E_VSI_MIRROR:
  5418. default:
  5419. /* there is no notification for other VSIs */
  5420. break;
  5421. }
  5422. }
  5423. /**
  5424. * i40e_veb_link_event - notify elements on the veb of a link event
  5425. * @veb: veb to be notified
  5426. * @link_up: link up or down
  5427. **/
  5428. static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
  5429. {
  5430. struct i40e_pf *pf;
  5431. int i;
  5432. if (!veb || !veb->pf)
  5433. return;
  5434. pf = veb->pf;
  5435. /* depth first... */
  5436. for (i = 0; i < I40E_MAX_VEB; i++)
  5437. if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
  5438. i40e_veb_link_event(pf->veb[i], link_up);
  5439. /* ... now the local VSIs */
  5440. for (i = 0; i < pf->num_alloc_vsi; i++)
  5441. if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
  5442. i40e_vsi_link_event(pf->vsi[i], link_up);
  5443. }
  5444. /**
  5445. * i40e_link_event - Update netif_carrier status
  5446. * @pf: board private structure
  5447. **/
  5448. static void i40e_link_event(struct i40e_pf *pf)
  5449. {
  5450. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  5451. u8 new_link_speed, old_link_speed;
  5452. i40e_status status;
  5453. bool new_link, old_link;
  5454. /* save off old link status information */
  5455. pf->hw.phy.link_info_old = pf->hw.phy.link_info;
  5456. /* set this to force the get_link_status call to refresh state */
  5457. pf->hw.phy.get_link_info = true;
  5458. old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
  5459. status = i40e_get_link_status(&pf->hw, &new_link);
  5460. /* On success, disable temp link polling */
  5461. if (status == I40E_SUCCESS) {
  5462. if (pf->flags & I40E_FLAG_TEMP_LINK_POLLING)
  5463. pf->flags &= ~I40E_FLAG_TEMP_LINK_POLLING;
  5464. } else {
  5465. /* Enable link polling temporarily until i40e_get_link_status
  5466. * returns I40E_SUCCESS
  5467. */
  5468. pf->flags |= I40E_FLAG_TEMP_LINK_POLLING;
  5469. dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
  5470. status);
  5471. return;
  5472. }
  5473. old_link_speed = pf->hw.phy.link_info_old.link_speed;
  5474. new_link_speed = pf->hw.phy.link_info.link_speed;
  5475. if (new_link == old_link &&
  5476. new_link_speed == old_link_speed &&
  5477. (test_bit(__I40E_DOWN, &vsi->state) ||
  5478. new_link == netif_carrier_ok(vsi->netdev)))
  5479. return;
  5480. if (!test_bit(__I40E_DOWN, &vsi->state))
  5481. i40e_print_link_message(vsi, new_link);
  5482. /* Notify the base of the switch tree connected to
  5483. * the link. Floating VEBs are not notified.
  5484. */
  5485. if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
  5486. i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
  5487. else
  5488. i40e_vsi_link_event(vsi, new_link);
  5489. if (pf->vf)
  5490. i40e_vc_notify_link_state(pf);
  5491. if (pf->flags & I40E_FLAG_PTP)
  5492. i40e_ptp_set_increment(pf);
  5493. }
  5494. /**
  5495. * i40e_watchdog_subtask - periodic checks not using event driven response
  5496. * @pf: board private structure
  5497. **/
  5498. static void i40e_watchdog_subtask(struct i40e_pf *pf)
  5499. {
  5500. int i;
  5501. /* if interface is down do nothing */
  5502. if (test_bit(__I40E_DOWN, &pf->state) ||
  5503. test_bit(__I40E_CONFIG_BUSY, &pf->state))
  5504. return;
  5505. /* make sure we don't do these things too often */
  5506. if (time_before(jiffies, (pf->service_timer_previous +
  5507. pf->service_timer_period)))
  5508. return;
  5509. pf->service_timer_previous = jiffies;
  5510. if ((pf->flags & I40E_FLAG_LINK_POLLING_ENABLED) ||
  5511. (pf->flags & I40E_FLAG_TEMP_LINK_POLLING))
  5512. i40e_link_event(pf);
  5513. /* Update the stats for active netdevs so the network stack
  5514. * can look at updated numbers whenever it cares to
  5515. */
  5516. for (i = 0; i < pf->num_alloc_vsi; i++)
  5517. if (pf->vsi[i] && pf->vsi[i]->netdev)
  5518. i40e_update_stats(pf->vsi[i]);
  5519. if (pf->flags & I40E_FLAG_VEB_STATS_ENABLED) {
  5520. /* Update the stats for the active switching components */
  5521. for (i = 0; i < I40E_MAX_VEB; i++)
  5522. if (pf->veb[i])
  5523. i40e_update_veb_stats(pf->veb[i]);
  5524. }
  5525. i40e_ptp_rx_hang(pf->vsi[pf->lan_vsi]);
  5526. }
  5527. /**
  5528. * i40e_reset_subtask - Set up for resetting the device and driver
  5529. * @pf: board private structure
  5530. **/
  5531. static void i40e_reset_subtask(struct i40e_pf *pf)
  5532. {
  5533. u32 reset_flags = 0;
  5534. rtnl_lock();
  5535. if (test_bit(__I40E_REINIT_REQUESTED, &pf->state)) {
  5536. reset_flags |= BIT(__I40E_REINIT_REQUESTED);
  5537. clear_bit(__I40E_REINIT_REQUESTED, &pf->state);
  5538. }
  5539. if (test_bit(__I40E_PF_RESET_REQUESTED, &pf->state)) {
  5540. reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
  5541. clear_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
  5542. }
  5543. if (test_bit(__I40E_CORE_RESET_REQUESTED, &pf->state)) {
  5544. reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
  5545. clear_bit(__I40E_CORE_RESET_REQUESTED, &pf->state);
  5546. }
  5547. if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state)) {
  5548. reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
  5549. clear_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state);
  5550. }
  5551. if (test_bit(__I40E_DOWN_REQUESTED, &pf->state)) {
  5552. reset_flags |= BIT(__I40E_DOWN_REQUESTED);
  5553. clear_bit(__I40E_DOWN_REQUESTED, &pf->state);
  5554. }
  5555. /* If there's a recovery already waiting, it takes
  5556. * precedence before starting a new reset sequence.
  5557. */
  5558. if (test_bit(__I40E_RESET_INTR_RECEIVED, &pf->state)) {
  5559. i40e_handle_reset_warning(pf);
  5560. goto unlock;
  5561. }
  5562. /* If we're already down or resetting, just bail */
  5563. if (reset_flags &&
  5564. !test_bit(__I40E_DOWN, &pf->state) &&
  5565. !test_bit(__I40E_CONFIG_BUSY, &pf->state))
  5566. i40e_do_reset(pf, reset_flags);
  5567. unlock:
  5568. rtnl_unlock();
  5569. }
  5570. /**
  5571. * i40e_handle_link_event - Handle link event
  5572. * @pf: board private structure
  5573. * @e: event info posted on ARQ
  5574. **/
  5575. static void i40e_handle_link_event(struct i40e_pf *pf,
  5576. struct i40e_arq_event_info *e)
  5577. {
  5578. struct i40e_aqc_get_link_status *status =
  5579. (struct i40e_aqc_get_link_status *)&e->desc.params.raw;
  5580. /* Do a new status request to re-enable LSE reporting
  5581. * and load new status information into the hw struct
  5582. * This completely ignores any state information
  5583. * in the ARQ event info, instead choosing to always
  5584. * issue the AQ update link status command.
  5585. */
  5586. i40e_link_event(pf);
  5587. /* check for unqualified module, if link is down */
  5588. if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
  5589. (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
  5590. (!(status->link_info & I40E_AQ_LINK_UP)))
  5591. dev_err(&pf->pdev->dev,
  5592. "The driver failed to link because an unqualified module was detected.\n");
  5593. }
  5594. /**
  5595. * i40e_clean_adminq_subtask - Clean the AdminQ rings
  5596. * @pf: board private structure
  5597. **/
  5598. static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
  5599. {
  5600. struct i40e_arq_event_info event;
  5601. struct i40e_hw *hw = &pf->hw;
  5602. u16 pending, i = 0;
  5603. i40e_status ret;
  5604. u16 opcode;
  5605. u32 oldval;
  5606. u32 val;
  5607. /* Do not run clean AQ when PF reset fails */
  5608. if (test_bit(__I40E_RESET_FAILED, &pf->state))
  5609. return;
  5610. /* check for error indications */
  5611. val = rd32(&pf->hw, pf->hw.aq.arq.len);
  5612. oldval = val;
  5613. if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
  5614. if (hw->debug_mask & I40E_DEBUG_AQ)
  5615. dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
  5616. val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
  5617. }
  5618. if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
  5619. if (hw->debug_mask & I40E_DEBUG_AQ)
  5620. dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
  5621. val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
  5622. pf->arq_overflows++;
  5623. }
  5624. if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
  5625. if (hw->debug_mask & I40E_DEBUG_AQ)
  5626. dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
  5627. val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
  5628. }
  5629. if (oldval != val)
  5630. wr32(&pf->hw, pf->hw.aq.arq.len, val);
  5631. val = rd32(&pf->hw, pf->hw.aq.asq.len);
  5632. oldval = val;
  5633. if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
  5634. if (pf->hw.debug_mask & I40E_DEBUG_AQ)
  5635. dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
  5636. val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
  5637. }
  5638. if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
  5639. if (pf->hw.debug_mask & I40E_DEBUG_AQ)
  5640. dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
  5641. val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
  5642. }
  5643. if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
  5644. if (pf->hw.debug_mask & I40E_DEBUG_AQ)
  5645. dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
  5646. val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
  5647. }
  5648. if (oldval != val)
  5649. wr32(&pf->hw, pf->hw.aq.asq.len, val);
  5650. event.buf_len = I40E_MAX_AQ_BUF_SIZE;
  5651. event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
  5652. if (!event.msg_buf)
  5653. return;
  5654. do {
  5655. ret = i40e_clean_arq_element(hw, &event, &pending);
  5656. if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK)
  5657. break;
  5658. else if (ret) {
  5659. dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
  5660. break;
  5661. }
  5662. opcode = le16_to_cpu(event.desc.opcode);
  5663. switch (opcode) {
  5664. case i40e_aqc_opc_get_link_status:
  5665. i40e_handle_link_event(pf, &event);
  5666. break;
  5667. case i40e_aqc_opc_send_msg_to_pf:
  5668. ret = i40e_vc_process_vf_msg(pf,
  5669. le16_to_cpu(event.desc.retval),
  5670. le32_to_cpu(event.desc.cookie_high),
  5671. le32_to_cpu(event.desc.cookie_low),
  5672. event.msg_buf,
  5673. event.msg_len);
  5674. break;
  5675. case i40e_aqc_opc_lldp_update_mib:
  5676. dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
  5677. #ifdef CONFIG_I40E_DCB
  5678. rtnl_lock();
  5679. ret = i40e_handle_lldp_event(pf, &event);
  5680. rtnl_unlock();
  5681. #endif /* CONFIG_I40E_DCB */
  5682. break;
  5683. case i40e_aqc_opc_event_lan_overflow:
  5684. dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
  5685. i40e_handle_lan_overflow_event(pf, &event);
  5686. break;
  5687. case i40e_aqc_opc_send_msg_to_peer:
  5688. dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
  5689. break;
  5690. case i40e_aqc_opc_nvm_erase:
  5691. case i40e_aqc_opc_nvm_update:
  5692. case i40e_aqc_opc_oem_post_update:
  5693. i40e_debug(&pf->hw, I40E_DEBUG_NVM,
  5694. "ARQ NVM operation 0x%04x completed\n",
  5695. opcode);
  5696. break;
  5697. default:
  5698. dev_info(&pf->pdev->dev,
  5699. "ARQ: Unknown event 0x%04x ignored\n",
  5700. opcode);
  5701. break;
  5702. }
  5703. } while (i++ < pf->adminq_work_limit);
  5704. if (i < pf->adminq_work_limit)
  5705. clear_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
  5706. /* re-enable Admin queue interrupt cause */
  5707. val = rd32(hw, I40E_PFINT_ICR0_ENA);
  5708. val |= I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
  5709. wr32(hw, I40E_PFINT_ICR0_ENA, val);
  5710. i40e_flush(hw);
  5711. kfree(event.msg_buf);
  5712. }
  5713. /**
  5714. * i40e_verify_eeprom - make sure eeprom is good to use
  5715. * @pf: board private structure
  5716. **/
  5717. static void i40e_verify_eeprom(struct i40e_pf *pf)
  5718. {
  5719. int err;
  5720. err = i40e_diag_eeprom_test(&pf->hw);
  5721. if (err) {
  5722. /* retry in case of garbage read */
  5723. err = i40e_diag_eeprom_test(&pf->hw);
  5724. if (err) {
  5725. dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
  5726. err);
  5727. set_bit(__I40E_BAD_EEPROM, &pf->state);
  5728. }
  5729. }
  5730. if (!err && test_bit(__I40E_BAD_EEPROM, &pf->state)) {
  5731. dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
  5732. clear_bit(__I40E_BAD_EEPROM, &pf->state);
  5733. }
  5734. }
  5735. /**
  5736. * i40e_enable_pf_switch_lb
  5737. * @pf: pointer to the PF structure
  5738. *
  5739. * enable switch loop back or die - no point in a return value
  5740. **/
  5741. static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
  5742. {
  5743. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  5744. struct i40e_vsi_context ctxt;
  5745. int ret;
  5746. ctxt.seid = pf->main_vsi_seid;
  5747. ctxt.pf_num = pf->hw.pf_id;
  5748. ctxt.vf_num = 0;
  5749. ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
  5750. if (ret) {
  5751. dev_info(&pf->pdev->dev,
  5752. "couldn't get PF vsi config, err %s aq_err %s\n",
  5753. i40e_stat_str(&pf->hw, ret),
  5754. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  5755. return;
  5756. }
  5757. ctxt.flags = I40E_AQ_VSI_TYPE_PF;
  5758. ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  5759. ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  5760. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  5761. if (ret) {
  5762. dev_info(&pf->pdev->dev,
  5763. "update vsi switch failed, err %s aq_err %s\n",
  5764. i40e_stat_str(&pf->hw, ret),
  5765. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  5766. }
  5767. }
  5768. /**
  5769. * i40e_disable_pf_switch_lb
  5770. * @pf: pointer to the PF structure
  5771. *
  5772. * disable switch loop back or die - no point in a return value
  5773. **/
  5774. static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
  5775. {
  5776. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  5777. struct i40e_vsi_context ctxt;
  5778. int ret;
  5779. ctxt.seid = pf->main_vsi_seid;
  5780. ctxt.pf_num = pf->hw.pf_id;
  5781. ctxt.vf_num = 0;
  5782. ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
  5783. if (ret) {
  5784. dev_info(&pf->pdev->dev,
  5785. "couldn't get PF vsi config, err %s aq_err %s\n",
  5786. i40e_stat_str(&pf->hw, ret),
  5787. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  5788. return;
  5789. }
  5790. ctxt.flags = I40E_AQ_VSI_TYPE_PF;
  5791. ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  5792. ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  5793. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  5794. if (ret) {
  5795. dev_info(&pf->pdev->dev,
  5796. "update vsi switch failed, err %s aq_err %s\n",
  5797. i40e_stat_str(&pf->hw, ret),
  5798. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  5799. }
  5800. }
  5801. /**
  5802. * i40e_config_bridge_mode - Configure the HW bridge mode
  5803. * @veb: pointer to the bridge instance
  5804. *
  5805. * Configure the loop back mode for the LAN VSI that is downlink to the
  5806. * specified HW bridge instance. It is expected this function is called
  5807. * when a new HW bridge is instantiated.
  5808. **/
  5809. static void i40e_config_bridge_mode(struct i40e_veb *veb)
  5810. {
  5811. struct i40e_pf *pf = veb->pf;
  5812. if (pf->hw.debug_mask & I40E_DEBUG_LAN)
  5813. dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
  5814. veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
  5815. if (veb->bridge_mode & BRIDGE_MODE_VEPA)
  5816. i40e_disable_pf_switch_lb(pf);
  5817. else
  5818. i40e_enable_pf_switch_lb(pf);
  5819. }
  5820. /**
  5821. * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
  5822. * @veb: pointer to the VEB instance
  5823. *
  5824. * This is a recursive function that first builds the attached VSIs then
  5825. * recurses in to build the next layer of VEB. We track the connections
  5826. * through our own index numbers because the seid's from the HW could
  5827. * change across the reset.
  5828. **/
  5829. static int i40e_reconstitute_veb(struct i40e_veb *veb)
  5830. {
  5831. struct i40e_vsi *ctl_vsi = NULL;
  5832. struct i40e_pf *pf = veb->pf;
  5833. int v, veb_idx;
  5834. int ret;
  5835. /* build VSI that owns this VEB, temporarily attached to base VEB */
  5836. for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
  5837. if (pf->vsi[v] &&
  5838. pf->vsi[v]->veb_idx == veb->idx &&
  5839. pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
  5840. ctl_vsi = pf->vsi[v];
  5841. break;
  5842. }
  5843. }
  5844. if (!ctl_vsi) {
  5845. dev_info(&pf->pdev->dev,
  5846. "missing owner VSI for veb_idx %d\n", veb->idx);
  5847. ret = -ENOENT;
  5848. goto end_reconstitute;
  5849. }
  5850. if (ctl_vsi != pf->vsi[pf->lan_vsi])
  5851. ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
  5852. ret = i40e_add_vsi(ctl_vsi);
  5853. if (ret) {
  5854. dev_info(&pf->pdev->dev,
  5855. "rebuild of veb_idx %d owner VSI failed: %d\n",
  5856. veb->idx, ret);
  5857. goto end_reconstitute;
  5858. }
  5859. i40e_vsi_reset_stats(ctl_vsi);
  5860. /* create the VEB in the switch and move the VSI onto the VEB */
  5861. ret = i40e_add_veb(veb, ctl_vsi);
  5862. if (ret)
  5863. goto end_reconstitute;
  5864. if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
  5865. veb->bridge_mode = BRIDGE_MODE_VEB;
  5866. else
  5867. veb->bridge_mode = BRIDGE_MODE_VEPA;
  5868. i40e_config_bridge_mode(veb);
  5869. /* create the remaining VSIs attached to this VEB */
  5870. for (v = 0; v < pf->num_alloc_vsi; v++) {
  5871. if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
  5872. continue;
  5873. if (pf->vsi[v]->veb_idx == veb->idx) {
  5874. struct i40e_vsi *vsi = pf->vsi[v];
  5875. vsi->uplink_seid = veb->seid;
  5876. ret = i40e_add_vsi(vsi);
  5877. if (ret) {
  5878. dev_info(&pf->pdev->dev,
  5879. "rebuild of vsi_idx %d failed: %d\n",
  5880. v, ret);
  5881. goto end_reconstitute;
  5882. }
  5883. i40e_vsi_reset_stats(vsi);
  5884. }
  5885. }
  5886. /* create any VEBs attached to this VEB - RECURSION */
  5887. for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
  5888. if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
  5889. pf->veb[veb_idx]->uplink_seid = veb->seid;
  5890. ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
  5891. if (ret)
  5892. break;
  5893. }
  5894. }
  5895. end_reconstitute:
  5896. return ret;
  5897. }
  5898. /**
  5899. * i40e_get_capabilities - get info about the HW
  5900. * @pf: the PF struct
  5901. **/
  5902. static int i40e_get_capabilities(struct i40e_pf *pf)
  5903. {
  5904. struct i40e_aqc_list_capabilities_element_resp *cap_buf;
  5905. u16 data_size;
  5906. int buf_len;
  5907. int err;
  5908. buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
  5909. do {
  5910. cap_buf = kzalloc(buf_len, GFP_KERNEL);
  5911. if (!cap_buf)
  5912. return -ENOMEM;
  5913. /* this loads the data into the hw struct for us */
  5914. err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
  5915. &data_size,
  5916. i40e_aqc_opc_list_func_capabilities,
  5917. NULL);
  5918. /* data loaded, buffer no longer needed */
  5919. kfree(cap_buf);
  5920. if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
  5921. /* retry with a larger buffer */
  5922. buf_len = data_size;
  5923. } else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK) {
  5924. dev_info(&pf->pdev->dev,
  5925. "capability discovery failed, err %s aq_err %s\n",
  5926. i40e_stat_str(&pf->hw, err),
  5927. i40e_aq_str(&pf->hw,
  5928. pf->hw.aq.asq_last_status));
  5929. return -ENODEV;
  5930. }
  5931. } while (err);
  5932. if (pf->hw.debug_mask & I40E_DEBUG_USER)
  5933. dev_info(&pf->pdev->dev,
  5934. "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",
  5935. pf->hw.pf_id, pf->hw.func_caps.num_vfs,
  5936. pf->hw.func_caps.num_msix_vectors,
  5937. pf->hw.func_caps.num_msix_vectors_vf,
  5938. pf->hw.func_caps.fd_filters_guaranteed,
  5939. pf->hw.func_caps.fd_filters_best_effort,
  5940. pf->hw.func_caps.num_tx_qp,
  5941. pf->hw.func_caps.num_vsis);
  5942. #define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
  5943. + pf->hw.func_caps.num_vfs)
  5944. if (pf->hw.revision_id == 0 && (DEF_NUM_VSI > pf->hw.func_caps.num_vsis)) {
  5945. dev_info(&pf->pdev->dev,
  5946. "got num_vsis %d, setting num_vsis to %d\n",
  5947. pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
  5948. pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
  5949. }
  5950. return 0;
  5951. }
  5952. static int i40e_vsi_clear(struct i40e_vsi *vsi);
  5953. /**
  5954. * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
  5955. * @pf: board private structure
  5956. **/
  5957. static void i40e_fdir_sb_setup(struct i40e_pf *pf)
  5958. {
  5959. struct i40e_vsi *vsi;
  5960. /* quick workaround for an NVM issue that leaves a critical register
  5961. * uninitialized
  5962. */
  5963. if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
  5964. static const u32 hkey[] = {
  5965. 0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
  5966. 0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
  5967. 0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
  5968. 0x95b3a76d};
  5969. int i;
  5970. for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
  5971. wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
  5972. }
  5973. if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
  5974. return;
  5975. /* find existing VSI and see if it needs configuring */
  5976. vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
  5977. /* create a new VSI if none exists */
  5978. if (!vsi) {
  5979. vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
  5980. pf->vsi[pf->lan_vsi]->seid, 0);
  5981. if (!vsi) {
  5982. dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
  5983. pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
  5984. return;
  5985. }
  5986. }
  5987. i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
  5988. }
  5989. /**
  5990. * i40e_fdir_teardown - release the Flow Director resources
  5991. * @pf: board private structure
  5992. **/
  5993. static void i40e_fdir_teardown(struct i40e_pf *pf)
  5994. {
  5995. struct i40e_vsi *vsi;
  5996. i40e_fdir_filter_exit(pf);
  5997. vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
  5998. if (vsi)
  5999. i40e_vsi_release(vsi);
  6000. }
  6001. /**
  6002. * i40e_prep_for_reset - prep for the core to reset
  6003. * @pf: board private structure
  6004. *
  6005. * Close up the VFs and other things in prep for PF Reset.
  6006. **/
  6007. static void i40e_prep_for_reset(struct i40e_pf *pf)
  6008. {
  6009. struct i40e_hw *hw = &pf->hw;
  6010. i40e_status ret = 0;
  6011. u32 v;
  6012. clear_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
  6013. if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
  6014. return;
  6015. if (i40e_check_asq_alive(&pf->hw))
  6016. i40e_vc_notify_reset(pf);
  6017. dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
  6018. /* quiesce the VSIs and their queues that are not already DOWN */
  6019. i40e_pf_quiesce_all_vsi(pf);
  6020. for (v = 0; v < pf->num_alloc_vsi; v++) {
  6021. if (pf->vsi[v])
  6022. pf->vsi[v]->seid = 0;
  6023. }
  6024. i40e_shutdown_adminq(&pf->hw);
  6025. /* call shutdown HMC */
  6026. if (hw->hmc.hmc_obj) {
  6027. ret = i40e_shutdown_lan_hmc(hw);
  6028. if (ret)
  6029. dev_warn(&pf->pdev->dev,
  6030. "shutdown_lan_hmc failed: %d\n", ret);
  6031. }
  6032. }
  6033. /**
  6034. * i40e_send_version - update firmware with driver version
  6035. * @pf: PF struct
  6036. */
  6037. static void i40e_send_version(struct i40e_pf *pf)
  6038. {
  6039. struct i40e_driver_version dv;
  6040. dv.major_version = DRV_VERSION_MAJOR;
  6041. dv.minor_version = DRV_VERSION_MINOR;
  6042. dv.build_version = DRV_VERSION_BUILD;
  6043. dv.subbuild_version = 0;
  6044. strlcpy(dv.driver_string, DRV_VERSION, sizeof(dv.driver_string));
  6045. i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
  6046. }
  6047. /**
  6048. * i40e_reset_and_rebuild - reset and rebuild using a saved config
  6049. * @pf: board private structure
  6050. * @reinit: if the Main VSI needs to re-initialized.
  6051. **/
  6052. static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit)
  6053. {
  6054. struct i40e_hw *hw = &pf->hw;
  6055. u8 set_fc_aq_fail = 0;
  6056. i40e_status ret;
  6057. u32 val;
  6058. u32 v;
  6059. /* Now we wait for GRST to settle out.
  6060. * We don't have to delete the VEBs or VSIs from the hw switch
  6061. * because the reset will make them disappear.
  6062. */
  6063. ret = i40e_pf_reset(hw);
  6064. if (ret) {
  6065. dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
  6066. set_bit(__I40E_RESET_FAILED, &pf->state);
  6067. goto clear_recovery;
  6068. }
  6069. pf->pfr_count++;
  6070. if (test_bit(__I40E_DOWN, &pf->state))
  6071. goto clear_recovery;
  6072. dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
  6073. /* rebuild the basics for the AdminQ, HMC, and initial HW switch */
  6074. ret = i40e_init_adminq(&pf->hw);
  6075. if (ret) {
  6076. dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %s aq_err %s\n",
  6077. i40e_stat_str(&pf->hw, ret),
  6078. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  6079. goto clear_recovery;
  6080. }
  6081. /* re-verify the eeprom if we just had an EMP reset */
  6082. if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, &pf->state))
  6083. i40e_verify_eeprom(pf);
  6084. i40e_clear_pxe_mode(hw);
  6085. ret = i40e_get_capabilities(pf);
  6086. if (ret)
  6087. goto end_core_reset;
  6088. ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
  6089. hw->func_caps.num_rx_qp, 0, 0);
  6090. if (ret) {
  6091. dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
  6092. goto end_core_reset;
  6093. }
  6094. ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
  6095. if (ret) {
  6096. dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
  6097. goto end_core_reset;
  6098. }
  6099. #ifdef CONFIG_I40E_DCB
  6100. ret = i40e_init_pf_dcb(pf);
  6101. if (ret) {
  6102. dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n", ret);
  6103. pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
  6104. /* Continue without DCB enabled */
  6105. }
  6106. #endif /* CONFIG_I40E_DCB */
  6107. /* do basic switch setup */
  6108. ret = i40e_setup_pf_switch(pf, reinit);
  6109. if (ret)
  6110. goto end_core_reset;
  6111. /* The driver only wants link up/down and module qualification
  6112. * reports from firmware. Note the negative logic.
  6113. */
  6114. ret = i40e_aq_set_phy_int_mask(&pf->hw,
  6115. ~(I40E_AQ_EVENT_LINK_UPDOWN |
  6116. I40E_AQ_EVENT_MEDIA_NA |
  6117. I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
  6118. if (ret)
  6119. dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
  6120. i40e_stat_str(&pf->hw, ret),
  6121. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  6122. /* make sure our flow control settings are restored */
  6123. ret = i40e_set_fc(&pf->hw, &set_fc_aq_fail, true);
  6124. if (ret)
  6125. dev_dbg(&pf->pdev->dev, "setting flow control: ret = %s last_status = %s\n",
  6126. i40e_stat_str(&pf->hw, ret),
  6127. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  6128. /* Rebuild the VSIs and VEBs that existed before reset.
  6129. * They are still in our local switch element arrays, so only
  6130. * need to rebuild the switch model in the HW.
  6131. *
  6132. * If there were VEBs but the reconstitution failed, we'll try
  6133. * try to recover minimal use by getting the basic PF VSI working.
  6134. */
  6135. if (pf->vsi[pf->lan_vsi]->uplink_seid != pf->mac_seid) {
  6136. dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
  6137. /* find the one VEB connected to the MAC, and find orphans */
  6138. for (v = 0; v < I40E_MAX_VEB; v++) {
  6139. if (!pf->veb[v])
  6140. continue;
  6141. if (pf->veb[v]->uplink_seid == pf->mac_seid ||
  6142. pf->veb[v]->uplink_seid == 0) {
  6143. ret = i40e_reconstitute_veb(pf->veb[v]);
  6144. if (!ret)
  6145. continue;
  6146. /* If Main VEB failed, we're in deep doodoo,
  6147. * so give up rebuilding the switch and set up
  6148. * for minimal rebuild of PF VSI.
  6149. * If orphan failed, we'll report the error
  6150. * but try to keep going.
  6151. */
  6152. if (pf->veb[v]->uplink_seid == pf->mac_seid) {
  6153. dev_info(&pf->pdev->dev,
  6154. "rebuild of switch failed: %d, will try to set up simple PF connection\n",
  6155. ret);
  6156. pf->vsi[pf->lan_vsi]->uplink_seid
  6157. = pf->mac_seid;
  6158. break;
  6159. } else if (pf->veb[v]->uplink_seid == 0) {
  6160. dev_info(&pf->pdev->dev,
  6161. "rebuild of orphan VEB failed: %d\n",
  6162. ret);
  6163. }
  6164. }
  6165. }
  6166. }
  6167. if (pf->vsi[pf->lan_vsi]->uplink_seid == pf->mac_seid) {
  6168. dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
  6169. /* no VEB, so rebuild only the Main VSI */
  6170. ret = i40e_add_vsi(pf->vsi[pf->lan_vsi]);
  6171. if (ret) {
  6172. dev_info(&pf->pdev->dev,
  6173. "rebuild of Main VSI failed: %d\n", ret);
  6174. goto end_core_reset;
  6175. }
  6176. }
  6177. /* Reconfigure hardware for allowing smaller MSS in the case
  6178. * of TSO, so that we avoid the MDD being fired and causing
  6179. * a reset in the case of small MSS+TSO.
  6180. */
  6181. #define I40E_REG_MSS 0x000E64DC
  6182. #define I40E_REG_MSS_MIN_MASK 0x3FF0000
  6183. #define I40E_64BYTE_MSS 0x400000
  6184. val = rd32(hw, I40E_REG_MSS);
  6185. if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
  6186. val &= ~I40E_REG_MSS_MIN_MASK;
  6187. val |= I40E_64BYTE_MSS;
  6188. wr32(hw, I40E_REG_MSS, val);
  6189. }
  6190. if (pf->flags & I40E_FLAG_RESTART_AUTONEG) {
  6191. msleep(75);
  6192. ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
  6193. if (ret)
  6194. dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
  6195. i40e_stat_str(&pf->hw, ret),
  6196. i40e_aq_str(&pf->hw,
  6197. pf->hw.aq.asq_last_status));
  6198. }
  6199. /* reinit the misc interrupt */
  6200. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  6201. ret = i40e_setup_misc_vector(pf);
  6202. /* Add a filter to drop all Flow control frames from any VSI from being
  6203. * transmitted. By doing so we stop a malicious VF from sending out
  6204. * PAUSE or PFC frames and potentially controlling traffic for other
  6205. * PF/VF VSIs.
  6206. * The FW can still send Flow control frames if enabled.
  6207. */
  6208. i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
  6209. pf->main_vsi_seid);
  6210. /* restart the VSIs that were rebuilt and running before the reset */
  6211. i40e_pf_unquiesce_all_vsi(pf);
  6212. if (pf->num_alloc_vfs) {
  6213. for (v = 0; v < pf->num_alloc_vfs; v++)
  6214. i40e_reset_vf(&pf->vf[v], true);
  6215. }
  6216. /* tell the firmware that we're starting */
  6217. i40e_send_version(pf);
  6218. end_core_reset:
  6219. clear_bit(__I40E_RESET_FAILED, &pf->state);
  6220. clear_recovery:
  6221. clear_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state);
  6222. }
  6223. /**
  6224. * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
  6225. * @pf: board private structure
  6226. *
  6227. * Close up the VFs and other things in prep for a Core Reset,
  6228. * then get ready to rebuild the world.
  6229. **/
  6230. static void i40e_handle_reset_warning(struct i40e_pf *pf)
  6231. {
  6232. i40e_prep_for_reset(pf);
  6233. i40e_reset_and_rebuild(pf, false);
  6234. }
  6235. /**
  6236. * i40e_handle_mdd_event
  6237. * @pf: pointer to the PF structure
  6238. *
  6239. * Called from the MDD irq handler to identify possibly malicious vfs
  6240. **/
  6241. static void i40e_handle_mdd_event(struct i40e_pf *pf)
  6242. {
  6243. struct i40e_hw *hw = &pf->hw;
  6244. bool mdd_detected = false;
  6245. bool pf_mdd_detected = false;
  6246. struct i40e_vf *vf;
  6247. u32 reg;
  6248. int i;
  6249. if (!test_bit(__I40E_MDD_EVENT_PENDING, &pf->state))
  6250. return;
  6251. /* find what triggered the MDD event */
  6252. reg = rd32(hw, I40E_GL_MDET_TX);
  6253. if (reg & I40E_GL_MDET_TX_VALID_MASK) {
  6254. u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
  6255. I40E_GL_MDET_TX_PF_NUM_SHIFT;
  6256. u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
  6257. I40E_GL_MDET_TX_VF_NUM_SHIFT;
  6258. u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
  6259. I40E_GL_MDET_TX_EVENT_SHIFT;
  6260. u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
  6261. I40E_GL_MDET_TX_QUEUE_SHIFT) -
  6262. pf->hw.func_caps.base_queue;
  6263. if (netif_msg_tx_err(pf))
  6264. dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on TX queue %d PF number 0x%02x VF number 0x%02x\n",
  6265. event, queue, pf_num, vf_num);
  6266. wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
  6267. mdd_detected = true;
  6268. }
  6269. reg = rd32(hw, I40E_GL_MDET_RX);
  6270. if (reg & I40E_GL_MDET_RX_VALID_MASK) {
  6271. u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
  6272. I40E_GL_MDET_RX_FUNCTION_SHIFT;
  6273. u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
  6274. I40E_GL_MDET_RX_EVENT_SHIFT;
  6275. u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
  6276. I40E_GL_MDET_RX_QUEUE_SHIFT) -
  6277. pf->hw.func_caps.base_queue;
  6278. if (netif_msg_rx_err(pf))
  6279. dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
  6280. event, queue, func);
  6281. wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
  6282. mdd_detected = true;
  6283. }
  6284. if (mdd_detected) {
  6285. reg = rd32(hw, I40E_PF_MDET_TX);
  6286. if (reg & I40E_PF_MDET_TX_VALID_MASK) {
  6287. wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
  6288. dev_info(&pf->pdev->dev, "TX driver issue detected, PF reset issued\n");
  6289. pf_mdd_detected = true;
  6290. }
  6291. reg = rd32(hw, I40E_PF_MDET_RX);
  6292. if (reg & I40E_PF_MDET_RX_VALID_MASK) {
  6293. wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
  6294. dev_info(&pf->pdev->dev, "RX driver issue detected, PF reset issued\n");
  6295. pf_mdd_detected = true;
  6296. }
  6297. /* Queue belongs to the PF, initiate a reset */
  6298. if (pf_mdd_detected) {
  6299. set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
  6300. i40e_service_event_schedule(pf);
  6301. }
  6302. }
  6303. /* see if one of the VFs needs its hand slapped */
  6304. for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
  6305. vf = &(pf->vf[i]);
  6306. reg = rd32(hw, I40E_VP_MDET_TX(i));
  6307. if (reg & I40E_VP_MDET_TX_VALID_MASK) {
  6308. wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
  6309. vf->num_mdd_events++;
  6310. dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
  6311. i);
  6312. }
  6313. reg = rd32(hw, I40E_VP_MDET_RX(i));
  6314. if (reg & I40E_VP_MDET_RX_VALID_MASK) {
  6315. wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
  6316. vf->num_mdd_events++;
  6317. dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
  6318. i);
  6319. }
  6320. if (vf->num_mdd_events > I40E_DEFAULT_NUM_MDD_EVENTS_ALLOWED) {
  6321. dev_info(&pf->pdev->dev,
  6322. "Too many MDD events on VF %d, disabled\n", i);
  6323. dev_info(&pf->pdev->dev,
  6324. "Use PF Control I/F to re-enable the VF\n");
  6325. set_bit(I40E_VF_STAT_DISABLED, &vf->vf_states);
  6326. }
  6327. }
  6328. /* re-enable mdd interrupt cause */
  6329. clear_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
  6330. reg = rd32(hw, I40E_PFINT_ICR0_ENA);
  6331. reg |= I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
  6332. wr32(hw, I40E_PFINT_ICR0_ENA, reg);
  6333. i40e_flush(hw);
  6334. }
  6335. /**
  6336. * i40e_sync_udp_filters_subtask - Sync the VSI filter list with HW
  6337. * @pf: board private structure
  6338. **/
  6339. static void i40e_sync_udp_filters_subtask(struct i40e_pf *pf)
  6340. {
  6341. struct i40e_hw *hw = &pf->hw;
  6342. i40e_status ret;
  6343. u16 port;
  6344. int i;
  6345. if (!(pf->flags & I40E_FLAG_UDP_FILTER_SYNC))
  6346. return;
  6347. pf->flags &= ~I40E_FLAG_UDP_FILTER_SYNC;
  6348. for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
  6349. if (pf->pending_udp_bitmap & BIT_ULL(i)) {
  6350. pf->pending_udp_bitmap &= ~BIT_ULL(i);
  6351. port = pf->udp_ports[i].index;
  6352. if (port)
  6353. ret = i40e_aq_add_udp_tunnel(hw, port,
  6354. pf->udp_ports[i].type,
  6355. NULL, NULL);
  6356. else
  6357. ret = i40e_aq_del_udp_tunnel(hw, i, NULL);
  6358. if (ret) {
  6359. dev_dbg(&pf->pdev->dev,
  6360. "%s %s port %d, index %d failed, err %s aq_err %s\n",
  6361. pf->udp_ports[i].type ? "vxlan" : "geneve",
  6362. port ? "add" : "delete",
  6363. port, i,
  6364. i40e_stat_str(&pf->hw, ret),
  6365. i40e_aq_str(&pf->hw,
  6366. pf->hw.aq.asq_last_status));
  6367. pf->udp_ports[i].index = 0;
  6368. }
  6369. }
  6370. }
  6371. }
  6372. /**
  6373. * i40e_service_task - Run the driver's async subtasks
  6374. * @work: pointer to work_struct containing our data
  6375. **/
  6376. static void i40e_service_task(struct work_struct *work)
  6377. {
  6378. struct i40e_pf *pf = container_of(work,
  6379. struct i40e_pf,
  6380. service_task);
  6381. unsigned long start_time = jiffies;
  6382. /* don't bother with service tasks if a reset is in progress */
  6383. if (test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
  6384. return;
  6385. }
  6386. if (test_and_set_bit(__I40E_SERVICE_SCHED, &pf->state))
  6387. return;
  6388. i40e_detect_recover_hung(pf);
  6389. i40e_sync_filters_subtask(pf);
  6390. i40e_reset_subtask(pf);
  6391. i40e_handle_mdd_event(pf);
  6392. i40e_vc_process_vflr_event(pf);
  6393. i40e_watchdog_subtask(pf);
  6394. i40e_fdir_reinit_subtask(pf);
  6395. if (pf->flags & I40E_FLAG_CLIENT_RESET) {
  6396. /* Client subtask will reopen next time through. */
  6397. i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], true);
  6398. pf->flags &= ~I40E_FLAG_CLIENT_RESET;
  6399. } else {
  6400. i40e_client_subtask(pf);
  6401. if (pf->flags & I40E_FLAG_CLIENT_L2_CHANGE) {
  6402. i40e_notify_client_of_l2_param_changes(
  6403. pf->vsi[pf->lan_vsi]);
  6404. pf->flags &= ~I40E_FLAG_CLIENT_L2_CHANGE;
  6405. }
  6406. }
  6407. i40e_sync_filters_subtask(pf);
  6408. i40e_sync_udp_filters_subtask(pf);
  6409. i40e_clean_adminq_subtask(pf);
  6410. /* flush memory to make sure state is correct before next watchdog */
  6411. smp_mb__before_atomic();
  6412. clear_bit(__I40E_SERVICE_SCHED, &pf->state);
  6413. /* If the tasks have taken longer than one timer cycle or there
  6414. * is more work to be done, reschedule the service task now
  6415. * rather than wait for the timer to tick again.
  6416. */
  6417. if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
  6418. test_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state) ||
  6419. test_bit(__I40E_MDD_EVENT_PENDING, &pf->state) ||
  6420. test_bit(__I40E_VFLR_EVENT_PENDING, &pf->state))
  6421. i40e_service_event_schedule(pf);
  6422. }
  6423. /**
  6424. * i40e_service_timer - timer callback
  6425. * @data: pointer to PF struct
  6426. **/
  6427. static void i40e_service_timer(unsigned long data)
  6428. {
  6429. struct i40e_pf *pf = (struct i40e_pf *)data;
  6430. mod_timer(&pf->service_timer,
  6431. round_jiffies(jiffies + pf->service_timer_period));
  6432. i40e_service_event_schedule(pf);
  6433. }
  6434. /**
  6435. * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
  6436. * @vsi: the VSI being configured
  6437. **/
  6438. static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
  6439. {
  6440. struct i40e_pf *pf = vsi->back;
  6441. switch (vsi->type) {
  6442. case I40E_VSI_MAIN:
  6443. vsi->alloc_queue_pairs = pf->num_lan_qps;
  6444. vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
  6445. I40E_REQ_DESCRIPTOR_MULTIPLE);
  6446. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  6447. vsi->num_q_vectors = pf->num_lan_msix;
  6448. else
  6449. vsi->num_q_vectors = 1;
  6450. break;
  6451. case I40E_VSI_FDIR:
  6452. vsi->alloc_queue_pairs = 1;
  6453. vsi->num_desc = ALIGN(I40E_FDIR_RING_COUNT,
  6454. I40E_REQ_DESCRIPTOR_MULTIPLE);
  6455. vsi->num_q_vectors = pf->num_fdsb_msix;
  6456. break;
  6457. case I40E_VSI_VMDQ2:
  6458. vsi->alloc_queue_pairs = pf->num_vmdq_qps;
  6459. vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
  6460. I40E_REQ_DESCRIPTOR_MULTIPLE);
  6461. vsi->num_q_vectors = pf->num_vmdq_msix;
  6462. break;
  6463. case I40E_VSI_SRIOV:
  6464. vsi->alloc_queue_pairs = pf->num_vf_qps;
  6465. vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
  6466. I40E_REQ_DESCRIPTOR_MULTIPLE);
  6467. break;
  6468. default:
  6469. WARN_ON(1);
  6470. return -ENODATA;
  6471. }
  6472. return 0;
  6473. }
  6474. /**
  6475. * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
  6476. * @type: VSI pointer
  6477. * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
  6478. *
  6479. * On error: returns error code (negative)
  6480. * On success: returns 0
  6481. **/
  6482. static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
  6483. {
  6484. int size;
  6485. int ret = 0;
  6486. /* allocate memory for both Tx and Rx ring pointers */
  6487. size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs * 2;
  6488. vsi->tx_rings = kzalloc(size, GFP_KERNEL);
  6489. if (!vsi->tx_rings)
  6490. return -ENOMEM;
  6491. vsi->rx_rings = &vsi->tx_rings[vsi->alloc_queue_pairs];
  6492. if (alloc_qvectors) {
  6493. /* allocate memory for q_vector pointers */
  6494. size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
  6495. vsi->q_vectors = kzalloc(size, GFP_KERNEL);
  6496. if (!vsi->q_vectors) {
  6497. ret = -ENOMEM;
  6498. goto err_vectors;
  6499. }
  6500. }
  6501. return ret;
  6502. err_vectors:
  6503. kfree(vsi->tx_rings);
  6504. return ret;
  6505. }
  6506. /**
  6507. * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
  6508. * @pf: board private structure
  6509. * @type: type of VSI
  6510. *
  6511. * On error: returns error code (negative)
  6512. * On success: returns vsi index in PF (positive)
  6513. **/
  6514. static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
  6515. {
  6516. int ret = -ENODEV;
  6517. struct i40e_vsi *vsi;
  6518. int vsi_idx;
  6519. int i;
  6520. /* Need to protect the allocation of the VSIs at the PF level */
  6521. mutex_lock(&pf->switch_mutex);
  6522. /* VSI list may be fragmented if VSI creation/destruction has
  6523. * been happening. We can afford to do a quick scan to look
  6524. * for any free VSIs in the list.
  6525. *
  6526. * find next empty vsi slot, looping back around if necessary
  6527. */
  6528. i = pf->next_vsi;
  6529. while (i < pf->num_alloc_vsi && pf->vsi[i])
  6530. i++;
  6531. if (i >= pf->num_alloc_vsi) {
  6532. i = 0;
  6533. while (i < pf->next_vsi && pf->vsi[i])
  6534. i++;
  6535. }
  6536. if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
  6537. vsi_idx = i; /* Found one! */
  6538. } else {
  6539. ret = -ENODEV;
  6540. goto unlock_pf; /* out of VSI slots! */
  6541. }
  6542. pf->next_vsi = ++i;
  6543. vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
  6544. if (!vsi) {
  6545. ret = -ENOMEM;
  6546. goto unlock_pf;
  6547. }
  6548. vsi->type = type;
  6549. vsi->back = pf;
  6550. set_bit(__I40E_DOWN, &vsi->state);
  6551. vsi->flags = 0;
  6552. vsi->idx = vsi_idx;
  6553. vsi->int_rate_limit = 0;
  6554. vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
  6555. pf->rss_table_size : 64;
  6556. vsi->netdev_registered = false;
  6557. vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
  6558. hash_init(vsi->mac_filter_hash);
  6559. vsi->irqs_ready = false;
  6560. ret = i40e_set_num_rings_in_vsi(vsi);
  6561. if (ret)
  6562. goto err_rings;
  6563. ret = i40e_vsi_alloc_arrays(vsi, true);
  6564. if (ret)
  6565. goto err_rings;
  6566. /* Setup default MSIX irq handler for VSI */
  6567. i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
  6568. /* Initialize VSI lock */
  6569. spin_lock_init(&vsi->mac_filter_hash_lock);
  6570. pf->vsi[vsi_idx] = vsi;
  6571. ret = vsi_idx;
  6572. goto unlock_pf;
  6573. err_rings:
  6574. pf->next_vsi = i - 1;
  6575. kfree(vsi);
  6576. unlock_pf:
  6577. mutex_unlock(&pf->switch_mutex);
  6578. return ret;
  6579. }
  6580. /**
  6581. * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
  6582. * @type: VSI pointer
  6583. * @free_qvectors: a bool to specify if q_vectors need to be freed.
  6584. *
  6585. * On error: returns error code (negative)
  6586. * On success: returns 0
  6587. **/
  6588. static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
  6589. {
  6590. /* free the ring and vector containers */
  6591. if (free_qvectors) {
  6592. kfree(vsi->q_vectors);
  6593. vsi->q_vectors = NULL;
  6594. }
  6595. kfree(vsi->tx_rings);
  6596. vsi->tx_rings = NULL;
  6597. vsi->rx_rings = NULL;
  6598. }
  6599. /**
  6600. * i40e_clear_rss_config_user - clear the user configured RSS hash keys
  6601. * and lookup table
  6602. * @vsi: Pointer to VSI structure
  6603. */
  6604. static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
  6605. {
  6606. if (!vsi)
  6607. return;
  6608. kfree(vsi->rss_hkey_user);
  6609. vsi->rss_hkey_user = NULL;
  6610. kfree(vsi->rss_lut_user);
  6611. vsi->rss_lut_user = NULL;
  6612. }
  6613. /**
  6614. * i40e_vsi_clear - Deallocate the VSI provided
  6615. * @vsi: the VSI being un-configured
  6616. **/
  6617. static int i40e_vsi_clear(struct i40e_vsi *vsi)
  6618. {
  6619. struct i40e_pf *pf;
  6620. if (!vsi)
  6621. return 0;
  6622. if (!vsi->back)
  6623. goto free_vsi;
  6624. pf = vsi->back;
  6625. mutex_lock(&pf->switch_mutex);
  6626. if (!pf->vsi[vsi->idx]) {
  6627. dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](%p,type %d)\n",
  6628. vsi->idx, vsi->idx, vsi, vsi->type);
  6629. goto unlock_vsi;
  6630. }
  6631. if (pf->vsi[vsi->idx] != vsi) {
  6632. dev_err(&pf->pdev->dev,
  6633. "pf->vsi[%d](%p, type %d) != vsi[%d](%p,type %d): no free!\n",
  6634. pf->vsi[vsi->idx]->idx,
  6635. pf->vsi[vsi->idx],
  6636. pf->vsi[vsi->idx]->type,
  6637. vsi->idx, vsi, vsi->type);
  6638. goto unlock_vsi;
  6639. }
  6640. /* updates the PF for this cleared vsi */
  6641. i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
  6642. i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
  6643. i40e_vsi_free_arrays(vsi, true);
  6644. i40e_clear_rss_config_user(vsi);
  6645. pf->vsi[vsi->idx] = NULL;
  6646. if (vsi->idx < pf->next_vsi)
  6647. pf->next_vsi = vsi->idx;
  6648. unlock_vsi:
  6649. mutex_unlock(&pf->switch_mutex);
  6650. free_vsi:
  6651. kfree(vsi);
  6652. return 0;
  6653. }
  6654. /**
  6655. * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
  6656. * @vsi: the VSI being cleaned
  6657. **/
  6658. static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
  6659. {
  6660. int i;
  6661. if (vsi->tx_rings && vsi->tx_rings[0]) {
  6662. for (i = 0; i < vsi->alloc_queue_pairs; i++) {
  6663. kfree_rcu(vsi->tx_rings[i], rcu);
  6664. vsi->tx_rings[i] = NULL;
  6665. vsi->rx_rings[i] = NULL;
  6666. }
  6667. }
  6668. }
  6669. /**
  6670. * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
  6671. * @vsi: the VSI being configured
  6672. **/
  6673. static int i40e_alloc_rings(struct i40e_vsi *vsi)
  6674. {
  6675. struct i40e_ring *tx_ring, *rx_ring;
  6676. struct i40e_pf *pf = vsi->back;
  6677. int i;
  6678. /* Set basic values in the rings to be used later during open() */
  6679. for (i = 0; i < vsi->alloc_queue_pairs; i++) {
  6680. /* allocate space for both Tx and Rx in one shot */
  6681. tx_ring = kzalloc(sizeof(struct i40e_ring) * 2, GFP_KERNEL);
  6682. if (!tx_ring)
  6683. goto err_out;
  6684. tx_ring->queue_index = i;
  6685. tx_ring->reg_idx = vsi->base_queue + i;
  6686. tx_ring->ring_active = false;
  6687. tx_ring->vsi = vsi;
  6688. tx_ring->netdev = vsi->netdev;
  6689. tx_ring->dev = &pf->pdev->dev;
  6690. tx_ring->count = vsi->num_desc;
  6691. tx_ring->size = 0;
  6692. tx_ring->dcb_tc = 0;
  6693. if (vsi->back->flags & I40E_FLAG_WB_ON_ITR_CAPABLE)
  6694. tx_ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
  6695. tx_ring->tx_itr_setting = pf->tx_itr_default;
  6696. vsi->tx_rings[i] = tx_ring;
  6697. rx_ring = &tx_ring[1];
  6698. rx_ring->queue_index = i;
  6699. rx_ring->reg_idx = vsi->base_queue + i;
  6700. rx_ring->ring_active = false;
  6701. rx_ring->vsi = vsi;
  6702. rx_ring->netdev = vsi->netdev;
  6703. rx_ring->dev = &pf->pdev->dev;
  6704. rx_ring->count = vsi->num_desc;
  6705. rx_ring->size = 0;
  6706. rx_ring->dcb_tc = 0;
  6707. rx_ring->rx_itr_setting = pf->rx_itr_default;
  6708. vsi->rx_rings[i] = rx_ring;
  6709. }
  6710. return 0;
  6711. err_out:
  6712. i40e_vsi_clear_rings(vsi);
  6713. return -ENOMEM;
  6714. }
  6715. /**
  6716. * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
  6717. * @pf: board private structure
  6718. * @vectors: the number of MSI-X vectors to request
  6719. *
  6720. * Returns the number of vectors reserved, or error
  6721. **/
  6722. static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
  6723. {
  6724. vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
  6725. I40E_MIN_MSIX, vectors);
  6726. if (vectors < 0) {
  6727. dev_info(&pf->pdev->dev,
  6728. "MSI-X vector reservation failed: %d\n", vectors);
  6729. vectors = 0;
  6730. }
  6731. return vectors;
  6732. }
  6733. /**
  6734. * i40e_init_msix - Setup the MSIX capability
  6735. * @pf: board private structure
  6736. *
  6737. * Work with the OS to set up the MSIX vectors needed.
  6738. *
  6739. * Returns the number of vectors reserved or negative on failure
  6740. **/
  6741. static int i40e_init_msix(struct i40e_pf *pf)
  6742. {
  6743. struct i40e_hw *hw = &pf->hw;
  6744. int cpus, extra_vectors;
  6745. int vectors_left;
  6746. int v_budget, i;
  6747. int v_actual;
  6748. int iwarp_requested = 0;
  6749. if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
  6750. return -ENODEV;
  6751. /* The number of vectors we'll request will be comprised of:
  6752. * - Add 1 for "other" cause for Admin Queue events, etc.
  6753. * - The number of LAN queue pairs
  6754. * - Queues being used for RSS.
  6755. * We don't need as many as max_rss_size vectors.
  6756. * use rss_size instead in the calculation since that
  6757. * is governed by number of cpus in the system.
  6758. * - assumes symmetric Tx/Rx pairing
  6759. * - The number of VMDq pairs
  6760. * - The CPU count within the NUMA node if iWARP is enabled
  6761. * Once we count this up, try the request.
  6762. *
  6763. * If we can't get what we want, we'll simplify to nearly nothing
  6764. * and try again. If that still fails, we punt.
  6765. */
  6766. vectors_left = hw->func_caps.num_msix_vectors;
  6767. v_budget = 0;
  6768. /* reserve one vector for miscellaneous handler */
  6769. if (vectors_left) {
  6770. v_budget++;
  6771. vectors_left--;
  6772. }
  6773. /* reserve some vectors for the main PF traffic queues. Initially we
  6774. * only reserve at most 50% of the available vectors, in the case that
  6775. * the number of online CPUs is large. This ensures that we can enable
  6776. * extra features as well. Once we've enabled the other features, we
  6777. * will use any remaining vectors to reach as close as we can to the
  6778. * number of online CPUs.
  6779. */
  6780. cpus = num_online_cpus();
  6781. pf->num_lan_msix = min_t(int, cpus, vectors_left / 2);
  6782. vectors_left -= pf->num_lan_msix;
  6783. /* reserve one vector for sideband flow director */
  6784. if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
  6785. if (vectors_left) {
  6786. pf->num_fdsb_msix = 1;
  6787. v_budget++;
  6788. vectors_left--;
  6789. } else {
  6790. pf->num_fdsb_msix = 0;
  6791. }
  6792. }
  6793. /* can we reserve enough for iWARP? */
  6794. if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
  6795. iwarp_requested = pf->num_iwarp_msix;
  6796. if (!vectors_left)
  6797. pf->num_iwarp_msix = 0;
  6798. else if (vectors_left < pf->num_iwarp_msix)
  6799. pf->num_iwarp_msix = 1;
  6800. v_budget += pf->num_iwarp_msix;
  6801. vectors_left -= pf->num_iwarp_msix;
  6802. }
  6803. /* any vectors left over go for VMDq support */
  6804. if (pf->flags & I40E_FLAG_VMDQ_ENABLED) {
  6805. int vmdq_vecs_wanted = pf->num_vmdq_vsis * pf->num_vmdq_qps;
  6806. int vmdq_vecs = min_t(int, vectors_left, vmdq_vecs_wanted);
  6807. if (!vectors_left) {
  6808. pf->num_vmdq_msix = 0;
  6809. pf->num_vmdq_qps = 0;
  6810. } else {
  6811. /* if we're short on vectors for what's desired, we limit
  6812. * the queues per vmdq. If this is still more than are
  6813. * available, the user will need to change the number of
  6814. * queues/vectors used by the PF later with the ethtool
  6815. * channels command
  6816. */
  6817. if (vmdq_vecs < vmdq_vecs_wanted)
  6818. pf->num_vmdq_qps = 1;
  6819. pf->num_vmdq_msix = pf->num_vmdq_qps;
  6820. v_budget += vmdq_vecs;
  6821. vectors_left -= vmdq_vecs;
  6822. }
  6823. }
  6824. /* On systems with a large number of SMP cores, we previously limited
  6825. * the number of vectors for num_lan_msix to be at most 50% of the
  6826. * available vectors, to allow for other features. Now, we add back
  6827. * the remaining vectors. However, we ensure that the total
  6828. * num_lan_msix will not exceed num_online_cpus(). To do this, we
  6829. * calculate the number of vectors we can add without going over the
  6830. * cap of CPUs. For systems with a small number of CPUs this will be
  6831. * zero.
  6832. */
  6833. extra_vectors = min_t(int, cpus - pf->num_lan_msix, vectors_left);
  6834. pf->num_lan_msix += extra_vectors;
  6835. vectors_left -= extra_vectors;
  6836. WARN(vectors_left < 0,
  6837. "Calculation of remaining vectors underflowed. This is an accounting bug when determining total MSI-X vectors.\n");
  6838. v_budget += pf->num_lan_msix;
  6839. pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
  6840. GFP_KERNEL);
  6841. if (!pf->msix_entries)
  6842. return -ENOMEM;
  6843. for (i = 0; i < v_budget; i++)
  6844. pf->msix_entries[i].entry = i;
  6845. v_actual = i40e_reserve_msix_vectors(pf, v_budget);
  6846. if (v_actual < I40E_MIN_MSIX) {
  6847. pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
  6848. kfree(pf->msix_entries);
  6849. pf->msix_entries = NULL;
  6850. pci_disable_msix(pf->pdev);
  6851. return -ENODEV;
  6852. } else if (v_actual == I40E_MIN_MSIX) {
  6853. /* Adjust for minimal MSIX use */
  6854. pf->num_vmdq_vsis = 0;
  6855. pf->num_vmdq_qps = 0;
  6856. pf->num_lan_qps = 1;
  6857. pf->num_lan_msix = 1;
  6858. } else if (!vectors_left) {
  6859. /* If we have limited resources, we will start with no vectors
  6860. * for the special features and then allocate vectors to some
  6861. * of these features based on the policy and at the end disable
  6862. * the features that did not get any vectors.
  6863. */
  6864. int vec;
  6865. dev_info(&pf->pdev->dev,
  6866. "MSI-X vector limit reached, attempting to redistribute vectors\n");
  6867. /* reserve the misc vector */
  6868. vec = v_actual - 1;
  6869. /* Scale vector usage down */
  6870. pf->num_vmdq_msix = 1; /* force VMDqs to only one vector */
  6871. pf->num_vmdq_vsis = 1;
  6872. pf->num_vmdq_qps = 1;
  6873. /* partition out the remaining vectors */
  6874. switch (vec) {
  6875. case 2:
  6876. pf->num_lan_msix = 1;
  6877. break;
  6878. case 3:
  6879. if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
  6880. pf->num_lan_msix = 1;
  6881. pf->num_iwarp_msix = 1;
  6882. } else {
  6883. pf->num_lan_msix = 2;
  6884. }
  6885. break;
  6886. default:
  6887. if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
  6888. pf->num_iwarp_msix = min_t(int, (vec / 3),
  6889. iwarp_requested);
  6890. pf->num_vmdq_vsis = min_t(int, (vec / 3),
  6891. I40E_DEFAULT_NUM_VMDQ_VSI);
  6892. } else {
  6893. pf->num_vmdq_vsis = min_t(int, (vec / 2),
  6894. I40E_DEFAULT_NUM_VMDQ_VSI);
  6895. }
  6896. if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
  6897. pf->num_fdsb_msix = 1;
  6898. vec--;
  6899. }
  6900. pf->num_lan_msix = min_t(int,
  6901. (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
  6902. pf->num_lan_msix);
  6903. pf->num_lan_qps = pf->num_lan_msix;
  6904. break;
  6905. }
  6906. }
  6907. if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
  6908. (pf->num_fdsb_msix == 0)) {
  6909. dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n");
  6910. pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
  6911. }
  6912. if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
  6913. (pf->num_vmdq_msix == 0)) {
  6914. dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
  6915. pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
  6916. }
  6917. if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
  6918. (pf->num_iwarp_msix == 0)) {
  6919. dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
  6920. pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
  6921. }
  6922. i40e_debug(&pf->hw, I40E_DEBUG_INIT,
  6923. "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n",
  6924. pf->num_lan_msix,
  6925. pf->num_vmdq_msix * pf->num_vmdq_vsis,
  6926. pf->num_fdsb_msix,
  6927. pf->num_iwarp_msix);
  6928. return v_actual;
  6929. }
  6930. /**
  6931. * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
  6932. * @vsi: the VSI being configured
  6933. * @v_idx: index of the vector in the vsi struct
  6934. * @cpu: cpu to be used on affinity_mask
  6935. *
  6936. * We allocate one q_vector. If allocation fails we return -ENOMEM.
  6937. **/
  6938. static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx, int cpu)
  6939. {
  6940. struct i40e_q_vector *q_vector;
  6941. /* allocate q_vector */
  6942. q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
  6943. if (!q_vector)
  6944. return -ENOMEM;
  6945. q_vector->vsi = vsi;
  6946. q_vector->v_idx = v_idx;
  6947. cpumask_set_cpu(cpu, &q_vector->affinity_mask);
  6948. if (vsi->netdev)
  6949. netif_napi_add(vsi->netdev, &q_vector->napi,
  6950. i40e_napi_poll, NAPI_POLL_WEIGHT);
  6951. q_vector->rx.latency_range = I40E_LOW_LATENCY;
  6952. q_vector->tx.latency_range = I40E_LOW_LATENCY;
  6953. /* tie q_vector and vsi together */
  6954. vsi->q_vectors[v_idx] = q_vector;
  6955. return 0;
  6956. }
  6957. /**
  6958. * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
  6959. * @vsi: the VSI being configured
  6960. *
  6961. * We allocate one q_vector per queue interrupt. If allocation fails we
  6962. * return -ENOMEM.
  6963. **/
  6964. static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
  6965. {
  6966. struct i40e_pf *pf = vsi->back;
  6967. int err, v_idx, num_q_vectors, current_cpu;
  6968. /* if not MSIX, give the one vector only to the LAN VSI */
  6969. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  6970. num_q_vectors = vsi->num_q_vectors;
  6971. else if (vsi == pf->vsi[pf->lan_vsi])
  6972. num_q_vectors = 1;
  6973. else
  6974. return -EINVAL;
  6975. current_cpu = cpumask_first(cpu_online_mask);
  6976. for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
  6977. err = i40e_vsi_alloc_q_vector(vsi, v_idx, current_cpu);
  6978. if (err)
  6979. goto err_out;
  6980. current_cpu = cpumask_next(current_cpu, cpu_online_mask);
  6981. if (unlikely(current_cpu >= nr_cpu_ids))
  6982. current_cpu = cpumask_first(cpu_online_mask);
  6983. }
  6984. return 0;
  6985. err_out:
  6986. while (v_idx--)
  6987. i40e_free_q_vector(vsi, v_idx);
  6988. return err;
  6989. }
  6990. /**
  6991. * i40e_init_interrupt_scheme - Determine proper interrupt scheme
  6992. * @pf: board private structure to initialize
  6993. **/
  6994. static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
  6995. {
  6996. int vectors = 0;
  6997. ssize_t size;
  6998. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  6999. vectors = i40e_init_msix(pf);
  7000. if (vectors < 0) {
  7001. pf->flags &= ~(I40E_FLAG_MSIX_ENABLED |
  7002. I40E_FLAG_IWARP_ENABLED |
  7003. I40E_FLAG_RSS_ENABLED |
  7004. I40E_FLAG_DCB_CAPABLE |
  7005. I40E_FLAG_DCB_ENABLED |
  7006. I40E_FLAG_SRIOV_ENABLED |
  7007. I40E_FLAG_FD_SB_ENABLED |
  7008. I40E_FLAG_FD_ATR_ENABLED |
  7009. I40E_FLAG_VMDQ_ENABLED);
  7010. /* rework the queue expectations without MSIX */
  7011. i40e_determine_queue_usage(pf);
  7012. }
  7013. }
  7014. if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
  7015. (pf->flags & I40E_FLAG_MSI_ENABLED)) {
  7016. dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
  7017. vectors = pci_enable_msi(pf->pdev);
  7018. if (vectors < 0) {
  7019. dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
  7020. vectors);
  7021. pf->flags &= ~I40E_FLAG_MSI_ENABLED;
  7022. }
  7023. vectors = 1; /* one MSI or Legacy vector */
  7024. }
  7025. if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
  7026. dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
  7027. /* set up vector assignment tracking */
  7028. size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
  7029. pf->irq_pile = kzalloc(size, GFP_KERNEL);
  7030. if (!pf->irq_pile) {
  7031. dev_err(&pf->pdev->dev, "error allocating irq_pile memory\n");
  7032. return -ENOMEM;
  7033. }
  7034. pf->irq_pile->num_entries = vectors;
  7035. pf->irq_pile->search_hint = 0;
  7036. /* track first vector for misc interrupts, ignore return */
  7037. (void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
  7038. return 0;
  7039. }
  7040. /**
  7041. * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
  7042. * @pf: board private structure
  7043. *
  7044. * This sets up the handler for MSIX 0, which is used to manage the
  7045. * non-queue interrupts, e.g. AdminQ and errors. This is not used
  7046. * when in MSI or Legacy interrupt mode.
  7047. **/
  7048. static int i40e_setup_misc_vector(struct i40e_pf *pf)
  7049. {
  7050. struct i40e_hw *hw = &pf->hw;
  7051. int err = 0;
  7052. /* Only request the irq if this is the first time through, and
  7053. * not when we're rebuilding after a Reset
  7054. */
  7055. if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
  7056. err = request_irq(pf->msix_entries[0].vector,
  7057. i40e_intr, 0, pf->int_name, pf);
  7058. if (err) {
  7059. dev_info(&pf->pdev->dev,
  7060. "request_irq for %s failed: %d\n",
  7061. pf->int_name, err);
  7062. return -EFAULT;
  7063. }
  7064. }
  7065. i40e_enable_misc_int_causes(pf);
  7066. /* associate no queues to the misc vector */
  7067. wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
  7068. wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K);
  7069. i40e_flush(hw);
  7070. i40e_irq_dynamic_enable_icr0(pf, true);
  7071. return err;
  7072. }
  7073. /**
  7074. * i40e_config_rss_aq - Prepare for RSS using AQ commands
  7075. * @vsi: vsi structure
  7076. * @seed: RSS hash seed
  7077. **/
  7078. static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
  7079. u8 *lut, u16 lut_size)
  7080. {
  7081. struct i40e_pf *pf = vsi->back;
  7082. struct i40e_hw *hw = &pf->hw;
  7083. int ret = 0;
  7084. if (seed) {
  7085. struct i40e_aqc_get_set_rss_key_data *seed_dw =
  7086. (struct i40e_aqc_get_set_rss_key_data *)seed;
  7087. ret = i40e_aq_set_rss_key(hw, vsi->id, seed_dw);
  7088. if (ret) {
  7089. dev_info(&pf->pdev->dev,
  7090. "Cannot set RSS key, err %s aq_err %s\n",
  7091. i40e_stat_str(hw, ret),
  7092. i40e_aq_str(hw, hw->aq.asq_last_status));
  7093. return ret;
  7094. }
  7095. }
  7096. if (lut) {
  7097. bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
  7098. ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
  7099. if (ret) {
  7100. dev_info(&pf->pdev->dev,
  7101. "Cannot set RSS lut, err %s aq_err %s\n",
  7102. i40e_stat_str(hw, ret),
  7103. i40e_aq_str(hw, hw->aq.asq_last_status));
  7104. return ret;
  7105. }
  7106. }
  7107. return ret;
  7108. }
  7109. /**
  7110. * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
  7111. * @vsi: Pointer to vsi structure
  7112. * @seed: Buffter to store the hash keys
  7113. * @lut: Buffer to store the lookup table entries
  7114. * @lut_size: Size of buffer to store the lookup table entries
  7115. *
  7116. * Return 0 on success, negative on failure
  7117. */
  7118. static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
  7119. u8 *lut, u16 lut_size)
  7120. {
  7121. struct i40e_pf *pf = vsi->back;
  7122. struct i40e_hw *hw = &pf->hw;
  7123. int ret = 0;
  7124. if (seed) {
  7125. ret = i40e_aq_get_rss_key(hw, vsi->id,
  7126. (struct i40e_aqc_get_set_rss_key_data *)seed);
  7127. if (ret) {
  7128. dev_info(&pf->pdev->dev,
  7129. "Cannot get RSS key, err %s aq_err %s\n",
  7130. i40e_stat_str(&pf->hw, ret),
  7131. i40e_aq_str(&pf->hw,
  7132. pf->hw.aq.asq_last_status));
  7133. return ret;
  7134. }
  7135. }
  7136. if (lut) {
  7137. bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
  7138. ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
  7139. if (ret) {
  7140. dev_info(&pf->pdev->dev,
  7141. "Cannot get RSS lut, err %s aq_err %s\n",
  7142. i40e_stat_str(&pf->hw, ret),
  7143. i40e_aq_str(&pf->hw,
  7144. pf->hw.aq.asq_last_status));
  7145. return ret;
  7146. }
  7147. }
  7148. return ret;
  7149. }
  7150. /**
  7151. * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
  7152. * @vsi: VSI structure
  7153. **/
  7154. static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
  7155. {
  7156. u8 seed[I40E_HKEY_ARRAY_SIZE];
  7157. struct i40e_pf *pf = vsi->back;
  7158. u8 *lut;
  7159. int ret;
  7160. if (!(pf->flags & I40E_FLAG_RSS_AQ_CAPABLE))
  7161. return 0;
  7162. if (!vsi->rss_size)
  7163. vsi->rss_size = min_t(int, pf->alloc_rss_size,
  7164. vsi->num_queue_pairs);
  7165. if (!vsi->rss_size)
  7166. return -EINVAL;
  7167. lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
  7168. if (!lut)
  7169. return -ENOMEM;
  7170. /* Use the user configured hash keys and lookup table if there is one,
  7171. * otherwise use default
  7172. */
  7173. if (vsi->rss_lut_user)
  7174. memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
  7175. else
  7176. i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
  7177. if (vsi->rss_hkey_user)
  7178. memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
  7179. else
  7180. netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
  7181. ret = i40e_config_rss_aq(vsi, seed, lut, vsi->rss_table_size);
  7182. kfree(lut);
  7183. return ret;
  7184. }
  7185. /**
  7186. * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
  7187. * @vsi: Pointer to vsi structure
  7188. * @seed: RSS hash seed
  7189. * @lut: Lookup table
  7190. * @lut_size: Lookup table size
  7191. *
  7192. * Returns 0 on success, negative on failure
  7193. **/
  7194. static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
  7195. const u8 *lut, u16 lut_size)
  7196. {
  7197. struct i40e_pf *pf = vsi->back;
  7198. struct i40e_hw *hw = &pf->hw;
  7199. u16 vf_id = vsi->vf_id;
  7200. u8 i;
  7201. /* Fill out hash function seed */
  7202. if (seed) {
  7203. u32 *seed_dw = (u32 *)seed;
  7204. if (vsi->type == I40E_VSI_MAIN) {
  7205. for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
  7206. wr32(hw, I40E_PFQF_HKEY(i), seed_dw[i]);
  7207. } else if (vsi->type == I40E_VSI_SRIOV) {
  7208. for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
  7209. wr32(hw, I40E_VFQF_HKEY1(i, vf_id), seed_dw[i]);
  7210. } else {
  7211. dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
  7212. }
  7213. }
  7214. if (lut) {
  7215. u32 *lut_dw = (u32 *)lut;
  7216. if (vsi->type == I40E_VSI_MAIN) {
  7217. if (lut_size != I40E_HLUT_ARRAY_SIZE)
  7218. return -EINVAL;
  7219. for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
  7220. wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
  7221. } else if (vsi->type == I40E_VSI_SRIOV) {
  7222. if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
  7223. return -EINVAL;
  7224. for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
  7225. wr32(hw, I40E_VFQF_HLUT1(i, vf_id), lut_dw[i]);
  7226. } else {
  7227. dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
  7228. }
  7229. }
  7230. i40e_flush(hw);
  7231. return 0;
  7232. }
  7233. /**
  7234. * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
  7235. * @vsi: Pointer to VSI structure
  7236. * @seed: Buffer to store the keys
  7237. * @lut: Buffer to store the lookup table entries
  7238. * @lut_size: Size of buffer to store the lookup table entries
  7239. *
  7240. * Returns 0 on success, negative on failure
  7241. */
  7242. static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
  7243. u8 *lut, u16 lut_size)
  7244. {
  7245. struct i40e_pf *pf = vsi->back;
  7246. struct i40e_hw *hw = &pf->hw;
  7247. u16 i;
  7248. if (seed) {
  7249. u32 *seed_dw = (u32 *)seed;
  7250. for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
  7251. seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
  7252. }
  7253. if (lut) {
  7254. u32 *lut_dw = (u32 *)lut;
  7255. if (lut_size != I40E_HLUT_ARRAY_SIZE)
  7256. return -EINVAL;
  7257. for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
  7258. lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
  7259. }
  7260. return 0;
  7261. }
  7262. /**
  7263. * i40e_config_rss - Configure RSS keys and lut
  7264. * @vsi: Pointer to VSI structure
  7265. * @seed: RSS hash seed
  7266. * @lut: Lookup table
  7267. * @lut_size: Lookup table size
  7268. *
  7269. * Returns 0 on success, negative on failure
  7270. */
  7271. int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
  7272. {
  7273. struct i40e_pf *pf = vsi->back;
  7274. if (pf->flags & I40E_FLAG_RSS_AQ_CAPABLE)
  7275. return i40e_config_rss_aq(vsi, seed, lut, lut_size);
  7276. else
  7277. return i40e_config_rss_reg(vsi, seed, lut, lut_size);
  7278. }
  7279. /**
  7280. * i40e_get_rss - Get RSS keys and lut
  7281. * @vsi: Pointer to VSI structure
  7282. * @seed: Buffer to store the keys
  7283. * @lut: Buffer to store the lookup table entries
  7284. * lut_size: Size of buffer to store the lookup table entries
  7285. *
  7286. * Returns 0 on success, negative on failure
  7287. */
  7288. int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
  7289. {
  7290. struct i40e_pf *pf = vsi->back;
  7291. if (pf->flags & I40E_FLAG_RSS_AQ_CAPABLE)
  7292. return i40e_get_rss_aq(vsi, seed, lut, lut_size);
  7293. else
  7294. return i40e_get_rss_reg(vsi, seed, lut, lut_size);
  7295. }
  7296. /**
  7297. * i40e_fill_rss_lut - Fill the RSS lookup table with default values
  7298. * @pf: Pointer to board private structure
  7299. * @lut: Lookup table
  7300. * @rss_table_size: Lookup table size
  7301. * @rss_size: Range of queue number for hashing
  7302. */
  7303. void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
  7304. u16 rss_table_size, u16 rss_size)
  7305. {
  7306. u16 i;
  7307. for (i = 0; i < rss_table_size; i++)
  7308. lut[i] = i % rss_size;
  7309. }
  7310. /**
  7311. * i40e_pf_config_rss - Prepare for RSS if used
  7312. * @pf: board private structure
  7313. **/
  7314. static int i40e_pf_config_rss(struct i40e_pf *pf)
  7315. {
  7316. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  7317. u8 seed[I40E_HKEY_ARRAY_SIZE];
  7318. u8 *lut;
  7319. struct i40e_hw *hw = &pf->hw;
  7320. u32 reg_val;
  7321. u64 hena;
  7322. int ret;
  7323. /* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
  7324. hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
  7325. ((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
  7326. hena |= i40e_pf_get_default_rss_hena(pf);
  7327. i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
  7328. i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
  7329. /* Determine the RSS table size based on the hardware capabilities */
  7330. reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
  7331. reg_val = (pf->rss_table_size == 512) ?
  7332. (reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
  7333. (reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
  7334. i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
  7335. /* Determine the RSS size of the VSI */
  7336. if (!vsi->rss_size) {
  7337. u16 qcount;
  7338. qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
  7339. vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
  7340. }
  7341. if (!vsi->rss_size)
  7342. return -EINVAL;
  7343. lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
  7344. if (!lut)
  7345. return -ENOMEM;
  7346. /* Use user configured lut if there is one, otherwise use default */
  7347. if (vsi->rss_lut_user)
  7348. memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
  7349. else
  7350. i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
  7351. /* Use user configured hash key if there is one, otherwise
  7352. * use default.
  7353. */
  7354. if (vsi->rss_hkey_user)
  7355. memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
  7356. else
  7357. netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
  7358. ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
  7359. kfree(lut);
  7360. return ret;
  7361. }
  7362. /**
  7363. * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
  7364. * @pf: board private structure
  7365. * @queue_count: the requested queue count for rss.
  7366. *
  7367. * returns 0 if rss is not enabled, if enabled returns the final rss queue
  7368. * count which may be different from the requested queue count.
  7369. **/
  7370. int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
  7371. {
  7372. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  7373. int new_rss_size;
  7374. if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
  7375. return 0;
  7376. new_rss_size = min_t(int, queue_count, pf->rss_size_max);
  7377. if (queue_count != vsi->num_queue_pairs) {
  7378. u16 qcount;
  7379. vsi->req_queue_pairs = queue_count;
  7380. i40e_prep_for_reset(pf);
  7381. pf->alloc_rss_size = new_rss_size;
  7382. i40e_reset_and_rebuild(pf, true);
  7383. /* Discard the user configured hash keys and lut, if less
  7384. * queues are enabled.
  7385. */
  7386. if (queue_count < vsi->rss_size) {
  7387. i40e_clear_rss_config_user(vsi);
  7388. dev_dbg(&pf->pdev->dev,
  7389. "discard user configured hash keys and lut\n");
  7390. }
  7391. /* Reset vsi->rss_size, as number of enabled queues changed */
  7392. qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
  7393. vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
  7394. i40e_pf_config_rss(pf);
  7395. }
  7396. dev_info(&pf->pdev->dev, "User requested queue count/HW max RSS count: %d/%d\n",
  7397. vsi->req_queue_pairs, pf->rss_size_max);
  7398. return pf->alloc_rss_size;
  7399. }
  7400. /**
  7401. * i40e_get_npar_bw_setting - Retrieve BW settings for this PF partition
  7402. * @pf: board private structure
  7403. **/
  7404. i40e_status i40e_get_npar_bw_setting(struct i40e_pf *pf)
  7405. {
  7406. i40e_status status;
  7407. bool min_valid, max_valid;
  7408. u32 max_bw, min_bw;
  7409. status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
  7410. &min_valid, &max_valid);
  7411. if (!status) {
  7412. if (min_valid)
  7413. pf->npar_min_bw = min_bw;
  7414. if (max_valid)
  7415. pf->npar_max_bw = max_bw;
  7416. }
  7417. return status;
  7418. }
  7419. /**
  7420. * i40e_set_npar_bw_setting - Set BW settings for this PF partition
  7421. * @pf: board private structure
  7422. **/
  7423. i40e_status i40e_set_npar_bw_setting(struct i40e_pf *pf)
  7424. {
  7425. struct i40e_aqc_configure_partition_bw_data bw_data;
  7426. i40e_status status;
  7427. /* Set the valid bit for this PF */
  7428. bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
  7429. bw_data.max_bw[pf->hw.pf_id] = pf->npar_max_bw & I40E_ALT_BW_VALUE_MASK;
  7430. bw_data.min_bw[pf->hw.pf_id] = pf->npar_min_bw & I40E_ALT_BW_VALUE_MASK;
  7431. /* Set the new bandwidths */
  7432. status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
  7433. return status;
  7434. }
  7435. /**
  7436. * i40e_commit_npar_bw_setting - Commit BW settings for this PF partition
  7437. * @pf: board private structure
  7438. **/
  7439. i40e_status i40e_commit_npar_bw_setting(struct i40e_pf *pf)
  7440. {
  7441. /* Commit temporary BW setting to permanent NVM image */
  7442. enum i40e_admin_queue_err last_aq_status;
  7443. i40e_status ret;
  7444. u16 nvm_word;
  7445. if (pf->hw.partition_id != 1) {
  7446. dev_info(&pf->pdev->dev,
  7447. "Commit BW only works on partition 1! This is partition %d",
  7448. pf->hw.partition_id);
  7449. ret = I40E_NOT_SUPPORTED;
  7450. goto bw_commit_out;
  7451. }
  7452. /* Acquire NVM for read access */
  7453. ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
  7454. last_aq_status = pf->hw.aq.asq_last_status;
  7455. if (ret) {
  7456. dev_info(&pf->pdev->dev,
  7457. "Cannot acquire NVM for read access, err %s aq_err %s\n",
  7458. i40e_stat_str(&pf->hw, ret),
  7459. i40e_aq_str(&pf->hw, last_aq_status));
  7460. goto bw_commit_out;
  7461. }
  7462. /* Read word 0x10 of NVM - SW compatibility word 1 */
  7463. ret = i40e_aq_read_nvm(&pf->hw,
  7464. I40E_SR_NVM_CONTROL_WORD,
  7465. 0x10, sizeof(nvm_word), &nvm_word,
  7466. false, NULL);
  7467. /* Save off last admin queue command status before releasing
  7468. * the NVM
  7469. */
  7470. last_aq_status = pf->hw.aq.asq_last_status;
  7471. i40e_release_nvm(&pf->hw);
  7472. if (ret) {
  7473. dev_info(&pf->pdev->dev, "NVM read error, err %s aq_err %s\n",
  7474. i40e_stat_str(&pf->hw, ret),
  7475. i40e_aq_str(&pf->hw, last_aq_status));
  7476. goto bw_commit_out;
  7477. }
  7478. /* Wait a bit for NVM release to complete */
  7479. msleep(50);
  7480. /* Acquire NVM for write access */
  7481. ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
  7482. last_aq_status = pf->hw.aq.asq_last_status;
  7483. if (ret) {
  7484. dev_info(&pf->pdev->dev,
  7485. "Cannot acquire NVM for write access, err %s aq_err %s\n",
  7486. i40e_stat_str(&pf->hw, ret),
  7487. i40e_aq_str(&pf->hw, last_aq_status));
  7488. goto bw_commit_out;
  7489. }
  7490. /* Write it back out unchanged to initiate update NVM,
  7491. * which will force a write of the shadow (alt) RAM to
  7492. * the NVM - thus storing the bandwidth values permanently.
  7493. */
  7494. ret = i40e_aq_update_nvm(&pf->hw,
  7495. I40E_SR_NVM_CONTROL_WORD,
  7496. 0x10, sizeof(nvm_word),
  7497. &nvm_word, true, NULL);
  7498. /* Save off last admin queue command status before releasing
  7499. * the NVM
  7500. */
  7501. last_aq_status = pf->hw.aq.asq_last_status;
  7502. i40e_release_nvm(&pf->hw);
  7503. if (ret)
  7504. dev_info(&pf->pdev->dev,
  7505. "BW settings NOT SAVED, err %s aq_err %s\n",
  7506. i40e_stat_str(&pf->hw, ret),
  7507. i40e_aq_str(&pf->hw, last_aq_status));
  7508. bw_commit_out:
  7509. return ret;
  7510. }
  7511. /**
  7512. * i40e_sw_init - Initialize general software structures (struct i40e_pf)
  7513. * @pf: board private structure to initialize
  7514. *
  7515. * i40e_sw_init initializes the Adapter private data structure.
  7516. * Fields are initialized based on PCI device information and
  7517. * OS network device settings (MTU size).
  7518. **/
  7519. static int i40e_sw_init(struct i40e_pf *pf)
  7520. {
  7521. int err = 0;
  7522. int size;
  7523. /* Set default capability flags */
  7524. pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
  7525. I40E_FLAG_MSI_ENABLED |
  7526. I40E_FLAG_MSIX_ENABLED;
  7527. /* Set default ITR */
  7528. pf->rx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_RX_DEF;
  7529. pf->tx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_TX_DEF;
  7530. /* Depending on PF configurations, it is possible that the RSS
  7531. * maximum might end up larger than the available queues
  7532. */
  7533. pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
  7534. pf->alloc_rss_size = 1;
  7535. pf->rss_table_size = pf->hw.func_caps.rss_table_size;
  7536. pf->rss_size_max = min_t(int, pf->rss_size_max,
  7537. pf->hw.func_caps.num_tx_qp);
  7538. if (pf->hw.func_caps.rss) {
  7539. pf->flags |= I40E_FLAG_RSS_ENABLED;
  7540. pf->alloc_rss_size = min_t(int, pf->rss_size_max,
  7541. num_online_cpus());
  7542. }
  7543. /* MFP mode enabled */
  7544. if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
  7545. pf->flags |= I40E_FLAG_MFP_ENABLED;
  7546. dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
  7547. if (i40e_get_npar_bw_setting(pf))
  7548. dev_warn(&pf->pdev->dev,
  7549. "Could not get NPAR bw settings\n");
  7550. else
  7551. dev_info(&pf->pdev->dev,
  7552. "Min BW = %8.8x, Max BW = %8.8x\n",
  7553. pf->npar_min_bw, pf->npar_max_bw);
  7554. }
  7555. /* FW/NVM is not yet fixed in this regard */
  7556. if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
  7557. (pf->hw.func_caps.fd_filters_best_effort > 0)) {
  7558. pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
  7559. pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
  7560. if (pf->flags & I40E_FLAG_MFP_ENABLED &&
  7561. pf->hw.num_partitions > 1)
  7562. dev_info(&pf->pdev->dev,
  7563. "Flow Director Sideband mode Disabled in MFP mode\n");
  7564. else
  7565. pf->flags |= I40E_FLAG_FD_SB_ENABLED;
  7566. pf->fdir_pf_filter_count =
  7567. pf->hw.func_caps.fd_filters_guaranteed;
  7568. pf->hw.fdir_shared_filter_count =
  7569. pf->hw.func_caps.fd_filters_best_effort;
  7570. }
  7571. if ((pf->hw.mac.type == I40E_MAC_XL710) &&
  7572. (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
  7573. (pf->hw.aq.fw_maj_ver < 4))) {
  7574. pf->flags |= I40E_FLAG_RESTART_AUTONEG;
  7575. /* No DCB support for FW < v4.33 */
  7576. pf->flags |= I40E_FLAG_NO_DCB_SUPPORT;
  7577. }
  7578. /* Disable FW LLDP if FW < v4.3 */
  7579. if ((pf->hw.mac.type == I40E_MAC_XL710) &&
  7580. (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
  7581. (pf->hw.aq.fw_maj_ver < 4)))
  7582. pf->flags |= I40E_FLAG_STOP_FW_LLDP;
  7583. /* Use the FW Set LLDP MIB API if FW > v4.40 */
  7584. if ((pf->hw.mac.type == I40E_MAC_XL710) &&
  7585. (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver >= 40)) ||
  7586. (pf->hw.aq.fw_maj_ver >= 5)))
  7587. pf->flags |= I40E_FLAG_USE_SET_LLDP_MIB;
  7588. if (pf->hw.func_caps.vmdq) {
  7589. pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
  7590. pf->flags |= I40E_FLAG_VMDQ_ENABLED;
  7591. pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
  7592. }
  7593. if (pf->hw.func_caps.iwarp) {
  7594. pf->flags |= I40E_FLAG_IWARP_ENABLED;
  7595. /* IWARP needs one extra vector for CQP just like MISC.*/
  7596. pf->num_iwarp_msix = (int)num_online_cpus() + 1;
  7597. }
  7598. #ifdef CONFIG_PCI_IOV
  7599. if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
  7600. pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
  7601. pf->flags |= I40E_FLAG_SRIOV_ENABLED;
  7602. pf->num_req_vfs = min_t(int,
  7603. pf->hw.func_caps.num_vfs,
  7604. I40E_MAX_VF_COUNT);
  7605. }
  7606. #endif /* CONFIG_PCI_IOV */
  7607. if (pf->hw.mac.type == I40E_MAC_X722) {
  7608. pf->flags |= I40E_FLAG_RSS_AQ_CAPABLE
  7609. | I40E_FLAG_128_QP_RSS_CAPABLE
  7610. | I40E_FLAG_HW_ATR_EVICT_CAPABLE
  7611. | I40E_FLAG_OUTER_UDP_CSUM_CAPABLE
  7612. | I40E_FLAG_WB_ON_ITR_CAPABLE
  7613. | I40E_FLAG_MULTIPLE_TCP_UDP_RSS_PCTYPE
  7614. | I40E_FLAG_NO_PCI_LINK_CHECK
  7615. | I40E_FLAG_USE_SET_LLDP_MIB
  7616. | I40E_FLAG_GENEVE_OFFLOAD_CAPABLE
  7617. | I40E_FLAG_PTP_L4_CAPABLE
  7618. | I40E_FLAG_WOL_MC_MAGIC_PKT_WAKE;
  7619. } else if ((pf->hw.aq.api_maj_ver > 1) ||
  7620. ((pf->hw.aq.api_maj_ver == 1) &&
  7621. (pf->hw.aq.api_min_ver > 4))) {
  7622. /* Supported in FW API version higher than 1.4 */
  7623. pf->flags |= I40E_FLAG_GENEVE_OFFLOAD_CAPABLE;
  7624. pf->hw_disabled_flags = I40E_FLAG_HW_ATR_EVICT_CAPABLE;
  7625. } else {
  7626. pf->hw_disabled_flags = I40E_FLAG_HW_ATR_EVICT_CAPABLE;
  7627. }
  7628. pf->eeprom_version = 0xDEAD;
  7629. pf->lan_veb = I40E_NO_VEB;
  7630. pf->lan_vsi = I40E_NO_VSI;
  7631. /* By default FW has this off for performance reasons */
  7632. pf->flags &= ~I40E_FLAG_VEB_STATS_ENABLED;
  7633. /* set up queue assignment tracking */
  7634. size = sizeof(struct i40e_lump_tracking)
  7635. + (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
  7636. pf->qp_pile = kzalloc(size, GFP_KERNEL);
  7637. if (!pf->qp_pile) {
  7638. err = -ENOMEM;
  7639. goto sw_init_done;
  7640. }
  7641. pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
  7642. pf->qp_pile->search_hint = 0;
  7643. pf->tx_timeout_recovery_level = 1;
  7644. mutex_init(&pf->switch_mutex);
  7645. /* If NPAR is enabled nudge the Tx scheduler */
  7646. if (pf->hw.func_caps.npar_enable && (!i40e_get_npar_bw_setting(pf)))
  7647. i40e_set_npar_bw_setting(pf);
  7648. sw_init_done:
  7649. return err;
  7650. }
  7651. /**
  7652. * i40e_set_ntuple - set the ntuple feature flag and take action
  7653. * @pf: board private structure to initialize
  7654. * @features: the feature set that the stack is suggesting
  7655. *
  7656. * returns a bool to indicate if reset needs to happen
  7657. **/
  7658. bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
  7659. {
  7660. bool need_reset = false;
  7661. /* Check if Flow Director n-tuple support was enabled or disabled. If
  7662. * the state changed, we need to reset.
  7663. */
  7664. if (features & NETIF_F_NTUPLE) {
  7665. /* Enable filters and mark for reset */
  7666. if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
  7667. need_reset = true;
  7668. /* enable FD_SB only if there is MSI-X vector */
  7669. if (pf->num_fdsb_msix > 0)
  7670. pf->flags |= I40E_FLAG_FD_SB_ENABLED;
  7671. } else {
  7672. /* turn off filters, mark for reset and clear SW filter list */
  7673. if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
  7674. need_reset = true;
  7675. i40e_fdir_filter_exit(pf);
  7676. }
  7677. pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
  7678. pf->hw_disabled_flags &= ~I40E_FLAG_FD_SB_ENABLED;
  7679. /* reset fd counters */
  7680. pf->fd_add_err = 0;
  7681. pf->fd_atr_cnt = 0;
  7682. /* if ATR was auto disabled it can be re-enabled. */
  7683. if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
  7684. (pf->hw_disabled_flags & I40E_FLAG_FD_ATR_ENABLED)) {
  7685. pf->hw_disabled_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
  7686. if (I40E_DEBUG_FD & pf->hw.debug_mask)
  7687. dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
  7688. }
  7689. }
  7690. return need_reset;
  7691. }
  7692. /**
  7693. * i40e_clear_rss_lut - clear the rx hash lookup table
  7694. * @vsi: the VSI being configured
  7695. **/
  7696. static void i40e_clear_rss_lut(struct i40e_vsi *vsi)
  7697. {
  7698. struct i40e_pf *pf = vsi->back;
  7699. struct i40e_hw *hw = &pf->hw;
  7700. u16 vf_id = vsi->vf_id;
  7701. u8 i;
  7702. if (vsi->type == I40E_VSI_MAIN) {
  7703. for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
  7704. wr32(hw, I40E_PFQF_HLUT(i), 0);
  7705. } else if (vsi->type == I40E_VSI_SRIOV) {
  7706. for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
  7707. i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0);
  7708. } else {
  7709. dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
  7710. }
  7711. }
  7712. /**
  7713. * i40e_set_features - set the netdev feature flags
  7714. * @netdev: ptr to the netdev being adjusted
  7715. * @features: the feature set that the stack is suggesting
  7716. **/
  7717. static int i40e_set_features(struct net_device *netdev,
  7718. netdev_features_t features)
  7719. {
  7720. struct i40e_netdev_priv *np = netdev_priv(netdev);
  7721. struct i40e_vsi *vsi = np->vsi;
  7722. struct i40e_pf *pf = vsi->back;
  7723. bool need_reset;
  7724. if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
  7725. i40e_pf_config_rss(pf);
  7726. else if (!(features & NETIF_F_RXHASH) &&
  7727. netdev->features & NETIF_F_RXHASH)
  7728. i40e_clear_rss_lut(vsi);
  7729. if (features & NETIF_F_HW_VLAN_CTAG_RX)
  7730. i40e_vlan_stripping_enable(vsi);
  7731. else
  7732. i40e_vlan_stripping_disable(vsi);
  7733. need_reset = i40e_set_ntuple(pf, features);
  7734. if (need_reset)
  7735. i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
  7736. return 0;
  7737. }
  7738. /**
  7739. * i40e_get_udp_port_idx - Lookup a possibly offloaded for Rx UDP port
  7740. * @pf: board private structure
  7741. * @port: The UDP port to look up
  7742. *
  7743. * Returns the index number or I40E_MAX_PF_UDP_OFFLOAD_PORTS if port not found
  7744. **/
  7745. static u8 i40e_get_udp_port_idx(struct i40e_pf *pf, u16 port)
  7746. {
  7747. u8 i;
  7748. for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
  7749. if (pf->udp_ports[i].index == port)
  7750. return i;
  7751. }
  7752. return i;
  7753. }
  7754. /**
  7755. * i40e_udp_tunnel_add - Get notifications about UDP tunnel ports that come up
  7756. * @netdev: This physical port's netdev
  7757. * @ti: Tunnel endpoint information
  7758. **/
  7759. static void i40e_udp_tunnel_add(struct net_device *netdev,
  7760. struct udp_tunnel_info *ti)
  7761. {
  7762. struct i40e_netdev_priv *np = netdev_priv(netdev);
  7763. struct i40e_vsi *vsi = np->vsi;
  7764. struct i40e_pf *pf = vsi->back;
  7765. u16 port = ntohs(ti->port);
  7766. u8 next_idx;
  7767. u8 idx;
  7768. idx = i40e_get_udp_port_idx(pf, port);
  7769. /* Check if port already exists */
  7770. if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
  7771. netdev_info(netdev, "port %d already offloaded\n", port);
  7772. return;
  7773. }
  7774. /* Now check if there is space to add the new port */
  7775. next_idx = i40e_get_udp_port_idx(pf, 0);
  7776. if (next_idx == I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
  7777. netdev_info(netdev, "maximum number of offloaded UDP ports reached, not adding port %d\n",
  7778. port);
  7779. return;
  7780. }
  7781. switch (ti->type) {
  7782. case UDP_TUNNEL_TYPE_VXLAN:
  7783. pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_VXLAN;
  7784. break;
  7785. case UDP_TUNNEL_TYPE_GENEVE:
  7786. if (!(pf->flags & I40E_FLAG_GENEVE_OFFLOAD_CAPABLE))
  7787. return;
  7788. pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_NGE;
  7789. break;
  7790. default:
  7791. return;
  7792. }
  7793. /* New port: add it and mark its index in the bitmap */
  7794. pf->udp_ports[next_idx].index = port;
  7795. pf->pending_udp_bitmap |= BIT_ULL(next_idx);
  7796. pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
  7797. }
  7798. /**
  7799. * i40e_udp_tunnel_del - Get notifications about UDP tunnel ports that go away
  7800. * @netdev: This physical port's netdev
  7801. * @ti: Tunnel endpoint information
  7802. **/
  7803. static void i40e_udp_tunnel_del(struct net_device *netdev,
  7804. struct udp_tunnel_info *ti)
  7805. {
  7806. struct i40e_netdev_priv *np = netdev_priv(netdev);
  7807. struct i40e_vsi *vsi = np->vsi;
  7808. struct i40e_pf *pf = vsi->back;
  7809. u16 port = ntohs(ti->port);
  7810. u8 idx;
  7811. idx = i40e_get_udp_port_idx(pf, port);
  7812. /* Check if port already exists */
  7813. if (idx >= I40E_MAX_PF_UDP_OFFLOAD_PORTS)
  7814. goto not_found;
  7815. switch (ti->type) {
  7816. case UDP_TUNNEL_TYPE_VXLAN:
  7817. if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_VXLAN)
  7818. goto not_found;
  7819. break;
  7820. case UDP_TUNNEL_TYPE_GENEVE:
  7821. if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_NGE)
  7822. goto not_found;
  7823. break;
  7824. default:
  7825. goto not_found;
  7826. }
  7827. /* if port exists, set it to 0 (mark for deletion)
  7828. * and make it pending
  7829. */
  7830. pf->udp_ports[idx].index = 0;
  7831. pf->pending_udp_bitmap |= BIT_ULL(idx);
  7832. pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
  7833. return;
  7834. not_found:
  7835. netdev_warn(netdev, "UDP port %d was not found, not deleting\n",
  7836. port);
  7837. }
  7838. static int i40e_get_phys_port_id(struct net_device *netdev,
  7839. struct netdev_phys_item_id *ppid)
  7840. {
  7841. struct i40e_netdev_priv *np = netdev_priv(netdev);
  7842. struct i40e_pf *pf = np->vsi->back;
  7843. struct i40e_hw *hw = &pf->hw;
  7844. if (!(pf->flags & I40E_FLAG_PORT_ID_VALID))
  7845. return -EOPNOTSUPP;
  7846. ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
  7847. memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
  7848. return 0;
  7849. }
  7850. /**
  7851. * i40e_ndo_fdb_add - add an entry to the hardware database
  7852. * @ndm: the input from the stack
  7853. * @tb: pointer to array of nladdr (unused)
  7854. * @dev: the net device pointer
  7855. * @addr: the MAC address entry being added
  7856. * @flags: instructions from stack about fdb operation
  7857. */
  7858. static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
  7859. struct net_device *dev,
  7860. const unsigned char *addr, u16 vid,
  7861. u16 flags)
  7862. {
  7863. struct i40e_netdev_priv *np = netdev_priv(dev);
  7864. struct i40e_pf *pf = np->vsi->back;
  7865. int err = 0;
  7866. if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
  7867. return -EOPNOTSUPP;
  7868. if (vid) {
  7869. pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
  7870. return -EINVAL;
  7871. }
  7872. /* Hardware does not support aging addresses so if a
  7873. * ndm_state is given only allow permanent addresses
  7874. */
  7875. if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
  7876. netdev_info(dev, "FDB only supports static addresses\n");
  7877. return -EINVAL;
  7878. }
  7879. if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
  7880. err = dev_uc_add_excl(dev, addr);
  7881. else if (is_multicast_ether_addr(addr))
  7882. err = dev_mc_add_excl(dev, addr);
  7883. else
  7884. err = -EINVAL;
  7885. /* Only return duplicate errors if NLM_F_EXCL is set */
  7886. if (err == -EEXIST && !(flags & NLM_F_EXCL))
  7887. err = 0;
  7888. return err;
  7889. }
  7890. /**
  7891. * i40e_ndo_bridge_setlink - Set the hardware bridge mode
  7892. * @dev: the netdev being configured
  7893. * @nlh: RTNL message
  7894. *
  7895. * Inserts a new hardware bridge if not already created and
  7896. * enables the bridging mode requested (VEB or VEPA). If the
  7897. * hardware bridge has already been inserted and the request
  7898. * is to change the mode then that requires a PF reset to
  7899. * allow rebuild of the components with required hardware
  7900. * bridge mode enabled.
  7901. **/
  7902. static int i40e_ndo_bridge_setlink(struct net_device *dev,
  7903. struct nlmsghdr *nlh,
  7904. u16 flags)
  7905. {
  7906. struct i40e_netdev_priv *np = netdev_priv(dev);
  7907. struct i40e_vsi *vsi = np->vsi;
  7908. struct i40e_pf *pf = vsi->back;
  7909. struct i40e_veb *veb = NULL;
  7910. struct nlattr *attr, *br_spec;
  7911. int i, rem;
  7912. /* Only for PF VSI for now */
  7913. if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
  7914. return -EOPNOTSUPP;
  7915. /* Find the HW bridge for PF VSI */
  7916. for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
  7917. if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
  7918. veb = pf->veb[i];
  7919. }
  7920. br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
  7921. nla_for_each_nested(attr, br_spec, rem) {
  7922. __u16 mode;
  7923. if (nla_type(attr) != IFLA_BRIDGE_MODE)
  7924. continue;
  7925. mode = nla_get_u16(attr);
  7926. if ((mode != BRIDGE_MODE_VEPA) &&
  7927. (mode != BRIDGE_MODE_VEB))
  7928. return -EINVAL;
  7929. /* Insert a new HW bridge */
  7930. if (!veb) {
  7931. veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
  7932. vsi->tc_config.enabled_tc);
  7933. if (veb) {
  7934. veb->bridge_mode = mode;
  7935. i40e_config_bridge_mode(veb);
  7936. } else {
  7937. /* No Bridge HW offload available */
  7938. return -ENOENT;
  7939. }
  7940. break;
  7941. } else if (mode != veb->bridge_mode) {
  7942. /* Existing HW bridge but different mode needs reset */
  7943. veb->bridge_mode = mode;
  7944. /* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
  7945. if (mode == BRIDGE_MODE_VEB)
  7946. pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
  7947. else
  7948. pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
  7949. i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
  7950. break;
  7951. }
  7952. }
  7953. return 0;
  7954. }
  7955. /**
  7956. * i40e_ndo_bridge_getlink - Get the hardware bridge mode
  7957. * @skb: skb buff
  7958. * @pid: process id
  7959. * @seq: RTNL message seq #
  7960. * @dev: the netdev being configured
  7961. * @filter_mask: unused
  7962. * @nlflags: netlink flags passed in
  7963. *
  7964. * Return the mode in which the hardware bridge is operating in
  7965. * i.e VEB or VEPA.
  7966. **/
  7967. static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
  7968. struct net_device *dev,
  7969. u32 __always_unused filter_mask,
  7970. int nlflags)
  7971. {
  7972. struct i40e_netdev_priv *np = netdev_priv(dev);
  7973. struct i40e_vsi *vsi = np->vsi;
  7974. struct i40e_pf *pf = vsi->back;
  7975. struct i40e_veb *veb = NULL;
  7976. int i;
  7977. /* Only for PF VSI for now */
  7978. if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
  7979. return -EOPNOTSUPP;
  7980. /* Find the HW bridge for the PF VSI */
  7981. for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
  7982. if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
  7983. veb = pf->veb[i];
  7984. }
  7985. if (!veb)
  7986. return 0;
  7987. return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
  7988. 0, 0, nlflags, filter_mask, NULL);
  7989. }
  7990. /**
  7991. * i40e_features_check - Validate encapsulated packet conforms to limits
  7992. * @skb: skb buff
  7993. * @dev: This physical port's netdev
  7994. * @features: Offload features that the stack believes apply
  7995. **/
  7996. static netdev_features_t i40e_features_check(struct sk_buff *skb,
  7997. struct net_device *dev,
  7998. netdev_features_t features)
  7999. {
  8000. size_t len;
  8001. /* No point in doing any of this if neither checksum nor GSO are
  8002. * being requested for this frame. We can rule out both by just
  8003. * checking for CHECKSUM_PARTIAL
  8004. */
  8005. if (skb->ip_summed != CHECKSUM_PARTIAL)
  8006. return features;
  8007. /* We cannot support GSO if the MSS is going to be less than
  8008. * 64 bytes. If it is then we need to drop support for GSO.
  8009. */
  8010. if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
  8011. features &= ~NETIF_F_GSO_MASK;
  8012. /* MACLEN can support at most 63 words */
  8013. len = skb_network_header(skb) - skb->data;
  8014. if (len & ~(63 * 2))
  8015. goto out_err;
  8016. /* IPLEN and EIPLEN can support at most 127 dwords */
  8017. len = skb_transport_header(skb) - skb_network_header(skb);
  8018. if (len & ~(127 * 4))
  8019. goto out_err;
  8020. if (skb->encapsulation) {
  8021. /* L4TUNLEN can support 127 words */
  8022. len = skb_inner_network_header(skb) - skb_transport_header(skb);
  8023. if (len & ~(127 * 2))
  8024. goto out_err;
  8025. /* IPLEN can support at most 127 dwords */
  8026. len = skb_inner_transport_header(skb) -
  8027. skb_inner_network_header(skb);
  8028. if (len & ~(127 * 4))
  8029. goto out_err;
  8030. }
  8031. /* No need to validate L4LEN as TCP is the only protocol with a
  8032. * a flexible value and we support all possible values supported
  8033. * by TCP, which is at most 15 dwords
  8034. */
  8035. return features;
  8036. out_err:
  8037. return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
  8038. }
  8039. static const struct net_device_ops i40e_netdev_ops = {
  8040. .ndo_open = i40e_open,
  8041. .ndo_stop = i40e_close,
  8042. .ndo_start_xmit = i40e_lan_xmit_frame,
  8043. .ndo_get_stats64 = i40e_get_netdev_stats_struct,
  8044. .ndo_set_rx_mode = i40e_set_rx_mode,
  8045. .ndo_validate_addr = eth_validate_addr,
  8046. .ndo_set_mac_address = i40e_set_mac,
  8047. .ndo_change_mtu = i40e_change_mtu,
  8048. .ndo_do_ioctl = i40e_ioctl,
  8049. .ndo_tx_timeout = i40e_tx_timeout,
  8050. .ndo_vlan_rx_add_vid = i40e_vlan_rx_add_vid,
  8051. .ndo_vlan_rx_kill_vid = i40e_vlan_rx_kill_vid,
  8052. #ifdef CONFIG_NET_POLL_CONTROLLER
  8053. .ndo_poll_controller = i40e_netpoll,
  8054. #endif
  8055. .ndo_setup_tc = __i40e_setup_tc,
  8056. .ndo_set_features = i40e_set_features,
  8057. .ndo_set_vf_mac = i40e_ndo_set_vf_mac,
  8058. .ndo_set_vf_vlan = i40e_ndo_set_vf_port_vlan,
  8059. .ndo_set_vf_rate = i40e_ndo_set_vf_bw,
  8060. .ndo_get_vf_config = i40e_ndo_get_vf_config,
  8061. .ndo_set_vf_link_state = i40e_ndo_set_vf_link_state,
  8062. .ndo_set_vf_spoofchk = i40e_ndo_set_vf_spoofchk,
  8063. .ndo_set_vf_trust = i40e_ndo_set_vf_trust,
  8064. .ndo_udp_tunnel_add = i40e_udp_tunnel_add,
  8065. .ndo_udp_tunnel_del = i40e_udp_tunnel_del,
  8066. .ndo_get_phys_port_id = i40e_get_phys_port_id,
  8067. .ndo_fdb_add = i40e_ndo_fdb_add,
  8068. .ndo_features_check = i40e_features_check,
  8069. .ndo_bridge_getlink = i40e_ndo_bridge_getlink,
  8070. .ndo_bridge_setlink = i40e_ndo_bridge_setlink,
  8071. };
  8072. /**
  8073. * i40e_config_netdev - Setup the netdev flags
  8074. * @vsi: the VSI being configured
  8075. *
  8076. * Returns 0 on success, negative value on failure
  8077. **/
  8078. static int i40e_config_netdev(struct i40e_vsi *vsi)
  8079. {
  8080. struct i40e_pf *pf = vsi->back;
  8081. struct i40e_hw *hw = &pf->hw;
  8082. struct i40e_netdev_priv *np;
  8083. struct net_device *netdev;
  8084. u8 broadcast[ETH_ALEN];
  8085. u8 mac_addr[ETH_ALEN];
  8086. int etherdev_size;
  8087. etherdev_size = sizeof(struct i40e_netdev_priv);
  8088. netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
  8089. if (!netdev)
  8090. return -ENOMEM;
  8091. vsi->netdev = netdev;
  8092. np = netdev_priv(netdev);
  8093. np->vsi = vsi;
  8094. netdev->hw_enc_features |= NETIF_F_SG |
  8095. NETIF_F_IP_CSUM |
  8096. NETIF_F_IPV6_CSUM |
  8097. NETIF_F_HIGHDMA |
  8098. NETIF_F_SOFT_FEATURES |
  8099. NETIF_F_TSO |
  8100. NETIF_F_TSO_ECN |
  8101. NETIF_F_TSO6 |
  8102. NETIF_F_GSO_GRE |
  8103. NETIF_F_GSO_GRE_CSUM |
  8104. NETIF_F_GSO_IPXIP4 |
  8105. NETIF_F_GSO_IPXIP6 |
  8106. NETIF_F_GSO_UDP_TUNNEL |
  8107. NETIF_F_GSO_UDP_TUNNEL_CSUM |
  8108. NETIF_F_GSO_PARTIAL |
  8109. NETIF_F_SCTP_CRC |
  8110. NETIF_F_RXHASH |
  8111. NETIF_F_RXCSUM |
  8112. 0;
  8113. if (!(pf->flags & I40E_FLAG_OUTER_UDP_CSUM_CAPABLE))
  8114. netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
  8115. netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
  8116. /* record features VLANs can make use of */
  8117. netdev->vlan_features |= netdev->hw_enc_features |
  8118. NETIF_F_TSO_MANGLEID;
  8119. if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
  8120. netdev->hw_features |= NETIF_F_NTUPLE;
  8121. netdev->hw_features |= netdev->hw_enc_features |
  8122. NETIF_F_HW_VLAN_CTAG_TX |
  8123. NETIF_F_HW_VLAN_CTAG_RX;
  8124. netdev->features |= netdev->hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
  8125. netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
  8126. if (vsi->type == I40E_VSI_MAIN) {
  8127. SET_NETDEV_DEV(netdev, &pf->pdev->dev);
  8128. ether_addr_copy(mac_addr, hw->mac.perm_addr);
  8129. /* The following steps are necessary to properly keep track of
  8130. * MAC-VLAN filters loaded into firmware - first we remove
  8131. * filter that is automatically generated by firmware and then
  8132. * add new filter both to the driver hash table and firmware.
  8133. */
  8134. i40e_rm_default_mac_filter(vsi, mac_addr);
  8135. spin_lock_bh(&vsi->mac_filter_hash_lock);
  8136. i40e_add_mac_filter(vsi, mac_addr);
  8137. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  8138. } else {
  8139. /* relate the VSI_VMDQ name to the VSI_MAIN name */
  8140. snprintf(netdev->name, IFNAMSIZ, "%sv%%d",
  8141. pf->vsi[pf->lan_vsi]->netdev->name);
  8142. random_ether_addr(mac_addr);
  8143. spin_lock_bh(&vsi->mac_filter_hash_lock);
  8144. i40e_add_mac_filter(vsi, mac_addr);
  8145. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  8146. }
  8147. /* Add the broadcast filter so that we initially will receive
  8148. * broadcast packets. Note that when a new VLAN is first added the
  8149. * driver will convert all filters marked I40E_VLAN_ANY into VLAN
  8150. * specific filters as part of transitioning into "vlan" operation.
  8151. * When more VLANs are added, the driver will copy each existing MAC
  8152. * filter and add it for the new VLAN.
  8153. *
  8154. * Broadcast filters are handled specially by
  8155. * i40e_sync_filters_subtask, as the driver must to set the broadcast
  8156. * promiscuous bit instead of adding this directly as a MAC/VLAN
  8157. * filter. The subtask will update the correct broadcast promiscuous
  8158. * bits as VLANs become active or inactive.
  8159. */
  8160. eth_broadcast_addr(broadcast);
  8161. spin_lock_bh(&vsi->mac_filter_hash_lock);
  8162. i40e_add_mac_filter(vsi, broadcast);
  8163. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  8164. ether_addr_copy(netdev->dev_addr, mac_addr);
  8165. ether_addr_copy(netdev->perm_addr, mac_addr);
  8166. netdev->priv_flags |= IFF_UNICAST_FLT;
  8167. netdev->priv_flags |= IFF_SUPP_NOFCS;
  8168. /* Setup netdev TC information */
  8169. i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
  8170. netdev->netdev_ops = &i40e_netdev_ops;
  8171. netdev->watchdog_timeo = 5 * HZ;
  8172. i40e_set_ethtool_ops(netdev);
  8173. /* MTU range: 68 - 9706 */
  8174. netdev->min_mtu = ETH_MIN_MTU;
  8175. netdev->max_mtu = I40E_MAX_RXBUFFER -
  8176. (ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
  8177. return 0;
  8178. }
  8179. /**
  8180. * i40e_vsi_delete - Delete a VSI from the switch
  8181. * @vsi: the VSI being removed
  8182. *
  8183. * Returns 0 on success, negative value on failure
  8184. **/
  8185. static void i40e_vsi_delete(struct i40e_vsi *vsi)
  8186. {
  8187. /* remove default VSI is not allowed */
  8188. if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
  8189. return;
  8190. i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
  8191. }
  8192. /**
  8193. * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
  8194. * @vsi: the VSI being queried
  8195. *
  8196. * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
  8197. **/
  8198. int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
  8199. {
  8200. struct i40e_veb *veb;
  8201. struct i40e_pf *pf = vsi->back;
  8202. /* Uplink is not a bridge so default to VEB */
  8203. if (vsi->veb_idx == I40E_NO_VEB)
  8204. return 1;
  8205. veb = pf->veb[vsi->veb_idx];
  8206. if (!veb) {
  8207. dev_info(&pf->pdev->dev,
  8208. "There is no veb associated with the bridge\n");
  8209. return -ENOENT;
  8210. }
  8211. /* Uplink is a bridge in VEPA mode */
  8212. if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
  8213. return 0;
  8214. } else {
  8215. /* Uplink is a bridge in VEB mode */
  8216. return 1;
  8217. }
  8218. /* VEPA is now default bridge, so return 0 */
  8219. return 0;
  8220. }
  8221. /**
  8222. * i40e_add_vsi - Add a VSI to the switch
  8223. * @vsi: the VSI being configured
  8224. *
  8225. * This initializes a VSI context depending on the VSI type to be added and
  8226. * passes it down to the add_vsi aq command.
  8227. **/
  8228. static int i40e_add_vsi(struct i40e_vsi *vsi)
  8229. {
  8230. int ret = -ENODEV;
  8231. struct i40e_pf *pf = vsi->back;
  8232. struct i40e_hw *hw = &pf->hw;
  8233. struct i40e_vsi_context ctxt;
  8234. struct i40e_mac_filter *f;
  8235. struct hlist_node *h;
  8236. int bkt;
  8237. u8 enabled_tc = 0x1; /* TC0 enabled */
  8238. int f_count = 0;
  8239. memset(&ctxt, 0, sizeof(ctxt));
  8240. switch (vsi->type) {
  8241. case I40E_VSI_MAIN:
  8242. /* The PF's main VSI is already setup as part of the
  8243. * device initialization, so we'll not bother with
  8244. * the add_vsi call, but we will retrieve the current
  8245. * VSI context.
  8246. */
  8247. ctxt.seid = pf->main_vsi_seid;
  8248. ctxt.pf_num = pf->hw.pf_id;
  8249. ctxt.vf_num = 0;
  8250. ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
  8251. ctxt.flags = I40E_AQ_VSI_TYPE_PF;
  8252. if (ret) {
  8253. dev_info(&pf->pdev->dev,
  8254. "couldn't get PF vsi config, err %s aq_err %s\n",
  8255. i40e_stat_str(&pf->hw, ret),
  8256. i40e_aq_str(&pf->hw,
  8257. pf->hw.aq.asq_last_status));
  8258. return -ENOENT;
  8259. }
  8260. vsi->info = ctxt.info;
  8261. vsi->info.valid_sections = 0;
  8262. vsi->seid = ctxt.seid;
  8263. vsi->id = ctxt.vsi_number;
  8264. enabled_tc = i40e_pf_get_tc_map(pf);
  8265. /* MFP mode setup queue map and update VSI */
  8266. if ((pf->flags & I40E_FLAG_MFP_ENABLED) &&
  8267. !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
  8268. memset(&ctxt, 0, sizeof(ctxt));
  8269. ctxt.seid = pf->main_vsi_seid;
  8270. ctxt.pf_num = pf->hw.pf_id;
  8271. ctxt.vf_num = 0;
  8272. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
  8273. ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
  8274. if (ret) {
  8275. dev_info(&pf->pdev->dev,
  8276. "update vsi failed, err %s aq_err %s\n",
  8277. i40e_stat_str(&pf->hw, ret),
  8278. i40e_aq_str(&pf->hw,
  8279. pf->hw.aq.asq_last_status));
  8280. ret = -ENOENT;
  8281. goto err;
  8282. }
  8283. /* update the local VSI info queue map */
  8284. i40e_vsi_update_queue_map(vsi, &ctxt);
  8285. vsi->info.valid_sections = 0;
  8286. } else {
  8287. /* Default/Main VSI is only enabled for TC0
  8288. * reconfigure it to enable all TCs that are
  8289. * available on the port in SFP mode.
  8290. * For MFP case the iSCSI PF would use this
  8291. * flow to enable LAN+iSCSI TC.
  8292. */
  8293. ret = i40e_vsi_config_tc(vsi, enabled_tc);
  8294. if (ret) {
  8295. dev_info(&pf->pdev->dev,
  8296. "failed to configure TCs for main VSI tc_map 0x%08x, err %s aq_err %s\n",
  8297. enabled_tc,
  8298. i40e_stat_str(&pf->hw, ret),
  8299. i40e_aq_str(&pf->hw,
  8300. pf->hw.aq.asq_last_status));
  8301. ret = -ENOENT;
  8302. }
  8303. }
  8304. break;
  8305. case I40E_VSI_FDIR:
  8306. ctxt.pf_num = hw->pf_id;
  8307. ctxt.vf_num = 0;
  8308. ctxt.uplink_seid = vsi->uplink_seid;
  8309. ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
  8310. ctxt.flags = I40E_AQ_VSI_TYPE_PF;
  8311. if ((pf->flags & I40E_FLAG_VEB_MODE_ENABLED) &&
  8312. (i40e_is_vsi_uplink_mode_veb(vsi))) {
  8313. ctxt.info.valid_sections |=
  8314. cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  8315. ctxt.info.switch_id =
  8316. cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  8317. }
  8318. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
  8319. break;
  8320. case I40E_VSI_VMDQ2:
  8321. ctxt.pf_num = hw->pf_id;
  8322. ctxt.vf_num = 0;
  8323. ctxt.uplink_seid = vsi->uplink_seid;
  8324. ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
  8325. ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
  8326. /* This VSI is connected to VEB so the switch_id
  8327. * should be set to zero by default.
  8328. */
  8329. if (i40e_is_vsi_uplink_mode_veb(vsi)) {
  8330. ctxt.info.valid_sections |=
  8331. cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  8332. ctxt.info.switch_id =
  8333. cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  8334. }
  8335. /* Setup the VSI tx/rx queue map for TC0 only for now */
  8336. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
  8337. break;
  8338. case I40E_VSI_SRIOV:
  8339. ctxt.pf_num = hw->pf_id;
  8340. ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
  8341. ctxt.uplink_seid = vsi->uplink_seid;
  8342. ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
  8343. ctxt.flags = I40E_AQ_VSI_TYPE_VF;
  8344. /* This VSI is connected to VEB so the switch_id
  8345. * should be set to zero by default.
  8346. */
  8347. if (i40e_is_vsi_uplink_mode_veb(vsi)) {
  8348. ctxt.info.valid_sections |=
  8349. cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  8350. ctxt.info.switch_id =
  8351. cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  8352. }
  8353. if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
  8354. ctxt.info.valid_sections |=
  8355. cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
  8356. ctxt.info.queueing_opt_flags |=
  8357. (I40E_AQ_VSI_QUE_OPT_TCP_ENA |
  8358. I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI);
  8359. }
  8360. ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
  8361. ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
  8362. if (pf->vf[vsi->vf_id].spoofchk) {
  8363. ctxt.info.valid_sections |=
  8364. cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
  8365. ctxt.info.sec_flags |=
  8366. (I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
  8367. I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
  8368. }
  8369. /* Setup the VSI tx/rx queue map for TC0 only for now */
  8370. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
  8371. break;
  8372. case I40E_VSI_IWARP:
  8373. /* send down message to iWARP */
  8374. break;
  8375. default:
  8376. return -ENODEV;
  8377. }
  8378. if (vsi->type != I40E_VSI_MAIN) {
  8379. ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
  8380. if (ret) {
  8381. dev_info(&vsi->back->pdev->dev,
  8382. "add vsi failed, err %s aq_err %s\n",
  8383. i40e_stat_str(&pf->hw, ret),
  8384. i40e_aq_str(&pf->hw,
  8385. pf->hw.aq.asq_last_status));
  8386. ret = -ENOENT;
  8387. goto err;
  8388. }
  8389. vsi->info = ctxt.info;
  8390. vsi->info.valid_sections = 0;
  8391. vsi->seid = ctxt.seid;
  8392. vsi->id = ctxt.vsi_number;
  8393. }
  8394. vsi->active_filters = 0;
  8395. clear_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state);
  8396. spin_lock_bh(&vsi->mac_filter_hash_lock);
  8397. /* If macvlan filters already exist, force them to get loaded */
  8398. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  8399. f->state = I40E_FILTER_NEW;
  8400. f_count++;
  8401. }
  8402. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  8403. if (f_count) {
  8404. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  8405. pf->flags |= I40E_FLAG_FILTER_SYNC;
  8406. }
  8407. /* Update VSI BW information */
  8408. ret = i40e_vsi_get_bw_info(vsi);
  8409. if (ret) {
  8410. dev_info(&pf->pdev->dev,
  8411. "couldn't get vsi bw info, err %s aq_err %s\n",
  8412. i40e_stat_str(&pf->hw, ret),
  8413. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  8414. /* VSI is already added so not tearing that up */
  8415. ret = 0;
  8416. }
  8417. err:
  8418. return ret;
  8419. }
  8420. /**
  8421. * i40e_vsi_release - Delete a VSI and free its resources
  8422. * @vsi: the VSI being removed
  8423. *
  8424. * Returns 0 on success or < 0 on error
  8425. **/
  8426. int i40e_vsi_release(struct i40e_vsi *vsi)
  8427. {
  8428. struct i40e_mac_filter *f;
  8429. struct hlist_node *h;
  8430. struct i40e_veb *veb = NULL;
  8431. struct i40e_pf *pf;
  8432. u16 uplink_seid;
  8433. int i, n, bkt;
  8434. pf = vsi->back;
  8435. /* release of a VEB-owner or last VSI is not allowed */
  8436. if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
  8437. dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
  8438. vsi->seid, vsi->uplink_seid);
  8439. return -ENODEV;
  8440. }
  8441. if (vsi == pf->vsi[pf->lan_vsi] &&
  8442. !test_bit(__I40E_DOWN, &pf->state)) {
  8443. dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
  8444. return -ENODEV;
  8445. }
  8446. uplink_seid = vsi->uplink_seid;
  8447. if (vsi->type != I40E_VSI_SRIOV) {
  8448. if (vsi->netdev_registered) {
  8449. vsi->netdev_registered = false;
  8450. if (vsi->netdev) {
  8451. /* results in a call to i40e_close() */
  8452. unregister_netdev(vsi->netdev);
  8453. }
  8454. } else {
  8455. i40e_vsi_close(vsi);
  8456. }
  8457. i40e_vsi_disable_irq(vsi);
  8458. }
  8459. spin_lock_bh(&vsi->mac_filter_hash_lock);
  8460. /* clear the sync flag on all filters */
  8461. if (vsi->netdev) {
  8462. __dev_uc_unsync(vsi->netdev, NULL);
  8463. __dev_mc_unsync(vsi->netdev, NULL);
  8464. }
  8465. /* make sure any remaining filters are marked for deletion */
  8466. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
  8467. __i40e_del_filter(vsi, f);
  8468. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  8469. i40e_sync_vsi_filters(vsi);
  8470. i40e_vsi_delete(vsi);
  8471. i40e_vsi_free_q_vectors(vsi);
  8472. if (vsi->netdev) {
  8473. free_netdev(vsi->netdev);
  8474. vsi->netdev = NULL;
  8475. }
  8476. i40e_vsi_clear_rings(vsi);
  8477. i40e_vsi_clear(vsi);
  8478. /* If this was the last thing on the VEB, except for the
  8479. * controlling VSI, remove the VEB, which puts the controlling
  8480. * VSI onto the next level down in the switch.
  8481. *
  8482. * Well, okay, there's one more exception here: don't remove
  8483. * the orphan VEBs yet. We'll wait for an explicit remove request
  8484. * from up the network stack.
  8485. */
  8486. for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
  8487. if (pf->vsi[i] &&
  8488. pf->vsi[i]->uplink_seid == uplink_seid &&
  8489. (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
  8490. n++; /* count the VSIs */
  8491. }
  8492. }
  8493. for (i = 0; i < I40E_MAX_VEB; i++) {
  8494. if (!pf->veb[i])
  8495. continue;
  8496. if (pf->veb[i]->uplink_seid == uplink_seid)
  8497. n++; /* count the VEBs */
  8498. if (pf->veb[i]->seid == uplink_seid)
  8499. veb = pf->veb[i];
  8500. }
  8501. if (n == 0 && veb && veb->uplink_seid != 0)
  8502. i40e_veb_release(veb);
  8503. return 0;
  8504. }
  8505. /**
  8506. * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
  8507. * @vsi: ptr to the VSI
  8508. *
  8509. * This should only be called after i40e_vsi_mem_alloc() which allocates the
  8510. * corresponding SW VSI structure and initializes num_queue_pairs for the
  8511. * newly allocated VSI.
  8512. *
  8513. * Returns 0 on success or negative on failure
  8514. **/
  8515. static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
  8516. {
  8517. int ret = -ENOENT;
  8518. struct i40e_pf *pf = vsi->back;
  8519. if (vsi->q_vectors[0]) {
  8520. dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
  8521. vsi->seid);
  8522. return -EEXIST;
  8523. }
  8524. if (vsi->base_vector) {
  8525. dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
  8526. vsi->seid, vsi->base_vector);
  8527. return -EEXIST;
  8528. }
  8529. ret = i40e_vsi_alloc_q_vectors(vsi);
  8530. if (ret) {
  8531. dev_info(&pf->pdev->dev,
  8532. "failed to allocate %d q_vector for VSI %d, ret=%d\n",
  8533. vsi->num_q_vectors, vsi->seid, ret);
  8534. vsi->num_q_vectors = 0;
  8535. goto vector_setup_out;
  8536. }
  8537. /* In Legacy mode, we do not have to get any other vector since we
  8538. * piggyback on the misc/ICR0 for queue interrupts.
  8539. */
  8540. if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
  8541. return ret;
  8542. if (vsi->num_q_vectors)
  8543. vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
  8544. vsi->num_q_vectors, vsi->idx);
  8545. if (vsi->base_vector < 0) {
  8546. dev_info(&pf->pdev->dev,
  8547. "failed to get tracking for %d vectors for VSI %d, err=%d\n",
  8548. vsi->num_q_vectors, vsi->seid, vsi->base_vector);
  8549. i40e_vsi_free_q_vectors(vsi);
  8550. ret = -ENOENT;
  8551. goto vector_setup_out;
  8552. }
  8553. vector_setup_out:
  8554. return ret;
  8555. }
  8556. /**
  8557. * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
  8558. * @vsi: pointer to the vsi.
  8559. *
  8560. * This re-allocates a vsi's queue resources.
  8561. *
  8562. * Returns pointer to the successfully allocated and configured VSI sw struct
  8563. * on success, otherwise returns NULL on failure.
  8564. **/
  8565. static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
  8566. {
  8567. struct i40e_pf *pf;
  8568. u8 enabled_tc;
  8569. int ret;
  8570. if (!vsi)
  8571. return NULL;
  8572. pf = vsi->back;
  8573. i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
  8574. i40e_vsi_clear_rings(vsi);
  8575. i40e_vsi_free_arrays(vsi, false);
  8576. i40e_set_num_rings_in_vsi(vsi);
  8577. ret = i40e_vsi_alloc_arrays(vsi, false);
  8578. if (ret)
  8579. goto err_vsi;
  8580. ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs, vsi->idx);
  8581. if (ret < 0) {
  8582. dev_info(&pf->pdev->dev,
  8583. "failed to get tracking for %d queues for VSI %d err %d\n",
  8584. vsi->alloc_queue_pairs, vsi->seid, ret);
  8585. goto err_vsi;
  8586. }
  8587. vsi->base_queue = ret;
  8588. /* Update the FW view of the VSI. Force a reset of TC and queue
  8589. * layout configurations.
  8590. */
  8591. enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
  8592. pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
  8593. pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
  8594. i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
  8595. if (vsi->type == I40E_VSI_MAIN)
  8596. i40e_rm_default_mac_filter(vsi, pf->hw.mac.perm_addr);
  8597. /* assign it some queues */
  8598. ret = i40e_alloc_rings(vsi);
  8599. if (ret)
  8600. goto err_rings;
  8601. /* map all of the rings to the q_vectors */
  8602. i40e_vsi_map_rings_to_vectors(vsi);
  8603. return vsi;
  8604. err_rings:
  8605. i40e_vsi_free_q_vectors(vsi);
  8606. if (vsi->netdev_registered) {
  8607. vsi->netdev_registered = false;
  8608. unregister_netdev(vsi->netdev);
  8609. free_netdev(vsi->netdev);
  8610. vsi->netdev = NULL;
  8611. }
  8612. i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
  8613. err_vsi:
  8614. i40e_vsi_clear(vsi);
  8615. return NULL;
  8616. }
  8617. /**
  8618. * i40e_vsi_setup - Set up a VSI by a given type
  8619. * @pf: board private structure
  8620. * @type: VSI type
  8621. * @uplink_seid: the switch element to link to
  8622. * @param1: usage depends upon VSI type. For VF types, indicates VF id
  8623. *
  8624. * This allocates the sw VSI structure and its queue resources, then add a VSI
  8625. * to the identified VEB.
  8626. *
  8627. * Returns pointer to the successfully allocated and configure VSI sw struct on
  8628. * success, otherwise returns NULL on failure.
  8629. **/
  8630. struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
  8631. u16 uplink_seid, u32 param1)
  8632. {
  8633. struct i40e_vsi *vsi = NULL;
  8634. struct i40e_veb *veb = NULL;
  8635. int ret, i;
  8636. int v_idx;
  8637. /* The requested uplink_seid must be either
  8638. * - the PF's port seid
  8639. * no VEB is needed because this is the PF
  8640. * or this is a Flow Director special case VSI
  8641. * - seid of an existing VEB
  8642. * - seid of a VSI that owns an existing VEB
  8643. * - seid of a VSI that doesn't own a VEB
  8644. * a new VEB is created and the VSI becomes the owner
  8645. * - seid of the PF VSI, which is what creates the first VEB
  8646. * this is a special case of the previous
  8647. *
  8648. * Find which uplink_seid we were given and create a new VEB if needed
  8649. */
  8650. for (i = 0; i < I40E_MAX_VEB; i++) {
  8651. if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
  8652. veb = pf->veb[i];
  8653. break;
  8654. }
  8655. }
  8656. if (!veb && uplink_seid != pf->mac_seid) {
  8657. for (i = 0; i < pf->num_alloc_vsi; i++) {
  8658. if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
  8659. vsi = pf->vsi[i];
  8660. break;
  8661. }
  8662. }
  8663. if (!vsi) {
  8664. dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
  8665. uplink_seid);
  8666. return NULL;
  8667. }
  8668. if (vsi->uplink_seid == pf->mac_seid)
  8669. veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
  8670. vsi->tc_config.enabled_tc);
  8671. else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
  8672. veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
  8673. vsi->tc_config.enabled_tc);
  8674. if (veb) {
  8675. if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
  8676. dev_info(&vsi->back->pdev->dev,
  8677. "New VSI creation error, uplink seid of LAN VSI expected.\n");
  8678. return NULL;
  8679. }
  8680. /* We come up by default in VEPA mode if SRIOV is not
  8681. * already enabled, in which case we can't force VEPA
  8682. * mode.
  8683. */
  8684. if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
  8685. veb->bridge_mode = BRIDGE_MODE_VEPA;
  8686. pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
  8687. }
  8688. i40e_config_bridge_mode(veb);
  8689. }
  8690. for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
  8691. if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
  8692. veb = pf->veb[i];
  8693. }
  8694. if (!veb) {
  8695. dev_info(&pf->pdev->dev, "couldn't add VEB\n");
  8696. return NULL;
  8697. }
  8698. vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
  8699. uplink_seid = veb->seid;
  8700. }
  8701. /* get vsi sw struct */
  8702. v_idx = i40e_vsi_mem_alloc(pf, type);
  8703. if (v_idx < 0)
  8704. goto err_alloc;
  8705. vsi = pf->vsi[v_idx];
  8706. if (!vsi)
  8707. goto err_alloc;
  8708. vsi->type = type;
  8709. vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
  8710. if (type == I40E_VSI_MAIN)
  8711. pf->lan_vsi = v_idx;
  8712. else if (type == I40E_VSI_SRIOV)
  8713. vsi->vf_id = param1;
  8714. /* assign it some queues */
  8715. ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs,
  8716. vsi->idx);
  8717. if (ret < 0) {
  8718. dev_info(&pf->pdev->dev,
  8719. "failed to get tracking for %d queues for VSI %d err=%d\n",
  8720. vsi->alloc_queue_pairs, vsi->seid, ret);
  8721. goto err_vsi;
  8722. }
  8723. vsi->base_queue = ret;
  8724. /* get a VSI from the hardware */
  8725. vsi->uplink_seid = uplink_seid;
  8726. ret = i40e_add_vsi(vsi);
  8727. if (ret)
  8728. goto err_vsi;
  8729. switch (vsi->type) {
  8730. /* setup the netdev if needed */
  8731. case I40E_VSI_MAIN:
  8732. /* Apply relevant filters if a platform-specific mac
  8733. * address was selected.
  8734. */
  8735. if (!!(pf->flags & I40E_FLAG_PF_MAC)) {
  8736. ret = i40e_macaddr_init(vsi, pf->hw.mac.addr);
  8737. if (ret) {
  8738. dev_warn(&pf->pdev->dev,
  8739. "could not set up macaddr; err %d\n",
  8740. ret);
  8741. }
  8742. }
  8743. case I40E_VSI_VMDQ2:
  8744. ret = i40e_config_netdev(vsi);
  8745. if (ret)
  8746. goto err_netdev;
  8747. ret = register_netdev(vsi->netdev);
  8748. if (ret)
  8749. goto err_netdev;
  8750. vsi->netdev_registered = true;
  8751. netif_carrier_off(vsi->netdev);
  8752. #ifdef CONFIG_I40E_DCB
  8753. /* Setup DCB netlink interface */
  8754. i40e_dcbnl_setup(vsi);
  8755. #endif /* CONFIG_I40E_DCB */
  8756. /* fall through */
  8757. case I40E_VSI_FDIR:
  8758. /* set up vectors and rings if needed */
  8759. ret = i40e_vsi_setup_vectors(vsi);
  8760. if (ret)
  8761. goto err_msix;
  8762. ret = i40e_alloc_rings(vsi);
  8763. if (ret)
  8764. goto err_rings;
  8765. /* map all of the rings to the q_vectors */
  8766. i40e_vsi_map_rings_to_vectors(vsi);
  8767. i40e_vsi_reset_stats(vsi);
  8768. break;
  8769. default:
  8770. /* no netdev or rings for the other VSI types */
  8771. break;
  8772. }
  8773. if ((pf->flags & I40E_FLAG_RSS_AQ_CAPABLE) &&
  8774. (vsi->type == I40E_VSI_VMDQ2)) {
  8775. ret = i40e_vsi_config_rss(vsi);
  8776. }
  8777. return vsi;
  8778. err_rings:
  8779. i40e_vsi_free_q_vectors(vsi);
  8780. err_msix:
  8781. if (vsi->netdev_registered) {
  8782. vsi->netdev_registered = false;
  8783. unregister_netdev(vsi->netdev);
  8784. free_netdev(vsi->netdev);
  8785. vsi->netdev = NULL;
  8786. }
  8787. err_netdev:
  8788. i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
  8789. err_vsi:
  8790. i40e_vsi_clear(vsi);
  8791. err_alloc:
  8792. return NULL;
  8793. }
  8794. /**
  8795. * i40e_veb_get_bw_info - Query VEB BW information
  8796. * @veb: the veb to query
  8797. *
  8798. * Query the Tx scheduler BW configuration data for given VEB
  8799. **/
  8800. static int i40e_veb_get_bw_info(struct i40e_veb *veb)
  8801. {
  8802. struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
  8803. struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
  8804. struct i40e_pf *pf = veb->pf;
  8805. struct i40e_hw *hw = &pf->hw;
  8806. u32 tc_bw_max;
  8807. int ret = 0;
  8808. int i;
  8809. ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
  8810. &bw_data, NULL);
  8811. if (ret) {
  8812. dev_info(&pf->pdev->dev,
  8813. "query veb bw config failed, err %s aq_err %s\n",
  8814. i40e_stat_str(&pf->hw, ret),
  8815. i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
  8816. goto out;
  8817. }
  8818. ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
  8819. &ets_data, NULL);
  8820. if (ret) {
  8821. dev_info(&pf->pdev->dev,
  8822. "query veb bw ets config failed, err %s aq_err %s\n",
  8823. i40e_stat_str(&pf->hw, ret),
  8824. i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
  8825. goto out;
  8826. }
  8827. veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
  8828. veb->bw_max_quanta = ets_data.tc_bw_max;
  8829. veb->is_abs_credits = bw_data.absolute_credits_enable;
  8830. veb->enabled_tc = ets_data.tc_valid_bits;
  8831. tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
  8832. (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
  8833. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  8834. veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
  8835. veb->bw_tc_limit_credits[i] =
  8836. le16_to_cpu(bw_data.tc_bw_limits[i]);
  8837. veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
  8838. }
  8839. out:
  8840. return ret;
  8841. }
  8842. /**
  8843. * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
  8844. * @pf: board private structure
  8845. *
  8846. * On error: returns error code (negative)
  8847. * On success: returns vsi index in PF (positive)
  8848. **/
  8849. static int i40e_veb_mem_alloc(struct i40e_pf *pf)
  8850. {
  8851. int ret = -ENOENT;
  8852. struct i40e_veb *veb;
  8853. int i;
  8854. /* Need to protect the allocation of switch elements at the PF level */
  8855. mutex_lock(&pf->switch_mutex);
  8856. /* VEB list may be fragmented if VEB creation/destruction has
  8857. * been happening. We can afford to do a quick scan to look
  8858. * for any free slots in the list.
  8859. *
  8860. * find next empty veb slot, looping back around if necessary
  8861. */
  8862. i = 0;
  8863. while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
  8864. i++;
  8865. if (i >= I40E_MAX_VEB) {
  8866. ret = -ENOMEM;
  8867. goto err_alloc_veb; /* out of VEB slots! */
  8868. }
  8869. veb = kzalloc(sizeof(*veb), GFP_KERNEL);
  8870. if (!veb) {
  8871. ret = -ENOMEM;
  8872. goto err_alloc_veb;
  8873. }
  8874. veb->pf = pf;
  8875. veb->idx = i;
  8876. veb->enabled_tc = 1;
  8877. pf->veb[i] = veb;
  8878. ret = i;
  8879. err_alloc_veb:
  8880. mutex_unlock(&pf->switch_mutex);
  8881. return ret;
  8882. }
  8883. /**
  8884. * i40e_switch_branch_release - Delete a branch of the switch tree
  8885. * @branch: where to start deleting
  8886. *
  8887. * This uses recursion to find the tips of the branch to be
  8888. * removed, deleting until we get back to and can delete this VEB.
  8889. **/
  8890. static void i40e_switch_branch_release(struct i40e_veb *branch)
  8891. {
  8892. struct i40e_pf *pf = branch->pf;
  8893. u16 branch_seid = branch->seid;
  8894. u16 veb_idx = branch->idx;
  8895. int i;
  8896. /* release any VEBs on this VEB - RECURSION */
  8897. for (i = 0; i < I40E_MAX_VEB; i++) {
  8898. if (!pf->veb[i])
  8899. continue;
  8900. if (pf->veb[i]->uplink_seid == branch->seid)
  8901. i40e_switch_branch_release(pf->veb[i]);
  8902. }
  8903. /* Release the VSIs on this VEB, but not the owner VSI.
  8904. *
  8905. * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
  8906. * the VEB itself, so don't use (*branch) after this loop.
  8907. */
  8908. for (i = 0; i < pf->num_alloc_vsi; i++) {
  8909. if (!pf->vsi[i])
  8910. continue;
  8911. if (pf->vsi[i]->uplink_seid == branch_seid &&
  8912. (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
  8913. i40e_vsi_release(pf->vsi[i]);
  8914. }
  8915. }
  8916. /* There's one corner case where the VEB might not have been
  8917. * removed, so double check it here and remove it if needed.
  8918. * This case happens if the veb was created from the debugfs
  8919. * commands and no VSIs were added to it.
  8920. */
  8921. if (pf->veb[veb_idx])
  8922. i40e_veb_release(pf->veb[veb_idx]);
  8923. }
  8924. /**
  8925. * i40e_veb_clear - remove veb struct
  8926. * @veb: the veb to remove
  8927. **/
  8928. static void i40e_veb_clear(struct i40e_veb *veb)
  8929. {
  8930. if (!veb)
  8931. return;
  8932. if (veb->pf) {
  8933. struct i40e_pf *pf = veb->pf;
  8934. mutex_lock(&pf->switch_mutex);
  8935. if (pf->veb[veb->idx] == veb)
  8936. pf->veb[veb->idx] = NULL;
  8937. mutex_unlock(&pf->switch_mutex);
  8938. }
  8939. kfree(veb);
  8940. }
  8941. /**
  8942. * i40e_veb_release - Delete a VEB and free its resources
  8943. * @veb: the VEB being removed
  8944. **/
  8945. void i40e_veb_release(struct i40e_veb *veb)
  8946. {
  8947. struct i40e_vsi *vsi = NULL;
  8948. struct i40e_pf *pf;
  8949. int i, n = 0;
  8950. pf = veb->pf;
  8951. /* find the remaining VSI and check for extras */
  8952. for (i = 0; i < pf->num_alloc_vsi; i++) {
  8953. if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
  8954. n++;
  8955. vsi = pf->vsi[i];
  8956. }
  8957. }
  8958. if (n != 1) {
  8959. dev_info(&pf->pdev->dev,
  8960. "can't remove VEB %d with %d VSIs left\n",
  8961. veb->seid, n);
  8962. return;
  8963. }
  8964. /* move the remaining VSI to uplink veb */
  8965. vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
  8966. if (veb->uplink_seid) {
  8967. vsi->uplink_seid = veb->uplink_seid;
  8968. if (veb->uplink_seid == pf->mac_seid)
  8969. vsi->veb_idx = I40E_NO_VEB;
  8970. else
  8971. vsi->veb_idx = veb->veb_idx;
  8972. } else {
  8973. /* floating VEB */
  8974. vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
  8975. vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
  8976. }
  8977. i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
  8978. i40e_veb_clear(veb);
  8979. }
  8980. /**
  8981. * i40e_add_veb - create the VEB in the switch
  8982. * @veb: the VEB to be instantiated
  8983. * @vsi: the controlling VSI
  8984. **/
  8985. static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
  8986. {
  8987. struct i40e_pf *pf = veb->pf;
  8988. bool enable_stats = !!(pf->flags & I40E_FLAG_VEB_STATS_ENABLED);
  8989. int ret;
  8990. ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi->seid,
  8991. veb->enabled_tc, false,
  8992. &veb->seid, enable_stats, NULL);
  8993. /* get a VEB from the hardware */
  8994. if (ret) {
  8995. dev_info(&pf->pdev->dev,
  8996. "couldn't add VEB, err %s aq_err %s\n",
  8997. i40e_stat_str(&pf->hw, ret),
  8998. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  8999. return -EPERM;
  9000. }
  9001. /* get statistics counter */
  9002. ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
  9003. &veb->stats_idx, NULL, NULL, NULL);
  9004. if (ret) {
  9005. dev_info(&pf->pdev->dev,
  9006. "couldn't get VEB statistics idx, err %s aq_err %s\n",
  9007. i40e_stat_str(&pf->hw, ret),
  9008. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  9009. return -EPERM;
  9010. }
  9011. ret = i40e_veb_get_bw_info(veb);
  9012. if (ret) {
  9013. dev_info(&pf->pdev->dev,
  9014. "couldn't get VEB bw info, err %s aq_err %s\n",
  9015. i40e_stat_str(&pf->hw, ret),
  9016. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  9017. i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
  9018. return -ENOENT;
  9019. }
  9020. vsi->uplink_seid = veb->seid;
  9021. vsi->veb_idx = veb->idx;
  9022. vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
  9023. return 0;
  9024. }
  9025. /**
  9026. * i40e_veb_setup - Set up a VEB
  9027. * @pf: board private structure
  9028. * @flags: VEB setup flags
  9029. * @uplink_seid: the switch element to link to
  9030. * @vsi_seid: the initial VSI seid
  9031. * @enabled_tc: Enabled TC bit-map
  9032. *
  9033. * This allocates the sw VEB structure and links it into the switch
  9034. * It is possible and legal for this to be a duplicate of an already
  9035. * existing VEB. It is also possible for both uplink and vsi seids
  9036. * to be zero, in order to create a floating VEB.
  9037. *
  9038. * Returns pointer to the successfully allocated VEB sw struct on
  9039. * success, otherwise returns NULL on failure.
  9040. **/
  9041. struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
  9042. u16 uplink_seid, u16 vsi_seid,
  9043. u8 enabled_tc)
  9044. {
  9045. struct i40e_veb *veb, *uplink_veb = NULL;
  9046. int vsi_idx, veb_idx;
  9047. int ret;
  9048. /* if one seid is 0, the other must be 0 to create a floating relay */
  9049. if ((uplink_seid == 0 || vsi_seid == 0) &&
  9050. (uplink_seid + vsi_seid != 0)) {
  9051. dev_info(&pf->pdev->dev,
  9052. "one, not both seid's are 0: uplink=%d vsi=%d\n",
  9053. uplink_seid, vsi_seid);
  9054. return NULL;
  9055. }
  9056. /* make sure there is such a vsi and uplink */
  9057. for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
  9058. if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
  9059. break;
  9060. if (vsi_idx >= pf->num_alloc_vsi && vsi_seid != 0) {
  9061. dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
  9062. vsi_seid);
  9063. return NULL;
  9064. }
  9065. if (uplink_seid && uplink_seid != pf->mac_seid) {
  9066. for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
  9067. if (pf->veb[veb_idx] &&
  9068. pf->veb[veb_idx]->seid == uplink_seid) {
  9069. uplink_veb = pf->veb[veb_idx];
  9070. break;
  9071. }
  9072. }
  9073. if (!uplink_veb) {
  9074. dev_info(&pf->pdev->dev,
  9075. "uplink seid %d not found\n", uplink_seid);
  9076. return NULL;
  9077. }
  9078. }
  9079. /* get veb sw struct */
  9080. veb_idx = i40e_veb_mem_alloc(pf);
  9081. if (veb_idx < 0)
  9082. goto err_alloc;
  9083. veb = pf->veb[veb_idx];
  9084. veb->flags = flags;
  9085. veb->uplink_seid = uplink_seid;
  9086. veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
  9087. veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
  9088. /* create the VEB in the switch */
  9089. ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
  9090. if (ret)
  9091. goto err_veb;
  9092. if (vsi_idx == pf->lan_vsi)
  9093. pf->lan_veb = veb->idx;
  9094. return veb;
  9095. err_veb:
  9096. i40e_veb_clear(veb);
  9097. err_alloc:
  9098. return NULL;
  9099. }
  9100. /**
  9101. * i40e_setup_pf_switch_element - set PF vars based on switch type
  9102. * @pf: board private structure
  9103. * @ele: element we are building info from
  9104. * @num_reported: total number of elements
  9105. * @printconfig: should we print the contents
  9106. *
  9107. * helper function to assist in extracting a few useful SEID values.
  9108. **/
  9109. static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
  9110. struct i40e_aqc_switch_config_element_resp *ele,
  9111. u16 num_reported, bool printconfig)
  9112. {
  9113. u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
  9114. u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
  9115. u8 element_type = ele->element_type;
  9116. u16 seid = le16_to_cpu(ele->seid);
  9117. if (printconfig)
  9118. dev_info(&pf->pdev->dev,
  9119. "type=%d seid=%d uplink=%d downlink=%d\n",
  9120. element_type, seid, uplink_seid, downlink_seid);
  9121. switch (element_type) {
  9122. case I40E_SWITCH_ELEMENT_TYPE_MAC:
  9123. pf->mac_seid = seid;
  9124. break;
  9125. case I40E_SWITCH_ELEMENT_TYPE_VEB:
  9126. /* Main VEB? */
  9127. if (uplink_seid != pf->mac_seid)
  9128. break;
  9129. if (pf->lan_veb == I40E_NO_VEB) {
  9130. int v;
  9131. /* find existing or else empty VEB */
  9132. for (v = 0; v < I40E_MAX_VEB; v++) {
  9133. if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
  9134. pf->lan_veb = v;
  9135. break;
  9136. }
  9137. }
  9138. if (pf->lan_veb == I40E_NO_VEB) {
  9139. v = i40e_veb_mem_alloc(pf);
  9140. if (v < 0)
  9141. break;
  9142. pf->lan_veb = v;
  9143. }
  9144. }
  9145. pf->veb[pf->lan_veb]->seid = seid;
  9146. pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
  9147. pf->veb[pf->lan_veb]->pf = pf;
  9148. pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
  9149. break;
  9150. case I40E_SWITCH_ELEMENT_TYPE_VSI:
  9151. if (num_reported != 1)
  9152. break;
  9153. /* This is immediately after a reset so we can assume this is
  9154. * the PF's VSI
  9155. */
  9156. pf->mac_seid = uplink_seid;
  9157. pf->pf_seid = downlink_seid;
  9158. pf->main_vsi_seid = seid;
  9159. if (printconfig)
  9160. dev_info(&pf->pdev->dev,
  9161. "pf_seid=%d main_vsi_seid=%d\n",
  9162. pf->pf_seid, pf->main_vsi_seid);
  9163. break;
  9164. case I40E_SWITCH_ELEMENT_TYPE_PF:
  9165. case I40E_SWITCH_ELEMENT_TYPE_VF:
  9166. case I40E_SWITCH_ELEMENT_TYPE_EMP:
  9167. case I40E_SWITCH_ELEMENT_TYPE_BMC:
  9168. case I40E_SWITCH_ELEMENT_TYPE_PE:
  9169. case I40E_SWITCH_ELEMENT_TYPE_PA:
  9170. /* ignore these for now */
  9171. break;
  9172. default:
  9173. dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
  9174. element_type, seid);
  9175. break;
  9176. }
  9177. }
  9178. /**
  9179. * i40e_fetch_switch_configuration - Get switch config from firmware
  9180. * @pf: board private structure
  9181. * @printconfig: should we print the contents
  9182. *
  9183. * Get the current switch configuration from the device and
  9184. * extract a few useful SEID values.
  9185. **/
  9186. int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
  9187. {
  9188. struct i40e_aqc_get_switch_config_resp *sw_config;
  9189. u16 next_seid = 0;
  9190. int ret = 0;
  9191. u8 *aq_buf;
  9192. int i;
  9193. aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
  9194. if (!aq_buf)
  9195. return -ENOMEM;
  9196. sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
  9197. do {
  9198. u16 num_reported, num_total;
  9199. ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
  9200. I40E_AQ_LARGE_BUF,
  9201. &next_seid, NULL);
  9202. if (ret) {
  9203. dev_info(&pf->pdev->dev,
  9204. "get switch config failed err %s aq_err %s\n",
  9205. i40e_stat_str(&pf->hw, ret),
  9206. i40e_aq_str(&pf->hw,
  9207. pf->hw.aq.asq_last_status));
  9208. kfree(aq_buf);
  9209. return -ENOENT;
  9210. }
  9211. num_reported = le16_to_cpu(sw_config->header.num_reported);
  9212. num_total = le16_to_cpu(sw_config->header.num_total);
  9213. if (printconfig)
  9214. dev_info(&pf->pdev->dev,
  9215. "header: %d reported %d total\n",
  9216. num_reported, num_total);
  9217. for (i = 0; i < num_reported; i++) {
  9218. struct i40e_aqc_switch_config_element_resp *ele =
  9219. &sw_config->element[i];
  9220. i40e_setup_pf_switch_element(pf, ele, num_reported,
  9221. printconfig);
  9222. }
  9223. } while (next_seid != 0);
  9224. kfree(aq_buf);
  9225. return ret;
  9226. }
  9227. /**
  9228. * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
  9229. * @pf: board private structure
  9230. * @reinit: if the Main VSI needs to re-initialized.
  9231. *
  9232. * Returns 0 on success, negative value on failure
  9233. **/
  9234. static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit)
  9235. {
  9236. u16 flags = 0;
  9237. int ret;
  9238. /* find out what's out there already */
  9239. ret = i40e_fetch_switch_configuration(pf, false);
  9240. if (ret) {
  9241. dev_info(&pf->pdev->dev,
  9242. "couldn't fetch switch config, err %s aq_err %s\n",
  9243. i40e_stat_str(&pf->hw, ret),
  9244. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  9245. return ret;
  9246. }
  9247. i40e_pf_reset_stats(pf);
  9248. /* set the switch config bit for the whole device to
  9249. * support limited promisc or true promisc
  9250. * when user requests promisc. The default is limited
  9251. * promisc.
  9252. */
  9253. if ((pf->hw.pf_id == 0) &&
  9254. !(pf->flags & I40E_FLAG_TRUE_PROMISC_SUPPORT))
  9255. flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
  9256. if (pf->hw.pf_id == 0) {
  9257. u16 valid_flags;
  9258. valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
  9259. ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags,
  9260. NULL);
  9261. if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) {
  9262. dev_info(&pf->pdev->dev,
  9263. "couldn't set switch config bits, err %s aq_err %s\n",
  9264. i40e_stat_str(&pf->hw, ret),
  9265. i40e_aq_str(&pf->hw,
  9266. pf->hw.aq.asq_last_status));
  9267. /* not a fatal problem, just keep going */
  9268. }
  9269. }
  9270. /* first time setup */
  9271. if (pf->lan_vsi == I40E_NO_VSI || reinit) {
  9272. struct i40e_vsi *vsi = NULL;
  9273. u16 uplink_seid;
  9274. /* Set up the PF VSI associated with the PF's main VSI
  9275. * that is already in the HW switch
  9276. */
  9277. if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
  9278. uplink_seid = pf->veb[pf->lan_veb]->seid;
  9279. else
  9280. uplink_seid = pf->mac_seid;
  9281. if (pf->lan_vsi == I40E_NO_VSI)
  9282. vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
  9283. else if (reinit)
  9284. vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
  9285. if (!vsi) {
  9286. dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
  9287. i40e_fdir_teardown(pf);
  9288. return -EAGAIN;
  9289. }
  9290. } else {
  9291. /* force a reset of TC and queue layout configurations */
  9292. u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
  9293. pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
  9294. pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
  9295. i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
  9296. }
  9297. i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
  9298. i40e_fdir_sb_setup(pf);
  9299. /* Setup static PF queue filter control settings */
  9300. ret = i40e_setup_pf_filter_control(pf);
  9301. if (ret) {
  9302. dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
  9303. ret);
  9304. /* Failure here should not stop continuing other steps */
  9305. }
  9306. /* enable RSS in the HW, even for only one queue, as the stack can use
  9307. * the hash
  9308. */
  9309. if ((pf->flags & I40E_FLAG_RSS_ENABLED))
  9310. i40e_pf_config_rss(pf);
  9311. /* fill in link information and enable LSE reporting */
  9312. i40e_link_event(pf);
  9313. /* Initialize user-specific link properties */
  9314. pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
  9315. I40E_AQ_AN_COMPLETED) ? true : false);
  9316. i40e_ptp_init(pf);
  9317. return ret;
  9318. }
  9319. /**
  9320. * i40e_determine_queue_usage - Work out queue distribution
  9321. * @pf: board private structure
  9322. **/
  9323. static void i40e_determine_queue_usage(struct i40e_pf *pf)
  9324. {
  9325. int queues_left;
  9326. pf->num_lan_qps = 0;
  9327. /* Find the max queues to be put into basic use. We'll always be
  9328. * using TC0, whether or not DCB is running, and TC0 will get the
  9329. * big RSS set.
  9330. */
  9331. queues_left = pf->hw.func_caps.num_tx_qp;
  9332. if ((queues_left == 1) ||
  9333. !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
  9334. /* one qp for PF, no queues for anything else */
  9335. queues_left = 0;
  9336. pf->alloc_rss_size = pf->num_lan_qps = 1;
  9337. /* make sure all the fancies are disabled */
  9338. pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
  9339. I40E_FLAG_IWARP_ENABLED |
  9340. I40E_FLAG_FD_SB_ENABLED |
  9341. I40E_FLAG_FD_ATR_ENABLED |
  9342. I40E_FLAG_DCB_CAPABLE |
  9343. I40E_FLAG_DCB_ENABLED |
  9344. I40E_FLAG_SRIOV_ENABLED |
  9345. I40E_FLAG_VMDQ_ENABLED);
  9346. } else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
  9347. I40E_FLAG_FD_SB_ENABLED |
  9348. I40E_FLAG_FD_ATR_ENABLED |
  9349. I40E_FLAG_DCB_CAPABLE))) {
  9350. /* one qp for PF */
  9351. pf->alloc_rss_size = pf->num_lan_qps = 1;
  9352. queues_left -= pf->num_lan_qps;
  9353. pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
  9354. I40E_FLAG_IWARP_ENABLED |
  9355. I40E_FLAG_FD_SB_ENABLED |
  9356. I40E_FLAG_FD_ATR_ENABLED |
  9357. I40E_FLAG_DCB_ENABLED |
  9358. I40E_FLAG_VMDQ_ENABLED);
  9359. } else {
  9360. /* Not enough queues for all TCs */
  9361. if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
  9362. (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
  9363. pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
  9364. I40E_FLAG_DCB_ENABLED);
  9365. dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
  9366. }
  9367. pf->num_lan_qps = max_t(int, pf->rss_size_max,
  9368. num_online_cpus());
  9369. pf->num_lan_qps = min_t(int, pf->num_lan_qps,
  9370. pf->hw.func_caps.num_tx_qp);
  9371. queues_left -= pf->num_lan_qps;
  9372. }
  9373. if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
  9374. if (queues_left > 1) {
  9375. queues_left -= 1; /* save 1 queue for FD */
  9376. } else {
  9377. pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
  9378. dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
  9379. }
  9380. }
  9381. if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
  9382. pf->num_vf_qps && pf->num_req_vfs && queues_left) {
  9383. pf->num_req_vfs = min_t(int, pf->num_req_vfs,
  9384. (queues_left / pf->num_vf_qps));
  9385. queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
  9386. }
  9387. if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
  9388. pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
  9389. pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
  9390. (queues_left / pf->num_vmdq_qps));
  9391. queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
  9392. }
  9393. pf->queues_left = queues_left;
  9394. dev_dbg(&pf->pdev->dev,
  9395. "qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
  9396. pf->hw.func_caps.num_tx_qp,
  9397. !!(pf->flags & I40E_FLAG_FD_SB_ENABLED),
  9398. pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
  9399. pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
  9400. queues_left);
  9401. }
  9402. /**
  9403. * i40e_setup_pf_filter_control - Setup PF static filter control
  9404. * @pf: PF to be setup
  9405. *
  9406. * i40e_setup_pf_filter_control sets up a PF's initial filter control
  9407. * settings. If PE/FCoE are enabled then it will also set the per PF
  9408. * based filter sizes required for them. It also enables Flow director,
  9409. * ethertype and macvlan type filter settings for the pf.
  9410. *
  9411. * Returns 0 on success, negative on failure
  9412. **/
  9413. static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
  9414. {
  9415. struct i40e_filter_control_settings *settings = &pf->filter_settings;
  9416. settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
  9417. /* Flow Director is enabled */
  9418. if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
  9419. settings->enable_fdir = true;
  9420. /* Ethtype and MACVLAN filters enabled for PF */
  9421. settings->enable_ethtype = true;
  9422. settings->enable_macvlan = true;
  9423. if (i40e_set_filter_control(&pf->hw, settings))
  9424. return -ENOENT;
  9425. return 0;
  9426. }
  9427. #define INFO_STRING_LEN 255
  9428. #define REMAIN(__x) (INFO_STRING_LEN - (__x))
  9429. static void i40e_print_features(struct i40e_pf *pf)
  9430. {
  9431. struct i40e_hw *hw = &pf->hw;
  9432. char *buf;
  9433. int i;
  9434. buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
  9435. if (!buf)
  9436. return;
  9437. i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
  9438. #ifdef CONFIG_PCI_IOV
  9439. i += snprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
  9440. #endif
  9441. i += snprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d",
  9442. pf->hw.func_caps.num_vsis,
  9443. pf->vsi[pf->lan_vsi]->num_queue_pairs);
  9444. if (pf->flags & I40E_FLAG_RSS_ENABLED)
  9445. i += snprintf(&buf[i], REMAIN(i), " RSS");
  9446. if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
  9447. i += snprintf(&buf[i], REMAIN(i), " FD_ATR");
  9448. if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
  9449. i += snprintf(&buf[i], REMAIN(i), " FD_SB");
  9450. i += snprintf(&buf[i], REMAIN(i), " NTUPLE");
  9451. }
  9452. if (pf->flags & I40E_FLAG_DCB_CAPABLE)
  9453. i += snprintf(&buf[i], REMAIN(i), " DCB");
  9454. i += snprintf(&buf[i], REMAIN(i), " VxLAN");
  9455. i += snprintf(&buf[i], REMAIN(i), " Geneve");
  9456. if (pf->flags & I40E_FLAG_PTP)
  9457. i += snprintf(&buf[i], REMAIN(i), " PTP");
  9458. if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
  9459. i += snprintf(&buf[i], REMAIN(i), " VEB");
  9460. else
  9461. i += snprintf(&buf[i], REMAIN(i), " VEPA");
  9462. dev_info(&pf->pdev->dev, "%s\n", buf);
  9463. kfree(buf);
  9464. WARN_ON(i > INFO_STRING_LEN);
  9465. }
  9466. /**
  9467. * i40e_get_platform_mac_addr - get platform-specific MAC address
  9468. *
  9469. * @pdev: PCI device information struct
  9470. * @pf: board private structure
  9471. *
  9472. * Look up the MAC address in Open Firmware on systems that support it,
  9473. * and use IDPROM on SPARC if no OF address is found. On return, the
  9474. * I40E_FLAG_PF_MAC will be wset in pf->flags if a platform-specific value
  9475. * has been selected.
  9476. **/
  9477. static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
  9478. {
  9479. pf->flags &= ~I40E_FLAG_PF_MAC;
  9480. if (!eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
  9481. pf->flags |= I40E_FLAG_PF_MAC;
  9482. }
  9483. /**
  9484. * i40e_probe - Device initialization routine
  9485. * @pdev: PCI device information struct
  9486. * @ent: entry in i40e_pci_tbl
  9487. *
  9488. * i40e_probe initializes a PF identified by a pci_dev structure.
  9489. * The OS initialization, configuring of the PF private structure,
  9490. * and a hardware reset occur.
  9491. *
  9492. * Returns 0 on success, negative on failure
  9493. **/
  9494. static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  9495. {
  9496. struct i40e_aq_get_phy_abilities_resp abilities;
  9497. struct i40e_pf *pf;
  9498. struct i40e_hw *hw;
  9499. static u16 pfs_found;
  9500. u16 wol_nvm_bits;
  9501. u16 link_status;
  9502. int err;
  9503. u32 val;
  9504. u32 i;
  9505. u8 set_fc_aq_fail;
  9506. err = pci_enable_device_mem(pdev);
  9507. if (err)
  9508. return err;
  9509. /* set up for high or low dma */
  9510. err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
  9511. if (err) {
  9512. err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  9513. if (err) {
  9514. dev_err(&pdev->dev,
  9515. "DMA configuration failed: 0x%x\n", err);
  9516. goto err_dma;
  9517. }
  9518. }
  9519. /* set up pci connections */
  9520. err = pci_request_mem_regions(pdev, i40e_driver_name);
  9521. if (err) {
  9522. dev_info(&pdev->dev,
  9523. "pci_request_selected_regions failed %d\n", err);
  9524. goto err_pci_reg;
  9525. }
  9526. pci_enable_pcie_error_reporting(pdev);
  9527. pci_set_master(pdev);
  9528. /* Now that we have a PCI connection, we need to do the
  9529. * low level device setup. This is primarily setting up
  9530. * the Admin Queue structures and then querying for the
  9531. * device's current profile information.
  9532. */
  9533. pf = kzalloc(sizeof(*pf), GFP_KERNEL);
  9534. if (!pf) {
  9535. err = -ENOMEM;
  9536. goto err_pf_alloc;
  9537. }
  9538. pf->next_vsi = 0;
  9539. pf->pdev = pdev;
  9540. set_bit(__I40E_DOWN, &pf->state);
  9541. hw = &pf->hw;
  9542. hw->back = pf;
  9543. pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
  9544. I40E_MAX_CSR_SPACE);
  9545. hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
  9546. if (!hw->hw_addr) {
  9547. err = -EIO;
  9548. dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
  9549. (unsigned int)pci_resource_start(pdev, 0),
  9550. pf->ioremap_len, err);
  9551. goto err_ioremap;
  9552. }
  9553. hw->vendor_id = pdev->vendor;
  9554. hw->device_id = pdev->device;
  9555. pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
  9556. hw->subsystem_vendor_id = pdev->subsystem_vendor;
  9557. hw->subsystem_device_id = pdev->subsystem_device;
  9558. hw->bus.device = PCI_SLOT(pdev->devfn);
  9559. hw->bus.func = PCI_FUNC(pdev->devfn);
  9560. hw->bus.bus_id = pdev->bus->number;
  9561. pf->instance = pfs_found;
  9562. INIT_LIST_HEAD(&pf->l3_flex_pit_list);
  9563. INIT_LIST_HEAD(&pf->l4_flex_pit_list);
  9564. /* set up the locks for the AQ, do this only once in probe
  9565. * and destroy them only once in remove
  9566. */
  9567. mutex_init(&hw->aq.asq_mutex);
  9568. mutex_init(&hw->aq.arq_mutex);
  9569. pf->msg_enable = netif_msg_init(debug,
  9570. NETIF_MSG_DRV |
  9571. NETIF_MSG_PROBE |
  9572. NETIF_MSG_LINK);
  9573. if (debug < -1)
  9574. pf->hw.debug_mask = debug;
  9575. /* do a special CORER for clearing PXE mode once at init */
  9576. if (hw->revision_id == 0 &&
  9577. (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
  9578. wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
  9579. i40e_flush(hw);
  9580. msleep(200);
  9581. pf->corer_count++;
  9582. i40e_clear_pxe_mode(hw);
  9583. }
  9584. /* Reset here to make sure all is clean and to define PF 'n' */
  9585. i40e_clear_hw(hw);
  9586. err = i40e_pf_reset(hw);
  9587. if (err) {
  9588. dev_info(&pdev->dev, "Initial pf_reset failed: %d\n", err);
  9589. goto err_pf_reset;
  9590. }
  9591. pf->pfr_count++;
  9592. hw->aq.num_arq_entries = I40E_AQ_LEN;
  9593. hw->aq.num_asq_entries = I40E_AQ_LEN;
  9594. hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
  9595. hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
  9596. pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
  9597. snprintf(pf->int_name, sizeof(pf->int_name) - 1,
  9598. "%s-%s:misc",
  9599. dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
  9600. err = i40e_init_shared_code(hw);
  9601. if (err) {
  9602. dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
  9603. err);
  9604. goto err_pf_reset;
  9605. }
  9606. /* set up a default setting for link flow control */
  9607. pf->hw.fc.requested_mode = I40E_FC_NONE;
  9608. err = i40e_init_adminq(hw);
  9609. if (err) {
  9610. if (err == I40E_ERR_FIRMWARE_API_VERSION)
  9611. dev_info(&pdev->dev,
  9612. "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");
  9613. else
  9614. dev_info(&pdev->dev,
  9615. "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
  9616. goto err_pf_reset;
  9617. }
  9618. /* provide nvm, fw, api versions */
  9619. dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s\n",
  9620. hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
  9621. hw->aq.api_maj_ver, hw->aq.api_min_ver,
  9622. i40e_nvm_version_str(hw));
  9623. if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
  9624. hw->aq.api_min_ver > I40E_FW_API_VERSION_MINOR)
  9625. dev_info(&pdev->dev,
  9626. "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");
  9627. else if (hw->aq.api_maj_ver < I40E_FW_API_VERSION_MAJOR ||
  9628. hw->aq.api_min_ver < (I40E_FW_API_VERSION_MINOR - 1))
  9629. dev_info(&pdev->dev,
  9630. "The driver for the device detected an older version of the NVM image than expected. Please update the NVM image.\n");
  9631. i40e_verify_eeprom(pf);
  9632. /* Rev 0 hardware was never productized */
  9633. if (hw->revision_id < 1)
  9634. 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");
  9635. i40e_clear_pxe_mode(hw);
  9636. err = i40e_get_capabilities(pf);
  9637. if (err)
  9638. goto err_adminq_setup;
  9639. err = i40e_sw_init(pf);
  9640. if (err) {
  9641. dev_info(&pdev->dev, "sw_init failed: %d\n", err);
  9642. goto err_sw_init;
  9643. }
  9644. err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
  9645. hw->func_caps.num_rx_qp, 0, 0);
  9646. if (err) {
  9647. dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
  9648. goto err_init_lan_hmc;
  9649. }
  9650. err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
  9651. if (err) {
  9652. dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
  9653. err = -ENOENT;
  9654. goto err_configure_lan_hmc;
  9655. }
  9656. /* Disable LLDP for NICs that have firmware versions lower than v4.3.
  9657. * Ignore error return codes because if it was already disabled via
  9658. * hardware settings this will fail
  9659. */
  9660. if (pf->flags & I40E_FLAG_STOP_FW_LLDP) {
  9661. dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
  9662. i40e_aq_stop_lldp(hw, true, NULL);
  9663. }
  9664. i40e_get_mac_addr(hw, hw->mac.addr);
  9665. /* allow a platform config to override the HW addr */
  9666. i40e_get_platform_mac_addr(pdev, pf);
  9667. if (!is_valid_ether_addr(hw->mac.addr)) {
  9668. dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
  9669. err = -EIO;
  9670. goto err_mac_addr;
  9671. }
  9672. dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
  9673. ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
  9674. i40e_get_port_mac_addr(hw, hw->mac.port_addr);
  9675. if (is_valid_ether_addr(hw->mac.port_addr))
  9676. pf->flags |= I40E_FLAG_PORT_ID_VALID;
  9677. pci_set_drvdata(pdev, pf);
  9678. pci_save_state(pdev);
  9679. #ifdef CONFIG_I40E_DCB
  9680. err = i40e_init_pf_dcb(pf);
  9681. if (err) {
  9682. dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
  9683. pf->flags &= ~(I40E_FLAG_DCB_CAPABLE | I40E_FLAG_DCB_ENABLED);
  9684. /* Continue without DCB enabled */
  9685. }
  9686. #endif /* CONFIG_I40E_DCB */
  9687. /* set up periodic task facility */
  9688. setup_timer(&pf->service_timer, i40e_service_timer, (unsigned long)pf);
  9689. pf->service_timer_period = HZ;
  9690. INIT_WORK(&pf->service_task, i40e_service_task);
  9691. clear_bit(__I40E_SERVICE_SCHED, &pf->state);
  9692. pf->flags |= I40E_FLAG_NEED_LINK_UPDATE;
  9693. /* NVM bit on means WoL disabled for the port */
  9694. i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
  9695. if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
  9696. pf->wol_en = false;
  9697. else
  9698. pf->wol_en = true;
  9699. device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
  9700. /* set up the main switch operations */
  9701. i40e_determine_queue_usage(pf);
  9702. err = i40e_init_interrupt_scheme(pf);
  9703. if (err)
  9704. goto err_switch_setup;
  9705. /* The number of VSIs reported by the FW is the minimum guaranteed
  9706. * to us; HW supports far more and we share the remaining pool with
  9707. * the other PFs. We allocate space for more than the guarantee with
  9708. * the understanding that we might not get them all later.
  9709. */
  9710. if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
  9711. pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
  9712. else
  9713. pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
  9714. /* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
  9715. pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
  9716. GFP_KERNEL);
  9717. if (!pf->vsi) {
  9718. err = -ENOMEM;
  9719. goto err_switch_setup;
  9720. }
  9721. #ifdef CONFIG_PCI_IOV
  9722. /* prep for VF support */
  9723. if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
  9724. (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
  9725. !test_bit(__I40E_BAD_EEPROM, &pf->state)) {
  9726. if (pci_num_vf(pdev))
  9727. pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
  9728. }
  9729. #endif
  9730. err = i40e_setup_pf_switch(pf, false);
  9731. if (err) {
  9732. dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
  9733. goto err_vsis;
  9734. }
  9735. /* Make sure flow control is set according to current settings */
  9736. err = i40e_set_fc(hw, &set_fc_aq_fail, true);
  9737. if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_GET)
  9738. dev_dbg(&pf->pdev->dev,
  9739. "Set fc with err %s aq_err %s on get_phy_cap\n",
  9740. i40e_stat_str(hw, err),
  9741. i40e_aq_str(hw, hw->aq.asq_last_status));
  9742. if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_SET)
  9743. dev_dbg(&pf->pdev->dev,
  9744. "Set fc with err %s aq_err %s on set_phy_config\n",
  9745. i40e_stat_str(hw, err),
  9746. i40e_aq_str(hw, hw->aq.asq_last_status));
  9747. if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_UPDATE)
  9748. dev_dbg(&pf->pdev->dev,
  9749. "Set fc with err %s aq_err %s on get_link_info\n",
  9750. i40e_stat_str(hw, err),
  9751. i40e_aq_str(hw, hw->aq.asq_last_status));
  9752. /* if FDIR VSI was set up, start it now */
  9753. for (i = 0; i < pf->num_alloc_vsi; i++) {
  9754. if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
  9755. i40e_vsi_open(pf->vsi[i]);
  9756. break;
  9757. }
  9758. }
  9759. /* The driver only wants link up/down and module qualification
  9760. * reports from firmware. Note the negative logic.
  9761. */
  9762. err = i40e_aq_set_phy_int_mask(&pf->hw,
  9763. ~(I40E_AQ_EVENT_LINK_UPDOWN |
  9764. I40E_AQ_EVENT_MEDIA_NA |
  9765. I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
  9766. if (err)
  9767. dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
  9768. i40e_stat_str(&pf->hw, err),
  9769. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  9770. /* Reconfigure hardware for allowing smaller MSS in the case
  9771. * of TSO, so that we avoid the MDD being fired and causing
  9772. * a reset in the case of small MSS+TSO.
  9773. */
  9774. val = rd32(hw, I40E_REG_MSS);
  9775. if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
  9776. val &= ~I40E_REG_MSS_MIN_MASK;
  9777. val |= I40E_64BYTE_MSS;
  9778. wr32(hw, I40E_REG_MSS, val);
  9779. }
  9780. if (pf->flags & I40E_FLAG_RESTART_AUTONEG) {
  9781. msleep(75);
  9782. err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
  9783. if (err)
  9784. dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
  9785. i40e_stat_str(&pf->hw, err),
  9786. i40e_aq_str(&pf->hw,
  9787. pf->hw.aq.asq_last_status));
  9788. }
  9789. /* The main driver is (mostly) up and happy. We need to set this state
  9790. * before setting up the misc vector or we get a race and the vector
  9791. * ends up disabled forever.
  9792. */
  9793. clear_bit(__I40E_DOWN, &pf->state);
  9794. /* In case of MSIX we are going to setup the misc vector right here
  9795. * to handle admin queue events etc. In case of legacy and MSI
  9796. * the misc functionality and queue processing is combined in
  9797. * the same vector and that gets setup at open.
  9798. */
  9799. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  9800. err = i40e_setup_misc_vector(pf);
  9801. if (err) {
  9802. dev_info(&pdev->dev,
  9803. "setup of misc vector failed: %d\n", err);
  9804. goto err_vsis;
  9805. }
  9806. }
  9807. #ifdef CONFIG_PCI_IOV
  9808. /* prep for VF support */
  9809. if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
  9810. (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
  9811. !test_bit(__I40E_BAD_EEPROM, &pf->state)) {
  9812. /* disable link interrupts for VFs */
  9813. val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
  9814. val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
  9815. wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
  9816. i40e_flush(hw);
  9817. if (pci_num_vf(pdev)) {
  9818. dev_info(&pdev->dev,
  9819. "Active VFs found, allocating resources.\n");
  9820. err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
  9821. if (err)
  9822. dev_info(&pdev->dev,
  9823. "Error %d allocating resources for existing VFs\n",
  9824. err);
  9825. }
  9826. }
  9827. #endif /* CONFIG_PCI_IOV */
  9828. if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
  9829. pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
  9830. pf->num_iwarp_msix,
  9831. I40E_IWARP_IRQ_PILE_ID);
  9832. if (pf->iwarp_base_vector < 0) {
  9833. dev_info(&pdev->dev,
  9834. "failed to get tracking for %d vectors for IWARP err=%d\n",
  9835. pf->num_iwarp_msix, pf->iwarp_base_vector);
  9836. pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
  9837. }
  9838. }
  9839. i40e_dbg_pf_init(pf);
  9840. /* tell the firmware that we're starting */
  9841. i40e_send_version(pf);
  9842. /* since everything's happy, start the service_task timer */
  9843. mod_timer(&pf->service_timer,
  9844. round_jiffies(jiffies + pf->service_timer_period));
  9845. /* add this PF to client device list and launch a client service task */
  9846. err = i40e_lan_add_device(pf);
  9847. if (err)
  9848. dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
  9849. err);
  9850. #define PCI_SPEED_SIZE 8
  9851. #define PCI_WIDTH_SIZE 8
  9852. /* Devices on the IOSF bus do not have this information
  9853. * and will report PCI Gen 1 x 1 by default so don't bother
  9854. * checking them.
  9855. */
  9856. if (!(pf->flags & I40E_FLAG_NO_PCI_LINK_CHECK)) {
  9857. char speed[PCI_SPEED_SIZE] = "Unknown";
  9858. char width[PCI_WIDTH_SIZE] = "Unknown";
  9859. /* Get the negotiated link width and speed from PCI config
  9860. * space
  9861. */
  9862. pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
  9863. &link_status);
  9864. i40e_set_pci_config_data(hw, link_status);
  9865. switch (hw->bus.speed) {
  9866. case i40e_bus_speed_8000:
  9867. strncpy(speed, "8.0", PCI_SPEED_SIZE); break;
  9868. case i40e_bus_speed_5000:
  9869. strncpy(speed, "5.0", PCI_SPEED_SIZE); break;
  9870. case i40e_bus_speed_2500:
  9871. strncpy(speed, "2.5", PCI_SPEED_SIZE); break;
  9872. default:
  9873. break;
  9874. }
  9875. switch (hw->bus.width) {
  9876. case i40e_bus_width_pcie_x8:
  9877. strncpy(width, "8", PCI_WIDTH_SIZE); break;
  9878. case i40e_bus_width_pcie_x4:
  9879. strncpy(width, "4", PCI_WIDTH_SIZE); break;
  9880. case i40e_bus_width_pcie_x2:
  9881. strncpy(width, "2", PCI_WIDTH_SIZE); break;
  9882. case i40e_bus_width_pcie_x1:
  9883. strncpy(width, "1", PCI_WIDTH_SIZE); break;
  9884. default:
  9885. break;
  9886. }
  9887. dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
  9888. speed, width);
  9889. if (hw->bus.width < i40e_bus_width_pcie_x8 ||
  9890. hw->bus.speed < i40e_bus_speed_8000) {
  9891. dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
  9892. dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
  9893. }
  9894. }
  9895. /* get the requested speeds from the fw */
  9896. err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
  9897. if (err)
  9898. dev_dbg(&pf->pdev->dev, "get requested speeds ret = %s last_status = %s\n",
  9899. i40e_stat_str(&pf->hw, err),
  9900. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  9901. pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
  9902. /* get the supported phy types from the fw */
  9903. err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
  9904. if (err)
  9905. dev_dbg(&pf->pdev->dev, "get supported phy types ret = %s last_status = %s\n",
  9906. i40e_stat_str(&pf->hw, err),
  9907. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  9908. /* Add a filter to drop all Flow control frames from any VSI from being
  9909. * transmitted. By doing so we stop a malicious VF from sending out
  9910. * PAUSE or PFC frames and potentially controlling traffic for other
  9911. * PF/VF VSIs.
  9912. * The FW can still send Flow control frames if enabled.
  9913. */
  9914. i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
  9915. pf->main_vsi_seid);
  9916. if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
  9917. (pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
  9918. pf->flags |= I40E_FLAG_PHY_CONTROLS_LEDS;
  9919. if (pf->hw.device_id == I40E_DEV_ID_SFP_I_X722)
  9920. pf->flags |= I40E_FLAG_HAVE_CRT_RETIMER;
  9921. /* print a string summarizing features */
  9922. i40e_print_features(pf);
  9923. return 0;
  9924. /* Unwind what we've done if something failed in the setup */
  9925. err_vsis:
  9926. set_bit(__I40E_DOWN, &pf->state);
  9927. i40e_clear_interrupt_scheme(pf);
  9928. kfree(pf->vsi);
  9929. err_switch_setup:
  9930. i40e_reset_interrupt_capability(pf);
  9931. del_timer_sync(&pf->service_timer);
  9932. err_mac_addr:
  9933. err_configure_lan_hmc:
  9934. (void)i40e_shutdown_lan_hmc(hw);
  9935. err_init_lan_hmc:
  9936. kfree(pf->qp_pile);
  9937. err_sw_init:
  9938. err_adminq_setup:
  9939. err_pf_reset:
  9940. iounmap(hw->hw_addr);
  9941. err_ioremap:
  9942. kfree(pf);
  9943. err_pf_alloc:
  9944. pci_disable_pcie_error_reporting(pdev);
  9945. pci_release_mem_regions(pdev);
  9946. err_pci_reg:
  9947. err_dma:
  9948. pci_disable_device(pdev);
  9949. return err;
  9950. }
  9951. /**
  9952. * i40e_remove - Device removal routine
  9953. * @pdev: PCI device information struct
  9954. *
  9955. * i40e_remove is called by the PCI subsystem to alert the driver
  9956. * that is should release a PCI device. This could be caused by a
  9957. * Hot-Plug event, or because the driver is going to be removed from
  9958. * memory.
  9959. **/
  9960. static void i40e_remove(struct pci_dev *pdev)
  9961. {
  9962. struct i40e_pf *pf = pci_get_drvdata(pdev);
  9963. struct i40e_hw *hw = &pf->hw;
  9964. i40e_status ret_code;
  9965. int i;
  9966. i40e_dbg_pf_exit(pf);
  9967. i40e_ptp_stop(pf);
  9968. /* Disable RSS in hw */
  9969. i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
  9970. i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
  9971. /* no more scheduling of any task */
  9972. set_bit(__I40E_SUSPENDED, &pf->state);
  9973. set_bit(__I40E_DOWN, &pf->state);
  9974. if (pf->service_timer.data)
  9975. del_timer_sync(&pf->service_timer);
  9976. if (pf->service_task.func)
  9977. cancel_work_sync(&pf->service_task);
  9978. if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
  9979. i40e_free_vfs(pf);
  9980. pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
  9981. }
  9982. i40e_fdir_teardown(pf);
  9983. /* If there is a switch structure or any orphans, remove them.
  9984. * This will leave only the PF's VSI remaining.
  9985. */
  9986. for (i = 0; i < I40E_MAX_VEB; i++) {
  9987. if (!pf->veb[i])
  9988. continue;
  9989. if (pf->veb[i]->uplink_seid == pf->mac_seid ||
  9990. pf->veb[i]->uplink_seid == 0)
  9991. i40e_switch_branch_release(pf->veb[i]);
  9992. }
  9993. /* Now we can shutdown the PF's VSI, just before we kill
  9994. * adminq and hmc.
  9995. */
  9996. if (pf->vsi[pf->lan_vsi])
  9997. i40e_vsi_release(pf->vsi[pf->lan_vsi]);
  9998. /* remove attached clients */
  9999. ret_code = i40e_lan_del_device(pf);
  10000. if (ret_code) {
  10001. dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
  10002. ret_code);
  10003. }
  10004. /* shutdown and destroy the HMC */
  10005. if (hw->hmc.hmc_obj) {
  10006. ret_code = i40e_shutdown_lan_hmc(hw);
  10007. if (ret_code)
  10008. dev_warn(&pdev->dev,
  10009. "Failed to destroy the HMC resources: %d\n",
  10010. ret_code);
  10011. }
  10012. /* shutdown the adminq */
  10013. i40e_shutdown_adminq(hw);
  10014. /* destroy the locks only once, here */
  10015. mutex_destroy(&hw->aq.arq_mutex);
  10016. mutex_destroy(&hw->aq.asq_mutex);
  10017. /* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
  10018. i40e_clear_interrupt_scheme(pf);
  10019. for (i = 0; i < pf->num_alloc_vsi; i++) {
  10020. if (pf->vsi[i]) {
  10021. i40e_vsi_clear_rings(pf->vsi[i]);
  10022. i40e_vsi_clear(pf->vsi[i]);
  10023. pf->vsi[i] = NULL;
  10024. }
  10025. }
  10026. for (i = 0; i < I40E_MAX_VEB; i++) {
  10027. kfree(pf->veb[i]);
  10028. pf->veb[i] = NULL;
  10029. }
  10030. kfree(pf->qp_pile);
  10031. kfree(pf->vsi);
  10032. iounmap(hw->hw_addr);
  10033. kfree(pf);
  10034. pci_release_mem_regions(pdev);
  10035. pci_disable_pcie_error_reporting(pdev);
  10036. pci_disable_device(pdev);
  10037. }
  10038. /**
  10039. * i40e_pci_error_detected - warning that something funky happened in PCI land
  10040. * @pdev: PCI device information struct
  10041. *
  10042. * Called to warn that something happened and the error handling steps
  10043. * are in progress. Allows the driver to quiesce things, be ready for
  10044. * remediation.
  10045. **/
  10046. static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
  10047. enum pci_channel_state error)
  10048. {
  10049. struct i40e_pf *pf = pci_get_drvdata(pdev);
  10050. dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
  10051. if (!pf) {
  10052. dev_info(&pdev->dev,
  10053. "Cannot recover - error happened during device probe\n");
  10054. return PCI_ERS_RESULT_DISCONNECT;
  10055. }
  10056. /* shutdown all operations */
  10057. if (!test_bit(__I40E_SUSPENDED, &pf->state)) {
  10058. rtnl_lock();
  10059. i40e_prep_for_reset(pf);
  10060. rtnl_unlock();
  10061. }
  10062. /* Request a slot reset */
  10063. return PCI_ERS_RESULT_NEED_RESET;
  10064. }
  10065. /**
  10066. * i40e_pci_error_slot_reset - a PCI slot reset just happened
  10067. * @pdev: PCI device information struct
  10068. *
  10069. * Called to find if the driver can work with the device now that
  10070. * the pci slot has been reset. If a basic connection seems good
  10071. * (registers are readable and have sane content) then return a
  10072. * happy little PCI_ERS_RESULT_xxx.
  10073. **/
  10074. static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
  10075. {
  10076. struct i40e_pf *pf = pci_get_drvdata(pdev);
  10077. pci_ers_result_t result;
  10078. int err;
  10079. u32 reg;
  10080. dev_dbg(&pdev->dev, "%s\n", __func__);
  10081. if (pci_enable_device_mem(pdev)) {
  10082. dev_info(&pdev->dev,
  10083. "Cannot re-enable PCI device after reset.\n");
  10084. result = PCI_ERS_RESULT_DISCONNECT;
  10085. } else {
  10086. pci_set_master(pdev);
  10087. pci_restore_state(pdev);
  10088. pci_save_state(pdev);
  10089. pci_wake_from_d3(pdev, false);
  10090. reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
  10091. if (reg == 0)
  10092. result = PCI_ERS_RESULT_RECOVERED;
  10093. else
  10094. result = PCI_ERS_RESULT_DISCONNECT;
  10095. }
  10096. err = pci_cleanup_aer_uncorrect_error_status(pdev);
  10097. if (err) {
  10098. dev_info(&pdev->dev,
  10099. "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
  10100. err);
  10101. /* non-fatal, continue */
  10102. }
  10103. return result;
  10104. }
  10105. /**
  10106. * i40e_pci_error_resume - restart operations after PCI error recovery
  10107. * @pdev: PCI device information struct
  10108. *
  10109. * Called to allow the driver to bring things back up after PCI error
  10110. * and/or reset recovery has finished.
  10111. **/
  10112. static void i40e_pci_error_resume(struct pci_dev *pdev)
  10113. {
  10114. struct i40e_pf *pf = pci_get_drvdata(pdev);
  10115. dev_dbg(&pdev->dev, "%s\n", __func__);
  10116. if (test_bit(__I40E_SUSPENDED, &pf->state))
  10117. return;
  10118. rtnl_lock();
  10119. i40e_handle_reset_warning(pf);
  10120. rtnl_unlock();
  10121. }
  10122. /**
  10123. * i40e_enable_mc_magic_wake - enable multicast magic packet wake up
  10124. * using the mac_address_write admin q function
  10125. * @pf: pointer to i40e_pf struct
  10126. **/
  10127. static void i40e_enable_mc_magic_wake(struct i40e_pf *pf)
  10128. {
  10129. struct i40e_hw *hw = &pf->hw;
  10130. i40e_status ret;
  10131. u8 mac_addr[6];
  10132. u16 flags = 0;
  10133. /* Get current MAC address in case it's an LAA */
  10134. if (pf->vsi[pf->lan_vsi] && pf->vsi[pf->lan_vsi]->netdev) {
  10135. ether_addr_copy(mac_addr,
  10136. pf->vsi[pf->lan_vsi]->netdev->dev_addr);
  10137. } else {
  10138. dev_err(&pf->pdev->dev,
  10139. "Failed to retrieve MAC address; using default\n");
  10140. ether_addr_copy(mac_addr, hw->mac.addr);
  10141. }
  10142. /* The FW expects the mac address write cmd to first be called with
  10143. * one of these flags before calling it again with the multicast
  10144. * enable flags.
  10145. */
  10146. flags = I40E_AQC_WRITE_TYPE_LAA_WOL;
  10147. if (hw->func_caps.flex10_enable && hw->partition_id != 1)
  10148. flags = I40E_AQC_WRITE_TYPE_LAA_ONLY;
  10149. ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
  10150. if (ret) {
  10151. dev_err(&pf->pdev->dev,
  10152. "Failed to update MAC address registers; cannot enable Multicast Magic packet wake up");
  10153. return;
  10154. }
  10155. flags = I40E_AQC_MC_MAG_EN
  10156. | I40E_AQC_WOL_PRESERVE_ON_PFR
  10157. | I40E_AQC_WRITE_TYPE_UPDATE_MC_MAG;
  10158. ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
  10159. if (ret)
  10160. dev_err(&pf->pdev->dev,
  10161. "Failed to enable Multicast Magic Packet wake up\n");
  10162. }
  10163. /**
  10164. * i40e_shutdown - PCI callback for shutting down
  10165. * @pdev: PCI device information struct
  10166. **/
  10167. static void i40e_shutdown(struct pci_dev *pdev)
  10168. {
  10169. struct i40e_pf *pf = pci_get_drvdata(pdev);
  10170. struct i40e_hw *hw = &pf->hw;
  10171. set_bit(__I40E_SUSPENDED, &pf->state);
  10172. set_bit(__I40E_DOWN, &pf->state);
  10173. rtnl_lock();
  10174. i40e_prep_for_reset(pf);
  10175. rtnl_unlock();
  10176. wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
  10177. wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
  10178. del_timer_sync(&pf->service_timer);
  10179. cancel_work_sync(&pf->service_task);
  10180. i40e_fdir_teardown(pf);
  10181. if (pf->wol_en && (pf->flags & I40E_FLAG_WOL_MC_MAGIC_PKT_WAKE))
  10182. i40e_enable_mc_magic_wake(pf);
  10183. rtnl_lock();
  10184. i40e_prep_for_reset(pf);
  10185. rtnl_unlock();
  10186. wr32(hw, I40E_PFPM_APM,
  10187. (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
  10188. wr32(hw, I40E_PFPM_WUFC,
  10189. (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
  10190. i40e_clear_interrupt_scheme(pf);
  10191. if (system_state == SYSTEM_POWER_OFF) {
  10192. pci_wake_from_d3(pdev, pf->wol_en);
  10193. pci_set_power_state(pdev, PCI_D3hot);
  10194. }
  10195. }
  10196. #ifdef CONFIG_PM
  10197. /**
  10198. * i40e_suspend - PCI callback for moving to D3
  10199. * @pdev: PCI device information struct
  10200. **/
  10201. static int i40e_suspend(struct pci_dev *pdev, pm_message_t state)
  10202. {
  10203. struct i40e_pf *pf = pci_get_drvdata(pdev);
  10204. struct i40e_hw *hw = &pf->hw;
  10205. int retval = 0;
  10206. set_bit(__I40E_SUSPENDED, &pf->state);
  10207. set_bit(__I40E_DOWN, &pf->state);
  10208. if (pf->wol_en && (pf->flags & I40E_FLAG_WOL_MC_MAGIC_PKT_WAKE))
  10209. i40e_enable_mc_magic_wake(pf);
  10210. rtnl_lock();
  10211. i40e_prep_for_reset(pf);
  10212. rtnl_unlock();
  10213. wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
  10214. wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
  10215. i40e_stop_misc_vector(pf);
  10216. retval = pci_save_state(pdev);
  10217. if (retval)
  10218. return retval;
  10219. pci_wake_from_d3(pdev, pf->wol_en);
  10220. pci_set_power_state(pdev, PCI_D3hot);
  10221. return retval;
  10222. }
  10223. /**
  10224. * i40e_resume - PCI callback for waking up from D3
  10225. * @pdev: PCI device information struct
  10226. **/
  10227. static int i40e_resume(struct pci_dev *pdev)
  10228. {
  10229. struct i40e_pf *pf = pci_get_drvdata(pdev);
  10230. u32 err;
  10231. pci_set_power_state(pdev, PCI_D0);
  10232. pci_restore_state(pdev);
  10233. /* pci_restore_state() clears dev->state_saves, so
  10234. * call pci_save_state() again to restore it.
  10235. */
  10236. pci_save_state(pdev);
  10237. err = pci_enable_device_mem(pdev);
  10238. if (err) {
  10239. dev_err(&pdev->dev, "Cannot enable PCI device from suspend\n");
  10240. return err;
  10241. }
  10242. pci_set_master(pdev);
  10243. /* no wakeup events while running */
  10244. pci_wake_from_d3(pdev, false);
  10245. /* handling the reset will rebuild the device state */
  10246. if (test_and_clear_bit(__I40E_SUSPENDED, &pf->state)) {
  10247. clear_bit(__I40E_DOWN, &pf->state);
  10248. rtnl_lock();
  10249. i40e_reset_and_rebuild(pf, false);
  10250. rtnl_unlock();
  10251. }
  10252. return 0;
  10253. }
  10254. #endif
  10255. static const struct pci_error_handlers i40e_err_handler = {
  10256. .error_detected = i40e_pci_error_detected,
  10257. .slot_reset = i40e_pci_error_slot_reset,
  10258. .resume = i40e_pci_error_resume,
  10259. };
  10260. static struct pci_driver i40e_driver = {
  10261. .name = i40e_driver_name,
  10262. .id_table = i40e_pci_tbl,
  10263. .probe = i40e_probe,
  10264. .remove = i40e_remove,
  10265. #ifdef CONFIG_PM
  10266. .suspend = i40e_suspend,
  10267. .resume = i40e_resume,
  10268. #endif
  10269. .shutdown = i40e_shutdown,
  10270. .err_handler = &i40e_err_handler,
  10271. .sriov_configure = i40e_pci_sriov_configure,
  10272. };
  10273. /**
  10274. * i40e_init_module - Driver registration routine
  10275. *
  10276. * i40e_init_module is the first routine called when the driver is
  10277. * loaded. All it does is register with the PCI subsystem.
  10278. **/
  10279. static int __init i40e_init_module(void)
  10280. {
  10281. pr_info("%s: %s - version %s\n", i40e_driver_name,
  10282. i40e_driver_string, i40e_driver_version_str);
  10283. pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
  10284. /* we will see if single thread per module is enough for now,
  10285. * it can't be any worse than using the system workqueue which
  10286. * was already single threaded
  10287. */
  10288. i40e_wq = alloc_workqueue("%s", WQ_UNBOUND | WQ_MEM_RECLAIM, 1,
  10289. i40e_driver_name);
  10290. if (!i40e_wq) {
  10291. pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
  10292. return -ENOMEM;
  10293. }
  10294. i40e_dbg_init();
  10295. return pci_register_driver(&i40e_driver);
  10296. }
  10297. module_init(i40e_init_module);
  10298. /**
  10299. * i40e_exit_module - Driver exit cleanup routine
  10300. *
  10301. * i40e_exit_module is called just before the driver is removed
  10302. * from memory.
  10303. **/
  10304. static void __exit i40e_exit_module(void)
  10305. {
  10306. pci_unregister_driver(&i40e_driver);
  10307. destroy_workqueue(i40e_wq);
  10308. i40e_dbg_exit();
  10309. }
  10310. module_exit(i40e_exit_module);