igb_main.c 158 KB

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