igb_main.c 217 KB

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