i40e_main.c 217 KB

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