igb_main.c 223 KB

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