igb_main.c 258 KB

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