i40e_main.c 264 KB

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