i40e_main.c 213 KB

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