igb_main.c 244 KB

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