igb_main.c 234 KB

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