igb_main.c 215 KB

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