igb_main.c 255 KB

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