i40e_main.c 213 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959
  1. /*******************************************************************************
  2. *
  3. * Intel Ethernet Controller XL710 Family Linux Driver
  4. * Copyright(c) 2013 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 with
  16. * this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  18. *
  19. * The full GNU General Public License is included in this distribution in
  20. * the file called "COPYING".
  21. *
  22. * Contact Information:
  23. * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
  24. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  25. *
  26. ******************************************************************************/
  27. /* Local includes */
  28. #include "i40e.h"
  29. const char i40e_driver_name[] = "i40e";
  30. static const char i40e_driver_string[] =
  31. "Intel(R) Ethernet Connection XL710 Network Driver";
  32. #define DRV_KERN "-k"
  33. #define DRV_VERSION_MAJOR 0
  34. #define DRV_VERSION_MINOR 3
  35. #define DRV_VERSION_BUILD 14
  36. #define DRV_VERSION __stringify(DRV_VERSION_MAJOR) "." \
  37. __stringify(DRV_VERSION_MINOR) "." \
  38. __stringify(DRV_VERSION_BUILD) DRV_KERN
  39. const char i40e_driver_version_str[] = DRV_VERSION;
  40. static const char i40e_copyright[] = "Copyright (c) 2013 Intel Corporation.";
  41. /* a bit of forward declarations */
  42. static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi);
  43. static void i40e_handle_reset_warning(struct i40e_pf *pf);
  44. static int i40e_add_vsi(struct i40e_vsi *vsi);
  45. static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi);
  46. static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit);
  47. static int i40e_setup_misc_vector(struct i40e_pf *pf);
  48. static void i40e_determine_queue_usage(struct i40e_pf *pf);
  49. static int i40e_setup_pf_filter_control(struct i40e_pf *pf);
  50. /* i40e_pci_tbl - PCI Device ID Table
  51. *
  52. * Last entry must be all 0s
  53. *
  54. * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
  55. * Class, Class Mask, private data (not used) }
  56. */
  57. static DEFINE_PCI_DEVICE_TABLE(i40e_pci_tbl) = {
  58. {PCI_VDEVICE(INTEL, I40E_SFP_XL710_DEVICE_ID), 0},
  59. {PCI_VDEVICE(INTEL, I40E_SFP_X710_DEVICE_ID), 0},
  60. {PCI_VDEVICE(INTEL, I40E_QEMU_DEVICE_ID), 0},
  61. {PCI_VDEVICE(INTEL, I40E_KX_A_DEVICE_ID), 0},
  62. {PCI_VDEVICE(INTEL, I40E_KX_B_DEVICE_ID), 0},
  63. {PCI_VDEVICE(INTEL, I40E_KX_C_DEVICE_ID), 0},
  64. {PCI_VDEVICE(INTEL, I40E_KX_D_DEVICE_ID), 0},
  65. {PCI_VDEVICE(INTEL, I40E_QSFP_A_DEVICE_ID), 0},
  66. {PCI_VDEVICE(INTEL, I40E_QSFP_B_DEVICE_ID), 0},
  67. {PCI_VDEVICE(INTEL, I40E_QSFP_C_DEVICE_ID), 0},
  68. /* required last entry */
  69. {0, }
  70. };
  71. MODULE_DEVICE_TABLE(pci, i40e_pci_tbl);
  72. #define I40E_MAX_VF_COUNT 128
  73. static int debug = -1;
  74. module_param(debug, int, 0);
  75. MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
  76. MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
  77. MODULE_DESCRIPTION("Intel(R) Ethernet Connection XL710 Network Driver");
  78. MODULE_LICENSE("GPL");
  79. MODULE_VERSION(DRV_VERSION);
  80. /**
  81. * i40e_allocate_dma_mem_d - OS specific memory alloc for shared code
  82. * @hw: pointer to the HW structure
  83. * @mem: ptr to mem struct to fill out
  84. * @size: size of memory requested
  85. * @alignment: what to align the allocation to
  86. **/
  87. int i40e_allocate_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem,
  88. u64 size, u32 alignment)
  89. {
  90. struct i40e_pf *pf = (struct i40e_pf *)hw->back;
  91. mem->size = ALIGN(size, alignment);
  92. mem->va = dma_zalloc_coherent(&pf->pdev->dev, mem->size,
  93. &mem->pa, GFP_KERNEL);
  94. if (!mem->va)
  95. return -ENOMEM;
  96. return 0;
  97. }
  98. /**
  99. * i40e_free_dma_mem_d - OS specific memory free for shared code
  100. * @hw: pointer to the HW structure
  101. * @mem: ptr to mem struct to free
  102. **/
  103. int i40e_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
  104. {
  105. struct i40e_pf *pf = (struct i40e_pf *)hw->back;
  106. dma_free_coherent(&pf->pdev->dev, mem->size, mem->va, mem->pa);
  107. mem->va = NULL;
  108. mem->pa = 0;
  109. mem->size = 0;
  110. return 0;
  111. }
  112. /**
  113. * i40e_allocate_virt_mem_d - OS specific memory alloc for shared code
  114. * @hw: pointer to the HW structure
  115. * @mem: ptr to mem struct to fill out
  116. * @size: size of memory requested
  117. **/
  118. int i40e_allocate_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem,
  119. u32 size)
  120. {
  121. mem->size = size;
  122. mem->va = kzalloc(size, GFP_KERNEL);
  123. if (!mem->va)
  124. return -ENOMEM;
  125. return 0;
  126. }
  127. /**
  128. * i40e_free_virt_mem_d - OS specific memory free for shared code
  129. * @hw: pointer to the HW structure
  130. * @mem: ptr to mem struct to free
  131. **/
  132. int i40e_free_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem)
  133. {
  134. /* it's ok to kfree a NULL pointer */
  135. kfree(mem->va);
  136. mem->va = NULL;
  137. mem->size = 0;
  138. return 0;
  139. }
  140. /**
  141. * i40e_get_lump - find a lump of free generic resource
  142. * @pf: board private structure
  143. * @pile: the pile of resource to search
  144. * @needed: the number of items needed
  145. * @id: an owner id to stick on the items assigned
  146. *
  147. * Returns the base item index of the lump, or negative for error
  148. *
  149. * The search_hint trick and lack of advanced fit-finding only work
  150. * because we're highly likely to have all the same size lump requests.
  151. * Linear search time and any fragmentation should be minimal.
  152. **/
  153. static int i40e_get_lump(struct i40e_pf *pf, struct i40e_lump_tracking *pile,
  154. u16 needed, u16 id)
  155. {
  156. int ret = -ENOMEM;
  157. int i, j;
  158. if (!pile || needed == 0 || id >= I40E_PILE_VALID_BIT) {
  159. dev_info(&pf->pdev->dev,
  160. "param err: pile=%p needed=%d id=0x%04x\n",
  161. pile, needed, id);
  162. return -EINVAL;
  163. }
  164. /* start the linear search with an imperfect hint */
  165. i = pile->search_hint;
  166. while (i < pile->num_entries) {
  167. /* skip already allocated entries */
  168. if (pile->list[i] & I40E_PILE_VALID_BIT) {
  169. i++;
  170. continue;
  171. }
  172. /* do we have enough in this lump? */
  173. for (j = 0; (j < needed) && ((i+j) < pile->num_entries); j++) {
  174. if (pile->list[i+j] & I40E_PILE_VALID_BIT)
  175. break;
  176. }
  177. if (j == needed) {
  178. /* there was enough, so assign it to the requestor */
  179. for (j = 0; j < needed; j++)
  180. pile->list[i+j] = id | I40E_PILE_VALID_BIT;
  181. ret = i;
  182. pile->search_hint = i + j;
  183. break;
  184. } else {
  185. /* not enough, so skip over it and continue looking */
  186. i += j;
  187. }
  188. }
  189. return ret;
  190. }
  191. /**
  192. * i40e_put_lump - return a lump of generic resource
  193. * @pile: the pile of resource to search
  194. * @index: the base item index
  195. * @id: the owner id of the items assigned
  196. *
  197. * Returns the count of items in the lump
  198. **/
  199. static int i40e_put_lump(struct i40e_lump_tracking *pile, u16 index, u16 id)
  200. {
  201. int valid_id = (id | I40E_PILE_VALID_BIT);
  202. int count = 0;
  203. int i;
  204. if (!pile || index >= pile->num_entries)
  205. return -EINVAL;
  206. for (i = index;
  207. i < pile->num_entries && pile->list[i] == valid_id;
  208. i++) {
  209. pile->list[i] = 0;
  210. count++;
  211. }
  212. if (count && index < pile->search_hint)
  213. pile->search_hint = index;
  214. return count;
  215. }
  216. /**
  217. * i40e_service_event_schedule - Schedule the service task to wake up
  218. * @pf: board private structure
  219. *
  220. * If not already scheduled, this puts the task into the work queue
  221. **/
  222. static void i40e_service_event_schedule(struct i40e_pf *pf)
  223. {
  224. if (!test_bit(__I40E_DOWN, &pf->state) &&
  225. !test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state) &&
  226. !test_and_set_bit(__I40E_SERVICE_SCHED, &pf->state))
  227. schedule_work(&pf->service_task);
  228. }
  229. /**
  230. * i40e_tx_timeout - Respond to a Tx Hang
  231. * @netdev: network interface device structure
  232. *
  233. * If any port has noticed a Tx timeout, it is likely that the whole
  234. * device is munged, not just the one netdev port, so go for the full
  235. * reset.
  236. **/
  237. static void i40e_tx_timeout(struct net_device *netdev)
  238. {
  239. struct i40e_netdev_priv *np = netdev_priv(netdev);
  240. struct i40e_vsi *vsi = np->vsi;
  241. struct i40e_pf *pf = vsi->back;
  242. pf->tx_timeout_count++;
  243. if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ*20)))
  244. pf->tx_timeout_recovery_level = 0;
  245. pf->tx_timeout_last_recovery = jiffies;
  246. netdev_info(netdev, "tx_timeout recovery level %d\n",
  247. pf->tx_timeout_recovery_level);
  248. switch (pf->tx_timeout_recovery_level) {
  249. case 0:
  250. /* disable and re-enable queues for the VSI */
  251. if (in_interrupt()) {
  252. set_bit(__I40E_REINIT_REQUESTED, &pf->state);
  253. set_bit(__I40E_REINIT_REQUESTED, &vsi->state);
  254. } else {
  255. i40e_vsi_reinit_locked(vsi);
  256. }
  257. break;
  258. case 1:
  259. set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
  260. break;
  261. case 2:
  262. set_bit(__I40E_CORE_RESET_REQUESTED, &pf->state);
  263. break;
  264. case 3:
  265. set_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state);
  266. break;
  267. default:
  268. netdev_err(netdev, "tx_timeout recovery unsuccessful\n");
  269. i40e_down(vsi);
  270. break;
  271. }
  272. i40e_service_event_schedule(pf);
  273. pf->tx_timeout_recovery_level++;
  274. }
  275. /**
  276. * i40e_release_rx_desc - Store the new tail and head values
  277. * @rx_ring: ring to bump
  278. * @val: new head index
  279. **/
  280. static inline void i40e_release_rx_desc(struct i40e_ring *rx_ring, u32 val)
  281. {
  282. rx_ring->next_to_use = val;
  283. /* Force memory writes to complete before letting h/w
  284. * know there are new descriptors to fetch. (Only
  285. * applicable for weak-ordered memory model archs,
  286. * such as IA-64).
  287. */
  288. wmb();
  289. writel(val, rx_ring->tail);
  290. }
  291. /**
  292. * i40e_get_vsi_stats_struct - Get System Network Statistics
  293. * @vsi: the VSI we care about
  294. *
  295. * Returns the address of the device statistics structure.
  296. * The statistics are actually updated from the service task.
  297. **/
  298. struct rtnl_link_stats64 *i40e_get_vsi_stats_struct(struct i40e_vsi *vsi)
  299. {
  300. return &vsi->net_stats;
  301. }
  302. /**
  303. * i40e_get_netdev_stats_struct - Get statistics for netdev interface
  304. * @netdev: network interface device structure
  305. *
  306. * Returns the address of the device statistics structure.
  307. * The statistics are actually updated from the service task.
  308. **/
  309. static struct rtnl_link_stats64 *i40e_get_netdev_stats_struct(
  310. struct net_device *netdev,
  311. struct rtnl_link_stats64 *stats)
  312. {
  313. struct i40e_netdev_priv *np = netdev_priv(netdev);
  314. struct i40e_vsi *vsi = np->vsi;
  315. struct rtnl_link_stats64 *vsi_stats = i40e_get_vsi_stats_struct(vsi);
  316. int i;
  317. if (test_bit(__I40E_DOWN, &vsi->state))
  318. return stats;
  319. if (!vsi->tx_rings)
  320. return stats;
  321. rcu_read_lock();
  322. for (i = 0; i < vsi->num_queue_pairs; i++) {
  323. struct i40e_ring *tx_ring, *rx_ring;
  324. u64 bytes, packets;
  325. unsigned int start;
  326. tx_ring = ACCESS_ONCE(vsi->tx_rings[i]);
  327. if (!tx_ring)
  328. continue;
  329. do {
  330. start = u64_stats_fetch_begin_bh(&tx_ring->syncp);
  331. packets = tx_ring->stats.packets;
  332. bytes = tx_ring->stats.bytes;
  333. } while (u64_stats_fetch_retry_bh(&tx_ring->syncp, start));
  334. stats->tx_packets += packets;
  335. stats->tx_bytes += bytes;
  336. rx_ring = &tx_ring[1];
  337. do {
  338. start = u64_stats_fetch_begin_bh(&rx_ring->syncp);
  339. packets = rx_ring->stats.packets;
  340. bytes = rx_ring->stats.bytes;
  341. } while (u64_stats_fetch_retry_bh(&rx_ring->syncp, start));
  342. stats->rx_packets += packets;
  343. stats->rx_bytes += bytes;
  344. }
  345. rcu_read_unlock();
  346. /* following stats updated by ixgbe_watchdog_task() */
  347. stats->multicast = vsi_stats->multicast;
  348. stats->tx_errors = vsi_stats->tx_errors;
  349. stats->tx_dropped = vsi_stats->tx_dropped;
  350. stats->rx_errors = vsi_stats->rx_errors;
  351. stats->rx_crc_errors = vsi_stats->rx_crc_errors;
  352. stats->rx_length_errors = vsi_stats->rx_length_errors;
  353. return stats;
  354. }
  355. /**
  356. * i40e_vsi_reset_stats - Resets all stats of the given vsi
  357. * @vsi: the VSI to have its stats reset
  358. **/
  359. void i40e_vsi_reset_stats(struct i40e_vsi *vsi)
  360. {
  361. struct rtnl_link_stats64 *ns;
  362. int i;
  363. if (!vsi)
  364. return;
  365. ns = i40e_get_vsi_stats_struct(vsi);
  366. memset(ns, 0, sizeof(*ns));
  367. memset(&vsi->net_stats_offsets, 0, sizeof(vsi->net_stats_offsets));
  368. memset(&vsi->eth_stats, 0, sizeof(vsi->eth_stats));
  369. memset(&vsi->eth_stats_offsets, 0, sizeof(vsi->eth_stats_offsets));
  370. if (vsi->rx_rings)
  371. for (i = 0; i < vsi->num_queue_pairs; i++) {
  372. memset(&vsi->rx_rings[i]->stats, 0 ,
  373. sizeof(vsi->rx_rings[i]->stats));
  374. memset(&vsi->rx_rings[i]->rx_stats, 0 ,
  375. sizeof(vsi->rx_rings[i]->rx_stats));
  376. memset(&vsi->tx_rings[i]->stats, 0 ,
  377. sizeof(vsi->tx_rings[i]->stats));
  378. memset(&vsi->tx_rings[i]->tx_stats, 0,
  379. sizeof(vsi->tx_rings[i]->tx_stats));
  380. }
  381. vsi->stat_offsets_loaded = false;
  382. }
  383. /**
  384. * i40e_pf_reset_stats - Reset all of the stats for the given pf
  385. * @pf: the PF to be reset
  386. **/
  387. void i40e_pf_reset_stats(struct i40e_pf *pf)
  388. {
  389. memset(&pf->stats, 0, sizeof(pf->stats));
  390. memset(&pf->stats_offsets, 0, sizeof(pf->stats_offsets));
  391. pf->stat_offsets_loaded = false;
  392. }
  393. /**
  394. * i40e_stat_update48 - read and update a 48 bit stat from the chip
  395. * @hw: ptr to the hardware info
  396. * @hireg: the high 32 bit reg to read
  397. * @loreg: the low 32 bit reg to read
  398. * @offset_loaded: has the initial offset been loaded yet
  399. * @offset: ptr to current offset value
  400. * @stat: ptr to the stat
  401. *
  402. * Since the device stats are not reset at PFReset, they likely will not
  403. * be zeroed when the driver starts. We'll save the first values read
  404. * and use them as offsets to be subtracted from the raw values in order
  405. * to report stats that count from zero. In the process, we also manage
  406. * the potential roll-over.
  407. **/
  408. static void i40e_stat_update48(struct i40e_hw *hw, u32 hireg, u32 loreg,
  409. bool offset_loaded, u64 *offset, u64 *stat)
  410. {
  411. u64 new_data;
  412. if (hw->device_id == I40E_QEMU_DEVICE_ID) {
  413. new_data = rd32(hw, loreg);
  414. new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
  415. } else {
  416. new_data = rd64(hw, loreg);
  417. }
  418. if (!offset_loaded)
  419. *offset = new_data;
  420. if (likely(new_data >= *offset))
  421. *stat = new_data - *offset;
  422. else
  423. *stat = (new_data + ((u64)1 << 48)) - *offset;
  424. *stat &= 0xFFFFFFFFFFFFULL;
  425. }
  426. /**
  427. * i40e_stat_update32 - read and update a 32 bit stat from the chip
  428. * @hw: ptr to the hardware info
  429. * @reg: the hw reg to read
  430. * @offset_loaded: has the initial offset been loaded yet
  431. * @offset: ptr to current offset value
  432. * @stat: ptr to the stat
  433. **/
  434. static void i40e_stat_update32(struct i40e_hw *hw, u32 reg,
  435. bool offset_loaded, u64 *offset, u64 *stat)
  436. {
  437. u32 new_data;
  438. new_data = rd32(hw, reg);
  439. if (!offset_loaded)
  440. *offset = new_data;
  441. if (likely(new_data >= *offset))
  442. *stat = (u32)(new_data - *offset);
  443. else
  444. *stat = (u32)((new_data + ((u64)1 << 32)) - *offset);
  445. }
  446. /**
  447. * i40e_update_eth_stats - Update VSI-specific ethernet statistics counters.
  448. * @vsi: the VSI to be updated
  449. **/
  450. void i40e_update_eth_stats(struct i40e_vsi *vsi)
  451. {
  452. int stat_idx = le16_to_cpu(vsi->info.stat_counter_idx);
  453. struct i40e_pf *pf = vsi->back;
  454. struct i40e_hw *hw = &pf->hw;
  455. struct i40e_eth_stats *oes;
  456. struct i40e_eth_stats *es; /* device's eth stats */
  457. es = &vsi->eth_stats;
  458. oes = &vsi->eth_stats_offsets;
  459. /* Gather up the stats that the hw collects */
  460. i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
  461. vsi->stat_offsets_loaded,
  462. &oes->tx_errors, &es->tx_errors);
  463. i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx),
  464. vsi->stat_offsets_loaded,
  465. &oes->rx_discards, &es->rx_discards);
  466. i40e_stat_update48(hw, I40E_GLV_GORCH(stat_idx),
  467. I40E_GLV_GORCL(stat_idx),
  468. vsi->stat_offsets_loaded,
  469. &oes->rx_bytes, &es->rx_bytes);
  470. i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
  471. I40E_GLV_UPRCL(stat_idx),
  472. vsi->stat_offsets_loaded,
  473. &oes->rx_unicast, &es->rx_unicast);
  474. i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
  475. I40E_GLV_MPRCL(stat_idx),
  476. vsi->stat_offsets_loaded,
  477. &oes->rx_multicast, &es->rx_multicast);
  478. i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
  479. I40E_GLV_BPRCL(stat_idx),
  480. vsi->stat_offsets_loaded,
  481. &oes->rx_broadcast, &es->rx_broadcast);
  482. i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
  483. I40E_GLV_GOTCL(stat_idx),
  484. vsi->stat_offsets_loaded,
  485. &oes->tx_bytes, &es->tx_bytes);
  486. i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
  487. I40E_GLV_UPTCL(stat_idx),
  488. vsi->stat_offsets_loaded,
  489. &oes->tx_unicast, &es->tx_unicast);
  490. i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
  491. I40E_GLV_MPTCL(stat_idx),
  492. vsi->stat_offsets_loaded,
  493. &oes->tx_multicast, &es->tx_multicast);
  494. i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
  495. I40E_GLV_BPTCL(stat_idx),
  496. vsi->stat_offsets_loaded,
  497. &oes->tx_broadcast, &es->tx_broadcast);
  498. vsi->stat_offsets_loaded = true;
  499. }
  500. /**
  501. * i40e_update_veb_stats - Update Switch component statistics
  502. * @veb: the VEB being updated
  503. **/
  504. static void i40e_update_veb_stats(struct i40e_veb *veb)
  505. {
  506. struct i40e_pf *pf = veb->pf;
  507. struct i40e_hw *hw = &pf->hw;
  508. struct i40e_eth_stats *oes;
  509. struct i40e_eth_stats *es; /* device's eth stats */
  510. int idx = 0;
  511. idx = veb->stats_idx;
  512. es = &veb->stats;
  513. oes = &veb->stats_offsets;
  514. /* Gather up the stats that the hw collects */
  515. i40e_stat_update32(hw, I40E_GLSW_TDPC(idx),
  516. veb->stat_offsets_loaded,
  517. &oes->tx_discards, &es->tx_discards);
  518. if (hw->revision_id > 0)
  519. i40e_stat_update32(hw, I40E_GLSW_RUPP(idx),
  520. veb->stat_offsets_loaded,
  521. &oes->rx_unknown_protocol,
  522. &es->rx_unknown_protocol);
  523. i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx),
  524. veb->stat_offsets_loaded,
  525. &oes->rx_bytes, &es->rx_bytes);
  526. i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx),
  527. veb->stat_offsets_loaded,
  528. &oes->rx_unicast, &es->rx_unicast);
  529. i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx),
  530. veb->stat_offsets_loaded,
  531. &oes->rx_multicast, &es->rx_multicast);
  532. i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx),
  533. veb->stat_offsets_loaded,
  534. &oes->rx_broadcast, &es->rx_broadcast);
  535. i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx),
  536. veb->stat_offsets_loaded,
  537. &oes->tx_bytes, &es->tx_bytes);
  538. i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx),
  539. veb->stat_offsets_loaded,
  540. &oes->tx_unicast, &es->tx_unicast);
  541. i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx),
  542. veb->stat_offsets_loaded,
  543. &oes->tx_multicast, &es->tx_multicast);
  544. i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx),
  545. veb->stat_offsets_loaded,
  546. &oes->tx_broadcast, &es->tx_broadcast);
  547. veb->stat_offsets_loaded = true;
  548. }
  549. /**
  550. * i40e_update_link_xoff_rx - Update XOFF received in link flow control mode
  551. * @pf: the corresponding PF
  552. *
  553. * Update the Rx XOFF counter (PAUSE frames) in link flow control mode
  554. **/
  555. static void i40e_update_link_xoff_rx(struct i40e_pf *pf)
  556. {
  557. struct i40e_hw_port_stats *osd = &pf->stats_offsets;
  558. struct i40e_hw_port_stats *nsd = &pf->stats;
  559. struct i40e_hw *hw = &pf->hw;
  560. u64 xoff = 0;
  561. u16 i, v;
  562. if ((hw->fc.current_mode != I40E_FC_FULL) &&
  563. (hw->fc.current_mode != I40E_FC_RX_PAUSE))
  564. return;
  565. xoff = nsd->link_xoff_rx;
  566. i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
  567. pf->stat_offsets_loaded,
  568. &osd->link_xoff_rx, &nsd->link_xoff_rx);
  569. /* No new LFC xoff rx */
  570. if (!(nsd->link_xoff_rx - xoff))
  571. return;
  572. /* Clear the __I40E_HANG_CHECK_ARMED bit for all Tx rings */
  573. for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
  574. struct i40e_vsi *vsi = pf->vsi[v];
  575. if (!vsi)
  576. continue;
  577. for (i = 0; i < vsi->num_queue_pairs; i++) {
  578. struct i40e_ring *ring = vsi->tx_rings[i];
  579. clear_bit(__I40E_HANG_CHECK_ARMED, &ring->state);
  580. }
  581. }
  582. }
  583. /**
  584. * i40e_update_prio_xoff_rx - Update XOFF received in PFC mode
  585. * @pf: the corresponding PF
  586. *
  587. * Update the Rx XOFF counter (PAUSE frames) in PFC mode
  588. **/
  589. static void i40e_update_prio_xoff_rx(struct i40e_pf *pf)
  590. {
  591. struct i40e_hw_port_stats *osd = &pf->stats_offsets;
  592. struct i40e_hw_port_stats *nsd = &pf->stats;
  593. bool xoff[I40E_MAX_TRAFFIC_CLASS] = {false};
  594. struct i40e_dcbx_config *dcb_cfg;
  595. struct i40e_hw *hw = &pf->hw;
  596. u16 i, v;
  597. u8 tc;
  598. dcb_cfg = &hw->local_dcbx_config;
  599. /* See if DCB enabled with PFC TC */
  600. if (!(pf->flags & I40E_FLAG_DCB_ENABLED) ||
  601. !(dcb_cfg->pfc.pfcenable)) {
  602. i40e_update_link_xoff_rx(pf);
  603. return;
  604. }
  605. for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
  606. u64 prio_xoff = nsd->priority_xoff_rx[i];
  607. i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
  608. pf->stat_offsets_loaded,
  609. &osd->priority_xoff_rx[i],
  610. &nsd->priority_xoff_rx[i]);
  611. /* No new PFC xoff rx */
  612. if (!(nsd->priority_xoff_rx[i] - prio_xoff))
  613. continue;
  614. /* Get the TC for given priority */
  615. tc = dcb_cfg->etscfg.prioritytable[i];
  616. xoff[tc] = true;
  617. }
  618. /* Clear the __I40E_HANG_CHECK_ARMED bit for Tx rings */
  619. for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
  620. struct i40e_vsi *vsi = pf->vsi[v];
  621. if (!vsi)
  622. continue;
  623. for (i = 0; i < vsi->num_queue_pairs; i++) {
  624. struct i40e_ring *ring = vsi->tx_rings[i];
  625. tc = ring->dcb_tc;
  626. if (xoff[tc])
  627. clear_bit(__I40E_HANG_CHECK_ARMED,
  628. &ring->state);
  629. }
  630. }
  631. }
  632. /**
  633. * i40e_update_stats - Update the board statistics counters.
  634. * @vsi: the VSI to be updated
  635. *
  636. * There are a few instances where we store the same stat in a
  637. * couple of different structs. This is partly because we have
  638. * the netdev stats that need to be filled out, which is slightly
  639. * different from the "eth_stats" defined by the chip and used in
  640. * VF communications. We sort it all out here in a central place.
  641. **/
  642. void i40e_update_stats(struct i40e_vsi *vsi)
  643. {
  644. struct i40e_pf *pf = vsi->back;
  645. struct i40e_hw *hw = &pf->hw;
  646. struct rtnl_link_stats64 *ons;
  647. struct rtnl_link_stats64 *ns; /* netdev stats */
  648. struct i40e_eth_stats *oes;
  649. struct i40e_eth_stats *es; /* device's eth stats */
  650. u32 tx_restart, tx_busy;
  651. u32 rx_page, rx_buf;
  652. u64 rx_p, rx_b;
  653. u64 tx_p, tx_b;
  654. int i;
  655. u16 q;
  656. if (test_bit(__I40E_DOWN, &vsi->state) ||
  657. test_bit(__I40E_CONFIG_BUSY, &pf->state))
  658. return;
  659. ns = i40e_get_vsi_stats_struct(vsi);
  660. ons = &vsi->net_stats_offsets;
  661. es = &vsi->eth_stats;
  662. oes = &vsi->eth_stats_offsets;
  663. /* Gather up the netdev and vsi stats that the driver collects
  664. * on the fly during packet processing
  665. */
  666. rx_b = rx_p = 0;
  667. tx_b = tx_p = 0;
  668. tx_restart = tx_busy = 0;
  669. rx_page = 0;
  670. rx_buf = 0;
  671. rcu_read_lock();
  672. for (q = 0; q < vsi->num_queue_pairs; q++) {
  673. struct i40e_ring *p;
  674. u64 bytes, packets;
  675. unsigned int start;
  676. /* locate Tx ring */
  677. p = ACCESS_ONCE(vsi->tx_rings[q]);
  678. do {
  679. start = u64_stats_fetch_begin_bh(&p->syncp);
  680. packets = p->stats.packets;
  681. bytes = p->stats.bytes;
  682. } while (u64_stats_fetch_retry_bh(&p->syncp, start));
  683. tx_b += bytes;
  684. tx_p += packets;
  685. tx_restart += p->tx_stats.restart_queue;
  686. tx_busy += p->tx_stats.tx_busy;
  687. /* Rx queue is part of the same block as Tx queue */
  688. p = &p[1];
  689. do {
  690. start = u64_stats_fetch_begin_bh(&p->syncp);
  691. packets = p->stats.packets;
  692. bytes = p->stats.bytes;
  693. } while (u64_stats_fetch_retry_bh(&p->syncp, start));
  694. rx_b += bytes;
  695. rx_p += packets;
  696. rx_buf += p->rx_stats.alloc_rx_buff_failed;
  697. rx_page += p->rx_stats.alloc_rx_page_failed;
  698. }
  699. rcu_read_unlock();
  700. vsi->tx_restart = tx_restart;
  701. vsi->tx_busy = tx_busy;
  702. vsi->rx_page_failed = rx_page;
  703. vsi->rx_buf_failed = rx_buf;
  704. ns->rx_packets = rx_p;
  705. ns->rx_bytes = rx_b;
  706. ns->tx_packets = tx_p;
  707. ns->tx_bytes = tx_b;
  708. i40e_update_eth_stats(vsi);
  709. /* update netdev stats from eth stats */
  710. ons->rx_errors = oes->rx_errors;
  711. ns->rx_errors = es->rx_errors;
  712. ons->tx_errors = oes->tx_errors;
  713. ns->tx_errors = es->tx_errors;
  714. ons->multicast = oes->rx_multicast;
  715. ns->multicast = es->rx_multicast;
  716. ons->tx_dropped = oes->tx_discards;
  717. ns->tx_dropped = es->tx_discards;
  718. /* Get the port data only if this is the main PF VSI */
  719. if (vsi == pf->vsi[pf->lan_vsi]) {
  720. struct i40e_hw_port_stats *nsd = &pf->stats;
  721. struct i40e_hw_port_stats *osd = &pf->stats_offsets;
  722. i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
  723. I40E_GLPRT_GORCL(hw->port),
  724. pf->stat_offsets_loaded,
  725. &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
  726. i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
  727. I40E_GLPRT_GOTCL(hw->port),
  728. pf->stat_offsets_loaded,
  729. &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
  730. i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
  731. pf->stat_offsets_loaded,
  732. &osd->eth.rx_discards,
  733. &nsd->eth.rx_discards);
  734. i40e_stat_update32(hw, I40E_GLPRT_TDPC(hw->port),
  735. pf->stat_offsets_loaded,
  736. &osd->eth.tx_discards,
  737. &nsd->eth.tx_discards);
  738. i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
  739. I40E_GLPRT_MPRCL(hw->port),
  740. pf->stat_offsets_loaded,
  741. &osd->eth.rx_multicast,
  742. &nsd->eth.rx_multicast);
  743. i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
  744. pf->stat_offsets_loaded,
  745. &osd->tx_dropped_link_down,
  746. &nsd->tx_dropped_link_down);
  747. i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
  748. pf->stat_offsets_loaded,
  749. &osd->crc_errors, &nsd->crc_errors);
  750. ns->rx_crc_errors = nsd->crc_errors;
  751. i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
  752. pf->stat_offsets_loaded,
  753. &osd->illegal_bytes, &nsd->illegal_bytes);
  754. ns->rx_errors = nsd->crc_errors
  755. + nsd->illegal_bytes;
  756. i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
  757. pf->stat_offsets_loaded,
  758. &osd->mac_local_faults,
  759. &nsd->mac_local_faults);
  760. i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
  761. pf->stat_offsets_loaded,
  762. &osd->mac_remote_faults,
  763. &nsd->mac_remote_faults);
  764. i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
  765. pf->stat_offsets_loaded,
  766. &osd->rx_length_errors,
  767. &nsd->rx_length_errors);
  768. ns->rx_length_errors = nsd->rx_length_errors;
  769. i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
  770. pf->stat_offsets_loaded,
  771. &osd->link_xon_rx, &nsd->link_xon_rx);
  772. i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
  773. pf->stat_offsets_loaded,
  774. &osd->link_xon_tx, &nsd->link_xon_tx);
  775. i40e_update_prio_xoff_rx(pf); /* handles I40E_GLPRT_LXOFFRXC */
  776. i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
  777. pf->stat_offsets_loaded,
  778. &osd->link_xoff_tx, &nsd->link_xoff_tx);
  779. for (i = 0; i < 8; i++) {
  780. i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
  781. pf->stat_offsets_loaded,
  782. &osd->priority_xon_rx[i],
  783. &nsd->priority_xon_rx[i]);
  784. i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
  785. pf->stat_offsets_loaded,
  786. &osd->priority_xon_tx[i],
  787. &nsd->priority_xon_tx[i]);
  788. i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
  789. pf->stat_offsets_loaded,
  790. &osd->priority_xoff_tx[i],
  791. &nsd->priority_xoff_tx[i]);
  792. i40e_stat_update32(hw,
  793. I40E_GLPRT_RXON2OFFCNT(hw->port, i),
  794. pf->stat_offsets_loaded,
  795. &osd->priority_xon_2_xoff[i],
  796. &nsd->priority_xon_2_xoff[i]);
  797. }
  798. i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
  799. I40E_GLPRT_PRC64L(hw->port),
  800. pf->stat_offsets_loaded,
  801. &osd->rx_size_64, &nsd->rx_size_64);
  802. i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
  803. I40E_GLPRT_PRC127L(hw->port),
  804. pf->stat_offsets_loaded,
  805. &osd->rx_size_127, &nsd->rx_size_127);
  806. i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
  807. I40E_GLPRT_PRC255L(hw->port),
  808. pf->stat_offsets_loaded,
  809. &osd->rx_size_255, &nsd->rx_size_255);
  810. i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
  811. I40E_GLPRT_PRC511L(hw->port),
  812. pf->stat_offsets_loaded,
  813. &osd->rx_size_511, &nsd->rx_size_511);
  814. i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
  815. I40E_GLPRT_PRC1023L(hw->port),
  816. pf->stat_offsets_loaded,
  817. &osd->rx_size_1023, &nsd->rx_size_1023);
  818. i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
  819. I40E_GLPRT_PRC1522L(hw->port),
  820. pf->stat_offsets_loaded,
  821. &osd->rx_size_1522, &nsd->rx_size_1522);
  822. i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
  823. I40E_GLPRT_PRC9522L(hw->port),
  824. pf->stat_offsets_loaded,
  825. &osd->rx_size_big, &nsd->rx_size_big);
  826. i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
  827. I40E_GLPRT_PTC64L(hw->port),
  828. pf->stat_offsets_loaded,
  829. &osd->tx_size_64, &nsd->tx_size_64);
  830. i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
  831. I40E_GLPRT_PTC127L(hw->port),
  832. pf->stat_offsets_loaded,
  833. &osd->tx_size_127, &nsd->tx_size_127);
  834. i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
  835. I40E_GLPRT_PTC255L(hw->port),
  836. pf->stat_offsets_loaded,
  837. &osd->tx_size_255, &nsd->tx_size_255);
  838. i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
  839. I40E_GLPRT_PTC511L(hw->port),
  840. pf->stat_offsets_loaded,
  841. &osd->tx_size_511, &nsd->tx_size_511);
  842. i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
  843. I40E_GLPRT_PTC1023L(hw->port),
  844. pf->stat_offsets_loaded,
  845. &osd->tx_size_1023, &nsd->tx_size_1023);
  846. i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
  847. I40E_GLPRT_PTC1522L(hw->port),
  848. pf->stat_offsets_loaded,
  849. &osd->tx_size_1522, &nsd->tx_size_1522);
  850. i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
  851. I40E_GLPRT_PTC9522L(hw->port),
  852. pf->stat_offsets_loaded,
  853. &osd->tx_size_big, &nsd->tx_size_big);
  854. i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
  855. pf->stat_offsets_loaded,
  856. &osd->rx_undersize, &nsd->rx_undersize);
  857. i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
  858. pf->stat_offsets_loaded,
  859. &osd->rx_fragments, &nsd->rx_fragments);
  860. i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
  861. pf->stat_offsets_loaded,
  862. &osd->rx_oversize, &nsd->rx_oversize);
  863. i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
  864. pf->stat_offsets_loaded,
  865. &osd->rx_jabber, &nsd->rx_jabber);
  866. }
  867. pf->stat_offsets_loaded = true;
  868. }
  869. /**
  870. * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
  871. * @vsi: the VSI to be searched
  872. * @macaddr: the MAC address
  873. * @vlan: the vlan
  874. * @is_vf: make sure its a vf filter, else doesn't matter
  875. * @is_netdev: make sure its a netdev filter, else doesn't matter
  876. *
  877. * Returns ptr to the filter object or NULL
  878. **/
  879. static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
  880. u8 *macaddr, s16 vlan,
  881. bool is_vf, bool is_netdev)
  882. {
  883. struct i40e_mac_filter *f;
  884. if (!vsi || !macaddr)
  885. return NULL;
  886. list_for_each_entry(f, &vsi->mac_filter_list, list) {
  887. if ((ether_addr_equal(macaddr, f->macaddr)) &&
  888. (vlan == f->vlan) &&
  889. (!is_vf || f->is_vf) &&
  890. (!is_netdev || f->is_netdev))
  891. return f;
  892. }
  893. return NULL;
  894. }
  895. /**
  896. * i40e_find_mac - Find a mac addr in the macvlan filters list
  897. * @vsi: the VSI to be searched
  898. * @macaddr: the MAC address we are searching for
  899. * @is_vf: make sure its a vf filter, else doesn't matter
  900. * @is_netdev: make sure its a netdev filter, else doesn't matter
  901. *
  902. * Returns the first filter with the provided MAC address or NULL if
  903. * MAC address was not found
  904. **/
  905. struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, u8 *macaddr,
  906. bool is_vf, bool is_netdev)
  907. {
  908. struct i40e_mac_filter *f;
  909. if (!vsi || !macaddr)
  910. return NULL;
  911. list_for_each_entry(f, &vsi->mac_filter_list, list) {
  912. if ((ether_addr_equal(macaddr, f->macaddr)) &&
  913. (!is_vf || f->is_vf) &&
  914. (!is_netdev || f->is_netdev))
  915. return f;
  916. }
  917. return NULL;
  918. }
  919. /**
  920. * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
  921. * @vsi: the VSI to be searched
  922. *
  923. * Returns true if VSI is in vlan mode or false otherwise
  924. **/
  925. bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
  926. {
  927. struct i40e_mac_filter *f;
  928. /* Only -1 for all the filters denotes not in vlan mode
  929. * so we have to go through all the list in order to make sure
  930. */
  931. list_for_each_entry(f, &vsi->mac_filter_list, list) {
  932. if (f->vlan >= 0)
  933. return true;
  934. }
  935. return false;
  936. }
  937. /**
  938. * i40e_put_mac_in_vlan - Make macvlan filters from macaddrs and vlans
  939. * @vsi: the VSI to be searched
  940. * @macaddr: the mac address to be filtered
  941. * @is_vf: true if it is a vf
  942. * @is_netdev: true if it is a netdev
  943. *
  944. * Goes through all the macvlan filters and adds a
  945. * macvlan filter for each unique vlan that already exists
  946. *
  947. * Returns first filter found on success, else NULL
  948. **/
  949. struct i40e_mac_filter *i40e_put_mac_in_vlan(struct i40e_vsi *vsi, u8 *macaddr,
  950. bool is_vf, bool is_netdev)
  951. {
  952. struct i40e_mac_filter *f;
  953. list_for_each_entry(f, &vsi->mac_filter_list, list) {
  954. if (!i40e_find_filter(vsi, macaddr, f->vlan,
  955. is_vf, is_netdev)) {
  956. if (!i40e_add_filter(vsi, macaddr, f->vlan,
  957. is_vf, is_netdev))
  958. return NULL;
  959. }
  960. }
  961. return list_first_entry_or_null(&vsi->mac_filter_list,
  962. struct i40e_mac_filter, list);
  963. }
  964. /**
  965. * i40e_add_filter - Add a mac/vlan filter to the VSI
  966. * @vsi: the VSI to be searched
  967. * @macaddr: the MAC address
  968. * @vlan: the vlan
  969. * @is_vf: make sure its a vf filter, else doesn't matter
  970. * @is_netdev: make sure its a netdev filter, else doesn't matter
  971. *
  972. * Returns ptr to the filter object or NULL when no memory available.
  973. **/
  974. struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
  975. u8 *macaddr, s16 vlan,
  976. bool is_vf, bool is_netdev)
  977. {
  978. struct i40e_mac_filter *f;
  979. if (!vsi || !macaddr)
  980. return NULL;
  981. f = i40e_find_filter(vsi, macaddr, vlan, is_vf, is_netdev);
  982. if (!f) {
  983. f = kzalloc(sizeof(*f), GFP_ATOMIC);
  984. if (!f)
  985. goto add_filter_out;
  986. memcpy(f->macaddr, macaddr, ETH_ALEN);
  987. f->vlan = vlan;
  988. f->changed = true;
  989. INIT_LIST_HEAD(&f->list);
  990. list_add(&f->list, &vsi->mac_filter_list);
  991. }
  992. /* increment counter and add a new flag if needed */
  993. if (is_vf) {
  994. if (!f->is_vf) {
  995. f->is_vf = true;
  996. f->counter++;
  997. }
  998. } else if (is_netdev) {
  999. if (!f->is_netdev) {
  1000. f->is_netdev = true;
  1001. f->counter++;
  1002. }
  1003. } else {
  1004. f->counter++;
  1005. }
  1006. /* changed tells sync_filters_subtask to
  1007. * push the filter down to the firmware
  1008. */
  1009. if (f->changed) {
  1010. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  1011. vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
  1012. }
  1013. add_filter_out:
  1014. return f;
  1015. }
  1016. /**
  1017. * i40e_del_filter - Remove a mac/vlan filter from the VSI
  1018. * @vsi: the VSI to be searched
  1019. * @macaddr: the MAC address
  1020. * @vlan: the vlan
  1021. * @is_vf: make sure it's a vf filter, else doesn't matter
  1022. * @is_netdev: make sure it's a netdev filter, else doesn't matter
  1023. **/
  1024. void i40e_del_filter(struct i40e_vsi *vsi,
  1025. u8 *macaddr, s16 vlan,
  1026. bool is_vf, bool is_netdev)
  1027. {
  1028. struct i40e_mac_filter *f;
  1029. if (!vsi || !macaddr)
  1030. return;
  1031. f = i40e_find_filter(vsi, macaddr, vlan, is_vf, is_netdev);
  1032. if (!f || f->counter == 0)
  1033. return;
  1034. if (is_vf) {
  1035. if (f->is_vf) {
  1036. f->is_vf = false;
  1037. f->counter--;
  1038. }
  1039. } else if (is_netdev) {
  1040. if (f->is_netdev) {
  1041. f->is_netdev = false;
  1042. f->counter--;
  1043. }
  1044. } else {
  1045. /* make sure we don't remove a filter in use by vf or netdev */
  1046. int min_f = 0;
  1047. min_f += (f->is_vf ? 1 : 0);
  1048. min_f += (f->is_netdev ? 1 : 0);
  1049. if (f->counter > min_f)
  1050. f->counter--;
  1051. }
  1052. /* counter == 0 tells sync_filters_subtask to
  1053. * remove the filter from the firmware's list
  1054. */
  1055. if (f->counter == 0) {
  1056. f->changed = true;
  1057. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  1058. vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
  1059. }
  1060. }
  1061. /**
  1062. * i40e_set_mac - NDO callback to set mac address
  1063. * @netdev: network interface device structure
  1064. * @p: pointer to an address structure
  1065. *
  1066. * Returns 0 on success, negative on failure
  1067. **/
  1068. static int i40e_set_mac(struct net_device *netdev, void *p)
  1069. {
  1070. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1071. struct i40e_vsi *vsi = np->vsi;
  1072. struct sockaddr *addr = p;
  1073. struct i40e_mac_filter *f;
  1074. if (!is_valid_ether_addr(addr->sa_data))
  1075. return -EADDRNOTAVAIL;
  1076. netdev_info(netdev, "set mac address=%pM\n", addr->sa_data);
  1077. if (ether_addr_equal(netdev->dev_addr, addr->sa_data))
  1078. return 0;
  1079. if (vsi->type == I40E_VSI_MAIN) {
  1080. i40e_status ret;
  1081. ret = i40e_aq_mac_address_write(&vsi->back->hw,
  1082. I40E_AQC_WRITE_TYPE_LAA_ONLY,
  1083. addr->sa_data, NULL);
  1084. if (ret) {
  1085. netdev_info(netdev,
  1086. "Addr change for Main VSI failed: %d\n",
  1087. ret);
  1088. return -EADDRNOTAVAIL;
  1089. }
  1090. memcpy(vsi->back->hw.mac.addr, addr->sa_data, netdev->addr_len);
  1091. }
  1092. /* In order to be sure to not drop any packets, add the new address
  1093. * then delete the old one.
  1094. */
  1095. f = i40e_add_filter(vsi, addr->sa_data, I40E_VLAN_ANY, false, false);
  1096. if (!f)
  1097. return -ENOMEM;
  1098. i40e_sync_vsi_filters(vsi);
  1099. i40e_del_filter(vsi, netdev->dev_addr, I40E_VLAN_ANY, false, false);
  1100. i40e_sync_vsi_filters(vsi);
  1101. memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
  1102. return 0;
  1103. }
  1104. /**
  1105. * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
  1106. * @vsi: the VSI being setup
  1107. * @ctxt: VSI context structure
  1108. * @enabled_tc: Enabled TCs bitmap
  1109. * @is_add: True if called before Add VSI
  1110. *
  1111. * Setup VSI queue mapping for enabled traffic classes.
  1112. **/
  1113. static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
  1114. struct i40e_vsi_context *ctxt,
  1115. u8 enabled_tc,
  1116. bool is_add)
  1117. {
  1118. struct i40e_pf *pf = vsi->back;
  1119. u16 sections = 0;
  1120. u8 netdev_tc = 0;
  1121. u16 numtc = 0;
  1122. u16 qcount;
  1123. u8 offset;
  1124. u16 qmap;
  1125. int i;
  1126. sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
  1127. offset = 0;
  1128. if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
  1129. /* Find numtc from enabled TC bitmap */
  1130. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  1131. if (enabled_tc & (1 << i)) /* TC is enabled */
  1132. numtc++;
  1133. }
  1134. if (!numtc) {
  1135. dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
  1136. numtc = 1;
  1137. }
  1138. } else {
  1139. /* At least TC0 is enabled in case of non-DCB case */
  1140. numtc = 1;
  1141. }
  1142. vsi->tc_config.numtc = numtc;
  1143. vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
  1144. /* Setup queue offset/count for all TCs for given VSI */
  1145. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  1146. /* See if the given TC is enabled for the given VSI */
  1147. if (vsi->tc_config.enabled_tc & (1 << i)) { /* TC is enabled */
  1148. int pow, num_qps;
  1149. vsi->tc_config.tc_info[i].qoffset = offset;
  1150. switch (vsi->type) {
  1151. case I40E_VSI_MAIN:
  1152. if (i == 0)
  1153. qcount = pf->rss_size;
  1154. else
  1155. qcount = pf->num_tc_qps;
  1156. vsi->tc_config.tc_info[i].qcount = qcount;
  1157. break;
  1158. case I40E_VSI_FDIR:
  1159. case I40E_VSI_SRIOV:
  1160. case I40E_VSI_VMDQ2:
  1161. default:
  1162. qcount = vsi->alloc_queue_pairs;
  1163. vsi->tc_config.tc_info[i].qcount = qcount;
  1164. WARN_ON(i != 0);
  1165. break;
  1166. }
  1167. /* find the power-of-2 of the number of queue pairs */
  1168. num_qps = vsi->tc_config.tc_info[i].qcount;
  1169. pow = 0;
  1170. while (num_qps &&
  1171. ((1 << pow) < vsi->tc_config.tc_info[i].qcount)) {
  1172. pow++;
  1173. num_qps >>= 1;
  1174. }
  1175. vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
  1176. qmap =
  1177. (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
  1178. (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
  1179. offset += vsi->tc_config.tc_info[i].qcount;
  1180. } else {
  1181. /* TC is not enabled so set the offset to
  1182. * default queue and allocate one queue
  1183. * for the given TC.
  1184. */
  1185. vsi->tc_config.tc_info[i].qoffset = 0;
  1186. vsi->tc_config.tc_info[i].qcount = 1;
  1187. vsi->tc_config.tc_info[i].netdev_tc = 0;
  1188. qmap = 0;
  1189. }
  1190. ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
  1191. }
  1192. /* Set actual Tx/Rx queue pairs */
  1193. vsi->num_queue_pairs = offset;
  1194. /* Scheduler section valid can only be set for ADD VSI */
  1195. if (is_add) {
  1196. sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
  1197. ctxt->info.up_enable_bits = enabled_tc;
  1198. }
  1199. if (vsi->type == I40E_VSI_SRIOV) {
  1200. ctxt->info.mapping_flags |=
  1201. cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
  1202. for (i = 0; i < vsi->num_queue_pairs; i++)
  1203. ctxt->info.queue_mapping[i] =
  1204. cpu_to_le16(vsi->base_queue + i);
  1205. } else {
  1206. ctxt->info.mapping_flags |=
  1207. cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
  1208. ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
  1209. }
  1210. ctxt->info.valid_sections |= cpu_to_le16(sections);
  1211. }
  1212. /**
  1213. * i40e_set_rx_mode - NDO callback to set the netdev filters
  1214. * @netdev: network interface device structure
  1215. **/
  1216. static void i40e_set_rx_mode(struct net_device *netdev)
  1217. {
  1218. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1219. struct i40e_mac_filter *f, *ftmp;
  1220. struct i40e_vsi *vsi = np->vsi;
  1221. struct netdev_hw_addr *uca;
  1222. struct netdev_hw_addr *mca;
  1223. struct netdev_hw_addr *ha;
  1224. /* add addr if not already in the filter list */
  1225. netdev_for_each_uc_addr(uca, netdev) {
  1226. if (!i40e_find_mac(vsi, uca->addr, false, true)) {
  1227. if (i40e_is_vsi_in_vlan(vsi))
  1228. i40e_put_mac_in_vlan(vsi, uca->addr,
  1229. false, true);
  1230. else
  1231. i40e_add_filter(vsi, uca->addr, I40E_VLAN_ANY,
  1232. false, true);
  1233. }
  1234. }
  1235. netdev_for_each_mc_addr(mca, netdev) {
  1236. if (!i40e_find_mac(vsi, mca->addr, false, true)) {
  1237. if (i40e_is_vsi_in_vlan(vsi))
  1238. i40e_put_mac_in_vlan(vsi, mca->addr,
  1239. false, true);
  1240. else
  1241. i40e_add_filter(vsi, mca->addr, I40E_VLAN_ANY,
  1242. false, true);
  1243. }
  1244. }
  1245. /* remove filter if not in netdev list */
  1246. list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
  1247. bool found = false;
  1248. if (!f->is_netdev)
  1249. continue;
  1250. if (is_multicast_ether_addr(f->macaddr)) {
  1251. netdev_for_each_mc_addr(mca, netdev) {
  1252. if (ether_addr_equal(mca->addr, f->macaddr)) {
  1253. found = true;
  1254. break;
  1255. }
  1256. }
  1257. } else {
  1258. netdev_for_each_uc_addr(uca, netdev) {
  1259. if (ether_addr_equal(uca->addr, f->macaddr)) {
  1260. found = true;
  1261. break;
  1262. }
  1263. }
  1264. for_each_dev_addr(netdev, ha) {
  1265. if (ether_addr_equal(ha->addr, f->macaddr)) {
  1266. found = true;
  1267. break;
  1268. }
  1269. }
  1270. }
  1271. if (!found)
  1272. i40e_del_filter(
  1273. vsi, f->macaddr, I40E_VLAN_ANY, false, true);
  1274. }
  1275. /* check for other flag changes */
  1276. if (vsi->current_netdev_flags != vsi->netdev->flags) {
  1277. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  1278. vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
  1279. }
  1280. }
  1281. /**
  1282. * i40e_sync_vsi_filters - Update the VSI filter list to the HW
  1283. * @vsi: ptr to the VSI
  1284. *
  1285. * Push any outstanding VSI filter changes through the AdminQ.
  1286. *
  1287. * Returns 0 or error value
  1288. **/
  1289. int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
  1290. {
  1291. struct i40e_mac_filter *f, *ftmp;
  1292. bool promisc_forced_on = false;
  1293. bool add_happened = false;
  1294. int filter_list_len = 0;
  1295. u32 changed_flags = 0;
  1296. i40e_status aq_ret = 0;
  1297. struct i40e_pf *pf;
  1298. int num_add = 0;
  1299. int num_del = 0;
  1300. u16 cmd_flags;
  1301. /* empty array typed pointers, kcalloc later */
  1302. struct i40e_aqc_add_macvlan_element_data *add_list;
  1303. struct i40e_aqc_remove_macvlan_element_data *del_list;
  1304. while (test_and_set_bit(__I40E_CONFIG_BUSY, &vsi->state))
  1305. usleep_range(1000, 2000);
  1306. pf = vsi->back;
  1307. if (vsi->netdev) {
  1308. changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
  1309. vsi->current_netdev_flags = vsi->netdev->flags;
  1310. }
  1311. if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
  1312. vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
  1313. filter_list_len = pf->hw.aq.asq_buf_size /
  1314. sizeof(struct i40e_aqc_remove_macvlan_element_data);
  1315. del_list = kcalloc(filter_list_len,
  1316. sizeof(struct i40e_aqc_remove_macvlan_element_data),
  1317. GFP_KERNEL);
  1318. if (!del_list)
  1319. return -ENOMEM;
  1320. list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
  1321. if (!f->changed)
  1322. continue;
  1323. if (f->counter != 0)
  1324. continue;
  1325. f->changed = false;
  1326. cmd_flags = 0;
  1327. /* add to delete list */
  1328. memcpy(del_list[num_del].mac_addr,
  1329. f->macaddr, ETH_ALEN);
  1330. del_list[num_del].vlan_tag =
  1331. cpu_to_le16((u16)(f->vlan ==
  1332. I40E_VLAN_ANY ? 0 : f->vlan));
  1333. /* vlan0 as wild card to allow packets from all vlans */
  1334. if (f->vlan == I40E_VLAN_ANY ||
  1335. (vsi->netdev && !(vsi->netdev->features &
  1336. NETIF_F_HW_VLAN_CTAG_FILTER)))
  1337. cmd_flags |= I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
  1338. cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
  1339. del_list[num_del].flags = cmd_flags;
  1340. num_del++;
  1341. /* unlink from filter list */
  1342. list_del(&f->list);
  1343. kfree(f);
  1344. /* flush a full buffer */
  1345. if (num_del == filter_list_len) {
  1346. aq_ret = i40e_aq_remove_macvlan(&pf->hw,
  1347. vsi->seid, del_list, num_del,
  1348. NULL);
  1349. num_del = 0;
  1350. memset(del_list, 0, sizeof(*del_list));
  1351. if (aq_ret)
  1352. dev_info(&pf->pdev->dev,
  1353. "ignoring delete macvlan error, err %d, aq_err %d while flushing a full buffer\n",
  1354. aq_ret,
  1355. pf->hw.aq.asq_last_status);
  1356. }
  1357. }
  1358. if (num_del) {
  1359. aq_ret = i40e_aq_remove_macvlan(&pf->hw, vsi->seid,
  1360. del_list, num_del, NULL);
  1361. num_del = 0;
  1362. if (aq_ret)
  1363. dev_info(&pf->pdev->dev,
  1364. "ignoring delete macvlan error, err %d, aq_err %d\n",
  1365. aq_ret, pf->hw.aq.asq_last_status);
  1366. }
  1367. kfree(del_list);
  1368. del_list = NULL;
  1369. /* do all the adds now */
  1370. filter_list_len = pf->hw.aq.asq_buf_size /
  1371. sizeof(struct i40e_aqc_add_macvlan_element_data),
  1372. add_list = kcalloc(filter_list_len,
  1373. sizeof(struct i40e_aqc_add_macvlan_element_data),
  1374. GFP_KERNEL);
  1375. if (!add_list)
  1376. return -ENOMEM;
  1377. list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
  1378. if (!f->changed)
  1379. continue;
  1380. if (f->counter == 0)
  1381. continue;
  1382. f->changed = false;
  1383. add_happened = true;
  1384. cmd_flags = 0;
  1385. /* add to add array */
  1386. memcpy(add_list[num_add].mac_addr,
  1387. f->macaddr, ETH_ALEN);
  1388. add_list[num_add].vlan_tag =
  1389. cpu_to_le16(
  1390. (u16)(f->vlan == I40E_VLAN_ANY ? 0 : f->vlan));
  1391. add_list[num_add].queue_number = 0;
  1392. cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
  1393. /* vlan0 as wild card to allow packets from all vlans */
  1394. if (f->vlan == I40E_VLAN_ANY || (vsi->netdev &&
  1395. !(vsi->netdev->features &
  1396. NETIF_F_HW_VLAN_CTAG_FILTER)))
  1397. cmd_flags |= I40E_AQC_MACVLAN_ADD_IGNORE_VLAN;
  1398. add_list[num_add].flags = cpu_to_le16(cmd_flags);
  1399. num_add++;
  1400. /* flush a full buffer */
  1401. if (num_add == filter_list_len) {
  1402. aq_ret = i40e_aq_add_macvlan(&pf->hw, vsi->seid,
  1403. add_list, num_add,
  1404. NULL);
  1405. num_add = 0;
  1406. if (aq_ret)
  1407. break;
  1408. memset(add_list, 0, sizeof(*add_list));
  1409. }
  1410. }
  1411. if (num_add) {
  1412. aq_ret = i40e_aq_add_macvlan(&pf->hw, vsi->seid,
  1413. add_list, num_add, NULL);
  1414. num_add = 0;
  1415. }
  1416. kfree(add_list);
  1417. add_list = NULL;
  1418. if (add_happened && (!aq_ret)) {
  1419. /* do nothing */;
  1420. } else if (add_happened && (aq_ret)) {
  1421. dev_info(&pf->pdev->dev,
  1422. "add filter failed, err %d, aq_err %d\n",
  1423. aq_ret, pf->hw.aq.asq_last_status);
  1424. if ((pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOSPC) &&
  1425. !test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
  1426. &vsi->state)) {
  1427. promisc_forced_on = true;
  1428. set_bit(__I40E_FILTER_OVERFLOW_PROMISC,
  1429. &vsi->state);
  1430. dev_info(&pf->pdev->dev, "promiscuous mode forced on\n");
  1431. }
  1432. }
  1433. }
  1434. /* check for changes in promiscuous modes */
  1435. if (changed_flags & IFF_ALLMULTI) {
  1436. bool cur_multipromisc;
  1437. cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
  1438. aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
  1439. vsi->seid,
  1440. cur_multipromisc,
  1441. NULL);
  1442. if (aq_ret)
  1443. dev_info(&pf->pdev->dev,
  1444. "set multi promisc failed, err %d, aq_err %d\n",
  1445. aq_ret, pf->hw.aq.asq_last_status);
  1446. }
  1447. if ((changed_flags & IFF_PROMISC) || promisc_forced_on) {
  1448. bool cur_promisc;
  1449. cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
  1450. test_bit(__I40E_FILTER_OVERFLOW_PROMISC,
  1451. &vsi->state));
  1452. aq_ret = i40e_aq_set_vsi_unicast_promiscuous(&vsi->back->hw,
  1453. vsi->seid,
  1454. cur_promisc, NULL);
  1455. if (aq_ret)
  1456. dev_info(&pf->pdev->dev,
  1457. "set uni promisc failed, err %d, aq_err %d\n",
  1458. aq_ret, pf->hw.aq.asq_last_status);
  1459. }
  1460. clear_bit(__I40E_CONFIG_BUSY, &vsi->state);
  1461. return 0;
  1462. }
  1463. /**
  1464. * i40e_sync_filters_subtask - Sync the VSI filter list with HW
  1465. * @pf: board private structure
  1466. **/
  1467. static void i40e_sync_filters_subtask(struct i40e_pf *pf)
  1468. {
  1469. int v;
  1470. if (!pf || !(pf->flags & I40E_FLAG_FILTER_SYNC))
  1471. return;
  1472. pf->flags &= ~I40E_FLAG_FILTER_SYNC;
  1473. for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
  1474. if (pf->vsi[v] &&
  1475. (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED))
  1476. i40e_sync_vsi_filters(pf->vsi[v]);
  1477. }
  1478. }
  1479. /**
  1480. * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
  1481. * @netdev: network interface device structure
  1482. * @new_mtu: new value for maximum frame size
  1483. *
  1484. * Returns 0 on success, negative on failure
  1485. **/
  1486. static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
  1487. {
  1488. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1489. int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN;
  1490. struct i40e_vsi *vsi = np->vsi;
  1491. /* MTU < 68 is an error and causes problems on some kernels */
  1492. if ((new_mtu < 68) || (max_frame > I40E_MAX_RXBUFFER))
  1493. return -EINVAL;
  1494. netdev_info(netdev, "changing MTU from %d to %d\n",
  1495. netdev->mtu, new_mtu);
  1496. netdev->mtu = new_mtu;
  1497. if (netif_running(netdev))
  1498. i40e_vsi_reinit_locked(vsi);
  1499. return 0;
  1500. }
  1501. /**
  1502. * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
  1503. * @vsi: the vsi being adjusted
  1504. **/
  1505. void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
  1506. {
  1507. struct i40e_vsi_context ctxt;
  1508. i40e_status ret;
  1509. if ((vsi->info.valid_sections &
  1510. cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
  1511. ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
  1512. return; /* already enabled */
  1513. vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
  1514. vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
  1515. I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
  1516. ctxt.seid = vsi->seid;
  1517. memcpy(&ctxt.info, &vsi->info, sizeof(vsi->info));
  1518. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  1519. if (ret) {
  1520. dev_info(&vsi->back->pdev->dev,
  1521. "%s: update vsi failed, aq_err=%d\n",
  1522. __func__, vsi->back->hw.aq.asq_last_status);
  1523. }
  1524. }
  1525. /**
  1526. * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
  1527. * @vsi: the vsi being adjusted
  1528. **/
  1529. void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
  1530. {
  1531. struct i40e_vsi_context ctxt;
  1532. i40e_status ret;
  1533. if ((vsi->info.valid_sections &
  1534. cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
  1535. ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
  1536. I40E_AQ_VSI_PVLAN_EMOD_MASK))
  1537. return; /* already disabled */
  1538. vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
  1539. vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
  1540. I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
  1541. ctxt.seid = vsi->seid;
  1542. memcpy(&ctxt.info, &vsi->info, sizeof(vsi->info));
  1543. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  1544. if (ret) {
  1545. dev_info(&vsi->back->pdev->dev,
  1546. "%s: update vsi failed, aq_err=%d\n",
  1547. __func__, vsi->back->hw.aq.asq_last_status);
  1548. }
  1549. }
  1550. /**
  1551. * i40e_vlan_rx_register - Setup or shutdown vlan offload
  1552. * @netdev: network interface to be adjusted
  1553. * @features: netdev features to test if VLAN offload is enabled or not
  1554. **/
  1555. static void i40e_vlan_rx_register(struct net_device *netdev, u32 features)
  1556. {
  1557. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1558. struct i40e_vsi *vsi = np->vsi;
  1559. if (features & NETIF_F_HW_VLAN_CTAG_RX)
  1560. i40e_vlan_stripping_enable(vsi);
  1561. else
  1562. i40e_vlan_stripping_disable(vsi);
  1563. }
  1564. /**
  1565. * i40e_vsi_add_vlan - Add vsi membership for given vlan
  1566. * @vsi: the vsi being configured
  1567. * @vid: vlan id to be added (0 = untagged only , -1 = any)
  1568. **/
  1569. int i40e_vsi_add_vlan(struct i40e_vsi *vsi, s16 vid)
  1570. {
  1571. struct i40e_mac_filter *f, *add_f;
  1572. bool is_netdev, is_vf;
  1573. int ret;
  1574. is_vf = (vsi->type == I40E_VSI_SRIOV);
  1575. is_netdev = !!(vsi->netdev);
  1576. if (is_netdev) {
  1577. add_f = i40e_add_filter(vsi, vsi->netdev->dev_addr, vid,
  1578. is_vf, is_netdev);
  1579. if (!add_f) {
  1580. dev_info(&vsi->back->pdev->dev,
  1581. "Could not add vlan filter %d for %pM\n",
  1582. vid, vsi->netdev->dev_addr);
  1583. return -ENOMEM;
  1584. }
  1585. }
  1586. list_for_each_entry(f, &vsi->mac_filter_list, list) {
  1587. add_f = i40e_add_filter(vsi, f->macaddr, vid, is_vf, is_netdev);
  1588. if (!add_f) {
  1589. dev_info(&vsi->back->pdev->dev,
  1590. "Could not add vlan filter %d for %pM\n",
  1591. vid, f->macaddr);
  1592. return -ENOMEM;
  1593. }
  1594. }
  1595. ret = i40e_sync_vsi_filters(vsi);
  1596. if (ret) {
  1597. dev_info(&vsi->back->pdev->dev,
  1598. "Could not sync filters for vid %d\n", vid);
  1599. return ret;
  1600. }
  1601. /* Now if we add a vlan tag, make sure to check if it is the first
  1602. * tag (i.e. a "tag" -1 does exist) and if so replace the -1 "tag"
  1603. * with 0, so we now accept untagged and specified tagged traffic
  1604. * (and not any taged and untagged)
  1605. */
  1606. if (vid > 0) {
  1607. if (is_netdev && i40e_find_filter(vsi, vsi->netdev->dev_addr,
  1608. I40E_VLAN_ANY,
  1609. is_vf, is_netdev)) {
  1610. i40e_del_filter(vsi, vsi->netdev->dev_addr,
  1611. I40E_VLAN_ANY, is_vf, is_netdev);
  1612. add_f = i40e_add_filter(vsi, vsi->netdev->dev_addr, 0,
  1613. is_vf, is_netdev);
  1614. if (!add_f) {
  1615. dev_info(&vsi->back->pdev->dev,
  1616. "Could not add filter 0 for %pM\n",
  1617. vsi->netdev->dev_addr);
  1618. return -ENOMEM;
  1619. }
  1620. }
  1621. list_for_each_entry(f, &vsi->mac_filter_list, list) {
  1622. if (i40e_find_filter(vsi, f->macaddr, I40E_VLAN_ANY,
  1623. is_vf, is_netdev)) {
  1624. i40e_del_filter(vsi, f->macaddr, I40E_VLAN_ANY,
  1625. is_vf, is_netdev);
  1626. add_f = i40e_add_filter(vsi, f->macaddr,
  1627. 0, is_vf, is_netdev);
  1628. if (!add_f) {
  1629. dev_info(&vsi->back->pdev->dev,
  1630. "Could not add filter 0 for %pM\n",
  1631. f->macaddr);
  1632. return -ENOMEM;
  1633. }
  1634. }
  1635. }
  1636. ret = i40e_sync_vsi_filters(vsi);
  1637. }
  1638. return ret;
  1639. }
  1640. /**
  1641. * i40e_vsi_kill_vlan - Remove vsi membership for given vlan
  1642. * @vsi: the vsi being configured
  1643. * @vid: vlan id to be removed (0 = untagged only , -1 = any)
  1644. *
  1645. * Return: 0 on success or negative otherwise
  1646. **/
  1647. int i40e_vsi_kill_vlan(struct i40e_vsi *vsi, s16 vid)
  1648. {
  1649. struct net_device *netdev = vsi->netdev;
  1650. struct i40e_mac_filter *f, *add_f;
  1651. bool is_vf, is_netdev;
  1652. int filter_count = 0;
  1653. int ret;
  1654. is_vf = (vsi->type == I40E_VSI_SRIOV);
  1655. is_netdev = !!(netdev);
  1656. if (is_netdev)
  1657. i40e_del_filter(vsi, netdev->dev_addr, vid, is_vf, is_netdev);
  1658. list_for_each_entry(f, &vsi->mac_filter_list, list)
  1659. i40e_del_filter(vsi, f->macaddr, vid, is_vf, is_netdev);
  1660. ret = i40e_sync_vsi_filters(vsi);
  1661. if (ret) {
  1662. dev_info(&vsi->back->pdev->dev, "Could not sync filters\n");
  1663. return ret;
  1664. }
  1665. /* go through all the filters for this VSI and if there is only
  1666. * vid == 0 it means there are no other filters, so vid 0 must
  1667. * be replaced with -1. This signifies that we should from now
  1668. * on accept any traffic (with any tag present, or untagged)
  1669. */
  1670. list_for_each_entry(f, &vsi->mac_filter_list, list) {
  1671. if (is_netdev) {
  1672. if (f->vlan &&
  1673. ether_addr_equal(netdev->dev_addr, f->macaddr))
  1674. filter_count++;
  1675. }
  1676. if (f->vlan)
  1677. filter_count++;
  1678. }
  1679. if (!filter_count && is_netdev) {
  1680. i40e_del_filter(vsi, netdev->dev_addr, 0, is_vf, is_netdev);
  1681. f = i40e_add_filter(vsi, netdev->dev_addr, I40E_VLAN_ANY,
  1682. is_vf, is_netdev);
  1683. if (!f) {
  1684. dev_info(&vsi->back->pdev->dev,
  1685. "Could not add filter %d for %pM\n",
  1686. I40E_VLAN_ANY, netdev->dev_addr);
  1687. return -ENOMEM;
  1688. }
  1689. }
  1690. if (!filter_count) {
  1691. list_for_each_entry(f, &vsi->mac_filter_list, list) {
  1692. i40e_del_filter(vsi, f->macaddr, 0, is_vf, is_netdev);
  1693. add_f = i40e_add_filter(vsi, f->macaddr, I40E_VLAN_ANY,
  1694. is_vf, is_netdev);
  1695. if (!add_f) {
  1696. dev_info(&vsi->back->pdev->dev,
  1697. "Could not add filter %d for %pM\n",
  1698. I40E_VLAN_ANY, f->macaddr);
  1699. return -ENOMEM;
  1700. }
  1701. }
  1702. }
  1703. return i40e_sync_vsi_filters(vsi);
  1704. }
  1705. /**
  1706. * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
  1707. * @netdev: network interface to be adjusted
  1708. * @vid: vlan id to be added
  1709. *
  1710. * net_device_ops implementation for adding vlan ids
  1711. **/
  1712. static int i40e_vlan_rx_add_vid(struct net_device *netdev,
  1713. __always_unused __be16 proto, u16 vid)
  1714. {
  1715. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1716. struct i40e_vsi *vsi = np->vsi;
  1717. int ret = 0;
  1718. if (vid > 4095)
  1719. return -EINVAL;
  1720. netdev_info(netdev, "adding %pM vid=%d\n", netdev->dev_addr, vid);
  1721. /* If the network stack called us with vid = 0, we should
  1722. * indicate to i40e_vsi_add_vlan() that we want to receive
  1723. * any traffic (i.e. with any vlan tag, or untagged)
  1724. */
  1725. ret = i40e_vsi_add_vlan(vsi, vid ? vid : I40E_VLAN_ANY);
  1726. if (!ret && (vid < VLAN_N_VID))
  1727. set_bit(vid, vsi->active_vlans);
  1728. return ret;
  1729. }
  1730. /**
  1731. * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
  1732. * @netdev: network interface to be adjusted
  1733. * @vid: vlan id to be removed
  1734. *
  1735. * net_device_ops implementation for adding vlan ids
  1736. **/
  1737. static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
  1738. __always_unused __be16 proto, u16 vid)
  1739. {
  1740. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1741. struct i40e_vsi *vsi = np->vsi;
  1742. netdev_info(netdev, "removing %pM vid=%d\n", netdev->dev_addr, vid);
  1743. /* return code is ignored as there is nothing a user
  1744. * can do about failure to remove and a log message was
  1745. * already printed from the other function
  1746. */
  1747. i40e_vsi_kill_vlan(vsi, vid);
  1748. clear_bit(vid, vsi->active_vlans);
  1749. return 0;
  1750. }
  1751. /**
  1752. * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
  1753. * @vsi: the vsi being brought back up
  1754. **/
  1755. static void i40e_restore_vlan(struct i40e_vsi *vsi)
  1756. {
  1757. u16 vid;
  1758. if (!vsi->netdev)
  1759. return;
  1760. i40e_vlan_rx_register(vsi->netdev, vsi->netdev->features);
  1761. for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
  1762. i40e_vlan_rx_add_vid(vsi->netdev, htons(ETH_P_8021Q),
  1763. vid);
  1764. }
  1765. /**
  1766. * i40e_vsi_add_pvid - Add pvid for the VSI
  1767. * @vsi: the vsi being adjusted
  1768. * @vid: the vlan id to set as a PVID
  1769. **/
  1770. int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
  1771. {
  1772. struct i40e_vsi_context ctxt;
  1773. i40e_status aq_ret;
  1774. vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
  1775. vsi->info.pvid = cpu_to_le16(vid);
  1776. vsi->info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_INSERT_PVID;
  1777. vsi->info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_UNTAGGED;
  1778. ctxt.seid = vsi->seid;
  1779. memcpy(&ctxt.info, &vsi->info, sizeof(vsi->info));
  1780. aq_ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  1781. if (aq_ret) {
  1782. dev_info(&vsi->back->pdev->dev,
  1783. "%s: update vsi failed, aq_err=%d\n",
  1784. __func__, vsi->back->hw.aq.asq_last_status);
  1785. return -ENOENT;
  1786. }
  1787. return 0;
  1788. }
  1789. /**
  1790. * i40e_vsi_remove_pvid - Remove the pvid from the VSI
  1791. * @vsi: the vsi being adjusted
  1792. *
  1793. * Just use the vlan_rx_register() service to put it back to normal
  1794. **/
  1795. void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
  1796. {
  1797. vsi->info.pvid = 0;
  1798. i40e_vlan_rx_register(vsi->netdev, vsi->netdev->features);
  1799. }
  1800. /**
  1801. * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
  1802. * @vsi: ptr to the VSI
  1803. *
  1804. * If this function returns with an error, then it's possible one or
  1805. * more of the rings is populated (while the rest are not). It is the
  1806. * callers duty to clean those orphaned rings.
  1807. *
  1808. * Return 0 on success, negative on failure
  1809. **/
  1810. static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
  1811. {
  1812. int i, err = 0;
  1813. for (i = 0; i < vsi->num_queue_pairs && !err; i++)
  1814. err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
  1815. return err;
  1816. }
  1817. /**
  1818. * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
  1819. * @vsi: ptr to the VSI
  1820. *
  1821. * Free VSI's transmit software resources
  1822. **/
  1823. static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
  1824. {
  1825. int i;
  1826. for (i = 0; i < vsi->num_queue_pairs; i++)
  1827. if (vsi->tx_rings[i]->desc)
  1828. i40e_free_tx_resources(vsi->tx_rings[i]);
  1829. }
  1830. /**
  1831. * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
  1832. * @vsi: ptr to the VSI
  1833. *
  1834. * If this function returns with an error, then it's possible one or
  1835. * more of the rings is populated (while the rest are not). It is the
  1836. * callers duty to clean those orphaned rings.
  1837. *
  1838. * Return 0 on success, negative on failure
  1839. **/
  1840. static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
  1841. {
  1842. int i, err = 0;
  1843. for (i = 0; i < vsi->num_queue_pairs && !err; i++)
  1844. err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
  1845. return err;
  1846. }
  1847. /**
  1848. * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
  1849. * @vsi: ptr to the VSI
  1850. *
  1851. * Free all receive software resources
  1852. **/
  1853. static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
  1854. {
  1855. int i;
  1856. for (i = 0; i < vsi->num_queue_pairs; i++)
  1857. if (vsi->rx_rings[i]->desc)
  1858. i40e_free_rx_resources(vsi->rx_rings[i]);
  1859. }
  1860. /**
  1861. * i40e_configure_tx_ring - Configure a transmit ring context and rest
  1862. * @ring: The Tx ring to configure
  1863. *
  1864. * Configure the Tx descriptor ring in the HMC context.
  1865. **/
  1866. static int i40e_configure_tx_ring(struct i40e_ring *ring)
  1867. {
  1868. struct i40e_vsi *vsi = ring->vsi;
  1869. u16 pf_q = vsi->base_queue + ring->queue_index;
  1870. struct i40e_hw *hw = &vsi->back->hw;
  1871. struct i40e_hmc_obj_txq tx_ctx;
  1872. i40e_status err = 0;
  1873. u32 qtx_ctl = 0;
  1874. /* some ATR related tx ring init */
  1875. if (vsi->back->flags & I40E_FLAG_FDIR_ATR_ENABLED) {
  1876. ring->atr_sample_rate = vsi->back->atr_sample_rate;
  1877. ring->atr_count = 0;
  1878. } else {
  1879. ring->atr_sample_rate = 0;
  1880. }
  1881. /* initialize XPS */
  1882. if (ring->q_vector && ring->netdev &&
  1883. !test_and_set_bit(__I40E_TX_XPS_INIT_DONE, &ring->state))
  1884. netif_set_xps_queue(ring->netdev,
  1885. &ring->q_vector->affinity_mask,
  1886. ring->queue_index);
  1887. /* clear the context structure first */
  1888. memset(&tx_ctx, 0, sizeof(tx_ctx));
  1889. tx_ctx.new_context = 1;
  1890. tx_ctx.base = (ring->dma / 128);
  1891. tx_ctx.qlen = ring->count;
  1892. tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FDIR_ENABLED |
  1893. I40E_FLAG_FDIR_ATR_ENABLED));
  1894. /* As part of VSI creation/update, FW allocates certain
  1895. * Tx arbitration queue sets for each TC enabled for
  1896. * the VSI. The FW returns the handles to these queue
  1897. * sets as part of the response buffer to Add VSI,
  1898. * Update VSI, etc. AQ commands. It is expected that
  1899. * these queue set handles be associated with the Tx
  1900. * queues by the driver as part of the TX queue context
  1901. * initialization. This has to be done regardless of
  1902. * DCB as by default everything is mapped to TC0.
  1903. */
  1904. tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
  1905. tx_ctx.rdylist_act = 0;
  1906. /* clear the context in the HMC */
  1907. err = i40e_clear_lan_tx_queue_context(hw, pf_q);
  1908. if (err) {
  1909. dev_info(&vsi->back->pdev->dev,
  1910. "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
  1911. ring->queue_index, pf_q, err);
  1912. return -ENOMEM;
  1913. }
  1914. /* set the context in the HMC */
  1915. err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
  1916. if (err) {
  1917. dev_info(&vsi->back->pdev->dev,
  1918. "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
  1919. ring->queue_index, pf_q, err);
  1920. return -ENOMEM;
  1921. }
  1922. /* Now associate this queue with this PCI function */
  1923. qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
  1924. qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
  1925. I40E_QTX_CTL_PF_INDX_MASK);
  1926. wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
  1927. i40e_flush(hw);
  1928. clear_bit(__I40E_HANG_CHECK_ARMED, &ring->state);
  1929. /* cache tail off for easier writes later */
  1930. ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
  1931. return 0;
  1932. }
  1933. /**
  1934. * i40e_configure_rx_ring - Configure a receive ring context
  1935. * @ring: The Rx ring to configure
  1936. *
  1937. * Configure the Rx descriptor ring in the HMC context.
  1938. **/
  1939. static int i40e_configure_rx_ring(struct i40e_ring *ring)
  1940. {
  1941. struct i40e_vsi *vsi = ring->vsi;
  1942. u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
  1943. u16 pf_q = vsi->base_queue + ring->queue_index;
  1944. struct i40e_hw *hw = &vsi->back->hw;
  1945. struct i40e_hmc_obj_rxq rx_ctx;
  1946. i40e_status err = 0;
  1947. ring->state = 0;
  1948. /* clear the context structure first */
  1949. memset(&rx_ctx, 0, sizeof(rx_ctx));
  1950. ring->rx_buf_len = vsi->rx_buf_len;
  1951. ring->rx_hdr_len = vsi->rx_hdr_len;
  1952. rx_ctx.dbuff = ring->rx_buf_len >> I40E_RXQ_CTX_DBUFF_SHIFT;
  1953. rx_ctx.hbuff = ring->rx_hdr_len >> I40E_RXQ_CTX_HBUFF_SHIFT;
  1954. rx_ctx.base = (ring->dma / 128);
  1955. rx_ctx.qlen = ring->count;
  1956. if (vsi->back->flags & I40E_FLAG_16BYTE_RX_DESC_ENABLED) {
  1957. set_ring_16byte_desc_enabled(ring);
  1958. rx_ctx.dsize = 0;
  1959. } else {
  1960. rx_ctx.dsize = 1;
  1961. }
  1962. rx_ctx.dtype = vsi->dtype;
  1963. if (vsi->dtype) {
  1964. set_ring_ps_enabled(ring);
  1965. rx_ctx.hsplit_0 = I40E_RX_SPLIT_L2 |
  1966. I40E_RX_SPLIT_IP |
  1967. I40E_RX_SPLIT_TCP_UDP |
  1968. I40E_RX_SPLIT_SCTP;
  1969. } else {
  1970. rx_ctx.hsplit_0 = 0;
  1971. }
  1972. rx_ctx.rxmax = min_t(u16, vsi->max_frame,
  1973. (chain_len * ring->rx_buf_len));
  1974. rx_ctx.tphrdesc_ena = 1;
  1975. rx_ctx.tphwdesc_ena = 1;
  1976. rx_ctx.tphdata_ena = 1;
  1977. rx_ctx.tphhead_ena = 1;
  1978. if (hw->revision_id == 0)
  1979. rx_ctx.lrxqthresh = 0;
  1980. else
  1981. rx_ctx.lrxqthresh = 2;
  1982. rx_ctx.crcstrip = 1;
  1983. rx_ctx.l2tsel = 1;
  1984. rx_ctx.showiv = 1;
  1985. /* clear the context in the HMC */
  1986. err = i40e_clear_lan_rx_queue_context(hw, pf_q);
  1987. if (err) {
  1988. dev_info(&vsi->back->pdev->dev,
  1989. "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
  1990. ring->queue_index, pf_q, err);
  1991. return -ENOMEM;
  1992. }
  1993. /* set the context in the HMC */
  1994. err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
  1995. if (err) {
  1996. dev_info(&vsi->back->pdev->dev,
  1997. "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
  1998. ring->queue_index, pf_q, err);
  1999. return -ENOMEM;
  2000. }
  2001. /* cache tail for quicker writes, and clear the reg before use */
  2002. ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
  2003. writel(0, ring->tail);
  2004. i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
  2005. return 0;
  2006. }
  2007. /**
  2008. * i40e_vsi_configure_tx - Configure the VSI for Tx
  2009. * @vsi: VSI structure describing this set of rings and resources
  2010. *
  2011. * Configure the Tx VSI for operation.
  2012. **/
  2013. static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
  2014. {
  2015. int err = 0;
  2016. u16 i;
  2017. for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
  2018. err = i40e_configure_tx_ring(vsi->tx_rings[i]);
  2019. return err;
  2020. }
  2021. /**
  2022. * i40e_vsi_configure_rx - Configure the VSI for Rx
  2023. * @vsi: the VSI being configured
  2024. *
  2025. * Configure the Rx VSI for operation.
  2026. **/
  2027. static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
  2028. {
  2029. int err = 0;
  2030. u16 i;
  2031. if (vsi->netdev && (vsi->netdev->mtu > ETH_DATA_LEN))
  2032. vsi->max_frame = vsi->netdev->mtu + ETH_HLEN
  2033. + ETH_FCS_LEN + VLAN_HLEN;
  2034. else
  2035. vsi->max_frame = I40E_RXBUFFER_2048;
  2036. /* figure out correct receive buffer length */
  2037. switch (vsi->back->flags & (I40E_FLAG_RX_1BUF_ENABLED |
  2038. I40E_FLAG_RX_PS_ENABLED)) {
  2039. case I40E_FLAG_RX_1BUF_ENABLED:
  2040. vsi->rx_hdr_len = 0;
  2041. vsi->rx_buf_len = vsi->max_frame;
  2042. vsi->dtype = I40E_RX_DTYPE_NO_SPLIT;
  2043. break;
  2044. case I40E_FLAG_RX_PS_ENABLED:
  2045. vsi->rx_hdr_len = I40E_RX_HDR_SIZE;
  2046. vsi->rx_buf_len = I40E_RXBUFFER_2048;
  2047. vsi->dtype = I40E_RX_DTYPE_HEADER_SPLIT;
  2048. break;
  2049. default:
  2050. vsi->rx_hdr_len = I40E_RX_HDR_SIZE;
  2051. vsi->rx_buf_len = I40E_RXBUFFER_2048;
  2052. vsi->dtype = I40E_RX_DTYPE_SPLIT_ALWAYS;
  2053. break;
  2054. }
  2055. /* round up for the chip's needs */
  2056. vsi->rx_hdr_len = ALIGN(vsi->rx_hdr_len,
  2057. (1 << I40E_RXQ_CTX_HBUFF_SHIFT));
  2058. vsi->rx_buf_len = ALIGN(vsi->rx_buf_len,
  2059. (1 << I40E_RXQ_CTX_DBUFF_SHIFT));
  2060. /* set up individual rings */
  2061. for (i = 0; i < vsi->num_queue_pairs && !err; i++)
  2062. err = i40e_configure_rx_ring(vsi->rx_rings[i]);
  2063. return err;
  2064. }
  2065. /**
  2066. * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
  2067. * @vsi: ptr to the VSI
  2068. **/
  2069. static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
  2070. {
  2071. u16 qoffset, qcount;
  2072. int i, n;
  2073. if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED))
  2074. return;
  2075. for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
  2076. if (!(vsi->tc_config.enabled_tc & (1 << n)))
  2077. continue;
  2078. qoffset = vsi->tc_config.tc_info[n].qoffset;
  2079. qcount = vsi->tc_config.tc_info[n].qcount;
  2080. for (i = qoffset; i < (qoffset + qcount); i++) {
  2081. struct i40e_ring *rx_ring = vsi->rx_rings[i];
  2082. struct i40e_ring *tx_ring = vsi->tx_rings[i];
  2083. rx_ring->dcb_tc = n;
  2084. tx_ring->dcb_tc = n;
  2085. }
  2086. }
  2087. }
  2088. /**
  2089. * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
  2090. * @vsi: ptr to the VSI
  2091. **/
  2092. static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
  2093. {
  2094. if (vsi->netdev)
  2095. i40e_set_rx_mode(vsi->netdev);
  2096. }
  2097. /**
  2098. * i40e_vsi_configure - Set up the VSI for action
  2099. * @vsi: the VSI being configured
  2100. **/
  2101. static int i40e_vsi_configure(struct i40e_vsi *vsi)
  2102. {
  2103. int err;
  2104. i40e_set_vsi_rx_mode(vsi);
  2105. i40e_restore_vlan(vsi);
  2106. i40e_vsi_config_dcb_rings(vsi);
  2107. err = i40e_vsi_configure_tx(vsi);
  2108. if (!err)
  2109. err = i40e_vsi_configure_rx(vsi);
  2110. return err;
  2111. }
  2112. /**
  2113. * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
  2114. * @vsi: the VSI being configured
  2115. **/
  2116. static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
  2117. {
  2118. struct i40e_pf *pf = vsi->back;
  2119. struct i40e_q_vector *q_vector;
  2120. struct i40e_hw *hw = &pf->hw;
  2121. u16 vector;
  2122. int i, q;
  2123. u32 val;
  2124. u32 qp;
  2125. /* The interrupt indexing is offset by 1 in the PFINT_ITRn
  2126. * and PFINT_LNKLSTn registers, e.g.:
  2127. * PFINT_ITRn[0..n-1] gets msix-1..msix-n (qpair interrupts)
  2128. */
  2129. qp = vsi->base_queue;
  2130. vector = vsi->base_vector;
  2131. for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
  2132. q_vector = vsi->q_vectors[i];
  2133. q_vector->rx.itr = ITR_TO_REG(vsi->rx_itr_setting);
  2134. q_vector->rx.latency_range = I40E_LOW_LATENCY;
  2135. wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
  2136. q_vector->rx.itr);
  2137. q_vector->tx.itr = ITR_TO_REG(vsi->tx_itr_setting);
  2138. q_vector->tx.latency_range = I40E_LOW_LATENCY;
  2139. wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
  2140. q_vector->tx.itr);
  2141. /* Linked list for the queuepairs assigned to this vector */
  2142. wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
  2143. for (q = 0; q < q_vector->num_ringpairs; q++) {
  2144. val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
  2145. (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
  2146. (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
  2147. (qp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
  2148. (I40E_QUEUE_TYPE_TX
  2149. << I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
  2150. wr32(hw, I40E_QINT_RQCTL(qp), val);
  2151. val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  2152. (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
  2153. (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
  2154. ((qp+1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT)|
  2155. (I40E_QUEUE_TYPE_RX
  2156. << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
  2157. /* Terminate the linked list */
  2158. if (q == (q_vector->num_ringpairs - 1))
  2159. val |= (I40E_QUEUE_END_OF_LIST
  2160. << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
  2161. wr32(hw, I40E_QINT_TQCTL(qp), val);
  2162. qp++;
  2163. }
  2164. }
  2165. i40e_flush(hw);
  2166. }
  2167. /**
  2168. * i40e_enable_misc_int_causes - enable the non-queue interrupts
  2169. * @hw: ptr to the hardware info
  2170. **/
  2171. static void i40e_enable_misc_int_causes(struct i40e_hw *hw)
  2172. {
  2173. u32 val;
  2174. /* clear things first */
  2175. wr32(hw, I40E_PFINT_ICR0_ENA, 0); /* disable all */
  2176. rd32(hw, I40E_PFINT_ICR0); /* read to clear */
  2177. val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK |
  2178. I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK |
  2179. I40E_PFINT_ICR0_ENA_GRST_MASK |
  2180. I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
  2181. I40E_PFINT_ICR0_ENA_GPIO_MASK |
  2182. I40E_PFINT_ICR0_ENA_STORM_DETECT_MASK |
  2183. I40E_PFINT_ICR0_ENA_HMC_ERR_MASK |
  2184. I40E_PFINT_ICR0_ENA_VFLR_MASK |
  2185. I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
  2186. wr32(hw, I40E_PFINT_ICR0_ENA, val);
  2187. /* SW_ITR_IDX = 0, but don't change INTENA */
  2188. wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
  2189. I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
  2190. /* OTHER_ITR_IDX = 0 */
  2191. wr32(hw, I40E_PFINT_STAT_CTL0, 0);
  2192. }
  2193. /**
  2194. * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
  2195. * @vsi: the VSI being configured
  2196. **/
  2197. static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
  2198. {
  2199. struct i40e_q_vector *q_vector = vsi->q_vectors[0];
  2200. struct i40e_pf *pf = vsi->back;
  2201. struct i40e_hw *hw = &pf->hw;
  2202. u32 val;
  2203. /* set the ITR configuration */
  2204. q_vector->rx.itr = ITR_TO_REG(vsi->rx_itr_setting);
  2205. q_vector->rx.latency_range = I40E_LOW_LATENCY;
  2206. wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.itr);
  2207. q_vector->tx.itr = ITR_TO_REG(vsi->tx_itr_setting);
  2208. q_vector->tx.latency_range = I40E_LOW_LATENCY;
  2209. wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.itr);
  2210. i40e_enable_misc_int_causes(hw);
  2211. /* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
  2212. wr32(hw, I40E_PFINT_LNKLST0, 0);
  2213. /* Associate the queue pair to the vector and enable the q int */
  2214. val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
  2215. (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
  2216. (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
  2217. wr32(hw, I40E_QINT_RQCTL(0), val);
  2218. val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  2219. (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
  2220. (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
  2221. wr32(hw, I40E_QINT_TQCTL(0), val);
  2222. i40e_flush(hw);
  2223. }
  2224. /**
  2225. * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
  2226. * @pf: board private structure
  2227. **/
  2228. void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf)
  2229. {
  2230. struct i40e_hw *hw = &pf->hw;
  2231. u32 val;
  2232. val = I40E_PFINT_DYN_CTL0_INTENA_MASK |
  2233. I40E_PFINT_DYN_CTL0_CLEARPBA_MASK |
  2234. (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
  2235. wr32(hw, I40E_PFINT_DYN_CTL0, val);
  2236. i40e_flush(hw);
  2237. }
  2238. /**
  2239. * i40e_irq_dynamic_enable - Enable default interrupt generation settings
  2240. * @vsi: pointer to a vsi
  2241. * @vector: enable a particular Hw Interrupt vector
  2242. **/
  2243. void i40e_irq_dynamic_enable(struct i40e_vsi *vsi, int vector)
  2244. {
  2245. struct i40e_pf *pf = vsi->back;
  2246. struct i40e_hw *hw = &pf->hw;
  2247. u32 val;
  2248. val = I40E_PFINT_DYN_CTLN_INTENA_MASK |
  2249. I40E_PFINT_DYN_CTLN_CLEARPBA_MASK |
  2250. (I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
  2251. wr32(hw, I40E_PFINT_DYN_CTLN(vector - 1), val);
  2252. /* skip the flush */
  2253. }
  2254. /**
  2255. * i40e_msix_clean_rings - MSIX mode Interrupt Handler
  2256. * @irq: interrupt number
  2257. * @data: pointer to a q_vector
  2258. **/
  2259. static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
  2260. {
  2261. struct i40e_q_vector *q_vector = data;
  2262. if (!q_vector->tx.ring && !q_vector->rx.ring)
  2263. return IRQ_HANDLED;
  2264. napi_schedule(&q_vector->napi);
  2265. return IRQ_HANDLED;
  2266. }
  2267. /**
  2268. * i40e_fdir_clean_rings - Interrupt Handler for FDIR rings
  2269. * @irq: interrupt number
  2270. * @data: pointer to a q_vector
  2271. **/
  2272. static irqreturn_t i40e_fdir_clean_rings(int irq, void *data)
  2273. {
  2274. struct i40e_q_vector *q_vector = data;
  2275. if (!q_vector->tx.ring && !q_vector->rx.ring)
  2276. return IRQ_HANDLED;
  2277. pr_info("fdir ring cleaning needed\n");
  2278. return IRQ_HANDLED;
  2279. }
  2280. /**
  2281. * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
  2282. * @vsi: the VSI being configured
  2283. * @basename: name for the vector
  2284. *
  2285. * Allocates MSI-X vectors and requests interrupts from the kernel.
  2286. **/
  2287. static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
  2288. {
  2289. int q_vectors = vsi->num_q_vectors;
  2290. struct i40e_pf *pf = vsi->back;
  2291. int base = vsi->base_vector;
  2292. int rx_int_idx = 0;
  2293. int tx_int_idx = 0;
  2294. int vector, err;
  2295. for (vector = 0; vector < q_vectors; vector++) {
  2296. struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
  2297. if (q_vector->tx.ring && q_vector->rx.ring) {
  2298. snprintf(q_vector->name, sizeof(q_vector->name) - 1,
  2299. "%s-%s-%d", basename, "TxRx", rx_int_idx++);
  2300. tx_int_idx++;
  2301. } else if (q_vector->rx.ring) {
  2302. snprintf(q_vector->name, sizeof(q_vector->name) - 1,
  2303. "%s-%s-%d", basename, "rx", rx_int_idx++);
  2304. } else if (q_vector->tx.ring) {
  2305. snprintf(q_vector->name, sizeof(q_vector->name) - 1,
  2306. "%s-%s-%d", basename, "tx", tx_int_idx++);
  2307. } else {
  2308. /* skip this unused q_vector */
  2309. continue;
  2310. }
  2311. err = request_irq(pf->msix_entries[base + vector].vector,
  2312. vsi->irq_handler,
  2313. 0,
  2314. q_vector->name,
  2315. q_vector);
  2316. if (err) {
  2317. dev_info(&pf->pdev->dev,
  2318. "%s: request_irq failed, error: %d\n",
  2319. __func__, err);
  2320. goto free_queue_irqs;
  2321. }
  2322. /* assign the mask for this irq */
  2323. irq_set_affinity_hint(pf->msix_entries[base + vector].vector,
  2324. &q_vector->affinity_mask);
  2325. }
  2326. return 0;
  2327. free_queue_irqs:
  2328. while (vector) {
  2329. vector--;
  2330. irq_set_affinity_hint(pf->msix_entries[base + vector].vector,
  2331. NULL);
  2332. free_irq(pf->msix_entries[base + vector].vector,
  2333. &(vsi->q_vectors[vector]));
  2334. }
  2335. return err;
  2336. }
  2337. /**
  2338. * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
  2339. * @vsi: the VSI being un-configured
  2340. **/
  2341. static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
  2342. {
  2343. struct i40e_pf *pf = vsi->back;
  2344. struct i40e_hw *hw = &pf->hw;
  2345. int base = vsi->base_vector;
  2346. int i;
  2347. for (i = 0; i < vsi->num_queue_pairs; i++) {
  2348. wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), 0);
  2349. wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), 0);
  2350. }
  2351. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  2352. for (i = vsi->base_vector;
  2353. i < (vsi->num_q_vectors + vsi->base_vector); i++)
  2354. wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
  2355. i40e_flush(hw);
  2356. for (i = 0; i < vsi->num_q_vectors; i++)
  2357. synchronize_irq(pf->msix_entries[i + base].vector);
  2358. } else {
  2359. /* Legacy and MSI mode - this stops all interrupt handling */
  2360. wr32(hw, I40E_PFINT_ICR0_ENA, 0);
  2361. wr32(hw, I40E_PFINT_DYN_CTL0, 0);
  2362. i40e_flush(hw);
  2363. synchronize_irq(pf->pdev->irq);
  2364. }
  2365. }
  2366. /**
  2367. * i40e_vsi_enable_irq - Enable IRQ for the given VSI
  2368. * @vsi: the VSI being configured
  2369. **/
  2370. static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
  2371. {
  2372. struct i40e_pf *pf = vsi->back;
  2373. int i;
  2374. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  2375. for (i = vsi->base_vector;
  2376. i < (vsi->num_q_vectors + vsi->base_vector); i++)
  2377. i40e_irq_dynamic_enable(vsi, i);
  2378. } else {
  2379. i40e_irq_dynamic_enable_icr0(pf);
  2380. }
  2381. i40e_flush(&pf->hw);
  2382. return 0;
  2383. }
  2384. /**
  2385. * i40e_stop_misc_vector - Stop the vector that handles non-queue events
  2386. * @pf: board private structure
  2387. **/
  2388. static void i40e_stop_misc_vector(struct i40e_pf *pf)
  2389. {
  2390. /* Disable ICR 0 */
  2391. wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
  2392. i40e_flush(&pf->hw);
  2393. }
  2394. /**
  2395. * i40e_intr - MSI/Legacy and non-queue interrupt handler
  2396. * @irq: interrupt number
  2397. * @data: pointer to a q_vector
  2398. *
  2399. * This is the handler used for all MSI/Legacy interrupts, and deals
  2400. * with both queue and non-queue interrupts. This is also used in
  2401. * MSIX mode to handle the non-queue interrupts.
  2402. **/
  2403. static irqreturn_t i40e_intr(int irq, void *data)
  2404. {
  2405. struct i40e_pf *pf = (struct i40e_pf *)data;
  2406. struct i40e_hw *hw = &pf->hw;
  2407. u32 icr0, icr0_remaining;
  2408. u32 val, ena_mask;
  2409. icr0 = rd32(hw, I40E_PFINT_ICR0);
  2410. val = rd32(hw, I40E_PFINT_DYN_CTL0);
  2411. val = val | I40E_PFINT_DYN_CTL0_CLEARPBA_MASK;
  2412. wr32(hw, I40E_PFINT_DYN_CTL0, val);
  2413. /* if sharing a legacy IRQ, we might get called w/o an intr pending */
  2414. if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
  2415. return IRQ_NONE;
  2416. ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
  2417. /* if interrupt but no bits showing, must be SWINT */
  2418. if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
  2419. (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
  2420. pf->sw_int_count++;
  2421. /* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
  2422. if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
  2423. /* temporarily disable queue cause for NAPI processing */
  2424. u32 qval = rd32(hw, I40E_QINT_RQCTL(0));
  2425. qval &= ~I40E_QINT_RQCTL_CAUSE_ENA_MASK;
  2426. wr32(hw, I40E_QINT_RQCTL(0), qval);
  2427. qval = rd32(hw, I40E_QINT_TQCTL(0));
  2428. qval &= ~I40E_QINT_TQCTL_CAUSE_ENA_MASK;
  2429. wr32(hw, I40E_QINT_TQCTL(0), qval);
  2430. if (!test_bit(__I40E_DOWN, &pf->state))
  2431. napi_schedule(&pf->vsi[pf->lan_vsi]->q_vectors[0]->napi);
  2432. }
  2433. if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
  2434. ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
  2435. set_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
  2436. }
  2437. if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
  2438. ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
  2439. set_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
  2440. }
  2441. if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
  2442. ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
  2443. set_bit(__I40E_VFLR_EVENT_PENDING, &pf->state);
  2444. }
  2445. if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
  2446. if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
  2447. set_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
  2448. ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
  2449. val = rd32(hw, I40E_GLGEN_RSTAT);
  2450. val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
  2451. >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
  2452. if (val == I40E_RESET_CORER)
  2453. pf->corer_count++;
  2454. else if (val == I40E_RESET_GLOBR)
  2455. pf->globr_count++;
  2456. else if (val == I40E_RESET_EMPR)
  2457. pf->empr_count++;
  2458. }
  2459. if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
  2460. icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
  2461. dev_info(&pf->pdev->dev, "HMC error interrupt\n");
  2462. }
  2463. /* If a critical error is pending we have no choice but to reset the
  2464. * device.
  2465. * Report and mask out any remaining unexpected interrupts.
  2466. */
  2467. icr0_remaining = icr0 & ena_mask;
  2468. if (icr0_remaining) {
  2469. dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
  2470. icr0_remaining);
  2471. if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
  2472. (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
  2473. (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK) ||
  2474. (icr0_remaining & I40E_PFINT_ICR0_MAL_DETECT_MASK)) {
  2475. dev_info(&pf->pdev->dev, "device will be reset\n");
  2476. set_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
  2477. i40e_service_event_schedule(pf);
  2478. }
  2479. ena_mask &= ~icr0_remaining;
  2480. }
  2481. /* re-enable interrupt causes */
  2482. wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
  2483. if (!test_bit(__I40E_DOWN, &pf->state)) {
  2484. i40e_service_event_schedule(pf);
  2485. i40e_irq_dynamic_enable_icr0(pf);
  2486. }
  2487. return IRQ_HANDLED;
  2488. }
  2489. /**
  2490. * i40e_map_vector_to_qp - Assigns the queue pair to the vector
  2491. * @vsi: the VSI being configured
  2492. * @v_idx: vector index
  2493. * @qp_idx: queue pair index
  2494. **/
  2495. static void map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
  2496. {
  2497. struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
  2498. struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
  2499. struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
  2500. tx_ring->q_vector = q_vector;
  2501. tx_ring->next = q_vector->tx.ring;
  2502. q_vector->tx.ring = tx_ring;
  2503. q_vector->tx.count++;
  2504. rx_ring->q_vector = q_vector;
  2505. rx_ring->next = q_vector->rx.ring;
  2506. q_vector->rx.ring = rx_ring;
  2507. q_vector->rx.count++;
  2508. }
  2509. /**
  2510. * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
  2511. * @vsi: the VSI being configured
  2512. *
  2513. * This function maps descriptor rings to the queue-specific vectors
  2514. * we were allotted through the MSI-X enabling code. Ideally, we'd have
  2515. * one vector per queue pair, but on a constrained vector budget, we
  2516. * group the queue pairs as "efficiently" as possible.
  2517. **/
  2518. static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
  2519. {
  2520. int qp_remaining = vsi->num_queue_pairs;
  2521. int q_vectors = vsi->num_q_vectors;
  2522. int num_ringpairs;
  2523. int v_start = 0;
  2524. int qp_idx = 0;
  2525. /* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
  2526. * group them so there are multiple queues per vector.
  2527. */
  2528. for (; v_start < q_vectors && qp_remaining; v_start++) {
  2529. struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
  2530. num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
  2531. q_vector->num_ringpairs = num_ringpairs;
  2532. q_vector->rx.count = 0;
  2533. q_vector->tx.count = 0;
  2534. q_vector->rx.ring = NULL;
  2535. q_vector->tx.ring = NULL;
  2536. while (num_ringpairs--) {
  2537. map_vector_to_qp(vsi, v_start, qp_idx);
  2538. qp_idx++;
  2539. qp_remaining--;
  2540. }
  2541. }
  2542. }
  2543. /**
  2544. * i40e_vsi_request_irq - Request IRQ from the OS
  2545. * @vsi: the VSI being configured
  2546. * @basename: name for the vector
  2547. **/
  2548. static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
  2549. {
  2550. struct i40e_pf *pf = vsi->back;
  2551. int err;
  2552. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  2553. err = i40e_vsi_request_irq_msix(vsi, basename);
  2554. else if (pf->flags & I40E_FLAG_MSI_ENABLED)
  2555. err = request_irq(pf->pdev->irq, i40e_intr, 0,
  2556. pf->misc_int_name, pf);
  2557. else
  2558. err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
  2559. pf->misc_int_name, pf);
  2560. if (err)
  2561. dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
  2562. return err;
  2563. }
  2564. #ifdef CONFIG_NET_POLL_CONTROLLER
  2565. /**
  2566. * i40e_netpoll - A Polling 'interrupt'handler
  2567. * @netdev: network interface device structure
  2568. *
  2569. * This is used by netconsole to send skbs without having to re-enable
  2570. * interrupts. It's not called while the normal interrupt routine is executing.
  2571. **/
  2572. static void i40e_netpoll(struct net_device *netdev)
  2573. {
  2574. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2575. struct i40e_vsi *vsi = np->vsi;
  2576. struct i40e_pf *pf = vsi->back;
  2577. int i;
  2578. /* if interface is down do nothing */
  2579. if (test_bit(__I40E_DOWN, &vsi->state))
  2580. return;
  2581. pf->flags |= I40E_FLAG_IN_NETPOLL;
  2582. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  2583. for (i = 0; i < vsi->num_q_vectors; i++)
  2584. i40e_msix_clean_rings(0, vsi->q_vectors[i]);
  2585. } else {
  2586. i40e_intr(pf->pdev->irq, netdev);
  2587. }
  2588. pf->flags &= ~I40E_FLAG_IN_NETPOLL;
  2589. }
  2590. #endif
  2591. /**
  2592. * i40e_vsi_control_tx - Start or stop a VSI's rings
  2593. * @vsi: the VSI being configured
  2594. * @enable: start or stop the rings
  2595. **/
  2596. static int i40e_vsi_control_tx(struct i40e_vsi *vsi, bool enable)
  2597. {
  2598. struct i40e_pf *pf = vsi->back;
  2599. struct i40e_hw *hw = &pf->hw;
  2600. int i, j, pf_q;
  2601. u32 tx_reg;
  2602. pf_q = vsi->base_queue;
  2603. for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
  2604. j = 1000;
  2605. do {
  2606. usleep_range(1000, 2000);
  2607. tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
  2608. } while (j-- && ((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT)
  2609. ^ (tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT)) & 1);
  2610. if (enable) {
  2611. /* is STAT set ? */
  2612. if ((tx_reg & I40E_QTX_ENA_QENA_STAT_MASK)) {
  2613. dev_info(&pf->pdev->dev,
  2614. "Tx %d already enabled\n", i);
  2615. continue;
  2616. }
  2617. } else {
  2618. /* is !STAT set ? */
  2619. if (!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK)) {
  2620. dev_info(&pf->pdev->dev,
  2621. "Tx %d already disabled\n", i);
  2622. continue;
  2623. }
  2624. }
  2625. /* turn on/off the queue */
  2626. if (enable)
  2627. tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK |
  2628. I40E_QTX_ENA_QENA_STAT_MASK;
  2629. else
  2630. tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
  2631. wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
  2632. /* wait for the change to finish */
  2633. for (j = 0; j < 10; j++) {
  2634. tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
  2635. if (enable) {
  2636. if ((tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
  2637. break;
  2638. } else {
  2639. if (!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
  2640. break;
  2641. }
  2642. udelay(10);
  2643. }
  2644. if (j >= 10) {
  2645. dev_info(&pf->pdev->dev, "Tx ring %d %sable timeout\n",
  2646. pf_q, (enable ? "en" : "dis"));
  2647. return -ETIMEDOUT;
  2648. }
  2649. }
  2650. if (hw->revision_id == 0)
  2651. mdelay(50);
  2652. return 0;
  2653. }
  2654. /**
  2655. * i40e_vsi_control_rx - Start or stop a VSI's rings
  2656. * @vsi: the VSI being configured
  2657. * @enable: start or stop the rings
  2658. **/
  2659. static int i40e_vsi_control_rx(struct i40e_vsi *vsi, bool enable)
  2660. {
  2661. struct i40e_pf *pf = vsi->back;
  2662. struct i40e_hw *hw = &pf->hw;
  2663. int i, j, pf_q;
  2664. u32 rx_reg;
  2665. pf_q = vsi->base_queue;
  2666. for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
  2667. j = 1000;
  2668. do {
  2669. usleep_range(1000, 2000);
  2670. rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
  2671. } while (j-- && ((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT)
  2672. ^ (rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT)) & 1);
  2673. if (enable) {
  2674. /* is STAT set ? */
  2675. if ((rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
  2676. continue;
  2677. } else {
  2678. /* is !STAT set ? */
  2679. if (!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
  2680. continue;
  2681. }
  2682. /* turn on/off the queue */
  2683. if (enable)
  2684. rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK |
  2685. I40E_QRX_ENA_QENA_STAT_MASK;
  2686. else
  2687. rx_reg &= ~(I40E_QRX_ENA_QENA_REQ_MASK |
  2688. I40E_QRX_ENA_QENA_STAT_MASK);
  2689. wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
  2690. /* wait for the change to finish */
  2691. for (j = 0; j < 10; j++) {
  2692. rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
  2693. if (enable) {
  2694. if ((rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
  2695. break;
  2696. } else {
  2697. if (!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
  2698. break;
  2699. }
  2700. udelay(10);
  2701. }
  2702. if (j >= 10) {
  2703. dev_info(&pf->pdev->dev, "Rx ring %d %sable timeout\n",
  2704. pf_q, (enable ? "en" : "dis"));
  2705. return -ETIMEDOUT;
  2706. }
  2707. }
  2708. return 0;
  2709. }
  2710. /**
  2711. * i40e_vsi_control_rings - Start or stop a VSI's rings
  2712. * @vsi: the VSI being configured
  2713. * @enable: start or stop the rings
  2714. **/
  2715. static int i40e_vsi_control_rings(struct i40e_vsi *vsi, bool request)
  2716. {
  2717. int ret;
  2718. /* do rx first for enable and last for disable */
  2719. if (request) {
  2720. ret = i40e_vsi_control_rx(vsi, request);
  2721. if (ret)
  2722. return ret;
  2723. ret = i40e_vsi_control_tx(vsi, request);
  2724. } else {
  2725. ret = i40e_vsi_control_tx(vsi, request);
  2726. if (ret)
  2727. return ret;
  2728. ret = i40e_vsi_control_rx(vsi, request);
  2729. }
  2730. return ret;
  2731. }
  2732. /**
  2733. * i40e_vsi_free_irq - Free the irq association with the OS
  2734. * @vsi: the VSI being configured
  2735. **/
  2736. static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
  2737. {
  2738. struct i40e_pf *pf = vsi->back;
  2739. struct i40e_hw *hw = &pf->hw;
  2740. int base = vsi->base_vector;
  2741. u32 val, qp;
  2742. int i;
  2743. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  2744. if (!vsi->q_vectors)
  2745. return;
  2746. for (i = 0; i < vsi->num_q_vectors; i++) {
  2747. u16 vector = i + base;
  2748. /* free only the irqs that were actually requested */
  2749. if (vsi->q_vectors[i]->num_ringpairs == 0)
  2750. continue;
  2751. /* clear the affinity_mask in the IRQ descriptor */
  2752. irq_set_affinity_hint(pf->msix_entries[vector].vector,
  2753. NULL);
  2754. free_irq(pf->msix_entries[vector].vector,
  2755. vsi->q_vectors[i]);
  2756. /* Tear down the interrupt queue link list
  2757. *
  2758. * We know that they come in pairs and always
  2759. * the Rx first, then the Tx. To clear the
  2760. * link list, stick the EOL value into the
  2761. * next_q field of the registers.
  2762. */
  2763. val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
  2764. qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
  2765. >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
  2766. val |= I40E_QUEUE_END_OF_LIST
  2767. << I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
  2768. wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
  2769. while (qp != I40E_QUEUE_END_OF_LIST) {
  2770. u32 next;
  2771. val = rd32(hw, I40E_QINT_RQCTL(qp));
  2772. val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
  2773. I40E_QINT_RQCTL_MSIX0_INDX_MASK |
  2774. I40E_QINT_RQCTL_CAUSE_ENA_MASK |
  2775. I40E_QINT_RQCTL_INTEVENT_MASK);
  2776. val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
  2777. I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
  2778. wr32(hw, I40E_QINT_RQCTL(qp), val);
  2779. val = rd32(hw, I40E_QINT_TQCTL(qp));
  2780. next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
  2781. >> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
  2782. val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
  2783. I40E_QINT_TQCTL_MSIX0_INDX_MASK |
  2784. I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  2785. I40E_QINT_TQCTL_INTEVENT_MASK);
  2786. val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
  2787. I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
  2788. wr32(hw, I40E_QINT_TQCTL(qp), val);
  2789. qp = next;
  2790. }
  2791. }
  2792. } else {
  2793. free_irq(pf->pdev->irq, pf);
  2794. val = rd32(hw, I40E_PFINT_LNKLST0);
  2795. qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
  2796. >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
  2797. val |= I40E_QUEUE_END_OF_LIST
  2798. << I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
  2799. wr32(hw, I40E_PFINT_LNKLST0, val);
  2800. val = rd32(hw, I40E_QINT_RQCTL(qp));
  2801. val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
  2802. I40E_QINT_RQCTL_MSIX0_INDX_MASK |
  2803. I40E_QINT_RQCTL_CAUSE_ENA_MASK |
  2804. I40E_QINT_RQCTL_INTEVENT_MASK);
  2805. val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
  2806. I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
  2807. wr32(hw, I40E_QINT_RQCTL(qp), val);
  2808. val = rd32(hw, I40E_QINT_TQCTL(qp));
  2809. val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
  2810. I40E_QINT_TQCTL_MSIX0_INDX_MASK |
  2811. I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  2812. I40E_QINT_TQCTL_INTEVENT_MASK);
  2813. val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
  2814. I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
  2815. wr32(hw, I40E_QINT_TQCTL(qp), val);
  2816. }
  2817. }
  2818. /**
  2819. * i40e_free_q_vector - Free memory allocated for specific interrupt vector
  2820. * @vsi: the VSI being configured
  2821. * @v_idx: Index of vector to be freed
  2822. *
  2823. * This function frees the memory allocated to the q_vector. In addition if
  2824. * NAPI is enabled it will delete any references to the NAPI struct prior
  2825. * to freeing the q_vector.
  2826. **/
  2827. static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
  2828. {
  2829. struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
  2830. struct i40e_ring *ring;
  2831. if (!q_vector)
  2832. return;
  2833. /* disassociate q_vector from rings */
  2834. i40e_for_each_ring(ring, q_vector->tx)
  2835. ring->q_vector = NULL;
  2836. i40e_for_each_ring(ring, q_vector->rx)
  2837. ring->q_vector = NULL;
  2838. /* only VSI w/ an associated netdev is set up w/ NAPI */
  2839. if (vsi->netdev)
  2840. netif_napi_del(&q_vector->napi);
  2841. vsi->q_vectors[v_idx] = NULL;
  2842. kfree_rcu(q_vector, rcu);
  2843. }
  2844. /**
  2845. * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
  2846. * @vsi: the VSI being un-configured
  2847. *
  2848. * This frees the memory allocated to the q_vectors and
  2849. * deletes references to the NAPI struct.
  2850. **/
  2851. static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
  2852. {
  2853. int v_idx;
  2854. for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
  2855. i40e_free_q_vector(vsi, v_idx);
  2856. }
  2857. /**
  2858. * i40e_reset_interrupt_capability - Disable interrupt setup in OS
  2859. * @pf: board private structure
  2860. **/
  2861. static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
  2862. {
  2863. /* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
  2864. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  2865. pci_disable_msix(pf->pdev);
  2866. kfree(pf->msix_entries);
  2867. pf->msix_entries = NULL;
  2868. } else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
  2869. pci_disable_msi(pf->pdev);
  2870. }
  2871. pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
  2872. }
  2873. /**
  2874. * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
  2875. * @pf: board private structure
  2876. *
  2877. * We go through and clear interrupt specific resources and reset the structure
  2878. * to pre-load conditions
  2879. **/
  2880. static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
  2881. {
  2882. int i;
  2883. i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
  2884. for (i = 0; i < pf->hw.func_caps.num_vsis; i++)
  2885. if (pf->vsi[i])
  2886. i40e_vsi_free_q_vectors(pf->vsi[i]);
  2887. i40e_reset_interrupt_capability(pf);
  2888. }
  2889. /**
  2890. * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
  2891. * @vsi: the VSI being configured
  2892. **/
  2893. static void i40e_napi_enable_all(struct i40e_vsi *vsi)
  2894. {
  2895. int q_idx;
  2896. if (!vsi->netdev)
  2897. return;
  2898. for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
  2899. napi_enable(&vsi->q_vectors[q_idx]->napi);
  2900. }
  2901. /**
  2902. * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
  2903. * @vsi: the VSI being configured
  2904. **/
  2905. static void i40e_napi_disable_all(struct i40e_vsi *vsi)
  2906. {
  2907. int q_idx;
  2908. if (!vsi->netdev)
  2909. return;
  2910. for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++)
  2911. napi_disable(&vsi->q_vectors[q_idx]->napi);
  2912. }
  2913. /**
  2914. * i40e_quiesce_vsi - Pause a given VSI
  2915. * @vsi: the VSI being paused
  2916. **/
  2917. static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
  2918. {
  2919. if (test_bit(__I40E_DOWN, &vsi->state))
  2920. return;
  2921. set_bit(__I40E_NEEDS_RESTART, &vsi->state);
  2922. if (vsi->netdev && netif_running(vsi->netdev)) {
  2923. vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
  2924. } else {
  2925. set_bit(__I40E_DOWN, &vsi->state);
  2926. i40e_down(vsi);
  2927. }
  2928. }
  2929. /**
  2930. * i40e_unquiesce_vsi - Resume a given VSI
  2931. * @vsi: the VSI being resumed
  2932. **/
  2933. static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
  2934. {
  2935. if (!test_bit(__I40E_NEEDS_RESTART, &vsi->state))
  2936. return;
  2937. clear_bit(__I40E_NEEDS_RESTART, &vsi->state);
  2938. if (vsi->netdev && netif_running(vsi->netdev))
  2939. vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
  2940. else
  2941. i40e_up(vsi); /* this clears the DOWN bit */
  2942. }
  2943. /**
  2944. * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
  2945. * @pf: the PF
  2946. **/
  2947. static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
  2948. {
  2949. int v;
  2950. for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
  2951. if (pf->vsi[v])
  2952. i40e_quiesce_vsi(pf->vsi[v]);
  2953. }
  2954. }
  2955. /**
  2956. * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
  2957. * @pf: the PF
  2958. **/
  2959. static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
  2960. {
  2961. int v;
  2962. for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
  2963. if (pf->vsi[v])
  2964. i40e_unquiesce_vsi(pf->vsi[v]);
  2965. }
  2966. }
  2967. /**
  2968. * i40e_dcb_get_num_tc - Get the number of TCs from DCBx config
  2969. * @dcbcfg: the corresponding DCBx configuration structure
  2970. *
  2971. * Return the number of TCs from given DCBx configuration
  2972. **/
  2973. static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
  2974. {
  2975. u8 num_tc = 0;
  2976. int i;
  2977. /* Scan the ETS Config Priority Table to find
  2978. * traffic class enabled for a given priority
  2979. * and use the traffic class index to get the
  2980. * number of traffic classes enabled
  2981. */
  2982. for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
  2983. if (dcbcfg->etscfg.prioritytable[i] > num_tc)
  2984. num_tc = dcbcfg->etscfg.prioritytable[i];
  2985. }
  2986. /* Traffic class index starts from zero so
  2987. * increment to return the actual count
  2988. */
  2989. return num_tc + 1;
  2990. }
  2991. /**
  2992. * i40e_dcb_get_enabled_tc - Get enabled traffic classes
  2993. * @dcbcfg: the corresponding DCBx configuration structure
  2994. *
  2995. * Query the current DCB configuration and return the number of
  2996. * traffic classes enabled from the given DCBX config
  2997. **/
  2998. static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
  2999. {
  3000. u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
  3001. u8 enabled_tc = 1;
  3002. u8 i;
  3003. for (i = 0; i < num_tc; i++)
  3004. enabled_tc |= 1 << i;
  3005. return enabled_tc;
  3006. }
  3007. /**
  3008. * i40e_pf_get_num_tc - Get enabled traffic classes for PF
  3009. * @pf: PF being queried
  3010. *
  3011. * Return number of traffic classes enabled for the given PF
  3012. **/
  3013. static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
  3014. {
  3015. struct i40e_hw *hw = &pf->hw;
  3016. u8 i, enabled_tc;
  3017. u8 num_tc = 0;
  3018. struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
  3019. /* If DCB is not enabled then always in single TC */
  3020. if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
  3021. return 1;
  3022. /* MFP mode return count of enabled TCs for this PF */
  3023. if (pf->flags & I40E_FLAG_MFP_ENABLED) {
  3024. enabled_tc = pf->hw.func_caps.enabled_tcmap;
  3025. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  3026. if (enabled_tc & (1 << i))
  3027. num_tc++;
  3028. }
  3029. return num_tc;
  3030. }
  3031. /* SFP mode will be enabled for all TCs on port */
  3032. return i40e_dcb_get_num_tc(dcbcfg);
  3033. }
  3034. /**
  3035. * i40e_pf_get_default_tc - Get bitmap for first enabled TC
  3036. * @pf: PF being queried
  3037. *
  3038. * Return a bitmap for first enabled traffic class for this PF.
  3039. **/
  3040. static u8 i40e_pf_get_default_tc(struct i40e_pf *pf)
  3041. {
  3042. u8 enabled_tc = pf->hw.func_caps.enabled_tcmap;
  3043. u8 i = 0;
  3044. if (!enabled_tc)
  3045. return 0x1; /* TC0 */
  3046. /* Find the first enabled TC */
  3047. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  3048. if (enabled_tc & (1 << i))
  3049. break;
  3050. }
  3051. return 1 << i;
  3052. }
  3053. /**
  3054. * i40e_pf_get_pf_tc_map - Get bitmap for enabled traffic classes
  3055. * @pf: PF being queried
  3056. *
  3057. * Return a bitmap for enabled traffic classes for this PF.
  3058. **/
  3059. static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
  3060. {
  3061. /* If DCB is not enabled for this PF then just return default TC */
  3062. if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
  3063. return i40e_pf_get_default_tc(pf);
  3064. /* MFP mode will have enabled TCs set by FW */
  3065. if (pf->flags & I40E_FLAG_MFP_ENABLED)
  3066. return pf->hw.func_caps.enabled_tcmap;
  3067. /* SFP mode we want PF to be enabled for all TCs */
  3068. return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
  3069. }
  3070. /**
  3071. * i40e_vsi_get_bw_info - Query VSI BW Information
  3072. * @vsi: the VSI being queried
  3073. *
  3074. * Returns 0 on success, negative value on failure
  3075. **/
  3076. static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
  3077. {
  3078. struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
  3079. struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
  3080. struct i40e_pf *pf = vsi->back;
  3081. struct i40e_hw *hw = &pf->hw;
  3082. i40e_status aq_ret;
  3083. u32 tc_bw_max;
  3084. int i;
  3085. /* Get the VSI level BW configuration */
  3086. aq_ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
  3087. if (aq_ret) {
  3088. dev_info(&pf->pdev->dev,
  3089. "couldn't get pf vsi bw config, err %d, aq_err %d\n",
  3090. aq_ret, pf->hw.aq.asq_last_status);
  3091. return -EINVAL;
  3092. }
  3093. /* Get the VSI level BW configuration per TC */
  3094. aq_ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
  3095. NULL);
  3096. if (aq_ret) {
  3097. dev_info(&pf->pdev->dev,
  3098. "couldn't get pf vsi ets bw config, err %d, aq_err %d\n",
  3099. aq_ret, pf->hw.aq.asq_last_status);
  3100. return -EINVAL;
  3101. }
  3102. if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
  3103. dev_info(&pf->pdev->dev,
  3104. "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
  3105. bw_config.tc_valid_bits,
  3106. bw_ets_config.tc_valid_bits);
  3107. /* Still continuing */
  3108. }
  3109. vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
  3110. vsi->bw_max_quanta = bw_config.max_bw;
  3111. tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
  3112. (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
  3113. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  3114. vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
  3115. vsi->bw_ets_limit_credits[i] =
  3116. le16_to_cpu(bw_ets_config.credits[i]);
  3117. /* 3 bits out of 4 for each TC */
  3118. vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
  3119. }
  3120. return 0;
  3121. }
  3122. /**
  3123. * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
  3124. * @vsi: the VSI being configured
  3125. * @enabled_tc: TC bitmap
  3126. * @bw_credits: BW shared credits per TC
  3127. *
  3128. * Returns 0 on success, negative value on failure
  3129. **/
  3130. static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
  3131. u8 *bw_share)
  3132. {
  3133. struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
  3134. i40e_status aq_ret;
  3135. int i;
  3136. bw_data.tc_valid_bits = enabled_tc;
  3137. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
  3138. bw_data.tc_bw_credits[i] = bw_share[i];
  3139. aq_ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, vsi->seid, &bw_data,
  3140. NULL);
  3141. if (aq_ret) {
  3142. dev_info(&vsi->back->pdev->dev,
  3143. "%s: AQ command Config VSI BW allocation per TC failed = %d\n",
  3144. __func__, vsi->back->hw.aq.asq_last_status);
  3145. return -EINVAL;
  3146. }
  3147. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
  3148. vsi->info.qs_handle[i] = bw_data.qs_handles[i];
  3149. return 0;
  3150. }
  3151. /**
  3152. * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
  3153. * @vsi: the VSI being configured
  3154. * @enabled_tc: TC map to be enabled
  3155. *
  3156. **/
  3157. static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
  3158. {
  3159. struct net_device *netdev = vsi->netdev;
  3160. struct i40e_pf *pf = vsi->back;
  3161. struct i40e_hw *hw = &pf->hw;
  3162. u8 netdev_tc = 0;
  3163. int i;
  3164. struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
  3165. if (!netdev)
  3166. return;
  3167. if (!enabled_tc) {
  3168. netdev_reset_tc(netdev);
  3169. return;
  3170. }
  3171. /* Set up actual enabled TCs on the VSI */
  3172. if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
  3173. return;
  3174. /* set per TC queues for the VSI */
  3175. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  3176. /* Only set TC queues for enabled tcs
  3177. *
  3178. * e.g. For a VSI that has TC0 and TC3 enabled the
  3179. * enabled_tc bitmap would be 0x00001001; the driver
  3180. * will set the numtc for netdev as 2 that will be
  3181. * referenced by the netdev layer as TC 0 and 1.
  3182. */
  3183. if (vsi->tc_config.enabled_tc & (1 << i))
  3184. netdev_set_tc_queue(netdev,
  3185. vsi->tc_config.tc_info[i].netdev_tc,
  3186. vsi->tc_config.tc_info[i].qcount,
  3187. vsi->tc_config.tc_info[i].qoffset);
  3188. }
  3189. /* Assign UP2TC map for the VSI */
  3190. for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
  3191. /* Get the actual TC# for the UP */
  3192. u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
  3193. /* Get the mapped netdev TC# for the UP */
  3194. netdev_tc = vsi->tc_config.tc_info[ets_tc].netdev_tc;
  3195. netdev_set_prio_tc_map(netdev, i, netdev_tc);
  3196. }
  3197. }
  3198. /**
  3199. * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
  3200. * @vsi: the VSI being configured
  3201. * @ctxt: the ctxt buffer returned from AQ VSI update param command
  3202. **/
  3203. static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
  3204. struct i40e_vsi_context *ctxt)
  3205. {
  3206. /* copy just the sections touched not the entire info
  3207. * since not all sections are valid as returned by
  3208. * update vsi params
  3209. */
  3210. vsi->info.mapping_flags = ctxt->info.mapping_flags;
  3211. memcpy(&vsi->info.queue_mapping,
  3212. &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
  3213. memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
  3214. sizeof(vsi->info.tc_mapping));
  3215. }
  3216. /**
  3217. * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
  3218. * @vsi: VSI to be configured
  3219. * @enabled_tc: TC bitmap
  3220. *
  3221. * This configures a particular VSI for TCs that are mapped to the
  3222. * given TC bitmap. It uses default bandwidth share for TCs across
  3223. * VSIs to configure TC for a particular VSI.
  3224. *
  3225. * NOTE:
  3226. * It is expected that the VSI queues have been quisced before calling
  3227. * this function.
  3228. **/
  3229. static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
  3230. {
  3231. u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
  3232. struct i40e_vsi_context ctxt;
  3233. int ret = 0;
  3234. int i;
  3235. /* Check if enabled_tc is same as existing or new TCs */
  3236. if (vsi->tc_config.enabled_tc == enabled_tc)
  3237. return ret;
  3238. /* Enable ETS TCs with equal BW Share for now across all VSIs */
  3239. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  3240. if (enabled_tc & (1 << i))
  3241. bw_share[i] = 1;
  3242. }
  3243. ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
  3244. if (ret) {
  3245. dev_info(&vsi->back->pdev->dev,
  3246. "Failed configuring TC map %d for VSI %d\n",
  3247. enabled_tc, vsi->seid);
  3248. goto out;
  3249. }
  3250. /* Update Queue Pairs Mapping for currently enabled UPs */
  3251. ctxt.seid = vsi->seid;
  3252. ctxt.pf_num = vsi->back->hw.pf_id;
  3253. ctxt.vf_num = 0;
  3254. ctxt.uplink_seid = vsi->uplink_seid;
  3255. memcpy(&ctxt.info, &vsi->info, sizeof(vsi->info));
  3256. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
  3257. /* Update the VSI after updating the VSI queue-mapping information */
  3258. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  3259. if (ret) {
  3260. dev_info(&vsi->back->pdev->dev,
  3261. "update vsi failed, aq_err=%d\n",
  3262. vsi->back->hw.aq.asq_last_status);
  3263. goto out;
  3264. }
  3265. /* update the local VSI info with updated queue map */
  3266. i40e_vsi_update_queue_map(vsi, &ctxt);
  3267. vsi->info.valid_sections = 0;
  3268. /* Update current VSI BW information */
  3269. ret = i40e_vsi_get_bw_info(vsi);
  3270. if (ret) {
  3271. dev_info(&vsi->back->pdev->dev,
  3272. "Failed updating vsi bw info, aq_err=%d\n",
  3273. vsi->back->hw.aq.asq_last_status);
  3274. goto out;
  3275. }
  3276. /* Update the netdev TC setup */
  3277. i40e_vsi_config_netdev_tc(vsi, enabled_tc);
  3278. out:
  3279. return ret;
  3280. }
  3281. /**
  3282. * i40e_up_complete - Finish the last steps of bringing up a connection
  3283. * @vsi: the VSI being configured
  3284. **/
  3285. static int i40e_up_complete(struct i40e_vsi *vsi)
  3286. {
  3287. struct i40e_pf *pf = vsi->back;
  3288. int err;
  3289. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  3290. i40e_vsi_configure_msix(vsi);
  3291. else
  3292. i40e_configure_msi_and_legacy(vsi);
  3293. /* start rings */
  3294. err = i40e_vsi_control_rings(vsi, true);
  3295. if (err)
  3296. return err;
  3297. clear_bit(__I40E_DOWN, &vsi->state);
  3298. i40e_napi_enable_all(vsi);
  3299. i40e_vsi_enable_irq(vsi);
  3300. if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
  3301. (vsi->netdev)) {
  3302. netdev_info(vsi->netdev, "NIC Link is Up\n");
  3303. netif_tx_start_all_queues(vsi->netdev);
  3304. netif_carrier_on(vsi->netdev);
  3305. } else if (vsi->netdev) {
  3306. netdev_info(vsi->netdev, "NIC Link is Down\n");
  3307. }
  3308. i40e_service_event_schedule(pf);
  3309. return 0;
  3310. }
  3311. /**
  3312. * i40e_vsi_reinit_locked - Reset the VSI
  3313. * @vsi: the VSI being configured
  3314. *
  3315. * Rebuild the ring structs after some configuration
  3316. * has changed, e.g. MTU size.
  3317. **/
  3318. static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
  3319. {
  3320. struct i40e_pf *pf = vsi->back;
  3321. WARN_ON(in_interrupt());
  3322. while (test_and_set_bit(__I40E_CONFIG_BUSY, &pf->state))
  3323. usleep_range(1000, 2000);
  3324. i40e_down(vsi);
  3325. /* Give a VF some time to respond to the reset. The
  3326. * two second wait is based upon the watchdog cycle in
  3327. * the VF driver.
  3328. */
  3329. if (vsi->type == I40E_VSI_SRIOV)
  3330. msleep(2000);
  3331. i40e_up(vsi);
  3332. clear_bit(__I40E_CONFIG_BUSY, &pf->state);
  3333. }
  3334. /**
  3335. * i40e_up - Bring the connection back up after being down
  3336. * @vsi: the VSI being configured
  3337. **/
  3338. int i40e_up(struct i40e_vsi *vsi)
  3339. {
  3340. int err;
  3341. err = i40e_vsi_configure(vsi);
  3342. if (!err)
  3343. err = i40e_up_complete(vsi);
  3344. return err;
  3345. }
  3346. /**
  3347. * i40e_down - Shutdown the connection processing
  3348. * @vsi: the VSI being stopped
  3349. **/
  3350. void i40e_down(struct i40e_vsi *vsi)
  3351. {
  3352. int i;
  3353. /* It is assumed that the caller of this function
  3354. * sets the vsi->state __I40E_DOWN bit.
  3355. */
  3356. if (vsi->netdev) {
  3357. netif_carrier_off(vsi->netdev);
  3358. netif_tx_disable(vsi->netdev);
  3359. }
  3360. i40e_vsi_disable_irq(vsi);
  3361. i40e_vsi_control_rings(vsi, false);
  3362. i40e_napi_disable_all(vsi);
  3363. for (i = 0; i < vsi->num_queue_pairs; i++) {
  3364. i40e_clean_tx_ring(vsi->tx_rings[i]);
  3365. i40e_clean_rx_ring(vsi->rx_rings[i]);
  3366. }
  3367. }
  3368. /**
  3369. * i40e_setup_tc - configure multiple traffic classes
  3370. * @netdev: net device to configure
  3371. * @tc: number of traffic classes to enable
  3372. **/
  3373. static int i40e_setup_tc(struct net_device *netdev, u8 tc)
  3374. {
  3375. struct i40e_netdev_priv *np = netdev_priv(netdev);
  3376. struct i40e_vsi *vsi = np->vsi;
  3377. struct i40e_pf *pf = vsi->back;
  3378. u8 enabled_tc = 0;
  3379. int ret = -EINVAL;
  3380. int i;
  3381. /* Check if DCB enabled to continue */
  3382. if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
  3383. netdev_info(netdev, "DCB is not enabled for adapter\n");
  3384. goto exit;
  3385. }
  3386. /* Check if MFP enabled */
  3387. if (pf->flags & I40E_FLAG_MFP_ENABLED) {
  3388. netdev_info(netdev, "Configuring TC not supported in MFP mode\n");
  3389. goto exit;
  3390. }
  3391. /* Check whether tc count is within enabled limit */
  3392. if (tc > i40e_pf_get_num_tc(pf)) {
  3393. netdev_info(netdev, "TC count greater than enabled on link for adapter\n");
  3394. goto exit;
  3395. }
  3396. /* Generate TC map for number of tc requested */
  3397. for (i = 0; i < tc; i++)
  3398. enabled_tc |= (1 << i);
  3399. /* Requesting same TC configuration as already enabled */
  3400. if (enabled_tc == vsi->tc_config.enabled_tc)
  3401. return 0;
  3402. /* Quiesce VSI queues */
  3403. i40e_quiesce_vsi(vsi);
  3404. /* Configure VSI for enabled TCs */
  3405. ret = i40e_vsi_config_tc(vsi, enabled_tc);
  3406. if (ret) {
  3407. netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
  3408. vsi->seid);
  3409. goto exit;
  3410. }
  3411. /* Unquiesce VSI */
  3412. i40e_unquiesce_vsi(vsi);
  3413. exit:
  3414. return ret;
  3415. }
  3416. /**
  3417. * i40e_open - Called when a network interface is made active
  3418. * @netdev: network interface device structure
  3419. *
  3420. * The open entry point is called when a network interface is made
  3421. * active by the system (IFF_UP). At this point all resources needed
  3422. * for transmit and receive operations are allocated, the interrupt
  3423. * handler is registered with the OS, the netdev watchdog subtask is
  3424. * enabled, and the stack is notified that the interface is ready.
  3425. *
  3426. * Returns 0 on success, negative value on failure
  3427. **/
  3428. static int i40e_open(struct net_device *netdev)
  3429. {
  3430. struct i40e_netdev_priv *np = netdev_priv(netdev);
  3431. struct i40e_vsi *vsi = np->vsi;
  3432. struct i40e_pf *pf = vsi->back;
  3433. char int_name[IFNAMSIZ];
  3434. int err;
  3435. /* disallow open during test */
  3436. if (test_bit(__I40E_TESTING, &pf->state))
  3437. return -EBUSY;
  3438. netif_carrier_off(netdev);
  3439. /* allocate descriptors */
  3440. err = i40e_vsi_setup_tx_resources(vsi);
  3441. if (err)
  3442. goto err_setup_tx;
  3443. err = i40e_vsi_setup_rx_resources(vsi);
  3444. if (err)
  3445. goto err_setup_rx;
  3446. err = i40e_vsi_configure(vsi);
  3447. if (err)
  3448. goto err_setup_rx;
  3449. snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
  3450. dev_driver_string(&pf->pdev->dev), netdev->name);
  3451. err = i40e_vsi_request_irq(vsi, int_name);
  3452. if (err)
  3453. goto err_setup_rx;
  3454. /* Notify the stack of the actual queue counts. */
  3455. err = netif_set_real_num_tx_queues(netdev, pf->num_tx_queues);
  3456. if (err)
  3457. goto err_set_queues;
  3458. err = netif_set_real_num_rx_queues(netdev, pf->num_rx_queues);
  3459. if (err)
  3460. goto err_set_queues;
  3461. err = i40e_up_complete(vsi);
  3462. if (err)
  3463. goto err_up_complete;
  3464. if ((vsi->type == I40E_VSI_MAIN) || (vsi->type == I40E_VSI_VMDQ2)) {
  3465. err = i40e_aq_set_vsi_broadcast(&pf->hw, vsi->seid, true, NULL);
  3466. if (err)
  3467. netdev_info(netdev,
  3468. "couldn't set broadcast err %d aq_err %d\n",
  3469. err, pf->hw.aq.asq_last_status);
  3470. }
  3471. return 0;
  3472. err_up_complete:
  3473. i40e_down(vsi);
  3474. err_set_queues:
  3475. i40e_vsi_free_irq(vsi);
  3476. err_setup_rx:
  3477. i40e_vsi_free_rx_resources(vsi);
  3478. err_setup_tx:
  3479. i40e_vsi_free_tx_resources(vsi);
  3480. if (vsi == pf->vsi[pf->lan_vsi])
  3481. i40e_do_reset(pf, (1 << __I40E_PF_RESET_REQUESTED));
  3482. return err;
  3483. }
  3484. /**
  3485. * i40e_close - Disables a network interface
  3486. * @netdev: network interface device structure
  3487. *
  3488. * The close entry point is called when an interface is de-activated
  3489. * by the OS. The hardware is still under the driver's control, but
  3490. * this netdev interface is disabled.
  3491. *
  3492. * Returns 0, this is not allowed to fail
  3493. **/
  3494. static int i40e_close(struct net_device *netdev)
  3495. {
  3496. struct i40e_netdev_priv *np = netdev_priv(netdev);
  3497. struct i40e_vsi *vsi = np->vsi;
  3498. if (test_and_set_bit(__I40E_DOWN, &vsi->state))
  3499. return 0;
  3500. i40e_down(vsi);
  3501. i40e_vsi_free_irq(vsi);
  3502. i40e_vsi_free_tx_resources(vsi);
  3503. i40e_vsi_free_rx_resources(vsi);
  3504. return 0;
  3505. }
  3506. /**
  3507. * i40e_do_reset - Start a PF or Core Reset sequence
  3508. * @pf: board private structure
  3509. * @reset_flags: which reset is requested
  3510. *
  3511. * The essential difference in resets is that the PF Reset
  3512. * doesn't clear the packet buffers, doesn't reset the PE
  3513. * firmware, and doesn't bother the other PFs on the chip.
  3514. **/
  3515. void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags)
  3516. {
  3517. u32 val;
  3518. WARN_ON(in_interrupt());
  3519. /* do the biggest reset indicated */
  3520. if (reset_flags & (1 << __I40E_GLOBAL_RESET_REQUESTED)) {
  3521. /* Request a Global Reset
  3522. *
  3523. * This will start the chip's countdown to the actual full
  3524. * chip reset event, and a warning interrupt to be sent
  3525. * to all PFs, including the requestor. Our handler
  3526. * for the warning interrupt will deal with the shutdown
  3527. * and recovery of the switch setup.
  3528. */
  3529. dev_info(&pf->pdev->dev, "GlobalR requested\n");
  3530. val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
  3531. val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
  3532. wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
  3533. } else if (reset_flags & (1 << __I40E_CORE_RESET_REQUESTED)) {
  3534. /* Request a Core Reset
  3535. *
  3536. * Same as Global Reset, except does *not* include the MAC/PHY
  3537. */
  3538. dev_info(&pf->pdev->dev, "CoreR requested\n");
  3539. val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
  3540. val |= I40E_GLGEN_RTRIG_CORER_MASK;
  3541. wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
  3542. i40e_flush(&pf->hw);
  3543. } else if (reset_flags & (1 << __I40E_EMP_RESET_REQUESTED)) {
  3544. /* Request a Firmware Reset
  3545. *
  3546. * Same as Global reset, plus restarting the
  3547. * embedded firmware engine.
  3548. */
  3549. /* enable EMP Reset */
  3550. val = rd32(&pf->hw, I40E_GLGEN_RSTENA_EMP);
  3551. val |= I40E_GLGEN_RSTENA_EMP_EMP_RST_ENA_MASK;
  3552. wr32(&pf->hw, I40E_GLGEN_RSTENA_EMP, val);
  3553. /* force the reset */
  3554. val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
  3555. val |= I40E_GLGEN_RTRIG_EMPFWR_MASK;
  3556. wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
  3557. i40e_flush(&pf->hw);
  3558. } else if (reset_flags & (1 << __I40E_PF_RESET_REQUESTED)) {
  3559. /* Request a PF Reset
  3560. *
  3561. * Resets only the PF-specific registers
  3562. *
  3563. * This goes directly to the tear-down and rebuild of
  3564. * the switch, since we need to do all the recovery as
  3565. * for the Core Reset.
  3566. */
  3567. dev_info(&pf->pdev->dev, "PFR requested\n");
  3568. i40e_handle_reset_warning(pf);
  3569. } else if (reset_flags & (1 << __I40E_REINIT_REQUESTED)) {
  3570. int v;
  3571. /* Find the VSI(s) that requested a re-init */
  3572. dev_info(&pf->pdev->dev,
  3573. "VSI reinit requested\n");
  3574. for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
  3575. struct i40e_vsi *vsi = pf->vsi[v];
  3576. if (vsi != NULL &&
  3577. test_bit(__I40E_REINIT_REQUESTED, &vsi->state)) {
  3578. i40e_vsi_reinit_locked(pf->vsi[v]);
  3579. clear_bit(__I40E_REINIT_REQUESTED, &vsi->state);
  3580. }
  3581. }
  3582. /* no further action needed, so return now */
  3583. return;
  3584. } else {
  3585. dev_info(&pf->pdev->dev,
  3586. "bad reset request 0x%08x\n", reset_flags);
  3587. return;
  3588. }
  3589. }
  3590. /**
  3591. * i40e_do_reset_safe - Protected reset path for userland calls.
  3592. * @pf: board private structure
  3593. * @reset_flags: which reset is requested
  3594. *
  3595. **/
  3596. void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
  3597. {
  3598. rtnl_lock();
  3599. i40e_do_reset(pf, reset_flags);
  3600. rtnl_unlock();
  3601. }
  3602. /**
  3603. * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
  3604. * @pf: board private structure
  3605. * @e: event info posted on ARQ
  3606. *
  3607. * Handler for LAN Queue Overflow Event generated by the firmware for PF
  3608. * and VF queues
  3609. **/
  3610. static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
  3611. struct i40e_arq_event_info *e)
  3612. {
  3613. struct i40e_aqc_lan_overflow *data =
  3614. (struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
  3615. u32 queue = le32_to_cpu(data->prtdcb_rupto);
  3616. u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
  3617. struct i40e_hw *hw = &pf->hw;
  3618. struct i40e_vf *vf;
  3619. u16 vf_id;
  3620. dev_info(&pf->pdev->dev, "%s: Rx Queue Number = %d QTX_CTL=0x%08x\n",
  3621. __func__, queue, qtx_ctl);
  3622. /* Queue belongs to VF, find the VF and issue VF reset */
  3623. if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
  3624. >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
  3625. vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
  3626. >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
  3627. vf_id -= hw->func_caps.vf_base_id;
  3628. vf = &pf->vf[vf_id];
  3629. i40e_vc_notify_vf_reset(vf);
  3630. /* Allow VF to process pending reset notification */
  3631. msleep(20);
  3632. i40e_reset_vf(vf, false);
  3633. }
  3634. }
  3635. /**
  3636. * i40e_service_event_complete - Finish up the service event
  3637. * @pf: board private structure
  3638. **/
  3639. static void i40e_service_event_complete(struct i40e_pf *pf)
  3640. {
  3641. BUG_ON(!test_bit(__I40E_SERVICE_SCHED, &pf->state));
  3642. /* flush memory to make sure state is correct before next watchog */
  3643. smp_mb__before_clear_bit();
  3644. clear_bit(__I40E_SERVICE_SCHED, &pf->state);
  3645. }
  3646. /**
  3647. * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
  3648. * @pf: board private structure
  3649. **/
  3650. static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
  3651. {
  3652. if (!(pf->flags & I40E_FLAG_FDIR_REQUIRES_REINIT))
  3653. return;
  3654. pf->flags &= ~I40E_FLAG_FDIR_REQUIRES_REINIT;
  3655. /* if interface is down do nothing */
  3656. if (test_bit(__I40E_DOWN, &pf->state))
  3657. return;
  3658. }
  3659. /**
  3660. * i40e_vsi_link_event - notify VSI of a link event
  3661. * @vsi: vsi to be notified
  3662. * @link_up: link up or down
  3663. **/
  3664. static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
  3665. {
  3666. if (!vsi)
  3667. return;
  3668. switch (vsi->type) {
  3669. case I40E_VSI_MAIN:
  3670. if (!vsi->netdev || !vsi->netdev_registered)
  3671. break;
  3672. if (link_up) {
  3673. netif_carrier_on(vsi->netdev);
  3674. netif_tx_wake_all_queues(vsi->netdev);
  3675. } else {
  3676. netif_carrier_off(vsi->netdev);
  3677. netif_tx_stop_all_queues(vsi->netdev);
  3678. }
  3679. break;
  3680. case I40E_VSI_SRIOV:
  3681. break;
  3682. case I40E_VSI_VMDQ2:
  3683. case I40E_VSI_CTRL:
  3684. case I40E_VSI_MIRROR:
  3685. default:
  3686. /* there is no notification for other VSIs */
  3687. break;
  3688. }
  3689. }
  3690. /**
  3691. * i40e_veb_link_event - notify elements on the veb of a link event
  3692. * @veb: veb to be notified
  3693. * @link_up: link up or down
  3694. **/
  3695. static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
  3696. {
  3697. struct i40e_pf *pf;
  3698. int i;
  3699. if (!veb || !veb->pf)
  3700. return;
  3701. pf = veb->pf;
  3702. /* depth first... */
  3703. for (i = 0; i < I40E_MAX_VEB; i++)
  3704. if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
  3705. i40e_veb_link_event(pf->veb[i], link_up);
  3706. /* ... now the local VSIs */
  3707. for (i = 0; i < pf->hw.func_caps.num_vsis; i++)
  3708. if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
  3709. i40e_vsi_link_event(pf->vsi[i], link_up);
  3710. }
  3711. /**
  3712. * i40e_link_event - Update netif_carrier status
  3713. * @pf: board private structure
  3714. **/
  3715. static void i40e_link_event(struct i40e_pf *pf)
  3716. {
  3717. bool new_link, old_link;
  3718. new_link = (pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP);
  3719. old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
  3720. if (new_link == old_link)
  3721. return;
  3722. if (!test_bit(__I40E_DOWN, &pf->vsi[pf->lan_vsi]->state))
  3723. netdev_info(pf->vsi[pf->lan_vsi]->netdev,
  3724. "NIC Link is %s\n", (new_link ? "Up" : "Down"));
  3725. /* Notify the base of the switch tree connected to
  3726. * the link. Floating VEBs are not notified.
  3727. */
  3728. if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
  3729. i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
  3730. else
  3731. i40e_vsi_link_event(pf->vsi[pf->lan_vsi], new_link);
  3732. if (pf->vf)
  3733. i40e_vc_notify_link_state(pf);
  3734. }
  3735. /**
  3736. * i40e_check_hang_subtask - Check for hung queues and dropped interrupts
  3737. * @pf: board private structure
  3738. *
  3739. * Set the per-queue flags to request a check for stuck queues in the irq
  3740. * clean functions, then force interrupts to be sure the irq clean is called.
  3741. **/
  3742. static void i40e_check_hang_subtask(struct i40e_pf *pf)
  3743. {
  3744. int i, v;
  3745. /* If we're down or resetting, just bail */
  3746. if (test_bit(__I40E_CONFIG_BUSY, &pf->state))
  3747. return;
  3748. /* for each VSI/netdev
  3749. * for each Tx queue
  3750. * set the check flag
  3751. * for each q_vector
  3752. * force an interrupt
  3753. */
  3754. for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
  3755. struct i40e_vsi *vsi = pf->vsi[v];
  3756. int armed = 0;
  3757. if (!pf->vsi[v] ||
  3758. test_bit(__I40E_DOWN, &vsi->state) ||
  3759. (vsi->netdev && !netif_carrier_ok(vsi->netdev)))
  3760. continue;
  3761. for (i = 0; i < vsi->num_queue_pairs; i++) {
  3762. set_check_for_tx_hang(vsi->tx_rings[i]);
  3763. if (test_bit(__I40E_HANG_CHECK_ARMED,
  3764. &vsi->tx_rings[i]->state))
  3765. armed++;
  3766. }
  3767. if (armed) {
  3768. if (!(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
  3769. wr32(&vsi->back->hw, I40E_PFINT_DYN_CTL0,
  3770. (I40E_PFINT_DYN_CTL0_INTENA_MASK |
  3771. I40E_PFINT_DYN_CTL0_SWINT_TRIG_MASK));
  3772. } else {
  3773. u16 vec = vsi->base_vector - 1;
  3774. u32 val = (I40E_PFINT_DYN_CTLN_INTENA_MASK |
  3775. I40E_PFINT_DYN_CTLN_SWINT_TRIG_MASK);
  3776. for (i = 0; i < vsi->num_q_vectors; i++, vec++)
  3777. wr32(&vsi->back->hw,
  3778. I40E_PFINT_DYN_CTLN(vec), val);
  3779. }
  3780. i40e_flush(&vsi->back->hw);
  3781. }
  3782. }
  3783. }
  3784. /**
  3785. * i40e_watchdog_subtask - Check and bring link up
  3786. * @pf: board private structure
  3787. **/
  3788. static void i40e_watchdog_subtask(struct i40e_pf *pf)
  3789. {
  3790. int i;
  3791. /* if interface is down do nothing */
  3792. if (test_bit(__I40E_DOWN, &pf->state) ||
  3793. test_bit(__I40E_CONFIG_BUSY, &pf->state))
  3794. return;
  3795. /* Update the stats for active netdevs so the network stack
  3796. * can look at updated numbers whenever it cares to
  3797. */
  3798. for (i = 0; i < pf->hw.func_caps.num_vsis; i++)
  3799. if (pf->vsi[i] && pf->vsi[i]->netdev)
  3800. i40e_update_stats(pf->vsi[i]);
  3801. /* Update the stats for the active switching components */
  3802. for (i = 0; i < I40E_MAX_VEB; i++)
  3803. if (pf->veb[i])
  3804. i40e_update_veb_stats(pf->veb[i]);
  3805. }
  3806. /**
  3807. * i40e_reset_subtask - Set up for resetting the device and driver
  3808. * @pf: board private structure
  3809. **/
  3810. static void i40e_reset_subtask(struct i40e_pf *pf)
  3811. {
  3812. u32 reset_flags = 0;
  3813. rtnl_lock();
  3814. if (test_bit(__I40E_REINIT_REQUESTED, &pf->state)) {
  3815. reset_flags |= (1 << __I40E_REINIT_REQUESTED);
  3816. clear_bit(__I40E_REINIT_REQUESTED, &pf->state);
  3817. }
  3818. if (test_bit(__I40E_PF_RESET_REQUESTED, &pf->state)) {
  3819. reset_flags |= (1 << __I40E_PF_RESET_REQUESTED);
  3820. clear_bit(__I40E_PF_RESET_REQUESTED, &pf->state);
  3821. }
  3822. if (test_bit(__I40E_CORE_RESET_REQUESTED, &pf->state)) {
  3823. reset_flags |= (1 << __I40E_CORE_RESET_REQUESTED);
  3824. clear_bit(__I40E_CORE_RESET_REQUESTED, &pf->state);
  3825. }
  3826. if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state)) {
  3827. reset_flags |= (1 << __I40E_GLOBAL_RESET_REQUESTED);
  3828. clear_bit(__I40E_GLOBAL_RESET_REQUESTED, &pf->state);
  3829. }
  3830. /* If there's a recovery already waiting, it takes
  3831. * precedence before starting a new reset sequence.
  3832. */
  3833. if (test_bit(__I40E_RESET_INTR_RECEIVED, &pf->state)) {
  3834. i40e_handle_reset_warning(pf);
  3835. goto unlock;
  3836. }
  3837. /* If we're already down or resetting, just bail */
  3838. if (reset_flags &&
  3839. !test_bit(__I40E_DOWN, &pf->state) &&
  3840. !test_bit(__I40E_CONFIG_BUSY, &pf->state))
  3841. i40e_do_reset(pf, reset_flags);
  3842. unlock:
  3843. rtnl_unlock();
  3844. }
  3845. /**
  3846. * i40e_handle_link_event - Handle link event
  3847. * @pf: board private structure
  3848. * @e: event info posted on ARQ
  3849. **/
  3850. static void i40e_handle_link_event(struct i40e_pf *pf,
  3851. struct i40e_arq_event_info *e)
  3852. {
  3853. struct i40e_hw *hw = &pf->hw;
  3854. struct i40e_aqc_get_link_status *status =
  3855. (struct i40e_aqc_get_link_status *)&e->desc.params.raw;
  3856. struct i40e_link_status *hw_link_info = &hw->phy.link_info;
  3857. /* save off old link status information */
  3858. memcpy(&pf->hw.phy.link_info_old, hw_link_info,
  3859. sizeof(pf->hw.phy.link_info_old));
  3860. /* update link status */
  3861. hw_link_info->phy_type = (enum i40e_aq_phy_type)status->phy_type;
  3862. hw_link_info->link_speed = (enum i40e_aq_link_speed)status->link_speed;
  3863. hw_link_info->link_info = status->link_info;
  3864. hw_link_info->an_info = status->an_info;
  3865. hw_link_info->ext_info = status->ext_info;
  3866. hw_link_info->lse_enable =
  3867. le16_to_cpu(status->command_flags) &
  3868. I40E_AQ_LSE_ENABLE;
  3869. /* process the event */
  3870. i40e_link_event(pf);
  3871. /* Do a new status request to re-enable LSE reporting
  3872. * and load new status information into the hw struct,
  3873. * then see if the status changed while processing the
  3874. * initial event.
  3875. */
  3876. i40e_aq_get_link_info(&pf->hw, true, NULL, NULL);
  3877. i40e_link_event(pf);
  3878. }
  3879. /**
  3880. * i40e_clean_adminq_subtask - Clean the AdminQ rings
  3881. * @pf: board private structure
  3882. **/
  3883. static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
  3884. {
  3885. struct i40e_arq_event_info event;
  3886. struct i40e_hw *hw = &pf->hw;
  3887. u16 pending, i = 0;
  3888. i40e_status ret;
  3889. u16 opcode;
  3890. u32 val;
  3891. if (!test_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state))
  3892. return;
  3893. event.msg_size = I40E_MAX_AQ_BUF_SIZE;
  3894. event.msg_buf = kzalloc(event.msg_size, GFP_KERNEL);
  3895. if (!event.msg_buf)
  3896. return;
  3897. do {
  3898. ret = i40e_clean_arq_element(hw, &event, &pending);
  3899. if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK) {
  3900. dev_info(&pf->pdev->dev, "No ARQ event found\n");
  3901. break;
  3902. } else if (ret) {
  3903. dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
  3904. break;
  3905. }
  3906. opcode = le16_to_cpu(event.desc.opcode);
  3907. switch (opcode) {
  3908. case i40e_aqc_opc_get_link_status:
  3909. i40e_handle_link_event(pf, &event);
  3910. break;
  3911. case i40e_aqc_opc_send_msg_to_pf:
  3912. ret = i40e_vc_process_vf_msg(pf,
  3913. le16_to_cpu(event.desc.retval),
  3914. le32_to_cpu(event.desc.cookie_high),
  3915. le32_to_cpu(event.desc.cookie_low),
  3916. event.msg_buf,
  3917. event.msg_size);
  3918. break;
  3919. case i40e_aqc_opc_lldp_update_mib:
  3920. dev_info(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
  3921. break;
  3922. case i40e_aqc_opc_event_lan_overflow:
  3923. dev_info(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
  3924. i40e_handle_lan_overflow_event(pf, &event);
  3925. break;
  3926. default:
  3927. dev_info(&pf->pdev->dev,
  3928. "ARQ Error: Unknown event %d received\n",
  3929. event.desc.opcode);
  3930. break;
  3931. }
  3932. } while (pending && (i++ < pf->adminq_work_limit));
  3933. clear_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state);
  3934. /* re-enable Admin queue interrupt cause */
  3935. val = rd32(hw, I40E_PFINT_ICR0_ENA);
  3936. val |= I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
  3937. wr32(hw, I40E_PFINT_ICR0_ENA, val);
  3938. i40e_flush(hw);
  3939. kfree(event.msg_buf);
  3940. }
  3941. /**
  3942. * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
  3943. * @veb: pointer to the VEB instance
  3944. *
  3945. * This is a recursive function that first builds the attached VSIs then
  3946. * recurses in to build the next layer of VEB. We track the connections
  3947. * through our own index numbers because the seid's from the HW could
  3948. * change across the reset.
  3949. **/
  3950. static int i40e_reconstitute_veb(struct i40e_veb *veb)
  3951. {
  3952. struct i40e_vsi *ctl_vsi = NULL;
  3953. struct i40e_pf *pf = veb->pf;
  3954. int v, veb_idx;
  3955. int ret;
  3956. /* build VSI that owns this VEB, temporarily attached to base VEB */
  3957. for (v = 0; v < pf->hw.func_caps.num_vsis && !ctl_vsi; v++) {
  3958. if (pf->vsi[v] &&
  3959. pf->vsi[v]->veb_idx == veb->idx &&
  3960. pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
  3961. ctl_vsi = pf->vsi[v];
  3962. break;
  3963. }
  3964. }
  3965. if (!ctl_vsi) {
  3966. dev_info(&pf->pdev->dev,
  3967. "missing owner VSI for veb_idx %d\n", veb->idx);
  3968. ret = -ENOENT;
  3969. goto end_reconstitute;
  3970. }
  3971. if (ctl_vsi != pf->vsi[pf->lan_vsi])
  3972. ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
  3973. ret = i40e_add_vsi(ctl_vsi);
  3974. if (ret) {
  3975. dev_info(&pf->pdev->dev,
  3976. "rebuild of owner VSI failed: %d\n", ret);
  3977. goto end_reconstitute;
  3978. }
  3979. i40e_vsi_reset_stats(ctl_vsi);
  3980. /* create the VEB in the switch and move the VSI onto the VEB */
  3981. ret = i40e_add_veb(veb, ctl_vsi);
  3982. if (ret)
  3983. goto end_reconstitute;
  3984. /* create the remaining VSIs attached to this VEB */
  3985. for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
  3986. if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
  3987. continue;
  3988. if (pf->vsi[v]->veb_idx == veb->idx) {
  3989. struct i40e_vsi *vsi = pf->vsi[v];
  3990. vsi->uplink_seid = veb->seid;
  3991. ret = i40e_add_vsi(vsi);
  3992. if (ret) {
  3993. dev_info(&pf->pdev->dev,
  3994. "rebuild of vsi_idx %d failed: %d\n",
  3995. v, ret);
  3996. goto end_reconstitute;
  3997. }
  3998. i40e_vsi_reset_stats(vsi);
  3999. }
  4000. }
  4001. /* create any VEBs attached to this VEB - RECURSION */
  4002. for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
  4003. if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
  4004. pf->veb[veb_idx]->uplink_seid = veb->seid;
  4005. ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
  4006. if (ret)
  4007. break;
  4008. }
  4009. }
  4010. end_reconstitute:
  4011. return ret;
  4012. }
  4013. /**
  4014. * i40e_get_capabilities - get info about the HW
  4015. * @pf: the PF struct
  4016. **/
  4017. static int i40e_get_capabilities(struct i40e_pf *pf)
  4018. {
  4019. struct i40e_aqc_list_capabilities_element_resp *cap_buf;
  4020. u16 data_size;
  4021. int buf_len;
  4022. int err;
  4023. buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
  4024. do {
  4025. cap_buf = kzalloc(buf_len, GFP_KERNEL);
  4026. if (!cap_buf)
  4027. return -ENOMEM;
  4028. /* this loads the data into the hw struct for us */
  4029. err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
  4030. &data_size,
  4031. i40e_aqc_opc_list_func_capabilities,
  4032. NULL);
  4033. /* data loaded, buffer no longer needed */
  4034. kfree(cap_buf);
  4035. if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
  4036. /* retry with a larger buffer */
  4037. buf_len = data_size;
  4038. } else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK) {
  4039. dev_info(&pf->pdev->dev,
  4040. "capability discovery failed: aq=%d\n",
  4041. pf->hw.aq.asq_last_status);
  4042. return -ENODEV;
  4043. }
  4044. } while (err);
  4045. if (pf->hw.revision_id == 0 && pf->hw.func_caps.npar_enable) {
  4046. pf->hw.func_caps.num_msix_vectors += 1;
  4047. pf->hw.func_caps.num_tx_qp =
  4048. min_t(int, pf->hw.func_caps.num_tx_qp,
  4049. I40E_MAX_NPAR_QPS);
  4050. }
  4051. if (pf->hw.debug_mask & I40E_DEBUG_USER)
  4052. dev_info(&pf->pdev->dev,
  4053. "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",
  4054. pf->hw.pf_id, pf->hw.func_caps.num_vfs,
  4055. pf->hw.func_caps.num_msix_vectors,
  4056. pf->hw.func_caps.num_msix_vectors_vf,
  4057. pf->hw.func_caps.fd_filters_guaranteed,
  4058. pf->hw.func_caps.fd_filters_best_effort,
  4059. pf->hw.func_caps.num_tx_qp,
  4060. pf->hw.func_caps.num_vsis);
  4061. #define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
  4062. + pf->hw.func_caps.num_vfs)
  4063. if (pf->hw.revision_id == 0 && (DEF_NUM_VSI > pf->hw.func_caps.num_vsis)) {
  4064. dev_info(&pf->pdev->dev,
  4065. "got num_vsis %d, setting num_vsis to %d\n",
  4066. pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
  4067. pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
  4068. }
  4069. return 0;
  4070. }
  4071. /**
  4072. * i40e_fdir_setup - initialize the Flow Director resources
  4073. * @pf: board private structure
  4074. **/
  4075. static void i40e_fdir_setup(struct i40e_pf *pf)
  4076. {
  4077. struct i40e_vsi *vsi;
  4078. bool new_vsi = false;
  4079. int err, i;
  4080. if (!(pf->flags & (I40E_FLAG_FDIR_ENABLED |
  4081. I40E_FLAG_FDIR_ATR_ENABLED)))
  4082. return;
  4083. pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
  4084. /* find existing or make new FDIR VSI */
  4085. vsi = NULL;
  4086. for (i = 0; i < pf->hw.func_caps.num_vsis; i++)
  4087. if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR)
  4088. vsi = pf->vsi[i];
  4089. if (!vsi) {
  4090. vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR, pf->mac_seid, 0);
  4091. if (!vsi) {
  4092. dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
  4093. pf->flags &= ~I40E_FLAG_FDIR_ENABLED;
  4094. return;
  4095. }
  4096. new_vsi = true;
  4097. }
  4098. WARN_ON(vsi->base_queue != I40E_FDIR_RING);
  4099. i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_rings);
  4100. err = i40e_vsi_setup_tx_resources(vsi);
  4101. if (!err)
  4102. err = i40e_vsi_setup_rx_resources(vsi);
  4103. if (!err)
  4104. err = i40e_vsi_configure(vsi);
  4105. if (!err && new_vsi) {
  4106. char int_name[IFNAMSIZ + 9];
  4107. snprintf(int_name, sizeof(int_name) - 1, "%s-fdir",
  4108. dev_driver_string(&pf->pdev->dev));
  4109. err = i40e_vsi_request_irq(vsi, int_name);
  4110. }
  4111. if (!err)
  4112. err = i40e_up_complete(vsi);
  4113. clear_bit(__I40E_NEEDS_RESTART, &vsi->state);
  4114. }
  4115. /**
  4116. * i40e_fdir_teardown - release the Flow Director resources
  4117. * @pf: board private structure
  4118. **/
  4119. static void i40e_fdir_teardown(struct i40e_pf *pf)
  4120. {
  4121. int i;
  4122. for (i = 0; i < pf->hw.func_caps.num_vsis; i++) {
  4123. if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
  4124. i40e_vsi_release(pf->vsi[i]);
  4125. break;
  4126. }
  4127. }
  4128. }
  4129. /**
  4130. * i40e_prep_for_reset - prep for the core to reset
  4131. * @pf: board private structure
  4132. *
  4133. * Close up the VFs and other things in prep for pf Reset.
  4134. **/
  4135. static int i40e_prep_for_reset(struct i40e_pf *pf)
  4136. {
  4137. struct i40e_hw *hw = &pf->hw;
  4138. i40e_status ret;
  4139. u32 v;
  4140. clear_bit(__I40E_RESET_INTR_RECEIVED, &pf->state);
  4141. if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state))
  4142. return 0;
  4143. dev_info(&pf->pdev->dev, "Tearing down internal switch for reset\n");
  4144. i40e_vc_notify_reset(pf);
  4145. /* quiesce the VSIs and their queues that are not already DOWN */
  4146. i40e_pf_quiesce_all_vsi(pf);
  4147. for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
  4148. if (pf->vsi[v])
  4149. pf->vsi[v]->seid = 0;
  4150. }
  4151. i40e_shutdown_adminq(&pf->hw);
  4152. /* call shutdown HMC */
  4153. ret = i40e_shutdown_lan_hmc(hw);
  4154. if (ret) {
  4155. dev_info(&pf->pdev->dev, "shutdown_lan_hmc failed: %d\n", ret);
  4156. clear_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state);
  4157. }
  4158. return ret;
  4159. }
  4160. /**
  4161. * i40e_reset_and_rebuild - reset and rebuid using a saved config
  4162. * @pf: board private structure
  4163. * @reinit: if the Main VSI needs to re-initialized.
  4164. **/
  4165. static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit)
  4166. {
  4167. struct i40e_driver_version dv;
  4168. struct i40e_hw *hw = &pf->hw;
  4169. i40e_status ret;
  4170. u32 v;
  4171. /* Now we wait for GRST to settle out.
  4172. * We don't have to delete the VEBs or VSIs from the hw switch
  4173. * because the reset will make them disappear.
  4174. */
  4175. ret = i40e_pf_reset(hw);
  4176. if (ret)
  4177. dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
  4178. pf->pfr_count++;
  4179. if (test_bit(__I40E_DOWN, &pf->state))
  4180. goto end_core_reset;
  4181. dev_info(&pf->pdev->dev, "Rebuilding internal switch\n");
  4182. /* rebuild the basics for the AdminQ, HMC, and initial HW switch */
  4183. ret = i40e_init_adminq(&pf->hw);
  4184. if (ret) {
  4185. dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, %d\n", ret);
  4186. goto end_core_reset;
  4187. }
  4188. ret = i40e_get_capabilities(pf);
  4189. if (ret) {
  4190. dev_info(&pf->pdev->dev, "i40e_get_capabilities failed, %d\n",
  4191. ret);
  4192. goto end_core_reset;
  4193. }
  4194. ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
  4195. hw->func_caps.num_rx_qp,
  4196. pf->fcoe_hmc_cntx_num, pf->fcoe_hmc_filt_num);
  4197. if (ret) {
  4198. dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
  4199. goto end_core_reset;
  4200. }
  4201. ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
  4202. if (ret) {
  4203. dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
  4204. goto end_core_reset;
  4205. }
  4206. /* do basic switch setup */
  4207. ret = i40e_setup_pf_switch(pf, reinit);
  4208. if (ret)
  4209. goto end_core_reset;
  4210. /* Rebuild the VSIs and VEBs that existed before reset.
  4211. * They are still in our local switch element arrays, so only
  4212. * need to rebuild the switch model in the HW.
  4213. *
  4214. * If there were VEBs but the reconstitution failed, we'll try
  4215. * try to recover minimal use by getting the basic PF VSI working.
  4216. */
  4217. if (pf->vsi[pf->lan_vsi]->uplink_seid != pf->mac_seid) {
  4218. dev_info(&pf->pdev->dev, "attempting to rebuild switch\n");
  4219. /* find the one VEB connected to the MAC, and find orphans */
  4220. for (v = 0; v < I40E_MAX_VEB; v++) {
  4221. if (!pf->veb[v])
  4222. continue;
  4223. if (pf->veb[v]->uplink_seid == pf->mac_seid ||
  4224. pf->veb[v]->uplink_seid == 0) {
  4225. ret = i40e_reconstitute_veb(pf->veb[v]);
  4226. if (!ret)
  4227. continue;
  4228. /* If Main VEB failed, we're in deep doodoo,
  4229. * so give up rebuilding the switch and set up
  4230. * for minimal rebuild of PF VSI.
  4231. * If orphan failed, we'll report the error
  4232. * but try to keep going.
  4233. */
  4234. if (pf->veb[v]->uplink_seid == pf->mac_seid) {
  4235. dev_info(&pf->pdev->dev,
  4236. "rebuild of switch failed: %d, will try to set up simple PF connection\n",
  4237. ret);
  4238. pf->vsi[pf->lan_vsi]->uplink_seid
  4239. = pf->mac_seid;
  4240. break;
  4241. } else if (pf->veb[v]->uplink_seid == 0) {
  4242. dev_info(&pf->pdev->dev,
  4243. "rebuild of orphan VEB failed: %d\n",
  4244. ret);
  4245. }
  4246. }
  4247. }
  4248. }
  4249. if (pf->vsi[pf->lan_vsi]->uplink_seid == pf->mac_seid) {
  4250. dev_info(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
  4251. /* no VEB, so rebuild only the Main VSI */
  4252. ret = i40e_add_vsi(pf->vsi[pf->lan_vsi]);
  4253. if (ret) {
  4254. dev_info(&pf->pdev->dev,
  4255. "rebuild of Main VSI failed: %d\n", ret);
  4256. goto end_core_reset;
  4257. }
  4258. }
  4259. /* reinit the misc interrupt */
  4260. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  4261. ret = i40e_setup_misc_vector(pf);
  4262. /* restart the VSIs that were rebuilt and running before the reset */
  4263. i40e_pf_unquiesce_all_vsi(pf);
  4264. /* tell the firmware that we're starting */
  4265. dv.major_version = DRV_VERSION_MAJOR;
  4266. dv.minor_version = DRV_VERSION_MINOR;
  4267. dv.build_version = DRV_VERSION_BUILD;
  4268. dv.subbuild_version = 0;
  4269. i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
  4270. dev_info(&pf->pdev->dev, "PF reset done\n");
  4271. end_core_reset:
  4272. clear_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state);
  4273. }
  4274. /**
  4275. * i40e_handle_reset_warning - prep for the pf to reset, reset and rebuild
  4276. * @pf: board private structure
  4277. *
  4278. * Close up the VFs and other things in prep for a Core Reset,
  4279. * then get ready to rebuild the world.
  4280. **/
  4281. static void i40e_handle_reset_warning(struct i40e_pf *pf)
  4282. {
  4283. i40e_status ret;
  4284. ret = i40e_prep_for_reset(pf);
  4285. if (!ret)
  4286. i40e_reset_and_rebuild(pf, false);
  4287. }
  4288. /**
  4289. * i40e_handle_mdd_event
  4290. * @pf: pointer to the pf structure
  4291. *
  4292. * Called from the MDD irq handler to identify possibly malicious vfs
  4293. **/
  4294. static void i40e_handle_mdd_event(struct i40e_pf *pf)
  4295. {
  4296. struct i40e_hw *hw = &pf->hw;
  4297. bool mdd_detected = false;
  4298. struct i40e_vf *vf;
  4299. u32 reg;
  4300. int i;
  4301. if (!test_bit(__I40E_MDD_EVENT_PENDING, &pf->state))
  4302. return;
  4303. /* find what triggered the MDD event */
  4304. reg = rd32(hw, I40E_GL_MDET_TX);
  4305. if (reg & I40E_GL_MDET_TX_VALID_MASK) {
  4306. u8 func = (reg & I40E_GL_MDET_TX_FUNCTION_MASK)
  4307. >> I40E_GL_MDET_TX_FUNCTION_SHIFT;
  4308. u8 event = (reg & I40E_GL_MDET_TX_EVENT_SHIFT)
  4309. >> I40E_GL_MDET_TX_EVENT_SHIFT;
  4310. u8 queue = (reg & I40E_GL_MDET_TX_QUEUE_MASK)
  4311. >> I40E_GL_MDET_TX_QUEUE_SHIFT;
  4312. dev_info(&pf->pdev->dev,
  4313. "Malicious Driver Detection TX event 0x%02x on q %d of function 0x%02x\n",
  4314. event, queue, func);
  4315. wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
  4316. mdd_detected = true;
  4317. }
  4318. reg = rd32(hw, I40E_GL_MDET_RX);
  4319. if (reg & I40E_GL_MDET_RX_VALID_MASK) {
  4320. u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK)
  4321. >> I40E_GL_MDET_RX_FUNCTION_SHIFT;
  4322. u8 event = (reg & I40E_GL_MDET_RX_EVENT_SHIFT)
  4323. >> I40E_GL_MDET_RX_EVENT_SHIFT;
  4324. u8 queue = (reg & I40E_GL_MDET_RX_QUEUE_MASK)
  4325. >> I40E_GL_MDET_RX_QUEUE_SHIFT;
  4326. dev_info(&pf->pdev->dev,
  4327. "Malicious Driver Detection RX event 0x%02x on q %d of function 0x%02x\n",
  4328. event, queue, func);
  4329. wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
  4330. mdd_detected = true;
  4331. }
  4332. /* see if one of the VFs needs its hand slapped */
  4333. for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
  4334. vf = &(pf->vf[i]);
  4335. reg = rd32(hw, I40E_VP_MDET_TX(i));
  4336. if (reg & I40E_VP_MDET_TX_VALID_MASK) {
  4337. wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
  4338. vf->num_mdd_events++;
  4339. dev_info(&pf->pdev->dev, "MDD TX event on VF %d\n", i);
  4340. }
  4341. reg = rd32(hw, I40E_VP_MDET_RX(i));
  4342. if (reg & I40E_VP_MDET_RX_VALID_MASK) {
  4343. wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
  4344. vf->num_mdd_events++;
  4345. dev_info(&pf->pdev->dev, "MDD RX event on VF %d\n", i);
  4346. }
  4347. if (vf->num_mdd_events > I40E_DEFAULT_NUM_MDD_EVENTS_ALLOWED) {
  4348. dev_info(&pf->pdev->dev,
  4349. "Too many MDD events on VF %d, disabled\n", i);
  4350. dev_info(&pf->pdev->dev,
  4351. "Use PF Control I/F to re-enable the VF\n");
  4352. set_bit(I40E_VF_STAT_DISABLED, &vf->vf_states);
  4353. }
  4354. }
  4355. /* re-enable mdd interrupt cause */
  4356. clear_bit(__I40E_MDD_EVENT_PENDING, &pf->state);
  4357. reg = rd32(hw, I40E_PFINT_ICR0_ENA);
  4358. reg |= I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
  4359. wr32(hw, I40E_PFINT_ICR0_ENA, reg);
  4360. i40e_flush(hw);
  4361. }
  4362. /**
  4363. * i40e_service_task - Run the driver's async subtasks
  4364. * @work: pointer to work_struct containing our data
  4365. **/
  4366. static void i40e_service_task(struct work_struct *work)
  4367. {
  4368. struct i40e_pf *pf = container_of(work,
  4369. struct i40e_pf,
  4370. service_task);
  4371. unsigned long start_time = jiffies;
  4372. i40e_reset_subtask(pf);
  4373. i40e_handle_mdd_event(pf);
  4374. i40e_vc_process_vflr_event(pf);
  4375. i40e_watchdog_subtask(pf);
  4376. i40e_fdir_reinit_subtask(pf);
  4377. i40e_check_hang_subtask(pf);
  4378. i40e_sync_filters_subtask(pf);
  4379. i40e_clean_adminq_subtask(pf);
  4380. i40e_service_event_complete(pf);
  4381. /* If the tasks have taken longer than one timer cycle or there
  4382. * is more work to be done, reschedule the service task now
  4383. * rather than wait for the timer to tick again.
  4384. */
  4385. if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
  4386. test_bit(__I40E_ADMINQ_EVENT_PENDING, &pf->state) ||
  4387. test_bit(__I40E_MDD_EVENT_PENDING, &pf->state) ||
  4388. test_bit(__I40E_VFLR_EVENT_PENDING, &pf->state))
  4389. i40e_service_event_schedule(pf);
  4390. }
  4391. /**
  4392. * i40e_service_timer - timer callback
  4393. * @data: pointer to PF struct
  4394. **/
  4395. static void i40e_service_timer(unsigned long data)
  4396. {
  4397. struct i40e_pf *pf = (struct i40e_pf *)data;
  4398. mod_timer(&pf->service_timer,
  4399. round_jiffies(jiffies + pf->service_timer_period));
  4400. i40e_service_event_schedule(pf);
  4401. }
  4402. /**
  4403. * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
  4404. * @vsi: the VSI being configured
  4405. **/
  4406. static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
  4407. {
  4408. struct i40e_pf *pf = vsi->back;
  4409. switch (vsi->type) {
  4410. case I40E_VSI_MAIN:
  4411. vsi->alloc_queue_pairs = pf->num_lan_qps;
  4412. vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
  4413. I40E_REQ_DESCRIPTOR_MULTIPLE);
  4414. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  4415. vsi->num_q_vectors = pf->num_lan_msix;
  4416. else
  4417. vsi->num_q_vectors = 1;
  4418. break;
  4419. case I40E_VSI_FDIR:
  4420. vsi->alloc_queue_pairs = 1;
  4421. vsi->num_desc = ALIGN(I40E_FDIR_RING_COUNT,
  4422. I40E_REQ_DESCRIPTOR_MULTIPLE);
  4423. vsi->num_q_vectors = 1;
  4424. break;
  4425. case I40E_VSI_VMDQ2:
  4426. vsi->alloc_queue_pairs = pf->num_vmdq_qps;
  4427. vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
  4428. I40E_REQ_DESCRIPTOR_MULTIPLE);
  4429. vsi->num_q_vectors = pf->num_vmdq_msix;
  4430. break;
  4431. case I40E_VSI_SRIOV:
  4432. vsi->alloc_queue_pairs = pf->num_vf_qps;
  4433. vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
  4434. I40E_REQ_DESCRIPTOR_MULTIPLE);
  4435. break;
  4436. default:
  4437. WARN_ON(1);
  4438. return -ENODATA;
  4439. }
  4440. return 0;
  4441. }
  4442. /**
  4443. * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
  4444. * @type: VSI pointer
  4445. * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
  4446. *
  4447. * On error: returns error code (negative)
  4448. * On success: returns 0
  4449. **/
  4450. static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
  4451. {
  4452. int size;
  4453. int ret = 0;
  4454. /* allocate memory for both Tx and Rx ring pointers */
  4455. size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs * 2;
  4456. vsi->tx_rings = kzalloc(size, GFP_KERNEL);
  4457. if (!vsi->tx_rings)
  4458. return -ENOMEM;
  4459. vsi->rx_rings = &vsi->tx_rings[vsi->alloc_queue_pairs];
  4460. if (alloc_qvectors) {
  4461. /* allocate memory for q_vector pointers */
  4462. size = sizeof(struct i40e_q_vectors *) * vsi->num_q_vectors;
  4463. vsi->q_vectors = kzalloc(size, GFP_KERNEL);
  4464. if (!vsi->q_vectors) {
  4465. ret = -ENOMEM;
  4466. goto err_vectors;
  4467. }
  4468. }
  4469. return ret;
  4470. err_vectors:
  4471. kfree(vsi->tx_rings);
  4472. return ret;
  4473. }
  4474. /**
  4475. * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
  4476. * @pf: board private structure
  4477. * @type: type of VSI
  4478. *
  4479. * On error: returns error code (negative)
  4480. * On success: returns vsi index in PF (positive)
  4481. **/
  4482. static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
  4483. {
  4484. int ret = -ENODEV;
  4485. struct i40e_vsi *vsi;
  4486. int vsi_idx;
  4487. int i;
  4488. /* Need to protect the allocation of the VSIs at the PF level */
  4489. mutex_lock(&pf->switch_mutex);
  4490. /* VSI list may be fragmented if VSI creation/destruction has
  4491. * been happening. We can afford to do a quick scan to look
  4492. * for any free VSIs in the list.
  4493. *
  4494. * find next empty vsi slot, looping back around if necessary
  4495. */
  4496. i = pf->next_vsi;
  4497. while (i < pf->hw.func_caps.num_vsis && pf->vsi[i])
  4498. i++;
  4499. if (i >= pf->hw.func_caps.num_vsis) {
  4500. i = 0;
  4501. while (i < pf->next_vsi && pf->vsi[i])
  4502. i++;
  4503. }
  4504. if (i < pf->hw.func_caps.num_vsis && !pf->vsi[i]) {
  4505. vsi_idx = i; /* Found one! */
  4506. } else {
  4507. ret = -ENODEV;
  4508. goto unlock_pf; /* out of VSI slots! */
  4509. }
  4510. pf->next_vsi = ++i;
  4511. vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
  4512. if (!vsi) {
  4513. ret = -ENOMEM;
  4514. goto unlock_pf;
  4515. }
  4516. vsi->type = type;
  4517. vsi->back = pf;
  4518. set_bit(__I40E_DOWN, &vsi->state);
  4519. vsi->flags = 0;
  4520. vsi->idx = vsi_idx;
  4521. vsi->rx_itr_setting = pf->rx_itr_default;
  4522. vsi->tx_itr_setting = pf->tx_itr_default;
  4523. vsi->netdev_registered = false;
  4524. vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
  4525. INIT_LIST_HEAD(&vsi->mac_filter_list);
  4526. ret = i40e_set_num_rings_in_vsi(vsi);
  4527. if (ret)
  4528. goto err_rings;
  4529. ret = i40e_vsi_alloc_arrays(vsi, true);
  4530. if (ret)
  4531. goto err_rings;
  4532. /* Setup default MSIX irq handler for VSI */
  4533. i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
  4534. pf->vsi[vsi_idx] = vsi;
  4535. ret = vsi_idx;
  4536. goto unlock_pf;
  4537. err_rings:
  4538. pf->next_vsi = i - 1;
  4539. kfree(vsi);
  4540. unlock_pf:
  4541. mutex_unlock(&pf->switch_mutex);
  4542. return ret;
  4543. }
  4544. /**
  4545. * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
  4546. * @type: VSI pointer
  4547. * @free_qvectors: a bool to specify if q_vectors need to be freed.
  4548. *
  4549. * On error: returns error code (negative)
  4550. * On success: returns 0
  4551. **/
  4552. static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
  4553. {
  4554. /* free the ring and vector containers */
  4555. if (free_qvectors) {
  4556. kfree(vsi->q_vectors);
  4557. vsi->q_vectors = NULL;
  4558. }
  4559. kfree(vsi->tx_rings);
  4560. vsi->tx_rings = NULL;
  4561. vsi->rx_rings = NULL;
  4562. }
  4563. /**
  4564. * i40e_vsi_clear - Deallocate the VSI provided
  4565. * @vsi: the VSI being un-configured
  4566. **/
  4567. static int i40e_vsi_clear(struct i40e_vsi *vsi)
  4568. {
  4569. struct i40e_pf *pf;
  4570. if (!vsi)
  4571. return 0;
  4572. if (!vsi->back)
  4573. goto free_vsi;
  4574. pf = vsi->back;
  4575. mutex_lock(&pf->switch_mutex);
  4576. if (!pf->vsi[vsi->idx]) {
  4577. dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](%p,type %d)\n",
  4578. vsi->idx, vsi->idx, vsi, vsi->type);
  4579. goto unlock_vsi;
  4580. }
  4581. if (pf->vsi[vsi->idx] != vsi) {
  4582. dev_err(&pf->pdev->dev,
  4583. "pf->vsi[%d](%p, type %d) != vsi[%d](%p,type %d): no free!\n",
  4584. pf->vsi[vsi->idx]->idx,
  4585. pf->vsi[vsi->idx],
  4586. pf->vsi[vsi->idx]->type,
  4587. vsi->idx, vsi, vsi->type);
  4588. goto unlock_vsi;
  4589. }
  4590. /* updates the pf for this cleared vsi */
  4591. i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
  4592. i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
  4593. i40e_vsi_free_arrays(vsi, true);
  4594. pf->vsi[vsi->idx] = NULL;
  4595. if (vsi->idx < pf->next_vsi)
  4596. pf->next_vsi = vsi->idx;
  4597. unlock_vsi:
  4598. mutex_unlock(&pf->switch_mutex);
  4599. free_vsi:
  4600. kfree(vsi);
  4601. return 0;
  4602. }
  4603. /**
  4604. * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
  4605. * @vsi: the VSI being cleaned
  4606. **/
  4607. static s32 i40e_vsi_clear_rings(struct i40e_vsi *vsi)
  4608. {
  4609. int i;
  4610. if (vsi->tx_rings[0])
  4611. for (i = 0; i < vsi->num_queue_pairs; i++) {
  4612. kfree_rcu(vsi->tx_rings[i], rcu);
  4613. vsi->tx_rings[i] = NULL;
  4614. vsi->rx_rings[i] = NULL;
  4615. }
  4616. return 0;
  4617. }
  4618. /**
  4619. * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
  4620. * @vsi: the VSI being configured
  4621. **/
  4622. static int i40e_alloc_rings(struct i40e_vsi *vsi)
  4623. {
  4624. struct i40e_pf *pf = vsi->back;
  4625. int i;
  4626. /* Set basic values in the rings to be used later during open() */
  4627. for (i = 0; i < vsi->num_queue_pairs; i++) {
  4628. struct i40e_ring *tx_ring;
  4629. struct i40e_ring *rx_ring;
  4630. /* allocate space for both Tx and Rx in one shot */
  4631. tx_ring = kzalloc(sizeof(struct i40e_ring) * 2, GFP_KERNEL);
  4632. if (!tx_ring)
  4633. goto err_out;
  4634. tx_ring->queue_index = i;
  4635. tx_ring->reg_idx = vsi->base_queue + i;
  4636. tx_ring->ring_active = false;
  4637. tx_ring->vsi = vsi;
  4638. tx_ring->netdev = vsi->netdev;
  4639. tx_ring->dev = &pf->pdev->dev;
  4640. tx_ring->count = vsi->num_desc;
  4641. tx_ring->size = 0;
  4642. tx_ring->dcb_tc = 0;
  4643. vsi->tx_rings[i] = tx_ring;
  4644. rx_ring = &tx_ring[1];
  4645. rx_ring->queue_index = i;
  4646. rx_ring->reg_idx = vsi->base_queue + i;
  4647. rx_ring->ring_active = false;
  4648. rx_ring->vsi = vsi;
  4649. rx_ring->netdev = vsi->netdev;
  4650. rx_ring->dev = &pf->pdev->dev;
  4651. rx_ring->count = vsi->num_desc;
  4652. rx_ring->size = 0;
  4653. rx_ring->dcb_tc = 0;
  4654. if (pf->flags & I40E_FLAG_16BYTE_RX_DESC_ENABLED)
  4655. set_ring_16byte_desc_enabled(rx_ring);
  4656. else
  4657. clear_ring_16byte_desc_enabled(rx_ring);
  4658. vsi->rx_rings[i] = rx_ring;
  4659. }
  4660. return 0;
  4661. err_out:
  4662. i40e_vsi_clear_rings(vsi);
  4663. return -ENOMEM;
  4664. }
  4665. /**
  4666. * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
  4667. * @pf: board private structure
  4668. * @vectors: the number of MSI-X vectors to request
  4669. *
  4670. * Returns the number of vectors reserved, or error
  4671. **/
  4672. static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
  4673. {
  4674. int err = 0;
  4675. pf->num_msix_entries = 0;
  4676. while (vectors >= I40E_MIN_MSIX) {
  4677. err = pci_enable_msix(pf->pdev, pf->msix_entries, vectors);
  4678. if (err == 0) {
  4679. /* good to go */
  4680. pf->num_msix_entries = vectors;
  4681. break;
  4682. } else if (err < 0) {
  4683. /* total failure */
  4684. dev_info(&pf->pdev->dev,
  4685. "MSI-X vector reservation failed: %d\n", err);
  4686. vectors = 0;
  4687. break;
  4688. } else {
  4689. /* err > 0 is the hint for retry */
  4690. dev_info(&pf->pdev->dev,
  4691. "MSI-X vectors wanted %d, retrying with %d\n",
  4692. vectors, err);
  4693. vectors = err;
  4694. }
  4695. }
  4696. if (vectors > 0 && vectors < I40E_MIN_MSIX) {
  4697. dev_info(&pf->pdev->dev,
  4698. "Couldn't get enough vectors, only %d available\n",
  4699. vectors);
  4700. vectors = 0;
  4701. }
  4702. return vectors;
  4703. }
  4704. /**
  4705. * i40e_init_msix - Setup the MSIX capability
  4706. * @pf: board private structure
  4707. *
  4708. * Work with the OS to set up the MSIX vectors needed.
  4709. *
  4710. * Returns 0 on success, negative on failure
  4711. **/
  4712. static int i40e_init_msix(struct i40e_pf *pf)
  4713. {
  4714. i40e_status err = 0;
  4715. struct i40e_hw *hw = &pf->hw;
  4716. int v_budget, i;
  4717. int vec;
  4718. if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
  4719. return -ENODEV;
  4720. /* The number of vectors we'll request will be comprised of:
  4721. * - Add 1 for "other" cause for Admin Queue events, etc.
  4722. * - The number of LAN queue pairs
  4723. * - Queues being used for RSS.
  4724. * We don't need as many as max_rss_size vectors.
  4725. * use rss_size instead in the calculation since that
  4726. * is governed by number of cpus in the system.
  4727. * - assumes symmetric Tx/Rx pairing
  4728. * - The number of VMDq pairs
  4729. * Once we count this up, try the request.
  4730. *
  4731. * If we can't get what we want, we'll simplify to nearly nothing
  4732. * and try again. If that still fails, we punt.
  4733. */
  4734. pf->num_lan_msix = pf->num_lan_qps - (pf->rss_size_max - pf->rss_size);
  4735. pf->num_vmdq_msix = pf->num_vmdq_qps;
  4736. v_budget = 1 + pf->num_lan_msix;
  4737. v_budget += (pf->num_vmdq_vsis * pf->num_vmdq_msix);
  4738. if (pf->flags & I40E_FLAG_FDIR_ENABLED)
  4739. v_budget++;
  4740. /* Scale down if necessary, and the rings will share vectors */
  4741. v_budget = min_t(int, v_budget, hw->func_caps.num_msix_vectors);
  4742. pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
  4743. GFP_KERNEL);
  4744. if (!pf->msix_entries)
  4745. return -ENOMEM;
  4746. for (i = 0; i < v_budget; i++)
  4747. pf->msix_entries[i].entry = i;
  4748. vec = i40e_reserve_msix_vectors(pf, v_budget);
  4749. if (vec < I40E_MIN_MSIX) {
  4750. pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
  4751. kfree(pf->msix_entries);
  4752. pf->msix_entries = NULL;
  4753. return -ENODEV;
  4754. } else if (vec == I40E_MIN_MSIX) {
  4755. /* Adjust for minimal MSIX use */
  4756. dev_info(&pf->pdev->dev, "Features disabled, not enough MSIX vectors\n");
  4757. pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
  4758. pf->num_vmdq_vsis = 0;
  4759. pf->num_vmdq_qps = 0;
  4760. pf->num_vmdq_msix = 0;
  4761. pf->num_lan_qps = 1;
  4762. pf->num_lan_msix = 1;
  4763. } else if (vec != v_budget) {
  4764. /* Scale vector usage down */
  4765. pf->num_vmdq_msix = 1; /* force VMDqs to only one vector */
  4766. vec--; /* reserve the misc vector */
  4767. /* partition out the remaining vectors */
  4768. switch (vec) {
  4769. case 2:
  4770. pf->num_vmdq_vsis = 1;
  4771. pf->num_lan_msix = 1;
  4772. break;
  4773. case 3:
  4774. pf->num_vmdq_vsis = 1;
  4775. pf->num_lan_msix = 2;
  4776. break;
  4777. default:
  4778. pf->num_lan_msix = min_t(int, (vec / 2),
  4779. pf->num_lan_qps);
  4780. pf->num_vmdq_vsis = min_t(int, (vec - pf->num_lan_msix),
  4781. I40E_DEFAULT_NUM_VMDQ_VSI);
  4782. break;
  4783. }
  4784. }
  4785. return err;
  4786. }
  4787. /**
  4788. * i40e_alloc_q_vector - Allocate memory for a single interrupt vector
  4789. * @vsi: the VSI being configured
  4790. * @v_idx: index of the vector in the vsi struct
  4791. *
  4792. * We allocate one q_vector. If allocation fails we return -ENOMEM.
  4793. **/
  4794. static int i40e_alloc_q_vector(struct i40e_vsi *vsi, int v_idx)
  4795. {
  4796. struct i40e_q_vector *q_vector;
  4797. /* allocate q_vector */
  4798. q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
  4799. if (!q_vector)
  4800. return -ENOMEM;
  4801. q_vector->vsi = vsi;
  4802. q_vector->v_idx = v_idx;
  4803. cpumask_set_cpu(v_idx, &q_vector->affinity_mask);
  4804. if (vsi->netdev)
  4805. netif_napi_add(vsi->netdev, &q_vector->napi,
  4806. i40e_napi_poll, vsi->work_limit);
  4807. q_vector->rx.latency_range = I40E_LOW_LATENCY;
  4808. q_vector->tx.latency_range = I40E_LOW_LATENCY;
  4809. /* tie q_vector and vsi together */
  4810. vsi->q_vectors[v_idx] = q_vector;
  4811. return 0;
  4812. }
  4813. /**
  4814. * i40e_alloc_q_vectors - Allocate memory for interrupt vectors
  4815. * @vsi: the VSI being configured
  4816. *
  4817. * We allocate one q_vector per queue interrupt. If allocation fails we
  4818. * return -ENOMEM.
  4819. **/
  4820. static int i40e_alloc_q_vectors(struct i40e_vsi *vsi)
  4821. {
  4822. struct i40e_pf *pf = vsi->back;
  4823. int v_idx, num_q_vectors;
  4824. int err;
  4825. /* if not MSIX, give the one vector only to the LAN VSI */
  4826. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  4827. num_q_vectors = vsi->num_q_vectors;
  4828. else if (vsi == pf->vsi[pf->lan_vsi])
  4829. num_q_vectors = 1;
  4830. else
  4831. return -EINVAL;
  4832. for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
  4833. err = i40e_alloc_q_vector(vsi, v_idx);
  4834. if (err)
  4835. goto err_out;
  4836. }
  4837. return 0;
  4838. err_out:
  4839. while (v_idx--)
  4840. i40e_free_q_vector(vsi, v_idx);
  4841. return err;
  4842. }
  4843. /**
  4844. * i40e_init_interrupt_scheme - Determine proper interrupt scheme
  4845. * @pf: board private structure to initialize
  4846. **/
  4847. static void i40e_init_interrupt_scheme(struct i40e_pf *pf)
  4848. {
  4849. int err = 0;
  4850. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  4851. err = i40e_init_msix(pf);
  4852. if (err) {
  4853. pf->flags &= ~(I40E_FLAG_MSIX_ENABLED |
  4854. I40E_FLAG_RSS_ENABLED |
  4855. I40E_FLAG_MQ_ENABLED |
  4856. I40E_FLAG_DCB_ENABLED |
  4857. I40E_FLAG_SRIOV_ENABLED |
  4858. I40E_FLAG_FDIR_ENABLED |
  4859. I40E_FLAG_FDIR_ATR_ENABLED |
  4860. I40E_FLAG_VMDQ_ENABLED);
  4861. /* rework the queue expectations without MSIX */
  4862. i40e_determine_queue_usage(pf);
  4863. }
  4864. }
  4865. if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
  4866. (pf->flags & I40E_FLAG_MSI_ENABLED)) {
  4867. dev_info(&pf->pdev->dev, "MSIX not available, trying MSI\n");
  4868. err = pci_enable_msi(pf->pdev);
  4869. if (err) {
  4870. dev_info(&pf->pdev->dev, "MSI init failed - %d\n", err);
  4871. pf->flags &= ~I40E_FLAG_MSI_ENABLED;
  4872. }
  4873. }
  4874. if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
  4875. dev_info(&pf->pdev->dev, "MSIX and MSI not available, falling back to Legacy IRQ\n");
  4876. /* track first vector for misc interrupts */
  4877. err = i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT-1);
  4878. }
  4879. /**
  4880. * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
  4881. * @pf: board private structure
  4882. *
  4883. * This sets up the handler for MSIX 0, which is used to manage the
  4884. * non-queue interrupts, e.g. AdminQ and errors. This is not used
  4885. * when in MSI or Legacy interrupt mode.
  4886. **/
  4887. static int i40e_setup_misc_vector(struct i40e_pf *pf)
  4888. {
  4889. struct i40e_hw *hw = &pf->hw;
  4890. int err = 0;
  4891. /* Only request the irq if this is the first time through, and
  4892. * not when we're rebuilding after a Reset
  4893. */
  4894. if (!test_bit(__I40E_RESET_RECOVERY_PENDING, &pf->state)) {
  4895. err = request_irq(pf->msix_entries[0].vector,
  4896. i40e_intr, 0, pf->misc_int_name, pf);
  4897. if (err) {
  4898. dev_info(&pf->pdev->dev,
  4899. "request_irq for msix_misc failed: %d\n", err);
  4900. return -EFAULT;
  4901. }
  4902. }
  4903. i40e_enable_misc_int_causes(hw);
  4904. /* associate no queues to the misc vector */
  4905. wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
  4906. wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K);
  4907. i40e_flush(hw);
  4908. i40e_irq_dynamic_enable_icr0(pf);
  4909. return err;
  4910. }
  4911. /**
  4912. * i40e_config_rss - Prepare for RSS if used
  4913. * @pf: board private structure
  4914. **/
  4915. static int i40e_config_rss(struct i40e_pf *pf)
  4916. {
  4917. const u64 default_hena =
  4918. ((u64)1 << I40E_FILTER_PCTYPE_NONF_UNICAST_IPV4_UDP) |
  4919. ((u64)1 << I40E_FILTER_PCTYPE_NONF_MULTICAST_IPV4_UDP) |
  4920. ((u64)1 << I40E_FILTER_PCTYPE_NONF_IPV4_UDP) |
  4921. ((u64)1 << I40E_FILTER_PCTYPE_NONF_IPV4_SCTP) |
  4922. ((u64)1 << I40E_FILTER_PCTYPE_NONF_IPV4_TCP_SYN) |
  4923. ((u64)1 << I40E_FILTER_PCTYPE_NONF_IPV4_TCP) |
  4924. ((u64)1 << I40E_FILTER_PCTYPE_NONF_IPV4_OTHER) |
  4925. ((u64)1 << I40E_FILTER_PCTYPE_FRAG_IPV4) |
  4926. ((u64)1 << I40E_FILTER_PCTYPE_NONF_UNICAST_IPV6_UDP) |
  4927. ((u64)1 << I40E_FILTER_PCTYPE_NONF_MULTICAST_IPV6_UDP) |
  4928. ((u64)1 << I40E_FILTER_PCTYPE_NONF_IPV6_UDP) |
  4929. ((u64)1 << I40E_FILTER_PCTYPE_NONF_IPV6_TCP_SYN) |
  4930. ((u64)1 << I40E_FILTER_PCTYPE_NONF_IPV6_TCP) |
  4931. ((u64)1 << I40E_FILTER_PCTYPE_NONF_IPV6_SCTP) |
  4932. ((u64)1 << I40E_FILTER_PCTYPE_NONF_IPV6_OTHER) |
  4933. ((u64)1 << I40E_FILTER_PCTYPE_FRAG_IPV6) |
  4934. ((u64)1 << I40E_FILTER_PCTYPE_L2_PAYLOAD);
  4935. /* Set of random keys generated using kernel random number generator */
  4936. static const u32 seed[I40E_PFQF_HKEY_MAX_INDEX + 1] = {0x41b01687,
  4937. 0x183cfd8c, 0xce880440, 0x580cbc3c, 0x35897377,
  4938. 0x328b25e1, 0x4fa98922, 0xb7d90c14, 0xd5bad70d,
  4939. 0xcd15a2c1, 0xe8580225, 0x4a1e9d11, 0xfe5731be};
  4940. struct i40e_hw *hw = &pf->hw;
  4941. u32 lut = 0;
  4942. int i, j;
  4943. u64 hena;
  4944. /* Fill out hash function seed */
  4945. for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
  4946. wr32(hw, I40E_PFQF_HKEY(i), seed[i]);
  4947. /* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
  4948. hena = (u64)rd32(hw, I40E_PFQF_HENA(0)) |
  4949. ((u64)rd32(hw, I40E_PFQF_HENA(1)) << 32);
  4950. hena |= default_hena;
  4951. wr32(hw, I40E_PFQF_HENA(0), (u32)hena);
  4952. wr32(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
  4953. /* Populate the LUT with max no. of queues in round robin fashion */
  4954. for (i = 0, j = 0; i < pf->hw.func_caps.rss_table_size; i++, j++) {
  4955. /* The assumption is that lan qp count will be the highest
  4956. * qp count for any PF VSI that needs RSS.
  4957. * If multiple VSIs need RSS support, all the qp counts
  4958. * for those VSIs should be a power of 2 for RSS to work.
  4959. * If LAN VSI is the only consumer for RSS then this requirement
  4960. * is not necessary.
  4961. */
  4962. if (j == pf->rss_size)
  4963. j = 0;
  4964. /* lut = 4-byte sliding window of 4 lut entries */
  4965. lut = (lut << 8) | (j &
  4966. ((0x1 << pf->hw.func_caps.rss_table_entry_width) - 1));
  4967. /* On i = 3, we have 4 entries in lut; write to the register */
  4968. if ((i & 3) == 3)
  4969. wr32(hw, I40E_PFQF_HLUT(i >> 2), lut);
  4970. }
  4971. i40e_flush(hw);
  4972. return 0;
  4973. }
  4974. /**
  4975. * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
  4976. * @pf: board private structure
  4977. * @queue_count: the requested queue count for rss.
  4978. *
  4979. * returns 0 if rss is not enabled, if enabled returns the final rss queue
  4980. * count which may be different from the requested queue count.
  4981. **/
  4982. int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
  4983. {
  4984. if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
  4985. return 0;
  4986. queue_count = min_t(int, queue_count, pf->rss_size_max);
  4987. queue_count = rounddown_pow_of_two(queue_count);
  4988. if (queue_count != pf->rss_size) {
  4989. if (pf->queues_left < (queue_count - pf->rss_size)) {
  4990. dev_info(&pf->pdev->dev,
  4991. "Not enough queues to do RSS on %d queues: remaining queues %d\n",
  4992. queue_count, pf->queues_left);
  4993. return pf->rss_size;
  4994. }
  4995. i40e_prep_for_reset(pf);
  4996. pf->num_lan_qps += (queue_count - pf->rss_size);
  4997. pf->queues_left -= (queue_count - pf->rss_size);
  4998. pf->rss_size = queue_count;
  4999. i40e_reset_and_rebuild(pf, true);
  5000. i40e_config_rss(pf);
  5001. }
  5002. dev_info(&pf->pdev->dev, "RSS count: %d\n", pf->rss_size);
  5003. return pf->rss_size;
  5004. }
  5005. /**
  5006. * i40e_sw_init - Initialize general software structures (struct i40e_pf)
  5007. * @pf: board private structure to initialize
  5008. *
  5009. * i40e_sw_init initializes the Adapter private data structure.
  5010. * Fields are initialized based on PCI device information and
  5011. * OS network device settings (MTU size).
  5012. **/
  5013. static int i40e_sw_init(struct i40e_pf *pf)
  5014. {
  5015. int err = 0;
  5016. int size;
  5017. pf->msg_enable = netif_msg_init(I40E_DEFAULT_MSG_ENABLE,
  5018. (NETIF_MSG_DRV|NETIF_MSG_PROBE|NETIF_MSG_LINK));
  5019. pf->hw.debug_mask = pf->msg_enable | I40E_DEBUG_DIAG;
  5020. if (debug != -1 && debug != I40E_DEFAULT_MSG_ENABLE) {
  5021. if (I40E_DEBUG_USER & debug)
  5022. pf->hw.debug_mask = debug;
  5023. pf->msg_enable = netif_msg_init((debug & ~I40E_DEBUG_USER),
  5024. I40E_DEFAULT_MSG_ENABLE);
  5025. }
  5026. /* Set default capability flags */
  5027. pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
  5028. I40E_FLAG_MSI_ENABLED |
  5029. I40E_FLAG_MSIX_ENABLED |
  5030. I40E_FLAG_RX_PS_ENABLED |
  5031. I40E_FLAG_MQ_ENABLED |
  5032. I40E_FLAG_RX_1BUF_ENABLED;
  5033. /* Depending on PF configurations, it is possible that the RSS
  5034. * maximum might end up larger than the available queues
  5035. */
  5036. pf->rss_size_max = 0x1 << pf->hw.func_caps.rss_table_entry_width;
  5037. pf->rss_size_max = min_t(int, pf->rss_size_max,
  5038. pf->hw.func_caps.num_tx_qp);
  5039. if (pf->hw.func_caps.rss) {
  5040. pf->flags |= I40E_FLAG_RSS_ENABLED;
  5041. pf->rss_size = min_t(int, pf->rss_size_max, num_online_cpus());
  5042. } else {
  5043. pf->rss_size = 1;
  5044. }
  5045. if (pf->hw.func_caps.dcb)
  5046. pf->num_tc_qps = I40E_DEFAULT_QUEUES_PER_TC;
  5047. else
  5048. pf->num_tc_qps = 0;
  5049. if (pf->hw.func_caps.fd) {
  5050. /* FW/NVM is not yet fixed in this regard */
  5051. if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
  5052. (pf->hw.func_caps.fd_filters_best_effort > 0)) {
  5053. pf->flags |= I40E_FLAG_FDIR_ATR_ENABLED;
  5054. dev_info(&pf->pdev->dev,
  5055. "Flow Director ATR mode Enabled\n");
  5056. pf->flags |= I40E_FLAG_FDIR_ENABLED;
  5057. dev_info(&pf->pdev->dev,
  5058. "Flow Director Side Band mode Enabled\n");
  5059. pf->fdir_pf_filter_count =
  5060. pf->hw.func_caps.fd_filters_guaranteed;
  5061. }
  5062. } else {
  5063. pf->fdir_pf_filter_count = 0;
  5064. }
  5065. if (pf->hw.func_caps.vmdq) {
  5066. pf->flags |= I40E_FLAG_VMDQ_ENABLED;
  5067. pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
  5068. pf->num_vmdq_qps = I40E_DEFAULT_QUEUES_PER_VMDQ;
  5069. }
  5070. /* MFP mode enabled */
  5071. if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.mfp_mode_1) {
  5072. pf->flags |= I40E_FLAG_MFP_ENABLED;
  5073. dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
  5074. }
  5075. #ifdef CONFIG_PCI_IOV
  5076. if (pf->hw.func_caps.num_vfs) {
  5077. pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
  5078. pf->flags |= I40E_FLAG_SRIOV_ENABLED;
  5079. pf->num_req_vfs = min_t(int,
  5080. pf->hw.func_caps.num_vfs,
  5081. I40E_MAX_VF_COUNT);
  5082. dev_info(&pf->pdev->dev,
  5083. "Number of VFs being requested for PF[%d] = %d\n",
  5084. pf->hw.pf_id, pf->num_req_vfs);
  5085. }
  5086. #endif /* CONFIG_PCI_IOV */
  5087. pf->eeprom_version = 0xDEAD;
  5088. pf->lan_veb = I40E_NO_VEB;
  5089. pf->lan_vsi = I40E_NO_VSI;
  5090. /* set up queue assignment tracking */
  5091. size = sizeof(struct i40e_lump_tracking)
  5092. + (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
  5093. pf->qp_pile = kzalloc(size, GFP_KERNEL);
  5094. if (!pf->qp_pile) {
  5095. err = -ENOMEM;
  5096. goto sw_init_done;
  5097. }
  5098. pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
  5099. pf->qp_pile->search_hint = 0;
  5100. /* set up vector assignment tracking */
  5101. size = sizeof(struct i40e_lump_tracking)
  5102. + (sizeof(u16) * pf->hw.func_caps.num_msix_vectors);
  5103. pf->irq_pile = kzalloc(size, GFP_KERNEL);
  5104. if (!pf->irq_pile) {
  5105. kfree(pf->qp_pile);
  5106. err = -ENOMEM;
  5107. goto sw_init_done;
  5108. }
  5109. pf->irq_pile->num_entries = pf->hw.func_caps.num_msix_vectors;
  5110. pf->irq_pile->search_hint = 0;
  5111. mutex_init(&pf->switch_mutex);
  5112. sw_init_done:
  5113. return err;
  5114. }
  5115. /**
  5116. * i40e_set_features - set the netdev feature flags
  5117. * @netdev: ptr to the netdev being adjusted
  5118. * @features: the feature set that the stack is suggesting
  5119. **/
  5120. static int i40e_set_features(struct net_device *netdev,
  5121. netdev_features_t features)
  5122. {
  5123. struct i40e_netdev_priv *np = netdev_priv(netdev);
  5124. struct i40e_vsi *vsi = np->vsi;
  5125. if (features & NETIF_F_HW_VLAN_CTAG_RX)
  5126. i40e_vlan_stripping_enable(vsi);
  5127. else
  5128. i40e_vlan_stripping_disable(vsi);
  5129. return 0;
  5130. }
  5131. static const struct net_device_ops i40e_netdev_ops = {
  5132. .ndo_open = i40e_open,
  5133. .ndo_stop = i40e_close,
  5134. .ndo_start_xmit = i40e_lan_xmit_frame,
  5135. .ndo_get_stats64 = i40e_get_netdev_stats_struct,
  5136. .ndo_set_rx_mode = i40e_set_rx_mode,
  5137. .ndo_validate_addr = eth_validate_addr,
  5138. .ndo_set_mac_address = i40e_set_mac,
  5139. .ndo_change_mtu = i40e_change_mtu,
  5140. .ndo_tx_timeout = i40e_tx_timeout,
  5141. .ndo_vlan_rx_add_vid = i40e_vlan_rx_add_vid,
  5142. .ndo_vlan_rx_kill_vid = i40e_vlan_rx_kill_vid,
  5143. #ifdef CONFIG_NET_POLL_CONTROLLER
  5144. .ndo_poll_controller = i40e_netpoll,
  5145. #endif
  5146. .ndo_setup_tc = i40e_setup_tc,
  5147. .ndo_set_features = i40e_set_features,
  5148. .ndo_set_vf_mac = i40e_ndo_set_vf_mac,
  5149. .ndo_set_vf_vlan = i40e_ndo_set_vf_port_vlan,
  5150. .ndo_set_vf_tx_rate = i40e_ndo_set_vf_bw,
  5151. .ndo_get_vf_config = i40e_ndo_get_vf_config,
  5152. };
  5153. /**
  5154. * i40e_config_netdev - Setup the netdev flags
  5155. * @vsi: the VSI being configured
  5156. *
  5157. * Returns 0 on success, negative value on failure
  5158. **/
  5159. static int i40e_config_netdev(struct i40e_vsi *vsi)
  5160. {
  5161. struct i40e_pf *pf = vsi->back;
  5162. struct i40e_hw *hw = &pf->hw;
  5163. struct i40e_netdev_priv *np;
  5164. struct net_device *netdev;
  5165. u8 mac_addr[ETH_ALEN];
  5166. int etherdev_size;
  5167. etherdev_size = sizeof(struct i40e_netdev_priv);
  5168. netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
  5169. if (!netdev)
  5170. return -ENOMEM;
  5171. vsi->netdev = netdev;
  5172. np = netdev_priv(netdev);
  5173. np->vsi = vsi;
  5174. netdev->hw_enc_features = NETIF_F_IP_CSUM |
  5175. NETIF_F_GSO_UDP_TUNNEL |
  5176. NETIF_F_TSO |
  5177. NETIF_F_SG;
  5178. netdev->features = NETIF_F_SG |
  5179. NETIF_F_IP_CSUM |
  5180. NETIF_F_SCTP_CSUM |
  5181. NETIF_F_HIGHDMA |
  5182. NETIF_F_GSO_UDP_TUNNEL |
  5183. NETIF_F_HW_VLAN_CTAG_TX |
  5184. NETIF_F_HW_VLAN_CTAG_RX |
  5185. NETIF_F_HW_VLAN_CTAG_FILTER |
  5186. NETIF_F_IPV6_CSUM |
  5187. NETIF_F_TSO |
  5188. NETIF_F_TSO6 |
  5189. NETIF_F_RXCSUM |
  5190. NETIF_F_RXHASH |
  5191. 0;
  5192. /* copy netdev features into list of user selectable features */
  5193. netdev->hw_features |= netdev->features;
  5194. if (vsi->type == I40E_VSI_MAIN) {
  5195. SET_NETDEV_DEV(netdev, &pf->pdev->dev);
  5196. memcpy(mac_addr, hw->mac.perm_addr, ETH_ALEN);
  5197. } else {
  5198. /* relate the VSI_VMDQ name to the VSI_MAIN name */
  5199. snprintf(netdev->name, IFNAMSIZ, "%sv%%d",
  5200. pf->vsi[pf->lan_vsi]->netdev->name);
  5201. random_ether_addr(mac_addr);
  5202. i40e_add_filter(vsi, mac_addr, I40E_VLAN_ANY, false, false);
  5203. }
  5204. memcpy(netdev->dev_addr, mac_addr, ETH_ALEN);
  5205. memcpy(netdev->perm_addr, mac_addr, ETH_ALEN);
  5206. /* vlan gets same features (except vlan offload)
  5207. * after any tweaks for specific VSI types
  5208. */
  5209. netdev->vlan_features = netdev->features & ~(NETIF_F_HW_VLAN_CTAG_TX |
  5210. NETIF_F_HW_VLAN_CTAG_RX |
  5211. NETIF_F_HW_VLAN_CTAG_FILTER);
  5212. netdev->priv_flags |= IFF_UNICAST_FLT;
  5213. netdev->priv_flags |= IFF_SUPP_NOFCS;
  5214. /* Setup netdev TC information */
  5215. i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
  5216. netdev->netdev_ops = &i40e_netdev_ops;
  5217. netdev->watchdog_timeo = 5 * HZ;
  5218. i40e_set_ethtool_ops(netdev);
  5219. return 0;
  5220. }
  5221. /**
  5222. * i40e_vsi_delete - Delete a VSI from the switch
  5223. * @vsi: the VSI being removed
  5224. *
  5225. * Returns 0 on success, negative value on failure
  5226. **/
  5227. static void i40e_vsi_delete(struct i40e_vsi *vsi)
  5228. {
  5229. /* remove default VSI is not allowed */
  5230. if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
  5231. return;
  5232. /* there is no HW VSI for FDIR */
  5233. if (vsi->type == I40E_VSI_FDIR)
  5234. return;
  5235. i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
  5236. return;
  5237. }
  5238. /**
  5239. * i40e_add_vsi - Add a VSI to the switch
  5240. * @vsi: the VSI being configured
  5241. *
  5242. * This initializes a VSI context depending on the VSI type to be added and
  5243. * passes it down to the add_vsi aq command.
  5244. **/
  5245. static int i40e_add_vsi(struct i40e_vsi *vsi)
  5246. {
  5247. int ret = -ENODEV;
  5248. struct i40e_mac_filter *f, *ftmp;
  5249. struct i40e_pf *pf = vsi->back;
  5250. struct i40e_hw *hw = &pf->hw;
  5251. struct i40e_vsi_context ctxt;
  5252. u8 enabled_tc = 0x1; /* TC0 enabled */
  5253. int f_count = 0;
  5254. memset(&ctxt, 0, sizeof(ctxt));
  5255. switch (vsi->type) {
  5256. case I40E_VSI_MAIN:
  5257. /* The PF's main VSI is already setup as part of the
  5258. * device initialization, so we'll not bother with
  5259. * the add_vsi call, but we will retrieve the current
  5260. * VSI context.
  5261. */
  5262. ctxt.seid = pf->main_vsi_seid;
  5263. ctxt.pf_num = pf->hw.pf_id;
  5264. ctxt.vf_num = 0;
  5265. ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
  5266. ctxt.flags = I40E_AQ_VSI_TYPE_PF;
  5267. if (ret) {
  5268. dev_info(&pf->pdev->dev,
  5269. "couldn't get pf vsi config, err %d, aq_err %d\n",
  5270. ret, pf->hw.aq.asq_last_status);
  5271. return -ENOENT;
  5272. }
  5273. memcpy(&vsi->info, &ctxt.info, sizeof(ctxt.info));
  5274. vsi->info.valid_sections = 0;
  5275. vsi->seid = ctxt.seid;
  5276. vsi->id = ctxt.vsi_number;
  5277. enabled_tc = i40e_pf_get_tc_map(pf);
  5278. /* MFP mode setup queue map and update VSI */
  5279. if (pf->flags & I40E_FLAG_MFP_ENABLED) {
  5280. memset(&ctxt, 0, sizeof(ctxt));
  5281. ctxt.seid = pf->main_vsi_seid;
  5282. ctxt.pf_num = pf->hw.pf_id;
  5283. ctxt.vf_num = 0;
  5284. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
  5285. ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
  5286. if (ret) {
  5287. dev_info(&pf->pdev->dev,
  5288. "update vsi failed, aq_err=%d\n",
  5289. pf->hw.aq.asq_last_status);
  5290. ret = -ENOENT;
  5291. goto err;
  5292. }
  5293. /* update the local VSI info queue map */
  5294. i40e_vsi_update_queue_map(vsi, &ctxt);
  5295. vsi->info.valid_sections = 0;
  5296. } else {
  5297. /* Default/Main VSI is only enabled for TC0
  5298. * reconfigure it to enable all TCs that are
  5299. * available on the port in SFP mode.
  5300. */
  5301. ret = i40e_vsi_config_tc(vsi, enabled_tc);
  5302. if (ret) {
  5303. dev_info(&pf->pdev->dev,
  5304. "failed to configure TCs for main VSI tc_map 0x%08x, err %d, aq_err %d\n",
  5305. enabled_tc, ret,
  5306. pf->hw.aq.asq_last_status);
  5307. ret = -ENOENT;
  5308. }
  5309. }
  5310. break;
  5311. case I40E_VSI_FDIR:
  5312. /* no queue mapping or actual HW VSI needed */
  5313. vsi->info.valid_sections = 0;
  5314. vsi->seid = 0;
  5315. vsi->id = 0;
  5316. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
  5317. return 0;
  5318. break;
  5319. case I40E_VSI_VMDQ2:
  5320. ctxt.pf_num = hw->pf_id;
  5321. ctxt.vf_num = 0;
  5322. ctxt.uplink_seid = vsi->uplink_seid;
  5323. ctxt.connection_type = 0x1; /* regular data port */
  5324. ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
  5325. ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  5326. /* This VSI is connected to VEB so the switch_id
  5327. * should be set to zero by default.
  5328. */
  5329. ctxt.info.switch_id = 0;
  5330. ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_LOCAL_LB);
  5331. ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  5332. /* Setup the VSI tx/rx queue map for TC0 only for now */
  5333. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
  5334. break;
  5335. case I40E_VSI_SRIOV:
  5336. ctxt.pf_num = hw->pf_id;
  5337. ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
  5338. ctxt.uplink_seid = vsi->uplink_seid;
  5339. ctxt.connection_type = 0x1; /* regular data port */
  5340. ctxt.flags = I40E_AQ_VSI_TYPE_VF;
  5341. ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  5342. /* This VSI is connected to VEB so the switch_id
  5343. * should be set to zero by default.
  5344. */
  5345. ctxt.info.switch_id = cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  5346. ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
  5347. ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
  5348. /* Setup the VSI tx/rx queue map for TC0 only for now */
  5349. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
  5350. break;
  5351. default:
  5352. return -ENODEV;
  5353. }
  5354. if (vsi->type != I40E_VSI_MAIN) {
  5355. ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
  5356. if (ret) {
  5357. dev_info(&vsi->back->pdev->dev,
  5358. "add vsi failed, aq_err=%d\n",
  5359. vsi->back->hw.aq.asq_last_status);
  5360. ret = -ENOENT;
  5361. goto err;
  5362. }
  5363. memcpy(&vsi->info, &ctxt.info, sizeof(ctxt.info));
  5364. vsi->info.valid_sections = 0;
  5365. vsi->seid = ctxt.seid;
  5366. vsi->id = ctxt.vsi_number;
  5367. }
  5368. /* If macvlan filters already exist, force them to get loaded */
  5369. list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list) {
  5370. f->changed = true;
  5371. f_count++;
  5372. }
  5373. if (f_count) {
  5374. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  5375. pf->flags |= I40E_FLAG_FILTER_SYNC;
  5376. }
  5377. /* Update VSI BW information */
  5378. ret = i40e_vsi_get_bw_info(vsi);
  5379. if (ret) {
  5380. dev_info(&pf->pdev->dev,
  5381. "couldn't get vsi bw info, err %d, aq_err %d\n",
  5382. ret, pf->hw.aq.asq_last_status);
  5383. /* VSI is already added so not tearing that up */
  5384. ret = 0;
  5385. }
  5386. err:
  5387. return ret;
  5388. }
  5389. /**
  5390. * i40e_vsi_release - Delete a VSI and free its resources
  5391. * @vsi: the VSI being removed
  5392. *
  5393. * Returns 0 on success or < 0 on error
  5394. **/
  5395. int i40e_vsi_release(struct i40e_vsi *vsi)
  5396. {
  5397. struct i40e_mac_filter *f, *ftmp;
  5398. struct i40e_veb *veb = NULL;
  5399. struct i40e_pf *pf;
  5400. u16 uplink_seid;
  5401. int i, n;
  5402. pf = vsi->back;
  5403. /* release of a VEB-owner or last VSI is not allowed */
  5404. if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
  5405. dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
  5406. vsi->seid, vsi->uplink_seid);
  5407. return -ENODEV;
  5408. }
  5409. if (vsi == pf->vsi[pf->lan_vsi] &&
  5410. !test_bit(__I40E_DOWN, &pf->state)) {
  5411. dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
  5412. return -ENODEV;
  5413. }
  5414. uplink_seid = vsi->uplink_seid;
  5415. if (vsi->type != I40E_VSI_SRIOV) {
  5416. if (vsi->netdev_registered) {
  5417. vsi->netdev_registered = false;
  5418. if (vsi->netdev) {
  5419. /* results in a call to i40e_close() */
  5420. unregister_netdev(vsi->netdev);
  5421. free_netdev(vsi->netdev);
  5422. vsi->netdev = NULL;
  5423. }
  5424. } else {
  5425. if (!test_and_set_bit(__I40E_DOWN, &vsi->state))
  5426. i40e_down(vsi);
  5427. i40e_vsi_free_irq(vsi);
  5428. i40e_vsi_free_tx_resources(vsi);
  5429. i40e_vsi_free_rx_resources(vsi);
  5430. }
  5431. i40e_vsi_disable_irq(vsi);
  5432. }
  5433. list_for_each_entry_safe(f, ftmp, &vsi->mac_filter_list, list)
  5434. i40e_del_filter(vsi, f->macaddr, f->vlan,
  5435. f->is_vf, f->is_netdev);
  5436. i40e_sync_vsi_filters(vsi);
  5437. i40e_vsi_delete(vsi);
  5438. i40e_vsi_free_q_vectors(vsi);
  5439. i40e_vsi_clear_rings(vsi);
  5440. i40e_vsi_clear(vsi);
  5441. /* If this was the last thing on the VEB, except for the
  5442. * controlling VSI, remove the VEB, which puts the controlling
  5443. * VSI onto the next level down in the switch.
  5444. *
  5445. * Well, okay, there's one more exception here: don't remove
  5446. * the orphan VEBs yet. We'll wait for an explicit remove request
  5447. * from up the network stack.
  5448. */
  5449. for (n = 0, i = 0; i < pf->hw.func_caps.num_vsis; i++) {
  5450. if (pf->vsi[i] &&
  5451. pf->vsi[i]->uplink_seid == uplink_seid &&
  5452. (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
  5453. n++; /* count the VSIs */
  5454. }
  5455. }
  5456. for (i = 0; i < I40E_MAX_VEB; i++) {
  5457. if (!pf->veb[i])
  5458. continue;
  5459. if (pf->veb[i]->uplink_seid == uplink_seid)
  5460. n++; /* count the VEBs */
  5461. if (pf->veb[i]->seid == uplink_seid)
  5462. veb = pf->veb[i];
  5463. }
  5464. if (n == 0 && veb && veb->uplink_seid != 0)
  5465. i40e_veb_release(veb);
  5466. return 0;
  5467. }
  5468. /**
  5469. * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
  5470. * @vsi: ptr to the VSI
  5471. *
  5472. * This should only be called after i40e_vsi_mem_alloc() which allocates the
  5473. * corresponding SW VSI structure and initializes num_queue_pairs for the
  5474. * newly allocated VSI.
  5475. *
  5476. * Returns 0 on success or negative on failure
  5477. **/
  5478. static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
  5479. {
  5480. int ret = -ENOENT;
  5481. struct i40e_pf *pf = vsi->back;
  5482. if (vsi->q_vectors[0]) {
  5483. dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
  5484. vsi->seid);
  5485. return -EEXIST;
  5486. }
  5487. if (vsi->base_vector) {
  5488. dev_info(&pf->pdev->dev,
  5489. "VSI %d has non-zero base vector %d\n",
  5490. vsi->seid, vsi->base_vector);
  5491. return -EEXIST;
  5492. }
  5493. ret = i40e_alloc_q_vectors(vsi);
  5494. if (ret) {
  5495. dev_info(&pf->pdev->dev,
  5496. "failed to allocate %d q_vector for VSI %d, ret=%d\n",
  5497. vsi->num_q_vectors, vsi->seid, ret);
  5498. vsi->num_q_vectors = 0;
  5499. goto vector_setup_out;
  5500. }
  5501. if (vsi->num_q_vectors)
  5502. vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
  5503. vsi->num_q_vectors, vsi->idx);
  5504. if (vsi->base_vector < 0) {
  5505. dev_info(&pf->pdev->dev,
  5506. "failed to get q tracking for VSI %d, err=%d\n",
  5507. vsi->seid, vsi->base_vector);
  5508. i40e_vsi_free_q_vectors(vsi);
  5509. ret = -ENOENT;
  5510. goto vector_setup_out;
  5511. }
  5512. vector_setup_out:
  5513. return ret;
  5514. }
  5515. /**
  5516. * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
  5517. * @vsi: pointer to the vsi.
  5518. *
  5519. * This re-allocates a vsi's queue resources.
  5520. *
  5521. * Returns pointer to the successfully allocated and configured VSI sw struct
  5522. * on success, otherwise returns NULL on failure.
  5523. **/
  5524. static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
  5525. {
  5526. struct i40e_pf *pf = vsi->back;
  5527. u8 enabled_tc;
  5528. int ret;
  5529. i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
  5530. i40e_vsi_clear_rings(vsi);
  5531. i40e_vsi_free_arrays(vsi, false);
  5532. i40e_set_num_rings_in_vsi(vsi);
  5533. ret = i40e_vsi_alloc_arrays(vsi, false);
  5534. if (ret)
  5535. goto err_vsi;
  5536. ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs, vsi->idx);
  5537. if (ret < 0) {
  5538. dev_info(&pf->pdev->dev, "VSI %d get_lump failed %d\n",
  5539. vsi->seid, ret);
  5540. goto err_vsi;
  5541. }
  5542. vsi->base_queue = ret;
  5543. /* Update the FW view of the VSI. Force a reset of TC and queue
  5544. * layout configurations.
  5545. */
  5546. enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
  5547. pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
  5548. pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
  5549. i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
  5550. /* assign it some queues */
  5551. ret = i40e_alloc_rings(vsi);
  5552. if (ret)
  5553. goto err_rings;
  5554. /* map all of the rings to the q_vectors */
  5555. i40e_vsi_map_rings_to_vectors(vsi);
  5556. return vsi;
  5557. err_rings:
  5558. i40e_vsi_free_q_vectors(vsi);
  5559. if (vsi->netdev_registered) {
  5560. vsi->netdev_registered = false;
  5561. unregister_netdev(vsi->netdev);
  5562. free_netdev(vsi->netdev);
  5563. vsi->netdev = NULL;
  5564. }
  5565. i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
  5566. err_vsi:
  5567. i40e_vsi_clear(vsi);
  5568. return NULL;
  5569. }
  5570. /**
  5571. * i40e_vsi_setup - Set up a VSI by a given type
  5572. * @pf: board private structure
  5573. * @type: VSI type
  5574. * @uplink_seid: the switch element to link to
  5575. * @param1: usage depends upon VSI type. For VF types, indicates VF id
  5576. *
  5577. * This allocates the sw VSI structure and its queue resources, then add a VSI
  5578. * to the identified VEB.
  5579. *
  5580. * Returns pointer to the successfully allocated and configure VSI sw struct on
  5581. * success, otherwise returns NULL on failure.
  5582. **/
  5583. struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
  5584. u16 uplink_seid, u32 param1)
  5585. {
  5586. struct i40e_vsi *vsi = NULL;
  5587. struct i40e_veb *veb = NULL;
  5588. int ret, i;
  5589. int v_idx;
  5590. /* The requested uplink_seid must be either
  5591. * - the PF's port seid
  5592. * no VEB is needed because this is the PF
  5593. * or this is a Flow Director special case VSI
  5594. * - seid of an existing VEB
  5595. * - seid of a VSI that owns an existing VEB
  5596. * - seid of a VSI that doesn't own a VEB
  5597. * a new VEB is created and the VSI becomes the owner
  5598. * - seid of the PF VSI, which is what creates the first VEB
  5599. * this is a special case of the previous
  5600. *
  5601. * Find which uplink_seid we were given and create a new VEB if needed
  5602. */
  5603. for (i = 0; i < I40E_MAX_VEB; i++) {
  5604. if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
  5605. veb = pf->veb[i];
  5606. break;
  5607. }
  5608. }
  5609. if (!veb && uplink_seid != pf->mac_seid) {
  5610. for (i = 0; i < pf->hw.func_caps.num_vsis; i++) {
  5611. if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
  5612. vsi = pf->vsi[i];
  5613. break;
  5614. }
  5615. }
  5616. if (!vsi) {
  5617. dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
  5618. uplink_seid);
  5619. return NULL;
  5620. }
  5621. if (vsi->uplink_seid == pf->mac_seid)
  5622. veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
  5623. vsi->tc_config.enabled_tc);
  5624. else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
  5625. veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
  5626. vsi->tc_config.enabled_tc);
  5627. for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
  5628. if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
  5629. veb = pf->veb[i];
  5630. }
  5631. if (!veb) {
  5632. dev_info(&pf->pdev->dev, "couldn't add VEB\n");
  5633. return NULL;
  5634. }
  5635. vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
  5636. uplink_seid = veb->seid;
  5637. }
  5638. /* get vsi sw struct */
  5639. v_idx = i40e_vsi_mem_alloc(pf, type);
  5640. if (v_idx < 0)
  5641. goto err_alloc;
  5642. vsi = pf->vsi[v_idx];
  5643. vsi->type = type;
  5644. vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
  5645. if (type == I40E_VSI_MAIN)
  5646. pf->lan_vsi = v_idx;
  5647. else if (type == I40E_VSI_SRIOV)
  5648. vsi->vf_id = param1;
  5649. /* assign it some queues */
  5650. ret = i40e_get_lump(pf, pf->qp_pile, vsi->alloc_queue_pairs, vsi->idx);
  5651. if (ret < 0) {
  5652. dev_info(&pf->pdev->dev, "VSI %d get_lump failed %d\n",
  5653. vsi->seid, ret);
  5654. goto err_vsi;
  5655. }
  5656. vsi->base_queue = ret;
  5657. /* get a VSI from the hardware */
  5658. vsi->uplink_seid = uplink_seid;
  5659. ret = i40e_add_vsi(vsi);
  5660. if (ret)
  5661. goto err_vsi;
  5662. switch (vsi->type) {
  5663. /* setup the netdev if needed */
  5664. case I40E_VSI_MAIN:
  5665. case I40E_VSI_VMDQ2:
  5666. ret = i40e_config_netdev(vsi);
  5667. if (ret)
  5668. goto err_netdev;
  5669. ret = register_netdev(vsi->netdev);
  5670. if (ret)
  5671. goto err_netdev;
  5672. vsi->netdev_registered = true;
  5673. netif_carrier_off(vsi->netdev);
  5674. /* fall through */
  5675. case I40E_VSI_FDIR:
  5676. /* set up vectors and rings if needed */
  5677. ret = i40e_vsi_setup_vectors(vsi);
  5678. if (ret)
  5679. goto err_msix;
  5680. ret = i40e_alloc_rings(vsi);
  5681. if (ret)
  5682. goto err_rings;
  5683. /* map all of the rings to the q_vectors */
  5684. i40e_vsi_map_rings_to_vectors(vsi);
  5685. i40e_vsi_reset_stats(vsi);
  5686. break;
  5687. default:
  5688. /* no netdev or rings for the other VSI types */
  5689. break;
  5690. }
  5691. return vsi;
  5692. err_rings:
  5693. i40e_vsi_free_q_vectors(vsi);
  5694. err_msix:
  5695. if (vsi->netdev_registered) {
  5696. vsi->netdev_registered = false;
  5697. unregister_netdev(vsi->netdev);
  5698. free_netdev(vsi->netdev);
  5699. vsi->netdev = NULL;
  5700. }
  5701. err_netdev:
  5702. i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
  5703. err_vsi:
  5704. i40e_vsi_clear(vsi);
  5705. err_alloc:
  5706. return NULL;
  5707. }
  5708. /**
  5709. * i40e_veb_get_bw_info - Query VEB BW information
  5710. * @veb: the veb to query
  5711. *
  5712. * Query the Tx scheduler BW configuration data for given VEB
  5713. **/
  5714. static int i40e_veb_get_bw_info(struct i40e_veb *veb)
  5715. {
  5716. struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
  5717. struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
  5718. struct i40e_pf *pf = veb->pf;
  5719. struct i40e_hw *hw = &pf->hw;
  5720. u32 tc_bw_max;
  5721. int ret = 0;
  5722. int i;
  5723. ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
  5724. &bw_data, NULL);
  5725. if (ret) {
  5726. dev_info(&pf->pdev->dev,
  5727. "query veb bw config failed, aq_err=%d\n",
  5728. hw->aq.asq_last_status);
  5729. goto out;
  5730. }
  5731. ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
  5732. &ets_data, NULL);
  5733. if (ret) {
  5734. dev_info(&pf->pdev->dev,
  5735. "query veb bw ets config failed, aq_err=%d\n",
  5736. hw->aq.asq_last_status);
  5737. goto out;
  5738. }
  5739. veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
  5740. veb->bw_max_quanta = ets_data.tc_bw_max;
  5741. veb->is_abs_credits = bw_data.absolute_credits_enable;
  5742. tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
  5743. (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
  5744. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  5745. veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
  5746. veb->bw_tc_limit_credits[i] =
  5747. le16_to_cpu(bw_data.tc_bw_limits[i]);
  5748. veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
  5749. }
  5750. out:
  5751. return ret;
  5752. }
  5753. /**
  5754. * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
  5755. * @pf: board private structure
  5756. *
  5757. * On error: returns error code (negative)
  5758. * On success: returns vsi index in PF (positive)
  5759. **/
  5760. static int i40e_veb_mem_alloc(struct i40e_pf *pf)
  5761. {
  5762. int ret = -ENOENT;
  5763. struct i40e_veb *veb;
  5764. int i;
  5765. /* Need to protect the allocation of switch elements at the PF level */
  5766. mutex_lock(&pf->switch_mutex);
  5767. /* VEB list may be fragmented if VEB creation/destruction has
  5768. * been happening. We can afford to do a quick scan to look
  5769. * for any free slots in the list.
  5770. *
  5771. * find next empty veb slot, looping back around if necessary
  5772. */
  5773. i = 0;
  5774. while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
  5775. i++;
  5776. if (i >= I40E_MAX_VEB) {
  5777. ret = -ENOMEM;
  5778. goto err_alloc_veb; /* out of VEB slots! */
  5779. }
  5780. veb = kzalloc(sizeof(*veb), GFP_KERNEL);
  5781. if (!veb) {
  5782. ret = -ENOMEM;
  5783. goto err_alloc_veb;
  5784. }
  5785. veb->pf = pf;
  5786. veb->idx = i;
  5787. veb->enabled_tc = 1;
  5788. pf->veb[i] = veb;
  5789. ret = i;
  5790. err_alloc_veb:
  5791. mutex_unlock(&pf->switch_mutex);
  5792. return ret;
  5793. }
  5794. /**
  5795. * i40e_switch_branch_release - Delete a branch of the switch tree
  5796. * @branch: where to start deleting
  5797. *
  5798. * This uses recursion to find the tips of the branch to be
  5799. * removed, deleting until we get back to and can delete this VEB.
  5800. **/
  5801. static void i40e_switch_branch_release(struct i40e_veb *branch)
  5802. {
  5803. struct i40e_pf *pf = branch->pf;
  5804. u16 branch_seid = branch->seid;
  5805. u16 veb_idx = branch->idx;
  5806. int i;
  5807. /* release any VEBs on this VEB - RECURSION */
  5808. for (i = 0; i < I40E_MAX_VEB; i++) {
  5809. if (!pf->veb[i])
  5810. continue;
  5811. if (pf->veb[i]->uplink_seid == branch->seid)
  5812. i40e_switch_branch_release(pf->veb[i]);
  5813. }
  5814. /* Release the VSIs on this VEB, but not the owner VSI.
  5815. *
  5816. * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
  5817. * the VEB itself, so don't use (*branch) after this loop.
  5818. */
  5819. for (i = 0; i < pf->hw.func_caps.num_vsis; i++) {
  5820. if (!pf->vsi[i])
  5821. continue;
  5822. if (pf->vsi[i]->uplink_seid == branch_seid &&
  5823. (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
  5824. i40e_vsi_release(pf->vsi[i]);
  5825. }
  5826. }
  5827. /* There's one corner case where the VEB might not have been
  5828. * removed, so double check it here and remove it if needed.
  5829. * This case happens if the veb was created from the debugfs
  5830. * commands and no VSIs were added to it.
  5831. */
  5832. if (pf->veb[veb_idx])
  5833. i40e_veb_release(pf->veb[veb_idx]);
  5834. }
  5835. /**
  5836. * i40e_veb_clear - remove veb struct
  5837. * @veb: the veb to remove
  5838. **/
  5839. static void i40e_veb_clear(struct i40e_veb *veb)
  5840. {
  5841. if (!veb)
  5842. return;
  5843. if (veb->pf) {
  5844. struct i40e_pf *pf = veb->pf;
  5845. mutex_lock(&pf->switch_mutex);
  5846. if (pf->veb[veb->idx] == veb)
  5847. pf->veb[veb->idx] = NULL;
  5848. mutex_unlock(&pf->switch_mutex);
  5849. }
  5850. kfree(veb);
  5851. }
  5852. /**
  5853. * i40e_veb_release - Delete a VEB and free its resources
  5854. * @veb: the VEB being removed
  5855. **/
  5856. void i40e_veb_release(struct i40e_veb *veb)
  5857. {
  5858. struct i40e_vsi *vsi = NULL;
  5859. struct i40e_pf *pf;
  5860. int i, n = 0;
  5861. pf = veb->pf;
  5862. /* find the remaining VSI and check for extras */
  5863. for (i = 0; i < pf->hw.func_caps.num_vsis; i++) {
  5864. if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
  5865. n++;
  5866. vsi = pf->vsi[i];
  5867. }
  5868. }
  5869. if (n != 1) {
  5870. dev_info(&pf->pdev->dev,
  5871. "can't remove VEB %d with %d VSIs left\n",
  5872. veb->seid, n);
  5873. return;
  5874. }
  5875. /* move the remaining VSI to uplink veb */
  5876. vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
  5877. if (veb->uplink_seid) {
  5878. vsi->uplink_seid = veb->uplink_seid;
  5879. if (veb->uplink_seid == pf->mac_seid)
  5880. vsi->veb_idx = I40E_NO_VEB;
  5881. else
  5882. vsi->veb_idx = veb->veb_idx;
  5883. } else {
  5884. /* floating VEB */
  5885. vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
  5886. vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
  5887. }
  5888. i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
  5889. i40e_veb_clear(veb);
  5890. return;
  5891. }
  5892. /**
  5893. * i40e_add_veb - create the VEB in the switch
  5894. * @veb: the VEB to be instantiated
  5895. * @vsi: the controlling VSI
  5896. **/
  5897. static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
  5898. {
  5899. bool is_default = (vsi->idx == vsi->back->lan_vsi);
  5900. bool is_cloud = false;
  5901. int ret;
  5902. /* get a VEB from the hardware */
  5903. ret = i40e_aq_add_veb(&veb->pf->hw, veb->uplink_seid, vsi->seid,
  5904. veb->enabled_tc, is_default,
  5905. is_cloud, &veb->seid, NULL);
  5906. if (ret) {
  5907. dev_info(&veb->pf->pdev->dev,
  5908. "couldn't add VEB, err %d, aq_err %d\n",
  5909. ret, veb->pf->hw.aq.asq_last_status);
  5910. return -EPERM;
  5911. }
  5912. /* get statistics counter */
  5913. ret = i40e_aq_get_veb_parameters(&veb->pf->hw, veb->seid, NULL, NULL,
  5914. &veb->stats_idx, NULL, NULL, NULL);
  5915. if (ret) {
  5916. dev_info(&veb->pf->pdev->dev,
  5917. "couldn't get VEB statistics idx, err %d, aq_err %d\n",
  5918. ret, veb->pf->hw.aq.asq_last_status);
  5919. return -EPERM;
  5920. }
  5921. ret = i40e_veb_get_bw_info(veb);
  5922. if (ret) {
  5923. dev_info(&veb->pf->pdev->dev,
  5924. "couldn't get VEB bw info, err %d, aq_err %d\n",
  5925. ret, veb->pf->hw.aq.asq_last_status);
  5926. i40e_aq_delete_element(&veb->pf->hw, veb->seid, NULL);
  5927. return -ENOENT;
  5928. }
  5929. vsi->uplink_seid = veb->seid;
  5930. vsi->veb_idx = veb->idx;
  5931. vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
  5932. return 0;
  5933. }
  5934. /**
  5935. * i40e_veb_setup - Set up a VEB
  5936. * @pf: board private structure
  5937. * @flags: VEB setup flags
  5938. * @uplink_seid: the switch element to link to
  5939. * @vsi_seid: the initial VSI seid
  5940. * @enabled_tc: Enabled TC bit-map
  5941. *
  5942. * This allocates the sw VEB structure and links it into the switch
  5943. * It is possible and legal for this to be a duplicate of an already
  5944. * existing VEB. It is also possible for both uplink and vsi seids
  5945. * to be zero, in order to create a floating VEB.
  5946. *
  5947. * Returns pointer to the successfully allocated VEB sw struct on
  5948. * success, otherwise returns NULL on failure.
  5949. **/
  5950. struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
  5951. u16 uplink_seid, u16 vsi_seid,
  5952. u8 enabled_tc)
  5953. {
  5954. struct i40e_veb *veb, *uplink_veb = NULL;
  5955. int vsi_idx, veb_idx;
  5956. int ret;
  5957. /* if one seid is 0, the other must be 0 to create a floating relay */
  5958. if ((uplink_seid == 0 || vsi_seid == 0) &&
  5959. (uplink_seid + vsi_seid != 0)) {
  5960. dev_info(&pf->pdev->dev,
  5961. "one, not both seid's are 0: uplink=%d vsi=%d\n",
  5962. uplink_seid, vsi_seid);
  5963. return NULL;
  5964. }
  5965. /* make sure there is such a vsi and uplink */
  5966. for (vsi_idx = 0; vsi_idx < pf->hw.func_caps.num_vsis; vsi_idx++)
  5967. if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
  5968. break;
  5969. if (vsi_idx >= pf->hw.func_caps.num_vsis && vsi_seid != 0) {
  5970. dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
  5971. vsi_seid);
  5972. return NULL;
  5973. }
  5974. if (uplink_seid && uplink_seid != pf->mac_seid) {
  5975. for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
  5976. if (pf->veb[veb_idx] &&
  5977. pf->veb[veb_idx]->seid == uplink_seid) {
  5978. uplink_veb = pf->veb[veb_idx];
  5979. break;
  5980. }
  5981. }
  5982. if (!uplink_veb) {
  5983. dev_info(&pf->pdev->dev,
  5984. "uplink seid %d not found\n", uplink_seid);
  5985. return NULL;
  5986. }
  5987. }
  5988. /* get veb sw struct */
  5989. veb_idx = i40e_veb_mem_alloc(pf);
  5990. if (veb_idx < 0)
  5991. goto err_alloc;
  5992. veb = pf->veb[veb_idx];
  5993. veb->flags = flags;
  5994. veb->uplink_seid = uplink_seid;
  5995. veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
  5996. veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
  5997. /* create the VEB in the switch */
  5998. ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
  5999. if (ret)
  6000. goto err_veb;
  6001. return veb;
  6002. err_veb:
  6003. i40e_veb_clear(veb);
  6004. err_alloc:
  6005. return NULL;
  6006. }
  6007. /**
  6008. * i40e_setup_pf_switch_element - set pf vars based on switch type
  6009. * @pf: board private structure
  6010. * @ele: element we are building info from
  6011. * @num_reported: total number of elements
  6012. * @printconfig: should we print the contents
  6013. *
  6014. * helper function to assist in extracting a few useful SEID values.
  6015. **/
  6016. static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
  6017. struct i40e_aqc_switch_config_element_resp *ele,
  6018. u16 num_reported, bool printconfig)
  6019. {
  6020. u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
  6021. u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
  6022. u8 element_type = ele->element_type;
  6023. u16 seid = le16_to_cpu(ele->seid);
  6024. if (printconfig)
  6025. dev_info(&pf->pdev->dev,
  6026. "type=%d seid=%d uplink=%d downlink=%d\n",
  6027. element_type, seid, uplink_seid, downlink_seid);
  6028. switch (element_type) {
  6029. case I40E_SWITCH_ELEMENT_TYPE_MAC:
  6030. pf->mac_seid = seid;
  6031. break;
  6032. case I40E_SWITCH_ELEMENT_TYPE_VEB:
  6033. /* Main VEB? */
  6034. if (uplink_seid != pf->mac_seid)
  6035. break;
  6036. if (pf->lan_veb == I40E_NO_VEB) {
  6037. int v;
  6038. /* find existing or else empty VEB */
  6039. for (v = 0; v < I40E_MAX_VEB; v++) {
  6040. if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
  6041. pf->lan_veb = v;
  6042. break;
  6043. }
  6044. }
  6045. if (pf->lan_veb == I40E_NO_VEB) {
  6046. v = i40e_veb_mem_alloc(pf);
  6047. if (v < 0)
  6048. break;
  6049. pf->lan_veb = v;
  6050. }
  6051. }
  6052. pf->veb[pf->lan_veb]->seid = seid;
  6053. pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
  6054. pf->veb[pf->lan_veb]->pf = pf;
  6055. pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
  6056. break;
  6057. case I40E_SWITCH_ELEMENT_TYPE_VSI:
  6058. if (num_reported != 1)
  6059. break;
  6060. /* This is immediately after a reset so we can assume this is
  6061. * the PF's VSI
  6062. */
  6063. pf->mac_seid = uplink_seid;
  6064. pf->pf_seid = downlink_seid;
  6065. pf->main_vsi_seid = seid;
  6066. if (printconfig)
  6067. dev_info(&pf->pdev->dev,
  6068. "pf_seid=%d main_vsi_seid=%d\n",
  6069. pf->pf_seid, pf->main_vsi_seid);
  6070. break;
  6071. case I40E_SWITCH_ELEMENT_TYPE_PF:
  6072. case I40E_SWITCH_ELEMENT_TYPE_VF:
  6073. case I40E_SWITCH_ELEMENT_TYPE_EMP:
  6074. case I40E_SWITCH_ELEMENT_TYPE_BMC:
  6075. case I40E_SWITCH_ELEMENT_TYPE_PE:
  6076. case I40E_SWITCH_ELEMENT_TYPE_PA:
  6077. /* ignore these for now */
  6078. break;
  6079. default:
  6080. dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
  6081. element_type, seid);
  6082. break;
  6083. }
  6084. }
  6085. /**
  6086. * i40e_fetch_switch_configuration - Get switch config from firmware
  6087. * @pf: board private structure
  6088. * @printconfig: should we print the contents
  6089. *
  6090. * Get the current switch configuration from the device and
  6091. * extract a few useful SEID values.
  6092. **/
  6093. int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
  6094. {
  6095. struct i40e_aqc_get_switch_config_resp *sw_config;
  6096. u16 next_seid = 0;
  6097. int ret = 0;
  6098. u8 *aq_buf;
  6099. int i;
  6100. aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
  6101. if (!aq_buf)
  6102. return -ENOMEM;
  6103. sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
  6104. do {
  6105. u16 num_reported, num_total;
  6106. ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
  6107. I40E_AQ_LARGE_BUF,
  6108. &next_seid, NULL);
  6109. if (ret) {
  6110. dev_info(&pf->pdev->dev,
  6111. "get switch config failed %d aq_err=%x\n",
  6112. ret, pf->hw.aq.asq_last_status);
  6113. kfree(aq_buf);
  6114. return -ENOENT;
  6115. }
  6116. num_reported = le16_to_cpu(sw_config->header.num_reported);
  6117. num_total = le16_to_cpu(sw_config->header.num_total);
  6118. if (printconfig)
  6119. dev_info(&pf->pdev->dev,
  6120. "header: %d reported %d total\n",
  6121. num_reported, num_total);
  6122. if (num_reported) {
  6123. int sz = sizeof(*sw_config) * num_reported;
  6124. kfree(pf->sw_config);
  6125. pf->sw_config = kzalloc(sz, GFP_KERNEL);
  6126. if (pf->sw_config)
  6127. memcpy(pf->sw_config, sw_config, sz);
  6128. }
  6129. for (i = 0; i < num_reported; i++) {
  6130. struct i40e_aqc_switch_config_element_resp *ele =
  6131. &sw_config->element[i];
  6132. i40e_setup_pf_switch_element(pf, ele, num_reported,
  6133. printconfig);
  6134. }
  6135. } while (next_seid != 0);
  6136. kfree(aq_buf);
  6137. return ret;
  6138. }
  6139. /**
  6140. * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
  6141. * @pf: board private structure
  6142. * @reinit: if the Main VSI needs to re-initialized.
  6143. *
  6144. * Returns 0 on success, negative value on failure
  6145. **/
  6146. static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit)
  6147. {
  6148. u32 rxfc = 0, txfc = 0, rxfc_reg;
  6149. int ret;
  6150. /* find out what's out there already */
  6151. ret = i40e_fetch_switch_configuration(pf, false);
  6152. if (ret) {
  6153. dev_info(&pf->pdev->dev,
  6154. "couldn't fetch switch config, err %d, aq_err %d\n",
  6155. ret, pf->hw.aq.asq_last_status);
  6156. return ret;
  6157. }
  6158. i40e_pf_reset_stats(pf);
  6159. /* fdir VSI must happen first to be sure it gets queue 0, but only
  6160. * if there is enough room for the fdir VSI
  6161. */
  6162. if (pf->num_lan_qps > 1)
  6163. i40e_fdir_setup(pf);
  6164. /* first time setup */
  6165. if (pf->lan_vsi == I40E_NO_VSI || reinit) {
  6166. struct i40e_vsi *vsi = NULL;
  6167. u16 uplink_seid;
  6168. /* Set up the PF VSI associated with the PF's main VSI
  6169. * that is already in the HW switch
  6170. */
  6171. if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
  6172. uplink_seid = pf->veb[pf->lan_veb]->seid;
  6173. else
  6174. uplink_seid = pf->mac_seid;
  6175. if (pf->lan_vsi == I40E_NO_VSI)
  6176. vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
  6177. else if (reinit)
  6178. vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
  6179. if (!vsi) {
  6180. dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
  6181. i40e_fdir_teardown(pf);
  6182. return -EAGAIN;
  6183. }
  6184. /* accommodate kcompat by copying the main VSI queue count
  6185. * into the pf, since this newer code pushes the pf queue
  6186. * info down a level into a VSI
  6187. */
  6188. pf->num_rx_queues = vsi->num_queue_pairs;
  6189. pf->num_tx_queues = vsi->num_queue_pairs;
  6190. } else {
  6191. /* force a reset of TC and queue layout configurations */
  6192. u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
  6193. pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
  6194. pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
  6195. i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
  6196. }
  6197. i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
  6198. /* Setup static PF queue filter control settings */
  6199. ret = i40e_setup_pf_filter_control(pf);
  6200. if (ret) {
  6201. dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
  6202. ret);
  6203. /* Failure here should not stop continuing other steps */
  6204. }
  6205. /* enable RSS in the HW, even for only one queue, as the stack can use
  6206. * the hash
  6207. */
  6208. if ((pf->flags & I40E_FLAG_RSS_ENABLED))
  6209. i40e_config_rss(pf);
  6210. /* fill in link information and enable LSE reporting */
  6211. i40e_aq_get_link_info(&pf->hw, true, NULL, NULL);
  6212. i40e_link_event(pf);
  6213. /* Initialize user-specific link properties */
  6214. pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
  6215. I40E_AQ_AN_COMPLETED) ? true : false);
  6216. /* requested_mode is set in probe or by ethtool */
  6217. if (!pf->fc_autoneg_status)
  6218. goto no_autoneg;
  6219. if ((pf->hw.phy.link_info.an_info & I40E_AQ_LINK_PAUSE_TX) &&
  6220. (pf->hw.phy.link_info.an_info & I40E_AQ_LINK_PAUSE_RX))
  6221. pf->hw.fc.current_mode = I40E_FC_FULL;
  6222. else if (pf->hw.phy.link_info.an_info & I40E_AQ_LINK_PAUSE_TX)
  6223. pf->hw.fc.current_mode = I40E_FC_TX_PAUSE;
  6224. else if (pf->hw.phy.link_info.an_info & I40E_AQ_LINK_PAUSE_RX)
  6225. pf->hw.fc.current_mode = I40E_FC_RX_PAUSE;
  6226. else
  6227. pf->hw.fc.current_mode = I40E_FC_NONE;
  6228. /* sync the flow control settings with the auto-neg values */
  6229. switch (pf->hw.fc.current_mode) {
  6230. case I40E_FC_FULL:
  6231. txfc = 1;
  6232. rxfc = 1;
  6233. break;
  6234. case I40E_FC_TX_PAUSE:
  6235. txfc = 1;
  6236. rxfc = 0;
  6237. break;
  6238. case I40E_FC_RX_PAUSE:
  6239. txfc = 0;
  6240. rxfc = 1;
  6241. break;
  6242. case I40E_FC_NONE:
  6243. case I40E_FC_DEFAULT:
  6244. txfc = 0;
  6245. rxfc = 0;
  6246. break;
  6247. case I40E_FC_PFC:
  6248. /* TBD */
  6249. break;
  6250. /* no default case, we have to handle all possibilities here */
  6251. }
  6252. wr32(&pf->hw, I40E_PRTDCB_FCCFG, txfc << I40E_PRTDCB_FCCFG_TFCE_SHIFT);
  6253. rxfc_reg = rd32(&pf->hw, I40E_PRTDCB_MFLCN) &
  6254. ~I40E_PRTDCB_MFLCN_RFCE_MASK;
  6255. rxfc_reg |= (rxfc << I40E_PRTDCB_MFLCN_RFCE_SHIFT);
  6256. wr32(&pf->hw, I40E_PRTDCB_MFLCN, rxfc_reg);
  6257. goto fc_complete;
  6258. no_autoneg:
  6259. /* disable L2 flow control, user can turn it on if they wish */
  6260. wr32(&pf->hw, I40E_PRTDCB_FCCFG, 0);
  6261. wr32(&pf->hw, I40E_PRTDCB_MFLCN, rd32(&pf->hw, I40E_PRTDCB_MFLCN) &
  6262. ~I40E_PRTDCB_MFLCN_RFCE_MASK);
  6263. fc_complete:
  6264. return ret;
  6265. }
  6266. /**
  6267. * i40e_set_rss_size - helper to set rss_size
  6268. * @pf: board private structure
  6269. * @queues_left: how many queues
  6270. */
  6271. static u16 i40e_set_rss_size(struct i40e_pf *pf, int queues_left)
  6272. {
  6273. int num_tc0;
  6274. num_tc0 = min_t(int, queues_left, pf->rss_size_max);
  6275. num_tc0 = min_t(int, num_tc0, num_online_cpus());
  6276. num_tc0 = rounddown_pow_of_two(num_tc0);
  6277. return num_tc0;
  6278. }
  6279. /**
  6280. * i40e_determine_queue_usage - Work out queue distribution
  6281. * @pf: board private structure
  6282. **/
  6283. static void i40e_determine_queue_usage(struct i40e_pf *pf)
  6284. {
  6285. int accum_tc_size;
  6286. int queues_left;
  6287. pf->num_lan_qps = 0;
  6288. pf->num_tc_qps = rounddown_pow_of_two(pf->num_tc_qps);
  6289. accum_tc_size = (I40E_MAX_TRAFFIC_CLASS - 1) * pf->num_tc_qps;
  6290. /* Find the max queues to be put into basic use. We'll always be
  6291. * using TC0, whether or not DCB is running, and TC0 will get the
  6292. * big RSS set.
  6293. */
  6294. queues_left = pf->hw.func_caps.num_tx_qp;
  6295. if (!((pf->flags & I40E_FLAG_MSIX_ENABLED) &&
  6296. (pf->flags & I40E_FLAG_MQ_ENABLED)) ||
  6297. !(pf->flags & (I40E_FLAG_RSS_ENABLED |
  6298. I40E_FLAG_FDIR_ENABLED | I40E_FLAG_DCB_ENABLED)) ||
  6299. (queues_left == 1)) {
  6300. /* one qp for PF, no queues for anything else */
  6301. queues_left = 0;
  6302. pf->rss_size = pf->num_lan_qps = 1;
  6303. /* make sure all the fancies are disabled */
  6304. pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
  6305. I40E_FLAG_MQ_ENABLED |
  6306. I40E_FLAG_FDIR_ENABLED |
  6307. I40E_FLAG_FDIR_ATR_ENABLED |
  6308. I40E_FLAG_DCB_ENABLED |
  6309. I40E_FLAG_SRIOV_ENABLED |
  6310. I40E_FLAG_VMDQ_ENABLED);
  6311. } else if (pf->flags & I40E_FLAG_RSS_ENABLED &&
  6312. !(pf->flags & I40E_FLAG_FDIR_ENABLED) &&
  6313. !(pf->flags & I40E_FLAG_DCB_ENABLED)) {
  6314. pf->rss_size = i40e_set_rss_size(pf, queues_left);
  6315. queues_left -= pf->rss_size;
  6316. pf->num_lan_qps = pf->rss_size_max;
  6317. } else if (pf->flags & I40E_FLAG_RSS_ENABLED &&
  6318. !(pf->flags & I40E_FLAG_FDIR_ENABLED) &&
  6319. (pf->flags & I40E_FLAG_DCB_ENABLED)) {
  6320. /* save num_tc_qps queues for TCs 1 thru 7 and the rest
  6321. * are set up for RSS in TC0
  6322. */
  6323. queues_left -= accum_tc_size;
  6324. pf->rss_size = i40e_set_rss_size(pf, queues_left);
  6325. queues_left -= pf->rss_size;
  6326. if (queues_left < 0) {
  6327. dev_info(&pf->pdev->dev, "not enough queues for DCB\n");
  6328. return;
  6329. }
  6330. pf->num_lan_qps = pf->rss_size_max + accum_tc_size;
  6331. } else if (pf->flags & I40E_FLAG_RSS_ENABLED &&
  6332. (pf->flags & I40E_FLAG_FDIR_ENABLED) &&
  6333. !(pf->flags & I40E_FLAG_DCB_ENABLED)) {
  6334. queues_left -= 1; /* save 1 queue for FD */
  6335. pf->rss_size = i40e_set_rss_size(pf, queues_left);
  6336. queues_left -= pf->rss_size;
  6337. if (queues_left < 0) {
  6338. dev_info(&pf->pdev->dev, "not enough queues for Flow Director\n");
  6339. return;
  6340. }
  6341. pf->num_lan_qps = pf->rss_size_max;
  6342. } else if (pf->flags & I40E_FLAG_RSS_ENABLED &&
  6343. (pf->flags & I40E_FLAG_FDIR_ENABLED) &&
  6344. (pf->flags & I40E_FLAG_DCB_ENABLED)) {
  6345. /* save 1 queue for TCs 1 thru 7,
  6346. * 1 queue for flow director,
  6347. * and the rest are set up for RSS in TC0
  6348. */
  6349. queues_left -= 1;
  6350. queues_left -= accum_tc_size;
  6351. pf->rss_size = i40e_set_rss_size(pf, queues_left);
  6352. queues_left -= pf->rss_size;
  6353. if (queues_left < 0) {
  6354. dev_info(&pf->pdev->dev, "not enough queues for DCB and Flow Director\n");
  6355. return;
  6356. }
  6357. pf->num_lan_qps = pf->rss_size_max + accum_tc_size;
  6358. } else {
  6359. dev_info(&pf->pdev->dev,
  6360. "Invalid configuration, flags=0x%08llx\n", pf->flags);
  6361. return;
  6362. }
  6363. if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
  6364. pf->num_vf_qps && pf->num_req_vfs && queues_left) {
  6365. pf->num_req_vfs = min_t(int, pf->num_req_vfs, (queues_left /
  6366. pf->num_vf_qps));
  6367. queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
  6368. }
  6369. if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
  6370. pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
  6371. pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
  6372. (queues_left / pf->num_vmdq_qps));
  6373. queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
  6374. }
  6375. pf->queues_left = queues_left;
  6376. return;
  6377. }
  6378. /**
  6379. * i40e_setup_pf_filter_control - Setup PF static filter control
  6380. * @pf: PF to be setup
  6381. *
  6382. * i40e_setup_pf_filter_control sets up a pf's initial filter control
  6383. * settings. If PE/FCoE are enabled then it will also set the per PF
  6384. * based filter sizes required for them. It also enables Flow director,
  6385. * ethertype and macvlan type filter settings for the pf.
  6386. *
  6387. * Returns 0 on success, negative on failure
  6388. **/
  6389. static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
  6390. {
  6391. struct i40e_filter_control_settings *settings = &pf->filter_settings;
  6392. settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
  6393. /* Flow Director is enabled */
  6394. if (pf->flags & (I40E_FLAG_FDIR_ENABLED | I40E_FLAG_FDIR_ATR_ENABLED))
  6395. settings->enable_fdir = true;
  6396. /* Ethtype and MACVLAN filters enabled for PF */
  6397. settings->enable_ethtype = true;
  6398. settings->enable_macvlan = true;
  6399. if (i40e_set_filter_control(&pf->hw, settings))
  6400. return -ENOENT;
  6401. return 0;
  6402. }
  6403. /**
  6404. * i40e_probe - Device initialization routine
  6405. * @pdev: PCI device information struct
  6406. * @ent: entry in i40e_pci_tbl
  6407. *
  6408. * i40e_probe initializes a pf identified by a pci_dev structure.
  6409. * The OS initialization, configuring of the pf private structure,
  6410. * and a hardware reset occur.
  6411. *
  6412. * Returns 0 on success, negative on failure
  6413. **/
  6414. static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  6415. {
  6416. struct i40e_driver_version dv;
  6417. struct i40e_pf *pf;
  6418. struct i40e_hw *hw;
  6419. static u16 pfs_found;
  6420. int err = 0;
  6421. u32 len;
  6422. err = pci_enable_device_mem(pdev);
  6423. if (err)
  6424. return err;
  6425. /* set up for high or low dma */
  6426. if (!dma_set_mask(&pdev->dev, DMA_BIT_MASK(64))) {
  6427. /* coherent mask for the same size will always succeed if
  6428. * dma_set_mask does
  6429. */
  6430. dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(64));
  6431. } else if (!dma_set_mask(&pdev->dev, DMA_BIT_MASK(32))) {
  6432. dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
  6433. } else {
  6434. dev_err(&pdev->dev, "DMA configuration failed: %d\n", err);
  6435. err = -EIO;
  6436. goto err_dma;
  6437. }
  6438. /* set up pci connections */
  6439. err = pci_request_selected_regions(pdev, pci_select_bars(pdev,
  6440. IORESOURCE_MEM), i40e_driver_name);
  6441. if (err) {
  6442. dev_info(&pdev->dev,
  6443. "pci_request_selected_regions failed %d\n", err);
  6444. goto err_pci_reg;
  6445. }
  6446. pci_enable_pcie_error_reporting(pdev);
  6447. pci_set_master(pdev);
  6448. /* Now that we have a PCI connection, we need to do the
  6449. * low level device setup. This is primarily setting up
  6450. * the Admin Queue structures and then querying for the
  6451. * device's current profile information.
  6452. */
  6453. pf = kzalloc(sizeof(*pf), GFP_KERNEL);
  6454. if (!pf) {
  6455. err = -ENOMEM;
  6456. goto err_pf_alloc;
  6457. }
  6458. pf->next_vsi = 0;
  6459. pf->pdev = pdev;
  6460. set_bit(__I40E_DOWN, &pf->state);
  6461. hw = &pf->hw;
  6462. hw->back = pf;
  6463. hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
  6464. pci_resource_len(pdev, 0));
  6465. if (!hw->hw_addr) {
  6466. err = -EIO;
  6467. dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
  6468. (unsigned int)pci_resource_start(pdev, 0),
  6469. (unsigned int)pci_resource_len(pdev, 0), err);
  6470. goto err_ioremap;
  6471. }
  6472. hw->vendor_id = pdev->vendor;
  6473. hw->device_id = pdev->device;
  6474. pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
  6475. hw->subsystem_vendor_id = pdev->subsystem_vendor;
  6476. hw->subsystem_device_id = pdev->subsystem_device;
  6477. hw->bus.device = PCI_SLOT(pdev->devfn);
  6478. hw->bus.func = PCI_FUNC(pdev->devfn);
  6479. pf->instance = pfs_found;
  6480. /* do a special CORER for clearing PXE mode once at init */
  6481. if (hw->revision_id == 0 &&
  6482. (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
  6483. wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
  6484. i40e_flush(hw);
  6485. msleep(200);
  6486. pf->corer_count++;
  6487. i40e_clear_pxe_mode(hw);
  6488. }
  6489. /* Reset here to make sure all is clean and to define PF 'n' */
  6490. err = i40e_pf_reset(hw);
  6491. if (err) {
  6492. dev_info(&pdev->dev, "Initial pf_reset failed: %d\n", err);
  6493. goto err_pf_reset;
  6494. }
  6495. pf->pfr_count++;
  6496. hw->aq.num_arq_entries = I40E_AQ_LEN;
  6497. hw->aq.num_asq_entries = I40E_AQ_LEN;
  6498. hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
  6499. hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
  6500. pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
  6501. snprintf(pf->misc_int_name, sizeof(pf->misc_int_name) - 1,
  6502. "%s-pf%d:misc",
  6503. dev_driver_string(&pf->pdev->dev), pf->hw.pf_id);
  6504. err = i40e_init_shared_code(hw);
  6505. if (err) {
  6506. dev_info(&pdev->dev, "init_shared_code failed: %d\n", err);
  6507. goto err_pf_reset;
  6508. }
  6509. /* set up a default setting for link flow control */
  6510. pf->hw.fc.requested_mode = I40E_FC_NONE;
  6511. err = i40e_init_adminq(hw);
  6512. dev_info(&pdev->dev, "%s\n", i40e_fw_version_str(hw));
  6513. if (((hw->nvm.version & I40E_NVM_VERSION_HI_MASK)
  6514. >> I40E_NVM_VERSION_HI_SHIFT) != I40E_CURRENT_NVM_VERSION_HI) {
  6515. dev_info(&pdev->dev,
  6516. "warning: NVM version not supported, supported version: %02x.%02x\n",
  6517. I40E_CURRENT_NVM_VERSION_HI,
  6518. I40E_CURRENT_NVM_VERSION_LO);
  6519. }
  6520. if (err) {
  6521. dev_info(&pdev->dev,
  6522. "init_adminq failed: %d expecting API %02x.%02x\n",
  6523. err,
  6524. I40E_FW_API_VERSION_MAJOR, I40E_FW_API_VERSION_MINOR);
  6525. goto err_pf_reset;
  6526. }
  6527. err = i40e_get_capabilities(pf);
  6528. if (err)
  6529. goto err_adminq_setup;
  6530. err = i40e_sw_init(pf);
  6531. if (err) {
  6532. dev_info(&pdev->dev, "sw_init failed: %d\n", err);
  6533. goto err_sw_init;
  6534. }
  6535. err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
  6536. hw->func_caps.num_rx_qp,
  6537. pf->fcoe_hmc_cntx_num, pf->fcoe_hmc_filt_num);
  6538. if (err) {
  6539. dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
  6540. goto err_init_lan_hmc;
  6541. }
  6542. err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
  6543. if (err) {
  6544. dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
  6545. err = -ENOENT;
  6546. goto err_configure_lan_hmc;
  6547. }
  6548. i40e_get_mac_addr(hw, hw->mac.addr);
  6549. if (i40e_validate_mac_addr(hw->mac.addr)) {
  6550. dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
  6551. err = -EIO;
  6552. goto err_mac_addr;
  6553. }
  6554. dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
  6555. memcpy(hw->mac.perm_addr, hw->mac.addr, ETH_ALEN);
  6556. pci_set_drvdata(pdev, pf);
  6557. pci_save_state(pdev);
  6558. /* set up periodic task facility */
  6559. setup_timer(&pf->service_timer, i40e_service_timer, (unsigned long)pf);
  6560. pf->service_timer_period = HZ;
  6561. INIT_WORK(&pf->service_task, i40e_service_task);
  6562. clear_bit(__I40E_SERVICE_SCHED, &pf->state);
  6563. pf->flags |= I40E_FLAG_NEED_LINK_UPDATE;
  6564. pf->link_check_timeout = jiffies;
  6565. /* set up the main switch operations */
  6566. i40e_determine_queue_usage(pf);
  6567. i40e_init_interrupt_scheme(pf);
  6568. /* Set up the *vsi struct based on the number of VSIs in the HW,
  6569. * and set up our local tracking of the MAIN PF vsi.
  6570. */
  6571. len = sizeof(struct i40e_vsi *) * pf->hw.func_caps.num_vsis;
  6572. pf->vsi = kzalloc(len, GFP_KERNEL);
  6573. if (!pf->vsi) {
  6574. err = -ENOMEM;
  6575. goto err_switch_setup;
  6576. }
  6577. err = i40e_setup_pf_switch(pf, false);
  6578. if (err) {
  6579. dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
  6580. goto err_vsis;
  6581. }
  6582. /* The main driver is (mostly) up and happy. We need to set this state
  6583. * before setting up the misc vector or we get a race and the vector
  6584. * ends up disabled forever.
  6585. */
  6586. clear_bit(__I40E_DOWN, &pf->state);
  6587. /* In case of MSIX we are going to setup the misc vector right here
  6588. * to handle admin queue events etc. In case of legacy and MSI
  6589. * the misc functionality and queue processing is combined in
  6590. * the same vector and that gets setup at open.
  6591. */
  6592. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  6593. err = i40e_setup_misc_vector(pf);
  6594. if (err) {
  6595. dev_info(&pdev->dev,
  6596. "setup of misc vector failed: %d\n", err);
  6597. goto err_vsis;
  6598. }
  6599. }
  6600. /* prep for VF support */
  6601. if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
  6602. (pf->flags & I40E_FLAG_MSIX_ENABLED)) {
  6603. u32 val;
  6604. /* disable link interrupts for VFs */
  6605. val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
  6606. val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
  6607. wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
  6608. i40e_flush(hw);
  6609. }
  6610. pfs_found++;
  6611. i40e_dbg_pf_init(pf);
  6612. /* tell the firmware that we're starting */
  6613. dv.major_version = DRV_VERSION_MAJOR;
  6614. dv.minor_version = DRV_VERSION_MINOR;
  6615. dv.build_version = DRV_VERSION_BUILD;
  6616. dv.subbuild_version = 0;
  6617. i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
  6618. /* since everything's happy, start the service_task timer */
  6619. mod_timer(&pf->service_timer,
  6620. round_jiffies(jiffies + pf->service_timer_period));
  6621. return 0;
  6622. /* Unwind what we've done if something failed in the setup */
  6623. err_vsis:
  6624. set_bit(__I40E_DOWN, &pf->state);
  6625. err_switch_setup:
  6626. i40e_clear_interrupt_scheme(pf);
  6627. kfree(pf->vsi);
  6628. del_timer_sync(&pf->service_timer);
  6629. err_mac_addr:
  6630. err_configure_lan_hmc:
  6631. (void)i40e_shutdown_lan_hmc(hw);
  6632. err_init_lan_hmc:
  6633. kfree(pf->qp_pile);
  6634. kfree(pf->irq_pile);
  6635. err_sw_init:
  6636. err_adminq_setup:
  6637. (void)i40e_shutdown_adminq(hw);
  6638. err_pf_reset:
  6639. iounmap(hw->hw_addr);
  6640. err_ioremap:
  6641. kfree(pf);
  6642. err_pf_alloc:
  6643. pci_disable_pcie_error_reporting(pdev);
  6644. pci_release_selected_regions(pdev,
  6645. pci_select_bars(pdev, IORESOURCE_MEM));
  6646. err_pci_reg:
  6647. err_dma:
  6648. pci_disable_device(pdev);
  6649. return err;
  6650. }
  6651. /**
  6652. * i40e_remove - Device removal routine
  6653. * @pdev: PCI device information struct
  6654. *
  6655. * i40e_remove is called by the PCI subsystem to alert the driver
  6656. * that is should release a PCI device. This could be caused by a
  6657. * Hot-Plug event, or because the driver is going to be removed from
  6658. * memory.
  6659. **/
  6660. static void i40e_remove(struct pci_dev *pdev)
  6661. {
  6662. struct i40e_pf *pf = pci_get_drvdata(pdev);
  6663. i40e_status ret_code;
  6664. u32 reg;
  6665. int i;
  6666. i40e_dbg_pf_exit(pf);
  6667. if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
  6668. i40e_free_vfs(pf);
  6669. pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
  6670. }
  6671. /* no more scheduling of any task */
  6672. set_bit(__I40E_DOWN, &pf->state);
  6673. del_timer_sync(&pf->service_timer);
  6674. cancel_work_sync(&pf->service_task);
  6675. i40e_fdir_teardown(pf);
  6676. /* If there is a switch structure or any orphans, remove them.
  6677. * This will leave only the PF's VSI remaining.
  6678. */
  6679. for (i = 0; i < I40E_MAX_VEB; i++) {
  6680. if (!pf->veb[i])
  6681. continue;
  6682. if (pf->veb[i]->uplink_seid == pf->mac_seid ||
  6683. pf->veb[i]->uplink_seid == 0)
  6684. i40e_switch_branch_release(pf->veb[i]);
  6685. }
  6686. /* Now we can shutdown the PF's VSI, just before we kill
  6687. * adminq and hmc.
  6688. */
  6689. if (pf->vsi[pf->lan_vsi])
  6690. i40e_vsi_release(pf->vsi[pf->lan_vsi]);
  6691. i40e_stop_misc_vector(pf);
  6692. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  6693. synchronize_irq(pf->msix_entries[0].vector);
  6694. free_irq(pf->msix_entries[0].vector, pf);
  6695. }
  6696. /* shutdown and destroy the HMC */
  6697. ret_code = i40e_shutdown_lan_hmc(&pf->hw);
  6698. if (ret_code)
  6699. dev_warn(&pdev->dev,
  6700. "Failed to destroy the HMC resources: %d\n", ret_code);
  6701. /* shutdown the adminq */
  6702. i40e_aq_queue_shutdown(&pf->hw, true);
  6703. ret_code = i40e_shutdown_adminq(&pf->hw);
  6704. if (ret_code)
  6705. dev_warn(&pdev->dev,
  6706. "Failed to destroy the Admin Queue resources: %d\n",
  6707. ret_code);
  6708. /* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
  6709. i40e_clear_interrupt_scheme(pf);
  6710. for (i = 0; i < pf->hw.func_caps.num_vsis; i++) {
  6711. if (pf->vsi[i]) {
  6712. i40e_vsi_clear_rings(pf->vsi[i]);
  6713. i40e_vsi_clear(pf->vsi[i]);
  6714. pf->vsi[i] = NULL;
  6715. }
  6716. }
  6717. for (i = 0; i < I40E_MAX_VEB; i++) {
  6718. kfree(pf->veb[i]);
  6719. pf->veb[i] = NULL;
  6720. }
  6721. kfree(pf->qp_pile);
  6722. kfree(pf->irq_pile);
  6723. kfree(pf->sw_config);
  6724. kfree(pf->vsi);
  6725. /* force a PF reset to clean anything leftover */
  6726. reg = rd32(&pf->hw, I40E_PFGEN_CTRL);
  6727. wr32(&pf->hw, I40E_PFGEN_CTRL, (reg | I40E_PFGEN_CTRL_PFSWR_MASK));
  6728. i40e_flush(&pf->hw);
  6729. iounmap(pf->hw.hw_addr);
  6730. kfree(pf);
  6731. pci_release_selected_regions(pdev,
  6732. pci_select_bars(pdev, IORESOURCE_MEM));
  6733. pci_disable_pcie_error_reporting(pdev);
  6734. pci_disable_device(pdev);
  6735. }
  6736. /**
  6737. * i40e_pci_error_detected - warning that something funky happened in PCI land
  6738. * @pdev: PCI device information struct
  6739. *
  6740. * Called to warn that something happened and the error handling steps
  6741. * are in progress. Allows the driver to quiesce things, be ready for
  6742. * remediation.
  6743. **/
  6744. static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
  6745. enum pci_channel_state error)
  6746. {
  6747. struct i40e_pf *pf = pci_get_drvdata(pdev);
  6748. dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
  6749. /* shutdown all operations */
  6750. if (!test_bit(__I40E_SUSPENDED, &pf->state)) {
  6751. rtnl_lock();
  6752. i40e_prep_for_reset(pf);
  6753. rtnl_unlock();
  6754. }
  6755. /* Request a slot reset */
  6756. return PCI_ERS_RESULT_NEED_RESET;
  6757. }
  6758. /**
  6759. * i40e_pci_error_slot_reset - a PCI slot reset just happened
  6760. * @pdev: PCI device information struct
  6761. *
  6762. * Called to find if the driver can work with the device now that
  6763. * the pci slot has been reset. If a basic connection seems good
  6764. * (registers are readable and have sane content) then return a
  6765. * happy little PCI_ERS_RESULT_xxx.
  6766. **/
  6767. static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
  6768. {
  6769. struct i40e_pf *pf = pci_get_drvdata(pdev);
  6770. pci_ers_result_t result;
  6771. int err;
  6772. u32 reg;
  6773. dev_info(&pdev->dev, "%s\n", __func__);
  6774. if (pci_enable_device_mem(pdev)) {
  6775. dev_info(&pdev->dev,
  6776. "Cannot re-enable PCI device after reset.\n");
  6777. result = PCI_ERS_RESULT_DISCONNECT;
  6778. } else {
  6779. pci_set_master(pdev);
  6780. pci_restore_state(pdev);
  6781. pci_save_state(pdev);
  6782. pci_wake_from_d3(pdev, false);
  6783. reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
  6784. if (reg == 0)
  6785. result = PCI_ERS_RESULT_RECOVERED;
  6786. else
  6787. result = PCI_ERS_RESULT_DISCONNECT;
  6788. }
  6789. err = pci_cleanup_aer_uncorrect_error_status(pdev);
  6790. if (err) {
  6791. dev_info(&pdev->dev,
  6792. "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
  6793. err);
  6794. /* non-fatal, continue */
  6795. }
  6796. return result;
  6797. }
  6798. /**
  6799. * i40e_pci_error_resume - restart operations after PCI error recovery
  6800. * @pdev: PCI device information struct
  6801. *
  6802. * Called to allow the driver to bring things back up after PCI error
  6803. * and/or reset recovery has finished.
  6804. **/
  6805. static void i40e_pci_error_resume(struct pci_dev *pdev)
  6806. {
  6807. struct i40e_pf *pf = pci_get_drvdata(pdev);
  6808. dev_info(&pdev->dev, "%s\n", __func__);
  6809. if (test_bit(__I40E_SUSPENDED, &pf->state))
  6810. return;
  6811. rtnl_lock();
  6812. i40e_handle_reset_warning(pf);
  6813. rtnl_lock();
  6814. }
  6815. /**
  6816. * i40e_shutdown - PCI callback for shutting down
  6817. * @pdev: PCI device information struct
  6818. **/
  6819. static void i40e_shutdown(struct pci_dev *pdev)
  6820. {
  6821. struct i40e_pf *pf = pci_get_drvdata(pdev);
  6822. set_bit(__I40E_SUSPENDED, &pf->state);
  6823. set_bit(__I40E_DOWN, &pf->state);
  6824. rtnl_lock();
  6825. i40e_prep_for_reset(pf);
  6826. rtnl_unlock();
  6827. if (system_state == SYSTEM_POWER_OFF) {
  6828. pci_wake_from_d3(pdev, false); /* No WoL support yet */
  6829. pci_set_power_state(pdev, PCI_D3hot);
  6830. }
  6831. }
  6832. #ifdef CONFIG_PM
  6833. /**
  6834. * i40e_suspend - PCI callback for moving to D3
  6835. * @pdev: PCI device information struct
  6836. **/
  6837. static int i40e_suspend(struct pci_dev *pdev, pm_message_t state)
  6838. {
  6839. struct i40e_pf *pf = pci_get_drvdata(pdev);
  6840. set_bit(__I40E_SUSPENDED, &pf->state);
  6841. set_bit(__I40E_DOWN, &pf->state);
  6842. rtnl_lock();
  6843. i40e_prep_for_reset(pf);
  6844. rtnl_unlock();
  6845. pci_wake_from_d3(pdev, false); /* No WoL support yet */
  6846. pci_set_power_state(pdev, PCI_D3hot);
  6847. return 0;
  6848. }
  6849. /**
  6850. * i40e_resume - PCI callback for waking up from D3
  6851. * @pdev: PCI device information struct
  6852. **/
  6853. static int i40e_resume(struct pci_dev *pdev)
  6854. {
  6855. struct i40e_pf *pf = pci_get_drvdata(pdev);
  6856. u32 err;
  6857. pci_set_power_state(pdev, PCI_D0);
  6858. pci_restore_state(pdev);
  6859. /* pci_restore_state() clears dev->state_saves, so
  6860. * call pci_save_state() again to restore it.
  6861. */
  6862. pci_save_state(pdev);
  6863. err = pci_enable_device_mem(pdev);
  6864. if (err) {
  6865. dev_err(&pdev->dev,
  6866. "%s: Cannot enable PCI device from suspend\n",
  6867. __func__);
  6868. return err;
  6869. }
  6870. pci_set_master(pdev);
  6871. /* no wakeup events while running */
  6872. pci_wake_from_d3(pdev, false);
  6873. /* handling the reset will rebuild the device state */
  6874. if (test_and_clear_bit(__I40E_SUSPENDED, &pf->state)) {
  6875. clear_bit(__I40E_DOWN, &pf->state);
  6876. rtnl_lock();
  6877. i40e_reset_and_rebuild(pf, false);
  6878. rtnl_unlock();
  6879. }
  6880. return 0;
  6881. }
  6882. #endif
  6883. static const struct pci_error_handlers i40e_err_handler = {
  6884. .error_detected = i40e_pci_error_detected,
  6885. .slot_reset = i40e_pci_error_slot_reset,
  6886. .resume = i40e_pci_error_resume,
  6887. };
  6888. static struct pci_driver i40e_driver = {
  6889. .name = i40e_driver_name,
  6890. .id_table = i40e_pci_tbl,
  6891. .probe = i40e_probe,
  6892. .remove = i40e_remove,
  6893. #ifdef CONFIG_PM
  6894. .suspend = i40e_suspend,
  6895. .resume = i40e_resume,
  6896. #endif
  6897. .shutdown = i40e_shutdown,
  6898. .err_handler = &i40e_err_handler,
  6899. .sriov_configure = i40e_pci_sriov_configure,
  6900. };
  6901. /**
  6902. * i40e_init_module - Driver registration routine
  6903. *
  6904. * i40e_init_module is the first routine called when the driver is
  6905. * loaded. All it does is register with the PCI subsystem.
  6906. **/
  6907. static int __init i40e_init_module(void)
  6908. {
  6909. pr_info("%s: %s - version %s\n", i40e_driver_name,
  6910. i40e_driver_string, i40e_driver_version_str);
  6911. pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
  6912. i40e_dbg_init();
  6913. return pci_register_driver(&i40e_driver);
  6914. }
  6915. module_init(i40e_init_module);
  6916. /**
  6917. * i40e_exit_module - Driver exit cleanup routine
  6918. *
  6919. * i40e_exit_module is called just before the driver is removed
  6920. * from memory.
  6921. **/
  6922. static void __exit i40e_exit_module(void)
  6923. {
  6924. pci_unregister_driver(&i40e_driver);
  6925. i40e_dbg_exit();
  6926. }
  6927. module_exit(i40e_exit_module);