i40e_main.c 198 KB

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