i40e_main.c 337 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226
  1. /*******************************************************************************
  2. *
  3. * Intel Ethernet Controller XL710 Family Linux Driver
  4. * Copyright(c) 2013 - 2017 Intel Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * The full GNU General Public License is included in this distribution in
  19. * the file called "COPYING".
  20. *
  21. * Contact Information:
  22. * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
  23. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  24. *
  25. ******************************************************************************/
  26. #include <linux/etherdevice.h>
  27. #include <linux/of_net.h>
  28. #include <linux/pci.h>
  29. #include <linux/bpf.h>
  30. /* Local includes */
  31. #include "i40e.h"
  32. #include "i40e_diag.h"
  33. #include <net/udp_tunnel.h>
  34. /* All i40e tracepoints are defined by the include below, which
  35. * must be included exactly once across the whole kernel with
  36. * CREATE_TRACE_POINTS defined
  37. */
  38. #define CREATE_TRACE_POINTS
  39. #include "i40e_trace.h"
  40. const char i40e_driver_name[] = "i40e";
  41. static const char i40e_driver_string[] =
  42. "Intel(R) Ethernet Connection XL710 Network Driver";
  43. #define DRV_KERN "-k"
  44. #define DRV_VERSION_MAJOR 2
  45. #define DRV_VERSION_MINOR 1
  46. #define DRV_VERSION_BUILD 14
  47. #define DRV_VERSION __stringify(DRV_VERSION_MAJOR) "." \
  48. __stringify(DRV_VERSION_MINOR) "." \
  49. __stringify(DRV_VERSION_BUILD) DRV_KERN
  50. const char i40e_driver_version_str[] = DRV_VERSION;
  51. static const char i40e_copyright[] = "Copyright (c) 2013 - 2014 Intel Corporation.";
  52. /* a bit of forward declarations */
  53. static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi);
  54. static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired);
  55. static int i40e_add_vsi(struct i40e_vsi *vsi);
  56. static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi);
  57. static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit);
  58. static int i40e_setup_misc_vector(struct i40e_pf *pf);
  59. static void i40e_determine_queue_usage(struct i40e_pf *pf);
  60. static int i40e_setup_pf_filter_control(struct i40e_pf *pf);
  61. static void i40e_prep_for_reset(struct i40e_pf *pf, bool lock_acquired);
  62. static int i40e_reset(struct i40e_pf *pf);
  63. static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired);
  64. static void i40e_fdir_sb_setup(struct i40e_pf *pf);
  65. static int i40e_veb_get_bw_info(struct i40e_veb *veb);
  66. /* i40e_pci_tbl - PCI Device ID Table
  67. *
  68. * Last entry must be all 0s
  69. *
  70. * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
  71. * Class, Class Mask, private data (not used) }
  72. */
  73. static const struct pci_device_id i40e_pci_tbl[] = {
  74. {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_XL710), 0},
  75. {PCI_VDEVICE(INTEL, I40E_DEV_ID_QEMU), 0},
  76. {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_B), 0},
  77. {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_C), 0},
  78. {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_A), 0},
  79. {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_B), 0},
  80. {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_C), 0},
  81. {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T), 0},
  82. {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T4), 0},
  83. {PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_X722), 0},
  84. {PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_X722), 0},
  85. {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_X722), 0},
  86. {PCI_VDEVICE(INTEL, I40E_DEV_ID_1G_BASE_T_X722), 0},
  87. {PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_X722), 0},
  88. {PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_I_X722), 0},
  89. {PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2), 0},
  90. {PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2_A), 0},
  91. {PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_B), 0},
  92. {PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_SFP28), 0},
  93. /* required last entry */
  94. {0, }
  95. };
  96. MODULE_DEVICE_TABLE(pci, i40e_pci_tbl);
  97. #define I40E_MAX_VF_COUNT 128
  98. static int debug = -1;
  99. module_param(debug, uint, 0);
  100. MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all), Debug mask (0x8XXXXXXX)");
  101. MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
  102. MODULE_DESCRIPTION("Intel(R) Ethernet Connection XL710 Network Driver");
  103. MODULE_LICENSE("GPL");
  104. MODULE_VERSION(DRV_VERSION);
  105. static struct workqueue_struct *i40e_wq;
  106. /**
  107. * i40e_allocate_dma_mem_d - OS specific memory alloc for shared code
  108. * @hw: pointer to the HW structure
  109. * @mem: ptr to mem struct to fill out
  110. * @size: size of memory requested
  111. * @alignment: what to align the allocation to
  112. **/
  113. int i40e_allocate_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem,
  114. u64 size, u32 alignment)
  115. {
  116. struct i40e_pf *pf = (struct i40e_pf *)hw->back;
  117. mem->size = ALIGN(size, alignment);
  118. mem->va = dma_zalloc_coherent(&pf->pdev->dev, mem->size,
  119. &mem->pa, GFP_KERNEL);
  120. if (!mem->va)
  121. return -ENOMEM;
  122. return 0;
  123. }
  124. /**
  125. * i40e_free_dma_mem_d - OS specific memory free for shared code
  126. * @hw: pointer to the HW structure
  127. * @mem: ptr to mem struct to free
  128. **/
  129. int i40e_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
  130. {
  131. struct i40e_pf *pf = (struct i40e_pf *)hw->back;
  132. dma_free_coherent(&pf->pdev->dev, mem->size, mem->va, mem->pa);
  133. mem->va = NULL;
  134. mem->pa = 0;
  135. mem->size = 0;
  136. return 0;
  137. }
  138. /**
  139. * i40e_allocate_virt_mem_d - OS specific memory alloc for shared code
  140. * @hw: pointer to the HW structure
  141. * @mem: ptr to mem struct to fill out
  142. * @size: size of memory requested
  143. **/
  144. int i40e_allocate_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem,
  145. u32 size)
  146. {
  147. mem->size = size;
  148. mem->va = kzalloc(size, GFP_KERNEL);
  149. if (!mem->va)
  150. return -ENOMEM;
  151. return 0;
  152. }
  153. /**
  154. * i40e_free_virt_mem_d - OS specific memory free for shared code
  155. * @hw: pointer to the HW structure
  156. * @mem: ptr to mem struct to free
  157. **/
  158. int i40e_free_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem)
  159. {
  160. /* it's ok to kfree a NULL pointer */
  161. kfree(mem->va);
  162. mem->va = NULL;
  163. mem->size = 0;
  164. return 0;
  165. }
  166. /**
  167. * i40e_get_lump - find a lump of free generic resource
  168. * @pf: board private structure
  169. * @pile: the pile of resource to search
  170. * @needed: the number of items needed
  171. * @id: an owner id to stick on the items assigned
  172. *
  173. * Returns the base item index of the lump, or negative for error
  174. *
  175. * The search_hint trick and lack of advanced fit-finding only work
  176. * because we're highly likely to have all the same size lump requests.
  177. * Linear search time and any fragmentation should be minimal.
  178. **/
  179. static int i40e_get_lump(struct i40e_pf *pf, struct i40e_lump_tracking *pile,
  180. u16 needed, u16 id)
  181. {
  182. int ret = -ENOMEM;
  183. int i, j;
  184. if (!pile || needed == 0 || id >= I40E_PILE_VALID_BIT) {
  185. dev_info(&pf->pdev->dev,
  186. "param err: pile=%p needed=%d id=0x%04x\n",
  187. pile, needed, id);
  188. return -EINVAL;
  189. }
  190. /* start the linear search with an imperfect hint */
  191. i = pile->search_hint;
  192. while (i < pile->num_entries) {
  193. /* skip already allocated entries */
  194. if (pile->list[i] & I40E_PILE_VALID_BIT) {
  195. i++;
  196. continue;
  197. }
  198. /* do we have enough in this lump? */
  199. for (j = 0; (j < needed) && ((i+j) < pile->num_entries); j++) {
  200. if (pile->list[i+j] & I40E_PILE_VALID_BIT)
  201. break;
  202. }
  203. if (j == needed) {
  204. /* there was enough, so assign it to the requestor */
  205. for (j = 0; j < needed; j++)
  206. pile->list[i+j] = id | I40E_PILE_VALID_BIT;
  207. ret = i;
  208. pile->search_hint = i + j;
  209. break;
  210. }
  211. /* not enough, so skip over it and continue looking */
  212. i += j;
  213. }
  214. return ret;
  215. }
  216. /**
  217. * i40e_put_lump - return a lump of generic resource
  218. * @pile: the pile of resource to search
  219. * @index: the base item index
  220. * @id: the owner id of the items assigned
  221. *
  222. * Returns the count of items in the lump
  223. **/
  224. static int i40e_put_lump(struct i40e_lump_tracking *pile, u16 index, u16 id)
  225. {
  226. int valid_id = (id | I40E_PILE_VALID_BIT);
  227. int count = 0;
  228. int i;
  229. if (!pile || index >= pile->num_entries)
  230. return -EINVAL;
  231. for (i = index;
  232. i < pile->num_entries && pile->list[i] == valid_id;
  233. i++) {
  234. pile->list[i] = 0;
  235. count++;
  236. }
  237. if (count && index < pile->search_hint)
  238. pile->search_hint = index;
  239. return count;
  240. }
  241. /**
  242. * i40e_find_vsi_from_id - searches for the vsi with the given id
  243. * @pf - the pf structure to search for the vsi
  244. * @id - id of the vsi it is searching for
  245. **/
  246. struct i40e_vsi *i40e_find_vsi_from_id(struct i40e_pf *pf, u16 id)
  247. {
  248. int i;
  249. for (i = 0; i < pf->num_alloc_vsi; i++)
  250. if (pf->vsi[i] && (pf->vsi[i]->id == id))
  251. return pf->vsi[i];
  252. return NULL;
  253. }
  254. /**
  255. * i40e_service_event_schedule - Schedule the service task to wake up
  256. * @pf: board private structure
  257. *
  258. * If not already scheduled, this puts the task into the work queue
  259. **/
  260. void i40e_service_event_schedule(struct i40e_pf *pf)
  261. {
  262. if (!test_bit(__I40E_DOWN, pf->state) &&
  263. !test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
  264. queue_work(i40e_wq, &pf->service_task);
  265. }
  266. /**
  267. * i40e_tx_timeout - Respond to a Tx Hang
  268. * @netdev: network interface device structure
  269. *
  270. * If any port has noticed a Tx timeout, it is likely that the whole
  271. * device is munged, not just the one netdev port, so go for the full
  272. * reset.
  273. **/
  274. static void i40e_tx_timeout(struct net_device *netdev)
  275. {
  276. struct i40e_netdev_priv *np = netdev_priv(netdev);
  277. struct i40e_vsi *vsi = np->vsi;
  278. struct i40e_pf *pf = vsi->back;
  279. struct i40e_ring *tx_ring = NULL;
  280. unsigned int i, hung_queue = 0;
  281. u32 head, val;
  282. pf->tx_timeout_count++;
  283. /* find the stopped queue the same way the stack does */
  284. for (i = 0; i < netdev->num_tx_queues; i++) {
  285. struct netdev_queue *q;
  286. unsigned long trans_start;
  287. q = netdev_get_tx_queue(netdev, i);
  288. trans_start = q->trans_start;
  289. if (netif_xmit_stopped(q) &&
  290. time_after(jiffies,
  291. (trans_start + netdev->watchdog_timeo))) {
  292. hung_queue = i;
  293. break;
  294. }
  295. }
  296. if (i == netdev->num_tx_queues) {
  297. netdev_info(netdev, "tx_timeout: no netdev hung queue found\n");
  298. } else {
  299. /* now that we have an index, find the tx_ring struct */
  300. for (i = 0; i < vsi->num_queue_pairs; i++) {
  301. if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
  302. if (hung_queue ==
  303. vsi->tx_rings[i]->queue_index) {
  304. tx_ring = vsi->tx_rings[i];
  305. break;
  306. }
  307. }
  308. }
  309. }
  310. if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ*20)))
  311. pf->tx_timeout_recovery_level = 1; /* reset after some time */
  312. else if (time_before(jiffies,
  313. (pf->tx_timeout_last_recovery + netdev->watchdog_timeo)))
  314. return; /* don't do any new action before the next timeout */
  315. if (tx_ring) {
  316. head = i40e_get_head(tx_ring);
  317. /* Read interrupt register */
  318. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  319. val = rd32(&pf->hw,
  320. I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
  321. tx_ring->vsi->base_vector - 1));
  322. else
  323. val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
  324. 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",
  325. vsi->seid, hung_queue, tx_ring->next_to_clean,
  326. head, tx_ring->next_to_use,
  327. readl(tx_ring->tail), val);
  328. }
  329. pf->tx_timeout_last_recovery = jiffies;
  330. netdev_info(netdev, "tx_timeout recovery level %d, hung_queue %d\n",
  331. pf->tx_timeout_recovery_level, hung_queue);
  332. switch (pf->tx_timeout_recovery_level) {
  333. case 1:
  334. set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
  335. break;
  336. case 2:
  337. set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
  338. break;
  339. case 3:
  340. set_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
  341. break;
  342. default:
  343. netdev_err(netdev, "tx_timeout recovery unsuccessful\n");
  344. break;
  345. }
  346. i40e_service_event_schedule(pf);
  347. pf->tx_timeout_recovery_level++;
  348. }
  349. /**
  350. * i40e_get_vsi_stats_struct - Get System Network Statistics
  351. * @vsi: the VSI we care about
  352. *
  353. * Returns the address of the device statistics structure.
  354. * The statistics are actually updated from the service task.
  355. **/
  356. struct rtnl_link_stats64 *i40e_get_vsi_stats_struct(struct i40e_vsi *vsi)
  357. {
  358. return &vsi->net_stats;
  359. }
  360. /**
  361. * i40e_get_netdev_stats_struct_tx - populate stats from a Tx ring
  362. * @ring: Tx ring to get statistics from
  363. * @stats: statistics entry to be updated
  364. **/
  365. static void i40e_get_netdev_stats_struct_tx(struct i40e_ring *ring,
  366. struct rtnl_link_stats64 *stats)
  367. {
  368. u64 bytes, packets;
  369. unsigned int start;
  370. do {
  371. start = u64_stats_fetch_begin_irq(&ring->syncp);
  372. packets = ring->stats.packets;
  373. bytes = ring->stats.bytes;
  374. } while (u64_stats_fetch_retry_irq(&ring->syncp, start));
  375. stats->tx_packets += packets;
  376. stats->tx_bytes += bytes;
  377. }
  378. /**
  379. * i40e_get_netdev_stats_struct - Get statistics for netdev interface
  380. * @netdev: network interface device structure
  381. *
  382. * Returns the address of the device statistics structure.
  383. * The statistics are actually updated from the service task.
  384. **/
  385. static void i40e_get_netdev_stats_struct(struct net_device *netdev,
  386. struct rtnl_link_stats64 *stats)
  387. {
  388. struct i40e_netdev_priv *np = netdev_priv(netdev);
  389. struct i40e_ring *tx_ring, *rx_ring;
  390. struct i40e_vsi *vsi = np->vsi;
  391. struct rtnl_link_stats64 *vsi_stats = i40e_get_vsi_stats_struct(vsi);
  392. int i;
  393. if (test_bit(__I40E_VSI_DOWN, vsi->state))
  394. return;
  395. if (!vsi->tx_rings)
  396. return;
  397. rcu_read_lock();
  398. for (i = 0; i < vsi->num_queue_pairs; i++) {
  399. u64 bytes, packets;
  400. unsigned int start;
  401. tx_ring = ACCESS_ONCE(vsi->tx_rings[i]);
  402. if (!tx_ring)
  403. continue;
  404. i40e_get_netdev_stats_struct_tx(tx_ring, stats);
  405. rx_ring = &tx_ring[1];
  406. do {
  407. start = u64_stats_fetch_begin_irq(&rx_ring->syncp);
  408. packets = rx_ring->stats.packets;
  409. bytes = rx_ring->stats.bytes;
  410. } while (u64_stats_fetch_retry_irq(&rx_ring->syncp, start));
  411. stats->rx_packets += packets;
  412. stats->rx_bytes += bytes;
  413. if (i40e_enabled_xdp_vsi(vsi))
  414. i40e_get_netdev_stats_struct_tx(&rx_ring[1], stats);
  415. }
  416. rcu_read_unlock();
  417. /* following stats updated by i40e_watchdog_subtask() */
  418. stats->multicast = vsi_stats->multicast;
  419. stats->tx_errors = vsi_stats->tx_errors;
  420. stats->tx_dropped = vsi_stats->tx_dropped;
  421. stats->rx_errors = vsi_stats->rx_errors;
  422. stats->rx_dropped = vsi_stats->rx_dropped;
  423. stats->rx_crc_errors = vsi_stats->rx_crc_errors;
  424. stats->rx_length_errors = vsi_stats->rx_length_errors;
  425. }
  426. /**
  427. * i40e_vsi_reset_stats - Resets all stats of the given vsi
  428. * @vsi: the VSI to have its stats reset
  429. **/
  430. void i40e_vsi_reset_stats(struct i40e_vsi *vsi)
  431. {
  432. struct rtnl_link_stats64 *ns;
  433. int i;
  434. if (!vsi)
  435. return;
  436. ns = i40e_get_vsi_stats_struct(vsi);
  437. memset(ns, 0, sizeof(*ns));
  438. memset(&vsi->net_stats_offsets, 0, sizeof(vsi->net_stats_offsets));
  439. memset(&vsi->eth_stats, 0, sizeof(vsi->eth_stats));
  440. memset(&vsi->eth_stats_offsets, 0, sizeof(vsi->eth_stats_offsets));
  441. if (vsi->rx_rings && vsi->rx_rings[0]) {
  442. for (i = 0; i < vsi->num_queue_pairs; i++) {
  443. memset(&vsi->rx_rings[i]->stats, 0,
  444. sizeof(vsi->rx_rings[i]->stats));
  445. memset(&vsi->rx_rings[i]->rx_stats, 0,
  446. sizeof(vsi->rx_rings[i]->rx_stats));
  447. memset(&vsi->tx_rings[i]->stats, 0,
  448. sizeof(vsi->tx_rings[i]->stats));
  449. memset(&vsi->tx_rings[i]->tx_stats, 0,
  450. sizeof(vsi->tx_rings[i]->tx_stats));
  451. }
  452. }
  453. vsi->stat_offsets_loaded = false;
  454. }
  455. /**
  456. * i40e_pf_reset_stats - Reset all of the stats for the given PF
  457. * @pf: the PF to be reset
  458. **/
  459. void i40e_pf_reset_stats(struct i40e_pf *pf)
  460. {
  461. int i;
  462. memset(&pf->stats, 0, sizeof(pf->stats));
  463. memset(&pf->stats_offsets, 0, sizeof(pf->stats_offsets));
  464. pf->stat_offsets_loaded = false;
  465. for (i = 0; i < I40E_MAX_VEB; i++) {
  466. if (pf->veb[i]) {
  467. memset(&pf->veb[i]->stats, 0,
  468. sizeof(pf->veb[i]->stats));
  469. memset(&pf->veb[i]->stats_offsets, 0,
  470. sizeof(pf->veb[i]->stats_offsets));
  471. pf->veb[i]->stat_offsets_loaded = false;
  472. }
  473. }
  474. pf->hw_csum_rx_error = 0;
  475. }
  476. /**
  477. * i40e_stat_update48 - read and update a 48 bit stat from the chip
  478. * @hw: ptr to the hardware info
  479. * @hireg: the high 32 bit reg to read
  480. * @loreg: the low 32 bit reg to read
  481. * @offset_loaded: has the initial offset been loaded yet
  482. * @offset: ptr to current offset value
  483. * @stat: ptr to the stat
  484. *
  485. * Since the device stats are not reset at PFReset, they likely will not
  486. * be zeroed when the driver starts. We'll save the first values read
  487. * and use them as offsets to be subtracted from the raw values in order
  488. * to report stats that count from zero. In the process, we also manage
  489. * the potential roll-over.
  490. **/
  491. static void i40e_stat_update48(struct i40e_hw *hw, u32 hireg, u32 loreg,
  492. bool offset_loaded, u64 *offset, u64 *stat)
  493. {
  494. u64 new_data;
  495. if (hw->device_id == I40E_DEV_ID_QEMU) {
  496. new_data = rd32(hw, loreg);
  497. new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
  498. } else {
  499. new_data = rd64(hw, loreg);
  500. }
  501. if (!offset_loaded)
  502. *offset = new_data;
  503. if (likely(new_data >= *offset))
  504. *stat = new_data - *offset;
  505. else
  506. *stat = (new_data + BIT_ULL(48)) - *offset;
  507. *stat &= 0xFFFFFFFFFFFFULL;
  508. }
  509. /**
  510. * i40e_stat_update32 - read and update a 32 bit stat from the chip
  511. * @hw: ptr to the hardware info
  512. * @reg: the hw reg to read
  513. * @offset_loaded: has the initial offset been loaded yet
  514. * @offset: ptr to current offset value
  515. * @stat: ptr to the stat
  516. **/
  517. static void i40e_stat_update32(struct i40e_hw *hw, u32 reg,
  518. bool offset_loaded, u64 *offset, u64 *stat)
  519. {
  520. u32 new_data;
  521. new_data = rd32(hw, reg);
  522. if (!offset_loaded)
  523. *offset = new_data;
  524. if (likely(new_data >= *offset))
  525. *stat = (u32)(new_data - *offset);
  526. else
  527. *stat = (u32)((new_data + BIT_ULL(32)) - *offset);
  528. }
  529. /**
  530. * i40e_update_eth_stats - Update VSI-specific ethernet statistics counters.
  531. * @vsi: the VSI to be updated
  532. **/
  533. void i40e_update_eth_stats(struct i40e_vsi *vsi)
  534. {
  535. int stat_idx = le16_to_cpu(vsi->info.stat_counter_idx);
  536. struct i40e_pf *pf = vsi->back;
  537. struct i40e_hw *hw = &pf->hw;
  538. struct i40e_eth_stats *oes;
  539. struct i40e_eth_stats *es; /* device's eth stats */
  540. es = &vsi->eth_stats;
  541. oes = &vsi->eth_stats_offsets;
  542. /* Gather up the stats that the hw collects */
  543. i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
  544. vsi->stat_offsets_loaded,
  545. &oes->tx_errors, &es->tx_errors);
  546. i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx),
  547. vsi->stat_offsets_loaded,
  548. &oes->rx_discards, &es->rx_discards);
  549. i40e_stat_update32(hw, I40E_GLV_RUPP(stat_idx),
  550. vsi->stat_offsets_loaded,
  551. &oes->rx_unknown_protocol, &es->rx_unknown_protocol);
  552. i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
  553. vsi->stat_offsets_loaded,
  554. &oes->tx_errors, &es->tx_errors);
  555. i40e_stat_update48(hw, I40E_GLV_GORCH(stat_idx),
  556. I40E_GLV_GORCL(stat_idx),
  557. vsi->stat_offsets_loaded,
  558. &oes->rx_bytes, &es->rx_bytes);
  559. i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
  560. I40E_GLV_UPRCL(stat_idx),
  561. vsi->stat_offsets_loaded,
  562. &oes->rx_unicast, &es->rx_unicast);
  563. i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
  564. I40E_GLV_MPRCL(stat_idx),
  565. vsi->stat_offsets_loaded,
  566. &oes->rx_multicast, &es->rx_multicast);
  567. i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
  568. I40E_GLV_BPRCL(stat_idx),
  569. vsi->stat_offsets_loaded,
  570. &oes->rx_broadcast, &es->rx_broadcast);
  571. i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
  572. I40E_GLV_GOTCL(stat_idx),
  573. vsi->stat_offsets_loaded,
  574. &oes->tx_bytes, &es->tx_bytes);
  575. i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
  576. I40E_GLV_UPTCL(stat_idx),
  577. vsi->stat_offsets_loaded,
  578. &oes->tx_unicast, &es->tx_unicast);
  579. i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
  580. I40E_GLV_MPTCL(stat_idx),
  581. vsi->stat_offsets_loaded,
  582. &oes->tx_multicast, &es->tx_multicast);
  583. i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
  584. I40E_GLV_BPTCL(stat_idx),
  585. vsi->stat_offsets_loaded,
  586. &oes->tx_broadcast, &es->tx_broadcast);
  587. vsi->stat_offsets_loaded = true;
  588. }
  589. /**
  590. * i40e_update_veb_stats - Update Switch component statistics
  591. * @veb: the VEB being updated
  592. **/
  593. static void i40e_update_veb_stats(struct i40e_veb *veb)
  594. {
  595. struct i40e_pf *pf = veb->pf;
  596. struct i40e_hw *hw = &pf->hw;
  597. struct i40e_eth_stats *oes;
  598. struct i40e_eth_stats *es; /* device's eth stats */
  599. struct i40e_veb_tc_stats *veb_oes;
  600. struct i40e_veb_tc_stats *veb_es;
  601. int i, idx = 0;
  602. idx = veb->stats_idx;
  603. es = &veb->stats;
  604. oes = &veb->stats_offsets;
  605. veb_es = &veb->tc_stats;
  606. veb_oes = &veb->tc_stats_offsets;
  607. /* Gather up the stats that the hw collects */
  608. i40e_stat_update32(hw, I40E_GLSW_TDPC(idx),
  609. veb->stat_offsets_loaded,
  610. &oes->tx_discards, &es->tx_discards);
  611. if (hw->revision_id > 0)
  612. i40e_stat_update32(hw, I40E_GLSW_RUPP(idx),
  613. veb->stat_offsets_loaded,
  614. &oes->rx_unknown_protocol,
  615. &es->rx_unknown_protocol);
  616. i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx),
  617. veb->stat_offsets_loaded,
  618. &oes->rx_bytes, &es->rx_bytes);
  619. i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx),
  620. veb->stat_offsets_loaded,
  621. &oes->rx_unicast, &es->rx_unicast);
  622. i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx),
  623. veb->stat_offsets_loaded,
  624. &oes->rx_multicast, &es->rx_multicast);
  625. i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx),
  626. veb->stat_offsets_loaded,
  627. &oes->rx_broadcast, &es->rx_broadcast);
  628. i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx),
  629. veb->stat_offsets_loaded,
  630. &oes->tx_bytes, &es->tx_bytes);
  631. i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx),
  632. veb->stat_offsets_loaded,
  633. &oes->tx_unicast, &es->tx_unicast);
  634. i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx),
  635. veb->stat_offsets_loaded,
  636. &oes->tx_multicast, &es->tx_multicast);
  637. i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx),
  638. veb->stat_offsets_loaded,
  639. &oes->tx_broadcast, &es->tx_broadcast);
  640. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  641. i40e_stat_update48(hw, I40E_GLVEBTC_RPCH(i, idx),
  642. I40E_GLVEBTC_RPCL(i, idx),
  643. veb->stat_offsets_loaded,
  644. &veb_oes->tc_rx_packets[i],
  645. &veb_es->tc_rx_packets[i]);
  646. i40e_stat_update48(hw, I40E_GLVEBTC_RBCH(i, idx),
  647. I40E_GLVEBTC_RBCL(i, idx),
  648. veb->stat_offsets_loaded,
  649. &veb_oes->tc_rx_bytes[i],
  650. &veb_es->tc_rx_bytes[i]);
  651. i40e_stat_update48(hw, I40E_GLVEBTC_TPCH(i, idx),
  652. I40E_GLVEBTC_TPCL(i, idx),
  653. veb->stat_offsets_loaded,
  654. &veb_oes->tc_tx_packets[i],
  655. &veb_es->tc_tx_packets[i]);
  656. i40e_stat_update48(hw, I40E_GLVEBTC_TBCH(i, idx),
  657. I40E_GLVEBTC_TBCL(i, idx),
  658. veb->stat_offsets_loaded,
  659. &veb_oes->tc_tx_bytes[i],
  660. &veb_es->tc_tx_bytes[i]);
  661. }
  662. veb->stat_offsets_loaded = true;
  663. }
  664. /**
  665. * i40e_update_vsi_stats - Update the vsi statistics counters.
  666. * @vsi: the VSI to be updated
  667. *
  668. * There are a few instances where we store the same stat in a
  669. * couple of different structs. This is partly because we have
  670. * the netdev stats that need to be filled out, which is slightly
  671. * different from the "eth_stats" defined by the chip and used in
  672. * VF communications. We sort it out here.
  673. **/
  674. static void i40e_update_vsi_stats(struct i40e_vsi *vsi)
  675. {
  676. struct i40e_pf *pf = vsi->back;
  677. struct rtnl_link_stats64 *ons;
  678. struct rtnl_link_stats64 *ns; /* netdev stats */
  679. struct i40e_eth_stats *oes;
  680. struct i40e_eth_stats *es; /* device's eth stats */
  681. u32 tx_restart, tx_busy;
  682. struct i40e_ring *p;
  683. u32 rx_page, rx_buf;
  684. u64 bytes, packets;
  685. unsigned int start;
  686. u64 tx_linearize;
  687. u64 tx_force_wb;
  688. u64 rx_p, rx_b;
  689. u64 tx_p, tx_b;
  690. u16 q;
  691. if (test_bit(__I40E_VSI_DOWN, vsi->state) ||
  692. test_bit(__I40E_CONFIG_BUSY, pf->state))
  693. return;
  694. ns = i40e_get_vsi_stats_struct(vsi);
  695. ons = &vsi->net_stats_offsets;
  696. es = &vsi->eth_stats;
  697. oes = &vsi->eth_stats_offsets;
  698. /* Gather up the netdev and vsi stats that the driver collects
  699. * on the fly during packet processing
  700. */
  701. rx_b = rx_p = 0;
  702. tx_b = tx_p = 0;
  703. tx_restart = tx_busy = tx_linearize = tx_force_wb = 0;
  704. rx_page = 0;
  705. rx_buf = 0;
  706. rcu_read_lock();
  707. for (q = 0; q < vsi->num_queue_pairs; q++) {
  708. /* locate Tx ring */
  709. p = ACCESS_ONCE(vsi->tx_rings[q]);
  710. do {
  711. start = u64_stats_fetch_begin_irq(&p->syncp);
  712. packets = p->stats.packets;
  713. bytes = p->stats.bytes;
  714. } while (u64_stats_fetch_retry_irq(&p->syncp, start));
  715. tx_b += bytes;
  716. tx_p += packets;
  717. tx_restart += p->tx_stats.restart_queue;
  718. tx_busy += p->tx_stats.tx_busy;
  719. tx_linearize += p->tx_stats.tx_linearize;
  720. tx_force_wb += p->tx_stats.tx_force_wb;
  721. /* Rx queue is part of the same block as Tx queue */
  722. p = &p[1];
  723. do {
  724. start = u64_stats_fetch_begin_irq(&p->syncp);
  725. packets = p->stats.packets;
  726. bytes = p->stats.bytes;
  727. } while (u64_stats_fetch_retry_irq(&p->syncp, start));
  728. rx_b += bytes;
  729. rx_p += packets;
  730. rx_buf += p->rx_stats.alloc_buff_failed;
  731. rx_page += p->rx_stats.alloc_page_failed;
  732. }
  733. rcu_read_unlock();
  734. vsi->tx_restart = tx_restart;
  735. vsi->tx_busy = tx_busy;
  736. vsi->tx_linearize = tx_linearize;
  737. vsi->tx_force_wb = tx_force_wb;
  738. vsi->rx_page_failed = rx_page;
  739. vsi->rx_buf_failed = rx_buf;
  740. ns->rx_packets = rx_p;
  741. ns->rx_bytes = rx_b;
  742. ns->tx_packets = tx_p;
  743. ns->tx_bytes = tx_b;
  744. /* update netdev stats from eth stats */
  745. i40e_update_eth_stats(vsi);
  746. ons->tx_errors = oes->tx_errors;
  747. ns->tx_errors = es->tx_errors;
  748. ons->multicast = oes->rx_multicast;
  749. ns->multicast = es->rx_multicast;
  750. ons->rx_dropped = oes->rx_discards;
  751. ns->rx_dropped = es->rx_discards;
  752. ons->tx_dropped = oes->tx_discards;
  753. ns->tx_dropped = es->tx_discards;
  754. /* pull in a couple PF stats if this is the main vsi */
  755. if (vsi == pf->vsi[pf->lan_vsi]) {
  756. ns->rx_crc_errors = pf->stats.crc_errors;
  757. ns->rx_errors = pf->stats.crc_errors + pf->stats.illegal_bytes;
  758. ns->rx_length_errors = pf->stats.rx_length_errors;
  759. }
  760. }
  761. /**
  762. * i40e_update_pf_stats - Update the PF statistics counters.
  763. * @pf: the PF to be updated
  764. **/
  765. static void i40e_update_pf_stats(struct i40e_pf *pf)
  766. {
  767. struct i40e_hw_port_stats *osd = &pf->stats_offsets;
  768. struct i40e_hw_port_stats *nsd = &pf->stats;
  769. struct i40e_hw *hw = &pf->hw;
  770. u32 val;
  771. int i;
  772. i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
  773. I40E_GLPRT_GORCL(hw->port),
  774. pf->stat_offsets_loaded,
  775. &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
  776. i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
  777. I40E_GLPRT_GOTCL(hw->port),
  778. pf->stat_offsets_loaded,
  779. &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
  780. i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
  781. pf->stat_offsets_loaded,
  782. &osd->eth.rx_discards,
  783. &nsd->eth.rx_discards);
  784. i40e_stat_update48(hw, I40E_GLPRT_UPRCH(hw->port),
  785. I40E_GLPRT_UPRCL(hw->port),
  786. pf->stat_offsets_loaded,
  787. &osd->eth.rx_unicast,
  788. &nsd->eth.rx_unicast);
  789. i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
  790. I40E_GLPRT_MPRCL(hw->port),
  791. pf->stat_offsets_loaded,
  792. &osd->eth.rx_multicast,
  793. &nsd->eth.rx_multicast);
  794. i40e_stat_update48(hw, I40E_GLPRT_BPRCH(hw->port),
  795. I40E_GLPRT_BPRCL(hw->port),
  796. pf->stat_offsets_loaded,
  797. &osd->eth.rx_broadcast,
  798. &nsd->eth.rx_broadcast);
  799. i40e_stat_update48(hw, I40E_GLPRT_UPTCH(hw->port),
  800. I40E_GLPRT_UPTCL(hw->port),
  801. pf->stat_offsets_loaded,
  802. &osd->eth.tx_unicast,
  803. &nsd->eth.tx_unicast);
  804. i40e_stat_update48(hw, I40E_GLPRT_MPTCH(hw->port),
  805. I40E_GLPRT_MPTCL(hw->port),
  806. pf->stat_offsets_loaded,
  807. &osd->eth.tx_multicast,
  808. &nsd->eth.tx_multicast);
  809. i40e_stat_update48(hw, I40E_GLPRT_BPTCH(hw->port),
  810. I40E_GLPRT_BPTCL(hw->port),
  811. pf->stat_offsets_loaded,
  812. &osd->eth.tx_broadcast,
  813. &nsd->eth.tx_broadcast);
  814. i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
  815. pf->stat_offsets_loaded,
  816. &osd->tx_dropped_link_down,
  817. &nsd->tx_dropped_link_down);
  818. i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
  819. pf->stat_offsets_loaded,
  820. &osd->crc_errors, &nsd->crc_errors);
  821. i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
  822. pf->stat_offsets_loaded,
  823. &osd->illegal_bytes, &nsd->illegal_bytes);
  824. i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
  825. pf->stat_offsets_loaded,
  826. &osd->mac_local_faults,
  827. &nsd->mac_local_faults);
  828. i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
  829. pf->stat_offsets_loaded,
  830. &osd->mac_remote_faults,
  831. &nsd->mac_remote_faults);
  832. i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
  833. pf->stat_offsets_loaded,
  834. &osd->rx_length_errors,
  835. &nsd->rx_length_errors);
  836. i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
  837. pf->stat_offsets_loaded,
  838. &osd->link_xon_rx, &nsd->link_xon_rx);
  839. i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
  840. pf->stat_offsets_loaded,
  841. &osd->link_xon_tx, &nsd->link_xon_tx);
  842. i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
  843. pf->stat_offsets_loaded,
  844. &osd->link_xoff_rx, &nsd->link_xoff_rx);
  845. i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
  846. pf->stat_offsets_loaded,
  847. &osd->link_xoff_tx, &nsd->link_xoff_tx);
  848. for (i = 0; i < 8; i++) {
  849. i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
  850. pf->stat_offsets_loaded,
  851. &osd->priority_xoff_rx[i],
  852. &nsd->priority_xoff_rx[i]);
  853. i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
  854. pf->stat_offsets_loaded,
  855. &osd->priority_xon_rx[i],
  856. &nsd->priority_xon_rx[i]);
  857. i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
  858. pf->stat_offsets_loaded,
  859. &osd->priority_xon_tx[i],
  860. &nsd->priority_xon_tx[i]);
  861. i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
  862. pf->stat_offsets_loaded,
  863. &osd->priority_xoff_tx[i],
  864. &nsd->priority_xoff_tx[i]);
  865. i40e_stat_update32(hw,
  866. I40E_GLPRT_RXON2OFFCNT(hw->port, i),
  867. pf->stat_offsets_loaded,
  868. &osd->priority_xon_2_xoff[i],
  869. &nsd->priority_xon_2_xoff[i]);
  870. }
  871. i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
  872. I40E_GLPRT_PRC64L(hw->port),
  873. pf->stat_offsets_loaded,
  874. &osd->rx_size_64, &nsd->rx_size_64);
  875. i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
  876. I40E_GLPRT_PRC127L(hw->port),
  877. pf->stat_offsets_loaded,
  878. &osd->rx_size_127, &nsd->rx_size_127);
  879. i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
  880. I40E_GLPRT_PRC255L(hw->port),
  881. pf->stat_offsets_loaded,
  882. &osd->rx_size_255, &nsd->rx_size_255);
  883. i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
  884. I40E_GLPRT_PRC511L(hw->port),
  885. pf->stat_offsets_loaded,
  886. &osd->rx_size_511, &nsd->rx_size_511);
  887. i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
  888. I40E_GLPRT_PRC1023L(hw->port),
  889. pf->stat_offsets_loaded,
  890. &osd->rx_size_1023, &nsd->rx_size_1023);
  891. i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
  892. I40E_GLPRT_PRC1522L(hw->port),
  893. pf->stat_offsets_loaded,
  894. &osd->rx_size_1522, &nsd->rx_size_1522);
  895. i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
  896. I40E_GLPRT_PRC9522L(hw->port),
  897. pf->stat_offsets_loaded,
  898. &osd->rx_size_big, &nsd->rx_size_big);
  899. i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
  900. I40E_GLPRT_PTC64L(hw->port),
  901. pf->stat_offsets_loaded,
  902. &osd->tx_size_64, &nsd->tx_size_64);
  903. i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
  904. I40E_GLPRT_PTC127L(hw->port),
  905. pf->stat_offsets_loaded,
  906. &osd->tx_size_127, &nsd->tx_size_127);
  907. i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
  908. I40E_GLPRT_PTC255L(hw->port),
  909. pf->stat_offsets_loaded,
  910. &osd->tx_size_255, &nsd->tx_size_255);
  911. i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
  912. I40E_GLPRT_PTC511L(hw->port),
  913. pf->stat_offsets_loaded,
  914. &osd->tx_size_511, &nsd->tx_size_511);
  915. i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
  916. I40E_GLPRT_PTC1023L(hw->port),
  917. pf->stat_offsets_loaded,
  918. &osd->tx_size_1023, &nsd->tx_size_1023);
  919. i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
  920. I40E_GLPRT_PTC1522L(hw->port),
  921. pf->stat_offsets_loaded,
  922. &osd->tx_size_1522, &nsd->tx_size_1522);
  923. i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
  924. I40E_GLPRT_PTC9522L(hw->port),
  925. pf->stat_offsets_loaded,
  926. &osd->tx_size_big, &nsd->tx_size_big);
  927. i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
  928. pf->stat_offsets_loaded,
  929. &osd->rx_undersize, &nsd->rx_undersize);
  930. i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
  931. pf->stat_offsets_loaded,
  932. &osd->rx_fragments, &nsd->rx_fragments);
  933. i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
  934. pf->stat_offsets_loaded,
  935. &osd->rx_oversize, &nsd->rx_oversize);
  936. i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
  937. pf->stat_offsets_loaded,
  938. &osd->rx_jabber, &nsd->rx_jabber);
  939. /* FDIR stats */
  940. i40e_stat_update32(hw,
  941. I40E_GLQF_PCNT(I40E_FD_ATR_STAT_IDX(pf->hw.pf_id)),
  942. pf->stat_offsets_loaded,
  943. &osd->fd_atr_match, &nsd->fd_atr_match);
  944. i40e_stat_update32(hw,
  945. I40E_GLQF_PCNT(I40E_FD_SB_STAT_IDX(pf->hw.pf_id)),
  946. pf->stat_offsets_loaded,
  947. &osd->fd_sb_match, &nsd->fd_sb_match);
  948. i40e_stat_update32(hw,
  949. I40E_GLQF_PCNT(I40E_FD_ATR_TUNNEL_STAT_IDX(pf->hw.pf_id)),
  950. pf->stat_offsets_loaded,
  951. &osd->fd_atr_tunnel_match, &nsd->fd_atr_tunnel_match);
  952. val = rd32(hw, I40E_PRTPM_EEE_STAT);
  953. nsd->tx_lpi_status =
  954. (val & I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_MASK) >>
  955. I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_SHIFT;
  956. nsd->rx_lpi_status =
  957. (val & I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_MASK) >>
  958. I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_SHIFT;
  959. i40e_stat_update32(hw, I40E_PRTPM_TLPIC,
  960. pf->stat_offsets_loaded,
  961. &osd->tx_lpi_count, &nsd->tx_lpi_count);
  962. i40e_stat_update32(hw, I40E_PRTPM_RLPIC,
  963. pf->stat_offsets_loaded,
  964. &osd->rx_lpi_count, &nsd->rx_lpi_count);
  965. if (pf->flags & I40E_FLAG_FD_SB_ENABLED &&
  966. !(pf->flags & I40E_FLAG_FD_SB_AUTO_DISABLED))
  967. nsd->fd_sb_status = true;
  968. else
  969. nsd->fd_sb_status = false;
  970. if (pf->flags & I40E_FLAG_FD_ATR_ENABLED &&
  971. !(pf->flags & I40E_FLAG_FD_ATR_AUTO_DISABLED))
  972. nsd->fd_atr_status = true;
  973. else
  974. nsd->fd_atr_status = false;
  975. pf->stat_offsets_loaded = true;
  976. }
  977. /**
  978. * i40e_update_stats - Update the various statistics counters.
  979. * @vsi: the VSI to be updated
  980. *
  981. * Update the various stats for this VSI and its related entities.
  982. **/
  983. void i40e_update_stats(struct i40e_vsi *vsi)
  984. {
  985. struct i40e_pf *pf = vsi->back;
  986. if (vsi == pf->vsi[pf->lan_vsi])
  987. i40e_update_pf_stats(pf);
  988. i40e_update_vsi_stats(vsi);
  989. }
  990. /**
  991. * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
  992. * @vsi: the VSI to be searched
  993. * @macaddr: the MAC address
  994. * @vlan: the vlan
  995. *
  996. * Returns ptr to the filter object or NULL
  997. **/
  998. static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
  999. const u8 *macaddr, s16 vlan)
  1000. {
  1001. struct i40e_mac_filter *f;
  1002. u64 key;
  1003. if (!vsi || !macaddr)
  1004. return NULL;
  1005. key = i40e_addr_to_hkey(macaddr);
  1006. hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
  1007. if ((ether_addr_equal(macaddr, f->macaddr)) &&
  1008. (vlan == f->vlan))
  1009. return f;
  1010. }
  1011. return NULL;
  1012. }
  1013. /**
  1014. * i40e_find_mac - Find a mac addr in the macvlan filters list
  1015. * @vsi: the VSI to be searched
  1016. * @macaddr: the MAC address we are searching for
  1017. *
  1018. * Returns the first filter with the provided MAC address or NULL if
  1019. * MAC address was not found
  1020. **/
  1021. struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, const u8 *macaddr)
  1022. {
  1023. struct i40e_mac_filter *f;
  1024. u64 key;
  1025. if (!vsi || !macaddr)
  1026. return NULL;
  1027. key = i40e_addr_to_hkey(macaddr);
  1028. hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
  1029. if ((ether_addr_equal(macaddr, f->macaddr)))
  1030. return f;
  1031. }
  1032. return NULL;
  1033. }
  1034. /**
  1035. * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
  1036. * @vsi: the VSI to be searched
  1037. *
  1038. * Returns true if VSI is in vlan mode or false otherwise
  1039. **/
  1040. bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
  1041. {
  1042. /* If we have a PVID, always operate in VLAN mode */
  1043. if (vsi->info.pvid)
  1044. return true;
  1045. /* We need to operate in VLAN mode whenever we have any filters with
  1046. * a VLAN other than I40E_VLAN_ALL. We could check the table each
  1047. * time, incurring search cost repeatedly. However, we can notice two
  1048. * things:
  1049. *
  1050. * 1) the only place where we can gain a VLAN filter is in
  1051. * i40e_add_filter.
  1052. *
  1053. * 2) the only place where filters are actually removed is in
  1054. * i40e_sync_filters_subtask.
  1055. *
  1056. * Thus, we can simply use a boolean value, has_vlan_filters which we
  1057. * will set to true when we add a VLAN filter in i40e_add_filter. Then
  1058. * we have to perform the full search after deleting filters in
  1059. * i40e_sync_filters_subtask, but we already have to search
  1060. * filters here and can perform the check at the same time. This
  1061. * results in avoiding embedding a loop for VLAN mode inside another
  1062. * loop over all the filters, and should maintain correctness as noted
  1063. * above.
  1064. */
  1065. return vsi->has_vlan_filter;
  1066. }
  1067. /**
  1068. * i40e_correct_mac_vlan_filters - Correct non-VLAN filters if necessary
  1069. * @vsi: the VSI to configure
  1070. * @tmp_add_list: list of filters ready to be added
  1071. * @tmp_del_list: list of filters ready to be deleted
  1072. * @vlan_filters: the number of active VLAN filters
  1073. *
  1074. * Update VLAN=0 and VLAN=-1 (I40E_VLAN_ANY) filters properly so that they
  1075. * behave as expected. If we have any active VLAN filters remaining or about
  1076. * to be added then we need to update non-VLAN filters to be marked as VLAN=0
  1077. * so that they only match against untagged traffic. If we no longer have any
  1078. * active VLAN filters, we need to make all non-VLAN filters marked as VLAN=-1
  1079. * so that they match against both tagged and untagged traffic. In this way,
  1080. * we ensure that we correctly receive the desired traffic. This ensures that
  1081. * when we have an active VLAN we will receive only untagged traffic and
  1082. * traffic matching active VLANs. If we have no active VLANs then we will
  1083. * operate in non-VLAN mode and receive all traffic, tagged or untagged.
  1084. *
  1085. * Finally, in a similar fashion, this function also corrects filters when
  1086. * there is an active PVID assigned to this VSI.
  1087. *
  1088. * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
  1089. *
  1090. * This function is only expected to be called from within
  1091. * i40e_sync_vsi_filters.
  1092. *
  1093. * NOTE: This function expects to be called while under the
  1094. * mac_filter_hash_lock
  1095. */
  1096. static int i40e_correct_mac_vlan_filters(struct i40e_vsi *vsi,
  1097. struct hlist_head *tmp_add_list,
  1098. struct hlist_head *tmp_del_list,
  1099. int vlan_filters)
  1100. {
  1101. s16 pvid = le16_to_cpu(vsi->info.pvid);
  1102. struct i40e_mac_filter *f, *add_head;
  1103. struct i40e_new_mac_filter *new;
  1104. struct hlist_node *h;
  1105. int bkt, new_vlan;
  1106. /* To determine if a particular filter needs to be replaced we
  1107. * have the three following conditions:
  1108. *
  1109. * a) if we have a PVID assigned, then all filters which are
  1110. * not marked as VLAN=PVID must be replaced with filters that
  1111. * are.
  1112. * b) otherwise, if we have any active VLANS, all filters
  1113. * which are marked as VLAN=-1 must be replaced with
  1114. * filters marked as VLAN=0
  1115. * c) finally, if we do not have any active VLANS, all filters
  1116. * which are marked as VLAN=0 must be replaced with filters
  1117. * marked as VLAN=-1
  1118. */
  1119. /* Update the filters about to be added in place */
  1120. hlist_for_each_entry(new, tmp_add_list, hlist) {
  1121. if (pvid && new->f->vlan != pvid)
  1122. new->f->vlan = pvid;
  1123. else if (vlan_filters && new->f->vlan == I40E_VLAN_ANY)
  1124. new->f->vlan = 0;
  1125. else if (!vlan_filters && new->f->vlan == 0)
  1126. new->f->vlan = I40E_VLAN_ANY;
  1127. }
  1128. /* Update the remaining active filters */
  1129. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  1130. /* Combine the checks for whether a filter needs to be changed
  1131. * and then determine the new VLAN inside the if block, in
  1132. * order to avoid duplicating code for adding the new filter
  1133. * then deleting the old filter.
  1134. */
  1135. if ((pvid && f->vlan != pvid) ||
  1136. (vlan_filters && f->vlan == I40E_VLAN_ANY) ||
  1137. (!vlan_filters && f->vlan == 0)) {
  1138. /* Determine the new vlan we will be adding */
  1139. if (pvid)
  1140. new_vlan = pvid;
  1141. else if (vlan_filters)
  1142. new_vlan = 0;
  1143. else
  1144. new_vlan = I40E_VLAN_ANY;
  1145. /* Create the new filter */
  1146. add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
  1147. if (!add_head)
  1148. return -ENOMEM;
  1149. /* Create a temporary i40e_new_mac_filter */
  1150. new = kzalloc(sizeof(*new), GFP_ATOMIC);
  1151. if (!new)
  1152. return -ENOMEM;
  1153. new->f = add_head;
  1154. new->state = add_head->state;
  1155. /* Add the new filter to the tmp list */
  1156. hlist_add_head(&new->hlist, tmp_add_list);
  1157. /* Put the original filter into the delete list */
  1158. f->state = I40E_FILTER_REMOVE;
  1159. hash_del(&f->hlist);
  1160. hlist_add_head(&f->hlist, tmp_del_list);
  1161. }
  1162. }
  1163. vsi->has_vlan_filter = !!vlan_filters;
  1164. return 0;
  1165. }
  1166. /**
  1167. * i40e_rm_default_mac_filter - Remove the default MAC filter set by NVM
  1168. * @vsi: the PF Main VSI - inappropriate for any other VSI
  1169. * @macaddr: the MAC address
  1170. *
  1171. * Remove whatever filter the firmware set up so the driver can manage
  1172. * its own filtering intelligently.
  1173. **/
  1174. static void i40e_rm_default_mac_filter(struct i40e_vsi *vsi, u8 *macaddr)
  1175. {
  1176. struct i40e_aqc_remove_macvlan_element_data element;
  1177. struct i40e_pf *pf = vsi->back;
  1178. /* Only appropriate for the PF main VSI */
  1179. if (vsi->type != I40E_VSI_MAIN)
  1180. return;
  1181. memset(&element, 0, sizeof(element));
  1182. ether_addr_copy(element.mac_addr, macaddr);
  1183. element.vlan_tag = 0;
  1184. /* Ignore error returns, some firmware does it this way... */
  1185. element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
  1186. i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
  1187. memset(&element, 0, sizeof(element));
  1188. ether_addr_copy(element.mac_addr, macaddr);
  1189. element.vlan_tag = 0;
  1190. /* ...and some firmware does it this way. */
  1191. element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH |
  1192. I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
  1193. i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
  1194. }
  1195. /**
  1196. * i40e_add_filter - Add a mac/vlan filter to the VSI
  1197. * @vsi: the VSI to be searched
  1198. * @macaddr: the MAC address
  1199. * @vlan: the vlan
  1200. *
  1201. * Returns ptr to the filter object or NULL when no memory available.
  1202. *
  1203. * NOTE: This function is expected to be called with mac_filter_hash_lock
  1204. * being held.
  1205. **/
  1206. struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
  1207. const u8 *macaddr, s16 vlan)
  1208. {
  1209. struct i40e_mac_filter *f;
  1210. u64 key;
  1211. if (!vsi || !macaddr)
  1212. return NULL;
  1213. f = i40e_find_filter(vsi, macaddr, vlan);
  1214. if (!f) {
  1215. f = kzalloc(sizeof(*f), GFP_ATOMIC);
  1216. if (!f)
  1217. return NULL;
  1218. /* Update the boolean indicating if we need to function in
  1219. * VLAN mode.
  1220. */
  1221. if (vlan >= 0)
  1222. vsi->has_vlan_filter = true;
  1223. ether_addr_copy(f->macaddr, macaddr);
  1224. f->vlan = vlan;
  1225. /* If we're in overflow promisc mode, set the state directly
  1226. * to failed, so we don't bother to try sending the filter
  1227. * to the hardware.
  1228. */
  1229. if (test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state))
  1230. f->state = I40E_FILTER_FAILED;
  1231. else
  1232. f->state = I40E_FILTER_NEW;
  1233. INIT_HLIST_NODE(&f->hlist);
  1234. key = i40e_addr_to_hkey(macaddr);
  1235. hash_add(vsi->mac_filter_hash, &f->hlist, key);
  1236. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  1237. vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
  1238. }
  1239. /* If we're asked to add a filter that has been marked for removal, it
  1240. * is safe to simply restore it to active state. __i40e_del_filter
  1241. * will have simply deleted any filters which were previously marked
  1242. * NEW or FAILED, so if it is currently marked REMOVE it must have
  1243. * previously been ACTIVE. Since we haven't yet run the sync filters
  1244. * task, just restore this filter to the ACTIVE state so that the
  1245. * sync task leaves it in place
  1246. */
  1247. if (f->state == I40E_FILTER_REMOVE)
  1248. f->state = I40E_FILTER_ACTIVE;
  1249. return f;
  1250. }
  1251. /**
  1252. * __i40e_del_filter - Remove a specific filter from the VSI
  1253. * @vsi: VSI to remove from
  1254. * @f: the filter to remove from the list
  1255. *
  1256. * This function should be called instead of i40e_del_filter only if you know
  1257. * the exact filter you will remove already, such as via i40e_find_filter or
  1258. * i40e_find_mac.
  1259. *
  1260. * NOTE: This function is expected to be called with mac_filter_hash_lock
  1261. * being held.
  1262. * ANOTHER NOTE: This function MUST be called from within the context of
  1263. * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
  1264. * instead of list_for_each_entry().
  1265. **/
  1266. void __i40e_del_filter(struct i40e_vsi *vsi, struct i40e_mac_filter *f)
  1267. {
  1268. if (!f)
  1269. return;
  1270. /* If the filter was never added to firmware then we can just delete it
  1271. * directly and we don't want to set the status to remove or else an
  1272. * admin queue command will unnecessarily fire.
  1273. */
  1274. if ((f->state == I40E_FILTER_FAILED) ||
  1275. (f->state == I40E_FILTER_NEW)) {
  1276. hash_del(&f->hlist);
  1277. kfree(f);
  1278. } else {
  1279. f->state = I40E_FILTER_REMOVE;
  1280. }
  1281. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  1282. vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
  1283. }
  1284. /**
  1285. * i40e_del_filter - Remove a MAC/VLAN filter from the VSI
  1286. * @vsi: the VSI to be searched
  1287. * @macaddr: the MAC address
  1288. * @vlan: the VLAN
  1289. *
  1290. * NOTE: This function is expected to be called with mac_filter_hash_lock
  1291. * being held.
  1292. * ANOTHER NOTE: This function MUST be called from within the context of
  1293. * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
  1294. * instead of list_for_each_entry().
  1295. **/
  1296. void i40e_del_filter(struct i40e_vsi *vsi, const u8 *macaddr, s16 vlan)
  1297. {
  1298. struct i40e_mac_filter *f;
  1299. if (!vsi || !macaddr)
  1300. return;
  1301. f = i40e_find_filter(vsi, macaddr, vlan);
  1302. __i40e_del_filter(vsi, f);
  1303. }
  1304. /**
  1305. * i40e_add_mac_filter - Add a MAC filter for all active VLANs
  1306. * @vsi: the VSI to be searched
  1307. * @macaddr: the mac address to be filtered
  1308. *
  1309. * If we're not in VLAN mode, just add the filter to I40E_VLAN_ANY. Otherwise,
  1310. * go through all the macvlan filters and add a macvlan filter for each
  1311. * unique vlan that already exists. If a PVID has been assigned, instead only
  1312. * add the macaddr to that VLAN.
  1313. *
  1314. * Returns last filter added on success, else NULL
  1315. **/
  1316. struct i40e_mac_filter *i40e_add_mac_filter(struct i40e_vsi *vsi,
  1317. const u8 *macaddr)
  1318. {
  1319. struct i40e_mac_filter *f, *add = NULL;
  1320. struct hlist_node *h;
  1321. int bkt;
  1322. if (vsi->info.pvid)
  1323. return i40e_add_filter(vsi, macaddr,
  1324. le16_to_cpu(vsi->info.pvid));
  1325. if (!i40e_is_vsi_in_vlan(vsi))
  1326. return i40e_add_filter(vsi, macaddr, I40E_VLAN_ANY);
  1327. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  1328. if (f->state == I40E_FILTER_REMOVE)
  1329. continue;
  1330. add = i40e_add_filter(vsi, macaddr, f->vlan);
  1331. if (!add)
  1332. return NULL;
  1333. }
  1334. return add;
  1335. }
  1336. /**
  1337. * i40e_del_mac_filter - Remove a MAC filter from all VLANs
  1338. * @vsi: the VSI to be searched
  1339. * @macaddr: the mac address to be removed
  1340. *
  1341. * Removes a given MAC address from a VSI regardless of what VLAN it has been
  1342. * associated with.
  1343. *
  1344. * Returns 0 for success, or error
  1345. **/
  1346. int i40e_del_mac_filter(struct i40e_vsi *vsi, const u8 *macaddr)
  1347. {
  1348. struct i40e_mac_filter *f;
  1349. struct hlist_node *h;
  1350. bool found = false;
  1351. int bkt;
  1352. WARN(!spin_is_locked(&vsi->mac_filter_hash_lock),
  1353. "Missing mac_filter_hash_lock\n");
  1354. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  1355. if (ether_addr_equal(macaddr, f->macaddr)) {
  1356. __i40e_del_filter(vsi, f);
  1357. found = true;
  1358. }
  1359. }
  1360. if (found)
  1361. return 0;
  1362. else
  1363. return -ENOENT;
  1364. }
  1365. /**
  1366. * i40e_set_mac - NDO callback to set mac address
  1367. * @netdev: network interface device structure
  1368. * @p: pointer to an address structure
  1369. *
  1370. * Returns 0 on success, negative on failure
  1371. **/
  1372. static int i40e_set_mac(struct net_device *netdev, void *p)
  1373. {
  1374. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1375. struct i40e_vsi *vsi = np->vsi;
  1376. struct i40e_pf *pf = vsi->back;
  1377. struct i40e_hw *hw = &pf->hw;
  1378. struct sockaddr *addr = p;
  1379. if (!is_valid_ether_addr(addr->sa_data))
  1380. return -EADDRNOTAVAIL;
  1381. if (ether_addr_equal(netdev->dev_addr, addr->sa_data)) {
  1382. netdev_info(netdev, "already using mac address %pM\n",
  1383. addr->sa_data);
  1384. return 0;
  1385. }
  1386. if (test_bit(__I40E_VSI_DOWN, vsi->back->state) ||
  1387. test_bit(__I40E_RESET_RECOVERY_PENDING, vsi->back->state))
  1388. return -EADDRNOTAVAIL;
  1389. if (ether_addr_equal(hw->mac.addr, addr->sa_data))
  1390. netdev_info(netdev, "returning to hw mac address %pM\n",
  1391. hw->mac.addr);
  1392. else
  1393. netdev_info(netdev, "set new mac address %pM\n", addr->sa_data);
  1394. spin_lock_bh(&vsi->mac_filter_hash_lock);
  1395. i40e_del_mac_filter(vsi, netdev->dev_addr);
  1396. i40e_add_mac_filter(vsi, addr->sa_data);
  1397. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  1398. ether_addr_copy(netdev->dev_addr, addr->sa_data);
  1399. if (vsi->type == I40E_VSI_MAIN) {
  1400. i40e_status ret;
  1401. ret = i40e_aq_mac_address_write(&vsi->back->hw,
  1402. I40E_AQC_WRITE_TYPE_LAA_WOL,
  1403. addr->sa_data, NULL);
  1404. if (ret)
  1405. netdev_info(netdev, "Ignoring error from firmware on LAA update, status %s, AQ ret %s\n",
  1406. i40e_stat_str(hw, ret),
  1407. i40e_aq_str(hw, hw->aq.asq_last_status));
  1408. }
  1409. /* schedule our worker thread which will take care of
  1410. * applying the new filter changes
  1411. */
  1412. i40e_service_event_schedule(vsi->back);
  1413. return 0;
  1414. }
  1415. /**
  1416. * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
  1417. * @vsi: the VSI being setup
  1418. * @ctxt: VSI context structure
  1419. * @enabled_tc: Enabled TCs bitmap
  1420. * @is_add: True if called before Add VSI
  1421. *
  1422. * Setup VSI queue mapping for enabled traffic classes.
  1423. **/
  1424. static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
  1425. struct i40e_vsi_context *ctxt,
  1426. u8 enabled_tc,
  1427. bool is_add)
  1428. {
  1429. struct i40e_pf *pf = vsi->back;
  1430. u16 sections = 0;
  1431. u8 netdev_tc = 0;
  1432. u16 numtc = 0;
  1433. u16 qcount;
  1434. u8 offset;
  1435. u16 qmap;
  1436. int i;
  1437. u16 num_tc_qps = 0;
  1438. sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
  1439. offset = 0;
  1440. if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
  1441. /* Find numtc from enabled TC bitmap */
  1442. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  1443. if (enabled_tc & BIT(i)) /* TC is enabled */
  1444. numtc++;
  1445. }
  1446. if (!numtc) {
  1447. dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
  1448. numtc = 1;
  1449. }
  1450. } else {
  1451. /* At least TC0 is enabled in case of non-DCB case */
  1452. numtc = 1;
  1453. }
  1454. vsi->tc_config.numtc = numtc;
  1455. vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
  1456. /* Number of queues per enabled TC */
  1457. qcount = vsi->alloc_queue_pairs;
  1458. num_tc_qps = qcount / numtc;
  1459. num_tc_qps = min_t(int, num_tc_qps, i40e_pf_get_max_q_per_tc(pf));
  1460. /* Setup queue offset/count for all TCs for given VSI */
  1461. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  1462. /* See if the given TC is enabled for the given VSI */
  1463. if (vsi->tc_config.enabled_tc & BIT(i)) {
  1464. /* TC is enabled */
  1465. int pow, num_qps;
  1466. switch (vsi->type) {
  1467. case I40E_VSI_MAIN:
  1468. qcount = min_t(int, pf->alloc_rss_size,
  1469. num_tc_qps);
  1470. break;
  1471. case I40E_VSI_FDIR:
  1472. case I40E_VSI_SRIOV:
  1473. case I40E_VSI_VMDQ2:
  1474. default:
  1475. qcount = num_tc_qps;
  1476. WARN_ON(i != 0);
  1477. break;
  1478. }
  1479. vsi->tc_config.tc_info[i].qoffset = offset;
  1480. vsi->tc_config.tc_info[i].qcount = qcount;
  1481. /* find the next higher power-of-2 of num queue pairs */
  1482. num_qps = qcount;
  1483. pow = 0;
  1484. while (num_qps && (BIT_ULL(pow) < qcount)) {
  1485. pow++;
  1486. num_qps >>= 1;
  1487. }
  1488. vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
  1489. qmap =
  1490. (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
  1491. (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
  1492. offset += qcount;
  1493. } else {
  1494. /* TC is not enabled so set the offset to
  1495. * default queue and allocate one queue
  1496. * for the given TC.
  1497. */
  1498. vsi->tc_config.tc_info[i].qoffset = 0;
  1499. vsi->tc_config.tc_info[i].qcount = 1;
  1500. vsi->tc_config.tc_info[i].netdev_tc = 0;
  1501. qmap = 0;
  1502. }
  1503. ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
  1504. }
  1505. /* Set actual Tx/Rx queue pairs */
  1506. vsi->num_queue_pairs = offset;
  1507. if ((vsi->type == I40E_VSI_MAIN) && (numtc == 1)) {
  1508. if (vsi->req_queue_pairs > 0)
  1509. vsi->num_queue_pairs = vsi->req_queue_pairs;
  1510. else if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  1511. vsi->num_queue_pairs = pf->num_lan_msix;
  1512. }
  1513. /* Scheduler section valid can only be set for ADD VSI */
  1514. if (is_add) {
  1515. sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
  1516. ctxt->info.up_enable_bits = enabled_tc;
  1517. }
  1518. if (vsi->type == I40E_VSI_SRIOV) {
  1519. ctxt->info.mapping_flags |=
  1520. cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
  1521. for (i = 0; i < vsi->num_queue_pairs; i++)
  1522. ctxt->info.queue_mapping[i] =
  1523. cpu_to_le16(vsi->base_queue + i);
  1524. } else {
  1525. ctxt->info.mapping_flags |=
  1526. cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
  1527. ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
  1528. }
  1529. ctxt->info.valid_sections |= cpu_to_le16(sections);
  1530. }
  1531. /**
  1532. * i40e_addr_sync - Callback for dev_(mc|uc)_sync to add address
  1533. * @netdev: the netdevice
  1534. * @addr: address to add
  1535. *
  1536. * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
  1537. * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
  1538. */
  1539. static int i40e_addr_sync(struct net_device *netdev, const u8 *addr)
  1540. {
  1541. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1542. struct i40e_vsi *vsi = np->vsi;
  1543. if (i40e_add_mac_filter(vsi, addr))
  1544. return 0;
  1545. else
  1546. return -ENOMEM;
  1547. }
  1548. /**
  1549. * i40e_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
  1550. * @netdev: the netdevice
  1551. * @addr: address to add
  1552. *
  1553. * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call
  1554. * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
  1555. */
  1556. static int i40e_addr_unsync(struct net_device *netdev, const u8 *addr)
  1557. {
  1558. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1559. struct i40e_vsi *vsi = np->vsi;
  1560. i40e_del_mac_filter(vsi, addr);
  1561. return 0;
  1562. }
  1563. /**
  1564. * i40e_set_rx_mode - NDO callback to set the netdev filters
  1565. * @netdev: network interface device structure
  1566. **/
  1567. static void i40e_set_rx_mode(struct net_device *netdev)
  1568. {
  1569. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1570. struct i40e_vsi *vsi = np->vsi;
  1571. spin_lock_bh(&vsi->mac_filter_hash_lock);
  1572. __dev_uc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
  1573. __dev_mc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
  1574. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  1575. /* check for other flag changes */
  1576. if (vsi->current_netdev_flags != vsi->netdev->flags) {
  1577. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  1578. vsi->back->flags |= I40E_FLAG_FILTER_SYNC;
  1579. }
  1580. /* schedule our worker thread which will take care of
  1581. * applying the new filter changes
  1582. */
  1583. i40e_service_event_schedule(vsi->back);
  1584. }
  1585. /**
  1586. * i40e_undo_del_filter_entries - Undo the changes made to MAC filter entries
  1587. * @vsi: Pointer to VSI struct
  1588. * @from: Pointer to list which contains MAC filter entries - changes to
  1589. * those entries needs to be undone.
  1590. *
  1591. * MAC filter entries from this list were slated for deletion.
  1592. **/
  1593. static void i40e_undo_del_filter_entries(struct i40e_vsi *vsi,
  1594. struct hlist_head *from)
  1595. {
  1596. struct i40e_mac_filter *f;
  1597. struct hlist_node *h;
  1598. hlist_for_each_entry_safe(f, h, from, hlist) {
  1599. u64 key = i40e_addr_to_hkey(f->macaddr);
  1600. /* Move the element back into MAC filter list*/
  1601. hlist_del(&f->hlist);
  1602. hash_add(vsi->mac_filter_hash, &f->hlist, key);
  1603. }
  1604. }
  1605. /**
  1606. * i40e_undo_add_filter_entries - Undo the changes made to MAC filter entries
  1607. * @vsi: Pointer to vsi struct
  1608. * @from: Pointer to list which contains MAC filter entries - changes to
  1609. * those entries needs to be undone.
  1610. *
  1611. * MAC filter entries from this list were slated for addition.
  1612. **/
  1613. static void i40e_undo_add_filter_entries(struct i40e_vsi *vsi,
  1614. struct hlist_head *from)
  1615. {
  1616. struct i40e_new_mac_filter *new;
  1617. struct hlist_node *h;
  1618. hlist_for_each_entry_safe(new, h, from, hlist) {
  1619. /* We can simply free the wrapper structure */
  1620. hlist_del(&new->hlist);
  1621. kfree(new);
  1622. }
  1623. }
  1624. /**
  1625. * i40e_next_entry - Get the next non-broadcast filter from a list
  1626. * @next: pointer to filter in list
  1627. *
  1628. * Returns the next non-broadcast filter in the list. Required so that we
  1629. * ignore broadcast filters within the list, since these are not handled via
  1630. * the normal firmware update path.
  1631. */
  1632. static
  1633. struct i40e_new_mac_filter *i40e_next_filter(struct i40e_new_mac_filter *next)
  1634. {
  1635. hlist_for_each_entry_continue(next, hlist) {
  1636. if (!is_broadcast_ether_addr(next->f->macaddr))
  1637. return next;
  1638. }
  1639. return NULL;
  1640. }
  1641. /**
  1642. * i40e_update_filter_state - Update filter state based on return data
  1643. * from firmware
  1644. * @count: Number of filters added
  1645. * @add_list: return data from fw
  1646. * @head: pointer to first filter in current batch
  1647. *
  1648. * MAC filter entries from list were slated to be added to device. Returns
  1649. * number of successful filters. Note that 0 does NOT mean success!
  1650. **/
  1651. static int
  1652. i40e_update_filter_state(int count,
  1653. struct i40e_aqc_add_macvlan_element_data *add_list,
  1654. struct i40e_new_mac_filter *add_head)
  1655. {
  1656. int retval = 0;
  1657. int i;
  1658. for (i = 0; i < count; i++) {
  1659. /* Always check status of each filter. We don't need to check
  1660. * the firmware return status because we pre-set the filter
  1661. * status to I40E_AQC_MM_ERR_NO_RES when sending the filter
  1662. * request to the adminq. Thus, if it no longer matches then
  1663. * we know the filter is active.
  1664. */
  1665. if (add_list[i].match_method == I40E_AQC_MM_ERR_NO_RES) {
  1666. add_head->state = I40E_FILTER_FAILED;
  1667. } else {
  1668. add_head->state = I40E_FILTER_ACTIVE;
  1669. retval++;
  1670. }
  1671. add_head = i40e_next_filter(add_head);
  1672. if (!add_head)
  1673. break;
  1674. }
  1675. return retval;
  1676. }
  1677. /**
  1678. * i40e_aqc_del_filters - Request firmware to delete a set of filters
  1679. * @vsi: ptr to the VSI
  1680. * @vsi_name: name to display in messages
  1681. * @list: the list of filters to send to firmware
  1682. * @num_del: the number of filters to delete
  1683. * @retval: Set to -EIO on failure to delete
  1684. *
  1685. * Send a request to firmware via AdminQ to delete a set of filters. Uses
  1686. * *retval instead of a return value so that success does not force ret_val to
  1687. * be set to 0. This ensures that a sequence of calls to this function
  1688. * preserve the previous value of *retval on successful delete.
  1689. */
  1690. static
  1691. void i40e_aqc_del_filters(struct i40e_vsi *vsi, const char *vsi_name,
  1692. struct i40e_aqc_remove_macvlan_element_data *list,
  1693. int num_del, int *retval)
  1694. {
  1695. struct i40e_hw *hw = &vsi->back->hw;
  1696. i40e_status aq_ret;
  1697. int aq_err;
  1698. aq_ret = i40e_aq_remove_macvlan(hw, vsi->seid, list, num_del, NULL);
  1699. aq_err = hw->aq.asq_last_status;
  1700. /* Explicitly ignore and do not report when firmware returns ENOENT */
  1701. if (aq_ret && !(aq_err == I40E_AQ_RC_ENOENT)) {
  1702. *retval = -EIO;
  1703. dev_info(&vsi->back->pdev->dev,
  1704. "ignoring delete macvlan error on %s, err %s, aq_err %s\n",
  1705. vsi_name, i40e_stat_str(hw, aq_ret),
  1706. i40e_aq_str(hw, aq_err));
  1707. }
  1708. }
  1709. /**
  1710. * i40e_aqc_add_filters - Request firmware to add a set of filters
  1711. * @vsi: ptr to the VSI
  1712. * @vsi_name: name to display in messages
  1713. * @list: the list of filters to send to firmware
  1714. * @add_head: Position in the add hlist
  1715. * @num_add: the number of filters to add
  1716. * @promisc_change: set to true on exit if promiscuous mode was forced on
  1717. *
  1718. * Send a request to firmware via AdminQ to add a chunk of filters. Will set
  1719. * promisc_changed to true if the firmware has run out of space for more
  1720. * filters.
  1721. */
  1722. static
  1723. void i40e_aqc_add_filters(struct i40e_vsi *vsi, const char *vsi_name,
  1724. struct i40e_aqc_add_macvlan_element_data *list,
  1725. struct i40e_new_mac_filter *add_head,
  1726. int num_add, bool *promisc_changed)
  1727. {
  1728. struct i40e_hw *hw = &vsi->back->hw;
  1729. int aq_err, fcnt;
  1730. i40e_aq_add_macvlan(hw, vsi->seid, list, num_add, NULL);
  1731. aq_err = hw->aq.asq_last_status;
  1732. fcnt = i40e_update_filter_state(num_add, list, add_head);
  1733. if (fcnt != num_add) {
  1734. *promisc_changed = true;
  1735. set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
  1736. dev_warn(&vsi->back->pdev->dev,
  1737. "Error %s adding RX filters on %s, promiscuous mode forced on\n",
  1738. i40e_aq_str(hw, aq_err),
  1739. vsi_name);
  1740. }
  1741. }
  1742. /**
  1743. * i40e_aqc_broadcast_filter - Set promiscuous broadcast flags
  1744. * @vsi: pointer to the VSI
  1745. * @f: filter data
  1746. *
  1747. * This function sets or clears the promiscuous broadcast flags for VLAN
  1748. * filters in order to properly receive broadcast frames. Assumes that only
  1749. * broadcast filters are passed.
  1750. *
  1751. * Returns status indicating success or failure;
  1752. **/
  1753. static i40e_status
  1754. i40e_aqc_broadcast_filter(struct i40e_vsi *vsi, const char *vsi_name,
  1755. struct i40e_mac_filter *f)
  1756. {
  1757. bool enable = f->state == I40E_FILTER_NEW;
  1758. struct i40e_hw *hw = &vsi->back->hw;
  1759. i40e_status aq_ret;
  1760. if (f->vlan == I40E_VLAN_ANY) {
  1761. aq_ret = i40e_aq_set_vsi_broadcast(hw,
  1762. vsi->seid,
  1763. enable,
  1764. NULL);
  1765. } else {
  1766. aq_ret = i40e_aq_set_vsi_bc_promisc_on_vlan(hw,
  1767. vsi->seid,
  1768. enable,
  1769. f->vlan,
  1770. NULL);
  1771. }
  1772. if (aq_ret)
  1773. dev_warn(&vsi->back->pdev->dev,
  1774. "Error %s setting broadcast promiscuous mode on %s\n",
  1775. i40e_aq_str(hw, hw->aq.asq_last_status),
  1776. vsi_name);
  1777. return aq_ret;
  1778. }
  1779. /**
  1780. * i40e_sync_vsi_filters - Update the VSI filter list to the HW
  1781. * @vsi: ptr to the VSI
  1782. *
  1783. * Push any outstanding VSI filter changes through the AdminQ.
  1784. *
  1785. * Returns 0 or error value
  1786. **/
  1787. int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
  1788. {
  1789. struct hlist_head tmp_add_list, tmp_del_list;
  1790. struct i40e_mac_filter *f;
  1791. struct i40e_new_mac_filter *new, *add_head = NULL;
  1792. struct i40e_hw *hw = &vsi->back->hw;
  1793. unsigned int failed_filters = 0;
  1794. unsigned int vlan_filters = 0;
  1795. bool promisc_changed = false;
  1796. char vsi_name[16] = "PF";
  1797. int filter_list_len = 0;
  1798. i40e_status aq_ret = 0;
  1799. u32 changed_flags = 0;
  1800. struct hlist_node *h;
  1801. struct i40e_pf *pf;
  1802. int num_add = 0;
  1803. int num_del = 0;
  1804. int retval = 0;
  1805. u16 cmd_flags;
  1806. int list_size;
  1807. int bkt;
  1808. /* empty array typed pointers, kcalloc later */
  1809. struct i40e_aqc_add_macvlan_element_data *add_list;
  1810. struct i40e_aqc_remove_macvlan_element_data *del_list;
  1811. while (test_and_set_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state))
  1812. usleep_range(1000, 2000);
  1813. pf = vsi->back;
  1814. if (vsi->netdev) {
  1815. changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
  1816. vsi->current_netdev_flags = vsi->netdev->flags;
  1817. }
  1818. INIT_HLIST_HEAD(&tmp_add_list);
  1819. INIT_HLIST_HEAD(&tmp_del_list);
  1820. if (vsi->type == I40E_VSI_SRIOV)
  1821. snprintf(vsi_name, sizeof(vsi_name) - 1, "VF %d", vsi->vf_id);
  1822. else if (vsi->type != I40E_VSI_MAIN)
  1823. snprintf(vsi_name, sizeof(vsi_name) - 1, "vsi %d", vsi->seid);
  1824. if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
  1825. vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
  1826. spin_lock_bh(&vsi->mac_filter_hash_lock);
  1827. /* Create a list of filters to delete. */
  1828. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  1829. if (f->state == I40E_FILTER_REMOVE) {
  1830. /* Move the element into temporary del_list */
  1831. hash_del(&f->hlist);
  1832. hlist_add_head(&f->hlist, &tmp_del_list);
  1833. /* Avoid counting removed filters */
  1834. continue;
  1835. }
  1836. if (f->state == I40E_FILTER_NEW) {
  1837. /* Create a temporary i40e_new_mac_filter */
  1838. new = kzalloc(sizeof(*new), GFP_ATOMIC);
  1839. if (!new)
  1840. goto err_no_memory_locked;
  1841. /* Store pointer to the real filter */
  1842. new->f = f;
  1843. new->state = f->state;
  1844. /* Add it to the hash list */
  1845. hlist_add_head(&new->hlist, &tmp_add_list);
  1846. }
  1847. /* Count the number of active (current and new) VLAN
  1848. * filters we have now. Does not count filters which
  1849. * are marked for deletion.
  1850. */
  1851. if (f->vlan > 0)
  1852. vlan_filters++;
  1853. }
  1854. retval = i40e_correct_mac_vlan_filters(vsi,
  1855. &tmp_add_list,
  1856. &tmp_del_list,
  1857. vlan_filters);
  1858. if (retval)
  1859. goto err_no_memory_locked;
  1860. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  1861. }
  1862. /* Now process 'del_list' outside the lock */
  1863. if (!hlist_empty(&tmp_del_list)) {
  1864. filter_list_len = hw->aq.asq_buf_size /
  1865. sizeof(struct i40e_aqc_remove_macvlan_element_data);
  1866. list_size = filter_list_len *
  1867. sizeof(struct i40e_aqc_remove_macvlan_element_data);
  1868. del_list = kzalloc(list_size, GFP_ATOMIC);
  1869. if (!del_list)
  1870. goto err_no_memory;
  1871. hlist_for_each_entry_safe(f, h, &tmp_del_list, hlist) {
  1872. cmd_flags = 0;
  1873. /* handle broadcast filters by updating the broadcast
  1874. * promiscuous flag and release filter list.
  1875. */
  1876. if (is_broadcast_ether_addr(f->macaddr)) {
  1877. i40e_aqc_broadcast_filter(vsi, vsi_name, f);
  1878. hlist_del(&f->hlist);
  1879. kfree(f);
  1880. continue;
  1881. }
  1882. /* add to delete list */
  1883. ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
  1884. if (f->vlan == I40E_VLAN_ANY) {
  1885. del_list[num_del].vlan_tag = 0;
  1886. cmd_flags |= I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
  1887. } else {
  1888. del_list[num_del].vlan_tag =
  1889. cpu_to_le16((u16)(f->vlan));
  1890. }
  1891. cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
  1892. del_list[num_del].flags = cmd_flags;
  1893. num_del++;
  1894. /* flush a full buffer */
  1895. if (num_del == filter_list_len) {
  1896. i40e_aqc_del_filters(vsi, vsi_name, del_list,
  1897. num_del, &retval);
  1898. memset(del_list, 0, list_size);
  1899. num_del = 0;
  1900. }
  1901. /* Release memory for MAC filter entries which were
  1902. * synced up with HW.
  1903. */
  1904. hlist_del(&f->hlist);
  1905. kfree(f);
  1906. }
  1907. if (num_del) {
  1908. i40e_aqc_del_filters(vsi, vsi_name, del_list,
  1909. num_del, &retval);
  1910. }
  1911. kfree(del_list);
  1912. del_list = NULL;
  1913. }
  1914. if (!hlist_empty(&tmp_add_list)) {
  1915. /* Do all the adds now. */
  1916. filter_list_len = hw->aq.asq_buf_size /
  1917. sizeof(struct i40e_aqc_add_macvlan_element_data);
  1918. list_size = filter_list_len *
  1919. sizeof(struct i40e_aqc_add_macvlan_element_data);
  1920. add_list = kzalloc(list_size, GFP_ATOMIC);
  1921. if (!add_list)
  1922. goto err_no_memory;
  1923. num_add = 0;
  1924. hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
  1925. if (test_bit(__I40E_VSI_OVERFLOW_PROMISC,
  1926. vsi->state)) {
  1927. new->state = I40E_FILTER_FAILED;
  1928. continue;
  1929. }
  1930. /* handle broadcast filters by updating the broadcast
  1931. * promiscuous flag instead of adding a MAC filter.
  1932. */
  1933. if (is_broadcast_ether_addr(new->f->macaddr)) {
  1934. if (i40e_aqc_broadcast_filter(vsi, vsi_name,
  1935. new->f))
  1936. new->state = I40E_FILTER_FAILED;
  1937. else
  1938. new->state = I40E_FILTER_ACTIVE;
  1939. continue;
  1940. }
  1941. /* add to add array */
  1942. if (num_add == 0)
  1943. add_head = new;
  1944. cmd_flags = 0;
  1945. ether_addr_copy(add_list[num_add].mac_addr,
  1946. new->f->macaddr);
  1947. if (new->f->vlan == I40E_VLAN_ANY) {
  1948. add_list[num_add].vlan_tag = 0;
  1949. cmd_flags |= I40E_AQC_MACVLAN_ADD_IGNORE_VLAN;
  1950. } else {
  1951. add_list[num_add].vlan_tag =
  1952. cpu_to_le16((u16)(new->f->vlan));
  1953. }
  1954. add_list[num_add].queue_number = 0;
  1955. /* set invalid match method for later detection */
  1956. add_list[num_add].match_method = I40E_AQC_MM_ERR_NO_RES;
  1957. cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
  1958. add_list[num_add].flags = cpu_to_le16(cmd_flags);
  1959. num_add++;
  1960. /* flush a full buffer */
  1961. if (num_add == filter_list_len) {
  1962. i40e_aqc_add_filters(vsi, vsi_name, add_list,
  1963. add_head, num_add,
  1964. &promisc_changed);
  1965. memset(add_list, 0, list_size);
  1966. num_add = 0;
  1967. }
  1968. }
  1969. if (num_add) {
  1970. i40e_aqc_add_filters(vsi, vsi_name, add_list, add_head,
  1971. num_add, &promisc_changed);
  1972. }
  1973. /* Now move all of the filters from the temp add list back to
  1974. * the VSI's list.
  1975. */
  1976. spin_lock_bh(&vsi->mac_filter_hash_lock);
  1977. hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
  1978. /* Only update the state if we're still NEW */
  1979. if (new->f->state == I40E_FILTER_NEW)
  1980. new->f->state = new->state;
  1981. hlist_del(&new->hlist);
  1982. kfree(new);
  1983. }
  1984. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  1985. kfree(add_list);
  1986. add_list = NULL;
  1987. }
  1988. /* Determine the number of active and failed filters. */
  1989. spin_lock_bh(&vsi->mac_filter_hash_lock);
  1990. vsi->active_filters = 0;
  1991. hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
  1992. if (f->state == I40E_FILTER_ACTIVE)
  1993. vsi->active_filters++;
  1994. else if (f->state == I40E_FILTER_FAILED)
  1995. failed_filters++;
  1996. }
  1997. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  1998. /* If promiscuous mode has changed, we need to calculate a new
  1999. * threshold for when we are safe to exit
  2000. */
  2001. if (promisc_changed)
  2002. vsi->promisc_threshold = (vsi->active_filters * 3) / 4;
  2003. /* Check if we are able to exit overflow promiscuous mode. We can
  2004. * safely exit if we didn't just enter, we no longer have any failed
  2005. * filters, and we have reduced filters below the threshold value.
  2006. */
  2007. if (test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state) &&
  2008. !promisc_changed && !failed_filters &&
  2009. (vsi->active_filters < vsi->promisc_threshold)) {
  2010. dev_info(&pf->pdev->dev,
  2011. "filter logjam cleared on %s, leaving overflow promiscuous mode\n",
  2012. vsi_name);
  2013. clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
  2014. promisc_changed = true;
  2015. vsi->promisc_threshold = 0;
  2016. }
  2017. /* if the VF is not trusted do not do promisc */
  2018. if ((vsi->type == I40E_VSI_SRIOV) && !pf->vf[vsi->vf_id].trusted) {
  2019. clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
  2020. goto out;
  2021. }
  2022. /* check for changes in promiscuous modes */
  2023. if (changed_flags & IFF_ALLMULTI) {
  2024. bool cur_multipromisc;
  2025. cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
  2026. aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
  2027. vsi->seid,
  2028. cur_multipromisc,
  2029. NULL);
  2030. if (aq_ret) {
  2031. retval = i40e_aq_rc_to_posix(aq_ret,
  2032. hw->aq.asq_last_status);
  2033. dev_info(&pf->pdev->dev,
  2034. "set multi promisc failed on %s, err %s aq_err %s\n",
  2035. vsi_name,
  2036. i40e_stat_str(hw, aq_ret),
  2037. i40e_aq_str(hw, hw->aq.asq_last_status));
  2038. }
  2039. }
  2040. if ((changed_flags & IFF_PROMISC) || promisc_changed) {
  2041. bool cur_promisc;
  2042. cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
  2043. test_bit(__I40E_VSI_OVERFLOW_PROMISC,
  2044. vsi->state));
  2045. if ((vsi->type == I40E_VSI_MAIN) &&
  2046. (pf->lan_veb != I40E_NO_VEB) &&
  2047. !(pf->flags & I40E_FLAG_MFP_ENABLED)) {
  2048. /* set defport ON for Main VSI instead of true promisc
  2049. * this way we will get all unicast/multicast and VLAN
  2050. * promisc behavior but will not get VF or VMDq traffic
  2051. * replicated on the Main VSI.
  2052. */
  2053. if (pf->cur_promisc != cur_promisc) {
  2054. pf->cur_promisc = cur_promisc;
  2055. if (cur_promisc)
  2056. aq_ret =
  2057. i40e_aq_set_default_vsi(hw,
  2058. vsi->seid,
  2059. NULL);
  2060. else
  2061. aq_ret =
  2062. i40e_aq_clear_default_vsi(hw,
  2063. vsi->seid,
  2064. NULL);
  2065. if (aq_ret) {
  2066. retval = i40e_aq_rc_to_posix(aq_ret,
  2067. hw->aq.asq_last_status);
  2068. dev_info(&pf->pdev->dev,
  2069. "Set default VSI failed on %s, err %s, aq_err %s\n",
  2070. vsi_name,
  2071. i40e_stat_str(hw, aq_ret),
  2072. i40e_aq_str(hw,
  2073. hw->aq.asq_last_status));
  2074. }
  2075. }
  2076. } else {
  2077. aq_ret = i40e_aq_set_vsi_unicast_promiscuous(
  2078. hw,
  2079. vsi->seid,
  2080. cur_promisc, NULL,
  2081. true);
  2082. if (aq_ret) {
  2083. retval =
  2084. i40e_aq_rc_to_posix(aq_ret,
  2085. hw->aq.asq_last_status);
  2086. dev_info(&pf->pdev->dev,
  2087. "set unicast promisc failed on %s, err %s, aq_err %s\n",
  2088. vsi_name,
  2089. i40e_stat_str(hw, aq_ret),
  2090. i40e_aq_str(hw,
  2091. hw->aq.asq_last_status));
  2092. }
  2093. aq_ret = i40e_aq_set_vsi_multicast_promiscuous(
  2094. hw,
  2095. vsi->seid,
  2096. cur_promisc, NULL);
  2097. if (aq_ret) {
  2098. retval =
  2099. i40e_aq_rc_to_posix(aq_ret,
  2100. hw->aq.asq_last_status);
  2101. dev_info(&pf->pdev->dev,
  2102. "set multicast promisc failed on %s, err %s, aq_err %s\n",
  2103. vsi_name,
  2104. i40e_stat_str(hw, aq_ret),
  2105. i40e_aq_str(hw,
  2106. hw->aq.asq_last_status));
  2107. }
  2108. }
  2109. aq_ret = i40e_aq_set_vsi_broadcast(&vsi->back->hw,
  2110. vsi->seid,
  2111. cur_promisc, NULL);
  2112. if (aq_ret) {
  2113. retval = i40e_aq_rc_to_posix(aq_ret,
  2114. pf->hw.aq.asq_last_status);
  2115. dev_info(&pf->pdev->dev,
  2116. "set brdcast promisc failed, err %s, aq_err %s\n",
  2117. i40e_stat_str(hw, aq_ret),
  2118. i40e_aq_str(hw,
  2119. hw->aq.asq_last_status));
  2120. }
  2121. }
  2122. out:
  2123. /* if something went wrong then set the changed flag so we try again */
  2124. if (retval)
  2125. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  2126. clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
  2127. return retval;
  2128. err_no_memory:
  2129. /* Restore elements on the temporary add and delete lists */
  2130. spin_lock_bh(&vsi->mac_filter_hash_lock);
  2131. err_no_memory_locked:
  2132. i40e_undo_del_filter_entries(vsi, &tmp_del_list);
  2133. i40e_undo_add_filter_entries(vsi, &tmp_add_list);
  2134. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2135. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  2136. clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
  2137. return -ENOMEM;
  2138. }
  2139. /**
  2140. * i40e_sync_filters_subtask - Sync the VSI filter list with HW
  2141. * @pf: board private structure
  2142. **/
  2143. static void i40e_sync_filters_subtask(struct i40e_pf *pf)
  2144. {
  2145. int v;
  2146. if (!pf || !(pf->flags & I40E_FLAG_FILTER_SYNC))
  2147. return;
  2148. pf->flags &= ~I40E_FLAG_FILTER_SYNC;
  2149. for (v = 0; v < pf->num_alloc_vsi; v++) {
  2150. if (pf->vsi[v] &&
  2151. (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED)) {
  2152. int ret = i40e_sync_vsi_filters(pf->vsi[v]);
  2153. if (ret) {
  2154. /* come back and try again later */
  2155. pf->flags |= I40E_FLAG_FILTER_SYNC;
  2156. break;
  2157. }
  2158. }
  2159. }
  2160. }
  2161. /**
  2162. * i40e_max_xdp_frame_size - returns the maximum allowed frame size for XDP
  2163. * @vsi: the vsi
  2164. **/
  2165. static int i40e_max_xdp_frame_size(struct i40e_vsi *vsi)
  2166. {
  2167. if (PAGE_SIZE >= 8192 || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
  2168. return I40E_RXBUFFER_2048;
  2169. else
  2170. return I40E_RXBUFFER_3072;
  2171. }
  2172. /**
  2173. * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
  2174. * @netdev: network interface device structure
  2175. * @new_mtu: new value for maximum frame size
  2176. *
  2177. * Returns 0 on success, negative on failure
  2178. **/
  2179. static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
  2180. {
  2181. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2182. struct i40e_vsi *vsi = np->vsi;
  2183. struct i40e_pf *pf = vsi->back;
  2184. if (i40e_enabled_xdp_vsi(vsi)) {
  2185. int frame_size = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
  2186. if (frame_size > i40e_max_xdp_frame_size(vsi))
  2187. return -EINVAL;
  2188. }
  2189. netdev_info(netdev, "changing MTU from %d to %d\n",
  2190. netdev->mtu, new_mtu);
  2191. netdev->mtu = new_mtu;
  2192. if (netif_running(netdev))
  2193. i40e_vsi_reinit_locked(vsi);
  2194. pf->flags |= (I40E_FLAG_SERVICE_CLIENT_REQUESTED |
  2195. I40E_FLAG_CLIENT_L2_CHANGE);
  2196. return 0;
  2197. }
  2198. /**
  2199. * i40e_ioctl - Access the hwtstamp interface
  2200. * @netdev: network interface device structure
  2201. * @ifr: interface request data
  2202. * @cmd: ioctl command
  2203. **/
  2204. int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
  2205. {
  2206. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2207. struct i40e_pf *pf = np->vsi->back;
  2208. switch (cmd) {
  2209. case SIOCGHWTSTAMP:
  2210. return i40e_ptp_get_ts_config(pf, ifr);
  2211. case SIOCSHWTSTAMP:
  2212. return i40e_ptp_set_ts_config(pf, ifr);
  2213. default:
  2214. return -EOPNOTSUPP;
  2215. }
  2216. }
  2217. /**
  2218. * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
  2219. * @vsi: the vsi being adjusted
  2220. **/
  2221. void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
  2222. {
  2223. struct i40e_vsi_context ctxt;
  2224. i40e_status ret;
  2225. if ((vsi->info.valid_sections &
  2226. cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
  2227. ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
  2228. return; /* already enabled */
  2229. vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
  2230. vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
  2231. I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
  2232. ctxt.seid = vsi->seid;
  2233. ctxt.info = vsi->info;
  2234. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  2235. if (ret) {
  2236. dev_info(&vsi->back->pdev->dev,
  2237. "update vlan stripping failed, err %s aq_err %s\n",
  2238. i40e_stat_str(&vsi->back->hw, ret),
  2239. i40e_aq_str(&vsi->back->hw,
  2240. vsi->back->hw.aq.asq_last_status));
  2241. }
  2242. }
  2243. /**
  2244. * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
  2245. * @vsi: the vsi being adjusted
  2246. **/
  2247. void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
  2248. {
  2249. struct i40e_vsi_context ctxt;
  2250. i40e_status ret;
  2251. if ((vsi->info.valid_sections &
  2252. cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
  2253. ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
  2254. I40E_AQ_VSI_PVLAN_EMOD_MASK))
  2255. return; /* already disabled */
  2256. vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
  2257. vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
  2258. I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
  2259. ctxt.seid = vsi->seid;
  2260. ctxt.info = vsi->info;
  2261. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  2262. if (ret) {
  2263. dev_info(&vsi->back->pdev->dev,
  2264. "update vlan stripping failed, err %s aq_err %s\n",
  2265. i40e_stat_str(&vsi->back->hw, ret),
  2266. i40e_aq_str(&vsi->back->hw,
  2267. vsi->back->hw.aq.asq_last_status));
  2268. }
  2269. }
  2270. /**
  2271. * i40e_vlan_rx_register - Setup or shutdown vlan offload
  2272. * @netdev: network interface to be adjusted
  2273. * @features: netdev features to test if VLAN offload is enabled or not
  2274. **/
  2275. static void i40e_vlan_rx_register(struct net_device *netdev, u32 features)
  2276. {
  2277. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2278. struct i40e_vsi *vsi = np->vsi;
  2279. if (features & NETIF_F_HW_VLAN_CTAG_RX)
  2280. i40e_vlan_stripping_enable(vsi);
  2281. else
  2282. i40e_vlan_stripping_disable(vsi);
  2283. }
  2284. /**
  2285. * i40e_add_vlan_all_mac - Add a MAC/VLAN filter for each existing MAC address
  2286. * @vsi: the vsi being configured
  2287. * @vid: vlan id to be added (0 = untagged only , -1 = any)
  2288. *
  2289. * This is a helper function for adding a new MAC/VLAN filter with the
  2290. * specified VLAN for each existing MAC address already in the hash table.
  2291. * This function does *not* perform any accounting to update filters based on
  2292. * VLAN mode.
  2293. *
  2294. * NOTE: this function expects to be called while under the
  2295. * mac_filter_hash_lock
  2296. **/
  2297. int i40e_add_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
  2298. {
  2299. struct i40e_mac_filter *f, *add_f;
  2300. struct hlist_node *h;
  2301. int bkt;
  2302. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  2303. if (f->state == I40E_FILTER_REMOVE)
  2304. continue;
  2305. add_f = i40e_add_filter(vsi, f->macaddr, vid);
  2306. if (!add_f) {
  2307. dev_info(&vsi->back->pdev->dev,
  2308. "Could not add vlan filter %d for %pM\n",
  2309. vid, f->macaddr);
  2310. return -ENOMEM;
  2311. }
  2312. }
  2313. return 0;
  2314. }
  2315. /**
  2316. * i40e_vsi_add_vlan - Add VSI membership for given VLAN
  2317. * @vsi: the VSI being configured
  2318. * @vid: VLAN id to be added
  2319. **/
  2320. int i40e_vsi_add_vlan(struct i40e_vsi *vsi, u16 vid)
  2321. {
  2322. int err;
  2323. if (!vid || vsi->info.pvid)
  2324. return -EINVAL;
  2325. /* Locked once because all functions invoked below iterates list*/
  2326. spin_lock_bh(&vsi->mac_filter_hash_lock);
  2327. err = i40e_add_vlan_all_mac(vsi, vid);
  2328. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2329. if (err)
  2330. return err;
  2331. /* schedule our worker thread which will take care of
  2332. * applying the new filter changes
  2333. */
  2334. i40e_service_event_schedule(vsi->back);
  2335. return 0;
  2336. }
  2337. /**
  2338. * i40e_rm_vlan_all_mac - Remove MAC/VLAN pair for all MAC with the given VLAN
  2339. * @vsi: the vsi being configured
  2340. * @vid: vlan id to be removed (0 = untagged only , -1 = any)
  2341. *
  2342. * This function should be used to remove all VLAN filters which match the
  2343. * given VID. It does not schedule the service event and does not take the
  2344. * mac_filter_hash_lock so it may be combined with other operations under
  2345. * a single invocation of the mac_filter_hash_lock.
  2346. *
  2347. * NOTE: this function expects to be called while under the
  2348. * mac_filter_hash_lock
  2349. */
  2350. void i40e_rm_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
  2351. {
  2352. struct i40e_mac_filter *f;
  2353. struct hlist_node *h;
  2354. int bkt;
  2355. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  2356. if (f->vlan == vid)
  2357. __i40e_del_filter(vsi, f);
  2358. }
  2359. }
  2360. /**
  2361. * i40e_vsi_kill_vlan - Remove VSI membership for given VLAN
  2362. * @vsi: the VSI being configured
  2363. * @vid: VLAN id to be removed
  2364. **/
  2365. void i40e_vsi_kill_vlan(struct i40e_vsi *vsi, u16 vid)
  2366. {
  2367. if (!vid || vsi->info.pvid)
  2368. return;
  2369. spin_lock_bh(&vsi->mac_filter_hash_lock);
  2370. i40e_rm_vlan_all_mac(vsi, vid);
  2371. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2372. /* schedule our worker thread which will take care of
  2373. * applying the new filter changes
  2374. */
  2375. i40e_service_event_schedule(vsi->back);
  2376. }
  2377. /**
  2378. * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
  2379. * @netdev: network interface to be adjusted
  2380. * @vid: vlan id to be added
  2381. *
  2382. * net_device_ops implementation for adding vlan ids
  2383. **/
  2384. static int i40e_vlan_rx_add_vid(struct net_device *netdev,
  2385. __always_unused __be16 proto, u16 vid)
  2386. {
  2387. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2388. struct i40e_vsi *vsi = np->vsi;
  2389. int ret = 0;
  2390. if (vid >= VLAN_N_VID)
  2391. return -EINVAL;
  2392. /* If the network stack called us with vid = 0 then
  2393. * it is asking to receive priority tagged packets with
  2394. * vlan id 0. Our HW receives them by default when configured
  2395. * to receive untagged packets so there is no need to add an
  2396. * extra filter for vlan 0 tagged packets.
  2397. */
  2398. if (vid)
  2399. ret = i40e_vsi_add_vlan(vsi, vid);
  2400. if (!ret)
  2401. set_bit(vid, vsi->active_vlans);
  2402. return ret;
  2403. }
  2404. /**
  2405. * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
  2406. * @netdev: network interface to be adjusted
  2407. * @vid: vlan id to be removed
  2408. *
  2409. * net_device_ops implementation for removing vlan ids
  2410. **/
  2411. static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
  2412. __always_unused __be16 proto, u16 vid)
  2413. {
  2414. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2415. struct i40e_vsi *vsi = np->vsi;
  2416. /* return code is ignored as there is nothing a user
  2417. * can do about failure to remove and a log message was
  2418. * already printed from the other function
  2419. */
  2420. i40e_vsi_kill_vlan(vsi, vid);
  2421. clear_bit(vid, vsi->active_vlans);
  2422. return 0;
  2423. }
  2424. /**
  2425. * i40e_macaddr_init - explicitly write the mac address filters
  2426. *
  2427. * @vsi: pointer to the vsi
  2428. * @macaddr: the MAC address
  2429. *
  2430. * This is needed when the macaddr has been obtained by other
  2431. * means than the default, e.g., from Open Firmware or IDPROM.
  2432. * Returns 0 on success, negative on failure
  2433. **/
  2434. static int i40e_macaddr_init(struct i40e_vsi *vsi, u8 *macaddr)
  2435. {
  2436. int ret;
  2437. struct i40e_aqc_add_macvlan_element_data element;
  2438. ret = i40e_aq_mac_address_write(&vsi->back->hw,
  2439. I40E_AQC_WRITE_TYPE_LAA_WOL,
  2440. macaddr, NULL);
  2441. if (ret) {
  2442. dev_info(&vsi->back->pdev->dev,
  2443. "Addr change for VSI failed: %d\n", ret);
  2444. return -EADDRNOTAVAIL;
  2445. }
  2446. memset(&element, 0, sizeof(element));
  2447. ether_addr_copy(element.mac_addr, macaddr);
  2448. element.flags = cpu_to_le16(I40E_AQC_MACVLAN_ADD_PERFECT_MATCH);
  2449. ret = i40e_aq_add_macvlan(&vsi->back->hw, vsi->seid, &element, 1, NULL);
  2450. if (ret) {
  2451. dev_info(&vsi->back->pdev->dev,
  2452. "add filter failed err %s aq_err %s\n",
  2453. i40e_stat_str(&vsi->back->hw, ret),
  2454. i40e_aq_str(&vsi->back->hw,
  2455. vsi->back->hw.aq.asq_last_status));
  2456. }
  2457. return ret;
  2458. }
  2459. /**
  2460. * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
  2461. * @vsi: the vsi being brought back up
  2462. **/
  2463. static void i40e_restore_vlan(struct i40e_vsi *vsi)
  2464. {
  2465. u16 vid;
  2466. if (!vsi->netdev)
  2467. return;
  2468. i40e_vlan_rx_register(vsi->netdev, vsi->netdev->features);
  2469. for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
  2470. i40e_vlan_rx_add_vid(vsi->netdev, htons(ETH_P_8021Q),
  2471. vid);
  2472. }
  2473. /**
  2474. * i40e_vsi_add_pvid - Add pvid for the VSI
  2475. * @vsi: the vsi being adjusted
  2476. * @vid: the vlan id to set as a PVID
  2477. **/
  2478. int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
  2479. {
  2480. struct i40e_vsi_context ctxt;
  2481. i40e_status ret;
  2482. vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
  2483. vsi->info.pvid = cpu_to_le16(vid);
  2484. vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
  2485. I40E_AQ_VSI_PVLAN_INSERT_PVID |
  2486. I40E_AQ_VSI_PVLAN_EMOD_STR;
  2487. ctxt.seid = vsi->seid;
  2488. ctxt.info = vsi->info;
  2489. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  2490. if (ret) {
  2491. dev_info(&vsi->back->pdev->dev,
  2492. "add pvid failed, err %s aq_err %s\n",
  2493. i40e_stat_str(&vsi->back->hw, ret),
  2494. i40e_aq_str(&vsi->back->hw,
  2495. vsi->back->hw.aq.asq_last_status));
  2496. return -ENOENT;
  2497. }
  2498. return 0;
  2499. }
  2500. /**
  2501. * i40e_vsi_remove_pvid - Remove the pvid from the VSI
  2502. * @vsi: the vsi being adjusted
  2503. *
  2504. * Just use the vlan_rx_register() service to put it back to normal
  2505. **/
  2506. void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
  2507. {
  2508. i40e_vlan_stripping_disable(vsi);
  2509. vsi->info.pvid = 0;
  2510. }
  2511. /**
  2512. * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
  2513. * @vsi: ptr to the VSI
  2514. *
  2515. * If this function returns with an error, then it's possible one or
  2516. * more of the rings is populated (while the rest are not). It is the
  2517. * callers duty to clean those orphaned rings.
  2518. *
  2519. * Return 0 on success, negative on failure
  2520. **/
  2521. static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
  2522. {
  2523. int i, err = 0;
  2524. for (i = 0; i < vsi->num_queue_pairs && !err; i++)
  2525. err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
  2526. if (!i40e_enabled_xdp_vsi(vsi))
  2527. return err;
  2528. for (i = 0; i < vsi->num_queue_pairs && !err; i++)
  2529. err = i40e_setup_tx_descriptors(vsi->xdp_rings[i]);
  2530. return err;
  2531. }
  2532. /**
  2533. * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
  2534. * @vsi: ptr to the VSI
  2535. *
  2536. * Free VSI's transmit software resources
  2537. **/
  2538. static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
  2539. {
  2540. int i;
  2541. if (vsi->tx_rings) {
  2542. for (i = 0; i < vsi->num_queue_pairs; i++)
  2543. if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
  2544. i40e_free_tx_resources(vsi->tx_rings[i]);
  2545. }
  2546. if (vsi->xdp_rings) {
  2547. for (i = 0; i < vsi->num_queue_pairs; i++)
  2548. if (vsi->xdp_rings[i] && vsi->xdp_rings[i]->desc)
  2549. i40e_free_tx_resources(vsi->xdp_rings[i]);
  2550. }
  2551. }
  2552. /**
  2553. * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
  2554. * @vsi: ptr to the VSI
  2555. *
  2556. * If this function returns with an error, then it's possible one or
  2557. * more of the rings is populated (while the rest are not). It is the
  2558. * callers duty to clean those orphaned rings.
  2559. *
  2560. * Return 0 on success, negative on failure
  2561. **/
  2562. static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
  2563. {
  2564. int i, err = 0;
  2565. for (i = 0; i < vsi->num_queue_pairs && !err; i++)
  2566. err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
  2567. return err;
  2568. }
  2569. /**
  2570. * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
  2571. * @vsi: ptr to the VSI
  2572. *
  2573. * Free all receive software resources
  2574. **/
  2575. static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
  2576. {
  2577. int i;
  2578. if (!vsi->rx_rings)
  2579. return;
  2580. for (i = 0; i < vsi->num_queue_pairs; i++)
  2581. if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
  2582. i40e_free_rx_resources(vsi->rx_rings[i]);
  2583. }
  2584. /**
  2585. * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
  2586. * @ring: The Tx ring to configure
  2587. *
  2588. * This enables/disables XPS for a given Tx descriptor ring
  2589. * based on the TCs enabled for the VSI that ring belongs to.
  2590. **/
  2591. static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
  2592. {
  2593. struct i40e_vsi *vsi = ring->vsi;
  2594. cpumask_var_t mask;
  2595. if (!ring->q_vector || !ring->netdev)
  2596. return;
  2597. /* Single TC mode enable XPS */
  2598. if (vsi->tc_config.numtc <= 1) {
  2599. if (!test_and_set_bit(__I40E_TX_XPS_INIT_DONE, &ring->state))
  2600. netif_set_xps_queue(ring->netdev,
  2601. &ring->q_vector->affinity_mask,
  2602. ring->queue_index);
  2603. } else if (alloc_cpumask_var(&mask, GFP_KERNEL)) {
  2604. /* Disable XPS to allow selection based on TC */
  2605. bitmap_zero(cpumask_bits(mask), nr_cpumask_bits);
  2606. netif_set_xps_queue(ring->netdev, mask, ring->queue_index);
  2607. free_cpumask_var(mask);
  2608. }
  2609. /* schedule our worker thread which will take care of
  2610. * applying the new filter changes
  2611. */
  2612. i40e_service_event_schedule(vsi->back);
  2613. }
  2614. /**
  2615. * i40e_configure_tx_ring - Configure a transmit ring context and rest
  2616. * @ring: The Tx ring to configure
  2617. *
  2618. * Configure the Tx descriptor ring in the HMC context.
  2619. **/
  2620. static int i40e_configure_tx_ring(struct i40e_ring *ring)
  2621. {
  2622. struct i40e_vsi *vsi = ring->vsi;
  2623. u16 pf_q = vsi->base_queue + ring->queue_index;
  2624. struct i40e_hw *hw = &vsi->back->hw;
  2625. struct i40e_hmc_obj_txq tx_ctx;
  2626. i40e_status err = 0;
  2627. u32 qtx_ctl = 0;
  2628. /* some ATR related tx ring init */
  2629. if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
  2630. ring->atr_sample_rate = vsi->back->atr_sample_rate;
  2631. ring->atr_count = 0;
  2632. } else {
  2633. ring->atr_sample_rate = 0;
  2634. }
  2635. /* configure XPS */
  2636. i40e_config_xps_tx_ring(ring);
  2637. /* clear the context structure first */
  2638. memset(&tx_ctx, 0, sizeof(tx_ctx));
  2639. tx_ctx.new_context = 1;
  2640. tx_ctx.base = (ring->dma / 128);
  2641. tx_ctx.qlen = ring->count;
  2642. tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
  2643. I40E_FLAG_FD_ATR_ENABLED));
  2644. tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
  2645. /* FDIR VSI tx ring can still use RS bit and writebacks */
  2646. if (vsi->type != I40E_VSI_FDIR)
  2647. tx_ctx.head_wb_ena = 1;
  2648. tx_ctx.head_wb_addr = ring->dma +
  2649. (ring->count * sizeof(struct i40e_tx_desc));
  2650. /* As part of VSI creation/update, FW allocates certain
  2651. * Tx arbitration queue sets for each TC enabled for
  2652. * the VSI. The FW returns the handles to these queue
  2653. * sets as part of the response buffer to Add VSI,
  2654. * Update VSI, etc. AQ commands. It is expected that
  2655. * these queue set handles be associated with the Tx
  2656. * queues by the driver as part of the TX queue context
  2657. * initialization. This has to be done regardless of
  2658. * DCB as by default everything is mapped to TC0.
  2659. */
  2660. tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
  2661. tx_ctx.rdylist_act = 0;
  2662. /* clear the context in the HMC */
  2663. err = i40e_clear_lan_tx_queue_context(hw, pf_q);
  2664. if (err) {
  2665. dev_info(&vsi->back->pdev->dev,
  2666. "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
  2667. ring->queue_index, pf_q, err);
  2668. return -ENOMEM;
  2669. }
  2670. /* set the context in the HMC */
  2671. err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
  2672. if (err) {
  2673. dev_info(&vsi->back->pdev->dev,
  2674. "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
  2675. ring->queue_index, pf_q, err);
  2676. return -ENOMEM;
  2677. }
  2678. /* Now associate this queue with this PCI function */
  2679. if (vsi->type == I40E_VSI_VMDQ2) {
  2680. qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
  2681. qtx_ctl |= ((vsi->id) << I40E_QTX_CTL_VFVM_INDX_SHIFT) &
  2682. I40E_QTX_CTL_VFVM_INDX_MASK;
  2683. } else {
  2684. qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
  2685. }
  2686. qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
  2687. I40E_QTX_CTL_PF_INDX_MASK);
  2688. wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
  2689. i40e_flush(hw);
  2690. /* cache tail off for easier writes later */
  2691. ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
  2692. return 0;
  2693. }
  2694. /**
  2695. * i40e_configure_rx_ring - Configure a receive ring context
  2696. * @ring: The Rx ring to configure
  2697. *
  2698. * Configure the Rx descriptor ring in the HMC context.
  2699. **/
  2700. static int i40e_configure_rx_ring(struct i40e_ring *ring)
  2701. {
  2702. struct i40e_vsi *vsi = ring->vsi;
  2703. u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
  2704. u16 pf_q = vsi->base_queue + ring->queue_index;
  2705. struct i40e_hw *hw = &vsi->back->hw;
  2706. struct i40e_hmc_obj_rxq rx_ctx;
  2707. i40e_status err = 0;
  2708. ring->state = 0;
  2709. /* clear the context structure first */
  2710. memset(&rx_ctx, 0, sizeof(rx_ctx));
  2711. ring->rx_buf_len = vsi->rx_buf_len;
  2712. rx_ctx.dbuff = DIV_ROUND_UP(ring->rx_buf_len,
  2713. BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
  2714. rx_ctx.base = (ring->dma / 128);
  2715. rx_ctx.qlen = ring->count;
  2716. /* use 32 byte descriptors */
  2717. rx_ctx.dsize = 1;
  2718. /* descriptor type is always zero
  2719. * rx_ctx.dtype = 0;
  2720. */
  2721. rx_ctx.hsplit_0 = 0;
  2722. rx_ctx.rxmax = min_t(u16, vsi->max_frame, chain_len * ring->rx_buf_len);
  2723. if (hw->revision_id == 0)
  2724. rx_ctx.lrxqthresh = 0;
  2725. else
  2726. rx_ctx.lrxqthresh = 2;
  2727. rx_ctx.crcstrip = 1;
  2728. rx_ctx.l2tsel = 1;
  2729. /* this controls whether VLAN is stripped from inner headers */
  2730. rx_ctx.showiv = 0;
  2731. /* set the prefena field to 1 because the manual says to */
  2732. rx_ctx.prefena = 1;
  2733. /* clear the context in the HMC */
  2734. err = i40e_clear_lan_rx_queue_context(hw, pf_q);
  2735. if (err) {
  2736. dev_info(&vsi->back->pdev->dev,
  2737. "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
  2738. ring->queue_index, pf_q, err);
  2739. return -ENOMEM;
  2740. }
  2741. /* set the context in the HMC */
  2742. err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
  2743. if (err) {
  2744. dev_info(&vsi->back->pdev->dev,
  2745. "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
  2746. ring->queue_index, pf_q, err);
  2747. return -ENOMEM;
  2748. }
  2749. /* configure Rx buffer alignment */
  2750. if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
  2751. clear_ring_build_skb_enabled(ring);
  2752. else
  2753. set_ring_build_skb_enabled(ring);
  2754. /* cache tail for quicker writes, and clear the reg before use */
  2755. ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
  2756. writel(0, ring->tail);
  2757. i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
  2758. return 0;
  2759. }
  2760. /**
  2761. * i40e_vsi_configure_tx - Configure the VSI for Tx
  2762. * @vsi: VSI structure describing this set of rings and resources
  2763. *
  2764. * Configure the Tx VSI for operation.
  2765. **/
  2766. static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
  2767. {
  2768. int err = 0;
  2769. u16 i;
  2770. for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
  2771. err = i40e_configure_tx_ring(vsi->tx_rings[i]);
  2772. if (!i40e_enabled_xdp_vsi(vsi))
  2773. return err;
  2774. for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
  2775. err = i40e_configure_tx_ring(vsi->xdp_rings[i]);
  2776. return err;
  2777. }
  2778. /**
  2779. * i40e_vsi_configure_rx - Configure the VSI for Rx
  2780. * @vsi: the VSI being configured
  2781. *
  2782. * Configure the Rx VSI for operation.
  2783. **/
  2784. static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
  2785. {
  2786. int err = 0;
  2787. u16 i;
  2788. if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX)) {
  2789. vsi->max_frame = I40E_MAX_RXBUFFER;
  2790. vsi->rx_buf_len = I40E_RXBUFFER_2048;
  2791. #if (PAGE_SIZE < 8192)
  2792. } else if (!I40E_2K_TOO_SMALL_WITH_PADDING &&
  2793. (vsi->netdev->mtu <= ETH_DATA_LEN)) {
  2794. vsi->max_frame = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
  2795. vsi->rx_buf_len = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
  2796. #endif
  2797. } else {
  2798. vsi->max_frame = I40E_MAX_RXBUFFER;
  2799. vsi->rx_buf_len = (PAGE_SIZE < 8192) ? I40E_RXBUFFER_3072 :
  2800. I40E_RXBUFFER_2048;
  2801. }
  2802. /* set up individual rings */
  2803. for (i = 0; i < vsi->num_queue_pairs && !err; i++)
  2804. err = i40e_configure_rx_ring(vsi->rx_rings[i]);
  2805. return err;
  2806. }
  2807. /**
  2808. * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
  2809. * @vsi: ptr to the VSI
  2810. **/
  2811. static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
  2812. {
  2813. struct i40e_ring *tx_ring, *rx_ring;
  2814. u16 qoffset, qcount;
  2815. int i, n;
  2816. if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
  2817. /* Reset the TC information */
  2818. for (i = 0; i < vsi->num_queue_pairs; i++) {
  2819. rx_ring = vsi->rx_rings[i];
  2820. tx_ring = vsi->tx_rings[i];
  2821. rx_ring->dcb_tc = 0;
  2822. tx_ring->dcb_tc = 0;
  2823. }
  2824. }
  2825. for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
  2826. if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
  2827. continue;
  2828. qoffset = vsi->tc_config.tc_info[n].qoffset;
  2829. qcount = vsi->tc_config.tc_info[n].qcount;
  2830. for (i = qoffset; i < (qoffset + qcount); i++) {
  2831. rx_ring = vsi->rx_rings[i];
  2832. tx_ring = vsi->tx_rings[i];
  2833. rx_ring->dcb_tc = n;
  2834. tx_ring->dcb_tc = n;
  2835. }
  2836. }
  2837. }
  2838. /**
  2839. * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
  2840. * @vsi: ptr to the VSI
  2841. **/
  2842. static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
  2843. {
  2844. struct i40e_pf *pf = vsi->back;
  2845. int err;
  2846. if (vsi->netdev)
  2847. i40e_set_rx_mode(vsi->netdev);
  2848. if (!!(pf->flags & I40E_FLAG_PF_MAC)) {
  2849. err = i40e_macaddr_init(vsi, pf->hw.mac.addr);
  2850. if (err) {
  2851. dev_warn(&pf->pdev->dev,
  2852. "could not set up macaddr; err %d\n", err);
  2853. }
  2854. }
  2855. }
  2856. /**
  2857. * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
  2858. * @vsi: Pointer to the targeted VSI
  2859. *
  2860. * This function replays the hlist on the hw where all the SB Flow Director
  2861. * filters were saved.
  2862. **/
  2863. static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
  2864. {
  2865. struct i40e_fdir_filter *filter;
  2866. struct i40e_pf *pf = vsi->back;
  2867. struct hlist_node *node;
  2868. if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
  2869. return;
  2870. /* Reset FDir counters as we're replaying all existing filters */
  2871. pf->fd_tcp4_filter_cnt = 0;
  2872. pf->fd_udp4_filter_cnt = 0;
  2873. pf->fd_sctp4_filter_cnt = 0;
  2874. pf->fd_ip4_filter_cnt = 0;
  2875. hlist_for_each_entry_safe(filter, node,
  2876. &pf->fdir_filter_list, fdir_node) {
  2877. i40e_add_del_fdir(vsi, filter, true);
  2878. }
  2879. }
  2880. /**
  2881. * i40e_vsi_configure - Set up the VSI for action
  2882. * @vsi: the VSI being configured
  2883. **/
  2884. static int i40e_vsi_configure(struct i40e_vsi *vsi)
  2885. {
  2886. int err;
  2887. i40e_set_vsi_rx_mode(vsi);
  2888. i40e_restore_vlan(vsi);
  2889. i40e_vsi_config_dcb_rings(vsi);
  2890. err = i40e_vsi_configure_tx(vsi);
  2891. if (!err)
  2892. err = i40e_vsi_configure_rx(vsi);
  2893. return err;
  2894. }
  2895. /**
  2896. * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
  2897. * @vsi: the VSI being configured
  2898. **/
  2899. static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
  2900. {
  2901. bool has_xdp = i40e_enabled_xdp_vsi(vsi);
  2902. struct i40e_pf *pf = vsi->back;
  2903. struct i40e_hw *hw = &pf->hw;
  2904. u16 vector;
  2905. int i, q;
  2906. u32 qp;
  2907. /* The interrupt indexing is offset by 1 in the PFINT_ITRn
  2908. * and PFINT_LNKLSTn registers, e.g.:
  2909. * PFINT_ITRn[0..n-1] gets msix-1..msix-n (qpair interrupts)
  2910. */
  2911. qp = vsi->base_queue;
  2912. vector = vsi->base_vector;
  2913. for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
  2914. struct i40e_q_vector *q_vector = vsi->q_vectors[i];
  2915. q_vector->itr_countdown = ITR_COUNTDOWN_START;
  2916. q_vector->rx.itr = ITR_TO_REG(vsi->rx_rings[i]->rx_itr_setting);
  2917. q_vector->rx.latency_range = I40E_LOW_LATENCY;
  2918. wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
  2919. q_vector->rx.itr);
  2920. q_vector->tx.itr = ITR_TO_REG(vsi->tx_rings[i]->tx_itr_setting);
  2921. q_vector->tx.latency_range = I40E_LOW_LATENCY;
  2922. wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
  2923. q_vector->tx.itr);
  2924. wr32(hw, I40E_PFINT_RATEN(vector - 1),
  2925. i40e_intrl_usec_to_reg(vsi->int_rate_limit));
  2926. /* Linked list for the queuepairs assigned to this vector */
  2927. wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
  2928. for (q = 0; q < q_vector->num_ringpairs; q++) {
  2929. u32 nextqp = has_xdp ? qp + vsi->alloc_queue_pairs : qp;
  2930. u32 val;
  2931. val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
  2932. (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
  2933. (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
  2934. (nextqp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) |
  2935. (I40E_QUEUE_TYPE_TX <<
  2936. I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
  2937. wr32(hw, I40E_QINT_RQCTL(qp), val);
  2938. if (has_xdp) {
  2939. val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  2940. (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
  2941. (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
  2942. (qp << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
  2943. (I40E_QUEUE_TYPE_TX <<
  2944. I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
  2945. wr32(hw, I40E_QINT_TQCTL(nextqp), val);
  2946. }
  2947. val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  2948. (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
  2949. (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
  2950. ((qp + 1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
  2951. (I40E_QUEUE_TYPE_RX <<
  2952. I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
  2953. /* Terminate the linked list */
  2954. if (q == (q_vector->num_ringpairs - 1))
  2955. val |= (I40E_QUEUE_END_OF_LIST <<
  2956. I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
  2957. wr32(hw, I40E_QINT_TQCTL(qp), val);
  2958. qp++;
  2959. }
  2960. }
  2961. i40e_flush(hw);
  2962. }
  2963. /**
  2964. * i40e_enable_misc_int_causes - enable the non-queue interrupts
  2965. * @hw: ptr to the hardware info
  2966. **/
  2967. static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
  2968. {
  2969. struct i40e_hw *hw = &pf->hw;
  2970. u32 val;
  2971. /* clear things first */
  2972. wr32(hw, I40E_PFINT_ICR0_ENA, 0); /* disable all */
  2973. rd32(hw, I40E_PFINT_ICR0); /* read to clear */
  2974. val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK |
  2975. I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK |
  2976. I40E_PFINT_ICR0_ENA_GRST_MASK |
  2977. I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
  2978. I40E_PFINT_ICR0_ENA_GPIO_MASK |
  2979. I40E_PFINT_ICR0_ENA_HMC_ERR_MASK |
  2980. I40E_PFINT_ICR0_ENA_VFLR_MASK |
  2981. I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
  2982. if (pf->flags & I40E_FLAG_IWARP_ENABLED)
  2983. val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
  2984. if (pf->flags & I40E_FLAG_PTP)
  2985. val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
  2986. wr32(hw, I40E_PFINT_ICR0_ENA, val);
  2987. /* SW_ITR_IDX = 0, but don't change INTENA */
  2988. wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
  2989. I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
  2990. /* OTHER_ITR_IDX = 0 */
  2991. wr32(hw, I40E_PFINT_STAT_CTL0, 0);
  2992. }
  2993. /**
  2994. * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
  2995. * @vsi: the VSI being configured
  2996. **/
  2997. static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
  2998. {
  2999. u32 nextqp = i40e_enabled_xdp_vsi(vsi) ? vsi->alloc_queue_pairs : 0;
  3000. struct i40e_q_vector *q_vector = vsi->q_vectors[0];
  3001. struct i40e_pf *pf = vsi->back;
  3002. struct i40e_hw *hw = &pf->hw;
  3003. u32 val;
  3004. /* set the ITR configuration */
  3005. q_vector->itr_countdown = ITR_COUNTDOWN_START;
  3006. q_vector->rx.itr = ITR_TO_REG(vsi->rx_rings[0]->rx_itr_setting);
  3007. q_vector->rx.latency_range = I40E_LOW_LATENCY;
  3008. wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.itr);
  3009. q_vector->tx.itr = ITR_TO_REG(vsi->tx_rings[0]->tx_itr_setting);
  3010. q_vector->tx.latency_range = I40E_LOW_LATENCY;
  3011. wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.itr);
  3012. i40e_enable_misc_int_causes(pf);
  3013. /* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
  3014. wr32(hw, I40E_PFINT_LNKLST0, 0);
  3015. /* Associate the queue pair to the vector and enable the queue int */
  3016. val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
  3017. (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
  3018. (nextqp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT)|
  3019. (I40E_QUEUE_TYPE_TX << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
  3020. wr32(hw, I40E_QINT_RQCTL(0), val);
  3021. if (i40e_enabled_xdp_vsi(vsi)) {
  3022. val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  3023. (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT)|
  3024. (I40E_QUEUE_TYPE_TX
  3025. << I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
  3026. wr32(hw, I40E_QINT_TQCTL(nextqp), val);
  3027. }
  3028. val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  3029. (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
  3030. (I40E_QUEUE_END_OF_LIST << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
  3031. wr32(hw, I40E_QINT_TQCTL(0), val);
  3032. i40e_flush(hw);
  3033. }
  3034. /**
  3035. * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
  3036. * @pf: board private structure
  3037. **/
  3038. void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
  3039. {
  3040. struct i40e_hw *hw = &pf->hw;
  3041. wr32(hw, I40E_PFINT_DYN_CTL0,
  3042. I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
  3043. i40e_flush(hw);
  3044. }
  3045. /**
  3046. * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
  3047. * @pf: board private structure
  3048. * @clearpba: true when all pending interrupt events should be cleared
  3049. **/
  3050. void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf, bool clearpba)
  3051. {
  3052. struct i40e_hw *hw = &pf->hw;
  3053. u32 val;
  3054. val = I40E_PFINT_DYN_CTL0_INTENA_MASK |
  3055. (clearpba ? I40E_PFINT_DYN_CTL0_CLEARPBA_MASK : 0) |
  3056. (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
  3057. wr32(hw, I40E_PFINT_DYN_CTL0, val);
  3058. i40e_flush(hw);
  3059. }
  3060. /**
  3061. * i40e_msix_clean_rings - MSIX mode Interrupt Handler
  3062. * @irq: interrupt number
  3063. * @data: pointer to a q_vector
  3064. **/
  3065. static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
  3066. {
  3067. struct i40e_q_vector *q_vector = data;
  3068. if (!q_vector->tx.ring && !q_vector->rx.ring)
  3069. return IRQ_HANDLED;
  3070. napi_schedule_irqoff(&q_vector->napi);
  3071. return IRQ_HANDLED;
  3072. }
  3073. /**
  3074. * i40e_irq_affinity_notify - Callback for affinity changes
  3075. * @notify: context as to what irq was changed
  3076. * @mask: the new affinity mask
  3077. *
  3078. * This is a callback function used by the irq_set_affinity_notifier function
  3079. * so that we may register to receive changes to the irq affinity masks.
  3080. **/
  3081. static void i40e_irq_affinity_notify(struct irq_affinity_notify *notify,
  3082. const cpumask_t *mask)
  3083. {
  3084. struct i40e_q_vector *q_vector =
  3085. container_of(notify, struct i40e_q_vector, affinity_notify);
  3086. q_vector->affinity_mask = *mask;
  3087. }
  3088. /**
  3089. * i40e_irq_affinity_release - Callback for affinity notifier release
  3090. * @ref: internal core kernel usage
  3091. *
  3092. * This is a callback function used by the irq_set_affinity_notifier function
  3093. * to inform the current notification subscriber that they will no longer
  3094. * receive notifications.
  3095. **/
  3096. static void i40e_irq_affinity_release(struct kref *ref) {}
  3097. /**
  3098. * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
  3099. * @vsi: the VSI being configured
  3100. * @basename: name for the vector
  3101. *
  3102. * Allocates MSI-X vectors and requests interrupts from the kernel.
  3103. **/
  3104. static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
  3105. {
  3106. int q_vectors = vsi->num_q_vectors;
  3107. struct i40e_pf *pf = vsi->back;
  3108. int base = vsi->base_vector;
  3109. int rx_int_idx = 0;
  3110. int tx_int_idx = 0;
  3111. int vector, err;
  3112. int irq_num;
  3113. for (vector = 0; vector < q_vectors; vector++) {
  3114. struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
  3115. irq_num = pf->msix_entries[base + vector].vector;
  3116. if (q_vector->tx.ring && q_vector->rx.ring) {
  3117. snprintf(q_vector->name, sizeof(q_vector->name) - 1,
  3118. "%s-%s-%d", basename, "TxRx", rx_int_idx++);
  3119. tx_int_idx++;
  3120. } else if (q_vector->rx.ring) {
  3121. snprintf(q_vector->name, sizeof(q_vector->name) - 1,
  3122. "%s-%s-%d", basename, "rx", rx_int_idx++);
  3123. } else if (q_vector->tx.ring) {
  3124. snprintf(q_vector->name, sizeof(q_vector->name) - 1,
  3125. "%s-%s-%d", basename, "tx", tx_int_idx++);
  3126. } else {
  3127. /* skip this unused q_vector */
  3128. continue;
  3129. }
  3130. err = request_irq(irq_num,
  3131. vsi->irq_handler,
  3132. 0,
  3133. q_vector->name,
  3134. q_vector);
  3135. if (err) {
  3136. dev_info(&pf->pdev->dev,
  3137. "MSIX request_irq failed, error: %d\n", err);
  3138. goto free_queue_irqs;
  3139. }
  3140. /* register for affinity change notifications */
  3141. q_vector->affinity_notify.notify = i40e_irq_affinity_notify;
  3142. q_vector->affinity_notify.release = i40e_irq_affinity_release;
  3143. irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
  3144. /* assign the mask for this irq */
  3145. irq_set_affinity_hint(irq_num, &q_vector->affinity_mask);
  3146. }
  3147. vsi->irqs_ready = true;
  3148. return 0;
  3149. free_queue_irqs:
  3150. while (vector) {
  3151. vector--;
  3152. irq_num = pf->msix_entries[base + vector].vector;
  3153. irq_set_affinity_notifier(irq_num, NULL);
  3154. irq_set_affinity_hint(irq_num, NULL);
  3155. free_irq(irq_num, &vsi->q_vectors[vector]);
  3156. }
  3157. return err;
  3158. }
  3159. /**
  3160. * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
  3161. * @vsi: the VSI being un-configured
  3162. **/
  3163. static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
  3164. {
  3165. struct i40e_pf *pf = vsi->back;
  3166. struct i40e_hw *hw = &pf->hw;
  3167. int base = vsi->base_vector;
  3168. int i;
  3169. /* disable interrupt causation from each queue */
  3170. for (i = 0; i < vsi->num_queue_pairs; i++) {
  3171. u32 val;
  3172. val = rd32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx));
  3173. val &= ~I40E_QINT_TQCTL_CAUSE_ENA_MASK;
  3174. wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), val);
  3175. val = rd32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx));
  3176. val &= ~I40E_QINT_RQCTL_CAUSE_ENA_MASK;
  3177. wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), val);
  3178. if (!i40e_enabled_xdp_vsi(vsi))
  3179. continue;
  3180. wr32(hw, I40E_QINT_TQCTL(vsi->xdp_rings[i]->reg_idx), 0);
  3181. }
  3182. /* disable each interrupt */
  3183. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  3184. for (i = vsi->base_vector;
  3185. i < (vsi->num_q_vectors + vsi->base_vector); i++)
  3186. wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
  3187. i40e_flush(hw);
  3188. for (i = 0; i < vsi->num_q_vectors; i++)
  3189. synchronize_irq(pf->msix_entries[i + base].vector);
  3190. } else {
  3191. /* Legacy and MSI mode - this stops all interrupt handling */
  3192. wr32(hw, I40E_PFINT_ICR0_ENA, 0);
  3193. wr32(hw, I40E_PFINT_DYN_CTL0, 0);
  3194. i40e_flush(hw);
  3195. synchronize_irq(pf->pdev->irq);
  3196. }
  3197. }
  3198. /**
  3199. * i40e_vsi_enable_irq - Enable IRQ for the given VSI
  3200. * @vsi: the VSI being configured
  3201. **/
  3202. static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
  3203. {
  3204. struct i40e_pf *pf = vsi->back;
  3205. int i;
  3206. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  3207. for (i = 0; i < vsi->num_q_vectors; i++)
  3208. i40e_irq_dynamic_enable(vsi, i);
  3209. } else {
  3210. i40e_irq_dynamic_enable_icr0(pf, true);
  3211. }
  3212. i40e_flush(&pf->hw);
  3213. return 0;
  3214. }
  3215. /**
  3216. * i40e_stop_misc_vector - Stop the vector that handles non-queue events
  3217. * @pf: board private structure
  3218. **/
  3219. static void i40e_stop_misc_vector(struct i40e_pf *pf)
  3220. {
  3221. /* Disable ICR 0 */
  3222. wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
  3223. i40e_flush(&pf->hw);
  3224. }
  3225. /**
  3226. * i40e_intr - MSI/Legacy and non-queue interrupt handler
  3227. * @irq: interrupt number
  3228. * @data: pointer to a q_vector
  3229. *
  3230. * This is the handler used for all MSI/Legacy interrupts, and deals
  3231. * with both queue and non-queue interrupts. This is also used in
  3232. * MSIX mode to handle the non-queue interrupts.
  3233. **/
  3234. static irqreturn_t i40e_intr(int irq, void *data)
  3235. {
  3236. struct i40e_pf *pf = (struct i40e_pf *)data;
  3237. struct i40e_hw *hw = &pf->hw;
  3238. irqreturn_t ret = IRQ_NONE;
  3239. u32 icr0, icr0_remaining;
  3240. u32 val, ena_mask;
  3241. icr0 = rd32(hw, I40E_PFINT_ICR0);
  3242. ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
  3243. /* if sharing a legacy IRQ, we might get called w/o an intr pending */
  3244. if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
  3245. goto enable_intr;
  3246. /* if interrupt but no bits showing, must be SWINT */
  3247. if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
  3248. (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
  3249. pf->sw_int_count++;
  3250. if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
  3251. (icr0 & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
  3252. ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
  3253. dev_dbg(&pf->pdev->dev, "cleared PE_CRITERR\n");
  3254. set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
  3255. }
  3256. /* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
  3257. if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
  3258. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  3259. struct i40e_q_vector *q_vector = vsi->q_vectors[0];
  3260. /* We do not have a way to disarm Queue causes while leaving
  3261. * interrupt enabled for all other causes, ideally
  3262. * interrupt should be disabled while we are in NAPI but
  3263. * this is not a performance path and napi_schedule()
  3264. * can deal with rescheduling.
  3265. */
  3266. if (!test_bit(__I40E_DOWN, pf->state))
  3267. napi_schedule_irqoff(&q_vector->napi);
  3268. }
  3269. if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
  3270. ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
  3271. set_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
  3272. i40e_debug(&pf->hw, I40E_DEBUG_NVM, "AdminQ event\n");
  3273. }
  3274. if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
  3275. ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
  3276. set_bit(__I40E_MDD_EVENT_PENDING, pf->state);
  3277. }
  3278. if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
  3279. ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
  3280. set_bit(__I40E_VFLR_EVENT_PENDING, pf->state);
  3281. }
  3282. if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
  3283. if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
  3284. set_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
  3285. ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
  3286. val = rd32(hw, I40E_GLGEN_RSTAT);
  3287. val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
  3288. >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
  3289. if (val == I40E_RESET_CORER) {
  3290. pf->corer_count++;
  3291. } else if (val == I40E_RESET_GLOBR) {
  3292. pf->globr_count++;
  3293. } else if (val == I40E_RESET_EMPR) {
  3294. pf->empr_count++;
  3295. set_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state);
  3296. }
  3297. }
  3298. if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
  3299. icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
  3300. dev_info(&pf->pdev->dev, "HMC error interrupt\n");
  3301. dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
  3302. rd32(hw, I40E_PFHMC_ERRORINFO),
  3303. rd32(hw, I40E_PFHMC_ERRORDATA));
  3304. }
  3305. if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
  3306. u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
  3307. if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK) {
  3308. icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
  3309. i40e_ptp_tx_hwtstamp(pf);
  3310. }
  3311. }
  3312. /* If a critical error is pending we have no choice but to reset the
  3313. * device.
  3314. * Report and mask out any remaining unexpected interrupts.
  3315. */
  3316. icr0_remaining = icr0 & ena_mask;
  3317. if (icr0_remaining) {
  3318. dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
  3319. icr0_remaining);
  3320. if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
  3321. (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
  3322. (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
  3323. dev_info(&pf->pdev->dev, "device will be reset\n");
  3324. set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
  3325. i40e_service_event_schedule(pf);
  3326. }
  3327. ena_mask &= ~icr0_remaining;
  3328. }
  3329. ret = IRQ_HANDLED;
  3330. enable_intr:
  3331. /* re-enable interrupt causes */
  3332. wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
  3333. if (!test_bit(__I40E_DOWN, pf->state)) {
  3334. i40e_service_event_schedule(pf);
  3335. i40e_irq_dynamic_enable_icr0(pf, false);
  3336. }
  3337. return ret;
  3338. }
  3339. /**
  3340. * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
  3341. * @tx_ring: tx ring to clean
  3342. * @budget: how many cleans we're allowed
  3343. *
  3344. * Returns true if there's any budget left (e.g. the clean is finished)
  3345. **/
  3346. static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
  3347. {
  3348. struct i40e_vsi *vsi = tx_ring->vsi;
  3349. u16 i = tx_ring->next_to_clean;
  3350. struct i40e_tx_buffer *tx_buf;
  3351. struct i40e_tx_desc *tx_desc;
  3352. tx_buf = &tx_ring->tx_bi[i];
  3353. tx_desc = I40E_TX_DESC(tx_ring, i);
  3354. i -= tx_ring->count;
  3355. do {
  3356. struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
  3357. /* if next_to_watch is not set then there is no work pending */
  3358. if (!eop_desc)
  3359. break;
  3360. /* prevent any other reads prior to eop_desc */
  3361. read_barrier_depends();
  3362. /* if the descriptor isn't done, no work yet to do */
  3363. if (!(eop_desc->cmd_type_offset_bsz &
  3364. cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
  3365. break;
  3366. /* clear next_to_watch to prevent false hangs */
  3367. tx_buf->next_to_watch = NULL;
  3368. tx_desc->buffer_addr = 0;
  3369. tx_desc->cmd_type_offset_bsz = 0;
  3370. /* move past filter desc */
  3371. tx_buf++;
  3372. tx_desc++;
  3373. i++;
  3374. if (unlikely(!i)) {
  3375. i -= tx_ring->count;
  3376. tx_buf = tx_ring->tx_bi;
  3377. tx_desc = I40E_TX_DESC(tx_ring, 0);
  3378. }
  3379. /* unmap skb header data */
  3380. dma_unmap_single(tx_ring->dev,
  3381. dma_unmap_addr(tx_buf, dma),
  3382. dma_unmap_len(tx_buf, len),
  3383. DMA_TO_DEVICE);
  3384. if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
  3385. kfree(tx_buf->raw_buf);
  3386. tx_buf->raw_buf = NULL;
  3387. tx_buf->tx_flags = 0;
  3388. tx_buf->next_to_watch = NULL;
  3389. dma_unmap_len_set(tx_buf, len, 0);
  3390. tx_desc->buffer_addr = 0;
  3391. tx_desc->cmd_type_offset_bsz = 0;
  3392. /* move us past the eop_desc for start of next FD desc */
  3393. tx_buf++;
  3394. tx_desc++;
  3395. i++;
  3396. if (unlikely(!i)) {
  3397. i -= tx_ring->count;
  3398. tx_buf = tx_ring->tx_bi;
  3399. tx_desc = I40E_TX_DESC(tx_ring, 0);
  3400. }
  3401. /* update budget accounting */
  3402. budget--;
  3403. } while (likely(budget));
  3404. i += tx_ring->count;
  3405. tx_ring->next_to_clean = i;
  3406. if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED)
  3407. i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
  3408. return budget > 0;
  3409. }
  3410. /**
  3411. * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
  3412. * @irq: interrupt number
  3413. * @data: pointer to a q_vector
  3414. **/
  3415. static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
  3416. {
  3417. struct i40e_q_vector *q_vector = data;
  3418. struct i40e_vsi *vsi;
  3419. if (!q_vector->tx.ring)
  3420. return IRQ_HANDLED;
  3421. vsi = q_vector->tx.ring->vsi;
  3422. i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
  3423. return IRQ_HANDLED;
  3424. }
  3425. /**
  3426. * i40e_map_vector_to_qp - Assigns the queue pair to the vector
  3427. * @vsi: the VSI being configured
  3428. * @v_idx: vector index
  3429. * @qp_idx: queue pair index
  3430. **/
  3431. static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
  3432. {
  3433. struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
  3434. struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
  3435. struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
  3436. tx_ring->q_vector = q_vector;
  3437. tx_ring->next = q_vector->tx.ring;
  3438. q_vector->tx.ring = tx_ring;
  3439. q_vector->tx.count++;
  3440. /* Place XDP Tx ring in the same q_vector ring list as regular Tx */
  3441. if (i40e_enabled_xdp_vsi(vsi)) {
  3442. struct i40e_ring *xdp_ring = vsi->xdp_rings[qp_idx];
  3443. xdp_ring->q_vector = q_vector;
  3444. xdp_ring->next = q_vector->tx.ring;
  3445. q_vector->tx.ring = xdp_ring;
  3446. q_vector->tx.count++;
  3447. }
  3448. rx_ring->q_vector = q_vector;
  3449. rx_ring->next = q_vector->rx.ring;
  3450. q_vector->rx.ring = rx_ring;
  3451. q_vector->rx.count++;
  3452. }
  3453. /**
  3454. * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
  3455. * @vsi: the VSI being configured
  3456. *
  3457. * This function maps descriptor rings to the queue-specific vectors
  3458. * we were allotted through the MSI-X enabling code. Ideally, we'd have
  3459. * one vector per queue pair, but on a constrained vector budget, we
  3460. * group the queue pairs as "efficiently" as possible.
  3461. **/
  3462. static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
  3463. {
  3464. int qp_remaining = vsi->num_queue_pairs;
  3465. int q_vectors = vsi->num_q_vectors;
  3466. int num_ringpairs;
  3467. int v_start = 0;
  3468. int qp_idx = 0;
  3469. /* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
  3470. * group them so there are multiple queues per vector.
  3471. * It is also important to go through all the vectors available to be
  3472. * sure that if we don't use all the vectors, that the remaining vectors
  3473. * are cleared. This is especially important when decreasing the
  3474. * number of queues in use.
  3475. */
  3476. for (; v_start < q_vectors; v_start++) {
  3477. struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
  3478. num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
  3479. q_vector->num_ringpairs = num_ringpairs;
  3480. q_vector->rx.count = 0;
  3481. q_vector->tx.count = 0;
  3482. q_vector->rx.ring = NULL;
  3483. q_vector->tx.ring = NULL;
  3484. while (num_ringpairs--) {
  3485. i40e_map_vector_to_qp(vsi, v_start, qp_idx);
  3486. qp_idx++;
  3487. qp_remaining--;
  3488. }
  3489. }
  3490. }
  3491. /**
  3492. * i40e_vsi_request_irq - Request IRQ from the OS
  3493. * @vsi: the VSI being configured
  3494. * @basename: name for the vector
  3495. **/
  3496. static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
  3497. {
  3498. struct i40e_pf *pf = vsi->back;
  3499. int err;
  3500. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  3501. err = i40e_vsi_request_irq_msix(vsi, basename);
  3502. else if (pf->flags & I40E_FLAG_MSI_ENABLED)
  3503. err = request_irq(pf->pdev->irq, i40e_intr, 0,
  3504. pf->int_name, pf);
  3505. else
  3506. err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
  3507. pf->int_name, pf);
  3508. if (err)
  3509. dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
  3510. return err;
  3511. }
  3512. #ifdef CONFIG_NET_POLL_CONTROLLER
  3513. /**
  3514. * i40e_netpoll - A Polling 'interrupt' handler
  3515. * @netdev: network interface device structure
  3516. *
  3517. * This is used by netconsole to send skbs without having to re-enable
  3518. * interrupts. It's not called while the normal interrupt routine is executing.
  3519. **/
  3520. static void i40e_netpoll(struct net_device *netdev)
  3521. {
  3522. struct i40e_netdev_priv *np = netdev_priv(netdev);
  3523. struct i40e_vsi *vsi = np->vsi;
  3524. struct i40e_pf *pf = vsi->back;
  3525. int i;
  3526. /* if interface is down do nothing */
  3527. if (test_bit(__I40E_VSI_DOWN, vsi->state))
  3528. return;
  3529. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  3530. for (i = 0; i < vsi->num_q_vectors; i++)
  3531. i40e_msix_clean_rings(0, vsi->q_vectors[i]);
  3532. } else {
  3533. i40e_intr(pf->pdev->irq, netdev);
  3534. }
  3535. }
  3536. #endif
  3537. #define I40E_QTX_ENA_WAIT_COUNT 50
  3538. /**
  3539. * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
  3540. * @pf: the PF being configured
  3541. * @pf_q: the PF queue
  3542. * @enable: enable or disable state of the queue
  3543. *
  3544. * This routine will wait for the given Tx queue of the PF to reach the
  3545. * enabled or disabled state.
  3546. * Returns -ETIMEDOUT in case of failing to reach the requested state after
  3547. * multiple retries; else will return 0 in case of success.
  3548. **/
  3549. static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
  3550. {
  3551. int i;
  3552. u32 tx_reg;
  3553. for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
  3554. tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
  3555. if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
  3556. break;
  3557. usleep_range(10, 20);
  3558. }
  3559. if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
  3560. return -ETIMEDOUT;
  3561. return 0;
  3562. }
  3563. /**
  3564. * i40e_control_tx_q - Start or stop a particular Tx queue
  3565. * @pf: the PF structure
  3566. * @pf_q: the PF queue to configure
  3567. * @enable: start or stop the queue
  3568. *
  3569. * This function enables or disables a single queue. Note that any delay
  3570. * required after the operation is expected to be handled by the caller of
  3571. * this function.
  3572. **/
  3573. static void i40e_control_tx_q(struct i40e_pf *pf, int pf_q, bool enable)
  3574. {
  3575. struct i40e_hw *hw = &pf->hw;
  3576. u32 tx_reg;
  3577. int i;
  3578. /* warn the TX unit of coming changes */
  3579. i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
  3580. if (!enable)
  3581. usleep_range(10, 20);
  3582. for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
  3583. tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
  3584. if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
  3585. ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
  3586. break;
  3587. usleep_range(1000, 2000);
  3588. }
  3589. /* Skip if the queue is already in the requested state */
  3590. if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
  3591. return;
  3592. /* turn on/off the queue */
  3593. if (enable) {
  3594. wr32(hw, I40E_QTX_HEAD(pf_q), 0);
  3595. tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
  3596. } else {
  3597. tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
  3598. }
  3599. wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
  3600. }
  3601. /**
  3602. * i40e_control_wait_tx_q - Start/stop Tx queue and wait for completion
  3603. * @seid: VSI SEID
  3604. * @pf: the PF structure
  3605. * @pf_q: the PF queue to configure
  3606. * @is_xdp: true if the queue is used for XDP
  3607. * @enable: start or stop the queue
  3608. **/
  3609. static int i40e_control_wait_tx_q(int seid, struct i40e_pf *pf, int pf_q,
  3610. bool is_xdp, bool enable)
  3611. {
  3612. int ret;
  3613. i40e_control_tx_q(pf, pf_q, enable);
  3614. /* wait for the change to finish */
  3615. ret = i40e_pf_txq_wait(pf, pf_q, enable);
  3616. if (ret) {
  3617. dev_info(&pf->pdev->dev,
  3618. "VSI seid %d %sTx ring %d %sable timeout\n",
  3619. seid, (is_xdp ? "XDP " : ""), pf_q,
  3620. (enable ? "en" : "dis"));
  3621. }
  3622. return ret;
  3623. }
  3624. /**
  3625. * i40e_vsi_control_tx - Start or stop a VSI's rings
  3626. * @vsi: the VSI being configured
  3627. * @enable: start or stop the rings
  3628. **/
  3629. static int i40e_vsi_control_tx(struct i40e_vsi *vsi, bool enable)
  3630. {
  3631. struct i40e_pf *pf = vsi->back;
  3632. int i, pf_q, ret = 0;
  3633. pf_q = vsi->base_queue;
  3634. for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
  3635. ret = i40e_control_wait_tx_q(vsi->seid, pf,
  3636. pf_q,
  3637. false /*is xdp*/, enable);
  3638. if (ret)
  3639. break;
  3640. if (!i40e_enabled_xdp_vsi(vsi))
  3641. continue;
  3642. ret = i40e_control_wait_tx_q(vsi->seid, pf,
  3643. pf_q + vsi->alloc_queue_pairs,
  3644. true /*is xdp*/, enable);
  3645. if (ret)
  3646. break;
  3647. }
  3648. return ret;
  3649. }
  3650. /**
  3651. * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
  3652. * @pf: the PF being configured
  3653. * @pf_q: the PF queue
  3654. * @enable: enable or disable state of the queue
  3655. *
  3656. * This routine will wait for the given Rx queue of the PF to reach the
  3657. * enabled or disabled state.
  3658. * Returns -ETIMEDOUT in case of failing to reach the requested state after
  3659. * multiple retries; else will return 0 in case of success.
  3660. **/
  3661. static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
  3662. {
  3663. int i;
  3664. u32 rx_reg;
  3665. for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
  3666. rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
  3667. if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
  3668. break;
  3669. usleep_range(10, 20);
  3670. }
  3671. if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
  3672. return -ETIMEDOUT;
  3673. return 0;
  3674. }
  3675. /**
  3676. * i40e_control_rx_q - Start or stop a particular Rx queue
  3677. * @pf: the PF structure
  3678. * @pf_q: the PF queue to configure
  3679. * @enable: start or stop the queue
  3680. *
  3681. * This function enables or disables a single queue. Note that any delay
  3682. * required after the operation is expected to be handled by the caller of
  3683. * this function.
  3684. **/
  3685. static void i40e_control_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
  3686. {
  3687. struct i40e_hw *hw = &pf->hw;
  3688. u32 rx_reg;
  3689. int i;
  3690. for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
  3691. rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
  3692. if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
  3693. ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
  3694. break;
  3695. usleep_range(1000, 2000);
  3696. }
  3697. /* Skip if the queue is already in the requested state */
  3698. if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
  3699. return;
  3700. /* turn on/off the queue */
  3701. if (enable)
  3702. rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
  3703. else
  3704. rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
  3705. wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
  3706. }
  3707. /**
  3708. * i40e_vsi_control_rx - Start or stop a VSI's rings
  3709. * @vsi: the VSI being configured
  3710. * @enable: start or stop the rings
  3711. **/
  3712. static int i40e_vsi_control_rx(struct i40e_vsi *vsi, bool enable)
  3713. {
  3714. struct i40e_pf *pf = vsi->back;
  3715. int i, pf_q, ret = 0;
  3716. pf_q = vsi->base_queue;
  3717. for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
  3718. i40e_control_rx_q(pf, pf_q, enable);
  3719. /* wait for the change to finish */
  3720. ret = i40e_pf_rxq_wait(pf, pf_q, enable);
  3721. if (ret) {
  3722. dev_info(&pf->pdev->dev,
  3723. "VSI seid %d Rx ring %d %sable timeout\n",
  3724. vsi->seid, pf_q, (enable ? "en" : "dis"));
  3725. break;
  3726. }
  3727. }
  3728. /* Due to HW errata, on Rx disable only, the register can indicate done
  3729. * before it really is. Needs 50ms to be sure
  3730. */
  3731. if (!enable)
  3732. mdelay(50);
  3733. return ret;
  3734. }
  3735. /**
  3736. * i40e_vsi_start_rings - Start a VSI's rings
  3737. * @vsi: the VSI being configured
  3738. **/
  3739. int i40e_vsi_start_rings(struct i40e_vsi *vsi)
  3740. {
  3741. int ret = 0;
  3742. /* do rx first for enable and last for disable */
  3743. ret = i40e_vsi_control_rx(vsi, true);
  3744. if (ret)
  3745. return ret;
  3746. ret = i40e_vsi_control_tx(vsi, true);
  3747. return ret;
  3748. }
  3749. /**
  3750. * i40e_vsi_stop_rings - Stop a VSI's rings
  3751. * @vsi: the VSI being configured
  3752. **/
  3753. void i40e_vsi_stop_rings(struct i40e_vsi *vsi)
  3754. {
  3755. /* When port TX is suspended, don't wait */
  3756. if (test_bit(__I40E_PORT_SUSPENDED, vsi->back->state))
  3757. return i40e_vsi_stop_rings_no_wait(vsi);
  3758. /* do rx first for enable and last for disable
  3759. * Ignore return value, we need to shutdown whatever we can
  3760. */
  3761. i40e_vsi_control_tx(vsi, false);
  3762. i40e_vsi_control_rx(vsi, false);
  3763. }
  3764. /**
  3765. * i40e_vsi_stop_rings_no_wait - Stop a VSI's rings and do not delay
  3766. * @vsi: the VSI being shutdown
  3767. *
  3768. * This function stops all the rings for a VSI but does not delay to verify
  3769. * that rings have been disabled. It is expected that the caller is shutting
  3770. * down multiple VSIs at once and will delay together for all the VSIs after
  3771. * initiating the shutdown. This is particularly useful for shutting down lots
  3772. * of VFs together. Otherwise, a large delay can be incurred while configuring
  3773. * each VSI in serial.
  3774. **/
  3775. void i40e_vsi_stop_rings_no_wait(struct i40e_vsi *vsi)
  3776. {
  3777. struct i40e_pf *pf = vsi->back;
  3778. int i, pf_q;
  3779. pf_q = vsi->base_queue;
  3780. for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
  3781. i40e_control_tx_q(pf, pf_q, false);
  3782. i40e_control_rx_q(pf, pf_q, false);
  3783. }
  3784. }
  3785. /**
  3786. * i40e_vsi_free_irq - Free the irq association with the OS
  3787. * @vsi: the VSI being configured
  3788. **/
  3789. static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
  3790. {
  3791. struct i40e_pf *pf = vsi->back;
  3792. struct i40e_hw *hw = &pf->hw;
  3793. int base = vsi->base_vector;
  3794. u32 val, qp;
  3795. int i;
  3796. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  3797. if (!vsi->q_vectors)
  3798. return;
  3799. if (!vsi->irqs_ready)
  3800. return;
  3801. vsi->irqs_ready = false;
  3802. for (i = 0; i < vsi->num_q_vectors; i++) {
  3803. int irq_num;
  3804. u16 vector;
  3805. vector = i + base;
  3806. irq_num = pf->msix_entries[vector].vector;
  3807. /* free only the irqs that were actually requested */
  3808. if (!vsi->q_vectors[i] ||
  3809. !vsi->q_vectors[i]->num_ringpairs)
  3810. continue;
  3811. /* clear the affinity notifier in the IRQ descriptor */
  3812. irq_set_affinity_notifier(irq_num, NULL);
  3813. /* clear the affinity_mask in the IRQ descriptor */
  3814. irq_set_affinity_hint(irq_num, NULL);
  3815. synchronize_irq(irq_num);
  3816. free_irq(irq_num, vsi->q_vectors[i]);
  3817. /* Tear down the interrupt queue link list
  3818. *
  3819. * We know that they come in pairs and always
  3820. * the Rx first, then the Tx. To clear the
  3821. * link list, stick the EOL value into the
  3822. * next_q field of the registers.
  3823. */
  3824. val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
  3825. qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
  3826. >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
  3827. val |= I40E_QUEUE_END_OF_LIST
  3828. << I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
  3829. wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
  3830. while (qp != I40E_QUEUE_END_OF_LIST) {
  3831. u32 next;
  3832. val = rd32(hw, I40E_QINT_RQCTL(qp));
  3833. val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
  3834. I40E_QINT_RQCTL_MSIX0_INDX_MASK |
  3835. I40E_QINT_RQCTL_CAUSE_ENA_MASK |
  3836. I40E_QINT_RQCTL_INTEVENT_MASK);
  3837. val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
  3838. I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
  3839. wr32(hw, I40E_QINT_RQCTL(qp), val);
  3840. val = rd32(hw, I40E_QINT_TQCTL(qp));
  3841. next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
  3842. >> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
  3843. val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
  3844. I40E_QINT_TQCTL_MSIX0_INDX_MASK |
  3845. I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  3846. I40E_QINT_TQCTL_INTEVENT_MASK);
  3847. val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
  3848. I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
  3849. wr32(hw, I40E_QINT_TQCTL(qp), val);
  3850. qp = next;
  3851. }
  3852. }
  3853. } else {
  3854. free_irq(pf->pdev->irq, pf);
  3855. val = rd32(hw, I40E_PFINT_LNKLST0);
  3856. qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
  3857. >> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
  3858. val |= I40E_QUEUE_END_OF_LIST
  3859. << I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
  3860. wr32(hw, I40E_PFINT_LNKLST0, val);
  3861. val = rd32(hw, I40E_QINT_RQCTL(qp));
  3862. val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK |
  3863. I40E_QINT_RQCTL_MSIX0_INDX_MASK |
  3864. I40E_QINT_RQCTL_CAUSE_ENA_MASK |
  3865. I40E_QINT_RQCTL_INTEVENT_MASK);
  3866. val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
  3867. I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
  3868. wr32(hw, I40E_QINT_RQCTL(qp), val);
  3869. val = rd32(hw, I40E_QINT_TQCTL(qp));
  3870. val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK |
  3871. I40E_QINT_TQCTL_MSIX0_INDX_MASK |
  3872. I40E_QINT_TQCTL_CAUSE_ENA_MASK |
  3873. I40E_QINT_TQCTL_INTEVENT_MASK);
  3874. val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
  3875. I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
  3876. wr32(hw, I40E_QINT_TQCTL(qp), val);
  3877. }
  3878. }
  3879. /**
  3880. * i40e_free_q_vector - Free memory allocated for specific interrupt vector
  3881. * @vsi: the VSI being configured
  3882. * @v_idx: Index of vector to be freed
  3883. *
  3884. * This function frees the memory allocated to the q_vector. In addition if
  3885. * NAPI is enabled it will delete any references to the NAPI struct prior
  3886. * to freeing the q_vector.
  3887. **/
  3888. static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
  3889. {
  3890. struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
  3891. struct i40e_ring *ring;
  3892. if (!q_vector)
  3893. return;
  3894. /* disassociate q_vector from rings */
  3895. i40e_for_each_ring(ring, q_vector->tx)
  3896. ring->q_vector = NULL;
  3897. i40e_for_each_ring(ring, q_vector->rx)
  3898. ring->q_vector = NULL;
  3899. /* only VSI w/ an associated netdev is set up w/ NAPI */
  3900. if (vsi->netdev)
  3901. netif_napi_del(&q_vector->napi);
  3902. vsi->q_vectors[v_idx] = NULL;
  3903. kfree_rcu(q_vector, rcu);
  3904. }
  3905. /**
  3906. * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
  3907. * @vsi: the VSI being un-configured
  3908. *
  3909. * This frees the memory allocated to the q_vectors and
  3910. * deletes references to the NAPI struct.
  3911. **/
  3912. static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
  3913. {
  3914. int v_idx;
  3915. for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
  3916. i40e_free_q_vector(vsi, v_idx);
  3917. }
  3918. /**
  3919. * i40e_reset_interrupt_capability - Disable interrupt setup in OS
  3920. * @pf: board private structure
  3921. **/
  3922. static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
  3923. {
  3924. /* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
  3925. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  3926. pci_disable_msix(pf->pdev);
  3927. kfree(pf->msix_entries);
  3928. pf->msix_entries = NULL;
  3929. kfree(pf->irq_pile);
  3930. pf->irq_pile = NULL;
  3931. } else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
  3932. pci_disable_msi(pf->pdev);
  3933. }
  3934. pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
  3935. }
  3936. /**
  3937. * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
  3938. * @pf: board private structure
  3939. *
  3940. * We go through and clear interrupt specific resources and reset the structure
  3941. * to pre-load conditions
  3942. **/
  3943. static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
  3944. {
  3945. int i;
  3946. i40e_stop_misc_vector(pf);
  3947. if (pf->flags & I40E_FLAG_MSIX_ENABLED && pf->msix_entries) {
  3948. synchronize_irq(pf->msix_entries[0].vector);
  3949. free_irq(pf->msix_entries[0].vector, pf);
  3950. }
  3951. i40e_put_lump(pf->irq_pile, pf->iwarp_base_vector,
  3952. I40E_IWARP_IRQ_PILE_ID);
  3953. i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
  3954. for (i = 0; i < pf->num_alloc_vsi; i++)
  3955. if (pf->vsi[i])
  3956. i40e_vsi_free_q_vectors(pf->vsi[i]);
  3957. i40e_reset_interrupt_capability(pf);
  3958. }
  3959. /**
  3960. * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
  3961. * @vsi: the VSI being configured
  3962. **/
  3963. static void i40e_napi_enable_all(struct i40e_vsi *vsi)
  3964. {
  3965. int q_idx;
  3966. if (!vsi->netdev)
  3967. return;
  3968. for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
  3969. struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
  3970. if (q_vector->rx.ring || q_vector->tx.ring)
  3971. napi_enable(&q_vector->napi);
  3972. }
  3973. }
  3974. /**
  3975. * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
  3976. * @vsi: the VSI being configured
  3977. **/
  3978. static void i40e_napi_disable_all(struct i40e_vsi *vsi)
  3979. {
  3980. int q_idx;
  3981. if (!vsi->netdev)
  3982. return;
  3983. for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
  3984. struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
  3985. if (q_vector->rx.ring || q_vector->tx.ring)
  3986. napi_disable(&q_vector->napi);
  3987. }
  3988. }
  3989. /**
  3990. * i40e_vsi_close - Shut down a VSI
  3991. * @vsi: the vsi to be quelled
  3992. **/
  3993. static void i40e_vsi_close(struct i40e_vsi *vsi)
  3994. {
  3995. struct i40e_pf *pf = vsi->back;
  3996. if (!test_and_set_bit(__I40E_VSI_DOWN, vsi->state))
  3997. i40e_down(vsi);
  3998. i40e_vsi_free_irq(vsi);
  3999. i40e_vsi_free_tx_resources(vsi);
  4000. i40e_vsi_free_rx_resources(vsi);
  4001. vsi->current_netdev_flags = 0;
  4002. pf->flags |= I40E_FLAG_SERVICE_CLIENT_REQUESTED;
  4003. if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
  4004. pf->flags |= I40E_FLAG_CLIENT_RESET;
  4005. }
  4006. /**
  4007. * i40e_quiesce_vsi - Pause a given VSI
  4008. * @vsi: the VSI being paused
  4009. **/
  4010. static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
  4011. {
  4012. if (test_bit(__I40E_VSI_DOWN, vsi->state))
  4013. return;
  4014. set_bit(__I40E_VSI_NEEDS_RESTART, vsi->state);
  4015. if (vsi->netdev && netif_running(vsi->netdev))
  4016. vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
  4017. else
  4018. i40e_vsi_close(vsi);
  4019. }
  4020. /**
  4021. * i40e_unquiesce_vsi - Resume a given VSI
  4022. * @vsi: the VSI being resumed
  4023. **/
  4024. static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
  4025. {
  4026. if (!test_and_clear_bit(__I40E_VSI_NEEDS_RESTART, vsi->state))
  4027. return;
  4028. if (vsi->netdev && netif_running(vsi->netdev))
  4029. vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
  4030. else
  4031. i40e_vsi_open(vsi); /* this clears the DOWN bit */
  4032. }
  4033. /**
  4034. * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
  4035. * @pf: the PF
  4036. **/
  4037. static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
  4038. {
  4039. int v;
  4040. for (v = 0; v < pf->num_alloc_vsi; v++) {
  4041. if (pf->vsi[v])
  4042. i40e_quiesce_vsi(pf->vsi[v]);
  4043. }
  4044. }
  4045. /**
  4046. * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
  4047. * @pf: the PF
  4048. **/
  4049. static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
  4050. {
  4051. int v;
  4052. for (v = 0; v < pf->num_alloc_vsi; v++) {
  4053. if (pf->vsi[v])
  4054. i40e_unquiesce_vsi(pf->vsi[v]);
  4055. }
  4056. }
  4057. /**
  4058. * i40e_vsi_wait_queues_disabled - Wait for VSI's queues to be disabled
  4059. * @vsi: the VSI being configured
  4060. *
  4061. * Wait until all queues on a given VSI have been disabled.
  4062. **/
  4063. int i40e_vsi_wait_queues_disabled(struct i40e_vsi *vsi)
  4064. {
  4065. struct i40e_pf *pf = vsi->back;
  4066. int i, pf_q, ret;
  4067. pf_q = vsi->base_queue;
  4068. for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
  4069. /* Check and wait for the Tx queue */
  4070. ret = i40e_pf_txq_wait(pf, pf_q, false);
  4071. if (ret) {
  4072. dev_info(&pf->pdev->dev,
  4073. "VSI seid %d Tx ring %d disable timeout\n",
  4074. vsi->seid, pf_q);
  4075. return ret;
  4076. }
  4077. if (!i40e_enabled_xdp_vsi(vsi))
  4078. goto wait_rx;
  4079. /* Check and wait for the XDP Tx queue */
  4080. ret = i40e_pf_txq_wait(pf, pf_q + vsi->alloc_queue_pairs,
  4081. false);
  4082. if (ret) {
  4083. dev_info(&pf->pdev->dev,
  4084. "VSI seid %d XDP Tx ring %d disable timeout\n",
  4085. vsi->seid, pf_q);
  4086. return ret;
  4087. }
  4088. wait_rx:
  4089. /* Check and wait for the Rx queue */
  4090. ret = i40e_pf_rxq_wait(pf, pf_q, false);
  4091. if (ret) {
  4092. dev_info(&pf->pdev->dev,
  4093. "VSI seid %d Rx ring %d disable timeout\n",
  4094. vsi->seid, pf_q);
  4095. return ret;
  4096. }
  4097. }
  4098. return 0;
  4099. }
  4100. #ifdef CONFIG_I40E_DCB
  4101. /**
  4102. * i40e_pf_wait_queues_disabled - Wait for all queues of PF VSIs to be disabled
  4103. * @pf: the PF
  4104. *
  4105. * This function waits for the queues to be in disabled state for all the
  4106. * VSIs that are managed by this PF.
  4107. **/
  4108. static int i40e_pf_wait_queues_disabled(struct i40e_pf *pf)
  4109. {
  4110. int v, ret = 0;
  4111. for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
  4112. if (pf->vsi[v]) {
  4113. ret = i40e_vsi_wait_queues_disabled(pf->vsi[v]);
  4114. if (ret)
  4115. break;
  4116. }
  4117. }
  4118. return ret;
  4119. }
  4120. #endif
  4121. /**
  4122. * i40e_detect_recover_hung_queue - Function to detect and recover hung_queue
  4123. * @q_idx: TX queue number
  4124. * @vsi: Pointer to VSI struct
  4125. *
  4126. * This function checks specified queue for given VSI. Detects hung condition.
  4127. * We proactively detect hung TX queues by checking if interrupts are disabled
  4128. * but there are pending descriptors. If it appears hung, attempt to recover
  4129. * by triggering a SW interrupt.
  4130. **/
  4131. static void i40e_detect_recover_hung_queue(int q_idx, struct i40e_vsi *vsi)
  4132. {
  4133. struct i40e_ring *tx_ring = NULL;
  4134. struct i40e_pf *pf;
  4135. u32 val, tx_pending;
  4136. int i;
  4137. pf = vsi->back;
  4138. /* now that we have an index, find the tx_ring struct */
  4139. for (i = 0; i < vsi->num_queue_pairs; i++) {
  4140. if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
  4141. if (q_idx == vsi->tx_rings[i]->queue_index) {
  4142. tx_ring = vsi->tx_rings[i];
  4143. break;
  4144. }
  4145. }
  4146. }
  4147. if (!tx_ring)
  4148. return;
  4149. /* Read interrupt register */
  4150. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  4151. val = rd32(&pf->hw,
  4152. I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
  4153. tx_ring->vsi->base_vector - 1));
  4154. else
  4155. val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
  4156. tx_pending = i40e_get_tx_pending(tx_ring);
  4157. /* Interrupts are disabled and TX pending is non-zero,
  4158. * trigger the SW interrupt (don't wait). Worst case
  4159. * there will be one extra interrupt which may result
  4160. * into not cleaning any queues because queues are cleaned.
  4161. */
  4162. if (tx_pending && (!(val & I40E_PFINT_DYN_CTLN_INTENA_MASK)))
  4163. i40e_force_wb(vsi, tx_ring->q_vector);
  4164. }
  4165. /**
  4166. * i40e_detect_recover_hung - Function to detect and recover hung_queues
  4167. * @pf: pointer to PF struct
  4168. *
  4169. * LAN VSI has netdev and netdev has TX queues. This function is to check
  4170. * each of those TX queues if they are hung, trigger recovery by issuing
  4171. * SW interrupt.
  4172. **/
  4173. static void i40e_detect_recover_hung(struct i40e_pf *pf)
  4174. {
  4175. struct net_device *netdev;
  4176. struct i40e_vsi *vsi;
  4177. unsigned int i;
  4178. /* Only for LAN VSI */
  4179. vsi = pf->vsi[pf->lan_vsi];
  4180. if (!vsi)
  4181. return;
  4182. /* Make sure, VSI state is not DOWN/RECOVERY_PENDING */
  4183. if (test_bit(__I40E_VSI_DOWN, vsi->back->state) ||
  4184. test_bit(__I40E_RESET_RECOVERY_PENDING, vsi->back->state))
  4185. return;
  4186. /* Make sure type is MAIN VSI */
  4187. if (vsi->type != I40E_VSI_MAIN)
  4188. return;
  4189. netdev = vsi->netdev;
  4190. if (!netdev)
  4191. return;
  4192. /* Bail out if netif_carrier is not OK */
  4193. if (!netif_carrier_ok(netdev))
  4194. return;
  4195. /* Go thru' TX queues for netdev */
  4196. for (i = 0; i < netdev->num_tx_queues; i++) {
  4197. struct netdev_queue *q;
  4198. q = netdev_get_tx_queue(netdev, i);
  4199. if (q)
  4200. i40e_detect_recover_hung_queue(i, vsi);
  4201. }
  4202. }
  4203. /**
  4204. * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
  4205. * @pf: pointer to PF
  4206. *
  4207. * Get TC map for ISCSI PF type that will include iSCSI TC
  4208. * and LAN TC.
  4209. **/
  4210. static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
  4211. {
  4212. struct i40e_dcb_app_priority_table app;
  4213. struct i40e_hw *hw = &pf->hw;
  4214. u8 enabled_tc = 1; /* TC0 is always enabled */
  4215. u8 tc, i;
  4216. /* Get the iSCSI APP TLV */
  4217. struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
  4218. for (i = 0; i < dcbcfg->numapps; i++) {
  4219. app = dcbcfg->app[i];
  4220. if (app.selector == I40E_APP_SEL_TCPIP &&
  4221. app.protocolid == I40E_APP_PROTOID_ISCSI) {
  4222. tc = dcbcfg->etscfg.prioritytable[app.priority];
  4223. enabled_tc |= BIT(tc);
  4224. break;
  4225. }
  4226. }
  4227. return enabled_tc;
  4228. }
  4229. /**
  4230. * i40e_dcb_get_num_tc - Get the number of TCs from DCBx config
  4231. * @dcbcfg: the corresponding DCBx configuration structure
  4232. *
  4233. * Return the number of TCs from given DCBx configuration
  4234. **/
  4235. static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
  4236. {
  4237. int i, tc_unused = 0;
  4238. u8 num_tc = 0;
  4239. u8 ret = 0;
  4240. /* Scan the ETS Config Priority Table to find
  4241. * traffic class enabled for a given priority
  4242. * and create a bitmask of enabled TCs
  4243. */
  4244. for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
  4245. num_tc |= BIT(dcbcfg->etscfg.prioritytable[i]);
  4246. /* Now scan the bitmask to check for
  4247. * contiguous TCs starting with TC0
  4248. */
  4249. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4250. if (num_tc & BIT(i)) {
  4251. if (!tc_unused) {
  4252. ret++;
  4253. } else {
  4254. pr_err("Non-contiguous TC - Disabling DCB\n");
  4255. return 1;
  4256. }
  4257. } else {
  4258. tc_unused = 1;
  4259. }
  4260. }
  4261. /* There is always at least TC0 */
  4262. if (!ret)
  4263. ret = 1;
  4264. return ret;
  4265. }
  4266. /**
  4267. * i40e_dcb_get_enabled_tc - Get enabled traffic classes
  4268. * @dcbcfg: the corresponding DCBx configuration structure
  4269. *
  4270. * Query the current DCB configuration and return the number of
  4271. * traffic classes enabled from the given DCBX config
  4272. **/
  4273. static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
  4274. {
  4275. u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
  4276. u8 enabled_tc = 1;
  4277. u8 i;
  4278. for (i = 0; i < num_tc; i++)
  4279. enabled_tc |= BIT(i);
  4280. return enabled_tc;
  4281. }
  4282. /**
  4283. * i40e_pf_get_num_tc - Get enabled traffic classes for PF
  4284. * @pf: PF being queried
  4285. *
  4286. * Return number of traffic classes enabled for the given PF
  4287. **/
  4288. static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
  4289. {
  4290. struct i40e_hw *hw = &pf->hw;
  4291. u8 i, enabled_tc = 1;
  4292. u8 num_tc = 0;
  4293. struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
  4294. /* If DCB is not enabled then always in single TC */
  4295. if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
  4296. return 1;
  4297. /* SFP mode will be enabled for all TCs on port */
  4298. if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
  4299. return i40e_dcb_get_num_tc(dcbcfg);
  4300. /* MFP mode return count of enabled TCs for this PF */
  4301. if (pf->hw.func_caps.iscsi)
  4302. enabled_tc = i40e_get_iscsi_tc_map(pf);
  4303. else
  4304. return 1; /* Only TC0 */
  4305. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4306. if (enabled_tc & BIT(i))
  4307. num_tc++;
  4308. }
  4309. return num_tc;
  4310. }
  4311. /**
  4312. * i40e_pf_get_pf_tc_map - Get bitmap for enabled traffic classes
  4313. * @pf: PF being queried
  4314. *
  4315. * Return a bitmap for enabled traffic classes for this PF.
  4316. **/
  4317. static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
  4318. {
  4319. /* If DCB is not enabled for this PF then just return default TC */
  4320. if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
  4321. return I40E_DEFAULT_TRAFFIC_CLASS;
  4322. /* SFP mode we want PF to be enabled for all TCs */
  4323. if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
  4324. return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
  4325. /* MFP enabled and iSCSI PF type */
  4326. if (pf->hw.func_caps.iscsi)
  4327. return i40e_get_iscsi_tc_map(pf);
  4328. else
  4329. return I40E_DEFAULT_TRAFFIC_CLASS;
  4330. }
  4331. /**
  4332. * i40e_vsi_get_bw_info - Query VSI BW Information
  4333. * @vsi: the VSI being queried
  4334. *
  4335. * Returns 0 on success, negative value on failure
  4336. **/
  4337. static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
  4338. {
  4339. struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
  4340. struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
  4341. struct i40e_pf *pf = vsi->back;
  4342. struct i40e_hw *hw = &pf->hw;
  4343. i40e_status ret;
  4344. u32 tc_bw_max;
  4345. int i;
  4346. /* Get the VSI level BW configuration */
  4347. ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
  4348. if (ret) {
  4349. dev_info(&pf->pdev->dev,
  4350. "couldn't get PF vsi bw config, err %s aq_err %s\n",
  4351. i40e_stat_str(&pf->hw, ret),
  4352. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  4353. return -EINVAL;
  4354. }
  4355. /* Get the VSI level BW configuration per TC */
  4356. ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
  4357. NULL);
  4358. if (ret) {
  4359. dev_info(&pf->pdev->dev,
  4360. "couldn't get PF vsi ets bw config, err %s aq_err %s\n",
  4361. i40e_stat_str(&pf->hw, ret),
  4362. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  4363. return -EINVAL;
  4364. }
  4365. if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
  4366. dev_info(&pf->pdev->dev,
  4367. "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
  4368. bw_config.tc_valid_bits,
  4369. bw_ets_config.tc_valid_bits);
  4370. /* Still continuing */
  4371. }
  4372. vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
  4373. vsi->bw_max_quanta = bw_config.max_bw;
  4374. tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
  4375. (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
  4376. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4377. vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
  4378. vsi->bw_ets_limit_credits[i] =
  4379. le16_to_cpu(bw_ets_config.credits[i]);
  4380. /* 3 bits out of 4 for each TC */
  4381. vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
  4382. }
  4383. return 0;
  4384. }
  4385. /**
  4386. * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
  4387. * @vsi: the VSI being configured
  4388. * @enabled_tc: TC bitmap
  4389. * @bw_credits: BW shared credits per TC
  4390. *
  4391. * Returns 0 on success, negative value on failure
  4392. **/
  4393. static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
  4394. u8 *bw_share)
  4395. {
  4396. struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
  4397. i40e_status ret;
  4398. int i;
  4399. bw_data.tc_valid_bits = enabled_tc;
  4400. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
  4401. bw_data.tc_bw_credits[i] = bw_share[i];
  4402. ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, vsi->seid, &bw_data,
  4403. NULL);
  4404. if (ret) {
  4405. dev_info(&vsi->back->pdev->dev,
  4406. "AQ command Config VSI BW allocation per TC failed = %d\n",
  4407. vsi->back->hw.aq.asq_last_status);
  4408. return -EINVAL;
  4409. }
  4410. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
  4411. vsi->info.qs_handle[i] = bw_data.qs_handles[i];
  4412. return 0;
  4413. }
  4414. /**
  4415. * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
  4416. * @vsi: the VSI being configured
  4417. * @enabled_tc: TC map to be enabled
  4418. *
  4419. **/
  4420. static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
  4421. {
  4422. struct net_device *netdev = vsi->netdev;
  4423. struct i40e_pf *pf = vsi->back;
  4424. struct i40e_hw *hw = &pf->hw;
  4425. u8 netdev_tc = 0;
  4426. int i;
  4427. struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
  4428. if (!netdev)
  4429. return;
  4430. if (!enabled_tc) {
  4431. netdev_reset_tc(netdev);
  4432. return;
  4433. }
  4434. /* Set up actual enabled TCs on the VSI */
  4435. if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
  4436. return;
  4437. /* set per TC queues for the VSI */
  4438. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4439. /* Only set TC queues for enabled tcs
  4440. *
  4441. * e.g. For a VSI that has TC0 and TC3 enabled the
  4442. * enabled_tc bitmap would be 0x00001001; the driver
  4443. * will set the numtc for netdev as 2 that will be
  4444. * referenced by the netdev layer as TC 0 and 1.
  4445. */
  4446. if (vsi->tc_config.enabled_tc & BIT(i))
  4447. netdev_set_tc_queue(netdev,
  4448. vsi->tc_config.tc_info[i].netdev_tc,
  4449. vsi->tc_config.tc_info[i].qcount,
  4450. vsi->tc_config.tc_info[i].qoffset);
  4451. }
  4452. /* Assign UP2TC map for the VSI */
  4453. for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
  4454. /* Get the actual TC# for the UP */
  4455. u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
  4456. /* Get the mapped netdev TC# for the UP */
  4457. netdev_tc = vsi->tc_config.tc_info[ets_tc].netdev_tc;
  4458. netdev_set_prio_tc_map(netdev, i, netdev_tc);
  4459. }
  4460. }
  4461. /**
  4462. * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
  4463. * @vsi: the VSI being configured
  4464. * @ctxt: the ctxt buffer returned from AQ VSI update param command
  4465. **/
  4466. static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
  4467. struct i40e_vsi_context *ctxt)
  4468. {
  4469. /* copy just the sections touched not the entire info
  4470. * since not all sections are valid as returned by
  4471. * update vsi params
  4472. */
  4473. vsi->info.mapping_flags = ctxt->info.mapping_flags;
  4474. memcpy(&vsi->info.queue_mapping,
  4475. &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
  4476. memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
  4477. sizeof(vsi->info.tc_mapping));
  4478. }
  4479. /**
  4480. * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
  4481. * @vsi: VSI to be configured
  4482. * @enabled_tc: TC bitmap
  4483. *
  4484. * This configures a particular VSI for TCs that are mapped to the
  4485. * given TC bitmap. It uses default bandwidth share for TCs across
  4486. * VSIs to configure TC for a particular VSI.
  4487. *
  4488. * NOTE:
  4489. * It is expected that the VSI queues have been quisced before calling
  4490. * this function.
  4491. **/
  4492. static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
  4493. {
  4494. u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
  4495. struct i40e_vsi_context ctxt;
  4496. int ret = 0;
  4497. int i;
  4498. /* Check if enabled_tc is same as existing or new TCs */
  4499. if (vsi->tc_config.enabled_tc == enabled_tc)
  4500. return ret;
  4501. /* Enable ETS TCs with equal BW Share for now across all VSIs */
  4502. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4503. if (enabled_tc & BIT(i))
  4504. bw_share[i] = 1;
  4505. }
  4506. ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
  4507. if (ret) {
  4508. dev_info(&vsi->back->pdev->dev,
  4509. "Failed configuring TC map %d for VSI %d\n",
  4510. enabled_tc, vsi->seid);
  4511. goto out;
  4512. }
  4513. /* Update Queue Pairs Mapping for currently enabled UPs */
  4514. ctxt.seid = vsi->seid;
  4515. ctxt.pf_num = vsi->back->hw.pf_id;
  4516. ctxt.vf_num = 0;
  4517. ctxt.uplink_seid = vsi->uplink_seid;
  4518. ctxt.info = vsi->info;
  4519. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
  4520. if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
  4521. ctxt.info.valid_sections |=
  4522. cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
  4523. ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
  4524. }
  4525. /* Update the VSI after updating the VSI queue-mapping information */
  4526. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  4527. if (ret) {
  4528. dev_info(&vsi->back->pdev->dev,
  4529. "Update vsi tc config failed, err %s aq_err %s\n",
  4530. i40e_stat_str(&vsi->back->hw, ret),
  4531. i40e_aq_str(&vsi->back->hw,
  4532. vsi->back->hw.aq.asq_last_status));
  4533. goto out;
  4534. }
  4535. /* update the local VSI info with updated queue map */
  4536. i40e_vsi_update_queue_map(vsi, &ctxt);
  4537. vsi->info.valid_sections = 0;
  4538. /* Update current VSI BW information */
  4539. ret = i40e_vsi_get_bw_info(vsi);
  4540. if (ret) {
  4541. dev_info(&vsi->back->pdev->dev,
  4542. "Failed updating vsi bw info, err %s aq_err %s\n",
  4543. i40e_stat_str(&vsi->back->hw, ret),
  4544. i40e_aq_str(&vsi->back->hw,
  4545. vsi->back->hw.aq.asq_last_status));
  4546. goto out;
  4547. }
  4548. /* Update the netdev TC setup */
  4549. i40e_vsi_config_netdev_tc(vsi, enabled_tc);
  4550. out:
  4551. return ret;
  4552. }
  4553. /**
  4554. * i40e_veb_config_tc - Configure TCs for given VEB
  4555. * @veb: given VEB
  4556. * @enabled_tc: TC bitmap
  4557. *
  4558. * Configures given TC bitmap for VEB (switching) element
  4559. **/
  4560. int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
  4561. {
  4562. struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
  4563. struct i40e_pf *pf = veb->pf;
  4564. int ret = 0;
  4565. int i;
  4566. /* No TCs or already enabled TCs just return */
  4567. if (!enabled_tc || veb->enabled_tc == enabled_tc)
  4568. return ret;
  4569. bw_data.tc_valid_bits = enabled_tc;
  4570. /* bw_data.absolute_credits is not set (relative) */
  4571. /* Enable ETS TCs with equal BW Share for now */
  4572. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  4573. if (enabled_tc & BIT(i))
  4574. bw_data.tc_bw_share_credits[i] = 1;
  4575. }
  4576. ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
  4577. &bw_data, NULL);
  4578. if (ret) {
  4579. dev_info(&pf->pdev->dev,
  4580. "VEB bw config failed, err %s aq_err %s\n",
  4581. i40e_stat_str(&pf->hw, ret),
  4582. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  4583. goto out;
  4584. }
  4585. /* Update the BW information */
  4586. ret = i40e_veb_get_bw_info(veb);
  4587. if (ret) {
  4588. dev_info(&pf->pdev->dev,
  4589. "Failed getting veb bw config, err %s aq_err %s\n",
  4590. i40e_stat_str(&pf->hw, ret),
  4591. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  4592. }
  4593. out:
  4594. return ret;
  4595. }
  4596. #ifdef CONFIG_I40E_DCB
  4597. /**
  4598. * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
  4599. * @pf: PF struct
  4600. *
  4601. * Reconfigure VEB/VSIs on a given PF; it is assumed that
  4602. * the caller would've quiesce all the VSIs before calling
  4603. * this function
  4604. **/
  4605. static void i40e_dcb_reconfigure(struct i40e_pf *pf)
  4606. {
  4607. u8 tc_map = 0;
  4608. int ret;
  4609. u8 v;
  4610. /* Enable the TCs available on PF to all VEBs */
  4611. tc_map = i40e_pf_get_tc_map(pf);
  4612. for (v = 0; v < I40E_MAX_VEB; v++) {
  4613. if (!pf->veb[v])
  4614. continue;
  4615. ret = i40e_veb_config_tc(pf->veb[v], tc_map);
  4616. if (ret) {
  4617. dev_info(&pf->pdev->dev,
  4618. "Failed configuring TC for VEB seid=%d\n",
  4619. pf->veb[v]->seid);
  4620. /* Will try to configure as many components */
  4621. }
  4622. }
  4623. /* Update each VSI */
  4624. for (v = 0; v < pf->num_alloc_vsi; v++) {
  4625. if (!pf->vsi[v])
  4626. continue;
  4627. /* - Enable all TCs for the LAN VSI
  4628. * - For all others keep them at TC0 for now
  4629. */
  4630. if (v == pf->lan_vsi)
  4631. tc_map = i40e_pf_get_tc_map(pf);
  4632. else
  4633. tc_map = I40E_DEFAULT_TRAFFIC_CLASS;
  4634. ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
  4635. if (ret) {
  4636. dev_info(&pf->pdev->dev,
  4637. "Failed configuring TC for VSI seid=%d\n",
  4638. pf->vsi[v]->seid);
  4639. /* Will try to configure as many components */
  4640. } else {
  4641. /* Re-configure VSI vectors based on updated TC map */
  4642. i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
  4643. if (pf->vsi[v]->netdev)
  4644. i40e_dcbnl_set_all(pf->vsi[v]);
  4645. }
  4646. }
  4647. }
  4648. /**
  4649. * i40e_resume_port_tx - Resume port Tx
  4650. * @pf: PF struct
  4651. *
  4652. * Resume a port's Tx and issue a PF reset in case of failure to
  4653. * resume.
  4654. **/
  4655. static int i40e_resume_port_tx(struct i40e_pf *pf)
  4656. {
  4657. struct i40e_hw *hw = &pf->hw;
  4658. int ret;
  4659. ret = i40e_aq_resume_port_tx(hw, NULL);
  4660. if (ret) {
  4661. dev_info(&pf->pdev->dev,
  4662. "Resume Port Tx failed, err %s aq_err %s\n",
  4663. i40e_stat_str(&pf->hw, ret),
  4664. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  4665. /* Schedule PF reset to recover */
  4666. set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
  4667. i40e_service_event_schedule(pf);
  4668. }
  4669. return ret;
  4670. }
  4671. /**
  4672. * i40e_init_pf_dcb - Initialize DCB configuration
  4673. * @pf: PF being configured
  4674. *
  4675. * Query the current DCB configuration and cache it
  4676. * in the hardware structure
  4677. **/
  4678. static int i40e_init_pf_dcb(struct i40e_pf *pf)
  4679. {
  4680. struct i40e_hw *hw = &pf->hw;
  4681. int err = 0;
  4682. /* Do not enable DCB for SW1 and SW2 images even if the FW is capable */
  4683. if (pf->flags & I40E_FLAG_NO_DCB_SUPPORT)
  4684. goto out;
  4685. /* Get the initial DCB configuration */
  4686. err = i40e_init_dcb(hw);
  4687. if (!err) {
  4688. /* Device/Function is not DCBX capable */
  4689. if ((!hw->func_caps.dcb) ||
  4690. (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
  4691. dev_info(&pf->pdev->dev,
  4692. "DCBX offload is not supported or is disabled for this PF.\n");
  4693. } else {
  4694. /* When status is not DISABLED then DCBX in FW */
  4695. pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
  4696. DCB_CAP_DCBX_VER_IEEE;
  4697. pf->flags |= I40E_FLAG_DCB_CAPABLE;
  4698. /* Enable DCB tagging only when more than one TC
  4699. * or explicitly disable if only one TC
  4700. */
  4701. if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
  4702. pf->flags |= I40E_FLAG_DCB_ENABLED;
  4703. else
  4704. pf->flags &= ~I40E_FLAG_DCB_ENABLED;
  4705. dev_dbg(&pf->pdev->dev,
  4706. "DCBX offload is supported for this PF.\n");
  4707. }
  4708. } else {
  4709. dev_info(&pf->pdev->dev,
  4710. "Query for DCB configuration failed, err %s aq_err %s\n",
  4711. i40e_stat_str(&pf->hw, err),
  4712. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  4713. }
  4714. out:
  4715. return err;
  4716. }
  4717. #endif /* CONFIG_I40E_DCB */
  4718. #define SPEED_SIZE 14
  4719. #define FC_SIZE 8
  4720. /**
  4721. * i40e_print_link_message - print link up or down
  4722. * @vsi: the VSI for which link needs a message
  4723. */
  4724. void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
  4725. {
  4726. enum i40e_aq_link_speed new_speed;
  4727. char *speed = "Unknown";
  4728. char *fc = "Unknown";
  4729. char *fec = "";
  4730. char *an = "";
  4731. new_speed = vsi->back->hw.phy.link_info.link_speed;
  4732. if ((vsi->current_isup == isup) && (vsi->current_speed == new_speed))
  4733. return;
  4734. vsi->current_isup = isup;
  4735. vsi->current_speed = new_speed;
  4736. if (!isup) {
  4737. netdev_info(vsi->netdev, "NIC Link is Down\n");
  4738. return;
  4739. }
  4740. /* Warn user if link speed on NPAR enabled partition is not at
  4741. * least 10GB
  4742. */
  4743. if (vsi->back->hw.func_caps.npar_enable &&
  4744. (vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
  4745. vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
  4746. netdev_warn(vsi->netdev,
  4747. "The partition detected link speed that is less than 10Gbps\n");
  4748. switch (vsi->back->hw.phy.link_info.link_speed) {
  4749. case I40E_LINK_SPEED_40GB:
  4750. speed = "40 G";
  4751. break;
  4752. case I40E_LINK_SPEED_20GB:
  4753. speed = "20 G";
  4754. break;
  4755. case I40E_LINK_SPEED_25GB:
  4756. speed = "25 G";
  4757. break;
  4758. case I40E_LINK_SPEED_10GB:
  4759. speed = "10 G";
  4760. break;
  4761. case I40E_LINK_SPEED_1GB:
  4762. speed = "1000 M";
  4763. break;
  4764. case I40E_LINK_SPEED_100MB:
  4765. speed = "100 M";
  4766. break;
  4767. default:
  4768. break;
  4769. }
  4770. switch (vsi->back->hw.fc.current_mode) {
  4771. case I40E_FC_FULL:
  4772. fc = "RX/TX";
  4773. break;
  4774. case I40E_FC_TX_PAUSE:
  4775. fc = "TX";
  4776. break;
  4777. case I40E_FC_RX_PAUSE:
  4778. fc = "RX";
  4779. break;
  4780. default:
  4781. fc = "None";
  4782. break;
  4783. }
  4784. if (vsi->back->hw.phy.link_info.link_speed == I40E_LINK_SPEED_25GB) {
  4785. fec = ", FEC: None";
  4786. an = ", Autoneg: False";
  4787. if (vsi->back->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
  4788. an = ", Autoneg: True";
  4789. if (vsi->back->hw.phy.link_info.fec_info &
  4790. I40E_AQ_CONFIG_FEC_KR_ENA)
  4791. fec = ", FEC: CL74 FC-FEC/BASE-R";
  4792. else if (vsi->back->hw.phy.link_info.fec_info &
  4793. I40E_AQ_CONFIG_FEC_RS_ENA)
  4794. fec = ", FEC: CL108 RS-FEC";
  4795. }
  4796. netdev_info(vsi->netdev, "NIC Link is Up, %sbps Full Duplex%s%s, Flow Control: %s\n",
  4797. speed, fec, an, fc);
  4798. }
  4799. /**
  4800. * i40e_up_complete - Finish the last steps of bringing up a connection
  4801. * @vsi: the VSI being configured
  4802. **/
  4803. static int i40e_up_complete(struct i40e_vsi *vsi)
  4804. {
  4805. struct i40e_pf *pf = vsi->back;
  4806. int err;
  4807. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  4808. i40e_vsi_configure_msix(vsi);
  4809. else
  4810. i40e_configure_msi_and_legacy(vsi);
  4811. /* start rings */
  4812. err = i40e_vsi_start_rings(vsi);
  4813. if (err)
  4814. return err;
  4815. clear_bit(__I40E_VSI_DOWN, vsi->state);
  4816. i40e_napi_enable_all(vsi);
  4817. i40e_vsi_enable_irq(vsi);
  4818. if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
  4819. (vsi->netdev)) {
  4820. i40e_print_link_message(vsi, true);
  4821. netif_tx_start_all_queues(vsi->netdev);
  4822. netif_carrier_on(vsi->netdev);
  4823. } else if (vsi->netdev) {
  4824. i40e_print_link_message(vsi, false);
  4825. /* need to check for qualified module here*/
  4826. if ((pf->hw.phy.link_info.link_info &
  4827. I40E_AQ_MEDIA_AVAILABLE) &&
  4828. (!(pf->hw.phy.link_info.an_info &
  4829. I40E_AQ_QUALIFIED_MODULE)))
  4830. netdev_err(vsi->netdev,
  4831. "the driver failed to link because an unqualified module was detected.");
  4832. }
  4833. /* replay FDIR SB filters */
  4834. if (vsi->type == I40E_VSI_FDIR) {
  4835. /* reset fd counters */
  4836. pf->fd_add_err = 0;
  4837. pf->fd_atr_cnt = 0;
  4838. i40e_fdir_filter_restore(vsi);
  4839. }
  4840. /* On the next run of the service_task, notify any clients of the new
  4841. * opened netdev
  4842. */
  4843. pf->flags |= I40E_FLAG_SERVICE_CLIENT_REQUESTED;
  4844. i40e_service_event_schedule(pf);
  4845. return 0;
  4846. }
  4847. /**
  4848. * i40e_vsi_reinit_locked - Reset the VSI
  4849. * @vsi: the VSI being configured
  4850. *
  4851. * Rebuild the ring structs after some configuration
  4852. * has changed, e.g. MTU size.
  4853. **/
  4854. static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
  4855. {
  4856. struct i40e_pf *pf = vsi->back;
  4857. WARN_ON(in_interrupt());
  4858. while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state))
  4859. usleep_range(1000, 2000);
  4860. i40e_down(vsi);
  4861. i40e_up(vsi);
  4862. clear_bit(__I40E_CONFIG_BUSY, pf->state);
  4863. }
  4864. /**
  4865. * i40e_up - Bring the connection back up after being down
  4866. * @vsi: the VSI being configured
  4867. **/
  4868. int i40e_up(struct i40e_vsi *vsi)
  4869. {
  4870. int err;
  4871. err = i40e_vsi_configure(vsi);
  4872. if (!err)
  4873. err = i40e_up_complete(vsi);
  4874. return err;
  4875. }
  4876. /**
  4877. * i40e_down - Shutdown the connection processing
  4878. * @vsi: the VSI being stopped
  4879. **/
  4880. void i40e_down(struct i40e_vsi *vsi)
  4881. {
  4882. int i;
  4883. /* It is assumed that the caller of this function
  4884. * sets the vsi->state __I40E_VSI_DOWN bit.
  4885. */
  4886. if (vsi->netdev) {
  4887. netif_carrier_off(vsi->netdev);
  4888. netif_tx_disable(vsi->netdev);
  4889. }
  4890. i40e_vsi_disable_irq(vsi);
  4891. i40e_vsi_stop_rings(vsi);
  4892. i40e_napi_disable_all(vsi);
  4893. for (i = 0; i < vsi->num_queue_pairs; i++) {
  4894. i40e_clean_tx_ring(vsi->tx_rings[i]);
  4895. if (i40e_enabled_xdp_vsi(vsi))
  4896. i40e_clean_tx_ring(vsi->xdp_rings[i]);
  4897. i40e_clean_rx_ring(vsi->rx_rings[i]);
  4898. }
  4899. }
  4900. /**
  4901. * i40e_setup_tc - configure multiple traffic classes
  4902. * @netdev: net device to configure
  4903. * @tc: number of traffic classes to enable
  4904. **/
  4905. static int i40e_setup_tc(struct net_device *netdev, u8 tc)
  4906. {
  4907. struct i40e_netdev_priv *np = netdev_priv(netdev);
  4908. struct i40e_vsi *vsi = np->vsi;
  4909. struct i40e_pf *pf = vsi->back;
  4910. u8 enabled_tc = 0;
  4911. int ret = -EINVAL;
  4912. int i;
  4913. /* Check if DCB enabled to continue */
  4914. if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
  4915. netdev_info(netdev, "DCB is not enabled for adapter\n");
  4916. goto exit;
  4917. }
  4918. /* Check if MFP enabled */
  4919. if (pf->flags & I40E_FLAG_MFP_ENABLED) {
  4920. netdev_info(netdev, "Configuring TC not supported in MFP mode\n");
  4921. goto exit;
  4922. }
  4923. /* Check whether tc count is within enabled limit */
  4924. if (tc > i40e_pf_get_num_tc(pf)) {
  4925. netdev_info(netdev, "TC count greater than enabled on link for adapter\n");
  4926. goto exit;
  4927. }
  4928. /* Generate TC map for number of tc requested */
  4929. for (i = 0; i < tc; i++)
  4930. enabled_tc |= BIT(i);
  4931. /* Requesting same TC configuration as already enabled */
  4932. if (enabled_tc == vsi->tc_config.enabled_tc)
  4933. return 0;
  4934. /* Quiesce VSI queues */
  4935. i40e_quiesce_vsi(vsi);
  4936. /* Configure VSI for enabled TCs */
  4937. ret = i40e_vsi_config_tc(vsi, enabled_tc);
  4938. if (ret) {
  4939. netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
  4940. vsi->seid);
  4941. goto exit;
  4942. }
  4943. /* Unquiesce VSI */
  4944. i40e_unquiesce_vsi(vsi);
  4945. exit:
  4946. return ret;
  4947. }
  4948. static int __i40e_setup_tc(struct net_device *netdev, enum tc_setup_type type,
  4949. u32 handle, u32 chain_index, __be16 proto,
  4950. struct tc_to_netdev *tc)
  4951. {
  4952. if (type != TC_SETUP_MQPRIO)
  4953. return -EINVAL;
  4954. tc->mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
  4955. return i40e_setup_tc(netdev, tc->mqprio->num_tc);
  4956. }
  4957. /**
  4958. * i40e_open - Called when a network interface is made active
  4959. * @netdev: network interface device structure
  4960. *
  4961. * The open entry point is called when a network interface is made
  4962. * active by the system (IFF_UP). At this point all resources needed
  4963. * for transmit and receive operations are allocated, the interrupt
  4964. * handler is registered with the OS, the netdev watchdog subtask is
  4965. * enabled, and the stack is notified that the interface is ready.
  4966. *
  4967. * Returns 0 on success, negative value on failure
  4968. **/
  4969. int i40e_open(struct net_device *netdev)
  4970. {
  4971. struct i40e_netdev_priv *np = netdev_priv(netdev);
  4972. struct i40e_vsi *vsi = np->vsi;
  4973. struct i40e_pf *pf = vsi->back;
  4974. int err;
  4975. /* disallow open during test or if eeprom is broken */
  4976. if (test_bit(__I40E_TESTING, pf->state) ||
  4977. test_bit(__I40E_BAD_EEPROM, pf->state))
  4978. return -EBUSY;
  4979. netif_carrier_off(netdev);
  4980. err = i40e_vsi_open(vsi);
  4981. if (err)
  4982. return err;
  4983. /* configure global TSO hardware offload settings */
  4984. wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
  4985. TCP_FLAG_FIN) >> 16);
  4986. wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
  4987. TCP_FLAG_FIN |
  4988. TCP_FLAG_CWR) >> 16);
  4989. wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
  4990. udp_tunnel_get_rx_info(netdev);
  4991. return 0;
  4992. }
  4993. /**
  4994. * i40e_vsi_open -
  4995. * @vsi: the VSI to open
  4996. *
  4997. * Finish initialization of the VSI.
  4998. *
  4999. * Returns 0 on success, negative value on failure
  5000. *
  5001. * Note: expects to be called while under rtnl_lock()
  5002. **/
  5003. int i40e_vsi_open(struct i40e_vsi *vsi)
  5004. {
  5005. struct i40e_pf *pf = vsi->back;
  5006. char int_name[I40E_INT_NAME_STR_LEN];
  5007. int err;
  5008. /* allocate descriptors */
  5009. err = i40e_vsi_setup_tx_resources(vsi);
  5010. if (err)
  5011. goto err_setup_tx;
  5012. err = i40e_vsi_setup_rx_resources(vsi);
  5013. if (err)
  5014. goto err_setup_rx;
  5015. err = i40e_vsi_configure(vsi);
  5016. if (err)
  5017. goto err_setup_rx;
  5018. if (vsi->netdev) {
  5019. snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
  5020. dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
  5021. err = i40e_vsi_request_irq(vsi, int_name);
  5022. if (err)
  5023. goto err_setup_rx;
  5024. /* Notify the stack of the actual queue counts. */
  5025. err = netif_set_real_num_tx_queues(vsi->netdev,
  5026. vsi->num_queue_pairs);
  5027. if (err)
  5028. goto err_set_queues;
  5029. err = netif_set_real_num_rx_queues(vsi->netdev,
  5030. vsi->num_queue_pairs);
  5031. if (err)
  5032. goto err_set_queues;
  5033. } else if (vsi->type == I40E_VSI_FDIR) {
  5034. snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
  5035. dev_driver_string(&pf->pdev->dev),
  5036. dev_name(&pf->pdev->dev));
  5037. err = i40e_vsi_request_irq(vsi, int_name);
  5038. } else {
  5039. err = -EINVAL;
  5040. goto err_setup_rx;
  5041. }
  5042. err = i40e_up_complete(vsi);
  5043. if (err)
  5044. goto err_up_complete;
  5045. return 0;
  5046. err_up_complete:
  5047. i40e_down(vsi);
  5048. err_set_queues:
  5049. i40e_vsi_free_irq(vsi);
  5050. err_setup_rx:
  5051. i40e_vsi_free_rx_resources(vsi);
  5052. err_setup_tx:
  5053. i40e_vsi_free_tx_resources(vsi);
  5054. if (vsi == pf->vsi[pf->lan_vsi])
  5055. i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED), true);
  5056. return err;
  5057. }
  5058. /**
  5059. * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
  5060. * @pf: Pointer to PF
  5061. *
  5062. * This function destroys the hlist where all the Flow Director
  5063. * filters were saved.
  5064. **/
  5065. static void i40e_fdir_filter_exit(struct i40e_pf *pf)
  5066. {
  5067. struct i40e_fdir_filter *filter;
  5068. struct i40e_flex_pit *pit_entry, *tmp;
  5069. struct hlist_node *node2;
  5070. hlist_for_each_entry_safe(filter, node2,
  5071. &pf->fdir_filter_list, fdir_node) {
  5072. hlist_del(&filter->fdir_node);
  5073. kfree(filter);
  5074. }
  5075. list_for_each_entry_safe(pit_entry, tmp, &pf->l3_flex_pit_list, list) {
  5076. list_del(&pit_entry->list);
  5077. kfree(pit_entry);
  5078. }
  5079. INIT_LIST_HEAD(&pf->l3_flex_pit_list);
  5080. list_for_each_entry_safe(pit_entry, tmp, &pf->l4_flex_pit_list, list) {
  5081. list_del(&pit_entry->list);
  5082. kfree(pit_entry);
  5083. }
  5084. INIT_LIST_HEAD(&pf->l4_flex_pit_list);
  5085. pf->fdir_pf_active_filters = 0;
  5086. pf->fd_tcp4_filter_cnt = 0;
  5087. pf->fd_udp4_filter_cnt = 0;
  5088. pf->fd_sctp4_filter_cnt = 0;
  5089. pf->fd_ip4_filter_cnt = 0;
  5090. /* Reprogram the default input set for TCP/IPv4 */
  5091. i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
  5092. I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
  5093. I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
  5094. /* Reprogram the default input set for UDP/IPv4 */
  5095. i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_UDP,
  5096. I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
  5097. I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
  5098. /* Reprogram the default input set for SCTP/IPv4 */
  5099. i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_SCTP,
  5100. I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
  5101. I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
  5102. /* Reprogram the default input set for Other/IPv4 */
  5103. i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_OTHER,
  5104. I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
  5105. }
  5106. /**
  5107. * i40e_close - Disables a network interface
  5108. * @netdev: network interface device structure
  5109. *
  5110. * The close entry point is called when an interface is de-activated
  5111. * by the OS. The hardware is still under the driver's control, but
  5112. * this netdev interface is disabled.
  5113. *
  5114. * Returns 0, this is not allowed to fail
  5115. **/
  5116. int i40e_close(struct net_device *netdev)
  5117. {
  5118. struct i40e_netdev_priv *np = netdev_priv(netdev);
  5119. struct i40e_vsi *vsi = np->vsi;
  5120. i40e_vsi_close(vsi);
  5121. return 0;
  5122. }
  5123. /**
  5124. * i40e_do_reset - Start a PF or Core Reset sequence
  5125. * @pf: board private structure
  5126. * @reset_flags: which reset is requested
  5127. * @lock_acquired: indicates whether or not the lock has been acquired
  5128. * before this function was called.
  5129. *
  5130. * The essential difference in resets is that the PF Reset
  5131. * doesn't clear the packet buffers, doesn't reset the PE
  5132. * firmware, and doesn't bother the other PFs on the chip.
  5133. **/
  5134. void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags, bool lock_acquired)
  5135. {
  5136. u32 val;
  5137. WARN_ON(in_interrupt());
  5138. /* do the biggest reset indicated */
  5139. if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
  5140. /* Request a Global Reset
  5141. *
  5142. * This will start the chip's countdown to the actual full
  5143. * chip reset event, and a warning interrupt to be sent
  5144. * to all PFs, including the requestor. Our handler
  5145. * for the warning interrupt will deal with the shutdown
  5146. * and recovery of the switch setup.
  5147. */
  5148. dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
  5149. val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
  5150. val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
  5151. wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
  5152. } else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
  5153. /* Request a Core Reset
  5154. *
  5155. * Same as Global Reset, except does *not* include the MAC/PHY
  5156. */
  5157. dev_dbg(&pf->pdev->dev, "CoreR requested\n");
  5158. val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
  5159. val |= I40E_GLGEN_RTRIG_CORER_MASK;
  5160. wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
  5161. i40e_flush(&pf->hw);
  5162. } else if (reset_flags & BIT_ULL(__I40E_PF_RESET_REQUESTED)) {
  5163. /* Request a PF Reset
  5164. *
  5165. * Resets only the PF-specific registers
  5166. *
  5167. * This goes directly to the tear-down and rebuild of
  5168. * the switch, since we need to do all the recovery as
  5169. * for the Core Reset.
  5170. */
  5171. dev_dbg(&pf->pdev->dev, "PFR requested\n");
  5172. i40e_handle_reset_warning(pf, lock_acquired);
  5173. } else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
  5174. int v;
  5175. /* Find the VSI(s) that requested a re-init */
  5176. dev_info(&pf->pdev->dev,
  5177. "VSI reinit requested\n");
  5178. for (v = 0; v < pf->num_alloc_vsi; v++) {
  5179. struct i40e_vsi *vsi = pf->vsi[v];
  5180. if (vsi != NULL &&
  5181. test_and_clear_bit(__I40E_VSI_REINIT_REQUESTED,
  5182. vsi->state))
  5183. i40e_vsi_reinit_locked(pf->vsi[v]);
  5184. }
  5185. } else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
  5186. int v;
  5187. /* Find the VSI(s) that needs to be brought down */
  5188. dev_info(&pf->pdev->dev, "VSI down requested\n");
  5189. for (v = 0; v < pf->num_alloc_vsi; v++) {
  5190. struct i40e_vsi *vsi = pf->vsi[v];
  5191. if (vsi != NULL &&
  5192. test_and_clear_bit(__I40E_VSI_DOWN_REQUESTED,
  5193. vsi->state)) {
  5194. set_bit(__I40E_VSI_DOWN, vsi->state);
  5195. i40e_down(vsi);
  5196. }
  5197. }
  5198. } else {
  5199. dev_info(&pf->pdev->dev,
  5200. "bad reset request 0x%08x\n", reset_flags);
  5201. }
  5202. }
  5203. #ifdef CONFIG_I40E_DCB
  5204. /**
  5205. * i40e_dcb_need_reconfig - Check if DCB needs reconfig
  5206. * @pf: board private structure
  5207. * @old_cfg: current DCB config
  5208. * @new_cfg: new DCB config
  5209. **/
  5210. bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
  5211. struct i40e_dcbx_config *old_cfg,
  5212. struct i40e_dcbx_config *new_cfg)
  5213. {
  5214. bool need_reconfig = false;
  5215. /* Check if ETS configuration has changed */
  5216. if (memcmp(&new_cfg->etscfg,
  5217. &old_cfg->etscfg,
  5218. sizeof(new_cfg->etscfg))) {
  5219. /* If Priority Table has changed reconfig is needed */
  5220. if (memcmp(&new_cfg->etscfg.prioritytable,
  5221. &old_cfg->etscfg.prioritytable,
  5222. sizeof(new_cfg->etscfg.prioritytable))) {
  5223. need_reconfig = true;
  5224. dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
  5225. }
  5226. if (memcmp(&new_cfg->etscfg.tcbwtable,
  5227. &old_cfg->etscfg.tcbwtable,
  5228. sizeof(new_cfg->etscfg.tcbwtable)))
  5229. dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
  5230. if (memcmp(&new_cfg->etscfg.tsatable,
  5231. &old_cfg->etscfg.tsatable,
  5232. sizeof(new_cfg->etscfg.tsatable)))
  5233. dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
  5234. }
  5235. /* Check if PFC configuration has changed */
  5236. if (memcmp(&new_cfg->pfc,
  5237. &old_cfg->pfc,
  5238. sizeof(new_cfg->pfc))) {
  5239. need_reconfig = true;
  5240. dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
  5241. }
  5242. /* Check if APP Table has changed */
  5243. if (memcmp(&new_cfg->app,
  5244. &old_cfg->app,
  5245. sizeof(new_cfg->app))) {
  5246. need_reconfig = true;
  5247. dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
  5248. }
  5249. dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
  5250. return need_reconfig;
  5251. }
  5252. /**
  5253. * i40e_handle_lldp_event - Handle LLDP Change MIB event
  5254. * @pf: board private structure
  5255. * @e: event info posted on ARQ
  5256. **/
  5257. static int i40e_handle_lldp_event(struct i40e_pf *pf,
  5258. struct i40e_arq_event_info *e)
  5259. {
  5260. struct i40e_aqc_lldp_get_mib *mib =
  5261. (struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
  5262. struct i40e_hw *hw = &pf->hw;
  5263. struct i40e_dcbx_config tmp_dcbx_cfg;
  5264. bool need_reconfig = false;
  5265. int ret = 0;
  5266. u8 type;
  5267. /* Not DCB capable or capability disabled */
  5268. if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
  5269. return ret;
  5270. /* Ignore if event is not for Nearest Bridge */
  5271. type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
  5272. & I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
  5273. dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
  5274. if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
  5275. return ret;
  5276. /* Check MIB Type and return if event for Remote MIB update */
  5277. type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
  5278. dev_dbg(&pf->pdev->dev,
  5279. "LLDP event mib type %s\n", type ? "remote" : "local");
  5280. if (type == I40E_AQ_LLDP_MIB_REMOTE) {
  5281. /* Update the remote cached instance and return */
  5282. ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
  5283. I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
  5284. &hw->remote_dcbx_config);
  5285. goto exit;
  5286. }
  5287. /* Store the old configuration */
  5288. tmp_dcbx_cfg = hw->local_dcbx_config;
  5289. /* Reset the old DCBx configuration data */
  5290. memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
  5291. /* Get updated DCBX data from firmware */
  5292. ret = i40e_get_dcb_config(&pf->hw);
  5293. if (ret) {
  5294. dev_info(&pf->pdev->dev,
  5295. "Failed querying DCB configuration data from firmware, err %s aq_err %s\n",
  5296. i40e_stat_str(&pf->hw, ret),
  5297. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  5298. goto exit;
  5299. }
  5300. /* No change detected in DCBX configs */
  5301. if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
  5302. sizeof(tmp_dcbx_cfg))) {
  5303. dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
  5304. goto exit;
  5305. }
  5306. need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
  5307. &hw->local_dcbx_config);
  5308. i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
  5309. if (!need_reconfig)
  5310. goto exit;
  5311. /* Enable DCB tagging only when more than one TC */
  5312. if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
  5313. pf->flags |= I40E_FLAG_DCB_ENABLED;
  5314. else
  5315. pf->flags &= ~I40E_FLAG_DCB_ENABLED;
  5316. set_bit(__I40E_PORT_SUSPENDED, pf->state);
  5317. /* Reconfiguration needed quiesce all VSIs */
  5318. i40e_pf_quiesce_all_vsi(pf);
  5319. /* Changes in configuration update VEB/VSI */
  5320. i40e_dcb_reconfigure(pf);
  5321. ret = i40e_resume_port_tx(pf);
  5322. clear_bit(__I40E_PORT_SUSPENDED, pf->state);
  5323. /* In case of error no point in resuming VSIs */
  5324. if (ret)
  5325. goto exit;
  5326. /* Wait for the PF's queues to be disabled */
  5327. ret = i40e_pf_wait_queues_disabled(pf);
  5328. if (ret) {
  5329. /* Schedule PF reset to recover */
  5330. set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
  5331. i40e_service_event_schedule(pf);
  5332. } else {
  5333. i40e_pf_unquiesce_all_vsi(pf);
  5334. pf->flags |= (I40E_FLAG_SERVICE_CLIENT_REQUESTED |
  5335. I40E_FLAG_CLIENT_L2_CHANGE);
  5336. }
  5337. exit:
  5338. return ret;
  5339. }
  5340. #endif /* CONFIG_I40E_DCB */
  5341. /**
  5342. * i40e_do_reset_safe - Protected reset path for userland calls.
  5343. * @pf: board private structure
  5344. * @reset_flags: which reset is requested
  5345. *
  5346. **/
  5347. void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
  5348. {
  5349. rtnl_lock();
  5350. i40e_do_reset(pf, reset_flags, true);
  5351. rtnl_unlock();
  5352. }
  5353. /**
  5354. * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
  5355. * @pf: board private structure
  5356. * @e: event info posted on ARQ
  5357. *
  5358. * Handler for LAN Queue Overflow Event generated by the firmware for PF
  5359. * and VF queues
  5360. **/
  5361. static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
  5362. struct i40e_arq_event_info *e)
  5363. {
  5364. struct i40e_aqc_lan_overflow *data =
  5365. (struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
  5366. u32 queue = le32_to_cpu(data->prtdcb_rupto);
  5367. u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
  5368. struct i40e_hw *hw = &pf->hw;
  5369. struct i40e_vf *vf;
  5370. u16 vf_id;
  5371. dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
  5372. queue, qtx_ctl);
  5373. /* Queue belongs to VF, find the VF and issue VF reset */
  5374. if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
  5375. >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
  5376. vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
  5377. >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
  5378. vf_id -= hw->func_caps.vf_base_id;
  5379. vf = &pf->vf[vf_id];
  5380. i40e_vc_notify_vf_reset(vf);
  5381. /* Allow VF to process pending reset notification */
  5382. msleep(20);
  5383. i40e_reset_vf(vf, false);
  5384. }
  5385. }
  5386. /**
  5387. * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
  5388. * @pf: board private structure
  5389. **/
  5390. u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
  5391. {
  5392. u32 val, fcnt_prog;
  5393. val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
  5394. fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
  5395. return fcnt_prog;
  5396. }
  5397. /**
  5398. * i40e_get_current_fd_count - Get total FD filters programmed for this PF
  5399. * @pf: board private structure
  5400. **/
  5401. u32 i40e_get_current_fd_count(struct i40e_pf *pf)
  5402. {
  5403. u32 val, fcnt_prog;
  5404. val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
  5405. fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
  5406. ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
  5407. I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
  5408. return fcnt_prog;
  5409. }
  5410. /**
  5411. * i40e_get_global_fd_count - Get total FD filters programmed on device
  5412. * @pf: board private structure
  5413. **/
  5414. u32 i40e_get_global_fd_count(struct i40e_pf *pf)
  5415. {
  5416. u32 val, fcnt_prog;
  5417. val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
  5418. fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
  5419. ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >>
  5420. I40E_GLQF_FDCNT_0_BESTCNT_SHIFT);
  5421. return fcnt_prog;
  5422. }
  5423. /**
  5424. * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
  5425. * @pf: board private structure
  5426. **/
  5427. void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
  5428. {
  5429. struct i40e_fdir_filter *filter;
  5430. u32 fcnt_prog, fcnt_avail;
  5431. struct hlist_node *node;
  5432. if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
  5433. return;
  5434. /* Check if we have enough room to re-enable FDir SB capability. */
  5435. fcnt_prog = i40e_get_global_fd_count(pf);
  5436. fcnt_avail = pf->fdir_pf_filter_count;
  5437. if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
  5438. (pf->fd_add_err == 0) ||
  5439. (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt)) {
  5440. if (pf->flags & I40E_FLAG_FD_SB_AUTO_DISABLED) {
  5441. pf->flags &= ~I40E_FLAG_FD_SB_AUTO_DISABLED;
  5442. if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
  5443. (I40E_DEBUG_FD & pf->hw.debug_mask))
  5444. dev_info(&pf->pdev->dev, "FD Sideband/ntuple is being enabled since we have space in the table now\n");
  5445. }
  5446. }
  5447. /* We should wait for even more space before re-enabling ATR.
  5448. * Additionally, we cannot enable ATR as long as we still have TCP SB
  5449. * rules active.
  5450. */
  5451. if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) &&
  5452. (pf->fd_tcp4_filter_cnt == 0)) {
  5453. if (pf->flags & I40E_FLAG_FD_ATR_AUTO_DISABLED) {
  5454. pf->flags &= ~I40E_FLAG_FD_ATR_AUTO_DISABLED;
  5455. if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
  5456. (I40E_DEBUG_FD & pf->hw.debug_mask))
  5457. dev_info(&pf->pdev->dev, "ATR is being enabled since we have space in the table and there are no conflicting ntuple rules\n");
  5458. }
  5459. }
  5460. /* if hw had a problem adding a filter, delete it */
  5461. if (pf->fd_inv > 0) {
  5462. hlist_for_each_entry_safe(filter, node,
  5463. &pf->fdir_filter_list, fdir_node) {
  5464. if (filter->fd_id == pf->fd_inv) {
  5465. hlist_del(&filter->fdir_node);
  5466. kfree(filter);
  5467. pf->fdir_pf_active_filters--;
  5468. }
  5469. }
  5470. }
  5471. }
  5472. #define I40E_MIN_FD_FLUSH_INTERVAL 10
  5473. #define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
  5474. /**
  5475. * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
  5476. * @pf: board private structure
  5477. **/
  5478. static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
  5479. {
  5480. unsigned long min_flush_time;
  5481. int flush_wait_retry = 50;
  5482. bool disable_atr = false;
  5483. int fd_room;
  5484. int reg;
  5485. if (!time_after(jiffies, pf->fd_flush_timestamp +
  5486. (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
  5487. return;
  5488. /* If the flush is happening too quick and we have mostly SB rules we
  5489. * should not re-enable ATR for some time.
  5490. */
  5491. min_flush_time = pf->fd_flush_timestamp +
  5492. (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
  5493. fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
  5494. if (!(time_after(jiffies, min_flush_time)) &&
  5495. (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
  5496. if (I40E_DEBUG_FD & pf->hw.debug_mask)
  5497. dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
  5498. disable_atr = true;
  5499. }
  5500. pf->fd_flush_timestamp = jiffies;
  5501. pf->flags |= I40E_FLAG_FD_ATR_AUTO_DISABLED;
  5502. /* flush all filters */
  5503. wr32(&pf->hw, I40E_PFQF_CTL_1,
  5504. I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
  5505. i40e_flush(&pf->hw);
  5506. pf->fd_flush_cnt++;
  5507. pf->fd_add_err = 0;
  5508. do {
  5509. /* Check FD flush status every 5-6msec */
  5510. usleep_range(5000, 6000);
  5511. reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
  5512. if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
  5513. break;
  5514. } while (flush_wait_retry--);
  5515. if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
  5516. dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
  5517. } else {
  5518. /* replay sideband filters */
  5519. i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
  5520. if (!disable_atr && !pf->fd_tcp4_filter_cnt)
  5521. pf->flags &= ~I40E_FLAG_FD_ATR_AUTO_DISABLED;
  5522. clear_bit(__I40E_FD_FLUSH_REQUESTED, pf->state);
  5523. if (I40E_DEBUG_FD & pf->hw.debug_mask)
  5524. dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
  5525. }
  5526. }
  5527. /**
  5528. * i40e_get_current_atr_count - Get the count of total FD ATR filters programmed
  5529. * @pf: board private structure
  5530. **/
  5531. u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
  5532. {
  5533. return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
  5534. }
  5535. /* We can see up to 256 filter programming desc in transit if the filters are
  5536. * being applied really fast; before we see the first
  5537. * filter miss error on Rx queue 0. Accumulating enough error messages before
  5538. * reacting will make sure we don't cause flush too often.
  5539. */
  5540. #define I40E_MAX_FD_PROGRAM_ERROR 256
  5541. /**
  5542. * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
  5543. * @pf: board private structure
  5544. **/
  5545. static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
  5546. {
  5547. /* if interface is down do nothing */
  5548. if (test_bit(__I40E_DOWN, pf->state))
  5549. return;
  5550. if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
  5551. i40e_fdir_flush_and_replay(pf);
  5552. i40e_fdir_check_and_reenable(pf);
  5553. }
  5554. /**
  5555. * i40e_vsi_link_event - notify VSI of a link event
  5556. * @vsi: vsi to be notified
  5557. * @link_up: link up or down
  5558. **/
  5559. static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
  5560. {
  5561. if (!vsi || test_bit(__I40E_VSI_DOWN, vsi->state))
  5562. return;
  5563. switch (vsi->type) {
  5564. case I40E_VSI_MAIN:
  5565. if (!vsi->netdev || !vsi->netdev_registered)
  5566. break;
  5567. if (link_up) {
  5568. netif_carrier_on(vsi->netdev);
  5569. netif_tx_wake_all_queues(vsi->netdev);
  5570. } else {
  5571. netif_carrier_off(vsi->netdev);
  5572. netif_tx_stop_all_queues(vsi->netdev);
  5573. }
  5574. break;
  5575. case I40E_VSI_SRIOV:
  5576. case I40E_VSI_VMDQ2:
  5577. case I40E_VSI_CTRL:
  5578. case I40E_VSI_IWARP:
  5579. case I40E_VSI_MIRROR:
  5580. default:
  5581. /* there is no notification for other VSIs */
  5582. break;
  5583. }
  5584. }
  5585. /**
  5586. * i40e_veb_link_event - notify elements on the veb of a link event
  5587. * @veb: veb to be notified
  5588. * @link_up: link up or down
  5589. **/
  5590. static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
  5591. {
  5592. struct i40e_pf *pf;
  5593. int i;
  5594. if (!veb || !veb->pf)
  5595. return;
  5596. pf = veb->pf;
  5597. /* depth first... */
  5598. for (i = 0; i < I40E_MAX_VEB; i++)
  5599. if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
  5600. i40e_veb_link_event(pf->veb[i], link_up);
  5601. /* ... now the local VSIs */
  5602. for (i = 0; i < pf->num_alloc_vsi; i++)
  5603. if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
  5604. i40e_vsi_link_event(pf->vsi[i], link_up);
  5605. }
  5606. /**
  5607. * i40e_link_event - Update netif_carrier status
  5608. * @pf: board private structure
  5609. **/
  5610. static void i40e_link_event(struct i40e_pf *pf)
  5611. {
  5612. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  5613. u8 new_link_speed, old_link_speed;
  5614. i40e_status status;
  5615. bool new_link, old_link;
  5616. /* save off old link status information */
  5617. pf->hw.phy.link_info_old = pf->hw.phy.link_info;
  5618. /* set this to force the get_link_status call to refresh state */
  5619. pf->hw.phy.get_link_info = true;
  5620. old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
  5621. status = i40e_get_link_status(&pf->hw, &new_link);
  5622. /* On success, disable temp link polling */
  5623. if (status == I40E_SUCCESS) {
  5624. if (pf->flags & I40E_FLAG_TEMP_LINK_POLLING)
  5625. pf->flags &= ~I40E_FLAG_TEMP_LINK_POLLING;
  5626. } else {
  5627. /* Enable link polling temporarily until i40e_get_link_status
  5628. * returns I40E_SUCCESS
  5629. */
  5630. pf->flags |= I40E_FLAG_TEMP_LINK_POLLING;
  5631. dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
  5632. status);
  5633. return;
  5634. }
  5635. old_link_speed = pf->hw.phy.link_info_old.link_speed;
  5636. new_link_speed = pf->hw.phy.link_info.link_speed;
  5637. if (new_link == old_link &&
  5638. new_link_speed == old_link_speed &&
  5639. (test_bit(__I40E_VSI_DOWN, vsi->state) ||
  5640. new_link == netif_carrier_ok(vsi->netdev)))
  5641. return;
  5642. if (!test_bit(__I40E_VSI_DOWN, vsi->state))
  5643. i40e_print_link_message(vsi, new_link);
  5644. /* Notify the base of the switch tree connected to
  5645. * the link. Floating VEBs are not notified.
  5646. */
  5647. if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
  5648. i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
  5649. else
  5650. i40e_vsi_link_event(vsi, new_link);
  5651. if (pf->vf)
  5652. i40e_vc_notify_link_state(pf);
  5653. if (pf->flags & I40E_FLAG_PTP)
  5654. i40e_ptp_set_increment(pf);
  5655. }
  5656. /**
  5657. * i40e_watchdog_subtask - periodic checks not using event driven response
  5658. * @pf: board private structure
  5659. **/
  5660. static void i40e_watchdog_subtask(struct i40e_pf *pf)
  5661. {
  5662. int i;
  5663. /* if interface is down do nothing */
  5664. if (test_bit(__I40E_DOWN, pf->state) ||
  5665. test_bit(__I40E_CONFIG_BUSY, pf->state))
  5666. return;
  5667. /* make sure we don't do these things too often */
  5668. if (time_before(jiffies, (pf->service_timer_previous +
  5669. pf->service_timer_period)))
  5670. return;
  5671. pf->service_timer_previous = jiffies;
  5672. if ((pf->flags & I40E_FLAG_LINK_POLLING_ENABLED) ||
  5673. (pf->flags & I40E_FLAG_TEMP_LINK_POLLING))
  5674. i40e_link_event(pf);
  5675. /* Update the stats for active netdevs so the network stack
  5676. * can look at updated numbers whenever it cares to
  5677. */
  5678. for (i = 0; i < pf->num_alloc_vsi; i++)
  5679. if (pf->vsi[i] && pf->vsi[i]->netdev)
  5680. i40e_update_stats(pf->vsi[i]);
  5681. if (pf->flags & I40E_FLAG_VEB_STATS_ENABLED) {
  5682. /* Update the stats for the active switching components */
  5683. for (i = 0; i < I40E_MAX_VEB; i++)
  5684. if (pf->veb[i])
  5685. i40e_update_veb_stats(pf->veb[i]);
  5686. }
  5687. i40e_ptp_rx_hang(pf);
  5688. i40e_ptp_tx_hang(pf);
  5689. }
  5690. /**
  5691. * i40e_reset_subtask - Set up for resetting the device and driver
  5692. * @pf: board private structure
  5693. **/
  5694. static void i40e_reset_subtask(struct i40e_pf *pf)
  5695. {
  5696. u32 reset_flags = 0;
  5697. if (test_bit(__I40E_REINIT_REQUESTED, pf->state)) {
  5698. reset_flags |= BIT(__I40E_REINIT_REQUESTED);
  5699. clear_bit(__I40E_REINIT_REQUESTED, pf->state);
  5700. }
  5701. if (test_bit(__I40E_PF_RESET_REQUESTED, pf->state)) {
  5702. reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
  5703. clear_bit(__I40E_PF_RESET_REQUESTED, pf->state);
  5704. }
  5705. if (test_bit(__I40E_CORE_RESET_REQUESTED, pf->state)) {
  5706. reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
  5707. clear_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
  5708. }
  5709. if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state)) {
  5710. reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
  5711. clear_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
  5712. }
  5713. if (test_bit(__I40E_DOWN_REQUESTED, pf->state)) {
  5714. reset_flags |= BIT(__I40E_DOWN_REQUESTED);
  5715. clear_bit(__I40E_DOWN_REQUESTED, pf->state);
  5716. }
  5717. /* If there's a recovery already waiting, it takes
  5718. * precedence before starting a new reset sequence.
  5719. */
  5720. if (test_bit(__I40E_RESET_INTR_RECEIVED, pf->state)) {
  5721. i40e_prep_for_reset(pf, false);
  5722. i40e_reset(pf);
  5723. i40e_rebuild(pf, false, false);
  5724. }
  5725. /* If we're already down or resetting, just bail */
  5726. if (reset_flags &&
  5727. !test_bit(__I40E_DOWN, pf->state) &&
  5728. !test_bit(__I40E_CONFIG_BUSY, pf->state)) {
  5729. i40e_do_reset(pf, reset_flags, false);
  5730. }
  5731. }
  5732. /**
  5733. * i40e_handle_link_event - Handle link event
  5734. * @pf: board private structure
  5735. * @e: event info posted on ARQ
  5736. **/
  5737. static void i40e_handle_link_event(struct i40e_pf *pf,
  5738. struct i40e_arq_event_info *e)
  5739. {
  5740. struct i40e_aqc_get_link_status *status =
  5741. (struct i40e_aqc_get_link_status *)&e->desc.params.raw;
  5742. /* Do a new status request to re-enable LSE reporting
  5743. * and load new status information into the hw struct
  5744. * This completely ignores any state information
  5745. * in the ARQ event info, instead choosing to always
  5746. * issue the AQ update link status command.
  5747. */
  5748. i40e_link_event(pf);
  5749. /* check for unqualified module, if link is down */
  5750. if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
  5751. (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
  5752. (!(status->link_info & I40E_AQ_LINK_UP)))
  5753. dev_err(&pf->pdev->dev,
  5754. "The driver failed to link because an unqualified module was detected.\n");
  5755. }
  5756. /**
  5757. * i40e_clean_adminq_subtask - Clean the AdminQ rings
  5758. * @pf: board private structure
  5759. **/
  5760. static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
  5761. {
  5762. struct i40e_arq_event_info event;
  5763. struct i40e_hw *hw = &pf->hw;
  5764. u16 pending, i = 0;
  5765. i40e_status ret;
  5766. u16 opcode;
  5767. u32 oldval;
  5768. u32 val;
  5769. /* Do not run clean AQ when PF reset fails */
  5770. if (test_bit(__I40E_RESET_FAILED, pf->state))
  5771. return;
  5772. /* check for error indications */
  5773. val = rd32(&pf->hw, pf->hw.aq.arq.len);
  5774. oldval = val;
  5775. if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
  5776. if (hw->debug_mask & I40E_DEBUG_AQ)
  5777. dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
  5778. val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
  5779. }
  5780. if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
  5781. if (hw->debug_mask & I40E_DEBUG_AQ)
  5782. dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
  5783. val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
  5784. pf->arq_overflows++;
  5785. }
  5786. if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
  5787. if (hw->debug_mask & I40E_DEBUG_AQ)
  5788. dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
  5789. val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
  5790. }
  5791. if (oldval != val)
  5792. wr32(&pf->hw, pf->hw.aq.arq.len, val);
  5793. val = rd32(&pf->hw, pf->hw.aq.asq.len);
  5794. oldval = val;
  5795. if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
  5796. if (pf->hw.debug_mask & I40E_DEBUG_AQ)
  5797. dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
  5798. val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
  5799. }
  5800. if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
  5801. if (pf->hw.debug_mask & I40E_DEBUG_AQ)
  5802. dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
  5803. val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
  5804. }
  5805. if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
  5806. if (pf->hw.debug_mask & I40E_DEBUG_AQ)
  5807. dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
  5808. val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
  5809. }
  5810. if (oldval != val)
  5811. wr32(&pf->hw, pf->hw.aq.asq.len, val);
  5812. event.buf_len = I40E_MAX_AQ_BUF_SIZE;
  5813. event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
  5814. if (!event.msg_buf)
  5815. return;
  5816. do {
  5817. ret = i40e_clean_arq_element(hw, &event, &pending);
  5818. if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK)
  5819. break;
  5820. else if (ret) {
  5821. dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
  5822. break;
  5823. }
  5824. opcode = le16_to_cpu(event.desc.opcode);
  5825. switch (opcode) {
  5826. case i40e_aqc_opc_get_link_status:
  5827. i40e_handle_link_event(pf, &event);
  5828. break;
  5829. case i40e_aqc_opc_send_msg_to_pf:
  5830. ret = i40e_vc_process_vf_msg(pf,
  5831. le16_to_cpu(event.desc.retval),
  5832. le32_to_cpu(event.desc.cookie_high),
  5833. le32_to_cpu(event.desc.cookie_low),
  5834. event.msg_buf,
  5835. event.msg_len);
  5836. break;
  5837. case i40e_aqc_opc_lldp_update_mib:
  5838. dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
  5839. #ifdef CONFIG_I40E_DCB
  5840. rtnl_lock();
  5841. ret = i40e_handle_lldp_event(pf, &event);
  5842. rtnl_unlock();
  5843. #endif /* CONFIG_I40E_DCB */
  5844. break;
  5845. case i40e_aqc_opc_event_lan_overflow:
  5846. dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
  5847. i40e_handle_lan_overflow_event(pf, &event);
  5848. break;
  5849. case i40e_aqc_opc_send_msg_to_peer:
  5850. dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
  5851. break;
  5852. case i40e_aqc_opc_nvm_erase:
  5853. case i40e_aqc_opc_nvm_update:
  5854. case i40e_aqc_opc_oem_post_update:
  5855. i40e_debug(&pf->hw, I40E_DEBUG_NVM,
  5856. "ARQ NVM operation 0x%04x completed\n",
  5857. opcode);
  5858. break;
  5859. default:
  5860. dev_info(&pf->pdev->dev,
  5861. "ARQ: Unknown event 0x%04x ignored\n",
  5862. opcode);
  5863. break;
  5864. }
  5865. } while (i++ < pf->adminq_work_limit);
  5866. if (i < pf->adminq_work_limit)
  5867. clear_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
  5868. /* re-enable Admin queue interrupt cause */
  5869. val = rd32(hw, I40E_PFINT_ICR0_ENA);
  5870. val |= I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
  5871. wr32(hw, I40E_PFINT_ICR0_ENA, val);
  5872. i40e_flush(hw);
  5873. kfree(event.msg_buf);
  5874. }
  5875. /**
  5876. * i40e_verify_eeprom - make sure eeprom is good to use
  5877. * @pf: board private structure
  5878. **/
  5879. static void i40e_verify_eeprom(struct i40e_pf *pf)
  5880. {
  5881. int err;
  5882. err = i40e_diag_eeprom_test(&pf->hw);
  5883. if (err) {
  5884. /* retry in case of garbage read */
  5885. err = i40e_diag_eeprom_test(&pf->hw);
  5886. if (err) {
  5887. dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
  5888. err);
  5889. set_bit(__I40E_BAD_EEPROM, pf->state);
  5890. }
  5891. }
  5892. if (!err && test_bit(__I40E_BAD_EEPROM, pf->state)) {
  5893. dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
  5894. clear_bit(__I40E_BAD_EEPROM, pf->state);
  5895. }
  5896. }
  5897. /**
  5898. * i40e_enable_pf_switch_lb
  5899. * @pf: pointer to the PF structure
  5900. *
  5901. * enable switch loop back or die - no point in a return value
  5902. **/
  5903. static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
  5904. {
  5905. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  5906. struct i40e_vsi_context ctxt;
  5907. int ret;
  5908. ctxt.seid = pf->main_vsi_seid;
  5909. ctxt.pf_num = pf->hw.pf_id;
  5910. ctxt.vf_num = 0;
  5911. ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
  5912. if (ret) {
  5913. dev_info(&pf->pdev->dev,
  5914. "couldn't get PF vsi config, err %s aq_err %s\n",
  5915. i40e_stat_str(&pf->hw, ret),
  5916. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  5917. return;
  5918. }
  5919. ctxt.flags = I40E_AQ_VSI_TYPE_PF;
  5920. ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  5921. ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  5922. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  5923. if (ret) {
  5924. dev_info(&pf->pdev->dev,
  5925. "update vsi switch failed, err %s aq_err %s\n",
  5926. i40e_stat_str(&pf->hw, ret),
  5927. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  5928. }
  5929. }
  5930. /**
  5931. * i40e_disable_pf_switch_lb
  5932. * @pf: pointer to the PF structure
  5933. *
  5934. * disable switch loop back or die - no point in a return value
  5935. **/
  5936. static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
  5937. {
  5938. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  5939. struct i40e_vsi_context ctxt;
  5940. int ret;
  5941. ctxt.seid = pf->main_vsi_seid;
  5942. ctxt.pf_num = pf->hw.pf_id;
  5943. ctxt.vf_num = 0;
  5944. ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
  5945. if (ret) {
  5946. dev_info(&pf->pdev->dev,
  5947. "couldn't get PF vsi config, err %s aq_err %s\n",
  5948. i40e_stat_str(&pf->hw, ret),
  5949. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  5950. return;
  5951. }
  5952. ctxt.flags = I40E_AQ_VSI_TYPE_PF;
  5953. ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  5954. ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  5955. ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
  5956. if (ret) {
  5957. dev_info(&pf->pdev->dev,
  5958. "update vsi switch failed, err %s aq_err %s\n",
  5959. i40e_stat_str(&pf->hw, ret),
  5960. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  5961. }
  5962. }
  5963. /**
  5964. * i40e_config_bridge_mode - Configure the HW bridge mode
  5965. * @veb: pointer to the bridge instance
  5966. *
  5967. * Configure the loop back mode for the LAN VSI that is downlink to the
  5968. * specified HW bridge instance. It is expected this function is called
  5969. * when a new HW bridge is instantiated.
  5970. **/
  5971. static void i40e_config_bridge_mode(struct i40e_veb *veb)
  5972. {
  5973. struct i40e_pf *pf = veb->pf;
  5974. if (pf->hw.debug_mask & I40E_DEBUG_LAN)
  5975. dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
  5976. veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
  5977. if (veb->bridge_mode & BRIDGE_MODE_VEPA)
  5978. i40e_disable_pf_switch_lb(pf);
  5979. else
  5980. i40e_enable_pf_switch_lb(pf);
  5981. }
  5982. /**
  5983. * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
  5984. * @veb: pointer to the VEB instance
  5985. *
  5986. * This is a recursive function that first builds the attached VSIs then
  5987. * recurses in to build the next layer of VEB. We track the connections
  5988. * through our own index numbers because the seid's from the HW could
  5989. * change across the reset.
  5990. **/
  5991. static int i40e_reconstitute_veb(struct i40e_veb *veb)
  5992. {
  5993. struct i40e_vsi *ctl_vsi = NULL;
  5994. struct i40e_pf *pf = veb->pf;
  5995. int v, veb_idx;
  5996. int ret;
  5997. /* build VSI that owns this VEB, temporarily attached to base VEB */
  5998. for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
  5999. if (pf->vsi[v] &&
  6000. pf->vsi[v]->veb_idx == veb->idx &&
  6001. pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
  6002. ctl_vsi = pf->vsi[v];
  6003. break;
  6004. }
  6005. }
  6006. if (!ctl_vsi) {
  6007. dev_info(&pf->pdev->dev,
  6008. "missing owner VSI for veb_idx %d\n", veb->idx);
  6009. ret = -ENOENT;
  6010. goto end_reconstitute;
  6011. }
  6012. if (ctl_vsi != pf->vsi[pf->lan_vsi])
  6013. ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
  6014. ret = i40e_add_vsi(ctl_vsi);
  6015. if (ret) {
  6016. dev_info(&pf->pdev->dev,
  6017. "rebuild of veb_idx %d owner VSI failed: %d\n",
  6018. veb->idx, ret);
  6019. goto end_reconstitute;
  6020. }
  6021. i40e_vsi_reset_stats(ctl_vsi);
  6022. /* create the VEB in the switch and move the VSI onto the VEB */
  6023. ret = i40e_add_veb(veb, ctl_vsi);
  6024. if (ret)
  6025. goto end_reconstitute;
  6026. if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
  6027. veb->bridge_mode = BRIDGE_MODE_VEB;
  6028. else
  6029. veb->bridge_mode = BRIDGE_MODE_VEPA;
  6030. i40e_config_bridge_mode(veb);
  6031. /* create the remaining VSIs attached to this VEB */
  6032. for (v = 0; v < pf->num_alloc_vsi; v++) {
  6033. if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
  6034. continue;
  6035. if (pf->vsi[v]->veb_idx == veb->idx) {
  6036. struct i40e_vsi *vsi = pf->vsi[v];
  6037. vsi->uplink_seid = veb->seid;
  6038. ret = i40e_add_vsi(vsi);
  6039. if (ret) {
  6040. dev_info(&pf->pdev->dev,
  6041. "rebuild of vsi_idx %d failed: %d\n",
  6042. v, ret);
  6043. goto end_reconstitute;
  6044. }
  6045. i40e_vsi_reset_stats(vsi);
  6046. }
  6047. }
  6048. /* create any VEBs attached to this VEB - RECURSION */
  6049. for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
  6050. if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
  6051. pf->veb[veb_idx]->uplink_seid = veb->seid;
  6052. ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
  6053. if (ret)
  6054. break;
  6055. }
  6056. }
  6057. end_reconstitute:
  6058. return ret;
  6059. }
  6060. /**
  6061. * i40e_get_capabilities - get info about the HW
  6062. * @pf: the PF struct
  6063. **/
  6064. static int i40e_get_capabilities(struct i40e_pf *pf)
  6065. {
  6066. struct i40e_aqc_list_capabilities_element_resp *cap_buf;
  6067. u16 data_size;
  6068. int buf_len;
  6069. int err;
  6070. buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
  6071. do {
  6072. cap_buf = kzalloc(buf_len, GFP_KERNEL);
  6073. if (!cap_buf)
  6074. return -ENOMEM;
  6075. /* this loads the data into the hw struct for us */
  6076. err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
  6077. &data_size,
  6078. i40e_aqc_opc_list_func_capabilities,
  6079. NULL);
  6080. /* data loaded, buffer no longer needed */
  6081. kfree(cap_buf);
  6082. if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
  6083. /* retry with a larger buffer */
  6084. buf_len = data_size;
  6085. } else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK) {
  6086. dev_info(&pf->pdev->dev,
  6087. "capability discovery failed, err %s aq_err %s\n",
  6088. i40e_stat_str(&pf->hw, err),
  6089. i40e_aq_str(&pf->hw,
  6090. pf->hw.aq.asq_last_status));
  6091. return -ENODEV;
  6092. }
  6093. } while (err);
  6094. if (pf->hw.debug_mask & I40E_DEBUG_USER)
  6095. dev_info(&pf->pdev->dev,
  6096. "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",
  6097. pf->hw.pf_id, pf->hw.func_caps.num_vfs,
  6098. pf->hw.func_caps.num_msix_vectors,
  6099. pf->hw.func_caps.num_msix_vectors_vf,
  6100. pf->hw.func_caps.fd_filters_guaranteed,
  6101. pf->hw.func_caps.fd_filters_best_effort,
  6102. pf->hw.func_caps.num_tx_qp,
  6103. pf->hw.func_caps.num_vsis);
  6104. #define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
  6105. + pf->hw.func_caps.num_vfs)
  6106. if (pf->hw.revision_id == 0 && (DEF_NUM_VSI > pf->hw.func_caps.num_vsis)) {
  6107. dev_info(&pf->pdev->dev,
  6108. "got num_vsis %d, setting num_vsis to %d\n",
  6109. pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
  6110. pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
  6111. }
  6112. return 0;
  6113. }
  6114. static int i40e_vsi_clear(struct i40e_vsi *vsi);
  6115. /**
  6116. * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
  6117. * @pf: board private structure
  6118. **/
  6119. static void i40e_fdir_sb_setup(struct i40e_pf *pf)
  6120. {
  6121. struct i40e_vsi *vsi;
  6122. /* quick workaround for an NVM issue that leaves a critical register
  6123. * uninitialized
  6124. */
  6125. if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
  6126. static const u32 hkey[] = {
  6127. 0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
  6128. 0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
  6129. 0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
  6130. 0x95b3a76d};
  6131. int i;
  6132. for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
  6133. wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
  6134. }
  6135. if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
  6136. return;
  6137. /* find existing VSI and see if it needs configuring */
  6138. vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
  6139. /* create a new VSI if none exists */
  6140. if (!vsi) {
  6141. vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
  6142. pf->vsi[pf->lan_vsi]->seid, 0);
  6143. if (!vsi) {
  6144. dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
  6145. pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
  6146. return;
  6147. }
  6148. }
  6149. i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
  6150. }
  6151. /**
  6152. * i40e_fdir_teardown - release the Flow Director resources
  6153. * @pf: board private structure
  6154. **/
  6155. static void i40e_fdir_teardown(struct i40e_pf *pf)
  6156. {
  6157. struct i40e_vsi *vsi;
  6158. i40e_fdir_filter_exit(pf);
  6159. vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
  6160. if (vsi)
  6161. i40e_vsi_release(vsi);
  6162. }
  6163. /**
  6164. * i40e_prep_for_reset - prep for the core to reset
  6165. * @pf: board private structure
  6166. * @lock_acquired: indicates whether or not the lock has been acquired
  6167. * before this function was called.
  6168. *
  6169. * Close up the VFs and other things in prep for PF Reset.
  6170. **/
  6171. static void i40e_prep_for_reset(struct i40e_pf *pf, bool lock_acquired)
  6172. {
  6173. struct i40e_hw *hw = &pf->hw;
  6174. i40e_status ret = 0;
  6175. u32 v;
  6176. clear_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
  6177. if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
  6178. return;
  6179. if (i40e_check_asq_alive(&pf->hw))
  6180. i40e_vc_notify_reset(pf);
  6181. dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
  6182. /* quiesce the VSIs and their queues that are not already DOWN */
  6183. /* pf_quiesce_all_vsi modifies netdev structures -rtnl_lock needed */
  6184. if (!lock_acquired)
  6185. rtnl_lock();
  6186. i40e_pf_quiesce_all_vsi(pf);
  6187. if (!lock_acquired)
  6188. rtnl_unlock();
  6189. for (v = 0; v < pf->num_alloc_vsi; v++) {
  6190. if (pf->vsi[v])
  6191. pf->vsi[v]->seid = 0;
  6192. }
  6193. i40e_shutdown_adminq(&pf->hw);
  6194. /* call shutdown HMC */
  6195. if (hw->hmc.hmc_obj) {
  6196. ret = i40e_shutdown_lan_hmc(hw);
  6197. if (ret)
  6198. dev_warn(&pf->pdev->dev,
  6199. "shutdown_lan_hmc failed: %d\n", ret);
  6200. }
  6201. }
  6202. /**
  6203. * i40e_send_version - update firmware with driver version
  6204. * @pf: PF struct
  6205. */
  6206. static void i40e_send_version(struct i40e_pf *pf)
  6207. {
  6208. struct i40e_driver_version dv;
  6209. dv.major_version = DRV_VERSION_MAJOR;
  6210. dv.minor_version = DRV_VERSION_MINOR;
  6211. dv.build_version = DRV_VERSION_BUILD;
  6212. dv.subbuild_version = 0;
  6213. strlcpy(dv.driver_string, DRV_VERSION, sizeof(dv.driver_string));
  6214. i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
  6215. }
  6216. /**
  6217. * i40e_get_oem_version - get OEM specific version information
  6218. * @hw: pointer to the hardware structure
  6219. **/
  6220. static void i40e_get_oem_version(struct i40e_hw *hw)
  6221. {
  6222. u16 block_offset = 0xffff;
  6223. u16 block_length = 0;
  6224. u16 capabilities = 0;
  6225. u16 gen_snap = 0;
  6226. u16 release = 0;
  6227. #define I40E_SR_NVM_OEM_VERSION_PTR 0x1B
  6228. #define I40E_NVM_OEM_LENGTH_OFFSET 0x00
  6229. #define I40E_NVM_OEM_CAPABILITIES_OFFSET 0x01
  6230. #define I40E_NVM_OEM_GEN_OFFSET 0x02
  6231. #define I40E_NVM_OEM_RELEASE_OFFSET 0x03
  6232. #define I40E_NVM_OEM_CAPABILITIES_MASK 0x000F
  6233. #define I40E_NVM_OEM_LENGTH 3
  6234. /* Check if pointer to OEM version block is valid. */
  6235. i40e_read_nvm_word(hw, I40E_SR_NVM_OEM_VERSION_PTR, &block_offset);
  6236. if (block_offset == 0xffff)
  6237. return;
  6238. /* Check if OEM version block has correct length. */
  6239. i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_LENGTH_OFFSET,
  6240. &block_length);
  6241. if (block_length < I40E_NVM_OEM_LENGTH)
  6242. return;
  6243. /* Check if OEM version format is as expected. */
  6244. i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_CAPABILITIES_OFFSET,
  6245. &capabilities);
  6246. if ((capabilities & I40E_NVM_OEM_CAPABILITIES_MASK) != 0)
  6247. return;
  6248. i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_GEN_OFFSET,
  6249. &gen_snap);
  6250. i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_RELEASE_OFFSET,
  6251. &release);
  6252. hw->nvm.oem_ver = (gen_snap << I40E_OEM_SNAP_SHIFT) | release;
  6253. hw->nvm.eetrack = I40E_OEM_EETRACK_ID;
  6254. }
  6255. /**
  6256. * i40e_reset - wait for core reset to finish reset, reset pf if corer not seen
  6257. * @pf: board private structure
  6258. **/
  6259. static int i40e_reset(struct i40e_pf *pf)
  6260. {
  6261. struct i40e_hw *hw = &pf->hw;
  6262. i40e_status ret;
  6263. ret = i40e_pf_reset(hw);
  6264. if (ret) {
  6265. dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
  6266. set_bit(__I40E_RESET_FAILED, pf->state);
  6267. clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
  6268. } else {
  6269. pf->pfr_count++;
  6270. }
  6271. return ret;
  6272. }
  6273. /**
  6274. * i40e_rebuild - rebuild using a saved config
  6275. * @pf: board private structure
  6276. * @reinit: if the Main VSI needs to re-initialized.
  6277. * @lock_acquired: indicates whether or not the lock has been acquired
  6278. * before this function was called.
  6279. **/
  6280. static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired)
  6281. {
  6282. struct i40e_hw *hw = &pf->hw;
  6283. u8 set_fc_aq_fail = 0;
  6284. i40e_status ret;
  6285. u32 val;
  6286. int v;
  6287. if (test_bit(__I40E_DOWN, pf->state))
  6288. goto clear_recovery;
  6289. dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
  6290. /* rebuild the basics for the AdminQ, HMC, and initial HW switch */
  6291. ret = i40e_init_adminq(&pf->hw);
  6292. if (ret) {
  6293. dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %s aq_err %s\n",
  6294. i40e_stat_str(&pf->hw, ret),
  6295. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  6296. goto clear_recovery;
  6297. }
  6298. i40e_get_oem_version(&pf->hw);
  6299. /* re-verify the eeprom if we just had an EMP reset */
  6300. if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state))
  6301. i40e_verify_eeprom(pf);
  6302. i40e_clear_pxe_mode(hw);
  6303. ret = i40e_get_capabilities(pf);
  6304. if (ret)
  6305. goto end_core_reset;
  6306. ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
  6307. hw->func_caps.num_rx_qp, 0, 0);
  6308. if (ret) {
  6309. dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
  6310. goto end_core_reset;
  6311. }
  6312. ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
  6313. if (ret) {
  6314. dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
  6315. goto end_core_reset;
  6316. }
  6317. #ifdef CONFIG_I40E_DCB
  6318. ret = i40e_init_pf_dcb(pf);
  6319. if (ret) {
  6320. dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n", ret);
  6321. pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
  6322. /* Continue without DCB enabled */
  6323. }
  6324. #endif /* CONFIG_I40E_DCB */
  6325. /* do basic switch setup */
  6326. if (!lock_acquired)
  6327. rtnl_lock();
  6328. ret = i40e_setup_pf_switch(pf, reinit);
  6329. if (ret)
  6330. goto end_unlock;
  6331. /* The driver only wants link up/down and module qualification
  6332. * reports from firmware. Note the negative logic.
  6333. */
  6334. ret = i40e_aq_set_phy_int_mask(&pf->hw,
  6335. ~(I40E_AQ_EVENT_LINK_UPDOWN |
  6336. I40E_AQ_EVENT_MEDIA_NA |
  6337. I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
  6338. if (ret)
  6339. dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
  6340. i40e_stat_str(&pf->hw, ret),
  6341. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  6342. /* make sure our flow control settings are restored */
  6343. ret = i40e_set_fc(&pf->hw, &set_fc_aq_fail, true);
  6344. if (ret)
  6345. dev_dbg(&pf->pdev->dev, "setting flow control: ret = %s last_status = %s\n",
  6346. i40e_stat_str(&pf->hw, ret),
  6347. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  6348. /* Rebuild the VSIs and VEBs that existed before reset.
  6349. * They are still in our local switch element arrays, so only
  6350. * need to rebuild the switch model in the HW.
  6351. *
  6352. * If there were VEBs but the reconstitution failed, we'll try
  6353. * try to recover minimal use by getting the basic PF VSI working.
  6354. */
  6355. if (pf->vsi[pf->lan_vsi]->uplink_seid != pf->mac_seid) {
  6356. dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
  6357. /* find the one VEB connected to the MAC, and find orphans */
  6358. for (v = 0; v < I40E_MAX_VEB; v++) {
  6359. if (!pf->veb[v])
  6360. continue;
  6361. if (pf->veb[v]->uplink_seid == pf->mac_seid ||
  6362. pf->veb[v]->uplink_seid == 0) {
  6363. ret = i40e_reconstitute_veb(pf->veb[v]);
  6364. if (!ret)
  6365. continue;
  6366. /* If Main VEB failed, we're in deep doodoo,
  6367. * so give up rebuilding the switch and set up
  6368. * for minimal rebuild of PF VSI.
  6369. * If orphan failed, we'll report the error
  6370. * but try to keep going.
  6371. */
  6372. if (pf->veb[v]->uplink_seid == pf->mac_seid) {
  6373. dev_info(&pf->pdev->dev,
  6374. "rebuild of switch failed: %d, will try to set up simple PF connection\n",
  6375. ret);
  6376. pf->vsi[pf->lan_vsi]->uplink_seid
  6377. = pf->mac_seid;
  6378. break;
  6379. } else if (pf->veb[v]->uplink_seid == 0) {
  6380. dev_info(&pf->pdev->dev,
  6381. "rebuild of orphan VEB failed: %d\n",
  6382. ret);
  6383. }
  6384. }
  6385. }
  6386. }
  6387. if (pf->vsi[pf->lan_vsi]->uplink_seid == pf->mac_seid) {
  6388. dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
  6389. /* no VEB, so rebuild only the Main VSI */
  6390. ret = i40e_add_vsi(pf->vsi[pf->lan_vsi]);
  6391. if (ret) {
  6392. dev_info(&pf->pdev->dev,
  6393. "rebuild of Main VSI failed: %d\n", ret);
  6394. goto end_unlock;
  6395. }
  6396. }
  6397. /* Reconfigure hardware for allowing smaller MSS in the case
  6398. * of TSO, so that we avoid the MDD being fired and causing
  6399. * a reset in the case of small MSS+TSO.
  6400. */
  6401. #define I40E_REG_MSS 0x000E64DC
  6402. #define I40E_REG_MSS_MIN_MASK 0x3FF0000
  6403. #define I40E_64BYTE_MSS 0x400000
  6404. val = rd32(hw, I40E_REG_MSS);
  6405. if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
  6406. val &= ~I40E_REG_MSS_MIN_MASK;
  6407. val |= I40E_64BYTE_MSS;
  6408. wr32(hw, I40E_REG_MSS, val);
  6409. }
  6410. if (pf->flags & I40E_FLAG_RESTART_AUTONEG) {
  6411. msleep(75);
  6412. ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
  6413. if (ret)
  6414. dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
  6415. i40e_stat_str(&pf->hw, ret),
  6416. i40e_aq_str(&pf->hw,
  6417. pf->hw.aq.asq_last_status));
  6418. }
  6419. /* reinit the misc interrupt */
  6420. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  6421. ret = i40e_setup_misc_vector(pf);
  6422. /* Add a filter to drop all Flow control frames from any VSI from being
  6423. * transmitted. By doing so we stop a malicious VF from sending out
  6424. * PAUSE or PFC frames and potentially controlling traffic for other
  6425. * PF/VF VSIs.
  6426. * The FW can still send Flow control frames if enabled.
  6427. */
  6428. i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
  6429. pf->main_vsi_seid);
  6430. /* restart the VSIs that were rebuilt and running before the reset */
  6431. i40e_pf_unquiesce_all_vsi(pf);
  6432. /* Release the RTNL lock before we start resetting VFs */
  6433. if (!lock_acquired)
  6434. rtnl_unlock();
  6435. i40e_reset_all_vfs(pf, true);
  6436. /* tell the firmware that we're starting */
  6437. i40e_send_version(pf);
  6438. /* We've already released the lock, so don't do it again */
  6439. goto end_core_reset;
  6440. end_unlock:
  6441. if (!lock_acquired)
  6442. rtnl_unlock();
  6443. end_core_reset:
  6444. clear_bit(__I40E_RESET_FAILED, pf->state);
  6445. clear_recovery:
  6446. clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
  6447. }
  6448. /**
  6449. * i40e_reset_and_rebuild - reset and rebuild using a saved config
  6450. * @pf: board private structure
  6451. * @reinit: if the Main VSI needs to re-initialized.
  6452. * @lock_acquired: indicates whether or not the lock has been acquired
  6453. * before this function was called.
  6454. **/
  6455. static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
  6456. bool lock_acquired)
  6457. {
  6458. int ret;
  6459. /* Now we wait for GRST to settle out.
  6460. * We don't have to delete the VEBs or VSIs from the hw switch
  6461. * because the reset will make them disappear.
  6462. */
  6463. ret = i40e_reset(pf);
  6464. if (!ret)
  6465. i40e_rebuild(pf, reinit, lock_acquired);
  6466. }
  6467. /**
  6468. * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
  6469. * @pf: board private structure
  6470. *
  6471. * Close up the VFs and other things in prep for a Core Reset,
  6472. * then get ready to rebuild the world.
  6473. * @lock_acquired: indicates whether or not the lock has been acquired
  6474. * before this function was called.
  6475. **/
  6476. static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired)
  6477. {
  6478. i40e_prep_for_reset(pf, lock_acquired);
  6479. i40e_reset_and_rebuild(pf, false, lock_acquired);
  6480. }
  6481. /**
  6482. * i40e_handle_mdd_event
  6483. * @pf: pointer to the PF structure
  6484. *
  6485. * Called from the MDD irq handler to identify possibly malicious vfs
  6486. **/
  6487. static void i40e_handle_mdd_event(struct i40e_pf *pf)
  6488. {
  6489. struct i40e_hw *hw = &pf->hw;
  6490. bool mdd_detected = false;
  6491. bool pf_mdd_detected = false;
  6492. struct i40e_vf *vf;
  6493. u32 reg;
  6494. int i;
  6495. if (!test_bit(__I40E_MDD_EVENT_PENDING, pf->state))
  6496. return;
  6497. /* find what triggered the MDD event */
  6498. reg = rd32(hw, I40E_GL_MDET_TX);
  6499. if (reg & I40E_GL_MDET_TX_VALID_MASK) {
  6500. u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
  6501. I40E_GL_MDET_TX_PF_NUM_SHIFT;
  6502. u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
  6503. I40E_GL_MDET_TX_VF_NUM_SHIFT;
  6504. u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
  6505. I40E_GL_MDET_TX_EVENT_SHIFT;
  6506. u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
  6507. I40E_GL_MDET_TX_QUEUE_SHIFT) -
  6508. pf->hw.func_caps.base_queue;
  6509. if (netif_msg_tx_err(pf))
  6510. dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on TX queue %d PF number 0x%02x VF number 0x%02x\n",
  6511. event, queue, pf_num, vf_num);
  6512. wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
  6513. mdd_detected = true;
  6514. }
  6515. reg = rd32(hw, I40E_GL_MDET_RX);
  6516. if (reg & I40E_GL_MDET_RX_VALID_MASK) {
  6517. u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
  6518. I40E_GL_MDET_RX_FUNCTION_SHIFT;
  6519. u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
  6520. I40E_GL_MDET_RX_EVENT_SHIFT;
  6521. u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
  6522. I40E_GL_MDET_RX_QUEUE_SHIFT) -
  6523. pf->hw.func_caps.base_queue;
  6524. if (netif_msg_rx_err(pf))
  6525. dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
  6526. event, queue, func);
  6527. wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
  6528. mdd_detected = true;
  6529. }
  6530. if (mdd_detected) {
  6531. reg = rd32(hw, I40E_PF_MDET_TX);
  6532. if (reg & I40E_PF_MDET_TX_VALID_MASK) {
  6533. wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
  6534. dev_info(&pf->pdev->dev, "TX driver issue detected, PF reset issued\n");
  6535. pf_mdd_detected = true;
  6536. }
  6537. reg = rd32(hw, I40E_PF_MDET_RX);
  6538. if (reg & I40E_PF_MDET_RX_VALID_MASK) {
  6539. wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
  6540. dev_info(&pf->pdev->dev, "RX driver issue detected, PF reset issued\n");
  6541. pf_mdd_detected = true;
  6542. }
  6543. /* Queue belongs to the PF, initiate a reset */
  6544. if (pf_mdd_detected) {
  6545. set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
  6546. i40e_service_event_schedule(pf);
  6547. }
  6548. }
  6549. /* see if one of the VFs needs its hand slapped */
  6550. for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
  6551. vf = &(pf->vf[i]);
  6552. reg = rd32(hw, I40E_VP_MDET_TX(i));
  6553. if (reg & I40E_VP_MDET_TX_VALID_MASK) {
  6554. wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
  6555. vf->num_mdd_events++;
  6556. dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
  6557. i);
  6558. }
  6559. reg = rd32(hw, I40E_VP_MDET_RX(i));
  6560. if (reg & I40E_VP_MDET_RX_VALID_MASK) {
  6561. wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
  6562. vf->num_mdd_events++;
  6563. dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
  6564. i);
  6565. }
  6566. if (vf->num_mdd_events > I40E_DEFAULT_NUM_MDD_EVENTS_ALLOWED) {
  6567. dev_info(&pf->pdev->dev,
  6568. "Too many MDD events on VF %d, disabled\n", i);
  6569. dev_info(&pf->pdev->dev,
  6570. "Use PF Control I/F to re-enable the VF\n");
  6571. set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
  6572. }
  6573. }
  6574. /* re-enable mdd interrupt cause */
  6575. clear_bit(__I40E_MDD_EVENT_PENDING, pf->state);
  6576. reg = rd32(hw, I40E_PFINT_ICR0_ENA);
  6577. reg |= I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
  6578. wr32(hw, I40E_PFINT_ICR0_ENA, reg);
  6579. i40e_flush(hw);
  6580. }
  6581. static const char *i40e_tunnel_name(struct i40e_udp_port_config *port)
  6582. {
  6583. switch (port->type) {
  6584. case UDP_TUNNEL_TYPE_VXLAN:
  6585. return "vxlan";
  6586. case UDP_TUNNEL_TYPE_GENEVE:
  6587. return "geneve";
  6588. default:
  6589. return "unknown";
  6590. }
  6591. }
  6592. /**
  6593. * i40e_sync_udp_filters - Trigger a sync event for existing UDP filters
  6594. * @pf: board private structure
  6595. **/
  6596. static void i40e_sync_udp_filters(struct i40e_pf *pf)
  6597. {
  6598. int i;
  6599. /* loop through and set pending bit for all active UDP filters */
  6600. for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
  6601. if (pf->udp_ports[i].port)
  6602. pf->pending_udp_bitmap |= BIT_ULL(i);
  6603. }
  6604. pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
  6605. }
  6606. /**
  6607. * i40e_sync_udp_filters_subtask - Sync the VSI filter list with HW
  6608. * @pf: board private structure
  6609. **/
  6610. static void i40e_sync_udp_filters_subtask(struct i40e_pf *pf)
  6611. {
  6612. struct i40e_hw *hw = &pf->hw;
  6613. i40e_status ret;
  6614. u16 port;
  6615. int i;
  6616. if (!(pf->flags & I40E_FLAG_UDP_FILTER_SYNC))
  6617. return;
  6618. pf->flags &= ~I40E_FLAG_UDP_FILTER_SYNC;
  6619. for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
  6620. if (pf->pending_udp_bitmap & BIT_ULL(i)) {
  6621. pf->pending_udp_bitmap &= ~BIT_ULL(i);
  6622. port = pf->udp_ports[i].port;
  6623. if (port)
  6624. ret = i40e_aq_add_udp_tunnel(hw, port,
  6625. pf->udp_ports[i].type,
  6626. NULL, NULL);
  6627. else
  6628. ret = i40e_aq_del_udp_tunnel(hw, i, NULL);
  6629. if (ret) {
  6630. dev_info(&pf->pdev->dev,
  6631. "%s %s port %d, index %d failed, err %s aq_err %s\n",
  6632. i40e_tunnel_name(&pf->udp_ports[i]),
  6633. port ? "add" : "delete",
  6634. port, i,
  6635. i40e_stat_str(&pf->hw, ret),
  6636. i40e_aq_str(&pf->hw,
  6637. pf->hw.aq.asq_last_status));
  6638. pf->udp_ports[i].port = 0;
  6639. }
  6640. }
  6641. }
  6642. }
  6643. /**
  6644. * i40e_service_task - Run the driver's async subtasks
  6645. * @work: pointer to work_struct containing our data
  6646. **/
  6647. static void i40e_service_task(struct work_struct *work)
  6648. {
  6649. struct i40e_pf *pf = container_of(work,
  6650. struct i40e_pf,
  6651. service_task);
  6652. unsigned long start_time = jiffies;
  6653. /* don't bother with service tasks if a reset is in progress */
  6654. if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
  6655. return;
  6656. if (test_and_set_bit(__I40E_SERVICE_SCHED, pf->state))
  6657. return;
  6658. i40e_detect_recover_hung(pf);
  6659. i40e_sync_filters_subtask(pf);
  6660. i40e_reset_subtask(pf);
  6661. i40e_handle_mdd_event(pf);
  6662. i40e_vc_process_vflr_event(pf);
  6663. i40e_watchdog_subtask(pf);
  6664. i40e_fdir_reinit_subtask(pf);
  6665. if (pf->flags & I40E_FLAG_CLIENT_RESET) {
  6666. /* Client subtask will reopen next time through. */
  6667. i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], true);
  6668. pf->flags &= ~I40E_FLAG_CLIENT_RESET;
  6669. } else {
  6670. i40e_client_subtask(pf);
  6671. if (pf->flags & I40E_FLAG_CLIENT_L2_CHANGE) {
  6672. i40e_notify_client_of_l2_param_changes(
  6673. pf->vsi[pf->lan_vsi]);
  6674. pf->flags &= ~I40E_FLAG_CLIENT_L2_CHANGE;
  6675. }
  6676. }
  6677. i40e_sync_filters_subtask(pf);
  6678. i40e_sync_udp_filters_subtask(pf);
  6679. i40e_clean_adminq_subtask(pf);
  6680. /* flush memory to make sure state is correct before next watchdog */
  6681. smp_mb__before_atomic();
  6682. clear_bit(__I40E_SERVICE_SCHED, pf->state);
  6683. /* If the tasks have taken longer than one timer cycle or there
  6684. * is more work to be done, reschedule the service task now
  6685. * rather than wait for the timer to tick again.
  6686. */
  6687. if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
  6688. test_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state) ||
  6689. test_bit(__I40E_MDD_EVENT_PENDING, pf->state) ||
  6690. test_bit(__I40E_VFLR_EVENT_PENDING, pf->state))
  6691. i40e_service_event_schedule(pf);
  6692. }
  6693. /**
  6694. * i40e_service_timer - timer callback
  6695. * @data: pointer to PF struct
  6696. **/
  6697. static void i40e_service_timer(unsigned long data)
  6698. {
  6699. struct i40e_pf *pf = (struct i40e_pf *)data;
  6700. mod_timer(&pf->service_timer,
  6701. round_jiffies(jiffies + pf->service_timer_period));
  6702. i40e_service_event_schedule(pf);
  6703. }
  6704. /**
  6705. * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
  6706. * @vsi: the VSI being configured
  6707. **/
  6708. static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
  6709. {
  6710. struct i40e_pf *pf = vsi->back;
  6711. switch (vsi->type) {
  6712. case I40E_VSI_MAIN:
  6713. vsi->alloc_queue_pairs = pf->num_lan_qps;
  6714. vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
  6715. I40E_REQ_DESCRIPTOR_MULTIPLE);
  6716. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  6717. vsi->num_q_vectors = pf->num_lan_msix;
  6718. else
  6719. vsi->num_q_vectors = 1;
  6720. break;
  6721. case I40E_VSI_FDIR:
  6722. vsi->alloc_queue_pairs = 1;
  6723. vsi->num_desc = ALIGN(I40E_FDIR_RING_COUNT,
  6724. I40E_REQ_DESCRIPTOR_MULTIPLE);
  6725. vsi->num_q_vectors = pf->num_fdsb_msix;
  6726. break;
  6727. case I40E_VSI_VMDQ2:
  6728. vsi->alloc_queue_pairs = pf->num_vmdq_qps;
  6729. vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
  6730. I40E_REQ_DESCRIPTOR_MULTIPLE);
  6731. vsi->num_q_vectors = pf->num_vmdq_msix;
  6732. break;
  6733. case I40E_VSI_SRIOV:
  6734. vsi->alloc_queue_pairs = pf->num_vf_qps;
  6735. vsi->num_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
  6736. I40E_REQ_DESCRIPTOR_MULTIPLE);
  6737. break;
  6738. default:
  6739. WARN_ON(1);
  6740. return -ENODATA;
  6741. }
  6742. return 0;
  6743. }
  6744. /**
  6745. * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
  6746. * @type: VSI pointer
  6747. * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
  6748. *
  6749. * On error: returns error code (negative)
  6750. * On success: returns 0
  6751. **/
  6752. static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
  6753. {
  6754. struct i40e_ring **next_rings;
  6755. int size;
  6756. int ret = 0;
  6757. /* allocate memory for both Tx, XDP Tx and Rx ring pointers */
  6758. size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs *
  6759. (i40e_enabled_xdp_vsi(vsi) ? 3 : 2);
  6760. vsi->tx_rings = kzalloc(size, GFP_KERNEL);
  6761. if (!vsi->tx_rings)
  6762. return -ENOMEM;
  6763. next_rings = vsi->tx_rings + vsi->alloc_queue_pairs;
  6764. if (i40e_enabled_xdp_vsi(vsi)) {
  6765. vsi->xdp_rings = next_rings;
  6766. next_rings += vsi->alloc_queue_pairs;
  6767. }
  6768. vsi->rx_rings = next_rings;
  6769. if (alloc_qvectors) {
  6770. /* allocate memory for q_vector pointers */
  6771. size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
  6772. vsi->q_vectors = kzalloc(size, GFP_KERNEL);
  6773. if (!vsi->q_vectors) {
  6774. ret = -ENOMEM;
  6775. goto err_vectors;
  6776. }
  6777. }
  6778. return ret;
  6779. err_vectors:
  6780. kfree(vsi->tx_rings);
  6781. return ret;
  6782. }
  6783. /**
  6784. * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
  6785. * @pf: board private structure
  6786. * @type: type of VSI
  6787. *
  6788. * On error: returns error code (negative)
  6789. * On success: returns vsi index in PF (positive)
  6790. **/
  6791. static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
  6792. {
  6793. int ret = -ENODEV;
  6794. struct i40e_vsi *vsi;
  6795. int vsi_idx;
  6796. int i;
  6797. /* Need to protect the allocation of the VSIs at the PF level */
  6798. mutex_lock(&pf->switch_mutex);
  6799. /* VSI list may be fragmented if VSI creation/destruction has
  6800. * been happening. We can afford to do a quick scan to look
  6801. * for any free VSIs in the list.
  6802. *
  6803. * find next empty vsi slot, looping back around if necessary
  6804. */
  6805. i = pf->next_vsi;
  6806. while (i < pf->num_alloc_vsi && pf->vsi[i])
  6807. i++;
  6808. if (i >= pf->num_alloc_vsi) {
  6809. i = 0;
  6810. while (i < pf->next_vsi && pf->vsi[i])
  6811. i++;
  6812. }
  6813. if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
  6814. vsi_idx = i; /* Found one! */
  6815. } else {
  6816. ret = -ENODEV;
  6817. goto unlock_pf; /* out of VSI slots! */
  6818. }
  6819. pf->next_vsi = ++i;
  6820. vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
  6821. if (!vsi) {
  6822. ret = -ENOMEM;
  6823. goto unlock_pf;
  6824. }
  6825. vsi->type = type;
  6826. vsi->back = pf;
  6827. set_bit(__I40E_VSI_DOWN, vsi->state);
  6828. vsi->flags = 0;
  6829. vsi->idx = vsi_idx;
  6830. vsi->int_rate_limit = 0;
  6831. vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
  6832. pf->rss_table_size : 64;
  6833. vsi->netdev_registered = false;
  6834. vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
  6835. hash_init(vsi->mac_filter_hash);
  6836. vsi->irqs_ready = false;
  6837. ret = i40e_set_num_rings_in_vsi(vsi);
  6838. if (ret)
  6839. goto err_rings;
  6840. ret = i40e_vsi_alloc_arrays(vsi, true);
  6841. if (ret)
  6842. goto err_rings;
  6843. /* Setup default MSIX irq handler for VSI */
  6844. i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
  6845. /* Initialize VSI lock */
  6846. spin_lock_init(&vsi->mac_filter_hash_lock);
  6847. pf->vsi[vsi_idx] = vsi;
  6848. ret = vsi_idx;
  6849. goto unlock_pf;
  6850. err_rings:
  6851. pf->next_vsi = i - 1;
  6852. kfree(vsi);
  6853. unlock_pf:
  6854. mutex_unlock(&pf->switch_mutex);
  6855. return ret;
  6856. }
  6857. /**
  6858. * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
  6859. * @type: VSI pointer
  6860. * @free_qvectors: a bool to specify if q_vectors need to be freed.
  6861. *
  6862. * On error: returns error code (negative)
  6863. * On success: returns 0
  6864. **/
  6865. static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
  6866. {
  6867. /* free the ring and vector containers */
  6868. if (free_qvectors) {
  6869. kfree(vsi->q_vectors);
  6870. vsi->q_vectors = NULL;
  6871. }
  6872. kfree(vsi->tx_rings);
  6873. vsi->tx_rings = NULL;
  6874. vsi->rx_rings = NULL;
  6875. vsi->xdp_rings = NULL;
  6876. }
  6877. /**
  6878. * i40e_clear_rss_config_user - clear the user configured RSS hash keys
  6879. * and lookup table
  6880. * @vsi: Pointer to VSI structure
  6881. */
  6882. static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
  6883. {
  6884. if (!vsi)
  6885. return;
  6886. kfree(vsi->rss_hkey_user);
  6887. vsi->rss_hkey_user = NULL;
  6888. kfree(vsi->rss_lut_user);
  6889. vsi->rss_lut_user = NULL;
  6890. }
  6891. /**
  6892. * i40e_vsi_clear - Deallocate the VSI provided
  6893. * @vsi: the VSI being un-configured
  6894. **/
  6895. static int i40e_vsi_clear(struct i40e_vsi *vsi)
  6896. {
  6897. struct i40e_pf *pf;
  6898. if (!vsi)
  6899. return 0;
  6900. if (!vsi->back)
  6901. goto free_vsi;
  6902. pf = vsi->back;
  6903. mutex_lock(&pf->switch_mutex);
  6904. if (!pf->vsi[vsi->idx]) {
  6905. dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](%p,type %d)\n",
  6906. vsi->idx, vsi->idx, vsi, vsi->type);
  6907. goto unlock_vsi;
  6908. }
  6909. if (pf->vsi[vsi->idx] != vsi) {
  6910. dev_err(&pf->pdev->dev,
  6911. "pf->vsi[%d](%p, type %d) != vsi[%d](%p,type %d): no free!\n",
  6912. pf->vsi[vsi->idx]->idx,
  6913. pf->vsi[vsi->idx],
  6914. pf->vsi[vsi->idx]->type,
  6915. vsi->idx, vsi, vsi->type);
  6916. goto unlock_vsi;
  6917. }
  6918. /* updates the PF for this cleared vsi */
  6919. i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
  6920. i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
  6921. i40e_vsi_free_arrays(vsi, true);
  6922. i40e_clear_rss_config_user(vsi);
  6923. pf->vsi[vsi->idx] = NULL;
  6924. if (vsi->idx < pf->next_vsi)
  6925. pf->next_vsi = vsi->idx;
  6926. unlock_vsi:
  6927. mutex_unlock(&pf->switch_mutex);
  6928. free_vsi:
  6929. kfree(vsi);
  6930. return 0;
  6931. }
  6932. /**
  6933. * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
  6934. * @vsi: the VSI being cleaned
  6935. **/
  6936. static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
  6937. {
  6938. int i;
  6939. if (vsi->tx_rings && vsi->tx_rings[0]) {
  6940. for (i = 0; i < vsi->alloc_queue_pairs; i++) {
  6941. kfree_rcu(vsi->tx_rings[i], rcu);
  6942. vsi->tx_rings[i] = NULL;
  6943. vsi->rx_rings[i] = NULL;
  6944. if (vsi->xdp_rings)
  6945. vsi->xdp_rings[i] = NULL;
  6946. }
  6947. }
  6948. }
  6949. /**
  6950. * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
  6951. * @vsi: the VSI being configured
  6952. **/
  6953. static int i40e_alloc_rings(struct i40e_vsi *vsi)
  6954. {
  6955. int i, qpv = i40e_enabled_xdp_vsi(vsi) ? 3 : 2;
  6956. struct i40e_pf *pf = vsi->back;
  6957. struct i40e_ring *ring;
  6958. /* Set basic values in the rings to be used later during open() */
  6959. for (i = 0; i < vsi->alloc_queue_pairs; i++) {
  6960. /* allocate space for both Tx and Rx in one shot */
  6961. ring = kcalloc(qpv, sizeof(struct i40e_ring), GFP_KERNEL);
  6962. if (!ring)
  6963. goto err_out;
  6964. ring->queue_index = i;
  6965. ring->reg_idx = vsi->base_queue + i;
  6966. ring->ring_active = false;
  6967. ring->vsi = vsi;
  6968. ring->netdev = vsi->netdev;
  6969. ring->dev = &pf->pdev->dev;
  6970. ring->count = vsi->num_desc;
  6971. ring->size = 0;
  6972. ring->dcb_tc = 0;
  6973. if (vsi->back->flags & I40E_FLAG_WB_ON_ITR_CAPABLE)
  6974. ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
  6975. ring->tx_itr_setting = pf->tx_itr_default;
  6976. vsi->tx_rings[i] = ring++;
  6977. if (!i40e_enabled_xdp_vsi(vsi))
  6978. goto setup_rx;
  6979. ring->queue_index = vsi->alloc_queue_pairs + i;
  6980. ring->reg_idx = vsi->base_queue + ring->queue_index;
  6981. ring->ring_active = false;
  6982. ring->vsi = vsi;
  6983. ring->netdev = NULL;
  6984. ring->dev = &pf->pdev->dev;
  6985. ring->count = vsi->num_desc;
  6986. ring->size = 0;
  6987. ring->dcb_tc = 0;
  6988. if (vsi->back->flags & I40E_FLAG_WB_ON_ITR_CAPABLE)
  6989. ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
  6990. set_ring_xdp(ring);
  6991. ring->tx_itr_setting = pf->tx_itr_default;
  6992. vsi->xdp_rings[i] = ring++;
  6993. setup_rx:
  6994. ring->queue_index = i;
  6995. ring->reg_idx = vsi->base_queue + i;
  6996. ring->ring_active = false;
  6997. ring->vsi = vsi;
  6998. ring->netdev = vsi->netdev;
  6999. ring->dev = &pf->pdev->dev;
  7000. ring->count = vsi->num_desc;
  7001. ring->size = 0;
  7002. ring->dcb_tc = 0;
  7003. ring->rx_itr_setting = pf->rx_itr_default;
  7004. vsi->rx_rings[i] = ring;
  7005. }
  7006. return 0;
  7007. err_out:
  7008. i40e_vsi_clear_rings(vsi);
  7009. return -ENOMEM;
  7010. }
  7011. /**
  7012. * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
  7013. * @pf: board private structure
  7014. * @vectors: the number of MSI-X vectors to request
  7015. *
  7016. * Returns the number of vectors reserved, or error
  7017. **/
  7018. static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
  7019. {
  7020. vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
  7021. I40E_MIN_MSIX, vectors);
  7022. if (vectors < 0) {
  7023. dev_info(&pf->pdev->dev,
  7024. "MSI-X vector reservation failed: %d\n", vectors);
  7025. vectors = 0;
  7026. }
  7027. return vectors;
  7028. }
  7029. /**
  7030. * i40e_init_msix - Setup the MSIX capability
  7031. * @pf: board private structure
  7032. *
  7033. * Work with the OS to set up the MSIX vectors needed.
  7034. *
  7035. * Returns the number of vectors reserved or negative on failure
  7036. **/
  7037. static int i40e_init_msix(struct i40e_pf *pf)
  7038. {
  7039. struct i40e_hw *hw = &pf->hw;
  7040. int cpus, extra_vectors;
  7041. int vectors_left;
  7042. int v_budget, i;
  7043. int v_actual;
  7044. int iwarp_requested = 0;
  7045. if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
  7046. return -ENODEV;
  7047. /* The number of vectors we'll request will be comprised of:
  7048. * - Add 1 for "other" cause for Admin Queue events, etc.
  7049. * - The number of LAN queue pairs
  7050. * - Queues being used for RSS.
  7051. * We don't need as many as max_rss_size vectors.
  7052. * use rss_size instead in the calculation since that
  7053. * is governed by number of cpus in the system.
  7054. * - assumes symmetric Tx/Rx pairing
  7055. * - The number of VMDq pairs
  7056. * - The CPU count within the NUMA node if iWARP is enabled
  7057. * Once we count this up, try the request.
  7058. *
  7059. * If we can't get what we want, we'll simplify to nearly nothing
  7060. * and try again. If that still fails, we punt.
  7061. */
  7062. vectors_left = hw->func_caps.num_msix_vectors;
  7063. v_budget = 0;
  7064. /* reserve one vector for miscellaneous handler */
  7065. if (vectors_left) {
  7066. v_budget++;
  7067. vectors_left--;
  7068. }
  7069. /* reserve some vectors for the main PF traffic queues. Initially we
  7070. * only reserve at most 50% of the available vectors, in the case that
  7071. * the number of online CPUs is large. This ensures that we can enable
  7072. * extra features as well. Once we've enabled the other features, we
  7073. * will use any remaining vectors to reach as close as we can to the
  7074. * number of online CPUs.
  7075. */
  7076. cpus = num_online_cpus();
  7077. pf->num_lan_msix = min_t(int, cpus, vectors_left / 2);
  7078. vectors_left -= pf->num_lan_msix;
  7079. /* reserve one vector for sideband flow director */
  7080. if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
  7081. if (vectors_left) {
  7082. pf->num_fdsb_msix = 1;
  7083. v_budget++;
  7084. vectors_left--;
  7085. } else {
  7086. pf->num_fdsb_msix = 0;
  7087. }
  7088. }
  7089. /* can we reserve enough for iWARP? */
  7090. if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
  7091. iwarp_requested = pf->num_iwarp_msix;
  7092. if (!vectors_left)
  7093. pf->num_iwarp_msix = 0;
  7094. else if (vectors_left < pf->num_iwarp_msix)
  7095. pf->num_iwarp_msix = 1;
  7096. v_budget += pf->num_iwarp_msix;
  7097. vectors_left -= pf->num_iwarp_msix;
  7098. }
  7099. /* any vectors left over go for VMDq support */
  7100. if (pf->flags & I40E_FLAG_VMDQ_ENABLED) {
  7101. int vmdq_vecs_wanted = pf->num_vmdq_vsis * pf->num_vmdq_qps;
  7102. int vmdq_vecs = min_t(int, vectors_left, vmdq_vecs_wanted);
  7103. if (!vectors_left) {
  7104. pf->num_vmdq_msix = 0;
  7105. pf->num_vmdq_qps = 0;
  7106. } else {
  7107. /* if we're short on vectors for what's desired, we limit
  7108. * the queues per vmdq. If this is still more than are
  7109. * available, the user will need to change the number of
  7110. * queues/vectors used by the PF later with the ethtool
  7111. * channels command
  7112. */
  7113. if (vmdq_vecs < vmdq_vecs_wanted)
  7114. pf->num_vmdq_qps = 1;
  7115. pf->num_vmdq_msix = pf->num_vmdq_qps;
  7116. v_budget += vmdq_vecs;
  7117. vectors_left -= vmdq_vecs;
  7118. }
  7119. }
  7120. /* On systems with a large number of SMP cores, we previously limited
  7121. * the number of vectors for num_lan_msix to be at most 50% of the
  7122. * available vectors, to allow for other features. Now, we add back
  7123. * the remaining vectors. However, we ensure that the total
  7124. * num_lan_msix will not exceed num_online_cpus(). To do this, we
  7125. * calculate the number of vectors we can add without going over the
  7126. * cap of CPUs. For systems with a small number of CPUs this will be
  7127. * zero.
  7128. */
  7129. extra_vectors = min_t(int, cpus - pf->num_lan_msix, vectors_left);
  7130. pf->num_lan_msix += extra_vectors;
  7131. vectors_left -= extra_vectors;
  7132. WARN(vectors_left < 0,
  7133. "Calculation of remaining vectors underflowed. This is an accounting bug when determining total MSI-X vectors.\n");
  7134. v_budget += pf->num_lan_msix;
  7135. pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
  7136. GFP_KERNEL);
  7137. if (!pf->msix_entries)
  7138. return -ENOMEM;
  7139. for (i = 0; i < v_budget; i++)
  7140. pf->msix_entries[i].entry = i;
  7141. v_actual = i40e_reserve_msix_vectors(pf, v_budget);
  7142. if (v_actual < I40E_MIN_MSIX) {
  7143. pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
  7144. kfree(pf->msix_entries);
  7145. pf->msix_entries = NULL;
  7146. pci_disable_msix(pf->pdev);
  7147. return -ENODEV;
  7148. } else if (v_actual == I40E_MIN_MSIX) {
  7149. /* Adjust for minimal MSIX use */
  7150. pf->num_vmdq_vsis = 0;
  7151. pf->num_vmdq_qps = 0;
  7152. pf->num_lan_qps = 1;
  7153. pf->num_lan_msix = 1;
  7154. } else if (!vectors_left) {
  7155. /* If we have limited resources, we will start with no vectors
  7156. * for the special features and then allocate vectors to some
  7157. * of these features based on the policy and at the end disable
  7158. * the features that did not get any vectors.
  7159. */
  7160. int vec;
  7161. dev_info(&pf->pdev->dev,
  7162. "MSI-X vector limit reached, attempting to redistribute vectors\n");
  7163. /* reserve the misc vector */
  7164. vec = v_actual - 1;
  7165. /* Scale vector usage down */
  7166. pf->num_vmdq_msix = 1; /* force VMDqs to only one vector */
  7167. pf->num_vmdq_vsis = 1;
  7168. pf->num_vmdq_qps = 1;
  7169. /* partition out the remaining vectors */
  7170. switch (vec) {
  7171. case 2:
  7172. pf->num_lan_msix = 1;
  7173. break;
  7174. case 3:
  7175. if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
  7176. pf->num_lan_msix = 1;
  7177. pf->num_iwarp_msix = 1;
  7178. } else {
  7179. pf->num_lan_msix = 2;
  7180. }
  7181. break;
  7182. default:
  7183. if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
  7184. pf->num_iwarp_msix = min_t(int, (vec / 3),
  7185. iwarp_requested);
  7186. pf->num_vmdq_vsis = min_t(int, (vec / 3),
  7187. I40E_DEFAULT_NUM_VMDQ_VSI);
  7188. } else {
  7189. pf->num_vmdq_vsis = min_t(int, (vec / 2),
  7190. I40E_DEFAULT_NUM_VMDQ_VSI);
  7191. }
  7192. if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
  7193. pf->num_fdsb_msix = 1;
  7194. vec--;
  7195. }
  7196. pf->num_lan_msix = min_t(int,
  7197. (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
  7198. pf->num_lan_msix);
  7199. pf->num_lan_qps = pf->num_lan_msix;
  7200. break;
  7201. }
  7202. }
  7203. if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
  7204. (pf->num_fdsb_msix == 0)) {
  7205. dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n");
  7206. pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
  7207. }
  7208. if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
  7209. (pf->num_vmdq_msix == 0)) {
  7210. dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
  7211. pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
  7212. }
  7213. if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
  7214. (pf->num_iwarp_msix == 0)) {
  7215. dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
  7216. pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
  7217. }
  7218. i40e_debug(&pf->hw, I40E_DEBUG_INIT,
  7219. "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n",
  7220. pf->num_lan_msix,
  7221. pf->num_vmdq_msix * pf->num_vmdq_vsis,
  7222. pf->num_fdsb_msix,
  7223. pf->num_iwarp_msix);
  7224. return v_actual;
  7225. }
  7226. /**
  7227. * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
  7228. * @vsi: the VSI being configured
  7229. * @v_idx: index of the vector in the vsi struct
  7230. * @cpu: cpu to be used on affinity_mask
  7231. *
  7232. * We allocate one q_vector. If allocation fails we return -ENOMEM.
  7233. **/
  7234. static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx, int cpu)
  7235. {
  7236. struct i40e_q_vector *q_vector;
  7237. /* allocate q_vector */
  7238. q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
  7239. if (!q_vector)
  7240. return -ENOMEM;
  7241. q_vector->vsi = vsi;
  7242. q_vector->v_idx = v_idx;
  7243. cpumask_set_cpu(cpu, &q_vector->affinity_mask);
  7244. if (vsi->netdev)
  7245. netif_napi_add(vsi->netdev, &q_vector->napi,
  7246. i40e_napi_poll, NAPI_POLL_WEIGHT);
  7247. q_vector->rx.latency_range = I40E_LOW_LATENCY;
  7248. q_vector->tx.latency_range = I40E_LOW_LATENCY;
  7249. /* tie q_vector and vsi together */
  7250. vsi->q_vectors[v_idx] = q_vector;
  7251. return 0;
  7252. }
  7253. /**
  7254. * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
  7255. * @vsi: the VSI being configured
  7256. *
  7257. * We allocate one q_vector per queue interrupt. If allocation fails we
  7258. * return -ENOMEM.
  7259. **/
  7260. static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
  7261. {
  7262. struct i40e_pf *pf = vsi->back;
  7263. int err, v_idx, num_q_vectors, current_cpu;
  7264. /* if not MSIX, give the one vector only to the LAN VSI */
  7265. if (pf->flags & I40E_FLAG_MSIX_ENABLED)
  7266. num_q_vectors = vsi->num_q_vectors;
  7267. else if (vsi == pf->vsi[pf->lan_vsi])
  7268. num_q_vectors = 1;
  7269. else
  7270. return -EINVAL;
  7271. current_cpu = cpumask_first(cpu_online_mask);
  7272. for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
  7273. err = i40e_vsi_alloc_q_vector(vsi, v_idx, current_cpu);
  7274. if (err)
  7275. goto err_out;
  7276. current_cpu = cpumask_next(current_cpu, cpu_online_mask);
  7277. if (unlikely(current_cpu >= nr_cpu_ids))
  7278. current_cpu = cpumask_first(cpu_online_mask);
  7279. }
  7280. return 0;
  7281. err_out:
  7282. while (v_idx--)
  7283. i40e_free_q_vector(vsi, v_idx);
  7284. return err;
  7285. }
  7286. /**
  7287. * i40e_init_interrupt_scheme - Determine proper interrupt scheme
  7288. * @pf: board private structure to initialize
  7289. **/
  7290. static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
  7291. {
  7292. int vectors = 0;
  7293. ssize_t size;
  7294. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  7295. vectors = i40e_init_msix(pf);
  7296. if (vectors < 0) {
  7297. pf->flags &= ~(I40E_FLAG_MSIX_ENABLED |
  7298. I40E_FLAG_IWARP_ENABLED |
  7299. I40E_FLAG_RSS_ENABLED |
  7300. I40E_FLAG_DCB_CAPABLE |
  7301. I40E_FLAG_DCB_ENABLED |
  7302. I40E_FLAG_SRIOV_ENABLED |
  7303. I40E_FLAG_FD_SB_ENABLED |
  7304. I40E_FLAG_FD_ATR_ENABLED |
  7305. I40E_FLAG_VMDQ_ENABLED);
  7306. /* rework the queue expectations without MSIX */
  7307. i40e_determine_queue_usage(pf);
  7308. }
  7309. }
  7310. if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
  7311. (pf->flags & I40E_FLAG_MSI_ENABLED)) {
  7312. dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
  7313. vectors = pci_enable_msi(pf->pdev);
  7314. if (vectors < 0) {
  7315. dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
  7316. vectors);
  7317. pf->flags &= ~I40E_FLAG_MSI_ENABLED;
  7318. }
  7319. vectors = 1; /* one MSI or Legacy vector */
  7320. }
  7321. if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
  7322. dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
  7323. /* set up vector assignment tracking */
  7324. size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
  7325. pf->irq_pile = kzalloc(size, GFP_KERNEL);
  7326. if (!pf->irq_pile) {
  7327. dev_err(&pf->pdev->dev, "error allocating irq_pile memory\n");
  7328. return -ENOMEM;
  7329. }
  7330. pf->irq_pile->num_entries = vectors;
  7331. pf->irq_pile->search_hint = 0;
  7332. /* track first vector for misc interrupts, ignore return */
  7333. (void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
  7334. return 0;
  7335. }
  7336. /**
  7337. * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
  7338. * @pf: board private structure
  7339. *
  7340. * This sets up the handler for MSIX 0, which is used to manage the
  7341. * non-queue interrupts, e.g. AdminQ and errors. This is not used
  7342. * when in MSI or Legacy interrupt mode.
  7343. **/
  7344. static int i40e_setup_misc_vector(struct i40e_pf *pf)
  7345. {
  7346. struct i40e_hw *hw = &pf->hw;
  7347. int err = 0;
  7348. /* Only request the irq if this is the first time through, and
  7349. * not when we're rebuilding after a Reset
  7350. */
  7351. if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state)) {
  7352. err = request_irq(pf->msix_entries[0].vector,
  7353. i40e_intr, 0, pf->int_name, pf);
  7354. if (err) {
  7355. dev_info(&pf->pdev->dev,
  7356. "request_irq for %s failed: %d\n",
  7357. pf->int_name, err);
  7358. return -EFAULT;
  7359. }
  7360. }
  7361. i40e_enable_misc_int_causes(pf);
  7362. /* associate no queues to the misc vector */
  7363. wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
  7364. wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K);
  7365. i40e_flush(hw);
  7366. i40e_irq_dynamic_enable_icr0(pf, true);
  7367. return err;
  7368. }
  7369. /**
  7370. * i40e_config_rss_aq - Prepare for RSS using AQ commands
  7371. * @vsi: vsi structure
  7372. * @seed: RSS hash seed
  7373. **/
  7374. static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
  7375. u8 *lut, u16 lut_size)
  7376. {
  7377. struct i40e_pf *pf = vsi->back;
  7378. struct i40e_hw *hw = &pf->hw;
  7379. int ret = 0;
  7380. if (seed) {
  7381. struct i40e_aqc_get_set_rss_key_data *seed_dw =
  7382. (struct i40e_aqc_get_set_rss_key_data *)seed;
  7383. ret = i40e_aq_set_rss_key(hw, vsi->id, seed_dw);
  7384. if (ret) {
  7385. dev_info(&pf->pdev->dev,
  7386. "Cannot set RSS key, err %s aq_err %s\n",
  7387. i40e_stat_str(hw, ret),
  7388. i40e_aq_str(hw, hw->aq.asq_last_status));
  7389. return ret;
  7390. }
  7391. }
  7392. if (lut) {
  7393. bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
  7394. ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
  7395. if (ret) {
  7396. dev_info(&pf->pdev->dev,
  7397. "Cannot set RSS lut, err %s aq_err %s\n",
  7398. i40e_stat_str(hw, ret),
  7399. i40e_aq_str(hw, hw->aq.asq_last_status));
  7400. return ret;
  7401. }
  7402. }
  7403. return ret;
  7404. }
  7405. /**
  7406. * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
  7407. * @vsi: Pointer to vsi structure
  7408. * @seed: Buffter to store the hash keys
  7409. * @lut: Buffer to store the lookup table entries
  7410. * @lut_size: Size of buffer to store the lookup table entries
  7411. *
  7412. * Return 0 on success, negative on failure
  7413. */
  7414. static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
  7415. u8 *lut, u16 lut_size)
  7416. {
  7417. struct i40e_pf *pf = vsi->back;
  7418. struct i40e_hw *hw = &pf->hw;
  7419. int ret = 0;
  7420. if (seed) {
  7421. ret = i40e_aq_get_rss_key(hw, vsi->id,
  7422. (struct i40e_aqc_get_set_rss_key_data *)seed);
  7423. if (ret) {
  7424. dev_info(&pf->pdev->dev,
  7425. "Cannot get RSS key, err %s aq_err %s\n",
  7426. i40e_stat_str(&pf->hw, ret),
  7427. i40e_aq_str(&pf->hw,
  7428. pf->hw.aq.asq_last_status));
  7429. return ret;
  7430. }
  7431. }
  7432. if (lut) {
  7433. bool pf_lut = vsi->type == I40E_VSI_MAIN ? true : false;
  7434. ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
  7435. if (ret) {
  7436. dev_info(&pf->pdev->dev,
  7437. "Cannot get RSS lut, err %s aq_err %s\n",
  7438. i40e_stat_str(&pf->hw, ret),
  7439. i40e_aq_str(&pf->hw,
  7440. pf->hw.aq.asq_last_status));
  7441. return ret;
  7442. }
  7443. }
  7444. return ret;
  7445. }
  7446. /**
  7447. * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
  7448. * @vsi: VSI structure
  7449. **/
  7450. static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
  7451. {
  7452. u8 seed[I40E_HKEY_ARRAY_SIZE];
  7453. struct i40e_pf *pf = vsi->back;
  7454. u8 *lut;
  7455. int ret;
  7456. if (!(pf->flags & I40E_FLAG_RSS_AQ_CAPABLE))
  7457. return 0;
  7458. if (!vsi->rss_size)
  7459. vsi->rss_size = min_t(int, pf->alloc_rss_size,
  7460. vsi->num_queue_pairs);
  7461. if (!vsi->rss_size)
  7462. return -EINVAL;
  7463. lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
  7464. if (!lut)
  7465. return -ENOMEM;
  7466. /* Use the user configured hash keys and lookup table if there is one,
  7467. * otherwise use default
  7468. */
  7469. if (vsi->rss_lut_user)
  7470. memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
  7471. else
  7472. i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
  7473. if (vsi->rss_hkey_user)
  7474. memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
  7475. else
  7476. netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
  7477. ret = i40e_config_rss_aq(vsi, seed, lut, vsi->rss_table_size);
  7478. kfree(lut);
  7479. return ret;
  7480. }
  7481. /**
  7482. * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
  7483. * @vsi: Pointer to vsi structure
  7484. * @seed: RSS hash seed
  7485. * @lut: Lookup table
  7486. * @lut_size: Lookup table size
  7487. *
  7488. * Returns 0 on success, negative on failure
  7489. **/
  7490. static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
  7491. const u8 *lut, u16 lut_size)
  7492. {
  7493. struct i40e_pf *pf = vsi->back;
  7494. struct i40e_hw *hw = &pf->hw;
  7495. u16 vf_id = vsi->vf_id;
  7496. u8 i;
  7497. /* Fill out hash function seed */
  7498. if (seed) {
  7499. u32 *seed_dw = (u32 *)seed;
  7500. if (vsi->type == I40E_VSI_MAIN) {
  7501. for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
  7502. wr32(hw, I40E_PFQF_HKEY(i), seed_dw[i]);
  7503. } else if (vsi->type == I40E_VSI_SRIOV) {
  7504. for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
  7505. wr32(hw, I40E_VFQF_HKEY1(i, vf_id), seed_dw[i]);
  7506. } else {
  7507. dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
  7508. }
  7509. }
  7510. if (lut) {
  7511. u32 *lut_dw = (u32 *)lut;
  7512. if (vsi->type == I40E_VSI_MAIN) {
  7513. if (lut_size != I40E_HLUT_ARRAY_SIZE)
  7514. return -EINVAL;
  7515. for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
  7516. wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
  7517. } else if (vsi->type == I40E_VSI_SRIOV) {
  7518. if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
  7519. return -EINVAL;
  7520. for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
  7521. wr32(hw, I40E_VFQF_HLUT1(i, vf_id), lut_dw[i]);
  7522. } else {
  7523. dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
  7524. }
  7525. }
  7526. i40e_flush(hw);
  7527. return 0;
  7528. }
  7529. /**
  7530. * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
  7531. * @vsi: Pointer to VSI structure
  7532. * @seed: Buffer to store the keys
  7533. * @lut: Buffer to store the lookup table entries
  7534. * @lut_size: Size of buffer to store the lookup table entries
  7535. *
  7536. * Returns 0 on success, negative on failure
  7537. */
  7538. static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
  7539. u8 *lut, u16 lut_size)
  7540. {
  7541. struct i40e_pf *pf = vsi->back;
  7542. struct i40e_hw *hw = &pf->hw;
  7543. u16 i;
  7544. if (seed) {
  7545. u32 *seed_dw = (u32 *)seed;
  7546. for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
  7547. seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
  7548. }
  7549. if (lut) {
  7550. u32 *lut_dw = (u32 *)lut;
  7551. if (lut_size != I40E_HLUT_ARRAY_SIZE)
  7552. return -EINVAL;
  7553. for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
  7554. lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
  7555. }
  7556. return 0;
  7557. }
  7558. /**
  7559. * i40e_config_rss - Configure RSS keys and lut
  7560. * @vsi: Pointer to VSI structure
  7561. * @seed: RSS hash seed
  7562. * @lut: Lookup table
  7563. * @lut_size: Lookup table size
  7564. *
  7565. * Returns 0 on success, negative on failure
  7566. */
  7567. int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
  7568. {
  7569. struct i40e_pf *pf = vsi->back;
  7570. if (pf->flags & I40E_FLAG_RSS_AQ_CAPABLE)
  7571. return i40e_config_rss_aq(vsi, seed, lut, lut_size);
  7572. else
  7573. return i40e_config_rss_reg(vsi, seed, lut, lut_size);
  7574. }
  7575. /**
  7576. * i40e_get_rss - Get RSS keys and lut
  7577. * @vsi: Pointer to VSI structure
  7578. * @seed: Buffer to store the keys
  7579. * @lut: Buffer to store the lookup table entries
  7580. * lut_size: Size of buffer to store the lookup table entries
  7581. *
  7582. * Returns 0 on success, negative on failure
  7583. */
  7584. int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
  7585. {
  7586. struct i40e_pf *pf = vsi->back;
  7587. if (pf->flags & I40E_FLAG_RSS_AQ_CAPABLE)
  7588. return i40e_get_rss_aq(vsi, seed, lut, lut_size);
  7589. else
  7590. return i40e_get_rss_reg(vsi, seed, lut, lut_size);
  7591. }
  7592. /**
  7593. * i40e_fill_rss_lut - Fill the RSS lookup table with default values
  7594. * @pf: Pointer to board private structure
  7595. * @lut: Lookup table
  7596. * @rss_table_size: Lookup table size
  7597. * @rss_size: Range of queue number for hashing
  7598. */
  7599. void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
  7600. u16 rss_table_size, u16 rss_size)
  7601. {
  7602. u16 i;
  7603. for (i = 0; i < rss_table_size; i++)
  7604. lut[i] = i % rss_size;
  7605. }
  7606. /**
  7607. * i40e_pf_config_rss - Prepare for RSS if used
  7608. * @pf: board private structure
  7609. **/
  7610. static int i40e_pf_config_rss(struct i40e_pf *pf)
  7611. {
  7612. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  7613. u8 seed[I40E_HKEY_ARRAY_SIZE];
  7614. u8 *lut;
  7615. struct i40e_hw *hw = &pf->hw;
  7616. u32 reg_val;
  7617. u64 hena;
  7618. int ret;
  7619. /* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
  7620. hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
  7621. ((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
  7622. hena |= i40e_pf_get_default_rss_hena(pf);
  7623. i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
  7624. i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
  7625. /* Determine the RSS table size based on the hardware capabilities */
  7626. reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
  7627. reg_val = (pf->rss_table_size == 512) ?
  7628. (reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
  7629. (reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
  7630. i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
  7631. /* Determine the RSS size of the VSI */
  7632. if (!vsi->rss_size) {
  7633. u16 qcount;
  7634. qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
  7635. vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
  7636. }
  7637. if (!vsi->rss_size)
  7638. return -EINVAL;
  7639. lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
  7640. if (!lut)
  7641. return -ENOMEM;
  7642. /* Use user configured lut if there is one, otherwise use default */
  7643. if (vsi->rss_lut_user)
  7644. memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
  7645. else
  7646. i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
  7647. /* Use user configured hash key if there is one, otherwise
  7648. * use default.
  7649. */
  7650. if (vsi->rss_hkey_user)
  7651. memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
  7652. else
  7653. netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
  7654. ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
  7655. kfree(lut);
  7656. return ret;
  7657. }
  7658. /**
  7659. * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
  7660. * @pf: board private structure
  7661. * @queue_count: the requested queue count for rss.
  7662. *
  7663. * returns 0 if rss is not enabled, if enabled returns the final rss queue
  7664. * count which may be different from the requested queue count.
  7665. * Note: expects to be called while under rtnl_lock()
  7666. **/
  7667. int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
  7668. {
  7669. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  7670. int new_rss_size;
  7671. if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
  7672. return 0;
  7673. new_rss_size = min_t(int, queue_count, pf->rss_size_max);
  7674. if (queue_count != vsi->num_queue_pairs) {
  7675. u16 qcount;
  7676. vsi->req_queue_pairs = queue_count;
  7677. i40e_prep_for_reset(pf, true);
  7678. pf->alloc_rss_size = new_rss_size;
  7679. i40e_reset_and_rebuild(pf, true, true);
  7680. /* Discard the user configured hash keys and lut, if less
  7681. * queues are enabled.
  7682. */
  7683. if (queue_count < vsi->rss_size) {
  7684. i40e_clear_rss_config_user(vsi);
  7685. dev_dbg(&pf->pdev->dev,
  7686. "discard user configured hash keys and lut\n");
  7687. }
  7688. /* Reset vsi->rss_size, as number of enabled queues changed */
  7689. qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
  7690. vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
  7691. i40e_pf_config_rss(pf);
  7692. }
  7693. dev_info(&pf->pdev->dev, "User requested queue count/HW max RSS count: %d/%d\n",
  7694. vsi->req_queue_pairs, pf->rss_size_max);
  7695. return pf->alloc_rss_size;
  7696. }
  7697. /**
  7698. * i40e_get_partition_bw_setting - Retrieve BW settings for this PF partition
  7699. * @pf: board private structure
  7700. **/
  7701. i40e_status i40e_get_partition_bw_setting(struct i40e_pf *pf)
  7702. {
  7703. i40e_status status;
  7704. bool min_valid, max_valid;
  7705. u32 max_bw, min_bw;
  7706. status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
  7707. &min_valid, &max_valid);
  7708. if (!status) {
  7709. if (min_valid)
  7710. pf->min_bw = min_bw;
  7711. if (max_valid)
  7712. pf->max_bw = max_bw;
  7713. }
  7714. return status;
  7715. }
  7716. /**
  7717. * i40e_set_partition_bw_setting - Set BW settings for this PF partition
  7718. * @pf: board private structure
  7719. **/
  7720. i40e_status i40e_set_partition_bw_setting(struct i40e_pf *pf)
  7721. {
  7722. struct i40e_aqc_configure_partition_bw_data bw_data;
  7723. i40e_status status;
  7724. /* Set the valid bit for this PF */
  7725. bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
  7726. bw_data.max_bw[pf->hw.pf_id] = pf->max_bw & I40E_ALT_BW_VALUE_MASK;
  7727. bw_data.min_bw[pf->hw.pf_id] = pf->min_bw & I40E_ALT_BW_VALUE_MASK;
  7728. /* Set the new bandwidths */
  7729. status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
  7730. return status;
  7731. }
  7732. /**
  7733. * i40e_commit_partition_bw_setting - Commit BW settings for this PF partition
  7734. * @pf: board private structure
  7735. **/
  7736. i40e_status i40e_commit_partition_bw_setting(struct i40e_pf *pf)
  7737. {
  7738. /* Commit temporary BW setting to permanent NVM image */
  7739. enum i40e_admin_queue_err last_aq_status;
  7740. i40e_status ret;
  7741. u16 nvm_word;
  7742. if (pf->hw.partition_id != 1) {
  7743. dev_info(&pf->pdev->dev,
  7744. "Commit BW only works on partition 1! This is partition %d",
  7745. pf->hw.partition_id);
  7746. ret = I40E_NOT_SUPPORTED;
  7747. goto bw_commit_out;
  7748. }
  7749. /* Acquire NVM for read access */
  7750. ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
  7751. last_aq_status = pf->hw.aq.asq_last_status;
  7752. if (ret) {
  7753. dev_info(&pf->pdev->dev,
  7754. "Cannot acquire NVM for read access, err %s aq_err %s\n",
  7755. i40e_stat_str(&pf->hw, ret),
  7756. i40e_aq_str(&pf->hw, last_aq_status));
  7757. goto bw_commit_out;
  7758. }
  7759. /* Read word 0x10 of NVM - SW compatibility word 1 */
  7760. ret = i40e_aq_read_nvm(&pf->hw,
  7761. I40E_SR_NVM_CONTROL_WORD,
  7762. 0x10, sizeof(nvm_word), &nvm_word,
  7763. false, NULL);
  7764. /* Save off last admin queue command status before releasing
  7765. * the NVM
  7766. */
  7767. last_aq_status = pf->hw.aq.asq_last_status;
  7768. i40e_release_nvm(&pf->hw);
  7769. if (ret) {
  7770. dev_info(&pf->pdev->dev, "NVM read error, err %s aq_err %s\n",
  7771. i40e_stat_str(&pf->hw, ret),
  7772. i40e_aq_str(&pf->hw, last_aq_status));
  7773. goto bw_commit_out;
  7774. }
  7775. /* Wait a bit for NVM release to complete */
  7776. msleep(50);
  7777. /* Acquire NVM for write access */
  7778. ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
  7779. last_aq_status = pf->hw.aq.asq_last_status;
  7780. if (ret) {
  7781. dev_info(&pf->pdev->dev,
  7782. "Cannot acquire NVM for write access, err %s aq_err %s\n",
  7783. i40e_stat_str(&pf->hw, ret),
  7784. i40e_aq_str(&pf->hw, last_aq_status));
  7785. goto bw_commit_out;
  7786. }
  7787. /* Write it back out unchanged to initiate update NVM,
  7788. * which will force a write of the shadow (alt) RAM to
  7789. * the NVM - thus storing the bandwidth values permanently.
  7790. */
  7791. ret = i40e_aq_update_nvm(&pf->hw,
  7792. I40E_SR_NVM_CONTROL_WORD,
  7793. 0x10, sizeof(nvm_word),
  7794. &nvm_word, true, NULL);
  7795. /* Save off last admin queue command status before releasing
  7796. * the NVM
  7797. */
  7798. last_aq_status = pf->hw.aq.asq_last_status;
  7799. i40e_release_nvm(&pf->hw);
  7800. if (ret)
  7801. dev_info(&pf->pdev->dev,
  7802. "BW settings NOT SAVED, err %s aq_err %s\n",
  7803. i40e_stat_str(&pf->hw, ret),
  7804. i40e_aq_str(&pf->hw, last_aq_status));
  7805. bw_commit_out:
  7806. return ret;
  7807. }
  7808. /**
  7809. * i40e_sw_init - Initialize general software structures (struct i40e_pf)
  7810. * @pf: board private structure to initialize
  7811. *
  7812. * i40e_sw_init initializes the Adapter private data structure.
  7813. * Fields are initialized based on PCI device information and
  7814. * OS network device settings (MTU size).
  7815. **/
  7816. static int i40e_sw_init(struct i40e_pf *pf)
  7817. {
  7818. int err = 0;
  7819. int size;
  7820. /* Set default capability flags */
  7821. pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
  7822. I40E_FLAG_MSI_ENABLED |
  7823. I40E_FLAG_MSIX_ENABLED;
  7824. /* Set default ITR */
  7825. pf->rx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_RX_DEF;
  7826. pf->tx_itr_default = I40E_ITR_DYNAMIC | I40E_ITR_TX_DEF;
  7827. /* Depending on PF configurations, it is possible that the RSS
  7828. * maximum might end up larger than the available queues
  7829. */
  7830. pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
  7831. pf->alloc_rss_size = 1;
  7832. pf->rss_table_size = pf->hw.func_caps.rss_table_size;
  7833. pf->rss_size_max = min_t(int, pf->rss_size_max,
  7834. pf->hw.func_caps.num_tx_qp);
  7835. if (pf->hw.func_caps.rss) {
  7836. pf->flags |= I40E_FLAG_RSS_ENABLED;
  7837. pf->alloc_rss_size = min_t(int, pf->rss_size_max,
  7838. num_online_cpus());
  7839. }
  7840. /* MFP mode enabled */
  7841. if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
  7842. pf->flags |= I40E_FLAG_MFP_ENABLED;
  7843. dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
  7844. if (i40e_get_partition_bw_setting(pf)) {
  7845. dev_warn(&pf->pdev->dev,
  7846. "Could not get partition bw settings\n");
  7847. } else {
  7848. dev_info(&pf->pdev->dev,
  7849. "Partition BW Min = %8.8x, Max = %8.8x\n",
  7850. pf->min_bw, pf->max_bw);
  7851. /* nudge the Tx scheduler */
  7852. i40e_set_partition_bw_setting(pf);
  7853. }
  7854. }
  7855. if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
  7856. (pf->hw.func_caps.fd_filters_best_effort > 0)) {
  7857. pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
  7858. pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
  7859. if (pf->flags & I40E_FLAG_MFP_ENABLED &&
  7860. pf->hw.num_partitions > 1)
  7861. dev_info(&pf->pdev->dev,
  7862. "Flow Director Sideband mode Disabled in MFP mode\n");
  7863. else
  7864. pf->flags |= I40E_FLAG_FD_SB_ENABLED;
  7865. pf->fdir_pf_filter_count =
  7866. pf->hw.func_caps.fd_filters_guaranteed;
  7867. pf->hw.fdir_shared_filter_count =
  7868. pf->hw.func_caps.fd_filters_best_effort;
  7869. }
  7870. if ((pf->hw.mac.type == I40E_MAC_XL710) &&
  7871. (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
  7872. (pf->hw.aq.fw_maj_ver < 4))) {
  7873. pf->flags |= I40E_FLAG_RESTART_AUTONEG;
  7874. /* No DCB support for FW < v4.33 */
  7875. pf->flags |= I40E_FLAG_NO_DCB_SUPPORT;
  7876. }
  7877. /* Disable FW LLDP if FW < v4.3 */
  7878. if ((pf->hw.mac.type == I40E_MAC_XL710) &&
  7879. (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
  7880. (pf->hw.aq.fw_maj_ver < 4)))
  7881. pf->flags |= I40E_FLAG_STOP_FW_LLDP;
  7882. /* Use the FW Set LLDP MIB API if FW > v4.40 */
  7883. if ((pf->hw.mac.type == I40E_MAC_XL710) &&
  7884. (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver >= 40)) ||
  7885. (pf->hw.aq.fw_maj_ver >= 5)))
  7886. pf->flags |= I40E_FLAG_USE_SET_LLDP_MIB;
  7887. if (pf->hw.func_caps.vmdq) {
  7888. pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
  7889. pf->flags |= I40E_FLAG_VMDQ_ENABLED;
  7890. pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
  7891. }
  7892. if (pf->hw.func_caps.iwarp) {
  7893. pf->flags |= I40E_FLAG_IWARP_ENABLED;
  7894. /* IWARP needs one extra vector for CQP just like MISC.*/
  7895. pf->num_iwarp_msix = (int)num_online_cpus() + 1;
  7896. }
  7897. #ifdef CONFIG_PCI_IOV
  7898. if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
  7899. pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
  7900. pf->flags |= I40E_FLAG_SRIOV_ENABLED;
  7901. pf->num_req_vfs = min_t(int,
  7902. pf->hw.func_caps.num_vfs,
  7903. I40E_MAX_VF_COUNT);
  7904. }
  7905. #endif /* CONFIG_PCI_IOV */
  7906. if (pf->hw.mac.type == I40E_MAC_X722) {
  7907. pf->flags |= I40E_FLAG_RSS_AQ_CAPABLE
  7908. | I40E_FLAG_128_QP_RSS_CAPABLE
  7909. | I40E_FLAG_HW_ATR_EVICT_CAPABLE
  7910. | I40E_FLAG_OUTER_UDP_CSUM_CAPABLE
  7911. | I40E_FLAG_WB_ON_ITR_CAPABLE
  7912. | I40E_FLAG_MULTIPLE_TCP_UDP_RSS_PCTYPE
  7913. | I40E_FLAG_NO_PCI_LINK_CHECK
  7914. | I40E_FLAG_USE_SET_LLDP_MIB
  7915. | I40E_FLAG_GENEVE_OFFLOAD_CAPABLE
  7916. | I40E_FLAG_PTP_L4_CAPABLE
  7917. | I40E_FLAG_WOL_MC_MAGIC_PKT_WAKE;
  7918. } else if ((pf->hw.aq.api_maj_ver > 1) ||
  7919. ((pf->hw.aq.api_maj_ver == 1) &&
  7920. (pf->hw.aq.api_min_ver > 4))) {
  7921. /* Supported in FW API version higher than 1.4 */
  7922. pf->flags |= I40E_FLAG_GENEVE_OFFLOAD_CAPABLE;
  7923. }
  7924. /* Enable HW ATR eviction if possible */
  7925. if (pf->flags & I40E_FLAG_HW_ATR_EVICT_CAPABLE)
  7926. pf->flags |= I40E_FLAG_HW_ATR_EVICT_ENABLED;
  7927. pf->eeprom_version = 0xDEAD;
  7928. pf->lan_veb = I40E_NO_VEB;
  7929. pf->lan_vsi = I40E_NO_VSI;
  7930. /* By default FW has this off for performance reasons */
  7931. pf->flags &= ~I40E_FLAG_VEB_STATS_ENABLED;
  7932. /* set up queue assignment tracking */
  7933. size = sizeof(struct i40e_lump_tracking)
  7934. + (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
  7935. pf->qp_pile = kzalloc(size, GFP_KERNEL);
  7936. if (!pf->qp_pile) {
  7937. err = -ENOMEM;
  7938. goto sw_init_done;
  7939. }
  7940. pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
  7941. pf->qp_pile->search_hint = 0;
  7942. pf->tx_timeout_recovery_level = 1;
  7943. mutex_init(&pf->switch_mutex);
  7944. sw_init_done:
  7945. return err;
  7946. }
  7947. /**
  7948. * i40e_set_ntuple - set the ntuple feature flag and take action
  7949. * @pf: board private structure to initialize
  7950. * @features: the feature set that the stack is suggesting
  7951. *
  7952. * returns a bool to indicate if reset needs to happen
  7953. **/
  7954. bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
  7955. {
  7956. bool need_reset = false;
  7957. /* Check if Flow Director n-tuple support was enabled or disabled. If
  7958. * the state changed, we need to reset.
  7959. */
  7960. if (features & NETIF_F_NTUPLE) {
  7961. /* Enable filters and mark for reset */
  7962. if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
  7963. need_reset = true;
  7964. /* enable FD_SB only if there is MSI-X vector */
  7965. if (pf->num_fdsb_msix > 0)
  7966. pf->flags |= I40E_FLAG_FD_SB_ENABLED;
  7967. } else {
  7968. /* turn off filters, mark for reset and clear SW filter list */
  7969. if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
  7970. need_reset = true;
  7971. i40e_fdir_filter_exit(pf);
  7972. }
  7973. pf->flags &= ~(I40E_FLAG_FD_SB_ENABLED |
  7974. I40E_FLAG_FD_SB_AUTO_DISABLED);
  7975. /* reset fd counters */
  7976. pf->fd_add_err = 0;
  7977. pf->fd_atr_cnt = 0;
  7978. /* if ATR was auto disabled it can be re-enabled. */
  7979. if (pf->flags & I40E_FLAG_FD_ATR_AUTO_DISABLED) {
  7980. pf->flags &= ~I40E_FLAG_FD_ATR_AUTO_DISABLED;
  7981. if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
  7982. (I40E_DEBUG_FD & pf->hw.debug_mask))
  7983. dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
  7984. }
  7985. }
  7986. return need_reset;
  7987. }
  7988. /**
  7989. * i40e_clear_rss_lut - clear the rx hash lookup table
  7990. * @vsi: the VSI being configured
  7991. **/
  7992. static void i40e_clear_rss_lut(struct i40e_vsi *vsi)
  7993. {
  7994. struct i40e_pf *pf = vsi->back;
  7995. struct i40e_hw *hw = &pf->hw;
  7996. u16 vf_id = vsi->vf_id;
  7997. u8 i;
  7998. if (vsi->type == I40E_VSI_MAIN) {
  7999. for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
  8000. wr32(hw, I40E_PFQF_HLUT(i), 0);
  8001. } else if (vsi->type == I40E_VSI_SRIOV) {
  8002. for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
  8003. i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0);
  8004. } else {
  8005. dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
  8006. }
  8007. }
  8008. /**
  8009. * i40e_set_features - set the netdev feature flags
  8010. * @netdev: ptr to the netdev being adjusted
  8011. * @features: the feature set that the stack is suggesting
  8012. * Note: expects to be called while under rtnl_lock()
  8013. **/
  8014. static int i40e_set_features(struct net_device *netdev,
  8015. netdev_features_t features)
  8016. {
  8017. struct i40e_netdev_priv *np = netdev_priv(netdev);
  8018. struct i40e_vsi *vsi = np->vsi;
  8019. struct i40e_pf *pf = vsi->back;
  8020. bool need_reset;
  8021. if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
  8022. i40e_pf_config_rss(pf);
  8023. else if (!(features & NETIF_F_RXHASH) &&
  8024. netdev->features & NETIF_F_RXHASH)
  8025. i40e_clear_rss_lut(vsi);
  8026. if (features & NETIF_F_HW_VLAN_CTAG_RX)
  8027. i40e_vlan_stripping_enable(vsi);
  8028. else
  8029. i40e_vlan_stripping_disable(vsi);
  8030. need_reset = i40e_set_ntuple(pf, features);
  8031. if (need_reset)
  8032. i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED), true);
  8033. return 0;
  8034. }
  8035. /**
  8036. * i40e_get_udp_port_idx - Lookup a possibly offloaded for Rx UDP port
  8037. * @pf: board private structure
  8038. * @port: The UDP port to look up
  8039. *
  8040. * Returns the index number or I40E_MAX_PF_UDP_OFFLOAD_PORTS if port not found
  8041. **/
  8042. static u8 i40e_get_udp_port_idx(struct i40e_pf *pf, u16 port)
  8043. {
  8044. u8 i;
  8045. for (i = 0; i < I40E_MAX_PF_UDP_OFFLOAD_PORTS; i++) {
  8046. if (pf->udp_ports[i].port == port)
  8047. return i;
  8048. }
  8049. return i;
  8050. }
  8051. /**
  8052. * i40e_udp_tunnel_add - Get notifications about UDP tunnel ports that come up
  8053. * @netdev: This physical port's netdev
  8054. * @ti: Tunnel endpoint information
  8055. **/
  8056. static void i40e_udp_tunnel_add(struct net_device *netdev,
  8057. struct udp_tunnel_info *ti)
  8058. {
  8059. struct i40e_netdev_priv *np = netdev_priv(netdev);
  8060. struct i40e_vsi *vsi = np->vsi;
  8061. struct i40e_pf *pf = vsi->back;
  8062. u16 port = ntohs(ti->port);
  8063. u8 next_idx;
  8064. u8 idx;
  8065. idx = i40e_get_udp_port_idx(pf, port);
  8066. /* Check if port already exists */
  8067. if (idx < I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
  8068. netdev_info(netdev, "port %d already offloaded\n", port);
  8069. return;
  8070. }
  8071. /* Now check if there is space to add the new port */
  8072. next_idx = i40e_get_udp_port_idx(pf, 0);
  8073. if (next_idx == I40E_MAX_PF_UDP_OFFLOAD_PORTS) {
  8074. netdev_info(netdev, "maximum number of offloaded UDP ports reached, not adding port %d\n",
  8075. port);
  8076. return;
  8077. }
  8078. switch (ti->type) {
  8079. case UDP_TUNNEL_TYPE_VXLAN:
  8080. pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_VXLAN;
  8081. break;
  8082. case UDP_TUNNEL_TYPE_GENEVE:
  8083. if (!(pf->flags & I40E_FLAG_GENEVE_OFFLOAD_CAPABLE))
  8084. return;
  8085. pf->udp_ports[next_idx].type = I40E_AQC_TUNNEL_TYPE_NGE;
  8086. break;
  8087. default:
  8088. return;
  8089. }
  8090. /* New port: add it and mark its index in the bitmap */
  8091. pf->udp_ports[next_idx].port = port;
  8092. pf->pending_udp_bitmap |= BIT_ULL(next_idx);
  8093. pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
  8094. }
  8095. /**
  8096. * i40e_udp_tunnel_del - Get notifications about UDP tunnel ports that go away
  8097. * @netdev: This physical port's netdev
  8098. * @ti: Tunnel endpoint information
  8099. **/
  8100. static void i40e_udp_tunnel_del(struct net_device *netdev,
  8101. struct udp_tunnel_info *ti)
  8102. {
  8103. struct i40e_netdev_priv *np = netdev_priv(netdev);
  8104. struct i40e_vsi *vsi = np->vsi;
  8105. struct i40e_pf *pf = vsi->back;
  8106. u16 port = ntohs(ti->port);
  8107. u8 idx;
  8108. idx = i40e_get_udp_port_idx(pf, port);
  8109. /* Check if port already exists */
  8110. if (idx >= I40E_MAX_PF_UDP_OFFLOAD_PORTS)
  8111. goto not_found;
  8112. switch (ti->type) {
  8113. case UDP_TUNNEL_TYPE_VXLAN:
  8114. if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_VXLAN)
  8115. goto not_found;
  8116. break;
  8117. case UDP_TUNNEL_TYPE_GENEVE:
  8118. if (pf->udp_ports[idx].type != I40E_AQC_TUNNEL_TYPE_NGE)
  8119. goto not_found;
  8120. break;
  8121. default:
  8122. goto not_found;
  8123. }
  8124. /* if port exists, set it to 0 (mark for deletion)
  8125. * and make it pending
  8126. */
  8127. pf->udp_ports[idx].port = 0;
  8128. pf->pending_udp_bitmap |= BIT_ULL(idx);
  8129. pf->flags |= I40E_FLAG_UDP_FILTER_SYNC;
  8130. return;
  8131. not_found:
  8132. netdev_warn(netdev, "UDP port %d was not found, not deleting\n",
  8133. port);
  8134. }
  8135. static int i40e_get_phys_port_id(struct net_device *netdev,
  8136. struct netdev_phys_item_id *ppid)
  8137. {
  8138. struct i40e_netdev_priv *np = netdev_priv(netdev);
  8139. struct i40e_pf *pf = np->vsi->back;
  8140. struct i40e_hw *hw = &pf->hw;
  8141. if (!(pf->flags & I40E_FLAG_PORT_ID_VALID))
  8142. return -EOPNOTSUPP;
  8143. ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
  8144. memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
  8145. return 0;
  8146. }
  8147. /**
  8148. * i40e_ndo_fdb_add - add an entry to the hardware database
  8149. * @ndm: the input from the stack
  8150. * @tb: pointer to array of nladdr (unused)
  8151. * @dev: the net device pointer
  8152. * @addr: the MAC address entry being added
  8153. * @flags: instructions from stack about fdb operation
  8154. */
  8155. static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
  8156. struct net_device *dev,
  8157. const unsigned char *addr, u16 vid,
  8158. u16 flags)
  8159. {
  8160. struct i40e_netdev_priv *np = netdev_priv(dev);
  8161. struct i40e_pf *pf = np->vsi->back;
  8162. int err = 0;
  8163. if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
  8164. return -EOPNOTSUPP;
  8165. if (vid) {
  8166. pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
  8167. return -EINVAL;
  8168. }
  8169. /* Hardware does not support aging addresses so if a
  8170. * ndm_state is given only allow permanent addresses
  8171. */
  8172. if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
  8173. netdev_info(dev, "FDB only supports static addresses\n");
  8174. return -EINVAL;
  8175. }
  8176. if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
  8177. err = dev_uc_add_excl(dev, addr);
  8178. else if (is_multicast_ether_addr(addr))
  8179. err = dev_mc_add_excl(dev, addr);
  8180. else
  8181. err = -EINVAL;
  8182. /* Only return duplicate errors if NLM_F_EXCL is set */
  8183. if (err == -EEXIST && !(flags & NLM_F_EXCL))
  8184. err = 0;
  8185. return err;
  8186. }
  8187. /**
  8188. * i40e_ndo_bridge_setlink - Set the hardware bridge mode
  8189. * @dev: the netdev being configured
  8190. * @nlh: RTNL message
  8191. *
  8192. * Inserts a new hardware bridge if not already created and
  8193. * enables the bridging mode requested (VEB or VEPA). If the
  8194. * hardware bridge has already been inserted and the request
  8195. * is to change the mode then that requires a PF reset to
  8196. * allow rebuild of the components with required hardware
  8197. * bridge mode enabled.
  8198. *
  8199. * Note: expects to be called while under rtnl_lock()
  8200. **/
  8201. static int i40e_ndo_bridge_setlink(struct net_device *dev,
  8202. struct nlmsghdr *nlh,
  8203. u16 flags)
  8204. {
  8205. struct i40e_netdev_priv *np = netdev_priv(dev);
  8206. struct i40e_vsi *vsi = np->vsi;
  8207. struct i40e_pf *pf = vsi->back;
  8208. struct i40e_veb *veb = NULL;
  8209. struct nlattr *attr, *br_spec;
  8210. int i, rem;
  8211. /* Only for PF VSI for now */
  8212. if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
  8213. return -EOPNOTSUPP;
  8214. /* Find the HW bridge for PF VSI */
  8215. for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
  8216. if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
  8217. veb = pf->veb[i];
  8218. }
  8219. br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
  8220. nla_for_each_nested(attr, br_spec, rem) {
  8221. __u16 mode;
  8222. if (nla_type(attr) != IFLA_BRIDGE_MODE)
  8223. continue;
  8224. mode = nla_get_u16(attr);
  8225. if ((mode != BRIDGE_MODE_VEPA) &&
  8226. (mode != BRIDGE_MODE_VEB))
  8227. return -EINVAL;
  8228. /* Insert a new HW bridge */
  8229. if (!veb) {
  8230. veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
  8231. vsi->tc_config.enabled_tc);
  8232. if (veb) {
  8233. veb->bridge_mode = mode;
  8234. i40e_config_bridge_mode(veb);
  8235. } else {
  8236. /* No Bridge HW offload available */
  8237. return -ENOENT;
  8238. }
  8239. break;
  8240. } else if (mode != veb->bridge_mode) {
  8241. /* Existing HW bridge but different mode needs reset */
  8242. veb->bridge_mode = mode;
  8243. /* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
  8244. if (mode == BRIDGE_MODE_VEB)
  8245. pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
  8246. else
  8247. pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
  8248. i40e_do_reset(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED),
  8249. true);
  8250. break;
  8251. }
  8252. }
  8253. return 0;
  8254. }
  8255. /**
  8256. * i40e_ndo_bridge_getlink - Get the hardware bridge mode
  8257. * @skb: skb buff
  8258. * @pid: process id
  8259. * @seq: RTNL message seq #
  8260. * @dev: the netdev being configured
  8261. * @filter_mask: unused
  8262. * @nlflags: netlink flags passed in
  8263. *
  8264. * Return the mode in which the hardware bridge is operating in
  8265. * i.e VEB or VEPA.
  8266. **/
  8267. static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
  8268. struct net_device *dev,
  8269. u32 __always_unused filter_mask,
  8270. int nlflags)
  8271. {
  8272. struct i40e_netdev_priv *np = netdev_priv(dev);
  8273. struct i40e_vsi *vsi = np->vsi;
  8274. struct i40e_pf *pf = vsi->back;
  8275. struct i40e_veb *veb = NULL;
  8276. int i;
  8277. /* Only for PF VSI for now */
  8278. if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
  8279. return -EOPNOTSUPP;
  8280. /* Find the HW bridge for the PF VSI */
  8281. for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
  8282. if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
  8283. veb = pf->veb[i];
  8284. }
  8285. if (!veb)
  8286. return 0;
  8287. return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
  8288. 0, 0, nlflags, filter_mask, NULL);
  8289. }
  8290. /**
  8291. * i40e_features_check - Validate encapsulated packet conforms to limits
  8292. * @skb: skb buff
  8293. * @dev: This physical port's netdev
  8294. * @features: Offload features that the stack believes apply
  8295. **/
  8296. static netdev_features_t i40e_features_check(struct sk_buff *skb,
  8297. struct net_device *dev,
  8298. netdev_features_t features)
  8299. {
  8300. size_t len;
  8301. /* No point in doing any of this if neither checksum nor GSO are
  8302. * being requested for this frame. We can rule out both by just
  8303. * checking for CHECKSUM_PARTIAL
  8304. */
  8305. if (skb->ip_summed != CHECKSUM_PARTIAL)
  8306. return features;
  8307. /* We cannot support GSO if the MSS is going to be less than
  8308. * 64 bytes. If it is then we need to drop support for GSO.
  8309. */
  8310. if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
  8311. features &= ~NETIF_F_GSO_MASK;
  8312. /* MACLEN can support at most 63 words */
  8313. len = skb_network_header(skb) - skb->data;
  8314. if (len & ~(63 * 2))
  8315. goto out_err;
  8316. /* IPLEN and EIPLEN can support at most 127 dwords */
  8317. len = skb_transport_header(skb) - skb_network_header(skb);
  8318. if (len & ~(127 * 4))
  8319. goto out_err;
  8320. if (skb->encapsulation) {
  8321. /* L4TUNLEN can support 127 words */
  8322. len = skb_inner_network_header(skb) - skb_transport_header(skb);
  8323. if (len & ~(127 * 2))
  8324. goto out_err;
  8325. /* IPLEN can support at most 127 dwords */
  8326. len = skb_inner_transport_header(skb) -
  8327. skb_inner_network_header(skb);
  8328. if (len & ~(127 * 4))
  8329. goto out_err;
  8330. }
  8331. /* No need to validate L4LEN as TCP is the only protocol with a
  8332. * a flexible value and we support all possible values supported
  8333. * by TCP, which is at most 15 dwords
  8334. */
  8335. return features;
  8336. out_err:
  8337. return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
  8338. }
  8339. /**
  8340. * i40e_xdp_setup - add/remove an XDP program
  8341. * @vsi: VSI to changed
  8342. * @prog: XDP program
  8343. **/
  8344. static int i40e_xdp_setup(struct i40e_vsi *vsi,
  8345. struct bpf_prog *prog)
  8346. {
  8347. int frame_size = vsi->netdev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
  8348. struct i40e_pf *pf = vsi->back;
  8349. struct bpf_prog *old_prog;
  8350. bool need_reset;
  8351. int i;
  8352. /* Don't allow frames that span over multiple buffers */
  8353. if (frame_size > vsi->rx_buf_len)
  8354. return -EINVAL;
  8355. if (!i40e_enabled_xdp_vsi(vsi) && !prog)
  8356. return 0;
  8357. /* When turning XDP on->off/off->on we reset and rebuild the rings. */
  8358. need_reset = (i40e_enabled_xdp_vsi(vsi) != !!prog);
  8359. if (need_reset)
  8360. i40e_prep_for_reset(pf, true);
  8361. old_prog = xchg(&vsi->xdp_prog, prog);
  8362. if (need_reset)
  8363. i40e_reset_and_rebuild(pf, true, true);
  8364. for (i = 0; i < vsi->num_queue_pairs; i++)
  8365. WRITE_ONCE(vsi->rx_rings[i]->xdp_prog, vsi->xdp_prog);
  8366. if (old_prog)
  8367. bpf_prog_put(old_prog);
  8368. return 0;
  8369. }
  8370. /**
  8371. * i40e_xdp - implements ndo_xdp for i40e
  8372. * @dev: netdevice
  8373. * @xdp: XDP command
  8374. **/
  8375. static int i40e_xdp(struct net_device *dev,
  8376. struct netdev_xdp *xdp)
  8377. {
  8378. struct i40e_netdev_priv *np = netdev_priv(dev);
  8379. struct i40e_vsi *vsi = np->vsi;
  8380. if (vsi->type != I40E_VSI_MAIN)
  8381. return -EINVAL;
  8382. switch (xdp->command) {
  8383. case XDP_SETUP_PROG:
  8384. return i40e_xdp_setup(vsi, xdp->prog);
  8385. case XDP_QUERY_PROG:
  8386. xdp->prog_attached = i40e_enabled_xdp_vsi(vsi);
  8387. xdp->prog_id = vsi->xdp_prog ? vsi->xdp_prog->aux->id : 0;
  8388. return 0;
  8389. default:
  8390. return -EINVAL;
  8391. }
  8392. }
  8393. static const struct net_device_ops i40e_netdev_ops = {
  8394. .ndo_open = i40e_open,
  8395. .ndo_stop = i40e_close,
  8396. .ndo_start_xmit = i40e_lan_xmit_frame,
  8397. .ndo_get_stats64 = i40e_get_netdev_stats_struct,
  8398. .ndo_set_rx_mode = i40e_set_rx_mode,
  8399. .ndo_validate_addr = eth_validate_addr,
  8400. .ndo_set_mac_address = i40e_set_mac,
  8401. .ndo_change_mtu = i40e_change_mtu,
  8402. .ndo_do_ioctl = i40e_ioctl,
  8403. .ndo_tx_timeout = i40e_tx_timeout,
  8404. .ndo_vlan_rx_add_vid = i40e_vlan_rx_add_vid,
  8405. .ndo_vlan_rx_kill_vid = i40e_vlan_rx_kill_vid,
  8406. #ifdef CONFIG_NET_POLL_CONTROLLER
  8407. .ndo_poll_controller = i40e_netpoll,
  8408. #endif
  8409. .ndo_setup_tc = __i40e_setup_tc,
  8410. .ndo_set_features = i40e_set_features,
  8411. .ndo_set_vf_mac = i40e_ndo_set_vf_mac,
  8412. .ndo_set_vf_vlan = i40e_ndo_set_vf_port_vlan,
  8413. .ndo_set_vf_rate = i40e_ndo_set_vf_bw,
  8414. .ndo_get_vf_config = i40e_ndo_get_vf_config,
  8415. .ndo_set_vf_link_state = i40e_ndo_set_vf_link_state,
  8416. .ndo_set_vf_spoofchk = i40e_ndo_set_vf_spoofchk,
  8417. .ndo_set_vf_trust = i40e_ndo_set_vf_trust,
  8418. .ndo_udp_tunnel_add = i40e_udp_tunnel_add,
  8419. .ndo_udp_tunnel_del = i40e_udp_tunnel_del,
  8420. .ndo_get_phys_port_id = i40e_get_phys_port_id,
  8421. .ndo_fdb_add = i40e_ndo_fdb_add,
  8422. .ndo_features_check = i40e_features_check,
  8423. .ndo_bridge_getlink = i40e_ndo_bridge_getlink,
  8424. .ndo_bridge_setlink = i40e_ndo_bridge_setlink,
  8425. .ndo_xdp = i40e_xdp,
  8426. };
  8427. /**
  8428. * i40e_config_netdev - Setup the netdev flags
  8429. * @vsi: the VSI being configured
  8430. *
  8431. * Returns 0 on success, negative value on failure
  8432. **/
  8433. static int i40e_config_netdev(struct i40e_vsi *vsi)
  8434. {
  8435. struct i40e_pf *pf = vsi->back;
  8436. struct i40e_hw *hw = &pf->hw;
  8437. struct i40e_netdev_priv *np;
  8438. struct net_device *netdev;
  8439. u8 broadcast[ETH_ALEN];
  8440. u8 mac_addr[ETH_ALEN];
  8441. int etherdev_size;
  8442. netdev_features_t hw_enc_features;
  8443. netdev_features_t hw_features;
  8444. etherdev_size = sizeof(struct i40e_netdev_priv);
  8445. netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
  8446. if (!netdev)
  8447. return -ENOMEM;
  8448. vsi->netdev = netdev;
  8449. np = netdev_priv(netdev);
  8450. np->vsi = vsi;
  8451. hw_enc_features = NETIF_F_SG |
  8452. NETIF_F_IP_CSUM |
  8453. NETIF_F_IPV6_CSUM |
  8454. NETIF_F_HIGHDMA |
  8455. NETIF_F_SOFT_FEATURES |
  8456. NETIF_F_TSO |
  8457. NETIF_F_TSO_ECN |
  8458. NETIF_F_TSO6 |
  8459. NETIF_F_GSO_GRE |
  8460. NETIF_F_GSO_GRE_CSUM |
  8461. NETIF_F_GSO_PARTIAL |
  8462. NETIF_F_GSO_UDP_TUNNEL |
  8463. NETIF_F_GSO_UDP_TUNNEL_CSUM |
  8464. NETIF_F_SCTP_CRC |
  8465. NETIF_F_RXHASH |
  8466. NETIF_F_RXCSUM |
  8467. 0;
  8468. if (!(pf->flags & I40E_FLAG_OUTER_UDP_CSUM_CAPABLE))
  8469. netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
  8470. netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
  8471. netdev->hw_enc_features |= hw_enc_features;
  8472. /* record features VLANs can make use of */
  8473. netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
  8474. if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
  8475. netdev->hw_features |= NETIF_F_NTUPLE;
  8476. hw_features = hw_enc_features |
  8477. NETIF_F_HW_VLAN_CTAG_TX |
  8478. NETIF_F_HW_VLAN_CTAG_RX;
  8479. netdev->hw_features |= hw_features;
  8480. netdev->features |= hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
  8481. netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
  8482. if (vsi->type == I40E_VSI_MAIN) {
  8483. SET_NETDEV_DEV(netdev, &pf->pdev->dev);
  8484. ether_addr_copy(mac_addr, hw->mac.perm_addr);
  8485. /* The following steps are necessary for two reasons. First,
  8486. * some older NVM configurations load a default MAC-VLAN
  8487. * filter that will accept any tagged packet, and we want to
  8488. * replace this with a normal filter. Additionally, it is
  8489. * possible our MAC address was provided by the platform using
  8490. * Open Firmware or similar.
  8491. *
  8492. * Thus, we need to remove the default filter and install one
  8493. * specific to the MAC address.
  8494. */
  8495. i40e_rm_default_mac_filter(vsi, mac_addr);
  8496. spin_lock_bh(&vsi->mac_filter_hash_lock);
  8497. i40e_add_mac_filter(vsi, mac_addr);
  8498. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  8499. } else {
  8500. /* relate the VSI_VMDQ name to the VSI_MAIN name */
  8501. snprintf(netdev->name, IFNAMSIZ, "%sv%%d",
  8502. pf->vsi[pf->lan_vsi]->netdev->name);
  8503. random_ether_addr(mac_addr);
  8504. spin_lock_bh(&vsi->mac_filter_hash_lock);
  8505. i40e_add_mac_filter(vsi, mac_addr);
  8506. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  8507. }
  8508. /* Add the broadcast filter so that we initially will receive
  8509. * broadcast packets. Note that when a new VLAN is first added the
  8510. * driver will convert all filters marked I40E_VLAN_ANY into VLAN
  8511. * specific filters as part of transitioning into "vlan" operation.
  8512. * When more VLANs are added, the driver will copy each existing MAC
  8513. * filter and add it for the new VLAN.
  8514. *
  8515. * Broadcast filters are handled specially by
  8516. * i40e_sync_filters_subtask, as the driver must to set the broadcast
  8517. * promiscuous bit instead of adding this directly as a MAC/VLAN
  8518. * filter. The subtask will update the correct broadcast promiscuous
  8519. * bits as VLANs become active or inactive.
  8520. */
  8521. eth_broadcast_addr(broadcast);
  8522. spin_lock_bh(&vsi->mac_filter_hash_lock);
  8523. i40e_add_mac_filter(vsi, broadcast);
  8524. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  8525. ether_addr_copy(netdev->dev_addr, mac_addr);
  8526. ether_addr_copy(netdev->perm_addr, mac_addr);
  8527. netdev->priv_flags |= IFF_UNICAST_FLT;
  8528. netdev->priv_flags |= IFF_SUPP_NOFCS;
  8529. /* Setup netdev TC information */
  8530. i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
  8531. netdev->netdev_ops = &i40e_netdev_ops;
  8532. netdev->watchdog_timeo = 5 * HZ;
  8533. i40e_set_ethtool_ops(netdev);
  8534. /* MTU range: 68 - 9706 */
  8535. netdev->min_mtu = ETH_MIN_MTU;
  8536. netdev->max_mtu = I40E_MAX_RXBUFFER -
  8537. (ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN);
  8538. return 0;
  8539. }
  8540. /**
  8541. * i40e_vsi_delete - Delete a VSI from the switch
  8542. * @vsi: the VSI being removed
  8543. *
  8544. * Returns 0 on success, negative value on failure
  8545. **/
  8546. static void i40e_vsi_delete(struct i40e_vsi *vsi)
  8547. {
  8548. /* remove default VSI is not allowed */
  8549. if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
  8550. return;
  8551. i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
  8552. }
  8553. /**
  8554. * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
  8555. * @vsi: the VSI being queried
  8556. *
  8557. * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
  8558. **/
  8559. int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
  8560. {
  8561. struct i40e_veb *veb;
  8562. struct i40e_pf *pf = vsi->back;
  8563. /* Uplink is not a bridge so default to VEB */
  8564. if (vsi->veb_idx == I40E_NO_VEB)
  8565. return 1;
  8566. veb = pf->veb[vsi->veb_idx];
  8567. if (!veb) {
  8568. dev_info(&pf->pdev->dev,
  8569. "There is no veb associated with the bridge\n");
  8570. return -ENOENT;
  8571. }
  8572. /* Uplink is a bridge in VEPA mode */
  8573. if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
  8574. return 0;
  8575. } else {
  8576. /* Uplink is a bridge in VEB mode */
  8577. return 1;
  8578. }
  8579. /* VEPA is now default bridge, so return 0 */
  8580. return 0;
  8581. }
  8582. /**
  8583. * i40e_add_vsi - Add a VSI to the switch
  8584. * @vsi: the VSI being configured
  8585. *
  8586. * This initializes a VSI context depending on the VSI type to be added and
  8587. * passes it down to the add_vsi aq command.
  8588. **/
  8589. static int i40e_add_vsi(struct i40e_vsi *vsi)
  8590. {
  8591. int ret = -ENODEV;
  8592. struct i40e_pf *pf = vsi->back;
  8593. struct i40e_hw *hw = &pf->hw;
  8594. struct i40e_vsi_context ctxt;
  8595. struct i40e_mac_filter *f;
  8596. struct hlist_node *h;
  8597. int bkt;
  8598. u8 enabled_tc = 0x1; /* TC0 enabled */
  8599. int f_count = 0;
  8600. memset(&ctxt, 0, sizeof(ctxt));
  8601. switch (vsi->type) {
  8602. case I40E_VSI_MAIN:
  8603. /* The PF's main VSI is already setup as part of the
  8604. * device initialization, so we'll not bother with
  8605. * the add_vsi call, but we will retrieve the current
  8606. * VSI context.
  8607. */
  8608. ctxt.seid = pf->main_vsi_seid;
  8609. ctxt.pf_num = pf->hw.pf_id;
  8610. ctxt.vf_num = 0;
  8611. ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
  8612. ctxt.flags = I40E_AQ_VSI_TYPE_PF;
  8613. if (ret) {
  8614. dev_info(&pf->pdev->dev,
  8615. "couldn't get PF vsi config, err %s aq_err %s\n",
  8616. i40e_stat_str(&pf->hw, ret),
  8617. i40e_aq_str(&pf->hw,
  8618. pf->hw.aq.asq_last_status));
  8619. return -ENOENT;
  8620. }
  8621. vsi->info = ctxt.info;
  8622. vsi->info.valid_sections = 0;
  8623. vsi->seid = ctxt.seid;
  8624. vsi->id = ctxt.vsi_number;
  8625. enabled_tc = i40e_pf_get_tc_map(pf);
  8626. /* MFP mode setup queue map and update VSI */
  8627. if ((pf->flags & I40E_FLAG_MFP_ENABLED) &&
  8628. !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
  8629. memset(&ctxt, 0, sizeof(ctxt));
  8630. ctxt.seid = pf->main_vsi_seid;
  8631. ctxt.pf_num = pf->hw.pf_id;
  8632. ctxt.vf_num = 0;
  8633. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
  8634. ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
  8635. if (ret) {
  8636. dev_info(&pf->pdev->dev,
  8637. "update vsi failed, err %s aq_err %s\n",
  8638. i40e_stat_str(&pf->hw, ret),
  8639. i40e_aq_str(&pf->hw,
  8640. pf->hw.aq.asq_last_status));
  8641. ret = -ENOENT;
  8642. goto err;
  8643. }
  8644. /* update the local VSI info queue map */
  8645. i40e_vsi_update_queue_map(vsi, &ctxt);
  8646. vsi->info.valid_sections = 0;
  8647. } else {
  8648. /* Default/Main VSI is only enabled for TC0
  8649. * reconfigure it to enable all TCs that are
  8650. * available on the port in SFP mode.
  8651. * For MFP case the iSCSI PF would use this
  8652. * flow to enable LAN+iSCSI TC.
  8653. */
  8654. ret = i40e_vsi_config_tc(vsi, enabled_tc);
  8655. if (ret) {
  8656. dev_info(&pf->pdev->dev,
  8657. "failed to configure TCs for main VSI tc_map 0x%08x, err %s aq_err %s\n",
  8658. enabled_tc,
  8659. i40e_stat_str(&pf->hw, ret),
  8660. i40e_aq_str(&pf->hw,
  8661. pf->hw.aq.asq_last_status));
  8662. ret = -ENOENT;
  8663. }
  8664. }
  8665. break;
  8666. case I40E_VSI_FDIR:
  8667. ctxt.pf_num = hw->pf_id;
  8668. ctxt.vf_num = 0;
  8669. ctxt.uplink_seid = vsi->uplink_seid;
  8670. ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
  8671. ctxt.flags = I40E_AQ_VSI_TYPE_PF;
  8672. if ((pf->flags & I40E_FLAG_VEB_MODE_ENABLED) &&
  8673. (i40e_is_vsi_uplink_mode_veb(vsi))) {
  8674. ctxt.info.valid_sections |=
  8675. cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  8676. ctxt.info.switch_id =
  8677. cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  8678. }
  8679. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
  8680. break;
  8681. case I40E_VSI_VMDQ2:
  8682. ctxt.pf_num = hw->pf_id;
  8683. ctxt.vf_num = 0;
  8684. ctxt.uplink_seid = vsi->uplink_seid;
  8685. ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
  8686. ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
  8687. /* This VSI is connected to VEB so the switch_id
  8688. * should be set to zero by default.
  8689. */
  8690. if (i40e_is_vsi_uplink_mode_veb(vsi)) {
  8691. ctxt.info.valid_sections |=
  8692. cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  8693. ctxt.info.switch_id =
  8694. cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  8695. }
  8696. /* Setup the VSI tx/rx queue map for TC0 only for now */
  8697. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
  8698. break;
  8699. case I40E_VSI_SRIOV:
  8700. ctxt.pf_num = hw->pf_id;
  8701. ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
  8702. ctxt.uplink_seid = vsi->uplink_seid;
  8703. ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
  8704. ctxt.flags = I40E_AQ_VSI_TYPE_VF;
  8705. /* This VSI is connected to VEB so the switch_id
  8706. * should be set to zero by default.
  8707. */
  8708. if (i40e_is_vsi_uplink_mode_veb(vsi)) {
  8709. ctxt.info.valid_sections |=
  8710. cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
  8711. ctxt.info.switch_id =
  8712. cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
  8713. }
  8714. if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
  8715. ctxt.info.valid_sections |=
  8716. cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
  8717. ctxt.info.queueing_opt_flags |=
  8718. (I40E_AQ_VSI_QUE_OPT_TCP_ENA |
  8719. I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI);
  8720. }
  8721. ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
  8722. ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
  8723. if (pf->vf[vsi->vf_id].spoofchk) {
  8724. ctxt.info.valid_sections |=
  8725. cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
  8726. ctxt.info.sec_flags |=
  8727. (I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
  8728. I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
  8729. }
  8730. /* Setup the VSI tx/rx queue map for TC0 only for now */
  8731. i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
  8732. break;
  8733. case I40E_VSI_IWARP:
  8734. /* send down message to iWARP */
  8735. break;
  8736. default:
  8737. return -ENODEV;
  8738. }
  8739. if (vsi->type != I40E_VSI_MAIN) {
  8740. ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
  8741. if (ret) {
  8742. dev_info(&vsi->back->pdev->dev,
  8743. "add vsi failed, err %s aq_err %s\n",
  8744. i40e_stat_str(&pf->hw, ret),
  8745. i40e_aq_str(&pf->hw,
  8746. pf->hw.aq.asq_last_status));
  8747. ret = -ENOENT;
  8748. goto err;
  8749. }
  8750. vsi->info = ctxt.info;
  8751. vsi->info.valid_sections = 0;
  8752. vsi->seid = ctxt.seid;
  8753. vsi->id = ctxt.vsi_number;
  8754. }
  8755. vsi->active_filters = 0;
  8756. clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
  8757. spin_lock_bh(&vsi->mac_filter_hash_lock);
  8758. /* If macvlan filters already exist, force them to get loaded */
  8759. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
  8760. f->state = I40E_FILTER_NEW;
  8761. f_count++;
  8762. }
  8763. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  8764. if (f_count) {
  8765. vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
  8766. pf->flags |= I40E_FLAG_FILTER_SYNC;
  8767. }
  8768. /* Update VSI BW information */
  8769. ret = i40e_vsi_get_bw_info(vsi);
  8770. if (ret) {
  8771. dev_info(&pf->pdev->dev,
  8772. "couldn't get vsi bw info, err %s aq_err %s\n",
  8773. i40e_stat_str(&pf->hw, ret),
  8774. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  8775. /* VSI is already added so not tearing that up */
  8776. ret = 0;
  8777. }
  8778. err:
  8779. return ret;
  8780. }
  8781. /**
  8782. * i40e_vsi_release - Delete a VSI and free its resources
  8783. * @vsi: the VSI being removed
  8784. *
  8785. * Returns 0 on success or < 0 on error
  8786. **/
  8787. int i40e_vsi_release(struct i40e_vsi *vsi)
  8788. {
  8789. struct i40e_mac_filter *f;
  8790. struct hlist_node *h;
  8791. struct i40e_veb *veb = NULL;
  8792. struct i40e_pf *pf;
  8793. u16 uplink_seid;
  8794. int i, n, bkt;
  8795. pf = vsi->back;
  8796. /* release of a VEB-owner or last VSI is not allowed */
  8797. if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
  8798. dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
  8799. vsi->seid, vsi->uplink_seid);
  8800. return -ENODEV;
  8801. }
  8802. if (vsi == pf->vsi[pf->lan_vsi] &&
  8803. !test_bit(__I40E_DOWN, pf->state)) {
  8804. dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
  8805. return -ENODEV;
  8806. }
  8807. uplink_seid = vsi->uplink_seid;
  8808. if (vsi->type != I40E_VSI_SRIOV) {
  8809. if (vsi->netdev_registered) {
  8810. vsi->netdev_registered = false;
  8811. if (vsi->netdev) {
  8812. /* results in a call to i40e_close() */
  8813. unregister_netdev(vsi->netdev);
  8814. }
  8815. } else {
  8816. i40e_vsi_close(vsi);
  8817. }
  8818. i40e_vsi_disable_irq(vsi);
  8819. }
  8820. spin_lock_bh(&vsi->mac_filter_hash_lock);
  8821. /* clear the sync flag on all filters */
  8822. if (vsi->netdev) {
  8823. __dev_uc_unsync(vsi->netdev, NULL);
  8824. __dev_mc_unsync(vsi->netdev, NULL);
  8825. }
  8826. /* make sure any remaining filters are marked for deletion */
  8827. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
  8828. __i40e_del_filter(vsi, f);
  8829. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  8830. i40e_sync_vsi_filters(vsi);
  8831. i40e_vsi_delete(vsi);
  8832. i40e_vsi_free_q_vectors(vsi);
  8833. if (vsi->netdev) {
  8834. free_netdev(vsi->netdev);
  8835. vsi->netdev = NULL;
  8836. }
  8837. i40e_vsi_clear_rings(vsi);
  8838. i40e_vsi_clear(vsi);
  8839. /* If this was the last thing on the VEB, except for the
  8840. * controlling VSI, remove the VEB, which puts the controlling
  8841. * VSI onto the next level down in the switch.
  8842. *
  8843. * Well, okay, there's one more exception here: don't remove
  8844. * the orphan VEBs yet. We'll wait for an explicit remove request
  8845. * from up the network stack.
  8846. */
  8847. for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
  8848. if (pf->vsi[i] &&
  8849. pf->vsi[i]->uplink_seid == uplink_seid &&
  8850. (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
  8851. n++; /* count the VSIs */
  8852. }
  8853. }
  8854. for (i = 0; i < I40E_MAX_VEB; i++) {
  8855. if (!pf->veb[i])
  8856. continue;
  8857. if (pf->veb[i]->uplink_seid == uplink_seid)
  8858. n++; /* count the VEBs */
  8859. if (pf->veb[i]->seid == uplink_seid)
  8860. veb = pf->veb[i];
  8861. }
  8862. if (n == 0 && veb && veb->uplink_seid != 0)
  8863. i40e_veb_release(veb);
  8864. return 0;
  8865. }
  8866. /**
  8867. * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
  8868. * @vsi: ptr to the VSI
  8869. *
  8870. * This should only be called after i40e_vsi_mem_alloc() which allocates the
  8871. * corresponding SW VSI structure and initializes num_queue_pairs for the
  8872. * newly allocated VSI.
  8873. *
  8874. * Returns 0 on success or negative on failure
  8875. **/
  8876. static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
  8877. {
  8878. int ret = -ENOENT;
  8879. struct i40e_pf *pf = vsi->back;
  8880. if (vsi->q_vectors[0]) {
  8881. dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
  8882. vsi->seid);
  8883. return -EEXIST;
  8884. }
  8885. if (vsi->base_vector) {
  8886. dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
  8887. vsi->seid, vsi->base_vector);
  8888. return -EEXIST;
  8889. }
  8890. ret = i40e_vsi_alloc_q_vectors(vsi);
  8891. if (ret) {
  8892. dev_info(&pf->pdev->dev,
  8893. "failed to allocate %d q_vector for VSI %d, ret=%d\n",
  8894. vsi->num_q_vectors, vsi->seid, ret);
  8895. vsi->num_q_vectors = 0;
  8896. goto vector_setup_out;
  8897. }
  8898. /* In Legacy mode, we do not have to get any other vector since we
  8899. * piggyback on the misc/ICR0 for queue interrupts.
  8900. */
  8901. if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
  8902. return ret;
  8903. if (vsi->num_q_vectors)
  8904. vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
  8905. vsi->num_q_vectors, vsi->idx);
  8906. if (vsi->base_vector < 0) {
  8907. dev_info(&pf->pdev->dev,
  8908. "failed to get tracking for %d vectors for VSI %d, err=%d\n",
  8909. vsi->num_q_vectors, vsi->seid, vsi->base_vector);
  8910. i40e_vsi_free_q_vectors(vsi);
  8911. ret = -ENOENT;
  8912. goto vector_setup_out;
  8913. }
  8914. vector_setup_out:
  8915. return ret;
  8916. }
  8917. /**
  8918. * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
  8919. * @vsi: pointer to the vsi.
  8920. *
  8921. * This re-allocates a vsi's queue resources.
  8922. *
  8923. * Returns pointer to the successfully allocated and configured VSI sw struct
  8924. * on success, otherwise returns NULL on failure.
  8925. **/
  8926. static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
  8927. {
  8928. u16 alloc_queue_pairs;
  8929. struct i40e_pf *pf;
  8930. u8 enabled_tc;
  8931. int ret;
  8932. if (!vsi)
  8933. return NULL;
  8934. pf = vsi->back;
  8935. i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
  8936. i40e_vsi_clear_rings(vsi);
  8937. i40e_vsi_free_arrays(vsi, false);
  8938. i40e_set_num_rings_in_vsi(vsi);
  8939. ret = i40e_vsi_alloc_arrays(vsi, false);
  8940. if (ret)
  8941. goto err_vsi;
  8942. alloc_queue_pairs = vsi->alloc_queue_pairs *
  8943. (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
  8944. ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
  8945. if (ret < 0) {
  8946. dev_info(&pf->pdev->dev,
  8947. "failed to get tracking for %d queues for VSI %d err %d\n",
  8948. alloc_queue_pairs, vsi->seid, ret);
  8949. goto err_vsi;
  8950. }
  8951. vsi->base_queue = ret;
  8952. /* Update the FW view of the VSI. Force a reset of TC and queue
  8953. * layout configurations.
  8954. */
  8955. enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
  8956. pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
  8957. pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
  8958. i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
  8959. if (vsi->type == I40E_VSI_MAIN)
  8960. i40e_rm_default_mac_filter(vsi, pf->hw.mac.perm_addr);
  8961. /* assign it some queues */
  8962. ret = i40e_alloc_rings(vsi);
  8963. if (ret)
  8964. goto err_rings;
  8965. /* map all of the rings to the q_vectors */
  8966. i40e_vsi_map_rings_to_vectors(vsi);
  8967. return vsi;
  8968. err_rings:
  8969. i40e_vsi_free_q_vectors(vsi);
  8970. if (vsi->netdev_registered) {
  8971. vsi->netdev_registered = false;
  8972. unregister_netdev(vsi->netdev);
  8973. free_netdev(vsi->netdev);
  8974. vsi->netdev = NULL;
  8975. }
  8976. i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
  8977. err_vsi:
  8978. i40e_vsi_clear(vsi);
  8979. return NULL;
  8980. }
  8981. /**
  8982. * i40e_vsi_setup - Set up a VSI by a given type
  8983. * @pf: board private structure
  8984. * @type: VSI type
  8985. * @uplink_seid: the switch element to link to
  8986. * @param1: usage depends upon VSI type. For VF types, indicates VF id
  8987. *
  8988. * This allocates the sw VSI structure and its queue resources, then add a VSI
  8989. * to the identified VEB.
  8990. *
  8991. * Returns pointer to the successfully allocated and configure VSI sw struct on
  8992. * success, otherwise returns NULL on failure.
  8993. **/
  8994. struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
  8995. u16 uplink_seid, u32 param1)
  8996. {
  8997. struct i40e_vsi *vsi = NULL;
  8998. struct i40e_veb *veb = NULL;
  8999. u16 alloc_queue_pairs;
  9000. int ret, i;
  9001. int v_idx;
  9002. /* The requested uplink_seid must be either
  9003. * - the PF's port seid
  9004. * no VEB is needed because this is the PF
  9005. * or this is a Flow Director special case VSI
  9006. * - seid of an existing VEB
  9007. * - seid of a VSI that owns an existing VEB
  9008. * - seid of a VSI that doesn't own a VEB
  9009. * a new VEB is created and the VSI becomes the owner
  9010. * - seid of the PF VSI, which is what creates the first VEB
  9011. * this is a special case of the previous
  9012. *
  9013. * Find which uplink_seid we were given and create a new VEB if needed
  9014. */
  9015. for (i = 0; i < I40E_MAX_VEB; i++) {
  9016. if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
  9017. veb = pf->veb[i];
  9018. break;
  9019. }
  9020. }
  9021. if (!veb && uplink_seid != pf->mac_seid) {
  9022. for (i = 0; i < pf->num_alloc_vsi; i++) {
  9023. if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
  9024. vsi = pf->vsi[i];
  9025. break;
  9026. }
  9027. }
  9028. if (!vsi) {
  9029. dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
  9030. uplink_seid);
  9031. return NULL;
  9032. }
  9033. if (vsi->uplink_seid == pf->mac_seid)
  9034. veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
  9035. vsi->tc_config.enabled_tc);
  9036. else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
  9037. veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
  9038. vsi->tc_config.enabled_tc);
  9039. if (veb) {
  9040. if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
  9041. dev_info(&vsi->back->pdev->dev,
  9042. "New VSI creation error, uplink seid of LAN VSI expected.\n");
  9043. return NULL;
  9044. }
  9045. /* We come up by default in VEPA mode if SRIOV is not
  9046. * already enabled, in which case we can't force VEPA
  9047. * mode.
  9048. */
  9049. if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
  9050. veb->bridge_mode = BRIDGE_MODE_VEPA;
  9051. pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
  9052. }
  9053. i40e_config_bridge_mode(veb);
  9054. }
  9055. for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
  9056. if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
  9057. veb = pf->veb[i];
  9058. }
  9059. if (!veb) {
  9060. dev_info(&pf->pdev->dev, "couldn't add VEB\n");
  9061. return NULL;
  9062. }
  9063. vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
  9064. uplink_seid = veb->seid;
  9065. }
  9066. /* get vsi sw struct */
  9067. v_idx = i40e_vsi_mem_alloc(pf, type);
  9068. if (v_idx < 0)
  9069. goto err_alloc;
  9070. vsi = pf->vsi[v_idx];
  9071. if (!vsi)
  9072. goto err_alloc;
  9073. vsi->type = type;
  9074. vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
  9075. if (type == I40E_VSI_MAIN)
  9076. pf->lan_vsi = v_idx;
  9077. else if (type == I40E_VSI_SRIOV)
  9078. vsi->vf_id = param1;
  9079. /* assign it some queues */
  9080. alloc_queue_pairs = vsi->alloc_queue_pairs *
  9081. (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
  9082. ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
  9083. if (ret < 0) {
  9084. dev_info(&pf->pdev->dev,
  9085. "failed to get tracking for %d queues for VSI %d err=%d\n",
  9086. alloc_queue_pairs, vsi->seid, ret);
  9087. goto err_vsi;
  9088. }
  9089. vsi->base_queue = ret;
  9090. /* get a VSI from the hardware */
  9091. vsi->uplink_seid = uplink_seid;
  9092. ret = i40e_add_vsi(vsi);
  9093. if (ret)
  9094. goto err_vsi;
  9095. switch (vsi->type) {
  9096. /* setup the netdev if needed */
  9097. case I40E_VSI_MAIN:
  9098. /* Apply relevant filters if a platform-specific mac
  9099. * address was selected.
  9100. */
  9101. if (!!(pf->flags & I40E_FLAG_PF_MAC)) {
  9102. ret = i40e_macaddr_init(vsi, pf->hw.mac.addr);
  9103. if (ret) {
  9104. dev_warn(&pf->pdev->dev,
  9105. "could not set up macaddr; err %d\n",
  9106. ret);
  9107. }
  9108. }
  9109. case I40E_VSI_VMDQ2:
  9110. ret = i40e_config_netdev(vsi);
  9111. if (ret)
  9112. goto err_netdev;
  9113. ret = register_netdev(vsi->netdev);
  9114. if (ret)
  9115. goto err_netdev;
  9116. vsi->netdev_registered = true;
  9117. netif_carrier_off(vsi->netdev);
  9118. #ifdef CONFIG_I40E_DCB
  9119. /* Setup DCB netlink interface */
  9120. i40e_dcbnl_setup(vsi);
  9121. #endif /* CONFIG_I40E_DCB */
  9122. /* fall through */
  9123. case I40E_VSI_FDIR:
  9124. /* set up vectors and rings if needed */
  9125. ret = i40e_vsi_setup_vectors(vsi);
  9126. if (ret)
  9127. goto err_msix;
  9128. ret = i40e_alloc_rings(vsi);
  9129. if (ret)
  9130. goto err_rings;
  9131. /* map all of the rings to the q_vectors */
  9132. i40e_vsi_map_rings_to_vectors(vsi);
  9133. i40e_vsi_reset_stats(vsi);
  9134. break;
  9135. default:
  9136. /* no netdev or rings for the other VSI types */
  9137. break;
  9138. }
  9139. if ((pf->flags & I40E_FLAG_RSS_AQ_CAPABLE) &&
  9140. (vsi->type == I40E_VSI_VMDQ2)) {
  9141. ret = i40e_vsi_config_rss(vsi);
  9142. }
  9143. return vsi;
  9144. err_rings:
  9145. i40e_vsi_free_q_vectors(vsi);
  9146. err_msix:
  9147. if (vsi->netdev_registered) {
  9148. vsi->netdev_registered = false;
  9149. unregister_netdev(vsi->netdev);
  9150. free_netdev(vsi->netdev);
  9151. vsi->netdev = NULL;
  9152. }
  9153. err_netdev:
  9154. i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
  9155. err_vsi:
  9156. i40e_vsi_clear(vsi);
  9157. err_alloc:
  9158. return NULL;
  9159. }
  9160. /**
  9161. * i40e_veb_get_bw_info - Query VEB BW information
  9162. * @veb: the veb to query
  9163. *
  9164. * Query the Tx scheduler BW configuration data for given VEB
  9165. **/
  9166. static int i40e_veb_get_bw_info(struct i40e_veb *veb)
  9167. {
  9168. struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
  9169. struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
  9170. struct i40e_pf *pf = veb->pf;
  9171. struct i40e_hw *hw = &pf->hw;
  9172. u32 tc_bw_max;
  9173. int ret = 0;
  9174. int i;
  9175. ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
  9176. &bw_data, NULL);
  9177. if (ret) {
  9178. dev_info(&pf->pdev->dev,
  9179. "query veb bw config failed, err %s aq_err %s\n",
  9180. i40e_stat_str(&pf->hw, ret),
  9181. i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
  9182. goto out;
  9183. }
  9184. ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
  9185. &ets_data, NULL);
  9186. if (ret) {
  9187. dev_info(&pf->pdev->dev,
  9188. "query veb bw ets config failed, err %s aq_err %s\n",
  9189. i40e_stat_str(&pf->hw, ret),
  9190. i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
  9191. goto out;
  9192. }
  9193. veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
  9194. veb->bw_max_quanta = ets_data.tc_bw_max;
  9195. veb->is_abs_credits = bw_data.absolute_credits_enable;
  9196. veb->enabled_tc = ets_data.tc_valid_bits;
  9197. tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
  9198. (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
  9199. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
  9200. veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
  9201. veb->bw_tc_limit_credits[i] =
  9202. le16_to_cpu(bw_data.tc_bw_limits[i]);
  9203. veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
  9204. }
  9205. out:
  9206. return ret;
  9207. }
  9208. /**
  9209. * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
  9210. * @pf: board private structure
  9211. *
  9212. * On error: returns error code (negative)
  9213. * On success: returns vsi index in PF (positive)
  9214. **/
  9215. static int i40e_veb_mem_alloc(struct i40e_pf *pf)
  9216. {
  9217. int ret = -ENOENT;
  9218. struct i40e_veb *veb;
  9219. int i;
  9220. /* Need to protect the allocation of switch elements at the PF level */
  9221. mutex_lock(&pf->switch_mutex);
  9222. /* VEB list may be fragmented if VEB creation/destruction has
  9223. * been happening. We can afford to do a quick scan to look
  9224. * for any free slots in the list.
  9225. *
  9226. * find next empty veb slot, looping back around if necessary
  9227. */
  9228. i = 0;
  9229. while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
  9230. i++;
  9231. if (i >= I40E_MAX_VEB) {
  9232. ret = -ENOMEM;
  9233. goto err_alloc_veb; /* out of VEB slots! */
  9234. }
  9235. veb = kzalloc(sizeof(*veb), GFP_KERNEL);
  9236. if (!veb) {
  9237. ret = -ENOMEM;
  9238. goto err_alloc_veb;
  9239. }
  9240. veb->pf = pf;
  9241. veb->idx = i;
  9242. veb->enabled_tc = 1;
  9243. pf->veb[i] = veb;
  9244. ret = i;
  9245. err_alloc_veb:
  9246. mutex_unlock(&pf->switch_mutex);
  9247. return ret;
  9248. }
  9249. /**
  9250. * i40e_switch_branch_release - Delete a branch of the switch tree
  9251. * @branch: where to start deleting
  9252. *
  9253. * This uses recursion to find the tips of the branch to be
  9254. * removed, deleting until we get back to and can delete this VEB.
  9255. **/
  9256. static void i40e_switch_branch_release(struct i40e_veb *branch)
  9257. {
  9258. struct i40e_pf *pf = branch->pf;
  9259. u16 branch_seid = branch->seid;
  9260. u16 veb_idx = branch->idx;
  9261. int i;
  9262. /* release any VEBs on this VEB - RECURSION */
  9263. for (i = 0; i < I40E_MAX_VEB; i++) {
  9264. if (!pf->veb[i])
  9265. continue;
  9266. if (pf->veb[i]->uplink_seid == branch->seid)
  9267. i40e_switch_branch_release(pf->veb[i]);
  9268. }
  9269. /* Release the VSIs on this VEB, but not the owner VSI.
  9270. *
  9271. * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
  9272. * the VEB itself, so don't use (*branch) after this loop.
  9273. */
  9274. for (i = 0; i < pf->num_alloc_vsi; i++) {
  9275. if (!pf->vsi[i])
  9276. continue;
  9277. if (pf->vsi[i]->uplink_seid == branch_seid &&
  9278. (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
  9279. i40e_vsi_release(pf->vsi[i]);
  9280. }
  9281. }
  9282. /* There's one corner case where the VEB might not have been
  9283. * removed, so double check it here and remove it if needed.
  9284. * This case happens if the veb was created from the debugfs
  9285. * commands and no VSIs were added to it.
  9286. */
  9287. if (pf->veb[veb_idx])
  9288. i40e_veb_release(pf->veb[veb_idx]);
  9289. }
  9290. /**
  9291. * i40e_veb_clear - remove veb struct
  9292. * @veb: the veb to remove
  9293. **/
  9294. static void i40e_veb_clear(struct i40e_veb *veb)
  9295. {
  9296. if (!veb)
  9297. return;
  9298. if (veb->pf) {
  9299. struct i40e_pf *pf = veb->pf;
  9300. mutex_lock(&pf->switch_mutex);
  9301. if (pf->veb[veb->idx] == veb)
  9302. pf->veb[veb->idx] = NULL;
  9303. mutex_unlock(&pf->switch_mutex);
  9304. }
  9305. kfree(veb);
  9306. }
  9307. /**
  9308. * i40e_veb_release - Delete a VEB and free its resources
  9309. * @veb: the VEB being removed
  9310. **/
  9311. void i40e_veb_release(struct i40e_veb *veb)
  9312. {
  9313. struct i40e_vsi *vsi = NULL;
  9314. struct i40e_pf *pf;
  9315. int i, n = 0;
  9316. pf = veb->pf;
  9317. /* find the remaining VSI and check for extras */
  9318. for (i = 0; i < pf->num_alloc_vsi; i++) {
  9319. if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
  9320. n++;
  9321. vsi = pf->vsi[i];
  9322. }
  9323. }
  9324. if (n != 1) {
  9325. dev_info(&pf->pdev->dev,
  9326. "can't remove VEB %d with %d VSIs left\n",
  9327. veb->seid, n);
  9328. return;
  9329. }
  9330. /* move the remaining VSI to uplink veb */
  9331. vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
  9332. if (veb->uplink_seid) {
  9333. vsi->uplink_seid = veb->uplink_seid;
  9334. if (veb->uplink_seid == pf->mac_seid)
  9335. vsi->veb_idx = I40E_NO_VEB;
  9336. else
  9337. vsi->veb_idx = veb->veb_idx;
  9338. } else {
  9339. /* floating VEB */
  9340. vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
  9341. vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
  9342. }
  9343. i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
  9344. i40e_veb_clear(veb);
  9345. }
  9346. /**
  9347. * i40e_add_veb - create the VEB in the switch
  9348. * @veb: the VEB to be instantiated
  9349. * @vsi: the controlling VSI
  9350. **/
  9351. static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
  9352. {
  9353. struct i40e_pf *pf = veb->pf;
  9354. bool enable_stats = !!(pf->flags & I40E_FLAG_VEB_STATS_ENABLED);
  9355. int ret;
  9356. ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi->seid,
  9357. veb->enabled_tc, false,
  9358. &veb->seid, enable_stats, NULL);
  9359. /* get a VEB from the hardware */
  9360. if (ret) {
  9361. dev_info(&pf->pdev->dev,
  9362. "couldn't add VEB, err %s aq_err %s\n",
  9363. i40e_stat_str(&pf->hw, ret),
  9364. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  9365. return -EPERM;
  9366. }
  9367. /* get statistics counter */
  9368. ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
  9369. &veb->stats_idx, NULL, NULL, NULL);
  9370. if (ret) {
  9371. dev_info(&pf->pdev->dev,
  9372. "couldn't get VEB statistics idx, err %s aq_err %s\n",
  9373. i40e_stat_str(&pf->hw, ret),
  9374. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  9375. return -EPERM;
  9376. }
  9377. ret = i40e_veb_get_bw_info(veb);
  9378. if (ret) {
  9379. dev_info(&pf->pdev->dev,
  9380. "couldn't get VEB bw info, err %s aq_err %s\n",
  9381. i40e_stat_str(&pf->hw, ret),
  9382. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  9383. i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
  9384. return -ENOENT;
  9385. }
  9386. vsi->uplink_seid = veb->seid;
  9387. vsi->veb_idx = veb->idx;
  9388. vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
  9389. return 0;
  9390. }
  9391. /**
  9392. * i40e_veb_setup - Set up a VEB
  9393. * @pf: board private structure
  9394. * @flags: VEB setup flags
  9395. * @uplink_seid: the switch element to link to
  9396. * @vsi_seid: the initial VSI seid
  9397. * @enabled_tc: Enabled TC bit-map
  9398. *
  9399. * This allocates the sw VEB structure and links it into the switch
  9400. * It is possible and legal for this to be a duplicate of an already
  9401. * existing VEB. It is also possible for both uplink and vsi seids
  9402. * to be zero, in order to create a floating VEB.
  9403. *
  9404. * Returns pointer to the successfully allocated VEB sw struct on
  9405. * success, otherwise returns NULL on failure.
  9406. **/
  9407. struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
  9408. u16 uplink_seid, u16 vsi_seid,
  9409. u8 enabled_tc)
  9410. {
  9411. struct i40e_veb *veb, *uplink_veb = NULL;
  9412. int vsi_idx, veb_idx;
  9413. int ret;
  9414. /* if one seid is 0, the other must be 0 to create a floating relay */
  9415. if ((uplink_seid == 0 || vsi_seid == 0) &&
  9416. (uplink_seid + vsi_seid != 0)) {
  9417. dev_info(&pf->pdev->dev,
  9418. "one, not both seid's are 0: uplink=%d vsi=%d\n",
  9419. uplink_seid, vsi_seid);
  9420. return NULL;
  9421. }
  9422. /* make sure there is such a vsi and uplink */
  9423. for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
  9424. if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
  9425. break;
  9426. if (vsi_idx >= pf->num_alloc_vsi && vsi_seid != 0) {
  9427. dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
  9428. vsi_seid);
  9429. return NULL;
  9430. }
  9431. if (uplink_seid && uplink_seid != pf->mac_seid) {
  9432. for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
  9433. if (pf->veb[veb_idx] &&
  9434. pf->veb[veb_idx]->seid == uplink_seid) {
  9435. uplink_veb = pf->veb[veb_idx];
  9436. break;
  9437. }
  9438. }
  9439. if (!uplink_veb) {
  9440. dev_info(&pf->pdev->dev,
  9441. "uplink seid %d not found\n", uplink_seid);
  9442. return NULL;
  9443. }
  9444. }
  9445. /* get veb sw struct */
  9446. veb_idx = i40e_veb_mem_alloc(pf);
  9447. if (veb_idx < 0)
  9448. goto err_alloc;
  9449. veb = pf->veb[veb_idx];
  9450. veb->flags = flags;
  9451. veb->uplink_seid = uplink_seid;
  9452. veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
  9453. veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
  9454. /* create the VEB in the switch */
  9455. ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
  9456. if (ret)
  9457. goto err_veb;
  9458. if (vsi_idx == pf->lan_vsi)
  9459. pf->lan_veb = veb->idx;
  9460. return veb;
  9461. err_veb:
  9462. i40e_veb_clear(veb);
  9463. err_alloc:
  9464. return NULL;
  9465. }
  9466. /**
  9467. * i40e_setup_pf_switch_element - set PF vars based on switch type
  9468. * @pf: board private structure
  9469. * @ele: element we are building info from
  9470. * @num_reported: total number of elements
  9471. * @printconfig: should we print the contents
  9472. *
  9473. * helper function to assist in extracting a few useful SEID values.
  9474. **/
  9475. static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
  9476. struct i40e_aqc_switch_config_element_resp *ele,
  9477. u16 num_reported, bool printconfig)
  9478. {
  9479. u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
  9480. u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
  9481. u8 element_type = ele->element_type;
  9482. u16 seid = le16_to_cpu(ele->seid);
  9483. if (printconfig)
  9484. dev_info(&pf->pdev->dev,
  9485. "type=%d seid=%d uplink=%d downlink=%d\n",
  9486. element_type, seid, uplink_seid, downlink_seid);
  9487. switch (element_type) {
  9488. case I40E_SWITCH_ELEMENT_TYPE_MAC:
  9489. pf->mac_seid = seid;
  9490. break;
  9491. case I40E_SWITCH_ELEMENT_TYPE_VEB:
  9492. /* Main VEB? */
  9493. if (uplink_seid != pf->mac_seid)
  9494. break;
  9495. if (pf->lan_veb == I40E_NO_VEB) {
  9496. int v;
  9497. /* find existing or else empty VEB */
  9498. for (v = 0; v < I40E_MAX_VEB; v++) {
  9499. if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
  9500. pf->lan_veb = v;
  9501. break;
  9502. }
  9503. }
  9504. if (pf->lan_veb == I40E_NO_VEB) {
  9505. v = i40e_veb_mem_alloc(pf);
  9506. if (v < 0)
  9507. break;
  9508. pf->lan_veb = v;
  9509. }
  9510. }
  9511. pf->veb[pf->lan_veb]->seid = seid;
  9512. pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
  9513. pf->veb[pf->lan_veb]->pf = pf;
  9514. pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
  9515. break;
  9516. case I40E_SWITCH_ELEMENT_TYPE_VSI:
  9517. if (num_reported != 1)
  9518. break;
  9519. /* This is immediately after a reset so we can assume this is
  9520. * the PF's VSI
  9521. */
  9522. pf->mac_seid = uplink_seid;
  9523. pf->pf_seid = downlink_seid;
  9524. pf->main_vsi_seid = seid;
  9525. if (printconfig)
  9526. dev_info(&pf->pdev->dev,
  9527. "pf_seid=%d main_vsi_seid=%d\n",
  9528. pf->pf_seid, pf->main_vsi_seid);
  9529. break;
  9530. case I40E_SWITCH_ELEMENT_TYPE_PF:
  9531. case I40E_SWITCH_ELEMENT_TYPE_VF:
  9532. case I40E_SWITCH_ELEMENT_TYPE_EMP:
  9533. case I40E_SWITCH_ELEMENT_TYPE_BMC:
  9534. case I40E_SWITCH_ELEMENT_TYPE_PE:
  9535. case I40E_SWITCH_ELEMENT_TYPE_PA:
  9536. /* ignore these for now */
  9537. break;
  9538. default:
  9539. dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
  9540. element_type, seid);
  9541. break;
  9542. }
  9543. }
  9544. /**
  9545. * i40e_fetch_switch_configuration - Get switch config from firmware
  9546. * @pf: board private structure
  9547. * @printconfig: should we print the contents
  9548. *
  9549. * Get the current switch configuration from the device and
  9550. * extract a few useful SEID values.
  9551. **/
  9552. int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
  9553. {
  9554. struct i40e_aqc_get_switch_config_resp *sw_config;
  9555. u16 next_seid = 0;
  9556. int ret = 0;
  9557. u8 *aq_buf;
  9558. int i;
  9559. aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
  9560. if (!aq_buf)
  9561. return -ENOMEM;
  9562. sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
  9563. do {
  9564. u16 num_reported, num_total;
  9565. ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
  9566. I40E_AQ_LARGE_BUF,
  9567. &next_seid, NULL);
  9568. if (ret) {
  9569. dev_info(&pf->pdev->dev,
  9570. "get switch config failed err %s aq_err %s\n",
  9571. i40e_stat_str(&pf->hw, ret),
  9572. i40e_aq_str(&pf->hw,
  9573. pf->hw.aq.asq_last_status));
  9574. kfree(aq_buf);
  9575. return -ENOENT;
  9576. }
  9577. num_reported = le16_to_cpu(sw_config->header.num_reported);
  9578. num_total = le16_to_cpu(sw_config->header.num_total);
  9579. if (printconfig)
  9580. dev_info(&pf->pdev->dev,
  9581. "header: %d reported %d total\n",
  9582. num_reported, num_total);
  9583. for (i = 0; i < num_reported; i++) {
  9584. struct i40e_aqc_switch_config_element_resp *ele =
  9585. &sw_config->element[i];
  9586. i40e_setup_pf_switch_element(pf, ele, num_reported,
  9587. printconfig);
  9588. }
  9589. } while (next_seid != 0);
  9590. kfree(aq_buf);
  9591. return ret;
  9592. }
  9593. /**
  9594. * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
  9595. * @pf: board private structure
  9596. * @reinit: if the Main VSI needs to re-initialized.
  9597. *
  9598. * Returns 0 on success, negative value on failure
  9599. **/
  9600. static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit)
  9601. {
  9602. u16 flags = 0;
  9603. int ret;
  9604. /* find out what's out there already */
  9605. ret = i40e_fetch_switch_configuration(pf, false);
  9606. if (ret) {
  9607. dev_info(&pf->pdev->dev,
  9608. "couldn't fetch switch config, err %s aq_err %s\n",
  9609. i40e_stat_str(&pf->hw, ret),
  9610. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  9611. return ret;
  9612. }
  9613. i40e_pf_reset_stats(pf);
  9614. /* set the switch config bit for the whole device to
  9615. * support limited promisc or true promisc
  9616. * when user requests promisc. The default is limited
  9617. * promisc.
  9618. */
  9619. if ((pf->hw.pf_id == 0) &&
  9620. !(pf->flags & I40E_FLAG_TRUE_PROMISC_SUPPORT))
  9621. flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
  9622. if (pf->hw.pf_id == 0) {
  9623. u16 valid_flags;
  9624. valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
  9625. ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags,
  9626. NULL);
  9627. if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) {
  9628. dev_info(&pf->pdev->dev,
  9629. "couldn't set switch config bits, err %s aq_err %s\n",
  9630. i40e_stat_str(&pf->hw, ret),
  9631. i40e_aq_str(&pf->hw,
  9632. pf->hw.aq.asq_last_status));
  9633. /* not a fatal problem, just keep going */
  9634. }
  9635. }
  9636. /* first time setup */
  9637. if (pf->lan_vsi == I40E_NO_VSI || reinit) {
  9638. struct i40e_vsi *vsi = NULL;
  9639. u16 uplink_seid;
  9640. /* Set up the PF VSI associated with the PF's main VSI
  9641. * that is already in the HW switch
  9642. */
  9643. if (pf->lan_veb != I40E_NO_VEB && pf->veb[pf->lan_veb])
  9644. uplink_seid = pf->veb[pf->lan_veb]->seid;
  9645. else
  9646. uplink_seid = pf->mac_seid;
  9647. if (pf->lan_vsi == I40E_NO_VSI)
  9648. vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
  9649. else if (reinit)
  9650. vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
  9651. if (!vsi) {
  9652. dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
  9653. i40e_fdir_teardown(pf);
  9654. return -EAGAIN;
  9655. }
  9656. } else {
  9657. /* force a reset of TC and queue layout configurations */
  9658. u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
  9659. pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
  9660. pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
  9661. i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
  9662. }
  9663. i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
  9664. i40e_fdir_sb_setup(pf);
  9665. /* Setup static PF queue filter control settings */
  9666. ret = i40e_setup_pf_filter_control(pf);
  9667. if (ret) {
  9668. dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
  9669. ret);
  9670. /* Failure here should not stop continuing other steps */
  9671. }
  9672. /* enable RSS in the HW, even for only one queue, as the stack can use
  9673. * the hash
  9674. */
  9675. if ((pf->flags & I40E_FLAG_RSS_ENABLED))
  9676. i40e_pf_config_rss(pf);
  9677. /* fill in link information and enable LSE reporting */
  9678. i40e_link_event(pf);
  9679. /* Initialize user-specific link properties */
  9680. pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
  9681. I40E_AQ_AN_COMPLETED) ? true : false);
  9682. i40e_ptp_init(pf);
  9683. /* repopulate tunnel port filters */
  9684. i40e_sync_udp_filters(pf);
  9685. return ret;
  9686. }
  9687. /**
  9688. * i40e_determine_queue_usage - Work out queue distribution
  9689. * @pf: board private structure
  9690. **/
  9691. static void i40e_determine_queue_usage(struct i40e_pf *pf)
  9692. {
  9693. int queues_left;
  9694. pf->num_lan_qps = 0;
  9695. /* Find the max queues to be put into basic use. We'll always be
  9696. * using TC0, whether or not DCB is running, and TC0 will get the
  9697. * big RSS set.
  9698. */
  9699. queues_left = pf->hw.func_caps.num_tx_qp;
  9700. if ((queues_left == 1) ||
  9701. !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
  9702. /* one qp for PF, no queues for anything else */
  9703. queues_left = 0;
  9704. pf->alloc_rss_size = pf->num_lan_qps = 1;
  9705. /* make sure all the fancies are disabled */
  9706. pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
  9707. I40E_FLAG_IWARP_ENABLED |
  9708. I40E_FLAG_FD_SB_ENABLED |
  9709. I40E_FLAG_FD_ATR_ENABLED |
  9710. I40E_FLAG_DCB_CAPABLE |
  9711. I40E_FLAG_DCB_ENABLED |
  9712. I40E_FLAG_SRIOV_ENABLED |
  9713. I40E_FLAG_VMDQ_ENABLED);
  9714. } else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
  9715. I40E_FLAG_FD_SB_ENABLED |
  9716. I40E_FLAG_FD_ATR_ENABLED |
  9717. I40E_FLAG_DCB_CAPABLE))) {
  9718. /* one qp for PF */
  9719. pf->alloc_rss_size = pf->num_lan_qps = 1;
  9720. queues_left -= pf->num_lan_qps;
  9721. pf->flags &= ~(I40E_FLAG_RSS_ENABLED |
  9722. I40E_FLAG_IWARP_ENABLED |
  9723. I40E_FLAG_FD_SB_ENABLED |
  9724. I40E_FLAG_FD_ATR_ENABLED |
  9725. I40E_FLAG_DCB_ENABLED |
  9726. I40E_FLAG_VMDQ_ENABLED);
  9727. } else {
  9728. /* Not enough queues for all TCs */
  9729. if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
  9730. (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
  9731. pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
  9732. I40E_FLAG_DCB_ENABLED);
  9733. dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
  9734. }
  9735. pf->num_lan_qps = max_t(int, pf->rss_size_max,
  9736. num_online_cpus());
  9737. pf->num_lan_qps = min_t(int, pf->num_lan_qps,
  9738. pf->hw.func_caps.num_tx_qp);
  9739. queues_left -= pf->num_lan_qps;
  9740. }
  9741. if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
  9742. if (queues_left > 1) {
  9743. queues_left -= 1; /* save 1 queue for FD */
  9744. } else {
  9745. pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
  9746. dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
  9747. }
  9748. }
  9749. if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
  9750. pf->num_vf_qps && pf->num_req_vfs && queues_left) {
  9751. pf->num_req_vfs = min_t(int, pf->num_req_vfs,
  9752. (queues_left / pf->num_vf_qps));
  9753. queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
  9754. }
  9755. if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
  9756. pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
  9757. pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
  9758. (queues_left / pf->num_vmdq_qps));
  9759. queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
  9760. }
  9761. pf->queues_left = queues_left;
  9762. dev_dbg(&pf->pdev->dev,
  9763. "qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
  9764. pf->hw.func_caps.num_tx_qp,
  9765. !!(pf->flags & I40E_FLAG_FD_SB_ENABLED),
  9766. pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
  9767. pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
  9768. queues_left);
  9769. }
  9770. /**
  9771. * i40e_setup_pf_filter_control - Setup PF static filter control
  9772. * @pf: PF to be setup
  9773. *
  9774. * i40e_setup_pf_filter_control sets up a PF's initial filter control
  9775. * settings. If PE/FCoE are enabled then it will also set the per PF
  9776. * based filter sizes required for them. It also enables Flow director,
  9777. * ethertype and macvlan type filter settings for the pf.
  9778. *
  9779. * Returns 0 on success, negative on failure
  9780. **/
  9781. static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
  9782. {
  9783. struct i40e_filter_control_settings *settings = &pf->filter_settings;
  9784. settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
  9785. /* Flow Director is enabled */
  9786. if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
  9787. settings->enable_fdir = true;
  9788. /* Ethtype and MACVLAN filters enabled for PF */
  9789. settings->enable_ethtype = true;
  9790. settings->enable_macvlan = true;
  9791. if (i40e_set_filter_control(&pf->hw, settings))
  9792. return -ENOENT;
  9793. return 0;
  9794. }
  9795. #define INFO_STRING_LEN 255
  9796. #define REMAIN(__x) (INFO_STRING_LEN - (__x))
  9797. static void i40e_print_features(struct i40e_pf *pf)
  9798. {
  9799. struct i40e_hw *hw = &pf->hw;
  9800. char *buf;
  9801. int i;
  9802. buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
  9803. if (!buf)
  9804. return;
  9805. i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
  9806. #ifdef CONFIG_PCI_IOV
  9807. i += snprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
  9808. #endif
  9809. i += snprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d",
  9810. pf->hw.func_caps.num_vsis,
  9811. pf->vsi[pf->lan_vsi]->num_queue_pairs);
  9812. if (pf->flags & I40E_FLAG_RSS_ENABLED)
  9813. i += snprintf(&buf[i], REMAIN(i), " RSS");
  9814. if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
  9815. i += snprintf(&buf[i], REMAIN(i), " FD_ATR");
  9816. if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
  9817. i += snprintf(&buf[i], REMAIN(i), " FD_SB");
  9818. i += snprintf(&buf[i], REMAIN(i), " NTUPLE");
  9819. }
  9820. if (pf->flags & I40E_FLAG_DCB_CAPABLE)
  9821. i += snprintf(&buf[i], REMAIN(i), " DCB");
  9822. i += snprintf(&buf[i], REMAIN(i), " VxLAN");
  9823. i += snprintf(&buf[i], REMAIN(i), " Geneve");
  9824. if (pf->flags & I40E_FLAG_PTP)
  9825. i += snprintf(&buf[i], REMAIN(i), " PTP");
  9826. if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
  9827. i += snprintf(&buf[i], REMAIN(i), " VEB");
  9828. else
  9829. i += snprintf(&buf[i], REMAIN(i), " VEPA");
  9830. dev_info(&pf->pdev->dev, "%s\n", buf);
  9831. kfree(buf);
  9832. WARN_ON(i > INFO_STRING_LEN);
  9833. }
  9834. /**
  9835. * i40e_get_platform_mac_addr - get platform-specific MAC address
  9836. * @pdev: PCI device information struct
  9837. * @pf: board private structure
  9838. *
  9839. * Look up the MAC address for the device. First we'll try
  9840. * eth_platform_get_mac_address, which will check Open Firmware, or arch
  9841. * specific fallback. Otherwise, we'll default to the stored value in
  9842. * firmware.
  9843. **/
  9844. static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
  9845. {
  9846. if (eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
  9847. i40e_get_mac_addr(&pf->hw, pf->hw.mac.addr);
  9848. }
  9849. /**
  9850. * i40e_probe - Device initialization routine
  9851. * @pdev: PCI device information struct
  9852. * @ent: entry in i40e_pci_tbl
  9853. *
  9854. * i40e_probe initializes a PF identified by a pci_dev structure.
  9855. * The OS initialization, configuring of the PF private structure,
  9856. * and a hardware reset occur.
  9857. *
  9858. * Returns 0 on success, negative on failure
  9859. **/
  9860. static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  9861. {
  9862. struct i40e_aq_get_phy_abilities_resp abilities;
  9863. struct i40e_pf *pf;
  9864. struct i40e_hw *hw;
  9865. static u16 pfs_found;
  9866. u16 wol_nvm_bits;
  9867. u16 link_status;
  9868. int err;
  9869. u32 val;
  9870. u32 i;
  9871. u8 set_fc_aq_fail;
  9872. err = pci_enable_device_mem(pdev);
  9873. if (err)
  9874. return err;
  9875. /* set up for high or low dma */
  9876. err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
  9877. if (err) {
  9878. err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  9879. if (err) {
  9880. dev_err(&pdev->dev,
  9881. "DMA configuration failed: 0x%x\n", err);
  9882. goto err_dma;
  9883. }
  9884. }
  9885. /* set up pci connections */
  9886. err = pci_request_mem_regions(pdev, i40e_driver_name);
  9887. if (err) {
  9888. dev_info(&pdev->dev,
  9889. "pci_request_selected_regions failed %d\n", err);
  9890. goto err_pci_reg;
  9891. }
  9892. pci_enable_pcie_error_reporting(pdev);
  9893. pci_set_master(pdev);
  9894. /* Now that we have a PCI connection, we need to do the
  9895. * low level device setup. This is primarily setting up
  9896. * the Admin Queue structures and then querying for the
  9897. * device's current profile information.
  9898. */
  9899. pf = kzalloc(sizeof(*pf), GFP_KERNEL);
  9900. if (!pf) {
  9901. err = -ENOMEM;
  9902. goto err_pf_alloc;
  9903. }
  9904. pf->next_vsi = 0;
  9905. pf->pdev = pdev;
  9906. set_bit(__I40E_DOWN, pf->state);
  9907. hw = &pf->hw;
  9908. hw->back = pf;
  9909. pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
  9910. I40E_MAX_CSR_SPACE);
  9911. hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
  9912. if (!hw->hw_addr) {
  9913. err = -EIO;
  9914. dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
  9915. (unsigned int)pci_resource_start(pdev, 0),
  9916. pf->ioremap_len, err);
  9917. goto err_ioremap;
  9918. }
  9919. hw->vendor_id = pdev->vendor;
  9920. hw->device_id = pdev->device;
  9921. pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
  9922. hw->subsystem_vendor_id = pdev->subsystem_vendor;
  9923. hw->subsystem_device_id = pdev->subsystem_device;
  9924. hw->bus.device = PCI_SLOT(pdev->devfn);
  9925. hw->bus.func = PCI_FUNC(pdev->devfn);
  9926. hw->bus.bus_id = pdev->bus->number;
  9927. pf->instance = pfs_found;
  9928. INIT_LIST_HEAD(&pf->l3_flex_pit_list);
  9929. INIT_LIST_HEAD(&pf->l4_flex_pit_list);
  9930. /* set up the locks for the AQ, do this only once in probe
  9931. * and destroy them only once in remove
  9932. */
  9933. mutex_init(&hw->aq.asq_mutex);
  9934. mutex_init(&hw->aq.arq_mutex);
  9935. pf->msg_enable = netif_msg_init(debug,
  9936. NETIF_MSG_DRV |
  9937. NETIF_MSG_PROBE |
  9938. NETIF_MSG_LINK);
  9939. if (debug < -1)
  9940. pf->hw.debug_mask = debug;
  9941. /* do a special CORER for clearing PXE mode once at init */
  9942. if (hw->revision_id == 0 &&
  9943. (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
  9944. wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
  9945. i40e_flush(hw);
  9946. msleep(200);
  9947. pf->corer_count++;
  9948. i40e_clear_pxe_mode(hw);
  9949. }
  9950. /* Reset here to make sure all is clean and to define PF 'n' */
  9951. i40e_clear_hw(hw);
  9952. err = i40e_pf_reset(hw);
  9953. if (err) {
  9954. dev_info(&pdev->dev, "Initial pf_reset failed: %d\n", err);
  9955. goto err_pf_reset;
  9956. }
  9957. pf->pfr_count++;
  9958. hw->aq.num_arq_entries = I40E_AQ_LEN;
  9959. hw->aq.num_asq_entries = I40E_AQ_LEN;
  9960. hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
  9961. hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
  9962. pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
  9963. snprintf(pf->int_name, sizeof(pf->int_name) - 1,
  9964. "%s-%s:misc",
  9965. dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
  9966. err = i40e_init_shared_code(hw);
  9967. if (err) {
  9968. dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
  9969. err);
  9970. goto err_pf_reset;
  9971. }
  9972. /* set up a default setting for link flow control */
  9973. pf->hw.fc.requested_mode = I40E_FC_NONE;
  9974. err = i40e_init_adminq(hw);
  9975. if (err) {
  9976. if (err == I40E_ERR_FIRMWARE_API_VERSION)
  9977. dev_info(&pdev->dev,
  9978. "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");
  9979. else
  9980. dev_info(&pdev->dev,
  9981. "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
  9982. goto err_pf_reset;
  9983. }
  9984. i40e_get_oem_version(hw);
  9985. /* provide nvm, fw, api versions */
  9986. dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s\n",
  9987. hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
  9988. hw->aq.api_maj_ver, hw->aq.api_min_ver,
  9989. i40e_nvm_version_str(hw));
  9990. if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
  9991. hw->aq.api_min_ver > I40E_FW_API_VERSION_MINOR)
  9992. dev_info(&pdev->dev,
  9993. "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");
  9994. else if (hw->aq.api_maj_ver < I40E_FW_API_VERSION_MAJOR ||
  9995. hw->aq.api_min_ver < (I40E_FW_API_VERSION_MINOR - 1))
  9996. dev_info(&pdev->dev,
  9997. "The driver for the device detected an older version of the NVM image than expected. Please update the NVM image.\n");
  9998. i40e_verify_eeprom(pf);
  9999. /* Rev 0 hardware was never productized */
  10000. if (hw->revision_id < 1)
  10001. 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");
  10002. i40e_clear_pxe_mode(hw);
  10003. err = i40e_get_capabilities(pf);
  10004. if (err)
  10005. goto err_adminq_setup;
  10006. err = i40e_sw_init(pf);
  10007. if (err) {
  10008. dev_info(&pdev->dev, "sw_init failed: %d\n", err);
  10009. goto err_sw_init;
  10010. }
  10011. err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
  10012. hw->func_caps.num_rx_qp, 0, 0);
  10013. if (err) {
  10014. dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
  10015. goto err_init_lan_hmc;
  10016. }
  10017. err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
  10018. if (err) {
  10019. dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
  10020. err = -ENOENT;
  10021. goto err_configure_lan_hmc;
  10022. }
  10023. /* Disable LLDP for NICs that have firmware versions lower than v4.3.
  10024. * Ignore error return codes because if it was already disabled via
  10025. * hardware settings this will fail
  10026. */
  10027. if (pf->flags & I40E_FLAG_STOP_FW_LLDP) {
  10028. dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
  10029. i40e_aq_stop_lldp(hw, true, NULL);
  10030. }
  10031. /* allow a platform config to override the HW addr */
  10032. i40e_get_platform_mac_addr(pdev, pf);
  10033. if (!is_valid_ether_addr(hw->mac.addr)) {
  10034. dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
  10035. err = -EIO;
  10036. goto err_mac_addr;
  10037. }
  10038. dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
  10039. ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
  10040. i40e_get_port_mac_addr(hw, hw->mac.port_addr);
  10041. if (is_valid_ether_addr(hw->mac.port_addr))
  10042. pf->flags |= I40E_FLAG_PORT_ID_VALID;
  10043. pci_set_drvdata(pdev, pf);
  10044. pci_save_state(pdev);
  10045. #ifdef CONFIG_I40E_DCB
  10046. err = i40e_init_pf_dcb(pf);
  10047. if (err) {
  10048. dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
  10049. pf->flags &= ~(I40E_FLAG_DCB_CAPABLE | I40E_FLAG_DCB_ENABLED);
  10050. /* Continue without DCB enabled */
  10051. }
  10052. #endif /* CONFIG_I40E_DCB */
  10053. /* set up periodic task facility */
  10054. setup_timer(&pf->service_timer, i40e_service_timer, (unsigned long)pf);
  10055. pf->service_timer_period = HZ;
  10056. INIT_WORK(&pf->service_task, i40e_service_task);
  10057. clear_bit(__I40E_SERVICE_SCHED, pf->state);
  10058. /* NVM bit on means WoL disabled for the port */
  10059. i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
  10060. if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
  10061. pf->wol_en = false;
  10062. else
  10063. pf->wol_en = true;
  10064. device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
  10065. /* set up the main switch operations */
  10066. i40e_determine_queue_usage(pf);
  10067. err = i40e_init_interrupt_scheme(pf);
  10068. if (err)
  10069. goto err_switch_setup;
  10070. /* The number of VSIs reported by the FW is the minimum guaranteed
  10071. * to us; HW supports far more and we share the remaining pool with
  10072. * the other PFs. We allocate space for more than the guarantee with
  10073. * the understanding that we might not get them all later.
  10074. */
  10075. if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
  10076. pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
  10077. else
  10078. pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
  10079. /* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
  10080. pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
  10081. GFP_KERNEL);
  10082. if (!pf->vsi) {
  10083. err = -ENOMEM;
  10084. goto err_switch_setup;
  10085. }
  10086. #ifdef CONFIG_PCI_IOV
  10087. /* prep for VF support */
  10088. if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
  10089. (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
  10090. !test_bit(__I40E_BAD_EEPROM, pf->state)) {
  10091. if (pci_num_vf(pdev))
  10092. pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
  10093. }
  10094. #endif
  10095. err = i40e_setup_pf_switch(pf, false);
  10096. if (err) {
  10097. dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
  10098. goto err_vsis;
  10099. }
  10100. /* Make sure flow control is set according to current settings */
  10101. err = i40e_set_fc(hw, &set_fc_aq_fail, true);
  10102. if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_GET)
  10103. dev_dbg(&pf->pdev->dev,
  10104. "Set fc with err %s aq_err %s on get_phy_cap\n",
  10105. i40e_stat_str(hw, err),
  10106. i40e_aq_str(hw, hw->aq.asq_last_status));
  10107. if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_SET)
  10108. dev_dbg(&pf->pdev->dev,
  10109. "Set fc with err %s aq_err %s on set_phy_config\n",
  10110. i40e_stat_str(hw, err),
  10111. i40e_aq_str(hw, hw->aq.asq_last_status));
  10112. if (set_fc_aq_fail & I40E_SET_FC_AQ_FAIL_UPDATE)
  10113. dev_dbg(&pf->pdev->dev,
  10114. "Set fc with err %s aq_err %s on get_link_info\n",
  10115. i40e_stat_str(hw, err),
  10116. i40e_aq_str(hw, hw->aq.asq_last_status));
  10117. /* if FDIR VSI was set up, start it now */
  10118. for (i = 0; i < pf->num_alloc_vsi; i++) {
  10119. if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
  10120. i40e_vsi_open(pf->vsi[i]);
  10121. break;
  10122. }
  10123. }
  10124. /* The driver only wants link up/down and module qualification
  10125. * reports from firmware. Note the negative logic.
  10126. */
  10127. err = i40e_aq_set_phy_int_mask(&pf->hw,
  10128. ~(I40E_AQ_EVENT_LINK_UPDOWN |
  10129. I40E_AQ_EVENT_MEDIA_NA |
  10130. I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
  10131. if (err)
  10132. dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
  10133. i40e_stat_str(&pf->hw, err),
  10134. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  10135. /* Reconfigure hardware for allowing smaller MSS in the case
  10136. * of TSO, so that we avoid the MDD being fired and causing
  10137. * a reset in the case of small MSS+TSO.
  10138. */
  10139. val = rd32(hw, I40E_REG_MSS);
  10140. if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
  10141. val &= ~I40E_REG_MSS_MIN_MASK;
  10142. val |= I40E_64BYTE_MSS;
  10143. wr32(hw, I40E_REG_MSS, val);
  10144. }
  10145. if (pf->flags & I40E_FLAG_RESTART_AUTONEG) {
  10146. msleep(75);
  10147. err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
  10148. if (err)
  10149. dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
  10150. i40e_stat_str(&pf->hw, err),
  10151. i40e_aq_str(&pf->hw,
  10152. pf->hw.aq.asq_last_status));
  10153. }
  10154. /* The main driver is (mostly) up and happy. We need to set this state
  10155. * before setting up the misc vector or we get a race and the vector
  10156. * ends up disabled forever.
  10157. */
  10158. clear_bit(__I40E_DOWN, pf->state);
  10159. /* In case of MSIX we are going to setup the misc vector right here
  10160. * to handle admin queue events etc. In case of legacy and MSI
  10161. * the misc functionality and queue processing is combined in
  10162. * the same vector and that gets setup at open.
  10163. */
  10164. if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
  10165. err = i40e_setup_misc_vector(pf);
  10166. if (err) {
  10167. dev_info(&pdev->dev,
  10168. "setup of misc vector failed: %d\n", err);
  10169. goto err_vsis;
  10170. }
  10171. }
  10172. #ifdef CONFIG_PCI_IOV
  10173. /* prep for VF support */
  10174. if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
  10175. (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
  10176. !test_bit(__I40E_BAD_EEPROM, pf->state)) {
  10177. /* disable link interrupts for VFs */
  10178. val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
  10179. val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
  10180. wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
  10181. i40e_flush(hw);
  10182. if (pci_num_vf(pdev)) {
  10183. dev_info(&pdev->dev,
  10184. "Active VFs found, allocating resources.\n");
  10185. err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
  10186. if (err)
  10187. dev_info(&pdev->dev,
  10188. "Error %d allocating resources for existing VFs\n",
  10189. err);
  10190. }
  10191. }
  10192. #endif /* CONFIG_PCI_IOV */
  10193. if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
  10194. pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
  10195. pf->num_iwarp_msix,
  10196. I40E_IWARP_IRQ_PILE_ID);
  10197. if (pf->iwarp_base_vector < 0) {
  10198. dev_info(&pdev->dev,
  10199. "failed to get tracking for %d vectors for IWARP err=%d\n",
  10200. pf->num_iwarp_msix, pf->iwarp_base_vector);
  10201. pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
  10202. }
  10203. }
  10204. i40e_dbg_pf_init(pf);
  10205. /* tell the firmware that we're starting */
  10206. i40e_send_version(pf);
  10207. /* since everything's happy, start the service_task timer */
  10208. mod_timer(&pf->service_timer,
  10209. round_jiffies(jiffies + pf->service_timer_period));
  10210. /* add this PF to client device list and launch a client service task */
  10211. if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
  10212. err = i40e_lan_add_device(pf);
  10213. if (err)
  10214. dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
  10215. err);
  10216. }
  10217. #define PCI_SPEED_SIZE 8
  10218. #define PCI_WIDTH_SIZE 8
  10219. /* Devices on the IOSF bus do not have this information
  10220. * and will report PCI Gen 1 x 1 by default so don't bother
  10221. * checking them.
  10222. */
  10223. if (!(pf->flags & I40E_FLAG_NO_PCI_LINK_CHECK)) {
  10224. char speed[PCI_SPEED_SIZE] = "Unknown";
  10225. char width[PCI_WIDTH_SIZE] = "Unknown";
  10226. /* Get the negotiated link width and speed from PCI config
  10227. * space
  10228. */
  10229. pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
  10230. &link_status);
  10231. i40e_set_pci_config_data(hw, link_status);
  10232. switch (hw->bus.speed) {
  10233. case i40e_bus_speed_8000:
  10234. strncpy(speed, "8.0", PCI_SPEED_SIZE); break;
  10235. case i40e_bus_speed_5000:
  10236. strncpy(speed, "5.0", PCI_SPEED_SIZE); break;
  10237. case i40e_bus_speed_2500:
  10238. strncpy(speed, "2.5", PCI_SPEED_SIZE); break;
  10239. default:
  10240. break;
  10241. }
  10242. switch (hw->bus.width) {
  10243. case i40e_bus_width_pcie_x8:
  10244. strncpy(width, "8", PCI_WIDTH_SIZE); break;
  10245. case i40e_bus_width_pcie_x4:
  10246. strncpy(width, "4", PCI_WIDTH_SIZE); break;
  10247. case i40e_bus_width_pcie_x2:
  10248. strncpy(width, "2", PCI_WIDTH_SIZE); break;
  10249. case i40e_bus_width_pcie_x1:
  10250. strncpy(width, "1", PCI_WIDTH_SIZE); break;
  10251. default:
  10252. break;
  10253. }
  10254. dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
  10255. speed, width);
  10256. if (hw->bus.width < i40e_bus_width_pcie_x8 ||
  10257. hw->bus.speed < i40e_bus_speed_8000) {
  10258. dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
  10259. dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
  10260. }
  10261. }
  10262. /* get the requested speeds from the fw */
  10263. err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
  10264. if (err)
  10265. dev_dbg(&pf->pdev->dev, "get requested speeds ret = %s last_status = %s\n",
  10266. i40e_stat_str(&pf->hw, err),
  10267. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  10268. pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
  10269. /* get the supported phy types from the fw */
  10270. err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
  10271. if (err)
  10272. dev_dbg(&pf->pdev->dev, "get supported phy types ret = %s last_status = %s\n",
  10273. i40e_stat_str(&pf->hw, err),
  10274. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  10275. /* Add a filter to drop all Flow control frames from any VSI from being
  10276. * transmitted. By doing so we stop a malicious VF from sending out
  10277. * PAUSE or PFC frames and potentially controlling traffic for other
  10278. * PF/VF VSIs.
  10279. * The FW can still send Flow control frames if enabled.
  10280. */
  10281. i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
  10282. pf->main_vsi_seid);
  10283. if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
  10284. (pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
  10285. pf->flags |= I40E_FLAG_PHY_CONTROLS_LEDS;
  10286. if (pf->hw.device_id == I40E_DEV_ID_SFP_I_X722)
  10287. pf->flags |= I40E_FLAG_HAVE_CRT_RETIMER;
  10288. /* print a string summarizing features */
  10289. i40e_print_features(pf);
  10290. return 0;
  10291. /* Unwind what we've done if something failed in the setup */
  10292. err_vsis:
  10293. set_bit(__I40E_DOWN, pf->state);
  10294. i40e_clear_interrupt_scheme(pf);
  10295. kfree(pf->vsi);
  10296. err_switch_setup:
  10297. i40e_reset_interrupt_capability(pf);
  10298. del_timer_sync(&pf->service_timer);
  10299. err_mac_addr:
  10300. err_configure_lan_hmc:
  10301. (void)i40e_shutdown_lan_hmc(hw);
  10302. err_init_lan_hmc:
  10303. kfree(pf->qp_pile);
  10304. err_sw_init:
  10305. err_adminq_setup:
  10306. err_pf_reset:
  10307. iounmap(hw->hw_addr);
  10308. err_ioremap:
  10309. kfree(pf);
  10310. err_pf_alloc:
  10311. pci_disable_pcie_error_reporting(pdev);
  10312. pci_release_mem_regions(pdev);
  10313. err_pci_reg:
  10314. err_dma:
  10315. pci_disable_device(pdev);
  10316. return err;
  10317. }
  10318. /**
  10319. * i40e_remove - Device removal routine
  10320. * @pdev: PCI device information struct
  10321. *
  10322. * i40e_remove is called by the PCI subsystem to alert the driver
  10323. * that is should release a PCI device. This could be caused by a
  10324. * Hot-Plug event, or because the driver is going to be removed from
  10325. * memory.
  10326. **/
  10327. static void i40e_remove(struct pci_dev *pdev)
  10328. {
  10329. struct i40e_pf *pf = pci_get_drvdata(pdev);
  10330. struct i40e_hw *hw = &pf->hw;
  10331. i40e_status ret_code;
  10332. int i;
  10333. i40e_dbg_pf_exit(pf);
  10334. i40e_ptp_stop(pf);
  10335. /* Disable RSS in hw */
  10336. i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
  10337. i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
  10338. /* no more scheduling of any task */
  10339. set_bit(__I40E_SUSPENDED, pf->state);
  10340. set_bit(__I40E_DOWN, pf->state);
  10341. if (pf->service_timer.data)
  10342. del_timer_sync(&pf->service_timer);
  10343. if (pf->service_task.func)
  10344. cancel_work_sync(&pf->service_task);
  10345. /* Client close must be called explicitly here because the timer
  10346. * has been stopped.
  10347. */
  10348. i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
  10349. if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
  10350. i40e_free_vfs(pf);
  10351. pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
  10352. }
  10353. i40e_fdir_teardown(pf);
  10354. /* If there is a switch structure or any orphans, remove them.
  10355. * This will leave only the PF's VSI remaining.
  10356. */
  10357. for (i = 0; i < I40E_MAX_VEB; i++) {
  10358. if (!pf->veb[i])
  10359. continue;
  10360. if (pf->veb[i]->uplink_seid == pf->mac_seid ||
  10361. pf->veb[i]->uplink_seid == 0)
  10362. i40e_switch_branch_release(pf->veb[i]);
  10363. }
  10364. /* Now we can shutdown the PF's VSI, just before we kill
  10365. * adminq and hmc.
  10366. */
  10367. if (pf->vsi[pf->lan_vsi])
  10368. i40e_vsi_release(pf->vsi[pf->lan_vsi]);
  10369. /* remove attached clients */
  10370. if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
  10371. ret_code = i40e_lan_del_device(pf);
  10372. if (ret_code)
  10373. dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
  10374. ret_code);
  10375. }
  10376. /* shutdown and destroy the HMC */
  10377. if (hw->hmc.hmc_obj) {
  10378. ret_code = i40e_shutdown_lan_hmc(hw);
  10379. if (ret_code)
  10380. dev_warn(&pdev->dev,
  10381. "Failed to destroy the HMC resources: %d\n",
  10382. ret_code);
  10383. }
  10384. /* shutdown the adminq */
  10385. i40e_shutdown_adminq(hw);
  10386. /* destroy the locks only once, here */
  10387. mutex_destroy(&hw->aq.arq_mutex);
  10388. mutex_destroy(&hw->aq.asq_mutex);
  10389. /* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
  10390. i40e_clear_interrupt_scheme(pf);
  10391. for (i = 0; i < pf->num_alloc_vsi; i++) {
  10392. if (pf->vsi[i]) {
  10393. i40e_vsi_clear_rings(pf->vsi[i]);
  10394. i40e_vsi_clear(pf->vsi[i]);
  10395. pf->vsi[i] = NULL;
  10396. }
  10397. }
  10398. for (i = 0; i < I40E_MAX_VEB; i++) {
  10399. kfree(pf->veb[i]);
  10400. pf->veb[i] = NULL;
  10401. }
  10402. kfree(pf->qp_pile);
  10403. kfree(pf->vsi);
  10404. iounmap(hw->hw_addr);
  10405. kfree(pf);
  10406. pci_release_mem_regions(pdev);
  10407. pci_disable_pcie_error_reporting(pdev);
  10408. pci_disable_device(pdev);
  10409. }
  10410. /**
  10411. * i40e_pci_error_detected - warning that something funky happened in PCI land
  10412. * @pdev: PCI device information struct
  10413. *
  10414. * Called to warn that something happened and the error handling steps
  10415. * are in progress. Allows the driver to quiesce things, be ready for
  10416. * remediation.
  10417. **/
  10418. static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
  10419. enum pci_channel_state error)
  10420. {
  10421. struct i40e_pf *pf = pci_get_drvdata(pdev);
  10422. dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
  10423. if (!pf) {
  10424. dev_info(&pdev->dev,
  10425. "Cannot recover - error happened during device probe\n");
  10426. return PCI_ERS_RESULT_DISCONNECT;
  10427. }
  10428. /* shutdown all operations */
  10429. if (!test_bit(__I40E_SUSPENDED, pf->state))
  10430. i40e_prep_for_reset(pf, false);
  10431. /* Request a slot reset */
  10432. return PCI_ERS_RESULT_NEED_RESET;
  10433. }
  10434. /**
  10435. * i40e_pci_error_slot_reset - a PCI slot reset just happened
  10436. * @pdev: PCI device information struct
  10437. *
  10438. * Called to find if the driver can work with the device now that
  10439. * the pci slot has been reset. If a basic connection seems good
  10440. * (registers are readable and have sane content) then return a
  10441. * happy little PCI_ERS_RESULT_xxx.
  10442. **/
  10443. static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
  10444. {
  10445. struct i40e_pf *pf = pci_get_drvdata(pdev);
  10446. pci_ers_result_t result;
  10447. int err;
  10448. u32 reg;
  10449. dev_dbg(&pdev->dev, "%s\n", __func__);
  10450. if (pci_enable_device_mem(pdev)) {
  10451. dev_info(&pdev->dev,
  10452. "Cannot re-enable PCI device after reset.\n");
  10453. result = PCI_ERS_RESULT_DISCONNECT;
  10454. } else {
  10455. pci_set_master(pdev);
  10456. pci_restore_state(pdev);
  10457. pci_save_state(pdev);
  10458. pci_wake_from_d3(pdev, false);
  10459. reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
  10460. if (reg == 0)
  10461. result = PCI_ERS_RESULT_RECOVERED;
  10462. else
  10463. result = PCI_ERS_RESULT_DISCONNECT;
  10464. }
  10465. err = pci_cleanup_aer_uncorrect_error_status(pdev);
  10466. if (err) {
  10467. dev_info(&pdev->dev,
  10468. "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
  10469. err);
  10470. /* non-fatal, continue */
  10471. }
  10472. return result;
  10473. }
  10474. /**
  10475. * i40e_pci_error_resume - restart operations after PCI error recovery
  10476. * @pdev: PCI device information struct
  10477. *
  10478. * Called to allow the driver to bring things back up after PCI error
  10479. * and/or reset recovery has finished.
  10480. **/
  10481. static void i40e_pci_error_resume(struct pci_dev *pdev)
  10482. {
  10483. struct i40e_pf *pf = pci_get_drvdata(pdev);
  10484. dev_dbg(&pdev->dev, "%s\n", __func__);
  10485. if (test_bit(__I40E_SUSPENDED, pf->state))
  10486. return;
  10487. i40e_handle_reset_warning(pf, false);
  10488. }
  10489. /**
  10490. * i40e_enable_mc_magic_wake - enable multicast magic packet wake up
  10491. * using the mac_address_write admin q function
  10492. * @pf: pointer to i40e_pf struct
  10493. **/
  10494. static void i40e_enable_mc_magic_wake(struct i40e_pf *pf)
  10495. {
  10496. struct i40e_hw *hw = &pf->hw;
  10497. i40e_status ret;
  10498. u8 mac_addr[6];
  10499. u16 flags = 0;
  10500. /* Get current MAC address in case it's an LAA */
  10501. if (pf->vsi[pf->lan_vsi] && pf->vsi[pf->lan_vsi]->netdev) {
  10502. ether_addr_copy(mac_addr,
  10503. pf->vsi[pf->lan_vsi]->netdev->dev_addr);
  10504. } else {
  10505. dev_err(&pf->pdev->dev,
  10506. "Failed to retrieve MAC address; using default\n");
  10507. ether_addr_copy(mac_addr, hw->mac.addr);
  10508. }
  10509. /* The FW expects the mac address write cmd to first be called with
  10510. * one of these flags before calling it again with the multicast
  10511. * enable flags.
  10512. */
  10513. flags = I40E_AQC_WRITE_TYPE_LAA_WOL;
  10514. if (hw->func_caps.flex10_enable && hw->partition_id != 1)
  10515. flags = I40E_AQC_WRITE_TYPE_LAA_ONLY;
  10516. ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
  10517. if (ret) {
  10518. dev_err(&pf->pdev->dev,
  10519. "Failed to update MAC address registers; cannot enable Multicast Magic packet wake up");
  10520. return;
  10521. }
  10522. flags = I40E_AQC_MC_MAG_EN
  10523. | I40E_AQC_WOL_PRESERVE_ON_PFR
  10524. | I40E_AQC_WRITE_TYPE_UPDATE_MC_MAG;
  10525. ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
  10526. if (ret)
  10527. dev_err(&pf->pdev->dev,
  10528. "Failed to enable Multicast Magic Packet wake up\n");
  10529. }
  10530. /**
  10531. * i40e_shutdown - PCI callback for shutting down
  10532. * @pdev: PCI device information struct
  10533. **/
  10534. static void i40e_shutdown(struct pci_dev *pdev)
  10535. {
  10536. struct i40e_pf *pf = pci_get_drvdata(pdev);
  10537. struct i40e_hw *hw = &pf->hw;
  10538. set_bit(__I40E_SUSPENDED, pf->state);
  10539. set_bit(__I40E_DOWN, pf->state);
  10540. rtnl_lock();
  10541. i40e_prep_for_reset(pf, true);
  10542. rtnl_unlock();
  10543. wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
  10544. wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
  10545. del_timer_sync(&pf->service_timer);
  10546. cancel_work_sync(&pf->service_task);
  10547. i40e_fdir_teardown(pf);
  10548. /* Client close must be called explicitly here because the timer
  10549. * has been stopped.
  10550. */
  10551. i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
  10552. if (pf->wol_en && (pf->flags & I40E_FLAG_WOL_MC_MAGIC_PKT_WAKE))
  10553. i40e_enable_mc_magic_wake(pf);
  10554. i40e_prep_for_reset(pf, false);
  10555. wr32(hw, I40E_PFPM_APM,
  10556. (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
  10557. wr32(hw, I40E_PFPM_WUFC,
  10558. (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
  10559. i40e_clear_interrupt_scheme(pf);
  10560. if (system_state == SYSTEM_POWER_OFF) {
  10561. pci_wake_from_d3(pdev, pf->wol_en);
  10562. pci_set_power_state(pdev, PCI_D3hot);
  10563. }
  10564. }
  10565. #ifdef CONFIG_PM
  10566. /**
  10567. * i40e_suspend - PCI callback for moving to D3
  10568. * @pdev: PCI device information struct
  10569. **/
  10570. static int i40e_suspend(struct pci_dev *pdev, pm_message_t state)
  10571. {
  10572. struct i40e_pf *pf = pci_get_drvdata(pdev);
  10573. struct i40e_hw *hw = &pf->hw;
  10574. int retval = 0;
  10575. set_bit(__I40E_SUSPENDED, pf->state);
  10576. set_bit(__I40E_DOWN, pf->state);
  10577. if (pf->wol_en && (pf->flags & I40E_FLAG_WOL_MC_MAGIC_PKT_WAKE))
  10578. i40e_enable_mc_magic_wake(pf);
  10579. i40e_prep_for_reset(pf, false);
  10580. wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
  10581. wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
  10582. i40e_stop_misc_vector(pf);
  10583. if (pf->msix_entries) {
  10584. synchronize_irq(pf->msix_entries[0].vector);
  10585. free_irq(pf->msix_entries[0].vector, pf);
  10586. }
  10587. retval = pci_save_state(pdev);
  10588. if (retval)
  10589. return retval;
  10590. pci_wake_from_d3(pdev, pf->wol_en);
  10591. pci_set_power_state(pdev, PCI_D3hot);
  10592. return retval;
  10593. }
  10594. /**
  10595. * i40e_resume - PCI callback for waking up from D3
  10596. * @pdev: PCI device information struct
  10597. **/
  10598. static int i40e_resume(struct pci_dev *pdev)
  10599. {
  10600. struct i40e_pf *pf = pci_get_drvdata(pdev);
  10601. u32 err;
  10602. pci_set_power_state(pdev, PCI_D0);
  10603. pci_restore_state(pdev);
  10604. /* pci_restore_state() clears dev->state_saves, so
  10605. * call pci_save_state() again to restore it.
  10606. */
  10607. pci_save_state(pdev);
  10608. err = pci_enable_device_mem(pdev);
  10609. if (err) {
  10610. dev_err(&pdev->dev, "Cannot enable PCI device from suspend\n");
  10611. return err;
  10612. }
  10613. pci_set_master(pdev);
  10614. /* no wakeup events while running */
  10615. pci_wake_from_d3(pdev, false);
  10616. /* handling the reset will rebuild the device state */
  10617. if (test_and_clear_bit(__I40E_SUSPENDED, pf->state)) {
  10618. clear_bit(__I40E_DOWN, pf->state);
  10619. if (pf->msix_entries) {
  10620. err = request_irq(pf->msix_entries[0].vector,
  10621. i40e_intr, 0, pf->int_name, pf);
  10622. if (err) {
  10623. dev_err(&pf->pdev->dev,
  10624. "request_irq for %s failed: %d\n",
  10625. pf->int_name, err);
  10626. }
  10627. }
  10628. i40e_reset_and_rebuild(pf, false, false);
  10629. }
  10630. return 0;
  10631. }
  10632. #endif
  10633. static const struct pci_error_handlers i40e_err_handler = {
  10634. .error_detected = i40e_pci_error_detected,
  10635. .slot_reset = i40e_pci_error_slot_reset,
  10636. .resume = i40e_pci_error_resume,
  10637. };
  10638. static struct pci_driver i40e_driver = {
  10639. .name = i40e_driver_name,
  10640. .id_table = i40e_pci_tbl,
  10641. .probe = i40e_probe,
  10642. .remove = i40e_remove,
  10643. #ifdef CONFIG_PM
  10644. .suspend = i40e_suspend,
  10645. .resume = i40e_resume,
  10646. #endif
  10647. .shutdown = i40e_shutdown,
  10648. .err_handler = &i40e_err_handler,
  10649. .sriov_configure = i40e_pci_sriov_configure,
  10650. };
  10651. /**
  10652. * i40e_init_module - Driver registration routine
  10653. *
  10654. * i40e_init_module is the first routine called when the driver is
  10655. * loaded. All it does is register with the PCI subsystem.
  10656. **/
  10657. static int __init i40e_init_module(void)
  10658. {
  10659. pr_info("%s: %s - version %s\n", i40e_driver_name,
  10660. i40e_driver_string, i40e_driver_version_str);
  10661. pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
  10662. /* There is no need to throttle the number of active tasks because
  10663. * each device limits its own task using a state bit for scheduling
  10664. * the service task, and the device tasks do not interfere with each
  10665. * other, so we don't set a max task limit. We must set WQ_MEM_RECLAIM
  10666. * since we need to be able to guarantee forward progress even under
  10667. * memory pressure.
  10668. */
  10669. i40e_wq = alloc_workqueue("%s", WQ_MEM_RECLAIM, 0, i40e_driver_name);
  10670. if (!i40e_wq) {
  10671. pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
  10672. return -ENOMEM;
  10673. }
  10674. i40e_dbg_init();
  10675. return pci_register_driver(&i40e_driver);
  10676. }
  10677. module_init(i40e_init_module);
  10678. /**
  10679. * i40e_exit_module - Driver exit cleanup routine
  10680. *
  10681. * i40e_exit_module is called just before the driver is removed
  10682. * from memory.
  10683. **/
  10684. static void __exit i40e_exit_module(void)
  10685. {
  10686. pci_unregister_driver(&i40e_driver);
  10687. destroy_workqueue(i40e_wq);
  10688. i40e_dbg_exit();
  10689. }
  10690. module_exit(i40e_exit_module);