igb_main.c 217 KB

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