igb_main.c 179 KB

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