igb_main.c 221 KB

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