i40e_main.c 400 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551
  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. case I40E_VSI_FDIR:
  1615. case I40E_VSI_SRIOV:
  1616. case I40E_VSI_VMDQ2:
  1617. default:
  1618. qcount = num_tc_qps;
  1619. WARN_ON(i != 0);
  1620. break;
  1621. }
  1622. vsi->tc_config.tc_info[i].qoffset = offset;
  1623. vsi->tc_config.tc_info[i].qcount = qcount;
  1624. /* find the next higher power-of-2 of num queue pairs */
  1625. num_qps = qcount;
  1626. pow = 0;
  1627. while (num_qps && (BIT_ULL(pow) < qcount)) {
  1628. pow++;
  1629. num_qps >>= 1;
  1630. }
  1631. vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
  1632. qmap =
  1633. (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
  1634. (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
  1635. offset += qcount;
  1636. } else {
  1637. /* TC is not enabled so set the offset to
  1638. * default queue and allocate one queue
  1639. * for the given TC.
  1640. */
  1641. vsi->tc_config.tc_info[i].qoffset = 0;
  1642. vsi->tc_config.tc_info[i].qcount = 1;
  1643. vsi->tc_config.tc_info[i].netdev_tc = 0;
  1644. qmap = 0;
  1645. }
  1646. ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
  1647. }
  1648. /* Set actual Tx/Rx queue pairs */
  1649. vsi->num_queue_pairs = offset;
  1650. if ((vsi->type == I40E_VSI_MAIN) && (numtc == 1)) {
  1651. if (vsi->req_queue_pairs > 0)
  1652. vsi->num_queue_pairs = vsi->req_queue_pairs;
  1653. else if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  1654. vsi->num_queue_pairs = pf->num_lan_msix;
  1655. }
  1656. /* Scheduler section valid can only be set for ADD VSI */
  1657. if (is_add) {
  1658. sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
  1659. ctxt->info.up_enable_bits = enabled_tc;
  1660. }
  1661. if (vsi->type == I40E_VSI_SRIOV) {
  1662. ctxt->info.mapping_flags |=
  1663. cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
  1664. for (i = 0; i < vsi->num_queue_pairs; i++)
  1665. ctxt->info.queue_mapping[i] =
  1666. cpu_to_le16(vsi->base_queue + i);
  1667. } else {
  1668. ctxt->info.mapping_flags |=
  1669. cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
  1670. ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
  1671. }
  1672. ctxt->info.valid_sections |= cpu_to_le16(sections);
  1673. }
  1674. /**
  1675. * i40e_addr_sync - Callback for dev_(mc|uc)_sync to add address
  1676. * @netdev: the netdevice
  1677. * @addr: address to add
  1678. *
  1679. * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
  1680. * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
  1681. */
  1682. static int i40e_addr_sync(struct net_device *netdev, const u8 *addr)
  1683. {
  1684. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1685. struct i40e_vsi *vsi = np->vsi;
  1686. if (i40e_add_mac_filter(vsi, addr))
  1687. return 0;
  1688. else
  1689. return -ENOMEM;
  1690. }
  1691. /**
  1692. * i40e_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
  1693. * @netdev: the netdevice
  1694. * @addr: address to add
  1695. *
  1696. * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call
  1697. * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
  1698. */
  1699. static int i40e_addr_unsync(struct net_device *netdev, const u8 *addr)
  1700. {
  1701. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1702. struct i40e_vsi *vsi = np->vsi;
  1703. /* Under some circumstances, we might receive a request to delete
  1704. * our own device address from our uc list. Because we store the
  1705. * device address in the VSI's MAC/VLAN filter list, we need to ignore
  1706. * such requests and not delete our device address from this list.
  1707. */
  1708. if (ether_addr_equal(addr, netdev->dev_addr))
  1709. return 0;
  1710. i40e_del_mac_filter(vsi, addr);
  1711. return 0;
  1712. }
  1713. /**
  1714. * i40e_set_rx_mode - NDO callback to set the netdev filters
  1715. * @netdev: network interface device structure
  1716. **/
  1717. static void i40e_set_rx_mode(struct net_device *netdev)
  1718. {
  1719. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1720. struct i40e_vsi *vsi = np->vsi;
  1721. spin_lock_bh(&vsi->mac_filter_hash_lock);
  1722. __dev_uc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
  1723. __dev_mc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
  1724. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  1725. /* check for other flag changes */
  1726. if (vsi->current_netdev_flags != vsi->netdev->flags) {
  1727. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  1728. set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
  1729. }
  1730. }
  1731. /**
  1732. * i40e_undo_del_filter_entries - Undo the changes made to MAC filter entries
  1733. * @vsi: Pointer to VSI struct
  1734. * @from: Pointer to list which contains MAC filter entries - changes to
  1735. * those entries needs to be undone.
  1736. *
  1737. * MAC filter entries from this list were slated for deletion.
  1738. **/
  1739. static void i40e_undo_del_filter_entries(struct i40e_vsi *vsi,
  1740. struct hlist_head *from)
  1741. {
  1742. struct i40e_mac_filter *f;
  1743. struct hlist_node *h;
  1744. hlist_for_each_entry_safe(f, h, from, hlist) {
  1745. u64 key = i40e_addr_to_hkey(f->macaddr);
  1746. /* Move the element back into MAC filter list*/
  1747. hlist_del(&f->hlist);
  1748. hash_add(vsi->mac_filter_hash, &f->hlist, key);
  1749. }
  1750. }
  1751. /**
  1752. * i40e_undo_add_filter_entries - Undo the changes made to MAC filter entries
  1753. * @vsi: Pointer to vsi struct
  1754. * @from: Pointer to list which contains MAC filter entries - changes to
  1755. * those entries needs to be undone.
  1756. *
  1757. * MAC filter entries from this list were slated for addition.
  1758. **/
  1759. static void i40e_undo_add_filter_entries(struct i40e_vsi *vsi,
  1760. struct hlist_head *from)
  1761. {
  1762. struct i40e_new_mac_filter *new;
  1763. struct hlist_node *h;
  1764. hlist_for_each_entry_safe(new, h, from, hlist) {
  1765. /* We can simply free the wrapper structure */
  1766. hlist_del(&new->hlist);
  1767. kfree(new);
  1768. }
  1769. }
  1770. /**
  1771. * i40e_next_entry - Get the next non-broadcast filter from a list
  1772. * @next: pointer to filter in list
  1773. *
  1774. * Returns the next non-broadcast filter in the list. Required so that we
  1775. * ignore broadcast filters within the list, since these are not handled via
  1776. * the normal firmware update path.
  1777. */
  1778. static
  1779. struct i40e_new_mac_filter *i40e_next_filter(struct i40e_new_mac_filter *next)
  1780. {
  1781. hlist_for_each_entry_continue(next, hlist) {
  1782. if (!is_broadcast_ether_addr(next->f->macaddr))
  1783. return next;
  1784. }
  1785. return NULL;
  1786. }
  1787. /**
  1788. * i40e_update_filter_state - Update filter state based on return data
  1789. * from firmware
  1790. * @count: Number of filters added
  1791. * @add_list: return data from fw
  1792. * @add_head: pointer to first filter in current batch
  1793. *
  1794. * MAC filter entries from list were slated to be added to device. Returns
  1795. * number of successful filters. Note that 0 does NOT mean success!
  1796. **/
  1797. static int
  1798. i40e_update_filter_state(int count,
  1799. struct i40e_aqc_add_macvlan_element_data *add_list,
  1800. struct i40e_new_mac_filter *add_head)
  1801. {
  1802. int retval = 0;
  1803. int i;
  1804. for (i = 0; i < count; i++) {
  1805. /* Always check status of each filter. We don't need to check
  1806. * the firmware return status because we pre-set the filter
  1807. * status to I40E_AQC_MM_ERR_NO_RES when sending the filter
  1808. * request to the adminq. Thus, if it no longer matches then
  1809. * we know the filter is active.
  1810. */
  1811. if (add_list[i].match_method == I40E_AQC_MM_ERR_NO_RES) {
  1812. add_head->state = I40E_FILTER_FAILED;
  1813. } else {
  1814. add_head->state = I40E_FILTER_ACTIVE;
  1815. retval++;
  1816. }
  1817. add_head = i40e_next_filter(add_head);
  1818. if (!add_head)
  1819. break;
  1820. }
  1821. return retval;
  1822. }
  1823. /**
  1824. * i40e_aqc_del_filters - Request firmware to delete a set of filters
  1825. * @vsi: ptr to the VSI
  1826. * @vsi_name: name to display in messages
  1827. * @list: the list of filters to send to firmware
  1828. * @num_del: the number of filters to delete
  1829. * @retval: Set to -EIO on failure to delete
  1830. *
  1831. * Send a request to firmware via AdminQ to delete a set of filters. Uses
  1832. * *retval instead of a return value so that success does not force ret_val to
  1833. * be set to 0. This ensures that a sequence of calls to this function
  1834. * preserve the previous value of *retval on successful delete.
  1835. */
  1836. static
  1837. void i40e_aqc_del_filters(struct i40e_vsi *vsi, const char *vsi_name,
  1838. struct i40e_aqc_remove_macvlan_element_data *list,
  1839. int num_del, int *retval)
  1840. {
  1841. struct i40e_hw *hw = &vsi->back->hw;
  1842. i40e_status aq_ret;
  1843. int aq_err;
  1844. aq_ret = i40e_aq_remove_macvlan(hw, vsi->seid, list, num_del, NULL);
  1845. aq_err = hw->aq.asq_last_status;
  1846. /* Explicitly ignore and do not report when firmware returns ENOENT */
  1847. if (aq_ret && !(aq_err == I40E_AQ_RC_ENOENT)) {
  1848. *retval = -EIO;
  1849. dev_info(&vsi->back->pdev->dev,
  1850. "ignoring delete macvlan error on %s, err %s, aq_err %s\n",
  1851. vsi_name, i40e_stat_str(hw, aq_ret),
  1852. i40e_aq_str(hw, aq_err));
  1853. }
  1854. }
  1855. /**
  1856. * i40e_aqc_add_filters - Request firmware to add a set of filters
  1857. * @vsi: ptr to the VSI
  1858. * @vsi_name: name to display in messages
  1859. * @list: the list of filters to send to firmware
  1860. * @add_head: Position in the add hlist
  1861. * @num_add: the number of filters to add
  1862. *
  1863. * Send a request to firmware via AdminQ to add a chunk of filters. Will set
  1864. * __I40E_VSI_OVERFLOW_PROMISC bit in vsi->state if the firmware has run out of
  1865. * space for more filters.
  1866. */
  1867. static
  1868. void i40e_aqc_add_filters(struct i40e_vsi *vsi, const char *vsi_name,
  1869. struct i40e_aqc_add_macvlan_element_data *list,
  1870. struct i40e_new_mac_filter *add_head,
  1871. int num_add)
  1872. {
  1873. struct i40e_hw *hw = &vsi->back->hw;
  1874. int aq_err, fcnt;
  1875. i40e_aq_add_macvlan(hw, vsi->seid, list, num_add, NULL);
  1876. aq_err = hw->aq.asq_last_status;
  1877. fcnt = i40e_update_filter_state(num_add, list, add_head);
  1878. if (fcnt != num_add) {
  1879. set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
  1880. dev_warn(&vsi->back->pdev->dev,
  1881. "Error %s adding RX filters on %s, promiscuous mode forced on\n",
  1882. i40e_aq_str(hw, aq_err),
  1883. vsi_name);
  1884. }
  1885. }
  1886. /**
  1887. * i40e_aqc_broadcast_filter - Set promiscuous broadcast flags
  1888. * @vsi: pointer to the VSI
  1889. * @vsi_name: the VSI name
  1890. * @f: filter data
  1891. *
  1892. * This function sets or clears the promiscuous broadcast flags for VLAN
  1893. * filters in order to properly receive broadcast frames. Assumes that only
  1894. * broadcast filters are passed.
  1895. *
  1896. * Returns status indicating success or failure;
  1897. **/
  1898. static i40e_status
  1899. i40e_aqc_broadcast_filter(struct i40e_vsi *vsi, const char *vsi_name,
  1900. struct i40e_mac_filter *f)
  1901. {
  1902. bool enable = f->state == I40E_FILTER_NEW;
  1903. struct i40e_hw *hw = &vsi->back->hw;
  1904. i40e_status aq_ret;
  1905. if (f->vlan == I40E_VLAN_ANY) {
  1906. aq_ret = i40e_aq_set_vsi_broadcast(hw,
  1907. vsi->seid,
  1908. enable,
  1909. NULL);
  1910. } else {
  1911. aq_ret = i40e_aq_set_vsi_bc_promisc_on_vlan(hw,
  1912. vsi->seid,
  1913. enable,
  1914. f->vlan,
  1915. NULL);
  1916. }
  1917. if (aq_ret) {
  1918. set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
  1919. dev_warn(&vsi->back->pdev->dev,
  1920. "Error %s, forcing overflow promiscuous on %s\n",
  1921. i40e_aq_str(hw, hw->aq.asq_last_status),
  1922. vsi_name);
  1923. }
  1924. return aq_ret;
  1925. }
  1926. /**
  1927. * i40e_set_promiscuous - set promiscuous mode
  1928. * @pf: board private structure
  1929. * @promisc: promisc on or off
  1930. *
  1931. * There are different ways of setting promiscuous mode on a PF depending on
  1932. * what state/environment we're in. This identifies and sets it appropriately.
  1933. * Returns 0 on success.
  1934. **/
  1935. static int i40e_set_promiscuous(struct i40e_pf *pf, bool promisc)
  1936. {
  1937. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  1938. struct i40e_hw *hw = &pf->hw;
  1939. i40e_status aq_ret;
  1940. if (vsi->type == I40E_VSI_MAIN &&
  1941. pf->lan_veb != I40E_NO_VEB &&
  1942. !(pf->flags & I40E_FLAG_MFP_ENABLED)) {
  1943. /* set defport ON for Main VSI instead of true promisc
  1944. * this way we will get all unicast/multicast and VLAN
  1945. * promisc behavior but will not get VF or VMDq traffic
  1946. * replicated on the Main VSI.
  1947. */
  1948. if (promisc)
  1949. aq_ret = i40e_aq_set_default_vsi(hw,
  1950. vsi->seid,
  1951. NULL);
  1952. else
  1953. aq_ret = i40e_aq_clear_default_vsi(hw,
  1954. vsi->seid,
  1955. NULL);
  1956. if (aq_ret) {
  1957. dev_info(&pf->pdev->dev,
  1958. "Set default VSI failed, err %s, aq_err %s\n",
  1959. i40e_stat_str(hw, aq_ret),
  1960. i40e_aq_str(hw, hw->aq.asq_last_status));
  1961. }
  1962. } else {
  1963. aq_ret = i40e_aq_set_vsi_unicast_promiscuous(
  1964. hw,
  1965. vsi->seid,
  1966. promisc, NULL,
  1967. true);
  1968. if (aq_ret) {
  1969. dev_info(&pf->pdev->dev,
  1970. "set unicast promisc failed, err %s, aq_err %s\n",
  1971. i40e_stat_str(hw, aq_ret),
  1972. i40e_aq_str(hw, hw->aq.asq_last_status));
  1973. }
  1974. aq_ret = i40e_aq_set_vsi_multicast_promiscuous(
  1975. hw,
  1976. vsi->seid,
  1977. promisc, NULL);
  1978. if (aq_ret) {
  1979. dev_info(&pf->pdev->dev,
  1980. "set multicast promisc failed, err %s, aq_err %s\n",
  1981. i40e_stat_str(hw, aq_ret),
  1982. i40e_aq_str(hw, hw->aq.asq_last_status));
  1983. }
  1984. }
  1985. if (!aq_ret)
  1986. pf->cur_promisc = promisc;
  1987. return aq_ret;
  1988. }
  1989. /**
  1990. * i40e_sync_vsi_filters - Update the VSI filter list to the HW
  1991. * @vsi: ptr to the VSI
  1992. *
  1993. * Push any outstanding VSI filter changes through the AdminQ.
  1994. *
  1995. * Returns 0 or error value
  1996. **/
  1997. int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
  1998. {
  1999. struct hlist_head tmp_add_list, tmp_del_list;
  2000. struct i40e_mac_filter *f;
  2001. struct i40e_new_mac_filter *new, *add_head = NULL;
  2002. struct i40e_hw *hw = &vsi->back->hw;
  2003. bool old_overflow, new_overflow;
  2004. unsigned int failed_filters = 0;
  2005. unsigned int vlan_filters = 0;
  2006. char vsi_name[16] = "PF";
  2007. int filter_list_len = 0;
  2008. i40e_status aq_ret = 0;
  2009. u32 changed_flags = 0;
  2010. struct hlist_node *h;
  2011. struct i40e_pf *pf;
  2012. int num_add = 0;
  2013. int num_del = 0;
  2014. int retval = 0;
  2015. u16 cmd_flags;
  2016. int list_size;
  2017. int bkt;
  2018. /* empty array typed pointers, kcalloc later */
  2019. struct i40e_aqc_add_macvlan_element_data *add_list;
  2020. struct i40e_aqc_remove_macvlan_element_data *del_list;
  2021. while (test_and_set_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state))
  2022. usleep_range(1000, 2000);
  2023. pf = vsi->back;
  2024. old_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
  2025. if (vsi->netdev) {
  2026. changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
  2027. vsi->current_netdev_flags = vsi->netdev->flags;
  2028. }
  2029. INIT_HLIST_HEAD(&tmp_add_list);
  2030. INIT_HLIST_HEAD(&tmp_del_list);
  2031. if (vsi->type == I40E_VSI_SRIOV)
  2032. snprintf(vsi_name, sizeof(vsi_name) - 1, "VF %d", vsi->vf_id);
  2033. else if (vsi->type != I40E_VSI_MAIN)
  2034. snprintf(vsi_name, sizeof(vsi_name) - 1, "vsi %d", vsi->seid);
  2035. if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
  2036. vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
  2037. spin_lock_bh(&vsi->mac_filter_hash_lock);
  2038. /* Create a list of filters to delete. */
  2039. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  2040. if (f->state == I40E_FILTER_REMOVE) {
  2041. /* Move the element into temporary del_list */
  2042. hash_del(&f->hlist);
  2043. hlist_add_head(&f->hlist, &tmp_del_list);
  2044. /* Avoid counting removed filters */
  2045. continue;
  2046. }
  2047. if (f->state == I40E_FILTER_NEW) {
  2048. /* Create a temporary i40e_new_mac_filter */
  2049. new = kzalloc(sizeof(*new), GFP_ATOMIC);
  2050. if (!new)
  2051. goto err_no_memory_locked;
  2052. /* Store pointer to the real filter */
  2053. new->f = f;
  2054. new->state = f->state;
  2055. /* Add it to the hash list */
  2056. hlist_add_head(&new->hlist, &tmp_add_list);
  2057. }
  2058. /* Count the number of active (current and new) VLAN
  2059. * filters we have now. Does not count filters which
  2060. * are marked for deletion.
  2061. */
  2062. if (f->vlan > 0)
  2063. vlan_filters++;
  2064. }
  2065. retval = i40e_correct_mac_vlan_filters(vsi,
  2066. &tmp_add_list,
  2067. &tmp_del_list,
  2068. vlan_filters);
  2069. if (retval)
  2070. goto err_no_memory_locked;
  2071. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2072. }
  2073. /* Now process 'del_list' outside the lock */
  2074. if (!hlist_empty(&tmp_del_list)) {
  2075. filter_list_len = hw->aq.asq_buf_size /
  2076. sizeof(struct i40e_aqc_remove_macvlan_element_data);
  2077. list_size = filter_list_len *
  2078. sizeof(struct i40e_aqc_remove_macvlan_element_data);
  2079. del_list = kzalloc(list_size, GFP_ATOMIC);
  2080. if (!del_list)
  2081. goto err_no_memory;
  2082. hlist_for_each_entry_safe(f, h, &tmp_del_list, hlist) {
  2083. cmd_flags = 0;
  2084. /* handle broadcast filters by updating the broadcast
  2085. * promiscuous flag and release filter list.
  2086. */
  2087. if (is_broadcast_ether_addr(f->macaddr)) {
  2088. i40e_aqc_broadcast_filter(vsi, vsi_name, f);
  2089. hlist_del(&f->hlist);
  2090. kfree(f);
  2091. continue;
  2092. }
  2093. /* add to delete list */
  2094. ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
  2095. if (f->vlan == I40E_VLAN_ANY) {
  2096. del_list[num_del].vlan_tag = 0;
  2097. cmd_flags |= I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
  2098. } else {
  2099. del_list[num_del].vlan_tag =
  2100. cpu_to_le16((u16)(f->vlan));
  2101. }
  2102. cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
  2103. del_list[num_del].flags = cmd_flags;
  2104. num_del++;
  2105. /* flush a full buffer */
  2106. if (num_del == filter_list_len) {
  2107. i40e_aqc_del_filters(vsi, vsi_name, del_list,
  2108. num_del, &retval);
  2109. memset(del_list, 0, list_size);
  2110. num_del = 0;
  2111. }
  2112. /* Release memory for MAC filter entries which were
  2113. * synced up with HW.
  2114. */
  2115. hlist_del(&f->hlist);
  2116. kfree(f);
  2117. }
  2118. if (num_del) {
  2119. i40e_aqc_del_filters(vsi, vsi_name, del_list,
  2120. num_del, &retval);
  2121. }
  2122. kfree(del_list);
  2123. del_list = NULL;
  2124. }
  2125. if (!hlist_empty(&tmp_add_list)) {
  2126. /* Do all the adds now. */
  2127. filter_list_len = hw->aq.asq_buf_size /
  2128. sizeof(struct i40e_aqc_add_macvlan_element_data);
  2129. list_size = filter_list_len *
  2130. sizeof(struct i40e_aqc_add_macvlan_element_data);
  2131. add_list = kzalloc(list_size, GFP_ATOMIC);
  2132. if (!add_list)
  2133. goto err_no_memory;
  2134. num_add = 0;
  2135. hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
  2136. /* handle broadcast filters by updating the broadcast
  2137. * promiscuous flag instead of adding a MAC filter.
  2138. */
  2139. if (is_broadcast_ether_addr(new->f->macaddr)) {
  2140. if (i40e_aqc_broadcast_filter(vsi, vsi_name,
  2141. new->f))
  2142. new->state = I40E_FILTER_FAILED;
  2143. else
  2144. new->state = I40E_FILTER_ACTIVE;
  2145. continue;
  2146. }
  2147. /* add to add array */
  2148. if (num_add == 0)
  2149. add_head = new;
  2150. cmd_flags = 0;
  2151. ether_addr_copy(add_list[num_add].mac_addr,
  2152. new->f->macaddr);
  2153. if (new->f->vlan == I40E_VLAN_ANY) {
  2154. add_list[num_add].vlan_tag = 0;
  2155. cmd_flags |= I40E_AQC_MACVLAN_ADD_IGNORE_VLAN;
  2156. } else {
  2157. add_list[num_add].vlan_tag =
  2158. cpu_to_le16((u16)(new->f->vlan));
  2159. }
  2160. add_list[num_add].queue_number = 0;
  2161. /* set invalid match method for later detection */
  2162. add_list[num_add].match_method = I40E_AQC_MM_ERR_NO_RES;
  2163. cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
  2164. add_list[num_add].flags = cpu_to_le16(cmd_flags);
  2165. num_add++;
  2166. /* flush a full buffer */
  2167. if (num_add == filter_list_len) {
  2168. i40e_aqc_add_filters(vsi, vsi_name, add_list,
  2169. add_head, num_add);
  2170. memset(add_list, 0, list_size);
  2171. num_add = 0;
  2172. }
  2173. }
  2174. if (num_add) {
  2175. i40e_aqc_add_filters(vsi, vsi_name, add_list, add_head,
  2176. num_add);
  2177. }
  2178. /* Now move all of the filters from the temp add list back to
  2179. * the VSI's list.
  2180. */
  2181. spin_lock_bh(&vsi->mac_filter_hash_lock);
  2182. hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
  2183. /* Only update the state if we're still NEW */
  2184. if (new->f->state == I40E_FILTER_NEW)
  2185. new->f->state = new->state;
  2186. hlist_del(&new->hlist);
  2187. kfree(new);
  2188. }
  2189. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2190. kfree(add_list);
  2191. add_list = NULL;
  2192. }
  2193. /* Determine the number of active and failed filters. */
  2194. spin_lock_bh(&vsi->mac_filter_hash_lock);
  2195. vsi->active_filters = 0;
  2196. hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
  2197. if (f->state == I40E_FILTER_ACTIVE)
  2198. vsi->active_filters++;
  2199. else if (f->state == I40E_FILTER_FAILED)
  2200. failed_filters++;
  2201. }
  2202. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2203. /* Check if we are able to exit overflow promiscuous mode. We can
  2204. * safely exit if we didn't just enter, we no longer have any failed
  2205. * filters, and we have reduced filters below the threshold value.
  2206. */
  2207. if (old_overflow && !failed_filters &&
  2208. vsi->active_filters < vsi->promisc_threshold) {
  2209. dev_info(&pf->pdev->dev,
  2210. "filter logjam cleared on %s, leaving overflow promiscuous mode\n",
  2211. vsi_name);
  2212. clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
  2213. vsi->promisc_threshold = 0;
  2214. }
  2215. /* if the VF is not trusted do not do promisc */
  2216. if ((vsi->type == I40E_VSI_SRIOV) && !pf->vf[vsi->vf_id].trusted) {
  2217. clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
  2218. goto out;
  2219. }
  2220. new_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
  2221. /* If we are entering overflow promiscuous, we need to calculate a new
  2222. * threshold for when we are safe to exit
  2223. */
  2224. if (!old_overflow && new_overflow)
  2225. vsi->promisc_threshold = (vsi->active_filters * 3) / 4;
  2226. /* check for changes in promiscuous modes */
  2227. if (changed_flags & IFF_ALLMULTI) {
  2228. bool cur_multipromisc;
  2229. cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
  2230. aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
  2231. vsi->seid,
  2232. cur_multipromisc,
  2233. NULL);
  2234. if (aq_ret) {
  2235. retval = i40e_aq_rc_to_posix(aq_ret,
  2236. hw->aq.asq_last_status);
  2237. dev_info(&pf->pdev->dev,
  2238. "set multi promisc failed on %s, err %s aq_err %s\n",
  2239. vsi_name,
  2240. i40e_stat_str(hw, aq_ret),
  2241. i40e_aq_str(hw, hw->aq.asq_last_status));
  2242. }
  2243. }
  2244. if ((changed_flags & IFF_PROMISC) || old_overflow != new_overflow) {
  2245. bool cur_promisc;
  2246. cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
  2247. new_overflow);
  2248. aq_ret = i40e_set_promiscuous(pf, cur_promisc);
  2249. if (aq_ret) {
  2250. retval = i40e_aq_rc_to_posix(aq_ret,
  2251. hw->aq.asq_last_status);
  2252. dev_info(&pf->pdev->dev,
  2253. "Setting promiscuous %s failed on %s, err %s aq_err %s\n",
  2254. cur_promisc ? "on" : "off",
  2255. vsi_name,
  2256. i40e_stat_str(hw, aq_ret),
  2257. i40e_aq_str(hw, hw->aq.asq_last_status));
  2258. }
  2259. }
  2260. out:
  2261. /* if something went wrong then set the changed flag so we try again */
  2262. if (retval)
  2263. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  2264. clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
  2265. return retval;
  2266. err_no_memory:
  2267. /* Restore elements on the temporary add and delete lists */
  2268. spin_lock_bh(&vsi->mac_filter_hash_lock);
  2269. err_no_memory_locked:
  2270. i40e_undo_del_filter_entries(vsi, &tmp_del_list);
  2271. i40e_undo_add_filter_entries(vsi, &tmp_add_list);
  2272. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2273. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  2274. clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
  2275. return -ENOMEM;
  2276. }
  2277. /**
  2278. * i40e_sync_filters_subtask - Sync the VSI filter list with HW
  2279. * @pf: board private structure
  2280. **/
  2281. static void i40e_sync_filters_subtask(struct i40e_pf *pf)
  2282. {
  2283. int v;
  2284. if (!pf)
  2285. return;
  2286. if (!test_and_clear_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state))
  2287. return;
  2288. for (v = 0; v < pf->num_alloc_vsi; v++) {
  2289. if (pf->vsi[v] &&
  2290. (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED)) {
  2291. int ret = i40e_sync_vsi_filters(pf->vsi[v]);
  2292. if (ret) {
  2293. /* come back and try again later */
  2294. set_bit(__I40E_MACVLAN_SYNC_PENDING,
  2295. pf->state);
  2296. break;
  2297. }
  2298. }
  2299. }
  2300. }
  2301. /**
  2302. * i40e_max_xdp_frame_size - returns the maximum allowed frame size for XDP
  2303. * @vsi: the vsi
  2304. **/
  2305. static int i40e_max_xdp_frame_size(struct i40e_vsi *vsi)
  2306. {
  2307. if (PAGE_SIZE >= 8192 || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
  2308. return I40E_RXBUFFER_2048;
  2309. else
  2310. return I40E_RXBUFFER_3072;
  2311. }
  2312. /**
  2313. * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
  2314. * @netdev: network interface device structure
  2315. * @new_mtu: new value for maximum frame size
  2316. *
  2317. * Returns 0 on success, negative on failure
  2318. **/
  2319. static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
  2320. {
  2321. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2322. struct i40e_vsi *vsi = np->vsi;
  2323. struct i40e_pf *pf = vsi->back;
  2324. if (i40e_enabled_xdp_vsi(vsi)) {
  2325. int frame_size = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
  2326. if (frame_size > i40e_max_xdp_frame_size(vsi))
  2327. return -EINVAL;
  2328. }
  2329. netdev_info(netdev, "changing MTU from %d to %d\n",
  2330. netdev->mtu, new_mtu);
  2331. netdev->mtu = new_mtu;
  2332. if (netif_running(netdev))
  2333. i40e_vsi_reinit_locked(vsi);
  2334. set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
  2335. set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
  2336. return 0;
  2337. }
  2338. /**
  2339. * i40e_ioctl - Access the hwtstamp interface
  2340. * @netdev: network interface device structure
  2341. * @ifr: interface request data
  2342. * @cmd: ioctl command
  2343. **/
  2344. int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
  2345. {
  2346. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2347. struct i40e_pf *pf = np->vsi->back;
  2348. switch (cmd) {
  2349. case SIOCGHWTSTAMP:
  2350. return i40e_ptp_get_ts_config(pf, ifr);
  2351. case SIOCSHWTSTAMP:
  2352. return i40e_ptp_set_ts_config(pf, ifr);
  2353. default:
  2354. return -EOPNOTSUPP;
  2355. }
  2356. }
  2357. /**
  2358. * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
  2359. * @vsi: the vsi being adjusted
  2360. **/
  2361. void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
  2362. {
  2363. struct i40e_vsi_context ctxt;
  2364. i40e_status ret;
  2365. if ((vsi->info.valid_sections &
  2366. cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
  2367. ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
  2368. return; /* already enabled */
  2369. vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
  2370. vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
  2371. I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
  2372. ctxt.seid = vsi->seid;
  2373. ctxt.info = vsi->info;
  2374. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  2375. if (ret) {
  2376. dev_info(&vsi->back->pdev->dev,
  2377. "update vlan stripping failed, err %s aq_err %s\n",
  2378. i40e_stat_str(&vsi->back->hw, ret),
  2379. i40e_aq_str(&vsi->back->hw,
  2380. vsi->back->hw.aq.asq_last_status));
  2381. }
  2382. }
  2383. /**
  2384. * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
  2385. * @vsi: the vsi being adjusted
  2386. **/
  2387. void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
  2388. {
  2389. struct i40e_vsi_context ctxt;
  2390. i40e_status ret;
  2391. if ((vsi->info.valid_sections &
  2392. cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
  2393. ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
  2394. I40E_AQ_VSI_PVLAN_EMOD_MASK))
  2395. return; /* already disabled */
  2396. vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
  2397. vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
  2398. I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
  2399. ctxt.seid = vsi->seid;
  2400. ctxt.info = vsi->info;
  2401. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  2402. if (ret) {
  2403. dev_info(&vsi->back->pdev->dev,
  2404. "update vlan stripping failed, err %s aq_err %s\n",
  2405. i40e_stat_str(&vsi->back->hw, ret),
  2406. i40e_aq_str(&vsi->back->hw,
  2407. vsi->back->hw.aq.asq_last_status));
  2408. }
  2409. }
  2410. /**
  2411. * i40e_add_vlan_all_mac - Add a MAC/VLAN filter for each existing MAC address
  2412. * @vsi: the vsi being configured
  2413. * @vid: vlan id to be added (0 = untagged only , -1 = any)
  2414. *
  2415. * This is a helper function for adding a new MAC/VLAN filter with the
  2416. * specified VLAN for each existing MAC address already in the hash table.
  2417. * This function does *not* perform any accounting to update filters based on
  2418. * VLAN mode.
  2419. *
  2420. * NOTE: this function expects to be called while under the
  2421. * mac_filter_hash_lock
  2422. **/
  2423. int i40e_add_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
  2424. {
  2425. struct i40e_mac_filter *f, *add_f;
  2426. struct hlist_node *h;
  2427. int bkt;
  2428. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  2429. if (f->state == I40E_FILTER_REMOVE)
  2430. continue;
  2431. add_f = i40e_add_filter(vsi, f->macaddr, vid);
  2432. if (!add_f) {
  2433. dev_info(&vsi->back->pdev->dev,
  2434. "Could not add vlan filter %d for %pM\n",
  2435. vid, f->macaddr);
  2436. return -ENOMEM;
  2437. }
  2438. }
  2439. return 0;
  2440. }
  2441. /**
  2442. * i40e_vsi_add_vlan - Add VSI membership for given VLAN
  2443. * @vsi: the VSI being configured
  2444. * @vid: VLAN id to be added
  2445. **/
  2446. int i40e_vsi_add_vlan(struct i40e_vsi *vsi, u16 vid)
  2447. {
  2448. int err;
  2449. if (vsi->info.pvid)
  2450. return -EINVAL;
  2451. /* The network stack will attempt to add VID=0, with the intention to
  2452. * receive priority tagged packets with a VLAN of 0. Our HW receives
  2453. * these packets by default when configured to receive untagged
  2454. * packets, so we don't need to add a filter for this case.
  2455. * Additionally, HW interprets adding a VID=0 filter as meaning to
  2456. * receive *only* tagged traffic and stops receiving untagged traffic.
  2457. * Thus, we do not want to actually add a filter for VID=0
  2458. */
  2459. if (!vid)
  2460. return 0;
  2461. /* Locked once because all functions invoked below iterates list*/
  2462. spin_lock_bh(&vsi->mac_filter_hash_lock);
  2463. err = i40e_add_vlan_all_mac(vsi, vid);
  2464. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2465. if (err)
  2466. return err;
  2467. /* schedule our worker thread which will take care of
  2468. * applying the new filter changes
  2469. */
  2470. i40e_service_event_schedule(vsi->back);
  2471. return 0;
  2472. }
  2473. /**
  2474. * i40e_rm_vlan_all_mac - Remove MAC/VLAN pair for all MAC with the given VLAN
  2475. * @vsi: the vsi being configured
  2476. * @vid: vlan id to be removed (0 = untagged only , -1 = any)
  2477. *
  2478. * This function should be used to remove all VLAN filters which match the
  2479. * given VID. It does not schedule the service event and does not take the
  2480. * mac_filter_hash_lock so it may be combined with other operations under
  2481. * a single invocation of the mac_filter_hash_lock.
  2482. *
  2483. * NOTE: this function expects to be called while under the
  2484. * mac_filter_hash_lock
  2485. */
  2486. void i40e_rm_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
  2487. {
  2488. struct i40e_mac_filter *f;
  2489. struct hlist_node *h;
  2490. int bkt;
  2491. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  2492. if (f->vlan == vid)
  2493. __i40e_del_filter(vsi, f);
  2494. }
  2495. }
  2496. /**
  2497. * i40e_vsi_kill_vlan - Remove VSI membership for given VLAN
  2498. * @vsi: the VSI being configured
  2499. * @vid: VLAN id to be removed
  2500. **/
  2501. void i40e_vsi_kill_vlan(struct i40e_vsi *vsi, u16 vid)
  2502. {
  2503. if (!vid || vsi->info.pvid)
  2504. return;
  2505. spin_lock_bh(&vsi->mac_filter_hash_lock);
  2506. i40e_rm_vlan_all_mac(vsi, vid);
  2507. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2508. /* schedule our worker thread which will take care of
  2509. * applying the new filter changes
  2510. */
  2511. i40e_service_event_schedule(vsi->back);
  2512. }
  2513. /**
  2514. * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
  2515. * @netdev: network interface to be adjusted
  2516. * @proto: unused protocol value
  2517. * @vid: vlan id to be added
  2518. *
  2519. * net_device_ops implementation for adding vlan ids
  2520. **/
  2521. static int i40e_vlan_rx_add_vid(struct net_device *netdev,
  2522. __always_unused __be16 proto, u16 vid)
  2523. {
  2524. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2525. struct i40e_vsi *vsi = np->vsi;
  2526. int ret = 0;
  2527. if (vid >= VLAN_N_VID)
  2528. return -EINVAL;
  2529. ret = i40e_vsi_add_vlan(vsi, vid);
  2530. if (!ret)
  2531. set_bit(vid, vsi->active_vlans);
  2532. return ret;
  2533. }
  2534. /**
  2535. * i40e_vlan_rx_add_vid_up - Add a vlan id filter to HW offload in UP path
  2536. * @netdev: network interface to be adjusted
  2537. * @proto: unused protocol value
  2538. * @vid: vlan id to be added
  2539. **/
  2540. static void i40e_vlan_rx_add_vid_up(struct net_device *netdev,
  2541. __always_unused __be16 proto, u16 vid)
  2542. {
  2543. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2544. struct i40e_vsi *vsi = np->vsi;
  2545. if (vid >= VLAN_N_VID)
  2546. return;
  2547. set_bit(vid, vsi->active_vlans);
  2548. }
  2549. /**
  2550. * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
  2551. * @netdev: network interface to be adjusted
  2552. * @proto: unused protocol value
  2553. * @vid: vlan id to be removed
  2554. *
  2555. * net_device_ops implementation for removing vlan ids
  2556. **/
  2557. static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
  2558. __always_unused __be16 proto, u16 vid)
  2559. {
  2560. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2561. struct i40e_vsi *vsi = np->vsi;
  2562. /* return code is ignored as there is nothing a user
  2563. * can do about failure to remove and a log message was
  2564. * already printed from the other function
  2565. */
  2566. i40e_vsi_kill_vlan(vsi, vid);
  2567. clear_bit(vid, vsi->active_vlans);
  2568. return 0;
  2569. }
  2570. /**
  2571. * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
  2572. * @vsi: the vsi being brought back up
  2573. **/
  2574. static void i40e_restore_vlan(struct i40e_vsi *vsi)
  2575. {
  2576. u16 vid;
  2577. if (!vsi->netdev)
  2578. return;
  2579. if (vsi->netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
  2580. i40e_vlan_stripping_enable(vsi);
  2581. else
  2582. i40e_vlan_stripping_disable(vsi);
  2583. for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
  2584. i40e_vlan_rx_add_vid_up(vsi->netdev, htons(ETH_P_8021Q),
  2585. vid);
  2586. }
  2587. /**
  2588. * i40e_vsi_add_pvid - Add pvid for the VSI
  2589. * @vsi: the vsi being adjusted
  2590. * @vid: the vlan id to set as a PVID
  2591. **/
  2592. int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
  2593. {
  2594. struct i40e_vsi_context ctxt;
  2595. i40e_status ret;
  2596. vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
  2597. vsi->info.pvid = cpu_to_le16(vid);
  2598. vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
  2599. I40E_AQ_VSI_PVLAN_INSERT_PVID |
  2600. I40E_AQ_VSI_PVLAN_EMOD_STR;
  2601. ctxt.seid = vsi->seid;
  2602. ctxt.info = vsi->info;
  2603. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  2604. if (ret) {
  2605. dev_info(&vsi->back->pdev->dev,
  2606. "add pvid failed, err %s aq_err %s\n",
  2607. i40e_stat_str(&vsi->back->hw, ret),
  2608. i40e_aq_str(&vsi->back->hw,
  2609. vsi->back->hw.aq.asq_last_status));
  2610. return -ENOENT;
  2611. }
  2612. return 0;
  2613. }
  2614. /**
  2615. * i40e_vsi_remove_pvid - Remove the pvid from the VSI
  2616. * @vsi: the vsi being adjusted
  2617. *
  2618. * Just use the vlan_rx_register() service to put it back to normal
  2619. **/
  2620. void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
  2621. {
  2622. i40e_vlan_stripping_disable(vsi);
  2623. vsi->info.pvid = 0;
  2624. }
  2625. /**
  2626. * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
  2627. * @vsi: ptr to the VSI
  2628. *
  2629. * If this function returns with an error, then it's possible one or
  2630. * more of the rings is populated (while the rest are not). It is the
  2631. * callers duty to clean those orphaned rings.
  2632. *
  2633. * Return 0 on success, negative on failure
  2634. **/
  2635. static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
  2636. {
  2637. int i, err = 0;
  2638. for (i = 0; i < vsi->num_queue_pairs && !err; i++)
  2639. err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
  2640. if (!i40e_enabled_xdp_vsi(vsi))
  2641. return err;
  2642. for (i = 0; i < vsi->num_queue_pairs && !err; i++)
  2643. err = i40e_setup_tx_descriptors(vsi->xdp_rings[i]);
  2644. return err;
  2645. }
  2646. /**
  2647. * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
  2648. * @vsi: ptr to the VSI
  2649. *
  2650. * Free VSI's transmit software resources
  2651. **/
  2652. static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
  2653. {
  2654. int i;
  2655. if (vsi->tx_rings) {
  2656. for (i = 0; i < vsi->num_queue_pairs; i++)
  2657. if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
  2658. i40e_free_tx_resources(vsi->tx_rings[i]);
  2659. }
  2660. if (vsi->xdp_rings) {
  2661. for (i = 0; i < vsi->num_queue_pairs; i++)
  2662. if (vsi->xdp_rings[i] && vsi->xdp_rings[i]->desc)
  2663. i40e_free_tx_resources(vsi->xdp_rings[i]);
  2664. }
  2665. }
  2666. /**
  2667. * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
  2668. * @vsi: ptr to the VSI
  2669. *
  2670. * If this function returns with an error, then it's possible one or
  2671. * more of the rings is populated (while the rest are not). It is the
  2672. * callers duty to clean those orphaned rings.
  2673. *
  2674. * Return 0 on success, negative on failure
  2675. **/
  2676. static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
  2677. {
  2678. int i, err = 0;
  2679. for (i = 0; i < vsi->num_queue_pairs && !err; i++)
  2680. err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
  2681. return err;
  2682. }
  2683. /**
  2684. * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
  2685. * @vsi: ptr to the VSI
  2686. *
  2687. * Free all receive software resources
  2688. **/
  2689. static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
  2690. {
  2691. int i;
  2692. if (!vsi->rx_rings)
  2693. return;
  2694. for (i = 0; i < vsi->num_queue_pairs; i++)
  2695. if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
  2696. i40e_free_rx_resources(vsi->rx_rings[i]);
  2697. }
  2698. /**
  2699. * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
  2700. * @ring: The Tx ring to configure
  2701. *
  2702. * This enables/disables XPS for a given Tx descriptor ring
  2703. * based on the TCs enabled for the VSI that ring belongs to.
  2704. **/
  2705. static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
  2706. {
  2707. int cpu;
  2708. if (!ring->q_vector || !ring->netdev || ring->ch)
  2709. return;
  2710. /* We only initialize XPS once, so as not to overwrite user settings */
  2711. if (test_and_set_bit(__I40E_TX_XPS_INIT_DONE, ring->state))
  2712. return;
  2713. cpu = cpumask_local_spread(ring->q_vector->v_idx, -1);
  2714. netif_set_xps_queue(ring->netdev, get_cpu_mask(cpu),
  2715. ring->queue_index);
  2716. }
  2717. /**
  2718. * i40e_configure_tx_ring - Configure a transmit ring context and rest
  2719. * @ring: The Tx ring to configure
  2720. *
  2721. * Configure the Tx descriptor ring in the HMC context.
  2722. **/
  2723. static int i40e_configure_tx_ring(struct i40e_ring *ring)
  2724. {
  2725. struct i40e_vsi *vsi = ring->vsi;
  2726. u16 pf_q = vsi->base_queue + ring->queue_index;
  2727. struct i40e_hw *hw = &vsi->back->hw;
  2728. struct i40e_hmc_obj_txq tx_ctx;
  2729. i40e_status err = 0;
  2730. u32 qtx_ctl = 0;
  2731. /* some ATR related tx ring init */
  2732. if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
  2733. ring->atr_sample_rate = vsi->back->atr_sample_rate;
  2734. ring->atr_count = 0;
  2735. } else {
  2736. ring->atr_sample_rate = 0;
  2737. }
  2738. /* configure XPS */
  2739. i40e_config_xps_tx_ring(ring);
  2740. /* clear the context structure first */
  2741. memset(&tx_ctx, 0, sizeof(tx_ctx));
  2742. tx_ctx.new_context = 1;
  2743. tx_ctx.base = (ring->dma / 128);
  2744. tx_ctx.qlen = ring->count;
  2745. tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
  2746. I40E_FLAG_FD_ATR_ENABLED));
  2747. tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
  2748. /* FDIR VSI tx ring can still use RS bit and writebacks */
  2749. if (vsi->type != I40E_VSI_FDIR)
  2750. tx_ctx.head_wb_ena = 1;
  2751. tx_ctx.head_wb_addr = ring->dma +
  2752. (ring->count * sizeof(struct i40e_tx_desc));
  2753. /* As part of VSI creation/update, FW allocates certain
  2754. * Tx arbitration queue sets for each TC enabled for
  2755. * the VSI. The FW returns the handles to these queue
  2756. * sets as part of the response buffer to Add VSI,
  2757. * Update VSI, etc. AQ commands. It is expected that
  2758. * these queue set handles be associated with the Tx
  2759. * queues by the driver as part of the TX queue context
  2760. * initialization. This has to be done regardless of
  2761. * DCB as by default everything is mapped to TC0.
  2762. */
  2763. if (ring->ch)
  2764. tx_ctx.rdylist =
  2765. le16_to_cpu(ring->ch->info.qs_handle[ring->dcb_tc]);
  2766. else
  2767. tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
  2768. tx_ctx.rdylist_act = 0;
  2769. /* clear the context in the HMC */
  2770. err = i40e_clear_lan_tx_queue_context(hw, pf_q);
  2771. if (err) {
  2772. dev_info(&vsi->back->pdev->dev,
  2773. "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
  2774. ring->queue_index, pf_q, err);
  2775. return -ENOMEM;
  2776. }
  2777. /* set the context in the HMC */
  2778. err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
  2779. if (err) {
  2780. dev_info(&vsi->back->pdev->dev,
  2781. "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
  2782. ring->queue_index, pf_q, err);
  2783. return -ENOMEM;
  2784. }
  2785. /* Now associate this queue with this PCI function */
  2786. if (ring->ch) {
  2787. if (ring->ch->type == I40E_VSI_VMDQ2)
  2788. qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
  2789. else
  2790. return -EINVAL;
  2791. qtx_ctl |= (ring->ch->vsi_number <<
  2792. I40E_QTX_CTL_VFVM_INDX_SHIFT) &
  2793. I40E_QTX_CTL_VFVM_INDX_MASK;
  2794. } else {
  2795. if (vsi->type == I40E_VSI_VMDQ2) {
  2796. qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
  2797. qtx_ctl |= ((vsi->id) << I40E_QTX_CTL_VFVM_INDX_SHIFT) &
  2798. I40E_QTX_CTL_VFVM_INDX_MASK;
  2799. } else {
  2800. qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
  2801. }
  2802. }
  2803. qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
  2804. I40E_QTX_CTL_PF_INDX_MASK);
  2805. wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
  2806. i40e_flush(hw);
  2807. /* cache tail off for easier writes later */
  2808. ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
  2809. return 0;
  2810. }
  2811. /**
  2812. * i40e_configure_rx_ring - Configure a receive ring context
  2813. * @ring: The Rx ring to configure
  2814. *
  2815. * Configure the Rx descriptor ring in the HMC context.
  2816. **/
  2817. static int i40e_configure_rx_ring(struct i40e_ring *ring)
  2818. {
  2819. struct i40e_vsi *vsi = ring->vsi;
  2820. u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
  2821. u16 pf_q = vsi->base_queue + ring->queue_index;
  2822. struct i40e_hw *hw = &vsi->back->hw;
  2823. struct i40e_hmc_obj_rxq rx_ctx;
  2824. i40e_status err = 0;
  2825. bitmap_zero(ring->state, __I40E_RING_STATE_NBITS);
  2826. /* clear the context structure first */
  2827. memset(&rx_ctx, 0, sizeof(rx_ctx));
  2828. ring->rx_buf_len = vsi->rx_buf_len;
  2829. rx_ctx.dbuff = DIV_ROUND_UP(ring->rx_buf_len,
  2830. BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
  2831. rx_ctx.base = (ring->dma / 128);
  2832. rx_ctx.qlen = ring->count;
  2833. /* use 32 byte descriptors */
  2834. rx_ctx.dsize = 1;
  2835. /* descriptor type is always zero
  2836. * rx_ctx.dtype = 0;
  2837. */
  2838. rx_ctx.hsplit_0 = 0;
  2839. rx_ctx.rxmax = min_t(u16, vsi->max_frame, chain_len * ring->rx_buf_len);
  2840. if (hw->revision_id == 0)
  2841. rx_ctx.lrxqthresh = 0;
  2842. else
  2843. rx_ctx.lrxqthresh = 1;
  2844. rx_ctx.crcstrip = 1;
  2845. rx_ctx.l2tsel = 1;
  2846. /* this controls whether VLAN is stripped from inner headers */
  2847. rx_ctx.showiv = 0;
  2848. /* set the prefena field to 1 because the manual says to */
  2849. rx_ctx.prefena = 1;
  2850. /* clear the context in the HMC */
  2851. err = i40e_clear_lan_rx_queue_context(hw, pf_q);
  2852. if (err) {
  2853. dev_info(&vsi->back->pdev->dev,
  2854. "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
  2855. ring->queue_index, pf_q, err);
  2856. return -ENOMEM;
  2857. }
  2858. /* set the context in the HMC */
  2859. err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
  2860. if (err) {
  2861. dev_info(&vsi->back->pdev->dev,
  2862. "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
  2863. ring->queue_index, pf_q, err);
  2864. return -ENOMEM;
  2865. }
  2866. /* configure Rx buffer alignment */
  2867. if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
  2868. clear_ring_build_skb_enabled(ring);
  2869. else
  2870. set_ring_build_skb_enabled(ring);
  2871. /* cache tail for quicker writes, and clear the reg before use */
  2872. ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
  2873. writel(0, ring->tail);
  2874. i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
  2875. return 0;
  2876. }
  2877. /**
  2878. * i40e_vsi_configure_tx - Configure the VSI for Tx
  2879. * @vsi: VSI structure describing this set of rings and resources
  2880. *
  2881. * Configure the Tx VSI for operation.
  2882. **/
  2883. static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
  2884. {
  2885. int err = 0;
  2886. u16 i;
  2887. for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
  2888. err = i40e_configure_tx_ring(vsi->tx_rings[i]);
  2889. if (!i40e_enabled_xdp_vsi(vsi))
  2890. return err;
  2891. for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
  2892. err = i40e_configure_tx_ring(vsi->xdp_rings[i]);
  2893. return err;
  2894. }
  2895. /**
  2896. * i40e_vsi_configure_rx - Configure the VSI for Rx
  2897. * @vsi: the VSI being configured
  2898. *
  2899. * Configure the Rx VSI for operation.
  2900. **/
  2901. static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
  2902. {
  2903. int err = 0;
  2904. u16 i;
  2905. if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX)) {
  2906. vsi->max_frame = I40E_MAX_RXBUFFER;
  2907. vsi->rx_buf_len = I40E_RXBUFFER_2048;
  2908. #if (PAGE_SIZE < 8192)
  2909. } else if (!I40E_2K_TOO_SMALL_WITH_PADDING &&
  2910. (vsi->netdev->mtu <= ETH_DATA_LEN)) {
  2911. vsi->max_frame = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
  2912. vsi->rx_buf_len = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
  2913. #endif
  2914. } else {
  2915. vsi->max_frame = I40E_MAX_RXBUFFER;
  2916. vsi->rx_buf_len = (PAGE_SIZE < 8192) ? I40E_RXBUFFER_3072 :
  2917. I40E_RXBUFFER_2048;
  2918. }
  2919. /* set up individual rings */
  2920. for (i = 0; i < vsi->num_queue_pairs && !err; i++)
  2921. err = i40e_configure_rx_ring(vsi->rx_rings[i]);
  2922. return err;
  2923. }
  2924. /**
  2925. * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
  2926. * @vsi: ptr to the VSI
  2927. **/
  2928. static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
  2929. {
  2930. struct i40e_ring *tx_ring, *rx_ring;
  2931. u16 qoffset, qcount;
  2932. int i, n;
  2933. if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
  2934. /* Reset the TC information */
  2935. for (i = 0; i < vsi->num_queue_pairs; i++) {
  2936. rx_ring = vsi->rx_rings[i];
  2937. tx_ring = vsi->tx_rings[i];
  2938. rx_ring->dcb_tc = 0;
  2939. tx_ring->dcb_tc = 0;
  2940. }
  2941. return;
  2942. }
  2943. for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
  2944. if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
  2945. continue;
  2946. qoffset = vsi->tc_config.tc_info[n].qoffset;
  2947. qcount = vsi->tc_config.tc_info[n].qcount;
  2948. for (i = qoffset; i < (qoffset + qcount); i++) {
  2949. rx_ring = vsi->rx_rings[i];
  2950. tx_ring = vsi->tx_rings[i];
  2951. rx_ring->dcb_tc = n;
  2952. tx_ring->dcb_tc = n;
  2953. }
  2954. }
  2955. }
  2956. /**
  2957. * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
  2958. * @vsi: ptr to the VSI
  2959. **/
  2960. static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
  2961. {
  2962. if (vsi->netdev)
  2963. i40e_set_rx_mode(vsi->netdev);
  2964. }
  2965. /**
  2966. * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
  2967. * @vsi: Pointer to the targeted VSI
  2968. *
  2969. * This function replays the hlist on the hw where all the SB Flow Director
  2970. * filters were saved.
  2971. **/
  2972. static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
  2973. {
  2974. struct i40e_fdir_filter *filter;
  2975. struct i40e_pf *pf = vsi->back;
  2976. struct hlist_node *node;
  2977. if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
  2978. return;
  2979. /* Reset FDir counters as we're replaying all existing filters */
  2980. pf->fd_tcp4_filter_cnt = 0;
  2981. pf->fd_udp4_filter_cnt = 0;
  2982. pf->fd_sctp4_filter_cnt = 0;
  2983. pf->fd_ip4_filter_cnt = 0;
  2984. hlist_for_each_entry_safe(filter, node,
  2985. &pf->fdir_filter_list, fdir_node) {
  2986. i40e_add_del_fdir(vsi, filter, true);
  2987. }
  2988. }
  2989. /**
  2990. * i40e_vsi_configure - Set up the VSI for action
  2991. * @vsi: the VSI being configured
  2992. **/
  2993. static int i40e_vsi_configure(struct i40e_vsi *vsi)
  2994. {
  2995. int err;
  2996. i40e_set_vsi_rx_mode(vsi);
  2997. i40e_restore_vlan(vsi);
  2998. i40e_vsi_config_dcb_rings(vsi);
  2999. err = i40e_vsi_configure_tx(vsi);
  3000. if (!err)
  3001. err = i40e_vsi_configure_rx(vsi);
  3002. return err;
  3003. }
  3004. /**
  3005. * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
  3006. * @vsi: the VSI being configured
  3007. **/
  3008. static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
  3009. {
  3010. bool has_xdp = i40e_enabled_xdp_vsi(vsi);
  3011. struct i40e_pf *pf = vsi->back;
  3012. struct i40e_hw *hw = &pf->hw;
  3013. u16 vector;
  3014. int i, q;
  3015. u32 qp;
  3016. /* The interrupt indexing is offset by 1 in the PFINT_ITRn
  3017. * and PFINT_LNKLSTn registers, e.g.:
  3018. * PFINT_ITRn[0..n-1] gets msix-1..msix-n (qpair interrupts)
  3019. */
  3020. qp = vsi->base_queue;
  3021. vector = vsi->base_vector;
  3022. for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
  3023. struct i40e_q_vector *q_vector = vsi->q_vectors[i];
  3024. q_vector->rx.next_update = jiffies + 1;
  3025. q_vector->rx.target_itr =
  3026. ITR_TO_REG(vsi->rx_rings[i]->itr_setting);
  3027. wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
  3028. q_vector->rx.target_itr);
  3029. q_vector->rx.current_itr = q_vector->rx.target_itr;
  3030. q_vector->tx.next_update = jiffies + 1;
  3031. q_vector->tx.target_itr =
  3032. ITR_TO_REG(vsi->tx_rings[i]->itr_setting);
  3033. wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
  3034. q_vector->tx.target_itr);
  3035. q_vector->tx.current_itr = q_vector->tx.target_itr;
  3036. wr32(hw, I40E_PFINT_RATEN(vector - 1),
  3037. i40e_intrl_usec_to_reg(vsi->int_rate_limit));
  3038. /* Linked list for the queuepairs assigned to this vector */
  3039. wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
  3040. for (q = 0; q < q_vector->num_ringpairs; q++) {
  3041. u32 nextqp = has_xdp ? qp + vsi->alloc_queue_pairs : qp;
  3042. u32 val;
  3043. val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
  3044. (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
  3045. (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
  3046. (nextqp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) |
  3047. (I40E_QUEUE_TYPE_TX <<
  3048. I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
  3049. wr32(hw, I40E_QINT_RQCTL(qp), val);
  3050. if (has_xdp) {
  3051. val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  3052. (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
  3053. (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
  3054. (qp << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
  3055. (I40E_QUEUE_TYPE_TX <<
  3056. I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
  3057. wr32(hw, I40E_QINT_TQCTL(nextqp), val);
  3058. }
  3059. val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  3060. (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
  3061. (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
  3062. ((qp + 1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
  3063. (I40E_QUEUE_TYPE_RX <<
  3064. I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
  3065. /* Terminate the linked list */
  3066. if (q == (q_vector->num_ringpairs - 1))
  3067. val |= (I40E_QUEUE_END_OF_LIST <<
  3068. I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
  3069. wr32(hw, I40E_QINT_TQCTL(qp), val);
  3070. qp++;
  3071. }
  3072. }
  3073. i40e_flush(hw);
  3074. }
  3075. /**
  3076. * i40e_enable_misc_int_causes - enable the non-queue interrupts
  3077. * @pf: pointer to private device data structure
  3078. **/
  3079. static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
  3080. {
  3081. struct i40e_hw *hw = &pf->hw;
  3082. u32 val;
  3083. /* clear things first */
  3084. wr32(hw, I40E_PFINT_ICR0_ENA, 0); /* disable all */
  3085. rd32(hw, I40E_PFINT_ICR0); /* read to clear */
  3086. val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK |
  3087. I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK |
  3088. I40E_PFINT_ICR0_ENA_GRST_MASK |
  3089. I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
  3090. I40E_PFINT_ICR0_ENA_GPIO_MASK |
  3091. I40E_PFINT_ICR0_ENA_HMC_ERR_MASK |
  3092. I40E_PFINT_ICR0_ENA_VFLR_MASK |
  3093. I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
  3094. if (pf->flags & I40E_FLAG_IWARP_ENABLED)
  3095. val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
  3096. if (pf->flags & I40E_FLAG_PTP)
  3097. val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
  3098. wr32(hw, I40E_PFINT_ICR0_ENA, val);
  3099. /* SW_ITR_IDX = 0, but don't change INTENA */
  3100. wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
  3101. I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
  3102. /* OTHER_ITR_IDX = 0 */
  3103. wr32(hw, I40E_PFINT_STAT_CTL0, 0);
  3104. }
  3105. /**
  3106. * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
  3107. * @vsi: the VSI being configured
  3108. **/
  3109. static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
  3110. {
  3111. u32 nextqp = i40e_enabled_xdp_vsi(vsi) ? vsi->alloc_queue_pairs : 0;
  3112. struct i40e_q_vector *q_vector = vsi->q_vectors[0];
  3113. struct i40e_pf *pf = vsi->back;
  3114. struct i40e_hw *hw = &pf->hw;
  3115. u32 val;
  3116. /* set the ITR configuration */
  3117. q_vector->rx.next_update = jiffies + 1;
  3118. q_vector->rx.target_itr = ITR_TO_REG(vsi->rx_rings[0]->itr_setting);
  3119. wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.target_itr);
  3120. q_vector->rx.current_itr = q_vector->rx.target_itr;
  3121. q_vector->tx.next_update = jiffies + 1;
  3122. q_vector->tx.target_itr = ITR_TO_REG(vsi->tx_rings[0]->itr_setting);
  3123. wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.target_itr);
  3124. q_vector->tx.current_itr = q_vector->tx.target_itr;
  3125. i40e_enable_misc_int_causes(pf);
  3126. /* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
  3127. wr32(hw, I40E_PFINT_LNKLST0, 0);
  3128. /* Associate the queue pair to the vector and enable the queue int */
  3129. val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
  3130. (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
  3131. (nextqp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
  3132. (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
  3133. wr32(hw, I40E_QINT_RQCTL(0), val);
  3134. if (i40e_enabled_xdp_vsi(vsi)) {
  3135. val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  3136. (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT)|
  3137. (I40E_QUEUE_TYPE_TX
  3138. << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
  3139. wr32(hw, I40E_QINT_TQCTL(nextqp), val);
  3140. }
  3141. val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  3142. (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
  3143. (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
  3144. wr32(hw, I40E_QINT_TQCTL(0), val);
  3145. i40e_flush(hw);
  3146. }
  3147. /**
  3148. * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
  3149. * @pf: board private structure
  3150. **/
  3151. void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
  3152. {
  3153. struct i40e_hw *hw = &pf->hw;
  3154. wr32(hw, I40E_PFINT_DYN_CTL0,
  3155. I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
  3156. i40e_flush(hw);
  3157. }
  3158. /**
  3159. * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
  3160. * @pf: board private structure
  3161. **/
  3162. void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf)
  3163. {
  3164. struct i40e_hw *hw = &pf->hw;
  3165. u32 val;
  3166. val = I40E_PFINT_DYN_CTL0_INTENA_MASK |
  3167. I40E_PFINT_DYN_CTL0_CLEARPBA_MASK |
  3168. (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
  3169. wr32(hw, I40E_PFINT_DYN_CTL0, val);
  3170. i40e_flush(hw);
  3171. }
  3172. /**
  3173. * i40e_msix_clean_rings - MSIX mode Interrupt Handler
  3174. * @irq: interrupt number
  3175. * @data: pointer to a q_vector
  3176. **/
  3177. static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
  3178. {
  3179. struct i40e_q_vector *q_vector = data;
  3180. if (!q_vector->tx.ring && !q_vector->rx.ring)
  3181. return IRQ_HANDLED;
  3182. napi_schedule_irqoff(&q_vector->napi);
  3183. return IRQ_HANDLED;
  3184. }
  3185. /**
  3186. * i40e_irq_affinity_notify - Callback for affinity changes
  3187. * @notify: context as to what irq was changed
  3188. * @mask: the new affinity mask
  3189. *
  3190. * This is a callback function used by the irq_set_affinity_notifier function
  3191. * so that we may register to receive changes to the irq affinity masks.
  3192. **/
  3193. static void i40e_irq_affinity_notify(struct irq_affinity_notify *notify,
  3194. const cpumask_t *mask)
  3195. {
  3196. struct i40e_q_vector *q_vector =
  3197. container_of(notify, struct i40e_q_vector, affinity_notify);
  3198. cpumask_copy(&q_vector->affinity_mask, mask);
  3199. }
  3200. /**
  3201. * i40e_irq_affinity_release - Callback for affinity notifier release
  3202. * @ref: internal core kernel usage
  3203. *
  3204. * This is a callback function used by the irq_set_affinity_notifier function
  3205. * to inform the current notification subscriber that they will no longer
  3206. * receive notifications.
  3207. **/
  3208. static void i40e_irq_affinity_release(struct kref *ref) {}
  3209. /**
  3210. * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
  3211. * @vsi: the VSI being configured
  3212. * @basename: name for the vector
  3213. *
  3214. * Allocates MSI-X vectors and requests interrupts from the kernel.
  3215. **/
  3216. static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
  3217. {
  3218. int q_vectors = vsi->num_q_vectors;
  3219. struct i40e_pf *pf = vsi->back;
  3220. int base = vsi->base_vector;
  3221. int rx_int_idx = 0;
  3222. int tx_int_idx = 0;
  3223. int vector, err;
  3224. int irq_num;
  3225. int cpu;
  3226. for (vector = 0; vector < q_vectors; vector++) {
  3227. struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
  3228. irq_num = pf->msix_entries[base + vector].vector;
  3229. if (q_vector->tx.ring && q_vector->rx.ring) {
  3230. snprintf(q_vector->name, sizeof(q_vector->name) - 1,
  3231. "%s-%s-%d", basename, "TxRx", rx_int_idx++);
  3232. tx_int_idx++;
  3233. } else if (q_vector->rx.ring) {
  3234. snprintf(q_vector->name, sizeof(q_vector->name) - 1,
  3235. "%s-%s-%d", basename, "rx", rx_int_idx++);
  3236. } else if (q_vector->tx.ring) {
  3237. snprintf(q_vector->name, sizeof(q_vector->name) - 1,
  3238. "%s-%s-%d", basename, "tx", tx_int_idx++);
  3239. } else {
  3240. /* skip this unused q_vector */
  3241. continue;
  3242. }
  3243. err = request_irq(irq_num,
  3244. vsi->irq_handler,
  3245. 0,
  3246. q_vector->name,
  3247. q_vector);
  3248. if (err) {
  3249. dev_info(&pf->pdev->dev,
  3250. "MSIX request_irq failed, error: %d\n", err);
  3251. goto free_queue_irqs;
  3252. }
  3253. /* register for affinity change notifications */
  3254. q_vector->affinity_notify.notify = i40e_irq_affinity_notify;
  3255. q_vector->affinity_notify.release = i40e_irq_affinity_release;
  3256. irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
  3257. /* Spread affinity hints out across online CPUs.
  3258. *
  3259. * get_cpu_mask returns a static constant mask with
  3260. * a permanent lifetime so it's ok to pass to
  3261. * irq_set_affinity_hint without making a copy.
  3262. */
  3263. cpu = cpumask_local_spread(q_vector->v_idx, -1);
  3264. irq_set_affinity_hint(irq_num, get_cpu_mask(cpu));
  3265. }
  3266. vsi->irqs_ready = true;
  3267. return 0;
  3268. free_queue_irqs:
  3269. while (vector) {
  3270. vector--;
  3271. irq_num = pf->msix_entries[base + vector].vector;
  3272. irq_set_affinity_notifier(irq_num, NULL);
  3273. irq_set_affinity_hint(irq_num, NULL);
  3274. free_irq(irq_num, &vsi->q_vectors[vector]);
  3275. }
  3276. return err;
  3277. }
  3278. /**
  3279. * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
  3280. * @vsi: the VSI being un-configured
  3281. **/
  3282. static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
  3283. {
  3284. struct i40e_pf *pf = vsi->back;
  3285. struct i40e_hw *hw = &pf->hw;
  3286. int base = vsi->base_vector;
  3287. int i;
  3288. /* disable interrupt causation from each queue */
  3289. for (i = 0; i < vsi->num_queue_pairs; i++) {
  3290. u32 val;
  3291. val = rd32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx));
  3292. val &= ~I40E_QINT_TQCTL_CAUSE_ENA_MASK;
  3293. wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), val);
  3294. val = rd32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx));
  3295. val &= ~I40E_QINT_RQCTL_CAUSE_ENA_MASK;
  3296. wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), val);
  3297. if (!i40e_enabled_xdp_vsi(vsi))
  3298. continue;
  3299. wr32(hw, I40E_QINT_TQCTL(vsi->xdp_rings[i]->reg_idx), 0);
  3300. }
  3301. /* disable each interrupt */
  3302. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  3303. for (i = vsi->base_vector;
  3304. i < (vsi->num_q_vectors + vsi->base_vector); i++)
  3305. wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
  3306. i40e_flush(hw);
  3307. for (i = 0; i < vsi->num_q_vectors; i++)
  3308. synchronize_irq(pf->msix_entries[i + base].vector);
  3309. } else {
  3310. /* Legacy and MSI mode - this stops all interrupt handling */
  3311. wr32(hw, I40E_PFINT_ICR0_ENA, 0);
  3312. wr32(hw, I40E_PFINT_DYN_CTL0, 0);
  3313. i40e_flush(hw);
  3314. synchronize_irq(pf->pdev->irq);
  3315. }
  3316. }
  3317. /**
  3318. * i40e_vsi_enable_irq - Enable IRQ for the given VSI
  3319. * @vsi: the VSI being configured
  3320. **/
  3321. static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
  3322. {
  3323. struct i40e_pf *pf = vsi->back;
  3324. int i;
  3325. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  3326. for (i = 0; i < vsi->num_q_vectors; i++)
  3327. i40e_irq_dynamic_enable(vsi, i);
  3328. } else {
  3329. i40e_irq_dynamic_enable_icr0(pf);
  3330. }
  3331. i40e_flush(&pf->hw);
  3332. return 0;
  3333. }
  3334. /**
  3335. * i40e_free_misc_vector - Free the vector that handles non-queue events
  3336. * @pf: board private structure
  3337. **/
  3338. static void i40e_free_misc_vector(struct i40e_pf *pf)
  3339. {
  3340. /* Disable ICR 0 */
  3341. wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
  3342. i40e_flush(&pf->hw);
  3343. if (pf->flags & I40E_FLAG_MSIX_ENABLED && pf->msix_entries) {
  3344. synchronize_irq(pf->msix_entries[0].vector);
  3345. free_irq(pf->msix_entries[0].vector, pf);
  3346. clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
  3347. }
  3348. }
  3349. /**
  3350. * i40e_intr - MSI/Legacy and non-queue interrupt handler
  3351. * @irq: interrupt number
  3352. * @data: pointer to a q_vector
  3353. *
  3354. * This is the handler used for all MSI/Legacy interrupts, and deals
  3355. * with both queue and non-queue interrupts. This is also used in
  3356. * MSIX mode to handle the non-queue interrupts.
  3357. **/
  3358. static irqreturn_t i40e_intr(int irq, void *data)
  3359. {
  3360. struct i40e_pf *pf = (struct i40e_pf *)data;
  3361. struct i40e_hw *hw = &pf->hw;
  3362. irqreturn_t ret = IRQ_NONE;
  3363. u32 icr0, icr0_remaining;
  3364. u32 val, ena_mask;
  3365. icr0 = rd32(hw, I40E_PFINT_ICR0);
  3366. ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
  3367. /* if sharing a legacy IRQ, we might get called w/o an intr pending */
  3368. if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
  3369. goto enable_intr;
  3370. /* if interrupt but no bits showing, must be SWINT */
  3371. if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
  3372. (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
  3373. pf->sw_int_count++;
  3374. if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
  3375. (icr0 & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
  3376. ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
  3377. dev_dbg(&pf->pdev->dev, "cleared PE_CRITERR\n");
  3378. set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
  3379. }
  3380. /* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
  3381. if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
  3382. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  3383. struct i40e_q_vector *q_vector = vsi->q_vectors[0];
  3384. /* We do not have a way to disarm Queue causes while leaving
  3385. * interrupt enabled for all other causes, ideally
  3386. * interrupt should be disabled while we are in NAPI but
  3387. * this is not a performance path and napi_schedule()
  3388. * can deal with rescheduling.
  3389. */
  3390. if (!test_bit(__I40E_DOWN, pf->state))
  3391. napi_schedule_irqoff(&q_vector->napi);
  3392. }
  3393. if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
  3394. ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
  3395. set_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
  3396. i40e_debug(&pf->hw, I40E_DEBUG_NVM, "AdminQ event\n");
  3397. }
  3398. if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
  3399. ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
  3400. set_bit(__I40E_MDD_EVENT_PENDING, pf->state);
  3401. }
  3402. if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
  3403. ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
  3404. set_bit(__I40E_VFLR_EVENT_PENDING, pf->state);
  3405. }
  3406. if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
  3407. if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
  3408. set_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
  3409. ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
  3410. val = rd32(hw, I40E_GLGEN_RSTAT);
  3411. val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
  3412. >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
  3413. if (val == I40E_RESET_CORER) {
  3414. pf->corer_count++;
  3415. } else if (val == I40E_RESET_GLOBR) {
  3416. pf->globr_count++;
  3417. } else if (val == I40E_RESET_EMPR) {
  3418. pf->empr_count++;
  3419. set_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state);
  3420. }
  3421. }
  3422. if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
  3423. icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
  3424. dev_info(&pf->pdev->dev, "HMC error interrupt\n");
  3425. dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
  3426. rd32(hw, I40E_PFHMC_ERRORINFO),
  3427. rd32(hw, I40E_PFHMC_ERRORDATA));
  3428. }
  3429. if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
  3430. u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
  3431. if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK) {
  3432. icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
  3433. i40e_ptp_tx_hwtstamp(pf);
  3434. }
  3435. }
  3436. /* If a critical error is pending we have no choice but to reset the
  3437. * device.
  3438. * Report and mask out any remaining unexpected interrupts.
  3439. */
  3440. icr0_remaining = icr0 & ena_mask;
  3441. if (icr0_remaining) {
  3442. dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
  3443. icr0_remaining);
  3444. if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
  3445. (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
  3446. (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
  3447. dev_info(&pf->pdev->dev, "device will be reset\n");
  3448. set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
  3449. i40e_service_event_schedule(pf);
  3450. }
  3451. ena_mask &= ~icr0_remaining;
  3452. }
  3453. ret = IRQ_HANDLED;
  3454. enable_intr:
  3455. /* re-enable interrupt causes */
  3456. wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
  3457. if (!test_bit(__I40E_DOWN, pf->state)) {
  3458. i40e_service_event_schedule(pf);
  3459. i40e_irq_dynamic_enable_icr0(pf);
  3460. }
  3461. return ret;
  3462. }
  3463. /**
  3464. * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
  3465. * @tx_ring: tx ring to clean
  3466. * @budget: how many cleans we're allowed
  3467. *
  3468. * Returns true if there's any budget left (e.g. the clean is finished)
  3469. **/
  3470. static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
  3471. {
  3472. struct i40e_vsi *vsi = tx_ring->vsi;
  3473. u16 i = tx_ring->next_to_clean;
  3474. struct i40e_tx_buffer *tx_buf;
  3475. struct i40e_tx_desc *tx_desc;
  3476. tx_buf = &tx_ring->tx_bi[i];
  3477. tx_desc = I40E_TX_DESC(tx_ring, i);
  3478. i -= tx_ring->count;
  3479. do {
  3480. struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
  3481. /* if next_to_watch is not set then there is no work pending */
  3482. if (!eop_desc)
  3483. break;
  3484. /* prevent any other reads prior to eop_desc */
  3485. smp_rmb();
  3486. /* if the descriptor isn't done, no work yet to do */
  3487. if (!(eop_desc->cmd_type_offset_bsz &
  3488. cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
  3489. break;
  3490. /* clear next_to_watch to prevent false hangs */
  3491. tx_buf->next_to_watch = NULL;
  3492. tx_desc->buffer_addr = 0;
  3493. tx_desc->cmd_type_offset_bsz = 0;
  3494. /* move past filter desc */
  3495. tx_buf++;
  3496. tx_desc++;
  3497. i++;
  3498. if (unlikely(!i)) {
  3499. i -= tx_ring->count;
  3500. tx_buf = tx_ring->tx_bi;
  3501. tx_desc = I40E_TX_DESC(tx_ring, 0);
  3502. }
  3503. /* unmap skb header data */
  3504. dma_unmap_single(tx_ring->dev,
  3505. dma_unmap_addr(tx_buf, dma),
  3506. dma_unmap_len(tx_buf, len),
  3507. DMA_TO_DEVICE);
  3508. if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
  3509. kfree(tx_buf->raw_buf);
  3510. tx_buf->raw_buf = NULL;
  3511. tx_buf->tx_flags = 0;
  3512. tx_buf->next_to_watch = NULL;
  3513. dma_unmap_len_set(tx_buf, len, 0);
  3514. tx_desc->buffer_addr = 0;
  3515. tx_desc->cmd_type_offset_bsz = 0;
  3516. /* move us past the eop_desc for start of next FD desc */
  3517. tx_buf++;
  3518. tx_desc++;
  3519. i++;
  3520. if (unlikely(!i)) {
  3521. i -= tx_ring->count;
  3522. tx_buf = tx_ring->tx_bi;
  3523. tx_desc = I40E_TX_DESC(tx_ring, 0);
  3524. }
  3525. /* update budget accounting */
  3526. budget--;
  3527. } while (likely(budget));
  3528. i += tx_ring->count;
  3529. tx_ring->next_to_clean = i;
  3530. if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED)
  3531. i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
  3532. return budget > 0;
  3533. }
  3534. /**
  3535. * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
  3536. * @irq: interrupt number
  3537. * @data: pointer to a q_vector
  3538. **/
  3539. static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
  3540. {
  3541. struct i40e_q_vector *q_vector = data;
  3542. struct i40e_vsi *vsi;
  3543. if (!q_vector->tx.ring)
  3544. return IRQ_HANDLED;
  3545. vsi = q_vector->tx.ring->vsi;
  3546. i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
  3547. return IRQ_HANDLED;
  3548. }
  3549. /**
  3550. * i40e_map_vector_to_qp - Assigns the queue pair to the vector
  3551. * @vsi: the VSI being configured
  3552. * @v_idx: vector index
  3553. * @qp_idx: queue pair index
  3554. **/
  3555. static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
  3556. {
  3557. struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
  3558. struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
  3559. struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
  3560. tx_ring->q_vector = q_vector;
  3561. tx_ring->next = q_vector->tx.ring;
  3562. q_vector->tx.ring = tx_ring;
  3563. q_vector->tx.count++;
  3564. /* Place XDP Tx ring in the same q_vector ring list as regular Tx */
  3565. if (i40e_enabled_xdp_vsi(vsi)) {
  3566. struct i40e_ring *xdp_ring = vsi->xdp_rings[qp_idx];
  3567. xdp_ring->q_vector = q_vector;
  3568. xdp_ring->next = q_vector->tx.ring;
  3569. q_vector->tx.ring = xdp_ring;
  3570. q_vector->tx.count++;
  3571. }
  3572. rx_ring->q_vector = q_vector;
  3573. rx_ring->next = q_vector->rx.ring;
  3574. q_vector->rx.ring = rx_ring;
  3575. q_vector->rx.count++;
  3576. }
  3577. /**
  3578. * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
  3579. * @vsi: the VSI being configured
  3580. *
  3581. * This function maps descriptor rings to the queue-specific vectors
  3582. * we were allotted through the MSI-X enabling code. Ideally, we'd have
  3583. * one vector per queue pair, but on a constrained vector budget, we
  3584. * group the queue pairs as "efficiently" as possible.
  3585. **/
  3586. static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
  3587. {
  3588. int qp_remaining = vsi->num_queue_pairs;
  3589. int q_vectors = vsi->num_q_vectors;
  3590. int num_ringpairs;
  3591. int v_start = 0;
  3592. int qp_idx = 0;
  3593. /* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
  3594. * group them so there are multiple queues per vector.
  3595. * It is also important to go through all the vectors available to be
  3596. * sure that if we don't use all the vectors, that the remaining vectors
  3597. * are cleared. This is especially important when decreasing the
  3598. * number of queues in use.
  3599. */
  3600. for (; v_start < q_vectors; v_start++) {
  3601. struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
  3602. num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
  3603. q_vector->num_ringpairs = num_ringpairs;
  3604. q_vector->reg_idx = q_vector->v_idx + vsi->base_vector - 1;
  3605. q_vector->rx.count = 0;
  3606. q_vector->tx.count = 0;
  3607. q_vector->rx.ring = NULL;
  3608. q_vector->tx.ring = NULL;
  3609. while (num_ringpairs--) {
  3610. i40e_map_vector_to_qp(vsi, v_start, qp_idx);
  3611. qp_idx++;
  3612. qp_remaining--;
  3613. }
  3614. }
  3615. }
  3616. /**
  3617. * i40e_vsi_request_irq - Request IRQ from the OS
  3618. * @vsi: the VSI being configured
  3619. * @basename: name for the vector
  3620. **/
  3621. static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
  3622. {
  3623. struct i40e_pf *pf = vsi->back;
  3624. int err;
  3625. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  3626. err = i40e_vsi_request_irq_msix(vsi, basename);
  3627. else if (pf->flags & I40E_FLAG_MSI_ENABLED)
  3628. err = request_irq(pf->pdev->irq, i40e_intr, 0,
  3629. pf->int_name, pf);
  3630. else
  3631. err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
  3632. pf->int_name, pf);
  3633. if (err)
  3634. dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
  3635. return err;
  3636. }
  3637. #ifdef CONFIG_NET_POLL_CONTROLLER
  3638. /**
  3639. * i40e_netpoll - A Polling 'interrupt' handler
  3640. * @netdev: network interface device structure
  3641. *
  3642. * This is used by netconsole to send skbs without having to re-enable
  3643. * interrupts. It's not called while the normal interrupt routine is executing.
  3644. **/
  3645. static void i40e_netpoll(struct net_device *netdev)
  3646. {
  3647. struct i40e_netdev_priv *np = netdev_priv(netdev);
  3648. struct i40e_vsi *vsi = np->vsi;
  3649. struct i40e_pf *pf = vsi->back;
  3650. int i;
  3651. /* if interface is down do nothing */
  3652. if (test_bit(__I40E_VSI_DOWN, vsi->state))
  3653. return;
  3654. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  3655. for (i = 0; i < vsi->num_q_vectors; i++)
  3656. i40e_msix_clean_rings(0, vsi->q_vectors[i]);
  3657. } else {
  3658. i40e_intr(pf->pdev->irq, netdev);
  3659. }
  3660. }
  3661. #endif
  3662. #define I40E_QTX_ENA_WAIT_COUNT 50
  3663. /**
  3664. * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
  3665. * @pf: the PF being configured
  3666. * @pf_q: the PF queue
  3667. * @enable: enable or disable state of the queue
  3668. *
  3669. * This routine will wait for the given Tx queue of the PF to reach the
  3670. * enabled or disabled state.
  3671. * Returns -ETIMEDOUT in case of failing to reach the requested state after
  3672. * multiple retries; else will return 0 in case of success.
  3673. **/
  3674. static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
  3675. {
  3676. int i;
  3677. u32 tx_reg;
  3678. for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
  3679. tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
  3680. if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
  3681. break;
  3682. usleep_range(10, 20);
  3683. }
  3684. if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
  3685. return -ETIMEDOUT;
  3686. return 0;
  3687. }
  3688. /**
  3689. * i40e_control_tx_q - Start or stop a particular Tx queue
  3690. * @pf: the PF structure
  3691. * @pf_q: the PF queue to configure
  3692. * @enable: start or stop the queue
  3693. *
  3694. * This function enables or disables a single queue. Note that any delay
  3695. * required after the operation is expected to be handled by the caller of
  3696. * this function.
  3697. **/
  3698. static void i40e_control_tx_q(struct i40e_pf *pf, int pf_q, bool enable)
  3699. {
  3700. struct i40e_hw *hw = &pf->hw;
  3701. u32 tx_reg;
  3702. int i;
  3703. /* warn the TX unit of coming changes */
  3704. i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
  3705. if (!enable)
  3706. usleep_range(10, 20);
  3707. for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
  3708. tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
  3709. if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
  3710. ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
  3711. break;
  3712. usleep_range(1000, 2000);
  3713. }
  3714. /* Skip if the queue is already in the requested state */
  3715. if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
  3716. return;
  3717. /* turn on/off the queue */
  3718. if (enable) {
  3719. wr32(hw, I40E_QTX_HEAD(pf_q), 0);
  3720. tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
  3721. } else {
  3722. tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
  3723. }
  3724. wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
  3725. }
  3726. /**
  3727. * i40e_control_wait_tx_q - Start/stop Tx queue and wait for completion
  3728. * @seid: VSI SEID
  3729. * @pf: the PF structure
  3730. * @pf_q: the PF queue to configure
  3731. * @is_xdp: true if the queue is used for XDP
  3732. * @enable: start or stop the queue
  3733. **/
  3734. int i40e_control_wait_tx_q(int seid, struct i40e_pf *pf, int pf_q,
  3735. bool is_xdp, bool enable)
  3736. {
  3737. int ret;
  3738. i40e_control_tx_q(pf, pf_q, enable);
  3739. /* wait for the change to finish */
  3740. ret = i40e_pf_txq_wait(pf, pf_q, enable);
  3741. if (ret) {
  3742. dev_info(&pf->pdev->dev,
  3743. "VSI seid %d %sTx ring %d %sable timeout\n",
  3744. seid, (is_xdp ? "XDP " : ""), pf_q,
  3745. (enable ? "en" : "dis"));
  3746. }
  3747. return ret;
  3748. }
  3749. /**
  3750. * i40e_vsi_control_tx - Start or stop a VSI's rings
  3751. * @vsi: the VSI being configured
  3752. * @enable: start or stop the rings
  3753. **/
  3754. static int i40e_vsi_control_tx(struct i40e_vsi *vsi, bool enable)
  3755. {
  3756. struct i40e_pf *pf = vsi->back;
  3757. int i, pf_q, ret = 0;
  3758. pf_q = vsi->base_queue;
  3759. for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
  3760. ret = i40e_control_wait_tx_q(vsi->seid, pf,
  3761. pf_q,
  3762. false /*is xdp*/, enable);
  3763. if (ret)
  3764. break;
  3765. if (!i40e_enabled_xdp_vsi(vsi))
  3766. continue;
  3767. ret = i40e_control_wait_tx_q(vsi->seid, pf,
  3768. pf_q + vsi->alloc_queue_pairs,
  3769. true /*is xdp*/, enable);
  3770. if (ret)
  3771. break;
  3772. }
  3773. return ret;
  3774. }
  3775. /**
  3776. * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
  3777. * @pf: the PF being configured
  3778. * @pf_q: the PF queue
  3779. * @enable: enable or disable state of the queue
  3780. *
  3781. * This routine will wait for the given Rx queue of the PF to reach the
  3782. * enabled or disabled state.
  3783. * Returns -ETIMEDOUT in case of failing to reach the requested state after
  3784. * multiple retries; else will return 0 in case of success.
  3785. **/
  3786. static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
  3787. {
  3788. int i;
  3789. u32 rx_reg;
  3790. for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
  3791. rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
  3792. if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
  3793. break;
  3794. usleep_range(10, 20);
  3795. }
  3796. if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
  3797. return -ETIMEDOUT;
  3798. return 0;
  3799. }
  3800. /**
  3801. * i40e_control_rx_q - Start or stop a particular Rx queue
  3802. * @pf: the PF structure
  3803. * @pf_q: the PF queue to configure
  3804. * @enable: start or stop the queue
  3805. *
  3806. * This function enables or disables a single queue. Note that
  3807. * any delay required after the operation is expected to be
  3808. * handled by the caller of this function.
  3809. **/
  3810. static void i40e_control_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
  3811. {
  3812. struct i40e_hw *hw = &pf->hw;
  3813. u32 rx_reg;
  3814. int i;
  3815. for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
  3816. rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
  3817. if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
  3818. ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
  3819. break;
  3820. usleep_range(1000, 2000);
  3821. }
  3822. /* Skip if the queue is already in the requested state */
  3823. if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
  3824. return;
  3825. /* turn on/off the queue */
  3826. if (enable)
  3827. rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
  3828. else
  3829. rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
  3830. wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
  3831. }
  3832. /**
  3833. * i40e_control_wait_rx_q
  3834. * @pf: the PF structure
  3835. * @pf_q: queue being configured
  3836. * @enable: start or stop the rings
  3837. *
  3838. * This function enables or disables a single queue along with waiting
  3839. * for the change to finish. The caller of this function should handle
  3840. * the delays needed in the case of disabling queues.
  3841. **/
  3842. int i40e_control_wait_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
  3843. {
  3844. int ret = 0;
  3845. i40e_control_rx_q(pf, pf_q, enable);
  3846. /* wait for the change to finish */
  3847. ret = i40e_pf_rxq_wait(pf, pf_q, enable);
  3848. if (ret)
  3849. return ret;
  3850. return ret;
  3851. }
  3852. /**
  3853. * i40e_vsi_control_rx - Start or stop a VSI's rings
  3854. * @vsi: the VSI being configured
  3855. * @enable: start or stop the rings
  3856. **/
  3857. static int i40e_vsi_control_rx(struct i40e_vsi *vsi, bool enable)
  3858. {
  3859. struct i40e_pf *pf = vsi->back;
  3860. int i, pf_q, ret = 0;
  3861. pf_q = vsi->base_queue;
  3862. for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
  3863. ret = i40e_control_wait_rx_q(pf, pf_q, enable);
  3864. if (ret) {
  3865. dev_info(&pf->pdev->dev,
  3866. "VSI seid %d Rx ring %d %sable timeout\n",
  3867. vsi->seid, pf_q, (enable ? "en" : "dis"));
  3868. break;
  3869. }
  3870. }
  3871. /* Due to HW errata, on Rx disable only, the register can indicate done
  3872. * before it really is. Needs 50ms to be sure
  3873. */
  3874. if (!enable)
  3875. mdelay(50);
  3876. return ret;
  3877. }
  3878. /**
  3879. * i40e_vsi_start_rings - Start a VSI's rings
  3880. * @vsi: the VSI being configured
  3881. **/
  3882. int i40e_vsi_start_rings(struct i40e_vsi *vsi)
  3883. {
  3884. int ret = 0;
  3885. /* do rx first for enable and last for disable */
  3886. ret = i40e_vsi_control_rx(vsi, true);
  3887. if (ret)
  3888. return ret;
  3889. ret = i40e_vsi_control_tx(vsi, true);
  3890. return ret;
  3891. }
  3892. /**
  3893. * i40e_vsi_stop_rings - Stop a VSI's rings
  3894. * @vsi: the VSI being configured
  3895. **/
  3896. void i40e_vsi_stop_rings(struct i40e_vsi *vsi)
  3897. {
  3898. /* When port TX is suspended, don't wait */
  3899. if (test_bit(__I40E_PORT_SUSPENDED, vsi->back->state))
  3900. return i40e_vsi_stop_rings_no_wait(vsi);
  3901. /* do rx first for enable and last for disable
  3902. * Ignore return value, we need to shutdown whatever we can
  3903. */
  3904. i40e_vsi_control_tx(vsi, false);
  3905. i40e_vsi_control_rx(vsi, false);
  3906. }
  3907. /**
  3908. * i40e_vsi_stop_rings_no_wait - Stop a VSI's rings and do not delay
  3909. * @vsi: the VSI being shutdown
  3910. *
  3911. * This function stops all the rings for a VSI but does not delay to verify
  3912. * that rings have been disabled. It is expected that the caller is shutting
  3913. * down multiple VSIs at once and will delay together for all the VSIs after
  3914. * initiating the shutdown. This is particularly useful for shutting down lots
  3915. * of VFs together. Otherwise, a large delay can be incurred while configuring
  3916. * each VSI in serial.
  3917. **/
  3918. void i40e_vsi_stop_rings_no_wait(struct i40e_vsi *vsi)
  3919. {
  3920. struct i40e_pf *pf = vsi->back;
  3921. int i, pf_q;
  3922. pf_q = vsi->base_queue;
  3923. for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
  3924. i40e_control_tx_q(pf, pf_q, false);
  3925. i40e_control_rx_q(pf, pf_q, false);
  3926. }
  3927. }
  3928. /**
  3929. * i40e_vsi_free_irq - Free the irq association with the OS
  3930. * @vsi: the VSI being configured
  3931. **/
  3932. static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
  3933. {
  3934. struct i40e_pf *pf = vsi->back;
  3935. struct i40e_hw *hw = &pf->hw;
  3936. int base = vsi->base_vector;
  3937. u32 val, qp;
  3938. int i;
  3939. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  3940. if (!vsi->q_vectors)
  3941. return;
  3942. if (!vsi->irqs_ready)
  3943. return;
  3944. vsi->irqs_ready = false;
  3945. for (i = 0; i < vsi->num_q_vectors; i++) {
  3946. int irq_num;
  3947. u16 vector;
  3948. vector = i + base;
  3949. irq_num = pf->msix_entries[vector].vector;
  3950. /* free only the irqs that were actually requested */
  3951. if (!vsi->q_vectors[i] ||
  3952. !vsi->q_vectors[i]->num_ringpairs)
  3953. continue;
  3954. /* clear the affinity notifier in the IRQ descriptor */
  3955. irq_set_affinity_notifier(irq_num, NULL);
  3956. /* remove our suggested affinity mask for this IRQ */
  3957. irq_set_affinity_hint(irq_num, NULL);
  3958. synchronize_irq(irq_num);
  3959. free_irq(irq_num, vsi->q_vectors[i]);
  3960. /* Tear down the interrupt queue link list
  3961. *
  3962. * We know that they come in pairs and always
  3963. * the Rx first, then the Tx. To clear the
  3964. * link list, stick the EOL value into the
  3965. * next_q field of the registers.
  3966. */
  3967. val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
  3968. qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
  3969. >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
  3970. val |= I40E_QUEUE_END_OF_LIST
  3971. << I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
  3972. wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
  3973. while (qp != I40E_QUEUE_END_OF_LIST) {
  3974. u32 next;
  3975. val = rd32(hw, I40E_QINT_RQCTL(qp));
  3976. val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
  3977. I40E_QINT_RQCTL_MSIX0_INDX_MASK |
  3978. I40E_QINT_RQCTL_CAUSE_ENA_MASK |
  3979. I40E_QINT_RQCTL_INTEVENT_MASK);
  3980. val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
  3981. I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
  3982. wr32(hw, I40E_QINT_RQCTL(qp), val);
  3983. val = rd32(hw, I40E_QINT_TQCTL(qp));
  3984. next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
  3985. >> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
  3986. val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
  3987. I40E_QINT_TQCTL_MSIX0_INDX_MASK |
  3988. I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  3989. I40E_QINT_TQCTL_INTEVENT_MASK);
  3990. val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
  3991. I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
  3992. wr32(hw, I40E_QINT_TQCTL(qp), val);
  3993. qp = next;
  3994. }
  3995. }
  3996. } else {
  3997. free_irq(pf->pdev->irq, pf);
  3998. val = rd32(hw, I40E_PFINT_LNKLST0);
  3999. qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
  4000. >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
  4001. val |= I40E_QUEUE_END_OF_LIST
  4002. << I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
  4003. wr32(hw, I40E_PFINT_LNKLST0, val);
  4004. val = rd32(hw, I40E_QINT_RQCTL(qp));
  4005. val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
  4006. I40E_QINT_RQCTL_MSIX0_INDX_MASK |
  4007. I40E_QINT_RQCTL_CAUSE_ENA_MASK |
  4008. I40E_QINT_RQCTL_INTEVENT_MASK);
  4009. val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
  4010. I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
  4011. wr32(hw, I40E_QINT_RQCTL(qp), val);
  4012. val = rd32(hw, I40E_QINT_TQCTL(qp));
  4013. val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
  4014. I40E_QINT_TQCTL_MSIX0_INDX_MASK |
  4015. I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  4016. I40E_QINT_TQCTL_INTEVENT_MASK);
  4017. val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
  4018. I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
  4019. wr32(hw, I40E_QINT_TQCTL(qp), val);
  4020. }
  4021. }
  4022. /**
  4023. * i40e_free_q_vector - Free memory allocated for specific interrupt vector
  4024. * @vsi: the VSI being configured
  4025. * @v_idx: Index of vector to be freed
  4026. *
  4027. * This function frees the memory allocated to the q_vector. In addition if
  4028. * NAPI is enabled it will delete any references to the NAPI struct prior
  4029. * to freeing the q_vector.
  4030. **/
  4031. static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
  4032. {
  4033. struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
  4034. struct i40e_ring *ring;
  4035. if (!q_vector)
  4036. return;
  4037. /* disassociate q_vector from rings */
  4038. i40e_for_each_ring(ring, q_vector->tx)
  4039. ring->q_vector = NULL;
  4040. i40e_for_each_ring(ring, q_vector->rx)
  4041. ring->q_vector = NULL;
  4042. /* only VSI w/ an associated netdev is set up w/ NAPI */
  4043. if (vsi->netdev)
  4044. netif_napi_del(&q_vector->napi);
  4045. vsi->q_vectors[v_idx] = NULL;
  4046. kfree_rcu(q_vector, rcu);
  4047. }
  4048. /**
  4049. * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
  4050. * @vsi: the VSI being un-configured
  4051. *
  4052. * This frees the memory allocated to the q_vectors and
  4053. * deletes references to the NAPI struct.
  4054. **/
  4055. static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
  4056. {
  4057. int v_idx;
  4058. for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
  4059. i40e_free_q_vector(vsi, v_idx);
  4060. }
  4061. /**
  4062. * i40e_reset_interrupt_capability - Disable interrupt setup in OS
  4063. * @pf: board private structure
  4064. **/
  4065. static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
  4066. {
  4067. /* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
  4068. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  4069. pci_disable_msix(pf->pdev);
  4070. kfree(pf->msix_entries);
  4071. pf->msix_entries = NULL;
  4072. kfree(pf->irq_pile);
  4073. pf->irq_pile = NULL;
  4074. } else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
  4075. pci_disable_msi(pf->pdev);
  4076. }
  4077. pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
  4078. }
  4079. /**
  4080. * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
  4081. * @pf: board private structure
  4082. *
  4083. * We go through and clear interrupt specific resources and reset the structure
  4084. * to pre-load conditions
  4085. **/
  4086. static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
  4087. {
  4088. int i;
  4089. i40e_free_misc_vector(pf);
  4090. i40e_put_lump(pf->irq_pile, pf->iwarp_base_vector,
  4091. I40E_IWARP_IRQ_PILE_ID);
  4092. i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
  4093. for (i = 0; i < pf->num_alloc_vsi; i++)
  4094. if (pf->vsi[i])
  4095. i40e_vsi_free_q_vectors(pf->vsi[i]);
  4096. i40e_reset_interrupt_capability(pf);
  4097. }
  4098. /**
  4099. * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
  4100. * @vsi: the VSI being configured
  4101. **/
  4102. static void i40e_napi_enable_all(struct i40e_vsi *vsi)
  4103. {
  4104. int q_idx;
  4105. if (!vsi->netdev)
  4106. return;
  4107. for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
  4108. struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
  4109. if (q_vector->rx.ring || q_vector->tx.ring)
  4110. napi_enable(&q_vector->napi);
  4111. }
  4112. }
  4113. /**
  4114. * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
  4115. * @vsi: the VSI being configured
  4116. **/
  4117. static void i40e_napi_disable_all(struct i40e_vsi *vsi)
  4118. {
  4119. int q_idx;
  4120. if (!vsi->netdev)
  4121. return;
  4122. for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
  4123. struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
  4124. if (q_vector->rx.ring || q_vector->tx.ring)
  4125. napi_disable(&q_vector->napi);
  4126. }
  4127. }
  4128. /**
  4129. * i40e_vsi_close - Shut down a VSI
  4130. * @vsi: the vsi to be quelled
  4131. **/
  4132. static void i40e_vsi_close(struct i40e_vsi *vsi)
  4133. {
  4134. struct i40e_pf *pf = vsi->back;
  4135. if (!test_and_set_bit(__I40E_VSI_DOWN, vsi->state))
  4136. i40e_down(vsi);
  4137. i40e_vsi_free_irq(vsi);
  4138. i40e_vsi_free_tx_resources(vsi);
  4139. i40e_vsi_free_rx_resources(vsi);
  4140. vsi->current_netdev_flags = 0;
  4141. set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
  4142. if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
  4143. set_bit(__I40E_CLIENT_RESET, pf->state);
  4144. }
  4145. /**
  4146. * i40e_quiesce_vsi - Pause a given VSI
  4147. * @vsi: the VSI being paused
  4148. **/
  4149. static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
  4150. {
  4151. if (test_bit(__I40E_VSI_DOWN, vsi->state))
  4152. return;
  4153. set_bit(__I40E_VSI_NEEDS_RESTART, vsi->state);
  4154. if (vsi->netdev && netif_running(vsi->netdev))
  4155. vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
  4156. else
  4157. i40e_vsi_close(vsi);
  4158. }
  4159. /**
  4160. * i40e_unquiesce_vsi - Resume a given VSI
  4161. * @vsi: the VSI being resumed
  4162. **/
  4163. static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
  4164. {
  4165. if (!test_and_clear_bit(__I40E_VSI_NEEDS_RESTART, vsi->state))
  4166. return;
  4167. if (vsi->netdev && netif_running(vsi->netdev))
  4168. vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
  4169. else
  4170. i40e_vsi_open(vsi); /* this clears the DOWN bit */
  4171. }
  4172. /**
  4173. * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
  4174. * @pf: the PF
  4175. **/
  4176. static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
  4177. {
  4178. int v;
  4179. for (v = 0; v < pf->num_alloc_vsi; v++) {
  4180. if (pf->vsi[v])
  4181. i40e_quiesce_vsi(pf->vsi[v]);
  4182. }
  4183. }
  4184. /**
  4185. * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
  4186. * @pf: the PF
  4187. **/
  4188. static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
  4189. {
  4190. int v;
  4191. for (v = 0; v < pf->num_alloc_vsi; v++) {
  4192. if (pf->vsi[v])
  4193. i40e_unquiesce_vsi(pf->vsi[v]);
  4194. }
  4195. }
  4196. /**
  4197. * i40e_vsi_wait_queues_disabled - Wait for VSI's queues to be disabled
  4198. * @vsi: the VSI being configured
  4199. *
  4200. * Wait until all queues on a given VSI have been disabled.
  4201. **/
  4202. int i40e_vsi_wait_queues_disabled(struct i40e_vsi *vsi)
  4203. {
  4204. struct i40e_pf *pf = vsi->back;
  4205. int i, pf_q, ret;
  4206. pf_q = vsi->base_queue;
  4207. for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
  4208. /* Check and wait for the Tx queue */
  4209. ret = i40e_pf_txq_wait(pf, pf_q, false);
  4210. if (ret) {
  4211. dev_info(&pf->pdev->dev,
  4212. "VSI seid %d Tx ring %d disable timeout\n",
  4213. vsi->seid, pf_q);
  4214. return ret;
  4215. }
  4216. if (!i40e_enabled_xdp_vsi(vsi))
  4217. goto wait_rx;
  4218. /* Check and wait for the XDP Tx queue */
  4219. ret = i40e_pf_txq_wait(pf, pf_q + vsi->alloc_queue_pairs,
  4220. false);
  4221. if (ret) {
  4222. dev_info(&pf->pdev->dev,
  4223. "VSI seid %d XDP Tx ring %d disable timeout\n",
  4224. vsi->seid, pf_q);
  4225. return ret;
  4226. }
  4227. wait_rx:
  4228. /* Check and wait for the Rx queue */
  4229. ret = i40e_pf_rxq_wait(pf, pf_q, false);
  4230. if (ret) {
  4231. dev_info(&pf->pdev->dev,
  4232. "VSI seid %d Rx ring %d disable timeout\n",
  4233. vsi->seid, pf_q);
  4234. return ret;
  4235. }
  4236. }
  4237. return 0;
  4238. }
  4239. #ifdef CONFIG_I40E_DCB
  4240. /**
  4241. * i40e_pf_wait_queues_disabled - Wait for all queues of PF VSIs to be disabled
  4242. * @pf: the PF
  4243. *
  4244. * This function waits for the queues to be in disabled state for all the
  4245. * VSIs that are managed by this PF.
  4246. **/
  4247. static int i40e_pf_wait_queues_disabled(struct i40e_pf *pf)
  4248. {
  4249. int v, ret = 0;
  4250. for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
  4251. if (pf->vsi[v]) {
  4252. ret = i40e_vsi_wait_queues_disabled(pf->vsi[v]);
  4253. if (ret)
  4254. break;
  4255. }
  4256. }
  4257. return ret;
  4258. }
  4259. #endif
  4260. /**
  4261. * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
  4262. * @pf: pointer to PF
  4263. *
  4264. * Get TC map for ISCSI PF type that will include iSCSI TC
  4265. * and LAN TC.
  4266. **/
  4267. static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
  4268. {
  4269. struct i40e_dcb_app_priority_table app;
  4270. struct i40e_hw *hw = &pf->hw;
  4271. u8 enabled_tc = 1; /* TC0 is always enabled */
  4272. u8 tc, i;
  4273. /* Get the iSCSI APP TLV */
  4274. struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
  4275. for (i = 0; i < dcbcfg->numapps; i++) {
  4276. app = dcbcfg->app[i];
  4277. if (app.selector == I40E_APP_SEL_TCPIP &&
  4278. app.protocolid == I40E_APP_PROTOID_ISCSI) {
  4279. tc = dcbcfg->etscfg.prioritytable[app.priority];
  4280. enabled_tc |= BIT(tc);
  4281. break;
  4282. }
  4283. }
  4284. return enabled_tc;
  4285. }
  4286. /**
  4287. * i40e_dcb_get_num_tc - Get the number of TCs from DCBx config
  4288. * @dcbcfg: the corresponding DCBx configuration structure
  4289. *
  4290. * Return the number of TCs from given DCBx configuration
  4291. **/
  4292. static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
  4293. {
  4294. int i, tc_unused = 0;
  4295. u8 num_tc = 0;
  4296. u8 ret = 0;
  4297. /* Scan the ETS Config Priority Table to find
  4298. * traffic class enabled for a given priority
  4299. * and create a bitmask of enabled TCs
  4300. */
  4301. for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
  4302. num_tc |= BIT(dcbcfg->etscfg.prioritytable[i]);
  4303. /* Now scan the bitmask to check for
  4304. * contiguous TCs starting with TC0
  4305. */
  4306. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4307. if (num_tc & BIT(i)) {
  4308. if (!tc_unused) {
  4309. ret++;
  4310. } else {
  4311. pr_err("Non-contiguous TC - Disabling DCB\n");
  4312. return 1;
  4313. }
  4314. } else {
  4315. tc_unused = 1;
  4316. }
  4317. }
  4318. /* There is always at least TC0 */
  4319. if (!ret)
  4320. ret = 1;
  4321. return ret;
  4322. }
  4323. /**
  4324. * i40e_dcb_get_enabled_tc - Get enabled traffic classes
  4325. * @dcbcfg: the corresponding DCBx configuration structure
  4326. *
  4327. * Query the current DCB configuration and return the number of
  4328. * traffic classes enabled from the given DCBX config
  4329. **/
  4330. static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
  4331. {
  4332. u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
  4333. u8 enabled_tc = 1;
  4334. u8 i;
  4335. for (i = 0; i < num_tc; i++)
  4336. enabled_tc |= BIT(i);
  4337. return enabled_tc;
  4338. }
  4339. /**
  4340. * i40e_mqprio_get_enabled_tc - Get enabled traffic classes
  4341. * @pf: PF being queried
  4342. *
  4343. * Query the current MQPRIO configuration and return the number of
  4344. * traffic classes enabled.
  4345. **/
  4346. static u8 i40e_mqprio_get_enabled_tc(struct i40e_pf *pf)
  4347. {
  4348. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  4349. u8 num_tc = vsi->mqprio_qopt.qopt.num_tc;
  4350. u8 enabled_tc = 1, i;
  4351. for (i = 1; i < num_tc; i++)
  4352. enabled_tc |= BIT(i);
  4353. return enabled_tc;
  4354. }
  4355. /**
  4356. * i40e_pf_get_num_tc - Get enabled traffic classes for PF
  4357. * @pf: PF being queried
  4358. *
  4359. * Return number of traffic classes enabled for the given PF
  4360. **/
  4361. static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
  4362. {
  4363. struct i40e_hw *hw = &pf->hw;
  4364. u8 i, enabled_tc = 1;
  4365. u8 num_tc = 0;
  4366. struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
  4367. if (pf->flags & I40E_FLAG_TC_MQPRIO)
  4368. return pf->vsi[pf->lan_vsi]->mqprio_qopt.qopt.num_tc;
  4369. /* If neither MQPRIO nor DCB is enabled, then always use single TC */
  4370. if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
  4371. return 1;
  4372. /* SFP mode will be enabled for all TCs on port */
  4373. if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
  4374. return i40e_dcb_get_num_tc(dcbcfg);
  4375. /* MFP mode return count of enabled TCs for this PF */
  4376. if (pf->hw.func_caps.iscsi)
  4377. enabled_tc = i40e_get_iscsi_tc_map(pf);
  4378. else
  4379. return 1; /* Only TC0 */
  4380. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4381. if (enabled_tc & BIT(i))
  4382. num_tc++;
  4383. }
  4384. return num_tc;
  4385. }
  4386. /**
  4387. * i40e_pf_get_pf_tc_map - Get bitmap for enabled traffic classes
  4388. * @pf: PF being queried
  4389. *
  4390. * Return a bitmap for enabled traffic classes for this PF.
  4391. **/
  4392. static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
  4393. {
  4394. if (pf->flags & I40E_FLAG_TC_MQPRIO)
  4395. return i40e_mqprio_get_enabled_tc(pf);
  4396. /* If neither MQPRIO nor DCB is enabled for this PF then just return
  4397. * default TC
  4398. */
  4399. if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
  4400. return I40E_DEFAULT_TRAFFIC_CLASS;
  4401. /* SFP mode we want PF to be enabled for all TCs */
  4402. if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
  4403. return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
  4404. /* MFP enabled and iSCSI PF type */
  4405. if (pf->hw.func_caps.iscsi)
  4406. return i40e_get_iscsi_tc_map(pf);
  4407. else
  4408. return I40E_DEFAULT_TRAFFIC_CLASS;
  4409. }
  4410. /**
  4411. * i40e_vsi_get_bw_info - Query VSI BW Information
  4412. * @vsi: the VSI being queried
  4413. *
  4414. * Returns 0 on success, negative value on failure
  4415. **/
  4416. static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
  4417. {
  4418. struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
  4419. struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
  4420. struct i40e_pf *pf = vsi->back;
  4421. struct i40e_hw *hw = &pf->hw;
  4422. i40e_status ret;
  4423. u32 tc_bw_max;
  4424. int i;
  4425. /* Get the VSI level BW configuration */
  4426. ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
  4427. if (ret) {
  4428. dev_info(&pf->pdev->dev,
  4429. "couldn't get PF vsi bw config, err %s aq_err %s\n",
  4430. i40e_stat_str(&pf->hw, ret),
  4431. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  4432. return -EINVAL;
  4433. }
  4434. /* Get the VSI level BW configuration per TC */
  4435. ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
  4436. NULL);
  4437. if (ret) {
  4438. dev_info(&pf->pdev->dev,
  4439. "couldn't get PF vsi ets bw config, err %s aq_err %s\n",
  4440. i40e_stat_str(&pf->hw, ret),
  4441. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  4442. return -EINVAL;
  4443. }
  4444. if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
  4445. dev_info(&pf->pdev->dev,
  4446. "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
  4447. bw_config.tc_valid_bits,
  4448. bw_ets_config.tc_valid_bits);
  4449. /* Still continuing */
  4450. }
  4451. vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
  4452. vsi->bw_max_quanta = bw_config.max_bw;
  4453. tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
  4454. (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
  4455. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4456. vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
  4457. vsi->bw_ets_limit_credits[i] =
  4458. le16_to_cpu(bw_ets_config.credits[i]);
  4459. /* 3 bits out of 4 for each TC */
  4460. vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
  4461. }
  4462. return 0;
  4463. }
  4464. /**
  4465. * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
  4466. * @vsi: the VSI being configured
  4467. * @enabled_tc: TC bitmap
  4468. * @bw_share: BW shared credits per TC
  4469. *
  4470. * Returns 0 on success, negative value on failure
  4471. **/
  4472. static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
  4473. u8 *bw_share)
  4474. {
  4475. struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
  4476. i40e_status ret;
  4477. int i;
  4478. if (vsi->back->flags & I40E_FLAG_TC_MQPRIO)
  4479. return 0;
  4480. if (!vsi->mqprio_qopt.qopt.hw) {
  4481. ret = i40e_set_bw_limit(vsi, vsi->seid, 0);
  4482. if (ret)
  4483. dev_info(&vsi->back->pdev->dev,
  4484. "Failed to reset tx rate for vsi->seid %u\n",
  4485. vsi->seid);
  4486. return ret;
  4487. }
  4488. bw_data.tc_valid_bits = enabled_tc;
  4489. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
  4490. bw_data.tc_bw_credits[i] = bw_share[i];
  4491. ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, vsi->seid, &bw_data,
  4492. NULL);
  4493. if (ret) {
  4494. dev_info(&vsi->back->pdev->dev,
  4495. "AQ command Config VSI BW allocation per TC failed = %d\n",
  4496. vsi->back->hw.aq.asq_last_status);
  4497. return -EINVAL;
  4498. }
  4499. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
  4500. vsi->info.qs_handle[i] = bw_data.qs_handles[i];
  4501. return 0;
  4502. }
  4503. /**
  4504. * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
  4505. * @vsi: the VSI being configured
  4506. * @enabled_tc: TC map to be enabled
  4507. *
  4508. **/
  4509. static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
  4510. {
  4511. struct net_device *netdev = vsi->netdev;
  4512. struct i40e_pf *pf = vsi->back;
  4513. struct i40e_hw *hw = &pf->hw;
  4514. u8 netdev_tc = 0;
  4515. int i;
  4516. struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
  4517. if (!netdev)
  4518. return;
  4519. if (!enabled_tc) {
  4520. netdev_reset_tc(netdev);
  4521. return;
  4522. }
  4523. /* Set up actual enabled TCs on the VSI */
  4524. if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
  4525. return;
  4526. /* set per TC queues for the VSI */
  4527. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4528. /* Only set TC queues for enabled tcs
  4529. *
  4530. * e.g. For a VSI that has TC0 and TC3 enabled the
  4531. * enabled_tc bitmap would be 0x00001001; the driver
  4532. * will set the numtc for netdev as 2 that will be
  4533. * referenced by the netdev layer as TC 0 and 1.
  4534. */
  4535. if (vsi->tc_config.enabled_tc & BIT(i))
  4536. netdev_set_tc_queue(netdev,
  4537. vsi->tc_config.tc_info[i].netdev_tc,
  4538. vsi->tc_config.tc_info[i].qcount,
  4539. vsi->tc_config.tc_info[i].qoffset);
  4540. }
  4541. if (pf->flags & I40E_FLAG_TC_MQPRIO)
  4542. return;
  4543. /* Assign UP2TC map for the VSI */
  4544. for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
  4545. /* Get the actual TC# for the UP */
  4546. u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
  4547. /* Get the mapped netdev TC# for the UP */
  4548. netdev_tc = vsi->tc_config.tc_info[ets_tc].netdev_tc;
  4549. netdev_set_prio_tc_map(netdev, i, netdev_tc);
  4550. }
  4551. }
  4552. /**
  4553. * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
  4554. * @vsi: the VSI being configured
  4555. * @ctxt: the ctxt buffer returned from AQ VSI update param command
  4556. **/
  4557. static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
  4558. struct i40e_vsi_context *ctxt)
  4559. {
  4560. /* copy just the sections touched not the entire info
  4561. * since not all sections are valid as returned by
  4562. * update vsi params
  4563. */
  4564. vsi->info.mapping_flags = ctxt->info.mapping_flags;
  4565. memcpy(&vsi->info.queue_mapping,
  4566. &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
  4567. memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
  4568. sizeof(vsi->info.tc_mapping));
  4569. }
  4570. /**
  4571. * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
  4572. * @vsi: VSI to be configured
  4573. * @enabled_tc: TC bitmap
  4574. *
  4575. * This configures a particular VSI for TCs that are mapped to the
  4576. * given TC bitmap. It uses default bandwidth share for TCs across
  4577. * VSIs to configure TC for a particular VSI.
  4578. *
  4579. * NOTE:
  4580. * It is expected that the VSI queues have been quisced before calling
  4581. * this function.
  4582. **/
  4583. static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
  4584. {
  4585. u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
  4586. struct i40e_pf *pf = vsi->back;
  4587. struct i40e_hw *hw = &pf->hw;
  4588. struct i40e_vsi_context ctxt;
  4589. int ret = 0;
  4590. int i;
  4591. /* Check if enabled_tc is same as existing or new TCs */
  4592. if (vsi->tc_config.enabled_tc == enabled_tc &&
  4593. vsi->mqprio_qopt.mode != TC_MQPRIO_MODE_CHANNEL)
  4594. return ret;
  4595. /* Enable ETS TCs with equal BW Share for now across all VSIs */
  4596. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4597. if (enabled_tc & BIT(i))
  4598. bw_share[i] = 1;
  4599. }
  4600. ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
  4601. if (ret) {
  4602. struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
  4603. dev_info(&pf->pdev->dev,
  4604. "Failed configuring TC map %d for VSI %d\n",
  4605. enabled_tc, vsi->seid);
  4606. ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid,
  4607. &bw_config, NULL);
  4608. if (ret) {
  4609. dev_info(&pf->pdev->dev,
  4610. "Failed querying vsi bw info, err %s aq_err %s\n",
  4611. i40e_stat_str(hw, ret),
  4612. i40e_aq_str(hw, hw->aq.asq_last_status));
  4613. goto out;
  4614. }
  4615. if ((bw_config.tc_valid_bits & enabled_tc) != enabled_tc) {
  4616. u8 valid_tc = bw_config.tc_valid_bits & enabled_tc;
  4617. if (!valid_tc)
  4618. valid_tc = bw_config.tc_valid_bits;
  4619. /* Always enable TC0, no matter what */
  4620. valid_tc |= 1;
  4621. dev_info(&pf->pdev->dev,
  4622. "Requested tc 0x%x, but FW reports 0x%x as valid. Attempting to use 0x%x.\n",
  4623. enabled_tc, bw_config.tc_valid_bits, valid_tc);
  4624. enabled_tc = valid_tc;
  4625. }
  4626. ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
  4627. if (ret) {
  4628. dev_err(&pf->pdev->dev,
  4629. "Unable to configure TC map %d for VSI %d\n",
  4630. enabled_tc, vsi->seid);
  4631. goto out;
  4632. }
  4633. }
  4634. /* Update Queue Pairs Mapping for currently enabled UPs */
  4635. ctxt.seid = vsi->seid;
  4636. ctxt.pf_num = vsi->back->hw.pf_id;
  4637. ctxt.vf_num = 0;
  4638. ctxt.uplink_seid = vsi->uplink_seid;
  4639. ctxt.info = vsi->info;
  4640. if (vsi->back->flags & I40E_FLAG_TC_MQPRIO) {
  4641. ret = i40e_vsi_setup_queue_map_mqprio(vsi, &ctxt, enabled_tc);
  4642. if (ret)
  4643. goto out;
  4644. } else {
  4645. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
  4646. }
  4647. /* On destroying the qdisc, reset vsi->rss_size, as number of enabled
  4648. * queues changed.
  4649. */
  4650. if (!vsi->mqprio_qopt.qopt.hw && vsi->reconfig_rss) {
  4651. vsi->rss_size = min_t(int, vsi->back->alloc_rss_size,
  4652. vsi->num_queue_pairs);
  4653. ret = i40e_vsi_config_rss(vsi);
  4654. if (ret) {
  4655. dev_info(&vsi->back->pdev->dev,
  4656. "Failed to reconfig rss for num_queues\n");
  4657. return ret;
  4658. }
  4659. vsi->reconfig_rss = false;
  4660. }
  4661. if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
  4662. ctxt.info.valid_sections |=
  4663. cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
  4664. ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
  4665. }
  4666. /* Update the VSI after updating the VSI queue-mapping
  4667. * information
  4668. */
  4669. ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
  4670. if (ret) {
  4671. dev_info(&pf->pdev->dev,
  4672. "Update vsi tc config failed, err %s aq_err %s\n",
  4673. i40e_stat_str(hw, ret),
  4674. i40e_aq_str(hw, hw->aq.asq_last_status));
  4675. goto out;
  4676. }
  4677. /* update the local VSI info with updated queue map */
  4678. i40e_vsi_update_queue_map(vsi, &ctxt);
  4679. vsi->info.valid_sections = 0;
  4680. /* Update current VSI BW information */
  4681. ret = i40e_vsi_get_bw_info(vsi);
  4682. if (ret) {
  4683. dev_info(&pf->pdev->dev,
  4684. "Failed updating vsi bw info, err %s aq_err %s\n",
  4685. i40e_stat_str(hw, ret),
  4686. i40e_aq_str(hw, hw->aq.asq_last_status));
  4687. goto out;
  4688. }
  4689. /* Update the netdev TC setup */
  4690. i40e_vsi_config_netdev_tc(vsi, enabled_tc);
  4691. out:
  4692. return ret;
  4693. }
  4694. /**
  4695. * i40e_get_link_speed - Returns link speed for the interface
  4696. * @vsi: VSI to be configured
  4697. *
  4698. **/
  4699. static int i40e_get_link_speed(struct i40e_vsi *vsi)
  4700. {
  4701. struct i40e_pf *pf = vsi->back;
  4702. switch (pf->hw.phy.link_info.link_speed) {
  4703. case I40E_LINK_SPEED_40GB:
  4704. return 40000;
  4705. case I40E_LINK_SPEED_25GB:
  4706. return 25000;
  4707. case I40E_LINK_SPEED_20GB:
  4708. return 20000;
  4709. case I40E_LINK_SPEED_10GB:
  4710. return 10000;
  4711. case I40E_LINK_SPEED_1GB:
  4712. return 1000;
  4713. default:
  4714. return -EINVAL;
  4715. }
  4716. }
  4717. /**
  4718. * i40e_set_bw_limit - setup BW limit for Tx traffic based on max_tx_rate
  4719. * @vsi: VSI to be configured
  4720. * @seid: seid of the channel/VSI
  4721. * @max_tx_rate: max TX rate to be configured as BW limit
  4722. *
  4723. * Helper function to set BW limit for a given VSI
  4724. **/
  4725. int i40e_set_bw_limit(struct i40e_vsi *vsi, u16 seid, u64 max_tx_rate)
  4726. {
  4727. struct i40e_pf *pf = vsi->back;
  4728. u64 credits = 0;
  4729. int speed = 0;
  4730. int ret = 0;
  4731. speed = i40e_get_link_speed(vsi);
  4732. if (max_tx_rate > speed) {
  4733. dev_err(&pf->pdev->dev,
  4734. "Invalid max tx rate %llu specified for VSI seid %d.",
  4735. max_tx_rate, seid);
  4736. return -EINVAL;
  4737. }
  4738. if (max_tx_rate && max_tx_rate < 50) {
  4739. dev_warn(&pf->pdev->dev,
  4740. "Setting max tx rate to minimum usable value of 50Mbps.\n");
  4741. max_tx_rate = 50;
  4742. }
  4743. /* Tx rate credits are in values of 50Mbps, 0 is disabled */
  4744. credits = max_tx_rate;
  4745. do_div(credits, I40E_BW_CREDIT_DIVISOR);
  4746. ret = i40e_aq_config_vsi_bw_limit(&pf->hw, seid, credits,
  4747. I40E_MAX_BW_INACTIVE_ACCUM, NULL);
  4748. if (ret)
  4749. dev_err(&pf->pdev->dev,
  4750. "Failed set tx rate (%llu Mbps) for vsi->seid %u, err %s aq_err %s\n",
  4751. max_tx_rate, seid, i40e_stat_str(&pf->hw, ret),
  4752. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  4753. return ret;
  4754. }
  4755. /**
  4756. * i40e_remove_queue_channels - Remove queue channels for the TCs
  4757. * @vsi: VSI to be configured
  4758. *
  4759. * Remove queue channels for the TCs
  4760. **/
  4761. static void i40e_remove_queue_channels(struct i40e_vsi *vsi)
  4762. {
  4763. enum i40e_admin_queue_err last_aq_status;
  4764. struct i40e_cloud_filter *cfilter;
  4765. struct i40e_channel *ch, *ch_tmp;
  4766. struct i40e_pf *pf = vsi->back;
  4767. struct hlist_node *node;
  4768. int ret, i;
  4769. /* Reset rss size that was stored when reconfiguring rss for
  4770. * channel VSIs with non-power-of-2 queue count.
  4771. */
  4772. vsi->current_rss_size = 0;
  4773. /* perform cleanup for channels if they exist */
  4774. if (list_empty(&vsi->ch_list))
  4775. return;
  4776. list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
  4777. struct i40e_vsi *p_vsi;
  4778. list_del(&ch->list);
  4779. p_vsi = ch->parent_vsi;
  4780. if (!p_vsi || !ch->initialized) {
  4781. kfree(ch);
  4782. continue;
  4783. }
  4784. /* Reset queue contexts */
  4785. for (i = 0; i < ch->num_queue_pairs; i++) {
  4786. struct i40e_ring *tx_ring, *rx_ring;
  4787. u16 pf_q;
  4788. pf_q = ch->base_queue + i;
  4789. tx_ring = vsi->tx_rings[pf_q];
  4790. tx_ring->ch = NULL;
  4791. rx_ring = vsi->rx_rings[pf_q];
  4792. rx_ring->ch = NULL;
  4793. }
  4794. /* Reset BW configured for this VSI via mqprio */
  4795. ret = i40e_set_bw_limit(vsi, ch->seid, 0);
  4796. if (ret)
  4797. dev_info(&vsi->back->pdev->dev,
  4798. "Failed to reset tx rate for ch->seid %u\n",
  4799. ch->seid);
  4800. /* delete cloud filters associated with this channel */
  4801. hlist_for_each_entry_safe(cfilter, node,
  4802. &pf->cloud_filter_list, cloud_node) {
  4803. if (cfilter->seid != ch->seid)
  4804. continue;
  4805. hash_del(&cfilter->cloud_node);
  4806. if (cfilter->dst_port)
  4807. ret = i40e_add_del_cloud_filter_big_buf(vsi,
  4808. cfilter,
  4809. false);
  4810. else
  4811. ret = i40e_add_del_cloud_filter(vsi, cfilter,
  4812. false);
  4813. last_aq_status = pf->hw.aq.asq_last_status;
  4814. if (ret)
  4815. dev_info(&pf->pdev->dev,
  4816. "Failed to delete cloud filter, err %s aq_err %s\n",
  4817. i40e_stat_str(&pf->hw, ret),
  4818. i40e_aq_str(&pf->hw, last_aq_status));
  4819. kfree(cfilter);
  4820. }
  4821. /* delete VSI from FW */
  4822. ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
  4823. NULL);
  4824. if (ret)
  4825. dev_err(&vsi->back->pdev->dev,
  4826. "unable to remove channel (%d) for parent VSI(%d)\n",
  4827. ch->seid, p_vsi->seid);
  4828. kfree(ch);
  4829. }
  4830. INIT_LIST_HEAD(&vsi->ch_list);
  4831. }
  4832. /**
  4833. * i40e_is_any_channel - channel exist or not
  4834. * @vsi: ptr to VSI to which channels are associated with
  4835. *
  4836. * Returns true or false if channel(s) exist for associated VSI or not
  4837. **/
  4838. static bool i40e_is_any_channel(struct i40e_vsi *vsi)
  4839. {
  4840. struct i40e_channel *ch, *ch_tmp;
  4841. list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
  4842. if (ch->initialized)
  4843. return true;
  4844. }
  4845. return false;
  4846. }
  4847. /**
  4848. * i40e_get_max_queues_for_channel
  4849. * @vsi: ptr to VSI to which channels are associated with
  4850. *
  4851. * Helper function which returns max value among the queue counts set on the
  4852. * channels/TCs created.
  4853. **/
  4854. static int i40e_get_max_queues_for_channel(struct i40e_vsi *vsi)
  4855. {
  4856. struct i40e_channel *ch, *ch_tmp;
  4857. int max = 0;
  4858. list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
  4859. if (!ch->initialized)
  4860. continue;
  4861. if (ch->num_queue_pairs > max)
  4862. max = ch->num_queue_pairs;
  4863. }
  4864. return max;
  4865. }
  4866. /**
  4867. * i40e_validate_num_queues - validate num_queues w.r.t channel
  4868. * @pf: ptr to PF device
  4869. * @num_queues: number of queues
  4870. * @vsi: the parent VSI
  4871. * @reconfig_rss: indicates should the RSS be reconfigured or not
  4872. *
  4873. * This function validates number of queues in the context of new channel
  4874. * which is being established and determines if RSS should be reconfigured
  4875. * or not for parent VSI.
  4876. **/
  4877. static int i40e_validate_num_queues(struct i40e_pf *pf, int num_queues,
  4878. struct i40e_vsi *vsi, bool *reconfig_rss)
  4879. {
  4880. int max_ch_queues;
  4881. if (!reconfig_rss)
  4882. return -EINVAL;
  4883. *reconfig_rss = false;
  4884. if (vsi->current_rss_size) {
  4885. if (num_queues > vsi->current_rss_size) {
  4886. dev_dbg(&pf->pdev->dev,
  4887. "Error: num_queues (%d) > vsi's current_size(%d)\n",
  4888. num_queues, vsi->current_rss_size);
  4889. return -EINVAL;
  4890. } else if ((num_queues < vsi->current_rss_size) &&
  4891. (!is_power_of_2(num_queues))) {
  4892. dev_dbg(&pf->pdev->dev,
  4893. "Error: num_queues (%d) < vsi's current_size(%d), but not power of 2\n",
  4894. num_queues, vsi->current_rss_size);
  4895. return -EINVAL;
  4896. }
  4897. }
  4898. if (!is_power_of_2(num_queues)) {
  4899. /* Find the max num_queues configured for channel if channel
  4900. * exist.
  4901. * if channel exist, then enforce 'num_queues' to be more than
  4902. * max ever queues configured for channel.
  4903. */
  4904. max_ch_queues = i40e_get_max_queues_for_channel(vsi);
  4905. if (num_queues < max_ch_queues) {
  4906. dev_dbg(&pf->pdev->dev,
  4907. "Error: num_queues (%d) < max queues configured for channel(%d)\n",
  4908. num_queues, max_ch_queues);
  4909. return -EINVAL;
  4910. }
  4911. *reconfig_rss = true;
  4912. }
  4913. return 0;
  4914. }
  4915. /**
  4916. * i40e_vsi_reconfig_rss - reconfig RSS based on specified rss_size
  4917. * @vsi: the VSI being setup
  4918. * @rss_size: size of RSS, accordingly LUT gets reprogrammed
  4919. *
  4920. * This function reconfigures RSS by reprogramming LUTs using 'rss_size'
  4921. **/
  4922. static int i40e_vsi_reconfig_rss(struct i40e_vsi *vsi, u16 rss_size)
  4923. {
  4924. struct i40e_pf *pf = vsi->back;
  4925. u8 seed[I40E_HKEY_ARRAY_SIZE];
  4926. struct i40e_hw *hw = &pf->hw;
  4927. int local_rss_size;
  4928. u8 *lut;
  4929. int ret;
  4930. if (!vsi->rss_size)
  4931. return -EINVAL;
  4932. if (rss_size > vsi->rss_size)
  4933. return -EINVAL;
  4934. local_rss_size = min_t(int, vsi->rss_size, rss_size);
  4935. lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
  4936. if (!lut)
  4937. return -ENOMEM;
  4938. /* Ignoring user configured lut if there is one */
  4939. i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, local_rss_size);
  4940. /* Use user configured hash key if there is one, otherwise
  4941. * use default.
  4942. */
  4943. if (vsi->rss_hkey_user)
  4944. memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
  4945. else
  4946. netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
  4947. ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
  4948. if (ret) {
  4949. dev_info(&pf->pdev->dev,
  4950. "Cannot set RSS lut, err %s aq_err %s\n",
  4951. i40e_stat_str(hw, ret),
  4952. i40e_aq_str(hw, hw->aq.asq_last_status));
  4953. kfree(lut);
  4954. return ret;
  4955. }
  4956. kfree(lut);
  4957. /* Do the update w.r.t. storing rss_size */
  4958. if (!vsi->orig_rss_size)
  4959. vsi->orig_rss_size = vsi->rss_size;
  4960. vsi->current_rss_size = local_rss_size;
  4961. return ret;
  4962. }
  4963. /**
  4964. * i40e_channel_setup_queue_map - Setup a channel queue map
  4965. * @pf: ptr to PF device
  4966. * @vsi: the VSI being setup
  4967. * @ctxt: VSI context structure
  4968. * @ch: ptr to channel structure
  4969. *
  4970. * Setup queue map for a specific channel
  4971. **/
  4972. static void i40e_channel_setup_queue_map(struct i40e_pf *pf,
  4973. struct i40e_vsi_context *ctxt,
  4974. struct i40e_channel *ch)
  4975. {
  4976. u16 qcount, qmap, sections = 0;
  4977. u8 offset = 0;
  4978. int pow;
  4979. sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
  4980. sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
  4981. qcount = min_t(int, ch->num_queue_pairs, pf->num_lan_msix);
  4982. ch->num_queue_pairs = qcount;
  4983. /* find the next higher power-of-2 of num queue pairs */
  4984. pow = ilog2(qcount);
  4985. if (!is_power_of_2(qcount))
  4986. pow++;
  4987. qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
  4988. (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
  4989. /* Setup queue TC[0].qmap for given VSI context */
  4990. ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
  4991. ctxt->info.up_enable_bits = 0x1; /* TC0 enabled */
  4992. ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
  4993. ctxt->info.queue_mapping[0] = cpu_to_le16(ch->base_queue);
  4994. ctxt->info.valid_sections |= cpu_to_le16(sections);
  4995. }
  4996. /**
  4997. * i40e_add_channel - add a channel by adding VSI
  4998. * @pf: ptr to PF device
  4999. * @uplink_seid: underlying HW switching element (VEB) ID
  5000. * @ch: ptr to channel structure
  5001. *
  5002. * Add a channel (VSI) using add_vsi and queue_map
  5003. **/
  5004. static int i40e_add_channel(struct i40e_pf *pf, u16 uplink_seid,
  5005. struct i40e_channel *ch)
  5006. {
  5007. struct i40e_hw *hw = &pf->hw;
  5008. struct i40e_vsi_context ctxt;
  5009. u8 enabled_tc = 0x1; /* TC0 enabled */
  5010. int ret;
  5011. if (ch->type != I40E_VSI_VMDQ2) {
  5012. dev_info(&pf->pdev->dev,
  5013. "add new vsi failed, ch->type %d\n", ch->type);
  5014. return -EINVAL;
  5015. }
  5016. memset(&ctxt, 0, sizeof(ctxt));
  5017. ctxt.pf_num = hw->pf_id;
  5018. ctxt.vf_num = 0;
  5019. ctxt.uplink_seid = uplink_seid;
  5020. ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
  5021. if (ch->type == I40E_VSI_VMDQ2)
  5022. ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
  5023. if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED) {
  5024. ctxt.info.valid_sections |=
  5025. cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  5026. ctxt.info.switch_id =
  5027. cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  5028. }
  5029. /* Set queue map for a given VSI context */
  5030. i40e_channel_setup_queue_map(pf, &ctxt, ch);
  5031. /* Now time to create VSI */
  5032. ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
  5033. if (ret) {
  5034. dev_info(&pf->pdev->dev,
  5035. "add new vsi failed, err %s aq_err %s\n",
  5036. i40e_stat_str(&pf->hw, ret),
  5037. i40e_aq_str(&pf->hw,
  5038. pf->hw.aq.asq_last_status));
  5039. return -ENOENT;
  5040. }
  5041. /* Success, update channel */
  5042. ch->enabled_tc = enabled_tc;
  5043. ch->seid = ctxt.seid;
  5044. ch->vsi_number = ctxt.vsi_number;
  5045. ch->stat_counter_idx = cpu_to_le16(ctxt.info.stat_counter_idx);
  5046. /* copy just the sections touched not the entire info
  5047. * since not all sections are valid as returned by
  5048. * update vsi params
  5049. */
  5050. ch->info.mapping_flags = ctxt.info.mapping_flags;
  5051. memcpy(&ch->info.queue_mapping,
  5052. &ctxt.info.queue_mapping, sizeof(ctxt.info.queue_mapping));
  5053. memcpy(&ch->info.tc_mapping, ctxt.info.tc_mapping,
  5054. sizeof(ctxt.info.tc_mapping));
  5055. return 0;
  5056. }
  5057. static int i40e_channel_config_bw(struct i40e_vsi *vsi, struct i40e_channel *ch,
  5058. u8 *bw_share)
  5059. {
  5060. struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
  5061. i40e_status ret;
  5062. int i;
  5063. bw_data.tc_valid_bits = ch->enabled_tc;
  5064. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
  5065. bw_data.tc_bw_credits[i] = bw_share[i];
  5066. ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, ch->seid,
  5067. &bw_data, NULL);
  5068. if (ret) {
  5069. dev_info(&vsi->back->pdev->dev,
  5070. "Config VSI BW allocation per TC failed, aq_err: %d for new_vsi->seid %u\n",
  5071. vsi->back->hw.aq.asq_last_status, ch->seid);
  5072. return -EINVAL;
  5073. }
  5074. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
  5075. ch->info.qs_handle[i] = bw_data.qs_handles[i];
  5076. return 0;
  5077. }
  5078. /**
  5079. * i40e_channel_config_tx_ring - config TX ring associated with new channel
  5080. * @pf: ptr to PF device
  5081. * @vsi: the VSI being setup
  5082. * @ch: ptr to channel structure
  5083. *
  5084. * Configure TX rings associated with channel (VSI) since queues are being
  5085. * from parent VSI.
  5086. **/
  5087. static int i40e_channel_config_tx_ring(struct i40e_pf *pf,
  5088. struct i40e_vsi *vsi,
  5089. struct i40e_channel *ch)
  5090. {
  5091. i40e_status ret;
  5092. int i;
  5093. u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
  5094. /* Enable ETS TCs with equal BW Share for now across all VSIs */
  5095. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  5096. if (ch->enabled_tc & BIT(i))
  5097. bw_share[i] = 1;
  5098. }
  5099. /* configure BW for new VSI */
  5100. ret = i40e_channel_config_bw(vsi, ch, bw_share);
  5101. if (ret) {
  5102. dev_info(&vsi->back->pdev->dev,
  5103. "Failed configuring TC map %d for channel (seid %u)\n",
  5104. ch->enabled_tc, ch->seid);
  5105. return ret;
  5106. }
  5107. for (i = 0; i < ch->num_queue_pairs; i++) {
  5108. struct i40e_ring *tx_ring, *rx_ring;
  5109. u16 pf_q;
  5110. pf_q = ch->base_queue + i;
  5111. /* Get to TX ring ptr of main VSI, for re-setup TX queue
  5112. * context
  5113. */
  5114. tx_ring = vsi->tx_rings[pf_q];
  5115. tx_ring->ch = ch;
  5116. /* Get the RX ring ptr */
  5117. rx_ring = vsi->rx_rings[pf_q];
  5118. rx_ring->ch = ch;
  5119. }
  5120. return 0;
  5121. }
  5122. /**
  5123. * i40e_setup_hw_channel - setup new channel
  5124. * @pf: ptr to PF device
  5125. * @vsi: the VSI being setup
  5126. * @ch: ptr to channel structure
  5127. * @uplink_seid: underlying HW switching element (VEB) ID
  5128. * @type: type of channel to be created (VMDq2/VF)
  5129. *
  5130. * Setup new channel (VSI) based on specified type (VMDq2/VF)
  5131. * and configures TX rings accordingly
  5132. **/
  5133. static inline int i40e_setup_hw_channel(struct i40e_pf *pf,
  5134. struct i40e_vsi *vsi,
  5135. struct i40e_channel *ch,
  5136. u16 uplink_seid, u8 type)
  5137. {
  5138. int ret;
  5139. ch->initialized = false;
  5140. ch->base_queue = vsi->next_base_queue;
  5141. ch->type = type;
  5142. /* Proceed with creation of channel (VMDq2) VSI */
  5143. ret = i40e_add_channel(pf, uplink_seid, ch);
  5144. if (ret) {
  5145. dev_info(&pf->pdev->dev,
  5146. "failed to add_channel using uplink_seid %u\n",
  5147. uplink_seid);
  5148. return ret;
  5149. }
  5150. /* Mark the successful creation of channel */
  5151. ch->initialized = true;
  5152. /* Reconfigure TX queues using QTX_CTL register */
  5153. ret = i40e_channel_config_tx_ring(pf, vsi, ch);
  5154. if (ret) {
  5155. dev_info(&pf->pdev->dev,
  5156. "failed to configure TX rings for channel %u\n",
  5157. ch->seid);
  5158. return ret;
  5159. }
  5160. /* update 'next_base_queue' */
  5161. vsi->next_base_queue = vsi->next_base_queue + ch->num_queue_pairs;
  5162. dev_dbg(&pf->pdev->dev,
  5163. "Added channel: vsi_seid %u, vsi_number %u, stat_counter_idx %u, num_queue_pairs %u, pf->next_base_queue %d\n",
  5164. ch->seid, ch->vsi_number, ch->stat_counter_idx,
  5165. ch->num_queue_pairs,
  5166. vsi->next_base_queue);
  5167. return ret;
  5168. }
  5169. /**
  5170. * i40e_setup_channel - setup new channel using uplink element
  5171. * @pf: ptr to PF device
  5172. * @type: type of channel to be created (VMDq2/VF)
  5173. * @uplink_seid: underlying HW switching element (VEB) ID
  5174. * @ch: ptr to channel structure
  5175. *
  5176. * Setup new channel (VSI) based on specified type (VMDq2/VF)
  5177. * and uplink switching element (uplink_seid)
  5178. **/
  5179. static bool i40e_setup_channel(struct i40e_pf *pf, struct i40e_vsi *vsi,
  5180. struct i40e_channel *ch)
  5181. {
  5182. u8 vsi_type;
  5183. u16 seid;
  5184. int ret;
  5185. if (vsi->type == I40E_VSI_MAIN) {
  5186. vsi_type = I40E_VSI_VMDQ2;
  5187. } else {
  5188. dev_err(&pf->pdev->dev, "unsupported parent vsi type(%d)\n",
  5189. vsi->type);
  5190. return false;
  5191. }
  5192. /* underlying switching element */
  5193. seid = pf->vsi[pf->lan_vsi]->uplink_seid;
  5194. /* create channel (VSI), configure TX rings */
  5195. ret = i40e_setup_hw_channel(pf, vsi, ch, seid, vsi_type);
  5196. if (ret) {
  5197. dev_err(&pf->pdev->dev, "failed to setup hw_channel\n");
  5198. return false;
  5199. }
  5200. return ch->initialized ? true : false;
  5201. }
  5202. /**
  5203. * i40e_validate_and_set_switch_mode - sets up switch mode correctly
  5204. * @vsi: ptr to VSI which has PF backing
  5205. *
  5206. * Sets up switch mode correctly if it needs to be changed and perform
  5207. * what are allowed modes.
  5208. **/
  5209. static int i40e_validate_and_set_switch_mode(struct i40e_vsi *vsi)
  5210. {
  5211. u8 mode;
  5212. struct i40e_pf *pf = vsi->back;
  5213. struct i40e_hw *hw = &pf->hw;
  5214. int ret;
  5215. ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_dev_capabilities);
  5216. if (ret)
  5217. return -EINVAL;
  5218. if (hw->dev_caps.switch_mode) {
  5219. /* if switch mode is set, support mode2 (non-tunneled for
  5220. * cloud filter) for now
  5221. */
  5222. u32 switch_mode = hw->dev_caps.switch_mode &
  5223. I40E_SWITCH_MODE_MASK;
  5224. if (switch_mode >= I40E_CLOUD_FILTER_MODE1) {
  5225. if (switch_mode == I40E_CLOUD_FILTER_MODE2)
  5226. return 0;
  5227. dev_err(&pf->pdev->dev,
  5228. "Invalid switch_mode (%d), only non-tunneled mode for cloud filter is supported\n",
  5229. hw->dev_caps.switch_mode);
  5230. return -EINVAL;
  5231. }
  5232. }
  5233. /* Set Bit 7 to be valid */
  5234. mode = I40E_AQ_SET_SWITCH_BIT7_VALID;
  5235. /* Set L4type for TCP support */
  5236. mode |= I40E_AQ_SET_SWITCH_L4_TYPE_TCP;
  5237. /* Set cloud filter mode */
  5238. mode |= I40E_AQ_SET_SWITCH_MODE_NON_TUNNEL;
  5239. /* Prep mode field for set_switch_config */
  5240. ret = i40e_aq_set_switch_config(hw, pf->last_sw_conf_flags,
  5241. pf->last_sw_conf_valid_flags,
  5242. mode, NULL);
  5243. if (ret && hw->aq.asq_last_status != I40E_AQ_RC_ESRCH)
  5244. dev_err(&pf->pdev->dev,
  5245. "couldn't set switch config bits, err %s aq_err %s\n",
  5246. i40e_stat_str(hw, ret),
  5247. i40e_aq_str(hw,
  5248. hw->aq.asq_last_status));
  5249. return ret;
  5250. }
  5251. /**
  5252. * i40e_create_queue_channel - function to create channel
  5253. * @vsi: VSI to be configured
  5254. * @ch: ptr to channel (it contains channel specific params)
  5255. *
  5256. * This function creates channel (VSI) using num_queues specified by user,
  5257. * reconfigs RSS if needed.
  5258. **/
  5259. int i40e_create_queue_channel(struct i40e_vsi *vsi,
  5260. struct i40e_channel *ch)
  5261. {
  5262. struct i40e_pf *pf = vsi->back;
  5263. bool reconfig_rss;
  5264. int err;
  5265. if (!ch)
  5266. return -EINVAL;
  5267. if (!ch->num_queue_pairs) {
  5268. dev_err(&pf->pdev->dev, "Invalid num_queues requested: %d\n",
  5269. ch->num_queue_pairs);
  5270. return -EINVAL;
  5271. }
  5272. /* validate user requested num_queues for channel */
  5273. err = i40e_validate_num_queues(pf, ch->num_queue_pairs, vsi,
  5274. &reconfig_rss);
  5275. if (err) {
  5276. dev_info(&pf->pdev->dev, "Failed to validate num_queues (%d)\n",
  5277. ch->num_queue_pairs);
  5278. return -EINVAL;
  5279. }
  5280. /* By default we are in VEPA mode, if this is the first VF/VMDq
  5281. * VSI to be added switch to VEB mode.
  5282. */
  5283. if ((!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) ||
  5284. (!i40e_is_any_channel(vsi))) {
  5285. if (!is_power_of_2(vsi->tc_config.tc_info[0].qcount)) {
  5286. dev_dbg(&pf->pdev->dev,
  5287. "Failed to create channel. Override queues (%u) not power of 2\n",
  5288. vsi->tc_config.tc_info[0].qcount);
  5289. return -EINVAL;
  5290. }
  5291. if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
  5292. pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
  5293. if (vsi->type == I40E_VSI_MAIN) {
  5294. if (pf->flags & I40E_FLAG_TC_MQPRIO)
  5295. i40e_do_reset(pf, I40E_PF_RESET_FLAG,
  5296. true);
  5297. else
  5298. i40e_do_reset_safe(pf,
  5299. I40E_PF_RESET_FLAG);
  5300. }
  5301. }
  5302. /* now onwards for main VSI, number of queues will be value
  5303. * of TC0's queue count
  5304. */
  5305. }
  5306. /* By this time, vsi->cnt_q_avail shall be set to non-zero and
  5307. * it should be more than num_queues
  5308. */
  5309. if (!vsi->cnt_q_avail || vsi->cnt_q_avail < ch->num_queue_pairs) {
  5310. dev_dbg(&pf->pdev->dev,
  5311. "Error: cnt_q_avail (%u) less than num_queues %d\n",
  5312. vsi->cnt_q_avail, ch->num_queue_pairs);
  5313. return -EINVAL;
  5314. }
  5315. /* reconfig_rss only if vsi type is MAIN_VSI */
  5316. if (reconfig_rss && (vsi->type == I40E_VSI_MAIN)) {
  5317. err = i40e_vsi_reconfig_rss(vsi, ch->num_queue_pairs);
  5318. if (err) {
  5319. dev_info(&pf->pdev->dev,
  5320. "Error: unable to reconfig rss for num_queues (%u)\n",
  5321. ch->num_queue_pairs);
  5322. return -EINVAL;
  5323. }
  5324. }
  5325. if (!i40e_setup_channel(pf, vsi, ch)) {
  5326. dev_info(&pf->pdev->dev, "Failed to setup channel\n");
  5327. return -EINVAL;
  5328. }
  5329. dev_info(&pf->pdev->dev,
  5330. "Setup channel (id:%u) utilizing num_queues %d\n",
  5331. ch->seid, ch->num_queue_pairs);
  5332. /* configure VSI for BW limit */
  5333. if (ch->max_tx_rate) {
  5334. u64 credits = ch->max_tx_rate;
  5335. if (i40e_set_bw_limit(vsi, ch->seid, ch->max_tx_rate))
  5336. return -EINVAL;
  5337. do_div(credits, I40E_BW_CREDIT_DIVISOR);
  5338. dev_dbg(&pf->pdev->dev,
  5339. "Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
  5340. ch->max_tx_rate,
  5341. credits,
  5342. ch->seid);
  5343. }
  5344. /* in case of VF, this will be main SRIOV VSI */
  5345. ch->parent_vsi = vsi;
  5346. /* and update main_vsi's count for queue_available to use */
  5347. vsi->cnt_q_avail -= ch->num_queue_pairs;
  5348. return 0;
  5349. }
  5350. /**
  5351. * i40e_configure_queue_channels - Add queue channel for the given TCs
  5352. * @vsi: VSI to be configured
  5353. *
  5354. * Configures queue channel mapping to the given TCs
  5355. **/
  5356. static int i40e_configure_queue_channels(struct i40e_vsi *vsi)
  5357. {
  5358. struct i40e_channel *ch;
  5359. u64 max_rate = 0;
  5360. int ret = 0, i;
  5361. /* Create app vsi with the TCs. Main VSI with TC0 is already set up */
  5362. vsi->tc_seid_map[0] = vsi->seid;
  5363. for (i = 1; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  5364. if (vsi->tc_config.enabled_tc & BIT(i)) {
  5365. ch = kzalloc(sizeof(*ch), GFP_KERNEL);
  5366. if (!ch) {
  5367. ret = -ENOMEM;
  5368. goto err_free;
  5369. }
  5370. INIT_LIST_HEAD(&ch->list);
  5371. ch->num_queue_pairs =
  5372. vsi->tc_config.tc_info[i].qcount;
  5373. ch->base_queue =
  5374. vsi->tc_config.tc_info[i].qoffset;
  5375. /* Bandwidth limit through tc interface is in bytes/s,
  5376. * change to Mbit/s
  5377. */
  5378. max_rate = vsi->mqprio_qopt.max_rate[i];
  5379. do_div(max_rate, I40E_BW_MBPS_DIVISOR);
  5380. ch->max_tx_rate = max_rate;
  5381. list_add_tail(&ch->list, &vsi->ch_list);
  5382. ret = i40e_create_queue_channel(vsi, ch);
  5383. if (ret) {
  5384. dev_err(&vsi->back->pdev->dev,
  5385. "Failed creating queue channel with TC%d: queues %d\n",
  5386. i, ch->num_queue_pairs);
  5387. goto err_free;
  5388. }
  5389. vsi->tc_seid_map[i] = ch->seid;
  5390. }
  5391. }
  5392. return ret;
  5393. err_free:
  5394. i40e_remove_queue_channels(vsi);
  5395. return ret;
  5396. }
  5397. /**
  5398. * i40e_veb_config_tc - Configure TCs for given VEB
  5399. * @veb: given VEB
  5400. * @enabled_tc: TC bitmap
  5401. *
  5402. * Configures given TC bitmap for VEB (switching) element
  5403. **/
  5404. int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
  5405. {
  5406. struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
  5407. struct i40e_pf *pf = veb->pf;
  5408. int ret = 0;
  5409. int i;
  5410. /* No TCs or already enabled TCs just return */
  5411. if (!enabled_tc || veb->enabled_tc == enabled_tc)
  5412. return ret;
  5413. bw_data.tc_valid_bits = enabled_tc;
  5414. /* bw_data.absolute_credits is not set (relative) */
  5415. /* Enable ETS TCs with equal BW Share for now */
  5416. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  5417. if (enabled_tc & BIT(i))
  5418. bw_data.tc_bw_share_credits[i] = 1;
  5419. }
  5420. ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
  5421. &bw_data, NULL);
  5422. if (ret) {
  5423. dev_info(&pf->pdev->dev,
  5424. "VEB bw config failed, err %s aq_err %s\n",
  5425. i40e_stat_str(&pf->hw, ret),
  5426. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  5427. goto out;
  5428. }
  5429. /* Update the BW information */
  5430. ret = i40e_veb_get_bw_info(veb);
  5431. if (ret) {
  5432. dev_info(&pf->pdev->dev,
  5433. "Failed getting veb bw config, err %s aq_err %s\n",
  5434. i40e_stat_str(&pf->hw, ret),
  5435. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  5436. }
  5437. out:
  5438. return ret;
  5439. }
  5440. #ifdef CONFIG_I40E_DCB
  5441. /**
  5442. * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
  5443. * @pf: PF struct
  5444. *
  5445. * Reconfigure VEB/VSIs on a given PF; it is assumed that
  5446. * the caller would've quiesce all the VSIs before calling
  5447. * this function
  5448. **/
  5449. static void i40e_dcb_reconfigure(struct i40e_pf *pf)
  5450. {
  5451. u8 tc_map = 0;
  5452. int ret;
  5453. u8 v;
  5454. /* Enable the TCs available on PF to all VEBs */
  5455. tc_map = i40e_pf_get_tc_map(pf);
  5456. for (v = 0; v < I40E_MAX_VEB; v++) {
  5457. if (!pf->veb[v])
  5458. continue;
  5459. ret = i40e_veb_config_tc(pf->veb[v], tc_map);
  5460. if (ret) {
  5461. dev_info(&pf->pdev->dev,
  5462. "Failed configuring TC for VEB seid=%d\n",
  5463. pf->veb[v]->seid);
  5464. /* Will try to configure as many components */
  5465. }
  5466. }
  5467. /* Update each VSI */
  5468. for (v = 0; v < pf->num_alloc_vsi; v++) {
  5469. if (!pf->vsi[v])
  5470. continue;
  5471. /* - Enable all TCs for the LAN VSI
  5472. * - For all others keep them at TC0 for now
  5473. */
  5474. if (v == pf->lan_vsi)
  5475. tc_map = i40e_pf_get_tc_map(pf);
  5476. else
  5477. tc_map = I40E_DEFAULT_TRAFFIC_CLASS;
  5478. ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
  5479. if (ret) {
  5480. dev_info(&pf->pdev->dev,
  5481. "Failed configuring TC for VSI seid=%d\n",
  5482. pf->vsi[v]->seid);
  5483. /* Will try to configure as many components */
  5484. } else {
  5485. /* Re-configure VSI vectors based on updated TC map */
  5486. i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
  5487. if (pf->vsi[v]->netdev)
  5488. i40e_dcbnl_set_all(pf->vsi[v]);
  5489. }
  5490. }
  5491. }
  5492. /**
  5493. * i40e_resume_port_tx - Resume port Tx
  5494. * @pf: PF struct
  5495. *
  5496. * Resume a port's Tx and issue a PF reset in case of failure to
  5497. * resume.
  5498. **/
  5499. static int i40e_resume_port_tx(struct i40e_pf *pf)
  5500. {
  5501. struct i40e_hw *hw = &pf->hw;
  5502. int ret;
  5503. ret = i40e_aq_resume_port_tx(hw, NULL);
  5504. if (ret) {
  5505. dev_info(&pf->pdev->dev,
  5506. "Resume Port Tx failed, err %s aq_err %s\n",
  5507. i40e_stat_str(&pf->hw, ret),
  5508. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  5509. /* Schedule PF reset to recover */
  5510. set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
  5511. i40e_service_event_schedule(pf);
  5512. }
  5513. return ret;
  5514. }
  5515. /**
  5516. * i40e_init_pf_dcb - Initialize DCB configuration
  5517. * @pf: PF being configured
  5518. *
  5519. * Query the current DCB configuration and cache it
  5520. * in the hardware structure
  5521. **/
  5522. static int i40e_init_pf_dcb(struct i40e_pf *pf)
  5523. {
  5524. struct i40e_hw *hw = &pf->hw;
  5525. int err = 0;
  5526. /* Do not enable DCB for SW1 and SW2 images even if the FW is capable
  5527. * Also do not enable DCBx if FW LLDP agent is disabled
  5528. */
  5529. if ((pf->hw_features & I40E_HW_NO_DCB_SUPPORT) ||
  5530. (pf->flags & I40E_FLAG_DISABLE_FW_LLDP))
  5531. goto out;
  5532. /* Get the initial DCB configuration */
  5533. err = i40e_init_dcb(hw);
  5534. if (!err) {
  5535. /* Device/Function is not DCBX capable */
  5536. if ((!hw->func_caps.dcb) ||
  5537. (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
  5538. dev_info(&pf->pdev->dev,
  5539. "DCBX offload is not supported or is disabled for this PF.\n");
  5540. } else {
  5541. /* When status is not DISABLED then DCBX in FW */
  5542. pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
  5543. DCB_CAP_DCBX_VER_IEEE;
  5544. pf->flags |= I40E_FLAG_DCB_CAPABLE;
  5545. /* Enable DCB tagging only when more than one TC
  5546. * or explicitly disable if only one TC
  5547. */
  5548. if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
  5549. pf->flags |= I40E_FLAG_DCB_ENABLED;
  5550. else
  5551. pf->flags &= ~I40E_FLAG_DCB_ENABLED;
  5552. dev_dbg(&pf->pdev->dev,
  5553. "DCBX offload is supported for this PF.\n");
  5554. }
  5555. } else if (pf->hw.aq.asq_last_status == I40E_AQ_RC_EPERM) {
  5556. dev_info(&pf->pdev->dev, "FW LLDP disabled for this PF.\n");
  5557. pf->flags |= I40E_FLAG_DISABLE_FW_LLDP;
  5558. } else {
  5559. dev_info(&pf->pdev->dev,
  5560. "Query for DCB configuration failed, err %s aq_err %s\n",
  5561. i40e_stat_str(&pf->hw, err),
  5562. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  5563. }
  5564. out:
  5565. return err;
  5566. }
  5567. #endif /* CONFIG_I40E_DCB */
  5568. #define SPEED_SIZE 14
  5569. #define FC_SIZE 8
  5570. /**
  5571. * i40e_print_link_message - print link up or down
  5572. * @vsi: the VSI for which link needs a message
  5573. * @isup: true of link is up, false otherwise
  5574. */
  5575. void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
  5576. {
  5577. enum i40e_aq_link_speed new_speed;
  5578. struct i40e_pf *pf = vsi->back;
  5579. char *speed = "Unknown";
  5580. char *fc = "Unknown";
  5581. char *fec = "";
  5582. char *req_fec = "";
  5583. char *an = "";
  5584. new_speed = pf->hw.phy.link_info.link_speed;
  5585. if ((vsi->current_isup == isup) && (vsi->current_speed == new_speed))
  5586. return;
  5587. vsi->current_isup = isup;
  5588. vsi->current_speed = new_speed;
  5589. if (!isup) {
  5590. netdev_info(vsi->netdev, "NIC Link is Down\n");
  5591. return;
  5592. }
  5593. /* Warn user if link speed on NPAR enabled partition is not at
  5594. * least 10GB
  5595. */
  5596. if (pf->hw.func_caps.npar_enable &&
  5597. (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
  5598. pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
  5599. netdev_warn(vsi->netdev,
  5600. "The partition detected link speed that is less than 10Gbps\n");
  5601. switch (pf->hw.phy.link_info.link_speed) {
  5602. case I40E_LINK_SPEED_40GB:
  5603. speed = "40 G";
  5604. break;
  5605. case I40E_LINK_SPEED_20GB:
  5606. speed = "20 G";
  5607. break;
  5608. case I40E_LINK_SPEED_25GB:
  5609. speed = "25 G";
  5610. break;
  5611. case I40E_LINK_SPEED_10GB:
  5612. speed = "10 G";
  5613. break;
  5614. case I40E_LINK_SPEED_1GB:
  5615. speed = "1000 M";
  5616. break;
  5617. case I40E_LINK_SPEED_100MB:
  5618. speed = "100 M";
  5619. break;
  5620. default:
  5621. break;
  5622. }
  5623. switch (pf->hw.fc.current_mode) {
  5624. case I40E_FC_FULL:
  5625. fc = "RX/TX";
  5626. break;
  5627. case I40E_FC_TX_PAUSE:
  5628. fc = "TX";
  5629. break;
  5630. case I40E_FC_RX_PAUSE:
  5631. fc = "RX";
  5632. break;
  5633. default:
  5634. fc = "None";
  5635. break;
  5636. }
  5637. if (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_25GB) {
  5638. req_fec = ", Requested FEC: None";
  5639. fec = ", FEC: None";
  5640. an = ", Autoneg: False";
  5641. if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
  5642. an = ", Autoneg: True";
  5643. if (pf->hw.phy.link_info.fec_info &
  5644. I40E_AQ_CONFIG_FEC_KR_ENA)
  5645. fec = ", FEC: CL74 FC-FEC/BASE-R";
  5646. else if (pf->hw.phy.link_info.fec_info &
  5647. I40E_AQ_CONFIG_FEC_RS_ENA)
  5648. fec = ", FEC: CL108 RS-FEC";
  5649. /* 'CL108 RS-FEC' should be displayed when RS is requested, or
  5650. * both RS and FC are requested
  5651. */
  5652. if (vsi->back->hw.phy.link_info.req_fec_info &
  5653. (I40E_AQ_REQUEST_FEC_KR | I40E_AQ_REQUEST_FEC_RS)) {
  5654. if (vsi->back->hw.phy.link_info.req_fec_info &
  5655. I40E_AQ_REQUEST_FEC_RS)
  5656. req_fec = ", Requested FEC: CL108 RS-FEC";
  5657. else
  5658. req_fec = ", Requested FEC: CL74 FC-FEC/BASE-R";
  5659. }
  5660. }
  5661. netdev_info(vsi->netdev, "NIC Link is Up, %sbps Full Duplex%s%s%s, Flow Control: %s\n",
  5662. speed, req_fec, fec, an, fc);
  5663. }
  5664. /**
  5665. * i40e_up_complete - Finish the last steps of bringing up a connection
  5666. * @vsi: the VSI being configured
  5667. **/
  5668. static int i40e_up_complete(struct i40e_vsi *vsi)
  5669. {
  5670. struct i40e_pf *pf = vsi->back;
  5671. int err;
  5672. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  5673. i40e_vsi_configure_msix(vsi);
  5674. else
  5675. i40e_configure_msi_and_legacy(vsi);
  5676. /* start rings */
  5677. err = i40e_vsi_start_rings(vsi);
  5678. if (err)
  5679. return err;
  5680. clear_bit(__I40E_VSI_DOWN, vsi->state);
  5681. i40e_napi_enable_all(vsi);
  5682. i40e_vsi_enable_irq(vsi);
  5683. if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
  5684. (vsi->netdev)) {
  5685. i40e_print_link_message(vsi, true);
  5686. netif_tx_start_all_queues(vsi->netdev);
  5687. netif_carrier_on(vsi->netdev);
  5688. }
  5689. /* replay FDIR SB filters */
  5690. if (vsi->type == I40E_VSI_FDIR) {
  5691. /* reset fd counters */
  5692. pf->fd_add_err = 0;
  5693. pf->fd_atr_cnt = 0;
  5694. i40e_fdir_filter_restore(vsi);
  5695. }
  5696. /* On the next run of the service_task, notify any clients of the new
  5697. * opened netdev
  5698. */
  5699. set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
  5700. i40e_service_event_schedule(pf);
  5701. return 0;
  5702. }
  5703. /**
  5704. * i40e_vsi_reinit_locked - Reset the VSI
  5705. * @vsi: the VSI being configured
  5706. *
  5707. * Rebuild the ring structs after some configuration
  5708. * has changed, e.g. MTU size.
  5709. **/
  5710. static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
  5711. {
  5712. struct i40e_pf *pf = vsi->back;
  5713. WARN_ON(in_interrupt());
  5714. while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state))
  5715. usleep_range(1000, 2000);
  5716. i40e_down(vsi);
  5717. i40e_up(vsi);
  5718. clear_bit(__I40E_CONFIG_BUSY, pf->state);
  5719. }
  5720. /**
  5721. * i40e_up - Bring the connection back up after being down
  5722. * @vsi: the VSI being configured
  5723. **/
  5724. int i40e_up(struct i40e_vsi *vsi)
  5725. {
  5726. int err;
  5727. err = i40e_vsi_configure(vsi);
  5728. if (!err)
  5729. err = i40e_up_complete(vsi);
  5730. return err;
  5731. }
  5732. /**
  5733. * i40e_force_link_state - Force the link status
  5734. * @pf: board private structure
  5735. * @is_up: whether the link state should be forced up or down
  5736. **/
  5737. static i40e_status i40e_force_link_state(struct i40e_pf *pf, bool is_up)
  5738. {
  5739. struct i40e_aq_get_phy_abilities_resp abilities;
  5740. struct i40e_aq_set_phy_config config = {0};
  5741. struct i40e_hw *hw = &pf->hw;
  5742. i40e_status err;
  5743. u64 mask;
  5744. /* Get the current phy config */
  5745. err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities,
  5746. NULL);
  5747. if (err) {
  5748. dev_err(&pf->pdev->dev,
  5749. "failed to get phy cap., ret = %s last_status = %s\n",
  5750. i40e_stat_str(hw, err),
  5751. i40e_aq_str(hw, hw->aq.asq_last_status));
  5752. return err;
  5753. }
  5754. /* If link needs to go up, but was not forced to go down,
  5755. * no need for a flap
  5756. */
  5757. if (is_up && abilities.phy_type != 0)
  5758. return I40E_SUCCESS;
  5759. /* To force link we need to set bits for all supported PHY types,
  5760. * but there are now more than 32, so we need to split the bitmap
  5761. * across two fields.
  5762. */
  5763. mask = I40E_PHY_TYPES_BITMASK;
  5764. config.phy_type = is_up ? cpu_to_le32((u32)(mask & 0xffffffff)) : 0;
  5765. config.phy_type_ext = is_up ? (u8)((mask >> 32) & 0xff) : 0;
  5766. /* Copy the old settings, except of phy_type */
  5767. config.abilities = abilities.abilities;
  5768. config.link_speed = abilities.link_speed;
  5769. config.eee_capability = abilities.eee_capability;
  5770. config.eeer = abilities.eeer_val;
  5771. config.low_power_ctrl = abilities.d3_lpan;
  5772. err = i40e_aq_set_phy_config(hw, &config, NULL);
  5773. if (err) {
  5774. dev_err(&pf->pdev->dev,
  5775. "set phy config ret = %s last_status = %s\n",
  5776. i40e_stat_str(&pf->hw, err),
  5777. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  5778. return err;
  5779. }
  5780. /* Update the link info */
  5781. err = i40e_update_link_info(hw);
  5782. if (err) {
  5783. /* Wait a little bit (on 40G cards it sometimes takes a really
  5784. * long time for link to come back from the atomic reset)
  5785. * and try once more
  5786. */
  5787. msleep(1000);
  5788. i40e_update_link_info(hw);
  5789. }
  5790. i40e_aq_set_link_restart_an(hw, true, NULL);
  5791. return I40E_SUCCESS;
  5792. }
  5793. /**
  5794. * i40e_down - Shutdown the connection processing
  5795. * @vsi: the VSI being stopped
  5796. **/
  5797. void i40e_down(struct i40e_vsi *vsi)
  5798. {
  5799. int i;
  5800. /* It is assumed that the caller of this function
  5801. * sets the vsi->state __I40E_VSI_DOWN bit.
  5802. */
  5803. if (vsi->netdev) {
  5804. netif_carrier_off(vsi->netdev);
  5805. netif_tx_disable(vsi->netdev);
  5806. }
  5807. i40e_vsi_disable_irq(vsi);
  5808. i40e_vsi_stop_rings(vsi);
  5809. if (vsi->type == I40E_VSI_MAIN &&
  5810. vsi->back->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED)
  5811. i40e_force_link_state(vsi->back, false);
  5812. i40e_napi_disable_all(vsi);
  5813. for (i = 0; i < vsi->num_queue_pairs; i++) {
  5814. i40e_clean_tx_ring(vsi->tx_rings[i]);
  5815. if (i40e_enabled_xdp_vsi(vsi))
  5816. i40e_clean_tx_ring(vsi->xdp_rings[i]);
  5817. i40e_clean_rx_ring(vsi->rx_rings[i]);
  5818. }
  5819. }
  5820. /**
  5821. * i40e_validate_mqprio_qopt- validate queue mapping info
  5822. * @vsi: the VSI being configured
  5823. * @mqprio_qopt: queue parametrs
  5824. **/
  5825. static int i40e_validate_mqprio_qopt(struct i40e_vsi *vsi,
  5826. struct tc_mqprio_qopt_offload *mqprio_qopt)
  5827. {
  5828. u64 sum_max_rate = 0;
  5829. u64 max_rate = 0;
  5830. int i;
  5831. if (mqprio_qopt->qopt.offset[0] != 0 ||
  5832. mqprio_qopt->qopt.num_tc < 1 ||
  5833. mqprio_qopt->qopt.num_tc > I40E_MAX_TRAFFIC_CLASS)
  5834. return -EINVAL;
  5835. for (i = 0; ; i++) {
  5836. if (!mqprio_qopt->qopt.count[i])
  5837. return -EINVAL;
  5838. if (mqprio_qopt->min_rate[i]) {
  5839. dev_err(&vsi->back->pdev->dev,
  5840. "Invalid min tx rate (greater than 0) specified\n");
  5841. return -EINVAL;
  5842. }
  5843. max_rate = mqprio_qopt->max_rate[i];
  5844. do_div(max_rate, I40E_BW_MBPS_DIVISOR);
  5845. sum_max_rate += max_rate;
  5846. if (i >= mqprio_qopt->qopt.num_tc - 1)
  5847. break;
  5848. if (mqprio_qopt->qopt.offset[i + 1] !=
  5849. (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i]))
  5850. return -EINVAL;
  5851. }
  5852. if (vsi->num_queue_pairs <
  5853. (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i])) {
  5854. return -EINVAL;
  5855. }
  5856. if (sum_max_rate > i40e_get_link_speed(vsi)) {
  5857. dev_err(&vsi->back->pdev->dev,
  5858. "Invalid max tx rate specified\n");
  5859. return -EINVAL;
  5860. }
  5861. return 0;
  5862. }
  5863. /**
  5864. * i40e_vsi_set_default_tc_config - set default values for tc configuration
  5865. * @vsi: the VSI being configured
  5866. **/
  5867. static void i40e_vsi_set_default_tc_config(struct i40e_vsi *vsi)
  5868. {
  5869. u16 qcount;
  5870. int i;
  5871. /* Only TC0 is enabled */
  5872. vsi->tc_config.numtc = 1;
  5873. vsi->tc_config.enabled_tc = 1;
  5874. qcount = min_t(int, vsi->alloc_queue_pairs,
  5875. i40e_pf_get_max_q_per_tc(vsi->back));
  5876. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  5877. /* For the TC that is not enabled set the offset to to default
  5878. * queue and allocate one queue for the given TC.
  5879. */
  5880. vsi->tc_config.tc_info[i].qoffset = 0;
  5881. if (i == 0)
  5882. vsi->tc_config.tc_info[i].qcount = qcount;
  5883. else
  5884. vsi->tc_config.tc_info[i].qcount = 1;
  5885. vsi->tc_config.tc_info[i].netdev_tc = 0;
  5886. }
  5887. }
  5888. /**
  5889. * i40e_setup_tc - configure multiple traffic classes
  5890. * @netdev: net device to configure
  5891. * @type_data: tc offload data
  5892. **/
  5893. static int i40e_setup_tc(struct net_device *netdev, void *type_data)
  5894. {
  5895. struct tc_mqprio_qopt_offload *mqprio_qopt = type_data;
  5896. struct i40e_netdev_priv *np = netdev_priv(netdev);
  5897. struct i40e_vsi *vsi = np->vsi;
  5898. struct i40e_pf *pf = vsi->back;
  5899. u8 enabled_tc = 0, num_tc, hw;
  5900. bool need_reset = false;
  5901. int ret = -EINVAL;
  5902. u16 mode;
  5903. int i;
  5904. num_tc = mqprio_qopt->qopt.num_tc;
  5905. hw = mqprio_qopt->qopt.hw;
  5906. mode = mqprio_qopt->mode;
  5907. if (!hw) {
  5908. pf->flags &= ~I40E_FLAG_TC_MQPRIO;
  5909. memcpy(&vsi->mqprio_qopt, mqprio_qopt, sizeof(*mqprio_qopt));
  5910. goto config_tc;
  5911. }
  5912. /* Check if MFP enabled */
  5913. if (pf->flags & I40E_FLAG_MFP_ENABLED) {
  5914. netdev_info(netdev,
  5915. "Configuring TC not supported in MFP mode\n");
  5916. return ret;
  5917. }
  5918. switch (mode) {
  5919. case TC_MQPRIO_MODE_DCB:
  5920. pf->flags &= ~I40E_FLAG_TC_MQPRIO;
  5921. /* Check if DCB enabled to continue */
  5922. if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
  5923. netdev_info(netdev,
  5924. "DCB is not enabled for adapter\n");
  5925. return ret;
  5926. }
  5927. /* Check whether tc count is within enabled limit */
  5928. if (num_tc > i40e_pf_get_num_tc(pf)) {
  5929. netdev_info(netdev,
  5930. "TC count greater than enabled on link for adapter\n");
  5931. return ret;
  5932. }
  5933. break;
  5934. case TC_MQPRIO_MODE_CHANNEL:
  5935. if (pf->flags & I40E_FLAG_DCB_ENABLED) {
  5936. netdev_info(netdev,
  5937. "Full offload of TC Mqprio options is not supported when DCB is enabled\n");
  5938. return ret;
  5939. }
  5940. if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
  5941. return ret;
  5942. ret = i40e_validate_mqprio_qopt(vsi, mqprio_qopt);
  5943. if (ret)
  5944. return ret;
  5945. memcpy(&vsi->mqprio_qopt, mqprio_qopt,
  5946. sizeof(*mqprio_qopt));
  5947. pf->flags |= I40E_FLAG_TC_MQPRIO;
  5948. pf->flags &= ~I40E_FLAG_DCB_ENABLED;
  5949. break;
  5950. default:
  5951. return -EINVAL;
  5952. }
  5953. config_tc:
  5954. /* Generate TC map for number of tc requested */
  5955. for (i = 0; i < num_tc; i++)
  5956. enabled_tc |= BIT(i);
  5957. /* Requesting same TC configuration as already enabled */
  5958. if (enabled_tc == vsi->tc_config.enabled_tc &&
  5959. mode != TC_MQPRIO_MODE_CHANNEL)
  5960. return 0;
  5961. /* Quiesce VSI queues */
  5962. i40e_quiesce_vsi(vsi);
  5963. if (!hw && !(pf->flags & I40E_FLAG_TC_MQPRIO))
  5964. i40e_remove_queue_channels(vsi);
  5965. /* Configure VSI for enabled TCs */
  5966. ret = i40e_vsi_config_tc(vsi, enabled_tc);
  5967. if (ret) {
  5968. netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
  5969. vsi->seid);
  5970. need_reset = true;
  5971. goto exit;
  5972. }
  5973. if (pf->flags & I40E_FLAG_TC_MQPRIO) {
  5974. if (vsi->mqprio_qopt.max_rate[0]) {
  5975. u64 max_tx_rate = vsi->mqprio_qopt.max_rate[0];
  5976. do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
  5977. ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
  5978. if (!ret) {
  5979. u64 credits = max_tx_rate;
  5980. do_div(credits, I40E_BW_CREDIT_DIVISOR);
  5981. dev_dbg(&vsi->back->pdev->dev,
  5982. "Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
  5983. max_tx_rate,
  5984. credits,
  5985. vsi->seid);
  5986. } else {
  5987. need_reset = true;
  5988. goto exit;
  5989. }
  5990. }
  5991. ret = i40e_configure_queue_channels(vsi);
  5992. if (ret) {
  5993. netdev_info(netdev,
  5994. "Failed configuring queue channels\n");
  5995. need_reset = true;
  5996. goto exit;
  5997. }
  5998. }
  5999. exit:
  6000. /* Reset the configuration data to defaults, only TC0 is enabled */
  6001. if (need_reset) {
  6002. i40e_vsi_set_default_tc_config(vsi);
  6003. need_reset = false;
  6004. }
  6005. /* Unquiesce VSI */
  6006. i40e_unquiesce_vsi(vsi);
  6007. return ret;
  6008. }
  6009. /**
  6010. * i40e_set_cld_element - sets cloud filter element data
  6011. * @filter: cloud filter rule
  6012. * @cld: ptr to cloud filter element data
  6013. *
  6014. * This is helper function to copy data into cloud filter element
  6015. **/
  6016. static inline void
  6017. i40e_set_cld_element(struct i40e_cloud_filter *filter,
  6018. struct i40e_aqc_cloud_filters_element_data *cld)
  6019. {
  6020. int i, j;
  6021. u32 ipa;
  6022. memset(cld, 0, sizeof(*cld));
  6023. ether_addr_copy(cld->outer_mac, filter->dst_mac);
  6024. ether_addr_copy(cld->inner_mac, filter->src_mac);
  6025. if (filter->n_proto != ETH_P_IP && filter->n_proto != ETH_P_IPV6)
  6026. return;
  6027. if (filter->n_proto == ETH_P_IPV6) {
  6028. #define IPV6_MAX_INDEX (ARRAY_SIZE(filter->dst_ipv6) - 1)
  6029. for (i = 0, j = 0; i < ARRAY_SIZE(filter->dst_ipv6);
  6030. i++, j += 2) {
  6031. ipa = be32_to_cpu(filter->dst_ipv6[IPV6_MAX_INDEX - i]);
  6032. ipa = cpu_to_le32(ipa);
  6033. memcpy(&cld->ipaddr.raw_v6.data[j], &ipa, sizeof(ipa));
  6034. }
  6035. } else {
  6036. ipa = be32_to_cpu(filter->dst_ipv4);
  6037. memcpy(&cld->ipaddr.v4.data, &ipa, sizeof(ipa));
  6038. }
  6039. cld->inner_vlan = cpu_to_le16(ntohs(filter->vlan_id));
  6040. /* tenant_id is not supported by FW now, once the support is enabled
  6041. * fill the cld->tenant_id with cpu_to_le32(filter->tenant_id)
  6042. */
  6043. if (filter->tenant_id)
  6044. return;
  6045. }
  6046. /**
  6047. * i40e_add_del_cloud_filter - Add/del cloud filter
  6048. * @vsi: pointer to VSI
  6049. * @filter: cloud filter rule
  6050. * @add: if true, add, if false, delete
  6051. *
  6052. * Add or delete a cloud filter for a specific flow spec.
  6053. * Returns 0 if the filter were successfully added.
  6054. **/
  6055. int i40e_add_del_cloud_filter(struct i40e_vsi *vsi,
  6056. struct i40e_cloud_filter *filter, bool add)
  6057. {
  6058. struct i40e_aqc_cloud_filters_element_data cld_filter;
  6059. struct i40e_pf *pf = vsi->back;
  6060. int ret;
  6061. static const u16 flag_table[128] = {
  6062. [I40E_CLOUD_FILTER_FLAGS_OMAC] =
  6063. I40E_AQC_ADD_CLOUD_FILTER_OMAC,
  6064. [I40E_CLOUD_FILTER_FLAGS_IMAC] =
  6065. I40E_AQC_ADD_CLOUD_FILTER_IMAC,
  6066. [I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN] =
  6067. I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN,
  6068. [I40E_CLOUD_FILTER_FLAGS_IMAC_TEN_ID] =
  6069. I40E_AQC_ADD_CLOUD_FILTER_IMAC_TEN_ID,
  6070. [I40E_CLOUD_FILTER_FLAGS_OMAC_TEN_ID_IMAC] =
  6071. I40E_AQC_ADD_CLOUD_FILTER_OMAC_TEN_ID_IMAC,
  6072. [I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN_TEN_ID] =
  6073. I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN_TEN_ID,
  6074. [I40E_CLOUD_FILTER_FLAGS_IIP] =
  6075. I40E_AQC_ADD_CLOUD_FILTER_IIP,
  6076. };
  6077. if (filter->flags >= ARRAY_SIZE(flag_table))
  6078. return I40E_ERR_CONFIG;
  6079. /* copy element needed to add cloud filter from filter */
  6080. i40e_set_cld_element(filter, &cld_filter);
  6081. if (filter->tunnel_type != I40E_CLOUD_TNL_TYPE_NONE)
  6082. cld_filter.flags = cpu_to_le16(filter->tunnel_type <<
  6083. I40E_AQC_ADD_CLOUD_TNL_TYPE_SHIFT);
  6084. if (filter->n_proto == ETH_P_IPV6)
  6085. cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
  6086. I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
  6087. else
  6088. cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
  6089. I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
  6090. if (add)
  6091. ret = i40e_aq_add_cloud_filters(&pf->hw, filter->seid,
  6092. &cld_filter, 1);
  6093. else
  6094. ret = i40e_aq_rem_cloud_filters(&pf->hw, filter->seid,
  6095. &cld_filter, 1);
  6096. if (ret)
  6097. dev_dbg(&pf->pdev->dev,
  6098. "Failed to %s cloud filter using l4 port %u, err %d aq_err %d\n",
  6099. add ? "add" : "delete", filter->dst_port, ret,
  6100. pf->hw.aq.asq_last_status);
  6101. else
  6102. dev_info(&pf->pdev->dev,
  6103. "%s cloud filter for VSI: %d\n",
  6104. add ? "Added" : "Deleted", filter->seid);
  6105. return ret;
  6106. }
  6107. /**
  6108. * i40e_add_del_cloud_filter_big_buf - Add/del cloud filter using big_buf
  6109. * @vsi: pointer to VSI
  6110. * @filter: cloud filter rule
  6111. * @add: if true, add, if false, delete
  6112. *
  6113. * Add or delete a cloud filter for a specific flow spec using big buffer.
  6114. * Returns 0 if the filter were successfully added.
  6115. **/
  6116. int i40e_add_del_cloud_filter_big_buf(struct i40e_vsi *vsi,
  6117. struct i40e_cloud_filter *filter,
  6118. bool add)
  6119. {
  6120. struct i40e_aqc_cloud_filters_element_bb cld_filter;
  6121. struct i40e_pf *pf = vsi->back;
  6122. int ret;
  6123. /* Both (src/dst) valid mac_addr are not supported */
  6124. if ((is_valid_ether_addr(filter->dst_mac) &&
  6125. is_valid_ether_addr(filter->src_mac)) ||
  6126. (is_multicast_ether_addr(filter->dst_mac) &&
  6127. is_multicast_ether_addr(filter->src_mac)))
  6128. return -EOPNOTSUPP;
  6129. /* Big buffer cloud filter needs 'L4 port' to be non-zero. Also, UDP
  6130. * ports are not supported via big buffer now.
  6131. */
  6132. if (!filter->dst_port || filter->ip_proto == IPPROTO_UDP)
  6133. return -EOPNOTSUPP;
  6134. /* adding filter using src_port/src_ip is not supported at this stage */
  6135. if (filter->src_port || filter->src_ipv4 ||
  6136. !ipv6_addr_any(&filter->ip.v6.src_ip6))
  6137. return -EOPNOTSUPP;
  6138. /* copy element needed to add cloud filter from filter */
  6139. i40e_set_cld_element(filter, &cld_filter.element);
  6140. if (is_valid_ether_addr(filter->dst_mac) ||
  6141. is_valid_ether_addr(filter->src_mac) ||
  6142. is_multicast_ether_addr(filter->dst_mac) ||
  6143. is_multicast_ether_addr(filter->src_mac)) {
  6144. /* MAC + IP : unsupported mode */
  6145. if (filter->dst_ipv4)
  6146. return -EOPNOTSUPP;
  6147. /* since we validated that L4 port must be valid before
  6148. * we get here, start with respective "flags" value
  6149. * and update if vlan is present or not
  6150. */
  6151. cld_filter.element.flags =
  6152. cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_PORT);
  6153. if (filter->vlan_id) {
  6154. cld_filter.element.flags =
  6155. cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_VLAN_PORT);
  6156. }
  6157. } else if (filter->dst_ipv4 ||
  6158. !ipv6_addr_any(&filter->ip.v6.dst_ip6)) {
  6159. cld_filter.element.flags =
  6160. cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_IP_PORT);
  6161. if (filter->n_proto == ETH_P_IPV6)
  6162. cld_filter.element.flags |=
  6163. cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
  6164. else
  6165. cld_filter.element.flags |=
  6166. cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
  6167. } else {
  6168. dev_err(&pf->pdev->dev,
  6169. "either mac or ip has to be valid for cloud filter\n");
  6170. return -EINVAL;
  6171. }
  6172. /* Now copy L4 port in Byte 6..7 in general fields */
  6173. cld_filter.general_fields[I40E_AQC_ADD_CLOUD_FV_FLU_0X16_WORD0] =
  6174. be16_to_cpu(filter->dst_port);
  6175. if (add) {
  6176. /* Validate current device switch mode, change if necessary */
  6177. ret = i40e_validate_and_set_switch_mode(vsi);
  6178. if (ret) {
  6179. dev_err(&pf->pdev->dev,
  6180. "failed to set switch mode, ret %d\n",
  6181. ret);
  6182. return ret;
  6183. }
  6184. ret = i40e_aq_add_cloud_filters_bb(&pf->hw, filter->seid,
  6185. &cld_filter, 1);
  6186. } else {
  6187. ret = i40e_aq_rem_cloud_filters_bb(&pf->hw, filter->seid,
  6188. &cld_filter, 1);
  6189. }
  6190. if (ret)
  6191. dev_dbg(&pf->pdev->dev,
  6192. "Failed to %s cloud filter(big buffer) err %d aq_err %d\n",
  6193. add ? "add" : "delete", ret, pf->hw.aq.asq_last_status);
  6194. else
  6195. dev_info(&pf->pdev->dev,
  6196. "%s cloud filter for VSI: %d, L4 port: %d\n",
  6197. add ? "add" : "delete", filter->seid,
  6198. ntohs(filter->dst_port));
  6199. return ret;
  6200. }
  6201. /**
  6202. * i40e_parse_cls_flower - Parse tc flower filters provided by kernel
  6203. * @vsi: Pointer to VSI
  6204. * @cls_flower: Pointer to struct tc_cls_flower_offload
  6205. * @filter: Pointer to cloud filter structure
  6206. *
  6207. **/
  6208. static int i40e_parse_cls_flower(struct i40e_vsi *vsi,
  6209. struct tc_cls_flower_offload *f,
  6210. struct i40e_cloud_filter *filter)
  6211. {
  6212. u16 n_proto_mask = 0, n_proto_key = 0, addr_type = 0;
  6213. struct i40e_pf *pf = vsi->back;
  6214. u8 field_flags = 0;
  6215. if (f->dissector->used_keys &
  6216. ~(BIT(FLOW_DISSECTOR_KEY_CONTROL) |
  6217. BIT(FLOW_DISSECTOR_KEY_BASIC) |
  6218. BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
  6219. BIT(FLOW_DISSECTOR_KEY_VLAN) |
  6220. BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
  6221. BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
  6222. BIT(FLOW_DISSECTOR_KEY_PORTS) |
  6223. BIT(FLOW_DISSECTOR_KEY_ENC_KEYID))) {
  6224. dev_err(&pf->pdev->dev, "Unsupported key used: 0x%x\n",
  6225. f->dissector->used_keys);
  6226. return -EOPNOTSUPP;
  6227. }
  6228. if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
  6229. struct flow_dissector_key_keyid *key =
  6230. skb_flow_dissector_target(f->dissector,
  6231. FLOW_DISSECTOR_KEY_ENC_KEYID,
  6232. f->key);
  6233. struct flow_dissector_key_keyid *mask =
  6234. skb_flow_dissector_target(f->dissector,
  6235. FLOW_DISSECTOR_KEY_ENC_KEYID,
  6236. f->mask);
  6237. if (mask->keyid != 0)
  6238. field_flags |= I40E_CLOUD_FIELD_TEN_ID;
  6239. filter->tenant_id = be32_to_cpu(key->keyid);
  6240. }
  6241. if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_BASIC)) {
  6242. struct flow_dissector_key_basic *key =
  6243. skb_flow_dissector_target(f->dissector,
  6244. FLOW_DISSECTOR_KEY_BASIC,
  6245. f->key);
  6246. struct flow_dissector_key_basic *mask =
  6247. skb_flow_dissector_target(f->dissector,
  6248. FLOW_DISSECTOR_KEY_BASIC,
  6249. f->mask);
  6250. n_proto_key = ntohs(key->n_proto);
  6251. n_proto_mask = ntohs(mask->n_proto);
  6252. if (n_proto_key == ETH_P_ALL) {
  6253. n_proto_key = 0;
  6254. n_proto_mask = 0;
  6255. }
  6256. filter->n_proto = n_proto_key & n_proto_mask;
  6257. filter->ip_proto = key->ip_proto;
  6258. }
  6259. if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
  6260. struct flow_dissector_key_eth_addrs *key =
  6261. skb_flow_dissector_target(f->dissector,
  6262. FLOW_DISSECTOR_KEY_ETH_ADDRS,
  6263. f->key);
  6264. struct flow_dissector_key_eth_addrs *mask =
  6265. skb_flow_dissector_target(f->dissector,
  6266. FLOW_DISSECTOR_KEY_ETH_ADDRS,
  6267. f->mask);
  6268. /* use is_broadcast and is_zero to check for all 0xf or 0 */
  6269. if (!is_zero_ether_addr(mask->dst)) {
  6270. if (is_broadcast_ether_addr(mask->dst)) {
  6271. field_flags |= I40E_CLOUD_FIELD_OMAC;
  6272. } else {
  6273. dev_err(&pf->pdev->dev, "Bad ether dest mask %pM\n",
  6274. mask->dst);
  6275. return I40E_ERR_CONFIG;
  6276. }
  6277. }
  6278. if (!is_zero_ether_addr(mask->src)) {
  6279. if (is_broadcast_ether_addr(mask->src)) {
  6280. field_flags |= I40E_CLOUD_FIELD_IMAC;
  6281. } else {
  6282. dev_err(&pf->pdev->dev, "Bad ether src mask %pM\n",
  6283. mask->src);
  6284. return I40E_ERR_CONFIG;
  6285. }
  6286. }
  6287. ether_addr_copy(filter->dst_mac, key->dst);
  6288. ether_addr_copy(filter->src_mac, key->src);
  6289. }
  6290. if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_VLAN)) {
  6291. struct flow_dissector_key_vlan *key =
  6292. skb_flow_dissector_target(f->dissector,
  6293. FLOW_DISSECTOR_KEY_VLAN,
  6294. f->key);
  6295. struct flow_dissector_key_vlan *mask =
  6296. skb_flow_dissector_target(f->dissector,
  6297. FLOW_DISSECTOR_KEY_VLAN,
  6298. f->mask);
  6299. if (mask->vlan_id) {
  6300. if (mask->vlan_id == VLAN_VID_MASK) {
  6301. field_flags |= I40E_CLOUD_FIELD_IVLAN;
  6302. } else {
  6303. dev_err(&pf->pdev->dev, "Bad vlan mask 0x%04x\n",
  6304. mask->vlan_id);
  6305. return I40E_ERR_CONFIG;
  6306. }
  6307. }
  6308. filter->vlan_id = cpu_to_be16(key->vlan_id);
  6309. }
  6310. if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_CONTROL)) {
  6311. struct flow_dissector_key_control *key =
  6312. skb_flow_dissector_target(f->dissector,
  6313. FLOW_DISSECTOR_KEY_CONTROL,
  6314. f->key);
  6315. addr_type = key->addr_type;
  6316. }
  6317. if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
  6318. struct flow_dissector_key_ipv4_addrs *key =
  6319. skb_flow_dissector_target(f->dissector,
  6320. FLOW_DISSECTOR_KEY_IPV4_ADDRS,
  6321. f->key);
  6322. struct flow_dissector_key_ipv4_addrs *mask =
  6323. skb_flow_dissector_target(f->dissector,
  6324. FLOW_DISSECTOR_KEY_IPV4_ADDRS,
  6325. f->mask);
  6326. if (mask->dst) {
  6327. if (mask->dst == cpu_to_be32(0xffffffff)) {
  6328. field_flags |= I40E_CLOUD_FIELD_IIP;
  6329. } else {
  6330. dev_err(&pf->pdev->dev, "Bad ip dst mask %pI4b\n",
  6331. &mask->dst);
  6332. return I40E_ERR_CONFIG;
  6333. }
  6334. }
  6335. if (mask->src) {
  6336. if (mask->src == cpu_to_be32(0xffffffff)) {
  6337. field_flags |= I40E_CLOUD_FIELD_IIP;
  6338. } else {
  6339. dev_err(&pf->pdev->dev, "Bad ip src mask %pI4b\n",
  6340. &mask->src);
  6341. return I40E_ERR_CONFIG;
  6342. }
  6343. }
  6344. if (field_flags & I40E_CLOUD_FIELD_TEN_ID) {
  6345. dev_err(&pf->pdev->dev, "Tenant id not allowed for ip filter\n");
  6346. return I40E_ERR_CONFIG;
  6347. }
  6348. filter->dst_ipv4 = key->dst;
  6349. filter->src_ipv4 = key->src;
  6350. }
  6351. if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
  6352. struct flow_dissector_key_ipv6_addrs *key =
  6353. skb_flow_dissector_target(f->dissector,
  6354. FLOW_DISSECTOR_KEY_IPV6_ADDRS,
  6355. f->key);
  6356. struct flow_dissector_key_ipv6_addrs *mask =
  6357. skb_flow_dissector_target(f->dissector,
  6358. FLOW_DISSECTOR_KEY_IPV6_ADDRS,
  6359. f->mask);
  6360. /* src and dest IPV6 address should not be LOOPBACK
  6361. * (0:0:0:0:0:0:0:1), which can be represented as ::1
  6362. */
  6363. if (ipv6_addr_loopback(&key->dst) ||
  6364. ipv6_addr_loopback(&key->src)) {
  6365. dev_err(&pf->pdev->dev,
  6366. "Bad ipv6, addr is LOOPBACK\n");
  6367. return I40E_ERR_CONFIG;
  6368. }
  6369. if (!ipv6_addr_any(&mask->dst) || !ipv6_addr_any(&mask->src))
  6370. field_flags |= I40E_CLOUD_FIELD_IIP;
  6371. memcpy(&filter->src_ipv6, &key->src.s6_addr32,
  6372. sizeof(filter->src_ipv6));
  6373. memcpy(&filter->dst_ipv6, &key->dst.s6_addr32,
  6374. sizeof(filter->dst_ipv6));
  6375. }
  6376. if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_PORTS)) {
  6377. struct flow_dissector_key_ports *key =
  6378. skb_flow_dissector_target(f->dissector,
  6379. FLOW_DISSECTOR_KEY_PORTS,
  6380. f->key);
  6381. struct flow_dissector_key_ports *mask =
  6382. skb_flow_dissector_target(f->dissector,
  6383. FLOW_DISSECTOR_KEY_PORTS,
  6384. f->mask);
  6385. if (mask->src) {
  6386. if (mask->src == cpu_to_be16(0xffff)) {
  6387. field_flags |= I40E_CLOUD_FIELD_IIP;
  6388. } else {
  6389. dev_err(&pf->pdev->dev, "Bad src port mask 0x%04x\n",
  6390. be16_to_cpu(mask->src));
  6391. return I40E_ERR_CONFIG;
  6392. }
  6393. }
  6394. if (mask->dst) {
  6395. if (mask->dst == cpu_to_be16(0xffff)) {
  6396. field_flags |= I40E_CLOUD_FIELD_IIP;
  6397. } else {
  6398. dev_err(&pf->pdev->dev, "Bad dst port mask 0x%04x\n",
  6399. be16_to_cpu(mask->dst));
  6400. return I40E_ERR_CONFIG;
  6401. }
  6402. }
  6403. filter->dst_port = key->dst;
  6404. filter->src_port = key->src;
  6405. switch (filter->ip_proto) {
  6406. case IPPROTO_TCP:
  6407. case IPPROTO_UDP:
  6408. break;
  6409. default:
  6410. dev_err(&pf->pdev->dev,
  6411. "Only UDP and TCP transport are supported\n");
  6412. return -EINVAL;
  6413. }
  6414. }
  6415. filter->flags = field_flags;
  6416. return 0;
  6417. }
  6418. /**
  6419. * i40e_handle_tclass: Forward to a traffic class on the device
  6420. * @vsi: Pointer to VSI
  6421. * @tc: traffic class index on the device
  6422. * @filter: Pointer to cloud filter structure
  6423. *
  6424. **/
  6425. static int i40e_handle_tclass(struct i40e_vsi *vsi, u32 tc,
  6426. struct i40e_cloud_filter *filter)
  6427. {
  6428. struct i40e_channel *ch, *ch_tmp;
  6429. /* direct to a traffic class on the same device */
  6430. if (tc == 0) {
  6431. filter->seid = vsi->seid;
  6432. return 0;
  6433. } else if (vsi->tc_config.enabled_tc & BIT(tc)) {
  6434. if (!filter->dst_port) {
  6435. dev_err(&vsi->back->pdev->dev,
  6436. "Specify destination port to direct to traffic class that is not default\n");
  6437. return -EINVAL;
  6438. }
  6439. if (list_empty(&vsi->ch_list))
  6440. return -EINVAL;
  6441. list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list,
  6442. list) {
  6443. if (ch->seid == vsi->tc_seid_map[tc])
  6444. filter->seid = ch->seid;
  6445. }
  6446. return 0;
  6447. }
  6448. dev_err(&vsi->back->pdev->dev, "TC is not enabled\n");
  6449. return -EINVAL;
  6450. }
  6451. /**
  6452. * i40e_configure_clsflower - Configure tc flower filters
  6453. * @vsi: Pointer to VSI
  6454. * @cls_flower: Pointer to struct tc_cls_flower_offload
  6455. *
  6456. **/
  6457. static int i40e_configure_clsflower(struct i40e_vsi *vsi,
  6458. struct tc_cls_flower_offload *cls_flower)
  6459. {
  6460. int tc = tc_classid_to_hwtc(vsi->netdev, cls_flower->classid);
  6461. struct i40e_cloud_filter *filter = NULL;
  6462. struct i40e_pf *pf = vsi->back;
  6463. int err = 0;
  6464. if (tc < 0) {
  6465. dev_err(&vsi->back->pdev->dev, "Invalid traffic class\n");
  6466. return -EOPNOTSUPP;
  6467. }
  6468. if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
  6469. test_bit(__I40E_RESET_INTR_RECEIVED, pf->state))
  6470. return -EBUSY;
  6471. if (pf->fdir_pf_active_filters ||
  6472. (!hlist_empty(&pf->fdir_filter_list))) {
  6473. dev_err(&vsi->back->pdev->dev,
  6474. "Flow Director Sideband filters exists, turn ntuple off to configure cloud filters\n");
  6475. return -EINVAL;
  6476. }
  6477. if (vsi->back->flags & I40E_FLAG_FD_SB_ENABLED) {
  6478. dev_err(&vsi->back->pdev->dev,
  6479. "Disable Flow Director Sideband, configuring Cloud filters via tc-flower\n");
  6480. vsi->back->flags &= ~I40E_FLAG_FD_SB_ENABLED;
  6481. vsi->back->flags |= I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
  6482. }
  6483. filter = kzalloc(sizeof(*filter), GFP_KERNEL);
  6484. if (!filter)
  6485. return -ENOMEM;
  6486. filter->cookie = cls_flower->cookie;
  6487. err = i40e_parse_cls_flower(vsi, cls_flower, filter);
  6488. if (err < 0)
  6489. goto err;
  6490. err = i40e_handle_tclass(vsi, tc, filter);
  6491. if (err < 0)
  6492. goto err;
  6493. /* Add cloud filter */
  6494. if (filter->dst_port)
  6495. err = i40e_add_del_cloud_filter_big_buf(vsi, filter, true);
  6496. else
  6497. err = i40e_add_del_cloud_filter(vsi, filter, true);
  6498. if (err) {
  6499. dev_err(&pf->pdev->dev,
  6500. "Failed to add cloud filter, err %s\n",
  6501. i40e_stat_str(&pf->hw, err));
  6502. goto err;
  6503. }
  6504. /* add filter to the ordered list */
  6505. INIT_HLIST_NODE(&filter->cloud_node);
  6506. hlist_add_head(&filter->cloud_node, &pf->cloud_filter_list);
  6507. pf->num_cloud_filters++;
  6508. return err;
  6509. err:
  6510. kfree(filter);
  6511. return err;
  6512. }
  6513. /**
  6514. * i40e_find_cloud_filter - Find the could filter in the list
  6515. * @vsi: Pointer to VSI
  6516. * @cookie: filter specific cookie
  6517. *
  6518. **/
  6519. static struct i40e_cloud_filter *i40e_find_cloud_filter(struct i40e_vsi *vsi,
  6520. unsigned long *cookie)
  6521. {
  6522. struct i40e_cloud_filter *filter = NULL;
  6523. struct hlist_node *node2;
  6524. hlist_for_each_entry_safe(filter, node2,
  6525. &vsi->back->cloud_filter_list, cloud_node)
  6526. if (!memcmp(cookie, &filter->cookie, sizeof(filter->cookie)))
  6527. return filter;
  6528. return NULL;
  6529. }
  6530. /**
  6531. * i40e_delete_clsflower - Remove tc flower filters
  6532. * @vsi: Pointer to VSI
  6533. * @cls_flower: Pointer to struct tc_cls_flower_offload
  6534. *
  6535. **/
  6536. static int i40e_delete_clsflower(struct i40e_vsi *vsi,
  6537. struct tc_cls_flower_offload *cls_flower)
  6538. {
  6539. struct i40e_cloud_filter *filter = NULL;
  6540. struct i40e_pf *pf = vsi->back;
  6541. int err = 0;
  6542. filter = i40e_find_cloud_filter(vsi, &cls_flower->cookie);
  6543. if (!filter)
  6544. return -EINVAL;
  6545. hash_del(&filter->cloud_node);
  6546. if (filter->dst_port)
  6547. err = i40e_add_del_cloud_filter_big_buf(vsi, filter, false);
  6548. else
  6549. err = i40e_add_del_cloud_filter(vsi, filter, false);
  6550. kfree(filter);
  6551. if (err) {
  6552. dev_err(&pf->pdev->dev,
  6553. "Failed to delete cloud filter, err %s\n",
  6554. i40e_stat_str(&pf->hw, err));
  6555. return i40e_aq_rc_to_posix(err, pf->hw.aq.asq_last_status);
  6556. }
  6557. pf->num_cloud_filters--;
  6558. if (!pf->num_cloud_filters)
  6559. if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
  6560. !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
  6561. pf->flags |= I40E_FLAG_FD_SB_ENABLED;
  6562. pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
  6563. pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
  6564. }
  6565. return 0;
  6566. }
  6567. /**
  6568. * i40e_setup_tc_cls_flower - flower classifier offloads
  6569. * @netdev: net device to configure
  6570. * @type_data: offload data
  6571. **/
  6572. static int i40e_setup_tc_cls_flower(struct i40e_netdev_priv *np,
  6573. struct tc_cls_flower_offload *cls_flower)
  6574. {
  6575. struct i40e_vsi *vsi = np->vsi;
  6576. switch (cls_flower->command) {
  6577. case TC_CLSFLOWER_REPLACE:
  6578. return i40e_configure_clsflower(vsi, cls_flower);
  6579. case TC_CLSFLOWER_DESTROY:
  6580. return i40e_delete_clsflower(vsi, cls_flower);
  6581. case TC_CLSFLOWER_STATS:
  6582. return -EOPNOTSUPP;
  6583. default:
  6584. return -EINVAL;
  6585. }
  6586. }
  6587. static int i40e_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
  6588. void *cb_priv)
  6589. {
  6590. struct i40e_netdev_priv *np = cb_priv;
  6591. if (!tc_cls_can_offload_and_chain0(np->vsi->netdev, type_data))
  6592. return -EOPNOTSUPP;
  6593. switch (type) {
  6594. case TC_SETUP_CLSFLOWER:
  6595. return i40e_setup_tc_cls_flower(np, type_data);
  6596. default:
  6597. return -EOPNOTSUPP;
  6598. }
  6599. }
  6600. static int i40e_setup_tc_block(struct net_device *dev,
  6601. struct tc_block_offload *f)
  6602. {
  6603. struct i40e_netdev_priv *np = netdev_priv(dev);
  6604. if (f->binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
  6605. return -EOPNOTSUPP;
  6606. switch (f->command) {
  6607. case TC_BLOCK_BIND:
  6608. return tcf_block_cb_register(f->block, i40e_setup_tc_block_cb,
  6609. np, np);
  6610. case TC_BLOCK_UNBIND:
  6611. tcf_block_cb_unregister(f->block, i40e_setup_tc_block_cb, np);
  6612. return 0;
  6613. default:
  6614. return -EOPNOTSUPP;
  6615. }
  6616. }
  6617. static int __i40e_setup_tc(struct net_device *netdev, enum tc_setup_type type,
  6618. void *type_data)
  6619. {
  6620. switch (type) {
  6621. case TC_SETUP_QDISC_MQPRIO:
  6622. return i40e_setup_tc(netdev, type_data);
  6623. case TC_SETUP_BLOCK:
  6624. return i40e_setup_tc_block(netdev, type_data);
  6625. default:
  6626. return -EOPNOTSUPP;
  6627. }
  6628. }
  6629. /**
  6630. * i40e_open - Called when a network interface is made active
  6631. * @netdev: network interface device structure
  6632. *
  6633. * The open entry point is called when a network interface is made
  6634. * active by the system (IFF_UP). At this point all resources needed
  6635. * for transmit and receive operations are allocated, the interrupt
  6636. * handler is registered with the OS, the netdev watchdog subtask is
  6637. * enabled, and the stack is notified that the interface is ready.
  6638. *
  6639. * Returns 0 on success, negative value on failure
  6640. **/
  6641. int i40e_open(struct net_device *netdev)
  6642. {
  6643. struct i40e_netdev_priv *np = netdev_priv(netdev);
  6644. struct i40e_vsi *vsi = np->vsi;
  6645. struct i40e_pf *pf = vsi->back;
  6646. int err;
  6647. /* disallow open during test or if eeprom is broken */
  6648. if (test_bit(__I40E_TESTING, pf->state) ||
  6649. test_bit(__I40E_BAD_EEPROM, pf->state))
  6650. return -EBUSY;
  6651. netif_carrier_off(netdev);
  6652. if (i40e_force_link_state(pf, true))
  6653. return -EAGAIN;
  6654. err = i40e_vsi_open(vsi);
  6655. if (err)
  6656. return err;
  6657. /* configure global TSO hardware offload settings */
  6658. wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
  6659. TCP_FLAG_FIN) >> 16);
  6660. wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
  6661. TCP_FLAG_FIN |
  6662. TCP_FLAG_CWR) >> 16);
  6663. wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
  6664. udp_tunnel_get_rx_info(netdev);
  6665. return 0;
  6666. }
  6667. /**
  6668. * i40e_vsi_open -
  6669. * @vsi: the VSI to open
  6670. *
  6671. * Finish initialization of the VSI.
  6672. *
  6673. * Returns 0 on success, negative value on failure
  6674. *
  6675. * Note: expects to be called while under rtnl_lock()
  6676. **/
  6677. int i40e_vsi_open(struct i40e_vsi *vsi)
  6678. {
  6679. struct i40e_pf *pf = vsi->back;
  6680. char int_name[I40E_INT_NAME_STR_LEN];
  6681. int err;
  6682. /* allocate descriptors */
  6683. err = i40e_vsi_setup_tx_resources(vsi);
  6684. if (err)
  6685. goto err_setup_tx;
  6686. err = i40e_vsi_setup_rx_resources(vsi);
  6687. if (err)
  6688. goto err_setup_rx;
  6689. err = i40e_vsi_configure(vsi);
  6690. if (err)
  6691. goto err_setup_rx;
  6692. if (vsi->netdev) {
  6693. snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
  6694. dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
  6695. err = i40e_vsi_request_irq(vsi, int_name);
  6696. if (err)
  6697. goto err_setup_rx;
  6698. /* Notify the stack of the actual queue counts. */
  6699. err = netif_set_real_num_tx_queues(vsi->netdev,
  6700. vsi->num_queue_pairs);
  6701. if (err)
  6702. goto err_set_queues;
  6703. err = netif_set_real_num_rx_queues(vsi->netdev,
  6704. vsi->num_queue_pairs);
  6705. if (err)
  6706. goto err_set_queues;
  6707. } else if (vsi->type == I40E_VSI_FDIR) {
  6708. snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
  6709. dev_driver_string(&pf->pdev->dev),
  6710. dev_name(&pf->pdev->dev));
  6711. err = i40e_vsi_request_irq(vsi, int_name);
  6712. } else {
  6713. err = -EINVAL;
  6714. goto err_setup_rx;
  6715. }
  6716. err = i40e_up_complete(vsi);
  6717. if (err)
  6718. goto err_up_complete;
  6719. return 0;
  6720. err_up_complete:
  6721. i40e_down(vsi);
  6722. err_set_queues:
  6723. i40e_vsi_free_irq(vsi);
  6724. err_setup_rx:
  6725. i40e_vsi_free_rx_resources(vsi);
  6726. err_setup_tx:
  6727. i40e_vsi_free_tx_resources(vsi);
  6728. if (vsi == pf->vsi[pf->lan_vsi])
  6729. i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
  6730. return err;
  6731. }
  6732. /**
  6733. * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
  6734. * @pf: Pointer to PF
  6735. *
  6736. * This function destroys the hlist where all the Flow Director
  6737. * filters were saved.
  6738. **/
  6739. static void i40e_fdir_filter_exit(struct i40e_pf *pf)
  6740. {
  6741. struct i40e_fdir_filter *filter;
  6742. struct i40e_flex_pit *pit_entry, *tmp;
  6743. struct hlist_node *node2;
  6744. hlist_for_each_entry_safe(filter, node2,
  6745. &pf->fdir_filter_list, fdir_node) {
  6746. hlist_del(&filter->fdir_node);
  6747. kfree(filter);
  6748. }
  6749. list_for_each_entry_safe(pit_entry, tmp, &pf->l3_flex_pit_list, list) {
  6750. list_del(&pit_entry->list);
  6751. kfree(pit_entry);
  6752. }
  6753. INIT_LIST_HEAD(&pf->l3_flex_pit_list);
  6754. list_for_each_entry_safe(pit_entry, tmp, &pf->l4_flex_pit_list, list) {
  6755. list_del(&pit_entry->list);
  6756. kfree(pit_entry);
  6757. }
  6758. INIT_LIST_HEAD(&pf->l4_flex_pit_list);
  6759. pf->fdir_pf_active_filters = 0;
  6760. pf->fd_tcp4_filter_cnt = 0;
  6761. pf->fd_udp4_filter_cnt = 0;
  6762. pf->fd_sctp4_filter_cnt = 0;
  6763. pf->fd_ip4_filter_cnt = 0;
  6764. /* Reprogram the default input set for TCP/IPv4 */
  6765. i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
  6766. I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
  6767. I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
  6768. /* Reprogram the default input set for UDP/IPv4 */
  6769. i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_UDP,
  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 SCTP/IPv4 */
  6773. i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_SCTP,
  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 Other/IPv4 */
  6777. i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_OTHER,
  6778. I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
  6779. i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_FRAG_IPV4,
  6780. I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
  6781. }
  6782. /**
  6783. * i40e_cloud_filter_exit - Cleans up the cloud filters
  6784. * @pf: Pointer to PF
  6785. *
  6786. * This function destroys the hlist where all the cloud filters
  6787. * were saved.
  6788. **/
  6789. static void i40e_cloud_filter_exit(struct i40e_pf *pf)
  6790. {
  6791. struct i40e_cloud_filter *cfilter;
  6792. struct hlist_node *node;
  6793. hlist_for_each_entry_safe(cfilter, node,
  6794. &pf->cloud_filter_list, cloud_node) {
  6795. hlist_del(&cfilter->cloud_node);
  6796. kfree(cfilter);
  6797. }
  6798. pf->num_cloud_filters = 0;
  6799. if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
  6800. !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
  6801. pf->flags |= I40E_FLAG_FD_SB_ENABLED;
  6802. pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
  6803. pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
  6804. }
  6805. }
  6806. /**
  6807. * i40e_close - Disables a network interface
  6808. * @netdev: network interface device structure
  6809. *
  6810. * The close entry point is called when an interface is de-activated
  6811. * by the OS. The hardware is still under the driver's control, but
  6812. * this netdev interface is disabled.
  6813. *
  6814. * Returns 0, this is not allowed to fail
  6815. **/
  6816. int i40e_close(struct net_device *netdev)
  6817. {
  6818. struct i40e_netdev_priv *np = netdev_priv(netdev);
  6819. struct i40e_vsi *vsi = np->vsi;
  6820. i40e_vsi_close(vsi);
  6821. return 0;
  6822. }
  6823. /**
  6824. * i40e_do_reset - Start a PF or Core Reset sequence
  6825. * @pf: board private structure
  6826. * @reset_flags: which reset is requested
  6827. * @lock_acquired: indicates whether or not the lock has been acquired
  6828. * before this function was called.
  6829. *
  6830. * The essential difference in resets is that the PF Reset
  6831. * doesn't clear the packet buffers, doesn't reset the PE
  6832. * firmware, and doesn't bother the other PFs on the chip.
  6833. **/
  6834. void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags, bool lock_acquired)
  6835. {
  6836. u32 val;
  6837. WARN_ON(in_interrupt());
  6838. /* do the biggest reset indicated */
  6839. if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
  6840. /* Request a Global Reset
  6841. *
  6842. * This will start the chip's countdown to the actual full
  6843. * chip reset event, and a warning interrupt to be sent
  6844. * to all PFs, including the requestor. Our handler
  6845. * for the warning interrupt will deal with the shutdown
  6846. * and recovery of the switch setup.
  6847. */
  6848. dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
  6849. val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
  6850. val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
  6851. wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
  6852. } else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
  6853. /* Request a Core Reset
  6854. *
  6855. * Same as Global Reset, except does *not* include the MAC/PHY
  6856. */
  6857. dev_dbg(&pf->pdev->dev, "CoreR requested\n");
  6858. val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
  6859. val |= I40E_GLGEN_RTRIG_CORER_MASK;
  6860. wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
  6861. i40e_flush(&pf->hw);
  6862. } else if (reset_flags & I40E_PF_RESET_FLAG) {
  6863. /* Request a PF Reset
  6864. *
  6865. * Resets only the PF-specific registers
  6866. *
  6867. * This goes directly to the tear-down and rebuild of
  6868. * the switch, since we need to do all the recovery as
  6869. * for the Core Reset.
  6870. */
  6871. dev_dbg(&pf->pdev->dev, "PFR requested\n");
  6872. i40e_handle_reset_warning(pf, lock_acquired);
  6873. } else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
  6874. int v;
  6875. /* Find the VSI(s) that requested a re-init */
  6876. dev_info(&pf->pdev->dev,
  6877. "VSI reinit requested\n");
  6878. for (v = 0; v < pf->num_alloc_vsi; v++) {
  6879. struct i40e_vsi *vsi = pf->vsi[v];
  6880. if (vsi != NULL &&
  6881. test_and_clear_bit(__I40E_VSI_REINIT_REQUESTED,
  6882. vsi->state))
  6883. i40e_vsi_reinit_locked(pf->vsi[v]);
  6884. }
  6885. } else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
  6886. int v;
  6887. /* Find the VSI(s) that needs to be brought down */
  6888. dev_info(&pf->pdev->dev, "VSI down requested\n");
  6889. for (v = 0; v < pf->num_alloc_vsi; v++) {
  6890. struct i40e_vsi *vsi = pf->vsi[v];
  6891. if (vsi != NULL &&
  6892. test_and_clear_bit(__I40E_VSI_DOWN_REQUESTED,
  6893. vsi->state)) {
  6894. set_bit(__I40E_VSI_DOWN, vsi->state);
  6895. i40e_down(vsi);
  6896. }
  6897. }
  6898. } else {
  6899. dev_info(&pf->pdev->dev,
  6900. "bad reset request 0x%08x\n", reset_flags);
  6901. }
  6902. }
  6903. #ifdef CONFIG_I40E_DCB
  6904. /**
  6905. * i40e_dcb_need_reconfig - Check if DCB needs reconfig
  6906. * @pf: board private structure
  6907. * @old_cfg: current DCB config
  6908. * @new_cfg: new DCB config
  6909. **/
  6910. bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
  6911. struct i40e_dcbx_config *old_cfg,
  6912. struct i40e_dcbx_config *new_cfg)
  6913. {
  6914. bool need_reconfig = false;
  6915. /* Check if ETS configuration has changed */
  6916. if (memcmp(&new_cfg->etscfg,
  6917. &old_cfg->etscfg,
  6918. sizeof(new_cfg->etscfg))) {
  6919. /* If Priority Table has changed reconfig is needed */
  6920. if (memcmp(&new_cfg->etscfg.prioritytable,
  6921. &old_cfg->etscfg.prioritytable,
  6922. sizeof(new_cfg->etscfg.prioritytable))) {
  6923. need_reconfig = true;
  6924. dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
  6925. }
  6926. if (memcmp(&new_cfg->etscfg.tcbwtable,
  6927. &old_cfg->etscfg.tcbwtable,
  6928. sizeof(new_cfg->etscfg.tcbwtable)))
  6929. dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
  6930. if (memcmp(&new_cfg->etscfg.tsatable,
  6931. &old_cfg->etscfg.tsatable,
  6932. sizeof(new_cfg->etscfg.tsatable)))
  6933. dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
  6934. }
  6935. /* Check if PFC configuration has changed */
  6936. if (memcmp(&new_cfg->pfc,
  6937. &old_cfg->pfc,
  6938. sizeof(new_cfg->pfc))) {
  6939. need_reconfig = true;
  6940. dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
  6941. }
  6942. /* Check if APP Table has changed */
  6943. if (memcmp(&new_cfg->app,
  6944. &old_cfg->app,
  6945. sizeof(new_cfg->app))) {
  6946. need_reconfig = true;
  6947. dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
  6948. }
  6949. dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
  6950. return need_reconfig;
  6951. }
  6952. /**
  6953. * i40e_handle_lldp_event - Handle LLDP Change MIB event
  6954. * @pf: board private structure
  6955. * @e: event info posted on ARQ
  6956. **/
  6957. static int i40e_handle_lldp_event(struct i40e_pf *pf,
  6958. struct i40e_arq_event_info *e)
  6959. {
  6960. struct i40e_aqc_lldp_get_mib *mib =
  6961. (struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
  6962. struct i40e_hw *hw = &pf->hw;
  6963. struct i40e_dcbx_config tmp_dcbx_cfg;
  6964. bool need_reconfig = false;
  6965. int ret = 0;
  6966. u8 type;
  6967. /* Not DCB capable or capability disabled */
  6968. if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
  6969. return ret;
  6970. /* Ignore if event is not for Nearest Bridge */
  6971. type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
  6972. & I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
  6973. dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
  6974. if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
  6975. return ret;
  6976. /* Check MIB Type and return if event for Remote MIB update */
  6977. type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
  6978. dev_dbg(&pf->pdev->dev,
  6979. "LLDP event mib type %s\n", type ? "remote" : "local");
  6980. if (type == I40E_AQ_LLDP_MIB_REMOTE) {
  6981. /* Update the remote cached instance and return */
  6982. ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
  6983. I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
  6984. &hw->remote_dcbx_config);
  6985. goto exit;
  6986. }
  6987. /* Store the old configuration */
  6988. tmp_dcbx_cfg = hw->local_dcbx_config;
  6989. /* Reset the old DCBx configuration data */
  6990. memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
  6991. /* Get updated DCBX data from firmware */
  6992. ret = i40e_get_dcb_config(&pf->hw);
  6993. if (ret) {
  6994. dev_info(&pf->pdev->dev,
  6995. "Failed querying DCB configuration data from firmware, err %s aq_err %s\n",
  6996. i40e_stat_str(&pf->hw, ret),
  6997. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  6998. goto exit;
  6999. }
  7000. /* No change detected in DCBX configs */
  7001. if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
  7002. sizeof(tmp_dcbx_cfg))) {
  7003. dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
  7004. goto exit;
  7005. }
  7006. need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
  7007. &hw->local_dcbx_config);
  7008. i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
  7009. if (!need_reconfig)
  7010. goto exit;
  7011. /* Enable DCB tagging only when more than one TC */
  7012. if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
  7013. pf->flags |= I40E_FLAG_DCB_ENABLED;
  7014. else
  7015. pf->flags &= ~I40E_FLAG_DCB_ENABLED;
  7016. set_bit(__I40E_PORT_SUSPENDED, pf->state);
  7017. /* Reconfiguration needed quiesce all VSIs */
  7018. i40e_pf_quiesce_all_vsi(pf);
  7019. /* Changes in configuration update VEB/VSI */
  7020. i40e_dcb_reconfigure(pf);
  7021. ret = i40e_resume_port_tx(pf);
  7022. clear_bit(__I40E_PORT_SUSPENDED, pf->state);
  7023. /* In case of error no point in resuming VSIs */
  7024. if (ret)
  7025. goto exit;
  7026. /* Wait for the PF's queues to be disabled */
  7027. ret = i40e_pf_wait_queues_disabled(pf);
  7028. if (ret) {
  7029. /* Schedule PF reset to recover */
  7030. set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
  7031. i40e_service_event_schedule(pf);
  7032. } else {
  7033. i40e_pf_unquiesce_all_vsi(pf);
  7034. set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
  7035. set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
  7036. }
  7037. exit:
  7038. return ret;
  7039. }
  7040. #endif /* CONFIG_I40E_DCB */
  7041. /**
  7042. * i40e_do_reset_safe - Protected reset path for userland calls.
  7043. * @pf: board private structure
  7044. * @reset_flags: which reset is requested
  7045. *
  7046. **/
  7047. void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
  7048. {
  7049. rtnl_lock();
  7050. i40e_do_reset(pf, reset_flags, true);
  7051. rtnl_unlock();
  7052. }
  7053. /**
  7054. * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
  7055. * @pf: board private structure
  7056. * @e: event info posted on ARQ
  7057. *
  7058. * Handler for LAN Queue Overflow Event generated by the firmware for PF
  7059. * and VF queues
  7060. **/
  7061. static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
  7062. struct i40e_arq_event_info *e)
  7063. {
  7064. struct i40e_aqc_lan_overflow *data =
  7065. (struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
  7066. u32 queue = le32_to_cpu(data->prtdcb_rupto);
  7067. u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
  7068. struct i40e_hw *hw = &pf->hw;
  7069. struct i40e_vf *vf;
  7070. u16 vf_id;
  7071. dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
  7072. queue, qtx_ctl);
  7073. /* Queue belongs to VF, find the VF and issue VF reset */
  7074. if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
  7075. >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
  7076. vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
  7077. >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
  7078. vf_id -= hw->func_caps.vf_base_id;
  7079. vf = &pf->vf[vf_id];
  7080. i40e_vc_notify_vf_reset(vf);
  7081. /* Allow VF to process pending reset notification */
  7082. msleep(20);
  7083. i40e_reset_vf(vf, false);
  7084. }
  7085. }
  7086. /**
  7087. * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
  7088. * @pf: board private structure
  7089. **/
  7090. u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
  7091. {
  7092. u32 val, fcnt_prog;
  7093. val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
  7094. fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
  7095. return fcnt_prog;
  7096. }
  7097. /**
  7098. * i40e_get_current_fd_count - Get total FD filters programmed for this PF
  7099. * @pf: board private structure
  7100. **/
  7101. u32 i40e_get_current_fd_count(struct i40e_pf *pf)
  7102. {
  7103. u32 val, fcnt_prog;
  7104. val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
  7105. fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
  7106. ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
  7107. I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
  7108. return fcnt_prog;
  7109. }
  7110. /**
  7111. * i40e_get_global_fd_count - Get total FD filters programmed on device
  7112. * @pf: board private structure
  7113. **/
  7114. u32 i40e_get_global_fd_count(struct i40e_pf *pf)
  7115. {
  7116. u32 val, fcnt_prog;
  7117. val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
  7118. fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
  7119. ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >>
  7120. I40E_GLQF_FDCNT_0_BESTCNT_SHIFT);
  7121. return fcnt_prog;
  7122. }
  7123. /**
  7124. * i40e_reenable_fdir_sb - Restore FDir SB capability
  7125. * @pf: board private structure
  7126. **/
  7127. static void i40e_reenable_fdir_sb(struct i40e_pf *pf)
  7128. {
  7129. if (test_and_clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
  7130. if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
  7131. (I40E_DEBUG_FD & pf->hw.debug_mask))
  7132. dev_info(&pf->pdev->dev, "FD Sideband/ntuple is being enabled since we have space in the table now\n");
  7133. }
  7134. /**
  7135. * i40e_reenable_fdir_atr - Restore FDir ATR capability
  7136. * @pf: board private structure
  7137. **/
  7138. static void i40e_reenable_fdir_atr(struct i40e_pf *pf)
  7139. {
  7140. if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state)) {
  7141. /* ATR uses the same filtering logic as SB rules. It only
  7142. * functions properly if the input set mask is at the default
  7143. * settings. It is safe to restore the default input set
  7144. * because there are no active TCPv4 filter rules.
  7145. */
  7146. i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
  7147. I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
  7148. I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
  7149. if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
  7150. (I40E_DEBUG_FD & pf->hw.debug_mask))
  7151. dev_info(&pf->pdev->dev, "ATR is being enabled since we have space in the table and there are no conflicting ntuple rules\n");
  7152. }
  7153. }
  7154. /**
  7155. * i40e_delete_invalid_filter - Delete an invalid FDIR filter
  7156. * @pf: board private structure
  7157. * @filter: FDir filter to remove
  7158. */
  7159. static void i40e_delete_invalid_filter(struct i40e_pf *pf,
  7160. struct i40e_fdir_filter *filter)
  7161. {
  7162. /* Update counters */
  7163. pf->fdir_pf_active_filters--;
  7164. pf->fd_inv = 0;
  7165. switch (filter->flow_type) {
  7166. case TCP_V4_FLOW:
  7167. pf->fd_tcp4_filter_cnt--;
  7168. break;
  7169. case UDP_V4_FLOW:
  7170. pf->fd_udp4_filter_cnt--;
  7171. break;
  7172. case SCTP_V4_FLOW:
  7173. pf->fd_sctp4_filter_cnt--;
  7174. break;
  7175. case IP_USER_FLOW:
  7176. switch (filter->ip4_proto) {
  7177. case IPPROTO_TCP:
  7178. pf->fd_tcp4_filter_cnt--;
  7179. break;
  7180. case IPPROTO_UDP:
  7181. pf->fd_udp4_filter_cnt--;
  7182. break;
  7183. case IPPROTO_SCTP:
  7184. pf->fd_sctp4_filter_cnt--;
  7185. break;
  7186. case IPPROTO_IP:
  7187. pf->fd_ip4_filter_cnt--;
  7188. break;
  7189. }
  7190. break;
  7191. }
  7192. /* Remove the filter from the list and free memory */
  7193. hlist_del(&filter->fdir_node);
  7194. kfree(filter);
  7195. }
  7196. /**
  7197. * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
  7198. * @pf: board private structure
  7199. **/
  7200. void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
  7201. {
  7202. struct i40e_fdir_filter *filter;
  7203. u32 fcnt_prog, fcnt_avail;
  7204. struct hlist_node *node;
  7205. if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
  7206. return;
  7207. /* Check if we have enough room to re-enable FDir SB capability. */
  7208. fcnt_prog = i40e_get_global_fd_count(pf);
  7209. fcnt_avail = pf->fdir_pf_filter_count;
  7210. if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
  7211. (pf->fd_add_err == 0) ||
  7212. (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt))
  7213. i40e_reenable_fdir_sb(pf);
  7214. /* We should wait for even more space before re-enabling ATR.
  7215. * Additionally, we cannot enable ATR as long as we still have TCP SB
  7216. * rules active.
  7217. */
  7218. if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) &&
  7219. (pf->fd_tcp4_filter_cnt == 0))
  7220. i40e_reenable_fdir_atr(pf);
  7221. /* if hw had a problem adding a filter, delete it */
  7222. if (pf->fd_inv > 0) {
  7223. hlist_for_each_entry_safe(filter, node,
  7224. &pf->fdir_filter_list, fdir_node)
  7225. if (filter->fd_id == pf->fd_inv)
  7226. i40e_delete_invalid_filter(pf, filter);
  7227. }
  7228. }
  7229. #define I40E_MIN_FD_FLUSH_INTERVAL 10
  7230. #define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
  7231. /**
  7232. * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
  7233. * @pf: board private structure
  7234. **/
  7235. static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
  7236. {
  7237. unsigned long min_flush_time;
  7238. int flush_wait_retry = 50;
  7239. bool disable_atr = false;
  7240. int fd_room;
  7241. int reg;
  7242. if (!time_after(jiffies, pf->fd_flush_timestamp +
  7243. (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
  7244. return;
  7245. /* If the flush is happening too quick and we have mostly SB rules we
  7246. * should not re-enable ATR for some time.
  7247. */
  7248. min_flush_time = pf->fd_flush_timestamp +
  7249. (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
  7250. fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
  7251. if (!(time_after(jiffies, min_flush_time)) &&
  7252. (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
  7253. if (I40E_DEBUG_FD & pf->hw.debug_mask)
  7254. dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
  7255. disable_atr = true;
  7256. }
  7257. pf->fd_flush_timestamp = jiffies;
  7258. set_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
  7259. /* flush all filters */
  7260. wr32(&pf->hw, I40E_PFQF_CTL_1,
  7261. I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
  7262. i40e_flush(&pf->hw);
  7263. pf->fd_flush_cnt++;
  7264. pf->fd_add_err = 0;
  7265. do {
  7266. /* Check FD flush status every 5-6msec */
  7267. usleep_range(5000, 6000);
  7268. reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
  7269. if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
  7270. break;
  7271. } while (flush_wait_retry--);
  7272. if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
  7273. dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
  7274. } else {
  7275. /* replay sideband filters */
  7276. i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
  7277. if (!disable_atr && !pf->fd_tcp4_filter_cnt)
  7278. clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
  7279. clear_bit(__I40E_FD_FLUSH_REQUESTED, pf->state);
  7280. if (I40E_DEBUG_FD & pf->hw.debug_mask)
  7281. dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
  7282. }
  7283. }
  7284. /**
  7285. * i40e_get_current_atr_count - Get the count of total FD ATR filters programmed
  7286. * @pf: board private structure
  7287. **/
  7288. u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
  7289. {
  7290. return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
  7291. }
  7292. /* We can see up to 256 filter programming desc in transit if the filters are
  7293. * being applied really fast; before we see the first
  7294. * filter miss error on Rx queue 0. Accumulating enough error messages before
  7295. * reacting will make sure we don't cause flush too often.
  7296. */
  7297. #define I40E_MAX_FD_PROGRAM_ERROR 256
  7298. /**
  7299. * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
  7300. * @pf: board private structure
  7301. **/
  7302. static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
  7303. {
  7304. /* if interface is down do nothing */
  7305. if (test_bit(__I40E_DOWN, pf->state))
  7306. return;
  7307. if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
  7308. i40e_fdir_flush_and_replay(pf);
  7309. i40e_fdir_check_and_reenable(pf);
  7310. }
  7311. /**
  7312. * i40e_vsi_link_event - notify VSI of a link event
  7313. * @vsi: vsi to be notified
  7314. * @link_up: link up or down
  7315. **/
  7316. static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
  7317. {
  7318. if (!vsi || test_bit(__I40E_VSI_DOWN, vsi->state))
  7319. return;
  7320. switch (vsi->type) {
  7321. case I40E_VSI_MAIN:
  7322. if (!vsi->netdev || !vsi->netdev_registered)
  7323. break;
  7324. if (link_up) {
  7325. netif_carrier_on(vsi->netdev);
  7326. netif_tx_wake_all_queues(vsi->netdev);
  7327. } else {
  7328. netif_carrier_off(vsi->netdev);
  7329. netif_tx_stop_all_queues(vsi->netdev);
  7330. }
  7331. break;
  7332. case I40E_VSI_SRIOV:
  7333. case I40E_VSI_VMDQ2:
  7334. case I40E_VSI_CTRL:
  7335. case I40E_VSI_IWARP:
  7336. case I40E_VSI_MIRROR:
  7337. default:
  7338. /* there is no notification for other VSIs */
  7339. break;
  7340. }
  7341. }
  7342. /**
  7343. * i40e_veb_link_event - notify elements on the veb of a link event
  7344. * @veb: veb to be notified
  7345. * @link_up: link up or down
  7346. **/
  7347. static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
  7348. {
  7349. struct i40e_pf *pf;
  7350. int i;
  7351. if (!veb || !veb->pf)
  7352. return;
  7353. pf = veb->pf;
  7354. /* depth first... */
  7355. for (i = 0; i < I40E_MAX_VEB; i++)
  7356. if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
  7357. i40e_veb_link_event(pf->veb[i], link_up);
  7358. /* ... now the local VSIs */
  7359. for (i = 0; i < pf->num_alloc_vsi; i++)
  7360. if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
  7361. i40e_vsi_link_event(pf->vsi[i], link_up);
  7362. }
  7363. /**
  7364. * i40e_link_event - Update netif_carrier status
  7365. * @pf: board private structure
  7366. **/
  7367. static void i40e_link_event(struct i40e_pf *pf)
  7368. {
  7369. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  7370. u8 new_link_speed, old_link_speed;
  7371. i40e_status status;
  7372. bool new_link, old_link;
  7373. /* save off old link status information */
  7374. pf->hw.phy.link_info_old = pf->hw.phy.link_info;
  7375. /* set this to force the get_link_status call to refresh state */
  7376. pf->hw.phy.get_link_info = true;
  7377. old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
  7378. status = i40e_get_link_status(&pf->hw, &new_link);
  7379. /* On success, disable temp link polling */
  7380. if (status == I40E_SUCCESS) {
  7381. clear_bit(__I40E_TEMP_LINK_POLLING, pf->state);
  7382. } else {
  7383. /* Enable link polling temporarily until i40e_get_link_status
  7384. * returns I40E_SUCCESS
  7385. */
  7386. set_bit(__I40E_TEMP_LINK_POLLING, pf->state);
  7387. dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
  7388. status);
  7389. return;
  7390. }
  7391. old_link_speed = pf->hw.phy.link_info_old.link_speed;
  7392. new_link_speed = pf->hw.phy.link_info.link_speed;
  7393. if (new_link == old_link &&
  7394. new_link_speed == old_link_speed &&
  7395. (test_bit(__I40E_VSI_DOWN, vsi->state) ||
  7396. new_link == netif_carrier_ok(vsi->netdev)))
  7397. return;
  7398. i40e_print_link_message(vsi, new_link);
  7399. /* Notify the base of the switch tree connected to
  7400. * the link. Floating VEBs are not notified.
  7401. */
  7402. if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
  7403. i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
  7404. else
  7405. i40e_vsi_link_event(vsi, new_link);
  7406. if (pf->vf)
  7407. i40e_vc_notify_link_state(pf);
  7408. if (pf->flags & I40E_FLAG_PTP)
  7409. i40e_ptp_set_increment(pf);
  7410. }
  7411. /**
  7412. * i40e_watchdog_subtask - periodic checks not using event driven response
  7413. * @pf: board private structure
  7414. **/
  7415. static void i40e_watchdog_subtask(struct i40e_pf *pf)
  7416. {
  7417. int i;
  7418. /* if interface is down do nothing */
  7419. if (test_bit(__I40E_DOWN, pf->state) ||
  7420. test_bit(__I40E_CONFIG_BUSY, pf->state))
  7421. return;
  7422. /* make sure we don't do these things too often */
  7423. if (time_before(jiffies, (pf->service_timer_previous +
  7424. pf->service_timer_period)))
  7425. return;
  7426. pf->service_timer_previous = jiffies;
  7427. if ((pf->flags & I40E_FLAG_LINK_POLLING_ENABLED) ||
  7428. test_bit(__I40E_TEMP_LINK_POLLING, pf->state))
  7429. i40e_link_event(pf);
  7430. /* Update the stats for active netdevs so the network stack
  7431. * can look at updated numbers whenever it cares to
  7432. */
  7433. for (i = 0; i < pf->num_alloc_vsi; i++)
  7434. if (pf->vsi[i] && pf->vsi[i]->netdev)
  7435. i40e_update_stats(pf->vsi[i]);
  7436. if (pf->flags & I40E_FLAG_VEB_STATS_ENABLED) {
  7437. /* Update the stats for the active switching components */
  7438. for (i = 0; i < I40E_MAX_VEB; i++)
  7439. if (pf->veb[i])
  7440. i40e_update_veb_stats(pf->veb[i]);
  7441. }
  7442. i40e_ptp_rx_hang(pf);
  7443. i40e_ptp_tx_hang(pf);
  7444. }
  7445. /**
  7446. * i40e_reset_subtask - Set up for resetting the device and driver
  7447. * @pf: board private structure
  7448. **/
  7449. static void i40e_reset_subtask(struct i40e_pf *pf)
  7450. {
  7451. u32 reset_flags = 0;
  7452. if (test_bit(__I40E_REINIT_REQUESTED, pf->state)) {
  7453. reset_flags |= BIT(__I40E_REINIT_REQUESTED);
  7454. clear_bit(__I40E_REINIT_REQUESTED, pf->state);
  7455. }
  7456. if (test_bit(__I40E_PF_RESET_REQUESTED, pf->state)) {
  7457. reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
  7458. clear_bit(__I40E_PF_RESET_REQUESTED, pf->state);
  7459. }
  7460. if (test_bit(__I40E_CORE_RESET_REQUESTED, pf->state)) {
  7461. reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
  7462. clear_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
  7463. }
  7464. if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state)) {
  7465. reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
  7466. clear_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
  7467. }
  7468. if (test_bit(__I40E_DOWN_REQUESTED, pf->state)) {
  7469. reset_flags |= BIT(__I40E_DOWN_REQUESTED);
  7470. clear_bit(__I40E_DOWN_REQUESTED, pf->state);
  7471. }
  7472. /* If there's a recovery already waiting, it takes
  7473. * precedence before starting a new reset sequence.
  7474. */
  7475. if (test_bit(__I40E_RESET_INTR_RECEIVED, pf->state)) {
  7476. i40e_prep_for_reset(pf, false);
  7477. i40e_reset(pf);
  7478. i40e_rebuild(pf, false, false);
  7479. }
  7480. /* If we're already down or resetting, just bail */
  7481. if (reset_flags &&
  7482. !test_bit(__I40E_DOWN, pf->state) &&
  7483. !test_bit(__I40E_CONFIG_BUSY, pf->state)) {
  7484. i40e_do_reset(pf, reset_flags, false);
  7485. }
  7486. }
  7487. /**
  7488. * i40e_handle_link_event - Handle link event
  7489. * @pf: board private structure
  7490. * @e: event info posted on ARQ
  7491. **/
  7492. static void i40e_handle_link_event(struct i40e_pf *pf,
  7493. struct i40e_arq_event_info *e)
  7494. {
  7495. struct i40e_aqc_get_link_status *status =
  7496. (struct i40e_aqc_get_link_status *)&e->desc.params.raw;
  7497. /* Do a new status request to re-enable LSE reporting
  7498. * and load new status information into the hw struct
  7499. * This completely ignores any state information
  7500. * in the ARQ event info, instead choosing to always
  7501. * issue the AQ update link status command.
  7502. */
  7503. i40e_link_event(pf);
  7504. /* Check if module meets thermal requirements */
  7505. if (status->phy_type == I40E_PHY_TYPE_NOT_SUPPORTED_HIGH_TEMP) {
  7506. dev_err(&pf->pdev->dev,
  7507. "Rx/Tx is disabled on this device because the module does not meet thermal requirements.\n");
  7508. dev_err(&pf->pdev->dev,
  7509. "Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
  7510. } else {
  7511. /* check for unqualified module, if link is down, suppress
  7512. * the message if link was forced to be down.
  7513. */
  7514. if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
  7515. (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
  7516. (!(status->link_info & I40E_AQ_LINK_UP)) &&
  7517. (!(pf->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED))) {
  7518. dev_err(&pf->pdev->dev,
  7519. "Rx/Tx is disabled on this device because an unsupported SFP module type was detected.\n");
  7520. dev_err(&pf->pdev->dev,
  7521. "Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
  7522. }
  7523. }
  7524. }
  7525. /**
  7526. * i40e_clean_adminq_subtask - Clean the AdminQ rings
  7527. * @pf: board private structure
  7528. **/
  7529. static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
  7530. {
  7531. struct i40e_arq_event_info event;
  7532. struct i40e_hw *hw = &pf->hw;
  7533. u16 pending, i = 0;
  7534. i40e_status ret;
  7535. u16 opcode;
  7536. u32 oldval;
  7537. u32 val;
  7538. /* Do not run clean AQ when PF reset fails */
  7539. if (test_bit(__I40E_RESET_FAILED, pf->state))
  7540. return;
  7541. /* check for error indications */
  7542. val = rd32(&pf->hw, pf->hw.aq.arq.len);
  7543. oldval = val;
  7544. if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
  7545. if (hw->debug_mask & I40E_DEBUG_AQ)
  7546. dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
  7547. val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
  7548. }
  7549. if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
  7550. if (hw->debug_mask & I40E_DEBUG_AQ)
  7551. dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
  7552. val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
  7553. pf->arq_overflows++;
  7554. }
  7555. if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
  7556. if (hw->debug_mask & I40E_DEBUG_AQ)
  7557. dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
  7558. val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
  7559. }
  7560. if (oldval != val)
  7561. wr32(&pf->hw, pf->hw.aq.arq.len, val);
  7562. val = rd32(&pf->hw, pf->hw.aq.asq.len);
  7563. oldval = val;
  7564. if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
  7565. if (pf->hw.debug_mask & I40E_DEBUG_AQ)
  7566. dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
  7567. val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
  7568. }
  7569. if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
  7570. if (pf->hw.debug_mask & I40E_DEBUG_AQ)
  7571. dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
  7572. val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
  7573. }
  7574. if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
  7575. if (pf->hw.debug_mask & I40E_DEBUG_AQ)
  7576. dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
  7577. val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
  7578. }
  7579. if (oldval != val)
  7580. wr32(&pf->hw, pf->hw.aq.asq.len, val);
  7581. event.buf_len = I40E_MAX_AQ_BUF_SIZE;
  7582. event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
  7583. if (!event.msg_buf)
  7584. return;
  7585. do {
  7586. ret = i40e_clean_arq_element(hw, &event, &pending);
  7587. if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK)
  7588. break;
  7589. else if (ret) {
  7590. dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
  7591. break;
  7592. }
  7593. opcode = le16_to_cpu(event.desc.opcode);
  7594. switch (opcode) {
  7595. case i40e_aqc_opc_get_link_status:
  7596. i40e_handle_link_event(pf, &event);
  7597. break;
  7598. case i40e_aqc_opc_send_msg_to_pf:
  7599. ret = i40e_vc_process_vf_msg(pf,
  7600. le16_to_cpu(event.desc.retval),
  7601. le32_to_cpu(event.desc.cookie_high),
  7602. le32_to_cpu(event.desc.cookie_low),
  7603. event.msg_buf,
  7604. event.msg_len);
  7605. break;
  7606. case i40e_aqc_opc_lldp_update_mib:
  7607. dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
  7608. #ifdef CONFIG_I40E_DCB
  7609. rtnl_lock();
  7610. ret = i40e_handle_lldp_event(pf, &event);
  7611. rtnl_unlock();
  7612. #endif /* CONFIG_I40E_DCB */
  7613. break;
  7614. case i40e_aqc_opc_event_lan_overflow:
  7615. dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
  7616. i40e_handle_lan_overflow_event(pf, &event);
  7617. break;
  7618. case i40e_aqc_opc_send_msg_to_peer:
  7619. dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
  7620. break;
  7621. case i40e_aqc_opc_nvm_erase:
  7622. case i40e_aqc_opc_nvm_update:
  7623. case i40e_aqc_opc_oem_post_update:
  7624. i40e_debug(&pf->hw, I40E_DEBUG_NVM,
  7625. "ARQ NVM operation 0x%04x completed\n",
  7626. opcode);
  7627. break;
  7628. default:
  7629. dev_info(&pf->pdev->dev,
  7630. "ARQ: Unknown event 0x%04x ignored\n",
  7631. opcode);
  7632. break;
  7633. }
  7634. } while (i++ < pf->adminq_work_limit);
  7635. if (i < pf->adminq_work_limit)
  7636. clear_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
  7637. /* re-enable Admin queue interrupt cause */
  7638. val = rd32(hw, I40E_PFINT_ICR0_ENA);
  7639. val |= I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
  7640. wr32(hw, I40E_PFINT_ICR0_ENA, val);
  7641. i40e_flush(hw);
  7642. kfree(event.msg_buf);
  7643. }
  7644. /**
  7645. * i40e_verify_eeprom - make sure eeprom is good to use
  7646. * @pf: board private structure
  7647. **/
  7648. static void i40e_verify_eeprom(struct i40e_pf *pf)
  7649. {
  7650. int err;
  7651. err = i40e_diag_eeprom_test(&pf->hw);
  7652. if (err) {
  7653. /* retry in case of garbage read */
  7654. err = i40e_diag_eeprom_test(&pf->hw);
  7655. if (err) {
  7656. dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
  7657. err);
  7658. set_bit(__I40E_BAD_EEPROM, pf->state);
  7659. }
  7660. }
  7661. if (!err && test_bit(__I40E_BAD_EEPROM, pf->state)) {
  7662. dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
  7663. clear_bit(__I40E_BAD_EEPROM, pf->state);
  7664. }
  7665. }
  7666. /**
  7667. * i40e_enable_pf_switch_lb
  7668. * @pf: pointer to the PF structure
  7669. *
  7670. * enable switch loop back or die - no point in a return value
  7671. **/
  7672. static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
  7673. {
  7674. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  7675. struct i40e_vsi_context ctxt;
  7676. int ret;
  7677. ctxt.seid = pf->main_vsi_seid;
  7678. ctxt.pf_num = pf->hw.pf_id;
  7679. ctxt.vf_num = 0;
  7680. ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
  7681. if (ret) {
  7682. dev_info(&pf->pdev->dev,
  7683. "couldn't get PF vsi config, err %s aq_err %s\n",
  7684. i40e_stat_str(&pf->hw, ret),
  7685. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  7686. return;
  7687. }
  7688. ctxt.flags = I40E_AQ_VSI_TYPE_PF;
  7689. ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  7690. ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  7691. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  7692. if (ret) {
  7693. dev_info(&pf->pdev->dev,
  7694. "update vsi switch failed, err %s aq_err %s\n",
  7695. i40e_stat_str(&pf->hw, ret),
  7696. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  7697. }
  7698. }
  7699. /**
  7700. * i40e_disable_pf_switch_lb
  7701. * @pf: pointer to the PF structure
  7702. *
  7703. * disable switch loop back or die - no point in a return value
  7704. **/
  7705. static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
  7706. {
  7707. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  7708. struct i40e_vsi_context ctxt;
  7709. int ret;
  7710. ctxt.seid = pf->main_vsi_seid;
  7711. ctxt.pf_num = pf->hw.pf_id;
  7712. ctxt.vf_num = 0;
  7713. ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
  7714. if (ret) {
  7715. dev_info(&pf->pdev->dev,
  7716. "couldn't get PF vsi config, err %s aq_err %s\n",
  7717. i40e_stat_str(&pf->hw, ret),
  7718. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  7719. return;
  7720. }
  7721. ctxt.flags = I40E_AQ_VSI_TYPE_PF;
  7722. ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  7723. ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  7724. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  7725. if (ret) {
  7726. dev_info(&pf->pdev->dev,
  7727. "update vsi switch failed, err %s aq_err %s\n",
  7728. i40e_stat_str(&pf->hw, ret),
  7729. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  7730. }
  7731. }
  7732. /**
  7733. * i40e_config_bridge_mode - Configure the HW bridge mode
  7734. * @veb: pointer to the bridge instance
  7735. *
  7736. * Configure the loop back mode for the LAN VSI that is downlink to the
  7737. * specified HW bridge instance. It is expected this function is called
  7738. * when a new HW bridge is instantiated.
  7739. **/
  7740. static void i40e_config_bridge_mode(struct i40e_veb *veb)
  7741. {
  7742. struct i40e_pf *pf = veb->pf;
  7743. if (pf->hw.debug_mask & I40E_DEBUG_LAN)
  7744. dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
  7745. veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
  7746. if (veb->bridge_mode & BRIDGE_MODE_VEPA)
  7747. i40e_disable_pf_switch_lb(pf);
  7748. else
  7749. i40e_enable_pf_switch_lb(pf);
  7750. }
  7751. /**
  7752. * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
  7753. * @veb: pointer to the VEB instance
  7754. *
  7755. * This is a recursive function that first builds the attached VSIs then
  7756. * recurses in to build the next layer of VEB. We track the connections
  7757. * through our own index numbers because the seid's from the HW could
  7758. * change across the reset.
  7759. **/
  7760. static int i40e_reconstitute_veb(struct i40e_veb *veb)
  7761. {
  7762. struct i40e_vsi *ctl_vsi = NULL;
  7763. struct i40e_pf *pf = veb->pf;
  7764. int v, veb_idx;
  7765. int ret;
  7766. /* build VSI that owns this VEB, temporarily attached to base VEB */
  7767. for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
  7768. if (pf->vsi[v] &&
  7769. pf->vsi[v]->veb_idx == veb->idx &&
  7770. pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
  7771. ctl_vsi = pf->vsi[v];
  7772. break;
  7773. }
  7774. }
  7775. if (!ctl_vsi) {
  7776. dev_info(&pf->pdev->dev,
  7777. "missing owner VSI for veb_idx %d\n", veb->idx);
  7778. ret = -ENOENT;
  7779. goto end_reconstitute;
  7780. }
  7781. if (ctl_vsi != pf->vsi[pf->lan_vsi])
  7782. ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
  7783. ret = i40e_add_vsi(ctl_vsi);
  7784. if (ret) {
  7785. dev_info(&pf->pdev->dev,
  7786. "rebuild of veb_idx %d owner VSI failed: %d\n",
  7787. veb->idx, ret);
  7788. goto end_reconstitute;
  7789. }
  7790. i40e_vsi_reset_stats(ctl_vsi);
  7791. /* create the VEB in the switch and move the VSI onto the VEB */
  7792. ret = i40e_add_veb(veb, ctl_vsi);
  7793. if (ret)
  7794. goto end_reconstitute;
  7795. if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
  7796. veb->bridge_mode = BRIDGE_MODE_VEB;
  7797. else
  7798. veb->bridge_mode = BRIDGE_MODE_VEPA;
  7799. i40e_config_bridge_mode(veb);
  7800. /* create the remaining VSIs attached to this VEB */
  7801. for (v = 0; v < pf->num_alloc_vsi; v++) {
  7802. if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
  7803. continue;
  7804. if (pf->vsi[v]->veb_idx == veb->idx) {
  7805. struct i40e_vsi *vsi = pf->vsi[v];
  7806. vsi->uplink_seid = veb->seid;
  7807. ret = i40e_add_vsi(vsi);
  7808. if (ret) {
  7809. dev_info(&pf->pdev->dev,
  7810. "rebuild of vsi_idx %d failed: %d\n",
  7811. v, ret);
  7812. goto end_reconstitute;
  7813. }
  7814. i40e_vsi_reset_stats(vsi);
  7815. }
  7816. }
  7817. /* create any VEBs attached to this VEB - RECURSION */
  7818. for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
  7819. if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
  7820. pf->veb[veb_idx]->uplink_seid = veb->seid;
  7821. ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
  7822. if (ret)
  7823. break;
  7824. }
  7825. }
  7826. end_reconstitute:
  7827. return ret;
  7828. }
  7829. /**
  7830. * i40e_get_capabilities - get info about the HW
  7831. * @pf: the PF struct
  7832. **/
  7833. static int i40e_get_capabilities(struct i40e_pf *pf,
  7834. enum i40e_admin_queue_opc list_type)
  7835. {
  7836. struct i40e_aqc_list_capabilities_element_resp *cap_buf;
  7837. u16 data_size;
  7838. int buf_len;
  7839. int err;
  7840. buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
  7841. do {
  7842. cap_buf = kzalloc(buf_len, GFP_KERNEL);
  7843. if (!cap_buf)
  7844. return -ENOMEM;
  7845. /* this loads the data into the hw struct for us */
  7846. err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
  7847. &data_size, list_type,
  7848. NULL);
  7849. /* data loaded, buffer no longer needed */
  7850. kfree(cap_buf);
  7851. if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
  7852. /* retry with a larger buffer */
  7853. buf_len = data_size;
  7854. } else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK) {
  7855. dev_info(&pf->pdev->dev,
  7856. "capability discovery failed, err %s aq_err %s\n",
  7857. i40e_stat_str(&pf->hw, err),
  7858. i40e_aq_str(&pf->hw,
  7859. pf->hw.aq.asq_last_status));
  7860. return -ENODEV;
  7861. }
  7862. } while (err);
  7863. if (pf->hw.debug_mask & I40E_DEBUG_USER) {
  7864. if (list_type == i40e_aqc_opc_list_func_capabilities) {
  7865. dev_info(&pf->pdev->dev,
  7866. "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",
  7867. pf->hw.pf_id, pf->hw.func_caps.num_vfs,
  7868. pf->hw.func_caps.num_msix_vectors,
  7869. pf->hw.func_caps.num_msix_vectors_vf,
  7870. pf->hw.func_caps.fd_filters_guaranteed,
  7871. pf->hw.func_caps.fd_filters_best_effort,
  7872. pf->hw.func_caps.num_tx_qp,
  7873. pf->hw.func_caps.num_vsis);
  7874. } else if (list_type == i40e_aqc_opc_list_dev_capabilities) {
  7875. dev_info(&pf->pdev->dev,
  7876. "switch_mode=0x%04x, function_valid=0x%08x\n",
  7877. pf->hw.dev_caps.switch_mode,
  7878. pf->hw.dev_caps.valid_functions);
  7879. dev_info(&pf->pdev->dev,
  7880. "SR-IOV=%d, num_vfs for all function=%u\n",
  7881. pf->hw.dev_caps.sr_iov_1_1,
  7882. pf->hw.dev_caps.num_vfs);
  7883. dev_info(&pf->pdev->dev,
  7884. "num_vsis=%u, num_rx:%u, num_tx=%u\n",
  7885. pf->hw.dev_caps.num_vsis,
  7886. pf->hw.dev_caps.num_rx_qp,
  7887. pf->hw.dev_caps.num_tx_qp);
  7888. }
  7889. }
  7890. if (list_type == i40e_aqc_opc_list_func_capabilities) {
  7891. #define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
  7892. + pf->hw.func_caps.num_vfs)
  7893. if (pf->hw.revision_id == 0 &&
  7894. pf->hw.func_caps.num_vsis < DEF_NUM_VSI) {
  7895. dev_info(&pf->pdev->dev,
  7896. "got num_vsis %d, setting num_vsis to %d\n",
  7897. pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
  7898. pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
  7899. }
  7900. }
  7901. return 0;
  7902. }
  7903. static int i40e_vsi_clear(struct i40e_vsi *vsi);
  7904. /**
  7905. * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
  7906. * @pf: board private structure
  7907. **/
  7908. static void i40e_fdir_sb_setup(struct i40e_pf *pf)
  7909. {
  7910. struct i40e_vsi *vsi;
  7911. /* quick workaround for an NVM issue that leaves a critical register
  7912. * uninitialized
  7913. */
  7914. if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
  7915. static const u32 hkey[] = {
  7916. 0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
  7917. 0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
  7918. 0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
  7919. 0x95b3a76d};
  7920. int i;
  7921. for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
  7922. wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
  7923. }
  7924. if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
  7925. return;
  7926. /* find existing VSI and see if it needs configuring */
  7927. vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
  7928. /* create a new VSI if none exists */
  7929. if (!vsi) {
  7930. vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
  7931. pf->vsi[pf->lan_vsi]->seid, 0);
  7932. if (!vsi) {
  7933. dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
  7934. pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
  7935. pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
  7936. return;
  7937. }
  7938. }
  7939. i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
  7940. }
  7941. /**
  7942. * i40e_fdir_teardown - release the Flow Director resources
  7943. * @pf: board private structure
  7944. **/
  7945. static void i40e_fdir_teardown(struct i40e_pf *pf)
  7946. {
  7947. struct i40e_vsi *vsi;
  7948. i40e_fdir_filter_exit(pf);
  7949. vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
  7950. if (vsi)
  7951. i40e_vsi_release(vsi);
  7952. }
  7953. /**
  7954. * i40e_rebuild_cloud_filters - Rebuilds cloud filters for VSIs
  7955. * @vsi: PF main vsi
  7956. * @seid: seid of main or channel VSIs
  7957. *
  7958. * Rebuilds cloud filters associated with main VSI and channel VSIs if they
  7959. * existed before reset
  7960. **/
  7961. static int i40e_rebuild_cloud_filters(struct i40e_vsi *vsi, u16 seid)
  7962. {
  7963. struct i40e_cloud_filter *cfilter;
  7964. struct i40e_pf *pf = vsi->back;
  7965. struct hlist_node *node;
  7966. i40e_status ret;
  7967. /* Add cloud filters back if they exist */
  7968. hlist_for_each_entry_safe(cfilter, node, &pf->cloud_filter_list,
  7969. cloud_node) {
  7970. if (cfilter->seid != seid)
  7971. continue;
  7972. if (cfilter->dst_port)
  7973. ret = i40e_add_del_cloud_filter_big_buf(vsi, cfilter,
  7974. true);
  7975. else
  7976. ret = i40e_add_del_cloud_filter(vsi, cfilter, true);
  7977. if (ret) {
  7978. dev_dbg(&pf->pdev->dev,
  7979. "Failed to rebuild cloud filter, err %s aq_err %s\n",
  7980. i40e_stat_str(&pf->hw, ret),
  7981. i40e_aq_str(&pf->hw,
  7982. pf->hw.aq.asq_last_status));
  7983. return ret;
  7984. }
  7985. }
  7986. return 0;
  7987. }
  7988. /**
  7989. * i40e_rebuild_channels - Rebuilds channel VSIs if they existed before reset
  7990. * @vsi: PF main vsi
  7991. *
  7992. * Rebuilds channel VSIs if they existed before reset
  7993. **/
  7994. static int i40e_rebuild_channels(struct i40e_vsi *vsi)
  7995. {
  7996. struct i40e_channel *ch, *ch_tmp;
  7997. i40e_status ret;
  7998. if (list_empty(&vsi->ch_list))
  7999. return 0;
  8000. list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
  8001. if (!ch->initialized)
  8002. break;
  8003. /* Proceed with creation of channel (VMDq2) VSI */
  8004. ret = i40e_add_channel(vsi->back, vsi->uplink_seid, ch);
  8005. if (ret) {
  8006. dev_info(&vsi->back->pdev->dev,
  8007. "failed to rebuild channels using uplink_seid %u\n",
  8008. vsi->uplink_seid);
  8009. return ret;
  8010. }
  8011. /* Reconfigure TX queues using QTX_CTL register */
  8012. ret = i40e_channel_config_tx_ring(vsi->back, vsi, ch);
  8013. if (ret) {
  8014. dev_info(&vsi->back->pdev->dev,
  8015. "failed to configure TX rings for channel %u\n",
  8016. ch->seid);
  8017. return ret;
  8018. }
  8019. /* update 'next_base_queue' */
  8020. vsi->next_base_queue = vsi->next_base_queue +
  8021. ch->num_queue_pairs;
  8022. if (ch->max_tx_rate) {
  8023. u64 credits = ch->max_tx_rate;
  8024. if (i40e_set_bw_limit(vsi, ch->seid,
  8025. ch->max_tx_rate))
  8026. return -EINVAL;
  8027. do_div(credits, I40E_BW_CREDIT_DIVISOR);
  8028. dev_dbg(&vsi->back->pdev->dev,
  8029. "Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
  8030. ch->max_tx_rate,
  8031. credits,
  8032. ch->seid);
  8033. }
  8034. ret = i40e_rebuild_cloud_filters(vsi, ch->seid);
  8035. if (ret) {
  8036. dev_dbg(&vsi->back->pdev->dev,
  8037. "Failed to rebuild cloud filters for channel VSI %u\n",
  8038. ch->seid);
  8039. return ret;
  8040. }
  8041. }
  8042. return 0;
  8043. }
  8044. /**
  8045. * i40e_prep_for_reset - prep for the core to reset
  8046. * @pf: board private structure
  8047. * @lock_acquired: indicates whether or not the lock has been acquired
  8048. * before this function was called.
  8049. *
  8050. * Close up the VFs and other things in prep for PF Reset.
  8051. **/
  8052. static void i40e_prep_for_reset(struct i40e_pf *pf, bool lock_acquired)
  8053. {
  8054. struct i40e_hw *hw = &pf->hw;
  8055. i40e_status ret = 0;
  8056. u32 v;
  8057. clear_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
  8058. if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
  8059. return;
  8060. if (i40e_check_asq_alive(&pf->hw))
  8061. i40e_vc_notify_reset(pf);
  8062. dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
  8063. /* quiesce the VSIs and their queues that are not already DOWN */
  8064. /* pf_quiesce_all_vsi modifies netdev structures -rtnl_lock needed */
  8065. if (!lock_acquired)
  8066. rtnl_lock();
  8067. i40e_pf_quiesce_all_vsi(pf);
  8068. if (!lock_acquired)
  8069. rtnl_unlock();
  8070. for (v = 0; v < pf->num_alloc_vsi; v++) {
  8071. if (pf->vsi[v])
  8072. pf->vsi[v]->seid = 0;
  8073. }
  8074. i40e_shutdown_adminq(&pf->hw);
  8075. /* call shutdown HMC */
  8076. if (hw->hmc.hmc_obj) {
  8077. ret = i40e_shutdown_lan_hmc(hw);
  8078. if (ret)
  8079. dev_warn(&pf->pdev->dev,
  8080. "shutdown_lan_hmc failed: %d\n", ret);
  8081. }
  8082. }
  8083. /**
  8084. * i40e_send_version - update firmware with driver version
  8085. * @pf: PF struct
  8086. */
  8087. static void i40e_send_version(struct i40e_pf *pf)
  8088. {
  8089. struct i40e_driver_version dv;
  8090. dv.major_version = DRV_VERSION_MAJOR;
  8091. dv.minor_version = DRV_VERSION_MINOR;
  8092. dv.build_version = DRV_VERSION_BUILD;
  8093. dv.subbuild_version = 0;
  8094. strlcpy(dv.driver_string, DRV_VERSION, sizeof(dv.driver_string));
  8095. i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
  8096. }
  8097. /**
  8098. * i40e_get_oem_version - get OEM specific version information
  8099. * @hw: pointer to the hardware structure
  8100. **/
  8101. static void i40e_get_oem_version(struct i40e_hw *hw)
  8102. {
  8103. u16 block_offset = 0xffff;
  8104. u16 block_length = 0;
  8105. u16 capabilities = 0;
  8106. u16 gen_snap = 0;
  8107. u16 release = 0;
  8108. #define I40E_SR_NVM_OEM_VERSION_PTR 0x1B
  8109. #define I40E_NVM_OEM_LENGTH_OFFSET 0x00
  8110. #define I40E_NVM_OEM_CAPABILITIES_OFFSET 0x01
  8111. #define I40E_NVM_OEM_GEN_OFFSET 0x02
  8112. #define I40E_NVM_OEM_RELEASE_OFFSET 0x03
  8113. #define I40E_NVM_OEM_CAPABILITIES_MASK 0x000F
  8114. #define I40E_NVM_OEM_LENGTH 3
  8115. /* Check if pointer to OEM version block is valid. */
  8116. i40e_read_nvm_word(hw, I40E_SR_NVM_OEM_VERSION_PTR, &block_offset);
  8117. if (block_offset == 0xffff)
  8118. return;
  8119. /* Check if OEM version block has correct length. */
  8120. i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_LENGTH_OFFSET,
  8121. &block_length);
  8122. if (block_length < I40E_NVM_OEM_LENGTH)
  8123. return;
  8124. /* Check if OEM version format is as expected. */
  8125. i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_CAPABILITIES_OFFSET,
  8126. &capabilities);
  8127. if ((capabilities & I40E_NVM_OEM_CAPABILITIES_MASK) != 0)
  8128. return;
  8129. i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_GEN_OFFSET,
  8130. &gen_snap);
  8131. i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_RELEASE_OFFSET,
  8132. &release);
  8133. hw->nvm.oem_ver = (gen_snap << I40E_OEM_SNAP_SHIFT) | release;
  8134. hw->nvm.eetrack = I40E_OEM_EETRACK_ID;
  8135. }
  8136. /**
  8137. * i40e_reset - wait for core reset to finish reset, reset pf if corer not seen
  8138. * @pf: board private structure
  8139. **/
  8140. static int i40e_reset(struct i40e_pf *pf)
  8141. {
  8142. struct i40e_hw *hw = &pf->hw;
  8143. i40e_status ret;
  8144. ret = i40e_pf_reset(hw);
  8145. if (ret) {
  8146. dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
  8147. set_bit(__I40E_RESET_FAILED, pf->state);
  8148. clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
  8149. } else {
  8150. pf->pfr_count++;
  8151. }
  8152. return ret;
  8153. }
  8154. /**
  8155. * i40e_rebuild - rebuild using a saved config
  8156. * @pf: board private structure
  8157. * @reinit: if the Main VSI needs to re-initialized.
  8158. * @lock_acquired: indicates whether or not the lock has been acquired
  8159. * before this function was called.
  8160. **/
  8161. static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired)
  8162. {
  8163. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  8164. struct i40e_hw *hw = &pf->hw;
  8165. u8 set_fc_aq_fail = 0;
  8166. i40e_status ret;
  8167. u32 val;
  8168. int v;
  8169. if (test_bit(__I40E_DOWN, pf->state))
  8170. goto clear_recovery;
  8171. dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
  8172. /* rebuild the basics for the AdminQ, HMC, and initial HW switch */
  8173. ret = i40e_init_adminq(&pf->hw);
  8174. if (ret) {
  8175. dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %s aq_err %s\n",
  8176. i40e_stat_str(&pf->hw, ret),
  8177. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  8178. goto clear_recovery;
  8179. }
  8180. i40e_get_oem_version(&pf->hw);
  8181. if (test_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state) &&
  8182. ((hw->aq.fw_maj_ver == 4 && hw->aq.fw_min_ver <= 33) ||
  8183. hw->aq.fw_maj_ver < 4) && hw->mac.type == I40E_MAC_XL710) {
  8184. /* The following delay is necessary for 4.33 firmware and older
  8185. * to recover after EMP reset. 200 ms should suffice but we
  8186. * put here 300 ms to be sure that FW is ready to operate
  8187. * after reset.
  8188. */
  8189. mdelay(300);
  8190. }
  8191. /* re-verify the eeprom if we just had an EMP reset */
  8192. if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state))
  8193. i40e_verify_eeprom(pf);
  8194. i40e_clear_pxe_mode(hw);
  8195. ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
  8196. if (ret)
  8197. goto end_core_reset;
  8198. ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
  8199. hw->func_caps.num_rx_qp, 0, 0);
  8200. if (ret) {
  8201. dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
  8202. goto end_core_reset;
  8203. }
  8204. ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
  8205. if (ret) {
  8206. dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
  8207. goto end_core_reset;
  8208. }
  8209. /* Enable FW to write a default DCB config on link-up */
  8210. i40e_aq_set_dcb_parameters(hw, true, NULL);
  8211. #ifdef CONFIG_I40E_DCB
  8212. ret = i40e_init_pf_dcb(pf);
  8213. if (ret) {
  8214. dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n", ret);
  8215. pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
  8216. /* Continue without DCB enabled */
  8217. }
  8218. #endif /* CONFIG_I40E_DCB */
  8219. /* do basic switch setup */
  8220. if (!lock_acquired)
  8221. rtnl_lock();
  8222. ret = i40e_setup_pf_switch(pf, reinit);
  8223. if (ret)
  8224. goto end_unlock;
  8225. /* The driver only wants link up/down and module qualification
  8226. * reports from firmware. Note the negative logic.
  8227. */
  8228. ret = i40e_aq_set_phy_int_mask(&pf->hw,
  8229. ~(I40E_AQ_EVENT_LINK_UPDOWN |
  8230. I40E_AQ_EVENT_MEDIA_NA |
  8231. I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
  8232. if (ret)
  8233. dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
  8234. i40e_stat_str(&pf->hw, ret),
  8235. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  8236. /* make sure our flow control settings are restored */
  8237. ret = i40e_set_fc(&pf->hw, &set_fc_aq_fail, true);
  8238. if (ret)
  8239. dev_dbg(&pf->pdev->dev, "setting flow control: ret = %s last_status = %s\n",
  8240. i40e_stat_str(&pf->hw, ret),
  8241. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  8242. /* Rebuild the VSIs and VEBs that existed before reset.
  8243. * They are still in our local switch element arrays, so only
  8244. * need to rebuild the switch model in the HW.
  8245. *
  8246. * If there were VEBs but the reconstitution failed, we'll try
  8247. * try to recover minimal use by getting the basic PF VSI working.
  8248. */
  8249. if (vsi->uplink_seid != pf->mac_seid) {
  8250. dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
  8251. /* find the one VEB connected to the MAC, and find orphans */
  8252. for (v = 0; v < I40E_MAX_VEB; v++) {
  8253. if (!pf->veb[v])
  8254. continue;
  8255. if (pf->veb[v]->uplink_seid == pf->mac_seid ||
  8256. pf->veb[v]->uplink_seid == 0) {
  8257. ret = i40e_reconstitute_veb(pf->veb[v]);
  8258. if (!ret)
  8259. continue;
  8260. /* If Main VEB failed, we're in deep doodoo,
  8261. * so give up rebuilding the switch and set up
  8262. * for minimal rebuild of PF VSI.
  8263. * If orphan failed, we'll report the error
  8264. * but try to keep going.
  8265. */
  8266. if (pf->veb[v]->uplink_seid == pf->mac_seid) {
  8267. dev_info(&pf->pdev->dev,
  8268. "rebuild of switch failed: %d, will try to set up simple PF connection\n",
  8269. ret);
  8270. vsi->uplink_seid = pf->mac_seid;
  8271. break;
  8272. } else if (pf->veb[v]->uplink_seid == 0) {
  8273. dev_info(&pf->pdev->dev,
  8274. "rebuild of orphan VEB failed: %d\n",
  8275. ret);
  8276. }
  8277. }
  8278. }
  8279. }
  8280. if (vsi->uplink_seid == pf->mac_seid) {
  8281. dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
  8282. /* no VEB, so rebuild only the Main VSI */
  8283. ret = i40e_add_vsi(vsi);
  8284. if (ret) {
  8285. dev_info(&pf->pdev->dev,
  8286. "rebuild of Main VSI failed: %d\n", ret);
  8287. goto end_unlock;
  8288. }
  8289. }
  8290. if (vsi->mqprio_qopt.max_rate[0]) {
  8291. u64 max_tx_rate = vsi->mqprio_qopt.max_rate[0];
  8292. u64 credits = 0;
  8293. do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
  8294. ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
  8295. if (ret)
  8296. goto end_unlock;
  8297. credits = max_tx_rate;
  8298. do_div(credits, I40E_BW_CREDIT_DIVISOR);
  8299. dev_dbg(&vsi->back->pdev->dev,
  8300. "Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
  8301. max_tx_rate,
  8302. credits,
  8303. vsi->seid);
  8304. }
  8305. ret = i40e_rebuild_cloud_filters(vsi, vsi->seid);
  8306. if (ret)
  8307. goto end_unlock;
  8308. /* PF Main VSI is rebuild by now, go ahead and rebuild channel VSIs
  8309. * for this main VSI if they exist
  8310. */
  8311. ret = i40e_rebuild_channels(vsi);
  8312. if (ret)
  8313. goto end_unlock;
  8314. /* Reconfigure hardware for allowing smaller MSS in the case
  8315. * of TSO, so that we avoid the MDD being fired and causing
  8316. * a reset in the case of small MSS+TSO.
  8317. */
  8318. #define I40E_REG_MSS 0x000E64DC
  8319. #define I40E_REG_MSS_MIN_MASK 0x3FF0000
  8320. #define I40E_64BYTE_MSS 0x400000
  8321. val = rd32(hw, I40E_REG_MSS);
  8322. if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
  8323. val &= ~I40E_REG_MSS_MIN_MASK;
  8324. val |= I40E_64BYTE_MSS;
  8325. wr32(hw, I40E_REG_MSS, val);
  8326. }
  8327. if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
  8328. msleep(75);
  8329. ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
  8330. if (ret)
  8331. dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
  8332. i40e_stat_str(&pf->hw, ret),
  8333. i40e_aq_str(&pf->hw,
  8334. pf->hw.aq.asq_last_status));
  8335. }
  8336. /* reinit the misc interrupt */
  8337. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  8338. ret = i40e_setup_misc_vector(pf);
  8339. /* Add a filter to drop all Flow control frames from any VSI from being
  8340. * transmitted. By doing so we stop a malicious VF from sending out
  8341. * PAUSE or PFC frames and potentially controlling traffic for other
  8342. * PF/VF VSIs.
  8343. * The FW can still send Flow control frames if enabled.
  8344. */
  8345. i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
  8346. pf->main_vsi_seid);
  8347. /* restart the VSIs that were rebuilt and running before the reset */
  8348. i40e_pf_unquiesce_all_vsi(pf);
  8349. /* Release the RTNL lock before we start resetting VFs */
  8350. if (!lock_acquired)
  8351. rtnl_unlock();
  8352. /* Restore promiscuous settings */
  8353. ret = i40e_set_promiscuous(pf, pf->cur_promisc);
  8354. if (ret)
  8355. dev_warn(&pf->pdev->dev,
  8356. "Failed to restore promiscuous setting: %s, err %s aq_err %s\n",
  8357. pf->cur_promisc ? "on" : "off",
  8358. i40e_stat_str(&pf->hw, ret),
  8359. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  8360. i40e_reset_all_vfs(pf, true);
  8361. /* tell the firmware that we're starting */
  8362. i40e_send_version(pf);
  8363. /* We've already released the lock, so don't do it again */
  8364. goto end_core_reset;
  8365. end_unlock:
  8366. if (!lock_acquired)
  8367. rtnl_unlock();
  8368. end_core_reset:
  8369. clear_bit(__I40E_RESET_FAILED, pf->state);
  8370. clear_recovery:
  8371. clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
  8372. }
  8373. /**
  8374. * i40e_reset_and_rebuild - reset and rebuild using a saved config
  8375. * @pf: board private structure
  8376. * @reinit: if the Main VSI needs to re-initialized.
  8377. * @lock_acquired: indicates whether or not the lock has been acquired
  8378. * before this function was called.
  8379. **/
  8380. static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
  8381. bool lock_acquired)
  8382. {
  8383. int ret;
  8384. /* Now we wait for GRST to settle out.
  8385. * We don't have to delete the VEBs or VSIs from the hw switch
  8386. * because the reset will make them disappear.
  8387. */
  8388. ret = i40e_reset(pf);
  8389. if (!ret)
  8390. i40e_rebuild(pf, reinit, lock_acquired);
  8391. }
  8392. /**
  8393. * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
  8394. * @pf: board private structure
  8395. *
  8396. * Close up the VFs and other things in prep for a Core Reset,
  8397. * then get ready to rebuild the world.
  8398. * @lock_acquired: indicates whether or not the lock has been acquired
  8399. * before this function was called.
  8400. **/
  8401. static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired)
  8402. {
  8403. i40e_prep_for_reset(pf, lock_acquired);
  8404. i40e_reset_and_rebuild(pf, false, lock_acquired);
  8405. }
  8406. /**
  8407. * i40e_handle_mdd_event
  8408. * @pf: pointer to the PF structure
  8409. *
  8410. * Called from the MDD irq handler to identify possibly malicious vfs
  8411. **/
  8412. static void i40e_handle_mdd_event(struct i40e_pf *pf)
  8413. {
  8414. struct i40e_hw *hw = &pf->hw;
  8415. bool mdd_detected = false;
  8416. bool pf_mdd_detected = false;
  8417. struct i40e_vf *vf;
  8418. u32 reg;
  8419. int i;
  8420. if (!test_bit(__I40E_MDD_EVENT_PENDING, pf->state))
  8421. return;
  8422. /* find what triggered the MDD event */
  8423. reg = rd32(hw, I40E_GL_MDET_TX);
  8424. if (reg & I40E_GL_MDET_TX_VALID_MASK) {
  8425. u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
  8426. I40E_GL_MDET_TX_PF_NUM_SHIFT;
  8427. u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
  8428. I40E_GL_MDET_TX_VF_NUM_SHIFT;
  8429. u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
  8430. I40E_GL_MDET_TX_EVENT_SHIFT;
  8431. u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
  8432. I40E_GL_MDET_TX_QUEUE_SHIFT) -
  8433. pf->hw.func_caps.base_queue;
  8434. if (netif_msg_tx_err(pf))
  8435. dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on TX queue %d PF number 0x%02x VF number 0x%02x\n",
  8436. event, queue, pf_num, vf_num);
  8437. wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
  8438. mdd_detected = true;
  8439. }
  8440. reg = rd32(hw, I40E_GL_MDET_RX);
  8441. if (reg & I40E_GL_MDET_RX_VALID_MASK) {
  8442. u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
  8443. I40E_GL_MDET_RX_FUNCTION_SHIFT;
  8444. u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
  8445. I40E_GL_MDET_RX_EVENT_SHIFT;
  8446. u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
  8447. I40E_GL_MDET_RX_QUEUE_SHIFT) -
  8448. pf->hw.func_caps.base_queue;
  8449. if (netif_msg_rx_err(pf))
  8450. dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
  8451. event, queue, func);
  8452. wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
  8453. mdd_detected = true;
  8454. }
  8455. if (mdd_detected) {
  8456. reg = rd32(hw, I40E_PF_MDET_TX);
  8457. if (reg & I40E_PF_MDET_TX_VALID_MASK) {
  8458. wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
  8459. dev_info(&pf->pdev->dev, "TX driver issue detected, PF reset issued\n");
  8460. pf_mdd_detected = true;
  8461. }
  8462. reg = rd32(hw, I40E_PF_MDET_RX);
  8463. if (reg & I40E_PF_MDET_RX_VALID_MASK) {
  8464. wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
  8465. dev_info(&pf->pdev->dev, "RX driver issue detected, PF reset issued\n");
  8466. pf_mdd_detected = true;
  8467. }
  8468. /* Queue belongs to the PF, initiate a reset */
  8469. if (pf_mdd_detected) {
  8470. set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
  8471. i40e_service_event_schedule(pf);
  8472. }
  8473. }
  8474. /* see if one of the VFs needs its hand slapped */
  8475. for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
  8476. vf = &(pf->vf[i]);
  8477. reg = rd32(hw, I40E_VP_MDET_TX(i));
  8478. if (reg & I40E_VP_MDET_TX_VALID_MASK) {
  8479. wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
  8480. vf->num_mdd_events++;
  8481. dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
  8482. i);
  8483. }
  8484. reg = rd32(hw, I40E_VP_MDET_RX(i));
  8485. if (reg & I40E_VP_MDET_RX_VALID_MASK) {
  8486. wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
  8487. vf->num_mdd_events++;
  8488. dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
  8489. i);
  8490. }
  8491. if (vf->num_mdd_events > I40E_DEFAULT_NUM_MDD_EVENTS_ALLOWED) {
  8492. dev_info(&pf->pdev->dev,
  8493. "Too many MDD events on VF %d, disabled\n", i);
  8494. dev_info(&pf->pdev->dev,
  8495. "Use PF Control I/F to re-enable the VF\n");
  8496. set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
  8497. }
  8498. }
  8499. /* re-enable mdd interrupt cause */
  8500. clear_bit(__I40E_MDD_EVENT_PENDING, pf->state);
  8501. reg = rd32(hw, I40E_PFINT_ICR0_ENA);
  8502. reg |= I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
  8503. wr32(hw, I40E_PFINT_ICR0_ENA, reg);
  8504. i40e_flush(hw);
  8505. }
  8506. static const char *i40e_tunnel_name(u8 type)
  8507. {
  8508. switch (type) {
  8509. case UDP_TUNNEL_TYPE_VXLAN:
  8510. return "vxlan";
  8511. case UDP_TUNNEL_TYPE_GENEVE:
  8512. return "geneve";
  8513. default:
  8514. return "unknown";
  8515. }
  8516. }
  8517. /**
  8518. * i40e_sync_udp_filters - Trigger a sync event for existing UDP filters
  8519. * @pf: board private structure
  8520. **/
  8521. static void i40e_sync_udp_filters(struct i40e_pf *pf)
  8522. {
  8523. int i;
  8524. /* loop through and set pending bit for all active UDP filters */
  8525. for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
  8526. if (pf->udp_ports[i].port)
  8527. pf->pending_udp_bitmap |= BIT_ULL(i);
  8528. }
  8529. set_bit(__I40E_UDP_FILTER_SYNC_PENDING, pf->state);
  8530. }
  8531. /**
  8532. * i40e_sync_udp_filters_subtask - Sync the VSI filter list with HW
  8533. * @pf: board private structure
  8534. **/
  8535. static void i40e_sync_udp_filters_subtask(struct i40e_pf *pf)
  8536. {
  8537. struct i40e_hw *hw = &pf->hw;
  8538. u8 filter_index, type;
  8539. u16 port;
  8540. int i;
  8541. if (!test_and_clear_bit(__I40E_UDP_FILTER_SYNC_PENDING, pf->state))
  8542. return;
  8543. /* acquire RTNL to maintain state of flags and port requests */
  8544. rtnl_lock();
  8545. for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
  8546. if (pf->pending_udp_bitmap & BIT_ULL(i)) {
  8547. struct i40e_udp_port_config *udp_port;
  8548. i40e_status ret = 0;
  8549. udp_port = &pf->udp_ports[i];
  8550. pf->pending_udp_bitmap &= ~BIT_ULL(i);
  8551. port = READ_ONCE(udp_port->port);
  8552. type = READ_ONCE(udp_port->type);
  8553. filter_index = READ_ONCE(udp_port->filter_index);
  8554. /* release RTNL while we wait on AQ command */
  8555. rtnl_unlock();
  8556. if (port)
  8557. ret = i40e_aq_add_udp_tunnel(hw, port,
  8558. type,
  8559. &filter_index,
  8560. NULL);
  8561. else if (filter_index != I40E_UDP_PORT_INDEX_UNUSED)
  8562. ret = i40e_aq_del_udp_tunnel(hw, filter_index,
  8563. NULL);
  8564. /* reacquire RTNL so we can update filter_index */
  8565. rtnl_lock();
  8566. if (ret) {
  8567. dev_info(&pf->pdev->dev,
  8568. "%s %s port %d, index %d failed, err %s aq_err %s\n",
  8569. i40e_tunnel_name(type),
  8570. port ? "add" : "delete",
  8571. port,
  8572. filter_index,
  8573. i40e_stat_str(&pf->hw, ret),
  8574. i40e_aq_str(&pf->hw,
  8575. pf->hw.aq.asq_last_status));
  8576. if (port) {
  8577. /* failed to add, just reset port,
  8578. * drop pending bit for any deletion
  8579. */
  8580. udp_port->port = 0;
  8581. pf->pending_udp_bitmap &= ~BIT_ULL(i);
  8582. }
  8583. } else if (port) {
  8584. /* record filter index on success */
  8585. udp_port->filter_index = filter_index;
  8586. }
  8587. }
  8588. }
  8589. rtnl_unlock();
  8590. }
  8591. /**
  8592. * i40e_service_task - Run the driver's async subtasks
  8593. * @work: pointer to work_struct containing our data
  8594. **/
  8595. static void i40e_service_task(struct work_struct *work)
  8596. {
  8597. struct i40e_pf *pf = container_of(work,
  8598. struct i40e_pf,
  8599. service_task);
  8600. unsigned long start_time = jiffies;
  8601. /* don't bother with service tasks if a reset is in progress */
  8602. if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
  8603. return;
  8604. if (test_and_set_bit(__I40E_SERVICE_SCHED, pf->state))
  8605. return;
  8606. i40e_detect_recover_hung(pf->vsi[pf->lan_vsi]);
  8607. i40e_sync_filters_subtask(pf);
  8608. i40e_reset_subtask(pf);
  8609. i40e_handle_mdd_event(pf);
  8610. i40e_vc_process_vflr_event(pf);
  8611. i40e_watchdog_subtask(pf);
  8612. i40e_fdir_reinit_subtask(pf);
  8613. if (test_and_clear_bit(__I40E_CLIENT_RESET, pf->state)) {
  8614. /* Client subtask will reopen next time through. */
  8615. i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], true);
  8616. } else {
  8617. i40e_client_subtask(pf);
  8618. if (test_and_clear_bit(__I40E_CLIENT_L2_CHANGE,
  8619. pf->state))
  8620. i40e_notify_client_of_l2_param_changes(
  8621. pf->vsi[pf->lan_vsi]);
  8622. }
  8623. i40e_sync_filters_subtask(pf);
  8624. i40e_sync_udp_filters_subtask(pf);
  8625. i40e_clean_adminq_subtask(pf);
  8626. /* flush memory to make sure state is correct before next watchdog */
  8627. smp_mb__before_atomic();
  8628. clear_bit(__I40E_SERVICE_SCHED, pf->state);
  8629. /* If the tasks have taken longer than one timer cycle or there
  8630. * is more work to be done, reschedule the service task now
  8631. * rather than wait for the timer to tick again.
  8632. */
  8633. if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
  8634. test_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state) ||
  8635. test_bit(__I40E_MDD_EVENT_PENDING, pf->state) ||
  8636. test_bit(__I40E_VFLR_EVENT_PENDING, pf->state))
  8637. i40e_service_event_schedule(pf);
  8638. }
  8639. /**
  8640. * i40e_service_timer - timer callback
  8641. * @data: pointer to PF struct
  8642. **/
  8643. static void i40e_service_timer(struct timer_list *t)
  8644. {
  8645. struct i40e_pf *pf = from_timer(pf, t, service_timer);
  8646. mod_timer(&pf->service_timer,
  8647. round_jiffies(jiffies + pf->service_timer_period));
  8648. i40e_service_event_schedule(pf);
  8649. }
  8650. /**
  8651. * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
  8652. * @vsi: the VSI being configured
  8653. **/
  8654. static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
  8655. {
  8656. struct i40e_pf *pf = vsi->back;
  8657. switch (vsi->type) {
  8658. case I40E_VSI_MAIN:
  8659. vsi->alloc_queue_pairs = pf->num_lan_qps;
  8660. vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
  8661. I40E_REQ_DESCRIPTOR_MULTIPLE);
  8662. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  8663. vsi->num_q_vectors = pf->num_lan_msix;
  8664. else
  8665. vsi->num_q_vectors = 1;
  8666. break;
  8667. case I40E_VSI_FDIR:
  8668. vsi->alloc_queue_pairs = 1;
  8669. vsi->num_desc = ALIGN(I40E_FDIR_RING_COUNT,
  8670. I40E_REQ_DESCRIPTOR_MULTIPLE);
  8671. vsi->num_q_vectors = pf->num_fdsb_msix;
  8672. break;
  8673. case I40E_VSI_VMDQ2:
  8674. vsi->alloc_queue_pairs = pf->num_vmdq_qps;
  8675. vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
  8676. I40E_REQ_DESCRIPTOR_MULTIPLE);
  8677. vsi->num_q_vectors = pf->num_vmdq_msix;
  8678. break;
  8679. case I40E_VSI_SRIOV:
  8680. vsi->alloc_queue_pairs = pf->num_vf_qps;
  8681. vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
  8682. I40E_REQ_DESCRIPTOR_MULTIPLE);
  8683. break;
  8684. default:
  8685. WARN_ON(1);
  8686. return -ENODATA;
  8687. }
  8688. return 0;
  8689. }
  8690. /**
  8691. * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
  8692. * @vsi: VSI pointer
  8693. * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
  8694. *
  8695. * On error: returns error code (negative)
  8696. * On success: returns 0
  8697. **/
  8698. static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
  8699. {
  8700. struct i40e_ring **next_rings;
  8701. int size;
  8702. int ret = 0;
  8703. /* allocate memory for both Tx, XDP Tx and Rx ring pointers */
  8704. size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs *
  8705. (i40e_enabled_xdp_vsi(vsi) ? 3 : 2);
  8706. vsi->tx_rings = kzalloc(size, GFP_KERNEL);
  8707. if (!vsi->tx_rings)
  8708. return -ENOMEM;
  8709. next_rings = vsi->tx_rings + vsi->alloc_queue_pairs;
  8710. if (i40e_enabled_xdp_vsi(vsi)) {
  8711. vsi->xdp_rings = next_rings;
  8712. next_rings += vsi->alloc_queue_pairs;
  8713. }
  8714. vsi->rx_rings = next_rings;
  8715. if (alloc_qvectors) {
  8716. /* allocate memory for q_vector pointers */
  8717. size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
  8718. vsi->q_vectors = kzalloc(size, GFP_KERNEL);
  8719. if (!vsi->q_vectors) {
  8720. ret = -ENOMEM;
  8721. goto err_vectors;
  8722. }
  8723. }
  8724. return ret;
  8725. err_vectors:
  8726. kfree(vsi->tx_rings);
  8727. return ret;
  8728. }
  8729. /**
  8730. * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
  8731. * @pf: board private structure
  8732. * @type: type of VSI
  8733. *
  8734. * On error: returns error code (negative)
  8735. * On success: returns vsi index in PF (positive)
  8736. **/
  8737. static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
  8738. {
  8739. int ret = -ENODEV;
  8740. struct i40e_vsi *vsi;
  8741. int vsi_idx;
  8742. int i;
  8743. /* Need to protect the allocation of the VSIs at the PF level */
  8744. mutex_lock(&pf->switch_mutex);
  8745. /* VSI list may be fragmented if VSI creation/destruction has
  8746. * been happening. We can afford to do a quick scan to look
  8747. * for any free VSIs in the list.
  8748. *
  8749. * find next empty vsi slot, looping back around if necessary
  8750. */
  8751. i = pf->next_vsi;
  8752. while (i < pf->num_alloc_vsi && pf->vsi[i])
  8753. i++;
  8754. if (i >= pf->num_alloc_vsi) {
  8755. i = 0;
  8756. while (i < pf->next_vsi && pf->vsi[i])
  8757. i++;
  8758. }
  8759. if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
  8760. vsi_idx = i; /* Found one! */
  8761. } else {
  8762. ret = -ENODEV;
  8763. goto unlock_pf; /* out of VSI slots! */
  8764. }
  8765. pf->next_vsi = ++i;
  8766. vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
  8767. if (!vsi) {
  8768. ret = -ENOMEM;
  8769. goto unlock_pf;
  8770. }
  8771. vsi->type = type;
  8772. vsi->back = pf;
  8773. set_bit(__I40E_VSI_DOWN, vsi->state);
  8774. vsi->flags = 0;
  8775. vsi->idx = vsi_idx;
  8776. vsi->int_rate_limit = 0;
  8777. vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
  8778. pf->rss_table_size : 64;
  8779. vsi->netdev_registered = false;
  8780. vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
  8781. hash_init(vsi->mac_filter_hash);
  8782. vsi->irqs_ready = false;
  8783. ret = i40e_set_num_rings_in_vsi(vsi);
  8784. if (ret)
  8785. goto err_rings;
  8786. ret = i40e_vsi_alloc_arrays(vsi, true);
  8787. if (ret)
  8788. goto err_rings;
  8789. /* Setup default MSIX irq handler for VSI */
  8790. i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
  8791. /* Initialize VSI lock */
  8792. spin_lock_init(&vsi->mac_filter_hash_lock);
  8793. pf->vsi[vsi_idx] = vsi;
  8794. ret = vsi_idx;
  8795. goto unlock_pf;
  8796. err_rings:
  8797. pf->next_vsi = i - 1;
  8798. kfree(vsi);
  8799. unlock_pf:
  8800. mutex_unlock(&pf->switch_mutex);
  8801. return ret;
  8802. }
  8803. /**
  8804. * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
  8805. * @vsi: VSI pointer
  8806. * @free_qvectors: a bool to specify if q_vectors need to be freed.
  8807. *
  8808. * On error: returns error code (negative)
  8809. * On success: returns 0
  8810. **/
  8811. static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
  8812. {
  8813. /* free the ring and vector containers */
  8814. if (free_qvectors) {
  8815. kfree(vsi->q_vectors);
  8816. vsi->q_vectors = NULL;
  8817. }
  8818. kfree(vsi->tx_rings);
  8819. vsi->tx_rings = NULL;
  8820. vsi->rx_rings = NULL;
  8821. vsi->xdp_rings = NULL;
  8822. }
  8823. /**
  8824. * i40e_clear_rss_config_user - clear the user configured RSS hash keys
  8825. * and lookup table
  8826. * @vsi: Pointer to VSI structure
  8827. */
  8828. static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
  8829. {
  8830. if (!vsi)
  8831. return;
  8832. kfree(vsi->rss_hkey_user);
  8833. vsi->rss_hkey_user = NULL;
  8834. kfree(vsi->rss_lut_user);
  8835. vsi->rss_lut_user = NULL;
  8836. }
  8837. /**
  8838. * i40e_vsi_clear - Deallocate the VSI provided
  8839. * @vsi: the VSI being un-configured
  8840. **/
  8841. static int i40e_vsi_clear(struct i40e_vsi *vsi)
  8842. {
  8843. struct i40e_pf *pf;
  8844. if (!vsi)
  8845. return 0;
  8846. if (!vsi->back)
  8847. goto free_vsi;
  8848. pf = vsi->back;
  8849. mutex_lock(&pf->switch_mutex);
  8850. if (!pf->vsi[vsi->idx]) {
  8851. dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](type %d)\n",
  8852. vsi->idx, vsi->idx, vsi->type);
  8853. goto unlock_vsi;
  8854. }
  8855. if (pf->vsi[vsi->idx] != vsi) {
  8856. dev_err(&pf->pdev->dev,
  8857. "pf->vsi[%d](type %d) != vsi[%d](type %d): no free!\n",
  8858. pf->vsi[vsi->idx]->idx,
  8859. pf->vsi[vsi->idx]->type,
  8860. vsi->idx, vsi->type);
  8861. goto unlock_vsi;
  8862. }
  8863. /* updates the PF for this cleared vsi */
  8864. i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
  8865. i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
  8866. i40e_vsi_free_arrays(vsi, true);
  8867. i40e_clear_rss_config_user(vsi);
  8868. pf->vsi[vsi->idx] = NULL;
  8869. if (vsi->idx < pf->next_vsi)
  8870. pf->next_vsi = vsi->idx;
  8871. unlock_vsi:
  8872. mutex_unlock(&pf->switch_mutex);
  8873. free_vsi:
  8874. kfree(vsi);
  8875. return 0;
  8876. }
  8877. /**
  8878. * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
  8879. * @vsi: the VSI being cleaned
  8880. **/
  8881. static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
  8882. {
  8883. int i;
  8884. if (vsi->tx_rings && vsi->tx_rings[0]) {
  8885. for (i = 0; i < vsi->alloc_queue_pairs; i++) {
  8886. kfree_rcu(vsi->tx_rings[i], rcu);
  8887. vsi->tx_rings[i] = NULL;
  8888. vsi->rx_rings[i] = NULL;
  8889. if (vsi->xdp_rings)
  8890. vsi->xdp_rings[i] = NULL;
  8891. }
  8892. }
  8893. }
  8894. /**
  8895. * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
  8896. * @vsi: the VSI being configured
  8897. **/
  8898. static int i40e_alloc_rings(struct i40e_vsi *vsi)
  8899. {
  8900. int i, qpv = i40e_enabled_xdp_vsi(vsi) ? 3 : 2;
  8901. struct i40e_pf *pf = vsi->back;
  8902. struct i40e_ring *ring;
  8903. /* Set basic values in the rings to be used later during open() */
  8904. for (i = 0; i < vsi->alloc_queue_pairs; i++) {
  8905. /* allocate space for both Tx and Rx in one shot */
  8906. ring = kcalloc(qpv, sizeof(struct i40e_ring), GFP_KERNEL);
  8907. if (!ring)
  8908. goto err_out;
  8909. ring->queue_index = i;
  8910. ring->reg_idx = vsi->base_queue + i;
  8911. ring->ring_active = false;
  8912. ring->vsi = vsi;
  8913. ring->netdev = vsi->netdev;
  8914. ring->dev = &pf->pdev->dev;
  8915. ring->count = vsi->num_desc;
  8916. ring->size = 0;
  8917. ring->dcb_tc = 0;
  8918. if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
  8919. ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
  8920. ring->itr_setting = pf->tx_itr_default;
  8921. vsi->tx_rings[i] = ring++;
  8922. if (!i40e_enabled_xdp_vsi(vsi))
  8923. goto setup_rx;
  8924. ring->queue_index = vsi->alloc_queue_pairs + i;
  8925. ring->reg_idx = vsi->base_queue + ring->queue_index;
  8926. ring->ring_active = false;
  8927. ring->vsi = vsi;
  8928. ring->netdev = NULL;
  8929. ring->dev = &pf->pdev->dev;
  8930. ring->count = vsi->num_desc;
  8931. ring->size = 0;
  8932. ring->dcb_tc = 0;
  8933. if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
  8934. ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
  8935. set_ring_xdp(ring);
  8936. ring->itr_setting = pf->tx_itr_default;
  8937. vsi->xdp_rings[i] = ring++;
  8938. setup_rx:
  8939. ring->queue_index = i;
  8940. ring->reg_idx = vsi->base_queue + i;
  8941. ring->ring_active = false;
  8942. ring->vsi = vsi;
  8943. ring->netdev = vsi->netdev;
  8944. ring->dev = &pf->pdev->dev;
  8945. ring->count = vsi->num_desc;
  8946. ring->size = 0;
  8947. ring->dcb_tc = 0;
  8948. ring->itr_setting = pf->rx_itr_default;
  8949. vsi->rx_rings[i] = ring;
  8950. }
  8951. return 0;
  8952. err_out:
  8953. i40e_vsi_clear_rings(vsi);
  8954. return -ENOMEM;
  8955. }
  8956. /**
  8957. * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
  8958. * @pf: board private structure
  8959. * @vectors: the number of MSI-X vectors to request
  8960. *
  8961. * Returns the number of vectors reserved, or error
  8962. **/
  8963. static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
  8964. {
  8965. vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
  8966. I40E_MIN_MSIX, vectors);
  8967. if (vectors < 0) {
  8968. dev_info(&pf->pdev->dev,
  8969. "MSI-X vector reservation failed: %d\n", vectors);
  8970. vectors = 0;
  8971. }
  8972. return vectors;
  8973. }
  8974. /**
  8975. * i40e_init_msix - Setup the MSIX capability
  8976. * @pf: board private structure
  8977. *
  8978. * Work with the OS to set up the MSIX vectors needed.
  8979. *
  8980. * Returns the number of vectors reserved or negative on failure
  8981. **/
  8982. static int i40e_init_msix(struct i40e_pf *pf)
  8983. {
  8984. struct i40e_hw *hw = &pf->hw;
  8985. int cpus, extra_vectors;
  8986. int vectors_left;
  8987. int v_budget, i;
  8988. int v_actual;
  8989. int iwarp_requested = 0;
  8990. if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
  8991. return -ENODEV;
  8992. /* The number of vectors we'll request will be comprised of:
  8993. * - Add 1 for "other" cause for Admin Queue events, etc.
  8994. * - The number of LAN queue pairs
  8995. * - Queues being used for RSS.
  8996. * We don't need as many as max_rss_size vectors.
  8997. * use rss_size instead in the calculation since that
  8998. * is governed by number of cpus in the system.
  8999. * - assumes symmetric Tx/Rx pairing
  9000. * - The number of VMDq pairs
  9001. * - The CPU count within the NUMA node if iWARP is enabled
  9002. * Once we count this up, try the request.
  9003. *
  9004. * If we can't get what we want, we'll simplify to nearly nothing
  9005. * and try again. If that still fails, we punt.
  9006. */
  9007. vectors_left = hw->func_caps.num_msix_vectors;
  9008. v_budget = 0;
  9009. /* reserve one vector for miscellaneous handler */
  9010. if (vectors_left) {
  9011. v_budget++;
  9012. vectors_left--;
  9013. }
  9014. /* reserve some vectors for the main PF traffic queues. Initially we
  9015. * only reserve at most 50% of the available vectors, in the case that
  9016. * the number of online CPUs is large. This ensures that we can enable
  9017. * extra features as well. Once we've enabled the other features, we
  9018. * will use any remaining vectors to reach as close as we can to the
  9019. * number of online CPUs.
  9020. */
  9021. cpus = num_online_cpus();
  9022. pf->num_lan_msix = min_t(int, cpus, vectors_left / 2);
  9023. vectors_left -= pf->num_lan_msix;
  9024. /* reserve one vector for sideband flow director */
  9025. if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
  9026. if (vectors_left) {
  9027. pf->num_fdsb_msix = 1;
  9028. v_budget++;
  9029. vectors_left--;
  9030. } else {
  9031. pf->num_fdsb_msix = 0;
  9032. }
  9033. }
  9034. /* can we reserve enough for iWARP? */
  9035. if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
  9036. iwarp_requested = pf->num_iwarp_msix;
  9037. if (!vectors_left)
  9038. pf->num_iwarp_msix = 0;
  9039. else if (vectors_left < pf->num_iwarp_msix)
  9040. pf->num_iwarp_msix = 1;
  9041. v_budget += pf->num_iwarp_msix;
  9042. vectors_left -= pf->num_iwarp_msix;
  9043. }
  9044. /* any vectors left over go for VMDq support */
  9045. if (pf->flags & I40E_FLAG_VMDQ_ENABLED) {
  9046. if (!vectors_left) {
  9047. pf->num_vmdq_msix = 0;
  9048. pf->num_vmdq_qps = 0;
  9049. } else {
  9050. int vmdq_vecs_wanted =
  9051. pf->num_vmdq_vsis * pf->num_vmdq_qps;
  9052. int vmdq_vecs =
  9053. min_t(int, vectors_left, vmdq_vecs_wanted);
  9054. /* if we're short on vectors for what's desired, we limit
  9055. * the queues per vmdq. If this is still more than are
  9056. * available, the user will need to change the number of
  9057. * queues/vectors used by the PF later with the ethtool
  9058. * channels command
  9059. */
  9060. if (vectors_left < vmdq_vecs_wanted) {
  9061. pf->num_vmdq_qps = 1;
  9062. vmdq_vecs_wanted = pf->num_vmdq_vsis;
  9063. vmdq_vecs = min_t(int,
  9064. vectors_left,
  9065. vmdq_vecs_wanted);
  9066. }
  9067. pf->num_vmdq_msix = pf->num_vmdq_qps;
  9068. v_budget += vmdq_vecs;
  9069. vectors_left -= vmdq_vecs;
  9070. }
  9071. }
  9072. /* On systems with a large number of SMP cores, we previously limited
  9073. * the number of vectors for num_lan_msix to be at most 50% of the
  9074. * available vectors, to allow for other features. Now, we add back
  9075. * the remaining vectors. However, we ensure that the total
  9076. * num_lan_msix will not exceed num_online_cpus(). To do this, we
  9077. * calculate the number of vectors we can add without going over the
  9078. * cap of CPUs. For systems with a small number of CPUs this will be
  9079. * zero.
  9080. */
  9081. extra_vectors = min_t(int, cpus - pf->num_lan_msix, vectors_left);
  9082. pf->num_lan_msix += extra_vectors;
  9083. vectors_left -= extra_vectors;
  9084. WARN(vectors_left < 0,
  9085. "Calculation of remaining vectors underflowed. This is an accounting bug when determining total MSI-X vectors.\n");
  9086. v_budget += pf->num_lan_msix;
  9087. pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
  9088. GFP_KERNEL);
  9089. if (!pf->msix_entries)
  9090. return -ENOMEM;
  9091. for (i = 0; i < v_budget; i++)
  9092. pf->msix_entries[i].entry = i;
  9093. v_actual = i40e_reserve_msix_vectors(pf, v_budget);
  9094. if (v_actual < I40E_MIN_MSIX) {
  9095. pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
  9096. kfree(pf->msix_entries);
  9097. pf->msix_entries = NULL;
  9098. pci_disable_msix(pf->pdev);
  9099. return -ENODEV;
  9100. } else if (v_actual == I40E_MIN_MSIX) {
  9101. /* Adjust for minimal MSIX use */
  9102. pf->num_vmdq_vsis = 0;
  9103. pf->num_vmdq_qps = 0;
  9104. pf->num_lan_qps = 1;
  9105. pf->num_lan_msix = 1;
  9106. } else if (v_actual != v_budget) {
  9107. /* If we have limited resources, we will start with no vectors
  9108. * for the special features and then allocate vectors to some
  9109. * of these features based on the policy and at the end disable
  9110. * the features that did not get any vectors.
  9111. */
  9112. int vec;
  9113. dev_info(&pf->pdev->dev,
  9114. "MSI-X vector limit reached with %d, wanted %d, attempting to redistribute vectors\n",
  9115. v_actual, v_budget);
  9116. /* reserve the misc vector */
  9117. vec = v_actual - 1;
  9118. /* Scale vector usage down */
  9119. pf->num_vmdq_msix = 1; /* force VMDqs to only one vector */
  9120. pf->num_vmdq_vsis = 1;
  9121. pf->num_vmdq_qps = 1;
  9122. /* partition out the remaining vectors */
  9123. switch (vec) {
  9124. case 2:
  9125. pf->num_lan_msix = 1;
  9126. break;
  9127. case 3:
  9128. if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
  9129. pf->num_lan_msix = 1;
  9130. pf->num_iwarp_msix = 1;
  9131. } else {
  9132. pf->num_lan_msix = 2;
  9133. }
  9134. break;
  9135. default:
  9136. if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
  9137. pf->num_iwarp_msix = min_t(int, (vec / 3),
  9138. iwarp_requested);
  9139. pf->num_vmdq_vsis = min_t(int, (vec / 3),
  9140. I40E_DEFAULT_NUM_VMDQ_VSI);
  9141. } else {
  9142. pf->num_vmdq_vsis = min_t(int, (vec / 2),
  9143. I40E_DEFAULT_NUM_VMDQ_VSI);
  9144. }
  9145. if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
  9146. pf->num_fdsb_msix = 1;
  9147. vec--;
  9148. }
  9149. pf->num_lan_msix = min_t(int,
  9150. (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
  9151. pf->num_lan_msix);
  9152. pf->num_lan_qps = pf->num_lan_msix;
  9153. break;
  9154. }
  9155. }
  9156. if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
  9157. (pf->num_fdsb_msix == 0)) {
  9158. dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n");
  9159. pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
  9160. pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
  9161. }
  9162. if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
  9163. (pf->num_vmdq_msix == 0)) {
  9164. dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
  9165. pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
  9166. }
  9167. if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
  9168. (pf->num_iwarp_msix == 0)) {
  9169. dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
  9170. pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
  9171. }
  9172. i40e_debug(&pf->hw, I40E_DEBUG_INIT,
  9173. "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n",
  9174. pf->num_lan_msix,
  9175. pf->num_vmdq_msix * pf->num_vmdq_vsis,
  9176. pf->num_fdsb_msix,
  9177. pf->num_iwarp_msix);
  9178. return v_actual;
  9179. }
  9180. /**
  9181. * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
  9182. * @vsi: the VSI being configured
  9183. * @v_idx: index of the vector in the vsi struct
  9184. * @cpu: cpu to be used on affinity_mask
  9185. *
  9186. * We allocate one q_vector. If allocation fails we return -ENOMEM.
  9187. **/
  9188. static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx, int cpu)
  9189. {
  9190. struct i40e_q_vector *q_vector;
  9191. /* allocate q_vector */
  9192. q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
  9193. if (!q_vector)
  9194. return -ENOMEM;
  9195. q_vector->vsi = vsi;
  9196. q_vector->v_idx = v_idx;
  9197. cpumask_copy(&q_vector->affinity_mask, cpu_possible_mask);
  9198. if (vsi->netdev)
  9199. netif_napi_add(vsi->netdev, &q_vector->napi,
  9200. i40e_napi_poll, NAPI_POLL_WEIGHT);
  9201. /* tie q_vector and vsi together */
  9202. vsi->q_vectors[v_idx] = q_vector;
  9203. return 0;
  9204. }
  9205. /**
  9206. * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
  9207. * @vsi: the VSI being configured
  9208. *
  9209. * We allocate one q_vector per queue interrupt. If allocation fails we
  9210. * return -ENOMEM.
  9211. **/
  9212. static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
  9213. {
  9214. struct i40e_pf *pf = vsi->back;
  9215. int err, v_idx, num_q_vectors, current_cpu;
  9216. /* if not MSIX, give the one vector only to the LAN VSI */
  9217. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  9218. num_q_vectors = vsi->num_q_vectors;
  9219. else if (vsi == pf->vsi[pf->lan_vsi])
  9220. num_q_vectors = 1;
  9221. else
  9222. return -EINVAL;
  9223. current_cpu = cpumask_first(cpu_online_mask);
  9224. for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
  9225. err = i40e_vsi_alloc_q_vector(vsi, v_idx, current_cpu);
  9226. if (err)
  9227. goto err_out;
  9228. current_cpu = cpumask_next(current_cpu, cpu_online_mask);
  9229. if (unlikely(current_cpu >= nr_cpu_ids))
  9230. current_cpu = cpumask_first(cpu_online_mask);
  9231. }
  9232. return 0;
  9233. err_out:
  9234. while (v_idx--)
  9235. i40e_free_q_vector(vsi, v_idx);
  9236. return err;
  9237. }
  9238. /**
  9239. * i40e_init_interrupt_scheme - Determine proper interrupt scheme
  9240. * @pf: board private structure to initialize
  9241. **/
  9242. static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
  9243. {
  9244. int vectors = 0;
  9245. ssize_t size;
  9246. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  9247. vectors = i40e_init_msix(pf);
  9248. if (vectors < 0) {
  9249. pf->flags &= ~(I40E_FLAG_MSIX_ENABLED |
  9250. I40E_FLAG_IWARP_ENABLED |
  9251. I40E_FLAG_RSS_ENABLED |
  9252. I40E_FLAG_DCB_CAPABLE |
  9253. I40E_FLAG_DCB_ENABLED |
  9254. I40E_FLAG_SRIOV_ENABLED |
  9255. I40E_FLAG_FD_SB_ENABLED |
  9256. I40E_FLAG_FD_ATR_ENABLED |
  9257. I40E_FLAG_VMDQ_ENABLED);
  9258. pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
  9259. /* rework the queue expectations without MSIX */
  9260. i40e_determine_queue_usage(pf);
  9261. }
  9262. }
  9263. if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
  9264. (pf->flags & I40E_FLAG_MSI_ENABLED)) {
  9265. dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
  9266. vectors = pci_enable_msi(pf->pdev);
  9267. if (vectors < 0) {
  9268. dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
  9269. vectors);
  9270. pf->flags &= ~I40E_FLAG_MSI_ENABLED;
  9271. }
  9272. vectors = 1; /* one MSI or Legacy vector */
  9273. }
  9274. if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
  9275. dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
  9276. /* set up vector assignment tracking */
  9277. size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
  9278. pf->irq_pile = kzalloc(size, GFP_KERNEL);
  9279. if (!pf->irq_pile)
  9280. return -ENOMEM;
  9281. pf->irq_pile->num_entries = vectors;
  9282. pf->irq_pile->search_hint = 0;
  9283. /* track first vector for misc interrupts, ignore return */
  9284. (void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
  9285. return 0;
  9286. }
  9287. /**
  9288. * i40e_restore_interrupt_scheme - Restore the interrupt scheme
  9289. * @pf: private board data structure
  9290. *
  9291. * Restore the interrupt scheme that was cleared when we suspended the
  9292. * device. This should be called during resume to re-allocate the q_vectors
  9293. * and reacquire IRQs.
  9294. */
  9295. static int i40e_restore_interrupt_scheme(struct i40e_pf *pf)
  9296. {
  9297. int err, i;
  9298. /* We cleared the MSI and MSI-X flags when disabling the old interrupt
  9299. * scheme. We need to re-enabled them here in order to attempt to
  9300. * re-acquire the MSI or MSI-X vectors
  9301. */
  9302. pf->flags |= (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
  9303. err = i40e_init_interrupt_scheme(pf);
  9304. if (err)
  9305. return err;
  9306. /* Now that we've re-acquired IRQs, we need to remap the vectors and
  9307. * rings together again.
  9308. */
  9309. for (i = 0; i < pf->num_alloc_vsi; i++) {
  9310. if (pf->vsi[i]) {
  9311. err = i40e_vsi_alloc_q_vectors(pf->vsi[i]);
  9312. if (err)
  9313. goto err_unwind;
  9314. i40e_vsi_map_rings_to_vectors(pf->vsi[i]);
  9315. }
  9316. }
  9317. err = i40e_setup_misc_vector(pf);
  9318. if (err)
  9319. goto err_unwind;
  9320. if (pf->flags & I40E_FLAG_IWARP_ENABLED)
  9321. i40e_client_update_msix_info(pf);
  9322. return 0;
  9323. err_unwind:
  9324. while (i--) {
  9325. if (pf->vsi[i])
  9326. i40e_vsi_free_q_vectors(pf->vsi[i]);
  9327. }
  9328. return err;
  9329. }
  9330. /**
  9331. * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
  9332. * @pf: board private structure
  9333. *
  9334. * This sets up the handler for MSIX 0, which is used to manage the
  9335. * non-queue interrupts, e.g. AdminQ and errors. This is not used
  9336. * when in MSI or Legacy interrupt mode.
  9337. **/
  9338. static int i40e_setup_misc_vector(struct i40e_pf *pf)
  9339. {
  9340. struct i40e_hw *hw = &pf->hw;
  9341. int err = 0;
  9342. /* Only request the IRQ once, the first time through. */
  9343. if (!test_and_set_bit(__I40E_MISC_IRQ_REQUESTED, pf->state)) {
  9344. err = request_irq(pf->msix_entries[0].vector,
  9345. i40e_intr, 0, pf->int_name, pf);
  9346. if (err) {
  9347. clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
  9348. dev_info(&pf->pdev->dev,
  9349. "request_irq for %s failed: %d\n",
  9350. pf->int_name, err);
  9351. return -EFAULT;
  9352. }
  9353. }
  9354. i40e_enable_misc_int_causes(pf);
  9355. /* associate no queues to the misc vector */
  9356. wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
  9357. wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K);
  9358. i40e_flush(hw);
  9359. i40e_irq_dynamic_enable_icr0(pf);
  9360. return err;
  9361. }
  9362. /**
  9363. * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
  9364. * @vsi: Pointer to vsi structure
  9365. * @seed: Buffter to store the hash keys
  9366. * @lut: Buffer to store the lookup table entries
  9367. * @lut_size: Size of buffer to store the lookup table entries
  9368. *
  9369. * Return 0 on success, negative on failure
  9370. */
  9371. static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
  9372. u8 *lut, u16 lut_size)
  9373. {
  9374. struct i40e_pf *pf = vsi->back;
  9375. struct i40e_hw *hw = &pf->hw;
  9376. int ret = 0;
  9377. if (seed) {
  9378. ret = i40e_aq_get_rss_key(hw, vsi->id,
  9379. (struct i40e_aqc_get_set_rss_key_data *)seed);
  9380. if (ret) {
  9381. dev_info(&pf->pdev->dev,
  9382. "Cannot get RSS key, err %s aq_err %s\n",
  9383. i40e_stat_str(&pf->hw, ret),
  9384. i40e_aq_str(&pf->hw,
  9385. pf->hw.aq.asq_last_status));
  9386. return ret;
  9387. }
  9388. }
  9389. if (lut) {
  9390. bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
  9391. ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
  9392. if (ret) {
  9393. dev_info(&pf->pdev->dev,
  9394. "Cannot get RSS lut, err %s aq_err %s\n",
  9395. i40e_stat_str(&pf->hw, ret),
  9396. i40e_aq_str(&pf->hw,
  9397. pf->hw.aq.asq_last_status));
  9398. return ret;
  9399. }
  9400. }
  9401. return ret;
  9402. }
  9403. /**
  9404. * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
  9405. * @vsi: Pointer to vsi structure
  9406. * @seed: RSS hash seed
  9407. * @lut: Lookup table
  9408. * @lut_size: Lookup table size
  9409. *
  9410. * Returns 0 on success, negative on failure
  9411. **/
  9412. static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
  9413. const u8 *lut, u16 lut_size)
  9414. {
  9415. struct i40e_pf *pf = vsi->back;
  9416. struct i40e_hw *hw = &pf->hw;
  9417. u16 vf_id = vsi->vf_id;
  9418. u8 i;
  9419. /* Fill out hash function seed */
  9420. if (seed) {
  9421. u32 *seed_dw = (u32 *)seed;
  9422. if (vsi->type == I40E_VSI_MAIN) {
  9423. for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
  9424. wr32(hw, I40E_PFQF_HKEY(i), seed_dw[i]);
  9425. } else if (vsi->type == I40E_VSI_SRIOV) {
  9426. for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
  9427. wr32(hw, I40E_VFQF_HKEY1(i, vf_id), seed_dw[i]);
  9428. } else {
  9429. dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
  9430. }
  9431. }
  9432. if (lut) {
  9433. u32 *lut_dw = (u32 *)lut;
  9434. if (vsi->type == I40E_VSI_MAIN) {
  9435. if (lut_size != I40E_HLUT_ARRAY_SIZE)
  9436. return -EINVAL;
  9437. for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
  9438. wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
  9439. } else if (vsi->type == I40E_VSI_SRIOV) {
  9440. if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
  9441. return -EINVAL;
  9442. for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
  9443. wr32(hw, I40E_VFQF_HLUT1(i, vf_id), lut_dw[i]);
  9444. } else {
  9445. dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
  9446. }
  9447. }
  9448. i40e_flush(hw);
  9449. return 0;
  9450. }
  9451. /**
  9452. * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
  9453. * @vsi: Pointer to VSI structure
  9454. * @seed: Buffer to store the keys
  9455. * @lut: Buffer to store the lookup table entries
  9456. * @lut_size: Size of buffer to store the lookup table entries
  9457. *
  9458. * Returns 0 on success, negative on failure
  9459. */
  9460. static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
  9461. u8 *lut, u16 lut_size)
  9462. {
  9463. struct i40e_pf *pf = vsi->back;
  9464. struct i40e_hw *hw = &pf->hw;
  9465. u16 i;
  9466. if (seed) {
  9467. u32 *seed_dw = (u32 *)seed;
  9468. for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
  9469. seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
  9470. }
  9471. if (lut) {
  9472. u32 *lut_dw = (u32 *)lut;
  9473. if (lut_size != I40E_HLUT_ARRAY_SIZE)
  9474. return -EINVAL;
  9475. for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
  9476. lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
  9477. }
  9478. return 0;
  9479. }
  9480. /**
  9481. * i40e_config_rss - Configure RSS keys and lut
  9482. * @vsi: Pointer to VSI structure
  9483. * @seed: RSS hash seed
  9484. * @lut: Lookup table
  9485. * @lut_size: Lookup table size
  9486. *
  9487. * Returns 0 on success, negative on failure
  9488. */
  9489. int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
  9490. {
  9491. struct i40e_pf *pf = vsi->back;
  9492. if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
  9493. return i40e_config_rss_aq(vsi, seed, lut, lut_size);
  9494. else
  9495. return i40e_config_rss_reg(vsi, seed, lut, lut_size);
  9496. }
  9497. /**
  9498. * i40e_get_rss - Get RSS keys and lut
  9499. * @vsi: Pointer to VSI structure
  9500. * @seed: Buffer to store the keys
  9501. * @lut: Buffer to store the lookup table entries
  9502. * @lut_size: Size of buffer to store the lookup table entries
  9503. *
  9504. * Returns 0 on success, negative on failure
  9505. */
  9506. int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
  9507. {
  9508. struct i40e_pf *pf = vsi->back;
  9509. if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
  9510. return i40e_get_rss_aq(vsi, seed, lut, lut_size);
  9511. else
  9512. return i40e_get_rss_reg(vsi, seed, lut, lut_size);
  9513. }
  9514. /**
  9515. * i40e_fill_rss_lut - Fill the RSS lookup table with default values
  9516. * @pf: Pointer to board private structure
  9517. * @lut: Lookup table
  9518. * @rss_table_size: Lookup table size
  9519. * @rss_size: Range of queue number for hashing
  9520. */
  9521. void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
  9522. u16 rss_table_size, u16 rss_size)
  9523. {
  9524. u16 i;
  9525. for (i = 0; i < rss_table_size; i++)
  9526. lut[i] = i % rss_size;
  9527. }
  9528. /**
  9529. * i40e_pf_config_rss - Prepare for RSS if used
  9530. * @pf: board private structure
  9531. **/
  9532. static int i40e_pf_config_rss(struct i40e_pf *pf)
  9533. {
  9534. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  9535. u8 seed[I40E_HKEY_ARRAY_SIZE];
  9536. u8 *lut;
  9537. struct i40e_hw *hw = &pf->hw;
  9538. u32 reg_val;
  9539. u64 hena;
  9540. int ret;
  9541. /* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
  9542. hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
  9543. ((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
  9544. hena |= i40e_pf_get_default_rss_hena(pf);
  9545. i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
  9546. i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
  9547. /* Determine the RSS table size based on the hardware capabilities */
  9548. reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
  9549. reg_val = (pf->rss_table_size == 512) ?
  9550. (reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
  9551. (reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
  9552. i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
  9553. /* Determine the RSS size of the VSI */
  9554. if (!vsi->rss_size) {
  9555. u16 qcount;
  9556. /* If the firmware does something weird during VSI init, we
  9557. * could end up with zero TCs. Check for that to avoid
  9558. * divide-by-zero. It probably won't pass traffic, but it also
  9559. * won't panic.
  9560. */
  9561. qcount = vsi->num_queue_pairs /
  9562. (vsi->tc_config.numtc ? vsi->tc_config.numtc : 1);
  9563. vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
  9564. }
  9565. if (!vsi->rss_size)
  9566. return -EINVAL;
  9567. lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
  9568. if (!lut)
  9569. return -ENOMEM;
  9570. /* Use user configured lut if there is one, otherwise use default */
  9571. if (vsi->rss_lut_user)
  9572. memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
  9573. else
  9574. i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
  9575. /* Use user configured hash key if there is one, otherwise
  9576. * use default.
  9577. */
  9578. if (vsi->rss_hkey_user)
  9579. memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
  9580. else
  9581. netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
  9582. ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
  9583. kfree(lut);
  9584. return ret;
  9585. }
  9586. /**
  9587. * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
  9588. * @pf: board private structure
  9589. * @queue_count: the requested queue count for rss.
  9590. *
  9591. * returns 0 if rss is not enabled, if enabled returns the final rss queue
  9592. * count which may be different from the requested queue count.
  9593. * Note: expects to be called while under rtnl_lock()
  9594. **/
  9595. int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
  9596. {
  9597. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  9598. int new_rss_size;
  9599. if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
  9600. return 0;
  9601. new_rss_size = min_t(int, queue_count, pf->rss_size_max);
  9602. if (queue_count != vsi->num_queue_pairs) {
  9603. u16 qcount;
  9604. vsi->req_queue_pairs = queue_count;
  9605. i40e_prep_for_reset(pf, true);
  9606. pf->alloc_rss_size = new_rss_size;
  9607. i40e_reset_and_rebuild(pf, true, true);
  9608. /* Discard the user configured hash keys and lut, if less
  9609. * queues are enabled.
  9610. */
  9611. if (queue_count < vsi->rss_size) {
  9612. i40e_clear_rss_config_user(vsi);
  9613. dev_dbg(&pf->pdev->dev,
  9614. "discard user configured hash keys and lut\n");
  9615. }
  9616. /* Reset vsi->rss_size, as number of enabled queues changed */
  9617. qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
  9618. vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
  9619. i40e_pf_config_rss(pf);
  9620. }
  9621. dev_info(&pf->pdev->dev, "User requested queue count/HW max RSS count: %d/%d\n",
  9622. vsi->req_queue_pairs, pf->rss_size_max);
  9623. return pf->alloc_rss_size;
  9624. }
  9625. /**
  9626. * i40e_get_partition_bw_setting - Retrieve BW settings for this PF partition
  9627. * @pf: board private structure
  9628. **/
  9629. i40e_status i40e_get_partition_bw_setting(struct i40e_pf *pf)
  9630. {
  9631. i40e_status status;
  9632. bool min_valid, max_valid;
  9633. u32 max_bw, min_bw;
  9634. status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
  9635. &min_valid, &max_valid);
  9636. if (!status) {
  9637. if (min_valid)
  9638. pf->min_bw = min_bw;
  9639. if (max_valid)
  9640. pf->max_bw = max_bw;
  9641. }
  9642. return status;
  9643. }
  9644. /**
  9645. * i40e_set_partition_bw_setting - Set BW settings for this PF partition
  9646. * @pf: board private structure
  9647. **/
  9648. i40e_status i40e_set_partition_bw_setting(struct i40e_pf *pf)
  9649. {
  9650. struct i40e_aqc_configure_partition_bw_data bw_data;
  9651. i40e_status status;
  9652. /* Set the valid bit for this PF */
  9653. bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
  9654. bw_data.max_bw[pf->hw.pf_id] = pf->max_bw & I40E_ALT_BW_VALUE_MASK;
  9655. bw_data.min_bw[pf->hw.pf_id] = pf->min_bw & I40E_ALT_BW_VALUE_MASK;
  9656. /* Set the new bandwidths */
  9657. status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
  9658. return status;
  9659. }
  9660. /**
  9661. * i40e_commit_partition_bw_setting - Commit BW settings for this PF partition
  9662. * @pf: board private structure
  9663. **/
  9664. i40e_status i40e_commit_partition_bw_setting(struct i40e_pf *pf)
  9665. {
  9666. /* Commit temporary BW setting to permanent NVM image */
  9667. enum i40e_admin_queue_err last_aq_status;
  9668. i40e_status ret;
  9669. u16 nvm_word;
  9670. if (pf->hw.partition_id != 1) {
  9671. dev_info(&pf->pdev->dev,
  9672. "Commit BW only works on partition 1! This is partition %d",
  9673. pf->hw.partition_id);
  9674. ret = I40E_NOT_SUPPORTED;
  9675. goto bw_commit_out;
  9676. }
  9677. /* Acquire NVM for read access */
  9678. ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
  9679. last_aq_status = pf->hw.aq.asq_last_status;
  9680. if (ret) {
  9681. dev_info(&pf->pdev->dev,
  9682. "Cannot acquire NVM for read access, err %s aq_err %s\n",
  9683. i40e_stat_str(&pf->hw, ret),
  9684. i40e_aq_str(&pf->hw, last_aq_status));
  9685. goto bw_commit_out;
  9686. }
  9687. /* Read word 0x10 of NVM - SW compatibility word 1 */
  9688. ret = i40e_aq_read_nvm(&pf->hw,
  9689. I40E_SR_NVM_CONTROL_WORD,
  9690. 0x10, sizeof(nvm_word), &nvm_word,
  9691. false, NULL);
  9692. /* Save off last admin queue command status before releasing
  9693. * the NVM
  9694. */
  9695. last_aq_status = pf->hw.aq.asq_last_status;
  9696. i40e_release_nvm(&pf->hw);
  9697. if (ret) {
  9698. dev_info(&pf->pdev->dev, "NVM read error, err %s aq_err %s\n",
  9699. i40e_stat_str(&pf->hw, ret),
  9700. i40e_aq_str(&pf->hw, last_aq_status));
  9701. goto bw_commit_out;
  9702. }
  9703. /* Wait a bit for NVM release to complete */
  9704. msleep(50);
  9705. /* Acquire NVM for write access */
  9706. ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
  9707. last_aq_status = pf->hw.aq.asq_last_status;
  9708. if (ret) {
  9709. dev_info(&pf->pdev->dev,
  9710. "Cannot acquire NVM for write access, err %s aq_err %s\n",
  9711. i40e_stat_str(&pf->hw, ret),
  9712. i40e_aq_str(&pf->hw, last_aq_status));
  9713. goto bw_commit_out;
  9714. }
  9715. /* Write it back out unchanged to initiate update NVM,
  9716. * which will force a write of the shadow (alt) RAM to
  9717. * the NVM - thus storing the bandwidth values permanently.
  9718. */
  9719. ret = i40e_aq_update_nvm(&pf->hw,
  9720. I40E_SR_NVM_CONTROL_WORD,
  9721. 0x10, sizeof(nvm_word),
  9722. &nvm_word, true, 0, NULL);
  9723. /* Save off last admin queue command status before releasing
  9724. * the NVM
  9725. */
  9726. last_aq_status = pf->hw.aq.asq_last_status;
  9727. i40e_release_nvm(&pf->hw);
  9728. if (ret)
  9729. dev_info(&pf->pdev->dev,
  9730. "BW settings NOT SAVED, err %s aq_err %s\n",
  9731. i40e_stat_str(&pf->hw, ret),
  9732. i40e_aq_str(&pf->hw, last_aq_status));
  9733. bw_commit_out:
  9734. return ret;
  9735. }
  9736. /**
  9737. * i40e_sw_init - Initialize general software structures (struct i40e_pf)
  9738. * @pf: board private structure to initialize
  9739. *
  9740. * i40e_sw_init initializes the Adapter private data structure.
  9741. * Fields are initialized based on PCI device information and
  9742. * OS network device settings (MTU size).
  9743. **/
  9744. static int i40e_sw_init(struct i40e_pf *pf)
  9745. {
  9746. int err = 0;
  9747. int size;
  9748. /* Set default capability flags */
  9749. pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
  9750. I40E_FLAG_MSI_ENABLED |
  9751. I40E_FLAG_MSIX_ENABLED;
  9752. /* Set default ITR */
  9753. pf->rx_itr_default = I40E_ITR_RX_DEF;
  9754. pf->tx_itr_default = I40E_ITR_TX_DEF;
  9755. /* Depending on PF configurations, it is possible that the RSS
  9756. * maximum might end up larger than the available queues
  9757. */
  9758. pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
  9759. pf->alloc_rss_size = 1;
  9760. pf->rss_table_size = pf->hw.func_caps.rss_table_size;
  9761. pf->rss_size_max = min_t(int, pf->rss_size_max,
  9762. pf->hw.func_caps.num_tx_qp);
  9763. if (pf->hw.func_caps.rss) {
  9764. pf->flags |= I40E_FLAG_RSS_ENABLED;
  9765. pf->alloc_rss_size = min_t(int, pf->rss_size_max,
  9766. num_online_cpus());
  9767. }
  9768. /* MFP mode enabled */
  9769. if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
  9770. pf->flags |= I40E_FLAG_MFP_ENABLED;
  9771. dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
  9772. if (i40e_get_partition_bw_setting(pf)) {
  9773. dev_warn(&pf->pdev->dev,
  9774. "Could not get partition bw settings\n");
  9775. } else {
  9776. dev_info(&pf->pdev->dev,
  9777. "Partition BW Min = %8.8x, Max = %8.8x\n",
  9778. pf->min_bw, pf->max_bw);
  9779. /* nudge the Tx scheduler */
  9780. i40e_set_partition_bw_setting(pf);
  9781. }
  9782. }
  9783. if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
  9784. (pf->hw.func_caps.fd_filters_best_effort > 0)) {
  9785. pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
  9786. pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
  9787. if (pf->flags & I40E_FLAG_MFP_ENABLED &&
  9788. pf->hw.num_partitions > 1)
  9789. dev_info(&pf->pdev->dev,
  9790. "Flow Director Sideband mode Disabled in MFP mode\n");
  9791. else
  9792. pf->flags |= I40E_FLAG_FD_SB_ENABLED;
  9793. pf->fdir_pf_filter_count =
  9794. pf->hw.func_caps.fd_filters_guaranteed;
  9795. pf->hw.fdir_shared_filter_count =
  9796. pf->hw.func_caps.fd_filters_best_effort;
  9797. }
  9798. if (pf->hw.mac.type == I40E_MAC_X722) {
  9799. pf->hw_features |= (I40E_HW_RSS_AQ_CAPABLE |
  9800. I40E_HW_128_QP_RSS_CAPABLE |
  9801. I40E_HW_ATR_EVICT_CAPABLE |
  9802. I40E_HW_WB_ON_ITR_CAPABLE |
  9803. I40E_HW_MULTIPLE_TCP_UDP_RSS_PCTYPE |
  9804. I40E_HW_NO_PCI_LINK_CHECK |
  9805. I40E_HW_USE_SET_LLDP_MIB |
  9806. I40E_HW_GENEVE_OFFLOAD_CAPABLE |
  9807. I40E_HW_PTP_L4_CAPABLE |
  9808. I40E_HW_WOL_MC_MAGIC_PKT_WAKE |
  9809. I40E_HW_OUTER_UDP_CSUM_CAPABLE);
  9810. #define I40E_FDEVICT_PCTYPE_DEFAULT 0xc03
  9811. if (rd32(&pf->hw, I40E_GLQF_FDEVICTENA(1)) !=
  9812. I40E_FDEVICT_PCTYPE_DEFAULT) {
  9813. dev_warn(&pf->pdev->dev,
  9814. "FD EVICT PCTYPES are not right, disable FD HW EVICT\n");
  9815. pf->hw_features &= ~I40E_HW_ATR_EVICT_CAPABLE;
  9816. }
  9817. } else if ((pf->hw.aq.api_maj_ver > 1) ||
  9818. ((pf->hw.aq.api_maj_ver == 1) &&
  9819. (pf->hw.aq.api_min_ver > 4))) {
  9820. /* Supported in FW API version higher than 1.4 */
  9821. pf->hw_features |= I40E_HW_GENEVE_OFFLOAD_CAPABLE;
  9822. }
  9823. /* Enable HW ATR eviction if possible */
  9824. if (pf->hw_features & I40E_HW_ATR_EVICT_CAPABLE)
  9825. pf->flags |= I40E_FLAG_HW_ATR_EVICT_ENABLED;
  9826. if ((pf->hw.mac.type == I40E_MAC_XL710) &&
  9827. (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
  9828. (pf->hw.aq.fw_maj_ver < 4))) {
  9829. pf->hw_features |= I40E_HW_RESTART_AUTONEG;
  9830. /* No DCB support for FW < v4.33 */
  9831. pf->hw_features |= I40E_HW_NO_DCB_SUPPORT;
  9832. }
  9833. /* Disable FW LLDP if FW < v4.3 */
  9834. if ((pf->hw.mac.type == I40E_MAC_XL710) &&
  9835. (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
  9836. (pf->hw.aq.fw_maj_ver < 4)))
  9837. pf->hw_features |= I40E_HW_STOP_FW_LLDP;
  9838. /* Use the FW Set LLDP MIB API if FW > v4.40 */
  9839. if ((pf->hw.mac.type == I40E_MAC_XL710) &&
  9840. (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver >= 40)) ||
  9841. (pf->hw.aq.fw_maj_ver >= 5)))
  9842. pf->hw_features |= I40E_HW_USE_SET_LLDP_MIB;
  9843. /* Enable PTP L4 if FW > v6.0 */
  9844. if (pf->hw.mac.type == I40E_MAC_XL710 &&
  9845. pf->hw.aq.fw_maj_ver >= 6)
  9846. pf->hw_features |= I40E_HW_PTP_L4_CAPABLE;
  9847. if (pf->hw.func_caps.vmdq && num_online_cpus() != 1) {
  9848. pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
  9849. pf->flags |= I40E_FLAG_VMDQ_ENABLED;
  9850. pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
  9851. }
  9852. if (pf->hw.func_caps.iwarp && num_online_cpus() != 1) {
  9853. pf->flags |= I40E_FLAG_IWARP_ENABLED;
  9854. /* IWARP needs one extra vector for CQP just like MISC.*/
  9855. pf->num_iwarp_msix = (int)num_online_cpus() + 1;
  9856. }
  9857. /* Stopping the FW LLDP engine is only supported on the
  9858. * XL710 with a FW ver >= 1.7. Also, stopping FW LLDP
  9859. * engine is not supported if NPAR is functioning on this
  9860. * part
  9861. */
  9862. if (pf->hw.mac.type == I40E_MAC_XL710 &&
  9863. !pf->hw.func_caps.npar_enable &&
  9864. (pf->hw.aq.api_maj_ver > 1 ||
  9865. (pf->hw.aq.api_maj_ver == 1 && pf->hw.aq.api_min_ver > 6)))
  9866. pf->hw_features |= I40E_HW_STOPPABLE_FW_LLDP;
  9867. #ifdef CONFIG_PCI_IOV
  9868. if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
  9869. pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
  9870. pf->flags |= I40E_FLAG_SRIOV_ENABLED;
  9871. pf->num_req_vfs = min_t(int,
  9872. pf->hw.func_caps.num_vfs,
  9873. I40E_MAX_VF_COUNT);
  9874. }
  9875. #endif /* CONFIG_PCI_IOV */
  9876. pf->eeprom_version = 0xDEAD;
  9877. pf->lan_veb = I40E_NO_VEB;
  9878. pf->lan_vsi = I40E_NO_VSI;
  9879. /* By default FW has this off for performance reasons */
  9880. pf->flags &= ~I40E_FLAG_VEB_STATS_ENABLED;
  9881. /* set up queue assignment tracking */
  9882. size = sizeof(struct i40e_lump_tracking)
  9883. + (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
  9884. pf->qp_pile = kzalloc(size, GFP_KERNEL);
  9885. if (!pf->qp_pile) {
  9886. err = -ENOMEM;
  9887. goto sw_init_done;
  9888. }
  9889. pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
  9890. pf->qp_pile->search_hint = 0;
  9891. pf->tx_timeout_recovery_level = 1;
  9892. mutex_init(&pf->switch_mutex);
  9893. sw_init_done:
  9894. return err;
  9895. }
  9896. /**
  9897. * i40e_set_ntuple - set the ntuple feature flag and take action
  9898. * @pf: board private structure to initialize
  9899. * @features: the feature set that the stack is suggesting
  9900. *
  9901. * returns a bool to indicate if reset needs to happen
  9902. **/
  9903. bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
  9904. {
  9905. bool need_reset = false;
  9906. /* Check if Flow Director n-tuple support was enabled or disabled. If
  9907. * the state changed, we need to reset.
  9908. */
  9909. if (features & NETIF_F_NTUPLE) {
  9910. /* Enable filters and mark for reset */
  9911. if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
  9912. need_reset = true;
  9913. /* enable FD_SB only if there is MSI-X vector and no cloud
  9914. * filters exist
  9915. */
  9916. if (pf->num_fdsb_msix > 0 && !pf->num_cloud_filters) {
  9917. pf->flags |= I40E_FLAG_FD_SB_ENABLED;
  9918. pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
  9919. }
  9920. } else {
  9921. /* turn off filters, mark for reset and clear SW filter list */
  9922. if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
  9923. need_reset = true;
  9924. i40e_fdir_filter_exit(pf);
  9925. }
  9926. pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
  9927. clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state);
  9928. pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
  9929. /* reset fd counters */
  9930. pf->fd_add_err = 0;
  9931. pf->fd_atr_cnt = 0;
  9932. /* if ATR was auto disabled it can be re-enabled. */
  9933. if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
  9934. if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
  9935. (I40E_DEBUG_FD & pf->hw.debug_mask))
  9936. dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
  9937. }
  9938. return need_reset;
  9939. }
  9940. /**
  9941. * i40e_clear_rss_lut - clear the rx hash lookup table
  9942. * @vsi: the VSI being configured
  9943. **/
  9944. static void i40e_clear_rss_lut(struct i40e_vsi *vsi)
  9945. {
  9946. struct i40e_pf *pf = vsi->back;
  9947. struct i40e_hw *hw = &pf->hw;
  9948. u16 vf_id = vsi->vf_id;
  9949. u8 i;
  9950. if (vsi->type == I40E_VSI_MAIN) {
  9951. for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
  9952. wr32(hw, I40E_PFQF_HLUT(i), 0);
  9953. } else if (vsi->type == I40E_VSI_SRIOV) {
  9954. for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
  9955. i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0);
  9956. } else {
  9957. dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
  9958. }
  9959. }
  9960. /**
  9961. * i40e_set_features - set the netdev feature flags
  9962. * @netdev: ptr to the netdev being adjusted
  9963. * @features: the feature set that the stack is suggesting
  9964. * Note: expects to be called while under rtnl_lock()
  9965. **/
  9966. static int i40e_set_features(struct net_device *netdev,
  9967. netdev_features_t features)
  9968. {
  9969. struct i40e_netdev_priv *np = netdev_priv(netdev);
  9970. struct i40e_vsi *vsi = np->vsi;
  9971. struct i40e_pf *pf = vsi->back;
  9972. bool need_reset;
  9973. if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
  9974. i40e_pf_config_rss(pf);
  9975. else if (!(features & NETIF_F_RXHASH) &&
  9976. netdev->features & NETIF_F_RXHASH)
  9977. i40e_clear_rss_lut(vsi);
  9978. if (features & NETIF_F_HW_VLAN_CTAG_RX)
  9979. i40e_vlan_stripping_enable(vsi);
  9980. else
  9981. i40e_vlan_stripping_disable(vsi);
  9982. if (!(features & NETIF_F_HW_TC) && pf->num_cloud_filters) {
  9983. dev_err(&pf->pdev->dev,
  9984. "Offloaded tc filters active, can't turn hw_tc_offload off");
  9985. return -EINVAL;
  9986. }
  9987. need_reset = i40e_set_ntuple(pf, features);
  9988. if (need_reset)
  9989. i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
  9990. return 0;
  9991. }
  9992. /**
  9993. * i40e_get_udp_port_idx - Lookup a possibly offloaded for Rx UDP port
  9994. * @pf: board private structure
  9995. * @port: The UDP port to look up
  9996. *
  9997. * Returns the index number or I40E_MAX_PF_UDP_OFFLOAD_PORTS if port not found
  9998. **/
  9999. static u8 i40e_get_udp_port_idx(struct i40e_pf *pf, u16 port)
  10000. {
  10001. u8 i;
  10002. for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
  10003. /* Do not report ports with pending deletions as
  10004. * being available.
  10005. */
  10006. if (!port && (pf->pending_udp_bitmap & BIT_ULL(i)))
  10007. continue;
  10008. if (pf->udp_ports[i].port == port)
  10009. return i;
  10010. }
  10011. return i;
  10012. }
  10013. /**
  10014. * i40e_udp_tunnel_add - Get notifications about UDP tunnel ports that come up
  10015. * @netdev: This physical port's netdev
  10016. * @ti: Tunnel endpoint information
  10017. **/
  10018. static void i40e_udp_tunnel_add(struct net_device *netdev,
  10019. struct udp_tunnel_info *ti)
  10020. {
  10021. struct i40e_netdev_priv *np = netdev_priv(netdev);
  10022. struct i40e_vsi *vsi = np->vsi;
  10023. struct i40e_pf *pf = vsi->back;
  10024. u16 port = ntohs(ti->port);
  10025. u8 next_idx;
  10026. u8 idx;
  10027. idx = i40e_get_udp_port_idx(pf, port);
  10028. /* Check if port already exists */
  10029. if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
  10030. netdev_info(netdev, "port %d already offloaded\n", port);
  10031. return;
  10032. }
  10033. /* Now check if there is space to add the new port */
  10034. next_idx = i40e_get_udp_port_idx(pf, 0);
  10035. if (next_idx == I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
  10036. netdev_info(netdev, "maximum number of offloaded UDP ports reached, not adding port %d\n",
  10037. port);
  10038. return;
  10039. }
  10040. switch (ti->type) {
  10041. case UDP_TUNNEL_TYPE_VXLAN:
  10042. pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_VXLAN;
  10043. break;
  10044. case UDP_TUNNEL_TYPE_GENEVE:
  10045. if (!(pf->hw_features & I40E_HW_GENEVE_OFFLOAD_CAPABLE))
  10046. return;
  10047. pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_NGE;
  10048. break;
  10049. default:
  10050. return;
  10051. }
  10052. /* New port: add it and mark its index in the bitmap */
  10053. pf->udp_ports[next_idx].port = port;
  10054. pf->udp_ports[next_idx].filter_index = I40E_UDP_PORT_INDEX_UNUSED;
  10055. pf->pending_udp_bitmap |= BIT_ULL(next_idx);
  10056. set_bit(__I40E_UDP_FILTER_SYNC_PENDING, pf->state);
  10057. }
  10058. /**
  10059. * i40e_udp_tunnel_del - Get notifications about UDP tunnel ports that go away
  10060. * @netdev: This physical port's netdev
  10061. * @ti: Tunnel endpoint information
  10062. **/
  10063. static void i40e_udp_tunnel_del(struct net_device *netdev,
  10064. struct udp_tunnel_info *ti)
  10065. {
  10066. struct i40e_netdev_priv *np = netdev_priv(netdev);
  10067. struct i40e_vsi *vsi = np->vsi;
  10068. struct i40e_pf *pf = vsi->back;
  10069. u16 port = ntohs(ti->port);
  10070. u8 idx;
  10071. idx = i40e_get_udp_port_idx(pf, port);
  10072. /* Check if port already exists */
  10073. if (idx >= I40E_MAX_PF_UDP_OFFLOAD_PORTS)
  10074. goto not_found;
  10075. switch (ti->type) {
  10076. case UDP_TUNNEL_TYPE_VXLAN:
  10077. if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_VXLAN)
  10078. goto not_found;
  10079. break;
  10080. case UDP_TUNNEL_TYPE_GENEVE:
  10081. if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_NGE)
  10082. goto not_found;
  10083. break;
  10084. default:
  10085. goto not_found;
  10086. }
  10087. /* if port exists, set it to 0 (mark for deletion)
  10088. * and make it pending
  10089. */
  10090. pf->udp_ports[idx].port = 0;
  10091. /* Toggle pending bit instead of setting it. This way if we are
  10092. * deleting a port that has yet to be added we just clear the pending
  10093. * bit and don't have to worry about it.
  10094. */
  10095. pf->pending_udp_bitmap ^= BIT_ULL(idx);
  10096. set_bit(__I40E_UDP_FILTER_SYNC_PENDING, pf->state);
  10097. return;
  10098. not_found:
  10099. netdev_warn(netdev, "UDP port %d was not found, not deleting\n",
  10100. port);
  10101. }
  10102. static int i40e_get_phys_port_id(struct net_device *netdev,
  10103. struct netdev_phys_item_id *ppid)
  10104. {
  10105. struct i40e_netdev_priv *np = netdev_priv(netdev);
  10106. struct i40e_pf *pf = np->vsi->back;
  10107. struct i40e_hw *hw = &pf->hw;
  10108. if (!(pf->hw_features & I40E_HW_PORT_ID_VALID))
  10109. return -EOPNOTSUPP;
  10110. ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
  10111. memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
  10112. return 0;
  10113. }
  10114. /**
  10115. * i40e_ndo_fdb_add - add an entry to the hardware database
  10116. * @ndm: the input from the stack
  10117. * @tb: pointer to array of nladdr (unused)
  10118. * @dev: the net device pointer
  10119. * @addr: the MAC address entry being added
  10120. * @vid: VLAN ID
  10121. * @flags: instructions from stack about fdb operation
  10122. */
  10123. static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
  10124. struct net_device *dev,
  10125. const unsigned char *addr, u16 vid,
  10126. u16 flags)
  10127. {
  10128. struct i40e_netdev_priv *np = netdev_priv(dev);
  10129. struct i40e_pf *pf = np->vsi->back;
  10130. int err = 0;
  10131. if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
  10132. return -EOPNOTSUPP;
  10133. if (vid) {
  10134. pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
  10135. return -EINVAL;
  10136. }
  10137. /* Hardware does not support aging addresses so if a
  10138. * ndm_state is given only allow permanent addresses
  10139. */
  10140. if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
  10141. netdev_info(dev, "FDB only supports static addresses\n");
  10142. return -EINVAL;
  10143. }
  10144. if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
  10145. err = dev_uc_add_excl(dev, addr);
  10146. else if (is_multicast_ether_addr(addr))
  10147. err = dev_mc_add_excl(dev, addr);
  10148. else
  10149. err = -EINVAL;
  10150. /* Only return duplicate errors if NLM_F_EXCL is set */
  10151. if (err == -EEXIST && !(flags & NLM_F_EXCL))
  10152. err = 0;
  10153. return err;
  10154. }
  10155. /**
  10156. * i40e_ndo_bridge_setlink - Set the hardware bridge mode
  10157. * @dev: the netdev being configured
  10158. * @nlh: RTNL message
  10159. * @flags: bridge flags
  10160. *
  10161. * Inserts a new hardware bridge if not already created and
  10162. * enables the bridging mode requested (VEB or VEPA). If the
  10163. * hardware bridge has already been inserted and the request
  10164. * is to change the mode then that requires a PF reset to
  10165. * allow rebuild of the components with required hardware
  10166. * bridge mode enabled.
  10167. *
  10168. * Note: expects to be called while under rtnl_lock()
  10169. **/
  10170. static int i40e_ndo_bridge_setlink(struct net_device *dev,
  10171. struct nlmsghdr *nlh,
  10172. u16 flags)
  10173. {
  10174. struct i40e_netdev_priv *np = netdev_priv(dev);
  10175. struct i40e_vsi *vsi = np->vsi;
  10176. struct i40e_pf *pf = vsi->back;
  10177. struct i40e_veb *veb = NULL;
  10178. struct nlattr *attr, *br_spec;
  10179. int i, rem;
  10180. /* Only for PF VSI for now */
  10181. if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
  10182. return -EOPNOTSUPP;
  10183. /* Find the HW bridge for PF VSI */
  10184. for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
  10185. if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
  10186. veb = pf->veb[i];
  10187. }
  10188. br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
  10189. nla_for_each_nested(attr, br_spec, rem) {
  10190. __u16 mode;
  10191. if (nla_type(attr) != IFLA_BRIDGE_MODE)
  10192. continue;
  10193. mode = nla_get_u16(attr);
  10194. if ((mode != BRIDGE_MODE_VEPA) &&
  10195. (mode != BRIDGE_MODE_VEB))
  10196. return -EINVAL;
  10197. /* Insert a new HW bridge */
  10198. if (!veb) {
  10199. veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
  10200. vsi->tc_config.enabled_tc);
  10201. if (veb) {
  10202. veb->bridge_mode = mode;
  10203. i40e_config_bridge_mode(veb);
  10204. } else {
  10205. /* No Bridge HW offload available */
  10206. return -ENOENT;
  10207. }
  10208. break;
  10209. } else if (mode != veb->bridge_mode) {
  10210. /* Existing HW bridge but different mode needs reset */
  10211. veb->bridge_mode = mode;
  10212. /* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
  10213. if (mode == BRIDGE_MODE_VEB)
  10214. pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
  10215. else
  10216. pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
  10217. i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
  10218. break;
  10219. }
  10220. }
  10221. return 0;
  10222. }
  10223. /**
  10224. * i40e_ndo_bridge_getlink - Get the hardware bridge mode
  10225. * @skb: skb buff
  10226. * @pid: process id
  10227. * @seq: RTNL message seq #
  10228. * @dev: the netdev being configured
  10229. * @filter_mask: unused
  10230. * @nlflags: netlink flags passed in
  10231. *
  10232. * Return the mode in which the hardware bridge is operating in
  10233. * i.e VEB or VEPA.
  10234. **/
  10235. static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
  10236. struct net_device *dev,
  10237. u32 __always_unused filter_mask,
  10238. int nlflags)
  10239. {
  10240. struct i40e_netdev_priv *np = netdev_priv(dev);
  10241. struct i40e_vsi *vsi = np->vsi;
  10242. struct i40e_pf *pf = vsi->back;
  10243. struct i40e_veb *veb = NULL;
  10244. int i;
  10245. /* Only for PF VSI for now */
  10246. if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
  10247. return -EOPNOTSUPP;
  10248. /* Find the HW bridge for the PF VSI */
  10249. for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
  10250. if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
  10251. veb = pf->veb[i];
  10252. }
  10253. if (!veb)
  10254. return 0;
  10255. return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
  10256. 0, 0, nlflags, filter_mask, NULL);
  10257. }
  10258. /**
  10259. * i40e_features_check - Validate encapsulated packet conforms to limits
  10260. * @skb: skb buff
  10261. * @dev: This physical port's netdev
  10262. * @features: Offload features that the stack believes apply
  10263. **/
  10264. static netdev_features_t i40e_features_check(struct sk_buff *skb,
  10265. struct net_device *dev,
  10266. netdev_features_t features)
  10267. {
  10268. size_t len;
  10269. /* No point in doing any of this if neither checksum nor GSO are
  10270. * being requested for this frame. We can rule out both by just
  10271. * checking for CHECKSUM_PARTIAL
  10272. */
  10273. if (skb->ip_summed != CHECKSUM_PARTIAL)
  10274. return features;
  10275. /* We cannot support GSO if the MSS is going to be less than
  10276. * 64 bytes. If it is then we need to drop support for GSO.
  10277. */
  10278. if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
  10279. features &= ~NETIF_F_GSO_MASK;
  10280. /* MACLEN can support at most 63 words */
  10281. len = skb_network_header(skb) - skb->data;
  10282. if (len & ~(63 * 2))
  10283. goto out_err;
  10284. /* IPLEN and EIPLEN can support at most 127 dwords */
  10285. len = skb_transport_header(skb) - skb_network_header(skb);
  10286. if (len & ~(127 * 4))
  10287. goto out_err;
  10288. if (skb->encapsulation) {
  10289. /* L4TUNLEN can support 127 words */
  10290. len = skb_inner_network_header(skb) - skb_transport_header(skb);
  10291. if (len & ~(127 * 2))
  10292. goto out_err;
  10293. /* IPLEN can support at most 127 dwords */
  10294. len = skb_inner_transport_header(skb) -
  10295. skb_inner_network_header(skb);
  10296. if (len & ~(127 * 4))
  10297. goto out_err;
  10298. }
  10299. /* No need to validate L4LEN as TCP is the only protocol with a
  10300. * a flexible value and we support all possible values supported
  10301. * by TCP, which is at most 15 dwords
  10302. */
  10303. return features;
  10304. out_err:
  10305. return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
  10306. }
  10307. /**
  10308. * i40e_xdp_setup - add/remove an XDP program
  10309. * @vsi: VSI to changed
  10310. * @prog: XDP program
  10311. **/
  10312. static int i40e_xdp_setup(struct i40e_vsi *vsi,
  10313. struct bpf_prog *prog)
  10314. {
  10315. int frame_size = vsi->netdev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
  10316. struct i40e_pf *pf = vsi->back;
  10317. struct bpf_prog *old_prog;
  10318. bool need_reset;
  10319. int i;
  10320. /* Don't allow frames that span over multiple buffers */
  10321. if (frame_size > vsi->rx_buf_len)
  10322. return -EINVAL;
  10323. if (!i40e_enabled_xdp_vsi(vsi) && !prog)
  10324. return 0;
  10325. /* When turning XDP on->off/off->on we reset and rebuild the rings. */
  10326. need_reset = (i40e_enabled_xdp_vsi(vsi) != !!prog);
  10327. if (need_reset)
  10328. i40e_prep_for_reset(pf, true);
  10329. old_prog = xchg(&vsi->xdp_prog, prog);
  10330. if (need_reset)
  10331. i40e_reset_and_rebuild(pf, true, true);
  10332. for (i = 0; i < vsi->num_queue_pairs; i++)
  10333. WRITE_ONCE(vsi->rx_rings[i]->xdp_prog, vsi->xdp_prog);
  10334. if (old_prog)
  10335. bpf_prog_put(old_prog);
  10336. return 0;
  10337. }
  10338. /**
  10339. * i40e_xdp - implements ndo_bpf for i40e
  10340. * @dev: netdevice
  10341. * @xdp: XDP command
  10342. **/
  10343. static int i40e_xdp(struct net_device *dev,
  10344. struct netdev_bpf *xdp)
  10345. {
  10346. struct i40e_netdev_priv *np = netdev_priv(dev);
  10347. struct i40e_vsi *vsi = np->vsi;
  10348. if (vsi->type != I40E_VSI_MAIN)
  10349. return -EINVAL;
  10350. switch (xdp->command) {
  10351. case XDP_SETUP_PROG:
  10352. return i40e_xdp_setup(vsi, xdp->prog);
  10353. case XDP_QUERY_PROG:
  10354. xdp->prog_attached = i40e_enabled_xdp_vsi(vsi);
  10355. xdp->prog_id = vsi->xdp_prog ? vsi->xdp_prog->aux->id : 0;
  10356. return 0;
  10357. default:
  10358. return -EINVAL;
  10359. }
  10360. }
  10361. static const struct net_device_ops i40e_netdev_ops = {
  10362. .ndo_open = i40e_open,
  10363. .ndo_stop = i40e_close,
  10364. .ndo_start_xmit = i40e_lan_xmit_frame,
  10365. .ndo_get_stats64 = i40e_get_netdev_stats_struct,
  10366. .ndo_set_rx_mode = i40e_set_rx_mode,
  10367. .ndo_validate_addr = eth_validate_addr,
  10368. .ndo_set_mac_address = i40e_set_mac,
  10369. .ndo_change_mtu = i40e_change_mtu,
  10370. .ndo_do_ioctl = i40e_ioctl,
  10371. .ndo_tx_timeout = i40e_tx_timeout,
  10372. .ndo_vlan_rx_add_vid = i40e_vlan_rx_add_vid,
  10373. .ndo_vlan_rx_kill_vid = i40e_vlan_rx_kill_vid,
  10374. #ifdef CONFIG_NET_POLL_CONTROLLER
  10375. .ndo_poll_controller = i40e_netpoll,
  10376. #endif
  10377. .ndo_setup_tc = __i40e_setup_tc,
  10378. .ndo_set_features = i40e_set_features,
  10379. .ndo_set_vf_mac = i40e_ndo_set_vf_mac,
  10380. .ndo_set_vf_vlan = i40e_ndo_set_vf_port_vlan,
  10381. .ndo_set_vf_rate = i40e_ndo_set_vf_bw,
  10382. .ndo_get_vf_config = i40e_ndo_get_vf_config,
  10383. .ndo_set_vf_link_state = i40e_ndo_set_vf_link_state,
  10384. .ndo_set_vf_spoofchk = i40e_ndo_set_vf_spoofchk,
  10385. .ndo_set_vf_trust = i40e_ndo_set_vf_trust,
  10386. .ndo_udp_tunnel_add = i40e_udp_tunnel_add,
  10387. .ndo_udp_tunnel_del = i40e_udp_tunnel_del,
  10388. .ndo_get_phys_port_id = i40e_get_phys_port_id,
  10389. .ndo_fdb_add = i40e_ndo_fdb_add,
  10390. .ndo_features_check = i40e_features_check,
  10391. .ndo_bridge_getlink = i40e_ndo_bridge_getlink,
  10392. .ndo_bridge_setlink = i40e_ndo_bridge_setlink,
  10393. .ndo_bpf = i40e_xdp,
  10394. .ndo_xdp_xmit = i40e_xdp_xmit,
  10395. };
  10396. /**
  10397. * i40e_config_netdev - Setup the netdev flags
  10398. * @vsi: the VSI being configured
  10399. *
  10400. * Returns 0 on success, negative value on failure
  10401. **/
  10402. static int i40e_config_netdev(struct i40e_vsi *vsi)
  10403. {
  10404. struct i40e_pf *pf = vsi->back;
  10405. struct i40e_hw *hw = &pf->hw;
  10406. struct i40e_netdev_priv *np;
  10407. struct net_device *netdev;
  10408. u8 broadcast[ETH_ALEN];
  10409. u8 mac_addr[ETH_ALEN];
  10410. int etherdev_size;
  10411. netdev_features_t hw_enc_features;
  10412. netdev_features_t hw_features;
  10413. etherdev_size = sizeof(struct i40e_netdev_priv);
  10414. netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
  10415. if (!netdev)
  10416. return -ENOMEM;
  10417. vsi->netdev = netdev;
  10418. np = netdev_priv(netdev);
  10419. np->vsi = vsi;
  10420. hw_enc_features = NETIF_F_SG |
  10421. NETIF_F_IP_CSUM |
  10422. NETIF_F_IPV6_CSUM |
  10423. NETIF_F_HIGHDMA |
  10424. NETIF_F_SOFT_FEATURES |
  10425. NETIF_F_TSO |
  10426. NETIF_F_TSO_ECN |
  10427. NETIF_F_TSO6 |
  10428. NETIF_F_GSO_GRE |
  10429. NETIF_F_GSO_GRE_CSUM |
  10430. NETIF_F_GSO_PARTIAL |
  10431. NETIF_F_GSO_UDP_TUNNEL |
  10432. NETIF_F_GSO_UDP_TUNNEL_CSUM |
  10433. NETIF_F_SCTP_CRC |
  10434. NETIF_F_RXHASH |
  10435. NETIF_F_RXCSUM |
  10436. 0;
  10437. if (!(pf->hw_features & I40E_HW_OUTER_UDP_CSUM_CAPABLE))
  10438. netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
  10439. netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
  10440. netdev->hw_enc_features |= hw_enc_features;
  10441. /* record features VLANs can make use of */
  10442. netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
  10443. if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
  10444. netdev->hw_features |= NETIF_F_NTUPLE | NETIF_F_HW_TC;
  10445. hw_features = hw_enc_features |
  10446. NETIF_F_HW_VLAN_CTAG_TX |
  10447. NETIF_F_HW_VLAN_CTAG_RX;
  10448. netdev->hw_features |= hw_features;
  10449. netdev->features |= hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
  10450. netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
  10451. if (vsi->type == I40E_VSI_MAIN) {
  10452. SET_NETDEV_DEV(netdev, &pf->pdev->dev);
  10453. ether_addr_copy(mac_addr, hw->mac.perm_addr);
  10454. /* The following steps are necessary for two reasons. First,
  10455. * some older NVM configurations load a default MAC-VLAN
  10456. * filter that will accept any tagged packet, and we want to
  10457. * replace this with a normal filter. Additionally, it is
  10458. * possible our MAC address was provided by the platform using
  10459. * Open Firmware or similar.
  10460. *
  10461. * Thus, we need to remove the default filter and install one
  10462. * specific to the MAC address.
  10463. */
  10464. i40e_rm_default_mac_filter(vsi, mac_addr);
  10465. spin_lock_bh(&vsi->mac_filter_hash_lock);
  10466. i40e_add_mac_filter(vsi, mac_addr);
  10467. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  10468. } else {
  10469. /* Relate the VSI_VMDQ name to the VSI_MAIN name. Note that we
  10470. * are still limited by IFNAMSIZ, but we're adding 'v%d\0' to
  10471. * the end, which is 4 bytes long, so force truncation of the
  10472. * original name by IFNAMSIZ - 4
  10473. */
  10474. snprintf(netdev->name, IFNAMSIZ, "%.*sv%%d",
  10475. IFNAMSIZ - 4,
  10476. pf->vsi[pf->lan_vsi]->netdev->name);
  10477. random_ether_addr(mac_addr);
  10478. spin_lock_bh(&vsi->mac_filter_hash_lock);
  10479. i40e_add_mac_filter(vsi, mac_addr);
  10480. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  10481. }
  10482. /* Add the broadcast filter so that we initially will receive
  10483. * broadcast packets. Note that when a new VLAN is first added the
  10484. * driver will convert all filters marked I40E_VLAN_ANY into VLAN
  10485. * specific filters as part of transitioning into "vlan" operation.
  10486. * When more VLANs are added, the driver will copy each existing MAC
  10487. * filter and add it for the new VLAN.
  10488. *
  10489. * Broadcast filters are handled specially by
  10490. * i40e_sync_filters_subtask, as the driver must to set the broadcast
  10491. * promiscuous bit instead of adding this directly as a MAC/VLAN
  10492. * filter. The subtask will update the correct broadcast promiscuous
  10493. * bits as VLANs become active or inactive.
  10494. */
  10495. eth_broadcast_addr(broadcast);
  10496. spin_lock_bh(&vsi->mac_filter_hash_lock);
  10497. i40e_add_mac_filter(vsi, broadcast);
  10498. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  10499. ether_addr_copy(netdev->dev_addr, mac_addr);
  10500. ether_addr_copy(netdev->perm_addr, mac_addr);
  10501. netdev->priv_flags |= IFF_UNICAST_FLT;
  10502. netdev->priv_flags |= IFF_SUPP_NOFCS;
  10503. /* Setup netdev TC information */
  10504. i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
  10505. netdev->netdev_ops = &i40e_netdev_ops;
  10506. netdev->watchdog_timeo = 5 * HZ;
  10507. i40e_set_ethtool_ops(netdev);
  10508. /* MTU range: 68 - 9706 */
  10509. netdev->min_mtu = ETH_MIN_MTU;
  10510. netdev->max_mtu = I40E_MAX_RXBUFFER - I40E_PACKET_HDR_PAD;
  10511. return 0;
  10512. }
  10513. /**
  10514. * i40e_vsi_delete - Delete a VSI from the switch
  10515. * @vsi: the VSI being removed
  10516. *
  10517. * Returns 0 on success, negative value on failure
  10518. **/
  10519. static void i40e_vsi_delete(struct i40e_vsi *vsi)
  10520. {
  10521. /* remove default VSI is not allowed */
  10522. if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
  10523. return;
  10524. i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
  10525. }
  10526. /**
  10527. * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
  10528. * @vsi: the VSI being queried
  10529. *
  10530. * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
  10531. **/
  10532. int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
  10533. {
  10534. struct i40e_veb *veb;
  10535. struct i40e_pf *pf = vsi->back;
  10536. /* Uplink is not a bridge so default to VEB */
  10537. if (vsi->veb_idx == I40E_NO_VEB)
  10538. return 1;
  10539. veb = pf->veb[vsi->veb_idx];
  10540. if (!veb) {
  10541. dev_info(&pf->pdev->dev,
  10542. "There is no veb associated with the bridge\n");
  10543. return -ENOENT;
  10544. }
  10545. /* Uplink is a bridge in VEPA mode */
  10546. if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
  10547. return 0;
  10548. } else {
  10549. /* Uplink is a bridge in VEB mode */
  10550. return 1;
  10551. }
  10552. /* VEPA is now default bridge, so return 0 */
  10553. return 0;
  10554. }
  10555. /**
  10556. * i40e_add_vsi - Add a VSI to the switch
  10557. * @vsi: the VSI being configured
  10558. *
  10559. * This initializes a VSI context depending on the VSI type to be added and
  10560. * passes it down to the add_vsi aq command.
  10561. **/
  10562. static int i40e_add_vsi(struct i40e_vsi *vsi)
  10563. {
  10564. int ret = -ENODEV;
  10565. struct i40e_pf *pf = vsi->back;
  10566. struct i40e_hw *hw = &pf->hw;
  10567. struct i40e_vsi_context ctxt;
  10568. struct i40e_mac_filter *f;
  10569. struct hlist_node *h;
  10570. int bkt;
  10571. u8 enabled_tc = 0x1; /* TC0 enabled */
  10572. int f_count = 0;
  10573. memset(&ctxt, 0, sizeof(ctxt));
  10574. switch (vsi->type) {
  10575. case I40E_VSI_MAIN:
  10576. /* The PF's main VSI is already setup as part of the
  10577. * device initialization, so we'll not bother with
  10578. * the add_vsi call, but we will retrieve the current
  10579. * VSI context.
  10580. */
  10581. ctxt.seid = pf->main_vsi_seid;
  10582. ctxt.pf_num = pf->hw.pf_id;
  10583. ctxt.vf_num = 0;
  10584. ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
  10585. ctxt.flags = I40E_AQ_VSI_TYPE_PF;
  10586. if (ret) {
  10587. dev_info(&pf->pdev->dev,
  10588. "couldn't get PF vsi config, err %s aq_err %s\n",
  10589. i40e_stat_str(&pf->hw, ret),
  10590. i40e_aq_str(&pf->hw,
  10591. pf->hw.aq.asq_last_status));
  10592. return -ENOENT;
  10593. }
  10594. vsi->info = ctxt.info;
  10595. vsi->info.valid_sections = 0;
  10596. vsi->seid = ctxt.seid;
  10597. vsi->id = ctxt.vsi_number;
  10598. enabled_tc = i40e_pf_get_tc_map(pf);
  10599. /* Source pruning is enabled by default, so the flag is
  10600. * negative logic - if it's set, we need to fiddle with
  10601. * the VSI to disable source pruning.
  10602. */
  10603. if (pf->flags & I40E_FLAG_SOURCE_PRUNING_DISABLED) {
  10604. memset(&ctxt, 0, sizeof(ctxt));
  10605. ctxt.seid = pf->main_vsi_seid;
  10606. ctxt.pf_num = pf->hw.pf_id;
  10607. ctxt.vf_num = 0;
  10608. ctxt.info.valid_sections |=
  10609. cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  10610. ctxt.info.switch_id =
  10611. cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_LOCAL_LB);
  10612. ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
  10613. if (ret) {
  10614. dev_info(&pf->pdev->dev,
  10615. "update vsi failed, err %s aq_err %s\n",
  10616. i40e_stat_str(&pf->hw, ret),
  10617. i40e_aq_str(&pf->hw,
  10618. pf->hw.aq.asq_last_status));
  10619. ret = -ENOENT;
  10620. goto err;
  10621. }
  10622. }
  10623. /* MFP mode setup queue map and update VSI */
  10624. if ((pf->flags & I40E_FLAG_MFP_ENABLED) &&
  10625. !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
  10626. memset(&ctxt, 0, sizeof(ctxt));
  10627. ctxt.seid = pf->main_vsi_seid;
  10628. ctxt.pf_num = pf->hw.pf_id;
  10629. ctxt.vf_num = 0;
  10630. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
  10631. ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
  10632. if (ret) {
  10633. dev_info(&pf->pdev->dev,
  10634. "update vsi failed, err %s aq_err %s\n",
  10635. i40e_stat_str(&pf->hw, ret),
  10636. i40e_aq_str(&pf->hw,
  10637. pf->hw.aq.asq_last_status));
  10638. ret = -ENOENT;
  10639. goto err;
  10640. }
  10641. /* update the local VSI info queue map */
  10642. i40e_vsi_update_queue_map(vsi, &ctxt);
  10643. vsi->info.valid_sections = 0;
  10644. } else {
  10645. /* Default/Main VSI is only enabled for TC0
  10646. * reconfigure it to enable all TCs that are
  10647. * available on the port in SFP mode.
  10648. * For MFP case the iSCSI PF would use this
  10649. * flow to enable LAN+iSCSI TC.
  10650. */
  10651. ret = i40e_vsi_config_tc(vsi, enabled_tc);
  10652. if (ret) {
  10653. /* Single TC condition is not fatal,
  10654. * message and continue
  10655. */
  10656. dev_info(&pf->pdev->dev,
  10657. "failed to configure TCs for main VSI tc_map 0x%08x, err %s aq_err %s\n",
  10658. enabled_tc,
  10659. i40e_stat_str(&pf->hw, ret),
  10660. i40e_aq_str(&pf->hw,
  10661. pf->hw.aq.asq_last_status));
  10662. }
  10663. }
  10664. break;
  10665. case I40E_VSI_FDIR:
  10666. ctxt.pf_num = hw->pf_id;
  10667. ctxt.vf_num = 0;
  10668. ctxt.uplink_seid = vsi->uplink_seid;
  10669. ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
  10670. ctxt.flags = I40E_AQ_VSI_TYPE_PF;
  10671. if ((pf->flags & I40E_FLAG_VEB_MODE_ENABLED) &&
  10672. (i40e_is_vsi_uplink_mode_veb(vsi))) {
  10673. ctxt.info.valid_sections |=
  10674. cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  10675. ctxt.info.switch_id =
  10676. cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  10677. }
  10678. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
  10679. break;
  10680. case I40E_VSI_VMDQ2:
  10681. ctxt.pf_num = hw->pf_id;
  10682. ctxt.vf_num = 0;
  10683. ctxt.uplink_seid = vsi->uplink_seid;
  10684. ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
  10685. ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
  10686. /* This VSI is connected to VEB so the switch_id
  10687. * should be set to zero by default.
  10688. */
  10689. if (i40e_is_vsi_uplink_mode_veb(vsi)) {
  10690. ctxt.info.valid_sections |=
  10691. cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  10692. ctxt.info.switch_id =
  10693. cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  10694. }
  10695. /* Setup the VSI tx/rx queue map for TC0 only for now */
  10696. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
  10697. break;
  10698. case I40E_VSI_SRIOV:
  10699. ctxt.pf_num = hw->pf_id;
  10700. ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
  10701. ctxt.uplink_seid = vsi->uplink_seid;
  10702. ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
  10703. ctxt.flags = I40E_AQ_VSI_TYPE_VF;
  10704. /* This VSI is connected to VEB so the switch_id
  10705. * should be set to zero by default.
  10706. */
  10707. if (i40e_is_vsi_uplink_mode_veb(vsi)) {
  10708. ctxt.info.valid_sections |=
  10709. cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  10710. ctxt.info.switch_id =
  10711. cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  10712. }
  10713. if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
  10714. ctxt.info.valid_sections |=
  10715. cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
  10716. ctxt.info.queueing_opt_flags |=
  10717. (I40E_AQ_VSI_QUE_OPT_TCP_ENA |
  10718. I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI);
  10719. }
  10720. ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
  10721. ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
  10722. if (pf->vf[vsi->vf_id].spoofchk) {
  10723. ctxt.info.valid_sections |=
  10724. cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
  10725. ctxt.info.sec_flags |=
  10726. (I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
  10727. I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
  10728. }
  10729. /* Setup the VSI tx/rx queue map for TC0 only for now */
  10730. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
  10731. break;
  10732. case I40E_VSI_IWARP:
  10733. /* send down message to iWARP */
  10734. break;
  10735. default:
  10736. return -ENODEV;
  10737. }
  10738. if (vsi->type != I40E_VSI_MAIN) {
  10739. ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
  10740. if (ret) {
  10741. dev_info(&vsi->back->pdev->dev,
  10742. "add vsi failed, err %s aq_err %s\n",
  10743. i40e_stat_str(&pf->hw, ret),
  10744. i40e_aq_str(&pf->hw,
  10745. pf->hw.aq.asq_last_status));
  10746. ret = -ENOENT;
  10747. goto err;
  10748. }
  10749. vsi->info = ctxt.info;
  10750. vsi->info.valid_sections = 0;
  10751. vsi->seid = ctxt.seid;
  10752. vsi->id = ctxt.vsi_number;
  10753. }
  10754. vsi->active_filters = 0;
  10755. clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
  10756. spin_lock_bh(&vsi->mac_filter_hash_lock);
  10757. /* If macvlan filters already exist, force them to get loaded */
  10758. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  10759. f->state = I40E_FILTER_NEW;
  10760. f_count++;
  10761. }
  10762. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  10763. if (f_count) {
  10764. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  10765. set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
  10766. }
  10767. /* Update VSI BW information */
  10768. ret = i40e_vsi_get_bw_info(vsi);
  10769. if (ret) {
  10770. dev_info(&pf->pdev->dev,
  10771. "couldn't get vsi bw info, err %s aq_err %s\n",
  10772. i40e_stat_str(&pf->hw, ret),
  10773. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  10774. /* VSI is already added so not tearing that up */
  10775. ret = 0;
  10776. }
  10777. err:
  10778. return ret;
  10779. }
  10780. /**
  10781. * i40e_vsi_release - Delete a VSI and free its resources
  10782. * @vsi: the VSI being removed
  10783. *
  10784. * Returns 0 on success or < 0 on error
  10785. **/
  10786. int i40e_vsi_release(struct i40e_vsi *vsi)
  10787. {
  10788. struct i40e_mac_filter *f;
  10789. struct hlist_node *h;
  10790. struct i40e_veb *veb = NULL;
  10791. struct i40e_pf *pf;
  10792. u16 uplink_seid;
  10793. int i, n, bkt;
  10794. pf = vsi->back;
  10795. /* release of a VEB-owner or last VSI is not allowed */
  10796. if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
  10797. dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
  10798. vsi->seid, vsi->uplink_seid);
  10799. return -ENODEV;
  10800. }
  10801. if (vsi == pf->vsi[pf->lan_vsi] &&
  10802. !test_bit(__I40E_DOWN, pf->state)) {
  10803. dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
  10804. return -ENODEV;
  10805. }
  10806. uplink_seid = vsi->uplink_seid;
  10807. if (vsi->type != I40E_VSI_SRIOV) {
  10808. if (vsi->netdev_registered) {
  10809. vsi->netdev_registered = false;
  10810. if (vsi->netdev) {
  10811. /* results in a call to i40e_close() */
  10812. unregister_netdev(vsi->netdev);
  10813. }
  10814. } else {
  10815. i40e_vsi_close(vsi);
  10816. }
  10817. i40e_vsi_disable_irq(vsi);
  10818. }
  10819. spin_lock_bh(&vsi->mac_filter_hash_lock);
  10820. /* clear the sync flag on all filters */
  10821. if (vsi->netdev) {
  10822. __dev_uc_unsync(vsi->netdev, NULL);
  10823. __dev_mc_unsync(vsi->netdev, NULL);
  10824. }
  10825. /* make sure any remaining filters are marked for deletion */
  10826. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
  10827. __i40e_del_filter(vsi, f);
  10828. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  10829. i40e_sync_vsi_filters(vsi);
  10830. i40e_vsi_delete(vsi);
  10831. i40e_vsi_free_q_vectors(vsi);
  10832. if (vsi->netdev) {
  10833. free_netdev(vsi->netdev);
  10834. vsi->netdev = NULL;
  10835. }
  10836. i40e_vsi_clear_rings(vsi);
  10837. i40e_vsi_clear(vsi);
  10838. /* If this was the last thing on the VEB, except for the
  10839. * controlling VSI, remove the VEB, which puts the controlling
  10840. * VSI onto the next level down in the switch.
  10841. *
  10842. * Well, okay, there's one more exception here: don't remove
  10843. * the orphan VEBs yet. We'll wait for an explicit remove request
  10844. * from up the network stack.
  10845. */
  10846. for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
  10847. if (pf->vsi[i] &&
  10848. pf->vsi[i]->uplink_seid == uplink_seid &&
  10849. (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
  10850. n++; /* count the VSIs */
  10851. }
  10852. }
  10853. for (i = 0; i < I40E_MAX_VEB; i++) {
  10854. if (!pf->veb[i])
  10855. continue;
  10856. if (pf->veb[i]->uplink_seid == uplink_seid)
  10857. n++; /* count the VEBs */
  10858. if (pf->veb[i]->seid == uplink_seid)
  10859. veb = pf->veb[i];
  10860. }
  10861. if (n == 0 && veb && veb->uplink_seid != 0)
  10862. i40e_veb_release(veb);
  10863. return 0;
  10864. }
  10865. /**
  10866. * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
  10867. * @vsi: ptr to the VSI
  10868. *
  10869. * This should only be called after i40e_vsi_mem_alloc() which allocates the
  10870. * corresponding SW VSI structure and initializes num_queue_pairs for the
  10871. * newly allocated VSI.
  10872. *
  10873. * Returns 0 on success or negative on failure
  10874. **/
  10875. static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
  10876. {
  10877. int ret = -ENOENT;
  10878. struct i40e_pf *pf = vsi->back;
  10879. if (vsi->q_vectors[0]) {
  10880. dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
  10881. vsi->seid);
  10882. return -EEXIST;
  10883. }
  10884. if (vsi->base_vector) {
  10885. dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
  10886. vsi->seid, vsi->base_vector);
  10887. return -EEXIST;
  10888. }
  10889. ret = i40e_vsi_alloc_q_vectors(vsi);
  10890. if (ret) {
  10891. dev_info(&pf->pdev->dev,
  10892. "failed to allocate %d q_vector for VSI %d, ret=%d\n",
  10893. vsi->num_q_vectors, vsi->seid, ret);
  10894. vsi->num_q_vectors = 0;
  10895. goto vector_setup_out;
  10896. }
  10897. /* In Legacy mode, we do not have to get any other vector since we
  10898. * piggyback on the misc/ICR0 for queue interrupts.
  10899. */
  10900. if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
  10901. return ret;
  10902. if (vsi->num_q_vectors)
  10903. vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
  10904. vsi->num_q_vectors, vsi->idx);
  10905. if (vsi->base_vector < 0) {
  10906. dev_info(&pf->pdev->dev,
  10907. "failed to get tracking for %d vectors for VSI %d, err=%d\n",
  10908. vsi->num_q_vectors, vsi->seid, vsi->base_vector);
  10909. i40e_vsi_free_q_vectors(vsi);
  10910. ret = -ENOENT;
  10911. goto vector_setup_out;
  10912. }
  10913. vector_setup_out:
  10914. return ret;
  10915. }
  10916. /**
  10917. * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
  10918. * @vsi: pointer to the vsi.
  10919. *
  10920. * This re-allocates a vsi's queue resources.
  10921. *
  10922. * Returns pointer to the successfully allocated and configured VSI sw struct
  10923. * on success, otherwise returns NULL on failure.
  10924. **/
  10925. static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
  10926. {
  10927. u16 alloc_queue_pairs;
  10928. struct i40e_pf *pf;
  10929. u8 enabled_tc;
  10930. int ret;
  10931. if (!vsi)
  10932. return NULL;
  10933. pf = vsi->back;
  10934. i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
  10935. i40e_vsi_clear_rings(vsi);
  10936. i40e_vsi_free_arrays(vsi, false);
  10937. i40e_set_num_rings_in_vsi(vsi);
  10938. ret = i40e_vsi_alloc_arrays(vsi, false);
  10939. if (ret)
  10940. goto err_vsi;
  10941. alloc_queue_pairs = vsi->alloc_queue_pairs *
  10942. (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
  10943. ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
  10944. if (ret < 0) {
  10945. dev_info(&pf->pdev->dev,
  10946. "failed to get tracking for %d queues for VSI %d err %d\n",
  10947. alloc_queue_pairs, vsi->seid, ret);
  10948. goto err_vsi;
  10949. }
  10950. vsi->base_queue = ret;
  10951. /* Update the FW view of the VSI. Force a reset of TC and queue
  10952. * layout configurations.
  10953. */
  10954. enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
  10955. pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
  10956. pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
  10957. i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
  10958. if (vsi->type == I40E_VSI_MAIN)
  10959. i40e_rm_default_mac_filter(vsi, pf->hw.mac.perm_addr);
  10960. /* assign it some queues */
  10961. ret = i40e_alloc_rings(vsi);
  10962. if (ret)
  10963. goto err_rings;
  10964. /* map all of the rings to the q_vectors */
  10965. i40e_vsi_map_rings_to_vectors(vsi);
  10966. return vsi;
  10967. err_rings:
  10968. i40e_vsi_free_q_vectors(vsi);
  10969. if (vsi->netdev_registered) {
  10970. vsi->netdev_registered = false;
  10971. unregister_netdev(vsi->netdev);
  10972. free_netdev(vsi->netdev);
  10973. vsi->netdev = NULL;
  10974. }
  10975. i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
  10976. err_vsi:
  10977. i40e_vsi_clear(vsi);
  10978. return NULL;
  10979. }
  10980. /**
  10981. * i40e_vsi_setup - Set up a VSI by a given type
  10982. * @pf: board private structure
  10983. * @type: VSI type
  10984. * @uplink_seid: the switch element to link to
  10985. * @param1: usage depends upon VSI type. For VF types, indicates VF id
  10986. *
  10987. * This allocates the sw VSI structure and its queue resources, then add a VSI
  10988. * to the identified VEB.
  10989. *
  10990. * Returns pointer to the successfully allocated and configure VSI sw struct on
  10991. * success, otherwise returns NULL on failure.
  10992. **/
  10993. struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
  10994. u16 uplink_seid, u32 param1)
  10995. {
  10996. struct i40e_vsi *vsi = NULL;
  10997. struct i40e_veb *veb = NULL;
  10998. u16 alloc_queue_pairs;
  10999. int ret, i;
  11000. int v_idx;
  11001. /* The requested uplink_seid must be either
  11002. * - the PF's port seid
  11003. * no VEB is needed because this is the PF
  11004. * or this is a Flow Director special case VSI
  11005. * - seid of an existing VEB
  11006. * - seid of a VSI that owns an existing VEB
  11007. * - seid of a VSI that doesn't own a VEB
  11008. * a new VEB is created and the VSI becomes the owner
  11009. * - seid of the PF VSI, which is what creates the first VEB
  11010. * this is a special case of the previous
  11011. *
  11012. * Find which uplink_seid we were given and create a new VEB if needed
  11013. */
  11014. for (i = 0; i < I40E_MAX_VEB; i++) {
  11015. if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
  11016. veb = pf->veb[i];
  11017. break;
  11018. }
  11019. }
  11020. if (!veb && uplink_seid != pf->mac_seid) {
  11021. for (i = 0; i < pf->num_alloc_vsi; i++) {
  11022. if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
  11023. vsi = pf->vsi[i];
  11024. break;
  11025. }
  11026. }
  11027. if (!vsi) {
  11028. dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
  11029. uplink_seid);
  11030. return NULL;
  11031. }
  11032. if (vsi->uplink_seid == pf->mac_seid)
  11033. veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
  11034. vsi->tc_config.enabled_tc);
  11035. else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
  11036. veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
  11037. vsi->tc_config.enabled_tc);
  11038. if (veb) {
  11039. if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
  11040. dev_info(&vsi->back->pdev->dev,
  11041. "New VSI creation error, uplink seid of LAN VSI expected.\n");
  11042. return NULL;
  11043. }
  11044. /* We come up by default in VEPA mode if SRIOV is not
  11045. * already enabled, in which case we can't force VEPA
  11046. * mode.
  11047. */
  11048. if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
  11049. veb->bridge_mode = BRIDGE_MODE_VEPA;
  11050. pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
  11051. }
  11052. i40e_config_bridge_mode(veb);
  11053. }
  11054. for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
  11055. if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
  11056. veb = pf->veb[i];
  11057. }
  11058. if (!veb) {
  11059. dev_info(&pf->pdev->dev, "couldn't add VEB\n");
  11060. return NULL;
  11061. }
  11062. vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
  11063. uplink_seid = veb->seid;
  11064. }
  11065. /* get vsi sw struct */
  11066. v_idx = i40e_vsi_mem_alloc(pf, type);
  11067. if (v_idx < 0)
  11068. goto err_alloc;
  11069. vsi = pf->vsi[v_idx];
  11070. if (!vsi)
  11071. goto err_alloc;
  11072. vsi->type = type;
  11073. vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
  11074. if (type == I40E_VSI_MAIN)
  11075. pf->lan_vsi = v_idx;
  11076. else if (type == I40E_VSI_SRIOV)
  11077. vsi->vf_id = param1;
  11078. /* assign it some queues */
  11079. alloc_queue_pairs = vsi->alloc_queue_pairs *
  11080. (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
  11081. ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
  11082. if (ret < 0) {
  11083. dev_info(&pf->pdev->dev,
  11084. "failed to get tracking for %d queues for VSI %d err=%d\n",
  11085. alloc_queue_pairs, vsi->seid, ret);
  11086. goto err_vsi;
  11087. }
  11088. vsi->base_queue = ret;
  11089. /* get a VSI from the hardware */
  11090. vsi->uplink_seid = uplink_seid;
  11091. ret = i40e_add_vsi(vsi);
  11092. if (ret)
  11093. goto err_vsi;
  11094. switch (vsi->type) {
  11095. /* setup the netdev if needed */
  11096. case I40E_VSI_MAIN:
  11097. case I40E_VSI_VMDQ2:
  11098. ret = i40e_config_netdev(vsi);
  11099. if (ret)
  11100. goto err_netdev;
  11101. ret = register_netdev(vsi->netdev);
  11102. if (ret)
  11103. goto err_netdev;
  11104. vsi->netdev_registered = true;
  11105. netif_carrier_off(vsi->netdev);
  11106. #ifdef CONFIG_I40E_DCB
  11107. /* Setup DCB netlink interface */
  11108. i40e_dcbnl_setup(vsi);
  11109. #endif /* CONFIG_I40E_DCB */
  11110. /* fall through */
  11111. case I40E_VSI_FDIR:
  11112. /* set up vectors and rings if needed */
  11113. ret = i40e_vsi_setup_vectors(vsi);
  11114. if (ret)
  11115. goto err_msix;
  11116. ret = i40e_alloc_rings(vsi);
  11117. if (ret)
  11118. goto err_rings;
  11119. /* map all of the rings to the q_vectors */
  11120. i40e_vsi_map_rings_to_vectors(vsi);
  11121. i40e_vsi_reset_stats(vsi);
  11122. break;
  11123. default:
  11124. /* no netdev or rings for the other VSI types */
  11125. break;
  11126. }
  11127. if ((pf->hw_features & I40E_HW_RSS_AQ_CAPABLE) &&
  11128. (vsi->type == I40E_VSI_VMDQ2)) {
  11129. ret = i40e_vsi_config_rss(vsi);
  11130. }
  11131. return vsi;
  11132. err_rings:
  11133. i40e_vsi_free_q_vectors(vsi);
  11134. err_msix:
  11135. if (vsi->netdev_registered) {
  11136. vsi->netdev_registered = false;
  11137. unregister_netdev(vsi->netdev);
  11138. free_netdev(vsi->netdev);
  11139. vsi->netdev = NULL;
  11140. }
  11141. err_netdev:
  11142. i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
  11143. err_vsi:
  11144. i40e_vsi_clear(vsi);
  11145. err_alloc:
  11146. return NULL;
  11147. }
  11148. /**
  11149. * i40e_veb_get_bw_info - Query VEB BW information
  11150. * @veb: the veb to query
  11151. *
  11152. * Query the Tx scheduler BW configuration data for given VEB
  11153. **/
  11154. static int i40e_veb_get_bw_info(struct i40e_veb *veb)
  11155. {
  11156. struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
  11157. struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
  11158. struct i40e_pf *pf = veb->pf;
  11159. struct i40e_hw *hw = &pf->hw;
  11160. u32 tc_bw_max;
  11161. int ret = 0;
  11162. int i;
  11163. ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
  11164. &bw_data, NULL);
  11165. if (ret) {
  11166. dev_info(&pf->pdev->dev,
  11167. "query veb bw config failed, err %s aq_err %s\n",
  11168. i40e_stat_str(&pf->hw, ret),
  11169. i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
  11170. goto out;
  11171. }
  11172. ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
  11173. &ets_data, NULL);
  11174. if (ret) {
  11175. dev_info(&pf->pdev->dev,
  11176. "query veb bw ets config failed, err %s aq_err %s\n",
  11177. i40e_stat_str(&pf->hw, ret),
  11178. i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
  11179. goto out;
  11180. }
  11181. veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
  11182. veb->bw_max_quanta = ets_data.tc_bw_max;
  11183. veb->is_abs_credits = bw_data.absolute_credits_enable;
  11184. veb->enabled_tc = ets_data.tc_valid_bits;
  11185. tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
  11186. (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
  11187. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  11188. veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
  11189. veb->bw_tc_limit_credits[i] =
  11190. le16_to_cpu(bw_data.tc_bw_limits[i]);
  11191. veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
  11192. }
  11193. out:
  11194. return ret;
  11195. }
  11196. /**
  11197. * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
  11198. * @pf: board private structure
  11199. *
  11200. * On error: returns error code (negative)
  11201. * On success: returns vsi index in PF (positive)
  11202. **/
  11203. static int i40e_veb_mem_alloc(struct i40e_pf *pf)
  11204. {
  11205. int ret = -ENOENT;
  11206. struct i40e_veb *veb;
  11207. int i;
  11208. /* Need to protect the allocation of switch elements at the PF level */
  11209. mutex_lock(&pf->switch_mutex);
  11210. /* VEB list may be fragmented if VEB creation/destruction has
  11211. * been happening. We can afford to do a quick scan to look
  11212. * for any free slots in the list.
  11213. *
  11214. * find next empty veb slot, looping back around if necessary
  11215. */
  11216. i = 0;
  11217. while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
  11218. i++;
  11219. if (i >= I40E_MAX_VEB) {
  11220. ret = -ENOMEM;
  11221. goto err_alloc_veb; /* out of VEB slots! */
  11222. }
  11223. veb = kzalloc(sizeof(*veb), GFP_KERNEL);
  11224. if (!veb) {
  11225. ret = -ENOMEM;
  11226. goto err_alloc_veb;
  11227. }
  11228. veb->pf = pf;
  11229. veb->idx = i;
  11230. veb->enabled_tc = 1;
  11231. pf->veb[i] = veb;
  11232. ret = i;
  11233. err_alloc_veb:
  11234. mutex_unlock(&pf->switch_mutex);
  11235. return ret;
  11236. }
  11237. /**
  11238. * i40e_switch_branch_release - Delete a branch of the switch tree
  11239. * @branch: where to start deleting
  11240. *
  11241. * This uses recursion to find the tips of the branch to be
  11242. * removed, deleting until we get back to and can delete this VEB.
  11243. **/
  11244. static void i40e_switch_branch_release(struct i40e_veb *branch)
  11245. {
  11246. struct i40e_pf *pf = branch->pf;
  11247. u16 branch_seid = branch->seid;
  11248. u16 veb_idx = branch->idx;
  11249. int i;
  11250. /* release any VEBs on this VEB - RECURSION */
  11251. for (i = 0; i < I40E_MAX_VEB; i++) {
  11252. if (!pf->veb[i])
  11253. continue;
  11254. if (pf->veb[i]->uplink_seid == branch->seid)
  11255. i40e_switch_branch_release(pf->veb[i]);
  11256. }
  11257. /* Release the VSIs on this VEB, but not the owner VSI.
  11258. *
  11259. * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
  11260. * the VEB itself, so don't use (*branch) after this loop.
  11261. */
  11262. for (i = 0; i < pf->num_alloc_vsi; i++) {
  11263. if (!pf->vsi[i])
  11264. continue;
  11265. if (pf->vsi[i]->uplink_seid == branch_seid &&
  11266. (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
  11267. i40e_vsi_release(pf->vsi[i]);
  11268. }
  11269. }
  11270. /* There's one corner case where the VEB might not have been
  11271. * removed, so double check it here and remove it if needed.
  11272. * This case happens if the veb was created from the debugfs
  11273. * commands and no VSIs were added to it.
  11274. */
  11275. if (pf->veb[veb_idx])
  11276. i40e_veb_release(pf->veb[veb_idx]);
  11277. }
  11278. /**
  11279. * i40e_veb_clear - remove veb struct
  11280. * @veb: the veb to remove
  11281. **/
  11282. static void i40e_veb_clear(struct i40e_veb *veb)
  11283. {
  11284. if (!veb)
  11285. return;
  11286. if (veb->pf) {
  11287. struct i40e_pf *pf = veb->pf;
  11288. mutex_lock(&pf->switch_mutex);
  11289. if (pf->veb[veb->idx] == veb)
  11290. pf->veb[veb->idx] = NULL;
  11291. mutex_unlock(&pf->switch_mutex);
  11292. }
  11293. kfree(veb);
  11294. }
  11295. /**
  11296. * i40e_veb_release - Delete a VEB and free its resources
  11297. * @veb: the VEB being removed
  11298. **/
  11299. void i40e_veb_release(struct i40e_veb *veb)
  11300. {
  11301. struct i40e_vsi *vsi = NULL;
  11302. struct i40e_pf *pf;
  11303. int i, n = 0;
  11304. pf = veb->pf;
  11305. /* find the remaining VSI and check for extras */
  11306. for (i = 0; i < pf->num_alloc_vsi; i++) {
  11307. if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
  11308. n++;
  11309. vsi = pf->vsi[i];
  11310. }
  11311. }
  11312. if (n != 1) {
  11313. dev_info(&pf->pdev->dev,
  11314. "can't remove VEB %d with %d VSIs left\n",
  11315. veb->seid, n);
  11316. return;
  11317. }
  11318. /* move the remaining VSI to uplink veb */
  11319. vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
  11320. if (veb->uplink_seid) {
  11321. vsi->uplink_seid = veb->uplink_seid;
  11322. if (veb->uplink_seid == pf->mac_seid)
  11323. vsi->veb_idx = I40E_NO_VEB;
  11324. else
  11325. vsi->veb_idx = veb->veb_idx;
  11326. } else {
  11327. /* floating VEB */
  11328. vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
  11329. vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
  11330. }
  11331. i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
  11332. i40e_veb_clear(veb);
  11333. }
  11334. /**
  11335. * i40e_add_veb - create the VEB in the switch
  11336. * @veb: the VEB to be instantiated
  11337. * @vsi: the controlling VSI
  11338. **/
  11339. static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
  11340. {
  11341. struct i40e_pf *pf = veb->pf;
  11342. bool enable_stats = !!(pf->flags & I40E_FLAG_VEB_STATS_ENABLED);
  11343. int ret;
  11344. ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi->seid,
  11345. veb->enabled_tc, false,
  11346. &veb->seid, enable_stats, NULL);
  11347. /* get a VEB from the hardware */
  11348. if (ret) {
  11349. dev_info(&pf->pdev->dev,
  11350. "couldn't add VEB, err %s aq_err %s\n",
  11351. i40e_stat_str(&pf->hw, ret),
  11352. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  11353. return -EPERM;
  11354. }
  11355. /* get statistics counter */
  11356. ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
  11357. &veb->stats_idx, NULL, NULL, NULL);
  11358. if (ret) {
  11359. dev_info(&pf->pdev->dev,
  11360. "couldn't get VEB statistics idx, err %s aq_err %s\n",
  11361. i40e_stat_str(&pf->hw, ret),
  11362. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  11363. return -EPERM;
  11364. }
  11365. ret = i40e_veb_get_bw_info(veb);
  11366. if (ret) {
  11367. dev_info(&pf->pdev->dev,
  11368. "couldn't get VEB bw info, err %s aq_err %s\n",
  11369. i40e_stat_str(&pf->hw, ret),
  11370. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  11371. i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
  11372. return -ENOENT;
  11373. }
  11374. vsi->uplink_seid = veb->seid;
  11375. vsi->veb_idx = veb->idx;
  11376. vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
  11377. return 0;
  11378. }
  11379. /**
  11380. * i40e_veb_setup - Set up a VEB
  11381. * @pf: board private structure
  11382. * @flags: VEB setup flags
  11383. * @uplink_seid: the switch element to link to
  11384. * @vsi_seid: the initial VSI seid
  11385. * @enabled_tc: Enabled TC bit-map
  11386. *
  11387. * This allocates the sw VEB structure and links it into the switch
  11388. * It is possible and legal for this to be a duplicate of an already
  11389. * existing VEB. It is also possible for both uplink and vsi seids
  11390. * to be zero, in order to create a floating VEB.
  11391. *
  11392. * Returns pointer to the successfully allocated VEB sw struct on
  11393. * success, otherwise returns NULL on failure.
  11394. **/
  11395. struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
  11396. u16 uplink_seid, u16 vsi_seid,
  11397. u8 enabled_tc)
  11398. {
  11399. struct i40e_veb *veb, *uplink_veb = NULL;
  11400. int vsi_idx, veb_idx;
  11401. int ret;
  11402. /* if one seid is 0, the other must be 0 to create a floating relay */
  11403. if ((uplink_seid == 0 || vsi_seid == 0) &&
  11404. (uplink_seid + vsi_seid != 0)) {
  11405. dev_info(&pf->pdev->dev,
  11406. "one, not both seid's are 0: uplink=%d vsi=%d\n",
  11407. uplink_seid, vsi_seid);
  11408. return NULL;
  11409. }
  11410. /* make sure there is such a vsi and uplink */
  11411. for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
  11412. if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
  11413. break;
  11414. if (vsi_idx >= pf->num_alloc_vsi && vsi_seid != 0) {
  11415. dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
  11416. vsi_seid);
  11417. return NULL;
  11418. }
  11419. if (uplink_seid && uplink_seid != pf->mac_seid) {
  11420. for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
  11421. if (pf->veb[veb_idx] &&
  11422. pf->veb[veb_idx]->seid == uplink_seid) {
  11423. uplink_veb = pf->veb[veb_idx];
  11424. break;
  11425. }
  11426. }
  11427. if (!uplink_veb) {
  11428. dev_info(&pf->pdev->dev,
  11429. "uplink seid %d not found\n", uplink_seid);
  11430. return NULL;
  11431. }
  11432. }
  11433. /* get veb sw struct */
  11434. veb_idx = i40e_veb_mem_alloc(pf);
  11435. if (veb_idx < 0)
  11436. goto err_alloc;
  11437. veb = pf->veb[veb_idx];
  11438. veb->flags = flags;
  11439. veb->uplink_seid = uplink_seid;
  11440. veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
  11441. veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
  11442. /* create the VEB in the switch */
  11443. ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
  11444. if (ret)
  11445. goto err_veb;
  11446. if (vsi_idx == pf->lan_vsi)
  11447. pf->lan_veb = veb->idx;
  11448. return veb;
  11449. err_veb:
  11450. i40e_veb_clear(veb);
  11451. err_alloc:
  11452. return NULL;
  11453. }
  11454. /**
  11455. * i40e_setup_pf_switch_element - set PF vars based on switch type
  11456. * @pf: board private structure
  11457. * @ele: element we are building info from
  11458. * @num_reported: total number of elements
  11459. * @printconfig: should we print the contents
  11460. *
  11461. * helper function to assist in extracting a few useful SEID values.
  11462. **/
  11463. static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
  11464. struct i40e_aqc_switch_config_element_resp *ele,
  11465. u16 num_reported, bool printconfig)
  11466. {
  11467. u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
  11468. u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
  11469. u8 element_type = ele->element_type;
  11470. u16 seid = le16_to_cpu(ele->seid);
  11471. if (printconfig)
  11472. dev_info(&pf->pdev->dev,
  11473. "type=%d seid=%d uplink=%d downlink=%d\n",
  11474. element_type, seid, uplink_seid, downlink_seid);
  11475. switch (element_type) {
  11476. case I40E_SWITCH_ELEMENT_TYPE_MAC:
  11477. pf->mac_seid = seid;
  11478. break;
  11479. case I40E_SWITCH_ELEMENT_TYPE_VEB:
  11480. /* Main VEB? */
  11481. if (uplink_seid != pf->mac_seid)
  11482. break;
  11483. if (pf->lan_veb == I40E_NO_VEB) {
  11484. int v;
  11485. /* find existing or else empty VEB */
  11486. for (v = 0; v < I40E_MAX_VEB; v++) {
  11487. if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
  11488. pf->lan_veb = v;
  11489. break;
  11490. }
  11491. }
  11492. if (pf->lan_veb == I40E_NO_VEB) {
  11493. v = i40e_veb_mem_alloc(pf);
  11494. if (v < 0)
  11495. break;
  11496. pf->lan_veb = v;
  11497. }
  11498. }
  11499. pf->veb[pf->lan_veb]->seid = seid;
  11500. pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
  11501. pf->veb[pf->lan_veb]->pf = pf;
  11502. pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
  11503. break;
  11504. case I40E_SWITCH_ELEMENT_TYPE_VSI:
  11505. if (num_reported != 1)
  11506. break;
  11507. /* This is immediately after a reset so we can assume this is
  11508. * the PF's VSI
  11509. */
  11510. pf->mac_seid = uplink_seid;
  11511. pf->pf_seid = downlink_seid;
  11512. pf->main_vsi_seid = seid;
  11513. if (printconfig)
  11514. dev_info(&pf->pdev->dev,
  11515. "pf_seid=%d main_vsi_seid=%d\n",
  11516. pf->pf_seid, pf->main_vsi_seid);
  11517. break;
  11518. case I40E_SWITCH_ELEMENT_TYPE_PF:
  11519. case I40E_SWITCH_ELEMENT_TYPE_VF:
  11520. case I40E_SWITCH_ELEMENT_TYPE_EMP:
  11521. case I40E_SWITCH_ELEMENT_TYPE_BMC:
  11522. case I40E_SWITCH_ELEMENT_TYPE_PE:
  11523. case I40E_SWITCH_ELEMENT_TYPE_PA:
  11524. /* ignore these for now */
  11525. break;
  11526. default:
  11527. dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
  11528. element_type, seid);
  11529. break;
  11530. }
  11531. }
  11532. /**
  11533. * i40e_fetch_switch_configuration - Get switch config from firmware
  11534. * @pf: board private structure
  11535. * @printconfig: should we print the contents
  11536. *
  11537. * Get the current switch configuration from the device and
  11538. * extract a few useful SEID values.
  11539. **/
  11540. int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
  11541. {
  11542. struct i40e_aqc_get_switch_config_resp *sw_config;
  11543. u16 next_seid = 0;
  11544. int ret = 0;
  11545. u8 *aq_buf;
  11546. int i;
  11547. aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
  11548. if (!aq_buf)
  11549. return -ENOMEM;
  11550. sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
  11551. do {
  11552. u16 num_reported, num_total;
  11553. ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
  11554. I40E_AQ_LARGE_BUF,
  11555. &next_seid, NULL);
  11556. if (ret) {
  11557. dev_info(&pf->pdev->dev,
  11558. "get switch config failed err %s aq_err %s\n",
  11559. i40e_stat_str(&pf->hw, ret),
  11560. i40e_aq_str(&pf->hw,
  11561. pf->hw.aq.asq_last_status));
  11562. kfree(aq_buf);
  11563. return -ENOENT;
  11564. }
  11565. num_reported = le16_to_cpu(sw_config->header.num_reported);
  11566. num_total = le16_to_cpu(sw_config->header.num_total);
  11567. if (printconfig)
  11568. dev_info(&pf->pdev->dev,
  11569. "header: %d reported %d total\n",
  11570. num_reported, num_total);
  11571. for (i = 0; i < num_reported; i++) {
  11572. struct i40e_aqc_switch_config_element_resp *ele =
  11573. &sw_config->element[i];
  11574. i40e_setup_pf_switch_element(pf, ele, num_reported,
  11575. printconfig);
  11576. }
  11577. } while (next_seid != 0);
  11578. kfree(aq_buf);
  11579. return ret;
  11580. }
  11581. /**
  11582. * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
  11583. * @pf: board private structure
  11584. * @reinit: if the Main VSI needs to re-initialized.
  11585. *
  11586. * Returns 0 on success, negative value on failure
  11587. **/
  11588. static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit)
  11589. {
  11590. u16 flags = 0;
  11591. int ret;
  11592. /* find out what's out there already */
  11593. ret = i40e_fetch_switch_configuration(pf, false);
  11594. if (ret) {
  11595. dev_info(&pf->pdev->dev,
  11596. "couldn't fetch switch config, err %s aq_err %s\n",
  11597. i40e_stat_str(&pf->hw, ret),
  11598. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  11599. return ret;
  11600. }
  11601. i40e_pf_reset_stats(pf);
  11602. /* set the switch config bit for the whole device to
  11603. * support limited promisc or true promisc
  11604. * when user requests promisc. The default is limited
  11605. * promisc.
  11606. */
  11607. if ((pf->hw.pf_id == 0) &&
  11608. !(pf->flags & I40E_FLAG_TRUE_PROMISC_SUPPORT)) {
  11609. flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
  11610. pf->last_sw_conf_flags = flags;
  11611. }
  11612. if (pf->hw.pf_id == 0) {
  11613. u16 valid_flags;
  11614. valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
  11615. ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags, 0,
  11616. NULL);
  11617. if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) {
  11618. dev_info(&pf->pdev->dev,
  11619. "couldn't set switch config bits, err %s aq_err %s\n",
  11620. i40e_stat_str(&pf->hw, ret),
  11621. i40e_aq_str(&pf->hw,
  11622. pf->hw.aq.asq_last_status));
  11623. /* not a fatal problem, just keep going */
  11624. }
  11625. pf->last_sw_conf_valid_flags = valid_flags;
  11626. }
  11627. /* first time setup */
  11628. if (pf->lan_vsi == I40E_NO_VSI || reinit) {
  11629. struct i40e_vsi *vsi = NULL;
  11630. u16 uplink_seid;
  11631. /* Set up the PF VSI associated with the PF's main VSI
  11632. * that is already in the HW switch
  11633. */
  11634. if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
  11635. uplink_seid = pf->veb[pf->lan_veb]->seid;
  11636. else
  11637. uplink_seid = pf->mac_seid;
  11638. if (pf->lan_vsi == I40E_NO_VSI)
  11639. vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
  11640. else if (reinit)
  11641. vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
  11642. if (!vsi) {
  11643. dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
  11644. i40e_cloud_filter_exit(pf);
  11645. i40e_fdir_teardown(pf);
  11646. return -EAGAIN;
  11647. }
  11648. } else {
  11649. /* force a reset of TC and queue layout configurations */
  11650. u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
  11651. pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
  11652. pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
  11653. i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
  11654. }
  11655. i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
  11656. i40e_fdir_sb_setup(pf);
  11657. /* Setup static PF queue filter control settings */
  11658. ret = i40e_setup_pf_filter_control(pf);
  11659. if (ret) {
  11660. dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
  11661. ret);
  11662. /* Failure here should not stop continuing other steps */
  11663. }
  11664. /* enable RSS in the HW, even for only one queue, as the stack can use
  11665. * the hash
  11666. */
  11667. if ((pf->flags & I40E_FLAG_RSS_ENABLED))
  11668. i40e_pf_config_rss(pf);
  11669. /* fill in link information and enable LSE reporting */
  11670. i40e_link_event(pf);
  11671. /* Initialize user-specific link properties */
  11672. pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
  11673. I40E_AQ_AN_COMPLETED) ? true : false);
  11674. i40e_ptp_init(pf);
  11675. /* repopulate tunnel port filters */
  11676. i40e_sync_udp_filters(pf);
  11677. return ret;
  11678. }
  11679. /**
  11680. * i40e_determine_queue_usage - Work out queue distribution
  11681. * @pf: board private structure
  11682. **/
  11683. static void i40e_determine_queue_usage(struct i40e_pf *pf)
  11684. {
  11685. int queues_left;
  11686. int q_max;
  11687. pf->num_lan_qps = 0;
  11688. /* Find the max queues to be put into basic use. We'll always be
  11689. * using TC0, whether or not DCB is running, and TC0 will get the
  11690. * big RSS set.
  11691. */
  11692. queues_left = pf->hw.func_caps.num_tx_qp;
  11693. if ((queues_left == 1) ||
  11694. !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
  11695. /* one qp for PF, no queues for anything else */
  11696. queues_left = 0;
  11697. pf->alloc_rss_size = pf->num_lan_qps = 1;
  11698. /* make sure all the fancies are disabled */
  11699. pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
  11700. I40E_FLAG_IWARP_ENABLED |
  11701. I40E_FLAG_FD_SB_ENABLED |
  11702. I40E_FLAG_FD_ATR_ENABLED |
  11703. I40E_FLAG_DCB_CAPABLE |
  11704. I40E_FLAG_DCB_ENABLED |
  11705. I40E_FLAG_SRIOV_ENABLED |
  11706. I40E_FLAG_VMDQ_ENABLED);
  11707. pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
  11708. } else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
  11709. I40E_FLAG_FD_SB_ENABLED |
  11710. I40E_FLAG_FD_ATR_ENABLED |
  11711. I40E_FLAG_DCB_CAPABLE))) {
  11712. /* one qp for PF */
  11713. pf->alloc_rss_size = pf->num_lan_qps = 1;
  11714. queues_left -= pf->num_lan_qps;
  11715. pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
  11716. I40E_FLAG_IWARP_ENABLED |
  11717. I40E_FLAG_FD_SB_ENABLED |
  11718. I40E_FLAG_FD_ATR_ENABLED |
  11719. I40E_FLAG_DCB_ENABLED |
  11720. I40E_FLAG_VMDQ_ENABLED);
  11721. pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
  11722. } else {
  11723. /* Not enough queues for all TCs */
  11724. if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
  11725. (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
  11726. pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
  11727. I40E_FLAG_DCB_ENABLED);
  11728. dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
  11729. }
  11730. /* limit lan qps to the smaller of qps, cpus or msix */
  11731. q_max = max_t(int, pf->rss_size_max, num_online_cpus());
  11732. q_max = min_t(int, q_max, pf->hw.func_caps.num_tx_qp);
  11733. q_max = min_t(int, q_max, pf->hw.func_caps.num_msix_vectors);
  11734. pf->num_lan_qps = q_max;
  11735. queues_left -= pf->num_lan_qps;
  11736. }
  11737. if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
  11738. if (queues_left > 1) {
  11739. queues_left -= 1; /* save 1 queue for FD */
  11740. } else {
  11741. pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
  11742. pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
  11743. dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
  11744. }
  11745. }
  11746. if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
  11747. pf->num_vf_qps && pf->num_req_vfs && queues_left) {
  11748. pf->num_req_vfs = min_t(int, pf->num_req_vfs,
  11749. (queues_left / pf->num_vf_qps));
  11750. queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
  11751. }
  11752. if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
  11753. pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
  11754. pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
  11755. (queues_left / pf->num_vmdq_qps));
  11756. queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
  11757. }
  11758. pf->queues_left = queues_left;
  11759. dev_dbg(&pf->pdev->dev,
  11760. "qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
  11761. pf->hw.func_caps.num_tx_qp,
  11762. !!(pf->flags & I40E_FLAG_FD_SB_ENABLED),
  11763. pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
  11764. pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
  11765. queues_left);
  11766. }
  11767. /**
  11768. * i40e_setup_pf_filter_control - Setup PF static filter control
  11769. * @pf: PF to be setup
  11770. *
  11771. * i40e_setup_pf_filter_control sets up a PF's initial filter control
  11772. * settings. If PE/FCoE are enabled then it will also set the per PF
  11773. * based filter sizes required for them. It also enables Flow director,
  11774. * ethertype and macvlan type filter settings for the pf.
  11775. *
  11776. * Returns 0 on success, negative on failure
  11777. **/
  11778. static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
  11779. {
  11780. struct i40e_filter_control_settings *settings = &pf->filter_settings;
  11781. settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
  11782. /* Flow Director is enabled */
  11783. if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
  11784. settings->enable_fdir = true;
  11785. /* Ethtype and MACVLAN filters enabled for PF */
  11786. settings->enable_ethtype = true;
  11787. settings->enable_macvlan = true;
  11788. if (i40e_set_filter_control(&pf->hw, settings))
  11789. return -ENOENT;
  11790. return 0;
  11791. }
  11792. #define INFO_STRING_LEN 255
  11793. #define REMAIN(__x) (INFO_STRING_LEN - (__x))
  11794. static void i40e_print_features(struct i40e_pf *pf)
  11795. {
  11796. struct i40e_hw *hw = &pf->hw;
  11797. char *buf;
  11798. int i;
  11799. buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
  11800. if (!buf)
  11801. return;
  11802. i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
  11803. #ifdef CONFIG_PCI_IOV
  11804. i += snprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
  11805. #endif
  11806. i += snprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d",
  11807. pf->hw.func_caps.num_vsis,
  11808. pf->vsi[pf->lan_vsi]->num_queue_pairs);
  11809. if (pf->flags & I40E_FLAG_RSS_ENABLED)
  11810. i += snprintf(&buf[i], REMAIN(i), " RSS");
  11811. if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
  11812. i += snprintf(&buf[i], REMAIN(i), " FD_ATR");
  11813. if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
  11814. i += snprintf(&buf[i], REMAIN(i), " FD_SB");
  11815. i += snprintf(&buf[i], REMAIN(i), " NTUPLE");
  11816. }
  11817. if (pf->flags & I40E_FLAG_DCB_CAPABLE)
  11818. i += snprintf(&buf[i], REMAIN(i), " DCB");
  11819. i += snprintf(&buf[i], REMAIN(i), " VxLAN");
  11820. i += snprintf(&buf[i], REMAIN(i), " Geneve");
  11821. if (pf->flags & I40E_FLAG_PTP)
  11822. i += snprintf(&buf[i], REMAIN(i), " PTP");
  11823. if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
  11824. i += snprintf(&buf[i], REMAIN(i), " VEB");
  11825. else
  11826. i += snprintf(&buf[i], REMAIN(i), " VEPA");
  11827. dev_info(&pf->pdev->dev, "%s\n", buf);
  11828. kfree(buf);
  11829. WARN_ON(i > INFO_STRING_LEN);
  11830. }
  11831. /**
  11832. * i40e_get_platform_mac_addr - get platform-specific MAC address
  11833. * @pdev: PCI device information struct
  11834. * @pf: board private structure
  11835. *
  11836. * Look up the MAC address for the device. First we'll try
  11837. * eth_platform_get_mac_address, which will check Open Firmware, or arch
  11838. * specific fallback. Otherwise, we'll default to the stored value in
  11839. * firmware.
  11840. **/
  11841. static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
  11842. {
  11843. if (eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
  11844. i40e_get_mac_addr(&pf->hw, pf->hw.mac.addr);
  11845. }
  11846. /**
  11847. * i40e_probe - Device initialization routine
  11848. * @pdev: PCI device information struct
  11849. * @ent: entry in i40e_pci_tbl
  11850. *
  11851. * i40e_probe initializes a PF identified by a pci_dev structure.
  11852. * The OS initialization, configuring of the PF private structure,
  11853. * and a hardware reset occur.
  11854. *
  11855. * Returns 0 on success, negative on failure
  11856. **/
  11857. static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  11858. {
  11859. struct i40e_aq_get_phy_abilities_resp abilities;
  11860. struct i40e_pf *pf;
  11861. struct i40e_hw *hw;
  11862. static u16 pfs_found;
  11863. u16 wol_nvm_bits;
  11864. u16 link_status;
  11865. int err;
  11866. u32 val;
  11867. u32 i;
  11868. u8 set_fc_aq_fail;
  11869. err = pci_enable_device_mem(pdev);
  11870. if (err)
  11871. return err;
  11872. /* set up for high or low dma */
  11873. err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
  11874. if (err) {
  11875. err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  11876. if (err) {
  11877. dev_err(&pdev->dev,
  11878. "DMA configuration failed: 0x%x\n", err);
  11879. goto err_dma;
  11880. }
  11881. }
  11882. /* set up pci connections */
  11883. err = pci_request_mem_regions(pdev, i40e_driver_name);
  11884. if (err) {
  11885. dev_info(&pdev->dev,
  11886. "pci_request_selected_regions failed %d\n", err);
  11887. goto err_pci_reg;
  11888. }
  11889. pci_enable_pcie_error_reporting(pdev);
  11890. pci_set_master(pdev);
  11891. /* Now that we have a PCI connection, we need to do the
  11892. * low level device setup. This is primarily setting up
  11893. * the Admin Queue structures and then querying for the
  11894. * device's current profile information.
  11895. */
  11896. pf = kzalloc(sizeof(*pf), GFP_KERNEL);
  11897. if (!pf) {
  11898. err = -ENOMEM;
  11899. goto err_pf_alloc;
  11900. }
  11901. pf->next_vsi = 0;
  11902. pf->pdev = pdev;
  11903. set_bit(__I40E_DOWN, pf->state);
  11904. hw = &pf->hw;
  11905. hw->back = pf;
  11906. pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
  11907. I40E_MAX_CSR_SPACE);
  11908. hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
  11909. if (!hw->hw_addr) {
  11910. err = -EIO;
  11911. dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
  11912. (unsigned int)pci_resource_start(pdev, 0),
  11913. pf->ioremap_len, err);
  11914. goto err_ioremap;
  11915. }
  11916. hw->vendor_id = pdev->vendor;
  11917. hw->device_id = pdev->device;
  11918. pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
  11919. hw->subsystem_vendor_id = pdev->subsystem_vendor;
  11920. hw->subsystem_device_id = pdev->subsystem_device;
  11921. hw->bus.device = PCI_SLOT(pdev->devfn);
  11922. hw->bus.func = PCI_FUNC(pdev->devfn);
  11923. hw->bus.bus_id = pdev->bus->number;
  11924. pf->instance = pfs_found;
  11925. /* Select something other than the 802.1ad ethertype for the
  11926. * switch to use internally and drop on ingress.
  11927. */
  11928. hw->switch_tag = 0xffff;
  11929. hw->first_tag = ETH_P_8021AD;
  11930. hw->second_tag = ETH_P_8021Q;
  11931. INIT_LIST_HEAD(&pf->l3_flex_pit_list);
  11932. INIT_LIST_HEAD(&pf->l4_flex_pit_list);
  11933. /* set up the locks for the AQ, do this only once in probe
  11934. * and destroy them only once in remove
  11935. */
  11936. mutex_init(&hw->aq.asq_mutex);
  11937. mutex_init(&hw->aq.arq_mutex);
  11938. pf->msg_enable = netif_msg_init(debug,
  11939. NETIF_MSG_DRV |
  11940. NETIF_MSG_PROBE |
  11941. NETIF_MSG_LINK);
  11942. if (debug < -1)
  11943. pf->hw.debug_mask = debug;
  11944. /* do a special CORER for clearing PXE mode once at init */
  11945. if (hw->revision_id == 0 &&
  11946. (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
  11947. wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
  11948. i40e_flush(hw);
  11949. msleep(200);
  11950. pf->corer_count++;
  11951. i40e_clear_pxe_mode(hw);
  11952. }
  11953. /* Reset here to make sure all is clean and to define PF 'n' */
  11954. i40e_clear_hw(hw);
  11955. err = i40e_pf_reset(hw);
  11956. if (err) {
  11957. dev_info(&pdev->dev, "Initial pf_reset failed: %d\n", err);
  11958. goto err_pf_reset;
  11959. }
  11960. pf->pfr_count++;
  11961. hw->aq.num_arq_entries = I40E_AQ_LEN;
  11962. hw->aq.num_asq_entries = I40E_AQ_LEN;
  11963. hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
  11964. hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
  11965. pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
  11966. snprintf(pf->int_name, sizeof(pf->int_name) - 1,
  11967. "%s-%s:misc",
  11968. dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
  11969. err = i40e_init_shared_code(hw);
  11970. if (err) {
  11971. dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
  11972. err);
  11973. goto err_pf_reset;
  11974. }
  11975. /* set up a default setting for link flow control */
  11976. pf->hw.fc.requested_mode = I40E_FC_NONE;
  11977. err = i40e_init_adminq(hw);
  11978. if (err) {
  11979. if (err == I40E_ERR_FIRMWARE_API_VERSION)
  11980. dev_info(&pdev->dev,
  11981. "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");
  11982. else
  11983. dev_info(&pdev->dev,
  11984. "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
  11985. goto err_pf_reset;
  11986. }
  11987. i40e_get_oem_version(hw);
  11988. /* provide nvm, fw, api versions */
  11989. dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s\n",
  11990. hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
  11991. hw->aq.api_maj_ver, hw->aq.api_min_ver,
  11992. i40e_nvm_version_str(hw));
  11993. if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
  11994. hw->aq.api_min_ver > I40E_FW_MINOR_VERSION(hw))
  11995. dev_info(&pdev->dev,
  11996. "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");
  11997. else if (hw->aq.api_maj_ver == 1 && hw->aq.api_min_ver < 4)
  11998. dev_info(&pdev->dev,
  11999. "The driver for the device detected an older version of the NVM image than expected. Please update the NVM image.\n");
  12000. i40e_verify_eeprom(pf);
  12001. /* Rev 0 hardware was never productized */
  12002. if (hw->revision_id < 1)
  12003. 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");
  12004. i40e_clear_pxe_mode(hw);
  12005. err = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
  12006. if (err)
  12007. goto err_adminq_setup;
  12008. err = i40e_sw_init(pf);
  12009. if (err) {
  12010. dev_info(&pdev->dev, "sw_init failed: %d\n", err);
  12011. goto err_sw_init;
  12012. }
  12013. err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
  12014. hw->func_caps.num_rx_qp, 0, 0);
  12015. if (err) {
  12016. dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
  12017. goto err_init_lan_hmc;
  12018. }
  12019. err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
  12020. if (err) {
  12021. dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
  12022. err = -ENOENT;
  12023. goto err_configure_lan_hmc;
  12024. }
  12025. /* Disable LLDP for NICs that have firmware versions lower than v4.3.
  12026. * Ignore error return codes because if it was already disabled via
  12027. * hardware settings this will fail
  12028. */
  12029. if (pf->hw_features & I40E_HW_STOP_FW_LLDP) {
  12030. dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
  12031. i40e_aq_stop_lldp(hw, true, NULL);
  12032. }
  12033. /* allow a platform config to override the HW addr */
  12034. i40e_get_platform_mac_addr(pdev, pf);
  12035. if (!is_valid_ether_addr(hw->mac.addr)) {
  12036. dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
  12037. err = -EIO;
  12038. goto err_mac_addr;
  12039. }
  12040. dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
  12041. ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
  12042. i40e_get_port_mac_addr(hw, hw->mac.port_addr);
  12043. if (is_valid_ether_addr(hw->mac.port_addr))
  12044. pf->hw_features |= I40E_HW_PORT_ID_VALID;
  12045. pci_set_drvdata(pdev, pf);
  12046. pci_save_state(pdev);
  12047. /* Enable FW to write default DCB config on link-up */
  12048. i40e_aq_set_dcb_parameters(hw, true, NULL);
  12049. #ifdef CONFIG_I40E_DCB
  12050. err = i40e_init_pf_dcb(pf);
  12051. if (err) {
  12052. dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
  12053. pf->flags &= ~(I40E_FLAG_DCB_CAPABLE | I40E_FLAG_DCB_ENABLED);
  12054. /* Continue without DCB enabled */
  12055. }
  12056. #endif /* CONFIG_I40E_DCB */
  12057. /* set up periodic task facility */
  12058. timer_setup(&pf->service_timer, i40e_service_timer, 0);
  12059. pf->service_timer_period = HZ;
  12060. INIT_WORK(&pf->service_task, i40e_service_task);
  12061. clear_bit(__I40E_SERVICE_SCHED, pf->state);
  12062. /* NVM bit on means WoL disabled for the port */
  12063. i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
  12064. if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
  12065. pf->wol_en = false;
  12066. else
  12067. pf->wol_en = true;
  12068. device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
  12069. /* set up the main switch operations */
  12070. i40e_determine_queue_usage(pf);
  12071. err = i40e_init_interrupt_scheme(pf);
  12072. if (err)
  12073. goto err_switch_setup;
  12074. /* The number of VSIs reported by the FW is the minimum guaranteed
  12075. * to us; HW supports far more and we share the remaining pool with
  12076. * the other PFs. We allocate space for more than the guarantee with
  12077. * the understanding that we might not get them all later.
  12078. */
  12079. if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
  12080. pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
  12081. else
  12082. pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
  12083. /* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
  12084. pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
  12085. GFP_KERNEL);
  12086. if (!pf->vsi) {
  12087. err = -ENOMEM;
  12088. goto err_switch_setup;
  12089. }
  12090. #ifdef CONFIG_PCI_IOV
  12091. /* prep for VF support */
  12092. if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
  12093. (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
  12094. !test_bit(__I40E_BAD_EEPROM, pf->state)) {
  12095. if (pci_num_vf(pdev))
  12096. pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
  12097. }
  12098. #endif
  12099. err = i40e_setup_pf_switch(pf, false);
  12100. if (err) {
  12101. dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
  12102. goto err_vsis;
  12103. }
  12104. INIT_LIST_HEAD(&pf->vsi[pf->lan_vsi]->ch_list);
  12105. /* Make sure flow control is set according to current settings */
  12106. err = i40e_set_fc(hw, &set_fc_aq_fail, true);
  12107. if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_GET)
  12108. dev_dbg(&pf->pdev->dev,
  12109. "Set fc with err %s aq_err %s on get_phy_cap\n",
  12110. i40e_stat_str(hw, err),
  12111. i40e_aq_str(hw, hw->aq.asq_last_status));
  12112. if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_SET)
  12113. dev_dbg(&pf->pdev->dev,
  12114. "Set fc with err %s aq_err %s on set_phy_config\n",
  12115. i40e_stat_str(hw, err),
  12116. i40e_aq_str(hw, hw->aq.asq_last_status));
  12117. if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_UPDATE)
  12118. dev_dbg(&pf->pdev->dev,
  12119. "Set fc with err %s aq_err %s on get_link_info\n",
  12120. i40e_stat_str(hw, err),
  12121. i40e_aq_str(hw, hw->aq.asq_last_status));
  12122. /* if FDIR VSI was set up, start it now */
  12123. for (i = 0; i < pf->num_alloc_vsi; i++) {
  12124. if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
  12125. i40e_vsi_open(pf->vsi[i]);
  12126. break;
  12127. }
  12128. }
  12129. /* The driver only wants link up/down and module qualification
  12130. * reports from firmware. Note the negative logic.
  12131. */
  12132. err = i40e_aq_set_phy_int_mask(&pf->hw,
  12133. ~(I40E_AQ_EVENT_LINK_UPDOWN |
  12134. I40E_AQ_EVENT_MEDIA_NA |
  12135. I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
  12136. if (err)
  12137. dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
  12138. i40e_stat_str(&pf->hw, err),
  12139. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  12140. /* Reconfigure hardware for allowing smaller MSS in the case
  12141. * of TSO, so that we avoid the MDD being fired and causing
  12142. * a reset in the case of small MSS+TSO.
  12143. */
  12144. val = rd32(hw, I40E_REG_MSS);
  12145. if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
  12146. val &= ~I40E_REG_MSS_MIN_MASK;
  12147. val |= I40E_64BYTE_MSS;
  12148. wr32(hw, I40E_REG_MSS, val);
  12149. }
  12150. if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
  12151. msleep(75);
  12152. err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
  12153. if (err)
  12154. dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
  12155. i40e_stat_str(&pf->hw, err),
  12156. i40e_aq_str(&pf->hw,
  12157. pf->hw.aq.asq_last_status));
  12158. }
  12159. /* The main driver is (mostly) up and happy. We need to set this state
  12160. * before setting up the misc vector or we get a race and the vector
  12161. * ends up disabled forever.
  12162. */
  12163. clear_bit(__I40E_DOWN, pf->state);
  12164. /* In case of MSIX we are going to setup the misc vector right here
  12165. * to handle admin queue events etc. In case of legacy and MSI
  12166. * the misc functionality and queue processing is combined in
  12167. * the same vector and that gets setup at open.
  12168. */
  12169. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  12170. err = i40e_setup_misc_vector(pf);
  12171. if (err) {
  12172. dev_info(&pdev->dev,
  12173. "setup of misc vector failed: %d\n", err);
  12174. goto err_vsis;
  12175. }
  12176. }
  12177. #ifdef CONFIG_PCI_IOV
  12178. /* prep for VF support */
  12179. if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
  12180. (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
  12181. !test_bit(__I40E_BAD_EEPROM, pf->state)) {
  12182. /* disable link interrupts for VFs */
  12183. val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
  12184. val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
  12185. wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
  12186. i40e_flush(hw);
  12187. if (pci_num_vf(pdev)) {
  12188. dev_info(&pdev->dev,
  12189. "Active VFs found, allocating resources.\n");
  12190. err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
  12191. if (err)
  12192. dev_info(&pdev->dev,
  12193. "Error %d allocating resources for existing VFs\n",
  12194. err);
  12195. }
  12196. }
  12197. #endif /* CONFIG_PCI_IOV */
  12198. if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
  12199. pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
  12200. pf->num_iwarp_msix,
  12201. I40E_IWARP_IRQ_PILE_ID);
  12202. if (pf->iwarp_base_vector < 0) {
  12203. dev_info(&pdev->dev,
  12204. "failed to get tracking for %d vectors for IWARP err=%d\n",
  12205. pf->num_iwarp_msix, pf->iwarp_base_vector);
  12206. pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
  12207. }
  12208. }
  12209. i40e_dbg_pf_init(pf);
  12210. /* tell the firmware that we're starting */
  12211. i40e_send_version(pf);
  12212. /* since everything's happy, start the service_task timer */
  12213. mod_timer(&pf->service_timer,
  12214. round_jiffies(jiffies + pf->service_timer_period));
  12215. /* add this PF to client device list and launch a client service task */
  12216. if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
  12217. err = i40e_lan_add_device(pf);
  12218. if (err)
  12219. dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
  12220. err);
  12221. }
  12222. #define PCI_SPEED_SIZE 8
  12223. #define PCI_WIDTH_SIZE 8
  12224. /* Devices on the IOSF bus do not have this information
  12225. * and will report PCI Gen 1 x 1 by default so don't bother
  12226. * checking them.
  12227. */
  12228. if (!(pf->hw_features & I40E_HW_NO_PCI_LINK_CHECK)) {
  12229. char speed[PCI_SPEED_SIZE] = "Unknown";
  12230. char width[PCI_WIDTH_SIZE] = "Unknown";
  12231. /* Get the negotiated link width and speed from PCI config
  12232. * space
  12233. */
  12234. pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
  12235. &link_status);
  12236. i40e_set_pci_config_data(hw, link_status);
  12237. switch (hw->bus.speed) {
  12238. case i40e_bus_speed_8000:
  12239. strncpy(speed, "8.0", PCI_SPEED_SIZE); break;
  12240. case i40e_bus_speed_5000:
  12241. strncpy(speed, "5.0", PCI_SPEED_SIZE); break;
  12242. case i40e_bus_speed_2500:
  12243. strncpy(speed, "2.5", PCI_SPEED_SIZE); break;
  12244. default:
  12245. break;
  12246. }
  12247. switch (hw->bus.width) {
  12248. case i40e_bus_width_pcie_x8:
  12249. strncpy(width, "8", PCI_WIDTH_SIZE); break;
  12250. case i40e_bus_width_pcie_x4:
  12251. strncpy(width, "4", PCI_WIDTH_SIZE); break;
  12252. case i40e_bus_width_pcie_x2:
  12253. strncpy(width, "2", PCI_WIDTH_SIZE); break;
  12254. case i40e_bus_width_pcie_x1:
  12255. strncpy(width, "1", PCI_WIDTH_SIZE); break;
  12256. default:
  12257. break;
  12258. }
  12259. dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
  12260. speed, width);
  12261. if (hw->bus.width < i40e_bus_width_pcie_x8 ||
  12262. hw->bus.speed < i40e_bus_speed_8000) {
  12263. dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
  12264. dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
  12265. }
  12266. }
  12267. /* get the requested speeds from the fw */
  12268. err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
  12269. if (err)
  12270. dev_dbg(&pf->pdev->dev, "get requested speeds ret = %s last_status = %s\n",
  12271. i40e_stat_str(&pf->hw, err),
  12272. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  12273. pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
  12274. /* get the supported phy types from the fw */
  12275. err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
  12276. if (err)
  12277. dev_dbg(&pf->pdev->dev, "get supported phy types ret = %s last_status = %s\n",
  12278. i40e_stat_str(&pf->hw, err),
  12279. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  12280. /* Add a filter to drop all Flow control frames from any VSI from being
  12281. * transmitted. By doing so we stop a malicious VF from sending out
  12282. * PAUSE or PFC frames and potentially controlling traffic for other
  12283. * PF/VF VSIs.
  12284. * The FW can still send Flow control frames if enabled.
  12285. */
  12286. i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
  12287. pf->main_vsi_seid);
  12288. if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
  12289. (pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
  12290. pf->hw_features |= I40E_HW_PHY_CONTROLS_LEDS;
  12291. if (pf->hw.device_id == I40E_DEV_ID_SFP_I_X722)
  12292. pf->hw_features |= I40E_HW_HAVE_CRT_RETIMER;
  12293. /* print a string summarizing features */
  12294. i40e_print_features(pf);
  12295. return 0;
  12296. /* Unwind what we've done if something failed in the setup */
  12297. err_vsis:
  12298. set_bit(__I40E_DOWN, pf->state);
  12299. i40e_clear_interrupt_scheme(pf);
  12300. kfree(pf->vsi);
  12301. err_switch_setup:
  12302. i40e_reset_interrupt_capability(pf);
  12303. del_timer_sync(&pf->service_timer);
  12304. err_mac_addr:
  12305. err_configure_lan_hmc:
  12306. (void)i40e_shutdown_lan_hmc(hw);
  12307. err_init_lan_hmc:
  12308. kfree(pf->qp_pile);
  12309. err_sw_init:
  12310. err_adminq_setup:
  12311. err_pf_reset:
  12312. iounmap(hw->hw_addr);
  12313. err_ioremap:
  12314. kfree(pf);
  12315. err_pf_alloc:
  12316. pci_disable_pcie_error_reporting(pdev);
  12317. pci_release_mem_regions(pdev);
  12318. err_pci_reg:
  12319. err_dma:
  12320. pci_disable_device(pdev);
  12321. return err;
  12322. }
  12323. /**
  12324. * i40e_remove - Device removal routine
  12325. * @pdev: PCI device information struct
  12326. *
  12327. * i40e_remove is called by the PCI subsystem to alert the driver
  12328. * that is should release a PCI device. This could be caused by a
  12329. * Hot-Plug event, or because the driver is going to be removed from
  12330. * memory.
  12331. **/
  12332. static void i40e_remove(struct pci_dev *pdev)
  12333. {
  12334. struct i40e_pf *pf = pci_get_drvdata(pdev);
  12335. struct i40e_hw *hw = &pf->hw;
  12336. i40e_status ret_code;
  12337. int i;
  12338. i40e_dbg_pf_exit(pf);
  12339. i40e_ptp_stop(pf);
  12340. /* Disable RSS in hw */
  12341. i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
  12342. i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
  12343. /* no more scheduling of any task */
  12344. set_bit(__I40E_SUSPENDED, pf->state);
  12345. set_bit(__I40E_DOWN, pf->state);
  12346. if (pf->service_timer.function)
  12347. del_timer_sync(&pf->service_timer);
  12348. if (pf->service_task.func)
  12349. cancel_work_sync(&pf->service_task);
  12350. /* Client close must be called explicitly here because the timer
  12351. * has been stopped.
  12352. */
  12353. i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
  12354. if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
  12355. i40e_free_vfs(pf);
  12356. pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
  12357. }
  12358. i40e_fdir_teardown(pf);
  12359. /* If there is a switch structure or any orphans, remove them.
  12360. * This will leave only the PF's VSI remaining.
  12361. */
  12362. for (i = 0; i < I40E_MAX_VEB; i++) {
  12363. if (!pf->veb[i])
  12364. continue;
  12365. if (pf->veb[i]->uplink_seid == pf->mac_seid ||
  12366. pf->veb[i]->uplink_seid == 0)
  12367. i40e_switch_branch_release(pf->veb[i]);
  12368. }
  12369. /* Now we can shutdown the PF's VSI, just before we kill
  12370. * adminq and hmc.
  12371. */
  12372. if (pf->vsi[pf->lan_vsi])
  12373. i40e_vsi_release(pf->vsi[pf->lan_vsi]);
  12374. i40e_cloud_filter_exit(pf);
  12375. /* remove attached clients */
  12376. if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
  12377. ret_code = i40e_lan_del_device(pf);
  12378. if (ret_code)
  12379. dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
  12380. ret_code);
  12381. }
  12382. /* shutdown and destroy the HMC */
  12383. if (hw->hmc.hmc_obj) {
  12384. ret_code = i40e_shutdown_lan_hmc(hw);
  12385. if (ret_code)
  12386. dev_warn(&pdev->dev,
  12387. "Failed to destroy the HMC resources: %d\n",
  12388. ret_code);
  12389. }
  12390. /* shutdown the adminq */
  12391. i40e_shutdown_adminq(hw);
  12392. /* destroy the locks only once, here */
  12393. mutex_destroy(&hw->aq.arq_mutex);
  12394. mutex_destroy(&hw->aq.asq_mutex);
  12395. /* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
  12396. i40e_clear_interrupt_scheme(pf);
  12397. for (i = 0; i < pf->num_alloc_vsi; i++) {
  12398. if (pf->vsi[i]) {
  12399. i40e_vsi_clear_rings(pf->vsi[i]);
  12400. i40e_vsi_clear(pf->vsi[i]);
  12401. pf->vsi[i] = NULL;
  12402. }
  12403. }
  12404. for (i = 0; i < I40E_MAX_VEB; i++) {
  12405. kfree(pf->veb[i]);
  12406. pf->veb[i] = NULL;
  12407. }
  12408. kfree(pf->qp_pile);
  12409. kfree(pf->vsi);
  12410. iounmap(hw->hw_addr);
  12411. kfree(pf);
  12412. pci_release_mem_regions(pdev);
  12413. pci_disable_pcie_error_reporting(pdev);
  12414. pci_disable_device(pdev);
  12415. }
  12416. /**
  12417. * i40e_pci_error_detected - warning that something funky happened in PCI land
  12418. * @pdev: PCI device information struct
  12419. * @error: the type of PCI error
  12420. *
  12421. * Called to warn that something happened and the error handling steps
  12422. * are in progress. Allows the driver to quiesce things, be ready for
  12423. * remediation.
  12424. **/
  12425. static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
  12426. enum pci_channel_state error)
  12427. {
  12428. struct i40e_pf *pf = pci_get_drvdata(pdev);
  12429. dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
  12430. if (!pf) {
  12431. dev_info(&pdev->dev,
  12432. "Cannot recover - error happened during device probe\n");
  12433. return PCI_ERS_RESULT_DISCONNECT;
  12434. }
  12435. /* shutdown all operations */
  12436. if (!test_bit(__I40E_SUSPENDED, pf->state))
  12437. i40e_prep_for_reset(pf, false);
  12438. /* Request a slot reset */
  12439. return PCI_ERS_RESULT_NEED_RESET;
  12440. }
  12441. /**
  12442. * i40e_pci_error_slot_reset - a PCI slot reset just happened
  12443. * @pdev: PCI device information struct
  12444. *
  12445. * Called to find if the driver can work with the device now that
  12446. * the pci slot has been reset. If a basic connection seems good
  12447. * (registers are readable and have sane content) then return a
  12448. * happy little PCI_ERS_RESULT_xxx.
  12449. **/
  12450. static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
  12451. {
  12452. struct i40e_pf *pf = pci_get_drvdata(pdev);
  12453. pci_ers_result_t result;
  12454. int err;
  12455. u32 reg;
  12456. dev_dbg(&pdev->dev, "%s\n", __func__);
  12457. if (pci_enable_device_mem(pdev)) {
  12458. dev_info(&pdev->dev,
  12459. "Cannot re-enable PCI device after reset.\n");
  12460. result = PCI_ERS_RESULT_DISCONNECT;
  12461. } else {
  12462. pci_set_master(pdev);
  12463. pci_restore_state(pdev);
  12464. pci_save_state(pdev);
  12465. pci_wake_from_d3(pdev, false);
  12466. reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
  12467. if (reg == 0)
  12468. result = PCI_ERS_RESULT_RECOVERED;
  12469. else
  12470. result = PCI_ERS_RESULT_DISCONNECT;
  12471. }
  12472. err = pci_cleanup_aer_uncorrect_error_status(pdev);
  12473. if (err) {
  12474. dev_info(&pdev->dev,
  12475. "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
  12476. err);
  12477. /* non-fatal, continue */
  12478. }
  12479. return result;
  12480. }
  12481. /**
  12482. * i40e_pci_error_reset_prepare - prepare device driver for pci reset
  12483. * @pdev: PCI device information struct
  12484. */
  12485. static void i40e_pci_error_reset_prepare(struct pci_dev *pdev)
  12486. {
  12487. struct i40e_pf *pf = pci_get_drvdata(pdev);
  12488. i40e_prep_for_reset(pf, false);
  12489. }
  12490. /**
  12491. * i40e_pci_error_reset_done - pci reset done, device driver reset can begin
  12492. * @pdev: PCI device information struct
  12493. */
  12494. static void i40e_pci_error_reset_done(struct pci_dev *pdev)
  12495. {
  12496. struct i40e_pf *pf = pci_get_drvdata(pdev);
  12497. i40e_reset_and_rebuild(pf, false, false);
  12498. }
  12499. /**
  12500. * i40e_pci_error_resume - restart operations after PCI error recovery
  12501. * @pdev: PCI device information struct
  12502. *
  12503. * Called to allow the driver to bring things back up after PCI error
  12504. * and/or reset recovery has finished.
  12505. **/
  12506. static void i40e_pci_error_resume(struct pci_dev *pdev)
  12507. {
  12508. struct i40e_pf *pf = pci_get_drvdata(pdev);
  12509. dev_dbg(&pdev->dev, "%s\n", __func__);
  12510. if (test_bit(__I40E_SUSPENDED, pf->state))
  12511. return;
  12512. i40e_handle_reset_warning(pf, false);
  12513. }
  12514. /**
  12515. * i40e_enable_mc_magic_wake - enable multicast magic packet wake up
  12516. * using the mac_address_write admin q function
  12517. * @pf: pointer to i40e_pf struct
  12518. **/
  12519. static void i40e_enable_mc_magic_wake(struct i40e_pf *pf)
  12520. {
  12521. struct i40e_hw *hw = &pf->hw;
  12522. i40e_status ret;
  12523. u8 mac_addr[6];
  12524. u16 flags = 0;
  12525. /* Get current MAC address in case it's an LAA */
  12526. if (pf->vsi[pf->lan_vsi] && pf->vsi[pf->lan_vsi]->netdev) {
  12527. ether_addr_copy(mac_addr,
  12528. pf->vsi[pf->lan_vsi]->netdev->dev_addr);
  12529. } else {
  12530. dev_err(&pf->pdev->dev,
  12531. "Failed to retrieve MAC address; using default\n");
  12532. ether_addr_copy(mac_addr, hw->mac.addr);
  12533. }
  12534. /* The FW expects the mac address write cmd to first be called with
  12535. * one of these flags before calling it again with the multicast
  12536. * enable flags.
  12537. */
  12538. flags = I40E_AQC_WRITE_TYPE_LAA_WOL;
  12539. if (hw->func_caps.flex10_enable && hw->partition_id != 1)
  12540. flags = I40E_AQC_WRITE_TYPE_LAA_ONLY;
  12541. ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
  12542. if (ret) {
  12543. dev_err(&pf->pdev->dev,
  12544. "Failed to update MAC address registers; cannot enable Multicast Magic packet wake up");
  12545. return;
  12546. }
  12547. flags = I40E_AQC_MC_MAG_EN
  12548. | I40E_AQC_WOL_PRESERVE_ON_PFR
  12549. | I40E_AQC_WRITE_TYPE_UPDATE_MC_MAG;
  12550. ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
  12551. if (ret)
  12552. dev_err(&pf->pdev->dev,
  12553. "Failed to enable Multicast Magic Packet wake up\n");
  12554. }
  12555. /**
  12556. * i40e_shutdown - PCI callback for shutting down
  12557. * @pdev: PCI device information struct
  12558. **/
  12559. static void i40e_shutdown(struct pci_dev *pdev)
  12560. {
  12561. struct i40e_pf *pf = pci_get_drvdata(pdev);
  12562. struct i40e_hw *hw = &pf->hw;
  12563. set_bit(__I40E_SUSPENDED, pf->state);
  12564. set_bit(__I40E_DOWN, pf->state);
  12565. rtnl_lock();
  12566. i40e_prep_for_reset(pf, true);
  12567. rtnl_unlock();
  12568. wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
  12569. wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
  12570. del_timer_sync(&pf->service_timer);
  12571. cancel_work_sync(&pf->service_task);
  12572. i40e_cloud_filter_exit(pf);
  12573. i40e_fdir_teardown(pf);
  12574. /* Client close must be called explicitly here because the timer
  12575. * has been stopped.
  12576. */
  12577. i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
  12578. if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
  12579. i40e_enable_mc_magic_wake(pf);
  12580. i40e_prep_for_reset(pf, false);
  12581. wr32(hw, I40E_PFPM_APM,
  12582. (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
  12583. wr32(hw, I40E_PFPM_WUFC,
  12584. (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
  12585. i40e_clear_interrupt_scheme(pf);
  12586. if (system_state == SYSTEM_POWER_OFF) {
  12587. pci_wake_from_d3(pdev, pf->wol_en);
  12588. pci_set_power_state(pdev, PCI_D3hot);
  12589. }
  12590. }
  12591. /**
  12592. * i40e_suspend - PM callback for moving to D3
  12593. * @dev: generic device information structure
  12594. **/
  12595. static int __maybe_unused i40e_suspend(struct device *dev)
  12596. {
  12597. struct pci_dev *pdev = to_pci_dev(dev);
  12598. struct i40e_pf *pf = pci_get_drvdata(pdev);
  12599. struct i40e_hw *hw = &pf->hw;
  12600. /* If we're already suspended, then there is nothing to do */
  12601. if (test_and_set_bit(__I40E_SUSPENDED, pf->state))
  12602. return 0;
  12603. set_bit(__I40E_DOWN, pf->state);
  12604. /* Ensure service task will not be running */
  12605. del_timer_sync(&pf->service_timer);
  12606. cancel_work_sync(&pf->service_task);
  12607. /* Client close must be called explicitly here because the timer
  12608. * has been stopped.
  12609. */
  12610. i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
  12611. if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
  12612. i40e_enable_mc_magic_wake(pf);
  12613. /* Since we're going to destroy queues during the
  12614. * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
  12615. * whole section
  12616. */
  12617. rtnl_lock();
  12618. i40e_prep_for_reset(pf, true);
  12619. wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
  12620. wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
  12621. /* Clear the interrupt scheme and release our IRQs so that the system
  12622. * can safely hibernate even when there are a large number of CPUs.
  12623. * Otherwise hibernation might fail when mapping all the vectors back
  12624. * to CPU0.
  12625. */
  12626. i40e_clear_interrupt_scheme(pf);
  12627. rtnl_unlock();
  12628. return 0;
  12629. }
  12630. /**
  12631. * i40e_resume - PM callback for waking up from D3
  12632. * @dev: generic device information structure
  12633. **/
  12634. static int __maybe_unused i40e_resume(struct device *dev)
  12635. {
  12636. struct pci_dev *pdev = to_pci_dev(dev);
  12637. struct i40e_pf *pf = pci_get_drvdata(pdev);
  12638. int err;
  12639. /* If we're not suspended, then there is nothing to do */
  12640. if (!test_bit(__I40E_SUSPENDED, pf->state))
  12641. return 0;
  12642. /* We need to hold the RTNL lock prior to restoring interrupt schemes,
  12643. * since we're going to be restoring queues
  12644. */
  12645. rtnl_lock();
  12646. /* We cleared the interrupt scheme when we suspended, so we need to
  12647. * restore it now to resume device functionality.
  12648. */
  12649. err = i40e_restore_interrupt_scheme(pf);
  12650. if (err) {
  12651. dev_err(&pdev->dev, "Cannot restore interrupt scheme: %d\n",
  12652. err);
  12653. }
  12654. clear_bit(__I40E_DOWN, pf->state);
  12655. i40e_reset_and_rebuild(pf, false, true);
  12656. rtnl_unlock();
  12657. /* Clear suspended state last after everything is recovered */
  12658. clear_bit(__I40E_SUSPENDED, pf->state);
  12659. /* Restart the service task */
  12660. mod_timer(&pf->service_timer,
  12661. round_jiffies(jiffies + pf->service_timer_period));
  12662. return 0;
  12663. }
  12664. static const struct pci_error_handlers i40e_err_handler = {
  12665. .error_detected = i40e_pci_error_detected,
  12666. .slot_reset = i40e_pci_error_slot_reset,
  12667. .reset_prepare = i40e_pci_error_reset_prepare,
  12668. .reset_done = i40e_pci_error_reset_done,
  12669. .resume = i40e_pci_error_resume,
  12670. };
  12671. static SIMPLE_DEV_PM_OPS(i40e_pm_ops, i40e_suspend, i40e_resume);
  12672. static struct pci_driver i40e_driver = {
  12673. .name = i40e_driver_name,
  12674. .id_table = i40e_pci_tbl,
  12675. .probe = i40e_probe,
  12676. .remove = i40e_remove,
  12677. .driver = {
  12678. .pm = &i40e_pm_ops,
  12679. },
  12680. .shutdown = i40e_shutdown,
  12681. .err_handler = &i40e_err_handler,
  12682. .sriov_configure = i40e_pci_sriov_configure,
  12683. };
  12684. /**
  12685. * i40e_init_module - Driver registration routine
  12686. *
  12687. * i40e_init_module is the first routine called when the driver is
  12688. * loaded. All it does is register with the PCI subsystem.
  12689. **/
  12690. static int __init i40e_init_module(void)
  12691. {
  12692. pr_info("%s: %s - version %s\n", i40e_driver_name,
  12693. i40e_driver_string, i40e_driver_version_str);
  12694. pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
  12695. /* There is no need to throttle the number of active tasks because
  12696. * each device limits its own task using a state bit for scheduling
  12697. * the service task, and the device tasks do not interfere with each
  12698. * other, so we don't set a max task limit. We must set WQ_MEM_RECLAIM
  12699. * since we need to be able to guarantee forward progress even under
  12700. * memory pressure.
  12701. */
  12702. i40e_wq = alloc_workqueue("%s", WQ_MEM_RECLAIM, 0, i40e_driver_name);
  12703. if (!i40e_wq) {
  12704. pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
  12705. return -ENOMEM;
  12706. }
  12707. i40e_dbg_init();
  12708. return pci_register_driver(&i40e_driver);
  12709. }
  12710. module_init(i40e_init_module);
  12711. /**
  12712. * i40e_exit_module - Driver exit cleanup routine
  12713. *
  12714. * i40e_exit_module is called just before the driver is removed
  12715. * from memory.
  12716. **/
  12717. static void __exit i40e_exit_module(void)
  12718. {
  12719. pci_unregister_driver(&i40e_driver);
  12720. destroy_workqueue(i40e_wq);
  12721. i40e_dbg_exit();
  12722. }
  12723. module_exit(i40e_exit_module);