i40e_main.c 304 KB

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