igb_main.c 246 KB

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