igb_main.c 149 KB

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