i40e_main.c 327 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823
  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, bool lock_acquired);
  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_prep_for_reset(struct i40e_pf *pf, bool lock_acquired);
  55. static int i40e_reset(struct i40e_pf *pf);
  56. static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired);
  57. static void i40e_fdir_sb_setup(struct i40e_pf *pf);
  58. static int i40e_veb_get_bw_info(struct i40e_veb *veb);
  59. /* i40e_pci_tbl - PCI Device ID Table
  60. *
  61. * Last entry must be all 0s
  62. *
  63. * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
  64. * Class, Class Mask, private data (not used) }
  65. */
  66. static const struct pci_device_id i40e_pci_tbl[] = {
  67. {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_XL710), 0},
  68. {PCI_VDEVICE(INTEL, I40E_DEV_ID_QEMU), 0},
  69. {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_B), 0},
  70. {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_C), 0},
  71. {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_A), 0},
  72. {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_B), 0},
  73. {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_C), 0},
  74. {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T), 0},
  75. {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T4), 0},
  76. {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_X722), 0},
  77. {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_X722), 0},
  78. {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_X722), 0},
  79. {PCI_VDEVICE(INTEL, I40E_DEV_ID_1G_BASE_T_X722), 0},
  80. {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_X722), 0},
  81. {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_I_X722), 0},
  82. {PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2), 0},
  83. {PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2_A), 0},
  84. {PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_B), 0},
  85. {PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_SFP28), 0},
  86. /* required last entry */
  87. {0, }
  88. };
  89. MODULE_DEVICE_TABLE(pci, i40e_pci_tbl);
  90. #define I40E_MAX_VF_COUNT 128
  91. static int debug = -1;
  92. module_param(debug, uint, 0);
  93. MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all), Debug mask (0x8XXXXXXX)");
  94. MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
  95. MODULE_DESCRIPTION("Intel(R) Ethernet Connection XL710 Network Driver");
  96. MODULE_LICENSE("GPL");
  97. MODULE_VERSION(DRV_VERSION);
  98. static struct workqueue_struct *i40e_wq;
  99. /**
  100. * i40e_allocate_dma_mem_d - OS specific memory alloc for shared code
  101. * @hw: pointer to the HW structure
  102. * @mem: ptr to mem struct to fill out
  103. * @size: size of memory requested
  104. * @alignment: what to align the allocation to
  105. **/
  106. int i40e_allocate_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem,
  107. u64 size, u32 alignment)
  108. {
  109. struct i40e_pf *pf = (struct i40e_pf *)hw->back;
  110. mem->size = ALIGN(size, alignment);
  111. mem->va = dma_zalloc_coherent(&pf->pdev->dev, mem->size,
  112. &mem->pa, GFP_KERNEL);
  113. if (!mem->va)
  114. return -ENOMEM;
  115. return 0;
  116. }
  117. /**
  118. * i40e_free_dma_mem_d - OS specific memory free for shared code
  119. * @hw: pointer to the HW structure
  120. * @mem: ptr to mem struct to free
  121. **/
  122. int i40e_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
  123. {
  124. struct i40e_pf *pf = (struct i40e_pf *)hw->back;
  125. dma_free_coherent(&pf->pdev->dev, mem->size, mem->va, mem->pa);
  126. mem->va = NULL;
  127. mem->pa = 0;
  128. mem->size = 0;
  129. return 0;
  130. }
  131. /**
  132. * i40e_allocate_virt_mem_d - OS specific memory alloc for shared code
  133. * @hw: pointer to the HW structure
  134. * @mem: ptr to mem struct to fill out
  135. * @size: size of memory requested
  136. **/
  137. int i40e_allocate_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem,
  138. u32 size)
  139. {
  140. mem->size = size;
  141. mem->va = kzalloc(size, GFP_KERNEL);
  142. if (!mem->va)
  143. return -ENOMEM;
  144. return 0;
  145. }
  146. /**
  147. * i40e_free_virt_mem_d - OS specific memory free for shared code
  148. * @hw: pointer to the HW structure
  149. * @mem: ptr to mem struct to free
  150. **/
  151. int i40e_free_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem)
  152. {
  153. /* it's ok to kfree a NULL pointer */
  154. kfree(mem->va);
  155. mem->va = NULL;
  156. mem->size = 0;
  157. return 0;
  158. }
  159. /**
  160. * i40e_get_lump - find a lump of free generic resource
  161. * @pf: board private structure
  162. * @pile: the pile of resource to search
  163. * @needed: the number of items needed
  164. * @id: an owner id to stick on the items assigned
  165. *
  166. * Returns the base item index of the lump, or negative for error
  167. *
  168. * The search_hint trick and lack of advanced fit-finding only work
  169. * because we're highly likely to have all the same size lump requests.
  170. * Linear search time and any fragmentation should be minimal.
  171. **/
  172. static int i40e_get_lump(struct i40e_pf *pf, struct i40e_lump_tracking *pile,
  173. u16 needed, u16 id)
  174. {
  175. int ret = -ENOMEM;
  176. int i, j;
  177. if (!pile || needed == 0 || id >= I40E_PILE_VALID_BIT) {
  178. dev_info(&pf->pdev->dev,
  179. "param err: pile=%p needed=%d id=0x%04x\n",
  180. pile, needed, id);
  181. return -EINVAL;
  182. }
  183. /* start the linear search with an imperfect hint */
  184. i = pile->search_hint;
  185. while (i < pile->num_entries) {
  186. /* skip already allocated entries */
  187. if (pile->list[i] & I40E_PILE_VALID_BIT) {
  188. i++;
  189. continue;
  190. }
  191. /* do we have enough in this lump? */
  192. for (j = 0; (j < needed) && ((i+j) < pile->num_entries); j++) {
  193. if (pile->list[i+j] & I40E_PILE_VALID_BIT)
  194. break;
  195. }
  196. if (j == needed) {
  197. /* there was enough, so assign it to the requestor */
  198. for (j = 0; j < needed; j++)
  199. pile->list[i+j] = id | I40E_PILE_VALID_BIT;
  200. ret = i;
  201. pile->search_hint = i + j;
  202. break;
  203. }
  204. /* not enough, so skip over it and continue looking */
  205. i += j;
  206. }
  207. return ret;
  208. }
  209. /**
  210. * i40e_put_lump - return a lump of generic resource
  211. * @pile: the pile of resource to search
  212. * @index: the base item index
  213. * @id: the owner id of the items assigned
  214. *
  215. * Returns the count of items in the lump
  216. **/
  217. static int i40e_put_lump(struct i40e_lump_tracking *pile, u16 index, u16 id)
  218. {
  219. int valid_id = (id | I40E_PILE_VALID_BIT);
  220. int count = 0;
  221. int i;
  222. if (!pile || index >= pile->num_entries)
  223. return -EINVAL;
  224. for (i = index;
  225. i < pile->num_entries && pile->list[i] == valid_id;
  226. i++) {
  227. pile->list[i] = 0;
  228. count++;
  229. }
  230. if (count && index < pile->search_hint)
  231. pile->search_hint = index;
  232. return count;
  233. }
  234. /**
  235. * i40e_find_vsi_from_id - searches for the vsi with the given id
  236. * @pf - the pf structure to search for the vsi
  237. * @id - id of the vsi it is searching for
  238. **/
  239. struct i40e_vsi *i40e_find_vsi_from_id(struct i40e_pf *pf, u16 id)
  240. {
  241. int i;
  242. for (i = 0; i < pf->num_alloc_vsi; i++)
  243. if (pf->vsi[i] && (pf->vsi[i]->id == id))
  244. return pf->vsi[i];
  245. return NULL;
  246. }
  247. /**
  248. * i40e_service_event_schedule - Schedule the service task to wake up
  249. * @pf: board private structure
  250. *
  251. * If not already scheduled, this puts the task into the work queue
  252. **/
  253. void i40e_service_event_schedule(struct i40e_pf *pf)
  254. {
  255. if (!test_bit(__I40E_DOWN, &pf->state) &&
  256. !test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
  257. queue_work(i40e_wq, &pf->service_task);
  258. }
  259. /**
  260. * i40e_tx_timeout - Respond to a Tx Hang
  261. * @netdev: network interface device structure
  262. *
  263. * If any port has noticed a Tx timeout, it is likely that the whole
  264. * device is munged, not just the one netdev port, so go for the full
  265. * reset.
  266. **/
  267. static void i40e_tx_timeout(struct net_device *netdev)
  268. {
  269. struct i40e_netdev_priv *np = netdev_priv(netdev);
  270. struct i40e_vsi *vsi = np->vsi;
  271. struct i40e_pf *pf = vsi->back;
  272. struct i40e_ring *tx_ring = NULL;
  273. unsigned int i, hung_queue = 0;
  274. u32 head, val;
  275. pf->tx_timeout_count++;
  276. /* find the stopped queue the same way the stack does */
  277. for (i = 0; i < netdev->num_tx_queues; i++) {
  278. struct netdev_queue *q;
  279. unsigned long trans_start;
  280. q = netdev_get_tx_queue(netdev, i);
  281. trans_start = q->trans_start;
  282. if (netif_xmit_stopped(q) &&
  283. time_after(jiffies,
  284. (trans_start + netdev->watchdog_timeo))) {
  285. hung_queue = i;
  286. break;
  287. }
  288. }
  289. if (i == netdev->num_tx_queues) {
  290. netdev_info(netdev, "tx_timeout: no netdev hung queue found\n");
  291. } else {
  292. /* now that we have an index, find the tx_ring struct */
  293. for (i = 0; i < vsi->num_queue_pairs; i++) {
  294. if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
  295. if (hung_queue ==
  296. vsi->tx_rings[i]->queue_index) {
  297. tx_ring = vsi->tx_rings[i];
  298. break;
  299. }
  300. }
  301. }
  302. }
  303. if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ*20)))
  304. pf->tx_timeout_recovery_level = 1; /* reset after some time */
  305. else if (time_before(jiffies,
  306. (pf->tx_timeout_last_recovery + netdev->watchdog_timeo)))
  307. return; /* don't do any new action before the next timeout */
  308. if (tx_ring) {
  309. head = i40e_get_head(tx_ring);
  310. /* Read interrupt register */
  311. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  312. val = rd32(&pf->hw,
  313. I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
  314. tx_ring->vsi->base_vector - 1));
  315. else
  316. val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
  317. 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",
  318. vsi->seid, hung_queue, tx_ring->next_to_clean,
  319. head, tx_ring->next_to_use,
  320. readl(tx_ring->tail), val);
  321. }
  322. pf->tx_timeout_last_recovery = jiffies;
  323. netdev_info(netdev, "tx_timeout recovery level %d, hung_queue %d\n",
  324. pf->tx_timeout_recovery_level, hung_queue);
  325. switch (pf->tx_timeout_recovery_level) {
  326. case 1:
  327. set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
  328. break;
  329. case 2:
  330. set_bit(__I40E_CORE_RESET_REQUESTED, &pf->state);
  331. break;
  332. case 3:
  333. set_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state);
  334. break;
  335. default:
  336. netdev_err(netdev, "tx_timeout recovery unsuccessful\n");
  337. break;
  338. }
  339. i40e_service_event_schedule(pf);
  340. pf->tx_timeout_recovery_level++;
  341. }
  342. /**
  343. * i40e_get_vsi_stats_struct - Get System Network Statistics
  344. * @vsi: the VSI we care about
  345. *
  346. * Returns the address of the device statistics structure.
  347. * The statistics are actually updated from the service task.
  348. **/
  349. struct rtnl_link_stats64 *i40e_get_vsi_stats_struct(struct i40e_vsi *vsi)
  350. {
  351. return &vsi->net_stats;
  352. }
  353. /**
  354. * i40e_get_netdev_stats_struct - Get statistics for netdev interface
  355. * @netdev: network interface device structure
  356. *
  357. * Returns the address of the device statistics structure.
  358. * The statistics are actually updated from the service task.
  359. **/
  360. static void i40e_get_netdev_stats_struct(struct net_device *netdev,
  361. struct rtnl_link_stats64 *stats)
  362. {
  363. struct i40e_netdev_priv *np = netdev_priv(netdev);
  364. struct i40e_ring *tx_ring, *rx_ring;
  365. struct i40e_vsi *vsi = np->vsi;
  366. struct rtnl_link_stats64 *vsi_stats = i40e_get_vsi_stats_struct(vsi);
  367. int i;
  368. if (test_bit(__I40E_DOWN, &vsi->state))
  369. return;
  370. if (!vsi->tx_rings)
  371. return;
  372. rcu_read_lock();
  373. for (i = 0; i < vsi->num_queue_pairs; i++) {
  374. u64 bytes, packets;
  375. unsigned int start;
  376. tx_ring = ACCESS_ONCE(vsi->tx_rings[i]);
  377. if (!tx_ring)
  378. continue;
  379. do {
  380. start = u64_stats_fetch_begin_irq(&tx_ring->syncp);
  381. packets = tx_ring->stats.packets;
  382. bytes = tx_ring->stats.bytes;
  383. } while (u64_stats_fetch_retry_irq(&tx_ring->syncp, start));
  384. stats->tx_packets += packets;
  385. stats->tx_bytes += bytes;
  386. rx_ring = &tx_ring[1];
  387. do {
  388. start = u64_stats_fetch_begin_irq(&rx_ring->syncp);
  389. packets = rx_ring->stats.packets;
  390. bytes = rx_ring->stats.bytes;
  391. } while (u64_stats_fetch_retry_irq(&rx_ring->syncp, start));
  392. stats->rx_packets += packets;
  393. stats->rx_bytes += bytes;
  394. }
  395. rcu_read_unlock();
  396. /* following stats updated by i40e_watchdog_subtask() */
  397. stats->multicast = vsi_stats->multicast;
  398. stats->tx_errors = vsi_stats->tx_errors;
  399. stats->tx_dropped = vsi_stats->tx_dropped;
  400. stats->rx_errors = vsi_stats->rx_errors;
  401. stats->rx_dropped = vsi_stats->rx_dropped;
  402. stats->rx_crc_errors = vsi_stats->rx_crc_errors;
  403. stats->rx_length_errors = vsi_stats->rx_length_errors;
  404. }
  405. /**
  406. * i40e_vsi_reset_stats - Resets all stats of the given vsi
  407. * @vsi: the VSI to have its stats reset
  408. **/
  409. void i40e_vsi_reset_stats(struct i40e_vsi *vsi)
  410. {
  411. struct rtnl_link_stats64 *ns;
  412. int i;
  413. if (!vsi)
  414. return;
  415. ns = i40e_get_vsi_stats_struct(vsi);
  416. memset(ns, 0, sizeof(*ns));
  417. memset(&vsi->net_stats_offsets, 0, sizeof(vsi->net_stats_offsets));
  418. memset(&vsi->eth_stats, 0, sizeof(vsi->eth_stats));
  419. memset(&vsi->eth_stats_offsets, 0, sizeof(vsi->eth_stats_offsets));
  420. if (vsi->rx_rings && vsi->rx_rings[0]) {
  421. for (i = 0; i < vsi->num_queue_pairs; i++) {
  422. memset(&vsi->rx_rings[i]->stats, 0,
  423. sizeof(vsi->rx_rings[i]->stats));
  424. memset(&vsi->rx_rings[i]->rx_stats, 0,
  425. sizeof(vsi->rx_rings[i]->rx_stats));
  426. memset(&vsi->tx_rings[i]->stats, 0,
  427. sizeof(vsi->tx_rings[i]->stats));
  428. memset(&vsi->tx_rings[i]->tx_stats, 0,
  429. sizeof(vsi->tx_rings[i]->tx_stats));
  430. }
  431. }
  432. vsi->stat_offsets_loaded = false;
  433. }
  434. /**
  435. * i40e_pf_reset_stats - Reset all of the stats for the given PF
  436. * @pf: the PF to be reset
  437. **/
  438. void i40e_pf_reset_stats(struct i40e_pf *pf)
  439. {
  440. int i;
  441. memset(&pf->stats, 0, sizeof(pf->stats));
  442. memset(&pf->stats_offsets, 0, sizeof(pf->stats_offsets));
  443. pf->stat_offsets_loaded = false;
  444. for (i = 0; i < I40E_MAX_VEB; i++) {
  445. if (pf->veb[i]) {
  446. memset(&pf->veb[i]->stats, 0,
  447. sizeof(pf->veb[i]->stats));
  448. memset(&pf->veb[i]->stats_offsets, 0,
  449. sizeof(pf->veb[i]->stats_offsets));
  450. pf->veb[i]->stat_offsets_loaded = false;
  451. }
  452. }
  453. pf->hw_csum_rx_error = 0;
  454. }
  455. /**
  456. * i40e_stat_update48 - read and update a 48 bit stat from the chip
  457. * @hw: ptr to the hardware info
  458. * @hireg: the high 32 bit reg to read
  459. * @loreg: the low 32 bit reg to read
  460. * @offset_loaded: has the initial offset been loaded yet
  461. * @offset: ptr to current offset value
  462. * @stat: ptr to the stat
  463. *
  464. * Since the device stats are not reset at PFReset, they likely will not
  465. * be zeroed when the driver starts. We'll save the first values read
  466. * and use them as offsets to be subtracted from the raw values in order
  467. * to report stats that count from zero. In the process, we also manage
  468. * the potential roll-over.
  469. **/
  470. static void i40e_stat_update48(struct i40e_hw *hw, u32 hireg, u32 loreg,
  471. bool offset_loaded, u64 *offset, u64 *stat)
  472. {
  473. u64 new_data;
  474. if (hw->device_id == I40E_DEV_ID_QEMU) {
  475. new_data = rd32(hw, loreg);
  476. new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
  477. } else {
  478. new_data = rd64(hw, loreg);
  479. }
  480. if (!offset_loaded)
  481. *offset = new_data;
  482. if (likely(new_data >= *offset))
  483. *stat = new_data - *offset;
  484. else
  485. *stat = (new_data + BIT_ULL(48)) - *offset;
  486. *stat &= 0xFFFFFFFFFFFFULL;
  487. }
  488. /**
  489. * i40e_stat_update32 - read and update a 32 bit stat from the chip
  490. * @hw: ptr to the hardware info
  491. * @reg: the hw reg to read
  492. * @offset_loaded: has the initial offset been loaded yet
  493. * @offset: ptr to current offset value
  494. * @stat: ptr to the stat
  495. **/
  496. static void i40e_stat_update32(struct i40e_hw *hw, u32 reg,
  497. bool offset_loaded, u64 *offset, u64 *stat)
  498. {
  499. u32 new_data;
  500. new_data = rd32(hw, reg);
  501. if (!offset_loaded)
  502. *offset = new_data;
  503. if (likely(new_data >= *offset))
  504. *stat = (u32)(new_data - *offset);
  505. else
  506. *stat = (u32)((new_data + BIT_ULL(32)) - *offset);
  507. }
  508. /**
  509. * i40e_update_eth_stats - Update VSI-specific ethernet statistics counters.
  510. * @vsi: the VSI to be updated
  511. **/
  512. void i40e_update_eth_stats(struct i40e_vsi *vsi)
  513. {
  514. int stat_idx = le16_to_cpu(vsi->info.stat_counter_idx);
  515. struct i40e_pf *pf = vsi->back;
  516. struct i40e_hw *hw = &pf->hw;
  517. struct i40e_eth_stats *oes;
  518. struct i40e_eth_stats *es; /* device's eth stats */
  519. es = &vsi->eth_stats;
  520. oes = &vsi->eth_stats_offsets;
  521. /* Gather up the stats that the hw collects */
  522. i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
  523. vsi->stat_offsets_loaded,
  524. &oes->tx_errors, &es->tx_errors);
  525. i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx),
  526. vsi->stat_offsets_loaded,
  527. &oes->rx_discards, &es->rx_discards);
  528. i40e_stat_update32(hw, I40E_GLV_RUPP(stat_idx),
  529. vsi->stat_offsets_loaded,
  530. &oes->rx_unknown_protocol, &es->rx_unknown_protocol);
  531. i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
  532. vsi->stat_offsets_loaded,
  533. &oes->tx_errors, &es->tx_errors);
  534. i40e_stat_update48(hw, I40E_GLV_GORCH(stat_idx),
  535. I40E_GLV_GORCL(stat_idx),
  536. vsi->stat_offsets_loaded,
  537. &oes->rx_bytes, &es->rx_bytes);
  538. i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
  539. I40E_GLV_UPRCL(stat_idx),
  540. vsi->stat_offsets_loaded,
  541. &oes->rx_unicast, &es->rx_unicast);
  542. i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
  543. I40E_GLV_MPRCL(stat_idx),
  544. vsi->stat_offsets_loaded,
  545. &oes->rx_multicast, &es->rx_multicast);
  546. i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
  547. I40E_GLV_BPRCL(stat_idx),
  548. vsi->stat_offsets_loaded,
  549. &oes->rx_broadcast, &es->rx_broadcast);
  550. i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
  551. I40E_GLV_GOTCL(stat_idx),
  552. vsi->stat_offsets_loaded,
  553. &oes->tx_bytes, &es->tx_bytes);
  554. i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
  555. I40E_GLV_UPTCL(stat_idx),
  556. vsi->stat_offsets_loaded,
  557. &oes->tx_unicast, &es->tx_unicast);
  558. i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
  559. I40E_GLV_MPTCL(stat_idx),
  560. vsi->stat_offsets_loaded,
  561. &oes->tx_multicast, &es->tx_multicast);
  562. i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
  563. I40E_GLV_BPTCL(stat_idx),
  564. vsi->stat_offsets_loaded,
  565. &oes->tx_broadcast, &es->tx_broadcast);
  566. vsi->stat_offsets_loaded = true;
  567. }
  568. /**
  569. * i40e_update_veb_stats - Update Switch component statistics
  570. * @veb: the VEB being updated
  571. **/
  572. static void i40e_update_veb_stats(struct i40e_veb *veb)
  573. {
  574. struct i40e_pf *pf = veb->pf;
  575. struct i40e_hw *hw = &pf->hw;
  576. struct i40e_eth_stats *oes;
  577. struct i40e_eth_stats *es; /* device's eth stats */
  578. struct i40e_veb_tc_stats *veb_oes;
  579. struct i40e_veb_tc_stats *veb_es;
  580. int i, idx = 0;
  581. idx = veb->stats_idx;
  582. es = &veb->stats;
  583. oes = &veb->stats_offsets;
  584. veb_es = &veb->tc_stats;
  585. veb_oes = &veb->tc_stats_offsets;
  586. /* Gather up the stats that the hw collects */
  587. i40e_stat_update32(hw, I40E_GLSW_TDPC(idx),
  588. veb->stat_offsets_loaded,
  589. &oes->tx_discards, &es->tx_discards);
  590. if (hw->revision_id > 0)
  591. i40e_stat_update32(hw, I40E_GLSW_RUPP(idx),
  592. veb->stat_offsets_loaded,
  593. &oes->rx_unknown_protocol,
  594. &es->rx_unknown_protocol);
  595. i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx),
  596. veb->stat_offsets_loaded,
  597. &oes->rx_bytes, &es->rx_bytes);
  598. i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx),
  599. veb->stat_offsets_loaded,
  600. &oes->rx_unicast, &es->rx_unicast);
  601. i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx),
  602. veb->stat_offsets_loaded,
  603. &oes->rx_multicast, &es->rx_multicast);
  604. i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx),
  605. veb->stat_offsets_loaded,
  606. &oes->rx_broadcast, &es->rx_broadcast);
  607. i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx),
  608. veb->stat_offsets_loaded,
  609. &oes->tx_bytes, &es->tx_bytes);
  610. i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx),
  611. veb->stat_offsets_loaded,
  612. &oes->tx_unicast, &es->tx_unicast);
  613. i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx),
  614. veb->stat_offsets_loaded,
  615. &oes->tx_multicast, &es->tx_multicast);
  616. i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx),
  617. veb->stat_offsets_loaded,
  618. &oes->tx_broadcast, &es->tx_broadcast);
  619. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  620. i40e_stat_update48(hw, I40E_GLVEBTC_RPCH(i, idx),
  621. I40E_GLVEBTC_RPCL(i, idx),
  622. veb->stat_offsets_loaded,
  623. &veb_oes->tc_rx_packets[i],
  624. &veb_es->tc_rx_packets[i]);
  625. i40e_stat_update48(hw, I40E_GLVEBTC_RBCH(i, idx),
  626. I40E_GLVEBTC_RBCL(i, idx),
  627. veb->stat_offsets_loaded,
  628. &veb_oes->tc_rx_bytes[i],
  629. &veb_es->tc_rx_bytes[i]);
  630. i40e_stat_update48(hw, I40E_GLVEBTC_TPCH(i, idx),
  631. I40E_GLVEBTC_TPCL(i, idx),
  632. veb->stat_offsets_loaded,
  633. &veb_oes->tc_tx_packets[i],
  634. &veb_es->tc_tx_packets[i]);
  635. i40e_stat_update48(hw, I40E_GLVEBTC_TBCH(i, idx),
  636. I40E_GLVEBTC_TBCL(i, idx),
  637. veb->stat_offsets_loaded,
  638. &veb_oes->tc_tx_bytes[i],
  639. &veb_es->tc_tx_bytes[i]);
  640. }
  641. veb->stat_offsets_loaded = true;
  642. }
  643. /**
  644. * i40e_update_vsi_stats - Update the vsi statistics counters.
  645. * @vsi: the VSI to be updated
  646. *
  647. * There are a few instances where we store the same stat in a
  648. * couple of different structs. This is partly because we have
  649. * the netdev stats that need to be filled out, which is slightly
  650. * different from the "eth_stats" defined by the chip and used in
  651. * VF communications. We sort it out here.
  652. **/
  653. static void i40e_update_vsi_stats(struct i40e_vsi *vsi)
  654. {
  655. struct i40e_pf *pf = vsi->back;
  656. struct rtnl_link_stats64 *ons;
  657. struct rtnl_link_stats64 *ns; /* netdev stats */
  658. struct i40e_eth_stats *oes;
  659. struct i40e_eth_stats *es; /* device's eth stats */
  660. u32 tx_restart, tx_busy;
  661. struct i40e_ring *p;
  662. u32 rx_page, rx_buf;
  663. u64 bytes, packets;
  664. unsigned int start;
  665. u64 tx_linearize;
  666. u64 tx_force_wb;
  667. u64 rx_p, rx_b;
  668. u64 tx_p, tx_b;
  669. u16 q;
  670. if (test_bit(__I40E_DOWN, &vsi->state) ||
  671. test_bit(__I40E_CONFIG_BUSY, &pf->state))
  672. return;
  673. ns = i40e_get_vsi_stats_struct(vsi);
  674. ons = &vsi->net_stats_offsets;
  675. es = &vsi->eth_stats;
  676. oes = &vsi->eth_stats_offsets;
  677. /* Gather up the netdev and vsi stats that the driver collects
  678. * on the fly during packet processing
  679. */
  680. rx_b = rx_p = 0;
  681. tx_b = tx_p = 0;
  682. tx_restart = tx_busy = tx_linearize = tx_force_wb = 0;
  683. rx_page = 0;
  684. rx_buf = 0;
  685. rcu_read_lock();
  686. for (q = 0; q < vsi->num_queue_pairs; q++) {
  687. /* locate Tx ring */
  688. p = ACCESS_ONCE(vsi->tx_rings[q]);
  689. do {
  690. start = u64_stats_fetch_begin_irq(&p->syncp);
  691. packets = p->stats.packets;
  692. bytes = p->stats.bytes;
  693. } while (u64_stats_fetch_retry_irq(&p->syncp, start));
  694. tx_b += bytes;
  695. tx_p += packets;
  696. tx_restart += p->tx_stats.restart_queue;
  697. tx_busy += p->tx_stats.tx_busy;
  698. tx_linearize += p->tx_stats.tx_linearize;
  699. tx_force_wb += p->tx_stats.tx_force_wb;
  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->rx_page_failed = rx_page;
  718. vsi->rx_buf_failed = rx_buf;
  719. ns->rx_packets = rx_p;
  720. ns->rx_bytes = rx_b;
  721. ns->tx_packets = tx_p;
  722. ns->tx_bytes = tx_b;
  723. /* update netdev stats from eth stats */
  724. i40e_update_eth_stats(vsi);
  725. ons->tx_errors = oes->tx_errors;
  726. ns->tx_errors = es->tx_errors;
  727. ons->multicast = oes->rx_multicast;
  728. ns->multicast = es->rx_multicast;
  729. ons->rx_dropped = oes->rx_discards;
  730. ns->rx_dropped = es->rx_discards;
  731. ons->tx_dropped = oes->tx_discards;
  732. ns->tx_dropped = es->tx_discards;
  733. /* pull in a couple PF stats if this is the main vsi */
  734. if (vsi == pf->vsi[pf->lan_vsi]) {
  735. ns->rx_crc_errors = pf->stats.crc_errors;
  736. ns->rx_errors = pf->stats.crc_errors + pf->stats.illegal_bytes;
  737. ns->rx_length_errors = pf->stats.rx_length_errors;
  738. }
  739. }
  740. /**
  741. * i40e_update_pf_stats - Update the PF statistics counters.
  742. * @pf: the PF to be updated
  743. **/
  744. static void i40e_update_pf_stats(struct i40e_pf *pf)
  745. {
  746. struct i40e_hw_port_stats *osd = &pf->stats_offsets;
  747. struct i40e_hw_port_stats *nsd = &pf->stats;
  748. struct i40e_hw *hw = &pf->hw;
  749. u32 val;
  750. int i;
  751. i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
  752. I40E_GLPRT_GORCL(hw->port),
  753. pf->stat_offsets_loaded,
  754. &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
  755. i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
  756. I40E_GLPRT_GOTCL(hw->port),
  757. pf->stat_offsets_loaded,
  758. &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
  759. i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
  760. pf->stat_offsets_loaded,
  761. &osd->eth.rx_discards,
  762. &nsd->eth.rx_discards);
  763. i40e_stat_update48(hw, I40E_GLPRT_UPRCH(hw->port),
  764. I40E_GLPRT_UPRCL(hw->port),
  765. pf->stat_offsets_loaded,
  766. &osd->eth.rx_unicast,
  767. &nsd->eth.rx_unicast);
  768. i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
  769. I40E_GLPRT_MPRCL(hw->port),
  770. pf->stat_offsets_loaded,
  771. &osd->eth.rx_multicast,
  772. &nsd->eth.rx_multicast);
  773. i40e_stat_update48(hw, I40E_GLPRT_BPRCH(hw->port),
  774. I40E_GLPRT_BPRCL(hw->port),
  775. pf->stat_offsets_loaded,
  776. &osd->eth.rx_broadcast,
  777. &nsd->eth.rx_broadcast);
  778. i40e_stat_update48(hw, I40E_GLPRT_UPTCH(hw->port),
  779. I40E_GLPRT_UPTCL(hw->port),
  780. pf->stat_offsets_loaded,
  781. &osd->eth.tx_unicast,
  782. &nsd->eth.tx_unicast);
  783. i40e_stat_update48(hw, I40E_GLPRT_MPTCH(hw->port),
  784. I40E_GLPRT_MPTCL(hw->port),
  785. pf->stat_offsets_loaded,
  786. &osd->eth.tx_multicast,
  787. &nsd->eth.tx_multicast);
  788. i40e_stat_update48(hw, I40E_GLPRT_BPTCH(hw->port),
  789. I40E_GLPRT_BPTCL(hw->port),
  790. pf->stat_offsets_loaded,
  791. &osd->eth.tx_broadcast,
  792. &nsd->eth.tx_broadcast);
  793. i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
  794. pf->stat_offsets_loaded,
  795. &osd->tx_dropped_link_down,
  796. &nsd->tx_dropped_link_down);
  797. i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
  798. pf->stat_offsets_loaded,
  799. &osd->crc_errors, &nsd->crc_errors);
  800. i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
  801. pf->stat_offsets_loaded,
  802. &osd->illegal_bytes, &nsd->illegal_bytes);
  803. i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
  804. pf->stat_offsets_loaded,
  805. &osd->mac_local_faults,
  806. &nsd->mac_local_faults);
  807. i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
  808. pf->stat_offsets_loaded,
  809. &osd->mac_remote_faults,
  810. &nsd->mac_remote_faults);
  811. i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
  812. pf->stat_offsets_loaded,
  813. &osd->rx_length_errors,
  814. &nsd->rx_length_errors);
  815. i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
  816. pf->stat_offsets_loaded,
  817. &osd->link_xon_rx, &nsd->link_xon_rx);
  818. i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
  819. pf->stat_offsets_loaded,
  820. &osd->link_xon_tx, &nsd->link_xon_tx);
  821. i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
  822. pf->stat_offsets_loaded,
  823. &osd->link_xoff_rx, &nsd->link_xoff_rx);
  824. i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
  825. pf->stat_offsets_loaded,
  826. &osd->link_xoff_tx, &nsd->link_xoff_tx);
  827. for (i = 0; i < 8; i++) {
  828. i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
  829. pf->stat_offsets_loaded,
  830. &osd->priority_xoff_rx[i],
  831. &nsd->priority_xoff_rx[i]);
  832. i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
  833. pf->stat_offsets_loaded,
  834. &osd->priority_xon_rx[i],
  835. &nsd->priority_xon_rx[i]);
  836. i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
  837. pf->stat_offsets_loaded,
  838. &osd->priority_xon_tx[i],
  839. &nsd->priority_xon_tx[i]);
  840. i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
  841. pf->stat_offsets_loaded,
  842. &osd->priority_xoff_tx[i],
  843. &nsd->priority_xoff_tx[i]);
  844. i40e_stat_update32(hw,
  845. I40E_GLPRT_RXON2OFFCNT(hw->port, i),
  846. pf->stat_offsets_loaded,
  847. &osd->priority_xon_2_xoff[i],
  848. &nsd->priority_xon_2_xoff[i]);
  849. }
  850. i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
  851. I40E_GLPRT_PRC64L(hw->port),
  852. pf->stat_offsets_loaded,
  853. &osd->rx_size_64, &nsd->rx_size_64);
  854. i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
  855. I40E_GLPRT_PRC127L(hw->port),
  856. pf->stat_offsets_loaded,
  857. &osd->rx_size_127, &nsd->rx_size_127);
  858. i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
  859. I40E_GLPRT_PRC255L(hw->port),
  860. pf->stat_offsets_loaded,
  861. &osd->rx_size_255, &nsd->rx_size_255);
  862. i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
  863. I40E_GLPRT_PRC511L(hw->port),
  864. pf->stat_offsets_loaded,
  865. &osd->rx_size_511, &nsd->rx_size_511);
  866. i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
  867. I40E_GLPRT_PRC1023L(hw->port),
  868. pf->stat_offsets_loaded,
  869. &osd->rx_size_1023, &nsd->rx_size_1023);
  870. i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
  871. I40E_GLPRT_PRC1522L(hw->port),
  872. pf->stat_offsets_loaded,
  873. &osd->rx_size_1522, &nsd->rx_size_1522);
  874. i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
  875. I40E_GLPRT_PRC9522L(hw->port),
  876. pf->stat_offsets_loaded,
  877. &osd->rx_size_big, &nsd->rx_size_big);
  878. i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
  879. I40E_GLPRT_PTC64L(hw->port),
  880. pf->stat_offsets_loaded,
  881. &osd->tx_size_64, &nsd->tx_size_64);
  882. i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
  883. I40E_GLPRT_PTC127L(hw->port),
  884. pf->stat_offsets_loaded,
  885. &osd->tx_size_127, &nsd->tx_size_127);
  886. i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
  887. I40E_GLPRT_PTC255L(hw->port),
  888. pf->stat_offsets_loaded,
  889. &osd->tx_size_255, &nsd->tx_size_255);
  890. i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
  891. I40E_GLPRT_PTC511L(hw->port),
  892. pf->stat_offsets_loaded,
  893. &osd->tx_size_511, &nsd->tx_size_511);
  894. i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
  895. I40E_GLPRT_PTC1023L(hw->port),
  896. pf->stat_offsets_loaded,
  897. &osd->tx_size_1023, &nsd->tx_size_1023);
  898. i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
  899. I40E_GLPRT_PTC1522L(hw->port),
  900. pf->stat_offsets_loaded,
  901. &osd->tx_size_1522, &nsd->tx_size_1522);
  902. i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
  903. I40E_GLPRT_PTC9522L(hw->port),
  904. pf->stat_offsets_loaded,
  905. &osd->tx_size_big, &nsd->tx_size_big);
  906. i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
  907. pf->stat_offsets_loaded,
  908. &osd->rx_undersize, &nsd->rx_undersize);
  909. i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
  910. pf->stat_offsets_loaded,
  911. &osd->rx_fragments, &nsd->rx_fragments);
  912. i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
  913. pf->stat_offsets_loaded,
  914. &osd->rx_oversize, &nsd->rx_oversize);
  915. i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
  916. pf->stat_offsets_loaded,
  917. &osd->rx_jabber, &nsd->rx_jabber);
  918. /* FDIR stats */
  919. i40e_stat_update32(hw,
  920. I40E_GLQF_PCNT(I40E_FD_ATR_STAT_IDX(pf->hw.pf_id)),
  921. pf->stat_offsets_loaded,
  922. &osd->fd_atr_match, &nsd->fd_atr_match);
  923. i40e_stat_update32(hw,
  924. I40E_GLQF_PCNT(I40E_FD_SB_STAT_IDX(pf->hw.pf_id)),
  925. pf->stat_offsets_loaded,
  926. &osd->fd_sb_match, &nsd->fd_sb_match);
  927. i40e_stat_update32(hw,
  928. I40E_GLQF_PCNT(I40E_FD_ATR_TUNNEL_STAT_IDX(pf->hw.pf_id)),
  929. pf->stat_offsets_loaded,
  930. &osd->fd_atr_tunnel_match, &nsd->fd_atr_tunnel_match);
  931. val = rd32(hw, I40E_PRTPM_EEE_STAT);
  932. nsd->tx_lpi_status =
  933. (val & I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_MASK) >>
  934. I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_SHIFT;
  935. nsd->rx_lpi_status =
  936. (val & I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_MASK) >>
  937. I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_SHIFT;
  938. i40e_stat_update32(hw, I40E_PRTPM_TLPIC,
  939. pf->stat_offsets_loaded,
  940. &osd->tx_lpi_count, &nsd->tx_lpi_count);
  941. i40e_stat_update32(hw, I40E_PRTPM_RLPIC,
  942. pf->stat_offsets_loaded,
  943. &osd->rx_lpi_count, &nsd->rx_lpi_count);
  944. if (pf->flags & I40E_FLAG_FD_SB_ENABLED &&
  945. !(pf->hw_disabled_flags & I40E_FLAG_FD_SB_ENABLED))
  946. nsd->fd_sb_status = true;
  947. else
  948. nsd->fd_sb_status = false;
  949. if (pf->flags & I40E_FLAG_FD_ATR_ENABLED &&
  950. !(pf->hw_disabled_flags & I40E_FLAG_FD_ATR_ENABLED))
  951. nsd->fd_atr_status = true;
  952. else
  953. nsd->fd_atr_status = false;
  954. pf->stat_offsets_loaded = true;
  955. }
  956. /**
  957. * i40e_update_stats - Update the various statistics counters.
  958. * @vsi: the VSI to be updated
  959. *
  960. * Update the various stats for this VSI and its related entities.
  961. **/
  962. void i40e_update_stats(struct i40e_vsi *vsi)
  963. {
  964. struct i40e_pf *pf = vsi->back;
  965. if (vsi == pf->vsi[pf->lan_vsi])
  966. i40e_update_pf_stats(pf);
  967. i40e_update_vsi_stats(vsi);
  968. }
  969. /**
  970. * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
  971. * @vsi: the VSI to be searched
  972. * @macaddr: the MAC address
  973. * @vlan: the vlan
  974. *
  975. * Returns ptr to the filter object or NULL
  976. **/
  977. static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
  978. const u8 *macaddr, s16 vlan)
  979. {
  980. struct i40e_mac_filter *f;
  981. u64 key;
  982. if (!vsi || !macaddr)
  983. return NULL;
  984. key = i40e_addr_to_hkey(macaddr);
  985. hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
  986. if ((ether_addr_equal(macaddr, f->macaddr)) &&
  987. (vlan == f->vlan))
  988. return f;
  989. }
  990. return NULL;
  991. }
  992. /**
  993. * i40e_find_mac - Find a mac addr in the macvlan filters list
  994. * @vsi: the VSI to be searched
  995. * @macaddr: the MAC address we are searching for
  996. *
  997. * Returns the first filter with the provided MAC address or NULL if
  998. * MAC address was not found
  999. **/
  1000. struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, const u8 *macaddr)
  1001. {
  1002. struct i40e_mac_filter *f;
  1003. u64 key;
  1004. if (!vsi || !macaddr)
  1005. return NULL;
  1006. key = i40e_addr_to_hkey(macaddr);
  1007. hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
  1008. if ((ether_addr_equal(macaddr, f->macaddr)))
  1009. return f;
  1010. }
  1011. return NULL;
  1012. }
  1013. /**
  1014. * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
  1015. * @vsi: the VSI to be searched
  1016. *
  1017. * Returns true if VSI is in vlan mode or false otherwise
  1018. **/
  1019. bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
  1020. {
  1021. /* If we have a PVID, always operate in VLAN mode */
  1022. if (vsi->info.pvid)
  1023. return true;
  1024. /* We need to operate in VLAN mode whenever we have any filters with
  1025. * a VLAN other than I40E_VLAN_ALL. We could check the table each
  1026. * time, incurring search cost repeatedly. However, we can notice two
  1027. * things:
  1028. *
  1029. * 1) the only place where we can gain a VLAN filter is in
  1030. * i40e_add_filter.
  1031. *
  1032. * 2) the only place where filters are actually removed is in
  1033. * i40e_sync_filters_subtask.
  1034. *
  1035. * Thus, we can simply use a boolean value, has_vlan_filters which we
  1036. * will set to true when we add a VLAN filter in i40e_add_filter. Then
  1037. * we have to perform the full search after deleting filters in
  1038. * i40e_sync_filters_subtask, but we already have to search
  1039. * filters here and can perform the check at the same time. This
  1040. * results in avoiding embedding a loop for VLAN mode inside another
  1041. * loop over all the filters, and should maintain correctness as noted
  1042. * above.
  1043. */
  1044. return vsi->has_vlan_filter;
  1045. }
  1046. /**
  1047. * i40e_correct_mac_vlan_filters - Correct non-VLAN filters if necessary
  1048. * @vsi: the VSI to configure
  1049. * @tmp_add_list: list of filters ready to be added
  1050. * @tmp_del_list: list of filters ready to be deleted
  1051. * @vlan_filters: the number of active VLAN filters
  1052. *
  1053. * Update VLAN=0 and VLAN=-1 (I40E_VLAN_ANY) filters properly so that they
  1054. * behave as expected. If we have any active VLAN filters remaining or about
  1055. * to be added then we need to update non-VLAN filters to be marked as VLAN=0
  1056. * so that they only match against untagged traffic. If we no longer have any
  1057. * active VLAN filters, we need to make all non-VLAN filters marked as VLAN=-1
  1058. * so that they match against both tagged and untagged traffic. In this way,
  1059. * we ensure that we correctly receive the desired traffic. This ensures that
  1060. * when we have an active VLAN we will receive only untagged traffic and
  1061. * traffic matching active VLANs. If we have no active VLANs then we will
  1062. * operate in non-VLAN mode and receive all traffic, tagged or untagged.
  1063. *
  1064. * Finally, in a similar fashion, this function also corrects filters when
  1065. * there is an active PVID assigned to this VSI.
  1066. *
  1067. * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
  1068. *
  1069. * This function is only expected to be called from within
  1070. * i40e_sync_vsi_filters.
  1071. *
  1072. * NOTE: This function expects to be called while under the
  1073. * mac_filter_hash_lock
  1074. */
  1075. static int i40e_correct_mac_vlan_filters(struct i40e_vsi *vsi,
  1076. struct hlist_head *tmp_add_list,
  1077. struct hlist_head *tmp_del_list,
  1078. int vlan_filters)
  1079. {
  1080. s16 pvid = le16_to_cpu(vsi->info.pvid);
  1081. struct i40e_mac_filter *f, *add_head;
  1082. struct i40e_new_mac_filter *new;
  1083. struct hlist_node *h;
  1084. int bkt, new_vlan;
  1085. /* To determine if a particular filter needs to be replaced we
  1086. * have the three following conditions:
  1087. *
  1088. * a) if we have a PVID assigned, then all filters which are
  1089. * not marked as VLAN=PVID must be replaced with filters that
  1090. * are.
  1091. * b) otherwise, if we have any active VLANS, all filters
  1092. * which are marked as VLAN=-1 must be replaced with
  1093. * filters marked as VLAN=0
  1094. * c) finally, if we do not have any active VLANS, all filters
  1095. * which are marked as VLAN=0 must be replaced with filters
  1096. * marked as VLAN=-1
  1097. */
  1098. /* Update the filters about to be added in place */
  1099. hlist_for_each_entry(new, tmp_add_list, hlist) {
  1100. if (pvid && new->f->vlan != pvid)
  1101. new->f->vlan = pvid;
  1102. else if (vlan_filters && new->f->vlan == I40E_VLAN_ANY)
  1103. new->f->vlan = 0;
  1104. else if (!vlan_filters && new->f->vlan == 0)
  1105. new->f->vlan = I40E_VLAN_ANY;
  1106. }
  1107. /* Update the remaining active filters */
  1108. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  1109. /* Combine the checks for whether a filter needs to be changed
  1110. * and then determine the new VLAN inside the if block, in
  1111. * order to avoid duplicating code for adding the new filter
  1112. * then deleting the old filter.
  1113. */
  1114. if ((pvid && f->vlan != pvid) ||
  1115. (vlan_filters && f->vlan == I40E_VLAN_ANY) ||
  1116. (!vlan_filters && f->vlan == 0)) {
  1117. /* Determine the new vlan we will be adding */
  1118. if (pvid)
  1119. new_vlan = pvid;
  1120. else if (vlan_filters)
  1121. new_vlan = 0;
  1122. else
  1123. new_vlan = I40E_VLAN_ANY;
  1124. /* Create the new filter */
  1125. add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
  1126. if (!add_head)
  1127. return -ENOMEM;
  1128. /* Create a temporary i40e_new_mac_filter */
  1129. new = kzalloc(sizeof(*new), GFP_ATOMIC);
  1130. if (!new)
  1131. return -ENOMEM;
  1132. new->f = add_head;
  1133. new->state = add_head->state;
  1134. /* Add the new filter to the tmp list */
  1135. hlist_add_head(&new->hlist, tmp_add_list);
  1136. /* Put the original filter into the delete list */
  1137. f->state = I40E_FILTER_REMOVE;
  1138. hash_del(&f->hlist);
  1139. hlist_add_head(&f->hlist, tmp_del_list);
  1140. }
  1141. }
  1142. vsi->has_vlan_filter = !!vlan_filters;
  1143. return 0;
  1144. }
  1145. /**
  1146. * i40e_rm_default_mac_filter - Remove the default MAC filter set by NVM
  1147. * @vsi: the PF Main VSI - inappropriate for any other VSI
  1148. * @macaddr: the MAC address
  1149. *
  1150. * Remove whatever filter the firmware set up so the driver can manage
  1151. * its own filtering intelligently.
  1152. **/
  1153. static void i40e_rm_default_mac_filter(struct i40e_vsi *vsi, u8 *macaddr)
  1154. {
  1155. struct i40e_aqc_remove_macvlan_element_data element;
  1156. struct i40e_pf *pf = vsi->back;
  1157. /* Only appropriate for the PF main VSI */
  1158. if (vsi->type != I40E_VSI_MAIN)
  1159. return;
  1160. memset(&element, 0, sizeof(element));
  1161. ether_addr_copy(element.mac_addr, macaddr);
  1162. element.vlan_tag = 0;
  1163. /* Ignore error returns, some firmware does it this way... */
  1164. element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
  1165. i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
  1166. memset(&element, 0, sizeof(element));
  1167. ether_addr_copy(element.mac_addr, macaddr);
  1168. element.vlan_tag = 0;
  1169. /* ...and some firmware does it this way. */
  1170. element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH |
  1171. I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
  1172. i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
  1173. }
  1174. /**
  1175. * i40e_add_filter - Add a mac/vlan filter to the VSI
  1176. * @vsi: the VSI to be searched
  1177. * @macaddr: the MAC address
  1178. * @vlan: the vlan
  1179. *
  1180. * Returns ptr to the filter object or NULL when no memory available.
  1181. *
  1182. * NOTE: This function is expected to be called with mac_filter_hash_lock
  1183. * being held.
  1184. **/
  1185. struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
  1186. const u8 *macaddr, s16 vlan)
  1187. {
  1188. struct i40e_mac_filter *f;
  1189. u64 key;
  1190. if (!vsi || !macaddr)
  1191. return NULL;
  1192. f = i40e_find_filter(vsi, macaddr, vlan);
  1193. if (!f) {
  1194. f = kzalloc(sizeof(*f), GFP_ATOMIC);
  1195. if (!f)
  1196. return NULL;
  1197. /* Update the boolean indicating if we need to function in
  1198. * VLAN mode.
  1199. */
  1200. if (vlan >= 0)
  1201. vsi->has_vlan_filter = true;
  1202. ether_addr_copy(f->macaddr, macaddr);
  1203. f->vlan = vlan;
  1204. /* If we're in overflow promisc mode, set the state directly
  1205. * to failed, so we don't bother to try sending the filter
  1206. * to the hardware.
  1207. */
  1208. if (test_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state))
  1209. f->state = I40E_FILTER_FAILED;
  1210. else
  1211. f->state = I40E_FILTER_NEW;
  1212. INIT_HLIST_NODE(&f->hlist);
  1213. key = i40e_addr_to_hkey(macaddr);
  1214. hash_add(vsi->mac_filter_hash, &f->hlist, key);
  1215. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  1216. vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
  1217. }
  1218. /* If we're asked to add a filter that has been marked for removal, it
  1219. * is safe to simply restore it to active state. __i40e_del_filter
  1220. * will have simply deleted any filters which were previously marked
  1221. * NEW or FAILED, so if it is currently marked REMOVE it must have
  1222. * previously been ACTIVE. Since we haven't yet run the sync filters
  1223. * task, just restore this filter to the ACTIVE state so that the
  1224. * sync task leaves it in place
  1225. */
  1226. if (f->state == I40E_FILTER_REMOVE)
  1227. f->state = I40E_FILTER_ACTIVE;
  1228. return f;
  1229. }
  1230. /**
  1231. * __i40e_del_filter - Remove a specific filter from the VSI
  1232. * @vsi: VSI to remove from
  1233. * @f: the filter to remove from the list
  1234. *
  1235. * This function should be called instead of i40e_del_filter only if you know
  1236. * the exact filter you will remove already, such as via i40e_find_filter or
  1237. * i40e_find_mac.
  1238. *
  1239. * NOTE: This function is expected to be called with mac_filter_hash_lock
  1240. * being held.
  1241. * ANOTHER NOTE: This function MUST be called from within the context of
  1242. * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
  1243. * instead of list_for_each_entry().
  1244. **/
  1245. void __i40e_del_filter(struct i40e_vsi *vsi, struct i40e_mac_filter *f)
  1246. {
  1247. if (!f)
  1248. return;
  1249. /* If the filter was never added to firmware then we can just delete it
  1250. * directly and we don't want to set the status to remove or else an
  1251. * admin queue command will unnecessarily fire.
  1252. */
  1253. if ((f->state == I40E_FILTER_FAILED) ||
  1254. (f->state == I40E_FILTER_NEW)) {
  1255. hash_del(&f->hlist);
  1256. kfree(f);
  1257. } else {
  1258. f->state = I40E_FILTER_REMOVE;
  1259. }
  1260. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  1261. vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
  1262. }
  1263. /**
  1264. * i40e_del_filter - Remove a MAC/VLAN filter from the VSI
  1265. * @vsi: the VSI to be searched
  1266. * @macaddr: the MAC address
  1267. * @vlan: the VLAN
  1268. *
  1269. * NOTE: This function is expected to be called with mac_filter_hash_lock
  1270. * being held.
  1271. * ANOTHER NOTE: This function MUST be called from within the context of
  1272. * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
  1273. * instead of list_for_each_entry().
  1274. **/
  1275. void i40e_del_filter(struct i40e_vsi *vsi, const u8 *macaddr, s16 vlan)
  1276. {
  1277. struct i40e_mac_filter *f;
  1278. if (!vsi || !macaddr)
  1279. return;
  1280. f = i40e_find_filter(vsi, macaddr, vlan);
  1281. __i40e_del_filter(vsi, f);
  1282. }
  1283. /**
  1284. * i40e_add_mac_filter - Add a MAC filter for all active VLANs
  1285. * @vsi: the VSI to be searched
  1286. * @macaddr: the mac address to be filtered
  1287. *
  1288. * If we're not in VLAN mode, just add the filter to I40E_VLAN_ANY. Otherwise,
  1289. * go through all the macvlan filters and add a macvlan filter for each
  1290. * unique vlan that already exists. If a PVID has been assigned, instead only
  1291. * add the macaddr to that VLAN.
  1292. *
  1293. * Returns last filter added on success, else NULL
  1294. **/
  1295. struct i40e_mac_filter *i40e_add_mac_filter(struct i40e_vsi *vsi,
  1296. const u8 *macaddr)
  1297. {
  1298. struct i40e_mac_filter *f, *add = NULL;
  1299. struct hlist_node *h;
  1300. int bkt;
  1301. if (vsi->info.pvid)
  1302. return i40e_add_filter(vsi, macaddr,
  1303. le16_to_cpu(vsi->info.pvid));
  1304. if (!i40e_is_vsi_in_vlan(vsi))
  1305. return i40e_add_filter(vsi, macaddr, I40E_VLAN_ANY);
  1306. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  1307. if (f->state == I40E_FILTER_REMOVE)
  1308. continue;
  1309. add = i40e_add_filter(vsi, macaddr, f->vlan);
  1310. if (!add)
  1311. return NULL;
  1312. }
  1313. return add;
  1314. }
  1315. /**
  1316. * i40e_del_mac_filter - Remove a MAC filter from all VLANs
  1317. * @vsi: the VSI to be searched
  1318. * @macaddr: the mac address to be removed
  1319. *
  1320. * Removes a given MAC address from a VSI regardless of what VLAN it has been
  1321. * associated with.
  1322. *
  1323. * Returns 0 for success, or error
  1324. **/
  1325. int i40e_del_mac_filter(struct i40e_vsi *vsi, const u8 *macaddr)
  1326. {
  1327. struct i40e_mac_filter *f;
  1328. struct hlist_node *h;
  1329. bool found = false;
  1330. int bkt;
  1331. WARN(!spin_is_locked(&vsi->mac_filter_hash_lock),
  1332. "Missing mac_filter_hash_lock\n");
  1333. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  1334. if (ether_addr_equal(macaddr, f->macaddr)) {
  1335. __i40e_del_filter(vsi, f);
  1336. found = true;
  1337. }
  1338. }
  1339. if (found)
  1340. return 0;
  1341. else
  1342. return -ENOENT;
  1343. }
  1344. /**
  1345. * i40e_set_mac - NDO callback to set mac address
  1346. * @netdev: network interface device structure
  1347. * @p: pointer to an address structure
  1348. *
  1349. * Returns 0 on success, negative on failure
  1350. **/
  1351. static int i40e_set_mac(struct net_device *netdev, void *p)
  1352. {
  1353. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1354. struct i40e_vsi *vsi = np->vsi;
  1355. struct i40e_pf *pf = vsi->back;
  1356. struct i40e_hw *hw = &pf->hw;
  1357. struct sockaddr *addr = p;
  1358. if (!is_valid_ether_addr(addr->sa_data))
  1359. return -EADDRNOTAVAIL;
  1360. if (ether_addr_equal(netdev->dev_addr, addr->sa_data)) {
  1361. netdev_info(netdev, "already using mac address %pM\n",
  1362. addr->sa_data);
  1363. return 0;
  1364. }
  1365. if (test_bit(__I40E_DOWN, &vsi->back->state) ||
  1366. test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
  1367. return -EADDRNOTAVAIL;
  1368. if (ether_addr_equal(hw->mac.addr, addr->sa_data))
  1369. netdev_info(netdev, "returning to hw mac address %pM\n",
  1370. hw->mac.addr);
  1371. else
  1372. netdev_info(netdev, "set new mac address %pM\n", addr->sa_data);
  1373. spin_lock_bh(&vsi->mac_filter_hash_lock);
  1374. i40e_del_mac_filter(vsi, netdev->dev_addr);
  1375. i40e_add_mac_filter(vsi, addr->sa_data);
  1376. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  1377. ether_addr_copy(netdev->dev_addr, addr->sa_data);
  1378. if (vsi->type == I40E_VSI_MAIN) {
  1379. i40e_status ret;
  1380. ret = i40e_aq_mac_address_write(&vsi->back->hw,
  1381. I40E_AQC_WRITE_TYPE_LAA_WOL,
  1382. addr->sa_data, NULL);
  1383. if (ret)
  1384. netdev_info(netdev, "Ignoring error from firmware on LAA update, status %s, AQ ret %s\n",
  1385. i40e_stat_str(hw, ret),
  1386. i40e_aq_str(hw, hw->aq.asq_last_status));
  1387. }
  1388. /* schedule our worker thread which will take care of
  1389. * applying the new filter changes
  1390. */
  1391. i40e_service_event_schedule(vsi->back);
  1392. return 0;
  1393. }
  1394. /**
  1395. * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
  1396. * @vsi: the VSI being setup
  1397. * @ctxt: VSI context structure
  1398. * @enabled_tc: Enabled TCs bitmap
  1399. * @is_add: True if called before Add VSI
  1400. *
  1401. * Setup VSI queue mapping for enabled traffic classes.
  1402. **/
  1403. static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
  1404. struct i40e_vsi_context *ctxt,
  1405. u8 enabled_tc,
  1406. bool is_add)
  1407. {
  1408. struct i40e_pf *pf = vsi->back;
  1409. u16 sections = 0;
  1410. u8 netdev_tc = 0;
  1411. u16 numtc = 0;
  1412. u16 qcount;
  1413. u8 offset;
  1414. u16 qmap;
  1415. int i;
  1416. u16 num_tc_qps = 0;
  1417. sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
  1418. offset = 0;
  1419. if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
  1420. /* Find numtc from enabled TC bitmap */
  1421. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  1422. if (enabled_tc & BIT(i)) /* TC is enabled */
  1423. numtc++;
  1424. }
  1425. if (!numtc) {
  1426. dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
  1427. numtc = 1;
  1428. }
  1429. } else {
  1430. /* At least TC0 is enabled in case of non-DCB case */
  1431. numtc = 1;
  1432. }
  1433. vsi->tc_config.numtc = numtc;
  1434. vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
  1435. /* Number of queues per enabled TC */
  1436. qcount = vsi->alloc_queue_pairs;
  1437. num_tc_qps = qcount / numtc;
  1438. num_tc_qps = min_t(int, num_tc_qps, i40e_pf_get_max_q_per_tc(pf));
  1439. /* Setup queue offset/count for all TCs for given VSI */
  1440. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  1441. /* See if the given TC is enabled for the given VSI */
  1442. if (vsi->tc_config.enabled_tc & BIT(i)) {
  1443. /* TC is enabled */
  1444. int pow, num_qps;
  1445. switch (vsi->type) {
  1446. case I40E_VSI_MAIN:
  1447. qcount = min_t(int, pf->alloc_rss_size,
  1448. num_tc_qps);
  1449. break;
  1450. case I40E_VSI_FDIR:
  1451. case I40E_VSI_SRIOV:
  1452. case I40E_VSI_VMDQ2:
  1453. default:
  1454. qcount = num_tc_qps;
  1455. WARN_ON(i != 0);
  1456. break;
  1457. }
  1458. vsi->tc_config.tc_info[i].qoffset = offset;
  1459. vsi->tc_config.tc_info[i].qcount = qcount;
  1460. /* find the next higher power-of-2 of num queue pairs */
  1461. num_qps = qcount;
  1462. pow = 0;
  1463. while (num_qps && (BIT_ULL(pow) < qcount)) {
  1464. pow++;
  1465. num_qps >>= 1;
  1466. }
  1467. vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
  1468. qmap =
  1469. (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
  1470. (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
  1471. offset += qcount;
  1472. } else {
  1473. /* TC is not enabled so set the offset to
  1474. * default queue and allocate one queue
  1475. * for the given TC.
  1476. */
  1477. vsi->tc_config.tc_info[i].qoffset = 0;
  1478. vsi->tc_config.tc_info[i].qcount = 1;
  1479. vsi->tc_config.tc_info[i].netdev_tc = 0;
  1480. qmap = 0;
  1481. }
  1482. ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
  1483. }
  1484. /* Set actual Tx/Rx queue pairs */
  1485. vsi->num_queue_pairs = offset;
  1486. if ((vsi->type == I40E_VSI_MAIN) && (numtc == 1)) {
  1487. if (vsi->req_queue_pairs > 0)
  1488. vsi->num_queue_pairs = vsi->req_queue_pairs;
  1489. else if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  1490. vsi->num_queue_pairs = pf->num_lan_msix;
  1491. }
  1492. /* Scheduler section valid can only be set for ADD VSI */
  1493. if (is_add) {
  1494. sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
  1495. ctxt->info.up_enable_bits = enabled_tc;
  1496. }
  1497. if (vsi->type == I40E_VSI_SRIOV) {
  1498. ctxt->info.mapping_flags |=
  1499. cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
  1500. for (i = 0; i < vsi->num_queue_pairs; i++)
  1501. ctxt->info.queue_mapping[i] =
  1502. cpu_to_le16(vsi->base_queue + i);
  1503. } else {
  1504. ctxt->info.mapping_flags |=
  1505. cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
  1506. ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
  1507. }
  1508. ctxt->info.valid_sections |= cpu_to_le16(sections);
  1509. }
  1510. /**
  1511. * i40e_addr_sync - Callback for dev_(mc|uc)_sync to add address
  1512. * @netdev: the netdevice
  1513. * @addr: address to add
  1514. *
  1515. * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
  1516. * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
  1517. */
  1518. static int i40e_addr_sync(struct net_device *netdev, const u8 *addr)
  1519. {
  1520. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1521. struct i40e_vsi *vsi = np->vsi;
  1522. if (i40e_add_mac_filter(vsi, addr))
  1523. return 0;
  1524. else
  1525. return -ENOMEM;
  1526. }
  1527. /**
  1528. * i40e_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
  1529. * @netdev: the netdevice
  1530. * @addr: address to add
  1531. *
  1532. * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call
  1533. * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
  1534. */
  1535. static int i40e_addr_unsync(struct net_device *netdev, const u8 *addr)
  1536. {
  1537. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1538. struct i40e_vsi *vsi = np->vsi;
  1539. i40e_del_mac_filter(vsi, addr);
  1540. return 0;
  1541. }
  1542. /**
  1543. * i40e_set_rx_mode - NDO callback to set the netdev filters
  1544. * @netdev: network interface device structure
  1545. **/
  1546. static void i40e_set_rx_mode(struct net_device *netdev)
  1547. {
  1548. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1549. struct i40e_vsi *vsi = np->vsi;
  1550. spin_lock_bh(&vsi->mac_filter_hash_lock);
  1551. __dev_uc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
  1552. __dev_mc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
  1553. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  1554. /* check for other flag changes */
  1555. if (vsi->current_netdev_flags != vsi->netdev->flags) {
  1556. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  1557. vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
  1558. }
  1559. /* schedule our worker thread which will take care of
  1560. * applying the new filter changes
  1561. */
  1562. i40e_service_event_schedule(vsi->back);
  1563. }
  1564. /**
  1565. * i40e_undo_del_filter_entries - Undo the changes made to MAC filter entries
  1566. * @vsi: Pointer to VSI struct
  1567. * @from: Pointer to list which contains MAC filter entries - changes to
  1568. * those entries needs to be undone.
  1569. *
  1570. * MAC filter entries from this list were slated for deletion.
  1571. **/
  1572. static void i40e_undo_del_filter_entries(struct i40e_vsi *vsi,
  1573. struct hlist_head *from)
  1574. {
  1575. struct i40e_mac_filter *f;
  1576. struct hlist_node *h;
  1577. hlist_for_each_entry_safe(f, h, from, hlist) {
  1578. u64 key = i40e_addr_to_hkey(f->macaddr);
  1579. /* Move the element back into MAC filter list*/
  1580. hlist_del(&f->hlist);
  1581. hash_add(vsi->mac_filter_hash, &f->hlist, key);
  1582. }
  1583. }
  1584. /**
  1585. * i40e_undo_add_filter_entries - Undo the changes made to MAC filter entries
  1586. * @vsi: Pointer to vsi struct
  1587. * @from: Pointer to list which contains MAC filter entries - changes to
  1588. * those entries needs to be undone.
  1589. *
  1590. * MAC filter entries from this list were slated for addition.
  1591. **/
  1592. static void i40e_undo_add_filter_entries(struct i40e_vsi *vsi,
  1593. struct hlist_head *from)
  1594. {
  1595. struct i40e_new_mac_filter *new;
  1596. struct hlist_node *h;
  1597. hlist_for_each_entry_safe(new, h, from, hlist) {
  1598. /* We can simply free the wrapper structure */
  1599. hlist_del(&new->hlist);
  1600. kfree(new);
  1601. }
  1602. }
  1603. /**
  1604. * i40e_next_entry - Get the next non-broadcast filter from a list
  1605. * @next: pointer to filter in list
  1606. *
  1607. * Returns the next non-broadcast filter in the list. Required so that we
  1608. * ignore broadcast filters within the list, since these are not handled via
  1609. * the normal firmware update path.
  1610. */
  1611. static
  1612. struct i40e_new_mac_filter *i40e_next_filter(struct i40e_new_mac_filter *next)
  1613. {
  1614. hlist_for_each_entry_continue(next, hlist) {
  1615. if (!is_broadcast_ether_addr(next->f->macaddr))
  1616. return next;
  1617. }
  1618. return NULL;
  1619. }
  1620. /**
  1621. * i40e_update_filter_state - Update filter state based on return data
  1622. * from firmware
  1623. * @count: Number of filters added
  1624. * @add_list: return data from fw
  1625. * @head: pointer to first filter in current batch
  1626. *
  1627. * MAC filter entries from list were slated to be added to device. Returns
  1628. * number of successful filters. Note that 0 does NOT mean success!
  1629. **/
  1630. static int
  1631. i40e_update_filter_state(int count,
  1632. struct i40e_aqc_add_macvlan_element_data *add_list,
  1633. struct i40e_new_mac_filter *add_head)
  1634. {
  1635. int retval = 0;
  1636. int i;
  1637. for (i = 0; i < count; i++) {
  1638. /* Always check status of each filter. We don't need to check
  1639. * the firmware return status because we pre-set the filter
  1640. * status to I40E_AQC_MM_ERR_NO_RES when sending the filter
  1641. * request to the adminq. Thus, if it no longer matches then
  1642. * we know the filter is active.
  1643. */
  1644. if (add_list[i].match_method == I40E_AQC_MM_ERR_NO_RES) {
  1645. add_head->state = I40E_FILTER_FAILED;
  1646. } else {
  1647. add_head->state = I40E_FILTER_ACTIVE;
  1648. retval++;
  1649. }
  1650. add_head = i40e_next_filter(add_head);
  1651. if (!add_head)
  1652. break;
  1653. }
  1654. return retval;
  1655. }
  1656. /**
  1657. * i40e_aqc_del_filters - Request firmware to delete a set of filters
  1658. * @vsi: ptr to the VSI
  1659. * @vsi_name: name to display in messages
  1660. * @list: the list of filters to send to firmware
  1661. * @num_del: the number of filters to delete
  1662. * @retval: Set to -EIO on failure to delete
  1663. *
  1664. * Send a request to firmware via AdminQ to delete a set of filters. Uses
  1665. * *retval instead of a return value so that success does not force ret_val to
  1666. * be set to 0. This ensures that a sequence of calls to this function
  1667. * preserve the previous value of *retval on successful delete.
  1668. */
  1669. static
  1670. void i40e_aqc_del_filters(struct i40e_vsi *vsi, const char *vsi_name,
  1671. struct i40e_aqc_remove_macvlan_element_data *list,
  1672. int num_del, int *retval)
  1673. {
  1674. struct i40e_hw *hw = &vsi->back->hw;
  1675. i40e_status aq_ret;
  1676. int aq_err;
  1677. aq_ret = i40e_aq_remove_macvlan(hw, vsi->seid, list, num_del, NULL);
  1678. aq_err = hw->aq.asq_last_status;
  1679. /* Explicitly ignore and do not report when firmware returns ENOENT */
  1680. if (aq_ret && !(aq_err == I40E_AQ_RC_ENOENT)) {
  1681. *retval = -EIO;
  1682. dev_info(&vsi->back->pdev->dev,
  1683. "ignoring delete macvlan error on %s, err %s, aq_err %s\n",
  1684. vsi_name, i40e_stat_str(hw, aq_ret),
  1685. i40e_aq_str(hw, aq_err));
  1686. }
  1687. }
  1688. /**
  1689. * i40e_aqc_add_filters - Request firmware to add a set of filters
  1690. * @vsi: ptr to the VSI
  1691. * @vsi_name: name to display in messages
  1692. * @list: the list of filters to send to firmware
  1693. * @add_head: Position in the add hlist
  1694. * @num_add: the number of filters to add
  1695. * @promisc_change: set to true on exit if promiscuous mode was forced on
  1696. *
  1697. * Send a request to firmware via AdminQ to add a chunk of filters. Will set
  1698. * promisc_changed to true if the firmware has run out of space for more
  1699. * filters.
  1700. */
  1701. static
  1702. void i40e_aqc_add_filters(struct i40e_vsi *vsi, const char *vsi_name,
  1703. struct i40e_aqc_add_macvlan_element_data *list,
  1704. struct i40e_new_mac_filter *add_head,
  1705. int num_add, bool *promisc_changed)
  1706. {
  1707. struct i40e_hw *hw = &vsi->back->hw;
  1708. int aq_err, fcnt;
  1709. i40e_aq_add_macvlan(hw, vsi->seid, list, num_add, NULL);
  1710. aq_err = hw->aq.asq_last_status;
  1711. fcnt = i40e_update_filter_state(num_add, list, add_head);
  1712. if (fcnt != num_add) {
  1713. *promisc_changed = true;
  1714. set_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state);
  1715. dev_warn(&vsi->back->pdev->dev,
  1716. "Error %s adding RX filters on %s, promiscuous mode forced on\n",
  1717. i40e_aq_str(hw, aq_err),
  1718. vsi_name);
  1719. }
  1720. }
  1721. /**
  1722. * i40e_aqc_broadcast_filter - Set promiscuous broadcast flags
  1723. * @vsi: pointer to the VSI
  1724. * @f: filter data
  1725. *
  1726. * This function sets or clears the promiscuous broadcast flags for VLAN
  1727. * filters in order to properly receive broadcast frames. Assumes that only
  1728. * broadcast filters are passed.
  1729. *
  1730. * Returns status indicating success or failure;
  1731. **/
  1732. static i40e_status
  1733. i40e_aqc_broadcast_filter(struct i40e_vsi *vsi, const char *vsi_name,
  1734. struct i40e_mac_filter *f)
  1735. {
  1736. bool enable = f->state == I40E_FILTER_NEW;
  1737. struct i40e_hw *hw = &vsi->back->hw;
  1738. i40e_status aq_ret;
  1739. if (f->vlan == I40E_VLAN_ANY) {
  1740. aq_ret = i40e_aq_set_vsi_broadcast(hw,
  1741. vsi->seid,
  1742. enable,
  1743. NULL);
  1744. } else {
  1745. aq_ret = i40e_aq_set_vsi_bc_promisc_on_vlan(hw,
  1746. vsi->seid,
  1747. enable,
  1748. f->vlan,
  1749. NULL);
  1750. }
  1751. if (aq_ret)
  1752. dev_warn(&vsi->back->pdev->dev,
  1753. "Error %s setting broadcast promiscuous mode on %s\n",
  1754. i40e_aq_str(hw, hw->aq.asq_last_status),
  1755. vsi_name);
  1756. return aq_ret;
  1757. }
  1758. /**
  1759. * i40e_sync_vsi_filters - Update the VSI filter list to the HW
  1760. * @vsi: ptr to the VSI
  1761. *
  1762. * Push any outstanding VSI filter changes through the AdminQ.
  1763. *
  1764. * Returns 0 or error value
  1765. **/
  1766. int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
  1767. {
  1768. struct hlist_head tmp_add_list, tmp_del_list;
  1769. struct i40e_mac_filter *f;
  1770. struct i40e_new_mac_filter *new, *add_head = NULL;
  1771. struct i40e_hw *hw = &vsi->back->hw;
  1772. unsigned int failed_filters = 0;
  1773. unsigned int vlan_filters = 0;
  1774. bool promisc_changed = false;
  1775. char vsi_name[16] = "PF";
  1776. int filter_list_len = 0;
  1777. i40e_status aq_ret = 0;
  1778. u32 changed_flags = 0;
  1779. struct hlist_node *h;
  1780. struct i40e_pf *pf;
  1781. int num_add = 0;
  1782. int num_del = 0;
  1783. int retval = 0;
  1784. u16 cmd_flags;
  1785. int list_size;
  1786. int bkt;
  1787. /* empty array typed pointers, kcalloc later */
  1788. struct i40e_aqc_add_macvlan_element_data *add_list;
  1789. struct i40e_aqc_remove_macvlan_element_data *del_list;
  1790. while (test_and_set_bit(__I40E_CONFIG_BUSY, &vsi->state))
  1791. usleep_range(1000, 2000);
  1792. pf = vsi->back;
  1793. if (vsi->netdev) {
  1794. changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
  1795. vsi->current_netdev_flags = vsi->netdev->flags;
  1796. }
  1797. INIT_HLIST_HEAD(&tmp_add_list);
  1798. INIT_HLIST_HEAD(&tmp_del_list);
  1799. if (vsi->type == I40E_VSI_SRIOV)
  1800. snprintf(vsi_name, sizeof(vsi_name) - 1, "VF %d", vsi->vf_id);
  1801. else if (vsi->type != I40E_VSI_MAIN)
  1802. snprintf(vsi_name, sizeof(vsi_name) - 1, "vsi %d", vsi->seid);
  1803. if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
  1804. vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
  1805. spin_lock_bh(&vsi->mac_filter_hash_lock);
  1806. /* Create a list of filters to delete. */
  1807. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  1808. if (f->state == I40E_FILTER_REMOVE) {
  1809. /* Move the element into temporary del_list */
  1810. hash_del(&f->hlist);
  1811. hlist_add_head(&f->hlist, &tmp_del_list);
  1812. /* Avoid counting removed filters */
  1813. continue;
  1814. }
  1815. if (f->state == I40E_FILTER_NEW) {
  1816. /* Create a temporary i40e_new_mac_filter */
  1817. new = kzalloc(sizeof(*new), GFP_ATOMIC);
  1818. if (!new)
  1819. goto err_no_memory_locked;
  1820. /* Store pointer to the real filter */
  1821. new->f = f;
  1822. new->state = f->state;
  1823. /* Add it to the hash list */
  1824. hlist_add_head(&new->hlist, &tmp_add_list);
  1825. }
  1826. /* Count the number of active (current and new) VLAN
  1827. * filters we have now. Does not count filters which
  1828. * are marked for deletion.
  1829. */
  1830. if (f->vlan > 0)
  1831. vlan_filters++;
  1832. }
  1833. retval = i40e_correct_mac_vlan_filters(vsi,
  1834. &tmp_add_list,
  1835. &tmp_del_list,
  1836. vlan_filters);
  1837. if (retval)
  1838. goto err_no_memory_locked;
  1839. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  1840. }
  1841. /* Now process 'del_list' outside the lock */
  1842. if (!hlist_empty(&tmp_del_list)) {
  1843. filter_list_len = hw->aq.asq_buf_size /
  1844. sizeof(struct i40e_aqc_remove_macvlan_element_data);
  1845. list_size = filter_list_len *
  1846. sizeof(struct i40e_aqc_remove_macvlan_element_data);
  1847. del_list = kzalloc(list_size, GFP_ATOMIC);
  1848. if (!del_list)
  1849. goto err_no_memory;
  1850. hlist_for_each_entry_safe(f, h, &tmp_del_list, hlist) {
  1851. cmd_flags = 0;
  1852. /* handle broadcast filters by updating the broadcast
  1853. * promiscuous flag and release filter list.
  1854. */
  1855. if (is_broadcast_ether_addr(f->macaddr)) {
  1856. i40e_aqc_broadcast_filter(vsi, vsi_name, f);
  1857. hlist_del(&f->hlist);
  1858. kfree(f);
  1859. continue;
  1860. }
  1861. /* add to delete list */
  1862. ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
  1863. if (f->vlan == I40E_VLAN_ANY) {
  1864. del_list[num_del].vlan_tag = 0;
  1865. cmd_flags |= I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
  1866. } else {
  1867. del_list[num_del].vlan_tag =
  1868. cpu_to_le16((u16)(f->vlan));
  1869. }
  1870. cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
  1871. del_list[num_del].flags = cmd_flags;
  1872. num_del++;
  1873. /* flush a full buffer */
  1874. if (num_del == filter_list_len) {
  1875. i40e_aqc_del_filters(vsi, vsi_name, del_list,
  1876. num_del, &retval);
  1877. memset(del_list, 0, list_size);
  1878. num_del = 0;
  1879. }
  1880. /* Release memory for MAC filter entries which were
  1881. * synced up with HW.
  1882. */
  1883. hlist_del(&f->hlist);
  1884. kfree(f);
  1885. }
  1886. if (num_del) {
  1887. i40e_aqc_del_filters(vsi, vsi_name, del_list,
  1888. num_del, &retval);
  1889. }
  1890. kfree(del_list);
  1891. del_list = NULL;
  1892. }
  1893. if (!hlist_empty(&tmp_add_list)) {
  1894. /* Do all the adds now. */
  1895. filter_list_len = hw->aq.asq_buf_size /
  1896. sizeof(struct i40e_aqc_add_macvlan_element_data);
  1897. list_size = filter_list_len *
  1898. sizeof(struct i40e_aqc_add_macvlan_element_data);
  1899. add_list = kzalloc(list_size, GFP_ATOMIC);
  1900. if (!add_list)
  1901. goto err_no_memory;
  1902. num_add = 0;
  1903. hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
  1904. if (test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
  1905. &vsi->state)) {
  1906. new->state = I40E_FILTER_FAILED;
  1907. continue;
  1908. }
  1909. /* handle broadcast filters by updating the broadcast
  1910. * promiscuous flag instead of adding a MAC filter.
  1911. */
  1912. if (is_broadcast_ether_addr(new->f->macaddr)) {
  1913. if (i40e_aqc_broadcast_filter(vsi, vsi_name,
  1914. new->f))
  1915. new->state = I40E_FILTER_FAILED;
  1916. else
  1917. new->state = I40E_FILTER_ACTIVE;
  1918. continue;
  1919. }
  1920. /* add to add array */
  1921. if (num_add == 0)
  1922. add_head = new;
  1923. cmd_flags = 0;
  1924. ether_addr_copy(add_list[num_add].mac_addr,
  1925. new->f->macaddr);
  1926. if (new->f->vlan == I40E_VLAN_ANY) {
  1927. add_list[num_add].vlan_tag = 0;
  1928. cmd_flags |= I40E_AQC_MACVLAN_ADD_IGNORE_VLAN;
  1929. } else {
  1930. add_list[num_add].vlan_tag =
  1931. cpu_to_le16((u16)(new->f->vlan));
  1932. }
  1933. add_list[num_add].queue_number = 0;
  1934. /* set invalid match method for later detection */
  1935. add_list[num_add].match_method = I40E_AQC_MM_ERR_NO_RES;
  1936. cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
  1937. add_list[num_add].flags = cpu_to_le16(cmd_flags);
  1938. num_add++;
  1939. /* flush a full buffer */
  1940. if (num_add == filter_list_len) {
  1941. i40e_aqc_add_filters(vsi, vsi_name, add_list,
  1942. add_head, num_add,
  1943. &promisc_changed);
  1944. memset(add_list, 0, list_size);
  1945. num_add = 0;
  1946. }
  1947. }
  1948. if (num_add) {
  1949. i40e_aqc_add_filters(vsi, vsi_name, add_list, add_head,
  1950. num_add, &promisc_changed);
  1951. }
  1952. /* Now move all of the filters from the temp add list back to
  1953. * the VSI's list.
  1954. */
  1955. spin_lock_bh(&vsi->mac_filter_hash_lock);
  1956. hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
  1957. /* Only update the state if we're still NEW */
  1958. if (new->f->state == I40E_FILTER_NEW)
  1959. new->f->state = new->state;
  1960. hlist_del(&new->hlist);
  1961. kfree(new);
  1962. }
  1963. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  1964. kfree(add_list);
  1965. add_list = NULL;
  1966. }
  1967. /* Determine the number of active and failed filters. */
  1968. spin_lock_bh(&vsi->mac_filter_hash_lock);
  1969. vsi->active_filters = 0;
  1970. hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
  1971. if (f->state == I40E_FILTER_ACTIVE)
  1972. vsi->active_filters++;
  1973. else if (f->state == I40E_FILTER_FAILED)
  1974. failed_filters++;
  1975. }
  1976. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  1977. /* If promiscuous mode has changed, we need to calculate a new
  1978. * threshold for when we are safe to exit
  1979. */
  1980. if (promisc_changed)
  1981. vsi->promisc_threshold = (vsi->active_filters * 3) / 4;
  1982. /* Check if we are able to exit overflow promiscuous mode. We can
  1983. * safely exit if we didn't just enter, we no longer have any failed
  1984. * filters, and we have reduced filters below the threshold value.
  1985. */
  1986. if (test_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state) &&
  1987. !promisc_changed && !failed_filters &&
  1988. (vsi->active_filters < vsi->promisc_threshold)) {
  1989. dev_info(&pf->pdev->dev,
  1990. "filter logjam cleared on %s, leaving overflow promiscuous mode\n",
  1991. vsi_name);
  1992. clear_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state);
  1993. promisc_changed = true;
  1994. vsi->promisc_threshold = 0;
  1995. }
  1996. /* if the VF is not trusted do not do promisc */
  1997. if ((vsi->type == I40E_VSI_SRIOV) && !pf->vf[vsi->vf_id].trusted) {
  1998. clear_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state);
  1999. goto out;
  2000. }
  2001. /* check for changes in promiscuous modes */
  2002. if (changed_flags & IFF_ALLMULTI) {
  2003. bool cur_multipromisc;
  2004. cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
  2005. aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
  2006. vsi->seid,
  2007. cur_multipromisc,
  2008. NULL);
  2009. if (aq_ret) {
  2010. retval = i40e_aq_rc_to_posix(aq_ret,
  2011. hw->aq.asq_last_status);
  2012. dev_info(&pf->pdev->dev,
  2013. "set multi promisc failed on %s, err %s aq_err %s\n",
  2014. vsi_name,
  2015. i40e_stat_str(hw, aq_ret),
  2016. i40e_aq_str(hw, hw->aq.asq_last_status));
  2017. }
  2018. }
  2019. if ((changed_flags & IFF_PROMISC) ||
  2020. (promisc_changed &&
  2021. test_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state))) {
  2022. bool cur_promisc;
  2023. cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
  2024. test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
  2025. &vsi->state));
  2026. if ((vsi->type == I40E_VSI_MAIN) &&
  2027. (pf->lan_veb != I40E_NO_VEB) &&
  2028. !(pf->flags & I40E_FLAG_MFP_ENABLED)) {
  2029. /* set defport ON for Main VSI instead of true promisc
  2030. * this way we will get all unicast/multicast and VLAN
  2031. * promisc behavior but will not get VF or VMDq traffic
  2032. * replicated on the Main VSI.
  2033. */
  2034. if (pf->cur_promisc != cur_promisc) {
  2035. pf->cur_promisc = cur_promisc;
  2036. if (cur_promisc)
  2037. aq_ret =
  2038. i40e_aq_set_default_vsi(hw,
  2039. vsi->seid,
  2040. NULL);
  2041. else
  2042. aq_ret =
  2043. i40e_aq_clear_default_vsi(hw,
  2044. vsi->seid,
  2045. NULL);
  2046. if (aq_ret) {
  2047. retval = i40e_aq_rc_to_posix(aq_ret,
  2048. hw->aq.asq_last_status);
  2049. dev_info(&pf->pdev->dev,
  2050. "Set default VSI failed on %s, err %s, aq_err %s\n",
  2051. vsi_name,
  2052. i40e_stat_str(hw, aq_ret),
  2053. i40e_aq_str(hw,
  2054. hw->aq.asq_last_status));
  2055. }
  2056. }
  2057. } else {
  2058. aq_ret = i40e_aq_set_vsi_unicast_promiscuous(
  2059. hw,
  2060. vsi->seid,
  2061. cur_promisc, NULL,
  2062. true);
  2063. if (aq_ret) {
  2064. retval =
  2065. i40e_aq_rc_to_posix(aq_ret,
  2066. hw->aq.asq_last_status);
  2067. dev_info(&pf->pdev->dev,
  2068. "set unicast promisc failed on %s, err %s, aq_err %s\n",
  2069. vsi_name,
  2070. i40e_stat_str(hw, aq_ret),
  2071. i40e_aq_str(hw,
  2072. hw->aq.asq_last_status));
  2073. }
  2074. aq_ret = i40e_aq_set_vsi_multicast_promiscuous(
  2075. hw,
  2076. vsi->seid,
  2077. cur_promisc, NULL);
  2078. if (aq_ret) {
  2079. retval =
  2080. i40e_aq_rc_to_posix(aq_ret,
  2081. hw->aq.asq_last_status);
  2082. dev_info(&pf->pdev->dev,
  2083. "set multicast promisc failed on %s, err %s, aq_err %s\n",
  2084. vsi_name,
  2085. i40e_stat_str(hw, aq_ret),
  2086. i40e_aq_str(hw,
  2087. hw->aq.asq_last_status));
  2088. }
  2089. }
  2090. aq_ret = i40e_aq_set_vsi_broadcast(&vsi->back->hw,
  2091. vsi->seid,
  2092. cur_promisc, NULL);
  2093. if (aq_ret) {
  2094. retval = i40e_aq_rc_to_posix(aq_ret,
  2095. pf->hw.aq.asq_last_status);
  2096. dev_info(&pf->pdev->dev,
  2097. "set brdcast promisc failed, err %s, aq_err %s\n",
  2098. i40e_stat_str(hw, aq_ret),
  2099. i40e_aq_str(hw,
  2100. hw->aq.asq_last_status));
  2101. }
  2102. }
  2103. out:
  2104. /* if something went wrong then set the changed flag so we try again */
  2105. if (retval)
  2106. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  2107. clear_bit(__I40E_CONFIG_BUSY, &vsi->state);
  2108. return retval;
  2109. err_no_memory:
  2110. /* Restore elements on the temporary add and delete lists */
  2111. spin_lock_bh(&vsi->mac_filter_hash_lock);
  2112. err_no_memory_locked:
  2113. i40e_undo_del_filter_entries(vsi, &tmp_del_list);
  2114. i40e_undo_add_filter_entries(vsi, &tmp_add_list);
  2115. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2116. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  2117. clear_bit(__I40E_CONFIG_BUSY, &vsi->state);
  2118. return -ENOMEM;
  2119. }
  2120. /**
  2121. * i40e_sync_filters_subtask - Sync the VSI filter list with HW
  2122. * @pf: board private structure
  2123. **/
  2124. static void i40e_sync_filters_subtask(struct i40e_pf *pf)
  2125. {
  2126. int v;
  2127. if (!pf || !(pf->flags & I40E_FLAG_FILTER_SYNC))
  2128. return;
  2129. pf->flags &= ~I40E_FLAG_FILTER_SYNC;
  2130. for (v = 0; v < pf->num_alloc_vsi; v++) {
  2131. if (pf->vsi[v] &&
  2132. (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED)) {
  2133. int ret = i40e_sync_vsi_filters(pf->vsi[v]);
  2134. if (ret) {
  2135. /* come back and try again later */
  2136. pf->flags |= I40E_FLAG_FILTER_SYNC;
  2137. break;
  2138. }
  2139. }
  2140. }
  2141. }
  2142. /**
  2143. * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
  2144. * @netdev: network interface device structure
  2145. * @new_mtu: new value for maximum frame size
  2146. *
  2147. * Returns 0 on success, negative on failure
  2148. **/
  2149. static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
  2150. {
  2151. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2152. struct i40e_vsi *vsi = np->vsi;
  2153. struct i40e_pf *pf = vsi->back;
  2154. netdev_info(netdev, "changing MTU from %d to %d\n",
  2155. netdev->mtu, new_mtu);
  2156. netdev->mtu = new_mtu;
  2157. if (netif_running(netdev))
  2158. i40e_vsi_reinit_locked(vsi);
  2159. pf->flags |= (I40E_FLAG_SERVICE_CLIENT_REQUESTED |
  2160. I40E_FLAG_CLIENT_L2_CHANGE);
  2161. return 0;
  2162. }
  2163. /**
  2164. * i40e_ioctl - Access the hwtstamp interface
  2165. * @netdev: network interface device structure
  2166. * @ifr: interface request data
  2167. * @cmd: ioctl command
  2168. **/
  2169. int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
  2170. {
  2171. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2172. struct i40e_pf *pf = np->vsi->back;
  2173. switch (cmd) {
  2174. case SIOCGHWTSTAMP:
  2175. return i40e_ptp_get_ts_config(pf, ifr);
  2176. case SIOCSHWTSTAMP:
  2177. return i40e_ptp_set_ts_config(pf, ifr);
  2178. default:
  2179. return -EOPNOTSUPP;
  2180. }
  2181. }
  2182. /**
  2183. * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
  2184. * @vsi: the vsi being adjusted
  2185. **/
  2186. void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
  2187. {
  2188. struct i40e_vsi_context ctxt;
  2189. i40e_status ret;
  2190. if ((vsi->info.valid_sections &
  2191. cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
  2192. ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
  2193. return; /* already enabled */
  2194. vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
  2195. vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
  2196. I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
  2197. ctxt.seid = vsi->seid;
  2198. ctxt.info = vsi->info;
  2199. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  2200. if (ret) {
  2201. dev_info(&vsi->back->pdev->dev,
  2202. "update vlan stripping failed, err %s aq_err %s\n",
  2203. i40e_stat_str(&vsi->back->hw, ret),
  2204. i40e_aq_str(&vsi->back->hw,
  2205. vsi->back->hw.aq.asq_last_status));
  2206. }
  2207. }
  2208. /**
  2209. * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
  2210. * @vsi: the vsi being adjusted
  2211. **/
  2212. void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
  2213. {
  2214. struct i40e_vsi_context ctxt;
  2215. i40e_status ret;
  2216. if ((vsi->info.valid_sections &
  2217. cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
  2218. ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
  2219. I40E_AQ_VSI_PVLAN_EMOD_MASK))
  2220. return; /* already disabled */
  2221. vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
  2222. vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
  2223. I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
  2224. ctxt.seid = vsi->seid;
  2225. ctxt.info = vsi->info;
  2226. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  2227. if (ret) {
  2228. dev_info(&vsi->back->pdev->dev,
  2229. "update vlan stripping failed, err %s aq_err %s\n",
  2230. i40e_stat_str(&vsi->back->hw, ret),
  2231. i40e_aq_str(&vsi->back->hw,
  2232. vsi->back->hw.aq.asq_last_status));
  2233. }
  2234. }
  2235. /**
  2236. * i40e_vlan_rx_register - Setup or shutdown vlan offload
  2237. * @netdev: network interface to be adjusted
  2238. * @features: netdev features to test if VLAN offload is enabled or not
  2239. **/
  2240. static void i40e_vlan_rx_register(struct net_device *netdev, u32 features)
  2241. {
  2242. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2243. struct i40e_vsi *vsi = np->vsi;
  2244. if (features & NETIF_F_HW_VLAN_CTAG_RX)
  2245. i40e_vlan_stripping_enable(vsi);
  2246. else
  2247. i40e_vlan_stripping_disable(vsi);
  2248. }
  2249. /**
  2250. * i40e_add_vlan_all_mac - Add a MAC/VLAN filter for each existing MAC address
  2251. * @vsi: the vsi being configured
  2252. * @vid: vlan id to be added (0 = untagged only , -1 = any)
  2253. *
  2254. * This is a helper function for adding a new MAC/VLAN filter with the
  2255. * specified VLAN for each existing MAC address already in the hash table.
  2256. * This function does *not* perform any accounting to update filters based on
  2257. * VLAN mode.
  2258. *
  2259. * NOTE: this function expects to be called while under the
  2260. * mac_filter_hash_lock
  2261. **/
  2262. int i40e_add_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
  2263. {
  2264. struct i40e_mac_filter *f, *add_f;
  2265. struct hlist_node *h;
  2266. int bkt;
  2267. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  2268. if (f->state == I40E_FILTER_REMOVE)
  2269. continue;
  2270. add_f = i40e_add_filter(vsi, f->macaddr, vid);
  2271. if (!add_f) {
  2272. dev_info(&vsi->back->pdev->dev,
  2273. "Could not add vlan filter %d for %pM\n",
  2274. vid, f->macaddr);
  2275. return -ENOMEM;
  2276. }
  2277. }
  2278. return 0;
  2279. }
  2280. /**
  2281. * i40e_vsi_add_vlan - Add VSI membership for given VLAN
  2282. * @vsi: the VSI being configured
  2283. * @vid: VLAN id to be added
  2284. **/
  2285. int i40e_vsi_add_vlan(struct i40e_vsi *vsi, u16 vid)
  2286. {
  2287. int err;
  2288. if (!vid || vsi->info.pvid)
  2289. return -EINVAL;
  2290. /* Locked once because all functions invoked below iterates list*/
  2291. spin_lock_bh(&vsi->mac_filter_hash_lock);
  2292. err = i40e_add_vlan_all_mac(vsi, vid);
  2293. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2294. if (err)
  2295. return err;
  2296. /* schedule our worker thread which will take care of
  2297. * applying the new filter changes
  2298. */
  2299. i40e_service_event_schedule(vsi->back);
  2300. return 0;
  2301. }
  2302. /**
  2303. * i40e_rm_vlan_all_mac - Remove MAC/VLAN pair for all MAC with the given VLAN
  2304. * @vsi: the vsi being configured
  2305. * @vid: vlan id to be removed (0 = untagged only , -1 = any)
  2306. *
  2307. * This function should be used to remove all VLAN filters which match the
  2308. * given VID. It does not schedule the service event and does not take the
  2309. * mac_filter_hash_lock so it may be combined with other operations under
  2310. * a single invocation of the mac_filter_hash_lock.
  2311. *
  2312. * NOTE: this function expects to be called while under the
  2313. * mac_filter_hash_lock
  2314. */
  2315. void i40e_rm_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
  2316. {
  2317. struct i40e_mac_filter *f;
  2318. struct hlist_node *h;
  2319. int bkt;
  2320. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  2321. if (f->vlan == vid)
  2322. __i40e_del_filter(vsi, f);
  2323. }
  2324. }
  2325. /**
  2326. * i40e_vsi_kill_vlan - Remove VSI membership for given VLAN
  2327. * @vsi: the VSI being configured
  2328. * @vid: VLAN id to be removed
  2329. **/
  2330. void i40e_vsi_kill_vlan(struct i40e_vsi *vsi, u16 vid)
  2331. {
  2332. if (!vid || vsi->info.pvid)
  2333. return;
  2334. spin_lock_bh(&vsi->mac_filter_hash_lock);
  2335. i40e_rm_vlan_all_mac(vsi, vid);
  2336. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2337. /* schedule our worker thread which will take care of
  2338. * applying the new filter changes
  2339. */
  2340. i40e_service_event_schedule(vsi->back);
  2341. }
  2342. /**
  2343. * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
  2344. * @netdev: network interface to be adjusted
  2345. * @vid: vlan id to be added
  2346. *
  2347. * net_device_ops implementation for adding vlan ids
  2348. **/
  2349. static int i40e_vlan_rx_add_vid(struct net_device *netdev,
  2350. __always_unused __be16 proto, u16 vid)
  2351. {
  2352. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2353. struct i40e_vsi *vsi = np->vsi;
  2354. int ret = 0;
  2355. if (vid >= VLAN_N_VID)
  2356. return -EINVAL;
  2357. /* If the network stack called us with vid = 0 then
  2358. * it is asking to receive priority tagged packets with
  2359. * vlan id 0. Our HW receives them by default when configured
  2360. * to receive untagged packets so there is no need to add an
  2361. * extra filter for vlan 0 tagged packets.
  2362. */
  2363. if (vid)
  2364. ret = i40e_vsi_add_vlan(vsi, vid);
  2365. if (!ret)
  2366. set_bit(vid, vsi->active_vlans);
  2367. return ret;
  2368. }
  2369. /**
  2370. * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
  2371. * @netdev: network interface to be adjusted
  2372. * @vid: vlan id to be removed
  2373. *
  2374. * net_device_ops implementation for removing vlan ids
  2375. **/
  2376. static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
  2377. __always_unused __be16 proto, u16 vid)
  2378. {
  2379. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2380. struct i40e_vsi *vsi = np->vsi;
  2381. /* return code is ignored as there is nothing a user
  2382. * can do about failure to remove and a log message was
  2383. * already printed from the other function
  2384. */
  2385. i40e_vsi_kill_vlan(vsi, vid);
  2386. clear_bit(vid, vsi->active_vlans);
  2387. return 0;
  2388. }
  2389. /**
  2390. * i40e_macaddr_init - explicitly write the mac address filters
  2391. *
  2392. * @vsi: pointer to the vsi
  2393. * @macaddr: the MAC address
  2394. *
  2395. * This is needed when the macaddr has been obtained by other
  2396. * means than the default, e.g., from Open Firmware or IDPROM.
  2397. * Returns 0 on success, negative on failure
  2398. **/
  2399. static int i40e_macaddr_init(struct i40e_vsi *vsi, u8 *macaddr)
  2400. {
  2401. int ret;
  2402. struct i40e_aqc_add_macvlan_element_data element;
  2403. ret = i40e_aq_mac_address_write(&vsi->back->hw,
  2404. I40E_AQC_WRITE_TYPE_LAA_WOL,
  2405. macaddr, NULL);
  2406. if (ret) {
  2407. dev_info(&vsi->back->pdev->dev,
  2408. "Addr change for VSI failed: %d\n", ret);
  2409. return -EADDRNOTAVAIL;
  2410. }
  2411. memset(&element, 0, sizeof(element));
  2412. ether_addr_copy(element.mac_addr, macaddr);
  2413. element.flags = cpu_to_le16(I40E_AQC_MACVLAN_ADD_PERFECT_MATCH);
  2414. ret = i40e_aq_add_macvlan(&vsi->back->hw, vsi->seid, &element, 1, NULL);
  2415. if (ret) {
  2416. dev_info(&vsi->back->pdev->dev,
  2417. "add filter failed err %s aq_err %s\n",
  2418. i40e_stat_str(&vsi->back->hw, ret),
  2419. i40e_aq_str(&vsi->back->hw,
  2420. vsi->back->hw.aq.asq_last_status));
  2421. }
  2422. return ret;
  2423. }
  2424. /**
  2425. * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
  2426. * @vsi: the vsi being brought back up
  2427. **/
  2428. static void i40e_restore_vlan(struct i40e_vsi *vsi)
  2429. {
  2430. u16 vid;
  2431. if (!vsi->netdev)
  2432. return;
  2433. i40e_vlan_rx_register(vsi->netdev, vsi->netdev->features);
  2434. for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
  2435. i40e_vlan_rx_add_vid(vsi->netdev, htons(ETH_P_8021Q),
  2436. vid);
  2437. }
  2438. /**
  2439. * i40e_vsi_add_pvid - Add pvid for the VSI
  2440. * @vsi: the vsi being adjusted
  2441. * @vid: the vlan id to set as a PVID
  2442. **/
  2443. int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
  2444. {
  2445. struct i40e_vsi_context ctxt;
  2446. i40e_status ret;
  2447. vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
  2448. vsi->info.pvid = cpu_to_le16(vid);
  2449. vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
  2450. I40E_AQ_VSI_PVLAN_INSERT_PVID |
  2451. I40E_AQ_VSI_PVLAN_EMOD_STR;
  2452. ctxt.seid = vsi->seid;
  2453. ctxt.info = vsi->info;
  2454. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  2455. if (ret) {
  2456. dev_info(&vsi->back->pdev->dev,
  2457. "add pvid failed, err %s aq_err %s\n",
  2458. i40e_stat_str(&vsi->back->hw, ret),
  2459. i40e_aq_str(&vsi->back->hw,
  2460. vsi->back->hw.aq.asq_last_status));
  2461. return -ENOENT;
  2462. }
  2463. return 0;
  2464. }
  2465. /**
  2466. * i40e_vsi_remove_pvid - Remove the pvid from the VSI
  2467. * @vsi: the vsi being adjusted
  2468. *
  2469. * Just use the vlan_rx_register() service to put it back to normal
  2470. **/
  2471. void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
  2472. {
  2473. i40e_vlan_stripping_disable(vsi);
  2474. vsi->info.pvid = 0;
  2475. }
  2476. /**
  2477. * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
  2478. * @vsi: ptr to the VSI
  2479. *
  2480. * If this function returns with an error, then it's possible one or
  2481. * more of the rings is populated (while the rest are not). It is the
  2482. * callers duty to clean those orphaned rings.
  2483. *
  2484. * Return 0 on success, negative on failure
  2485. **/
  2486. static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
  2487. {
  2488. int i, err = 0;
  2489. for (i = 0; i < vsi->num_queue_pairs && !err; i++)
  2490. err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
  2491. return err;
  2492. }
  2493. /**
  2494. * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
  2495. * @vsi: ptr to the VSI
  2496. *
  2497. * Free VSI's transmit software resources
  2498. **/
  2499. static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
  2500. {
  2501. int i;
  2502. if (!vsi->tx_rings)
  2503. return;
  2504. for (i = 0; i < vsi->num_queue_pairs; i++)
  2505. if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
  2506. i40e_free_tx_resources(vsi->tx_rings[i]);
  2507. }
  2508. /**
  2509. * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
  2510. * @vsi: ptr to the VSI
  2511. *
  2512. * If this function returns with an error, then it's possible one or
  2513. * more of the rings is populated (while the rest are not). It is the
  2514. * callers duty to clean those orphaned rings.
  2515. *
  2516. * Return 0 on success, negative on failure
  2517. **/
  2518. static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
  2519. {
  2520. int i, err = 0;
  2521. for (i = 0; i < vsi->num_queue_pairs && !err; i++)
  2522. err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
  2523. return err;
  2524. }
  2525. /**
  2526. * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
  2527. * @vsi: ptr to the VSI
  2528. *
  2529. * Free all receive software resources
  2530. **/
  2531. static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
  2532. {
  2533. int i;
  2534. if (!vsi->rx_rings)
  2535. return;
  2536. for (i = 0; i < vsi->num_queue_pairs; i++)
  2537. if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
  2538. i40e_free_rx_resources(vsi->rx_rings[i]);
  2539. }
  2540. /**
  2541. * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
  2542. * @ring: The Tx ring to configure
  2543. *
  2544. * This enables/disables XPS for a given Tx descriptor ring
  2545. * based on the TCs enabled for the VSI that ring belongs to.
  2546. **/
  2547. static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
  2548. {
  2549. struct i40e_vsi *vsi = ring->vsi;
  2550. cpumask_var_t mask;
  2551. if (!ring->q_vector || !ring->netdev)
  2552. return;
  2553. /* Single TC mode enable XPS */
  2554. if (vsi->tc_config.numtc <= 1) {
  2555. if (!test_and_set_bit(__I40E_TX_XPS_INIT_DONE, &ring->state))
  2556. netif_set_xps_queue(ring->netdev,
  2557. &ring->q_vector->affinity_mask,
  2558. ring->queue_index);
  2559. } else if (alloc_cpumask_var(&mask, GFP_KERNEL)) {
  2560. /* Disable XPS to allow selection based on TC */
  2561. bitmap_zero(cpumask_bits(mask), nr_cpumask_bits);
  2562. netif_set_xps_queue(ring->netdev, mask, ring->queue_index);
  2563. free_cpumask_var(mask);
  2564. }
  2565. /* schedule our worker thread which will take care of
  2566. * applying the new filter changes
  2567. */
  2568. i40e_service_event_schedule(vsi->back);
  2569. }
  2570. /**
  2571. * i40e_configure_tx_ring - Configure a transmit ring context and rest
  2572. * @ring: The Tx ring to configure
  2573. *
  2574. * Configure the Tx descriptor ring in the HMC context.
  2575. **/
  2576. static int i40e_configure_tx_ring(struct i40e_ring *ring)
  2577. {
  2578. struct i40e_vsi *vsi = ring->vsi;
  2579. u16 pf_q = vsi->base_queue + ring->queue_index;
  2580. struct i40e_hw *hw = &vsi->back->hw;
  2581. struct i40e_hmc_obj_txq tx_ctx;
  2582. i40e_status err = 0;
  2583. u32 qtx_ctl = 0;
  2584. /* some ATR related tx ring init */
  2585. if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
  2586. ring->atr_sample_rate = vsi->back->atr_sample_rate;
  2587. ring->atr_count = 0;
  2588. } else {
  2589. ring->atr_sample_rate = 0;
  2590. }
  2591. /* configure XPS */
  2592. i40e_config_xps_tx_ring(ring);
  2593. /* clear the context structure first */
  2594. memset(&tx_ctx, 0, sizeof(tx_ctx));
  2595. tx_ctx.new_context = 1;
  2596. tx_ctx.base = (ring->dma / 128);
  2597. tx_ctx.qlen = ring->count;
  2598. tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
  2599. I40E_FLAG_FD_ATR_ENABLED));
  2600. tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
  2601. /* FDIR VSI tx ring can still use RS bit and writebacks */
  2602. if (vsi->type != I40E_VSI_FDIR)
  2603. tx_ctx.head_wb_ena = 1;
  2604. tx_ctx.head_wb_addr = ring->dma +
  2605. (ring->count * sizeof(struct i40e_tx_desc));
  2606. /* As part of VSI creation/update, FW allocates certain
  2607. * Tx arbitration queue sets for each TC enabled for
  2608. * the VSI. The FW returns the handles to these queue
  2609. * sets as part of the response buffer to Add VSI,
  2610. * Update VSI, etc. AQ commands. It is expected that
  2611. * these queue set handles be associated with the Tx
  2612. * queues by the driver as part of the TX queue context
  2613. * initialization. This has to be done regardless of
  2614. * DCB as by default everything is mapped to TC0.
  2615. */
  2616. tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
  2617. tx_ctx.rdylist_act = 0;
  2618. /* clear the context in the HMC */
  2619. err = i40e_clear_lan_tx_queue_context(hw, pf_q);
  2620. if (err) {
  2621. dev_info(&vsi->back->pdev->dev,
  2622. "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
  2623. ring->queue_index, pf_q, err);
  2624. return -ENOMEM;
  2625. }
  2626. /* set the context in the HMC */
  2627. err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
  2628. if (err) {
  2629. dev_info(&vsi->back->pdev->dev,
  2630. "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
  2631. ring->queue_index, pf_q, err);
  2632. return -ENOMEM;
  2633. }
  2634. /* Now associate this queue with this PCI function */
  2635. if (vsi->type == I40E_VSI_VMDQ2) {
  2636. qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
  2637. qtx_ctl |= ((vsi->id) << I40E_QTX_CTL_VFVM_INDX_SHIFT) &
  2638. I40E_QTX_CTL_VFVM_INDX_MASK;
  2639. } else {
  2640. qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
  2641. }
  2642. qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
  2643. I40E_QTX_CTL_PF_INDX_MASK);
  2644. wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
  2645. i40e_flush(hw);
  2646. /* cache tail off for easier writes later */
  2647. ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
  2648. return 0;
  2649. }
  2650. /**
  2651. * i40e_configure_rx_ring - Configure a receive ring context
  2652. * @ring: The Rx ring to configure
  2653. *
  2654. * Configure the Rx descriptor ring in the HMC context.
  2655. **/
  2656. static int i40e_configure_rx_ring(struct i40e_ring *ring)
  2657. {
  2658. struct i40e_vsi *vsi = ring->vsi;
  2659. u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
  2660. u16 pf_q = vsi->base_queue + ring->queue_index;
  2661. struct i40e_hw *hw = &vsi->back->hw;
  2662. struct i40e_hmc_obj_rxq rx_ctx;
  2663. i40e_status err = 0;
  2664. ring->state = 0;
  2665. /* clear the context structure first */
  2666. memset(&rx_ctx, 0, sizeof(rx_ctx));
  2667. ring->rx_buf_len = vsi->rx_buf_len;
  2668. rx_ctx.dbuff = DIV_ROUND_UP(ring->rx_buf_len,
  2669. BIT_ULL(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->back->flags & I40E_FLAG_LEGACY_RX)) {
  2736. vsi->max_frame = I40E_MAX_RXBUFFER;
  2737. vsi->rx_buf_len = I40E_RXBUFFER_2048;
  2738. #if (PAGE_SIZE < 8192)
  2739. } else if (vsi->netdev->mtu <= ETH_DATA_LEN) {
  2740. vsi->max_frame = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
  2741. vsi->rx_buf_len = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
  2742. #endif
  2743. } else {
  2744. vsi->max_frame = I40E_MAX_RXBUFFER;
  2745. vsi->rx_buf_len = I40E_RXBUFFER_2048;
  2746. }
  2747. /* set up individual rings */
  2748. for (i = 0; i < vsi->num_queue_pairs && !err; i++)
  2749. err = i40e_configure_rx_ring(vsi->rx_rings[i]);
  2750. return err;
  2751. }
  2752. /**
  2753. * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
  2754. * @vsi: ptr to the VSI
  2755. **/
  2756. static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
  2757. {
  2758. struct i40e_ring *tx_ring, *rx_ring;
  2759. u16 qoffset, qcount;
  2760. int i, n;
  2761. if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
  2762. /* Reset the TC information */
  2763. for (i = 0; i < vsi->num_queue_pairs; i++) {
  2764. rx_ring = vsi->rx_rings[i];
  2765. tx_ring = vsi->tx_rings[i];
  2766. rx_ring->dcb_tc = 0;
  2767. tx_ring->dcb_tc = 0;
  2768. }
  2769. }
  2770. for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
  2771. if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
  2772. continue;
  2773. qoffset = vsi->tc_config.tc_info[n].qoffset;
  2774. qcount = vsi->tc_config.tc_info[n].qcount;
  2775. for (i = qoffset; i < (qoffset + qcount); i++) {
  2776. rx_ring = vsi->rx_rings[i];
  2777. tx_ring = vsi->tx_rings[i];
  2778. rx_ring->dcb_tc = n;
  2779. tx_ring->dcb_tc = n;
  2780. }
  2781. }
  2782. }
  2783. /**
  2784. * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
  2785. * @vsi: ptr to the VSI
  2786. **/
  2787. static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
  2788. {
  2789. struct i40e_pf *pf = vsi->back;
  2790. int err;
  2791. if (vsi->netdev)
  2792. i40e_set_rx_mode(vsi->netdev);
  2793. if (!!(pf->flags & I40E_FLAG_PF_MAC)) {
  2794. err = i40e_macaddr_init(vsi, pf->hw.mac.addr);
  2795. if (err) {
  2796. dev_warn(&pf->pdev->dev,
  2797. "could not set up macaddr; err %d\n", err);
  2798. }
  2799. }
  2800. }
  2801. /**
  2802. * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
  2803. * @vsi: Pointer to the targeted VSI
  2804. *
  2805. * This function replays the hlist on the hw where all the SB Flow Director
  2806. * filters were saved.
  2807. **/
  2808. static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
  2809. {
  2810. struct i40e_fdir_filter *filter;
  2811. struct i40e_pf *pf = vsi->back;
  2812. struct hlist_node *node;
  2813. if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
  2814. return;
  2815. /* Reset FDir counters as we're replaying all existing filters */
  2816. pf->fd_tcp4_filter_cnt = 0;
  2817. pf->fd_udp4_filter_cnt = 0;
  2818. pf->fd_sctp4_filter_cnt = 0;
  2819. pf->fd_ip4_filter_cnt = 0;
  2820. hlist_for_each_entry_safe(filter, node,
  2821. &pf->fdir_filter_list, fdir_node) {
  2822. i40e_add_del_fdir(vsi, filter, true);
  2823. }
  2824. }
  2825. /**
  2826. * i40e_vsi_configure - Set up the VSI for action
  2827. * @vsi: the VSI being configured
  2828. **/
  2829. static int i40e_vsi_configure(struct i40e_vsi *vsi)
  2830. {
  2831. int err;
  2832. i40e_set_vsi_rx_mode(vsi);
  2833. i40e_restore_vlan(vsi);
  2834. i40e_vsi_config_dcb_rings(vsi);
  2835. err = i40e_vsi_configure_tx(vsi);
  2836. if (!err)
  2837. err = i40e_vsi_configure_rx(vsi);
  2838. return err;
  2839. }
  2840. /**
  2841. * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
  2842. * @vsi: the VSI being configured
  2843. **/
  2844. static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
  2845. {
  2846. struct i40e_pf *pf = vsi->back;
  2847. struct i40e_hw *hw = &pf->hw;
  2848. u16 vector;
  2849. int i, q;
  2850. u32 qp;
  2851. /* The interrupt indexing is offset by 1 in the PFINT_ITRn
  2852. * and PFINT_LNKLSTn registers, e.g.:
  2853. * PFINT_ITRn[0..n-1] gets msix-1..msix-n (qpair interrupts)
  2854. */
  2855. qp = vsi->base_queue;
  2856. vector = vsi->base_vector;
  2857. for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
  2858. struct i40e_q_vector *q_vector = vsi->q_vectors[i];
  2859. q_vector->itr_countdown = ITR_COUNTDOWN_START;
  2860. q_vector->rx.itr = ITR_TO_REG(vsi->rx_rings[i]->rx_itr_setting);
  2861. q_vector->rx.latency_range = I40E_LOW_LATENCY;
  2862. wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
  2863. q_vector->rx.itr);
  2864. q_vector->tx.itr = ITR_TO_REG(vsi->tx_rings[i]->tx_itr_setting);
  2865. q_vector->tx.latency_range = I40E_LOW_LATENCY;
  2866. wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
  2867. q_vector->tx.itr);
  2868. wr32(hw, I40E_PFINT_RATEN(vector - 1),
  2869. i40e_intrl_usec_to_reg(vsi->int_rate_limit));
  2870. /* Linked list for the queuepairs assigned to this vector */
  2871. wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
  2872. for (q = 0; q < q_vector->num_ringpairs; q++) {
  2873. u32 val;
  2874. val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
  2875. (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
  2876. (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
  2877. (qp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
  2878. (I40E_QUEUE_TYPE_TX
  2879. << I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
  2880. wr32(hw, I40E_QINT_RQCTL(qp), val);
  2881. val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  2882. (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
  2883. (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
  2884. ((qp+1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT)|
  2885. (I40E_QUEUE_TYPE_RX
  2886. << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
  2887. /* Terminate the linked list */
  2888. if (q == (q_vector->num_ringpairs - 1))
  2889. val |= (I40E_QUEUE_END_OF_LIST
  2890. << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
  2891. wr32(hw, I40E_QINT_TQCTL(qp), val);
  2892. qp++;
  2893. }
  2894. }
  2895. i40e_flush(hw);
  2896. }
  2897. /**
  2898. * i40e_enable_misc_int_causes - enable the non-queue interrupts
  2899. * @hw: ptr to the hardware info
  2900. **/
  2901. static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
  2902. {
  2903. struct i40e_hw *hw = &pf->hw;
  2904. u32 val;
  2905. /* clear things first */
  2906. wr32(hw, I40E_PFINT_ICR0_ENA, 0); /* disable all */
  2907. rd32(hw, I40E_PFINT_ICR0); /* read to clear */
  2908. val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK |
  2909. I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK |
  2910. I40E_PFINT_ICR0_ENA_GRST_MASK |
  2911. I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
  2912. I40E_PFINT_ICR0_ENA_GPIO_MASK |
  2913. I40E_PFINT_ICR0_ENA_HMC_ERR_MASK |
  2914. I40E_PFINT_ICR0_ENA_VFLR_MASK |
  2915. I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
  2916. if (pf->flags & I40E_FLAG_IWARP_ENABLED)
  2917. val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
  2918. if (pf->flags & I40E_FLAG_PTP)
  2919. val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
  2920. wr32(hw, I40E_PFINT_ICR0_ENA, val);
  2921. /* SW_ITR_IDX = 0, but don't change INTENA */
  2922. wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
  2923. I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
  2924. /* OTHER_ITR_IDX = 0 */
  2925. wr32(hw, I40E_PFINT_STAT_CTL0, 0);
  2926. }
  2927. /**
  2928. * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
  2929. * @vsi: the VSI being configured
  2930. **/
  2931. static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
  2932. {
  2933. struct i40e_q_vector *q_vector = vsi->q_vectors[0];
  2934. struct i40e_pf *pf = vsi->back;
  2935. struct i40e_hw *hw = &pf->hw;
  2936. u32 val;
  2937. /* set the ITR configuration */
  2938. q_vector->itr_countdown = ITR_COUNTDOWN_START;
  2939. q_vector->rx.itr = ITR_TO_REG(vsi->rx_rings[0]->rx_itr_setting);
  2940. q_vector->rx.latency_range = I40E_LOW_LATENCY;
  2941. wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.itr);
  2942. q_vector->tx.itr = ITR_TO_REG(vsi->tx_rings[0]->tx_itr_setting);
  2943. q_vector->tx.latency_range = I40E_LOW_LATENCY;
  2944. wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.itr);
  2945. i40e_enable_misc_int_causes(pf);
  2946. /* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
  2947. wr32(hw, I40E_PFINT_LNKLST0, 0);
  2948. /* Associate the queue pair to the vector and enable the queue int */
  2949. val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
  2950. (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
  2951. (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
  2952. wr32(hw, I40E_QINT_RQCTL(0), val);
  2953. val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  2954. (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
  2955. (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
  2956. wr32(hw, I40E_QINT_TQCTL(0), val);
  2957. i40e_flush(hw);
  2958. }
  2959. /**
  2960. * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
  2961. * @pf: board private structure
  2962. **/
  2963. void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
  2964. {
  2965. struct i40e_hw *hw = &pf->hw;
  2966. wr32(hw, I40E_PFINT_DYN_CTL0,
  2967. I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
  2968. i40e_flush(hw);
  2969. }
  2970. /**
  2971. * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
  2972. * @pf: board private structure
  2973. * @clearpba: true when all pending interrupt events should be cleared
  2974. **/
  2975. void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf, bool clearpba)
  2976. {
  2977. struct i40e_hw *hw = &pf->hw;
  2978. u32 val;
  2979. val = I40E_PFINT_DYN_CTL0_INTENA_MASK |
  2980. (clearpba ? I40E_PFINT_DYN_CTL0_CLEARPBA_MASK : 0) |
  2981. (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
  2982. wr32(hw, I40E_PFINT_DYN_CTL0, val);
  2983. i40e_flush(hw);
  2984. }
  2985. /**
  2986. * i40e_msix_clean_rings - MSIX mode Interrupt Handler
  2987. * @irq: interrupt number
  2988. * @data: pointer to a q_vector
  2989. **/
  2990. static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
  2991. {
  2992. struct i40e_q_vector *q_vector = data;
  2993. if (!q_vector->tx.ring && !q_vector->rx.ring)
  2994. return IRQ_HANDLED;
  2995. napi_schedule_irqoff(&q_vector->napi);
  2996. return IRQ_HANDLED;
  2997. }
  2998. /**
  2999. * i40e_irq_affinity_notify - Callback for affinity changes
  3000. * @notify: context as to what irq was changed
  3001. * @mask: the new affinity mask
  3002. *
  3003. * This is a callback function used by the irq_set_affinity_notifier function
  3004. * so that we may register to receive changes to the irq affinity masks.
  3005. **/
  3006. static void i40e_irq_affinity_notify(struct irq_affinity_notify *notify,
  3007. const cpumask_t *mask)
  3008. {
  3009. struct i40e_q_vector *q_vector =
  3010. container_of(notify, struct i40e_q_vector, affinity_notify);
  3011. q_vector->affinity_mask = *mask;
  3012. }
  3013. /**
  3014. * i40e_irq_affinity_release - Callback for affinity notifier release
  3015. * @ref: internal core kernel usage
  3016. *
  3017. * This is a callback function used by the irq_set_affinity_notifier function
  3018. * to inform the current notification subscriber that they will no longer
  3019. * receive notifications.
  3020. **/
  3021. static void i40e_irq_affinity_release(struct kref *ref) {}
  3022. /**
  3023. * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
  3024. * @vsi: the VSI being configured
  3025. * @basename: name for the vector
  3026. *
  3027. * Allocates MSI-X vectors and requests interrupts from the kernel.
  3028. **/
  3029. static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
  3030. {
  3031. int q_vectors = vsi->num_q_vectors;
  3032. struct i40e_pf *pf = vsi->back;
  3033. int base = vsi->base_vector;
  3034. int rx_int_idx = 0;
  3035. int tx_int_idx = 0;
  3036. int vector, err;
  3037. int irq_num;
  3038. for (vector = 0; vector < q_vectors; vector++) {
  3039. struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
  3040. irq_num = pf->msix_entries[base + vector].vector;
  3041. if (q_vector->tx.ring && q_vector->rx.ring) {
  3042. snprintf(q_vector->name, sizeof(q_vector->name) - 1,
  3043. "%s-%s-%d", basename, "TxRx", rx_int_idx++);
  3044. tx_int_idx++;
  3045. } else if (q_vector->rx.ring) {
  3046. snprintf(q_vector->name, sizeof(q_vector->name) - 1,
  3047. "%s-%s-%d", basename, "rx", rx_int_idx++);
  3048. } else if (q_vector->tx.ring) {
  3049. snprintf(q_vector->name, sizeof(q_vector->name) - 1,
  3050. "%s-%s-%d", basename, "tx", tx_int_idx++);
  3051. } else {
  3052. /* skip this unused q_vector */
  3053. continue;
  3054. }
  3055. err = request_irq(irq_num,
  3056. vsi->irq_handler,
  3057. 0,
  3058. q_vector->name,
  3059. q_vector);
  3060. if (err) {
  3061. dev_info(&pf->pdev->dev,
  3062. "MSIX request_irq failed, error: %d\n", err);
  3063. goto free_queue_irqs;
  3064. }
  3065. /* register for affinity change notifications */
  3066. q_vector->affinity_notify.notify = i40e_irq_affinity_notify;
  3067. q_vector->affinity_notify.release = i40e_irq_affinity_release;
  3068. irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
  3069. /* assign the mask for this irq */
  3070. irq_set_affinity_hint(irq_num, &q_vector->affinity_mask);
  3071. }
  3072. vsi->irqs_ready = true;
  3073. return 0;
  3074. free_queue_irqs:
  3075. while (vector) {
  3076. vector--;
  3077. irq_num = pf->msix_entries[base + vector].vector;
  3078. irq_set_affinity_notifier(irq_num, NULL);
  3079. irq_set_affinity_hint(irq_num, NULL);
  3080. free_irq(irq_num, &vsi->q_vectors[vector]);
  3081. }
  3082. return err;
  3083. }
  3084. /**
  3085. * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
  3086. * @vsi: the VSI being un-configured
  3087. **/
  3088. static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
  3089. {
  3090. struct i40e_pf *pf = vsi->back;
  3091. struct i40e_hw *hw = &pf->hw;
  3092. int base = vsi->base_vector;
  3093. int i;
  3094. for (i = 0; i < vsi->num_queue_pairs; i++) {
  3095. wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), 0);
  3096. wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), 0);
  3097. }
  3098. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  3099. for (i = vsi->base_vector;
  3100. i < (vsi->num_q_vectors + vsi->base_vector); i++)
  3101. wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
  3102. i40e_flush(hw);
  3103. for (i = 0; i < vsi->num_q_vectors; i++)
  3104. synchronize_irq(pf->msix_entries[i + base].vector);
  3105. } else {
  3106. /* Legacy and MSI mode - this stops all interrupt handling */
  3107. wr32(hw, I40E_PFINT_ICR0_ENA, 0);
  3108. wr32(hw, I40E_PFINT_DYN_CTL0, 0);
  3109. i40e_flush(hw);
  3110. synchronize_irq(pf->pdev->irq);
  3111. }
  3112. }
  3113. /**
  3114. * i40e_vsi_enable_irq - Enable IRQ for the given VSI
  3115. * @vsi: the VSI being configured
  3116. **/
  3117. static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
  3118. {
  3119. struct i40e_pf *pf = vsi->back;
  3120. int i;
  3121. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  3122. for (i = 0; i < vsi->num_q_vectors; i++)
  3123. i40e_irq_dynamic_enable(vsi, i);
  3124. } else {
  3125. i40e_irq_dynamic_enable_icr0(pf, true);
  3126. }
  3127. i40e_flush(&pf->hw);
  3128. return 0;
  3129. }
  3130. /**
  3131. * i40e_stop_misc_vector - Stop the vector that handles non-queue events
  3132. * @pf: board private structure
  3133. **/
  3134. static void i40e_stop_misc_vector(struct i40e_pf *pf)
  3135. {
  3136. /* Disable ICR 0 */
  3137. wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
  3138. i40e_flush(&pf->hw);
  3139. }
  3140. /**
  3141. * i40e_intr - MSI/Legacy and non-queue interrupt handler
  3142. * @irq: interrupt number
  3143. * @data: pointer to a q_vector
  3144. *
  3145. * This is the handler used for all MSI/Legacy interrupts, and deals
  3146. * with both queue and non-queue interrupts. This is also used in
  3147. * MSIX mode to handle the non-queue interrupts.
  3148. **/
  3149. static irqreturn_t i40e_intr(int irq, void *data)
  3150. {
  3151. struct i40e_pf *pf = (struct i40e_pf *)data;
  3152. struct i40e_hw *hw = &pf->hw;
  3153. irqreturn_t ret = IRQ_NONE;
  3154. u32 icr0, icr0_remaining;
  3155. u32 val, ena_mask;
  3156. icr0 = rd32(hw, I40E_PFINT_ICR0);
  3157. ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
  3158. /* if sharing a legacy IRQ, we might get called w/o an intr pending */
  3159. if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
  3160. goto enable_intr;
  3161. /* if interrupt but no bits showing, must be SWINT */
  3162. if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
  3163. (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
  3164. pf->sw_int_count++;
  3165. if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
  3166. (ena_mask & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
  3167. ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
  3168. icr0 &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
  3169. dev_dbg(&pf->pdev->dev, "cleared PE_CRITERR\n");
  3170. }
  3171. /* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
  3172. if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
  3173. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  3174. struct i40e_q_vector *q_vector = vsi->q_vectors[0];
  3175. /* We do not have a way to disarm Queue causes while leaving
  3176. * interrupt enabled for all other causes, ideally
  3177. * interrupt should be disabled while we are in NAPI but
  3178. * this is not a performance path and napi_schedule()
  3179. * can deal with rescheduling.
  3180. */
  3181. if (!test_bit(__I40E_DOWN, &pf->state))
  3182. napi_schedule_irqoff(&q_vector->napi);
  3183. }
  3184. if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
  3185. ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
  3186. set_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
  3187. i40e_debug(&pf->hw, I40E_DEBUG_NVM, "AdminQ event\n");
  3188. }
  3189. if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
  3190. ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
  3191. set_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
  3192. }
  3193. if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
  3194. ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
  3195. set_bit(__I40E_VFLR_EVENT_PENDING, &pf->state);
  3196. }
  3197. if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
  3198. if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
  3199. set_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
  3200. ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
  3201. val = rd32(hw, I40E_GLGEN_RSTAT);
  3202. val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
  3203. >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
  3204. if (val == I40E_RESET_CORER) {
  3205. pf->corer_count++;
  3206. } else if (val == I40E_RESET_GLOBR) {
  3207. pf->globr_count++;
  3208. } else if (val == I40E_RESET_EMPR) {
  3209. pf->empr_count++;
  3210. set_bit(__I40E_EMP_RESET_INTR_RECEIVED, &pf->state);
  3211. }
  3212. }
  3213. if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
  3214. icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
  3215. dev_info(&pf->pdev->dev, "HMC error interrupt\n");
  3216. dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
  3217. rd32(hw, I40E_PFHMC_ERRORINFO),
  3218. rd32(hw, I40E_PFHMC_ERRORDATA));
  3219. }
  3220. if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
  3221. u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
  3222. if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK) {
  3223. icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
  3224. i40e_ptp_tx_hwtstamp(pf);
  3225. }
  3226. }
  3227. /* If a critical error is pending we have no choice but to reset the
  3228. * device.
  3229. * Report and mask out any remaining unexpected interrupts.
  3230. */
  3231. icr0_remaining = icr0 & ena_mask;
  3232. if (icr0_remaining) {
  3233. dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
  3234. icr0_remaining);
  3235. if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
  3236. (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
  3237. (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
  3238. dev_info(&pf->pdev->dev, "device will be reset\n");
  3239. set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
  3240. i40e_service_event_schedule(pf);
  3241. }
  3242. ena_mask &= ~icr0_remaining;
  3243. }
  3244. ret = IRQ_HANDLED;
  3245. enable_intr:
  3246. /* re-enable interrupt causes */
  3247. wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
  3248. if (!test_bit(__I40E_DOWN, &pf->state)) {
  3249. i40e_service_event_schedule(pf);
  3250. i40e_irq_dynamic_enable_icr0(pf, false);
  3251. }
  3252. return ret;
  3253. }
  3254. /**
  3255. * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
  3256. * @tx_ring: tx ring to clean
  3257. * @budget: how many cleans we're allowed
  3258. *
  3259. * Returns true if there's any budget left (e.g. the clean is finished)
  3260. **/
  3261. static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
  3262. {
  3263. struct i40e_vsi *vsi = tx_ring->vsi;
  3264. u16 i = tx_ring->next_to_clean;
  3265. struct i40e_tx_buffer *tx_buf;
  3266. struct i40e_tx_desc *tx_desc;
  3267. tx_buf = &tx_ring->tx_bi[i];
  3268. tx_desc = I40E_TX_DESC(tx_ring, i);
  3269. i -= tx_ring->count;
  3270. do {
  3271. struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
  3272. /* if next_to_watch is not set then there is no work pending */
  3273. if (!eop_desc)
  3274. break;
  3275. /* prevent any other reads prior to eop_desc */
  3276. read_barrier_depends();
  3277. /* if the descriptor isn't done, no work yet to do */
  3278. if (!(eop_desc->cmd_type_offset_bsz &
  3279. cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
  3280. break;
  3281. /* clear next_to_watch to prevent false hangs */
  3282. tx_buf->next_to_watch = NULL;
  3283. tx_desc->buffer_addr = 0;
  3284. tx_desc->cmd_type_offset_bsz = 0;
  3285. /* move past filter desc */
  3286. tx_buf++;
  3287. tx_desc++;
  3288. i++;
  3289. if (unlikely(!i)) {
  3290. i -= tx_ring->count;
  3291. tx_buf = tx_ring->tx_bi;
  3292. tx_desc = I40E_TX_DESC(tx_ring, 0);
  3293. }
  3294. /* unmap skb header data */
  3295. dma_unmap_single(tx_ring->dev,
  3296. dma_unmap_addr(tx_buf, dma),
  3297. dma_unmap_len(tx_buf, len),
  3298. DMA_TO_DEVICE);
  3299. if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
  3300. kfree(tx_buf->raw_buf);
  3301. tx_buf->raw_buf = NULL;
  3302. tx_buf->tx_flags = 0;
  3303. tx_buf->next_to_watch = NULL;
  3304. dma_unmap_len_set(tx_buf, len, 0);
  3305. tx_desc->buffer_addr = 0;
  3306. tx_desc->cmd_type_offset_bsz = 0;
  3307. /* move us past the eop_desc for start of next FD desc */
  3308. tx_buf++;
  3309. tx_desc++;
  3310. i++;
  3311. if (unlikely(!i)) {
  3312. i -= tx_ring->count;
  3313. tx_buf = tx_ring->tx_bi;
  3314. tx_desc = I40E_TX_DESC(tx_ring, 0);
  3315. }
  3316. /* update budget accounting */
  3317. budget--;
  3318. } while (likely(budget));
  3319. i += tx_ring->count;
  3320. tx_ring->next_to_clean = i;
  3321. if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED)
  3322. i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
  3323. return budget > 0;
  3324. }
  3325. /**
  3326. * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
  3327. * @irq: interrupt number
  3328. * @data: pointer to a q_vector
  3329. **/
  3330. static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
  3331. {
  3332. struct i40e_q_vector *q_vector = data;
  3333. struct i40e_vsi *vsi;
  3334. if (!q_vector->tx.ring)
  3335. return IRQ_HANDLED;
  3336. vsi = q_vector->tx.ring->vsi;
  3337. i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
  3338. return IRQ_HANDLED;
  3339. }
  3340. /**
  3341. * i40e_map_vector_to_qp - Assigns the queue pair to the vector
  3342. * @vsi: the VSI being configured
  3343. * @v_idx: vector index
  3344. * @qp_idx: queue pair index
  3345. **/
  3346. static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
  3347. {
  3348. struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
  3349. struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
  3350. struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
  3351. tx_ring->q_vector = q_vector;
  3352. tx_ring->next = q_vector->tx.ring;
  3353. q_vector->tx.ring = tx_ring;
  3354. q_vector->tx.count++;
  3355. rx_ring->q_vector = q_vector;
  3356. rx_ring->next = q_vector->rx.ring;
  3357. q_vector->rx.ring = rx_ring;
  3358. q_vector->rx.count++;
  3359. }
  3360. /**
  3361. * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
  3362. * @vsi: the VSI being configured
  3363. *
  3364. * This function maps descriptor rings to the queue-specific vectors
  3365. * we were allotted through the MSI-X enabling code. Ideally, we'd have
  3366. * one vector per queue pair, but on a constrained vector budget, we
  3367. * group the queue pairs as "efficiently" as possible.
  3368. **/
  3369. static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
  3370. {
  3371. int qp_remaining = vsi->num_queue_pairs;
  3372. int q_vectors = vsi->num_q_vectors;
  3373. int num_ringpairs;
  3374. int v_start = 0;
  3375. int qp_idx = 0;
  3376. /* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
  3377. * group them so there are multiple queues per vector.
  3378. * It is also important to go through all the vectors available to be
  3379. * sure that if we don't use all the vectors, that the remaining vectors
  3380. * are cleared. This is especially important when decreasing the
  3381. * number of queues in use.
  3382. */
  3383. for (; v_start < q_vectors; v_start++) {
  3384. struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
  3385. num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
  3386. q_vector->num_ringpairs = num_ringpairs;
  3387. q_vector->rx.count = 0;
  3388. q_vector->tx.count = 0;
  3389. q_vector->rx.ring = NULL;
  3390. q_vector->tx.ring = NULL;
  3391. while (num_ringpairs--) {
  3392. i40e_map_vector_to_qp(vsi, v_start, qp_idx);
  3393. qp_idx++;
  3394. qp_remaining--;
  3395. }
  3396. }
  3397. }
  3398. /**
  3399. * i40e_vsi_request_irq - Request IRQ from the OS
  3400. * @vsi: the VSI being configured
  3401. * @basename: name for the vector
  3402. **/
  3403. static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
  3404. {
  3405. struct i40e_pf *pf = vsi->back;
  3406. int err;
  3407. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  3408. err = i40e_vsi_request_irq_msix(vsi, basename);
  3409. else if (pf->flags & I40E_FLAG_MSI_ENABLED)
  3410. err = request_irq(pf->pdev->irq, i40e_intr, 0,
  3411. pf->int_name, pf);
  3412. else
  3413. err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
  3414. pf->int_name, pf);
  3415. if (err)
  3416. dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
  3417. return err;
  3418. }
  3419. #ifdef CONFIG_NET_POLL_CONTROLLER
  3420. /**
  3421. * i40e_netpoll - A Polling 'interrupt' handler
  3422. * @netdev: network interface device structure
  3423. *
  3424. * This is used by netconsole to send skbs without having to re-enable
  3425. * interrupts. It's not called while the normal interrupt routine is executing.
  3426. **/
  3427. static void i40e_netpoll(struct net_device *netdev)
  3428. {
  3429. struct i40e_netdev_priv *np = netdev_priv(netdev);
  3430. struct i40e_vsi *vsi = np->vsi;
  3431. struct i40e_pf *pf = vsi->back;
  3432. int i;
  3433. /* if interface is down do nothing */
  3434. if (test_bit(__I40E_DOWN, &vsi->state))
  3435. return;
  3436. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  3437. for (i = 0; i < vsi->num_q_vectors; i++)
  3438. i40e_msix_clean_rings(0, vsi->q_vectors[i]);
  3439. } else {
  3440. i40e_intr(pf->pdev->irq, netdev);
  3441. }
  3442. }
  3443. #endif
  3444. /**
  3445. * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
  3446. * @pf: the PF being configured
  3447. * @pf_q: the PF queue
  3448. * @enable: enable or disable state of the queue
  3449. *
  3450. * This routine will wait for the given Tx queue of the PF to reach the
  3451. * enabled or disabled state.
  3452. * Returns -ETIMEDOUT in case of failing to reach the requested state after
  3453. * multiple retries; else will return 0 in case of success.
  3454. **/
  3455. static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
  3456. {
  3457. int i;
  3458. u32 tx_reg;
  3459. for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
  3460. tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
  3461. if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
  3462. break;
  3463. usleep_range(10, 20);
  3464. }
  3465. if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
  3466. return -ETIMEDOUT;
  3467. return 0;
  3468. }
  3469. /**
  3470. * i40e_vsi_control_tx - Start or stop a VSI's rings
  3471. * @vsi: the VSI being configured
  3472. * @enable: start or stop the rings
  3473. **/
  3474. static int i40e_vsi_control_tx(struct i40e_vsi *vsi, bool enable)
  3475. {
  3476. struct i40e_pf *pf = vsi->back;
  3477. struct i40e_hw *hw = &pf->hw;
  3478. int i, j, pf_q, ret = 0;
  3479. u32 tx_reg;
  3480. pf_q = vsi->base_queue;
  3481. for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
  3482. /* warn the TX unit of coming changes */
  3483. i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
  3484. if (!enable)
  3485. usleep_range(10, 20);
  3486. for (j = 0; j < 50; j++) {
  3487. tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
  3488. if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
  3489. ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
  3490. break;
  3491. usleep_range(1000, 2000);
  3492. }
  3493. /* Skip if the queue is already in the requested state */
  3494. if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
  3495. continue;
  3496. /* turn on/off the queue */
  3497. if (enable) {
  3498. wr32(hw, I40E_QTX_HEAD(pf_q), 0);
  3499. tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
  3500. } else {
  3501. tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
  3502. }
  3503. wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
  3504. /* No waiting for the Tx queue to disable */
  3505. if (!enable && test_bit(__I40E_PORT_TX_SUSPENDED, &pf->state))
  3506. continue;
  3507. /* wait for the change to finish */
  3508. ret = i40e_pf_txq_wait(pf, pf_q, enable);
  3509. if (ret) {
  3510. dev_info(&pf->pdev->dev,
  3511. "VSI seid %d Tx ring %d %sable timeout\n",
  3512. vsi->seid, pf_q, (enable ? "en" : "dis"));
  3513. break;
  3514. }
  3515. }
  3516. return ret;
  3517. }
  3518. /**
  3519. * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
  3520. * @pf: the PF being configured
  3521. * @pf_q: the PF queue
  3522. * @enable: enable or disable state of the queue
  3523. *
  3524. * This routine will wait for the given Rx queue of the PF to reach the
  3525. * enabled or disabled state.
  3526. * Returns -ETIMEDOUT in case of failing to reach the requested state after
  3527. * multiple retries; else will return 0 in case of success.
  3528. **/
  3529. static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
  3530. {
  3531. int i;
  3532. u32 rx_reg;
  3533. for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
  3534. rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
  3535. if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
  3536. break;
  3537. usleep_range(10, 20);
  3538. }
  3539. if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
  3540. return -ETIMEDOUT;
  3541. return 0;
  3542. }
  3543. /**
  3544. * i40e_vsi_control_rx - Start or stop a VSI's rings
  3545. * @vsi: the VSI being configured
  3546. * @enable: start or stop the rings
  3547. **/
  3548. static int i40e_vsi_control_rx(struct i40e_vsi *vsi, bool enable)
  3549. {
  3550. struct i40e_pf *pf = vsi->back;
  3551. struct i40e_hw *hw = &pf->hw;
  3552. int i, j, pf_q, ret = 0;
  3553. u32 rx_reg;
  3554. pf_q = vsi->base_queue;
  3555. for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
  3556. for (j = 0; j < 50; j++) {
  3557. rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
  3558. if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
  3559. ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
  3560. break;
  3561. usleep_range(1000, 2000);
  3562. }
  3563. /* Skip if the queue is already in the requested state */
  3564. if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
  3565. continue;
  3566. /* turn on/off the queue */
  3567. if (enable)
  3568. rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
  3569. else
  3570. rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
  3571. wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
  3572. /* No waiting for the Tx queue to disable */
  3573. if (!enable && test_bit(__I40E_PORT_TX_SUSPENDED, &pf->state))
  3574. continue;
  3575. /* wait for the change to finish */
  3576. ret = i40e_pf_rxq_wait(pf, pf_q, enable);
  3577. if (ret) {
  3578. dev_info(&pf->pdev->dev,
  3579. "VSI seid %d Rx ring %d %sable timeout\n",
  3580. vsi->seid, pf_q, (enable ? "en" : "dis"));
  3581. break;
  3582. }
  3583. }
  3584. /* Due to HW errata, on Rx disable only, the register can indicate done
  3585. * before it really is. Needs 50ms to be sure
  3586. */
  3587. if (!enable)
  3588. mdelay(50);
  3589. return ret;
  3590. }
  3591. /**
  3592. * i40e_vsi_start_rings - Start a VSI's rings
  3593. * @vsi: the VSI being configured
  3594. **/
  3595. int i40e_vsi_start_rings(struct i40e_vsi *vsi)
  3596. {
  3597. int ret = 0;
  3598. /* do rx first for enable and last for disable */
  3599. ret = i40e_vsi_control_rx(vsi, true);
  3600. if (ret)
  3601. return ret;
  3602. ret = i40e_vsi_control_tx(vsi, true);
  3603. return ret;
  3604. }
  3605. /**
  3606. * i40e_vsi_stop_rings - Stop a VSI's rings
  3607. * @vsi: the VSI being configured
  3608. **/
  3609. void i40e_vsi_stop_rings(struct i40e_vsi *vsi)
  3610. {
  3611. /* do rx first for enable and last for disable
  3612. * Ignore return value, we need to shutdown whatever we can
  3613. */
  3614. i40e_vsi_control_tx(vsi, false);
  3615. i40e_vsi_control_rx(vsi, false);
  3616. }
  3617. /**
  3618. * i40e_vsi_free_irq - Free the irq association with the OS
  3619. * @vsi: the VSI being configured
  3620. **/
  3621. static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
  3622. {
  3623. struct i40e_pf *pf = vsi->back;
  3624. struct i40e_hw *hw = &pf->hw;
  3625. int base = vsi->base_vector;
  3626. u32 val, qp;
  3627. int i;
  3628. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  3629. if (!vsi->q_vectors)
  3630. return;
  3631. if (!vsi->irqs_ready)
  3632. return;
  3633. vsi->irqs_ready = false;
  3634. for (i = 0; i < vsi->num_q_vectors; i++) {
  3635. int irq_num;
  3636. u16 vector;
  3637. vector = i + base;
  3638. irq_num = pf->msix_entries[vector].vector;
  3639. /* free only the irqs that were actually requested */
  3640. if (!vsi->q_vectors[i] ||
  3641. !vsi->q_vectors[i]->num_ringpairs)
  3642. continue;
  3643. /* clear the affinity notifier in the IRQ descriptor */
  3644. irq_set_affinity_notifier(irq_num, NULL);
  3645. /* clear the affinity_mask in the IRQ descriptor */
  3646. irq_set_affinity_hint(irq_num, NULL);
  3647. synchronize_irq(irq_num);
  3648. free_irq(irq_num, vsi->q_vectors[i]);
  3649. /* Tear down the interrupt queue link list
  3650. *
  3651. * We know that they come in pairs and always
  3652. * the Rx first, then the Tx. To clear the
  3653. * link list, stick the EOL value into the
  3654. * next_q field of the registers.
  3655. */
  3656. val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
  3657. qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
  3658. >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
  3659. val |= I40E_QUEUE_END_OF_LIST
  3660. << I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
  3661. wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
  3662. while (qp != I40E_QUEUE_END_OF_LIST) {
  3663. u32 next;
  3664. val = rd32(hw, I40E_QINT_RQCTL(qp));
  3665. val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
  3666. I40E_QINT_RQCTL_MSIX0_INDX_MASK |
  3667. I40E_QINT_RQCTL_CAUSE_ENA_MASK |
  3668. I40E_QINT_RQCTL_INTEVENT_MASK);
  3669. val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
  3670. I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
  3671. wr32(hw, I40E_QINT_RQCTL(qp), val);
  3672. val = rd32(hw, I40E_QINT_TQCTL(qp));
  3673. next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
  3674. >> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
  3675. val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
  3676. I40E_QINT_TQCTL_MSIX0_INDX_MASK |
  3677. I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  3678. I40E_QINT_TQCTL_INTEVENT_MASK);
  3679. val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
  3680. I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
  3681. wr32(hw, I40E_QINT_TQCTL(qp), val);
  3682. qp = next;
  3683. }
  3684. }
  3685. } else {
  3686. free_irq(pf->pdev->irq, pf);
  3687. val = rd32(hw, I40E_PFINT_LNKLST0);
  3688. qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
  3689. >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
  3690. val |= I40E_QUEUE_END_OF_LIST
  3691. << I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
  3692. wr32(hw, I40E_PFINT_LNKLST0, val);
  3693. val = rd32(hw, I40E_QINT_RQCTL(qp));
  3694. val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
  3695. I40E_QINT_RQCTL_MSIX0_INDX_MASK |
  3696. I40E_QINT_RQCTL_CAUSE_ENA_MASK |
  3697. I40E_QINT_RQCTL_INTEVENT_MASK);
  3698. val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
  3699. I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
  3700. wr32(hw, I40E_QINT_RQCTL(qp), val);
  3701. val = rd32(hw, I40E_QINT_TQCTL(qp));
  3702. val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
  3703. I40E_QINT_TQCTL_MSIX0_INDX_MASK |
  3704. I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  3705. I40E_QINT_TQCTL_INTEVENT_MASK);
  3706. val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
  3707. I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
  3708. wr32(hw, I40E_QINT_TQCTL(qp), val);
  3709. }
  3710. }
  3711. /**
  3712. * i40e_free_q_vector - Free memory allocated for specific interrupt vector
  3713. * @vsi: the VSI being configured
  3714. * @v_idx: Index of vector to be freed
  3715. *
  3716. * This function frees the memory allocated to the q_vector. In addition if
  3717. * NAPI is enabled it will delete any references to the NAPI struct prior
  3718. * to freeing the q_vector.
  3719. **/
  3720. static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
  3721. {
  3722. struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
  3723. struct i40e_ring *ring;
  3724. if (!q_vector)
  3725. return;
  3726. /* disassociate q_vector from rings */
  3727. i40e_for_each_ring(ring, q_vector->tx)
  3728. ring->q_vector = NULL;
  3729. i40e_for_each_ring(ring, q_vector->rx)
  3730. ring->q_vector = NULL;
  3731. /* only VSI w/ an associated netdev is set up w/ NAPI */
  3732. if (vsi->netdev)
  3733. netif_napi_del(&q_vector->napi);
  3734. vsi->q_vectors[v_idx] = NULL;
  3735. kfree_rcu(q_vector, rcu);
  3736. }
  3737. /**
  3738. * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
  3739. * @vsi: the VSI being un-configured
  3740. *
  3741. * This frees the memory allocated to the q_vectors and
  3742. * deletes references to the NAPI struct.
  3743. **/
  3744. static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
  3745. {
  3746. int v_idx;
  3747. for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
  3748. i40e_free_q_vector(vsi, v_idx);
  3749. }
  3750. /**
  3751. * i40e_reset_interrupt_capability - Disable interrupt setup in OS
  3752. * @pf: board private structure
  3753. **/
  3754. static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
  3755. {
  3756. /* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
  3757. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  3758. pci_disable_msix(pf->pdev);
  3759. kfree(pf->msix_entries);
  3760. pf->msix_entries = NULL;
  3761. kfree(pf->irq_pile);
  3762. pf->irq_pile = NULL;
  3763. } else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
  3764. pci_disable_msi(pf->pdev);
  3765. }
  3766. pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
  3767. }
  3768. /**
  3769. * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
  3770. * @pf: board private structure
  3771. *
  3772. * We go through and clear interrupt specific resources and reset the structure
  3773. * to pre-load conditions
  3774. **/
  3775. static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
  3776. {
  3777. int i;
  3778. i40e_stop_misc_vector(pf);
  3779. if (pf->flags & I40E_FLAG_MSIX_ENABLED && pf->msix_entries) {
  3780. synchronize_irq(pf->msix_entries[0].vector);
  3781. free_irq(pf->msix_entries[0].vector, pf);
  3782. }
  3783. i40e_put_lump(pf->irq_pile, pf->iwarp_base_vector,
  3784. I40E_IWARP_IRQ_PILE_ID);
  3785. i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
  3786. for (i = 0; i < pf->num_alloc_vsi; i++)
  3787. if (pf->vsi[i])
  3788. i40e_vsi_free_q_vectors(pf->vsi[i]);
  3789. i40e_reset_interrupt_capability(pf);
  3790. }
  3791. /**
  3792. * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
  3793. * @vsi: the VSI being configured
  3794. **/
  3795. static void i40e_napi_enable_all(struct i40e_vsi *vsi)
  3796. {
  3797. int q_idx;
  3798. if (!vsi->netdev)
  3799. return;
  3800. for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
  3801. struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
  3802. if (q_vector->rx.ring || q_vector->tx.ring)
  3803. napi_enable(&q_vector->napi);
  3804. }
  3805. }
  3806. /**
  3807. * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
  3808. * @vsi: the VSI being configured
  3809. **/
  3810. static void i40e_napi_disable_all(struct i40e_vsi *vsi)
  3811. {
  3812. int q_idx;
  3813. if (!vsi->netdev)
  3814. return;
  3815. for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
  3816. struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
  3817. if (q_vector->rx.ring || q_vector->tx.ring)
  3818. napi_disable(&q_vector->napi);
  3819. }
  3820. }
  3821. /**
  3822. * i40e_vsi_close - Shut down a VSI
  3823. * @vsi: the vsi to be quelled
  3824. **/
  3825. static void i40e_vsi_close(struct i40e_vsi *vsi)
  3826. {
  3827. struct i40e_pf *pf = vsi->back;
  3828. if (!test_and_set_bit(__I40E_DOWN, &vsi->state))
  3829. i40e_down(vsi);
  3830. i40e_vsi_free_irq(vsi);
  3831. i40e_vsi_free_tx_resources(vsi);
  3832. i40e_vsi_free_rx_resources(vsi);
  3833. vsi->current_netdev_flags = 0;
  3834. pf->flags |= I40E_FLAG_SERVICE_CLIENT_REQUESTED;
  3835. if (test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
  3836. pf->flags |= I40E_FLAG_CLIENT_RESET;
  3837. }
  3838. /**
  3839. * i40e_quiesce_vsi - Pause a given VSI
  3840. * @vsi: the VSI being paused
  3841. **/
  3842. static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
  3843. {
  3844. if (test_bit(__I40E_DOWN, &vsi->state))
  3845. return;
  3846. set_bit(__I40E_NEEDS_RESTART, &vsi->state);
  3847. if (vsi->netdev && netif_running(vsi->netdev))
  3848. vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
  3849. else
  3850. i40e_vsi_close(vsi);
  3851. }
  3852. /**
  3853. * i40e_unquiesce_vsi - Resume a given VSI
  3854. * @vsi: the VSI being resumed
  3855. **/
  3856. static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
  3857. {
  3858. if (!test_bit(__I40E_NEEDS_RESTART, &vsi->state))
  3859. return;
  3860. clear_bit(__I40E_NEEDS_RESTART, &vsi->state);
  3861. if (vsi->netdev && netif_running(vsi->netdev))
  3862. vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
  3863. else
  3864. i40e_vsi_open(vsi); /* this clears the DOWN bit */
  3865. }
  3866. /**
  3867. * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
  3868. * @pf: the PF
  3869. **/
  3870. static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
  3871. {
  3872. int v;
  3873. for (v = 0; v < pf->num_alloc_vsi; v++) {
  3874. if (pf->vsi[v])
  3875. i40e_quiesce_vsi(pf->vsi[v]);
  3876. }
  3877. }
  3878. /**
  3879. * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
  3880. * @pf: the PF
  3881. **/
  3882. static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
  3883. {
  3884. int v;
  3885. for (v = 0; v < pf->num_alloc_vsi; v++) {
  3886. if (pf->vsi[v])
  3887. i40e_unquiesce_vsi(pf->vsi[v]);
  3888. }
  3889. }
  3890. #ifdef CONFIG_I40E_DCB
  3891. /**
  3892. * i40e_vsi_wait_queues_disabled - Wait for VSI's queues to be disabled
  3893. * @vsi: the VSI being configured
  3894. *
  3895. * Wait until all queues on a given VSI have been disabled.
  3896. **/
  3897. static int i40e_vsi_wait_queues_disabled(struct i40e_vsi *vsi)
  3898. {
  3899. struct i40e_pf *pf = vsi->back;
  3900. int i, pf_q, ret;
  3901. pf_q = vsi->base_queue;
  3902. for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
  3903. /* Check and wait for the Tx queue */
  3904. ret = i40e_pf_txq_wait(pf, pf_q, false);
  3905. if (ret) {
  3906. dev_info(&pf->pdev->dev,
  3907. "VSI seid %d Tx ring %d disable timeout\n",
  3908. vsi->seid, pf_q);
  3909. return ret;
  3910. }
  3911. /* Check and wait for the Tx queue */
  3912. ret = i40e_pf_rxq_wait(pf, pf_q, false);
  3913. if (ret) {
  3914. dev_info(&pf->pdev->dev,
  3915. "VSI seid %d Rx ring %d disable timeout\n",
  3916. vsi->seid, pf_q);
  3917. return ret;
  3918. }
  3919. }
  3920. return 0;
  3921. }
  3922. /**
  3923. * i40e_pf_wait_queues_disabled - Wait for all queues of PF VSIs to be disabled
  3924. * @pf: the PF
  3925. *
  3926. * This function waits for the queues to be in disabled state for all the
  3927. * VSIs that are managed by this PF.
  3928. **/
  3929. static int i40e_pf_wait_queues_disabled(struct i40e_pf *pf)
  3930. {
  3931. int v, ret = 0;
  3932. for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
  3933. if (pf->vsi[v]) {
  3934. ret = i40e_vsi_wait_queues_disabled(pf->vsi[v]);
  3935. if (ret)
  3936. break;
  3937. }
  3938. }
  3939. return ret;
  3940. }
  3941. #endif
  3942. /**
  3943. * i40e_detect_recover_hung_queue - Function to detect and recover hung_queue
  3944. * @q_idx: TX queue number
  3945. * @vsi: Pointer to VSI struct
  3946. *
  3947. * This function checks specified queue for given VSI. Detects hung condition.
  3948. * We proactively detect hung TX queues by checking if interrupts are disabled
  3949. * but there are pending descriptors. If it appears hung, attempt to recover
  3950. * by triggering a SW interrupt.
  3951. **/
  3952. static void i40e_detect_recover_hung_queue(int q_idx, struct i40e_vsi *vsi)
  3953. {
  3954. struct i40e_ring *tx_ring = NULL;
  3955. struct i40e_pf *pf;
  3956. u32 val, tx_pending;
  3957. int i;
  3958. pf = vsi->back;
  3959. /* now that we have an index, find the tx_ring struct */
  3960. for (i = 0; i < vsi->num_queue_pairs; i++) {
  3961. if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
  3962. if (q_idx == vsi->tx_rings[i]->queue_index) {
  3963. tx_ring = vsi->tx_rings[i];
  3964. break;
  3965. }
  3966. }
  3967. }
  3968. if (!tx_ring)
  3969. return;
  3970. /* Read interrupt register */
  3971. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  3972. val = rd32(&pf->hw,
  3973. I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
  3974. tx_ring->vsi->base_vector - 1));
  3975. else
  3976. val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
  3977. tx_pending = i40e_get_tx_pending(tx_ring);
  3978. /* Interrupts are disabled and TX pending is non-zero,
  3979. * trigger the SW interrupt (don't wait). Worst case
  3980. * there will be one extra interrupt which may result
  3981. * into not cleaning any queues because queues are cleaned.
  3982. */
  3983. if (tx_pending && (!(val & I40E_PFINT_DYN_CTLN_INTENA_MASK)))
  3984. i40e_force_wb(vsi, tx_ring->q_vector);
  3985. }
  3986. /**
  3987. * i40e_detect_recover_hung - Function to detect and recover hung_queues
  3988. * @pf: pointer to PF struct
  3989. *
  3990. * LAN VSI has netdev and netdev has TX queues. This function is to check
  3991. * each of those TX queues if they are hung, trigger recovery by issuing
  3992. * SW interrupt.
  3993. **/
  3994. static void i40e_detect_recover_hung(struct i40e_pf *pf)
  3995. {
  3996. struct net_device *netdev;
  3997. struct i40e_vsi *vsi;
  3998. int i;
  3999. /* Only for LAN VSI */
  4000. vsi = pf->vsi[pf->lan_vsi];
  4001. if (!vsi)
  4002. return;
  4003. /* Make sure, VSI state is not DOWN/RECOVERY_PENDING */
  4004. if (test_bit(__I40E_DOWN, &vsi->back->state) ||
  4005. test_bit(__I40E_RESET_RECOVERY_PENDING, &vsi->back->state))
  4006. return;
  4007. /* Make sure type is MAIN VSI */
  4008. if (vsi->type != I40E_VSI_MAIN)
  4009. return;
  4010. netdev = vsi->netdev;
  4011. if (!netdev)
  4012. return;
  4013. /* Bail out if netif_carrier is not OK */
  4014. if (!netif_carrier_ok(netdev))
  4015. return;
  4016. /* Go thru' TX queues for netdev */
  4017. for (i = 0; i < netdev->num_tx_queues; i++) {
  4018. struct netdev_queue *q;
  4019. q = netdev_get_tx_queue(netdev, i);
  4020. if (q)
  4021. i40e_detect_recover_hung_queue(i, vsi);
  4022. }
  4023. }
  4024. /**
  4025. * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
  4026. * @pf: pointer to PF
  4027. *
  4028. * Get TC map for ISCSI PF type that will include iSCSI TC
  4029. * and LAN TC.
  4030. **/
  4031. static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
  4032. {
  4033. struct i40e_dcb_app_priority_table app;
  4034. struct i40e_hw *hw = &pf->hw;
  4035. u8 enabled_tc = 1; /* TC0 is always enabled */
  4036. u8 tc, i;
  4037. /* Get the iSCSI APP TLV */
  4038. struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
  4039. for (i = 0; i < dcbcfg->numapps; i++) {
  4040. app = dcbcfg->app[i];
  4041. if (app.selector == I40E_APP_SEL_TCPIP &&
  4042. app.protocolid == I40E_APP_PROTOID_ISCSI) {
  4043. tc = dcbcfg->etscfg.prioritytable[app.priority];
  4044. enabled_tc |= BIT(tc);
  4045. break;
  4046. }
  4047. }
  4048. return enabled_tc;
  4049. }
  4050. /**
  4051. * i40e_dcb_get_num_tc - Get the number of TCs from DCBx config
  4052. * @dcbcfg: the corresponding DCBx configuration structure
  4053. *
  4054. * Return the number of TCs from given DCBx configuration
  4055. **/
  4056. static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
  4057. {
  4058. int i, tc_unused = 0;
  4059. u8 num_tc = 0;
  4060. u8 ret = 0;
  4061. /* Scan the ETS Config Priority Table to find
  4062. * traffic class enabled for a given priority
  4063. * and create a bitmask of enabled TCs
  4064. */
  4065. for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
  4066. num_tc |= BIT(dcbcfg->etscfg.prioritytable[i]);
  4067. /* Now scan the bitmask to check for
  4068. * contiguous TCs starting with TC0
  4069. */
  4070. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4071. if (num_tc & BIT(i)) {
  4072. if (!tc_unused) {
  4073. ret++;
  4074. } else {
  4075. pr_err("Non-contiguous TC - Disabling DCB\n");
  4076. return 1;
  4077. }
  4078. } else {
  4079. tc_unused = 1;
  4080. }
  4081. }
  4082. /* There is always at least TC0 */
  4083. if (!ret)
  4084. ret = 1;
  4085. return ret;
  4086. }
  4087. /**
  4088. * i40e_dcb_get_enabled_tc - Get enabled traffic classes
  4089. * @dcbcfg: the corresponding DCBx configuration structure
  4090. *
  4091. * Query the current DCB configuration and return the number of
  4092. * traffic classes enabled from the given DCBX config
  4093. **/
  4094. static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
  4095. {
  4096. u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
  4097. u8 enabled_tc = 1;
  4098. u8 i;
  4099. for (i = 0; i < num_tc; i++)
  4100. enabled_tc |= BIT(i);
  4101. return enabled_tc;
  4102. }
  4103. /**
  4104. * i40e_pf_get_num_tc - Get enabled traffic classes for PF
  4105. * @pf: PF being queried
  4106. *
  4107. * Return number of traffic classes enabled for the given PF
  4108. **/
  4109. static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
  4110. {
  4111. struct i40e_hw *hw = &pf->hw;
  4112. u8 i, enabled_tc = 1;
  4113. u8 num_tc = 0;
  4114. struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
  4115. /* If DCB is not enabled then always in single TC */
  4116. if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
  4117. return 1;
  4118. /* SFP mode will be enabled for all TCs on port */
  4119. if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
  4120. return i40e_dcb_get_num_tc(dcbcfg);
  4121. /* MFP mode return count of enabled TCs for this PF */
  4122. if (pf->hw.func_caps.iscsi)
  4123. enabled_tc = i40e_get_iscsi_tc_map(pf);
  4124. else
  4125. return 1; /* Only TC0 */
  4126. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4127. if (enabled_tc & BIT(i))
  4128. num_tc++;
  4129. }
  4130. return num_tc;
  4131. }
  4132. /**
  4133. * i40e_pf_get_pf_tc_map - Get bitmap for enabled traffic classes
  4134. * @pf: PF being queried
  4135. *
  4136. * Return a bitmap for enabled traffic classes for this PF.
  4137. **/
  4138. static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
  4139. {
  4140. /* If DCB is not enabled for this PF then just return default TC */
  4141. if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
  4142. return I40E_DEFAULT_TRAFFIC_CLASS;
  4143. /* SFP mode we want PF to be enabled for all TCs */
  4144. if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
  4145. return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
  4146. /* MFP enabled and iSCSI PF type */
  4147. if (pf->hw.func_caps.iscsi)
  4148. return i40e_get_iscsi_tc_map(pf);
  4149. else
  4150. return I40E_DEFAULT_TRAFFIC_CLASS;
  4151. }
  4152. /**
  4153. * i40e_vsi_get_bw_info - Query VSI BW Information
  4154. * @vsi: the VSI being queried
  4155. *
  4156. * Returns 0 on success, negative value on failure
  4157. **/
  4158. static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
  4159. {
  4160. struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
  4161. struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
  4162. struct i40e_pf *pf = vsi->back;
  4163. struct i40e_hw *hw = &pf->hw;
  4164. i40e_status ret;
  4165. u32 tc_bw_max;
  4166. int i;
  4167. /* Get the VSI level BW configuration */
  4168. ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
  4169. if (ret) {
  4170. dev_info(&pf->pdev->dev,
  4171. "couldn't get PF vsi bw config, err %s aq_err %s\n",
  4172. i40e_stat_str(&pf->hw, ret),
  4173. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  4174. return -EINVAL;
  4175. }
  4176. /* Get the VSI level BW configuration per TC */
  4177. ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
  4178. NULL);
  4179. if (ret) {
  4180. dev_info(&pf->pdev->dev,
  4181. "couldn't get PF vsi ets bw config, err %s aq_err %s\n",
  4182. i40e_stat_str(&pf->hw, ret),
  4183. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  4184. return -EINVAL;
  4185. }
  4186. if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
  4187. dev_info(&pf->pdev->dev,
  4188. "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
  4189. bw_config.tc_valid_bits,
  4190. bw_ets_config.tc_valid_bits);
  4191. /* Still continuing */
  4192. }
  4193. vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
  4194. vsi->bw_max_quanta = bw_config.max_bw;
  4195. tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
  4196. (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
  4197. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4198. vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
  4199. vsi->bw_ets_limit_credits[i] =
  4200. le16_to_cpu(bw_ets_config.credits[i]);
  4201. /* 3 bits out of 4 for each TC */
  4202. vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
  4203. }
  4204. return 0;
  4205. }
  4206. /**
  4207. * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
  4208. * @vsi: the VSI being configured
  4209. * @enabled_tc: TC bitmap
  4210. * @bw_credits: BW shared credits per TC
  4211. *
  4212. * Returns 0 on success, negative value on failure
  4213. **/
  4214. static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
  4215. u8 *bw_share)
  4216. {
  4217. struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
  4218. i40e_status ret;
  4219. int i;
  4220. bw_data.tc_valid_bits = enabled_tc;
  4221. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
  4222. bw_data.tc_bw_credits[i] = bw_share[i];
  4223. ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, vsi->seid, &bw_data,
  4224. NULL);
  4225. if (ret) {
  4226. dev_info(&vsi->back->pdev->dev,
  4227. "AQ command Config VSI BW allocation per TC failed = %d\n",
  4228. vsi->back->hw.aq.asq_last_status);
  4229. return -EINVAL;
  4230. }
  4231. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
  4232. vsi->info.qs_handle[i] = bw_data.qs_handles[i];
  4233. return 0;
  4234. }
  4235. /**
  4236. * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
  4237. * @vsi: the VSI being configured
  4238. * @enabled_tc: TC map to be enabled
  4239. *
  4240. **/
  4241. static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
  4242. {
  4243. struct net_device *netdev = vsi->netdev;
  4244. struct i40e_pf *pf = vsi->back;
  4245. struct i40e_hw *hw = &pf->hw;
  4246. u8 netdev_tc = 0;
  4247. int i;
  4248. struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
  4249. if (!netdev)
  4250. return;
  4251. if (!enabled_tc) {
  4252. netdev_reset_tc(netdev);
  4253. return;
  4254. }
  4255. /* Set up actual enabled TCs on the VSI */
  4256. if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
  4257. return;
  4258. /* set per TC queues for the VSI */
  4259. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4260. /* Only set TC queues for enabled tcs
  4261. *
  4262. * e.g. For a VSI that has TC0 and TC3 enabled the
  4263. * enabled_tc bitmap would be 0x00001001; the driver
  4264. * will set the numtc for netdev as 2 that will be
  4265. * referenced by the netdev layer as TC 0 and 1.
  4266. */
  4267. if (vsi->tc_config.enabled_tc & BIT(i))
  4268. netdev_set_tc_queue(netdev,
  4269. vsi->tc_config.tc_info[i].netdev_tc,
  4270. vsi->tc_config.tc_info[i].qcount,
  4271. vsi->tc_config.tc_info[i].qoffset);
  4272. }
  4273. /* Assign UP2TC map for the VSI */
  4274. for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
  4275. /* Get the actual TC# for the UP */
  4276. u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
  4277. /* Get the mapped netdev TC# for the UP */
  4278. netdev_tc = vsi->tc_config.tc_info[ets_tc].netdev_tc;
  4279. netdev_set_prio_tc_map(netdev, i, netdev_tc);
  4280. }
  4281. }
  4282. /**
  4283. * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
  4284. * @vsi: the VSI being configured
  4285. * @ctxt: the ctxt buffer returned from AQ VSI update param command
  4286. **/
  4287. static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
  4288. struct i40e_vsi_context *ctxt)
  4289. {
  4290. /* copy just the sections touched not the entire info
  4291. * since not all sections are valid as returned by
  4292. * update vsi params
  4293. */
  4294. vsi->info.mapping_flags = ctxt->info.mapping_flags;
  4295. memcpy(&vsi->info.queue_mapping,
  4296. &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
  4297. memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
  4298. sizeof(vsi->info.tc_mapping));
  4299. }
  4300. /**
  4301. * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
  4302. * @vsi: VSI to be configured
  4303. * @enabled_tc: TC bitmap
  4304. *
  4305. * This configures a particular VSI for TCs that are mapped to the
  4306. * given TC bitmap. It uses default bandwidth share for TCs across
  4307. * VSIs to configure TC for a particular VSI.
  4308. *
  4309. * NOTE:
  4310. * It is expected that the VSI queues have been quisced before calling
  4311. * this function.
  4312. **/
  4313. static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
  4314. {
  4315. u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
  4316. struct i40e_vsi_context ctxt;
  4317. int ret = 0;
  4318. int i;
  4319. /* Check if enabled_tc is same as existing or new TCs */
  4320. if (vsi->tc_config.enabled_tc == enabled_tc)
  4321. return ret;
  4322. /* Enable ETS TCs with equal BW Share for now across all VSIs */
  4323. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4324. if (enabled_tc & BIT(i))
  4325. bw_share[i] = 1;
  4326. }
  4327. ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
  4328. if (ret) {
  4329. dev_info(&vsi->back->pdev->dev,
  4330. "Failed configuring TC map %d for VSI %d\n",
  4331. enabled_tc, vsi->seid);
  4332. goto out;
  4333. }
  4334. /* Update Queue Pairs Mapping for currently enabled UPs */
  4335. ctxt.seid = vsi->seid;
  4336. ctxt.pf_num = vsi->back->hw.pf_id;
  4337. ctxt.vf_num = 0;
  4338. ctxt.uplink_seid = vsi->uplink_seid;
  4339. ctxt.info = vsi->info;
  4340. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
  4341. if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
  4342. ctxt.info.valid_sections |=
  4343. cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
  4344. ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
  4345. }
  4346. /* Update the VSI after updating the VSI queue-mapping information */
  4347. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  4348. if (ret) {
  4349. dev_info(&vsi->back->pdev->dev,
  4350. "Update vsi tc config failed, err %s aq_err %s\n",
  4351. i40e_stat_str(&vsi->back->hw, ret),
  4352. i40e_aq_str(&vsi->back->hw,
  4353. vsi->back->hw.aq.asq_last_status));
  4354. goto out;
  4355. }
  4356. /* update the local VSI info with updated queue map */
  4357. i40e_vsi_update_queue_map(vsi, &ctxt);
  4358. vsi->info.valid_sections = 0;
  4359. /* Update current VSI BW information */
  4360. ret = i40e_vsi_get_bw_info(vsi);
  4361. if (ret) {
  4362. dev_info(&vsi->back->pdev->dev,
  4363. "Failed updating vsi bw info, err %s aq_err %s\n",
  4364. i40e_stat_str(&vsi->back->hw, ret),
  4365. i40e_aq_str(&vsi->back->hw,
  4366. vsi->back->hw.aq.asq_last_status));
  4367. goto out;
  4368. }
  4369. /* Update the netdev TC setup */
  4370. i40e_vsi_config_netdev_tc(vsi, enabled_tc);
  4371. out:
  4372. return ret;
  4373. }
  4374. /**
  4375. * i40e_veb_config_tc - Configure TCs for given VEB
  4376. * @veb: given VEB
  4377. * @enabled_tc: TC bitmap
  4378. *
  4379. * Configures given TC bitmap for VEB (switching) element
  4380. **/
  4381. int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
  4382. {
  4383. struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
  4384. struct i40e_pf *pf = veb->pf;
  4385. int ret = 0;
  4386. int i;
  4387. /* No TCs or already enabled TCs just return */
  4388. if (!enabled_tc || veb->enabled_tc == enabled_tc)
  4389. return ret;
  4390. bw_data.tc_valid_bits = enabled_tc;
  4391. /* bw_data.absolute_credits is not set (relative) */
  4392. /* Enable ETS TCs with equal BW Share for now */
  4393. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4394. if (enabled_tc & BIT(i))
  4395. bw_data.tc_bw_share_credits[i] = 1;
  4396. }
  4397. ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
  4398. &bw_data, NULL);
  4399. if (ret) {
  4400. dev_info(&pf->pdev->dev,
  4401. "VEB bw config failed, err %s aq_err %s\n",
  4402. i40e_stat_str(&pf->hw, ret),
  4403. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  4404. goto out;
  4405. }
  4406. /* Update the BW information */
  4407. ret = i40e_veb_get_bw_info(veb);
  4408. if (ret) {
  4409. dev_info(&pf->pdev->dev,
  4410. "Failed getting veb bw config, err %s aq_err %s\n",
  4411. i40e_stat_str(&pf->hw, ret),
  4412. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  4413. }
  4414. out:
  4415. return ret;
  4416. }
  4417. #ifdef CONFIG_I40E_DCB
  4418. /**
  4419. * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
  4420. * @pf: PF struct
  4421. *
  4422. * Reconfigure VEB/VSIs on a given PF; it is assumed that
  4423. * the caller would've quiesce all the VSIs before calling
  4424. * this function
  4425. **/
  4426. static void i40e_dcb_reconfigure(struct i40e_pf *pf)
  4427. {
  4428. u8 tc_map = 0;
  4429. int ret;
  4430. u8 v;
  4431. /* Enable the TCs available on PF to all VEBs */
  4432. tc_map = i40e_pf_get_tc_map(pf);
  4433. for (v = 0; v < I40E_MAX_VEB; v++) {
  4434. if (!pf->veb[v])
  4435. continue;
  4436. ret = i40e_veb_config_tc(pf->veb[v], tc_map);
  4437. if (ret) {
  4438. dev_info(&pf->pdev->dev,
  4439. "Failed configuring TC for VEB seid=%d\n",
  4440. pf->veb[v]->seid);
  4441. /* Will try to configure as many components */
  4442. }
  4443. }
  4444. /* Update each VSI */
  4445. for (v = 0; v < pf->num_alloc_vsi; v++) {
  4446. if (!pf->vsi[v])
  4447. continue;
  4448. /* - Enable all TCs for the LAN VSI
  4449. * - For all others keep them at TC0 for now
  4450. */
  4451. if (v == pf->lan_vsi)
  4452. tc_map = i40e_pf_get_tc_map(pf);
  4453. else
  4454. tc_map = I40E_DEFAULT_TRAFFIC_CLASS;
  4455. ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
  4456. if (ret) {
  4457. dev_info(&pf->pdev->dev,
  4458. "Failed configuring TC for VSI seid=%d\n",
  4459. pf->vsi[v]->seid);
  4460. /* Will try to configure as many components */
  4461. } else {
  4462. /* Re-configure VSI vectors based on updated TC map */
  4463. i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
  4464. if (pf->vsi[v]->netdev)
  4465. i40e_dcbnl_set_all(pf->vsi[v]);
  4466. }
  4467. }
  4468. }
  4469. /**
  4470. * i40e_resume_port_tx - Resume port Tx
  4471. * @pf: PF struct
  4472. *
  4473. * Resume a port's Tx and issue a PF reset in case of failure to
  4474. * resume.
  4475. **/
  4476. static int i40e_resume_port_tx(struct i40e_pf *pf)
  4477. {
  4478. struct i40e_hw *hw = &pf->hw;
  4479. int ret;
  4480. ret = i40e_aq_resume_port_tx(hw, NULL);
  4481. if (ret) {
  4482. dev_info(&pf->pdev->dev,
  4483. "Resume Port Tx failed, err %s aq_err %s\n",
  4484. i40e_stat_str(&pf->hw, ret),
  4485. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  4486. /* Schedule PF reset to recover */
  4487. set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
  4488. i40e_service_event_schedule(pf);
  4489. }
  4490. return ret;
  4491. }
  4492. /**
  4493. * i40e_init_pf_dcb - Initialize DCB configuration
  4494. * @pf: PF being configured
  4495. *
  4496. * Query the current DCB configuration and cache it
  4497. * in the hardware structure
  4498. **/
  4499. static int i40e_init_pf_dcb(struct i40e_pf *pf)
  4500. {
  4501. struct i40e_hw *hw = &pf->hw;
  4502. int err = 0;
  4503. /* Do not enable DCB for SW1 and SW2 images even if the FW is capable */
  4504. if (pf->flags & I40E_FLAG_NO_DCB_SUPPORT)
  4505. goto out;
  4506. /* Get the initial DCB configuration */
  4507. err = i40e_init_dcb(hw);
  4508. if (!err) {
  4509. /* Device/Function is not DCBX capable */
  4510. if ((!hw->func_caps.dcb) ||
  4511. (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
  4512. dev_info(&pf->pdev->dev,
  4513. "DCBX offload is not supported or is disabled for this PF.\n");
  4514. } else {
  4515. /* When status is not DISABLED then DCBX in FW */
  4516. pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
  4517. DCB_CAP_DCBX_VER_IEEE;
  4518. pf->flags |= I40E_FLAG_DCB_CAPABLE;
  4519. /* Enable DCB tagging only when more than one TC
  4520. * or explicitly disable if only one TC
  4521. */
  4522. if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
  4523. pf->flags |= I40E_FLAG_DCB_ENABLED;
  4524. else
  4525. pf->flags &= ~I40E_FLAG_DCB_ENABLED;
  4526. dev_dbg(&pf->pdev->dev,
  4527. "DCBX offload is supported for this PF.\n");
  4528. }
  4529. } else {
  4530. dev_info(&pf->pdev->dev,
  4531. "Query for DCB configuration failed, err %s aq_err %s\n",
  4532. i40e_stat_str(&pf->hw, err),
  4533. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  4534. }
  4535. out:
  4536. return err;
  4537. }
  4538. #endif /* CONFIG_I40E_DCB */
  4539. #define SPEED_SIZE 14
  4540. #define FC_SIZE 8
  4541. /**
  4542. * i40e_print_link_message - print link up or down
  4543. * @vsi: the VSI for which link needs a message
  4544. */
  4545. void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
  4546. {
  4547. enum i40e_aq_link_speed new_speed;
  4548. char *speed = "Unknown";
  4549. char *fc = "Unknown";
  4550. char *fec = "";
  4551. char *an = "";
  4552. new_speed = vsi->back->hw.phy.link_info.link_speed;
  4553. if ((vsi->current_isup == isup) && (vsi->current_speed == new_speed))
  4554. return;
  4555. vsi->current_isup = isup;
  4556. vsi->current_speed = new_speed;
  4557. if (!isup) {
  4558. netdev_info(vsi->netdev, "NIC Link is Down\n");
  4559. return;
  4560. }
  4561. /* Warn user if link speed on NPAR enabled partition is not at
  4562. * least 10GB
  4563. */
  4564. if (vsi->back->hw.func_caps.npar_enable &&
  4565. (vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
  4566. vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
  4567. netdev_warn(vsi->netdev,
  4568. "The partition detected link speed that is less than 10Gbps\n");
  4569. switch (vsi->back->hw.phy.link_info.link_speed) {
  4570. case I40E_LINK_SPEED_40GB:
  4571. speed = "40 G";
  4572. break;
  4573. case I40E_LINK_SPEED_20GB:
  4574. speed = "20 G";
  4575. break;
  4576. case I40E_LINK_SPEED_25GB:
  4577. speed = "25 G";
  4578. break;
  4579. case I40E_LINK_SPEED_10GB:
  4580. speed = "10 G";
  4581. break;
  4582. case I40E_LINK_SPEED_1GB:
  4583. speed = "1000 M";
  4584. break;
  4585. case I40E_LINK_SPEED_100MB:
  4586. speed = "100 M";
  4587. break;
  4588. default:
  4589. break;
  4590. }
  4591. switch (vsi->back->hw.fc.current_mode) {
  4592. case I40E_FC_FULL:
  4593. fc = "RX/TX";
  4594. break;
  4595. case I40E_FC_TX_PAUSE:
  4596. fc = "TX";
  4597. break;
  4598. case I40E_FC_RX_PAUSE:
  4599. fc = "RX";
  4600. break;
  4601. default:
  4602. fc = "None";
  4603. break;
  4604. }
  4605. if (vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_25GB) {
  4606. fec = ", FEC: None";
  4607. an = ", Autoneg: False";
  4608. if (vsi->back->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
  4609. an = ", Autoneg: True";
  4610. if (vsi->back->hw.phy.link_info.fec_info &
  4611. I40E_AQ_CONFIG_FEC_KR_ENA)
  4612. fec = ", FEC: CL74 FC-FEC/BASE-R";
  4613. else if (vsi->back->hw.phy.link_info.fec_info &
  4614. I40E_AQ_CONFIG_FEC_RS_ENA)
  4615. fec = ", FEC: CL108 RS-FEC";
  4616. }
  4617. netdev_info(vsi->netdev, "NIC Link is Up, %sbps Full Duplex%s%s, Flow Control: %s\n",
  4618. speed, fec, an, fc);
  4619. }
  4620. /**
  4621. * i40e_up_complete - Finish the last steps of bringing up a connection
  4622. * @vsi: the VSI being configured
  4623. **/
  4624. static int i40e_up_complete(struct i40e_vsi *vsi)
  4625. {
  4626. struct i40e_pf *pf = vsi->back;
  4627. int err;
  4628. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  4629. i40e_vsi_configure_msix(vsi);
  4630. else
  4631. i40e_configure_msi_and_legacy(vsi);
  4632. /* start rings */
  4633. err = i40e_vsi_start_rings(vsi);
  4634. if (err)
  4635. return err;
  4636. clear_bit(__I40E_DOWN, &vsi->state);
  4637. i40e_napi_enable_all(vsi);
  4638. i40e_vsi_enable_irq(vsi);
  4639. if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
  4640. (vsi->netdev)) {
  4641. i40e_print_link_message(vsi, true);
  4642. netif_tx_start_all_queues(vsi->netdev);
  4643. netif_carrier_on(vsi->netdev);
  4644. } else if (vsi->netdev) {
  4645. i40e_print_link_message(vsi, false);
  4646. /* need to check for qualified module here*/
  4647. if ((pf->hw.phy.link_info.link_info &
  4648. I40E_AQ_MEDIA_AVAILABLE) &&
  4649. (!(pf->hw.phy.link_info.an_info &
  4650. I40E_AQ_QUALIFIED_MODULE)))
  4651. netdev_err(vsi->netdev,
  4652. "the driver failed to link because an unqualified module was detected.");
  4653. }
  4654. /* replay FDIR SB filters */
  4655. if (vsi->type == I40E_VSI_FDIR) {
  4656. /* reset fd counters */
  4657. pf->fd_add_err = 0;
  4658. pf->fd_atr_cnt = 0;
  4659. i40e_fdir_filter_restore(vsi);
  4660. }
  4661. /* On the next run of the service_task, notify any clients of the new
  4662. * opened netdev
  4663. */
  4664. pf->flags |= I40E_FLAG_SERVICE_CLIENT_REQUESTED;
  4665. i40e_service_event_schedule(pf);
  4666. return 0;
  4667. }
  4668. /**
  4669. * i40e_vsi_reinit_locked - Reset the VSI
  4670. * @vsi: the VSI being configured
  4671. *
  4672. * Rebuild the ring structs after some configuration
  4673. * has changed, e.g. MTU size.
  4674. **/
  4675. static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
  4676. {
  4677. struct i40e_pf *pf = vsi->back;
  4678. WARN_ON(in_interrupt());
  4679. while (test_and_set_bit(__I40E_CONFIG_BUSY, &pf->state))
  4680. usleep_range(1000, 2000);
  4681. i40e_down(vsi);
  4682. i40e_up(vsi);
  4683. clear_bit(__I40E_CONFIG_BUSY, &pf->state);
  4684. }
  4685. /**
  4686. * i40e_up - Bring the connection back up after being down
  4687. * @vsi: the VSI being configured
  4688. **/
  4689. int i40e_up(struct i40e_vsi *vsi)
  4690. {
  4691. int err;
  4692. err = i40e_vsi_configure(vsi);
  4693. if (!err)
  4694. err = i40e_up_complete(vsi);
  4695. return err;
  4696. }
  4697. /**
  4698. * i40e_down - Shutdown the connection processing
  4699. * @vsi: the VSI being stopped
  4700. **/
  4701. void i40e_down(struct i40e_vsi *vsi)
  4702. {
  4703. int i;
  4704. /* It is assumed that the caller of this function
  4705. * sets the vsi->state __I40E_DOWN bit.
  4706. */
  4707. if (vsi->netdev) {
  4708. netif_carrier_off(vsi->netdev);
  4709. netif_tx_disable(vsi->netdev);
  4710. }
  4711. i40e_vsi_disable_irq(vsi);
  4712. i40e_vsi_stop_rings(vsi);
  4713. i40e_napi_disable_all(vsi);
  4714. for (i = 0; i < vsi->num_queue_pairs; i++) {
  4715. i40e_clean_tx_ring(vsi->tx_rings[i]);
  4716. i40e_clean_rx_ring(vsi->rx_rings[i]);
  4717. }
  4718. }
  4719. /**
  4720. * i40e_setup_tc - configure multiple traffic classes
  4721. * @netdev: net device to configure
  4722. * @tc: number of traffic classes to enable
  4723. **/
  4724. static int i40e_setup_tc(struct net_device *netdev, u8 tc)
  4725. {
  4726. struct i40e_netdev_priv *np = netdev_priv(netdev);
  4727. struct i40e_vsi *vsi = np->vsi;
  4728. struct i40e_pf *pf = vsi->back;
  4729. u8 enabled_tc = 0;
  4730. int ret = -EINVAL;
  4731. int i;
  4732. /* Check if DCB enabled to continue */
  4733. if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
  4734. netdev_info(netdev, "DCB is not enabled for adapter\n");
  4735. goto exit;
  4736. }
  4737. /* Check if MFP enabled */
  4738. if (pf->flags & I40E_FLAG_MFP_ENABLED) {
  4739. netdev_info(netdev, "Configuring TC not supported in MFP mode\n");
  4740. goto exit;
  4741. }
  4742. /* Check whether tc count is within enabled limit */
  4743. if (tc > i40e_pf_get_num_tc(pf)) {
  4744. netdev_info(netdev, "TC count greater than enabled on link for adapter\n");
  4745. goto exit;
  4746. }
  4747. /* Generate TC map for number of tc requested */
  4748. for (i = 0; i < tc; i++)
  4749. enabled_tc |= BIT(i);
  4750. /* Requesting same TC configuration as already enabled */
  4751. if (enabled_tc == vsi->tc_config.enabled_tc)
  4752. return 0;
  4753. /* Quiesce VSI queues */
  4754. i40e_quiesce_vsi(vsi);
  4755. /* Configure VSI for enabled TCs */
  4756. ret = i40e_vsi_config_tc(vsi, enabled_tc);
  4757. if (ret) {
  4758. netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
  4759. vsi->seid);
  4760. goto exit;
  4761. }
  4762. /* Unquiesce VSI */
  4763. i40e_unquiesce_vsi(vsi);
  4764. exit:
  4765. return ret;
  4766. }
  4767. static int __i40e_setup_tc(struct net_device *netdev, u32 handle, __be16 proto,
  4768. struct tc_to_netdev *tc)
  4769. {
  4770. if (tc->type != TC_SETUP_MQPRIO)
  4771. return -EINVAL;
  4772. tc->mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
  4773. return i40e_setup_tc(netdev, tc->mqprio->num_tc);
  4774. }
  4775. /**
  4776. * i40e_open - Called when a network interface is made active
  4777. * @netdev: network interface device structure
  4778. *
  4779. * The open entry point is called when a network interface is made
  4780. * active by the system (IFF_UP). At this point all resources needed
  4781. * for transmit and receive operations are allocated, the interrupt
  4782. * handler is registered with the OS, the netdev watchdog subtask is
  4783. * enabled, and the stack is notified that the interface is ready.
  4784. *
  4785. * Returns 0 on success, negative value on failure
  4786. **/
  4787. int i40e_open(struct net_device *netdev)
  4788. {
  4789. struct i40e_netdev_priv *np = netdev_priv(netdev);
  4790. struct i40e_vsi *vsi = np->vsi;
  4791. struct i40e_pf *pf = vsi->back;
  4792. int err;
  4793. /* disallow open during test or if eeprom is broken */
  4794. if (test_bit(__I40E_TESTING, &pf->state) ||
  4795. test_bit(__I40E_BAD_EEPROM, &pf->state))
  4796. return -EBUSY;
  4797. netif_carrier_off(netdev);
  4798. err = i40e_vsi_open(vsi);
  4799. if (err)
  4800. return err;
  4801. /* configure global TSO hardware offload settings */
  4802. wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
  4803. TCP_FLAG_FIN) >> 16);
  4804. wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
  4805. TCP_FLAG_FIN |
  4806. TCP_FLAG_CWR) >> 16);
  4807. wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
  4808. udp_tunnel_get_rx_info(netdev);
  4809. return 0;
  4810. }
  4811. /**
  4812. * i40e_vsi_open -
  4813. * @vsi: the VSI to open
  4814. *
  4815. * Finish initialization of the VSI.
  4816. *
  4817. * Returns 0 on success, negative value on failure
  4818. *
  4819. * Note: expects to be called while under rtnl_lock()
  4820. **/
  4821. int i40e_vsi_open(struct i40e_vsi *vsi)
  4822. {
  4823. struct i40e_pf *pf = vsi->back;
  4824. char int_name[I40E_INT_NAME_STR_LEN];
  4825. int err;
  4826. /* allocate descriptors */
  4827. err = i40e_vsi_setup_tx_resources(vsi);
  4828. if (err)
  4829. goto err_setup_tx;
  4830. err = i40e_vsi_setup_rx_resources(vsi);
  4831. if (err)
  4832. goto err_setup_rx;
  4833. err = i40e_vsi_configure(vsi);
  4834. if (err)
  4835. goto err_setup_rx;
  4836. if (vsi->netdev) {
  4837. snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
  4838. dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
  4839. err = i40e_vsi_request_irq(vsi, int_name);
  4840. if (err)
  4841. goto err_setup_rx;
  4842. /* Notify the stack of the actual queue counts. */
  4843. err = netif_set_real_num_tx_queues(vsi->netdev,
  4844. vsi->num_queue_pairs);
  4845. if (err)
  4846. goto err_set_queues;
  4847. err = netif_set_real_num_rx_queues(vsi->netdev,
  4848. vsi->num_queue_pairs);
  4849. if (err)
  4850. goto err_set_queues;
  4851. } else if (vsi->type == I40E_VSI_FDIR) {
  4852. snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
  4853. dev_driver_string(&pf->pdev->dev),
  4854. dev_name(&pf->pdev->dev));
  4855. err = i40e_vsi_request_irq(vsi, int_name);
  4856. } else {
  4857. err = -EINVAL;
  4858. goto err_setup_rx;
  4859. }
  4860. err = i40e_up_complete(vsi);
  4861. if (err)
  4862. goto err_up_complete;
  4863. return 0;
  4864. err_up_complete:
  4865. i40e_down(vsi);
  4866. err_set_queues:
  4867. i40e_vsi_free_irq(vsi);
  4868. err_setup_rx:
  4869. i40e_vsi_free_rx_resources(vsi);
  4870. err_setup_tx:
  4871. i40e_vsi_free_tx_resources(vsi);
  4872. if (vsi == pf->vsi[pf->lan_vsi])
  4873. i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED), true);
  4874. return err;
  4875. }
  4876. /**
  4877. * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
  4878. * @pf: Pointer to PF
  4879. *
  4880. * This function destroys the hlist where all the Flow Director
  4881. * filters were saved.
  4882. **/
  4883. static void i40e_fdir_filter_exit(struct i40e_pf *pf)
  4884. {
  4885. struct i40e_fdir_filter *filter;
  4886. struct i40e_flex_pit *pit_entry, *tmp;
  4887. struct hlist_node *node2;
  4888. hlist_for_each_entry_safe(filter, node2,
  4889. &pf->fdir_filter_list, fdir_node) {
  4890. hlist_del(&filter->fdir_node);
  4891. kfree(filter);
  4892. }
  4893. list_for_each_entry_safe(pit_entry, tmp, &pf->l3_flex_pit_list, list) {
  4894. list_del(&pit_entry->list);
  4895. kfree(pit_entry);
  4896. }
  4897. INIT_LIST_HEAD(&pf->l3_flex_pit_list);
  4898. list_for_each_entry_safe(pit_entry, tmp, &pf->l4_flex_pit_list, list) {
  4899. list_del(&pit_entry->list);
  4900. kfree(pit_entry);
  4901. }
  4902. INIT_LIST_HEAD(&pf->l4_flex_pit_list);
  4903. pf->fdir_pf_active_filters = 0;
  4904. pf->fd_tcp4_filter_cnt = 0;
  4905. pf->fd_udp4_filter_cnt = 0;
  4906. pf->fd_sctp4_filter_cnt = 0;
  4907. pf->fd_ip4_filter_cnt = 0;
  4908. /* Reprogram the default input set for TCP/IPv4 */
  4909. i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
  4910. I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
  4911. I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
  4912. /* Reprogram the default input set for UDP/IPv4 */
  4913. i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_UDP,
  4914. I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
  4915. I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
  4916. /* Reprogram the default input set for SCTP/IPv4 */
  4917. i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_SCTP,
  4918. I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
  4919. I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
  4920. /* Reprogram the default input set for Other/IPv4 */
  4921. i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_OTHER,
  4922. I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
  4923. }
  4924. /**
  4925. * i40e_close - Disables a network interface
  4926. * @netdev: network interface device structure
  4927. *
  4928. * The close entry point is called when an interface is de-activated
  4929. * by the OS. The hardware is still under the driver's control, but
  4930. * this netdev interface is disabled.
  4931. *
  4932. * Returns 0, this is not allowed to fail
  4933. **/
  4934. int i40e_close(struct net_device *netdev)
  4935. {
  4936. struct i40e_netdev_priv *np = netdev_priv(netdev);
  4937. struct i40e_vsi *vsi = np->vsi;
  4938. i40e_vsi_close(vsi);
  4939. return 0;
  4940. }
  4941. /**
  4942. * i40e_do_reset - Start a PF or Core Reset sequence
  4943. * @pf: board private structure
  4944. * @reset_flags: which reset is requested
  4945. * @lock_acquired: indicates whether or not the lock has been acquired
  4946. * before this function was called.
  4947. *
  4948. * The essential difference in resets is that the PF Reset
  4949. * doesn't clear the packet buffers, doesn't reset the PE
  4950. * firmware, and doesn't bother the other PFs on the chip.
  4951. **/
  4952. void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags, bool lock_acquired)
  4953. {
  4954. u32 val;
  4955. WARN_ON(in_interrupt());
  4956. /* do the biggest reset indicated */
  4957. if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
  4958. /* Request a Global Reset
  4959. *
  4960. * This will start the chip's countdown to the actual full
  4961. * chip reset event, and a warning interrupt to be sent
  4962. * to all PFs, including the requestor. Our handler
  4963. * for the warning interrupt will deal with the shutdown
  4964. * and recovery of the switch setup.
  4965. */
  4966. dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
  4967. val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
  4968. val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
  4969. wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
  4970. } else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
  4971. /* Request a Core Reset
  4972. *
  4973. * Same as Global Reset, except does *not* include the MAC/PHY
  4974. */
  4975. dev_dbg(&pf->pdev->dev, "CoreR requested\n");
  4976. val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
  4977. val |= I40E_GLGEN_RTRIG_CORER_MASK;
  4978. wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
  4979. i40e_flush(&pf->hw);
  4980. } else if (reset_flags & BIT_ULL(__I40E_PF_RESET_REQUESTED)) {
  4981. /* Request a PF Reset
  4982. *
  4983. * Resets only the PF-specific registers
  4984. *
  4985. * This goes directly to the tear-down and rebuild of
  4986. * the switch, since we need to do all the recovery as
  4987. * for the Core Reset.
  4988. */
  4989. dev_dbg(&pf->pdev->dev, "PFR requested\n");
  4990. i40e_handle_reset_warning(pf, lock_acquired);
  4991. } else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
  4992. int v;
  4993. /* Find the VSI(s) that requested a re-init */
  4994. dev_info(&pf->pdev->dev,
  4995. "VSI reinit requested\n");
  4996. for (v = 0; v < pf->num_alloc_vsi; v++) {
  4997. struct i40e_vsi *vsi = pf->vsi[v];
  4998. if (vsi != NULL &&
  4999. test_bit(__I40E_REINIT_REQUESTED, &vsi->state)) {
  5000. i40e_vsi_reinit_locked(pf->vsi[v]);
  5001. clear_bit(__I40E_REINIT_REQUESTED, &vsi->state);
  5002. }
  5003. }
  5004. } else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
  5005. int v;
  5006. /* Find the VSI(s) that needs to be brought down */
  5007. dev_info(&pf->pdev->dev, "VSI down requested\n");
  5008. for (v = 0; v < pf->num_alloc_vsi; v++) {
  5009. struct i40e_vsi *vsi = pf->vsi[v];
  5010. if (vsi != NULL &&
  5011. test_bit(__I40E_DOWN_REQUESTED, &vsi->state)) {
  5012. set_bit(__I40E_DOWN, &vsi->state);
  5013. i40e_down(vsi);
  5014. clear_bit(__I40E_DOWN_REQUESTED, &vsi->state);
  5015. }
  5016. }
  5017. } else {
  5018. dev_info(&pf->pdev->dev,
  5019. "bad reset request 0x%08x\n", reset_flags);
  5020. }
  5021. }
  5022. #ifdef CONFIG_I40E_DCB
  5023. /**
  5024. * i40e_dcb_need_reconfig - Check if DCB needs reconfig
  5025. * @pf: board private structure
  5026. * @old_cfg: current DCB config
  5027. * @new_cfg: new DCB config
  5028. **/
  5029. bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
  5030. struct i40e_dcbx_config *old_cfg,
  5031. struct i40e_dcbx_config *new_cfg)
  5032. {
  5033. bool need_reconfig = false;
  5034. /* Check if ETS configuration has changed */
  5035. if (memcmp(&new_cfg->etscfg,
  5036. &old_cfg->etscfg,
  5037. sizeof(new_cfg->etscfg))) {
  5038. /* If Priority Table has changed reconfig is needed */
  5039. if (memcmp(&new_cfg->etscfg.prioritytable,
  5040. &old_cfg->etscfg.prioritytable,
  5041. sizeof(new_cfg->etscfg.prioritytable))) {
  5042. need_reconfig = true;
  5043. dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
  5044. }
  5045. if (memcmp(&new_cfg->etscfg.tcbwtable,
  5046. &old_cfg->etscfg.tcbwtable,
  5047. sizeof(new_cfg->etscfg.tcbwtable)))
  5048. dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
  5049. if (memcmp(&new_cfg->etscfg.tsatable,
  5050. &old_cfg->etscfg.tsatable,
  5051. sizeof(new_cfg->etscfg.tsatable)))
  5052. dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
  5053. }
  5054. /* Check if PFC configuration has changed */
  5055. if (memcmp(&new_cfg->pfc,
  5056. &old_cfg->pfc,
  5057. sizeof(new_cfg->pfc))) {
  5058. need_reconfig = true;
  5059. dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
  5060. }
  5061. /* Check if APP Table has changed */
  5062. if (memcmp(&new_cfg->app,
  5063. &old_cfg->app,
  5064. sizeof(new_cfg->app))) {
  5065. need_reconfig = true;
  5066. dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
  5067. }
  5068. dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
  5069. return need_reconfig;
  5070. }
  5071. /**
  5072. * i40e_handle_lldp_event - Handle LLDP Change MIB event
  5073. * @pf: board private structure
  5074. * @e: event info posted on ARQ
  5075. **/
  5076. static int i40e_handle_lldp_event(struct i40e_pf *pf,
  5077. struct i40e_arq_event_info *e)
  5078. {
  5079. struct i40e_aqc_lldp_get_mib *mib =
  5080. (struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
  5081. struct i40e_hw *hw = &pf->hw;
  5082. struct i40e_dcbx_config tmp_dcbx_cfg;
  5083. bool need_reconfig = false;
  5084. int ret = 0;
  5085. u8 type;
  5086. /* Not DCB capable or capability disabled */
  5087. if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
  5088. return ret;
  5089. /* Ignore if event is not for Nearest Bridge */
  5090. type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
  5091. & I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
  5092. dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
  5093. if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
  5094. return ret;
  5095. /* Check MIB Type and return if event for Remote MIB update */
  5096. type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
  5097. dev_dbg(&pf->pdev->dev,
  5098. "LLDP event mib type %s\n", type ? "remote" : "local");
  5099. if (type == I40E_AQ_LLDP_MIB_REMOTE) {
  5100. /* Update the remote cached instance and return */
  5101. ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
  5102. I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
  5103. &hw->remote_dcbx_config);
  5104. goto exit;
  5105. }
  5106. /* Store the old configuration */
  5107. tmp_dcbx_cfg = hw->local_dcbx_config;
  5108. /* Reset the old DCBx configuration data */
  5109. memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
  5110. /* Get updated DCBX data from firmware */
  5111. ret = i40e_get_dcb_config(&pf->hw);
  5112. if (ret) {
  5113. dev_info(&pf->pdev->dev,
  5114. "Failed querying DCB configuration data from firmware, err %s aq_err %s\n",
  5115. i40e_stat_str(&pf->hw, ret),
  5116. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  5117. goto exit;
  5118. }
  5119. /* No change detected in DCBX configs */
  5120. if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
  5121. sizeof(tmp_dcbx_cfg))) {
  5122. dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
  5123. goto exit;
  5124. }
  5125. need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
  5126. &hw->local_dcbx_config);
  5127. i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
  5128. if (!need_reconfig)
  5129. goto exit;
  5130. /* Enable DCB tagging only when more than one TC */
  5131. if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
  5132. pf->flags |= I40E_FLAG_DCB_ENABLED;
  5133. else
  5134. pf->flags &= ~I40E_FLAG_DCB_ENABLED;
  5135. set_bit(__I40E_PORT_TX_SUSPENDED, &pf->state);
  5136. /* Reconfiguration needed quiesce all VSIs */
  5137. i40e_pf_quiesce_all_vsi(pf);
  5138. /* Changes in configuration update VEB/VSI */
  5139. i40e_dcb_reconfigure(pf);
  5140. ret = i40e_resume_port_tx(pf);
  5141. clear_bit(__I40E_PORT_TX_SUSPENDED, &pf->state);
  5142. /* In case of error no point in resuming VSIs */
  5143. if (ret)
  5144. goto exit;
  5145. /* Wait for the PF's queues to be disabled */
  5146. ret = i40e_pf_wait_queues_disabled(pf);
  5147. if (ret) {
  5148. /* Schedule PF reset to recover */
  5149. set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
  5150. i40e_service_event_schedule(pf);
  5151. } else {
  5152. i40e_pf_unquiesce_all_vsi(pf);
  5153. pf->flags |= (I40E_FLAG_SERVICE_CLIENT_REQUESTED |
  5154. I40E_FLAG_CLIENT_L2_CHANGE);
  5155. }
  5156. exit:
  5157. return ret;
  5158. }
  5159. #endif /* CONFIG_I40E_DCB */
  5160. /**
  5161. * i40e_do_reset_safe - Protected reset path for userland calls.
  5162. * @pf: board private structure
  5163. * @reset_flags: which reset is requested
  5164. *
  5165. **/
  5166. void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
  5167. {
  5168. rtnl_lock();
  5169. i40e_do_reset(pf, reset_flags, true);
  5170. rtnl_unlock();
  5171. }
  5172. /**
  5173. * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
  5174. * @pf: board private structure
  5175. * @e: event info posted on ARQ
  5176. *
  5177. * Handler for LAN Queue Overflow Event generated by the firmware for PF
  5178. * and VF queues
  5179. **/
  5180. static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
  5181. struct i40e_arq_event_info *e)
  5182. {
  5183. struct i40e_aqc_lan_overflow *data =
  5184. (struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
  5185. u32 queue = le32_to_cpu(data->prtdcb_rupto);
  5186. u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
  5187. struct i40e_hw *hw = &pf->hw;
  5188. struct i40e_vf *vf;
  5189. u16 vf_id;
  5190. dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
  5191. queue, qtx_ctl);
  5192. /* Queue belongs to VF, find the VF and issue VF reset */
  5193. if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
  5194. >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
  5195. vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
  5196. >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
  5197. vf_id -= hw->func_caps.vf_base_id;
  5198. vf = &pf->vf[vf_id];
  5199. i40e_vc_notify_vf_reset(vf);
  5200. /* Allow VF to process pending reset notification */
  5201. msleep(20);
  5202. i40e_reset_vf(vf, false);
  5203. }
  5204. }
  5205. /**
  5206. * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
  5207. * @pf: board private structure
  5208. **/
  5209. u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
  5210. {
  5211. u32 val, fcnt_prog;
  5212. val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
  5213. fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
  5214. return fcnt_prog;
  5215. }
  5216. /**
  5217. * i40e_get_current_fd_count - Get total FD filters programmed for this PF
  5218. * @pf: board private structure
  5219. **/
  5220. u32 i40e_get_current_fd_count(struct i40e_pf *pf)
  5221. {
  5222. u32 val, fcnt_prog;
  5223. val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
  5224. fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
  5225. ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
  5226. I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
  5227. return fcnt_prog;
  5228. }
  5229. /**
  5230. * i40e_get_global_fd_count - Get total FD filters programmed on device
  5231. * @pf: board private structure
  5232. **/
  5233. u32 i40e_get_global_fd_count(struct i40e_pf *pf)
  5234. {
  5235. u32 val, fcnt_prog;
  5236. val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
  5237. fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
  5238. ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >>
  5239. I40E_GLQF_FDCNT_0_BESTCNT_SHIFT);
  5240. return fcnt_prog;
  5241. }
  5242. /**
  5243. * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
  5244. * @pf: board private structure
  5245. **/
  5246. void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
  5247. {
  5248. struct i40e_fdir_filter *filter;
  5249. u32 fcnt_prog, fcnt_avail;
  5250. struct hlist_node *node;
  5251. if (test_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state))
  5252. return;
  5253. /* Check if, FD SB or ATR was auto disabled and if there is enough room
  5254. * to re-enable
  5255. */
  5256. fcnt_prog = i40e_get_global_fd_count(pf);
  5257. fcnt_avail = pf->fdir_pf_filter_count;
  5258. if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
  5259. (pf->fd_add_err == 0) ||
  5260. (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt)) {
  5261. if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
  5262. (pf->hw_disabled_flags & I40E_FLAG_FD_SB_ENABLED)) {
  5263. pf->hw_disabled_flags &= ~I40E_FLAG_FD_SB_ENABLED;
  5264. if (I40E_DEBUG_FD & pf->hw.debug_mask)
  5265. dev_info(&pf->pdev->dev, "FD Sideband/ntuple is being enabled since we have space in the table now\n");
  5266. }
  5267. }
  5268. /* Wait for some more space to be available to turn on ATR. We also
  5269. * must check that no existing ntuple rules for TCP are in effect
  5270. */
  5271. if (fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM * 2)) {
  5272. if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
  5273. (pf->hw_disabled_flags & I40E_FLAG_FD_ATR_ENABLED) &&
  5274. (pf->fd_tcp4_filter_cnt == 0)) {
  5275. pf->hw_disabled_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
  5276. if (I40E_DEBUG_FD & pf->hw.debug_mask)
  5277. dev_info(&pf->pdev->dev, "ATR is being enabled since we have space in the table and there are no conflicting ntuple rules\n");
  5278. }
  5279. }
  5280. /* if hw had a problem adding a filter, delete it */
  5281. if (pf->fd_inv > 0) {
  5282. hlist_for_each_entry_safe(filter, node,
  5283. &pf->fdir_filter_list, fdir_node) {
  5284. if (filter->fd_id == pf->fd_inv) {
  5285. hlist_del(&filter->fdir_node);
  5286. kfree(filter);
  5287. pf->fdir_pf_active_filters--;
  5288. }
  5289. }
  5290. }
  5291. }
  5292. #define I40E_MIN_FD_FLUSH_INTERVAL 10
  5293. #define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
  5294. /**
  5295. * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
  5296. * @pf: board private structure
  5297. **/
  5298. static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
  5299. {
  5300. unsigned long min_flush_time;
  5301. int flush_wait_retry = 50;
  5302. bool disable_atr = false;
  5303. int fd_room;
  5304. int reg;
  5305. if (!time_after(jiffies, pf->fd_flush_timestamp +
  5306. (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
  5307. return;
  5308. /* If the flush is happening too quick and we have mostly SB rules we
  5309. * should not re-enable ATR for some time.
  5310. */
  5311. min_flush_time = pf->fd_flush_timestamp +
  5312. (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
  5313. fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
  5314. if (!(time_after(jiffies, min_flush_time)) &&
  5315. (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
  5316. if (I40E_DEBUG_FD & pf->hw.debug_mask)
  5317. dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
  5318. disable_atr = true;
  5319. }
  5320. pf->fd_flush_timestamp = jiffies;
  5321. pf->hw_disabled_flags |= I40E_FLAG_FD_ATR_ENABLED;
  5322. /* flush all filters */
  5323. wr32(&pf->hw, I40E_PFQF_CTL_1,
  5324. I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
  5325. i40e_flush(&pf->hw);
  5326. pf->fd_flush_cnt++;
  5327. pf->fd_add_err = 0;
  5328. do {
  5329. /* Check FD flush status every 5-6msec */
  5330. usleep_range(5000, 6000);
  5331. reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
  5332. if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
  5333. break;
  5334. } while (flush_wait_retry--);
  5335. if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
  5336. dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
  5337. } else {
  5338. /* replay sideband filters */
  5339. i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
  5340. if (!disable_atr && !pf->fd_tcp4_filter_cnt)
  5341. pf->hw_disabled_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
  5342. clear_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state);
  5343. if (I40E_DEBUG_FD & pf->hw.debug_mask)
  5344. dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
  5345. }
  5346. }
  5347. /**
  5348. * i40e_get_current_atr_count - Get the count of total FD ATR filters programmed
  5349. * @pf: board private structure
  5350. **/
  5351. u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
  5352. {
  5353. return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
  5354. }
  5355. /* We can see up to 256 filter programming desc in transit if the filters are
  5356. * being applied really fast; before we see the first
  5357. * filter miss error on Rx queue 0. Accumulating enough error messages before
  5358. * reacting will make sure we don't cause flush too often.
  5359. */
  5360. #define I40E_MAX_FD_PROGRAM_ERROR 256
  5361. /**
  5362. * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
  5363. * @pf: board private structure
  5364. **/
  5365. static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
  5366. {
  5367. /* if interface is down do nothing */
  5368. if (test_bit(__I40E_DOWN, &pf->state))
  5369. return;
  5370. if (test_bit(__I40E_FD_FLUSH_REQUESTED, &pf->state))
  5371. i40e_fdir_flush_and_replay(pf);
  5372. i40e_fdir_check_and_reenable(pf);
  5373. }
  5374. /**
  5375. * i40e_vsi_link_event - notify VSI of a link event
  5376. * @vsi: vsi to be notified
  5377. * @link_up: link up or down
  5378. **/
  5379. static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
  5380. {
  5381. if (!vsi || test_bit(__I40E_DOWN, &vsi->state))
  5382. return;
  5383. switch (vsi->type) {
  5384. case I40E_VSI_MAIN:
  5385. if (!vsi->netdev || !vsi->netdev_registered)
  5386. break;
  5387. if (link_up) {
  5388. netif_carrier_on(vsi->netdev);
  5389. netif_tx_wake_all_queues(vsi->netdev);
  5390. } else {
  5391. netif_carrier_off(vsi->netdev);
  5392. netif_tx_stop_all_queues(vsi->netdev);
  5393. }
  5394. break;
  5395. case I40E_VSI_SRIOV:
  5396. case I40E_VSI_VMDQ2:
  5397. case I40E_VSI_CTRL:
  5398. case I40E_VSI_IWARP:
  5399. case I40E_VSI_MIRROR:
  5400. default:
  5401. /* there is no notification for other VSIs */
  5402. break;
  5403. }
  5404. }
  5405. /**
  5406. * i40e_veb_link_event - notify elements on the veb of a link event
  5407. * @veb: veb to be notified
  5408. * @link_up: link up or down
  5409. **/
  5410. static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
  5411. {
  5412. struct i40e_pf *pf;
  5413. int i;
  5414. if (!veb || !veb->pf)
  5415. return;
  5416. pf = veb->pf;
  5417. /* depth first... */
  5418. for (i = 0; i < I40E_MAX_VEB; i++)
  5419. if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
  5420. i40e_veb_link_event(pf->veb[i], link_up);
  5421. /* ... now the local VSIs */
  5422. for (i = 0; i < pf->num_alloc_vsi; i++)
  5423. if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
  5424. i40e_vsi_link_event(pf->vsi[i], link_up);
  5425. }
  5426. /**
  5427. * i40e_link_event - Update netif_carrier status
  5428. * @pf: board private structure
  5429. **/
  5430. static void i40e_link_event(struct i40e_pf *pf)
  5431. {
  5432. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  5433. u8 new_link_speed, old_link_speed;
  5434. i40e_status status;
  5435. bool new_link, old_link;
  5436. /* save off old link status information */
  5437. pf->hw.phy.link_info_old = pf->hw.phy.link_info;
  5438. /* set this to force the get_link_status call to refresh state */
  5439. pf->hw.phy.get_link_info = true;
  5440. old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
  5441. status = i40e_get_link_status(&pf->hw, &new_link);
  5442. /* On success, disable temp link polling */
  5443. if (status == I40E_SUCCESS) {
  5444. if (pf->flags & I40E_FLAG_TEMP_LINK_POLLING)
  5445. pf->flags &= ~I40E_FLAG_TEMP_LINK_POLLING;
  5446. } else {
  5447. /* Enable link polling temporarily until i40e_get_link_status
  5448. * returns I40E_SUCCESS
  5449. */
  5450. pf->flags |= I40E_FLAG_TEMP_LINK_POLLING;
  5451. dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
  5452. status);
  5453. return;
  5454. }
  5455. old_link_speed = pf->hw.phy.link_info_old.link_speed;
  5456. new_link_speed = pf->hw.phy.link_info.link_speed;
  5457. if (new_link == old_link &&
  5458. new_link_speed == old_link_speed &&
  5459. (test_bit(__I40E_DOWN, &vsi->state) ||
  5460. new_link == netif_carrier_ok(vsi->netdev)))
  5461. return;
  5462. if (!test_bit(__I40E_DOWN, &vsi->state))
  5463. i40e_print_link_message(vsi, new_link);
  5464. /* Notify the base of the switch tree connected to
  5465. * the link. Floating VEBs are not notified.
  5466. */
  5467. if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
  5468. i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
  5469. else
  5470. i40e_vsi_link_event(vsi, new_link);
  5471. if (pf->vf)
  5472. i40e_vc_notify_link_state(pf);
  5473. if (pf->flags & I40E_FLAG_PTP)
  5474. i40e_ptp_set_increment(pf);
  5475. }
  5476. /**
  5477. * i40e_watchdog_subtask - periodic checks not using event driven response
  5478. * @pf: board private structure
  5479. **/
  5480. static void i40e_watchdog_subtask(struct i40e_pf *pf)
  5481. {
  5482. int i;
  5483. /* if interface is down do nothing */
  5484. if (test_bit(__I40E_DOWN, &pf->state) ||
  5485. test_bit(__I40E_CONFIG_BUSY, &pf->state))
  5486. return;
  5487. /* make sure we don't do these things too often */
  5488. if (time_before(jiffies, (pf->service_timer_previous +
  5489. pf->service_timer_period)))
  5490. return;
  5491. pf->service_timer_previous = jiffies;
  5492. if ((pf->flags & I40E_FLAG_LINK_POLLING_ENABLED) ||
  5493. (pf->flags & I40E_FLAG_TEMP_LINK_POLLING))
  5494. i40e_link_event(pf);
  5495. /* Update the stats for active netdevs so the network stack
  5496. * can look at updated numbers whenever it cares to
  5497. */
  5498. for (i = 0; i < pf->num_alloc_vsi; i++)
  5499. if (pf->vsi[i] && pf->vsi[i]->netdev)
  5500. i40e_update_stats(pf->vsi[i]);
  5501. if (pf->flags & I40E_FLAG_VEB_STATS_ENABLED) {
  5502. /* Update the stats for the active switching components */
  5503. for (i = 0; i < I40E_MAX_VEB; i++)
  5504. if (pf->veb[i])
  5505. i40e_update_veb_stats(pf->veb[i]);
  5506. }
  5507. i40e_ptp_rx_hang(pf->vsi[pf->lan_vsi]);
  5508. }
  5509. /**
  5510. * i40e_reset_subtask - Set up for resetting the device and driver
  5511. * @pf: board private structure
  5512. **/
  5513. static void i40e_reset_subtask(struct i40e_pf *pf)
  5514. {
  5515. u32 reset_flags = 0;
  5516. if (test_bit(__I40E_REINIT_REQUESTED, &pf->state)) {
  5517. reset_flags |= BIT(__I40E_REINIT_REQUESTED);
  5518. clear_bit(__I40E_REINIT_REQUESTED, &pf->state);
  5519. }
  5520. if (test_bit(__I40E_PF_RESET_REQUESTED, &pf->state)) {
  5521. reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
  5522. clear_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
  5523. }
  5524. if (test_bit(__I40E_CORE_RESET_REQUESTED, &pf->state)) {
  5525. reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
  5526. clear_bit(__I40E_CORE_RESET_REQUESTED, &pf->state);
  5527. }
  5528. if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state)) {
  5529. reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
  5530. clear_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state);
  5531. }
  5532. if (test_bit(__I40E_DOWN_REQUESTED, &pf->state)) {
  5533. reset_flags |= BIT(__I40E_DOWN_REQUESTED);
  5534. clear_bit(__I40E_DOWN_REQUESTED, &pf->state);
  5535. }
  5536. /* If there's a recovery already waiting, it takes
  5537. * precedence before starting a new reset sequence.
  5538. */
  5539. if (test_bit(__I40E_RESET_INTR_RECEIVED, &pf->state)) {
  5540. i40e_prep_for_reset(pf, false);
  5541. i40e_reset(pf);
  5542. i40e_rebuild(pf, false, false);
  5543. }
  5544. /* If we're already down or resetting, just bail */
  5545. if (reset_flags &&
  5546. !test_bit(__I40E_DOWN, &pf->state) &&
  5547. !test_bit(__I40E_CONFIG_BUSY, &pf->state)) {
  5548. rtnl_lock();
  5549. i40e_do_reset(pf, reset_flags, true);
  5550. rtnl_unlock();
  5551. }
  5552. }
  5553. /**
  5554. * i40e_handle_link_event - Handle link event
  5555. * @pf: board private structure
  5556. * @e: event info posted on ARQ
  5557. **/
  5558. static void i40e_handle_link_event(struct i40e_pf *pf,
  5559. struct i40e_arq_event_info *e)
  5560. {
  5561. struct i40e_aqc_get_link_status *status =
  5562. (struct i40e_aqc_get_link_status *)&e->desc.params.raw;
  5563. /* Do a new status request to re-enable LSE reporting
  5564. * and load new status information into the hw struct
  5565. * This completely ignores any state information
  5566. * in the ARQ event info, instead choosing to always
  5567. * issue the AQ update link status command.
  5568. */
  5569. i40e_link_event(pf);
  5570. /* check for unqualified module, if link is down */
  5571. if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
  5572. (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
  5573. (!(status->link_info & I40E_AQ_LINK_UP)))
  5574. dev_err(&pf->pdev->dev,
  5575. "The driver failed to link because an unqualified module was detected.\n");
  5576. }
  5577. /**
  5578. * i40e_clean_adminq_subtask - Clean the AdminQ rings
  5579. * @pf: board private structure
  5580. **/
  5581. static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
  5582. {
  5583. struct i40e_arq_event_info event;
  5584. struct i40e_hw *hw = &pf->hw;
  5585. u16 pending, i = 0;
  5586. i40e_status ret;
  5587. u16 opcode;
  5588. u32 oldval;
  5589. u32 val;
  5590. /* Do not run clean AQ when PF reset fails */
  5591. if (test_bit(__I40E_RESET_FAILED, &pf->state))
  5592. return;
  5593. /* check for error indications */
  5594. val = rd32(&pf->hw, pf->hw.aq.arq.len);
  5595. oldval = val;
  5596. if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
  5597. if (hw->debug_mask & I40E_DEBUG_AQ)
  5598. dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
  5599. val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
  5600. }
  5601. if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
  5602. if (hw->debug_mask & I40E_DEBUG_AQ)
  5603. dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
  5604. val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
  5605. pf->arq_overflows++;
  5606. }
  5607. if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
  5608. if (hw->debug_mask & I40E_DEBUG_AQ)
  5609. dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
  5610. val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
  5611. }
  5612. if (oldval != val)
  5613. wr32(&pf->hw, pf->hw.aq.arq.len, val);
  5614. val = rd32(&pf->hw, pf->hw.aq.asq.len);
  5615. oldval = val;
  5616. if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
  5617. if (pf->hw.debug_mask & I40E_DEBUG_AQ)
  5618. dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
  5619. val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
  5620. }
  5621. if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
  5622. if (pf->hw.debug_mask & I40E_DEBUG_AQ)
  5623. dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
  5624. val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
  5625. }
  5626. if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
  5627. if (pf->hw.debug_mask & I40E_DEBUG_AQ)
  5628. dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
  5629. val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
  5630. }
  5631. if (oldval != val)
  5632. wr32(&pf->hw, pf->hw.aq.asq.len, val);
  5633. event.buf_len = I40E_MAX_AQ_BUF_SIZE;
  5634. event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
  5635. if (!event.msg_buf)
  5636. return;
  5637. do {
  5638. ret = i40e_clean_arq_element(hw, &event, &pending);
  5639. if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK)
  5640. break;
  5641. else if (ret) {
  5642. dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
  5643. break;
  5644. }
  5645. opcode = le16_to_cpu(event.desc.opcode);
  5646. switch (opcode) {
  5647. case i40e_aqc_opc_get_link_status:
  5648. i40e_handle_link_event(pf, &event);
  5649. break;
  5650. case i40e_aqc_opc_send_msg_to_pf:
  5651. ret = i40e_vc_process_vf_msg(pf,
  5652. le16_to_cpu(event.desc.retval),
  5653. le32_to_cpu(event.desc.cookie_high),
  5654. le32_to_cpu(event.desc.cookie_low),
  5655. event.msg_buf,
  5656. event.msg_len);
  5657. break;
  5658. case i40e_aqc_opc_lldp_update_mib:
  5659. dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
  5660. #ifdef CONFIG_I40E_DCB
  5661. rtnl_lock();
  5662. ret = i40e_handle_lldp_event(pf, &event);
  5663. rtnl_unlock();
  5664. #endif /* CONFIG_I40E_DCB */
  5665. break;
  5666. case i40e_aqc_opc_event_lan_overflow:
  5667. dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
  5668. i40e_handle_lan_overflow_event(pf, &event);
  5669. break;
  5670. case i40e_aqc_opc_send_msg_to_peer:
  5671. dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
  5672. break;
  5673. case i40e_aqc_opc_nvm_erase:
  5674. case i40e_aqc_opc_nvm_update:
  5675. case i40e_aqc_opc_oem_post_update:
  5676. i40e_debug(&pf->hw, I40E_DEBUG_NVM,
  5677. "ARQ NVM operation 0x%04x completed\n",
  5678. opcode);
  5679. break;
  5680. default:
  5681. dev_info(&pf->pdev->dev,
  5682. "ARQ: Unknown event 0x%04x ignored\n",
  5683. opcode);
  5684. break;
  5685. }
  5686. } while (i++ < pf->adminq_work_limit);
  5687. if (i < pf->adminq_work_limit)
  5688. clear_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
  5689. /* re-enable Admin queue interrupt cause */
  5690. val = rd32(hw, I40E_PFINT_ICR0_ENA);
  5691. val |= I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
  5692. wr32(hw, I40E_PFINT_ICR0_ENA, val);
  5693. i40e_flush(hw);
  5694. kfree(event.msg_buf);
  5695. }
  5696. /**
  5697. * i40e_verify_eeprom - make sure eeprom is good to use
  5698. * @pf: board private structure
  5699. **/
  5700. static void i40e_verify_eeprom(struct i40e_pf *pf)
  5701. {
  5702. int err;
  5703. err = i40e_diag_eeprom_test(&pf->hw);
  5704. if (err) {
  5705. /* retry in case of garbage read */
  5706. err = i40e_diag_eeprom_test(&pf->hw);
  5707. if (err) {
  5708. dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
  5709. err);
  5710. set_bit(__I40E_BAD_EEPROM, &pf->state);
  5711. }
  5712. }
  5713. if (!err && test_bit(__I40E_BAD_EEPROM, &pf->state)) {
  5714. dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
  5715. clear_bit(__I40E_BAD_EEPROM, &pf->state);
  5716. }
  5717. }
  5718. /**
  5719. * i40e_enable_pf_switch_lb
  5720. * @pf: pointer to the PF structure
  5721. *
  5722. * enable switch loop back or die - no point in a return value
  5723. **/
  5724. static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
  5725. {
  5726. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  5727. struct i40e_vsi_context ctxt;
  5728. int ret;
  5729. ctxt.seid = pf->main_vsi_seid;
  5730. ctxt.pf_num = pf->hw.pf_id;
  5731. ctxt.vf_num = 0;
  5732. ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
  5733. if (ret) {
  5734. dev_info(&pf->pdev->dev,
  5735. "couldn't get PF vsi config, err %s aq_err %s\n",
  5736. i40e_stat_str(&pf->hw, ret),
  5737. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  5738. return;
  5739. }
  5740. ctxt.flags = I40E_AQ_VSI_TYPE_PF;
  5741. ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  5742. ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  5743. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  5744. if (ret) {
  5745. dev_info(&pf->pdev->dev,
  5746. "update vsi switch failed, err %s aq_err %s\n",
  5747. i40e_stat_str(&pf->hw, ret),
  5748. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  5749. }
  5750. }
  5751. /**
  5752. * i40e_disable_pf_switch_lb
  5753. * @pf: pointer to the PF structure
  5754. *
  5755. * disable switch loop back or die - no point in a return value
  5756. **/
  5757. static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
  5758. {
  5759. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  5760. struct i40e_vsi_context ctxt;
  5761. int ret;
  5762. ctxt.seid = pf->main_vsi_seid;
  5763. ctxt.pf_num = pf->hw.pf_id;
  5764. ctxt.vf_num = 0;
  5765. ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
  5766. if (ret) {
  5767. dev_info(&pf->pdev->dev,
  5768. "couldn't get PF vsi config, err %s aq_err %s\n",
  5769. i40e_stat_str(&pf->hw, ret),
  5770. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  5771. return;
  5772. }
  5773. ctxt.flags = I40E_AQ_VSI_TYPE_PF;
  5774. ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  5775. ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  5776. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  5777. if (ret) {
  5778. dev_info(&pf->pdev->dev,
  5779. "update vsi switch failed, err %s aq_err %s\n",
  5780. i40e_stat_str(&pf->hw, ret),
  5781. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  5782. }
  5783. }
  5784. /**
  5785. * i40e_config_bridge_mode - Configure the HW bridge mode
  5786. * @veb: pointer to the bridge instance
  5787. *
  5788. * Configure the loop back mode for the LAN VSI that is downlink to the
  5789. * specified HW bridge instance. It is expected this function is called
  5790. * when a new HW bridge is instantiated.
  5791. **/
  5792. static void i40e_config_bridge_mode(struct i40e_veb *veb)
  5793. {
  5794. struct i40e_pf *pf = veb->pf;
  5795. if (pf->hw.debug_mask & I40E_DEBUG_LAN)
  5796. dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
  5797. veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
  5798. if (veb->bridge_mode & BRIDGE_MODE_VEPA)
  5799. i40e_disable_pf_switch_lb(pf);
  5800. else
  5801. i40e_enable_pf_switch_lb(pf);
  5802. }
  5803. /**
  5804. * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
  5805. * @veb: pointer to the VEB instance
  5806. *
  5807. * This is a recursive function that first builds the attached VSIs then
  5808. * recurses in to build the next layer of VEB. We track the connections
  5809. * through our own index numbers because the seid's from the HW could
  5810. * change across the reset.
  5811. **/
  5812. static int i40e_reconstitute_veb(struct i40e_veb *veb)
  5813. {
  5814. struct i40e_vsi *ctl_vsi = NULL;
  5815. struct i40e_pf *pf = veb->pf;
  5816. int v, veb_idx;
  5817. int ret;
  5818. /* build VSI that owns this VEB, temporarily attached to base VEB */
  5819. for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
  5820. if (pf->vsi[v] &&
  5821. pf->vsi[v]->veb_idx == veb->idx &&
  5822. pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
  5823. ctl_vsi = pf->vsi[v];
  5824. break;
  5825. }
  5826. }
  5827. if (!ctl_vsi) {
  5828. dev_info(&pf->pdev->dev,
  5829. "missing owner VSI for veb_idx %d\n", veb->idx);
  5830. ret = -ENOENT;
  5831. goto end_reconstitute;
  5832. }
  5833. if (ctl_vsi != pf->vsi[pf->lan_vsi])
  5834. ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
  5835. ret = i40e_add_vsi(ctl_vsi);
  5836. if (ret) {
  5837. dev_info(&pf->pdev->dev,
  5838. "rebuild of veb_idx %d owner VSI failed: %d\n",
  5839. veb->idx, ret);
  5840. goto end_reconstitute;
  5841. }
  5842. i40e_vsi_reset_stats(ctl_vsi);
  5843. /* create the VEB in the switch and move the VSI onto the VEB */
  5844. ret = i40e_add_veb(veb, ctl_vsi);
  5845. if (ret)
  5846. goto end_reconstitute;
  5847. if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
  5848. veb->bridge_mode = BRIDGE_MODE_VEB;
  5849. else
  5850. veb->bridge_mode = BRIDGE_MODE_VEPA;
  5851. i40e_config_bridge_mode(veb);
  5852. /* create the remaining VSIs attached to this VEB */
  5853. for (v = 0; v < pf->num_alloc_vsi; v++) {
  5854. if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
  5855. continue;
  5856. if (pf->vsi[v]->veb_idx == veb->idx) {
  5857. struct i40e_vsi *vsi = pf->vsi[v];
  5858. vsi->uplink_seid = veb->seid;
  5859. ret = i40e_add_vsi(vsi);
  5860. if (ret) {
  5861. dev_info(&pf->pdev->dev,
  5862. "rebuild of vsi_idx %d failed: %d\n",
  5863. v, ret);
  5864. goto end_reconstitute;
  5865. }
  5866. i40e_vsi_reset_stats(vsi);
  5867. }
  5868. }
  5869. /* create any VEBs attached to this VEB - RECURSION */
  5870. for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
  5871. if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
  5872. pf->veb[veb_idx]->uplink_seid = veb->seid;
  5873. ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
  5874. if (ret)
  5875. break;
  5876. }
  5877. }
  5878. end_reconstitute:
  5879. return ret;
  5880. }
  5881. /**
  5882. * i40e_get_capabilities - get info about the HW
  5883. * @pf: the PF struct
  5884. **/
  5885. static int i40e_get_capabilities(struct i40e_pf *pf)
  5886. {
  5887. struct i40e_aqc_list_capabilities_element_resp *cap_buf;
  5888. u16 data_size;
  5889. int buf_len;
  5890. int err;
  5891. buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
  5892. do {
  5893. cap_buf = kzalloc(buf_len, GFP_KERNEL);
  5894. if (!cap_buf)
  5895. return -ENOMEM;
  5896. /* this loads the data into the hw struct for us */
  5897. err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
  5898. &data_size,
  5899. i40e_aqc_opc_list_func_capabilities,
  5900. NULL);
  5901. /* data loaded, buffer no longer needed */
  5902. kfree(cap_buf);
  5903. if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
  5904. /* retry with a larger buffer */
  5905. buf_len = data_size;
  5906. } else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK) {
  5907. dev_info(&pf->pdev->dev,
  5908. "capability discovery failed, err %s aq_err %s\n",
  5909. i40e_stat_str(&pf->hw, err),
  5910. i40e_aq_str(&pf->hw,
  5911. pf->hw.aq.asq_last_status));
  5912. return -ENODEV;
  5913. }
  5914. } while (err);
  5915. if (pf->hw.debug_mask & I40E_DEBUG_USER)
  5916. dev_info(&pf->pdev->dev,
  5917. "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",
  5918. pf->hw.pf_id, pf->hw.func_caps.num_vfs,
  5919. pf->hw.func_caps.num_msix_vectors,
  5920. pf->hw.func_caps.num_msix_vectors_vf,
  5921. pf->hw.func_caps.fd_filters_guaranteed,
  5922. pf->hw.func_caps.fd_filters_best_effort,
  5923. pf->hw.func_caps.num_tx_qp,
  5924. pf->hw.func_caps.num_vsis);
  5925. #define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
  5926. + pf->hw.func_caps.num_vfs)
  5927. if (pf->hw.revision_id == 0 && (DEF_NUM_VSI > pf->hw.func_caps.num_vsis)) {
  5928. dev_info(&pf->pdev->dev,
  5929. "got num_vsis %d, setting num_vsis to %d\n",
  5930. pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
  5931. pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
  5932. }
  5933. return 0;
  5934. }
  5935. static int i40e_vsi_clear(struct i40e_vsi *vsi);
  5936. /**
  5937. * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
  5938. * @pf: board private structure
  5939. **/
  5940. static void i40e_fdir_sb_setup(struct i40e_pf *pf)
  5941. {
  5942. struct i40e_vsi *vsi;
  5943. /* quick workaround for an NVM issue that leaves a critical register
  5944. * uninitialized
  5945. */
  5946. if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
  5947. static const u32 hkey[] = {
  5948. 0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
  5949. 0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
  5950. 0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
  5951. 0x95b3a76d};
  5952. int i;
  5953. for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
  5954. wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
  5955. }
  5956. if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
  5957. return;
  5958. /* find existing VSI and see if it needs configuring */
  5959. vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
  5960. /* create a new VSI if none exists */
  5961. if (!vsi) {
  5962. vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
  5963. pf->vsi[pf->lan_vsi]->seid, 0);
  5964. if (!vsi) {
  5965. dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
  5966. pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
  5967. return;
  5968. }
  5969. }
  5970. i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
  5971. }
  5972. /**
  5973. * i40e_fdir_teardown - release the Flow Director resources
  5974. * @pf: board private structure
  5975. **/
  5976. static void i40e_fdir_teardown(struct i40e_pf *pf)
  5977. {
  5978. struct i40e_vsi *vsi;
  5979. i40e_fdir_filter_exit(pf);
  5980. vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
  5981. if (vsi)
  5982. i40e_vsi_release(vsi);
  5983. }
  5984. /**
  5985. * i40e_prep_for_reset - prep for the core to reset
  5986. * @pf: board private structure
  5987. * @lock_acquired: indicates whether or not the lock has been acquired
  5988. * before this function was called.
  5989. *
  5990. * Close up the VFs and other things in prep for PF Reset.
  5991. **/
  5992. static void i40e_prep_for_reset(struct i40e_pf *pf, bool lock_acquired)
  5993. {
  5994. struct i40e_hw *hw = &pf->hw;
  5995. i40e_status ret = 0;
  5996. u32 v;
  5997. clear_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
  5998. if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
  5999. return;
  6000. if (i40e_check_asq_alive(&pf->hw))
  6001. i40e_vc_notify_reset(pf);
  6002. dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
  6003. /* quiesce the VSIs and their queues that are not already DOWN */
  6004. /* pf_quiesce_all_vsi modifies netdev structures -rtnl_lock needed */
  6005. if (!lock_acquired)
  6006. rtnl_lock();
  6007. i40e_pf_quiesce_all_vsi(pf);
  6008. if (!lock_acquired)
  6009. rtnl_unlock();
  6010. for (v = 0; v < pf->num_alloc_vsi; v++) {
  6011. if (pf->vsi[v])
  6012. pf->vsi[v]->seid = 0;
  6013. }
  6014. i40e_shutdown_adminq(&pf->hw);
  6015. /* call shutdown HMC */
  6016. if (hw->hmc.hmc_obj) {
  6017. ret = i40e_shutdown_lan_hmc(hw);
  6018. if (ret)
  6019. dev_warn(&pf->pdev->dev,
  6020. "shutdown_lan_hmc failed: %d\n", ret);
  6021. }
  6022. }
  6023. /**
  6024. * i40e_send_version - update firmware with driver version
  6025. * @pf: PF struct
  6026. */
  6027. static void i40e_send_version(struct i40e_pf *pf)
  6028. {
  6029. struct i40e_driver_version dv;
  6030. dv.major_version = DRV_VERSION_MAJOR;
  6031. dv.minor_version = DRV_VERSION_MINOR;
  6032. dv.build_version = DRV_VERSION_BUILD;
  6033. dv.subbuild_version = 0;
  6034. strlcpy(dv.driver_string, DRV_VERSION, sizeof(dv.driver_string));
  6035. i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
  6036. }
  6037. /**
  6038. * i40e_reset - wait for core reset to finish reset, reset pf if corer not seen
  6039. * @pf: board private structure
  6040. **/
  6041. static int i40e_reset(struct i40e_pf *pf)
  6042. {
  6043. struct i40e_hw *hw = &pf->hw;
  6044. i40e_status ret;
  6045. ret = i40e_pf_reset(hw);
  6046. if (ret) {
  6047. dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
  6048. set_bit(__I40E_RESET_FAILED, &pf->state);
  6049. clear_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state);
  6050. } else {
  6051. pf->pfr_count++;
  6052. }
  6053. return ret;
  6054. }
  6055. /**
  6056. * i40e_rebuild - rebuild using a saved config
  6057. * @pf: board private structure
  6058. * @reinit: if the Main VSI needs to re-initialized.
  6059. * @lock_acquired: indicates whether or not the lock has been acquired
  6060. * before this function was called.
  6061. **/
  6062. static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired)
  6063. {
  6064. struct i40e_hw *hw = &pf->hw;
  6065. u8 set_fc_aq_fail = 0;
  6066. i40e_status ret;
  6067. u32 val;
  6068. int v;
  6069. if (test_bit(__I40E_DOWN, &pf->state))
  6070. goto clear_recovery;
  6071. dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
  6072. /* rebuild the basics for the AdminQ, HMC, and initial HW switch */
  6073. ret = i40e_init_adminq(&pf->hw);
  6074. if (ret) {
  6075. dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %s aq_err %s\n",
  6076. i40e_stat_str(&pf->hw, ret),
  6077. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  6078. goto clear_recovery;
  6079. }
  6080. /* re-verify the eeprom if we just had an EMP reset */
  6081. if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, &pf->state))
  6082. i40e_verify_eeprom(pf);
  6083. i40e_clear_pxe_mode(hw);
  6084. ret = i40e_get_capabilities(pf);
  6085. if (ret)
  6086. goto end_core_reset;
  6087. ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
  6088. hw->func_caps.num_rx_qp, 0, 0);
  6089. if (ret) {
  6090. dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
  6091. goto end_core_reset;
  6092. }
  6093. ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
  6094. if (ret) {
  6095. dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
  6096. goto end_core_reset;
  6097. }
  6098. #ifdef CONFIG_I40E_DCB
  6099. ret = i40e_init_pf_dcb(pf);
  6100. if (ret) {
  6101. dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n", ret);
  6102. pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
  6103. /* Continue without DCB enabled */
  6104. }
  6105. #endif /* CONFIG_I40E_DCB */
  6106. /* do basic switch setup */
  6107. if (!lock_acquired)
  6108. rtnl_lock();
  6109. ret = i40e_setup_pf_switch(pf, reinit);
  6110. if (ret)
  6111. goto end_unlock;
  6112. /* The driver only wants link up/down and module qualification
  6113. * reports from firmware. Note the negative logic.
  6114. */
  6115. ret = i40e_aq_set_phy_int_mask(&pf->hw,
  6116. ~(I40E_AQ_EVENT_LINK_UPDOWN |
  6117. I40E_AQ_EVENT_MEDIA_NA |
  6118. I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
  6119. if (ret)
  6120. dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
  6121. i40e_stat_str(&pf->hw, ret),
  6122. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  6123. /* make sure our flow control settings are restored */
  6124. ret = i40e_set_fc(&pf->hw, &set_fc_aq_fail, true);
  6125. if (ret)
  6126. dev_dbg(&pf->pdev->dev, "setting flow control: ret = %s last_status = %s\n",
  6127. i40e_stat_str(&pf->hw, ret),
  6128. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  6129. /* Rebuild the VSIs and VEBs that existed before reset.
  6130. * They are still in our local switch element arrays, so only
  6131. * need to rebuild the switch model in the HW.
  6132. *
  6133. * If there were VEBs but the reconstitution failed, we'll try
  6134. * try to recover minimal use by getting the basic PF VSI working.
  6135. */
  6136. if (pf->vsi[pf->lan_vsi]->uplink_seid != pf->mac_seid) {
  6137. dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
  6138. /* find the one VEB connected to the MAC, and find orphans */
  6139. for (v = 0; v < I40E_MAX_VEB; v++) {
  6140. if (!pf->veb[v])
  6141. continue;
  6142. if (pf->veb[v]->uplink_seid == pf->mac_seid ||
  6143. pf->veb[v]->uplink_seid == 0) {
  6144. ret = i40e_reconstitute_veb(pf->veb[v]);
  6145. if (!ret)
  6146. continue;
  6147. /* If Main VEB failed, we're in deep doodoo,
  6148. * so give up rebuilding the switch and set up
  6149. * for minimal rebuild of PF VSI.
  6150. * If orphan failed, we'll report the error
  6151. * but try to keep going.
  6152. */
  6153. if (pf->veb[v]->uplink_seid == pf->mac_seid) {
  6154. dev_info(&pf->pdev->dev,
  6155. "rebuild of switch failed: %d, will try to set up simple PF connection\n",
  6156. ret);
  6157. pf->vsi[pf->lan_vsi]->uplink_seid
  6158. = pf->mac_seid;
  6159. break;
  6160. } else if (pf->veb[v]->uplink_seid == 0) {
  6161. dev_info(&pf->pdev->dev,
  6162. "rebuild of orphan VEB failed: %d\n",
  6163. ret);
  6164. }
  6165. }
  6166. }
  6167. }
  6168. if (pf->vsi[pf->lan_vsi]->uplink_seid == pf->mac_seid) {
  6169. dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
  6170. /* no VEB, so rebuild only the Main VSI */
  6171. ret = i40e_add_vsi(pf->vsi[pf->lan_vsi]);
  6172. if (ret) {
  6173. dev_info(&pf->pdev->dev,
  6174. "rebuild of Main VSI failed: %d\n", ret);
  6175. goto end_unlock;
  6176. }
  6177. }
  6178. /* Reconfigure hardware for allowing smaller MSS in the case
  6179. * of TSO, so that we avoid the MDD being fired and causing
  6180. * a reset in the case of small MSS+TSO.
  6181. */
  6182. #define I40E_REG_MSS 0x000E64DC
  6183. #define I40E_REG_MSS_MIN_MASK 0x3FF0000
  6184. #define I40E_64BYTE_MSS 0x400000
  6185. val = rd32(hw, I40E_REG_MSS);
  6186. if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
  6187. val &= ~I40E_REG_MSS_MIN_MASK;
  6188. val |= I40E_64BYTE_MSS;
  6189. wr32(hw, I40E_REG_MSS, val);
  6190. }
  6191. if (pf->flags & I40E_FLAG_RESTART_AUTONEG) {
  6192. msleep(75);
  6193. ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
  6194. if (ret)
  6195. dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
  6196. i40e_stat_str(&pf->hw, ret),
  6197. i40e_aq_str(&pf->hw,
  6198. pf->hw.aq.asq_last_status));
  6199. }
  6200. /* reinit the misc interrupt */
  6201. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  6202. ret = i40e_setup_misc_vector(pf);
  6203. /* Add a filter to drop all Flow control frames from any VSI from being
  6204. * transmitted. By doing so we stop a malicious VF from sending out
  6205. * PAUSE or PFC frames and potentially controlling traffic for other
  6206. * PF/VF VSIs.
  6207. * The FW can still send Flow control frames if enabled.
  6208. */
  6209. i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
  6210. pf->main_vsi_seid);
  6211. /* restart the VSIs that were rebuilt and running before the reset */
  6212. i40e_pf_unquiesce_all_vsi(pf);
  6213. if (pf->num_alloc_vfs) {
  6214. for (v = 0; v < pf->num_alloc_vfs; v++)
  6215. i40e_reset_vf(&pf->vf[v], true);
  6216. }
  6217. /* tell the firmware that we're starting */
  6218. i40e_send_version(pf);
  6219. end_unlock:
  6220. if (!lock_acquired)
  6221. rtnl_unlock();
  6222. end_core_reset:
  6223. clear_bit(__I40E_RESET_FAILED, &pf->state);
  6224. clear_recovery:
  6225. clear_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state);
  6226. }
  6227. /**
  6228. * i40e_reset_and_rebuild - reset and rebuild using a saved config
  6229. * @pf: board private structure
  6230. * @reinit: if the Main VSI needs to re-initialized.
  6231. * @lock_acquired: indicates whether or not the lock has been acquired
  6232. * before this function was called.
  6233. **/
  6234. static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
  6235. bool lock_acquired)
  6236. {
  6237. int ret;
  6238. /* Now we wait for GRST to settle out.
  6239. * We don't have to delete the VEBs or VSIs from the hw switch
  6240. * because the reset will make them disappear.
  6241. */
  6242. ret = i40e_reset(pf);
  6243. if (!ret)
  6244. i40e_rebuild(pf, reinit, lock_acquired);
  6245. }
  6246. /**
  6247. * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
  6248. * @pf: board private structure
  6249. *
  6250. * Close up the VFs and other things in prep for a Core Reset,
  6251. * then get ready to rebuild the world.
  6252. * @lock_acquired: indicates whether or not the lock has been acquired
  6253. * before this function was called.
  6254. **/
  6255. static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired)
  6256. {
  6257. i40e_prep_for_reset(pf, lock_acquired);
  6258. i40e_reset_and_rebuild(pf, false, lock_acquired);
  6259. }
  6260. /**
  6261. * i40e_handle_mdd_event
  6262. * @pf: pointer to the PF structure
  6263. *
  6264. * Called from the MDD irq handler to identify possibly malicious vfs
  6265. **/
  6266. static void i40e_handle_mdd_event(struct i40e_pf *pf)
  6267. {
  6268. struct i40e_hw *hw = &pf->hw;
  6269. bool mdd_detected = false;
  6270. bool pf_mdd_detected = false;
  6271. struct i40e_vf *vf;
  6272. u32 reg;
  6273. int i;
  6274. if (!test_bit(__I40E_MDD_EVENT_PENDING, &pf->state))
  6275. return;
  6276. /* find what triggered the MDD event */
  6277. reg = rd32(hw, I40E_GL_MDET_TX);
  6278. if (reg & I40E_GL_MDET_TX_VALID_MASK) {
  6279. u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
  6280. I40E_GL_MDET_TX_PF_NUM_SHIFT;
  6281. u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
  6282. I40E_GL_MDET_TX_VF_NUM_SHIFT;
  6283. u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
  6284. I40E_GL_MDET_TX_EVENT_SHIFT;
  6285. u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
  6286. I40E_GL_MDET_TX_QUEUE_SHIFT) -
  6287. pf->hw.func_caps.base_queue;
  6288. if (netif_msg_tx_err(pf))
  6289. dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on TX queue %d PF number 0x%02x VF number 0x%02x\n",
  6290. event, queue, pf_num, vf_num);
  6291. wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
  6292. mdd_detected = true;
  6293. }
  6294. reg = rd32(hw, I40E_GL_MDET_RX);
  6295. if (reg & I40E_GL_MDET_RX_VALID_MASK) {
  6296. u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
  6297. I40E_GL_MDET_RX_FUNCTION_SHIFT;
  6298. u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
  6299. I40E_GL_MDET_RX_EVENT_SHIFT;
  6300. u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
  6301. I40E_GL_MDET_RX_QUEUE_SHIFT) -
  6302. pf->hw.func_caps.base_queue;
  6303. if (netif_msg_rx_err(pf))
  6304. dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
  6305. event, queue, func);
  6306. wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
  6307. mdd_detected = true;
  6308. }
  6309. if (mdd_detected) {
  6310. reg = rd32(hw, I40E_PF_MDET_TX);
  6311. if (reg & I40E_PF_MDET_TX_VALID_MASK) {
  6312. wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
  6313. dev_info(&pf->pdev->dev, "TX driver issue detected, PF reset issued\n");
  6314. pf_mdd_detected = true;
  6315. }
  6316. reg = rd32(hw, I40E_PF_MDET_RX);
  6317. if (reg & I40E_PF_MDET_RX_VALID_MASK) {
  6318. wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
  6319. dev_info(&pf->pdev->dev, "RX driver issue detected, PF reset issued\n");
  6320. pf_mdd_detected = true;
  6321. }
  6322. /* Queue belongs to the PF, initiate a reset */
  6323. if (pf_mdd_detected) {
  6324. set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
  6325. i40e_service_event_schedule(pf);
  6326. }
  6327. }
  6328. /* see if one of the VFs needs its hand slapped */
  6329. for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
  6330. vf = &(pf->vf[i]);
  6331. reg = rd32(hw, I40E_VP_MDET_TX(i));
  6332. if (reg & I40E_VP_MDET_TX_VALID_MASK) {
  6333. wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
  6334. vf->num_mdd_events++;
  6335. dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
  6336. i);
  6337. }
  6338. reg = rd32(hw, I40E_VP_MDET_RX(i));
  6339. if (reg & I40E_VP_MDET_RX_VALID_MASK) {
  6340. wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
  6341. vf->num_mdd_events++;
  6342. dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
  6343. i);
  6344. }
  6345. if (vf->num_mdd_events > I40E_DEFAULT_NUM_MDD_EVENTS_ALLOWED) {
  6346. dev_info(&pf->pdev->dev,
  6347. "Too many MDD events on VF %d, disabled\n", i);
  6348. dev_info(&pf->pdev->dev,
  6349. "Use PF Control I/F to re-enable the VF\n");
  6350. set_bit(I40E_VF_STAT_DISABLED, &vf->vf_states);
  6351. }
  6352. }
  6353. /* re-enable mdd interrupt cause */
  6354. clear_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
  6355. reg = rd32(hw, I40E_PFINT_ICR0_ENA);
  6356. reg |= I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
  6357. wr32(hw, I40E_PFINT_ICR0_ENA, reg);
  6358. i40e_flush(hw);
  6359. }
  6360. /**
  6361. * i40e_sync_udp_filters_subtask - Sync the VSI filter list with HW
  6362. * @pf: board private structure
  6363. **/
  6364. static void i40e_sync_udp_filters_subtask(struct i40e_pf *pf)
  6365. {
  6366. struct i40e_hw *hw = &pf->hw;
  6367. i40e_status ret;
  6368. u16 port;
  6369. int i;
  6370. if (!(pf->flags & I40E_FLAG_UDP_FILTER_SYNC))
  6371. return;
  6372. pf->flags &= ~I40E_FLAG_UDP_FILTER_SYNC;
  6373. for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
  6374. if (pf->pending_udp_bitmap & BIT_ULL(i)) {
  6375. pf->pending_udp_bitmap &= ~BIT_ULL(i);
  6376. port = pf->udp_ports[i].index;
  6377. if (port)
  6378. ret = i40e_aq_add_udp_tunnel(hw, port,
  6379. pf->udp_ports[i].type,
  6380. NULL, NULL);
  6381. else
  6382. ret = i40e_aq_del_udp_tunnel(hw, i, NULL);
  6383. if (ret) {
  6384. dev_dbg(&pf->pdev->dev,
  6385. "%s %s port %d, index %d failed, err %s aq_err %s\n",
  6386. pf->udp_ports[i].type ? "vxlan" : "geneve",
  6387. port ? "add" : "delete",
  6388. port, i,
  6389. i40e_stat_str(&pf->hw, ret),
  6390. i40e_aq_str(&pf->hw,
  6391. pf->hw.aq.asq_last_status));
  6392. pf->udp_ports[i].index = 0;
  6393. }
  6394. }
  6395. }
  6396. }
  6397. /**
  6398. * i40e_service_task - Run the driver's async subtasks
  6399. * @work: pointer to work_struct containing our data
  6400. **/
  6401. static void i40e_service_task(struct work_struct *work)
  6402. {
  6403. struct i40e_pf *pf = container_of(work,
  6404. struct i40e_pf,
  6405. service_task);
  6406. unsigned long start_time = jiffies;
  6407. /* don't bother with service tasks if a reset is in progress */
  6408. if (test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
  6409. return;
  6410. }
  6411. if (test_and_set_bit(__I40E_SERVICE_SCHED, &pf->state))
  6412. return;
  6413. i40e_detect_recover_hung(pf);
  6414. i40e_sync_filters_subtask(pf);
  6415. i40e_reset_subtask(pf);
  6416. i40e_handle_mdd_event(pf);
  6417. i40e_vc_process_vflr_event(pf);
  6418. i40e_watchdog_subtask(pf);
  6419. i40e_fdir_reinit_subtask(pf);
  6420. if (pf->flags & I40E_FLAG_CLIENT_RESET) {
  6421. /* Client subtask will reopen next time through. */
  6422. i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], true);
  6423. pf->flags &= ~I40E_FLAG_CLIENT_RESET;
  6424. } else {
  6425. i40e_client_subtask(pf);
  6426. if (pf->flags & I40E_FLAG_CLIENT_L2_CHANGE) {
  6427. i40e_notify_client_of_l2_param_changes(
  6428. pf->vsi[pf->lan_vsi]);
  6429. pf->flags &= ~I40E_FLAG_CLIENT_L2_CHANGE;
  6430. }
  6431. }
  6432. i40e_sync_filters_subtask(pf);
  6433. i40e_sync_udp_filters_subtask(pf);
  6434. i40e_clean_adminq_subtask(pf);
  6435. /* flush memory to make sure state is correct before next watchdog */
  6436. smp_mb__before_atomic();
  6437. clear_bit(__I40E_SERVICE_SCHED, &pf->state);
  6438. /* If the tasks have taken longer than one timer cycle or there
  6439. * is more work to be done, reschedule the service task now
  6440. * rather than wait for the timer to tick again.
  6441. */
  6442. if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
  6443. test_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state) ||
  6444. test_bit(__I40E_MDD_EVENT_PENDING, &pf->state) ||
  6445. test_bit(__I40E_VFLR_EVENT_PENDING, &pf->state))
  6446. i40e_service_event_schedule(pf);
  6447. }
  6448. /**
  6449. * i40e_service_timer - timer callback
  6450. * @data: pointer to PF struct
  6451. **/
  6452. static void i40e_service_timer(unsigned long data)
  6453. {
  6454. struct i40e_pf *pf = (struct i40e_pf *)data;
  6455. mod_timer(&pf->service_timer,
  6456. round_jiffies(jiffies + pf->service_timer_period));
  6457. i40e_service_event_schedule(pf);
  6458. }
  6459. /**
  6460. * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
  6461. * @vsi: the VSI being configured
  6462. **/
  6463. static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
  6464. {
  6465. struct i40e_pf *pf = vsi->back;
  6466. switch (vsi->type) {
  6467. case I40E_VSI_MAIN:
  6468. vsi->alloc_queue_pairs = pf->num_lan_qps;
  6469. vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
  6470. I40E_REQ_DESCRIPTOR_MULTIPLE);
  6471. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  6472. vsi->num_q_vectors = pf->num_lan_msix;
  6473. else
  6474. vsi->num_q_vectors = 1;
  6475. break;
  6476. case I40E_VSI_FDIR:
  6477. vsi->alloc_queue_pairs = 1;
  6478. vsi->num_desc = ALIGN(I40E_FDIR_RING_COUNT,
  6479. I40E_REQ_DESCRIPTOR_MULTIPLE);
  6480. vsi->num_q_vectors = pf->num_fdsb_msix;
  6481. break;
  6482. case I40E_VSI_VMDQ2:
  6483. vsi->alloc_queue_pairs = pf->num_vmdq_qps;
  6484. vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
  6485. I40E_REQ_DESCRIPTOR_MULTIPLE);
  6486. vsi->num_q_vectors = pf->num_vmdq_msix;
  6487. break;
  6488. case I40E_VSI_SRIOV:
  6489. vsi->alloc_queue_pairs = pf->num_vf_qps;
  6490. vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
  6491. I40E_REQ_DESCRIPTOR_MULTIPLE);
  6492. break;
  6493. default:
  6494. WARN_ON(1);
  6495. return -ENODATA;
  6496. }
  6497. return 0;
  6498. }
  6499. /**
  6500. * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
  6501. * @type: VSI pointer
  6502. * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
  6503. *
  6504. * On error: returns error code (negative)
  6505. * On success: returns 0
  6506. **/
  6507. static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
  6508. {
  6509. int size;
  6510. int ret = 0;
  6511. /* allocate memory for both Tx and Rx ring pointers */
  6512. size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs * 2;
  6513. vsi->tx_rings = kzalloc(size, GFP_KERNEL);
  6514. if (!vsi->tx_rings)
  6515. return -ENOMEM;
  6516. vsi->rx_rings = &vsi->tx_rings[vsi->alloc_queue_pairs];
  6517. if (alloc_qvectors) {
  6518. /* allocate memory for q_vector pointers */
  6519. size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
  6520. vsi->q_vectors = kzalloc(size, GFP_KERNEL);
  6521. if (!vsi->q_vectors) {
  6522. ret = -ENOMEM;
  6523. goto err_vectors;
  6524. }
  6525. }
  6526. return ret;
  6527. err_vectors:
  6528. kfree(vsi->tx_rings);
  6529. return ret;
  6530. }
  6531. /**
  6532. * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
  6533. * @pf: board private structure
  6534. * @type: type of VSI
  6535. *
  6536. * On error: returns error code (negative)
  6537. * On success: returns vsi index in PF (positive)
  6538. **/
  6539. static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
  6540. {
  6541. int ret = -ENODEV;
  6542. struct i40e_vsi *vsi;
  6543. int vsi_idx;
  6544. int i;
  6545. /* Need to protect the allocation of the VSIs at the PF level */
  6546. mutex_lock(&pf->switch_mutex);
  6547. /* VSI list may be fragmented if VSI creation/destruction has
  6548. * been happening. We can afford to do a quick scan to look
  6549. * for any free VSIs in the list.
  6550. *
  6551. * find next empty vsi slot, looping back around if necessary
  6552. */
  6553. i = pf->next_vsi;
  6554. while (i < pf->num_alloc_vsi && pf->vsi[i])
  6555. i++;
  6556. if (i >= pf->num_alloc_vsi) {
  6557. i = 0;
  6558. while (i < pf->next_vsi && pf->vsi[i])
  6559. i++;
  6560. }
  6561. if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
  6562. vsi_idx = i; /* Found one! */
  6563. } else {
  6564. ret = -ENODEV;
  6565. goto unlock_pf; /* out of VSI slots! */
  6566. }
  6567. pf->next_vsi = ++i;
  6568. vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
  6569. if (!vsi) {
  6570. ret = -ENOMEM;
  6571. goto unlock_pf;
  6572. }
  6573. vsi->type = type;
  6574. vsi->back = pf;
  6575. set_bit(__I40E_DOWN, &vsi->state);
  6576. vsi->flags = 0;
  6577. vsi->idx = vsi_idx;
  6578. vsi->int_rate_limit = 0;
  6579. vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
  6580. pf->rss_table_size : 64;
  6581. vsi->netdev_registered = false;
  6582. vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
  6583. hash_init(vsi->mac_filter_hash);
  6584. vsi->irqs_ready = false;
  6585. ret = i40e_set_num_rings_in_vsi(vsi);
  6586. if (ret)
  6587. goto err_rings;
  6588. ret = i40e_vsi_alloc_arrays(vsi, true);
  6589. if (ret)
  6590. goto err_rings;
  6591. /* Setup default MSIX irq handler for VSI */
  6592. i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
  6593. /* Initialize VSI lock */
  6594. spin_lock_init(&vsi->mac_filter_hash_lock);
  6595. pf->vsi[vsi_idx] = vsi;
  6596. ret = vsi_idx;
  6597. goto unlock_pf;
  6598. err_rings:
  6599. pf->next_vsi = i - 1;
  6600. kfree(vsi);
  6601. unlock_pf:
  6602. mutex_unlock(&pf->switch_mutex);
  6603. return ret;
  6604. }
  6605. /**
  6606. * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
  6607. * @type: VSI pointer
  6608. * @free_qvectors: a bool to specify if q_vectors need to be freed.
  6609. *
  6610. * On error: returns error code (negative)
  6611. * On success: returns 0
  6612. **/
  6613. static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
  6614. {
  6615. /* free the ring and vector containers */
  6616. if (free_qvectors) {
  6617. kfree(vsi->q_vectors);
  6618. vsi->q_vectors = NULL;
  6619. }
  6620. kfree(vsi->tx_rings);
  6621. vsi->tx_rings = NULL;
  6622. vsi->rx_rings = NULL;
  6623. }
  6624. /**
  6625. * i40e_clear_rss_config_user - clear the user configured RSS hash keys
  6626. * and lookup table
  6627. * @vsi: Pointer to VSI structure
  6628. */
  6629. static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
  6630. {
  6631. if (!vsi)
  6632. return;
  6633. kfree(vsi->rss_hkey_user);
  6634. vsi->rss_hkey_user = NULL;
  6635. kfree(vsi->rss_lut_user);
  6636. vsi->rss_lut_user = NULL;
  6637. }
  6638. /**
  6639. * i40e_vsi_clear - Deallocate the VSI provided
  6640. * @vsi: the VSI being un-configured
  6641. **/
  6642. static int i40e_vsi_clear(struct i40e_vsi *vsi)
  6643. {
  6644. struct i40e_pf *pf;
  6645. if (!vsi)
  6646. return 0;
  6647. if (!vsi->back)
  6648. goto free_vsi;
  6649. pf = vsi->back;
  6650. mutex_lock(&pf->switch_mutex);
  6651. if (!pf->vsi[vsi->idx]) {
  6652. dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](%p,type %d)\n",
  6653. vsi->idx, vsi->idx, vsi, vsi->type);
  6654. goto unlock_vsi;
  6655. }
  6656. if (pf->vsi[vsi->idx] != vsi) {
  6657. dev_err(&pf->pdev->dev,
  6658. "pf->vsi[%d](%p, type %d) != vsi[%d](%p,type %d): no free!\n",
  6659. pf->vsi[vsi->idx]->idx,
  6660. pf->vsi[vsi->idx],
  6661. pf->vsi[vsi->idx]->type,
  6662. vsi->idx, vsi, vsi->type);
  6663. goto unlock_vsi;
  6664. }
  6665. /* updates the PF for this cleared vsi */
  6666. i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
  6667. i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
  6668. i40e_vsi_free_arrays(vsi, true);
  6669. i40e_clear_rss_config_user(vsi);
  6670. pf->vsi[vsi->idx] = NULL;
  6671. if (vsi->idx < pf->next_vsi)
  6672. pf->next_vsi = vsi->idx;
  6673. unlock_vsi:
  6674. mutex_unlock(&pf->switch_mutex);
  6675. free_vsi:
  6676. kfree(vsi);
  6677. return 0;
  6678. }
  6679. /**
  6680. * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
  6681. * @vsi: the VSI being cleaned
  6682. **/
  6683. static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
  6684. {
  6685. int i;
  6686. if (vsi->tx_rings && vsi->tx_rings[0]) {
  6687. for (i = 0; i < vsi->alloc_queue_pairs; i++) {
  6688. kfree_rcu(vsi->tx_rings[i], rcu);
  6689. vsi->tx_rings[i] = NULL;
  6690. vsi->rx_rings[i] = NULL;
  6691. }
  6692. }
  6693. }
  6694. /**
  6695. * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
  6696. * @vsi: the VSI being configured
  6697. **/
  6698. static int i40e_alloc_rings(struct i40e_vsi *vsi)
  6699. {
  6700. struct i40e_ring *tx_ring, *rx_ring;
  6701. struct i40e_pf *pf = vsi->back;
  6702. int i;
  6703. /* Set basic values in the rings to be used later during open() */
  6704. for (i = 0; i < vsi->alloc_queue_pairs; i++) {
  6705. /* allocate space for both Tx and Rx in one shot */
  6706. tx_ring = kzalloc(sizeof(struct i40e_ring) * 2, GFP_KERNEL);
  6707. if (!tx_ring)
  6708. goto err_out;
  6709. tx_ring->queue_index = i;
  6710. tx_ring->reg_idx = vsi->base_queue + i;
  6711. tx_ring->ring_active = false;
  6712. tx_ring->vsi = vsi;
  6713. tx_ring->netdev = vsi->netdev;
  6714. tx_ring->dev = &pf->pdev->dev;
  6715. tx_ring->count = vsi->num_desc;
  6716. tx_ring->size = 0;
  6717. tx_ring->dcb_tc = 0;
  6718. if (vsi->back->flags & I40E_FLAG_WB_ON_ITR_CAPABLE)
  6719. tx_ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
  6720. tx_ring->tx_itr_setting = pf->tx_itr_default;
  6721. vsi->tx_rings[i] = tx_ring;
  6722. rx_ring = &tx_ring[1];
  6723. rx_ring->queue_index = i;
  6724. rx_ring->reg_idx = vsi->base_queue + i;
  6725. rx_ring->ring_active = false;
  6726. rx_ring->vsi = vsi;
  6727. rx_ring->netdev = vsi->netdev;
  6728. rx_ring->dev = &pf->pdev->dev;
  6729. rx_ring->count = vsi->num_desc;
  6730. rx_ring->size = 0;
  6731. rx_ring->dcb_tc = 0;
  6732. rx_ring->rx_itr_setting = pf->rx_itr_default;
  6733. vsi->rx_rings[i] = rx_ring;
  6734. }
  6735. return 0;
  6736. err_out:
  6737. i40e_vsi_clear_rings(vsi);
  6738. return -ENOMEM;
  6739. }
  6740. /**
  6741. * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
  6742. * @pf: board private structure
  6743. * @vectors: the number of MSI-X vectors to request
  6744. *
  6745. * Returns the number of vectors reserved, or error
  6746. **/
  6747. static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
  6748. {
  6749. vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
  6750. I40E_MIN_MSIX, vectors);
  6751. if (vectors < 0) {
  6752. dev_info(&pf->pdev->dev,
  6753. "MSI-X vector reservation failed: %d\n", vectors);
  6754. vectors = 0;
  6755. }
  6756. return vectors;
  6757. }
  6758. /**
  6759. * i40e_init_msix - Setup the MSIX capability
  6760. * @pf: board private structure
  6761. *
  6762. * Work with the OS to set up the MSIX vectors needed.
  6763. *
  6764. * Returns the number of vectors reserved or negative on failure
  6765. **/
  6766. static int i40e_init_msix(struct i40e_pf *pf)
  6767. {
  6768. struct i40e_hw *hw = &pf->hw;
  6769. int cpus, extra_vectors;
  6770. int vectors_left;
  6771. int v_budget, i;
  6772. int v_actual;
  6773. int iwarp_requested = 0;
  6774. if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
  6775. return -ENODEV;
  6776. /* The number of vectors we'll request will be comprised of:
  6777. * - Add 1 for "other" cause for Admin Queue events, etc.
  6778. * - The number of LAN queue pairs
  6779. * - Queues being used for RSS.
  6780. * We don't need as many as max_rss_size vectors.
  6781. * use rss_size instead in the calculation since that
  6782. * is governed by number of cpus in the system.
  6783. * - assumes symmetric Tx/Rx pairing
  6784. * - The number of VMDq pairs
  6785. * - The CPU count within the NUMA node if iWARP is enabled
  6786. * Once we count this up, try the request.
  6787. *
  6788. * If we can't get what we want, we'll simplify to nearly nothing
  6789. * and try again. If that still fails, we punt.
  6790. */
  6791. vectors_left = hw->func_caps.num_msix_vectors;
  6792. v_budget = 0;
  6793. /* reserve one vector for miscellaneous handler */
  6794. if (vectors_left) {
  6795. v_budget++;
  6796. vectors_left--;
  6797. }
  6798. /* reserve some vectors for the main PF traffic queues. Initially we
  6799. * only reserve at most 50% of the available vectors, in the case that
  6800. * the number of online CPUs is large. This ensures that we can enable
  6801. * extra features as well. Once we've enabled the other features, we
  6802. * will use any remaining vectors to reach as close as we can to the
  6803. * number of online CPUs.
  6804. */
  6805. cpus = num_online_cpus();
  6806. pf->num_lan_msix = min_t(int, cpus, vectors_left / 2);
  6807. vectors_left -= pf->num_lan_msix;
  6808. /* reserve one vector for sideband flow director */
  6809. if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
  6810. if (vectors_left) {
  6811. pf->num_fdsb_msix = 1;
  6812. v_budget++;
  6813. vectors_left--;
  6814. } else {
  6815. pf->num_fdsb_msix = 0;
  6816. }
  6817. }
  6818. /* can we reserve enough for iWARP? */
  6819. if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
  6820. iwarp_requested = pf->num_iwarp_msix;
  6821. if (!vectors_left)
  6822. pf->num_iwarp_msix = 0;
  6823. else if (vectors_left < pf->num_iwarp_msix)
  6824. pf->num_iwarp_msix = 1;
  6825. v_budget += pf->num_iwarp_msix;
  6826. vectors_left -= pf->num_iwarp_msix;
  6827. }
  6828. /* any vectors left over go for VMDq support */
  6829. if (pf->flags & I40E_FLAG_VMDQ_ENABLED) {
  6830. int vmdq_vecs_wanted = pf->num_vmdq_vsis * pf->num_vmdq_qps;
  6831. int vmdq_vecs = min_t(int, vectors_left, vmdq_vecs_wanted);
  6832. if (!vectors_left) {
  6833. pf->num_vmdq_msix = 0;
  6834. pf->num_vmdq_qps = 0;
  6835. } else {
  6836. /* if we're short on vectors for what's desired, we limit
  6837. * the queues per vmdq. If this is still more than are
  6838. * available, the user will need to change the number of
  6839. * queues/vectors used by the PF later with the ethtool
  6840. * channels command
  6841. */
  6842. if (vmdq_vecs < vmdq_vecs_wanted)
  6843. pf->num_vmdq_qps = 1;
  6844. pf->num_vmdq_msix = pf->num_vmdq_qps;
  6845. v_budget += vmdq_vecs;
  6846. vectors_left -= vmdq_vecs;
  6847. }
  6848. }
  6849. /* On systems with a large number of SMP cores, we previously limited
  6850. * the number of vectors for num_lan_msix to be at most 50% of the
  6851. * available vectors, to allow for other features. Now, we add back
  6852. * the remaining vectors. However, we ensure that the total
  6853. * num_lan_msix will not exceed num_online_cpus(). To do this, we
  6854. * calculate the number of vectors we can add without going over the
  6855. * cap of CPUs. For systems with a small number of CPUs this will be
  6856. * zero.
  6857. */
  6858. extra_vectors = min_t(int, cpus - pf->num_lan_msix, vectors_left);
  6859. pf->num_lan_msix += extra_vectors;
  6860. vectors_left -= extra_vectors;
  6861. WARN(vectors_left < 0,
  6862. "Calculation of remaining vectors underflowed. This is an accounting bug when determining total MSI-X vectors.\n");
  6863. v_budget += pf->num_lan_msix;
  6864. pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
  6865. GFP_KERNEL);
  6866. if (!pf->msix_entries)
  6867. return -ENOMEM;
  6868. for (i = 0; i < v_budget; i++)
  6869. pf->msix_entries[i].entry = i;
  6870. v_actual = i40e_reserve_msix_vectors(pf, v_budget);
  6871. if (v_actual < I40E_MIN_MSIX) {
  6872. pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
  6873. kfree(pf->msix_entries);
  6874. pf->msix_entries = NULL;
  6875. pci_disable_msix(pf->pdev);
  6876. return -ENODEV;
  6877. } else if (v_actual == I40E_MIN_MSIX) {
  6878. /* Adjust for minimal MSIX use */
  6879. pf->num_vmdq_vsis = 0;
  6880. pf->num_vmdq_qps = 0;
  6881. pf->num_lan_qps = 1;
  6882. pf->num_lan_msix = 1;
  6883. } else if (!vectors_left) {
  6884. /* If we have limited resources, we will start with no vectors
  6885. * for the special features and then allocate vectors to some
  6886. * of these features based on the policy and at the end disable
  6887. * the features that did not get any vectors.
  6888. */
  6889. int vec;
  6890. dev_info(&pf->pdev->dev,
  6891. "MSI-X vector limit reached, attempting to redistribute vectors\n");
  6892. /* reserve the misc vector */
  6893. vec = v_actual - 1;
  6894. /* Scale vector usage down */
  6895. pf->num_vmdq_msix = 1; /* force VMDqs to only one vector */
  6896. pf->num_vmdq_vsis = 1;
  6897. pf->num_vmdq_qps = 1;
  6898. /* partition out the remaining vectors */
  6899. switch (vec) {
  6900. case 2:
  6901. pf->num_lan_msix = 1;
  6902. break;
  6903. case 3:
  6904. if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
  6905. pf->num_lan_msix = 1;
  6906. pf->num_iwarp_msix = 1;
  6907. } else {
  6908. pf->num_lan_msix = 2;
  6909. }
  6910. break;
  6911. default:
  6912. if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
  6913. pf->num_iwarp_msix = min_t(int, (vec / 3),
  6914. iwarp_requested);
  6915. pf->num_vmdq_vsis = min_t(int, (vec / 3),
  6916. I40E_DEFAULT_NUM_VMDQ_VSI);
  6917. } else {
  6918. pf->num_vmdq_vsis = min_t(int, (vec / 2),
  6919. I40E_DEFAULT_NUM_VMDQ_VSI);
  6920. }
  6921. if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
  6922. pf->num_fdsb_msix = 1;
  6923. vec--;
  6924. }
  6925. pf->num_lan_msix = min_t(int,
  6926. (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
  6927. pf->num_lan_msix);
  6928. pf->num_lan_qps = pf->num_lan_msix;
  6929. break;
  6930. }
  6931. }
  6932. if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
  6933. (pf->num_fdsb_msix == 0)) {
  6934. dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n");
  6935. pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
  6936. }
  6937. if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
  6938. (pf->num_vmdq_msix == 0)) {
  6939. dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
  6940. pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
  6941. }
  6942. if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
  6943. (pf->num_iwarp_msix == 0)) {
  6944. dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
  6945. pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
  6946. }
  6947. i40e_debug(&pf->hw, I40E_DEBUG_INIT,
  6948. "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n",
  6949. pf->num_lan_msix,
  6950. pf->num_vmdq_msix * pf->num_vmdq_vsis,
  6951. pf->num_fdsb_msix,
  6952. pf->num_iwarp_msix);
  6953. return v_actual;
  6954. }
  6955. /**
  6956. * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
  6957. * @vsi: the VSI being configured
  6958. * @v_idx: index of the vector in the vsi struct
  6959. * @cpu: cpu to be used on affinity_mask
  6960. *
  6961. * We allocate one q_vector. If allocation fails we return -ENOMEM.
  6962. **/
  6963. static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx, int cpu)
  6964. {
  6965. struct i40e_q_vector *q_vector;
  6966. /* allocate q_vector */
  6967. q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
  6968. if (!q_vector)
  6969. return -ENOMEM;
  6970. q_vector->vsi = vsi;
  6971. q_vector->v_idx = v_idx;
  6972. cpumask_set_cpu(cpu, &q_vector->affinity_mask);
  6973. if (vsi->netdev)
  6974. netif_napi_add(vsi->netdev, &q_vector->napi,
  6975. i40e_napi_poll, NAPI_POLL_WEIGHT);
  6976. q_vector->rx.latency_range = I40E_LOW_LATENCY;
  6977. q_vector->tx.latency_range = I40E_LOW_LATENCY;
  6978. /* tie q_vector and vsi together */
  6979. vsi->q_vectors[v_idx] = q_vector;
  6980. return 0;
  6981. }
  6982. /**
  6983. * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
  6984. * @vsi: the VSI being configured
  6985. *
  6986. * We allocate one q_vector per queue interrupt. If allocation fails we
  6987. * return -ENOMEM.
  6988. **/
  6989. static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
  6990. {
  6991. struct i40e_pf *pf = vsi->back;
  6992. int err, v_idx, num_q_vectors, current_cpu;
  6993. /* if not MSIX, give the one vector only to the LAN VSI */
  6994. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  6995. num_q_vectors = vsi->num_q_vectors;
  6996. else if (vsi == pf->vsi[pf->lan_vsi])
  6997. num_q_vectors = 1;
  6998. else
  6999. return -EINVAL;
  7000. current_cpu = cpumask_first(cpu_online_mask);
  7001. for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
  7002. err = i40e_vsi_alloc_q_vector(vsi, v_idx, current_cpu);
  7003. if (err)
  7004. goto err_out;
  7005. current_cpu = cpumask_next(current_cpu, cpu_online_mask);
  7006. if (unlikely(current_cpu >= nr_cpu_ids))
  7007. current_cpu = cpumask_first(cpu_online_mask);
  7008. }
  7009. return 0;
  7010. err_out:
  7011. while (v_idx--)
  7012. i40e_free_q_vector(vsi, v_idx);
  7013. return err;
  7014. }
  7015. /**
  7016. * i40e_init_interrupt_scheme - Determine proper interrupt scheme
  7017. * @pf: board private structure to initialize
  7018. **/
  7019. static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
  7020. {
  7021. int vectors = 0;
  7022. ssize_t size;
  7023. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  7024. vectors = i40e_init_msix(pf);
  7025. if (vectors < 0) {
  7026. pf->flags &= ~(I40E_FLAG_MSIX_ENABLED |
  7027. I40E_FLAG_IWARP_ENABLED |
  7028. I40E_FLAG_RSS_ENABLED |
  7029. I40E_FLAG_DCB_CAPABLE |
  7030. I40E_FLAG_DCB_ENABLED |
  7031. I40E_FLAG_SRIOV_ENABLED |
  7032. I40E_FLAG_FD_SB_ENABLED |
  7033. I40E_FLAG_FD_ATR_ENABLED |
  7034. I40E_FLAG_VMDQ_ENABLED);
  7035. /* rework the queue expectations without MSIX */
  7036. i40e_determine_queue_usage(pf);
  7037. }
  7038. }
  7039. if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
  7040. (pf->flags & I40E_FLAG_MSI_ENABLED)) {
  7041. dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
  7042. vectors = pci_enable_msi(pf->pdev);
  7043. if (vectors < 0) {
  7044. dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
  7045. vectors);
  7046. pf->flags &= ~I40E_FLAG_MSI_ENABLED;
  7047. }
  7048. vectors = 1; /* one MSI or Legacy vector */
  7049. }
  7050. if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
  7051. dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
  7052. /* set up vector assignment tracking */
  7053. size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
  7054. pf->irq_pile = kzalloc(size, GFP_KERNEL);
  7055. if (!pf->irq_pile) {
  7056. dev_err(&pf->pdev->dev, "error allocating irq_pile memory\n");
  7057. return -ENOMEM;
  7058. }
  7059. pf->irq_pile->num_entries = vectors;
  7060. pf->irq_pile->search_hint = 0;
  7061. /* track first vector for misc interrupts, ignore return */
  7062. (void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
  7063. return 0;
  7064. }
  7065. /**
  7066. * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
  7067. * @pf: board private structure
  7068. *
  7069. * This sets up the handler for MSIX 0, which is used to manage the
  7070. * non-queue interrupts, e.g. AdminQ and errors. This is not used
  7071. * when in MSI or Legacy interrupt mode.
  7072. **/
  7073. static int i40e_setup_misc_vector(struct i40e_pf *pf)
  7074. {
  7075. struct i40e_hw *hw = &pf->hw;
  7076. int err = 0;
  7077. /* Only request the irq if this is the first time through, and
  7078. * not when we're rebuilding after a Reset
  7079. */
  7080. if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
  7081. err = request_irq(pf->msix_entries[0].vector,
  7082. i40e_intr, 0, pf->int_name, pf);
  7083. if (err) {
  7084. dev_info(&pf->pdev->dev,
  7085. "request_irq for %s failed: %d\n",
  7086. pf->int_name, err);
  7087. return -EFAULT;
  7088. }
  7089. }
  7090. i40e_enable_misc_int_causes(pf);
  7091. /* associate no queues to the misc vector */
  7092. wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
  7093. wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K);
  7094. i40e_flush(hw);
  7095. i40e_irq_dynamic_enable_icr0(pf, true);
  7096. return err;
  7097. }
  7098. /**
  7099. * i40e_config_rss_aq - Prepare for RSS using AQ commands
  7100. * @vsi: vsi structure
  7101. * @seed: RSS hash seed
  7102. **/
  7103. static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
  7104. u8 *lut, u16 lut_size)
  7105. {
  7106. struct i40e_pf *pf = vsi->back;
  7107. struct i40e_hw *hw = &pf->hw;
  7108. int ret = 0;
  7109. if (seed) {
  7110. struct i40e_aqc_get_set_rss_key_data *seed_dw =
  7111. (struct i40e_aqc_get_set_rss_key_data *)seed;
  7112. ret = i40e_aq_set_rss_key(hw, vsi->id, seed_dw);
  7113. if (ret) {
  7114. dev_info(&pf->pdev->dev,
  7115. "Cannot set RSS key, err %s aq_err %s\n",
  7116. i40e_stat_str(hw, ret),
  7117. i40e_aq_str(hw, hw->aq.asq_last_status));
  7118. return ret;
  7119. }
  7120. }
  7121. if (lut) {
  7122. bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
  7123. ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
  7124. if (ret) {
  7125. dev_info(&pf->pdev->dev,
  7126. "Cannot set RSS lut, err %s aq_err %s\n",
  7127. i40e_stat_str(hw, ret),
  7128. i40e_aq_str(hw, hw->aq.asq_last_status));
  7129. return ret;
  7130. }
  7131. }
  7132. return ret;
  7133. }
  7134. /**
  7135. * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
  7136. * @vsi: Pointer to vsi structure
  7137. * @seed: Buffter to store the hash keys
  7138. * @lut: Buffer to store the lookup table entries
  7139. * @lut_size: Size of buffer to store the lookup table entries
  7140. *
  7141. * Return 0 on success, negative on failure
  7142. */
  7143. static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
  7144. u8 *lut, u16 lut_size)
  7145. {
  7146. struct i40e_pf *pf = vsi->back;
  7147. struct i40e_hw *hw = &pf->hw;
  7148. int ret = 0;
  7149. if (seed) {
  7150. ret = i40e_aq_get_rss_key(hw, vsi->id,
  7151. (struct i40e_aqc_get_set_rss_key_data *)seed);
  7152. if (ret) {
  7153. dev_info(&pf->pdev->dev,
  7154. "Cannot get RSS key, err %s aq_err %s\n",
  7155. i40e_stat_str(&pf->hw, ret),
  7156. i40e_aq_str(&pf->hw,
  7157. pf->hw.aq.asq_last_status));
  7158. return ret;
  7159. }
  7160. }
  7161. if (lut) {
  7162. bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
  7163. ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
  7164. if (ret) {
  7165. dev_info(&pf->pdev->dev,
  7166. "Cannot get RSS lut, err %s aq_err %s\n",
  7167. i40e_stat_str(&pf->hw, ret),
  7168. i40e_aq_str(&pf->hw,
  7169. pf->hw.aq.asq_last_status));
  7170. return ret;
  7171. }
  7172. }
  7173. return ret;
  7174. }
  7175. /**
  7176. * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
  7177. * @vsi: VSI structure
  7178. **/
  7179. static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
  7180. {
  7181. u8 seed[I40E_HKEY_ARRAY_SIZE];
  7182. struct i40e_pf *pf = vsi->back;
  7183. u8 *lut;
  7184. int ret;
  7185. if (!(pf->flags & I40E_FLAG_RSS_AQ_CAPABLE))
  7186. return 0;
  7187. if (!vsi->rss_size)
  7188. vsi->rss_size = min_t(int, pf->alloc_rss_size,
  7189. vsi->num_queue_pairs);
  7190. if (!vsi->rss_size)
  7191. return -EINVAL;
  7192. lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
  7193. if (!lut)
  7194. return -ENOMEM;
  7195. /* Use the user configured hash keys and lookup table if there is one,
  7196. * otherwise use default
  7197. */
  7198. if (vsi->rss_lut_user)
  7199. memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
  7200. else
  7201. i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
  7202. if (vsi->rss_hkey_user)
  7203. memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
  7204. else
  7205. netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
  7206. ret = i40e_config_rss_aq(vsi, seed, lut, vsi->rss_table_size);
  7207. kfree(lut);
  7208. return ret;
  7209. }
  7210. /**
  7211. * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
  7212. * @vsi: Pointer to vsi structure
  7213. * @seed: RSS hash seed
  7214. * @lut: Lookup table
  7215. * @lut_size: Lookup table size
  7216. *
  7217. * Returns 0 on success, negative on failure
  7218. **/
  7219. static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
  7220. const u8 *lut, u16 lut_size)
  7221. {
  7222. struct i40e_pf *pf = vsi->back;
  7223. struct i40e_hw *hw = &pf->hw;
  7224. u16 vf_id = vsi->vf_id;
  7225. u8 i;
  7226. /* Fill out hash function seed */
  7227. if (seed) {
  7228. u32 *seed_dw = (u32 *)seed;
  7229. if (vsi->type == I40E_VSI_MAIN) {
  7230. for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
  7231. wr32(hw, I40E_PFQF_HKEY(i), seed_dw[i]);
  7232. } else if (vsi->type == I40E_VSI_SRIOV) {
  7233. for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
  7234. wr32(hw, I40E_VFQF_HKEY1(i, vf_id), seed_dw[i]);
  7235. } else {
  7236. dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
  7237. }
  7238. }
  7239. if (lut) {
  7240. u32 *lut_dw = (u32 *)lut;
  7241. if (vsi->type == I40E_VSI_MAIN) {
  7242. if (lut_size != I40E_HLUT_ARRAY_SIZE)
  7243. return -EINVAL;
  7244. for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
  7245. wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
  7246. } else if (vsi->type == I40E_VSI_SRIOV) {
  7247. if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
  7248. return -EINVAL;
  7249. for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
  7250. wr32(hw, I40E_VFQF_HLUT1(i, vf_id), lut_dw[i]);
  7251. } else {
  7252. dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
  7253. }
  7254. }
  7255. i40e_flush(hw);
  7256. return 0;
  7257. }
  7258. /**
  7259. * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
  7260. * @vsi: Pointer to VSI structure
  7261. * @seed: Buffer to store the keys
  7262. * @lut: Buffer to store the lookup table entries
  7263. * @lut_size: Size of buffer to store the lookup table entries
  7264. *
  7265. * Returns 0 on success, negative on failure
  7266. */
  7267. static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
  7268. u8 *lut, u16 lut_size)
  7269. {
  7270. struct i40e_pf *pf = vsi->back;
  7271. struct i40e_hw *hw = &pf->hw;
  7272. u16 i;
  7273. if (seed) {
  7274. u32 *seed_dw = (u32 *)seed;
  7275. for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
  7276. seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
  7277. }
  7278. if (lut) {
  7279. u32 *lut_dw = (u32 *)lut;
  7280. if (lut_size != I40E_HLUT_ARRAY_SIZE)
  7281. return -EINVAL;
  7282. for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
  7283. lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
  7284. }
  7285. return 0;
  7286. }
  7287. /**
  7288. * i40e_config_rss - Configure RSS keys and lut
  7289. * @vsi: Pointer to VSI structure
  7290. * @seed: RSS hash seed
  7291. * @lut: Lookup table
  7292. * @lut_size: Lookup table size
  7293. *
  7294. * Returns 0 on success, negative on failure
  7295. */
  7296. int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
  7297. {
  7298. struct i40e_pf *pf = vsi->back;
  7299. if (pf->flags & I40E_FLAG_RSS_AQ_CAPABLE)
  7300. return i40e_config_rss_aq(vsi, seed, lut, lut_size);
  7301. else
  7302. return i40e_config_rss_reg(vsi, seed, lut, lut_size);
  7303. }
  7304. /**
  7305. * i40e_get_rss - Get RSS keys and lut
  7306. * @vsi: Pointer to VSI structure
  7307. * @seed: Buffer to store the keys
  7308. * @lut: Buffer to store the lookup table entries
  7309. * lut_size: Size of buffer to store the lookup table entries
  7310. *
  7311. * Returns 0 on success, negative on failure
  7312. */
  7313. int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
  7314. {
  7315. struct i40e_pf *pf = vsi->back;
  7316. if (pf->flags & I40E_FLAG_RSS_AQ_CAPABLE)
  7317. return i40e_get_rss_aq(vsi, seed, lut, lut_size);
  7318. else
  7319. return i40e_get_rss_reg(vsi, seed, lut, lut_size);
  7320. }
  7321. /**
  7322. * i40e_fill_rss_lut - Fill the RSS lookup table with default values
  7323. * @pf: Pointer to board private structure
  7324. * @lut: Lookup table
  7325. * @rss_table_size: Lookup table size
  7326. * @rss_size: Range of queue number for hashing
  7327. */
  7328. void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
  7329. u16 rss_table_size, u16 rss_size)
  7330. {
  7331. u16 i;
  7332. for (i = 0; i < rss_table_size; i++)
  7333. lut[i] = i % rss_size;
  7334. }
  7335. /**
  7336. * i40e_pf_config_rss - Prepare for RSS if used
  7337. * @pf: board private structure
  7338. **/
  7339. static int i40e_pf_config_rss(struct i40e_pf *pf)
  7340. {
  7341. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  7342. u8 seed[I40E_HKEY_ARRAY_SIZE];
  7343. u8 *lut;
  7344. struct i40e_hw *hw = &pf->hw;
  7345. u32 reg_val;
  7346. u64 hena;
  7347. int ret;
  7348. /* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
  7349. hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
  7350. ((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
  7351. hena |= i40e_pf_get_default_rss_hena(pf);
  7352. i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
  7353. i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
  7354. /* Determine the RSS table size based on the hardware capabilities */
  7355. reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
  7356. reg_val = (pf->rss_table_size == 512) ?
  7357. (reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
  7358. (reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
  7359. i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
  7360. /* Determine the RSS size of the VSI */
  7361. if (!vsi->rss_size) {
  7362. u16 qcount;
  7363. qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
  7364. vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
  7365. }
  7366. if (!vsi->rss_size)
  7367. return -EINVAL;
  7368. lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
  7369. if (!lut)
  7370. return -ENOMEM;
  7371. /* Use user configured lut if there is one, otherwise use default */
  7372. if (vsi->rss_lut_user)
  7373. memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
  7374. else
  7375. i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
  7376. /* Use user configured hash key if there is one, otherwise
  7377. * use default.
  7378. */
  7379. if (vsi->rss_hkey_user)
  7380. memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
  7381. else
  7382. netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
  7383. ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
  7384. kfree(lut);
  7385. return ret;
  7386. }
  7387. /**
  7388. * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
  7389. * @pf: board private structure
  7390. * @queue_count: the requested queue count for rss.
  7391. *
  7392. * returns 0 if rss is not enabled, if enabled returns the final rss queue
  7393. * count which may be different from the requested queue count.
  7394. * Note: expects to be called while under rtnl_lock()
  7395. **/
  7396. int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
  7397. {
  7398. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  7399. int new_rss_size;
  7400. if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
  7401. return 0;
  7402. new_rss_size = min_t(int, queue_count, pf->rss_size_max);
  7403. if (queue_count != vsi->num_queue_pairs) {
  7404. u16 qcount;
  7405. vsi->req_queue_pairs = queue_count;
  7406. i40e_prep_for_reset(pf, true);
  7407. pf->alloc_rss_size = new_rss_size;
  7408. i40e_reset_and_rebuild(pf, true, true);
  7409. /* Discard the user configured hash keys and lut, if less
  7410. * queues are enabled.
  7411. */
  7412. if (queue_count < vsi->rss_size) {
  7413. i40e_clear_rss_config_user(vsi);
  7414. dev_dbg(&pf->pdev->dev,
  7415. "discard user configured hash keys and lut\n");
  7416. }
  7417. /* Reset vsi->rss_size, as number of enabled queues changed */
  7418. qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
  7419. vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
  7420. i40e_pf_config_rss(pf);
  7421. }
  7422. dev_info(&pf->pdev->dev, "User requested queue count/HW max RSS count: %d/%d\n",
  7423. vsi->req_queue_pairs, pf->rss_size_max);
  7424. return pf->alloc_rss_size;
  7425. }
  7426. /**
  7427. * i40e_get_npar_bw_setting - Retrieve BW settings for this PF partition
  7428. * @pf: board private structure
  7429. **/
  7430. i40e_status i40e_get_npar_bw_setting(struct i40e_pf *pf)
  7431. {
  7432. i40e_status status;
  7433. bool min_valid, max_valid;
  7434. u32 max_bw, min_bw;
  7435. status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
  7436. &min_valid, &max_valid);
  7437. if (!status) {
  7438. if (min_valid)
  7439. pf->npar_min_bw = min_bw;
  7440. if (max_valid)
  7441. pf->npar_max_bw = max_bw;
  7442. }
  7443. return status;
  7444. }
  7445. /**
  7446. * i40e_set_npar_bw_setting - Set BW settings for this PF partition
  7447. * @pf: board private structure
  7448. **/
  7449. i40e_status i40e_set_npar_bw_setting(struct i40e_pf *pf)
  7450. {
  7451. struct i40e_aqc_configure_partition_bw_data bw_data;
  7452. i40e_status status;
  7453. /* Set the valid bit for this PF */
  7454. bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
  7455. bw_data.max_bw[pf->hw.pf_id] = pf->npar_max_bw & I40E_ALT_BW_VALUE_MASK;
  7456. bw_data.min_bw[pf->hw.pf_id] = pf->npar_min_bw & I40E_ALT_BW_VALUE_MASK;
  7457. /* Set the new bandwidths */
  7458. status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
  7459. return status;
  7460. }
  7461. /**
  7462. * i40e_commit_npar_bw_setting - Commit BW settings for this PF partition
  7463. * @pf: board private structure
  7464. **/
  7465. i40e_status i40e_commit_npar_bw_setting(struct i40e_pf *pf)
  7466. {
  7467. /* Commit temporary BW setting to permanent NVM image */
  7468. enum i40e_admin_queue_err last_aq_status;
  7469. i40e_status ret;
  7470. u16 nvm_word;
  7471. if (pf->hw.partition_id != 1) {
  7472. dev_info(&pf->pdev->dev,
  7473. "Commit BW only works on partition 1! This is partition %d",
  7474. pf->hw.partition_id);
  7475. ret = I40E_NOT_SUPPORTED;
  7476. goto bw_commit_out;
  7477. }
  7478. /* Acquire NVM for read access */
  7479. ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
  7480. last_aq_status = pf->hw.aq.asq_last_status;
  7481. if (ret) {
  7482. dev_info(&pf->pdev->dev,
  7483. "Cannot acquire NVM for read access, err %s aq_err %s\n",
  7484. i40e_stat_str(&pf->hw, ret),
  7485. i40e_aq_str(&pf->hw, last_aq_status));
  7486. goto bw_commit_out;
  7487. }
  7488. /* Read word 0x10 of NVM - SW compatibility word 1 */
  7489. ret = i40e_aq_read_nvm(&pf->hw,
  7490. I40E_SR_NVM_CONTROL_WORD,
  7491. 0x10, sizeof(nvm_word), &nvm_word,
  7492. false, NULL);
  7493. /* Save off last admin queue command status before releasing
  7494. * the NVM
  7495. */
  7496. last_aq_status = pf->hw.aq.asq_last_status;
  7497. i40e_release_nvm(&pf->hw);
  7498. if (ret) {
  7499. dev_info(&pf->pdev->dev, "NVM read error, err %s aq_err %s\n",
  7500. i40e_stat_str(&pf->hw, ret),
  7501. i40e_aq_str(&pf->hw, last_aq_status));
  7502. goto bw_commit_out;
  7503. }
  7504. /* Wait a bit for NVM release to complete */
  7505. msleep(50);
  7506. /* Acquire NVM for write access */
  7507. ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
  7508. last_aq_status = pf->hw.aq.asq_last_status;
  7509. if (ret) {
  7510. dev_info(&pf->pdev->dev,
  7511. "Cannot acquire NVM for write access, err %s aq_err %s\n",
  7512. i40e_stat_str(&pf->hw, ret),
  7513. i40e_aq_str(&pf->hw, last_aq_status));
  7514. goto bw_commit_out;
  7515. }
  7516. /* Write it back out unchanged to initiate update NVM,
  7517. * which will force a write of the shadow (alt) RAM to
  7518. * the NVM - thus storing the bandwidth values permanently.
  7519. */
  7520. ret = i40e_aq_update_nvm(&pf->hw,
  7521. I40E_SR_NVM_CONTROL_WORD,
  7522. 0x10, sizeof(nvm_word),
  7523. &nvm_word, true, NULL);
  7524. /* Save off last admin queue command status before releasing
  7525. * the NVM
  7526. */
  7527. last_aq_status = pf->hw.aq.asq_last_status;
  7528. i40e_release_nvm(&pf->hw);
  7529. if (ret)
  7530. dev_info(&pf->pdev->dev,
  7531. "BW settings NOT SAVED, err %s aq_err %s\n",
  7532. i40e_stat_str(&pf->hw, ret),
  7533. i40e_aq_str(&pf->hw, last_aq_status));
  7534. bw_commit_out:
  7535. return ret;
  7536. }
  7537. /**
  7538. * i40e_sw_init - Initialize general software structures (struct i40e_pf)
  7539. * @pf: board private structure to initialize
  7540. *
  7541. * i40e_sw_init initializes the Adapter private data structure.
  7542. * Fields are initialized based on PCI device information and
  7543. * OS network device settings (MTU size).
  7544. **/
  7545. static int i40e_sw_init(struct i40e_pf *pf)
  7546. {
  7547. int err = 0;
  7548. int size;
  7549. /* Set default capability flags */
  7550. pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
  7551. I40E_FLAG_MSI_ENABLED |
  7552. I40E_FLAG_MSIX_ENABLED;
  7553. /* Set default ITR */
  7554. pf->rx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_RX_DEF;
  7555. pf->tx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_TX_DEF;
  7556. /* Depending on PF configurations, it is possible that the RSS
  7557. * maximum might end up larger than the available queues
  7558. */
  7559. pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
  7560. pf->alloc_rss_size = 1;
  7561. pf->rss_table_size = pf->hw.func_caps.rss_table_size;
  7562. pf->rss_size_max = min_t(int, pf->rss_size_max,
  7563. pf->hw.func_caps.num_tx_qp);
  7564. if (pf->hw.func_caps.rss) {
  7565. pf->flags |= I40E_FLAG_RSS_ENABLED;
  7566. pf->alloc_rss_size = min_t(int, pf->rss_size_max,
  7567. num_online_cpus());
  7568. }
  7569. /* MFP mode enabled */
  7570. if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
  7571. pf->flags |= I40E_FLAG_MFP_ENABLED;
  7572. dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
  7573. if (i40e_get_npar_bw_setting(pf))
  7574. dev_warn(&pf->pdev->dev,
  7575. "Could not get NPAR bw settings\n");
  7576. else
  7577. dev_info(&pf->pdev->dev,
  7578. "Min BW = %8.8x, Max BW = %8.8x\n",
  7579. pf->npar_min_bw, pf->npar_max_bw);
  7580. }
  7581. /* FW/NVM is not yet fixed in this regard */
  7582. if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
  7583. (pf->hw.func_caps.fd_filters_best_effort > 0)) {
  7584. pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
  7585. pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
  7586. if (pf->flags & I40E_FLAG_MFP_ENABLED &&
  7587. pf->hw.num_partitions > 1)
  7588. dev_info(&pf->pdev->dev,
  7589. "Flow Director Sideband mode Disabled in MFP mode\n");
  7590. else
  7591. pf->flags |= I40E_FLAG_FD_SB_ENABLED;
  7592. pf->fdir_pf_filter_count =
  7593. pf->hw.func_caps.fd_filters_guaranteed;
  7594. pf->hw.fdir_shared_filter_count =
  7595. pf->hw.func_caps.fd_filters_best_effort;
  7596. }
  7597. if ((pf->hw.mac.type == I40E_MAC_XL710) &&
  7598. (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
  7599. (pf->hw.aq.fw_maj_ver < 4))) {
  7600. pf->flags |= I40E_FLAG_RESTART_AUTONEG;
  7601. /* No DCB support for FW < v4.33 */
  7602. pf->flags |= I40E_FLAG_NO_DCB_SUPPORT;
  7603. }
  7604. /* Disable FW LLDP if FW < v4.3 */
  7605. if ((pf->hw.mac.type == I40E_MAC_XL710) &&
  7606. (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
  7607. (pf->hw.aq.fw_maj_ver < 4)))
  7608. pf->flags |= I40E_FLAG_STOP_FW_LLDP;
  7609. /* Use the FW Set LLDP MIB API if FW > v4.40 */
  7610. if ((pf->hw.mac.type == I40E_MAC_XL710) &&
  7611. (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver >= 40)) ||
  7612. (pf->hw.aq.fw_maj_ver >= 5)))
  7613. pf->flags |= I40E_FLAG_USE_SET_LLDP_MIB;
  7614. if (pf->hw.func_caps.vmdq) {
  7615. pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
  7616. pf->flags |= I40E_FLAG_VMDQ_ENABLED;
  7617. pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
  7618. }
  7619. if (pf->hw.func_caps.iwarp) {
  7620. pf->flags |= I40E_FLAG_IWARP_ENABLED;
  7621. /* IWARP needs one extra vector for CQP just like MISC.*/
  7622. pf->num_iwarp_msix = (int)num_online_cpus() + 1;
  7623. }
  7624. #ifdef CONFIG_PCI_IOV
  7625. if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
  7626. pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
  7627. pf->flags |= I40E_FLAG_SRIOV_ENABLED;
  7628. pf->num_req_vfs = min_t(int,
  7629. pf->hw.func_caps.num_vfs,
  7630. I40E_MAX_VF_COUNT);
  7631. }
  7632. #endif /* CONFIG_PCI_IOV */
  7633. if (pf->hw.mac.type == I40E_MAC_X722) {
  7634. pf->flags |= I40E_FLAG_RSS_AQ_CAPABLE
  7635. | I40E_FLAG_128_QP_RSS_CAPABLE
  7636. | I40E_FLAG_HW_ATR_EVICT_CAPABLE
  7637. | I40E_FLAG_OUTER_UDP_CSUM_CAPABLE
  7638. | I40E_FLAG_WB_ON_ITR_CAPABLE
  7639. | I40E_FLAG_MULTIPLE_TCP_UDP_RSS_PCTYPE
  7640. | I40E_FLAG_NO_PCI_LINK_CHECK
  7641. | I40E_FLAG_USE_SET_LLDP_MIB
  7642. | I40E_FLAG_GENEVE_OFFLOAD_CAPABLE
  7643. | I40E_FLAG_PTP_L4_CAPABLE
  7644. | I40E_FLAG_WOL_MC_MAGIC_PKT_WAKE;
  7645. } else if ((pf->hw.aq.api_maj_ver > 1) ||
  7646. ((pf->hw.aq.api_maj_ver == 1) &&
  7647. (pf->hw.aq.api_min_ver > 4))) {
  7648. /* Supported in FW API version higher than 1.4 */
  7649. pf->flags |= I40E_FLAG_GENEVE_OFFLOAD_CAPABLE;
  7650. pf->hw_disabled_flags = I40E_FLAG_HW_ATR_EVICT_CAPABLE;
  7651. } else {
  7652. pf->hw_disabled_flags = I40E_FLAG_HW_ATR_EVICT_CAPABLE;
  7653. }
  7654. pf->eeprom_version = 0xDEAD;
  7655. pf->lan_veb = I40E_NO_VEB;
  7656. pf->lan_vsi = I40E_NO_VSI;
  7657. /* By default FW has this off for performance reasons */
  7658. pf->flags &= ~I40E_FLAG_VEB_STATS_ENABLED;
  7659. /* set up queue assignment tracking */
  7660. size = sizeof(struct i40e_lump_tracking)
  7661. + (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
  7662. pf->qp_pile = kzalloc(size, GFP_KERNEL);
  7663. if (!pf->qp_pile) {
  7664. err = -ENOMEM;
  7665. goto sw_init_done;
  7666. }
  7667. pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
  7668. pf->qp_pile->search_hint = 0;
  7669. pf->tx_timeout_recovery_level = 1;
  7670. mutex_init(&pf->switch_mutex);
  7671. /* If NPAR is enabled nudge the Tx scheduler */
  7672. if (pf->hw.func_caps.npar_enable && (!i40e_get_npar_bw_setting(pf)))
  7673. i40e_set_npar_bw_setting(pf);
  7674. sw_init_done:
  7675. return err;
  7676. }
  7677. /**
  7678. * i40e_set_ntuple - set the ntuple feature flag and take action
  7679. * @pf: board private structure to initialize
  7680. * @features: the feature set that the stack is suggesting
  7681. *
  7682. * returns a bool to indicate if reset needs to happen
  7683. **/
  7684. bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
  7685. {
  7686. bool need_reset = false;
  7687. /* Check if Flow Director n-tuple support was enabled or disabled. If
  7688. * the state changed, we need to reset.
  7689. */
  7690. if (features & NETIF_F_NTUPLE) {
  7691. /* Enable filters and mark for reset */
  7692. if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
  7693. need_reset = true;
  7694. /* enable FD_SB only if there is MSI-X vector */
  7695. if (pf->num_fdsb_msix > 0)
  7696. pf->flags |= I40E_FLAG_FD_SB_ENABLED;
  7697. } else {
  7698. /* turn off filters, mark for reset and clear SW filter list */
  7699. if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
  7700. need_reset = true;
  7701. i40e_fdir_filter_exit(pf);
  7702. }
  7703. pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
  7704. pf->hw_disabled_flags &= ~I40E_FLAG_FD_SB_ENABLED;
  7705. /* reset fd counters */
  7706. pf->fd_add_err = 0;
  7707. pf->fd_atr_cnt = 0;
  7708. /* if ATR was auto disabled it can be re-enabled. */
  7709. if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
  7710. (pf->hw_disabled_flags & I40E_FLAG_FD_ATR_ENABLED)) {
  7711. pf->hw_disabled_flags &= ~I40E_FLAG_FD_ATR_ENABLED;
  7712. if (I40E_DEBUG_FD & pf->hw.debug_mask)
  7713. dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
  7714. }
  7715. }
  7716. return need_reset;
  7717. }
  7718. /**
  7719. * i40e_clear_rss_lut - clear the rx hash lookup table
  7720. * @vsi: the VSI being configured
  7721. **/
  7722. static void i40e_clear_rss_lut(struct i40e_vsi *vsi)
  7723. {
  7724. struct i40e_pf *pf = vsi->back;
  7725. struct i40e_hw *hw = &pf->hw;
  7726. u16 vf_id = vsi->vf_id;
  7727. u8 i;
  7728. if (vsi->type == I40E_VSI_MAIN) {
  7729. for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
  7730. wr32(hw, I40E_PFQF_HLUT(i), 0);
  7731. } else if (vsi->type == I40E_VSI_SRIOV) {
  7732. for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
  7733. i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0);
  7734. } else {
  7735. dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
  7736. }
  7737. }
  7738. /**
  7739. * i40e_set_features - set the netdev feature flags
  7740. * @netdev: ptr to the netdev being adjusted
  7741. * @features: the feature set that the stack is suggesting
  7742. * Note: expects to be called while under rtnl_lock()
  7743. **/
  7744. static int i40e_set_features(struct net_device *netdev,
  7745. netdev_features_t features)
  7746. {
  7747. struct i40e_netdev_priv *np = netdev_priv(netdev);
  7748. struct i40e_vsi *vsi = np->vsi;
  7749. struct i40e_pf *pf = vsi->back;
  7750. bool need_reset;
  7751. if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
  7752. i40e_pf_config_rss(pf);
  7753. else if (!(features & NETIF_F_RXHASH) &&
  7754. netdev->features & NETIF_F_RXHASH)
  7755. i40e_clear_rss_lut(vsi);
  7756. if (features & NETIF_F_HW_VLAN_CTAG_RX)
  7757. i40e_vlan_stripping_enable(vsi);
  7758. else
  7759. i40e_vlan_stripping_disable(vsi);
  7760. need_reset = i40e_set_ntuple(pf, features);
  7761. if (need_reset)
  7762. i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED), true);
  7763. return 0;
  7764. }
  7765. /**
  7766. * i40e_get_udp_port_idx - Lookup a possibly offloaded for Rx UDP port
  7767. * @pf: board private structure
  7768. * @port: The UDP port to look up
  7769. *
  7770. * Returns the index number or I40E_MAX_PF_UDP_OFFLOAD_PORTS if port not found
  7771. **/
  7772. static u8 i40e_get_udp_port_idx(struct i40e_pf *pf, u16 port)
  7773. {
  7774. u8 i;
  7775. for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
  7776. if (pf->udp_ports[i].index == port)
  7777. return i;
  7778. }
  7779. return i;
  7780. }
  7781. /**
  7782. * i40e_udp_tunnel_add - Get notifications about UDP tunnel ports that come up
  7783. * @netdev: This physical port's netdev
  7784. * @ti: Tunnel endpoint information
  7785. **/
  7786. static void i40e_udp_tunnel_add(struct net_device *netdev,
  7787. struct udp_tunnel_info *ti)
  7788. {
  7789. struct i40e_netdev_priv *np = netdev_priv(netdev);
  7790. struct i40e_vsi *vsi = np->vsi;
  7791. struct i40e_pf *pf = vsi->back;
  7792. u16 port = ntohs(ti->port);
  7793. u8 next_idx;
  7794. u8 idx;
  7795. idx = i40e_get_udp_port_idx(pf, port);
  7796. /* Check if port already exists */
  7797. if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
  7798. netdev_info(netdev, "port %d already offloaded\n", port);
  7799. return;
  7800. }
  7801. /* Now check if there is space to add the new port */
  7802. next_idx = i40e_get_udp_port_idx(pf, 0);
  7803. if (next_idx == I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
  7804. netdev_info(netdev, "maximum number of offloaded UDP ports reached, not adding port %d\n",
  7805. port);
  7806. return;
  7807. }
  7808. switch (ti->type) {
  7809. case UDP_TUNNEL_TYPE_VXLAN:
  7810. pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_VXLAN;
  7811. break;
  7812. case UDP_TUNNEL_TYPE_GENEVE:
  7813. if (!(pf->flags & I40E_FLAG_GENEVE_OFFLOAD_CAPABLE))
  7814. return;
  7815. pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_NGE;
  7816. break;
  7817. default:
  7818. return;
  7819. }
  7820. /* New port: add it and mark its index in the bitmap */
  7821. pf->udp_ports[next_idx].index = port;
  7822. pf->pending_udp_bitmap |= BIT_ULL(next_idx);
  7823. pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
  7824. }
  7825. /**
  7826. * i40e_udp_tunnel_del - Get notifications about UDP tunnel ports that go away
  7827. * @netdev: This physical port's netdev
  7828. * @ti: Tunnel endpoint information
  7829. **/
  7830. static void i40e_udp_tunnel_del(struct net_device *netdev,
  7831. struct udp_tunnel_info *ti)
  7832. {
  7833. struct i40e_netdev_priv *np = netdev_priv(netdev);
  7834. struct i40e_vsi *vsi = np->vsi;
  7835. struct i40e_pf *pf = vsi->back;
  7836. u16 port = ntohs(ti->port);
  7837. u8 idx;
  7838. idx = i40e_get_udp_port_idx(pf, port);
  7839. /* Check if port already exists */
  7840. if (idx >= I40E_MAX_PF_UDP_OFFLOAD_PORTS)
  7841. goto not_found;
  7842. switch (ti->type) {
  7843. case UDP_TUNNEL_TYPE_VXLAN:
  7844. if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_VXLAN)
  7845. goto not_found;
  7846. break;
  7847. case UDP_TUNNEL_TYPE_GENEVE:
  7848. if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_NGE)
  7849. goto not_found;
  7850. break;
  7851. default:
  7852. goto not_found;
  7853. }
  7854. /* if port exists, set it to 0 (mark for deletion)
  7855. * and make it pending
  7856. */
  7857. pf->udp_ports[idx].index = 0;
  7858. pf->pending_udp_bitmap |= BIT_ULL(idx);
  7859. pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
  7860. return;
  7861. not_found:
  7862. netdev_warn(netdev, "UDP port %d was not found, not deleting\n",
  7863. port);
  7864. }
  7865. static int i40e_get_phys_port_id(struct net_device *netdev,
  7866. struct netdev_phys_item_id *ppid)
  7867. {
  7868. struct i40e_netdev_priv *np = netdev_priv(netdev);
  7869. struct i40e_pf *pf = np->vsi->back;
  7870. struct i40e_hw *hw = &pf->hw;
  7871. if (!(pf->flags & I40E_FLAG_PORT_ID_VALID))
  7872. return -EOPNOTSUPP;
  7873. ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
  7874. memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
  7875. return 0;
  7876. }
  7877. /**
  7878. * i40e_ndo_fdb_add - add an entry to the hardware database
  7879. * @ndm: the input from the stack
  7880. * @tb: pointer to array of nladdr (unused)
  7881. * @dev: the net device pointer
  7882. * @addr: the MAC address entry being added
  7883. * @flags: instructions from stack about fdb operation
  7884. */
  7885. static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
  7886. struct net_device *dev,
  7887. const unsigned char *addr, u16 vid,
  7888. u16 flags)
  7889. {
  7890. struct i40e_netdev_priv *np = netdev_priv(dev);
  7891. struct i40e_pf *pf = np->vsi->back;
  7892. int err = 0;
  7893. if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
  7894. return -EOPNOTSUPP;
  7895. if (vid) {
  7896. pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
  7897. return -EINVAL;
  7898. }
  7899. /* Hardware does not support aging addresses so if a
  7900. * ndm_state is given only allow permanent addresses
  7901. */
  7902. if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
  7903. netdev_info(dev, "FDB only supports static addresses\n");
  7904. return -EINVAL;
  7905. }
  7906. if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
  7907. err = dev_uc_add_excl(dev, addr);
  7908. else if (is_multicast_ether_addr(addr))
  7909. err = dev_mc_add_excl(dev, addr);
  7910. else
  7911. err = -EINVAL;
  7912. /* Only return duplicate errors if NLM_F_EXCL is set */
  7913. if (err == -EEXIST && !(flags & NLM_F_EXCL))
  7914. err = 0;
  7915. return err;
  7916. }
  7917. /**
  7918. * i40e_ndo_bridge_setlink - Set the hardware bridge mode
  7919. * @dev: the netdev being configured
  7920. * @nlh: RTNL message
  7921. *
  7922. * Inserts a new hardware bridge if not already created and
  7923. * enables the bridging mode requested (VEB or VEPA). If the
  7924. * hardware bridge has already been inserted and the request
  7925. * is to change the mode then that requires a PF reset to
  7926. * allow rebuild of the components with required hardware
  7927. * bridge mode enabled.
  7928. *
  7929. * Note: expects to be called while under rtnl_lock()
  7930. **/
  7931. static int i40e_ndo_bridge_setlink(struct net_device *dev,
  7932. struct nlmsghdr *nlh,
  7933. u16 flags)
  7934. {
  7935. struct i40e_netdev_priv *np = netdev_priv(dev);
  7936. struct i40e_vsi *vsi = np->vsi;
  7937. struct i40e_pf *pf = vsi->back;
  7938. struct i40e_veb *veb = NULL;
  7939. struct nlattr *attr, *br_spec;
  7940. int i, rem;
  7941. /* Only for PF VSI for now */
  7942. if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
  7943. return -EOPNOTSUPP;
  7944. /* Find the HW bridge for PF VSI */
  7945. for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
  7946. if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
  7947. veb = pf->veb[i];
  7948. }
  7949. br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
  7950. nla_for_each_nested(attr, br_spec, rem) {
  7951. __u16 mode;
  7952. if (nla_type(attr) != IFLA_BRIDGE_MODE)
  7953. continue;
  7954. mode = nla_get_u16(attr);
  7955. if ((mode != BRIDGE_MODE_VEPA) &&
  7956. (mode != BRIDGE_MODE_VEB))
  7957. return -EINVAL;
  7958. /* Insert a new HW bridge */
  7959. if (!veb) {
  7960. veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
  7961. vsi->tc_config.enabled_tc);
  7962. if (veb) {
  7963. veb->bridge_mode = mode;
  7964. i40e_config_bridge_mode(veb);
  7965. } else {
  7966. /* No Bridge HW offload available */
  7967. return -ENOENT;
  7968. }
  7969. break;
  7970. } else if (mode != veb->bridge_mode) {
  7971. /* Existing HW bridge but different mode needs reset */
  7972. veb->bridge_mode = mode;
  7973. /* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
  7974. if (mode == BRIDGE_MODE_VEB)
  7975. pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
  7976. else
  7977. pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
  7978. i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED),
  7979. true);
  7980. break;
  7981. }
  7982. }
  7983. return 0;
  7984. }
  7985. /**
  7986. * i40e_ndo_bridge_getlink - Get the hardware bridge mode
  7987. * @skb: skb buff
  7988. * @pid: process id
  7989. * @seq: RTNL message seq #
  7990. * @dev: the netdev being configured
  7991. * @filter_mask: unused
  7992. * @nlflags: netlink flags passed in
  7993. *
  7994. * Return the mode in which the hardware bridge is operating in
  7995. * i.e VEB or VEPA.
  7996. **/
  7997. static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
  7998. struct net_device *dev,
  7999. u32 __always_unused filter_mask,
  8000. int nlflags)
  8001. {
  8002. struct i40e_netdev_priv *np = netdev_priv(dev);
  8003. struct i40e_vsi *vsi = np->vsi;
  8004. struct i40e_pf *pf = vsi->back;
  8005. struct i40e_veb *veb = NULL;
  8006. int i;
  8007. /* Only for PF VSI for now */
  8008. if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
  8009. return -EOPNOTSUPP;
  8010. /* Find the HW bridge for the PF VSI */
  8011. for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
  8012. if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
  8013. veb = pf->veb[i];
  8014. }
  8015. if (!veb)
  8016. return 0;
  8017. return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
  8018. 0, 0, nlflags, filter_mask, NULL);
  8019. }
  8020. /**
  8021. * i40e_features_check - Validate encapsulated packet conforms to limits
  8022. * @skb: skb buff
  8023. * @dev: This physical port's netdev
  8024. * @features: Offload features that the stack believes apply
  8025. **/
  8026. static netdev_features_t i40e_features_check(struct sk_buff *skb,
  8027. struct net_device *dev,
  8028. netdev_features_t features)
  8029. {
  8030. size_t len;
  8031. /* No point in doing any of this if neither checksum nor GSO are
  8032. * being requested for this frame. We can rule out both by just
  8033. * checking for CHECKSUM_PARTIAL
  8034. */
  8035. if (skb->ip_summed != CHECKSUM_PARTIAL)
  8036. return features;
  8037. /* We cannot support GSO if the MSS is going to be less than
  8038. * 64 bytes. If it is then we need to drop support for GSO.
  8039. */
  8040. if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
  8041. features &= ~NETIF_F_GSO_MASK;
  8042. /* MACLEN can support at most 63 words */
  8043. len = skb_network_header(skb) - skb->data;
  8044. if (len & ~(63 * 2))
  8045. goto out_err;
  8046. /* IPLEN and EIPLEN can support at most 127 dwords */
  8047. len = skb_transport_header(skb) - skb_network_header(skb);
  8048. if (len & ~(127 * 4))
  8049. goto out_err;
  8050. if (skb->encapsulation) {
  8051. /* L4TUNLEN can support 127 words */
  8052. len = skb_inner_network_header(skb) - skb_transport_header(skb);
  8053. if (len & ~(127 * 2))
  8054. goto out_err;
  8055. /* IPLEN can support at most 127 dwords */
  8056. len = skb_inner_transport_header(skb) -
  8057. skb_inner_network_header(skb);
  8058. if (len & ~(127 * 4))
  8059. goto out_err;
  8060. }
  8061. /* No need to validate L4LEN as TCP is the only protocol with a
  8062. * a flexible value and we support all possible values supported
  8063. * by TCP, which is at most 15 dwords
  8064. */
  8065. return features;
  8066. out_err:
  8067. return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
  8068. }
  8069. static const struct net_device_ops i40e_netdev_ops = {
  8070. .ndo_open = i40e_open,
  8071. .ndo_stop = i40e_close,
  8072. .ndo_start_xmit = i40e_lan_xmit_frame,
  8073. .ndo_get_stats64 = i40e_get_netdev_stats_struct,
  8074. .ndo_set_rx_mode = i40e_set_rx_mode,
  8075. .ndo_validate_addr = eth_validate_addr,
  8076. .ndo_set_mac_address = i40e_set_mac,
  8077. .ndo_change_mtu = i40e_change_mtu,
  8078. .ndo_do_ioctl = i40e_ioctl,
  8079. .ndo_tx_timeout = i40e_tx_timeout,
  8080. .ndo_vlan_rx_add_vid = i40e_vlan_rx_add_vid,
  8081. .ndo_vlan_rx_kill_vid = i40e_vlan_rx_kill_vid,
  8082. #ifdef CONFIG_NET_POLL_CONTROLLER
  8083. .ndo_poll_controller = i40e_netpoll,
  8084. #endif
  8085. .ndo_setup_tc = __i40e_setup_tc,
  8086. .ndo_set_features = i40e_set_features,
  8087. .ndo_set_vf_mac = i40e_ndo_set_vf_mac,
  8088. .ndo_set_vf_vlan = i40e_ndo_set_vf_port_vlan,
  8089. .ndo_set_vf_rate = i40e_ndo_set_vf_bw,
  8090. .ndo_get_vf_config = i40e_ndo_get_vf_config,
  8091. .ndo_set_vf_link_state = i40e_ndo_set_vf_link_state,
  8092. .ndo_set_vf_spoofchk = i40e_ndo_set_vf_spoofchk,
  8093. .ndo_set_vf_trust = i40e_ndo_set_vf_trust,
  8094. .ndo_udp_tunnel_add = i40e_udp_tunnel_add,
  8095. .ndo_udp_tunnel_del = i40e_udp_tunnel_del,
  8096. .ndo_get_phys_port_id = i40e_get_phys_port_id,
  8097. .ndo_fdb_add = i40e_ndo_fdb_add,
  8098. .ndo_features_check = i40e_features_check,
  8099. .ndo_bridge_getlink = i40e_ndo_bridge_getlink,
  8100. .ndo_bridge_setlink = i40e_ndo_bridge_setlink,
  8101. };
  8102. /**
  8103. * i40e_config_netdev - Setup the netdev flags
  8104. * @vsi: the VSI being configured
  8105. *
  8106. * Returns 0 on success, negative value on failure
  8107. **/
  8108. static int i40e_config_netdev(struct i40e_vsi *vsi)
  8109. {
  8110. struct i40e_pf *pf = vsi->back;
  8111. struct i40e_hw *hw = &pf->hw;
  8112. struct i40e_netdev_priv *np;
  8113. struct net_device *netdev;
  8114. u8 broadcast[ETH_ALEN];
  8115. u8 mac_addr[ETH_ALEN];
  8116. int etherdev_size;
  8117. netdev_features_t hw_enc_features;
  8118. netdev_features_t hw_features;
  8119. etherdev_size = sizeof(struct i40e_netdev_priv);
  8120. netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
  8121. if (!netdev)
  8122. return -ENOMEM;
  8123. vsi->netdev = netdev;
  8124. np = netdev_priv(netdev);
  8125. np->vsi = vsi;
  8126. hw_enc_features = NETIF_F_SG |
  8127. NETIF_F_IP_CSUM |
  8128. NETIF_F_IPV6_CSUM |
  8129. NETIF_F_HIGHDMA |
  8130. NETIF_F_SOFT_FEATURES |
  8131. NETIF_F_TSO |
  8132. NETIF_F_TSO_ECN |
  8133. NETIF_F_TSO6 |
  8134. NETIF_F_GSO_GRE |
  8135. NETIF_F_GSO_GRE_CSUM |
  8136. NETIF_F_GSO_PARTIAL |
  8137. NETIF_F_GSO_UDP_TUNNEL |
  8138. NETIF_F_GSO_UDP_TUNNEL_CSUM |
  8139. NETIF_F_SCTP_CRC |
  8140. NETIF_F_RXHASH |
  8141. NETIF_F_RXCSUM |
  8142. 0;
  8143. if (!(pf->flags & I40E_FLAG_OUTER_UDP_CSUM_CAPABLE))
  8144. netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
  8145. netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
  8146. netdev->hw_enc_features |= hw_enc_features;
  8147. /* record features VLANs can make use of */
  8148. netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
  8149. if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
  8150. netdev->hw_features |= NETIF_F_NTUPLE;
  8151. hw_features = hw_enc_features |
  8152. NETIF_F_HW_VLAN_CTAG_TX |
  8153. NETIF_F_HW_VLAN_CTAG_RX;
  8154. netdev->hw_features |= hw_features;
  8155. netdev->features |= hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
  8156. netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
  8157. if (vsi->type == I40E_VSI_MAIN) {
  8158. SET_NETDEV_DEV(netdev, &pf->pdev->dev);
  8159. ether_addr_copy(mac_addr, hw->mac.perm_addr);
  8160. /* The following steps are necessary for two reasons. First,
  8161. * some older NVM configurations load a default MAC-VLAN
  8162. * filter that will accept any tagged packet, and we want to
  8163. * replace this with a normal filter. Additionally, it is
  8164. * possible our MAC address was provided by the platform using
  8165. * Open Firmware or similar.
  8166. *
  8167. * Thus, we need to remove the default filter and install one
  8168. * specific to the MAC address.
  8169. */
  8170. i40e_rm_default_mac_filter(vsi, mac_addr);
  8171. spin_lock_bh(&vsi->mac_filter_hash_lock);
  8172. i40e_add_mac_filter(vsi, mac_addr);
  8173. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  8174. } else {
  8175. /* relate the VSI_VMDQ name to the VSI_MAIN name */
  8176. snprintf(netdev->name, IFNAMSIZ, "%sv%%d",
  8177. pf->vsi[pf->lan_vsi]->netdev->name);
  8178. random_ether_addr(mac_addr);
  8179. spin_lock_bh(&vsi->mac_filter_hash_lock);
  8180. i40e_add_mac_filter(vsi, mac_addr);
  8181. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  8182. }
  8183. /* Add the broadcast filter so that we initially will receive
  8184. * broadcast packets. Note that when a new VLAN is first added the
  8185. * driver will convert all filters marked I40E_VLAN_ANY into VLAN
  8186. * specific filters as part of transitioning into "vlan" operation.
  8187. * When more VLANs are added, the driver will copy each existing MAC
  8188. * filter and add it for the new VLAN.
  8189. *
  8190. * Broadcast filters are handled specially by
  8191. * i40e_sync_filters_subtask, as the driver must to set the broadcast
  8192. * promiscuous bit instead of adding this directly as a MAC/VLAN
  8193. * filter. The subtask will update the correct broadcast promiscuous
  8194. * bits as VLANs become active or inactive.
  8195. */
  8196. eth_broadcast_addr(broadcast);
  8197. spin_lock_bh(&vsi->mac_filter_hash_lock);
  8198. i40e_add_mac_filter(vsi, broadcast);
  8199. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  8200. ether_addr_copy(netdev->dev_addr, mac_addr);
  8201. ether_addr_copy(netdev->perm_addr, mac_addr);
  8202. netdev->priv_flags |= IFF_UNICAST_FLT;
  8203. netdev->priv_flags |= IFF_SUPP_NOFCS;
  8204. /* Setup netdev TC information */
  8205. i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
  8206. netdev->netdev_ops = &i40e_netdev_ops;
  8207. netdev->watchdog_timeo = 5 * HZ;
  8208. i40e_set_ethtool_ops(netdev);
  8209. /* MTU range: 68 - 9706 */
  8210. netdev->min_mtu = ETH_MIN_MTU;
  8211. netdev->max_mtu = I40E_MAX_RXBUFFER -
  8212. (ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
  8213. return 0;
  8214. }
  8215. /**
  8216. * i40e_vsi_delete - Delete a VSI from the switch
  8217. * @vsi: the VSI being removed
  8218. *
  8219. * Returns 0 on success, negative value on failure
  8220. **/
  8221. static void i40e_vsi_delete(struct i40e_vsi *vsi)
  8222. {
  8223. /* remove default VSI is not allowed */
  8224. if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
  8225. return;
  8226. i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
  8227. }
  8228. /**
  8229. * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
  8230. * @vsi: the VSI being queried
  8231. *
  8232. * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
  8233. **/
  8234. int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
  8235. {
  8236. struct i40e_veb *veb;
  8237. struct i40e_pf *pf = vsi->back;
  8238. /* Uplink is not a bridge so default to VEB */
  8239. if (vsi->veb_idx == I40E_NO_VEB)
  8240. return 1;
  8241. veb = pf->veb[vsi->veb_idx];
  8242. if (!veb) {
  8243. dev_info(&pf->pdev->dev,
  8244. "There is no veb associated with the bridge\n");
  8245. return -ENOENT;
  8246. }
  8247. /* Uplink is a bridge in VEPA mode */
  8248. if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
  8249. return 0;
  8250. } else {
  8251. /* Uplink is a bridge in VEB mode */
  8252. return 1;
  8253. }
  8254. /* VEPA is now default bridge, so return 0 */
  8255. return 0;
  8256. }
  8257. /**
  8258. * i40e_add_vsi - Add a VSI to the switch
  8259. * @vsi: the VSI being configured
  8260. *
  8261. * This initializes a VSI context depending on the VSI type to be added and
  8262. * passes it down to the add_vsi aq command.
  8263. **/
  8264. static int i40e_add_vsi(struct i40e_vsi *vsi)
  8265. {
  8266. int ret = -ENODEV;
  8267. struct i40e_pf *pf = vsi->back;
  8268. struct i40e_hw *hw = &pf->hw;
  8269. struct i40e_vsi_context ctxt;
  8270. struct i40e_mac_filter *f;
  8271. struct hlist_node *h;
  8272. int bkt;
  8273. u8 enabled_tc = 0x1; /* TC0 enabled */
  8274. int f_count = 0;
  8275. memset(&ctxt, 0, sizeof(ctxt));
  8276. switch (vsi->type) {
  8277. case I40E_VSI_MAIN:
  8278. /* The PF's main VSI is already setup as part of the
  8279. * device initialization, so we'll not bother with
  8280. * the add_vsi call, but we will retrieve the current
  8281. * VSI context.
  8282. */
  8283. ctxt.seid = pf->main_vsi_seid;
  8284. ctxt.pf_num = pf->hw.pf_id;
  8285. ctxt.vf_num = 0;
  8286. ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
  8287. ctxt.flags = I40E_AQ_VSI_TYPE_PF;
  8288. if (ret) {
  8289. dev_info(&pf->pdev->dev,
  8290. "couldn't get PF vsi config, err %s aq_err %s\n",
  8291. i40e_stat_str(&pf->hw, ret),
  8292. i40e_aq_str(&pf->hw,
  8293. pf->hw.aq.asq_last_status));
  8294. return -ENOENT;
  8295. }
  8296. vsi->info = ctxt.info;
  8297. vsi->info.valid_sections = 0;
  8298. vsi->seid = ctxt.seid;
  8299. vsi->id = ctxt.vsi_number;
  8300. enabled_tc = i40e_pf_get_tc_map(pf);
  8301. /* MFP mode setup queue map and update VSI */
  8302. if ((pf->flags & I40E_FLAG_MFP_ENABLED) &&
  8303. !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
  8304. memset(&ctxt, 0, sizeof(ctxt));
  8305. ctxt.seid = pf->main_vsi_seid;
  8306. ctxt.pf_num = pf->hw.pf_id;
  8307. ctxt.vf_num = 0;
  8308. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
  8309. ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
  8310. if (ret) {
  8311. dev_info(&pf->pdev->dev,
  8312. "update vsi failed, err %s aq_err %s\n",
  8313. i40e_stat_str(&pf->hw, ret),
  8314. i40e_aq_str(&pf->hw,
  8315. pf->hw.aq.asq_last_status));
  8316. ret = -ENOENT;
  8317. goto err;
  8318. }
  8319. /* update the local VSI info queue map */
  8320. i40e_vsi_update_queue_map(vsi, &ctxt);
  8321. vsi->info.valid_sections = 0;
  8322. } else {
  8323. /* Default/Main VSI is only enabled for TC0
  8324. * reconfigure it to enable all TCs that are
  8325. * available on the port in SFP mode.
  8326. * For MFP case the iSCSI PF would use this
  8327. * flow to enable LAN+iSCSI TC.
  8328. */
  8329. ret = i40e_vsi_config_tc(vsi, enabled_tc);
  8330. if (ret) {
  8331. dev_info(&pf->pdev->dev,
  8332. "failed to configure TCs for main VSI tc_map 0x%08x, err %s aq_err %s\n",
  8333. enabled_tc,
  8334. i40e_stat_str(&pf->hw, ret),
  8335. i40e_aq_str(&pf->hw,
  8336. pf->hw.aq.asq_last_status));
  8337. ret = -ENOENT;
  8338. }
  8339. }
  8340. break;
  8341. case I40E_VSI_FDIR:
  8342. ctxt.pf_num = hw->pf_id;
  8343. ctxt.vf_num = 0;
  8344. ctxt.uplink_seid = vsi->uplink_seid;
  8345. ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
  8346. ctxt.flags = I40E_AQ_VSI_TYPE_PF;
  8347. if ((pf->flags & I40E_FLAG_VEB_MODE_ENABLED) &&
  8348. (i40e_is_vsi_uplink_mode_veb(vsi))) {
  8349. ctxt.info.valid_sections |=
  8350. cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  8351. ctxt.info.switch_id =
  8352. cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  8353. }
  8354. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
  8355. break;
  8356. case I40E_VSI_VMDQ2:
  8357. ctxt.pf_num = hw->pf_id;
  8358. ctxt.vf_num = 0;
  8359. ctxt.uplink_seid = vsi->uplink_seid;
  8360. ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
  8361. ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
  8362. /* This VSI is connected to VEB so the switch_id
  8363. * should be set to zero by default.
  8364. */
  8365. if (i40e_is_vsi_uplink_mode_veb(vsi)) {
  8366. ctxt.info.valid_sections |=
  8367. cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  8368. ctxt.info.switch_id =
  8369. cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  8370. }
  8371. /* Setup the VSI tx/rx queue map for TC0 only for now */
  8372. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
  8373. break;
  8374. case I40E_VSI_SRIOV:
  8375. ctxt.pf_num = hw->pf_id;
  8376. ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
  8377. ctxt.uplink_seid = vsi->uplink_seid;
  8378. ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
  8379. ctxt.flags = I40E_AQ_VSI_TYPE_VF;
  8380. /* This VSI is connected to VEB so the switch_id
  8381. * should be set to zero by default.
  8382. */
  8383. if (i40e_is_vsi_uplink_mode_veb(vsi)) {
  8384. ctxt.info.valid_sections |=
  8385. cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  8386. ctxt.info.switch_id =
  8387. cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  8388. }
  8389. if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
  8390. ctxt.info.valid_sections |=
  8391. cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
  8392. ctxt.info.queueing_opt_flags |=
  8393. (I40E_AQ_VSI_QUE_OPT_TCP_ENA |
  8394. I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI);
  8395. }
  8396. ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
  8397. ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
  8398. if (pf->vf[vsi->vf_id].spoofchk) {
  8399. ctxt.info.valid_sections |=
  8400. cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
  8401. ctxt.info.sec_flags |=
  8402. (I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
  8403. I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
  8404. }
  8405. /* Setup the VSI tx/rx queue map for TC0 only for now */
  8406. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
  8407. break;
  8408. case I40E_VSI_IWARP:
  8409. /* send down message to iWARP */
  8410. break;
  8411. default:
  8412. return -ENODEV;
  8413. }
  8414. if (vsi->type != I40E_VSI_MAIN) {
  8415. ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
  8416. if (ret) {
  8417. dev_info(&vsi->back->pdev->dev,
  8418. "add vsi failed, err %s aq_err %s\n",
  8419. i40e_stat_str(&pf->hw, ret),
  8420. i40e_aq_str(&pf->hw,
  8421. pf->hw.aq.asq_last_status));
  8422. ret = -ENOENT;
  8423. goto err;
  8424. }
  8425. vsi->info = ctxt.info;
  8426. vsi->info.valid_sections = 0;
  8427. vsi->seid = ctxt.seid;
  8428. vsi->id = ctxt.vsi_number;
  8429. }
  8430. vsi->active_filters = 0;
  8431. clear_bit(__I40E_FILTER_OVERFLOW_PROMISC, &vsi->state);
  8432. spin_lock_bh(&vsi->mac_filter_hash_lock);
  8433. /* If macvlan filters already exist, force them to get loaded */
  8434. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  8435. f->state = I40E_FILTER_NEW;
  8436. f_count++;
  8437. }
  8438. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  8439. if (f_count) {
  8440. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  8441. pf->flags |= I40E_FLAG_FILTER_SYNC;
  8442. }
  8443. /* Update VSI BW information */
  8444. ret = i40e_vsi_get_bw_info(vsi);
  8445. if (ret) {
  8446. dev_info(&pf->pdev->dev,
  8447. "couldn't get vsi bw info, err %s aq_err %s\n",
  8448. i40e_stat_str(&pf->hw, ret),
  8449. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  8450. /* VSI is already added so not tearing that up */
  8451. ret = 0;
  8452. }
  8453. err:
  8454. return ret;
  8455. }
  8456. /**
  8457. * i40e_vsi_release - Delete a VSI and free its resources
  8458. * @vsi: the VSI being removed
  8459. *
  8460. * Returns 0 on success or < 0 on error
  8461. **/
  8462. int i40e_vsi_release(struct i40e_vsi *vsi)
  8463. {
  8464. struct i40e_mac_filter *f;
  8465. struct hlist_node *h;
  8466. struct i40e_veb *veb = NULL;
  8467. struct i40e_pf *pf;
  8468. u16 uplink_seid;
  8469. int i, n, bkt;
  8470. pf = vsi->back;
  8471. /* release of a VEB-owner or last VSI is not allowed */
  8472. if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
  8473. dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
  8474. vsi->seid, vsi->uplink_seid);
  8475. return -ENODEV;
  8476. }
  8477. if (vsi == pf->vsi[pf->lan_vsi] &&
  8478. !test_bit(__I40E_DOWN, &pf->state)) {
  8479. dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
  8480. return -ENODEV;
  8481. }
  8482. uplink_seid = vsi->uplink_seid;
  8483. if (vsi->type != I40E_VSI_SRIOV) {
  8484. if (vsi->netdev_registered) {
  8485. vsi->netdev_registered = false;
  8486. if (vsi->netdev) {
  8487. /* results in a call to i40e_close() */
  8488. unregister_netdev(vsi->netdev);
  8489. }
  8490. } else {
  8491. i40e_vsi_close(vsi);
  8492. }
  8493. i40e_vsi_disable_irq(vsi);
  8494. }
  8495. spin_lock_bh(&vsi->mac_filter_hash_lock);
  8496. /* clear the sync flag on all filters */
  8497. if (vsi->netdev) {
  8498. __dev_uc_unsync(vsi->netdev, NULL);
  8499. __dev_mc_unsync(vsi->netdev, NULL);
  8500. }
  8501. /* make sure any remaining filters are marked for deletion */
  8502. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
  8503. __i40e_del_filter(vsi, f);
  8504. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  8505. i40e_sync_vsi_filters(vsi);
  8506. i40e_vsi_delete(vsi);
  8507. i40e_vsi_free_q_vectors(vsi);
  8508. if (vsi->netdev) {
  8509. free_netdev(vsi->netdev);
  8510. vsi->netdev = NULL;
  8511. }
  8512. i40e_vsi_clear_rings(vsi);
  8513. i40e_vsi_clear(vsi);
  8514. /* If this was the last thing on the VEB, except for the
  8515. * controlling VSI, remove the VEB, which puts the controlling
  8516. * VSI onto the next level down in the switch.
  8517. *
  8518. * Well, okay, there's one more exception here: don't remove
  8519. * the orphan VEBs yet. We'll wait for an explicit remove request
  8520. * from up the network stack.
  8521. */
  8522. for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
  8523. if (pf->vsi[i] &&
  8524. pf->vsi[i]->uplink_seid == uplink_seid &&
  8525. (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
  8526. n++; /* count the VSIs */
  8527. }
  8528. }
  8529. for (i = 0; i < I40E_MAX_VEB; i++) {
  8530. if (!pf->veb[i])
  8531. continue;
  8532. if (pf->veb[i]->uplink_seid == uplink_seid)
  8533. n++; /* count the VEBs */
  8534. if (pf->veb[i]->seid == uplink_seid)
  8535. veb = pf->veb[i];
  8536. }
  8537. if (n == 0 && veb && veb->uplink_seid != 0)
  8538. i40e_veb_release(veb);
  8539. return 0;
  8540. }
  8541. /**
  8542. * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
  8543. * @vsi: ptr to the VSI
  8544. *
  8545. * This should only be called after i40e_vsi_mem_alloc() which allocates the
  8546. * corresponding SW VSI structure and initializes num_queue_pairs for the
  8547. * newly allocated VSI.
  8548. *
  8549. * Returns 0 on success or negative on failure
  8550. **/
  8551. static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
  8552. {
  8553. int ret = -ENOENT;
  8554. struct i40e_pf *pf = vsi->back;
  8555. if (vsi->q_vectors[0]) {
  8556. dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
  8557. vsi->seid);
  8558. return -EEXIST;
  8559. }
  8560. if (vsi->base_vector) {
  8561. dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
  8562. vsi->seid, vsi->base_vector);
  8563. return -EEXIST;
  8564. }
  8565. ret = i40e_vsi_alloc_q_vectors(vsi);
  8566. if (ret) {
  8567. dev_info(&pf->pdev->dev,
  8568. "failed to allocate %d q_vector for VSI %d, ret=%d\n",
  8569. vsi->num_q_vectors, vsi->seid, ret);
  8570. vsi->num_q_vectors = 0;
  8571. goto vector_setup_out;
  8572. }
  8573. /* In Legacy mode, we do not have to get any other vector since we
  8574. * piggyback on the misc/ICR0 for queue interrupts.
  8575. */
  8576. if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
  8577. return ret;
  8578. if (vsi->num_q_vectors)
  8579. vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
  8580. vsi->num_q_vectors, vsi->idx);
  8581. if (vsi->base_vector < 0) {
  8582. dev_info(&pf->pdev->dev,
  8583. "failed to get tracking for %d vectors for VSI %d, err=%d\n",
  8584. vsi->num_q_vectors, vsi->seid, vsi->base_vector);
  8585. i40e_vsi_free_q_vectors(vsi);
  8586. ret = -ENOENT;
  8587. goto vector_setup_out;
  8588. }
  8589. vector_setup_out:
  8590. return ret;
  8591. }
  8592. /**
  8593. * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
  8594. * @vsi: pointer to the vsi.
  8595. *
  8596. * This re-allocates a vsi's queue resources.
  8597. *
  8598. * Returns pointer to the successfully allocated and configured VSI sw struct
  8599. * on success, otherwise returns NULL on failure.
  8600. **/
  8601. static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
  8602. {
  8603. struct i40e_pf *pf;
  8604. u8 enabled_tc;
  8605. int ret;
  8606. if (!vsi)
  8607. return NULL;
  8608. pf = vsi->back;
  8609. i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
  8610. i40e_vsi_clear_rings(vsi);
  8611. i40e_vsi_free_arrays(vsi, false);
  8612. i40e_set_num_rings_in_vsi(vsi);
  8613. ret = i40e_vsi_alloc_arrays(vsi, false);
  8614. if (ret)
  8615. goto err_vsi;
  8616. ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs, vsi->idx);
  8617. if (ret < 0) {
  8618. dev_info(&pf->pdev->dev,
  8619. "failed to get tracking for %d queues for VSI %d err %d\n",
  8620. vsi->alloc_queue_pairs, vsi->seid, ret);
  8621. goto err_vsi;
  8622. }
  8623. vsi->base_queue = ret;
  8624. /* Update the FW view of the VSI. Force a reset of TC and queue
  8625. * layout configurations.
  8626. */
  8627. enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
  8628. pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
  8629. pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
  8630. i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
  8631. if (vsi->type == I40E_VSI_MAIN)
  8632. i40e_rm_default_mac_filter(vsi, pf->hw.mac.perm_addr);
  8633. /* assign it some queues */
  8634. ret = i40e_alloc_rings(vsi);
  8635. if (ret)
  8636. goto err_rings;
  8637. /* map all of the rings to the q_vectors */
  8638. i40e_vsi_map_rings_to_vectors(vsi);
  8639. return vsi;
  8640. err_rings:
  8641. i40e_vsi_free_q_vectors(vsi);
  8642. if (vsi->netdev_registered) {
  8643. vsi->netdev_registered = false;
  8644. unregister_netdev(vsi->netdev);
  8645. free_netdev(vsi->netdev);
  8646. vsi->netdev = NULL;
  8647. }
  8648. i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
  8649. err_vsi:
  8650. i40e_vsi_clear(vsi);
  8651. return NULL;
  8652. }
  8653. /**
  8654. * i40e_vsi_setup - Set up a VSI by a given type
  8655. * @pf: board private structure
  8656. * @type: VSI type
  8657. * @uplink_seid: the switch element to link to
  8658. * @param1: usage depends upon VSI type. For VF types, indicates VF id
  8659. *
  8660. * This allocates the sw VSI structure and its queue resources, then add a VSI
  8661. * to the identified VEB.
  8662. *
  8663. * Returns pointer to the successfully allocated and configure VSI sw struct on
  8664. * success, otherwise returns NULL on failure.
  8665. **/
  8666. struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
  8667. u16 uplink_seid, u32 param1)
  8668. {
  8669. struct i40e_vsi *vsi = NULL;
  8670. struct i40e_veb *veb = NULL;
  8671. int ret, i;
  8672. int v_idx;
  8673. /* The requested uplink_seid must be either
  8674. * - the PF's port seid
  8675. * no VEB is needed because this is the PF
  8676. * or this is a Flow Director special case VSI
  8677. * - seid of an existing VEB
  8678. * - seid of a VSI that owns an existing VEB
  8679. * - seid of a VSI that doesn't own a VEB
  8680. * a new VEB is created and the VSI becomes the owner
  8681. * - seid of the PF VSI, which is what creates the first VEB
  8682. * this is a special case of the previous
  8683. *
  8684. * Find which uplink_seid we were given and create a new VEB if needed
  8685. */
  8686. for (i = 0; i < I40E_MAX_VEB; i++) {
  8687. if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
  8688. veb = pf->veb[i];
  8689. break;
  8690. }
  8691. }
  8692. if (!veb && uplink_seid != pf->mac_seid) {
  8693. for (i = 0; i < pf->num_alloc_vsi; i++) {
  8694. if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
  8695. vsi = pf->vsi[i];
  8696. break;
  8697. }
  8698. }
  8699. if (!vsi) {
  8700. dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
  8701. uplink_seid);
  8702. return NULL;
  8703. }
  8704. if (vsi->uplink_seid == pf->mac_seid)
  8705. veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
  8706. vsi->tc_config.enabled_tc);
  8707. else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
  8708. veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
  8709. vsi->tc_config.enabled_tc);
  8710. if (veb) {
  8711. if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
  8712. dev_info(&vsi->back->pdev->dev,
  8713. "New VSI creation error, uplink seid of LAN VSI expected.\n");
  8714. return NULL;
  8715. }
  8716. /* We come up by default in VEPA mode if SRIOV is not
  8717. * already enabled, in which case we can't force VEPA
  8718. * mode.
  8719. */
  8720. if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
  8721. veb->bridge_mode = BRIDGE_MODE_VEPA;
  8722. pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
  8723. }
  8724. i40e_config_bridge_mode(veb);
  8725. }
  8726. for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
  8727. if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
  8728. veb = pf->veb[i];
  8729. }
  8730. if (!veb) {
  8731. dev_info(&pf->pdev->dev, "couldn't add VEB\n");
  8732. return NULL;
  8733. }
  8734. vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
  8735. uplink_seid = veb->seid;
  8736. }
  8737. /* get vsi sw struct */
  8738. v_idx = i40e_vsi_mem_alloc(pf, type);
  8739. if (v_idx < 0)
  8740. goto err_alloc;
  8741. vsi = pf->vsi[v_idx];
  8742. if (!vsi)
  8743. goto err_alloc;
  8744. vsi->type = type;
  8745. vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
  8746. if (type == I40E_VSI_MAIN)
  8747. pf->lan_vsi = v_idx;
  8748. else if (type == I40E_VSI_SRIOV)
  8749. vsi->vf_id = param1;
  8750. /* assign it some queues */
  8751. ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs,
  8752. vsi->idx);
  8753. if (ret < 0) {
  8754. dev_info(&pf->pdev->dev,
  8755. "failed to get tracking for %d queues for VSI %d err=%d\n",
  8756. vsi->alloc_queue_pairs, vsi->seid, ret);
  8757. goto err_vsi;
  8758. }
  8759. vsi->base_queue = ret;
  8760. /* get a VSI from the hardware */
  8761. vsi->uplink_seid = uplink_seid;
  8762. ret = i40e_add_vsi(vsi);
  8763. if (ret)
  8764. goto err_vsi;
  8765. switch (vsi->type) {
  8766. /* setup the netdev if needed */
  8767. case I40E_VSI_MAIN:
  8768. /* Apply relevant filters if a platform-specific mac
  8769. * address was selected.
  8770. */
  8771. if (!!(pf->flags & I40E_FLAG_PF_MAC)) {
  8772. ret = i40e_macaddr_init(vsi, pf->hw.mac.addr);
  8773. if (ret) {
  8774. dev_warn(&pf->pdev->dev,
  8775. "could not set up macaddr; err %d\n",
  8776. ret);
  8777. }
  8778. }
  8779. case I40E_VSI_VMDQ2:
  8780. ret = i40e_config_netdev(vsi);
  8781. if (ret)
  8782. goto err_netdev;
  8783. ret = register_netdev(vsi->netdev);
  8784. if (ret)
  8785. goto err_netdev;
  8786. vsi->netdev_registered = true;
  8787. netif_carrier_off(vsi->netdev);
  8788. #ifdef CONFIG_I40E_DCB
  8789. /* Setup DCB netlink interface */
  8790. i40e_dcbnl_setup(vsi);
  8791. #endif /* CONFIG_I40E_DCB */
  8792. /* fall through */
  8793. case I40E_VSI_FDIR:
  8794. /* set up vectors and rings if needed */
  8795. ret = i40e_vsi_setup_vectors(vsi);
  8796. if (ret)
  8797. goto err_msix;
  8798. ret = i40e_alloc_rings(vsi);
  8799. if (ret)
  8800. goto err_rings;
  8801. /* map all of the rings to the q_vectors */
  8802. i40e_vsi_map_rings_to_vectors(vsi);
  8803. i40e_vsi_reset_stats(vsi);
  8804. break;
  8805. default:
  8806. /* no netdev or rings for the other VSI types */
  8807. break;
  8808. }
  8809. if ((pf->flags & I40E_FLAG_RSS_AQ_CAPABLE) &&
  8810. (vsi->type == I40E_VSI_VMDQ2)) {
  8811. ret = i40e_vsi_config_rss(vsi);
  8812. }
  8813. return vsi;
  8814. err_rings:
  8815. i40e_vsi_free_q_vectors(vsi);
  8816. err_msix:
  8817. if (vsi->netdev_registered) {
  8818. vsi->netdev_registered = false;
  8819. unregister_netdev(vsi->netdev);
  8820. free_netdev(vsi->netdev);
  8821. vsi->netdev = NULL;
  8822. }
  8823. err_netdev:
  8824. i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
  8825. err_vsi:
  8826. i40e_vsi_clear(vsi);
  8827. err_alloc:
  8828. return NULL;
  8829. }
  8830. /**
  8831. * i40e_veb_get_bw_info - Query VEB BW information
  8832. * @veb: the veb to query
  8833. *
  8834. * Query the Tx scheduler BW configuration data for given VEB
  8835. **/
  8836. static int i40e_veb_get_bw_info(struct i40e_veb *veb)
  8837. {
  8838. struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
  8839. struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
  8840. struct i40e_pf *pf = veb->pf;
  8841. struct i40e_hw *hw = &pf->hw;
  8842. u32 tc_bw_max;
  8843. int ret = 0;
  8844. int i;
  8845. ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
  8846. &bw_data, NULL);
  8847. if (ret) {
  8848. dev_info(&pf->pdev->dev,
  8849. "query veb bw config failed, err %s aq_err %s\n",
  8850. i40e_stat_str(&pf->hw, ret),
  8851. i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
  8852. goto out;
  8853. }
  8854. ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
  8855. &ets_data, NULL);
  8856. if (ret) {
  8857. dev_info(&pf->pdev->dev,
  8858. "query veb bw ets config failed, err %s aq_err %s\n",
  8859. i40e_stat_str(&pf->hw, ret),
  8860. i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
  8861. goto out;
  8862. }
  8863. veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
  8864. veb->bw_max_quanta = ets_data.tc_bw_max;
  8865. veb->is_abs_credits = bw_data.absolute_credits_enable;
  8866. veb->enabled_tc = ets_data.tc_valid_bits;
  8867. tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
  8868. (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
  8869. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  8870. veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
  8871. veb->bw_tc_limit_credits[i] =
  8872. le16_to_cpu(bw_data.tc_bw_limits[i]);
  8873. veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
  8874. }
  8875. out:
  8876. return ret;
  8877. }
  8878. /**
  8879. * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
  8880. * @pf: board private structure
  8881. *
  8882. * On error: returns error code (negative)
  8883. * On success: returns vsi index in PF (positive)
  8884. **/
  8885. static int i40e_veb_mem_alloc(struct i40e_pf *pf)
  8886. {
  8887. int ret = -ENOENT;
  8888. struct i40e_veb *veb;
  8889. int i;
  8890. /* Need to protect the allocation of switch elements at the PF level */
  8891. mutex_lock(&pf->switch_mutex);
  8892. /* VEB list may be fragmented if VEB creation/destruction has
  8893. * been happening. We can afford to do a quick scan to look
  8894. * for any free slots in the list.
  8895. *
  8896. * find next empty veb slot, looping back around if necessary
  8897. */
  8898. i = 0;
  8899. while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
  8900. i++;
  8901. if (i >= I40E_MAX_VEB) {
  8902. ret = -ENOMEM;
  8903. goto err_alloc_veb; /* out of VEB slots! */
  8904. }
  8905. veb = kzalloc(sizeof(*veb), GFP_KERNEL);
  8906. if (!veb) {
  8907. ret = -ENOMEM;
  8908. goto err_alloc_veb;
  8909. }
  8910. veb->pf = pf;
  8911. veb->idx = i;
  8912. veb->enabled_tc = 1;
  8913. pf->veb[i] = veb;
  8914. ret = i;
  8915. err_alloc_veb:
  8916. mutex_unlock(&pf->switch_mutex);
  8917. return ret;
  8918. }
  8919. /**
  8920. * i40e_switch_branch_release - Delete a branch of the switch tree
  8921. * @branch: where to start deleting
  8922. *
  8923. * This uses recursion to find the tips of the branch to be
  8924. * removed, deleting until we get back to and can delete this VEB.
  8925. **/
  8926. static void i40e_switch_branch_release(struct i40e_veb *branch)
  8927. {
  8928. struct i40e_pf *pf = branch->pf;
  8929. u16 branch_seid = branch->seid;
  8930. u16 veb_idx = branch->idx;
  8931. int i;
  8932. /* release any VEBs on this VEB - RECURSION */
  8933. for (i = 0; i < I40E_MAX_VEB; i++) {
  8934. if (!pf->veb[i])
  8935. continue;
  8936. if (pf->veb[i]->uplink_seid == branch->seid)
  8937. i40e_switch_branch_release(pf->veb[i]);
  8938. }
  8939. /* Release the VSIs on this VEB, but not the owner VSI.
  8940. *
  8941. * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
  8942. * the VEB itself, so don't use (*branch) after this loop.
  8943. */
  8944. for (i = 0; i < pf->num_alloc_vsi; i++) {
  8945. if (!pf->vsi[i])
  8946. continue;
  8947. if (pf->vsi[i]->uplink_seid == branch_seid &&
  8948. (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
  8949. i40e_vsi_release(pf->vsi[i]);
  8950. }
  8951. }
  8952. /* There's one corner case where the VEB might not have been
  8953. * removed, so double check it here and remove it if needed.
  8954. * This case happens if the veb was created from the debugfs
  8955. * commands and no VSIs were added to it.
  8956. */
  8957. if (pf->veb[veb_idx])
  8958. i40e_veb_release(pf->veb[veb_idx]);
  8959. }
  8960. /**
  8961. * i40e_veb_clear - remove veb struct
  8962. * @veb: the veb to remove
  8963. **/
  8964. static void i40e_veb_clear(struct i40e_veb *veb)
  8965. {
  8966. if (!veb)
  8967. return;
  8968. if (veb->pf) {
  8969. struct i40e_pf *pf = veb->pf;
  8970. mutex_lock(&pf->switch_mutex);
  8971. if (pf->veb[veb->idx] == veb)
  8972. pf->veb[veb->idx] = NULL;
  8973. mutex_unlock(&pf->switch_mutex);
  8974. }
  8975. kfree(veb);
  8976. }
  8977. /**
  8978. * i40e_veb_release - Delete a VEB and free its resources
  8979. * @veb: the VEB being removed
  8980. **/
  8981. void i40e_veb_release(struct i40e_veb *veb)
  8982. {
  8983. struct i40e_vsi *vsi = NULL;
  8984. struct i40e_pf *pf;
  8985. int i, n = 0;
  8986. pf = veb->pf;
  8987. /* find the remaining VSI and check for extras */
  8988. for (i = 0; i < pf->num_alloc_vsi; i++) {
  8989. if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
  8990. n++;
  8991. vsi = pf->vsi[i];
  8992. }
  8993. }
  8994. if (n != 1) {
  8995. dev_info(&pf->pdev->dev,
  8996. "can't remove VEB %d with %d VSIs left\n",
  8997. veb->seid, n);
  8998. return;
  8999. }
  9000. /* move the remaining VSI to uplink veb */
  9001. vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
  9002. if (veb->uplink_seid) {
  9003. vsi->uplink_seid = veb->uplink_seid;
  9004. if (veb->uplink_seid == pf->mac_seid)
  9005. vsi->veb_idx = I40E_NO_VEB;
  9006. else
  9007. vsi->veb_idx = veb->veb_idx;
  9008. } else {
  9009. /* floating VEB */
  9010. vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
  9011. vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
  9012. }
  9013. i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
  9014. i40e_veb_clear(veb);
  9015. }
  9016. /**
  9017. * i40e_add_veb - create the VEB in the switch
  9018. * @veb: the VEB to be instantiated
  9019. * @vsi: the controlling VSI
  9020. **/
  9021. static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
  9022. {
  9023. struct i40e_pf *pf = veb->pf;
  9024. bool enable_stats = !!(pf->flags & I40E_FLAG_VEB_STATS_ENABLED);
  9025. int ret;
  9026. ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi->seid,
  9027. veb->enabled_tc, false,
  9028. &veb->seid, enable_stats, NULL);
  9029. /* get a VEB from the hardware */
  9030. if (ret) {
  9031. dev_info(&pf->pdev->dev,
  9032. "couldn't add VEB, err %s aq_err %s\n",
  9033. i40e_stat_str(&pf->hw, ret),
  9034. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  9035. return -EPERM;
  9036. }
  9037. /* get statistics counter */
  9038. ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
  9039. &veb->stats_idx, NULL, NULL, NULL);
  9040. if (ret) {
  9041. dev_info(&pf->pdev->dev,
  9042. "couldn't get VEB statistics idx, err %s aq_err %s\n",
  9043. i40e_stat_str(&pf->hw, ret),
  9044. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  9045. return -EPERM;
  9046. }
  9047. ret = i40e_veb_get_bw_info(veb);
  9048. if (ret) {
  9049. dev_info(&pf->pdev->dev,
  9050. "couldn't get VEB bw info, err %s aq_err %s\n",
  9051. i40e_stat_str(&pf->hw, ret),
  9052. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  9053. i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
  9054. return -ENOENT;
  9055. }
  9056. vsi->uplink_seid = veb->seid;
  9057. vsi->veb_idx = veb->idx;
  9058. vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
  9059. return 0;
  9060. }
  9061. /**
  9062. * i40e_veb_setup - Set up a VEB
  9063. * @pf: board private structure
  9064. * @flags: VEB setup flags
  9065. * @uplink_seid: the switch element to link to
  9066. * @vsi_seid: the initial VSI seid
  9067. * @enabled_tc: Enabled TC bit-map
  9068. *
  9069. * This allocates the sw VEB structure and links it into the switch
  9070. * It is possible and legal for this to be a duplicate of an already
  9071. * existing VEB. It is also possible for both uplink and vsi seids
  9072. * to be zero, in order to create a floating VEB.
  9073. *
  9074. * Returns pointer to the successfully allocated VEB sw struct on
  9075. * success, otherwise returns NULL on failure.
  9076. **/
  9077. struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
  9078. u16 uplink_seid, u16 vsi_seid,
  9079. u8 enabled_tc)
  9080. {
  9081. struct i40e_veb *veb, *uplink_veb = NULL;
  9082. int vsi_idx, veb_idx;
  9083. int ret;
  9084. /* if one seid is 0, the other must be 0 to create a floating relay */
  9085. if ((uplink_seid == 0 || vsi_seid == 0) &&
  9086. (uplink_seid + vsi_seid != 0)) {
  9087. dev_info(&pf->pdev->dev,
  9088. "one, not both seid's are 0: uplink=%d vsi=%d\n",
  9089. uplink_seid, vsi_seid);
  9090. return NULL;
  9091. }
  9092. /* make sure there is such a vsi and uplink */
  9093. for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
  9094. if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
  9095. break;
  9096. if (vsi_idx >= pf->num_alloc_vsi && vsi_seid != 0) {
  9097. dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
  9098. vsi_seid);
  9099. return NULL;
  9100. }
  9101. if (uplink_seid && uplink_seid != pf->mac_seid) {
  9102. for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
  9103. if (pf->veb[veb_idx] &&
  9104. pf->veb[veb_idx]->seid == uplink_seid) {
  9105. uplink_veb = pf->veb[veb_idx];
  9106. break;
  9107. }
  9108. }
  9109. if (!uplink_veb) {
  9110. dev_info(&pf->pdev->dev,
  9111. "uplink seid %d not found\n", uplink_seid);
  9112. return NULL;
  9113. }
  9114. }
  9115. /* get veb sw struct */
  9116. veb_idx = i40e_veb_mem_alloc(pf);
  9117. if (veb_idx < 0)
  9118. goto err_alloc;
  9119. veb = pf->veb[veb_idx];
  9120. veb->flags = flags;
  9121. veb->uplink_seid = uplink_seid;
  9122. veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
  9123. veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
  9124. /* create the VEB in the switch */
  9125. ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
  9126. if (ret)
  9127. goto err_veb;
  9128. if (vsi_idx == pf->lan_vsi)
  9129. pf->lan_veb = veb->idx;
  9130. return veb;
  9131. err_veb:
  9132. i40e_veb_clear(veb);
  9133. err_alloc:
  9134. return NULL;
  9135. }
  9136. /**
  9137. * i40e_setup_pf_switch_element - set PF vars based on switch type
  9138. * @pf: board private structure
  9139. * @ele: element we are building info from
  9140. * @num_reported: total number of elements
  9141. * @printconfig: should we print the contents
  9142. *
  9143. * helper function to assist in extracting a few useful SEID values.
  9144. **/
  9145. static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
  9146. struct i40e_aqc_switch_config_element_resp *ele,
  9147. u16 num_reported, bool printconfig)
  9148. {
  9149. u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
  9150. u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
  9151. u8 element_type = ele->element_type;
  9152. u16 seid = le16_to_cpu(ele->seid);
  9153. if (printconfig)
  9154. dev_info(&pf->pdev->dev,
  9155. "type=%d seid=%d uplink=%d downlink=%d\n",
  9156. element_type, seid, uplink_seid, downlink_seid);
  9157. switch (element_type) {
  9158. case I40E_SWITCH_ELEMENT_TYPE_MAC:
  9159. pf->mac_seid = seid;
  9160. break;
  9161. case I40E_SWITCH_ELEMENT_TYPE_VEB:
  9162. /* Main VEB? */
  9163. if (uplink_seid != pf->mac_seid)
  9164. break;
  9165. if (pf->lan_veb == I40E_NO_VEB) {
  9166. int v;
  9167. /* find existing or else empty VEB */
  9168. for (v = 0; v < I40E_MAX_VEB; v++) {
  9169. if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
  9170. pf->lan_veb = v;
  9171. break;
  9172. }
  9173. }
  9174. if (pf->lan_veb == I40E_NO_VEB) {
  9175. v = i40e_veb_mem_alloc(pf);
  9176. if (v < 0)
  9177. break;
  9178. pf->lan_veb = v;
  9179. }
  9180. }
  9181. pf->veb[pf->lan_veb]->seid = seid;
  9182. pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
  9183. pf->veb[pf->lan_veb]->pf = pf;
  9184. pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
  9185. break;
  9186. case I40E_SWITCH_ELEMENT_TYPE_VSI:
  9187. if (num_reported != 1)
  9188. break;
  9189. /* This is immediately after a reset so we can assume this is
  9190. * the PF's VSI
  9191. */
  9192. pf->mac_seid = uplink_seid;
  9193. pf->pf_seid = downlink_seid;
  9194. pf->main_vsi_seid = seid;
  9195. if (printconfig)
  9196. dev_info(&pf->pdev->dev,
  9197. "pf_seid=%d main_vsi_seid=%d\n",
  9198. pf->pf_seid, pf->main_vsi_seid);
  9199. break;
  9200. case I40E_SWITCH_ELEMENT_TYPE_PF:
  9201. case I40E_SWITCH_ELEMENT_TYPE_VF:
  9202. case I40E_SWITCH_ELEMENT_TYPE_EMP:
  9203. case I40E_SWITCH_ELEMENT_TYPE_BMC:
  9204. case I40E_SWITCH_ELEMENT_TYPE_PE:
  9205. case I40E_SWITCH_ELEMENT_TYPE_PA:
  9206. /* ignore these for now */
  9207. break;
  9208. default:
  9209. dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
  9210. element_type, seid);
  9211. break;
  9212. }
  9213. }
  9214. /**
  9215. * i40e_fetch_switch_configuration - Get switch config from firmware
  9216. * @pf: board private structure
  9217. * @printconfig: should we print the contents
  9218. *
  9219. * Get the current switch configuration from the device and
  9220. * extract a few useful SEID values.
  9221. **/
  9222. int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
  9223. {
  9224. struct i40e_aqc_get_switch_config_resp *sw_config;
  9225. u16 next_seid = 0;
  9226. int ret = 0;
  9227. u8 *aq_buf;
  9228. int i;
  9229. aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
  9230. if (!aq_buf)
  9231. return -ENOMEM;
  9232. sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
  9233. do {
  9234. u16 num_reported, num_total;
  9235. ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
  9236. I40E_AQ_LARGE_BUF,
  9237. &next_seid, NULL);
  9238. if (ret) {
  9239. dev_info(&pf->pdev->dev,
  9240. "get switch config failed err %s aq_err %s\n",
  9241. i40e_stat_str(&pf->hw, ret),
  9242. i40e_aq_str(&pf->hw,
  9243. pf->hw.aq.asq_last_status));
  9244. kfree(aq_buf);
  9245. return -ENOENT;
  9246. }
  9247. num_reported = le16_to_cpu(sw_config->header.num_reported);
  9248. num_total = le16_to_cpu(sw_config->header.num_total);
  9249. if (printconfig)
  9250. dev_info(&pf->pdev->dev,
  9251. "header: %d reported %d total\n",
  9252. num_reported, num_total);
  9253. for (i = 0; i < num_reported; i++) {
  9254. struct i40e_aqc_switch_config_element_resp *ele =
  9255. &sw_config->element[i];
  9256. i40e_setup_pf_switch_element(pf, ele, num_reported,
  9257. printconfig);
  9258. }
  9259. } while (next_seid != 0);
  9260. kfree(aq_buf);
  9261. return ret;
  9262. }
  9263. /**
  9264. * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
  9265. * @pf: board private structure
  9266. * @reinit: if the Main VSI needs to re-initialized.
  9267. *
  9268. * Returns 0 on success, negative value on failure
  9269. **/
  9270. static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit)
  9271. {
  9272. u16 flags = 0;
  9273. int ret;
  9274. /* find out what's out there already */
  9275. ret = i40e_fetch_switch_configuration(pf, false);
  9276. if (ret) {
  9277. dev_info(&pf->pdev->dev,
  9278. "couldn't fetch switch config, err %s aq_err %s\n",
  9279. i40e_stat_str(&pf->hw, ret),
  9280. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  9281. return ret;
  9282. }
  9283. i40e_pf_reset_stats(pf);
  9284. /* set the switch config bit for the whole device to
  9285. * support limited promisc or true promisc
  9286. * when user requests promisc. The default is limited
  9287. * promisc.
  9288. */
  9289. if ((pf->hw.pf_id == 0) &&
  9290. !(pf->flags & I40E_FLAG_TRUE_PROMISC_SUPPORT))
  9291. flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
  9292. if (pf->hw.pf_id == 0) {
  9293. u16 valid_flags;
  9294. valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
  9295. ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags,
  9296. NULL);
  9297. if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) {
  9298. dev_info(&pf->pdev->dev,
  9299. "couldn't set switch config bits, err %s aq_err %s\n",
  9300. i40e_stat_str(&pf->hw, ret),
  9301. i40e_aq_str(&pf->hw,
  9302. pf->hw.aq.asq_last_status));
  9303. /* not a fatal problem, just keep going */
  9304. }
  9305. }
  9306. /* first time setup */
  9307. if (pf->lan_vsi == I40E_NO_VSI || reinit) {
  9308. struct i40e_vsi *vsi = NULL;
  9309. u16 uplink_seid;
  9310. /* Set up the PF VSI associated with the PF's main VSI
  9311. * that is already in the HW switch
  9312. */
  9313. if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
  9314. uplink_seid = pf->veb[pf->lan_veb]->seid;
  9315. else
  9316. uplink_seid = pf->mac_seid;
  9317. if (pf->lan_vsi == I40E_NO_VSI)
  9318. vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
  9319. else if (reinit)
  9320. vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
  9321. if (!vsi) {
  9322. dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
  9323. i40e_fdir_teardown(pf);
  9324. return -EAGAIN;
  9325. }
  9326. } else {
  9327. /* force a reset of TC and queue layout configurations */
  9328. u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
  9329. pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
  9330. pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
  9331. i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
  9332. }
  9333. i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
  9334. i40e_fdir_sb_setup(pf);
  9335. /* Setup static PF queue filter control settings */
  9336. ret = i40e_setup_pf_filter_control(pf);
  9337. if (ret) {
  9338. dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
  9339. ret);
  9340. /* Failure here should not stop continuing other steps */
  9341. }
  9342. /* enable RSS in the HW, even for only one queue, as the stack can use
  9343. * the hash
  9344. */
  9345. if ((pf->flags & I40E_FLAG_RSS_ENABLED))
  9346. i40e_pf_config_rss(pf);
  9347. /* fill in link information and enable LSE reporting */
  9348. i40e_link_event(pf);
  9349. /* Initialize user-specific link properties */
  9350. pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
  9351. I40E_AQ_AN_COMPLETED) ? true : false);
  9352. i40e_ptp_init(pf);
  9353. return ret;
  9354. }
  9355. /**
  9356. * i40e_determine_queue_usage - Work out queue distribution
  9357. * @pf: board private structure
  9358. **/
  9359. static void i40e_determine_queue_usage(struct i40e_pf *pf)
  9360. {
  9361. int queues_left;
  9362. pf->num_lan_qps = 0;
  9363. /* Find the max queues to be put into basic use. We'll always be
  9364. * using TC0, whether or not DCB is running, and TC0 will get the
  9365. * big RSS set.
  9366. */
  9367. queues_left = pf->hw.func_caps.num_tx_qp;
  9368. if ((queues_left == 1) ||
  9369. !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
  9370. /* one qp for PF, no queues for anything else */
  9371. queues_left = 0;
  9372. pf->alloc_rss_size = pf->num_lan_qps = 1;
  9373. /* make sure all the fancies are disabled */
  9374. pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
  9375. I40E_FLAG_IWARP_ENABLED |
  9376. I40E_FLAG_FD_SB_ENABLED |
  9377. I40E_FLAG_FD_ATR_ENABLED |
  9378. I40E_FLAG_DCB_CAPABLE |
  9379. I40E_FLAG_DCB_ENABLED |
  9380. I40E_FLAG_SRIOV_ENABLED |
  9381. I40E_FLAG_VMDQ_ENABLED);
  9382. } else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
  9383. I40E_FLAG_FD_SB_ENABLED |
  9384. I40E_FLAG_FD_ATR_ENABLED |
  9385. I40E_FLAG_DCB_CAPABLE))) {
  9386. /* one qp for PF */
  9387. pf->alloc_rss_size = pf->num_lan_qps = 1;
  9388. queues_left -= pf->num_lan_qps;
  9389. pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
  9390. I40E_FLAG_IWARP_ENABLED |
  9391. I40E_FLAG_FD_SB_ENABLED |
  9392. I40E_FLAG_FD_ATR_ENABLED |
  9393. I40E_FLAG_DCB_ENABLED |
  9394. I40E_FLAG_VMDQ_ENABLED);
  9395. } else {
  9396. /* Not enough queues for all TCs */
  9397. if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
  9398. (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
  9399. pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
  9400. I40E_FLAG_DCB_ENABLED);
  9401. dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
  9402. }
  9403. pf->num_lan_qps = max_t(int, pf->rss_size_max,
  9404. num_online_cpus());
  9405. pf->num_lan_qps = min_t(int, pf->num_lan_qps,
  9406. pf->hw.func_caps.num_tx_qp);
  9407. queues_left -= pf->num_lan_qps;
  9408. }
  9409. if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
  9410. if (queues_left > 1) {
  9411. queues_left -= 1; /* save 1 queue for FD */
  9412. } else {
  9413. pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
  9414. dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
  9415. }
  9416. }
  9417. if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
  9418. pf->num_vf_qps && pf->num_req_vfs && queues_left) {
  9419. pf->num_req_vfs = min_t(int, pf->num_req_vfs,
  9420. (queues_left / pf->num_vf_qps));
  9421. queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
  9422. }
  9423. if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
  9424. pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
  9425. pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
  9426. (queues_left / pf->num_vmdq_qps));
  9427. queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
  9428. }
  9429. pf->queues_left = queues_left;
  9430. dev_dbg(&pf->pdev->dev,
  9431. "qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
  9432. pf->hw.func_caps.num_tx_qp,
  9433. !!(pf->flags & I40E_FLAG_FD_SB_ENABLED),
  9434. pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
  9435. pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
  9436. queues_left);
  9437. }
  9438. /**
  9439. * i40e_setup_pf_filter_control - Setup PF static filter control
  9440. * @pf: PF to be setup
  9441. *
  9442. * i40e_setup_pf_filter_control sets up a PF's initial filter control
  9443. * settings. If PE/FCoE are enabled then it will also set the per PF
  9444. * based filter sizes required for them. It also enables Flow director,
  9445. * ethertype and macvlan type filter settings for the pf.
  9446. *
  9447. * Returns 0 on success, negative on failure
  9448. **/
  9449. static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
  9450. {
  9451. struct i40e_filter_control_settings *settings = &pf->filter_settings;
  9452. settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
  9453. /* Flow Director is enabled */
  9454. if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
  9455. settings->enable_fdir = true;
  9456. /* Ethtype and MACVLAN filters enabled for PF */
  9457. settings->enable_ethtype = true;
  9458. settings->enable_macvlan = true;
  9459. if (i40e_set_filter_control(&pf->hw, settings))
  9460. return -ENOENT;
  9461. return 0;
  9462. }
  9463. #define INFO_STRING_LEN 255
  9464. #define REMAIN(__x) (INFO_STRING_LEN - (__x))
  9465. static void i40e_print_features(struct i40e_pf *pf)
  9466. {
  9467. struct i40e_hw *hw = &pf->hw;
  9468. char *buf;
  9469. int i;
  9470. buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
  9471. if (!buf)
  9472. return;
  9473. i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
  9474. #ifdef CONFIG_PCI_IOV
  9475. i += snprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
  9476. #endif
  9477. i += snprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d",
  9478. pf->hw.func_caps.num_vsis,
  9479. pf->vsi[pf->lan_vsi]->num_queue_pairs);
  9480. if (pf->flags & I40E_FLAG_RSS_ENABLED)
  9481. i += snprintf(&buf[i], REMAIN(i), " RSS");
  9482. if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
  9483. i += snprintf(&buf[i], REMAIN(i), " FD_ATR");
  9484. if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
  9485. i += snprintf(&buf[i], REMAIN(i), " FD_SB");
  9486. i += snprintf(&buf[i], REMAIN(i), " NTUPLE");
  9487. }
  9488. if (pf->flags & I40E_FLAG_DCB_CAPABLE)
  9489. i += snprintf(&buf[i], REMAIN(i), " DCB");
  9490. i += snprintf(&buf[i], REMAIN(i), " VxLAN");
  9491. i += snprintf(&buf[i], REMAIN(i), " Geneve");
  9492. if (pf->flags & I40E_FLAG_PTP)
  9493. i += snprintf(&buf[i], REMAIN(i), " PTP");
  9494. if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
  9495. i += snprintf(&buf[i], REMAIN(i), " VEB");
  9496. else
  9497. i += snprintf(&buf[i], REMAIN(i), " VEPA");
  9498. dev_info(&pf->pdev->dev, "%s\n", buf);
  9499. kfree(buf);
  9500. WARN_ON(i > INFO_STRING_LEN);
  9501. }
  9502. /**
  9503. * i40e_get_platform_mac_addr - get platform-specific MAC address
  9504. * @pdev: PCI device information struct
  9505. * @pf: board private structure
  9506. *
  9507. * Look up the MAC address for the device. First we'll try
  9508. * eth_platform_get_mac_address, which will check Open Firmware, or arch
  9509. * specific fallback. Otherwise, we'll default to the stored value in
  9510. * firmware.
  9511. **/
  9512. static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
  9513. {
  9514. if (eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
  9515. i40e_get_mac_addr(&pf->hw, pf->hw.mac.addr);
  9516. }
  9517. /**
  9518. * i40e_probe - Device initialization routine
  9519. * @pdev: PCI device information struct
  9520. * @ent: entry in i40e_pci_tbl
  9521. *
  9522. * i40e_probe initializes a PF identified by a pci_dev structure.
  9523. * The OS initialization, configuring of the PF private structure,
  9524. * and a hardware reset occur.
  9525. *
  9526. * Returns 0 on success, negative on failure
  9527. **/
  9528. static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  9529. {
  9530. struct i40e_aq_get_phy_abilities_resp abilities;
  9531. struct i40e_pf *pf;
  9532. struct i40e_hw *hw;
  9533. static u16 pfs_found;
  9534. u16 wol_nvm_bits;
  9535. u16 link_status;
  9536. int err;
  9537. u32 val;
  9538. u32 i;
  9539. u8 set_fc_aq_fail;
  9540. err = pci_enable_device_mem(pdev);
  9541. if (err)
  9542. return err;
  9543. /* set up for high or low dma */
  9544. err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
  9545. if (err) {
  9546. err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  9547. if (err) {
  9548. dev_err(&pdev->dev,
  9549. "DMA configuration failed: 0x%x\n", err);
  9550. goto err_dma;
  9551. }
  9552. }
  9553. /* set up pci connections */
  9554. err = pci_request_mem_regions(pdev, i40e_driver_name);
  9555. if (err) {
  9556. dev_info(&pdev->dev,
  9557. "pci_request_selected_regions failed %d\n", err);
  9558. goto err_pci_reg;
  9559. }
  9560. pci_enable_pcie_error_reporting(pdev);
  9561. pci_set_master(pdev);
  9562. /* Now that we have a PCI connection, we need to do the
  9563. * low level device setup. This is primarily setting up
  9564. * the Admin Queue structures and then querying for the
  9565. * device's current profile information.
  9566. */
  9567. pf = kzalloc(sizeof(*pf), GFP_KERNEL);
  9568. if (!pf) {
  9569. err = -ENOMEM;
  9570. goto err_pf_alloc;
  9571. }
  9572. pf->next_vsi = 0;
  9573. pf->pdev = pdev;
  9574. set_bit(__I40E_DOWN, &pf->state);
  9575. hw = &pf->hw;
  9576. hw->back = pf;
  9577. pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
  9578. I40E_MAX_CSR_SPACE);
  9579. hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
  9580. if (!hw->hw_addr) {
  9581. err = -EIO;
  9582. dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
  9583. (unsigned int)pci_resource_start(pdev, 0),
  9584. pf->ioremap_len, err);
  9585. goto err_ioremap;
  9586. }
  9587. hw->vendor_id = pdev->vendor;
  9588. hw->device_id = pdev->device;
  9589. pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
  9590. hw->subsystem_vendor_id = pdev->subsystem_vendor;
  9591. hw->subsystem_device_id = pdev->subsystem_device;
  9592. hw->bus.device = PCI_SLOT(pdev->devfn);
  9593. hw->bus.func = PCI_FUNC(pdev->devfn);
  9594. hw->bus.bus_id = pdev->bus->number;
  9595. pf->instance = pfs_found;
  9596. INIT_LIST_HEAD(&pf->l3_flex_pit_list);
  9597. INIT_LIST_HEAD(&pf->l4_flex_pit_list);
  9598. /* set up the locks for the AQ, do this only once in probe
  9599. * and destroy them only once in remove
  9600. */
  9601. mutex_init(&hw->aq.asq_mutex);
  9602. mutex_init(&hw->aq.arq_mutex);
  9603. pf->msg_enable = netif_msg_init(debug,
  9604. NETIF_MSG_DRV |
  9605. NETIF_MSG_PROBE |
  9606. NETIF_MSG_LINK);
  9607. if (debug < -1)
  9608. pf->hw.debug_mask = debug;
  9609. /* do a special CORER for clearing PXE mode once at init */
  9610. if (hw->revision_id == 0 &&
  9611. (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
  9612. wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
  9613. i40e_flush(hw);
  9614. msleep(200);
  9615. pf->corer_count++;
  9616. i40e_clear_pxe_mode(hw);
  9617. }
  9618. /* Reset here to make sure all is clean and to define PF 'n' */
  9619. i40e_clear_hw(hw);
  9620. err = i40e_pf_reset(hw);
  9621. if (err) {
  9622. dev_info(&pdev->dev, "Initial pf_reset failed: %d\n", err);
  9623. goto err_pf_reset;
  9624. }
  9625. pf->pfr_count++;
  9626. hw->aq.num_arq_entries = I40E_AQ_LEN;
  9627. hw->aq.num_asq_entries = I40E_AQ_LEN;
  9628. hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
  9629. hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
  9630. pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
  9631. snprintf(pf->int_name, sizeof(pf->int_name) - 1,
  9632. "%s-%s:misc",
  9633. dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
  9634. err = i40e_init_shared_code(hw);
  9635. if (err) {
  9636. dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
  9637. err);
  9638. goto err_pf_reset;
  9639. }
  9640. /* set up a default setting for link flow control */
  9641. pf->hw.fc.requested_mode = I40E_FC_NONE;
  9642. err = i40e_init_adminq(hw);
  9643. if (err) {
  9644. if (err == I40E_ERR_FIRMWARE_API_VERSION)
  9645. dev_info(&pdev->dev,
  9646. "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");
  9647. else
  9648. dev_info(&pdev->dev,
  9649. "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
  9650. goto err_pf_reset;
  9651. }
  9652. /* provide nvm, fw, api versions */
  9653. dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s\n",
  9654. hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
  9655. hw->aq.api_maj_ver, hw->aq.api_min_ver,
  9656. i40e_nvm_version_str(hw));
  9657. if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
  9658. hw->aq.api_min_ver > I40E_FW_API_VERSION_MINOR)
  9659. dev_info(&pdev->dev,
  9660. "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");
  9661. else if (hw->aq.api_maj_ver < I40E_FW_API_VERSION_MAJOR ||
  9662. hw->aq.api_min_ver < (I40E_FW_API_VERSION_MINOR - 1))
  9663. dev_info(&pdev->dev,
  9664. "The driver for the device detected an older version of the NVM image than expected. Please update the NVM image.\n");
  9665. i40e_verify_eeprom(pf);
  9666. /* Rev 0 hardware was never productized */
  9667. if (hw->revision_id < 1)
  9668. 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");
  9669. i40e_clear_pxe_mode(hw);
  9670. err = i40e_get_capabilities(pf);
  9671. if (err)
  9672. goto err_adminq_setup;
  9673. err = i40e_sw_init(pf);
  9674. if (err) {
  9675. dev_info(&pdev->dev, "sw_init failed: %d\n", err);
  9676. goto err_sw_init;
  9677. }
  9678. err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
  9679. hw->func_caps.num_rx_qp, 0, 0);
  9680. if (err) {
  9681. dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
  9682. goto err_init_lan_hmc;
  9683. }
  9684. err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
  9685. if (err) {
  9686. dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
  9687. err = -ENOENT;
  9688. goto err_configure_lan_hmc;
  9689. }
  9690. /* Disable LLDP for NICs that have firmware versions lower than v4.3.
  9691. * Ignore error return codes because if it was already disabled via
  9692. * hardware settings this will fail
  9693. */
  9694. if (pf->flags & I40E_FLAG_STOP_FW_LLDP) {
  9695. dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
  9696. i40e_aq_stop_lldp(hw, true, NULL);
  9697. }
  9698. /* allow a platform config to override the HW addr */
  9699. i40e_get_platform_mac_addr(pdev, pf);
  9700. if (!is_valid_ether_addr(hw->mac.addr)) {
  9701. dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
  9702. err = -EIO;
  9703. goto err_mac_addr;
  9704. }
  9705. dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
  9706. ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
  9707. i40e_get_port_mac_addr(hw, hw->mac.port_addr);
  9708. if (is_valid_ether_addr(hw->mac.port_addr))
  9709. pf->flags |= I40E_FLAG_PORT_ID_VALID;
  9710. pci_set_drvdata(pdev, pf);
  9711. pci_save_state(pdev);
  9712. #ifdef CONFIG_I40E_DCB
  9713. err = i40e_init_pf_dcb(pf);
  9714. if (err) {
  9715. dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
  9716. pf->flags &= ~(I40E_FLAG_DCB_CAPABLE | I40E_FLAG_DCB_ENABLED);
  9717. /* Continue without DCB enabled */
  9718. }
  9719. #endif /* CONFIG_I40E_DCB */
  9720. /* set up periodic task facility */
  9721. setup_timer(&pf->service_timer, i40e_service_timer, (unsigned long)pf);
  9722. pf->service_timer_period = HZ;
  9723. INIT_WORK(&pf->service_task, i40e_service_task);
  9724. clear_bit(__I40E_SERVICE_SCHED, &pf->state);
  9725. pf->flags |= I40E_FLAG_NEED_LINK_UPDATE;
  9726. /* NVM bit on means WoL disabled for the port */
  9727. i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
  9728. if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
  9729. pf->wol_en = false;
  9730. else
  9731. pf->wol_en = true;
  9732. device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
  9733. /* set up the main switch operations */
  9734. i40e_determine_queue_usage(pf);
  9735. err = i40e_init_interrupt_scheme(pf);
  9736. if (err)
  9737. goto err_switch_setup;
  9738. /* The number of VSIs reported by the FW is the minimum guaranteed
  9739. * to us; HW supports far more and we share the remaining pool with
  9740. * the other PFs. We allocate space for more than the guarantee with
  9741. * the understanding that we might not get them all later.
  9742. */
  9743. if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
  9744. pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
  9745. else
  9746. pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
  9747. /* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
  9748. pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
  9749. GFP_KERNEL);
  9750. if (!pf->vsi) {
  9751. err = -ENOMEM;
  9752. goto err_switch_setup;
  9753. }
  9754. #ifdef CONFIG_PCI_IOV
  9755. /* prep for VF support */
  9756. if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
  9757. (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
  9758. !test_bit(__I40E_BAD_EEPROM, &pf->state)) {
  9759. if (pci_num_vf(pdev))
  9760. pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
  9761. }
  9762. #endif
  9763. err = i40e_setup_pf_switch(pf, false);
  9764. if (err) {
  9765. dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
  9766. goto err_vsis;
  9767. }
  9768. /* Make sure flow control is set according to current settings */
  9769. err = i40e_set_fc(hw, &set_fc_aq_fail, true);
  9770. if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_GET)
  9771. dev_dbg(&pf->pdev->dev,
  9772. "Set fc with err %s aq_err %s on get_phy_cap\n",
  9773. i40e_stat_str(hw, err),
  9774. i40e_aq_str(hw, hw->aq.asq_last_status));
  9775. if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_SET)
  9776. dev_dbg(&pf->pdev->dev,
  9777. "Set fc with err %s aq_err %s on set_phy_config\n",
  9778. i40e_stat_str(hw, err),
  9779. i40e_aq_str(hw, hw->aq.asq_last_status));
  9780. if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_UPDATE)
  9781. dev_dbg(&pf->pdev->dev,
  9782. "Set fc with err %s aq_err %s on get_link_info\n",
  9783. i40e_stat_str(hw, err),
  9784. i40e_aq_str(hw, hw->aq.asq_last_status));
  9785. /* if FDIR VSI was set up, start it now */
  9786. for (i = 0; i < pf->num_alloc_vsi; i++) {
  9787. if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
  9788. i40e_vsi_open(pf->vsi[i]);
  9789. break;
  9790. }
  9791. }
  9792. /* The driver only wants link up/down and module qualification
  9793. * reports from firmware. Note the negative logic.
  9794. */
  9795. err = i40e_aq_set_phy_int_mask(&pf->hw,
  9796. ~(I40E_AQ_EVENT_LINK_UPDOWN |
  9797. I40E_AQ_EVENT_MEDIA_NA |
  9798. I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
  9799. if (err)
  9800. dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
  9801. i40e_stat_str(&pf->hw, err),
  9802. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  9803. /* Reconfigure hardware for allowing smaller MSS in the case
  9804. * of TSO, so that we avoid the MDD being fired and causing
  9805. * a reset in the case of small MSS+TSO.
  9806. */
  9807. val = rd32(hw, I40E_REG_MSS);
  9808. if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
  9809. val &= ~I40E_REG_MSS_MIN_MASK;
  9810. val |= I40E_64BYTE_MSS;
  9811. wr32(hw, I40E_REG_MSS, val);
  9812. }
  9813. if (pf->flags & I40E_FLAG_RESTART_AUTONEG) {
  9814. msleep(75);
  9815. err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
  9816. if (err)
  9817. dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
  9818. i40e_stat_str(&pf->hw, err),
  9819. i40e_aq_str(&pf->hw,
  9820. pf->hw.aq.asq_last_status));
  9821. }
  9822. /* The main driver is (mostly) up and happy. We need to set this state
  9823. * before setting up the misc vector or we get a race and the vector
  9824. * ends up disabled forever.
  9825. */
  9826. clear_bit(__I40E_DOWN, &pf->state);
  9827. /* In case of MSIX we are going to setup the misc vector right here
  9828. * to handle admin queue events etc. In case of legacy and MSI
  9829. * the misc functionality and queue processing is combined in
  9830. * the same vector and that gets setup at open.
  9831. */
  9832. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  9833. err = i40e_setup_misc_vector(pf);
  9834. if (err) {
  9835. dev_info(&pdev->dev,
  9836. "setup of misc vector failed: %d\n", err);
  9837. goto err_vsis;
  9838. }
  9839. }
  9840. #ifdef CONFIG_PCI_IOV
  9841. /* prep for VF support */
  9842. if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
  9843. (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
  9844. !test_bit(__I40E_BAD_EEPROM, &pf->state)) {
  9845. /* disable link interrupts for VFs */
  9846. val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
  9847. val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
  9848. wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
  9849. i40e_flush(hw);
  9850. if (pci_num_vf(pdev)) {
  9851. dev_info(&pdev->dev,
  9852. "Active VFs found, allocating resources.\n");
  9853. err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
  9854. if (err)
  9855. dev_info(&pdev->dev,
  9856. "Error %d allocating resources for existing VFs\n",
  9857. err);
  9858. }
  9859. }
  9860. #endif /* CONFIG_PCI_IOV */
  9861. if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
  9862. pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
  9863. pf->num_iwarp_msix,
  9864. I40E_IWARP_IRQ_PILE_ID);
  9865. if (pf->iwarp_base_vector < 0) {
  9866. dev_info(&pdev->dev,
  9867. "failed to get tracking for %d vectors for IWARP err=%d\n",
  9868. pf->num_iwarp_msix, pf->iwarp_base_vector);
  9869. pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
  9870. }
  9871. }
  9872. i40e_dbg_pf_init(pf);
  9873. /* tell the firmware that we're starting */
  9874. i40e_send_version(pf);
  9875. /* since everything's happy, start the service_task timer */
  9876. mod_timer(&pf->service_timer,
  9877. round_jiffies(jiffies + pf->service_timer_period));
  9878. /* add this PF to client device list and launch a client service task */
  9879. if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
  9880. err = i40e_lan_add_device(pf);
  9881. if (err)
  9882. dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
  9883. err);
  9884. }
  9885. #define PCI_SPEED_SIZE 8
  9886. #define PCI_WIDTH_SIZE 8
  9887. /* Devices on the IOSF bus do not have this information
  9888. * and will report PCI Gen 1 x 1 by default so don't bother
  9889. * checking them.
  9890. */
  9891. if (!(pf->flags & I40E_FLAG_NO_PCI_LINK_CHECK)) {
  9892. char speed[PCI_SPEED_SIZE] = "Unknown";
  9893. char width[PCI_WIDTH_SIZE] = "Unknown";
  9894. /* Get the negotiated link width and speed from PCI config
  9895. * space
  9896. */
  9897. pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
  9898. &link_status);
  9899. i40e_set_pci_config_data(hw, link_status);
  9900. switch (hw->bus.speed) {
  9901. case i40e_bus_speed_8000:
  9902. strncpy(speed, "8.0", PCI_SPEED_SIZE); break;
  9903. case i40e_bus_speed_5000:
  9904. strncpy(speed, "5.0", PCI_SPEED_SIZE); break;
  9905. case i40e_bus_speed_2500:
  9906. strncpy(speed, "2.5", PCI_SPEED_SIZE); break;
  9907. default:
  9908. break;
  9909. }
  9910. switch (hw->bus.width) {
  9911. case i40e_bus_width_pcie_x8:
  9912. strncpy(width, "8", PCI_WIDTH_SIZE); break;
  9913. case i40e_bus_width_pcie_x4:
  9914. strncpy(width, "4", PCI_WIDTH_SIZE); break;
  9915. case i40e_bus_width_pcie_x2:
  9916. strncpy(width, "2", PCI_WIDTH_SIZE); break;
  9917. case i40e_bus_width_pcie_x1:
  9918. strncpy(width, "1", PCI_WIDTH_SIZE); break;
  9919. default:
  9920. break;
  9921. }
  9922. dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
  9923. speed, width);
  9924. if (hw->bus.width < i40e_bus_width_pcie_x8 ||
  9925. hw->bus.speed < i40e_bus_speed_8000) {
  9926. dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
  9927. dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
  9928. }
  9929. }
  9930. /* get the requested speeds from the fw */
  9931. err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
  9932. if (err)
  9933. dev_dbg(&pf->pdev->dev, "get requested speeds ret = %s last_status = %s\n",
  9934. i40e_stat_str(&pf->hw, err),
  9935. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  9936. pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
  9937. /* get the supported phy types from the fw */
  9938. err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
  9939. if (err)
  9940. dev_dbg(&pf->pdev->dev, "get supported phy types ret = %s last_status = %s\n",
  9941. i40e_stat_str(&pf->hw, err),
  9942. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  9943. /* Add a filter to drop all Flow control frames from any VSI from being
  9944. * transmitted. By doing so we stop a malicious VF from sending out
  9945. * PAUSE or PFC frames and potentially controlling traffic for other
  9946. * PF/VF VSIs.
  9947. * The FW can still send Flow control frames if enabled.
  9948. */
  9949. i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
  9950. pf->main_vsi_seid);
  9951. if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
  9952. (pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
  9953. pf->flags |= I40E_FLAG_PHY_CONTROLS_LEDS;
  9954. if (pf->hw.device_id == I40E_DEV_ID_SFP_I_X722)
  9955. pf->flags |= I40E_FLAG_HAVE_CRT_RETIMER;
  9956. /* print a string summarizing features */
  9957. i40e_print_features(pf);
  9958. return 0;
  9959. /* Unwind what we've done if something failed in the setup */
  9960. err_vsis:
  9961. set_bit(__I40E_DOWN, &pf->state);
  9962. i40e_clear_interrupt_scheme(pf);
  9963. kfree(pf->vsi);
  9964. err_switch_setup:
  9965. i40e_reset_interrupt_capability(pf);
  9966. del_timer_sync(&pf->service_timer);
  9967. err_mac_addr:
  9968. err_configure_lan_hmc:
  9969. (void)i40e_shutdown_lan_hmc(hw);
  9970. err_init_lan_hmc:
  9971. kfree(pf->qp_pile);
  9972. err_sw_init:
  9973. err_adminq_setup:
  9974. err_pf_reset:
  9975. iounmap(hw->hw_addr);
  9976. err_ioremap:
  9977. kfree(pf);
  9978. err_pf_alloc:
  9979. pci_disable_pcie_error_reporting(pdev);
  9980. pci_release_mem_regions(pdev);
  9981. err_pci_reg:
  9982. err_dma:
  9983. pci_disable_device(pdev);
  9984. return err;
  9985. }
  9986. /**
  9987. * i40e_remove - Device removal routine
  9988. * @pdev: PCI device information struct
  9989. *
  9990. * i40e_remove is called by the PCI subsystem to alert the driver
  9991. * that is should release a PCI device. This could be caused by a
  9992. * Hot-Plug event, or because the driver is going to be removed from
  9993. * memory.
  9994. **/
  9995. static void i40e_remove(struct pci_dev *pdev)
  9996. {
  9997. struct i40e_pf *pf = pci_get_drvdata(pdev);
  9998. struct i40e_hw *hw = &pf->hw;
  9999. i40e_status ret_code;
  10000. int i;
  10001. i40e_dbg_pf_exit(pf);
  10002. i40e_ptp_stop(pf);
  10003. /* Disable RSS in hw */
  10004. i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
  10005. i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
  10006. /* no more scheduling of any task */
  10007. set_bit(__I40E_SUSPENDED, &pf->state);
  10008. set_bit(__I40E_DOWN, &pf->state);
  10009. if (pf->service_timer.data)
  10010. del_timer_sync(&pf->service_timer);
  10011. if (pf->service_task.func)
  10012. cancel_work_sync(&pf->service_task);
  10013. /* Client close must be called explicitly here because the timer
  10014. * has been stopped.
  10015. */
  10016. i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
  10017. if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
  10018. i40e_free_vfs(pf);
  10019. pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
  10020. }
  10021. i40e_fdir_teardown(pf);
  10022. /* If there is a switch structure or any orphans, remove them.
  10023. * This will leave only the PF's VSI remaining.
  10024. */
  10025. for (i = 0; i < I40E_MAX_VEB; i++) {
  10026. if (!pf->veb[i])
  10027. continue;
  10028. if (pf->veb[i]->uplink_seid == pf->mac_seid ||
  10029. pf->veb[i]->uplink_seid == 0)
  10030. i40e_switch_branch_release(pf->veb[i]);
  10031. }
  10032. /* Now we can shutdown the PF's VSI, just before we kill
  10033. * adminq and hmc.
  10034. */
  10035. if (pf->vsi[pf->lan_vsi])
  10036. i40e_vsi_release(pf->vsi[pf->lan_vsi]);
  10037. /* remove attached clients */
  10038. if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
  10039. ret_code = i40e_lan_del_device(pf);
  10040. if (ret_code)
  10041. dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
  10042. ret_code);
  10043. }
  10044. /* shutdown and destroy the HMC */
  10045. if (hw->hmc.hmc_obj) {
  10046. ret_code = i40e_shutdown_lan_hmc(hw);
  10047. if (ret_code)
  10048. dev_warn(&pdev->dev,
  10049. "Failed to destroy the HMC resources: %d\n",
  10050. ret_code);
  10051. }
  10052. /* shutdown the adminq */
  10053. i40e_shutdown_adminq(hw);
  10054. /* destroy the locks only once, here */
  10055. mutex_destroy(&hw->aq.arq_mutex);
  10056. mutex_destroy(&hw->aq.asq_mutex);
  10057. /* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
  10058. i40e_clear_interrupt_scheme(pf);
  10059. for (i = 0; i < pf->num_alloc_vsi; i++) {
  10060. if (pf->vsi[i]) {
  10061. i40e_vsi_clear_rings(pf->vsi[i]);
  10062. i40e_vsi_clear(pf->vsi[i]);
  10063. pf->vsi[i] = NULL;
  10064. }
  10065. }
  10066. for (i = 0; i < I40E_MAX_VEB; i++) {
  10067. kfree(pf->veb[i]);
  10068. pf->veb[i] = NULL;
  10069. }
  10070. kfree(pf->qp_pile);
  10071. kfree(pf->vsi);
  10072. iounmap(hw->hw_addr);
  10073. kfree(pf);
  10074. pci_release_mem_regions(pdev);
  10075. pci_disable_pcie_error_reporting(pdev);
  10076. pci_disable_device(pdev);
  10077. }
  10078. /**
  10079. * i40e_pci_error_detected - warning that something funky happened in PCI land
  10080. * @pdev: PCI device information struct
  10081. *
  10082. * Called to warn that something happened and the error handling steps
  10083. * are in progress. Allows the driver to quiesce things, be ready for
  10084. * remediation.
  10085. **/
  10086. static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
  10087. enum pci_channel_state error)
  10088. {
  10089. struct i40e_pf *pf = pci_get_drvdata(pdev);
  10090. dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
  10091. if (!pf) {
  10092. dev_info(&pdev->dev,
  10093. "Cannot recover - error happened during device probe\n");
  10094. return PCI_ERS_RESULT_DISCONNECT;
  10095. }
  10096. /* shutdown all operations */
  10097. if (!test_bit(__I40E_SUSPENDED, &pf->state)) {
  10098. rtnl_lock();
  10099. i40e_prep_for_reset(pf, true);
  10100. rtnl_unlock();
  10101. }
  10102. /* Request a slot reset */
  10103. return PCI_ERS_RESULT_NEED_RESET;
  10104. }
  10105. /**
  10106. * i40e_pci_error_slot_reset - a PCI slot reset just happened
  10107. * @pdev: PCI device information struct
  10108. *
  10109. * Called to find if the driver can work with the device now that
  10110. * the pci slot has been reset. If a basic connection seems good
  10111. * (registers are readable and have sane content) then return a
  10112. * happy little PCI_ERS_RESULT_xxx.
  10113. **/
  10114. static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
  10115. {
  10116. struct i40e_pf *pf = pci_get_drvdata(pdev);
  10117. pci_ers_result_t result;
  10118. int err;
  10119. u32 reg;
  10120. dev_dbg(&pdev->dev, "%s\n", __func__);
  10121. if (pci_enable_device_mem(pdev)) {
  10122. dev_info(&pdev->dev,
  10123. "Cannot re-enable PCI device after reset.\n");
  10124. result = PCI_ERS_RESULT_DISCONNECT;
  10125. } else {
  10126. pci_set_master(pdev);
  10127. pci_restore_state(pdev);
  10128. pci_save_state(pdev);
  10129. pci_wake_from_d3(pdev, false);
  10130. reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
  10131. if (reg == 0)
  10132. result = PCI_ERS_RESULT_RECOVERED;
  10133. else
  10134. result = PCI_ERS_RESULT_DISCONNECT;
  10135. }
  10136. err = pci_cleanup_aer_uncorrect_error_status(pdev);
  10137. if (err) {
  10138. dev_info(&pdev->dev,
  10139. "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
  10140. err);
  10141. /* non-fatal, continue */
  10142. }
  10143. return result;
  10144. }
  10145. /**
  10146. * i40e_pci_error_resume - restart operations after PCI error recovery
  10147. * @pdev: PCI device information struct
  10148. *
  10149. * Called to allow the driver to bring things back up after PCI error
  10150. * and/or reset recovery has finished.
  10151. **/
  10152. static void i40e_pci_error_resume(struct pci_dev *pdev)
  10153. {
  10154. struct i40e_pf *pf = pci_get_drvdata(pdev);
  10155. dev_dbg(&pdev->dev, "%s\n", __func__);
  10156. if (test_bit(__I40E_SUSPENDED, &pf->state))
  10157. return;
  10158. rtnl_lock();
  10159. i40e_handle_reset_warning(pf, true);
  10160. rtnl_unlock();
  10161. }
  10162. /**
  10163. * i40e_enable_mc_magic_wake - enable multicast magic packet wake up
  10164. * using the mac_address_write admin q function
  10165. * @pf: pointer to i40e_pf struct
  10166. **/
  10167. static void i40e_enable_mc_magic_wake(struct i40e_pf *pf)
  10168. {
  10169. struct i40e_hw *hw = &pf->hw;
  10170. i40e_status ret;
  10171. u8 mac_addr[6];
  10172. u16 flags = 0;
  10173. /* Get current MAC address in case it's an LAA */
  10174. if (pf->vsi[pf->lan_vsi] && pf->vsi[pf->lan_vsi]->netdev) {
  10175. ether_addr_copy(mac_addr,
  10176. pf->vsi[pf->lan_vsi]->netdev->dev_addr);
  10177. } else {
  10178. dev_err(&pf->pdev->dev,
  10179. "Failed to retrieve MAC address; using default\n");
  10180. ether_addr_copy(mac_addr, hw->mac.addr);
  10181. }
  10182. /* The FW expects the mac address write cmd to first be called with
  10183. * one of these flags before calling it again with the multicast
  10184. * enable flags.
  10185. */
  10186. flags = I40E_AQC_WRITE_TYPE_LAA_WOL;
  10187. if (hw->func_caps.flex10_enable && hw->partition_id != 1)
  10188. flags = I40E_AQC_WRITE_TYPE_LAA_ONLY;
  10189. ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
  10190. if (ret) {
  10191. dev_err(&pf->pdev->dev,
  10192. "Failed to update MAC address registers; cannot enable Multicast Magic packet wake up");
  10193. return;
  10194. }
  10195. flags = I40E_AQC_MC_MAG_EN
  10196. | I40E_AQC_WOL_PRESERVE_ON_PFR
  10197. | I40E_AQC_WRITE_TYPE_UPDATE_MC_MAG;
  10198. ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
  10199. if (ret)
  10200. dev_err(&pf->pdev->dev,
  10201. "Failed to enable Multicast Magic Packet wake up\n");
  10202. }
  10203. /**
  10204. * i40e_shutdown - PCI callback for shutting down
  10205. * @pdev: PCI device information struct
  10206. **/
  10207. static void i40e_shutdown(struct pci_dev *pdev)
  10208. {
  10209. struct i40e_pf *pf = pci_get_drvdata(pdev);
  10210. struct i40e_hw *hw = &pf->hw;
  10211. set_bit(__I40E_SUSPENDED, &pf->state);
  10212. set_bit(__I40E_DOWN, &pf->state);
  10213. rtnl_lock();
  10214. i40e_prep_for_reset(pf, true);
  10215. rtnl_unlock();
  10216. wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
  10217. wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
  10218. del_timer_sync(&pf->service_timer);
  10219. cancel_work_sync(&pf->service_task);
  10220. i40e_fdir_teardown(pf);
  10221. /* Client close must be called explicitly here because the timer
  10222. * has been stopped.
  10223. */
  10224. i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
  10225. if (pf->wol_en && (pf->flags & I40E_FLAG_WOL_MC_MAGIC_PKT_WAKE))
  10226. i40e_enable_mc_magic_wake(pf);
  10227. rtnl_lock();
  10228. i40e_prep_for_reset(pf, true);
  10229. rtnl_unlock();
  10230. wr32(hw, I40E_PFPM_APM,
  10231. (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
  10232. wr32(hw, I40E_PFPM_WUFC,
  10233. (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
  10234. i40e_clear_interrupt_scheme(pf);
  10235. if (system_state == SYSTEM_POWER_OFF) {
  10236. pci_wake_from_d3(pdev, pf->wol_en);
  10237. pci_set_power_state(pdev, PCI_D3hot);
  10238. }
  10239. }
  10240. #ifdef CONFIG_PM
  10241. /**
  10242. * i40e_suspend - PCI callback for moving to D3
  10243. * @pdev: PCI device information struct
  10244. **/
  10245. static int i40e_suspend(struct pci_dev *pdev, pm_message_t state)
  10246. {
  10247. struct i40e_pf *pf = pci_get_drvdata(pdev);
  10248. struct i40e_hw *hw = &pf->hw;
  10249. int retval = 0;
  10250. set_bit(__I40E_SUSPENDED, &pf->state);
  10251. set_bit(__I40E_DOWN, &pf->state);
  10252. if (pf->wol_en && (pf->flags & I40E_FLAG_WOL_MC_MAGIC_PKT_WAKE))
  10253. i40e_enable_mc_magic_wake(pf);
  10254. rtnl_lock();
  10255. i40e_prep_for_reset(pf, true);
  10256. rtnl_unlock();
  10257. wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
  10258. wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
  10259. i40e_stop_misc_vector(pf);
  10260. retval = pci_save_state(pdev);
  10261. if (retval)
  10262. return retval;
  10263. pci_wake_from_d3(pdev, pf->wol_en);
  10264. pci_set_power_state(pdev, PCI_D3hot);
  10265. return retval;
  10266. }
  10267. /**
  10268. * i40e_resume - PCI callback for waking up from D3
  10269. * @pdev: PCI device information struct
  10270. **/
  10271. static int i40e_resume(struct pci_dev *pdev)
  10272. {
  10273. struct i40e_pf *pf = pci_get_drvdata(pdev);
  10274. u32 err;
  10275. pci_set_power_state(pdev, PCI_D0);
  10276. pci_restore_state(pdev);
  10277. /* pci_restore_state() clears dev->state_saves, so
  10278. * call pci_save_state() again to restore it.
  10279. */
  10280. pci_save_state(pdev);
  10281. err = pci_enable_device_mem(pdev);
  10282. if (err) {
  10283. dev_err(&pdev->dev, "Cannot enable PCI device from suspend\n");
  10284. return err;
  10285. }
  10286. pci_set_master(pdev);
  10287. /* no wakeup events while running */
  10288. pci_wake_from_d3(pdev, false);
  10289. /* handling the reset will rebuild the device state */
  10290. if (test_and_clear_bit(__I40E_SUSPENDED, &pf->state)) {
  10291. clear_bit(__I40E_DOWN, &pf->state);
  10292. rtnl_lock();
  10293. i40e_reset_and_rebuild(pf, false, true);
  10294. rtnl_unlock();
  10295. }
  10296. return 0;
  10297. }
  10298. #endif
  10299. static const struct pci_error_handlers i40e_err_handler = {
  10300. .error_detected = i40e_pci_error_detected,
  10301. .slot_reset = i40e_pci_error_slot_reset,
  10302. .resume = i40e_pci_error_resume,
  10303. };
  10304. static struct pci_driver i40e_driver = {
  10305. .name = i40e_driver_name,
  10306. .id_table = i40e_pci_tbl,
  10307. .probe = i40e_probe,
  10308. .remove = i40e_remove,
  10309. #ifdef CONFIG_PM
  10310. .suspend = i40e_suspend,
  10311. .resume = i40e_resume,
  10312. #endif
  10313. .shutdown = i40e_shutdown,
  10314. .err_handler = &i40e_err_handler,
  10315. .sriov_configure = i40e_pci_sriov_configure,
  10316. };
  10317. /**
  10318. * i40e_init_module - Driver registration routine
  10319. *
  10320. * i40e_init_module is the first routine called when the driver is
  10321. * loaded. All it does is register with the PCI subsystem.
  10322. **/
  10323. static int __init i40e_init_module(void)
  10324. {
  10325. pr_info("%s: %s - version %s\n", i40e_driver_name,
  10326. i40e_driver_string, i40e_driver_version_str);
  10327. pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
  10328. /* we will see if single thread per module is enough for now,
  10329. * it can't be any worse than using the system workqueue which
  10330. * was already single threaded
  10331. */
  10332. i40e_wq = alloc_workqueue("%s", WQ_UNBOUND | WQ_MEM_RECLAIM, 1,
  10333. i40e_driver_name);
  10334. if (!i40e_wq) {
  10335. pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
  10336. return -ENOMEM;
  10337. }
  10338. i40e_dbg_init();
  10339. return pci_register_driver(&i40e_driver);
  10340. }
  10341. module_init(i40e_init_module);
  10342. /**
  10343. * i40e_exit_module - Driver exit cleanup routine
  10344. *
  10345. * i40e_exit_module is called just before the driver is removed
  10346. * from memory.
  10347. **/
  10348. static void __exit i40e_exit_module(void)
  10349. {
  10350. pci_unregister_driver(&i40e_driver);
  10351. destroy_workqueue(i40e_wq);
  10352. i40e_dbg_exit();
  10353. }
  10354. module_exit(i40e_exit_module);