igb_main.c 234 KB

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