igb_main.c 257 KB

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