i40e_main.c 400 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright(c) 2013 - 2018 Intel Corporation. */
  3. #include <linux/etherdevice.h>
  4. #include <linux/of_net.h>
  5. #include <linux/pci.h>
  6. #include <linux/bpf.h>
  7. /* Local includes */
  8. #include "i40e.h"
  9. #include "i40e_diag.h"
  10. #include <net/udp_tunnel.h>
  11. /* All i40e tracepoints are defined by the include below, which
  12. * must be included exactly once across the whole kernel with
  13. * CREATE_TRACE_POINTS defined
  14. */
  15. #define CREATE_TRACE_POINTS
  16. #include "i40e_trace.h"
  17. const char i40e_driver_name[] = "i40e";
  18. static const char i40e_driver_string[] =
  19. "Intel(R) Ethernet Connection XL710 Network Driver";
  20. #define DRV_KERN "-k"
  21. #define DRV_VERSION_MAJOR 2
  22. #define DRV_VERSION_MINOR 3
  23. #define DRV_VERSION_BUILD 2
  24. #define DRV_VERSION __stringify(DRV_VERSION_MAJOR) "." \
  25. __stringify(DRV_VERSION_MINOR) "." \
  26. __stringify(DRV_VERSION_BUILD) DRV_KERN
  27. const char i40e_driver_version_str[] = DRV_VERSION;
  28. static const char i40e_copyright[] = "Copyright (c) 2013 - 2014 Intel Corporation.";
  29. /* a bit of forward declarations */
  30. static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi);
  31. static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired);
  32. static int i40e_add_vsi(struct i40e_vsi *vsi);
  33. static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi);
  34. static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit);
  35. static int i40e_setup_misc_vector(struct i40e_pf *pf);
  36. static void i40e_determine_queue_usage(struct i40e_pf *pf);
  37. static int i40e_setup_pf_filter_control(struct i40e_pf *pf);
  38. static void i40e_prep_for_reset(struct i40e_pf *pf, bool lock_acquired);
  39. static int i40e_reset(struct i40e_pf *pf);
  40. static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired);
  41. static void i40e_fdir_sb_setup(struct i40e_pf *pf);
  42. static int i40e_veb_get_bw_info(struct i40e_veb *veb);
  43. static int i40e_get_capabilities(struct i40e_pf *pf,
  44. enum i40e_admin_queue_opc list_type);
  45. /* i40e_pci_tbl - PCI Device ID Table
  46. *
  47. * Last entry must be all 0s
  48. *
  49. * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
  50. * Class, Class Mask, private data (not used) }
  51. */
  52. static const struct pci_device_id i40e_pci_tbl[] = {
  53. {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_XL710), 0},
  54. {PCI_VDEVICE(INTEL, I40E_DEV_ID_QEMU), 0},
  55. {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_B), 0},
  56. {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_C), 0},
  57. {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_A), 0},
  58. {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_B), 0},
  59. {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_C), 0},
  60. {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T), 0},
  61. {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T4), 0},
  62. {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_X722), 0},
  63. {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_X722), 0},
  64. {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_X722), 0},
  65. {PCI_VDEVICE(INTEL, I40E_DEV_ID_1G_BASE_T_X722), 0},
  66. {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_X722), 0},
  67. {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_I_X722), 0},
  68. {PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2), 0},
  69. {PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2_A), 0},
  70. {PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_B), 0},
  71. {PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_SFP28), 0},
  72. /* required last entry */
  73. {0, }
  74. };
  75. MODULE_DEVICE_TABLE(pci, i40e_pci_tbl);
  76. #define I40E_MAX_VF_COUNT 128
  77. static int debug = -1;
  78. module_param(debug, uint, 0);
  79. MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all), Debug mask (0x8XXXXXXX)");
  80. MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
  81. MODULE_DESCRIPTION("Intel(R) Ethernet Connection XL710 Network Driver");
  82. MODULE_LICENSE("GPL");
  83. MODULE_VERSION(DRV_VERSION);
  84. static struct workqueue_struct *i40e_wq;
  85. /**
  86. * i40e_allocate_dma_mem_d - OS specific memory alloc for shared code
  87. * @hw: pointer to the HW structure
  88. * @mem: ptr to mem struct to fill out
  89. * @size: size of memory requested
  90. * @alignment: what to align the allocation to
  91. **/
  92. int i40e_allocate_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem,
  93. u64 size, u32 alignment)
  94. {
  95. struct i40e_pf *pf = (struct i40e_pf *)hw->back;
  96. mem->size = ALIGN(size, alignment);
  97. mem->va = dma_zalloc_coherent(&pf->pdev->dev, mem->size,
  98. &mem->pa, GFP_KERNEL);
  99. if (!mem->va)
  100. return -ENOMEM;
  101. return 0;
  102. }
  103. /**
  104. * i40e_free_dma_mem_d - OS specific memory free for shared code
  105. * @hw: pointer to the HW structure
  106. * @mem: ptr to mem struct to free
  107. **/
  108. int i40e_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
  109. {
  110. struct i40e_pf *pf = (struct i40e_pf *)hw->back;
  111. dma_free_coherent(&pf->pdev->dev, mem->size, mem->va, mem->pa);
  112. mem->va = NULL;
  113. mem->pa = 0;
  114. mem->size = 0;
  115. return 0;
  116. }
  117. /**
  118. * i40e_allocate_virt_mem_d - OS specific memory alloc for shared code
  119. * @hw: pointer to the HW structure
  120. * @mem: ptr to mem struct to fill out
  121. * @size: size of memory requested
  122. **/
  123. int i40e_allocate_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem,
  124. u32 size)
  125. {
  126. mem->size = size;
  127. mem->va = kzalloc(size, GFP_KERNEL);
  128. if (!mem->va)
  129. return -ENOMEM;
  130. return 0;
  131. }
  132. /**
  133. * i40e_free_virt_mem_d - OS specific memory free for shared code
  134. * @hw: pointer to the HW structure
  135. * @mem: ptr to mem struct to free
  136. **/
  137. int i40e_free_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem)
  138. {
  139. /* it's ok to kfree a NULL pointer */
  140. kfree(mem->va);
  141. mem->va = NULL;
  142. mem->size = 0;
  143. return 0;
  144. }
  145. /**
  146. * i40e_get_lump - find a lump of free generic resource
  147. * @pf: board private structure
  148. * @pile: the pile of resource to search
  149. * @needed: the number of items needed
  150. * @id: an owner id to stick on the items assigned
  151. *
  152. * Returns the base item index of the lump, or negative for error
  153. *
  154. * The search_hint trick and lack of advanced fit-finding only work
  155. * because we're highly likely to have all the same size lump requests.
  156. * Linear search time and any fragmentation should be minimal.
  157. **/
  158. static int i40e_get_lump(struct i40e_pf *pf, struct i40e_lump_tracking *pile,
  159. u16 needed, u16 id)
  160. {
  161. int ret = -ENOMEM;
  162. int i, j;
  163. if (!pile || needed == 0 || id >= I40E_PILE_VALID_BIT) {
  164. dev_info(&pf->pdev->dev,
  165. "param err: pile=%s needed=%d id=0x%04x\n",
  166. pile ? "<valid>" : "<null>", needed, id);
  167. return -EINVAL;
  168. }
  169. /* start the linear search with an imperfect hint */
  170. i = pile->search_hint;
  171. while (i < pile->num_entries) {
  172. /* skip already allocated entries */
  173. if (pile->list[i] & I40E_PILE_VALID_BIT) {
  174. i++;
  175. continue;
  176. }
  177. /* do we have enough in this lump? */
  178. for (j = 0; (j < needed) && ((i+j) < pile->num_entries); j++) {
  179. if (pile->list[i+j] & I40E_PILE_VALID_BIT)
  180. break;
  181. }
  182. if (j == needed) {
  183. /* there was enough, so assign it to the requestor */
  184. for (j = 0; j < needed; j++)
  185. pile->list[i+j] = id | I40E_PILE_VALID_BIT;
  186. ret = i;
  187. pile->search_hint = i + j;
  188. break;
  189. }
  190. /* not enough, so skip over it and continue looking */
  191. i += j;
  192. }
  193. return ret;
  194. }
  195. /**
  196. * i40e_put_lump - return a lump of generic resource
  197. * @pile: the pile of resource to search
  198. * @index: the base item index
  199. * @id: the owner id of the items assigned
  200. *
  201. * Returns the count of items in the lump
  202. **/
  203. static int i40e_put_lump(struct i40e_lump_tracking *pile, u16 index, u16 id)
  204. {
  205. int valid_id = (id | I40E_PILE_VALID_BIT);
  206. int count = 0;
  207. int i;
  208. if (!pile || index >= pile->num_entries)
  209. return -EINVAL;
  210. for (i = index;
  211. i < pile->num_entries && pile->list[i] == valid_id;
  212. i++) {
  213. pile->list[i] = 0;
  214. count++;
  215. }
  216. if (count && index < pile->search_hint)
  217. pile->search_hint = index;
  218. return count;
  219. }
  220. /**
  221. * i40e_find_vsi_from_id - searches for the vsi with the given id
  222. * @pf: the pf structure to search for the vsi
  223. * @id: id of the vsi it is searching for
  224. **/
  225. struct i40e_vsi *i40e_find_vsi_from_id(struct i40e_pf *pf, u16 id)
  226. {
  227. int i;
  228. for (i = 0; i < pf->num_alloc_vsi; i++)
  229. if (pf->vsi[i] && (pf->vsi[i]->id == id))
  230. return pf->vsi[i];
  231. return NULL;
  232. }
  233. /**
  234. * i40e_service_event_schedule - Schedule the service task to wake up
  235. * @pf: board private structure
  236. *
  237. * If not already scheduled, this puts the task into the work queue
  238. **/
  239. void i40e_service_event_schedule(struct i40e_pf *pf)
  240. {
  241. if (!test_bit(__I40E_DOWN, pf->state) &&
  242. !test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
  243. queue_work(i40e_wq, &pf->service_task);
  244. }
  245. /**
  246. * i40e_tx_timeout - Respond to a Tx Hang
  247. * @netdev: network interface device structure
  248. *
  249. * If any port has noticed a Tx timeout, it is likely that the whole
  250. * device is munged, not just the one netdev port, so go for the full
  251. * reset.
  252. **/
  253. static void i40e_tx_timeout(struct net_device *netdev)
  254. {
  255. struct i40e_netdev_priv *np = netdev_priv(netdev);
  256. struct i40e_vsi *vsi = np->vsi;
  257. struct i40e_pf *pf = vsi->back;
  258. struct i40e_ring *tx_ring = NULL;
  259. unsigned int i, hung_queue = 0;
  260. u32 head, val;
  261. pf->tx_timeout_count++;
  262. /* find the stopped queue the same way the stack does */
  263. for (i = 0; i < netdev->num_tx_queues; i++) {
  264. struct netdev_queue *q;
  265. unsigned long trans_start;
  266. q = netdev_get_tx_queue(netdev, i);
  267. trans_start = q->trans_start;
  268. if (netif_xmit_stopped(q) &&
  269. time_after(jiffies,
  270. (trans_start + netdev->watchdog_timeo))) {
  271. hung_queue = i;
  272. break;
  273. }
  274. }
  275. if (i == netdev->num_tx_queues) {
  276. netdev_info(netdev, "tx_timeout: no netdev hung queue found\n");
  277. } else {
  278. /* now that we have an index, find the tx_ring struct */
  279. for (i = 0; i < vsi->num_queue_pairs; i++) {
  280. if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
  281. if (hung_queue ==
  282. vsi->tx_rings[i]->queue_index) {
  283. tx_ring = vsi->tx_rings[i];
  284. break;
  285. }
  286. }
  287. }
  288. }
  289. if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ*20)))
  290. pf->tx_timeout_recovery_level = 1; /* reset after some time */
  291. else if (time_before(jiffies,
  292. (pf->tx_timeout_last_recovery + netdev->watchdog_timeo)))
  293. return; /* don't do any new action before the next timeout */
  294. if (tx_ring) {
  295. head = i40e_get_head(tx_ring);
  296. /* Read interrupt register */
  297. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  298. val = rd32(&pf->hw,
  299. I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
  300. tx_ring->vsi->base_vector - 1));
  301. else
  302. val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
  303. netdev_info(netdev, "tx_timeout: VSI_seid: %d, Q %d, NTC: 0x%x, HWB: 0x%x, NTU: 0x%x, TAIL: 0x%x, INT: 0x%x\n",
  304. vsi->seid, hung_queue, tx_ring->next_to_clean,
  305. head, tx_ring->next_to_use,
  306. readl(tx_ring->tail), val);
  307. }
  308. pf->tx_timeout_last_recovery = jiffies;
  309. netdev_info(netdev, "tx_timeout recovery level %d, hung_queue %d\n",
  310. pf->tx_timeout_recovery_level, hung_queue);
  311. switch (pf->tx_timeout_recovery_level) {
  312. case 1:
  313. set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
  314. break;
  315. case 2:
  316. set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
  317. break;
  318. case 3:
  319. set_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
  320. break;
  321. default:
  322. netdev_err(netdev, "tx_timeout recovery unsuccessful\n");
  323. break;
  324. }
  325. i40e_service_event_schedule(pf);
  326. pf->tx_timeout_recovery_level++;
  327. }
  328. /**
  329. * i40e_get_vsi_stats_struct - Get System Network Statistics
  330. * @vsi: the VSI we care about
  331. *
  332. * Returns the address of the device statistics structure.
  333. * The statistics are actually updated from the service task.
  334. **/
  335. struct rtnl_link_stats64 *i40e_get_vsi_stats_struct(struct i40e_vsi *vsi)
  336. {
  337. return &vsi->net_stats;
  338. }
  339. /**
  340. * i40e_get_netdev_stats_struct_tx - populate stats from a Tx ring
  341. * @ring: Tx ring to get statistics from
  342. * @stats: statistics entry to be updated
  343. **/
  344. static void i40e_get_netdev_stats_struct_tx(struct i40e_ring *ring,
  345. struct rtnl_link_stats64 *stats)
  346. {
  347. u64 bytes, packets;
  348. unsigned int start;
  349. do {
  350. start = u64_stats_fetch_begin_irq(&ring->syncp);
  351. packets = ring->stats.packets;
  352. bytes = ring->stats.bytes;
  353. } while (u64_stats_fetch_retry_irq(&ring->syncp, start));
  354. stats->tx_packets += packets;
  355. stats->tx_bytes += bytes;
  356. }
  357. /**
  358. * i40e_get_netdev_stats_struct - Get statistics for netdev interface
  359. * @netdev: network interface device structure
  360. * @stats: data structure to store statistics
  361. *
  362. * Returns the address of the device statistics structure.
  363. * The statistics are actually updated from the service task.
  364. **/
  365. static void i40e_get_netdev_stats_struct(struct net_device *netdev,
  366. struct rtnl_link_stats64 *stats)
  367. {
  368. struct i40e_netdev_priv *np = netdev_priv(netdev);
  369. struct i40e_ring *tx_ring, *rx_ring;
  370. struct i40e_vsi *vsi = np->vsi;
  371. struct rtnl_link_stats64 *vsi_stats = i40e_get_vsi_stats_struct(vsi);
  372. int i;
  373. if (test_bit(__I40E_VSI_DOWN, vsi->state))
  374. return;
  375. if (!vsi->tx_rings)
  376. return;
  377. rcu_read_lock();
  378. for (i = 0; i < vsi->num_queue_pairs; i++) {
  379. u64 bytes, packets;
  380. unsigned int start;
  381. tx_ring = READ_ONCE(vsi->tx_rings[i]);
  382. if (!tx_ring)
  383. continue;
  384. i40e_get_netdev_stats_struct_tx(tx_ring, stats);
  385. rx_ring = &tx_ring[1];
  386. do {
  387. start = u64_stats_fetch_begin_irq(&rx_ring->syncp);
  388. packets = rx_ring->stats.packets;
  389. bytes = rx_ring->stats.bytes;
  390. } while (u64_stats_fetch_retry_irq(&rx_ring->syncp, start));
  391. stats->rx_packets += packets;
  392. stats->rx_bytes += bytes;
  393. if (i40e_enabled_xdp_vsi(vsi))
  394. i40e_get_netdev_stats_struct_tx(&rx_ring[1], stats);
  395. }
  396. rcu_read_unlock();
  397. /* following stats updated by i40e_watchdog_subtask() */
  398. stats->multicast = vsi_stats->multicast;
  399. stats->tx_errors = vsi_stats->tx_errors;
  400. stats->tx_dropped = vsi_stats->tx_dropped;
  401. stats->rx_errors = vsi_stats->rx_errors;
  402. stats->rx_dropped = vsi_stats->rx_dropped;
  403. stats->rx_crc_errors = vsi_stats->rx_crc_errors;
  404. stats->rx_length_errors = vsi_stats->rx_length_errors;
  405. }
  406. /**
  407. * i40e_vsi_reset_stats - Resets all stats of the given vsi
  408. * @vsi: the VSI to have its stats reset
  409. **/
  410. void i40e_vsi_reset_stats(struct i40e_vsi *vsi)
  411. {
  412. struct rtnl_link_stats64 *ns;
  413. int i;
  414. if (!vsi)
  415. return;
  416. ns = i40e_get_vsi_stats_struct(vsi);
  417. memset(ns, 0, sizeof(*ns));
  418. memset(&vsi->net_stats_offsets, 0, sizeof(vsi->net_stats_offsets));
  419. memset(&vsi->eth_stats, 0, sizeof(vsi->eth_stats));
  420. memset(&vsi->eth_stats_offsets, 0, sizeof(vsi->eth_stats_offsets));
  421. if (vsi->rx_rings && vsi->rx_rings[0]) {
  422. for (i = 0; i < vsi->num_queue_pairs; i++) {
  423. memset(&vsi->rx_rings[i]->stats, 0,
  424. sizeof(vsi->rx_rings[i]->stats));
  425. memset(&vsi->rx_rings[i]->rx_stats, 0,
  426. sizeof(vsi->rx_rings[i]->rx_stats));
  427. memset(&vsi->tx_rings[i]->stats, 0,
  428. sizeof(vsi->tx_rings[i]->stats));
  429. memset(&vsi->tx_rings[i]->tx_stats, 0,
  430. sizeof(vsi->tx_rings[i]->tx_stats));
  431. }
  432. }
  433. vsi->stat_offsets_loaded = false;
  434. }
  435. /**
  436. * i40e_pf_reset_stats - Reset all of the stats for the given PF
  437. * @pf: the PF to be reset
  438. **/
  439. void i40e_pf_reset_stats(struct i40e_pf *pf)
  440. {
  441. int i;
  442. memset(&pf->stats, 0, sizeof(pf->stats));
  443. memset(&pf->stats_offsets, 0, sizeof(pf->stats_offsets));
  444. pf->stat_offsets_loaded = false;
  445. for (i = 0; i < I40E_MAX_VEB; i++) {
  446. if (pf->veb[i]) {
  447. memset(&pf->veb[i]->stats, 0,
  448. sizeof(pf->veb[i]->stats));
  449. memset(&pf->veb[i]->stats_offsets, 0,
  450. sizeof(pf->veb[i]->stats_offsets));
  451. pf->veb[i]->stat_offsets_loaded = false;
  452. }
  453. }
  454. pf->hw_csum_rx_error = 0;
  455. }
  456. /**
  457. * i40e_stat_update48 - read and update a 48 bit stat from the chip
  458. * @hw: ptr to the hardware info
  459. * @hireg: the high 32 bit reg to read
  460. * @loreg: the low 32 bit reg to read
  461. * @offset_loaded: has the initial offset been loaded yet
  462. * @offset: ptr to current offset value
  463. * @stat: ptr to the stat
  464. *
  465. * Since the device stats are not reset at PFReset, they likely will not
  466. * be zeroed when the driver starts. We'll save the first values read
  467. * and use them as offsets to be subtracted from the raw values in order
  468. * to report stats that count from zero. In the process, we also manage
  469. * the potential roll-over.
  470. **/
  471. static void i40e_stat_update48(struct i40e_hw *hw, u32 hireg, u32 loreg,
  472. bool offset_loaded, u64 *offset, u64 *stat)
  473. {
  474. u64 new_data;
  475. if (hw->device_id == I40E_DEV_ID_QEMU) {
  476. new_data = rd32(hw, loreg);
  477. new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
  478. } else {
  479. new_data = rd64(hw, loreg);
  480. }
  481. if (!offset_loaded)
  482. *offset = new_data;
  483. if (likely(new_data >= *offset))
  484. *stat = new_data - *offset;
  485. else
  486. *stat = (new_data + BIT_ULL(48)) - *offset;
  487. *stat &= 0xFFFFFFFFFFFFULL;
  488. }
  489. /**
  490. * i40e_stat_update32 - read and update a 32 bit stat from the chip
  491. * @hw: ptr to the hardware info
  492. * @reg: the hw reg to read
  493. * @offset_loaded: has the initial offset been loaded yet
  494. * @offset: ptr to current offset value
  495. * @stat: ptr to the stat
  496. **/
  497. static void i40e_stat_update32(struct i40e_hw *hw, u32 reg,
  498. bool offset_loaded, u64 *offset, u64 *stat)
  499. {
  500. u32 new_data;
  501. new_data = rd32(hw, reg);
  502. if (!offset_loaded)
  503. *offset = new_data;
  504. if (likely(new_data >= *offset))
  505. *stat = (u32)(new_data - *offset);
  506. else
  507. *stat = (u32)((new_data + BIT_ULL(32)) - *offset);
  508. }
  509. /**
  510. * i40e_stat_update_and_clear32 - read and clear hw reg, update a 32 bit stat
  511. * @hw: ptr to the hardware info
  512. * @reg: the hw reg to read and clear
  513. * @stat: ptr to the stat
  514. **/
  515. static void i40e_stat_update_and_clear32(struct i40e_hw *hw, u32 reg, u64 *stat)
  516. {
  517. u32 new_data = rd32(hw, reg);
  518. wr32(hw, reg, 1); /* must write a nonzero value to clear register */
  519. *stat += new_data;
  520. }
  521. /**
  522. * i40e_update_eth_stats - Update VSI-specific ethernet statistics counters.
  523. * @vsi: the VSI to be updated
  524. **/
  525. void i40e_update_eth_stats(struct i40e_vsi *vsi)
  526. {
  527. int stat_idx = le16_to_cpu(vsi->info.stat_counter_idx);
  528. struct i40e_pf *pf = vsi->back;
  529. struct i40e_hw *hw = &pf->hw;
  530. struct i40e_eth_stats *oes;
  531. struct i40e_eth_stats *es; /* device's eth stats */
  532. es = &vsi->eth_stats;
  533. oes = &vsi->eth_stats_offsets;
  534. /* Gather up the stats that the hw collects */
  535. i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
  536. vsi->stat_offsets_loaded,
  537. &oes->tx_errors, &es->tx_errors);
  538. i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx),
  539. vsi->stat_offsets_loaded,
  540. &oes->rx_discards, &es->rx_discards);
  541. i40e_stat_update32(hw, I40E_GLV_RUPP(stat_idx),
  542. vsi->stat_offsets_loaded,
  543. &oes->rx_unknown_protocol, &es->rx_unknown_protocol);
  544. i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
  545. vsi->stat_offsets_loaded,
  546. &oes->tx_errors, &es->tx_errors);
  547. i40e_stat_update48(hw, I40E_GLV_GORCH(stat_idx),
  548. I40E_GLV_GORCL(stat_idx),
  549. vsi->stat_offsets_loaded,
  550. &oes->rx_bytes, &es->rx_bytes);
  551. i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
  552. I40E_GLV_UPRCL(stat_idx),
  553. vsi->stat_offsets_loaded,
  554. &oes->rx_unicast, &es->rx_unicast);
  555. i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
  556. I40E_GLV_MPRCL(stat_idx),
  557. vsi->stat_offsets_loaded,
  558. &oes->rx_multicast, &es->rx_multicast);
  559. i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
  560. I40E_GLV_BPRCL(stat_idx),
  561. vsi->stat_offsets_loaded,
  562. &oes->rx_broadcast, &es->rx_broadcast);
  563. i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
  564. I40E_GLV_GOTCL(stat_idx),
  565. vsi->stat_offsets_loaded,
  566. &oes->tx_bytes, &es->tx_bytes);
  567. i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
  568. I40E_GLV_UPTCL(stat_idx),
  569. vsi->stat_offsets_loaded,
  570. &oes->tx_unicast, &es->tx_unicast);
  571. i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
  572. I40E_GLV_MPTCL(stat_idx),
  573. vsi->stat_offsets_loaded,
  574. &oes->tx_multicast, &es->tx_multicast);
  575. i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
  576. I40E_GLV_BPTCL(stat_idx),
  577. vsi->stat_offsets_loaded,
  578. &oes->tx_broadcast, &es->tx_broadcast);
  579. vsi->stat_offsets_loaded = true;
  580. }
  581. /**
  582. * i40e_update_veb_stats - Update Switch component statistics
  583. * @veb: the VEB being updated
  584. **/
  585. static void i40e_update_veb_stats(struct i40e_veb *veb)
  586. {
  587. struct i40e_pf *pf = veb->pf;
  588. struct i40e_hw *hw = &pf->hw;
  589. struct i40e_eth_stats *oes;
  590. struct i40e_eth_stats *es; /* device's eth stats */
  591. struct i40e_veb_tc_stats *veb_oes;
  592. struct i40e_veb_tc_stats *veb_es;
  593. int i, idx = 0;
  594. idx = veb->stats_idx;
  595. es = &veb->stats;
  596. oes = &veb->stats_offsets;
  597. veb_es = &veb->tc_stats;
  598. veb_oes = &veb->tc_stats_offsets;
  599. /* Gather up the stats that the hw collects */
  600. i40e_stat_update32(hw, I40E_GLSW_TDPC(idx),
  601. veb->stat_offsets_loaded,
  602. &oes->tx_discards, &es->tx_discards);
  603. if (hw->revision_id > 0)
  604. i40e_stat_update32(hw, I40E_GLSW_RUPP(idx),
  605. veb->stat_offsets_loaded,
  606. &oes->rx_unknown_protocol,
  607. &es->rx_unknown_protocol);
  608. i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx),
  609. veb->stat_offsets_loaded,
  610. &oes->rx_bytes, &es->rx_bytes);
  611. i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx),
  612. veb->stat_offsets_loaded,
  613. &oes->rx_unicast, &es->rx_unicast);
  614. i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx),
  615. veb->stat_offsets_loaded,
  616. &oes->rx_multicast, &es->rx_multicast);
  617. i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx),
  618. veb->stat_offsets_loaded,
  619. &oes->rx_broadcast, &es->rx_broadcast);
  620. i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx),
  621. veb->stat_offsets_loaded,
  622. &oes->tx_bytes, &es->tx_bytes);
  623. i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx),
  624. veb->stat_offsets_loaded,
  625. &oes->tx_unicast, &es->tx_unicast);
  626. i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx),
  627. veb->stat_offsets_loaded,
  628. &oes->tx_multicast, &es->tx_multicast);
  629. i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx),
  630. veb->stat_offsets_loaded,
  631. &oes->tx_broadcast, &es->tx_broadcast);
  632. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  633. i40e_stat_update48(hw, I40E_GLVEBTC_RPCH(i, idx),
  634. I40E_GLVEBTC_RPCL(i, idx),
  635. veb->stat_offsets_loaded,
  636. &veb_oes->tc_rx_packets[i],
  637. &veb_es->tc_rx_packets[i]);
  638. i40e_stat_update48(hw, I40E_GLVEBTC_RBCH(i, idx),
  639. I40E_GLVEBTC_RBCL(i, idx),
  640. veb->stat_offsets_loaded,
  641. &veb_oes->tc_rx_bytes[i],
  642. &veb_es->tc_rx_bytes[i]);
  643. i40e_stat_update48(hw, I40E_GLVEBTC_TPCH(i, idx),
  644. I40E_GLVEBTC_TPCL(i, idx),
  645. veb->stat_offsets_loaded,
  646. &veb_oes->tc_tx_packets[i],
  647. &veb_es->tc_tx_packets[i]);
  648. i40e_stat_update48(hw, I40E_GLVEBTC_TBCH(i, idx),
  649. I40E_GLVEBTC_TBCL(i, idx),
  650. veb->stat_offsets_loaded,
  651. &veb_oes->tc_tx_bytes[i],
  652. &veb_es->tc_tx_bytes[i]);
  653. }
  654. veb->stat_offsets_loaded = true;
  655. }
  656. /**
  657. * i40e_update_vsi_stats - Update the vsi statistics counters.
  658. * @vsi: the VSI to be updated
  659. *
  660. * There are a few instances where we store the same stat in a
  661. * couple of different structs. This is partly because we have
  662. * the netdev stats that need to be filled out, which is slightly
  663. * different from the "eth_stats" defined by the chip and used in
  664. * VF communications. We sort it out here.
  665. **/
  666. static void i40e_update_vsi_stats(struct i40e_vsi *vsi)
  667. {
  668. struct i40e_pf *pf = vsi->back;
  669. struct rtnl_link_stats64 *ons;
  670. struct rtnl_link_stats64 *ns; /* netdev stats */
  671. struct i40e_eth_stats *oes;
  672. struct i40e_eth_stats *es; /* device's eth stats */
  673. u32 tx_restart, tx_busy;
  674. struct i40e_ring *p;
  675. u32 rx_page, rx_buf;
  676. u64 bytes, packets;
  677. unsigned int start;
  678. u64 tx_linearize;
  679. u64 tx_force_wb;
  680. u64 rx_p, rx_b;
  681. u64 tx_p, tx_b;
  682. u16 q;
  683. if (test_bit(__I40E_VSI_DOWN, vsi->state) ||
  684. test_bit(__I40E_CONFIG_BUSY, pf->state))
  685. return;
  686. ns = i40e_get_vsi_stats_struct(vsi);
  687. ons = &vsi->net_stats_offsets;
  688. es = &vsi->eth_stats;
  689. oes = &vsi->eth_stats_offsets;
  690. /* Gather up the netdev and vsi stats that the driver collects
  691. * on the fly during packet processing
  692. */
  693. rx_b = rx_p = 0;
  694. tx_b = tx_p = 0;
  695. tx_restart = tx_busy = tx_linearize = tx_force_wb = 0;
  696. rx_page = 0;
  697. rx_buf = 0;
  698. rcu_read_lock();
  699. for (q = 0; q < vsi->num_queue_pairs; q++) {
  700. /* locate Tx ring */
  701. p = READ_ONCE(vsi->tx_rings[q]);
  702. do {
  703. start = u64_stats_fetch_begin_irq(&p->syncp);
  704. packets = p->stats.packets;
  705. bytes = p->stats.bytes;
  706. } while (u64_stats_fetch_retry_irq(&p->syncp, start));
  707. tx_b += bytes;
  708. tx_p += packets;
  709. tx_restart += p->tx_stats.restart_queue;
  710. tx_busy += p->tx_stats.tx_busy;
  711. tx_linearize += p->tx_stats.tx_linearize;
  712. tx_force_wb += p->tx_stats.tx_force_wb;
  713. /* Rx queue is part of the same block as Tx queue */
  714. p = &p[1];
  715. do {
  716. start = u64_stats_fetch_begin_irq(&p->syncp);
  717. packets = p->stats.packets;
  718. bytes = p->stats.bytes;
  719. } while (u64_stats_fetch_retry_irq(&p->syncp, start));
  720. rx_b += bytes;
  721. rx_p += packets;
  722. rx_buf += p->rx_stats.alloc_buff_failed;
  723. rx_page += p->rx_stats.alloc_page_failed;
  724. }
  725. rcu_read_unlock();
  726. vsi->tx_restart = tx_restart;
  727. vsi->tx_busy = tx_busy;
  728. vsi->tx_linearize = tx_linearize;
  729. vsi->tx_force_wb = tx_force_wb;
  730. vsi->rx_page_failed = rx_page;
  731. vsi->rx_buf_failed = rx_buf;
  732. ns->rx_packets = rx_p;
  733. ns->rx_bytes = rx_b;
  734. ns->tx_packets = tx_p;
  735. ns->tx_bytes = tx_b;
  736. /* update netdev stats from eth stats */
  737. i40e_update_eth_stats(vsi);
  738. ons->tx_errors = oes->tx_errors;
  739. ns->tx_errors = es->tx_errors;
  740. ons->multicast = oes->rx_multicast;
  741. ns->multicast = es->rx_multicast;
  742. ons->rx_dropped = oes->rx_discards;
  743. ns->rx_dropped = es->rx_discards;
  744. ons->tx_dropped = oes->tx_discards;
  745. ns->tx_dropped = es->tx_discards;
  746. /* pull in a couple PF stats if this is the main vsi */
  747. if (vsi == pf->vsi[pf->lan_vsi]) {
  748. ns->rx_crc_errors = pf->stats.crc_errors;
  749. ns->rx_errors = pf->stats.crc_errors + pf->stats.illegal_bytes;
  750. ns->rx_length_errors = pf->stats.rx_length_errors;
  751. }
  752. }
  753. /**
  754. * i40e_update_pf_stats - Update the PF statistics counters.
  755. * @pf: the PF to be updated
  756. **/
  757. static void i40e_update_pf_stats(struct i40e_pf *pf)
  758. {
  759. struct i40e_hw_port_stats *osd = &pf->stats_offsets;
  760. struct i40e_hw_port_stats *nsd = &pf->stats;
  761. struct i40e_hw *hw = &pf->hw;
  762. u32 val;
  763. int i;
  764. i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
  765. I40E_GLPRT_GORCL(hw->port),
  766. pf->stat_offsets_loaded,
  767. &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
  768. i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
  769. I40E_GLPRT_GOTCL(hw->port),
  770. pf->stat_offsets_loaded,
  771. &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
  772. i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
  773. pf->stat_offsets_loaded,
  774. &osd->eth.rx_discards,
  775. &nsd->eth.rx_discards);
  776. i40e_stat_update48(hw, I40E_GLPRT_UPRCH(hw->port),
  777. I40E_GLPRT_UPRCL(hw->port),
  778. pf->stat_offsets_loaded,
  779. &osd->eth.rx_unicast,
  780. &nsd->eth.rx_unicast);
  781. i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
  782. I40E_GLPRT_MPRCL(hw->port),
  783. pf->stat_offsets_loaded,
  784. &osd->eth.rx_multicast,
  785. &nsd->eth.rx_multicast);
  786. i40e_stat_update48(hw, I40E_GLPRT_BPRCH(hw->port),
  787. I40E_GLPRT_BPRCL(hw->port),
  788. pf->stat_offsets_loaded,
  789. &osd->eth.rx_broadcast,
  790. &nsd->eth.rx_broadcast);
  791. i40e_stat_update48(hw, I40E_GLPRT_UPTCH(hw->port),
  792. I40E_GLPRT_UPTCL(hw->port),
  793. pf->stat_offsets_loaded,
  794. &osd->eth.tx_unicast,
  795. &nsd->eth.tx_unicast);
  796. i40e_stat_update48(hw, I40E_GLPRT_MPTCH(hw->port),
  797. I40E_GLPRT_MPTCL(hw->port),
  798. pf->stat_offsets_loaded,
  799. &osd->eth.tx_multicast,
  800. &nsd->eth.tx_multicast);
  801. i40e_stat_update48(hw, I40E_GLPRT_BPTCH(hw->port),
  802. I40E_GLPRT_BPTCL(hw->port),
  803. pf->stat_offsets_loaded,
  804. &osd->eth.tx_broadcast,
  805. &nsd->eth.tx_broadcast);
  806. i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
  807. pf->stat_offsets_loaded,
  808. &osd->tx_dropped_link_down,
  809. &nsd->tx_dropped_link_down);
  810. i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
  811. pf->stat_offsets_loaded,
  812. &osd->crc_errors, &nsd->crc_errors);
  813. i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
  814. pf->stat_offsets_loaded,
  815. &osd->illegal_bytes, &nsd->illegal_bytes);
  816. i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
  817. pf->stat_offsets_loaded,
  818. &osd->mac_local_faults,
  819. &nsd->mac_local_faults);
  820. i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
  821. pf->stat_offsets_loaded,
  822. &osd->mac_remote_faults,
  823. &nsd->mac_remote_faults);
  824. i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
  825. pf->stat_offsets_loaded,
  826. &osd->rx_length_errors,
  827. &nsd->rx_length_errors);
  828. i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
  829. pf->stat_offsets_loaded,
  830. &osd->link_xon_rx, &nsd->link_xon_rx);
  831. i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
  832. pf->stat_offsets_loaded,
  833. &osd->link_xon_tx, &nsd->link_xon_tx);
  834. i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
  835. pf->stat_offsets_loaded,
  836. &osd->link_xoff_rx, &nsd->link_xoff_rx);
  837. i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
  838. pf->stat_offsets_loaded,
  839. &osd->link_xoff_tx, &nsd->link_xoff_tx);
  840. for (i = 0; i < 8; i++) {
  841. i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
  842. pf->stat_offsets_loaded,
  843. &osd->priority_xoff_rx[i],
  844. &nsd->priority_xoff_rx[i]);
  845. i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
  846. pf->stat_offsets_loaded,
  847. &osd->priority_xon_rx[i],
  848. &nsd->priority_xon_rx[i]);
  849. i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
  850. pf->stat_offsets_loaded,
  851. &osd->priority_xon_tx[i],
  852. &nsd->priority_xon_tx[i]);
  853. i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
  854. pf->stat_offsets_loaded,
  855. &osd->priority_xoff_tx[i],
  856. &nsd->priority_xoff_tx[i]);
  857. i40e_stat_update32(hw,
  858. I40E_GLPRT_RXON2OFFCNT(hw->port, i),
  859. pf->stat_offsets_loaded,
  860. &osd->priority_xon_2_xoff[i],
  861. &nsd->priority_xon_2_xoff[i]);
  862. }
  863. i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
  864. I40E_GLPRT_PRC64L(hw->port),
  865. pf->stat_offsets_loaded,
  866. &osd->rx_size_64, &nsd->rx_size_64);
  867. i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
  868. I40E_GLPRT_PRC127L(hw->port),
  869. pf->stat_offsets_loaded,
  870. &osd->rx_size_127, &nsd->rx_size_127);
  871. i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
  872. I40E_GLPRT_PRC255L(hw->port),
  873. pf->stat_offsets_loaded,
  874. &osd->rx_size_255, &nsd->rx_size_255);
  875. i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
  876. I40E_GLPRT_PRC511L(hw->port),
  877. pf->stat_offsets_loaded,
  878. &osd->rx_size_511, &nsd->rx_size_511);
  879. i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
  880. I40E_GLPRT_PRC1023L(hw->port),
  881. pf->stat_offsets_loaded,
  882. &osd->rx_size_1023, &nsd->rx_size_1023);
  883. i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
  884. I40E_GLPRT_PRC1522L(hw->port),
  885. pf->stat_offsets_loaded,
  886. &osd->rx_size_1522, &nsd->rx_size_1522);
  887. i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
  888. I40E_GLPRT_PRC9522L(hw->port),
  889. pf->stat_offsets_loaded,
  890. &osd->rx_size_big, &nsd->rx_size_big);
  891. i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
  892. I40E_GLPRT_PTC64L(hw->port),
  893. pf->stat_offsets_loaded,
  894. &osd->tx_size_64, &nsd->tx_size_64);
  895. i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
  896. I40E_GLPRT_PTC127L(hw->port),
  897. pf->stat_offsets_loaded,
  898. &osd->tx_size_127, &nsd->tx_size_127);
  899. i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
  900. I40E_GLPRT_PTC255L(hw->port),
  901. pf->stat_offsets_loaded,
  902. &osd->tx_size_255, &nsd->tx_size_255);
  903. i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
  904. I40E_GLPRT_PTC511L(hw->port),
  905. pf->stat_offsets_loaded,
  906. &osd->tx_size_511, &nsd->tx_size_511);
  907. i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
  908. I40E_GLPRT_PTC1023L(hw->port),
  909. pf->stat_offsets_loaded,
  910. &osd->tx_size_1023, &nsd->tx_size_1023);
  911. i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
  912. I40E_GLPRT_PTC1522L(hw->port),
  913. pf->stat_offsets_loaded,
  914. &osd->tx_size_1522, &nsd->tx_size_1522);
  915. i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
  916. I40E_GLPRT_PTC9522L(hw->port),
  917. pf->stat_offsets_loaded,
  918. &osd->tx_size_big, &nsd->tx_size_big);
  919. i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
  920. pf->stat_offsets_loaded,
  921. &osd->rx_undersize, &nsd->rx_undersize);
  922. i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
  923. pf->stat_offsets_loaded,
  924. &osd->rx_fragments, &nsd->rx_fragments);
  925. i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
  926. pf->stat_offsets_loaded,
  927. &osd->rx_oversize, &nsd->rx_oversize);
  928. i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
  929. pf->stat_offsets_loaded,
  930. &osd->rx_jabber, &nsd->rx_jabber);
  931. /* FDIR stats */
  932. i40e_stat_update_and_clear32(hw,
  933. I40E_GLQF_PCNT(I40E_FD_ATR_STAT_IDX(hw->pf_id)),
  934. &nsd->fd_atr_match);
  935. i40e_stat_update_and_clear32(hw,
  936. I40E_GLQF_PCNT(I40E_FD_SB_STAT_IDX(hw->pf_id)),
  937. &nsd->fd_sb_match);
  938. i40e_stat_update_and_clear32(hw,
  939. I40E_GLQF_PCNT(I40E_FD_ATR_TUNNEL_STAT_IDX(hw->pf_id)),
  940. &nsd->fd_atr_tunnel_match);
  941. val = rd32(hw, I40E_PRTPM_EEE_STAT);
  942. nsd->tx_lpi_status =
  943. (val & I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_MASK) >>
  944. I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_SHIFT;
  945. nsd->rx_lpi_status =
  946. (val & I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_MASK) >>
  947. I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_SHIFT;
  948. i40e_stat_update32(hw, I40E_PRTPM_TLPIC,
  949. pf->stat_offsets_loaded,
  950. &osd->tx_lpi_count, &nsd->tx_lpi_count);
  951. i40e_stat_update32(hw, I40E_PRTPM_RLPIC,
  952. pf->stat_offsets_loaded,
  953. &osd->rx_lpi_count, &nsd->rx_lpi_count);
  954. if (pf->flags & I40E_FLAG_FD_SB_ENABLED &&
  955. !test_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
  956. nsd->fd_sb_status = true;
  957. else
  958. nsd->fd_sb_status = false;
  959. if (pf->flags & I40E_FLAG_FD_ATR_ENABLED &&
  960. !test_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
  961. nsd->fd_atr_status = true;
  962. else
  963. nsd->fd_atr_status = false;
  964. pf->stat_offsets_loaded = true;
  965. }
  966. /**
  967. * i40e_update_stats - Update the various statistics counters.
  968. * @vsi: the VSI to be updated
  969. *
  970. * Update the various stats for this VSI and its related entities.
  971. **/
  972. void i40e_update_stats(struct i40e_vsi *vsi)
  973. {
  974. struct i40e_pf *pf = vsi->back;
  975. if (vsi == pf->vsi[pf->lan_vsi])
  976. i40e_update_pf_stats(pf);
  977. i40e_update_vsi_stats(vsi);
  978. }
  979. /**
  980. * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
  981. * @vsi: the VSI to be searched
  982. * @macaddr: the MAC address
  983. * @vlan: the vlan
  984. *
  985. * Returns ptr to the filter object or NULL
  986. **/
  987. static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
  988. const u8 *macaddr, s16 vlan)
  989. {
  990. struct i40e_mac_filter *f;
  991. u64 key;
  992. if (!vsi || !macaddr)
  993. return NULL;
  994. key = i40e_addr_to_hkey(macaddr);
  995. hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
  996. if ((ether_addr_equal(macaddr, f->macaddr)) &&
  997. (vlan == f->vlan))
  998. return f;
  999. }
  1000. return NULL;
  1001. }
  1002. /**
  1003. * i40e_find_mac - Find a mac addr in the macvlan filters list
  1004. * @vsi: the VSI to be searched
  1005. * @macaddr: the MAC address we are searching for
  1006. *
  1007. * Returns the first filter with the provided MAC address or NULL if
  1008. * MAC address was not found
  1009. **/
  1010. struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, const u8 *macaddr)
  1011. {
  1012. struct i40e_mac_filter *f;
  1013. u64 key;
  1014. if (!vsi || !macaddr)
  1015. return NULL;
  1016. key = i40e_addr_to_hkey(macaddr);
  1017. hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
  1018. if ((ether_addr_equal(macaddr, f->macaddr)))
  1019. return f;
  1020. }
  1021. return NULL;
  1022. }
  1023. /**
  1024. * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
  1025. * @vsi: the VSI to be searched
  1026. *
  1027. * Returns true if VSI is in vlan mode or false otherwise
  1028. **/
  1029. bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
  1030. {
  1031. /* If we have a PVID, always operate in VLAN mode */
  1032. if (vsi->info.pvid)
  1033. return true;
  1034. /* We need to operate in VLAN mode whenever we have any filters with
  1035. * a VLAN other than I40E_VLAN_ALL. We could check the table each
  1036. * time, incurring search cost repeatedly. However, we can notice two
  1037. * things:
  1038. *
  1039. * 1) the only place where we can gain a VLAN filter is in
  1040. * i40e_add_filter.
  1041. *
  1042. * 2) the only place where filters are actually removed is in
  1043. * i40e_sync_filters_subtask.
  1044. *
  1045. * Thus, we can simply use a boolean value, has_vlan_filters which we
  1046. * will set to true when we add a VLAN filter in i40e_add_filter. Then
  1047. * we have to perform the full search after deleting filters in
  1048. * i40e_sync_filters_subtask, but we already have to search
  1049. * filters here and can perform the check at the same time. This
  1050. * results in avoiding embedding a loop for VLAN mode inside another
  1051. * loop over all the filters, and should maintain correctness as noted
  1052. * above.
  1053. */
  1054. return vsi->has_vlan_filter;
  1055. }
  1056. /**
  1057. * i40e_correct_mac_vlan_filters - Correct non-VLAN filters if necessary
  1058. * @vsi: the VSI to configure
  1059. * @tmp_add_list: list of filters ready to be added
  1060. * @tmp_del_list: list of filters ready to be deleted
  1061. * @vlan_filters: the number of active VLAN filters
  1062. *
  1063. * Update VLAN=0 and VLAN=-1 (I40E_VLAN_ANY) filters properly so that they
  1064. * behave as expected. If we have any active VLAN filters remaining or about
  1065. * to be added then we need to update non-VLAN filters to be marked as VLAN=0
  1066. * so that they only match against untagged traffic. If we no longer have any
  1067. * active VLAN filters, we need to make all non-VLAN filters marked as VLAN=-1
  1068. * so that they match against both tagged and untagged traffic. In this way,
  1069. * we ensure that we correctly receive the desired traffic. This ensures that
  1070. * when we have an active VLAN we will receive only untagged traffic and
  1071. * traffic matching active VLANs. If we have no active VLANs then we will
  1072. * operate in non-VLAN mode and receive all traffic, tagged or untagged.
  1073. *
  1074. * Finally, in a similar fashion, this function also corrects filters when
  1075. * there is an active PVID assigned to this VSI.
  1076. *
  1077. * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
  1078. *
  1079. * This function is only expected to be called from within
  1080. * i40e_sync_vsi_filters.
  1081. *
  1082. * NOTE: This function expects to be called while under the
  1083. * mac_filter_hash_lock
  1084. */
  1085. static int i40e_correct_mac_vlan_filters(struct i40e_vsi *vsi,
  1086. struct hlist_head *tmp_add_list,
  1087. struct hlist_head *tmp_del_list,
  1088. int vlan_filters)
  1089. {
  1090. s16 pvid = le16_to_cpu(vsi->info.pvid);
  1091. struct i40e_mac_filter *f, *add_head;
  1092. struct i40e_new_mac_filter *new;
  1093. struct hlist_node *h;
  1094. int bkt, new_vlan;
  1095. /* To determine if a particular filter needs to be replaced we
  1096. * have the three following conditions:
  1097. *
  1098. * a) if we have a PVID assigned, then all filters which are
  1099. * not marked as VLAN=PVID must be replaced with filters that
  1100. * are.
  1101. * b) otherwise, if we have any active VLANS, all filters
  1102. * which are marked as VLAN=-1 must be replaced with
  1103. * filters marked as VLAN=0
  1104. * c) finally, if we do not have any active VLANS, all filters
  1105. * which are marked as VLAN=0 must be replaced with filters
  1106. * marked as VLAN=-1
  1107. */
  1108. /* Update the filters about to be added in place */
  1109. hlist_for_each_entry(new, tmp_add_list, hlist) {
  1110. if (pvid && new->f->vlan != pvid)
  1111. new->f->vlan = pvid;
  1112. else if (vlan_filters && new->f->vlan == I40E_VLAN_ANY)
  1113. new->f->vlan = 0;
  1114. else if (!vlan_filters && new->f->vlan == 0)
  1115. new->f->vlan = I40E_VLAN_ANY;
  1116. }
  1117. /* Update the remaining active filters */
  1118. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  1119. /* Combine the checks for whether a filter needs to be changed
  1120. * and then determine the new VLAN inside the if block, in
  1121. * order to avoid duplicating code for adding the new filter
  1122. * then deleting the old filter.
  1123. */
  1124. if ((pvid && f->vlan != pvid) ||
  1125. (vlan_filters && f->vlan == I40E_VLAN_ANY) ||
  1126. (!vlan_filters && f->vlan == 0)) {
  1127. /* Determine the new vlan we will be adding */
  1128. if (pvid)
  1129. new_vlan = pvid;
  1130. else if (vlan_filters)
  1131. new_vlan = 0;
  1132. else
  1133. new_vlan = I40E_VLAN_ANY;
  1134. /* Create the new filter */
  1135. add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
  1136. if (!add_head)
  1137. return -ENOMEM;
  1138. /* Create a temporary i40e_new_mac_filter */
  1139. new = kzalloc(sizeof(*new), GFP_ATOMIC);
  1140. if (!new)
  1141. return -ENOMEM;
  1142. new->f = add_head;
  1143. new->state = add_head->state;
  1144. /* Add the new filter to the tmp list */
  1145. hlist_add_head(&new->hlist, tmp_add_list);
  1146. /* Put the original filter into the delete list */
  1147. f->state = I40E_FILTER_REMOVE;
  1148. hash_del(&f->hlist);
  1149. hlist_add_head(&f->hlist, tmp_del_list);
  1150. }
  1151. }
  1152. vsi->has_vlan_filter = !!vlan_filters;
  1153. return 0;
  1154. }
  1155. /**
  1156. * i40e_rm_default_mac_filter - Remove the default MAC filter set by NVM
  1157. * @vsi: the PF Main VSI - inappropriate for any other VSI
  1158. * @macaddr: the MAC address
  1159. *
  1160. * Remove whatever filter the firmware set up so the driver can manage
  1161. * its own filtering intelligently.
  1162. **/
  1163. static void i40e_rm_default_mac_filter(struct i40e_vsi *vsi, u8 *macaddr)
  1164. {
  1165. struct i40e_aqc_remove_macvlan_element_data element;
  1166. struct i40e_pf *pf = vsi->back;
  1167. /* Only appropriate for the PF main VSI */
  1168. if (vsi->type != I40E_VSI_MAIN)
  1169. return;
  1170. memset(&element, 0, sizeof(element));
  1171. ether_addr_copy(element.mac_addr, macaddr);
  1172. element.vlan_tag = 0;
  1173. /* Ignore error returns, some firmware does it this way... */
  1174. element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
  1175. i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
  1176. memset(&element, 0, sizeof(element));
  1177. ether_addr_copy(element.mac_addr, macaddr);
  1178. element.vlan_tag = 0;
  1179. /* ...and some firmware does it this way. */
  1180. element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH |
  1181. I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
  1182. i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
  1183. }
  1184. /**
  1185. * i40e_add_filter - Add a mac/vlan filter to the VSI
  1186. * @vsi: the VSI to be searched
  1187. * @macaddr: the MAC address
  1188. * @vlan: the vlan
  1189. *
  1190. * Returns ptr to the filter object or NULL when no memory available.
  1191. *
  1192. * NOTE: This function is expected to be called with mac_filter_hash_lock
  1193. * being held.
  1194. **/
  1195. struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
  1196. const u8 *macaddr, s16 vlan)
  1197. {
  1198. struct i40e_mac_filter *f;
  1199. u64 key;
  1200. if (!vsi || !macaddr)
  1201. return NULL;
  1202. f = i40e_find_filter(vsi, macaddr, vlan);
  1203. if (!f) {
  1204. f = kzalloc(sizeof(*f), GFP_ATOMIC);
  1205. if (!f)
  1206. return NULL;
  1207. /* Update the boolean indicating if we need to function in
  1208. * VLAN mode.
  1209. */
  1210. if (vlan >= 0)
  1211. vsi->has_vlan_filter = true;
  1212. ether_addr_copy(f->macaddr, macaddr);
  1213. f->vlan = vlan;
  1214. f->state = I40E_FILTER_NEW;
  1215. INIT_HLIST_NODE(&f->hlist);
  1216. key = i40e_addr_to_hkey(macaddr);
  1217. hash_add(vsi->mac_filter_hash, &f->hlist, key);
  1218. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  1219. set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
  1220. }
  1221. /* If we're asked to add a filter that has been marked for removal, it
  1222. * is safe to simply restore it to active state. __i40e_del_filter
  1223. * will have simply deleted any filters which were previously marked
  1224. * NEW or FAILED, so if it is currently marked REMOVE it must have
  1225. * previously been ACTIVE. Since we haven't yet run the sync filters
  1226. * task, just restore this filter to the ACTIVE state so that the
  1227. * sync task leaves it in place
  1228. */
  1229. if (f->state == I40E_FILTER_REMOVE)
  1230. f->state = I40E_FILTER_ACTIVE;
  1231. return f;
  1232. }
  1233. /**
  1234. * __i40e_del_filter - Remove a specific filter from the VSI
  1235. * @vsi: VSI to remove from
  1236. * @f: the filter to remove from the list
  1237. *
  1238. * This function should be called instead of i40e_del_filter only if you know
  1239. * the exact filter you will remove already, such as via i40e_find_filter or
  1240. * i40e_find_mac.
  1241. *
  1242. * NOTE: This function is expected to be called with mac_filter_hash_lock
  1243. * being held.
  1244. * ANOTHER NOTE: This function MUST be called from within the context of
  1245. * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
  1246. * instead of list_for_each_entry().
  1247. **/
  1248. void __i40e_del_filter(struct i40e_vsi *vsi, struct i40e_mac_filter *f)
  1249. {
  1250. if (!f)
  1251. return;
  1252. /* If the filter was never added to firmware then we can just delete it
  1253. * directly and we don't want to set the status to remove or else an
  1254. * admin queue command will unnecessarily fire.
  1255. */
  1256. if ((f->state == I40E_FILTER_FAILED) ||
  1257. (f->state == I40E_FILTER_NEW)) {
  1258. hash_del(&f->hlist);
  1259. kfree(f);
  1260. } else {
  1261. f->state = I40E_FILTER_REMOVE;
  1262. }
  1263. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  1264. set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->state);
  1265. }
  1266. /**
  1267. * i40e_del_filter - Remove a MAC/VLAN filter from the VSI
  1268. * @vsi: the VSI to be searched
  1269. * @macaddr: the MAC address
  1270. * @vlan: the VLAN
  1271. *
  1272. * NOTE: This function is expected to be called with mac_filter_hash_lock
  1273. * being held.
  1274. * ANOTHER NOTE: This function MUST be called from within the context of
  1275. * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
  1276. * instead of list_for_each_entry().
  1277. **/
  1278. void i40e_del_filter(struct i40e_vsi *vsi, const u8 *macaddr, s16 vlan)
  1279. {
  1280. struct i40e_mac_filter *f;
  1281. if (!vsi || !macaddr)
  1282. return;
  1283. f = i40e_find_filter(vsi, macaddr, vlan);
  1284. __i40e_del_filter(vsi, f);
  1285. }
  1286. /**
  1287. * i40e_add_mac_filter - Add a MAC filter for all active VLANs
  1288. * @vsi: the VSI to be searched
  1289. * @macaddr: the mac address to be filtered
  1290. *
  1291. * If we're not in VLAN mode, just add the filter to I40E_VLAN_ANY. Otherwise,
  1292. * go through all the macvlan filters and add a macvlan filter for each
  1293. * unique vlan that already exists. If a PVID has been assigned, instead only
  1294. * add the macaddr to that VLAN.
  1295. *
  1296. * Returns last filter added on success, else NULL
  1297. **/
  1298. struct i40e_mac_filter *i40e_add_mac_filter(struct i40e_vsi *vsi,
  1299. const u8 *macaddr)
  1300. {
  1301. struct i40e_mac_filter *f, *add = NULL;
  1302. struct hlist_node *h;
  1303. int bkt;
  1304. if (vsi->info.pvid)
  1305. return i40e_add_filter(vsi, macaddr,
  1306. le16_to_cpu(vsi->info.pvid));
  1307. if (!i40e_is_vsi_in_vlan(vsi))
  1308. return i40e_add_filter(vsi, macaddr, I40E_VLAN_ANY);
  1309. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  1310. if (f->state == I40E_FILTER_REMOVE)
  1311. continue;
  1312. add = i40e_add_filter(vsi, macaddr, f->vlan);
  1313. if (!add)
  1314. return NULL;
  1315. }
  1316. return add;
  1317. }
  1318. /**
  1319. * i40e_del_mac_filter - Remove a MAC filter from all VLANs
  1320. * @vsi: the VSI to be searched
  1321. * @macaddr: the mac address to be removed
  1322. *
  1323. * Removes a given MAC address from a VSI regardless of what VLAN it has been
  1324. * associated with.
  1325. *
  1326. * Returns 0 for success, or error
  1327. **/
  1328. int i40e_del_mac_filter(struct i40e_vsi *vsi, const u8 *macaddr)
  1329. {
  1330. struct i40e_mac_filter *f;
  1331. struct hlist_node *h;
  1332. bool found = false;
  1333. int bkt;
  1334. WARN(!spin_is_locked(&vsi->mac_filter_hash_lock),
  1335. "Missing mac_filter_hash_lock\n");
  1336. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  1337. if (ether_addr_equal(macaddr, f->macaddr)) {
  1338. __i40e_del_filter(vsi, f);
  1339. found = true;
  1340. }
  1341. }
  1342. if (found)
  1343. return 0;
  1344. else
  1345. return -ENOENT;
  1346. }
  1347. /**
  1348. * i40e_set_mac - NDO callback to set mac address
  1349. * @netdev: network interface device structure
  1350. * @p: pointer to an address structure
  1351. *
  1352. * Returns 0 on success, negative on failure
  1353. **/
  1354. static int i40e_set_mac(struct net_device *netdev, void *p)
  1355. {
  1356. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1357. struct i40e_vsi *vsi = np->vsi;
  1358. struct i40e_pf *pf = vsi->back;
  1359. struct i40e_hw *hw = &pf->hw;
  1360. struct sockaddr *addr = p;
  1361. if (!is_valid_ether_addr(addr->sa_data))
  1362. return -EADDRNOTAVAIL;
  1363. if (ether_addr_equal(netdev->dev_addr, addr->sa_data)) {
  1364. netdev_info(netdev, "already using mac address %pM\n",
  1365. addr->sa_data);
  1366. return 0;
  1367. }
  1368. if (test_bit(__I40E_VSI_DOWN, vsi->back->state) ||
  1369. test_bit(__I40E_RESET_RECOVERY_PENDING, vsi->back->state))
  1370. return -EADDRNOTAVAIL;
  1371. if (ether_addr_equal(hw->mac.addr, addr->sa_data))
  1372. netdev_info(netdev, "returning to hw mac address %pM\n",
  1373. hw->mac.addr);
  1374. else
  1375. netdev_info(netdev, "set new mac address %pM\n", addr->sa_data);
  1376. /* Copy the address first, so that we avoid a possible race with
  1377. * .set_rx_mode(). If we copy after changing the address in the filter
  1378. * list, we might open ourselves to a narrow race window where
  1379. * .set_rx_mode could delete our dev_addr filter and prevent traffic
  1380. * from passing.
  1381. */
  1382. ether_addr_copy(netdev->dev_addr, addr->sa_data);
  1383. spin_lock_bh(&vsi->mac_filter_hash_lock);
  1384. i40e_del_mac_filter(vsi, netdev->dev_addr);
  1385. i40e_add_mac_filter(vsi, addr->sa_data);
  1386. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  1387. if (vsi->type == I40E_VSI_MAIN) {
  1388. i40e_status ret;
  1389. ret = i40e_aq_mac_address_write(&vsi->back->hw,
  1390. I40E_AQC_WRITE_TYPE_LAA_WOL,
  1391. addr->sa_data, NULL);
  1392. if (ret)
  1393. netdev_info(netdev, "Ignoring error from firmware on LAA update, status %s, AQ ret %s\n",
  1394. i40e_stat_str(hw, ret),
  1395. i40e_aq_str(hw, hw->aq.asq_last_status));
  1396. }
  1397. /* schedule our worker thread which will take care of
  1398. * applying the new filter changes
  1399. */
  1400. i40e_service_event_schedule(vsi->back);
  1401. return 0;
  1402. }
  1403. /**
  1404. * i40e_config_rss_aq - Prepare for RSS using AQ commands
  1405. * @vsi: vsi structure
  1406. * @seed: RSS hash seed
  1407. **/
  1408. static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
  1409. u8 *lut, u16 lut_size)
  1410. {
  1411. struct i40e_pf *pf = vsi->back;
  1412. struct i40e_hw *hw = &pf->hw;
  1413. int ret = 0;
  1414. if (seed) {
  1415. struct i40e_aqc_get_set_rss_key_data *seed_dw =
  1416. (struct i40e_aqc_get_set_rss_key_data *)seed;
  1417. ret = i40e_aq_set_rss_key(hw, vsi->id, seed_dw);
  1418. if (ret) {
  1419. dev_info(&pf->pdev->dev,
  1420. "Cannot set RSS key, err %s aq_err %s\n",
  1421. i40e_stat_str(hw, ret),
  1422. i40e_aq_str(hw, hw->aq.asq_last_status));
  1423. return ret;
  1424. }
  1425. }
  1426. if (lut) {
  1427. bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
  1428. ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
  1429. if (ret) {
  1430. dev_info(&pf->pdev->dev,
  1431. "Cannot set RSS lut, err %s aq_err %s\n",
  1432. i40e_stat_str(hw, ret),
  1433. i40e_aq_str(hw, hw->aq.asq_last_status));
  1434. return ret;
  1435. }
  1436. }
  1437. return ret;
  1438. }
  1439. /**
  1440. * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
  1441. * @vsi: VSI structure
  1442. **/
  1443. static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
  1444. {
  1445. struct i40e_pf *pf = vsi->back;
  1446. u8 seed[I40E_HKEY_ARRAY_SIZE];
  1447. u8 *lut;
  1448. int ret;
  1449. if (!(pf->hw_features & I40E_HW_RSS_AQ_CAPABLE))
  1450. return 0;
  1451. if (!vsi->rss_size)
  1452. vsi->rss_size = min_t(int, pf->alloc_rss_size,
  1453. vsi->num_queue_pairs);
  1454. if (!vsi->rss_size)
  1455. return -EINVAL;
  1456. lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
  1457. if (!lut)
  1458. return -ENOMEM;
  1459. /* Use the user configured hash keys and lookup table if there is one,
  1460. * otherwise use default
  1461. */
  1462. if (vsi->rss_lut_user)
  1463. memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
  1464. else
  1465. i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
  1466. if (vsi->rss_hkey_user)
  1467. memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
  1468. else
  1469. netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
  1470. ret = i40e_config_rss_aq(vsi, seed, lut, vsi->rss_table_size);
  1471. kfree(lut);
  1472. return ret;
  1473. }
  1474. /**
  1475. * i40e_vsi_setup_queue_map_mqprio - Prepares mqprio based tc_config
  1476. * @vsi: the VSI being configured,
  1477. * @ctxt: VSI context structure
  1478. * @enabled_tc: number of traffic classes to enable
  1479. *
  1480. * Prepares VSI tc_config to have queue configurations based on MQPRIO options.
  1481. **/
  1482. static int i40e_vsi_setup_queue_map_mqprio(struct i40e_vsi *vsi,
  1483. struct i40e_vsi_context *ctxt,
  1484. u8 enabled_tc)
  1485. {
  1486. u16 qcount = 0, max_qcount, qmap, sections = 0;
  1487. int i, override_q, pow, num_qps, ret;
  1488. u8 netdev_tc = 0, offset = 0;
  1489. if (vsi->type != I40E_VSI_MAIN)
  1490. return -EINVAL;
  1491. sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
  1492. sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
  1493. vsi->tc_config.numtc = vsi->mqprio_qopt.qopt.num_tc;
  1494. vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
  1495. num_qps = vsi->mqprio_qopt.qopt.count[0];
  1496. /* find the next higher power-of-2 of num queue pairs */
  1497. pow = ilog2(num_qps);
  1498. if (!is_power_of_2(num_qps))
  1499. pow++;
  1500. qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
  1501. (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
  1502. /* Setup queue offset/count for all TCs for given VSI */
  1503. max_qcount = vsi->mqprio_qopt.qopt.count[0];
  1504. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  1505. /* See if the given TC is enabled for the given VSI */
  1506. if (vsi->tc_config.enabled_tc & BIT(i)) {
  1507. offset = vsi->mqprio_qopt.qopt.offset[i];
  1508. qcount = vsi->mqprio_qopt.qopt.count[i];
  1509. if (qcount > max_qcount)
  1510. max_qcount = qcount;
  1511. vsi->tc_config.tc_info[i].qoffset = offset;
  1512. vsi->tc_config.tc_info[i].qcount = qcount;
  1513. vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
  1514. } else {
  1515. /* TC is not enabled so set the offset to
  1516. * default queue and allocate one queue
  1517. * for the given TC.
  1518. */
  1519. vsi->tc_config.tc_info[i].qoffset = 0;
  1520. vsi->tc_config.tc_info[i].qcount = 1;
  1521. vsi->tc_config.tc_info[i].netdev_tc = 0;
  1522. }
  1523. }
  1524. /* Set actual Tx/Rx queue pairs */
  1525. vsi->num_queue_pairs = offset + qcount;
  1526. /* Setup queue TC[0].qmap for given VSI context */
  1527. ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
  1528. ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
  1529. ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
  1530. ctxt->info.valid_sections |= cpu_to_le16(sections);
  1531. /* Reconfigure RSS for main VSI with max queue count */
  1532. vsi->rss_size = max_qcount;
  1533. ret = i40e_vsi_config_rss(vsi);
  1534. if (ret) {
  1535. dev_info(&vsi->back->pdev->dev,
  1536. "Failed to reconfig rss for num_queues (%u)\n",
  1537. max_qcount);
  1538. return ret;
  1539. }
  1540. vsi->reconfig_rss = true;
  1541. dev_dbg(&vsi->back->pdev->dev,
  1542. "Reconfigured rss with num_queues (%u)\n", max_qcount);
  1543. /* Find queue count available for channel VSIs and starting offset
  1544. * for channel VSIs
  1545. */
  1546. override_q = vsi->mqprio_qopt.qopt.count[0];
  1547. if (override_q && override_q < vsi->num_queue_pairs) {
  1548. vsi->cnt_q_avail = vsi->num_queue_pairs - override_q;
  1549. vsi->next_base_queue = override_q;
  1550. }
  1551. return 0;
  1552. }
  1553. /**
  1554. * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
  1555. * @vsi: the VSI being setup
  1556. * @ctxt: VSI context structure
  1557. * @enabled_tc: Enabled TCs bitmap
  1558. * @is_add: True if called before Add VSI
  1559. *
  1560. * Setup VSI queue mapping for enabled traffic classes.
  1561. **/
  1562. static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
  1563. struct i40e_vsi_context *ctxt,
  1564. u8 enabled_tc,
  1565. bool is_add)
  1566. {
  1567. struct i40e_pf *pf = vsi->back;
  1568. u16 sections = 0;
  1569. u8 netdev_tc = 0;
  1570. u16 numtc = 1;
  1571. u16 qcount;
  1572. u8 offset;
  1573. u16 qmap;
  1574. int i;
  1575. u16 num_tc_qps = 0;
  1576. sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
  1577. offset = 0;
  1578. /* Number of queues per enabled TC */
  1579. num_tc_qps = vsi->alloc_queue_pairs;
  1580. if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
  1581. /* Find numtc from enabled TC bitmap */
  1582. for (i = 0, numtc = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  1583. if (enabled_tc & BIT(i)) /* TC is enabled */
  1584. numtc++;
  1585. }
  1586. if (!numtc) {
  1587. dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
  1588. numtc = 1;
  1589. }
  1590. num_tc_qps = num_tc_qps / numtc;
  1591. num_tc_qps = min_t(int, num_tc_qps,
  1592. i40e_pf_get_max_q_per_tc(pf));
  1593. }
  1594. vsi->tc_config.numtc = numtc;
  1595. vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
  1596. /* Do not allow use more TC queue pairs than MSI-X vectors exist */
  1597. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  1598. num_tc_qps = min_t(int, num_tc_qps, pf->num_lan_msix);
  1599. /* Setup queue offset/count for all TCs for given VSI */
  1600. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  1601. /* See if the given TC is enabled for the given VSI */
  1602. if (vsi->tc_config.enabled_tc & BIT(i)) {
  1603. /* TC is enabled */
  1604. int pow, num_qps;
  1605. switch (vsi->type) {
  1606. case I40E_VSI_MAIN:
  1607. if (!(pf->flags & (I40E_FLAG_FD_SB_ENABLED |
  1608. I40E_FLAG_FD_ATR_ENABLED)) ||
  1609. vsi->tc_config.enabled_tc != 1) {
  1610. qcount = min_t(int, pf->alloc_rss_size,
  1611. num_tc_qps);
  1612. break;
  1613. }
  1614. /* fall through */
  1615. case I40E_VSI_FDIR:
  1616. case I40E_VSI_SRIOV:
  1617. case I40E_VSI_VMDQ2:
  1618. default:
  1619. qcount = num_tc_qps;
  1620. WARN_ON(i != 0);
  1621. break;
  1622. }
  1623. vsi->tc_config.tc_info[i].qoffset = offset;
  1624. vsi->tc_config.tc_info[i].qcount = qcount;
  1625. /* find the next higher power-of-2 of num queue pairs */
  1626. num_qps = qcount;
  1627. pow = 0;
  1628. while (num_qps && (BIT_ULL(pow) < qcount)) {
  1629. pow++;
  1630. num_qps >>= 1;
  1631. }
  1632. vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
  1633. qmap =
  1634. (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
  1635. (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
  1636. offset += qcount;
  1637. } else {
  1638. /* TC is not enabled so set the offset to
  1639. * default queue and allocate one queue
  1640. * for the given TC.
  1641. */
  1642. vsi->tc_config.tc_info[i].qoffset = 0;
  1643. vsi->tc_config.tc_info[i].qcount = 1;
  1644. vsi->tc_config.tc_info[i].netdev_tc = 0;
  1645. qmap = 0;
  1646. }
  1647. ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
  1648. }
  1649. /* Set actual Tx/Rx queue pairs */
  1650. vsi->num_queue_pairs = offset;
  1651. if ((vsi->type == I40E_VSI_MAIN) && (numtc == 1)) {
  1652. if (vsi->req_queue_pairs > 0)
  1653. vsi->num_queue_pairs = vsi->req_queue_pairs;
  1654. else if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  1655. vsi->num_queue_pairs = pf->num_lan_msix;
  1656. }
  1657. /* Scheduler section valid can only be set for ADD VSI */
  1658. if (is_add) {
  1659. sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
  1660. ctxt->info.up_enable_bits = enabled_tc;
  1661. }
  1662. if (vsi->type == I40E_VSI_SRIOV) {
  1663. ctxt->info.mapping_flags |=
  1664. cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
  1665. for (i = 0; i < vsi->num_queue_pairs; i++)
  1666. ctxt->info.queue_mapping[i] =
  1667. cpu_to_le16(vsi->base_queue + i);
  1668. } else {
  1669. ctxt->info.mapping_flags |=
  1670. cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
  1671. ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
  1672. }
  1673. ctxt->info.valid_sections |= cpu_to_le16(sections);
  1674. }
  1675. /**
  1676. * i40e_addr_sync - Callback for dev_(mc|uc)_sync to add address
  1677. * @netdev: the netdevice
  1678. * @addr: address to add
  1679. *
  1680. * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
  1681. * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
  1682. */
  1683. static int i40e_addr_sync(struct net_device *netdev, const u8 *addr)
  1684. {
  1685. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1686. struct i40e_vsi *vsi = np->vsi;
  1687. if (i40e_add_mac_filter(vsi, addr))
  1688. return 0;
  1689. else
  1690. return -ENOMEM;
  1691. }
  1692. /**
  1693. * i40e_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
  1694. * @netdev: the netdevice
  1695. * @addr: address to add
  1696. *
  1697. * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call
  1698. * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
  1699. */
  1700. static int i40e_addr_unsync(struct net_device *netdev, const u8 *addr)
  1701. {
  1702. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1703. struct i40e_vsi *vsi = np->vsi;
  1704. /* Under some circumstances, we might receive a request to delete
  1705. * our own device address from our uc list. Because we store the
  1706. * device address in the VSI's MAC/VLAN filter list, we need to ignore
  1707. * such requests and not delete our device address from this list.
  1708. */
  1709. if (ether_addr_equal(addr, netdev->dev_addr))
  1710. return 0;
  1711. i40e_del_mac_filter(vsi, addr);
  1712. return 0;
  1713. }
  1714. /**
  1715. * i40e_set_rx_mode - NDO callback to set the netdev filters
  1716. * @netdev: network interface device structure
  1717. **/
  1718. static void i40e_set_rx_mode(struct net_device *netdev)
  1719. {
  1720. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1721. struct i40e_vsi *vsi = np->vsi;
  1722. spin_lock_bh(&vsi->mac_filter_hash_lock);
  1723. __dev_uc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
  1724. __dev_mc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
  1725. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  1726. /* check for other flag changes */
  1727. if (vsi->current_netdev_flags != vsi->netdev->flags) {
  1728. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  1729. set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
  1730. }
  1731. }
  1732. /**
  1733. * i40e_undo_del_filter_entries - Undo the changes made to MAC filter entries
  1734. * @vsi: Pointer to VSI struct
  1735. * @from: Pointer to list which contains MAC filter entries - changes to
  1736. * those entries needs to be undone.
  1737. *
  1738. * MAC filter entries from this list were slated for deletion.
  1739. **/
  1740. static void i40e_undo_del_filter_entries(struct i40e_vsi *vsi,
  1741. struct hlist_head *from)
  1742. {
  1743. struct i40e_mac_filter *f;
  1744. struct hlist_node *h;
  1745. hlist_for_each_entry_safe(f, h, from, hlist) {
  1746. u64 key = i40e_addr_to_hkey(f->macaddr);
  1747. /* Move the element back into MAC filter list*/
  1748. hlist_del(&f->hlist);
  1749. hash_add(vsi->mac_filter_hash, &f->hlist, key);
  1750. }
  1751. }
  1752. /**
  1753. * i40e_undo_add_filter_entries - Undo the changes made to MAC filter entries
  1754. * @vsi: Pointer to vsi struct
  1755. * @from: Pointer to list which contains MAC filter entries - changes to
  1756. * those entries needs to be undone.
  1757. *
  1758. * MAC filter entries from this list were slated for addition.
  1759. **/
  1760. static void i40e_undo_add_filter_entries(struct i40e_vsi *vsi,
  1761. struct hlist_head *from)
  1762. {
  1763. struct i40e_new_mac_filter *new;
  1764. struct hlist_node *h;
  1765. hlist_for_each_entry_safe(new, h, from, hlist) {
  1766. /* We can simply free the wrapper structure */
  1767. hlist_del(&new->hlist);
  1768. kfree(new);
  1769. }
  1770. }
  1771. /**
  1772. * i40e_next_entry - Get the next non-broadcast filter from a list
  1773. * @next: pointer to filter in list
  1774. *
  1775. * Returns the next non-broadcast filter in the list. Required so that we
  1776. * ignore broadcast filters within the list, since these are not handled via
  1777. * the normal firmware update path.
  1778. */
  1779. static
  1780. struct i40e_new_mac_filter *i40e_next_filter(struct i40e_new_mac_filter *next)
  1781. {
  1782. hlist_for_each_entry_continue(next, hlist) {
  1783. if (!is_broadcast_ether_addr(next->f->macaddr))
  1784. return next;
  1785. }
  1786. return NULL;
  1787. }
  1788. /**
  1789. * i40e_update_filter_state - Update filter state based on return data
  1790. * from firmware
  1791. * @count: Number of filters added
  1792. * @add_list: return data from fw
  1793. * @add_head: pointer to first filter in current batch
  1794. *
  1795. * MAC filter entries from list were slated to be added to device. Returns
  1796. * number of successful filters. Note that 0 does NOT mean success!
  1797. **/
  1798. static int
  1799. i40e_update_filter_state(int count,
  1800. struct i40e_aqc_add_macvlan_element_data *add_list,
  1801. struct i40e_new_mac_filter *add_head)
  1802. {
  1803. int retval = 0;
  1804. int i;
  1805. for (i = 0; i < count; i++) {
  1806. /* Always check status of each filter. We don't need to check
  1807. * the firmware return status because we pre-set the filter
  1808. * status to I40E_AQC_MM_ERR_NO_RES when sending the filter
  1809. * request to the adminq. Thus, if it no longer matches then
  1810. * we know the filter is active.
  1811. */
  1812. if (add_list[i].match_method == I40E_AQC_MM_ERR_NO_RES) {
  1813. add_head->state = I40E_FILTER_FAILED;
  1814. } else {
  1815. add_head->state = I40E_FILTER_ACTIVE;
  1816. retval++;
  1817. }
  1818. add_head = i40e_next_filter(add_head);
  1819. if (!add_head)
  1820. break;
  1821. }
  1822. return retval;
  1823. }
  1824. /**
  1825. * i40e_aqc_del_filters - Request firmware to delete a set of filters
  1826. * @vsi: ptr to the VSI
  1827. * @vsi_name: name to display in messages
  1828. * @list: the list of filters to send to firmware
  1829. * @num_del: the number of filters to delete
  1830. * @retval: Set to -EIO on failure to delete
  1831. *
  1832. * Send a request to firmware via AdminQ to delete a set of filters. Uses
  1833. * *retval instead of a return value so that success does not force ret_val to
  1834. * be set to 0. This ensures that a sequence of calls to this function
  1835. * preserve the previous value of *retval on successful delete.
  1836. */
  1837. static
  1838. void i40e_aqc_del_filters(struct i40e_vsi *vsi, const char *vsi_name,
  1839. struct i40e_aqc_remove_macvlan_element_data *list,
  1840. int num_del, int *retval)
  1841. {
  1842. struct i40e_hw *hw = &vsi->back->hw;
  1843. i40e_status aq_ret;
  1844. int aq_err;
  1845. aq_ret = i40e_aq_remove_macvlan(hw, vsi->seid, list, num_del, NULL);
  1846. aq_err = hw->aq.asq_last_status;
  1847. /* Explicitly ignore and do not report when firmware returns ENOENT */
  1848. if (aq_ret && !(aq_err == I40E_AQ_RC_ENOENT)) {
  1849. *retval = -EIO;
  1850. dev_info(&vsi->back->pdev->dev,
  1851. "ignoring delete macvlan error on %s, err %s, aq_err %s\n",
  1852. vsi_name, i40e_stat_str(hw, aq_ret),
  1853. i40e_aq_str(hw, aq_err));
  1854. }
  1855. }
  1856. /**
  1857. * i40e_aqc_add_filters - Request firmware to add a set of filters
  1858. * @vsi: ptr to the VSI
  1859. * @vsi_name: name to display in messages
  1860. * @list: the list of filters to send to firmware
  1861. * @add_head: Position in the add hlist
  1862. * @num_add: the number of filters to add
  1863. *
  1864. * Send a request to firmware via AdminQ to add a chunk of filters. Will set
  1865. * __I40E_VSI_OVERFLOW_PROMISC bit in vsi->state if the firmware has run out of
  1866. * space for more filters.
  1867. */
  1868. static
  1869. void i40e_aqc_add_filters(struct i40e_vsi *vsi, const char *vsi_name,
  1870. struct i40e_aqc_add_macvlan_element_data *list,
  1871. struct i40e_new_mac_filter *add_head,
  1872. int num_add)
  1873. {
  1874. struct i40e_hw *hw = &vsi->back->hw;
  1875. int aq_err, fcnt;
  1876. i40e_aq_add_macvlan(hw, vsi->seid, list, num_add, NULL);
  1877. aq_err = hw->aq.asq_last_status;
  1878. fcnt = i40e_update_filter_state(num_add, list, add_head);
  1879. if (fcnt != num_add) {
  1880. set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
  1881. dev_warn(&vsi->back->pdev->dev,
  1882. "Error %s adding RX filters on %s, promiscuous mode forced on\n",
  1883. i40e_aq_str(hw, aq_err),
  1884. vsi_name);
  1885. }
  1886. }
  1887. /**
  1888. * i40e_aqc_broadcast_filter - Set promiscuous broadcast flags
  1889. * @vsi: pointer to the VSI
  1890. * @vsi_name: the VSI name
  1891. * @f: filter data
  1892. *
  1893. * This function sets or clears the promiscuous broadcast flags for VLAN
  1894. * filters in order to properly receive broadcast frames. Assumes that only
  1895. * broadcast filters are passed.
  1896. *
  1897. * Returns status indicating success or failure;
  1898. **/
  1899. static i40e_status
  1900. i40e_aqc_broadcast_filter(struct i40e_vsi *vsi, const char *vsi_name,
  1901. struct i40e_mac_filter *f)
  1902. {
  1903. bool enable = f->state == I40E_FILTER_NEW;
  1904. struct i40e_hw *hw = &vsi->back->hw;
  1905. i40e_status aq_ret;
  1906. if (f->vlan == I40E_VLAN_ANY) {
  1907. aq_ret = i40e_aq_set_vsi_broadcast(hw,
  1908. vsi->seid,
  1909. enable,
  1910. NULL);
  1911. } else {
  1912. aq_ret = i40e_aq_set_vsi_bc_promisc_on_vlan(hw,
  1913. vsi->seid,
  1914. enable,
  1915. f->vlan,
  1916. NULL);
  1917. }
  1918. if (aq_ret) {
  1919. set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
  1920. dev_warn(&vsi->back->pdev->dev,
  1921. "Error %s, forcing overflow promiscuous on %s\n",
  1922. i40e_aq_str(hw, hw->aq.asq_last_status),
  1923. vsi_name);
  1924. }
  1925. return aq_ret;
  1926. }
  1927. /**
  1928. * i40e_set_promiscuous - set promiscuous mode
  1929. * @pf: board private structure
  1930. * @promisc: promisc on or off
  1931. *
  1932. * There are different ways of setting promiscuous mode on a PF depending on
  1933. * what state/environment we're in. This identifies and sets it appropriately.
  1934. * Returns 0 on success.
  1935. **/
  1936. static int i40e_set_promiscuous(struct i40e_pf *pf, bool promisc)
  1937. {
  1938. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  1939. struct i40e_hw *hw = &pf->hw;
  1940. i40e_status aq_ret;
  1941. if (vsi->type == I40E_VSI_MAIN &&
  1942. pf->lan_veb != I40E_NO_VEB &&
  1943. !(pf->flags & I40E_FLAG_MFP_ENABLED)) {
  1944. /* set defport ON for Main VSI instead of true promisc
  1945. * this way we will get all unicast/multicast and VLAN
  1946. * promisc behavior but will not get VF or VMDq traffic
  1947. * replicated on the Main VSI.
  1948. */
  1949. if (promisc)
  1950. aq_ret = i40e_aq_set_default_vsi(hw,
  1951. vsi->seid,
  1952. NULL);
  1953. else
  1954. aq_ret = i40e_aq_clear_default_vsi(hw,
  1955. vsi->seid,
  1956. NULL);
  1957. if (aq_ret) {
  1958. dev_info(&pf->pdev->dev,
  1959. "Set default VSI failed, err %s, aq_err %s\n",
  1960. i40e_stat_str(hw, aq_ret),
  1961. i40e_aq_str(hw, hw->aq.asq_last_status));
  1962. }
  1963. } else {
  1964. aq_ret = i40e_aq_set_vsi_unicast_promiscuous(
  1965. hw,
  1966. vsi->seid,
  1967. promisc, NULL,
  1968. true);
  1969. if (aq_ret) {
  1970. dev_info(&pf->pdev->dev,
  1971. "set unicast promisc failed, err %s, aq_err %s\n",
  1972. i40e_stat_str(hw, aq_ret),
  1973. i40e_aq_str(hw, hw->aq.asq_last_status));
  1974. }
  1975. aq_ret = i40e_aq_set_vsi_multicast_promiscuous(
  1976. hw,
  1977. vsi->seid,
  1978. promisc, NULL);
  1979. if (aq_ret) {
  1980. dev_info(&pf->pdev->dev,
  1981. "set multicast promisc failed, err %s, aq_err %s\n",
  1982. i40e_stat_str(hw, aq_ret),
  1983. i40e_aq_str(hw, hw->aq.asq_last_status));
  1984. }
  1985. }
  1986. if (!aq_ret)
  1987. pf->cur_promisc = promisc;
  1988. return aq_ret;
  1989. }
  1990. /**
  1991. * i40e_sync_vsi_filters - Update the VSI filter list to the HW
  1992. * @vsi: ptr to the VSI
  1993. *
  1994. * Push any outstanding VSI filter changes through the AdminQ.
  1995. *
  1996. * Returns 0 or error value
  1997. **/
  1998. int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
  1999. {
  2000. struct hlist_head tmp_add_list, tmp_del_list;
  2001. struct i40e_mac_filter *f;
  2002. struct i40e_new_mac_filter *new, *add_head = NULL;
  2003. struct i40e_hw *hw = &vsi->back->hw;
  2004. bool old_overflow, new_overflow;
  2005. unsigned int failed_filters = 0;
  2006. unsigned int vlan_filters = 0;
  2007. char vsi_name[16] = "PF";
  2008. int filter_list_len = 0;
  2009. i40e_status aq_ret = 0;
  2010. u32 changed_flags = 0;
  2011. struct hlist_node *h;
  2012. struct i40e_pf *pf;
  2013. int num_add = 0;
  2014. int num_del = 0;
  2015. int retval = 0;
  2016. u16 cmd_flags;
  2017. int list_size;
  2018. int bkt;
  2019. /* empty array typed pointers, kcalloc later */
  2020. struct i40e_aqc_add_macvlan_element_data *add_list;
  2021. struct i40e_aqc_remove_macvlan_element_data *del_list;
  2022. while (test_and_set_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state))
  2023. usleep_range(1000, 2000);
  2024. pf = vsi->back;
  2025. old_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
  2026. if (vsi->netdev) {
  2027. changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
  2028. vsi->current_netdev_flags = vsi->netdev->flags;
  2029. }
  2030. INIT_HLIST_HEAD(&tmp_add_list);
  2031. INIT_HLIST_HEAD(&tmp_del_list);
  2032. if (vsi->type == I40E_VSI_SRIOV)
  2033. snprintf(vsi_name, sizeof(vsi_name) - 1, "VF %d", vsi->vf_id);
  2034. else if (vsi->type != I40E_VSI_MAIN)
  2035. snprintf(vsi_name, sizeof(vsi_name) - 1, "vsi %d", vsi->seid);
  2036. if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
  2037. vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
  2038. spin_lock_bh(&vsi->mac_filter_hash_lock);
  2039. /* Create a list of filters to delete. */
  2040. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  2041. if (f->state == I40E_FILTER_REMOVE) {
  2042. /* Move the element into temporary del_list */
  2043. hash_del(&f->hlist);
  2044. hlist_add_head(&f->hlist, &tmp_del_list);
  2045. /* Avoid counting removed filters */
  2046. continue;
  2047. }
  2048. if (f->state == I40E_FILTER_NEW) {
  2049. /* Create a temporary i40e_new_mac_filter */
  2050. new = kzalloc(sizeof(*new), GFP_ATOMIC);
  2051. if (!new)
  2052. goto err_no_memory_locked;
  2053. /* Store pointer to the real filter */
  2054. new->f = f;
  2055. new->state = f->state;
  2056. /* Add it to the hash list */
  2057. hlist_add_head(&new->hlist, &tmp_add_list);
  2058. }
  2059. /* Count the number of active (current and new) VLAN
  2060. * filters we have now. Does not count filters which
  2061. * are marked for deletion.
  2062. */
  2063. if (f->vlan > 0)
  2064. vlan_filters++;
  2065. }
  2066. retval = i40e_correct_mac_vlan_filters(vsi,
  2067. &tmp_add_list,
  2068. &tmp_del_list,
  2069. vlan_filters);
  2070. if (retval)
  2071. goto err_no_memory_locked;
  2072. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2073. }
  2074. /* Now process 'del_list' outside the lock */
  2075. if (!hlist_empty(&tmp_del_list)) {
  2076. filter_list_len = hw->aq.asq_buf_size /
  2077. sizeof(struct i40e_aqc_remove_macvlan_element_data);
  2078. list_size = filter_list_len *
  2079. sizeof(struct i40e_aqc_remove_macvlan_element_data);
  2080. del_list = kzalloc(list_size, GFP_ATOMIC);
  2081. if (!del_list)
  2082. goto err_no_memory;
  2083. hlist_for_each_entry_safe(f, h, &tmp_del_list, hlist) {
  2084. cmd_flags = 0;
  2085. /* handle broadcast filters by updating the broadcast
  2086. * promiscuous flag and release filter list.
  2087. */
  2088. if (is_broadcast_ether_addr(f->macaddr)) {
  2089. i40e_aqc_broadcast_filter(vsi, vsi_name, f);
  2090. hlist_del(&f->hlist);
  2091. kfree(f);
  2092. continue;
  2093. }
  2094. /* add to delete list */
  2095. ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
  2096. if (f->vlan == I40E_VLAN_ANY) {
  2097. del_list[num_del].vlan_tag = 0;
  2098. cmd_flags |= I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
  2099. } else {
  2100. del_list[num_del].vlan_tag =
  2101. cpu_to_le16((u16)(f->vlan));
  2102. }
  2103. cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
  2104. del_list[num_del].flags = cmd_flags;
  2105. num_del++;
  2106. /* flush a full buffer */
  2107. if (num_del == filter_list_len) {
  2108. i40e_aqc_del_filters(vsi, vsi_name, del_list,
  2109. num_del, &retval);
  2110. memset(del_list, 0, list_size);
  2111. num_del = 0;
  2112. }
  2113. /* Release memory for MAC filter entries which were
  2114. * synced up with HW.
  2115. */
  2116. hlist_del(&f->hlist);
  2117. kfree(f);
  2118. }
  2119. if (num_del) {
  2120. i40e_aqc_del_filters(vsi, vsi_name, del_list,
  2121. num_del, &retval);
  2122. }
  2123. kfree(del_list);
  2124. del_list = NULL;
  2125. }
  2126. if (!hlist_empty(&tmp_add_list)) {
  2127. /* Do all the adds now. */
  2128. filter_list_len = hw->aq.asq_buf_size /
  2129. sizeof(struct i40e_aqc_add_macvlan_element_data);
  2130. list_size = filter_list_len *
  2131. sizeof(struct i40e_aqc_add_macvlan_element_data);
  2132. add_list = kzalloc(list_size, GFP_ATOMIC);
  2133. if (!add_list)
  2134. goto err_no_memory;
  2135. num_add = 0;
  2136. hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
  2137. /* handle broadcast filters by updating the broadcast
  2138. * promiscuous flag instead of adding a MAC filter.
  2139. */
  2140. if (is_broadcast_ether_addr(new->f->macaddr)) {
  2141. if (i40e_aqc_broadcast_filter(vsi, vsi_name,
  2142. new->f))
  2143. new->state = I40E_FILTER_FAILED;
  2144. else
  2145. new->state = I40E_FILTER_ACTIVE;
  2146. continue;
  2147. }
  2148. /* add to add array */
  2149. if (num_add == 0)
  2150. add_head = new;
  2151. cmd_flags = 0;
  2152. ether_addr_copy(add_list[num_add].mac_addr,
  2153. new->f->macaddr);
  2154. if (new->f->vlan == I40E_VLAN_ANY) {
  2155. add_list[num_add].vlan_tag = 0;
  2156. cmd_flags |= I40E_AQC_MACVLAN_ADD_IGNORE_VLAN;
  2157. } else {
  2158. add_list[num_add].vlan_tag =
  2159. cpu_to_le16((u16)(new->f->vlan));
  2160. }
  2161. add_list[num_add].queue_number = 0;
  2162. /* set invalid match method for later detection */
  2163. add_list[num_add].match_method = I40E_AQC_MM_ERR_NO_RES;
  2164. cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
  2165. add_list[num_add].flags = cpu_to_le16(cmd_flags);
  2166. num_add++;
  2167. /* flush a full buffer */
  2168. if (num_add == filter_list_len) {
  2169. i40e_aqc_add_filters(vsi, vsi_name, add_list,
  2170. add_head, num_add);
  2171. memset(add_list, 0, list_size);
  2172. num_add = 0;
  2173. }
  2174. }
  2175. if (num_add) {
  2176. i40e_aqc_add_filters(vsi, vsi_name, add_list, add_head,
  2177. num_add);
  2178. }
  2179. /* Now move all of the filters from the temp add list back to
  2180. * the VSI's list.
  2181. */
  2182. spin_lock_bh(&vsi->mac_filter_hash_lock);
  2183. hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
  2184. /* Only update the state if we're still NEW */
  2185. if (new->f->state == I40E_FILTER_NEW)
  2186. new->f->state = new->state;
  2187. hlist_del(&new->hlist);
  2188. kfree(new);
  2189. }
  2190. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2191. kfree(add_list);
  2192. add_list = NULL;
  2193. }
  2194. /* Determine the number of active and failed filters. */
  2195. spin_lock_bh(&vsi->mac_filter_hash_lock);
  2196. vsi->active_filters = 0;
  2197. hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
  2198. if (f->state == I40E_FILTER_ACTIVE)
  2199. vsi->active_filters++;
  2200. else if (f->state == I40E_FILTER_FAILED)
  2201. failed_filters++;
  2202. }
  2203. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2204. /* Check if we are able to exit overflow promiscuous mode. We can
  2205. * safely exit if we didn't just enter, we no longer have any failed
  2206. * filters, and we have reduced filters below the threshold value.
  2207. */
  2208. if (old_overflow && !failed_filters &&
  2209. vsi->active_filters < vsi->promisc_threshold) {
  2210. dev_info(&pf->pdev->dev,
  2211. "filter logjam cleared on %s, leaving overflow promiscuous mode\n",
  2212. vsi_name);
  2213. clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
  2214. vsi->promisc_threshold = 0;
  2215. }
  2216. /* if the VF is not trusted do not do promisc */
  2217. if ((vsi->type == I40E_VSI_SRIOV) && !pf->vf[vsi->vf_id].trusted) {
  2218. clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
  2219. goto out;
  2220. }
  2221. new_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
  2222. /* If we are entering overflow promiscuous, we need to calculate a new
  2223. * threshold for when we are safe to exit
  2224. */
  2225. if (!old_overflow && new_overflow)
  2226. vsi->promisc_threshold = (vsi->active_filters * 3) / 4;
  2227. /* check for changes in promiscuous modes */
  2228. if (changed_flags & IFF_ALLMULTI) {
  2229. bool cur_multipromisc;
  2230. cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
  2231. aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
  2232. vsi->seid,
  2233. cur_multipromisc,
  2234. NULL);
  2235. if (aq_ret) {
  2236. retval = i40e_aq_rc_to_posix(aq_ret,
  2237. hw->aq.asq_last_status);
  2238. dev_info(&pf->pdev->dev,
  2239. "set multi promisc failed on %s, err %s aq_err %s\n",
  2240. vsi_name,
  2241. i40e_stat_str(hw, aq_ret),
  2242. i40e_aq_str(hw, hw->aq.asq_last_status));
  2243. }
  2244. }
  2245. if ((changed_flags & IFF_PROMISC) || old_overflow != new_overflow) {
  2246. bool cur_promisc;
  2247. cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
  2248. new_overflow);
  2249. aq_ret = i40e_set_promiscuous(pf, cur_promisc);
  2250. if (aq_ret) {
  2251. retval = i40e_aq_rc_to_posix(aq_ret,
  2252. hw->aq.asq_last_status);
  2253. dev_info(&pf->pdev->dev,
  2254. "Setting promiscuous %s failed on %s, err %s aq_err %s\n",
  2255. cur_promisc ? "on" : "off",
  2256. vsi_name,
  2257. i40e_stat_str(hw, aq_ret),
  2258. i40e_aq_str(hw, hw->aq.asq_last_status));
  2259. }
  2260. }
  2261. out:
  2262. /* if something went wrong then set the changed flag so we try again */
  2263. if (retval)
  2264. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  2265. clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
  2266. return retval;
  2267. err_no_memory:
  2268. /* Restore elements on the temporary add and delete lists */
  2269. spin_lock_bh(&vsi->mac_filter_hash_lock);
  2270. err_no_memory_locked:
  2271. i40e_undo_del_filter_entries(vsi, &tmp_del_list);
  2272. i40e_undo_add_filter_entries(vsi, &tmp_add_list);
  2273. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2274. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  2275. clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
  2276. return -ENOMEM;
  2277. }
  2278. /**
  2279. * i40e_sync_filters_subtask - Sync the VSI filter list with HW
  2280. * @pf: board private structure
  2281. **/
  2282. static void i40e_sync_filters_subtask(struct i40e_pf *pf)
  2283. {
  2284. int v;
  2285. if (!pf)
  2286. return;
  2287. if (!test_and_clear_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state))
  2288. return;
  2289. for (v = 0; v < pf->num_alloc_vsi; v++) {
  2290. if (pf->vsi[v] &&
  2291. (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED)) {
  2292. int ret = i40e_sync_vsi_filters(pf->vsi[v]);
  2293. if (ret) {
  2294. /* come back and try again later */
  2295. set_bit(__I40E_MACVLAN_SYNC_PENDING,
  2296. pf->state);
  2297. break;
  2298. }
  2299. }
  2300. }
  2301. }
  2302. /**
  2303. * i40e_max_xdp_frame_size - returns the maximum allowed frame size for XDP
  2304. * @vsi: the vsi
  2305. **/
  2306. static int i40e_max_xdp_frame_size(struct i40e_vsi *vsi)
  2307. {
  2308. if (PAGE_SIZE >= 8192 || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
  2309. return I40E_RXBUFFER_2048;
  2310. else
  2311. return I40E_RXBUFFER_3072;
  2312. }
  2313. /**
  2314. * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
  2315. * @netdev: network interface device structure
  2316. * @new_mtu: new value for maximum frame size
  2317. *
  2318. * Returns 0 on success, negative on failure
  2319. **/
  2320. static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
  2321. {
  2322. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2323. struct i40e_vsi *vsi = np->vsi;
  2324. struct i40e_pf *pf = vsi->back;
  2325. if (i40e_enabled_xdp_vsi(vsi)) {
  2326. int frame_size = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
  2327. if (frame_size > i40e_max_xdp_frame_size(vsi))
  2328. return -EINVAL;
  2329. }
  2330. netdev_info(netdev, "changing MTU from %d to %d\n",
  2331. netdev->mtu, new_mtu);
  2332. netdev->mtu = new_mtu;
  2333. if (netif_running(netdev))
  2334. i40e_vsi_reinit_locked(vsi);
  2335. set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
  2336. set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
  2337. return 0;
  2338. }
  2339. /**
  2340. * i40e_ioctl - Access the hwtstamp interface
  2341. * @netdev: network interface device structure
  2342. * @ifr: interface request data
  2343. * @cmd: ioctl command
  2344. **/
  2345. int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
  2346. {
  2347. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2348. struct i40e_pf *pf = np->vsi->back;
  2349. switch (cmd) {
  2350. case SIOCGHWTSTAMP:
  2351. return i40e_ptp_get_ts_config(pf, ifr);
  2352. case SIOCSHWTSTAMP:
  2353. return i40e_ptp_set_ts_config(pf, ifr);
  2354. default:
  2355. return -EOPNOTSUPP;
  2356. }
  2357. }
  2358. /**
  2359. * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
  2360. * @vsi: the vsi being adjusted
  2361. **/
  2362. void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
  2363. {
  2364. struct i40e_vsi_context ctxt;
  2365. i40e_status ret;
  2366. if ((vsi->info.valid_sections &
  2367. cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
  2368. ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
  2369. return; /* already enabled */
  2370. vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
  2371. vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
  2372. I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
  2373. ctxt.seid = vsi->seid;
  2374. ctxt.info = vsi->info;
  2375. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  2376. if (ret) {
  2377. dev_info(&vsi->back->pdev->dev,
  2378. "update vlan stripping failed, err %s aq_err %s\n",
  2379. i40e_stat_str(&vsi->back->hw, ret),
  2380. i40e_aq_str(&vsi->back->hw,
  2381. vsi->back->hw.aq.asq_last_status));
  2382. }
  2383. }
  2384. /**
  2385. * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
  2386. * @vsi: the vsi being adjusted
  2387. **/
  2388. void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
  2389. {
  2390. struct i40e_vsi_context ctxt;
  2391. i40e_status ret;
  2392. if ((vsi->info.valid_sections &
  2393. cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
  2394. ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
  2395. I40E_AQ_VSI_PVLAN_EMOD_MASK))
  2396. return; /* already disabled */
  2397. vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
  2398. vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
  2399. I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
  2400. ctxt.seid = vsi->seid;
  2401. ctxt.info = vsi->info;
  2402. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  2403. if (ret) {
  2404. dev_info(&vsi->back->pdev->dev,
  2405. "update vlan stripping failed, err %s aq_err %s\n",
  2406. i40e_stat_str(&vsi->back->hw, ret),
  2407. i40e_aq_str(&vsi->back->hw,
  2408. vsi->back->hw.aq.asq_last_status));
  2409. }
  2410. }
  2411. /**
  2412. * i40e_add_vlan_all_mac - Add a MAC/VLAN filter for each existing MAC address
  2413. * @vsi: the vsi being configured
  2414. * @vid: vlan id to be added (0 = untagged only , -1 = any)
  2415. *
  2416. * This is a helper function for adding a new MAC/VLAN filter with the
  2417. * specified VLAN for each existing MAC address already in the hash table.
  2418. * This function does *not* perform any accounting to update filters based on
  2419. * VLAN mode.
  2420. *
  2421. * NOTE: this function expects to be called while under the
  2422. * mac_filter_hash_lock
  2423. **/
  2424. int i40e_add_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
  2425. {
  2426. struct i40e_mac_filter *f, *add_f;
  2427. struct hlist_node *h;
  2428. int bkt;
  2429. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  2430. if (f->state == I40E_FILTER_REMOVE)
  2431. continue;
  2432. add_f = i40e_add_filter(vsi, f->macaddr, vid);
  2433. if (!add_f) {
  2434. dev_info(&vsi->back->pdev->dev,
  2435. "Could not add vlan filter %d for %pM\n",
  2436. vid, f->macaddr);
  2437. return -ENOMEM;
  2438. }
  2439. }
  2440. return 0;
  2441. }
  2442. /**
  2443. * i40e_vsi_add_vlan - Add VSI membership for given VLAN
  2444. * @vsi: the VSI being configured
  2445. * @vid: VLAN id to be added
  2446. **/
  2447. int i40e_vsi_add_vlan(struct i40e_vsi *vsi, u16 vid)
  2448. {
  2449. int err;
  2450. if (vsi->info.pvid)
  2451. return -EINVAL;
  2452. /* The network stack will attempt to add VID=0, with the intention to
  2453. * receive priority tagged packets with a VLAN of 0. Our HW receives
  2454. * these packets by default when configured to receive untagged
  2455. * packets, so we don't need to add a filter for this case.
  2456. * Additionally, HW interprets adding a VID=0 filter as meaning to
  2457. * receive *only* tagged traffic and stops receiving untagged traffic.
  2458. * Thus, we do not want to actually add a filter for VID=0
  2459. */
  2460. if (!vid)
  2461. return 0;
  2462. /* Locked once because all functions invoked below iterates list*/
  2463. spin_lock_bh(&vsi->mac_filter_hash_lock);
  2464. err = i40e_add_vlan_all_mac(vsi, vid);
  2465. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2466. if (err)
  2467. return err;
  2468. /* schedule our worker thread which will take care of
  2469. * applying the new filter changes
  2470. */
  2471. i40e_service_event_schedule(vsi->back);
  2472. return 0;
  2473. }
  2474. /**
  2475. * i40e_rm_vlan_all_mac - Remove MAC/VLAN pair for all MAC with the given VLAN
  2476. * @vsi: the vsi being configured
  2477. * @vid: vlan id to be removed (0 = untagged only , -1 = any)
  2478. *
  2479. * This function should be used to remove all VLAN filters which match the
  2480. * given VID. It does not schedule the service event and does not take the
  2481. * mac_filter_hash_lock so it may be combined with other operations under
  2482. * a single invocation of the mac_filter_hash_lock.
  2483. *
  2484. * NOTE: this function expects to be called while under the
  2485. * mac_filter_hash_lock
  2486. */
  2487. void i40e_rm_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
  2488. {
  2489. struct i40e_mac_filter *f;
  2490. struct hlist_node *h;
  2491. int bkt;
  2492. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  2493. if (f->vlan == vid)
  2494. __i40e_del_filter(vsi, f);
  2495. }
  2496. }
  2497. /**
  2498. * i40e_vsi_kill_vlan - Remove VSI membership for given VLAN
  2499. * @vsi: the VSI being configured
  2500. * @vid: VLAN id to be removed
  2501. **/
  2502. void i40e_vsi_kill_vlan(struct i40e_vsi *vsi, u16 vid)
  2503. {
  2504. if (!vid || vsi->info.pvid)
  2505. return;
  2506. spin_lock_bh(&vsi->mac_filter_hash_lock);
  2507. i40e_rm_vlan_all_mac(vsi, vid);
  2508. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2509. /* schedule our worker thread which will take care of
  2510. * applying the new filter changes
  2511. */
  2512. i40e_service_event_schedule(vsi->back);
  2513. }
  2514. /**
  2515. * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
  2516. * @netdev: network interface to be adjusted
  2517. * @proto: unused protocol value
  2518. * @vid: vlan id to be added
  2519. *
  2520. * net_device_ops implementation for adding vlan ids
  2521. **/
  2522. static int i40e_vlan_rx_add_vid(struct net_device *netdev,
  2523. __always_unused __be16 proto, u16 vid)
  2524. {
  2525. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2526. struct i40e_vsi *vsi = np->vsi;
  2527. int ret = 0;
  2528. if (vid >= VLAN_N_VID)
  2529. return -EINVAL;
  2530. ret = i40e_vsi_add_vlan(vsi, vid);
  2531. if (!ret)
  2532. set_bit(vid, vsi->active_vlans);
  2533. return ret;
  2534. }
  2535. /**
  2536. * i40e_vlan_rx_add_vid_up - Add a vlan id filter to HW offload in UP path
  2537. * @netdev: network interface to be adjusted
  2538. * @proto: unused protocol value
  2539. * @vid: vlan id to be added
  2540. **/
  2541. static void i40e_vlan_rx_add_vid_up(struct net_device *netdev,
  2542. __always_unused __be16 proto, u16 vid)
  2543. {
  2544. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2545. struct i40e_vsi *vsi = np->vsi;
  2546. if (vid >= VLAN_N_VID)
  2547. return;
  2548. set_bit(vid, vsi->active_vlans);
  2549. }
  2550. /**
  2551. * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
  2552. * @netdev: network interface to be adjusted
  2553. * @proto: unused protocol value
  2554. * @vid: vlan id to be removed
  2555. *
  2556. * net_device_ops implementation for removing vlan ids
  2557. **/
  2558. static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
  2559. __always_unused __be16 proto, u16 vid)
  2560. {
  2561. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2562. struct i40e_vsi *vsi = np->vsi;
  2563. /* return code is ignored as there is nothing a user
  2564. * can do about failure to remove and a log message was
  2565. * already printed from the other function
  2566. */
  2567. i40e_vsi_kill_vlan(vsi, vid);
  2568. clear_bit(vid, vsi->active_vlans);
  2569. return 0;
  2570. }
  2571. /**
  2572. * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
  2573. * @vsi: the vsi being brought back up
  2574. **/
  2575. static void i40e_restore_vlan(struct i40e_vsi *vsi)
  2576. {
  2577. u16 vid;
  2578. if (!vsi->netdev)
  2579. return;
  2580. if (vsi->netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
  2581. i40e_vlan_stripping_enable(vsi);
  2582. else
  2583. i40e_vlan_stripping_disable(vsi);
  2584. for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
  2585. i40e_vlan_rx_add_vid_up(vsi->netdev, htons(ETH_P_8021Q),
  2586. vid);
  2587. }
  2588. /**
  2589. * i40e_vsi_add_pvid - Add pvid for the VSI
  2590. * @vsi: the vsi being adjusted
  2591. * @vid: the vlan id to set as a PVID
  2592. **/
  2593. int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
  2594. {
  2595. struct i40e_vsi_context ctxt;
  2596. i40e_status ret;
  2597. vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
  2598. vsi->info.pvid = cpu_to_le16(vid);
  2599. vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
  2600. I40E_AQ_VSI_PVLAN_INSERT_PVID |
  2601. I40E_AQ_VSI_PVLAN_EMOD_STR;
  2602. ctxt.seid = vsi->seid;
  2603. ctxt.info = vsi->info;
  2604. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  2605. if (ret) {
  2606. dev_info(&vsi->back->pdev->dev,
  2607. "add pvid failed, err %s aq_err %s\n",
  2608. i40e_stat_str(&vsi->back->hw, ret),
  2609. i40e_aq_str(&vsi->back->hw,
  2610. vsi->back->hw.aq.asq_last_status));
  2611. return -ENOENT;
  2612. }
  2613. return 0;
  2614. }
  2615. /**
  2616. * i40e_vsi_remove_pvid - Remove the pvid from the VSI
  2617. * @vsi: the vsi being adjusted
  2618. *
  2619. * Just use the vlan_rx_register() service to put it back to normal
  2620. **/
  2621. void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
  2622. {
  2623. i40e_vlan_stripping_disable(vsi);
  2624. vsi->info.pvid = 0;
  2625. }
  2626. /**
  2627. * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
  2628. * @vsi: ptr to the VSI
  2629. *
  2630. * If this function returns with an error, then it's possible one or
  2631. * more of the rings is populated (while the rest are not). It is the
  2632. * callers duty to clean those orphaned rings.
  2633. *
  2634. * Return 0 on success, negative on failure
  2635. **/
  2636. static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
  2637. {
  2638. int i, err = 0;
  2639. for (i = 0; i < vsi->num_queue_pairs && !err; i++)
  2640. err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
  2641. if (!i40e_enabled_xdp_vsi(vsi))
  2642. return err;
  2643. for (i = 0; i < vsi->num_queue_pairs && !err; i++)
  2644. err = i40e_setup_tx_descriptors(vsi->xdp_rings[i]);
  2645. return err;
  2646. }
  2647. /**
  2648. * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
  2649. * @vsi: ptr to the VSI
  2650. *
  2651. * Free VSI's transmit software resources
  2652. **/
  2653. static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
  2654. {
  2655. int i;
  2656. if (vsi->tx_rings) {
  2657. for (i = 0; i < vsi->num_queue_pairs; i++)
  2658. if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
  2659. i40e_free_tx_resources(vsi->tx_rings[i]);
  2660. }
  2661. if (vsi->xdp_rings) {
  2662. for (i = 0; i < vsi->num_queue_pairs; i++)
  2663. if (vsi->xdp_rings[i] && vsi->xdp_rings[i]->desc)
  2664. i40e_free_tx_resources(vsi->xdp_rings[i]);
  2665. }
  2666. }
  2667. /**
  2668. * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
  2669. * @vsi: ptr to the VSI
  2670. *
  2671. * If this function returns with an error, then it's possible one or
  2672. * more of the rings is populated (while the rest are not). It is the
  2673. * callers duty to clean those orphaned rings.
  2674. *
  2675. * Return 0 on success, negative on failure
  2676. **/
  2677. static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
  2678. {
  2679. int i, err = 0;
  2680. for (i = 0; i < vsi->num_queue_pairs && !err; i++)
  2681. err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
  2682. return err;
  2683. }
  2684. /**
  2685. * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
  2686. * @vsi: ptr to the VSI
  2687. *
  2688. * Free all receive software resources
  2689. **/
  2690. static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
  2691. {
  2692. int i;
  2693. if (!vsi->rx_rings)
  2694. return;
  2695. for (i = 0; i < vsi->num_queue_pairs; i++)
  2696. if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
  2697. i40e_free_rx_resources(vsi->rx_rings[i]);
  2698. }
  2699. /**
  2700. * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
  2701. * @ring: The Tx ring to configure
  2702. *
  2703. * This enables/disables XPS for a given Tx descriptor ring
  2704. * based on the TCs enabled for the VSI that ring belongs to.
  2705. **/
  2706. static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
  2707. {
  2708. int cpu;
  2709. if (!ring->q_vector || !ring->netdev || ring->ch)
  2710. return;
  2711. /* We only initialize XPS once, so as not to overwrite user settings */
  2712. if (test_and_set_bit(__I40E_TX_XPS_INIT_DONE, ring->state))
  2713. return;
  2714. cpu = cpumask_local_spread(ring->q_vector->v_idx, -1);
  2715. netif_set_xps_queue(ring->netdev, get_cpu_mask(cpu),
  2716. ring->queue_index);
  2717. }
  2718. /**
  2719. * i40e_configure_tx_ring - Configure a transmit ring context and rest
  2720. * @ring: The Tx ring to configure
  2721. *
  2722. * Configure the Tx descriptor ring in the HMC context.
  2723. **/
  2724. static int i40e_configure_tx_ring(struct i40e_ring *ring)
  2725. {
  2726. struct i40e_vsi *vsi = ring->vsi;
  2727. u16 pf_q = vsi->base_queue + ring->queue_index;
  2728. struct i40e_hw *hw = &vsi->back->hw;
  2729. struct i40e_hmc_obj_txq tx_ctx;
  2730. i40e_status err = 0;
  2731. u32 qtx_ctl = 0;
  2732. /* some ATR related tx ring init */
  2733. if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
  2734. ring->atr_sample_rate = vsi->back->atr_sample_rate;
  2735. ring->atr_count = 0;
  2736. } else {
  2737. ring->atr_sample_rate = 0;
  2738. }
  2739. /* configure XPS */
  2740. i40e_config_xps_tx_ring(ring);
  2741. /* clear the context structure first */
  2742. memset(&tx_ctx, 0, sizeof(tx_ctx));
  2743. tx_ctx.new_context = 1;
  2744. tx_ctx.base = (ring->dma / 128);
  2745. tx_ctx.qlen = ring->count;
  2746. tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
  2747. I40E_FLAG_FD_ATR_ENABLED));
  2748. tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
  2749. /* FDIR VSI tx ring can still use RS bit and writebacks */
  2750. if (vsi->type != I40E_VSI_FDIR)
  2751. tx_ctx.head_wb_ena = 1;
  2752. tx_ctx.head_wb_addr = ring->dma +
  2753. (ring->count * sizeof(struct i40e_tx_desc));
  2754. /* As part of VSI creation/update, FW allocates certain
  2755. * Tx arbitration queue sets for each TC enabled for
  2756. * the VSI. The FW returns the handles to these queue
  2757. * sets as part of the response buffer to Add VSI,
  2758. * Update VSI, etc. AQ commands. It is expected that
  2759. * these queue set handles be associated with the Tx
  2760. * queues by the driver as part of the TX queue context
  2761. * initialization. This has to be done regardless of
  2762. * DCB as by default everything is mapped to TC0.
  2763. */
  2764. if (ring->ch)
  2765. tx_ctx.rdylist =
  2766. le16_to_cpu(ring->ch->info.qs_handle[ring->dcb_tc]);
  2767. else
  2768. tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
  2769. tx_ctx.rdylist_act = 0;
  2770. /* clear the context in the HMC */
  2771. err = i40e_clear_lan_tx_queue_context(hw, pf_q);
  2772. if (err) {
  2773. dev_info(&vsi->back->pdev->dev,
  2774. "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
  2775. ring->queue_index, pf_q, err);
  2776. return -ENOMEM;
  2777. }
  2778. /* set the context in the HMC */
  2779. err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
  2780. if (err) {
  2781. dev_info(&vsi->back->pdev->dev,
  2782. "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
  2783. ring->queue_index, pf_q, err);
  2784. return -ENOMEM;
  2785. }
  2786. /* Now associate this queue with this PCI function */
  2787. if (ring->ch) {
  2788. if (ring->ch->type == I40E_VSI_VMDQ2)
  2789. qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
  2790. else
  2791. return -EINVAL;
  2792. qtx_ctl |= (ring->ch->vsi_number <<
  2793. I40E_QTX_CTL_VFVM_INDX_SHIFT) &
  2794. I40E_QTX_CTL_VFVM_INDX_MASK;
  2795. } else {
  2796. if (vsi->type == I40E_VSI_VMDQ2) {
  2797. qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
  2798. qtx_ctl |= ((vsi->id) << I40E_QTX_CTL_VFVM_INDX_SHIFT) &
  2799. I40E_QTX_CTL_VFVM_INDX_MASK;
  2800. } else {
  2801. qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
  2802. }
  2803. }
  2804. qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
  2805. I40E_QTX_CTL_PF_INDX_MASK);
  2806. wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
  2807. i40e_flush(hw);
  2808. /* cache tail off for easier writes later */
  2809. ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
  2810. return 0;
  2811. }
  2812. /**
  2813. * i40e_configure_rx_ring - Configure a receive ring context
  2814. * @ring: The Rx ring to configure
  2815. *
  2816. * Configure the Rx descriptor ring in the HMC context.
  2817. **/
  2818. static int i40e_configure_rx_ring(struct i40e_ring *ring)
  2819. {
  2820. struct i40e_vsi *vsi = ring->vsi;
  2821. u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
  2822. u16 pf_q = vsi->base_queue + ring->queue_index;
  2823. struct i40e_hw *hw = &vsi->back->hw;
  2824. struct i40e_hmc_obj_rxq rx_ctx;
  2825. i40e_status err = 0;
  2826. bitmap_zero(ring->state, __I40E_RING_STATE_NBITS);
  2827. /* clear the context structure first */
  2828. memset(&rx_ctx, 0, sizeof(rx_ctx));
  2829. ring->rx_buf_len = vsi->rx_buf_len;
  2830. rx_ctx.dbuff = DIV_ROUND_UP(ring->rx_buf_len,
  2831. BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
  2832. rx_ctx.base = (ring->dma / 128);
  2833. rx_ctx.qlen = ring->count;
  2834. /* use 32 byte descriptors */
  2835. rx_ctx.dsize = 1;
  2836. /* descriptor type is always zero
  2837. * rx_ctx.dtype = 0;
  2838. */
  2839. rx_ctx.hsplit_0 = 0;
  2840. rx_ctx.rxmax = min_t(u16, vsi->max_frame, chain_len * ring->rx_buf_len);
  2841. if (hw->revision_id == 0)
  2842. rx_ctx.lrxqthresh = 0;
  2843. else
  2844. rx_ctx.lrxqthresh = 1;
  2845. rx_ctx.crcstrip = 1;
  2846. rx_ctx.l2tsel = 1;
  2847. /* this controls whether VLAN is stripped from inner headers */
  2848. rx_ctx.showiv = 0;
  2849. /* set the prefena field to 1 because the manual says to */
  2850. rx_ctx.prefena = 1;
  2851. /* clear the context in the HMC */
  2852. err = i40e_clear_lan_rx_queue_context(hw, pf_q);
  2853. if (err) {
  2854. dev_info(&vsi->back->pdev->dev,
  2855. "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
  2856. ring->queue_index, pf_q, err);
  2857. return -ENOMEM;
  2858. }
  2859. /* set the context in the HMC */
  2860. err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
  2861. if (err) {
  2862. dev_info(&vsi->back->pdev->dev,
  2863. "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
  2864. ring->queue_index, pf_q, err);
  2865. return -ENOMEM;
  2866. }
  2867. /* configure Rx buffer alignment */
  2868. if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
  2869. clear_ring_build_skb_enabled(ring);
  2870. else
  2871. set_ring_build_skb_enabled(ring);
  2872. /* cache tail for quicker writes, and clear the reg before use */
  2873. ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
  2874. writel(0, ring->tail);
  2875. i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
  2876. return 0;
  2877. }
  2878. /**
  2879. * i40e_vsi_configure_tx - Configure the VSI for Tx
  2880. * @vsi: VSI structure describing this set of rings and resources
  2881. *
  2882. * Configure the Tx VSI for operation.
  2883. **/
  2884. static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
  2885. {
  2886. int err = 0;
  2887. u16 i;
  2888. for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
  2889. err = i40e_configure_tx_ring(vsi->tx_rings[i]);
  2890. if (!i40e_enabled_xdp_vsi(vsi))
  2891. return err;
  2892. for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
  2893. err = i40e_configure_tx_ring(vsi->xdp_rings[i]);
  2894. return err;
  2895. }
  2896. /**
  2897. * i40e_vsi_configure_rx - Configure the VSI for Rx
  2898. * @vsi: the VSI being configured
  2899. *
  2900. * Configure the Rx VSI for operation.
  2901. **/
  2902. static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
  2903. {
  2904. int err = 0;
  2905. u16 i;
  2906. if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX)) {
  2907. vsi->max_frame = I40E_MAX_RXBUFFER;
  2908. vsi->rx_buf_len = I40E_RXBUFFER_2048;
  2909. #if (PAGE_SIZE < 8192)
  2910. } else if (!I40E_2K_TOO_SMALL_WITH_PADDING &&
  2911. (vsi->netdev->mtu <= ETH_DATA_LEN)) {
  2912. vsi->max_frame = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
  2913. vsi->rx_buf_len = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
  2914. #endif
  2915. } else {
  2916. vsi->max_frame = I40E_MAX_RXBUFFER;
  2917. vsi->rx_buf_len = (PAGE_SIZE < 8192) ? I40E_RXBUFFER_3072 :
  2918. I40E_RXBUFFER_2048;
  2919. }
  2920. /* set up individual rings */
  2921. for (i = 0; i < vsi->num_queue_pairs && !err; i++)
  2922. err = i40e_configure_rx_ring(vsi->rx_rings[i]);
  2923. return err;
  2924. }
  2925. /**
  2926. * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
  2927. * @vsi: ptr to the VSI
  2928. **/
  2929. static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
  2930. {
  2931. struct i40e_ring *tx_ring, *rx_ring;
  2932. u16 qoffset, qcount;
  2933. int i, n;
  2934. if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
  2935. /* Reset the TC information */
  2936. for (i = 0; i < vsi->num_queue_pairs; i++) {
  2937. rx_ring = vsi->rx_rings[i];
  2938. tx_ring = vsi->tx_rings[i];
  2939. rx_ring->dcb_tc = 0;
  2940. tx_ring->dcb_tc = 0;
  2941. }
  2942. return;
  2943. }
  2944. for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
  2945. if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
  2946. continue;
  2947. qoffset = vsi->tc_config.tc_info[n].qoffset;
  2948. qcount = vsi->tc_config.tc_info[n].qcount;
  2949. for (i = qoffset; i < (qoffset + qcount); i++) {
  2950. rx_ring = vsi->rx_rings[i];
  2951. tx_ring = vsi->tx_rings[i];
  2952. rx_ring->dcb_tc = n;
  2953. tx_ring->dcb_tc = n;
  2954. }
  2955. }
  2956. }
  2957. /**
  2958. * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
  2959. * @vsi: ptr to the VSI
  2960. **/
  2961. static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
  2962. {
  2963. if (vsi->netdev)
  2964. i40e_set_rx_mode(vsi->netdev);
  2965. }
  2966. /**
  2967. * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
  2968. * @vsi: Pointer to the targeted VSI
  2969. *
  2970. * This function replays the hlist on the hw where all the SB Flow Director
  2971. * filters were saved.
  2972. **/
  2973. static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
  2974. {
  2975. struct i40e_fdir_filter *filter;
  2976. struct i40e_pf *pf = vsi->back;
  2977. struct hlist_node *node;
  2978. if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
  2979. return;
  2980. /* Reset FDir counters as we're replaying all existing filters */
  2981. pf->fd_tcp4_filter_cnt = 0;
  2982. pf->fd_udp4_filter_cnt = 0;
  2983. pf->fd_sctp4_filter_cnt = 0;
  2984. pf->fd_ip4_filter_cnt = 0;
  2985. hlist_for_each_entry_safe(filter, node,
  2986. &pf->fdir_filter_list, fdir_node) {
  2987. i40e_add_del_fdir(vsi, filter, true);
  2988. }
  2989. }
  2990. /**
  2991. * i40e_vsi_configure - Set up the VSI for action
  2992. * @vsi: the VSI being configured
  2993. **/
  2994. static int i40e_vsi_configure(struct i40e_vsi *vsi)
  2995. {
  2996. int err;
  2997. i40e_set_vsi_rx_mode(vsi);
  2998. i40e_restore_vlan(vsi);
  2999. i40e_vsi_config_dcb_rings(vsi);
  3000. err = i40e_vsi_configure_tx(vsi);
  3001. if (!err)
  3002. err = i40e_vsi_configure_rx(vsi);
  3003. return err;
  3004. }
  3005. /**
  3006. * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
  3007. * @vsi: the VSI being configured
  3008. **/
  3009. static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
  3010. {
  3011. bool has_xdp = i40e_enabled_xdp_vsi(vsi);
  3012. struct i40e_pf *pf = vsi->back;
  3013. struct i40e_hw *hw = &pf->hw;
  3014. u16 vector;
  3015. int i, q;
  3016. u32 qp;
  3017. /* The interrupt indexing is offset by 1 in the PFINT_ITRn
  3018. * and PFINT_LNKLSTn registers, e.g.:
  3019. * PFINT_ITRn[0..n-1] gets msix-1..msix-n (qpair interrupts)
  3020. */
  3021. qp = vsi->base_queue;
  3022. vector = vsi->base_vector;
  3023. for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
  3024. struct i40e_q_vector *q_vector = vsi->q_vectors[i];
  3025. q_vector->rx.next_update = jiffies + 1;
  3026. q_vector->rx.target_itr =
  3027. ITR_TO_REG(vsi->rx_rings[i]->itr_setting);
  3028. wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
  3029. q_vector->rx.target_itr);
  3030. q_vector->rx.current_itr = q_vector->rx.target_itr;
  3031. q_vector->tx.next_update = jiffies + 1;
  3032. q_vector->tx.target_itr =
  3033. ITR_TO_REG(vsi->tx_rings[i]->itr_setting);
  3034. wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
  3035. q_vector->tx.target_itr);
  3036. q_vector->tx.current_itr = q_vector->tx.target_itr;
  3037. wr32(hw, I40E_PFINT_RATEN(vector - 1),
  3038. i40e_intrl_usec_to_reg(vsi->int_rate_limit));
  3039. /* Linked list for the queuepairs assigned to this vector */
  3040. wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
  3041. for (q = 0; q < q_vector->num_ringpairs; q++) {
  3042. u32 nextqp = has_xdp ? qp + vsi->alloc_queue_pairs : qp;
  3043. u32 val;
  3044. val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
  3045. (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
  3046. (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
  3047. (nextqp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) |
  3048. (I40E_QUEUE_TYPE_TX <<
  3049. I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
  3050. wr32(hw, I40E_QINT_RQCTL(qp), val);
  3051. if (has_xdp) {
  3052. val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  3053. (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
  3054. (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
  3055. (qp << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
  3056. (I40E_QUEUE_TYPE_TX <<
  3057. I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
  3058. wr32(hw, I40E_QINT_TQCTL(nextqp), val);
  3059. }
  3060. val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  3061. (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
  3062. (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
  3063. ((qp + 1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
  3064. (I40E_QUEUE_TYPE_RX <<
  3065. I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
  3066. /* Terminate the linked list */
  3067. if (q == (q_vector->num_ringpairs - 1))
  3068. val |= (I40E_QUEUE_END_OF_LIST <<
  3069. I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
  3070. wr32(hw, I40E_QINT_TQCTL(qp), val);
  3071. qp++;
  3072. }
  3073. }
  3074. i40e_flush(hw);
  3075. }
  3076. /**
  3077. * i40e_enable_misc_int_causes - enable the non-queue interrupts
  3078. * @pf: pointer to private device data structure
  3079. **/
  3080. static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
  3081. {
  3082. struct i40e_hw *hw = &pf->hw;
  3083. u32 val;
  3084. /* clear things first */
  3085. wr32(hw, I40E_PFINT_ICR0_ENA, 0); /* disable all */
  3086. rd32(hw, I40E_PFINT_ICR0); /* read to clear */
  3087. val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK |
  3088. I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK |
  3089. I40E_PFINT_ICR0_ENA_GRST_MASK |
  3090. I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
  3091. I40E_PFINT_ICR0_ENA_GPIO_MASK |
  3092. I40E_PFINT_ICR0_ENA_HMC_ERR_MASK |
  3093. I40E_PFINT_ICR0_ENA_VFLR_MASK |
  3094. I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
  3095. if (pf->flags & I40E_FLAG_IWARP_ENABLED)
  3096. val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
  3097. if (pf->flags & I40E_FLAG_PTP)
  3098. val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
  3099. wr32(hw, I40E_PFINT_ICR0_ENA, val);
  3100. /* SW_ITR_IDX = 0, but don't change INTENA */
  3101. wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
  3102. I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
  3103. /* OTHER_ITR_IDX = 0 */
  3104. wr32(hw, I40E_PFINT_STAT_CTL0, 0);
  3105. }
  3106. /**
  3107. * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
  3108. * @vsi: the VSI being configured
  3109. **/
  3110. static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
  3111. {
  3112. u32 nextqp = i40e_enabled_xdp_vsi(vsi) ? vsi->alloc_queue_pairs : 0;
  3113. struct i40e_q_vector *q_vector = vsi->q_vectors[0];
  3114. struct i40e_pf *pf = vsi->back;
  3115. struct i40e_hw *hw = &pf->hw;
  3116. u32 val;
  3117. /* set the ITR configuration */
  3118. q_vector->rx.next_update = jiffies + 1;
  3119. q_vector->rx.target_itr = ITR_TO_REG(vsi->rx_rings[0]->itr_setting);
  3120. wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.target_itr);
  3121. q_vector->rx.current_itr = q_vector->rx.target_itr;
  3122. q_vector->tx.next_update = jiffies + 1;
  3123. q_vector->tx.target_itr = ITR_TO_REG(vsi->tx_rings[0]->itr_setting);
  3124. wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.target_itr);
  3125. q_vector->tx.current_itr = q_vector->tx.target_itr;
  3126. i40e_enable_misc_int_causes(pf);
  3127. /* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
  3128. wr32(hw, I40E_PFINT_LNKLST0, 0);
  3129. /* Associate the queue pair to the vector and enable the queue int */
  3130. val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
  3131. (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
  3132. (nextqp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
  3133. (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
  3134. wr32(hw, I40E_QINT_RQCTL(0), val);
  3135. if (i40e_enabled_xdp_vsi(vsi)) {
  3136. val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  3137. (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT)|
  3138. (I40E_QUEUE_TYPE_TX
  3139. << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
  3140. wr32(hw, I40E_QINT_TQCTL(nextqp), val);
  3141. }
  3142. val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  3143. (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
  3144. (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
  3145. wr32(hw, I40E_QINT_TQCTL(0), val);
  3146. i40e_flush(hw);
  3147. }
  3148. /**
  3149. * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
  3150. * @pf: board private structure
  3151. **/
  3152. void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
  3153. {
  3154. struct i40e_hw *hw = &pf->hw;
  3155. wr32(hw, I40E_PFINT_DYN_CTL0,
  3156. I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
  3157. i40e_flush(hw);
  3158. }
  3159. /**
  3160. * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
  3161. * @pf: board private structure
  3162. **/
  3163. void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf)
  3164. {
  3165. struct i40e_hw *hw = &pf->hw;
  3166. u32 val;
  3167. val = I40E_PFINT_DYN_CTL0_INTENA_MASK |
  3168. I40E_PFINT_DYN_CTL0_CLEARPBA_MASK |
  3169. (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
  3170. wr32(hw, I40E_PFINT_DYN_CTL0, val);
  3171. i40e_flush(hw);
  3172. }
  3173. /**
  3174. * i40e_msix_clean_rings - MSIX mode Interrupt Handler
  3175. * @irq: interrupt number
  3176. * @data: pointer to a q_vector
  3177. **/
  3178. static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
  3179. {
  3180. struct i40e_q_vector *q_vector = data;
  3181. if (!q_vector->tx.ring && !q_vector->rx.ring)
  3182. return IRQ_HANDLED;
  3183. napi_schedule_irqoff(&q_vector->napi);
  3184. return IRQ_HANDLED;
  3185. }
  3186. /**
  3187. * i40e_irq_affinity_notify - Callback for affinity changes
  3188. * @notify: context as to what irq was changed
  3189. * @mask: the new affinity mask
  3190. *
  3191. * This is a callback function used by the irq_set_affinity_notifier function
  3192. * so that we may register to receive changes to the irq affinity masks.
  3193. **/
  3194. static void i40e_irq_affinity_notify(struct irq_affinity_notify *notify,
  3195. const cpumask_t *mask)
  3196. {
  3197. struct i40e_q_vector *q_vector =
  3198. container_of(notify, struct i40e_q_vector, affinity_notify);
  3199. cpumask_copy(&q_vector->affinity_mask, mask);
  3200. }
  3201. /**
  3202. * i40e_irq_affinity_release - Callback for affinity notifier release
  3203. * @ref: internal core kernel usage
  3204. *
  3205. * This is a callback function used by the irq_set_affinity_notifier function
  3206. * to inform the current notification subscriber that they will no longer
  3207. * receive notifications.
  3208. **/
  3209. static void i40e_irq_affinity_release(struct kref *ref) {}
  3210. /**
  3211. * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
  3212. * @vsi: the VSI being configured
  3213. * @basename: name for the vector
  3214. *
  3215. * Allocates MSI-X vectors and requests interrupts from the kernel.
  3216. **/
  3217. static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
  3218. {
  3219. int q_vectors = vsi->num_q_vectors;
  3220. struct i40e_pf *pf = vsi->back;
  3221. int base = vsi->base_vector;
  3222. int rx_int_idx = 0;
  3223. int tx_int_idx = 0;
  3224. int vector, err;
  3225. int irq_num;
  3226. int cpu;
  3227. for (vector = 0; vector < q_vectors; vector++) {
  3228. struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
  3229. irq_num = pf->msix_entries[base + vector].vector;
  3230. if (q_vector->tx.ring && q_vector->rx.ring) {
  3231. snprintf(q_vector->name, sizeof(q_vector->name) - 1,
  3232. "%s-%s-%d", basename, "TxRx", rx_int_idx++);
  3233. tx_int_idx++;
  3234. } else if (q_vector->rx.ring) {
  3235. snprintf(q_vector->name, sizeof(q_vector->name) - 1,
  3236. "%s-%s-%d", basename, "rx", rx_int_idx++);
  3237. } else if (q_vector->tx.ring) {
  3238. snprintf(q_vector->name, sizeof(q_vector->name) - 1,
  3239. "%s-%s-%d", basename, "tx", tx_int_idx++);
  3240. } else {
  3241. /* skip this unused q_vector */
  3242. continue;
  3243. }
  3244. err = request_irq(irq_num,
  3245. vsi->irq_handler,
  3246. 0,
  3247. q_vector->name,
  3248. q_vector);
  3249. if (err) {
  3250. dev_info(&pf->pdev->dev,
  3251. "MSIX request_irq failed, error: %d\n", err);
  3252. goto free_queue_irqs;
  3253. }
  3254. /* register for affinity change notifications */
  3255. q_vector->affinity_notify.notify = i40e_irq_affinity_notify;
  3256. q_vector->affinity_notify.release = i40e_irq_affinity_release;
  3257. irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
  3258. /* Spread affinity hints out across online CPUs.
  3259. *
  3260. * get_cpu_mask returns a static constant mask with
  3261. * a permanent lifetime so it's ok to pass to
  3262. * irq_set_affinity_hint without making a copy.
  3263. */
  3264. cpu = cpumask_local_spread(q_vector->v_idx, -1);
  3265. irq_set_affinity_hint(irq_num, get_cpu_mask(cpu));
  3266. }
  3267. vsi->irqs_ready = true;
  3268. return 0;
  3269. free_queue_irqs:
  3270. while (vector) {
  3271. vector--;
  3272. irq_num = pf->msix_entries[base + vector].vector;
  3273. irq_set_affinity_notifier(irq_num, NULL);
  3274. irq_set_affinity_hint(irq_num, NULL);
  3275. free_irq(irq_num, &vsi->q_vectors[vector]);
  3276. }
  3277. return err;
  3278. }
  3279. /**
  3280. * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
  3281. * @vsi: the VSI being un-configured
  3282. **/
  3283. static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
  3284. {
  3285. struct i40e_pf *pf = vsi->back;
  3286. struct i40e_hw *hw = &pf->hw;
  3287. int base = vsi->base_vector;
  3288. int i;
  3289. /* disable interrupt causation from each queue */
  3290. for (i = 0; i < vsi->num_queue_pairs; i++) {
  3291. u32 val;
  3292. val = rd32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx));
  3293. val &= ~I40E_QINT_TQCTL_CAUSE_ENA_MASK;
  3294. wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), val);
  3295. val = rd32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx));
  3296. val &= ~I40E_QINT_RQCTL_CAUSE_ENA_MASK;
  3297. wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), val);
  3298. if (!i40e_enabled_xdp_vsi(vsi))
  3299. continue;
  3300. wr32(hw, I40E_QINT_TQCTL(vsi->xdp_rings[i]->reg_idx), 0);
  3301. }
  3302. /* disable each interrupt */
  3303. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  3304. for (i = vsi->base_vector;
  3305. i < (vsi->num_q_vectors + vsi->base_vector); i++)
  3306. wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
  3307. i40e_flush(hw);
  3308. for (i = 0; i < vsi->num_q_vectors; i++)
  3309. synchronize_irq(pf->msix_entries[i + base].vector);
  3310. } else {
  3311. /* Legacy and MSI mode - this stops all interrupt handling */
  3312. wr32(hw, I40E_PFINT_ICR0_ENA, 0);
  3313. wr32(hw, I40E_PFINT_DYN_CTL0, 0);
  3314. i40e_flush(hw);
  3315. synchronize_irq(pf->pdev->irq);
  3316. }
  3317. }
  3318. /**
  3319. * i40e_vsi_enable_irq - Enable IRQ for the given VSI
  3320. * @vsi: the VSI being configured
  3321. **/
  3322. static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
  3323. {
  3324. struct i40e_pf *pf = vsi->back;
  3325. int i;
  3326. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  3327. for (i = 0; i < vsi->num_q_vectors; i++)
  3328. i40e_irq_dynamic_enable(vsi, i);
  3329. } else {
  3330. i40e_irq_dynamic_enable_icr0(pf);
  3331. }
  3332. i40e_flush(&pf->hw);
  3333. return 0;
  3334. }
  3335. /**
  3336. * i40e_free_misc_vector - Free the vector that handles non-queue events
  3337. * @pf: board private structure
  3338. **/
  3339. static void i40e_free_misc_vector(struct i40e_pf *pf)
  3340. {
  3341. /* Disable ICR 0 */
  3342. wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
  3343. i40e_flush(&pf->hw);
  3344. if (pf->flags & I40E_FLAG_MSIX_ENABLED && pf->msix_entries) {
  3345. synchronize_irq(pf->msix_entries[0].vector);
  3346. free_irq(pf->msix_entries[0].vector, pf);
  3347. clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
  3348. }
  3349. }
  3350. /**
  3351. * i40e_intr - MSI/Legacy and non-queue interrupt handler
  3352. * @irq: interrupt number
  3353. * @data: pointer to a q_vector
  3354. *
  3355. * This is the handler used for all MSI/Legacy interrupts, and deals
  3356. * with both queue and non-queue interrupts. This is also used in
  3357. * MSIX mode to handle the non-queue interrupts.
  3358. **/
  3359. static irqreturn_t i40e_intr(int irq, void *data)
  3360. {
  3361. struct i40e_pf *pf = (struct i40e_pf *)data;
  3362. struct i40e_hw *hw = &pf->hw;
  3363. irqreturn_t ret = IRQ_NONE;
  3364. u32 icr0, icr0_remaining;
  3365. u32 val, ena_mask;
  3366. icr0 = rd32(hw, I40E_PFINT_ICR0);
  3367. ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
  3368. /* if sharing a legacy IRQ, we might get called w/o an intr pending */
  3369. if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
  3370. goto enable_intr;
  3371. /* if interrupt but no bits showing, must be SWINT */
  3372. if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
  3373. (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
  3374. pf->sw_int_count++;
  3375. if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
  3376. (icr0 & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
  3377. ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
  3378. dev_dbg(&pf->pdev->dev, "cleared PE_CRITERR\n");
  3379. set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
  3380. }
  3381. /* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
  3382. if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
  3383. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  3384. struct i40e_q_vector *q_vector = vsi->q_vectors[0];
  3385. /* We do not have a way to disarm Queue causes while leaving
  3386. * interrupt enabled for all other causes, ideally
  3387. * interrupt should be disabled while we are in NAPI but
  3388. * this is not a performance path and napi_schedule()
  3389. * can deal with rescheduling.
  3390. */
  3391. if (!test_bit(__I40E_DOWN, pf->state))
  3392. napi_schedule_irqoff(&q_vector->napi);
  3393. }
  3394. if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
  3395. ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
  3396. set_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
  3397. i40e_debug(&pf->hw, I40E_DEBUG_NVM, "AdminQ event\n");
  3398. }
  3399. if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
  3400. ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
  3401. set_bit(__I40E_MDD_EVENT_PENDING, pf->state);
  3402. }
  3403. if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
  3404. ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
  3405. set_bit(__I40E_VFLR_EVENT_PENDING, pf->state);
  3406. }
  3407. if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
  3408. if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
  3409. set_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
  3410. ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
  3411. val = rd32(hw, I40E_GLGEN_RSTAT);
  3412. val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
  3413. >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
  3414. if (val == I40E_RESET_CORER) {
  3415. pf->corer_count++;
  3416. } else if (val == I40E_RESET_GLOBR) {
  3417. pf->globr_count++;
  3418. } else if (val == I40E_RESET_EMPR) {
  3419. pf->empr_count++;
  3420. set_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state);
  3421. }
  3422. }
  3423. if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
  3424. icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
  3425. dev_info(&pf->pdev->dev, "HMC error interrupt\n");
  3426. dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
  3427. rd32(hw, I40E_PFHMC_ERRORINFO),
  3428. rd32(hw, I40E_PFHMC_ERRORDATA));
  3429. }
  3430. if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
  3431. u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
  3432. if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK) {
  3433. icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
  3434. i40e_ptp_tx_hwtstamp(pf);
  3435. }
  3436. }
  3437. /* If a critical error is pending we have no choice but to reset the
  3438. * device.
  3439. * Report and mask out any remaining unexpected interrupts.
  3440. */
  3441. icr0_remaining = icr0 & ena_mask;
  3442. if (icr0_remaining) {
  3443. dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
  3444. icr0_remaining);
  3445. if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
  3446. (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
  3447. (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
  3448. dev_info(&pf->pdev->dev, "device will be reset\n");
  3449. set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
  3450. i40e_service_event_schedule(pf);
  3451. }
  3452. ena_mask &= ~icr0_remaining;
  3453. }
  3454. ret = IRQ_HANDLED;
  3455. enable_intr:
  3456. /* re-enable interrupt causes */
  3457. wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
  3458. if (!test_bit(__I40E_DOWN, pf->state)) {
  3459. i40e_service_event_schedule(pf);
  3460. i40e_irq_dynamic_enable_icr0(pf);
  3461. }
  3462. return ret;
  3463. }
  3464. /**
  3465. * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
  3466. * @tx_ring: tx ring to clean
  3467. * @budget: how many cleans we're allowed
  3468. *
  3469. * Returns true if there's any budget left (e.g. the clean is finished)
  3470. **/
  3471. static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
  3472. {
  3473. struct i40e_vsi *vsi = tx_ring->vsi;
  3474. u16 i = tx_ring->next_to_clean;
  3475. struct i40e_tx_buffer *tx_buf;
  3476. struct i40e_tx_desc *tx_desc;
  3477. tx_buf = &tx_ring->tx_bi[i];
  3478. tx_desc = I40E_TX_DESC(tx_ring, i);
  3479. i -= tx_ring->count;
  3480. do {
  3481. struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
  3482. /* if next_to_watch is not set then there is no work pending */
  3483. if (!eop_desc)
  3484. break;
  3485. /* prevent any other reads prior to eop_desc */
  3486. smp_rmb();
  3487. /* if the descriptor isn't done, no work yet to do */
  3488. if (!(eop_desc->cmd_type_offset_bsz &
  3489. cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
  3490. break;
  3491. /* clear next_to_watch to prevent false hangs */
  3492. tx_buf->next_to_watch = NULL;
  3493. tx_desc->buffer_addr = 0;
  3494. tx_desc->cmd_type_offset_bsz = 0;
  3495. /* move past filter desc */
  3496. tx_buf++;
  3497. tx_desc++;
  3498. i++;
  3499. if (unlikely(!i)) {
  3500. i -= tx_ring->count;
  3501. tx_buf = tx_ring->tx_bi;
  3502. tx_desc = I40E_TX_DESC(tx_ring, 0);
  3503. }
  3504. /* unmap skb header data */
  3505. dma_unmap_single(tx_ring->dev,
  3506. dma_unmap_addr(tx_buf, dma),
  3507. dma_unmap_len(tx_buf, len),
  3508. DMA_TO_DEVICE);
  3509. if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
  3510. kfree(tx_buf->raw_buf);
  3511. tx_buf->raw_buf = NULL;
  3512. tx_buf->tx_flags = 0;
  3513. tx_buf->next_to_watch = NULL;
  3514. dma_unmap_len_set(tx_buf, len, 0);
  3515. tx_desc->buffer_addr = 0;
  3516. tx_desc->cmd_type_offset_bsz = 0;
  3517. /* move us past the eop_desc for start of next FD desc */
  3518. tx_buf++;
  3519. tx_desc++;
  3520. i++;
  3521. if (unlikely(!i)) {
  3522. i -= tx_ring->count;
  3523. tx_buf = tx_ring->tx_bi;
  3524. tx_desc = I40E_TX_DESC(tx_ring, 0);
  3525. }
  3526. /* update budget accounting */
  3527. budget--;
  3528. } while (likely(budget));
  3529. i += tx_ring->count;
  3530. tx_ring->next_to_clean = i;
  3531. if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED)
  3532. i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
  3533. return budget > 0;
  3534. }
  3535. /**
  3536. * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
  3537. * @irq: interrupt number
  3538. * @data: pointer to a q_vector
  3539. **/
  3540. static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
  3541. {
  3542. struct i40e_q_vector *q_vector = data;
  3543. struct i40e_vsi *vsi;
  3544. if (!q_vector->tx.ring)
  3545. return IRQ_HANDLED;
  3546. vsi = q_vector->tx.ring->vsi;
  3547. i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
  3548. return IRQ_HANDLED;
  3549. }
  3550. /**
  3551. * i40e_map_vector_to_qp - Assigns the queue pair to the vector
  3552. * @vsi: the VSI being configured
  3553. * @v_idx: vector index
  3554. * @qp_idx: queue pair index
  3555. **/
  3556. static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
  3557. {
  3558. struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
  3559. struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
  3560. struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
  3561. tx_ring->q_vector = q_vector;
  3562. tx_ring->next = q_vector->tx.ring;
  3563. q_vector->tx.ring = tx_ring;
  3564. q_vector->tx.count++;
  3565. /* Place XDP Tx ring in the same q_vector ring list as regular Tx */
  3566. if (i40e_enabled_xdp_vsi(vsi)) {
  3567. struct i40e_ring *xdp_ring = vsi->xdp_rings[qp_idx];
  3568. xdp_ring->q_vector = q_vector;
  3569. xdp_ring->next = q_vector->tx.ring;
  3570. q_vector->tx.ring = xdp_ring;
  3571. q_vector->tx.count++;
  3572. }
  3573. rx_ring->q_vector = q_vector;
  3574. rx_ring->next = q_vector->rx.ring;
  3575. q_vector->rx.ring = rx_ring;
  3576. q_vector->rx.count++;
  3577. }
  3578. /**
  3579. * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
  3580. * @vsi: the VSI being configured
  3581. *
  3582. * This function maps descriptor rings to the queue-specific vectors
  3583. * we were allotted through the MSI-X enabling code. Ideally, we'd have
  3584. * one vector per queue pair, but on a constrained vector budget, we
  3585. * group the queue pairs as "efficiently" as possible.
  3586. **/
  3587. static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
  3588. {
  3589. int qp_remaining = vsi->num_queue_pairs;
  3590. int q_vectors = vsi->num_q_vectors;
  3591. int num_ringpairs;
  3592. int v_start = 0;
  3593. int qp_idx = 0;
  3594. /* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
  3595. * group them so there are multiple queues per vector.
  3596. * It is also important to go through all the vectors available to be
  3597. * sure that if we don't use all the vectors, that the remaining vectors
  3598. * are cleared. This is especially important when decreasing the
  3599. * number of queues in use.
  3600. */
  3601. for (; v_start < q_vectors; v_start++) {
  3602. struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
  3603. num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
  3604. q_vector->num_ringpairs = num_ringpairs;
  3605. q_vector->reg_idx = q_vector->v_idx + vsi->base_vector - 1;
  3606. q_vector->rx.count = 0;
  3607. q_vector->tx.count = 0;
  3608. q_vector->rx.ring = NULL;
  3609. q_vector->tx.ring = NULL;
  3610. while (num_ringpairs--) {
  3611. i40e_map_vector_to_qp(vsi, v_start, qp_idx);
  3612. qp_idx++;
  3613. qp_remaining--;
  3614. }
  3615. }
  3616. }
  3617. /**
  3618. * i40e_vsi_request_irq - Request IRQ from the OS
  3619. * @vsi: the VSI being configured
  3620. * @basename: name for the vector
  3621. **/
  3622. static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
  3623. {
  3624. struct i40e_pf *pf = vsi->back;
  3625. int err;
  3626. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  3627. err = i40e_vsi_request_irq_msix(vsi, basename);
  3628. else if (pf->flags & I40E_FLAG_MSI_ENABLED)
  3629. err = request_irq(pf->pdev->irq, i40e_intr, 0,
  3630. pf->int_name, pf);
  3631. else
  3632. err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
  3633. pf->int_name, pf);
  3634. if (err)
  3635. dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
  3636. return err;
  3637. }
  3638. #ifdef CONFIG_NET_POLL_CONTROLLER
  3639. /**
  3640. * i40e_netpoll - A Polling 'interrupt' handler
  3641. * @netdev: network interface device structure
  3642. *
  3643. * This is used by netconsole to send skbs without having to re-enable
  3644. * interrupts. It's not called while the normal interrupt routine is executing.
  3645. **/
  3646. static void i40e_netpoll(struct net_device *netdev)
  3647. {
  3648. struct i40e_netdev_priv *np = netdev_priv(netdev);
  3649. struct i40e_vsi *vsi = np->vsi;
  3650. struct i40e_pf *pf = vsi->back;
  3651. int i;
  3652. /* if interface is down do nothing */
  3653. if (test_bit(__I40E_VSI_DOWN, vsi->state))
  3654. return;
  3655. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  3656. for (i = 0; i < vsi->num_q_vectors; i++)
  3657. i40e_msix_clean_rings(0, vsi->q_vectors[i]);
  3658. } else {
  3659. i40e_intr(pf->pdev->irq, netdev);
  3660. }
  3661. }
  3662. #endif
  3663. #define I40E_QTX_ENA_WAIT_COUNT 50
  3664. /**
  3665. * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
  3666. * @pf: the PF being configured
  3667. * @pf_q: the PF queue
  3668. * @enable: enable or disable state of the queue
  3669. *
  3670. * This routine will wait for the given Tx queue of the PF to reach the
  3671. * enabled or disabled state.
  3672. * Returns -ETIMEDOUT in case of failing to reach the requested state after
  3673. * multiple retries; else will return 0 in case of success.
  3674. **/
  3675. static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
  3676. {
  3677. int i;
  3678. u32 tx_reg;
  3679. for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
  3680. tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
  3681. if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
  3682. break;
  3683. usleep_range(10, 20);
  3684. }
  3685. if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
  3686. return -ETIMEDOUT;
  3687. return 0;
  3688. }
  3689. /**
  3690. * i40e_control_tx_q - Start or stop a particular Tx queue
  3691. * @pf: the PF structure
  3692. * @pf_q: the PF queue to configure
  3693. * @enable: start or stop the queue
  3694. *
  3695. * This function enables or disables a single queue. Note that any delay
  3696. * required after the operation is expected to be handled by the caller of
  3697. * this function.
  3698. **/
  3699. static void i40e_control_tx_q(struct i40e_pf *pf, int pf_q, bool enable)
  3700. {
  3701. struct i40e_hw *hw = &pf->hw;
  3702. u32 tx_reg;
  3703. int i;
  3704. /* warn the TX unit of coming changes */
  3705. i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
  3706. if (!enable)
  3707. usleep_range(10, 20);
  3708. for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
  3709. tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
  3710. if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
  3711. ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
  3712. break;
  3713. usleep_range(1000, 2000);
  3714. }
  3715. /* Skip if the queue is already in the requested state */
  3716. if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
  3717. return;
  3718. /* turn on/off the queue */
  3719. if (enable) {
  3720. wr32(hw, I40E_QTX_HEAD(pf_q), 0);
  3721. tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
  3722. } else {
  3723. tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
  3724. }
  3725. wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
  3726. }
  3727. /**
  3728. * i40e_control_wait_tx_q - Start/stop Tx queue and wait for completion
  3729. * @seid: VSI SEID
  3730. * @pf: the PF structure
  3731. * @pf_q: the PF queue to configure
  3732. * @is_xdp: true if the queue is used for XDP
  3733. * @enable: start or stop the queue
  3734. **/
  3735. int i40e_control_wait_tx_q(int seid, struct i40e_pf *pf, int pf_q,
  3736. bool is_xdp, bool enable)
  3737. {
  3738. int ret;
  3739. i40e_control_tx_q(pf, pf_q, enable);
  3740. /* wait for the change to finish */
  3741. ret = i40e_pf_txq_wait(pf, pf_q, enable);
  3742. if (ret) {
  3743. dev_info(&pf->pdev->dev,
  3744. "VSI seid %d %sTx ring %d %sable timeout\n",
  3745. seid, (is_xdp ? "XDP " : ""), pf_q,
  3746. (enable ? "en" : "dis"));
  3747. }
  3748. return ret;
  3749. }
  3750. /**
  3751. * i40e_vsi_control_tx - Start or stop a VSI's rings
  3752. * @vsi: the VSI being configured
  3753. * @enable: start or stop the rings
  3754. **/
  3755. static int i40e_vsi_control_tx(struct i40e_vsi *vsi, bool enable)
  3756. {
  3757. struct i40e_pf *pf = vsi->back;
  3758. int i, pf_q, ret = 0;
  3759. pf_q = vsi->base_queue;
  3760. for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
  3761. ret = i40e_control_wait_tx_q(vsi->seid, pf,
  3762. pf_q,
  3763. false /*is xdp*/, enable);
  3764. if (ret)
  3765. break;
  3766. if (!i40e_enabled_xdp_vsi(vsi))
  3767. continue;
  3768. ret = i40e_control_wait_tx_q(vsi->seid, pf,
  3769. pf_q + vsi->alloc_queue_pairs,
  3770. true /*is xdp*/, enable);
  3771. if (ret)
  3772. break;
  3773. }
  3774. return ret;
  3775. }
  3776. /**
  3777. * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
  3778. * @pf: the PF being configured
  3779. * @pf_q: the PF queue
  3780. * @enable: enable or disable state of the queue
  3781. *
  3782. * This routine will wait for the given Rx queue of the PF to reach the
  3783. * enabled or disabled state.
  3784. * Returns -ETIMEDOUT in case of failing to reach the requested state after
  3785. * multiple retries; else will return 0 in case of success.
  3786. **/
  3787. static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
  3788. {
  3789. int i;
  3790. u32 rx_reg;
  3791. for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
  3792. rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
  3793. if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
  3794. break;
  3795. usleep_range(10, 20);
  3796. }
  3797. if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
  3798. return -ETIMEDOUT;
  3799. return 0;
  3800. }
  3801. /**
  3802. * i40e_control_rx_q - Start or stop a particular Rx queue
  3803. * @pf: the PF structure
  3804. * @pf_q: the PF queue to configure
  3805. * @enable: start or stop the queue
  3806. *
  3807. * This function enables or disables a single queue. Note that
  3808. * any delay required after the operation is expected to be
  3809. * handled by the caller of this function.
  3810. **/
  3811. static void i40e_control_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
  3812. {
  3813. struct i40e_hw *hw = &pf->hw;
  3814. u32 rx_reg;
  3815. int i;
  3816. for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
  3817. rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
  3818. if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
  3819. ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
  3820. break;
  3821. usleep_range(1000, 2000);
  3822. }
  3823. /* Skip if the queue is already in the requested state */
  3824. if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
  3825. return;
  3826. /* turn on/off the queue */
  3827. if (enable)
  3828. rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
  3829. else
  3830. rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
  3831. wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
  3832. }
  3833. /**
  3834. * i40e_control_wait_rx_q
  3835. * @pf: the PF structure
  3836. * @pf_q: queue being configured
  3837. * @enable: start or stop the rings
  3838. *
  3839. * This function enables or disables a single queue along with waiting
  3840. * for the change to finish. The caller of this function should handle
  3841. * the delays needed in the case of disabling queues.
  3842. **/
  3843. int i40e_control_wait_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
  3844. {
  3845. int ret = 0;
  3846. i40e_control_rx_q(pf, pf_q, enable);
  3847. /* wait for the change to finish */
  3848. ret = i40e_pf_rxq_wait(pf, pf_q, enable);
  3849. if (ret)
  3850. return ret;
  3851. return ret;
  3852. }
  3853. /**
  3854. * i40e_vsi_control_rx - Start or stop a VSI's rings
  3855. * @vsi: the VSI being configured
  3856. * @enable: start or stop the rings
  3857. **/
  3858. static int i40e_vsi_control_rx(struct i40e_vsi *vsi, bool enable)
  3859. {
  3860. struct i40e_pf *pf = vsi->back;
  3861. int i, pf_q, ret = 0;
  3862. pf_q = vsi->base_queue;
  3863. for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
  3864. ret = i40e_control_wait_rx_q(pf, pf_q, enable);
  3865. if (ret) {
  3866. dev_info(&pf->pdev->dev,
  3867. "VSI seid %d Rx ring %d %sable timeout\n",
  3868. vsi->seid, pf_q, (enable ? "en" : "dis"));
  3869. break;
  3870. }
  3871. }
  3872. /* Due to HW errata, on Rx disable only, the register can indicate done
  3873. * before it really is. Needs 50ms to be sure
  3874. */
  3875. if (!enable)
  3876. mdelay(50);
  3877. return ret;
  3878. }
  3879. /**
  3880. * i40e_vsi_start_rings - Start a VSI's rings
  3881. * @vsi: the VSI being configured
  3882. **/
  3883. int i40e_vsi_start_rings(struct i40e_vsi *vsi)
  3884. {
  3885. int ret = 0;
  3886. /* do rx first for enable and last for disable */
  3887. ret = i40e_vsi_control_rx(vsi, true);
  3888. if (ret)
  3889. return ret;
  3890. ret = i40e_vsi_control_tx(vsi, true);
  3891. return ret;
  3892. }
  3893. /**
  3894. * i40e_vsi_stop_rings - Stop a VSI's rings
  3895. * @vsi: the VSI being configured
  3896. **/
  3897. void i40e_vsi_stop_rings(struct i40e_vsi *vsi)
  3898. {
  3899. /* When port TX is suspended, don't wait */
  3900. if (test_bit(__I40E_PORT_SUSPENDED, vsi->back->state))
  3901. return i40e_vsi_stop_rings_no_wait(vsi);
  3902. /* do rx first for enable and last for disable
  3903. * Ignore return value, we need to shutdown whatever we can
  3904. */
  3905. i40e_vsi_control_tx(vsi, false);
  3906. i40e_vsi_control_rx(vsi, false);
  3907. }
  3908. /**
  3909. * i40e_vsi_stop_rings_no_wait - Stop a VSI's rings and do not delay
  3910. * @vsi: the VSI being shutdown
  3911. *
  3912. * This function stops all the rings for a VSI but does not delay to verify
  3913. * that rings have been disabled. It is expected that the caller is shutting
  3914. * down multiple VSIs at once and will delay together for all the VSIs after
  3915. * initiating the shutdown. This is particularly useful for shutting down lots
  3916. * of VFs together. Otherwise, a large delay can be incurred while configuring
  3917. * each VSI in serial.
  3918. **/
  3919. void i40e_vsi_stop_rings_no_wait(struct i40e_vsi *vsi)
  3920. {
  3921. struct i40e_pf *pf = vsi->back;
  3922. int i, pf_q;
  3923. pf_q = vsi->base_queue;
  3924. for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
  3925. i40e_control_tx_q(pf, pf_q, false);
  3926. i40e_control_rx_q(pf, pf_q, false);
  3927. }
  3928. }
  3929. /**
  3930. * i40e_vsi_free_irq - Free the irq association with the OS
  3931. * @vsi: the VSI being configured
  3932. **/
  3933. static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
  3934. {
  3935. struct i40e_pf *pf = vsi->back;
  3936. struct i40e_hw *hw = &pf->hw;
  3937. int base = vsi->base_vector;
  3938. u32 val, qp;
  3939. int i;
  3940. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  3941. if (!vsi->q_vectors)
  3942. return;
  3943. if (!vsi->irqs_ready)
  3944. return;
  3945. vsi->irqs_ready = false;
  3946. for (i = 0; i < vsi->num_q_vectors; i++) {
  3947. int irq_num;
  3948. u16 vector;
  3949. vector = i + base;
  3950. irq_num = pf->msix_entries[vector].vector;
  3951. /* free only the irqs that were actually requested */
  3952. if (!vsi->q_vectors[i] ||
  3953. !vsi->q_vectors[i]->num_ringpairs)
  3954. continue;
  3955. /* clear the affinity notifier in the IRQ descriptor */
  3956. irq_set_affinity_notifier(irq_num, NULL);
  3957. /* remove our suggested affinity mask for this IRQ */
  3958. irq_set_affinity_hint(irq_num, NULL);
  3959. synchronize_irq(irq_num);
  3960. free_irq(irq_num, vsi->q_vectors[i]);
  3961. /* Tear down the interrupt queue link list
  3962. *
  3963. * We know that they come in pairs and always
  3964. * the Rx first, then the Tx. To clear the
  3965. * link list, stick the EOL value into the
  3966. * next_q field of the registers.
  3967. */
  3968. val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
  3969. qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
  3970. >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
  3971. val |= I40E_QUEUE_END_OF_LIST
  3972. << I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
  3973. wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
  3974. while (qp != I40E_QUEUE_END_OF_LIST) {
  3975. u32 next;
  3976. val = rd32(hw, I40E_QINT_RQCTL(qp));
  3977. val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
  3978. I40E_QINT_RQCTL_MSIX0_INDX_MASK |
  3979. I40E_QINT_RQCTL_CAUSE_ENA_MASK |
  3980. I40E_QINT_RQCTL_INTEVENT_MASK);
  3981. val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
  3982. I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
  3983. wr32(hw, I40E_QINT_RQCTL(qp), val);
  3984. val = rd32(hw, I40E_QINT_TQCTL(qp));
  3985. next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
  3986. >> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
  3987. val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
  3988. I40E_QINT_TQCTL_MSIX0_INDX_MASK |
  3989. I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  3990. I40E_QINT_TQCTL_INTEVENT_MASK);
  3991. val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
  3992. I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
  3993. wr32(hw, I40E_QINT_TQCTL(qp), val);
  3994. qp = next;
  3995. }
  3996. }
  3997. } else {
  3998. free_irq(pf->pdev->irq, pf);
  3999. val = rd32(hw, I40E_PFINT_LNKLST0);
  4000. qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
  4001. >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
  4002. val |= I40E_QUEUE_END_OF_LIST
  4003. << I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
  4004. wr32(hw, I40E_PFINT_LNKLST0, val);
  4005. val = rd32(hw, I40E_QINT_RQCTL(qp));
  4006. val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
  4007. I40E_QINT_RQCTL_MSIX0_INDX_MASK |
  4008. I40E_QINT_RQCTL_CAUSE_ENA_MASK |
  4009. I40E_QINT_RQCTL_INTEVENT_MASK);
  4010. val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
  4011. I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
  4012. wr32(hw, I40E_QINT_RQCTL(qp), val);
  4013. val = rd32(hw, I40E_QINT_TQCTL(qp));
  4014. val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
  4015. I40E_QINT_TQCTL_MSIX0_INDX_MASK |
  4016. I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  4017. I40E_QINT_TQCTL_INTEVENT_MASK);
  4018. val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
  4019. I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
  4020. wr32(hw, I40E_QINT_TQCTL(qp), val);
  4021. }
  4022. }
  4023. /**
  4024. * i40e_free_q_vector - Free memory allocated for specific interrupt vector
  4025. * @vsi: the VSI being configured
  4026. * @v_idx: Index of vector to be freed
  4027. *
  4028. * This function frees the memory allocated to the q_vector. In addition if
  4029. * NAPI is enabled it will delete any references to the NAPI struct prior
  4030. * to freeing the q_vector.
  4031. **/
  4032. static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
  4033. {
  4034. struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
  4035. struct i40e_ring *ring;
  4036. if (!q_vector)
  4037. return;
  4038. /* disassociate q_vector from rings */
  4039. i40e_for_each_ring(ring, q_vector->tx)
  4040. ring->q_vector = NULL;
  4041. i40e_for_each_ring(ring, q_vector->rx)
  4042. ring->q_vector = NULL;
  4043. /* only VSI w/ an associated netdev is set up w/ NAPI */
  4044. if (vsi->netdev)
  4045. netif_napi_del(&q_vector->napi);
  4046. vsi->q_vectors[v_idx] = NULL;
  4047. kfree_rcu(q_vector, rcu);
  4048. }
  4049. /**
  4050. * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
  4051. * @vsi: the VSI being un-configured
  4052. *
  4053. * This frees the memory allocated to the q_vectors and
  4054. * deletes references to the NAPI struct.
  4055. **/
  4056. static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
  4057. {
  4058. int v_idx;
  4059. for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
  4060. i40e_free_q_vector(vsi, v_idx);
  4061. }
  4062. /**
  4063. * i40e_reset_interrupt_capability - Disable interrupt setup in OS
  4064. * @pf: board private structure
  4065. **/
  4066. static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
  4067. {
  4068. /* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
  4069. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  4070. pci_disable_msix(pf->pdev);
  4071. kfree(pf->msix_entries);
  4072. pf->msix_entries = NULL;
  4073. kfree(pf->irq_pile);
  4074. pf->irq_pile = NULL;
  4075. } else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
  4076. pci_disable_msi(pf->pdev);
  4077. }
  4078. pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
  4079. }
  4080. /**
  4081. * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
  4082. * @pf: board private structure
  4083. *
  4084. * We go through and clear interrupt specific resources and reset the structure
  4085. * to pre-load conditions
  4086. **/
  4087. static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
  4088. {
  4089. int i;
  4090. i40e_free_misc_vector(pf);
  4091. i40e_put_lump(pf->irq_pile, pf->iwarp_base_vector,
  4092. I40E_IWARP_IRQ_PILE_ID);
  4093. i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
  4094. for (i = 0; i < pf->num_alloc_vsi; i++)
  4095. if (pf->vsi[i])
  4096. i40e_vsi_free_q_vectors(pf->vsi[i]);
  4097. i40e_reset_interrupt_capability(pf);
  4098. }
  4099. /**
  4100. * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
  4101. * @vsi: the VSI being configured
  4102. **/
  4103. static void i40e_napi_enable_all(struct i40e_vsi *vsi)
  4104. {
  4105. int q_idx;
  4106. if (!vsi->netdev)
  4107. return;
  4108. for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
  4109. struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
  4110. if (q_vector->rx.ring || q_vector->tx.ring)
  4111. napi_enable(&q_vector->napi);
  4112. }
  4113. }
  4114. /**
  4115. * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
  4116. * @vsi: the VSI being configured
  4117. **/
  4118. static void i40e_napi_disable_all(struct i40e_vsi *vsi)
  4119. {
  4120. int q_idx;
  4121. if (!vsi->netdev)
  4122. return;
  4123. for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
  4124. struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
  4125. if (q_vector->rx.ring || q_vector->tx.ring)
  4126. napi_disable(&q_vector->napi);
  4127. }
  4128. }
  4129. /**
  4130. * i40e_vsi_close - Shut down a VSI
  4131. * @vsi: the vsi to be quelled
  4132. **/
  4133. static void i40e_vsi_close(struct i40e_vsi *vsi)
  4134. {
  4135. struct i40e_pf *pf = vsi->back;
  4136. if (!test_and_set_bit(__I40E_VSI_DOWN, vsi->state))
  4137. i40e_down(vsi);
  4138. i40e_vsi_free_irq(vsi);
  4139. i40e_vsi_free_tx_resources(vsi);
  4140. i40e_vsi_free_rx_resources(vsi);
  4141. vsi->current_netdev_flags = 0;
  4142. set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
  4143. if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
  4144. set_bit(__I40E_CLIENT_RESET, pf->state);
  4145. }
  4146. /**
  4147. * i40e_quiesce_vsi - Pause a given VSI
  4148. * @vsi: the VSI being paused
  4149. **/
  4150. static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
  4151. {
  4152. if (test_bit(__I40E_VSI_DOWN, vsi->state))
  4153. return;
  4154. set_bit(__I40E_VSI_NEEDS_RESTART, vsi->state);
  4155. if (vsi->netdev && netif_running(vsi->netdev))
  4156. vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
  4157. else
  4158. i40e_vsi_close(vsi);
  4159. }
  4160. /**
  4161. * i40e_unquiesce_vsi - Resume a given VSI
  4162. * @vsi: the VSI being resumed
  4163. **/
  4164. static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
  4165. {
  4166. if (!test_and_clear_bit(__I40E_VSI_NEEDS_RESTART, vsi->state))
  4167. return;
  4168. if (vsi->netdev && netif_running(vsi->netdev))
  4169. vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
  4170. else
  4171. i40e_vsi_open(vsi); /* this clears the DOWN bit */
  4172. }
  4173. /**
  4174. * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
  4175. * @pf: the PF
  4176. **/
  4177. static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
  4178. {
  4179. int v;
  4180. for (v = 0; v < pf->num_alloc_vsi; v++) {
  4181. if (pf->vsi[v])
  4182. i40e_quiesce_vsi(pf->vsi[v]);
  4183. }
  4184. }
  4185. /**
  4186. * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
  4187. * @pf: the PF
  4188. **/
  4189. static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
  4190. {
  4191. int v;
  4192. for (v = 0; v < pf->num_alloc_vsi; v++) {
  4193. if (pf->vsi[v])
  4194. i40e_unquiesce_vsi(pf->vsi[v]);
  4195. }
  4196. }
  4197. /**
  4198. * i40e_vsi_wait_queues_disabled - Wait for VSI's queues to be disabled
  4199. * @vsi: the VSI being configured
  4200. *
  4201. * Wait until all queues on a given VSI have been disabled.
  4202. **/
  4203. int i40e_vsi_wait_queues_disabled(struct i40e_vsi *vsi)
  4204. {
  4205. struct i40e_pf *pf = vsi->back;
  4206. int i, pf_q, ret;
  4207. pf_q = vsi->base_queue;
  4208. for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
  4209. /* Check and wait for the Tx queue */
  4210. ret = i40e_pf_txq_wait(pf, pf_q, false);
  4211. if (ret) {
  4212. dev_info(&pf->pdev->dev,
  4213. "VSI seid %d Tx ring %d disable timeout\n",
  4214. vsi->seid, pf_q);
  4215. return ret;
  4216. }
  4217. if (!i40e_enabled_xdp_vsi(vsi))
  4218. goto wait_rx;
  4219. /* Check and wait for the XDP Tx queue */
  4220. ret = i40e_pf_txq_wait(pf, pf_q + vsi->alloc_queue_pairs,
  4221. false);
  4222. if (ret) {
  4223. dev_info(&pf->pdev->dev,
  4224. "VSI seid %d XDP Tx ring %d disable timeout\n",
  4225. vsi->seid, pf_q);
  4226. return ret;
  4227. }
  4228. wait_rx:
  4229. /* Check and wait for the Rx queue */
  4230. ret = i40e_pf_rxq_wait(pf, pf_q, false);
  4231. if (ret) {
  4232. dev_info(&pf->pdev->dev,
  4233. "VSI seid %d Rx ring %d disable timeout\n",
  4234. vsi->seid, pf_q);
  4235. return ret;
  4236. }
  4237. }
  4238. return 0;
  4239. }
  4240. #ifdef CONFIG_I40E_DCB
  4241. /**
  4242. * i40e_pf_wait_queues_disabled - Wait for all queues of PF VSIs to be disabled
  4243. * @pf: the PF
  4244. *
  4245. * This function waits for the queues to be in disabled state for all the
  4246. * VSIs that are managed by this PF.
  4247. **/
  4248. static int i40e_pf_wait_queues_disabled(struct i40e_pf *pf)
  4249. {
  4250. int v, ret = 0;
  4251. for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
  4252. if (pf->vsi[v]) {
  4253. ret = i40e_vsi_wait_queues_disabled(pf->vsi[v]);
  4254. if (ret)
  4255. break;
  4256. }
  4257. }
  4258. return ret;
  4259. }
  4260. #endif
  4261. /**
  4262. * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
  4263. * @pf: pointer to PF
  4264. *
  4265. * Get TC map for ISCSI PF type that will include iSCSI TC
  4266. * and LAN TC.
  4267. **/
  4268. static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
  4269. {
  4270. struct i40e_dcb_app_priority_table app;
  4271. struct i40e_hw *hw = &pf->hw;
  4272. u8 enabled_tc = 1; /* TC0 is always enabled */
  4273. u8 tc, i;
  4274. /* Get the iSCSI APP TLV */
  4275. struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
  4276. for (i = 0; i < dcbcfg->numapps; i++) {
  4277. app = dcbcfg->app[i];
  4278. if (app.selector == I40E_APP_SEL_TCPIP &&
  4279. app.protocolid == I40E_APP_PROTOID_ISCSI) {
  4280. tc = dcbcfg->etscfg.prioritytable[app.priority];
  4281. enabled_tc |= BIT(tc);
  4282. break;
  4283. }
  4284. }
  4285. return enabled_tc;
  4286. }
  4287. /**
  4288. * i40e_dcb_get_num_tc - Get the number of TCs from DCBx config
  4289. * @dcbcfg: the corresponding DCBx configuration structure
  4290. *
  4291. * Return the number of TCs from given DCBx configuration
  4292. **/
  4293. static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
  4294. {
  4295. int i, tc_unused = 0;
  4296. u8 num_tc = 0;
  4297. u8 ret = 0;
  4298. /* Scan the ETS Config Priority Table to find
  4299. * traffic class enabled for a given priority
  4300. * and create a bitmask of enabled TCs
  4301. */
  4302. for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
  4303. num_tc |= BIT(dcbcfg->etscfg.prioritytable[i]);
  4304. /* Now scan the bitmask to check for
  4305. * contiguous TCs starting with TC0
  4306. */
  4307. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4308. if (num_tc & BIT(i)) {
  4309. if (!tc_unused) {
  4310. ret++;
  4311. } else {
  4312. pr_err("Non-contiguous TC - Disabling DCB\n");
  4313. return 1;
  4314. }
  4315. } else {
  4316. tc_unused = 1;
  4317. }
  4318. }
  4319. /* There is always at least TC0 */
  4320. if (!ret)
  4321. ret = 1;
  4322. return ret;
  4323. }
  4324. /**
  4325. * i40e_dcb_get_enabled_tc - Get enabled traffic classes
  4326. * @dcbcfg: the corresponding DCBx configuration structure
  4327. *
  4328. * Query the current DCB configuration and return the number of
  4329. * traffic classes enabled from the given DCBX config
  4330. **/
  4331. static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
  4332. {
  4333. u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
  4334. u8 enabled_tc = 1;
  4335. u8 i;
  4336. for (i = 0; i < num_tc; i++)
  4337. enabled_tc |= BIT(i);
  4338. return enabled_tc;
  4339. }
  4340. /**
  4341. * i40e_mqprio_get_enabled_tc - Get enabled traffic classes
  4342. * @pf: PF being queried
  4343. *
  4344. * Query the current MQPRIO configuration and return the number of
  4345. * traffic classes enabled.
  4346. **/
  4347. static u8 i40e_mqprio_get_enabled_tc(struct i40e_pf *pf)
  4348. {
  4349. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  4350. u8 num_tc = vsi->mqprio_qopt.qopt.num_tc;
  4351. u8 enabled_tc = 1, i;
  4352. for (i = 1; i < num_tc; i++)
  4353. enabled_tc |= BIT(i);
  4354. return enabled_tc;
  4355. }
  4356. /**
  4357. * i40e_pf_get_num_tc - Get enabled traffic classes for PF
  4358. * @pf: PF being queried
  4359. *
  4360. * Return number of traffic classes enabled for the given PF
  4361. **/
  4362. static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
  4363. {
  4364. struct i40e_hw *hw = &pf->hw;
  4365. u8 i, enabled_tc = 1;
  4366. u8 num_tc = 0;
  4367. struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
  4368. if (pf->flags & I40E_FLAG_TC_MQPRIO)
  4369. return pf->vsi[pf->lan_vsi]->mqprio_qopt.qopt.num_tc;
  4370. /* If neither MQPRIO nor DCB is enabled, then always use single TC */
  4371. if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
  4372. return 1;
  4373. /* SFP mode will be enabled for all TCs on port */
  4374. if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
  4375. return i40e_dcb_get_num_tc(dcbcfg);
  4376. /* MFP mode return count of enabled TCs for this PF */
  4377. if (pf->hw.func_caps.iscsi)
  4378. enabled_tc = i40e_get_iscsi_tc_map(pf);
  4379. else
  4380. return 1; /* Only TC0 */
  4381. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4382. if (enabled_tc & BIT(i))
  4383. num_tc++;
  4384. }
  4385. return num_tc;
  4386. }
  4387. /**
  4388. * i40e_pf_get_pf_tc_map - Get bitmap for enabled traffic classes
  4389. * @pf: PF being queried
  4390. *
  4391. * Return a bitmap for enabled traffic classes for this PF.
  4392. **/
  4393. static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
  4394. {
  4395. if (pf->flags & I40E_FLAG_TC_MQPRIO)
  4396. return i40e_mqprio_get_enabled_tc(pf);
  4397. /* If neither MQPRIO nor DCB is enabled for this PF then just return
  4398. * default TC
  4399. */
  4400. if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
  4401. return I40E_DEFAULT_TRAFFIC_CLASS;
  4402. /* SFP mode we want PF to be enabled for all TCs */
  4403. if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
  4404. return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
  4405. /* MFP enabled and iSCSI PF type */
  4406. if (pf->hw.func_caps.iscsi)
  4407. return i40e_get_iscsi_tc_map(pf);
  4408. else
  4409. return I40E_DEFAULT_TRAFFIC_CLASS;
  4410. }
  4411. /**
  4412. * i40e_vsi_get_bw_info - Query VSI BW Information
  4413. * @vsi: the VSI being queried
  4414. *
  4415. * Returns 0 on success, negative value on failure
  4416. **/
  4417. static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
  4418. {
  4419. struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
  4420. struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
  4421. struct i40e_pf *pf = vsi->back;
  4422. struct i40e_hw *hw = &pf->hw;
  4423. i40e_status ret;
  4424. u32 tc_bw_max;
  4425. int i;
  4426. /* Get the VSI level BW configuration */
  4427. ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
  4428. if (ret) {
  4429. dev_info(&pf->pdev->dev,
  4430. "couldn't get PF vsi bw config, err %s aq_err %s\n",
  4431. i40e_stat_str(&pf->hw, ret),
  4432. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  4433. return -EINVAL;
  4434. }
  4435. /* Get the VSI level BW configuration per TC */
  4436. ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
  4437. NULL);
  4438. if (ret) {
  4439. dev_info(&pf->pdev->dev,
  4440. "couldn't get PF vsi ets bw config, err %s aq_err %s\n",
  4441. i40e_stat_str(&pf->hw, ret),
  4442. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  4443. return -EINVAL;
  4444. }
  4445. if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
  4446. dev_info(&pf->pdev->dev,
  4447. "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
  4448. bw_config.tc_valid_bits,
  4449. bw_ets_config.tc_valid_bits);
  4450. /* Still continuing */
  4451. }
  4452. vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
  4453. vsi->bw_max_quanta = bw_config.max_bw;
  4454. tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
  4455. (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
  4456. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4457. vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
  4458. vsi->bw_ets_limit_credits[i] =
  4459. le16_to_cpu(bw_ets_config.credits[i]);
  4460. /* 3 bits out of 4 for each TC */
  4461. vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
  4462. }
  4463. return 0;
  4464. }
  4465. /**
  4466. * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
  4467. * @vsi: the VSI being configured
  4468. * @enabled_tc: TC bitmap
  4469. * @bw_share: BW shared credits per TC
  4470. *
  4471. * Returns 0 on success, negative value on failure
  4472. **/
  4473. static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
  4474. u8 *bw_share)
  4475. {
  4476. struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
  4477. struct i40e_pf *pf = vsi->back;
  4478. i40e_status ret;
  4479. int i;
  4480. /* There is no need to reset BW when mqprio mode is on. */
  4481. if (pf->flags & I40E_FLAG_TC_MQPRIO)
  4482. return 0;
  4483. if (!vsi->mqprio_qopt.qopt.hw && !(pf->flags & I40E_FLAG_DCB_ENABLED)) {
  4484. ret = i40e_set_bw_limit(vsi, vsi->seid, 0);
  4485. if (ret)
  4486. dev_info(&pf->pdev->dev,
  4487. "Failed to reset tx rate for vsi->seid %u\n",
  4488. vsi->seid);
  4489. return ret;
  4490. }
  4491. bw_data.tc_valid_bits = enabled_tc;
  4492. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
  4493. bw_data.tc_bw_credits[i] = bw_share[i];
  4494. ret = i40e_aq_config_vsi_tc_bw(&pf->hw, vsi->seid, &bw_data, NULL);
  4495. if (ret) {
  4496. dev_info(&pf->pdev->dev,
  4497. "AQ command Config VSI BW allocation per TC failed = %d\n",
  4498. pf->hw.aq.asq_last_status);
  4499. return -EINVAL;
  4500. }
  4501. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
  4502. vsi->info.qs_handle[i] = bw_data.qs_handles[i];
  4503. return 0;
  4504. }
  4505. /**
  4506. * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
  4507. * @vsi: the VSI being configured
  4508. * @enabled_tc: TC map to be enabled
  4509. *
  4510. **/
  4511. static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
  4512. {
  4513. struct net_device *netdev = vsi->netdev;
  4514. struct i40e_pf *pf = vsi->back;
  4515. struct i40e_hw *hw = &pf->hw;
  4516. u8 netdev_tc = 0;
  4517. int i;
  4518. struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
  4519. if (!netdev)
  4520. return;
  4521. if (!enabled_tc) {
  4522. netdev_reset_tc(netdev);
  4523. return;
  4524. }
  4525. /* Set up actual enabled TCs on the VSI */
  4526. if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
  4527. return;
  4528. /* set per TC queues for the VSI */
  4529. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4530. /* Only set TC queues for enabled tcs
  4531. *
  4532. * e.g. For a VSI that has TC0 and TC3 enabled the
  4533. * enabled_tc bitmap would be 0x00001001; the driver
  4534. * will set the numtc for netdev as 2 that will be
  4535. * referenced by the netdev layer as TC 0 and 1.
  4536. */
  4537. if (vsi->tc_config.enabled_tc & BIT(i))
  4538. netdev_set_tc_queue(netdev,
  4539. vsi->tc_config.tc_info[i].netdev_tc,
  4540. vsi->tc_config.tc_info[i].qcount,
  4541. vsi->tc_config.tc_info[i].qoffset);
  4542. }
  4543. if (pf->flags & I40E_FLAG_TC_MQPRIO)
  4544. return;
  4545. /* Assign UP2TC map for the VSI */
  4546. for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
  4547. /* Get the actual TC# for the UP */
  4548. u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
  4549. /* Get the mapped netdev TC# for the UP */
  4550. netdev_tc = vsi->tc_config.tc_info[ets_tc].netdev_tc;
  4551. netdev_set_prio_tc_map(netdev, i, netdev_tc);
  4552. }
  4553. }
  4554. /**
  4555. * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
  4556. * @vsi: the VSI being configured
  4557. * @ctxt: the ctxt buffer returned from AQ VSI update param command
  4558. **/
  4559. static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
  4560. struct i40e_vsi_context *ctxt)
  4561. {
  4562. /* copy just the sections touched not the entire info
  4563. * since not all sections are valid as returned by
  4564. * update vsi params
  4565. */
  4566. vsi->info.mapping_flags = ctxt->info.mapping_flags;
  4567. memcpy(&vsi->info.queue_mapping,
  4568. &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
  4569. memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
  4570. sizeof(vsi->info.tc_mapping));
  4571. }
  4572. /**
  4573. * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
  4574. * @vsi: VSI to be configured
  4575. * @enabled_tc: TC bitmap
  4576. *
  4577. * This configures a particular VSI for TCs that are mapped to the
  4578. * given TC bitmap. It uses default bandwidth share for TCs across
  4579. * VSIs to configure TC for a particular VSI.
  4580. *
  4581. * NOTE:
  4582. * It is expected that the VSI queues have been quisced before calling
  4583. * this function.
  4584. **/
  4585. static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
  4586. {
  4587. u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
  4588. struct i40e_pf *pf = vsi->back;
  4589. struct i40e_hw *hw = &pf->hw;
  4590. struct i40e_vsi_context ctxt;
  4591. int ret = 0;
  4592. int i;
  4593. /* Check if enabled_tc is same as existing or new TCs */
  4594. if (vsi->tc_config.enabled_tc == enabled_tc &&
  4595. vsi->mqprio_qopt.mode != TC_MQPRIO_MODE_CHANNEL)
  4596. return ret;
  4597. /* Enable ETS TCs with equal BW Share for now across all VSIs */
  4598. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4599. if (enabled_tc & BIT(i))
  4600. bw_share[i] = 1;
  4601. }
  4602. ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
  4603. if (ret) {
  4604. struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
  4605. dev_info(&pf->pdev->dev,
  4606. "Failed configuring TC map %d for VSI %d\n",
  4607. enabled_tc, vsi->seid);
  4608. ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid,
  4609. &bw_config, NULL);
  4610. if (ret) {
  4611. dev_info(&pf->pdev->dev,
  4612. "Failed querying vsi bw info, err %s aq_err %s\n",
  4613. i40e_stat_str(hw, ret),
  4614. i40e_aq_str(hw, hw->aq.asq_last_status));
  4615. goto out;
  4616. }
  4617. if ((bw_config.tc_valid_bits & enabled_tc) != enabled_tc) {
  4618. u8 valid_tc = bw_config.tc_valid_bits & enabled_tc;
  4619. if (!valid_tc)
  4620. valid_tc = bw_config.tc_valid_bits;
  4621. /* Always enable TC0, no matter what */
  4622. valid_tc |= 1;
  4623. dev_info(&pf->pdev->dev,
  4624. "Requested tc 0x%x, but FW reports 0x%x as valid. Attempting to use 0x%x.\n",
  4625. enabled_tc, bw_config.tc_valid_bits, valid_tc);
  4626. enabled_tc = valid_tc;
  4627. }
  4628. ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
  4629. if (ret) {
  4630. dev_err(&pf->pdev->dev,
  4631. "Unable to configure TC map %d for VSI %d\n",
  4632. enabled_tc, vsi->seid);
  4633. goto out;
  4634. }
  4635. }
  4636. /* Update Queue Pairs Mapping for currently enabled UPs */
  4637. ctxt.seid = vsi->seid;
  4638. ctxt.pf_num = vsi->back->hw.pf_id;
  4639. ctxt.vf_num = 0;
  4640. ctxt.uplink_seid = vsi->uplink_seid;
  4641. ctxt.info = vsi->info;
  4642. if (vsi->back->flags & I40E_FLAG_TC_MQPRIO) {
  4643. ret = i40e_vsi_setup_queue_map_mqprio(vsi, &ctxt, enabled_tc);
  4644. if (ret)
  4645. goto out;
  4646. } else {
  4647. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
  4648. }
  4649. /* On destroying the qdisc, reset vsi->rss_size, as number of enabled
  4650. * queues changed.
  4651. */
  4652. if (!vsi->mqprio_qopt.qopt.hw && vsi->reconfig_rss) {
  4653. vsi->rss_size = min_t(int, vsi->back->alloc_rss_size,
  4654. vsi->num_queue_pairs);
  4655. ret = i40e_vsi_config_rss(vsi);
  4656. if (ret) {
  4657. dev_info(&vsi->back->pdev->dev,
  4658. "Failed to reconfig rss for num_queues\n");
  4659. return ret;
  4660. }
  4661. vsi->reconfig_rss = false;
  4662. }
  4663. if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
  4664. ctxt.info.valid_sections |=
  4665. cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
  4666. ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
  4667. }
  4668. /* Update the VSI after updating the VSI queue-mapping
  4669. * information
  4670. */
  4671. ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
  4672. if (ret) {
  4673. dev_info(&pf->pdev->dev,
  4674. "Update vsi tc config failed, err %s aq_err %s\n",
  4675. i40e_stat_str(hw, ret),
  4676. i40e_aq_str(hw, hw->aq.asq_last_status));
  4677. goto out;
  4678. }
  4679. /* update the local VSI info with updated queue map */
  4680. i40e_vsi_update_queue_map(vsi, &ctxt);
  4681. vsi->info.valid_sections = 0;
  4682. /* Update current VSI BW information */
  4683. ret = i40e_vsi_get_bw_info(vsi);
  4684. if (ret) {
  4685. dev_info(&pf->pdev->dev,
  4686. "Failed updating vsi bw info, err %s aq_err %s\n",
  4687. i40e_stat_str(hw, ret),
  4688. i40e_aq_str(hw, hw->aq.asq_last_status));
  4689. goto out;
  4690. }
  4691. /* Update the netdev TC setup */
  4692. i40e_vsi_config_netdev_tc(vsi, enabled_tc);
  4693. out:
  4694. return ret;
  4695. }
  4696. /**
  4697. * i40e_get_link_speed - Returns link speed for the interface
  4698. * @vsi: VSI to be configured
  4699. *
  4700. **/
  4701. static int i40e_get_link_speed(struct i40e_vsi *vsi)
  4702. {
  4703. struct i40e_pf *pf = vsi->back;
  4704. switch (pf->hw.phy.link_info.link_speed) {
  4705. case I40E_LINK_SPEED_40GB:
  4706. return 40000;
  4707. case I40E_LINK_SPEED_25GB:
  4708. return 25000;
  4709. case I40E_LINK_SPEED_20GB:
  4710. return 20000;
  4711. case I40E_LINK_SPEED_10GB:
  4712. return 10000;
  4713. case I40E_LINK_SPEED_1GB:
  4714. return 1000;
  4715. default:
  4716. return -EINVAL;
  4717. }
  4718. }
  4719. /**
  4720. * i40e_set_bw_limit - setup BW limit for Tx traffic based on max_tx_rate
  4721. * @vsi: VSI to be configured
  4722. * @seid: seid of the channel/VSI
  4723. * @max_tx_rate: max TX rate to be configured as BW limit
  4724. *
  4725. * Helper function to set BW limit for a given VSI
  4726. **/
  4727. int i40e_set_bw_limit(struct i40e_vsi *vsi, u16 seid, u64 max_tx_rate)
  4728. {
  4729. struct i40e_pf *pf = vsi->back;
  4730. u64 credits = 0;
  4731. int speed = 0;
  4732. int ret = 0;
  4733. speed = i40e_get_link_speed(vsi);
  4734. if (max_tx_rate > speed) {
  4735. dev_err(&pf->pdev->dev,
  4736. "Invalid max tx rate %llu specified for VSI seid %d.",
  4737. max_tx_rate, seid);
  4738. return -EINVAL;
  4739. }
  4740. if (max_tx_rate && max_tx_rate < 50) {
  4741. dev_warn(&pf->pdev->dev,
  4742. "Setting max tx rate to minimum usable value of 50Mbps.\n");
  4743. max_tx_rate = 50;
  4744. }
  4745. /* Tx rate credits are in values of 50Mbps, 0 is disabled */
  4746. credits = max_tx_rate;
  4747. do_div(credits, I40E_BW_CREDIT_DIVISOR);
  4748. ret = i40e_aq_config_vsi_bw_limit(&pf->hw, seid, credits,
  4749. I40E_MAX_BW_INACTIVE_ACCUM, NULL);
  4750. if (ret)
  4751. dev_err(&pf->pdev->dev,
  4752. "Failed set tx rate (%llu Mbps) for vsi->seid %u, err %s aq_err %s\n",
  4753. max_tx_rate, seid, i40e_stat_str(&pf->hw, ret),
  4754. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  4755. return ret;
  4756. }
  4757. /**
  4758. * i40e_remove_queue_channels - Remove queue channels for the TCs
  4759. * @vsi: VSI to be configured
  4760. *
  4761. * Remove queue channels for the TCs
  4762. **/
  4763. static void i40e_remove_queue_channels(struct i40e_vsi *vsi)
  4764. {
  4765. enum i40e_admin_queue_err last_aq_status;
  4766. struct i40e_cloud_filter *cfilter;
  4767. struct i40e_channel *ch, *ch_tmp;
  4768. struct i40e_pf *pf = vsi->back;
  4769. struct hlist_node *node;
  4770. int ret, i;
  4771. /* Reset rss size that was stored when reconfiguring rss for
  4772. * channel VSIs with non-power-of-2 queue count.
  4773. */
  4774. vsi->current_rss_size = 0;
  4775. /* perform cleanup for channels if they exist */
  4776. if (list_empty(&vsi->ch_list))
  4777. return;
  4778. list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
  4779. struct i40e_vsi *p_vsi;
  4780. list_del(&ch->list);
  4781. p_vsi = ch->parent_vsi;
  4782. if (!p_vsi || !ch->initialized) {
  4783. kfree(ch);
  4784. continue;
  4785. }
  4786. /* Reset queue contexts */
  4787. for (i = 0; i < ch->num_queue_pairs; i++) {
  4788. struct i40e_ring *tx_ring, *rx_ring;
  4789. u16 pf_q;
  4790. pf_q = ch->base_queue + i;
  4791. tx_ring = vsi->tx_rings[pf_q];
  4792. tx_ring->ch = NULL;
  4793. rx_ring = vsi->rx_rings[pf_q];
  4794. rx_ring->ch = NULL;
  4795. }
  4796. /* Reset BW configured for this VSI via mqprio */
  4797. ret = i40e_set_bw_limit(vsi, ch->seid, 0);
  4798. if (ret)
  4799. dev_info(&vsi->back->pdev->dev,
  4800. "Failed to reset tx rate for ch->seid %u\n",
  4801. ch->seid);
  4802. /* delete cloud filters associated with this channel */
  4803. hlist_for_each_entry_safe(cfilter, node,
  4804. &pf->cloud_filter_list, cloud_node) {
  4805. if (cfilter->seid != ch->seid)
  4806. continue;
  4807. hash_del(&cfilter->cloud_node);
  4808. if (cfilter->dst_port)
  4809. ret = i40e_add_del_cloud_filter_big_buf(vsi,
  4810. cfilter,
  4811. false);
  4812. else
  4813. ret = i40e_add_del_cloud_filter(vsi, cfilter,
  4814. false);
  4815. last_aq_status = pf->hw.aq.asq_last_status;
  4816. if (ret)
  4817. dev_info(&pf->pdev->dev,
  4818. "Failed to delete cloud filter, err %s aq_err %s\n",
  4819. i40e_stat_str(&pf->hw, ret),
  4820. i40e_aq_str(&pf->hw, last_aq_status));
  4821. kfree(cfilter);
  4822. }
  4823. /* delete VSI from FW */
  4824. ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
  4825. NULL);
  4826. if (ret)
  4827. dev_err(&vsi->back->pdev->dev,
  4828. "unable to remove channel (%d) for parent VSI(%d)\n",
  4829. ch->seid, p_vsi->seid);
  4830. kfree(ch);
  4831. }
  4832. INIT_LIST_HEAD(&vsi->ch_list);
  4833. }
  4834. /**
  4835. * i40e_is_any_channel - channel exist or not
  4836. * @vsi: ptr to VSI to which channels are associated with
  4837. *
  4838. * Returns true or false if channel(s) exist for associated VSI or not
  4839. **/
  4840. static bool i40e_is_any_channel(struct i40e_vsi *vsi)
  4841. {
  4842. struct i40e_channel *ch, *ch_tmp;
  4843. list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
  4844. if (ch->initialized)
  4845. return true;
  4846. }
  4847. return false;
  4848. }
  4849. /**
  4850. * i40e_get_max_queues_for_channel
  4851. * @vsi: ptr to VSI to which channels are associated with
  4852. *
  4853. * Helper function which returns max value among the queue counts set on the
  4854. * channels/TCs created.
  4855. **/
  4856. static int i40e_get_max_queues_for_channel(struct i40e_vsi *vsi)
  4857. {
  4858. struct i40e_channel *ch, *ch_tmp;
  4859. int max = 0;
  4860. list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
  4861. if (!ch->initialized)
  4862. continue;
  4863. if (ch->num_queue_pairs > max)
  4864. max = ch->num_queue_pairs;
  4865. }
  4866. return max;
  4867. }
  4868. /**
  4869. * i40e_validate_num_queues - validate num_queues w.r.t channel
  4870. * @pf: ptr to PF device
  4871. * @num_queues: number of queues
  4872. * @vsi: the parent VSI
  4873. * @reconfig_rss: indicates should the RSS be reconfigured or not
  4874. *
  4875. * This function validates number of queues in the context of new channel
  4876. * which is being established and determines if RSS should be reconfigured
  4877. * or not for parent VSI.
  4878. **/
  4879. static int i40e_validate_num_queues(struct i40e_pf *pf, int num_queues,
  4880. struct i40e_vsi *vsi, bool *reconfig_rss)
  4881. {
  4882. int max_ch_queues;
  4883. if (!reconfig_rss)
  4884. return -EINVAL;
  4885. *reconfig_rss = false;
  4886. if (vsi->current_rss_size) {
  4887. if (num_queues > vsi->current_rss_size) {
  4888. dev_dbg(&pf->pdev->dev,
  4889. "Error: num_queues (%d) > vsi's current_size(%d)\n",
  4890. num_queues, vsi->current_rss_size);
  4891. return -EINVAL;
  4892. } else if ((num_queues < vsi->current_rss_size) &&
  4893. (!is_power_of_2(num_queues))) {
  4894. dev_dbg(&pf->pdev->dev,
  4895. "Error: num_queues (%d) < vsi's current_size(%d), but not power of 2\n",
  4896. num_queues, vsi->current_rss_size);
  4897. return -EINVAL;
  4898. }
  4899. }
  4900. if (!is_power_of_2(num_queues)) {
  4901. /* Find the max num_queues configured for channel if channel
  4902. * exist.
  4903. * if channel exist, then enforce 'num_queues' to be more than
  4904. * max ever queues configured for channel.
  4905. */
  4906. max_ch_queues = i40e_get_max_queues_for_channel(vsi);
  4907. if (num_queues < max_ch_queues) {
  4908. dev_dbg(&pf->pdev->dev,
  4909. "Error: num_queues (%d) < max queues configured for channel(%d)\n",
  4910. num_queues, max_ch_queues);
  4911. return -EINVAL;
  4912. }
  4913. *reconfig_rss = true;
  4914. }
  4915. return 0;
  4916. }
  4917. /**
  4918. * i40e_vsi_reconfig_rss - reconfig RSS based on specified rss_size
  4919. * @vsi: the VSI being setup
  4920. * @rss_size: size of RSS, accordingly LUT gets reprogrammed
  4921. *
  4922. * This function reconfigures RSS by reprogramming LUTs using 'rss_size'
  4923. **/
  4924. static int i40e_vsi_reconfig_rss(struct i40e_vsi *vsi, u16 rss_size)
  4925. {
  4926. struct i40e_pf *pf = vsi->back;
  4927. u8 seed[I40E_HKEY_ARRAY_SIZE];
  4928. struct i40e_hw *hw = &pf->hw;
  4929. int local_rss_size;
  4930. u8 *lut;
  4931. int ret;
  4932. if (!vsi->rss_size)
  4933. return -EINVAL;
  4934. if (rss_size > vsi->rss_size)
  4935. return -EINVAL;
  4936. local_rss_size = min_t(int, vsi->rss_size, rss_size);
  4937. lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
  4938. if (!lut)
  4939. return -ENOMEM;
  4940. /* Ignoring user configured lut if there is one */
  4941. i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, local_rss_size);
  4942. /* Use user configured hash key if there is one, otherwise
  4943. * use default.
  4944. */
  4945. if (vsi->rss_hkey_user)
  4946. memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
  4947. else
  4948. netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
  4949. ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
  4950. if (ret) {
  4951. dev_info(&pf->pdev->dev,
  4952. "Cannot set RSS lut, err %s aq_err %s\n",
  4953. i40e_stat_str(hw, ret),
  4954. i40e_aq_str(hw, hw->aq.asq_last_status));
  4955. kfree(lut);
  4956. return ret;
  4957. }
  4958. kfree(lut);
  4959. /* Do the update w.r.t. storing rss_size */
  4960. if (!vsi->orig_rss_size)
  4961. vsi->orig_rss_size = vsi->rss_size;
  4962. vsi->current_rss_size = local_rss_size;
  4963. return ret;
  4964. }
  4965. /**
  4966. * i40e_channel_setup_queue_map - Setup a channel queue map
  4967. * @pf: ptr to PF device
  4968. * @vsi: the VSI being setup
  4969. * @ctxt: VSI context structure
  4970. * @ch: ptr to channel structure
  4971. *
  4972. * Setup queue map for a specific channel
  4973. **/
  4974. static void i40e_channel_setup_queue_map(struct i40e_pf *pf,
  4975. struct i40e_vsi_context *ctxt,
  4976. struct i40e_channel *ch)
  4977. {
  4978. u16 qcount, qmap, sections = 0;
  4979. u8 offset = 0;
  4980. int pow;
  4981. sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
  4982. sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
  4983. qcount = min_t(int, ch->num_queue_pairs, pf->num_lan_msix);
  4984. ch->num_queue_pairs = qcount;
  4985. /* find the next higher power-of-2 of num queue pairs */
  4986. pow = ilog2(qcount);
  4987. if (!is_power_of_2(qcount))
  4988. pow++;
  4989. qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
  4990. (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
  4991. /* Setup queue TC[0].qmap for given VSI context */
  4992. ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
  4993. ctxt->info.up_enable_bits = 0x1; /* TC0 enabled */
  4994. ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
  4995. ctxt->info.queue_mapping[0] = cpu_to_le16(ch->base_queue);
  4996. ctxt->info.valid_sections |= cpu_to_le16(sections);
  4997. }
  4998. /**
  4999. * i40e_add_channel - add a channel by adding VSI
  5000. * @pf: ptr to PF device
  5001. * @uplink_seid: underlying HW switching element (VEB) ID
  5002. * @ch: ptr to channel structure
  5003. *
  5004. * Add a channel (VSI) using add_vsi and queue_map
  5005. **/
  5006. static int i40e_add_channel(struct i40e_pf *pf, u16 uplink_seid,
  5007. struct i40e_channel *ch)
  5008. {
  5009. struct i40e_hw *hw = &pf->hw;
  5010. struct i40e_vsi_context ctxt;
  5011. u8 enabled_tc = 0x1; /* TC0 enabled */
  5012. int ret;
  5013. if (ch->type != I40E_VSI_VMDQ2) {
  5014. dev_info(&pf->pdev->dev,
  5015. "add new vsi failed, ch->type %d\n", ch->type);
  5016. return -EINVAL;
  5017. }
  5018. memset(&ctxt, 0, sizeof(ctxt));
  5019. ctxt.pf_num = hw->pf_id;
  5020. ctxt.vf_num = 0;
  5021. ctxt.uplink_seid = uplink_seid;
  5022. ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
  5023. if (ch->type == I40E_VSI_VMDQ2)
  5024. ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
  5025. if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED) {
  5026. ctxt.info.valid_sections |=
  5027. cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  5028. ctxt.info.switch_id =
  5029. cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  5030. }
  5031. /* Set queue map for a given VSI context */
  5032. i40e_channel_setup_queue_map(pf, &ctxt, ch);
  5033. /* Now time to create VSI */
  5034. ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
  5035. if (ret) {
  5036. dev_info(&pf->pdev->dev,
  5037. "add new vsi failed, err %s aq_err %s\n",
  5038. i40e_stat_str(&pf->hw, ret),
  5039. i40e_aq_str(&pf->hw,
  5040. pf->hw.aq.asq_last_status));
  5041. return -ENOENT;
  5042. }
  5043. /* Success, update channel */
  5044. ch->enabled_tc = enabled_tc;
  5045. ch->seid = ctxt.seid;
  5046. ch->vsi_number = ctxt.vsi_number;
  5047. ch->stat_counter_idx = cpu_to_le16(ctxt.info.stat_counter_idx);
  5048. /* copy just the sections touched not the entire info
  5049. * since not all sections are valid as returned by
  5050. * update vsi params
  5051. */
  5052. ch->info.mapping_flags = ctxt.info.mapping_flags;
  5053. memcpy(&ch->info.queue_mapping,
  5054. &ctxt.info.queue_mapping, sizeof(ctxt.info.queue_mapping));
  5055. memcpy(&ch->info.tc_mapping, ctxt.info.tc_mapping,
  5056. sizeof(ctxt.info.tc_mapping));
  5057. return 0;
  5058. }
  5059. static int i40e_channel_config_bw(struct i40e_vsi *vsi, struct i40e_channel *ch,
  5060. u8 *bw_share)
  5061. {
  5062. struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
  5063. i40e_status ret;
  5064. int i;
  5065. bw_data.tc_valid_bits = ch->enabled_tc;
  5066. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
  5067. bw_data.tc_bw_credits[i] = bw_share[i];
  5068. ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, ch->seid,
  5069. &bw_data, NULL);
  5070. if (ret) {
  5071. dev_info(&vsi->back->pdev->dev,
  5072. "Config VSI BW allocation per TC failed, aq_err: %d for new_vsi->seid %u\n",
  5073. vsi->back->hw.aq.asq_last_status, ch->seid);
  5074. return -EINVAL;
  5075. }
  5076. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
  5077. ch->info.qs_handle[i] = bw_data.qs_handles[i];
  5078. return 0;
  5079. }
  5080. /**
  5081. * i40e_channel_config_tx_ring - config TX ring associated with new channel
  5082. * @pf: ptr to PF device
  5083. * @vsi: the VSI being setup
  5084. * @ch: ptr to channel structure
  5085. *
  5086. * Configure TX rings associated with channel (VSI) since queues are being
  5087. * from parent VSI.
  5088. **/
  5089. static int i40e_channel_config_tx_ring(struct i40e_pf *pf,
  5090. struct i40e_vsi *vsi,
  5091. struct i40e_channel *ch)
  5092. {
  5093. i40e_status ret;
  5094. int i;
  5095. u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
  5096. /* Enable ETS TCs with equal BW Share for now across all VSIs */
  5097. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  5098. if (ch->enabled_tc & BIT(i))
  5099. bw_share[i] = 1;
  5100. }
  5101. /* configure BW for new VSI */
  5102. ret = i40e_channel_config_bw(vsi, ch, bw_share);
  5103. if (ret) {
  5104. dev_info(&vsi->back->pdev->dev,
  5105. "Failed configuring TC map %d for channel (seid %u)\n",
  5106. ch->enabled_tc, ch->seid);
  5107. return ret;
  5108. }
  5109. for (i = 0; i < ch->num_queue_pairs; i++) {
  5110. struct i40e_ring *tx_ring, *rx_ring;
  5111. u16 pf_q;
  5112. pf_q = ch->base_queue + i;
  5113. /* Get to TX ring ptr of main VSI, for re-setup TX queue
  5114. * context
  5115. */
  5116. tx_ring = vsi->tx_rings[pf_q];
  5117. tx_ring->ch = ch;
  5118. /* Get the RX ring ptr */
  5119. rx_ring = vsi->rx_rings[pf_q];
  5120. rx_ring->ch = ch;
  5121. }
  5122. return 0;
  5123. }
  5124. /**
  5125. * i40e_setup_hw_channel - setup new channel
  5126. * @pf: ptr to PF device
  5127. * @vsi: the VSI being setup
  5128. * @ch: ptr to channel structure
  5129. * @uplink_seid: underlying HW switching element (VEB) ID
  5130. * @type: type of channel to be created (VMDq2/VF)
  5131. *
  5132. * Setup new channel (VSI) based on specified type (VMDq2/VF)
  5133. * and configures TX rings accordingly
  5134. **/
  5135. static inline int i40e_setup_hw_channel(struct i40e_pf *pf,
  5136. struct i40e_vsi *vsi,
  5137. struct i40e_channel *ch,
  5138. u16 uplink_seid, u8 type)
  5139. {
  5140. int ret;
  5141. ch->initialized = false;
  5142. ch->base_queue = vsi->next_base_queue;
  5143. ch->type = type;
  5144. /* Proceed with creation of channel (VMDq2) VSI */
  5145. ret = i40e_add_channel(pf, uplink_seid, ch);
  5146. if (ret) {
  5147. dev_info(&pf->pdev->dev,
  5148. "failed to add_channel using uplink_seid %u\n",
  5149. uplink_seid);
  5150. return ret;
  5151. }
  5152. /* Mark the successful creation of channel */
  5153. ch->initialized = true;
  5154. /* Reconfigure TX queues using QTX_CTL register */
  5155. ret = i40e_channel_config_tx_ring(pf, vsi, ch);
  5156. if (ret) {
  5157. dev_info(&pf->pdev->dev,
  5158. "failed to configure TX rings for channel %u\n",
  5159. ch->seid);
  5160. return ret;
  5161. }
  5162. /* update 'next_base_queue' */
  5163. vsi->next_base_queue = vsi->next_base_queue + ch->num_queue_pairs;
  5164. dev_dbg(&pf->pdev->dev,
  5165. "Added channel: vsi_seid %u, vsi_number %u, stat_counter_idx %u, num_queue_pairs %u, pf->next_base_queue %d\n",
  5166. ch->seid, ch->vsi_number, ch->stat_counter_idx,
  5167. ch->num_queue_pairs,
  5168. vsi->next_base_queue);
  5169. return ret;
  5170. }
  5171. /**
  5172. * i40e_setup_channel - setup new channel using uplink element
  5173. * @pf: ptr to PF device
  5174. * @type: type of channel to be created (VMDq2/VF)
  5175. * @uplink_seid: underlying HW switching element (VEB) ID
  5176. * @ch: ptr to channel structure
  5177. *
  5178. * Setup new channel (VSI) based on specified type (VMDq2/VF)
  5179. * and uplink switching element (uplink_seid)
  5180. **/
  5181. static bool i40e_setup_channel(struct i40e_pf *pf, struct i40e_vsi *vsi,
  5182. struct i40e_channel *ch)
  5183. {
  5184. u8 vsi_type;
  5185. u16 seid;
  5186. int ret;
  5187. if (vsi->type == I40E_VSI_MAIN) {
  5188. vsi_type = I40E_VSI_VMDQ2;
  5189. } else {
  5190. dev_err(&pf->pdev->dev, "unsupported parent vsi type(%d)\n",
  5191. vsi->type);
  5192. return false;
  5193. }
  5194. /* underlying switching element */
  5195. seid = pf->vsi[pf->lan_vsi]->uplink_seid;
  5196. /* create channel (VSI), configure TX rings */
  5197. ret = i40e_setup_hw_channel(pf, vsi, ch, seid, vsi_type);
  5198. if (ret) {
  5199. dev_err(&pf->pdev->dev, "failed to setup hw_channel\n");
  5200. return false;
  5201. }
  5202. return ch->initialized ? true : false;
  5203. }
  5204. /**
  5205. * i40e_validate_and_set_switch_mode - sets up switch mode correctly
  5206. * @vsi: ptr to VSI which has PF backing
  5207. *
  5208. * Sets up switch mode correctly if it needs to be changed and perform
  5209. * what are allowed modes.
  5210. **/
  5211. static int i40e_validate_and_set_switch_mode(struct i40e_vsi *vsi)
  5212. {
  5213. u8 mode;
  5214. struct i40e_pf *pf = vsi->back;
  5215. struct i40e_hw *hw = &pf->hw;
  5216. int ret;
  5217. ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_dev_capabilities);
  5218. if (ret)
  5219. return -EINVAL;
  5220. if (hw->dev_caps.switch_mode) {
  5221. /* if switch mode is set, support mode2 (non-tunneled for
  5222. * cloud filter) for now
  5223. */
  5224. u32 switch_mode = hw->dev_caps.switch_mode &
  5225. I40E_SWITCH_MODE_MASK;
  5226. if (switch_mode >= I40E_CLOUD_FILTER_MODE1) {
  5227. if (switch_mode == I40E_CLOUD_FILTER_MODE2)
  5228. return 0;
  5229. dev_err(&pf->pdev->dev,
  5230. "Invalid switch_mode (%d), only non-tunneled mode for cloud filter is supported\n",
  5231. hw->dev_caps.switch_mode);
  5232. return -EINVAL;
  5233. }
  5234. }
  5235. /* Set Bit 7 to be valid */
  5236. mode = I40E_AQ_SET_SWITCH_BIT7_VALID;
  5237. /* Set L4type for TCP support */
  5238. mode |= I40E_AQ_SET_SWITCH_L4_TYPE_TCP;
  5239. /* Set cloud filter mode */
  5240. mode |= I40E_AQ_SET_SWITCH_MODE_NON_TUNNEL;
  5241. /* Prep mode field for set_switch_config */
  5242. ret = i40e_aq_set_switch_config(hw, pf->last_sw_conf_flags,
  5243. pf->last_sw_conf_valid_flags,
  5244. mode, NULL);
  5245. if (ret && hw->aq.asq_last_status != I40E_AQ_RC_ESRCH)
  5246. dev_err(&pf->pdev->dev,
  5247. "couldn't set switch config bits, err %s aq_err %s\n",
  5248. i40e_stat_str(hw, ret),
  5249. i40e_aq_str(hw,
  5250. hw->aq.asq_last_status));
  5251. return ret;
  5252. }
  5253. /**
  5254. * i40e_create_queue_channel - function to create channel
  5255. * @vsi: VSI to be configured
  5256. * @ch: ptr to channel (it contains channel specific params)
  5257. *
  5258. * This function creates channel (VSI) using num_queues specified by user,
  5259. * reconfigs RSS if needed.
  5260. **/
  5261. int i40e_create_queue_channel(struct i40e_vsi *vsi,
  5262. struct i40e_channel *ch)
  5263. {
  5264. struct i40e_pf *pf = vsi->back;
  5265. bool reconfig_rss;
  5266. int err;
  5267. if (!ch)
  5268. return -EINVAL;
  5269. if (!ch->num_queue_pairs) {
  5270. dev_err(&pf->pdev->dev, "Invalid num_queues requested: %d\n",
  5271. ch->num_queue_pairs);
  5272. return -EINVAL;
  5273. }
  5274. /* validate user requested num_queues for channel */
  5275. err = i40e_validate_num_queues(pf, ch->num_queue_pairs, vsi,
  5276. &reconfig_rss);
  5277. if (err) {
  5278. dev_info(&pf->pdev->dev, "Failed to validate num_queues (%d)\n",
  5279. ch->num_queue_pairs);
  5280. return -EINVAL;
  5281. }
  5282. /* By default we are in VEPA mode, if this is the first VF/VMDq
  5283. * VSI to be added switch to VEB mode.
  5284. */
  5285. if ((!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) ||
  5286. (!i40e_is_any_channel(vsi))) {
  5287. if (!is_power_of_2(vsi->tc_config.tc_info[0].qcount)) {
  5288. dev_dbg(&pf->pdev->dev,
  5289. "Failed to create channel. Override queues (%u) not power of 2\n",
  5290. vsi->tc_config.tc_info[0].qcount);
  5291. return -EINVAL;
  5292. }
  5293. if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
  5294. pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
  5295. if (vsi->type == I40E_VSI_MAIN) {
  5296. if (pf->flags & I40E_FLAG_TC_MQPRIO)
  5297. i40e_do_reset(pf, I40E_PF_RESET_FLAG,
  5298. true);
  5299. else
  5300. i40e_do_reset_safe(pf,
  5301. I40E_PF_RESET_FLAG);
  5302. }
  5303. }
  5304. /* now onwards for main VSI, number of queues will be value
  5305. * of TC0's queue count
  5306. */
  5307. }
  5308. /* By this time, vsi->cnt_q_avail shall be set to non-zero and
  5309. * it should be more than num_queues
  5310. */
  5311. if (!vsi->cnt_q_avail || vsi->cnt_q_avail < ch->num_queue_pairs) {
  5312. dev_dbg(&pf->pdev->dev,
  5313. "Error: cnt_q_avail (%u) less than num_queues %d\n",
  5314. vsi->cnt_q_avail, ch->num_queue_pairs);
  5315. return -EINVAL;
  5316. }
  5317. /* reconfig_rss only if vsi type is MAIN_VSI */
  5318. if (reconfig_rss && (vsi->type == I40E_VSI_MAIN)) {
  5319. err = i40e_vsi_reconfig_rss(vsi, ch->num_queue_pairs);
  5320. if (err) {
  5321. dev_info(&pf->pdev->dev,
  5322. "Error: unable to reconfig rss for num_queues (%u)\n",
  5323. ch->num_queue_pairs);
  5324. return -EINVAL;
  5325. }
  5326. }
  5327. if (!i40e_setup_channel(pf, vsi, ch)) {
  5328. dev_info(&pf->pdev->dev, "Failed to setup channel\n");
  5329. return -EINVAL;
  5330. }
  5331. dev_info(&pf->pdev->dev,
  5332. "Setup channel (id:%u) utilizing num_queues %d\n",
  5333. ch->seid, ch->num_queue_pairs);
  5334. /* configure VSI for BW limit */
  5335. if (ch->max_tx_rate) {
  5336. u64 credits = ch->max_tx_rate;
  5337. if (i40e_set_bw_limit(vsi, ch->seid, ch->max_tx_rate))
  5338. return -EINVAL;
  5339. do_div(credits, I40E_BW_CREDIT_DIVISOR);
  5340. dev_dbg(&pf->pdev->dev,
  5341. "Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
  5342. ch->max_tx_rate,
  5343. credits,
  5344. ch->seid);
  5345. }
  5346. /* in case of VF, this will be main SRIOV VSI */
  5347. ch->parent_vsi = vsi;
  5348. /* and update main_vsi's count for queue_available to use */
  5349. vsi->cnt_q_avail -= ch->num_queue_pairs;
  5350. return 0;
  5351. }
  5352. /**
  5353. * i40e_configure_queue_channels - Add queue channel for the given TCs
  5354. * @vsi: VSI to be configured
  5355. *
  5356. * Configures queue channel mapping to the given TCs
  5357. **/
  5358. static int i40e_configure_queue_channels(struct i40e_vsi *vsi)
  5359. {
  5360. struct i40e_channel *ch;
  5361. u64 max_rate = 0;
  5362. int ret = 0, i;
  5363. /* Create app vsi with the TCs. Main VSI with TC0 is already set up */
  5364. vsi->tc_seid_map[0] = vsi->seid;
  5365. for (i = 1; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  5366. if (vsi->tc_config.enabled_tc & BIT(i)) {
  5367. ch = kzalloc(sizeof(*ch), GFP_KERNEL);
  5368. if (!ch) {
  5369. ret = -ENOMEM;
  5370. goto err_free;
  5371. }
  5372. INIT_LIST_HEAD(&ch->list);
  5373. ch->num_queue_pairs =
  5374. vsi->tc_config.tc_info[i].qcount;
  5375. ch->base_queue =
  5376. vsi->tc_config.tc_info[i].qoffset;
  5377. /* Bandwidth limit through tc interface is in bytes/s,
  5378. * change to Mbit/s
  5379. */
  5380. max_rate = vsi->mqprio_qopt.max_rate[i];
  5381. do_div(max_rate, I40E_BW_MBPS_DIVISOR);
  5382. ch->max_tx_rate = max_rate;
  5383. list_add_tail(&ch->list, &vsi->ch_list);
  5384. ret = i40e_create_queue_channel(vsi, ch);
  5385. if (ret) {
  5386. dev_err(&vsi->back->pdev->dev,
  5387. "Failed creating queue channel with TC%d: queues %d\n",
  5388. i, ch->num_queue_pairs);
  5389. goto err_free;
  5390. }
  5391. vsi->tc_seid_map[i] = ch->seid;
  5392. }
  5393. }
  5394. return ret;
  5395. err_free:
  5396. i40e_remove_queue_channels(vsi);
  5397. return ret;
  5398. }
  5399. /**
  5400. * i40e_veb_config_tc - Configure TCs for given VEB
  5401. * @veb: given VEB
  5402. * @enabled_tc: TC bitmap
  5403. *
  5404. * Configures given TC bitmap for VEB (switching) element
  5405. **/
  5406. int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
  5407. {
  5408. struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
  5409. struct i40e_pf *pf = veb->pf;
  5410. int ret = 0;
  5411. int i;
  5412. /* No TCs or already enabled TCs just return */
  5413. if (!enabled_tc || veb->enabled_tc == enabled_tc)
  5414. return ret;
  5415. bw_data.tc_valid_bits = enabled_tc;
  5416. /* bw_data.absolute_credits is not set (relative) */
  5417. /* Enable ETS TCs with equal BW Share for now */
  5418. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  5419. if (enabled_tc & BIT(i))
  5420. bw_data.tc_bw_share_credits[i] = 1;
  5421. }
  5422. ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
  5423. &bw_data, NULL);
  5424. if (ret) {
  5425. dev_info(&pf->pdev->dev,
  5426. "VEB bw config failed, err %s aq_err %s\n",
  5427. i40e_stat_str(&pf->hw, ret),
  5428. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  5429. goto out;
  5430. }
  5431. /* Update the BW information */
  5432. ret = i40e_veb_get_bw_info(veb);
  5433. if (ret) {
  5434. dev_info(&pf->pdev->dev,
  5435. "Failed getting veb bw config, err %s aq_err %s\n",
  5436. i40e_stat_str(&pf->hw, ret),
  5437. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  5438. }
  5439. out:
  5440. return ret;
  5441. }
  5442. #ifdef CONFIG_I40E_DCB
  5443. /**
  5444. * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
  5445. * @pf: PF struct
  5446. *
  5447. * Reconfigure VEB/VSIs on a given PF; it is assumed that
  5448. * the caller would've quiesce all the VSIs before calling
  5449. * this function
  5450. **/
  5451. static void i40e_dcb_reconfigure(struct i40e_pf *pf)
  5452. {
  5453. u8 tc_map = 0;
  5454. int ret;
  5455. u8 v;
  5456. /* Enable the TCs available on PF to all VEBs */
  5457. tc_map = i40e_pf_get_tc_map(pf);
  5458. for (v = 0; v < I40E_MAX_VEB; v++) {
  5459. if (!pf->veb[v])
  5460. continue;
  5461. ret = i40e_veb_config_tc(pf->veb[v], tc_map);
  5462. if (ret) {
  5463. dev_info(&pf->pdev->dev,
  5464. "Failed configuring TC for VEB seid=%d\n",
  5465. pf->veb[v]->seid);
  5466. /* Will try to configure as many components */
  5467. }
  5468. }
  5469. /* Update each VSI */
  5470. for (v = 0; v < pf->num_alloc_vsi; v++) {
  5471. if (!pf->vsi[v])
  5472. continue;
  5473. /* - Enable all TCs for the LAN VSI
  5474. * - For all others keep them at TC0 for now
  5475. */
  5476. if (v == pf->lan_vsi)
  5477. tc_map = i40e_pf_get_tc_map(pf);
  5478. else
  5479. tc_map = I40E_DEFAULT_TRAFFIC_CLASS;
  5480. ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
  5481. if (ret) {
  5482. dev_info(&pf->pdev->dev,
  5483. "Failed configuring TC for VSI seid=%d\n",
  5484. pf->vsi[v]->seid);
  5485. /* Will try to configure as many components */
  5486. } else {
  5487. /* Re-configure VSI vectors based on updated TC map */
  5488. i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
  5489. if (pf->vsi[v]->netdev)
  5490. i40e_dcbnl_set_all(pf->vsi[v]);
  5491. }
  5492. }
  5493. }
  5494. /**
  5495. * i40e_resume_port_tx - Resume port Tx
  5496. * @pf: PF struct
  5497. *
  5498. * Resume a port's Tx and issue a PF reset in case of failure to
  5499. * resume.
  5500. **/
  5501. static int i40e_resume_port_tx(struct i40e_pf *pf)
  5502. {
  5503. struct i40e_hw *hw = &pf->hw;
  5504. int ret;
  5505. ret = i40e_aq_resume_port_tx(hw, NULL);
  5506. if (ret) {
  5507. dev_info(&pf->pdev->dev,
  5508. "Resume Port Tx failed, err %s aq_err %s\n",
  5509. i40e_stat_str(&pf->hw, ret),
  5510. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  5511. /* Schedule PF reset to recover */
  5512. set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
  5513. i40e_service_event_schedule(pf);
  5514. }
  5515. return ret;
  5516. }
  5517. /**
  5518. * i40e_init_pf_dcb - Initialize DCB configuration
  5519. * @pf: PF being configured
  5520. *
  5521. * Query the current DCB configuration and cache it
  5522. * in the hardware structure
  5523. **/
  5524. static int i40e_init_pf_dcb(struct i40e_pf *pf)
  5525. {
  5526. struct i40e_hw *hw = &pf->hw;
  5527. int err = 0;
  5528. /* Do not enable DCB for SW1 and SW2 images even if the FW is capable
  5529. * Also do not enable DCBx if FW LLDP agent is disabled
  5530. */
  5531. if ((pf->hw_features & I40E_HW_NO_DCB_SUPPORT) ||
  5532. (pf->flags & I40E_FLAG_DISABLE_FW_LLDP))
  5533. goto out;
  5534. /* Get the initial DCB configuration */
  5535. err = i40e_init_dcb(hw);
  5536. if (!err) {
  5537. /* Device/Function is not DCBX capable */
  5538. if ((!hw->func_caps.dcb) ||
  5539. (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
  5540. dev_info(&pf->pdev->dev,
  5541. "DCBX offload is not supported or is disabled for this PF.\n");
  5542. } else {
  5543. /* When status is not DISABLED then DCBX in FW */
  5544. pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
  5545. DCB_CAP_DCBX_VER_IEEE;
  5546. pf->flags |= I40E_FLAG_DCB_CAPABLE;
  5547. /* Enable DCB tagging only when more than one TC
  5548. * or explicitly disable if only one TC
  5549. */
  5550. if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
  5551. pf->flags |= I40E_FLAG_DCB_ENABLED;
  5552. else
  5553. pf->flags &= ~I40E_FLAG_DCB_ENABLED;
  5554. dev_dbg(&pf->pdev->dev,
  5555. "DCBX offload is supported for this PF.\n");
  5556. }
  5557. } else if (pf->hw.aq.asq_last_status == I40E_AQ_RC_EPERM) {
  5558. dev_info(&pf->pdev->dev, "FW LLDP disabled for this PF.\n");
  5559. pf->flags |= I40E_FLAG_DISABLE_FW_LLDP;
  5560. } else {
  5561. dev_info(&pf->pdev->dev,
  5562. "Query for DCB configuration failed, err %s aq_err %s\n",
  5563. i40e_stat_str(&pf->hw, err),
  5564. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  5565. }
  5566. out:
  5567. return err;
  5568. }
  5569. #endif /* CONFIG_I40E_DCB */
  5570. #define SPEED_SIZE 14
  5571. #define FC_SIZE 8
  5572. /**
  5573. * i40e_print_link_message - print link up or down
  5574. * @vsi: the VSI for which link needs a message
  5575. * @isup: true of link is up, false otherwise
  5576. */
  5577. void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
  5578. {
  5579. enum i40e_aq_link_speed new_speed;
  5580. struct i40e_pf *pf = vsi->back;
  5581. char *speed = "Unknown";
  5582. char *fc = "Unknown";
  5583. char *fec = "";
  5584. char *req_fec = "";
  5585. char *an = "";
  5586. new_speed = pf->hw.phy.link_info.link_speed;
  5587. if ((vsi->current_isup == isup) && (vsi->current_speed == new_speed))
  5588. return;
  5589. vsi->current_isup = isup;
  5590. vsi->current_speed = new_speed;
  5591. if (!isup) {
  5592. netdev_info(vsi->netdev, "NIC Link is Down\n");
  5593. return;
  5594. }
  5595. /* Warn user if link speed on NPAR enabled partition is not at
  5596. * least 10GB
  5597. */
  5598. if (pf->hw.func_caps.npar_enable &&
  5599. (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
  5600. pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
  5601. netdev_warn(vsi->netdev,
  5602. "The partition detected link speed that is less than 10Gbps\n");
  5603. switch (pf->hw.phy.link_info.link_speed) {
  5604. case I40E_LINK_SPEED_40GB:
  5605. speed = "40 G";
  5606. break;
  5607. case I40E_LINK_SPEED_20GB:
  5608. speed = "20 G";
  5609. break;
  5610. case I40E_LINK_SPEED_25GB:
  5611. speed = "25 G";
  5612. break;
  5613. case I40E_LINK_SPEED_10GB:
  5614. speed = "10 G";
  5615. break;
  5616. case I40E_LINK_SPEED_1GB:
  5617. speed = "1000 M";
  5618. break;
  5619. case I40E_LINK_SPEED_100MB:
  5620. speed = "100 M";
  5621. break;
  5622. default:
  5623. break;
  5624. }
  5625. switch (pf->hw.fc.current_mode) {
  5626. case I40E_FC_FULL:
  5627. fc = "RX/TX";
  5628. break;
  5629. case I40E_FC_TX_PAUSE:
  5630. fc = "TX";
  5631. break;
  5632. case I40E_FC_RX_PAUSE:
  5633. fc = "RX";
  5634. break;
  5635. default:
  5636. fc = "None";
  5637. break;
  5638. }
  5639. if (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_25GB) {
  5640. req_fec = ", Requested FEC: None";
  5641. fec = ", FEC: None";
  5642. an = ", Autoneg: False";
  5643. if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
  5644. an = ", Autoneg: True";
  5645. if (pf->hw.phy.link_info.fec_info &
  5646. I40E_AQ_CONFIG_FEC_KR_ENA)
  5647. fec = ", FEC: CL74 FC-FEC/BASE-R";
  5648. else if (pf->hw.phy.link_info.fec_info &
  5649. I40E_AQ_CONFIG_FEC_RS_ENA)
  5650. fec = ", FEC: CL108 RS-FEC";
  5651. /* 'CL108 RS-FEC' should be displayed when RS is requested, or
  5652. * both RS and FC are requested
  5653. */
  5654. if (vsi->back->hw.phy.link_info.req_fec_info &
  5655. (I40E_AQ_REQUEST_FEC_KR | I40E_AQ_REQUEST_FEC_RS)) {
  5656. if (vsi->back->hw.phy.link_info.req_fec_info &
  5657. I40E_AQ_REQUEST_FEC_RS)
  5658. req_fec = ", Requested FEC: CL108 RS-FEC";
  5659. else
  5660. req_fec = ", Requested FEC: CL74 FC-FEC/BASE-R";
  5661. }
  5662. }
  5663. netdev_info(vsi->netdev, "NIC Link is Up, %sbps Full Duplex%s%s%s, Flow Control: %s\n",
  5664. speed, req_fec, fec, an, fc);
  5665. }
  5666. /**
  5667. * i40e_up_complete - Finish the last steps of bringing up a connection
  5668. * @vsi: the VSI being configured
  5669. **/
  5670. static int i40e_up_complete(struct i40e_vsi *vsi)
  5671. {
  5672. struct i40e_pf *pf = vsi->back;
  5673. int err;
  5674. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  5675. i40e_vsi_configure_msix(vsi);
  5676. else
  5677. i40e_configure_msi_and_legacy(vsi);
  5678. /* start rings */
  5679. err = i40e_vsi_start_rings(vsi);
  5680. if (err)
  5681. return err;
  5682. clear_bit(__I40E_VSI_DOWN, vsi->state);
  5683. i40e_napi_enable_all(vsi);
  5684. i40e_vsi_enable_irq(vsi);
  5685. if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
  5686. (vsi->netdev)) {
  5687. i40e_print_link_message(vsi, true);
  5688. netif_tx_start_all_queues(vsi->netdev);
  5689. netif_carrier_on(vsi->netdev);
  5690. }
  5691. /* replay FDIR SB filters */
  5692. if (vsi->type == I40E_VSI_FDIR) {
  5693. /* reset fd counters */
  5694. pf->fd_add_err = 0;
  5695. pf->fd_atr_cnt = 0;
  5696. i40e_fdir_filter_restore(vsi);
  5697. }
  5698. /* On the next run of the service_task, notify any clients of the new
  5699. * opened netdev
  5700. */
  5701. set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
  5702. i40e_service_event_schedule(pf);
  5703. return 0;
  5704. }
  5705. /**
  5706. * i40e_vsi_reinit_locked - Reset the VSI
  5707. * @vsi: the VSI being configured
  5708. *
  5709. * Rebuild the ring structs after some configuration
  5710. * has changed, e.g. MTU size.
  5711. **/
  5712. static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
  5713. {
  5714. struct i40e_pf *pf = vsi->back;
  5715. WARN_ON(in_interrupt());
  5716. while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state))
  5717. usleep_range(1000, 2000);
  5718. i40e_down(vsi);
  5719. i40e_up(vsi);
  5720. clear_bit(__I40E_CONFIG_BUSY, pf->state);
  5721. }
  5722. /**
  5723. * i40e_up - Bring the connection back up after being down
  5724. * @vsi: the VSI being configured
  5725. **/
  5726. int i40e_up(struct i40e_vsi *vsi)
  5727. {
  5728. int err;
  5729. err = i40e_vsi_configure(vsi);
  5730. if (!err)
  5731. err = i40e_up_complete(vsi);
  5732. return err;
  5733. }
  5734. /**
  5735. * i40e_force_link_state - Force the link status
  5736. * @pf: board private structure
  5737. * @is_up: whether the link state should be forced up or down
  5738. **/
  5739. static i40e_status i40e_force_link_state(struct i40e_pf *pf, bool is_up)
  5740. {
  5741. struct i40e_aq_get_phy_abilities_resp abilities;
  5742. struct i40e_aq_set_phy_config config = {0};
  5743. struct i40e_hw *hw = &pf->hw;
  5744. i40e_status err;
  5745. u64 mask;
  5746. /* Get the current phy config */
  5747. err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities,
  5748. NULL);
  5749. if (err) {
  5750. dev_err(&pf->pdev->dev,
  5751. "failed to get phy cap., ret = %s last_status = %s\n",
  5752. i40e_stat_str(hw, err),
  5753. i40e_aq_str(hw, hw->aq.asq_last_status));
  5754. return err;
  5755. }
  5756. /* If link needs to go up, but was not forced to go down,
  5757. * no need for a flap
  5758. */
  5759. if (is_up && abilities.phy_type != 0)
  5760. return I40E_SUCCESS;
  5761. /* To force link we need to set bits for all supported PHY types,
  5762. * but there are now more than 32, so we need to split the bitmap
  5763. * across two fields.
  5764. */
  5765. mask = I40E_PHY_TYPES_BITMASK;
  5766. config.phy_type = is_up ? cpu_to_le32((u32)(mask & 0xffffffff)) : 0;
  5767. config.phy_type_ext = is_up ? (u8)((mask >> 32) & 0xff) : 0;
  5768. /* Copy the old settings, except of phy_type */
  5769. config.abilities = abilities.abilities;
  5770. config.link_speed = abilities.link_speed;
  5771. config.eee_capability = abilities.eee_capability;
  5772. config.eeer = abilities.eeer_val;
  5773. config.low_power_ctrl = abilities.d3_lpan;
  5774. config.fec_config = abilities.fec_cfg_curr_mod_ext_info &
  5775. I40E_AQ_PHY_FEC_CONFIG_MASK;
  5776. err = i40e_aq_set_phy_config(hw, &config, NULL);
  5777. if (err) {
  5778. dev_err(&pf->pdev->dev,
  5779. "set phy config ret = %s last_status = %s\n",
  5780. i40e_stat_str(&pf->hw, err),
  5781. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  5782. return err;
  5783. }
  5784. /* Update the link info */
  5785. err = i40e_update_link_info(hw);
  5786. if (err) {
  5787. /* Wait a little bit (on 40G cards it sometimes takes a really
  5788. * long time for link to come back from the atomic reset)
  5789. * and try once more
  5790. */
  5791. msleep(1000);
  5792. i40e_update_link_info(hw);
  5793. }
  5794. i40e_aq_set_link_restart_an(hw, true, NULL);
  5795. return I40E_SUCCESS;
  5796. }
  5797. /**
  5798. * i40e_down - Shutdown the connection processing
  5799. * @vsi: the VSI being stopped
  5800. **/
  5801. void i40e_down(struct i40e_vsi *vsi)
  5802. {
  5803. int i;
  5804. /* It is assumed that the caller of this function
  5805. * sets the vsi->state __I40E_VSI_DOWN bit.
  5806. */
  5807. if (vsi->netdev) {
  5808. netif_carrier_off(vsi->netdev);
  5809. netif_tx_disable(vsi->netdev);
  5810. }
  5811. i40e_vsi_disable_irq(vsi);
  5812. i40e_vsi_stop_rings(vsi);
  5813. if (vsi->type == I40E_VSI_MAIN &&
  5814. vsi->back->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED)
  5815. i40e_force_link_state(vsi->back, false);
  5816. i40e_napi_disable_all(vsi);
  5817. for (i = 0; i < vsi->num_queue_pairs; i++) {
  5818. i40e_clean_tx_ring(vsi->tx_rings[i]);
  5819. if (i40e_enabled_xdp_vsi(vsi))
  5820. i40e_clean_tx_ring(vsi->xdp_rings[i]);
  5821. i40e_clean_rx_ring(vsi->rx_rings[i]);
  5822. }
  5823. }
  5824. /**
  5825. * i40e_validate_mqprio_qopt- validate queue mapping info
  5826. * @vsi: the VSI being configured
  5827. * @mqprio_qopt: queue parametrs
  5828. **/
  5829. static int i40e_validate_mqprio_qopt(struct i40e_vsi *vsi,
  5830. struct tc_mqprio_qopt_offload *mqprio_qopt)
  5831. {
  5832. u64 sum_max_rate = 0;
  5833. u64 max_rate = 0;
  5834. int i;
  5835. if (mqprio_qopt->qopt.offset[0] != 0 ||
  5836. mqprio_qopt->qopt.num_tc < 1 ||
  5837. mqprio_qopt->qopt.num_tc > I40E_MAX_TRAFFIC_CLASS)
  5838. return -EINVAL;
  5839. for (i = 0; ; i++) {
  5840. if (!mqprio_qopt->qopt.count[i])
  5841. return -EINVAL;
  5842. if (mqprio_qopt->min_rate[i]) {
  5843. dev_err(&vsi->back->pdev->dev,
  5844. "Invalid min tx rate (greater than 0) specified\n");
  5845. return -EINVAL;
  5846. }
  5847. max_rate = mqprio_qopt->max_rate[i];
  5848. do_div(max_rate, I40E_BW_MBPS_DIVISOR);
  5849. sum_max_rate += max_rate;
  5850. if (i >= mqprio_qopt->qopt.num_tc - 1)
  5851. break;
  5852. if (mqprio_qopt->qopt.offset[i + 1] !=
  5853. (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i]))
  5854. return -EINVAL;
  5855. }
  5856. if (vsi->num_queue_pairs <
  5857. (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i])) {
  5858. return -EINVAL;
  5859. }
  5860. if (sum_max_rate > i40e_get_link_speed(vsi)) {
  5861. dev_err(&vsi->back->pdev->dev,
  5862. "Invalid max tx rate specified\n");
  5863. return -EINVAL;
  5864. }
  5865. return 0;
  5866. }
  5867. /**
  5868. * i40e_vsi_set_default_tc_config - set default values for tc configuration
  5869. * @vsi: the VSI being configured
  5870. **/
  5871. static void i40e_vsi_set_default_tc_config(struct i40e_vsi *vsi)
  5872. {
  5873. u16 qcount;
  5874. int i;
  5875. /* Only TC0 is enabled */
  5876. vsi->tc_config.numtc = 1;
  5877. vsi->tc_config.enabled_tc = 1;
  5878. qcount = min_t(int, vsi->alloc_queue_pairs,
  5879. i40e_pf_get_max_q_per_tc(vsi->back));
  5880. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  5881. /* For the TC that is not enabled set the offset to to default
  5882. * queue and allocate one queue for the given TC.
  5883. */
  5884. vsi->tc_config.tc_info[i].qoffset = 0;
  5885. if (i == 0)
  5886. vsi->tc_config.tc_info[i].qcount = qcount;
  5887. else
  5888. vsi->tc_config.tc_info[i].qcount = 1;
  5889. vsi->tc_config.tc_info[i].netdev_tc = 0;
  5890. }
  5891. }
  5892. /**
  5893. * i40e_setup_tc - configure multiple traffic classes
  5894. * @netdev: net device to configure
  5895. * @type_data: tc offload data
  5896. **/
  5897. static int i40e_setup_tc(struct net_device *netdev, void *type_data)
  5898. {
  5899. struct tc_mqprio_qopt_offload *mqprio_qopt = type_data;
  5900. struct i40e_netdev_priv *np = netdev_priv(netdev);
  5901. struct i40e_vsi *vsi = np->vsi;
  5902. struct i40e_pf *pf = vsi->back;
  5903. u8 enabled_tc = 0, num_tc, hw;
  5904. bool need_reset = false;
  5905. int ret = -EINVAL;
  5906. u16 mode;
  5907. int i;
  5908. num_tc = mqprio_qopt->qopt.num_tc;
  5909. hw = mqprio_qopt->qopt.hw;
  5910. mode = mqprio_qopt->mode;
  5911. if (!hw) {
  5912. pf->flags &= ~I40E_FLAG_TC_MQPRIO;
  5913. memcpy(&vsi->mqprio_qopt, mqprio_qopt, sizeof(*mqprio_qopt));
  5914. goto config_tc;
  5915. }
  5916. /* Check if MFP enabled */
  5917. if (pf->flags & I40E_FLAG_MFP_ENABLED) {
  5918. netdev_info(netdev,
  5919. "Configuring TC not supported in MFP mode\n");
  5920. return ret;
  5921. }
  5922. switch (mode) {
  5923. case TC_MQPRIO_MODE_DCB:
  5924. pf->flags &= ~I40E_FLAG_TC_MQPRIO;
  5925. /* Check if DCB enabled to continue */
  5926. if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
  5927. netdev_info(netdev,
  5928. "DCB is not enabled for adapter\n");
  5929. return ret;
  5930. }
  5931. /* Check whether tc count is within enabled limit */
  5932. if (num_tc > i40e_pf_get_num_tc(pf)) {
  5933. netdev_info(netdev,
  5934. "TC count greater than enabled on link for adapter\n");
  5935. return ret;
  5936. }
  5937. break;
  5938. case TC_MQPRIO_MODE_CHANNEL:
  5939. if (pf->flags & I40E_FLAG_DCB_ENABLED) {
  5940. netdev_info(netdev,
  5941. "Full offload of TC Mqprio options is not supported when DCB is enabled\n");
  5942. return ret;
  5943. }
  5944. if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
  5945. return ret;
  5946. ret = i40e_validate_mqprio_qopt(vsi, mqprio_qopt);
  5947. if (ret)
  5948. return ret;
  5949. memcpy(&vsi->mqprio_qopt, mqprio_qopt,
  5950. sizeof(*mqprio_qopt));
  5951. pf->flags |= I40E_FLAG_TC_MQPRIO;
  5952. pf->flags &= ~I40E_FLAG_DCB_ENABLED;
  5953. break;
  5954. default:
  5955. return -EINVAL;
  5956. }
  5957. config_tc:
  5958. /* Generate TC map for number of tc requested */
  5959. for (i = 0; i < num_tc; i++)
  5960. enabled_tc |= BIT(i);
  5961. /* Requesting same TC configuration as already enabled */
  5962. if (enabled_tc == vsi->tc_config.enabled_tc &&
  5963. mode != TC_MQPRIO_MODE_CHANNEL)
  5964. return 0;
  5965. /* Quiesce VSI queues */
  5966. i40e_quiesce_vsi(vsi);
  5967. if (!hw && !(pf->flags & I40E_FLAG_TC_MQPRIO))
  5968. i40e_remove_queue_channels(vsi);
  5969. /* Configure VSI for enabled TCs */
  5970. ret = i40e_vsi_config_tc(vsi, enabled_tc);
  5971. if (ret) {
  5972. netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
  5973. vsi->seid);
  5974. need_reset = true;
  5975. goto exit;
  5976. }
  5977. if (pf->flags & I40E_FLAG_TC_MQPRIO) {
  5978. if (vsi->mqprio_qopt.max_rate[0]) {
  5979. u64 max_tx_rate = vsi->mqprio_qopt.max_rate[0];
  5980. do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
  5981. ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
  5982. if (!ret) {
  5983. u64 credits = max_tx_rate;
  5984. do_div(credits, I40E_BW_CREDIT_DIVISOR);
  5985. dev_dbg(&vsi->back->pdev->dev,
  5986. "Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
  5987. max_tx_rate,
  5988. credits,
  5989. vsi->seid);
  5990. } else {
  5991. need_reset = true;
  5992. goto exit;
  5993. }
  5994. }
  5995. ret = i40e_configure_queue_channels(vsi);
  5996. if (ret) {
  5997. netdev_info(netdev,
  5998. "Failed configuring queue channels\n");
  5999. need_reset = true;
  6000. goto exit;
  6001. }
  6002. }
  6003. exit:
  6004. /* Reset the configuration data to defaults, only TC0 is enabled */
  6005. if (need_reset) {
  6006. i40e_vsi_set_default_tc_config(vsi);
  6007. need_reset = false;
  6008. }
  6009. /* Unquiesce VSI */
  6010. i40e_unquiesce_vsi(vsi);
  6011. return ret;
  6012. }
  6013. /**
  6014. * i40e_set_cld_element - sets cloud filter element data
  6015. * @filter: cloud filter rule
  6016. * @cld: ptr to cloud filter element data
  6017. *
  6018. * This is helper function to copy data into cloud filter element
  6019. **/
  6020. static inline void
  6021. i40e_set_cld_element(struct i40e_cloud_filter *filter,
  6022. struct i40e_aqc_cloud_filters_element_data *cld)
  6023. {
  6024. int i, j;
  6025. u32 ipa;
  6026. memset(cld, 0, sizeof(*cld));
  6027. ether_addr_copy(cld->outer_mac, filter->dst_mac);
  6028. ether_addr_copy(cld->inner_mac, filter->src_mac);
  6029. if (filter->n_proto != ETH_P_IP && filter->n_proto != ETH_P_IPV6)
  6030. return;
  6031. if (filter->n_proto == ETH_P_IPV6) {
  6032. #define IPV6_MAX_INDEX (ARRAY_SIZE(filter->dst_ipv6) - 1)
  6033. for (i = 0, j = 0; i < ARRAY_SIZE(filter->dst_ipv6);
  6034. i++, j += 2) {
  6035. ipa = be32_to_cpu(filter->dst_ipv6[IPV6_MAX_INDEX - i]);
  6036. ipa = cpu_to_le32(ipa);
  6037. memcpy(&cld->ipaddr.raw_v6.data[j], &ipa, sizeof(ipa));
  6038. }
  6039. } else {
  6040. ipa = be32_to_cpu(filter->dst_ipv4);
  6041. memcpy(&cld->ipaddr.v4.data, &ipa, sizeof(ipa));
  6042. }
  6043. cld->inner_vlan = cpu_to_le16(ntohs(filter->vlan_id));
  6044. /* tenant_id is not supported by FW now, once the support is enabled
  6045. * fill the cld->tenant_id with cpu_to_le32(filter->tenant_id)
  6046. */
  6047. if (filter->tenant_id)
  6048. return;
  6049. }
  6050. /**
  6051. * i40e_add_del_cloud_filter - Add/del cloud filter
  6052. * @vsi: pointer to VSI
  6053. * @filter: cloud filter rule
  6054. * @add: if true, add, if false, delete
  6055. *
  6056. * Add or delete a cloud filter for a specific flow spec.
  6057. * Returns 0 if the filter were successfully added.
  6058. **/
  6059. int i40e_add_del_cloud_filter(struct i40e_vsi *vsi,
  6060. struct i40e_cloud_filter *filter, bool add)
  6061. {
  6062. struct i40e_aqc_cloud_filters_element_data cld_filter;
  6063. struct i40e_pf *pf = vsi->back;
  6064. int ret;
  6065. static const u16 flag_table[128] = {
  6066. [I40E_CLOUD_FILTER_FLAGS_OMAC] =
  6067. I40E_AQC_ADD_CLOUD_FILTER_OMAC,
  6068. [I40E_CLOUD_FILTER_FLAGS_IMAC] =
  6069. I40E_AQC_ADD_CLOUD_FILTER_IMAC,
  6070. [I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN] =
  6071. I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN,
  6072. [I40E_CLOUD_FILTER_FLAGS_IMAC_TEN_ID] =
  6073. I40E_AQC_ADD_CLOUD_FILTER_IMAC_TEN_ID,
  6074. [I40E_CLOUD_FILTER_FLAGS_OMAC_TEN_ID_IMAC] =
  6075. I40E_AQC_ADD_CLOUD_FILTER_OMAC_TEN_ID_IMAC,
  6076. [I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN_TEN_ID] =
  6077. I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN_TEN_ID,
  6078. [I40E_CLOUD_FILTER_FLAGS_IIP] =
  6079. I40E_AQC_ADD_CLOUD_FILTER_IIP,
  6080. };
  6081. if (filter->flags >= ARRAY_SIZE(flag_table))
  6082. return I40E_ERR_CONFIG;
  6083. /* copy element needed to add cloud filter from filter */
  6084. i40e_set_cld_element(filter, &cld_filter);
  6085. if (filter->tunnel_type != I40E_CLOUD_TNL_TYPE_NONE)
  6086. cld_filter.flags = cpu_to_le16(filter->tunnel_type <<
  6087. I40E_AQC_ADD_CLOUD_TNL_TYPE_SHIFT);
  6088. if (filter->n_proto == ETH_P_IPV6)
  6089. cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
  6090. I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
  6091. else
  6092. cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
  6093. I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
  6094. if (add)
  6095. ret = i40e_aq_add_cloud_filters(&pf->hw, filter->seid,
  6096. &cld_filter, 1);
  6097. else
  6098. ret = i40e_aq_rem_cloud_filters(&pf->hw, filter->seid,
  6099. &cld_filter, 1);
  6100. if (ret)
  6101. dev_dbg(&pf->pdev->dev,
  6102. "Failed to %s cloud filter using l4 port %u, err %d aq_err %d\n",
  6103. add ? "add" : "delete", filter->dst_port, ret,
  6104. pf->hw.aq.asq_last_status);
  6105. else
  6106. dev_info(&pf->pdev->dev,
  6107. "%s cloud filter for VSI: %d\n",
  6108. add ? "Added" : "Deleted", filter->seid);
  6109. return ret;
  6110. }
  6111. /**
  6112. * i40e_add_del_cloud_filter_big_buf - Add/del cloud filter using big_buf
  6113. * @vsi: pointer to VSI
  6114. * @filter: cloud filter rule
  6115. * @add: if true, add, if false, delete
  6116. *
  6117. * Add or delete a cloud filter for a specific flow spec using big buffer.
  6118. * Returns 0 if the filter were successfully added.
  6119. **/
  6120. int i40e_add_del_cloud_filter_big_buf(struct i40e_vsi *vsi,
  6121. struct i40e_cloud_filter *filter,
  6122. bool add)
  6123. {
  6124. struct i40e_aqc_cloud_filters_element_bb cld_filter;
  6125. struct i40e_pf *pf = vsi->back;
  6126. int ret;
  6127. /* Both (src/dst) valid mac_addr are not supported */
  6128. if ((is_valid_ether_addr(filter->dst_mac) &&
  6129. is_valid_ether_addr(filter->src_mac)) ||
  6130. (is_multicast_ether_addr(filter->dst_mac) &&
  6131. is_multicast_ether_addr(filter->src_mac)))
  6132. return -EOPNOTSUPP;
  6133. /* Big buffer cloud filter needs 'L4 port' to be non-zero. Also, UDP
  6134. * ports are not supported via big buffer now.
  6135. */
  6136. if (!filter->dst_port || filter->ip_proto == IPPROTO_UDP)
  6137. return -EOPNOTSUPP;
  6138. /* adding filter using src_port/src_ip is not supported at this stage */
  6139. if (filter->src_port || filter->src_ipv4 ||
  6140. !ipv6_addr_any(&filter->ip.v6.src_ip6))
  6141. return -EOPNOTSUPP;
  6142. /* copy element needed to add cloud filter from filter */
  6143. i40e_set_cld_element(filter, &cld_filter.element);
  6144. if (is_valid_ether_addr(filter->dst_mac) ||
  6145. is_valid_ether_addr(filter->src_mac) ||
  6146. is_multicast_ether_addr(filter->dst_mac) ||
  6147. is_multicast_ether_addr(filter->src_mac)) {
  6148. /* MAC + IP : unsupported mode */
  6149. if (filter->dst_ipv4)
  6150. return -EOPNOTSUPP;
  6151. /* since we validated that L4 port must be valid before
  6152. * we get here, start with respective "flags" value
  6153. * and update if vlan is present or not
  6154. */
  6155. cld_filter.element.flags =
  6156. cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_PORT);
  6157. if (filter->vlan_id) {
  6158. cld_filter.element.flags =
  6159. cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_VLAN_PORT);
  6160. }
  6161. } else if (filter->dst_ipv4 ||
  6162. !ipv6_addr_any(&filter->ip.v6.dst_ip6)) {
  6163. cld_filter.element.flags =
  6164. cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_IP_PORT);
  6165. if (filter->n_proto == ETH_P_IPV6)
  6166. cld_filter.element.flags |=
  6167. cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
  6168. else
  6169. cld_filter.element.flags |=
  6170. cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
  6171. } else {
  6172. dev_err(&pf->pdev->dev,
  6173. "either mac or ip has to be valid for cloud filter\n");
  6174. return -EINVAL;
  6175. }
  6176. /* Now copy L4 port in Byte 6..7 in general fields */
  6177. cld_filter.general_fields[I40E_AQC_ADD_CLOUD_FV_FLU_0X16_WORD0] =
  6178. be16_to_cpu(filter->dst_port);
  6179. if (add) {
  6180. /* Validate current device switch mode, change if necessary */
  6181. ret = i40e_validate_and_set_switch_mode(vsi);
  6182. if (ret) {
  6183. dev_err(&pf->pdev->dev,
  6184. "failed to set switch mode, ret %d\n",
  6185. ret);
  6186. return ret;
  6187. }
  6188. ret = i40e_aq_add_cloud_filters_bb(&pf->hw, filter->seid,
  6189. &cld_filter, 1);
  6190. } else {
  6191. ret = i40e_aq_rem_cloud_filters_bb(&pf->hw, filter->seid,
  6192. &cld_filter, 1);
  6193. }
  6194. if (ret)
  6195. dev_dbg(&pf->pdev->dev,
  6196. "Failed to %s cloud filter(big buffer) err %d aq_err %d\n",
  6197. add ? "add" : "delete", ret, pf->hw.aq.asq_last_status);
  6198. else
  6199. dev_info(&pf->pdev->dev,
  6200. "%s cloud filter for VSI: %d, L4 port: %d\n",
  6201. add ? "add" : "delete", filter->seid,
  6202. ntohs(filter->dst_port));
  6203. return ret;
  6204. }
  6205. /**
  6206. * i40e_parse_cls_flower - Parse tc flower filters provided by kernel
  6207. * @vsi: Pointer to VSI
  6208. * @cls_flower: Pointer to struct tc_cls_flower_offload
  6209. * @filter: Pointer to cloud filter structure
  6210. *
  6211. **/
  6212. static int i40e_parse_cls_flower(struct i40e_vsi *vsi,
  6213. struct tc_cls_flower_offload *f,
  6214. struct i40e_cloud_filter *filter)
  6215. {
  6216. u16 n_proto_mask = 0, n_proto_key = 0, addr_type = 0;
  6217. struct i40e_pf *pf = vsi->back;
  6218. u8 field_flags = 0;
  6219. if (f->dissector->used_keys &
  6220. ~(BIT(FLOW_DISSECTOR_KEY_CONTROL) |
  6221. BIT(FLOW_DISSECTOR_KEY_BASIC) |
  6222. BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
  6223. BIT(FLOW_DISSECTOR_KEY_VLAN) |
  6224. BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
  6225. BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
  6226. BIT(FLOW_DISSECTOR_KEY_PORTS) |
  6227. BIT(FLOW_DISSECTOR_KEY_ENC_KEYID))) {
  6228. dev_err(&pf->pdev->dev, "Unsupported key used: 0x%x\n",
  6229. f->dissector->used_keys);
  6230. return -EOPNOTSUPP;
  6231. }
  6232. if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
  6233. struct flow_dissector_key_keyid *key =
  6234. skb_flow_dissector_target(f->dissector,
  6235. FLOW_DISSECTOR_KEY_ENC_KEYID,
  6236. f->key);
  6237. struct flow_dissector_key_keyid *mask =
  6238. skb_flow_dissector_target(f->dissector,
  6239. FLOW_DISSECTOR_KEY_ENC_KEYID,
  6240. f->mask);
  6241. if (mask->keyid != 0)
  6242. field_flags |= I40E_CLOUD_FIELD_TEN_ID;
  6243. filter->tenant_id = be32_to_cpu(key->keyid);
  6244. }
  6245. if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_BASIC)) {
  6246. struct flow_dissector_key_basic *key =
  6247. skb_flow_dissector_target(f->dissector,
  6248. FLOW_DISSECTOR_KEY_BASIC,
  6249. f->key);
  6250. struct flow_dissector_key_basic *mask =
  6251. skb_flow_dissector_target(f->dissector,
  6252. FLOW_DISSECTOR_KEY_BASIC,
  6253. f->mask);
  6254. n_proto_key = ntohs(key->n_proto);
  6255. n_proto_mask = ntohs(mask->n_proto);
  6256. if (n_proto_key == ETH_P_ALL) {
  6257. n_proto_key = 0;
  6258. n_proto_mask = 0;
  6259. }
  6260. filter->n_proto = n_proto_key & n_proto_mask;
  6261. filter->ip_proto = key->ip_proto;
  6262. }
  6263. if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
  6264. struct flow_dissector_key_eth_addrs *key =
  6265. skb_flow_dissector_target(f->dissector,
  6266. FLOW_DISSECTOR_KEY_ETH_ADDRS,
  6267. f->key);
  6268. struct flow_dissector_key_eth_addrs *mask =
  6269. skb_flow_dissector_target(f->dissector,
  6270. FLOW_DISSECTOR_KEY_ETH_ADDRS,
  6271. f->mask);
  6272. /* use is_broadcast and is_zero to check for all 0xf or 0 */
  6273. if (!is_zero_ether_addr(mask->dst)) {
  6274. if (is_broadcast_ether_addr(mask->dst)) {
  6275. field_flags |= I40E_CLOUD_FIELD_OMAC;
  6276. } else {
  6277. dev_err(&pf->pdev->dev, "Bad ether dest mask %pM\n",
  6278. mask->dst);
  6279. return I40E_ERR_CONFIG;
  6280. }
  6281. }
  6282. if (!is_zero_ether_addr(mask->src)) {
  6283. if (is_broadcast_ether_addr(mask->src)) {
  6284. field_flags |= I40E_CLOUD_FIELD_IMAC;
  6285. } else {
  6286. dev_err(&pf->pdev->dev, "Bad ether src mask %pM\n",
  6287. mask->src);
  6288. return I40E_ERR_CONFIG;
  6289. }
  6290. }
  6291. ether_addr_copy(filter->dst_mac, key->dst);
  6292. ether_addr_copy(filter->src_mac, key->src);
  6293. }
  6294. if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_VLAN)) {
  6295. struct flow_dissector_key_vlan *key =
  6296. skb_flow_dissector_target(f->dissector,
  6297. FLOW_DISSECTOR_KEY_VLAN,
  6298. f->key);
  6299. struct flow_dissector_key_vlan *mask =
  6300. skb_flow_dissector_target(f->dissector,
  6301. FLOW_DISSECTOR_KEY_VLAN,
  6302. f->mask);
  6303. if (mask->vlan_id) {
  6304. if (mask->vlan_id == VLAN_VID_MASK) {
  6305. field_flags |= I40E_CLOUD_FIELD_IVLAN;
  6306. } else {
  6307. dev_err(&pf->pdev->dev, "Bad vlan mask 0x%04x\n",
  6308. mask->vlan_id);
  6309. return I40E_ERR_CONFIG;
  6310. }
  6311. }
  6312. filter->vlan_id = cpu_to_be16(key->vlan_id);
  6313. }
  6314. if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_CONTROL)) {
  6315. struct flow_dissector_key_control *key =
  6316. skb_flow_dissector_target(f->dissector,
  6317. FLOW_DISSECTOR_KEY_CONTROL,
  6318. f->key);
  6319. addr_type = key->addr_type;
  6320. }
  6321. if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
  6322. struct flow_dissector_key_ipv4_addrs *key =
  6323. skb_flow_dissector_target(f->dissector,
  6324. FLOW_DISSECTOR_KEY_IPV4_ADDRS,
  6325. f->key);
  6326. struct flow_dissector_key_ipv4_addrs *mask =
  6327. skb_flow_dissector_target(f->dissector,
  6328. FLOW_DISSECTOR_KEY_IPV4_ADDRS,
  6329. f->mask);
  6330. if (mask->dst) {
  6331. if (mask->dst == cpu_to_be32(0xffffffff)) {
  6332. field_flags |= I40E_CLOUD_FIELD_IIP;
  6333. } else {
  6334. dev_err(&pf->pdev->dev, "Bad ip dst mask %pI4b\n",
  6335. &mask->dst);
  6336. return I40E_ERR_CONFIG;
  6337. }
  6338. }
  6339. if (mask->src) {
  6340. if (mask->src == cpu_to_be32(0xffffffff)) {
  6341. field_flags |= I40E_CLOUD_FIELD_IIP;
  6342. } else {
  6343. dev_err(&pf->pdev->dev, "Bad ip src mask %pI4b\n",
  6344. &mask->src);
  6345. return I40E_ERR_CONFIG;
  6346. }
  6347. }
  6348. if (field_flags & I40E_CLOUD_FIELD_TEN_ID) {
  6349. dev_err(&pf->pdev->dev, "Tenant id not allowed for ip filter\n");
  6350. return I40E_ERR_CONFIG;
  6351. }
  6352. filter->dst_ipv4 = key->dst;
  6353. filter->src_ipv4 = key->src;
  6354. }
  6355. if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
  6356. struct flow_dissector_key_ipv6_addrs *key =
  6357. skb_flow_dissector_target(f->dissector,
  6358. FLOW_DISSECTOR_KEY_IPV6_ADDRS,
  6359. f->key);
  6360. struct flow_dissector_key_ipv6_addrs *mask =
  6361. skb_flow_dissector_target(f->dissector,
  6362. FLOW_DISSECTOR_KEY_IPV6_ADDRS,
  6363. f->mask);
  6364. /* src and dest IPV6 address should not be LOOPBACK
  6365. * (0:0:0:0:0:0:0:1), which can be represented as ::1
  6366. */
  6367. if (ipv6_addr_loopback(&key->dst) ||
  6368. ipv6_addr_loopback(&key->src)) {
  6369. dev_err(&pf->pdev->dev,
  6370. "Bad ipv6, addr is LOOPBACK\n");
  6371. return I40E_ERR_CONFIG;
  6372. }
  6373. if (!ipv6_addr_any(&mask->dst) || !ipv6_addr_any(&mask->src))
  6374. field_flags |= I40E_CLOUD_FIELD_IIP;
  6375. memcpy(&filter->src_ipv6, &key->src.s6_addr32,
  6376. sizeof(filter->src_ipv6));
  6377. memcpy(&filter->dst_ipv6, &key->dst.s6_addr32,
  6378. sizeof(filter->dst_ipv6));
  6379. }
  6380. if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_PORTS)) {
  6381. struct flow_dissector_key_ports *key =
  6382. skb_flow_dissector_target(f->dissector,
  6383. FLOW_DISSECTOR_KEY_PORTS,
  6384. f->key);
  6385. struct flow_dissector_key_ports *mask =
  6386. skb_flow_dissector_target(f->dissector,
  6387. FLOW_DISSECTOR_KEY_PORTS,
  6388. f->mask);
  6389. if (mask->src) {
  6390. if (mask->src == cpu_to_be16(0xffff)) {
  6391. field_flags |= I40E_CLOUD_FIELD_IIP;
  6392. } else {
  6393. dev_err(&pf->pdev->dev, "Bad src port mask 0x%04x\n",
  6394. be16_to_cpu(mask->src));
  6395. return I40E_ERR_CONFIG;
  6396. }
  6397. }
  6398. if (mask->dst) {
  6399. if (mask->dst == cpu_to_be16(0xffff)) {
  6400. field_flags |= I40E_CLOUD_FIELD_IIP;
  6401. } else {
  6402. dev_err(&pf->pdev->dev, "Bad dst port mask 0x%04x\n",
  6403. be16_to_cpu(mask->dst));
  6404. return I40E_ERR_CONFIG;
  6405. }
  6406. }
  6407. filter->dst_port = key->dst;
  6408. filter->src_port = key->src;
  6409. switch (filter->ip_proto) {
  6410. case IPPROTO_TCP:
  6411. case IPPROTO_UDP:
  6412. break;
  6413. default:
  6414. dev_err(&pf->pdev->dev,
  6415. "Only UDP and TCP transport are supported\n");
  6416. return -EINVAL;
  6417. }
  6418. }
  6419. filter->flags = field_flags;
  6420. return 0;
  6421. }
  6422. /**
  6423. * i40e_handle_tclass: Forward to a traffic class on the device
  6424. * @vsi: Pointer to VSI
  6425. * @tc: traffic class index on the device
  6426. * @filter: Pointer to cloud filter structure
  6427. *
  6428. **/
  6429. static int i40e_handle_tclass(struct i40e_vsi *vsi, u32 tc,
  6430. struct i40e_cloud_filter *filter)
  6431. {
  6432. struct i40e_channel *ch, *ch_tmp;
  6433. /* direct to a traffic class on the same device */
  6434. if (tc == 0) {
  6435. filter->seid = vsi->seid;
  6436. return 0;
  6437. } else if (vsi->tc_config.enabled_tc & BIT(tc)) {
  6438. if (!filter->dst_port) {
  6439. dev_err(&vsi->back->pdev->dev,
  6440. "Specify destination port to direct to traffic class that is not default\n");
  6441. return -EINVAL;
  6442. }
  6443. if (list_empty(&vsi->ch_list))
  6444. return -EINVAL;
  6445. list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list,
  6446. list) {
  6447. if (ch->seid == vsi->tc_seid_map[tc])
  6448. filter->seid = ch->seid;
  6449. }
  6450. return 0;
  6451. }
  6452. dev_err(&vsi->back->pdev->dev, "TC is not enabled\n");
  6453. return -EINVAL;
  6454. }
  6455. /**
  6456. * i40e_configure_clsflower - Configure tc flower filters
  6457. * @vsi: Pointer to VSI
  6458. * @cls_flower: Pointer to struct tc_cls_flower_offload
  6459. *
  6460. **/
  6461. static int i40e_configure_clsflower(struct i40e_vsi *vsi,
  6462. struct tc_cls_flower_offload *cls_flower)
  6463. {
  6464. int tc = tc_classid_to_hwtc(vsi->netdev, cls_flower->classid);
  6465. struct i40e_cloud_filter *filter = NULL;
  6466. struct i40e_pf *pf = vsi->back;
  6467. int err = 0;
  6468. if (tc < 0) {
  6469. dev_err(&vsi->back->pdev->dev, "Invalid traffic class\n");
  6470. return -EOPNOTSUPP;
  6471. }
  6472. if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
  6473. test_bit(__I40E_RESET_INTR_RECEIVED, pf->state))
  6474. return -EBUSY;
  6475. if (pf->fdir_pf_active_filters ||
  6476. (!hlist_empty(&pf->fdir_filter_list))) {
  6477. dev_err(&vsi->back->pdev->dev,
  6478. "Flow Director Sideband filters exists, turn ntuple off to configure cloud filters\n");
  6479. return -EINVAL;
  6480. }
  6481. if (vsi->back->flags & I40E_FLAG_FD_SB_ENABLED) {
  6482. dev_err(&vsi->back->pdev->dev,
  6483. "Disable Flow Director Sideband, configuring Cloud filters via tc-flower\n");
  6484. vsi->back->flags &= ~I40E_FLAG_FD_SB_ENABLED;
  6485. vsi->back->flags |= I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
  6486. }
  6487. filter = kzalloc(sizeof(*filter), GFP_KERNEL);
  6488. if (!filter)
  6489. return -ENOMEM;
  6490. filter->cookie = cls_flower->cookie;
  6491. err = i40e_parse_cls_flower(vsi, cls_flower, filter);
  6492. if (err < 0)
  6493. goto err;
  6494. err = i40e_handle_tclass(vsi, tc, filter);
  6495. if (err < 0)
  6496. goto err;
  6497. /* Add cloud filter */
  6498. if (filter->dst_port)
  6499. err = i40e_add_del_cloud_filter_big_buf(vsi, filter, true);
  6500. else
  6501. err = i40e_add_del_cloud_filter(vsi, filter, true);
  6502. if (err) {
  6503. dev_err(&pf->pdev->dev,
  6504. "Failed to add cloud filter, err %s\n",
  6505. i40e_stat_str(&pf->hw, err));
  6506. goto err;
  6507. }
  6508. /* add filter to the ordered list */
  6509. INIT_HLIST_NODE(&filter->cloud_node);
  6510. hlist_add_head(&filter->cloud_node, &pf->cloud_filter_list);
  6511. pf->num_cloud_filters++;
  6512. return err;
  6513. err:
  6514. kfree(filter);
  6515. return err;
  6516. }
  6517. /**
  6518. * i40e_find_cloud_filter - Find the could filter in the list
  6519. * @vsi: Pointer to VSI
  6520. * @cookie: filter specific cookie
  6521. *
  6522. **/
  6523. static struct i40e_cloud_filter *i40e_find_cloud_filter(struct i40e_vsi *vsi,
  6524. unsigned long *cookie)
  6525. {
  6526. struct i40e_cloud_filter *filter = NULL;
  6527. struct hlist_node *node2;
  6528. hlist_for_each_entry_safe(filter, node2,
  6529. &vsi->back->cloud_filter_list, cloud_node)
  6530. if (!memcmp(cookie, &filter->cookie, sizeof(filter->cookie)))
  6531. return filter;
  6532. return NULL;
  6533. }
  6534. /**
  6535. * i40e_delete_clsflower - Remove tc flower filters
  6536. * @vsi: Pointer to VSI
  6537. * @cls_flower: Pointer to struct tc_cls_flower_offload
  6538. *
  6539. **/
  6540. static int i40e_delete_clsflower(struct i40e_vsi *vsi,
  6541. struct tc_cls_flower_offload *cls_flower)
  6542. {
  6543. struct i40e_cloud_filter *filter = NULL;
  6544. struct i40e_pf *pf = vsi->back;
  6545. int err = 0;
  6546. filter = i40e_find_cloud_filter(vsi, &cls_flower->cookie);
  6547. if (!filter)
  6548. return -EINVAL;
  6549. hash_del(&filter->cloud_node);
  6550. if (filter->dst_port)
  6551. err = i40e_add_del_cloud_filter_big_buf(vsi, filter, false);
  6552. else
  6553. err = i40e_add_del_cloud_filter(vsi, filter, false);
  6554. kfree(filter);
  6555. if (err) {
  6556. dev_err(&pf->pdev->dev,
  6557. "Failed to delete cloud filter, err %s\n",
  6558. i40e_stat_str(&pf->hw, err));
  6559. return i40e_aq_rc_to_posix(err, pf->hw.aq.asq_last_status);
  6560. }
  6561. pf->num_cloud_filters--;
  6562. if (!pf->num_cloud_filters)
  6563. if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
  6564. !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
  6565. pf->flags |= I40E_FLAG_FD_SB_ENABLED;
  6566. pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
  6567. pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
  6568. }
  6569. return 0;
  6570. }
  6571. /**
  6572. * i40e_setup_tc_cls_flower - flower classifier offloads
  6573. * @netdev: net device to configure
  6574. * @type_data: offload data
  6575. **/
  6576. static int i40e_setup_tc_cls_flower(struct i40e_netdev_priv *np,
  6577. struct tc_cls_flower_offload *cls_flower)
  6578. {
  6579. struct i40e_vsi *vsi = np->vsi;
  6580. switch (cls_flower->command) {
  6581. case TC_CLSFLOWER_REPLACE:
  6582. return i40e_configure_clsflower(vsi, cls_flower);
  6583. case TC_CLSFLOWER_DESTROY:
  6584. return i40e_delete_clsflower(vsi, cls_flower);
  6585. case TC_CLSFLOWER_STATS:
  6586. return -EOPNOTSUPP;
  6587. default:
  6588. return -EOPNOTSUPP;
  6589. }
  6590. }
  6591. static int i40e_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
  6592. void *cb_priv)
  6593. {
  6594. struct i40e_netdev_priv *np = cb_priv;
  6595. if (!tc_cls_can_offload_and_chain0(np->vsi->netdev, type_data))
  6596. return -EOPNOTSUPP;
  6597. switch (type) {
  6598. case TC_SETUP_CLSFLOWER:
  6599. return i40e_setup_tc_cls_flower(np, type_data);
  6600. default:
  6601. return -EOPNOTSUPP;
  6602. }
  6603. }
  6604. static int i40e_setup_tc_block(struct net_device *dev,
  6605. struct tc_block_offload *f)
  6606. {
  6607. struct i40e_netdev_priv *np = netdev_priv(dev);
  6608. if (f->binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
  6609. return -EOPNOTSUPP;
  6610. switch (f->command) {
  6611. case TC_BLOCK_BIND:
  6612. return tcf_block_cb_register(f->block, i40e_setup_tc_block_cb,
  6613. np, np, f->extack);
  6614. case TC_BLOCK_UNBIND:
  6615. tcf_block_cb_unregister(f->block, i40e_setup_tc_block_cb, np);
  6616. return 0;
  6617. default:
  6618. return -EOPNOTSUPP;
  6619. }
  6620. }
  6621. static int __i40e_setup_tc(struct net_device *netdev, enum tc_setup_type type,
  6622. void *type_data)
  6623. {
  6624. switch (type) {
  6625. case TC_SETUP_QDISC_MQPRIO:
  6626. return i40e_setup_tc(netdev, type_data);
  6627. case TC_SETUP_BLOCK:
  6628. return i40e_setup_tc_block(netdev, type_data);
  6629. default:
  6630. return -EOPNOTSUPP;
  6631. }
  6632. }
  6633. /**
  6634. * i40e_open - Called when a network interface is made active
  6635. * @netdev: network interface device structure
  6636. *
  6637. * The open entry point is called when a network interface is made
  6638. * active by the system (IFF_UP). At this point all resources needed
  6639. * for transmit and receive operations are allocated, the interrupt
  6640. * handler is registered with the OS, the netdev watchdog subtask is
  6641. * enabled, and the stack is notified that the interface is ready.
  6642. *
  6643. * Returns 0 on success, negative value on failure
  6644. **/
  6645. int i40e_open(struct net_device *netdev)
  6646. {
  6647. struct i40e_netdev_priv *np = netdev_priv(netdev);
  6648. struct i40e_vsi *vsi = np->vsi;
  6649. struct i40e_pf *pf = vsi->back;
  6650. int err;
  6651. /* disallow open during test or if eeprom is broken */
  6652. if (test_bit(__I40E_TESTING, pf->state) ||
  6653. test_bit(__I40E_BAD_EEPROM, pf->state))
  6654. return -EBUSY;
  6655. netif_carrier_off(netdev);
  6656. if (i40e_force_link_state(pf, true))
  6657. return -EAGAIN;
  6658. err = i40e_vsi_open(vsi);
  6659. if (err)
  6660. return err;
  6661. /* configure global TSO hardware offload settings */
  6662. wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
  6663. TCP_FLAG_FIN) >> 16);
  6664. wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
  6665. TCP_FLAG_FIN |
  6666. TCP_FLAG_CWR) >> 16);
  6667. wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
  6668. udp_tunnel_get_rx_info(netdev);
  6669. return 0;
  6670. }
  6671. /**
  6672. * i40e_vsi_open -
  6673. * @vsi: the VSI to open
  6674. *
  6675. * Finish initialization of the VSI.
  6676. *
  6677. * Returns 0 on success, negative value on failure
  6678. *
  6679. * Note: expects to be called while under rtnl_lock()
  6680. **/
  6681. int i40e_vsi_open(struct i40e_vsi *vsi)
  6682. {
  6683. struct i40e_pf *pf = vsi->back;
  6684. char int_name[I40E_INT_NAME_STR_LEN];
  6685. int err;
  6686. /* allocate descriptors */
  6687. err = i40e_vsi_setup_tx_resources(vsi);
  6688. if (err)
  6689. goto err_setup_tx;
  6690. err = i40e_vsi_setup_rx_resources(vsi);
  6691. if (err)
  6692. goto err_setup_rx;
  6693. err = i40e_vsi_configure(vsi);
  6694. if (err)
  6695. goto err_setup_rx;
  6696. if (vsi->netdev) {
  6697. snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
  6698. dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
  6699. err = i40e_vsi_request_irq(vsi, int_name);
  6700. if (err)
  6701. goto err_setup_rx;
  6702. /* Notify the stack of the actual queue counts. */
  6703. err = netif_set_real_num_tx_queues(vsi->netdev,
  6704. vsi->num_queue_pairs);
  6705. if (err)
  6706. goto err_set_queues;
  6707. err = netif_set_real_num_rx_queues(vsi->netdev,
  6708. vsi->num_queue_pairs);
  6709. if (err)
  6710. goto err_set_queues;
  6711. } else if (vsi->type == I40E_VSI_FDIR) {
  6712. snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
  6713. dev_driver_string(&pf->pdev->dev),
  6714. dev_name(&pf->pdev->dev));
  6715. err = i40e_vsi_request_irq(vsi, int_name);
  6716. } else {
  6717. err = -EINVAL;
  6718. goto err_setup_rx;
  6719. }
  6720. err = i40e_up_complete(vsi);
  6721. if (err)
  6722. goto err_up_complete;
  6723. return 0;
  6724. err_up_complete:
  6725. i40e_down(vsi);
  6726. err_set_queues:
  6727. i40e_vsi_free_irq(vsi);
  6728. err_setup_rx:
  6729. i40e_vsi_free_rx_resources(vsi);
  6730. err_setup_tx:
  6731. i40e_vsi_free_tx_resources(vsi);
  6732. if (vsi == pf->vsi[pf->lan_vsi])
  6733. i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
  6734. return err;
  6735. }
  6736. /**
  6737. * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
  6738. * @pf: Pointer to PF
  6739. *
  6740. * This function destroys the hlist where all the Flow Director
  6741. * filters were saved.
  6742. **/
  6743. static void i40e_fdir_filter_exit(struct i40e_pf *pf)
  6744. {
  6745. struct i40e_fdir_filter *filter;
  6746. struct i40e_flex_pit *pit_entry, *tmp;
  6747. struct hlist_node *node2;
  6748. hlist_for_each_entry_safe(filter, node2,
  6749. &pf->fdir_filter_list, fdir_node) {
  6750. hlist_del(&filter->fdir_node);
  6751. kfree(filter);
  6752. }
  6753. list_for_each_entry_safe(pit_entry, tmp, &pf->l3_flex_pit_list, list) {
  6754. list_del(&pit_entry->list);
  6755. kfree(pit_entry);
  6756. }
  6757. INIT_LIST_HEAD(&pf->l3_flex_pit_list);
  6758. list_for_each_entry_safe(pit_entry, tmp, &pf->l4_flex_pit_list, list) {
  6759. list_del(&pit_entry->list);
  6760. kfree(pit_entry);
  6761. }
  6762. INIT_LIST_HEAD(&pf->l4_flex_pit_list);
  6763. pf->fdir_pf_active_filters = 0;
  6764. pf->fd_tcp4_filter_cnt = 0;
  6765. pf->fd_udp4_filter_cnt = 0;
  6766. pf->fd_sctp4_filter_cnt = 0;
  6767. pf->fd_ip4_filter_cnt = 0;
  6768. /* Reprogram the default input set for TCP/IPv4 */
  6769. i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
  6770. I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
  6771. I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
  6772. /* Reprogram the default input set for UDP/IPv4 */
  6773. i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_UDP,
  6774. I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
  6775. I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
  6776. /* Reprogram the default input set for SCTP/IPv4 */
  6777. i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_SCTP,
  6778. I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
  6779. I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
  6780. /* Reprogram the default input set for Other/IPv4 */
  6781. i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_OTHER,
  6782. I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
  6783. i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_FRAG_IPV4,
  6784. I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
  6785. }
  6786. /**
  6787. * i40e_cloud_filter_exit - Cleans up the cloud filters
  6788. * @pf: Pointer to PF
  6789. *
  6790. * This function destroys the hlist where all the cloud filters
  6791. * were saved.
  6792. **/
  6793. static void i40e_cloud_filter_exit(struct i40e_pf *pf)
  6794. {
  6795. struct i40e_cloud_filter *cfilter;
  6796. struct hlist_node *node;
  6797. hlist_for_each_entry_safe(cfilter, node,
  6798. &pf->cloud_filter_list, cloud_node) {
  6799. hlist_del(&cfilter->cloud_node);
  6800. kfree(cfilter);
  6801. }
  6802. pf->num_cloud_filters = 0;
  6803. if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
  6804. !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
  6805. pf->flags |= I40E_FLAG_FD_SB_ENABLED;
  6806. pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
  6807. pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
  6808. }
  6809. }
  6810. /**
  6811. * i40e_close - Disables a network interface
  6812. * @netdev: network interface device structure
  6813. *
  6814. * The close entry point is called when an interface is de-activated
  6815. * by the OS. The hardware is still under the driver's control, but
  6816. * this netdev interface is disabled.
  6817. *
  6818. * Returns 0, this is not allowed to fail
  6819. **/
  6820. int i40e_close(struct net_device *netdev)
  6821. {
  6822. struct i40e_netdev_priv *np = netdev_priv(netdev);
  6823. struct i40e_vsi *vsi = np->vsi;
  6824. i40e_vsi_close(vsi);
  6825. return 0;
  6826. }
  6827. /**
  6828. * i40e_do_reset - Start a PF or Core Reset sequence
  6829. * @pf: board private structure
  6830. * @reset_flags: which reset is requested
  6831. * @lock_acquired: indicates whether or not the lock has been acquired
  6832. * before this function was called.
  6833. *
  6834. * The essential difference in resets is that the PF Reset
  6835. * doesn't clear the packet buffers, doesn't reset the PE
  6836. * firmware, and doesn't bother the other PFs on the chip.
  6837. **/
  6838. void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags, bool lock_acquired)
  6839. {
  6840. u32 val;
  6841. WARN_ON(in_interrupt());
  6842. /* do the biggest reset indicated */
  6843. if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
  6844. /* Request a Global Reset
  6845. *
  6846. * This will start the chip's countdown to the actual full
  6847. * chip reset event, and a warning interrupt to be sent
  6848. * to all PFs, including the requestor. Our handler
  6849. * for the warning interrupt will deal with the shutdown
  6850. * and recovery of the switch setup.
  6851. */
  6852. dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
  6853. val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
  6854. val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
  6855. wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
  6856. } else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
  6857. /* Request a Core Reset
  6858. *
  6859. * Same as Global Reset, except does *not* include the MAC/PHY
  6860. */
  6861. dev_dbg(&pf->pdev->dev, "CoreR requested\n");
  6862. val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
  6863. val |= I40E_GLGEN_RTRIG_CORER_MASK;
  6864. wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
  6865. i40e_flush(&pf->hw);
  6866. } else if (reset_flags & I40E_PF_RESET_FLAG) {
  6867. /* Request a PF Reset
  6868. *
  6869. * Resets only the PF-specific registers
  6870. *
  6871. * This goes directly to the tear-down and rebuild of
  6872. * the switch, since we need to do all the recovery as
  6873. * for the Core Reset.
  6874. */
  6875. dev_dbg(&pf->pdev->dev, "PFR requested\n");
  6876. i40e_handle_reset_warning(pf, lock_acquired);
  6877. } else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
  6878. int v;
  6879. /* Find the VSI(s) that requested a re-init */
  6880. dev_info(&pf->pdev->dev,
  6881. "VSI reinit requested\n");
  6882. for (v = 0; v < pf->num_alloc_vsi; v++) {
  6883. struct i40e_vsi *vsi = pf->vsi[v];
  6884. if (vsi != NULL &&
  6885. test_and_clear_bit(__I40E_VSI_REINIT_REQUESTED,
  6886. vsi->state))
  6887. i40e_vsi_reinit_locked(pf->vsi[v]);
  6888. }
  6889. } else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
  6890. int v;
  6891. /* Find the VSI(s) that needs to be brought down */
  6892. dev_info(&pf->pdev->dev, "VSI down requested\n");
  6893. for (v = 0; v < pf->num_alloc_vsi; v++) {
  6894. struct i40e_vsi *vsi = pf->vsi[v];
  6895. if (vsi != NULL &&
  6896. test_and_clear_bit(__I40E_VSI_DOWN_REQUESTED,
  6897. vsi->state)) {
  6898. set_bit(__I40E_VSI_DOWN, vsi->state);
  6899. i40e_down(vsi);
  6900. }
  6901. }
  6902. } else {
  6903. dev_info(&pf->pdev->dev,
  6904. "bad reset request 0x%08x\n", reset_flags);
  6905. }
  6906. }
  6907. #ifdef CONFIG_I40E_DCB
  6908. /**
  6909. * i40e_dcb_need_reconfig - Check if DCB needs reconfig
  6910. * @pf: board private structure
  6911. * @old_cfg: current DCB config
  6912. * @new_cfg: new DCB config
  6913. **/
  6914. bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
  6915. struct i40e_dcbx_config *old_cfg,
  6916. struct i40e_dcbx_config *new_cfg)
  6917. {
  6918. bool need_reconfig = false;
  6919. /* Check if ETS configuration has changed */
  6920. if (memcmp(&new_cfg->etscfg,
  6921. &old_cfg->etscfg,
  6922. sizeof(new_cfg->etscfg))) {
  6923. /* If Priority Table has changed reconfig is needed */
  6924. if (memcmp(&new_cfg->etscfg.prioritytable,
  6925. &old_cfg->etscfg.prioritytable,
  6926. sizeof(new_cfg->etscfg.prioritytable))) {
  6927. need_reconfig = true;
  6928. dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
  6929. }
  6930. if (memcmp(&new_cfg->etscfg.tcbwtable,
  6931. &old_cfg->etscfg.tcbwtable,
  6932. sizeof(new_cfg->etscfg.tcbwtable)))
  6933. dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
  6934. if (memcmp(&new_cfg->etscfg.tsatable,
  6935. &old_cfg->etscfg.tsatable,
  6936. sizeof(new_cfg->etscfg.tsatable)))
  6937. dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
  6938. }
  6939. /* Check if PFC configuration has changed */
  6940. if (memcmp(&new_cfg->pfc,
  6941. &old_cfg->pfc,
  6942. sizeof(new_cfg->pfc))) {
  6943. need_reconfig = true;
  6944. dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
  6945. }
  6946. /* Check if APP Table has changed */
  6947. if (memcmp(&new_cfg->app,
  6948. &old_cfg->app,
  6949. sizeof(new_cfg->app))) {
  6950. need_reconfig = true;
  6951. dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
  6952. }
  6953. dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
  6954. return need_reconfig;
  6955. }
  6956. /**
  6957. * i40e_handle_lldp_event - Handle LLDP Change MIB event
  6958. * @pf: board private structure
  6959. * @e: event info posted on ARQ
  6960. **/
  6961. static int i40e_handle_lldp_event(struct i40e_pf *pf,
  6962. struct i40e_arq_event_info *e)
  6963. {
  6964. struct i40e_aqc_lldp_get_mib *mib =
  6965. (struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
  6966. struct i40e_hw *hw = &pf->hw;
  6967. struct i40e_dcbx_config tmp_dcbx_cfg;
  6968. bool need_reconfig = false;
  6969. int ret = 0;
  6970. u8 type;
  6971. /* Not DCB capable or capability disabled */
  6972. if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
  6973. return ret;
  6974. /* Ignore if event is not for Nearest Bridge */
  6975. type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
  6976. & I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
  6977. dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
  6978. if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
  6979. return ret;
  6980. /* Check MIB Type and return if event for Remote MIB update */
  6981. type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
  6982. dev_dbg(&pf->pdev->dev,
  6983. "LLDP event mib type %s\n", type ? "remote" : "local");
  6984. if (type == I40E_AQ_LLDP_MIB_REMOTE) {
  6985. /* Update the remote cached instance and return */
  6986. ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
  6987. I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
  6988. &hw->remote_dcbx_config);
  6989. goto exit;
  6990. }
  6991. /* Store the old configuration */
  6992. tmp_dcbx_cfg = hw->local_dcbx_config;
  6993. /* Reset the old DCBx configuration data */
  6994. memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
  6995. /* Get updated DCBX data from firmware */
  6996. ret = i40e_get_dcb_config(&pf->hw);
  6997. if (ret) {
  6998. dev_info(&pf->pdev->dev,
  6999. "Failed querying DCB configuration data from firmware, err %s aq_err %s\n",
  7000. i40e_stat_str(&pf->hw, ret),
  7001. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  7002. goto exit;
  7003. }
  7004. /* No change detected in DCBX configs */
  7005. if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
  7006. sizeof(tmp_dcbx_cfg))) {
  7007. dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
  7008. goto exit;
  7009. }
  7010. need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
  7011. &hw->local_dcbx_config);
  7012. i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
  7013. if (!need_reconfig)
  7014. goto exit;
  7015. /* Enable DCB tagging only when more than one TC */
  7016. if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
  7017. pf->flags |= I40E_FLAG_DCB_ENABLED;
  7018. else
  7019. pf->flags &= ~I40E_FLAG_DCB_ENABLED;
  7020. set_bit(__I40E_PORT_SUSPENDED, pf->state);
  7021. /* Reconfiguration needed quiesce all VSIs */
  7022. i40e_pf_quiesce_all_vsi(pf);
  7023. /* Changes in configuration update VEB/VSI */
  7024. i40e_dcb_reconfigure(pf);
  7025. ret = i40e_resume_port_tx(pf);
  7026. clear_bit(__I40E_PORT_SUSPENDED, pf->state);
  7027. /* In case of error no point in resuming VSIs */
  7028. if (ret)
  7029. goto exit;
  7030. /* Wait for the PF's queues to be disabled */
  7031. ret = i40e_pf_wait_queues_disabled(pf);
  7032. if (ret) {
  7033. /* Schedule PF reset to recover */
  7034. set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
  7035. i40e_service_event_schedule(pf);
  7036. } else {
  7037. i40e_pf_unquiesce_all_vsi(pf);
  7038. set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
  7039. set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
  7040. }
  7041. exit:
  7042. return ret;
  7043. }
  7044. #endif /* CONFIG_I40E_DCB */
  7045. /**
  7046. * i40e_do_reset_safe - Protected reset path for userland calls.
  7047. * @pf: board private structure
  7048. * @reset_flags: which reset is requested
  7049. *
  7050. **/
  7051. void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
  7052. {
  7053. rtnl_lock();
  7054. i40e_do_reset(pf, reset_flags, true);
  7055. rtnl_unlock();
  7056. }
  7057. /**
  7058. * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
  7059. * @pf: board private structure
  7060. * @e: event info posted on ARQ
  7061. *
  7062. * Handler for LAN Queue Overflow Event generated by the firmware for PF
  7063. * and VF queues
  7064. **/
  7065. static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
  7066. struct i40e_arq_event_info *e)
  7067. {
  7068. struct i40e_aqc_lan_overflow *data =
  7069. (struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
  7070. u32 queue = le32_to_cpu(data->prtdcb_rupto);
  7071. u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
  7072. struct i40e_hw *hw = &pf->hw;
  7073. struct i40e_vf *vf;
  7074. u16 vf_id;
  7075. dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
  7076. queue, qtx_ctl);
  7077. /* Queue belongs to VF, find the VF and issue VF reset */
  7078. if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
  7079. >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
  7080. vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
  7081. >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
  7082. vf_id -= hw->func_caps.vf_base_id;
  7083. vf = &pf->vf[vf_id];
  7084. i40e_vc_notify_vf_reset(vf);
  7085. /* Allow VF to process pending reset notification */
  7086. msleep(20);
  7087. i40e_reset_vf(vf, false);
  7088. }
  7089. }
  7090. /**
  7091. * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
  7092. * @pf: board private structure
  7093. **/
  7094. u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
  7095. {
  7096. u32 val, fcnt_prog;
  7097. val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
  7098. fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
  7099. return fcnt_prog;
  7100. }
  7101. /**
  7102. * i40e_get_current_fd_count - Get total FD filters programmed for this PF
  7103. * @pf: board private structure
  7104. **/
  7105. u32 i40e_get_current_fd_count(struct i40e_pf *pf)
  7106. {
  7107. u32 val, fcnt_prog;
  7108. val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
  7109. fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
  7110. ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
  7111. I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
  7112. return fcnt_prog;
  7113. }
  7114. /**
  7115. * i40e_get_global_fd_count - Get total FD filters programmed on device
  7116. * @pf: board private structure
  7117. **/
  7118. u32 i40e_get_global_fd_count(struct i40e_pf *pf)
  7119. {
  7120. u32 val, fcnt_prog;
  7121. val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
  7122. fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
  7123. ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >>
  7124. I40E_GLQF_FDCNT_0_BESTCNT_SHIFT);
  7125. return fcnt_prog;
  7126. }
  7127. /**
  7128. * i40e_reenable_fdir_sb - Restore FDir SB capability
  7129. * @pf: board private structure
  7130. **/
  7131. static void i40e_reenable_fdir_sb(struct i40e_pf *pf)
  7132. {
  7133. if (test_and_clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
  7134. if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
  7135. (I40E_DEBUG_FD & pf->hw.debug_mask))
  7136. dev_info(&pf->pdev->dev, "FD Sideband/ntuple is being enabled since we have space in the table now\n");
  7137. }
  7138. /**
  7139. * i40e_reenable_fdir_atr - Restore FDir ATR capability
  7140. * @pf: board private structure
  7141. **/
  7142. static void i40e_reenable_fdir_atr(struct i40e_pf *pf)
  7143. {
  7144. if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state)) {
  7145. /* ATR uses the same filtering logic as SB rules. It only
  7146. * functions properly if the input set mask is at the default
  7147. * settings. It is safe to restore the default input set
  7148. * because there are no active TCPv4 filter rules.
  7149. */
  7150. i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
  7151. I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
  7152. I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
  7153. if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
  7154. (I40E_DEBUG_FD & pf->hw.debug_mask))
  7155. dev_info(&pf->pdev->dev, "ATR is being enabled since we have space in the table and there are no conflicting ntuple rules\n");
  7156. }
  7157. }
  7158. /**
  7159. * i40e_delete_invalid_filter - Delete an invalid FDIR filter
  7160. * @pf: board private structure
  7161. * @filter: FDir filter to remove
  7162. */
  7163. static void i40e_delete_invalid_filter(struct i40e_pf *pf,
  7164. struct i40e_fdir_filter *filter)
  7165. {
  7166. /* Update counters */
  7167. pf->fdir_pf_active_filters--;
  7168. pf->fd_inv = 0;
  7169. switch (filter->flow_type) {
  7170. case TCP_V4_FLOW:
  7171. pf->fd_tcp4_filter_cnt--;
  7172. break;
  7173. case UDP_V4_FLOW:
  7174. pf->fd_udp4_filter_cnt--;
  7175. break;
  7176. case SCTP_V4_FLOW:
  7177. pf->fd_sctp4_filter_cnt--;
  7178. break;
  7179. case IP_USER_FLOW:
  7180. switch (filter->ip4_proto) {
  7181. case IPPROTO_TCP:
  7182. pf->fd_tcp4_filter_cnt--;
  7183. break;
  7184. case IPPROTO_UDP:
  7185. pf->fd_udp4_filter_cnt--;
  7186. break;
  7187. case IPPROTO_SCTP:
  7188. pf->fd_sctp4_filter_cnt--;
  7189. break;
  7190. case IPPROTO_IP:
  7191. pf->fd_ip4_filter_cnt--;
  7192. break;
  7193. }
  7194. break;
  7195. }
  7196. /* Remove the filter from the list and free memory */
  7197. hlist_del(&filter->fdir_node);
  7198. kfree(filter);
  7199. }
  7200. /**
  7201. * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
  7202. * @pf: board private structure
  7203. **/
  7204. void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
  7205. {
  7206. struct i40e_fdir_filter *filter;
  7207. u32 fcnt_prog, fcnt_avail;
  7208. struct hlist_node *node;
  7209. if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
  7210. return;
  7211. /* Check if we have enough room to re-enable FDir SB capability. */
  7212. fcnt_prog = i40e_get_global_fd_count(pf);
  7213. fcnt_avail = pf->fdir_pf_filter_count;
  7214. if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
  7215. (pf->fd_add_err == 0) ||
  7216. (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt))
  7217. i40e_reenable_fdir_sb(pf);
  7218. /* We should wait for even more space before re-enabling ATR.
  7219. * Additionally, we cannot enable ATR as long as we still have TCP SB
  7220. * rules active.
  7221. */
  7222. if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) &&
  7223. (pf->fd_tcp4_filter_cnt == 0))
  7224. i40e_reenable_fdir_atr(pf);
  7225. /* if hw had a problem adding a filter, delete it */
  7226. if (pf->fd_inv > 0) {
  7227. hlist_for_each_entry_safe(filter, node,
  7228. &pf->fdir_filter_list, fdir_node)
  7229. if (filter->fd_id == pf->fd_inv)
  7230. i40e_delete_invalid_filter(pf, filter);
  7231. }
  7232. }
  7233. #define I40E_MIN_FD_FLUSH_INTERVAL 10
  7234. #define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
  7235. /**
  7236. * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
  7237. * @pf: board private structure
  7238. **/
  7239. static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
  7240. {
  7241. unsigned long min_flush_time;
  7242. int flush_wait_retry = 50;
  7243. bool disable_atr = false;
  7244. int fd_room;
  7245. int reg;
  7246. if (!time_after(jiffies, pf->fd_flush_timestamp +
  7247. (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
  7248. return;
  7249. /* If the flush is happening too quick and we have mostly SB rules we
  7250. * should not re-enable ATR for some time.
  7251. */
  7252. min_flush_time = pf->fd_flush_timestamp +
  7253. (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
  7254. fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
  7255. if (!(time_after(jiffies, min_flush_time)) &&
  7256. (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
  7257. if (I40E_DEBUG_FD & pf->hw.debug_mask)
  7258. dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
  7259. disable_atr = true;
  7260. }
  7261. pf->fd_flush_timestamp = jiffies;
  7262. set_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
  7263. /* flush all filters */
  7264. wr32(&pf->hw, I40E_PFQF_CTL_1,
  7265. I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
  7266. i40e_flush(&pf->hw);
  7267. pf->fd_flush_cnt++;
  7268. pf->fd_add_err = 0;
  7269. do {
  7270. /* Check FD flush status every 5-6msec */
  7271. usleep_range(5000, 6000);
  7272. reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
  7273. if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
  7274. break;
  7275. } while (flush_wait_retry--);
  7276. if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
  7277. dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
  7278. } else {
  7279. /* replay sideband filters */
  7280. i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
  7281. if (!disable_atr && !pf->fd_tcp4_filter_cnt)
  7282. clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
  7283. clear_bit(__I40E_FD_FLUSH_REQUESTED, pf->state);
  7284. if (I40E_DEBUG_FD & pf->hw.debug_mask)
  7285. dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
  7286. }
  7287. }
  7288. /**
  7289. * i40e_get_current_atr_count - Get the count of total FD ATR filters programmed
  7290. * @pf: board private structure
  7291. **/
  7292. u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
  7293. {
  7294. return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
  7295. }
  7296. /* We can see up to 256 filter programming desc in transit if the filters are
  7297. * being applied really fast; before we see the first
  7298. * filter miss error on Rx queue 0. Accumulating enough error messages before
  7299. * reacting will make sure we don't cause flush too often.
  7300. */
  7301. #define I40E_MAX_FD_PROGRAM_ERROR 256
  7302. /**
  7303. * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
  7304. * @pf: board private structure
  7305. **/
  7306. static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
  7307. {
  7308. /* if interface is down do nothing */
  7309. if (test_bit(__I40E_DOWN, pf->state))
  7310. return;
  7311. if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
  7312. i40e_fdir_flush_and_replay(pf);
  7313. i40e_fdir_check_and_reenable(pf);
  7314. }
  7315. /**
  7316. * i40e_vsi_link_event - notify VSI of a link event
  7317. * @vsi: vsi to be notified
  7318. * @link_up: link up or down
  7319. **/
  7320. static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
  7321. {
  7322. if (!vsi || test_bit(__I40E_VSI_DOWN, vsi->state))
  7323. return;
  7324. switch (vsi->type) {
  7325. case I40E_VSI_MAIN:
  7326. if (!vsi->netdev || !vsi->netdev_registered)
  7327. break;
  7328. if (link_up) {
  7329. netif_carrier_on(vsi->netdev);
  7330. netif_tx_wake_all_queues(vsi->netdev);
  7331. } else {
  7332. netif_carrier_off(vsi->netdev);
  7333. netif_tx_stop_all_queues(vsi->netdev);
  7334. }
  7335. break;
  7336. case I40E_VSI_SRIOV:
  7337. case I40E_VSI_VMDQ2:
  7338. case I40E_VSI_CTRL:
  7339. case I40E_VSI_IWARP:
  7340. case I40E_VSI_MIRROR:
  7341. default:
  7342. /* there is no notification for other VSIs */
  7343. break;
  7344. }
  7345. }
  7346. /**
  7347. * i40e_veb_link_event - notify elements on the veb of a link event
  7348. * @veb: veb to be notified
  7349. * @link_up: link up or down
  7350. **/
  7351. static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
  7352. {
  7353. struct i40e_pf *pf;
  7354. int i;
  7355. if (!veb || !veb->pf)
  7356. return;
  7357. pf = veb->pf;
  7358. /* depth first... */
  7359. for (i = 0; i < I40E_MAX_VEB; i++)
  7360. if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
  7361. i40e_veb_link_event(pf->veb[i], link_up);
  7362. /* ... now the local VSIs */
  7363. for (i = 0; i < pf->num_alloc_vsi; i++)
  7364. if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
  7365. i40e_vsi_link_event(pf->vsi[i], link_up);
  7366. }
  7367. /**
  7368. * i40e_link_event - Update netif_carrier status
  7369. * @pf: board private structure
  7370. **/
  7371. static void i40e_link_event(struct i40e_pf *pf)
  7372. {
  7373. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  7374. u8 new_link_speed, old_link_speed;
  7375. i40e_status status;
  7376. bool new_link, old_link;
  7377. /* save off old link status information */
  7378. pf->hw.phy.link_info_old = pf->hw.phy.link_info;
  7379. /* set this to force the get_link_status call to refresh state */
  7380. pf->hw.phy.get_link_info = true;
  7381. old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
  7382. status = i40e_get_link_status(&pf->hw, &new_link);
  7383. /* On success, disable temp link polling */
  7384. if (status == I40E_SUCCESS) {
  7385. clear_bit(__I40E_TEMP_LINK_POLLING, pf->state);
  7386. } else {
  7387. /* Enable link polling temporarily until i40e_get_link_status
  7388. * returns I40E_SUCCESS
  7389. */
  7390. set_bit(__I40E_TEMP_LINK_POLLING, pf->state);
  7391. dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
  7392. status);
  7393. return;
  7394. }
  7395. old_link_speed = pf->hw.phy.link_info_old.link_speed;
  7396. new_link_speed = pf->hw.phy.link_info.link_speed;
  7397. if (new_link == old_link &&
  7398. new_link_speed == old_link_speed &&
  7399. (test_bit(__I40E_VSI_DOWN, vsi->state) ||
  7400. new_link == netif_carrier_ok(vsi->netdev)))
  7401. return;
  7402. i40e_print_link_message(vsi, new_link);
  7403. /* Notify the base of the switch tree connected to
  7404. * the link. Floating VEBs are not notified.
  7405. */
  7406. if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
  7407. i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
  7408. else
  7409. i40e_vsi_link_event(vsi, new_link);
  7410. if (pf->vf)
  7411. i40e_vc_notify_link_state(pf);
  7412. if (pf->flags & I40E_FLAG_PTP)
  7413. i40e_ptp_set_increment(pf);
  7414. }
  7415. /**
  7416. * i40e_watchdog_subtask - periodic checks not using event driven response
  7417. * @pf: board private structure
  7418. **/
  7419. static void i40e_watchdog_subtask(struct i40e_pf *pf)
  7420. {
  7421. int i;
  7422. /* if interface is down do nothing */
  7423. if (test_bit(__I40E_DOWN, pf->state) ||
  7424. test_bit(__I40E_CONFIG_BUSY, pf->state))
  7425. return;
  7426. /* make sure we don't do these things too often */
  7427. if (time_before(jiffies, (pf->service_timer_previous +
  7428. pf->service_timer_period)))
  7429. return;
  7430. pf->service_timer_previous = jiffies;
  7431. if ((pf->flags & I40E_FLAG_LINK_POLLING_ENABLED) ||
  7432. test_bit(__I40E_TEMP_LINK_POLLING, pf->state))
  7433. i40e_link_event(pf);
  7434. /* Update the stats for active netdevs so the network stack
  7435. * can look at updated numbers whenever it cares to
  7436. */
  7437. for (i = 0; i < pf->num_alloc_vsi; i++)
  7438. if (pf->vsi[i] && pf->vsi[i]->netdev)
  7439. i40e_update_stats(pf->vsi[i]);
  7440. if (pf->flags & I40E_FLAG_VEB_STATS_ENABLED) {
  7441. /* Update the stats for the active switching components */
  7442. for (i = 0; i < I40E_MAX_VEB; i++)
  7443. if (pf->veb[i])
  7444. i40e_update_veb_stats(pf->veb[i]);
  7445. }
  7446. i40e_ptp_rx_hang(pf);
  7447. i40e_ptp_tx_hang(pf);
  7448. }
  7449. /**
  7450. * i40e_reset_subtask - Set up for resetting the device and driver
  7451. * @pf: board private structure
  7452. **/
  7453. static void i40e_reset_subtask(struct i40e_pf *pf)
  7454. {
  7455. u32 reset_flags = 0;
  7456. if (test_bit(__I40E_REINIT_REQUESTED, pf->state)) {
  7457. reset_flags |= BIT(__I40E_REINIT_REQUESTED);
  7458. clear_bit(__I40E_REINIT_REQUESTED, pf->state);
  7459. }
  7460. if (test_bit(__I40E_PF_RESET_REQUESTED, pf->state)) {
  7461. reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
  7462. clear_bit(__I40E_PF_RESET_REQUESTED, pf->state);
  7463. }
  7464. if (test_bit(__I40E_CORE_RESET_REQUESTED, pf->state)) {
  7465. reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
  7466. clear_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
  7467. }
  7468. if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state)) {
  7469. reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
  7470. clear_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
  7471. }
  7472. if (test_bit(__I40E_DOWN_REQUESTED, pf->state)) {
  7473. reset_flags |= BIT(__I40E_DOWN_REQUESTED);
  7474. clear_bit(__I40E_DOWN_REQUESTED, pf->state);
  7475. }
  7476. /* If there's a recovery already waiting, it takes
  7477. * precedence before starting a new reset sequence.
  7478. */
  7479. if (test_bit(__I40E_RESET_INTR_RECEIVED, pf->state)) {
  7480. i40e_prep_for_reset(pf, false);
  7481. i40e_reset(pf);
  7482. i40e_rebuild(pf, false, false);
  7483. }
  7484. /* If we're already down or resetting, just bail */
  7485. if (reset_flags &&
  7486. !test_bit(__I40E_DOWN, pf->state) &&
  7487. !test_bit(__I40E_CONFIG_BUSY, pf->state)) {
  7488. i40e_do_reset(pf, reset_flags, false);
  7489. }
  7490. }
  7491. /**
  7492. * i40e_handle_link_event - Handle link event
  7493. * @pf: board private structure
  7494. * @e: event info posted on ARQ
  7495. **/
  7496. static void i40e_handle_link_event(struct i40e_pf *pf,
  7497. struct i40e_arq_event_info *e)
  7498. {
  7499. struct i40e_aqc_get_link_status *status =
  7500. (struct i40e_aqc_get_link_status *)&e->desc.params.raw;
  7501. /* Do a new status request to re-enable LSE reporting
  7502. * and load new status information into the hw struct
  7503. * This completely ignores any state information
  7504. * in the ARQ event info, instead choosing to always
  7505. * issue the AQ update link status command.
  7506. */
  7507. i40e_link_event(pf);
  7508. /* Check if module meets thermal requirements */
  7509. if (status->phy_type == I40E_PHY_TYPE_NOT_SUPPORTED_HIGH_TEMP) {
  7510. dev_err(&pf->pdev->dev,
  7511. "Rx/Tx is disabled on this device because the module does not meet thermal requirements.\n");
  7512. dev_err(&pf->pdev->dev,
  7513. "Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
  7514. } else {
  7515. /* check for unqualified module, if link is down, suppress
  7516. * the message if link was forced to be down.
  7517. */
  7518. if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
  7519. (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
  7520. (!(status->link_info & I40E_AQ_LINK_UP)) &&
  7521. (!(pf->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED))) {
  7522. dev_err(&pf->pdev->dev,
  7523. "Rx/Tx is disabled on this device because an unsupported SFP module type was detected.\n");
  7524. dev_err(&pf->pdev->dev,
  7525. "Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
  7526. }
  7527. }
  7528. }
  7529. /**
  7530. * i40e_clean_adminq_subtask - Clean the AdminQ rings
  7531. * @pf: board private structure
  7532. **/
  7533. static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
  7534. {
  7535. struct i40e_arq_event_info event;
  7536. struct i40e_hw *hw = &pf->hw;
  7537. u16 pending, i = 0;
  7538. i40e_status ret;
  7539. u16 opcode;
  7540. u32 oldval;
  7541. u32 val;
  7542. /* Do not run clean AQ when PF reset fails */
  7543. if (test_bit(__I40E_RESET_FAILED, pf->state))
  7544. return;
  7545. /* check for error indications */
  7546. val = rd32(&pf->hw, pf->hw.aq.arq.len);
  7547. oldval = val;
  7548. if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
  7549. if (hw->debug_mask & I40E_DEBUG_AQ)
  7550. dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
  7551. val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
  7552. }
  7553. if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
  7554. if (hw->debug_mask & I40E_DEBUG_AQ)
  7555. dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
  7556. val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
  7557. pf->arq_overflows++;
  7558. }
  7559. if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
  7560. if (hw->debug_mask & I40E_DEBUG_AQ)
  7561. dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
  7562. val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
  7563. }
  7564. if (oldval != val)
  7565. wr32(&pf->hw, pf->hw.aq.arq.len, val);
  7566. val = rd32(&pf->hw, pf->hw.aq.asq.len);
  7567. oldval = val;
  7568. if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
  7569. if (pf->hw.debug_mask & I40E_DEBUG_AQ)
  7570. dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
  7571. val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
  7572. }
  7573. if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
  7574. if (pf->hw.debug_mask & I40E_DEBUG_AQ)
  7575. dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
  7576. val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
  7577. }
  7578. if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
  7579. if (pf->hw.debug_mask & I40E_DEBUG_AQ)
  7580. dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
  7581. val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
  7582. }
  7583. if (oldval != val)
  7584. wr32(&pf->hw, pf->hw.aq.asq.len, val);
  7585. event.buf_len = I40E_MAX_AQ_BUF_SIZE;
  7586. event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
  7587. if (!event.msg_buf)
  7588. return;
  7589. do {
  7590. ret = i40e_clean_arq_element(hw, &event, &pending);
  7591. if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK)
  7592. break;
  7593. else if (ret) {
  7594. dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
  7595. break;
  7596. }
  7597. opcode = le16_to_cpu(event.desc.opcode);
  7598. switch (opcode) {
  7599. case i40e_aqc_opc_get_link_status:
  7600. i40e_handle_link_event(pf, &event);
  7601. break;
  7602. case i40e_aqc_opc_send_msg_to_pf:
  7603. ret = i40e_vc_process_vf_msg(pf,
  7604. le16_to_cpu(event.desc.retval),
  7605. le32_to_cpu(event.desc.cookie_high),
  7606. le32_to_cpu(event.desc.cookie_low),
  7607. event.msg_buf,
  7608. event.msg_len);
  7609. break;
  7610. case i40e_aqc_opc_lldp_update_mib:
  7611. dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
  7612. #ifdef CONFIG_I40E_DCB
  7613. rtnl_lock();
  7614. ret = i40e_handle_lldp_event(pf, &event);
  7615. rtnl_unlock();
  7616. #endif /* CONFIG_I40E_DCB */
  7617. break;
  7618. case i40e_aqc_opc_event_lan_overflow:
  7619. dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
  7620. i40e_handle_lan_overflow_event(pf, &event);
  7621. break;
  7622. case i40e_aqc_opc_send_msg_to_peer:
  7623. dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
  7624. break;
  7625. case i40e_aqc_opc_nvm_erase:
  7626. case i40e_aqc_opc_nvm_update:
  7627. case i40e_aqc_opc_oem_post_update:
  7628. i40e_debug(&pf->hw, I40E_DEBUG_NVM,
  7629. "ARQ NVM operation 0x%04x completed\n",
  7630. opcode);
  7631. break;
  7632. default:
  7633. dev_info(&pf->pdev->dev,
  7634. "ARQ: Unknown event 0x%04x ignored\n",
  7635. opcode);
  7636. break;
  7637. }
  7638. } while (i++ < pf->adminq_work_limit);
  7639. if (i < pf->adminq_work_limit)
  7640. clear_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
  7641. /* re-enable Admin queue interrupt cause */
  7642. val = rd32(hw, I40E_PFINT_ICR0_ENA);
  7643. val |= I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
  7644. wr32(hw, I40E_PFINT_ICR0_ENA, val);
  7645. i40e_flush(hw);
  7646. kfree(event.msg_buf);
  7647. }
  7648. /**
  7649. * i40e_verify_eeprom - make sure eeprom is good to use
  7650. * @pf: board private structure
  7651. **/
  7652. static void i40e_verify_eeprom(struct i40e_pf *pf)
  7653. {
  7654. int err;
  7655. err = i40e_diag_eeprom_test(&pf->hw);
  7656. if (err) {
  7657. /* retry in case of garbage read */
  7658. err = i40e_diag_eeprom_test(&pf->hw);
  7659. if (err) {
  7660. dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
  7661. err);
  7662. set_bit(__I40E_BAD_EEPROM, pf->state);
  7663. }
  7664. }
  7665. if (!err && test_bit(__I40E_BAD_EEPROM, pf->state)) {
  7666. dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
  7667. clear_bit(__I40E_BAD_EEPROM, pf->state);
  7668. }
  7669. }
  7670. /**
  7671. * i40e_enable_pf_switch_lb
  7672. * @pf: pointer to the PF structure
  7673. *
  7674. * enable switch loop back or die - no point in a return value
  7675. **/
  7676. static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
  7677. {
  7678. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  7679. struct i40e_vsi_context ctxt;
  7680. int ret;
  7681. ctxt.seid = pf->main_vsi_seid;
  7682. ctxt.pf_num = pf->hw.pf_id;
  7683. ctxt.vf_num = 0;
  7684. ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
  7685. if (ret) {
  7686. dev_info(&pf->pdev->dev,
  7687. "couldn't get PF vsi config, err %s aq_err %s\n",
  7688. i40e_stat_str(&pf->hw, ret),
  7689. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  7690. return;
  7691. }
  7692. ctxt.flags = I40E_AQ_VSI_TYPE_PF;
  7693. ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  7694. ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  7695. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  7696. if (ret) {
  7697. dev_info(&pf->pdev->dev,
  7698. "update vsi switch failed, err %s aq_err %s\n",
  7699. i40e_stat_str(&pf->hw, ret),
  7700. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  7701. }
  7702. }
  7703. /**
  7704. * i40e_disable_pf_switch_lb
  7705. * @pf: pointer to the PF structure
  7706. *
  7707. * disable switch loop back or die - no point in a return value
  7708. **/
  7709. static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
  7710. {
  7711. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  7712. struct i40e_vsi_context ctxt;
  7713. int ret;
  7714. ctxt.seid = pf->main_vsi_seid;
  7715. ctxt.pf_num = pf->hw.pf_id;
  7716. ctxt.vf_num = 0;
  7717. ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
  7718. if (ret) {
  7719. dev_info(&pf->pdev->dev,
  7720. "couldn't get PF vsi config, err %s aq_err %s\n",
  7721. i40e_stat_str(&pf->hw, ret),
  7722. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  7723. return;
  7724. }
  7725. ctxt.flags = I40E_AQ_VSI_TYPE_PF;
  7726. ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  7727. ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  7728. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  7729. if (ret) {
  7730. dev_info(&pf->pdev->dev,
  7731. "update vsi switch failed, err %s aq_err %s\n",
  7732. i40e_stat_str(&pf->hw, ret),
  7733. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  7734. }
  7735. }
  7736. /**
  7737. * i40e_config_bridge_mode - Configure the HW bridge mode
  7738. * @veb: pointer to the bridge instance
  7739. *
  7740. * Configure the loop back mode for the LAN VSI that is downlink to the
  7741. * specified HW bridge instance. It is expected this function is called
  7742. * when a new HW bridge is instantiated.
  7743. **/
  7744. static void i40e_config_bridge_mode(struct i40e_veb *veb)
  7745. {
  7746. struct i40e_pf *pf = veb->pf;
  7747. if (pf->hw.debug_mask & I40E_DEBUG_LAN)
  7748. dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
  7749. veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
  7750. if (veb->bridge_mode & BRIDGE_MODE_VEPA)
  7751. i40e_disable_pf_switch_lb(pf);
  7752. else
  7753. i40e_enable_pf_switch_lb(pf);
  7754. }
  7755. /**
  7756. * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
  7757. * @veb: pointer to the VEB instance
  7758. *
  7759. * This is a recursive function that first builds the attached VSIs then
  7760. * recurses in to build the next layer of VEB. We track the connections
  7761. * through our own index numbers because the seid's from the HW could
  7762. * change across the reset.
  7763. **/
  7764. static int i40e_reconstitute_veb(struct i40e_veb *veb)
  7765. {
  7766. struct i40e_vsi *ctl_vsi = NULL;
  7767. struct i40e_pf *pf = veb->pf;
  7768. int v, veb_idx;
  7769. int ret;
  7770. /* build VSI that owns this VEB, temporarily attached to base VEB */
  7771. for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
  7772. if (pf->vsi[v] &&
  7773. pf->vsi[v]->veb_idx == veb->idx &&
  7774. pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
  7775. ctl_vsi = pf->vsi[v];
  7776. break;
  7777. }
  7778. }
  7779. if (!ctl_vsi) {
  7780. dev_info(&pf->pdev->dev,
  7781. "missing owner VSI for veb_idx %d\n", veb->idx);
  7782. ret = -ENOENT;
  7783. goto end_reconstitute;
  7784. }
  7785. if (ctl_vsi != pf->vsi[pf->lan_vsi])
  7786. ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
  7787. ret = i40e_add_vsi(ctl_vsi);
  7788. if (ret) {
  7789. dev_info(&pf->pdev->dev,
  7790. "rebuild of veb_idx %d owner VSI failed: %d\n",
  7791. veb->idx, ret);
  7792. goto end_reconstitute;
  7793. }
  7794. i40e_vsi_reset_stats(ctl_vsi);
  7795. /* create the VEB in the switch and move the VSI onto the VEB */
  7796. ret = i40e_add_veb(veb, ctl_vsi);
  7797. if (ret)
  7798. goto end_reconstitute;
  7799. if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
  7800. veb->bridge_mode = BRIDGE_MODE_VEB;
  7801. else
  7802. veb->bridge_mode = BRIDGE_MODE_VEPA;
  7803. i40e_config_bridge_mode(veb);
  7804. /* create the remaining VSIs attached to this VEB */
  7805. for (v = 0; v < pf->num_alloc_vsi; v++) {
  7806. if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
  7807. continue;
  7808. if (pf->vsi[v]->veb_idx == veb->idx) {
  7809. struct i40e_vsi *vsi = pf->vsi[v];
  7810. vsi->uplink_seid = veb->seid;
  7811. ret = i40e_add_vsi(vsi);
  7812. if (ret) {
  7813. dev_info(&pf->pdev->dev,
  7814. "rebuild of vsi_idx %d failed: %d\n",
  7815. v, ret);
  7816. goto end_reconstitute;
  7817. }
  7818. i40e_vsi_reset_stats(vsi);
  7819. }
  7820. }
  7821. /* create any VEBs attached to this VEB - RECURSION */
  7822. for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
  7823. if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
  7824. pf->veb[veb_idx]->uplink_seid = veb->seid;
  7825. ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
  7826. if (ret)
  7827. break;
  7828. }
  7829. }
  7830. end_reconstitute:
  7831. return ret;
  7832. }
  7833. /**
  7834. * i40e_get_capabilities - get info about the HW
  7835. * @pf: the PF struct
  7836. **/
  7837. static int i40e_get_capabilities(struct i40e_pf *pf,
  7838. enum i40e_admin_queue_opc list_type)
  7839. {
  7840. struct i40e_aqc_list_capabilities_element_resp *cap_buf;
  7841. u16 data_size;
  7842. int buf_len;
  7843. int err;
  7844. buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
  7845. do {
  7846. cap_buf = kzalloc(buf_len, GFP_KERNEL);
  7847. if (!cap_buf)
  7848. return -ENOMEM;
  7849. /* this loads the data into the hw struct for us */
  7850. err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
  7851. &data_size, list_type,
  7852. NULL);
  7853. /* data loaded, buffer no longer needed */
  7854. kfree(cap_buf);
  7855. if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
  7856. /* retry with a larger buffer */
  7857. buf_len = data_size;
  7858. } else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK) {
  7859. dev_info(&pf->pdev->dev,
  7860. "capability discovery failed, err %s aq_err %s\n",
  7861. i40e_stat_str(&pf->hw, err),
  7862. i40e_aq_str(&pf->hw,
  7863. pf->hw.aq.asq_last_status));
  7864. return -ENODEV;
  7865. }
  7866. } while (err);
  7867. if (pf->hw.debug_mask & I40E_DEBUG_USER) {
  7868. if (list_type == i40e_aqc_opc_list_func_capabilities) {
  7869. dev_info(&pf->pdev->dev,
  7870. "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",
  7871. pf->hw.pf_id, pf->hw.func_caps.num_vfs,
  7872. pf->hw.func_caps.num_msix_vectors,
  7873. pf->hw.func_caps.num_msix_vectors_vf,
  7874. pf->hw.func_caps.fd_filters_guaranteed,
  7875. pf->hw.func_caps.fd_filters_best_effort,
  7876. pf->hw.func_caps.num_tx_qp,
  7877. pf->hw.func_caps.num_vsis);
  7878. } else if (list_type == i40e_aqc_opc_list_dev_capabilities) {
  7879. dev_info(&pf->pdev->dev,
  7880. "switch_mode=0x%04x, function_valid=0x%08x\n",
  7881. pf->hw.dev_caps.switch_mode,
  7882. pf->hw.dev_caps.valid_functions);
  7883. dev_info(&pf->pdev->dev,
  7884. "SR-IOV=%d, num_vfs for all function=%u\n",
  7885. pf->hw.dev_caps.sr_iov_1_1,
  7886. pf->hw.dev_caps.num_vfs);
  7887. dev_info(&pf->pdev->dev,
  7888. "num_vsis=%u, num_rx:%u, num_tx=%u\n",
  7889. pf->hw.dev_caps.num_vsis,
  7890. pf->hw.dev_caps.num_rx_qp,
  7891. pf->hw.dev_caps.num_tx_qp);
  7892. }
  7893. }
  7894. if (list_type == i40e_aqc_opc_list_func_capabilities) {
  7895. #define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
  7896. + pf->hw.func_caps.num_vfs)
  7897. if (pf->hw.revision_id == 0 &&
  7898. pf->hw.func_caps.num_vsis < DEF_NUM_VSI) {
  7899. dev_info(&pf->pdev->dev,
  7900. "got num_vsis %d, setting num_vsis to %d\n",
  7901. pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
  7902. pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
  7903. }
  7904. }
  7905. return 0;
  7906. }
  7907. static int i40e_vsi_clear(struct i40e_vsi *vsi);
  7908. /**
  7909. * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
  7910. * @pf: board private structure
  7911. **/
  7912. static void i40e_fdir_sb_setup(struct i40e_pf *pf)
  7913. {
  7914. struct i40e_vsi *vsi;
  7915. /* quick workaround for an NVM issue that leaves a critical register
  7916. * uninitialized
  7917. */
  7918. if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
  7919. static const u32 hkey[] = {
  7920. 0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
  7921. 0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
  7922. 0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
  7923. 0x95b3a76d};
  7924. int i;
  7925. for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
  7926. wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
  7927. }
  7928. if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
  7929. return;
  7930. /* find existing VSI and see if it needs configuring */
  7931. vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
  7932. /* create a new VSI if none exists */
  7933. if (!vsi) {
  7934. vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
  7935. pf->vsi[pf->lan_vsi]->seid, 0);
  7936. if (!vsi) {
  7937. dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
  7938. pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
  7939. pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
  7940. return;
  7941. }
  7942. }
  7943. i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
  7944. }
  7945. /**
  7946. * i40e_fdir_teardown - release the Flow Director resources
  7947. * @pf: board private structure
  7948. **/
  7949. static void i40e_fdir_teardown(struct i40e_pf *pf)
  7950. {
  7951. struct i40e_vsi *vsi;
  7952. i40e_fdir_filter_exit(pf);
  7953. vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
  7954. if (vsi)
  7955. i40e_vsi_release(vsi);
  7956. }
  7957. /**
  7958. * i40e_rebuild_cloud_filters - Rebuilds cloud filters for VSIs
  7959. * @vsi: PF main vsi
  7960. * @seid: seid of main or channel VSIs
  7961. *
  7962. * Rebuilds cloud filters associated with main VSI and channel VSIs if they
  7963. * existed before reset
  7964. **/
  7965. static int i40e_rebuild_cloud_filters(struct i40e_vsi *vsi, u16 seid)
  7966. {
  7967. struct i40e_cloud_filter *cfilter;
  7968. struct i40e_pf *pf = vsi->back;
  7969. struct hlist_node *node;
  7970. i40e_status ret;
  7971. /* Add cloud filters back if they exist */
  7972. hlist_for_each_entry_safe(cfilter, node, &pf->cloud_filter_list,
  7973. cloud_node) {
  7974. if (cfilter->seid != seid)
  7975. continue;
  7976. if (cfilter->dst_port)
  7977. ret = i40e_add_del_cloud_filter_big_buf(vsi, cfilter,
  7978. true);
  7979. else
  7980. ret = i40e_add_del_cloud_filter(vsi, cfilter, true);
  7981. if (ret) {
  7982. dev_dbg(&pf->pdev->dev,
  7983. "Failed to rebuild cloud filter, err %s aq_err %s\n",
  7984. i40e_stat_str(&pf->hw, ret),
  7985. i40e_aq_str(&pf->hw,
  7986. pf->hw.aq.asq_last_status));
  7987. return ret;
  7988. }
  7989. }
  7990. return 0;
  7991. }
  7992. /**
  7993. * i40e_rebuild_channels - Rebuilds channel VSIs if they existed before reset
  7994. * @vsi: PF main vsi
  7995. *
  7996. * Rebuilds channel VSIs if they existed before reset
  7997. **/
  7998. static int i40e_rebuild_channels(struct i40e_vsi *vsi)
  7999. {
  8000. struct i40e_channel *ch, *ch_tmp;
  8001. i40e_status ret;
  8002. if (list_empty(&vsi->ch_list))
  8003. return 0;
  8004. list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
  8005. if (!ch->initialized)
  8006. break;
  8007. /* Proceed with creation of channel (VMDq2) VSI */
  8008. ret = i40e_add_channel(vsi->back, vsi->uplink_seid, ch);
  8009. if (ret) {
  8010. dev_info(&vsi->back->pdev->dev,
  8011. "failed to rebuild channels using uplink_seid %u\n",
  8012. vsi->uplink_seid);
  8013. return ret;
  8014. }
  8015. /* Reconfigure TX queues using QTX_CTL register */
  8016. ret = i40e_channel_config_tx_ring(vsi->back, vsi, ch);
  8017. if (ret) {
  8018. dev_info(&vsi->back->pdev->dev,
  8019. "failed to configure TX rings for channel %u\n",
  8020. ch->seid);
  8021. return ret;
  8022. }
  8023. /* update 'next_base_queue' */
  8024. vsi->next_base_queue = vsi->next_base_queue +
  8025. ch->num_queue_pairs;
  8026. if (ch->max_tx_rate) {
  8027. u64 credits = ch->max_tx_rate;
  8028. if (i40e_set_bw_limit(vsi, ch->seid,
  8029. ch->max_tx_rate))
  8030. return -EINVAL;
  8031. do_div(credits, I40E_BW_CREDIT_DIVISOR);
  8032. dev_dbg(&vsi->back->pdev->dev,
  8033. "Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
  8034. ch->max_tx_rate,
  8035. credits,
  8036. ch->seid);
  8037. }
  8038. ret = i40e_rebuild_cloud_filters(vsi, ch->seid);
  8039. if (ret) {
  8040. dev_dbg(&vsi->back->pdev->dev,
  8041. "Failed to rebuild cloud filters for channel VSI %u\n",
  8042. ch->seid);
  8043. return ret;
  8044. }
  8045. }
  8046. return 0;
  8047. }
  8048. /**
  8049. * i40e_prep_for_reset - prep for the core to reset
  8050. * @pf: board private structure
  8051. * @lock_acquired: indicates whether or not the lock has been acquired
  8052. * before this function was called.
  8053. *
  8054. * Close up the VFs and other things in prep for PF Reset.
  8055. **/
  8056. static void i40e_prep_for_reset(struct i40e_pf *pf, bool lock_acquired)
  8057. {
  8058. struct i40e_hw *hw = &pf->hw;
  8059. i40e_status ret = 0;
  8060. u32 v;
  8061. clear_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
  8062. if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
  8063. return;
  8064. if (i40e_check_asq_alive(&pf->hw))
  8065. i40e_vc_notify_reset(pf);
  8066. dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
  8067. /* quiesce the VSIs and their queues that are not already DOWN */
  8068. /* pf_quiesce_all_vsi modifies netdev structures -rtnl_lock needed */
  8069. if (!lock_acquired)
  8070. rtnl_lock();
  8071. i40e_pf_quiesce_all_vsi(pf);
  8072. if (!lock_acquired)
  8073. rtnl_unlock();
  8074. for (v = 0; v < pf->num_alloc_vsi; v++) {
  8075. if (pf->vsi[v])
  8076. pf->vsi[v]->seid = 0;
  8077. }
  8078. i40e_shutdown_adminq(&pf->hw);
  8079. /* call shutdown HMC */
  8080. if (hw->hmc.hmc_obj) {
  8081. ret = i40e_shutdown_lan_hmc(hw);
  8082. if (ret)
  8083. dev_warn(&pf->pdev->dev,
  8084. "shutdown_lan_hmc failed: %d\n", ret);
  8085. }
  8086. }
  8087. /**
  8088. * i40e_send_version - update firmware with driver version
  8089. * @pf: PF struct
  8090. */
  8091. static void i40e_send_version(struct i40e_pf *pf)
  8092. {
  8093. struct i40e_driver_version dv;
  8094. dv.major_version = DRV_VERSION_MAJOR;
  8095. dv.minor_version = DRV_VERSION_MINOR;
  8096. dv.build_version = DRV_VERSION_BUILD;
  8097. dv.subbuild_version = 0;
  8098. strlcpy(dv.driver_string, DRV_VERSION, sizeof(dv.driver_string));
  8099. i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
  8100. }
  8101. /**
  8102. * i40e_get_oem_version - get OEM specific version information
  8103. * @hw: pointer to the hardware structure
  8104. **/
  8105. static void i40e_get_oem_version(struct i40e_hw *hw)
  8106. {
  8107. u16 block_offset = 0xffff;
  8108. u16 block_length = 0;
  8109. u16 capabilities = 0;
  8110. u16 gen_snap = 0;
  8111. u16 release = 0;
  8112. #define I40E_SR_NVM_OEM_VERSION_PTR 0x1B
  8113. #define I40E_NVM_OEM_LENGTH_OFFSET 0x00
  8114. #define I40E_NVM_OEM_CAPABILITIES_OFFSET 0x01
  8115. #define I40E_NVM_OEM_GEN_OFFSET 0x02
  8116. #define I40E_NVM_OEM_RELEASE_OFFSET 0x03
  8117. #define I40E_NVM_OEM_CAPABILITIES_MASK 0x000F
  8118. #define I40E_NVM_OEM_LENGTH 3
  8119. /* Check if pointer to OEM version block is valid. */
  8120. i40e_read_nvm_word(hw, I40E_SR_NVM_OEM_VERSION_PTR, &block_offset);
  8121. if (block_offset == 0xffff)
  8122. return;
  8123. /* Check if OEM version block has correct length. */
  8124. i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_LENGTH_OFFSET,
  8125. &block_length);
  8126. if (block_length < I40E_NVM_OEM_LENGTH)
  8127. return;
  8128. /* Check if OEM version format is as expected. */
  8129. i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_CAPABILITIES_OFFSET,
  8130. &capabilities);
  8131. if ((capabilities & I40E_NVM_OEM_CAPABILITIES_MASK) != 0)
  8132. return;
  8133. i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_GEN_OFFSET,
  8134. &gen_snap);
  8135. i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_RELEASE_OFFSET,
  8136. &release);
  8137. hw->nvm.oem_ver = (gen_snap << I40E_OEM_SNAP_SHIFT) | release;
  8138. hw->nvm.eetrack = I40E_OEM_EETRACK_ID;
  8139. }
  8140. /**
  8141. * i40e_reset - wait for core reset to finish reset, reset pf if corer not seen
  8142. * @pf: board private structure
  8143. **/
  8144. static int i40e_reset(struct i40e_pf *pf)
  8145. {
  8146. struct i40e_hw *hw = &pf->hw;
  8147. i40e_status ret;
  8148. ret = i40e_pf_reset(hw);
  8149. if (ret) {
  8150. dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
  8151. set_bit(__I40E_RESET_FAILED, pf->state);
  8152. clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
  8153. } else {
  8154. pf->pfr_count++;
  8155. }
  8156. return ret;
  8157. }
  8158. /**
  8159. * i40e_rebuild - rebuild using a saved config
  8160. * @pf: board private structure
  8161. * @reinit: if the Main VSI needs to re-initialized.
  8162. * @lock_acquired: indicates whether or not the lock has been acquired
  8163. * before this function was called.
  8164. **/
  8165. static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired)
  8166. {
  8167. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  8168. struct i40e_hw *hw = &pf->hw;
  8169. u8 set_fc_aq_fail = 0;
  8170. i40e_status ret;
  8171. u32 val;
  8172. int v;
  8173. if (test_bit(__I40E_DOWN, pf->state))
  8174. goto clear_recovery;
  8175. dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
  8176. /* rebuild the basics for the AdminQ, HMC, and initial HW switch */
  8177. ret = i40e_init_adminq(&pf->hw);
  8178. if (ret) {
  8179. dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %s aq_err %s\n",
  8180. i40e_stat_str(&pf->hw, ret),
  8181. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  8182. goto clear_recovery;
  8183. }
  8184. i40e_get_oem_version(&pf->hw);
  8185. if (test_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state) &&
  8186. ((hw->aq.fw_maj_ver == 4 && hw->aq.fw_min_ver <= 33) ||
  8187. hw->aq.fw_maj_ver < 4) && hw->mac.type == I40E_MAC_XL710) {
  8188. /* The following delay is necessary for 4.33 firmware and older
  8189. * to recover after EMP reset. 200 ms should suffice but we
  8190. * put here 300 ms to be sure that FW is ready to operate
  8191. * after reset.
  8192. */
  8193. mdelay(300);
  8194. }
  8195. /* re-verify the eeprom if we just had an EMP reset */
  8196. if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state))
  8197. i40e_verify_eeprom(pf);
  8198. i40e_clear_pxe_mode(hw);
  8199. ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
  8200. if (ret)
  8201. goto end_core_reset;
  8202. ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
  8203. hw->func_caps.num_rx_qp, 0, 0);
  8204. if (ret) {
  8205. dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
  8206. goto end_core_reset;
  8207. }
  8208. ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
  8209. if (ret) {
  8210. dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
  8211. goto end_core_reset;
  8212. }
  8213. /* Enable FW to write a default DCB config on link-up */
  8214. i40e_aq_set_dcb_parameters(hw, true, NULL);
  8215. #ifdef CONFIG_I40E_DCB
  8216. ret = i40e_init_pf_dcb(pf);
  8217. if (ret) {
  8218. dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n", ret);
  8219. pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
  8220. /* Continue without DCB enabled */
  8221. }
  8222. #endif /* CONFIG_I40E_DCB */
  8223. /* do basic switch setup */
  8224. if (!lock_acquired)
  8225. rtnl_lock();
  8226. ret = i40e_setup_pf_switch(pf, reinit);
  8227. if (ret)
  8228. goto end_unlock;
  8229. /* The driver only wants link up/down and module qualification
  8230. * reports from firmware. Note the negative logic.
  8231. */
  8232. ret = i40e_aq_set_phy_int_mask(&pf->hw,
  8233. ~(I40E_AQ_EVENT_LINK_UPDOWN |
  8234. I40E_AQ_EVENT_MEDIA_NA |
  8235. I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
  8236. if (ret)
  8237. dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
  8238. i40e_stat_str(&pf->hw, ret),
  8239. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  8240. /* make sure our flow control settings are restored */
  8241. ret = i40e_set_fc(&pf->hw, &set_fc_aq_fail, true);
  8242. if (ret)
  8243. dev_dbg(&pf->pdev->dev, "setting flow control: ret = %s last_status = %s\n",
  8244. i40e_stat_str(&pf->hw, ret),
  8245. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  8246. /* Rebuild the VSIs and VEBs that existed before reset.
  8247. * They are still in our local switch element arrays, so only
  8248. * need to rebuild the switch model in the HW.
  8249. *
  8250. * If there were VEBs but the reconstitution failed, we'll try
  8251. * try to recover minimal use by getting the basic PF VSI working.
  8252. */
  8253. if (vsi->uplink_seid != pf->mac_seid) {
  8254. dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
  8255. /* find the one VEB connected to the MAC, and find orphans */
  8256. for (v = 0; v < I40E_MAX_VEB; v++) {
  8257. if (!pf->veb[v])
  8258. continue;
  8259. if (pf->veb[v]->uplink_seid == pf->mac_seid ||
  8260. pf->veb[v]->uplink_seid == 0) {
  8261. ret = i40e_reconstitute_veb(pf->veb[v]);
  8262. if (!ret)
  8263. continue;
  8264. /* If Main VEB failed, we're in deep doodoo,
  8265. * so give up rebuilding the switch and set up
  8266. * for minimal rebuild of PF VSI.
  8267. * If orphan failed, we'll report the error
  8268. * but try to keep going.
  8269. */
  8270. if (pf->veb[v]->uplink_seid == pf->mac_seid) {
  8271. dev_info(&pf->pdev->dev,
  8272. "rebuild of switch failed: %d, will try to set up simple PF connection\n",
  8273. ret);
  8274. vsi->uplink_seid = pf->mac_seid;
  8275. break;
  8276. } else if (pf->veb[v]->uplink_seid == 0) {
  8277. dev_info(&pf->pdev->dev,
  8278. "rebuild of orphan VEB failed: %d\n",
  8279. ret);
  8280. }
  8281. }
  8282. }
  8283. }
  8284. if (vsi->uplink_seid == pf->mac_seid) {
  8285. dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
  8286. /* no VEB, so rebuild only the Main VSI */
  8287. ret = i40e_add_vsi(vsi);
  8288. if (ret) {
  8289. dev_info(&pf->pdev->dev,
  8290. "rebuild of Main VSI failed: %d\n", ret);
  8291. goto end_unlock;
  8292. }
  8293. }
  8294. if (vsi->mqprio_qopt.max_rate[0]) {
  8295. u64 max_tx_rate = vsi->mqprio_qopt.max_rate[0];
  8296. u64 credits = 0;
  8297. do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
  8298. ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
  8299. if (ret)
  8300. goto end_unlock;
  8301. credits = max_tx_rate;
  8302. do_div(credits, I40E_BW_CREDIT_DIVISOR);
  8303. dev_dbg(&vsi->back->pdev->dev,
  8304. "Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
  8305. max_tx_rate,
  8306. credits,
  8307. vsi->seid);
  8308. }
  8309. ret = i40e_rebuild_cloud_filters(vsi, vsi->seid);
  8310. if (ret)
  8311. goto end_unlock;
  8312. /* PF Main VSI is rebuild by now, go ahead and rebuild channel VSIs
  8313. * for this main VSI if they exist
  8314. */
  8315. ret = i40e_rebuild_channels(vsi);
  8316. if (ret)
  8317. goto end_unlock;
  8318. /* Reconfigure hardware for allowing smaller MSS in the case
  8319. * of TSO, so that we avoid the MDD being fired and causing
  8320. * a reset in the case of small MSS+TSO.
  8321. */
  8322. #define I40E_REG_MSS 0x000E64DC
  8323. #define I40E_REG_MSS_MIN_MASK 0x3FF0000
  8324. #define I40E_64BYTE_MSS 0x400000
  8325. val = rd32(hw, I40E_REG_MSS);
  8326. if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
  8327. val &= ~I40E_REG_MSS_MIN_MASK;
  8328. val |= I40E_64BYTE_MSS;
  8329. wr32(hw, I40E_REG_MSS, val);
  8330. }
  8331. if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
  8332. msleep(75);
  8333. ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
  8334. if (ret)
  8335. dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
  8336. i40e_stat_str(&pf->hw, ret),
  8337. i40e_aq_str(&pf->hw,
  8338. pf->hw.aq.asq_last_status));
  8339. }
  8340. /* reinit the misc interrupt */
  8341. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  8342. ret = i40e_setup_misc_vector(pf);
  8343. /* Add a filter to drop all Flow control frames from any VSI from being
  8344. * transmitted. By doing so we stop a malicious VF from sending out
  8345. * PAUSE or PFC frames and potentially controlling traffic for other
  8346. * PF/VF VSIs.
  8347. * The FW can still send Flow control frames if enabled.
  8348. */
  8349. i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
  8350. pf->main_vsi_seid);
  8351. /* restart the VSIs that were rebuilt and running before the reset */
  8352. i40e_pf_unquiesce_all_vsi(pf);
  8353. /* Release the RTNL lock before we start resetting VFs */
  8354. if (!lock_acquired)
  8355. rtnl_unlock();
  8356. /* Restore promiscuous settings */
  8357. ret = i40e_set_promiscuous(pf, pf->cur_promisc);
  8358. if (ret)
  8359. dev_warn(&pf->pdev->dev,
  8360. "Failed to restore promiscuous setting: %s, err %s aq_err %s\n",
  8361. pf->cur_promisc ? "on" : "off",
  8362. i40e_stat_str(&pf->hw, ret),
  8363. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  8364. i40e_reset_all_vfs(pf, true);
  8365. /* tell the firmware that we're starting */
  8366. i40e_send_version(pf);
  8367. /* We've already released the lock, so don't do it again */
  8368. goto end_core_reset;
  8369. end_unlock:
  8370. if (!lock_acquired)
  8371. rtnl_unlock();
  8372. end_core_reset:
  8373. clear_bit(__I40E_RESET_FAILED, pf->state);
  8374. clear_recovery:
  8375. clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
  8376. }
  8377. /**
  8378. * i40e_reset_and_rebuild - reset and rebuild using a saved config
  8379. * @pf: board private structure
  8380. * @reinit: if the Main VSI needs to re-initialized.
  8381. * @lock_acquired: indicates whether or not the lock has been acquired
  8382. * before this function was called.
  8383. **/
  8384. static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
  8385. bool lock_acquired)
  8386. {
  8387. int ret;
  8388. /* Now we wait for GRST to settle out.
  8389. * We don't have to delete the VEBs or VSIs from the hw switch
  8390. * because the reset will make them disappear.
  8391. */
  8392. ret = i40e_reset(pf);
  8393. if (!ret)
  8394. i40e_rebuild(pf, reinit, lock_acquired);
  8395. }
  8396. /**
  8397. * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
  8398. * @pf: board private structure
  8399. *
  8400. * Close up the VFs and other things in prep for a Core Reset,
  8401. * then get ready to rebuild the world.
  8402. * @lock_acquired: indicates whether or not the lock has been acquired
  8403. * before this function was called.
  8404. **/
  8405. static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired)
  8406. {
  8407. i40e_prep_for_reset(pf, lock_acquired);
  8408. i40e_reset_and_rebuild(pf, false, lock_acquired);
  8409. }
  8410. /**
  8411. * i40e_handle_mdd_event
  8412. * @pf: pointer to the PF structure
  8413. *
  8414. * Called from the MDD irq handler to identify possibly malicious vfs
  8415. **/
  8416. static void i40e_handle_mdd_event(struct i40e_pf *pf)
  8417. {
  8418. struct i40e_hw *hw = &pf->hw;
  8419. bool mdd_detected = false;
  8420. bool pf_mdd_detected = false;
  8421. struct i40e_vf *vf;
  8422. u32 reg;
  8423. int i;
  8424. if (!test_bit(__I40E_MDD_EVENT_PENDING, pf->state))
  8425. return;
  8426. /* find what triggered the MDD event */
  8427. reg = rd32(hw, I40E_GL_MDET_TX);
  8428. if (reg & I40E_GL_MDET_TX_VALID_MASK) {
  8429. u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
  8430. I40E_GL_MDET_TX_PF_NUM_SHIFT;
  8431. u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
  8432. I40E_GL_MDET_TX_VF_NUM_SHIFT;
  8433. u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
  8434. I40E_GL_MDET_TX_EVENT_SHIFT;
  8435. u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
  8436. I40E_GL_MDET_TX_QUEUE_SHIFT) -
  8437. pf->hw.func_caps.base_queue;
  8438. if (netif_msg_tx_err(pf))
  8439. dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on TX queue %d PF number 0x%02x VF number 0x%02x\n",
  8440. event, queue, pf_num, vf_num);
  8441. wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
  8442. mdd_detected = true;
  8443. }
  8444. reg = rd32(hw, I40E_GL_MDET_RX);
  8445. if (reg & I40E_GL_MDET_RX_VALID_MASK) {
  8446. u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
  8447. I40E_GL_MDET_RX_FUNCTION_SHIFT;
  8448. u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
  8449. I40E_GL_MDET_RX_EVENT_SHIFT;
  8450. u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
  8451. I40E_GL_MDET_RX_QUEUE_SHIFT) -
  8452. pf->hw.func_caps.base_queue;
  8453. if (netif_msg_rx_err(pf))
  8454. dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
  8455. event, queue, func);
  8456. wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
  8457. mdd_detected = true;
  8458. }
  8459. if (mdd_detected) {
  8460. reg = rd32(hw, I40E_PF_MDET_TX);
  8461. if (reg & I40E_PF_MDET_TX_VALID_MASK) {
  8462. wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
  8463. dev_info(&pf->pdev->dev, "TX driver issue detected, PF reset issued\n");
  8464. pf_mdd_detected = true;
  8465. }
  8466. reg = rd32(hw, I40E_PF_MDET_RX);
  8467. if (reg & I40E_PF_MDET_RX_VALID_MASK) {
  8468. wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
  8469. dev_info(&pf->pdev->dev, "RX driver issue detected, PF reset issued\n");
  8470. pf_mdd_detected = true;
  8471. }
  8472. /* Queue belongs to the PF, initiate a reset */
  8473. if (pf_mdd_detected) {
  8474. set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
  8475. i40e_service_event_schedule(pf);
  8476. }
  8477. }
  8478. /* see if one of the VFs needs its hand slapped */
  8479. for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
  8480. vf = &(pf->vf[i]);
  8481. reg = rd32(hw, I40E_VP_MDET_TX(i));
  8482. if (reg & I40E_VP_MDET_TX_VALID_MASK) {
  8483. wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
  8484. vf->num_mdd_events++;
  8485. dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
  8486. i);
  8487. }
  8488. reg = rd32(hw, I40E_VP_MDET_RX(i));
  8489. if (reg & I40E_VP_MDET_RX_VALID_MASK) {
  8490. wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
  8491. vf->num_mdd_events++;
  8492. dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
  8493. i);
  8494. }
  8495. if (vf->num_mdd_events > I40E_DEFAULT_NUM_MDD_EVENTS_ALLOWED) {
  8496. dev_info(&pf->pdev->dev,
  8497. "Too many MDD events on VF %d, disabled\n", i);
  8498. dev_info(&pf->pdev->dev,
  8499. "Use PF Control I/F to re-enable the VF\n");
  8500. set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
  8501. }
  8502. }
  8503. /* re-enable mdd interrupt cause */
  8504. clear_bit(__I40E_MDD_EVENT_PENDING, pf->state);
  8505. reg = rd32(hw, I40E_PFINT_ICR0_ENA);
  8506. reg |= I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
  8507. wr32(hw, I40E_PFINT_ICR0_ENA, reg);
  8508. i40e_flush(hw);
  8509. }
  8510. static const char *i40e_tunnel_name(u8 type)
  8511. {
  8512. switch (type) {
  8513. case UDP_TUNNEL_TYPE_VXLAN:
  8514. return "vxlan";
  8515. case UDP_TUNNEL_TYPE_GENEVE:
  8516. return "geneve";
  8517. default:
  8518. return "unknown";
  8519. }
  8520. }
  8521. /**
  8522. * i40e_sync_udp_filters - Trigger a sync event for existing UDP filters
  8523. * @pf: board private structure
  8524. **/
  8525. static void i40e_sync_udp_filters(struct i40e_pf *pf)
  8526. {
  8527. int i;
  8528. /* loop through and set pending bit for all active UDP filters */
  8529. for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
  8530. if (pf->udp_ports[i].port)
  8531. pf->pending_udp_bitmap |= BIT_ULL(i);
  8532. }
  8533. set_bit(__I40E_UDP_FILTER_SYNC_PENDING, pf->state);
  8534. }
  8535. /**
  8536. * i40e_sync_udp_filters_subtask - Sync the VSI filter list with HW
  8537. * @pf: board private structure
  8538. **/
  8539. static void i40e_sync_udp_filters_subtask(struct i40e_pf *pf)
  8540. {
  8541. struct i40e_hw *hw = &pf->hw;
  8542. u8 filter_index, type;
  8543. u16 port;
  8544. int i;
  8545. if (!test_and_clear_bit(__I40E_UDP_FILTER_SYNC_PENDING, pf->state))
  8546. return;
  8547. /* acquire RTNL to maintain state of flags and port requests */
  8548. rtnl_lock();
  8549. for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
  8550. if (pf->pending_udp_bitmap & BIT_ULL(i)) {
  8551. struct i40e_udp_port_config *udp_port;
  8552. i40e_status ret = 0;
  8553. udp_port = &pf->udp_ports[i];
  8554. pf->pending_udp_bitmap &= ~BIT_ULL(i);
  8555. port = READ_ONCE(udp_port->port);
  8556. type = READ_ONCE(udp_port->type);
  8557. filter_index = READ_ONCE(udp_port->filter_index);
  8558. /* release RTNL while we wait on AQ command */
  8559. rtnl_unlock();
  8560. if (port)
  8561. ret = i40e_aq_add_udp_tunnel(hw, port,
  8562. type,
  8563. &filter_index,
  8564. NULL);
  8565. else if (filter_index != I40E_UDP_PORT_INDEX_UNUSED)
  8566. ret = i40e_aq_del_udp_tunnel(hw, filter_index,
  8567. NULL);
  8568. /* reacquire RTNL so we can update filter_index */
  8569. rtnl_lock();
  8570. if (ret) {
  8571. dev_info(&pf->pdev->dev,
  8572. "%s %s port %d, index %d failed, err %s aq_err %s\n",
  8573. i40e_tunnel_name(type),
  8574. port ? "add" : "delete",
  8575. port,
  8576. filter_index,
  8577. i40e_stat_str(&pf->hw, ret),
  8578. i40e_aq_str(&pf->hw,
  8579. pf->hw.aq.asq_last_status));
  8580. if (port) {
  8581. /* failed to add, just reset port,
  8582. * drop pending bit for any deletion
  8583. */
  8584. udp_port->port = 0;
  8585. pf->pending_udp_bitmap &= ~BIT_ULL(i);
  8586. }
  8587. } else if (port) {
  8588. /* record filter index on success */
  8589. udp_port->filter_index = filter_index;
  8590. }
  8591. }
  8592. }
  8593. rtnl_unlock();
  8594. }
  8595. /**
  8596. * i40e_service_task - Run the driver's async subtasks
  8597. * @work: pointer to work_struct containing our data
  8598. **/
  8599. static void i40e_service_task(struct work_struct *work)
  8600. {
  8601. struct i40e_pf *pf = container_of(work,
  8602. struct i40e_pf,
  8603. service_task);
  8604. unsigned long start_time = jiffies;
  8605. /* don't bother with service tasks if a reset is in progress */
  8606. if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
  8607. return;
  8608. if (test_and_set_bit(__I40E_SERVICE_SCHED, pf->state))
  8609. return;
  8610. i40e_detect_recover_hung(pf->vsi[pf->lan_vsi]);
  8611. i40e_sync_filters_subtask(pf);
  8612. i40e_reset_subtask(pf);
  8613. i40e_handle_mdd_event(pf);
  8614. i40e_vc_process_vflr_event(pf);
  8615. i40e_watchdog_subtask(pf);
  8616. i40e_fdir_reinit_subtask(pf);
  8617. if (test_and_clear_bit(__I40E_CLIENT_RESET, pf->state)) {
  8618. /* Client subtask will reopen next time through. */
  8619. i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], true);
  8620. } else {
  8621. i40e_client_subtask(pf);
  8622. if (test_and_clear_bit(__I40E_CLIENT_L2_CHANGE,
  8623. pf->state))
  8624. i40e_notify_client_of_l2_param_changes(
  8625. pf->vsi[pf->lan_vsi]);
  8626. }
  8627. i40e_sync_filters_subtask(pf);
  8628. i40e_sync_udp_filters_subtask(pf);
  8629. i40e_clean_adminq_subtask(pf);
  8630. /* flush memory to make sure state is correct before next watchdog */
  8631. smp_mb__before_atomic();
  8632. clear_bit(__I40E_SERVICE_SCHED, pf->state);
  8633. /* If the tasks have taken longer than one timer cycle or there
  8634. * is more work to be done, reschedule the service task now
  8635. * rather than wait for the timer to tick again.
  8636. */
  8637. if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
  8638. test_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state) ||
  8639. test_bit(__I40E_MDD_EVENT_PENDING, pf->state) ||
  8640. test_bit(__I40E_VFLR_EVENT_PENDING, pf->state))
  8641. i40e_service_event_schedule(pf);
  8642. }
  8643. /**
  8644. * i40e_service_timer - timer callback
  8645. * @data: pointer to PF struct
  8646. **/
  8647. static void i40e_service_timer(struct timer_list *t)
  8648. {
  8649. struct i40e_pf *pf = from_timer(pf, t, service_timer);
  8650. mod_timer(&pf->service_timer,
  8651. round_jiffies(jiffies + pf->service_timer_period));
  8652. i40e_service_event_schedule(pf);
  8653. }
  8654. /**
  8655. * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
  8656. * @vsi: the VSI being configured
  8657. **/
  8658. static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
  8659. {
  8660. struct i40e_pf *pf = vsi->back;
  8661. switch (vsi->type) {
  8662. case I40E_VSI_MAIN:
  8663. vsi->alloc_queue_pairs = pf->num_lan_qps;
  8664. vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
  8665. I40E_REQ_DESCRIPTOR_MULTIPLE);
  8666. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  8667. vsi->num_q_vectors = pf->num_lan_msix;
  8668. else
  8669. vsi->num_q_vectors = 1;
  8670. break;
  8671. case I40E_VSI_FDIR:
  8672. vsi->alloc_queue_pairs = 1;
  8673. vsi->num_desc = ALIGN(I40E_FDIR_RING_COUNT,
  8674. I40E_REQ_DESCRIPTOR_MULTIPLE);
  8675. vsi->num_q_vectors = pf->num_fdsb_msix;
  8676. break;
  8677. case I40E_VSI_VMDQ2:
  8678. vsi->alloc_queue_pairs = pf->num_vmdq_qps;
  8679. vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
  8680. I40E_REQ_DESCRIPTOR_MULTIPLE);
  8681. vsi->num_q_vectors = pf->num_vmdq_msix;
  8682. break;
  8683. case I40E_VSI_SRIOV:
  8684. vsi->alloc_queue_pairs = pf->num_vf_qps;
  8685. vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
  8686. I40E_REQ_DESCRIPTOR_MULTIPLE);
  8687. break;
  8688. default:
  8689. WARN_ON(1);
  8690. return -ENODATA;
  8691. }
  8692. return 0;
  8693. }
  8694. /**
  8695. * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
  8696. * @vsi: VSI pointer
  8697. * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
  8698. *
  8699. * On error: returns error code (negative)
  8700. * On success: returns 0
  8701. **/
  8702. static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
  8703. {
  8704. struct i40e_ring **next_rings;
  8705. int size;
  8706. int ret = 0;
  8707. /* allocate memory for both Tx, XDP Tx and Rx ring pointers */
  8708. size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs *
  8709. (i40e_enabled_xdp_vsi(vsi) ? 3 : 2);
  8710. vsi->tx_rings = kzalloc(size, GFP_KERNEL);
  8711. if (!vsi->tx_rings)
  8712. return -ENOMEM;
  8713. next_rings = vsi->tx_rings + vsi->alloc_queue_pairs;
  8714. if (i40e_enabled_xdp_vsi(vsi)) {
  8715. vsi->xdp_rings = next_rings;
  8716. next_rings += vsi->alloc_queue_pairs;
  8717. }
  8718. vsi->rx_rings = next_rings;
  8719. if (alloc_qvectors) {
  8720. /* allocate memory for q_vector pointers */
  8721. size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
  8722. vsi->q_vectors = kzalloc(size, GFP_KERNEL);
  8723. if (!vsi->q_vectors) {
  8724. ret = -ENOMEM;
  8725. goto err_vectors;
  8726. }
  8727. }
  8728. return ret;
  8729. err_vectors:
  8730. kfree(vsi->tx_rings);
  8731. return ret;
  8732. }
  8733. /**
  8734. * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
  8735. * @pf: board private structure
  8736. * @type: type of VSI
  8737. *
  8738. * On error: returns error code (negative)
  8739. * On success: returns vsi index in PF (positive)
  8740. **/
  8741. static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
  8742. {
  8743. int ret = -ENODEV;
  8744. struct i40e_vsi *vsi;
  8745. int vsi_idx;
  8746. int i;
  8747. /* Need to protect the allocation of the VSIs at the PF level */
  8748. mutex_lock(&pf->switch_mutex);
  8749. /* VSI list may be fragmented if VSI creation/destruction has
  8750. * been happening. We can afford to do a quick scan to look
  8751. * for any free VSIs in the list.
  8752. *
  8753. * find next empty vsi slot, looping back around if necessary
  8754. */
  8755. i = pf->next_vsi;
  8756. while (i < pf->num_alloc_vsi && pf->vsi[i])
  8757. i++;
  8758. if (i >= pf->num_alloc_vsi) {
  8759. i = 0;
  8760. while (i < pf->next_vsi && pf->vsi[i])
  8761. i++;
  8762. }
  8763. if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
  8764. vsi_idx = i; /* Found one! */
  8765. } else {
  8766. ret = -ENODEV;
  8767. goto unlock_pf; /* out of VSI slots! */
  8768. }
  8769. pf->next_vsi = ++i;
  8770. vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
  8771. if (!vsi) {
  8772. ret = -ENOMEM;
  8773. goto unlock_pf;
  8774. }
  8775. vsi->type = type;
  8776. vsi->back = pf;
  8777. set_bit(__I40E_VSI_DOWN, vsi->state);
  8778. vsi->flags = 0;
  8779. vsi->idx = vsi_idx;
  8780. vsi->int_rate_limit = 0;
  8781. vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
  8782. pf->rss_table_size : 64;
  8783. vsi->netdev_registered = false;
  8784. vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
  8785. hash_init(vsi->mac_filter_hash);
  8786. vsi->irqs_ready = false;
  8787. ret = i40e_set_num_rings_in_vsi(vsi);
  8788. if (ret)
  8789. goto err_rings;
  8790. ret = i40e_vsi_alloc_arrays(vsi, true);
  8791. if (ret)
  8792. goto err_rings;
  8793. /* Setup default MSIX irq handler for VSI */
  8794. i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
  8795. /* Initialize VSI lock */
  8796. spin_lock_init(&vsi->mac_filter_hash_lock);
  8797. pf->vsi[vsi_idx] = vsi;
  8798. ret = vsi_idx;
  8799. goto unlock_pf;
  8800. err_rings:
  8801. pf->next_vsi = i - 1;
  8802. kfree(vsi);
  8803. unlock_pf:
  8804. mutex_unlock(&pf->switch_mutex);
  8805. return ret;
  8806. }
  8807. /**
  8808. * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
  8809. * @vsi: VSI pointer
  8810. * @free_qvectors: a bool to specify if q_vectors need to be freed.
  8811. *
  8812. * On error: returns error code (negative)
  8813. * On success: returns 0
  8814. **/
  8815. static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
  8816. {
  8817. /* free the ring and vector containers */
  8818. if (free_qvectors) {
  8819. kfree(vsi->q_vectors);
  8820. vsi->q_vectors = NULL;
  8821. }
  8822. kfree(vsi->tx_rings);
  8823. vsi->tx_rings = NULL;
  8824. vsi->rx_rings = NULL;
  8825. vsi->xdp_rings = NULL;
  8826. }
  8827. /**
  8828. * i40e_clear_rss_config_user - clear the user configured RSS hash keys
  8829. * and lookup table
  8830. * @vsi: Pointer to VSI structure
  8831. */
  8832. static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
  8833. {
  8834. if (!vsi)
  8835. return;
  8836. kfree(vsi->rss_hkey_user);
  8837. vsi->rss_hkey_user = NULL;
  8838. kfree(vsi->rss_lut_user);
  8839. vsi->rss_lut_user = NULL;
  8840. }
  8841. /**
  8842. * i40e_vsi_clear - Deallocate the VSI provided
  8843. * @vsi: the VSI being un-configured
  8844. **/
  8845. static int i40e_vsi_clear(struct i40e_vsi *vsi)
  8846. {
  8847. struct i40e_pf *pf;
  8848. if (!vsi)
  8849. return 0;
  8850. if (!vsi->back)
  8851. goto free_vsi;
  8852. pf = vsi->back;
  8853. mutex_lock(&pf->switch_mutex);
  8854. if (!pf->vsi[vsi->idx]) {
  8855. dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](type %d)\n",
  8856. vsi->idx, vsi->idx, vsi->type);
  8857. goto unlock_vsi;
  8858. }
  8859. if (pf->vsi[vsi->idx] != vsi) {
  8860. dev_err(&pf->pdev->dev,
  8861. "pf->vsi[%d](type %d) != vsi[%d](type %d): no free!\n",
  8862. pf->vsi[vsi->idx]->idx,
  8863. pf->vsi[vsi->idx]->type,
  8864. vsi->idx, vsi->type);
  8865. goto unlock_vsi;
  8866. }
  8867. /* updates the PF for this cleared vsi */
  8868. i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
  8869. i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
  8870. i40e_vsi_free_arrays(vsi, true);
  8871. i40e_clear_rss_config_user(vsi);
  8872. pf->vsi[vsi->idx] = NULL;
  8873. if (vsi->idx < pf->next_vsi)
  8874. pf->next_vsi = vsi->idx;
  8875. unlock_vsi:
  8876. mutex_unlock(&pf->switch_mutex);
  8877. free_vsi:
  8878. kfree(vsi);
  8879. return 0;
  8880. }
  8881. /**
  8882. * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
  8883. * @vsi: the VSI being cleaned
  8884. **/
  8885. static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
  8886. {
  8887. int i;
  8888. if (vsi->tx_rings && vsi->tx_rings[0]) {
  8889. for (i = 0; i < vsi->alloc_queue_pairs; i++) {
  8890. kfree_rcu(vsi->tx_rings[i], rcu);
  8891. vsi->tx_rings[i] = NULL;
  8892. vsi->rx_rings[i] = NULL;
  8893. if (vsi->xdp_rings)
  8894. vsi->xdp_rings[i] = NULL;
  8895. }
  8896. }
  8897. }
  8898. /**
  8899. * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
  8900. * @vsi: the VSI being configured
  8901. **/
  8902. static int i40e_alloc_rings(struct i40e_vsi *vsi)
  8903. {
  8904. int i, qpv = i40e_enabled_xdp_vsi(vsi) ? 3 : 2;
  8905. struct i40e_pf *pf = vsi->back;
  8906. struct i40e_ring *ring;
  8907. /* Set basic values in the rings to be used later during open() */
  8908. for (i = 0; i < vsi->alloc_queue_pairs; i++) {
  8909. /* allocate space for both Tx and Rx in one shot */
  8910. ring = kcalloc(qpv, sizeof(struct i40e_ring), GFP_KERNEL);
  8911. if (!ring)
  8912. goto err_out;
  8913. ring->queue_index = i;
  8914. ring->reg_idx = vsi->base_queue + i;
  8915. ring->ring_active = false;
  8916. ring->vsi = vsi;
  8917. ring->netdev = vsi->netdev;
  8918. ring->dev = &pf->pdev->dev;
  8919. ring->count = vsi->num_desc;
  8920. ring->size = 0;
  8921. ring->dcb_tc = 0;
  8922. if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
  8923. ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
  8924. ring->itr_setting = pf->tx_itr_default;
  8925. vsi->tx_rings[i] = ring++;
  8926. if (!i40e_enabled_xdp_vsi(vsi))
  8927. goto setup_rx;
  8928. ring->queue_index = vsi->alloc_queue_pairs + i;
  8929. ring->reg_idx = vsi->base_queue + ring->queue_index;
  8930. ring->ring_active = false;
  8931. ring->vsi = vsi;
  8932. ring->netdev = NULL;
  8933. ring->dev = &pf->pdev->dev;
  8934. ring->count = vsi->num_desc;
  8935. ring->size = 0;
  8936. ring->dcb_tc = 0;
  8937. if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
  8938. ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
  8939. set_ring_xdp(ring);
  8940. ring->itr_setting = pf->tx_itr_default;
  8941. vsi->xdp_rings[i] = ring++;
  8942. setup_rx:
  8943. ring->queue_index = i;
  8944. ring->reg_idx = vsi->base_queue + i;
  8945. ring->ring_active = false;
  8946. ring->vsi = vsi;
  8947. ring->netdev = vsi->netdev;
  8948. ring->dev = &pf->pdev->dev;
  8949. ring->count = vsi->num_desc;
  8950. ring->size = 0;
  8951. ring->dcb_tc = 0;
  8952. ring->itr_setting = pf->rx_itr_default;
  8953. vsi->rx_rings[i] = ring;
  8954. }
  8955. return 0;
  8956. err_out:
  8957. i40e_vsi_clear_rings(vsi);
  8958. return -ENOMEM;
  8959. }
  8960. /**
  8961. * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
  8962. * @pf: board private structure
  8963. * @vectors: the number of MSI-X vectors to request
  8964. *
  8965. * Returns the number of vectors reserved, or error
  8966. **/
  8967. static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
  8968. {
  8969. vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
  8970. I40E_MIN_MSIX, vectors);
  8971. if (vectors < 0) {
  8972. dev_info(&pf->pdev->dev,
  8973. "MSI-X vector reservation failed: %d\n", vectors);
  8974. vectors = 0;
  8975. }
  8976. return vectors;
  8977. }
  8978. /**
  8979. * i40e_init_msix - Setup the MSIX capability
  8980. * @pf: board private structure
  8981. *
  8982. * Work with the OS to set up the MSIX vectors needed.
  8983. *
  8984. * Returns the number of vectors reserved or negative on failure
  8985. **/
  8986. static int i40e_init_msix(struct i40e_pf *pf)
  8987. {
  8988. struct i40e_hw *hw = &pf->hw;
  8989. int cpus, extra_vectors;
  8990. int vectors_left;
  8991. int v_budget, i;
  8992. int v_actual;
  8993. int iwarp_requested = 0;
  8994. if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
  8995. return -ENODEV;
  8996. /* The number of vectors we'll request will be comprised of:
  8997. * - Add 1 for "other" cause for Admin Queue events, etc.
  8998. * - The number of LAN queue pairs
  8999. * - Queues being used for RSS.
  9000. * We don't need as many as max_rss_size vectors.
  9001. * use rss_size instead in the calculation since that
  9002. * is governed by number of cpus in the system.
  9003. * - assumes symmetric Tx/Rx pairing
  9004. * - The number of VMDq pairs
  9005. * - The CPU count within the NUMA node if iWARP is enabled
  9006. * Once we count this up, try the request.
  9007. *
  9008. * If we can't get what we want, we'll simplify to nearly nothing
  9009. * and try again. If that still fails, we punt.
  9010. */
  9011. vectors_left = hw->func_caps.num_msix_vectors;
  9012. v_budget = 0;
  9013. /* reserve one vector for miscellaneous handler */
  9014. if (vectors_left) {
  9015. v_budget++;
  9016. vectors_left--;
  9017. }
  9018. /* reserve some vectors for the main PF traffic queues. Initially we
  9019. * only reserve at most 50% of the available vectors, in the case that
  9020. * the number of online CPUs is large. This ensures that we can enable
  9021. * extra features as well. Once we've enabled the other features, we
  9022. * will use any remaining vectors to reach as close as we can to the
  9023. * number of online CPUs.
  9024. */
  9025. cpus = num_online_cpus();
  9026. pf->num_lan_msix = min_t(int, cpus, vectors_left / 2);
  9027. vectors_left -= pf->num_lan_msix;
  9028. /* reserve one vector for sideband flow director */
  9029. if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
  9030. if (vectors_left) {
  9031. pf->num_fdsb_msix = 1;
  9032. v_budget++;
  9033. vectors_left--;
  9034. } else {
  9035. pf->num_fdsb_msix = 0;
  9036. }
  9037. }
  9038. /* can we reserve enough for iWARP? */
  9039. if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
  9040. iwarp_requested = pf->num_iwarp_msix;
  9041. if (!vectors_left)
  9042. pf->num_iwarp_msix = 0;
  9043. else if (vectors_left < pf->num_iwarp_msix)
  9044. pf->num_iwarp_msix = 1;
  9045. v_budget += pf->num_iwarp_msix;
  9046. vectors_left -= pf->num_iwarp_msix;
  9047. }
  9048. /* any vectors left over go for VMDq support */
  9049. if (pf->flags & I40E_FLAG_VMDQ_ENABLED) {
  9050. if (!vectors_left) {
  9051. pf->num_vmdq_msix = 0;
  9052. pf->num_vmdq_qps = 0;
  9053. } else {
  9054. int vmdq_vecs_wanted =
  9055. pf->num_vmdq_vsis * pf->num_vmdq_qps;
  9056. int vmdq_vecs =
  9057. min_t(int, vectors_left, vmdq_vecs_wanted);
  9058. /* if we're short on vectors for what's desired, we limit
  9059. * the queues per vmdq. If this is still more than are
  9060. * available, the user will need to change the number of
  9061. * queues/vectors used by the PF later with the ethtool
  9062. * channels command
  9063. */
  9064. if (vectors_left < vmdq_vecs_wanted) {
  9065. pf->num_vmdq_qps = 1;
  9066. vmdq_vecs_wanted = pf->num_vmdq_vsis;
  9067. vmdq_vecs = min_t(int,
  9068. vectors_left,
  9069. vmdq_vecs_wanted);
  9070. }
  9071. pf->num_vmdq_msix = pf->num_vmdq_qps;
  9072. v_budget += vmdq_vecs;
  9073. vectors_left -= vmdq_vecs;
  9074. }
  9075. }
  9076. /* On systems with a large number of SMP cores, we previously limited
  9077. * the number of vectors for num_lan_msix to be at most 50% of the
  9078. * available vectors, to allow for other features. Now, we add back
  9079. * the remaining vectors. However, we ensure that the total
  9080. * num_lan_msix will not exceed num_online_cpus(). To do this, we
  9081. * calculate the number of vectors we can add without going over the
  9082. * cap of CPUs. For systems with a small number of CPUs this will be
  9083. * zero.
  9084. */
  9085. extra_vectors = min_t(int, cpus - pf->num_lan_msix, vectors_left);
  9086. pf->num_lan_msix += extra_vectors;
  9087. vectors_left -= extra_vectors;
  9088. WARN(vectors_left < 0,
  9089. "Calculation of remaining vectors underflowed. This is an accounting bug when determining total MSI-X vectors.\n");
  9090. v_budget += pf->num_lan_msix;
  9091. pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
  9092. GFP_KERNEL);
  9093. if (!pf->msix_entries)
  9094. return -ENOMEM;
  9095. for (i = 0; i < v_budget; i++)
  9096. pf->msix_entries[i].entry = i;
  9097. v_actual = i40e_reserve_msix_vectors(pf, v_budget);
  9098. if (v_actual < I40E_MIN_MSIX) {
  9099. pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
  9100. kfree(pf->msix_entries);
  9101. pf->msix_entries = NULL;
  9102. pci_disable_msix(pf->pdev);
  9103. return -ENODEV;
  9104. } else if (v_actual == I40E_MIN_MSIX) {
  9105. /* Adjust for minimal MSIX use */
  9106. pf->num_vmdq_vsis = 0;
  9107. pf->num_vmdq_qps = 0;
  9108. pf->num_lan_qps = 1;
  9109. pf->num_lan_msix = 1;
  9110. } else if (v_actual != v_budget) {
  9111. /* If we have limited resources, we will start with no vectors
  9112. * for the special features and then allocate vectors to some
  9113. * of these features based on the policy and at the end disable
  9114. * the features that did not get any vectors.
  9115. */
  9116. int vec;
  9117. dev_info(&pf->pdev->dev,
  9118. "MSI-X vector limit reached with %d, wanted %d, attempting to redistribute vectors\n",
  9119. v_actual, v_budget);
  9120. /* reserve the misc vector */
  9121. vec = v_actual - 1;
  9122. /* Scale vector usage down */
  9123. pf->num_vmdq_msix = 1; /* force VMDqs to only one vector */
  9124. pf->num_vmdq_vsis = 1;
  9125. pf->num_vmdq_qps = 1;
  9126. /* partition out the remaining vectors */
  9127. switch (vec) {
  9128. case 2:
  9129. pf->num_lan_msix = 1;
  9130. break;
  9131. case 3:
  9132. if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
  9133. pf->num_lan_msix = 1;
  9134. pf->num_iwarp_msix = 1;
  9135. } else {
  9136. pf->num_lan_msix = 2;
  9137. }
  9138. break;
  9139. default:
  9140. if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
  9141. pf->num_iwarp_msix = min_t(int, (vec / 3),
  9142. iwarp_requested);
  9143. pf->num_vmdq_vsis = min_t(int, (vec / 3),
  9144. I40E_DEFAULT_NUM_VMDQ_VSI);
  9145. } else {
  9146. pf->num_vmdq_vsis = min_t(int, (vec / 2),
  9147. I40E_DEFAULT_NUM_VMDQ_VSI);
  9148. }
  9149. if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
  9150. pf->num_fdsb_msix = 1;
  9151. vec--;
  9152. }
  9153. pf->num_lan_msix = min_t(int,
  9154. (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
  9155. pf->num_lan_msix);
  9156. pf->num_lan_qps = pf->num_lan_msix;
  9157. break;
  9158. }
  9159. }
  9160. if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
  9161. (pf->num_fdsb_msix == 0)) {
  9162. dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n");
  9163. pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
  9164. pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
  9165. }
  9166. if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
  9167. (pf->num_vmdq_msix == 0)) {
  9168. dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
  9169. pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
  9170. }
  9171. if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
  9172. (pf->num_iwarp_msix == 0)) {
  9173. dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
  9174. pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
  9175. }
  9176. i40e_debug(&pf->hw, I40E_DEBUG_INIT,
  9177. "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n",
  9178. pf->num_lan_msix,
  9179. pf->num_vmdq_msix * pf->num_vmdq_vsis,
  9180. pf->num_fdsb_msix,
  9181. pf->num_iwarp_msix);
  9182. return v_actual;
  9183. }
  9184. /**
  9185. * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
  9186. * @vsi: the VSI being configured
  9187. * @v_idx: index of the vector in the vsi struct
  9188. * @cpu: cpu to be used on affinity_mask
  9189. *
  9190. * We allocate one q_vector. If allocation fails we return -ENOMEM.
  9191. **/
  9192. static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx, int cpu)
  9193. {
  9194. struct i40e_q_vector *q_vector;
  9195. /* allocate q_vector */
  9196. q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
  9197. if (!q_vector)
  9198. return -ENOMEM;
  9199. q_vector->vsi = vsi;
  9200. q_vector->v_idx = v_idx;
  9201. cpumask_copy(&q_vector->affinity_mask, cpu_possible_mask);
  9202. if (vsi->netdev)
  9203. netif_napi_add(vsi->netdev, &q_vector->napi,
  9204. i40e_napi_poll, NAPI_POLL_WEIGHT);
  9205. /* tie q_vector and vsi together */
  9206. vsi->q_vectors[v_idx] = q_vector;
  9207. return 0;
  9208. }
  9209. /**
  9210. * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
  9211. * @vsi: the VSI being configured
  9212. *
  9213. * We allocate one q_vector per queue interrupt. If allocation fails we
  9214. * return -ENOMEM.
  9215. **/
  9216. static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
  9217. {
  9218. struct i40e_pf *pf = vsi->back;
  9219. int err, v_idx, num_q_vectors, current_cpu;
  9220. /* if not MSIX, give the one vector only to the LAN VSI */
  9221. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  9222. num_q_vectors = vsi->num_q_vectors;
  9223. else if (vsi == pf->vsi[pf->lan_vsi])
  9224. num_q_vectors = 1;
  9225. else
  9226. return -EINVAL;
  9227. current_cpu = cpumask_first(cpu_online_mask);
  9228. for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
  9229. err = i40e_vsi_alloc_q_vector(vsi, v_idx, current_cpu);
  9230. if (err)
  9231. goto err_out;
  9232. current_cpu = cpumask_next(current_cpu, cpu_online_mask);
  9233. if (unlikely(current_cpu >= nr_cpu_ids))
  9234. current_cpu = cpumask_first(cpu_online_mask);
  9235. }
  9236. return 0;
  9237. err_out:
  9238. while (v_idx--)
  9239. i40e_free_q_vector(vsi, v_idx);
  9240. return err;
  9241. }
  9242. /**
  9243. * i40e_init_interrupt_scheme - Determine proper interrupt scheme
  9244. * @pf: board private structure to initialize
  9245. **/
  9246. static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
  9247. {
  9248. int vectors = 0;
  9249. ssize_t size;
  9250. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  9251. vectors = i40e_init_msix(pf);
  9252. if (vectors < 0) {
  9253. pf->flags &= ~(I40E_FLAG_MSIX_ENABLED |
  9254. I40E_FLAG_IWARP_ENABLED |
  9255. I40E_FLAG_RSS_ENABLED |
  9256. I40E_FLAG_DCB_CAPABLE |
  9257. I40E_FLAG_DCB_ENABLED |
  9258. I40E_FLAG_SRIOV_ENABLED |
  9259. I40E_FLAG_FD_SB_ENABLED |
  9260. I40E_FLAG_FD_ATR_ENABLED |
  9261. I40E_FLAG_VMDQ_ENABLED);
  9262. pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
  9263. /* rework the queue expectations without MSIX */
  9264. i40e_determine_queue_usage(pf);
  9265. }
  9266. }
  9267. if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
  9268. (pf->flags & I40E_FLAG_MSI_ENABLED)) {
  9269. dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
  9270. vectors = pci_enable_msi(pf->pdev);
  9271. if (vectors < 0) {
  9272. dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
  9273. vectors);
  9274. pf->flags &= ~I40E_FLAG_MSI_ENABLED;
  9275. }
  9276. vectors = 1; /* one MSI or Legacy vector */
  9277. }
  9278. if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
  9279. dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
  9280. /* set up vector assignment tracking */
  9281. size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
  9282. pf->irq_pile = kzalloc(size, GFP_KERNEL);
  9283. if (!pf->irq_pile)
  9284. return -ENOMEM;
  9285. pf->irq_pile->num_entries = vectors;
  9286. pf->irq_pile->search_hint = 0;
  9287. /* track first vector for misc interrupts, ignore return */
  9288. (void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
  9289. return 0;
  9290. }
  9291. /**
  9292. * i40e_restore_interrupt_scheme - Restore the interrupt scheme
  9293. * @pf: private board data structure
  9294. *
  9295. * Restore the interrupt scheme that was cleared when we suspended the
  9296. * device. This should be called during resume to re-allocate the q_vectors
  9297. * and reacquire IRQs.
  9298. */
  9299. static int i40e_restore_interrupt_scheme(struct i40e_pf *pf)
  9300. {
  9301. int err, i;
  9302. /* We cleared the MSI and MSI-X flags when disabling the old interrupt
  9303. * scheme. We need to re-enabled them here in order to attempt to
  9304. * re-acquire the MSI or MSI-X vectors
  9305. */
  9306. pf->flags |= (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
  9307. err = i40e_init_interrupt_scheme(pf);
  9308. if (err)
  9309. return err;
  9310. /* Now that we've re-acquired IRQs, we need to remap the vectors and
  9311. * rings together again.
  9312. */
  9313. for (i = 0; i < pf->num_alloc_vsi; i++) {
  9314. if (pf->vsi[i]) {
  9315. err = i40e_vsi_alloc_q_vectors(pf->vsi[i]);
  9316. if (err)
  9317. goto err_unwind;
  9318. i40e_vsi_map_rings_to_vectors(pf->vsi[i]);
  9319. }
  9320. }
  9321. err = i40e_setup_misc_vector(pf);
  9322. if (err)
  9323. goto err_unwind;
  9324. if (pf->flags & I40E_FLAG_IWARP_ENABLED)
  9325. i40e_client_update_msix_info(pf);
  9326. return 0;
  9327. err_unwind:
  9328. while (i--) {
  9329. if (pf->vsi[i])
  9330. i40e_vsi_free_q_vectors(pf->vsi[i]);
  9331. }
  9332. return err;
  9333. }
  9334. /**
  9335. * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
  9336. * @pf: board private structure
  9337. *
  9338. * This sets up the handler for MSIX 0, which is used to manage the
  9339. * non-queue interrupts, e.g. AdminQ and errors. This is not used
  9340. * when in MSI or Legacy interrupt mode.
  9341. **/
  9342. static int i40e_setup_misc_vector(struct i40e_pf *pf)
  9343. {
  9344. struct i40e_hw *hw = &pf->hw;
  9345. int err = 0;
  9346. /* Only request the IRQ once, the first time through. */
  9347. if (!test_and_set_bit(__I40E_MISC_IRQ_REQUESTED, pf->state)) {
  9348. err = request_irq(pf->msix_entries[0].vector,
  9349. i40e_intr, 0, pf->int_name, pf);
  9350. if (err) {
  9351. clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
  9352. dev_info(&pf->pdev->dev,
  9353. "request_irq for %s failed: %d\n",
  9354. pf->int_name, err);
  9355. return -EFAULT;
  9356. }
  9357. }
  9358. i40e_enable_misc_int_causes(pf);
  9359. /* associate no queues to the misc vector */
  9360. wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
  9361. wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K);
  9362. i40e_flush(hw);
  9363. i40e_irq_dynamic_enable_icr0(pf);
  9364. return err;
  9365. }
  9366. /**
  9367. * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
  9368. * @vsi: Pointer to vsi structure
  9369. * @seed: Buffter to store the hash keys
  9370. * @lut: Buffer to store the lookup table entries
  9371. * @lut_size: Size of buffer to store the lookup table entries
  9372. *
  9373. * Return 0 on success, negative on failure
  9374. */
  9375. static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
  9376. u8 *lut, u16 lut_size)
  9377. {
  9378. struct i40e_pf *pf = vsi->back;
  9379. struct i40e_hw *hw = &pf->hw;
  9380. int ret = 0;
  9381. if (seed) {
  9382. ret = i40e_aq_get_rss_key(hw, vsi->id,
  9383. (struct i40e_aqc_get_set_rss_key_data *)seed);
  9384. if (ret) {
  9385. dev_info(&pf->pdev->dev,
  9386. "Cannot get RSS key, err %s aq_err %s\n",
  9387. i40e_stat_str(&pf->hw, ret),
  9388. i40e_aq_str(&pf->hw,
  9389. pf->hw.aq.asq_last_status));
  9390. return ret;
  9391. }
  9392. }
  9393. if (lut) {
  9394. bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
  9395. ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
  9396. if (ret) {
  9397. dev_info(&pf->pdev->dev,
  9398. "Cannot get RSS lut, err %s aq_err %s\n",
  9399. i40e_stat_str(&pf->hw, ret),
  9400. i40e_aq_str(&pf->hw,
  9401. pf->hw.aq.asq_last_status));
  9402. return ret;
  9403. }
  9404. }
  9405. return ret;
  9406. }
  9407. /**
  9408. * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
  9409. * @vsi: Pointer to vsi structure
  9410. * @seed: RSS hash seed
  9411. * @lut: Lookup table
  9412. * @lut_size: Lookup table size
  9413. *
  9414. * Returns 0 on success, negative on failure
  9415. **/
  9416. static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
  9417. const u8 *lut, u16 lut_size)
  9418. {
  9419. struct i40e_pf *pf = vsi->back;
  9420. struct i40e_hw *hw = &pf->hw;
  9421. u16 vf_id = vsi->vf_id;
  9422. u8 i;
  9423. /* Fill out hash function seed */
  9424. if (seed) {
  9425. u32 *seed_dw = (u32 *)seed;
  9426. if (vsi->type == I40E_VSI_MAIN) {
  9427. for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
  9428. wr32(hw, I40E_PFQF_HKEY(i), seed_dw[i]);
  9429. } else if (vsi->type == I40E_VSI_SRIOV) {
  9430. for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
  9431. wr32(hw, I40E_VFQF_HKEY1(i, vf_id), seed_dw[i]);
  9432. } else {
  9433. dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
  9434. }
  9435. }
  9436. if (lut) {
  9437. u32 *lut_dw = (u32 *)lut;
  9438. if (vsi->type == I40E_VSI_MAIN) {
  9439. if (lut_size != I40E_HLUT_ARRAY_SIZE)
  9440. return -EINVAL;
  9441. for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
  9442. wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
  9443. } else if (vsi->type == I40E_VSI_SRIOV) {
  9444. if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
  9445. return -EINVAL;
  9446. for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
  9447. wr32(hw, I40E_VFQF_HLUT1(i, vf_id), lut_dw[i]);
  9448. } else {
  9449. dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
  9450. }
  9451. }
  9452. i40e_flush(hw);
  9453. return 0;
  9454. }
  9455. /**
  9456. * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
  9457. * @vsi: Pointer to VSI structure
  9458. * @seed: Buffer to store the keys
  9459. * @lut: Buffer to store the lookup table entries
  9460. * @lut_size: Size of buffer to store the lookup table entries
  9461. *
  9462. * Returns 0 on success, negative on failure
  9463. */
  9464. static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
  9465. u8 *lut, u16 lut_size)
  9466. {
  9467. struct i40e_pf *pf = vsi->back;
  9468. struct i40e_hw *hw = &pf->hw;
  9469. u16 i;
  9470. if (seed) {
  9471. u32 *seed_dw = (u32 *)seed;
  9472. for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
  9473. seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
  9474. }
  9475. if (lut) {
  9476. u32 *lut_dw = (u32 *)lut;
  9477. if (lut_size != I40E_HLUT_ARRAY_SIZE)
  9478. return -EINVAL;
  9479. for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
  9480. lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
  9481. }
  9482. return 0;
  9483. }
  9484. /**
  9485. * i40e_config_rss - Configure RSS keys and lut
  9486. * @vsi: Pointer to VSI structure
  9487. * @seed: RSS hash seed
  9488. * @lut: Lookup table
  9489. * @lut_size: Lookup table size
  9490. *
  9491. * Returns 0 on success, negative on failure
  9492. */
  9493. int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
  9494. {
  9495. struct i40e_pf *pf = vsi->back;
  9496. if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
  9497. return i40e_config_rss_aq(vsi, seed, lut, lut_size);
  9498. else
  9499. return i40e_config_rss_reg(vsi, seed, lut, lut_size);
  9500. }
  9501. /**
  9502. * i40e_get_rss - Get RSS keys and lut
  9503. * @vsi: Pointer to VSI structure
  9504. * @seed: Buffer to store the keys
  9505. * @lut: Buffer to store the lookup table entries
  9506. * @lut_size: Size of buffer to store the lookup table entries
  9507. *
  9508. * Returns 0 on success, negative on failure
  9509. */
  9510. int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
  9511. {
  9512. struct i40e_pf *pf = vsi->back;
  9513. if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
  9514. return i40e_get_rss_aq(vsi, seed, lut, lut_size);
  9515. else
  9516. return i40e_get_rss_reg(vsi, seed, lut, lut_size);
  9517. }
  9518. /**
  9519. * i40e_fill_rss_lut - Fill the RSS lookup table with default values
  9520. * @pf: Pointer to board private structure
  9521. * @lut: Lookup table
  9522. * @rss_table_size: Lookup table size
  9523. * @rss_size: Range of queue number for hashing
  9524. */
  9525. void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
  9526. u16 rss_table_size, u16 rss_size)
  9527. {
  9528. u16 i;
  9529. for (i = 0; i < rss_table_size; i++)
  9530. lut[i] = i % rss_size;
  9531. }
  9532. /**
  9533. * i40e_pf_config_rss - Prepare for RSS if used
  9534. * @pf: board private structure
  9535. **/
  9536. static int i40e_pf_config_rss(struct i40e_pf *pf)
  9537. {
  9538. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  9539. u8 seed[I40E_HKEY_ARRAY_SIZE];
  9540. u8 *lut;
  9541. struct i40e_hw *hw = &pf->hw;
  9542. u32 reg_val;
  9543. u64 hena;
  9544. int ret;
  9545. /* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
  9546. hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
  9547. ((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
  9548. hena |= i40e_pf_get_default_rss_hena(pf);
  9549. i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
  9550. i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
  9551. /* Determine the RSS table size based on the hardware capabilities */
  9552. reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
  9553. reg_val = (pf->rss_table_size == 512) ?
  9554. (reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
  9555. (reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
  9556. i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
  9557. /* Determine the RSS size of the VSI */
  9558. if (!vsi->rss_size) {
  9559. u16 qcount;
  9560. /* If the firmware does something weird during VSI init, we
  9561. * could end up with zero TCs. Check for that to avoid
  9562. * divide-by-zero. It probably won't pass traffic, but it also
  9563. * won't panic.
  9564. */
  9565. qcount = vsi->num_queue_pairs /
  9566. (vsi->tc_config.numtc ? vsi->tc_config.numtc : 1);
  9567. vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
  9568. }
  9569. if (!vsi->rss_size)
  9570. return -EINVAL;
  9571. lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
  9572. if (!lut)
  9573. return -ENOMEM;
  9574. /* Use user configured lut if there is one, otherwise use default */
  9575. if (vsi->rss_lut_user)
  9576. memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
  9577. else
  9578. i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
  9579. /* Use user configured hash key if there is one, otherwise
  9580. * use default.
  9581. */
  9582. if (vsi->rss_hkey_user)
  9583. memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
  9584. else
  9585. netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
  9586. ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
  9587. kfree(lut);
  9588. return ret;
  9589. }
  9590. /**
  9591. * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
  9592. * @pf: board private structure
  9593. * @queue_count: the requested queue count for rss.
  9594. *
  9595. * returns 0 if rss is not enabled, if enabled returns the final rss queue
  9596. * count which may be different from the requested queue count.
  9597. * Note: expects to be called while under rtnl_lock()
  9598. **/
  9599. int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
  9600. {
  9601. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  9602. int new_rss_size;
  9603. if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
  9604. return 0;
  9605. new_rss_size = min_t(int, queue_count, pf->rss_size_max);
  9606. if (queue_count != vsi->num_queue_pairs) {
  9607. u16 qcount;
  9608. vsi->req_queue_pairs = queue_count;
  9609. i40e_prep_for_reset(pf, true);
  9610. pf->alloc_rss_size = new_rss_size;
  9611. i40e_reset_and_rebuild(pf, true, true);
  9612. /* Discard the user configured hash keys and lut, if less
  9613. * queues are enabled.
  9614. */
  9615. if (queue_count < vsi->rss_size) {
  9616. i40e_clear_rss_config_user(vsi);
  9617. dev_dbg(&pf->pdev->dev,
  9618. "discard user configured hash keys and lut\n");
  9619. }
  9620. /* Reset vsi->rss_size, as number of enabled queues changed */
  9621. qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
  9622. vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
  9623. i40e_pf_config_rss(pf);
  9624. }
  9625. dev_info(&pf->pdev->dev, "User requested queue count/HW max RSS count: %d/%d\n",
  9626. vsi->req_queue_pairs, pf->rss_size_max);
  9627. return pf->alloc_rss_size;
  9628. }
  9629. /**
  9630. * i40e_get_partition_bw_setting - Retrieve BW settings for this PF partition
  9631. * @pf: board private structure
  9632. **/
  9633. i40e_status i40e_get_partition_bw_setting(struct i40e_pf *pf)
  9634. {
  9635. i40e_status status;
  9636. bool min_valid, max_valid;
  9637. u32 max_bw, min_bw;
  9638. status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
  9639. &min_valid, &max_valid);
  9640. if (!status) {
  9641. if (min_valid)
  9642. pf->min_bw = min_bw;
  9643. if (max_valid)
  9644. pf->max_bw = max_bw;
  9645. }
  9646. return status;
  9647. }
  9648. /**
  9649. * i40e_set_partition_bw_setting - Set BW settings for this PF partition
  9650. * @pf: board private structure
  9651. **/
  9652. i40e_status i40e_set_partition_bw_setting(struct i40e_pf *pf)
  9653. {
  9654. struct i40e_aqc_configure_partition_bw_data bw_data;
  9655. i40e_status status;
  9656. /* Set the valid bit for this PF */
  9657. bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
  9658. bw_data.max_bw[pf->hw.pf_id] = pf->max_bw & I40E_ALT_BW_VALUE_MASK;
  9659. bw_data.min_bw[pf->hw.pf_id] = pf->min_bw & I40E_ALT_BW_VALUE_MASK;
  9660. /* Set the new bandwidths */
  9661. status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
  9662. return status;
  9663. }
  9664. /**
  9665. * i40e_commit_partition_bw_setting - Commit BW settings for this PF partition
  9666. * @pf: board private structure
  9667. **/
  9668. i40e_status i40e_commit_partition_bw_setting(struct i40e_pf *pf)
  9669. {
  9670. /* Commit temporary BW setting to permanent NVM image */
  9671. enum i40e_admin_queue_err last_aq_status;
  9672. i40e_status ret;
  9673. u16 nvm_word;
  9674. if (pf->hw.partition_id != 1) {
  9675. dev_info(&pf->pdev->dev,
  9676. "Commit BW only works on partition 1! This is partition %d",
  9677. pf->hw.partition_id);
  9678. ret = I40E_NOT_SUPPORTED;
  9679. goto bw_commit_out;
  9680. }
  9681. /* Acquire NVM for read access */
  9682. ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
  9683. last_aq_status = pf->hw.aq.asq_last_status;
  9684. if (ret) {
  9685. dev_info(&pf->pdev->dev,
  9686. "Cannot acquire NVM for read access, err %s aq_err %s\n",
  9687. i40e_stat_str(&pf->hw, ret),
  9688. i40e_aq_str(&pf->hw, last_aq_status));
  9689. goto bw_commit_out;
  9690. }
  9691. /* Read word 0x10 of NVM - SW compatibility word 1 */
  9692. ret = i40e_aq_read_nvm(&pf->hw,
  9693. I40E_SR_NVM_CONTROL_WORD,
  9694. 0x10, sizeof(nvm_word), &nvm_word,
  9695. false, NULL);
  9696. /* Save off last admin queue command status before releasing
  9697. * the NVM
  9698. */
  9699. last_aq_status = pf->hw.aq.asq_last_status;
  9700. i40e_release_nvm(&pf->hw);
  9701. if (ret) {
  9702. dev_info(&pf->pdev->dev, "NVM read error, err %s aq_err %s\n",
  9703. i40e_stat_str(&pf->hw, ret),
  9704. i40e_aq_str(&pf->hw, last_aq_status));
  9705. goto bw_commit_out;
  9706. }
  9707. /* Wait a bit for NVM release to complete */
  9708. msleep(50);
  9709. /* Acquire NVM for write access */
  9710. ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
  9711. last_aq_status = pf->hw.aq.asq_last_status;
  9712. if (ret) {
  9713. dev_info(&pf->pdev->dev,
  9714. "Cannot acquire NVM for write access, err %s aq_err %s\n",
  9715. i40e_stat_str(&pf->hw, ret),
  9716. i40e_aq_str(&pf->hw, last_aq_status));
  9717. goto bw_commit_out;
  9718. }
  9719. /* Write it back out unchanged to initiate update NVM,
  9720. * which will force a write of the shadow (alt) RAM to
  9721. * the NVM - thus storing the bandwidth values permanently.
  9722. */
  9723. ret = i40e_aq_update_nvm(&pf->hw,
  9724. I40E_SR_NVM_CONTROL_WORD,
  9725. 0x10, sizeof(nvm_word),
  9726. &nvm_word, true, 0, NULL);
  9727. /* Save off last admin queue command status before releasing
  9728. * the NVM
  9729. */
  9730. last_aq_status = pf->hw.aq.asq_last_status;
  9731. i40e_release_nvm(&pf->hw);
  9732. if (ret)
  9733. dev_info(&pf->pdev->dev,
  9734. "BW settings NOT SAVED, err %s aq_err %s\n",
  9735. i40e_stat_str(&pf->hw, ret),
  9736. i40e_aq_str(&pf->hw, last_aq_status));
  9737. bw_commit_out:
  9738. return ret;
  9739. }
  9740. /**
  9741. * i40e_sw_init - Initialize general software structures (struct i40e_pf)
  9742. * @pf: board private structure to initialize
  9743. *
  9744. * i40e_sw_init initializes the Adapter private data structure.
  9745. * Fields are initialized based on PCI device information and
  9746. * OS network device settings (MTU size).
  9747. **/
  9748. static int i40e_sw_init(struct i40e_pf *pf)
  9749. {
  9750. int err = 0;
  9751. int size;
  9752. /* Set default capability flags */
  9753. pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
  9754. I40E_FLAG_MSI_ENABLED |
  9755. I40E_FLAG_MSIX_ENABLED;
  9756. /* Set default ITR */
  9757. pf->rx_itr_default = I40E_ITR_RX_DEF;
  9758. pf->tx_itr_default = I40E_ITR_TX_DEF;
  9759. /* Depending on PF configurations, it is possible that the RSS
  9760. * maximum might end up larger than the available queues
  9761. */
  9762. pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
  9763. pf->alloc_rss_size = 1;
  9764. pf->rss_table_size = pf->hw.func_caps.rss_table_size;
  9765. pf->rss_size_max = min_t(int, pf->rss_size_max,
  9766. pf->hw.func_caps.num_tx_qp);
  9767. if (pf->hw.func_caps.rss) {
  9768. pf->flags |= I40E_FLAG_RSS_ENABLED;
  9769. pf->alloc_rss_size = min_t(int, pf->rss_size_max,
  9770. num_online_cpus());
  9771. }
  9772. /* MFP mode enabled */
  9773. if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
  9774. pf->flags |= I40E_FLAG_MFP_ENABLED;
  9775. dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
  9776. if (i40e_get_partition_bw_setting(pf)) {
  9777. dev_warn(&pf->pdev->dev,
  9778. "Could not get partition bw settings\n");
  9779. } else {
  9780. dev_info(&pf->pdev->dev,
  9781. "Partition BW Min = %8.8x, Max = %8.8x\n",
  9782. pf->min_bw, pf->max_bw);
  9783. /* nudge the Tx scheduler */
  9784. i40e_set_partition_bw_setting(pf);
  9785. }
  9786. }
  9787. if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
  9788. (pf->hw.func_caps.fd_filters_best_effort > 0)) {
  9789. pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
  9790. pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
  9791. if (pf->flags & I40E_FLAG_MFP_ENABLED &&
  9792. pf->hw.num_partitions > 1)
  9793. dev_info(&pf->pdev->dev,
  9794. "Flow Director Sideband mode Disabled in MFP mode\n");
  9795. else
  9796. pf->flags |= I40E_FLAG_FD_SB_ENABLED;
  9797. pf->fdir_pf_filter_count =
  9798. pf->hw.func_caps.fd_filters_guaranteed;
  9799. pf->hw.fdir_shared_filter_count =
  9800. pf->hw.func_caps.fd_filters_best_effort;
  9801. }
  9802. if (pf->hw.mac.type == I40E_MAC_X722) {
  9803. pf->hw_features |= (I40E_HW_RSS_AQ_CAPABLE |
  9804. I40E_HW_128_QP_RSS_CAPABLE |
  9805. I40E_HW_ATR_EVICT_CAPABLE |
  9806. I40E_HW_WB_ON_ITR_CAPABLE |
  9807. I40E_HW_MULTIPLE_TCP_UDP_RSS_PCTYPE |
  9808. I40E_HW_NO_PCI_LINK_CHECK |
  9809. I40E_HW_USE_SET_LLDP_MIB |
  9810. I40E_HW_GENEVE_OFFLOAD_CAPABLE |
  9811. I40E_HW_PTP_L4_CAPABLE |
  9812. I40E_HW_WOL_MC_MAGIC_PKT_WAKE |
  9813. I40E_HW_OUTER_UDP_CSUM_CAPABLE);
  9814. #define I40E_FDEVICT_PCTYPE_DEFAULT 0xc03
  9815. if (rd32(&pf->hw, I40E_GLQF_FDEVICTENA(1)) !=
  9816. I40E_FDEVICT_PCTYPE_DEFAULT) {
  9817. dev_warn(&pf->pdev->dev,
  9818. "FD EVICT PCTYPES are not right, disable FD HW EVICT\n");
  9819. pf->hw_features &= ~I40E_HW_ATR_EVICT_CAPABLE;
  9820. }
  9821. } else if ((pf->hw.aq.api_maj_ver > 1) ||
  9822. ((pf->hw.aq.api_maj_ver == 1) &&
  9823. (pf->hw.aq.api_min_ver > 4))) {
  9824. /* Supported in FW API version higher than 1.4 */
  9825. pf->hw_features |= I40E_HW_GENEVE_OFFLOAD_CAPABLE;
  9826. }
  9827. /* Enable HW ATR eviction if possible */
  9828. if (pf->hw_features & I40E_HW_ATR_EVICT_CAPABLE)
  9829. pf->flags |= I40E_FLAG_HW_ATR_EVICT_ENABLED;
  9830. if ((pf->hw.mac.type == I40E_MAC_XL710) &&
  9831. (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
  9832. (pf->hw.aq.fw_maj_ver < 4))) {
  9833. pf->hw_features |= I40E_HW_RESTART_AUTONEG;
  9834. /* No DCB support for FW < v4.33 */
  9835. pf->hw_features |= I40E_HW_NO_DCB_SUPPORT;
  9836. }
  9837. /* Disable FW LLDP if FW < v4.3 */
  9838. if ((pf->hw.mac.type == I40E_MAC_XL710) &&
  9839. (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
  9840. (pf->hw.aq.fw_maj_ver < 4)))
  9841. pf->hw_features |= I40E_HW_STOP_FW_LLDP;
  9842. /* Use the FW Set LLDP MIB API if FW > v4.40 */
  9843. if ((pf->hw.mac.type == I40E_MAC_XL710) &&
  9844. (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver >= 40)) ||
  9845. (pf->hw.aq.fw_maj_ver >= 5)))
  9846. pf->hw_features |= I40E_HW_USE_SET_LLDP_MIB;
  9847. /* Enable PTP L4 if FW > v6.0 */
  9848. if (pf->hw.mac.type == I40E_MAC_XL710 &&
  9849. pf->hw.aq.fw_maj_ver >= 6)
  9850. pf->hw_features |= I40E_HW_PTP_L4_CAPABLE;
  9851. if (pf->hw.func_caps.vmdq && num_online_cpus() != 1) {
  9852. pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
  9853. pf->flags |= I40E_FLAG_VMDQ_ENABLED;
  9854. pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
  9855. }
  9856. if (pf->hw.func_caps.iwarp && num_online_cpus() != 1) {
  9857. pf->flags |= I40E_FLAG_IWARP_ENABLED;
  9858. /* IWARP needs one extra vector for CQP just like MISC.*/
  9859. pf->num_iwarp_msix = (int)num_online_cpus() + 1;
  9860. }
  9861. /* Stopping the FW LLDP engine is only supported on the
  9862. * XL710 with a FW ver >= 1.7. Also, stopping FW LLDP
  9863. * engine is not supported if NPAR is functioning on this
  9864. * part
  9865. */
  9866. if (pf->hw.mac.type == I40E_MAC_XL710 &&
  9867. !pf->hw.func_caps.npar_enable &&
  9868. (pf->hw.aq.api_maj_ver > 1 ||
  9869. (pf->hw.aq.api_maj_ver == 1 && pf->hw.aq.api_min_ver > 6)))
  9870. pf->hw_features |= I40E_HW_STOPPABLE_FW_LLDP;
  9871. #ifdef CONFIG_PCI_IOV
  9872. if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
  9873. pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
  9874. pf->flags |= I40E_FLAG_SRIOV_ENABLED;
  9875. pf->num_req_vfs = min_t(int,
  9876. pf->hw.func_caps.num_vfs,
  9877. I40E_MAX_VF_COUNT);
  9878. }
  9879. #endif /* CONFIG_PCI_IOV */
  9880. pf->eeprom_version = 0xDEAD;
  9881. pf->lan_veb = I40E_NO_VEB;
  9882. pf->lan_vsi = I40E_NO_VSI;
  9883. /* By default FW has this off for performance reasons */
  9884. pf->flags &= ~I40E_FLAG_VEB_STATS_ENABLED;
  9885. /* set up queue assignment tracking */
  9886. size = sizeof(struct i40e_lump_tracking)
  9887. + (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
  9888. pf->qp_pile = kzalloc(size, GFP_KERNEL);
  9889. if (!pf->qp_pile) {
  9890. err = -ENOMEM;
  9891. goto sw_init_done;
  9892. }
  9893. pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
  9894. pf->qp_pile->search_hint = 0;
  9895. pf->tx_timeout_recovery_level = 1;
  9896. mutex_init(&pf->switch_mutex);
  9897. sw_init_done:
  9898. return err;
  9899. }
  9900. /**
  9901. * i40e_set_ntuple - set the ntuple feature flag and take action
  9902. * @pf: board private structure to initialize
  9903. * @features: the feature set that the stack is suggesting
  9904. *
  9905. * returns a bool to indicate if reset needs to happen
  9906. **/
  9907. bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
  9908. {
  9909. bool need_reset = false;
  9910. /* Check if Flow Director n-tuple support was enabled or disabled. If
  9911. * the state changed, we need to reset.
  9912. */
  9913. if (features & NETIF_F_NTUPLE) {
  9914. /* Enable filters and mark for reset */
  9915. if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
  9916. need_reset = true;
  9917. /* enable FD_SB only if there is MSI-X vector and no cloud
  9918. * filters exist
  9919. */
  9920. if (pf->num_fdsb_msix > 0 && !pf->num_cloud_filters) {
  9921. pf->flags |= I40E_FLAG_FD_SB_ENABLED;
  9922. pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
  9923. }
  9924. } else {
  9925. /* turn off filters, mark for reset and clear SW filter list */
  9926. if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
  9927. need_reset = true;
  9928. i40e_fdir_filter_exit(pf);
  9929. }
  9930. pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
  9931. clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state);
  9932. pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
  9933. /* reset fd counters */
  9934. pf->fd_add_err = 0;
  9935. pf->fd_atr_cnt = 0;
  9936. /* if ATR was auto disabled it can be re-enabled. */
  9937. if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
  9938. if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
  9939. (I40E_DEBUG_FD & pf->hw.debug_mask))
  9940. dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
  9941. }
  9942. return need_reset;
  9943. }
  9944. /**
  9945. * i40e_clear_rss_lut - clear the rx hash lookup table
  9946. * @vsi: the VSI being configured
  9947. **/
  9948. static void i40e_clear_rss_lut(struct i40e_vsi *vsi)
  9949. {
  9950. struct i40e_pf *pf = vsi->back;
  9951. struct i40e_hw *hw = &pf->hw;
  9952. u16 vf_id = vsi->vf_id;
  9953. u8 i;
  9954. if (vsi->type == I40E_VSI_MAIN) {
  9955. for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
  9956. wr32(hw, I40E_PFQF_HLUT(i), 0);
  9957. } else if (vsi->type == I40E_VSI_SRIOV) {
  9958. for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
  9959. i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0);
  9960. } else {
  9961. dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
  9962. }
  9963. }
  9964. /**
  9965. * i40e_set_features - set the netdev feature flags
  9966. * @netdev: ptr to the netdev being adjusted
  9967. * @features: the feature set that the stack is suggesting
  9968. * Note: expects to be called while under rtnl_lock()
  9969. **/
  9970. static int i40e_set_features(struct net_device *netdev,
  9971. netdev_features_t features)
  9972. {
  9973. struct i40e_netdev_priv *np = netdev_priv(netdev);
  9974. struct i40e_vsi *vsi = np->vsi;
  9975. struct i40e_pf *pf = vsi->back;
  9976. bool need_reset;
  9977. if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
  9978. i40e_pf_config_rss(pf);
  9979. else if (!(features & NETIF_F_RXHASH) &&
  9980. netdev->features & NETIF_F_RXHASH)
  9981. i40e_clear_rss_lut(vsi);
  9982. if (features & NETIF_F_HW_VLAN_CTAG_RX)
  9983. i40e_vlan_stripping_enable(vsi);
  9984. else
  9985. i40e_vlan_stripping_disable(vsi);
  9986. if (!(features & NETIF_F_HW_TC) && pf->num_cloud_filters) {
  9987. dev_err(&pf->pdev->dev,
  9988. "Offloaded tc filters active, can't turn hw_tc_offload off");
  9989. return -EINVAL;
  9990. }
  9991. need_reset = i40e_set_ntuple(pf, features);
  9992. if (need_reset)
  9993. i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
  9994. return 0;
  9995. }
  9996. /**
  9997. * i40e_get_udp_port_idx - Lookup a possibly offloaded for Rx UDP port
  9998. * @pf: board private structure
  9999. * @port: The UDP port to look up
  10000. *
  10001. * Returns the index number or I40E_MAX_PF_UDP_OFFLOAD_PORTS if port not found
  10002. **/
  10003. static u8 i40e_get_udp_port_idx(struct i40e_pf *pf, u16 port)
  10004. {
  10005. u8 i;
  10006. for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
  10007. /* Do not report ports with pending deletions as
  10008. * being available.
  10009. */
  10010. if (!port && (pf->pending_udp_bitmap & BIT_ULL(i)))
  10011. continue;
  10012. if (pf->udp_ports[i].port == port)
  10013. return i;
  10014. }
  10015. return i;
  10016. }
  10017. /**
  10018. * i40e_udp_tunnel_add - Get notifications about UDP tunnel ports that come up
  10019. * @netdev: This physical port's netdev
  10020. * @ti: Tunnel endpoint information
  10021. **/
  10022. static void i40e_udp_tunnel_add(struct net_device *netdev,
  10023. struct udp_tunnel_info *ti)
  10024. {
  10025. struct i40e_netdev_priv *np = netdev_priv(netdev);
  10026. struct i40e_vsi *vsi = np->vsi;
  10027. struct i40e_pf *pf = vsi->back;
  10028. u16 port = ntohs(ti->port);
  10029. u8 next_idx;
  10030. u8 idx;
  10031. idx = i40e_get_udp_port_idx(pf, port);
  10032. /* Check if port already exists */
  10033. if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
  10034. netdev_info(netdev, "port %d already offloaded\n", port);
  10035. return;
  10036. }
  10037. /* Now check if there is space to add the new port */
  10038. next_idx = i40e_get_udp_port_idx(pf, 0);
  10039. if (next_idx == I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
  10040. netdev_info(netdev, "maximum number of offloaded UDP ports reached, not adding port %d\n",
  10041. port);
  10042. return;
  10043. }
  10044. switch (ti->type) {
  10045. case UDP_TUNNEL_TYPE_VXLAN:
  10046. pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_VXLAN;
  10047. break;
  10048. case UDP_TUNNEL_TYPE_GENEVE:
  10049. if (!(pf->hw_features & I40E_HW_GENEVE_OFFLOAD_CAPABLE))
  10050. return;
  10051. pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_NGE;
  10052. break;
  10053. default:
  10054. return;
  10055. }
  10056. /* New port: add it and mark its index in the bitmap */
  10057. pf->udp_ports[next_idx].port = port;
  10058. pf->udp_ports[next_idx].filter_index = I40E_UDP_PORT_INDEX_UNUSED;
  10059. pf->pending_udp_bitmap |= BIT_ULL(next_idx);
  10060. set_bit(__I40E_UDP_FILTER_SYNC_PENDING, pf->state);
  10061. }
  10062. /**
  10063. * i40e_udp_tunnel_del - Get notifications about UDP tunnel ports that go away
  10064. * @netdev: This physical port's netdev
  10065. * @ti: Tunnel endpoint information
  10066. **/
  10067. static void i40e_udp_tunnel_del(struct net_device *netdev,
  10068. struct udp_tunnel_info *ti)
  10069. {
  10070. struct i40e_netdev_priv *np = netdev_priv(netdev);
  10071. struct i40e_vsi *vsi = np->vsi;
  10072. struct i40e_pf *pf = vsi->back;
  10073. u16 port = ntohs(ti->port);
  10074. u8 idx;
  10075. idx = i40e_get_udp_port_idx(pf, port);
  10076. /* Check if port already exists */
  10077. if (idx >= I40E_MAX_PF_UDP_OFFLOAD_PORTS)
  10078. goto not_found;
  10079. switch (ti->type) {
  10080. case UDP_TUNNEL_TYPE_VXLAN:
  10081. if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_VXLAN)
  10082. goto not_found;
  10083. break;
  10084. case UDP_TUNNEL_TYPE_GENEVE:
  10085. if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_NGE)
  10086. goto not_found;
  10087. break;
  10088. default:
  10089. goto not_found;
  10090. }
  10091. /* if port exists, set it to 0 (mark for deletion)
  10092. * and make it pending
  10093. */
  10094. pf->udp_ports[idx].port = 0;
  10095. /* Toggle pending bit instead of setting it. This way if we are
  10096. * deleting a port that has yet to be added we just clear the pending
  10097. * bit and don't have to worry about it.
  10098. */
  10099. pf->pending_udp_bitmap ^= BIT_ULL(idx);
  10100. set_bit(__I40E_UDP_FILTER_SYNC_PENDING, pf->state);
  10101. return;
  10102. not_found:
  10103. netdev_warn(netdev, "UDP port %d was not found, not deleting\n",
  10104. port);
  10105. }
  10106. static int i40e_get_phys_port_id(struct net_device *netdev,
  10107. struct netdev_phys_item_id *ppid)
  10108. {
  10109. struct i40e_netdev_priv *np = netdev_priv(netdev);
  10110. struct i40e_pf *pf = np->vsi->back;
  10111. struct i40e_hw *hw = &pf->hw;
  10112. if (!(pf->hw_features & I40E_HW_PORT_ID_VALID))
  10113. return -EOPNOTSUPP;
  10114. ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
  10115. memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
  10116. return 0;
  10117. }
  10118. /**
  10119. * i40e_ndo_fdb_add - add an entry to the hardware database
  10120. * @ndm: the input from the stack
  10121. * @tb: pointer to array of nladdr (unused)
  10122. * @dev: the net device pointer
  10123. * @addr: the MAC address entry being added
  10124. * @vid: VLAN ID
  10125. * @flags: instructions from stack about fdb operation
  10126. */
  10127. static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
  10128. struct net_device *dev,
  10129. const unsigned char *addr, u16 vid,
  10130. u16 flags)
  10131. {
  10132. struct i40e_netdev_priv *np = netdev_priv(dev);
  10133. struct i40e_pf *pf = np->vsi->back;
  10134. int err = 0;
  10135. if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
  10136. return -EOPNOTSUPP;
  10137. if (vid) {
  10138. pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
  10139. return -EINVAL;
  10140. }
  10141. /* Hardware does not support aging addresses so if a
  10142. * ndm_state is given only allow permanent addresses
  10143. */
  10144. if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
  10145. netdev_info(dev, "FDB only supports static addresses\n");
  10146. return -EINVAL;
  10147. }
  10148. if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
  10149. err = dev_uc_add_excl(dev, addr);
  10150. else if (is_multicast_ether_addr(addr))
  10151. err = dev_mc_add_excl(dev, addr);
  10152. else
  10153. err = -EINVAL;
  10154. /* Only return duplicate errors if NLM_F_EXCL is set */
  10155. if (err == -EEXIST && !(flags & NLM_F_EXCL))
  10156. err = 0;
  10157. return err;
  10158. }
  10159. /**
  10160. * i40e_ndo_bridge_setlink - Set the hardware bridge mode
  10161. * @dev: the netdev being configured
  10162. * @nlh: RTNL message
  10163. * @flags: bridge flags
  10164. *
  10165. * Inserts a new hardware bridge if not already created and
  10166. * enables the bridging mode requested (VEB or VEPA). If the
  10167. * hardware bridge has already been inserted and the request
  10168. * is to change the mode then that requires a PF reset to
  10169. * allow rebuild of the components with required hardware
  10170. * bridge mode enabled.
  10171. *
  10172. * Note: expects to be called while under rtnl_lock()
  10173. **/
  10174. static int i40e_ndo_bridge_setlink(struct net_device *dev,
  10175. struct nlmsghdr *nlh,
  10176. u16 flags)
  10177. {
  10178. struct i40e_netdev_priv *np = netdev_priv(dev);
  10179. struct i40e_vsi *vsi = np->vsi;
  10180. struct i40e_pf *pf = vsi->back;
  10181. struct i40e_veb *veb = NULL;
  10182. struct nlattr *attr, *br_spec;
  10183. int i, rem;
  10184. /* Only for PF VSI for now */
  10185. if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
  10186. return -EOPNOTSUPP;
  10187. /* Find the HW bridge for PF VSI */
  10188. for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
  10189. if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
  10190. veb = pf->veb[i];
  10191. }
  10192. br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
  10193. nla_for_each_nested(attr, br_spec, rem) {
  10194. __u16 mode;
  10195. if (nla_type(attr) != IFLA_BRIDGE_MODE)
  10196. continue;
  10197. mode = nla_get_u16(attr);
  10198. if ((mode != BRIDGE_MODE_VEPA) &&
  10199. (mode != BRIDGE_MODE_VEB))
  10200. return -EINVAL;
  10201. /* Insert a new HW bridge */
  10202. if (!veb) {
  10203. veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
  10204. vsi->tc_config.enabled_tc);
  10205. if (veb) {
  10206. veb->bridge_mode = mode;
  10207. i40e_config_bridge_mode(veb);
  10208. } else {
  10209. /* No Bridge HW offload available */
  10210. return -ENOENT;
  10211. }
  10212. break;
  10213. } else if (mode != veb->bridge_mode) {
  10214. /* Existing HW bridge but different mode needs reset */
  10215. veb->bridge_mode = mode;
  10216. /* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
  10217. if (mode == BRIDGE_MODE_VEB)
  10218. pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
  10219. else
  10220. pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
  10221. i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
  10222. break;
  10223. }
  10224. }
  10225. return 0;
  10226. }
  10227. /**
  10228. * i40e_ndo_bridge_getlink - Get the hardware bridge mode
  10229. * @skb: skb buff
  10230. * @pid: process id
  10231. * @seq: RTNL message seq #
  10232. * @dev: the netdev being configured
  10233. * @filter_mask: unused
  10234. * @nlflags: netlink flags passed in
  10235. *
  10236. * Return the mode in which the hardware bridge is operating in
  10237. * i.e VEB or VEPA.
  10238. **/
  10239. static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
  10240. struct net_device *dev,
  10241. u32 __always_unused filter_mask,
  10242. int nlflags)
  10243. {
  10244. struct i40e_netdev_priv *np = netdev_priv(dev);
  10245. struct i40e_vsi *vsi = np->vsi;
  10246. struct i40e_pf *pf = vsi->back;
  10247. struct i40e_veb *veb = NULL;
  10248. int i;
  10249. /* Only for PF VSI for now */
  10250. if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
  10251. return -EOPNOTSUPP;
  10252. /* Find the HW bridge for the PF VSI */
  10253. for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
  10254. if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
  10255. veb = pf->veb[i];
  10256. }
  10257. if (!veb)
  10258. return 0;
  10259. return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
  10260. 0, 0, nlflags, filter_mask, NULL);
  10261. }
  10262. /**
  10263. * i40e_features_check - Validate encapsulated packet conforms to limits
  10264. * @skb: skb buff
  10265. * @dev: This physical port's netdev
  10266. * @features: Offload features that the stack believes apply
  10267. **/
  10268. static netdev_features_t i40e_features_check(struct sk_buff *skb,
  10269. struct net_device *dev,
  10270. netdev_features_t features)
  10271. {
  10272. size_t len;
  10273. /* No point in doing any of this if neither checksum nor GSO are
  10274. * being requested for this frame. We can rule out both by just
  10275. * checking for CHECKSUM_PARTIAL
  10276. */
  10277. if (skb->ip_summed != CHECKSUM_PARTIAL)
  10278. return features;
  10279. /* We cannot support GSO if the MSS is going to be less than
  10280. * 64 bytes. If it is then we need to drop support for GSO.
  10281. */
  10282. if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
  10283. features &= ~NETIF_F_GSO_MASK;
  10284. /* MACLEN can support at most 63 words */
  10285. len = skb_network_header(skb) - skb->data;
  10286. if (len & ~(63 * 2))
  10287. goto out_err;
  10288. /* IPLEN and EIPLEN can support at most 127 dwords */
  10289. len = skb_transport_header(skb) - skb_network_header(skb);
  10290. if (len & ~(127 * 4))
  10291. goto out_err;
  10292. if (skb->encapsulation) {
  10293. /* L4TUNLEN can support 127 words */
  10294. len = skb_inner_network_header(skb) - skb_transport_header(skb);
  10295. if (len & ~(127 * 2))
  10296. goto out_err;
  10297. /* IPLEN can support at most 127 dwords */
  10298. len = skb_inner_transport_header(skb) -
  10299. skb_inner_network_header(skb);
  10300. if (len & ~(127 * 4))
  10301. goto out_err;
  10302. }
  10303. /* No need to validate L4LEN as TCP is the only protocol with a
  10304. * a flexible value and we support all possible values supported
  10305. * by TCP, which is at most 15 dwords
  10306. */
  10307. return features;
  10308. out_err:
  10309. return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
  10310. }
  10311. /**
  10312. * i40e_xdp_setup - add/remove an XDP program
  10313. * @vsi: VSI to changed
  10314. * @prog: XDP program
  10315. **/
  10316. static int i40e_xdp_setup(struct i40e_vsi *vsi,
  10317. struct bpf_prog *prog)
  10318. {
  10319. int frame_size = vsi->netdev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
  10320. struct i40e_pf *pf = vsi->back;
  10321. struct bpf_prog *old_prog;
  10322. bool need_reset;
  10323. int i;
  10324. /* Don't allow frames that span over multiple buffers */
  10325. if (frame_size > vsi->rx_buf_len)
  10326. return -EINVAL;
  10327. if (!i40e_enabled_xdp_vsi(vsi) && !prog)
  10328. return 0;
  10329. /* When turning XDP on->off/off->on we reset and rebuild the rings. */
  10330. need_reset = (i40e_enabled_xdp_vsi(vsi) != !!prog);
  10331. if (need_reset)
  10332. i40e_prep_for_reset(pf, true);
  10333. old_prog = xchg(&vsi->xdp_prog, prog);
  10334. if (need_reset)
  10335. i40e_reset_and_rebuild(pf, true, true);
  10336. for (i = 0; i < vsi->num_queue_pairs; i++)
  10337. WRITE_ONCE(vsi->rx_rings[i]->xdp_prog, vsi->xdp_prog);
  10338. if (old_prog)
  10339. bpf_prog_put(old_prog);
  10340. return 0;
  10341. }
  10342. /**
  10343. * i40e_xdp - implements ndo_bpf for i40e
  10344. * @dev: netdevice
  10345. * @xdp: XDP command
  10346. **/
  10347. static int i40e_xdp(struct net_device *dev,
  10348. struct netdev_bpf *xdp)
  10349. {
  10350. struct i40e_netdev_priv *np = netdev_priv(dev);
  10351. struct i40e_vsi *vsi = np->vsi;
  10352. if (vsi->type != I40E_VSI_MAIN)
  10353. return -EINVAL;
  10354. switch (xdp->command) {
  10355. case XDP_SETUP_PROG:
  10356. return i40e_xdp_setup(vsi, xdp->prog);
  10357. case XDP_QUERY_PROG:
  10358. xdp->prog_id = vsi->xdp_prog ? vsi->xdp_prog->aux->id : 0;
  10359. return 0;
  10360. default:
  10361. return -EINVAL;
  10362. }
  10363. }
  10364. static const struct net_device_ops i40e_netdev_ops = {
  10365. .ndo_open = i40e_open,
  10366. .ndo_stop = i40e_close,
  10367. .ndo_start_xmit = i40e_lan_xmit_frame,
  10368. .ndo_get_stats64 = i40e_get_netdev_stats_struct,
  10369. .ndo_set_rx_mode = i40e_set_rx_mode,
  10370. .ndo_validate_addr = eth_validate_addr,
  10371. .ndo_set_mac_address = i40e_set_mac,
  10372. .ndo_change_mtu = i40e_change_mtu,
  10373. .ndo_do_ioctl = i40e_ioctl,
  10374. .ndo_tx_timeout = i40e_tx_timeout,
  10375. .ndo_vlan_rx_add_vid = i40e_vlan_rx_add_vid,
  10376. .ndo_vlan_rx_kill_vid = i40e_vlan_rx_kill_vid,
  10377. #ifdef CONFIG_NET_POLL_CONTROLLER
  10378. .ndo_poll_controller = i40e_netpoll,
  10379. #endif
  10380. .ndo_setup_tc = __i40e_setup_tc,
  10381. .ndo_set_features = i40e_set_features,
  10382. .ndo_set_vf_mac = i40e_ndo_set_vf_mac,
  10383. .ndo_set_vf_vlan = i40e_ndo_set_vf_port_vlan,
  10384. .ndo_set_vf_rate = i40e_ndo_set_vf_bw,
  10385. .ndo_get_vf_config = i40e_ndo_get_vf_config,
  10386. .ndo_set_vf_link_state = i40e_ndo_set_vf_link_state,
  10387. .ndo_set_vf_spoofchk = i40e_ndo_set_vf_spoofchk,
  10388. .ndo_set_vf_trust = i40e_ndo_set_vf_trust,
  10389. .ndo_udp_tunnel_add = i40e_udp_tunnel_add,
  10390. .ndo_udp_tunnel_del = i40e_udp_tunnel_del,
  10391. .ndo_get_phys_port_id = i40e_get_phys_port_id,
  10392. .ndo_fdb_add = i40e_ndo_fdb_add,
  10393. .ndo_features_check = i40e_features_check,
  10394. .ndo_bridge_getlink = i40e_ndo_bridge_getlink,
  10395. .ndo_bridge_setlink = i40e_ndo_bridge_setlink,
  10396. .ndo_bpf = i40e_xdp,
  10397. .ndo_xdp_xmit = i40e_xdp_xmit,
  10398. };
  10399. /**
  10400. * i40e_config_netdev - Setup the netdev flags
  10401. * @vsi: the VSI being configured
  10402. *
  10403. * Returns 0 on success, negative value on failure
  10404. **/
  10405. static int i40e_config_netdev(struct i40e_vsi *vsi)
  10406. {
  10407. struct i40e_pf *pf = vsi->back;
  10408. struct i40e_hw *hw = &pf->hw;
  10409. struct i40e_netdev_priv *np;
  10410. struct net_device *netdev;
  10411. u8 broadcast[ETH_ALEN];
  10412. u8 mac_addr[ETH_ALEN];
  10413. int etherdev_size;
  10414. netdev_features_t hw_enc_features;
  10415. netdev_features_t hw_features;
  10416. etherdev_size = sizeof(struct i40e_netdev_priv);
  10417. netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
  10418. if (!netdev)
  10419. return -ENOMEM;
  10420. vsi->netdev = netdev;
  10421. np = netdev_priv(netdev);
  10422. np->vsi = vsi;
  10423. hw_enc_features = NETIF_F_SG |
  10424. NETIF_F_IP_CSUM |
  10425. NETIF_F_IPV6_CSUM |
  10426. NETIF_F_HIGHDMA |
  10427. NETIF_F_SOFT_FEATURES |
  10428. NETIF_F_TSO |
  10429. NETIF_F_TSO_ECN |
  10430. NETIF_F_TSO6 |
  10431. NETIF_F_GSO_GRE |
  10432. NETIF_F_GSO_GRE_CSUM |
  10433. NETIF_F_GSO_PARTIAL |
  10434. NETIF_F_GSO_UDP_TUNNEL |
  10435. NETIF_F_GSO_UDP_TUNNEL_CSUM |
  10436. NETIF_F_SCTP_CRC |
  10437. NETIF_F_RXHASH |
  10438. NETIF_F_RXCSUM |
  10439. 0;
  10440. if (!(pf->hw_features & I40E_HW_OUTER_UDP_CSUM_CAPABLE))
  10441. netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
  10442. netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
  10443. netdev->hw_enc_features |= hw_enc_features;
  10444. /* record features VLANs can make use of */
  10445. netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
  10446. if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
  10447. netdev->hw_features |= NETIF_F_NTUPLE | NETIF_F_HW_TC;
  10448. hw_features = hw_enc_features |
  10449. NETIF_F_HW_VLAN_CTAG_TX |
  10450. NETIF_F_HW_VLAN_CTAG_RX;
  10451. netdev->hw_features |= hw_features;
  10452. netdev->features |= hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
  10453. netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
  10454. if (vsi->type == I40E_VSI_MAIN) {
  10455. SET_NETDEV_DEV(netdev, &pf->pdev->dev);
  10456. ether_addr_copy(mac_addr, hw->mac.perm_addr);
  10457. /* The following steps are necessary for two reasons. First,
  10458. * some older NVM configurations load a default MAC-VLAN
  10459. * filter that will accept any tagged packet, and we want to
  10460. * replace this with a normal filter. Additionally, it is
  10461. * possible our MAC address was provided by the platform using
  10462. * Open Firmware or similar.
  10463. *
  10464. * Thus, we need to remove the default filter and install one
  10465. * specific to the MAC address.
  10466. */
  10467. i40e_rm_default_mac_filter(vsi, mac_addr);
  10468. spin_lock_bh(&vsi->mac_filter_hash_lock);
  10469. i40e_add_mac_filter(vsi, mac_addr);
  10470. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  10471. } else {
  10472. /* Relate the VSI_VMDQ name to the VSI_MAIN name. Note that we
  10473. * are still limited by IFNAMSIZ, but we're adding 'v%d\0' to
  10474. * the end, which is 4 bytes long, so force truncation of the
  10475. * original name by IFNAMSIZ - 4
  10476. */
  10477. snprintf(netdev->name, IFNAMSIZ, "%.*sv%%d",
  10478. IFNAMSIZ - 4,
  10479. pf->vsi[pf->lan_vsi]->netdev->name);
  10480. eth_random_addr(mac_addr);
  10481. spin_lock_bh(&vsi->mac_filter_hash_lock);
  10482. i40e_add_mac_filter(vsi, mac_addr);
  10483. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  10484. }
  10485. /* Add the broadcast filter so that we initially will receive
  10486. * broadcast packets. Note that when a new VLAN is first added the
  10487. * driver will convert all filters marked I40E_VLAN_ANY into VLAN
  10488. * specific filters as part of transitioning into "vlan" operation.
  10489. * When more VLANs are added, the driver will copy each existing MAC
  10490. * filter and add it for the new VLAN.
  10491. *
  10492. * Broadcast filters are handled specially by
  10493. * i40e_sync_filters_subtask, as the driver must to set the broadcast
  10494. * promiscuous bit instead of adding this directly as a MAC/VLAN
  10495. * filter. The subtask will update the correct broadcast promiscuous
  10496. * bits as VLANs become active or inactive.
  10497. */
  10498. eth_broadcast_addr(broadcast);
  10499. spin_lock_bh(&vsi->mac_filter_hash_lock);
  10500. i40e_add_mac_filter(vsi, broadcast);
  10501. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  10502. ether_addr_copy(netdev->dev_addr, mac_addr);
  10503. ether_addr_copy(netdev->perm_addr, mac_addr);
  10504. netdev->priv_flags |= IFF_UNICAST_FLT;
  10505. netdev->priv_flags |= IFF_SUPP_NOFCS;
  10506. /* Setup netdev TC information */
  10507. i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
  10508. netdev->netdev_ops = &i40e_netdev_ops;
  10509. netdev->watchdog_timeo = 5 * HZ;
  10510. i40e_set_ethtool_ops(netdev);
  10511. /* MTU range: 68 - 9706 */
  10512. netdev->min_mtu = ETH_MIN_MTU;
  10513. netdev->max_mtu = I40E_MAX_RXBUFFER - I40E_PACKET_HDR_PAD;
  10514. return 0;
  10515. }
  10516. /**
  10517. * i40e_vsi_delete - Delete a VSI from the switch
  10518. * @vsi: the VSI being removed
  10519. *
  10520. * Returns 0 on success, negative value on failure
  10521. **/
  10522. static void i40e_vsi_delete(struct i40e_vsi *vsi)
  10523. {
  10524. /* remove default VSI is not allowed */
  10525. if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
  10526. return;
  10527. i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
  10528. }
  10529. /**
  10530. * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
  10531. * @vsi: the VSI being queried
  10532. *
  10533. * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
  10534. **/
  10535. int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
  10536. {
  10537. struct i40e_veb *veb;
  10538. struct i40e_pf *pf = vsi->back;
  10539. /* Uplink is not a bridge so default to VEB */
  10540. if (vsi->veb_idx == I40E_NO_VEB)
  10541. return 1;
  10542. veb = pf->veb[vsi->veb_idx];
  10543. if (!veb) {
  10544. dev_info(&pf->pdev->dev,
  10545. "There is no veb associated with the bridge\n");
  10546. return -ENOENT;
  10547. }
  10548. /* Uplink is a bridge in VEPA mode */
  10549. if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
  10550. return 0;
  10551. } else {
  10552. /* Uplink is a bridge in VEB mode */
  10553. return 1;
  10554. }
  10555. /* VEPA is now default bridge, so return 0 */
  10556. return 0;
  10557. }
  10558. /**
  10559. * i40e_add_vsi - Add a VSI to the switch
  10560. * @vsi: the VSI being configured
  10561. *
  10562. * This initializes a VSI context depending on the VSI type to be added and
  10563. * passes it down to the add_vsi aq command.
  10564. **/
  10565. static int i40e_add_vsi(struct i40e_vsi *vsi)
  10566. {
  10567. int ret = -ENODEV;
  10568. struct i40e_pf *pf = vsi->back;
  10569. struct i40e_hw *hw = &pf->hw;
  10570. struct i40e_vsi_context ctxt;
  10571. struct i40e_mac_filter *f;
  10572. struct hlist_node *h;
  10573. int bkt;
  10574. u8 enabled_tc = 0x1; /* TC0 enabled */
  10575. int f_count = 0;
  10576. memset(&ctxt, 0, sizeof(ctxt));
  10577. switch (vsi->type) {
  10578. case I40E_VSI_MAIN:
  10579. /* The PF's main VSI is already setup as part of the
  10580. * device initialization, so we'll not bother with
  10581. * the add_vsi call, but we will retrieve the current
  10582. * VSI context.
  10583. */
  10584. ctxt.seid = pf->main_vsi_seid;
  10585. ctxt.pf_num = pf->hw.pf_id;
  10586. ctxt.vf_num = 0;
  10587. ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
  10588. ctxt.flags = I40E_AQ_VSI_TYPE_PF;
  10589. if (ret) {
  10590. dev_info(&pf->pdev->dev,
  10591. "couldn't get PF vsi config, err %s aq_err %s\n",
  10592. i40e_stat_str(&pf->hw, ret),
  10593. i40e_aq_str(&pf->hw,
  10594. pf->hw.aq.asq_last_status));
  10595. return -ENOENT;
  10596. }
  10597. vsi->info = ctxt.info;
  10598. vsi->info.valid_sections = 0;
  10599. vsi->seid = ctxt.seid;
  10600. vsi->id = ctxt.vsi_number;
  10601. enabled_tc = i40e_pf_get_tc_map(pf);
  10602. /* Source pruning is enabled by default, so the flag is
  10603. * negative logic - if it's set, we need to fiddle with
  10604. * the VSI to disable source pruning.
  10605. */
  10606. if (pf->flags & I40E_FLAG_SOURCE_PRUNING_DISABLED) {
  10607. memset(&ctxt, 0, sizeof(ctxt));
  10608. ctxt.seid = pf->main_vsi_seid;
  10609. ctxt.pf_num = pf->hw.pf_id;
  10610. ctxt.vf_num = 0;
  10611. ctxt.info.valid_sections |=
  10612. cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  10613. ctxt.info.switch_id =
  10614. cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_LOCAL_LB);
  10615. ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
  10616. if (ret) {
  10617. dev_info(&pf->pdev->dev,
  10618. "update vsi failed, err %s aq_err %s\n",
  10619. i40e_stat_str(&pf->hw, ret),
  10620. i40e_aq_str(&pf->hw,
  10621. pf->hw.aq.asq_last_status));
  10622. ret = -ENOENT;
  10623. goto err;
  10624. }
  10625. }
  10626. /* MFP mode setup queue map and update VSI */
  10627. if ((pf->flags & I40E_FLAG_MFP_ENABLED) &&
  10628. !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
  10629. memset(&ctxt, 0, sizeof(ctxt));
  10630. ctxt.seid = pf->main_vsi_seid;
  10631. ctxt.pf_num = pf->hw.pf_id;
  10632. ctxt.vf_num = 0;
  10633. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
  10634. ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
  10635. if (ret) {
  10636. dev_info(&pf->pdev->dev,
  10637. "update vsi failed, err %s aq_err %s\n",
  10638. i40e_stat_str(&pf->hw, ret),
  10639. i40e_aq_str(&pf->hw,
  10640. pf->hw.aq.asq_last_status));
  10641. ret = -ENOENT;
  10642. goto err;
  10643. }
  10644. /* update the local VSI info queue map */
  10645. i40e_vsi_update_queue_map(vsi, &ctxt);
  10646. vsi->info.valid_sections = 0;
  10647. } else {
  10648. /* Default/Main VSI is only enabled for TC0
  10649. * reconfigure it to enable all TCs that are
  10650. * available on the port in SFP mode.
  10651. * For MFP case the iSCSI PF would use this
  10652. * flow to enable LAN+iSCSI TC.
  10653. */
  10654. ret = i40e_vsi_config_tc(vsi, enabled_tc);
  10655. if (ret) {
  10656. /* Single TC condition is not fatal,
  10657. * message and continue
  10658. */
  10659. dev_info(&pf->pdev->dev,
  10660. "failed to configure TCs for main VSI tc_map 0x%08x, err %s aq_err %s\n",
  10661. enabled_tc,
  10662. i40e_stat_str(&pf->hw, ret),
  10663. i40e_aq_str(&pf->hw,
  10664. pf->hw.aq.asq_last_status));
  10665. }
  10666. }
  10667. break;
  10668. case I40E_VSI_FDIR:
  10669. ctxt.pf_num = hw->pf_id;
  10670. ctxt.vf_num = 0;
  10671. ctxt.uplink_seid = vsi->uplink_seid;
  10672. ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
  10673. ctxt.flags = I40E_AQ_VSI_TYPE_PF;
  10674. if ((pf->flags & I40E_FLAG_VEB_MODE_ENABLED) &&
  10675. (i40e_is_vsi_uplink_mode_veb(vsi))) {
  10676. ctxt.info.valid_sections |=
  10677. cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  10678. ctxt.info.switch_id =
  10679. cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  10680. }
  10681. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
  10682. break;
  10683. case I40E_VSI_VMDQ2:
  10684. ctxt.pf_num = hw->pf_id;
  10685. ctxt.vf_num = 0;
  10686. ctxt.uplink_seid = vsi->uplink_seid;
  10687. ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
  10688. ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
  10689. /* This VSI is connected to VEB so the switch_id
  10690. * should be set to zero by default.
  10691. */
  10692. if (i40e_is_vsi_uplink_mode_veb(vsi)) {
  10693. ctxt.info.valid_sections |=
  10694. cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  10695. ctxt.info.switch_id =
  10696. cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  10697. }
  10698. /* Setup the VSI tx/rx queue map for TC0 only for now */
  10699. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
  10700. break;
  10701. case I40E_VSI_SRIOV:
  10702. ctxt.pf_num = hw->pf_id;
  10703. ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
  10704. ctxt.uplink_seid = vsi->uplink_seid;
  10705. ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
  10706. ctxt.flags = I40E_AQ_VSI_TYPE_VF;
  10707. /* This VSI is connected to VEB so the switch_id
  10708. * should be set to zero by default.
  10709. */
  10710. if (i40e_is_vsi_uplink_mode_veb(vsi)) {
  10711. ctxt.info.valid_sections |=
  10712. cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  10713. ctxt.info.switch_id =
  10714. cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  10715. }
  10716. if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
  10717. ctxt.info.valid_sections |=
  10718. cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
  10719. ctxt.info.queueing_opt_flags |=
  10720. (I40E_AQ_VSI_QUE_OPT_TCP_ENA |
  10721. I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI);
  10722. }
  10723. ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
  10724. ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
  10725. if (pf->vf[vsi->vf_id].spoofchk) {
  10726. ctxt.info.valid_sections |=
  10727. cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
  10728. ctxt.info.sec_flags |=
  10729. (I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
  10730. I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
  10731. }
  10732. /* Setup the VSI tx/rx queue map for TC0 only for now */
  10733. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
  10734. break;
  10735. case I40E_VSI_IWARP:
  10736. /* send down message to iWARP */
  10737. break;
  10738. default:
  10739. return -ENODEV;
  10740. }
  10741. if (vsi->type != I40E_VSI_MAIN) {
  10742. ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
  10743. if (ret) {
  10744. dev_info(&vsi->back->pdev->dev,
  10745. "add vsi failed, err %s aq_err %s\n",
  10746. i40e_stat_str(&pf->hw, ret),
  10747. i40e_aq_str(&pf->hw,
  10748. pf->hw.aq.asq_last_status));
  10749. ret = -ENOENT;
  10750. goto err;
  10751. }
  10752. vsi->info = ctxt.info;
  10753. vsi->info.valid_sections = 0;
  10754. vsi->seid = ctxt.seid;
  10755. vsi->id = ctxt.vsi_number;
  10756. }
  10757. vsi->active_filters = 0;
  10758. clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
  10759. spin_lock_bh(&vsi->mac_filter_hash_lock);
  10760. /* If macvlan filters already exist, force them to get loaded */
  10761. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  10762. f->state = I40E_FILTER_NEW;
  10763. f_count++;
  10764. }
  10765. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  10766. if (f_count) {
  10767. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  10768. set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
  10769. }
  10770. /* Update VSI BW information */
  10771. ret = i40e_vsi_get_bw_info(vsi);
  10772. if (ret) {
  10773. dev_info(&pf->pdev->dev,
  10774. "couldn't get vsi bw info, err %s aq_err %s\n",
  10775. i40e_stat_str(&pf->hw, ret),
  10776. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  10777. /* VSI is already added so not tearing that up */
  10778. ret = 0;
  10779. }
  10780. err:
  10781. return ret;
  10782. }
  10783. /**
  10784. * i40e_vsi_release - Delete a VSI and free its resources
  10785. * @vsi: the VSI being removed
  10786. *
  10787. * Returns 0 on success or < 0 on error
  10788. **/
  10789. int i40e_vsi_release(struct i40e_vsi *vsi)
  10790. {
  10791. struct i40e_mac_filter *f;
  10792. struct hlist_node *h;
  10793. struct i40e_veb *veb = NULL;
  10794. struct i40e_pf *pf;
  10795. u16 uplink_seid;
  10796. int i, n, bkt;
  10797. pf = vsi->back;
  10798. /* release of a VEB-owner or last VSI is not allowed */
  10799. if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
  10800. dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
  10801. vsi->seid, vsi->uplink_seid);
  10802. return -ENODEV;
  10803. }
  10804. if (vsi == pf->vsi[pf->lan_vsi] &&
  10805. !test_bit(__I40E_DOWN, pf->state)) {
  10806. dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
  10807. return -ENODEV;
  10808. }
  10809. uplink_seid = vsi->uplink_seid;
  10810. if (vsi->type != I40E_VSI_SRIOV) {
  10811. if (vsi->netdev_registered) {
  10812. vsi->netdev_registered = false;
  10813. if (vsi->netdev) {
  10814. /* results in a call to i40e_close() */
  10815. unregister_netdev(vsi->netdev);
  10816. }
  10817. } else {
  10818. i40e_vsi_close(vsi);
  10819. }
  10820. i40e_vsi_disable_irq(vsi);
  10821. }
  10822. spin_lock_bh(&vsi->mac_filter_hash_lock);
  10823. /* clear the sync flag on all filters */
  10824. if (vsi->netdev) {
  10825. __dev_uc_unsync(vsi->netdev, NULL);
  10826. __dev_mc_unsync(vsi->netdev, NULL);
  10827. }
  10828. /* make sure any remaining filters are marked for deletion */
  10829. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
  10830. __i40e_del_filter(vsi, f);
  10831. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  10832. i40e_sync_vsi_filters(vsi);
  10833. i40e_vsi_delete(vsi);
  10834. i40e_vsi_free_q_vectors(vsi);
  10835. if (vsi->netdev) {
  10836. free_netdev(vsi->netdev);
  10837. vsi->netdev = NULL;
  10838. }
  10839. i40e_vsi_clear_rings(vsi);
  10840. i40e_vsi_clear(vsi);
  10841. /* If this was the last thing on the VEB, except for the
  10842. * controlling VSI, remove the VEB, which puts the controlling
  10843. * VSI onto the next level down in the switch.
  10844. *
  10845. * Well, okay, there's one more exception here: don't remove
  10846. * the orphan VEBs yet. We'll wait for an explicit remove request
  10847. * from up the network stack.
  10848. */
  10849. for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
  10850. if (pf->vsi[i] &&
  10851. pf->vsi[i]->uplink_seid == uplink_seid &&
  10852. (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
  10853. n++; /* count the VSIs */
  10854. }
  10855. }
  10856. for (i = 0; i < I40E_MAX_VEB; i++) {
  10857. if (!pf->veb[i])
  10858. continue;
  10859. if (pf->veb[i]->uplink_seid == uplink_seid)
  10860. n++; /* count the VEBs */
  10861. if (pf->veb[i]->seid == uplink_seid)
  10862. veb = pf->veb[i];
  10863. }
  10864. if (n == 0 && veb && veb->uplink_seid != 0)
  10865. i40e_veb_release(veb);
  10866. return 0;
  10867. }
  10868. /**
  10869. * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
  10870. * @vsi: ptr to the VSI
  10871. *
  10872. * This should only be called after i40e_vsi_mem_alloc() which allocates the
  10873. * corresponding SW VSI structure and initializes num_queue_pairs for the
  10874. * newly allocated VSI.
  10875. *
  10876. * Returns 0 on success or negative on failure
  10877. **/
  10878. static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
  10879. {
  10880. int ret = -ENOENT;
  10881. struct i40e_pf *pf = vsi->back;
  10882. if (vsi->q_vectors[0]) {
  10883. dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
  10884. vsi->seid);
  10885. return -EEXIST;
  10886. }
  10887. if (vsi->base_vector) {
  10888. dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
  10889. vsi->seid, vsi->base_vector);
  10890. return -EEXIST;
  10891. }
  10892. ret = i40e_vsi_alloc_q_vectors(vsi);
  10893. if (ret) {
  10894. dev_info(&pf->pdev->dev,
  10895. "failed to allocate %d q_vector for VSI %d, ret=%d\n",
  10896. vsi->num_q_vectors, vsi->seid, ret);
  10897. vsi->num_q_vectors = 0;
  10898. goto vector_setup_out;
  10899. }
  10900. /* In Legacy mode, we do not have to get any other vector since we
  10901. * piggyback on the misc/ICR0 for queue interrupts.
  10902. */
  10903. if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
  10904. return ret;
  10905. if (vsi->num_q_vectors)
  10906. vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
  10907. vsi->num_q_vectors, vsi->idx);
  10908. if (vsi->base_vector < 0) {
  10909. dev_info(&pf->pdev->dev,
  10910. "failed to get tracking for %d vectors for VSI %d, err=%d\n",
  10911. vsi->num_q_vectors, vsi->seid, vsi->base_vector);
  10912. i40e_vsi_free_q_vectors(vsi);
  10913. ret = -ENOENT;
  10914. goto vector_setup_out;
  10915. }
  10916. vector_setup_out:
  10917. return ret;
  10918. }
  10919. /**
  10920. * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
  10921. * @vsi: pointer to the vsi.
  10922. *
  10923. * This re-allocates a vsi's queue resources.
  10924. *
  10925. * Returns pointer to the successfully allocated and configured VSI sw struct
  10926. * on success, otherwise returns NULL on failure.
  10927. **/
  10928. static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
  10929. {
  10930. u16 alloc_queue_pairs;
  10931. struct i40e_pf *pf;
  10932. u8 enabled_tc;
  10933. int ret;
  10934. if (!vsi)
  10935. return NULL;
  10936. pf = vsi->back;
  10937. i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
  10938. i40e_vsi_clear_rings(vsi);
  10939. i40e_vsi_free_arrays(vsi, false);
  10940. i40e_set_num_rings_in_vsi(vsi);
  10941. ret = i40e_vsi_alloc_arrays(vsi, false);
  10942. if (ret)
  10943. goto err_vsi;
  10944. alloc_queue_pairs = vsi->alloc_queue_pairs *
  10945. (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
  10946. ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
  10947. if (ret < 0) {
  10948. dev_info(&pf->pdev->dev,
  10949. "failed to get tracking for %d queues for VSI %d err %d\n",
  10950. alloc_queue_pairs, vsi->seid, ret);
  10951. goto err_vsi;
  10952. }
  10953. vsi->base_queue = ret;
  10954. /* Update the FW view of the VSI. Force a reset of TC and queue
  10955. * layout configurations.
  10956. */
  10957. enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
  10958. pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
  10959. pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
  10960. i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
  10961. if (vsi->type == I40E_VSI_MAIN)
  10962. i40e_rm_default_mac_filter(vsi, pf->hw.mac.perm_addr);
  10963. /* assign it some queues */
  10964. ret = i40e_alloc_rings(vsi);
  10965. if (ret)
  10966. goto err_rings;
  10967. /* map all of the rings to the q_vectors */
  10968. i40e_vsi_map_rings_to_vectors(vsi);
  10969. return vsi;
  10970. err_rings:
  10971. i40e_vsi_free_q_vectors(vsi);
  10972. if (vsi->netdev_registered) {
  10973. vsi->netdev_registered = false;
  10974. unregister_netdev(vsi->netdev);
  10975. free_netdev(vsi->netdev);
  10976. vsi->netdev = NULL;
  10977. }
  10978. i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
  10979. err_vsi:
  10980. i40e_vsi_clear(vsi);
  10981. return NULL;
  10982. }
  10983. /**
  10984. * i40e_vsi_setup - Set up a VSI by a given type
  10985. * @pf: board private structure
  10986. * @type: VSI type
  10987. * @uplink_seid: the switch element to link to
  10988. * @param1: usage depends upon VSI type. For VF types, indicates VF id
  10989. *
  10990. * This allocates the sw VSI structure and its queue resources, then add a VSI
  10991. * to the identified VEB.
  10992. *
  10993. * Returns pointer to the successfully allocated and configure VSI sw struct on
  10994. * success, otherwise returns NULL on failure.
  10995. **/
  10996. struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
  10997. u16 uplink_seid, u32 param1)
  10998. {
  10999. struct i40e_vsi *vsi = NULL;
  11000. struct i40e_veb *veb = NULL;
  11001. u16 alloc_queue_pairs;
  11002. int ret, i;
  11003. int v_idx;
  11004. /* The requested uplink_seid must be either
  11005. * - the PF's port seid
  11006. * no VEB is needed because this is the PF
  11007. * or this is a Flow Director special case VSI
  11008. * - seid of an existing VEB
  11009. * - seid of a VSI that owns an existing VEB
  11010. * - seid of a VSI that doesn't own a VEB
  11011. * a new VEB is created and the VSI becomes the owner
  11012. * - seid of the PF VSI, which is what creates the first VEB
  11013. * this is a special case of the previous
  11014. *
  11015. * Find which uplink_seid we were given and create a new VEB if needed
  11016. */
  11017. for (i = 0; i < I40E_MAX_VEB; i++) {
  11018. if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
  11019. veb = pf->veb[i];
  11020. break;
  11021. }
  11022. }
  11023. if (!veb && uplink_seid != pf->mac_seid) {
  11024. for (i = 0; i < pf->num_alloc_vsi; i++) {
  11025. if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
  11026. vsi = pf->vsi[i];
  11027. break;
  11028. }
  11029. }
  11030. if (!vsi) {
  11031. dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
  11032. uplink_seid);
  11033. return NULL;
  11034. }
  11035. if (vsi->uplink_seid == pf->mac_seid)
  11036. veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
  11037. vsi->tc_config.enabled_tc);
  11038. else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
  11039. veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
  11040. vsi->tc_config.enabled_tc);
  11041. if (veb) {
  11042. if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
  11043. dev_info(&vsi->back->pdev->dev,
  11044. "New VSI creation error, uplink seid of LAN VSI expected.\n");
  11045. return NULL;
  11046. }
  11047. /* We come up by default in VEPA mode if SRIOV is not
  11048. * already enabled, in which case we can't force VEPA
  11049. * mode.
  11050. */
  11051. if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
  11052. veb->bridge_mode = BRIDGE_MODE_VEPA;
  11053. pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
  11054. }
  11055. i40e_config_bridge_mode(veb);
  11056. }
  11057. for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
  11058. if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
  11059. veb = pf->veb[i];
  11060. }
  11061. if (!veb) {
  11062. dev_info(&pf->pdev->dev, "couldn't add VEB\n");
  11063. return NULL;
  11064. }
  11065. vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
  11066. uplink_seid = veb->seid;
  11067. }
  11068. /* get vsi sw struct */
  11069. v_idx = i40e_vsi_mem_alloc(pf, type);
  11070. if (v_idx < 0)
  11071. goto err_alloc;
  11072. vsi = pf->vsi[v_idx];
  11073. if (!vsi)
  11074. goto err_alloc;
  11075. vsi->type = type;
  11076. vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
  11077. if (type == I40E_VSI_MAIN)
  11078. pf->lan_vsi = v_idx;
  11079. else if (type == I40E_VSI_SRIOV)
  11080. vsi->vf_id = param1;
  11081. /* assign it some queues */
  11082. alloc_queue_pairs = vsi->alloc_queue_pairs *
  11083. (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
  11084. ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
  11085. if (ret < 0) {
  11086. dev_info(&pf->pdev->dev,
  11087. "failed to get tracking for %d queues for VSI %d err=%d\n",
  11088. alloc_queue_pairs, vsi->seid, ret);
  11089. goto err_vsi;
  11090. }
  11091. vsi->base_queue = ret;
  11092. /* get a VSI from the hardware */
  11093. vsi->uplink_seid = uplink_seid;
  11094. ret = i40e_add_vsi(vsi);
  11095. if (ret)
  11096. goto err_vsi;
  11097. switch (vsi->type) {
  11098. /* setup the netdev if needed */
  11099. case I40E_VSI_MAIN:
  11100. case I40E_VSI_VMDQ2:
  11101. ret = i40e_config_netdev(vsi);
  11102. if (ret)
  11103. goto err_netdev;
  11104. ret = register_netdev(vsi->netdev);
  11105. if (ret)
  11106. goto err_netdev;
  11107. vsi->netdev_registered = true;
  11108. netif_carrier_off(vsi->netdev);
  11109. #ifdef CONFIG_I40E_DCB
  11110. /* Setup DCB netlink interface */
  11111. i40e_dcbnl_setup(vsi);
  11112. #endif /* CONFIG_I40E_DCB */
  11113. /* fall through */
  11114. case I40E_VSI_FDIR:
  11115. /* set up vectors and rings if needed */
  11116. ret = i40e_vsi_setup_vectors(vsi);
  11117. if (ret)
  11118. goto err_msix;
  11119. ret = i40e_alloc_rings(vsi);
  11120. if (ret)
  11121. goto err_rings;
  11122. /* map all of the rings to the q_vectors */
  11123. i40e_vsi_map_rings_to_vectors(vsi);
  11124. i40e_vsi_reset_stats(vsi);
  11125. break;
  11126. default:
  11127. /* no netdev or rings for the other VSI types */
  11128. break;
  11129. }
  11130. if ((pf->hw_features & I40E_HW_RSS_AQ_CAPABLE) &&
  11131. (vsi->type == I40E_VSI_VMDQ2)) {
  11132. ret = i40e_vsi_config_rss(vsi);
  11133. }
  11134. return vsi;
  11135. err_rings:
  11136. i40e_vsi_free_q_vectors(vsi);
  11137. err_msix:
  11138. if (vsi->netdev_registered) {
  11139. vsi->netdev_registered = false;
  11140. unregister_netdev(vsi->netdev);
  11141. free_netdev(vsi->netdev);
  11142. vsi->netdev = NULL;
  11143. }
  11144. err_netdev:
  11145. i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
  11146. err_vsi:
  11147. i40e_vsi_clear(vsi);
  11148. err_alloc:
  11149. return NULL;
  11150. }
  11151. /**
  11152. * i40e_veb_get_bw_info - Query VEB BW information
  11153. * @veb: the veb to query
  11154. *
  11155. * Query the Tx scheduler BW configuration data for given VEB
  11156. **/
  11157. static int i40e_veb_get_bw_info(struct i40e_veb *veb)
  11158. {
  11159. struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
  11160. struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
  11161. struct i40e_pf *pf = veb->pf;
  11162. struct i40e_hw *hw = &pf->hw;
  11163. u32 tc_bw_max;
  11164. int ret = 0;
  11165. int i;
  11166. ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
  11167. &bw_data, NULL);
  11168. if (ret) {
  11169. dev_info(&pf->pdev->dev,
  11170. "query veb bw config failed, err %s aq_err %s\n",
  11171. i40e_stat_str(&pf->hw, ret),
  11172. i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
  11173. goto out;
  11174. }
  11175. ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
  11176. &ets_data, NULL);
  11177. if (ret) {
  11178. dev_info(&pf->pdev->dev,
  11179. "query veb bw ets config failed, err %s aq_err %s\n",
  11180. i40e_stat_str(&pf->hw, ret),
  11181. i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
  11182. goto out;
  11183. }
  11184. veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
  11185. veb->bw_max_quanta = ets_data.tc_bw_max;
  11186. veb->is_abs_credits = bw_data.absolute_credits_enable;
  11187. veb->enabled_tc = ets_data.tc_valid_bits;
  11188. tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
  11189. (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
  11190. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  11191. veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
  11192. veb->bw_tc_limit_credits[i] =
  11193. le16_to_cpu(bw_data.tc_bw_limits[i]);
  11194. veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
  11195. }
  11196. out:
  11197. return ret;
  11198. }
  11199. /**
  11200. * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
  11201. * @pf: board private structure
  11202. *
  11203. * On error: returns error code (negative)
  11204. * On success: returns vsi index in PF (positive)
  11205. **/
  11206. static int i40e_veb_mem_alloc(struct i40e_pf *pf)
  11207. {
  11208. int ret = -ENOENT;
  11209. struct i40e_veb *veb;
  11210. int i;
  11211. /* Need to protect the allocation of switch elements at the PF level */
  11212. mutex_lock(&pf->switch_mutex);
  11213. /* VEB list may be fragmented if VEB creation/destruction has
  11214. * been happening. We can afford to do a quick scan to look
  11215. * for any free slots in the list.
  11216. *
  11217. * find next empty veb slot, looping back around if necessary
  11218. */
  11219. i = 0;
  11220. while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
  11221. i++;
  11222. if (i >= I40E_MAX_VEB) {
  11223. ret = -ENOMEM;
  11224. goto err_alloc_veb; /* out of VEB slots! */
  11225. }
  11226. veb = kzalloc(sizeof(*veb), GFP_KERNEL);
  11227. if (!veb) {
  11228. ret = -ENOMEM;
  11229. goto err_alloc_veb;
  11230. }
  11231. veb->pf = pf;
  11232. veb->idx = i;
  11233. veb->enabled_tc = 1;
  11234. pf->veb[i] = veb;
  11235. ret = i;
  11236. err_alloc_veb:
  11237. mutex_unlock(&pf->switch_mutex);
  11238. return ret;
  11239. }
  11240. /**
  11241. * i40e_switch_branch_release - Delete a branch of the switch tree
  11242. * @branch: where to start deleting
  11243. *
  11244. * This uses recursion to find the tips of the branch to be
  11245. * removed, deleting until we get back to and can delete this VEB.
  11246. **/
  11247. static void i40e_switch_branch_release(struct i40e_veb *branch)
  11248. {
  11249. struct i40e_pf *pf = branch->pf;
  11250. u16 branch_seid = branch->seid;
  11251. u16 veb_idx = branch->idx;
  11252. int i;
  11253. /* release any VEBs on this VEB - RECURSION */
  11254. for (i = 0; i < I40E_MAX_VEB; i++) {
  11255. if (!pf->veb[i])
  11256. continue;
  11257. if (pf->veb[i]->uplink_seid == branch->seid)
  11258. i40e_switch_branch_release(pf->veb[i]);
  11259. }
  11260. /* Release the VSIs on this VEB, but not the owner VSI.
  11261. *
  11262. * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
  11263. * the VEB itself, so don't use (*branch) after this loop.
  11264. */
  11265. for (i = 0; i < pf->num_alloc_vsi; i++) {
  11266. if (!pf->vsi[i])
  11267. continue;
  11268. if (pf->vsi[i]->uplink_seid == branch_seid &&
  11269. (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
  11270. i40e_vsi_release(pf->vsi[i]);
  11271. }
  11272. }
  11273. /* There's one corner case where the VEB might not have been
  11274. * removed, so double check it here and remove it if needed.
  11275. * This case happens if the veb was created from the debugfs
  11276. * commands and no VSIs were added to it.
  11277. */
  11278. if (pf->veb[veb_idx])
  11279. i40e_veb_release(pf->veb[veb_idx]);
  11280. }
  11281. /**
  11282. * i40e_veb_clear - remove veb struct
  11283. * @veb: the veb to remove
  11284. **/
  11285. static void i40e_veb_clear(struct i40e_veb *veb)
  11286. {
  11287. if (!veb)
  11288. return;
  11289. if (veb->pf) {
  11290. struct i40e_pf *pf = veb->pf;
  11291. mutex_lock(&pf->switch_mutex);
  11292. if (pf->veb[veb->idx] == veb)
  11293. pf->veb[veb->idx] = NULL;
  11294. mutex_unlock(&pf->switch_mutex);
  11295. }
  11296. kfree(veb);
  11297. }
  11298. /**
  11299. * i40e_veb_release - Delete a VEB and free its resources
  11300. * @veb: the VEB being removed
  11301. **/
  11302. void i40e_veb_release(struct i40e_veb *veb)
  11303. {
  11304. struct i40e_vsi *vsi = NULL;
  11305. struct i40e_pf *pf;
  11306. int i, n = 0;
  11307. pf = veb->pf;
  11308. /* find the remaining VSI and check for extras */
  11309. for (i = 0; i < pf->num_alloc_vsi; i++) {
  11310. if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
  11311. n++;
  11312. vsi = pf->vsi[i];
  11313. }
  11314. }
  11315. if (n != 1) {
  11316. dev_info(&pf->pdev->dev,
  11317. "can't remove VEB %d with %d VSIs left\n",
  11318. veb->seid, n);
  11319. return;
  11320. }
  11321. /* move the remaining VSI to uplink veb */
  11322. vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
  11323. if (veb->uplink_seid) {
  11324. vsi->uplink_seid = veb->uplink_seid;
  11325. if (veb->uplink_seid == pf->mac_seid)
  11326. vsi->veb_idx = I40E_NO_VEB;
  11327. else
  11328. vsi->veb_idx = veb->veb_idx;
  11329. } else {
  11330. /* floating VEB */
  11331. vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
  11332. vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
  11333. }
  11334. i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
  11335. i40e_veb_clear(veb);
  11336. }
  11337. /**
  11338. * i40e_add_veb - create the VEB in the switch
  11339. * @veb: the VEB to be instantiated
  11340. * @vsi: the controlling VSI
  11341. **/
  11342. static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
  11343. {
  11344. struct i40e_pf *pf = veb->pf;
  11345. bool enable_stats = !!(pf->flags & I40E_FLAG_VEB_STATS_ENABLED);
  11346. int ret;
  11347. ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi->seid,
  11348. veb->enabled_tc, false,
  11349. &veb->seid, enable_stats, NULL);
  11350. /* get a VEB from the hardware */
  11351. if (ret) {
  11352. dev_info(&pf->pdev->dev,
  11353. "couldn't add VEB, err %s aq_err %s\n",
  11354. i40e_stat_str(&pf->hw, ret),
  11355. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  11356. return -EPERM;
  11357. }
  11358. /* get statistics counter */
  11359. ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
  11360. &veb->stats_idx, NULL, NULL, NULL);
  11361. if (ret) {
  11362. dev_info(&pf->pdev->dev,
  11363. "couldn't get VEB statistics idx, err %s aq_err %s\n",
  11364. i40e_stat_str(&pf->hw, ret),
  11365. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  11366. return -EPERM;
  11367. }
  11368. ret = i40e_veb_get_bw_info(veb);
  11369. if (ret) {
  11370. dev_info(&pf->pdev->dev,
  11371. "couldn't get VEB bw info, err %s aq_err %s\n",
  11372. i40e_stat_str(&pf->hw, ret),
  11373. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  11374. i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
  11375. return -ENOENT;
  11376. }
  11377. vsi->uplink_seid = veb->seid;
  11378. vsi->veb_idx = veb->idx;
  11379. vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
  11380. return 0;
  11381. }
  11382. /**
  11383. * i40e_veb_setup - Set up a VEB
  11384. * @pf: board private structure
  11385. * @flags: VEB setup flags
  11386. * @uplink_seid: the switch element to link to
  11387. * @vsi_seid: the initial VSI seid
  11388. * @enabled_tc: Enabled TC bit-map
  11389. *
  11390. * This allocates the sw VEB structure and links it into the switch
  11391. * It is possible and legal for this to be a duplicate of an already
  11392. * existing VEB. It is also possible for both uplink and vsi seids
  11393. * to be zero, in order to create a floating VEB.
  11394. *
  11395. * Returns pointer to the successfully allocated VEB sw struct on
  11396. * success, otherwise returns NULL on failure.
  11397. **/
  11398. struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
  11399. u16 uplink_seid, u16 vsi_seid,
  11400. u8 enabled_tc)
  11401. {
  11402. struct i40e_veb *veb, *uplink_veb = NULL;
  11403. int vsi_idx, veb_idx;
  11404. int ret;
  11405. /* if one seid is 0, the other must be 0 to create a floating relay */
  11406. if ((uplink_seid == 0 || vsi_seid == 0) &&
  11407. (uplink_seid + vsi_seid != 0)) {
  11408. dev_info(&pf->pdev->dev,
  11409. "one, not both seid's are 0: uplink=%d vsi=%d\n",
  11410. uplink_seid, vsi_seid);
  11411. return NULL;
  11412. }
  11413. /* make sure there is such a vsi and uplink */
  11414. for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
  11415. if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
  11416. break;
  11417. if (vsi_idx >= pf->num_alloc_vsi && vsi_seid != 0) {
  11418. dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
  11419. vsi_seid);
  11420. return NULL;
  11421. }
  11422. if (uplink_seid && uplink_seid != pf->mac_seid) {
  11423. for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
  11424. if (pf->veb[veb_idx] &&
  11425. pf->veb[veb_idx]->seid == uplink_seid) {
  11426. uplink_veb = pf->veb[veb_idx];
  11427. break;
  11428. }
  11429. }
  11430. if (!uplink_veb) {
  11431. dev_info(&pf->pdev->dev,
  11432. "uplink seid %d not found\n", uplink_seid);
  11433. return NULL;
  11434. }
  11435. }
  11436. /* get veb sw struct */
  11437. veb_idx = i40e_veb_mem_alloc(pf);
  11438. if (veb_idx < 0)
  11439. goto err_alloc;
  11440. veb = pf->veb[veb_idx];
  11441. veb->flags = flags;
  11442. veb->uplink_seid = uplink_seid;
  11443. veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
  11444. veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
  11445. /* create the VEB in the switch */
  11446. ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
  11447. if (ret)
  11448. goto err_veb;
  11449. if (vsi_idx == pf->lan_vsi)
  11450. pf->lan_veb = veb->idx;
  11451. return veb;
  11452. err_veb:
  11453. i40e_veb_clear(veb);
  11454. err_alloc:
  11455. return NULL;
  11456. }
  11457. /**
  11458. * i40e_setup_pf_switch_element - set PF vars based on switch type
  11459. * @pf: board private structure
  11460. * @ele: element we are building info from
  11461. * @num_reported: total number of elements
  11462. * @printconfig: should we print the contents
  11463. *
  11464. * helper function to assist in extracting a few useful SEID values.
  11465. **/
  11466. static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
  11467. struct i40e_aqc_switch_config_element_resp *ele,
  11468. u16 num_reported, bool printconfig)
  11469. {
  11470. u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
  11471. u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
  11472. u8 element_type = ele->element_type;
  11473. u16 seid = le16_to_cpu(ele->seid);
  11474. if (printconfig)
  11475. dev_info(&pf->pdev->dev,
  11476. "type=%d seid=%d uplink=%d downlink=%d\n",
  11477. element_type, seid, uplink_seid, downlink_seid);
  11478. switch (element_type) {
  11479. case I40E_SWITCH_ELEMENT_TYPE_MAC:
  11480. pf->mac_seid = seid;
  11481. break;
  11482. case I40E_SWITCH_ELEMENT_TYPE_VEB:
  11483. /* Main VEB? */
  11484. if (uplink_seid != pf->mac_seid)
  11485. break;
  11486. if (pf->lan_veb == I40E_NO_VEB) {
  11487. int v;
  11488. /* find existing or else empty VEB */
  11489. for (v = 0; v < I40E_MAX_VEB; v++) {
  11490. if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
  11491. pf->lan_veb = v;
  11492. break;
  11493. }
  11494. }
  11495. if (pf->lan_veb == I40E_NO_VEB) {
  11496. v = i40e_veb_mem_alloc(pf);
  11497. if (v < 0)
  11498. break;
  11499. pf->lan_veb = v;
  11500. }
  11501. }
  11502. pf->veb[pf->lan_veb]->seid = seid;
  11503. pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
  11504. pf->veb[pf->lan_veb]->pf = pf;
  11505. pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
  11506. break;
  11507. case I40E_SWITCH_ELEMENT_TYPE_VSI:
  11508. if (num_reported != 1)
  11509. break;
  11510. /* This is immediately after a reset so we can assume this is
  11511. * the PF's VSI
  11512. */
  11513. pf->mac_seid = uplink_seid;
  11514. pf->pf_seid = downlink_seid;
  11515. pf->main_vsi_seid = seid;
  11516. if (printconfig)
  11517. dev_info(&pf->pdev->dev,
  11518. "pf_seid=%d main_vsi_seid=%d\n",
  11519. pf->pf_seid, pf->main_vsi_seid);
  11520. break;
  11521. case I40E_SWITCH_ELEMENT_TYPE_PF:
  11522. case I40E_SWITCH_ELEMENT_TYPE_VF:
  11523. case I40E_SWITCH_ELEMENT_TYPE_EMP:
  11524. case I40E_SWITCH_ELEMENT_TYPE_BMC:
  11525. case I40E_SWITCH_ELEMENT_TYPE_PE:
  11526. case I40E_SWITCH_ELEMENT_TYPE_PA:
  11527. /* ignore these for now */
  11528. break;
  11529. default:
  11530. dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
  11531. element_type, seid);
  11532. break;
  11533. }
  11534. }
  11535. /**
  11536. * i40e_fetch_switch_configuration - Get switch config from firmware
  11537. * @pf: board private structure
  11538. * @printconfig: should we print the contents
  11539. *
  11540. * Get the current switch configuration from the device and
  11541. * extract a few useful SEID values.
  11542. **/
  11543. int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
  11544. {
  11545. struct i40e_aqc_get_switch_config_resp *sw_config;
  11546. u16 next_seid = 0;
  11547. int ret = 0;
  11548. u8 *aq_buf;
  11549. int i;
  11550. aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
  11551. if (!aq_buf)
  11552. return -ENOMEM;
  11553. sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
  11554. do {
  11555. u16 num_reported, num_total;
  11556. ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
  11557. I40E_AQ_LARGE_BUF,
  11558. &next_seid, NULL);
  11559. if (ret) {
  11560. dev_info(&pf->pdev->dev,
  11561. "get switch config failed err %s aq_err %s\n",
  11562. i40e_stat_str(&pf->hw, ret),
  11563. i40e_aq_str(&pf->hw,
  11564. pf->hw.aq.asq_last_status));
  11565. kfree(aq_buf);
  11566. return -ENOENT;
  11567. }
  11568. num_reported = le16_to_cpu(sw_config->header.num_reported);
  11569. num_total = le16_to_cpu(sw_config->header.num_total);
  11570. if (printconfig)
  11571. dev_info(&pf->pdev->dev,
  11572. "header: %d reported %d total\n",
  11573. num_reported, num_total);
  11574. for (i = 0; i < num_reported; i++) {
  11575. struct i40e_aqc_switch_config_element_resp *ele =
  11576. &sw_config->element[i];
  11577. i40e_setup_pf_switch_element(pf, ele, num_reported,
  11578. printconfig);
  11579. }
  11580. } while (next_seid != 0);
  11581. kfree(aq_buf);
  11582. return ret;
  11583. }
  11584. /**
  11585. * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
  11586. * @pf: board private structure
  11587. * @reinit: if the Main VSI needs to re-initialized.
  11588. *
  11589. * Returns 0 on success, negative value on failure
  11590. **/
  11591. static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit)
  11592. {
  11593. u16 flags = 0;
  11594. int ret;
  11595. /* find out what's out there already */
  11596. ret = i40e_fetch_switch_configuration(pf, false);
  11597. if (ret) {
  11598. dev_info(&pf->pdev->dev,
  11599. "couldn't fetch switch config, err %s aq_err %s\n",
  11600. i40e_stat_str(&pf->hw, ret),
  11601. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  11602. return ret;
  11603. }
  11604. i40e_pf_reset_stats(pf);
  11605. /* set the switch config bit for the whole device to
  11606. * support limited promisc or true promisc
  11607. * when user requests promisc. The default is limited
  11608. * promisc.
  11609. */
  11610. if ((pf->hw.pf_id == 0) &&
  11611. !(pf->flags & I40E_FLAG_TRUE_PROMISC_SUPPORT)) {
  11612. flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
  11613. pf->last_sw_conf_flags = flags;
  11614. }
  11615. if (pf->hw.pf_id == 0) {
  11616. u16 valid_flags;
  11617. valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
  11618. ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags, 0,
  11619. NULL);
  11620. if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) {
  11621. dev_info(&pf->pdev->dev,
  11622. "couldn't set switch config bits, err %s aq_err %s\n",
  11623. i40e_stat_str(&pf->hw, ret),
  11624. i40e_aq_str(&pf->hw,
  11625. pf->hw.aq.asq_last_status));
  11626. /* not a fatal problem, just keep going */
  11627. }
  11628. pf->last_sw_conf_valid_flags = valid_flags;
  11629. }
  11630. /* first time setup */
  11631. if (pf->lan_vsi == I40E_NO_VSI || reinit) {
  11632. struct i40e_vsi *vsi = NULL;
  11633. u16 uplink_seid;
  11634. /* Set up the PF VSI associated with the PF's main VSI
  11635. * that is already in the HW switch
  11636. */
  11637. if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
  11638. uplink_seid = pf->veb[pf->lan_veb]->seid;
  11639. else
  11640. uplink_seid = pf->mac_seid;
  11641. if (pf->lan_vsi == I40E_NO_VSI)
  11642. vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
  11643. else if (reinit)
  11644. vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
  11645. if (!vsi) {
  11646. dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
  11647. i40e_cloud_filter_exit(pf);
  11648. i40e_fdir_teardown(pf);
  11649. return -EAGAIN;
  11650. }
  11651. } else {
  11652. /* force a reset of TC and queue layout configurations */
  11653. u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
  11654. pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
  11655. pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
  11656. i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
  11657. }
  11658. i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
  11659. i40e_fdir_sb_setup(pf);
  11660. /* Setup static PF queue filter control settings */
  11661. ret = i40e_setup_pf_filter_control(pf);
  11662. if (ret) {
  11663. dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
  11664. ret);
  11665. /* Failure here should not stop continuing other steps */
  11666. }
  11667. /* enable RSS in the HW, even for only one queue, as the stack can use
  11668. * the hash
  11669. */
  11670. if ((pf->flags & I40E_FLAG_RSS_ENABLED))
  11671. i40e_pf_config_rss(pf);
  11672. /* fill in link information and enable LSE reporting */
  11673. i40e_link_event(pf);
  11674. /* Initialize user-specific link properties */
  11675. pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
  11676. I40E_AQ_AN_COMPLETED) ? true : false);
  11677. i40e_ptp_init(pf);
  11678. /* repopulate tunnel port filters */
  11679. i40e_sync_udp_filters(pf);
  11680. return ret;
  11681. }
  11682. /**
  11683. * i40e_determine_queue_usage - Work out queue distribution
  11684. * @pf: board private structure
  11685. **/
  11686. static void i40e_determine_queue_usage(struct i40e_pf *pf)
  11687. {
  11688. int queues_left;
  11689. int q_max;
  11690. pf->num_lan_qps = 0;
  11691. /* Find the max queues to be put into basic use. We'll always be
  11692. * using TC0, whether or not DCB is running, and TC0 will get the
  11693. * big RSS set.
  11694. */
  11695. queues_left = pf->hw.func_caps.num_tx_qp;
  11696. if ((queues_left == 1) ||
  11697. !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
  11698. /* one qp for PF, no queues for anything else */
  11699. queues_left = 0;
  11700. pf->alloc_rss_size = pf->num_lan_qps = 1;
  11701. /* make sure all the fancies are disabled */
  11702. pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
  11703. I40E_FLAG_IWARP_ENABLED |
  11704. I40E_FLAG_FD_SB_ENABLED |
  11705. I40E_FLAG_FD_ATR_ENABLED |
  11706. I40E_FLAG_DCB_CAPABLE |
  11707. I40E_FLAG_DCB_ENABLED |
  11708. I40E_FLAG_SRIOV_ENABLED |
  11709. I40E_FLAG_VMDQ_ENABLED);
  11710. pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
  11711. } else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
  11712. I40E_FLAG_FD_SB_ENABLED |
  11713. I40E_FLAG_FD_ATR_ENABLED |
  11714. I40E_FLAG_DCB_CAPABLE))) {
  11715. /* one qp for PF */
  11716. pf->alloc_rss_size = pf->num_lan_qps = 1;
  11717. queues_left -= pf->num_lan_qps;
  11718. pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
  11719. I40E_FLAG_IWARP_ENABLED |
  11720. I40E_FLAG_FD_SB_ENABLED |
  11721. I40E_FLAG_FD_ATR_ENABLED |
  11722. I40E_FLAG_DCB_ENABLED |
  11723. I40E_FLAG_VMDQ_ENABLED);
  11724. pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
  11725. } else {
  11726. /* Not enough queues for all TCs */
  11727. if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
  11728. (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
  11729. pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
  11730. I40E_FLAG_DCB_ENABLED);
  11731. dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
  11732. }
  11733. /* limit lan qps to the smaller of qps, cpus or msix */
  11734. q_max = max_t(int, pf->rss_size_max, num_online_cpus());
  11735. q_max = min_t(int, q_max, pf->hw.func_caps.num_tx_qp);
  11736. q_max = min_t(int, q_max, pf->hw.func_caps.num_msix_vectors);
  11737. pf->num_lan_qps = q_max;
  11738. queues_left -= pf->num_lan_qps;
  11739. }
  11740. if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
  11741. if (queues_left > 1) {
  11742. queues_left -= 1; /* save 1 queue for FD */
  11743. } else {
  11744. pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
  11745. pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
  11746. dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
  11747. }
  11748. }
  11749. if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
  11750. pf->num_vf_qps && pf->num_req_vfs && queues_left) {
  11751. pf->num_req_vfs = min_t(int, pf->num_req_vfs,
  11752. (queues_left / pf->num_vf_qps));
  11753. queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
  11754. }
  11755. if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
  11756. pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
  11757. pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
  11758. (queues_left / pf->num_vmdq_qps));
  11759. queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
  11760. }
  11761. pf->queues_left = queues_left;
  11762. dev_dbg(&pf->pdev->dev,
  11763. "qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
  11764. pf->hw.func_caps.num_tx_qp,
  11765. !!(pf->flags & I40E_FLAG_FD_SB_ENABLED),
  11766. pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
  11767. pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
  11768. queues_left);
  11769. }
  11770. /**
  11771. * i40e_setup_pf_filter_control - Setup PF static filter control
  11772. * @pf: PF to be setup
  11773. *
  11774. * i40e_setup_pf_filter_control sets up a PF's initial filter control
  11775. * settings. If PE/FCoE are enabled then it will also set the per PF
  11776. * based filter sizes required for them. It also enables Flow director,
  11777. * ethertype and macvlan type filter settings for the pf.
  11778. *
  11779. * Returns 0 on success, negative on failure
  11780. **/
  11781. static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
  11782. {
  11783. struct i40e_filter_control_settings *settings = &pf->filter_settings;
  11784. settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
  11785. /* Flow Director is enabled */
  11786. if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
  11787. settings->enable_fdir = true;
  11788. /* Ethtype and MACVLAN filters enabled for PF */
  11789. settings->enable_ethtype = true;
  11790. settings->enable_macvlan = true;
  11791. if (i40e_set_filter_control(&pf->hw, settings))
  11792. return -ENOENT;
  11793. return 0;
  11794. }
  11795. #define INFO_STRING_LEN 255
  11796. #define REMAIN(__x) (INFO_STRING_LEN - (__x))
  11797. static void i40e_print_features(struct i40e_pf *pf)
  11798. {
  11799. struct i40e_hw *hw = &pf->hw;
  11800. char *buf;
  11801. int i;
  11802. buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
  11803. if (!buf)
  11804. return;
  11805. i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
  11806. #ifdef CONFIG_PCI_IOV
  11807. i += snprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
  11808. #endif
  11809. i += snprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d",
  11810. pf->hw.func_caps.num_vsis,
  11811. pf->vsi[pf->lan_vsi]->num_queue_pairs);
  11812. if (pf->flags & I40E_FLAG_RSS_ENABLED)
  11813. i += snprintf(&buf[i], REMAIN(i), " RSS");
  11814. if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
  11815. i += snprintf(&buf[i], REMAIN(i), " FD_ATR");
  11816. if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
  11817. i += snprintf(&buf[i], REMAIN(i), " FD_SB");
  11818. i += snprintf(&buf[i], REMAIN(i), " NTUPLE");
  11819. }
  11820. if (pf->flags & I40E_FLAG_DCB_CAPABLE)
  11821. i += snprintf(&buf[i], REMAIN(i), " DCB");
  11822. i += snprintf(&buf[i], REMAIN(i), " VxLAN");
  11823. i += snprintf(&buf[i], REMAIN(i), " Geneve");
  11824. if (pf->flags & I40E_FLAG_PTP)
  11825. i += snprintf(&buf[i], REMAIN(i), " PTP");
  11826. if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
  11827. i += snprintf(&buf[i], REMAIN(i), " VEB");
  11828. else
  11829. i += snprintf(&buf[i], REMAIN(i), " VEPA");
  11830. dev_info(&pf->pdev->dev, "%s\n", buf);
  11831. kfree(buf);
  11832. WARN_ON(i > INFO_STRING_LEN);
  11833. }
  11834. /**
  11835. * i40e_get_platform_mac_addr - get platform-specific MAC address
  11836. * @pdev: PCI device information struct
  11837. * @pf: board private structure
  11838. *
  11839. * Look up the MAC address for the device. First we'll try
  11840. * eth_platform_get_mac_address, which will check Open Firmware, or arch
  11841. * specific fallback. Otherwise, we'll default to the stored value in
  11842. * firmware.
  11843. **/
  11844. static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
  11845. {
  11846. if (eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
  11847. i40e_get_mac_addr(&pf->hw, pf->hw.mac.addr);
  11848. }
  11849. /**
  11850. * i40e_probe - Device initialization routine
  11851. * @pdev: PCI device information struct
  11852. * @ent: entry in i40e_pci_tbl
  11853. *
  11854. * i40e_probe initializes a PF identified by a pci_dev structure.
  11855. * The OS initialization, configuring of the PF private structure,
  11856. * and a hardware reset occur.
  11857. *
  11858. * Returns 0 on success, negative on failure
  11859. **/
  11860. static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  11861. {
  11862. struct i40e_aq_get_phy_abilities_resp abilities;
  11863. struct i40e_pf *pf;
  11864. struct i40e_hw *hw;
  11865. static u16 pfs_found;
  11866. u16 wol_nvm_bits;
  11867. u16 link_status;
  11868. int err;
  11869. u32 val;
  11870. u32 i;
  11871. u8 set_fc_aq_fail;
  11872. err = pci_enable_device_mem(pdev);
  11873. if (err)
  11874. return err;
  11875. /* set up for high or low dma */
  11876. err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
  11877. if (err) {
  11878. err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  11879. if (err) {
  11880. dev_err(&pdev->dev,
  11881. "DMA configuration failed: 0x%x\n", err);
  11882. goto err_dma;
  11883. }
  11884. }
  11885. /* set up pci connections */
  11886. err = pci_request_mem_regions(pdev, i40e_driver_name);
  11887. if (err) {
  11888. dev_info(&pdev->dev,
  11889. "pci_request_selected_regions failed %d\n", err);
  11890. goto err_pci_reg;
  11891. }
  11892. pci_enable_pcie_error_reporting(pdev);
  11893. pci_set_master(pdev);
  11894. /* Now that we have a PCI connection, we need to do the
  11895. * low level device setup. This is primarily setting up
  11896. * the Admin Queue structures and then querying for the
  11897. * device's current profile information.
  11898. */
  11899. pf = kzalloc(sizeof(*pf), GFP_KERNEL);
  11900. if (!pf) {
  11901. err = -ENOMEM;
  11902. goto err_pf_alloc;
  11903. }
  11904. pf->next_vsi = 0;
  11905. pf->pdev = pdev;
  11906. set_bit(__I40E_DOWN, pf->state);
  11907. hw = &pf->hw;
  11908. hw->back = pf;
  11909. pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
  11910. I40E_MAX_CSR_SPACE);
  11911. hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
  11912. if (!hw->hw_addr) {
  11913. err = -EIO;
  11914. dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
  11915. (unsigned int)pci_resource_start(pdev, 0),
  11916. pf->ioremap_len, err);
  11917. goto err_ioremap;
  11918. }
  11919. hw->vendor_id = pdev->vendor;
  11920. hw->device_id = pdev->device;
  11921. pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
  11922. hw->subsystem_vendor_id = pdev->subsystem_vendor;
  11923. hw->subsystem_device_id = pdev->subsystem_device;
  11924. hw->bus.device = PCI_SLOT(pdev->devfn);
  11925. hw->bus.func = PCI_FUNC(pdev->devfn);
  11926. hw->bus.bus_id = pdev->bus->number;
  11927. pf->instance = pfs_found;
  11928. /* Select something other than the 802.1ad ethertype for the
  11929. * switch to use internally and drop on ingress.
  11930. */
  11931. hw->switch_tag = 0xffff;
  11932. hw->first_tag = ETH_P_8021AD;
  11933. hw->second_tag = ETH_P_8021Q;
  11934. INIT_LIST_HEAD(&pf->l3_flex_pit_list);
  11935. INIT_LIST_HEAD(&pf->l4_flex_pit_list);
  11936. /* set up the locks for the AQ, do this only once in probe
  11937. * and destroy them only once in remove
  11938. */
  11939. mutex_init(&hw->aq.asq_mutex);
  11940. mutex_init(&hw->aq.arq_mutex);
  11941. pf->msg_enable = netif_msg_init(debug,
  11942. NETIF_MSG_DRV |
  11943. NETIF_MSG_PROBE |
  11944. NETIF_MSG_LINK);
  11945. if (debug < -1)
  11946. pf->hw.debug_mask = debug;
  11947. /* do a special CORER for clearing PXE mode once at init */
  11948. if (hw->revision_id == 0 &&
  11949. (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
  11950. wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
  11951. i40e_flush(hw);
  11952. msleep(200);
  11953. pf->corer_count++;
  11954. i40e_clear_pxe_mode(hw);
  11955. }
  11956. /* Reset here to make sure all is clean and to define PF 'n' */
  11957. i40e_clear_hw(hw);
  11958. err = i40e_pf_reset(hw);
  11959. if (err) {
  11960. dev_info(&pdev->dev, "Initial pf_reset failed: %d\n", err);
  11961. goto err_pf_reset;
  11962. }
  11963. pf->pfr_count++;
  11964. hw->aq.num_arq_entries = I40E_AQ_LEN;
  11965. hw->aq.num_asq_entries = I40E_AQ_LEN;
  11966. hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
  11967. hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
  11968. pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
  11969. snprintf(pf->int_name, sizeof(pf->int_name) - 1,
  11970. "%s-%s:misc",
  11971. dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
  11972. err = i40e_init_shared_code(hw);
  11973. if (err) {
  11974. dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
  11975. err);
  11976. goto err_pf_reset;
  11977. }
  11978. /* set up a default setting for link flow control */
  11979. pf->hw.fc.requested_mode = I40E_FC_NONE;
  11980. err = i40e_init_adminq(hw);
  11981. if (err) {
  11982. if (err == I40E_ERR_FIRMWARE_API_VERSION)
  11983. dev_info(&pdev->dev,
  11984. "The driver for the device stopped because the NVM image is newer than expected. You must install the most recent version of the network driver.\n");
  11985. else
  11986. dev_info(&pdev->dev,
  11987. "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
  11988. goto err_pf_reset;
  11989. }
  11990. i40e_get_oem_version(hw);
  11991. /* provide nvm, fw, api versions */
  11992. dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s\n",
  11993. hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
  11994. hw->aq.api_maj_ver, hw->aq.api_min_ver,
  11995. i40e_nvm_version_str(hw));
  11996. if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
  11997. hw->aq.api_min_ver > I40E_FW_MINOR_VERSION(hw))
  11998. dev_info(&pdev->dev,
  11999. "The driver for the device detected a newer version of the NVM image than expected. Please install the most recent version of the network driver.\n");
  12000. else if (hw->aq.api_maj_ver == 1 && hw->aq.api_min_ver < 4)
  12001. dev_info(&pdev->dev,
  12002. "The driver for the device detected an older version of the NVM image than expected. Please update the NVM image.\n");
  12003. i40e_verify_eeprom(pf);
  12004. /* Rev 0 hardware was never productized */
  12005. if (hw->revision_id < 1)
  12006. dev_warn(&pdev->dev, "This device is a pre-production adapter/LOM. Please be aware there may be issues with your hardware. If you are experiencing problems please contact your Intel or hardware representative who provided you with this hardware.\n");
  12007. i40e_clear_pxe_mode(hw);
  12008. err = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
  12009. if (err)
  12010. goto err_adminq_setup;
  12011. err = i40e_sw_init(pf);
  12012. if (err) {
  12013. dev_info(&pdev->dev, "sw_init failed: %d\n", err);
  12014. goto err_sw_init;
  12015. }
  12016. err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
  12017. hw->func_caps.num_rx_qp, 0, 0);
  12018. if (err) {
  12019. dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
  12020. goto err_init_lan_hmc;
  12021. }
  12022. err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
  12023. if (err) {
  12024. dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
  12025. err = -ENOENT;
  12026. goto err_configure_lan_hmc;
  12027. }
  12028. /* Disable LLDP for NICs that have firmware versions lower than v4.3.
  12029. * Ignore error return codes because if it was already disabled via
  12030. * hardware settings this will fail
  12031. */
  12032. if (pf->hw_features & I40E_HW_STOP_FW_LLDP) {
  12033. dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
  12034. i40e_aq_stop_lldp(hw, true, NULL);
  12035. }
  12036. /* allow a platform config to override the HW addr */
  12037. i40e_get_platform_mac_addr(pdev, pf);
  12038. if (!is_valid_ether_addr(hw->mac.addr)) {
  12039. dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
  12040. err = -EIO;
  12041. goto err_mac_addr;
  12042. }
  12043. dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
  12044. ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
  12045. i40e_get_port_mac_addr(hw, hw->mac.port_addr);
  12046. if (is_valid_ether_addr(hw->mac.port_addr))
  12047. pf->hw_features |= I40E_HW_PORT_ID_VALID;
  12048. pci_set_drvdata(pdev, pf);
  12049. pci_save_state(pdev);
  12050. /* Enable FW to write default DCB config on link-up */
  12051. i40e_aq_set_dcb_parameters(hw, true, NULL);
  12052. #ifdef CONFIG_I40E_DCB
  12053. err = i40e_init_pf_dcb(pf);
  12054. if (err) {
  12055. dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
  12056. pf->flags &= ~(I40E_FLAG_DCB_CAPABLE | I40E_FLAG_DCB_ENABLED);
  12057. /* Continue without DCB enabled */
  12058. }
  12059. #endif /* CONFIG_I40E_DCB */
  12060. /* set up periodic task facility */
  12061. timer_setup(&pf->service_timer, i40e_service_timer, 0);
  12062. pf->service_timer_period = HZ;
  12063. INIT_WORK(&pf->service_task, i40e_service_task);
  12064. clear_bit(__I40E_SERVICE_SCHED, pf->state);
  12065. /* NVM bit on means WoL disabled for the port */
  12066. i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
  12067. if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
  12068. pf->wol_en = false;
  12069. else
  12070. pf->wol_en = true;
  12071. device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
  12072. /* set up the main switch operations */
  12073. i40e_determine_queue_usage(pf);
  12074. err = i40e_init_interrupt_scheme(pf);
  12075. if (err)
  12076. goto err_switch_setup;
  12077. /* The number of VSIs reported by the FW is the minimum guaranteed
  12078. * to us; HW supports far more and we share the remaining pool with
  12079. * the other PFs. We allocate space for more than the guarantee with
  12080. * the understanding that we might not get them all later.
  12081. */
  12082. if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
  12083. pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
  12084. else
  12085. pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
  12086. /* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
  12087. pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
  12088. GFP_KERNEL);
  12089. if (!pf->vsi) {
  12090. err = -ENOMEM;
  12091. goto err_switch_setup;
  12092. }
  12093. #ifdef CONFIG_PCI_IOV
  12094. /* prep for VF support */
  12095. if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
  12096. (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
  12097. !test_bit(__I40E_BAD_EEPROM, pf->state)) {
  12098. if (pci_num_vf(pdev))
  12099. pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
  12100. }
  12101. #endif
  12102. err = i40e_setup_pf_switch(pf, false);
  12103. if (err) {
  12104. dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
  12105. goto err_vsis;
  12106. }
  12107. INIT_LIST_HEAD(&pf->vsi[pf->lan_vsi]->ch_list);
  12108. /* Make sure flow control is set according to current settings */
  12109. err = i40e_set_fc(hw, &set_fc_aq_fail, true);
  12110. if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_GET)
  12111. dev_dbg(&pf->pdev->dev,
  12112. "Set fc with err %s aq_err %s on get_phy_cap\n",
  12113. i40e_stat_str(hw, err),
  12114. i40e_aq_str(hw, hw->aq.asq_last_status));
  12115. if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_SET)
  12116. dev_dbg(&pf->pdev->dev,
  12117. "Set fc with err %s aq_err %s on set_phy_config\n",
  12118. i40e_stat_str(hw, err),
  12119. i40e_aq_str(hw, hw->aq.asq_last_status));
  12120. if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_UPDATE)
  12121. dev_dbg(&pf->pdev->dev,
  12122. "Set fc with err %s aq_err %s on get_link_info\n",
  12123. i40e_stat_str(hw, err),
  12124. i40e_aq_str(hw, hw->aq.asq_last_status));
  12125. /* if FDIR VSI was set up, start it now */
  12126. for (i = 0; i < pf->num_alloc_vsi; i++) {
  12127. if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
  12128. i40e_vsi_open(pf->vsi[i]);
  12129. break;
  12130. }
  12131. }
  12132. /* The driver only wants link up/down and module qualification
  12133. * reports from firmware. Note the negative logic.
  12134. */
  12135. err = i40e_aq_set_phy_int_mask(&pf->hw,
  12136. ~(I40E_AQ_EVENT_LINK_UPDOWN |
  12137. I40E_AQ_EVENT_MEDIA_NA |
  12138. I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
  12139. if (err)
  12140. dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
  12141. i40e_stat_str(&pf->hw, err),
  12142. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  12143. /* Reconfigure hardware for allowing smaller MSS in the case
  12144. * of TSO, so that we avoid the MDD being fired and causing
  12145. * a reset in the case of small MSS+TSO.
  12146. */
  12147. val = rd32(hw, I40E_REG_MSS);
  12148. if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
  12149. val &= ~I40E_REG_MSS_MIN_MASK;
  12150. val |= I40E_64BYTE_MSS;
  12151. wr32(hw, I40E_REG_MSS, val);
  12152. }
  12153. if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
  12154. msleep(75);
  12155. err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
  12156. if (err)
  12157. dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
  12158. i40e_stat_str(&pf->hw, err),
  12159. i40e_aq_str(&pf->hw,
  12160. pf->hw.aq.asq_last_status));
  12161. }
  12162. /* The main driver is (mostly) up and happy. We need to set this state
  12163. * before setting up the misc vector or we get a race and the vector
  12164. * ends up disabled forever.
  12165. */
  12166. clear_bit(__I40E_DOWN, pf->state);
  12167. /* In case of MSIX we are going to setup the misc vector right here
  12168. * to handle admin queue events etc. In case of legacy and MSI
  12169. * the misc functionality and queue processing is combined in
  12170. * the same vector and that gets setup at open.
  12171. */
  12172. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  12173. err = i40e_setup_misc_vector(pf);
  12174. if (err) {
  12175. dev_info(&pdev->dev,
  12176. "setup of misc vector failed: %d\n", err);
  12177. goto err_vsis;
  12178. }
  12179. }
  12180. #ifdef CONFIG_PCI_IOV
  12181. /* prep for VF support */
  12182. if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
  12183. (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
  12184. !test_bit(__I40E_BAD_EEPROM, pf->state)) {
  12185. /* disable link interrupts for VFs */
  12186. val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
  12187. val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
  12188. wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
  12189. i40e_flush(hw);
  12190. if (pci_num_vf(pdev)) {
  12191. dev_info(&pdev->dev,
  12192. "Active VFs found, allocating resources.\n");
  12193. err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
  12194. if (err)
  12195. dev_info(&pdev->dev,
  12196. "Error %d allocating resources for existing VFs\n",
  12197. err);
  12198. }
  12199. }
  12200. #endif /* CONFIG_PCI_IOV */
  12201. if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
  12202. pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
  12203. pf->num_iwarp_msix,
  12204. I40E_IWARP_IRQ_PILE_ID);
  12205. if (pf->iwarp_base_vector < 0) {
  12206. dev_info(&pdev->dev,
  12207. "failed to get tracking for %d vectors for IWARP err=%d\n",
  12208. pf->num_iwarp_msix, pf->iwarp_base_vector);
  12209. pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
  12210. }
  12211. }
  12212. i40e_dbg_pf_init(pf);
  12213. /* tell the firmware that we're starting */
  12214. i40e_send_version(pf);
  12215. /* since everything's happy, start the service_task timer */
  12216. mod_timer(&pf->service_timer,
  12217. round_jiffies(jiffies + pf->service_timer_period));
  12218. /* add this PF to client device list and launch a client service task */
  12219. if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
  12220. err = i40e_lan_add_device(pf);
  12221. if (err)
  12222. dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
  12223. err);
  12224. }
  12225. #define PCI_SPEED_SIZE 8
  12226. #define PCI_WIDTH_SIZE 8
  12227. /* Devices on the IOSF bus do not have this information
  12228. * and will report PCI Gen 1 x 1 by default so don't bother
  12229. * checking them.
  12230. */
  12231. if (!(pf->hw_features & I40E_HW_NO_PCI_LINK_CHECK)) {
  12232. char speed[PCI_SPEED_SIZE] = "Unknown";
  12233. char width[PCI_WIDTH_SIZE] = "Unknown";
  12234. /* Get the negotiated link width and speed from PCI config
  12235. * space
  12236. */
  12237. pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
  12238. &link_status);
  12239. i40e_set_pci_config_data(hw, link_status);
  12240. switch (hw->bus.speed) {
  12241. case i40e_bus_speed_8000:
  12242. strncpy(speed, "8.0", PCI_SPEED_SIZE); break;
  12243. case i40e_bus_speed_5000:
  12244. strncpy(speed, "5.0", PCI_SPEED_SIZE); break;
  12245. case i40e_bus_speed_2500:
  12246. strncpy(speed, "2.5", PCI_SPEED_SIZE); break;
  12247. default:
  12248. break;
  12249. }
  12250. switch (hw->bus.width) {
  12251. case i40e_bus_width_pcie_x8:
  12252. strncpy(width, "8", PCI_WIDTH_SIZE); break;
  12253. case i40e_bus_width_pcie_x4:
  12254. strncpy(width, "4", PCI_WIDTH_SIZE); break;
  12255. case i40e_bus_width_pcie_x2:
  12256. strncpy(width, "2", PCI_WIDTH_SIZE); break;
  12257. case i40e_bus_width_pcie_x1:
  12258. strncpy(width, "1", PCI_WIDTH_SIZE); break;
  12259. default:
  12260. break;
  12261. }
  12262. dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
  12263. speed, width);
  12264. if (hw->bus.width < i40e_bus_width_pcie_x8 ||
  12265. hw->bus.speed < i40e_bus_speed_8000) {
  12266. dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
  12267. dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
  12268. }
  12269. }
  12270. /* get the requested speeds from the fw */
  12271. err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
  12272. if (err)
  12273. dev_dbg(&pf->pdev->dev, "get requested speeds ret = %s last_status = %s\n",
  12274. i40e_stat_str(&pf->hw, err),
  12275. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  12276. pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
  12277. /* get the supported phy types from the fw */
  12278. err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
  12279. if (err)
  12280. dev_dbg(&pf->pdev->dev, "get supported phy types ret = %s last_status = %s\n",
  12281. i40e_stat_str(&pf->hw, err),
  12282. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  12283. /* Add a filter to drop all Flow control frames from any VSI from being
  12284. * transmitted. By doing so we stop a malicious VF from sending out
  12285. * PAUSE or PFC frames and potentially controlling traffic for other
  12286. * PF/VF VSIs.
  12287. * The FW can still send Flow control frames if enabled.
  12288. */
  12289. i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
  12290. pf->main_vsi_seid);
  12291. if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
  12292. (pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
  12293. pf->hw_features |= I40E_HW_PHY_CONTROLS_LEDS;
  12294. if (pf->hw.device_id == I40E_DEV_ID_SFP_I_X722)
  12295. pf->hw_features |= I40E_HW_HAVE_CRT_RETIMER;
  12296. /* print a string summarizing features */
  12297. i40e_print_features(pf);
  12298. return 0;
  12299. /* Unwind what we've done if something failed in the setup */
  12300. err_vsis:
  12301. set_bit(__I40E_DOWN, pf->state);
  12302. i40e_clear_interrupt_scheme(pf);
  12303. kfree(pf->vsi);
  12304. err_switch_setup:
  12305. i40e_reset_interrupt_capability(pf);
  12306. del_timer_sync(&pf->service_timer);
  12307. err_mac_addr:
  12308. err_configure_lan_hmc:
  12309. (void)i40e_shutdown_lan_hmc(hw);
  12310. err_init_lan_hmc:
  12311. kfree(pf->qp_pile);
  12312. err_sw_init:
  12313. err_adminq_setup:
  12314. err_pf_reset:
  12315. iounmap(hw->hw_addr);
  12316. err_ioremap:
  12317. kfree(pf);
  12318. err_pf_alloc:
  12319. pci_disable_pcie_error_reporting(pdev);
  12320. pci_release_mem_regions(pdev);
  12321. err_pci_reg:
  12322. err_dma:
  12323. pci_disable_device(pdev);
  12324. return err;
  12325. }
  12326. /**
  12327. * i40e_remove - Device removal routine
  12328. * @pdev: PCI device information struct
  12329. *
  12330. * i40e_remove is called by the PCI subsystem to alert the driver
  12331. * that is should release a PCI device. This could be caused by a
  12332. * Hot-Plug event, or because the driver is going to be removed from
  12333. * memory.
  12334. **/
  12335. static void i40e_remove(struct pci_dev *pdev)
  12336. {
  12337. struct i40e_pf *pf = pci_get_drvdata(pdev);
  12338. struct i40e_hw *hw = &pf->hw;
  12339. i40e_status ret_code;
  12340. int i;
  12341. i40e_dbg_pf_exit(pf);
  12342. i40e_ptp_stop(pf);
  12343. /* Disable RSS in hw */
  12344. i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
  12345. i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
  12346. /* no more scheduling of any task */
  12347. set_bit(__I40E_SUSPENDED, pf->state);
  12348. set_bit(__I40E_DOWN, pf->state);
  12349. if (pf->service_timer.function)
  12350. del_timer_sync(&pf->service_timer);
  12351. if (pf->service_task.func)
  12352. cancel_work_sync(&pf->service_task);
  12353. /* Client close must be called explicitly here because the timer
  12354. * has been stopped.
  12355. */
  12356. i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
  12357. if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
  12358. i40e_free_vfs(pf);
  12359. pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
  12360. }
  12361. i40e_fdir_teardown(pf);
  12362. /* If there is a switch structure or any orphans, remove them.
  12363. * This will leave only the PF's VSI remaining.
  12364. */
  12365. for (i = 0; i < I40E_MAX_VEB; i++) {
  12366. if (!pf->veb[i])
  12367. continue;
  12368. if (pf->veb[i]->uplink_seid == pf->mac_seid ||
  12369. pf->veb[i]->uplink_seid == 0)
  12370. i40e_switch_branch_release(pf->veb[i]);
  12371. }
  12372. /* Now we can shutdown the PF's VSI, just before we kill
  12373. * adminq and hmc.
  12374. */
  12375. if (pf->vsi[pf->lan_vsi])
  12376. i40e_vsi_release(pf->vsi[pf->lan_vsi]);
  12377. i40e_cloud_filter_exit(pf);
  12378. /* remove attached clients */
  12379. if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
  12380. ret_code = i40e_lan_del_device(pf);
  12381. if (ret_code)
  12382. dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
  12383. ret_code);
  12384. }
  12385. /* shutdown and destroy the HMC */
  12386. if (hw->hmc.hmc_obj) {
  12387. ret_code = i40e_shutdown_lan_hmc(hw);
  12388. if (ret_code)
  12389. dev_warn(&pdev->dev,
  12390. "Failed to destroy the HMC resources: %d\n",
  12391. ret_code);
  12392. }
  12393. /* shutdown the adminq */
  12394. i40e_shutdown_adminq(hw);
  12395. /* destroy the locks only once, here */
  12396. mutex_destroy(&hw->aq.arq_mutex);
  12397. mutex_destroy(&hw->aq.asq_mutex);
  12398. /* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
  12399. i40e_clear_interrupt_scheme(pf);
  12400. for (i = 0; i < pf->num_alloc_vsi; i++) {
  12401. if (pf->vsi[i]) {
  12402. i40e_vsi_clear_rings(pf->vsi[i]);
  12403. i40e_vsi_clear(pf->vsi[i]);
  12404. pf->vsi[i] = NULL;
  12405. }
  12406. }
  12407. for (i = 0; i < I40E_MAX_VEB; i++) {
  12408. kfree(pf->veb[i]);
  12409. pf->veb[i] = NULL;
  12410. }
  12411. kfree(pf->qp_pile);
  12412. kfree(pf->vsi);
  12413. iounmap(hw->hw_addr);
  12414. kfree(pf);
  12415. pci_release_mem_regions(pdev);
  12416. pci_disable_pcie_error_reporting(pdev);
  12417. pci_disable_device(pdev);
  12418. }
  12419. /**
  12420. * i40e_pci_error_detected - warning that something funky happened in PCI land
  12421. * @pdev: PCI device information struct
  12422. * @error: the type of PCI error
  12423. *
  12424. * Called to warn that something happened and the error handling steps
  12425. * are in progress. Allows the driver to quiesce things, be ready for
  12426. * remediation.
  12427. **/
  12428. static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
  12429. enum pci_channel_state error)
  12430. {
  12431. struct i40e_pf *pf = pci_get_drvdata(pdev);
  12432. dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
  12433. if (!pf) {
  12434. dev_info(&pdev->dev,
  12435. "Cannot recover - error happened during device probe\n");
  12436. return PCI_ERS_RESULT_DISCONNECT;
  12437. }
  12438. /* shutdown all operations */
  12439. if (!test_bit(__I40E_SUSPENDED, pf->state))
  12440. i40e_prep_for_reset(pf, false);
  12441. /* Request a slot reset */
  12442. return PCI_ERS_RESULT_NEED_RESET;
  12443. }
  12444. /**
  12445. * i40e_pci_error_slot_reset - a PCI slot reset just happened
  12446. * @pdev: PCI device information struct
  12447. *
  12448. * Called to find if the driver can work with the device now that
  12449. * the pci slot has been reset. If a basic connection seems good
  12450. * (registers are readable and have sane content) then return a
  12451. * happy little PCI_ERS_RESULT_xxx.
  12452. **/
  12453. static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
  12454. {
  12455. struct i40e_pf *pf = pci_get_drvdata(pdev);
  12456. pci_ers_result_t result;
  12457. int err;
  12458. u32 reg;
  12459. dev_dbg(&pdev->dev, "%s\n", __func__);
  12460. if (pci_enable_device_mem(pdev)) {
  12461. dev_info(&pdev->dev,
  12462. "Cannot re-enable PCI device after reset.\n");
  12463. result = PCI_ERS_RESULT_DISCONNECT;
  12464. } else {
  12465. pci_set_master(pdev);
  12466. pci_restore_state(pdev);
  12467. pci_save_state(pdev);
  12468. pci_wake_from_d3(pdev, false);
  12469. reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
  12470. if (reg == 0)
  12471. result = PCI_ERS_RESULT_RECOVERED;
  12472. else
  12473. result = PCI_ERS_RESULT_DISCONNECT;
  12474. }
  12475. err = pci_cleanup_aer_uncorrect_error_status(pdev);
  12476. if (err) {
  12477. dev_info(&pdev->dev,
  12478. "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
  12479. err);
  12480. /* non-fatal, continue */
  12481. }
  12482. return result;
  12483. }
  12484. /**
  12485. * i40e_pci_error_reset_prepare - prepare device driver for pci reset
  12486. * @pdev: PCI device information struct
  12487. */
  12488. static void i40e_pci_error_reset_prepare(struct pci_dev *pdev)
  12489. {
  12490. struct i40e_pf *pf = pci_get_drvdata(pdev);
  12491. i40e_prep_for_reset(pf, false);
  12492. }
  12493. /**
  12494. * i40e_pci_error_reset_done - pci reset done, device driver reset can begin
  12495. * @pdev: PCI device information struct
  12496. */
  12497. static void i40e_pci_error_reset_done(struct pci_dev *pdev)
  12498. {
  12499. struct i40e_pf *pf = pci_get_drvdata(pdev);
  12500. i40e_reset_and_rebuild(pf, false, false);
  12501. }
  12502. /**
  12503. * i40e_pci_error_resume - restart operations after PCI error recovery
  12504. * @pdev: PCI device information struct
  12505. *
  12506. * Called to allow the driver to bring things back up after PCI error
  12507. * and/or reset recovery has finished.
  12508. **/
  12509. static void i40e_pci_error_resume(struct pci_dev *pdev)
  12510. {
  12511. struct i40e_pf *pf = pci_get_drvdata(pdev);
  12512. dev_dbg(&pdev->dev, "%s\n", __func__);
  12513. if (test_bit(__I40E_SUSPENDED, pf->state))
  12514. return;
  12515. i40e_handle_reset_warning(pf, false);
  12516. }
  12517. /**
  12518. * i40e_enable_mc_magic_wake - enable multicast magic packet wake up
  12519. * using the mac_address_write admin q function
  12520. * @pf: pointer to i40e_pf struct
  12521. **/
  12522. static void i40e_enable_mc_magic_wake(struct i40e_pf *pf)
  12523. {
  12524. struct i40e_hw *hw = &pf->hw;
  12525. i40e_status ret;
  12526. u8 mac_addr[6];
  12527. u16 flags = 0;
  12528. /* Get current MAC address in case it's an LAA */
  12529. if (pf->vsi[pf->lan_vsi] && pf->vsi[pf->lan_vsi]->netdev) {
  12530. ether_addr_copy(mac_addr,
  12531. pf->vsi[pf->lan_vsi]->netdev->dev_addr);
  12532. } else {
  12533. dev_err(&pf->pdev->dev,
  12534. "Failed to retrieve MAC address; using default\n");
  12535. ether_addr_copy(mac_addr, hw->mac.addr);
  12536. }
  12537. /* The FW expects the mac address write cmd to first be called with
  12538. * one of these flags before calling it again with the multicast
  12539. * enable flags.
  12540. */
  12541. flags = I40E_AQC_WRITE_TYPE_LAA_WOL;
  12542. if (hw->func_caps.flex10_enable && hw->partition_id != 1)
  12543. flags = I40E_AQC_WRITE_TYPE_LAA_ONLY;
  12544. ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
  12545. if (ret) {
  12546. dev_err(&pf->pdev->dev,
  12547. "Failed to update MAC address registers; cannot enable Multicast Magic packet wake up");
  12548. return;
  12549. }
  12550. flags = I40E_AQC_MC_MAG_EN
  12551. | I40E_AQC_WOL_PRESERVE_ON_PFR
  12552. | I40E_AQC_WRITE_TYPE_UPDATE_MC_MAG;
  12553. ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
  12554. if (ret)
  12555. dev_err(&pf->pdev->dev,
  12556. "Failed to enable Multicast Magic Packet wake up\n");
  12557. }
  12558. /**
  12559. * i40e_shutdown - PCI callback for shutting down
  12560. * @pdev: PCI device information struct
  12561. **/
  12562. static void i40e_shutdown(struct pci_dev *pdev)
  12563. {
  12564. struct i40e_pf *pf = pci_get_drvdata(pdev);
  12565. struct i40e_hw *hw = &pf->hw;
  12566. set_bit(__I40E_SUSPENDED, pf->state);
  12567. set_bit(__I40E_DOWN, pf->state);
  12568. del_timer_sync(&pf->service_timer);
  12569. cancel_work_sync(&pf->service_task);
  12570. i40e_cloud_filter_exit(pf);
  12571. i40e_fdir_teardown(pf);
  12572. /* Client close must be called explicitly here because the timer
  12573. * has been stopped.
  12574. */
  12575. i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
  12576. if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
  12577. i40e_enable_mc_magic_wake(pf);
  12578. i40e_prep_for_reset(pf, false);
  12579. wr32(hw, I40E_PFPM_APM,
  12580. (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
  12581. wr32(hw, I40E_PFPM_WUFC,
  12582. (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
  12583. i40e_clear_interrupt_scheme(pf);
  12584. if (system_state == SYSTEM_POWER_OFF) {
  12585. pci_wake_from_d3(pdev, pf->wol_en);
  12586. pci_set_power_state(pdev, PCI_D3hot);
  12587. }
  12588. }
  12589. /**
  12590. * i40e_suspend - PM callback for moving to D3
  12591. * @dev: generic device information structure
  12592. **/
  12593. static int __maybe_unused i40e_suspend(struct device *dev)
  12594. {
  12595. struct pci_dev *pdev = to_pci_dev(dev);
  12596. struct i40e_pf *pf = pci_get_drvdata(pdev);
  12597. struct i40e_hw *hw = &pf->hw;
  12598. /* If we're already suspended, then there is nothing to do */
  12599. if (test_and_set_bit(__I40E_SUSPENDED, pf->state))
  12600. return 0;
  12601. set_bit(__I40E_DOWN, pf->state);
  12602. /* Ensure service task will not be running */
  12603. del_timer_sync(&pf->service_timer);
  12604. cancel_work_sync(&pf->service_task);
  12605. /* Client close must be called explicitly here because the timer
  12606. * has been stopped.
  12607. */
  12608. i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
  12609. if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
  12610. i40e_enable_mc_magic_wake(pf);
  12611. /* Since we're going to destroy queues during the
  12612. * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
  12613. * whole section
  12614. */
  12615. rtnl_lock();
  12616. i40e_prep_for_reset(pf, true);
  12617. wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
  12618. wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
  12619. /* Clear the interrupt scheme and release our IRQs so that the system
  12620. * can safely hibernate even when there are a large number of CPUs.
  12621. * Otherwise hibernation might fail when mapping all the vectors back
  12622. * to CPU0.
  12623. */
  12624. i40e_clear_interrupt_scheme(pf);
  12625. rtnl_unlock();
  12626. return 0;
  12627. }
  12628. /**
  12629. * i40e_resume - PM callback for waking up from D3
  12630. * @dev: generic device information structure
  12631. **/
  12632. static int __maybe_unused i40e_resume(struct device *dev)
  12633. {
  12634. struct pci_dev *pdev = to_pci_dev(dev);
  12635. struct i40e_pf *pf = pci_get_drvdata(pdev);
  12636. int err;
  12637. /* If we're not suspended, then there is nothing to do */
  12638. if (!test_bit(__I40E_SUSPENDED, pf->state))
  12639. return 0;
  12640. /* We need to hold the RTNL lock prior to restoring interrupt schemes,
  12641. * since we're going to be restoring queues
  12642. */
  12643. rtnl_lock();
  12644. /* We cleared the interrupt scheme when we suspended, so we need to
  12645. * restore it now to resume device functionality.
  12646. */
  12647. err = i40e_restore_interrupt_scheme(pf);
  12648. if (err) {
  12649. dev_err(&pdev->dev, "Cannot restore interrupt scheme: %d\n",
  12650. err);
  12651. }
  12652. clear_bit(__I40E_DOWN, pf->state);
  12653. i40e_reset_and_rebuild(pf, false, true);
  12654. rtnl_unlock();
  12655. /* Clear suspended state last after everything is recovered */
  12656. clear_bit(__I40E_SUSPENDED, pf->state);
  12657. /* Restart the service task */
  12658. mod_timer(&pf->service_timer,
  12659. round_jiffies(jiffies + pf->service_timer_period));
  12660. return 0;
  12661. }
  12662. static const struct pci_error_handlers i40e_err_handler = {
  12663. .error_detected = i40e_pci_error_detected,
  12664. .slot_reset = i40e_pci_error_slot_reset,
  12665. .reset_prepare = i40e_pci_error_reset_prepare,
  12666. .reset_done = i40e_pci_error_reset_done,
  12667. .resume = i40e_pci_error_resume,
  12668. };
  12669. static SIMPLE_DEV_PM_OPS(i40e_pm_ops, i40e_suspend, i40e_resume);
  12670. static struct pci_driver i40e_driver = {
  12671. .name = i40e_driver_name,
  12672. .id_table = i40e_pci_tbl,
  12673. .probe = i40e_probe,
  12674. .remove = i40e_remove,
  12675. .driver = {
  12676. .pm = &i40e_pm_ops,
  12677. },
  12678. .shutdown = i40e_shutdown,
  12679. .err_handler = &i40e_err_handler,
  12680. .sriov_configure = i40e_pci_sriov_configure,
  12681. };
  12682. /**
  12683. * i40e_init_module - Driver registration routine
  12684. *
  12685. * i40e_init_module is the first routine called when the driver is
  12686. * loaded. All it does is register with the PCI subsystem.
  12687. **/
  12688. static int __init i40e_init_module(void)
  12689. {
  12690. pr_info("%s: %s - version %s\n", i40e_driver_name,
  12691. i40e_driver_string, i40e_driver_version_str);
  12692. pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
  12693. /* There is no need to throttle the number of active tasks because
  12694. * each device limits its own task using a state bit for scheduling
  12695. * the service task, and the device tasks do not interfere with each
  12696. * other, so we don't set a max task limit. We must set WQ_MEM_RECLAIM
  12697. * since we need to be able to guarantee forward progress even under
  12698. * memory pressure.
  12699. */
  12700. i40e_wq = alloc_workqueue("%s", WQ_MEM_RECLAIM, 0, i40e_driver_name);
  12701. if (!i40e_wq) {
  12702. pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
  12703. return -ENOMEM;
  12704. }
  12705. i40e_dbg_init();
  12706. return pci_register_driver(&i40e_driver);
  12707. }
  12708. module_init(i40e_init_module);
  12709. /**
  12710. * i40e_exit_module - Driver exit cleanup routine
  12711. *
  12712. * i40e_exit_module is called just before the driver is removed
  12713. * from memory.
  12714. **/
  12715. static void __exit i40e_exit_module(void)
  12716. {
  12717. pci_unregister_driver(&i40e_driver);
  12718. destroy_workqueue(i40e_wq);
  12719. i40e_dbg_exit();
  12720. }
  12721. module_exit(i40e_exit_module);