i40e_main.c 319 KB

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