i40e_main.c 304 KB

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