igb_main.c 217 KB

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