igb_main.c 246 KB

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