igb_main.c 217 KB

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