igb_main.c 220 KB

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