igb_main.c 257 KB

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