igb_main.c 223 KB

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