igb_main.c 216 KB

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