igb_main.c 251 KB

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