igb_main.c 215 KB

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