e1000_main.c 136 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978
  1. /*******************************************************************************
  2. Intel PRO/1000 Linux driver
  3. Copyright(c) 1999 - 2006 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. Linux NICS <linux.nics@intel.com>
  18. e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
  19. Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  20. *******************************************************************************/
  21. #include "e1000.h"
  22. #include <net/ip6_checksum.h>
  23. #include <linux/io.h>
  24. #include <linux/prefetch.h>
  25. #include <linux/bitops.h>
  26. #include <linux/if_vlan.h>
  27. /* Intel Media SOC GbE MDIO physical base address */
  28. static unsigned long ce4100_gbe_mdio_base_phy;
  29. /* Intel Media SOC GbE MDIO virtual base address */
  30. void __iomem *ce4100_gbe_mdio_base_virt;
  31. char e1000_driver_name[] = "e1000";
  32. static char e1000_driver_string[] = "Intel(R) PRO/1000 Network Driver";
  33. #define DRV_VERSION "7.3.21-k8-NAPI"
  34. const char e1000_driver_version[] = DRV_VERSION;
  35. static const char e1000_copyright[] = "Copyright (c) 1999-2006 Intel Corporation.";
  36. /* e1000_pci_tbl - PCI Device ID Table
  37. *
  38. * Last entry must be all 0s
  39. *
  40. * Macro expands to...
  41. * {PCI_DEVICE(PCI_VENDOR_ID_INTEL, device_id)}
  42. */
  43. static DEFINE_PCI_DEVICE_TABLE(e1000_pci_tbl) = {
  44. INTEL_E1000_ETHERNET_DEVICE(0x1000),
  45. INTEL_E1000_ETHERNET_DEVICE(0x1001),
  46. INTEL_E1000_ETHERNET_DEVICE(0x1004),
  47. INTEL_E1000_ETHERNET_DEVICE(0x1008),
  48. INTEL_E1000_ETHERNET_DEVICE(0x1009),
  49. INTEL_E1000_ETHERNET_DEVICE(0x100C),
  50. INTEL_E1000_ETHERNET_DEVICE(0x100D),
  51. INTEL_E1000_ETHERNET_DEVICE(0x100E),
  52. INTEL_E1000_ETHERNET_DEVICE(0x100F),
  53. INTEL_E1000_ETHERNET_DEVICE(0x1010),
  54. INTEL_E1000_ETHERNET_DEVICE(0x1011),
  55. INTEL_E1000_ETHERNET_DEVICE(0x1012),
  56. INTEL_E1000_ETHERNET_DEVICE(0x1013),
  57. INTEL_E1000_ETHERNET_DEVICE(0x1014),
  58. INTEL_E1000_ETHERNET_DEVICE(0x1015),
  59. INTEL_E1000_ETHERNET_DEVICE(0x1016),
  60. INTEL_E1000_ETHERNET_DEVICE(0x1017),
  61. INTEL_E1000_ETHERNET_DEVICE(0x1018),
  62. INTEL_E1000_ETHERNET_DEVICE(0x1019),
  63. INTEL_E1000_ETHERNET_DEVICE(0x101A),
  64. INTEL_E1000_ETHERNET_DEVICE(0x101D),
  65. INTEL_E1000_ETHERNET_DEVICE(0x101E),
  66. INTEL_E1000_ETHERNET_DEVICE(0x1026),
  67. INTEL_E1000_ETHERNET_DEVICE(0x1027),
  68. INTEL_E1000_ETHERNET_DEVICE(0x1028),
  69. INTEL_E1000_ETHERNET_DEVICE(0x1075),
  70. INTEL_E1000_ETHERNET_DEVICE(0x1076),
  71. INTEL_E1000_ETHERNET_DEVICE(0x1077),
  72. INTEL_E1000_ETHERNET_DEVICE(0x1078),
  73. INTEL_E1000_ETHERNET_DEVICE(0x1079),
  74. INTEL_E1000_ETHERNET_DEVICE(0x107A),
  75. INTEL_E1000_ETHERNET_DEVICE(0x107B),
  76. INTEL_E1000_ETHERNET_DEVICE(0x107C),
  77. INTEL_E1000_ETHERNET_DEVICE(0x108A),
  78. INTEL_E1000_ETHERNET_DEVICE(0x1099),
  79. INTEL_E1000_ETHERNET_DEVICE(0x10B5),
  80. INTEL_E1000_ETHERNET_DEVICE(0x2E6E),
  81. /* required last entry */
  82. {0,}
  83. };
  84. MODULE_DEVICE_TABLE(pci, e1000_pci_tbl);
  85. int e1000_up(struct e1000_adapter *adapter);
  86. void e1000_down(struct e1000_adapter *adapter);
  87. void e1000_reinit_locked(struct e1000_adapter *adapter);
  88. void e1000_reset(struct e1000_adapter *adapter);
  89. int e1000_setup_all_tx_resources(struct e1000_adapter *adapter);
  90. int e1000_setup_all_rx_resources(struct e1000_adapter *adapter);
  91. void e1000_free_all_tx_resources(struct e1000_adapter *adapter);
  92. void e1000_free_all_rx_resources(struct e1000_adapter *adapter);
  93. static int e1000_setup_tx_resources(struct e1000_adapter *adapter,
  94. struct e1000_tx_ring *txdr);
  95. static int e1000_setup_rx_resources(struct e1000_adapter *adapter,
  96. struct e1000_rx_ring *rxdr);
  97. static void e1000_free_tx_resources(struct e1000_adapter *adapter,
  98. struct e1000_tx_ring *tx_ring);
  99. static void e1000_free_rx_resources(struct e1000_adapter *adapter,
  100. struct e1000_rx_ring *rx_ring);
  101. void e1000_update_stats(struct e1000_adapter *adapter);
  102. static int e1000_init_module(void);
  103. static void e1000_exit_module(void);
  104. static int e1000_probe(struct pci_dev *pdev, const struct pci_device_id *ent);
  105. static void __devexit e1000_remove(struct pci_dev *pdev);
  106. static int e1000_alloc_queues(struct e1000_adapter *adapter);
  107. static int e1000_sw_init(struct e1000_adapter *adapter);
  108. static int e1000_open(struct net_device *netdev);
  109. static int e1000_close(struct net_device *netdev);
  110. static void e1000_configure_tx(struct e1000_adapter *adapter);
  111. static void e1000_configure_rx(struct e1000_adapter *adapter);
  112. static void e1000_setup_rctl(struct e1000_adapter *adapter);
  113. static void e1000_clean_all_tx_rings(struct e1000_adapter *adapter);
  114. static void e1000_clean_all_rx_rings(struct e1000_adapter *adapter);
  115. static void e1000_clean_tx_ring(struct e1000_adapter *adapter,
  116. struct e1000_tx_ring *tx_ring);
  117. static void e1000_clean_rx_ring(struct e1000_adapter *adapter,
  118. struct e1000_rx_ring *rx_ring);
  119. static void e1000_set_rx_mode(struct net_device *netdev);
  120. static void e1000_update_phy_info(unsigned long data);
  121. static void e1000_update_phy_info_task(struct work_struct *work);
  122. static void e1000_watchdog(unsigned long data);
  123. static void e1000_82547_tx_fifo_stall(unsigned long data);
  124. static void e1000_82547_tx_fifo_stall_task(struct work_struct *work);
  125. static netdev_tx_t e1000_xmit_frame(struct sk_buff *skb,
  126. struct net_device *netdev);
  127. static struct net_device_stats * e1000_get_stats(struct net_device *netdev);
  128. static int e1000_change_mtu(struct net_device *netdev, int new_mtu);
  129. static int e1000_set_mac(struct net_device *netdev, void *p);
  130. static irqreturn_t e1000_intr(int irq, void *data);
  131. static bool e1000_clean_tx_irq(struct e1000_adapter *adapter,
  132. struct e1000_tx_ring *tx_ring);
  133. static int e1000_clean(struct napi_struct *napi, int budget);
  134. static bool e1000_clean_rx_irq(struct e1000_adapter *adapter,
  135. struct e1000_rx_ring *rx_ring,
  136. int *work_done, int work_to_do);
  137. static bool e1000_clean_jumbo_rx_irq(struct e1000_adapter *adapter,
  138. struct e1000_rx_ring *rx_ring,
  139. int *work_done, int work_to_do);
  140. static void e1000_alloc_rx_buffers(struct e1000_adapter *adapter,
  141. struct e1000_rx_ring *rx_ring,
  142. int cleaned_count);
  143. static void e1000_alloc_jumbo_rx_buffers(struct e1000_adapter *adapter,
  144. struct e1000_rx_ring *rx_ring,
  145. int cleaned_count);
  146. static int e1000_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd);
  147. static int e1000_mii_ioctl(struct net_device *netdev, struct ifreq *ifr,
  148. int cmd);
  149. static void e1000_enter_82542_rst(struct e1000_adapter *adapter);
  150. static void e1000_leave_82542_rst(struct e1000_adapter *adapter);
  151. static void e1000_tx_timeout(struct net_device *dev);
  152. static void e1000_reset_task(struct work_struct *work);
  153. static void e1000_smartspeed(struct e1000_adapter *adapter);
  154. static int e1000_82547_fifo_workaround(struct e1000_adapter *adapter,
  155. struct sk_buff *skb);
  156. static bool e1000_vlan_used(struct e1000_adapter *adapter);
  157. static void e1000_vlan_mode(struct net_device *netdev, u32 features);
  158. static void e1000_vlan_rx_add_vid(struct net_device *netdev, u16 vid);
  159. static void e1000_vlan_rx_kill_vid(struct net_device *netdev, u16 vid);
  160. static void e1000_restore_vlan(struct e1000_adapter *adapter);
  161. #ifdef CONFIG_PM
  162. static int e1000_suspend(struct pci_dev *pdev, pm_message_t state);
  163. static int e1000_resume(struct pci_dev *pdev);
  164. #endif
  165. static void e1000_shutdown(struct pci_dev *pdev);
  166. #ifdef CONFIG_NET_POLL_CONTROLLER
  167. /* for netdump / net console */
  168. static void e1000_netpoll (struct net_device *netdev);
  169. #endif
  170. #define COPYBREAK_DEFAULT 256
  171. static unsigned int copybreak __read_mostly = COPYBREAK_DEFAULT;
  172. module_param(copybreak, uint, 0644);
  173. MODULE_PARM_DESC(copybreak,
  174. "Maximum size of packet that is copied to a new buffer on receive");
  175. static pci_ers_result_t e1000_io_error_detected(struct pci_dev *pdev,
  176. pci_channel_state_t state);
  177. static pci_ers_result_t e1000_io_slot_reset(struct pci_dev *pdev);
  178. static void e1000_io_resume(struct pci_dev *pdev);
  179. static struct pci_error_handlers e1000_err_handler = {
  180. .error_detected = e1000_io_error_detected,
  181. .slot_reset = e1000_io_slot_reset,
  182. .resume = e1000_io_resume,
  183. };
  184. static struct pci_driver e1000_driver = {
  185. .name = e1000_driver_name,
  186. .id_table = e1000_pci_tbl,
  187. .probe = e1000_probe,
  188. .remove = __devexit_p(e1000_remove),
  189. #ifdef CONFIG_PM
  190. /* Power Management Hooks */
  191. .suspend = e1000_suspend,
  192. .resume = e1000_resume,
  193. #endif
  194. .shutdown = e1000_shutdown,
  195. .err_handler = &e1000_err_handler
  196. };
  197. MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
  198. MODULE_DESCRIPTION("Intel(R) PRO/1000 Network Driver");
  199. MODULE_LICENSE("GPL");
  200. MODULE_VERSION(DRV_VERSION);
  201. static int debug = NETIF_MSG_DRV | NETIF_MSG_PROBE;
  202. module_param(debug, int, 0);
  203. MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
  204. /**
  205. * e1000_get_hw_dev - return device
  206. * used by hardware layer to print debugging information
  207. *
  208. **/
  209. struct net_device *e1000_get_hw_dev(struct e1000_hw *hw)
  210. {
  211. struct e1000_adapter *adapter = hw->back;
  212. return adapter->netdev;
  213. }
  214. /**
  215. * e1000_init_module - Driver Registration Routine
  216. *
  217. * e1000_init_module is the first routine called when the driver is
  218. * loaded. All it does is register with the PCI subsystem.
  219. **/
  220. static int __init e1000_init_module(void)
  221. {
  222. int ret;
  223. pr_info("%s - version %s\n", e1000_driver_string, e1000_driver_version);
  224. pr_info("%s\n", e1000_copyright);
  225. ret = pci_register_driver(&e1000_driver);
  226. if (copybreak != COPYBREAK_DEFAULT) {
  227. if (copybreak == 0)
  228. pr_info("copybreak disabled\n");
  229. else
  230. pr_info("copybreak enabled for "
  231. "packets <= %u bytes\n", copybreak);
  232. }
  233. return ret;
  234. }
  235. module_init(e1000_init_module);
  236. /**
  237. * e1000_exit_module - Driver Exit Cleanup Routine
  238. *
  239. * e1000_exit_module is called just before the driver is removed
  240. * from memory.
  241. **/
  242. static void __exit e1000_exit_module(void)
  243. {
  244. pci_unregister_driver(&e1000_driver);
  245. }
  246. module_exit(e1000_exit_module);
  247. static int e1000_request_irq(struct e1000_adapter *adapter)
  248. {
  249. struct net_device *netdev = adapter->netdev;
  250. irq_handler_t handler = e1000_intr;
  251. int irq_flags = IRQF_SHARED;
  252. int err;
  253. err = request_irq(adapter->pdev->irq, handler, irq_flags, netdev->name,
  254. netdev);
  255. if (err) {
  256. e_err(probe, "Unable to allocate interrupt Error: %d\n", err);
  257. }
  258. return err;
  259. }
  260. static void e1000_free_irq(struct e1000_adapter *adapter)
  261. {
  262. struct net_device *netdev = adapter->netdev;
  263. free_irq(adapter->pdev->irq, netdev);
  264. }
  265. /**
  266. * e1000_irq_disable - Mask off interrupt generation on the NIC
  267. * @adapter: board private structure
  268. **/
  269. static void e1000_irq_disable(struct e1000_adapter *adapter)
  270. {
  271. struct e1000_hw *hw = &adapter->hw;
  272. ew32(IMC, ~0);
  273. E1000_WRITE_FLUSH();
  274. synchronize_irq(adapter->pdev->irq);
  275. }
  276. /**
  277. * e1000_irq_enable - Enable default interrupt generation settings
  278. * @adapter: board private structure
  279. **/
  280. static void e1000_irq_enable(struct e1000_adapter *adapter)
  281. {
  282. struct e1000_hw *hw = &adapter->hw;
  283. ew32(IMS, IMS_ENABLE_MASK);
  284. E1000_WRITE_FLUSH();
  285. }
  286. static void e1000_update_mng_vlan(struct e1000_adapter *adapter)
  287. {
  288. struct e1000_hw *hw = &adapter->hw;
  289. struct net_device *netdev = adapter->netdev;
  290. u16 vid = hw->mng_cookie.vlan_id;
  291. u16 old_vid = adapter->mng_vlan_id;
  292. if (!e1000_vlan_used(adapter))
  293. return;
  294. if (!test_bit(vid, adapter->active_vlans)) {
  295. if (hw->mng_cookie.status &
  296. E1000_MNG_DHCP_COOKIE_STATUS_VLAN_SUPPORT) {
  297. e1000_vlan_rx_add_vid(netdev, vid);
  298. adapter->mng_vlan_id = vid;
  299. } else {
  300. adapter->mng_vlan_id = E1000_MNG_VLAN_NONE;
  301. }
  302. if ((old_vid != (u16)E1000_MNG_VLAN_NONE) &&
  303. (vid != old_vid) &&
  304. !test_bit(old_vid, adapter->active_vlans))
  305. e1000_vlan_rx_kill_vid(netdev, old_vid);
  306. } else {
  307. adapter->mng_vlan_id = vid;
  308. }
  309. }
  310. static void e1000_init_manageability(struct e1000_adapter *adapter)
  311. {
  312. struct e1000_hw *hw = &adapter->hw;
  313. if (adapter->en_mng_pt) {
  314. u32 manc = er32(MANC);
  315. /* disable hardware interception of ARP */
  316. manc &= ~(E1000_MANC_ARP_EN);
  317. ew32(MANC, manc);
  318. }
  319. }
  320. static void e1000_release_manageability(struct e1000_adapter *adapter)
  321. {
  322. struct e1000_hw *hw = &adapter->hw;
  323. if (adapter->en_mng_pt) {
  324. u32 manc = er32(MANC);
  325. /* re-enable hardware interception of ARP */
  326. manc |= E1000_MANC_ARP_EN;
  327. ew32(MANC, manc);
  328. }
  329. }
  330. /**
  331. * e1000_configure - configure the hardware for RX and TX
  332. * @adapter = private board structure
  333. **/
  334. static void e1000_configure(struct e1000_adapter *adapter)
  335. {
  336. struct net_device *netdev = adapter->netdev;
  337. int i;
  338. e1000_set_rx_mode(netdev);
  339. e1000_restore_vlan(adapter);
  340. e1000_init_manageability(adapter);
  341. e1000_configure_tx(adapter);
  342. e1000_setup_rctl(adapter);
  343. e1000_configure_rx(adapter);
  344. /* call E1000_DESC_UNUSED which always leaves
  345. * at least 1 descriptor unused to make sure
  346. * next_to_use != next_to_clean */
  347. for (i = 0; i < adapter->num_rx_queues; i++) {
  348. struct e1000_rx_ring *ring = &adapter->rx_ring[i];
  349. adapter->alloc_rx_buf(adapter, ring,
  350. E1000_DESC_UNUSED(ring));
  351. }
  352. }
  353. int e1000_up(struct e1000_adapter *adapter)
  354. {
  355. struct e1000_hw *hw = &adapter->hw;
  356. /* hardware has been reset, we need to reload some things */
  357. e1000_configure(adapter);
  358. clear_bit(__E1000_DOWN, &adapter->flags);
  359. napi_enable(&adapter->napi);
  360. e1000_irq_enable(adapter);
  361. netif_wake_queue(adapter->netdev);
  362. /* fire a link change interrupt to start the watchdog */
  363. ew32(ICS, E1000_ICS_LSC);
  364. return 0;
  365. }
  366. /**
  367. * e1000_power_up_phy - restore link in case the phy was powered down
  368. * @adapter: address of board private structure
  369. *
  370. * The phy may be powered down to save power and turn off link when the
  371. * driver is unloaded and wake on lan is not enabled (among others)
  372. * *** this routine MUST be followed by a call to e1000_reset ***
  373. *
  374. **/
  375. void e1000_power_up_phy(struct e1000_adapter *adapter)
  376. {
  377. struct e1000_hw *hw = &adapter->hw;
  378. u16 mii_reg = 0;
  379. /* Just clear the power down bit to wake the phy back up */
  380. if (hw->media_type == e1000_media_type_copper) {
  381. /* according to the manual, the phy will retain its
  382. * settings across a power-down/up cycle */
  383. e1000_read_phy_reg(hw, PHY_CTRL, &mii_reg);
  384. mii_reg &= ~MII_CR_POWER_DOWN;
  385. e1000_write_phy_reg(hw, PHY_CTRL, mii_reg);
  386. }
  387. }
  388. static void e1000_power_down_phy(struct e1000_adapter *adapter)
  389. {
  390. struct e1000_hw *hw = &adapter->hw;
  391. /* Power down the PHY so no link is implied when interface is down *
  392. * The PHY cannot be powered down if any of the following is true *
  393. * (a) WoL is enabled
  394. * (b) AMT is active
  395. * (c) SoL/IDER session is active */
  396. if (!adapter->wol && hw->mac_type >= e1000_82540 &&
  397. hw->media_type == e1000_media_type_copper) {
  398. u16 mii_reg = 0;
  399. switch (hw->mac_type) {
  400. case e1000_82540:
  401. case e1000_82545:
  402. case e1000_82545_rev_3:
  403. case e1000_82546:
  404. case e1000_ce4100:
  405. case e1000_82546_rev_3:
  406. case e1000_82541:
  407. case e1000_82541_rev_2:
  408. case e1000_82547:
  409. case e1000_82547_rev_2:
  410. if (er32(MANC) & E1000_MANC_SMBUS_EN)
  411. goto out;
  412. break;
  413. default:
  414. goto out;
  415. }
  416. e1000_read_phy_reg(hw, PHY_CTRL, &mii_reg);
  417. mii_reg |= MII_CR_POWER_DOWN;
  418. e1000_write_phy_reg(hw, PHY_CTRL, mii_reg);
  419. mdelay(1);
  420. }
  421. out:
  422. return;
  423. }
  424. void e1000_down(struct e1000_adapter *adapter)
  425. {
  426. struct e1000_hw *hw = &adapter->hw;
  427. struct net_device *netdev = adapter->netdev;
  428. u32 rctl, tctl;
  429. /* disable receives in the hardware */
  430. rctl = er32(RCTL);
  431. ew32(RCTL, rctl & ~E1000_RCTL_EN);
  432. /* flush and sleep below */
  433. netif_tx_disable(netdev);
  434. /* disable transmits in the hardware */
  435. tctl = er32(TCTL);
  436. tctl &= ~E1000_TCTL_EN;
  437. ew32(TCTL, tctl);
  438. /* flush both disables and wait for them to finish */
  439. E1000_WRITE_FLUSH();
  440. msleep(10);
  441. napi_disable(&adapter->napi);
  442. e1000_irq_disable(adapter);
  443. /*
  444. * Setting DOWN must be after irq_disable to prevent
  445. * a screaming interrupt. Setting DOWN also prevents
  446. * timers and tasks from rescheduling.
  447. */
  448. set_bit(__E1000_DOWN, &adapter->flags);
  449. del_timer_sync(&adapter->tx_fifo_stall_timer);
  450. del_timer_sync(&adapter->watchdog_timer);
  451. del_timer_sync(&adapter->phy_info_timer);
  452. adapter->link_speed = 0;
  453. adapter->link_duplex = 0;
  454. netif_carrier_off(netdev);
  455. e1000_reset(adapter);
  456. e1000_clean_all_tx_rings(adapter);
  457. e1000_clean_all_rx_rings(adapter);
  458. }
  459. static void e1000_reinit_safe(struct e1000_adapter *adapter)
  460. {
  461. while (test_and_set_bit(__E1000_RESETTING, &adapter->flags))
  462. msleep(1);
  463. rtnl_lock();
  464. e1000_down(adapter);
  465. e1000_up(adapter);
  466. rtnl_unlock();
  467. clear_bit(__E1000_RESETTING, &adapter->flags);
  468. }
  469. void e1000_reinit_locked(struct e1000_adapter *adapter)
  470. {
  471. /* if rtnl_lock is not held the call path is bogus */
  472. ASSERT_RTNL();
  473. WARN_ON(in_interrupt());
  474. while (test_and_set_bit(__E1000_RESETTING, &adapter->flags))
  475. msleep(1);
  476. e1000_down(adapter);
  477. e1000_up(adapter);
  478. clear_bit(__E1000_RESETTING, &adapter->flags);
  479. }
  480. void e1000_reset(struct e1000_adapter *adapter)
  481. {
  482. struct e1000_hw *hw = &adapter->hw;
  483. u32 pba = 0, tx_space, min_tx_space, min_rx_space;
  484. bool legacy_pba_adjust = false;
  485. u16 hwm;
  486. /* Repartition Pba for greater than 9k mtu
  487. * To take effect CTRL.RST is required.
  488. */
  489. switch (hw->mac_type) {
  490. case e1000_82542_rev2_0:
  491. case e1000_82542_rev2_1:
  492. case e1000_82543:
  493. case e1000_82544:
  494. case e1000_82540:
  495. case e1000_82541:
  496. case e1000_82541_rev_2:
  497. legacy_pba_adjust = true;
  498. pba = E1000_PBA_48K;
  499. break;
  500. case e1000_82545:
  501. case e1000_82545_rev_3:
  502. case e1000_82546:
  503. case e1000_ce4100:
  504. case e1000_82546_rev_3:
  505. pba = E1000_PBA_48K;
  506. break;
  507. case e1000_82547:
  508. case e1000_82547_rev_2:
  509. legacy_pba_adjust = true;
  510. pba = E1000_PBA_30K;
  511. break;
  512. case e1000_undefined:
  513. case e1000_num_macs:
  514. break;
  515. }
  516. if (legacy_pba_adjust) {
  517. if (hw->max_frame_size > E1000_RXBUFFER_8192)
  518. pba -= 8; /* allocate more FIFO for Tx */
  519. if (hw->mac_type == e1000_82547) {
  520. adapter->tx_fifo_head = 0;
  521. adapter->tx_head_addr = pba << E1000_TX_HEAD_ADDR_SHIFT;
  522. adapter->tx_fifo_size =
  523. (E1000_PBA_40K - pba) << E1000_PBA_BYTES_SHIFT;
  524. atomic_set(&adapter->tx_fifo_stall, 0);
  525. }
  526. } else if (hw->max_frame_size > ETH_FRAME_LEN + ETH_FCS_LEN) {
  527. /* adjust PBA for jumbo frames */
  528. ew32(PBA, pba);
  529. /* To maintain wire speed transmits, the Tx FIFO should be
  530. * large enough to accommodate two full transmit packets,
  531. * rounded up to the next 1KB and expressed in KB. Likewise,
  532. * the Rx FIFO should be large enough to accommodate at least
  533. * one full receive packet and is similarly rounded up and
  534. * expressed in KB. */
  535. pba = er32(PBA);
  536. /* upper 16 bits has Tx packet buffer allocation size in KB */
  537. tx_space = pba >> 16;
  538. /* lower 16 bits has Rx packet buffer allocation size in KB */
  539. pba &= 0xffff;
  540. /*
  541. * the tx fifo also stores 16 bytes of information about the tx
  542. * but don't include ethernet FCS because hardware appends it
  543. */
  544. min_tx_space = (hw->max_frame_size +
  545. sizeof(struct e1000_tx_desc) -
  546. ETH_FCS_LEN) * 2;
  547. min_tx_space = ALIGN(min_tx_space, 1024);
  548. min_tx_space >>= 10;
  549. /* software strips receive CRC, so leave room for it */
  550. min_rx_space = hw->max_frame_size;
  551. min_rx_space = ALIGN(min_rx_space, 1024);
  552. min_rx_space >>= 10;
  553. /* If current Tx allocation is less than the min Tx FIFO size,
  554. * and the min Tx FIFO size is less than the current Rx FIFO
  555. * allocation, take space away from current Rx allocation */
  556. if (tx_space < min_tx_space &&
  557. ((min_tx_space - tx_space) < pba)) {
  558. pba = pba - (min_tx_space - tx_space);
  559. /* PCI/PCIx hardware has PBA alignment constraints */
  560. switch (hw->mac_type) {
  561. case e1000_82545 ... e1000_82546_rev_3:
  562. pba &= ~(E1000_PBA_8K - 1);
  563. break;
  564. default:
  565. break;
  566. }
  567. /* if short on rx space, rx wins and must trump tx
  568. * adjustment or use Early Receive if available */
  569. if (pba < min_rx_space)
  570. pba = min_rx_space;
  571. }
  572. }
  573. ew32(PBA, pba);
  574. /*
  575. * flow control settings:
  576. * The high water mark must be low enough to fit one full frame
  577. * (or the size used for early receive) above it in the Rx FIFO.
  578. * Set it to the lower of:
  579. * - 90% of the Rx FIFO size, and
  580. * - the full Rx FIFO size minus the early receive size (for parts
  581. * with ERT support assuming ERT set to E1000_ERT_2048), or
  582. * - the full Rx FIFO size minus one full frame
  583. */
  584. hwm = min(((pba << 10) * 9 / 10),
  585. ((pba << 10) - hw->max_frame_size));
  586. hw->fc_high_water = hwm & 0xFFF8; /* 8-byte granularity */
  587. hw->fc_low_water = hw->fc_high_water - 8;
  588. hw->fc_pause_time = E1000_FC_PAUSE_TIME;
  589. hw->fc_send_xon = 1;
  590. hw->fc = hw->original_fc;
  591. /* Allow time for pending master requests to run */
  592. e1000_reset_hw(hw);
  593. if (hw->mac_type >= e1000_82544)
  594. ew32(WUC, 0);
  595. if (e1000_init_hw(hw))
  596. e_dev_err("Hardware Error\n");
  597. e1000_update_mng_vlan(adapter);
  598. /* if (adapter->hwflags & HWFLAGS_PHY_PWR_BIT) { */
  599. if (hw->mac_type >= e1000_82544 &&
  600. hw->autoneg == 1 &&
  601. hw->autoneg_advertised == ADVERTISE_1000_FULL) {
  602. u32 ctrl = er32(CTRL);
  603. /* clear phy power management bit if we are in gig only mode,
  604. * which if enabled will attempt negotiation to 100Mb, which
  605. * can cause a loss of link at power off or driver unload */
  606. ctrl &= ~E1000_CTRL_SWDPIN3;
  607. ew32(CTRL, ctrl);
  608. }
  609. /* Enable h/w to recognize an 802.1Q VLAN Ethernet packet */
  610. ew32(VET, ETHERNET_IEEE_VLAN_TYPE);
  611. e1000_reset_adaptive(hw);
  612. e1000_phy_get_info(hw, &adapter->phy_info);
  613. e1000_release_manageability(adapter);
  614. }
  615. /**
  616. * Dump the eeprom for users having checksum issues
  617. **/
  618. static void e1000_dump_eeprom(struct e1000_adapter *adapter)
  619. {
  620. struct net_device *netdev = adapter->netdev;
  621. struct ethtool_eeprom eeprom;
  622. const struct ethtool_ops *ops = netdev->ethtool_ops;
  623. u8 *data;
  624. int i;
  625. u16 csum_old, csum_new = 0;
  626. eeprom.len = ops->get_eeprom_len(netdev);
  627. eeprom.offset = 0;
  628. data = kmalloc(eeprom.len, GFP_KERNEL);
  629. if (!data) {
  630. pr_err("Unable to allocate memory to dump EEPROM data\n");
  631. return;
  632. }
  633. ops->get_eeprom(netdev, &eeprom, data);
  634. csum_old = (data[EEPROM_CHECKSUM_REG * 2]) +
  635. (data[EEPROM_CHECKSUM_REG * 2 + 1] << 8);
  636. for (i = 0; i < EEPROM_CHECKSUM_REG * 2; i += 2)
  637. csum_new += data[i] + (data[i + 1] << 8);
  638. csum_new = EEPROM_SUM - csum_new;
  639. pr_err("/*********************/\n");
  640. pr_err("Current EEPROM Checksum : 0x%04x\n", csum_old);
  641. pr_err("Calculated : 0x%04x\n", csum_new);
  642. pr_err("Offset Values\n");
  643. pr_err("======== ======\n");
  644. print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 16, 1, data, 128, 0);
  645. pr_err("Include this output when contacting your support provider.\n");
  646. pr_err("This is not a software error! Something bad happened to\n");
  647. pr_err("your hardware or EEPROM image. Ignoring this problem could\n");
  648. pr_err("result in further problems, possibly loss of data,\n");
  649. pr_err("corruption or system hangs!\n");
  650. pr_err("The MAC Address will be reset to 00:00:00:00:00:00,\n");
  651. pr_err("which is invalid and requires you to set the proper MAC\n");
  652. pr_err("address manually before continuing to enable this network\n");
  653. pr_err("device. Please inspect the EEPROM dump and report the\n");
  654. pr_err("issue to your hardware vendor or Intel Customer Support.\n");
  655. pr_err("/*********************/\n");
  656. kfree(data);
  657. }
  658. /**
  659. * e1000_is_need_ioport - determine if an adapter needs ioport resources or not
  660. * @pdev: PCI device information struct
  661. *
  662. * Return true if an adapter needs ioport resources
  663. **/
  664. static int e1000_is_need_ioport(struct pci_dev *pdev)
  665. {
  666. switch (pdev->device) {
  667. case E1000_DEV_ID_82540EM:
  668. case E1000_DEV_ID_82540EM_LOM:
  669. case E1000_DEV_ID_82540EP:
  670. case E1000_DEV_ID_82540EP_LOM:
  671. case E1000_DEV_ID_82540EP_LP:
  672. case E1000_DEV_ID_82541EI:
  673. case E1000_DEV_ID_82541EI_MOBILE:
  674. case E1000_DEV_ID_82541ER:
  675. case E1000_DEV_ID_82541ER_LOM:
  676. case E1000_DEV_ID_82541GI:
  677. case E1000_DEV_ID_82541GI_LF:
  678. case E1000_DEV_ID_82541GI_MOBILE:
  679. case E1000_DEV_ID_82544EI_COPPER:
  680. case E1000_DEV_ID_82544EI_FIBER:
  681. case E1000_DEV_ID_82544GC_COPPER:
  682. case E1000_DEV_ID_82544GC_LOM:
  683. case E1000_DEV_ID_82545EM_COPPER:
  684. case E1000_DEV_ID_82545EM_FIBER:
  685. case E1000_DEV_ID_82546EB_COPPER:
  686. case E1000_DEV_ID_82546EB_FIBER:
  687. case E1000_DEV_ID_82546EB_QUAD_COPPER:
  688. return true;
  689. default:
  690. return false;
  691. }
  692. }
  693. static u32 e1000_fix_features(struct net_device *netdev, u32 features)
  694. {
  695. /*
  696. * Since there is no support for separate rx/tx vlan accel
  697. * enable/disable make sure tx flag is always in same state as rx.
  698. */
  699. if (features & NETIF_F_HW_VLAN_RX)
  700. features |= NETIF_F_HW_VLAN_TX;
  701. else
  702. features &= ~NETIF_F_HW_VLAN_TX;
  703. return features;
  704. }
  705. static int e1000_set_features(struct net_device *netdev, u32 features)
  706. {
  707. struct e1000_adapter *adapter = netdev_priv(netdev);
  708. u32 changed = features ^ netdev->features;
  709. if (changed & NETIF_F_HW_VLAN_RX)
  710. e1000_vlan_mode(netdev, features);
  711. if (!(changed & NETIF_F_RXCSUM))
  712. return 0;
  713. adapter->rx_csum = !!(features & NETIF_F_RXCSUM);
  714. if (netif_running(netdev))
  715. e1000_reinit_locked(adapter);
  716. else
  717. e1000_reset(adapter);
  718. return 0;
  719. }
  720. static const struct net_device_ops e1000_netdev_ops = {
  721. .ndo_open = e1000_open,
  722. .ndo_stop = e1000_close,
  723. .ndo_start_xmit = e1000_xmit_frame,
  724. .ndo_get_stats = e1000_get_stats,
  725. .ndo_set_rx_mode = e1000_set_rx_mode,
  726. .ndo_set_mac_address = e1000_set_mac,
  727. .ndo_tx_timeout = e1000_tx_timeout,
  728. .ndo_change_mtu = e1000_change_mtu,
  729. .ndo_do_ioctl = e1000_ioctl,
  730. .ndo_validate_addr = eth_validate_addr,
  731. .ndo_vlan_rx_add_vid = e1000_vlan_rx_add_vid,
  732. .ndo_vlan_rx_kill_vid = e1000_vlan_rx_kill_vid,
  733. #ifdef CONFIG_NET_POLL_CONTROLLER
  734. .ndo_poll_controller = e1000_netpoll,
  735. #endif
  736. .ndo_fix_features = e1000_fix_features,
  737. .ndo_set_features = e1000_set_features,
  738. };
  739. /**
  740. * e1000_init_hw_struct - initialize members of hw struct
  741. * @adapter: board private struct
  742. * @hw: structure used by e1000_hw.c
  743. *
  744. * Factors out initialization of the e1000_hw struct to its own function
  745. * that can be called very early at init (just after struct allocation).
  746. * Fields are initialized based on PCI device information and
  747. * OS network device settings (MTU size).
  748. * Returns negative error codes if MAC type setup fails.
  749. */
  750. static int e1000_init_hw_struct(struct e1000_adapter *adapter,
  751. struct e1000_hw *hw)
  752. {
  753. struct pci_dev *pdev = adapter->pdev;
  754. /* PCI config space info */
  755. hw->vendor_id = pdev->vendor;
  756. hw->device_id = pdev->device;
  757. hw->subsystem_vendor_id = pdev->subsystem_vendor;
  758. hw->subsystem_id = pdev->subsystem_device;
  759. hw->revision_id = pdev->revision;
  760. pci_read_config_word(pdev, PCI_COMMAND, &hw->pci_cmd_word);
  761. hw->max_frame_size = adapter->netdev->mtu +
  762. ENET_HEADER_SIZE + ETHERNET_FCS_SIZE;
  763. hw->min_frame_size = MINIMUM_ETHERNET_FRAME_SIZE;
  764. /* identify the MAC */
  765. if (e1000_set_mac_type(hw)) {
  766. e_err(probe, "Unknown MAC Type\n");
  767. return -EIO;
  768. }
  769. switch (hw->mac_type) {
  770. default:
  771. break;
  772. case e1000_82541:
  773. case e1000_82547:
  774. case e1000_82541_rev_2:
  775. case e1000_82547_rev_2:
  776. hw->phy_init_script = 1;
  777. break;
  778. }
  779. e1000_set_media_type(hw);
  780. e1000_get_bus_info(hw);
  781. hw->wait_autoneg_complete = false;
  782. hw->tbi_compatibility_en = true;
  783. hw->adaptive_ifs = true;
  784. /* Copper options */
  785. if (hw->media_type == e1000_media_type_copper) {
  786. hw->mdix = AUTO_ALL_MODES;
  787. hw->disable_polarity_correction = false;
  788. hw->master_slave = E1000_MASTER_SLAVE;
  789. }
  790. return 0;
  791. }
  792. /**
  793. * e1000_probe - Device Initialization Routine
  794. * @pdev: PCI device information struct
  795. * @ent: entry in e1000_pci_tbl
  796. *
  797. * Returns 0 on success, negative on failure
  798. *
  799. * e1000_probe initializes an adapter identified by a pci_dev structure.
  800. * The OS initialization, configuring of the adapter private structure,
  801. * and a hardware reset occur.
  802. **/
  803. static int __devinit e1000_probe(struct pci_dev *pdev,
  804. const struct pci_device_id *ent)
  805. {
  806. struct net_device *netdev;
  807. struct e1000_adapter *adapter;
  808. struct e1000_hw *hw;
  809. static int cards_found = 0;
  810. static int global_quad_port_a = 0; /* global ksp3 port a indication */
  811. int i, err, pci_using_dac;
  812. u16 eeprom_data = 0;
  813. u16 tmp = 0;
  814. u16 eeprom_apme_mask = E1000_EEPROM_APME;
  815. int bars, need_ioport;
  816. /* do not allocate ioport bars when not needed */
  817. need_ioport = e1000_is_need_ioport(pdev);
  818. if (need_ioport) {
  819. bars = pci_select_bars(pdev, IORESOURCE_MEM | IORESOURCE_IO);
  820. err = pci_enable_device(pdev);
  821. } else {
  822. bars = pci_select_bars(pdev, IORESOURCE_MEM);
  823. err = pci_enable_device_mem(pdev);
  824. }
  825. if (err)
  826. return err;
  827. err = pci_request_selected_regions(pdev, bars, e1000_driver_name);
  828. if (err)
  829. goto err_pci_reg;
  830. pci_set_master(pdev);
  831. err = pci_save_state(pdev);
  832. if (err)
  833. goto err_alloc_etherdev;
  834. err = -ENOMEM;
  835. netdev = alloc_etherdev(sizeof(struct e1000_adapter));
  836. if (!netdev)
  837. goto err_alloc_etherdev;
  838. SET_NETDEV_DEV(netdev, &pdev->dev);
  839. pci_set_drvdata(pdev, netdev);
  840. adapter = netdev_priv(netdev);
  841. adapter->netdev = netdev;
  842. adapter->pdev = pdev;
  843. adapter->msg_enable = (1 << debug) - 1;
  844. adapter->bars = bars;
  845. adapter->need_ioport = need_ioport;
  846. hw = &adapter->hw;
  847. hw->back = adapter;
  848. err = -EIO;
  849. hw->hw_addr = pci_ioremap_bar(pdev, BAR_0);
  850. if (!hw->hw_addr)
  851. goto err_ioremap;
  852. if (adapter->need_ioport) {
  853. for (i = BAR_1; i <= BAR_5; i++) {
  854. if (pci_resource_len(pdev, i) == 0)
  855. continue;
  856. if (pci_resource_flags(pdev, i) & IORESOURCE_IO) {
  857. hw->io_base = pci_resource_start(pdev, i);
  858. break;
  859. }
  860. }
  861. }
  862. /* make ready for any if (hw->...) below */
  863. err = e1000_init_hw_struct(adapter, hw);
  864. if (err)
  865. goto err_sw_init;
  866. /*
  867. * there is a workaround being applied below that limits
  868. * 64-bit DMA addresses to 64-bit hardware. There are some
  869. * 32-bit adapters that Tx hang when given 64-bit DMA addresses
  870. */
  871. pci_using_dac = 0;
  872. if ((hw->bus_type == e1000_bus_type_pcix) &&
  873. !dma_set_mask(&pdev->dev, DMA_BIT_MASK(64))) {
  874. /*
  875. * according to DMA-API-HOWTO, coherent calls will always
  876. * succeed if the set call did
  877. */
  878. dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(64));
  879. pci_using_dac = 1;
  880. } else {
  881. err = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
  882. if (err) {
  883. pr_err("No usable DMA config, aborting\n");
  884. goto err_dma;
  885. }
  886. dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
  887. }
  888. netdev->netdev_ops = &e1000_netdev_ops;
  889. e1000_set_ethtool_ops(netdev);
  890. netdev->watchdog_timeo = 5 * HZ;
  891. netif_napi_add(netdev, &adapter->napi, e1000_clean, 64);
  892. strncpy(netdev->name, pci_name(pdev), sizeof(netdev->name) - 1);
  893. adapter->bd_number = cards_found;
  894. /* setup the private structure */
  895. err = e1000_sw_init(adapter);
  896. if (err)
  897. goto err_sw_init;
  898. err = -EIO;
  899. if (hw->mac_type == e1000_ce4100) {
  900. ce4100_gbe_mdio_base_phy = pci_resource_start(pdev, BAR_1);
  901. ce4100_gbe_mdio_base_virt = ioremap(ce4100_gbe_mdio_base_phy,
  902. pci_resource_len(pdev, BAR_1));
  903. if (!ce4100_gbe_mdio_base_virt)
  904. goto err_mdio_ioremap;
  905. }
  906. if (hw->mac_type >= e1000_82543) {
  907. netdev->hw_features = NETIF_F_SG |
  908. NETIF_F_HW_CSUM |
  909. NETIF_F_HW_VLAN_RX;
  910. netdev->features = NETIF_F_HW_VLAN_TX |
  911. NETIF_F_HW_VLAN_FILTER;
  912. }
  913. if ((hw->mac_type >= e1000_82544) &&
  914. (hw->mac_type != e1000_82547))
  915. netdev->hw_features |= NETIF_F_TSO;
  916. netdev->features |= netdev->hw_features;
  917. netdev->hw_features |= NETIF_F_RXCSUM;
  918. if (pci_using_dac) {
  919. netdev->features |= NETIF_F_HIGHDMA;
  920. netdev->vlan_features |= NETIF_F_HIGHDMA;
  921. }
  922. netdev->vlan_features |= NETIF_F_TSO;
  923. netdev->vlan_features |= NETIF_F_HW_CSUM;
  924. netdev->vlan_features |= NETIF_F_SG;
  925. netdev->priv_flags |= IFF_UNICAST_FLT;
  926. adapter->en_mng_pt = e1000_enable_mng_pass_thru(hw);
  927. /* initialize eeprom parameters */
  928. if (e1000_init_eeprom_params(hw)) {
  929. e_err(probe, "EEPROM initialization failed\n");
  930. goto err_eeprom;
  931. }
  932. /* before reading the EEPROM, reset the controller to
  933. * put the device in a known good starting state */
  934. e1000_reset_hw(hw);
  935. /* make sure the EEPROM is good */
  936. if (e1000_validate_eeprom_checksum(hw) < 0) {
  937. e_err(probe, "The EEPROM Checksum Is Not Valid\n");
  938. e1000_dump_eeprom(adapter);
  939. /*
  940. * set MAC address to all zeroes to invalidate and temporary
  941. * disable this device for the user. This blocks regular
  942. * traffic while still permitting ethtool ioctls from reaching
  943. * the hardware as well as allowing the user to run the
  944. * interface after manually setting a hw addr using
  945. * `ip set address`
  946. */
  947. memset(hw->mac_addr, 0, netdev->addr_len);
  948. } else {
  949. /* copy the MAC address out of the EEPROM */
  950. if (e1000_read_mac_addr(hw))
  951. e_err(probe, "EEPROM Read Error\n");
  952. }
  953. /* don't block initalization here due to bad MAC address */
  954. memcpy(netdev->dev_addr, hw->mac_addr, netdev->addr_len);
  955. memcpy(netdev->perm_addr, hw->mac_addr, netdev->addr_len);
  956. if (!is_valid_ether_addr(netdev->perm_addr))
  957. e_err(probe, "Invalid MAC Address\n");
  958. init_timer(&adapter->tx_fifo_stall_timer);
  959. adapter->tx_fifo_stall_timer.function = e1000_82547_tx_fifo_stall;
  960. adapter->tx_fifo_stall_timer.data = (unsigned long)adapter;
  961. init_timer(&adapter->watchdog_timer);
  962. adapter->watchdog_timer.function = e1000_watchdog;
  963. adapter->watchdog_timer.data = (unsigned long) adapter;
  964. init_timer(&adapter->phy_info_timer);
  965. adapter->phy_info_timer.function = e1000_update_phy_info;
  966. adapter->phy_info_timer.data = (unsigned long)adapter;
  967. INIT_WORK(&adapter->fifo_stall_task, e1000_82547_tx_fifo_stall_task);
  968. INIT_WORK(&adapter->reset_task, e1000_reset_task);
  969. INIT_WORK(&adapter->phy_info_task, e1000_update_phy_info_task);
  970. e1000_check_options(adapter);
  971. /* Initial Wake on LAN setting
  972. * If APM wake is enabled in the EEPROM,
  973. * enable the ACPI Magic Packet filter
  974. */
  975. switch (hw->mac_type) {
  976. case e1000_82542_rev2_0:
  977. case e1000_82542_rev2_1:
  978. case e1000_82543:
  979. break;
  980. case e1000_82544:
  981. e1000_read_eeprom(hw,
  982. EEPROM_INIT_CONTROL2_REG, 1, &eeprom_data);
  983. eeprom_apme_mask = E1000_EEPROM_82544_APM;
  984. break;
  985. case e1000_82546:
  986. case e1000_82546_rev_3:
  987. if (er32(STATUS) & E1000_STATUS_FUNC_1){
  988. e1000_read_eeprom(hw,
  989. EEPROM_INIT_CONTROL3_PORT_B, 1, &eeprom_data);
  990. break;
  991. }
  992. /* Fall Through */
  993. default:
  994. e1000_read_eeprom(hw,
  995. EEPROM_INIT_CONTROL3_PORT_A, 1, &eeprom_data);
  996. break;
  997. }
  998. if (eeprom_data & eeprom_apme_mask)
  999. adapter->eeprom_wol |= E1000_WUFC_MAG;
  1000. /* now that we have the eeprom settings, apply the special cases
  1001. * where the eeprom may be wrong or the board simply won't support
  1002. * wake on lan on a particular port */
  1003. switch (pdev->device) {
  1004. case E1000_DEV_ID_82546GB_PCIE:
  1005. adapter->eeprom_wol = 0;
  1006. break;
  1007. case E1000_DEV_ID_82546EB_FIBER:
  1008. case E1000_DEV_ID_82546GB_FIBER:
  1009. /* Wake events only supported on port A for dual fiber
  1010. * regardless of eeprom setting */
  1011. if (er32(STATUS) & E1000_STATUS_FUNC_1)
  1012. adapter->eeprom_wol = 0;
  1013. break;
  1014. case E1000_DEV_ID_82546GB_QUAD_COPPER_KSP3:
  1015. /* if quad port adapter, disable WoL on all but port A */
  1016. if (global_quad_port_a != 0)
  1017. adapter->eeprom_wol = 0;
  1018. else
  1019. adapter->quad_port_a = 1;
  1020. /* Reset for multiple quad port adapters */
  1021. if (++global_quad_port_a == 4)
  1022. global_quad_port_a = 0;
  1023. break;
  1024. }
  1025. /* initialize the wol settings based on the eeprom settings */
  1026. adapter->wol = adapter->eeprom_wol;
  1027. device_set_wakeup_enable(&adapter->pdev->dev, adapter->wol);
  1028. /* Auto detect PHY address */
  1029. if (hw->mac_type == e1000_ce4100) {
  1030. for (i = 0; i < 32; i++) {
  1031. hw->phy_addr = i;
  1032. e1000_read_phy_reg(hw, PHY_ID2, &tmp);
  1033. if (tmp == 0 || tmp == 0xFF) {
  1034. if (i == 31)
  1035. goto err_eeprom;
  1036. continue;
  1037. } else
  1038. break;
  1039. }
  1040. }
  1041. /* reset the hardware with the new settings */
  1042. e1000_reset(adapter);
  1043. strcpy(netdev->name, "eth%d");
  1044. err = register_netdev(netdev);
  1045. if (err)
  1046. goto err_register;
  1047. e1000_vlan_mode(netdev, netdev->features);
  1048. /* print bus type/speed/width info */
  1049. e_info(probe, "(PCI%s:%dMHz:%d-bit) %pM\n",
  1050. ((hw->bus_type == e1000_bus_type_pcix) ? "-X" : ""),
  1051. ((hw->bus_speed == e1000_bus_speed_133) ? 133 :
  1052. (hw->bus_speed == e1000_bus_speed_120) ? 120 :
  1053. (hw->bus_speed == e1000_bus_speed_100) ? 100 :
  1054. (hw->bus_speed == e1000_bus_speed_66) ? 66 : 33),
  1055. ((hw->bus_width == e1000_bus_width_64) ? 64 : 32),
  1056. netdev->dev_addr);
  1057. /* carrier off reporting is important to ethtool even BEFORE open */
  1058. netif_carrier_off(netdev);
  1059. e_info(probe, "Intel(R) PRO/1000 Network Connection\n");
  1060. cards_found++;
  1061. return 0;
  1062. err_register:
  1063. err_eeprom:
  1064. e1000_phy_hw_reset(hw);
  1065. if (hw->flash_address)
  1066. iounmap(hw->flash_address);
  1067. kfree(adapter->tx_ring);
  1068. kfree(adapter->rx_ring);
  1069. err_dma:
  1070. err_sw_init:
  1071. err_mdio_ioremap:
  1072. iounmap(ce4100_gbe_mdio_base_virt);
  1073. iounmap(hw->hw_addr);
  1074. err_ioremap:
  1075. free_netdev(netdev);
  1076. err_alloc_etherdev:
  1077. pci_release_selected_regions(pdev, bars);
  1078. err_pci_reg:
  1079. pci_disable_device(pdev);
  1080. return err;
  1081. }
  1082. /**
  1083. * e1000_remove - Device Removal Routine
  1084. * @pdev: PCI device information struct
  1085. *
  1086. * e1000_remove is called by the PCI subsystem to alert the driver
  1087. * that it should release a PCI device. The could be caused by a
  1088. * Hot-Plug event, or because the driver is going to be removed from
  1089. * memory.
  1090. **/
  1091. static void __devexit e1000_remove(struct pci_dev *pdev)
  1092. {
  1093. struct net_device *netdev = pci_get_drvdata(pdev);
  1094. struct e1000_adapter *adapter = netdev_priv(netdev);
  1095. struct e1000_hw *hw = &adapter->hw;
  1096. set_bit(__E1000_DOWN, &adapter->flags);
  1097. del_timer_sync(&adapter->tx_fifo_stall_timer);
  1098. del_timer_sync(&adapter->watchdog_timer);
  1099. del_timer_sync(&adapter->phy_info_timer);
  1100. cancel_work_sync(&adapter->reset_task);
  1101. e1000_release_manageability(adapter);
  1102. unregister_netdev(netdev);
  1103. e1000_phy_hw_reset(hw);
  1104. kfree(adapter->tx_ring);
  1105. kfree(adapter->rx_ring);
  1106. iounmap(hw->hw_addr);
  1107. if (hw->flash_address)
  1108. iounmap(hw->flash_address);
  1109. pci_release_selected_regions(pdev, adapter->bars);
  1110. free_netdev(netdev);
  1111. pci_disable_device(pdev);
  1112. }
  1113. /**
  1114. * e1000_sw_init - Initialize general software structures (struct e1000_adapter)
  1115. * @adapter: board private structure to initialize
  1116. *
  1117. * e1000_sw_init initializes the Adapter private data structure.
  1118. * e1000_init_hw_struct MUST be called before this function
  1119. **/
  1120. static int __devinit e1000_sw_init(struct e1000_adapter *adapter)
  1121. {
  1122. adapter->rx_buffer_len = MAXIMUM_ETHERNET_VLAN_SIZE;
  1123. adapter->num_tx_queues = 1;
  1124. adapter->num_rx_queues = 1;
  1125. if (e1000_alloc_queues(adapter)) {
  1126. e_err(probe, "Unable to allocate memory for queues\n");
  1127. return -ENOMEM;
  1128. }
  1129. /* Explicitly disable IRQ since the NIC can be in any state. */
  1130. e1000_irq_disable(adapter);
  1131. spin_lock_init(&adapter->stats_lock);
  1132. set_bit(__E1000_DOWN, &adapter->flags);
  1133. return 0;
  1134. }
  1135. /**
  1136. * e1000_alloc_queues - Allocate memory for all rings
  1137. * @adapter: board private structure to initialize
  1138. *
  1139. * We allocate one ring per queue at run-time since we don't know the
  1140. * number of queues at compile-time.
  1141. **/
  1142. static int __devinit e1000_alloc_queues(struct e1000_adapter *adapter)
  1143. {
  1144. adapter->tx_ring = kcalloc(adapter->num_tx_queues,
  1145. sizeof(struct e1000_tx_ring), GFP_KERNEL);
  1146. if (!adapter->tx_ring)
  1147. return -ENOMEM;
  1148. adapter->rx_ring = kcalloc(adapter->num_rx_queues,
  1149. sizeof(struct e1000_rx_ring), GFP_KERNEL);
  1150. if (!adapter->rx_ring) {
  1151. kfree(adapter->tx_ring);
  1152. return -ENOMEM;
  1153. }
  1154. return E1000_SUCCESS;
  1155. }
  1156. /**
  1157. * e1000_open - Called when a network interface is made active
  1158. * @netdev: network interface device structure
  1159. *
  1160. * Returns 0 on success, negative value on failure
  1161. *
  1162. * The open entry point is called when a network interface is made
  1163. * active by the system (IFF_UP). At this point all resources needed
  1164. * for transmit and receive operations are allocated, the interrupt
  1165. * handler is registered with the OS, the watchdog timer is started,
  1166. * and the stack is notified that the interface is ready.
  1167. **/
  1168. static int e1000_open(struct net_device *netdev)
  1169. {
  1170. struct e1000_adapter *adapter = netdev_priv(netdev);
  1171. struct e1000_hw *hw = &adapter->hw;
  1172. int err;
  1173. /* disallow open during test */
  1174. if (test_bit(__E1000_TESTING, &adapter->flags))
  1175. return -EBUSY;
  1176. netif_carrier_off(netdev);
  1177. /* allocate transmit descriptors */
  1178. err = e1000_setup_all_tx_resources(adapter);
  1179. if (err)
  1180. goto err_setup_tx;
  1181. /* allocate receive descriptors */
  1182. err = e1000_setup_all_rx_resources(adapter);
  1183. if (err)
  1184. goto err_setup_rx;
  1185. e1000_power_up_phy(adapter);
  1186. adapter->mng_vlan_id = E1000_MNG_VLAN_NONE;
  1187. if ((hw->mng_cookie.status &
  1188. E1000_MNG_DHCP_COOKIE_STATUS_VLAN_SUPPORT)) {
  1189. e1000_update_mng_vlan(adapter);
  1190. }
  1191. /* before we allocate an interrupt, we must be ready to handle it.
  1192. * Setting DEBUG_SHIRQ in the kernel makes it fire an interrupt
  1193. * as soon as we call pci_request_irq, so we have to setup our
  1194. * clean_rx handler before we do so. */
  1195. e1000_configure(adapter);
  1196. err = e1000_request_irq(adapter);
  1197. if (err)
  1198. goto err_req_irq;
  1199. /* From here on the code is the same as e1000_up() */
  1200. clear_bit(__E1000_DOWN, &adapter->flags);
  1201. napi_enable(&adapter->napi);
  1202. e1000_irq_enable(adapter);
  1203. netif_start_queue(netdev);
  1204. /* fire a link status change interrupt to start the watchdog */
  1205. ew32(ICS, E1000_ICS_LSC);
  1206. return E1000_SUCCESS;
  1207. err_req_irq:
  1208. e1000_power_down_phy(adapter);
  1209. e1000_free_all_rx_resources(adapter);
  1210. err_setup_rx:
  1211. e1000_free_all_tx_resources(adapter);
  1212. err_setup_tx:
  1213. e1000_reset(adapter);
  1214. return err;
  1215. }
  1216. /**
  1217. * e1000_close - Disables a network interface
  1218. * @netdev: network interface device structure
  1219. *
  1220. * Returns 0, this is not allowed to fail
  1221. *
  1222. * The close entry point is called when an interface is de-activated
  1223. * by the OS. The hardware is still under the drivers control, but
  1224. * needs to be disabled. A global MAC reset is issued to stop the
  1225. * hardware, and all transmit and receive resources are freed.
  1226. **/
  1227. static int e1000_close(struct net_device *netdev)
  1228. {
  1229. struct e1000_adapter *adapter = netdev_priv(netdev);
  1230. struct e1000_hw *hw = &adapter->hw;
  1231. WARN_ON(test_bit(__E1000_RESETTING, &adapter->flags));
  1232. e1000_down(adapter);
  1233. e1000_power_down_phy(adapter);
  1234. e1000_free_irq(adapter);
  1235. e1000_free_all_tx_resources(adapter);
  1236. e1000_free_all_rx_resources(adapter);
  1237. /* kill manageability vlan ID if supported, but not if a vlan with
  1238. * the same ID is registered on the host OS (let 8021q kill it) */
  1239. if ((hw->mng_cookie.status &
  1240. E1000_MNG_DHCP_COOKIE_STATUS_VLAN_SUPPORT) &&
  1241. !test_bit(adapter->mng_vlan_id, adapter->active_vlans)) {
  1242. e1000_vlan_rx_kill_vid(netdev, adapter->mng_vlan_id);
  1243. }
  1244. return 0;
  1245. }
  1246. /**
  1247. * e1000_check_64k_bound - check that memory doesn't cross 64kB boundary
  1248. * @adapter: address of board private structure
  1249. * @start: address of beginning of memory
  1250. * @len: length of memory
  1251. **/
  1252. static bool e1000_check_64k_bound(struct e1000_adapter *adapter, void *start,
  1253. unsigned long len)
  1254. {
  1255. struct e1000_hw *hw = &adapter->hw;
  1256. unsigned long begin = (unsigned long)start;
  1257. unsigned long end = begin + len;
  1258. /* First rev 82545 and 82546 need to not allow any memory
  1259. * write location to cross 64k boundary due to errata 23 */
  1260. if (hw->mac_type == e1000_82545 ||
  1261. hw->mac_type == e1000_ce4100 ||
  1262. hw->mac_type == e1000_82546) {
  1263. return ((begin ^ (end - 1)) >> 16) != 0 ? false : true;
  1264. }
  1265. return true;
  1266. }
  1267. /**
  1268. * e1000_setup_tx_resources - allocate Tx resources (Descriptors)
  1269. * @adapter: board private structure
  1270. * @txdr: tx descriptor ring (for a specific queue) to setup
  1271. *
  1272. * Return 0 on success, negative on failure
  1273. **/
  1274. static int e1000_setup_tx_resources(struct e1000_adapter *adapter,
  1275. struct e1000_tx_ring *txdr)
  1276. {
  1277. struct pci_dev *pdev = adapter->pdev;
  1278. int size;
  1279. size = sizeof(struct e1000_buffer) * txdr->count;
  1280. txdr->buffer_info = vzalloc(size);
  1281. if (!txdr->buffer_info) {
  1282. e_err(probe, "Unable to allocate memory for the Tx descriptor "
  1283. "ring\n");
  1284. return -ENOMEM;
  1285. }
  1286. /* round up to nearest 4K */
  1287. txdr->size = txdr->count * sizeof(struct e1000_tx_desc);
  1288. txdr->size = ALIGN(txdr->size, 4096);
  1289. txdr->desc = dma_alloc_coherent(&pdev->dev, txdr->size, &txdr->dma,
  1290. GFP_KERNEL);
  1291. if (!txdr->desc) {
  1292. setup_tx_desc_die:
  1293. vfree(txdr->buffer_info);
  1294. e_err(probe, "Unable to allocate memory for the Tx descriptor "
  1295. "ring\n");
  1296. return -ENOMEM;
  1297. }
  1298. /* Fix for errata 23, can't cross 64kB boundary */
  1299. if (!e1000_check_64k_bound(adapter, txdr->desc, txdr->size)) {
  1300. void *olddesc = txdr->desc;
  1301. dma_addr_t olddma = txdr->dma;
  1302. e_err(tx_err, "txdr align check failed: %u bytes at %p\n",
  1303. txdr->size, txdr->desc);
  1304. /* Try again, without freeing the previous */
  1305. txdr->desc = dma_alloc_coherent(&pdev->dev, txdr->size,
  1306. &txdr->dma, GFP_KERNEL);
  1307. /* Failed allocation, critical failure */
  1308. if (!txdr->desc) {
  1309. dma_free_coherent(&pdev->dev, txdr->size, olddesc,
  1310. olddma);
  1311. goto setup_tx_desc_die;
  1312. }
  1313. if (!e1000_check_64k_bound(adapter, txdr->desc, txdr->size)) {
  1314. /* give up */
  1315. dma_free_coherent(&pdev->dev, txdr->size, txdr->desc,
  1316. txdr->dma);
  1317. dma_free_coherent(&pdev->dev, txdr->size, olddesc,
  1318. olddma);
  1319. e_err(probe, "Unable to allocate aligned memory "
  1320. "for the transmit descriptor ring\n");
  1321. vfree(txdr->buffer_info);
  1322. return -ENOMEM;
  1323. } else {
  1324. /* Free old allocation, new allocation was successful */
  1325. dma_free_coherent(&pdev->dev, txdr->size, olddesc,
  1326. olddma);
  1327. }
  1328. }
  1329. memset(txdr->desc, 0, txdr->size);
  1330. txdr->next_to_use = 0;
  1331. txdr->next_to_clean = 0;
  1332. return 0;
  1333. }
  1334. /**
  1335. * e1000_setup_all_tx_resources - wrapper to allocate Tx resources
  1336. * (Descriptors) for all queues
  1337. * @adapter: board private structure
  1338. *
  1339. * Return 0 on success, negative on failure
  1340. **/
  1341. int e1000_setup_all_tx_resources(struct e1000_adapter *adapter)
  1342. {
  1343. int i, err = 0;
  1344. for (i = 0; i < adapter->num_tx_queues; i++) {
  1345. err = e1000_setup_tx_resources(adapter, &adapter->tx_ring[i]);
  1346. if (err) {
  1347. e_err(probe, "Allocation for Tx Queue %u failed\n", i);
  1348. for (i-- ; i >= 0; i--)
  1349. e1000_free_tx_resources(adapter,
  1350. &adapter->tx_ring[i]);
  1351. break;
  1352. }
  1353. }
  1354. return err;
  1355. }
  1356. /**
  1357. * e1000_configure_tx - Configure 8254x Transmit Unit after Reset
  1358. * @adapter: board private structure
  1359. *
  1360. * Configure the Tx unit of the MAC after a reset.
  1361. **/
  1362. static void e1000_configure_tx(struct e1000_adapter *adapter)
  1363. {
  1364. u64 tdba;
  1365. struct e1000_hw *hw = &adapter->hw;
  1366. u32 tdlen, tctl, tipg;
  1367. u32 ipgr1, ipgr2;
  1368. /* Setup the HW Tx Head and Tail descriptor pointers */
  1369. switch (adapter->num_tx_queues) {
  1370. case 1:
  1371. default:
  1372. tdba = adapter->tx_ring[0].dma;
  1373. tdlen = adapter->tx_ring[0].count *
  1374. sizeof(struct e1000_tx_desc);
  1375. ew32(TDLEN, tdlen);
  1376. ew32(TDBAH, (tdba >> 32));
  1377. ew32(TDBAL, (tdba & 0x00000000ffffffffULL));
  1378. ew32(TDT, 0);
  1379. ew32(TDH, 0);
  1380. adapter->tx_ring[0].tdh = ((hw->mac_type >= e1000_82543) ? E1000_TDH : E1000_82542_TDH);
  1381. adapter->tx_ring[0].tdt = ((hw->mac_type >= e1000_82543) ? E1000_TDT : E1000_82542_TDT);
  1382. break;
  1383. }
  1384. /* Set the default values for the Tx Inter Packet Gap timer */
  1385. if ((hw->media_type == e1000_media_type_fiber ||
  1386. hw->media_type == e1000_media_type_internal_serdes))
  1387. tipg = DEFAULT_82543_TIPG_IPGT_FIBER;
  1388. else
  1389. tipg = DEFAULT_82543_TIPG_IPGT_COPPER;
  1390. switch (hw->mac_type) {
  1391. case e1000_82542_rev2_0:
  1392. case e1000_82542_rev2_1:
  1393. tipg = DEFAULT_82542_TIPG_IPGT;
  1394. ipgr1 = DEFAULT_82542_TIPG_IPGR1;
  1395. ipgr2 = DEFAULT_82542_TIPG_IPGR2;
  1396. break;
  1397. default:
  1398. ipgr1 = DEFAULT_82543_TIPG_IPGR1;
  1399. ipgr2 = DEFAULT_82543_TIPG_IPGR2;
  1400. break;
  1401. }
  1402. tipg |= ipgr1 << E1000_TIPG_IPGR1_SHIFT;
  1403. tipg |= ipgr2 << E1000_TIPG_IPGR2_SHIFT;
  1404. ew32(TIPG, tipg);
  1405. /* Set the Tx Interrupt Delay register */
  1406. ew32(TIDV, adapter->tx_int_delay);
  1407. if (hw->mac_type >= e1000_82540)
  1408. ew32(TADV, adapter->tx_abs_int_delay);
  1409. /* Program the Transmit Control Register */
  1410. tctl = er32(TCTL);
  1411. tctl &= ~E1000_TCTL_CT;
  1412. tctl |= E1000_TCTL_PSP | E1000_TCTL_RTLC |
  1413. (E1000_COLLISION_THRESHOLD << E1000_CT_SHIFT);
  1414. e1000_config_collision_dist(hw);
  1415. /* Setup Transmit Descriptor Settings for eop descriptor */
  1416. adapter->txd_cmd = E1000_TXD_CMD_EOP | E1000_TXD_CMD_IFCS;
  1417. /* only set IDE if we are delaying interrupts using the timers */
  1418. if (adapter->tx_int_delay)
  1419. adapter->txd_cmd |= E1000_TXD_CMD_IDE;
  1420. if (hw->mac_type < e1000_82543)
  1421. adapter->txd_cmd |= E1000_TXD_CMD_RPS;
  1422. else
  1423. adapter->txd_cmd |= E1000_TXD_CMD_RS;
  1424. /* Cache if we're 82544 running in PCI-X because we'll
  1425. * need this to apply a workaround later in the send path. */
  1426. if (hw->mac_type == e1000_82544 &&
  1427. hw->bus_type == e1000_bus_type_pcix)
  1428. adapter->pcix_82544 = 1;
  1429. ew32(TCTL, tctl);
  1430. }
  1431. /**
  1432. * e1000_setup_rx_resources - allocate Rx resources (Descriptors)
  1433. * @adapter: board private structure
  1434. * @rxdr: rx descriptor ring (for a specific queue) to setup
  1435. *
  1436. * Returns 0 on success, negative on failure
  1437. **/
  1438. static int e1000_setup_rx_resources(struct e1000_adapter *adapter,
  1439. struct e1000_rx_ring *rxdr)
  1440. {
  1441. struct pci_dev *pdev = adapter->pdev;
  1442. int size, desc_len;
  1443. size = sizeof(struct e1000_buffer) * rxdr->count;
  1444. rxdr->buffer_info = vzalloc(size);
  1445. if (!rxdr->buffer_info) {
  1446. e_err(probe, "Unable to allocate memory for the Rx descriptor "
  1447. "ring\n");
  1448. return -ENOMEM;
  1449. }
  1450. desc_len = sizeof(struct e1000_rx_desc);
  1451. /* Round up to nearest 4K */
  1452. rxdr->size = rxdr->count * desc_len;
  1453. rxdr->size = ALIGN(rxdr->size, 4096);
  1454. rxdr->desc = dma_alloc_coherent(&pdev->dev, rxdr->size, &rxdr->dma,
  1455. GFP_KERNEL);
  1456. if (!rxdr->desc) {
  1457. e_err(probe, "Unable to allocate memory for the Rx descriptor "
  1458. "ring\n");
  1459. setup_rx_desc_die:
  1460. vfree(rxdr->buffer_info);
  1461. return -ENOMEM;
  1462. }
  1463. /* Fix for errata 23, can't cross 64kB boundary */
  1464. if (!e1000_check_64k_bound(adapter, rxdr->desc, rxdr->size)) {
  1465. void *olddesc = rxdr->desc;
  1466. dma_addr_t olddma = rxdr->dma;
  1467. e_err(rx_err, "rxdr align check failed: %u bytes at %p\n",
  1468. rxdr->size, rxdr->desc);
  1469. /* Try again, without freeing the previous */
  1470. rxdr->desc = dma_alloc_coherent(&pdev->dev, rxdr->size,
  1471. &rxdr->dma, GFP_KERNEL);
  1472. /* Failed allocation, critical failure */
  1473. if (!rxdr->desc) {
  1474. dma_free_coherent(&pdev->dev, rxdr->size, olddesc,
  1475. olddma);
  1476. e_err(probe, "Unable to allocate memory for the Rx "
  1477. "descriptor ring\n");
  1478. goto setup_rx_desc_die;
  1479. }
  1480. if (!e1000_check_64k_bound(adapter, rxdr->desc, rxdr->size)) {
  1481. /* give up */
  1482. dma_free_coherent(&pdev->dev, rxdr->size, rxdr->desc,
  1483. rxdr->dma);
  1484. dma_free_coherent(&pdev->dev, rxdr->size, olddesc,
  1485. olddma);
  1486. e_err(probe, "Unable to allocate aligned memory for "
  1487. "the Rx descriptor ring\n");
  1488. goto setup_rx_desc_die;
  1489. } else {
  1490. /* Free old allocation, new allocation was successful */
  1491. dma_free_coherent(&pdev->dev, rxdr->size, olddesc,
  1492. olddma);
  1493. }
  1494. }
  1495. memset(rxdr->desc, 0, rxdr->size);
  1496. rxdr->next_to_clean = 0;
  1497. rxdr->next_to_use = 0;
  1498. rxdr->rx_skb_top = NULL;
  1499. return 0;
  1500. }
  1501. /**
  1502. * e1000_setup_all_rx_resources - wrapper to allocate Rx resources
  1503. * (Descriptors) for all queues
  1504. * @adapter: board private structure
  1505. *
  1506. * Return 0 on success, negative on failure
  1507. **/
  1508. int e1000_setup_all_rx_resources(struct e1000_adapter *adapter)
  1509. {
  1510. int i, err = 0;
  1511. for (i = 0; i < adapter->num_rx_queues; i++) {
  1512. err = e1000_setup_rx_resources(adapter, &adapter->rx_ring[i]);
  1513. if (err) {
  1514. e_err(probe, "Allocation for Rx Queue %u failed\n", i);
  1515. for (i-- ; i >= 0; i--)
  1516. e1000_free_rx_resources(adapter,
  1517. &adapter->rx_ring[i]);
  1518. break;
  1519. }
  1520. }
  1521. return err;
  1522. }
  1523. /**
  1524. * e1000_setup_rctl - configure the receive control registers
  1525. * @adapter: Board private structure
  1526. **/
  1527. static void e1000_setup_rctl(struct e1000_adapter *adapter)
  1528. {
  1529. struct e1000_hw *hw = &adapter->hw;
  1530. u32 rctl;
  1531. rctl = er32(RCTL);
  1532. rctl &= ~(3 << E1000_RCTL_MO_SHIFT);
  1533. rctl |= E1000_RCTL_BAM | E1000_RCTL_LBM_NO |
  1534. E1000_RCTL_RDMTS_HALF |
  1535. (hw->mc_filter_type << E1000_RCTL_MO_SHIFT);
  1536. if (hw->tbi_compatibility_on == 1)
  1537. rctl |= E1000_RCTL_SBP;
  1538. else
  1539. rctl &= ~E1000_RCTL_SBP;
  1540. if (adapter->netdev->mtu <= ETH_DATA_LEN)
  1541. rctl &= ~E1000_RCTL_LPE;
  1542. else
  1543. rctl |= E1000_RCTL_LPE;
  1544. /* Setup buffer sizes */
  1545. rctl &= ~E1000_RCTL_SZ_4096;
  1546. rctl |= E1000_RCTL_BSEX;
  1547. switch (adapter->rx_buffer_len) {
  1548. case E1000_RXBUFFER_2048:
  1549. default:
  1550. rctl |= E1000_RCTL_SZ_2048;
  1551. rctl &= ~E1000_RCTL_BSEX;
  1552. break;
  1553. case E1000_RXBUFFER_4096:
  1554. rctl |= E1000_RCTL_SZ_4096;
  1555. break;
  1556. case E1000_RXBUFFER_8192:
  1557. rctl |= E1000_RCTL_SZ_8192;
  1558. break;
  1559. case E1000_RXBUFFER_16384:
  1560. rctl |= E1000_RCTL_SZ_16384;
  1561. break;
  1562. }
  1563. ew32(RCTL, rctl);
  1564. }
  1565. /**
  1566. * e1000_configure_rx - Configure 8254x Receive Unit after Reset
  1567. * @adapter: board private structure
  1568. *
  1569. * Configure the Rx unit of the MAC after a reset.
  1570. **/
  1571. static void e1000_configure_rx(struct e1000_adapter *adapter)
  1572. {
  1573. u64 rdba;
  1574. struct e1000_hw *hw = &adapter->hw;
  1575. u32 rdlen, rctl, rxcsum;
  1576. if (adapter->netdev->mtu > ETH_DATA_LEN) {
  1577. rdlen = adapter->rx_ring[0].count *
  1578. sizeof(struct e1000_rx_desc);
  1579. adapter->clean_rx = e1000_clean_jumbo_rx_irq;
  1580. adapter->alloc_rx_buf = e1000_alloc_jumbo_rx_buffers;
  1581. } else {
  1582. rdlen = adapter->rx_ring[0].count *
  1583. sizeof(struct e1000_rx_desc);
  1584. adapter->clean_rx = e1000_clean_rx_irq;
  1585. adapter->alloc_rx_buf = e1000_alloc_rx_buffers;
  1586. }
  1587. /* disable receives while setting up the descriptors */
  1588. rctl = er32(RCTL);
  1589. ew32(RCTL, rctl & ~E1000_RCTL_EN);
  1590. /* set the Receive Delay Timer Register */
  1591. ew32(RDTR, adapter->rx_int_delay);
  1592. if (hw->mac_type >= e1000_82540) {
  1593. ew32(RADV, adapter->rx_abs_int_delay);
  1594. if (adapter->itr_setting != 0)
  1595. ew32(ITR, 1000000000 / (adapter->itr * 256));
  1596. }
  1597. /* Setup the HW Rx Head and Tail Descriptor Pointers and
  1598. * the Base and Length of the Rx Descriptor Ring */
  1599. switch (adapter->num_rx_queues) {
  1600. case 1:
  1601. default:
  1602. rdba = adapter->rx_ring[0].dma;
  1603. ew32(RDLEN, rdlen);
  1604. ew32(RDBAH, (rdba >> 32));
  1605. ew32(RDBAL, (rdba & 0x00000000ffffffffULL));
  1606. ew32(RDT, 0);
  1607. ew32(RDH, 0);
  1608. adapter->rx_ring[0].rdh = ((hw->mac_type >= e1000_82543) ? E1000_RDH : E1000_82542_RDH);
  1609. adapter->rx_ring[0].rdt = ((hw->mac_type >= e1000_82543) ? E1000_RDT : E1000_82542_RDT);
  1610. break;
  1611. }
  1612. /* Enable 82543 Receive Checksum Offload for TCP and UDP */
  1613. if (hw->mac_type >= e1000_82543) {
  1614. rxcsum = er32(RXCSUM);
  1615. if (adapter->rx_csum)
  1616. rxcsum |= E1000_RXCSUM_TUOFL;
  1617. else
  1618. /* don't need to clear IPPCSE as it defaults to 0 */
  1619. rxcsum &= ~E1000_RXCSUM_TUOFL;
  1620. ew32(RXCSUM, rxcsum);
  1621. }
  1622. /* Enable Receives */
  1623. ew32(RCTL, rctl | E1000_RCTL_EN);
  1624. }
  1625. /**
  1626. * e1000_free_tx_resources - Free Tx Resources per Queue
  1627. * @adapter: board private structure
  1628. * @tx_ring: Tx descriptor ring for a specific queue
  1629. *
  1630. * Free all transmit software resources
  1631. **/
  1632. static void e1000_free_tx_resources(struct e1000_adapter *adapter,
  1633. struct e1000_tx_ring *tx_ring)
  1634. {
  1635. struct pci_dev *pdev = adapter->pdev;
  1636. e1000_clean_tx_ring(adapter, tx_ring);
  1637. vfree(tx_ring->buffer_info);
  1638. tx_ring->buffer_info = NULL;
  1639. dma_free_coherent(&pdev->dev, tx_ring->size, tx_ring->desc,
  1640. tx_ring->dma);
  1641. tx_ring->desc = NULL;
  1642. }
  1643. /**
  1644. * e1000_free_all_tx_resources - Free Tx Resources for All Queues
  1645. * @adapter: board private structure
  1646. *
  1647. * Free all transmit software resources
  1648. **/
  1649. void e1000_free_all_tx_resources(struct e1000_adapter *adapter)
  1650. {
  1651. int i;
  1652. for (i = 0; i < adapter->num_tx_queues; i++)
  1653. e1000_free_tx_resources(adapter, &adapter->tx_ring[i]);
  1654. }
  1655. static void e1000_unmap_and_free_tx_resource(struct e1000_adapter *adapter,
  1656. struct e1000_buffer *buffer_info)
  1657. {
  1658. if (buffer_info->dma) {
  1659. if (buffer_info->mapped_as_page)
  1660. dma_unmap_page(&adapter->pdev->dev, buffer_info->dma,
  1661. buffer_info->length, DMA_TO_DEVICE);
  1662. else
  1663. dma_unmap_single(&adapter->pdev->dev, buffer_info->dma,
  1664. buffer_info->length,
  1665. DMA_TO_DEVICE);
  1666. buffer_info->dma = 0;
  1667. }
  1668. if (buffer_info->skb) {
  1669. dev_kfree_skb_any(buffer_info->skb);
  1670. buffer_info->skb = NULL;
  1671. }
  1672. buffer_info->time_stamp = 0;
  1673. /* buffer_info must be completely set up in the transmit path */
  1674. }
  1675. /**
  1676. * e1000_clean_tx_ring - Free Tx Buffers
  1677. * @adapter: board private structure
  1678. * @tx_ring: ring to be cleaned
  1679. **/
  1680. static void e1000_clean_tx_ring(struct e1000_adapter *adapter,
  1681. struct e1000_tx_ring *tx_ring)
  1682. {
  1683. struct e1000_hw *hw = &adapter->hw;
  1684. struct e1000_buffer *buffer_info;
  1685. unsigned long size;
  1686. unsigned int i;
  1687. /* Free all the Tx ring sk_buffs */
  1688. for (i = 0; i < tx_ring->count; i++) {
  1689. buffer_info = &tx_ring->buffer_info[i];
  1690. e1000_unmap_and_free_tx_resource(adapter, buffer_info);
  1691. }
  1692. size = sizeof(struct e1000_buffer) * tx_ring->count;
  1693. memset(tx_ring->buffer_info, 0, size);
  1694. /* Zero out the descriptor ring */
  1695. memset(tx_ring->desc, 0, tx_ring->size);
  1696. tx_ring->next_to_use = 0;
  1697. tx_ring->next_to_clean = 0;
  1698. tx_ring->last_tx_tso = 0;
  1699. writel(0, hw->hw_addr + tx_ring->tdh);
  1700. writel(0, hw->hw_addr + tx_ring->tdt);
  1701. }
  1702. /**
  1703. * e1000_clean_all_tx_rings - Free Tx Buffers for all queues
  1704. * @adapter: board private structure
  1705. **/
  1706. static void e1000_clean_all_tx_rings(struct e1000_adapter *adapter)
  1707. {
  1708. int i;
  1709. for (i = 0; i < adapter->num_tx_queues; i++)
  1710. e1000_clean_tx_ring(adapter, &adapter->tx_ring[i]);
  1711. }
  1712. /**
  1713. * e1000_free_rx_resources - Free Rx Resources
  1714. * @adapter: board private structure
  1715. * @rx_ring: ring to clean the resources from
  1716. *
  1717. * Free all receive software resources
  1718. **/
  1719. static void e1000_free_rx_resources(struct e1000_adapter *adapter,
  1720. struct e1000_rx_ring *rx_ring)
  1721. {
  1722. struct pci_dev *pdev = adapter->pdev;
  1723. e1000_clean_rx_ring(adapter, rx_ring);
  1724. vfree(rx_ring->buffer_info);
  1725. rx_ring->buffer_info = NULL;
  1726. dma_free_coherent(&pdev->dev, rx_ring->size, rx_ring->desc,
  1727. rx_ring->dma);
  1728. rx_ring->desc = NULL;
  1729. }
  1730. /**
  1731. * e1000_free_all_rx_resources - Free Rx Resources for All Queues
  1732. * @adapter: board private structure
  1733. *
  1734. * Free all receive software resources
  1735. **/
  1736. void e1000_free_all_rx_resources(struct e1000_adapter *adapter)
  1737. {
  1738. int i;
  1739. for (i = 0; i < adapter->num_rx_queues; i++)
  1740. e1000_free_rx_resources(adapter, &adapter->rx_ring[i]);
  1741. }
  1742. /**
  1743. * e1000_clean_rx_ring - Free Rx Buffers per Queue
  1744. * @adapter: board private structure
  1745. * @rx_ring: ring to free buffers from
  1746. **/
  1747. static void e1000_clean_rx_ring(struct e1000_adapter *adapter,
  1748. struct e1000_rx_ring *rx_ring)
  1749. {
  1750. struct e1000_hw *hw = &adapter->hw;
  1751. struct e1000_buffer *buffer_info;
  1752. struct pci_dev *pdev = adapter->pdev;
  1753. unsigned long size;
  1754. unsigned int i;
  1755. /* Free all the Rx ring sk_buffs */
  1756. for (i = 0; i < rx_ring->count; i++) {
  1757. buffer_info = &rx_ring->buffer_info[i];
  1758. if (buffer_info->dma &&
  1759. adapter->clean_rx == e1000_clean_rx_irq) {
  1760. dma_unmap_single(&pdev->dev, buffer_info->dma,
  1761. buffer_info->length,
  1762. DMA_FROM_DEVICE);
  1763. } else if (buffer_info->dma &&
  1764. adapter->clean_rx == e1000_clean_jumbo_rx_irq) {
  1765. dma_unmap_page(&pdev->dev, buffer_info->dma,
  1766. buffer_info->length,
  1767. DMA_FROM_DEVICE);
  1768. }
  1769. buffer_info->dma = 0;
  1770. if (buffer_info->page) {
  1771. put_page(buffer_info->page);
  1772. buffer_info->page = NULL;
  1773. }
  1774. if (buffer_info->skb) {
  1775. dev_kfree_skb(buffer_info->skb);
  1776. buffer_info->skb = NULL;
  1777. }
  1778. }
  1779. /* there also may be some cached data from a chained receive */
  1780. if (rx_ring->rx_skb_top) {
  1781. dev_kfree_skb(rx_ring->rx_skb_top);
  1782. rx_ring->rx_skb_top = NULL;
  1783. }
  1784. size = sizeof(struct e1000_buffer) * rx_ring->count;
  1785. memset(rx_ring->buffer_info, 0, size);
  1786. /* Zero out the descriptor ring */
  1787. memset(rx_ring->desc, 0, rx_ring->size);
  1788. rx_ring->next_to_clean = 0;
  1789. rx_ring->next_to_use = 0;
  1790. writel(0, hw->hw_addr + rx_ring->rdh);
  1791. writel(0, hw->hw_addr + rx_ring->rdt);
  1792. }
  1793. /**
  1794. * e1000_clean_all_rx_rings - Free Rx Buffers for all queues
  1795. * @adapter: board private structure
  1796. **/
  1797. static void e1000_clean_all_rx_rings(struct e1000_adapter *adapter)
  1798. {
  1799. int i;
  1800. for (i = 0; i < adapter->num_rx_queues; i++)
  1801. e1000_clean_rx_ring(adapter, &adapter->rx_ring[i]);
  1802. }
  1803. /* The 82542 2.0 (revision 2) needs to have the receive unit in reset
  1804. * and memory write and invalidate disabled for certain operations
  1805. */
  1806. static void e1000_enter_82542_rst(struct e1000_adapter *adapter)
  1807. {
  1808. struct e1000_hw *hw = &adapter->hw;
  1809. struct net_device *netdev = adapter->netdev;
  1810. u32 rctl;
  1811. e1000_pci_clear_mwi(hw);
  1812. rctl = er32(RCTL);
  1813. rctl |= E1000_RCTL_RST;
  1814. ew32(RCTL, rctl);
  1815. E1000_WRITE_FLUSH();
  1816. mdelay(5);
  1817. if (netif_running(netdev))
  1818. e1000_clean_all_rx_rings(adapter);
  1819. }
  1820. static void e1000_leave_82542_rst(struct e1000_adapter *adapter)
  1821. {
  1822. struct e1000_hw *hw = &adapter->hw;
  1823. struct net_device *netdev = adapter->netdev;
  1824. u32 rctl;
  1825. rctl = er32(RCTL);
  1826. rctl &= ~E1000_RCTL_RST;
  1827. ew32(RCTL, rctl);
  1828. E1000_WRITE_FLUSH();
  1829. mdelay(5);
  1830. if (hw->pci_cmd_word & PCI_COMMAND_INVALIDATE)
  1831. e1000_pci_set_mwi(hw);
  1832. if (netif_running(netdev)) {
  1833. /* No need to loop, because 82542 supports only 1 queue */
  1834. struct e1000_rx_ring *ring = &adapter->rx_ring[0];
  1835. e1000_configure_rx(adapter);
  1836. adapter->alloc_rx_buf(adapter, ring, E1000_DESC_UNUSED(ring));
  1837. }
  1838. }
  1839. /**
  1840. * e1000_set_mac - Change the Ethernet Address of the NIC
  1841. * @netdev: network interface device structure
  1842. * @p: pointer to an address structure
  1843. *
  1844. * Returns 0 on success, negative on failure
  1845. **/
  1846. static int e1000_set_mac(struct net_device *netdev, void *p)
  1847. {
  1848. struct e1000_adapter *adapter = netdev_priv(netdev);
  1849. struct e1000_hw *hw = &adapter->hw;
  1850. struct sockaddr *addr = p;
  1851. if (!is_valid_ether_addr(addr->sa_data))
  1852. return -EADDRNOTAVAIL;
  1853. /* 82542 2.0 needs to be in reset to write receive address registers */
  1854. if (hw->mac_type == e1000_82542_rev2_0)
  1855. e1000_enter_82542_rst(adapter);
  1856. memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
  1857. memcpy(hw->mac_addr, addr->sa_data, netdev->addr_len);
  1858. e1000_rar_set(hw, hw->mac_addr, 0);
  1859. if (hw->mac_type == e1000_82542_rev2_0)
  1860. e1000_leave_82542_rst(adapter);
  1861. return 0;
  1862. }
  1863. /**
  1864. * e1000_set_rx_mode - Secondary Unicast, Multicast and Promiscuous mode set
  1865. * @netdev: network interface device structure
  1866. *
  1867. * The set_rx_mode entry point is called whenever the unicast or multicast
  1868. * address lists or the network interface flags are updated. This routine is
  1869. * responsible for configuring the hardware for proper unicast, multicast,
  1870. * promiscuous mode, and all-multi behavior.
  1871. **/
  1872. static void e1000_set_rx_mode(struct net_device *netdev)
  1873. {
  1874. struct e1000_adapter *adapter = netdev_priv(netdev);
  1875. struct e1000_hw *hw = &adapter->hw;
  1876. struct netdev_hw_addr *ha;
  1877. bool use_uc = false;
  1878. u32 rctl;
  1879. u32 hash_value;
  1880. int i, rar_entries = E1000_RAR_ENTRIES;
  1881. int mta_reg_count = E1000_NUM_MTA_REGISTERS;
  1882. u32 *mcarray = kcalloc(mta_reg_count, sizeof(u32), GFP_ATOMIC);
  1883. if (!mcarray) {
  1884. e_err(probe, "memory allocation failed\n");
  1885. return;
  1886. }
  1887. /* Check for Promiscuous and All Multicast modes */
  1888. rctl = er32(RCTL);
  1889. if (netdev->flags & IFF_PROMISC) {
  1890. rctl |= (E1000_RCTL_UPE | E1000_RCTL_MPE);
  1891. rctl &= ~E1000_RCTL_VFE;
  1892. } else {
  1893. if (netdev->flags & IFF_ALLMULTI)
  1894. rctl |= E1000_RCTL_MPE;
  1895. else
  1896. rctl &= ~E1000_RCTL_MPE;
  1897. /* Enable VLAN filter if there is a VLAN */
  1898. if (e1000_vlan_used(adapter))
  1899. rctl |= E1000_RCTL_VFE;
  1900. }
  1901. if (netdev_uc_count(netdev) > rar_entries - 1) {
  1902. rctl |= E1000_RCTL_UPE;
  1903. } else if (!(netdev->flags & IFF_PROMISC)) {
  1904. rctl &= ~E1000_RCTL_UPE;
  1905. use_uc = true;
  1906. }
  1907. ew32(RCTL, rctl);
  1908. /* 82542 2.0 needs to be in reset to write receive address registers */
  1909. if (hw->mac_type == e1000_82542_rev2_0)
  1910. e1000_enter_82542_rst(adapter);
  1911. /* load the first 14 addresses into the exact filters 1-14. Unicast
  1912. * addresses take precedence to avoid disabling unicast filtering
  1913. * when possible.
  1914. *
  1915. * RAR 0 is used for the station MAC address
  1916. * if there are not 14 addresses, go ahead and clear the filters
  1917. */
  1918. i = 1;
  1919. if (use_uc)
  1920. netdev_for_each_uc_addr(ha, netdev) {
  1921. if (i == rar_entries)
  1922. break;
  1923. e1000_rar_set(hw, ha->addr, i++);
  1924. }
  1925. netdev_for_each_mc_addr(ha, netdev) {
  1926. if (i == rar_entries) {
  1927. /* load any remaining addresses into the hash table */
  1928. u32 hash_reg, hash_bit, mta;
  1929. hash_value = e1000_hash_mc_addr(hw, ha->addr);
  1930. hash_reg = (hash_value >> 5) & 0x7F;
  1931. hash_bit = hash_value & 0x1F;
  1932. mta = (1 << hash_bit);
  1933. mcarray[hash_reg] |= mta;
  1934. } else {
  1935. e1000_rar_set(hw, ha->addr, i++);
  1936. }
  1937. }
  1938. for (; i < rar_entries; i++) {
  1939. E1000_WRITE_REG_ARRAY(hw, RA, i << 1, 0);
  1940. E1000_WRITE_FLUSH();
  1941. E1000_WRITE_REG_ARRAY(hw, RA, (i << 1) + 1, 0);
  1942. E1000_WRITE_FLUSH();
  1943. }
  1944. /* write the hash table completely, write from bottom to avoid
  1945. * both stupid write combining chipsets, and flushing each write */
  1946. for (i = mta_reg_count - 1; i >= 0 ; i--) {
  1947. /*
  1948. * If we are on an 82544 has an errata where writing odd
  1949. * offsets overwrites the previous even offset, but writing
  1950. * backwards over the range solves the issue by always
  1951. * writing the odd offset first
  1952. */
  1953. E1000_WRITE_REG_ARRAY(hw, MTA, i, mcarray[i]);
  1954. }
  1955. E1000_WRITE_FLUSH();
  1956. if (hw->mac_type == e1000_82542_rev2_0)
  1957. e1000_leave_82542_rst(adapter);
  1958. kfree(mcarray);
  1959. }
  1960. /* Need to wait a few seconds after link up to get diagnostic information from
  1961. * the phy */
  1962. static void e1000_update_phy_info(unsigned long data)
  1963. {
  1964. struct e1000_adapter *adapter = (struct e1000_adapter *)data;
  1965. schedule_work(&adapter->phy_info_task);
  1966. }
  1967. static void e1000_update_phy_info_task(struct work_struct *work)
  1968. {
  1969. struct e1000_adapter *adapter = container_of(work,
  1970. struct e1000_adapter,
  1971. phy_info_task);
  1972. struct e1000_hw *hw = &adapter->hw;
  1973. rtnl_lock();
  1974. e1000_phy_get_info(hw, &adapter->phy_info);
  1975. rtnl_unlock();
  1976. }
  1977. /**
  1978. * e1000_82547_tx_fifo_stall - Timer Call-back
  1979. * @data: pointer to adapter cast into an unsigned long
  1980. **/
  1981. static void e1000_82547_tx_fifo_stall(unsigned long data)
  1982. {
  1983. struct e1000_adapter *adapter = (struct e1000_adapter *)data;
  1984. schedule_work(&adapter->fifo_stall_task);
  1985. }
  1986. /**
  1987. * e1000_82547_tx_fifo_stall_task - task to complete work
  1988. * @work: work struct contained inside adapter struct
  1989. **/
  1990. static void e1000_82547_tx_fifo_stall_task(struct work_struct *work)
  1991. {
  1992. struct e1000_adapter *adapter = container_of(work,
  1993. struct e1000_adapter,
  1994. fifo_stall_task);
  1995. struct e1000_hw *hw = &adapter->hw;
  1996. struct net_device *netdev = adapter->netdev;
  1997. u32 tctl;
  1998. rtnl_lock();
  1999. if (atomic_read(&adapter->tx_fifo_stall)) {
  2000. if ((er32(TDT) == er32(TDH)) &&
  2001. (er32(TDFT) == er32(TDFH)) &&
  2002. (er32(TDFTS) == er32(TDFHS))) {
  2003. tctl = er32(TCTL);
  2004. ew32(TCTL, tctl & ~E1000_TCTL_EN);
  2005. ew32(TDFT, adapter->tx_head_addr);
  2006. ew32(TDFH, adapter->tx_head_addr);
  2007. ew32(TDFTS, adapter->tx_head_addr);
  2008. ew32(TDFHS, adapter->tx_head_addr);
  2009. ew32(TCTL, tctl);
  2010. E1000_WRITE_FLUSH();
  2011. adapter->tx_fifo_head = 0;
  2012. atomic_set(&adapter->tx_fifo_stall, 0);
  2013. netif_wake_queue(netdev);
  2014. } else if (!test_bit(__E1000_DOWN, &adapter->flags)) {
  2015. mod_timer(&adapter->tx_fifo_stall_timer, jiffies + 1);
  2016. }
  2017. }
  2018. rtnl_unlock();
  2019. }
  2020. bool e1000_has_link(struct e1000_adapter *adapter)
  2021. {
  2022. struct e1000_hw *hw = &adapter->hw;
  2023. bool link_active = false;
  2024. /* get_link_status is set on LSC (link status) interrupt or rx
  2025. * sequence error interrupt (except on intel ce4100).
  2026. * get_link_status will stay false until the
  2027. * e1000_check_for_link establishes link for copper adapters
  2028. * ONLY
  2029. */
  2030. switch (hw->media_type) {
  2031. case e1000_media_type_copper:
  2032. if (hw->mac_type == e1000_ce4100)
  2033. hw->get_link_status = 1;
  2034. if (hw->get_link_status) {
  2035. e1000_check_for_link(hw);
  2036. link_active = !hw->get_link_status;
  2037. } else {
  2038. link_active = true;
  2039. }
  2040. break;
  2041. case e1000_media_type_fiber:
  2042. e1000_check_for_link(hw);
  2043. link_active = !!(er32(STATUS) & E1000_STATUS_LU);
  2044. break;
  2045. case e1000_media_type_internal_serdes:
  2046. e1000_check_for_link(hw);
  2047. link_active = hw->serdes_has_link;
  2048. break;
  2049. default:
  2050. break;
  2051. }
  2052. return link_active;
  2053. }
  2054. /**
  2055. * e1000_watchdog - Timer Call-back
  2056. * @data: pointer to adapter cast into an unsigned long
  2057. **/
  2058. static void e1000_watchdog(unsigned long data)
  2059. {
  2060. struct e1000_adapter *adapter = (struct e1000_adapter *)data;
  2061. struct e1000_hw *hw = &adapter->hw;
  2062. struct net_device *netdev = adapter->netdev;
  2063. struct e1000_tx_ring *txdr = adapter->tx_ring;
  2064. u32 link, tctl;
  2065. link = e1000_has_link(adapter);
  2066. if ((netif_carrier_ok(netdev)) && link)
  2067. goto link_up;
  2068. if (link) {
  2069. if (!netif_carrier_ok(netdev)) {
  2070. u32 ctrl;
  2071. bool txb2b = true;
  2072. /* update snapshot of PHY registers on LSC */
  2073. e1000_get_speed_and_duplex(hw,
  2074. &adapter->link_speed,
  2075. &adapter->link_duplex);
  2076. ctrl = er32(CTRL);
  2077. pr_info("%s NIC Link is Up %d Mbps %s, "
  2078. "Flow Control: %s\n",
  2079. netdev->name,
  2080. adapter->link_speed,
  2081. adapter->link_duplex == FULL_DUPLEX ?
  2082. "Full Duplex" : "Half Duplex",
  2083. ((ctrl & E1000_CTRL_TFCE) && (ctrl &
  2084. E1000_CTRL_RFCE)) ? "RX/TX" : ((ctrl &
  2085. E1000_CTRL_RFCE) ? "RX" : ((ctrl &
  2086. E1000_CTRL_TFCE) ? "TX" : "None")));
  2087. /* adjust timeout factor according to speed/duplex */
  2088. adapter->tx_timeout_factor = 1;
  2089. switch (adapter->link_speed) {
  2090. case SPEED_10:
  2091. txb2b = false;
  2092. adapter->tx_timeout_factor = 16;
  2093. break;
  2094. case SPEED_100:
  2095. txb2b = false;
  2096. /* maybe add some timeout factor ? */
  2097. break;
  2098. }
  2099. /* enable transmits in the hardware */
  2100. tctl = er32(TCTL);
  2101. tctl |= E1000_TCTL_EN;
  2102. ew32(TCTL, tctl);
  2103. netif_carrier_on(netdev);
  2104. if (!test_bit(__E1000_DOWN, &adapter->flags))
  2105. mod_timer(&adapter->phy_info_timer,
  2106. round_jiffies(jiffies + 2 * HZ));
  2107. adapter->smartspeed = 0;
  2108. }
  2109. } else {
  2110. if (netif_carrier_ok(netdev)) {
  2111. adapter->link_speed = 0;
  2112. adapter->link_duplex = 0;
  2113. pr_info("%s NIC Link is Down\n",
  2114. netdev->name);
  2115. netif_carrier_off(netdev);
  2116. if (!test_bit(__E1000_DOWN, &adapter->flags))
  2117. mod_timer(&adapter->phy_info_timer,
  2118. round_jiffies(jiffies + 2 * HZ));
  2119. }
  2120. e1000_smartspeed(adapter);
  2121. }
  2122. link_up:
  2123. e1000_update_stats(adapter);
  2124. hw->tx_packet_delta = adapter->stats.tpt - adapter->tpt_old;
  2125. adapter->tpt_old = adapter->stats.tpt;
  2126. hw->collision_delta = adapter->stats.colc - adapter->colc_old;
  2127. adapter->colc_old = adapter->stats.colc;
  2128. adapter->gorcl = adapter->stats.gorcl - adapter->gorcl_old;
  2129. adapter->gorcl_old = adapter->stats.gorcl;
  2130. adapter->gotcl = adapter->stats.gotcl - adapter->gotcl_old;
  2131. adapter->gotcl_old = adapter->stats.gotcl;
  2132. e1000_update_adaptive(hw);
  2133. if (!netif_carrier_ok(netdev)) {
  2134. if (E1000_DESC_UNUSED(txdr) + 1 < txdr->count) {
  2135. /* We've lost link, so the controller stops DMA,
  2136. * but we've got queued Tx work that's never going
  2137. * to get done, so reset controller to flush Tx.
  2138. * (Do the reset outside of interrupt context). */
  2139. adapter->tx_timeout_count++;
  2140. schedule_work(&adapter->reset_task);
  2141. /* return immediately since reset is imminent */
  2142. return;
  2143. }
  2144. }
  2145. /* Simple mode for Interrupt Throttle Rate (ITR) */
  2146. if (hw->mac_type >= e1000_82540 && adapter->itr_setting == 4) {
  2147. /*
  2148. * Symmetric Tx/Rx gets a reduced ITR=2000;
  2149. * Total asymmetrical Tx or Rx gets ITR=8000;
  2150. * everyone else is between 2000-8000.
  2151. */
  2152. u32 goc = (adapter->gotcl + adapter->gorcl) / 10000;
  2153. u32 dif = (adapter->gotcl > adapter->gorcl ?
  2154. adapter->gotcl - adapter->gorcl :
  2155. adapter->gorcl - adapter->gotcl) / 10000;
  2156. u32 itr = goc > 0 ? (dif * 6000 / goc + 2000) : 8000;
  2157. ew32(ITR, 1000000000 / (itr * 256));
  2158. }
  2159. /* Cause software interrupt to ensure rx ring is cleaned */
  2160. ew32(ICS, E1000_ICS_RXDMT0);
  2161. /* Force detection of hung controller every watchdog period */
  2162. adapter->detect_tx_hung = true;
  2163. /* Reset the timer */
  2164. if (!test_bit(__E1000_DOWN, &adapter->flags))
  2165. mod_timer(&adapter->watchdog_timer,
  2166. round_jiffies(jiffies + 2 * HZ));
  2167. }
  2168. enum latency_range {
  2169. lowest_latency = 0,
  2170. low_latency = 1,
  2171. bulk_latency = 2,
  2172. latency_invalid = 255
  2173. };
  2174. /**
  2175. * e1000_update_itr - update the dynamic ITR value based on statistics
  2176. * @adapter: pointer to adapter
  2177. * @itr_setting: current adapter->itr
  2178. * @packets: the number of packets during this measurement interval
  2179. * @bytes: the number of bytes during this measurement interval
  2180. *
  2181. * Stores a new ITR value based on packets and byte
  2182. * counts during the last interrupt. The advantage of per interrupt
  2183. * computation is faster updates and more accurate ITR for the current
  2184. * traffic pattern. Constants in this function were computed
  2185. * based on theoretical maximum wire speed and thresholds were set based
  2186. * on testing data as well as attempting to minimize response time
  2187. * while increasing bulk throughput.
  2188. * this functionality is controlled by the InterruptThrottleRate module
  2189. * parameter (see e1000_param.c)
  2190. **/
  2191. static unsigned int e1000_update_itr(struct e1000_adapter *adapter,
  2192. u16 itr_setting, int packets, int bytes)
  2193. {
  2194. unsigned int retval = itr_setting;
  2195. struct e1000_hw *hw = &adapter->hw;
  2196. if (unlikely(hw->mac_type < e1000_82540))
  2197. goto update_itr_done;
  2198. if (packets == 0)
  2199. goto update_itr_done;
  2200. switch (itr_setting) {
  2201. case lowest_latency:
  2202. /* jumbo frames get bulk treatment*/
  2203. if (bytes/packets > 8000)
  2204. retval = bulk_latency;
  2205. else if ((packets < 5) && (bytes > 512))
  2206. retval = low_latency;
  2207. break;
  2208. case low_latency: /* 50 usec aka 20000 ints/s */
  2209. if (bytes > 10000) {
  2210. /* jumbo frames need bulk latency setting */
  2211. if (bytes/packets > 8000)
  2212. retval = bulk_latency;
  2213. else if ((packets < 10) || ((bytes/packets) > 1200))
  2214. retval = bulk_latency;
  2215. else if ((packets > 35))
  2216. retval = lowest_latency;
  2217. } else if (bytes/packets > 2000)
  2218. retval = bulk_latency;
  2219. else if (packets <= 2 && bytes < 512)
  2220. retval = lowest_latency;
  2221. break;
  2222. case bulk_latency: /* 250 usec aka 4000 ints/s */
  2223. if (bytes > 25000) {
  2224. if (packets > 35)
  2225. retval = low_latency;
  2226. } else if (bytes < 6000) {
  2227. retval = low_latency;
  2228. }
  2229. break;
  2230. }
  2231. update_itr_done:
  2232. return retval;
  2233. }
  2234. static void e1000_set_itr(struct e1000_adapter *adapter)
  2235. {
  2236. struct e1000_hw *hw = &adapter->hw;
  2237. u16 current_itr;
  2238. u32 new_itr = adapter->itr;
  2239. if (unlikely(hw->mac_type < e1000_82540))
  2240. return;
  2241. /* for non-gigabit speeds, just fix the interrupt rate at 4000 */
  2242. if (unlikely(adapter->link_speed != SPEED_1000)) {
  2243. current_itr = 0;
  2244. new_itr = 4000;
  2245. goto set_itr_now;
  2246. }
  2247. adapter->tx_itr = e1000_update_itr(adapter,
  2248. adapter->tx_itr,
  2249. adapter->total_tx_packets,
  2250. adapter->total_tx_bytes);
  2251. /* conservative mode (itr 3) eliminates the lowest_latency setting */
  2252. if (adapter->itr_setting == 3 && adapter->tx_itr == lowest_latency)
  2253. adapter->tx_itr = low_latency;
  2254. adapter->rx_itr = e1000_update_itr(adapter,
  2255. adapter->rx_itr,
  2256. adapter->total_rx_packets,
  2257. adapter->total_rx_bytes);
  2258. /* conservative mode (itr 3) eliminates the lowest_latency setting */
  2259. if (adapter->itr_setting == 3 && adapter->rx_itr == lowest_latency)
  2260. adapter->rx_itr = low_latency;
  2261. current_itr = max(adapter->rx_itr, adapter->tx_itr);
  2262. switch (current_itr) {
  2263. /* counts and packets in update_itr are dependent on these numbers */
  2264. case lowest_latency:
  2265. new_itr = 70000;
  2266. break;
  2267. case low_latency:
  2268. new_itr = 20000; /* aka hwitr = ~200 */
  2269. break;
  2270. case bulk_latency:
  2271. new_itr = 4000;
  2272. break;
  2273. default:
  2274. break;
  2275. }
  2276. set_itr_now:
  2277. if (new_itr != adapter->itr) {
  2278. /* this attempts to bias the interrupt rate towards Bulk
  2279. * by adding intermediate steps when interrupt rate is
  2280. * increasing */
  2281. new_itr = new_itr > adapter->itr ?
  2282. min(adapter->itr + (new_itr >> 2), new_itr) :
  2283. new_itr;
  2284. adapter->itr = new_itr;
  2285. ew32(ITR, 1000000000 / (new_itr * 256));
  2286. }
  2287. }
  2288. #define E1000_TX_FLAGS_CSUM 0x00000001
  2289. #define E1000_TX_FLAGS_VLAN 0x00000002
  2290. #define E1000_TX_FLAGS_TSO 0x00000004
  2291. #define E1000_TX_FLAGS_IPV4 0x00000008
  2292. #define E1000_TX_FLAGS_VLAN_MASK 0xffff0000
  2293. #define E1000_TX_FLAGS_VLAN_SHIFT 16
  2294. static int e1000_tso(struct e1000_adapter *adapter,
  2295. struct e1000_tx_ring *tx_ring, struct sk_buff *skb)
  2296. {
  2297. struct e1000_context_desc *context_desc;
  2298. struct e1000_buffer *buffer_info;
  2299. unsigned int i;
  2300. u32 cmd_length = 0;
  2301. u16 ipcse = 0, tucse, mss;
  2302. u8 ipcss, ipcso, tucss, tucso, hdr_len;
  2303. int err;
  2304. if (skb_is_gso(skb)) {
  2305. if (skb_header_cloned(skb)) {
  2306. err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
  2307. if (err)
  2308. return err;
  2309. }
  2310. hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
  2311. mss = skb_shinfo(skb)->gso_size;
  2312. if (skb->protocol == htons(ETH_P_IP)) {
  2313. struct iphdr *iph = ip_hdr(skb);
  2314. iph->tot_len = 0;
  2315. iph->check = 0;
  2316. tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
  2317. iph->daddr, 0,
  2318. IPPROTO_TCP,
  2319. 0);
  2320. cmd_length = E1000_TXD_CMD_IP;
  2321. ipcse = skb_transport_offset(skb) - 1;
  2322. } else if (skb->protocol == htons(ETH_P_IPV6)) {
  2323. ipv6_hdr(skb)->payload_len = 0;
  2324. tcp_hdr(skb)->check =
  2325. ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
  2326. &ipv6_hdr(skb)->daddr,
  2327. 0, IPPROTO_TCP, 0);
  2328. ipcse = 0;
  2329. }
  2330. ipcss = skb_network_offset(skb);
  2331. ipcso = (void *)&(ip_hdr(skb)->check) - (void *)skb->data;
  2332. tucss = skb_transport_offset(skb);
  2333. tucso = (void *)&(tcp_hdr(skb)->check) - (void *)skb->data;
  2334. tucse = 0;
  2335. cmd_length |= (E1000_TXD_CMD_DEXT | E1000_TXD_CMD_TSE |
  2336. E1000_TXD_CMD_TCP | (skb->len - (hdr_len)));
  2337. i = tx_ring->next_to_use;
  2338. context_desc = E1000_CONTEXT_DESC(*tx_ring, i);
  2339. buffer_info = &tx_ring->buffer_info[i];
  2340. context_desc->lower_setup.ip_fields.ipcss = ipcss;
  2341. context_desc->lower_setup.ip_fields.ipcso = ipcso;
  2342. context_desc->lower_setup.ip_fields.ipcse = cpu_to_le16(ipcse);
  2343. context_desc->upper_setup.tcp_fields.tucss = tucss;
  2344. context_desc->upper_setup.tcp_fields.tucso = tucso;
  2345. context_desc->upper_setup.tcp_fields.tucse = cpu_to_le16(tucse);
  2346. context_desc->tcp_seg_setup.fields.mss = cpu_to_le16(mss);
  2347. context_desc->tcp_seg_setup.fields.hdr_len = hdr_len;
  2348. context_desc->cmd_and_length = cpu_to_le32(cmd_length);
  2349. buffer_info->time_stamp = jiffies;
  2350. buffer_info->next_to_watch = i;
  2351. if (++i == tx_ring->count) i = 0;
  2352. tx_ring->next_to_use = i;
  2353. return true;
  2354. }
  2355. return false;
  2356. }
  2357. static bool e1000_tx_csum(struct e1000_adapter *adapter,
  2358. struct e1000_tx_ring *tx_ring, struct sk_buff *skb)
  2359. {
  2360. struct e1000_context_desc *context_desc;
  2361. struct e1000_buffer *buffer_info;
  2362. unsigned int i;
  2363. u8 css;
  2364. u32 cmd_len = E1000_TXD_CMD_DEXT;
  2365. if (skb->ip_summed != CHECKSUM_PARTIAL)
  2366. return false;
  2367. switch (skb->protocol) {
  2368. case cpu_to_be16(ETH_P_IP):
  2369. if (ip_hdr(skb)->protocol == IPPROTO_TCP)
  2370. cmd_len |= E1000_TXD_CMD_TCP;
  2371. break;
  2372. case cpu_to_be16(ETH_P_IPV6):
  2373. /* XXX not handling all IPV6 headers */
  2374. if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
  2375. cmd_len |= E1000_TXD_CMD_TCP;
  2376. break;
  2377. default:
  2378. if (unlikely(net_ratelimit()))
  2379. e_warn(drv, "checksum_partial proto=%x!\n",
  2380. skb->protocol);
  2381. break;
  2382. }
  2383. css = skb_checksum_start_offset(skb);
  2384. i = tx_ring->next_to_use;
  2385. buffer_info = &tx_ring->buffer_info[i];
  2386. context_desc = E1000_CONTEXT_DESC(*tx_ring, i);
  2387. context_desc->lower_setup.ip_config = 0;
  2388. context_desc->upper_setup.tcp_fields.tucss = css;
  2389. context_desc->upper_setup.tcp_fields.tucso =
  2390. css + skb->csum_offset;
  2391. context_desc->upper_setup.tcp_fields.tucse = 0;
  2392. context_desc->tcp_seg_setup.data = 0;
  2393. context_desc->cmd_and_length = cpu_to_le32(cmd_len);
  2394. buffer_info->time_stamp = jiffies;
  2395. buffer_info->next_to_watch = i;
  2396. if (unlikely(++i == tx_ring->count)) i = 0;
  2397. tx_ring->next_to_use = i;
  2398. return true;
  2399. }
  2400. #define E1000_MAX_TXD_PWR 12
  2401. #define E1000_MAX_DATA_PER_TXD (1<<E1000_MAX_TXD_PWR)
  2402. static int e1000_tx_map(struct e1000_adapter *adapter,
  2403. struct e1000_tx_ring *tx_ring,
  2404. struct sk_buff *skb, unsigned int first,
  2405. unsigned int max_per_txd, unsigned int nr_frags,
  2406. unsigned int mss)
  2407. {
  2408. struct e1000_hw *hw = &adapter->hw;
  2409. struct pci_dev *pdev = adapter->pdev;
  2410. struct e1000_buffer *buffer_info;
  2411. unsigned int len = skb_headlen(skb);
  2412. unsigned int offset = 0, size, count = 0, i;
  2413. unsigned int f, bytecount, segs;
  2414. i = tx_ring->next_to_use;
  2415. while (len) {
  2416. buffer_info = &tx_ring->buffer_info[i];
  2417. size = min(len, max_per_txd);
  2418. /* Workaround for Controller erratum --
  2419. * descriptor for non-tso packet in a linear SKB that follows a
  2420. * tso gets written back prematurely before the data is fully
  2421. * DMA'd to the controller */
  2422. if (!skb->data_len && tx_ring->last_tx_tso &&
  2423. !skb_is_gso(skb)) {
  2424. tx_ring->last_tx_tso = 0;
  2425. size -= 4;
  2426. }
  2427. /* Workaround for premature desc write-backs
  2428. * in TSO mode. Append 4-byte sentinel desc */
  2429. if (unlikely(mss && !nr_frags && size == len && size > 8))
  2430. size -= 4;
  2431. /* work-around for errata 10 and it applies
  2432. * to all controllers in PCI-X mode
  2433. * The fix is to make sure that the first descriptor of a
  2434. * packet is smaller than 2048 - 16 - 16 (or 2016) bytes
  2435. */
  2436. if (unlikely((hw->bus_type == e1000_bus_type_pcix) &&
  2437. (size > 2015) && count == 0))
  2438. size = 2015;
  2439. /* Workaround for potential 82544 hang in PCI-X. Avoid
  2440. * terminating buffers within evenly-aligned dwords. */
  2441. if (unlikely(adapter->pcix_82544 &&
  2442. !((unsigned long)(skb->data + offset + size - 1) & 4) &&
  2443. size > 4))
  2444. size -= 4;
  2445. buffer_info->length = size;
  2446. /* set time_stamp *before* dma to help avoid a possible race */
  2447. buffer_info->time_stamp = jiffies;
  2448. buffer_info->mapped_as_page = false;
  2449. buffer_info->dma = dma_map_single(&pdev->dev,
  2450. skb->data + offset,
  2451. size, DMA_TO_DEVICE);
  2452. if (dma_mapping_error(&pdev->dev, buffer_info->dma))
  2453. goto dma_error;
  2454. buffer_info->next_to_watch = i;
  2455. len -= size;
  2456. offset += size;
  2457. count++;
  2458. if (len) {
  2459. i++;
  2460. if (unlikely(i == tx_ring->count))
  2461. i = 0;
  2462. }
  2463. }
  2464. for (f = 0; f < nr_frags; f++) {
  2465. struct skb_frag_struct *frag;
  2466. frag = &skb_shinfo(skb)->frags[f];
  2467. len = frag->size;
  2468. offset = 0;
  2469. while (len) {
  2470. unsigned long bufend;
  2471. i++;
  2472. if (unlikely(i == tx_ring->count))
  2473. i = 0;
  2474. buffer_info = &tx_ring->buffer_info[i];
  2475. size = min(len, max_per_txd);
  2476. /* Workaround for premature desc write-backs
  2477. * in TSO mode. Append 4-byte sentinel desc */
  2478. if (unlikely(mss && f == (nr_frags-1) && size == len && size > 8))
  2479. size -= 4;
  2480. /* Workaround for potential 82544 hang in PCI-X.
  2481. * Avoid terminating buffers within evenly-aligned
  2482. * dwords. */
  2483. bufend = (unsigned long)
  2484. page_to_phys(skb_frag_page(frag));
  2485. bufend += offset + size - 1;
  2486. if (unlikely(adapter->pcix_82544 &&
  2487. !(bufend & 4) &&
  2488. size > 4))
  2489. size -= 4;
  2490. buffer_info->length = size;
  2491. buffer_info->time_stamp = jiffies;
  2492. buffer_info->mapped_as_page = true;
  2493. buffer_info->dma = skb_frag_dma_map(&pdev->dev, frag,
  2494. offset, size, DMA_TO_DEVICE);
  2495. if (dma_mapping_error(&pdev->dev, buffer_info->dma))
  2496. goto dma_error;
  2497. buffer_info->next_to_watch = i;
  2498. len -= size;
  2499. offset += size;
  2500. count++;
  2501. }
  2502. }
  2503. segs = skb_shinfo(skb)->gso_segs ?: 1;
  2504. /* multiply data chunks by size of headers */
  2505. bytecount = ((segs - 1) * skb_headlen(skb)) + skb->len;
  2506. tx_ring->buffer_info[i].skb = skb;
  2507. tx_ring->buffer_info[i].segs = segs;
  2508. tx_ring->buffer_info[i].bytecount = bytecount;
  2509. tx_ring->buffer_info[first].next_to_watch = i;
  2510. return count;
  2511. dma_error:
  2512. dev_err(&pdev->dev, "TX DMA map failed\n");
  2513. buffer_info->dma = 0;
  2514. if (count)
  2515. count--;
  2516. while (count--) {
  2517. if (i==0)
  2518. i += tx_ring->count;
  2519. i--;
  2520. buffer_info = &tx_ring->buffer_info[i];
  2521. e1000_unmap_and_free_tx_resource(adapter, buffer_info);
  2522. }
  2523. return 0;
  2524. }
  2525. static void e1000_tx_queue(struct e1000_adapter *adapter,
  2526. struct e1000_tx_ring *tx_ring, int tx_flags,
  2527. int count)
  2528. {
  2529. struct e1000_hw *hw = &adapter->hw;
  2530. struct e1000_tx_desc *tx_desc = NULL;
  2531. struct e1000_buffer *buffer_info;
  2532. u32 txd_upper = 0, txd_lower = E1000_TXD_CMD_IFCS;
  2533. unsigned int i;
  2534. if (likely(tx_flags & E1000_TX_FLAGS_TSO)) {
  2535. txd_lower |= E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D |
  2536. E1000_TXD_CMD_TSE;
  2537. txd_upper |= E1000_TXD_POPTS_TXSM << 8;
  2538. if (likely(tx_flags & E1000_TX_FLAGS_IPV4))
  2539. txd_upper |= E1000_TXD_POPTS_IXSM << 8;
  2540. }
  2541. if (likely(tx_flags & E1000_TX_FLAGS_CSUM)) {
  2542. txd_lower |= E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D;
  2543. txd_upper |= E1000_TXD_POPTS_TXSM << 8;
  2544. }
  2545. if (unlikely(tx_flags & E1000_TX_FLAGS_VLAN)) {
  2546. txd_lower |= E1000_TXD_CMD_VLE;
  2547. txd_upper |= (tx_flags & E1000_TX_FLAGS_VLAN_MASK);
  2548. }
  2549. i = tx_ring->next_to_use;
  2550. while (count--) {
  2551. buffer_info = &tx_ring->buffer_info[i];
  2552. tx_desc = E1000_TX_DESC(*tx_ring, i);
  2553. tx_desc->buffer_addr = cpu_to_le64(buffer_info->dma);
  2554. tx_desc->lower.data =
  2555. cpu_to_le32(txd_lower | buffer_info->length);
  2556. tx_desc->upper.data = cpu_to_le32(txd_upper);
  2557. if (unlikely(++i == tx_ring->count)) i = 0;
  2558. }
  2559. tx_desc->lower.data |= cpu_to_le32(adapter->txd_cmd);
  2560. /* Force memory writes to complete before letting h/w
  2561. * know there are new descriptors to fetch. (Only
  2562. * applicable for weak-ordered memory model archs,
  2563. * such as IA-64). */
  2564. wmb();
  2565. tx_ring->next_to_use = i;
  2566. writel(i, hw->hw_addr + tx_ring->tdt);
  2567. /* we need this if more than one processor can write to our tail
  2568. * at a time, it syncronizes IO on IA64/Altix systems */
  2569. mmiowb();
  2570. }
  2571. /**
  2572. * 82547 workaround to avoid controller hang in half-duplex environment.
  2573. * The workaround is to avoid queuing a large packet that would span
  2574. * the internal Tx FIFO ring boundary by notifying the stack to resend
  2575. * the packet at a later time. This gives the Tx FIFO an opportunity to
  2576. * flush all packets. When that occurs, we reset the Tx FIFO pointers
  2577. * to the beginning of the Tx FIFO.
  2578. **/
  2579. #define E1000_FIFO_HDR 0x10
  2580. #define E1000_82547_PAD_LEN 0x3E0
  2581. static int e1000_82547_fifo_workaround(struct e1000_adapter *adapter,
  2582. struct sk_buff *skb)
  2583. {
  2584. u32 fifo_space = adapter->tx_fifo_size - adapter->tx_fifo_head;
  2585. u32 skb_fifo_len = skb->len + E1000_FIFO_HDR;
  2586. skb_fifo_len = ALIGN(skb_fifo_len, E1000_FIFO_HDR);
  2587. if (adapter->link_duplex != HALF_DUPLEX)
  2588. goto no_fifo_stall_required;
  2589. if (atomic_read(&adapter->tx_fifo_stall))
  2590. return 1;
  2591. if (skb_fifo_len >= (E1000_82547_PAD_LEN + fifo_space)) {
  2592. atomic_set(&adapter->tx_fifo_stall, 1);
  2593. return 1;
  2594. }
  2595. no_fifo_stall_required:
  2596. adapter->tx_fifo_head += skb_fifo_len;
  2597. if (adapter->tx_fifo_head >= adapter->tx_fifo_size)
  2598. adapter->tx_fifo_head -= adapter->tx_fifo_size;
  2599. return 0;
  2600. }
  2601. static int __e1000_maybe_stop_tx(struct net_device *netdev, int size)
  2602. {
  2603. struct e1000_adapter *adapter = netdev_priv(netdev);
  2604. struct e1000_tx_ring *tx_ring = adapter->tx_ring;
  2605. netif_stop_queue(netdev);
  2606. /* Herbert's original patch had:
  2607. * smp_mb__after_netif_stop_queue();
  2608. * but since that doesn't exist yet, just open code it. */
  2609. smp_mb();
  2610. /* We need to check again in a case another CPU has just
  2611. * made room available. */
  2612. if (likely(E1000_DESC_UNUSED(tx_ring) < size))
  2613. return -EBUSY;
  2614. /* A reprieve! */
  2615. netif_start_queue(netdev);
  2616. ++adapter->restart_queue;
  2617. return 0;
  2618. }
  2619. static int e1000_maybe_stop_tx(struct net_device *netdev,
  2620. struct e1000_tx_ring *tx_ring, int size)
  2621. {
  2622. if (likely(E1000_DESC_UNUSED(tx_ring) >= size))
  2623. return 0;
  2624. return __e1000_maybe_stop_tx(netdev, size);
  2625. }
  2626. #define TXD_USE_COUNT(S, X) (((S) >> (X)) + 1 )
  2627. static netdev_tx_t e1000_xmit_frame(struct sk_buff *skb,
  2628. struct net_device *netdev)
  2629. {
  2630. struct e1000_adapter *adapter = netdev_priv(netdev);
  2631. struct e1000_hw *hw = &adapter->hw;
  2632. struct e1000_tx_ring *tx_ring;
  2633. unsigned int first, max_per_txd = E1000_MAX_DATA_PER_TXD;
  2634. unsigned int max_txd_pwr = E1000_MAX_TXD_PWR;
  2635. unsigned int tx_flags = 0;
  2636. unsigned int len = skb_headlen(skb);
  2637. unsigned int nr_frags;
  2638. unsigned int mss;
  2639. int count = 0;
  2640. int tso;
  2641. unsigned int f;
  2642. /* This goes back to the question of how to logically map a tx queue
  2643. * to a flow. Right now, performance is impacted slightly negatively
  2644. * if using multiple tx queues. If the stack breaks away from a
  2645. * single qdisc implementation, we can look at this again. */
  2646. tx_ring = adapter->tx_ring;
  2647. if (unlikely(skb->len <= 0)) {
  2648. dev_kfree_skb_any(skb);
  2649. return NETDEV_TX_OK;
  2650. }
  2651. mss = skb_shinfo(skb)->gso_size;
  2652. /* The controller does a simple calculation to
  2653. * make sure there is enough room in the FIFO before
  2654. * initiating the DMA for each buffer. The calc is:
  2655. * 4 = ceil(buffer len/mss). To make sure we don't
  2656. * overrun the FIFO, adjust the max buffer len if mss
  2657. * drops. */
  2658. if (mss) {
  2659. u8 hdr_len;
  2660. max_per_txd = min(mss << 2, max_per_txd);
  2661. max_txd_pwr = fls(max_per_txd) - 1;
  2662. hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
  2663. if (skb->data_len && hdr_len == len) {
  2664. switch (hw->mac_type) {
  2665. unsigned int pull_size;
  2666. case e1000_82544:
  2667. /* Make sure we have room to chop off 4 bytes,
  2668. * and that the end alignment will work out to
  2669. * this hardware's requirements
  2670. * NOTE: this is a TSO only workaround
  2671. * if end byte alignment not correct move us
  2672. * into the next dword */
  2673. if ((unsigned long)(skb_tail_pointer(skb) - 1) & 4)
  2674. break;
  2675. /* fall through */
  2676. pull_size = min((unsigned int)4, skb->data_len);
  2677. if (!__pskb_pull_tail(skb, pull_size)) {
  2678. e_err(drv, "__pskb_pull_tail "
  2679. "failed.\n");
  2680. dev_kfree_skb_any(skb);
  2681. return NETDEV_TX_OK;
  2682. }
  2683. len = skb_headlen(skb);
  2684. break;
  2685. default:
  2686. /* do nothing */
  2687. break;
  2688. }
  2689. }
  2690. }
  2691. /* reserve a descriptor for the offload context */
  2692. if ((mss) || (skb->ip_summed == CHECKSUM_PARTIAL))
  2693. count++;
  2694. count++;
  2695. /* Controller Erratum workaround */
  2696. if (!skb->data_len && tx_ring->last_tx_tso && !skb_is_gso(skb))
  2697. count++;
  2698. count += TXD_USE_COUNT(len, max_txd_pwr);
  2699. if (adapter->pcix_82544)
  2700. count++;
  2701. /* work-around for errata 10 and it applies to all controllers
  2702. * in PCI-X mode, so add one more descriptor to the count
  2703. */
  2704. if (unlikely((hw->bus_type == e1000_bus_type_pcix) &&
  2705. (len > 2015)))
  2706. count++;
  2707. nr_frags = skb_shinfo(skb)->nr_frags;
  2708. for (f = 0; f < nr_frags; f++)
  2709. count += TXD_USE_COUNT(skb_shinfo(skb)->frags[f].size,
  2710. max_txd_pwr);
  2711. if (adapter->pcix_82544)
  2712. count += nr_frags;
  2713. /* need: count + 2 desc gap to keep tail from touching
  2714. * head, otherwise try next time */
  2715. if (unlikely(e1000_maybe_stop_tx(netdev, tx_ring, count + 2)))
  2716. return NETDEV_TX_BUSY;
  2717. if (unlikely(hw->mac_type == e1000_82547)) {
  2718. if (unlikely(e1000_82547_fifo_workaround(adapter, skb))) {
  2719. netif_stop_queue(netdev);
  2720. if (!test_bit(__E1000_DOWN, &adapter->flags))
  2721. mod_timer(&adapter->tx_fifo_stall_timer,
  2722. jiffies + 1);
  2723. return NETDEV_TX_BUSY;
  2724. }
  2725. }
  2726. if (vlan_tx_tag_present(skb)) {
  2727. tx_flags |= E1000_TX_FLAGS_VLAN;
  2728. tx_flags |= (vlan_tx_tag_get(skb) << E1000_TX_FLAGS_VLAN_SHIFT);
  2729. }
  2730. first = tx_ring->next_to_use;
  2731. tso = e1000_tso(adapter, tx_ring, skb);
  2732. if (tso < 0) {
  2733. dev_kfree_skb_any(skb);
  2734. return NETDEV_TX_OK;
  2735. }
  2736. if (likely(tso)) {
  2737. if (likely(hw->mac_type != e1000_82544))
  2738. tx_ring->last_tx_tso = 1;
  2739. tx_flags |= E1000_TX_FLAGS_TSO;
  2740. } else if (likely(e1000_tx_csum(adapter, tx_ring, skb)))
  2741. tx_flags |= E1000_TX_FLAGS_CSUM;
  2742. if (likely(skb->protocol == htons(ETH_P_IP)))
  2743. tx_flags |= E1000_TX_FLAGS_IPV4;
  2744. count = e1000_tx_map(adapter, tx_ring, skb, first, max_per_txd,
  2745. nr_frags, mss);
  2746. if (count) {
  2747. e1000_tx_queue(adapter, tx_ring, tx_flags, count);
  2748. /* Make sure there is space in the ring for the next send. */
  2749. e1000_maybe_stop_tx(netdev, tx_ring, MAX_SKB_FRAGS + 2);
  2750. } else {
  2751. dev_kfree_skb_any(skb);
  2752. tx_ring->buffer_info[first].time_stamp = 0;
  2753. tx_ring->next_to_use = first;
  2754. }
  2755. return NETDEV_TX_OK;
  2756. }
  2757. /**
  2758. * e1000_tx_timeout - Respond to a Tx Hang
  2759. * @netdev: network interface device structure
  2760. **/
  2761. static void e1000_tx_timeout(struct net_device *netdev)
  2762. {
  2763. struct e1000_adapter *adapter = netdev_priv(netdev);
  2764. /* Do the reset outside of interrupt context */
  2765. adapter->tx_timeout_count++;
  2766. schedule_work(&adapter->reset_task);
  2767. }
  2768. static void e1000_reset_task(struct work_struct *work)
  2769. {
  2770. struct e1000_adapter *adapter =
  2771. container_of(work, struct e1000_adapter, reset_task);
  2772. e1000_reinit_safe(adapter);
  2773. }
  2774. /**
  2775. * e1000_get_stats - Get System Network Statistics
  2776. * @netdev: network interface device structure
  2777. *
  2778. * Returns the address of the device statistics structure.
  2779. * The statistics are actually updated from the timer callback.
  2780. **/
  2781. static struct net_device_stats *e1000_get_stats(struct net_device *netdev)
  2782. {
  2783. /* only return the current stats */
  2784. return &netdev->stats;
  2785. }
  2786. /**
  2787. * e1000_change_mtu - Change the Maximum Transfer Unit
  2788. * @netdev: network interface device structure
  2789. * @new_mtu: new value for maximum frame size
  2790. *
  2791. * Returns 0 on success, negative on failure
  2792. **/
  2793. static int e1000_change_mtu(struct net_device *netdev, int new_mtu)
  2794. {
  2795. struct e1000_adapter *adapter = netdev_priv(netdev);
  2796. struct e1000_hw *hw = &adapter->hw;
  2797. int max_frame = new_mtu + ENET_HEADER_SIZE + ETHERNET_FCS_SIZE;
  2798. if ((max_frame < MINIMUM_ETHERNET_FRAME_SIZE) ||
  2799. (max_frame > MAX_JUMBO_FRAME_SIZE)) {
  2800. e_err(probe, "Invalid MTU setting\n");
  2801. return -EINVAL;
  2802. }
  2803. /* Adapter-specific max frame size limits. */
  2804. switch (hw->mac_type) {
  2805. case e1000_undefined ... e1000_82542_rev2_1:
  2806. if (max_frame > (ETH_FRAME_LEN + ETH_FCS_LEN)) {
  2807. e_err(probe, "Jumbo Frames not supported.\n");
  2808. return -EINVAL;
  2809. }
  2810. break;
  2811. default:
  2812. /* Capable of supporting up to MAX_JUMBO_FRAME_SIZE limit. */
  2813. break;
  2814. }
  2815. while (test_and_set_bit(__E1000_RESETTING, &adapter->flags))
  2816. msleep(1);
  2817. /* e1000_down has a dependency on max_frame_size */
  2818. hw->max_frame_size = max_frame;
  2819. if (netif_running(netdev))
  2820. e1000_down(adapter);
  2821. /* NOTE: netdev_alloc_skb reserves 16 bytes, and typically NET_IP_ALIGN
  2822. * means we reserve 2 more, this pushes us to allocate from the next
  2823. * larger slab size.
  2824. * i.e. RXBUFFER_2048 --> size-4096 slab
  2825. * however with the new *_jumbo_rx* routines, jumbo receives will use
  2826. * fragmented skbs */
  2827. if (max_frame <= E1000_RXBUFFER_2048)
  2828. adapter->rx_buffer_len = E1000_RXBUFFER_2048;
  2829. else
  2830. #if (PAGE_SIZE >= E1000_RXBUFFER_16384)
  2831. adapter->rx_buffer_len = E1000_RXBUFFER_16384;
  2832. #elif (PAGE_SIZE >= E1000_RXBUFFER_4096)
  2833. adapter->rx_buffer_len = PAGE_SIZE;
  2834. #endif
  2835. /* adjust allocation if LPE protects us, and we aren't using SBP */
  2836. if (!hw->tbi_compatibility_on &&
  2837. ((max_frame == (ETH_FRAME_LEN + ETH_FCS_LEN)) ||
  2838. (max_frame == MAXIMUM_ETHERNET_VLAN_SIZE)))
  2839. adapter->rx_buffer_len = MAXIMUM_ETHERNET_VLAN_SIZE;
  2840. pr_info("%s changing MTU from %d to %d\n",
  2841. netdev->name, netdev->mtu, new_mtu);
  2842. netdev->mtu = new_mtu;
  2843. if (netif_running(netdev))
  2844. e1000_up(adapter);
  2845. else
  2846. e1000_reset(adapter);
  2847. clear_bit(__E1000_RESETTING, &adapter->flags);
  2848. return 0;
  2849. }
  2850. /**
  2851. * e1000_update_stats - Update the board statistics counters
  2852. * @adapter: board private structure
  2853. **/
  2854. void e1000_update_stats(struct e1000_adapter *adapter)
  2855. {
  2856. struct net_device *netdev = adapter->netdev;
  2857. struct e1000_hw *hw = &adapter->hw;
  2858. struct pci_dev *pdev = adapter->pdev;
  2859. unsigned long flags;
  2860. u16 phy_tmp;
  2861. #define PHY_IDLE_ERROR_COUNT_MASK 0x00FF
  2862. /*
  2863. * Prevent stats update while adapter is being reset, or if the pci
  2864. * connection is down.
  2865. */
  2866. if (adapter->link_speed == 0)
  2867. return;
  2868. if (pci_channel_offline(pdev))
  2869. return;
  2870. spin_lock_irqsave(&adapter->stats_lock, flags);
  2871. /* these counters are modified from e1000_tbi_adjust_stats,
  2872. * called from the interrupt context, so they must only
  2873. * be written while holding adapter->stats_lock
  2874. */
  2875. adapter->stats.crcerrs += er32(CRCERRS);
  2876. adapter->stats.gprc += er32(GPRC);
  2877. adapter->stats.gorcl += er32(GORCL);
  2878. adapter->stats.gorch += er32(GORCH);
  2879. adapter->stats.bprc += er32(BPRC);
  2880. adapter->stats.mprc += er32(MPRC);
  2881. adapter->stats.roc += er32(ROC);
  2882. adapter->stats.prc64 += er32(PRC64);
  2883. adapter->stats.prc127 += er32(PRC127);
  2884. adapter->stats.prc255 += er32(PRC255);
  2885. adapter->stats.prc511 += er32(PRC511);
  2886. adapter->stats.prc1023 += er32(PRC1023);
  2887. adapter->stats.prc1522 += er32(PRC1522);
  2888. adapter->stats.symerrs += er32(SYMERRS);
  2889. adapter->stats.mpc += er32(MPC);
  2890. adapter->stats.scc += er32(SCC);
  2891. adapter->stats.ecol += er32(ECOL);
  2892. adapter->stats.mcc += er32(MCC);
  2893. adapter->stats.latecol += er32(LATECOL);
  2894. adapter->stats.dc += er32(DC);
  2895. adapter->stats.sec += er32(SEC);
  2896. adapter->stats.rlec += er32(RLEC);
  2897. adapter->stats.xonrxc += er32(XONRXC);
  2898. adapter->stats.xontxc += er32(XONTXC);
  2899. adapter->stats.xoffrxc += er32(XOFFRXC);
  2900. adapter->stats.xofftxc += er32(XOFFTXC);
  2901. adapter->stats.fcruc += er32(FCRUC);
  2902. adapter->stats.gptc += er32(GPTC);
  2903. adapter->stats.gotcl += er32(GOTCL);
  2904. adapter->stats.gotch += er32(GOTCH);
  2905. adapter->stats.rnbc += er32(RNBC);
  2906. adapter->stats.ruc += er32(RUC);
  2907. adapter->stats.rfc += er32(RFC);
  2908. adapter->stats.rjc += er32(RJC);
  2909. adapter->stats.torl += er32(TORL);
  2910. adapter->stats.torh += er32(TORH);
  2911. adapter->stats.totl += er32(TOTL);
  2912. adapter->stats.toth += er32(TOTH);
  2913. adapter->stats.tpr += er32(TPR);
  2914. adapter->stats.ptc64 += er32(PTC64);
  2915. adapter->stats.ptc127 += er32(PTC127);
  2916. adapter->stats.ptc255 += er32(PTC255);
  2917. adapter->stats.ptc511 += er32(PTC511);
  2918. adapter->stats.ptc1023 += er32(PTC1023);
  2919. adapter->stats.ptc1522 += er32(PTC1522);
  2920. adapter->stats.mptc += er32(MPTC);
  2921. adapter->stats.bptc += er32(BPTC);
  2922. /* used for adaptive IFS */
  2923. hw->tx_packet_delta = er32(TPT);
  2924. adapter->stats.tpt += hw->tx_packet_delta;
  2925. hw->collision_delta = er32(COLC);
  2926. adapter->stats.colc += hw->collision_delta;
  2927. if (hw->mac_type >= e1000_82543) {
  2928. adapter->stats.algnerrc += er32(ALGNERRC);
  2929. adapter->stats.rxerrc += er32(RXERRC);
  2930. adapter->stats.tncrs += er32(TNCRS);
  2931. adapter->stats.cexterr += er32(CEXTERR);
  2932. adapter->stats.tsctc += er32(TSCTC);
  2933. adapter->stats.tsctfc += er32(TSCTFC);
  2934. }
  2935. /* Fill out the OS statistics structure */
  2936. netdev->stats.multicast = adapter->stats.mprc;
  2937. netdev->stats.collisions = adapter->stats.colc;
  2938. /* Rx Errors */
  2939. /* RLEC on some newer hardware can be incorrect so build
  2940. * our own version based on RUC and ROC */
  2941. netdev->stats.rx_errors = adapter->stats.rxerrc +
  2942. adapter->stats.crcerrs + adapter->stats.algnerrc +
  2943. adapter->stats.ruc + adapter->stats.roc +
  2944. adapter->stats.cexterr;
  2945. adapter->stats.rlerrc = adapter->stats.ruc + adapter->stats.roc;
  2946. netdev->stats.rx_length_errors = adapter->stats.rlerrc;
  2947. netdev->stats.rx_crc_errors = adapter->stats.crcerrs;
  2948. netdev->stats.rx_frame_errors = adapter->stats.algnerrc;
  2949. netdev->stats.rx_missed_errors = adapter->stats.mpc;
  2950. /* Tx Errors */
  2951. adapter->stats.txerrc = adapter->stats.ecol + adapter->stats.latecol;
  2952. netdev->stats.tx_errors = adapter->stats.txerrc;
  2953. netdev->stats.tx_aborted_errors = adapter->stats.ecol;
  2954. netdev->stats.tx_window_errors = adapter->stats.latecol;
  2955. netdev->stats.tx_carrier_errors = adapter->stats.tncrs;
  2956. if (hw->bad_tx_carr_stats_fd &&
  2957. adapter->link_duplex == FULL_DUPLEX) {
  2958. netdev->stats.tx_carrier_errors = 0;
  2959. adapter->stats.tncrs = 0;
  2960. }
  2961. /* Tx Dropped needs to be maintained elsewhere */
  2962. /* Phy Stats */
  2963. if (hw->media_type == e1000_media_type_copper) {
  2964. if ((adapter->link_speed == SPEED_1000) &&
  2965. (!e1000_read_phy_reg(hw, PHY_1000T_STATUS, &phy_tmp))) {
  2966. phy_tmp &= PHY_IDLE_ERROR_COUNT_MASK;
  2967. adapter->phy_stats.idle_errors += phy_tmp;
  2968. }
  2969. if ((hw->mac_type <= e1000_82546) &&
  2970. (hw->phy_type == e1000_phy_m88) &&
  2971. !e1000_read_phy_reg(hw, M88E1000_RX_ERR_CNTR, &phy_tmp))
  2972. adapter->phy_stats.receive_errors += phy_tmp;
  2973. }
  2974. /* Management Stats */
  2975. if (hw->has_smbus) {
  2976. adapter->stats.mgptc += er32(MGTPTC);
  2977. adapter->stats.mgprc += er32(MGTPRC);
  2978. adapter->stats.mgpdc += er32(MGTPDC);
  2979. }
  2980. spin_unlock_irqrestore(&adapter->stats_lock, flags);
  2981. }
  2982. /**
  2983. * e1000_intr - Interrupt Handler
  2984. * @irq: interrupt number
  2985. * @data: pointer to a network interface device structure
  2986. **/
  2987. static irqreturn_t e1000_intr(int irq, void *data)
  2988. {
  2989. struct net_device *netdev = data;
  2990. struct e1000_adapter *adapter = netdev_priv(netdev);
  2991. struct e1000_hw *hw = &adapter->hw;
  2992. u32 icr = er32(ICR);
  2993. if (unlikely((!icr)))
  2994. return IRQ_NONE; /* Not our interrupt */
  2995. /*
  2996. * we might have caused the interrupt, but the above
  2997. * read cleared it, and just in case the driver is
  2998. * down there is nothing to do so return handled
  2999. */
  3000. if (unlikely(test_bit(__E1000_DOWN, &adapter->flags)))
  3001. return IRQ_HANDLED;
  3002. if (unlikely(icr & (E1000_ICR_RXSEQ | E1000_ICR_LSC))) {
  3003. hw->get_link_status = 1;
  3004. /* guard against interrupt when we're going down */
  3005. if (!test_bit(__E1000_DOWN, &adapter->flags))
  3006. mod_timer(&adapter->watchdog_timer, jiffies + 1);
  3007. }
  3008. /* disable interrupts, without the synchronize_irq bit */
  3009. ew32(IMC, ~0);
  3010. E1000_WRITE_FLUSH();
  3011. if (likely(napi_schedule_prep(&adapter->napi))) {
  3012. adapter->total_tx_bytes = 0;
  3013. adapter->total_tx_packets = 0;
  3014. adapter->total_rx_bytes = 0;
  3015. adapter->total_rx_packets = 0;
  3016. __napi_schedule(&adapter->napi);
  3017. } else {
  3018. /* this really should not happen! if it does it is basically a
  3019. * bug, but not a hard error, so enable ints and continue */
  3020. if (!test_bit(__E1000_DOWN, &adapter->flags))
  3021. e1000_irq_enable(adapter);
  3022. }
  3023. return IRQ_HANDLED;
  3024. }
  3025. /**
  3026. * e1000_clean - NAPI Rx polling callback
  3027. * @adapter: board private structure
  3028. **/
  3029. static int e1000_clean(struct napi_struct *napi, int budget)
  3030. {
  3031. struct e1000_adapter *adapter = container_of(napi, struct e1000_adapter, napi);
  3032. int tx_clean_complete = 0, work_done = 0;
  3033. tx_clean_complete = e1000_clean_tx_irq(adapter, &adapter->tx_ring[0]);
  3034. adapter->clean_rx(adapter, &adapter->rx_ring[0], &work_done, budget);
  3035. if (!tx_clean_complete)
  3036. work_done = budget;
  3037. /* If budget not fully consumed, exit the polling mode */
  3038. if (work_done < budget) {
  3039. if (likely(adapter->itr_setting & 3))
  3040. e1000_set_itr(adapter);
  3041. napi_complete(napi);
  3042. if (!test_bit(__E1000_DOWN, &adapter->flags))
  3043. e1000_irq_enable(adapter);
  3044. }
  3045. return work_done;
  3046. }
  3047. /**
  3048. * e1000_clean_tx_irq - Reclaim resources after transmit completes
  3049. * @adapter: board private structure
  3050. **/
  3051. static bool e1000_clean_tx_irq(struct e1000_adapter *adapter,
  3052. struct e1000_tx_ring *tx_ring)
  3053. {
  3054. struct e1000_hw *hw = &adapter->hw;
  3055. struct net_device *netdev = adapter->netdev;
  3056. struct e1000_tx_desc *tx_desc, *eop_desc;
  3057. struct e1000_buffer *buffer_info;
  3058. unsigned int i, eop;
  3059. unsigned int count = 0;
  3060. unsigned int total_tx_bytes=0, total_tx_packets=0;
  3061. i = tx_ring->next_to_clean;
  3062. eop = tx_ring->buffer_info[i].next_to_watch;
  3063. eop_desc = E1000_TX_DESC(*tx_ring, eop);
  3064. while ((eop_desc->upper.data & cpu_to_le32(E1000_TXD_STAT_DD)) &&
  3065. (count < tx_ring->count)) {
  3066. bool cleaned = false;
  3067. rmb(); /* read buffer_info after eop_desc */
  3068. for ( ; !cleaned; count++) {
  3069. tx_desc = E1000_TX_DESC(*tx_ring, i);
  3070. buffer_info = &tx_ring->buffer_info[i];
  3071. cleaned = (i == eop);
  3072. if (cleaned) {
  3073. total_tx_packets += buffer_info->segs;
  3074. total_tx_bytes += buffer_info->bytecount;
  3075. }
  3076. e1000_unmap_and_free_tx_resource(adapter, buffer_info);
  3077. tx_desc->upper.data = 0;
  3078. if (unlikely(++i == tx_ring->count)) i = 0;
  3079. }
  3080. eop = tx_ring->buffer_info[i].next_to_watch;
  3081. eop_desc = E1000_TX_DESC(*tx_ring, eop);
  3082. }
  3083. tx_ring->next_to_clean = i;
  3084. #define TX_WAKE_THRESHOLD 32
  3085. if (unlikely(count && netif_carrier_ok(netdev) &&
  3086. E1000_DESC_UNUSED(tx_ring) >= TX_WAKE_THRESHOLD)) {
  3087. /* Make sure that anybody stopping the queue after this
  3088. * sees the new next_to_clean.
  3089. */
  3090. smp_mb();
  3091. if (netif_queue_stopped(netdev) &&
  3092. !(test_bit(__E1000_DOWN, &adapter->flags))) {
  3093. netif_wake_queue(netdev);
  3094. ++adapter->restart_queue;
  3095. }
  3096. }
  3097. if (adapter->detect_tx_hung) {
  3098. /* Detect a transmit hang in hardware, this serializes the
  3099. * check with the clearing of time_stamp and movement of i */
  3100. adapter->detect_tx_hung = false;
  3101. if (tx_ring->buffer_info[eop].time_stamp &&
  3102. time_after(jiffies, tx_ring->buffer_info[eop].time_stamp +
  3103. (adapter->tx_timeout_factor * HZ)) &&
  3104. !(er32(STATUS) & E1000_STATUS_TXOFF)) {
  3105. /* detected Tx unit hang */
  3106. e_err(drv, "Detected Tx Unit Hang\n"
  3107. " Tx Queue <%lu>\n"
  3108. " TDH <%x>\n"
  3109. " TDT <%x>\n"
  3110. " next_to_use <%x>\n"
  3111. " next_to_clean <%x>\n"
  3112. "buffer_info[next_to_clean]\n"
  3113. " time_stamp <%lx>\n"
  3114. " next_to_watch <%x>\n"
  3115. " jiffies <%lx>\n"
  3116. " next_to_watch.status <%x>\n",
  3117. (unsigned long)((tx_ring - adapter->tx_ring) /
  3118. sizeof(struct e1000_tx_ring)),
  3119. readl(hw->hw_addr + tx_ring->tdh),
  3120. readl(hw->hw_addr + tx_ring->tdt),
  3121. tx_ring->next_to_use,
  3122. tx_ring->next_to_clean,
  3123. tx_ring->buffer_info[eop].time_stamp,
  3124. eop,
  3125. jiffies,
  3126. eop_desc->upper.fields.status);
  3127. netif_stop_queue(netdev);
  3128. }
  3129. }
  3130. adapter->total_tx_bytes += total_tx_bytes;
  3131. adapter->total_tx_packets += total_tx_packets;
  3132. netdev->stats.tx_bytes += total_tx_bytes;
  3133. netdev->stats.tx_packets += total_tx_packets;
  3134. return count < tx_ring->count;
  3135. }
  3136. /**
  3137. * e1000_rx_checksum - Receive Checksum Offload for 82543
  3138. * @adapter: board private structure
  3139. * @status_err: receive descriptor status and error fields
  3140. * @csum: receive descriptor csum field
  3141. * @sk_buff: socket buffer with received data
  3142. **/
  3143. static void e1000_rx_checksum(struct e1000_adapter *adapter, u32 status_err,
  3144. u32 csum, struct sk_buff *skb)
  3145. {
  3146. struct e1000_hw *hw = &adapter->hw;
  3147. u16 status = (u16)status_err;
  3148. u8 errors = (u8)(status_err >> 24);
  3149. skb_checksum_none_assert(skb);
  3150. /* 82543 or newer only */
  3151. if (unlikely(hw->mac_type < e1000_82543)) return;
  3152. /* Ignore Checksum bit is set */
  3153. if (unlikely(status & E1000_RXD_STAT_IXSM)) return;
  3154. /* TCP/UDP checksum error bit is set */
  3155. if (unlikely(errors & E1000_RXD_ERR_TCPE)) {
  3156. /* let the stack verify checksum errors */
  3157. adapter->hw_csum_err++;
  3158. return;
  3159. }
  3160. /* TCP/UDP Checksum has not been calculated */
  3161. if (!(status & E1000_RXD_STAT_TCPCS))
  3162. return;
  3163. /* It must be a TCP or UDP packet with a valid checksum */
  3164. if (likely(status & E1000_RXD_STAT_TCPCS)) {
  3165. /* TCP checksum is good */
  3166. skb->ip_summed = CHECKSUM_UNNECESSARY;
  3167. }
  3168. adapter->hw_csum_good++;
  3169. }
  3170. /**
  3171. * e1000_consume_page - helper function
  3172. **/
  3173. static void e1000_consume_page(struct e1000_buffer *bi, struct sk_buff *skb,
  3174. u16 length)
  3175. {
  3176. bi->page = NULL;
  3177. skb->len += length;
  3178. skb->data_len += length;
  3179. skb->truesize += length;
  3180. }
  3181. /**
  3182. * e1000_receive_skb - helper function to handle rx indications
  3183. * @adapter: board private structure
  3184. * @status: descriptor status field as written by hardware
  3185. * @vlan: descriptor vlan field as written by hardware (no le/be conversion)
  3186. * @skb: pointer to sk_buff to be indicated to stack
  3187. */
  3188. static void e1000_receive_skb(struct e1000_adapter *adapter, u8 status,
  3189. __le16 vlan, struct sk_buff *skb)
  3190. {
  3191. skb->protocol = eth_type_trans(skb, adapter->netdev);
  3192. if (status & E1000_RXD_STAT_VP) {
  3193. u16 vid = le16_to_cpu(vlan) & E1000_RXD_SPC_VLAN_MASK;
  3194. __vlan_hwaccel_put_tag(skb, vid);
  3195. }
  3196. napi_gro_receive(&adapter->napi, skb);
  3197. }
  3198. /**
  3199. * e1000_clean_jumbo_rx_irq - Send received data up the network stack; legacy
  3200. * @adapter: board private structure
  3201. * @rx_ring: ring to clean
  3202. * @work_done: amount of napi work completed this call
  3203. * @work_to_do: max amount of work allowed for this call to do
  3204. *
  3205. * the return value indicates whether actual cleaning was done, there
  3206. * is no guarantee that everything was cleaned
  3207. */
  3208. static bool e1000_clean_jumbo_rx_irq(struct e1000_adapter *adapter,
  3209. struct e1000_rx_ring *rx_ring,
  3210. int *work_done, int work_to_do)
  3211. {
  3212. struct e1000_hw *hw = &adapter->hw;
  3213. struct net_device *netdev = adapter->netdev;
  3214. struct pci_dev *pdev = adapter->pdev;
  3215. struct e1000_rx_desc *rx_desc, *next_rxd;
  3216. struct e1000_buffer *buffer_info, *next_buffer;
  3217. unsigned long irq_flags;
  3218. u32 length;
  3219. unsigned int i;
  3220. int cleaned_count = 0;
  3221. bool cleaned = false;
  3222. unsigned int total_rx_bytes=0, total_rx_packets=0;
  3223. i = rx_ring->next_to_clean;
  3224. rx_desc = E1000_RX_DESC(*rx_ring, i);
  3225. buffer_info = &rx_ring->buffer_info[i];
  3226. while (rx_desc->status & E1000_RXD_STAT_DD) {
  3227. struct sk_buff *skb;
  3228. u8 status;
  3229. if (*work_done >= work_to_do)
  3230. break;
  3231. (*work_done)++;
  3232. rmb(); /* read descriptor and rx_buffer_info after status DD */
  3233. status = rx_desc->status;
  3234. skb = buffer_info->skb;
  3235. buffer_info->skb = NULL;
  3236. if (++i == rx_ring->count) i = 0;
  3237. next_rxd = E1000_RX_DESC(*rx_ring, i);
  3238. prefetch(next_rxd);
  3239. next_buffer = &rx_ring->buffer_info[i];
  3240. cleaned = true;
  3241. cleaned_count++;
  3242. dma_unmap_page(&pdev->dev, buffer_info->dma,
  3243. buffer_info->length, DMA_FROM_DEVICE);
  3244. buffer_info->dma = 0;
  3245. length = le16_to_cpu(rx_desc->length);
  3246. /* errors is only valid for DD + EOP descriptors */
  3247. if (unlikely((status & E1000_RXD_STAT_EOP) &&
  3248. (rx_desc->errors & E1000_RXD_ERR_FRAME_ERR_MASK))) {
  3249. u8 last_byte = *(skb->data + length - 1);
  3250. if (TBI_ACCEPT(hw, status, rx_desc->errors, length,
  3251. last_byte)) {
  3252. spin_lock_irqsave(&adapter->stats_lock,
  3253. irq_flags);
  3254. e1000_tbi_adjust_stats(hw, &adapter->stats,
  3255. length, skb->data);
  3256. spin_unlock_irqrestore(&adapter->stats_lock,
  3257. irq_flags);
  3258. length--;
  3259. } else {
  3260. /* recycle both page and skb */
  3261. buffer_info->skb = skb;
  3262. /* an error means any chain goes out the window
  3263. * too */
  3264. if (rx_ring->rx_skb_top)
  3265. dev_kfree_skb(rx_ring->rx_skb_top);
  3266. rx_ring->rx_skb_top = NULL;
  3267. goto next_desc;
  3268. }
  3269. }
  3270. #define rxtop rx_ring->rx_skb_top
  3271. if (!(status & E1000_RXD_STAT_EOP)) {
  3272. /* this descriptor is only the beginning (or middle) */
  3273. if (!rxtop) {
  3274. /* this is the beginning of a chain */
  3275. rxtop = skb;
  3276. skb_fill_page_desc(rxtop, 0, buffer_info->page,
  3277. 0, length);
  3278. } else {
  3279. /* this is the middle of a chain */
  3280. skb_fill_page_desc(rxtop,
  3281. skb_shinfo(rxtop)->nr_frags,
  3282. buffer_info->page, 0, length);
  3283. /* re-use the skb, only consumed the page */
  3284. buffer_info->skb = skb;
  3285. }
  3286. e1000_consume_page(buffer_info, rxtop, length);
  3287. goto next_desc;
  3288. } else {
  3289. if (rxtop) {
  3290. /* end of the chain */
  3291. skb_fill_page_desc(rxtop,
  3292. skb_shinfo(rxtop)->nr_frags,
  3293. buffer_info->page, 0, length);
  3294. /* re-use the current skb, we only consumed the
  3295. * page */
  3296. buffer_info->skb = skb;
  3297. skb = rxtop;
  3298. rxtop = NULL;
  3299. e1000_consume_page(buffer_info, skb, length);
  3300. } else {
  3301. /* no chain, got EOP, this buf is the packet
  3302. * copybreak to save the put_page/alloc_page */
  3303. if (length <= copybreak &&
  3304. skb_tailroom(skb) >= length) {
  3305. u8 *vaddr;
  3306. vaddr = kmap_atomic(buffer_info->page,
  3307. KM_SKB_DATA_SOFTIRQ);
  3308. memcpy(skb_tail_pointer(skb), vaddr, length);
  3309. kunmap_atomic(vaddr,
  3310. KM_SKB_DATA_SOFTIRQ);
  3311. /* re-use the page, so don't erase
  3312. * buffer_info->page */
  3313. skb_put(skb, length);
  3314. } else {
  3315. skb_fill_page_desc(skb, 0,
  3316. buffer_info->page, 0,
  3317. length);
  3318. e1000_consume_page(buffer_info, skb,
  3319. length);
  3320. }
  3321. }
  3322. }
  3323. /* Receive Checksum Offload XXX recompute due to CRC strip? */
  3324. e1000_rx_checksum(adapter,
  3325. (u32)(status) |
  3326. ((u32)(rx_desc->errors) << 24),
  3327. le16_to_cpu(rx_desc->csum), skb);
  3328. pskb_trim(skb, skb->len - 4);
  3329. /* probably a little skewed due to removing CRC */
  3330. total_rx_bytes += skb->len;
  3331. total_rx_packets++;
  3332. /* eth type trans needs skb->data to point to something */
  3333. if (!pskb_may_pull(skb, ETH_HLEN)) {
  3334. e_err(drv, "pskb_may_pull failed.\n");
  3335. dev_kfree_skb(skb);
  3336. goto next_desc;
  3337. }
  3338. e1000_receive_skb(adapter, status, rx_desc->special, skb);
  3339. next_desc:
  3340. rx_desc->status = 0;
  3341. /* return some buffers to hardware, one at a time is too slow */
  3342. if (unlikely(cleaned_count >= E1000_RX_BUFFER_WRITE)) {
  3343. adapter->alloc_rx_buf(adapter, rx_ring, cleaned_count);
  3344. cleaned_count = 0;
  3345. }
  3346. /* use prefetched values */
  3347. rx_desc = next_rxd;
  3348. buffer_info = next_buffer;
  3349. }
  3350. rx_ring->next_to_clean = i;
  3351. cleaned_count = E1000_DESC_UNUSED(rx_ring);
  3352. if (cleaned_count)
  3353. adapter->alloc_rx_buf(adapter, rx_ring, cleaned_count);
  3354. adapter->total_rx_packets += total_rx_packets;
  3355. adapter->total_rx_bytes += total_rx_bytes;
  3356. netdev->stats.rx_bytes += total_rx_bytes;
  3357. netdev->stats.rx_packets += total_rx_packets;
  3358. return cleaned;
  3359. }
  3360. /*
  3361. * this should improve performance for small packets with large amounts
  3362. * of reassembly being done in the stack
  3363. */
  3364. static void e1000_check_copybreak(struct net_device *netdev,
  3365. struct e1000_buffer *buffer_info,
  3366. u32 length, struct sk_buff **skb)
  3367. {
  3368. struct sk_buff *new_skb;
  3369. if (length > copybreak)
  3370. return;
  3371. new_skb = netdev_alloc_skb_ip_align(netdev, length);
  3372. if (!new_skb)
  3373. return;
  3374. skb_copy_to_linear_data_offset(new_skb, -NET_IP_ALIGN,
  3375. (*skb)->data - NET_IP_ALIGN,
  3376. length + NET_IP_ALIGN);
  3377. /* save the skb in buffer_info as good */
  3378. buffer_info->skb = *skb;
  3379. *skb = new_skb;
  3380. }
  3381. /**
  3382. * e1000_clean_rx_irq - Send received data up the network stack; legacy
  3383. * @adapter: board private structure
  3384. * @rx_ring: ring to clean
  3385. * @work_done: amount of napi work completed this call
  3386. * @work_to_do: max amount of work allowed for this call to do
  3387. */
  3388. static bool e1000_clean_rx_irq(struct e1000_adapter *adapter,
  3389. struct e1000_rx_ring *rx_ring,
  3390. int *work_done, int work_to_do)
  3391. {
  3392. struct e1000_hw *hw = &adapter->hw;
  3393. struct net_device *netdev = adapter->netdev;
  3394. struct pci_dev *pdev = adapter->pdev;
  3395. struct e1000_rx_desc *rx_desc, *next_rxd;
  3396. struct e1000_buffer *buffer_info, *next_buffer;
  3397. unsigned long flags;
  3398. u32 length;
  3399. unsigned int i;
  3400. int cleaned_count = 0;
  3401. bool cleaned = false;
  3402. unsigned int total_rx_bytes=0, total_rx_packets=0;
  3403. i = rx_ring->next_to_clean;
  3404. rx_desc = E1000_RX_DESC(*rx_ring, i);
  3405. buffer_info = &rx_ring->buffer_info[i];
  3406. while (rx_desc->status & E1000_RXD_STAT_DD) {
  3407. struct sk_buff *skb;
  3408. u8 status;
  3409. if (*work_done >= work_to_do)
  3410. break;
  3411. (*work_done)++;
  3412. rmb(); /* read descriptor and rx_buffer_info after status DD */
  3413. status = rx_desc->status;
  3414. skb = buffer_info->skb;
  3415. buffer_info->skb = NULL;
  3416. prefetch(skb->data - NET_IP_ALIGN);
  3417. if (++i == rx_ring->count) i = 0;
  3418. next_rxd = E1000_RX_DESC(*rx_ring, i);
  3419. prefetch(next_rxd);
  3420. next_buffer = &rx_ring->buffer_info[i];
  3421. cleaned = true;
  3422. cleaned_count++;
  3423. dma_unmap_single(&pdev->dev, buffer_info->dma,
  3424. buffer_info->length, DMA_FROM_DEVICE);
  3425. buffer_info->dma = 0;
  3426. length = le16_to_cpu(rx_desc->length);
  3427. /* !EOP means multiple descriptors were used to store a single
  3428. * packet, if thats the case we need to toss it. In fact, we
  3429. * to toss every packet with the EOP bit clear and the next
  3430. * frame that _does_ have the EOP bit set, as it is by
  3431. * definition only a frame fragment
  3432. */
  3433. if (unlikely(!(status & E1000_RXD_STAT_EOP)))
  3434. adapter->discarding = true;
  3435. if (adapter->discarding) {
  3436. /* All receives must fit into a single buffer */
  3437. e_dbg("Receive packet consumed multiple buffers\n");
  3438. /* recycle */
  3439. buffer_info->skb = skb;
  3440. if (status & E1000_RXD_STAT_EOP)
  3441. adapter->discarding = false;
  3442. goto next_desc;
  3443. }
  3444. if (unlikely(rx_desc->errors & E1000_RXD_ERR_FRAME_ERR_MASK)) {
  3445. u8 last_byte = *(skb->data + length - 1);
  3446. if (TBI_ACCEPT(hw, status, rx_desc->errors, length,
  3447. last_byte)) {
  3448. spin_lock_irqsave(&adapter->stats_lock, flags);
  3449. e1000_tbi_adjust_stats(hw, &adapter->stats,
  3450. length, skb->data);
  3451. spin_unlock_irqrestore(&adapter->stats_lock,
  3452. flags);
  3453. length--;
  3454. } else {
  3455. /* recycle */
  3456. buffer_info->skb = skb;
  3457. goto next_desc;
  3458. }
  3459. }
  3460. /* adjust length to remove Ethernet CRC, this must be
  3461. * done after the TBI_ACCEPT workaround above */
  3462. length -= 4;
  3463. /* probably a little skewed due to removing CRC */
  3464. total_rx_bytes += length;
  3465. total_rx_packets++;
  3466. e1000_check_copybreak(netdev, buffer_info, length, &skb);
  3467. skb_put(skb, length);
  3468. /* Receive Checksum Offload */
  3469. e1000_rx_checksum(adapter,
  3470. (u32)(status) |
  3471. ((u32)(rx_desc->errors) << 24),
  3472. le16_to_cpu(rx_desc->csum), skb);
  3473. e1000_receive_skb(adapter, status, rx_desc->special, skb);
  3474. next_desc:
  3475. rx_desc->status = 0;
  3476. /* return some buffers to hardware, one at a time is too slow */
  3477. if (unlikely(cleaned_count >= E1000_RX_BUFFER_WRITE)) {
  3478. adapter->alloc_rx_buf(adapter, rx_ring, cleaned_count);
  3479. cleaned_count = 0;
  3480. }
  3481. /* use prefetched values */
  3482. rx_desc = next_rxd;
  3483. buffer_info = next_buffer;
  3484. }
  3485. rx_ring->next_to_clean = i;
  3486. cleaned_count = E1000_DESC_UNUSED(rx_ring);
  3487. if (cleaned_count)
  3488. adapter->alloc_rx_buf(adapter, rx_ring, cleaned_count);
  3489. adapter->total_rx_packets += total_rx_packets;
  3490. adapter->total_rx_bytes += total_rx_bytes;
  3491. netdev->stats.rx_bytes += total_rx_bytes;
  3492. netdev->stats.rx_packets += total_rx_packets;
  3493. return cleaned;
  3494. }
  3495. /**
  3496. * e1000_alloc_jumbo_rx_buffers - Replace used jumbo receive buffers
  3497. * @adapter: address of board private structure
  3498. * @rx_ring: pointer to receive ring structure
  3499. * @cleaned_count: number of buffers to allocate this pass
  3500. **/
  3501. static void
  3502. e1000_alloc_jumbo_rx_buffers(struct e1000_adapter *adapter,
  3503. struct e1000_rx_ring *rx_ring, int cleaned_count)
  3504. {
  3505. struct net_device *netdev = adapter->netdev;
  3506. struct pci_dev *pdev = adapter->pdev;
  3507. struct e1000_rx_desc *rx_desc;
  3508. struct e1000_buffer *buffer_info;
  3509. struct sk_buff *skb;
  3510. unsigned int i;
  3511. unsigned int bufsz = 256 - 16 /*for skb_reserve */ ;
  3512. i = rx_ring->next_to_use;
  3513. buffer_info = &rx_ring->buffer_info[i];
  3514. while (cleaned_count--) {
  3515. skb = buffer_info->skb;
  3516. if (skb) {
  3517. skb_trim(skb, 0);
  3518. goto check_page;
  3519. }
  3520. skb = netdev_alloc_skb_ip_align(netdev, bufsz);
  3521. if (unlikely(!skb)) {
  3522. /* Better luck next round */
  3523. adapter->alloc_rx_buff_failed++;
  3524. break;
  3525. }
  3526. /* Fix for errata 23, can't cross 64kB boundary */
  3527. if (!e1000_check_64k_bound(adapter, skb->data, bufsz)) {
  3528. struct sk_buff *oldskb = skb;
  3529. e_err(rx_err, "skb align check failed: %u bytes at "
  3530. "%p\n", bufsz, skb->data);
  3531. /* Try again, without freeing the previous */
  3532. skb = netdev_alloc_skb_ip_align(netdev, bufsz);
  3533. /* Failed allocation, critical failure */
  3534. if (!skb) {
  3535. dev_kfree_skb(oldskb);
  3536. adapter->alloc_rx_buff_failed++;
  3537. break;
  3538. }
  3539. if (!e1000_check_64k_bound(adapter, skb->data, bufsz)) {
  3540. /* give up */
  3541. dev_kfree_skb(skb);
  3542. dev_kfree_skb(oldskb);
  3543. break; /* while (cleaned_count--) */
  3544. }
  3545. /* Use new allocation */
  3546. dev_kfree_skb(oldskb);
  3547. }
  3548. buffer_info->skb = skb;
  3549. buffer_info->length = adapter->rx_buffer_len;
  3550. check_page:
  3551. /* allocate a new page if necessary */
  3552. if (!buffer_info->page) {
  3553. buffer_info->page = alloc_page(GFP_ATOMIC);
  3554. if (unlikely(!buffer_info->page)) {
  3555. adapter->alloc_rx_buff_failed++;
  3556. break;
  3557. }
  3558. }
  3559. if (!buffer_info->dma) {
  3560. buffer_info->dma = dma_map_page(&pdev->dev,
  3561. buffer_info->page, 0,
  3562. buffer_info->length,
  3563. DMA_FROM_DEVICE);
  3564. if (dma_mapping_error(&pdev->dev, buffer_info->dma)) {
  3565. put_page(buffer_info->page);
  3566. dev_kfree_skb(skb);
  3567. buffer_info->page = NULL;
  3568. buffer_info->skb = NULL;
  3569. buffer_info->dma = 0;
  3570. adapter->alloc_rx_buff_failed++;
  3571. break; /* while !buffer_info->skb */
  3572. }
  3573. }
  3574. rx_desc = E1000_RX_DESC(*rx_ring, i);
  3575. rx_desc->buffer_addr = cpu_to_le64(buffer_info->dma);
  3576. if (unlikely(++i == rx_ring->count))
  3577. i = 0;
  3578. buffer_info = &rx_ring->buffer_info[i];
  3579. }
  3580. if (likely(rx_ring->next_to_use != i)) {
  3581. rx_ring->next_to_use = i;
  3582. if (unlikely(i-- == 0))
  3583. i = (rx_ring->count - 1);
  3584. /* Force memory writes to complete before letting h/w
  3585. * know there are new descriptors to fetch. (Only
  3586. * applicable for weak-ordered memory model archs,
  3587. * such as IA-64). */
  3588. wmb();
  3589. writel(i, adapter->hw.hw_addr + rx_ring->rdt);
  3590. }
  3591. }
  3592. /**
  3593. * e1000_alloc_rx_buffers - Replace used receive buffers; legacy & extended
  3594. * @adapter: address of board private structure
  3595. **/
  3596. static void e1000_alloc_rx_buffers(struct e1000_adapter *adapter,
  3597. struct e1000_rx_ring *rx_ring,
  3598. int cleaned_count)
  3599. {
  3600. struct e1000_hw *hw = &adapter->hw;
  3601. struct net_device *netdev = adapter->netdev;
  3602. struct pci_dev *pdev = adapter->pdev;
  3603. struct e1000_rx_desc *rx_desc;
  3604. struct e1000_buffer *buffer_info;
  3605. struct sk_buff *skb;
  3606. unsigned int i;
  3607. unsigned int bufsz = adapter->rx_buffer_len;
  3608. i = rx_ring->next_to_use;
  3609. buffer_info = &rx_ring->buffer_info[i];
  3610. while (cleaned_count--) {
  3611. skb = buffer_info->skb;
  3612. if (skb) {
  3613. skb_trim(skb, 0);
  3614. goto map_skb;
  3615. }
  3616. skb = netdev_alloc_skb_ip_align(netdev, bufsz);
  3617. if (unlikely(!skb)) {
  3618. /* Better luck next round */
  3619. adapter->alloc_rx_buff_failed++;
  3620. break;
  3621. }
  3622. /* Fix for errata 23, can't cross 64kB boundary */
  3623. if (!e1000_check_64k_bound(adapter, skb->data, bufsz)) {
  3624. struct sk_buff *oldskb = skb;
  3625. e_err(rx_err, "skb align check failed: %u bytes at "
  3626. "%p\n", bufsz, skb->data);
  3627. /* Try again, without freeing the previous */
  3628. skb = netdev_alloc_skb_ip_align(netdev, bufsz);
  3629. /* Failed allocation, critical failure */
  3630. if (!skb) {
  3631. dev_kfree_skb(oldskb);
  3632. adapter->alloc_rx_buff_failed++;
  3633. break;
  3634. }
  3635. if (!e1000_check_64k_bound(adapter, skb->data, bufsz)) {
  3636. /* give up */
  3637. dev_kfree_skb(skb);
  3638. dev_kfree_skb(oldskb);
  3639. adapter->alloc_rx_buff_failed++;
  3640. break; /* while !buffer_info->skb */
  3641. }
  3642. /* Use new allocation */
  3643. dev_kfree_skb(oldskb);
  3644. }
  3645. buffer_info->skb = skb;
  3646. buffer_info->length = adapter->rx_buffer_len;
  3647. map_skb:
  3648. buffer_info->dma = dma_map_single(&pdev->dev,
  3649. skb->data,
  3650. buffer_info->length,
  3651. DMA_FROM_DEVICE);
  3652. if (dma_mapping_error(&pdev->dev, buffer_info->dma)) {
  3653. dev_kfree_skb(skb);
  3654. buffer_info->skb = NULL;
  3655. buffer_info->dma = 0;
  3656. adapter->alloc_rx_buff_failed++;
  3657. break; /* while !buffer_info->skb */
  3658. }
  3659. /*
  3660. * XXX if it was allocated cleanly it will never map to a
  3661. * boundary crossing
  3662. */
  3663. /* Fix for errata 23, can't cross 64kB boundary */
  3664. if (!e1000_check_64k_bound(adapter,
  3665. (void *)(unsigned long)buffer_info->dma,
  3666. adapter->rx_buffer_len)) {
  3667. e_err(rx_err, "dma align check failed: %u bytes at "
  3668. "%p\n", adapter->rx_buffer_len,
  3669. (void *)(unsigned long)buffer_info->dma);
  3670. dev_kfree_skb(skb);
  3671. buffer_info->skb = NULL;
  3672. dma_unmap_single(&pdev->dev, buffer_info->dma,
  3673. adapter->rx_buffer_len,
  3674. DMA_FROM_DEVICE);
  3675. buffer_info->dma = 0;
  3676. adapter->alloc_rx_buff_failed++;
  3677. break; /* while !buffer_info->skb */
  3678. }
  3679. rx_desc = E1000_RX_DESC(*rx_ring, i);
  3680. rx_desc->buffer_addr = cpu_to_le64(buffer_info->dma);
  3681. if (unlikely(++i == rx_ring->count))
  3682. i = 0;
  3683. buffer_info = &rx_ring->buffer_info[i];
  3684. }
  3685. if (likely(rx_ring->next_to_use != i)) {
  3686. rx_ring->next_to_use = i;
  3687. if (unlikely(i-- == 0))
  3688. i = (rx_ring->count - 1);
  3689. /* Force memory writes to complete before letting h/w
  3690. * know there are new descriptors to fetch. (Only
  3691. * applicable for weak-ordered memory model archs,
  3692. * such as IA-64). */
  3693. wmb();
  3694. writel(i, hw->hw_addr + rx_ring->rdt);
  3695. }
  3696. }
  3697. /**
  3698. * e1000_smartspeed - Workaround for SmartSpeed on 82541 and 82547 controllers.
  3699. * @adapter:
  3700. **/
  3701. static void e1000_smartspeed(struct e1000_adapter *adapter)
  3702. {
  3703. struct e1000_hw *hw = &adapter->hw;
  3704. u16 phy_status;
  3705. u16 phy_ctrl;
  3706. if ((hw->phy_type != e1000_phy_igp) || !hw->autoneg ||
  3707. !(hw->autoneg_advertised & ADVERTISE_1000_FULL))
  3708. return;
  3709. if (adapter->smartspeed == 0) {
  3710. /* If Master/Slave config fault is asserted twice,
  3711. * we assume back-to-back */
  3712. e1000_read_phy_reg(hw, PHY_1000T_STATUS, &phy_status);
  3713. if (!(phy_status & SR_1000T_MS_CONFIG_FAULT)) return;
  3714. e1000_read_phy_reg(hw, PHY_1000T_STATUS, &phy_status);
  3715. if (!(phy_status & SR_1000T_MS_CONFIG_FAULT)) return;
  3716. e1000_read_phy_reg(hw, PHY_1000T_CTRL, &phy_ctrl);
  3717. if (phy_ctrl & CR_1000T_MS_ENABLE) {
  3718. phy_ctrl &= ~CR_1000T_MS_ENABLE;
  3719. e1000_write_phy_reg(hw, PHY_1000T_CTRL,
  3720. phy_ctrl);
  3721. adapter->smartspeed++;
  3722. if (!e1000_phy_setup_autoneg(hw) &&
  3723. !e1000_read_phy_reg(hw, PHY_CTRL,
  3724. &phy_ctrl)) {
  3725. phy_ctrl |= (MII_CR_AUTO_NEG_EN |
  3726. MII_CR_RESTART_AUTO_NEG);
  3727. e1000_write_phy_reg(hw, PHY_CTRL,
  3728. phy_ctrl);
  3729. }
  3730. }
  3731. return;
  3732. } else if (adapter->smartspeed == E1000_SMARTSPEED_DOWNSHIFT) {
  3733. /* If still no link, perhaps using 2/3 pair cable */
  3734. e1000_read_phy_reg(hw, PHY_1000T_CTRL, &phy_ctrl);
  3735. phy_ctrl |= CR_1000T_MS_ENABLE;
  3736. e1000_write_phy_reg(hw, PHY_1000T_CTRL, phy_ctrl);
  3737. if (!e1000_phy_setup_autoneg(hw) &&
  3738. !e1000_read_phy_reg(hw, PHY_CTRL, &phy_ctrl)) {
  3739. phy_ctrl |= (MII_CR_AUTO_NEG_EN |
  3740. MII_CR_RESTART_AUTO_NEG);
  3741. e1000_write_phy_reg(hw, PHY_CTRL, phy_ctrl);
  3742. }
  3743. }
  3744. /* Restart process after E1000_SMARTSPEED_MAX iterations */
  3745. if (adapter->smartspeed++ == E1000_SMARTSPEED_MAX)
  3746. adapter->smartspeed = 0;
  3747. }
  3748. /**
  3749. * e1000_ioctl -
  3750. * @netdev:
  3751. * @ifreq:
  3752. * @cmd:
  3753. **/
  3754. static int e1000_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
  3755. {
  3756. switch (cmd) {
  3757. case SIOCGMIIPHY:
  3758. case SIOCGMIIREG:
  3759. case SIOCSMIIREG:
  3760. return e1000_mii_ioctl(netdev, ifr, cmd);
  3761. default:
  3762. return -EOPNOTSUPP;
  3763. }
  3764. }
  3765. /**
  3766. * e1000_mii_ioctl -
  3767. * @netdev:
  3768. * @ifreq:
  3769. * @cmd:
  3770. **/
  3771. static int e1000_mii_ioctl(struct net_device *netdev, struct ifreq *ifr,
  3772. int cmd)
  3773. {
  3774. struct e1000_adapter *adapter = netdev_priv(netdev);
  3775. struct e1000_hw *hw = &adapter->hw;
  3776. struct mii_ioctl_data *data = if_mii(ifr);
  3777. int retval;
  3778. u16 mii_reg;
  3779. unsigned long flags;
  3780. if (hw->media_type != e1000_media_type_copper)
  3781. return -EOPNOTSUPP;
  3782. switch (cmd) {
  3783. case SIOCGMIIPHY:
  3784. data->phy_id = hw->phy_addr;
  3785. break;
  3786. case SIOCGMIIREG:
  3787. spin_lock_irqsave(&adapter->stats_lock, flags);
  3788. if (e1000_read_phy_reg(hw, data->reg_num & 0x1F,
  3789. &data->val_out)) {
  3790. spin_unlock_irqrestore(&adapter->stats_lock, flags);
  3791. return -EIO;
  3792. }
  3793. spin_unlock_irqrestore(&adapter->stats_lock, flags);
  3794. break;
  3795. case SIOCSMIIREG:
  3796. if (data->reg_num & ~(0x1F))
  3797. return -EFAULT;
  3798. mii_reg = data->val_in;
  3799. spin_lock_irqsave(&adapter->stats_lock, flags);
  3800. if (e1000_write_phy_reg(hw, data->reg_num,
  3801. mii_reg)) {
  3802. spin_unlock_irqrestore(&adapter->stats_lock, flags);
  3803. return -EIO;
  3804. }
  3805. spin_unlock_irqrestore(&adapter->stats_lock, flags);
  3806. if (hw->media_type == e1000_media_type_copper) {
  3807. switch (data->reg_num) {
  3808. case PHY_CTRL:
  3809. if (mii_reg & MII_CR_POWER_DOWN)
  3810. break;
  3811. if (mii_reg & MII_CR_AUTO_NEG_EN) {
  3812. hw->autoneg = 1;
  3813. hw->autoneg_advertised = 0x2F;
  3814. } else {
  3815. u32 speed;
  3816. if (mii_reg & 0x40)
  3817. speed = SPEED_1000;
  3818. else if (mii_reg & 0x2000)
  3819. speed = SPEED_100;
  3820. else
  3821. speed = SPEED_10;
  3822. retval = e1000_set_spd_dplx(
  3823. adapter, speed,
  3824. ((mii_reg & 0x100)
  3825. ? DUPLEX_FULL :
  3826. DUPLEX_HALF));
  3827. if (retval)
  3828. return retval;
  3829. }
  3830. if (netif_running(adapter->netdev))
  3831. e1000_reinit_locked(adapter);
  3832. else
  3833. e1000_reset(adapter);
  3834. break;
  3835. case M88E1000_PHY_SPEC_CTRL:
  3836. case M88E1000_EXT_PHY_SPEC_CTRL:
  3837. if (e1000_phy_reset(hw))
  3838. return -EIO;
  3839. break;
  3840. }
  3841. } else {
  3842. switch (data->reg_num) {
  3843. case PHY_CTRL:
  3844. if (mii_reg & MII_CR_POWER_DOWN)
  3845. break;
  3846. if (netif_running(adapter->netdev))
  3847. e1000_reinit_locked(adapter);
  3848. else
  3849. e1000_reset(adapter);
  3850. break;
  3851. }
  3852. }
  3853. break;
  3854. default:
  3855. return -EOPNOTSUPP;
  3856. }
  3857. return E1000_SUCCESS;
  3858. }
  3859. void e1000_pci_set_mwi(struct e1000_hw *hw)
  3860. {
  3861. struct e1000_adapter *adapter = hw->back;
  3862. int ret_val = pci_set_mwi(adapter->pdev);
  3863. if (ret_val)
  3864. e_err(probe, "Error in setting MWI\n");
  3865. }
  3866. void e1000_pci_clear_mwi(struct e1000_hw *hw)
  3867. {
  3868. struct e1000_adapter *adapter = hw->back;
  3869. pci_clear_mwi(adapter->pdev);
  3870. }
  3871. int e1000_pcix_get_mmrbc(struct e1000_hw *hw)
  3872. {
  3873. struct e1000_adapter *adapter = hw->back;
  3874. return pcix_get_mmrbc(adapter->pdev);
  3875. }
  3876. void e1000_pcix_set_mmrbc(struct e1000_hw *hw, int mmrbc)
  3877. {
  3878. struct e1000_adapter *adapter = hw->back;
  3879. pcix_set_mmrbc(adapter->pdev, mmrbc);
  3880. }
  3881. void e1000_io_write(struct e1000_hw *hw, unsigned long port, u32 value)
  3882. {
  3883. outl(value, port);
  3884. }
  3885. static bool e1000_vlan_used(struct e1000_adapter *adapter)
  3886. {
  3887. u16 vid;
  3888. for_each_set_bit(vid, adapter->active_vlans, VLAN_N_VID)
  3889. return true;
  3890. return false;
  3891. }
  3892. static void e1000_vlan_filter_on_off(struct e1000_adapter *adapter,
  3893. bool filter_on)
  3894. {
  3895. struct e1000_hw *hw = &adapter->hw;
  3896. u32 rctl;
  3897. if (!test_bit(__E1000_DOWN, &adapter->flags))
  3898. e1000_irq_disable(adapter);
  3899. if (filter_on) {
  3900. /* enable VLAN receive filtering */
  3901. rctl = er32(RCTL);
  3902. rctl &= ~E1000_RCTL_CFIEN;
  3903. if (!(adapter->netdev->flags & IFF_PROMISC))
  3904. rctl |= E1000_RCTL_VFE;
  3905. ew32(RCTL, rctl);
  3906. e1000_update_mng_vlan(adapter);
  3907. } else {
  3908. /* disable VLAN receive filtering */
  3909. rctl = er32(RCTL);
  3910. rctl &= ~E1000_RCTL_VFE;
  3911. ew32(RCTL, rctl);
  3912. }
  3913. if (!test_bit(__E1000_DOWN, &adapter->flags))
  3914. e1000_irq_enable(adapter);
  3915. }
  3916. static void e1000_vlan_mode(struct net_device *netdev, u32 features)
  3917. {
  3918. struct e1000_adapter *adapter = netdev_priv(netdev);
  3919. struct e1000_hw *hw = &adapter->hw;
  3920. u32 ctrl;
  3921. if (!test_bit(__E1000_DOWN, &adapter->flags))
  3922. e1000_irq_disable(adapter);
  3923. ctrl = er32(CTRL);
  3924. if (features & NETIF_F_HW_VLAN_RX) {
  3925. /* enable VLAN tag insert/strip */
  3926. ctrl |= E1000_CTRL_VME;
  3927. } else {
  3928. /* disable VLAN tag insert/strip */
  3929. ctrl &= ~E1000_CTRL_VME;
  3930. }
  3931. ew32(CTRL, ctrl);
  3932. if (!test_bit(__E1000_DOWN, &adapter->flags))
  3933. e1000_irq_enable(adapter);
  3934. }
  3935. static void e1000_vlan_rx_add_vid(struct net_device *netdev, u16 vid)
  3936. {
  3937. struct e1000_adapter *adapter = netdev_priv(netdev);
  3938. struct e1000_hw *hw = &adapter->hw;
  3939. u32 vfta, index;
  3940. if ((hw->mng_cookie.status &
  3941. E1000_MNG_DHCP_COOKIE_STATUS_VLAN_SUPPORT) &&
  3942. (vid == adapter->mng_vlan_id))
  3943. return;
  3944. if (!e1000_vlan_used(adapter))
  3945. e1000_vlan_filter_on_off(adapter, true);
  3946. /* add VID to filter table */
  3947. index = (vid >> 5) & 0x7F;
  3948. vfta = E1000_READ_REG_ARRAY(hw, VFTA, index);
  3949. vfta |= (1 << (vid & 0x1F));
  3950. e1000_write_vfta(hw, index, vfta);
  3951. set_bit(vid, adapter->active_vlans);
  3952. }
  3953. static void e1000_vlan_rx_kill_vid(struct net_device *netdev, u16 vid)
  3954. {
  3955. struct e1000_adapter *adapter = netdev_priv(netdev);
  3956. struct e1000_hw *hw = &adapter->hw;
  3957. u32 vfta, index;
  3958. if (!test_bit(__E1000_DOWN, &adapter->flags))
  3959. e1000_irq_disable(adapter);
  3960. if (!test_bit(__E1000_DOWN, &adapter->flags))
  3961. e1000_irq_enable(adapter);
  3962. /* remove VID from filter table */
  3963. index = (vid >> 5) & 0x7F;
  3964. vfta = E1000_READ_REG_ARRAY(hw, VFTA, index);
  3965. vfta &= ~(1 << (vid & 0x1F));
  3966. e1000_write_vfta(hw, index, vfta);
  3967. clear_bit(vid, adapter->active_vlans);
  3968. if (!e1000_vlan_used(adapter))
  3969. e1000_vlan_filter_on_off(adapter, false);
  3970. }
  3971. static void e1000_restore_vlan(struct e1000_adapter *adapter)
  3972. {
  3973. u16 vid;
  3974. if (!e1000_vlan_used(adapter))
  3975. return;
  3976. e1000_vlan_filter_on_off(adapter, true);
  3977. for_each_set_bit(vid, adapter->active_vlans, VLAN_N_VID)
  3978. e1000_vlan_rx_add_vid(adapter->netdev, vid);
  3979. }
  3980. int e1000_set_spd_dplx(struct e1000_adapter *adapter, u32 spd, u8 dplx)
  3981. {
  3982. struct e1000_hw *hw = &adapter->hw;
  3983. hw->autoneg = 0;
  3984. /* Make sure dplx is at most 1 bit and lsb of speed is not set
  3985. * for the switch() below to work */
  3986. if ((spd & 1) || (dplx & ~1))
  3987. goto err_inval;
  3988. /* Fiber NICs only allow 1000 gbps Full duplex */
  3989. if ((hw->media_type == e1000_media_type_fiber) &&
  3990. spd != SPEED_1000 &&
  3991. dplx != DUPLEX_FULL)
  3992. goto err_inval;
  3993. switch (spd + dplx) {
  3994. case SPEED_10 + DUPLEX_HALF:
  3995. hw->forced_speed_duplex = e1000_10_half;
  3996. break;
  3997. case SPEED_10 + DUPLEX_FULL:
  3998. hw->forced_speed_duplex = e1000_10_full;
  3999. break;
  4000. case SPEED_100 + DUPLEX_HALF:
  4001. hw->forced_speed_duplex = e1000_100_half;
  4002. break;
  4003. case SPEED_100 + DUPLEX_FULL:
  4004. hw->forced_speed_duplex = e1000_100_full;
  4005. break;
  4006. case SPEED_1000 + DUPLEX_FULL:
  4007. hw->autoneg = 1;
  4008. hw->autoneg_advertised = ADVERTISE_1000_FULL;
  4009. break;
  4010. case SPEED_1000 + DUPLEX_HALF: /* not supported */
  4011. default:
  4012. goto err_inval;
  4013. }
  4014. return 0;
  4015. err_inval:
  4016. e_err(probe, "Unsupported Speed/Duplex configuration\n");
  4017. return -EINVAL;
  4018. }
  4019. static int __e1000_shutdown(struct pci_dev *pdev, bool *enable_wake)
  4020. {
  4021. struct net_device *netdev = pci_get_drvdata(pdev);
  4022. struct e1000_adapter *adapter = netdev_priv(netdev);
  4023. struct e1000_hw *hw = &adapter->hw;
  4024. u32 ctrl, ctrl_ext, rctl, status;
  4025. u32 wufc = adapter->wol;
  4026. #ifdef CONFIG_PM
  4027. int retval = 0;
  4028. #endif
  4029. netif_device_detach(netdev);
  4030. if (netif_running(netdev)) {
  4031. WARN_ON(test_bit(__E1000_RESETTING, &adapter->flags));
  4032. e1000_down(adapter);
  4033. }
  4034. #ifdef CONFIG_PM
  4035. retval = pci_save_state(pdev);
  4036. if (retval)
  4037. return retval;
  4038. #endif
  4039. status = er32(STATUS);
  4040. if (status & E1000_STATUS_LU)
  4041. wufc &= ~E1000_WUFC_LNKC;
  4042. if (wufc) {
  4043. e1000_setup_rctl(adapter);
  4044. e1000_set_rx_mode(netdev);
  4045. /* turn on all-multi mode if wake on multicast is enabled */
  4046. if (wufc & E1000_WUFC_MC) {
  4047. rctl = er32(RCTL);
  4048. rctl |= E1000_RCTL_MPE;
  4049. ew32(RCTL, rctl);
  4050. }
  4051. if (hw->mac_type >= e1000_82540) {
  4052. ctrl = er32(CTRL);
  4053. /* advertise wake from D3Cold */
  4054. #define E1000_CTRL_ADVD3WUC 0x00100000
  4055. /* phy power management enable */
  4056. #define E1000_CTRL_EN_PHY_PWR_MGMT 0x00200000
  4057. ctrl |= E1000_CTRL_ADVD3WUC |
  4058. E1000_CTRL_EN_PHY_PWR_MGMT;
  4059. ew32(CTRL, ctrl);
  4060. }
  4061. if (hw->media_type == e1000_media_type_fiber ||
  4062. hw->media_type == e1000_media_type_internal_serdes) {
  4063. /* keep the laser running in D3 */
  4064. ctrl_ext = er32(CTRL_EXT);
  4065. ctrl_ext |= E1000_CTRL_EXT_SDP7_DATA;
  4066. ew32(CTRL_EXT, ctrl_ext);
  4067. }
  4068. ew32(WUC, E1000_WUC_PME_EN);
  4069. ew32(WUFC, wufc);
  4070. } else {
  4071. ew32(WUC, 0);
  4072. ew32(WUFC, 0);
  4073. }
  4074. e1000_release_manageability(adapter);
  4075. *enable_wake = !!wufc;
  4076. /* make sure adapter isn't asleep if manageability is enabled */
  4077. if (adapter->en_mng_pt)
  4078. *enable_wake = true;
  4079. if (netif_running(netdev))
  4080. e1000_free_irq(adapter);
  4081. pci_disable_device(pdev);
  4082. return 0;
  4083. }
  4084. #ifdef CONFIG_PM
  4085. static int e1000_suspend(struct pci_dev *pdev, pm_message_t state)
  4086. {
  4087. int retval;
  4088. bool wake;
  4089. retval = __e1000_shutdown(pdev, &wake);
  4090. if (retval)
  4091. return retval;
  4092. if (wake) {
  4093. pci_prepare_to_sleep(pdev);
  4094. } else {
  4095. pci_wake_from_d3(pdev, false);
  4096. pci_set_power_state(pdev, PCI_D3hot);
  4097. }
  4098. return 0;
  4099. }
  4100. static int e1000_resume(struct pci_dev *pdev)
  4101. {
  4102. struct net_device *netdev = pci_get_drvdata(pdev);
  4103. struct e1000_adapter *adapter = netdev_priv(netdev);
  4104. struct e1000_hw *hw = &adapter->hw;
  4105. u32 err;
  4106. pci_set_power_state(pdev, PCI_D0);
  4107. pci_restore_state(pdev);
  4108. pci_save_state(pdev);
  4109. if (adapter->need_ioport)
  4110. err = pci_enable_device(pdev);
  4111. else
  4112. err = pci_enable_device_mem(pdev);
  4113. if (err) {
  4114. pr_err("Cannot enable PCI device from suspend\n");
  4115. return err;
  4116. }
  4117. pci_set_master(pdev);
  4118. pci_enable_wake(pdev, PCI_D3hot, 0);
  4119. pci_enable_wake(pdev, PCI_D3cold, 0);
  4120. if (netif_running(netdev)) {
  4121. err = e1000_request_irq(adapter);
  4122. if (err)
  4123. return err;
  4124. }
  4125. e1000_power_up_phy(adapter);
  4126. e1000_reset(adapter);
  4127. ew32(WUS, ~0);
  4128. e1000_init_manageability(adapter);
  4129. if (netif_running(netdev))
  4130. e1000_up(adapter);
  4131. netif_device_attach(netdev);
  4132. return 0;
  4133. }
  4134. #endif
  4135. static void e1000_shutdown(struct pci_dev *pdev)
  4136. {
  4137. bool wake;
  4138. __e1000_shutdown(pdev, &wake);
  4139. if (system_state == SYSTEM_POWER_OFF) {
  4140. pci_wake_from_d3(pdev, wake);
  4141. pci_set_power_state(pdev, PCI_D3hot);
  4142. }
  4143. }
  4144. #ifdef CONFIG_NET_POLL_CONTROLLER
  4145. /*
  4146. * Polling 'interrupt' - used by things like netconsole to send skbs
  4147. * without having to re-enable interrupts. It's not called while
  4148. * the interrupt routine is executing.
  4149. */
  4150. static void e1000_netpoll(struct net_device *netdev)
  4151. {
  4152. struct e1000_adapter *adapter = netdev_priv(netdev);
  4153. disable_irq(adapter->pdev->irq);
  4154. e1000_intr(adapter->pdev->irq, netdev);
  4155. enable_irq(adapter->pdev->irq);
  4156. }
  4157. #endif
  4158. /**
  4159. * e1000_io_error_detected - called when PCI error is detected
  4160. * @pdev: Pointer to PCI device
  4161. * @state: The current pci connection state
  4162. *
  4163. * This function is called after a PCI bus error affecting
  4164. * this device has been detected.
  4165. */
  4166. static pci_ers_result_t e1000_io_error_detected(struct pci_dev *pdev,
  4167. pci_channel_state_t state)
  4168. {
  4169. struct net_device *netdev = pci_get_drvdata(pdev);
  4170. struct e1000_adapter *adapter = netdev_priv(netdev);
  4171. netif_device_detach(netdev);
  4172. if (state == pci_channel_io_perm_failure)
  4173. return PCI_ERS_RESULT_DISCONNECT;
  4174. if (netif_running(netdev))
  4175. e1000_down(adapter);
  4176. pci_disable_device(pdev);
  4177. /* Request a slot slot reset. */
  4178. return PCI_ERS_RESULT_NEED_RESET;
  4179. }
  4180. /**
  4181. * e1000_io_slot_reset - called after the pci bus has been reset.
  4182. * @pdev: Pointer to PCI device
  4183. *
  4184. * Restart the card from scratch, as if from a cold-boot. Implementation
  4185. * resembles the first-half of the e1000_resume routine.
  4186. */
  4187. static pci_ers_result_t e1000_io_slot_reset(struct pci_dev *pdev)
  4188. {
  4189. struct net_device *netdev = pci_get_drvdata(pdev);
  4190. struct e1000_adapter *adapter = netdev_priv(netdev);
  4191. struct e1000_hw *hw = &adapter->hw;
  4192. int err;
  4193. if (adapter->need_ioport)
  4194. err = pci_enable_device(pdev);
  4195. else
  4196. err = pci_enable_device_mem(pdev);
  4197. if (err) {
  4198. pr_err("Cannot re-enable PCI device after reset.\n");
  4199. return PCI_ERS_RESULT_DISCONNECT;
  4200. }
  4201. pci_set_master(pdev);
  4202. pci_enable_wake(pdev, PCI_D3hot, 0);
  4203. pci_enable_wake(pdev, PCI_D3cold, 0);
  4204. e1000_reset(adapter);
  4205. ew32(WUS, ~0);
  4206. return PCI_ERS_RESULT_RECOVERED;
  4207. }
  4208. /**
  4209. * e1000_io_resume - called when traffic can start flowing again.
  4210. * @pdev: Pointer to PCI device
  4211. *
  4212. * This callback is called when the error recovery driver tells us that
  4213. * its OK to resume normal operation. Implementation resembles the
  4214. * second-half of the e1000_resume routine.
  4215. */
  4216. static void e1000_io_resume(struct pci_dev *pdev)
  4217. {
  4218. struct net_device *netdev = pci_get_drvdata(pdev);
  4219. struct e1000_adapter *adapter = netdev_priv(netdev);
  4220. e1000_init_manageability(adapter);
  4221. if (netif_running(netdev)) {
  4222. if (e1000_up(adapter)) {
  4223. pr_info("can't bring device back up after reset\n");
  4224. return;
  4225. }
  4226. }
  4227. netif_device_attach(netdev);
  4228. }
  4229. /* e1000_main.c */