igb_main.c 179 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511
  1. /*******************************************************************************
  2. Intel(R) Gigabit Ethernet Linux driver
  3. Copyright(c) 2007-2009 Intel Corporation.
  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. This program is distributed in the hope it will be useful, but WITHOUT
  8. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  9. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  10. more details.
  11. You should have received a copy of the GNU General Public License along with
  12. this program; if not, write to the Free Software Foundation, Inc.,
  13. 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  14. The full GNU General Public License is included in this distribution in
  15. the file called "COPYING".
  16. Contact Information:
  17. e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
  18. Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  19. *******************************************************************************/
  20. #include <linux/module.h>
  21. #include <linux/types.h>
  22. #include <linux/init.h>
  23. #include <linux/vmalloc.h>
  24. #include <linux/pagemap.h>
  25. #include <linux/netdevice.h>
  26. #include <linux/ipv6.h>
  27. #include <linux/slab.h>
  28. #include <net/checksum.h>
  29. #include <net/ip6_checksum.h>
  30. #include <linux/net_tstamp.h>
  31. #include <linux/mii.h>
  32. #include <linux/ethtool.h>
  33. #include <linux/if_vlan.h>
  34. #include <linux/pci.h>
  35. #include <linux/pci-aspm.h>
  36. #include <linux/delay.h>
  37. #include <linux/interrupt.h>
  38. #include <linux/if_ether.h>
  39. #include <linux/aer.h>
  40. #ifdef CONFIG_IGB_DCA
  41. #include <linux/dca.h>
  42. #endif
  43. #include "igb.h"
  44. #define DRV_VERSION "2.1.0-k2"
  45. char igb_driver_name[] = "igb";
  46. char igb_driver_version[] = DRV_VERSION;
  47. static const char igb_driver_string[] =
  48. "Intel(R) Gigabit Ethernet Network Driver";
  49. static const char igb_copyright[] = "Copyright (c) 2007-2009 Intel Corporation.";
  50. static const struct e1000_info *igb_info_tbl[] = {
  51. [board_82575] = &e1000_82575_info,
  52. };
  53. static DEFINE_PCI_DEVICE_TABLE(igb_pci_tbl) = {
  54. { PCI_VDEVICE(INTEL, E1000_DEV_ID_I350_COPPER), board_82575 },
  55. { PCI_VDEVICE(INTEL, E1000_DEV_ID_I350_FIBER), board_82575 },
  56. { PCI_VDEVICE(INTEL, E1000_DEV_ID_I350_SERDES), board_82575 },
  57. { PCI_VDEVICE(INTEL, E1000_DEV_ID_I350_SGMII), board_82575 },
  58. { PCI_VDEVICE(INTEL, E1000_DEV_ID_82580_COPPER), board_82575 },
  59. { PCI_VDEVICE(INTEL, E1000_DEV_ID_82580_FIBER), board_82575 },
  60. { PCI_VDEVICE(INTEL, E1000_DEV_ID_82580_SERDES), board_82575 },
  61. { PCI_VDEVICE(INTEL, E1000_DEV_ID_82580_SGMII), board_82575 },
  62. { PCI_VDEVICE(INTEL, E1000_DEV_ID_82580_COPPER_DUAL), board_82575 },
  63. { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576), board_82575 },
  64. { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_NS), board_82575 },
  65. { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_NS_SERDES), board_82575 },
  66. { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_FIBER), board_82575 },
  67. { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_SERDES), board_82575 },
  68. { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_SERDES_QUAD), board_82575 },
  69. { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_QUAD_COPPER_ET2), board_82575 },
  70. { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_QUAD_COPPER), board_82575 },
  71. { PCI_VDEVICE(INTEL, E1000_DEV_ID_82575EB_COPPER), board_82575 },
  72. { PCI_VDEVICE(INTEL, E1000_DEV_ID_82575EB_FIBER_SERDES), board_82575 },
  73. { PCI_VDEVICE(INTEL, E1000_DEV_ID_82575GB_QUAD_COPPER), board_82575 },
  74. /* required last entry */
  75. {0, }
  76. };
  77. MODULE_DEVICE_TABLE(pci, igb_pci_tbl);
  78. void igb_reset(struct igb_adapter *);
  79. static int igb_setup_all_tx_resources(struct igb_adapter *);
  80. static int igb_setup_all_rx_resources(struct igb_adapter *);
  81. static void igb_free_all_tx_resources(struct igb_adapter *);
  82. static void igb_free_all_rx_resources(struct igb_adapter *);
  83. static void igb_setup_mrqc(struct igb_adapter *);
  84. void igb_update_stats(struct igb_adapter *);
  85. static int igb_probe(struct pci_dev *, const struct pci_device_id *);
  86. static void __devexit igb_remove(struct pci_dev *pdev);
  87. static int igb_sw_init(struct igb_adapter *);
  88. static int igb_open(struct net_device *);
  89. static int igb_close(struct net_device *);
  90. static void igb_configure_tx(struct igb_adapter *);
  91. static void igb_configure_rx(struct igb_adapter *);
  92. static void igb_clean_all_tx_rings(struct igb_adapter *);
  93. static void igb_clean_all_rx_rings(struct igb_adapter *);
  94. static void igb_clean_tx_ring(struct igb_ring *);
  95. static void igb_clean_rx_ring(struct igb_ring *);
  96. static void igb_set_rx_mode(struct net_device *);
  97. static void igb_update_phy_info(unsigned long);
  98. static void igb_watchdog(unsigned long);
  99. static void igb_watchdog_task(struct work_struct *);
  100. static netdev_tx_t igb_xmit_frame_adv(struct sk_buff *skb, struct net_device *);
  101. static struct net_device_stats *igb_get_stats(struct net_device *);
  102. static int igb_change_mtu(struct net_device *, int);
  103. static int igb_set_mac(struct net_device *, void *);
  104. static void igb_set_uta(struct igb_adapter *adapter);
  105. static irqreturn_t igb_intr(int irq, void *);
  106. static irqreturn_t igb_intr_msi(int irq, void *);
  107. static irqreturn_t igb_msix_other(int irq, void *);
  108. static irqreturn_t igb_msix_ring(int irq, void *);
  109. #ifdef CONFIG_IGB_DCA
  110. static void igb_update_dca(struct igb_q_vector *);
  111. static void igb_setup_dca(struct igb_adapter *);
  112. #endif /* CONFIG_IGB_DCA */
  113. static bool igb_clean_tx_irq(struct igb_q_vector *);
  114. static int igb_poll(struct napi_struct *, int);
  115. static bool igb_clean_rx_irq_adv(struct igb_q_vector *, int *, int);
  116. static int igb_ioctl(struct net_device *, struct ifreq *, int cmd);
  117. static void igb_tx_timeout(struct net_device *);
  118. static void igb_reset_task(struct work_struct *);
  119. static void igb_vlan_rx_register(struct net_device *, struct vlan_group *);
  120. static void igb_vlan_rx_add_vid(struct net_device *, u16);
  121. static void igb_vlan_rx_kill_vid(struct net_device *, u16);
  122. static void igb_restore_vlan(struct igb_adapter *);
  123. static void igb_rar_set_qsel(struct igb_adapter *, u8 *, u32 , u8);
  124. static void igb_ping_all_vfs(struct igb_adapter *);
  125. static void igb_msg_task(struct igb_adapter *);
  126. static void igb_vmm_control(struct igb_adapter *);
  127. static int igb_set_vf_mac(struct igb_adapter *, int, unsigned char *);
  128. static void igb_restore_vf_multicasts(struct igb_adapter *adapter);
  129. static int igb_ndo_set_vf_mac(struct net_device *netdev, int vf, u8 *mac);
  130. static int igb_ndo_set_vf_vlan(struct net_device *netdev,
  131. int vf, u16 vlan, u8 qos);
  132. static int igb_ndo_set_vf_bw(struct net_device *netdev, int vf, int tx_rate);
  133. static int igb_ndo_get_vf_config(struct net_device *netdev, int vf,
  134. struct ifla_vf_info *ivi);
  135. #ifdef CONFIG_PM
  136. static int igb_suspend(struct pci_dev *, pm_message_t);
  137. static int igb_resume(struct pci_dev *);
  138. #endif
  139. static void igb_shutdown(struct pci_dev *);
  140. #ifdef CONFIG_IGB_DCA
  141. static int igb_notify_dca(struct notifier_block *, unsigned long, void *);
  142. static struct notifier_block dca_notifier = {
  143. .notifier_call = igb_notify_dca,
  144. .next = NULL,
  145. .priority = 0
  146. };
  147. #endif
  148. #ifdef CONFIG_NET_POLL_CONTROLLER
  149. /* for netdump / net console */
  150. static void igb_netpoll(struct net_device *);
  151. #endif
  152. #ifdef CONFIG_PCI_IOV
  153. static unsigned int max_vfs = 0;
  154. module_param(max_vfs, uint, 0);
  155. MODULE_PARM_DESC(max_vfs, "Maximum number of virtual functions to allocate "
  156. "per physical function");
  157. #endif /* CONFIG_PCI_IOV */
  158. static pci_ers_result_t igb_io_error_detected(struct pci_dev *,
  159. pci_channel_state_t);
  160. static pci_ers_result_t igb_io_slot_reset(struct pci_dev *);
  161. static void igb_io_resume(struct pci_dev *);
  162. static struct pci_error_handlers igb_err_handler = {
  163. .error_detected = igb_io_error_detected,
  164. .slot_reset = igb_io_slot_reset,
  165. .resume = igb_io_resume,
  166. };
  167. static struct pci_driver igb_driver = {
  168. .name = igb_driver_name,
  169. .id_table = igb_pci_tbl,
  170. .probe = igb_probe,
  171. .remove = __devexit_p(igb_remove),
  172. #ifdef CONFIG_PM
  173. /* Power Managment Hooks */
  174. .suspend = igb_suspend,
  175. .resume = igb_resume,
  176. #endif
  177. .shutdown = igb_shutdown,
  178. .err_handler = &igb_err_handler
  179. };
  180. MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
  181. MODULE_DESCRIPTION("Intel(R) Gigabit Ethernet Network Driver");
  182. MODULE_LICENSE("GPL");
  183. MODULE_VERSION(DRV_VERSION);
  184. struct igb_reg_info {
  185. u32 ofs;
  186. char *name;
  187. };
  188. static const struct igb_reg_info igb_reg_info_tbl[] = {
  189. /* General Registers */
  190. {E1000_CTRL, "CTRL"},
  191. {E1000_STATUS, "STATUS"},
  192. {E1000_CTRL_EXT, "CTRL_EXT"},
  193. /* Interrupt Registers */
  194. {E1000_ICR, "ICR"},
  195. /* RX Registers */
  196. {E1000_RCTL, "RCTL"},
  197. {E1000_RDLEN(0), "RDLEN"},
  198. {E1000_RDH(0), "RDH"},
  199. {E1000_RDT(0), "RDT"},
  200. {E1000_RXDCTL(0), "RXDCTL"},
  201. {E1000_RDBAL(0), "RDBAL"},
  202. {E1000_RDBAH(0), "RDBAH"},
  203. /* TX Registers */
  204. {E1000_TCTL, "TCTL"},
  205. {E1000_TDBAL(0), "TDBAL"},
  206. {E1000_TDBAH(0), "TDBAH"},
  207. {E1000_TDLEN(0), "TDLEN"},
  208. {E1000_TDH(0), "TDH"},
  209. {E1000_TDT(0), "TDT"},
  210. {E1000_TXDCTL(0), "TXDCTL"},
  211. {E1000_TDFH, "TDFH"},
  212. {E1000_TDFT, "TDFT"},
  213. {E1000_TDFHS, "TDFHS"},
  214. {E1000_TDFPC, "TDFPC"},
  215. /* List Terminator */
  216. {}
  217. };
  218. /*
  219. * igb_regdump - register printout routine
  220. */
  221. static void igb_regdump(struct e1000_hw *hw, struct igb_reg_info *reginfo)
  222. {
  223. int n = 0;
  224. char rname[16];
  225. u32 regs[8];
  226. switch (reginfo->ofs) {
  227. case E1000_RDLEN(0):
  228. for (n = 0; n < 4; n++)
  229. regs[n] = rd32(E1000_RDLEN(n));
  230. break;
  231. case E1000_RDH(0):
  232. for (n = 0; n < 4; n++)
  233. regs[n] = rd32(E1000_RDH(n));
  234. break;
  235. case E1000_RDT(0):
  236. for (n = 0; n < 4; n++)
  237. regs[n] = rd32(E1000_RDT(n));
  238. break;
  239. case E1000_RXDCTL(0):
  240. for (n = 0; n < 4; n++)
  241. regs[n] = rd32(E1000_RXDCTL(n));
  242. break;
  243. case E1000_RDBAL(0):
  244. for (n = 0; n < 4; n++)
  245. regs[n] = rd32(E1000_RDBAL(n));
  246. break;
  247. case E1000_RDBAH(0):
  248. for (n = 0; n < 4; n++)
  249. regs[n] = rd32(E1000_RDBAH(n));
  250. break;
  251. case E1000_TDBAL(0):
  252. for (n = 0; n < 4; n++)
  253. regs[n] = rd32(E1000_RDBAL(n));
  254. break;
  255. case E1000_TDBAH(0):
  256. for (n = 0; n < 4; n++)
  257. regs[n] = rd32(E1000_TDBAH(n));
  258. break;
  259. case E1000_TDLEN(0):
  260. for (n = 0; n < 4; n++)
  261. regs[n] = rd32(E1000_TDLEN(n));
  262. break;
  263. case E1000_TDH(0):
  264. for (n = 0; n < 4; n++)
  265. regs[n] = rd32(E1000_TDH(n));
  266. break;
  267. case E1000_TDT(0):
  268. for (n = 0; n < 4; n++)
  269. regs[n] = rd32(E1000_TDT(n));
  270. break;
  271. case E1000_TXDCTL(0):
  272. for (n = 0; n < 4; n++)
  273. regs[n] = rd32(E1000_TXDCTL(n));
  274. break;
  275. default:
  276. printk(KERN_INFO "%-15s %08x\n",
  277. reginfo->name, rd32(reginfo->ofs));
  278. return;
  279. }
  280. snprintf(rname, 16, "%s%s", reginfo->name, "[0-3]");
  281. printk(KERN_INFO "%-15s ", rname);
  282. for (n = 0; n < 4; n++)
  283. printk(KERN_CONT "%08x ", regs[n]);
  284. printk(KERN_CONT "\n");
  285. }
  286. /*
  287. * igb_dump - Print registers, tx-rings and rx-rings
  288. */
  289. static void igb_dump(struct igb_adapter *adapter)
  290. {
  291. struct net_device *netdev = adapter->netdev;
  292. struct e1000_hw *hw = &adapter->hw;
  293. struct igb_reg_info *reginfo;
  294. int n = 0;
  295. struct igb_ring *tx_ring;
  296. union e1000_adv_tx_desc *tx_desc;
  297. struct my_u0 { u64 a; u64 b; } *u0;
  298. struct igb_buffer *buffer_info;
  299. struct igb_ring *rx_ring;
  300. union e1000_adv_rx_desc *rx_desc;
  301. u32 staterr;
  302. int i = 0;
  303. if (!netif_msg_hw(adapter))
  304. return;
  305. /* Print netdevice Info */
  306. if (netdev) {
  307. dev_info(&adapter->pdev->dev, "Net device Info\n");
  308. printk(KERN_INFO "Device Name state "
  309. "trans_start last_rx\n");
  310. printk(KERN_INFO "%-15s %016lX %016lX %016lX\n",
  311. netdev->name,
  312. netdev->state,
  313. netdev->trans_start,
  314. netdev->last_rx);
  315. }
  316. /* Print Registers */
  317. dev_info(&adapter->pdev->dev, "Register Dump\n");
  318. printk(KERN_INFO " Register Name Value\n");
  319. for (reginfo = (struct igb_reg_info *)igb_reg_info_tbl;
  320. reginfo->name; reginfo++) {
  321. igb_regdump(hw, reginfo);
  322. }
  323. /* Print TX Ring Summary */
  324. if (!netdev || !netif_running(netdev))
  325. goto exit;
  326. dev_info(&adapter->pdev->dev, "TX Rings Summary\n");
  327. printk(KERN_INFO "Queue [NTU] [NTC] [bi(ntc)->dma ]"
  328. " leng ntw timestamp\n");
  329. for (n = 0; n < adapter->num_tx_queues; n++) {
  330. tx_ring = adapter->tx_ring[n];
  331. buffer_info = &tx_ring->buffer_info[tx_ring->next_to_clean];
  332. printk(KERN_INFO " %5d %5X %5X %016llX %04X %3X %016llX\n",
  333. n, tx_ring->next_to_use, tx_ring->next_to_clean,
  334. (u64)buffer_info->dma,
  335. buffer_info->length,
  336. buffer_info->next_to_watch,
  337. (u64)buffer_info->time_stamp);
  338. }
  339. /* Print TX Rings */
  340. if (!netif_msg_tx_done(adapter))
  341. goto rx_ring_summary;
  342. dev_info(&adapter->pdev->dev, "TX Rings Dump\n");
  343. /* Transmit Descriptor Formats
  344. *
  345. * Advanced Transmit Descriptor
  346. * +--------------------------------------------------------------+
  347. * 0 | Buffer Address [63:0] |
  348. * +--------------------------------------------------------------+
  349. * 8 | PAYLEN | PORTS |CC|IDX | STA | DCMD |DTYP|MAC|RSV| DTALEN |
  350. * +--------------------------------------------------------------+
  351. * 63 46 45 40 39 38 36 35 32 31 24 15 0
  352. */
  353. for (n = 0; n < adapter->num_tx_queues; n++) {
  354. tx_ring = adapter->tx_ring[n];
  355. printk(KERN_INFO "------------------------------------\n");
  356. printk(KERN_INFO "TX QUEUE INDEX = %d\n", tx_ring->queue_index);
  357. printk(KERN_INFO "------------------------------------\n");
  358. printk(KERN_INFO "T [desc] [address 63:0 ] "
  359. "[PlPOCIStDDM Ln] [bi->dma ] "
  360. "leng ntw timestamp bi->skb\n");
  361. for (i = 0; tx_ring->desc && (i < tx_ring->count); i++) {
  362. tx_desc = E1000_TX_DESC_ADV(*tx_ring, i);
  363. buffer_info = &tx_ring->buffer_info[i];
  364. u0 = (struct my_u0 *)tx_desc;
  365. printk(KERN_INFO "T [0x%03X] %016llX %016llX %016llX"
  366. " %04X %3X %016llX %p", i,
  367. le64_to_cpu(u0->a),
  368. le64_to_cpu(u0->b),
  369. (u64)buffer_info->dma,
  370. buffer_info->length,
  371. buffer_info->next_to_watch,
  372. (u64)buffer_info->time_stamp,
  373. buffer_info->skb);
  374. if (i == tx_ring->next_to_use &&
  375. i == tx_ring->next_to_clean)
  376. printk(KERN_CONT " NTC/U\n");
  377. else if (i == tx_ring->next_to_use)
  378. printk(KERN_CONT " NTU\n");
  379. else if (i == tx_ring->next_to_clean)
  380. printk(KERN_CONT " NTC\n");
  381. else
  382. printk(KERN_CONT "\n");
  383. if (netif_msg_pktdata(adapter) && buffer_info->dma != 0)
  384. print_hex_dump(KERN_INFO, "",
  385. DUMP_PREFIX_ADDRESS,
  386. 16, 1, phys_to_virt(buffer_info->dma),
  387. buffer_info->length, true);
  388. }
  389. }
  390. /* Print RX Rings Summary */
  391. rx_ring_summary:
  392. dev_info(&adapter->pdev->dev, "RX Rings Summary\n");
  393. printk(KERN_INFO "Queue [NTU] [NTC]\n");
  394. for (n = 0; n < adapter->num_rx_queues; n++) {
  395. rx_ring = adapter->rx_ring[n];
  396. printk(KERN_INFO " %5d %5X %5X\n", n,
  397. rx_ring->next_to_use, rx_ring->next_to_clean);
  398. }
  399. /* Print RX Rings */
  400. if (!netif_msg_rx_status(adapter))
  401. goto exit;
  402. dev_info(&adapter->pdev->dev, "RX Rings Dump\n");
  403. /* Advanced Receive Descriptor (Read) Format
  404. * 63 1 0
  405. * +-----------------------------------------------------+
  406. * 0 | Packet Buffer Address [63:1] |A0/NSE|
  407. * +----------------------------------------------+------+
  408. * 8 | Header Buffer Address [63:1] | DD |
  409. * +-----------------------------------------------------+
  410. *
  411. *
  412. * Advanced Receive Descriptor (Write-Back) Format
  413. *
  414. * 63 48 47 32 31 30 21 20 17 16 4 3 0
  415. * +------------------------------------------------------+
  416. * 0 | Packet IP |SPH| HDR_LEN | RSV|Packet| RSS |
  417. * | Checksum Ident | | | | Type | Type |
  418. * +------------------------------------------------------+
  419. * 8 | VLAN Tag | Length | Extended Error | Extended Status |
  420. * +------------------------------------------------------+
  421. * 63 48 47 32 31 20 19 0
  422. */
  423. for (n = 0; n < adapter->num_rx_queues; n++) {
  424. rx_ring = adapter->rx_ring[n];
  425. printk(KERN_INFO "------------------------------------\n");
  426. printk(KERN_INFO "RX QUEUE INDEX = %d\n", rx_ring->queue_index);
  427. printk(KERN_INFO "------------------------------------\n");
  428. printk(KERN_INFO "R [desc] [ PktBuf A0] "
  429. "[ HeadBuf DD] [bi->dma ] [bi->skb] "
  430. "<-- Adv Rx Read format\n");
  431. printk(KERN_INFO "RWB[desc] [PcsmIpSHl PtRs] "
  432. "[vl er S cks ln] ---------------- [bi->skb] "
  433. "<-- Adv Rx Write-Back format\n");
  434. for (i = 0; i < rx_ring->count; i++) {
  435. buffer_info = &rx_ring->buffer_info[i];
  436. rx_desc = E1000_RX_DESC_ADV(*rx_ring, i);
  437. u0 = (struct my_u0 *)rx_desc;
  438. staterr = le32_to_cpu(rx_desc->wb.upper.status_error);
  439. if (staterr & E1000_RXD_STAT_DD) {
  440. /* Descriptor Done */
  441. printk(KERN_INFO "RWB[0x%03X] %016llX "
  442. "%016llX ---------------- %p", i,
  443. le64_to_cpu(u0->a),
  444. le64_to_cpu(u0->b),
  445. buffer_info->skb);
  446. } else {
  447. printk(KERN_INFO "R [0x%03X] %016llX "
  448. "%016llX %016llX %p", i,
  449. le64_to_cpu(u0->a),
  450. le64_to_cpu(u0->b),
  451. (u64)buffer_info->dma,
  452. buffer_info->skb);
  453. if (netif_msg_pktdata(adapter)) {
  454. print_hex_dump(KERN_INFO, "",
  455. DUMP_PREFIX_ADDRESS,
  456. 16, 1,
  457. phys_to_virt(buffer_info->dma),
  458. rx_ring->rx_buffer_len, true);
  459. if (rx_ring->rx_buffer_len
  460. < IGB_RXBUFFER_1024)
  461. print_hex_dump(KERN_INFO, "",
  462. DUMP_PREFIX_ADDRESS,
  463. 16, 1,
  464. phys_to_virt(
  465. buffer_info->page_dma +
  466. buffer_info->page_offset),
  467. PAGE_SIZE/2, true);
  468. }
  469. }
  470. if (i == rx_ring->next_to_use)
  471. printk(KERN_CONT " NTU\n");
  472. else if (i == rx_ring->next_to_clean)
  473. printk(KERN_CONT " NTC\n");
  474. else
  475. printk(KERN_CONT "\n");
  476. }
  477. }
  478. exit:
  479. return;
  480. }
  481. /**
  482. * igb_read_clock - read raw cycle counter (to be used by time counter)
  483. */
  484. static cycle_t igb_read_clock(const struct cyclecounter *tc)
  485. {
  486. struct igb_adapter *adapter =
  487. container_of(tc, struct igb_adapter, cycles);
  488. struct e1000_hw *hw = &adapter->hw;
  489. u64 stamp = 0;
  490. int shift = 0;
  491. /*
  492. * The timestamp latches on lowest register read. For the 82580
  493. * the lowest register is SYSTIMR instead of SYSTIML. However we never
  494. * adjusted TIMINCA so SYSTIMR will just read as all 0s so ignore it.
  495. */
  496. if (hw->mac.type == e1000_82580) {
  497. stamp = rd32(E1000_SYSTIMR) >> 8;
  498. shift = IGB_82580_TSYNC_SHIFT;
  499. }
  500. stamp |= (u64)rd32(E1000_SYSTIML) << shift;
  501. stamp |= (u64)rd32(E1000_SYSTIMH) << (shift + 32);
  502. return stamp;
  503. }
  504. /**
  505. * igb_get_hw_dev - return device
  506. * used by hardware layer to print debugging information
  507. **/
  508. struct net_device *igb_get_hw_dev(struct e1000_hw *hw)
  509. {
  510. struct igb_adapter *adapter = hw->back;
  511. return adapter->netdev;
  512. }
  513. /**
  514. * igb_init_module - Driver Registration Routine
  515. *
  516. * igb_init_module is the first routine called when the driver is
  517. * loaded. All it does is register with the PCI subsystem.
  518. **/
  519. static int __init igb_init_module(void)
  520. {
  521. int ret;
  522. printk(KERN_INFO "%s - version %s\n",
  523. igb_driver_string, igb_driver_version);
  524. printk(KERN_INFO "%s\n", igb_copyright);
  525. #ifdef CONFIG_IGB_DCA
  526. dca_register_notify(&dca_notifier);
  527. #endif
  528. ret = pci_register_driver(&igb_driver);
  529. return ret;
  530. }
  531. module_init(igb_init_module);
  532. /**
  533. * igb_exit_module - Driver Exit Cleanup Routine
  534. *
  535. * igb_exit_module is called just before the driver is removed
  536. * from memory.
  537. **/
  538. static void __exit igb_exit_module(void)
  539. {
  540. #ifdef CONFIG_IGB_DCA
  541. dca_unregister_notify(&dca_notifier);
  542. #endif
  543. pci_unregister_driver(&igb_driver);
  544. }
  545. module_exit(igb_exit_module);
  546. #define Q_IDX_82576(i) (((i & 0x1) << 3) + (i >> 1))
  547. /**
  548. * igb_cache_ring_register - Descriptor ring to register mapping
  549. * @adapter: board private structure to initialize
  550. *
  551. * Once we know the feature-set enabled for the device, we'll cache
  552. * the register offset the descriptor ring is assigned to.
  553. **/
  554. static void igb_cache_ring_register(struct igb_adapter *adapter)
  555. {
  556. int i = 0, j = 0;
  557. u32 rbase_offset = adapter->vfs_allocated_count;
  558. switch (adapter->hw.mac.type) {
  559. case e1000_82576:
  560. /* The queues are allocated for virtualization such that VF 0
  561. * is allocated queues 0 and 8, VF 1 queues 1 and 9, etc.
  562. * In order to avoid collision we start at the first free queue
  563. * and continue consuming queues in the same sequence
  564. */
  565. if (adapter->vfs_allocated_count) {
  566. for (; i < adapter->rss_queues; i++)
  567. adapter->rx_ring[i]->reg_idx = rbase_offset +
  568. Q_IDX_82576(i);
  569. for (; j < adapter->rss_queues; j++)
  570. adapter->tx_ring[j]->reg_idx = rbase_offset +
  571. Q_IDX_82576(j);
  572. }
  573. case e1000_82575:
  574. case e1000_82580:
  575. case e1000_i350:
  576. default:
  577. for (; i < adapter->num_rx_queues; i++)
  578. adapter->rx_ring[i]->reg_idx = rbase_offset + i;
  579. for (; j < adapter->num_tx_queues; j++)
  580. adapter->tx_ring[j]->reg_idx = rbase_offset + j;
  581. break;
  582. }
  583. }
  584. static void igb_free_queues(struct igb_adapter *adapter)
  585. {
  586. int i;
  587. for (i = 0; i < adapter->num_tx_queues; i++) {
  588. kfree(adapter->tx_ring[i]);
  589. adapter->tx_ring[i] = NULL;
  590. }
  591. for (i = 0; i < adapter->num_rx_queues; i++) {
  592. kfree(adapter->rx_ring[i]);
  593. adapter->rx_ring[i] = NULL;
  594. }
  595. adapter->num_rx_queues = 0;
  596. adapter->num_tx_queues = 0;
  597. }
  598. /**
  599. * igb_alloc_queues - Allocate memory for all rings
  600. * @adapter: board private structure to initialize
  601. *
  602. * We allocate one ring per queue at run-time since we don't know the
  603. * number of queues at compile-time.
  604. **/
  605. static int igb_alloc_queues(struct igb_adapter *adapter)
  606. {
  607. struct igb_ring *ring;
  608. int i;
  609. for (i = 0; i < adapter->num_tx_queues; i++) {
  610. ring = kzalloc(sizeof(struct igb_ring), GFP_KERNEL);
  611. if (!ring)
  612. goto err;
  613. ring->count = adapter->tx_ring_count;
  614. ring->queue_index = i;
  615. ring->dev = &adapter->pdev->dev;
  616. ring->netdev = adapter->netdev;
  617. /* For 82575, context index must be unique per ring. */
  618. if (adapter->hw.mac.type == e1000_82575)
  619. ring->flags = IGB_RING_FLAG_TX_CTX_IDX;
  620. adapter->tx_ring[i] = ring;
  621. }
  622. for (i = 0; i < adapter->num_rx_queues; i++) {
  623. ring = kzalloc(sizeof(struct igb_ring), GFP_KERNEL);
  624. if (!ring)
  625. goto err;
  626. ring->count = adapter->rx_ring_count;
  627. ring->queue_index = i;
  628. ring->dev = &adapter->pdev->dev;
  629. ring->netdev = adapter->netdev;
  630. ring->rx_buffer_len = MAXIMUM_ETHERNET_VLAN_SIZE;
  631. ring->flags = IGB_RING_FLAG_RX_CSUM; /* enable rx checksum */
  632. /* set flag indicating ring supports SCTP checksum offload */
  633. if (adapter->hw.mac.type >= e1000_82576)
  634. ring->flags |= IGB_RING_FLAG_RX_SCTP_CSUM;
  635. adapter->rx_ring[i] = ring;
  636. }
  637. igb_cache_ring_register(adapter);
  638. return 0;
  639. err:
  640. igb_free_queues(adapter);
  641. return -ENOMEM;
  642. }
  643. #define IGB_N0_QUEUE -1
  644. static void igb_assign_vector(struct igb_q_vector *q_vector, int msix_vector)
  645. {
  646. u32 msixbm = 0;
  647. struct igb_adapter *adapter = q_vector->adapter;
  648. struct e1000_hw *hw = &adapter->hw;
  649. u32 ivar, index;
  650. int rx_queue = IGB_N0_QUEUE;
  651. int tx_queue = IGB_N0_QUEUE;
  652. if (q_vector->rx_ring)
  653. rx_queue = q_vector->rx_ring->reg_idx;
  654. if (q_vector->tx_ring)
  655. tx_queue = q_vector->tx_ring->reg_idx;
  656. switch (hw->mac.type) {
  657. case e1000_82575:
  658. /* The 82575 assigns vectors using a bitmask, which matches the
  659. bitmask for the EICR/EIMS/EIMC registers. To assign one
  660. or more queues to a vector, we write the appropriate bits
  661. into the MSIXBM register for that vector. */
  662. if (rx_queue > IGB_N0_QUEUE)
  663. msixbm = E1000_EICR_RX_QUEUE0 << rx_queue;
  664. if (tx_queue > IGB_N0_QUEUE)
  665. msixbm |= E1000_EICR_TX_QUEUE0 << tx_queue;
  666. if (!adapter->msix_entries && msix_vector == 0)
  667. msixbm |= E1000_EIMS_OTHER;
  668. array_wr32(E1000_MSIXBM(0), msix_vector, msixbm);
  669. q_vector->eims_value = msixbm;
  670. break;
  671. case e1000_82576:
  672. /* 82576 uses a table-based method for assigning vectors.
  673. Each queue has a single entry in the table to which we write
  674. a vector number along with a "valid" bit. Sadly, the layout
  675. of the table is somewhat counterintuitive. */
  676. if (rx_queue > IGB_N0_QUEUE) {
  677. index = (rx_queue & 0x7);
  678. ivar = array_rd32(E1000_IVAR0, index);
  679. if (rx_queue < 8) {
  680. /* vector goes into low byte of register */
  681. ivar = ivar & 0xFFFFFF00;
  682. ivar |= msix_vector | E1000_IVAR_VALID;
  683. } else {
  684. /* vector goes into third byte of register */
  685. ivar = ivar & 0xFF00FFFF;
  686. ivar |= (msix_vector | E1000_IVAR_VALID) << 16;
  687. }
  688. array_wr32(E1000_IVAR0, index, ivar);
  689. }
  690. if (tx_queue > IGB_N0_QUEUE) {
  691. index = (tx_queue & 0x7);
  692. ivar = array_rd32(E1000_IVAR0, index);
  693. if (tx_queue < 8) {
  694. /* vector goes into second byte of register */
  695. ivar = ivar & 0xFFFF00FF;
  696. ivar |= (msix_vector | E1000_IVAR_VALID) << 8;
  697. } else {
  698. /* vector goes into high byte of register */
  699. ivar = ivar & 0x00FFFFFF;
  700. ivar |= (msix_vector | E1000_IVAR_VALID) << 24;
  701. }
  702. array_wr32(E1000_IVAR0, index, ivar);
  703. }
  704. q_vector->eims_value = 1 << msix_vector;
  705. break;
  706. case e1000_82580:
  707. case e1000_i350:
  708. /* 82580 uses the same table-based approach as 82576 but has fewer
  709. entries as a result we carry over for queues greater than 4. */
  710. if (rx_queue > IGB_N0_QUEUE) {
  711. index = (rx_queue >> 1);
  712. ivar = array_rd32(E1000_IVAR0, index);
  713. if (rx_queue & 0x1) {
  714. /* vector goes into third byte of register */
  715. ivar = ivar & 0xFF00FFFF;
  716. ivar |= (msix_vector | E1000_IVAR_VALID) << 16;
  717. } else {
  718. /* vector goes into low byte of register */
  719. ivar = ivar & 0xFFFFFF00;
  720. ivar |= msix_vector | E1000_IVAR_VALID;
  721. }
  722. array_wr32(E1000_IVAR0, index, ivar);
  723. }
  724. if (tx_queue > IGB_N0_QUEUE) {
  725. index = (tx_queue >> 1);
  726. ivar = array_rd32(E1000_IVAR0, index);
  727. if (tx_queue & 0x1) {
  728. /* vector goes into high byte of register */
  729. ivar = ivar & 0x00FFFFFF;
  730. ivar |= (msix_vector | E1000_IVAR_VALID) << 24;
  731. } else {
  732. /* vector goes into second byte of register */
  733. ivar = ivar & 0xFFFF00FF;
  734. ivar |= (msix_vector | E1000_IVAR_VALID) << 8;
  735. }
  736. array_wr32(E1000_IVAR0, index, ivar);
  737. }
  738. q_vector->eims_value = 1 << msix_vector;
  739. break;
  740. default:
  741. BUG();
  742. break;
  743. }
  744. /* add q_vector eims value to global eims_enable_mask */
  745. adapter->eims_enable_mask |= q_vector->eims_value;
  746. /* configure q_vector to set itr on first interrupt */
  747. q_vector->set_itr = 1;
  748. }
  749. /**
  750. * igb_configure_msix - Configure MSI-X hardware
  751. *
  752. * igb_configure_msix sets up the hardware to properly
  753. * generate MSI-X interrupts.
  754. **/
  755. static void igb_configure_msix(struct igb_adapter *adapter)
  756. {
  757. u32 tmp;
  758. int i, vector = 0;
  759. struct e1000_hw *hw = &adapter->hw;
  760. adapter->eims_enable_mask = 0;
  761. /* set vector for other causes, i.e. link changes */
  762. switch (hw->mac.type) {
  763. case e1000_82575:
  764. tmp = rd32(E1000_CTRL_EXT);
  765. /* enable MSI-X PBA support*/
  766. tmp |= E1000_CTRL_EXT_PBA_CLR;
  767. /* Auto-Mask interrupts upon ICR read. */
  768. tmp |= E1000_CTRL_EXT_EIAME;
  769. tmp |= E1000_CTRL_EXT_IRCA;
  770. wr32(E1000_CTRL_EXT, tmp);
  771. /* enable msix_other interrupt */
  772. array_wr32(E1000_MSIXBM(0), vector++,
  773. E1000_EIMS_OTHER);
  774. adapter->eims_other = E1000_EIMS_OTHER;
  775. break;
  776. case e1000_82576:
  777. case e1000_82580:
  778. case e1000_i350:
  779. /* Turn on MSI-X capability first, or our settings
  780. * won't stick. And it will take days to debug. */
  781. wr32(E1000_GPIE, E1000_GPIE_MSIX_MODE |
  782. E1000_GPIE_PBA | E1000_GPIE_EIAME |
  783. E1000_GPIE_NSICR);
  784. /* enable msix_other interrupt */
  785. adapter->eims_other = 1 << vector;
  786. tmp = (vector++ | E1000_IVAR_VALID) << 8;
  787. wr32(E1000_IVAR_MISC, tmp);
  788. break;
  789. default:
  790. /* do nothing, since nothing else supports MSI-X */
  791. break;
  792. } /* switch (hw->mac.type) */
  793. adapter->eims_enable_mask |= adapter->eims_other;
  794. for (i = 0; i < adapter->num_q_vectors; i++)
  795. igb_assign_vector(adapter->q_vector[i], vector++);
  796. wrfl();
  797. }
  798. /**
  799. * igb_request_msix - Initialize MSI-X interrupts
  800. *
  801. * igb_request_msix allocates MSI-X vectors and requests interrupts from the
  802. * kernel.
  803. **/
  804. static int igb_request_msix(struct igb_adapter *adapter)
  805. {
  806. struct net_device *netdev = adapter->netdev;
  807. struct e1000_hw *hw = &adapter->hw;
  808. int i, err = 0, vector = 0;
  809. err = request_irq(adapter->msix_entries[vector].vector,
  810. igb_msix_other, 0, netdev->name, adapter);
  811. if (err)
  812. goto out;
  813. vector++;
  814. for (i = 0; i < adapter->num_q_vectors; i++) {
  815. struct igb_q_vector *q_vector = adapter->q_vector[i];
  816. q_vector->itr_register = hw->hw_addr + E1000_EITR(vector);
  817. if (q_vector->rx_ring && q_vector->tx_ring)
  818. sprintf(q_vector->name, "%s-TxRx-%u", netdev->name,
  819. q_vector->rx_ring->queue_index);
  820. else if (q_vector->tx_ring)
  821. sprintf(q_vector->name, "%s-tx-%u", netdev->name,
  822. q_vector->tx_ring->queue_index);
  823. else if (q_vector->rx_ring)
  824. sprintf(q_vector->name, "%s-rx-%u", netdev->name,
  825. q_vector->rx_ring->queue_index);
  826. else
  827. sprintf(q_vector->name, "%s-unused", netdev->name);
  828. err = request_irq(adapter->msix_entries[vector].vector,
  829. igb_msix_ring, 0, q_vector->name,
  830. q_vector);
  831. if (err)
  832. goto out;
  833. vector++;
  834. }
  835. igb_configure_msix(adapter);
  836. return 0;
  837. out:
  838. return err;
  839. }
  840. static void igb_reset_interrupt_capability(struct igb_adapter *adapter)
  841. {
  842. if (adapter->msix_entries) {
  843. pci_disable_msix(adapter->pdev);
  844. kfree(adapter->msix_entries);
  845. adapter->msix_entries = NULL;
  846. } else if (adapter->flags & IGB_FLAG_HAS_MSI) {
  847. pci_disable_msi(adapter->pdev);
  848. }
  849. }
  850. /**
  851. * igb_free_q_vectors - Free memory allocated for interrupt vectors
  852. * @adapter: board private structure to initialize
  853. *
  854. * This function frees the memory allocated to the q_vectors. In addition if
  855. * NAPI is enabled it will delete any references to the NAPI struct prior
  856. * to freeing the q_vector.
  857. **/
  858. static void igb_free_q_vectors(struct igb_adapter *adapter)
  859. {
  860. int v_idx;
  861. for (v_idx = 0; v_idx < adapter->num_q_vectors; v_idx++) {
  862. struct igb_q_vector *q_vector = adapter->q_vector[v_idx];
  863. adapter->q_vector[v_idx] = NULL;
  864. if (!q_vector)
  865. continue;
  866. netif_napi_del(&q_vector->napi);
  867. kfree(q_vector);
  868. }
  869. adapter->num_q_vectors = 0;
  870. }
  871. /**
  872. * igb_clear_interrupt_scheme - reset the device to a state of no interrupts
  873. *
  874. * This function resets the device so that it has 0 rx queues, tx queues, and
  875. * MSI-X interrupts allocated.
  876. */
  877. static void igb_clear_interrupt_scheme(struct igb_adapter *adapter)
  878. {
  879. igb_free_queues(adapter);
  880. igb_free_q_vectors(adapter);
  881. igb_reset_interrupt_capability(adapter);
  882. }
  883. /**
  884. * igb_set_interrupt_capability - set MSI or MSI-X if supported
  885. *
  886. * Attempt to configure interrupts using the best available
  887. * capabilities of the hardware and kernel.
  888. **/
  889. static void igb_set_interrupt_capability(struct igb_adapter *adapter)
  890. {
  891. int err;
  892. int numvecs, i;
  893. /* Number of supported queues. */
  894. adapter->num_rx_queues = adapter->rss_queues;
  895. adapter->num_tx_queues = adapter->rss_queues;
  896. /* start with one vector for every rx queue */
  897. numvecs = adapter->num_rx_queues;
  898. /* if tx handler is separate add 1 for every tx queue */
  899. if (!(adapter->flags & IGB_FLAG_QUEUE_PAIRS))
  900. numvecs += adapter->num_tx_queues;
  901. /* store the number of vectors reserved for queues */
  902. adapter->num_q_vectors = numvecs;
  903. /* add 1 vector for link status interrupts */
  904. numvecs++;
  905. adapter->msix_entries = kcalloc(numvecs, sizeof(struct msix_entry),
  906. GFP_KERNEL);
  907. if (!adapter->msix_entries)
  908. goto msi_only;
  909. for (i = 0; i < numvecs; i++)
  910. adapter->msix_entries[i].entry = i;
  911. err = pci_enable_msix(adapter->pdev,
  912. adapter->msix_entries,
  913. numvecs);
  914. if (err == 0)
  915. goto out;
  916. igb_reset_interrupt_capability(adapter);
  917. /* If we can't do MSI-X, try MSI */
  918. msi_only:
  919. #ifdef CONFIG_PCI_IOV
  920. /* disable SR-IOV for non MSI-X configurations */
  921. if (adapter->vf_data) {
  922. struct e1000_hw *hw = &adapter->hw;
  923. /* disable iov and allow time for transactions to clear */
  924. pci_disable_sriov(adapter->pdev);
  925. msleep(500);
  926. kfree(adapter->vf_data);
  927. adapter->vf_data = NULL;
  928. wr32(E1000_IOVCTL, E1000_IOVCTL_REUSE_VFQ);
  929. msleep(100);
  930. dev_info(&adapter->pdev->dev, "IOV Disabled\n");
  931. }
  932. #endif
  933. adapter->vfs_allocated_count = 0;
  934. adapter->rss_queues = 1;
  935. adapter->flags |= IGB_FLAG_QUEUE_PAIRS;
  936. adapter->num_rx_queues = 1;
  937. adapter->num_tx_queues = 1;
  938. adapter->num_q_vectors = 1;
  939. if (!pci_enable_msi(adapter->pdev))
  940. adapter->flags |= IGB_FLAG_HAS_MSI;
  941. out:
  942. /* Notify the stack of the (possibly) reduced Tx Queue count. */
  943. adapter->netdev->real_num_tx_queues = adapter->num_tx_queues;
  944. }
  945. /**
  946. * igb_alloc_q_vectors - Allocate memory for interrupt vectors
  947. * @adapter: board private structure to initialize
  948. *
  949. * We allocate one q_vector per queue interrupt. If allocation fails we
  950. * return -ENOMEM.
  951. **/
  952. static int igb_alloc_q_vectors(struct igb_adapter *adapter)
  953. {
  954. struct igb_q_vector *q_vector;
  955. struct e1000_hw *hw = &adapter->hw;
  956. int v_idx;
  957. for (v_idx = 0; v_idx < adapter->num_q_vectors; v_idx++) {
  958. q_vector = kzalloc(sizeof(struct igb_q_vector), GFP_KERNEL);
  959. if (!q_vector)
  960. goto err_out;
  961. q_vector->adapter = adapter;
  962. q_vector->itr_register = hw->hw_addr + E1000_EITR(0);
  963. q_vector->itr_val = IGB_START_ITR;
  964. netif_napi_add(adapter->netdev, &q_vector->napi, igb_poll, 64);
  965. adapter->q_vector[v_idx] = q_vector;
  966. }
  967. return 0;
  968. err_out:
  969. igb_free_q_vectors(adapter);
  970. return -ENOMEM;
  971. }
  972. static void igb_map_rx_ring_to_vector(struct igb_adapter *adapter,
  973. int ring_idx, int v_idx)
  974. {
  975. struct igb_q_vector *q_vector = adapter->q_vector[v_idx];
  976. q_vector->rx_ring = adapter->rx_ring[ring_idx];
  977. q_vector->rx_ring->q_vector = q_vector;
  978. q_vector->itr_val = adapter->rx_itr_setting;
  979. if (q_vector->itr_val && q_vector->itr_val <= 3)
  980. q_vector->itr_val = IGB_START_ITR;
  981. }
  982. static void igb_map_tx_ring_to_vector(struct igb_adapter *adapter,
  983. int ring_idx, int v_idx)
  984. {
  985. struct igb_q_vector *q_vector = adapter->q_vector[v_idx];
  986. q_vector->tx_ring = adapter->tx_ring[ring_idx];
  987. q_vector->tx_ring->q_vector = q_vector;
  988. q_vector->itr_val = adapter->tx_itr_setting;
  989. if (q_vector->itr_val && q_vector->itr_val <= 3)
  990. q_vector->itr_val = IGB_START_ITR;
  991. }
  992. /**
  993. * igb_map_ring_to_vector - maps allocated queues to vectors
  994. *
  995. * This function maps the recently allocated queues to vectors.
  996. **/
  997. static int igb_map_ring_to_vector(struct igb_adapter *adapter)
  998. {
  999. int i;
  1000. int v_idx = 0;
  1001. if ((adapter->num_q_vectors < adapter->num_rx_queues) ||
  1002. (adapter->num_q_vectors < adapter->num_tx_queues))
  1003. return -ENOMEM;
  1004. if (adapter->num_q_vectors >=
  1005. (adapter->num_rx_queues + adapter->num_tx_queues)) {
  1006. for (i = 0; i < adapter->num_rx_queues; i++)
  1007. igb_map_rx_ring_to_vector(adapter, i, v_idx++);
  1008. for (i = 0; i < adapter->num_tx_queues; i++)
  1009. igb_map_tx_ring_to_vector(adapter, i, v_idx++);
  1010. } else {
  1011. for (i = 0; i < adapter->num_rx_queues; i++) {
  1012. if (i < adapter->num_tx_queues)
  1013. igb_map_tx_ring_to_vector(adapter, i, v_idx);
  1014. igb_map_rx_ring_to_vector(adapter, i, v_idx++);
  1015. }
  1016. for (; i < adapter->num_tx_queues; i++)
  1017. igb_map_tx_ring_to_vector(adapter, i, v_idx++);
  1018. }
  1019. return 0;
  1020. }
  1021. /**
  1022. * igb_init_interrupt_scheme - initialize interrupts, allocate queues/vectors
  1023. *
  1024. * This function initializes the interrupts and allocates all of the queues.
  1025. **/
  1026. static int igb_init_interrupt_scheme(struct igb_adapter *adapter)
  1027. {
  1028. struct pci_dev *pdev = adapter->pdev;
  1029. int err;
  1030. igb_set_interrupt_capability(adapter);
  1031. err = igb_alloc_q_vectors(adapter);
  1032. if (err) {
  1033. dev_err(&pdev->dev, "Unable to allocate memory for vectors\n");
  1034. goto err_alloc_q_vectors;
  1035. }
  1036. err = igb_alloc_queues(adapter);
  1037. if (err) {
  1038. dev_err(&pdev->dev, "Unable to allocate memory for queues\n");
  1039. goto err_alloc_queues;
  1040. }
  1041. err = igb_map_ring_to_vector(adapter);
  1042. if (err) {
  1043. dev_err(&pdev->dev, "Invalid q_vector to ring mapping\n");
  1044. goto err_map_queues;
  1045. }
  1046. return 0;
  1047. err_map_queues:
  1048. igb_free_queues(adapter);
  1049. err_alloc_queues:
  1050. igb_free_q_vectors(adapter);
  1051. err_alloc_q_vectors:
  1052. igb_reset_interrupt_capability(adapter);
  1053. return err;
  1054. }
  1055. /**
  1056. * igb_request_irq - initialize interrupts
  1057. *
  1058. * Attempts to configure interrupts using the best available
  1059. * capabilities of the hardware and kernel.
  1060. **/
  1061. static int igb_request_irq(struct igb_adapter *adapter)
  1062. {
  1063. struct net_device *netdev = adapter->netdev;
  1064. struct pci_dev *pdev = adapter->pdev;
  1065. int err = 0;
  1066. if (adapter->msix_entries) {
  1067. err = igb_request_msix(adapter);
  1068. if (!err)
  1069. goto request_done;
  1070. /* fall back to MSI */
  1071. igb_clear_interrupt_scheme(adapter);
  1072. if (!pci_enable_msi(adapter->pdev))
  1073. adapter->flags |= IGB_FLAG_HAS_MSI;
  1074. igb_free_all_tx_resources(adapter);
  1075. igb_free_all_rx_resources(adapter);
  1076. adapter->num_tx_queues = 1;
  1077. adapter->num_rx_queues = 1;
  1078. adapter->num_q_vectors = 1;
  1079. err = igb_alloc_q_vectors(adapter);
  1080. if (err) {
  1081. dev_err(&pdev->dev,
  1082. "Unable to allocate memory for vectors\n");
  1083. goto request_done;
  1084. }
  1085. err = igb_alloc_queues(adapter);
  1086. if (err) {
  1087. dev_err(&pdev->dev,
  1088. "Unable to allocate memory for queues\n");
  1089. igb_free_q_vectors(adapter);
  1090. goto request_done;
  1091. }
  1092. igb_setup_all_tx_resources(adapter);
  1093. igb_setup_all_rx_resources(adapter);
  1094. } else {
  1095. igb_assign_vector(adapter->q_vector[0], 0);
  1096. }
  1097. if (adapter->flags & IGB_FLAG_HAS_MSI) {
  1098. err = request_irq(adapter->pdev->irq, igb_intr_msi, 0,
  1099. netdev->name, adapter);
  1100. if (!err)
  1101. goto request_done;
  1102. /* fall back to legacy interrupts */
  1103. igb_reset_interrupt_capability(adapter);
  1104. adapter->flags &= ~IGB_FLAG_HAS_MSI;
  1105. }
  1106. err = request_irq(adapter->pdev->irq, igb_intr, IRQF_SHARED,
  1107. netdev->name, adapter);
  1108. if (err)
  1109. dev_err(&adapter->pdev->dev, "Error %d getting interrupt\n",
  1110. err);
  1111. request_done:
  1112. return err;
  1113. }
  1114. static void igb_free_irq(struct igb_adapter *adapter)
  1115. {
  1116. if (adapter->msix_entries) {
  1117. int vector = 0, i;
  1118. free_irq(adapter->msix_entries[vector++].vector, adapter);
  1119. for (i = 0; i < adapter->num_q_vectors; i++) {
  1120. struct igb_q_vector *q_vector = adapter->q_vector[i];
  1121. free_irq(adapter->msix_entries[vector++].vector,
  1122. q_vector);
  1123. }
  1124. } else {
  1125. free_irq(adapter->pdev->irq, adapter);
  1126. }
  1127. }
  1128. /**
  1129. * igb_irq_disable - Mask off interrupt generation on the NIC
  1130. * @adapter: board private structure
  1131. **/
  1132. static void igb_irq_disable(struct igb_adapter *adapter)
  1133. {
  1134. struct e1000_hw *hw = &adapter->hw;
  1135. /*
  1136. * we need to be careful when disabling interrupts. The VFs are also
  1137. * mapped into these registers and so clearing the bits can cause
  1138. * issues on the VF drivers so we only need to clear what we set
  1139. */
  1140. if (adapter->msix_entries) {
  1141. u32 regval = rd32(E1000_EIAM);
  1142. wr32(E1000_EIAM, regval & ~adapter->eims_enable_mask);
  1143. wr32(E1000_EIMC, adapter->eims_enable_mask);
  1144. regval = rd32(E1000_EIAC);
  1145. wr32(E1000_EIAC, regval & ~adapter->eims_enable_mask);
  1146. }
  1147. wr32(E1000_IAM, 0);
  1148. wr32(E1000_IMC, ~0);
  1149. wrfl();
  1150. synchronize_irq(adapter->pdev->irq);
  1151. }
  1152. /**
  1153. * igb_irq_enable - Enable default interrupt generation settings
  1154. * @adapter: board private structure
  1155. **/
  1156. static void igb_irq_enable(struct igb_adapter *adapter)
  1157. {
  1158. struct e1000_hw *hw = &adapter->hw;
  1159. if (adapter->msix_entries) {
  1160. u32 ims = E1000_IMS_LSC | E1000_IMS_DOUTSYNC;
  1161. u32 regval = rd32(E1000_EIAC);
  1162. wr32(E1000_EIAC, regval | adapter->eims_enable_mask);
  1163. regval = rd32(E1000_EIAM);
  1164. wr32(E1000_EIAM, regval | adapter->eims_enable_mask);
  1165. wr32(E1000_EIMS, adapter->eims_enable_mask);
  1166. if (adapter->vfs_allocated_count) {
  1167. wr32(E1000_MBVFIMR, 0xFF);
  1168. ims |= E1000_IMS_VMMB;
  1169. }
  1170. if (adapter->hw.mac.type == e1000_82580)
  1171. ims |= E1000_IMS_DRSTA;
  1172. wr32(E1000_IMS, ims);
  1173. } else {
  1174. wr32(E1000_IMS, IMS_ENABLE_MASK |
  1175. E1000_IMS_DRSTA);
  1176. wr32(E1000_IAM, IMS_ENABLE_MASK |
  1177. E1000_IMS_DRSTA);
  1178. }
  1179. }
  1180. static void igb_update_mng_vlan(struct igb_adapter *adapter)
  1181. {
  1182. struct e1000_hw *hw = &adapter->hw;
  1183. u16 vid = adapter->hw.mng_cookie.vlan_id;
  1184. u16 old_vid = adapter->mng_vlan_id;
  1185. if (hw->mng_cookie.status & E1000_MNG_DHCP_COOKIE_STATUS_VLAN) {
  1186. /* add VID to filter table */
  1187. igb_vfta_set(hw, vid, true);
  1188. adapter->mng_vlan_id = vid;
  1189. } else {
  1190. adapter->mng_vlan_id = IGB_MNG_VLAN_NONE;
  1191. }
  1192. if ((old_vid != (u16)IGB_MNG_VLAN_NONE) &&
  1193. (vid != old_vid) &&
  1194. !vlan_group_get_device(adapter->vlgrp, old_vid)) {
  1195. /* remove VID from filter table */
  1196. igb_vfta_set(hw, old_vid, false);
  1197. }
  1198. }
  1199. /**
  1200. * igb_release_hw_control - release control of the h/w to f/w
  1201. * @adapter: address of board private structure
  1202. *
  1203. * igb_release_hw_control resets CTRL_EXT:DRV_LOAD bit.
  1204. * For ASF and Pass Through versions of f/w this means that the
  1205. * driver is no longer loaded.
  1206. *
  1207. **/
  1208. static void igb_release_hw_control(struct igb_adapter *adapter)
  1209. {
  1210. struct e1000_hw *hw = &adapter->hw;
  1211. u32 ctrl_ext;
  1212. /* Let firmware take over control of h/w */
  1213. ctrl_ext = rd32(E1000_CTRL_EXT);
  1214. wr32(E1000_CTRL_EXT,
  1215. ctrl_ext & ~E1000_CTRL_EXT_DRV_LOAD);
  1216. }
  1217. /**
  1218. * igb_get_hw_control - get control of the h/w from f/w
  1219. * @adapter: address of board private structure
  1220. *
  1221. * igb_get_hw_control sets CTRL_EXT:DRV_LOAD bit.
  1222. * For ASF and Pass Through versions of f/w this means that
  1223. * the driver is loaded.
  1224. *
  1225. **/
  1226. static void igb_get_hw_control(struct igb_adapter *adapter)
  1227. {
  1228. struct e1000_hw *hw = &adapter->hw;
  1229. u32 ctrl_ext;
  1230. /* Let firmware know the driver has taken over */
  1231. ctrl_ext = rd32(E1000_CTRL_EXT);
  1232. wr32(E1000_CTRL_EXT,
  1233. ctrl_ext | E1000_CTRL_EXT_DRV_LOAD);
  1234. }
  1235. /**
  1236. * igb_configure - configure the hardware for RX and TX
  1237. * @adapter: private board structure
  1238. **/
  1239. static void igb_configure(struct igb_adapter *adapter)
  1240. {
  1241. struct net_device *netdev = adapter->netdev;
  1242. int i;
  1243. igb_get_hw_control(adapter);
  1244. igb_set_rx_mode(netdev);
  1245. igb_restore_vlan(adapter);
  1246. igb_setup_tctl(adapter);
  1247. igb_setup_mrqc(adapter);
  1248. igb_setup_rctl(adapter);
  1249. igb_configure_tx(adapter);
  1250. igb_configure_rx(adapter);
  1251. igb_rx_fifo_flush_82575(&adapter->hw);
  1252. /* call igb_desc_unused which always leaves
  1253. * at least 1 descriptor unused to make sure
  1254. * next_to_use != next_to_clean */
  1255. for (i = 0; i < adapter->num_rx_queues; i++) {
  1256. struct igb_ring *ring = adapter->rx_ring[i];
  1257. igb_alloc_rx_buffers_adv(ring, igb_desc_unused(ring));
  1258. }
  1259. }
  1260. /**
  1261. * igb_power_up_link - Power up the phy/serdes link
  1262. * @adapter: address of board private structure
  1263. **/
  1264. void igb_power_up_link(struct igb_adapter *adapter)
  1265. {
  1266. if (adapter->hw.phy.media_type == e1000_media_type_copper)
  1267. igb_power_up_phy_copper(&adapter->hw);
  1268. else
  1269. igb_power_up_serdes_link_82575(&adapter->hw);
  1270. }
  1271. /**
  1272. * igb_power_down_link - Power down the phy/serdes link
  1273. * @adapter: address of board private structure
  1274. */
  1275. static void igb_power_down_link(struct igb_adapter *adapter)
  1276. {
  1277. if (adapter->hw.phy.media_type == e1000_media_type_copper)
  1278. igb_power_down_phy_copper_82575(&adapter->hw);
  1279. else
  1280. igb_shutdown_serdes_link_82575(&adapter->hw);
  1281. }
  1282. /**
  1283. * igb_up - Open the interface and prepare it to handle traffic
  1284. * @adapter: board private structure
  1285. **/
  1286. int igb_up(struct igb_adapter *adapter)
  1287. {
  1288. struct e1000_hw *hw = &adapter->hw;
  1289. int i;
  1290. /* hardware has been reset, we need to reload some things */
  1291. igb_configure(adapter);
  1292. clear_bit(__IGB_DOWN, &adapter->state);
  1293. for (i = 0; i < adapter->num_q_vectors; i++) {
  1294. struct igb_q_vector *q_vector = adapter->q_vector[i];
  1295. napi_enable(&q_vector->napi);
  1296. }
  1297. if (adapter->msix_entries)
  1298. igb_configure_msix(adapter);
  1299. else
  1300. igb_assign_vector(adapter->q_vector[0], 0);
  1301. /* Clear any pending interrupts. */
  1302. rd32(E1000_ICR);
  1303. igb_irq_enable(adapter);
  1304. /* notify VFs that reset has been completed */
  1305. if (adapter->vfs_allocated_count) {
  1306. u32 reg_data = rd32(E1000_CTRL_EXT);
  1307. reg_data |= E1000_CTRL_EXT_PFRSTD;
  1308. wr32(E1000_CTRL_EXT, reg_data);
  1309. }
  1310. netif_tx_start_all_queues(adapter->netdev);
  1311. /* start the watchdog. */
  1312. hw->mac.get_link_status = 1;
  1313. schedule_work(&adapter->watchdog_task);
  1314. return 0;
  1315. }
  1316. void igb_down(struct igb_adapter *adapter)
  1317. {
  1318. struct net_device *netdev = adapter->netdev;
  1319. struct e1000_hw *hw = &adapter->hw;
  1320. u32 tctl, rctl;
  1321. int i;
  1322. /* signal that we're down so the interrupt handler does not
  1323. * reschedule our watchdog timer */
  1324. set_bit(__IGB_DOWN, &adapter->state);
  1325. /* disable receives in the hardware */
  1326. rctl = rd32(E1000_RCTL);
  1327. wr32(E1000_RCTL, rctl & ~E1000_RCTL_EN);
  1328. /* flush and sleep below */
  1329. netif_tx_stop_all_queues(netdev);
  1330. /* disable transmits in the hardware */
  1331. tctl = rd32(E1000_TCTL);
  1332. tctl &= ~E1000_TCTL_EN;
  1333. wr32(E1000_TCTL, tctl);
  1334. /* flush both disables and wait for them to finish */
  1335. wrfl();
  1336. msleep(10);
  1337. for (i = 0; i < adapter->num_q_vectors; i++) {
  1338. struct igb_q_vector *q_vector = adapter->q_vector[i];
  1339. napi_disable(&q_vector->napi);
  1340. }
  1341. igb_irq_disable(adapter);
  1342. del_timer_sync(&adapter->watchdog_timer);
  1343. del_timer_sync(&adapter->phy_info_timer);
  1344. netif_carrier_off(netdev);
  1345. /* record the stats before reset*/
  1346. igb_update_stats(adapter);
  1347. adapter->link_speed = 0;
  1348. adapter->link_duplex = 0;
  1349. if (!pci_channel_offline(adapter->pdev))
  1350. igb_reset(adapter);
  1351. igb_clean_all_tx_rings(adapter);
  1352. igb_clean_all_rx_rings(adapter);
  1353. #ifdef CONFIG_IGB_DCA
  1354. /* since we reset the hardware DCA settings were cleared */
  1355. igb_setup_dca(adapter);
  1356. #endif
  1357. }
  1358. void igb_reinit_locked(struct igb_adapter *adapter)
  1359. {
  1360. WARN_ON(in_interrupt());
  1361. while (test_and_set_bit(__IGB_RESETTING, &adapter->state))
  1362. msleep(1);
  1363. igb_down(adapter);
  1364. igb_up(adapter);
  1365. clear_bit(__IGB_RESETTING, &adapter->state);
  1366. }
  1367. void igb_reset(struct igb_adapter *adapter)
  1368. {
  1369. struct pci_dev *pdev = adapter->pdev;
  1370. struct e1000_hw *hw = &adapter->hw;
  1371. struct e1000_mac_info *mac = &hw->mac;
  1372. struct e1000_fc_info *fc = &hw->fc;
  1373. u32 pba = 0, tx_space, min_tx_space, min_rx_space;
  1374. u16 hwm;
  1375. /* Repartition Pba for greater than 9k mtu
  1376. * To take effect CTRL.RST is required.
  1377. */
  1378. switch (mac->type) {
  1379. case e1000_i350:
  1380. case e1000_82580:
  1381. pba = rd32(E1000_RXPBS);
  1382. pba = igb_rxpbs_adjust_82580(pba);
  1383. break;
  1384. case e1000_82576:
  1385. pba = rd32(E1000_RXPBS);
  1386. pba &= E1000_RXPBS_SIZE_MASK_82576;
  1387. break;
  1388. case e1000_82575:
  1389. default:
  1390. pba = E1000_PBA_34K;
  1391. break;
  1392. }
  1393. if ((adapter->max_frame_size > ETH_FRAME_LEN + ETH_FCS_LEN) &&
  1394. (mac->type < e1000_82576)) {
  1395. /* adjust PBA for jumbo frames */
  1396. wr32(E1000_PBA, pba);
  1397. /* To maintain wire speed transmits, the Tx FIFO should be
  1398. * large enough to accommodate two full transmit packets,
  1399. * rounded up to the next 1KB and expressed in KB. Likewise,
  1400. * the Rx FIFO should be large enough to accommodate at least
  1401. * one full receive packet and is similarly rounded up and
  1402. * expressed in KB. */
  1403. pba = rd32(E1000_PBA);
  1404. /* upper 16 bits has Tx packet buffer allocation size in KB */
  1405. tx_space = pba >> 16;
  1406. /* lower 16 bits has Rx packet buffer allocation size in KB */
  1407. pba &= 0xffff;
  1408. /* the tx fifo also stores 16 bytes of information about the tx
  1409. * but don't include ethernet FCS because hardware appends it */
  1410. min_tx_space = (adapter->max_frame_size +
  1411. sizeof(union e1000_adv_tx_desc) -
  1412. ETH_FCS_LEN) * 2;
  1413. min_tx_space = ALIGN(min_tx_space, 1024);
  1414. min_tx_space >>= 10;
  1415. /* software strips receive CRC, so leave room for it */
  1416. min_rx_space = adapter->max_frame_size;
  1417. min_rx_space = ALIGN(min_rx_space, 1024);
  1418. min_rx_space >>= 10;
  1419. /* If current Tx allocation is less than the min Tx FIFO size,
  1420. * and the min Tx FIFO size is less than the current Rx FIFO
  1421. * allocation, take space away from current Rx allocation */
  1422. if (tx_space < min_tx_space &&
  1423. ((min_tx_space - tx_space) < pba)) {
  1424. pba = pba - (min_tx_space - tx_space);
  1425. /* if short on rx space, rx wins and must trump tx
  1426. * adjustment */
  1427. if (pba < min_rx_space)
  1428. pba = min_rx_space;
  1429. }
  1430. wr32(E1000_PBA, pba);
  1431. }
  1432. /* flow control settings */
  1433. /* The high water mark must be low enough to fit one full frame
  1434. * (or the size used for early receive) above it in the Rx FIFO.
  1435. * Set it to the lower of:
  1436. * - 90% of the Rx FIFO size, or
  1437. * - the full Rx FIFO size minus one full frame */
  1438. hwm = min(((pba << 10) * 9 / 10),
  1439. ((pba << 10) - 2 * adapter->max_frame_size));
  1440. fc->high_water = hwm & 0xFFF0; /* 16-byte granularity */
  1441. fc->low_water = fc->high_water - 16;
  1442. fc->pause_time = 0xFFFF;
  1443. fc->send_xon = 1;
  1444. fc->current_mode = fc->requested_mode;
  1445. /* disable receive for all VFs and wait one second */
  1446. if (adapter->vfs_allocated_count) {
  1447. int i;
  1448. for (i = 0 ; i < adapter->vfs_allocated_count; i++)
  1449. adapter->vf_data[i].flags = 0;
  1450. /* ping all the active vfs to let them know we are going down */
  1451. igb_ping_all_vfs(adapter);
  1452. /* disable transmits and receives */
  1453. wr32(E1000_VFRE, 0);
  1454. wr32(E1000_VFTE, 0);
  1455. }
  1456. /* Allow time for pending master requests to run */
  1457. hw->mac.ops.reset_hw(hw);
  1458. wr32(E1000_WUC, 0);
  1459. if (hw->mac.ops.init_hw(hw))
  1460. dev_err(&pdev->dev, "Hardware Error\n");
  1461. if (hw->mac.type == e1000_82580) {
  1462. u32 reg = rd32(E1000_PCIEMISC);
  1463. wr32(E1000_PCIEMISC,
  1464. reg & ~E1000_PCIEMISC_LX_DECISION);
  1465. }
  1466. if (!netif_running(adapter->netdev))
  1467. igb_power_down_link(adapter);
  1468. igb_update_mng_vlan(adapter);
  1469. /* Enable h/w to recognize an 802.1Q VLAN Ethernet packet */
  1470. wr32(E1000_VET, ETHERNET_IEEE_VLAN_TYPE);
  1471. igb_get_phy_info(hw);
  1472. }
  1473. static const struct net_device_ops igb_netdev_ops = {
  1474. .ndo_open = igb_open,
  1475. .ndo_stop = igb_close,
  1476. .ndo_start_xmit = igb_xmit_frame_adv,
  1477. .ndo_get_stats = igb_get_stats,
  1478. .ndo_set_rx_mode = igb_set_rx_mode,
  1479. .ndo_set_multicast_list = igb_set_rx_mode,
  1480. .ndo_set_mac_address = igb_set_mac,
  1481. .ndo_change_mtu = igb_change_mtu,
  1482. .ndo_do_ioctl = igb_ioctl,
  1483. .ndo_tx_timeout = igb_tx_timeout,
  1484. .ndo_validate_addr = eth_validate_addr,
  1485. .ndo_vlan_rx_register = igb_vlan_rx_register,
  1486. .ndo_vlan_rx_add_vid = igb_vlan_rx_add_vid,
  1487. .ndo_vlan_rx_kill_vid = igb_vlan_rx_kill_vid,
  1488. .ndo_set_vf_mac = igb_ndo_set_vf_mac,
  1489. .ndo_set_vf_vlan = igb_ndo_set_vf_vlan,
  1490. .ndo_set_vf_tx_rate = igb_ndo_set_vf_bw,
  1491. .ndo_get_vf_config = igb_ndo_get_vf_config,
  1492. #ifdef CONFIG_NET_POLL_CONTROLLER
  1493. .ndo_poll_controller = igb_netpoll,
  1494. #endif
  1495. };
  1496. /**
  1497. * igb_probe - Device Initialization Routine
  1498. * @pdev: PCI device information struct
  1499. * @ent: entry in igb_pci_tbl
  1500. *
  1501. * Returns 0 on success, negative on failure
  1502. *
  1503. * igb_probe initializes an adapter identified by a pci_dev structure.
  1504. * The OS initialization, configuring of the adapter private structure,
  1505. * and a hardware reset occur.
  1506. **/
  1507. static int __devinit igb_probe(struct pci_dev *pdev,
  1508. const struct pci_device_id *ent)
  1509. {
  1510. struct net_device *netdev;
  1511. struct igb_adapter *adapter;
  1512. struct e1000_hw *hw;
  1513. u16 eeprom_data = 0;
  1514. static int global_quad_port_a; /* global quad port a indication */
  1515. const struct e1000_info *ei = igb_info_tbl[ent->driver_data];
  1516. unsigned long mmio_start, mmio_len;
  1517. int err, pci_using_dac;
  1518. u16 eeprom_apme_mask = IGB_EEPROM_APME;
  1519. u32 part_num;
  1520. /* Catch broken hardware that put the wrong VF device ID in
  1521. * the PCIe SR-IOV capability.
  1522. */
  1523. if (pdev->is_virtfn) {
  1524. WARN(1, KERN_ERR "%s (%hx:%hx) should not be a VF!\n",
  1525. pci_name(pdev), pdev->vendor, pdev->device);
  1526. return -EINVAL;
  1527. }
  1528. err = pci_enable_device_mem(pdev);
  1529. if (err)
  1530. return err;
  1531. pci_using_dac = 0;
  1532. err = dma_set_mask(&pdev->dev, DMA_BIT_MASK(64));
  1533. if (!err) {
  1534. err = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(64));
  1535. if (!err)
  1536. pci_using_dac = 1;
  1537. } else {
  1538. err = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
  1539. if (err) {
  1540. err = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
  1541. if (err) {
  1542. dev_err(&pdev->dev, "No usable DMA "
  1543. "configuration, aborting\n");
  1544. goto err_dma;
  1545. }
  1546. }
  1547. }
  1548. err = pci_request_selected_regions(pdev, pci_select_bars(pdev,
  1549. IORESOURCE_MEM),
  1550. igb_driver_name);
  1551. if (err)
  1552. goto err_pci_reg;
  1553. pci_enable_pcie_error_reporting(pdev);
  1554. pci_set_master(pdev);
  1555. pci_save_state(pdev);
  1556. err = -ENOMEM;
  1557. netdev = alloc_etherdev_mq(sizeof(struct igb_adapter),
  1558. IGB_ABS_MAX_TX_QUEUES);
  1559. if (!netdev)
  1560. goto err_alloc_etherdev;
  1561. SET_NETDEV_DEV(netdev, &pdev->dev);
  1562. pci_set_drvdata(pdev, netdev);
  1563. adapter = netdev_priv(netdev);
  1564. adapter->netdev = netdev;
  1565. adapter->pdev = pdev;
  1566. hw = &adapter->hw;
  1567. hw->back = adapter;
  1568. adapter->msg_enable = NETIF_MSG_DRV | NETIF_MSG_PROBE;
  1569. mmio_start = pci_resource_start(pdev, 0);
  1570. mmio_len = pci_resource_len(pdev, 0);
  1571. err = -EIO;
  1572. hw->hw_addr = ioremap(mmio_start, mmio_len);
  1573. if (!hw->hw_addr)
  1574. goto err_ioremap;
  1575. netdev->netdev_ops = &igb_netdev_ops;
  1576. igb_set_ethtool_ops(netdev);
  1577. netdev->watchdog_timeo = 5 * HZ;
  1578. strncpy(netdev->name, pci_name(pdev), sizeof(netdev->name) - 1);
  1579. netdev->mem_start = mmio_start;
  1580. netdev->mem_end = mmio_start + mmio_len;
  1581. /* PCI config space info */
  1582. hw->vendor_id = pdev->vendor;
  1583. hw->device_id = pdev->device;
  1584. hw->revision_id = pdev->revision;
  1585. hw->subsystem_vendor_id = pdev->subsystem_vendor;
  1586. hw->subsystem_device_id = pdev->subsystem_device;
  1587. /* Copy the default MAC, PHY and NVM function pointers */
  1588. memcpy(&hw->mac.ops, ei->mac_ops, sizeof(hw->mac.ops));
  1589. memcpy(&hw->phy.ops, ei->phy_ops, sizeof(hw->phy.ops));
  1590. memcpy(&hw->nvm.ops, ei->nvm_ops, sizeof(hw->nvm.ops));
  1591. /* Initialize skew-specific constants */
  1592. err = ei->get_invariants(hw);
  1593. if (err)
  1594. goto err_sw_init;
  1595. /* setup the private structure */
  1596. err = igb_sw_init(adapter);
  1597. if (err)
  1598. goto err_sw_init;
  1599. igb_get_bus_info_pcie(hw);
  1600. hw->phy.autoneg_wait_to_complete = false;
  1601. /* Copper options */
  1602. if (hw->phy.media_type == e1000_media_type_copper) {
  1603. hw->phy.mdix = AUTO_ALL_MODES;
  1604. hw->phy.disable_polarity_correction = false;
  1605. hw->phy.ms_type = e1000_ms_hw_default;
  1606. }
  1607. if (igb_check_reset_block(hw))
  1608. dev_info(&pdev->dev,
  1609. "PHY reset is blocked due to SOL/IDER session.\n");
  1610. netdev->features = NETIF_F_SG |
  1611. NETIF_F_IP_CSUM |
  1612. NETIF_F_HW_VLAN_TX |
  1613. NETIF_F_HW_VLAN_RX |
  1614. NETIF_F_HW_VLAN_FILTER;
  1615. netdev->features |= NETIF_F_IPV6_CSUM;
  1616. netdev->features |= NETIF_F_TSO;
  1617. netdev->features |= NETIF_F_TSO6;
  1618. netdev->features |= NETIF_F_GRO;
  1619. netdev->vlan_features |= NETIF_F_TSO;
  1620. netdev->vlan_features |= NETIF_F_TSO6;
  1621. netdev->vlan_features |= NETIF_F_IP_CSUM;
  1622. netdev->vlan_features |= NETIF_F_IPV6_CSUM;
  1623. netdev->vlan_features |= NETIF_F_SG;
  1624. if (pci_using_dac)
  1625. netdev->features |= NETIF_F_HIGHDMA;
  1626. if (hw->mac.type >= e1000_82576)
  1627. netdev->features |= NETIF_F_SCTP_CSUM;
  1628. adapter->en_mng_pt = igb_enable_mng_pass_thru(hw);
  1629. /* before reading the NVM, reset the controller to put the device in a
  1630. * known good starting state */
  1631. hw->mac.ops.reset_hw(hw);
  1632. /* make sure the NVM is good */
  1633. if (igb_validate_nvm_checksum(hw) < 0) {
  1634. dev_err(&pdev->dev, "The NVM Checksum Is Not Valid\n");
  1635. err = -EIO;
  1636. goto err_eeprom;
  1637. }
  1638. /* copy the MAC address out of the NVM */
  1639. if (hw->mac.ops.read_mac_addr(hw))
  1640. dev_err(&pdev->dev, "NVM Read Error\n");
  1641. memcpy(netdev->dev_addr, hw->mac.addr, netdev->addr_len);
  1642. memcpy(netdev->perm_addr, hw->mac.addr, netdev->addr_len);
  1643. if (!is_valid_ether_addr(netdev->perm_addr)) {
  1644. dev_err(&pdev->dev, "Invalid MAC Address\n");
  1645. err = -EIO;
  1646. goto err_eeprom;
  1647. }
  1648. setup_timer(&adapter->watchdog_timer, &igb_watchdog,
  1649. (unsigned long) adapter);
  1650. setup_timer(&adapter->phy_info_timer, &igb_update_phy_info,
  1651. (unsigned long) adapter);
  1652. INIT_WORK(&adapter->reset_task, igb_reset_task);
  1653. INIT_WORK(&adapter->watchdog_task, igb_watchdog_task);
  1654. /* Initialize link properties that are user-changeable */
  1655. adapter->fc_autoneg = true;
  1656. hw->mac.autoneg = true;
  1657. hw->phy.autoneg_advertised = 0x2f;
  1658. hw->fc.requested_mode = e1000_fc_default;
  1659. hw->fc.current_mode = e1000_fc_default;
  1660. igb_validate_mdi_setting(hw);
  1661. /* Initial Wake on LAN setting If APM wake is enabled in the EEPROM,
  1662. * enable the ACPI Magic Packet filter
  1663. */
  1664. if (hw->bus.func == 0)
  1665. hw->nvm.ops.read(hw, NVM_INIT_CONTROL3_PORT_A, 1, &eeprom_data);
  1666. else if (hw->mac.type == e1000_82580)
  1667. hw->nvm.ops.read(hw, NVM_INIT_CONTROL3_PORT_A +
  1668. NVM_82580_LAN_FUNC_OFFSET(hw->bus.func), 1,
  1669. &eeprom_data);
  1670. else if (hw->bus.func == 1)
  1671. hw->nvm.ops.read(hw, NVM_INIT_CONTROL3_PORT_B, 1, &eeprom_data);
  1672. if (eeprom_data & eeprom_apme_mask)
  1673. adapter->eeprom_wol |= E1000_WUFC_MAG;
  1674. /* now that we have the eeprom settings, apply the special cases where
  1675. * the eeprom may be wrong or the board simply won't support wake on
  1676. * lan on a particular port */
  1677. switch (pdev->device) {
  1678. case E1000_DEV_ID_82575GB_QUAD_COPPER:
  1679. adapter->eeprom_wol = 0;
  1680. break;
  1681. case E1000_DEV_ID_82575EB_FIBER_SERDES:
  1682. case E1000_DEV_ID_82576_FIBER:
  1683. case E1000_DEV_ID_82576_SERDES:
  1684. /* Wake events only supported on port A for dual fiber
  1685. * regardless of eeprom setting */
  1686. if (rd32(E1000_STATUS) & E1000_STATUS_FUNC_1)
  1687. adapter->eeprom_wol = 0;
  1688. break;
  1689. case E1000_DEV_ID_82576_QUAD_COPPER:
  1690. case E1000_DEV_ID_82576_QUAD_COPPER_ET2:
  1691. /* if quad port adapter, disable WoL on all but port A */
  1692. if (global_quad_port_a != 0)
  1693. adapter->eeprom_wol = 0;
  1694. else
  1695. adapter->flags |= IGB_FLAG_QUAD_PORT_A;
  1696. /* Reset for multiple quad port adapters */
  1697. if (++global_quad_port_a == 4)
  1698. global_quad_port_a = 0;
  1699. break;
  1700. }
  1701. /* initialize the wol settings based on the eeprom settings */
  1702. adapter->wol = adapter->eeprom_wol;
  1703. device_set_wakeup_enable(&adapter->pdev->dev, adapter->wol);
  1704. /* reset the hardware with the new settings */
  1705. igb_reset(adapter);
  1706. /* let the f/w know that the h/w is now under the control of the
  1707. * driver. */
  1708. igb_get_hw_control(adapter);
  1709. strcpy(netdev->name, "eth%d");
  1710. err = register_netdev(netdev);
  1711. if (err)
  1712. goto err_register;
  1713. /* carrier off reporting is important to ethtool even BEFORE open */
  1714. netif_carrier_off(netdev);
  1715. #ifdef CONFIG_IGB_DCA
  1716. if (dca_add_requester(&pdev->dev) == 0) {
  1717. adapter->flags |= IGB_FLAG_DCA_ENABLED;
  1718. dev_info(&pdev->dev, "DCA enabled\n");
  1719. igb_setup_dca(adapter);
  1720. }
  1721. #endif
  1722. dev_info(&pdev->dev, "Intel(R) Gigabit Ethernet Network Connection\n");
  1723. /* print bus type/speed/width info */
  1724. dev_info(&pdev->dev, "%s: (PCIe:%s:%s) %pM\n",
  1725. netdev->name,
  1726. ((hw->bus.speed == e1000_bus_speed_2500) ? "2.5Gb/s" :
  1727. (hw->bus.speed == e1000_bus_speed_5000) ? "5.0Gb/s" :
  1728. "unknown"),
  1729. ((hw->bus.width == e1000_bus_width_pcie_x4) ? "Width x4" :
  1730. (hw->bus.width == e1000_bus_width_pcie_x2) ? "Width x2" :
  1731. (hw->bus.width == e1000_bus_width_pcie_x1) ? "Width x1" :
  1732. "unknown"),
  1733. netdev->dev_addr);
  1734. igb_read_part_num(hw, &part_num);
  1735. dev_info(&pdev->dev, "%s: PBA No: %06x-%03x\n", netdev->name,
  1736. (part_num >> 8), (part_num & 0xff));
  1737. dev_info(&pdev->dev,
  1738. "Using %s interrupts. %d rx queue(s), %d tx queue(s)\n",
  1739. adapter->msix_entries ? "MSI-X" :
  1740. (adapter->flags & IGB_FLAG_HAS_MSI) ? "MSI" : "legacy",
  1741. adapter->num_rx_queues, adapter->num_tx_queues);
  1742. return 0;
  1743. err_register:
  1744. igb_release_hw_control(adapter);
  1745. err_eeprom:
  1746. if (!igb_check_reset_block(hw))
  1747. igb_reset_phy(hw);
  1748. if (hw->flash_address)
  1749. iounmap(hw->flash_address);
  1750. err_sw_init:
  1751. igb_clear_interrupt_scheme(adapter);
  1752. iounmap(hw->hw_addr);
  1753. err_ioremap:
  1754. free_netdev(netdev);
  1755. err_alloc_etherdev:
  1756. pci_release_selected_regions(pdev,
  1757. pci_select_bars(pdev, IORESOURCE_MEM));
  1758. err_pci_reg:
  1759. err_dma:
  1760. pci_disable_device(pdev);
  1761. return err;
  1762. }
  1763. /**
  1764. * igb_remove - Device Removal Routine
  1765. * @pdev: PCI device information struct
  1766. *
  1767. * igb_remove is called by the PCI subsystem to alert the driver
  1768. * that it should release a PCI device. The could be caused by a
  1769. * Hot-Plug event, or because the driver is going to be removed from
  1770. * memory.
  1771. **/
  1772. static void __devexit igb_remove(struct pci_dev *pdev)
  1773. {
  1774. struct net_device *netdev = pci_get_drvdata(pdev);
  1775. struct igb_adapter *adapter = netdev_priv(netdev);
  1776. struct e1000_hw *hw = &adapter->hw;
  1777. /* flush_scheduled work may reschedule our watchdog task, so
  1778. * explicitly disable watchdog tasks from being rescheduled */
  1779. set_bit(__IGB_DOWN, &adapter->state);
  1780. del_timer_sync(&adapter->watchdog_timer);
  1781. del_timer_sync(&adapter->phy_info_timer);
  1782. flush_scheduled_work();
  1783. #ifdef CONFIG_IGB_DCA
  1784. if (adapter->flags & IGB_FLAG_DCA_ENABLED) {
  1785. dev_info(&pdev->dev, "DCA disabled\n");
  1786. dca_remove_requester(&pdev->dev);
  1787. adapter->flags &= ~IGB_FLAG_DCA_ENABLED;
  1788. wr32(E1000_DCA_CTRL, E1000_DCA_CTRL_DCA_MODE_DISABLE);
  1789. }
  1790. #endif
  1791. /* Release control of h/w to f/w. If f/w is AMT enabled, this
  1792. * would have already happened in close and is redundant. */
  1793. igb_release_hw_control(adapter);
  1794. unregister_netdev(netdev);
  1795. igb_clear_interrupt_scheme(adapter);
  1796. #ifdef CONFIG_PCI_IOV
  1797. /* reclaim resources allocated to VFs */
  1798. if (adapter->vf_data) {
  1799. /* disable iov and allow time for transactions to clear */
  1800. pci_disable_sriov(pdev);
  1801. msleep(500);
  1802. kfree(adapter->vf_data);
  1803. adapter->vf_data = NULL;
  1804. wr32(E1000_IOVCTL, E1000_IOVCTL_REUSE_VFQ);
  1805. msleep(100);
  1806. dev_info(&pdev->dev, "IOV Disabled\n");
  1807. }
  1808. #endif
  1809. iounmap(hw->hw_addr);
  1810. if (hw->flash_address)
  1811. iounmap(hw->flash_address);
  1812. pci_release_selected_regions(pdev,
  1813. pci_select_bars(pdev, IORESOURCE_MEM));
  1814. free_netdev(netdev);
  1815. pci_disable_pcie_error_reporting(pdev);
  1816. pci_disable_device(pdev);
  1817. }
  1818. /**
  1819. * igb_probe_vfs - Initialize vf data storage and add VFs to pci config space
  1820. * @adapter: board private structure to initialize
  1821. *
  1822. * This function initializes the vf specific data storage and then attempts to
  1823. * allocate the VFs. The reason for ordering it this way is because it is much
  1824. * mor expensive time wise to disable SR-IOV than it is to allocate and free
  1825. * the memory for the VFs.
  1826. **/
  1827. static void __devinit igb_probe_vfs(struct igb_adapter * adapter)
  1828. {
  1829. #ifdef CONFIG_PCI_IOV
  1830. struct pci_dev *pdev = adapter->pdev;
  1831. if (adapter->vfs_allocated_count > 7)
  1832. adapter->vfs_allocated_count = 7;
  1833. if (adapter->vfs_allocated_count) {
  1834. adapter->vf_data = kcalloc(adapter->vfs_allocated_count,
  1835. sizeof(struct vf_data_storage),
  1836. GFP_KERNEL);
  1837. /* if allocation failed then we do not support SR-IOV */
  1838. if (!adapter->vf_data) {
  1839. adapter->vfs_allocated_count = 0;
  1840. dev_err(&pdev->dev, "Unable to allocate memory for VF "
  1841. "Data Storage\n");
  1842. }
  1843. }
  1844. if (pci_enable_sriov(pdev, adapter->vfs_allocated_count)) {
  1845. kfree(adapter->vf_data);
  1846. adapter->vf_data = NULL;
  1847. #endif /* CONFIG_PCI_IOV */
  1848. adapter->vfs_allocated_count = 0;
  1849. #ifdef CONFIG_PCI_IOV
  1850. } else {
  1851. unsigned char mac_addr[ETH_ALEN];
  1852. int i;
  1853. dev_info(&pdev->dev, "%d vfs allocated\n",
  1854. adapter->vfs_allocated_count);
  1855. for (i = 0; i < adapter->vfs_allocated_count; i++) {
  1856. random_ether_addr(mac_addr);
  1857. igb_set_vf_mac(adapter, i, mac_addr);
  1858. }
  1859. }
  1860. #endif /* CONFIG_PCI_IOV */
  1861. }
  1862. /**
  1863. * igb_init_hw_timer - Initialize hardware timer used with IEEE 1588 timestamp
  1864. * @adapter: board private structure to initialize
  1865. *
  1866. * igb_init_hw_timer initializes the function pointer and values for the hw
  1867. * timer found in hardware.
  1868. **/
  1869. static void igb_init_hw_timer(struct igb_adapter *adapter)
  1870. {
  1871. struct e1000_hw *hw = &adapter->hw;
  1872. switch (hw->mac.type) {
  1873. case e1000_i350:
  1874. case e1000_82580:
  1875. memset(&adapter->cycles, 0, sizeof(adapter->cycles));
  1876. adapter->cycles.read = igb_read_clock;
  1877. adapter->cycles.mask = CLOCKSOURCE_MASK(64);
  1878. adapter->cycles.mult = 1;
  1879. /*
  1880. * The 82580 timesync updates the system timer every 8ns by 8ns
  1881. * and the value cannot be shifted. Instead we need to shift
  1882. * the registers to generate a 64bit timer value. As a result
  1883. * SYSTIMR/L/H, TXSTMPL/H, RXSTMPL/H all have to be shifted by
  1884. * 24 in order to generate a larger value for synchronization.
  1885. */
  1886. adapter->cycles.shift = IGB_82580_TSYNC_SHIFT;
  1887. /* disable system timer temporarily by setting bit 31 */
  1888. wr32(E1000_TSAUXC, 0x80000000);
  1889. wrfl();
  1890. /* Set registers so that rollover occurs soon to test this. */
  1891. wr32(E1000_SYSTIMR, 0x00000000);
  1892. wr32(E1000_SYSTIML, 0x80000000);
  1893. wr32(E1000_SYSTIMH, 0x000000FF);
  1894. wrfl();
  1895. /* enable system timer by clearing bit 31 */
  1896. wr32(E1000_TSAUXC, 0x0);
  1897. wrfl();
  1898. timecounter_init(&adapter->clock,
  1899. &adapter->cycles,
  1900. ktime_to_ns(ktime_get_real()));
  1901. /*
  1902. * Synchronize our NIC clock against system wall clock. NIC
  1903. * time stamp reading requires ~3us per sample, each sample
  1904. * was pretty stable even under load => only require 10
  1905. * samples for each offset comparison.
  1906. */
  1907. memset(&adapter->compare, 0, sizeof(adapter->compare));
  1908. adapter->compare.source = &adapter->clock;
  1909. adapter->compare.target = ktime_get_real;
  1910. adapter->compare.num_samples = 10;
  1911. timecompare_update(&adapter->compare, 0);
  1912. break;
  1913. case e1000_82576:
  1914. /*
  1915. * Initialize hardware timer: we keep it running just in case
  1916. * that some program needs it later on.
  1917. */
  1918. memset(&adapter->cycles, 0, sizeof(adapter->cycles));
  1919. adapter->cycles.read = igb_read_clock;
  1920. adapter->cycles.mask = CLOCKSOURCE_MASK(64);
  1921. adapter->cycles.mult = 1;
  1922. /**
  1923. * Scale the NIC clock cycle by a large factor so that
  1924. * relatively small clock corrections can be added or
  1925. * substracted at each clock tick. The drawbacks of a large
  1926. * factor are a) that the clock register overflows more quickly
  1927. * (not such a big deal) and b) that the increment per tick has
  1928. * to fit into 24 bits. As a result we need to use a shift of
  1929. * 19 so we can fit a value of 16 into the TIMINCA register.
  1930. */
  1931. adapter->cycles.shift = IGB_82576_TSYNC_SHIFT;
  1932. wr32(E1000_TIMINCA,
  1933. (1 << E1000_TIMINCA_16NS_SHIFT) |
  1934. (16 << IGB_82576_TSYNC_SHIFT));
  1935. /* Set registers so that rollover occurs soon to test this. */
  1936. wr32(E1000_SYSTIML, 0x00000000);
  1937. wr32(E1000_SYSTIMH, 0xFF800000);
  1938. wrfl();
  1939. timecounter_init(&adapter->clock,
  1940. &adapter->cycles,
  1941. ktime_to_ns(ktime_get_real()));
  1942. /*
  1943. * Synchronize our NIC clock against system wall clock. NIC
  1944. * time stamp reading requires ~3us per sample, each sample
  1945. * was pretty stable even under load => only require 10
  1946. * samples for each offset comparison.
  1947. */
  1948. memset(&adapter->compare, 0, sizeof(adapter->compare));
  1949. adapter->compare.source = &adapter->clock;
  1950. adapter->compare.target = ktime_get_real;
  1951. adapter->compare.num_samples = 10;
  1952. timecompare_update(&adapter->compare, 0);
  1953. break;
  1954. case e1000_82575:
  1955. /* 82575 does not support timesync */
  1956. default:
  1957. break;
  1958. }
  1959. }
  1960. /**
  1961. * igb_sw_init - Initialize general software structures (struct igb_adapter)
  1962. * @adapter: board private structure to initialize
  1963. *
  1964. * igb_sw_init initializes the Adapter private data structure.
  1965. * Fields are initialized based on PCI device information and
  1966. * OS network device settings (MTU size).
  1967. **/
  1968. static int __devinit igb_sw_init(struct igb_adapter *adapter)
  1969. {
  1970. struct e1000_hw *hw = &adapter->hw;
  1971. struct net_device *netdev = adapter->netdev;
  1972. struct pci_dev *pdev = adapter->pdev;
  1973. pci_read_config_word(pdev, PCI_COMMAND, &hw->bus.pci_cmd_word);
  1974. adapter->tx_ring_count = IGB_DEFAULT_TXD;
  1975. adapter->rx_ring_count = IGB_DEFAULT_RXD;
  1976. adapter->rx_itr_setting = IGB_DEFAULT_ITR;
  1977. adapter->tx_itr_setting = IGB_DEFAULT_ITR;
  1978. adapter->max_frame_size = netdev->mtu + ETH_HLEN + ETH_FCS_LEN;
  1979. adapter->min_frame_size = ETH_ZLEN + ETH_FCS_LEN;
  1980. #ifdef CONFIG_PCI_IOV
  1981. if (hw->mac.type == e1000_82576)
  1982. adapter->vfs_allocated_count = max_vfs;
  1983. #endif /* CONFIG_PCI_IOV */
  1984. adapter->rss_queues = min_t(u32, IGB_MAX_RX_QUEUES, num_online_cpus());
  1985. /*
  1986. * if rss_queues > 4 or vfs are going to be allocated with rss_queues
  1987. * then we should combine the queues into a queue pair in order to
  1988. * conserve interrupts due to limited supply
  1989. */
  1990. if ((adapter->rss_queues > 4) ||
  1991. ((adapter->rss_queues > 1) && (adapter->vfs_allocated_count > 6)))
  1992. adapter->flags |= IGB_FLAG_QUEUE_PAIRS;
  1993. /* This call may decrease the number of queues */
  1994. if (igb_init_interrupt_scheme(adapter)) {
  1995. dev_err(&pdev->dev, "Unable to allocate memory for queues\n");
  1996. return -ENOMEM;
  1997. }
  1998. igb_init_hw_timer(adapter);
  1999. igb_probe_vfs(adapter);
  2000. /* Explicitly disable IRQ since the NIC can be in any state. */
  2001. igb_irq_disable(adapter);
  2002. set_bit(__IGB_DOWN, &adapter->state);
  2003. return 0;
  2004. }
  2005. /**
  2006. * igb_open - Called when a network interface is made active
  2007. * @netdev: network interface device structure
  2008. *
  2009. * Returns 0 on success, negative value on failure
  2010. *
  2011. * The open entry point is called when a network interface is made
  2012. * active by the system (IFF_UP). At this point all resources needed
  2013. * for transmit and receive operations are allocated, the interrupt
  2014. * handler is registered with the OS, the watchdog timer is started,
  2015. * and the stack is notified that the interface is ready.
  2016. **/
  2017. static int igb_open(struct net_device *netdev)
  2018. {
  2019. struct igb_adapter *adapter = netdev_priv(netdev);
  2020. struct e1000_hw *hw = &adapter->hw;
  2021. int err;
  2022. int i;
  2023. /* disallow open during test */
  2024. if (test_bit(__IGB_TESTING, &adapter->state))
  2025. return -EBUSY;
  2026. netif_carrier_off(netdev);
  2027. /* allocate transmit descriptors */
  2028. err = igb_setup_all_tx_resources(adapter);
  2029. if (err)
  2030. goto err_setup_tx;
  2031. /* allocate receive descriptors */
  2032. err = igb_setup_all_rx_resources(adapter);
  2033. if (err)
  2034. goto err_setup_rx;
  2035. igb_power_up_link(adapter);
  2036. /* before we allocate an interrupt, we must be ready to handle it.
  2037. * Setting DEBUG_SHIRQ in the kernel makes it fire an interrupt
  2038. * as soon as we call pci_request_irq, so we have to setup our
  2039. * clean_rx handler before we do so. */
  2040. igb_configure(adapter);
  2041. err = igb_request_irq(adapter);
  2042. if (err)
  2043. goto err_req_irq;
  2044. /* From here on the code is the same as igb_up() */
  2045. clear_bit(__IGB_DOWN, &adapter->state);
  2046. for (i = 0; i < adapter->num_q_vectors; i++) {
  2047. struct igb_q_vector *q_vector = adapter->q_vector[i];
  2048. napi_enable(&q_vector->napi);
  2049. }
  2050. /* Clear any pending interrupts. */
  2051. rd32(E1000_ICR);
  2052. igb_irq_enable(adapter);
  2053. /* notify VFs that reset has been completed */
  2054. if (adapter->vfs_allocated_count) {
  2055. u32 reg_data = rd32(E1000_CTRL_EXT);
  2056. reg_data |= E1000_CTRL_EXT_PFRSTD;
  2057. wr32(E1000_CTRL_EXT, reg_data);
  2058. }
  2059. netif_tx_start_all_queues(netdev);
  2060. /* start the watchdog. */
  2061. hw->mac.get_link_status = 1;
  2062. schedule_work(&adapter->watchdog_task);
  2063. return 0;
  2064. err_req_irq:
  2065. igb_release_hw_control(adapter);
  2066. igb_power_down_link(adapter);
  2067. igb_free_all_rx_resources(adapter);
  2068. err_setup_rx:
  2069. igb_free_all_tx_resources(adapter);
  2070. err_setup_tx:
  2071. igb_reset(adapter);
  2072. return err;
  2073. }
  2074. /**
  2075. * igb_close - Disables a network interface
  2076. * @netdev: network interface device structure
  2077. *
  2078. * Returns 0, this is not allowed to fail
  2079. *
  2080. * The close entry point is called when an interface is de-activated
  2081. * by the OS. The hardware is still under the driver's control, but
  2082. * needs to be disabled. A global MAC reset is issued to stop the
  2083. * hardware, and all transmit and receive resources are freed.
  2084. **/
  2085. static int igb_close(struct net_device *netdev)
  2086. {
  2087. struct igb_adapter *adapter = netdev_priv(netdev);
  2088. WARN_ON(test_bit(__IGB_RESETTING, &adapter->state));
  2089. igb_down(adapter);
  2090. igb_free_irq(adapter);
  2091. igb_free_all_tx_resources(adapter);
  2092. igb_free_all_rx_resources(adapter);
  2093. return 0;
  2094. }
  2095. /**
  2096. * igb_setup_tx_resources - allocate Tx resources (Descriptors)
  2097. * @tx_ring: tx descriptor ring (for a specific queue) to setup
  2098. *
  2099. * Return 0 on success, negative on failure
  2100. **/
  2101. int igb_setup_tx_resources(struct igb_ring *tx_ring)
  2102. {
  2103. struct device *dev = tx_ring->dev;
  2104. int size;
  2105. size = sizeof(struct igb_buffer) * tx_ring->count;
  2106. tx_ring->buffer_info = vmalloc(size);
  2107. if (!tx_ring->buffer_info)
  2108. goto err;
  2109. memset(tx_ring->buffer_info, 0, size);
  2110. /* round up to nearest 4K */
  2111. tx_ring->size = tx_ring->count * sizeof(union e1000_adv_tx_desc);
  2112. tx_ring->size = ALIGN(tx_ring->size, 4096);
  2113. tx_ring->desc = dma_alloc_coherent(dev,
  2114. tx_ring->size,
  2115. &tx_ring->dma,
  2116. GFP_KERNEL);
  2117. if (!tx_ring->desc)
  2118. goto err;
  2119. tx_ring->next_to_use = 0;
  2120. tx_ring->next_to_clean = 0;
  2121. return 0;
  2122. err:
  2123. vfree(tx_ring->buffer_info);
  2124. dev_err(dev,
  2125. "Unable to allocate memory for the transmit descriptor ring\n");
  2126. return -ENOMEM;
  2127. }
  2128. /**
  2129. * igb_setup_all_tx_resources - wrapper to allocate Tx resources
  2130. * (Descriptors) for all queues
  2131. * @adapter: board private structure
  2132. *
  2133. * Return 0 on success, negative on failure
  2134. **/
  2135. static int igb_setup_all_tx_resources(struct igb_adapter *adapter)
  2136. {
  2137. struct pci_dev *pdev = adapter->pdev;
  2138. int i, err = 0;
  2139. for (i = 0; i < adapter->num_tx_queues; i++) {
  2140. err = igb_setup_tx_resources(adapter->tx_ring[i]);
  2141. if (err) {
  2142. dev_err(&pdev->dev,
  2143. "Allocation for Tx Queue %u failed\n", i);
  2144. for (i--; i >= 0; i--)
  2145. igb_free_tx_resources(adapter->tx_ring[i]);
  2146. break;
  2147. }
  2148. }
  2149. for (i = 0; i < IGB_ABS_MAX_TX_QUEUES; i++) {
  2150. int r_idx = i % adapter->num_tx_queues;
  2151. adapter->multi_tx_table[i] = adapter->tx_ring[r_idx];
  2152. }
  2153. return err;
  2154. }
  2155. /**
  2156. * igb_setup_tctl - configure the transmit control registers
  2157. * @adapter: Board private structure
  2158. **/
  2159. void igb_setup_tctl(struct igb_adapter *adapter)
  2160. {
  2161. struct e1000_hw *hw = &adapter->hw;
  2162. u32 tctl;
  2163. /* disable queue 0 which is enabled by default on 82575 and 82576 */
  2164. wr32(E1000_TXDCTL(0), 0);
  2165. /* Program the Transmit Control Register */
  2166. tctl = rd32(E1000_TCTL);
  2167. tctl &= ~E1000_TCTL_CT;
  2168. tctl |= E1000_TCTL_PSP | E1000_TCTL_RTLC |
  2169. (E1000_COLLISION_THRESHOLD << E1000_CT_SHIFT);
  2170. igb_config_collision_dist(hw);
  2171. /* Enable transmits */
  2172. tctl |= E1000_TCTL_EN;
  2173. wr32(E1000_TCTL, tctl);
  2174. }
  2175. /**
  2176. * igb_configure_tx_ring - Configure transmit ring after Reset
  2177. * @adapter: board private structure
  2178. * @ring: tx ring to configure
  2179. *
  2180. * Configure a transmit ring after a reset.
  2181. **/
  2182. void igb_configure_tx_ring(struct igb_adapter *adapter,
  2183. struct igb_ring *ring)
  2184. {
  2185. struct e1000_hw *hw = &adapter->hw;
  2186. u32 txdctl;
  2187. u64 tdba = ring->dma;
  2188. int reg_idx = ring->reg_idx;
  2189. /* disable the queue */
  2190. txdctl = rd32(E1000_TXDCTL(reg_idx));
  2191. wr32(E1000_TXDCTL(reg_idx),
  2192. txdctl & ~E1000_TXDCTL_QUEUE_ENABLE);
  2193. wrfl();
  2194. mdelay(10);
  2195. wr32(E1000_TDLEN(reg_idx),
  2196. ring->count * sizeof(union e1000_adv_tx_desc));
  2197. wr32(E1000_TDBAL(reg_idx),
  2198. tdba & 0x00000000ffffffffULL);
  2199. wr32(E1000_TDBAH(reg_idx), tdba >> 32);
  2200. ring->head = hw->hw_addr + E1000_TDH(reg_idx);
  2201. ring->tail = hw->hw_addr + E1000_TDT(reg_idx);
  2202. writel(0, ring->head);
  2203. writel(0, ring->tail);
  2204. txdctl |= IGB_TX_PTHRESH;
  2205. txdctl |= IGB_TX_HTHRESH << 8;
  2206. txdctl |= IGB_TX_WTHRESH << 16;
  2207. txdctl |= E1000_TXDCTL_QUEUE_ENABLE;
  2208. wr32(E1000_TXDCTL(reg_idx), txdctl);
  2209. }
  2210. /**
  2211. * igb_configure_tx - Configure transmit Unit after Reset
  2212. * @adapter: board private structure
  2213. *
  2214. * Configure the Tx unit of the MAC after a reset.
  2215. **/
  2216. static void igb_configure_tx(struct igb_adapter *adapter)
  2217. {
  2218. int i;
  2219. for (i = 0; i < adapter->num_tx_queues; i++)
  2220. igb_configure_tx_ring(adapter, adapter->tx_ring[i]);
  2221. }
  2222. /**
  2223. * igb_setup_rx_resources - allocate Rx resources (Descriptors)
  2224. * @rx_ring: rx descriptor ring (for a specific queue) to setup
  2225. *
  2226. * Returns 0 on success, negative on failure
  2227. **/
  2228. int igb_setup_rx_resources(struct igb_ring *rx_ring)
  2229. {
  2230. struct device *dev = rx_ring->dev;
  2231. int size, desc_len;
  2232. size = sizeof(struct igb_buffer) * rx_ring->count;
  2233. rx_ring->buffer_info = vmalloc(size);
  2234. if (!rx_ring->buffer_info)
  2235. goto err;
  2236. memset(rx_ring->buffer_info, 0, size);
  2237. desc_len = sizeof(union e1000_adv_rx_desc);
  2238. /* Round up to nearest 4K */
  2239. rx_ring->size = rx_ring->count * desc_len;
  2240. rx_ring->size = ALIGN(rx_ring->size, 4096);
  2241. rx_ring->desc = dma_alloc_coherent(dev,
  2242. rx_ring->size,
  2243. &rx_ring->dma,
  2244. GFP_KERNEL);
  2245. if (!rx_ring->desc)
  2246. goto err;
  2247. rx_ring->next_to_clean = 0;
  2248. rx_ring->next_to_use = 0;
  2249. return 0;
  2250. err:
  2251. vfree(rx_ring->buffer_info);
  2252. rx_ring->buffer_info = NULL;
  2253. dev_err(dev, "Unable to allocate memory for the receive descriptor"
  2254. " ring\n");
  2255. return -ENOMEM;
  2256. }
  2257. /**
  2258. * igb_setup_all_rx_resources - wrapper to allocate Rx resources
  2259. * (Descriptors) for all queues
  2260. * @adapter: board private structure
  2261. *
  2262. * Return 0 on success, negative on failure
  2263. **/
  2264. static int igb_setup_all_rx_resources(struct igb_adapter *adapter)
  2265. {
  2266. struct pci_dev *pdev = adapter->pdev;
  2267. int i, err = 0;
  2268. for (i = 0; i < adapter->num_rx_queues; i++) {
  2269. err = igb_setup_rx_resources(adapter->rx_ring[i]);
  2270. if (err) {
  2271. dev_err(&pdev->dev,
  2272. "Allocation for Rx Queue %u failed\n", i);
  2273. for (i--; i >= 0; i--)
  2274. igb_free_rx_resources(adapter->rx_ring[i]);
  2275. break;
  2276. }
  2277. }
  2278. return err;
  2279. }
  2280. /**
  2281. * igb_setup_mrqc - configure the multiple receive queue control registers
  2282. * @adapter: Board private structure
  2283. **/
  2284. static void igb_setup_mrqc(struct igb_adapter *adapter)
  2285. {
  2286. struct e1000_hw *hw = &adapter->hw;
  2287. u32 mrqc, rxcsum;
  2288. u32 j, num_rx_queues, shift = 0, shift2 = 0;
  2289. union e1000_reta {
  2290. u32 dword;
  2291. u8 bytes[4];
  2292. } reta;
  2293. static const u8 rsshash[40] = {
  2294. 0x6d, 0x5a, 0x56, 0xda, 0x25, 0x5b, 0x0e, 0xc2, 0x41, 0x67,
  2295. 0x25, 0x3d, 0x43, 0xa3, 0x8f, 0xb0, 0xd0, 0xca, 0x2b, 0xcb,
  2296. 0xae, 0x7b, 0x30, 0xb4, 0x77, 0xcb, 0x2d, 0xa3, 0x80, 0x30,
  2297. 0xf2, 0x0c, 0x6a, 0x42, 0xb7, 0x3b, 0xbe, 0xac, 0x01, 0xfa };
  2298. /* Fill out hash function seeds */
  2299. for (j = 0; j < 10; j++) {
  2300. u32 rsskey = rsshash[(j * 4)];
  2301. rsskey |= rsshash[(j * 4) + 1] << 8;
  2302. rsskey |= rsshash[(j * 4) + 2] << 16;
  2303. rsskey |= rsshash[(j * 4) + 3] << 24;
  2304. array_wr32(E1000_RSSRK(0), j, rsskey);
  2305. }
  2306. num_rx_queues = adapter->rss_queues;
  2307. if (adapter->vfs_allocated_count) {
  2308. /* 82575 and 82576 supports 2 RSS queues for VMDq */
  2309. switch (hw->mac.type) {
  2310. case e1000_i350:
  2311. case e1000_82580:
  2312. num_rx_queues = 1;
  2313. shift = 0;
  2314. break;
  2315. case e1000_82576:
  2316. shift = 3;
  2317. num_rx_queues = 2;
  2318. break;
  2319. case e1000_82575:
  2320. shift = 2;
  2321. shift2 = 6;
  2322. default:
  2323. break;
  2324. }
  2325. } else {
  2326. if (hw->mac.type == e1000_82575)
  2327. shift = 6;
  2328. }
  2329. for (j = 0; j < (32 * 4); j++) {
  2330. reta.bytes[j & 3] = (j % num_rx_queues) << shift;
  2331. if (shift2)
  2332. reta.bytes[j & 3] |= num_rx_queues << shift2;
  2333. if ((j & 3) == 3)
  2334. wr32(E1000_RETA(j >> 2), reta.dword);
  2335. }
  2336. /*
  2337. * Disable raw packet checksumming so that RSS hash is placed in
  2338. * descriptor on writeback. No need to enable TCP/UDP/IP checksum
  2339. * offloads as they are enabled by default
  2340. */
  2341. rxcsum = rd32(E1000_RXCSUM);
  2342. rxcsum |= E1000_RXCSUM_PCSD;
  2343. if (adapter->hw.mac.type >= e1000_82576)
  2344. /* Enable Receive Checksum Offload for SCTP */
  2345. rxcsum |= E1000_RXCSUM_CRCOFL;
  2346. /* Don't need to set TUOFL or IPOFL, they default to 1 */
  2347. wr32(E1000_RXCSUM, rxcsum);
  2348. /* If VMDq is enabled then we set the appropriate mode for that, else
  2349. * we default to RSS so that an RSS hash is calculated per packet even
  2350. * if we are only using one queue */
  2351. if (adapter->vfs_allocated_count) {
  2352. if (hw->mac.type > e1000_82575) {
  2353. /* Set the default pool for the PF's first queue */
  2354. u32 vtctl = rd32(E1000_VT_CTL);
  2355. vtctl &= ~(E1000_VT_CTL_DEFAULT_POOL_MASK |
  2356. E1000_VT_CTL_DISABLE_DEF_POOL);
  2357. vtctl |= adapter->vfs_allocated_count <<
  2358. E1000_VT_CTL_DEFAULT_POOL_SHIFT;
  2359. wr32(E1000_VT_CTL, vtctl);
  2360. }
  2361. if (adapter->rss_queues > 1)
  2362. mrqc = E1000_MRQC_ENABLE_VMDQ_RSS_2Q;
  2363. else
  2364. mrqc = E1000_MRQC_ENABLE_VMDQ;
  2365. } else {
  2366. mrqc = E1000_MRQC_ENABLE_RSS_4Q;
  2367. }
  2368. igb_vmm_control(adapter);
  2369. mrqc |= (E1000_MRQC_RSS_FIELD_IPV4 |
  2370. E1000_MRQC_RSS_FIELD_IPV4_TCP);
  2371. mrqc |= (E1000_MRQC_RSS_FIELD_IPV6 |
  2372. E1000_MRQC_RSS_FIELD_IPV6_TCP);
  2373. mrqc |= (E1000_MRQC_RSS_FIELD_IPV4_UDP |
  2374. E1000_MRQC_RSS_FIELD_IPV6_UDP);
  2375. mrqc |= (E1000_MRQC_RSS_FIELD_IPV6_UDP_EX |
  2376. E1000_MRQC_RSS_FIELD_IPV6_TCP_EX);
  2377. wr32(E1000_MRQC, mrqc);
  2378. }
  2379. /**
  2380. * igb_setup_rctl - configure the receive control registers
  2381. * @adapter: Board private structure
  2382. **/
  2383. void igb_setup_rctl(struct igb_adapter *adapter)
  2384. {
  2385. struct e1000_hw *hw = &adapter->hw;
  2386. u32 rctl;
  2387. rctl = rd32(E1000_RCTL);
  2388. rctl &= ~(3 << E1000_RCTL_MO_SHIFT);
  2389. rctl &= ~(E1000_RCTL_LBM_TCVR | E1000_RCTL_LBM_MAC);
  2390. rctl |= E1000_RCTL_EN | E1000_RCTL_BAM | E1000_RCTL_RDMTS_HALF |
  2391. (hw->mac.mc_filter_type << E1000_RCTL_MO_SHIFT);
  2392. /*
  2393. * enable stripping of CRC. It's unlikely this will break BMC
  2394. * redirection as it did with e1000. Newer features require
  2395. * that the HW strips the CRC.
  2396. */
  2397. rctl |= E1000_RCTL_SECRC;
  2398. /* disable store bad packets and clear size bits. */
  2399. rctl &= ~(E1000_RCTL_SBP | E1000_RCTL_SZ_256);
  2400. /* enable LPE to prevent packets larger than max_frame_size */
  2401. rctl |= E1000_RCTL_LPE;
  2402. /* disable queue 0 to prevent tail write w/o re-config */
  2403. wr32(E1000_RXDCTL(0), 0);
  2404. /* Attention!!! For SR-IOV PF driver operations you must enable
  2405. * queue drop for all VF and PF queues to prevent head of line blocking
  2406. * if an un-trusted VF does not provide descriptors to hardware.
  2407. */
  2408. if (adapter->vfs_allocated_count) {
  2409. /* set all queue drop enable bits */
  2410. wr32(E1000_QDE, ALL_QUEUES);
  2411. }
  2412. wr32(E1000_RCTL, rctl);
  2413. }
  2414. static inline int igb_set_vf_rlpml(struct igb_adapter *adapter, int size,
  2415. int vfn)
  2416. {
  2417. struct e1000_hw *hw = &adapter->hw;
  2418. u32 vmolr;
  2419. /* if it isn't the PF check to see if VFs are enabled and
  2420. * increase the size to support vlan tags */
  2421. if (vfn < adapter->vfs_allocated_count &&
  2422. adapter->vf_data[vfn].vlans_enabled)
  2423. size += VLAN_TAG_SIZE;
  2424. vmolr = rd32(E1000_VMOLR(vfn));
  2425. vmolr &= ~E1000_VMOLR_RLPML_MASK;
  2426. vmolr |= size | E1000_VMOLR_LPE;
  2427. wr32(E1000_VMOLR(vfn), vmolr);
  2428. return 0;
  2429. }
  2430. /**
  2431. * igb_rlpml_set - set maximum receive packet size
  2432. * @adapter: board private structure
  2433. *
  2434. * Configure maximum receivable packet size.
  2435. **/
  2436. static void igb_rlpml_set(struct igb_adapter *adapter)
  2437. {
  2438. u32 max_frame_size = adapter->max_frame_size;
  2439. struct e1000_hw *hw = &adapter->hw;
  2440. u16 pf_id = adapter->vfs_allocated_count;
  2441. if (adapter->vlgrp)
  2442. max_frame_size += VLAN_TAG_SIZE;
  2443. /* if vfs are enabled we set RLPML to the largest possible request
  2444. * size and set the VMOLR RLPML to the size we need */
  2445. if (pf_id) {
  2446. igb_set_vf_rlpml(adapter, max_frame_size, pf_id);
  2447. max_frame_size = MAX_JUMBO_FRAME_SIZE;
  2448. }
  2449. wr32(E1000_RLPML, max_frame_size);
  2450. }
  2451. static inline void igb_set_vmolr(struct igb_adapter *adapter,
  2452. int vfn, bool aupe)
  2453. {
  2454. struct e1000_hw *hw = &adapter->hw;
  2455. u32 vmolr;
  2456. /*
  2457. * This register exists only on 82576 and newer so if we are older then
  2458. * we should exit and do nothing
  2459. */
  2460. if (hw->mac.type < e1000_82576)
  2461. return;
  2462. vmolr = rd32(E1000_VMOLR(vfn));
  2463. vmolr |= E1000_VMOLR_STRVLAN; /* Strip vlan tags */
  2464. if (aupe)
  2465. vmolr |= E1000_VMOLR_AUPE; /* Accept untagged packets */
  2466. else
  2467. vmolr &= ~(E1000_VMOLR_AUPE); /* Tagged packets ONLY */
  2468. /* clear all bits that might not be set */
  2469. vmolr &= ~(E1000_VMOLR_BAM | E1000_VMOLR_RSSE);
  2470. if (adapter->rss_queues > 1 && vfn == adapter->vfs_allocated_count)
  2471. vmolr |= E1000_VMOLR_RSSE; /* enable RSS */
  2472. /*
  2473. * for VMDq only allow the VFs and pool 0 to accept broadcast and
  2474. * multicast packets
  2475. */
  2476. if (vfn <= adapter->vfs_allocated_count)
  2477. vmolr |= E1000_VMOLR_BAM; /* Accept broadcast */
  2478. wr32(E1000_VMOLR(vfn), vmolr);
  2479. }
  2480. /**
  2481. * igb_configure_rx_ring - Configure a receive ring after Reset
  2482. * @adapter: board private structure
  2483. * @ring: receive ring to be configured
  2484. *
  2485. * Configure the Rx unit of the MAC after a reset.
  2486. **/
  2487. void igb_configure_rx_ring(struct igb_adapter *adapter,
  2488. struct igb_ring *ring)
  2489. {
  2490. struct e1000_hw *hw = &adapter->hw;
  2491. u64 rdba = ring->dma;
  2492. int reg_idx = ring->reg_idx;
  2493. u32 srrctl, rxdctl;
  2494. /* disable the queue */
  2495. rxdctl = rd32(E1000_RXDCTL(reg_idx));
  2496. wr32(E1000_RXDCTL(reg_idx),
  2497. rxdctl & ~E1000_RXDCTL_QUEUE_ENABLE);
  2498. /* Set DMA base address registers */
  2499. wr32(E1000_RDBAL(reg_idx),
  2500. rdba & 0x00000000ffffffffULL);
  2501. wr32(E1000_RDBAH(reg_idx), rdba >> 32);
  2502. wr32(E1000_RDLEN(reg_idx),
  2503. ring->count * sizeof(union e1000_adv_rx_desc));
  2504. /* initialize head and tail */
  2505. ring->head = hw->hw_addr + E1000_RDH(reg_idx);
  2506. ring->tail = hw->hw_addr + E1000_RDT(reg_idx);
  2507. writel(0, ring->head);
  2508. writel(0, ring->tail);
  2509. /* set descriptor configuration */
  2510. if (ring->rx_buffer_len < IGB_RXBUFFER_1024) {
  2511. srrctl = ALIGN(ring->rx_buffer_len, 64) <<
  2512. E1000_SRRCTL_BSIZEHDRSIZE_SHIFT;
  2513. #if (PAGE_SIZE / 2) > IGB_RXBUFFER_16384
  2514. srrctl |= IGB_RXBUFFER_16384 >>
  2515. E1000_SRRCTL_BSIZEPKT_SHIFT;
  2516. #else
  2517. srrctl |= (PAGE_SIZE / 2) >>
  2518. E1000_SRRCTL_BSIZEPKT_SHIFT;
  2519. #endif
  2520. srrctl |= E1000_SRRCTL_DESCTYPE_HDR_SPLIT_ALWAYS;
  2521. } else {
  2522. srrctl = ALIGN(ring->rx_buffer_len, 1024) >>
  2523. E1000_SRRCTL_BSIZEPKT_SHIFT;
  2524. srrctl |= E1000_SRRCTL_DESCTYPE_ADV_ONEBUF;
  2525. }
  2526. if (hw->mac.type == e1000_82580)
  2527. srrctl |= E1000_SRRCTL_TIMESTAMP;
  2528. /* Only set Drop Enable if we are supporting multiple queues */
  2529. if (adapter->vfs_allocated_count || adapter->num_rx_queues > 1)
  2530. srrctl |= E1000_SRRCTL_DROP_EN;
  2531. wr32(E1000_SRRCTL(reg_idx), srrctl);
  2532. /* set filtering for VMDQ pools */
  2533. igb_set_vmolr(adapter, reg_idx & 0x7, true);
  2534. /* enable receive descriptor fetching */
  2535. rxdctl = rd32(E1000_RXDCTL(reg_idx));
  2536. rxdctl |= E1000_RXDCTL_QUEUE_ENABLE;
  2537. rxdctl &= 0xFFF00000;
  2538. rxdctl |= IGB_RX_PTHRESH;
  2539. rxdctl |= IGB_RX_HTHRESH << 8;
  2540. rxdctl |= IGB_RX_WTHRESH << 16;
  2541. wr32(E1000_RXDCTL(reg_idx), rxdctl);
  2542. }
  2543. /**
  2544. * igb_configure_rx - Configure receive Unit after Reset
  2545. * @adapter: board private structure
  2546. *
  2547. * Configure the Rx unit of the MAC after a reset.
  2548. **/
  2549. static void igb_configure_rx(struct igb_adapter *adapter)
  2550. {
  2551. int i;
  2552. /* set UTA to appropriate mode */
  2553. igb_set_uta(adapter);
  2554. /* set the correct pool for the PF default MAC address in entry 0 */
  2555. igb_rar_set_qsel(adapter, adapter->hw.mac.addr, 0,
  2556. adapter->vfs_allocated_count);
  2557. /* Setup the HW Rx Head and Tail Descriptor Pointers and
  2558. * the Base and Length of the Rx Descriptor Ring */
  2559. for (i = 0; i < adapter->num_rx_queues; i++)
  2560. igb_configure_rx_ring(adapter, adapter->rx_ring[i]);
  2561. }
  2562. /**
  2563. * igb_free_tx_resources - Free Tx Resources per Queue
  2564. * @tx_ring: Tx descriptor ring for a specific queue
  2565. *
  2566. * Free all transmit software resources
  2567. **/
  2568. void igb_free_tx_resources(struct igb_ring *tx_ring)
  2569. {
  2570. igb_clean_tx_ring(tx_ring);
  2571. vfree(tx_ring->buffer_info);
  2572. tx_ring->buffer_info = NULL;
  2573. /* if not set, then don't free */
  2574. if (!tx_ring->desc)
  2575. return;
  2576. dma_free_coherent(tx_ring->dev, tx_ring->size,
  2577. tx_ring->desc, tx_ring->dma);
  2578. tx_ring->desc = NULL;
  2579. }
  2580. /**
  2581. * igb_free_all_tx_resources - Free Tx Resources for All Queues
  2582. * @adapter: board private structure
  2583. *
  2584. * Free all transmit software resources
  2585. **/
  2586. static void igb_free_all_tx_resources(struct igb_adapter *adapter)
  2587. {
  2588. int i;
  2589. for (i = 0; i < adapter->num_tx_queues; i++)
  2590. igb_free_tx_resources(adapter->tx_ring[i]);
  2591. }
  2592. void igb_unmap_and_free_tx_resource(struct igb_ring *tx_ring,
  2593. struct igb_buffer *buffer_info)
  2594. {
  2595. if (buffer_info->dma) {
  2596. if (buffer_info->mapped_as_page)
  2597. dma_unmap_page(tx_ring->dev,
  2598. buffer_info->dma,
  2599. buffer_info->length,
  2600. DMA_TO_DEVICE);
  2601. else
  2602. dma_unmap_single(tx_ring->dev,
  2603. buffer_info->dma,
  2604. buffer_info->length,
  2605. DMA_TO_DEVICE);
  2606. buffer_info->dma = 0;
  2607. }
  2608. if (buffer_info->skb) {
  2609. dev_kfree_skb_any(buffer_info->skb);
  2610. buffer_info->skb = NULL;
  2611. }
  2612. buffer_info->time_stamp = 0;
  2613. buffer_info->length = 0;
  2614. buffer_info->next_to_watch = 0;
  2615. buffer_info->mapped_as_page = false;
  2616. }
  2617. /**
  2618. * igb_clean_tx_ring - Free Tx Buffers
  2619. * @tx_ring: ring to be cleaned
  2620. **/
  2621. static void igb_clean_tx_ring(struct igb_ring *tx_ring)
  2622. {
  2623. struct igb_buffer *buffer_info;
  2624. unsigned long size;
  2625. unsigned int i;
  2626. if (!tx_ring->buffer_info)
  2627. return;
  2628. /* Free all the Tx ring sk_buffs */
  2629. for (i = 0; i < tx_ring->count; i++) {
  2630. buffer_info = &tx_ring->buffer_info[i];
  2631. igb_unmap_and_free_tx_resource(tx_ring, buffer_info);
  2632. }
  2633. size = sizeof(struct igb_buffer) * tx_ring->count;
  2634. memset(tx_ring->buffer_info, 0, size);
  2635. /* Zero out the descriptor ring */
  2636. memset(tx_ring->desc, 0, tx_ring->size);
  2637. tx_ring->next_to_use = 0;
  2638. tx_ring->next_to_clean = 0;
  2639. }
  2640. /**
  2641. * igb_clean_all_tx_rings - Free Tx Buffers for all queues
  2642. * @adapter: board private structure
  2643. **/
  2644. static void igb_clean_all_tx_rings(struct igb_adapter *adapter)
  2645. {
  2646. int i;
  2647. for (i = 0; i < adapter->num_tx_queues; i++)
  2648. igb_clean_tx_ring(adapter->tx_ring[i]);
  2649. }
  2650. /**
  2651. * igb_free_rx_resources - Free Rx Resources
  2652. * @rx_ring: ring to clean the resources from
  2653. *
  2654. * Free all receive software resources
  2655. **/
  2656. void igb_free_rx_resources(struct igb_ring *rx_ring)
  2657. {
  2658. igb_clean_rx_ring(rx_ring);
  2659. vfree(rx_ring->buffer_info);
  2660. rx_ring->buffer_info = NULL;
  2661. /* if not set, then don't free */
  2662. if (!rx_ring->desc)
  2663. return;
  2664. dma_free_coherent(rx_ring->dev, rx_ring->size,
  2665. rx_ring->desc, rx_ring->dma);
  2666. rx_ring->desc = NULL;
  2667. }
  2668. /**
  2669. * igb_free_all_rx_resources - Free Rx Resources for All Queues
  2670. * @adapter: board private structure
  2671. *
  2672. * Free all receive software resources
  2673. **/
  2674. static void igb_free_all_rx_resources(struct igb_adapter *adapter)
  2675. {
  2676. int i;
  2677. for (i = 0; i < adapter->num_rx_queues; i++)
  2678. igb_free_rx_resources(adapter->rx_ring[i]);
  2679. }
  2680. /**
  2681. * igb_clean_rx_ring - Free Rx Buffers per Queue
  2682. * @rx_ring: ring to free buffers from
  2683. **/
  2684. static void igb_clean_rx_ring(struct igb_ring *rx_ring)
  2685. {
  2686. struct igb_buffer *buffer_info;
  2687. unsigned long size;
  2688. unsigned int i;
  2689. if (!rx_ring->buffer_info)
  2690. return;
  2691. /* Free all the Rx ring sk_buffs */
  2692. for (i = 0; i < rx_ring->count; i++) {
  2693. buffer_info = &rx_ring->buffer_info[i];
  2694. if (buffer_info->dma) {
  2695. dma_unmap_single(rx_ring->dev,
  2696. buffer_info->dma,
  2697. rx_ring->rx_buffer_len,
  2698. DMA_FROM_DEVICE);
  2699. buffer_info->dma = 0;
  2700. }
  2701. if (buffer_info->skb) {
  2702. dev_kfree_skb(buffer_info->skb);
  2703. buffer_info->skb = NULL;
  2704. }
  2705. if (buffer_info->page_dma) {
  2706. dma_unmap_page(rx_ring->dev,
  2707. buffer_info->page_dma,
  2708. PAGE_SIZE / 2,
  2709. DMA_FROM_DEVICE);
  2710. buffer_info->page_dma = 0;
  2711. }
  2712. if (buffer_info->page) {
  2713. put_page(buffer_info->page);
  2714. buffer_info->page = NULL;
  2715. buffer_info->page_offset = 0;
  2716. }
  2717. }
  2718. size = sizeof(struct igb_buffer) * rx_ring->count;
  2719. memset(rx_ring->buffer_info, 0, size);
  2720. /* Zero out the descriptor ring */
  2721. memset(rx_ring->desc, 0, rx_ring->size);
  2722. rx_ring->next_to_clean = 0;
  2723. rx_ring->next_to_use = 0;
  2724. }
  2725. /**
  2726. * igb_clean_all_rx_rings - Free Rx Buffers for all queues
  2727. * @adapter: board private structure
  2728. **/
  2729. static void igb_clean_all_rx_rings(struct igb_adapter *adapter)
  2730. {
  2731. int i;
  2732. for (i = 0; i < adapter->num_rx_queues; i++)
  2733. igb_clean_rx_ring(adapter->rx_ring[i]);
  2734. }
  2735. /**
  2736. * igb_set_mac - Change the Ethernet Address of the NIC
  2737. * @netdev: network interface device structure
  2738. * @p: pointer to an address structure
  2739. *
  2740. * Returns 0 on success, negative on failure
  2741. **/
  2742. static int igb_set_mac(struct net_device *netdev, void *p)
  2743. {
  2744. struct igb_adapter *adapter = netdev_priv(netdev);
  2745. struct e1000_hw *hw = &adapter->hw;
  2746. struct sockaddr *addr = p;
  2747. if (!is_valid_ether_addr(addr->sa_data))
  2748. return -EADDRNOTAVAIL;
  2749. memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
  2750. memcpy(hw->mac.addr, addr->sa_data, netdev->addr_len);
  2751. /* set the correct pool for the new PF MAC address in entry 0 */
  2752. igb_rar_set_qsel(adapter, hw->mac.addr, 0,
  2753. adapter->vfs_allocated_count);
  2754. return 0;
  2755. }
  2756. /**
  2757. * igb_write_mc_addr_list - write multicast addresses to MTA
  2758. * @netdev: network interface device structure
  2759. *
  2760. * Writes multicast address list to the MTA hash table.
  2761. * Returns: -ENOMEM on failure
  2762. * 0 on no addresses written
  2763. * X on writing X addresses to MTA
  2764. **/
  2765. static int igb_write_mc_addr_list(struct net_device *netdev)
  2766. {
  2767. struct igb_adapter *adapter = netdev_priv(netdev);
  2768. struct e1000_hw *hw = &adapter->hw;
  2769. struct netdev_hw_addr *ha;
  2770. u8 *mta_list;
  2771. int i;
  2772. if (netdev_mc_empty(netdev)) {
  2773. /* nothing to program, so clear mc list */
  2774. igb_update_mc_addr_list(hw, NULL, 0);
  2775. igb_restore_vf_multicasts(adapter);
  2776. return 0;
  2777. }
  2778. mta_list = kzalloc(netdev_mc_count(netdev) * 6, GFP_ATOMIC);
  2779. if (!mta_list)
  2780. return -ENOMEM;
  2781. /* The shared function expects a packed array of only addresses. */
  2782. i = 0;
  2783. netdev_for_each_mc_addr(ha, netdev)
  2784. memcpy(mta_list + (i++ * ETH_ALEN), ha->addr, ETH_ALEN);
  2785. igb_update_mc_addr_list(hw, mta_list, i);
  2786. kfree(mta_list);
  2787. return netdev_mc_count(netdev);
  2788. }
  2789. /**
  2790. * igb_write_uc_addr_list - write unicast addresses to RAR table
  2791. * @netdev: network interface device structure
  2792. *
  2793. * Writes unicast address list to the RAR table.
  2794. * Returns: -ENOMEM on failure/insufficient address space
  2795. * 0 on no addresses written
  2796. * X on writing X addresses to the RAR table
  2797. **/
  2798. static int igb_write_uc_addr_list(struct net_device *netdev)
  2799. {
  2800. struct igb_adapter *adapter = netdev_priv(netdev);
  2801. struct e1000_hw *hw = &adapter->hw;
  2802. unsigned int vfn = adapter->vfs_allocated_count;
  2803. unsigned int rar_entries = hw->mac.rar_entry_count - (vfn + 1);
  2804. int count = 0;
  2805. /* return ENOMEM indicating insufficient memory for addresses */
  2806. if (netdev_uc_count(netdev) > rar_entries)
  2807. return -ENOMEM;
  2808. if (!netdev_uc_empty(netdev) && rar_entries) {
  2809. struct netdev_hw_addr *ha;
  2810. netdev_for_each_uc_addr(ha, netdev) {
  2811. if (!rar_entries)
  2812. break;
  2813. igb_rar_set_qsel(adapter, ha->addr,
  2814. rar_entries--,
  2815. vfn);
  2816. count++;
  2817. }
  2818. }
  2819. /* write the addresses in reverse order to avoid write combining */
  2820. for (; rar_entries > 0 ; rar_entries--) {
  2821. wr32(E1000_RAH(rar_entries), 0);
  2822. wr32(E1000_RAL(rar_entries), 0);
  2823. }
  2824. wrfl();
  2825. return count;
  2826. }
  2827. /**
  2828. * igb_set_rx_mode - Secondary Unicast, Multicast and Promiscuous mode set
  2829. * @netdev: network interface device structure
  2830. *
  2831. * The set_rx_mode entry point is called whenever the unicast or multicast
  2832. * address lists or the network interface flags are updated. This routine is
  2833. * responsible for configuring the hardware for proper unicast, multicast,
  2834. * promiscuous mode, and all-multi behavior.
  2835. **/
  2836. static void igb_set_rx_mode(struct net_device *netdev)
  2837. {
  2838. struct igb_adapter *adapter = netdev_priv(netdev);
  2839. struct e1000_hw *hw = &adapter->hw;
  2840. unsigned int vfn = adapter->vfs_allocated_count;
  2841. u32 rctl, vmolr = 0;
  2842. int count;
  2843. /* Check for Promiscuous and All Multicast modes */
  2844. rctl = rd32(E1000_RCTL);
  2845. /* clear the effected bits */
  2846. rctl &= ~(E1000_RCTL_UPE | E1000_RCTL_MPE | E1000_RCTL_VFE);
  2847. if (netdev->flags & IFF_PROMISC) {
  2848. rctl |= (E1000_RCTL_UPE | E1000_RCTL_MPE);
  2849. vmolr |= (E1000_VMOLR_ROPE | E1000_VMOLR_MPME);
  2850. } else {
  2851. if (netdev->flags & IFF_ALLMULTI) {
  2852. rctl |= E1000_RCTL_MPE;
  2853. vmolr |= E1000_VMOLR_MPME;
  2854. } else {
  2855. /*
  2856. * Write addresses to the MTA, if the attempt fails
  2857. * then we should just turn on promiscous mode so
  2858. * that we can at least receive multicast traffic
  2859. */
  2860. count = igb_write_mc_addr_list(netdev);
  2861. if (count < 0) {
  2862. rctl |= E1000_RCTL_MPE;
  2863. vmolr |= E1000_VMOLR_MPME;
  2864. } else if (count) {
  2865. vmolr |= E1000_VMOLR_ROMPE;
  2866. }
  2867. }
  2868. /*
  2869. * Write addresses to available RAR registers, if there is not
  2870. * sufficient space to store all the addresses then enable
  2871. * unicast promiscous mode
  2872. */
  2873. count = igb_write_uc_addr_list(netdev);
  2874. if (count < 0) {
  2875. rctl |= E1000_RCTL_UPE;
  2876. vmolr |= E1000_VMOLR_ROPE;
  2877. }
  2878. rctl |= E1000_RCTL_VFE;
  2879. }
  2880. wr32(E1000_RCTL, rctl);
  2881. /*
  2882. * In order to support SR-IOV and eventually VMDq it is necessary to set
  2883. * the VMOLR to enable the appropriate modes. Without this workaround
  2884. * we will have issues with VLAN tag stripping not being done for frames
  2885. * that are only arriving because we are the default pool
  2886. */
  2887. if (hw->mac.type < e1000_82576)
  2888. return;
  2889. vmolr |= rd32(E1000_VMOLR(vfn)) &
  2890. ~(E1000_VMOLR_ROPE | E1000_VMOLR_MPME | E1000_VMOLR_ROMPE);
  2891. wr32(E1000_VMOLR(vfn), vmolr);
  2892. igb_restore_vf_multicasts(adapter);
  2893. }
  2894. /* Need to wait a few seconds after link up to get diagnostic information from
  2895. * the phy */
  2896. static void igb_update_phy_info(unsigned long data)
  2897. {
  2898. struct igb_adapter *adapter = (struct igb_adapter *) data;
  2899. igb_get_phy_info(&adapter->hw);
  2900. }
  2901. /**
  2902. * igb_has_link - check shared code for link and determine up/down
  2903. * @adapter: pointer to driver private info
  2904. **/
  2905. bool igb_has_link(struct igb_adapter *adapter)
  2906. {
  2907. struct e1000_hw *hw = &adapter->hw;
  2908. bool link_active = false;
  2909. s32 ret_val = 0;
  2910. /* get_link_status is set on LSC (link status) interrupt or
  2911. * rx sequence error interrupt. get_link_status will stay
  2912. * false until the e1000_check_for_link establishes link
  2913. * for copper adapters ONLY
  2914. */
  2915. switch (hw->phy.media_type) {
  2916. case e1000_media_type_copper:
  2917. if (hw->mac.get_link_status) {
  2918. ret_val = hw->mac.ops.check_for_link(hw);
  2919. link_active = !hw->mac.get_link_status;
  2920. } else {
  2921. link_active = true;
  2922. }
  2923. break;
  2924. case e1000_media_type_internal_serdes:
  2925. ret_val = hw->mac.ops.check_for_link(hw);
  2926. link_active = hw->mac.serdes_has_link;
  2927. break;
  2928. default:
  2929. case e1000_media_type_unknown:
  2930. break;
  2931. }
  2932. return link_active;
  2933. }
  2934. /**
  2935. * igb_watchdog - Timer Call-back
  2936. * @data: pointer to adapter cast into an unsigned long
  2937. **/
  2938. static void igb_watchdog(unsigned long data)
  2939. {
  2940. struct igb_adapter *adapter = (struct igb_adapter *)data;
  2941. /* Do the rest outside of interrupt context */
  2942. schedule_work(&adapter->watchdog_task);
  2943. }
  2944. static void igb_watchdog_task(struct work_struct *work)
  2945. {
  2946. struct igb_adapter *adapter = container_of(work,
  2947. struct igb_adapter,
  2948. watchdog_task);
  2949. struct e1000_hw *hw = &adapter->hw;
  2950. struct net_device *netdev = adapter->netdev;
  2951. u32 link;
  2952. int i;
  2953. link = igb_has_link(adapter);
  2954. if (link) {
  2955. if (!netif_carrier_ok(netdev)) {
  2956. u32 ctrl;
  2957. hw->mac.ops.get_speed_and_duplex(hw,
  2958. &adapter->link_speed,
  2959. &adapter->link_duplex);
  2960. ctrl = rd32(E1000_CTRL);
  2961. /* Links status message must follow this format */
  2962. printk(KERN_INFO "igb: %s NIC Link is Up %d Mbps %s, "
  2963. "Flow Control: %s\n",
  2964. netdev->name,
  2965. adapter->link_speed,
  2966. adapter->link_duplex == FULL_DUPLEX ?
  2967. "Full Duplex" : "Half Duplex",
  2968. ((ctrl & E1000_CTRL_TFCE) &&
  2969. (ctrl & E1000_CTRL_RFCE)) ? "RX/TX" :
  2970. ((ctrl & E1000_CTRL_RFCE) ? "RX" :
  2971. ((ctrl & E1000_CTRL_TFCE) ? "TX" : "None")));
  2972. /* adjust timeout factor according to speed/duplex */
  2973. adapter->tx_timeout_factor = 1;
  2974. switch (adapter->link_speed) {
  2975. case SPEED_10:
  2976. adapter->tx_timeout_factor = 14;
  2977. break;
  2978. case SPEED_100:
  2979. /* maybe add some timeout factor ? */
  2980. break;
  2981. }
  2982. netif_carrier_on(netdev);
  2983. igb_ping_all_vfs(adapter);
  2984. /* link state has changed, schedule phy info update */
  2985. if (!test_bit(__IGB_DOWN, &adapter->state))
  2986. mod_timer(&adapter->phy_info_timer,
  2987. round_jiffies(jiffies + 2 * HZ));
  2988. }
  2989. } else {
  2990. if (netif_carrier_ok(netdev)) {
  2991. adapter->link_speed = 0;
  2992. adapter->link_duplex = 0;
  2993. /* Links status message must follow this format */
  2994. printk(KERN_INFO "igb: %s NIC Link is Down\n",
  2995. netdev->name);
  2996. netif_carrier_off(netdev);
  2997. igb_ping_all_vfs(adapter);
  2998. /* link state has changed, schedule phy info update */
  2999. if (!test_bit(__IGB_DOWN, &adapter->state))
  3000. mod_timer(&adapter->phy_info_timer,
  3001. round_jiffies(jiffies + 2 * HZ));
  3002. }
  3003. }
  3004. igb_update_stats(adapter);
  3005. for (i = 0; i < adapter->num_tx_queues; i++) {
  3006. struct igb_ring *tx_ring = adapter->tx_ring[i];
  3007. if (!netif_carrier_ok(netdev)) {
  3008. /* We've lost link, so the controller stops DMA,
  3009. * but we've got queued Tx work that's never going
  3010. * to get done, so reset controller to flush Tx.
  3011. * (Do the reset outside of interrupt context). */
  3012. if (igb_desc_unused(tx_ring) + 1 < tx_ring->count) {
  3013. adapter->tx_timeout_count++;
  3014. schedule_work(&adapter->reset_task);
  3015. /* return immediately since reset is imminent */
  3016. return;
  3017. }
  3018. }
  3019. /* Force detection of hung controller every watchdog period */
  3020. tx_ring->detect_tx_hung = true;
  3021. }
  3022. /* Cause software interrupt to ensure rx ring is cleaned */
  3023. if (adapter->msix_entries) {
  3024. u32 eics = 0;
  3025. for (i = 0; i < adapter->num_q_vectors; i++) {
  3026. struct igb_q_vector *q_vector = adapter->q_vector[i];
  3027. eics |= q_vector->eims_value;
  3028. }
  3029. wr32(E1000_EICS, eics);
  3030. } else {
  3031. wr32(E1000_ICS, E1000_ICS_RXDMT0);
  3032. }
  3033. /* Reset the timer */
  3034. if (!test_bit(__IGB_DOWN, &adapter->state))
  3035. mod_timer(&adapter->watchdog_timer,
  3036. round_jiffies(jiffies + 2 * HZ));
  3037. }
  3038. enum latency_range {
  3039. lowest_latency = 0,
  3040. low_latency = 1,
  3041. bulk_latency = 2,
  3042. latency_invalid = 255
  3043. };
  3044. /**
  3045. * igb_update_ring_itr - update the dynamic ITR value based on packet size
  3046. *
  3047. * Stores a new ITR value based on strictly on packet size. This
  3048. * algorithm is less sophisticated than that used in igb_update_itr,
  3049. * due to the difficulty of synchronizing statistics across multiple
  3050. * receive rings. The divisors and thresholds used by this fuction
  3051. * were determined based on theoretical maximum wire speed and testing
  3052. * data, in order to minimize response time while increasing bulk
  3053. * throughput.
  3054. * This functionality is controlled by the InterruptThrottleRate module
  3055. * parameter (see igb_param.c)
  3056. * NOTE: This function is called only when operating in a multiqueue
  3057. * receive environment.
  3058. * @q_vector: pointer to q_vector
  3059. **/
  3060. static void igb_update_ring_itr(struct igb_q_vector *q_vector)
  3061. {
  3062. int new_val = q_vector->itr_val;
  3063. int avg_wire_size = 0;
  3064. struct igb_adapter *adapter = q_vector->adapter;
  3065. /* For non-gigabit speeds, just fix the interrupt rate at 4000
  3066. * ints/sec - ITR timer value of 120 ticks.
  3067. */
  3068. if (adapter->link_speed != SPEED_1000) {
  3069. new_val = 976;
  3070. goto set_itr_val;
  3071. }
  3072. if (q_vector->rx_ring && q_vector->rx_ring->total_packets) {
  3073. struct igb_ring *ring = q_vector->rx_ring;
  3074. avg_wire_size = ring->total_bytes / ring->total_packets;
  3075. }
  3076. if (q_vector->tx_ring && q_vector->tx_ring->total_packets) {
  3077. struct igb_ring *ring = q_vector->tx_ring;
  3078. avg_wire_size = max_t(u32, avg_wire_size,
  3079. (ring->total_bytes /
  3080. ring->total_packets));
  3081. }
  3082. /* if avg_wire_size isn't set no work was done */
  3083. if (!avg_wire_size)
  3084. goto clear_counts;
  3085. /* Add 24 bytes to size to account for CRC, preamble, and gap */
  3086. avg_wire_size += 24;
  3087. /* Don't starve jumbo frames */
  3088. avg_wire_size = min(avg_wire_size, 3000);
  3089. /* Give a little boost to mid-size frames */
  3090. if ((avg_wire_size > 300) && (avg_wire_size < 1200))
  3091. new_val = avg_wire_size / 3;
  3092. else
  3093. new_val = avg_wire_size / 2;
  3094. /* when in itr mode 3 do not exceed 20K ints/sec */
  3095. if (adapter->rx_itr_setting == 3 && new_val < 196)
  3096. new_val = 196;
  3097. set_itr_val:
  3098. if (new_val != q_vector->itr_val) {
  3099. q_vector->itr_val = new_val;
  3100. q_vector->set_itr = 1;
  3101. }
  3102. clear_counts:
  3103. if (q_vector->rx_ring) {
  3104. q_vector->rx_ring->total_bytes = 0;
  3105. q_vector->rx_ring->total_packets = 0;
  3106. }
  3107. if (q_vector->tx_ring) {
  3108. q_vector->tx_ring->total_bytes = 0;
  3109. q_vector->tx_ring->total_packets = 0;
  3110. }
  3111. }
  3112. /**
  3113. * igb_update_itr - update the dynamic ITR value based on statistics
  3114. * Stores a new ITR value based on packets and byte
  3115. * counts during the last interrupt. The advantage of per interrupt
  3116. * computation is faster updates and more accurate ITR for the current
  3117. * traffic pattern. Constants in this function were computed
  3118. * based on theoretical maximum wire speed and thresholds were set based
  3119. * on testing data as well as attempting to minimize response time
  3120. * while increasing bulk throughput.
  3121. * this functionality is controlled by the InterruptThrottleRate module
  3122. * parameter (see igb_param.c)
  3123. * NOTE: These calculations are only valid when operating in a single-
  3124. * queue environment.
  3125. * @adapter: pointer to adapter
  3126. * @itr_setting: current q_vector->itr_val
  3127. * @packets: the number of packets during this measurement interval
  3128. * @bytes: the number of bytes during this measurement interval
  3129. **/
  3130. static unsigned int igb_update_itr(struct igb_adapter *adapter, u16 itr_setting,
  3131. int packets, int bytes)
  3132. {
  3133. unsigned int retval = itr_setting;
  3134. if (packets == 0)
  3135. goto update_itr_done;
  3136. switch (itr_setting) {
  3137. case lowest_latency:
  3138. /* handle TSO and jumbo frames */
  3139. if (bytes/packets > 8000)
  3140. retval = bulk_latency;
  3141. else if ((packets < 5) && (bytes > 512))
  3142. retval = low_latency;
  3143. break;
  3144. case low_latency: /* 50 usec aka 20000 ints/s */
  3145. if (bytes > 10000) {
  3146. /* this if handles the TSO accounting */
  3147. if (bytes/packets > 8000) {
  3148. retval = bulk_latency;
  3149. } else if ((packets < 10) || ((bytes/packets) > 1200)) {
  3150. retval = bulk_latency;
  3151. } else if ((packets > 35)) {
  3152. retval = lowest_latency;
  3153. }
  3154. } else if (bytes/packets > 2000) {
  3155. retval = bulk_latency;
  3156. } else if (packets <= 2 && bytes < 512) {
  3157. retval = lowest_latency;
  3158. }
  3159. break;
  3160. case bulk_latency: /* 250 usec aka 4000 ints/s */
  3161. if (bytes > 25000) {
  3162. if (packets > 35)
  3163. retval = low_latency;
  3164. } else if (bytes < 1500) {
  3165. retval = low_latency;
  3166. }
  3167. break;
  3168. }
  3169. update_itr_done:
  3170. return retval;
  3171. }
  3172. static void igb_set_itr(struct igb_adapter *adapter)
  3173. {
  3174. struct igb_q_vector *q_vector = adapter->q_vector[0];
  3175. u16 current_itr;
  3176. u32 new_itr = q_vector->itr_val;
  3177. /* for non-gigabit speeds, just fix the interrupt rate at 4000 */
  3178. if (adapter->link_speed != SPEED_1000) {
  3179. current_itr = 0;
  3180. new_itr = 4000;
  3181. goto set_itr_now;
  3182. }
  3183. adapter->rx_itr = igb_update_itr(adapter,
  3184. adapter->rx_itr,
  3185. q_vector->rx_ring->total_packets,
  3186. q_vector->rx_ring->total_bytes);
  3187. adapter->tx_itr = igb_update_itr(adapter,
  3188. adapter->tx_itr,
  3189. q_vector->tx_ring->total_packets,
  3190. q_vector->tx_ring->total_bytes);
  3191. current_itr = max(adapter->rx_itr, adapter->tx_itr);
  3192. /* conservative mode (itr 3) eliminates the lowest_latency setting */
  3193. if (adapter->rx_itr_setting == 3 && current_itr == lowest_latency)
  3194. current_itr = low_latency;
  3195. switch (current_itr) {
  3196. /* counts and packets in update_itr are dependent on these numbers */
  3197. case lowest_latency:
  3198. new_itr = 56; /* aka 70,000 ints/sec */
  3199. break;
  3200. case low_latency:
  3201. new_itr = 196; /* aka 20,000 ints/sec */
  3202. break;
  3203. case bulk_latency:
  3204. new_itr = 980; /* aka 4,000 ints/sec */
  3205. break;
  3206. default:
  3207. break;
  3208. }
  3209. set_itr_now:
  3210. q_vector->rx_ring->total_bytes = 0;
  3211. q_vector->rx_ring->total_packets = 0;
  3212. q_vector->tx_ring->total_bytes = 0;
  3213. q_vector->tx_ring->total_packets = 0;
  3214. if (new_itr != q_vector->itr_val) {
  3215. /* this attempts to bias the interrupt rate towards Bulk
  3216. * by adding intermediate steps when interrupt rate is
  3217. * increasing */
  3218. new_itr = new_itr > q_vector->itr_val ?
  3219. max((new_itr * q_vector->itr_val) /
  3220. (new_itr + (q_vector->itr_val >> 2)),
  3221. new_itr) :
  3222. new_itr;
  3223. /* Don't write the value here; it resets the adapter's
  3224. * internal timer, and causes us to delay far longer than
  3225. * we should between interrupts. Instead, we write the ITR
  3226. * value at the beginning of the next interrupt so the timing
  3227. * ends up being correct.
  3228. */
  3229. q_vector->itr_val = new_itr;
  3230. q_vector->set_itr = 1;
  3231. }
  3232. }
  3233. #define IGB_TX_FLAGS_CSUM 0x00000001
  3234. #define IGB_TX_FLAGS_VLAN 0x00000002
  3235. #define IGB_TX_FLAGS_TSO 0x00000004
  3236. #define IGB_TX_FLAGS_IPV4 0x00000008
  3237. #define IGB_TX_FLAGS_TSTAMP 0x00000010
  3238. #define IGB_TX_FLAGS_VLAN_MASK 0xffff0000
  3239. #define IGB_TX_FLAGS_VLAN_SHIFT 16
  3240. static inline int igb_tso_adv(struct igb_ring *tx_ring,
  3241. struct sk_buff *skb, u32 tx_flags, u8 *hdr_len)
  3242. {
  3243. struct e1000_adv_tx_context_desc *context_desc;
  3244. unsigned int i;
  3245. int err;
  3246. struct igb_buffer *buffer_info;
  3247. u32 info = 0, tu_cmd = 0;
  3248. u32 mss_l4len_idx;
  3249. u8 l4len;
  3250. if (skb_header_cloned(skb)) {
  3251. err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
  3252. if (err)
  3253. return err;
  3254. }
  3255. l4len = tcp_hdrlen(skb);
  3256. *hdr_len += l4len;
  3257. if (skb->protocol == htons(ETH_P_IP)) {
  3258. struct iphdr *iph = ip_hdr(skb);
  3259. iph->tot_len = 0;
  3260. iph->check = 0;
  3261. tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
  3262. iph->daddr, 0,
  3263. IPPROTO_TCP,
  3264. 0);
  3265. } else if (skb_is_gso_v6(skb)) {
  3266. ipv6_hdr(skb)->payload_len = 0;
  3267. tcp_hdr(skb)->check = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
  3268. &ipv6_hdr(skb)->daddr,
  3269. 0, IPPROTO_TCP, 0);
  3270. }
  3271. i = tx_ring->next_to_use;
  3272. buffer_info = &tx_ring->buffer_info[i];
  3273. context_desc = E1000_TX_CTXTDESC_ADV(*tx_ring, i);
  3274. /* VLAN MACLEN IPLEN */
  3275. if (tx_flags & IGB_TX_FLAGS_VLAN)
  3276. info |= (tx_flags & IGB_TX_FLAGS_VLAN_MASK);
  3277. info |= (skb_network_offset(skb) << E1000_ADVTXD_MACLEN_SHIFT);
  3278. *hdr_len += skb_network_offset(skb);
  3279. info |= skb_network_header_len(skb);
  3280. *hdr_len += skb_network_header_len(skb);
  3281. context_desc->vlan_macip_lens = cpu_to_le32(info);
  3282. /* ADV DTYP TUCMD MKRLOC/ISCSIHEDLEN */
  3283. tu_cmd |= (E1000_TXD_CMD_DEXT | E1000_ADVTXD_DTYP_CTXT);
  3284. if (skb->protocol == htons(ETH_P_IP))
  3285. tu_cmd |= E1000_ADVTXD_TUCMD_IPV4;
  3286. tu_cmd |= E1000_ADVTXD_TUCMD_L4T_TCP;
  3287. context_desc->type_tucmd_mlhl = cpu_to_le32(tu_cmd);
  3288. /* MSS L4LEN IDX */
  3289. mss_l4len_idx = (skb_shinfo(skb)->gso_size << E1000_ADVTXD_MSS_SHIFT);
  3290. mss_l4len_idx |= (l4len << E1000_ADVTXD_L4LEN_SHIFT);
  3291. /* For 82575, context index must be unique per ring. */
  3292. if (tx_ring->flags & IGB_RING_FLAG_TX_CTX_IDX)
  3293. mss_l4len_idx |= tx_ring->reg_idx << 4;
  3294. context_desc->mss_l4len_idx = cpu_to_le32(mss_l4len_idx);
  3295. context_desc->seqnum_seed = 0;
  3296. buffer_info->time_stamp = jiffies;
  3297. buffer_info->next_to_watch = i;
  3298. buffer_info->dma = 0;
  3299. i++;
  3300. if (i == tx_ring->count)
  3301. i = 0;
  3302. tx_ring->next_to_use = i;
  3303. return true;
  3304. }
  3305. static inline bool igb_tx_csum_adv(struct igb_ring *tx_ring,
  3306. struct sk_buff *skb, u32 tx_flags)
  3307. {
  3308. struct e1000_adv_tx_context_desc *context_desc;
  3309. struct device *dev = tx_ring->dev;
  3310. struct igb_buffer *buffer_info;
  3311. u32 info = 0, tu_cmd = 0;
  3312. unsigned int i;
  3313. if ((skb->ip_summed == CHECKSUM_PARTIAL) ||
  3314. (tx_flags & IGB_TX_FLAGS_VLAN)) {
  3315. i = tx_ring->next_to_use;
  3316. buffer_info = &tx_ring->buffer_info[i];
  3317. context_desc = E1000_TX_CTXTDESC_ADV(*tx_ring, i);
  3318. if (tx_flags & IGB_TX_FLAGS_VLAN)
  3319. info |= (tx_flags & IGB_TX_FLAGS_VLAN_MASK);
  3320. info |= (skb_network_offset(skb) << E1000_ADVTXD_MACLEN_SHIFT);
  3321. if (skb->ip_summed == CHECKSUM_PARTIAL)
  3322. info |= skb_network_header_len(skb);
  3323. context_desc->vlan_macip_lens = cpu_to_le32(info);
  3324. tu_cmd |= (E1000_TXD_CMD_DEXT | E1000_ADVTXD_DTYP_CTXT);
  3325. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  3326. __be16 protocol;
  3327. if (skb->protocol == cpu_to_be16(ETH_P_8021Q)) {
  3328. const struct vlan_ethhdr *vhdr =
  3329. (const struct vlan_ethhdr*)skb->data;
  3330. protocol = vhdr->h_vlan_encapsulated_proto;
  3331. } else {
  3332. protocol = skb->protocol;
  3333. }
  3334. switch (protocol) {
  3335. case cpu_to_be16(ETH_P_IP):
  3336. tu_cmd |= E1000_ADVTXD_TUCMD_IPV4;
  3337. if (ip_hdr(skb)->protocol == IPPROTO_TCP)
  3338. tu_cmd |= E1000_ADVTXD_TUCMD_L4T_TCP;
  3339. else if (ip_hdr(skb)->protocol == IPPROTO_SCTP)
  3340. tu_cmd |= E1000_ADVTXD_TUCMD_L4T_SCTP;
  3341. break;
  3342. case cpu_to_be16(ETH_P_IPV6):
  3343. /* XXX what about other V6 headers?? */
  3344. if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
  3345. tu_cmd |= E1000_ADVTXD_TUCMD_L4T_TCP;
  3346. else if (ipv6_hdr(skb)->nexthdr == IPPROTO_SCTP)
  3347. tu_cmd |= E1000_ADVTXD_TUCMD_L4T_SCTP;
  3348. break;
  3349. default:
  3350. if (unlikely(net_ratelimit()))
  3351. dev_warn(dev,
  3352. "partial checksum but proto=%x!\n",
  3353. skb->protocol);
  3354. break;
  3355. }
  3356. }
  3357. context_desc->type_tucmd_mlhl = cpu_to_le32(tu_cmd);
  3358. context_desc->seqnum_seed = 0;
  3359. if (tx_ring->flags & IGB_RING_FLAG_TX_CTX_IDX)
  3360. context_desc->mss_l4len_idx =
  3361. cpu_to_le32(tx_ring->reg_idx << 4);
  3362. buffer_info->time_stamp = jiffies;
  3363. buffer_info->next_to_watch = i;
  3364. buffer_info->dma = 0;
  3365. i++;
  3366. if (i == tx_ring->count)
  3367. i = 0;
  3368. tx_ring->next_to_use = i;
  3369. return true;
  3370. }
  3371. return false;
  3372. }
  3373. #define IGB_MAX_TXD_PWR 16
  3374. #define IGB_MAX_DATA_PER_TXD (1<<IGB_MAX_TXD_PWR)
  3375. static inline int igb_tx_map_adv(struct igb_ring *tx_ring, struct sk_buff *skb,
  3376. unsigned int first)
  3377. {
  3378. struct igb_buffer *buffer_info;
  3379. struct device *dev = tx_ring->dev;
  3380. unsigned int hlen = skb_headlen(skb);
  3381. unsigned int count = 0, i;
  3382. unsigned int f;
  3383. u16 gso_segs = skb_shinfo(skb)->gso_segs ?: 1;
  3384. i = tx_ring->next_to_use;
  3385. buffer_info = &tx_ring->buffer_info[i];
  3386. BUG_ON(hlen >= IGB_MAX_DATA_PER_TXD);
  3387. buffer_info->length = hlen;
  3388. /* set time_stamp *before* dma to help avoid a possible race */
  3389. buffer_info->time_stamp = jiffies;
  3390. buffer_info->next_to_watch = i;
  3391. buffer_info->dma = dma_map_single(dev, skb->data, hlen,
  3392. DMA_TO_DEVICE);
  3393. if (dma_mapping_error(dev, buffer_info->dma))
  3394. goto dma_error;
  3395. for (f = 0; f < skb_shinfo(skb)->nr_frags; f++) {
  3396. struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[f];
  3397. unsigned int len = frag->size;
  3398. count++;
  3399. i++;
  3400. if (i == tx_ring->count)
  3401. i = 0;
  3402. buffer_info = &tx_ring->buffer_info[i];
  3403. BUG_ON(len >= IGB_MAX_DATA_PER_TXD);
  3404. buffer_info->length = len;
  3405. buffer_info->time_stamp = jiffies;
  3406. buffer_info->next_to_watch = i;
  3407. buffer_info->mapped_as_page = true;
  3408. buffer_info->dma = dma_map_page(dev,
  3409. frag->page,
  3410. frag->page_offset,
  3411. len,
  3412. DMA_TO_DEVICE);
  3413. if (dma_mapping_error(dev, buffer_info->dma))
  3414. goto dma_error;
  3415. }
  3416. tx_ring->buffer_info[i].skb = skb;
  3417. tx_ring->buffer_info[i].shtx = skb_shinfo(skb)->tx_flags;
  3418. /* multiply data chunks by size of headers */
  3419. tx_ring->buffer_info[i].bytecount = ((gso_segs - 1) * hlen) + skb->len;
  3420. tx_ring->buffer_info[i].gso_segs = gso_segs;
  3421. tx_ring->buffer_info[first].next_to_watch = i;
  3422. return ++count;
  3423. dma_error:
  3424. dev_err(dev, "TX DMA map failed\n");
  3425. /* clear timestamp and dma mappings for failed buffer_info mapping */
  3426. buffer_info->dma = 0;
  3427. buffer_info->time_stamp = 0;
  3428. buffer_info->length = 0;
  3429. buffer_info->next_to_watch = 0;
  3430. buffer_info->mapped_as_page = false;
  3431. /* clear timestamp and dma mappings for remaining portion of packet */
  3432. while (count--) {
  3433. if (i == 0)
  3434. i = tx_ring->count;
  3435. i--;
  3436. buffer_info = &tx_ring->buffer_info[i];
  3437. igb_unmap_and_free_tx_resource(tx_ring, buffer_info);
  3438. }
  3439. return 0;
  3440. }
  3441. static inline void igb_tx_queue_adv(struct igb_ring *tx_ring,
  3442. u32 tx_flags, int count, u32 paylen,
  3443. u8 hdr_len)
  3444. {
  3445. union e1000_adv_tx_desc *tx_desc;
  3446. struct igb_buffer *buffer_info;
  3447. u32 olinfo_status = 0, cmd_type_len;
  3448. unsigned int i = tx_ring->next_to_use;
  3449. cmd_type_len = (E1000_ADVTXD_DTYP_DATA | E1000_ADVTXD_DCMD_IFCS |
  3450. E1000_ADVTXD_DCMD_DEXT);
  3451. if (tx_flags & IGB_TX_FLAGS_VLAN)
  3452. cmd_type_len |= E1000_ADVTXD_DCMD_VLE;
  3453. if (tx_flags & IGB_TX_FLAGS_TSTAMP)
  3454. cmd_type_len |= E1000_ADVTXD_MAC_TSTAMP;
  3455. if (tx_flags & IGB_TX_FLAGS_TSO) {
  3456. cmd_type_len |= E1000_ADVTXD_DCMD_TSE;
  3457. /* insert tcp checksum */
  3458. olinfo_status |= E1000_TXD_POPTS_TXSM << 8;
  3459. /* insert ip checksum */
  3460. if (tx_flags & IGB_TX_FLAGS_IPV4)
  3461. olinfo_status |= E1000_TXD_POPTS_IXSM << 8;
  3462. } else if (tx_flags & IGB_TX_FLAGS_CSUM) {
  3463. olinfo_status |= E1000_TXD_POPTS_TXSM << 8;
  3464. }
  3465. if ((tx_ring->flags & IGB_RING_FLAG_TX_CTX_IDX) &&
  3466. (tx_flags & (IGB_TX_FLAGS_CSUM |
  3467. IGB_TX_FLAGS_TSO |
  3468. IGB_TX_FLAGS_VLAN)))
  3469. olinfo_status |= tx_ring->reg_idx << 4;
  3470. olinfo_status |= ((paylen - hdr_len) << E1000_ADVTXD_PAYLEN_SHIFT);
  3471. do {
  3472. buffer_info = &tx_ring->buffer_info[i];
  3473. tx_desc = E1000_TX_DESC_ADV(*tx_ring, i);
  3474. tx_desc->read.buffer_addr = cpu_to_le64(buffer_info->dma);
  3475. tx_desc->read.cmd_type_len =
  3476. cpu_to_le32(cmd_type_len | buffer_info->length);
  3477. tx_desc->read.olinfo_status = cpu_to_le32(olinfo_status);
  3478. count--;
  3479. i++;
  3480. if (i == tx_ring->count)
  3481. i = 0;
  3482. } while (count > 0);
  3483. tx_desc->read.cmd_type_len |= cpu_to_le32(IGB_ADVTXD_DCMD);
  3484. /* Force memory writes to complete before letting h/w
  3485. * know there are new descriptors to fetch. (Only
  3486. * applicable for weak-ordered memory model archs,
  3487. * such as IA-64). */
  3488. wmb();
  3489. tx_ring->next_to_use = i;
  3490. writel(i, tx_ring->tail);
  3491. /* we need this if more than one processor can write to our tail
  3492. * at a time, it syncronizes IO on IA64/Altix systems */
  3493. mmiowb();
  3494. }
  3495. static int __igb_maybe_stop_tx(struct igb_ring *tx_ring, int size)
  3496. {
  3497. struct net_device *netdev = tx_ring->netdev;
  3498. netif_stop_subqueue(netdev, tx_ring->queue_index);
  3499. /* Herbert's original patch had:
  3500. * smp_mb__after_netif_stop_queue();
  3501. * but since that doesn't exist yet, just open code it. */
  3502. smp_mb();
  3503. /* We need to check again in a case another CPU has just
  3504. * made room available. */
  3505. if (igb_desc_unused(tx_ring) < size)
  3506. return -EBUSY;
  3507. /* A reprieve! */
  3508. netif_wake_subqueue(netdev, tx_ring->queue_index);
  3509. tx_ring->tx_stats.restart_queue++;
  3510. return 0;
  3511. }
  3512. static inline int igb_maybe_stop_tx(struct igb_ring *tx_ring, int size)
  3513. {
  3514. if (igb_desc_unused(tx_ring) >= size)
  3515. return 0;
  3516. return __igb_maybe_stop_tx(tx_ring, size);
  3517. }
  3518. netdev_tx_t igb_xmit_frame_ring_adv(struct sk_buff *skb,
  3519. struct igb_ring *tx_ring)
  3520. {
  3521. struct igb_adapter *adapter = netdev_priv(tx_ring->netdev);
  3522. int tso = 0, count;
  3523. u32 tx_flags = 0;
  3524. u16 first;
  3525. u8 hdr_len = 0;
  3526. union skb_shared_tx *shtx = skb_tx(skb);
  3527. /* need: 1 descriptor per page,
  3528. * + 2 desc gap to keep tail from touching head,
  3529. * + 1 desc for skb->data,
  3530. * + 1 desc for context descriptor,
  3531. * otherwise try next time */
  3532. if (igb_maybe_stop_tx(tx_ring, skb_shinfo(skb)->nr_frags + 4)) {
  3533. /* this is a hard error */
  3534. return NETDEV_TX_BUSY;
  3535. }
  3536. if (unlikely(shtx->hardware)) {
  3537. shtx->in_progress = 1;
  3538. tx_flags |= IGB_TX_FLAGS_TSTAMP;
  3539. }
  3540. if (vlan_tx_tag_present(skb) && adapter->vlgrp) {
  3541. tx_flags |= IGB_TX_FLAGS_VLAN;
  3542. tx_flags |= (vlan_tx_tag_get(skb) << IGB_TX_FLAGS_VLAN_SHIFT);
  3543. }
  3544. if (skb->protocol == htons(ETH_P_IP))
  3545. tx_flags |= IGB_TX_FLAGS_IPV4;
  3546. first = tx_ring->next_to_use;
  3547. if (skb_is_gso(skb)) {
  3548. tso = igb_tso_adv(tx_ring, skb, tx_flags, &hdr_len);
  3549. if (tso < 0) {
  3550. dev_kfree_skb_any(skb);
  3551. return NETDEV_TX_OK;
  3552. }
  3553. }
  3554. if (tso)
  3555. tx_flags |= IGB_TX_FLAGS_TSO;
  3556. else if (igb_tx_csum_adv(tx_ring, skb, tx_flags) &&
  3557. (skb->ip_summed == CHECKSUM_PARTIAL))
  3558. tx_flags |= IGB_TX_FLAGS_CSUM;
  3559. /*
  3560. * count reflects descriptors mapped, if 0 or less then mapping error
  3561. * has occured and we need to rewind the descriptor queue
  3562. */
  3563. count = igb_tx_map_adv(tx_ring, skb, first);
  3564. if (!count) {
  3565. dev_kfree_skb_any(skb);
  3566. tx_ring->buffer_info[first].time_stamp = 0;
  3567. tx_ring->next_to_use = first;
  3568. return NETDEV_TX_OK;
  3569. }
  3570. igb_tx_queue_adv(tx_ring, tx_flags, count, skb->len, hdr_len);
  3571. /* Make sure there is space in the ring for the next send. */
  3572. igb_maybe_stop_tx(tx_ring, MAX_SKB_FRAGS + 4);
  3573. return NETDEV_TX_OK;
  3574. }
  3575. static netdev_tx_t igb_xmit_frame_adv(struct sk_buff *skb,
  3576. struct net_device *netdev)
  3577. {
  3578. struct igb_adapter *adapter = netdev_priv(netdev);
  3579. struct igb_ring *tx_ring;
  3580. int r_idx = 0;
  3581. if (test_bit(__IGB_DOWN, &adapter->state)) {
  3582. dev_kfree_skb_any(skb);
  3583. return NETDEV_TX_OK;
  3584. }
  3585. if (skb->len <= 0) {
  3586. dev_kfree_skb_any(skb);
  3587. return NETDEV_TX_OK;
  3588. }
  3589. r_idx = skb->queue_mapping & (IGB_ABS_MAX_TX_QUEUES - 1);
  3590. tx_ring = adapter->multi_tx_table[r_idx];
  3591. /* This goes back to the question of how to logically map a tx queue
  3592. * to a flow. Right now, performance is impacted slightly negatively
  3593. * if using multiple tx queues. If the stack breaks away from a
  3594. * single qdisc implementation, we can look at this again. */
  3595. return igb_xmit_frame_ring_adv(skb, tx_ring);
  3596. }
  3597. /**
  3598. * igb_tx_timeout - Respond to a Tx Hang
  3599. * @netdev: network interface device structure
  3600. **/
  3601. static void igb_tx_timeout(struct net_device *netdev)
  3602. {
  3603. struct igb_adapter *adapter = netdev_priv(netdev);
  3604. struct e1000_hw *hw = &adapter->hw;
  3605. /* Do the reset outside of interrupt context */
  3606. adapter->tx_timeout_count++;
  3607. if (hw->mac.type == e1000_82580)
  3608. hw->dev_spec._82575.global_device_reset = true;
  3609. schedule_work(&adapter->reset_task);
  3610. wr32(E1000_EICS,
  3611. (adapter->eims_enable_mask & ~adapter->eims_other));
  3612. }
  3613. static void igb_reset_task(struct work_struct *work)
  3614. {
  3615. struct igb_adapter *adapter;
  3616. adapter = container_of(work, struct igb_adapter, reset_task);
  3617. igb_dump(adapter);
  3618. netdev_err(adapter->netdev, "Reset adapter\n");
  3619. igb_reinit_locked(adapter);
  3620. }
  3621. /**
  3622. * igb_get_stats - Get System Network Statistics
  3623. * @netdev: network interface device structure
  3624. *
  3625. * Returns the address of the device statistics structure.
  3626. * The statistics are actually updated from the timer callback.
  3627. **/
  3628. static struct net_device_stats *igb_get_stats(struct net_device *netdev)
  3629. {
  3630. /* only return the current stats */
  3631. return &netdev->stats;
  3632. }
  3633. /**
  3634. * igb_change_mtu - Change the Maximum Transfer Unit
  3635. * @netdev: network interface device structure
  3636. * @new_mtu: new value for maximum frame size
  3637. *
  3638. * Returns 0 on success, negative on failure
  3639. **/
  3640. static int igb_change_mtu(struct net_device *netdev, int new_mtu)
  3641. {
  3642. struct igb_adapter *adapter = netdev_priv(netdev);
  3643. struct pci_dev *pdev = adapter->pdev;
  3644. int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN;
  3645. u32 rx_buffer_len, i;
  3646. if ((new_mtu < 68) || (max_frame > MAX_JUMBO_FRAME_SIZE)) {
  3647. dev_err(&pdev->dev, "Invalid MTU setting\n");
  3648. return -EINVAL;
  3649. }
  3650. if (max_frame > MAX_STD_JUMBO_FRAME_SIZE) {
  3651. dev_err(&pdev->dev, "MTU > 9216 not supported.\n");
  3652. return -EINVAL;
  3653. }
  3654. while (test_and_set_bit(__IGB_RESETTING, &adapter->state))
  3655. msleep(1);
  3656. /* igb_down has a dependency on max_frame_size */
  3657. adapter->max_frame_size = max_frame;
  3658. /* NOTE: netdev_alloc_skb reserves 16 bytes, and typically NET_IP_ALIGN
  3659. * means we reserve 2 more, this pushes us to allocate from the next
  3660. * larger slab size.
  3661. * i.e. RXBUFFER_2048 --> size-4096 slab
  3662. */
  3663. if (adapter->hw.mac.type == e1000_82580)
  3664. max_frame += IGB_TS_HDR_LEN;
  3665. if (max_frame <= IGB_RXBUFFER_1024)
  3666. rx_buffer_len = IGB_RXBUFFER_1024;
  3667. else if (max_frame <= MAXIMUM_ETHERNET_VLAN_SIZE)
  3668. rx_buffer_len = MAXIMUM_ETHERNET_VLAN_SIZE;
  3669. else
  3670. rx_buffer_len = IGB_RXBUFFER_128;
  3671. if ((max_frame == ETH_FRAME_LEN + ETH_FCS_LEN + IGB_TS_HDR_LEN) ||
  3672. (max_frame == MAXIMUM_ETHERNET_VLAN_SIZE + IGB_TS_HDR_LEN))
  3673. rx_buffer_len = MAXIMUM_ETHERNET_VLAN_SIZE + IGB_TS_HDR_LEN;
  3674. if ((adapter->hw.mac.type == e1000_82580) &&
  3675. (rx_buffer_len == IGB_RXBUFFER_128))
  3676. rx_buffer_len += IGB_RXBUFFER_64;
  3677. if (netif_running(netdev))
  3678. igb_down(adapter);
  3679. dev_info(&pdev->dev, "changing MTU from %d to %d\n",
  3680. netdev->mtu, new_mtu);
  3681. netdev->mtu = new_mtu;
  3682. for (i = 0; i < adapter->num_rx_queues; i++)
  3683. adapter->rx_ring[i]->rx_buffer_len = rx_buffer_len;
  3684. if (netif_running(netdev))
  3685. igb_up(adapter);
  3686. else
  3687. igb_reset(adapter);
  3688. clear_bit(__IGB_RESETTING, &adapter->state);
  3689. return 0;
  3690. }
  3691. /**
  3692. * igb_update_stats - Update the board statistics counters
  3693. * @adapter: board private structure
  3694. **/
  3695. void igb_update_stats(struct igb_adapter *adapter)
  3696. {
  3697. struct net_device_stats *net_stats = igb_get_stats(adapter->netdev);
  3698. struct e1000_hw *hw = &adapter->hw;
  3699. struct pci_dev *pdev = adapter->pdev;
  3700. u32 reg, mpc;
  3701. u16 phy_tmp;
  3702. int i;
  3703. u64 bytes, packets;
  3704. #define PHY_IDLE_ERROR_COUNT_MASK 0x00FF
  3705. /*
  3706. * Prevent stats update while adapter is being reset, or if the pci
  3707. * connection is down.
  3708. */
  3709. if (adapter->link_speed == 0)
  3710. return;
  3711. if (pci_channel_offline(pdev))
  3712. return;
  3713. bytes = 0;
  3714. packets = 0;
  3715. for (i = 0; i < adapter->num_rx_queues; i++) {
  3716. u32 rqdpc_tmp = rd32(E1000_RQDPC(i)) & 0x0FFF;
  3717. struct igb_ring *ring = adapter->rx_ring[i];
  3718. ring->rx_stats.drops += rqdpc_tmp;
  3719. net_stats->rx_fifo_errors += rqdpc_tmp;
  3720. bytes += ring->rx_stats.bytes;
  3721. packets += ring->rx_stats.packets;
  3722. }
  3723. net_stats->rx_bytes = bytes;
  3724. net_stats->rx_packets = packets;
  3725. bytes = 0;
  3726. packets = 0;
  3727. for (i = 0; i < adapter->num_tx_queues; i++) {
  3728. struct igb_ring *ring = adapter->tx_ring[i];
  3729. bytes += ring->tx_stats.bytes;
  3730. packets += ring->tx_stats.packets;
  3731. }
  3732. net_stats->tx_bytes = bytes;
  3733. net_stats->tx_packets = packets;
  3734. /* read stats registers */
  3735. adapter->stats.crcerrs += rd32(E1000_CRCERRS);
  3736. adapter->stats.gprc += rd32(E1000_GPRC);
  3737. adapter->stats.gorc += rd32(E1000_GORCL);
  3738. rd32(E1000_GORCH); /* clear GORCL */
  3739. adapter->stats.bprc += rd32(E1000_BPRC);
  3740. adapter->stats.mprc += rd32(E1000_MPRC);
  3741. adapter->stats.roc += rd32(E1000_ROC);
  3742. adapter->stats.prc64 += rd32(E1000_PRC64);
  3743. adapter->stats.prc127 += rd32(E1000_PRC127);
  3744. adapter->stats.prc255 += rd32(E1000_PRC255);
  3745. adapter->stats.prc511 += rd32(E1000_PRC511);
  3746. adapter->stats.prc1023 += rd32(E1000_PRC1023);
  3747. adapter->stats.prc1522 += rd32(E1000_PRC1522);
  3748. adapter->stats.symerrs += rd32(E1000_SYMERRS);
  3749. adapter->stats.sec += rd32(E1000_SEC);
  3750. mpc = rd32(E1000_MPC);
  3751. adapter->stats.mpc += mpc;
  3752. net_stats->rx_fifo_errors += mpc;
  3753. adapter->stats.scc += rd32(E1000_SCC);
  3754. adapter->stats.ecol += rd32(E1000_ECOL);
  3755. adapter->stats.mcc += rd32(E1000_MCC);
  3756. adapter->stats.latecol += rd32(E1000_LATECOL);
  3757. adapter->stats.dc += rd32(E1000_DC);
  3758. adapter->stats.rlec += rd32(E1000_RLEC);
  3759. adapter->stats.xonrxc += rd32(E1000_XONRXC);
  3760. adapter->stats.xontxc += rd32(E1000_XONTXC);
  3761. adapter->stats.xoffrxc += rd32(E1000_XOFFRXC);
  3762. adapter->stats.xofftxc += rd32(E1000_XOFFTXC);
  3763. adapter->stats.fcruc += rd32(E1000_FCRUC);
  3764. adapter->stats.gptc += rd32(E1000_GPTC);
  3765. adapter->stats.gotc += rd32(E1000_GOTCL);
  3766. rd32(E1000_GOTCH); /* clear GOTCL */
  3767. adapter->stats.rnbc += rd32(E1000_RNBC);
  3768. adapter->stats.ruc += rd32(E1000_RUC);
  3769. adapter->stats.rfc += rd32(E1000_RFC);
  3770. adapter->stats.rjc += rd32(E1000_RJC);
  3771. adapter->stats.tor += rd32(E1000_TORH);
  3772. adapter->stats.tot += rd32(E1000_TOTH);
  3773. adapter->stats.tpr += rd32(E1000_TPR);
  3774. adapter->stats.ptc64 += rd32(E1000_PTC64);
  3775. adapter->stats.ptc127 += rd32(E1000_PTC127);
  3776. adapter->stats.ptc255 += rd32(E1000_PTC255);
  3777. adapter->stats.ptc511 += rd32(E1000_PTC511);
  3778. adapter->stats.ptc1023 += rd32(E1000_PTC1023);
  3779. adapter->stats.ptc1522 += rd32(E1000_PTC1522);
  3780. adapter->stats.mptc += rd32(E1000_MPTC);
  3781. adapter->stats.bptc += rd32(E1000_BPTC);
  3782. adapter->stats.tpt += rd32(E1000_TPT);
  3783. adapter->stats.colc += rd32(E1000_COLC);
  3784. adapter->stats.algnerrc += rd32(E1000_ALGNERRC);
  3785. /* read internal phy specific stats */
  3786. reg = rd32(E1000_CTRL_EXT);
  3787. if (!(reg & E1000_CTRL_EXT_LINK_MODE_MASK)) {
  3788. adapter->stats.rxerrc += rd32(E1000_RXERRC);
  3789. adapter->stats.tncrs += rd32(E1000_TNCRS);
  3790. }
  3791. adapter->stats.tsctc += rd32(E1000_TSCTC);
  3792. adapter->stats.tsctfc += rd32(E1000_TSCTFC);
  3793. adapter->stats.iac += rd32(E1000_IAC);
  3794. adapter->stats.icrxoc += rd32(E1000_ICRXOC);
  3795. adapter->stats.icrxptc += rd32(E1000_ICRXPTC);
  3796. adapter->stats.icrxatc += rd32(E1000_ICRXATC);
  3797. adapter->stats.ictxptc += rd32(E1000_ICTXPTC);
  3798. adapter->stats.ictxatc += rd32(E1000_ICTXATC);
  3799. adapter->stats.ictxqec += rd32(E1000_ICTXQEC);
  3800. adapter->stats.ictxqmtc += rd32(E1000_ICTXQMTC);
  3801. adapter->stats.icrxdmtc += rd32(E1000_ICRXDMTC);
  3802. /* Fill out the OS statistics structure */
  3803. net_stats->multicast = adapter->stats.mprc;
  3804. net_stats->collisions = adapter->stats.colc;
  3805. /* Rx Errors */
  3806. /* RLEC on some newer hardware can be incorrect so build
  3807. * our own version based on RUC and ROC */
  3808. net_stats->rx_errors = adapter->stats.rxerrc +
  3809. adapter->stats.crcerrs + adapter->stats.algnerrc +
  3810. adapter->stats.ruc + adapter->stats.roc +
  3811. adapter->stats.cexterr;
  3812. net_stats->rx_length_errors = adapter->stats.ruc +
  3813. adapter->stats.roc;
  3814. net_stats->rx_crc_errors = adapter->stats.crcerrs;
  3815. net_stats->rx_frame_errors = adapter->stats.algnerrc;
  3816. net_stats->rx_missed_errors = adapter->stats.mpc;
  3817. /* Tx Errors */
  3818. net_stats->tx_errors = adapter->stats.ecol +
  3819. adapter->stats.latecol;
  3820. net_stats->tx_aborted_errors = adapter->stats.ecol;
  3821. net_stats->tx_window_errors = adapter->stats.latecol;
  3822. net_stats->tx_carrier_errors = adapter->stats.tncrs;
  3823. /* Tx Dropped needs to be maintained elsewhere */
  3824. /* Phy Stats */
  3825. if (hw->phy.media_type == e1000_media_type_copper) {
  3826. if ((adapter->link_speed == SPEED_1000) &&
  3827. (!igb_read_phy_reg(hw, PHY_1000T_STATUS, &phy_tmp))) {
  3828. phy_tmp &= PHY_IDLE_ERROR_COUNT_MASK;
  3829. adapter->phy_stats.idle_errors += phy_tmp;
  3830. }
  3831. }
  3832. /* Management Stats */
  3833. adapter->stats.mgptc += rd32(E1000_MGTPTC);
  3834. adapter->stats.mgprc += rd32(E1000_MGTPRC);
  3835. adapter->stats.mgpdc += rd32(E1000_MGTPDC);
  3836. }
  3837. static irqreturn_t igb_msix_other(int irq, void *data)
  3838. {
  3839. struct igb_adapter *adapter = data;
  3840. struct e1000_hw *hw = &adapter->hw;
  3841. u32 icr = rd32(E1000_ICR);
  3842. /* reading ICR causes bit 31 of EICR to be cleared */
  3843. if (icr & E1000_ICR_DRSTA)
  3844. schedule_work(&adapter->reset_task);
  3845. if (icr & E1000_ICR_DOUTSYNC) {
  3846. /* HW is reporting DMA is out of sync */
  3847. adapter->stats.doosync++;
  3848. }
  3849. /* Check for a mailbox event */
  3850. if (icr & E1000_ICR_VMMB)
  3851. igb_msg_task(adapter);
  3852. if (icr & E1000_ICR_LSC) {
  3853. hw->mac.get_link_status = 1;
  3854. /* guard against interrupt when we're going down */
  3855. if (!test_bit(__IGB_DOWN, &adapter->state))
  3856. mod_timer(&adapter->watchdog_timer, jiffies + 1);
  3857. }
  3858. if (adapter->vfs_allocated_count)
  3859. wr32(E1000_IMS, E1000_IMS_LSC |
  3860. E1000_IMS_VMMB |
  3861. E1000_IMS_DOUTSYNC);
  3862. else
  3863. wr32(E1000_IMS, E1000_IMS_LSC | E1000_IMS_DOUTSYNC);
  3864. wr32(E1000_EIMS, adapter->eims_other);
  3865. return IRQ_HANDLED;
  3866. }
  3867. static void igb_write_itr(struct igb_q_vector *q_vector)
  3868. {
  3869. struct igb_adapter *adapter = q_vector->adapter;
  3870. u32 itr_val = q_vector->itr_val & 0x7FFC;
  3871. if (!q_vector->set_itr)
  3872. return;
  3873. if (!itr_val)
  3874. itr_val = 0x4;
  3875. if (adapter->hw.mac.type == e1000_82575)
  3876. itr_val |= itr_val << 16;
  3877. else
  3878. itr_val |= 0x8000000;
  3879. writel(itr_val, q_vector->itr_register);
  3880. q_vector->set_itr = 0;
  3881. }
  3882. static irqreturn_t igb_msix_ring(int irq, void *data)
  3883. {
  3884. struct igb_q_vector *q_vector = data;
  3885. /* Write the ITR value calculated from the previous interrupt. */
  3886. igb_write_itr(q_vector);
  3887. napi_schedule(&q_vector->napi);
  3888. return IRQ_HANDLED;
  3889. }
  3890. #ifdef CONFIG_IGB_DCA
  3891. static void igb_update_dca(struct igb_q_vector *q_vector)
  3892. {
  3893. struct igb_adapter *adapter = q_vector->adapter;
  3894. struct e1000_hw *hw = &adapter->hw;
  3895. int cpu = get_cpu();
  3896. if (q_vector->cpu == cpu)
  3897. goto out_no_update;
  3898. if (q_vector->tx_ring) {
  3899. int q = q_vector->tx_ring->reg_idx;
  3900. u32 dca_txctrl = rd32(E1000_DCA_TXCTRL(q));
  3901. if (hw->mac.type == e1000_82575) {
  3902. dca_txctrl &= ~E1000_DCA_TXCTRL_CPUID_MASK;
  3903. dca_txctrl |= dca3_get_tag(&adapter->pdev->dev, cpu);
  3904. } else {
  3905. dca_txctrl &= ~E1000_DCA_TXCTRL_CPUID_MASK_82576;
  3906. dca_txctrl |= dca3_get_tag(&adapter->pdev->dev, cpu) <<
  3907. E1000_DCA_TXCTRL_CPUID_SHIFT;
  3908. }
  3909. dca_txctrl |= E1000_DCA_TXCTRL_DESC_DCA_EN;
  3910. wr32(E1000_DCA_TXCTRL(q), dca_txctrl);
  3911. }
  3912. if (q_vector->rx_ring) {
  3913. int q = q_vector->rx_ring->reg_idx;
  3914. u32 dca_rxctrl = rd32(E1000_DCA_RXCTRL(q));
  3915. if (hw->mac.type == e1000_82575) {
  3916. dca_rxctrl &= ~E1000_DCA_RXCTRL_CPUID_MASK;
  3917. dca_rxctrl |= dca3_get_tag(&adapter->pdev->dev, cpu);
  3918. } else {
  3919. dca_rxctrl &= ~E1000_DCA_RXCTRL_CPUID_MASK_82576;
  3920. dca_rxctrl |= dca3_get_tag(&adapter->pdev->dev, cpu) <<
  3921. E1000_DCA_RXCTRL_CPUID_SHIFT;
  3922. }
  3923. dca_rxctrl |= E1000_DCA_RXCTRL_DESC_DCA_EN;
  3924. dca_rxctrl |= E1000_DCA_RXCTRL_HEAD_DCA_EN;
  3925. dca_rxctrl |= E1000_DCA_RXCTRL_DATA_DCA_EN;
  3926. wr32(E1000_DCA_RXCTRL(q), dca_rxctrl);
  3927. }
  3928. q_vector->cpu = cpu;
  3929. out_no_update:
  3930. put_cpu();
  3931. }
  3932. static void igb_setup_dca(struct igb_adapter *adapter)
  3933. {
  3934. struct e1000_hw *hw = &adapter->hw;
  3935. int i;
  3936. if (!(adapter->flags & IGB_FLAG_DCA_ENABLED))
  3937. return;
  3938. /* Always use CB2 mode, difference is masked in the CB driver. */
  3939. wr32(E1000_DCA_CTRL, E1000_DCA_CTRL_DCA_MODE_CB2);
  3940. for (i = 0; i < adapter->num_q_vectors; i++) {
  3941. adapter->q_vector[i]->cpu = -1;
  3942. igb_update_dca(adapter->q_vector[i]);
  3943. }
  3944. }
  3945. static int __igb_notify_dca(struct device *dev, void *data)
  3946. {
  3947. struct net_device *netdev = dev_get_drvdata(dev);
  3948. struct igb_adapter *adapter = netdev_priv(netdev);
  3949. struct pci_dev *pdev = adapter->pdev;
  3950. struct e1000_hw *hw = &adapter->hw;
  3951. unsigned long event = *(unsigned long *)data;
  3952. switch (event) {
  3953. case DCA_PROVIDER_ADD:
  3954. /* if already enabled, don't do it again */
  3955. if (adapter->flags & IGB_FLAG_DCA_ENABLED)
  3956. break;
  3957. if (dca_add_requester(dev) == 0) {
  3958. adapter->flags |= IGB_FLAG_DCA_ENABLED;
  3959. dev_info(&pdev->dev, "DCA enabled\n");
  3960. igb_setup_dca(adapter);
  3961. break;
  3962. }
  3963. /* Fall Through since DCA is disabled. */
  3964. case DCA_PROVIDER_REMOVE:
  3965. if (adapter->flags & IGB_FLAG_DCA_ENABLED) {
  3966. /* without this a class_device is left
  3967. * hanging around in the sysfs model */
  3968. dca_remove_requester(dev);
  3969. dev_info(&pdev->dev, "DCA disabled\n");
  3970. adapter->flags &= ~IGB_FLAG_DCA_ENABLED;
  3971. wr32(E1000_DCA_CTRL, E1000_DCA_CTRL_DCA_MODE_DISABLE);
  3972. }
  3973. break;
  3974. }
  3975. return 0;
  3976. }
  3977. static int igb_notify_dca(struct notifier_block *nb, unsigned long event,
  3978. void *p)
  3979. {
  3980. int ret_val;
  3981. ret_val = driver_for_each_device(&igb_driver.driver, NULL, &event,
  3982. __igb_notify_dca);
  3983. return ret_val ? NOTIFY_BAD : NOTIFY_DONE;
  3984. }
  3985. #endif /* CONFIG_IGB_DCA */
  3986. static void igb_ping_all_vfs(struct igb_adapter *adapter)
  3987. {
  3988. struct e1000_hw *hw = &adapter->hw;
  3989. u32 ping;
  3990. int i;
  3991. for (i = 0 ; i < adapter->vfs_allocated_count; i++) {
  3992. ping = E1000_PF_CONTROL_MSG;
  3993. if (adapter->vf_data[i].flags & IGB_VF_FLAG_CTS)
  3994. ping |= E1000_VT_MSGTYPE_CTS;
  3995. igb_write_mbx(hw, &ping, 1, i);
  3996. }
  3997. }
  3998. static int igb_set_vf_promisc(struct igb_adapter *adapter, u32 *msgbuf, u32 vf)
  3999. {
  4000. struct e1000_hw *hw = &adapter->hw;
  4001. u32 vmolr = rd32(E1000_VMOLR(vf));
  4002. struct vf_data_storage *vf_data = &adapter->vf_data[vf];
  4003. vf_data->flags |= ~(IGB_VF_FLAG_UNI_PROMISC |
  4004. IGB_VF_FLAG_MULTI_PROMISC);
  4005. vmolr &= ~(E1000_VMOLR_ROPE | E1000_VMOLR_ROMPE | E1000_VMOLR_MPME);
  4006. if (*msgbuf & E1000_VF_SET_PROMISC_MULTICAST) {
  4007. vmolr |= E1000_VMOLR_MPME;
  4008. *msgbuf &= ~E1000_VF_SET_PROMISC_MULTICAST;
  4009. } else {
  4010. /*
  4011. * if we have hashes and we are clearing a multicast promisc
  4012. * flag we need to write the hashes to the MTA as this step
  4013. * was previously skipped
  4014. */
  4015. if (vf_data->num_vf_mc_hashes > 30) {
  4016. vmolr |= E1000_VMOLR_MPME;
  4017. } else if (vf_data->num_vf_mc_hashes) {
  4018. int j;
  4019. vmolr |= E1000_VMOLR_ROMPE;
  4020. for (j = 0; j < vf_data->num_vf_mc_hashes; j++)
  4021. igb_mta_set(hw, vf_data->vf_mc_hashes[j]);
  4022. }
  4023. }
  4024. wr32(E1000_VMOLR(vf), vmolr);
  4025. /* there are flags left unprocessed, likely not supported */
  4026. if (*msgbuf & E1000_VT_MSGINFO_MASK)
  4027. return -EINVAL;
  4028. return 0;
  4029. }
  4030. static int igb_set_vf_multicasts(struct igb_adapter *adapter,
  4031. u32 *msgbuf, u32 vf)
  4032. {
  4033. int n = (msgbuf[0] & E1000_VT_MSGINFO_MASK) >> E1000_VT_MSGINFO_SHIFT;
  4034. u16 *hash_list = (u16 *)&msgbuf[1];
  4035. struct vf_data_storage *vf_data = &adapter->vf_data[vf];
  4036. int i;
  4037. /* salt away the number of multicast addresses assigned
  4038. * to this VF for later use to restore when the PF multi cast
  4039. * list changes
  4040. */
  4041. vf_data->num_vf_mc_hashes = n;
  4042. /* only up to 30 hash values supported */
  4043. if (n > 30)
  4044. n = 30;
  4045. /* store the hashes for later use */
  4046. for (i = 0; i < n; i++)
  4047. vf_data->vf_mc_hashes[i] = hash_list[i];
  4048. /* Flush and reset the mta with the new values */
  4049. igb_set_rx_mode(adapter->netdev);
  4050. return 0;
  4051. }
  4052. static void igb_restore_vf_multicasts(struct igb_adapter *adapter)
  4053. {
  4054. struct e1000_hw *hw = &adapter->hw;
  4055. struct vf_data_storage *vf_data;
  4056. int i, j;
  4057. for (i = 0; i < adapter->vfs_allocated_count; i++) {
  4058. u32 vmolr = rd32(E1000_VMOLR(i));
  4059. vmolr &= ~(E1000_VMOLR_ROMPE | E1000_VMOLR_MPME);
  4060. vf_data = &adapter->vf_data[i];
  4061. if ((vf_data->num_vf_mc_hashes > 30) ||
  4062. (vf_data->flags & IGB_VF_FLAG_MULTI_PROMISC)) {
  4063. vmolr |= E1000_VMOLR_MPME;
  4064. } else if (vf_data->num_vf_mc_hashes) {
  4065. vmolr |= E1000_VMOLR_ROMPE;
  4066. for (j = 0; j < vf_data->num_vf_mc_hashes; j++)
  4067. igb_mta_set(hw, vf_data->vf_mc_hashes[j]);
  4068. }
  4069. wr32(E1000_VMOLR(i), vmolr);
  4070. }
  4071. }
  4072. static void igb_clear_vf_vfta(struct igb_adapter *adapter, u32 vf)
  4073. {
  4074. struct e1000_hw *hw = &adapter->hw;
  4075. u32 pool_mask, reg, vid;
  4076. int i;
  4077. pool_mask = 1 << (E1000_VLVF_POOLSEL_SHIFT + vf);
  4078. /* Find the vlan filter for this id */
  4079. for (i = 0; i < E1000_VLVF_ARRAY_SIZE; i++) {
  4080. reg = rd32(E1000_VLVF(i));
  4081. /* remove the vf from the pool */
  4082. reg &= ~pool_mask;
  4083. /* if pool is empty then remove entry from vfta */
  4084. if (!(reg & E1000_VLVF_POOLSEL_MASK) &&
  4085. (reg & E1000_VLVF_VLANID_ENABLE)) {
  4086. reg = 0;
  4087. vid = reg & E1000_VLVF_VLANID_MASK;
  4088. igb_vfta_set(hw, vid, false);
  4089. }
  4090. wr32(E1000_VLVF(i), reg);
  4091. }
  4092. adapter->vf_data[vf].vlans_enabled = 0;
  4093. }
  4094. static s32 igb_vlvf_set(struct igb_adapter *adapter, u32 vid, bool add, u32 vf)
  4095. {
  4096. struct e1000_hw *hw = &adapter->hw;
  4097. u32 reg, i;
  4098. /* The vlvf table only exists on 82576 hardware and newer */
  4099. if (hw->mac.type < e1000_82576)
  4100. return -1;
  4101. /* we only need to do this if VMDq is enabled */
  4102. if (!adapter->vfs_allocated_count)
  4103. return -1;
  4104. /* Find the vlan filter for this id */
  4105. for (i = 0; i < E1000_VLVF_ARRAY_SIZE; i++) {
  4106. reg = rd32(E1000_VLVF(i));
  4107. if ((reg & E1000_VLVF_VLANID_ENABLE) &&
  4108. vid == (reg & E1000_VLVF_VLANID_MASK))
  4109. break;
  4110. }
  4111. if (add) {
  4112. if (i == E1000_VLVF_ARRAY_SIZE) {
  4113. /* Did not find a matching VLAN ID entry that was
  4114. * enabled. Search for a free filter entry, i.e.
  4115. * one without the enable bit set
  4116. */
  4117. for (i = 0; i < E1000_VLVF_ARRAY_SIZE; i++) {
  4118. reg = rd32(E1000_VLVF(i));
  4119. if (!(reg & E1000_VLVF_VLANID_ENABLE))
  4120. break;
  4121. }
  4122. }
  4123. if (i < E1000_VLVF_ARRAY_SIZE) {
  4124. /* Found an enabled/available entry */
  4125. reg |= 1 << (E1000_VLVF_POOLSEL_SHIFT + vf);
  4126. /* if !enabled we need to set this up in vfta */
  4127. if (!(reg & E1000_VLVF_VLANID_ENABLE)) {
  4128. /* add VID to filter table */
  4129. igb_vfta_set(hw, vid, true);
  4130. reg |= E1000_VLVF_VLANID_ENABLE;
  4131. }
  4132. reg &= ~E1000_VLVF_VLANID_MASK;
  4133. reg |= vid;
  4134. wr32(E1000_VLVF(i), reg);
  4135. /* do not modify RLPML for PF devices */
  4136. if (vf >= adapter->vfs_allocated_count)
  4137. return 0;
  4138. if (!adapter->vf_data[vf].vlans_enabled) {
  4139. u32 size;
  4140. reg = rd32(E1000_VMOLR(vf));
  4141. size = reg & E1000_VMOLR_RLPML_MASK;
  4142. size += 4;
  4143. reg &= ~E1000_VMOLR_RLPML_MASK;
  4144. reg |= size;
  4145. wr32(E1000_VMOLR(vf), reg);
  4146. }
  4147. adapter->vf_data[vf].vlans_enabled++;
  4148. return 0;
  4149. }
  4150. } else {
  4151. if (i < E1000_VLVF_ARRAY_SIZE) {
  4152. /* remove vf from the pool */
  4153. reg &= ~(1 << (E1000_VLVF_POOLSEL_SHIFT + vf));
  4154. /* if pool is empty then remove entry from vfta */
  4155. if (!(reg & E1000_VLVF_POOLSEL_MASK)) {
  4156. reg = 0;
  4157. igb_vfta_set(hw, vid, false);
  4158. }
  4159. wr32(E1000_VLVF(i), reg);
  4160. /* do not modify RLPML for PF devices */
  4161. if (vf >= adapter->vfs_allocated_count)
  4162. return 0;
  4163. adapter->vf_data[vf].vlans_enabled--;
  4164. if (!adapter->vf_data[vf].vlans_enabled) {
  4165. u32 size;
  4166. reg = rd32(E1000_VMOLR(vf));
  4167. size = reg & E1000_VMOLR_RLPML_MASK;
  4168. size -= 4;
  4169. reg &= ~E1000_VMOLR_RLPML_MASK;
  4170. reg |= size;
  4171. wr32(E1000_VMOLR(vf), reg);
  4172. }
  4173. }
  4174. }
  4175. return 0;
  4176. }
  4177. static void igb_set_vmvir(struct igb_adapter *adapter, u32 vid, u32 vf)
  4178. {
  4179. struct e1000_hw *hw = &adapter->hw;
  4180. if (vid)
  4181. wr32(E1000_VMVIR(vf), (vid | E1000_VMVIR_VLANA_DEFAULT));
  4182. else
  4183. wr32(E1000_VMVIR(vf), 0);
  4184. }
  4185. static int igb_ndo_set_vf_vlan(struct net_device *netdev,
  4186. int vf, u16 vlan, u8 qos)
  4187. {
  4188. int err = 0;
  4189. struct igb_adapter *adapter = netdev_priv(netdev);
  4190. if ((vf >= adapter->vfs_allocated_count) || (vlan > 4095) || (qos > 7))
  4191. return -EINVAL;
  4192. if (vlan || qos) {
  4193. err = igb_vlvf_set(adapter, vlan, !!vlan, vf);
  4194. if (err)
  4195. goto out;
  4196. igb_set_vmvir(adapter, vlan | (qos << VLAN_PRIO_SHIFT), vf);
  4197. igb_set_vmolr(adapter, vf, !vlan);
  4198. adapter->vf_data[vf].pf_vlan = vlan;
  4199. adapter->vf_data[vf].pf_qos = qos;
  4200. dev_info(&adapter->pdev->dev,
  4201. "Setting VLAN %d, QOS 0x%x on VF %d\n", vlan, qos, vf);
  4202. if (test_bit(__IGB_DOWN, &adapter->state)) {
  4203. dev_warn(&adapter->pdev->dev,
  4204. "The VF VLAN has been set,"
  4205. " but the PF device is not up.\n");
  4206. dev_warn(&adapter->pdev->dev,
  4207. "Bring the PF device up before"
  4208. " attempting to use the VF device.\n");
  4209. }
  4210. } else {
  4211. igb_vlvf_set(adapter, adapter->vf_data[vf].pf_vlan,
  4212. false, vf);
  4213. igb_set_vmvir(adapter, vlan, vf);
  4214. igb_set_vmolr(adapter, vf, true);
  4215. adapter->vf_data[vf].pf_vlan = 0;
  4216. adapter->vf_data[vf].pf_qos = 0;
  4217. }
  4218. out:
  4219. return err;
  4220. }
  4221. static int igb_set_vf_vlan(struct igb_adapter *adapter, u32 *msgbuf, u32 vf)
  4222. {
  4223. int add = (msgbuf[0] & E1000_VT_MSGINFO_MASK) >> E1000_VT_MSGINFO_SHIFT;
  4224. int vid = (msgbuf[1] & E1000_VLVF_VLANID_MASK);
  4225. return igb_vlvf_set(adapter, vid, add, vf);
  4226. }
  4227. static inline void igb_vf_reset(struct igb_adapter *adapter, u32 vf)
  4228. {
  4229. /* clear flags */
  4230. adapter->vf_data[vf].flags &= ~(IGB_VF_FLAG_PF_SET_MAC);
  4231. adapter->vf_data[vf].last_nack = jiffies;
  4232. /* reset offloads to defaults */
  4233. igb_set_vmolr(adapter, vf, true);
  4234. /* reset vlans for device */
  4235. igb_clear_vf_vfta(adapter, vf);
  4236. if (adapter->vf_data[vf].pf_vlan)
  4237. igb_ndo_set_vf_vlan(adapter->netdev, vf,
  4238. adapter->vf_data[vf].pf_vlan,
  4239. adapter->vf_data[vf].pf_qos);
  4240. else
  4241. igb_clear_vf_vfta(adapter, vf);
  4242. /* reset multicast table array for vf */
  4243. adapter->vf_data[vf].num_vf_mc_hashes = 0;
  4244. /* Flush and reset the mta with the new values */
  4245. igb_set_rx_mode(adapter->netdev);
  4246. }
  4247. static void igb_vf_reset_event(struct igb_adapter *adapter, u32 vf)
  4248. {
  4249. unsigned char *vf_mac = adapter->vf_data[vf].vf_mac_addresses;
  4250. /* generate a new mac address as we were hotplug removed/added */
  4251. if (!(adapter->vf_data[vf].flags & IGB_VF_FLAG_PF_SET_MAC))
  4252. random_ether_addr(vf_mac);
  4253. /* process remaining reset events */
  4254. igb_vf_reset(adapter, vf);
  4255. }
  4256. static void igb_vf_reset_msg(struct igb_adapter *adapter, u32 vf)
  4257. {
  4258. struct e1000_hw *hw = &adapter->hw;
  4259. unsigned char *vf_mac = adapter->vf_data[vf].vf_mac_addresses;
  4260. int rar_entry = hw->mac.rar_entry_count - (vf + 1);
  4261. u32 reg, msgbuf[3];
  4262. u8 *addr = (u8 *)(&msgbuf[1]);
  4263. /* process all the same items cleared in a function level reset */
  4264. igb_vf_reset(adapter, vf);
  4265. /* set vf mac address */
  4266. igb_rar_set_qsel(adapter, vf_mac, rar_entry, vf);
  4267. /* enable transmit and receive for vf */
  4268. reg = rd32(E1000_VFTE);
  4269. wr32(E1000_VFTE, reg | (1 << vf));
  4270. reg = rd32(E1000_VFRE);
  4271. wr32(E1000_VFRE, reg | (1 << vf));
  4272. adapter->vf_data[vf].flags = IGB_VF_FLAG_CTS;
  4273. /* reply to reset with ack and vf mac address */
  4274. msgbuf[0] = E1000_VF_RESET | E1000_VT_MSGTYPE_ACK;
  4275. memcpy(addr, vf_mac, 6);
  4276. igb_write_mbx(hw, msgbuf, 3, vf);
  4277. }
  4278. static int igb_set_vf_mac_addr(struct igb_adapter *adapter, u32 *msg, int vf)
  4279. {
  4280. unsigned char *addr = (char *)&msg[1];
  4281. int err = -1;
  4282. if (is_valid_ether_addr(addr))
  4283. err = igb_set_vf_mac(adapter, vf, addr);
  4284. return err;
  4285. }
  4286. static void igb_rcv_ack_from_vf(struct igb_adapter *adapter, u32 vf)
  4287. {
  4288. struct e1000_hw *hw = &adapter->hw;
  4289. struct vf_data_storage *vf_data = &adapter->vf_data[vf];
  4290. u32 msg = E1000_VT_MSGTYPE_NACK;
  4291. /* if device isn't clear to send it shouldn't be reading either */
  4292. if (!(vf_data->flags & IGB_VF_FLAG_CTS) &&
  4293. time_after(jiffies, vf_data->last_nack + (2 * HZ))) {
  4294. igb_write_mbx(hw, &msg, 1, vf);
  4295. vf_data->last_nack = jiffies;
  4296. }
  4297. }
  4298. static void igb_rcv_msg_from_vf(struct igb_adapter *adapter, u32 vf)
  4299. {
  4300. struct pci_dev *pdev = adapter->pdev;
  4301. u32 msgbuf[E1000_VFMAILBOX_SIZE];
  4302. struct e1000_hw *hw = &adapter->hw;
  4303. struct vf_data_storage *vf_data = &adapter->vf_data[vf];
  4304. s32 retval;
  4305. retval = igb_read_mbx(hw, msgbuf, E1000_VFMAILBOX_SIZE, vf);
  4306. if (retval) {
  4307. /* if receive failed revoke VF CTS stats and restart init */
  4308. dev_err(&pdev->dev, "Error receiving message from VF\n");
  4309. vf_data->flags &= ~IGB_VF_FLAG_CTS;
  4310. if (!time_after(jiffies, vf_data->last_nack + (2 * HZ)))
  4311. return;
  4312. goto out;
  4313. }
  4314. /* this is a message we already processed, do nothing */
  4315. if (msgbuf[0] & (E1000_VT_MSGTYPE_ACK | E1000_VT_MSGTYPE_NACK))
  4316. return;
  4317. /*
  4318. * until the vf completes a reset it should not be
  4319. * allowed to start any configuration.
  4320. */
  4321. if (msgbuf[0] == E1000_VF_RESET) {
  4322. igb_vf_reset_msg(adapter, vf);
  4323. return;
  4324. }
  4325. if (!(vf_data->flags & IGB_VF_FLAG_CTS)) {
  4326. if (!time_after(jiffies, vf_data->last_nack + (2 * HZ)))
  4327. return;
  4328. retval = -1;
  4329. goto out;
  4330. }
  4331. switch ((msgbuf[0] & 0xFFFF)) {
  4332. case E1000_VF_SET_MAC_ADDR:
  4333. retval = igb_set_vf_mac_addr(adapter, msgbuf, vf);
  4334. break;
  4335. case E1000_VF_SET_PROMISC:
  4336. retval = igb_set_vf_promisc(adapter, msgbuf, vf);
  4337. break;
  4338. case E1000_VF_SET_MULTICAST:
  4339. retval = igb_set_vf_multicasts(adapter, msgbuf, vf);
  4340. break;
  4341. case E1000_VF_SET_LPE:
  4342. retval = igb_set_vf_rlpml(adapter, msgbuf[1], vf);
  4343. break;
  4344. case E1000_VF_SET_VLAN:
  4345. if (adapter->vf_data[vf].pf_vlan)
  4346. retval = -1;
  4347. else
  4348. retval = igb_set_vf_vlan(adapter, msgbuf, vf);
  4349. break;
  4350. default:
  4351. dev_err(&pdev->dev, "Unhandled Msg %08x\n", msgbuf[0]);
  4352. retval = -1;
  4353. break;
  4354. }
  4355. msgbuf[0] |= E1000_VT_MSGTYPE_CTS;
  4356. out:
  4357. /* notify the VF of the results of what it sent us */
  4358. if (retval)
  4359. msgbuf[0] |= E1000_VT_MSGTYPE_NACK;
  4360. else
  4361. msgbuf[0] |= E1000_VT_MSGTYPE_ACK;
  4362. igb_write_mbx(hw, msgbuf, 1, vf);
  4363. }
  4364. static void igb_msg_task(struct igb_adapter *adapter)
  4365. {
  4366. struct e1000_hw *hw = &adapter->hw;
  4367. u32 vf;
  4368. for (vf = 0; vf < adapter->vfs_allocated_count; vf++) {
  4369. /* process any reset requests */
  4370. if (!igb_check_for_rst(hw, vf))
  4371. igb_vf_reset_event(adapter, vf);
  4372. /* process any messages pending */
  4373. if (!igb_check_for_msg(hw, vf))
  4374. igb_rcv_msg_from_vf(adapter, vf);
  4375. /* process any acks */
  4376. if (!igb_check_for_ack(hw, vf))
  4377. igb_rcv_ack_from_vf(adapter, vf);
  4378. }
  4379. }
  4380. /**
  4381. * igb_set_uta - Set unicast filter table address
  4382. * @adapter: board private structure
  4383. *
  4384. * The unicast table address is a register array of 32-bit registers.
  4385. * The table is meant to be used in a way similar to how the MTA is used
  4386. * however due to certain limitations in the hardware it is necessary to
  4387. * set all the hash bits to 1 and use the VMOLR ROPE bit as a promiscous
  4388. * enable bit to allow vlan tag stripping when promiscous mode is enabled
  4389. **/
  4390. static void igb_set_uta(struct igb_adapter *adapter)
  4391. {
  4392. struct e1000_hw *hw = &adapter->hw;
  4393. int i;
  4394. /* The UTA table only exists on 82576 hardware and newer */
  4395. if (hw->mac.type < e1000_82576)
  4396. return;
  4397. /* we only need to do this if VMDq is enabled */
  4398. if (!adapter->vfs_allocated_count)
  4399. return;
  4400. for (i = 0; i < hw->mac.uta_reg_count; i++)
  4401. array_wr32(E1000_UTA, i, ~0);
  4402. }
  4403. /**
  4404. * igb_intr_msi - Interrupt Handler
  4405. * @irq: interrupt number
  4406. * @data: pointer to a network interface device structure
  4407. **/
  4408. static irqreturn_t igb_intr_msi(int irq, void *data)
  4409. {
  4410. struct igb_adapter *adapter = data;
  4411. struct igb_q_vector *q_vector = adapter->q_vector[0];
  4412. struct e1000_hw *hw = &adapter->hw;
  4413. /* read ICR disables interrupts using IAM */
  4414. u32 icr = rd32(E1000_ICR);
  4415. igb_write_itr(q_vector);
  4416. if (icr & E1000_ICR_DRSTA)
  4417. schedule_work(&adapter->reset_task);
  4418. if (icr & E1000_ICR_DOUTSYNC) {
  4419. /* HW is reporting DMA is out of sync */
  4420. adapter->stats.doosync++;
  4421. }
  4422. if (icr & (E1000_ICR_RXSEQ | E1000_ICR_LSC)) {
  4423. hw->mac.get_link_status = 1;
  4424. if (!test_bit(__IGB_DOWN, &adapter->state))
  4425. mod_timer(&adapter->watchdog_timer, jiffies + 1);
  4426. }
  4427. napi_schedule(&q_vector->napi);
  4428. return IRQ_HANDLED;
  4429. }
  4430. /**
  4431. * igb_intr - Legacy Interrupt Handler
  4432. * @irq: interrupt number
  4433. * @data: pointer to a network interface device structure
  4434. **/
  4435. static irqreturn_t igb_intr(int irq, void *data)
  4436. {
  4437. struct igb_adapter *adapter = data;
  4438. struct igb_q_vector *q_vector = adapter->q_vector[0];
  4439. struct e1000_hw *hw = &adapter->hw;
  4440. /* Interrupt Auto-Mask...upon reading ICR, interrupts are masked. No
  4441. * need for the IMC write */
  4442. u32 icr = rd32(E1000_ICR);
  4443. if (!icr)
  4444. return IRQ_NONE; /* Not our interrupt */
  4445. igb_write_itr(q_vector);
  4446. /* IMS will not auto-mask if INT_ASSERTED is not set, and if it is
  4447. * not set, then the adapter didn't send an interrupt */
  4448. if (!(icr & E1000_ICR_INT_ASSERTED))
  4449. return IRQ_NONE;
  4450. if (icr & E1000_ICR_DRSTA)
  4451. schedule_work(&adapter->reset_task);
  4452. if (icr & E1000_ICR_DOUTSYNC) {
  4453. /* HW is reporting DMA is out of sync */
  4454. adapter->stats.doosync++;
  4455. }
  4456. if (icr & (E1000_ICR_RXSEQ | E1000_ICR_LSC)) {
  4457. hw->mac.get_link_status = 1;
  4458. /* guard against interrupt when we're going down */
  4459. if (!test_bit(__IGB_DOWN, &adapter->state))
  4460. mod_timer(&adapter->watchdog_timer, jiffies + 1);
  4461. }
  4462. napi_schedule(&q_vector->napi);
  4463. return IRQ_HANDLED;
  4464. }
  4465. static inline void igb_ring_irq_enable(struct igb_q_vector *q_vector)
  4466. {
  4467. struct igb_adapter *adapter = q_vector->adapter;
  4468. struct e1000_hw *hw = &adapter->hw;
  4469. if ((q_vector->rx_ring && (adapter->rx_itr_setting & 3)) ||
  4470. (!q_vector->rx_ring && (adapter->tx_itr_setting & 3))) {
  4471. if (!adapter->msix_entries)
  4472. igb_set_itr(adapter);
  4473. else
  4474. igb_update_ring_itr(q_vector);
  4475. }
  4476. if (!test_bit(__IGB_DOWN, &adapter->state)) {
  4477. if (adapter->msix_entries)
  4478. wr32(E1000_EIMS, q_vector->eims_value);
  4479. else
  4480. igb_irq_enable(adapter);
  4481. }
  4482. }
  4483. /**
  4484. * igb_poll - NAPI Rx polling callback
  4485. * @napi: napi polling structure
  4486. * @budget: count of how many packets we should handle
  4487. **/
  4488. static int igb_poll(struct napi_struct *napi, int budget)
  4489. {
  4490. struct igb_q_vector *q_vector = container_of(napi,
  4491. struct igb_q_vector,
  4492. napi);
  4493. int tx_clean_complete = 1, work_done = 0;
  4494. #ifdef CONFIG_IGB_DCA
  4495. if (q_vector->adapter->flags & IGB_FLAG_DCA_ENABLED)
  4496. igb_update_dca(q_vector);
  4497. #endif
  4498. if (q_vector->tx_ring)
  4499. tx_clean_complete = igb_clean_tx_irq(q_vector);
  4500. if (q_vector->rx_ring)
  4501. igb_clean_rx_irq_adv(q_vector, &work_done, budget);
  4502. if (!tx_clean_complete)
  4503. work_done = budget;
  4504. /* If not enough Rx work done, exit the polling mode */
  4505. if (work_done < budget) {
  4506. napi_complete(napi);
  4507. igb_ring_irq_enable(q_vector);
  4508. }
  4509. return work_done;
  4510. }
  4511. /**
  4512. * igb_systim_to_hwtstamp - convert system time value to hw timestamp
  4513. * @adapter: board private structure
  4514. * @shhwtstamps: timestamp structure to update
  4515. * @regval: unsigned 64bit system time value.
  4516. *
  4517. * We need to convert the system time value stored in the RX/TXSTMP registers
  4518. * into a hwtstamp which can be used by the upper level timestamping functions
  4519. */
  4520. static void igb_systim_to_hwtstamp(struct igb_adapter *adapter,
  4521. struct skb_shared_hwtstamps *shhwtstamps,
  4522. u64 regval)
  4523. {
  4524. u64 ns;
  4525. /*
  4526. * The 82580 starts with 1ns at bit 0 in RX/TXSTMPL, shift this up to
  4527. * 24 to match clock shift we setup earlier.
  4528. */
  4529. if (adapter->hw.mac.type == e1000_82580)
  4530. regval <<= IGB_82580_TSYNC_SHIFT;
  4531. ns = timecounter_cyc2time(&adapter->clock, regval);
  4532. timecompare_update(&adapter->compare, ns);
  4533. memset(shhwtstamps, 0, sizeof(struct skb_shared_hwtstamps));
  4534. shhwtstamps->hwtstamp = ns_to_ktime(ns);
  4535. shhwtstamps->syststamp = timecompare_transform(&adapter->compare, ns);
  4536. }
  4537. /**
  4538. * igb_tx_hwtstamp - utility function which checks for TX time stamp
  4539. * @q_vector: pointer to q_vector containing needed info
  4540. * @buffer: pointer to igb_buffer structure
  4541. *
  4542. * If we were asked to do hardware stamping and such a time stamp is
  4543. * available, then it must have been for this skb here because we only
  4544. * allow only one such packet into the queue.
  4545. */
  4546. static void igb_tx_hwtstamp(struct igb_q_vector *q_vector, struct igb_buffer *buffer_info)
  4547. {
  4548. struct igb_adapter *adapter = q_vector->adapter;
  4549. struct e1000_hw *hw = &adapter->hw;
  4550. struct skb_shared_hwtstamps shhwtstamps;
  4551. u64 regval;
  4552. /* if skb does not support hw timestamp or TX stamp not valid exit */
  4553. if (likely(!buffer_info->shtx.hardware) ||
  4554. !(rd32(E1000_TSYNCTXCTL) & E1000_TSYNCTXCTL_VALID))
  4555. return;
  4556. regval = rd32(E1000_TXSTMPL);
  4557. regval |= (u64)rd32(E1000_TXSTMPH) << 32;
  4558. igb_systim_to_hwtstamp(adapter, &shhwtstamps, regval);
  4559. skb_tstamp_tx(buffer_info->skb, &shhwtstamps);
  4560. }
  4561. /**
  4562. * igb_clean_tx_irq - Reclaim resources after transmit completes
  4563. * @q_vector: pointer to q_vector containing needed info
  4564. * returns true if ring is completely cleaned
  4565. **/
  4566. static bool igb_clean_tx_irq(struct igb_q_vector *q_vector)
  4567. {
  4568. struct igb_adapter *adapter = q_vector->adapter;
  4569. struct igb_ring *tx_ring = q_vector->tx_ring;
  4570. struct net_device *netdev = tx_ring->netdev;
  4571. struct e1000_hw *hw = &adapter->hw;
  4572. struct igb_buffer *buffer_info;
  4573. union e1000_adv_tx_desc *tx_desc, *eop_desc;
  4574. unsigned int total_bytes = 0, total_packets = 0;
  4575. unsigned int i, eop, count = 0;
  4576. bool cleaned = false;
  4577. i = tx_ring->next_to_clean;
  4578. eop = tx_ring->buffer_info[i].next_to_watch;
  4579. eop_desc = E1000_TX_DESC_ADV(*tx_ring, eop);
  4580. while ((eop_desc->wb.status & cpu_to_le32(E1000_TXD_STAT_DD)) &&
  4581. (count < tx_ring->count)) {
  4582. for (cleaned = false; !cleaned; count++) {
  4583. tx_desc = E1000_TX_DESC_ADV(*tx_ring, i);
  4584. buffer_info = &tx_ring->buffer_info[i];
  4585. cleaned = (i == eop);
  4586. if (buffer_info->skb) {
  4587. total_bytes += buffer_info->bytecount;
  4588. /* gso_segs is currently only valid for tcp */
  4589. total_packets += buffer_info->gso_segs;
  4590. igb_tx_hwtstamp(q_vector, buffer_info);
  4591. }
  4592. igb_unmap_and_free_tx_resource(tx_ring, buffer_info);
  4593. tx_desc->wb.status = 0;
  4594. i++;
  4595. if (i == tx_ring->count)
  4596. i = 0;
  4597. }
  4598. eop = tx_ring->buffer_info[i].next_to_watch;
  4599. eop_desc = E1000_TX_DESC_ADV(*tx_ring, eop);
  4600. }
  4601. tx_ring->next_to_clean = i;
  4602. if (unlikely(count &&
  4603. netif_carrier_ok(netdev) &&
  4604. igb_desc_unused(tx_ring) >= IGB_TX_QUEUE_WAKE)) {
  4605. /* Make sure that anybody stopping the queue after this
  4606. * sees the new next_to_clean.
  4607. */
  4608. smp_mb();
  4609. if (__netif_subqueue_stopped(netdev, tx_ring->queue_index) &&
  4610. !(test_bit(__IGB_DOWN, &adapter->state))) {
  4611. netif_wake_subqueue(netdev, tx_ring->queue_index);
  4612. tx_ring->tx_stats.restart_queue++;
  4613. }
  4614. }
  4615. if (tx_ring->detect_tx_hung) {
  4616. /* Detect a transmit hang in hardware, this serializes the
  4617. * check with the clearing of time_stamp and movement of i */
  4618. tx_ring->detect_tx_hung = false;
  4619. if (tx_ring->buffer_info[i].time_stamp &&
  4620. time_after(jiffies, tx_ring->buffer_info[i].time_stamp +
  4621. (adapter->tx_timeout_factor * HZ)) &&
  4622. !(rd32(E1000_STATUS) & E1000_STATUS_TXOFF)) {
  4623. /* detected Tx unit hang */
  4624. dev_err(tx_ring->dev,
  4625. "Detected Tx Unit Hang\n"
  4626. " Tx Queue <%d>\n"
  4627. " TDH <%x>\n"
  4628. " TDT <%x>\n"
  4629. " next_to_use <%x>\n"
  4630. " next_to_clean <%x>\n"
  4631. "buffer_info[next_to_clean]\n"
  4632. " time_stamp <%lx>\n"
  4633. " next_to_watch <%x>\n"
  4634. " jiffies <%lx>\n"
  4635. " desc.status <%x>\n",
  4636. tx_ring->queue_index,
  4637. readl(tx_ring->head),
  4638. readl(tx_ring->tail),
  4639. tx_ring->next_to_use,
  4640. tx_ring->next_to_clean,
  4641. tx_ring->buffer_info[eop].time_stamp,
  4642. eop,
  4643. jiffies,
  4644. eop_desc->wb.status);
  4645. netif_stop_subqueue(netdev, tx_ring->queue_index);
  4646. }
  4647. }
  4648. tx_ring->total_bytes += total_bytes;
  4649. tx_ring->total_packets += total_packets;
  4650. tx_ring->tx_stats.bytes += total_bytes;
  4651. tx_ring->tx_stats.packets += total_packets;
  4652. return (count < tx_ring->count);
  4653. }
  4654. /**
  4655. * igb_receive_skb - helper function to handle rx indications
  4656. * @q_vector: structure containing interrupt and ring information
  4657. * @skb: packet to send up
  4658. * @vlan_tag: vlan tag for packet
  4659. **/
  4660. static void igb_receive_skb(struct igb_q_vector *q_vector,
  4661. struct sk_buff *skb,
  4662. u16 vlan_tag)
  4663. {
  4664. struct igb_adapter *adapter = q_vector->adapter;
  4665. if (vlan_tag && adapter->vlgrp)
  4666. vlan_gro_receive(&q_vector->napi, adapter->vlgrp,
  4667. vlan_tag, skb);
  4668. else
  4669. napi_gro_receive(&q_vector->napi, skb);
  4670. }
  4671. static inline void igb_rx_checksum_adv(struct igb_ring *ring,
  4672. u32 status_err, struct sk_buff *skb)
  4673. {
  4674. skb->ip_summed = CHECKSUM_NONE;
  4675. /* Ignore Checksum bit is set or checksum is disabled through ethtool */
  4676. if (!(ring->flags & IGB_RING_FLAG_RX_CSUM) ||
  4677. (status_err & E1000_RXD_STAT_IXSM))
  4678. return;
  4679. /* TCP/UDP checksum error bit is set */
  4680. if (status_err &
  4681. (E1000_RXDEXT_STATERR_TCPE | E1000_RXDEXT_STATERR_IPE)) {
  4682. /*
  4683. * work around errata with sctp packets where the TCPE aka
  4684. * L4E bit is set incorrectly on 64 byte (60 byte w/o crc)
  4685. * packets, (aka let the stack check the crc32c)
  4686. */
  4687. if ((skb->len == 60) &&
  4688. (ring->flags & IGB_RING_FLAG_RX_SCTP_CSUM))
  4689. ring->rx_stats.csum_err++;
  4690. /* let the stack verify checksum errors */
  4691. return;
  4692. }
  4693. /* It must be a TCP or UDP packet with a valid checksum */
  4694. if (status_err & (E1000_RXD_STAT_TCPCS | E1000_RXD_STAT_UDPCS))
  4695. skb->ip_summed = CHECKSUM_UNNECESSARY;
  4696. dev_dbg(ring->dev, "cksum success: bits %08X\n", status_err);
  4697. }
  4698. static void igb_rx_hwtstamp(struct igb_q_vector *q_vector, u32 staterr,
  4699. struct sk_buff *skb)
  4700. {
  4701. struct igb_adapter *adapter = q_vector->adapter;
  4702. struct e1000_hw *hw = &adapter->hw;
  4703. u64 regval;
  4704. /*
  4705. * If this bit is set, then the RX registers contain the time stamp. No
  4706. * other packet will be time stamped until we read these registers, so
  4707. * read the registers to make them available again. Because only one
  4708. * packet can be time stamped at a time, we know that the register
  4709. * values must belong to this one here and therefore we don't need to
  4710. * compare any of the additional attributes stored for it.
  4711. *
  4712. * If nothing went wrong, then it should have a skb_shared_tx that we
  4713. * can turn into a skb_shared_hwtstamps.
  4714. */
  4715. if (staterr & E1000_RXDADV_STAT_TSIP) {
  4716. u32 *stamp = (u32 *)skb->data;
  4717. regval = le32_to_cpu(*(stamp + 2));
  4718. regval |= (u64)le32_to_cpu(*(stamp + 3)) << 32;
  4719. skb_pull(skb, IGB_TS_HDR_LEN);
  4720. } else {
  4721. if(!(rd32(E1000_TSYNCRXCTL) & E1000_TSYNCRXCTL_VALID))
  4722. return;
  4723. regval = rd32(E1000_RXSTMPL);
  4724. regval |= (u64)rd32(E1000_RXSTMPH) << 32;
  4725. }
  4726. igb_systim_to_hwtstamp(adapter, skb_hwtstamps(skb), regval);
  4727. }
  4728. static inline u16 igb_get_hlen(struct igb_ring *rx_ring,
  4729. union e1000_adv_rx_desc *rx_desc)
  4730. {
  4731. /* HW will not DMA in data larger than the given buffer, even if it
  4732. * parses the (NFS, of course) header to be larger. In that case, it
  4733. * fills the header buffer and spills the rest into the page.
  4734. */
  4735. u16 hlen = (le16_to_cpu(rx_desc->wb.lower.lo_dword.hdr_info) &
  4736. E1000_RXDADV_HDRBUFLEN_MASK) >> E1000_RXDADV_HDRBUFLEN_SHIFT;
  4737. if (hlen > rx_ring->rx_buffer_len)
  4738. hlen = rx_ring->rx_buffer_len;
  4739. return hlen;
  4740. }
  4741. static bool igb_clean_rx_irq_adv(struct igb_q_vector *q_vector,
  4742. int *work_done, int budget)
  4743. {
  4744. struct igb_ring *rx_ring = q_vector->rx_ring;
  4745. struct net_device *netdev = rx_ring->netdev;
  4746. struct device *dev = rx_ring->dev;
  4747. union e1000_adv_rx_desc *rx_desc , *next_rxd;
  4748. struct igb_buffer *buffer_info , *next_buffer;
  4749. struct sk_buff *skb;
  4750. bool cleaned = false;
  4751. int cleaned_count = 0;
  4752. int current_node = numa_node_id();
  4753. unsigned int total_bytes = 0, total_packets = 0;
  4754. unsigned int i;
  4755. u32 staterr;
  4756. u16 length;
  4757. u16 vlan_tag;
  4758. i = rx_ring->next_to_clean;
  4759. buffer_info = &rx_ring->buffer_info[i];
  4760. rx_desc = E1000_RX_DESC_ADV(*rx_ring, i);
  4761. staterr = le32_to_cpu(rx_desc->wb.upper.status_error);
  4762. while (staterr & E1000_RXD_STAT_DD) {
  4763. if (*work_done >= budget)
  4764. break;
  4765. (*work_done)++;
  4766. skb = buffer_info->skb;
  4767. prefetch(skb->data - NET_IP_ALIGN);
  4768. buffer_info->skb = NULL;
  4769. i++;
  4770. if (i == rx_ring->count)
  4771. i = 0;
  4772. next_rxd = E1000_RX_DESC_ADV(*rx_ring, i);
  4773. prefetch(next_rxd);
  4774. next_buffer = &rx_ring->buffer_info[i];
  4775. length = le16_to_cpu(rx_desc->wb.upper.length);
  4776. cleaned = true;
  4777. cleaned_count++;
  4778. if (buffer_info->dma) {
  4779. dma_unmap_single(dev, buffer_info->dma,
  4780. rx_ring->rx_buffer_len,
  4781. DMA_FROM_DEVICE);
  4782. buffer_info->dma = 0;
  4783. if (rx_ring->rx_buffer_len >= IGB_RXBUFFER_1024) {
  4784. skb_put(skb, length);
  4785. goto send_up;
  4786. }
  4787. skb_put(skb, igb_get_hlen(rx_ring, rx_desc));
  4788. }
  4789. if (length) {
  4790. dma_unmap_page(dev, buffer_info->page_dma,
  4791. PAGE_SIZE / 2, DMA_FROM_DEVICE);
  4792. buffer_info->page_dma = 0;
  4793. skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags,
  4794. buffer_info->page,
  4795. buffer_info->page_offset,
  4796. length);
  4797. if ((page_count(buffer_info->page) != 1) ||
  4798. (page_to_nid(buffer_info->page) != current_node))
  4799. buffer_info->page = NULL;
  4800. else
  4801. get_page(buffer_info->page);
  4802. skb->len += length;
  4803. skb->data_len += length;
  4804. skb->truesize += length;
  4805. }
  4806. if (!(staterr & E1000_RXD_STAT_EOP)) {
  4807. buffer_info->skb = next_buffer->skb;
  4808. buffer_info->dma = next_buffer->dma;
  4809. next_buffer->skb = skb;
  4810. next_buffer->dma = 0;
  4811. goto next_desc;
  4812. }
  4813. send_up:
  4814. if (staterr & E1000_RXDEXT_ERR_FRAME_ERR_MASK) {
  4815. dev_kfree_skb_irq(skb);
  4816. goto next_desc;
  4817. }
  4818. if (staterr & (E1000_RXDADV_STAT_TSIP | E1000_RXDADV_STAT_TS))
  4819. igb_rx_hwtstamp(q_vector, staterr, skb);
  4820. total_bytes += skb->len;
  4821. total_packets++;
  4822. igb_rx_checksum_adv(rx_ring, staterr, skb);
  4823. skb->protocol = eth_type_trans(skb, netdev);
  4824. skb_record_rx_queue(skb, rx_ring->queue_index);
  4825. vlan_tag = ((staterr & E1000_RXD_STAT_VP) ?
  4826. le16_to_cpu(rx_desc->wb.upper.vlan) : 0);
  4827. igb_receive_skb(q_vector, skb, vlan_tag);
  4828. next_desc:
  4829. rx_desc->wb.upper.status_error = 0;
  4830. /* return some buffers to hardware, one at a time is too slow */
  4831. if (cleaned_count >= IGB_RX_BUFFER_WRITE) {
  4832. igb_alloc_rx_buffers_adv(rx_ring, cleaned_count);
  4833. cleaned_count = 0;
  4834. }
  4835. /* use prefetched values */
  4836. rx_desc = next_rxd;
  4837. buffer_info = next_buffer;
  4838. staterr = le32_to_cpu(rx_desc->wb.upper.status_error);
  4839. }
  4840. rx_ring->next_to_clean = i;
  4841. cleaned_count = igb_desc_unused(rx_ring);
  4842. if (cleaned_count)
  4843. igb_alloc_rx_buffers_adv(rx_ring, cleaned_count);
  4844. rx_ring->total_packets += total_packets;
  4845. rx_ring->total_bytes += total_bytes;
  4846. rx_ring->rx_stats.packets += total_packets;
  4847. rx_ring->rx_stats.bytes += total_bytes;
  4848. return cleaned;
  4849. }
  4850. /**
  4851. * igb_alloc_rx_buffers_adv - Replace used receive buffers; packet split
  4852. * @adapter: address of board private structure
  4853. **/
  4854. void igb_alloc_rx_buffers_adv(struct igb_ring *rx_ring, int cleaned_count)
  4855. {
  4856. struct net_device *netdev = rx_ring->netdev;
  4857. union e1000_adv_rx_desc *rx_desc;
  4858. struct igb_buffer *buffer_info;
  4859. struct sk_buff *skb;
  4860. unsigned int i;
  4861. int bufsz;
  4862. i = rx_ring->next_to_use;
  4863. buffer_info = &rx_ring->buffer_info[i];
  4864. bufsz = rx_ring->rx_buffer_len;
  4865. while (cleaned_count--) {
  4866. rx_desc = E1000_RX_DESC_ADV(*rx_ring, i);
  4867. if ((bufsz < IGB_RXBUFFER_1024) && !buffer_info->page_dma) {
  4868. if (!buffer_info->page) {
  4869. buffer_info->page = netdev_alloc_page(netdev);
  4870. if (!buffer_info->page) {
  4871. rx_ring->rx_stats.alloc_failed++;
  4872. goto no_buffers;
  4873. }
  4874. buffer_info->page_offset = 0;
  4875. } else {
  4876. buffer_info->page_offset ^= PAGE_SIZE / 2;
  4877. }
  4878. buffer_info->page_dma =
  4879. dma_map_page(rx_ring->dev, buffer_info->page,
  4880. buffer_info->page_offset,
  4881. PAGE_SIZE / 2,
  4882. DMA_FROM_DEVICE);
  4883. if (dma_mapping_error(rx_ring->dev,
  4884. buffer_info->page_dma)) {
  4885. buffer_info->page_dma = 0;
  4886. rx_ring->rx_stats.alloc_failed++;
  4887. goto no_buffers;
  4888. }
  4889. }
  4890. skb = buffer_info->skb;
  4891. if (!skb) {
  4892. skb = netdev_alloc_skb_ip_align(netdev, bufsz);
  4893. if (!skb) {
  4894. rx_ring->rx_stats.alloc_failed++;
  4895. goto no_buffers;
  4896. }
  4897. buffer_info->skb = skb;
  4898. }
  4899. if (!buffer_info->dma) {
  4900. buffer_info->dma = dma_map_single(rx_ring->dev,
  4901. skb->data,
  4902. bufsz,
  4903. DMA_FROM_DEVICE);
  4904. if (dma_mapping_error(rx_ring->dev,
  4905. buffer_info->dma)) {
  4906. buffer_info->dma = 0;
  4907. rx_ring->rx_stats.alloc_failed++;
  4908. goto no_buffers;
  4909. }
  4910. }
  4911. /* Refresh the desc even if buffer_addrs didn't change because
  4912. * each write-back erases this info. */
  4913. if (bufsz < IGB_RXBUFFER_1024) {
  4914. rx_desc->read.pkt_addr =
  4915. cpu_to_le64(buffer_info->page_dma);
  4916. rx_desc->read.hdr_addr = cpu_to_le64(buffer_info->dma);
  4917. } else {
  4918. rx_desc->read.pkt_addr = cpu_to_le64(buffer_info->dma);
  4919. rx_desc->read.hdr_addr = 0;
  4920. }
  4921. i++;
  4922. if (i == rx_ring->count)
  4923. i = 0;
  4924. buffer_info = &rx_ring->buffer_info[i];
  4925. }
  4926. no_buffers:
  4927. if (rx_ring->next_to_use != i) {
  4928. rx_ring->next_to_use = i;
  4929. if (i == 0)
  4930. i = (rx_ring->count - 1);
  4931. else
  4932. i--;
  4933. /* Force memory writes to complete before letting h/w
  4934. * know there are new descriptors to fetch. (Only
  4935. * applicable for weak-ordered memory model archs,
  4936. * such as IA-64). */
  4937. wmb();
  4938. writel(i, rx_ring->tail);
  4939. }
  4940. }
  4941. /**
  4942. * igb_mii_ioctl -
  4943. * @netdev:
  4944. * @ifreq:
  4945. * @cmd:
  4946. **/
  4947. static int igb_mii_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
  4948. {
  4949. struct igb_adapter *adapter = netdev_priv(netdev);
  4950. struct mii_ioctl_data *data = if_mii(ifr);
  4951. if (adapter->hw.phy.media_type != e1000_media_type_copper)
  4952. return -EOPNOTSUPP;
  4953. switch (cmd) {
  4954. case SIOCGMIIPHY:
  4955. data->phy_id = adapter->hw.phy.addr;
  4956. break;
  4957. case SIOCGMIIREG:
  4958. if (igb_read_phy_reg(&adapter->hw, data->reg_num & 0x1F,
  4959. &data->val_out))
  4960. return -EIO;
  4961. break;
  4962. case SIOCSMIIREG:
  4963. default:
  4964. return -EOPNOTSUPP;
  4965. }
  4966. return 0;
  4967. }
  4968. /**
  4969. * igb_hwtstamp_ioctl - control hardware time stamping
  4970. * @netdev:
  4971. * @ifreq:
  4972. * @cmd:
  4973. *
  4974. * Outgoing time stamping can be enabled and disabled. Play nice and
  4975. * disable it when requested, although it shouldn't case any overhead
  4976. * when no packet needs it. At most one packet in the queue may be
  4977. * marked for time stamping, otherwise it would be impossible to tell
  4978. * for sure to which packet the hardware time stamp belongs.
  4979. *
  4980. * Incoming time stamping has to be configured via the hardware
  4981. * filters. Not all combinations are supported, in particular event
  4982. * type has to be specified. Matching the kind of event packet is
  4983. * not supported, with the exception of "all V2 events regardless of
  4984. * level 2 or 4".
  4985. *
  4986. **/
  4987. static int igb_hwtstamp_ioctl(struct net_device *netdev,
  4988. struct ifreq *ifr, int cmd)
  4989. {
  4990. struct igb_adapter *adapter = netdev_priv(netdev);
  4991. struct e1000_hw *hw = &adapter->hw;
  4992. struct hwtstamp_config config;
  4993. u32 tsync_tx_ctl = E1000_TSYNCTXCTL_ENABLED;
  4994. u32 tsync_rx_ctl = E1000_TSYNCRXCTL_ENABLED;
  4995. u32 tsync_rx_cfg = 0;
  4996. bool is_l4 = false;
  4997. bool is_l2 = false;
  4998. u32 regval;
  4999. if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
  5000. return -EFAULT;
  5001. /* reserved for future extensions */
  5002. if (config.flags)
  5003. return -EINVAL;
  5004. switch (config.tx_type) {
  5005. case HWTSTAMP_TX_OFF:
  5006. tsync_tx_ctl = 0;
  5007. case HWTSTAMP_TX_ON:
  5008. break;
  5009. default:
  5010. return -ERANGE;
  5011. }
  5012. switch (config.rx_filter) {
  5013. case HWTSTAMP_FILTER_NONE:
  5014. tsync_rx_ctl = 0;
  5015. break;
  5016. case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
  5017. case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
  5018. case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
  5019. case HWTSTAMP_FILTER_ALL:
  5020. /*
  5021. * register TSYNCRXCFG must be set, therefore it is not
  5022. * possible to time stamp both Sync and Delay_Req messages
  5023. * => fall back to time stamping all packets
  5024. */
  5025. tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_ALL;
  5026. config.rx_filter = HWTSTAMP_FILTER_ALL;
  5027. break;
  5028. case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
  5029. tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_L4_V1;
  5030. tsync_rx_cfg = E1000_TSYNCRXCFG_PTP_V1_SYNC_MESSAGE;
  5031. is_l4 = true;
  5032. break;
  5033. case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
  5034. tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_L4_V1;
  5035. tsync_rx_cfg = E1000_TSYNCRXCFG_PTP_V1_DELAY_REQ_MESSAGE;
  5036. is_l4 = true;
  5037. break;
  5038. case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
  5039. case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
  5040. tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_L2_L4_V2;
  5041. tsync_rx_cfg = E1000_TSYNCRXCFG_PTP_V2_SYNC_MESSAGE;
  5042. is_l2 = true;
  5043. is_l4 = true;
  5044. config.rx_filter = HWTSTAMP_FILTER_SOME;
  5045. break;
  5046. case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
  5047. case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
  5048. tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_L2_L4_V2;
  5049. tsync_rx_cfg = E1000_TSYNCRXCFG_PTP_V2_DELAY_REQ_MESSAGE;
  5050. is_l2 = true;
  5051. is_l4 = true;
  5052. config.rx_filter = HWTSTAMP_FILTER_SOME;
  5053. break;
  5054. case HWTSTAMP_FILTER_PTP_V2_EVENT:
  5055. case HWTSTAMP_FILTER_PTP_V2_SYNC:
  5056. case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
  5057. tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_EVENT_V2;
  5058. config.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
  5059. is_l2 = true;
  5060. break;
  5061. default:
  5062. return -ERANGE;
  5063. }
  5064. if (hw->mac.type == e1000_82575) {
  5065. if (tsync_rx_ctl | tsync_tx_ctl)
  5066. return -EINVAL;
  5067. return 0;
  5068. }
  5069. /*
  5070. * Per-packet timestamping only works if all packets are
  5071. * timestamped, so enable timestamping in all packets as
  5072. * long as one rx filter was configured.
  5073. */
  5074. if ((hw->mac.type == e1000_82580) && tsync_rx_ctl) {
  5075. tsync_rx_ctl = E1000_TSYNCRXCTL_ENABLED;
  5076. tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_ALL;
  5077. }
  5078. /* enable/disable TX */
  5079. regval = rd32(E1000_TSYNCTXCTL);
  5080. regval &= ~E1000_TSYNCTXCTL_ENABLED;
  5081. regval |= tsync_tx_ctl;
  5082. wr32(E1000_TSYNCTXCTL, regval);
  5083. /* enable/disable RX */
  5084. regval = rd32(E1000_TSYNCRXCTL);
  5085. regval &= ~(E1000_TSYNCRXCTL_ENABLED | E1000_TSYNCRXCTL_TYPE_MASK);
  5086. regval |= tsync_rx_ctl;
  5087. wr32(E1000_TSYNCRXCTL, regval);
  5088. /* define which PTP packets are time stamped */
  5089. wr32(E1000_TSYNCRXCFG, tsync_rx_cfg);
  5090. /* define ethertype filter for timestamped packets */
  5091. if (is_l2)
  5092. wr32(E1000_ETQF(3),
  5093. (E1000_ETQF_FILTER_ENABLE | /* enable filter */
  5094. E1000_ETQF_1588 | /* enable timestamping */
  5095. ETH_P_1588)); /* 1588 eth protocol type */
  5096. else
  5097. wr32(E1000_ETQF(3), 0);
  5098. #define PTP_PORT 319
  5099. /* L4 Queue Filter[3]: filter by destination port and protocol */
  5100. if (is_l4) {
  5101. u32 ftqf = (IPPROTO_UDP /* UDP */
  5102. | E1000_FTQF_VF_BP /* VF not compared */
  5103. | E1000_FTQF_1588_TIME_STAMP /* Enable Timestamping */
  5104. | E1000_FTQF_MASK); /* mask all inputs */
  5105. ftqf &= ~E1000_FTQF_MASK_PROTO_BP; /* enable protocol check */
  5106. wr32(E1000_IMIR(3), htons(PTP_PORT));
  5107. wr32(E1000_IMIREXT(3),
  5108. (E1000_IMIREXT_SIZE_BP | E1000_IMIREXT_CTRL_BP));
  5109. if (hw->mac.type == e1000_82576) {
  5110. /* enable source port check */
  5111. wr32(E1000_SPQF(3), htons(PTP_PORT));
  5112. ftqf &= ~E1000_FTQF_MASK_SOURCE_PORT_BP;
  5113. }
  5114. wr32(E1000_FTQF(3), ftqf);
  5115. } else {
  5116. wr32(E1000_FTQF(3), E1000_FTQF_MASK);
  5117. }
  5118. wrfl();
  5119. adapter->hwtstamp_config = config;
  5120. /* clear TX/RX time stamp registers, just to be sure */
  5121. regval = rd32(E1000_TXSTMPH);
  5122. regval = rd32(E1000_RXSTMPH);
  5123. return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
  5124. -EFAULT : 0;
  5125. }
  5126. /**
  5127. * igb_ioctl -
  5128. * @netdev:
  5129. * @ifreq:
  5130. * @cmd:
  5131. **/
  5132. static int igb_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
  5133. {
  5134. switch (cmd) {
  5135. case SIOCGMIIPHY:
  5136. case SIOCGMIIREG:
  5137. case SIOCSMIIREG:
  5138. return igb_mii_ioctl(netdev, ifr, cmd);
  5139. case SIOCSHWTSTAMP:
  5140. return igb_hwtstamp_ioctl(netdev, ifr, cmd);
  5141. default:
  5142. return -EOPNOTSUPP;
  5143. }
  5144. }
  5145. s32 igb_read_pcie_cap_reg(struct e1000_hw *hw, u32 reg, u16 *value)
  5146. {
  5147. struct igb_adapter *adapter = hw->back;
  5148. u16 cap_offset;
  5149. cap_offset = pci_find_capability(adapter->pdev, PCI_CAP_ID_EXP);
  5150. if (!cap_offset)
  5151. return -E1000_ERR_CONFIG;
  5152. pci_read_config_word(adapter->pdev, cap_offset + reg, value);
  5153. return 0;
  5154. }
  5155. s32 igb_write_pcie_cap_reg(struct e1000_hw *hw, u32 reg, u16 *value)
  5156. {
  5157. struct igb_adapter *adapter = hw->back;
  5158. u16 cap_offset;
  5159. cap_offset = pci_find_capability(adapter->pdev, PCI_CAP_ID_EXP);
  5160. if (!cap_offset)
  5161. return -E1000_ERR_CONFIG;
  5162. pci_write_config_word(adapter->pdev, cap_offset + reg, *value);
  5163. return 0;
  5164. }
  5165. static void igb_vlan_rx_register(struct net_device *netdev,
  5166. struct vlan_group *grp)
  5167. {
  5168. struct igb_adapter *adapter = netdev_priv(netdev);
  5169. struct e1000_hw *hw = &adapter->hw;
  5170. u32 ctrl, rctl;
  5171. igb_irq_disable(adapter);
  5172. adapter->vlgrp = grp;
  5173. if (grp) {
  5174. /* enable VLAN tag insert/strip */
  5175. ctrl = rd32(E1000_CTRL);
  5176. ctrl |= E1000_CTRL_VME;
  5177. wr32(E1000_CTRL, ctrl);
  5178. /* Disable CFI check */
  5179. rctl = rd32(E1000_RCTL);
  5180. rctl &= ~E1000_RCTL_CFIEN;
  5181. wr32(E1000_RCTL, rctl);
  5182. } else {
  5183. /* disable VLAN tag insert/strip */
  5184. ctrl = rd32(E1000_CTRL);
  5185. ctrl &= ~E1000_CTRL_VME;
  5186. wr32(E1000_CTRL, ctrl);
  5187. }
  5188. igb_rlpml_set(adapter);
  5189. if (!test_bit(__IGB_DOWN, &adapter->state))
  5190. igb_irq_enable(adapter);
  5191. }
  5192. static void igb_vlan_rx_add_vid(struct net_device *netdev, u16 vid)
  5193. {
  5194. struct igb_adapter *adapter = netdev_priv(netdev);
  5195. struct e1000_hw *hw = &adapter->hw;
  5196. int pf_id = adapter->vfs_allocated_count;
  5197. /* attempt to add filter to vlvf array */
  5198. igb_vlvf_set(adapter, vid, true, pf_id);
  5199. /* add the filter since PF can receive vlans w/o entry in vlvf */
  5200. igb_vfta_set(hw, vid, true);
  5201. }
  5202. static void igb_vlan_rx_kill_vid(struct net_device *netdev, u16 vid)
  5203. {
  5204. struct igb_adapter *adapter = netdev_priv(netdev);
  5205. struct e1000_hw *hw = &adapter->hw;
  5206. int pf_id = adapter->vfs_allocated_count;
  5207. s32 err;
  5208. igb_irq_disable(adapter);
  5209. vlan_group_set_device(adapter->vlgrp, vid, NULL);
  5210. if (!test_bit(__IGB_DOWN, &adapter->state))
  5211. igb_irq_enable(adapter);
  5212. /* remove vlan from VLVF table array */
  5213. err = igb_vlvf_set(adapter, vid, false, pf_id);
  5214. /* if vid was not present in VLVF just remove it from table */
  5215. if (err)
  5216. igb_vfta_set(hw, vid, false);
  5217. }
  5218. static void igb_restore_vlan(struct igb_adapter *adapter)
  5219. {
  5220. igb_vlan_rx_register(adapter->netdev, adapter->vlgrp);
  5221. if (adapter->vlgrp) {
  5222. u16 vid;
  5223. for (vid = 0; vid < VLAN_GROUP_ARRAY_LEN; vid++) {
  5224. if (!vlan_group_get_device(adapter->vlgrp, vid))
  5225. continue;
  5226. igb_vlan_rx_add_vid(adapter->netdev, vid);
  5227. }
  5228. }
  5229. }
  5230. int igb_set_spd_dplx(struct igb_adapter *adapter, u16 spddplx)
  5231. {
  5232. struct pci_dev *pdev = adapter->pdev;
  5233. struct e1000_mac_info *mac = &adapter->hw.mac;
  5234. mac->autoneg = 0;
  5235. switch (spddplx) {
  5236. case SPEED_10 + DUPLEX_HALF:
  5237. mac->forced_speed_duplex = ADVERTISE_10_HALF;
  5238. break;
  5239. case SPEED_10 + DUPLEX_FULL:
  5240. mac->forced_speed_duplex = ADVERTISE_10_FULL;
  5241. break;
  5242. case SPEED_100 + DUPLEX_HALF:
  5243. mac->forced_speed_duplex = ADVERTISE_100_HALF;
  5244. break;
  5245. case SPEED_100 + DUPLEX_FULL:
  5246. mac->forced_speed_duplex = ADVERTISE_100_FULL;
  5247. break;
  5248. case SPEED_1000 + DUPLEX_FULL:
  5249. mac->autoneg = 1;
  5250. adapter->hw.phy.autoneg_advertised = ADVERTISE_1000_FULL;
  5251. break;
  5252. case SPEED_1000 + DUPLEX_HALF: /* not supported */
  5253. default:
  5254. dev_err(&pdev->dev, "Unsupported Speed/Duplex configuration\n");
  5255. return -EINVAL;
  5256. }
  5257. return 0;
  5258. }
  5259. static int __igb_shutdown(struct pci_dev *pdev, bool *enable_wake)
  5260. {
  5261. struct net_device *netdev = pci_get_drvdata(pdev);
  5262. struct igb_adapter *adapter = netdev_priv(netdev);
  5263. struct e1000_hw *hw = &adapter->hw;
  5264. u32 ctrl, rctl, status;
  5265. u32 wufc = adapter->wol;
  5266. #ifdef CONFIG_PM
  5267. int retval = 0;
  5268. #endif
  5269. netif_device_detach(netdev);
  5270. if (netif_running(netdev))
  5271. igb_close(netdev);
  5272. igb_clear_interrupt_scheme(adapter);
  5273. #ifdef CONFIG_PM
  5274. retval = pci_save_state(pdev);
  5275. if (retval)
  5276. return retval;
  5277. #endif
  5278. status = rd32(E1000_STATUS);
  5279. if (status & E1000_STATUS_LU)
  5280. wufc &= ~E1000_WUFC_LNKC;
  5281. if (wufc) {
  5282. igb_setup_rctl(adapter);
  5283. igb_set_rx_mode(netdev);
  5284. /* turn on all-multi mode if wake on multicast is enabled */
  5285. if (wufc & E1000_WUFC_MC) {
  5286. rctl = rd32(E1000_RCTL);
  5287. rctl |= E1000_RCTL_MPE;
  5288. wr32(E1000_RCTL, rctl);
  5289. }
  5290. ctrl = rd32(E1000_CTRL);
  5291. /* advertise wake from D3Cold */
  5292. #define E1000_CTRL_ADVD3WUC 0x00100000
  5293. /* phy power management enable */
  5294. #define E1000_CTRL_EN_PHY_PWR_MGMT 0x00200000
  5295. ctrl |= E1000_CTRL_ADVD3WUC;
  5296. wr32(E1000_CTRL, ctrl);
  5297. /* Allow time for pending master requests to run */
  5298. igb_disable_pcie_master(hw);
  5299. wr32(E1000_WUC, E1000_WUC_PME_EN);
  5300. wr32(E1000_WUFC, wufc);
  5301. } else {
  5302. wr32(E1000_WUC, 0);
  5303. wr32(E1000_WUFC, 0);
  5304. }
  5305. *enable_wake = wufc || adapter->en_mng_pt;
  5306. if (!*enable_wake)
  5307. igb_power_down_link(adapter);
  5308. else
  5309. igb_power_up_link(adapter);
  5310. /* Release control of h/w to f/w. If f/w is AMT enabled, this
  5311. * would have already happened in close and is redundant. */
  5312. igb_release_hw_control(adapter);
  5313. pci_disable_device(pdev);
  5314. return 0;
  5315. }
  5316. #ifdef CONFIG_PM
  5317. static int igb_suspend(struct pci_dev *pdev, pm_message_t state)
  5318. {
  5319. int retval;
  5320. bool wake;
  5321. retval = __igb_shutdown(pdev, &wake);
  5322. if (retval)
  5323. return retval;
  5324. if (wake) {
  5325. pci_prepare_to_sleep(pdev);
  5326. } else {
  5327. pci_wake_from_d3(pdev, false);
  5328. pci_set_power_state(pdev, PCI_D3hot);
  5329. }
  5330. return 0;
  5331. }
  5332. static int igb_resume(struct pci_dev *pdev)
  5333. {
  5334. struct net_device *netdev = pci_get_drvdata(pdev);
  5335. struct igb_adapter *adapter = netdev_priv(netdev);
  5336. struct e1000_hw *hw = &adapter->hw;
  5337. u32 err;
  5338. pci_set_power_state(pdev, PCI_D0);
  5339. pci_restore_state(pdev);
  5340. pci_save_state(pdev);
  5341. err = pci_enable_device_mem(pdev);
  5342. if (err) {
  5343. dev_err(&pdev->dev,
  5344. "igb: Cannot enable PCI device from suspend\n");
  5345. return err;
  5346. }
  5347. pci_set_master(pdev);
  5348. pci_enable_wake(pdev, PCI_D3hot, 0);
  5349. pci_enable_wake(pdev, PCI_D3cold, 0);
  5350. if (igb_init_interrupt_scheme(adapter)) {
  5351. dev_err(&pdev->dev, "Unable to allocate memory for queues\n");
  5352. return -ENOMEM;
  5353. }
  5354. igb_reset(adapter);
  5355. /* let the f/w know that the h/w is now under the control of the
  5356. * driver. */
  5357. igb_get_hw_control(adapter);
  5358. wr32(E1000_WUS, ~0);
  5359. if (netif_running(netdev)) {
  5360. err = igb_open(netdev);
  5361. if (err)
  5362. return err;
  5363. }
  5364. netif_device_attach(netdev);
  5365. return 0;
  5366. }
  5367. #endif
  5368. static void igb_shutdown(struct pci_dev *pdev)
  5369. {
  5370. bool wake;
  5371. __igb_shutdown(pdev, &wake);
  5372. if (system_state == SYSTEM_POWER_OFF) {
  5373. pci_wake_from_d3(pdev, wake);
  5374. pci_set_power_state(pdev, PCI_D3hot);
  5375. }
  5376. }
  5377. #ifdef CONFIG_NET_POLL_CONTROLLER
  5378. /*
  5379. * Polling 'interrupt' - used by things like netconsole to send skbs
  5380. * without having to re-enable interrupts. It's not called while
  5381. * the interrupt routine is executing.
  5382. */
  5383. static void igb_netpoll(struct net_device *netdev)
  5384. {
  5385. struct igb_adapter *adapter = netdev_priv(netdev);
  5386. struct e1000_hw *hw = &adapter->hw;
  5387. int i;
  5388. if (!adapter->msix_entries) {
  5389. struct igb_q_vector *q_vector = adapter->q_vector[0];
  5390. igb_irq_disable(adapter);
  5391. napi_schedule(&q_vector->napi);
  5392. return;
  5393. }
  5394. for (i = 0; i < adapter->num_q_vectors; i++) {
  5395. struct igb_q_vector *q_vector = adapter->q_vector[i];
  5396. wr32(E1000_EIMC, q_vector->eims_value);
  5397. napi_schedule(&q_vector->napi);
  5398. }
  5399. }
  5400. #endif /* CONFIG_NET_POLL_CONTROLLER */
  5401. /**
  5402. * igb_io_error_detected - called when PCI error is detected
  5403. * @pdev: Pointer to PCI device
  5404. * @state: The current pci connection state
  5405. *
  5406. * This function is called after a PCI bus error affecting
  5407. * this device has been detected.
  5408. */
  5409. static pci_ers_result_t igb_io_error_detected(struct pci_dev *pdev,
  5410. pci_channel_state_t state)
  5411. {
  5412. struct net_device *netdev = pci_get_drvdata(pdev);
  5413. struct igb_adapter *adapter = netdev_priv(netdev);
  5414. netif_device_detach(netdev);
  5415. if (state == pci_channel_io_perm_failure)
  5416. return PCI_ERS_RESULT_DISCONNECT;
  5417. if (netif_running(netdev))
  5418. igb_down(adapter);
  5419. pci_disable_device(pdev);
  5420. /* Request a slot slot reset. */
  5421. return PCI_ERS_RESULT_NEED_RESET;
  5422. }
  5423. /**
  5424. * igb_io_slot_reset - called after the pci bus has been reset.
  5425. * @pdev: Pointer to PCI device
  5426. *
  5427. * Restart the card from scratch, as if from a cold-boot. Implementation
  5428. * resembles the first-half of the igb_resume routine.
  5429. */
  5430. static pci_ers_result_t igb_io_slot_reset(struct pci_dev *pdev)
  5431. {
  5432. struct net_device *netdev = pci_get_drvdata(pdev);
  5433. struct igb_adapter *adapter = netdev_priv(netdev);
  5434. struct e1000_hw *hw = &adapter->hw;
  5435. pci_ers_result_t result;
  5436. int err;
  5437. if (pci_enable_device_mem(pdev)) {
  5438. dev_err(&pdev->dev,
  5439. "Cannot re-enable PCI device after reset.\n");
  5440. result = PCI_ERS_RESULT_DISCONNECT;
  5441. } else {
  5442. pci_set_master(pdev);
  5443. pci_restore_state(pdev);
  5444. pci_save_state(pdev);
  5445. pci_enable_wake(pdev, PCI_D3hot, 0);
  5446. pci_enable_wake(pdev, PCI_D3cold, 0);
  5447. igb_reset(adapter);
  5448. wr32(E1000_WUS, ~0);
  5449. result = PCI_ERS_RESULT_RECOVERED;
  5450. }
  5451. err = pci_cleanup_aer_uncorrect_error_status(pdev);
  5452. if (err) {
  5453. dev_err(&pdev->dev, "pci_cleanup_aer_uncorrect_error_status "
  5454. "failed 0x%0x\n", err);
  5455. /* non-fatal, continue */
  5456. }
  5457. return result;
  5458. }
  5459. /**
  5460. * igb_io_resume - called when traffic can start flowing again.
  5461. * @pdev: Pointer to PCI device
  5462. *
  5463. * This callback is called when the error recovery driver tells us that
  5464. * its OK to resume normal operation. Implementation resembles the
  5465. * second-half of the igb_resume routine.
  5466. */
  5467. static void igb_io_resume(struct pci_dev *pdev)
  5468. {
  5469. struct net_device *netdev = pci_get_drvdata(pdev);
  5470. struct igb_adapter *adapter = netdev_priv(netdev);
  5471. if (netif_running(netdev)) {
  5472. if (igb_up(adapter)) {
  5473. dev_err(&pdev->dev, "igb_up failed after reset\n");
  5474. return;
  5475. }
  5476. }
  5477. netif_device_attach(netdev);
  5478. /* let the f/w know that the h/w is now under the control of the
  5479. * driver. */
  5480. igb_get_hw_control(adapter);
  5481. }
  5482. static void igb_rar_set_qsel(struct igb_adapter *adapter, u8 *addr, u32 index,
  5483. u8 qsel)
  5484. {
  5485. u32 rar_low, rar_high;
  5486. struct e1000_hw *hw = &adapter->hw;
  5487. /* HW expects these in little endian so we reverse the byte order
  5488. * from network order (big endian) to little endian
  5489. */
  5490. rar_low = ((u32) addr[0] | ((u32) addr[1] << 8) |
  5491. ((u32) addr[2] << 16) | ((u32) addr[3] << 24));
  5492. rar_high = ((u32) addr[4] | ((u32) addr[5] << 8));
  5493. /* Indicate to hardware the Address is Valid. */
  5494. rar_high |= E1000_RAH_AV;
  5495. if (hw->mac.type == e1000_82575)
  5496. rar_high |= E1000_RAH_POOL_1 * qsel;
  5497. else
  5498. rar_high |= E1000_RAH_POOL_1 << qsel;
  5499. wr32(E1000_RAL(index), rar_low);
  5500. wrfl();
  5501. wr32(E1000_RAH(index), rar_high);
  5502. wrfl();
  5503. }
  5504. static int igb_set_vf_mac(struct igb_adapter *adapter,
  5505. int vf, unsigned char *mac_addr)
  5506. {
  5507. struct e1000_hw *hw = &adapter->hw;
  5508. /* VF MAC addresses start at end of receive addresses and moves
  5509. * torwards the first, as a result a collision should not be possible */
  5510. int rar_entry = hw->mac.rar_entry_count - (vf + 1);
  5511. memcpy(adapter->vf_data[vf].vf_mac_addresses, mac_addr, ETH_ALEN);
  5512. igb_rar_set_qsel(adapter, mac_addr, rar_entry, vf);
  5513. return 0;
  5514. }
  5515. static int igb_ndo_set_vf_mac(struct net_device *netdev, int vf, u8 *mac)
  5516. {
  5517. struct igb_adapter *adapter = netdev_priv(netdev);
  5518. if (!is_valid_ether_addr(mac) || (vf >= adapter->vfs_allocated_count))
  5519. return -EINVAL;
  5520. adapter->vf_data[vf].flags |= IGB_VF_FLAG_PF_SET_MAC;
  5521. dev_info(&adapter->pdev->dev, "setting MAC %pM on VF %d\n", mac, vf);
  5522. dev_info(&adapter->pdev->dev, "Reload the VF driver to make this"
  5523. " change effective.");
  5524. if (test_bit(__IGB_DOWN, &adapter->state)) {
  5525. dev_warn(&adapter->pdev->dev, "The VF MAC address has been set,"
  5526. " but the PF device is not up.\n");
  5527. dev_warn(&adapter->pdev->dev, "Bring the PF device up before"
  5528. " attempting to use the VF device.\n");
  5529. }
  5530. return igb_set_vf_mac(adapter, vf, mac);
  5531. }
  5532. static int igb_ndo_set_vf_bw(struct net_device *netdev, int vf, int tx_rate)
  5533. {
  5534. return -EOPNOTSUPP;
  5535. }
  5536. static int igb_ndo_get_vf_config(struct net_device *netdev,
  5537. int vf, struct ifla_vf_info *ivi)
  5538. {
  5539. struct igb_adapter *adapter = netdev_priv(netdev);
  5540. if (vf >= adapter->vfs_allocated_count)
  5541. return -EINVAL;
  5542. ivi->vf = vf;
  5543. memcpy(&ivi->mac, adapter->vf_data[vf].vf_mac_addresses, ETH_ALEN);
  5544. ivi->tx_rate = 0;
  5545. ivi->vlan = adapter->vf_data[vf].pf_vlan;
  5546. ivi->qos = adapter->vf_data[vf].pf_qos;
  5547. return 0;
  5548. }
  5549. static void igb_vmm_control(struct igb_adapter *adapter)
  5550. {
  5551. struct e1000_hw *hw = &adapter->hw;
  5552. u32 reg;
  5553. switch (hw->mac.type) {
  5554. case e1000_82575:
  5555. default:
  5556. /* replication is not supported for 82575 */
  5557. return;
  5558. case e1000_82576:
  5559. /* notify HW that the MAC is adding vlan tags */
  5560. reg = rd32(E1000_DTXCTL);
  5561. reg |= E1000_DTXCTL_VLAN_ADDED;
  5562. wr32(E1000_DTXCTL, reg);
  5563. case e1000_82580:
  5564. /* enable replication vlan tag stripping */
  5565. reg = rd32(E1000_RPLOLR);
  5566. reg |= E1000_RPLOLR_STRVLAN;
  5567. wr32(E1000_RPLOLR, reg);
  5568. case e1000_i350:
  5569. /* none of the above registers are supported by i350 */
  5570. break;
  5571. }
  5572. if (adapter->vfs_allocated_count) {
  5573. igb_vmdq_set_loopback_pf(hw, true);
  5574. igb_vmdq_set_replication_pf(hw, true);
  5575. } else {
  5576. igb_vmdq_set_loopback_pf(hw, false);
  5577. igb_vmdq_set_replication_pf(hw, false);
  5578. }
  5579. }
  5580. /* igb_main.c */