igb_main.c 254 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright(c) 2007 - 2018 Intel Corporation. */
  3. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  4. #include <linux/module.h>
  5. #include <linux/types.h>
  6. #include <linux/init.h>
  7. #include <linux/bitops.h>
  8. #include <linux/vmalloc.h>
  9. #include <linux/pagemap.h>
  10. #include <linux/netdevice.h>
  11. #include <linux/ipv6.h>
  12. #include <linux/slab.h>
  13. #include <net/checksum.h>
  14. #include <net/ip6_checksum.h>
  15. #include <net/pkt_sched.h>
  16. #include <net/pkt_cls.h>
  17. #include <linux/net_tstamp.h>
  18. #include <linux/mii.h>
  19. #include <linux/ethtool.h>
  20. #include <linux/if.h>
  21. #include <linux/if_vlan.h>
  22. #include <linux/pci.h>
  23. #include <linux/pci-aspm.h>
  24. #include <linux/delay.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/ip.h>
  27. #include <linux/tcp.h>
  28. #include <linux/sctp.h>
  29. #include <linux/if_ether.h>
  30. #include <linux/aer.h>
  31. #include <linux/prefetch.h>
  32. #include <linux/pm_runtime.h>
  33. #include <linux/etherdevice.h>
  34. #ifdef CONFIG_IGB_DCA
  35. #include <linux/dca.h>
  36. #endif
  37. #include <linux/i2c.h>
  38. #include "igb.h"
  39. #define MAJ 5
  40. #define MIN 4
  41. #define BUILD 0
  42. #define DRV_VERSION __stringify(MAJ) "." __stringify(MIN) "." \
  43. __stringify(BUILD) "-k"
  44. enum queue_mode {
  45. QUEUE_MODE_STRICT_PRIORITY,
  46. QUEUE_MODE_STREAM_RESERVATION,
  47. };
  48. enum tx_queue_prio {
  49. TX_QUEUE_PRIO_HIGH,
  50. TX_QUEUE_PRIO_LOW,
  51. };
  52. char igb_driver_name[] = "igb";
  53. char igb_driver_version[] = DRV_VERSION;
  54. static const char igb_driver_string[] =
  55. "Intel(R) Gigabit Ethernet Network Driver";
  56. static const char igb_copyright[] =
  57. "Copyright (c) 2007-2014 Intel Corporation.";
  58. static const struct e1000_info *igb_info_tbl[] = {
  59. [board_82575] = &e1000_82575_info,
  60. };
  61. static const struct pci_device_id igb_pci_tbl[] = {
  62. { PCI_VDEVICE(INTEL, E1000_DEV_ID_I354_BACKPLANE_1GBPS) },
  63. { PCI_VDEVICE(INTEL, E1000_DEV_ID_I354_SGMII) },
  64. { PCI_VDEVICE(INTEL, E1000_DEV_ID_I354_BACKPLANE_2_5GBPS) },
  65. { PCI_VDEVICE(INTEL, E1000_DEV_ID_I211_COPPER), board_82575 },
  66. { PCI_VDEVICE(INTEL, E1000_DEV_ID_I210_COPPER), board_82575 },
  67. { PCI_VDEVICE(INTEL, E1000_DEV_ID_I210_FIBER), board_82575 },
  68. { PCI_VDEVICE(INTEL, E1000_DEV_ID_I210_SERDES), board_82575 },
  69. { PCI_VDEVICE(INTEL, E1000_DEV_ID_I210_SGMII), board_82575 },
  70. { PCI_VDEVICE(INTEL, E1000_DEV_ID_I210_COPPER_FLASHLESS), board_82575 },
  71. { PCI_VDEVICE(INTEL, E1000_DEV_ID_I210_SERDES_FLASHLESS), board_82575 },
  72. { PCI_VDEVICE(INTEL, E1000_DEV_ID_I350_COPPER), board_82575 },
  73. { PCI_VDEVICE(INTEL, E1000_DEV_ID_I350_FIBER), board_82575 },
  74. { PCI_VDEVICE(INTEL, E1000_DEV_ID_I350_SERDES), board_82575 },
  75. { PCI_VDEVICE(INTEL, E1000_DEV_ID_I350_SGMII), board_82575 },
  76. { PCI_VDEVICE(INTEL, E1000_DEV_ID_82580_COPPER), board_82575 },
  77. { PCI_VDEVICE(INTEL, E1000_DEV_ID_82580_FIBER), board_82575 },
  78. { PCI_VDEVICE(INTEL, E1000_DEV_ID_82580_QUAD_FIBER), board_82575 },
  79. { PCI_VDEVICE(INTEL, E1000_DEV_ID_82580_SERDES), board_82575 },
  80. { PCI_VDEVICE(INTEL, E1000_DEV_ID_82580_SGMII), board_82575 },
  81. { PCI_VDEVICE(INTEL, E1000_DEV_ID_82580_COPPER_DUAL), board_82575 },
  82. { PCI_VDEVICE(INTEL, E1000_DEV_ID_DH89XXCC_SGMII), board_82575 },
  83. { PCI_VDEVICE(INTEL, E1000_DEV_ID_DH89XXCC_SERDES), board_82575 },
  84. { PCI_VDEVICE(INTEL, E1000_DEV_ID_DH89XXCC_BACKPLANE), board_82575 },
  85. { PCI_VDEVICE(INTEL, E1000_DEV_ID_DH89XXCC_SFP), board_82575 },
  86. { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576), board_82575 },
  87. { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_NS), board_82575 },
  88. { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_NS_SERDES), board_82575 },
  89. { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_FIBER), board_82575 },
  90. { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_SERDES), board_82575 },
  91. { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_SERDES_QUAD), board_82575 },
  92. { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_QUAD_COPPER_ET2), board_82575 },
  93. { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_QUAD_COPPER), board_82575 },
  94. { PCI_VDEVICE(INTEL, E1000_DEV_ID_82575EB_COPPER), board_82575 },
  95. { PCI_VDEVICE(INTEL, E1000_DEV_ID_82575EB_FIBER_SERDES), board_82575 },
  96. { PCI_VDEVICE(INTEL, E1000_DEV_ID_82575GB_QUAD_COPPER), board_82575 },
  97. /* required last entry */
  98. {0, }
  99. };
  100. MODULE_DEVICE_TABLE(pci, igb_pci_tbl);
  101. static int igb_setup_all_tx_resources(struct igb_adapter *);
  102. static int igb_setup_all_rx_resources(struct igb_adapter *);
  103. static void igb_free_all_tx_resources(struct igb_adapter *);
  104. static void igb_free_all_rx_resources(struct igb_adapter *);
  105. static void igb_setup_mrqc(struct igb_adapter *);
  106. static int igb_probe(struct pci_dev *, const struct pci_device_id *);
  107. static void igb_remove(struct pci_dev *pdev);
  108. static int igb_sw_init(struct igb_adapter *);
  109. int igb_open(struct net_device *);
  110. int igb_close(struct net_device *);
  111. static void igb_configure(struct igb_adapter *);
  112. static void igb_configure_tx(struct igb_adapter *);
  113. static void igb_configure_rx(struct igb_adapter *);
  114. static void igb_clean_all_tx_rings(struct igb_adapter *);
  115. static void igb_clean_all_rx_rings(struct igb_adapter *);
  116. static void igb_clean_tx_ring(struct igb_ring *);
  117. static void igb_clean_rx_ring(struct igb_ring *);
  118. static void igb_set_rx_mode(struct net_device *);
  119. static void igb_update_phy_info(struct timer_list *);
  120. static void igb_watchdog(struct timer_list *);
  121. static void igb_watchdog_task(struct work_struct *);
  122. static netdev_tx_t igb_xmit_frame(struct sk_buff *skb, struct net_device *);
  123. static void igb_get_stats64(struct net_device *dev,
  124. struct rtnl_link_stats64 *stats);
  125. static int igb_change_mtu(struct net_device *, int);
  126. static int igb_set_mac(struct net_device *, void *);
  127. static void igb_set_uta(struct igb_adapter *adapter, bool set);
  128. static irqreturn_t igb_intr(int irq, void *);
  129. static irqreturn_t igb_intr_msi(int irq, void *);
  130. static irqreturn_t igb_msix_other(int irq, void *);
  131. static irqreturn_t igb_msix_ring(int irq, void *);
  132. #ifdef CONFIG_IGB_DCA
  133. static void igb_update_dca(struct igb_q_vector *);
  134. static void igb_setup_dca(struct igb_adapter *);
  135. #endif /* CONFIG_IGB_DCA */
  136. static int igb_poll(struct napi_struct *, int);
  137. static bool igb_clean_tx_irq(struct igb_q_vector *, int);
  138. static int igb_clean_rx_irq(struct igb_q_vector *, int);
  139. static int igb_ioctl(struct net_device *, struct ifreq *, int cmd);
  140. static void igb_tx_timeout(struct net_device *);
  141. static void igb_reset_task(struct work_struct *);
  142. static void igb_vlan_mode(struct net_device *netdev,
  143. netdev_features_t features);
  144. static int igb_vlan_rx_add_vid(struct net_device *, __be16, u16);
  145. static int igb_vlan_rx_kill_vid(struct net_device *, __be16, u16);
  146. static void igb_restore_vlan(struct igb_adapter *);
  147. static void igb_rar_set_index(struct igb_adapter *, u32);
  148. static void igb_ping_all_vfs(struct igb_adapter *);
  149. static void igb_msg_task(struct igb_adapter *);
  150. static void igb_vmm_control(struct igb_adapter *);
  151. static int igb_set_vf_mac(struct igb_adapter *, int, unsigned char *);
  152. static void igb_flush_mac_table(struct igb_adapter *);
  153. static int igb_available_rars(struct igb_adapter *, u8);
  154. static void igb_set_default_mac_filter(struct igb_adapter *);
  155. static int igb_uc_sync(struct net_device *, const unsigned char *);
  156. static int igb_uc_unsync(struct net_device *, const unsigned char *);
  157. static void igb_restore_vf_multicasts(struct igb_adapter *adapter);
  158. static int igb_ndo_set_vf_mac(struct net_device *netdev, int vf, u8 *mac);
  159. static int igb_ndo_set_vf_vlan(struct net_device *netdev,
  160. int vf, u16 vlan, u8 qos, __be16 vlan_proto);
  161. static int igb_ndo_set_vf_bw(struct net_device *, int, int, int);
  162. static int igb_ndo_set_vf_spoofchk(struct net_device *netdev, int vf,
  163. bool setting);
  164. static int igb_ndo_set_vf_trust(struct net_device *netdev, int vf,
  165. bool setting);
  166. static int igb_ndo_get_vf_config(struct net_device *netdev, int vf,
  167. struct ifla_vf_info *ivi);
  168. static void igb_check_vf_rate_limit(struct igb_adapter *);
  169. static void igb_nfc_filter_exit(struct igb_adapter *adapter);
  170. static void igb_nfc_filter_restore(struct igb_adapter *adapter);
  171. #ifdef CONFIG_PCI_IOV
  172. static int igb_vf_configure(struct igb_adapter *adapter, int vf);
  173. static int igb_pci_enable_sriov(struct pci_dev *dev, int num_vfs);
  174. static int igb_disable_sriov(struct pci_dev *dev);
  175. static int igb_pci_disable_sriov(struct pci_dev *dev);
  176. #endif
  177. static int igb_suspend(struct device *);
  178. static int igb_resume(struct device *);
  179. static int igb_runtime_suspend(struct device *dev);
  180. static int igb_runtime_resume(struct device *dev);
  181. static int igb_runtime_idle(struct device *dev);
  182. static const struct dev_pm_ops igb_pm_ops = {
  183. SET_SYSTEM_SLEEP_PM_OPS(igb_suspend, igb_resume)
  184. SET_RUNTIME_PM_OPS(igb_runtime_suspend, igb_runtime_resume,
  185. igb_runtime_idle)
  186. };
  187. static void igb_shutdown(struct pci_dev *);
  188. static int igb_pci_sriov_configure(struct pci_dev *dev, int num_vfs);
  189. #ifdef CONFIG_IGB_DCA
  190. static int igb_notify_dca(struct notifier_block *, unsigned long, void *);
  191. static struct notifier_block dca_notifier = {
  192. .notifier_call = igb_notify_dca,
  193. .next = NULL,
  194. .priority = 0
  195. };
  196. #endif
  197. #ifdef CONFIG_NET_POLL_CONTROLLER
  198. /* for netdump / net console */
  199. static void igb_netpoll(struct net_device *);
  200. #endif
  201. #ifdef CONFIG_PCI_IOV
  202. static unsigned int max_vfs;
  203. module_param(max_vfs, uint, 0);
  204. MODULE_PARM_DESC(max_vfs, "Maximum number of virtual functions to allocate per physical function");
  205. #endif /* CONFIG_PCI_IOV */
  206. static pci_ers_result_t igb_io_error_detected(struct pci_dev *,
  207. pci_channel_state_t);
  208. static pci_ers_result_t igb_io_slot_reset(struct pci_dev *);
  209. static void igb_io_resume(struct pci_dev *);
  210. static const struct pci_error_handlers igb_err_handler = {
  211. .error_detected = igb_io_error_detected,
  212. .slot_reset = igb_io_slot_reset,
  213. .resume = igb_io_resume,
  214. };
  215. static void igb_init_dmac(struct igb_adapter *adapter, u32 pba);
  216. static struct pci_driver igb_driver = {
  217. .name = igb_driver_name,
  218. .id_table = igb_pci_tbl,
  219. .probe = igb_probe,
  220. .remove = igb_remove,
  221. #ifdef CONFIG_PM
  222. .driver.pm = &igb_pm_ops,
  223. #endif
  224. .shutdown = igb_shutdown,
  225. .sriov_configure = igb_pci_sriov_configure,
  226. .err_handler = &igb_err_handler
  227. };
  228. MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
  229. MODULE_DESCRIPTION("Intel(R) Gigabit Ethernet Network Driver");
  230. MODULE_LICENSE("GPL");
  231. MODULE_VERSION(DRV_VERSION);
  232. #define DEFAULT_MSG_ENABLE (NETIF_MSG_DRV|NETIF_MSG_PROBE|NETIF_MSG_LINK)
  233. static int debug = -1;
  234. module_param(debug, int, 0);
  235. MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
  236. struct igb_reg_info {
  237. u32 ofs;
  238. char *name;
  239. };
  240. static const struct igb_reg_info igb_reg_info_tbl[] = {
  241. /* General Registers */
  242. {E1000_CTRL, "CTRL"},
  243. {E1000_STATUS, "STATUS"},
  244. {E1000_CTRL_EXT, "CTRL_EXT"},
  245. /* Interrupt Registers */
  246. {E1000_ICR, "ICR"},
  247. /* RX Registers */
  248. {E1000_RCTL, "RCTL"},
  249. {E1000_RDLEN(0), "RDLEN"},
  250. {E1000_RDH(0), "RDH"},
  251. {E1000_RDT(0), "RDT"},
  252. {E1000_RXDCTL(0), "RXDCTL"},
  253. {E1000_RDBAL(0), "RDBAL"},
  254. {E1000_RDBAH(0), "RDBAH"},
  255. /* TX Registers */
  256. {E1000_TCTL, "TCTL"},
  257. {E1000_TDBAL(0), "TDBAL"},
  258. {E1000_TDBAH(0), "TDBAH"},
  259. {E1000_TDLEN(0), "TDLEN"},
  260. {E1000_TDH(0), "TDH"},
  261. {E1000_TDT(0), "TDT"},
  262. {E1000_TXDCTL(0), "TXDCTL"},
  263. {E1000_TDFH, "TDFH"},
  264. {E1000_TDFT, "TDFT"},
  265. {E1000_TDFHS, "TDFHS"},
  266. {E1000_TDFPC, "TDFPC"},
  267. /* List Terminator */
  268. {}
  269. };
  270. /* igb_regdump - register printout routine */
  271. static void igb_regdump(struct e1000_hw *hw, struct igb_reg_info *reginfo)
  272. {
  273. int n = 0;
  274. char rname[16];
  275. u32 regs[8];
  276. switch (reginfo->ofs) {
  277. case E1000_RDLEN(0):
  278. for (n = 0; n < 4; n++)
  279. regs[n] = rd32(E1000_RDLEN(n));
  280. break;
  281. case E1000_RDH(0):
  282. for (n = 0; n < 4; n++)
  283. regs[n] = rd32(E1000_RDH(n));
  284. break;
  285. case E1000_RDT(0):
  286. for (n = 0; n < 4; n++)
  287. regs[n] = rd32(E1000_RDT(n));
  288. break;
  289. case E1000_RXDCTL(0):
  290. for (n = 0; n < 4; n++)
  291. regs[n] = rd32(E1000_RXDCTL(n));
  292. break;
  293. case E1000_RDBAL(0):
  294. for (n = 0; n < 4; n++)
  295. regs[n] = rd32(E1000_RDBAL(n));
  296. break;
  297. case E1000_RDBAH(0):
  298. for (n = 0; n < 4; n++)
  299. regs[n] = rd32(E1000_RDBAH(n));
  300. break;
  301. case E1000_TDBAL(0):
  302. for (n = 0; n < 4; n++)
  303. regs[n] = rd32(E1000_RDBAL(n));
  304. break;
  305. case E1000_TDBAH(0):
  306. for (n = 0; n < 4; n++)
  307. regs[n] = rd32(E1000_TDBAH(n));
  308. break;
  309. case E1000_TDLEN(0):
  310. for (n = 0; n < 4; n++)
  311. regs[n] = rd32(E1000_TDLEN(n));
  312. break;
  313. case E1000_TDH(0):
  314. for (n = 0; n < 4; n++)
  315. regs[n] = rd32(E1000_TDH(n));
  316. break;
  317. case E1000_TDT(0):
  318. for (n = 0; n < 4; n++)
  319. regs[n] = rd32(E1000_TDT(n));
  320. break;
  321. case E1000_TXDCTL(0):
  322. for (n = 0; n < 4; n++)
  323. regs[n] = rd32(E1000_TXDCTL(n));
  324. break;
  325. default:
  326. pr_info("%-15s %08x\n", reginfo->name, rd32(reginfo->ofs));
  327. return;
  328. }
  329. snprintf(rname, 16, "%s%s", reginfo->name, "[0-3]");
  330. pr_info("%-15s %08x %08x %08x %08x\n", rname, regs[0], regs[1],
  331. regs[2], regs[3]);
  332. }
  333. /* igb_dump - Print registers, Tx-rings and Rx-rings */
  334. static void igb_dump(struct igb_adapter *adapter)
  335. {
  336. struct net_device *netdev = adapter->netdev;
  337. struct e1000_hw *hw = &adapter->hw;
  338. struct igb_reg_info *reginfo;
  339. struct igb_ring *tx_ring;
  340. union e1000_adv_tx_desc *tx_desc;
  341. struct my_u0 { u64 a; u64 b; } *u0;
  342. struct igb_ring *rx_ring;
  343. union e1000_adv_rx_desc *rx_desc;
  344. u32 staterr;
  345. u16 i, n;
  346. if (!netif_msg_hw(adapter))
  347. return;
  348. /* Print netdevice Info */
  349. if (netdev) {
  350. dev_info(&adapter->pdev->dev, "Net device Info\n");
  351. pr_info("Device Name state trans_start\n");
  352. pr_info("%-15s %016lX %016lX\n", netdev->name,
  353. netdev->state, dev_trans_start(netdev));
  354. }
  355. /* Print Registers */
  356. dev_info(&adapter->pdev->dev, "Register Dump\n");
  357. pr_info(" Register Name Value\n");
  358. for (reginfo = (struct igb_reg_info *)igb_reg_info_tbl;
  359. reginfo->name; reginfo++) {
  360. igb_regdump(hw, reginfo);
  361. }
  362. /* Print TX Ring Summary */
  363. if (!netdev || !netif_running(netdev))
  364. goto exit;
  365. dev_info(&adapter->pdev->dev, "TX Rings Summary\n");
  366. pr_info("Queue [NTU] [NTC] [bi(ntc)->dma ] leng ntw timestamp\n");
  367. for (n = 0; n < adapter->num_tx_queues; n++) {
  368. struct igb_tx_buffer *buffer_info;
  369. tx_ring = adapter->tx_ring[n];
  370. buffer_info = &tx_ring->tx_buffer_info[tx_ring->next_to_clean];
  371. pr_info(" %5d %5X %5X %016llX %04X %p %016llX\n",
  372. n, tx_ring->next_to_use, tx_ring->next_to_clean,
  373. (u64)dma_unmap_addr(buffer_info, dma),
  374. dma_unmap_len(buffer_info, len),
  375. buffer_info->next_to_watch,
  376. (u64)buffer_info->time_stamp);
  377. }
  378. /* Print TX Rings */
  379. if (!netif_msg_tx_done(adapter))
  380. goto rx_ring_summary;
  381. dev_info(&adapter->pdev->dev, "TX Rings Dump\n");
  382. /* Transmit Descriptor Formats
  383. *
  384. * Advanced Transmit Descriptor
  385. * +--------------------------------------------------------------+
  386. * 0 | Buffer Address [63:0] |
  387. * +--------------------------------------------------------------+
  388. * 8 | PAYLEN | PORTS |CC|IDX | STA | DCMD |DTYP|MAC|RSV| DTALEN |
  389. * +--------------------------------------------------------------+
  390. * 63 46 45 40 39 38 36 35 32 31 24 15 0
  391. */
  392. for (n = 0; n < adapter->num_tx_queues; n++) {
  393. tx_ring = adapter->tx_ring[n];
  394. pr_info("------------------------------------\n");
  395. pr_info("TX QUEUE INDEX = %d\n", tx_ring->queue_index);
  396. pr_info("------------------------------------\n");
  397. pr_info("T [desc] [address 63:0 ] [PlPOCIStDDM Ln] [bi->dma ] leng ntw timestamp bi->skb\n");
  398. for (i = 0; tx_ring->desc && (i < tx_ring->count); i++) {
  399. const char *next_desc;
  400. struct igb_tx_buffer *buffer_info;
  401. tx_desc = IGB_TX_DESC(tx_ring, i);
  402. buffer_info = &tx_ring->tx_buffer_info[i];
  403. u0 = (struct my_u0 *)tx_desc;
  404. if (i == tx_ring->next_to_use &&
  405. i == tx_ring->next_to_clean)
  406. next_desc = " NTC/U";
  407. else if (i == tx_ring->next_to_use)
  408. next_desc = " NTU";
  409. else if (i == tx_ring->next_to_clean)
  410. next_desc = " NTC";
  411. else
  412. next_desc = "";
  413. pr_info("T [0x%03X] %016llX %016llX %016llX %04X %p %016llX %p%s\n",
  414. i, le64_to_cpu(u0->a),
  415. le64_to_cpu(u0->b),
  416. (u64)dma_unmap_addr(buffer_info, dma),
  417. dma_unmap_len(buffer_info, len),
  418. buffer_info->next_to_watch,
  419. (u64)buffer_info->time_stamp,
  420. buffer_info->skb, next_desc);
  421. if (netif_msg_pktdata(adapter) && buffer_info->skb)
  422. print_hex_dump(KERN_INFO, "",
  423. DUMP_PREFIX_ADDRESS,
  424. 16, 1, buffer_info->skb->data,
  425. dma_unmap_len(buffer_info, len),
  426. true);
  427. }
  428. }
  429. /* Print RX Rings Summary */
  430. rx_ring_summary:
  431. dev_info(&adapter->pdev->dev, "RX Rings Summary\n");
  432. pr_info("Queue [NTU] [NTC]\n");
  433. for (n = 0; n < adapter->num_rx_queues; n++) {
  434. rx_ring = adapter->rx_ring[n];
  435. pr_info(" %5d %5X %5X\n",
  436. n, rx_ring->next_to_use, rx_ring->next_to_clean);
  437. }
  438. /* Print RX Rings */
  439. if (!netif_msg_rx_status(adapter))
  440. goto exit;
  441. dev_info(&adapter->pdev->dev, "RX Rings Dump\n");
  442. /* Advanced Receive Descriptor (Read) Format
  443. * 63 1 0
  444. * +-----------------------------------------------------+
  445. * 0 | Packet Buffer Address [63:1] |A0/NSE|
  446. * +----------------------------------------------+------+
  447. * 8 | Header Buffer Address [63:1] | DD |
  448. * +-----------------------------------------------------+
  449. *
  450. *
  451. * Advanced Receive Descriptor (Write-Back) Format
  452. *
  453. * 63 48 47 32 31 30 21 20 17 16 4 3 0
  454. * +------------------------------------------------------+
  455. * 0 | Packet IP |SPH| HDR_LEN | RSV|Packet| RSS |
  456. * | Checksum Ident | | | | Type | Type |
  457. * +------------------------------------------------------+
  458. * 8 | VLAN Tag | Length | Extended Error | Extended Status |
  459. * +------------------------------------------------------+
  460. * 63 48 47 32 31 20 19 0
  461. */
  462. for (n = 0; n < adapter->num_rx_queues; n++) {
  463. rx_ring = adapter->rx_ring[n];
  464. pr_info("------------------------------------\n");
  465. pr_info("RX QUEUE INDEX = %d\n", rx_ring->queue_index);
  466. pr_info("------------------------------------\n");
  467. pr_info("R [desc] [ PktBuf A0] [ HeadBuf DD] [bi->dma ] [bi->skb] <-- Adv Rx Read format\n");
  468. pr_info("RWB[desc] [PcsmIpSHl PtRs] [vl er S cks ln] ---------------- [bi->skb] <-- Adv Rx Write-Back format\n");
  469. for (i = 0; i < rx_ring->count; i++) {
  470. const char *next_desc;
  471. struct igb_rx_buffer *buffer_info;
  472. buffer_info = &rx_ring->rx_buffer_info[i];
  473. rx_desc = IGB_RX_DESC(rx_ring, i);
  474. u0 = (struct my_u0 *)rx_desc;
  475. staterr = le32_to_cpu(rx_desc->wb.upper.status_error);
  476. if (i == rx_ring->next_to_use)
  477. next_desc = " NTU";
  478. else if (i == rx_ring->next_to_clean)
  479. next_desc = " NTC";
  480. else
  481. next_desc = "";
  482. if (staterr & E1000_RXD_STAT_DD) {
  483. /* Descriptor Done */
  484. pr_info("%s[0x%03X] %016llX %016llX ---------------- %s\n",
  485. "RWB", i,
  486. le64_to_cpu(u0->a),
  487. le64_to_cpu(u0->b),
  488. next_desc);
  489. } else {
  490. pr_info("%s[0x%03X] %016llX %016llX %016llX %s\n",
  491. "R ", i,
  492. le64_to_cpu(u0->a),
  493. le64_to_cpu(u0->b),
  494. (u64)buffer_info->dma,
  495. next_desc);
  496. if (netif_msg_pktdata(adapter) &&
  497. buffer_info->dma && buffer_info->page) {
  498. print_hex_dump(KERN_INFO, "",
  499. DUMP_PREFIX_ADDRESS,
  500. 16, 1,
  501. page_address(buffer_info->page) +
  502. buffer_info->page_offset,
  503. igb_rx_bufsz(rx_ring), true);
  504. }
  505. }
  506. }
  507. }
  508. exit:
  509. return;
  510. }
  511. /**
  512. * igb_get_i2c_data - Reads the I2C SDA data bit
  513. * @hw: pointer to hardware structure
  514. * @i2cctl: Current value of I2CCTL register
  515. *
  516. * Returns the I2C data bit value
  517. **/
  518. static int igb_get_i2c_data(void *data)
  519. {
  520. struct igb_adapter *adapter = (struct igb_adapter *)data;
  521. struct e1000_hw *hw = &adapter->hw;
  522. s32 i2cctl = rd32(E1000_I2CPARAMS);
  523. return !!(i2cctl & E1000_I2C_DATA_IN);
  524. }
  525. /**
  526. * igb_set_i2c_data - Sets the I2C data bit
  527. * @data: pointer to hardware structure
  528. * @state: I2C data value (0 or 1) to set
  529. *
  530. * Sets the I2C data bit
  531. **/
  532. static void igb_set_i2c_data(void *data, int state)
  533. {
  534. struct igb_adapter *adapter = (struct igb_adapter *)data;
  535. struct e1000_hw *hw = &adapter->hw;
  536. s32 i2cctl = rd32(E1000_I2CPARAMS);
  537. if (state)
  538. i2cctl |= E1000_I2C_DATA_OUT;
  539. else
  540. i2cctl &= ~E1000_I2C_DATA_OUT;
  541. i2cctl &= ~E1000_I2C_DATA_OE_N;
  542. i2cctl |= E1000_I2C_CLK_OE_N;
  543. wr32(E1000_I2CPARAMS, i2cctl);
  544. wrfl();
  545. }
  546. /**
  547. * igb_set_i2c_clk - Sets the I2C SCL clock
  548. * @data: pointer to hardware structure
  549. * @state: state to set clock
  550. *
  551. * Sets the I2C clock line to state
  552. **/
  553. static void igb_set_i2c_clk(void *data, int state)
  554. {
  555. struct igb_adapter *adapter = (struct igb_adapter *)data;
  556. struct e1000_hw *hw = &adapter->hw;
  557. s32 i2cctl = rd32(E1000_I2CPARAMS);
  558. if (state) {
  559. i2cctl |= E1000_I2C_CLK_OUT;
  560. i2cctl &= ~E1000_I2C_CLK_OE_N;
  561. } else {
  562. i2cctl &= ~E1000_I2C_CLK_OUT;
  563. i2cctl &= ~E1000_I2C_CLK_OE_N;
  564. }
  565. wr32(E1000_I2CPARAMS, i2cctl);
  566. wrfl();
  567. }
  568. /**
  569. * igb_get_i2c_clk - Gets the I2C SCL clock state
  570. * @data: pointer to hardware structure
  571. *
  572. * Gets the I2C clock state
  573. **/
  574. static int igb_get_i2c_clk(void *data)
  575. {
  576. struct igb_adapter *adapter = (struct igb_adapter *)data;
  577. struct e1000_hw *hw = &adapter->hw;
  578. s32 i2cctl = rd32(E1000_I2CPARAMS);
  579. return !!(i2cctl & E1000_I2C_CLK_IN);
  580. }
  581. static const struct i2c_algo_bit_data igb_i2c_algo = {
  582. .setsda = igb_set_i2c_data,
  583. .setscl = igb_set_i2c_clk,
  584. .getsda = igb_get_i2c_data,
  585. .getscl = igb_get_i2c_clk,
  586. .udelay = 5,
  587. .timeout = 20,
  588. };
  589. /**
  590. * igb_get_hw_dev - return device
  591. * @hw: pointer to hardware structure
  592. *
  593. * used by hardware layer to print debugging information
  594. **/
  595. struct net_device *igb_get_hw_dev(struct e1000_hw *hw)
  596. {
  597. struct igb_adapter *adapter = hw->back;
  598. return adapter->netdev;
  599. }
  600. /**
  601. * igb_init_module - Driver Registration Routine
  602. *
  603. * igb_init_module is the first routine called when the driver is
  604. * loaded. All it does is register with the PCI subsystem.
  605. **/
  606. static int __init igb_init_module(void)
  607. {
  608. int ret;
  609. pr_info("%s - version %s\n",
  610. igb_driver_string, igb_driver_version);
  611. pr_info("%s\n", igb_copyright);
  612. #ifdef CONFIG_IGB_DCA
  613. dca_register_notify(&dca_notifier);
  614. #endif
  615. ret = pci_register_driver(&igb_driver);
  616. return ret;
  617. }
  618. module_init(igb_init_module);
  619. /**
  620. * igb_exit_module - Driver Exit Cleanup Routine
  621. *
  622. * igb_exit_module is called just before the driver is removed
  623. * from memory.
  624. **/
  625. static void __exit igb_exit_module(void)
  626. {
  627. #ifdef CONFIG_IGB_DCA
  628. dca_unregister_notify(&dca_notifier);
  629. #endif
  630. pci_unregister_driver(&igb_driver);
  631. }
  632. module_exit(igb_exit_module);
  633. #define Q_IDX_82576(i) (((i & 0x1) << 3) + (i >> 1))
  634. /**
  635. * igb_cache_ring_register - Descriptor ring to register mapping
  636. * @adapter: board private structure to initialize
  637. *
  638. * Once we know the feature-set enabled for the device, we'll cache
  639. * the register offset the descriptor ring is assigned to.
  640. **/
  641. static void igb_cache_ring_register(struct igb_adapter *adapter)
  642. {
  643. int i = 0, j = 0;
  644. u32 rbase_offset = adapter->vfs_allocated_count;
  645. switch (adapter->hw.mac.type) {
  646. case e1000_82576:
  647. /* The queues are allocated for virtualization such that VF 0
  648. * is allocated queues 0 and 8, VF 1 queues 1 and 9, etc.
  649. * In order to avoid collision we start at the first free queue
  650. * and continue consuming queues in the same sequence
  651. */
  652. if (adapter->vfs_allocated_count) {
  653. for (; i < adapter->rss_queues; i++)
  654. adapter->rx_ring[i]->reg_idx = rbase_offset +
  655. Q_IDX_82576(i);
  656. }
  657. /* Fall through */
  658. case e1000_82575:
  659. case e1000_82580:
  660. case e1000_i350:
  661. case e1000_i354:
  662. case e1000_i210:
  663. case e1000_i211:
  664. /* Fall through */
  665. default:
  666. for (; i < adapter->num_rx_queues; i++)
  667. adapter->rx_ring[i]->reg_idx = rbase_offset + i;
  668. for (; j < adapter->num_tx_queues; j++)
  669. adapter->tx_ring[j]->reg_idx = rbase_offset + j;
  670. break;
  671. }
  672. }
  673. u32 igb_rd32(struct e1000_hw *hw, u32 reg)
  674. {
  675. struct igb_adapter *igb = container_of(hw, struct igb_adapter, hw);
  676. u8 __iomem *hw_addr = READ_ONCE(hw->hw_addr);
  677. u32 value = 0;
  678. if (E1000_REMOVED(hw_addr))
  679. return ~value;
  680. value = readl(&hw_addr[reg]);
  681. /* reads should not return all F's */
  682. if (!(~value) && (!reg || !(~readl(hw_addr)))) {
  683. struct net_device *netdev = igb->netdev;
  684. hw->hw_addr = NULL;
  685. netdev_err(netdev, "PCIe link lost\n");
  686. }
  687. return value;
  688. }
  689. /**
  690. * igb_write_ivar - configure ivar for given MSI-X vector
  691. * @hw: pointer to the HW structure
  692. * @msix_vector: vector number we are allocating to a given ring
  693. * @index: row index of IVAR register to write within IVAR table
  694. * @offset: column offset of in IVAR, should be multiple of 8
  695. *
  696. * This function is intended to handle the writing of the IVAR register
  697. * for adapters 82576 and newer. The IVAR table consists of 2 columns,
  698. * each containing an cause allocation for an Rx and Tx ring, and a
  699. * variable number of rows depending on the number of queues supported.
  700. **/
  701. static void igb_write_ivar(struct e1000_hw *hw, int msix_vector,
  702. int index, int offset)
  703. {
  704. u32 ivar = array_rd32(E1000_IVAR0, index);
  705. /* clear any bits that are currently set */
  706. ivar &= ~((u32)0xFF << offset);
  707. /* write vector and valid bit */
  708. ivar |= (msix_vector | E1000_IVAR_VALID) << offset;
  709. array_wr32(E1000_IVAR0, index, ivar);
  710. }
  711. #define IGB_N0_QUEUE -1
  712. static void igb_assign_vector(struct igb_q_vector *q_vector, int msix_vector)
  713. {
  714. struct igb_adapter *adapter = q_vector->adapter;
  715. struct e1000_hw *hw = &adapter->hw;
  716. int rx_queue = IGB_N0_QUEUE;
  717. int tx_queue = IGB_N0_QUEUE;
  718. u32 msixbm = 0;
  719. if (q_vector->rx.ring)
  720. rx_queue = q_vector->rx.ring->reg_idx;
  721. if (q_vector->tx.ring)
  722. tx_queue = q_vector->tx.ring->reg_idx;
  723. switch (hw->mac.type) {
  724. case e1000_82575:
  725. /* The 82575 assigns vectors using a bitmask, which matches the
  726. * bitmask for the EICR/EIMS/EIMC registers. To assign one
  727. * or more queues to a vector, we write the appropriate bits
  728. * into the MSIXBM register for that vector.
  729. */
  730. if (rx_queue > IGB_N0_QUEUE)
  731. msixbm = E1000_EICR_RX_QUEUE0 << rx_queue;
  732. if (tx_queue > IGB_N0_QUEUE)
  733. msixbm |= E1000_EICR_TX_QUEUE0 << tx_queue;
  734. if (!(adapter->flags & IGB_FLAG_HAS_MSIX) && msix_vector == 0)
  735. msixbm |= E1000_EIMS_OTHER;
  736. array_wr32(E1000_MSIXBM(0), msix_vector, msixbm);
  737. q_vector->eims_value = msixbm;
  738. break;
  739. case e1000_82576:
  740. /* 82576 uses a table that essentially consists of 2 columns
  741. * with 8 rows. The ordering is column-major so we use the
  742. * lower 3 bits as the row index, and the 4th bit as the
  743. * column offset.
  744. */
  745. if (rx_queue > IGB_N0_QUEUE)
  746. igb_write_ivar(hw, msix_vector,
  747. rx_queue & 0x7,
  748. (rx_queue & 0x8) << 1);
  749. if (tx_queue > IGB_N0_QUEUE)
  750. igb_write_ivar(hw, msix_vector,
  751. tx_queue & 0x7,
  752. ((tx_queue & 0x8) << 1) + 8);
  753. q_vector->eims_value = BIT(msix_vector);
  754. break;
  755. case e1000_82580:
  756. case e1000_i350:
  757. case e1000_i354:
  758. case e1000_i210:
  759. case e1000_i211:
  760. /* On 82580 and newer adapters the scheme is similar to 82576
  761. * however instead of ordering column-major we have things
  762. * ordered row-major. So we traverse the table by using
  763. * bit 0 as the column offset, and the remaining bits as the
  764. * row index.
  765. */
  766. if (rx_queue > IGB_N0_QUEUE)
  767. igb_write_ivar(hw, msix_vector,
  768. rx_queue >> 1,
  769. (rx_queue & 0x1) << 4);
  770. if (tx_queue > IGB_N0_QUEUE)
  771. igb_write_ivar(hw, msix_vector,
  772. tx_queue >> 1,
  773. ((tx_queue & 0x1) << 4) + 8);
  774. q_vector->eims_value = BIT(msix_vector);
  775. break;
  776. default:
  777. BUG();
  778. break;
  779. }
  780. /* add q_vector eims value to global eims_enable_mask */
  781. adapter->eims_enable_mask |= q_vector->eims_value;
  782. /* configure q_vector to set itr on first interrupt */
  783. q_vector->set_itr = 1;
  784. }
  785. /**
  786. * igb_configure_msix - Configure MSI-X hardware
  787. * @adapter: board private structure to initialize
  788. *
  789. * igb_configure_msix sets up the hardware to properly
  790. * generate MSI-X interrupts.
  791. **/
  792. static void igb_configure_msix(struct igb_adapter *adapter)
  793. {
  794. u32 tmp;
  795. int i, vector = 0;
  796. struct e1000_hw *hw = &adapter->hw;
  797. adapter->eims_enable_mask = 0;
  798. /* set vector for other causes, i.e. link changes */
  799. switch (hw->mac.type) {
  800. case e1000_82575:
  801. tmp = rd32(E1000_CTRL_EXT);
  802. /* enable MSI-X PBA support*/
  803. tmp |= E1000_CTRL_EXT_PBA_CLR;
  804. /* Auto-Mask interrupts upon ICR read. */
  805. tmp |= E1000_CTRL_EXT_EIAME;
  806. tmp |= E1000_CTRL_EXT_IRCA;
  807. wr32(E1000_CTRL_EXT, tmp);
  808. /* enable msix_other interrupt */
  809. array_wr32(E1000_MSIXBM(0), vector++, E1000_EIMS_OTHER);
  810. adapter->eims_other = E1000_EIMS_OTHER;
  811. break;
  812. case e1000_82576:
  813. case e1000_82580:
  814. case e1000_i350:
  815. case e1000_i354:
  816. case e1000_i210:
  817. case e1000_i211:
  818. /* Turn on MSI-X capability first, or our settings
  819. * won't stick. And it will take days to debug.
  820. */
  821. wr32(E1000_GPIE, E1000_GPIE_MSIX_MODE |
  822. E1000_GPIE_PBA | E1000_GPIE_EIAME |
  823. E1000_GPIE_NSICR);
  824. /* enable msix_other interrupt */
  825. adapter->eims_other = BIT(vector);
  826. tmp = (vector++ | E1000_IVAR_VALID) << 8;
  827. wr32(E1000_IVAR_MISC, tmp);
  828. break;
  829. default:
  830. /* do nothing, since nothing else supports MSI-X */
  831. break;
  832. } /* switch (hw->mac.type) */
  833. adapter->eims_enable_mask |= adapter->eims_other;
  834. for (i = 0; i < adapter->num_q_vectors; i++)
  835. igb_assign_vector(adapter->q_vector[i], vector++);
  836. wrfl();
  837. }
  838. /**
  839. * igb_request_msix - Initialize MSI-X interrupts
  840. * @adapter: board private structure to initialize
  841. *
  842. * igb_request_msix allocates MSI-X vectors and requests interrupts from the
  843. * kernel.
  844. **/
  845. static int igb_request_msix(struct igb_adapter *adapter)
  846. {
  847. struct net_device *netdev = adapter->netdev;
  848. int i, err = 0, vector = 0, free_vector = 0;
  849. err = request_irq(adapter->msix_entries[vector].vector,
  850. igb_msix_other, 0, netdev->name, adapter);
  851. if (err)
  852. goto err_out;
  853. for (i = 0; i < adapter->num_q_vectors; i++) {
  854. struct igb_q_vector *q_vector = adapter->q_vector[i];
  855. vector++;
  856. q_vector->itr_register = adapter->io_addr + E1000_EITR(vector);
  857. if (q_vector->rx.ring && q_vector->tx.ring)
  858. sprintf(q_vector->name, "%s-TxRx-%u", netdev->name,
  859. q_vector->rx.ring->queue_index);
  860. else if (q_vector->tx.ring)
  861. sprintf(q_vector->name, "%s-tx-%u", netdev->name,
  862. q_vector->tx.ring->queue_index);
  863. else if (q_vector->rx.ring)
  864. sprintf(q_vector->name, "%s-rx-%u", netdev->name,
  865. q_vector->rx.ring->queue_index);
  866. else
  867. sprintf(q_vector->name, "%s-unused", netdev->name);
  868. err = request_irq(adapter->msix_entries[vector].vector,
  869. igb_msix_ring, 0, q_vector->name,
  870. q_vector);
  871. if (err)
  872. goto err_free;
  873. }
  874. igb_configure_msix(adapter);
  875. return 0;
  876. err_free:
  877. /* free already assigned IRQs */
  878. free_irq(adapter->msix_entries[free_vector++].vector, adapter);
  879. vector--;
  880. for (i = 0; i < vector; i++) {
  881. free_irq(adapter->msix_entries[free_vector++].vector,
  882. adapter->q_vector[i]);
  883. }
  884. err_out:
  885. return err;
  886. }
  887. /**
  888. * igb_free_q_vector - Free memory allocated for specific interrupt vector
  889. * @adapter: board private structure to initialize
  890. * @v_idx: Index of vector to be freed
  891. *
  892. * This function frees the memory allocated to the q_vector.
  893. **/
  894. static void igb_free_q_vector(struct igb_adapter *adapter, int v_idx)
  895. {
  896. struct igb_q_vector *q_vector = adapter->q_vector[v_idx];
  897. adapter->q_vector[v_idx] = NULL;
  898. /* igb_get_stats64() might access the rings on this vector,
  899. * we must wait a grace period before freeing it.
  900. */
  901. if (q_vector)
  902. kfree_rcu(q_vector, rcu);
  903. }
  904. /**
  905. * igb_reset_q_vector - Reset config for interrupt vector
  906. * @adapter: board private structure to initialize
  907. * @v_idx: Index of vector to be reset
  908. *
  909. * If NAPI is enabled it will delete any references to the
  910. * NAPI struct. This is preparation for igb_free_q_vector.
  911. **/
  912. static void igb_reset_q_vector(struct igb_adapter *adapter, int v_idx)
  913. {
  914. struct igb_q_vector *q_vector = adapter->q_vector[v_idx];
  915. /* Coming from igb_set_interrupt_capability, the vectors are not yet
  916. * allocated. So, q_vector is NULL so we should stop here.
  917. */
  918. if (!q_vector)
  919. return;
  920. if (q_vector->tx.ring)
  921. adapter->tx_ring[q_vector->tx.ring->queue_index] = NULL;
  922. if (q_vector->rx.ring)
  923. adapter->rx_ring[q_vector->rx.ring->queue_index] = NULL;
  924. netif_napi_del(&q_vector->napi);
  925. }
  926. static void igb_reset_interrupt_capability(struct igb_adapter *adapter)
  927. {
  928. int v_idx = adapter->num_q_vectors;
  929. if (adapter->flags & IGB_FLAG_HAS_MSIX)
  930. pci_disable_msix(adapter->pdev);
  931. else if (adapter->flags & IGB_FLAG_HAS_MSI)
  932. pci_disable_msi(adapter->pdev);
  933. while (v_idx--)
  934. igb_reset_q_vector(adapter, v_idx);
  935. }
  936. /**
  937. * igb_free_q_vectors - Free memory allocated for interrupt vectors
  938. * @adapter: board private structure to initialize
  939. *
  940. * This function frees the memory allocated to the q_vectors. In addition if
  941. * NAPI is enabled it will delete any references to the NAPI struct prior
  942. * to freeing the q_vector.
  943. **/
  944. static void igb_free_q_vectors(struct igb_adapter *adapter)
  945. {
  946. int v_idx = adapter->num_q_vectors;
  947. adapter->num_tx_queues = 0;
  948. adapter->num_rx_queues = 0;
  949. adapter->num_q_vectors = 0;
  950. while (v_idx--) {
  951. igb_reset_q_vector(adapter, v_idx);
  952. igb_free_q_vector(adapter, v_idx);
  953. }
  954. }
  955. /**
  956. * igb_clear_interrupt_scheme - reset the device to a state of no interrupts
  957. * @adapter: board private structure to initialize
  958. *
  959. * This function resets the device so that it has 0 Rx queues, Tx queues, and
  960. * MSI-X interrupts allocated.
  961. */
  962. static void igb_clear_interrupt_scheme(struct igb_adapter *adapter)
  963. {
  964. igb_free_q_vectors(adapter);
  965. igb_reset_interrupt_capability(adapter);
  966. }
  967. /**
  968. * igb_set_interrupt_capability - set MSI or MSI-X if supported
  969. * @adapter: board private structure to initialize
  970. * @msix: boolean value of MSIX capability
  971. *
  972. * Attempt to configure interrupts using the best available
  973. * capabilities of the hardware and kernel.
  974. **/
  975. static void igb_set_interrupt_capability(struct igb_adapter *adapter, bool msix)
  976. {
  977. int err;
  978. int numvecs, i;
  979. if (!msix)
  980. goto msi_only;
  981. adapter->flags |= IGB_FLAG_HAS_MSIX;
  982. /* Number of supported queues. */
  983. adapter->num_rx_queues = adapter->rss_queues;
  984. if (adapter->vfs_allocated_count)
  985. adapter->num_tx_queues = 1;
  986. else
  987. adapter->num_tx_queues = adapter->rss_queues;
  988. /* start with one vector for every Rx queue */
  989. numvecs = adapter->num_rx_queues;
  990. /* if Tx handler is separate add 1 for every Tx queue */
  991. if (!(adapter->flags & IGB_FLAG_QUEUE_PAIRS))
  992. numvecs += adapter->num_tx_queues;
  993. /* store the number of vectors reserved for queues */
  994. adapter->num_q_vectors = numvecs;
  995. /* add 1 vector for link status interrupts */
  996. numvecs++;
  997. for (i = 0; i < numvecs; i++)
  998. adapter->msix_entries[i].entry = i;
  999. err = pci_enable_msix_range(adapter->pdev,
  1000. adapter->msix_entries,
  1001. numvecs,
  1002. numvecs);
  1003. if (err > 0)
  1004. return;
  1005. igb_reset_interrupt_capability(adapter);
  1006. /* If we can't do MSI-X, try MSI */
  1007. msi_only:
  1008. adapter->flags &= ~IGB_FLAG_HAS_MSIX;
  1009. #ifdef CONFIG_PCI_IOV
  1010. /* disable SR-IOV for non MSI-X configurations */
  1011. if (adapter->vf_data) {
  1012. struct e1000_hw *hw = &adapter->hw;
  1013. /* disable iov and allow time for transactions to clear */
  1014. pci_disable_sriov(adapter->pdev);
  1015. msleep(500);
  1016. kfree(adapter->vf_mac_list);
  1017. adapter->vf_mac_list = NULL;
  1018. kfree(adapter->vf_data);
  1019. adapter->vf_data = NULL;
  1020. wr32(E1000_IOVCTL, E1000_IOVCTL_REUSE_VFQ);
  1021. wrfl();
  1022. msleep(100);
  1023. dev_info(&adapter->pdev->dev, "IOV Disabled\n");
  1024. }
  1025. #endif
  1026. adapter->vfs_allocated_count = 0;
  1027. adapter->rss_queues = 1;
  1028. adapter->flags |= IGB_FLAG_QUEUE_PAIRS;
  1029. adapter->num_rx_queues = 1;
  1030. adapter->num_tx_queues = 1;
  1031. adapter->num_q_vectors = 1;
  1032. if (!pci_enable_msi(adapter->pdev))
  1033. adapter->flags |= IGB_FLAG_HAS_MSI;
  1034. }
  1035. static void igb_add_ring(struct igb_ring *ring,
  1036. struct igb_ring_container *head)
  1037. {
  1038. head->ring = ring;
  1039. head->count++;
  1040. }
  1041. /**
  1042. * igb_alloc_q_vector - Allocate memory for a single interrupt vector
  1043. * @adapter: board private structure to initialize
  1044. * @v_count: q_vectors allocated on adapter, used for ring interleaving
  1045. * @v_idx: index of vector in adapter struct
  1046. * @txr_count: total number of Tx rings to allocate
  1047. * @txr_idx: index of first Tx ring to allocate
  1048. * @rxr_count: total number of Rx rings to allocate
  1049. * @rxr_idx: index of first Rx ring to allocate
  1050. *
  1051. * We allocate one q_vector. If allocation fails we return -ENOMEM.
  1052. **/
  1053. static int igb_alloc_q_vector(struct igb_adapter *adapter,
  1054. int v_count, int v_idx,
  1055. int txr_count, int txr_idx,
  1056. int rxr_count, int rxr_idx)
  1057. {
  1058. struct igb_q_vector *q_vector;
  1059. struct igb_ring *ring;
  1060. int ring_count, size;
  1061. /* igb only supports 1 Tx and/or 1 Rx queue per vector */
  1062. if (txr_count > 1 || rxr_count > 1)
  1063. return -ENOMEM;
  1064. ring_count = txr_count + rxr_count;
  1065. size = sizeof(struct igb_q_vector) +
  1066. (sizeof(struct igb_ring) * ring_count);
  1067. /* allocate q_vector and rings */
  1068. q_vector = adapter->q_vector[v_idx];
  1069. if (!q_vector) {
  1070. q_vector = kzalloc(size, GFP_KERNEL);
  1071. } else if (size > ksize(q_vector)) {
  1072. kfree_rcu(q_vector, rcu);
  1073. q_vector = kzalloc(size, GFP_KERNEL);
  1074. } else {
  1075. memset(q_vector, 0, size);
  1076. }
  1077. if (!q_vector)
  1078. return -ENOMEM;
  1079. /* initialize NAPI */
  1080. netif_napi_add(adapter->netdev, &q_vector->napi,
  1081. igb_poll, 64);
  1082. /* tie q_vector and adapter together */
  1083. adapter->q_vector[v_idx] = q_vector;
  1084. q_vector->adapter = adapter;
  1085. /* initialize work limits */
  1086. q_vector->tx.work_limit = adapter->tx_work_limit;
  1087. /* initialize ITR configuration */
  1088. q_vector->itr_register = adapter->io_addr + E1000_EITR(0);
  1089. q_vector->itr_val = IGB_START_ITR;
  1090. /* initialize pointer to rings */
  1091. ring = q_vector->ring;
  1092. /* intialize ITR */
  1093. if (rxr_count) {
  1094. /* rx or rx/tx vector */
  1095. if (!adapter->rx_itr_setting || adapter->rx_itr_setting > 3)
  1096. q_vector->itr_val = adapter->rx_itr_setting;
  1097. } else {
  1098. /* tx only vector */
  1099. if (!adapter->tx_itr_setting || adapter->tx_itr_setting > 3)
  1100. q_vector->itr_val = adapter->tx_itr_setting;
  1101. }
  1102. if (txr_count) {
  1103. /* assign generic ring traits */
  1104. ring->dev = &adapter->pdev->dev;
  1105. ring->netdev = adapter->netdev;
  1106. /* configure backlink on ring */
  1107. ring->q_vector = q_vector;
  1108. /* update q_vector Tx values */
  1109. igb_add_ring(ring, &q_vector->tx);
  1110. /* For 82575, context index must be unique per ring. */
  1111. if (adapter->hw.mac.type == e1000_82575)
  1112. set_bit(IGB_RING_FLAG_TX_CTX_IDX, &ring->flags);
  1113. /* apply Tx specific ring traits */
  1114. ring->count = adapter->tx_ring_count;
  1115. ring->queue_index = txr_idx;
  1116. ring->cbs_enable = false;
  1117. ring->idleslope = 0;
  1118. ring->sendslope = 0;
  1119. ring->hicredit = 0;
  1120. ring->locredit = 0;
  1121. u64_stats_init(&ring->tx_syncp);
  1122. u64_stats_init(&ring->tx_syncp2);
  1123. /* assign ring to adapter */
  1124. adapter->tx_ring[txr_idx] = ring;
  1125. /* push pointer to next ring */
  1126. ring++;
  1127. }
  1128. if (rxr_count) {
  1129. /* assign generic ring traits */
  1130. ring->dev = &adapter->pdev->dev;
  1131. ring->netdev = adapter->netdev;
  1132. /* configure backlink on ring */
  1133. ring->q_vector = q_vector;
  1134. /* update q_vector Rx values */
  1135. igb_add_ring(ring, &q_vector->rx);
  1136. /* set flag indicating ring supports SCTP checksum offload */
  1137. if (adapter->hw.mac.type >= e1000_82576)
  1138. set_bit(IGB_RING_FLAG_RX_SCTP_CSUM, &ring->flags);
  1139. /* On i350, i354, i210, and i211, loopback VLAN packets
  1140. * have the tag byte-swapped.
  1141. */
  1142. if (adapter->hw.mac.type >= e1000_i350)
  1143. set_bit(IGB_RING_FLAG_RX_LB_VLAN_BSWAP, &ring->flags);
  1144. /* apply Rx specific ring traits */
  1145. ring->count = adapter->rx_ring_count;
  1146. ring->queue_index = rxr_idx;
  1147. u64_stats_init(&ring->rx_syncp);
  1148. /* assign ring to adapter */
  1149. adapter->rx_ring[rxr_idx] = ring;
  1150. }
  1151. return 0;
  1152. }
  1153. /**
  1154. * igb_alloc_q_vectors - Allocate memory for interrupt vectors
  1155. * @adapter: board private structure to initialize
  1156. *
  1157. * We allocate one q_vector per queue interrupt. If allocation fails we
  1158. * return -ENOMEM.
  1159. **/
  1160. static int igb_alloc_q_vectors(struct igb_adapter *adapter)
  1161. {
  1162. int q_vectors = adapter->num_q_vectors;
  1163. int rxr_remaining = adapter->num_rx_queues;
  1164. int txr_remaining = adapter->num_tx_queues;
  1165. int rxr_idx = 0, txr_idx = 0, v_idx = 0;
  1166. int err;
  1167. if (q_vectors >= (rxr_remaining + txr_remaining)) {
  1168. for (; rxr_remaining; v_idx++) {
  1169. err = igb_alloc_q_vector(adapter, q_vectors, v_idx,
  1170. 0, 0, 1, rxr_idx);
  1171. if (err)
  1172. goto err_out;
  1173. /* update counts and index */
  1174. rxr_remaining--;
  1175. rxr_idx++;
  1176. }
  1177. }
  1178. for (; v_idx < q_vectors; v_idx++) {
  1179. int rqpv = DIV_ROUND_UP(rxr_remaining, q_vectors - v_idx);
  1180. int tqpv = DIV_ROUND_UP(txr_remaining, q_vectors - v_idx);
  1181. err = igb_alloc_q_vector(adapter, q_vectors, v_idx,
  1182. tqpv, txr_idx, rqpv, rxr_idx);
  1183. if (err)
  1184. goto err_out;
  1185. /* update counts and index */
  1186. rxr_remaining -= rqpv;
  1187. txr_remaining -= tqpv;
  1188. rxr_idx++;
  1189. txr_idx++;
  1190. }
  1191. return 0;
  1192. err_out:
  1193. adapter->num_tx_queues = 0;
  1194. adapter->num_rx_queues = 0;
  1195. adapter->num_q_vectors = 0;
  1196. while (v_idx--)
  1197. igb_free_q_vector(adapter, v_idx);
  1198. return -ENOMEM;
  1199. }
  1200. /**
  1201. * igb_init_interrupt_scheme - initialize interrupts, allocate queues/vectors
  1202. * @adapter: board private structure to initialize
  1203. * @msix: boolean value of MSIX capability
  1204. *
  1205. * This function initializes the interrupts and allocates all of the queues.
  1206. **/
  1207. static int igb_init_interrupt_scheme(struct igb_adapter *adapter, bool msix)
  1208. {
  1209. struct pci_dev *pdev = adapter->pdev;
  1210. int err;
  1211. igb_set_interrupt_capability(adapter, msix);
  1212. err = igb_alloc_q_vectors(adapter);
  1213. if (err) {
  1214. dev_err(&pdev->dev, "Unable to allocate memory for vectors\n");
  1215. goto err_alloc_q_vectors;
  1216. }
  1217. igb_cache_ring_register(adapter);
  1218. return 0;
  1219. err_alloc_q_vectors:
  1220. igb_reset_interrupt_capability(adapter);
  1221. return err;
  1222. }
  1223. /**
  1224. * igb_request_irq - initialize interrupts
  1225. * @adapter: board private structure to initialize
  1226. *
  1227. * Attempts to configure interrupts using the best available
  1228. * capabilities of the hardware and kernel.
  1229. **/
  1230. static int igb_request_irq(struct igb_adapter *adapter)
  1231. {
  1232. struct net_device *netdev = adapter->netdev;
  1233. struct pci_dev *pdev = adapter->pdev;
  1234. int err = 0;
  1235. if (adapter->flags & IGB_FLAG_HAS_MSIX) {
  1236. err = igb_request_msix(adapter);
  1237. if (!err)
  1238. goto request_done;
  1239. /* fall back to MSI */
  1240. igb_free_all_tx_resources(adapter);
  1241. igb_free_all_rx_resources(adapter);
  1242. igb_clear_interrupt_scheme(adapter);
  1243. err = igb_init_interrupt_scheme(adapter, false);
  1244. if (err)
  1245. goto request_done;
  1246. igb_setup_all_tx_resources(adapter);
  1247. igb_setup_all_rx_resources(adapter);
  1248. igb_configure(adapter);
  1249. }
  1250. igb_assign_vector(adapter->q_vector[0], 0);
  1251. if (adapter->flags & IGB_FLAG_HAS_MSI) {
  1252. err = request_irq(pdev->irq, igb_intr_msi, 0,
  1253. netdev->name, adapter);
  1254. if (!err)
  1255. goto request_done;
  1256. /* fall back to legacy interrupts */
  1257. igb_reset_interrupt_capability(adapter);
  1258. adapter->flags &= ~IGB_FLAG_HAS_MSI;
  1259. }
  1260. err = request_irq(pdev->irq, igb_intr, IRQF_SHARED,
  1261. netdev->name, adapter);
  1262. if (err)
  1263. dev_err(&pdev->dev, "Error %d getting interrupt\n",
  1264. err);
  1265. request_done:
  1266. return err;
  1267. }
  1268. static void igb_free_irq(struct igb_adapter *adapter)
  1269. {
  1270. if (adapter->flags & IGB_FLAG_HAS_MSIX) {
  1271. int vector = 0, i;
  1272. free_irq(adapter->msix_entries[vector++].vector, adapter);
  1273. for (i = 0; i < adapter->num_q_vectors; i++)
  1274. free_irq(adapter->msix_entries[vector++].vector,
  1275. adapter->q_vector[i]);
  1276. } else {
  1277. free_irq(adapter->pdev->irq, adapter);
  1278. }
  1279. }
  1280. /**
  1281. * igb_irq_disable - Mask off interrupt generation on the NIC
  1282. * @adapter: board private structure
  1283. **/
  1284. static void igb_irq_disable(struct igb_adapter *adapter)
  1285. {
  1286. struct e1000_hw *hw = &adapter->hw;
  1287. /* we need to be careful when disabling interrupts. The VFs are also
  1288. * mapped into these registers and so clearing the bits can cause
  1289. * issues on the VF drivers so we only need to clear what we set
  1290. */
  1291. if (adapter->flags & IGB_FLAG_HAS_MSIX) {
  1292. u32 regval = rd32(E1000_EIAM);
  1293. wr32(E1000_EIAM, regval & ~adapter->eims_enable_mask);
  1294. wr32(E1000_EIMC, adapter->eims_enable_mask);
  1295. regval = rd32(E1000_EIAC);
  1296. wr32(E1000_EIAC, regval & ~adapter->eims_enable_mask);
  1297. }
  1298. wr32(E1000_IAM, 0);
  1299. wr32(E1000_IMC, ~0);
  1300. wrfl();
  1301. if (adapter->flags & IGB_FLAG_HAS_MSIX) {
  1302. int i;
  1303. for (i = 0; i < adapter->num_q_vectors; i++)
  1304. synchronize_irq(adapter->msix_entries[i].vector);
  1305. } else {
  1306. synchronize_irq(adapter->pdev->irq);
  1307. }
  1308. }
  1309. /**
  1310. * igb_irq_enable - Enable default interrupt generation settings
  1311. * @adapter: board private structure
  1312. **/
  1313. static void igb_irq_enable(struct igb_adapter *adapter)
  1314. {
  1315. struct e1000_hw *hw = &adapter->hw;
  1316. if (adapter->flags & IGB_FLAG_HAS_MSIX) {
  1317. u32 ims = E1000_IMS_LSC | E1000_IMS_DOUTSYNC | E1000_IMS_DRSTA;
  1318. u32 regval = rd32(E1000_EIAC);
  1319. wr32(E1000_EIAC, regval | adapter->eims_enable_mask);
  1320. regval = rd32(E1000_EIAM);
  1321. wr32(E1000_EIAM, regval | adapter->eims_enable_mask);
  1322. wr32(E1000_EIMS, adapter->eims_enable_mask);
  1323. if (adapter->vfs_allocated_count) {
  1324. wr32(E1000_MBVFIMR, 0xFF);
  1325. ims |= E1000_IMS_VMMB;
  1326. }
  1327. wr32(E1000_IMS, ims);
  1328. } else {
  1329. wr32(E1000_IMS, IMS_ENABLE_MASK |
  1330. E1000_IMS_DRSTA);
  1331. wr32(E1000_IAM, IMS_ENABLE_MASK |
  1332. E1000_IMS_DRSTA);
  1333. }
  1334. }
  1335. static void igb_update_mng_vlan(struct igb_adapter *adapter)
  1336. {
  1337. struct e1000_hw *hw = &adapter->hw;
  1338. u16 pf_id = adapter->vfs_allocated_count;
  1339. u16 vid = adapter->hw.mng_cookie.vlan_id;
  1340. u16 old_vid = adapter->mng_vlan_id;
  1341. if (hw->mng_cookie.status & E1000_MNG_DHCP_COOKIE_STATUS_VLAN) {
  1342. /* add VID to filter table */
  1343. igb_vfta_set(hw, vid, pf_id, true, true);
  1344. adapter->mng_vlan_id = vid;
  1345. } else {
  1346. adapter->mng_vlan_id = IGB_MNG_VLAN_NONE;
  1347. }
  1348. if ((old_vid != (u16)IGB_MNG_VLAN_NONE) &&
  1349. (vid != old_vid) &&
  1350. !test_bit(old_vid, adapter->active_vlans)) {
  1351. /* remove VID from filter table */
  1352. igb_vfta_set(hw, vid, pf_id, false, true);
  1353. }
  1354. }
  1355. /**
  1356. * igb_release_hw_control - release control of the h/w to f/w
  1357. * @adapter: address of board private structure
  1358. *
  1359. * igb_release_hw_control resets CTRL_EXT:DRV_LOAD bit.
  1360. * For ASF and Pass Through versions of f/w this means that the
  1361. * driver is no longer loaded.
  1362. **/
  1363. static void igb_release_hw_control(struct igb_adapter *adapter)
  1364. {
  1365. struct e1000_hw *hw = &adapter->hw;
  1366. u32 ctrl_ext;
  1367. /* Let firmware take over control of h/w */
  1368. ctrl_ext = rd32(E1000_CTRL_EXT);
  1369. wr32(E1000_CTRL_EXT,
  1370. ctrl_ext & ~E1000_CTRL_EXT_DRV_LOAD);
  1371. }
  1372. /**
  1373. * igb_get_hw_control - get control of the h/w from f/w
  1374. * @adapter: address of board private structure
  1375. *
  1376. * igb_get_hw_control sets CTRL_EXT:DRV_LOAD bit.
  1377. * For ASF and Pass Through versions of f/w this means that
  1378. * the driver is loaded.
  1379. **/
  1380. static void igb_get_hw_control(struct igb_adapter *adapter)
  1381. {
  1382. struct e1000_hw *hw = &adapter->hw;
  1383. u32 ctrl_ext;
  1384. /* Let firmware know the driver has taken over */
  1385. ctrl_ext = rd32(E1000_CTRL_EXT);
  1386. wr32(E1000_CTRL_EXT,
  1387. ctrl_ext | E1000_CTRL_EXT_DRV_LOAD);
  1388. }
  1389. static void enable_fqtss(struct igb_adapter *adapter, bool enable)
  1390. {
  1391. struct net_device *netdev = adapter->netdev;
  1392. struct e1000_hw *hw = &adapter->hw;
  1393. WARN_ON(hw->mac.type != e1000_i210);
  1394. if (enable)
  1395. adapter->flags |= IGB_FLAG_FQTSS;
  1396. else
  1397. adapter->flags &= ~IGB_FLAG_FQTSS;
  1398. if (netif_running(netdev))
  1399. schedule_work(&adapter->reset_task);
  1400. }
  1401. static bool is_fqtss_enabled(struct igb_adapter *adapter)
  1402. {
  1403. return (adapter->flags & IGB_FLAG_FQTSS) ? true : false;
  1404. }
  1405. static void set_tx_desc_fetch_prio(struct e1000_hw *hw, int queue,
  1406. enum tx_queue_prio prio)
  1407. {
  1408. u32 val;
  1409. WARN_ON(hw->mac.type != e1000_i210);
  1410. WARN_ON(queue < 0 || queue > 4);
  1411. val = rd32(E1000_I210_TXDCTL(queue));
  1412. if (prio == TX_QUEUE_PRIO_HIGH)
  1413. val |= E1000_TXDCTL_PRIORITY;
  1414. else
  1415. val &= ~E1000_TXDCTL_PRIORITY;
  1416. wr32(E1000_I210_TXDCTL(queue), val);
  1417. }
  1418. static void set_queue_mode(struct e1000_hw *hw, int queue, enum queue_mode mode)
  1419. {
  1420. u32 val;
  1421. WARN_ON(hw->mac.type != e1000_i210);
  1422. WARN_ON(queue < 0 || queue > 1);
  1423. val = rd32(E1000_I210_TQAVCC(queue));
  1424. if (mode == QUEUE_MODE_STREAM_RESERVATION)
  1425. val |= E1000_TQAVCC_QUEUEMODE;
  1426. else
  1427. val &= ~E1000_TQAVCC_QUEUEMODE;
  1428. wr32(E1000_I210_TQAVCC(queue), val);
  1429. }
  1430. /**
  1431. * igb_configure_cbs - Configure Credit-Based Shaper (CBS)
  1432. * @adapter: pointer to adapter struct
  1433. * @queue: queue number
  1434. * @enable: true = enable CBS, false = disable CBS
  1435. * @idleslope: idleSlope in kbps
  1436. * @sendslope: sendSlope in kbps
  1437. * @hicredit: hiCredit in bytes
  1438. * @locredit: loCredit in bytes
  1439. *
  1440. * Configure CBS for a given hardware queue. When disabling, idleslope,
  1441. * sendslope, hicredit, locredit arguments are ignored. Returns 0 if
  1442. * success. Negative otherwise.
  1443. **/
  1444. static void igb_configure_cbs(struct igb_adapter *adapter, int queue,
  1445. bool enable, int idleslope, int sendslope,
  1446. int hicredit, int locredit)
  1447. {
  1448. struct net_device *netdev = adapter->netdev;
  1449. struct e1000_hw *hw = &adapter->hw;
  1450. u32 tqavcc;
  1451. u16 value;
  1452. WARN_ON(hw->mac.type != e1000_i210);
  1453. WARN_ON(queue < 0 || queue > 1);
  1454. if (enable || queue == 0) {
  1455. /* i210 does not allow the queue 0 to be in the Strict
  1456. * Priority mode while the Qav mode is enabled, so,
  1457. * instead of disabling strict priority mode, we give
  1458. * queue 0 the maximum of credits possible.
  1459. *
  1460. * See section 8.12.19 of the i210 datasheet, "Note:
  1461. * Queue0 QueueMode must be set to 1b when
  1462. * TransmitMode is set to Qav."
  1463. */
  1464. if (queue == 0 && !enable) {
  1465. /* max "linkspeed" idleslope in kbps */
  1466. idleslope = 1000000;
  1467. hicredit = ETH_FRAME_LEN;
  1468. }
  1469. set_tx_desc_fetch_prio(hw, queue, TX_QUEUE_PRIO_HIGH);
  1470. set_queue_mode(hw, queue, QUEUE_MODE_STREAM_RESERVATION);
  1471. /* According to i210 datasheet section 7.2.7.7, we should set
  1472. * the 'idleSlope' field from TQAVCC register following the
  1473. * equation:
  1474. *
  1475. * For 100 Mbps link speed:
  1476. *
  1477. * value = BW * 0x7735 * 0.2 (E1)
  1478. *
  1479. * For 1000Mbps link speed:
  1480. *
  1481. * value = BW * 0x7735 * 2 (E2)
  1482. *
  1483. * E1 and E2 can be merged into one equation as shown below.
  1484. * Note that 'link-speed' is in Mbps.
  1485. *
  1486. * value = BW * 0x7735 * 2 * link-speed
  1487. * -------------- (E3)
  1488. * 1000
  1489. *
  1490. * 'BW' is the percentage bandwidth out of full link speed
  1491. * which can be found with the following equation. Note that
  1492. * idleSlope here is the parameter from this function which
  1493. * is in kbps.
  1494. *
  1495. * BW = idleSlope
  1496. * ----------------- (E4)
  1497. * link-speed * 1000
  1498. *
  1499. * That said, we can come up with a generic equation to
  1500. * calculate the value we should set it TQAVCC register by
  1501. * replacing 'BW' in E3 by E4. The resulting equation is:
  1502. *
  1503. * value = idleSlope * 0x7735 * 2 * link-speed
  1504. * ----------------- -------------- (E5)
  1505. * link-speed * 1000 1000
  1506. *
  1507. * 'link-speed' is present in both sides of the fraction so
  1508. * it is canceled out. The final equation is the following:
  1509. *
  1510. * value = idleSlope * 61034
  1511. * ----------------- (E6)
  1512. * 1000000
  1513. *
  1514. * NOTE: For i210, given the above, we can see that idleslope
  1515. * is represented in 16.38431 kbps units by the value at
  1516. * the TQAVCC register (1Gbps / 61034), which reduces
  1517. * the granularity for idleslope increments.
  1518. * For instance, if you want to configure a 2576kbps
  1519. * idleslope, the value to be written on the register
  1520. * would have to be 157.23. If rounded down, you end
  1521. * up with less bandwidth available than originally
  1522. * required (~2572 kbps). If rounded up, you end up
  1523. * with a higher bandwidth (~2589 kbps). Below the
  1524. * approach we take is to always round up the
  1525. * calculated value, so the resulting bandwidth might
  1526. * be slightly higher for some configurations.
  1527. */
  1528. value = DIV_ROUND_UP_ULL(idleslope * 61034ULL, 1000000);
  1529. tqavcc = rd32(E1000_I210_TQAVCC(queue));
  1530. tqavcc &= ~E1000_TQAVCC_IDLESLOPE_MASK;
  1531. tqavcc |= value;
  1532. wr32(E1000_I210_TQAVCC(queue), tqavcc);
  1533. wr32(E1000_I210_TQAVHC(queue), 0x80000000 + hicredit * 0x7735);
  1534. } else {
  1535. set_tx_desc_fetch_prio(hw, queue, TX_QUEUE_PRIO_LOW);
  1536. set_queue_mode(hw, queue, QUEUE_MODE_STRICT_PRIORITY);
  1537. /* Set idleSlope to zero. */
  1538. tqavcc = rd32(E1000_I210_TQAVCC(queue));
  1539. tqavcc &= ~E1000_TQAVCC_IDLESLOPE_MASK;
  1540. wr32(E1000_I210_TQAVCC(queue), tqavcc);
  1541. /* Set hiCredit to zero. */
  1542. wr32(E1000_I210_TQAVHC(queue), 0);
  1543. }
  1544. /* XXX: In i210 controller the sendSlope and loCredit parameters from
  1545. * CBS are not configurable by software so we don't do any 'controller
  1546. * configuration' in respect to these parameters.
  1547. */
  1548. netdev_dbg(netdev, "CBS %s: queue %d idleslope %d sendslope %d hiCredit %d locredit %d\n",
  1549. (enable) ? "enabled" : "disabled", queue,
  1550. idleslope, sendslope, hicredit, locredit);
  1551. }
  1552. static int igb_save_cbs_params(struct igb_adapter *adapter, int queue,
  1553. bool enable, int idleslope, int sendslope,
  1554. int hicredit, int locredit)
  1555. {
  1556. struct igb_ring *ring;
  1557. if (queue < 0 || queue > adapter->num_tx_queues)
  1558. return -EINVAL;
  1559. ring = adapter->tx_ring[queue];
  1560. ring->cbs_enable = enable;
  1561. ring->idleslope = idleslope;
  1562. ring->sendslope = sendslope;
  1563. ring->hicredit = hicredit;
  1564. ring->locredit = locredit;
  1565. return 0;
  1566. }
  1567. static bool is_any_cbs_enabled(struct igb_adapter *adapter)
  1568. {
  1569. struct igb_ring *ring;
  1570. int i;
  1571. for (i = 0; i < adapter->num_tx_queues; i++) {
  1572. ring = adapter->tx_ring[i];
  1573. if (ring->cbs_enable)
  1574. return true;
  1575. }
  1576. return false;
  1577. }
  1578. static void igb_setup_tx_mode(struct igb_adapter *adapter)
  1579. {
  1580. struct net_device *netdev = adapter->netdev;
  1581. struct e1000_hw *hw = &adapter->hw;
  1582. u32 val;
  1583. /* Only i210 controller supports changing the transmission mode. */
  1584. if (hw->mac.type != e1000_i210)
  1585. return;
  1586. if (is_fqtss_enabled(adapter)) {
  1587. int i, max_queue;
  1588. /* Configure TQAVCTRL register: set transmit mode to 'Qav',
  1589. * set data fetch arbitration to 'round robin' and set data
  1590. * transfer arbitration to 'credit shaper algorithm.
  1591. */
  1592. val = rd32(E1000_I210_TQAVCTRL);
  1593. val |= E1000_TQAVCTRL_XMIT_MODE | E1000_TQAVCTRL_DATATRANARB;
  1594. val &= ~E1000_TQAVCTRL_DATAFETCHARB;
  1595. wr32(E1000_I210_TQAVCTRL, val);
  1596. /* Configure Tx and Rx packet buffers sizes as described in
  1597. * i210 datasheet section 7.2.7.7.
  1598. */
  1599. val = rd32(E1000_TXPBS);
  1600. val &= ~I210_TXPBSIZE_MASK;
  1601. val |= I210_TXPBSIZE_PB0_8KB | I210_TXPBSIZE_PB1_8KB |
  1602. I210_TXPBSIZE_PB2_4KB | I210_TXPBSIZE_PB3_4KB;
  1603. wr32(E1000_TXPBS, val);
  1604. val = rd32(E1000_RXPBS);
  1605. val &= ~I210_RXPBSIZE_MASK;
  1606. val |= I210_RXPBSIZE_PB_32KB;
  1607. wr32(E1000_RXPBS, val);
  1608. /* Section 8.12.9 states that MAX_TPKT_SIZE from DTXMXPKTSZ
  1609. * register should not exceed the buffer size programmed in
  1610. * TXPBS. The smallest buffer size programmed in TXPBS is 4kB
  1611. * so according to the datasheet we should set MAX_TPKT_SIZE to
  1612. * 4kB / 64.
  1613. *
  1614. * However, when we do so, no frame from queue 2 and 3 are
  1615. * transmitted. It seems the MAX_TPKT_SIZE should not be great
  1616. * or _equal_ to the buffer size programmed in TXPBS. For this
  1617. * reason, we set set MAX_ TPKT_SIZE to (4kB - 1) / 64.
  1618. */
  1619. val = (4096 - 1) / 64;
  1620. wr32(E1000_I210_DTXMXPKTSZ, val);
  1621. /* Since FQTSS mode is enabled, apply any CBS configuration
  1622. * previously set. If no previous CBS configuration has been
  1623. * done, then the initial configuration is applied, which means
  1624. * CBS is disabled.
  1625. */
  1626. max_queue = (adapter->num_tx_queues < I210_SR_QUEUES_NUM) ?
  1627. adapter->num_tx_queues : I210_SR_QUEUES_NUM;
  1628. for (i = 0; i < max_queue; i++) {
  1629. struct igb_ring *ring = adapter->tx_ring[i];
  1630. igb_configure_cbs(adapter, i, ring->cbs_enable,
  1631. ring->idleslope, ring->sendslope,
  1632. ring->hicredit, ring->locredit);
  1633. }
  1634. } else {
  1635. wr32(E1000_RXPBS, I210_RXPBSIZE_DEFAULT);
  1636. wr32(E1000_TXPBS, I210_TXPBSIZE_DEFAULT);
  1637. wr32(E1000_I210_DTXMXPKTSZ, I210_DTXMXPKTSZ_DEFAULT);
  1638. val = rd32(E1000_I210_TQAVCTRL);
  1639. /* According to Section 8.12.21, the other flags we've set when
  1640. * enabling FQTSS are not relevant when disabling FQTSS so we
  1641. * don't set they here.
  1642. */
  1643. val &= ~E1000_TQAVCTRL_XMIT_MODE;
  1644. wr32(E1000_I210_TQAVCTRL, val);
  1645. }
  1646. netdev_dbg(netdev, "FQTSS %s\n", (is_fqtss_enabled(adapter)) ?
  1647. "enabled" : "disabled");
  1648. }
  1649. /**
  1650. * igb_configure - configure the hardware for RX and TX
  1651. * @adapter: private board structure
  1652. **/
  1653. static void igb_configure(struct igb_adapter *adapter)
  1654. {
  1655. struct net_device *netdev = adapter->netdev;
  1656. int i;
  1657. igb_get_hw_control(adapter);
  1658. igb_set_rx_mode(netdev);
  1659. igb_setup_tx_mode(adapter);
  1660. igb_restore_vlan(adapter);
  1661. igb_setup_tctl(adapter);
  1662. igb_setup_mrqc(adapter);
  1663. igb_setup_rctl(adapter);
  1664. igb_nfc_filter_restore(adapter);
  1665. igb_configure_tx(adapter);
  1666. igb_configure_rx(adapter);
  1667. igb_rx_fifo_flush_82575(&adapter->hw);
  1668. /* call igb_desc_unused which always leaves
  1669. * at least 1 descriptor unused to make sure
  1670. * next_to_use != next_to_clean
  1671. */
  1672. for (i = 0; i < adapter->num_rx_queues; i++) {
  1673. struct igb_ring *ring = adapter->rx_ring[i];
  1674. igb_alloc_rx_buffers(ring, igb_desc_unused(ring));
  1675. }
  1676. }
  1677. /**
  1678. * igb_power_up_link - Power up the phy/serdes link
  1679. * @adapter: address of board private structure
  1680. **/
  1681. void igb_power_up_link(struct igb_adapter *adapter)
  1682. {
  1683. igb_reset_phy(&adapter->hw);
  1684. if (adapter->hw.phy.media_type == e1000_media_type_copper)
  1685. igb_power_up_phy_copper(&adapter->hw);
  1686. else
  1687. igb_power_up_serdes_link_82575(&adapter->hw);
  1688. igb_setup_link(&adapter->hw);
  1689. }
  1690. /**
  1691. * igb_power_down_link - Power down the phy/serdes link
  1692. * @adapter: address of board private structure
  1693. */
  1694. static void igb_power_down_link(struct igb_adapter *adapter)
  1695. {
  1696. if (adapter->hw.phy.media_type == e1000_media_type_copper)
  1697. igb_power_down_phy_copper_82575(&adapter->hw);
  1698. else
  1699. igb_shutdown_serdes_link_82575(&adapter->hw);
  1700. }
  1701. /**
  1702. * Detect and switch function for Media Auto Sense
  1703. * @adapter: address of the board private structure
  1704. **/
  1705. static void igb_check_swap_media(struct igb_adapter *adapter)
  1706. {
  1707. struct e1000_hw *hw = &adapter->hw;
  1708. u32 ctrl_ext, connsw;
  1709. bool swap_now = false;
  1710. ctrl_ext = rd32(E1000_CTRL_EXT);
  1711. connsw = rd32(E1000_CONNSW);
  1712. /* need to live swap if current media is copper and we have fiber/serdes
  1713. * to go to.
  1714. */
  1715. if ((hw->phy.media_type == e1000_media_type_copper) &&
  1716. (!(connsw & E1000_CONNSW_AUTOSENSE_EN))) {
  1717. swap_now = true;
  1718. } else if (!(connsw & E1000_CONNSW_SERDESD)) {
  1719. /* copper signal takes time to appear */
  1720. if (adapter->copper_tries < 4) {
  1721. adapter->copper_tries++;
  1722. connsw |= E1000_CONNSW_AUTOSENSE_CONF;
  1723. wr32(E1000_CONNSW, connsw);
  1724. return;
  1725. } else {
  1726. adapter->copper_tries = 0;
  1727. if ((connsw & E1000_CONNSW_PHYSD) &&
  1728. (!(connsw & E1000_CONNSW_PHY_PDN))) {
  1729. swap_now = true;
  1730. connsw &= ~E1000_CONNSW_AUTOSENSE_CONF;
  1731. wr32(E1000_CONNSW, connsw);
  1732. }
  1733. }
  1734. }
  1735. if (!swap_now)
  1736. return;
  1737. switch (hw->phy.media_type) {
  1738. case e1000_media_type_copper:
  1739. netdev_info(adapter->netdev,
  1740. "MAS: changing media to fiber/serdes\n");
  1741. ctrl_ext |=
  1742. E1000_CTRL_EXT_LINK_MODE_PCIE_SERDES;
  1743. adapter->flags |= IGB_FLAG_MEDIA_RESET;
  1744. adapter->copper_tries = 0;
  1745. break;
  1746. case e1000_media_type_internal_serdes:
  1747. case e1000_media_type_fiber:
  1748. netdev_info(adapter->netdev,
  1749. "MAS: changing media to copper\n");
  1750. ctrl_ext &=
  1751. ~E1000_CTRL_EXT_LINK_MODE_PCIE_SERDES;
  1752. adapter->flags |= IGB_FLAG_MEDIA_RESET;
  1753. break;
  1754. default:
  1755. /* shouldn't get here during regular operation */
  1756. netdev_err(adapter->netdev,
  1757. "AMS: Invalid media type found, returning\n");
  1758. break;
  1759. }
  1760. wr32(E1000_CTRL_EXT, ctrl_ext);
  1761. }
  1762. /**
  1763. * igb_up - Open the interface and prepare it to handle traffic
  1764. * @adapter: board private structure
  1765. **/
  1766. int igb_up(struct igb_adapter *adapter)
  1767. {
  1768. struct e1000_hw *hw = &adapter->hw;
  1769. int i;
  1770. /* hardware has been reset, we need to reload some things */
  1771. igb_configure(adapter);
  1772. clear_bit(__IGB_DOWN, &adapter->state);
  1773. for (i = 0; i < adapter->num_q_vectors; i++)
  1774. napi_enable(&(adapter->q_vector[i]->napi));
  1775. if (adapter->flags & IGB_FLAG_HAS_MSIX)
  1776. igb_configure_msix(adapter);
  1777. else
  1778. igb_assign_vector(adapter->q_vector[0], 0);
  1779. /* Clear any pending interrupts. */
  1780. rd32(E1000_TSICR);
  1781. rd32(E1000_ICR);
  1782. igb_irq_enable(adapter);
  1783. /* notify VFs that reset has been completed */
  1784. if (adapter->vfs_allocated_count) {
  1785. u32 reg_data = rd32(E1000_CTRL_EXT);
  1786. reg_data |= E1000_CTRL_EXT_PFRSTD;
  1787. wr32(E1000_CTRL_EXT, reg_data);
  1788. }
  1789. netif_tx_start_all_queues(adapter->netdev);
  1790. /* start the watchdog. */
  1791. hw->mac.get_link_status = 1;
  1792. schedule_work(&adapter->watchdog_task);
  1793. if ((adapter->flags & IGB_FLAG_EEE) &&
  1794. (!hw->dev_spec._82575.eee_disable))
  1795. adapter->eee_advert = MDIO_EEE_100TX | MDIO_EEE_1000T;
  1796. return 0;
  1797. }
  1798. void igb_down(struct igb_adapter *adapter)
  1799. {
  1800. struct net_device *netdev = adapter->netdev;
  1801. struct e1000_hw *hw = &adapter->hw;
  1802. u32 tctl, rctl;
  1803. int i;
  1804. /* signal that we're down so the interrupt handler does not
  1805. * reschedule our watchdog timer
  1806. */
  1807. set_bit(__IGB_DOWN, &adapter->state);
  1808. /* disable receives in the hardware */
  1809. rctl = rd32(E1000_RCTL);
  1810. wr32(E1000_RCTL, rctl & ~E1000_RCTL_EN);
  1811. /* flush and sleep below */
  1812. igb_nfc_filter_exit(adapter);
  1813. netif_carrier_off(netdev);
  1814. netif_tx_stop_all_queues(netdev);
  1815. /* disable transmits in the hardware */
  1816. tctl = rd32(E1000_TCTL);
  1817. tctl &= ~E1000_TCTL_EN;
  1818. wr32(E1000_TCTL, tctl);
  1819. /* flush both disables and wait for them to finish */
  1820. wrfl();
  1821. usleep_range(10000, 11000);
  1822. igb_irq_disable(adapter);
  1823. adapter->flags &= ~IGB_FLAG_NEED_LINK_UPDATE;
  1824. for (i = 0; i < adapter->num_q_vectors; i++) {
  1825. if (adapter->q_vector[i]) {
  1826. napi_synchronize(&adapter->q_vector[i]->napi);
  1827. napi_disable(&adapter->q_vector[i]->napi);
  1828. }
  1829. }
  1830. del_timer_sync(&adapter->watchdog_timer);
  1831. del_timer_sync(&adapter->phy_info_timer);
  1832. /* record the stats before reset*/
  1833. spin_lock(&adapter->stats64_lock);
  1834. igb_update_stats(adapter);
  1835. spin_unlock(&adapter->stats64_lock);
  1836. adapter->link_speed = 0;
  1837. adapter->link_duplex = 0;
  1838. if (!pci_channel_offline(adapter->pdev))
  1839. igb_reset(adapter);
  1840. /* clear VLAN promisc flag so VFTA will be updated if necessary */
  1841. adapter->flags &= ~IGB_FLAG_VLAN_PROMISC;
  1842. igb_clean_all_tx_rings(adapter);
  1843. igb_clean_all_rx_rings(adapter);
  1844. #ifdef CONFIG_IGB_DCA
  1845. /* since we reset the hardware DCA settings were cleared */
  1846. igb_setup_dca(adapter);
  1847. #endif
  1848. }
  1849. void igb_reinit_locked(struct igb_adapter *adapter)
  1850. {
  1851. WARN_ON(in_interrupt());
  1852. while (test_and_set_bit(__IGB_RESETTING, &adapter->state))
  1853. usleep_range(1000, 2000);
  1854. igb_down(adapter);
  1855. igb_up(adapter);
  1856. clear_bit(__IGB_RESETTING, &adapter->state);
  1857. }
  1858. /** igb_enable_mas - Media Autosense re-enable after swap
  1859. *
  1860. * @adapter: adapter struct
  1861. **/
  1862. static void igb_enable_mas(struct igb_adapter *adapter)
  1863. {
  1864. struct e1000_hw *hw = &adapter->hw;
  1865. u32 connsw = rd32(E1000_CONNSW);
  1866. /* configure for SerDes media detect */
  1867. if ((hw->phy.media_type == e1000_media_type_copper) &&
  1868. (!(connsw & E1000_CONNSW_SERDESD))) {
  1869. connsw |= E1000_CONNSW_ENRGSRC;
  1870. connsw |= E1000_CONNSW_AUTOSENSE_EN;
  1871. wr32(E1000_CONNSW, connsw);
  1872. wrfl();
  1873. }
  1874. }
  1875. void igb_reset(struct igb_adapter *adapter)
  1876. {
  1877. struct pci_dev *pdev = adapter->pdev;
  1878. struct e1000_hw *hw = &adapter->hw;
  1879. struct e1000_mac_info *mac = &hw->mac;
  1880. struct e1000_fc_info *fc = &hw->fc;
  1881. u32 pba, hwm;
  1882. /* Repartition Pba for greater than 9k mtu
  1883. * To take effect CTRL.RST is required.
  1884. */
  1885. switch (mac->type) {
  1886. case e1000_i350:
  1887. case e1000_i354:
  1888. case e1000_82580:
  1889. pba = rd32(E1000_RXPBS);
  1890. pba = igb_rxpbs_adjust_82580(pba);
  1891. break;
  1892. case e1000_82576:
  1893. pba = rd32(E1000_RXPBS);
  1894. pba &= E1000_RXPBS_SIZE_MASK_82576;
  1895. break;
  1896. case e1000_82575:
  1897. case e1000_i210:
  1898. case e1000_i211:
  1899. default:
  1900. pba = E1000_PBA_34K;
  1901. break;
  1902. }
  1903. if (mac->type == e1000_82575) {
  1904. u32 min_rx_space, min_tx_space, needed_tx_space;
  1905. /* write Rx PBA so that hardware can report correct Tx PBA */
  1906. wr32(E1000_PBA, pba);
  1907. /* To maintain wire speed transmits, the Tx FIFO should be
  1908. * large enough to accommodate two full transmit packets,
  1909. * rounded up to the next 1KB and expressed in KB. Likewise,
  1910. * the Rx FIFO should be large enough to accommodate at least
  1911. * one full receive packet and is similarly rounded up and
  1912. * expressed in KB.
  1913. */
  1914. min_rx_space = DIV_ROUND_UP(MAX_JUMBO_FRAME_SIZE, 1024);
  1915. /* The Tx FIFO also stores 16 bytes of information about the Tx
  1916. * but don't include Ethernet FCS because hardware appends it.
  1917. * We only need to round down to the nearest 512 byte block
  1918. * count since the value we care about is 2 frames, not 1.
  1919. */
  1920. min_tx_space = adapter->max_frame_size;
  1921. min_tx_space += sizeof(union e1000_adv_tx_desc) - ETH_FCS_LEN;
  1922. min_tx_space = DIV_ROUND_UP(min_tx_space, 512);
  1923. /* upper 16 bits has Tx packet buffer allocation size in KB */
  1924. needed_tx_space = min_tx_space - (rd32(E1000_PBA) >> 16);
  1925. /* If current Tx allocation is less than the min Tx FIFO size,
  1926. * and the min Tx FIFO size is less than the current Rx FIFO
  1927. * allocation, take space away from current Rx allocation.
  1928. */
  1929. if (needed_tx_space < pba) {
  1930. pba -= needed_tx_space;
  1931. /* if short on Rx space, Rx wins and must trump Tx
  1932. * adjustment
  1933. */
  1934. if (pba < min_rx_space)
  1935. pba = min_rx_space;
  1936. }
  1937. /* adjust PBA for jumbo frames */
  1938. wr32(E1000_PBA, pba);
  1939. }
  1940. /* flow control settings
  1941. * The high water mark must be low enough to fit one full frame
  1942. * after transmitting the pause frame. As such we must have enough
  1943. * space to allow for us to complete our current transmit and then
  1944. * receive the frame that is in progress from the link partner.
  1945. * Set it to:
  1946. * - the full Rx FIFO size minus one full Tx plus one full Rx frame
  1947. */
  1948. hwm = (pba << 10) - (adapter->max_frame_size + MAX_JUMBO_FRAME_SIZE);
  1949. fc->high_water = hwm & 0xFFFFFFF0; /* 16-byte granularity */
  1950. fc->low_water = fc->high_water - 16;
  1951. fc->pause_time = 0xFFFF;
  1952. fc->send_xon = 1;
  1953. fc->current_mode = fc->requested_mode;
  1954. /* disable receive for all VFs and wait one second */
  1955. if (adapter->vfs_allocated_count) {
  1956. int i;
  1957. for (i = 0 ; i < adapter->vfs_allocated_count; i++)
  1958. adapter->vf_data[i].flags &= IGB_VF_FLAG_PF_SET_MAC;
  1959. /* ping all the active vfs to let them know we are going down */
  1960. igb_ping_all_vfs(adapter);
  1961. /* disable transmits and receives */
  1962. wr32(E1000_VFRE, 0);
  1963. wr32(E1000_VFTE, 0);
  1964. }
  1965. /* Allow time for pending master requests to run */
  1966. hw->mac.ops.reset_hw(hw);
  1967. wr32(E1000_WUC, 0);
  1968. if (adapter->flags & IGB_FLAG_MEDIA_RESET) {
  1969. /* need to resetup here after media swap */
  1970. adapter->ei.get_invariants(hw);
  1971. adapter->flags &= ~IGB_FLAG_MEDIA_RESET;
  1972. }
  1973. if ((mac->type == e1000_82575) &&
  1974. (adapter->flags & IGB_FLAG_MAS_ENABLE)) {
  1975. igb_enable_mas(adapter);
  1976. }
  1977. if (hw->mac.ops.init_hw(hw))
  1978. dev_err(&pdev->dev, "Hardware Error\n");
  1979. /* RAR registers were cleared during init_hw, clear mac table */
  1980. igb_flush_mac_table(adapter);
  1981. __dev_uc_unsync(adapter->netdev, NULL);
  1982. /* Recover default RAR entry */
  1983. igb_set_default_mac_filter(adapter);
  1984. /* Flow control settings reset on hardware reset, so guarantee flow
  1985. * control is off when forcing speed.
  1986. */
  1987. if (!hw->mac.autoneg)
  1988. igb_force_mac_fc(hw);
  1989. igb_init_dmac(adapter, pba);
  1990. #ifdef CONFIG_IGB_HWMON
  1991. /* Re-initialize the thermal sensor on i350 devices. */
  1992. if (!test_bit(__IGB_DOWN, &adapter->state)) {
  1993. if (mac->type == e1000_i350 && hw->bus.func == 0) {
  1994. /* If present, re-initialize the external thermal sensor
  1995. * interface.
  1996. */
  1997. if (adapter->ets)
  1998. mac->ops.init_thermal_sensor_thresh(hw);
  1999. }
  2000. }
  2001. #endif
  2002. /* Re-establish EEE setting */
  2003. if (hw->phy.media_type == e1000_media_type_copper) {
  2004. switch (mac->type) {
  2005. case e1000_i350:
  2006. case e1000_i210:
  2007. case e1000_i211:
  2008. igb_set_eee_i350(hw, true, true);
  2009. break;
  2010. case e1000_i354:
  2011. igb_set_eee_i354(hw, true, true);
  2012. break;
  2013. default:
  2014. break;
  2015. }
  2016. }
  2017. if (!netif_running(adapter->netdev))
  2018. igb_power_down_link(adapter);
  2019. igb_update_mng_vlan(adapter);
  2020. /* Enable h/w to recognize an 802.1Q VLAN Ethernet packet */
  2021. wr32(E1000_VET, ETHERNET_IEEE_VLAN_TYPE);
  2022. /* Re-enable PTP, where applicable. */
  2023. if (adapter->ptp_flags & IGB_PTP_ENABLED)
  2024. igb_ptp_reset(adapter);
  2025. igb_get_phy_info(hw);
  2026. }
  2027. static netdev_features_t igb_fix_features(struct net_device *netdev,
  2028. netdev_features_t features)
  2029. {
  2030. /* Since there is no support for separate Rx/Tx vlan accel
  2031. * enable/disable make sure Tx flag is always in same state as Rx.
  2032. */
  2033. if (features & NETIF_F_HW_VLAN_CTAG_RX)
  2034. features |= NETIF_F_HW_VLAN_CTAG_TX;
  2035. else
  2036. features &= ~NETIF_F_HW_VLAN_CTAG_TX;
  2037. return features;
  2038. }
  2039. static int igb_set_features(struct net_device *netdev,
  2040. netdev_features_t features)
  2041. {
  2042. netdev_features_t changed = netdev->features ^ features;
  2043. struct igb_adapter *adapter = netdev_priv(netdev);
  2044. if (changed & NETIF_F_HW_VLAN_CTAG_RX)
  2045. igb_vlan_mode(netdev, features);
  2046. if (!(changed & (NETIF_F_RXALL | NETIF_F_NTUPLE)))
  2047. return 0;
  2048. if (!(features & NETIF_F_NTUPLE)) {
  2049. struct hlist_node *node2;
  2050. struct igb_nfc_filter *rule;
  2051. spin_lock(&adapter->nfc_lock);
  2052. hlist_for_each_entry_safe(rule, node2,
  2053. &adapter->nfc_filter_list, nfc_node) {
  2054. igb_erase_filter(adapter, rule);
  2055. hlist_del(&rule->nfc_node);
  2056. kfree(rule);
  2057. }
  2058. spin_unlock(&adapter->nfc_lock);
  2059. adapter->nfc_filter_count = 0;
  2060. }
  2061. netdev->features = features;
  2062. if (netif_running(netdev))
  2063. igb_reinit_locked(adapter);
  2064. else
  2065. igb_reset(adapter);
  2066. return 0;
  2067. }
  2068. static int igb_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
  2069. struct net_device *dev,
  2070. const unsigned char *addr, u16 vid,
  2071. u16 flags)
  2072. {
  2073. /* guarantee we can provide a unique filter for the unicast address */
  2074. if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr)) {
  2075. struct igb_adapter *adapter = netdev_priv(dev);
  2076. int vfn = adapter->vfs_allocated_count;
  2077. if (netdev_uc_count(dev) >= igb_available_rars(adapter, vfn))
  2078. return -ENOMEM;
  2079. }
  2080. return ndo_dflt_fdb_add(ndm, tb, dev, addr, vid, flags);
  2081. }
  2082. #define IGB_MAX_MAC_HDR_LEN 127
  2083. #define IGB_MAX_NETWORK_HDR_LEN 511
  2084. static netdev_features_t
  2085. igb_features_check(struct sk_buff *skb, struct net_device *dev,
  2086. netdev_features_t features)
  2087. {
  2088. unsigned int network_hdr_len, mac_hdr_len;
  2089. /* Make certain the headers can be described by a context descriptor */
  2090. mac_hdr_len = skb_network_header(skb) - skb->data;
  2091. if (unlikely(mac_hdr_len > IGB_MAX_MAC_HDR_LEN))
  2092. return features & ~(NETIF_F_HW_CSUM |
  2093. NETIF_F_SCTP_CRC |
  2094. NETIF_F_HW_VLAN_CTAG_TX |
  2095. NETIF_F_TSO |
  2096. NETIF_F_TSO6);
  2097. network_hdr_len = skb_checksum_start(skb) - skb_network_header(skb);
  2098. if (unlikely(network_hdr_len > IGB_MAX_NETWORK_HDR_LEN))
  2099. return features & ~(NETIF_F_HW_CSUM |
  2100. NETIF_F_SCTP_CRC |
  2101. NETIF_F_TSO |
  2102. NETIF_F_TSO6);
  2103. /* We can only support IPV4 TSO in tunnels if we can mangle the
  2104. * inner IP ID field, so strip TSO if MANGLEID is not supported.
  2105. */
  2106. if (skb->encapsulation && !(features & NETIF_F_TSO_MANGLEID))
  2107. features &= ~NETIF_F_TSO;
  2108. return features;
  2109. }
  2110. static int igb_offload_cbs(struct igb_adapter *adapter,
  2111. struct tc_cbs_qopt_offload *qopt)
  2112. {
  2113. struct e1000_hw *hw = &adapter->hw;
  2114. int err;
  2115. /* CBS offloading is only supported by i210 controller. */
  2116. if (hw->mac.type != e1000_i210)
  2117. return -EOPNOTSUPP;
  2118. /* CBS offloading is only supported by queue 0 and queue 1. */
  2119. if (qopt->queue < 0 || qopt->queue > 1)
  2120. return -EINVAL;
  2121. err = igb_save_cbs_params(adapter, qopt->queue, qopt->enable,
  2122. qopt->idleslope, qopt->sendslope,
  2123. qopt->hicredit, qopt->locredit);
  2124. if (err)
  2125. return err;
  2126. if (is_fqtss_enabled(adapter)) {
  2127. igb_configure_cbs(adapter, qopt->queue, qopt->enable,
  2128. qopt->idleslope, qopt->sendslope,
  2129. qopt->hicredit, qopt->locredit);
  2130. if (!is_any_cbs_enabled(adapter))
  2131. enable_fqtss(adapter, false);
  2132. } else {
  2133. enable_fqtss(adapter, true);
  2134. }
  2135. return 0;
  2136. }
  2137. #define ETHER_TYPE_FULL_MASK ((__force __be16)~0)
  2138. #define VLAN_PRIO_FULL_MASK (0x07)
  2139. static int igb_parse_cls_flower(struct igb_adapter *adapter,
  2140. struct tc_cls_flower_offload *f,
  2141. int traffic_class,
  2142. struct igb_nfc_filter *input)
  2143. {
  2144. struct netlink_ext_ack *extack = f->common.extack;
  2145. if (f->dissector->used_keys &
  2146. ~(BIT(FLOW_DISSECTOR_KEY_BASIC) |
  2147. BIT(FLOW_DISSECTOR_KEY_CONTROL) |
  2148. BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
  2149. BIT(FLOW_DISSECTOR_KEY_VLAN))) {
  2150. NL_SET_ERR_MSG_MOD(extack,
  2151. "Unsupported key used, only BASIC, CONTROL, ETH_ADDRS and VLAN are supported");
  2152. return -EOPNOTSUPP;
  2153. }
  2154. if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
  2155. struct flow_dissector_key_eth_addrs *key, *mask;
  2156. key = skb_flow_dissector_target(f->dissector,
  2157. FLOW_DISSECTOR_KEY_ETH_ADDRS,
  2158. f->key);
  2159. mask = skb_flow_dissector_target(f->dissector,
  2160. FLOW_DISSECTOR_KEY_ETH_ADDRS,
  2161. f->mask);
  2162. if (!is_zero_ether_addr(mask->dst)) {
  2163. if (!is_broadcast_ether_addr(mask->dst)) {
  2164. NL_SET_ERR_MSG_MOD(extack, "Only full masks are supported for destination MAC address");
  2165. return -EINVAL;
  2166. }
  2167. input->filter.match_flags |=
  2168. IGB_FILTER_FLAG_DST_MAC_ADDR;
  2169. ether_addr_copy(input->filter.dst_addr, key->dst);
  2170. }
  2171. if (!is_zero_ether_addr(mask->src)) {
  2172. if (!is_broadcast_ether_addr(mask->src)) {
  2173. NL_SET_ERR_MSG_MOD(extack, "Only full masks are supported for source MAC address");
  2174. return -EINVAL;
  2175. }
  2176. input->filter.match_flags |=
  2177. IGB_FILTER_FLAG_SRC_MAC_ADDR;
  2178. ether_addr_copy(input->filter.src_addr, key->src);
  2179. }
  2180. }
  2181. if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_BASIC)) {
  2182. struct flow_dissector_key_basic *key, *mask;
  2183. key = skb_flow_dissector_target(f->dissector,
  2184. FLOW_DISSECTOR_KEY_BASIC,
  2185. f->key);
  2186. mask = skb_flow_dissector_target(f->dissector,
  2187. FLOW_DISSECTOR_KEY_BASIC,
  2188. f->mask);
  2189. if (mask->n_proto) {
  2190. if (mask->n_proto != ETHER_TYPE_FULL_MASK) {
  2191. NL_SET_ERR_MSG_MOD(extack, "Only full mask is supported for EtherType filter");
  2192. return -EINVAL;
  2193. }
  2194. input->filter.match_flags |= IGB_FILTER_FLAG_ETHER_TYPE;
  2195. input->filter.etype = key->n_proto;
  2196. }
  2197. }
  2198. if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_VLAN)) {
  2199. struct flow_dissector_key_vlan *key, *mask;
  2200. key = skb_flow_dissector_target(f->dissector,
  2201. FLOW_DISSECTOR_KEY_VLAN,
  2202. f->key);
  2203. mask = skb_flow_dissector_target(f->dissector,
  2204. FLOW_DISSECTOR_KEY_VLAN,
  2205. f->mask);
  2206. if (mask->vlan_priority) {
  2207. if (mask->vlan_priority != VLAN_PRIO_FULL_MASK) {
  2208. NL_SET_ERR_MSG_MOD(extack, "Only full mask is supported for VLAN priority");
  2209. return -EINVAL;
  2210. }
  2211. input->filter.match_flags |= IGB_FILTER_FLAG_VLAN_TCI;
  2212. input->filter.vlan_tci = key->vlan_priority;
  2213. }
  2214. }
  2215. input->action = traffic_class;
  2216. input->cookie = f->cookie;
  2217. return 0;
  2218. }
  2219. static int igb_configure_clsflower(struct igb_adapter *adapter,
  2220. struct tc_cls_flower_offload *cls_flower)
  2221. {
  2222. struct netlink_ext_ack *extack = cls_flower->common.extack;
  2223. struct igb_nfc_filter *filter, *f;
  2224. int err, tc;
  2225. tc = tc_classid_to_hwtc(adapter->netdev, cls_flower->classid);
  2226. if (tc < 0) {
  2227. NL_SET_ERR_MSG_MOD(extack, "Invalid traffic class");
  2228. return -EINVAL;
  2229. }
  2230. filter = kzalloc(sizeof(*filter), GFP_KERNEL);
  2231. if (!filter)
  2232. return -ENOMEM;
  2233. err = igb_parse_cls_flower(adapter, cls_flower, tc, filter);
  2234. if (err < 0)
  2235. goto err_parse;
  2236. spin_lock(&adapter->nfc_lock);
  2237. hlist_for_each_entry(f, &adapter->nfc_filter_list, nfc_node) {
  2238. if (!memcmp(&f->filter, &filter->filter, sizeof(f->filter))) {
  2239. err = -EEXIST;
  2240. NL_SET_ERR_MSG_MOD(extack,
  2241. "This filter is already set in ethtool");
  2242. goto err_locked;
  2243. }
  2244. }
  2245. hlist_for_each_entry(f, &adapter->cls_flower_list, nfc_node) {
  2246. if (!memcmp(&f->filter, &filter->filter, sizeof(f->filter))) {
  2247. err = -EEXIST;
  2248. NL_SET_ERR_MSG_MOD(extack,
  2249. "This filter is already set in cls_flower");
  2250. goto err_locked;
  2251. }
  2252. }
  2253. err = igb_add_filter(adapter, filter);
  2254. if (err < 0) {
  2255. NL_SET_ERR_MSG_MOD(extack, "Could not add filter to the adapter");
  2256. goto err_locked;
  2257. }
  2258. hlist_add_head(&filter->nfc_node, &adapter->cls_flower_list);
  2259. spin_unlock(&adapter->nfc_lock);
  2260. return 0;
  2261. err_locked:
  2262. spin_unlock(&adapter->nfc_lock);
  2263. err_parse:
  2264. kfree(filter);
  2265. return err;
  2266. }
  2267. static int igb_delete_clsflower(struct igb_adapter *adapter,
  2268. struct tc_cls_flower_offload *cls_flower)
  2269. {
  2270. struct igb_nfc_filter *filter;
  2271. int err;
  2272. spin_lock(&adapter->nfc_lock);
  2273. hlist_for_each_entry(filter, &adapter->cls_flower_list, nfc_node)
  2274. if (filter->cookie == cls_flower->cookie)
  2275. break;
  2276. if (!filter) {
  2277. err = -ENOENT;
  2278. goto out;
  2279. }
  2280. err = igb_erase_filter(adapter, filter);
  2281. if (err < 0)
  2282. goto out;
  2283. hlist_del(&filter->nfc_node);
  2284. kfree(filter);
  2285. out:
  2286. spin_unlock(&adapter->nfc_lock);
  2287. return err;
  2288. }
  2289. static int igb_setup_tc_cls_flower(struct igb_adapter *adapter,
  2290. struct tc_cls_flower_offload *cls_flower)
  2291. {
  2292. switch (cls_flower->command) {
  2293. case TC_CLSFLOWER_REPLACE:
  2294. return igb_configure_clsflower(adapter, cls_flower);
  2295. case TC_CLSFLOWER_DESTROY:
  2296. return igb_delete_clsflower(adapter, cls_flower);
  2297. case TC_CLSFLOWER_STATS:
  2298. return -EOPNOTSUPP;
  2299. default:
  2300. return -EINVAL;
  2301. }
  2302. }
  2303. static int igb_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
  2304. void *cb_priv)
  2305. {
  2306. struct igb_adapter *adapter = cb_priv;
  2307. if (!tc_cls_can_offload_and_chain0(adapter->netdev, type_data))
  2308. return -EOPNOTSUPP;
  2309. switch (type) {
  2310. case TC_SETUP_CLSFLOWER:
  2311. return igb_setup_tc_cls_flower(adapter, type_data);
  2312. default:
  2313. return -EOPNOTSUPP;
  2314. }
  2315. }
  2316. static int igb_setup_tc_block(struct igb_adapter *adapter,
  2317. struct tc_block_offload *f)
  2318. {
  2319. if (f->binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
  2320. return -EOPNOTSUPP;
  2321. switch (f->command) {
  2322. case TC_BLOCK_BIND:
  2323. return tcf_block_cb_register(f->block, igb_setup_tc_block_cb,
  2324. adapter, adapter);
  2325. case TC_BLOCK_UNBIND:
  2326. tcf_block_cb_unregister(f->block, igb_setup_tc_block_cb,
  2327. adapter);
  2328. return 0;
  2329. default:
  2330. return -EOPNOTSUPP;
  2331. }
  2332. }
  2333. static int igb_setup_tc(struct net_device *dev, enum tc_setup_type type,
  2334. void *type_data)
  2335. {
  2336. struct igb_adapter *adapter = netdev_priv(dev);
  2337. switch (type) {
  2338. case TC_SETUP_QDISC_CBS:
  2339. return igb_offload_cbs(adapter, type_data);
  2340. case TC_SETUP_BLOCK:
  2341. return igb_setup_tc_block(adapter, type_data);
  2342. default:
  2343. return -EOPNOTSUPP;
  2344. }
  2345. }
  2346. static const struct net_device_ops igb_netdev_ops = {
  2347. .ndo_open = igb_open,
  2348. .ndo_stop = igb_close,
  2349. .ndo_start_xmit = igb_xmit_frame,
  2350. .ndo_get_stats64 = igb_get_stats64,
  2351. .ndo_set_rx_mode = igb_set_rx_mode,
  2352. .ndo_set_mac_address = igb_set_mac,
  2353. .ndo_change_mtu = igb_change_mtu,
  2354. .ndo_do_ioctl = igb_ioctl,
  2355. .ndo_tx_timeout = igb_tx_timeout,
  2356. .ndo_validate_addr = eth_validate_addr,
  2357. .ndo_vlan_rx_add_vid = igb_vlan_rx_add_vid,
  2358. .ndo_vlan_rx_kill_vid = igb_vlan_rx_kill_vid,
  2359. .ndo_set_vf_mac = igb_ndo_set_vf_mac,
  2360. .ndo_set_vf_vlan = igb_ndo_set_vf_vlan,
  2361. .ndo_set_vf_rate = igb_ndo_set_vf_bw,
  2362. .ndo_set_vf_spoofchk = igb_ndo_set_vf_spoofchk,
  2363. .ndo_set_vf_trust = igb_ndo_set_vf_trust,
  2364. .ndo_get_vf_config = igb_ndo_get_vf_config,
  2365. #ifdef CONFIG_NET_POLL_CONTROLLER
  2366. .ndo_poll_controller = igb_netpoll,
  2367. #endif
  2368. .ndo_fix_features = igb_fix_features,
  2369. .ndo_set_features = igb_set_features,
  2370. .ndo_fdb_add = igb_ndo_fdb_add,
  2371. .ndo_features_check = igb_features_check,
  2372. .ndo_setup_tc = igb_setup_tc,
  2373. };
  2374. /**
  2375. * igb_set_fw_version - Configure version string for ethtool
  2376. * @adapter: adapter struct
  2377. **/
  2378. void igb_set_fw_version(struct igb_adapter *adapter)
  2379. {
  2380. struct e1000_hw *hw = &adapter->hw;
  2381. struct e1000_fw_version fw;
  2382. igb_get_fw_version(hw, &fw);
  2383. switch (hw->mac.type) {
  2384. case e1000_i210:
  2385. case e1000_i211:
  2386. if (!(igb_get_flash_presence_i210(hw))) {
  2387. snprintf(adapter->fw_version,
  2388. sizeof(adapter->fw_version),
  2389. "%2d.%2d-%d",
  2390. fw.invm_major, fw.invm_minor,
  2391. fw.invm_img_type);
  2392. break;
  2393. }
  2394. /* fall through */
  2395. default:
  2396. /* if option is rom valid, display its version too */
  2397. if (fw.or_valid) {
  2398. snprintf(adapter->fw_version,
  2399. sizeof(adapter->fw_version),
  2400. "%d.%d, 0x%08x, %d.%d.%d",
  2401. fw.eep_major, fw.eep_minor, fw.etrack_id,
  2402. fw.or_major, fw.or_build, fw.or_patch);
  2403. /* no option rom */
  2404. } else if (fw.etrack_id != 0X0000) {
  2405. snprintf(adapter->fw_version,
  2406. sizeof(adapter->fw_version),
  2407. "%d.%d, 0x%08x",
  2408. fw.eep_major, fw.eep_minor, fw.etrack_id);
  2409. } else {
  2410. snprintf(adapter->fw_version,
  2411. sizeof(adapter->fw_version),
  2412. "%d.%d.%d",
  2413. fw.eep_major, fw.eep_minor, fw.eep_build);
  2414. }
  2415. break;
  2416. }
  2417. }
  2418. /**
  2419. * igb_init_mas - init Media Autosense feature if enabled in the NVM
  2420. *
  2421. * @adapter: adapter struct
  2422. **/
  2423. static void igb_init_mas(struct igb_adapter *adapter)
  2424. {
  2425. struct e1000_hw *hw = &adapter->hw;
  2426. u16 eeprom_data;
  2427. hw->nvm.ops.read(hw, NVM_COMPAT, 1, &eeprom_data);
  2428. switch (hw->bus.func) {
  2429. case E1000_FUNC_0:
  2430. if (eeprom_data & IGB_MAS_ENABLE_0) {
  2431. adapter->flags |= IGB_FLAG_MAS_ENABLE;
  2432. netdev_info(adapter->netdev,
  2433. "MAS: Enabling Media Autosense for port %d\n",
  2434. hw->bus.func);
  2435. }
  2436. break;
  2437. case E1000_FUNC_1:
  2438. if (eeprom_data & IGB_MAS_ENABLE_1) {
  2439. adapter->flags |= IGB_FLAG_MAS_ENABLE;
  2440. netdev_info(adapter->netdev,
  2441. "MAS: Enabling Media Autosense for port %d\n",
  2442. hw->bus.func);
  2443. }
  2444. break;
  2445. case E1000_FUNC_2:
  2446. if (eeprom_data & IGB_MAS_ENABLE_2) {
  2447. adapter->flags |= IGB_FLAG_MAS_ENABLE;
  2448. netdev_info(adapter->netdev,
  2449. "MAS: Enabling Media Autosense for port %d\n",
  2450. hw->bus.func);
  2451. }
  2452. break;
  2453. case E1000_FUNC_3:
  2454. if (eeprom_data & IGB_MAS_ENABLE_3) {
  2455. adapter->flags |= IGB_FLAG_MAS_ENABLE;
  2456. netdev_info(adapter->netdev,
  2457. "MAS: Enabling Media Autosense for port %d\n",
  2458. hw->bus.func);
  2459. }
  2460. break;
  2461. default:
  2462. /* Shouldn't get here */
  2463. netdev_err(adapter->netdev,
  2464. "MAS: Invalid port configuration, returning\n");
  2465. break;
  2466. }
  2467. }
  2468. /**
  2469. * igb_init_i2c - Init I2C interface
  2470. * @adapter: pointer to adapter structure
  2471. **/
  2472. static s32 igb_init_i2c(struct igb_adapter *adapter)
  2473. {
  2474. s32 status = 0;
  2475. /* I2C interface supported on i350 devices */
  2476. if (adapter->hw.mac.type != e1000_i350)
  2477. return 0;
  2478. /* Initialize the i2c bus which is controlled by the registers.
  2479. * This bus will use the i2c_algo_bit structue that implements
  2480. * the protocol through toggling of the 4 bits in the register.
  2481. */
  2482. adapter->i2c_adap.owner = THIS_MODULE;
  2483. adapter->i2c_algo = igb_i2c_algo;
  2484. adapter->i2c_algo.data = adapter;
  2485. adapter->i2c_adap.algo_data = &adapter->i2c_algo;
  2486. adapter->i2c_adap.dev.parent = &adapter->pdev->dev;
  2487. strlcpy(adapter->i2c_adap.name, "igb BB",
  2488. sizeof(adapter->i2c_adap.name));
  2489. status = i2c_bit_add_bus(&adapter->i2c_adap);
  2490. return status;
  2491. }
  2492. /**
  2493. * igb_probe - Device Initialization Routine
  2494. * @pdev: PCI device information struct
  2495. * @ent: entry in igb_pci_tbl
  2496. *
  2497. * Returns 0 on success, negative on failure
  2498. *
  2499. * igb_probe initializes an adapter identified by a pci_dev structure.
  2500. * The OS initialization, configuring of the adapter private structure,
  2501. * and a hardware reset occur.
  2502. **/
  2503. static int igb_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  2504. {
  2505. struct net_device *netdev;
  2506. struct igb_adapter *adapter;
  2507. struct e1000_hw *hw;
  2508. u16 eeprom_data = 0;
  2509. s32 ret_val;
  2510. static int global_quad_port_a; /* global quad port a indication */
  2511. const struct e1000_info *ei = igb_info_tbl[ent->driver_data];
  2512. int err, pci_using_dac;
  2513. u8 part_str[E1000_PBANUM_LENGTH];
  2514. /* Catch broken hardware that put the wrong VF device ID in
  2515. * the PCIe SR-IOV capability.
  2516. */
  2517. if (pdev->is_virtfn) {
  2518. WARN(1, KERN_ERR "%s (%hx:%hx) should not be a VF!\n",
  2519. pci_name(pdev), pdev->vendor, pdev->device);
  2520. return -EINVAL;
  2521. }
  2522. err = pci_enable_device_mem(pdev);
  2523. if (err)
  2524. return err;
  2525. pci_using_dac = 0;
  2526. err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
  2527. if (!err) {
  2528. pci_using_dac = 1;
  2529. } else {
  2530. err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  2531. if (err) {
  2532. dev_err(&pdev->dev,
  2533. "No usable DMA configuration, aborting\n");
  2534. goto err_dma;
  2535. }
  2536. }
  2537. err = pci_request_mem_regions(pdev, igb_driver_name);
  2538. if (err)
  2539. goto err_pci_reg;
  2540. pci_enable_pcie_error_reporting(pdev);
  2541. pci_set_master(pdev);
  2542. pci_save_state(pdev);
  2543. err = -ENOMEM;
  2544. netdev = alloc_etherdev_mq(sizeof(struct igb_adapter),
  2545. IGB_MAX_TX_QUEUES);
  2546. if (!netdev)
  2547. goto err_alloc_etherdev;
  2548. SET_NETDEV_DEV(netdev, &pdev->dev);
  2549. pci_set_drvdata(pdev, netdev);
  2550. adapter = netdev_priv(netdev);
  2551. adapter->netdev = netdev;
  2552. adapter->pdev = pdev;
  2553. hw = &adapter->hw;
  2554. hw->back = adapter;
  2555. adapter->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
  2556. err = -EIO;
  2557. adapter->io_addr = pci_iomap(pdev, 0, 0);
  2558. if (!adapter->io_addr)
  2559. goto err_ioremap;
  2560. /* hw->hw_addr can be altered, we'll use adapter->io_addr for unmap */
  2561. hw->hw_addr = adapter->io_addr;
  2562. netdev->netdev_ops = &igb_netdev_ops;
  2563. igb_set_ethtool_ops(netdev);
  2564. netdev->watchdog_timeo = 5 * HZ;
  2565. strncpy(netdev->name, pci_name(pdev), sizeof(netdev->name) - 1);
  2566. netdev->mem_start = pci_resource_start(pdev, 0);
  2567. netdev->mem_end = pci_resource_end(pdev, 0);
  2568. /* PCI config space info */
  2569. hw->vendor_id = pdev->vendor;
  2570. hw->device_id = pdev->device;
  2571. hw->revision_id = pdev->revision;
  2572. hw->subsystem_vendor_id = pdev->subsystem_vendor;
  2573. hw->subsystem_device_id = pdev->subsystem_device;
  2574. /* Copy the default MAC, PHY and NVM function pointers */
  2575. memcpy(&hw->mac.ops, ei->mac_ops, sizeof(hw->mac.ops));
  2576. memcpy(&hw->phy.ops, ei->phy_ops, sizeof(hw->phy.ops));
  2577. memcpy(&hw->nvm.ops, ei->nvm_ops, sizeof(hw->nvm.ops));
  2578. /* Initialize skew-specific constants */
  2579. err = ei->get_invariants(hw);
  2580. if (err)
  2581. goto err_sw_init;
  2582. /* setup the private structure */
  2583. err = igb_sw_init(adapter);
  2584. if (err)
  2585. goto err_sw_init;
  2586. igb_get_bus_info_pcie(hw);
  2587. hw->phy.autoneg_wait_to_complete = false;
  2588. /* Copper options */
  2589. if (hw->phy.media_type == e1000_media_type_copper) {
  2590. hw->phy.mdix = AUTO_ALL_MODES;
  2591. hw->phy.disable_polarity_correction = false;
  2592. hw->phy.ms_type = e1000_ms_hw_default;
  2593. }
  2594. if (igb_check_reset_block(hw))
  2595. dev_info(&pdev->dev,
  2596. "PHY reset is blocked due to SOL/IDER session.\n");
  2597. /* features is initialized to 0 in allocation, it might have bits
  2598. * set by igb_sw_init so we should use an or instead of an
  2599. * assignment.
  2600. */
  2601. netdev->features |= NETIF_F_SG |
  2602. NETIF_F_TSO |
  2603. NETIF_F_TSO6 |
  2604. NETIF_F_RXHASH |
  2605. NETIF_F_RXCSUM |
  2606. NETIF_F_HW_CSUM;
  2607. if (hw->mac.type >= e1000_82576)
  2608. netdev->features |= NETIF_F_SCTP_CRC;
  2609. if (hw->mac.type >= e1000_i350)
  2610. netdev->features |= NETIF_F_HW_TC;
  2611. #define IGB_GSO_PARTIAL_FEATURES (NETIF_F_GSO_GRE | \
  2612. NETIF_F_GSO_GRE_CSUM | \
  2613. NETIF_F_GSO_IPXIP4 | \
  2614. NETIF_F_GSO_IPXIP6 | \
  2615. NETIF_F_GSO_UDP_TUNNEL | \
  2616. NETIF_F_GSO_UDP_TUNNEL_CSUM)
  2617. netdev->gso_partial_features = IGB_GSO_PARTIAL_FEATURES;
  2618. netdev->features |= NETIF_F_GSO_PARTIAL | IGB_GSO_PARTIAL_FEATURES;
  2619. /* copy netdev features into list of user selectable features */
  2620. netdev->hw_features |= netdev->features |
  2621. NETIF_F_HW_VLAN_CTAG_RX |
  2622. NETIF_F_HW_VLAN_CTAG_TX |
  2623. NETIF_F_RXALL;
  2624. if (hw->mac.type >= e1000_i350)
  2625. netdev->hw_features |= NETIF_F_NTUPLE;
  2626. if (pci_using_dac)
  2627. netdev->features |= NETIF_F_HIGHDMA;
  2628. netdev->vlan_features |= netdev->features | NETIF_F_TSO_MANGLEID;
  2629. netdev->mpls_features |= NETIF_F_HW_CSUM;
  2630. netdev->hw_enc_features |= netdev->vlan_features;
  2631. /* set this bit last since it cannot be part of vlan_features */
  2632. netdev->features |= NETIF_F_HW_VLAN_CTAG_FILTER |
  2633. NETIF_F_HW_VLAN_CTAG_RX |
  2634. NETIF_F_HW_VLAN_CTAG_TX;
  2635. netdev->priv_flags |= IFF_SUPP_NOFCS;
  2636. netdev->priv_flags |= IFF_UNICAST_FLT;
  2637. /* MTU range: 68 - 9216 */
  2638. netdev->min_mtu = ETH_MIN_MTU;
  2639. netdev->max_mtu = MAX_STD_JUMBO_FRAME_SIZE;
  2640. adapter->en_mng_pt = igb_enable_mng_pass_thru(hw);
  2641. /* before reading the NVM, reset the controller to put the device in a
  2642. * known good starting state
  2643. */
  2644. hw->mac.ops.reset_hw(hw);
  2645. /* make sure the NVM is good , i211/i210 parts can have special NVM
  2646. * that doesn't contain a checksum
  2647. */
  2648. switch (hw->mac.type) {
  2649. case e1000_i210:
  2650. case e1000_i211:
  2651. if (igb_get_flash_presence_i210(hw)) {
  2652. if (hw->nvm.ops.validate(hw) < 0) {
  2653. dev_err(&pdev->dev,
  2654. "The NVM Checksum Is Not Valid\n");
  2655. err = -EIO;
  2656. goto err_eeprom;
  2657. }
  2658. }
  2659. break;
  2660. default:
  2661. if (hw->nvm.ops.validate(hw) < 0) {
  2662. dev_err(&pdev->dev, "The NVM Checksum Is Not Valid\n");
  2663. err = -EIO;
  2664. goto err_eeprom;
  2665. }
  2666. break;
  2667. }
  2668. if (eth_platform_get_mac_address(&pdev->dev, hw->mac.addr)) {
  2669. /* copy the MAC address out of the NVM */
  2670. if (hw->mac.ops.read_mac_addr(hw))
  2671. dev_err(&pdev->dev, "NVM Read Error\n");
  2672. }
  2673. memcpy(netdev->dev_addr, hw->mac.addr, netdev->addr_len);
  2674. if (!is_valid_ether_addr(netdev->dev_addr)) {
  2675. dev_err(&pdev->dev, "Invalid MAC Address\n");
  2676. err = -EIO;
  2677. goto err_eeprom;
  2678. }
  2679. igb_set_default_mac_filter(adapter);
  2680. /* get firmware version for ethtool -i */
  2681. igb_set_fw_version(adapter);
  2682. /* configure RXPBSIZE and TXPBSIZE */
  2683. if (hw->mac.type == e1000_i210) {
  2684. wr32(E1000_RXPBS, I210_RXPBSIZE_DEFAULT);
  2685. wr32(E1000_TXPBS, I210_TXPBSIZE_DEFAULT);
  2686. }
  2687. timer_setup(&adapter->watchdog_timer, igb_watchdog, 0);
  2688. timer_setup(&adapter->phy_info_timer, igb_update_phy_info, 0);
  2689. INIT_WORK(&adapter->reset_task, igb_reset_task);
  2690. INIT_WORK(&adapter->watchdog_task, igb_watchdog_task);
  2691. /* Initialize link properties that are user-changeable */
  2692. adapter->fc_autoneg = true;
  2693. hw->mac.autoneg = true;
  2694. hw->phy.autoneg_advertised = 0x2f;
  2695. hw->fc.requested_mode = e1000_fc_default;
  2696. hw->fc.current_mode = e1000_fc_default;
  2697. igb_validate_mdi_setting(hw);
  2698. /* By default, support wake on port A */
  2699. if (hw->bus.func == 0)
  2700. adapter->flags |= IGB_FLAG_WOL_SUPPORTED;
  2701. /* Check the NVM for wake support on non-port A ports */
  2702. if (hw->mac.type >= e1000_82580)
  2703. hw->nvm.ops.read(hw, NVM_INIT_CONTROL3_PORT_A +
  2704. NVM_82580_LAN_FUNC_OFFSET(hw->bus.func), 1,
  2705. &eeprom_data);
  2706. else if (hw->bus.func == 1)
  2707. hw->nvm.ops.read(hw, NVM_INIT_CONTROL3_PORT_B, 1, &eeprom_data);
  2708. if (eeprom_data & IGB_EEPROM_APME)
  2709. adapter->flags |= IGB_FLAG_WOL_SUPPORTED;
  2710. /* now that we have the eeprom settings, apply the special cases where
  2711. * the eeprom may be wrong or the board simply won't support wake on
  2712. * lan on a particular port
  2713. */
  2714. switch (pdev->device) {
  2715. case E1000_DEV_ID_82575GB_QUAD_COPPER:
  2716. adapter->flags &= ~IGB_FLAG_WOL_SUPPORTED;
  2717. break;
  2718. case E1000_DEV_ID_82575EB_FIBER_SERDES:
  2719. case E1000_DEV_ID_82576_FIBER:
  2720. case E1000_DEV_ID_82576_SERDES:
  2721. /* Wake events only supported on port A for dual fiber
  2722. * regardless of eeprom setting
  2723. */
  2724. if (rd32(E1000_STATUS) & E1000_STATUS_FUNC_1)
  2725. adapter->flags &= ~IGB_FLAG_WOL_SUPPORTED;
  2726. break;
  2727. case E1000_DEV_ID_82576_QUAD_COPPER:
  2728. case E1000_DEV_ID_82576_QUAD_COPPER_ET2:
  2729. /* if quad port adapter, disable WoL on all but port A */
  2730. if (global_quad_port_a != 0)
  2731. adapter->flags &= ~IGB_FLAG_WOL_SUPPORTED;
  2732. else
  2733. adapter->flags |= IGB_FLAG_QUAD_PORT_A;
  2734. /* Reset for multiple quad port adapters */
  2735. if (++global_quad_port_a == 4)
  2736. global_quad_port_a = 0;
  2737. break;
  2738. default:
  2739. /* If the device can't wake, don't set software support */
  2740. if (!device_can_wakeup(&adapter->pdev->dev))
  2741. adapter->flags &= ~IGB_FLAG_WOL_SUPPORTED;
  2742. }
  2743. /* initialize the wol settings based on the eeprom settings */
  2744. if (adapter->flags & IGB_FLAG_WOL_SUPPORTED)
  2745. adapter->wol |= E1000_WUFC_MAG;
  2746. /* Some vendors want WoL disabled by default, but still supported */
  2747. if ((hw->mac.type == e1000_i350) &&
  2748. (pdev->subsystem_vendor == PCI_VENDOR_ID_HP)) {
  2749. adapter->flags |= IGB_FLAG_WOL_SUPPORTED;
  2750. adapter->wol = 0;
  2751. }
  2752. /* Some vendors want the ability to Use the EEPROM setting as
  2753. * enable/disable only, and not for capability
  2754. */
  2755. if (((hw->mac.type == e1000_i350) ||
  2756. (hw->mac.type == e1000_i354)) &&
  2757. (pdev->subsystem_vendor == PCI_VENDOR_ID_DELL)) {
  2758. adapter->flags |= IGB_FLAG_WOL_SUPPORTED;
  2759. adapter->wol = 0;
  2760. }
  2761. if (hw->mac.type == e1000_i350) {
  2762. if (((pdev->subsystem_device == 0x5001) ||
  2763. (pdev->subsystem_device == 0x5002)) &&
  2764. (hw->bus.func == 0)) {
  2765. adapter->flags |= IGB_FLAG_WOL_SUPPORTED;
  2766. adapter->wol = 0;
  2767. }
  2768. if (pdev->subsystem_device == 0x1F52)
  2769. adapter->flags |= IGB_FLAG_WOL_SUPPORTED;
  2770. }
  2771. device_set_wakeup_enable(&adapter->pdev->dev,
  2772. adapter->flags & IGB_FLAG_WOL_SUPPORTED);
  2773. /* reset the hardware with the new settings */
  2774. igb_reset(adapter);
  2775. /* Init the I2C interface */
  2776. err = igb_init_i2c(adapter);
  2777. if (err) {
  2778. dev_err(&pdev->dev, "failed to init i2c interface\n");
  2779. goto err_eeprom;
  2780. }
  2781. /* let the f/w know that the h/w is now under the control of the
  2782. * driver.
  2783. */
  2784. igb_get_hw_control(adapter);
  2785. strcpy(netdev->name, "eth%d");
  2786. err = register_netdev(netdev);
  2787. if (err)
  2788. goto err_register;
  2789. /* carrier off reporting is important to ethtool even BEFORE open */
  2790. netif_carrier_off(netdev);
  2791. #ifdef CONFIG_IGB_DCA
  2792. if (dca_add_requester(&pdev->dev) == 0) {
  2793. adapter->flags |= IGB_FLAG_DCA_ENABLED;
  2794. dev_info(&pdev->dev, "DCA enabled\n");
  2795. igb_setup_dca(adapter);
  2796. }
  2797. #endif
  2798. #ifdef CONFIG_IGB_HWMON
  2799. /* Initialize the thermal sensor on i350 devices. */
  2800. if (hw->mac.type == e1000_i350 && hw->bus.func == 0) {
  2801. u16 ets_word;
  2802. /* Read the NVM to determine if this i350 device supports an
  2803. * external thermal sensor.
  2804. */
  2805. hw->nvm.ops.read(hw, NVM_ETS_CFG, 1, &ets_word);
  2806. if (ets_word != 0x0000 && ets_word != 0xFFFF)
  2807. adapter->ets = true;
  2808. else
  2809. adapter->ets = false;
  2810. if (igb_sysfs_init(adapter))
  2811. dev_err(&pdev->dev,
  2812. "failed to allocate sysfs resources\n");
  2813. } else {
  2814. adapter->ets = false;
  2815. }
  2816. #endif
  2817. /* Check if Media Autosense is enabled */
  2818. adapter->ei = *ei;
  2819. if (hw->dev_spec._82575.mas_capable)
  2820. igb_init_mas(adapter);
  2821. /* do hw tstamp init after resetting */
  2822. igb_ptp_init(adapter);
  2823. dev_info(&pdev->dev, "Intel(R) Gigabit Ethernet Network Connection\n");
  2824. /* print bus type/speed/width info, not applicable to i354 */
  2825. if (hw->mac.type != e1000_i354) {
  2826. dev_info(&pdev->dev, "%s: (PCIe:%s:%s) %pM\n",
  2827. netdev->name,
  2828. ((hw->bus.speed == e1000_bus_speed_2500) ? "2.5Gb/s" :
  2829. (hw->bus.speed == e1000_bus_speed_5000) ? "5.0Gb/s" :
  2830. "unknown"),
  2831. ((hw->bus.width == e1000_bus_width_pcie_x4) ?
  2832. "Width x4" :
  2833. (hw->bus.width == e1000_bus_width_pcie_x2) ?
  2834. "Width x2" :
  2835. (hw->bus.width == e1000_bus_width_pcie_x1) ?
  2836. "Width x1" : "unknown"), netdev->dev_addr);
  2837. }
  2838. if ((hw->mac.type >= e1000_i210 ||
  2839. igb_get_flash_presence_i210(hw))) {
  2840. ret_val = igb_read_part_string(hw, part_str,
  2841. E1000_PBANUM_LENGTH);
  2842. } else {
  2843. ret_val = -E1000_ERR_INVM_VALUE_NOT_FOUND;
  2844. }
  2845. if (ret_val)
  2846. strcpy(part_str, "Unknown");
  2847. dev_info(&pdev->dev, "%s: PBA No: %s\n", netdev->name, part_str);
  2848. dev_info(&pdev->dev,
  2849. "Using %s interrupts. %d rx queue(s), %d tx queue(s)\n",
  2850. (adapter->flags & IGB_FLAG_HAS_MSIX) ? "MSI-X" :
  2851. (adapter->flags & IGB_FLAG_HAS_MSI) ? "MSI" : "legacy",
  2852. adapter->num_rx_queues, adapter->num_tx_queues);
  2853. if (hw->phy.media_type == e1000_media_type_copper) {
  2854. switch (hw->mac.type) {
  2855. case e1000_i350:
  2856. case e1000_i210:
  2857. case e1000_i211:
  2858. /* Enable EEE for internal copper PHY devices */
  2859. err = igb_set_eee_i350(hw, true, true);
  2860. if ((!err) &&
  2861. (!hw->dev_spec._82575.eee_disable)) {
  2862. adapter->eee_advert =
  2863. MDIO_EEE_100TX | MDIO_EEE_1000T;
  2864. adapter->flags |= IGB_FLAG_EEE;
  2865. }
  2866. break;
  2867. case e1000_i354:
  2868. if ((rd32(E1000_CTRL_EXT) &
  2869. E1000_CTRL_EXT_LINK_MODE_SGMII)) {
  2870. err = igb_set_eee_i354(hw, true, true);
  2871. if ((!err) &&
  2872. (!hw->dev_spec._82575.eee_disable)) {
  2873. adapter->eee_advert =
  2874. MDIO_EEE_100TX | MDIO_EEE_1000T;
  2875. adapter->flags |= IGB_FLAG_EEE;
  2876. }
  2877. }
  2878. break;
  2879. default:
  2880. break;
  2881. }
  2882. }
  2883. pm_runtime_put_noidle(&pdev->dev);
  2884. return 0;
  2885. err_register:
  2886. igb_release_hw_control(adapter);
  2887. memset(&adapter->i2c_adap, 0, sizeof(adapter->i2c_adap));
  2888. err_eeprom:
  2889. if (!igb_check_reset_block(hw))
  2890. igb_reset_phy(hw);
  2891. if (hw->flash_address)
  2892. iounmap(hw->flash_address);
  2893. err_sw_init:
  2894. kfree(adapter->mac_table);
  2895. kfree(adapter->shadow_vfta);
  2896. igb_clear_interrupt_scheme(adapter);
  2897. #ifdef CONFIG_PCI_IOV
  2898. igb_disable_sriov(pdev);
  2899. #endif
  2900. pci_iounmap(pdev, adapter->io_addr);
  2901. err_ioremap:
  2902. free_netdev(netdev);
  2903. err_alloc_etherdev:
  2904. pci_release_mem_regions(pdev);
  2905. err_pci_reg:
  2906. err_dma:
  2907. pci_disable_device(pdev);
  2908. return err;
  2909. }
  2910. #ifdef CONFIG_PCI_IOV
  2911. static int igb_disable_sriov(struct pci_dev *pdev)
  2912. {
  2913. struct net_device *netdev = pci_get_drvdata(pdev);
  2914. struct igb_adapter *adapter = netdev_priv(netdev);
  2915. struct e1000_hw *hw = &adapter->hw;
  2916. /* reclaim resources allocated to VFs */
  2917. if (adapter->vf_data) {
  2918. /* disable iov and allow time for transactions to clear */
  2919. if (pci_vfs_assigned(pdev)) {
  2920. dev_warn(&pdev->dev,
  2921. "Cannot deallocate SR-IOV virtual functions while they are assigned - VFs will not be deallocated\n");
  2922. return -EPERM;
  2923. } else {
  2924. pci_disable_sriov(pdev);
  2925. msleep(500);
  2926. }
  2927. kfree(adapter->vf_mac_list);
  2928. adapter->vf_mac_list = NULL;
  2929. kfree(adapter->vf_data);
  2930. adapter->vf_data = NULL;
  2931. adapter->vfs_allocated_count = 0;
  2932. wr32(E1000_IOVCTL, E1000_IOVCTL_REUSE_VFQ);
  2933. wrfl();
  2934. msleep(100);
  2935. dev_info(&pdev->dev, "IOV Disabled\n");
  2936. /* Re-enable DMA Coalescing flag since IOV is turned off */
  2937. adapter->flags |= IGB_FLAG_DMAC;
  2938. }
  2939. return 0;
  2940. }
  2941. static int igb_enable_sriov(struct pci_dev *pdev, int num_vfs)
  2942. {
  2943. struct net_device *netdev = pci_get_drvdata(pdev);
  2944. struct igb_adapter *adapter = netdev_priv(netdev);
  2945. int old_vfs = pci_num_vf(pdev);
  2946. struct vf_mac_filter *mac_list;
  2947. int err = 0;
  2948. int num_vf_mac_filters, i;
  2949. if (!(adapter->flags & IGB_FLAG_HAS_MSIX) || num_vfs > 7) {
  2950. err = -EPERM;
  2951. goto out;
  2952. }
  2953. if (!num_vfs)
  2954. goto out;
  2955. if (old_vfs) {
  2956. dev_info(&pdev->dev, "%d pre-allocated VFs found - override max_vfs setting of %d\n",
  2957. old_vfs, max_vfs);
  2958. adapter->vfs_allocated_count = old_vfs;
  2959. } else
  2960. adapter->vfs_allocated_count = num_vfs;
  2961. adapter->vf_data = kcalloc(adapter->vfs_allocated_count,
  2962. sizeof(struct vf_data_storage), GFP_KERNEL);
  2963. /* if allocation failed then we do not support SR-IOV */
  2964. if (!adapter->vf_data) {
  2965. adapter->vfs_allocated_count = 0;
  2966. err = -ENOMEM;
  2967. goto out;
  2968. }
  2969. /* Due to the limited number of RAR entries calculate potential
  2970. * number of MAC filters available for the VFs. Reserve entries
  2971. * for PF default MAC, PF MAC filters and at least one RAR entry
  2972. * for each VF for VF MAC.
  2973. */
  2974. num_vf_mac_filters = adapter->hw.mac.rar_entry_count -
  2975. (1 + IGB_PF_MAC_FILTERS_RESERVED +
  2976. adapter->vfs_allocated_count);
  2977. adapter->vf_mac_list = kcalloc(num_vf_mac_filters,
  2978. sizeof(struct vf_mac_filter),
  2979. GFP_KERNEL);
  2980. mac_list = adapter->vf_mac_list;
  2981. INIT_LIST_HEAD(&adapter->vf_macs.l);
  2982. if (adapter->vf_mac_list) {
  2983. /* Initialize list of VF MAC filters */
  2984. for (i = 0; i < num_vf_mac_filters; i++) {
  2985. mac_list->vf = -1;
  2986. mac_list->free = true;
  2987. list_add(&mac_list->l, &adapter->vf_macs.l);
  2988. mac_list++;
  2989. }
  2990. } else {
  2991. /* If we could not allocate memory for the VF MAC filters
  2992. * we can continue without this feature but warn user.
  2993. */
  2994. dev_err(&pdev->dev,
  2995. "Unable to allocate memory for VF MAC filter list\n");
  2996. }
  2997. /* only call pci_enable_sriov() if no VFs are allocated already */
  2998. if (!old_vfs) {
  2999. err = pci_enable_sriov(pdev, adapter->vfs_allocated_count);
  3000. if (err)
  3001. goto err_out;
  3002. }
  3003. dev_info(&pdev->dev, "%d VFs allocated\n",
  3004. adapter->vfs_allocated_count);
  3005. for (i = 0; i < adapter->vfs_allocated_count; i++)
  3006. igb_vf_configure(adapter, i);
  3007. /* DMA Coalescing is not supported in IOV mode. */
  3008. adapter->flags &= ~IGB_FLAG_DMAC;
  3009. goto out;
  3010. err_out:
  3011. kfree(adapter->vf_mac_list);
  3012. adapter->vf_mac_list = NULL;
  3013. kfree(adapter->vf_data);
  3014. adapter->vf_data = NULL;
  3015. adapter->vfs_allocated_count = 0;
  3016. out:
  3017. return err;
  3018. }
  3019. #endif
  3020. /**
  3021. * igb_remove_i2c - Cleanup I2C interface
  3022. * @adapter: pointer to adapter structure
  3023. **/
  3024. static void igb_remove_i2c(struct igb_adapter *adapter)
  3025. {
  3026. /* free the adapter bus structure */
  3027. i2c_del_adapter(&adapter->i2c_adap);
  3028. }
  3029. /**
  3030. * igb_remove - Device Removal Routine
  3031. * @pdev: PCI device information struct
  3032. *
  3033. * igb_remove is called by the PCI subsystem to alert the driver
  3034. * that it should release a PCI device. The could be caused by a
  3035. * Hot-Plug event, or because the driver is going to be removed from
  3036. * memory.
  3037. **/
  3038. static void igb_remove(struct pci_dev *pdev)
  3039. {
  3040. struct net_device *netdev = pci_get_drvdata(pdev);
  3041. struct igb_adapter *adapter = netdev_priv(netdev);
  3042. struct e1000_hw *hw = &adapter->hw;
  3043. pm_runtime_get_noresume(&pdev->dev);
  3044. #ifdef CONFIG_IGB_HWMON
  3045. igb_sysfs_exit(adapter);
  3046. #endif
  3047. igb_remove_i2c(adapter);
  3048. igb_ptp_stop(adapter);
  3049. /* The watchdog timer may be rescheduled, so explicitly
  3050. * disable watchdog from being rescheduled.
  3051. */
  3052. set_bit(__IGB_DOWN, &adapter->state);
  3053. del_timer_sync(&adapter->watchdog_timer);
  3054. del_timer_sync(&adapter->phy_info_timer);
  3055. cancel_work_sync(&adapter->reset_task);
  3056. cancel_work_sync(&adapter->watchdog_task);
  3057. #ifdef CONFIG_IGB_DCA
  3058. if (adapter->flags & IGB_FLAG_DCA_ENABLED) {
  3059. dev_info(&pdev->dev, "DCA disabled\n");
  3060. dca_remove_requester(&pdev->dev);
  3061. adapter->flags &= ~IGB_FLAG_DCA_ENABLED;
  3062. wr32(E1000_DCA_CTRL, E1000_DCA_CTRL_DCA_MODE_DISABLE);
  3063. }
  3064. #endif
  3065. /* Release control of h/w to f/w. If f/w is AMT enabled, this
  3066. * would have already happened in close and is redundant.
  3067. */
  3068. igb_release_hw_control(adapter);
  3069. #ifdef CONFIG_PCI_IOV
  3070. igb_disable_sriov(pdev);
  3071. #endif
  3072. unregister_netdev(netdev);
  3073. igb_clear_interrupt_scheme(adapter);
  3074. pci_iounmap(pdev, adapter->io_addr);
  3075. if (hw->flash_address)
  3076. iounmap(hw->flash_address);
  3077. pci_release_mem_regions(pdev);
  3078. kfree(adapter->mac_table);
  3079. kfree(adapter->shadow_vfta);
  3080. free_netdev(netdev);
  3081. pci_disable_pcie_error_reporting(pdev);
  3082. pci_disable_device(pdev);
  3083. }
  3084. /**
  3085. * igb_probe_vfs - Initialize vf data storage and add VFs to pci config space
  3086. * @adapter: board private structure to initialize
  3087. *
  3088. * This function initializes the vf specific data storage and then attempts to
  3089. * allocate the VFs. The reason for ordering it this way is because it is much
  3090. * mor expensive time wise to disable SR-IOV than it is to allocate and free
  3091. * the memory for the VFs.
  3092. **/
  3093. static void igb_probe_vfs(struct igb_adapter *adapter)
  3094. {
  3095. #ifdef CONFIG_PCI_IOV
  3096. struct pci_dev *pdev = adapter->pdev;
  3097. struct e1000_hw *hw = &adapter->hw;
  3098. /* Virtualization features not supported on i210 family. */
  3099. if ((hw->mac.type == e1000_i210) || (hw->mac.type == e1000_i211))
  3100. return;
  3101. /* Of the below we really only want the effect of getting
  3102. * IGB_FLAG_HAS_MSIX set (if available), without which
  3103. * igb_enable_sriov() has no effect.
  3104. */
  3105. igb_set_interrupt_capability(adapter, true);
  3106. igb_reset_interrupt_capability(adapter);
  3107. pci_sriov_set_totalvfs(pdev, 7);
  3108. igb_enable_sriov(pdev, max_vfs);
  3109. #endif /* CONFIG_PCI_IOV */
  3110. }
  3111. unsigned int igb_get_max_rss_queues(struct igb_adapter *adapter)
  3112. {
  3113. struct e1000_hw *hw = &adapter->hw;
  3114. unsigned int max_rss_queues;
  3115. /* Determine the maximum number of RSS queues supported. */
  3116. switch (hw->mac.type) {
  3117. case e1000_i211:
  3118. max_rss_queues = IGB_MAX_RX_QUEUES_I211;
  3119. break;
  3120. case e1000_82575:
  3121. case e1000_i210:
  3122. max_rss_queues = IGB_MAX_RX_QUEUES_82575;
  3123. break;
  3124. case e1000_i350:
  3125. /* I350 cannot do RSS and SR-IOV at the same time */
  3126. if (!!adapter->vfs_allocated_count) {
  3127. max_rss_queues = 1;
  3128. break;
  3129. }
  3130. /* fall through */
  3131. case e1000_82576:
  3132. if (!!adapter->vfs_allocated_count) {
  3133. max_rss_queues = 2;
  3134. break;
  3135. }
  3136. /* fall through */
  3137. case e1000_82580:
  3138. case e1000_i354:
  3139. default:
  3140. max_rss_queues = IGB_MAX_RX_QUEUES;
  3141. break;
  3142. }
  3143. return max_rss_queues;
  3144. }
  3145. static void igb_init_queue_configuration(struct igb_adapter *adapter)
  3146. {
  3147. u32 max_rss_queues;
  3148. max_rss_queues = igb_get_max_rss_queues(adapter);
  3149. adapter->rss_queues = min_t(u32, max_rss_queues, num_online_cpus());
  3150. igb_set_flag_queue_pairs(adapter, max_rss_queues);
  3151. }
  3152. void igb_set_flag_queue_pairs(struct igb_adapter *adapter,
  3153. const u32 max_rss_queues)
  3154. {
  3155. struct e1000_hw *hw = &adapter->hw;
  3156. /* Determine if we need to pair queues. */
  3157. switch (hw->mac.type) {
  3158. case e1000_82575:
  3159. case e1000_i211:
  3160. /* Device supports enough interrupts without queue pairing. */
  3161. break;
  3162. case e1000_82576:
  3163. case e1000_82580:
  3164. case e1000_i350:
  3165. case e1000_i354:
  3166. case e1000_i210:
  3167. default:
  3168. /* If rss_queues > half of max_rss_queues, pair the queues in
  3169. * order to conserve interrupts due to limited supply.
  3170. */
  3171. if (adapter->rss_queues > (max_rss_queues / 2))
  3172. adapter->flags |= IGB_FLAG_QUEUE_PAIRS;
  3173. else
  3174. adapter->flags &= ~IGB_FLAG_QUEUE_PAIRS;
  3175. break;
  3176. }
  3177. }
  3178. /**
  3179. * igb_sw_init - Initialize general software structures (struct igb_adapter)
  3180. * @adapter: board private structure to initialize
  3181. *
  3182. * igb_sw_init initializes the Adapter private data structure.
  3183. * Fields are initialized based on PCI device information and
  3184. * OS network device settings (MTU size).
  3185. **/
  3186. static int igb_sw_init(struct igb_adapter *adapter)
  3187. {
  3188. struct e1000_hw *hw = &adapter->hw;
  3189. struct net_device *netdev = adapter->netdev;
  3190. struct pci_dev *pdev = adapter->pdev;
  3191. pci_read_config_word(pdev, PCI_COMMAND, &hw->bus.pci_cmd_word);
  3192. /* set default ring sizes */
  3193. adapter->tx_ring_count = IGB_DEFAULT_TXD;
  3194. adapter->rx_ring_count = IGB_DEFAULT_RXD;
  3195. /* set default ITR values */
  3196. adapter->rx_itr_setting = IGB_DEFAULT_ITR;
  3197. adapter->tx_itr_setting = IGB_DEFAULT_ITR;
  3198. /* set default work limits */
  3199. adapter->tx_work_limit = IGB_DEFAULT_TX_WORK;
  3200. adapter->max_frame_size = netdev->mtu + ETH_HLEN + ETH_FCS_LEN +
  3201. VLAN_HLEN;
  3202. adapter->min_frame_size = ETH_ZLEN + ETH_FCS_LEN;
  3203. spin_lock_init(&adapter->nfc_lock);
  3204. spin_lock_init(&adapter->stats64_lock);
  3205. #ifdef CONFIG_PCI_IOV
  3206. switch (hw->mac.type) {
  3207. case e1000_82576:
  3208. case e1000_i350:
  3209. if (max_vfs > 7) {
  3210. dev_warn(&pdev->dev,
  3211. "Maximum of 7 VFs per PF, using max\n");
  3212. max_vfs = adapter->vfs_allocated_count = 7;
  3213. } else
  3214. adapter->vfs_allocated_count = max_vfs;
  3215. if (adapter->vfs_allocated_count)
  3216. dev_warn(&pdev->dev,
  3217. "Enabling SR-IOV VFs using the module parameter is deprecated - please use the pci sysfs interface.\n");
  3218. break;
  3219. default:
  3220. break;
  3221. }
  3222. #endif /* CONFIG_PCI_IOV */
  3223. /* Assume MSI-X interrupts, will be checked during IRQ allocation */
  3224. adapter->flags |= IGB_FLAG_HAS_MSIX;
  3225. adapter->mac_table = kcalloc(hw->mac.rar_entry_count,
  3226. sizeof(struct igb_mac_addr),
  3227. GFP_ATOMIC);
  3228. if (!adapter->mac_table)
  3229. return -ENOMEM;
  3230. igb_probe_vfs(adapter);
  3231. igb_init_queue_configuration(adapter);
  3232. /* Setup and initialize a copy of the hw vlan table array */
  3233. adapter->shadow_vfta = kcalloc(E1000_VLAN_FILTER_TBL_SIZE, sizeof(u32),
  3234. GFP_ATOMIC);
  3235. if (!adapter->shadow_vfta)
  3236. return -ENOMEM;
  3237. /* This call may decrease the number of queues */
  3238. if (igb_init_interrupt_scheme(adapter, true)) {
  3239. dev_err(&pdev->dev, "Unable to allocate memory for queues\n");
  3240. return -ENOMEM;
  3241. }
  3242. /* Explicitly disable IRQ since the NIC can be in any state. */
  3243. igb_irq_disable(adapter);
  3244. if (hw->mac.type >= e1000_i350)
  3245. adapter->flags &= ~IGB_FLAG_DMAC;
  3246. set_bit(__IGB_DOWN, &adapter->state);
  3247. return 0;
  3248. }
  3249. /**
  3250. * igb_open - Called when a network interface is made active
  3251. * @netdev: network interface device structure
  3252. *
  3253. * Returns 0 on success, negative value on failure
  3254. *
  3255. * The open entry point is called when a network interface is made
  3256. * active by the system (IFF_UP). At this point all resources needed
  3257. * for transmit and receive operations are allocated, the interrupt
  3258. * handler is registered with the OS, the watchdog timer is started,
  3259. * and the stack is notified that the interface is ready.
  3260. **/
  3261. static int __igb_open(struct net_device *netdev, bool resuming)
  3262. {
  3263. struct igb_adapter *adapter = netdev_priv(netdev);
  3264. struct e1000_hw *hw = &adapter->hw;
  3265. struct pci_dev *pdev = adapter->pdev;
  3266. int err;
  3267. int i;
  3268. /* disallow open during test */
  3269. if (test_bit(__IGB_TESTING, &adapter->state)) {
  3270. WARN_ON(resuming);
  3271. return -EBUSY;
  3272. }
  3273. if (!resuming)
  3274. pm_runtime_get_sync(&pdev->dev);
  3275. netif_carrier_off(netdev);
  3276. /* allocate transmit descriptors */
  3277. err = igb_setup_all_tx_resources(adapter);
  3278. if (err)
  3279. goto err_setup_tx;
  3280. /* allocate receive descriptors */
  3281. err = igb_setup_all_rx_resources(adapter);
  3282. if (err)
  3283. goto err_setup_rx;
  3284. igb_power_up_link(adapter);
  3285. /* before we allocate an interrupt, we must be ready to handle it.
  3286. * Setting DEBUG_SHIRQ in the kernel makes it fire an interrupt
  3287. * as soon as we call pci_request_irq, so we have to setup our
  3288. * clean_rx handler before we do so.
  3289. */
  3290. igb_configure(adapter);
  3291. err = igb_request_irq(adapter);
  3292. if (err)
  3293. goto err_req_irq;
  3294. /* Notify the stack of the actual queue counts. */
  3295. err = netif_set_real_num_tx_queues(adapter->netdev,
  3296. adapter->num_tx_queues);
  3297. if (err)
  3298. goto err_set_queues;
  3299. err = netif_set_real_num_rx_queues(adapter->netdev,
  3300. adapter->num_rx_queues);
  3301. if (err)
  3302. goto err_set_queues;
  3303. /* From here on the code is the same as igb_up() */
  3304. clear_bit(__IGB_DOWN, &adapter->state);
  3305. for (i = 0; i < adapter->num_q_vectors; i++)
  3306. napi_enable(&(adapter->q_vector[i]->napi));
  3307. /* Clear any pending interrupts. */
  3308. rd32(E1000_TSICR);
  3309. rd32(E1000_ICR);
  3310. igb_irq_enable(adapter);
  3311. /* notify VFs that reset has been completed */
  3312. if (adapter->vfs_allocated_count) {
  3313. u32 reg_data = rd32(E1000_CTRL_EXT);
  3314. reg_data |= E1000_CTRL_EXT_PFRSTD;
  3315. wr32(E1000_CTRL_EXT, reg_data);
  3316. }
  3317. netif_tx_start_all_queues(netdev);
  3318. if (!resuming)
  3319. pm_runtime_put(&pdev->dev);
  3320. /* start the watchdog. */
  3321. hw->mac.get_link_status = 1;
  3322. schedule_work(&adapter->watchdog_task);
  3323. return 0;
  3324. err_set_queues:
  3325. igb_free_irq(adapter);
  3326. err_req_irq:
  3327. igb_release_hw_control(adapter);
  3328. igb_power_down_link(adapter);
  3329. igb_free_all_rx_resources(adapter);
  3330. err_setup_rx:
  3331. igb_free_all_tx_resources(adapter);
  3332. err_setup_tx:
  3333. igb_reset(adapter);
  3334. if (!resuming)
  3335. pm_runtime_put(&pdev->dev);
  3336. return err;
  3337. }
  3338. int igb_open(struct net_device *netdev)
  3339. {
  3340. return __igb_open(netdev, false);
  3341. }
  3342. /**
  3343. * igb_close - Disables a network interface
  3344. * @netdev: network interface device structure
  3345. *
  3346. * Returns 0, this is not allowed to fail
  3347. *
  3348. * The close entry point is called when an interface is de-activated
  3349. * by the OS. The hardware is still under the driver's control, but
  3350. * needs to be disabled. A global MAC reset is issued to stop the
  3351. * hardware, and all transmit and receive resources are freed.
  3352. **/
  3353. static int __igb_close(struct net_device *netdev, bool suspending)
  3354. {
  3355. struct igb_adapter *adapter = netdev_priv(netdev);
  3356. struct pci_dev *pdev = adapter->pdev;
  3357. WARN_ON(test_bit(__IGB_RESETTING, &adapter->state));
  3358. if (!suspending)
  3359. pm_runtime_get_sync(&pdev->dev);
  3360. igb_down(adapter);
  3361. igb_free_irq(adapter);
  3362. igb_free_all_tx_resources(adapter);
  3363. igb_free_all_rx_resources(adapter);
  3364. if (!suspending)
  3365. pm_runtime_put_sync(&pdev->dev);
  3366. return 0;
  3367. }
  3368. int igb_close(struct net_device *netdev)
  3369. {
  3370. if (netif_device_present(netdev) || netdev->dismantle)
  3371. return __igb_close(netdev, false);
  3372. return 0;
  3373. }
  3374. /**
  3375. * igb_setup_tx_resources - allocate Tx resources (Descriptors)
  3376. * @tx_ring: tx descriptor ring (for a specific queue) to setup
  3377. *
  3378. * Return 0 on success, negative on failure
  3379. **/
  3380. int igb_setup_tx_resources(struct igb_ring *tx_ring)
  3381. {
  3382. struct device *dev = tx_ring->dev;
  3383. int size;
  3384. size = sizeof(struct igb_tx_buffer) * tx_ring->count;
  3385. tx_ring->tx_buffer_info = vmalloc(size);
  3386. if (!tx_ring->tx_buffer_info)
  3387. goto err;
  3388. /* round up to nearest 4K */
  3389. tx_ring->size = tx_ring->count * sizeof(union e1000_adv_tx_desc);
  3390. tx_ring->size = ALIGN(tx_ring->size, 4096);
  3391. tx_ring->desc = dma_alloc_coherent(dev, tx_ring->size,
  3392. &tx_ring->dma, GFP_KERNEL);
  3393. if (!tx_ring->desc)
  3394. goto err;
  3395. tx_ring->next_to_use = 0;
  3396. tx_ring->next_to_clean = 0;
  3397. return 0;
  3398. err:
  3399. vfree(tx_ring->tx_buffer_info);
  3400. tx_ring->tx_buffer_info = NULL;
  3401. dev_err(dev, "Unable to allocate memory for the Tx descriptor ring\n");
  3402. return -ENOMEM;
  3403. }
  3404. /**
  3405. * igb_setup_all_tx_resources - wrapper to allocate Tx resources
  3406. * (Descriptors) for all queues
  3407. * @adapter: board private structure
  3408. *
  3409. * Return 0 on success, negative on failure
  3410. **/
  3411. static int igb_setup_all_tx_resources(struct igb_adapter *adapter)
  3412. {
  3413. struct pci_dev *pdev = adapter->pdev;
  3414. int i, err = 0;
  3415. for (i = 0; i < adapter->num_tx_queues; i++) {
  3416. err = igb_setup_tx_resources(adapter->tx_ring[i]);
  3417. if (err) {
  3418. dev_err(&pdev->dev,
  3419. "Allocation for Tx Queue %u failed\n", i);
  3420. for (i--; i >= 0; i--)
  3421. igb_free_tx_resources(adapter->tx_ring[i]);
  3422. break;
  3423. }
  3424. }
  3425. return err;
  3426. }
  3427. /**
  3428. * igb_setup_tctl - configure the transmit control registers
  3429. * @adapter: Board private structure
  3430. **/
  3431. void igb_setup_tctl(struct igb_adapter *adapter)
  3432. {
  3433. struct e1000_hw *hw = &adapter->hw;
  3434. u32 tctl;
  3435. /* disable queue 0 which is enabled by default on 82575 and 82576 */
  3436. wr32(E1000_TXDCTL(0), 0);
  3437. /* Program the Transmit Control Register */
  3438. tctl = rd32(E1000_TCTL);
  3439. tctl &= ~E1000_TCTL_CT;
  3440. tctl |= E1000_TCTL_PSP | E1000_TCTL_RTLC |
  3441. (E1000_COLLISION_THRESHOLD << E1000_CT_SHIFT);
  3442. igb_config_collision_dist(hw);
  3443. /* Enable transmits */
  3444. tctl |= E1000_TCTL_EN;
  3445. wr32(E1000_TCTL, tctl);
  3446. }
  3447. /**
  3448. * igb_configure_tx_ring - Configure transmit ring after Reset
  3449. * @adapter: board private structure
  3450. * @ring: tx ring to configure
  3451. *
  3452. * Configure a transmit ring after a reset.
  3453. **/
  3454. void igb_configure_tx_ring(struct igb_adapter *adapter,
  3455. struct igb_ring *ring)
  3456. {
  3457. struct e1000_hw *hw = &adapter->hw;
  3458. u32 txdctl = 0;
  3459. u64 tdba = ring->dma;
  3460. int reg_idx = ring->reg_idx;
  3461. wr32(E1000_TDLEN(reg_idx),
  3462. ring->count * sizeof(union e1000_adv_tx_desc));
  3463. wr32(E1000_TDBAL(reg_idx),
  3464. tdba & 0x00000000ffffffffULL);
  3465. wr32(E1000_TDBAH(reg_idx), tdba >> 32);
  3466. ring->tail = adapter->io_addr + E1000_TDT(reg_idx);
  3467. wr32(E1000_TDH(reg_idx), 0);
  3468. writel(0, ring->tail);
  3469. txdctl |= IGB_TX_PTHRESH;
  3470. txdctl |= IGB_TX_HTHRESH << 8;
  3471. txdctl |= IGB_TX_WTHRESH << 16;
  3472. /* reinitialize tx_buffer_info */
  3473. memset(ring->tx_buffer_info, 0,
  3474. sizeof(struct igb_tx_buffer) * ring->count);
  3475. txdctl |= E1000_TXDCTL_QUEUE_ENABLE;
  3476. wr32(E1000_TXDCTL(reg_idx), txdctl);
  3477. }
  3478. /**
  3479. * igb_configure_tx - Configure transmit Unit after Reset
  3480. * @adapter: board private structure
  3481. *
  3482. * Configure the Tx unit of the MAC after a reset.
  3483. **/
  3484. static void igb_configure_tx(struct igb_adapter *adapter)
  3485. {
  3486. struct e1000_hw *hw = &adapter->hw;
  3487. int i;
  3488. /* disable the queues */
  3489. for (i = 0; i < adapter->num_tx_queues; i++)
  3490. wr32(E1000_TXDCTL(adapter->tx_ring[i]->reg_idx), 0);
  3491. wrfl();
  3492. usleep_range(10000, 20000);
  3493. for (i = 0; i < adapter->num_tx_queues; i++)
  3494. igb_configure_tx_ring(adapter, adapter->tx_ring[i]);
  3495. }
  3496. /**
  3497. * igb_setup_rx_resources - allocate Rx resources (Descriptors)
  3498. * @rx_ring: Rx descriptor ring (for a specific queue) to setup
  3499. *
  3500. * Returns 0 on success, negative on failure
  3501. **/
  3502. int igb_setup_rx_resources(struct igb_ring *rx_ring)
  3503. {
  3504. struct device *dev = rx_ring->dev;
  3505. int size;
  3506. size = sizeof(struct igb_rx_buffer) * rx_ring->count;
  3507. rx_ring->rx_buffer_info = vmalloc(size);
  3508. if (!rx_ring->rx_buffer_info)
  3509. goto err;
  3510. /* Round up to nearest 4K */
  3511. rx_ring->size = rx_ring->count * sizeof(union e1000_adv_rx_desc);
  3512. rx_ring->size = ALIGN(rx_ring->size, 4096);
  3513. rx_ring->desc = dma_alloc_coherent(dev, rx_ring->size,
  3514. &rx_ring->dma, GFP_KERNEL);
  3515. if (!rx_ring->desc)
  3516. goto err;
  3517. rx_ring->next_to_alloc = 0;
  3518. rx_ring->next_to_clean = 0;
  3519. rx_ring->next_to_use = 0;
  3520. return 0;
  3521. err:
  3522. vfree(rx_ring->rx_buffer_info);
  3523. rx_ring->rx_buffer_info = NULL;
  3524. dev_err(dev, "Unable to allocate memory for the Rx descriptor ring\n");
  3525. return -ENOMEM;
  3526. }
  3527. /**
  3528. * igb_setup_all_rx_resources - wrapper to allocate Rx resources
  3529. * (Descriptors) for all queues
  3530. * @adapter: board private structure
  3531. *
  3532. * Return 0 on success, negative on failure
  3533. **/
  3534. static int igb_setup_all_rx_resources(struct igb_adapter *adapter)
  3535. {
  3536. struct pci_dev *pdev = adapter->pdev;
  3537. int i, err = 0;
  3538. for (i = 0; i < adapter->num_rx_queues; i++) {
  3539. err = igb_setup_rx_resources(adapter->rx_ring[i]);
  3540. if (err) {
  3541. dev_err(&pdev->dev,
  3542. "Allocation for Rx Queue %u failed\n", i);
  3543. for (i--; i >= 0; i--)
  3544. igb_free_rx_resources(adapter->rx_ring[i]);
  3545. break;
  3546. }
  3547. }
  3548. return err;
  3549. }
  3550. /**
  3551. * igb_setup_mrqc - configure the multiple receive queue control registers
  3552. * @adapter: Board private structure
  3553. **/
  3554. static void igb_setup_mrqc(struct igb_adapter *adapter)
  3555. {
  3556. struct e1000_hw *hw = &adapter->hw;
  3557. u32 mrqc, rxcsum;
  3558. u32 j, num_rx_queues;
  3559. u32 rss_key[10];
  3560. netdev_rss_key_fill(rss_key, sizeof(rss_key));
  3561. for (j = 0; j < 10; j++)
  3562. wr32(E1000_RSSRK(j), rss_key[j]);
  3563. num_rx_queues = adapter->rss_queues;
  3564. switch (hw->mac.type) {
  3565. case e1000_82576:
  3566. /* 82576 supports 2 RSS queues for SR-IOV */
  3567. if (adapter->vfs_allocated_count)
  3568. num_rx_queues = 2;
  3569. break;
  3570. default:
  3571. break;
  3572. }
  3573. if (adapter->rss_indir_tbl_init != num_rx_queues) {
  3574. for (j = 0; j < IGB_RETA_SIZE; j++)
  3575. adapter->rss_indir_tbl[j] =
  3576. (j * num_rx_queues) / IGB_RETA_SIZE;
  3577. adapter->rss_indir_tbl_init = num_rx_queues;
  3578. }
  3579. igb_write_rss_indir_tbl(adapter);
  3580. /* Disable raw packet checksumming so that RSS hash is placed in
  3581. * descriptor on writeback. No need to enable TCP/UDP/IP checksum
  3582. * offloads as they are enabled by default
  3583. */
  3584. rxcsum = rd32(E1000_RXCSUM);
  3585. rxcsum |= E1000_RXCSUM_PCSD;
  3586. if (adapter->hw.mac.type >= e1000_82576)
  3587. /* Enable Receive Checksum Offload for SCTP */
  3588. rxcsum |= E1000_RXCSUM_CRCOFL;
  3589. /* Don't need to set TUOFL or IPOFL, they default to 1 */
  3590. wr32(E1000_RXCSUM, rxcsum);
  3591. /* Generate RSS hash based on packet types, TCP/UDP
  3592. * port numbers and/or IPv4/v6 src and dst addresses
  3593. */
  3594. mrqc = E1000_MRQC_RSS_FIELD_IPV4 |
  3595. E1000_MRQC_RSS_FIELD_IPV4_TCP |
  3596. E1000_MRQC_RSS_FIELD_IPV6 |
  3597. E1000_MRQC_RSS_FIELD_IPV6_TCP |
  3598. E1000_MRQC_RSS_FIELD_IPV6_TCP_EX;
  3599. if (adapter->flags & IGB_FLAG_RSS_FIELD_IPV4_UDP)
  3600. mrqc |= E1000_MRQC_RSS_FIELD_IPV4_UDP;
  3601. if (adapter->flags & IGB_FLAG_RSS_FIELD_IPV6_UDP)
  3602. mrqc |= E1000_MRQC_RSS_FIELD_IPV6_UDP;
  3603. /* If VMDq is enabled then we set the appropriate mode for that, else
  3604. * we default to RSS so that an RSS hash is calculated per packet even
  3605. * if we are only using one queue
  3606. */
  3607. if (adapter->vfs_allocated_count) {
  3608. if (hw->mac.type > e1000_82575) {
  3609. /* Set the default pool for the PF's first queue */
  3610. u32 vtctl = rd32(E1000_VT_CTL);
  3611. vtctl &= ~(E1000_VT_CTL_DEFAULT_POOL_MASK |
  3612. E1000_VT_CTL_DISABLE_DEF_POOL);
  3613. vtctl |= adapter->vfs_allocated_count <<
  3614. E1000_VT_CTL_DEFAULT_POOL_SHIFT;
  3615. wr32(E1000_VT_CTL, vtctl);
  3616. }
  3617. if (adapter->rss_queues > 1)
  3618. mrqc |= E1000_MRQC_ENABLE_VMDQ_RSS_MQ;
  3619. else
  3620. mrqc |= E1000_MRQC_ENABLE_VMDQ;
  3621. } else {
  3622. if (hw->mac.type != e1000_i211)
  3623. mrqc |= E1000_MRQC_ENABLE_RSS_MQ;
  3624. }
  3625. igb_vmm_control(adapter);
  3626. wr32(E1000_MRQC, mrqc);
  3627. }
  3628. /**
  3629. * igb_setup_rctl - configure the receive control registers
  3630. * @adapter: Board private structure
  3631. **/
  3632. void igb_setup_rctl(struct igb_adapter *adapter)
  3633. {
  3634. struct e1000_hw *hw = &adapter->hw;
  3635. u32 rctl;
  3636. rctl = rd32(E1000_RCTL);
  3637. rctl &= ~(3 << E1000_RCTL_MO_SHIFT);
  3638. rctl &= ~(E1000_RCTL_LBM_TCVR | E1000_RCTL_LBM_MAC);
  3639. rctl |= E1000_RCTL_EN | E1000_RCTL_BAM | E1000_RCTL_RDMTS_HALF |
  3640. (hw->mac.mc_filter_type << E1000_RCTL_MO_SHIFT);
  3641. /* enable stripping of CRC. It's unlikely this will break BMC
  3642. * redirection as it did with e1000. Newer features require
  3643. * that the HW strips the CRC.
  3644. */
  3645. rctl |= E1000_RCTL_SECRC;
  3646. /* disable store bad packets and clear size bits. */
  3647. rctl &= ~(E1000_RCTL_SBP | E1000_RCTL_SZ_256);
  3648. /* enable LPE to allow for reception of jumbo frames */
  3649. rctl |= E1000_RCTL_LPE;
  3650. /* disable queue 0 to prevent tail write w/o re-config */
  3651. wr32(E1000_RXDCTL(0), 0);
  3652. /* Attention!!! For SR-IOV PF driver operations you must enable
  3653. * queue drop for all VF and PF queues to prevent head of line blocking
  3654. * if an un-trusted VF does not provide descriptors to hardware.
  3655. */
  3656. if (adapter->vfs_allocated_count) {
  3657. /* set all queue drop enable bits */
  3658. wr32(E1000_QDE, ALL_QUEUES);
  3659. }
  3660. /* This is useful for sniffing bad packets. */
  3661. if (adapter->netdev->features & NETIF_F_RXALL) {
  3662. /* UPE and MPE will be handled by normal PROMISC logic
  3663. * in e1000e_set_rx_mode
  3664. */
  3665. rctl |= (E1000_RCTL_SBP | /* Receive bad packets */
  3666. E1000_RCTL_BAM | /* RX All Bcast Pkts */
  3667. E1000_RCTL_PMCF); /* RX All MAC Ctrl Pkts */
  3668. rctl &= ~(E1000_RCTL_DPF | /* Allow filtered pause */
  3669. E1000_RCTL_CFIEN); /* Dis VLAN CFIEN Filter */
  3670. /* Do not mess with E1000_CTRL_VME, it affects transmit as well,
  3671. * and that breaks VLANs.
  3672. */
  3673. }
  3674. wr32(E1000_RCTL, rctl);
  3675. }
  3676. static inline int igb_set_vf_rlpml(struct igb_adapter *adapter, int size,
  3677. int vfn)
  3678. {
  3679. struct e1000_hw *hw = &adapter->hw;
  3680. u32 vmolr;
  3681. if (size > MAX_JUMBO_FRAME_SIZE)
  3682. size = MAX_JUMBO_FRAME_SIZE;
  3683. vmolr = rd32(E1000_VMOLR(vfn));
  3684. vmolr &= ~E1000_VMOLR_RLPML_MASK;
  3685. vmolr |= size | E1000_VMOLR_LPE;
  3686. wr32(E1000_VMOLR(vfn), vmolr);
  3687. return 0;
  3688. }
  3689. static inline void igb_set_vf_vlan_strip(struct igb_adapter *adapter,
  3690. int vfn, bool enable)
  3691. {
  3692. struct e1000_hw *hw = &adapter->hw;
  3693. u32 val, reg;
  3694. if (hw->mac.type < e1000_82576)
  3695. return;
  3696. if (hw->mac.type == e1000_i350)
  3697. reg = E1000_DVMOLR(vfn);
  3698. else
  3699. reg = E1000_VMOLR(vfn);
  3700. val = rd32(reg);
  3701. if (enable)
  3702. val |= E1000_VMOLR_STRVLAN;
  3703. else
  3704. val &= ~(E1000_VMOLR_STRVLAN);
  3705. wr32(reg, val);
  3706. }
  3707. static inline void igb_set_vmolr(struct igb_adapter *adapter,
  3708. int vfn, bool aupe)
  3709. {
  3710. struct e1000_hw *hw = &adapter->hw;
  3711. u32 vmolr;
  3712. /* This register exists only on 82576 and newer so if we are older then
  3713. * we should exit and do nothing
  3714. */
  3715. if (hw->mac.type < e1000_82576)
  3716. return;
  3717. vmolr = rd32(E1000_VMOLR(vfn));
  3718. if (aupe)
  3719. vmolr |= E1000_VMOLR_AUPE; /* Accept untagged packets */
  3720. else
  3721. vmolr &= ~(E1000_VMOLR_AUPE); /* Tagged packets ONLY */
  3722. /* clear all bits that might not be set */
  3723. vmolr &= ~(E1000_VMOLR_BAM | E1000_VMOLR_RSSE);
  3724. if (adapter->rss_queues > 1 && vfn == adapter->vfs_allocated_count)
  3725. vmolr |= E1000_VMOLR_RSSE; /* enable RSS */
  3726. /* for VMDq only allow the VFs and pool 0 to accept broadcast and
  3727. * multicast packets
  3728. */
  3729. if (vfn <= adapter->vfs_allocated_count)
  3730. vmolr |= E1000_VMOLR_BAM; /* Accept broadcast */
  3731. wr32(E1000_VMOLR(vfn), vmolr);
  3732. }
  3733. /**
  3734. * igb_configure_rx_ring - Configure a receive ring after Reset
  3735. * @adapter: board private structure
  3736. * @ring: receive ring to be configured
  3737. *
  3738. * Configure the Rx unit of the MAC after a reset.
  3739. **/
  3740. void igb_configure_rx_ring(struct igb_adapter *adapter,
  3741. struct igb_ring *ring)
  3742. {
  3743. struct e1000_hw *hw = &adapter->hw;
  3744. union e1000_adv_rx_desc *rx_desc;
  3745. u64 rdba = ring->dma;
  3746. int reg_idx = ring->reg_idx;
  3747. u32 srrctl = 0, rxdctl = 0;
  3748. /* disable the queue */
  3749. wr32(E1000_RXDCTL(reg_idx), 0);
  3750. /* Set DMA base address registers */
  3751. wr32(E1000_RDBAL(reg_idx),
  3752. rdba & 0x00000000ffffffffULL);
  3753. wr32(E1000_RDBAH(reg_idx), rdba >> 32);
  3754. wr32(E1000_RDLEN(reg_idx),
  3755. ring->count * sizeof(union e1000_adv_rx_desc));
  3756. /* initialize head and tail */
  3757. ring->tail = adapter->io_addr + E1000_RDT(reg_idx);
  3758. wr32(E1000_RDH(reg_idx), 0);
  3759. writel(0, ring->tail);
  3760. /* set descriptor configuration */
  3761. srrctl = IGB_RX_HDR_LEN << E1000_SRRCTL_BSIZEHDRSIZE_SHIFT;
  3762. if (ring_uses_large_buffer(ring))
  3763. srrctl |= IGB_RXBUFFER_3072 >> E1000_SRRCTL_BSIZEPKT_SHIFT;
  3764. else
  3765. srrctl |= IGB_RXBUFFER_2048 >> E1000_SRRCTL_BSIZEPKT_SHIFT;
  3766. srrctl |= E1000_SRRCTL_DESCTYPE_ADV_ONEBUF;
  3767. if (hw->mac.type >= e1000_82580)
  3768. srrctl |= E1000_SRRCTL_TIMESTAMP;
  3769. /* Only set Drop Enable if we are supporting multiple queues */
  3770. if (adapter->vfs_allocated_count || adapter->num_rx_queues > 1)
  3771. srrctl |= E1000_SRRCTL_DROP_EN;
  3772. wr32(E1000_SRRCTL(reg_idx), srrctl);
  3773. /* set filtering for VMDQ pools */
  3774. igb_set_vmolr(adapter, reg_idx & 0x7, true);
  3775. rxdctl |= IGB_RX_PTHRESH;
  3776. rxdctl |= IGB_RX_HTHRESH << 8;
  3777. rxdctl |= IGB_RX_WTHRESH << 16;
  3778. /* initialize rx_buffer_info */
  3779. memset(ring->rx_buffer_info, 0,
  3780. sizeof(struct igb_rx_buffer) * ring->count);
  3781. /* initialize Rx descriptor 0 */
  3782. rx_desc = IGB_RX_DESC(ring, 0);
  3783. rx_desc->wb.upper.length = 0;
  3784. /* enable receive descriptor fetching */
  3785. rxdctl |= E1000_RXDCTL_QUEUE_ENABLE;
  3786. wr32(E1000_RXDCTL(reg_idx), rxdctl);
  3787. }
  3788. static void igb_set_rx_buffer_len(struct igb_adapter *adapter,
  3789. struct igb_ring *rx_ring)
  3790. {
  3791. /* set build_skb and buffer size flags */
  3792. clear_ring_build_skb_enabled(rx_ring);
  3793. clear_ring_uses_large_buffer(rx_ring);
  3794. if (adapter->flags & IGB_FLAG_RX_LEGACY)
  3795. return;
  3796. set_ring_build_skb_enabled(rx_ring);
  3797. #if (PAGE_SIZE < 8192)
  3798. if (adapter->max_frame_size <= IGB_MAX_FRAME_BUILD_SKB)
  3799. return;
  3800. set_ring_uses_large_buffer(rx_ring);
  3801. #endif
  3802. }
  3803. /**
  3804. * igb_configure_rx - Configure receive Unit after Reset
  3805. * @adapter: board private structure
  3806. *
  3807. * Configure the Rx unit of the MAC after a reset.
  3808. **/
  3809. static void igb_configure_rx(struct igb_adapter *adapter)
  3810. {
  3811. int i;
  3812. /* set the correct pool for the PF default MAC address in entry 0 */
  3813. igb_set_default_mac_filter(adapter);
  3814. /* Setup the HW Rx Head and Tail Descriptor Pointers and
  3815. * the Base and Length of the Rx Descriptor Ring
  3816. */
  3817. for (i = 0; i < adapter->num_rx_queues; i++) {
  3818. struct igb_ring *rx_ring = adapter->rx_ring[i];
  3819. igb_set_rx_buffer_len(adapter, rx_ring);
  3820. igb_configure_rx_ring(adapter, rx_ring);
  3821. }
  3822. }
  3823. /**
  3824. * igb_free_tx_resources - Free Tx Resources per Queue
  3825. * @tx_ring: Tx descriptor ring for a specific queue
  3826. *
  3827. * Free all transmit software resources
  3828. **/
  3829. void igb_free_tx_resources(struct igb_ring *tx_ring)
  3830. {
  3831. igb_clean_tx_ring(tx_ring);
  3832. vfree(tx_ring->tx_buffer_info);
  3833. tx_ring->tx_buffer_info = NULL;
  3834. /* if not set, then don't free */
  3835. if (!tx_ring->desc)
  3836. return;
  3837. dma_free_coherent(tx_ring->dev, tx_ring->size,
  3838. tx_ring->desc, tx_ring->dma);
  3839. tx_ring->desc = NULL;
  3840. }
  3841. /**
  3842. * igb_free_all_tx_resources - Free Tx Resources for All Queues
  3843. * @adapter: board private structure
  3844. *
  3845. * Free all transmit software resources
  3846. **/
  3847. static void igb_free_all_tx_resources(struct igb_adapter *adapter)
  3848. {
  3849. int i;
  3850. for (i = 0; i < adapter->num_tx_queues; i++)
  3851. if (adapter->tx_ring[i])
  3852. igb_free_tx_resources(adapter->tx_ring[i]);
  3853. }
  3854. /**
  3855. * igb_clean_tx_ring - Free Tx Buffers
  3856. * @tx_ring: ring to be cleaned
  3857. **/
  3858. static void igb_clean_tx_ring(struct igb_ring *tx_ring)
  3859. {
  3860. u16 i = tx_ring->next_to_clean;
  3861. struct igb_tx_buffer *tx_buffer = &tx_ring->tx_buffer_info[i];
  3862. while (i != tx_ring->next_to_use) {
  3863. union e1000_adv_tx_desc *eop_desc, *tx_desc;
  3864. /* Free all the Tx ring sk_buffs */
  3865. dev_kfree_skb_any(tx_buffer->skb);
  3866. /* unmap skb header data */
  3867. dma_unmap_single(tx_ring->dev,
  3868. dma_unmap_addr(tx_buffer, dma),
  3869. dma_unmap_len(tx_buffer, len),
  3870. DMA_TO_DEVICE);
  3871. /* check for eop_desc to determine the end of the packet */
  3872. eop_desc = tx_buffer->next_to_watch;
  3873. tx_desc = IGB_TX_DESC(tx_ring, i);
  3874. /* unmap remaining buffers */
  3875. while (tx_desc != eop_desc) {
  3876. tx_buffer++;
  3877. tx_desc++;
  3878. i++;
  3879. if (unlikely(i == tx_ring->count)) {
  3880. i = 0;
  3881. tx_buffer = tx_ring->tx_buffer_info;
  3882. tx_desc = IGB_TX_DESC(tx_ring, 0);
  3883. }
  3884. /* unmap any remaining paged data */
  3885. if (dma_unmap_len(tx_buffer, len))
  3886. dma_unmap_page(tx_ring->dev,
  3887. dma_unmap_addr(tx_buffer, dma),
  3888. dma_unmap_len(tx_buffer, len),
  3889. DMA_TO_DEVICE);
  3890. }
  3891. /* move us one more past the eop_desc for start of next pkt */
  3892. tx_buffer++;
  3893. i++;
  3894. if (unlikely(i == tx_ring->count)) {
  3895. i = 0;
  3896. tx_buffer = tx_ring->tx_buffer_info;
  3897. }
  3898. }
  3899. /* reset BQL for queue */
  3900. netdev_tx_reset_queue(txring_txq(tx_ring));
  3901. /* reset next_to_use and next_to_clean */
  3902. tx_ring->next_to_use = 0;
  3903. tx_ring->next_to_clean = 0;
  3904. }
  3905. /**
  3906. * igb_clean_all_tx_rings - Free Tx Buffers for all queues
  3907. * @adapter: board private structure
  3908. **/
  3909. static void igb_clean_all_tx_rings(struct igb_adapter *adapter)
  3910. {
  3911. int i;
  3912. for (i = 0; i < adapter->num_tx_queues; i++)
  3913. if (adapter->tx_ring[i])
  3914. igb_clean_tx_ring(adapter->tx_ring[i]);
  3915. }
  3916. /**
  3917. * igb_free_rx_resources - Free Rx Resources
  3918. * @rx_ring: ring to clean the resources from
  3919. *
  3920. * Free all receive software resources
  3921. **/
  3922. void igb_free_rx_resources(struct igb_ring *rx_ring)
  3923. {
  3924. igb_clean_rx_ring(rx_ring);
  3925. vfree(rx_ring->rx_buffer_info);
  3926. rx_ring->rx_buffer_info = NULL;
  3927. /* if not set, then don't free */
  3928. if (!rx_ring->desc)
  3929. return;
  3930. dma_free_coherent(rx_ring->dev, rx_ring->size,
  3931. rx_ring->desc, rx_ring->dma);
  3932. rx_ring->desc = NULL;
  3933. }
  3934. /**
  3935. * igb_free_all_rx_resources - Free Rx Resources for All Queues
  3936. * @adapter: board private structure
  3937. *
  3938. * Free all receive software resources
  3939. **/
  3940. static void igb_free_all_rx_resources(struct igb_adapter *adapter)
  3941. {
  3942. int i;
  3943. for (i = 0; i < adapter->num_rx_queues; i++)
  3944. if (adapter->rx_ring[i])
  3945. igb_free_rx_resources(adapter->rx_ring[i]);
  3946. }
  3947. /**
  3948. * igb_clean_rx_ring - Free Rx Buffers per Queue
  3949. * @rx_ring: ring to free buffers from
  3950. **/
  3951. static void igb_clean_rx_ring(struct igb_ring *rx_ring)
  3952. {
  3953. u16 i = rx_ring->next_to_clean;
  3954. if (rx_ring->skb)
  3955. dev_kfree_skb(rx_ring->skb);
  3956. rx_ring->skb = NULL;
  3957. /* Free all the Rx ring sk_buffs */
  3958. while (i != rx_ring->next_to_alloc) {
  3959. struct igb_rx_buffer *buffer_info = &rx_ring->rx_buffer_info[i];
  3960. /* Invalidate cache lines that may have been written to by
  3961. * device so that we avoid corrupting memory.
  3962. */
  3963. dma_sync_single_range_for_cpu(rx_ring->dev,
  3964. buffer_info->dma,
  3965. buffer_info->page_offset,
  3966. igb_rx_bufsz(rx_ring),
  3967. DMA_FROM_DEVICE);
  3968. /* free resources associated with mapping */
  3969. dma_unmap_page_attrs(rx_ring->dev,
  3970. buffer_info->dma,
  3971. igb_rx_pg_size(rx_ring),
  3972. DMA_FROM_DEVICE,
  3973. IGB_RX_DMA_ATTR);
  3974. __page_frag_cache_drain(buffer_info->page,
  3975. buffer_info->pagecnt_bias);
  3976. i++;
  3977. if (i == rx_ring->count)
  3978. i = 0;
  3979. }
  3980. rx_ring->next_to_alloc = 0;
  3981. rx_ring->next_to_clean = 0;
  3982. rx_ring->next_to_use = 0;
  3983. }
  3984. /**
  3985. * igb_clean_all_rx_rings - Free Rx Buffers for all queues
  3986. * @adapter: board private structure
  3987. **/
  3988. static void igb_clean_all_rx_rings(struct igb_adapter *adapter)
  3989. {
  3990. int i;
  3991. for (i = 0; i < adapter->num_rx_queues; i++)
  3992. if (adapter->rx_ring[i])
  3993. igb_clean_rx_ring(adapter->rx_ring[i]);
  3994. }
  3995. /**
  3996. * igb_set_mac - Change the Ethernet Address of the NIC
  3997. * @netdev: network interface device structure
  3998. * @p: pointer to an address structure
  3999. *
  4000. * Returns 0 on success, negative on failure
  4001. **/
  4002. static int igb_set_mac(struct net_device *netdev, void *p)
  4003. {
  4004. struct igb_adapter *adapter = netdev_priv(netdev);
  4005. struct e1000_hw *hw = &adapter->hw;
  4006. struct sockaddr *addr = p;
  4007. if (!is_valid_ether_addr(addr->sa_data))
  4008. return -EADDRNOTAVAIL;
  4009. memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
  4010. memcpy(hw->mac.addr, addr->sa_data, netdev->addr_len);
  4011. /* set the correct pool for the new PF MAC address in entry 0 */
  4012. igb_set_default_mac_filter(adapter);
  4013. return 0;
  4014. }
  4015. /**
  4016. * igb_write_mc_addr_list - write multicast addresses to MTA
  4017. * @netdev: network interface device structure
  4018. *
  4019. * Writes multicast address list to the MTA hash table.
  4020. * Returns: -ENOMEM on failure
  4021. * 0 on no addresses written
  4022. * X on writing X addresses to MTA
  4023. **/
  4024. static int igb_write_mc_addr_list(struct net_device *netdev)
  4025. {
  4026. struct igb_adapter *adapter = netdev_priv(netdev);
  4027. struct e1000_hw *hw = &adapter->hw;
  4028. struct netdev_hw_addr *ha;
  4029. u8 *mta_list;
  4030. int i;
  4031. if (netdev_mc_empty(netdev)) {
  4032. /* nothing to program, so clear mc list */
  4033. igb_update_mc_addr_list(hw, NULL, 0);
  4034. igb_restore_vf_multicasts(adapter);
  4035. return 0;
  4036. }
  4037. mta_list = kcalloc(netdev_mc_count(netdev), 6, GFP_ATOMIC);
  4038. if (!mta_list)
  4039. return -ENOMEM;
  4040. /* The shared function expects a packed array of only addresses. */
  4041. i = 0;
  4042. netdev_for_each_mc_addr(ha, netdev)
  4043. memcpy(mta_list + (i++ * ETH_ALEN), ha->addr, ETH_ALEN);
  4044. igb_update_mc_addr_list(hw, mta_list, i);
  4045. kfree(mta_list);
  4046. return netdev_mc_count(netdev);
  4047. }
  4048. static int igb_vlan_promisc_enable(struct igb_adapter *adapter)
  4049. {
  4050. struct e1000_hw *hw = &adapter->hw;
  4051. u32 i, pf_id;
  4052. switch (hw->mac.type) {
  4053. case e1000_i210:
  4054. case e1000_i211:
  4055. case e1000_i350:
  4056. /* VLAN filtering needed for VLAN prio filter */
  4057. if (adapter->netdev->features & NETIF_F_NTUPLE)
  4058. break;
  4059. /* fall through */
  4060. case e1000_82576:
  4061. case e1000_82580:
  4062. case e1000_i354:
  4063. /* VLAN filtering needed for pool filtering */
  4064. if (adapter->vfs_allocated_count)
  4065. break;
  4066. /* fall through */
  4067. default:
  4068. return 1;
  4069. }
  4070. /* We are already in VLAN promisc, nothing to do */
  4071. if (adapter->flags & IGB_FLAG_VLAN_PROMISC)
  4072. return 0;
  4073. if (!adapter->vfs_allocated_count)
  4074. goto set_vfta;
  4075. /* Add PF to all active pools */
  4076. pf_id = adapter->vfs_allocated_count + E1000_VLVF_POOLSEL_SHIFT;
  4077. for (i = E1000_VLVF_ARRAY_SIZE; --i;) {
  4078. u32 vlvf = rd32(E1000_VLVF(i));
  4079. vlvf |= BIT(pf_id);
  4080. wr32(E1000_VLVF(i), vlvf);
  4081. }
  4082. set_vfta:
  4083. /* Set all bits in the VLAN filter table array */
  4084. for (i = E1000_VLAN_FILTER_TBL_SIZE; i--;)
  4085. hw->mac.ops.write_vfta(hw, i, ~0U);
  4086. /* Set flag so we don't redo unnecessary work */
  4087. adapter->flags |= IGB_FLAG_VLAN_PROMISC;
  4088. return 0;
  4089. }
  4090. #define VFTA_BLOCK_SIZE 8
  4091. static void igb_scrub_vfta(struct igb_adapter *adapter, u32 vfta_offset)
  4092. {
  4093. struct e1000_hw *hw = &adapter->hw;
  4094. u32 vfta[VFTA_BLOCK_SIZE] = { 0 };
  4095. u32 vid_start = vfta_offset * 32;
  4096. u32 vid_end = vid_start + (VFTA_BLOCK_SIZE * 32);
  4097. u32 i, vid, word, bits, pf_id;
  4098. /* guarantee that we don't scrub out management VLAN */
  4099. vid = adapter->mng_vlan_id;
  4100. if (vid >= vid_start && vid < vid_end)
  4101. vfta[(vid - vid_start) / 32] |= BIT(vid % 32);
  4102. if (!adapter->vfs_allocated_count)
  4103. goto set_vfta;
  4104. pf_id = adapter->vfs_allocated_count + E1000_VLVF_POOLSEL_SHIFT;
  4105. for (i = E1000_VLVF_ARRAY_SIZE; --i;) {
  4106. u32 vlvf = rd32(E1000_VLVF(i));
  4107. /* pull VLAN ID from VLVF */
  4108. vid = vlvf & VLAN_VID_MASK;
  4109. /* only concern ourselves with a certain range */
  4110. if (vid < vid_start || vid >= vid_end)
  4111. continue;
  4112. if (vlvf & E1000_VLVF_VLANID_ENABLE) {
  4113. /* record VLAN ID in VFTA */
  4114. vfta[(vid - vid_start) / 32] |= BIT(vid % 32);
  4115. /* if PF is part of this then continue */
  4116. if (test_bit(vid, adapter->active_vlans))
  4117. continue;
  4118. }
  4119. /* remove PF from the pool */
  4120. bits = ~BIT(pf_id);
  4121. bits &= rd32(E1000_VLVF(i));
  4122. wr32(E1000_VLVF(i), bits);
  4123. }
  4124. set_vfta:
  4125. /* extract values from active_vlans and write back to VFTA */
  4126. for (i = VFTA_BLOCK_SIZE; i--;) {
  4127. vid = (vfta_offset + i) * 32;
  4128. word = vid / BITS_PER_LONG;
  4129. bits = vid % BITS_PER_LONG;
  4130. vfta[i] |= adapter->active_vlans[word] >> bits;
  4131. hw->mac.ops.write_vfta(hw, vfta_offset + i, vfta[i]);
  4132. }
  4133. }
  4134. static void igb_vlan_promisc_disable(struct igb_adapter *adapter)
  4135. {
  4136. u32 i;
  4137. /* We are not in VLAN promisc, nothing to do */
  4138. if (!(adapter->flags & IGB_FLAG_VLAN_PROMISC))
  4139. return;
  4140. /* Set flag so we don't redo unnecessary work */
  4141. adapter->flags &= ~IGB_FLAG_VLAN_PROMISC;
  4142. for (i = 0; i < E1000_VLAN_FILTER_TBL_SIZE; i += VFTA_BLOCK_SIZE)
  4143. igb_scrub_vfta(adapter, i);
  4144. }
  4145. /**
  4146. * igb_set_rx_mode - Secondary Unicast, Multicast and Promiscuous mode set
  4147. * @netdev: network interface device structure
  4148. *
  4149. * The set_rx_mode entry point is called whenever the unicast or multicast
  4150. * address lists or the network interface flags are updated. This routine is
  4151. * responsible for configuring the hardware for proper unicast, multicast,
  4152. * promiscuous mode, and all-multi behavior.
  4153. **/
  4154. static void igb_set_rx_mode(struct net_device *netdev)
  4155. {
  4156. struct igb_adapter *adapter = netdev_priv(netdev);
  4157. struct e1000_hw *hw = &adapter->hw;
  4158. unsigned int vfn = adapter->vfs_allocated_count;
  4159. u32 rctl = 0, vmolr = 0, rlpml = MAX_JUMBO_FRAME_SIZE;
  4160. int count;
  4161. /* Check for Promiscuous and All Multicast modes */
  4162. if (netdev->flags & IFF_PROMISC) {
  4163. rctl |= E1000_RCTL_UPE | E1000_RCTL_MPE;
  4164. vmolr |= E1000_VMOLR_MPME;
  4165. /* enable use of UTA filter to force packets to default pool */
  4166. if (hw->mac.type == e1000_82576)
  4167. vmolr |= E1000_VMOLR_ROPE;
  4168. } else {
  4169. if (netdev->flags & IFF_ALLMULTI) {
  4170. rctl |= E1000_RCTL_MPE;
  4171. vmolr |= E1000_VMOLR_MPME;
  4172. } else {
  4173. /* Write addresses to the MTA, if the attempt fails
  4174. * then we should just turn on promiscuous mode so
  4175. * that we can at least receive multicast traffic
  4176. */
  4177. count = igb_write_mc_addr_list(netdev);
  4178. if (count < 0) {
  4179. rctl |= E1000_RCTL_MPE;
  4180. vmolr |= E1000_VMOLR_MPME;
  4181. } else if (count) {
  4182. vmolr |= E1000_VMOLR_ROMPE;
  4183. }
  4184. }
  4185. }
  4186. /* Write addresses to available RAR registers, if there is not
  4187. * sufficient space to store all the addresses then enable
  4188. * unicast promiscuous mode
  4189. */
  4190. if (__dev_uc_sync(netdev, igb_uc_sync, igb_uc_unsync)) {
  4191. rctl |= E1000_RCTL_UPE;
  4192. vmolr |= E1000_VMOLR_ROPE;
  4193. }
  4194. /* enable VLAN filtering by default */
  4195. rctl |= E1000_RCTL_VFE;
  4196. /* disable VLAN filtering for modes that require it */
  4197. if ((netdev->flags & IFF_PROMISC) ||
  4198. (netdev->features & NETIF_F_RXALL)) {
  4199. /* if we fail to set all rules then just clear VFE */
  4200. if (igb_vlan_promisc_enable(adapter))
  4201. rctl &= ~E1000_RCTL_VFE;
  4202. } else {
  4203. igb_vlan_promisc_disable(adapter);
  4204. }
  4205. /* update state of unicast, multicast, and VLAN filtering modes */
  4206. rctl |= rd32(E1000_RCTL) & ~(E1000_RCTL_UPE | E1000_RCTL_MPE |
  4207. E1000_RCTL_VFE);
  4208. wr32(E1000_RCTL, rctl);
  4209. #if (PAGE_SIZE < 8192)
  4210. if (!adapter->vfs_allocated_count) {
  4211. if (adapter->max_frame_size <= IGB_MAX_FRAME_BUILD_SKB)
  4212. rlpml = IGB_MAX_FRAME_BUILD_SKB;
  4213. }
  4214. #endif
  4215. wr32(E1000_RLPML, rlpml);
  4216. /* In order to support SR-IOV and eventually VMDq it is necessary to set
  4217. * the VMOLR to enable the appropriate modes. Without this workaround
  4218. * we will have issues with VLAN tag stripping not being done for frames
  4219. * that are only arriving because we are the default pool
  4220. */
  4221. if ((hw->mac.type < e1000_82576) || (hw->mac.type > e1000_i350))
  4222. return;
  4223. /* set UTA to appropriate mode */
  4224. igb_set_uta(adapter, !!(vmolr & E1000_VMOLR_ROPE));
  4225. vmolr |= rd32(E1000_VMOLR(vfn)) &
  4226. ~(E1000_VMOLR_ROPE | E1000_VMOLR_MPME | E1000_VMOLR_ROMPE);
  4227. /* enable Rx jumbo frames, restrict as needed to support build_skb */
  4228. vmolr &= ~E1000_VMOLR_RLPML_MASK;
  4229. #if (PAGE_SIZE < 8192)
  4230. if (adapter->max_frame_size <= IGB_MAX_FRAME_BUILD_SKB)
  4231. vmolr |= IGB_MAX_FRAME_BUILD_SKB;
  4232. else
  4233. #endif
  4234. vmolr |= MAX_JUMBO_FRAME_SIZE;
  4235. vmolr |= E1000_VMOLR_LPE;
  4236. wr32(E1000_VMOLR(vfn), vmolr);
  4237. igb_restore_vf_multicasts(adapter);
  4238. }
  4239. static void igb_check_wvbr(struct igb_adapter *adapter)
  4240. {
  4241. struct e1000_hw *hw = &adapter->hw;
  4242. u32 wvbr = 0;
  4243. switch (hw->mac.type) {
  4244. case e1000_82576:
  4245. case e1000_i350:
  4246. wvbr = rd32(E1000_WVBR);
  4247. if (!wvbr)
  4248. return;
  4249. break;
  4250. default:
  4251. break;
  4252. }
  4253. adapter->wvbr |= wvbr;
  4254. }
  4255. #define IGB_STAGGERED_QUEUE_OFFSET 8
  4256. static void igb_spoof_check(struct igb_adapter *adapter)
  4257. {
  4258. int j;
  4259. if (!adapter->wvbr)
  4260. return;
  4261. for (j = 0; j < adapter->vfs_allocated_count; j++) {
  4262. if (adapter->wvbr & BIT(j) ||
  4263. adapter->wvbr & BIT(j + IGB_STAGGERED_QUEUE_OFFSET)) {
  4264. dev_warn(&adapter->pdev->dev,
  4265. "Spoof event(s) detected on VF %d\n", j);
  4266. adapter->wvbr &=
  4267. ~(BIT(j) |
  4268. BIT(j + IGB_STAGGERED_QUEUE_OFFSET));
  4269. }
  4270. }
  4271. }
  4272. /* Need to wait a few seconds after link up to get diagnostic information from
  4273. * the phy
  4274. */
  4275. static void igb_update_phy_info(struct timer_list *t)
  4276. {
  4277. struct igb_adapter *adapter = from_timer(adapter, t, phy_info_timer);
  4278. igb_get_phy_info(&adapter->hw);
  4279. }
  4280. /**
  4281. * igb_has_link - check shared code for link and determine up/down
  4282. * @adapter: pointer to driver private info
  4283. **/
  4284. bool igb_has_link(struct igb_adapter *adapter)
  4285. {
  4286. struct e1000_hw *hw = &adapter->hw;
  4287. bool link_active = false;
  4288. /* get_link_status is set on LSC (link status) interrupt or
  4289. * rx sequence error interrupt. get_link_status will stay
  4290. * false until the e1000_check_for_link establishes link
  4291. * for copper adapters ONLY
  4292. */
  4293. switch (hw->phy.media_type) {
  4294. case e1000_media_type_copper:
  4295. if (!hw->mac.get_link_status)
  4296. return true;
  4297. case e1000_media_type_internal_serdes:
  4298. hw->mac.ops.check_for_link(hw);
  4299. link_active = !hw->mac.get_link_status;
  4300. break;
  4301. default:
  4302. case e1000_media_type_unknown:
  4303. break;
  4304. }
  4305. if (((hw->mac.type == e1000_i210) ||
  4306. (hw->mac.type == e1000_i211)) &&
  4307. (hw->phy.id == I210_I_PHY_ID)) {
  4308. if (!netif_carrier_ok(adapter->netdev)) {
  4309. adapter->flags &= ~IGB_FLAG_NEED_LINK_UPDATE;
  4310. } else if (!(adapter->flags & IGB_FLAG_NEED_LINK_UPDATE)) {
  4311. adapter->flags |= IGB_FLAG_NEED_LINK_UPDATE;
  4312. adapter->link_check_timeout = jiffies;
  4313. }
  4314. }
  4315. return link_active;
  4316. }
  4317. static bool igb_thermal_sensor_event(struct e1000_hw *hw, u32 event)
  4318. {
  4319. bool ret = false;
  4320. u32 ctrl_ext, thstat;
  4321. /* check for thermal sensor event on i350 copper only */
  4322. if (hw->mac.type == e1000_i350) {
  4323. thstat = rd32(E1000_THSTAT);
  4324. ctrl_ext = rd32(E1000_CTRL_EXT);
  4325. if ((hw->phy.media_type == e1000_media_type_copper) &&
  4326. !(ctrl_ext & E1000_CTRL_EXT_LINK_MODE_SGMII))
  4327. ret = !!(thstat & event);
  4328. }
  4329. return ret;
  4330. }
  4331. /**
  4332. * igb_check_lvmmc - check for malformed packets received
  4333. * and indicated in LVMMC register
  4334. * @adapter: pointer to adapter
  4335. **/
  4336. static void igb_check_lvmmc(struct igb_adapter *adapter)
  4337. {
  4338. struct e1000_hw *hw = &adapter->hw;
  4339. u32 lvmmc;
  4340. lvmmc = rd32(E1000_LVMMC);
  4341. if (lvmmc) {
  4342. if (unlikely(net_ratelimit())) {
  4343. netdev_warn(adapter->netdev,
  4344. "malformed Tx packet detected and dropped, LVMMC:0x%08x\n",
  4345. lvmmc);
  4346. }
  4347. }
  4348. }
  4349. /**
  4350. * igb_watchdog - Timer Call-back
  4351. * @data: pointer to adapter cast into an unsigned long
  4352. **/
  4353. static void igb_watchdog(struct timer_list *t)
  4354. {
  4355. struct igb_adapter *adapter = from_timer(adapter, t, watchdog_timer);
  4356. /* Do the rest outside of interrupt context */
  4357. schedule_work(&adapter->watchdog_task);
  4358. }
  4359. static void igb_watchdog_task(struct work_struct *work)
  4360. {
  4361. struct igb_adapter *adapter = container_of(work,
  4362. struct igb_adapter,
  4363. watchdog_task);
  4364. struct e1000_hw *hw = &adapter->hw;
  4365. struct e1000_phy_info *phy = &hw->phy;
  4366. struct net_device *netdev = adapter->netdev;
  4367. u32 link;
  4368. int i;
  4369. u32 connsw;
  4370. u16 phy_data, retry_count = 20;
  4371. link = igb_has_link(adapter);
  4372. if (adapter->flags & IGB_FLAG_NEED_LINK_UPDATE) {
  4373. if (time_after(jiffies, (adapter->link_check_timeout + HZ)))
  4374. adapter->flags &= ~IGB_FLAG_NEED_LINK_UPDATE;
  4375. else
  4376. link = false;
  4377. }
  4378. /* Force link down if we have fiber to swap to */
  4379. if (adapter->flags & IGB_FLAG_MAS_ENABLE) {
  4380. if (hw->phy.media_type == e1000_media_type_copper) {
  4381. connsw = rd32(E1000_CONNSW);
  4382. if (!(connsw & E1000_CONNSW_AUTOSENSE_EN))
  4383. link = 0;
  4384. }
  4385. }
  4386. if (link) {
  4387. /* Perform a reset if the media type changed. */
  4388. if (hw->dev_spec._82575.media_changed) {
  4389. hw->dev_spec._82575.media_changed = false;
  4390. adapter->flags |= IGB_FLAG_MEDIA_RESET;
  4391. igb_reset(adapter);
  4392. }
  4393. /* Cancel scheduled suspend requests. */
  4394. pm_runtime_resume(netdev->dev.parent);
  4395. if (!netif_carrier_ok(netdev)) {
  4396. u32 ctrl;
  4397. hw->mac.ops.get_speed_and_duplex(hw,
  4398. &adapter->link_speed,
  4399. &adapter->link_duplex);
  4400. ctrl = rd32(E1000_CTRL);
  4401. /* Links status message must follow this format */
  4402. netdev_info(netdev,
  4403. "igb: %s NIC Link is Up %d Mbps %s Duplex, Flow Control: %s\n",
  4404. netdev->name,
  4405. adapter->link_speed,
  4406. adapter->link_duplex == FULL_DUPLEX ?
  4407. "Full" : "Half",
  4408. (ctrl & E1000_CTRL_TFCE) &&
  4409. (ctrl & E1000_CTRL_RFCE) ? "RX/TX" :
  4410. (ctrl & E1000_CTRL_RFCE) ? "RX" :
  4411. (ctrl & E1000_CTRL_TFCE) ? "TX" : "None");
  4412. /* disable EEE if enabled */
  4413. if ((adapter->flags & IGB_FLAG_EEE) &&
  4414. (adapter->link_duplex == HALF_DUPLEX)) {
  4415. dev_info(&adapter->pdev->dev,
  4416. "EEE Disabled: unsupported at half duplex. Re-enable using ethtool when at full duplex.\n");
  4417. adapter->hw.dev_spec._82575.eee_disable = true;
  4418. adapter->flags &= ~IGB_FLAG_EEE;
  4419. }
  4420. /* check if SmartSpeed worked */
  4421. igb_check_downshift(hw);
  4422. if (phy->speed_downgraded)
  4423. netdev_warn(netdev, "Link Speed was downgraded by SmartSpeed\n");
  4424. /* check for thermal sensor event */
  4425. if (igb_thermal_sensor_event(hw,
  4426. E1000_THSTAT_LINK_THROTTLE))
  4427. netdev_info(netdev, "The network adapter link speed was downshifted because it overheated\n");
  4428. /* adjust timeout factor according to speed/duplex */
  4429. adapter->tx_timeout_factor = 1;
  4430. switch (adapter->link_speed) {
  4431. case SPEED_10:
  4432. adapter->tx_timeout_factor = 14;
  4433. break;
  4434. case SPEED_100:
  4435. /* maybe add some timeout factor ? */
  4436. break;
  4437. }
  4438. if (adapter->link_speed != SPEED_1000)
  4439. goto no_wait;
  4440. /* wait for Remote receiver status OK */
  4441. retry_read_status:
  4442. if (!igb_read_phy_reg(hw, PHY_1000T_STATUS,
  4443. &phy_data)) {
  4444. if (!(phy_data & SR_1000T_REMOTE_RX_STATUS) &&
  4445. retry_count) {
  4446. msleep(100);
  4447. retry_count--;
  4448. goto retry_read_status;
  4449. } else if (!retry_count) {
  4450. dev_err(&adapter->pdev->dev, "exceed max 2 second\n");
  4451. }
  4452. } else {
  4453. dev_err(&adapter->pdev->dev, "read 1000Base-T Status Reg\n");
  4454. }
  4455. no_wait:
  4456. netif_carrier_on(netdev);
  4457. igb_ping_all_vfs(adapter);
  4458. igb_check_vf_rate_limit(adapter);
  4459. /* link state has changed, schedule phy info update */
  4460. if (!test_bit(__IGB_DOWN, &adapter->state))
  4461. mod_timer(&adapter->phy_info_timer,
  4462. round_jiffies(jiffies + 2 * HZ));
  4463. }
  4464. } else {
  4465. if (netif_carrier_ok(netdev)) {
  4466. adapter->link_speed = 0;
  4467. adapter->link_duplex = 0;
  4468. /* check for thermal sensor event */
  4469. if (igb_thermal_sensor_event(hw,
  4470. E1000_THSTAT_PWR_DOWN)) {
  4471. netdev_err(netdev, "The network adapter was stopped because it overheated\n");
  4472. }
  4473. /* Links status message must follow this format */
  4474. netdev_info(netdev, "igb: %s NIC Link is Down\n",
  4475. netdev->name);
  4476. netif_carrier_off(netdev);
  4477. igb_ping_all_vfs(adapter);
  4478. /* link state has changed, schedule phy info update */
  4479. if (!test_bit(__IGB_DOWN, &adapter->state))
  4480. mod_timer(&adapter->phy_info_timer,
  4481. round_jiffies(jiffies + 2 * HZ));
  4482. /* link is down, time to check for alternate media */
  4483. if (adapter->flags & IGB_FLAG_MAS_ENABLE) {
  4484. igb_check_swap_media(adapter);
  4485. if (adapter->flags & IGB_FLAG_MEDIA_RESET) {
  4486. schedule_work(&adapter->reset_task);
  4487. /* return immediately */
  4488. return;
  4489. }
  4490. }
  4491. pm_schedule_suspend(netdev->dev.parent,
  4492. MSEC_PER_SEC * 5);
  4493. /* also check for alternate media here */
  4494. } else if (!netif_carrier_ok(netdev) &&
  4495. (adapter->flags & IGB_FLAG_MAS_ENABLE)) {
  4496. igb_check_swap_media(adapter);
  4497. if (adapter->flags & IGB_FLAG_MEDIA_RESET) {
  4498. schedule_work(&adapter->reset_task);
  4499. /* return immediately */
  4500. return;
  4501. }
  4502. }
  4503. }
  4504. spin_lock(&adapter->stats64_lock);
  4505. igb_update_stats(adapter);
  4506. spin_unlock(&adapter->stats64_lock);
  4507. for (i = 0; i < adapter->num_tx_queues; i++) {
  4508. struct igb_ring *tx_ring = adapter->tx_ring[i];
  4509. if (!netif_carrier_ok(netdev)) {
  4510. /* We've lost link, so the controller stops DMA,
  4511. * but we've got queued Tx work that's never going
  4512. * to get done, so reset controller to flush Tx.
  4513. * (Do the reset outside of interrupt context).
  4514. */
  4515. if (igb_desc_unused(tx_ring) + 1 < tx_ring->count) {
  4516. adapter->tx_timeout_count++;
  4517. schedule_work(&adapter->reset_task);
  4518. /* return immediately since reset is imminent */
  4519. return;
  4520. }
  4521. }
  4522. /* Force detection of hung controller every watchdog period */
  4523. set_bit(IGB_RING_FLAG_TX_DETECT_HANG, &tx_ring->flags);
  4524. }
  4525. /* Cause software interrupt to ensure Rx ring is cleaned */
  4526. if (adapter->flags & IGB_FLAG_HAS_MSIX) {
  4527. u32 eics = 0;
  4528. for (i = 0; i < adapter->num_q_vectors; i++)
  4529. eics |= adapter->q_vector[i]->eims_value;
  4530. wr32(E1000_EICS, eics);
  4531. } else {
  4532. wr32(E1000_ICS, E1000_ICS_RXDMT0);
  4533. }
  4534. igb_spoof_check(adapter);
  4535. igb_ptp_rx_hang(adapter);
  4536. igb_ptp_tx_hang(adapter);
  4537. /* Check LVMMC register on i350/i354 only */
  4538. if ((adapter->hw.mac.type == e1000_i350) ||
  4539. (adapter->hw.mac.type == e1000_i354))
  4540. igb_check_lvmmc(adapter);
  4541. /* Reset the timer */
  4542. if (!test_bit(__IGB_DOWN, &adapter->state)) {
  4543. if (adapter->flags & IGB_FLAG_NEED_LINK_UPDATE)
  4544. mod_timer(&adapter->watchdog_timer,
  4545. round_jiffies(jiffies + HZ));
  4546. else
  4547. mod_timer(&adapter->watchdog_timer,
  4548. round_jiffies(jiffies + 2 * HZ));
  4549. }
  4550. }
  4551. enum latency_range {
  4552. lowest_latency = 0,
  4553. low_latency = 1,
  4554. bulk_latency = 2,
  4555. latency_invalid = 255
  4556. };
  4557. /**
  4558. * igb_update_ring_itr - update the dynamic ITR value based on packet size
  4559. * @q_vector: pointer to q_vector
  4560. *
  4561. * Stores a new ITR value based on strictly on packet size. This
  4562. * algorithm is less sophisticated than that used in igb_update_itr,
  4563. * due to the difficulty of synchronizing statistics across multiple
  4564. * receive rings. The divisors and thresholds used by this function
  4565. * were determined based on theoretical maximum wire speed and testing
  4566. * data, in order to minimize response time while increasing bulk
  4567. * throughput.
  4568. * This functionality is controlled by ethtool's coalescing settings.
  4569. * NOTE: This function is called only when operating in a multiqueue
  4570. * receive environment.
  4571. **/
  4572. static void igb_update_ring_itr(struct igb_q_vector *q_vector)
  4573. {
  4574. int new_val = q_vector->itr_val;
  4575. int avg_wire_size = 0;
  4576. struct igb_adapter *adapter = q_vector->adapter;
  4577. unsigned int packets;
  4578. /* For non-gigabit speeds, just fix the interrupt rate at 4000
  4579. * ints/sec - ITR timer value of 120 ticks.
  4580. */
  4581. if (adapter->link_speed != SPEED_1000) {
  4582. new_val = IGB_4K_ITR;
  4583. goto set_itr_val;
  4584. }
  4585. packets = q_vector->rx.total_packets;
  4586. if (packets)
  4587. avg_wire_size = q_vector->rx.total_bytes / packets;
  4588. packets = q_vector->tx.total_packets;
  4589. if (packets)
  4590. avg_wire_size = max_t(u32, avg_wire_size,
  4591. q_vector->tx.total_bytes / packets);
  4592. /* if avg_wire_size isn't set no work was done */
  4593. if (!avg_wire_size)
  4594. goto clear_counts;
  4595. /* Add 24 bytes to size to account for CRC, preamble, and gap */
  4596. avg_wire_size += 24;
  4597. /* Don't starve jumbo frames */
  4598. avg_wire_size = min(avg_wire_size, 3000);
  4599. /* Give a little boost to mid-size frames */
  4600. if ((avg_wire_size > 300) && (avg_wire_size < 1200))
  4601. new_val = avg_wire_size / 3;
  4602. else
  4603. new_val = avg_wire_size / 2;
  4604. /* conservative mode (itr 3) eliminates the lowest_latency setting */
  4605. if (new_val < IGB_20K_ITR &&
  4606. ((q_vector->rx.ring && adapter->rx_itr_setting == 3) ||
  4607. (!q_vector->rx.ring && adapter->tx_itr_setting == 3)))
  4608. new_val = IGB_20K_ITR;
  4609. set_itr_val:
  4610. if (new_val != q_vector->itr_val) {
  4611. q_vector->itr_val = new_val;
  4612. q_vector->set_itr = 1;
  4613. }
  4614. clear_counts:
  4615. q_vector->rx.total_bytes = 0;
  4616. q_vector->rx.total_packets = 0;
  4617. q_vector->tx.total_bytes = 0;
  4618. q_vector->tx.total_packets = 0;
  4619. }
  4620. /**
  4621. * igb_update_itr - update the dynamic ITR value based on statistics
  4622. * @q_vector: pointer to q_vector
  4623. * @ring_container: ring info to update the itr for
  4624. *
  4625. * Stores a new ITR value based on packets and byte
  4626. * counts during the last interrupt. The advantage of per interrupt
  4627. * computation is faster updates and more accurate ITR for the current
  4628. * traffic pattern. Constants in this function were computed
  4629. * based on theoretical maximum wire speed and thresholds were set based
  4630. * on testing data as well as attempting to minimize response time
  4631. * while increasing bulk throughput.
  4632. * This functionality is controlled by ethtool's coalescing settings.
  4633. * NOTE: These calculations are only valid when operating in a single-
  4634. * queue environment.
  4635. **/
  4636. static void igb_update_itr(struct igb_q_vector *q_vector,
  4637. struct igb_ring_container *ring_container)
  4638. {
  4639. unsigned int packets = ring_container->total_packets;
  4640. unsigned int bytes = ring_container->total_bytes;
  4641. u8 itrval = ring_container->itr;
  4642. /* no packets, exit with status unchanged */
  4643. if (packets == 0)
  4644. return;
  4645. switch (itrval) {
  4646. case lowest_latency:
  4647. /* handle TSO and jumbo frames */
  4648. if (bytes/packets > 8000)
  4649. itrval = bulk_latency;
  4650. else if ((packets < 5) && (bytes > 512))
  4651. itrval = low_latency;
  4652. break;
  4653. case low_latency: /* 50 usec aka 20000 ints/s */
  4654. if (bytes > 10000) {
  4655. /* this if handles the TSO accounting */
  4656. if (bytes/packets > 8000)
  4657. itrval = bulk_latency;
  4658. else if ((packets < 10) || ((bytes/packets) > 1200))
  4659. itrval = bulk_latency;
  4660. else if ((packets > 35))
  4661. itrval = lowest_latency;
  4662. } else if (bytes/packets > 2000) {
  4663. itrval = bulk_latency;
  4664. } else if (packets <= 2 && bytes < 512) {
  4665. itrval = lowest_latency;
  4666. }
  4667. break;
  4668. case bulk_latency: /* 250 usec aka 4000 ints/s */
  4669. if (bytes > 25000) {
  4670. if (packets > 35)
  4671. itrval = low_latency;
  4672. } else if (bytes < 1500) {
  4673. itrval = low_latency;
  4674. }
  4675. break;
  4676. }
  4677. /* clear work counters since we have the values we need */
  4678. ring_container->total_bytes = 0;
  4679. ring_container->total_packets = 0;
  4680. /* write updated itr to ring container */
  4681. ring_container->itr = itrval;
  4682. }
  4683. static void igb_set_itr(struct igb_q_vector *q_vector)
  4684. {
  4685. struct igb_adapter *adapter = q_vector->adapter;
  4686. u32 new_itr = q_vector->itr_val;
  4687. u8 current_itr = 0;
  4688. /* for non-gigabit speeds, just fix the interrupt rate at 4000 */
  4689. if (adapter->link_speed != SPEED_1000) {
  4690. current_itr = 0;
  4691. new_itr = IGB_4K_ITR;
  4692. goto set_itr_now;
  4693. }
  4694. igb_update_itr(q_vector, &q_vector->tx);
  4695. igb_update_itr(q_vector, &q_vector->rx);
  4696. current_itr = max(q_vector->rx.itr, q_vector->tx.itr);
  4697. /* conservative mode (itr 3) eliminates the lowest_latency setting */
  4698. if (current_itr == lowest_latency &&
  4699. ((q_vector->rx.ring && adapter->rx_itr_setting == 3) ||
  4700. (!q_vector->rx.ring && adapter->tx_itr_setting == 3)))
  4701. current_itr = low_latency;
  4702. switch (current_itr) {
  4703. /* counts and packets in update_itr are dependent on these numbers */
  4704. case lowest_latency:
  4705. new_itr = IGB_70K_ITR; /* 70,000 ints/sec */
  4706. break;
  4707. case low_latency:
  4708. new_itr = IGB_20K_ITR; /* 20,000 ints/sec */
  4709. break;
  4710. case bulk_latency:
  4711. new_itr = IGB_4K_ITR; /* 4,000 ints/sec */
  4712. break;
  4713. default:
  4714. break;
  4715. }
  4716. set_itr_now:
  4717. if (new_itr != q_vector->itr_val) {
  4718. /* this attempts to bias the interrupt rate towards Bulk
  4719. * by adding intermediate steps when interrupt rate is
  4720. * increasing
  4721. */
  4722. new_itr = new_itr > q_vector->itr_val ?
  4723. max((new_itr * q_vector->itr_val) /
  4724. (new_itr + (q_vector->itr_val >> 2)),
  4725. new_itr) : new_itr;
  4726. /* Don't write the value here; it resets the adapter's
  4727. * internal timer, and causes us to delay far longer than
  4728. * we should between interrupts. Instead, we write the ITR
  4729. * value at the beginning of the next interrupt so the timing
  4730. * ends up being correct.
  4731. */
  4732. q_vector->itr_val = new_itr;
  4733. q_vector->set_itr = 1;
  4734. }
  4735. }
  4736. static void igb_tx_ctxtdesc(struct igb_ring *tx_ring, u32 vlan_macip_lens,
  4737. u32 type_tucmd, u32 mss_l4len_idx)
  4738. {
  4739. struct e1000_adv_tx_context_desc *context_desc;
  4740. u16 i = tx_ring->next_to_use;
  4741. context_desc = IGB_TX_CTXTDESC(tx_ring, i);
  4742. i++;
  4743. tx_ring->next_to_use = (i < tx_ring->count) ? i : 0;
  4744. /* set bits to identify this as an advanced context descriptor */
  4745. type_tucmd |= E1000_TXD_CMD_DEXT | E1000_ADVTXD_DTYP_CTXT;
  4746. /* For 82575, context index must be unique per ring. */
  4747. if (test_bit(IGB_RING_FLAG_TX_CTX_IDX, &tx_ring->flags))
  4748. mss_l4len_idx |= tx_ring->reg_idx << 4;
  4749. context_desc->vlan_macip_lens = cpu_to_le32(vlan_macip_lens);
  4750. context_desc->seqnum_seed = 0;
  4751. context_desc->type_tucmd_mlhl = cpu_to_le32(type_tucmd);
  4752. context_desc->mss_l4len_idx = cpu_to_le32(mss_l4len_idx);
  4753. }
  4754. static int igb_tso(struct igb_ring *tx_ring,
  4755. struct igb_tx_buffer *first,
  4756. u8 *hdr_len)
  4757. {
  4758. u32 vlan_macip_lens, type_tucmd, mss_l4len_idx;
  4759. struct sk_buff *skb = first->skb;
  4760. union {
  4761. struct iphdr *v4;
  4762. struct ipv6hdr *v6;
  4763. unsigned char *hdr;
  4764. } ip;
  4765. union {
  4766. struct tcphdr *tcp;
  4767. unsigned char *hdr;
  4768. } l4;
  4769. u32 paylen, l4_offset;
  4770. int err;
  4771. if (skb->ip_summed != CHECKSUM_PARTIAL)
  4772. return 0;
  4773. if (!skb_is_gso(skb))
  4774. return 0;
  4775. err = skb_cow_head(skb, 0);
  4776. if (err < 0)
  4777. return err;
  4778. ip.hdr = skb_network_header(skb);
  4779. l4.hdr = skb_checksum_start(skb);
  4780. /* ADV DTYP TUCMD MKRLOC/ISCSIHEDLEN */
  4781. type_tucmd = E1000_ADVTXD_TUCMD_L4T_TCP;
  4782. /* initialize outer IP header fields */
  4783. if (ip.v4->version == 4) {
  4784. unsigned char *csum_start = skb_checksum_start(skb);
  4785. unsigned char *trans_start = ip.hdr + (ip.v4->ihl * 4);
  4786. /* IP header will have to cancel out any data that
  4787. * is not a part of the outer IP header
  4788. */
  4789. ip.v4->check = csum_fold(csum_partial(trans_start,
  4790. csum_start - trans_start,
  4791. 0));
  4792. type_tucmd |= E1000_ADVTXD_TUCMD_IPV4;
  4793. ip.v4->tot_len = 0;
  4794. first->tx_flags |= IGB_TX_FLAGS_TSO |
  4795. IGB_TX_FLAGS_CSUM |
  4796. IGB_TX_FLAGS_IPV4;
  4797. } else {
  4798. ip.v6->payload_len = 0;
  4799. first->tx_flags |= IGB_TX_FLAGS_TSO |
  4800. IGB_TX_FLAGS_CSUM;
  4801. }
  4802. /* determine offset of inner transport header */
  4803. l4_offset = l4.hdr - skb->data;
  4804. /* compute length of segmentation header */
  4805. *hdr_len = (l4.tcp->doff * 4) + l4_offset;
  4806. /* remove payload length from inner checksum */
  4807. paylen = skb->len - l4_offset;
  4808. csum_replace_by_diff(&l4.tcp->check, htonl(paylen));
  4809. /* update gso size and bytecount with header size */
  4810. first->gso_segs = skb_shinfo(skb)->gso_segs;
  4811. first->bytecount += (first->gso_segs - 1) * *hdr_len;
  4812. /* MSS L4LEN IDX */
  4813. mss_l4len_idx = (*hdr_len - l4_offset) << E1000_ADVTXD_L4LEN_SHIFT;
  4814. mss_l4len_idx |= skb_shinfo(skb)->gso_size << E1000_ADVTXD_MSS_SHIFT;
  4815. /* VLAN MACLEN IPLEN */
  4816. vlan_macip_lens = l4.hdr - ip.hdr;
  4817. vlan_macip_lens |= (ip.hdr - skb->data) << E1000_ADVTXD_MACLEN_SHIFT;
  4818. vlan_macip_lens |= first->tx_flags & IGB_TX_FLAGS_VLAN_MASK;
  4819. igb_tx_ctxtdesc(tx_ring, vlan_macip_lens, type_tucmd, mss_l4len_idx);
  4820. return 1;
  4821. }
  4822. static inline bool igb_ipv6_csum_is_sctp(struct sk_buff *skb)
  4823. {
  4824. unsigned int offset = 0;
  4825. ipv6_find_hdr(skb, &offset, IPPROTO_SCTP, NULL, NULL);
  4826. return offset == skb_checksum_start_offset(skb);
  4827. }
  4828. static void igb_tx_csum(struct igb_ring *tx_ring, struct igb_tx_buffer *first)
  4829. {
  4830. struct sk_buff *skb = first->skb;
  4831. u32 vlan_macip_lens = 0;
  4832. u32 type_tucmd = 0;
  4833. if (skb->ip_summed != CHECKSUM_PARTIAL) {
  4834. csum_failed:
  4835. if (!(first->tx_flags & IGB_TX_FLAGS_VLAN))
  4836. return;
  4837. goto no_csum;
  4838. }
  4839. switch (skb->csum_offset) {
  4840. case offsetof(struct tcphdr, check):
  4841. type_tucmd = E1000_ADVTXD_TUCMD_L4T_TCP;
  4842. /* fall through */
  4843. case offsetof(struct udphdr, check):
  4844. break;
  4845. case offsetof(struct sctphdr, checksum):
  4846. /* validate that this is actually an SCTP request */
  4847. if (((first->protocol == htons(ETH_P_IP)) &&
  4848. (ip_hdr(skb)->protocol == IPPROTO_SCTP)) ||
  4849. ((first->protocol == htons(ETH_P_IPV6)) &&
  4850. igb_ipv6_csum_is_sctp(skb))) {
  4851. type_tucmd = E1000_ADVTXD_TUCMD_L4T_SCTP;
  4852. break;
  4853. }
  4854. default:
  4855. skb_checksum_help(skb);
  4856. goto csum_failed;
  4857. }
  4858. /* update TX checksum flag */
  4859. first->tx_flags |= IGB_TX_FLAGS_CSUM;
  4860. vlan_macip_lens = skb_checksum_start_offset(skb) -
  4861. skb_network_offset(skb);
  4862. no_csum:
  4863. vlan_macip_lens |= skb_network_offset(skb) << E1000_ADVTXD_MACLEN_SHIFT;
  4864. vlan_macip_lens |= first->tx_flags & IGB_TX_FLAGS_VLAN_MASK;
  4865. igb_tx_ctxtdesc(tx_ring, vlan_macip_lens, type_tucmd, 0);
  4866. }
  4867. #define IGB_SET_FLAG(_input, _flag, _result) \
  4868. ((_flag <= _result) ? \
  4869. ((u32)(_input & _flag) * (_result / _flag)) : \
  4870. ((u32)(_input & _flag) / (_flag / _result)))
  4871. static u32 igb_tx_cmd_type(struct sk_buff *skb, u32 tx_flags)
  4872. {
  4873. /* set type for advanced descriptor with frame checksum insertion */
  4874. u32 cmd_type = E1000_ADVTXD_DTYP_DATA |
  4875. E1000_ADVTXD_DCMD_DEXT |
  4876. E1000_ADVTXD_DCMD_IFCS;
  4877. /* set HW vlan bit if vlan is present */
  4878. cmd_type |= IGB_SET_FLAG(tx_flags, IGB_TX_FLAGS_VLAN,
  4879. (E1000_ADVTXD_DCMD_VLE));
  4880. /* set segmentation bits for TSO */
  4881. cmd_type |= IGB_SET_FLAG(tx_flags, IGB_TX_FLAGS_TSO,
  4882. (E1000_ADVTXD_DCMD_TSE));
  4883. /* set timestamp bit if present */
  4884. cmd_type |= IGB_SET_FLAG(tx_flags, IGB_TX_FLAGS_TSTAMP,
  4885. (E1000_ADVTXD_MAC_TSTAMP));
  4886. /* insert frame checksum */
  4887. cmd_type ^= IGB_SET_FLAG(skb->no_fcs, 1, E1000_ADVTXD_DCMD_IFCS);
  4888. return cmd_type;
  4889. }
  4890. static void igb_tx_olinfo_status(struct igb_ring *tx_ring,
  4891. union e1000_adv_tx_desc *tx_desc,
  4892. u32 tx_flags, unsigned int paylen)
  4893. {
  4894. u32 olinfo_status = paylen << E1000_ADVTXD_PAYLEN_SHIFT;
  4895. /* 82575 requires a unique index per ring */
  4896. if (test_bit(IGB_RING_FLAG_TX_CTX_IDX, &tx_ring->flags))
  4897. olinfo_status |= tx_ring->reg_idx << 4;
  4898. /* insert L4 checksum */
  4899. olinfo_status |= IGB_SET_FLAG(tx_flags,
  4900. IGB_TX_FLAGS_CSUM,
  4901. (E1000_TXD_POPTS_TXSM << 8));
  4902. /* insert IPv4 checksum */
  4903. olinfo_status |= IGB_SET_FLAG(tx_flags,
  4904. IGB_TX_FLAGS_IPV4,
  4905. (E1000_TXD_POPTS_IXSM << 8));
  4906. tx_desc->read.olinfo_status = cpu_to_le32(olinfo_status);
  4907. }
  4908. static int __igb_maybe_stop_tx(struct igb_ring *tx_ring, const u16 size)
  4909. {
  4910. struct net_device *netdev = tx_ring->netdev;
  4911. netif_stop_subqueue(netdev, tx_ring->queue_index);
  4912. /* Herbert's original patch had:
  4913. * smp_mb__after_netif_stop_queue();
  4914. * but since that doesn't exist yet, just open code it.
  4915. */
  4916. smp_mb();
  4917. /* We need to check again in a case another CPU has just
  4918. * made room available.
  4919. */
  4920. if (igb_desc_unused(tx_ring) < size)
  4921. return -EBUSY;
  4922. /* A reprieve! */
  4923. netif_wake_subqueue(netdev, tx_ring->queue_index);
  4924. u64_stats_update_begin(&tx_ring->tx_syncp2);
  4925. tx_ring->tx_stats.restart_queue2++;
  4926. u64_stats_update_end(&tx_ring->tx_syncp2);
  4927. return 0;
  4928. }
  4929. static inline int igb_maybe_stop_tx(struct igb_ring *tx_ring, const u16 size)
  4930. {
  4931. if (igb_desc_unused(tx_ring) >= size)
  4932. return 0;
  4933. return __igb_maybe_stop_tx(tx_ring, size);
  4934. }
  4935. static int igb_tx_map(struct igb_ring *tx_ring,
  4936. struct igb_tx_buffer *first,
  4937. const u8 hdr_len)
  4938. {
  4939. struct sk_buff *skb = first->skb;
  4940. struct igb_tx_buffer *tx_buffer;
  4941. union e1000_adv_tx_desc *tx_desc;
  4942. struct skb_frag_struct *frag;
  4943. dma_addr_t dma;
  4944. unsigned int data_len, size;
  4945. u32 tx_flags = first->tx_flags;
  4946. u32 cmd_type = igb_tx_cmd_type(skb, tx_flags);
  4947. u16 i = tx_ring->next_to_use;
  4948. tx_desc = IGB_TX_DESC(tx_ring, i);
  4949. igb_tx_olinfo_status(tx_ring, tx_desc, tx_flags, skb->len - hdr_len);
  4950. size = skb_headlen(skb);
  4951. data_len = skb->data_len;
  4952. dma = dma_map_single(tx_ring->dev, skb->data, size, DMA_TO_DEVICE);
  4953. tx_buffer = first;
  4954. for (frag = &skb_shinfo(skb)->frags[0];; frag++) {
  4955. if (dma_mapping_error(tx_ring->dev, dma))
  4956. goto dma_error;
  4957. /* record length, and DMA address */
  4958. dma_unmap_len_set(tx_buffer, len, size);
  4959. dma_unmap_addr_set(tx_buffer, dma, dma);
  4960. tx_desc->read.buffer_addr = cpu_to_le64(dma);
  4961. while (unlikely(size > IGB_MAX_DATA_PER_TXD)) {
  4962. tx_desc->read.cmd_type_len =
  4963. cpu_to_le32(cmd_type ^ IGB_MAX_DATA_PER_TXD);
  4964. i++;
  4965. tx_desc++;
  4966. if (i == tx_ring->count) {
  4967. tx_desc = IGB_TX_DESC(tx_ring, 0);
  4968. i = 0;
  4969. }
  4970. tx_desc->read.olinfo_status = 0;
  4971. dma += IGB_MAX_DATA_PER_TXD;
  4972. size -= IGB_MAX_DATA_PER_TXD;
  4973. tx_desc->read.buffer_addr = cpu_to_le64(dma);
  4974. }
  4975. if (likely(!data_len))
  4976. break;
  4977. tx_desc->read.cmd_type_len = cpu_to_le32(cmd_type ^ size);
  4978. i++;
  4979. tx_desc++;
  4980. if (i == tx_ring->count) {
  4981. tx_desc = IGB_TX_DESC(tx_ring, 0);
  4982. i = 0;
  4983. }
  4984. tx_desc->read.olinfo_status = 0;
  4985. size = skb_frag_size(frag);
  4986. data_len -= size;
  4987. dma = skb_frag_dma_map(tx_ring->dev, frag, 0,
  4988. size, DMA_TO_DEVICE);
  4989. tx_buffer = &tx_ring->tx_buffer_info[i];
  4990. }
  4991. /* write last descriptor with RS and EOP bits */
  4992. cmd_type |= size | IGB_TXD_DCMD;
  4993. tx_desc->read.cmd_type_len = cpu_to_le32(cmd_type);
  4994. netdev_tx_sent_queue(txring_txq(tx_ring), first->bytecount);
  4995. /* set the timestamp */
  4996. first->time_stamp = jiffies;
  4997. /* Force memory writes to complete before letting h/w know there
  4998. * are new descriptors to fetch. (Only applicable for weak-ordered
  4999. * memory model archs, such as IA-64).
  5000. *
  5001. * We also need this memory barrier to make certain all of the
  5002. * status bits have been updated before next_to_watch is written.
  5003. */
  5004. wmb();
  5005. /* set next_to_watch value indicating a packet is present */
  5006. first->next_to_watch = tx_desc;
  5007. i++;
  5008. if (i == tx_ring->count)
  5009. i = 0;
  5010. tx_ring->next_to_use = i;
  5011. /* Make sure there is space in the ring for the next send. */
  5012. igb_maybe_stop_tx(tx_ring, DESC_NEEDED);
  5013. if (netif_xmit_stopped(txring_txq(tx_ring)) || !skb->xmit_more) {
  5014. writel(i, tx_ring->tail);
  5015. /* we need this if more than one processor can write to our tail
  5016. * at a time, it synchronizes IO on IA64/Altix systems
  5017. */
  5018. mmiowb();
  5019. }
  5020. return 0;
  5021. dma_error:
  5022. dev_err(tx_ring->dev, "TX DMA map failed\n");
  5023. tx_buffer = &tx_ring->tx_buffer_info[i];
  5024. /* clear dma mappings for failed tx_buffer_info map */
  5025. while (tx_buffer != first) {
  5026. if (dma_unmap_len(tx_buffer, len))
  5027. dma_unmap_page(tx_ring->dev,
  5028. dma_unmap_addr(tx_buffer, dma),
  5029. dma_unmap_len(tx_buffer, len),
  5030. DMA_TO_DEVICE);
  5031. dma_unmap_len_set(tx_buffer, len, 0);
  5032. if (i-- == 0)
  5033. i += tx_ring->count;
  5034. tx_buffer = &tx_ring->tx_buffer_info[i];
  5035. }
  5036. if (dma_unmap_len(tx_buffer, len))
  5037. dma_unmap_single(tx_ring->dev,
  5038. dma_unmap_addr(tx_buffer, dma),
  5039. dma_unmap_len(tx_buffer, len),
  5040. DMA_TO_DEVICE);
  5041. dma_unmap_len_set(tx_buffer, len, 0);
  5042. dev_kfree_skb_any(tx_buffer->skb);
  5043. tx_buffer->skb = NULL;
  5044. tx_ring->next_to_use = i;
  5045. return -1;
  5046. }
  5047. netdev_tx_t igb_xmit_frame_ring(struct sk_buff *skb,
  5048. struct igb_ring *tx_ring)
  5049. {
  5050. struct igb_tx_buffer *first;
  5051. int tso;
  5052. u32 tx_flags = 0;
  5053. unsigned short f;
  5054. u16 count = TXD_USE_COUNT(skb_headlen(skb));
  5055. __be16 protocol = vlan_get_protocol(skb);
  5056. u8 hdr_len = 0;
  5057. /* need: 1 descriptor per page * PAGE_SIZE/IGB_MAX_DATA_PER_TXD,
  5058. * + 1 desc for skb_headlen/IGB_MAX_DATA_PER_TXD,
  5059. * + 2 desc gap to keep tail from touching head,
  5060. * + 1 desc for context descriptor,
  5061. * otherwise try next time
  5062. */
  5063. for (f = 0; f < skb_shinfo(skb)->nr_frags; f++)
  5064. count += TXD_USE_COUNT(skb_shinfo(skb)->frags[f].size);
  5065. if (igb_maybe_stop_tx(tx_ring, count + 3)) {
  5066. /* this is a hard error */
  5067. return NETDEV_TX_BUSY;
  5068. }
  5069. /* record the location of the first descriptor for this packet */
  5070. first = &tx_ring->tx_buffer_info[tx_ring->next_to_use];
  5071. first->skb = skb;
  5072. first->bytecount = skb->len;
  5073. first->gso_segs = 1;
  5074. if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) {
  5075. struct igb_adapter *adapter = netdev_priv(tx_ring->netdev);
  5076. if (adapter->tstamp_config.tx_type == HWTSTAMP_TX_ON &&
  5077. !test_and_set_bit_lock(__IGB_PTP_TX_IN_PROGRESS,
  5078. &adapter->state)) {
  5079. skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
  5080. tx_flags |= IGB_TX_FLAGS_TSTAMP;
  5081. adapter->ptp_tx_skb = skb_get(skb);
  5082. adapter->ptp_tx_start = jiffies;
  5083. if (adapter->hw.mac.type == e1000_82576)
  5084. schedule_work(&adapter->ptp_tx_work);
  5085. } else {
  5086. adapter->tx_hwtstamp_skipped++;
  5087. }
  5088. }
  5089. skb_tx_timestamp(skb);
  5090. if (skb_vlan_tag_present(skb)) {
  5091. tx_flags |= IGB_TX_FLAGS_VLAN;
  5092. tx_flags |= (skb_vlan_tag_get(skb) << IGB_TX_FLAGS_VLAN_SHIFT);
  5093. }
  5094. /* record initial flags and protocol */
  5095. first->tx_flags = tx_flags;
  5096. first->protocol = protocol;
  5097. tso = igb_tso(tx_ring, first, &hdr_len);
  5098. if (tso < 0)
  5099. goto out_drop;
  5100. else if (!tso)
  5101. igb_tx_csum(tx_ring, first);
  5102. if (igb_tx_map(tx_ring, first, hdr_len))
  5103. goto cleanup_tx_tstamp;
  5104. return NETDEV_TX_OK;
  5105. out_drop:
  5106. dev_kfree_skb_any(first->skb);
  5107. first->skb = NULL;
  5108. cleanup_tx_tstamp:
  5109. if (unlikely(tx_flags & IGB_TX_FLAGS_TSTAMP)) {
  5110. struct igb_adapter *adapter = netdev_priv(tx_ring->netdev);
  5111. dev_kfree_skb_any(adapter->ptp_tx_skb);
  5112. adapter->ptp_tx_skb = NULL;
  5113. if (adapter->hw.mac.type == e1000_82576)
  5114. cancel_work_sync(&adapter->ptp_tx_work);
  5115. clear_bit_unlock(__IGB_PTP_TX_IN_PROGRESS, &adapter->state);
  5116. }
  5117. return NETDEV_TX_OK;
  5118. }
  5119. static inline struct igb_ring *igb_tx_queue_mapping(struct igb_adapter *adapter,
  5120. struct sk_buff *skb)
  5121. {
  5122. unsigned int r_idx = skb->queue_mapping;
  5123. if (r_idx >= adapter->num_tx_queues)
  5124. r_idx = r_idx % adapter->num_tx_queues;
  5125. return adapter->tx_ring[r_idx];
  5126. }
  5127. static netdev_tx_t igb_xmit_frame(struct sk_buff *skb,
  5128. struct net_device *netdev)
  5129. {
  5130. struct igb_adapter *adapter = netdev_priv(netdev);
  5131. /* The minimum packet size with TCTL.PSP set is 17 so pad the skb
  5132. * in order to meet this minimum size requirement.
  5133. */
  5134. if (skb_put_padto(skb, 17))
  5135. return NETDEV_TX_OK;
  5136. return igb_xmit_frame_ring(skb, igb_tx_queue_mapping(adapter, skb));
  5137. }
  5138. /**
  5139. * igb_tx_timeout - Respond to a Tx Hang
  5140. * @netdev: network interface device structure
  5141. **/
  5142. static void igb_tx_timeout(struct net_device *netdev)
  5143. {
  5144. struct igb_adapter *adapter = netdev_priv(netdev);
  5145. struct e1000_hw *hw = &adapter->hw;
  5146. /* Do the reset outside of interrupt context */
  5147. adapter->tx_timeout_count++;
  5148. if (hw->mac.type >= e1000_82580)
  5149. hw->dev_spec._82575.global_device_reset = true;
  5150. schedule_work(&adapter->reset_task);
  5151. wr32(E1000_EICS,
  5152. (adapter->eims_enable_mask & ~adapter->eims_other));
  5153. }
  5154. static void igb_reset_task(struct work_struct *work)
  5155. {
  5156. struct igb_adapter *adapter;
  5157. adapter = container_of(work, struct igb_adapter, reset_task);
  5158. igb_dump(adapter);
  5159. netdev_err(adapter->netdev, "Reset adapter\n");
  5160. igb_reinit_locked(adapter);
  5161. }
  5162. /**
  5163. * igb_get_stats64 - Get System Network Statistics
  5164. * @netdev: network interface device structure
  5165. * @stats: rtnl_link_stats64 pointer
  5166. **/
  5167. static void igb_get_stats64(struct net_device *netdev,
  5168. struct rtnl_link_stats64 *stats)
  5169. {
  5170. struct igb_adapter *adapter = netdev_priv(netdev);
  5171. spin_lock(&adapter->stats64_lock);
  5172. igb_update_stats(adapter);
  5173. memcpy(stats, &adapter->stats64, sizeof(*stats));
  5174. spin_unlock(&adapter->stats64_lock);
  5175. }
  5176. /**
  5177. * igb_change_mtu - Change the Maximum Transfer Unit
  5178. * @netdev: network interface device structure
  5179. * @new_mtu: new value for maximum frame size
  5180. *
  5181. * Returns 0 on success, negative on failure
  5182. **/
  5183. static int igb_change_mtu(struct net_device *netdev, int new_mtu)
  5184. {
  5185. struct igb_adapter *adapter = netdev_priv(netdev);
  5186. struct pci_dev *pdev = adapter->pdev;
  5187. int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
  5188. /* adjust max frame to be at least the size of a standard frame */
  5189. if (max_frame < (ETH_FRAME_LEN + ETH_FCS_LEN))
  5190. max_frame = ETH_FRAME_LEN + ETH_FCS_LEN;
  5191. while (test_and_set_bit(__IGB_RESETTING, &adapter->state))
  5192. usleep_range(1000, 2000);
  5193. /* igb_down has a dependency on max_frame_size */
  5194. adapter->max_frame_size = max_frame;
  5195. if (netif_running(netdev))
  5196. igb_down(adapter);
  5197. dev_info(&pdev->dev, "changing MTU from %d to %d\n",
  5198. netdev->mtu, new_mtu);
  5199. netdev->mtu = new_mtu;
  5200. if (netif_running(netdev))
  5201. igb_up(adapter);
  5202. else
  5203. igb_reset(adapter);
  5204. clear_bit(__IGB_RESETTING, &adapter->state);
  5205. return 0;
  5206. }
  5207. /**
  5208. * igb_update_stats - Update the board statistics counters
  5209. * @adapter: board private structure
  5210. **/
  5211. void igb_update_stats(struct igb_adapter *adapter)
  5212. {
  5213. struct rtnl_link_stats64 *net_stats = &adapter->stats64;
  5214. struct e1000_hw *hw = &adapter->hw;
  5215. struct pci_dev *pdev = adapter->pdev;
  5216. u32 reg, mpc;
  5217. int i;
  5218. u64 bytes, packets;
  5219. unsigned int start;
  5220. u64 _bytes, _packets;
  5221. /* Prevent stats update while adapter is being reset, or if the pci
  5222. * connection is down.
  5223. */
  5224. if (adapter->link_speed == 0)
  5225. return;
  5226. if (pci_channel_offline(pdev))
  5227. return;
  5228. bytes = 0;
  5229. packets = 0;
  5230. rcu_read_lock();
  5231. for (i = 0; i < adapter->num_rx_queues; i++) {
  5232. struct igb_ring *ring = adapter->rx_ring[i];
  5233. u32 rqdpc = rd32(E1000_RQDPC(i));
  5234. if (hw->mac.type >= e1000_i210)
  5235. wr32(E1000_RQDPC(i), 0);
  5236. if (rqdpc) {
  5237. ring->rx_stats.drops += rqdpc;
  5238. net_stats->rx_fifo_errors += rqdpc;
  5239. }
  5240. do {
  5241. start = u64_stats_fetch_begin_irq(&ring->rx_syncp);
  5242. _bytes = ring->rx_stats.bytes;
  5243. _packets = ring->rx_stats.packets;
  5244. } while (u64_stats_fetch_retry_irq(&ring->rx_syncp, start));
  5245. bytes += _bytes;
  5246. packets += _packets;
  5247. }
  5248. net_stats->rx_bytes = bytes;
  5249. net_stats->rx_packets = packets;
  5250. bytes = 0;
  5251. packets = 0;
  5252. for (i = 0; i < adapter->num_tx_queues; i++) {
  5253. struct igb_ring *ring = adapter->tx_ring[i];
  5254. do {
  5255. start = u64_stats_fetch_begin_irq(&ring->tx_syncp);
  5256. _bytes = ring->tx_stats.bytes;
  5257. _packets = ring->tx_stats.packets;
  5258. } while (u64_stats_fetch_retry_irq(&ring->tx_syncp, start));
  5259. bytes += _bytes;
  5260. packets += _packets;
  5261. }
  5262. net_stats->tx_bytes = bytes;
  5263. net_stats->tx_packets = packets;
  5264. rcu_read_unlock();
  5265. /* read stats registers */
  5266. adapter->stats.crcerrs += rd32(E1000_CRCERRS);
  5267. adapter->stats.gprc += rd32(E1000_GPRC);
  5268. adapter->stats.gorc += rd32(E1000_GORCL);
  5269. rd32(E1000_GORCH); /* clear GORCL */
  5270. adapter->stats.bprc += rd32(E1000_BPRC);
  5271. adapter->stats.mprc += rd32(E1000_MPRC);
  5272. adapter->stats.roc += rd32(E1000_ROC);
  5273. adapter->stats.prc64 += rd32(E1000_PRC64);
  5274. adapter->stats.prc127 += rd32(E1000_PRC127);
  5275. adapter->stats.prc255 += rd32(E1000_PRC255);
  5276. adapter->stats.prc511 += rd32(E1000_PRC511);
  5277. adapter->stats.prc1023 += rd32(E1000_PRC1023);
  5278. adapter->stats.prc1522 += rd32(E1000_PRC1522);
  5279. adapter->stats.symerrs += rd32(E1000_SYMERRS);
  5280. adapter->stats.sec += rd32(E1000_SEC);
  5281. mpc = rd32(E1000_MPC);
  5282. adapter->stats.mpc += mpc;
  5283. net_stats->rx_fifo_errors += mpc;
  5284. adapter->stats.scc += rd32(E1000_SCC);
  5285. adapter->stats.ecol += rd32(E1000_ECOL);
  5286. adapter->stats.mcc += rd32(E1000_MCC);
  5287. adapter->stats.latecol += rd32(E1000_LATECOL);
  5288. adapter->stats.dc += rd32(E1000_DC);
  5289. adapter->stats.rlec += rd32(E1000_RLEC);
  5290. adapter->stats.xonrxc += rd32(E1000_XONRXC);
  5291. adapter->stats.xontxc += rd32(E1000_XONTXC);
  5292. adapter->stats.xoffrxc += rd32(E1000_XOFFRXC);
  5293. adapter->stats.xofftxc += rd32(E1000_XOFFTXC);
  5294. adapter->stats.fcruc += rd32(E1000_FCRUC);
  5295. adapter->stats.gptc += rd32(E1000_GPTC);
  5296. adapter->stats.gotc += rd32(E1000_GOTCL);
  5297. rd32(E1000_GOTCH); /* clear GOTCL */
  5298. adapter->stats.rnbc += rd32(E1000_RNBC);
  5299. adapter->stats.ruc += rd32(E1000_RUC);
  5300. adapter->stats.rfc += rd32(E1000_RFC);
  5301. adapter->stats.rjc += rd32(E1000_RJC);
  5302. adapter->stats.tor += rd32(E1000_TORH);
  5303. adapter->stats.tot += rd32(E1000_TOTH);
  5304. adapter->stats.tpr += rd32(E1000_TPR);
  5305. adapter->stats.ptc64 += rd32(E1000_PTC64);
  5306. adapter->stats.ptc127 += rd32(E1000_PTC127);
  5307. adapter->stats.ptc255 += rd32(E1000_PTC255);
  5308. adapter->stats.ptc511 += rd32(E1000_PTC511);
  5309. adapter->stats.ptc1023 += rd32(E1000_PTC1023);
  5310. adapter->stats.ptc1522 += rd32(E1000_PTC1522);
  5311. adapter->stats.mptc += rd32(E1000_MPTC);
  5312. adapter->stats.bptc += rd32(E1000_BPTC);
  5313. adapter->stats.tpt += rd32(E1000_TPT);
  5314. adapter->stats.colc += rd32(E1000_COLC);
  5315. adapter->stats.algnerrc += rd32(E1000_ALGNERRC);
  5316. /* read internal phy specific stats */
  5317. reg = rd32(E1000_CTRL_EXT);
  5318. if (!(reg & E1000_CTRL_EXT_LINK_MODE_MASK)) {
  5319. adapter->stats.rxerrc += rd32(E1000_RXERRC);
  5320. /* this stat has invalid values on i210/i211 */
  5321. if ((hw->mac.type != e1000_i210) &&
  5322. (hw->mac.type != e1000_i211))
  5323. adapter->stats.tncrs += rd32(E1000_TNCRS);
  5324. }
  5325. adapter->stats.tsctc += rd32(E1000_TSCTC);
  5326. adapter->stats.tsctfc += rd32(E1000_TSCTFC);
  5327. adapter->stats.iac += rd32(E1000_IAC);
  5328. adapter->stats.icrxoc += rd32(E1000_ICRXOC);
  5329. adapter->stats.icrxptc += rd32(E1000_ICRXPTC);
  5330. adapter->stats.icrxatc += rd32(E1000_ICRXATC);
  5331. adapter->stats.ictxptc += rd32(E1000_ICTXPTC);
  5332. adapter->stats.ictxatc += rd32(E1000_ICTXATC);
  5333. adapter->stats.ictxqec += rd32(E1000_ICTXQEC);
  5334. adapter->stats.ictxqmtc += rd32(E1000_ICTXQMTC);
  5335. adapter->stats.icrxdmtc += rd32(E1000_ICRXDMTC);
  5336. /* Fill out the OS statistics structure */
  5337. net_stats->multicast = adapter->stats.mprc;
  5338. net_stats->collisions = adapter->stats.colc;
  5339. /* Rx Errors */
  5340. /* RLEC on some newer hardware can be incorrect so build
  5341. * our own version based on RUC and ROC
  5342. */
  5343. net_stats->rx_errors = adapter->stats.rxerrc +
  5344. adapter->stats.crcerrs + adapter->stats.algnerrc +
  5345. adapter->stats.ruc + adapter->stats.roc +
  5346. adapter->stats.cexterr;
  5347. net_stats->rx_length_errors = adapter->stats.ruc +
  5348. adapter->stats.roc;
  5349. net_stats->rx_crc_errors = adapter->stats.crcerrs;
  5350. net_stats->rx_frame_errors = adapter->stats.algnerrc;
  5351. net_stats->rx_missed_errors = adapter->stats.mpc;
  5352. /* Tx Errors */
  5353. net_stats->tx_errors = adapter->stats.ecol +
  5354. adapter->stats.latecol;
  5355. net_stats->tx_aborted_errors = adapter->stats.ecol;
  5356. net_stats->tx_window_errors = adapter->stats.latecol;
  5357. net_stats->tx_carrier_errors = adapter->stats.tncrs;
  5358. /* Tx Dropped needs to be maintained elsewhere */
  5359. /* Management Stats */
  5360. adapter->stats.mgptc += rd32(E1000_MGTPTC);
  5361. adapter->stats.mgprc += rd32(E1000_MGTPRC);
  5362. adapter->stats.mgpdc += rd32(E1000_MGTPDC);
  5363. /* OS2BMC Stats */
  5364. reg = rd32(E1000_MANC);
  5365. if (reg & E1000_MANC_EN_BMC2OS) {
  5366. adapter->stats.o2bgptc += rd32(E1000_O2BGPTC);
  5367. adapter->stats.o2bspc += rd32(E1000_O2BSPC);
  5368. adapter->stats.b2ospc += rd32(E1000_B2OSPC);
  5369. adapter->stats.b2ogprc += rd32(E1000_B2OGPRC);
  5370. }
  5371. }
  5372. static void igb_tsync_interrupt(struct igb_adapter *adapter)
  5373. {
  5374. struct e1000_hw *hw = &adapter->hw;
  5375. struct ptp_clock_event event;
  5376. struct timespec64 ts;
  5377. u32 ack = 0, tsauxc, sec, nsec, tsicr = rd32(E1000_TSICR);
  5378. if (tsicr & TSINTR_SYS_WRAP) {
  5379. event.type = PTP_CLOCK_PPS;
  5380. if (adapter->ptp_caps.pps)
  5381. ptp_clock_event(adapter->ptp_clock, &event);
  5382. ack |= TSINTR_SYS_WRAP;
  5383. }
  5384. if (tsicr & E1000_TSICR_TXTS) {
  5385. /* retrieve hardware timestamp */
  5386. schedule_work(&adapter->ptp_tx_work);
  5387. ack |= E1000_TSICR_TXTS;
  5388. }
  5389. if (tsicr & TSINTR_TT0) {
  5390. spin_lock(&adapter->tmreg_lock);
  5391. ts = timespec64_add(adapter->perout[0].start,
  5392. adapter->perout[0].period);
  5393. /* u32 conversion of tv_sec is safe until y2106 */
  5394. wr32(E1000_TRGTTIML0, ts.tv_nsec);
  5395. wr32(E1000_TRGTTIMH0, (u32)ts.tv_sec);
  5396. tsauxc = rd32(E1000_TSAUXC);
  5397. tsauxc |= TSAUXC_EN_TT0;
  5398. wr32(E1000_TSAUXC, tsauxc);
  5399. adapter->perout[0].start = ts;
  5400. spin_unlock(&adapter->tmreg_lock);
  5401. ack |= TSINTR_TT0;
  5402. }
  5403. if (tsicr & TSINTR_TT1) {
  5404. spin_lock(&adapter->tmreg_lock);
  5405. ts = timespec64_add(adapter->perout[1].start,
  5406. adapter->perout[1].period);
  5407. wr32(E1000_TRGTTIML1, ts.tv_nsec);
  5408. wr32(E1000_TRGTTIMH1, (u32)ts.tv_sec);
  5409. tsauxc = rd32(E1000_TSAUXC);
  5410. tsauxc |= TSAUXC_EN_TT1;
  5411. wr32(E1000_TSAUXC, tsauxc);
  5412. adapter->perout[1].start = ts;
  5413. spin_unlock(&adapter->tmreg_lock);
  5414. ack |= TSINTR_TT1;
  5415. }
  5416. if (tsicr & TSINTR_AUTT0) {
  5417. nsec = rd32(E1000_AUXSTMPL0);
  5418. sec = rd32(E1000_AUXSTMPH0);
  5419. event.type = PTP_CLOCK_EXTTS;
  5420. event.index = 0;
  5421. event.timestamp = sec * 1000000000ULL + nsec;
  5422. ptp_clock_event(adapter->ptp_clock, &event);
  5423. ack |= TSINTR_AUTT0;
  5424. }
  5425. if (tsicr & TSINTR_AUTT1) {
  5426. nsec = rd32(E1000_AUXSTMPL1);
  5427. sec = rd32(E1000_AUXSTMPH1);
  5428. event.type = PTP_CLOCK_EXTTS;
  5429. event.index = 1;
  5430. event.timestamp = sec * 1000000000ULL + nsec;
  5431. ptp_clock_event(adapter->ptp_clock, &event);
  5432. ack |= TSINTR_AUTT1;
  5433. }
  5434. /* acknowledge the interrupts */
  5435. wr32(E1000_TSICR, ack);
  5436. }
  5437. static irqreturn_t igb_msix_other(int irq, void *data)
  5438. {
  5439. struct igb_adapter *adapter = data;
  5440. struct e1000_hw *hw = &adapter->hw;
  5441. u32 icr = rd32(E1000_ICR);
  5442. /* reading ICR causes bit 31 of EICR to be cleared */
  5443. if (icr & E1000_ICR_DRSTA)
  5444. schedule_work(&adapter->reset_task);
  5445. if (icr & E1000_ICR_DOUTSYNC) {
  5446. /* HW is reporting DMA is out of sync */
  5447. adapter->stats.doosync++;
  5448. /* The DMA Out of Sync is also indication of a spoof event
  5449. * in IOV mode. Check the Wrong VM Behavior register to
  5450. * see if it is really a spoof event.
  5451. */
  5452. igb_check_wvbr(adapter);
  5453. }
  5454. /* Check for a mailbox event */
  5455. if (icr & E1000_ICR_VMMB)
  5456. igb_msg_task(adapter);
  5457. if (icr & E1000_ICR_LSC) {
  5458. hw->mac.get_link_status = 1;
  5459. /* guard against interrupt when we're going down */
  5460. if (!test_bit(__IGB_DOWN, &adapter->state))
  5461. mod_timer(&adapter->watchdog_timer, jiffies + 1);
  5462. }
  5463. if (icr & E1000_ICR_TS)
  5464. igb_tsync_interrupt(adapter);
  5465. wr32(E1000_EIMS, adapter->eims_other);
  5466. return IRQ_HANDLED;
  5467. }
  5468. static void igb_write_itr(struct igb_q_vector *q_vector)
  5469. {
  5470. struct igb_adapter *adapter = q_vector->adapter;
  5471. u32 itr_val = q_vector->itr_val & 0x7FFC;
  5472. if (!q_vector->set_itr)
  5473. return;
  5474. if (!itr_val)
  5475. itr_val = 0x4;
  5476. if (adapter->hw.mac.type == e1000_82575)
  5477. itr_val |= itr_val << 16;
  5478. else
  5479. itr_val |= E1000_EITR_CNT_IGNR;
  5480. writel(itr_val, q_vector->itr_register);
  5481. q_vector->set_itr = 0;
  5482. }
  5483. static irqreturn_t igb_msix_ring(int irq, void *data)
  5484. {
  5485. struct igb_q_vector *q_vector = data;
  5486. /* Write the ITR value calculated from the previous interrupt. */
  5487. igb_write_itr(q_vector);
  5488. napi_schedule(&q_vector->napi);
  5489. return IRQ_HANDLED;
  5490. }
  5491. #ifdef CONFIG_IGB_DCA
  5492. static void igb_update_tx_dca(struct igb_adapter *adapter,
  5493. struct igb_ring *tx_ring,
  5494. int cpu)
  5495. {
  5496. struct e1000_hw *hw = &adapter->hw;
  5497. u32 txctrl = dca3_get_tag(tx_ring->dev, cpu);
  5498. if (hw->mac.type != e1000_82575)
  5499. txctrl <<= E1000_DCA_TXCTRL_CPUID_SHIFT;
  5500. /* We can enable relaxed ordering for reads, but not writes when
  5501. * DCA is enabled. This is due to a known issue in some chipsets
  5502. * which will cause the DCA tag to be cleared.
  5503. */
  5504. txctrl |= E1000_DCA_TXCTRL_DESC_RRO_EN |
  5505. E1000_DCA_TXCTRL_DATA_RRO_EN |
  5506. E1000_DCA_TXCTRL_DESC_DCA_EN;
  5507. wr32(E1000_DCA_TXCTRL(tx_ring->reg_idx), txctrl);
  5508. }
  5509. static void igb_update_rx_dca(struct igb_adapter *adapter,
  5510. struct igb_ring *rx_ring,
  5511. int cpu)
  5512. {
  5513. struct e1000_hw *hw = &adapter->hw;
  5514. u32 rxctrl = dca3_get_tag(&adapter->pdev->dev, cpu);
  5515. if (hw->mac.type != e1000_82575)
  5516. rxctrl <<= E1000_DCA_RXCTRL_CPUID_SHIFT;
  5517. /* We can enable relaxed ordering for reads, but not writes when
  5518. * DCA is enabled. This is due to a known issue in some chipsets
  5519. * which will cause the DCA tag to be cleared.
  5520. */
  5521. rxctrl |= E1000_DCA_RXCTRL_DESC_RRO_EN |
  5522. E1000_DCA_RXCTRL_DESC_DCA_EN;
  5523. wr32(E1000_DCA_RXCTRL(rx_ring->reg_idx), rxctrl);
  5524. }
  5525. static void igb_update_dca(struct igb_q_vector *q_vector)
  5526. {
  5527. struct igb_adapter *adapter = q_vector->adapter;
  5528. int cpu = get_cpu();
  5529. if (q_vector->cpu == cpu)
  5530. goto out_no_update;
  5531. if (q_vector->tx.ring)
  5532. igb_update_tx_dca(adapter, q_vector->tx.ring, cpu);
  5533. if (q_vector->rx.ring)
  5534. igb_update_rx_dca(adapter, q_vector->rx.ring, cpu);
  5535. q_vector->cpu = cpu;
  5536. out_no_update:
  5537. put_cpu();
  5538. }
  5539. static void igb_setup_dca(struct igb_adapter *adapter)
  5540. {
  5541. struct e1000_hw *hw = &adapter->hw;
  5542. int i;
  5543. if (!(adapter->flags & IGB_FLAG_DCA_ENABLED))
  5544. return;
  5545. /* Always use CB2 mode, difference is masked in the CB driver. */
  5546. wr32(E1000_DCA_CTRL, E1000_DCA_CTRL_DCA_MODE_CB2);
  5547. for (i = 0; i < adapter->num_q_vectors; i++) {
  5548. adapter->q_vector[i]->cpu = -1;
  5549. igb_update_dca(adapter->q_vector[i]);
  5550. }
  5551. }
  5552. static int __igb_notify_dca(struct device *dev, void *data)
  5553. {
  5554. struct net_device *netdev = dev_get_drvdata(dev);
  5555. struct igb_adapter *adapter = netdev_priv(netdev);
  5556. struct pci_dev *pdev = adapter->pdev;
  5557. struct e1000_hw *hw = &adapter->hw;
  5558. unsigned long event = *(unsigned long *)data;
  5559. switch (event) {
  5560. case DCA_PROVIDER_ADD:
  5561. /* if already enabled, don't do it again */
  5562. if (adapter->flags & IGB_FLAG_DCA_ENABLED)
  5563. break;
  5564. if (dca_add_requester(dev) == 0) {
  5565. adapter->flags |= IGB_FLAG_DCA_ENABLED;
  5566. dev_info(&pdev->dev, "DCA enabled\n");
  5567. igb_setup_dca(adapter);
  5568. break;
  5569. }
  5570. /* Fall Through since DCA is disabled. */
  5571. case DCA_PROVIDER_REMOVE:
  5572. if (adapter->flags & IGB_FLAG_DCA_ENABLED) {
  5573. /* without this a class_device is left
  5574. * hanging around in the sysfs model
  5575. */
  5576. dca_remove_requester(dev);
  5577. dev_info(&pdev->dev, "DCA disabled\n");
  5578. adapter->flags &= ~IGB_FLAG_DCA_ENABLED;
  5579. wr32(E1000_DCA_CTRL, E1000_DCA_CTRL_DCA_MODE_DISABLE);
  5580. }
  5581. break;
  5582. }
  5583. return 0;
  5584. }
  5585. static int igb_notify_dca(struct notifier_block *nb, unsigned long event,
  5586. void *p)
  5587. {
  5588. int ret_val;
  5589. ret_val = driver_for_each_device(&igb_driver.driver, NULL, &event,
  5590. __igb_notify_dca);
  5591. return ret_val ? NOTIFY_BAD : NOTIFY_DONE;
  5592. }
  5593. #endif /* CONFIG_IGB_DCA */
  5594. #ifdef CONFIG_PCI_IOV
  5595. static int igb_vf_configure(struct igb_adapter *adapter, int vf)
  5596. {
  5597. unsigned char mac_addr[ETH_ALEN];
  5598. eth_zero_addr(mac_addr);
  5599. igb_set_vf_mac(adapter, vf, mac_addr);
  5600. /* By default spoof check is enabled for all VFs */
  5601. adapter->vf_data[vf].spoofchk_enabled = true;
  5602. /* By default VFs are not trusted */
  5603. adapter->vf_data[vf].trusted = false;
  5604. return 0;
  5605. }
  5606. #endif
  5607. static void igb_ping_all_vfs(struct igb_adapter *adapter)
  5608. {
  5609. struct e1000_hw *hw = &adapter->hw;
  5610. u32 ping;
  5611. int i;
  5612. for (i = 0 ; i < adapter->vfs_allocated_count; i++) {
  5613. ping = E1000_PF_CONTROL_MSG;
  5614. if (adapter->vf_data[i].flags & IGB_VF_FLAG_CTS)
  5615. ping |= E1000_VT_MSGTYPE_CTS;
  5616. igb_write_mbx(hw, &ping, 1, i);
  5617. }
  5618. }
  5619. static int igb_set_vf_promisc(struct igb_adapter *adapter, u32 *msgbuf, u32 vf)
  5620. {
  5621. struct e1000_hw *hw = &adapter->hw;
  5622. u32 vmolr = rd32(E1000_VMOLR(vf));
  5623. struct vf_data_storage *vf_data = &adapter->vf_data[vf];
  5624. vf_data->flags &= ~(IGB_VF_FLAG_UNI_PROMISC |
  5625. IGB_VF_FLAG_MULTI_PROMISC);
  5626. vmolr &= ~(E1000_VMOLR_ROPE | E1000_VMOLR_ROMPE | E1000_VMOLR_MPME);
  5627. if (*msgbuf & E1000_VF_SET_PROMISC_MULTICAST) {
  5628. vmolr |= E1000_VMOLR_MPME;
  5629. vf_data->flags |= IGB_VF_FLAG_MULTI_PROMISC;
  5630. *msgbuf &= ~E1000_VF_SET_PROMISC_MULTICAST;
  5631. } else {
  5632. /* if we have hashes and we are clearing a multicast promisc
  5633. * flag we need to write the hashes to the MTA as this step
  5634. * was previously skipped
  5635. */
  5636. if (vf_data->num_vf_mc_hashes > 30) {
  5637. vmolr |= E1000_VMOLR_MPME;
  5638. } else if (vf_data->num_vf_mc_hashes) {
  5639. int j;
  5640. vmolr |= E1000_VMOLR_ROMPE;
  5641. for (j = 0; j < vf_data->num_vf_mc_hashes; j++)
  5642. igb_mta_set(hw, vf_data->vf_mc_hashes[j]);
  5643. }
  5644. }
  5645. wr32(E1000_VMOLR(vf), vmolr);
  5646. /* there are flags left unprocessed, likely not supported */
  5647. if (*msgbuf & E1000_VT_MSGINFO_MASK)
  5648. return -EINVAL;
  5649. return 0;
  5650. }
  5651. static int igb_set_vf_multicasts(struct igb_adapter *adapter,
  5652. u32 *msgbuf, u32 vf)
  5653. {
  5654. int n = (msgbuf[0] & E1000_VT_MSGINFO_MASK) >> E1000_VT_MSGINFO_SHIFT;
  5655. u16 *hash_list = (u16 *)&msgbuf[1];
  5656. struct vf_data_storage *vf_data = &adapter->vf_data[vf];
  5657. int i;
  5658. /* salt away the number of multicast addresses assigned
  5659. * to this VF for later use to restore when the PF multi cast
  5660. * list changes
  5661. */
  5662. vf_data->num_vf_mc_hashes = n;
  5663. /* only up to 30 hash values supported */
  5664. if (n > 30)
  5665. n = 30;
  5666. /* store the hashes for later use */
  5667. for (i = 0; i < n; i++)
  5668. vf_data->vf_mc_hashes[i] = hash_list[i];
  5669. /* Flush and reset the mta with the new values */
  5670. igb_set_rx_mode(adapter->netdev);
  5671. return 0;
  5672. }
  5673. static void igb_restore_vf_multicasts(struct igb_adapter *adapter)
  5674. {
  5675. struct e1000_hw *hw = &adapter->hw;
  5676. struct vf_data_storage *vf_data;
  5677. int i, j;
  5678. for (i = 0; i < adapter->vfs_allocated_count; i++) {
  5679. u32 vmolr = rd32(E1000_VMOLR(i));
  5680. vmolr &= ~(E1000_VMOLR_ROMPE | E1000_VMOLR_MPME);
  5681. vf_data = &adapter->vf_data[i];
  5682. if ((vf_data->num_vf_mc_hashes > 30) ||
  5683. (vf_data->flags & IGB_VF_FLAG_MULTI_PROMISC)) {
  5684. vmolr |= E1000_VMOLR_MPME;
  5685. } else if (vf_data->num_vf_mc_hashes) {
  5686. vmolr |= E1000_VMOLR_ROMPE;
  5687. for (j = 0; j < vf_data->num_vf_mc_hashes; j++)
  5688. igb_mta_set(hw, vf_data->vf_mc_hashes[j]);
  5689. }
  5690. wr32(E1000_VMOLR(i), vmolr);
  5691. }
  5692. }
  5693. static void igb_clear_vf_vfta(struct igb_adapter *adapter, u32 vf)
  5694. {
  5695. struct e1000_hw *hw = &adapter->hw;
  5696. u32 pool_mask, vlvf_mask, i;
  5697. /* create mask for VF and other pools */
  5698. pool_mask = E1000_VLVF_POOLSEL_MASK;
  5699. vlvf_mask = BIT(E1000_VLVF_POOLSEL_SHIFT + vf);
  5700. /* drop PF from pool bits */
  5701. pool_mask &= ~BIT(E1000_VLVF_POOLSEL_SHIFT +
  5702. adapter->vfs_allocated_count);
  5703. /* Find the vlan filter for this id */
  5704. for (i = E1000_VLVF_ARRAY_SIZE; i--;) {
  5705. u32 vlvf = rd32(E1000_VLVF(i));
  5706. u32 vfta_mask, vid, vfta;
  5707. /* remove the vf from the pool */
  5708. if (!(vlvf & vlvf_mask))
  5709. continue;
  5710. /* clear out bit from VLVF */
  5711. vlvf ^= vlvf_mask;
  5712. /* if other pools are present, just remove ourselves */
  5713. if (vlvf & pool_mask)
  5714. goto update_vlvfb;
  5715. /* if PF is present, leave VFTA */
  5716. if (vlvf & E1000_VLVF_POOLSEL_MASK)
  5717. goto update_vlvf;
  5718. vid = vlvf & E1000_VLVF_VLANID_MASK;
  5719. vfta_mask = BIT(vid % 32);
  5720. /* clear bit from VFTA */
  5721. vfta = adapter->shadow_vfta[vid / 32];
  5722. if (vfta & vfta_mask)
  5723. hw->mac.ops.write_vfta(hw, vid / 32, vfta ^ vfta_mask);
  5724. update_vlvf:
  5725. /* clear pool selection enable */
  5726. if (adapter->flags & IGB_FLAG_VLAN_PROMISC)
  5727. vlvf &= E1000_VLVF_POOLSEL_MASK;
  5728. else
  5729. vlvf = 0;
  5730. update_vlvfb:
  5731. /* clear pool bits */
  5732. wr32(E1000_VLVF(i), vlvf);
  5733. }
  5734. }
  5735. static int igb_find_vlvf_entry(struct e1000_hw *hw, u32 vlan)
  5736. {
  5737. u32 vlvf;
  5738. int idx;
  5739. /* short cut the special case */
  5740. if (vlan == 0)
  5741. return 0;
  5742. /* Search for the VLAN id in the VLVF entries */
  5743. for (idx = E1000_VLVF_ARRAY_SIZE; --idx;) {
  5744. vlvf = rd32(E1000_VLVF(idx));
  5745. if ((vlvf & VLAN_VID_MASK) == vlan)
  5746. break;
  5747. }
  5748. return idx;
  5749. }
  5750. static void igb_update_pf_vlvf(struct igb_adapter *adapter, u32 vid)
  5751. {
  5752. struct e1000_hw *hw = &adapter->hw;
  5753. u32 bits, pf_id;
  5754. int idx;
  5755. idx = igb_find_vlvf_entry(hw, vid);
  5756. if (!idx)
  5757. return;
  5758. /* See if any other pools are set for this VLAN filter
  5759. * entry other than the PF.
  5760. */
  5761. pf_id = adapter->vfs_allocated_count + E1000_VLVF_POOLSEL_SHIFT;
  5762. bits = ~BIT(pf_id) & E1000_VLVF_POOLSEL_MASK;
  5763. bits &= rd32(E1000_VLVF(idx));
  5764. /* Disable the filter so this falls into the default pool. */
  5765. if (!bits) {
  5766. if (adapter->flags & IGB_FLAG_VLAN_PROMISC)
  5767. wr32(E1000_VLVF(idx), BIT(pf_id));
  5768. else
  5769. wr32(E1000_VLVF(idx), 0);
  5770. }
  5771. }
  5772. static s32 igb_set_vf_vlan(struct igb_adapter *adapter, u32 vid,
  5773. bool add, u32 vf)
  5774. {
  5775. int pf_id = adapter->vfs_allocated_count;
  5776. struct e1000_hw *hw = &adapter->hw;
  5777. int err;
  5778. /* If VLAN overlaps with one the PF is currently monitoring make
  5779. * sure that we are able to allocate a VLVF entry. This may be
  5780. * redundant but it guarantees PF will maintain visibility to
  5781. * the VLAN.
  5782. */
  5783. if (add && test_bit(vid, adapter->active_vlans)) {
  5784. err = igb_vfta_set(hw, vid, pf_id, true, false);
  5785. if (err)
  5786. return err;
  5787. }
  5788. err = igb_vfta_set(hw, vid, vf, add, false);
  5789. if (add && !err)
  5790. return err;
  5791. /* If we failed to add the VF VLAN or we are removing the VF VLAN
  5792. * we may need to drop the PF pool bit in order to allow us to free
  5793. * up the VLVF resources.
  5794. */
  5795. if (test_bit(vid, adapter->active_vlans) ||
  5796. (adapter->flags & IGB_FLAG_VLAN_PROMISC))
  5797. igb_update_pf_vlvf(adapter, vid);
  5798. return err;
  5799. }
  5800. static void igb_set_vmvir(struct igb_adapter *adapter, u32 vid, u32 vf)
  5801. {
  5802. struct e1000_hw *hw = &adapter->hw;
  5803. if (vid)
  5804. wr32(E1000_VMVIR(vf), (vid | E1000_VMVIR_VLANA_DEFAULT));
  5805. else
  5806. wr32(E1000_VMVIR(vf), 0);
  5807. }
  5808. static int igb_enable_port_vlan(struct igb_adapter *adapter, int vf,
  5809. u16 vlan, u8 qos)
  5810. {
  5811. int err;
  5812. err = igb_set_vf_vlan(adapter, vlan, true, vf);
  5813. if (err)
  5814. return err;
  5815. igb_set_vmvir(adapter, vlan | (qos << VLAN_PRIO_SHIFT), vf);
  5816. igb_set_vmolr(adapter, vf, !vlan);
  5817. /* revoke access to previous VLAN */
  5818. if (vlan != adapter->vf_data[vf].pf_vlan)
  5819. igb_set_vf_vlan(adapter, adapter->vf_data[vf].pf_vlan,
  5820. false, vf);
  5821. adapter->vf_data[vf].pf_vlan = vlan;
  5822. adapter->vf_data[vf].pf_qos = qos;
  5823. igb_set_vf_vlan_strip(adapter, vf, true);
  5824. dev_info(&adapter->pdev->dev,
  5825. "Setting VLAN %d, QOS 0x%x on VF %d\n", vlan, qos, vf);
  5826. if (test_bit(__IGB_DOWN, &adapter->state)) {
  5827. dev_warn(&adapter->pdev->dev,
  5828. "The VF VLAN has been set, but the PF device is not up.\n");
  5829. dev_warn(&adapter->pdev->dev,
  5830. "Bring the PF device up before attempting to use the VF device.\n");
  5831. }
  5832. return err;
  5833. }
  5834. static int igb_disable_port_vlan(struct igb_adapter *adapter, int vf)
  5835. {
  5836. /* Restore tagless access via VLAN 0 */
  5837. igb_set_vf_vlan(adapter, 0, true, vf);
  5838. igb_set_vmvir(adapter, 0, vf);
  5839. igb_set_vmolr(adapter, vf, true);
  5840. /* Remove any PF assigned VLAN */
  5841. if (adapter->vf_data[vf].pf_vlan)
  5842. igb_set_vf_vlan(adapter, adapter->vf_data[vf].pf_vlan,
  5843. false, vf);
  5844. adapter->vf_data[vf].pf_vlan = 0;
  5845. adapter->vf_data[vf].pf_qos = 0;
  5846. igb_set_vf_vlan_strip(adapter, vf, false);
  5847. return 0;
  5848. }
  5849. static int igb_ndo_set_vf_vlan(struct net_device *netdev, int vf,
  5850. u16 vlan, u8 qos, __be16 vlan_proto)
  5851. {
  5852. struct igb_adapter *adapter = netdev_priv(netdev);
  5853. if ((vf >= adapter->vfs_allocated_count) || (vlan > 4095) || (qos > 7))
  5854. return -EINVAL;
  5855. if (vlan_proto != htons(ETH_P_8021Q))
  5856. return -EPROTONOSUPPORT;
  5857. return (vlan || qos) ? igb_enable_port_vlan(adapter, vf, vlan, qos) :
  5858. igb_disable_port_vlan(adapter, vf);
  5859. }
  5860. static int igb_set_vf_vlan_msg(struct igb_adapter *adapter, u32 *msgbuf, u32 vf)
  5861. {
  5862. int add = (msgbuf[0] & E1000_VT_MSGINFO_MASK) >> E1000_VT_MSGINFO_SHIFT;
  5863. int vid = (msgbuf[1] & E1000_VLVF_VLANID_MASK);
  5864. int ret;
  5865. if (adapter->vf_data[vf].pf_vlan)
  5866. return -1;
  5867. /* VLAN 0 is a special case, don't allow it to be removed */
  5868. if (!vid && !add)
  5869. return 0;
  5870. ret = igb_set_vf_vlan(adapter, vid, !!add, vf);
  5871. if (!ret)
  5872. igb_set_vf_vlan_strip(adapter, vf, !!vid);
  5873. return ret;
  5874. }
  5875. static inline void igb_vf_reset(struct igb_adapter *adapter, u32 vf)
  5876. {
  5877. struct vf_data_storage *vf_data = &adapter->vf_data[vf];
  5878. /* clear flags - except flag that indicates PF has set the MAC */
  5879. vf_data->flags &= IGB_VF_FLAG_PF_SET_MAC;
  5880. vf_data->last_nack = jiffies;
  5881. /* reset vlans for device */
  5882. igb_clear_vf_vfta(adapter, vf);
  5883. igb_set_vf_vlan(adapter, vf_data->pf_vlan, true, vf);
  5884. igb_set_vmvir(adapter, vf_data->pf_vlan |
  5885. (vf_data->pf_qos << VLAN_PRIO_SHIFT), vf);
  5886. igb_set_vmolr(adapter, vf, !vf_data->pf_vlan);
  5887. igb_set_vf_vlan_strip(adapter, vf, !!(vf_data->pf_vlan));
  5888. /* reset multicast table array for vf */
  5889. adapter->vf_data[vf].num_vf_mc_hashes = 0;
  5890. /* Flush and reset the mta with the new values */
  5891. igb_set_rx_mode(adapter->netdev);
  5892. }
  5893. static void igb_vf_reset_event(struct igb_adapter *adapter, u32 vf)
  5894. {
  5895. unsigned char *vf_mac = adapter->vf_data[vf].vf_mac_addresses;
  5896. /* clear mac address as we were hotplug removed/added */
  5897. if (!(adapter->vf_data[vf].flags & IGB_VF_FLAG_PF_SET_MAC))
  5898. eth_zero_addr(vf_mac);
  5899. /* process remaining reset events */
  5900. igb_vf_reset(adapter, vf);
  5901. }
  5902. static void igb_vf_reset_msg(struct igb_adapter *adapter, u32 vf)
  5903. {
  5904. struct e1000_hw *hw = &adapter->hw;
  5905. unsigned char *vf_mac = adapter->vf_data[vf].vf_mac_addresses;
  5906. u32 reg, msgbuf[3];
  5907. u8 *addr = (u8 *)(&msgbuf[1]);
  5908. /* process all the same items cleared in a function level reset */
  5909. igb_vf_reset(adapter, vf);
  5910. /* set vf mac address */
  5911. igb_set_vf_mac(adapter, vf, vf_mac);
  5912. /* enable transmit and receive for vf */
  5913. reg = rd32(E1000_VFTE);
  5914. wr32(E1000_VFTE, reg | BIT(vf));
  5915. reg = rd32(E1000_VFRE);
  5916. wr32(E1000_VFRE, reg | BIT(vf));
  5917. adapter->vf_data[vf].flags |= IGB_VF_FLAG_CTS;
  5918. /* reply to reset with ack and vf mac address */
  5919. if (!is_zero_ether_addr(vf_mac)) {
  5920. msgbuf[0] = E1000_VF_RESET | E1000_VT_MSGTYPE_ACK;
  5921. memcpy(addr, vf_mac, ETH_ALEN);
  5922. } else {
  5923. msgbuf[0] = E1000_VF_RESET | E1000_VT_MSGTYPE_NACK;
  5924. }
  5925. igb_write_mbx(hw, msgbuf, 3, vf);
  5926. }
  5927. static void igb_flush_mac_table(struct igb_adapter *adapter)
  5928. {
  5929. struct e1000_hw *hw = &adapter->hw;
  5930. int i;
  5931. for (i = 0; i < hw->mac.rar_entry_count; i++) {
  5932. adapter->mac_table[i].state &= ~IGB_MAC_STATE_IN_USE;
  5933. memset(adapter->mac_table[i].addr, 0, ETH_ALEN);
  5934. adapter->mac_table[i].queue = 0;
  5935. igb_rar_set_index(adapter, i);
  5936. }
  5937. }
  5938. static int igb_available_rars(struct igb_adapter *adapter, u8 queue)
  5939. {
  5940. struct e1000_hw *hw = &adapter->hw;
  5941. /* do not count rar entries reserved for VFs MAC addresses */
  5942. int rar_entries = hw->mac.rar_entry_count -
  5943. adapter->vfs_allocated_count;
  5944. int i, count = 0;
  5945. for (i = 0; i < rar_entries; i++) {
  5946. /* do not count default entries */
  5947. if (adapter->mac_table[i].state & IGB_MAC_STATE_DEFAULT)
  5948. continue;
  5949. /* do not count "in use" entries for different queues */
  5950. if ((adapter->mac_table[i].state & IGB_MAC_STATE_IN_USE) &&
  5951. (adapter->mac_table[i].queue != queue))
  5952. continue;
  5953. count++;
  5954. }
  5955. return count;
  5956. }
  5957. /* Set default MAC address for the PF in the first RAR entry */
  5958. static void igb_set_default_mac_filter(struct igb_adapter *adapter)
  5959. {
  5960. struct igb_mac_addr *mac_table = &adapter->mac_table[0];
  5961. ether_addr_copy(mac_table->addr, adapter->hw.mac.addr);
  5962. mac_table->queue = adapter->vfs_allocated_count;
  5963. mac_table->state = IGB_MAC_STATE_DEFAULT | IGB_MAC_STATE_IN_USE;
  5964. igb_rar_set_index(adapter, 0);
  5965. }
  5966. /* If the filter to be added and an already existing filter express
  5967. * the same address and address type, it should be possible to only
  5968. * override the other configurations, for example the queue to steer
  5969. * traffic.
  5970. */
  5971. static bool igb_mac_entry_can_be_used(const struct igb_mac_addr *entry,
  5972. const u8 *addr, const u8 flags)
  5973. {
  5974. if (!(entry->state & IGB_MAC_STATE_IN_USE))
  5975. return true;
  5976. if ((entry->state & IGB_MAC_STATE_SRC_ADDR) !=
  5977. (flags & IGB_MAC_STATE_SRC_ADDR))
  5978. return false;
  5979. if (!ether_addr_equal(addr, entry->addr))
  5980. return false;
  5981. return true;
  5982. }
  5983. /* Add a MAC filter for 'addr' directing matching traffic to 'queue',
  5984. * 'flags' is used to indicate what kind of match is made, match is by
  5985. * default for the destination address, if matching by source address
  5986. * is desired the flag IGB_MAC_STATE_SRC_ADDR can be used.
  5987. */
  5988. static int igb_add_mac_filter_flags(struct igb_adapter *adapter,
  5989. const u8 *addr, const u8 queue,
  5990. const u8 flags)
  5991. {
  5992. struct e1000_hw *hw = &adapter->hw;
  5993. int rar_entries = hw->mac.rar_entry_count -
  5994. adapter->vfs_allocated_count;
  5995. int i;
  5996. if (is_zero_ether_addr(addr))
  5997. return -EINVAL;
  5998. /* Search for the first empty entry in the MAC table.
  5999. * Do not touch entries at the end of the table reserved for the VF MAC
  6000. * addresses.
  6001. */
  6002. for (i = 0; i < rar_entries; i++) {
  6003. if (!igb_mac_entry_can_be_used(&adapter->mac_table[i],
  6004. addr, flags))
  6005. continue;
  6006. ether_addr_copy(adapter->mac_table[i].addr, addr);
  6007. adapter->mac_table[i].queue = queue;
  6008. adapter->mac_table[i].state |= IGB_MAC_STATE_IN_USE | flags;
  6009. igb_rar_set_index(adapter, i);
  6010. return i;
  6011. }
  6012. return -ENOSPC;
  6013. }
  6014. static int igb_add_mac_filter(struct igb_adapter *adapter, const u8 *addr,
  6015. const u8 queue)
  6016. {
  6017. return igb_add_mac_filter_flags(adapter, addr, queue, 0);
  6018. }
  6019. /* Remove a MAC filter for 'addr' directing matching traffic to
  6020. * 'queue', 'flags' is used to indicate what kind of match need to be
  6021. * removed, match is by default for the destination address, if
  6022. * matching by source address is to be removed the flag
  6023. * IGB_MAC_STATE_SRC_ADDR can be used.
  6024. */
  6025. static int igb_del_mac_filter_flags(struct igb_adapter *adapter,
  6026. const u8 *addr, const u8 queue,
  6027. const u8 flags)
  6028. {
  6029. struct e1000_hw *hw = &adapter->hw;
  6030. int rar_entries = hw->mac.rar_entry_count -
  6031. adapter->vfs_allocated_count;
  6032. int i;
  6033. if (is_zero_ether_addr(addr))
  6034. return -EINVAL;
  6035. /* Search for matching entry in the MAC table based on given address
  6036. * and queue. Do not touch entries at the end of the table reserved
  6037. * for the VF MAC addresses.
  6038. */
  6039. for (i = 0; i < rar_entries; i++) {
  6040. if (!(adapter->mac_table[i].state & IGB_MAC_STATE_IN_USE))
  6041. continue;
  6042. if ((adapter->mac_table[i].state & flags) != flags)
  6043. continue;
  6044. if (adapter->mac_table[i].queue != queue)
  6045. continue;
  6046. if (!ether_addr_equal(adapter->mac_table[i].addr, addr))
  6047. continue;
  6048. /* When a filter for the default address is "deleted",
  6049. * we return it to its initial configuration
  6050. */
  6051. if (adapter->mac_table[i].state & IGB_MAC_STATE_DEFAULT) {
  6052. adapter->mac_table[i].state =
  6053. IGB_MAC_STATE_DEFAULT | IGB_MAC_STATE_IN_USE;
  6054. adapter->mac_table[i].queue =
  6055. adapter->vfs_allocated_count;
  6056. } else {
  6057. adapter->mac_table[i].state = 0;
  6058. adapter->mac_table[i].queue = 0;
  6059. memset(adapter->mac_table[i].addr, 0, ETH_ALEN);
  6060. }
  6061. igb_rar_set_index(adapter, i);
  6062. return 0;
  6063. }
  6064. return -ENOENT;
  6065. }
  6066. static int igb_del_mac_filter(struct igb_adapter *adapter, const u8 *addr,
  6067. const u8 queue)
  6068. {
  6069. return igb_del_mac_filter_flags(adapter, addr, queue, 0);
  6070. }
  6071. int igb_add_mac_steering_filter(struct igb_adapter *adapter,
  6072. const u8 *addr, u8 queue, u8 flags)
  6073. {
  6074. struct e1000_hw *hw = &adapter->hw;
  6075. /* In theory, this should be supported on 82575 as well, but
  6076. * that part wasn't easily accessible during development.
  6077. */
  6078. if (hw->mac.type != e1000_i210)
  6079. return -EOPNOTSUPP;
  6080. return igb_add_mac_filter_flags(adapter, addr, queue,
  6081. IGB_MAC_STATE_QUEUE_STEERING | flags);
  6082. }
  6083. int igb_del_mac_steering_filter(struct igb_adapter *adapter,
  6084. const u8 *addr, u8 queue, u8 flags)
  6085. {
  6086. return igb_del_mac_filter_flags(adapter, addr, queue,
  6087. IGB_MAC_STATE_QUEUE_STEERING | flags);
  6088. }
  6089. static int igb_uc_sync(struct net_device *netdev, const unsigned char *addr)
  6090. {
  6091. struct igb_adapter *adapter = netdev_priv(netdev);
  6092. int ret;
  6093. ret = igb_add_mac_filter(adapter, addr, adapter->vfs_allocated_count);
  6094. return min_t(int, ret, 0);
  6095. }
  6096. static int igb_uc_unsync(struct net_device *netdev, const unsigned char *addr)
  6097. {
  6098. struct igb_adapter *adapter = netdev_priv(netdev);
  6099. igb_del_mac_filter(adapter, addr, adapter->vfs_allocated_count);
  6100. return 0;
  6101. }
  6102. static int igb_set_vf_mac_filter(struct igb_adapter *adapter, const int vf,
  6103. const u32 info, const u8 *addr)
  6104. {
  6105. struct pci_dev *pdev = adapter->pdev;
  6106. struct vf_data_storage *vf_data = &adapter->vf_data[vf];
  6107. struct list_head *pos;
  6108. struct vf_mac_filter *entry = NULL;
  6109. int ret = 0;
  6110. switch (info) {
  6111. case E1000_VF_MAC_FILTER_CLR:
  6112. /* remove all unicast MAC filters related to the current VF */
  6113. list_for_each(pos, &adapter->vf_macs.l) {
  6114. entry = list_entry(pos, struct vf_mac_filter, l);
  6115. if (entry->vf == vf) {
  6116. entry->vf = -1;
  6117. entry->free = true;
  6118. igb_del_mac_filter(adapter, entry->vf_mac, vf);
  6119. }
  6120. }
  6121. break;
  6122. case E1000_VF_MAC_FILTER_ADD:
  6123. if ((vf_data->flags & IGB_VF_FLAG_PF_SET_MAC) &&
  6124. !vf_data->trusted) {
  6125. dev_warn(&pdev->dev,
  6126. "VF %d requested MAC filter but is administratively denied\n",
  6127. vf);
  6128. return -EINVAL;
  6129. }
  6130. if (!is_valid_ether_addr(addr)) {
  6131. dev_warn(&pdev->dev,
  6132. "VF %d attempted to set invalid MAC filter\n",
  6133. vf);
  6134. return -EINVAL;
  6135. }
  6136. /* try to find empty slot in the list */
  6137. list_for_each(pos, &adapter->vf_macs.l) {
  6138. entry = list_entry(pos, struct vf_mac_filter, l);
  6139. if (entry->free)
  6140. break;
  6141. }
  6142. if (entry && entry->free) {
  6143. entry->free = false;
  6144. entry->vf = vf;
  6145. ether_addr_copy(entry->vf_mac, addr);
  6146. ret = igb_add_mac_filter(adapter, addr, vf);
  6147. ret = min_t(int, ret, 0);
  6148. } else {
  6149. ret = -ENOSPC;
  6150. }
  6151. if (ret == -ENOSPC)
  6152. dev_warn(&pdev->dev,
  6153. "VF %d has requested MAC filter but there is no space for it\n",
  6154. vf);
  6155. break;
  6156. default:
  6157. ret = -EINVAL;
  6158. break;
  6159. }
  6160. return ret;
  6161. }
  6162. static int igb_set_vf_mac_addr(struct igb_adapter *adapter, u32 *msg, int vf)
  6163. {
  6164. struct pci_dev *pdev = adapter->pdev;
  6165. struct vf_data_storage *vf_data = &adapter->vf_data[vf];
  6166. u32 info = msg[0] & E1000_VT_MSGINFO_MASK;
  6167. /* The VF MAC Address is stored in a packed array of bytes
  6168. * starting at the second 32 bit word of the msg array
  6169. */
  6170. unsigned char *addr = (unsigned char *)&msg[1];
  6171. int ret = 0;
  6172. if (!info) {
  6173. if ((vf_data->flags & IGB_VF_FLAG_PF_SET_MAC) &&
  6174. !vf_data->trusted) {
  6175. dev_warn(&pdev->dev,
  6176. "VF %d attempted to override administratively set MAC address\nReload the VF driver to resume operations\n",
  6177. vf);
  6178. return -EINVAL;
  6179. }
  6180. if (!is_valid_ether_addr(addr)) {
  6181. dev_warn(&pdev->dev,
  6182. "VF %d attempted to set invalid MAC\n",
  6183. vf);
  6184. return -EINVAL;
  6185. }
  6186. ret = igb_set_vf_mac(adapter, vf, addr);
  6187. } else {
  6188. ret = igb_set_vf_mac_filter(adapter, vf, info, addr);
  6189. }
  6190. return ret;
  6191. }
  6192. static void igb_rcv_ack_from_vf(struct igb_adapter *adapter, u32 vf)
  6193. {
  6194. struct e1000_hw *hw = &adapter->hw;
  6195. struct vf_data_storage *vf_data = &adapter->vf_data[vf];
  6196. u32 msg = E1000_VT_MSGTYPE_NACK;
  6197. /* if device isn't clear to send it shouldn't be reading either */
  6198. if (!(vf_data->flags & IGB_VF_FLAG_CTS) &&
  6199. time_after(jiffies, vf_data->last_nack + (2 * HZ))) {
  6200. igb_write_mbx(hw, &msg, 1, vf);
  6201. vf_data->last_nack = jiffies;
  6202. }
  6203. }
  6204. static void igb_rcv_msg_from_vf(struct igb_adapter *adapter, u32 vf)
  6205. {
  6206. struct pci_dev *pdev = adapter->pdev;
  6207. u32 msgbuf[E1000_VFMAILBOX_SIZE];
  6208. struct e1000_hw *hw = &adapter->hw;
  6209. struct vf_data_storage *vf_data = &adapter->vf_data[vf];
  6210. s32 retval;
  6211. retval = igb_read_mbx(hw, msgbuf, E1000_VFMAILBOX_SIZE, vf, false);
  6212. if (retval) {
  6213. /* if receive failed revoke VF CTS stats and restart init */
  6214. dev_err(&pdev->dev, "Error receiving message from VF\n");
  6215. vf_data->flags &= ~IGB_VF_FLAG_CTS;
  6216. if (!time_after(jiffies, vf_data->last_nack + (2 * HZ)))
  6217. goto unlock;
  6218. goto out;
  6219. }
  6220. /* this is a message we already processed, do nothing */
  6221. if (msgbuf[0] & (E1000_VT_MSGTYPE_ACK | E1000_VT_MSGTYPE_NACK))
  6222. goto unlock;
  6223. /* until the vf completes a reset it should not be
  6224. * allowed to start any configuration.
  6225. */
  6226. if (msgbuf[0] == E1000_VF_RESET) {
  6227. /* unlocks mailbox */
  6228. igb_vf_reset_msg(adapter, vf);
  6229. return;
  6230. }
  6231. if (!(vf_data->flags & IGB_VF_FLAG_CTS)) {
  6232. if (!time_after(jiffies, vf_data->last_nack + (2 * HZ)))
  6233. goto unlock;
  6234. retval = -1;
  6235. goto out;
  6236. }
  6237. switch ((msgbuf[0] & 0xFFFF)) {
  6238. case E1000_VF_SET_MAC_ADDR:
  6239. retval = igb_set_vf_mac_addr(adapter, msgbuf, vf);
  6240. break;
  6241. case E1000_VF_SET_PROMISC:
  6242. retval = igb_set_vf_promisc(adapter, msgbuf, vf);
  6243. break;
  6244. case E1000_VF_SET_MULTICAST:
  6245. retval = igb_set_vf_multicasts(adapter, msgbuf, vf);
  6246. break;
  6247. case E1000_VF_SET_LPE:
  6248. retval = igb_set_vf_rlpml(adapter, msgbuf[1], vf);
  6249. break;
  6250. case E1000_VF_SET_VLAN:
  6251. retval = -1;
  6252. if (vf_data->pf_vlan)
  6253. dev_warn(&pdev->dev,
  6254. "VF %d attempted to override administratively set VLAN tag\nReload the VF driver to resume operations\n",
  6255. vf);
  6256. else
  6257. retval = igb_set_vf_vlan_msg(adapter, msgbuf, vf);
  6258. break;
  6259. default:
  6260. dev_err(&pdev->dev, "Unhandled Msg %08x\n", msgbuf[0]);
  6261. retval = -1;
  6262. break;
  6263. }
  6264. msgbuf[0] |= E1000_VT_MSGTYPE_CTS;
  6265. out:
  6266. /* notify the VF of the results of what it sent us */
  6267. if (retval)
  6268. msgbuf[0] |= E1000_VT_MSGTYPE_NACK;
  6269. else
  6270. msgbuf[0] |= E1000_VT_MSGTYPE_ACK;
  6271. /* unlocks mailbox */
  6272. igb_write_mbx(hw, msgbuf, 1, vf);
  6273. return;
  6274. unlock:
  6275. igb_unlock_mbx(hw, vf);
  6276. }
  6277. static void igb_msg_task(struct igb_adapter *adapter)
  6278. {
  6279. struct e1000_hw *hw = &adapter->hw;
  6280. u32 vf;
  6281. for (vf = 0; vf < adapter->vfs_allocated_count; vf++) {
  6282. /* process any reset requests */
  6283. if (!igb_check_for_rst(hw, vf))
  6284. igb_vf_reset_event(adapter, vf);
  6285. /* process any messages pending */
  6286. if (!igb_check_for_msg(hw, vf))
  6287. igb_rcv_msg_from_vf(adapter, vf);
  6288. /* process any acks */
  6289. if (!igb_check_for_ack(hw, vf))
  6290. igb_rcv_ack_from_vf(adapter, vf);
  6291. }
  6292. }
  6293. /**
  6294. * igb_set_uta - Set unicast filter table address
  6295. * @adapter: board private structure
  6296. * @set: boolean indicating if we are setting or clearing bits
  6297. *
  6298. * The unicast table address is a register array of 32-bit registers.
  6299. * The table is meant to be used in a way similar to how the MTA is used
  6300. * however due to certain limitations in the hardware it is necessary to
  6301. * set all the hash bits to 1 and use the VMOLR ROPE bit as a promiscuous
  6302. * enable bit to allow vlan tag stripping when promiscuous mode is enabled
  6303. **/
  6304. static void igb_set_uta(struct igb_adapter *adapter, bool set)
  6305. {
  6306. struct e1000_hw *hw = &adapter->hw;
  6307. u32 uta = set ? ~0 : 0;
  6308. int i;
  6309. /* we only need to do this if VMDq is enabled */
  6310. if (!adapter->vfs_allocated_count)
  6311. return;
  6312. for (i = hw->mac.uta_reg_count; i--;)
  6313. array_wr32(E1000_UTA, i, uta);
  6314. }
  6315. /**
  6316. * igb_intr_msi - Interrupt Handler
  6317. * @irq: interrupt number
  6318. * @data: pointer to a network interface device structure
  6319. **/
  6320. static irqreturn_t igb_intr_msi(int irq, void *data)
  6321. {
  6322. struct igb_adapter *adapter = data;
  6323. struct igb_q_vector *q_vector = adapter->q_vector[0];
  6324. struct e1000_hw *hw = &adapter->hw;
  6325. /* read ICR disables interrupts using IAM */
  6326. u32 icr = rd32(E1000_ICR);
  6327. igb_write_itr(q_vector);
  6328. if (icr & E1000_ICR_DRSTA)
  6329. schedule_work(&adapter->reset_task);
  6330. if (icr & E1000_ICR_DOUTSYNC) {
  6331. /* HW is reporting DMA is out of sync */
  6332. adapter->stats.doosync++;
  6333. }
  6334. if (icr & (E1000_ICR_RXSEQ | E1000_ICR_LSC)) {
  6335. hw->mac.get_link_status = 1;
  6336. if (!test_bit(__IGB_DOWN, &adapter->state))
  6337. mod_timer(&adapter->watchdog_timer, jiffies + 1);
  6338. }
  6339. if (icr & E1000_ICR_TS)
  6340. igb_tsync_interrupt(adapter);
  6341. napi_schedule(&q_vector->napi);
  6342. return IRQ_HANDLED;
  6343. }
  6344. /**
  6345. * igb_intr - Legacy Interrupt Handler
  6346. * @irq: interrupt number
  6347. * @data: pointer to a network interface device structure
  6348. **/
  6349. static irqreturn_t igb_intr(int irq, void *data)
  6350. {
  6351. struct igb_adapter *adapter = data;
  6352. struct igb_q_vector *q_vector = adapter->q_vector[0];
  6353. struct e1000_hw *hw = &adapter->hw;
  6354. /* Interrupt Auto-Mask...upon reading ICR, interrupts are masked. No
  6355. * need for the IMC write
  6356. */
  6357. u32 icr = rd32(E1000_ICR);
  6358. /* IMS will not auto-mask if INT_ASSERTED is not set, and if it is
  6359. * not set, then the adapter didn't send an interrupt
  6360. */
  6361. if (!(icr & E1000_ICR_INT_ASSERTED))
  6362. return IRQ_NONE;
  6363. igb_write_itr(q_vector);
  6364. if (icr & E1000_ICR_DRSTA)
  6365. schedule_work(&adapter->reset_task);
  6366. if (icr & E1000_ICR_DOUTSYNC) {
  6367. /* HW is reporting DMA is out of sync */
  6368. adapter->stats.doosync++;
  6369. }
  6370. if (icr & (E1000_ICR_RXSEQ | E1000_ICR_LSC)) {
  6371. hw->mac.get_link_status = 1;
  6372. /* guard against interrupt when we're going down */
  6373. if (!test_bit(__IGB_DOWN, &adapter->state))
  6374. mod_timer(&adapter->watchdog_timer, jiffies + 1);
  6375. }
  6376. if (icr & E1000_ICR_TS)
  6377. igb_tsync_interrupt(adapter);
  6378. napi_schedule(&q_vector->napi);
  6379. return IRQ_HANDLED;
  6380. }
  6381. static void igb_ring_irq_enable(struct igb_q_vector *q_vector)
  6382. {
  6383. struct igb_adapter *adapter = q_vector->adapter;
  6384. struct e1000_hw *hw = &adapter->hw;
  6385. if ((q_vector->rx.ring && (adapter->rx_itr_setting & 3)) ||
  6386. (!q_vector->rx.ring && (adapter->tx_itr_setting & 3))) {
  6387. if ((adapter->num_q_vectors == 1) && !adapter->vf_data)
  6388. igb_set_itr(q_vector);
  6389. else
  6390. igb_update_ring_itr(q_vector);
  6391. }
  6392. if (!test_bit(__IGB_DOWN, &adapter->state)) {
  6393. if (adapter->flags & IGB_FLAG_HAS_MSIX)
  6394. wr32(E1000_EIMS, q_vector->eims_value);
  6395. else
  6396. igb_irq_enable(adapter);
  6397. }
  6398. }
  6399. /**
  6400. * igb_poll - NAPI Rx polling callback
  6401. * @napi: napi polling structure
  6402. * @budget: count of how many packets we should handle
  6403. **/
  6404. static int igb_poll(struct napi_struct *napi, int budget)
  6405. {
  6406. struct igb_q_vector *q_vector = container_of(napi,
  6407. struct igb_q_vector,
  6408. napi);
  6409. bool clean_complete = true;
  6410. int work_done = 0;
  6411. #ifdef CONFIG_IGB_DCA
  6412. if (q_vector->adapter->flags & IGB_FLAG_DCA_ENABLED)
  6413. igb_update_dca(q_vector);
  6414. #endif
  6415. if (q_vector->tx.ring)
  6416. clean_complete = igb_clean_tx_irq(q_vector, budget);
  6417. if (q_vector->rx.ring) {
  6418. int cleaned = igb_clean_rx_irq(q_vector, budget);
  6419. work_done += cleaned;
  6420. if (cleaned >= budget)
  6421. clean_complete = false;
  6422. }
  6423. /* If all work not completed, return budget and keep polling */
  6424. if (!clean_complete)
  6425. return budget;
  6426. /* If not enough Rx work done, exit the polling mode */
  6427. napi_complete_done(napi, work_done);
  6428. igb_ring_irq_enable(q_vector);
  6429. return 0;
  6430. }
  6431. /**
  6432. * igb_clean_tx_irq - Reclaim resources after transmit completes
  6433. * @q_vector: pointer to q_vector containing needed info
  6434. * @napi_budget: Used to determine if we are in netpoll
  6435. *
  6436. * returns true if ring is completely cleaned
  6437. **/
  6438. static bool igb_clean_tx_irq(struct igb_q_vector *q_vector, int napi_budget)
  6439. {
  6440. struct igb_adapter *adapter = q_vector->adapter;
  6441. struct igb_ring *tx_ring = q_vector->tx.ring;
  6442. struct igb_tx_buffer *tx_buffer;
  6443. union e1000_adv_tx_desc *tx_desc;
  6444. unsigned int total_bytes = 0, total_packets = 0;
  6445. unsigned int budget = q_vector->tx.work_limit;
  6446. unsigned int i = tx_ring->next_to_clean;
  6447. if (test_bit(__IGB_DOWN, &adapter->state))
  6448. return true;
  6449. tx_buffer = &tx_ring->tx_buffer_info[i];
  6450. tx_desc = IGB_TX_DESC(tx_ring, i);
  6451. i -= tx_ring->count;
  6452. do {
  6453. union e1000_adv_tx_desc *eop_desc = tx_buffer->next_to_watch;
  6454. /* if next_to_watch is not set then there is no work pending */
  6455. if (!eop_desc)
  6456. break;
  6457. /* prevent any other reads prior to eop_desc */
  6458. smp_rmb();
  6459. /* if DD is not set pending work has not been completed */
  6460. if (!(eop_desc->wb.status & cpu_to_le32(E1000_TXD_STAT_DD)))
  6461. break;
  6462. /* clear next_to_watch to prevent false hangs */
  6463. tx_buffer->next_to_watch = NULL;
  6464. /* update the statistics for this packet */
  6465. total_bytes += tx_buffer->bytecount;
  6466. total_packets += tx_buffer->gso_segs;
  6467. /* free the skb */
  6468. napi_consume_skb(tx_buffer->skb, napi_budget);
  6469. /* unmap skb header data */
  6470. dma_unmap_single(tx_ring->dev,
  6471. dma_unmap_addr(tx_buffer, dma),
  6472. dma_unmap_len(tx_buffer, len),
  6473. DMA_TO_DEVICE);
  6474. /* clear tx_buffer data */
  6475. dma_unmap_len_set(tx_buffer, len, 0);
  6476. /* clear last DMA location and unmap remaining buffers */
  6477. while (tx_desc != eop_desc) {
  6478. tx_buffer++;
  6479. tx_desc++;
  6480. i++;
  6481. if (unlikely(!i)) {
  6482. i -= tx_ring->count;
  6483. tx_buffer = tx_ring->tx_buffer_info;
  6484. tx_desc = IGB_TX_DESC(tx_ring, 0);
  6485. }
  6486. /* unmap any remaining paged data */
  6487. if (dma_unmap_len(tx_buffer, len)) {
  6488. dma_unmap_page(tx_ring->dev,
  6489. dma_unmap_addr(tx_buffer, dma),
  6490. dma_unmap_len(tx_buffer, len),
  6491. DMA_TO_DEVICE);
  6492. dma_unmap_len_set(tx_buffer, len, 0);
  6493. }
  6494. }
  6495. /* move us one more past the eop_desc for start of next pkt */
  6496. tx_buffer++;
  6497. tx_desc++;
  6498. i++;
  6499. if (unlikely(!i)) {
  6500. i -= tx_ring->count;
  6501. tx_buffer = tx_ring->tx_buffer_info;
  6502. tx_desc = IGB_TX_DESC(tx_ring, 0);
  6503. }
  6504. /* issue prefetch for next Tx descriptor */
  6505. prefetch(tx_desc);
  6506. /* update budget accounting */
  6507. budget--;
  6508. } while (likely(budget));
  6509. netdev_tx_completed_queue(txring_txq(tx_ring),
  6510. total_packets, total_bytes);
  6511. i += tx_ring->count;
  6512. tx_ring->next_to_clean = i;
  6513. u64_stats_update_begin(&tx_ring->tx_syncp);
  6514. tx_ring->tx_stats.bytes += total_bytes;
  6515. tx_ring->tx_stats.packets += total_packets;
  6516. u64_stats_update_end(&tx_ring->tx_syncp);
  6517. q_vector->tx.total_bytes += total_bytes;
  6518. q_vector->tx.total_packets += total_packets;
  6519. if (test_bit(IGB_RING_FLAG_TX_DETECT_HANG, &tx_ring->flags)) {
  6520. struct e1000_hw *hw = &adapter->hw;
  6521. /* Detect a transmit hang in hardware, this serializes the
  6522. * check with the clearing of time_stamp and movement of i
  6523. */
  6524. clear_bit(IGB_RING_FLAG_TX_DETECT_HANG, &tx_ring->flags);
  6525. if (tx_buffer->next_to_watch &&
  6526. time_after(jiffies, tx_buffer->time_stamp +
  6527. (adapter->tx_timeout_factor * HZ)) &&
  6528. !(rd32(E1000_STATUS) & E1000_STATUS_TXOFF)) {
  6529. /* detected Tx unit hang */
  6530. dev_err(tx_ring->dev,
  6531. "Detected Tx Unit Hang\n"
  6532. " Tx Queue <%d>\n"
  6533. " TDH <%x>\n"
  6534. " TDT <%x>\n"
  6535. " next_to_use <%x>\n"
  6536. " next_to_clean <%x>\n"
  6537. "buffer_info[next_to_clean]\n"
  6538. " time_stamp <%lx>\n"
  6539. " next_to_watch <%p>\n"
  6540. " jiffies <%lx>\n"
  6541. " desc.status <%x>\n",
  6542. tx_ring->queue_index,
  6543. rd32(E1000_TDH(tx_ring->reg_idx)),
  6544. readl(tx_ring->tail),
  6545. tx_ring->next_to_use,
  6546. tx_ring->next_to_clean,
  6547. tx_buffer->time_stamp,
  6548. tx_buffer->next_to_watch,
  6549. jiffies,
  6550. tx_buffer->next_to_watch->wb.status);
  6551. netif_stop_subqueue(tx_ring->netdev,
  6552. tx_ring->queue_index);
  6553. /* we are about to reset, no point in enabling stuff */
  6554. return true;
  6555. }
  6556. }
  6557. #define TX_WAKE_THRESHOLD (DESC_NEEDED * 2)
  6558. if (unlikely(total_packets &&
  6559. netif_carrier_ok(tx_ring->netdev) &&
  6560. igb_desc_unused(tx_ring) >= TX_WAKE_THRESHOLD)) {
  6561. /* Make sure that anybody stopping the queue after this
  6562. * sees the new next_to_clean.
  6563. */
  6564. smp_mb();
  6565. if (__netif_subqueue_stopped(tx_ring->netdev,
  6566. tx_ring->queue_index) &&
  6567. !(test_bit(__IGB_DOWN, &adapter->state))) {
  6568. netif_wake_subqueue(tx_ring->netdev,
  6569. tx_ring->queue_index);
  6570. u64_stats_update_begin(&tx_ring->tx_syncp);
  6571. tx_ring->tx_stats.restart_queue++;
  6572. u64_stats_update_end(&tx_ring->tx_syncp);
  6573. }
  6574. }
  6575. return !!budget;
  6576. }
  6577. /**
  6578. * igb_reuse_rx_page - page flip buffer and store it back on the ring
  6579. * @rx_ring: rx descriptor ring to store buffers on
  6580. * @old_buff: donor buffer to have page reused
  6581. *
  6582. * Synchronizes page for reuse by the adapter
  6583. **/
  6584. static void igb_reuse_rx_page(struct igb_ring *rx_ring,
  6585. struct igb_rx_buffer *old_buff)
  6586. {
  6587. struct igb_rx_buffer *new_buff;
  6588. u16 nta = rx_ring->next_to_alloc;
  6589. new_buff = &rx_ring->rx_buffer_info[nta];
  6590. /* update, and store next to alloc */
  6591. nta++;
  6592. rx_ring->next_to_alloc = (nta < rx_ring->count) ? nta : 0;
  6593. /* Transfer page from old buffer to new buffer.
  6594. * Move each member individually to avoid possible store
  6595. * forwarding stalls.
  6596. */
  6597. new_buff->dma = old_buff->dma;
  6598. new_buff->page = old_buff->page;
  6599. new_buff->page_offset = old_buff->page_offset;
  6600. new_buff->pagecnt_bias = old_buff->pagecnt_bias;
  6601. }
  6602. static inline bool igb_page_is_reserved(struct page *page)
  6603. {
  6604. return (page_to_nid(page) != numa_mem_id()) || page_is_pfmemalloc(page);
  6605. }
  6606. static bool igb_can_reuse_rx_page(struct igb_rx_buffer *rx_buffer)
  6607. {
  6608. unsigned int pagecnt_bias = rx_buffer->pagecnt_bias;
  6609. struct page *page = rx_buffer->page;
  6610. /* avoid re-using remote pages */
  6611. if (unlikely(igb_page_is_reserved(page)))
  6612. return false;
  6613. #if (PAGE_SIZE < 8192)
  6614. /* if we are only owner of page we can reuse it */
  6615. if (unlikely((page_ref_count(page) - pagecnt_bias) > 1))
  6616. return false;
  6617. #else
  6618. #define IGB_LAST_OFFSET \
  6619. (SKB_WITH_OVERHEAD(PAGE_SIZE) - IGB_RXBUFFER_2048)
  6620. if (rx_buffer->page_offset > IGB_LAST_OFFSET)
  6621. return false;
  6622. #endif
  6623. /* If we have drained the page fragment pool we need to update
  6624. * the pagecnt_bias and page count so that we fully restock the
  6625. * number of references the driver holds.
  6626. */
  6627. if (unlikely(!pagecnt_bias)) {
  6628. page_ref_add(page, USHRT_MAX);
  6629. rx_buffer->pagecnt_bias = USHRT_MAX;
  6630. }
  6631. return true;
  6632. }
  6633. /**
  6634. * igb_add_rx_frag - Add contents of Rx buffer to sk_buff
  6635. * @rx_ring: rx descriptor ring to transact packets on
  6636. * @rx_buffer: buffer containing page to add
  6637. * @skb: sk_buff to place the data into
  6638. * @size: size of buffer to be added
  6639. *
  6640. * This function will add the data contained in rx_buffer->page to the skb.
  6641. **/
  6642. static void igb_add_rx_frag(struct igb_ring *rx_ring,
  6643. struct igb_rx_buffer *rx_buffer,
  6644. struct sk_buff *skb,
  6645. unsigned int size)
  6646. {
  6647. #if (PAGE_SIZE < 8192)
  6648. unsigned int truesize = igb_rx_pg_size(rx_ring) / 2;
  6649. #else
  6650. unsigned int truesize = ring_uses_build_skb(rx_ring) ?
  6651. SKB_DATA_ALIGN(IGB_SKB_PAD + size) :
  6652. SKB_DATA_ALIGN(size);
  6653. #endif
  6654. skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, rx_buffer->page,
  6655. rx_buffer->page_offset, size, truesize);
  6656. #if (PAGE_SIZE < 8192)
  6657. rx_buffer->page_offset ^= truesize;
  6658. #else
  6659. rx_buffer->page_offset += truesize;
  6660. #endif
  6661. }
  6662. static struct sk_buff *igb_construct_skb(struct igb_ring *rx_ring,
  6663. struct igb_rx_buffer *rx_buffer,
  6664. union e1000_adv_rx_desc *rx_desc,
  6665. unsigned int size)
  6666. {
  6667. void *va = page_address(rx_buffer->page) + rx_buffer->page_offset;
  6668. #if (PAGE_SIZE < 8192)
  6669. unsigned int truesize = igb_rx_pg_size(rx_ring) / 2;
  6670. #else
  6671. unsigned int truesize = SKB_DATA_ALIGN(size);
  6672. #endif
  6673. unsigned int headlen;
  6674. struct sk_buff *skb;
  6675. /* prefetch first cache line of first page */
  6676. prefetch(va);
  6677. #if L1_CACHE_BYTES < 128
  6678. prefetch(va + L1_CACHE_BYTES);
  6679. #endif
  6680. /* allocate a skb to store the frags */
  6681. skb = napi_alloc_skb(&rx_ring->q_vector->napi, IGB_RX_HDR_LEN);
  6682. if (unlikely(!skb))
  6683. return NULL;
  6684. if (unlikely(igb_test_staterr(rx_desc, E1000_RXDADV_STAT_TSIP))) {
  6685. igb_ptp_rx_pktstamp(rx_ring->q_vector, va, skb);
  6686. va += IGB_TS_HDR_LEN;
  6687. size -= IGB_TS_HDR_LEN;
  6688. }
  6689. /* Determine available headroom for copy */
  6690. headlen = size;
  6691. if (headlen > IGB_RX_HDR_LEN)
  6692. headlen = eth_get_headlen(va, IGB_RX_HDR_LEN);
  6693. /* align pull length to size of long to optimize memcpy performance */
  6694. memcpy(__skb_put(skb, headlen), va, ALIGN(headlen, sizeof(long)));
  6695. /* update all of the pointers */
  6696. size -= headlen;
  6697. if (size) {
  6698. skb_add_rx_frag(skb, 0, rx_buffer->page,
  6699. (va + headlen) - page_address(rx_buffer->page),
  6700. size, truesize);
  6701. #if (PAGE_SIZE < 8192)
  6702. rx_buffer->page_offset ^= truesize;
  6703. #else
  6704. rx_buffer->page_offset += truesize;
  6705. #endif
  6706. } else {
  6707. rx_buffer->pagecnt_bias++;
  6708. }
  6709. return skb;
  6710. }
  6711. static struct sk_buff *igb_build_skb(struct igb_ring *rx_ring,
  6712. struct igb_rx_buffer *rx_buffer,
  6713. union e1000_adv_rx_desc *rx_desc,
  6714. unsigned int size)
  6715. {
  6716. void *va = page_address(rx_buffer->page) + rx_buffer->page_offset;
  6717. #if (PAGE_SIZE < 8192)
  6718. unsigned int truesize = igb_rx_pg_size(rx_ring) / 2;
  6719. #else
  6720. unsigned int truesize = SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) +
  6721. SKB_DATA_ALIGN(IGB_SKB_PAD + size);
  6722. #endif
  6723. struct sk_buff *skb;
  6724. /* prefetch first cache line of first page */
  6725. prefetch(va);
  6726. #if L1_CACHE_BYTES < 128
  6727. prefetch(va + L1_CACHE_BYTES);
  6728. #endif
  6729. /* build an skb around the page buffer */
  6730. skb = build_skb(va - IGB_SKB_PAD, truesize);
  6731. if (unlikely(!skb))
  6732. return NULL;
  6733. /* update pointers within the skb to store the data */
  6734. skb_reserve(skb, IGB_SKB_PAD);
  6735. __skb_put(skb, size);
  6736. /* pull timestamp out of packet data */
  6737. if (igb_test_staterr(rx_desc, E1000_RXDADV_STAT_TSIP)) {
  6738. igb_ptp_rx_pktstamp(rx_ring->q_vector, skb->data, skb);
  6739. __skb_pull(skb, IGB_TS_HDR_LEN);
  6740. }
  6741. /* update buffer offset */
  6742. #if (PAGE_SIZE < 8192)
  6743. rx_buffer->page_offset ^= truesize;
  6744. #else
  6745. rx_buffer->page_offset += truesize;
  6746. #endif
  6747. return skb;
  6748. }
  6749. static inline void igb_rx_checksum(struct igb_ring *ring,
  6750. union e1000_adv_rx_desc *rx_desc,
  6751. struct sk_buff *skb)
  6752. {
  6753. skb_checksum_none_assert(skb);
  6754. /* Ignore Checksum bit is set */
  6755. if (igb_test_staterr(rx_desc, E1000_RXD_STAT_IXSM))
  6756. return;
  6757. /* Rx checksum disabled via ethtool */
  6758. if (!(ring->netdev->features & NETIF_F_RXCSUM))
  6759. return;
  6760. /* TCP/UDP checksum error bit is set */
  6761. if (igb_test_staterr(rx_desc,
  6762. E1000_RXDEXT_STATERR_TCPE |
  6763. E1000_RXDEXT_STATERR_IPE)) {
  6764. /* work around errata with sctp packets where the TCPE aka
  6765. * L4E bit is set incorrectly on 64 byte (60 byte w/o crc)
  6766. * packets, (aka let the stack check the crc32c)
  6767. */
  6768. if (!((skb->len == 60) &&
  6769. test_bit(IGB_RING_FLAG_RX_SCTP_CSUM, &ring->flags))) {
  6770. u64_stats_update_begin(&ring->rx_syncp);
  6771. ring->rx_stats.csum_err++;
  6772. u64_stats_update_end(&ring->rx_syncp);
  6773. }
  6774. /* let the stack verify checksum errors */
  6775. return;
  6776. }
  6777. /* It must be a TCP or UDP packet with a valid checksum */
  6778. if (igb_test_staterr(rx_desc, E1000_RXD_STAT_TCPCS |
  6779. E1000_RXD_STAT_UDPCS))
  6780. skb->ip_summed = CHECKSUM_UNNECESSARY;
  6781. dev_dbg(ring->dev, "cksum success: bits %08X\n",
  6782. le32_to_cpu(rx_desc->wb.upper.status_error));
  6783. }
  6784. static inline void igb_rx_hash(struct igb_ring *ring,
  6785. union e1000_adv_rx_desc *rx_desc,
  6786. struct sk_buff *skb)
  6787. {
  6788. if (ring->netdev->features & NETIF_F_RXHASH)
  6789. skb_set_hash(skb,
  6790. le32_to_cpu(rx_desc->wb.lower.hi_dword.rss),
  6791. PKT_HASH_TYPE_L3);
  6792. }
  6793. /**
  6794. * igb_is_non_eop - process handling of non-EOP buffers
  6795. * @rx_ring: Rx ring being processed
  6796. * @rx_desc: Rx descriptor for current buffer
  6797. * @skb: current socket buffer containing buffer in progress
  6798. *
  6799. * This function updates next to clean. If the buffer is an EOP buffer
  6800. * this function exits returning false, otherwise it will place the
  6801. * sk_buff in the next buffer to be chained and return true indicating
  6802. * that this is in fact a non-EOP buffer.
  6803. **/
  6804. static bool igb_is_non_eop(struct igb_ring *rx_ring,
  6805. union e1000_adv_rx_desc *rx_desc)
  6806. {
  6807. u32 ntc = rx_ring->next_to_clean + 1;
  6808. /* fetch, update, and store next to clean */
  6809. ntc = (ntc < rx_ring->count) ? ntc : 0;
  6810. rx_ring->next_to_clean = ntc;
  6811. prefetch(IGB_RX_DESC(rx_ring, ntc));
  6812. if (likely(igb_test_staterr(rx_desc, E1000_RXD_STAT_EOP)))
  6813. return false;
  6814. return true;
  6815. }
  6816. /**
  6817. * igb_cleanup_headers - Correct corrupted or empty headers
  6818. * @rx_ring: rx descriptor ring packet is being transacted on
  6819. * @rx_desc: pointer to the EOP Rx descriptor
  6820. * @skb: pointer to current skb being fixed
  6821. *
  6822. * Address the case where we are pulling data in on pages only
  6823. * and as such no data is present in the skb header.
  6824. *
  6825. * In addition if skb is not at least 60 bytes we need to pad it so that
  6826. * it is large enough to qualify as a valid Ethernet frame.
  6827. *
  6828. * Returns true if an error was encountered and skb was freed.
  6829. **/
  6830. static bool igb_cleanup_headers(struct igb_ring *rx_ring,
  6831. union e1000_adv_rx_desc *rx_desc,
  6832. struct sk_buff *skb)
  6833. {
  6834. if (unlikely((igb_test_staterr(rx_desc,
  6835. E1000_RXDEXT_ERR_FRAME_ERR_MASK)))) {
  6836. struct net_device *netdev = rx_ring->netdev;
  6837. if (!(netdev->features & NETIF_F_RXALL)) {
  6838. dev_kfree_skb_any(skb);
  6839. return true;
  6840. }
  6841. }
  6842. /* if eth_skb_pad returns an error the skb was freed */
  6843. if (eth_skb_pad(skb))
  6844. return true;
  6845. return false;
  6846. }
  6847. /**
  6848. * igb_process_skb_fields - Populate skb header fields from Rx descriptor
  6849. * @rx_ring: rx descriptor ring packet is being transacted on
  6850. * @rx_desc: pointer to the EOP Rx descriptor
  6851. * @skb: pointer to current skb being populated
  6852. *
  6853. * This function checks the ring, descriptor, and packet information in
  6854. * order to populate the hash, checksum, VLAN, timestamp, protocol, and
  6855. * other fields within the skb.
  6856. **/
  6857. static void igb_process_skb_fields(struct igb_ring *rx_ring,
  6858. union e1000_adv_rx_desc *rx_desc,
  6859. struct sk_buff *skb)
  6860. {
  6861. struct net_device *dev = rx_ring->netdev;
  6862. igb_rx_hash(rx_ring, rx_desc, skb);
  6863. igb_rx_checksum(rx_ring, rx_desc, skb);
  6864. if (igb_test_staterr(rx_desc, E1000_RXDADV_STAT_TS) &&
  6865. !igb_test_staterr(rx_desc, E1000_RXDADV_STAT_TSIP))
  6866. igb_ptp_rx_rgtstamp(rx_ring->q_vector, skb);
  6867. if ((dev->features & NETIF_F_HW_VLAN_CTAG_RX) &&
  6868. igb_test_staterr(rx_desc, E1000_RXD_STAT_VP)) {
  6869. u16 vid;
  6870. if (igb_test_staterr(rx_desc, E1000_RXDEXT_STATERR_LB) &&
  6871. test_bit(IGB_RING_FLAG_RX_LB_VLAN_BSWAP, &rx_ring->flags))
  6872. vid = be16_to_cpu(rx_desc->wb.upper.vlan);
  6873. else
  6874. vid = le16_to_cpu(rx_desc->wb.upper.vlan);
  6875. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vid);
  6876. }
  6877. skb_record_rx_queue(skb, rx_ring->queue_index);
  6878. skb->protocol = eth_type_trans(skb, rx_ring->netdev);
  6879. }
  6880. static struct igb_rx_buffer *igb_get_rx_buffer(struct igb_ring *rx_ring,
  6881. const unsigned int size)
  6882. {
  6883. struct igb_rx_buffer *rx_buffer;
  6884. rx_buffer = &rx_ring->rx_buffer_info[rx_ring->next_to_clean];
  6885. prefetchw(rx_buffer->page);
  6886. /* we are reusing so sync this buffer for CPU use */
  6887. dma_sync_single_range_for_cpu(rx_ring->dev,
  6888. rx_buffer->dma,
  6889. rx_buffer->page_offset,
  6890. size,
  6891. DMA_FROM_DEVICE);
  6892. rx_buffer->pagecnt_bias--;
  6893. return rx_buffer;
  6894. }
  6895. static void igb_put_rx_buffer(struct igb_ring *rx_ring,
  6896. struct igb_rx_buffer *rx_buffer)
  6897. {
  6898. if (igb_can_reuse_rx_page(rx_buffer)) {
  6899. /* hand second half of page back to the ring */
  6900. igb_reuse_rx_page(rx_ring, rx_buffer);
  6901. } else {
  6902. /* We are not reusing the buffer so unmap it and free
  6903. * any references we are holding to it
  6904. */
  6905. dma_unmap_page_attrs(rx_ring->dev, rx_buffer->dma,
  6906. igb_rx_pg_size(rx_ring), DMA_FROM_DEVICE,
  6907. IGB_RX_DMA_ATTR);
  6908. __page_frag_cache_drain(rx_buffer->page,
  6909. rx_buffer->pagecnt_bias);
  6910. }
  6911. /* clear contents of rx_buffer */
  6912. rx_buffer->page = NULL;
  6913. }
  6914. static int igb_clean_rx_irq(struct igb_q_vector *q_vector, const int budget)
  6915. {
  6916. struct igb_ring *rx_ring = q_vector->rx.ring;
  6917. struct sk_buff *skb = rx_ring->skb;
  6918. unsigned int total_bytes = 0, total_packets = 0;
  6919. u16 cleaned_count = igb_desc_unused(rx_ring);
  6920. while (likely(total_packets < budget)) {
  6921. union e1000_adv_rx_desc *rx_desc;
  6922. struct igb_rx_buffer *rx_buffer;
  6923. unsigned int size;
  6924. /* return some buffers to hardware, one at a time is too slow */
  6925. if (cleaned_count >= IGB_RX_BUFFER_WRITE) {
  6926. igb_alloc_rx_buffers(rx_ring, cleaned_count);
  6927. cleaned_count = 0;
  6928. }
  6929. rx_desc = IGB_RX_DESC(rx_ring, rx_ring->next_to_clean);
  6930. size = le16_to_cpu(rx_desc->wb.upper.length);
  6931. if (!size)
  6932. break;
  6933. /* This memory barrier is needed to keep us from reading
  6934. * any other fields out of the rx_desc until we know the
  6935. * descriptor has been written back
  6936. */
  6937. dma_rmb();
  6938. rx_buffer = igb_get_rx_buffer(rx_ring, size);
  6939. /* retrieve a buffer from the ring */
  6940. if (skb)
  6941. igb_add_rx_frag(rx_ring, rx_buffer, skb, size);
  6942. else if (ring_uses_build_skb(rx_ring))
  6943. skb = igb_build_skb(rx_ring, rx_buffer, rx_desc, size);
  6944. else
  6945. skb = igb_construct_skb(rx_ring, rx_buffer,
  6946. rx_desc, size);
  6947. /* exit if we failed to retrieve a buffer */
  6948. if (!skb) {
  6949. rx_ring->rx_stats.alloc_failed++;
  6950. rx_buffer->pagecnt_bias++;
  6951. break;
  6952. }
  6953. igb_put_rx_buffer(rx_ring, rx_buffer);
  6954. cleaned_count++;
  6955. /* fetch next buffer in frame if non-eop */
  6956. if (igb_is_non_eop(rx_ring, rx_desc))
  6957. continue;
  6958. /* verify the packet layout is correct */
  6959. if (igb_cleanup_headers(rx_ring, rx_desc, skb)) {
  6960. skb = NULL;
  6961. continue;
  6962. }
  6963. /* probably a little skewed due to removing CRC */
  6964. total_bytes += skb->len;
  6965. /* populate checksum, timestamp, VLAN, and protocol */
  6966. igb_process_skb_fields(rx_ring, rx_desc, skb);
  6967. napi_gro_receive(&q_vector->napi, skb);
  6968. /* reset skb pointer */
  6969. skb = NULL;
  6970. /* update budget accounting */
  6971. total_packets++;
  6972. }
  6973. /* place incomplete frames back on ring for completion */
  6974. rx_ring->skb = skb;
  6975. u64_stats_update_begin(&rx_ring->rx_syncp);
  6976. rx_ring->rx_stats.packets += total_packets;
  6977. rx_ring->rx_stats.bytes += total_bytes;
  6978. u64_stats_update_end(&rx_ring->rx_syncp);
  6979. q_vector->rx.total_packets += total_packets;
  6980. q_vector->rx.total_bytes += total_bytes;
  6981. if (cleaned_count)
  6982. igb_alloc_rx_buffers(rx_ring, cleaned_count);
  6983. return total_packets;
  6984. }
  6985. static inline unsigned int igb_rx_offset(struct igb_ring *rx_ring)
  6986. {
  6987. return ring_uses_build_skb(rx_ring) ? IGB_SKB_PAD : 0;
  6988. }
  6989. static bool igb_alloc_mapped_page(struct igb_ring *rx_ring,
  6990. struct igb_rx_buffer *bi)
  6991. {
  6992. struct page *page = bi->page;
  6993. dma_addr_t dma;
  6994. /* since we are recycling buffers we should seldom need to alloc */
  6995. if (likely(page))
  6996. return true;
  6997. /* alloc new page for storage */
  6998. page = dev_alloc_pages(igb_rx_pg_order(rx_ring));
  6999. if (unlikely(!page)) {
  7000. rx_ring->rx_stats.alloc_failed++;
  7001. return false;
  7002. }
  7003. /* map page for use */
  7004. dma = dma_map_page_attrs(rx_ring->dev, page, 0,
  7005. igb_rx_pg_size(rx_ring),
  7006. DMA_FROM_DEVICE,
  7007. IGB_RX_DMA_ATTR);
  7008. /* if mapping failed free memory back to system since
  7009. * there isn't much point in holding memory we can't use
  7010. */
  7011. if (dma_mapping_error(rx_ring->dev, dma)) {
  7012. __free_pages(page, igb_rx_pg_order(rx_ring));
  7013. rx_ring->rx_stats.alloc_failed++;
  7014. return false;
  7015. }
  7016. bi->dma = dma;
  7017. bi->page = page;
  7018. bi->page_offset = igb_rx_offset(rx_ring);
  7019. bi->pagecnt_bias = 1;
  7020. return true;
  7021. }
  7022. /**
  7023. * igb_alloc_rx_buffers - Replace used receive buffers; packet split
  7024. * @adapter: address of board private structure
  7025. **/
  7026. void igb_alloc_rx_buffers(struct igb_ring *rx_ring, u16 cleaned_count)
  7027. {
  7028. union e1000_adv_rx_desc *rx_desc;
  7029. struct igb_rx_buffer *bi;
  7030. u16 i = rx_ring->next_to_use;
  7031. u16 bufsz;
  7032. /* nothing to do */
  7033. if (!cleaned_count)
  7034. return;
  7035. rx_desc = IGB_RX_DESC(rx_ring, i);
  7036. bi = &rx_ring->rx_buffer_info[i];
  7037. i -= rx_ring->count;
  7038. bufsz = igb_rx_bufsz(rx_ring);
  7039. do {
  7040. if (!igb_alloc_mapped_page(rx_ring, bi))
  7041. break;
  7042. /* sync the buffer for use by the device */
  7043. dma_sync_single_range_for_device(rx_ring->dev, bi->dma,
  7044. bi->page_offset, bufsz,
  7045. DMA_FROM_DEVICE);
  7046. /* Refresh the desc even if buffer_addrs didn't change
  7047. * because each write-back erases this info.
  7048. */
  7049. rx_desc->read.pkt_addr = cpu_to_le64(bi->dma + bi->page_offset);
  7050. rx_desc++;
  7051. bi++;
  7052. i++;
  7053. if (unlikely(!i)) {
  7054. rx_desc = IGB_RX_DESC(rx_ring, 0);
  7055. bi = rx_ring->rx_buffer_info;
  7056. i -= rx_ring->count;
  7057. }
  7058. /* clear the length for the next_to_use descriptor */
  7059. rx_desc->wb.upper.length = 0;
  7060. cleaned_count--;
  7061. } while (cleaned_count);
  7062. i += rx_ring->count;
  7063. if (rx_ring->next_to_use != i) {
  7064. /* record the next descriptor to use */
  7065. rx_ring->next_to_use = i;
  7066. /* update next to alloc since we have filled the ring */
  7067. rx_ring->next_to_alloc = i;
  7068. /* Force memory writes to complete before letting h/w
  7069. * know there are new descriptors to fetch. (Only
  7070. * applicable for weak-ordered memory model archs,
  7071. * such as IA-64).
  7072. */
  7073. wmb();
  7074. writel(i, rx_ring->tail);
  7075. }
  7076. }
  7077. /**
  7078. * igb_mii_ioctl -
  7079. * @netdev:
  7080. * @ifreq:
  7081. * @cmd:
  7082. **/
  7083. static int igb_mii_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
  7084. {
  7085. struct igb_adapter *adapter = netdev_priv(netdev);
  7086. struct mii_ioctl_data *data = if_mii(ifr);
  7087. if (adapter->hw.phy.media_type != e1000_media_type_copper)
  7088. return -EOPNOTSUPP;
  7089. switch (cmd) {
  7090. case SIOCGMIIPHY:
  7091. data->phy_id = adapter->hw.phy.addr;
  7092. break;
  7093. case SIOCGMIIREG:
  7094. if (igb_read_phy_reg(&adapter->hw, data->reg_num & 0x1F,
  7095. &data->val_out))
  7096. return -EIO;
  7097. break;
  7098. case SIOCSMIIREG:
  7099. default:
  7100. return -EOPNOTSUPP;
  7101. }
  7102. return 0;
  7103. }
  7104. /**
  7105. * igb_ioctl -
  7106. * @netdev:
  7107. * @ifreq:
  7108. * @cmd:
  7109. **/
  7110. static int igb_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
  7111. {
  7112. switch (cmd) {
  7113. case SIOCGMIIPHY:
  7114. case SIOCGMIIREG:
  7115. case SIOCSMIIREG:
  7116. return igb_mii_ioctl(netdev, ifr, cmd);
  7117. case SIOCGHWTSTAMP:
  7118. return igb_ptp_get_ts_config(netdev, ifr);
  7119. case SIOCSHWTSTAMP:
  7120. return igb_ptp_set_ts_config(netdev, ifr);
  7121. default:
  7122. return -EOPNOTSUPP;
  7123. }
  7124. }
  7125. void igb_read_pci_cfg(struct e1000_hw *hw, u32 reg, u16 *value)
  7126. {
  7127. struct igb_adapter *adapter = hw->back;
  7128. pci_read_config_word(adapter->pdev, reg, value);
  7129. }
  7130. void igb_write_pci_cfg(struct e1000_hw *hw, u32 reg, u16 *value)
  7131. {
  7132. struct igb_adapter *adapter = hw->back;
  7133. pci_write_config_word(adapter->pdev, reg, *value);
  7134. }
  7135. s32 igb_read_pcie_cap_reg(struct e1000_hw *hw, u32 reg, u16 *value)
  7136. {
  7137. struct igb_adapter *adapter = hw->back;
  7138. if (pcie_capability_read_word(adapter->pdev, reg, value))
  7139. return -E1000_ERR_CONFIG;
  7140. return 0;
  7141. }
  7142. s32 igb_write_pcie_cap_reg(struct e1000_hw *hw, u32 reg, u16 *value)
  7143. {
  7144. struct igb_adapter *adapter = hw->back;
  7145. if (pcie_capability_write_word(adapter->pdev, reg, *value))
  7146. return -E1000_ERR_CONFIG;
  7147. return 0;
  7148. }
  7149. static void igb_vlan_mode(struct net_device *netdev, netdev_features_t features)
  7150. {
  7151. struct igb_adapter *adapter = netdev_priv(netdev);
  7152. struct e1000_hw *hw = &adapter->hw;
  7153. u32 ctrl, rctl;
  7154. bool enable = !!(features & NETIF_F_HW_VLAN_CTAG_RX);
  7155. if (enable) {
  7156. /* enable VLAN tag insert/strip */
  7157. ctrl = rd32(E1000_CTRL);
  7158. ctrl |= E1000_CTRL_VME;
  7159. wr32(E1000_CTRL, ctrl);
  7160. /* Disable CFI check */
  7161. rctl = rd32(E1000_RCTL);
  7162. rctl &= ~E1000_RCTL_CFIEN;
  7163. wr32(E1000_RCTL, rctl);
  7164. } else {
  7165. /* disable VLAN tag insert/strip */
  7166. ctrl = rd32(E1000_CTRL);
  7167. ctrl &= ~E1000_CTRL_VME;
  7168. wr32(E1000_CTRL, ctrl);
  7169. }
  7170. igb_set_vf_vlan_strip(adapter, adapter->vfs_allocated_count, enable);
  7171. }
  7172. static int igb_vlan_rx_add_vid(struct net_device *netdev,
  7173. __be16 proto, u16 vid)
  7174. {
  7175. struct igb_adapter *adapter = netdev_priv(netdev);
  7176. struct e1000_hw *hw = &adapter->hw;
  7177. int pf_id = adapter->vfs_allocated_count;
  7178. /* add the filter since PF can receive vlans w/o entry in vlvf */
  7179. if (!vid || !(adapter->flags & IGB_FLAG_VLAN_PROMISC))
  7180. igb_vfta_set(hw, vid, pf_id, true, !!vid);
  7181. set_bit(vid, adapter->active_vlans);
  7182. return 0;
  7183. }
  7184. static int igb_vlan_rx_kill_vid(struct net_device *netdev,
  7185. __be16 proto, u16 vid)
  7186. {
  7187. struct igb_adapter *adapter = netdev_priv(netdev);
  7188. int pf_id = adapter->vfs_allocated_count;
  7189. struct e1000_hw *hw = &adapter->hw;
  7190. /* remove VID from filter table */
  7191. if (vid && !(adapter->flags & IGB_FLAG_VLAN_PROMISC))
  7192. igb_vfta_set(hw, vid, pf_id, false, true);
  7193. clear_bit(vid, adapter->active_vlans);
  7194. return 0;
  7195. }
  7196. static void igb_restore_vlan(struct igb_adapter *adapter)
  7197. {
  7198. u16 vid = 1;
  7199. igb_vlan_mode(adapter->netdev, adapter->netdev->features);
  7200. igb_vlan_rx_add_vid(adapter->netdev, htons(ETH_P_8021Q), 0);
  7201. for_each_set_bit_from(vid, adapter->active_vlans, VLAN_N_VID)
  7202. igb_vlan_rx_add_vid(adapter->netdev, htons(ETH_P_8021Q), vid);
  7203. }
  7204. int igb_set_spd_dplx(struct igb_adapter *adapter, u32 spd, u8 dplx)
  7205. {
  7206. struct pci_dev *pdev = adapter->pdev;
  7207. struct e1000_mac_info *mac = &adapter->hw.mac;
  7208. mac->autoneg = 0;
  7209. /* Make sure dplx is at most 1 bit and lsb of speed is not set
  7210. * for the switch() below to work
  7211. */
  7212. if ((spd & 1) || (dplx & ~1))
  7213. goto err_inval;
  7214. /* Fiber NIC's only allow 1000 gbps Full duplex
  7215. * and 100Mbps Full duplex for 100baseFx sfp
  7216. */
  7217. if (adapter->hw.phy.media_type == e1000_media_type_internal_serdes) {
  7218. switch (spd + dplx) {
  7219. case SPEED_10 + DUPLEX_HALF:
  7220. case SPEED_10 + DUPLEX_FULL:
  7221. case SPEED_100 + DUPLEX_HALF:
  7222. goto err_inval;
  7223. default:
  7224. break;
  7225. }
  7226. }
  7227. switch (spd + dplx) {
  7228. case SPEED_10 + DUPLEX_HALF:
  7229. mac->forced_speed_duplex = ADVERTISE_10_HALF;
  7230. break;
  7231. case SPEED_10 + DUPLEX_FULL:
  7232. mac->forced_speed_duplex = ADVERTISE_10_FULL;
  7233. break;
  7234. case SPEED_100 + DUPLEX_HALF:
  7235. mac->forced_speed_duplex = ADVERTISE_100_HALF;
  7236. break;
  7237. case SPEED_100 + DUPLEX_FULL:
  7238. mac->forced_speed_duplex = ADVERTISE_100_FULL;
  7239. break;
  7240. case SPEED_1000 + DUPLEX_FULL:
  7241. mac->autoneg = 1;
  7242. adapter->hw.phy.autoneg_advertised = ADVERTISE_1000_FULL;
  7243. break;
  7244. case SPEED_1000 + DUPLEX_HALF: /* not supported */
  7245. default:
  7246. goto err_inval;
  7247. }
  7248. /* clear MDI, MDI(-X) override is only allowed when autoneg enabled */
  7249. adapter->hw.phy.mdix = AUTO_ALL_MODES;
  7250. return 0;
  7251. err_inval:
  7252. dev_err(&pdev->dev, "Unsupported Speed/Duplex configuration\n");
  7253. return -EINVAL;
  7254. }
  7255. static int __igb_shutdown(struct pci_dev *pdev, bool *enable_wake,
  7256. bool runtime)
  7257. {
  7258. struct net_device *netdev = pci_get_drvdata(pdev);
  7259. struct igb_adapter *adapter = netdev_priv(netdev);
  7260. struct e1000_hw *hw = &adapter->hw;
  7261. u32 ctrl, rctl, status;
  7262. u32 wufc = runtime ? E1000_WUFC_LNKC : adapter->wol;
  7263. #ifdef CONFIG_PM
  7264. int retval = 0;
  7265. #endif
  7266. rtnl_lock();
  7267. netif_device_detach(netdev);
  7268. if (netif_running(netdev))
  7269. __igb_close(netdev, true);
  7270. igb_ptp_suspend(adapter);
  7271. igb_clear_interrupt_scheme(adapter);
  7272. rtnl_unlock();
  7273. #ifdef CONFIG_PM
  7274. retval = pci_save_state(pdev);
  7275. if (retval)
  7276. return retval;
  7277. #endif
  7278. status = rd32(E1000_STATUS);
  7279. if (status & E1000_STATUS_LU)
  7280. wufc &= ~E1000_WUFC_LNKC;
  7281. if (wufc) {
  7282. igb_setup_rctl(adapter);
  7283. igb_set_rx_mode(netdev);
  7284. /* turn on all-multi mode if wake on multicast is enabled */
  7285. if (wufc & E1000_WUFC_MC) {
  7286. rctl = rd32(E1000_RCTL);
  7287. rctl |= E1000_RCTL_MPE;
  7288. wr32(E1000_RCTL, rctl);
  7289. }
  7290. ctrl = rd32(E1000_CTRL);
  7291. /* advertise wake from D3Cold */
  7292. #define E1000_CTRL_ADVD3WUC 0x00100000
  7293. /* phy power management enable */
  7294. #define E1000_CTRL_EN_PHY_PWR_MGMT 0x00200000
  7295. ctrl |= E1000_CTRL_ADVD3WUC;
  7296. wr32(E1000_CTRL, ctrl);
  7297. /* Allow time for pending master requests to run */
  7298. igb_disable_pcie_master(hw);
  7299. wr32(E1000_WUC, E1000_WUC_PME_EN);
  7300. wr32(E1000_WUFC, wufc);
  7301. } else {
  7302. wr32(E1000_WUC, 0);
  7303. wr32(E1000_WUFC, 0);
  7304. }
  7305. *enable_wake = wufc || adapter->en_mng_pt;
  7306. if (!*enable_wake)
  7307. igb_power_down_link(adapter);
  7308. else
  7309. igb_power_up_link(adapter);
  7310. /* Release control of h/w to f/w. If f/w is AMT enabled, this
  7311. * would have already happened in close and is redundant.
  7312. */
  7313. igb_release_hw_control(adapter);
  7314. pci_disable_device(pdev);
  7315. return 0;
  7316. }
  7317. static void igb_deliver_wake_packet(struct net_device *netdev)
  7318. {
  7319. struct igb_adapter *adapter = netdev_priv(netdev);
  7320. struct e1000_hw *hw = &adapter->hw;
  7321. struct sk_buff *skb;
  7322. u32 wupl;
  7323. wupl = rd32(E1000_WUPL) & E1000_WUPL_MASK;
  7324. /* WUPM stores only the first 128 bytes of the wake packet.
  7325. * Read the packet only if we have the whole thing.
  7326. */
  7327. if ((wupl == 0) || (wupl > E1000_WUPM_BYTES))
  7328. return;
  7329. skb = netdev_alloc_skb_ip_align(netdev, E1000_WUPM_BYTES);
  7330. if (!skb)
  7331. return;
  7332. skb_put(skb, wupl);
  7333. /* Ensure reads are 32-bit aligned */
  7334. wupl = roundup(wupl, 4);
  7335. memcpy_fromio(skb->data, hw->hw_addr + E1000_WUPM_REG(0), wupl);
  7336. skb->protocol = eth_type_trans(skb, netdev);
  7337. netif_rx(skb);
  7338. }
  7339. static int __maybe_unused igb_suspend(struct device *dev)
  7340. {
  7341. int retval;
  7342. bool wake;
  7343. struct pci_dev *pdev = to_pci_dev(dev);
  7344. retval = __igb_shutdown(pdev, &wake, 0);
  7345. if (retval)
  7346. return retval;
  7347. if (wake) {
  7348. pci_prepare_to_sleep(pdev);
  7349. } else {
  7350. pci_wake_from_d3(pdev, false);
  7351. pci_set_power_state(pdev, PCI_D3hot);
  7352. }
  7353. return 0;
  7354. }
  7355. static int __maybe_unused igb_resume(struct device *dev)
  7356. {
  7357. struct pci_dev *pdev = to_pci_dev(dev);
  7358. struct net_device *netdev = pci_get_drvdata(pdev);
  7359. struct igb_adapter *adapter = netdev_priv(netdev);
  7360. struct e1000_hw *hw = &adapter->hw;
  7361. u32 err, val;
  7362. pci_set_power_state(pdev, PCI_D0);
  7363. pci_restore_state(pdev);
  7364. pci_save_state(pdev);
  7365. if (!pci_device_is_present(pdev))
  7366. return -ENODEV;
  7367. err = pci_enable_device_mem(pdev);
  7368. if (err) {
  7369. dev_err(&pdev->dev,
  7370. "igb: Cannot enable PCI device from suspend\n");
  7371. return err;
  7372. }
  7373. pci_set_master(pdev);
  7374. pci_enable_wake(pdev, PCI_D3hot, 0);
  7375. pci_enable_wake(pdev, PCI_D3cold, 0);
  7376. if (igb_init_interrupt_scheme(adapter, true)) {
  7377. dev_err(&pdev->dev, "Unable to allocate memory for queues\n");
  7378. return -ENOMEM;
  7379. }
  7380. igb_reset(adapter);
  7381. /* let the f/w know that the h/w is now under the control of the
  7382. * driver.
  7383. */
  7384. igb_get_hw_control(adapter);
  7385. val = rd32(E1000_WUS);
  7386. if (val & WAKE_PKT_WUS)
  7387. igb_deliver_wake_packet(netdev);
  7388. wr32(E1000_WUS, ~0);
  7389. rtnl_lock();
  7390. if (!err && netif_running(netdev))
  7391. err = __igb_open(netdev, true);
  7392. if (!err)
  7393. netif_device_attach(netdev);
  7394. rtnl_unlock();
  7395. return err;
  7396. }
  7397. static int __maybe_unused igb_runtime_idle(struct device *dev)
  7398. {
  7399. struct pci_dev *pdev = to_pci_dev(dev);
  7400. struct net_device *netdev = pci_get_drvdata(pdev);
  7401. struct igb_adapter *adapter = netdev_priv(netdev);
  7402. if (!igb_has_link(adapter))
  7403. pm_schedule_suspend(dev, MSEC_PER_SEC * 5);
  7404. return -EBUSY;
  7405. }
  7406. static int __maybe_unused igb_runtime_suspend(struct device *dev)
  7407. {
  7408. struct pci_dev *pdev = to_pci_dev(dev);
  7409. int retval;
  7410. bool wake;
  7411. retval = __igb_shutdown(pdev, &wake, 1);
  7412. if (retval)
  7413. return retval;
  7414. if (wake) {
  7415. pci_prepare_to_sleep(pdev);
  7416. } else {
  7417. pci_wake_from_d3(pdev, false);
  7418. pci_set_power_state(pdev, PCI_D3hot);
  7419. }
  7420. return 0;
  7421. }
  7422. static int __maybe_unused igb_runtime_resume(struct device *dev)
  7423. {
  7424. return igb_resume(dev);
  7425. }
  7426. static void igb_shutdown(struct pci_dev *pdev)
  7427. {
  7428. bool wake;
  7429. __igb_shutdown(pdev, &wake, 0);
  7430. if (system_state == SYSTEM_POWER_OFF) {
  7431. pci_wake_from_d3(pdev, wake);
  7432. pci_set_power_state(pdev, PCI_D3hot);
  7433. }
  7434. }
  7435. #ifdef CONFIG_PCI_IOV
  7436. static int igb_sriov_reinit(struct pci_dev *dev)
  7437. {
  7438. struct net_device *netdev = pci_get_drvdata(dev);
  7439. struct igb_adapter *adapter = netdev_priv(netdev);
  7440. struct pci_dev *pdev = adapter->pdev;
  7441. rtnl_lock();
  7442. if (netif_running(netdev))
  7443. igb_close(netdev);
  7444. else
  7445. igb_reset(adapter);
  7446. igb_clear_interrupt_scheme(adapter);
  7447. igb_init_queue_configuration(adapter);
  7448. if (igb_init_interrupt_scheme(adapter, true)) {
  7449. rtnl_unlock();
  7450. dev_err(&pdev->dev, "Unable to allocate memory for queues\n");
  7451. return -ENOMEM;
  7452. }
  7453. if (netif_running(netdev))
  7454. igb_open(netdev);
  7455. rtnl_unlock();
  7456. return 0;
  7457. }
  7458. static int igb_pci_disable_sriov(struct pci_dev *dev)
  7459. {
  7460. int err = igb_disable_sriov(dev);
  7461. if (!err)
  7462. err = igb_sriov_reinit(dev);
  7463. return err;
  7464. }
  7465. static int igb_pci_enable_sriov(struct pci_dev *dev, int num_vfs)
  7466. {
  7467. int err = igb_enable_sriov(dev, num_vfs);
  7468. if (err)
  7469. goto out;
  7470. err = igb_sriov_reinit(dev);
  7471. if (!err)
  7472. return num_vfs;
  7473. out:
  7474. return err;
  7475. }
  7476. #endif
  7477. static int igb_pci_sriov_configure(struct pci_dev *dev, int num_vfs)
  7478. {
  7479. #ifdef CONFIG_PCI_IOV
  7480. if (num_vfs == 0)
  7481. return igb_pci_disable_sriov(dev);
  7482. else
  7483. return igb_pci_enable_sriov(dev, num_vfs);
  7484. #endif
  7485. return 0;
  7486. }
  7487. #ifdef CONFIG_NET_POLL_CONTROLLER
  7488. /* Polling 'interrupt' - used by things like netconsole to send skbs
  7489. * without having to re-enable interrupts. It's not called while
  7490. * the interrupt routine is executing.
  7491. */
  7492. static void igb_netpoll(struct net_device *netdev)
  7493. {
  7494. struct igb_adapter *adapter = netdev_priv(netdev);
  7495. struct e1000_hw *hw = &adapter->hw;
  7496. struct igb_q_vector *q_vector;
  7497. int i;
  7498. for (i = 0; i < adapter->num_q_vectors; i++) {
  7499. q_vector = adapter->q_vector[i];
  7500. if (adapter->flags & IGB_FLAG_HAS_MSIX)
  7501. wr32(E1000_EIMC, q_vector->eims_value);
  7502. else
  7503. igb_irq_disable(adapter);
  7504. napi_schedule(&q_vector->napi);
  7505. }
  7506. }
  7507. #endif /* CONFIG_NET_POLL_CONTROLLER */
  7508. /**
  7509. * igb_io_error_detected - called when PCI error is detected
  7510. * @pdev: Pointer to PCI device
  7511. * @state: The current pci connection state
  7512. *
  7513. * This function is called after a PCI bus error affecting
  7514. * this device has been detected.
  7515. **/
  7516. static pci_ers_result_t igb_io_error_detected(struct pci_dev *pdev,
  7517. pci_channel_state_t state)
  7518. {
  7519. struct net_device *netdev = pci_get_drvdata(pdev);
  7520. struct igb_adapter *adapter = netdev_priv(netdev);
  7521. netif_device_detach(netdev);
  7522. if (state == pci_channel_io_perm_failure)
  7523. return PCI_ERS_RESULT_DISCONNECT;
  7524. if (netif_running(netdev))
  7525. igb_down(adapter);
  7526. pci_disable_device(pdev);
  7527. /* Request a slot slot reset. */
  7528. return PCI_ERS_RESULT_NEED_RESET;
  7529. }
  7530. /**
  7531. * igb_io_slot_reset - called after the pci bus has been reset.
  7532. * @pdev: Pointer to PCI device
  7533. *
  7534. * Restart the card from scratch, as if from a cold-boot. Implementation
  7535. * resembles the first-half of the igb_resume routine.
  7536. **/
  7537. static pci_ers_result_t igb_io_slot_reset(struct pci_dev *pdev)
  7538. {
  7539. struct net_device *netdev = pci_get_drvdata(pdev);
  7540. struct igb_adapter *adapter = netdev_priv(netdev);
  7541. struct e1000_hw *hw = &adapter->hw;
  7542. pci_ers_result_t result;
  7543. int err;
  7544. if (pci_enable_device_mem(pdev)) {
  7545. dev_err(&pdev->dev,
  7546. "Cannot re-enable PCI device after reset.\n");
  7547. result = PCI_ERS_RESULT_DISCONNECT;
  7548. } else {
  7549. pci_set_master(pdev);
  7550. pci_restore_state(pdev);
  7551. pci_save_state(pdev);
  7552. pci_enable_wake(pdev, PCI_D3hot, 0);
  7553. pci_enable_wake(pdev, PCI_D3cold, 0);
  7554. /* In case of PCI error, adapter lose its HW address
  7555. * so we should re-assign it here.
  7556. */
  7557. hw->hw_addr = adapter->io_addr;
  7558. igb_reset(adapter);
  7559. wr32(E1000_WUS, ~0);
  7560. result = PCI_ERS_RESULT_RECOVERED;
  7561. }
  7562. err = pci_cleanup_aer_uncorrect_error_status(pdev);
  7563. if (err) {
  7564. dev_err(&pdev->dev,
  7565. "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
  7566. err);
  7567. /* non-fatal, continue */
  7568. }
  7569. return result;
  7570. }
  7571. /**
  7572. * igb_io_resume - called when traffic can start flowing again.
  7573. * @pdev: Pointer to PCI device
  7574. *
  7575. * This callback is called when the error recovery driver tells us that
  7576. * its OK to resume normal operation. Implementation resembles the
  7577. * second-half of the igb_resume routine.
  7578. */
  7579. static void igb_io_resume(struct pci_dev *pdev)
  7580. {
  7581. struct net_device *netdev = pci_get_drvdata(pdev);
  7582. struct igb_adapter *adapter = netdev_priv(netdev);
  7583. if (netif_running(netdev)) {
  7584. if (igb_up(adapter)) {
  7585. dev_err(&pdev->dev, "igb_up failed after reset\n");
  7586. return;
  7587. }
  7588. }
  7589. netif_device_attach(netdev);
  7590. /* let the f/w know that the h/w is now under the control of the
  7591. * driver.
  7592. */
  7593. igb_get_hw_control(adapter);
  7594. }
  7595. /**
  7596. * igb_rar_set_index - Sync RAL[index] and RAH[index] registers with MAC table
  7597. * @adapter: Pointer to adapter structure
  7598. * @index: Index of the RAR entry which need to be synced with MAC table
  7599. **/
  7600. static void igb_rar_set_index(struct igb_adapter *adapter, u32 index)
  7601. {
  7602. struct e1000_hw *hw = &adapter->hw;
  7603. u32 rar_low, rar_high;
  7604. u8 *addr = adapter->mac_table[index].addr;
  7605. /* HW expects these to be in network order when they are plugged
  7606. * into the registers which are little endian. In order to guarantee
  7607. * that ordering we need to do an leXX_to_cpup here in order to be
  7608. * ready for the byteswap that occurs with writel
  7609. */
  7610. rar_low = le32_to_cpup((__le32 *)(addr));
  7611. rar_high = le16_to_cpup((__le16 *)(addr + 4));
  7612. /* Indicate to hardware the Address is Valid. */
  7613. if (adapter->mac_table[index].state & IGB_MAC_STATE_IN_USE) {
  7614. if (is_valid_ether_addr(addr))
  7615. rar_high |= E1000_RAH_AV;
  7616. if (adapter->mac_table[index].state & IGB_MAC_STATE_SRC_ADDR)
  7617. rar_high |= E1000_RAH_ASEL_SRC_ADDR;
  7618. switch (hw->mac.type) {
  7619. case e1000_82575:
  7620. case e1000_i210:
  7621. if (adapter->mac_table[index].state &
  7622. IGB_MAC_STATE_QUEUE_STEERING)
  7623. rar_high |= E1000_RAH_QSEL_ENABLE;
  7624. rar_high |= E1000_RAH_POOL_1 *
  7625. adapter->mac_table[index].queue;
  7626. break;
  7627. default:
  7628. rar_high |= E1000_RAH_POOL_1 <<
  7629. adapter->mac_table[index].queue;
  7630. break;
  7631. }
  7632. }
  7633. wr32(E1000_RAL(index), rar_low);
  7634. wrfl();
  7635. wr32(E1000_RAH(index), rar_high);
  7636. wrfl();
  7637. }
  7638. static int igb_set_vf_mac(struct igb_adapter *adapter,
  7639. int vf, unsigned char *mac_addr)
  7640. {
  7641. struct e1000_hw *hw = &adapter->hw;
  7642. /* VF MAC addresses start at end of receive addresses and moves
  7643. * towards the first, as a result a collision should not be possible
  7644. */
  7645. int rar_entry = hw->mac.rar_entry_count - (vf + 1);
  7646. unsigned char *vf_mac_addr = adapter->vf_data[vf].vf_mac_addresses;
  7647. ether_addr_copy(vf_mac_addr, mac_addr);
  7648. ether_addr_copy(adapter->mac_table[rar_entry].addr, mac_addr);
  7649. adapter->mac_table[rar_entry].queue = vf;
  7650. adapter->mac_table[rar_entry].state |= IGB_MAC_STATE_IN_USE;
  7651. igb_rar_set_index(adapter, rar_entry);
  7652. return 0;
  7653. }
  7654. static int igb_ndo_set_vf_mac(struct net_device *netdev, int vf, u8 *mac)
  7655. {
  7656. struct igb_adapter *adapter = netdev_priv(netdev);
  7657. if (vf >= adapter->vfs_allocated_count)
  7658. return -EINVAL;
  7659. /* Setting the VF MAC to 0 reverts the IGB_VF_FLAG_PF_SET_MAC
  7660. * flag and allows to overwrite the MAC via VF netdev. This
  7661. * is necessary to allow libvirt a way to restore the original
  7662. * MAC after unbinding vfio-pci and reloading igbvf after shutting
  7663. * down a VM.
  7664. */
  7665. if (is_zero_ether_addr(mac)) {
  7666. adapter->vf_data[vf].flags &= ~IGB_VF_FLAG_PF_SET_MAC;
  7667. dev_info(&adapter->pdev->dev,
  7668. "remove administratively set MAC on VF %d\n",
  7669. vf);
  7670. } else if (is_valid_ether_addr(mac)) {
  7671. adapter->vf_data[vf].flags |= IGB_VF_FLAG_PF_SET_MAC;
  7672. dev_info(&adapter->pdev->dev, "setting MAC %pM on VF %d\n",
  7673. mac, vf);
  7674. dev_info(&adapter->pdev->dev,
  7675. "Reload the VF driver to make this change effective.");
  7676. /* Generate additional warning if PF is down */
  7677. if (test_bit(__IGB_DOWN, &adapter->state)) {
  7678. dev_warn(&adapter->pdev->dev,
  7679. "The VF MAC address has been set, but the PF device is not up.\n");
  7680. dev_warn(&adapter->pdev->dev,
  7681. "Bring the PF device up before attempting to use the VF device.\n");
  7682. }
  7683. } else {
  7684. return -EINVAL;
  7685. }
  7686. return igb_set_vf_mac(adapter, vf, mac);
  7687. }
  7688. static int igb_link_mbps(int internal_link_speed)
  7689. {
  7690. switch (internal_link_speed) {
  7691. case SPEED_100:
  7692. return 100;
  7693. case SPEED_1000:
  7694. return 1000;
  7695. default:
  7696. return 0;
  7697. }
  7698. }
  7699. static void igb_set_vf_rate_limit(struct e1000_hw *hw, int vf, int tx_rate,
  7700. int link_speed)
  7701. {
  7702. int rf_dec, rf_int;
  7703. u32 bcnrc_val;
  7704. if (tx_rate != 0) {
  7705. /* Calculate the rate factor values to set */
  7706. rf_int = link_speed / tx_rate;
  7707. rf_dec = (link_speed - (rf_int * tx_rate));
  7708. rf_dec = (rf_dec * BIT(E1000_RTTBCNRC_RF_INT_SHIFT)) /
  7709. tx_rate;
  7710. bcnrc_val = E1000_RTTBCNRC_RS_ENA;
  7711. bcnrc_val |= ((rf_int << E1000_RTTBCNRC_RF_INT_SHIFT) &
  7712. E1000_RTTBCNRC_RF_INT_MASK);
  7713. bcnrc_val |= (rf_dec & E1000_RTTBCNRC_RF_DEC_MASK);
  7714. } else {
  7715. bcnrc_val = 0;
  7716. }
  7717. wr32(E1000_RTTDQSEL, vf); /* vf X uses queue X */
  7718. /* Set global transmit compensation time to the MMW_SIZE in RTTBCNRM
  7719. * register. MMW_SIZE=0x014 if 9728-byte jumbo is supported.
  7720. */
  7721. wr32(E1000_RTTBCNRM, 0x14);
  7722. wr32(E1000_RTTBCNRC, bcnrc_val);
  7723. }
  7724. static void igb_check_vf_rate_limit(struct igb_adapter *adapter)
  7725. {
  7726. int actual_link_speed, i;
  7727. bool reset_rate = false;
  7728. /* VF TX rate limit was not set or not supported */
  7729. if ((adapter->vf_rate_link_speed == 0) ||
  7730. (adapter->hw.mac.type != e1000_82576))
  7731. return;
  7732. actual_link_speed = igb_link_mbps(adapter->link_speed);
  7733. if (actual_link_speed != adapter->vf_rate_link_speed) {
  7734. reset_rate = true;
  7735. adapter->vf_rate_link_speed = 0;
  7736. dev_info(&adapter->pdev->dev,
  7737. "Link speed has been changed. VF Transmit rate is disabled\n");
  7738. }
  7739. for (i = 0; i < adapter->vfs_allocated_count; i++) {
  7740. if (reset_rate)
  7741. adapter->vf_data[i].tx_rate = 0;
  7742. igb_set_vf_rate_limit(&adapter->hw, i,
  7743. adapter->vf_data[i].tx_rate,
  7744. actual_link_speed);
  7745. }
  7746. }
  7747. static int igb_ndo_set_vf_bw(struct net_device *netdev, int vf,
  7748. int min_tx_rate, int max_tx_rate)
  7749. {
  7750. struct igb_adapter *adapter = netdev_priv(netdev);
  7751. struct e1000_hw *hw = &adapter->hw;
  7752. int actual_link_speed;
  7753. if (hw->mac.type != e1000_82576)
  7754. return -EOPNOTSUPP;
  7755. if (min_tx_rate)
  7756. return -EINVAL;
  7757. actual_link_speed = igb_link_mbps(adapter->link_speed);
  7758. if ((vf >= adapter->vfs_allocated_count) ||
  7759. (!(rd32(E1000_STATUS) & E1000_STATUS_LU)) ||
  7760. (max_tx_rate < 0) ||
  7761. (max_tx_rate > actual_link_speed))
  7762. return -EINVAL;
  7763. adapter->vf_rate_link_speed = actual_link_speed;
  7764. adapter->vf_data[vf].tx_rate = (u16)max_tx_rate;
  7765. igb_set_vf_rate_limit(hw, vf, max_tx_rate, actual_link_speed);
  7766. return 0;
  7767. }
  7768. static int igb_ndo_set_vf_spoofchk(struct net_device *netdev, int vf,
  7769. bool setting)
  7770. {
  7771. struct igb_adapter *adapter = netdev_priv(netdev);
  7772. struct e1000_hw *hw = &adapter->hw;
  7773. u32 reg_val, reg_offset;
  7774. if (!adapter->vfs_allocated_count)
  7775. return -EOPNOTSUPP;
  7776. if (vf >= adapter->vfs_allocated_count)
  7777. return -EINVAL;
  7778. reg_offset = (hw->mac.type == e1000_82576) ? E1000_DTXSWC : E1000_TXSWC;
  7779. reg_val = rd32(reg_offset);
  7780. if (setting)
  7781. reg_val |= (BIT(vf) |
  7782. BIT(vf + E1000_DTXSWC_VLAN_SPOOF_SHIFT));
  7783. else
  7784. reg_val &= ~(BIT(vf) |
  7785. BIT(vf + E1000_DTXSWC_VLAN_SPOOF_SHIFT));
  7786. wr32(reg_offset, reg_val);
  7787. adapter->vf_data[vf].spoofchk_enabled = setting;
  7788. return 0;
  7789. }
  7790. static int igb_ndo_set_vf_trust(struct net_device *netdev, int vf, bool setting)
  7791. {
  7792. struct igb_adapter *adapter = netdev_priv(netdev);
  7793. if (vf >= adapter->vfs_allocated_count)
  7794. return -EINVAL;
  7795. if (adapter->vf_data[vf].trusted == setting)
  7796. return 0;
  7797. adapter->vf_data[vf].trusted = setting;
  7798. dev_info(&adapter->pdev->dev, "VF %u is %strusted\n",
  7799. vf, setting ? "" : "not ");
  7800. return 0;
  7801. }
  7802. static int igb_ndo_get_vf_config(struct net_device *netdev,
  7803. int vf, struct ifla_vf_info *ivi)
  7804. {
  7805. struct igb_adapter *adapter = netdev_priv(netdev);
  7806. if (vf >= adapter->vfs_allocated_count)
  7807. return -EINVAL;
  7808. ivi->vf = vf;
  7809. memcpy(&ivi->mac, adapter->vf_data[vf].vf_mac_addresses, ETH_ALEN);
  7810. ivi->max_tx_rate = adapter->vf_data[vf].tx_rate;
  7811. ivi->min_tx_rate = 0;
  7812. ivi->vlan = adapter->vf_data[vf].pf_vlan;
  7813. ivi->qos = adapter->vf_data[vf].pf_qos;
  7814. ivi->spoofchk = adapter->vf_data[vf].spoofchk_enabled;
  7815. ivi->trusted = adapter->vf_data[vf].trusted;
  7816. return 0;
  7817. }
  7818. static void igb_vmm_control(struct igb_adapter *adapter)
  7819. {
  7820. struct e1000_hw *hw = &adapter->hw;
  7821. u32 reg;
  7822. switch (hw->mac.type) {
  7823. case e1000_82575:
  7824. case e1000_i210:
  7825. case e1000_i211:
  7826. case e1000_i354:
  7827. default:
  7828. /* replication is not supported for 82575 */
  7829. return;
  7830. case e1000_82576:
  7831. /* notify HW that the MAC is adding vlan tags */
  7832. reg = rd32(E1000_DTXCTL);
  7833. reg |= E1000_DTXCTL_VLAN_ADDED;
  7834. wr32(E1000_DTXCTL, reg);
  7835. /* Fall through */
  7836. case e1000_82580:
  7837. /* enable replication vlan tag stripping */
  7838. reg = rd32(E1000_RPLOLR);
  7839. reg |= E1000_RPLOLR_STRVLAN;
  7840. wr32(E1000_RPLOLR, reg);
  7841. /* Fall through */
  7842. case e1000_i350:
  7843. /* none of the above registers are supported by i350 */
  7844. break;
  7845. }
  7846. if (adapter->vfs_allocated_count) {
  7847. igb_vmdq_set_loopback_pf(hw, true);
  7848. igb_vmdq_set_replication_pf(hw, true);
  7849. igb_vmdq_set_anti_spoofing_pf(hw, true,
  7850. adapter->vfs_allocated_count);
  7851. } else {
  7852. igb_vmdq_set_loopback_pf(hw, false);
  7853. igb_vmdq_set_replication_pf(hw, false);
  7854. }
  7855. }
  7856. static void igb_init_dmac(struct igb_adapter *adapter, u32 pba)
  7857. {
  7858. struct e1000_hw *hw = &adapter->hw;
  7859. u32 dmac_thr;
  7860. u16 hwm;
  7861. if (hw->mac.type > e1000_82580) {
  7862. if (adapter->flags & IGB_FLAG_DMAC) {
  7863. u32 reg;
  7864. /* force threshold to 0. */
  7865. wr32(E1000_DMCTXTH, 0);
  7866. /* DMA Coalescing high water mark needs to be greater
  7867. * than the Rx threshold. Set hwm to PBA - max frame
  7868. * size in 16B units, capping it at PBA - 6KB.
  7869. */
  7870. hwm = 64 * (pba - 6);
  7871. reg = rd32(E1000_FCRTC);
  7872. reg &= ~E1000_FCRTC_RTH_COAL_MASK;
  7873. reg |= ((hwm << E1000_FCRTC_RTH_COAL_SHIFT)
  7874. & E1000_FCRTC_RTH_COAL_MASK);
  7875. wr32(E1000_FCRTC, reg);
  7876. /* Set the DMA Coalescing Rx threshold to PBA - 2 * max
  7877. * frame size, capping it at PBA - 10KB.
  7878. */
  7879. dmac_thr = pba - 10;
  7880. reg = rd32(E1000_DMACR);
  7881. reg &= ~E1000_DMACR_DMACTHR_MASK;
  7882. reg |= ((dmac_thr << E1000_DMACR_DMACTHR_SHIFT)
  7883. & E1000_DMACR_DMACTHR_MASK);
  7884. /* transition to L0x or L1 if available..*/
  7885. reg |= (E1000_DMACR_DMAC_EN | E1000_DMACR_DMAC_LX_MASK);
  7886. /* watchdog timer= +-1000 usec in 32usec intervals */
  7887. reg |= (1000 >> 5);
  7888. /* Disable BMC-to-OS Watchdog Enable */
  7889. if (hw->mac.type != e1000_i354)
  7890. reg &= ~E1000_DMACR_DC_BMC2OSW_EN;
  7891. wr32(E1000_DMACR, reg);
  7892. /* no lower threshold to disable
  7893. * coalescing(smart fifb)-UTRESH=0
  7894. */
  7895. wr32(E1000_DMCRTRH, 0);
  7896. reg = (IGB_DMCTLX_DCFLUSH_DIS | 0x4);
  7897. wr32(E1000_DMCTLX, reg);
  7898. /* free space in tx packet buffer to wake from
  7899. * DMA coal
  7900. */
  7901. wr32(E1000_DMCTXTH, (IGB_MIN_TXPBSIZE -
  7902. (IGB_TX_BUF_4096 + adapter->max_frame_size)) >> 6);
  7903. /* make low power state decision controlled
  7904. * by DMA coal
  7905. */
  7906. reg = rd32(E1000_PCIEMISC);
  7907. reg &= ~E1000_PCIEMISC_LX_DECISION;
  7908. wr32(E1000_PCIEMISC, reg);
  7909. } /* endif adapter->dmac is not disabled */
  7910. } else if (hw->mac.type == e1000_82580) {
  7911. u32 reg = rd32(E1000_PCIEMISC);
  7912. wr32(E1000_PCIEMISC, reg & ~E1000_PCIEMISC_LX_DECISION);
  7913. wr32(E1000_DMACR, 0);
  7914. }
  7915. }
  7916. /**
  7917. * igb_read_i2c_byte - Reads 8 bit word over I2C
  7918. * @hw: pointer to hardware structure
  7919. * @byte_offset: byte offset to read
  7920. * @dev_addr: device address
  7921. * @data: value read
  7922. *
  7923. * Performs byte read operation over I2C interface at
  7924. * a specified device address.
  7925. **/
  7926. s32 igb_read_i2c_byte(struct e1000_hw *hw, u8 byte_offset,
  7927. u8 dev_addr, u8 *data)
  7928. {
  7929. struct igb_adapter *adapter = container_of(hw, struct igb_adapter, hw);
  7930. struct i2c_client *this_client = adapter->i2c_client;
  7931. s32 status;
  7932. u16 swfw_mask = 0;
  7933. if (!this_client)
  7934. return E1000_ERR_I2C;
  7935. swfw_mask = E1000_SWFW_PHY0_SM;
  7936. if (hw->mac.ops.acquire_swfw_sync(hw, swfw_mask))
  7937. return E1000_ERR_SWFW_SYNC;
  7938. status = i2c_smbus_read_byte_data(this_client, byte_offset);
  7939. hw->mac.ops.release_swfw_sync(hw, swfw_mask);
  7940. if (status < 0)
  7941. return E1000_ERR_I2C;
  7942. else {
  7943. *data = status;
  7944. return 0;
  7945. }
  7946. }
  7947. /**
  7948. * igb_write_i2c_byte - Writes 8 bit word over I2C
  7949. * @hw: pointer to hardware structure
  7950. * @byte_offset: byte offset to write
  7951. * @dev_addr: device address
  7952. * @data: value to write
  7953. *
  7954. * Performs byte write operation over I2C interface at
  7955. * a specified device address.
  7956. **/
  7957. s32 igb_write_i2c_byte(struct e1000_hw *hw, u8 byte_offset,
  7958. u8 dev_addr, u8 data)
  7959. {
  7960. struct igb_adapter *adapter = container_of(hw, struct igb_adapter, hw);
  7961. struct i2c_client *this_client = adapter->i2c_client;
  7962. s32 status;
  7963. u16 swfw_mask = E1000_SWFW_PHY0_SM;
  7964. if (!this_client)
  7965. return E1000_ERR_I2C;
  7966. if (hw->mac.ops.acquire_swfw_sync(hw, swfw_mask))
  7967. return E1000_ERR_SWFW_SYNC;
  7968. status = i2c_smbus_write_byte_data(this_client, byte_offset, data);
  7969. hw->mac.ops.release_swfw_sync(hw, swfw_mask);
  7970. if (status)
  7971. return E1000_ERR_I2C;
  7972. else
  7973. return 0;
  7974. }
  7975. int igb_reinit_queues(struct igb_adapter *adapter)
  7976. {
  7977. struct net_device *netdev = adapter->netdev;
  7978. struct pci_dev *pdev = adapter->pdev;
  7979. int err = 0;
  7980. if (netif_running(netdev))
  7981. igb_close(netdev);
  7982. igb_reset_interrupt_capability(adapter);
  7983. if (igb_init_interrupt_scheme(adapter, true)) {
  7984. dev_err(&pdev->dev, "Unable to allocate memory for queues\n");
  7985. return -ENOMEM;
  7986. }
  7987. if (netif_running(netdev))
  7988. err = igb_open(netdev);
  7989. return err;
  7990. }
  7991. static void igb_nfc_filter_exit(struct igb_adapter *adapter)
  7992. {
  7993. struct igb_nfc_filter *rule;
  7994. spin_lock(&adapter->nfc_lock);
  7995. hlist_for_each_entry(rule, &adapter->nfc_filter_list, nfc_node)
  7996. igb_erase_filter(adapter, rule);
  7997. hlist_for_each_entry(rule, &adapter->cls_flower_list, nfc_node)
  7998. igb_erase_filter(adapter, rule);
  7999. spin_unlock(&adapter->nfc_lock);
  8000. }
  8001. static void igb_nfc_filter_restore(struct igb_adapter *adapter)
  8002. {
  8003. struct igb_nfc_filter *rule;
  8004. spin_lock(&adapter->nfc_lock);
  8005. hlist_for_each_entry(rule, &adapter->nfc_filter_list, nfc_node)
  8006. igb_add_filter(adapter, rule);
  8007. spin_unlock(&adapter->nfc_lock);
  8008. }
  8009. /* igb_main.c */