igb_main.c 122 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519
  1. /*******************************************************************************
  2. Intel(R) Gigabit Ethernet Linux driver
  3. Copyright(c) 2007 Intel Corporation.
  4. This program is free software; you can redistribute it and/or modify it
  5. under the terms and conditions of the GNU General Public License,
  6. version 2, as published by the Free Software Foundation.
  7. This program is distributed in the hope it will be useful, but WITHOUT
  8. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  9. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  10. more details.
  11. You should have received a copy of the GNU General Public License along with
  12. this program; if not, write to the Free Software Foundation, Inc.,
  13. 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  14. The full GNU General Public License is included in this distribution in
  15. the file called "COPYING".
  16. Contact Information:
  17. e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
  18. Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  19. *******************************************************************************/
  20. #include <linux/module.h>
  21. #include <linux/types.h>
  22. #include <linux/init.h>
  23. #include <linux/vmalloc.h>
  24. #include <linux/pagemap.h>
  25. #include <linux/netdevice.h>
  26. #include <linux/ipv6.h>
  27. #include <net/checksum.h>
  28. #include <net/ip6_checksum.h>
  29. #include <linux/mii.h>
  30. #include <linux/ethtool.h>
  31. #include <linux/if_vlan.h>
  32. #include <linux/pci.h>
  33. #include <linux/delay.h>
  34. #include <linux/interrupt.h>
  35. #include <linux/if_ether.h>
  36. #ifdef CONFIG_DCA
  37. #include <linux/dca.h>
  38. #endif
  39. #include "igb.h"
  40. #define DRV_VERSION "1.2.45-k2"
  41. char igb_driver_name[] = "igb";
  42. char igb_driver_version[] = DRV_VERSION;
  43. static const char igb_driver_string[] =
  44. "Intel(R) Gigabit Ethernet Network Driver";
  45. static const char igb_copyright[] = "Copyright (c) 2008 Intel Corporation.";
  46. static const struct e1000_info *igb_info_tbl[] = {
  47. [board_82575] = &e1000_82575_info,
  48. };
  49. static struct pci_device_id igb_pci_tbl[] = {
  50. { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576), board_82575 },
  51. { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_FIBER), board_82575 },
  52. { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_SERDES), board_82575 },
  53. { PCI_VDEVICE(INTEL, E1000_DEV_ID_82576_QUAD_COPPER), board_82575 },
  54. { PCI_VDEVICE(INTEL, E1000_DEV_ID_82575EB_COPPER), board_82575 },
  55. { PCI_VDEVICE(INTEL, E1000_DEV_ID_82575EB_FIBER_SERDES), board_82575 },
  56. { PCI_VDEVICE(INTEL, E1000_DEV_ID_82575GB_QUAD_COPPER), board_82575 },
  57. /* required last entry */
  58. {0, }
  59. };
  60. MODULE_DEVICE_TABLE(pci, igb_pci_tbl);
  61. void igb_reset(struct igb_adapter *);
  62. static int igb_setup_all_tx_resources(struct igb_adapter *);
  63. static int igb_setup_all_rx_resources(struct igb_adapter *);
  64. static void igb_free_all_tx_resources(struct igb_adapter *);
  65. static void igb_free_all_rx_resources(struct igb_adapter *);
  66. static void igb_free_tx_resources(struct igb_ring *);
  67. static void igb_free_rx_resources(struct igb_ring *);
  68. void igb_update_stats(struct igb_adapter *);
  69. static int igb_probe(struct pci_dev *, const struct pci_device_id *);
  70. static void __devexit igb_remove(struct pci_dev *pdev);
  71. static int igb_sw_init(struct igb_adapter *);
  72. static int igb_open(struct net_device *);
  73. static int igb_close(struct net_device *);
  74. static void igb_configure_tx(struct igb_adapter *);
  75. static void igb_configure_rx(struct igb_adapter *);
  76. static void igb_setup_rctl(struct igb_adapter *);
  77. static void igb_clean_all_tx_rings(struct igb_adapter *);
  78. static void igb_clean_all_rx_rings(struct igb_adapter *);
  79. static void igb_clean_tx_ring(struct igb_ring *);
  80. static void igb_clean_rx_ring(struct igb_ring *);
  81. static void igb_set_multi(struct net_device *);
  82. static void igb_update_phy_info(unsigned long);
  83. static void igb_watchdog(unsigned long);
  84. static void igb_watchdog_task(struct work_struct *);
  85. static int igb_xmit_frame_ring_adv(struct sk_buff *, struct net_device *,
  86. struct igb_ring *);
  87. static int igb_xmit_frame_adv(struct sk_buff *skb, struct net_device *);
  88. static struct net_device_stats *igb_get_stats(struct net_device *);
  89. static int igb_change_mtu(struct net_device *, int);
  90. static int igb_set_mac(struct net_device *, void *);
  91. static irqreturn_t igb_intr(int irq, void *);
  92. static irqreturn_t igb_intr_msi(int irq, void *);
  93. static irqreturn_t igb_msix_other(int irq, void *);
  94. static irqreturn_t igb_msix_rx(int irq, void *);
  95. static irqreturn_t igb_msix_tx(int irq, void *);
  96. static int igb_clean_rx_ring_msix(struct napi_struct *, int);
  97. #ifdef CONFIG_DCA
  98. static void igb_update_rx_dca(struct igb_ring *);
  99. static void igb_update_tx_dca(struct igb_ring *);
  100. static void igb_setup_dca(struct igb_adapter *);
  101. #endif /* CONFIG_DCA */
  102. static bool igb_clean_tx_irq(struct igb_ring *);
  103. static int igb_poll(struct napi_struct *, int);
  104. static bool igb_clean_rx_irq_adv(struct igb_ring *, int *, int);
  105. static void igb_alloc_rx_buffers_adv(struct igb_ring *, int);
  106. #ifdef CONFIG_IGB_LRO
  107. static int igb_get_skb_hdr(struct sk_buff *skb, void **, void **, u64 *, void *);
  108. #endif
  109. static int igb_ioctl(struct net_device *, struct ifreq *, int cmd);
  110. static void igb_tx_timeout(struct net_device *);
  111. static void igb_reset_task(struct work_struct *);
  112. static void igb_vlan_rx_register(struct net_device *, struct vlan_group *);
  113. static void igb_vlan_rx_add_vid(struct net_device *, u16);
  114. static void igb_vlan_rx_kill_vid(struct net_device *, u16);
  115. static void igb_restore_vlan(struct igb_adapter *);
  116. static int igb_suspend(struct pci_dev *, pm_message_t);
  117. #ifdef CONFIG_PM
  118. static int igb_resume(struct pci_dev *);
  119. #endif
  120. static void igb_shutdown(struct pci_dev *);
  121. #ifdef CONFIG_DCA
  122. static int igb_notify_dca(struct notifier_block *, unsigned long, void *);
  123. static struct notifier_block dca_notifier = {
  124. .notifier_call = igb_notify_dca,
  125. .next = NULL,
  126. .priority = 0
  127. };
  128. #endif
  129. #ifdef CONFIG_NET_POLL_CONTROLLER
  130. /* for netdump / net console */
  131. static void igb_netpoll(struct net_device *);
  132. #endif
  133. static pci_ers_result_t igb_io_error_detected(struct pci_dev *,
  134. pci_channel_state_t);
  135. static pci_ers_result_t igb_io_slot_reset(struct pci_dev *);
  136. static void igb_io_resume(struct pci_dev *);
  137. static struct pci_error_handlers igb_err_handler = {
  138. .error_detected = igb_io_error_detected,
  139. .slot_reset = igb_io_slot_reset,
  140. .resume = igb_io_resume,
  141. };
  142. static struct pci_driver igb_driver = {
  143. .name = igb_driver_name,
  144. .id_table = igb_pci_tbl,
  145. .probe = igb_probe,
  146. .remove = __devexit_p(igb_remove),
  147. #ifdef CONFIG_PM
  148. /* Power Managment Hooks */
  149. .suspend = igb_suspend,
  150. .resume = igb_resume,
  151. #endif
  152. .shutdown = igb_shutdown,
  153. .err_handler = &igb_err_handler
  154. };
  155. static int global_quad_port_a; /* global quad port a indication */
  156. MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
  157. MODULE_DESCRIPTION("Intel(R) Gigabit Ethernet Network Driver");
  158. MODULE_LICENSE("GPL");
  159. MODULE_VERSION(DRV_VERSION);
  160. #ifdef DEBUG
  161. /**
  162. * igb_get_hw_dev_name - return device name string
  163. * used by hardware layer to print debugging information
  164. **/
  165. char *igb_get_hw_dev_name(struct e1000_hw *hw)
  166. {
  167. struct igb_adapter *adapter = hw->back;
  168. return adapter->netdev->name;
  169. }
  170. #endif
  171. /**
  172. * igb_init_module - Driver Registration Routine
  173. *
  174. * igb_init_module is the first routine called when the driver is
  175. * loaded. All it does is register with the PCI subsystem.
  176. **/
  177. static int __init igb_init_module(void)
  178. {
  179. int ret;
  180. printk(KERN_INFO "%s - version %s\n",
  181. igb_driver_string, igb_driver_version);
  182. printk(KERN_INFO "%s\n", igb_copyright);
  183. global_quad_port_a = 0;
  184. ret = pci_register_driver(&igb_driver);
  185. #ifdef CONFIG_DCA
  186. dca_register_notify(&dca_notifier);
  187. #endif
  188. return ret;
  189. }
  190. module_init(igb_init_module);
  191. /**
  192. * igb_exit_module - Driver Exit Cleanup Routine
  193. *
  194. * igb_exit_module is called just before the driver is removed
  195. * from memory.
  196. **/
  197. static void __exit igb_exit_module(void)
  198. {
  199. #ifdef CONFIG_DCA
  200. dca_unregister_notify(&dca_notifier);
  201. #endif
  202. pci_unregister_driver(&igb_driver);
  203. }
  204. module_exit(igb_exit_module);
  205. /**
  206. * igb_alloc_queues - Allocate memory for all rings
  207. * @adapter: board private structure to initialize
  208. *
  209. * We allocate one ring per queue at run-time since we don't know the
  210. * number of queues at compile-time.
  211. **/
  212. static int igb_alloc_queues(struct igb_adapter *adapter)
  213. {
  214. int i;
  215. adapter->tx_ring = kcalloc(adapter->num_tx_queues,
  216. sizeof(struct igb_ring), GFP_KERNEL);
  217. if (!adapter->tx_ring)
  218. return -ENOMEM;
  219. adapter->rx_ring = kcalloc(adapter->num_rx_queues,
  220. sizeof(struct igb_ring), GFP_KERNEL);
  221. if (!adapter->rx_ring) {
  222. kfree(adapter->tx_ring);
  223. return -ENOMEM;
  224. }
  225. adapter->rx_ring->buddy = adapter->tx_ring;
  226. for (i = 0; i < adapter->num_tx_queues; i++) {
  227. struct igb_ring *ring = &(adapter->tx_ring[i]);
  228. ring->adapter = adapter;
  229. ring->queue_index = i;
  230. }
  231. for (i = 0; i < adapter->num_rx_queues; i++) {
  232. struct igb_ring *ring = &(adapter->rx_ring[i]);
  233. ring->adapter = adapter;
  234. ring->queue_index = i;
  235. ring->itr_register = E1000_ITR;
  236. /* set a default napi handler for each rx_ring */
  237. netif_napi_add(adapter->netdev, &ring->napi, igb_poll, 64);
  238. }
  239. return 0;
  240. }
  241. static void igb_free_queues(struct igb_adapter *adapter)
  242. {
  243. int i;
  244. for (i = 0; i < adapter->num_rx_queues; i++)
  245. netif_napi_del(&adapter->rx_ring[i].napi);
  246. kfree(adapter->tx_ring);
  247. kfree(adapter->rx_ring);
  248. }
  249. #define IGB_N0_QUEUE -1
  250. static void igb_assign_vector(struct igb_adapter *adapter, int rx_queue,
  251. int tx_queue, int msix_vector)
  252. {
  253. u32 msixbm = 0;
  254. struct e1000_hw *hw = &adapter->hw;
  255. u32 ivar, index;
  256. switch (hw->mac.type) {
  257. case e1000_82575:
  258. /* The 82575 assigns vectors using a bitmask, which matches the
  259. bitmask for the EICR/EIMS/EIMC registers. To assign one
  260. or more queues to a vector, we write the appropriate bits
  261. into the MSIXBM register for that vector. */
  262. if (rx_queue > IGB_N0_QUEUE) {
  263. msixbm = E1000_EICR_RX_QUEUE0 << rx_queue;
  264. adapter->rx_ring[rx_queue].eims_value = msixbm;
  265. }
  266. if (tx_queue > IGB_N0_QUEUE) {
  267. msixbm |= E1000_EICR_TX_QUEUE0 << tx_queue;
  268. adapter->tx_ring[tx_queue].eims_value =
  269. E1000_EICR_TX_QUEUE0 << tx_queue;
  270. }
  271. array_wr32(E1000_MSIXBM(0), msix_vector, msixbm);
  272. break;
  273. case e1000_82576:
  274. /* The 82576 uses a table-based method for assigning vectors.
  275. Each queue has a single entry in the table to which we write
  276. a vector number along with a "valid" bit. Sadly, the layout
  277. of the table is somewhat counterintuitive. */
  278. if (rx_queue > IGB_N0_QUEUE) {
  279. index = (rx_queue & 0x7);
  280. ivar = array_rd32(E1000_IVAR0, index);
  281. if (rx_queue < 8) {
  282. /* vector goes into low byte of register */
  283. ivar = ivar & 0xFFFFFF00;
  284. ivar |= msix_vector | E1000_IVAR_VALID;
  285. } else {
  286. /* vector goes into third byte of register */
  287. ivar = ivar & 0xFF00FFFF;
  288. ivar |= (msix_vector | E1000_IVAR_VALID) << 16;
  289. }
  290. adapter->rx_ring[rx_queue].eims_value= 1 << msix_vector;
  291. array_wr32(E1000_IVAR0, index, ivar);
  292. }
  293. if (tx_queue > IGB_N0_QUEUE) {
  294. index = (tx_queue & 0x7);
  295. ivar = array_rd32(E1000_IVAR0, index);
  296. if (tx_queue < 8) {
  297. /* vector goes into second byte of register */
  298. ivar = ivar & 0xFFFF00FF;
  299. ivar |= (msix_vector | E1000_IVAR_VALID) << 8;
  300. } else {
  301. /* vector goes into high byte of register */
  302. ivar = ivar & 0x00FFFFFF;
  303. ivar |= (msix_vector | E1000_IVAR_VALID) << 24;
  304. }
  305. adapter->tx_ring[tx_queue].eims_value= 1 << msix_vector;
  306. array_wr32(E1000_IVAR0, index, ivar);
  307. }
  308. break;
  309. default:
  310. BUG();
  311. break;
  312. }
  313. }
  314. /**
  315. * igb_configure_msix - Configure MSI-X hardware
  316. *
  317. * igb_configure_msix sets up the hardware to properly
  318. * generate MSI-X interrupts.
  319. **/
  320. static void igb_configure_msix(struct igb_adapter *adapter)
  321. {
  322. u32 tmp;
  323. int i, vector = 0;
  324. struct e1000_hw *hw = &adapter->hw;
  325. adapter->eims_enable_mask = 0;
  326. if (hw->mac.type == e1000_82576)
  327. /* Turn on MSI-X capability first, or our settings
  328. * won't stick. And it will take days to debug. */
  329. wr32(E1000_GPIE, E1000_GPIE_MSIX_MODE |
  330. E1000_GPIE_PBA | E1000_GPIE_EIAME |
  331. E1000_GPIE_NSICR);
  332. for (i = 0; i < adapter->num_tx_queues; i++) {
  333. struct igb_ring *tx_ring = &adapter->tx_ring[i];
  334. igb_assign_vector(adapter, IGB_N0_QUEUE, i, vector++);
  335. adapter->eims_enable_mask |= tx_ring->eims_value;
  336. if (tx_ring->itr_val)
  337. writel(tx_ring->itr_val,
  338. hw->hw_addr + tx_ring->itr_register);
  339. else
  340. writel(1, hw->hw_addr + tx_ring->itr_register);
  341. }
  342. for (i = 0; i < adapter->num_rx_queues; i++) {
  343. struct igb_ring *rx_ring = &adapter->rx_ring[i];
  344. rx_ring->buddy = NULL;
  345. igb_assign_vector(adapter, i, IGB_N0_QUEUE, vector++);
  346. adapter->eims_enable_mask |= rx_ring->eims_value;
  347. if (rx_ring->itr_val)
  348. writel(rx_ring->itr_val,
  349. hw->hw_addr + rx_ring->itr_register);
  350. else
  351. writel(1, hw->hw_addr + rx_ring->itr_register);
  352. }
  353. /* set vector for other causes, i.e. link changes */
  354. switch (hw->mac.type) {
  355. case e1000_82575:
  356. array_wr32(E1000_MSIXBM(0), vector++,
  357. E1000_EIMS_OTHER);
  358. tmp = rd32(E1000_CTRL_EXT);
  359. /* enable MSI-X PBA support*/
  360. tmp |= E1000_CTRL_EXT_PBA_CLR;
  361. /* Auto-Mask interrupts upon ICR read. */
  362. tmp |= E1000_CTRL_EXT_EIAME;
  363. tmp |= E1000_CTRL_EXT_IRCA;
  364. wr32(E1000_CTRL_EXT, tmp);
  365. adapter->eims_enable_mask |= E1000_EIMS_OTHER;
  366. adapter->eims_other = E1000_EIMS_OTHER;
  367. break;
  368. case e1000_82576:
  369. tmp = (vector++ | E1000_IVAR_VALID) << 8;
  370. wr32(E1000_IVAR_MISC, tmp);
  371. adapter->eims_enable_mask = (1 << (vector)) - 1;
  372. adapter->eims_other = 1 << (vector - 1);
  373. break;
  374. default:
  375. /* do nothing, since nothing else supports MSI-X */
  376. break;
  377. } /* switch (hw->mac.type) */
  378. wrfl();
  379. }
  380. /**
  381. * igb_request_msix - Initialize MSI-X interrupts
  382. *
  383. * igb_request_msix allocates MSI-X vectors and requests interrupts from the
  384. * kernel.
  385. **/
  386. static int igb_request_msix(struct igb_adapter *adapter)
  387. {
  388. struct net_device *netdev = adapter->netdev;
  389. int i, err = 0, vector = 0;
  390. vector = 0;
  391. for (i = 0; i < adapter->num_tx_queues; i++) {
  392. struct igb_ring *ring = &(adapter->tx_ring[i]);
  393. sprintf(ring->name, "%s-tx%d", netdev->name, i);
  394. err = request_irq(adapter->msix_entries[vector].vector,
  395. &igb_msix_tx, 0, ring->name,
  396. &(adapter->tx_ring[i]));
  397. if (err)
  398. goto out;
  399. ring->itr_register = E1000_EITR(0) + (vector << 2);
  400. ring->itr_val = 976; /* ~4000 ints/sec */
  401. vector++;
  402. }
  403. for (i = 0; i < adapter->num_rx_queues; i++) {
  404. struct igb_ring *ring = &(adapter->rx_ring[i]);
  405. if (strlen(netdev->name) < (IFNAMSIZ - 5))
  406. sprintf(ring->name, "%s-rx%d", netdev->name, i);
  407. else
  408. memcpy(ring->name, netdev->name, IFNAMSIZ);
  409. err = request_irq(adapter->msix_entries[vector].vector,
  410. &igb_msix_rx, 0, ring->name,
  411. &(adapter->rx_ring[i]));
  412. if (err)
  413. goto out;
  414. ring->itr_register = E1000_EITR(0) + (vector << 2);
  415. ring->itr_val = adapter->itr;
  416. /* overwrite the poll routine for MSIX, we've already done
  417. * netif_napi_add */
  418. ring->napi.poll = &igb_clean_rx_ring_msix;
  419. vector++;
  420. }
  421. err = request_irq(adapter->msix_entries[vector].vector,
  422. &igb_msix_other, 0, netdev->name, netdev);
  423. if (err)
  424. goto out;
  425. igb_configure_msix(adapter);
  426. return 0;
  427. out:
  428. return err;
  429. }
  430. static void igb_reset_interrupt_capability(struct igb_adapter *adapter)
  431. {
  432. if (adapter->msix_entries) {
  433. pci_disable_msix(adapter->pdev);
  434. kfree(adapter->msix_entries);
  435. adapter->msix_entries = NULL;
  436. } else if (adapter->flags & IGB_FLAG_HAS_MSI)
  437. pci_disable_msi(adapter->pdev);
  438. return;
  439. }
  440. /**
  441. * igb_set_interrupt_capability - set MSI or MSI-X if supported
  442. *
  443. * Attempt to configure interrupts using the best available
  444. * capabilities of the hardware and kernel.
  445. **/
  446. static void igb_set_interrupt_capability(struct igb_adapter *adapter)
  447. {
  448. int err;
  449. int numvecs, i;
  450. numvecs = adapter->num_tx_queues + adapter->num_rx_queues + 1;
  451. adapter->msix_entries = kcalloc(numvecs, sizeof(struct msix_entry),
  452. GFP_KERNEL);
  453. if (!adapter->msix_entries)
  454. goto msi_only;
  455. for (i = 0; i < numvecs; i++)
  456. adapter->msix_entries[i].entry = i;
  457. err = pci_enable_msix(adapter->pdev,
  458. adapter->msix_entries,
  459. numvecs);
  460. if (err == 0)
  461. return;
  462. igb_reset_interrupt_capability(adapter);
  463. /* If we can't do MSI-X, try MSI */
  464. msi_only:
  465. adapter->num_rx_queues = 1;
  466. adapter->num_tx_queues = 1;
  467. if (!pci_enable_msi(adapter->pdev))
  468. adapter->flags |= IGB_FLAG_HAS_MSI;
  469. /* Notify the stack of the (possibly) reduced Tx Queue count. */
  470. adapter->netdev->real_num_tx_queues = adapter->num_tx_queues;
  471. return;
  472. }
  473. /**
  474. * igb_request_irq - initialize interrupts
  475. *
  476. * Attempts to configure interrupts using the best available
  477. * capabilities of the hardware and kernel.
  478. **/
  479. static int igb_request_irq(struct igb_adapter *adapter)
  480. {
  481. struct net_device *netdev = adapter->netdev;
  482. struct e1000_hw *hw = &adapter->hw;
  483. int err = 0;
  484. if (adapter->msix_entries) {
  485. err = igb_request_msix(adapter);
  486. if (!err)
  487. goto request_done;
  488. /* fall back to MSI */
  489. igb_reset_interrupt_capability(adapter);
  490. if (!pci_enable_msi(adapter->pdev))
  491. adapter->flags |= IGB_FLAG_HAS_MSI;
  492. igb_free_all_tx_resources(adapter);
  493. igb_free_all_rx_resources(adapter);
  494. adapter->num_rx_queues = 1;
  495. igb_alloc_queues(adapter);
  496. } else {
  497. switch (hw->mac.type) {
  498. case e1000_82575:
  499. wr32(E1000_MSIXBM(0),
  500. (E1000_EICR_RX_QUEUE0 | E1000_EIMS_OTHER));
  501. break;
  502. case e1000_82576:
  503. wr32(E1000_IVAR0, E1000_IVAR_VALID);
  504. break;
  505. default:
  506. break;
  507. }
  508. }
  509. if (adapter->flags & IGB_FLAG_HAS_MSI) {
  510. err = request_irq(adapter->pdev->irq, &igb_intr_msi, 0,
  511. netdev->name, netdev);
  512. if (!err)
  513. goto request_done;
  514. /* fall back to legacy interrupts */
  515. igb_reset_interrupt_capability(adapter);
  516. adapter->flags &= ~IGB_FLAG_HAS_MSI;
  517. }
  518. err = request_irq(adapter->pdev->irq, &igb_intr, IRQF_SHARED,
  519. netdev->name, netdev);
  520. if (err)
  521. dev_err(&adapter->pdev->dev, "Error %d getting interrupt\n",
  522. err);
  523. request_done:
  524. return err;
  525. }
  526. static void igb_free_irq(struct igb_adapter *adapter)
  527. {
  528. struct net_device *netdev = adapter->netdev;
  529. if (adapter->msix_entries) {
  530. int vector = 0, i;
  531. for (i = 0; i < adapter->num_tx_queues; i++)
  532. free_irq(adapter->msix_entries[vector++].vector,
  533. &(adapter->tx_ring[i]));
  534. for (i = 0; i < adapter->num_rx_queues; i++)
  535. free_irq(adapter->msix_entries[vector++].vector,
  536. &(adapter->rx_ring[i]));
  537. free_irq(adapter->msix_entries[vector++].vector, netdev);
  538. return;
  539. }
  540. free_irq(adapter->pdev->irq, netdev);
  541. }
  542. /**
  543. * igb_irq_disable - Mask off interrupt generation on the NIC
  544. * @adapter: board private structure
  545. **/
  546. static void igb_irq_disable(struct igb_adapter *adapter)
  547. {
  548. struct e1000_hw *hw = &adapter->hw;
  549. if (adapter->msix_entries) {
  550. wr32(E1000_EIAM, 0);
  551. wr32(E1000_EIMC, ~0);
  552. wr32(E1000_EIAC, 0);
  553. }
  554. wr32(E1000_IAM, 0);
  555. wr32(E1000_IMC, ~0);
  556. wrfl();
  557. synchronize_irq(adapter->pdev->irq);
  558. }
  559. /**
  560. * igb_irq_enable - Enable default interrupt generation settings
  561. * @adapter: board private structure
  562. **/
  563. static void igb_irq_enable(struct igb_adapter *adapter)
  564. {
  565. struct e1000_hw *hw = &adapter->hw;
  566. if (adapter->msix_entries) {
  567. wr32(E1000_EIAC, adapter->eims_enable_mask);
  568. wr32(E1000_EIAM, adapter->eims_enable_mask);
  569. wr32(E1000_EIMS, adapter->eims_enable_mask);
  570. wr32(E1000_IMS, E1000_IMS_LSC);
  571. } else {
  572. wr32(E1000_IMS, IMS_ENABLE_MASK);
  573. wr32(E1000_IAM, IMS_ENABLE_MASK);
  574. }
  575. }
  576. static void igb_update_mng_vlan(struct igb_adapter *adapter)
  577. {
  578. struct net_device *netdev = adapter->netdev;
  579. u16 vid = adapter->hw.mng_cookie.vlan_id;
  580. u16 old_vid = adapter->mng_vlan_id;
  581. if (adapter->vlgrp) {
  582. if (!vlan_group_get_device(adapter->vlgrp, vid)) {
  583. if (adapter->hw.mng_cookie.status &
  584. E1000_MNG_DHCP_COOKIE_STATUS_VLAN) {
  585. igb_vlan_rx_add_vid(netdev, vid);
  586. adapter->mng_vlan_id = vid;
  587. } else
  588. adapter->mng_vlan_id = IGB_MNG_VLAN_NONE;
  589. if ((old_vid != (u16)IGB_MNG_VLAN_NONE) &&
  590. (vid != old_vid) &&
  591. !vlan_group_get_device(adapter->vlgrp, old_vid))
  592. igb_vlan_rx_kill_vid(netdev, old_vid);
  593. } else
  594. adapter->mng_vlan_id = vid;
  595. }
  596. }
  597. /**
  598. * igb_release_hw_control - release control of the h/w to f/w
  599. * @adapter: address of board private structure
  600. *
  601. * igb_release_hw_control resets CTRL_EXT:DRV_LOAD bit.
  602. * For ASF and Pass Through versions of f/w this means that the
  603. * driver is no longer loaded.
  604. *
  605. **/
  606. static void igb_release_hw_control(struct igb_adapter *adapter)
  607. {
  608. struct e1000_hw *hw = &adapter->hw;
  609. u32 ctrl_ext;
  610. /* Let firmware take over control of h/w */
  611. ctrl_ext = rd32(E1000_CTRL_EXT);
  612. wr32(E1000_CTRL_EXT,
  613. ctrl_ext & ~E1000_CTRL_EXT_DRV_LOAD);
  614. }
  615. /**
  616. * igb_get_hw_control - get control of the h/w from f/w
  617. * @adapter: address of board private structure
  618. *
  619. * igb_get_hw_control sets CTRL_EXT:DRV_LOAD bit.
  620. * For ASF and Pass Through versions of f/w this means that
  621. * the driver is loaded.
  622. *
  623. **/
  624. static void igb_get_hw_control(struct igb_adapter *adapter)
  625. {
  626. struct e1000_hw *hw = &adapter->hw;
  627. u32 ctrl_ext;
  628. /* Let firmware know the driver has taken over */
  629. ctrl_ext = rd32(E1000_CTRL_EXT);
  630. wr32(E1000_CTRL_EXT,
  631. ctrl_ext | E1000_CTRL_EXT_DRV_LOAD);
  632. }
  633. /**
  634. * igb_configure - configure the hardware for RX and TX
  635. * @adapter: private board structure
  636. **/
  637. static void igb_configure(struct igb_adapter *adapter)
  638. {
  639. struct net_device *netdev = adapter->netdev;
  640. int i;
  641. igb_get_hw_control(adapter);
  642. igb_set_multi(netdev);
  643. igb_restore_vlan(adapter);
  644. igb_configure_tx(adapter);
  645. igb_setup_rctl(adapter);
  646. igb_configure_rx(adapter);
  647. igb_rx_fifo_flush_82575(&adapter->hw);
  648. /* call IGB_DESC_UNUSED which always leaves
  649. * at least 1 descriptor unused to make sure
  650. * next_to_use != next_to_clean */
  651. for (i = 0; i < adapter->num_rx_queues; i++) {
  652. struct igb_ring *ring = &adapter->rx_ring[i];
  653. igb_alloc_rx_buffers_adv(ring, IGB_DESC_UNUSED(ring));
  654. }
  655. adapter->tx_queue_len = netdev->tx_queue_len;
  656. }
  657. /**
  658. * igb_up - Open the interface and prepare it to handle traffic
  659. * @adapter: board private structure
  660. **/
  661. int igb_up(struct igb_adapter *adapter)
  662. {
  663. struct e1000_hw *hw = &adapter->hw;
  664. int i;
  665. /* hardware has been reset, we need to reload some things */
  666. igb_configure(adapter);
  667. clear_bit(__IGB_DOWN, &adapter->state);
  668. for (i = 0; i < adapter->num_rx_queues; i++)
  669. napi_enable(&adapter->rx_ring[i].napi);
  670. if (adapter->msix_entries)
  671. igb_configure_msix(adapter);
  672. /* Clear any pending interrupts. */
  673. rd32(E1000_ICR);
  674. igb_irq_enable(adapter);
  675. /* Fire a link change interrupt to start the watchdog. */
  676. wr32(E1000_ICS, E1000_ICS_LSC);
  677. return 0;
  678. }
  679. void igb_down(struct igb_adapter *adapter)
  680. {
  681. struct e1000_hw *hw = &adapter->hw;
  682. struct net_device *netdev = adapter->netdev;
  683. u32 tctl, rctl;
  684. int i;
  685. /* signal that we're down so the interrupt handler does not
  686. * reschedule our watchdog timer */
  687. set_bit(__IGB_DOWN, &adapter->state);
  688. /* disable receives in the hardware */
  689. rctl = rd32(E1000_RCTL);
  690. wr32(E1000_RCTL, rctl & ~E1000_RCTL_EN);
  691. /* flush and sleep below */
  692. netif_tx_stop_all_queues(netdev);
  693. /* disable transmits in the hardware */
  694. tctl = rd32(E1000_TCTL);
  695. tctl &= ~E1000_TCTL_EN;
  696. wr32(E1000_TCTL, tctl);
  697. /* flush both disables and wait for them to finish */
  698. wrfl();
  699. msleep(10);
  700. for (i = 0; i < adapter->num_rx_queues; i++)
  701. napi_disable(&adapter->rx_ring[i].napi);
  702. igb_irq_disable(adapter);
  703. del_timer_sync(&adapter->watchdog_timer);
  704. del_timer_sync(&adapter->phy_info_timer);
  705. netdev->tx_queue_len = adapter->tx_queue_len;
  706. netif_carrier_off(netdev);
  707. adapter->link_speed = 0;
  708. adapter->link_duplex = 0;
  709. if (!pci_channel_offline(adapter->pdev))
  710. igb_reset(adapter);
  711. igb_clean_all_tx_rings(adapter);
  712. igb_clean_all_rx_rings(adapter);
  713. }
  714. void igb_reinit_locked(struct igb_adapter *adapter)
  715. {
  716. WARN_ON(in_interrupt());
  717. while (test_and_set_bit(__IGB_RESETTING, &adapter->state))
  718. msleep(1);
  719. igb_down(adapter);
  720. igb_up(adapter);
  721. clear_bit(__IGB_RESETTING, &adapter->state);
  722. }
  723. void igb_reset(struct igb_adapter *adapter)
  724. {
  725. struct e1000_hw *hw = &adapter->hw;
  726. struct e1000_mac_info *mac = &hw->mac;
  727. struct e1000_fc_info *fc = &hw->fc;
  728. u32 pba = 0, tx_space, min_tx_space, min_rx_space;
  729. u16 hwm;
  730. /* Repartition Pba for greater than 9k mtu
  731. * To take effect CTRL.RST is required.
  732. */
  733. if (mac->type != e1000_82576) {
  734. pba = E1000_PBA_34K;
  735. }
  736. else {
  737. pba = E1000_PBA_64K;
  738. }
  739. if ((adapter->max_frame_size > ETH_FRAME_LEN + ETH_FCS_LEN) &&
  740. (mac->type < e1000_82576)) {
  741. /* adjust PBA for jumbo frames */
  742. wr32(E1000_PBA, pba);
  743. /* To maintain wire speed transmits, the Tx FIFO should be
  744. * large enough to accommodate two full transmit packets,
  745. * rounded up to the next 1KB and expressed in KB. Likewise,
  746. * the Rx FIFO should be large enough to accommodate at least
  747. * one full receive packet and is similarly rounded up and
  748. * expressed in KB. */
  749. pba = rd32(E1000_PBA);
  750. /* upper 16 bits has Tx packet buffer allocation size in KB */
  751. tx_space = pba >> 16;
  752. /* lower 16 bits has Rx packet buffer allocation size in KB */
  753. pba &= 0xffff;
  754. /* the tx fifo also stores 16 bytes of information about the tx
  755. * but don't include ethernet FCS because hardware appends it */
  756. min_tx_space = (adapter->max_frame_size +
  757. sizeof(struct e1000_tx_desc) -
  758. ETH_FCS_LEN) * 2;
  759. min_tx_space = ALIGN(min_tx_space, 1024);
  760. min_tx_space >>= 10;
  761. /* software strips receive CRC, so leave room for it */
  762. min_rx_space = adapter->max_frame_size;
  763. min_rx_space = ALIGN(min_rx_space, 1024);
  764. min_rx_space >>= 10;
  765. /* If current Tx allocation is less than the min Tx FIFO size,
  766. * and the min Tx FIFO size is less than the current Rx FIFO
  767. * allocation, take space away from current Rx allocation */
  768. if (tx_space < min_tx_space &&
  769. ((min_tx_space - tx_space) < pba)) {
  770. pba = pba - (min_tx_space - tx_space);
  771. /* if short on rx space, rx wins and must trump tx
  772. * adjustment */
  773. if (pba < min_rx_space)
  774. pba = min_rx_space;
  775. }
  776. wr32(E1000_PBA, pba);
  777. }
  778. /* flow control settings */
  779. /* The high water mark must be low enough to fit one full frame
  780. * (or the size used for early receive) above it in the Rx FIFO.
  781. * Set it to the lower of:
  782. * - 90% of the Rx FIFO size, or
  783. * - the full Rx FIFO size minus one full frame */
  784. hwm = min(((pba << 10) * 9 / 10),
  785. ((pba << 10) - 2 * adapter->max_frame_size));
  786. if (mac->type < e1000_82576) {
  787. fc->high_water = hwm & 0xFFF8; /* 8-byte granularity */
  788. fc->low_water = fc->high_water - 8;
  789. } else {
  790. fc->high_water = hwm & 0xFFF0; /* 16-byte granularity */
  791. fc->low_water = fc->high_water - 16;
  792. }
  793. fc->pause_time = 0xFFFF;
  794. fc->send_xon = 1;
  795. fc->type = fc->original_type;
  796. /* Allow time for pending master requests to run */
  797. adapter->hw.mac.ops.reset_hw(&adapter->hw);
  798. wr32(E1000_WUC, 0);
  799. if (adapter->hw.mac.ops.init_hw(&adapter->hw))
  800. dev_err(&adapter->pdev->dev, "Hardware Error\n");
  801. igb_update_mng_vlan(adapter);
  802. /* Enable h/w to recognize an 802.1Q VLAN Ethernet packet */
  803. wr32(E1000_VET, ETHERNET_IEEE_VLAN_TYPE);
  804. igb_reset_adaptive(&adapter->hw);
  805. if (adapter->hw.phy.ops.get_phy_info)
  806. adapter->hw.phy.ops.get_phy_info(&adapter->hw);
  807. }
  808. /**
  809. * igb_is_need_ioport - determine if an adapter needs ioport resources or not
  810. * @pdev: PCI device information struct
  811. *
  812. * Returns true if an adapter needs ioport resources
  813. **/
  814. static int igb_is_need_ioport(struct pci_dev *pdev)
  815. {
  816. switch (pdev->device) {
  817. /* Currently there are no adapters that need ioport resources */
  818. default:
  819. return false;
  820. }
  821. }
  822. /**
  823. * igb_probe - Device Initialization Routine
  824. * @pdev: PCI device information struct
  825. * @ent: entry in igb_pci_tbl
  826. *
  827. * Returns 0 on success, negative on failure
  828. *
  829. * igb_probe initializes an adapter identified by a pci_dev structure.
  830. * The OS initialization, configuring of the adapter private structure,
  831. * and a hardware reset occur.
  832. **/
  833. static int __devinit igb_probe(struct pci_dev *pdev,
  834. const struct pci_device_id *ent)
  835. {
  836. struct net_device *netdev;
  837. struct igb_adapter *adapter;
  838. struct e1000_hw *hw;
  839. const struct e1000_info *ei = igb_info_tbl[ent->driver_data];
  840. unsigned long mmio_start, mmio_len;
  841. int i, err, pci_using_dac;
  842. u16 eeprom_data = 0;
  843. u16 eeprom_apme_mask = IGB_EEPROM_APME;
  844. u32 part_num;
  845. int bars, need_ioport;
  846. /* do not allocate ioport bars when not needed */
  847. need_ioport = igb_is_need_ioport(pdev);
  848. if (need_ioport) {
  849. bars = pci_select_bars(pdev, IORESOURCE_MEM | IORESOURCE_IO);
  850. err = pci_enable_device(pdev);
  851. } else {
  852. bars = pci_select_bars(pdev, IORESOURCE_MEM);
  853. err = pci_enable_device_mem(pdev);
  854. }
  855. if (err)
  856. return err;
  857. pci_using_dac = 0;
  858. err = pci_set_dma_mask(pdev, DMA_64BIT_MASK);
  859. if (!err) {
  860. err = pci_set_consistent_dma_mask(pdev, DMA_64BIT_MASK);
  861. if (!err)
  862. pci_using_dac = 1;
  863. } else {
  864. err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
  865. if (err) {
  866. err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
  867. if (err) {
  868. dev_err(&pdev->dev, "No usable DMA "
  869. "configuration, aborting\n");
  870. goto err_dma;
  871. }
  872. }
  873. }
  874. err = pci_request_selected_regions(pdev, bars, igb_driver_name);
  875. if (err)
  876. goto err_pci_reg;
  877. pci_set_master(pdev);
  878. pci_save_state(pdev);
  879. err = -ENOMEM;
  880. netdev = alloc_etherdev_mq(sizeof(struct igb_adapter), IGB_MAX_TX_QUEUES);
  881. if (!netdev)
  882. goto err_alloc_etherdev;
  883. SET_NETDEV_DEV(netdev, &pdev->dev);
  884. pci_set_drvdata(pdev, netdev);
  885. adapter = netdev_priv(netdev);
  886. adapter->netdev = netdev;
  887. adapter->pdev = pdev;
  888. hw = &adapter->hw;
  889. hw->back = adapter;
  890. adapter->msg_enable = NETIF_MSG_DRV | NETIF_MSG_PROBE;
  891. adapter->bars = bars;
  892. adapter->need_ioport = need_ioport;
  893. mmio_start = pci_resource_start(pdev, 0);
  894. mmio_len = pci_resource_len(pdev, 0);
  895. err = -EIO;
  896. adapter->hw.hw_addr = ioremap(mmio_start, mmio_len);
  897. if (!adapter->hw.hw_addr)
  898. goto err_ioremap;
  899. netdev->open = &igb_open;
  900. netdev->stop = &igb_close;
  901. netdev->get_stats = &igb_get_stats;
  902. netdev->set_multicast_list = &igb_set_multi;
  903. netdev->set_mac_address = &igb_set_mac;
  904. netdev->change_mtu = &igb_change_mtu;
  905. netdev->do_ioctl = &igb_ioctl;
  906. igb_set_ethtool_ops(netdev);
  907. netdev->tx_timeout = &igb_tx_timeout;
  908. netdev->watchdog_timeo = 5 * HZ;
  909. netdev->vlan_rx_register = igb_vlan_rx_register;
  910. netdev->vlan_rx_add_vid = igb_vlan_rx_add_vid;
  911. netdev->vlan_rx_kill_vid = igb_vlan_rx_kill_vid;
  912. #ifdef CONFIG_NET_POLL_CONTROLLER
  913. netdev->poll_controller = igb_netpoll;
  914. #endif
  915. netdev->hard_start_xmit = &igb_xmit_frame_adv;
  916. strncpy(netdev->name, pci_name(pdev), sizeof(netdev->name) - 1);
  917. netdev->mem_start = mmio_start;
  918. netdev->mem_end = mmio_start + mmio_len;
  919. /* PCI config space info */
  920. hw->vendor_id = pdev->vendor;
  921. hw->device_id = pdev->device;
  922. hw->revision_id = pdev->revision;
  923. hw->subsystem_vendor_id = pdev->subsystem_vendor;
  924. hw->subsystem_device_id = pdev->subsystem_device;
  925. /* setup the private structure */
  926. hw->back = adapter;
  927. /* Copy the default MAC, PHY and NVM function pointers */
  928. memcpy(&hw->mac.ops, ei->mac_ops, sizeof(hw->mac.ops));
  929. memcpy(&hw->phy.ops, ei->phy_ops, sizeof(hw->phy.ops));
  930. memcpy(&hw->nvm.ops, ei->nvm_ops, sizeof(hw->nvm.ops));
  931. /* Initialize skew-specific constants */
  932. err = ei->get_invariants(hw);
  933. if (err)
  934. goto err_hw_init;
  935. err = igb_sw_init(adapter);
  936. if (err)
  937. goto err_sw_init;
  938. igb_get_bus_info_pcie(hw);
  939. /* set flags */
  940. switch (hw->mac.type) {
  941. case e1000_82576:
  942. case e1000_82575:
  943. adapter->flags |= IGB_FLAG_HAS_DCA;
  944. adapter->flags |= IGB_FLAG_NEED_CTX_IDX;
  945. break;
  946. default:
  947. break;
  948. }
  949. hw->phy.autoneg_wait_to_complete = false;
  950. hw->mac.adaptive_ifs = true;
  951. /* Copper options */
  952. if (hw->phy.media_type == e1000_media_type_copper) {
  953. hw->phy.mdix = AUTO_ALL_MODES;
  954. hw->phy.disable_polarity_correction = false;
  955. hw->phy.ms_type = e1000_ms_hw_default;
  956. }
  957. if (igb_check_reset_block(hw))
  958. dev_info(&pdev->dev,
  959. "PHY reset is blocked due to SOL/IDER session.\n");
  960. netdev->features = NETIF_F_SG |
  961. NETIF_F_HW_CSUM |
  962. NETIF_F_HW_VLAN_TX |
  963. NETIF_F_HW_VLAN_RX |
  964. NETIF_F_HW_VLAN_FILTER;
  965. netdev->features |= NETIF_F_TSO;
  966. netdev->features |= NETIF_F_TSO6;
  967. #ifdef CONFIG_IGB_LRO
  968. netdev->features |= NETIF_F_LRO;
  969. #endif
  970. netdev->vlan_features |= NETIF_F_TSO;
  971. netdev->vlan_features |= NETIF_F_TSO6;
  972. netdev->vlan_features |= NETIF_F_HW_CSUM;
  973. netdev->vlan_features |= NETIF_F_SG;
  974. if (pci_using_dac)
  975. netdev->features |= NETIF_F_HIGHDMA;
  976. netdev->features |= NETIF_F_LLTX;
  977. adapter->en_mng_pt = igb_enable_mng_pass_thru(&adapter->hw);
  978. /* before reading the NVM, reset the controller to put the device in a
  979. * known good starting state */
  980. hw->mac.ops.reset_hw(hw);
  981. /* make sure the NVM is good */
  982. if (igb_validate_nvm_checksum(hw) < 0) {
  983. dev_err(&pdev->dev, "The NVM Checksum Is Not Valid\n");
  984. err = -EIO;
  985. goto err_eeprom;
  986. }
  987. /* copy the MAC address out of the NVM */
  988. if (hw->mac.ops.read_mac_addr(hw))
  989. dev_err(&pdev->dev, "NVM Read Error\n");
  990. memcpy(netdev->dev_addr, hw->mac.addr, netdev->addr_len);
  991. memcpy(netdev->perm_addr, hw->mac.addr, netdev->addr_len);
  992. if (!is_valid_ether_addr(netdev->perm_addr)) {
  993. dev_err(&pdev->dev, "Invalid MAC Address\n");
  994. err = -EIO;
  995. goto err_eeprom;
  996. }
  997. init_timer(&adapter->watchdog_timer);
  998. adapter->watchdog_timer.function = &igb_watchdog;
  999. adapter->watchdog_timer.data = (unsigned long) adapter;
  1000. init_timer(&adapter->phy_info_timer);
  1001. adapter->phy_info_timer.function = &igb_update_phy_info;
  1002. adapter->phy_info_timer.data = (unsigned long) adapter;
  1003. INIT_WORK(&adapter->reset_task, igb_reset_task);
  1004. INIT_WORK(&adapter->watchdog_task, igb_watchdog_task);
  1005. /* Initialize link & ring properties that are user-changeable */
  1006. adapter->tx_ring->count = 256;
  1007. for (i = 0; i < adapter->num_tx_queues; i++)
  1008. adapter->tx_ring[i].count = adapter->tx_ring->count;
  1009. adapter->rx_ring->count = 256;
  1010. for (i = 0; i < adapter->num_rx_queues; i++)
  1011. adapter->rx_ring[i].count = adapter->rx_ring->count;
  1012. adapter->fc_autoneg = true;
  1013. hw->mac.autoneg = true;
  1014. hw->phy.autoneg_advertised = 0x2f;
  1015. hw->fc.original_type = e1000_fc_default;
  1016. hw->fc.type = e1000_fc_default;
  1017. adapter->itr_setting = 3;
  1018. adapter->itr = IGB_START_ITR;
  1019. igb_validate_mdi_setting(hw);
  1020. adapter->rx_csum = 1;
  1021. /* Initial Wake on LAN setting If APM wake is enabled in the EEPROM,
  1022. * enable the ACPI Magic Packet filter
  1023. */
  1024. if (hw->bus.func == 0 ||
  1025. hw->device_id == E1000_DEV_ID_82575EB_COPPER)
  1026. hw->nvm.ops.read_nvm(hw, NVM_INIT_CONTROL3_PORT_A, 1,
  1027. &eeprom_data);
  1028. if (eeprom_data & eeprom_apme_mask)
  1029. adapter->eeprom_wol |= E1000_WUFC_MAG;
  1030. /* now that we have the eeprom settings, apply the special cases where
  1031. * the eeprom may be wrong or the board simply won't support wake on
  1032. * lan on a particular port */
  1033. switch (pdev->device) {
  1034. case E1000_DEV_ID_82575GB_QUAD_COPPER:
  1035. adapter->eeprom_wol = 0;
  1036. break;
  1037. case E1000_DEV_ID_82575EB_FIBER_SERDES:
  1038. case E1000_DEV_ID_82576_FIBER:
  1039. case E1000_DEV_ID_82576_SERDES:
  1040. /* Wake events only supported on port A for dual fiber
  1041. * regardless of eeprom setting */
  1042. if (rd32(E1000_STATUS) & E1000_STATUS_FUNC_1)
  1043. adapter->eeprom_wol = 0;
  1044. break;
  1045. case E1000_DEV_ID_82576_QUAD_COPPER:
  1046. /* if quad port adapter, disable WoL on all but port A */
  1047. if (global_quad_port_a != 0)
  1048. adapter->eeprom_wol = 0;
  1049. else
  1050. adapter->flags |= IGB_FLAG_QUAD_PORT_A;
  1051. /* Reset for multiple quad port adapters */
  1052. if (++global_quad_port_a == 4)
  1053. global_quad_port_a = 0;
  1054. break;
  1055. }
  1056. /* initialize the wol settings based on the eeprom settings */
  1057. adapter->wol = adapter->eeprom_wol;
  1058. /* reset the hardware with the new settings */
  1059. igb_reset(adapter);
  1060. /* let the f/w know that the h/w is now under the control of the
  1061. * driver. */
  1062. igb_get_hw_control(adapter);
  1063. /* tell the stack to leave us alone until igb_open() is called */
  1064. netif_carrier_off(netdev);
  1065. netif_tx_stop_all_queues(netdev);
  1066. strcpy(netdev->name, "eth%d");
  1067. err = register_netdev(netdev);
  1068. if (err)
  1069. goto err_register;
  1070. #ifdef CONFIG_DCA
  1071. if ((adapter->flags & IGB_FLAG_HAS_DCA) &&
  1072. (dca_add_requester(&pdev->dev) == 0)) {
  1073. adapter->flags |= IGB_FLAG_DCA_ENABLED;
  1074. dev_info(&pdev->dev, "DCA enabled\n");
  1075. /* Always use CB2 mode, difference is masked
  1076. * in the CB driver. */
  1077. wr32(E1000_DCA_CTRL, 2);
  1078. igb_setup_dca(adapter);
  1079. }
  1080. #endif
  1081. dev_info(&pdev->dev, "Intel(R) Gigabit Ethernet Network Connection\n");
  1082. /* print bus type/speed/width info */
  1083. dev_info(&pdev->dev,
  1084. "%s: (PCIe:%s:%s) %02x:%02x:%02x:%02x:%02x:%02x\n",
  1085. netdev->name,
  1086. ((hw->bus.speed == e1000_bus_speed_2500)
  1087. ? "2.5Gb/s" : "unknown"),
  1088. ((hw->bus.width == e1000_bus_width_pcie_x4)
  1089. ? "Width x4" : (hw->bus.width == e1000_bus_width_pcie_x1)
  1090. ? "Width x1" : "unknown"),
  1091. netdev->dev_addr[0], netdev->dev_addr[1], netdev->dev_addr[2],
  1092. netdev->dev_addr[3], netdev->dev_addr[4], netdev->dev_addr[5]);
  1093. igb_read_part_num(hw, &part_num);
  1094. dev_info(&pdev->dev, "%s: PBA No: %06x-%03x\n", netdev->name,
  1095. (part_num >> 8), (part_num & 0xff));
  1096. dev_info(&pdev->dev,
  1097. "Using %s interrupts. %d rx queue(s), %d tx queue(s)\n",
  1098. adapter->msix_entries ? "MSI-X" :
  1099. (adapter->flags & IGB_FLAG_HAS_MSI) ? "MSI" : "legacy",
  1100. adapter->num_rx_queues, adapter->num_tx_queues);
  1101. return 0;
  1102. err_register:
  1103. igb_release_hw_control(adapter);
  1104. err_eeprom:
  1105. if (!igb_check_reset_block(hw))
  1106. hw->phy.ops.reset_phy(hw);
  1107. if (hw->flash_address)
  1108. iounmap(hw->flash_address);
  1109. igb_remove_device(hw);
  1110. igb_free_queues(adapter);
  1111. err_sw_init:
  1112. err_hw_init:
  1113. iounmap(hw->hw_addr);
  1114. err_ioremap:
  1115. free_netdev(netdev);
  1116. err_alloc_etherdev:
  1117. pci_release_selected_regions(pdev, bars);
  1118. err_pci_reg:
  1119. err_dma:
  1120. pci_disable_device(pdev);
  1121. return err;
  1122. }
  1123. /**
  1124. * igb_remove - Device Removal Routine
  1125. * @pdev: PCI device information struct
  1126. *
  1127. * igb_remove is called by the PCI subsystem to alert the driver
  1128. * that it should release a PCI device. The could be caused by a
  1129. * Hot-Plug event, or because the driver is going to be removed from
  1130. * memory.
  1131. **/
  1132. static void __devexit igb_remove(struct pci_dev *pdev)
  1133. {
  1134. struct net_device *netdev = pci_get_drvdata(pdev);
  1135. struct igb_adapter *adapter = netdev_priv(netdev);
  1136. #ifdef CONFIG_DCA
  1137. struct e1000_hw *hw = &adapter->hw;
  1138. #endif
  1139. /* flush_scheduled work may reschedule our watchdog task, so
  1140. * explicitly disable watchdog tasks from being rescheduled */
  1141. set_bit(__IGB_DOWN, &adapter->state);
  1142. del_timer_sync(&adapter->watchdog_timer);
  1143. del_timer_sync(&adapter->phy_info_timer);
  1144. flush_scheduled_work();
  1145. #ifdef CONFIG_DCA
  1146. if (adapter->flags & IGB_FLAG_DCA_ENABLED) {
  1147. dev_info(&pdev->dev, "DCA disabled\n");
  1148. dca_remove_requester(&pdev->dev);
  1149. adapter->flags &= ~IGB_FLAG_DCA_ENABLED;
  1150. wr32(E1000_DCA_CTRL, 1);
  1151. }
  1152. #endif
  1153. /* Release control of h/w to f/w. If f/w is AMT enabled, this
  1154. * would have already happened in close and is redundant. */
  1155. igb_release_hw_control(adapter);
  1156. unregister_netdev(netdev);
  1157. if (adapter->hw.phy.ops.reset_phy &&
  1158. !igb_check_reset_block(&adapter->hw))
  1159. adapter->hw.phy.ops.reset_phy(&adapter->hw);
  1160. igb_remove_device(&adapter->hw);
  1161. igb_reset_interrupt_capability(adapter);
  1162. igb_free_queues(adapter);
  1163. iounmap(adapter->hw.hw_addr);
  1164. if (adapter->hw.flash_address)
  1165. iounmap(adapter->hw.flash_address);
  1166. pci_release_selected_regions(pdev, adapter->bars);
  1167. free_netdev(netdev);
  1168. pci_disable_device(pdev);
  1169. }
  1170. /**
  1171. * igb_sw_init - Initialize general software structures (struct igb_adapter)
  1172. * @adapter: board private structure to initialize
  1173. *
  1174. * igb_sw_init initializes the Adapter private data structure.
  1175. * Fields are initialized based on PCI device information and
  1176. * OS network device settings (MTU size).
  1177. **/
  1178. static int __devinit igb_sw_init(struct igb_adapter *adapter)
  1179. {
  1180. struct e1000_hw *hw = &adapter->hw;
  1181. struct net_device *netdev = adapter->netdev;
  1182. struct pci_dev *pdev = adapter->pdev;
  1183. pci_read_config_word(pdev, PCI_COMMAND, &hw->bus.pci_cmd_word);
  1184. adapter->rx_buffer_len = MAXIMUM_ETHERNET_VLAN_SIZE;
  1185. adapter->rx_ps_hdr_size = 0; /* disable packet split */
  1186. adapter->max_frame_size = netdev->mtu + ETH_HLEN + ETH_FCS_LEN;
  1187. adapter->min_frame_size = ETH_ZLEN + ETH_FCS_LEN;
  1188. /* Number of supported queues. */
  1189. /* Having more queues than CPUs doesn't make sense. */
  1190. adapter->num_rx_queues = min((u32)IGB_MAX_RX_QUEUES, (u32)num_online_cpus());
  1191. adapter->num_tx_queues = min(IGB_MAX_TX_QUEUES, num_online_cpus());
  1192. /* This call may decrease the number of queues depending on
  1193. * interrupt mode. */
  1194. igb_set_interrupt_capability(adapter);
  1195. if (igb_alloc_queues(adapter)) {
  1196. dev_err(&pdev->dev, "Unable to allocate memory for queues\n");
  1197. return -ENOMEM;
  1198. }
  1199. /* Explicitly disable IRQ since the NIC can be in any state. */
  1200. igb_irq_disable(adapter);
  1201. set_bit(__IGB_DOWN, &adapter->state);
  1202. return 0;
  1203. }
  1204. /**
  1205. * igb_open - Called when a network interface is made active
  1206. * @netdev: network interface device structure
  1207. *
  1208. * Returns 0 on success, negative value on failure
  1209. *
  1210. * The open entry point is called when a network interface is made
  1211. * active by the system (IFF_UP). At this point all resources needed
  1212. * for transmit and receive operations are allocated, the interrupt
  1213. * handler is registered with the OS, the watchdog timer is started,
  1214. * and the stack is notified that the interface is ready.
  1215. **/
  1216. static int igb_open(struct net_device *netdev)
  1217. {
  1218. struct igb_adapter *adapter = netdev_priv(netdev);
  1219. struct e1000_hw *hw = &adapter->hw;
  1220. int err;
  1221. int i;
  1222. /* disallow open during test */
  1223. if (test_bit(__IGB_TESTING, &adapter->state))
  1224. return -EBUSY;
  1225. /* allocate transmit descriptors */
  1226. err = igb_setup_all_tx_resources(adapter);
  1227. if (err)
  1228. goto err_setup_tx;
  1229. /* allocate receive descriptors */
  1230. err = igb_setup_all_rx_resources(adapter);
  1231. if (err)
  1232. goto err_setup_rx;
  1233. /* e1000_power_up_phy(adapter); */
  1234. adapter->mng_vlan_id = IGB_MNG_VLAN_NONE;
  1235. if ((adapter->hw.mng_cookie.status &
  1236. E1000_MNG_DHCP_COOKIE_STATUS_VLAN))
  1237. igb_update_mng_vlan(adapter);
  1238. /* before we allocate an interrupt, we must be ready to handle it.
  1239. * Setting DEBUG_SHIRQ in the kernel makes it fire an interrupt
  1240. * as soon as we call pci_request_irq, so we have to setup our
  1241. * clean_rx handler before we do so. */
  1242. igb_configure(adapter);
  1243. err = igb_request_irq(adapter);
  1244. if (err)
  1245. goto err_req_irq;
  1246. /* From here on the code is the same as igb_up() */
  1247. clear_bit(__IGB_DOWN, &adapter->state);
  1248. for (i = 0; i < adapter->num_rx_queues; i++)
  1249. napi_enable(&adapter->rx_ring[i].napi);
  1250. /* Clear any pending interrupts. */
  1251. rd32(E1000_ICR);
  1252. igb_irq_enable(adapter);
  1253. netif_tx_start_all_queues(netdev);
  1254. /* Fire a link status change interrupt to start the watchdog. */
  1255. wr32(E1000_ICS, E1000_ICS_LSC);
  1256. return 0;
  1257. err_req_irq:
  1258. igb_release_hw_control(adapter);
  1259. /* e1000_power_down_phy(adapter); */
  1260. igb_free_all_rx_resources(adapter);
  1261. err_setup_rx:
  1262. igb_free_all_tx_resources(adapter);
  1263. err_setup_tx:
  1264. igb_reset(adapter);
  1265. return err;
  1266. }
  1267. /**
  1268. * igb_close - Disables a network interface
  1269. * @netdev: network interface device structure
  1270. *
  1271. * Returns 0, this is not allowed to fail
  1272. *
  1273. * The close entry point is called when an interface is de-activated
  1274. * by the OS. The hardware is still under the driver's control, but
  1275. * needs to be disabled. A global MAC reset is issued to stop the
  1276. * hardware, and all transmit and receive resources are freed.
  1277. **/
  1278. static int igb_close(struct net_device *netdev)
  1279. {
  1280. struct igb_adapter *adapter = netdev_priv(netdev);
  1281. WARN_ON(test_bit(__IGB_RESETTING, &adapter->state));
  1282. igb_down(adapter);
  1283. igb_free_irq(adapter);
  1284. igb_free_all_tx_resources(adapter);
  1285. igb_free_all_rx_resources(adapter);
  1286. /* kill manageability vlan ID if supported, but not if a vlan with
  1287. * the same ID is registered on the host OS (let 8021q kill it) */
  1288. if ((adapter->hw.mng_cookie.status &
  1289. E1000_MNG_DHCP_COOKIE_STATUS_VLAN) &&
  1290. !(adapter->vlgrp &&
  1291. vlan_group_get_device(adapter->vlgrp, adapter->mng_vlan_id)))
  1292. igb_vlan_rx_kill_vid(netdev, adapter->mng_vlan_id);
  1293. return 0;
  1294. }
  1295. /**
  1296. * igb_setup_tx_resources - allocate Tx resources (Descriptors)
  1297. * @adapter: board private structure
  1298. * @tx_ring: tx descriptor ring (for a specific queue) to setup
  1299. *
  1300. * Return 0 on success, negative on failure
  1301. **/
  1302. int igb_setup_tx_resources(struct igb_adapter *adapter,
  1303. struct igb_ring *tx_ring)
  1304. {
  1305. struct pci_dev *pdev = adapter->pdev;
  1306. int size;
  1307. size = sizeof(struct igb_buffer) * tx_ring->count;
  1308. tx_ring->buffer_info = vmalloc(size);
  1309. if (!tx_ring->buffer_info)
  1310. goto err;
  1311. memset(tx_ring->buffer_info, 0, size);
  1312. /* round up to nearest 4K */
  1313. tx_ring->size = tx_ring->count * sizeof(struct e1000_tx_desc)
  1314. + sizeof(u32);
  1315. tx_ring->size = ALIGN(tx_ring->size, 4096);
  1316. tx_ring->desc = pci_alloc_consistent(pdev, tx_ring->size,
  1317. &tx_ring->dma);
  1318. if (!tx_ring->desc)
  1319. goto err;
  1320. tx_ring->adapter = adapter;
  1321. tx_ring->next_to_use = 0;
  1322. tx_ring->next_to_clean = 0;
  1323. return 0;
  1324. err:
  1325. vfree(tx_ring->buffer_info);
  1326. dev_err(&adapter->pdev->dev,
  1327. "Unable to allocate memory for the transmit descriptor ring\n");
  1328. return -ENOMEM;
  1329. }
  1330. /**
  1331. * igb_setup_all_tx_resources - wrapper to allocate Tx resources
  1332. * (Descriptors) for all queues
  1333. * @adapter: board private structure
  1334. *
  1335. * Return 0 on success, negative on failure
  1336. **/
  1337. static int igb_setup_all_tx_resources(struct igb_adapter *adapter)
  1338. {
  1339. int i, err = 0;
  1340. int r_idx;
  1341. for (i = 0; i < adapter->num_tx_queues; i++) {
  1342. err = igb_setup_tx_resources(adapter, &adapter->tx_ring[i]);
  1343. if (err) {
  1344. dev_err(&adapter->pdev->dev,
  1345. "Allocation for Tx Queue %u failed\n", i);
  1346. for (i--; i >= 0; i--)
  1347. igb_free_tx_resources(&adapter->tx_ring[i]);
  1348. break;
  1349. }
  1350. }
  1351. for (i = 0; i < IGB_MAX_TX_QUEUES; i++) {
  1352. r_idx = i % adapter->num_tx_queues;
  1353. adapter->multi_tx_table[i] = &adapter->tx_ring[r_idx];
  1354. }
  1355. return err;
  1356. }
  1357. /**
  1358. * igb_configure_tx - Configure transmit Unit after Reset
  1359. * @adapter: board private structure
  1360. *
  1361. * Configure the Tx unit of the MAC after a reset.
  1362. **/
  1363. static void igb_configure_tx(struct igb_adapter *adapter)
  1364. {
  1365. u64 tdba, tdwba;
  1366. struct e1000_hw *hw = &adapter->hw;
  1367. u32 tctl;
  1368. u32 txdctl, txctrl;
  1369. int i;
  1370. for (i = 0; i < adapter->num_tx_queues; i++) {
  1371. struct igb_ring *ring = &(adapter->tx_ring[i]);
  1372. wr32(E1000_TDLEN(i),
  1373. ring->count * sizeof(struct e1000_tx_desc));
  1374. tdba = ring->dma;
  1375. wr32(E1000_TDBAL(i),
  1376. tdba & 0x00000000ffffffffULL);
  1377. wr32(E1000_TDBAH(i), tdba >> 32);
  1378. tdwba = ring->dma + ring->count * sizeof(struct e1000_tx_desc);
  1379. tdwba |= 1; /* enable head wb */
  1380. wr32(E1000_TDWBAL(i),
  1381. tdwba & 0x00000000ffffffffULL);
  1382. wr32(E1000_TDWBAH(i), tdwba >> 32);
  1383. ring->head = E1000_TDH(i);
  1384. ring->tail = E1000_TDT(i);
  1385. writel(0, hw->hw_addr + ring->tail);
  1386. writel(0, hw->hw_addr + ring->head);
  1387. txdctl = rd32(E1000_TXDCTL(i));
  1388. txdctl |= E1000_TXDCTL_QUEUE_ENABLE;
  1389. wr32(E1000_TXDCTL(i), txdctl);
  1390. /* Turn off Relaxed Ordering on head write-backs. The
  1391. * writebacks MUST be delivered in order or it will
  1392. * completely screw up our bookeeping.
  1393. */
  1394. txctrl = rd32(E1000_DCA_TXCTRL(i));
  1395. txctrl &= ~E1000_DCA_TXCTRL_TX_WB_RO_EN;
  1396. wr32(E1000_DCA_TXCTRL(i), txctrl);
  1397. }
  1398. /* Use the default values for the Tx Inter Packet Gap (IPG) timer */
  1399. /* Program the Transmit Control Register */
  1400. tctl = rd32(E1000_TCTL);
  1401. tctl &= ~E1000_TCTL_CT;
  1402. tctl |= E1000_TCTL_PSP | E1000_TCTL_RTLC |
  1403. (E1000_COLLISION_THRESHOLD << E1000_CT_SHIFT);
  1404. igb_config_collision_dist(hw);
  1405. /* Setup Transmit Descriptor Settings for eop descriptor */
  1406. adapter->txd_cmd = E1000_TXD_CMD_EOP | E1000_TXD_CMD_RS;
  1407. /* Enable transmits */
  1408. tctl |= E1000_TCTL_EN;
  1409. wr32(E1000_TCTL, tctl);
  1410. }
  1411. /**
  1412. * igb_setup_rx_resources - allocate Rx resources (Descriptors)
  1413. * @adapter: board private structure
  1414. * @rx_ring: rx descriptor ring (for a specific queue) to setup
  1415. *
  1416. * Returns 0 on success, negative on failure
  1417. **/
  1418. int igb_setup_rx_resources(struct igb_adapter *adapter,
  1419. struct igb_ring *rx_ring)
  1420. {
  1421. struct pci_dev *pdev = adapter->pdev;
  1422. int size, desc_len;
  1423. #ifdef CONFIG_IGB_LRO
  1424. size = sizeof(struct net_lro_desc) * MAX_LRO_DESCRIPTORS;
  1425. rx_ring->lro_mgr.lro_arr = vmalloc(size);
  1426. if (!rx_ring->lro_mgr.lro_arr)
  1427. goto err;
  1428. memset(rx_ring->lro_mgr.lro_arr, 0, size);
  1429. #endif
  1430. size = sizeof(struct igb_buffer) * rx_ring->count;
  1431. rx_ring->buffer_info = vmalloc(size);
  1432. if (!rx_ring->buffer_info)
  1433. goto err;
  1434. memset(rx_ring->buffer_info, 0, size);
  1435. desc_len = sizeof(union e1000_adv_rx_desc);
  1436. /* Round up to nearest 4K */
  1437. rx_ring->size = rx_ring->count * desc_len;
  1438. rx_ring->size = ALIGN(rx_ring->size, 4096);
  1439. rx_ring->desc = pci_alloc_consistent(pdev, rx_ring->size,
  1440. &rx_ring->dma);
  1441. if (!rx_ring->desc)
  1442. goto err;
  1443. rx_ring->next_to_clean = 0;
  1444. rx_ring->next_to_use = 0;
  1445. rx_ring->adapter = adapter;
  1446. return 0;
  1447. err:
  1448. #ifdef CONFIG_IGB_LRO
  1449. vfree(rx_ring->lro_mgr.lro_arr);
  1450. rx_ring->lro_mgr.lro_arr = NULL;
  1451. #endif
  1452. vfree(rx_ring->buffer_info);
  1453. dev_err(&adapter->pdev->dev, "Unable to allocate memory for "
  1454. "the receive descriptor ring\n");
  1455. return -ENOMEM;
  1456. }
  1457. /**
  1458. * igb_setup_all_rx_resources - wrapper to allocate Rx resources
  1459. * (Descriptors) for all queues
  1460. * @adapter: board private structure
  1461. *
  1462. * Return 0 on success, negative on failure
  1463. **/
  1464. static int igb_setup_all_rx_resources(struct igb_adapter *adapter)
  1465. {
  1466. int i, err = 0;
  1467. for (i = 0; i < adapter->num_rx_queues; i++) {
  1468. err = igb_setup_rx_resources(adapter, &adapter->rx_ring[i]);
  1469. if (err) {
  1470. dev_err(&adapter->pdev->dev,
  1471. "Allocation for Rx Queue %u failed\n", i);
  1472. for (i--; i >= 0; i--)
  1473. igb_free_rx_resources(&adapter->rx_ring[i]);
  1474. break;
  1475. }
  1476. }
  1477. return err;
  1478. }
  1479. /**
  1480. * igb_setup_rctl - configure the receive control registers
  1481. * @adapter: Board private structure
  1482. **/
  1483. static void igb_setup_rctl(struct igb_adapter *adapter)
  1484. {
  1485. struct e1000_hw *hw = &adapter->hw;
  1486. u32 rctl;
  1487. u32 srrctl = 0;
  1488. int i;
  1489. rctl = rd32(E1000_RCTL);
  1490. rctl &= ~(3 << E1000_RCTL_MO_SHIFT);
  1491. rctl |= E1000_RCTL_EN | E1000_RCTL_BAM |
  1492. E1000_RCTL_LBM_NO | E1000_RCTL_RDMTS_HALF |
  1493. (adapter->hw.mac.mc_filter_type << E1000_RCTL_MO_SHIFT);
  1494. /*
  1495. * enable stripping of CRC. It's unlikely this will break BMC
  1496. * redirection as it did with e1000. Newer features require
  1497. * that the HW strips the CRC.
  1498. */
  1499. rctl |= E1000_RCTL_SECRC;
  1500. rctl &= ~E1000_RCTL_SBP;
  1501. if (adapter->netdev->mtu <= ETH_DATA_LEN)
  1502. rctl &= ~E1000_RCTL_LPE;
  1503. else
  1504. rctl |= E1000_RCTL_LPE;
  1505. if (adapter->rx_buffer_len <= IGB_RXBUFFER_2048) {
  1506. /* Setup buffer sizes */
  1507. rctl &= ~E1000_RCTL_SZ_4096;
  1508. rctl |= E1000_RCTL_BSEX;
  1509. switch (adapter->rx_buffer_len) {
  1510. case IGB_RXBUFFER_256:
  1511. rctl |= E1000_RCTL_SZ_256;
  1512. rctl &= ~E1000_RCTL_BSEX;
  1513. break;
  1514. case IGB_RXBUFFER_512:
  1515. rctl |= E1000_RCTL_SZ_512;
  1516. rctl &= ~E1000_RCTL_BSEX;
  1517. break;
  1518. case IGB_RXBUFFER_1024:
  1519. rctl |= E1000_RCTL_SZ_1024;
  1520. rctl &= ~E1000_RCTL_BSEX;
  1521. break;
  1522. case IGB_RXBUFFER_2048:
  1523. default:
  1524. rctl |= E1000_RCTL_SZ_2048;
  1525. rctl &= ~E1000_RCTL_BSEX;
  1526. break;
  1527. }
  1528. } else {
  1529. rctl &= ~E1000_RCTL_BSEX;
  1530. srrctl = adapter->rx_buffer_len >> E1000_SRRCTL_BSIZEPKT_SHIFT;
  1531. }
  1532. /* 82575 and greater support packet-split where the protocol
  1533. * header is placed in skb->data and the packet data is
  1534. * placed in pages hanging off of skb_shinfo(skb)->nr_frags.
  1535. * In the case of a non-split, skb->data is linearly filled,
  1536. * followed by the page buffers. Therefore, skb->data is
  1537. * sized to hold the largest protocol header.
  1538. */
  1539. /* allocations using alloc_page take too long for regular MTU
  1540. * so only enable packet split for jumbo frames */
  1541. if (rctl & E1000_RCTL_LPE) {
  1542. adapter->rx_ps_hdr_size = IGB_RXBUFFER_128;
  1543. srrctl |= adapter->rx_ps_hdr_size <<
  1544. E1000_SRRCTL_BSIZEHDRSIZE_SHIFT;
  1545. srrctl |= E1000_SRRCTL_DESCTYPE_HDR_SPLIT_ALWAYS;
  1546. } else {
  1547. adapter->rx_ps_hdr_size = 0;
  1548. srrctl |= E1000_SRRCTL_DESCTYPE_ADV_ONEBUF;
  1549. }
  1550. for (i = 0; i < adapter->num_rx_queues; i++)
  1551. wr32(E1000_SRRCTL(i), srrctl);
  1552. wr32(E1000_RCTL, rctl);
  1553. }
  1554. /**
  1555. * igb_configure_rx - Configure receive Unit after Reset
  1556. * @adapter: board private structure
  1557. *
  1558. * Configure the Rx unit of the MAC after a reset.
  1559. **/
  1560. static void igb_configure_rx(struct igb_adapter *adapter)
  1561. {
  1562. u64 rdba;
  1563. struct e1000_hw *hw = &adapter->hw;
  1564. u32 rctl, rxcsum;
  1565. u32 rxdctl;
  1566. int i;
  1567. /* disable receives while setting up the descriptors */
  1568. rctl = rd32(E1000_RCTL);
  1569. wr32(E1000_RCTL, rctl & ~E1000_RCTL_EN);
  1570. wrfl();
  1571. mdelay(10);
  1572. if (adapter->itr_setting > 3)
  1573. wr32(E1000_ITR, adapter->itr);
  1574. /* Setup the HW Rx Head and Tail Descriptor Pointers and
  1575. * the Base and Length of the Rx Descriptor Ring */
  1576. for (i = 0; i < adapter->num_rx_queues; i++) {
  1577. struct igb_ring *ring = &(adapter->rx_ring[i]);
  1578. rdba = ring->dma;
  1579. wr32(E1000_RDBAL(i),
  1580. rdba & 0x00000000ffffffffULL);
  1581. wr32(E1000_RDBAH(i), rdba >> 32);
  1582. wr32(E1000_RDLEN(i),
  1583. ring->count * sizeof(union e1000_adv_rx_desc));
  1584. ring->head = E1000_RDH(i);
  1585. ring->tail = E1000_RDT(i);
  1586. writel(0, hw->hw_addr + ring->tail);
  1587. writel(0, hw->hw_addr + ring->head);
  1588. rxdctl = rd32(E1000_RXDCTL(i));
  1589. rxdctl |= E1000_RXDCTL_QUEUE_ENABLE;
  1590. rxdctl &= 0xFFF00000;
  1591. rxdctl |= IGB_RX_PTHRESH;
  1592. rxdctl |= IGB_RX_HTHRESH << 8;
  1593. rxdctl |= IGB_RX_WTHRESH << 16;
  1594. wr32(E1000_RXDCTL(i), rxdctl);
  1595. #ifdef CONFIG_IGB_LRO
  1596. /* Intitial LRO Settings */
  1597. ring->lro_mgr.max_aggr = MAX_LRO_AGGR;
  1598. ring->lro_mgr.max_desc = MAX_LRO_DESCRIPTORS;
  1599. ring->lro_mgr.get_skb_header = igb_get_skb_hdr;
  1600. ring->lro_mgr.features = LRO_F_NAPI | LRO_F_EXTRACT_VLAN_ID;
  1601. ring->lro_mgr.dev = adapter->netdev;
  1602. ring->lro_mgr.ip_summed = CHECKSUM_UNNECESSARY;
  1603. ring->lro_mgr.ip_summed_aggr = CHECKSUM_UNNECESSARY;
  1604. #endif
  1605. }
  1606. if (adapter->num_rx_queues > 1) {
  1607. u32 random[10];
  1608. u32 mrqc;
  1609. u32 j, shift;
  1610. union e1000_reta {
  1611. u32 dword;
  1612. u8 bytes[4];
  1613. } reta;
  1614. get_random_bytes(&random[0], 40);
  1615. if (hw->mac.type >= e1000_82576)
  1616. shift = 0;
  1617. else
  1618. shift = 6;
  1619. for (j = 0; j < (32 * 4); j++) {
  1620. reta.bytes[j & 3] =
  1621. (j % adapter->num_rx_queues) << shift;
  1622. if ((j & 3) == 3)
  1623. writel(reta.dword,
  1624. hw->hw_addr + E1000_RETA(0) + (j & ~3));
  1625. }
  1626. mrqc = E1000_MRQC_ENABLE_RSS_4Q;
  1627. /* Fill out hash function seeds */
  1628. for (j = 0; j < 10; j++)
  1629. array_wr32(E1000_RSSRK(0), j, random[j]);
  1630. mrqc |= (E1000_MRQC_RSS_FIELD_IPV4 |
  1631. E1000_MRQC_RSS_FIELD_IPV4_TCP);
  1632. mrqc |= (E1000_MRQC_RSS_FIELD_IPV6 |
  1633. E1000_MRQC_RSS_FIELD_IPV6_TCP);
  1634. mrqc |= (E1000_MRQC_RSS_FIELD_IPV4_UDP |
  1635. E1000_MRQC_RSS_FIELD_IPV6_UDP);
  1636. mrqc |= (E1000_MRQC_RSS_FIELD_IPV6_UDP_EX |
  1637. E1000_MRQC_RSS_FIELD_IPV6_TCP_EX);
  1638. wr32(E1000_MRQC, mrqc);
  1639. /* Multiqueue and raw packet checksumming are mutually
  1640. * exclusive. Note that this not the same as TCP/IP
  1641. * checksumming, which works fine. */
  1642. rxcsum = rd32(E1000_RXCSUM);
  1643. rxcsum |= E1000_RXCSUM_PCSD;
  1644. wr32(E1000_RXCSUM, rxcsum);
  1645. } else {
  1646. /* Enable Receive Checksum Offload for TCP and UDP */
  1647. rxcsum = rd32(E1000_RXCSUM);
  1648. if (adapter->rx_csum) {
  1649. rxcsum |= E1000_RXCSUM_TUOFL;
  1650. /* Enable IPv4 payload checksum for UDP fragments
  1651. * Must be used in conjunction with packet-split. */
  1652. if (adapter->rx_ps_hdr_size)
  1653. rxcsum |= E1000_RXCSUM_IPPCSE;
  1654. } else {
  1655. rxcsum &= ~E1000_RXCSUM_TUOFL;
  1656. /* don't need to clear IPPCSE as it defaults to 0 */
  1657. }
  1658. wr32(E1000_RXCSUM, rxcsum);
  1659. }
  1660. if (adapter->vlgrp)
  1661. wr32(E1000_RLPML,
  1662. adapter->max_frame_size + VLAN_TAG_SIZE);
  1663. else
  1664. wr32(E1000_RLPML, adapter->max_frame_size);
  1665. /* Enable Receives */
  1666. wr32(E1000_RCTL, rctl);
  1667. }
  1668. /**
  1669. * igb_free_tx_resources - Free Tx Resources per Queue
  1670. * @adapter: board private structure
  1671. * @tx_ring: Tx descriptor ring for a specific queue
  1672. *
  1673. * Free all transmit software resources
  1674. **/
  1675. static void igb_free_tx_resources(struct igb_ring *tx_ring)
  1676. {
  1677. struct pci_dev *pdev = tx_ring->adapter->pdev;
  1678. igb_clean_tx_ring(tx_ring);
  1679. vfree(tx_ring->buffer_info);
  1680. tx_ring->buffer_info = NULL;
  1681. pci_free_consistent(pdev, tx_ring->size, tx_ring->desc, tx_ring->dma);
  1682. tx_ring->desc = NULL;
  1683. }
  1684. /**
  1685. * igb_free_all_tx_resources - Free Tx Resources for All Queues
  1686. * @adapter: board private structure
  1687. *
  1688. * Free all transmit software resources
  1689. **/
  1690. static void igb_free_all_tx_resources(struct igb_adapter *adapter)
  1691. {
  1692. int i;
  1693. for (i = 0; i < adapter->num_tx_queues; i++)
  1694. igb_free_tx_resources(&adapter->tx_ring[i]);
  1695. }
  1696. static void igb_unmap_and_free_tx_resource(struct igb_adapter *adapter,
  1697. struct igb_buffer *buffer_info)
  1698. {
  1699. if (buffer_info->dma) {
  1700. pci_unmap_page(adapter->pdev,
  1701. buffer_info->dma,
  1702. buffer_info->length,
  1703. PCI_DMA_TODEVICE);
  1704. buffer_info->dma = 0;
  1705. }
  1706. if (buffer_info->skb) {
  1707. dev_kfree_skb_any(buffer_info->skb);
  1708. buffer_info->skb = NULL;
  1709. }
  1710. buffer_info->time_stamp = 0;
  1711. /* buffer_info must be completely set up in the transmit path */
  1712. }
  1713. /**
  1714. * igb_clean_tx_ring - Free Tx Buffers
  1715. * @adapter: board private structure
  1716. * @tx_ring: ring to be cleaned
  1717. **/
  1718. static void igb_clean_tx_ring(struct igb_ring *tx_ring)
  1719. {
  1720. struct igb_adapter *adapter = tx_ring->adapter;
  1721. struct igb_buffer *buffer_info;
  1722. unsigned long size;
  1723. unsigned int i;
  1724. if (!tx_ring->buffer_info)
  1725. return;
  1726. /* Free all the Tx ring sk_buffs */
  1727. for (i = 0; i < tx_ring->count; i++) {
  1728. buffer_info = &tx_ring->buffer_info[i];
  1729. igb_unmap_and_free_tx_resource(adapter, buffer_info);
  1730. }
  1731. size = sizeof(struct igb_buffer) * tx_ring->count;
  1732. memset(tx_ring->buffer_info, 0, size);
  1733. /* Zero out the descriptor ring */
  1734. memset(tx_ring->desc, 0, tx_ring->size);
  1735. tx_ring->next_to_use = 0;
  1736. tx_ring->next_to_clean = 0;
  1737. writel(0, adapter->hw.hw_addr + tx_ring->head);
  1738. writel(0, adapter->hw.hw_addr + tx_ring->tail);
  1739. }
  1740. /**
  1741. * igb_clean_all_tx_rings - Free Tx Buffers for all queues
  1742. * @adapter: board private structure
  1743. **/
  1744. static void igb_clean_all_tx_rings(struct igb_adapter *adapter)
  1745. {
  1746. int i;
  1747. for (i = 0; i < adapter->num_tx_queues; i++)
  1748. igb_clean_tx_ring(&adapter->tx_ring[i]);
  1749. }
  1750. /**
  1751. * igb_free_rx_resources - Free Rx Resources
  1752. * @adapter: board private structure
  1753. * @rx_ring: ring to clean the resources from
  1754. *
  1755. * Free all receive software resources
  1756. **/
  1757. static void igb_free_rx_resources(struct igb_ring *rx_ring)
  1758. {
  1759. struct pci_dev *pdev = rx_ring->adapter->pdev;
  1760. igb_clean_rx_ring(rx_ring);
  1761. vfree(rx_ring->buffer_info);
  1762. rx_ring->buffer_info = NULL;
  1763. #ifdef CONFIG_IGB_LRO
  1764. vfree(rx_ring->lro_mgr.lro_arr);
  1765. rx_ring->lro_mgr.lro_arr = NULL;
  1766. #endif
  1767. pci_free_consistent(pdev, rx_ring->size, rx_ring->desc, rx_ring->dma);
  1768. rx_ring->desc = NULL;
  1769. }
  1770. /**
  1771. * igb_free_all_rx_resources - Free Rx Resources for All Queues
  1772. * @adapter: board private structure
  1773. *
  1774. * Free all receive software resources
  1775. **/
  1776. static void igb_free_all_rx_resources(struct igb_adapter *adapter)
  1777. {
  1778. int i;
  1779. for (i = 0; i < adapter->num_rx_queues; i++)
  1780. igb_free_rx_resources(&adapter->rx_ring[i]);
  1781. }
  1782. /**
  1783. * igb_clean_rx_ring - Free Rx Buffers per Queue
  1784. * @adapter: board private structure
  1785. * @rx_ring: ring to free buffers from
  1786. **/
  1787. static void igb_clean_rx_ring(struct igb_ring *rx_ring)
  1788. {
  1789. struct igb_adapter *adapter = rx_ring->adapter;
  1790. struct igb_buffer *buffer_info;
  1791. struct pci_dev *pdev = adapter->pdev;
  1792. unsigned long size;
  1793. unsigned int i;
  1794. if (!rx_ring->buffer_info)
  1795. return;
  1796. /* Free all the Rx ring sk_buffs */
  1797. for (i = 0; i < rx_ring->count; i++) {
  1798. buffer_info = &rx_ring->buffer_info[i];
  1799. if (buffer_info->dma) {
  1800. if (adapter->rx_ps_hdr_size)
  1801. pci_unmap_single(pdev, buffer_info->dma,
  1802. adapter->rx_ps_hdr_size,
  1803. PCI_DMA_FROMDEVICE);
  1804. else
  1805. pci_unmap_single(pdev, buffer_info->dma,
  1806. adapter->rx_buffer_len,
  1807. PCI_DMA_FROMDEVICE);
  1808. buffer_info->dma = 0;
  1809. }
  1810. if (buffer_info->skb) {
  1811. dev_kfree_skb(buffer_info->skb);
  1812. buffer_info->skb = NULL;
  1813. }
  1814. if (buffer_info->page) {
  1815. if (buffer_info->page_dma)
  1816. pci_unmap_page(pdev, buffer_info->page_dma,
  1817. PAGE_SIZE / 2,
  1818. PCI_DMA_FROMDEVICE);
  1819. put_page(buffer_info->page);
  1820. buffer_info->page = NULL;
  1821. buffer_info->page_dma = 0;
  1822. buffer_info->page_offset = 0;
  1823. }
  1824. }
  1825. size = sizeof(struct igb_buffer) * rx_ring->count;
  1826. memset(rx_ring->buffer_info, 0, size);
  1827. /* Zero out the descriptor ring */
  1828. memset(rx_ring->desc, 0, rx_ring->size);
  1829. rx_ring->next_to_clean = 0;
  1830. rx_ring->next_to_use = 0;
  1831. writel(0, adapter->hw.hw_addr + rx_ring->head);
  1832. writel(0, adapter->hw.hw_addr + rx_ring->tail);
  1833. }
  1834. /**
  1835. * igb_clean_all_rx_rings - Free Rx Buffers for all queues
  1836. * @adapter: board private structure
  1837. **/
  1838. static void igb_clean_all_rx_rings(struct igb_adapter *adapter)
  1839. {
  1840. int i;
  1841. for (i = 0; i < adapter->num_rx_queues; i++)
  1842. igb_clean_rx_ring(&adapter->rx_ring[i]);
  1843. }
  1844. /**
  1845. * igb_set_mac - Change the Ethernet Address of the NIC
  1846. * @netdev: network interface device structure
  1847. * @p: pointer to an address structure
  1848. *
  1849. * Returns 0 on success, negative on failure
  1850. **/
  1851. static int igb_set_mac(struct net_device *netdev, void *p)
  1852. {
  1853. struct igb_adapter *adapter = netdev_priv(netdev);
  1854. struct sockaddr *addr = p;
  1855. if (!is_valid_ether_addr(addr->sa_data))
  1856. return -EADDRNOTAVAIL;
  1857. memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
  1858. memcpy(adapter->hw.mac.addr, addr->sa_data, netdev->addr_len);
  1859. adapter->hw.mac.ops.rar_set(&adapter->hw, adapter->hw.mac.addr, 0);
  1860. return 0;
  1861. }
  1862. /**
  1863. * igb_set_multi - Multicast and Promiscuous mode set
  1864. * @netdev: network interface device structure
  1865. *
  1866. * The set_multi entry point is called whenever the multicast address
  1867. * list or the network interface flags are updated. This routine is
  1868. * responsible for configuring the hardware for proper multicast,
  1869. * promiscuous mode, and all-multi behavior.
  1870. **/
  1871. static void igb_set_multi(struct net_device *netdev)
  1872. {
  1873. struct igb_adapter *adapter = netdev_priv(netdev);
  1874. struct e1000_hw *hw = &adapter->hw;
  1875. struct e1000_mac_info *mac = &hw->mac;
  1876. struct dev_mc_list *mc_ptr;
  1877. u8 *mta_list;
  1878. u32 rctl;
  1879. int i;
  1880. /* Check for Promiscuous and All Multicast modes */
  1881. rctl = rd32(E1000_RCTL);
  1882. if (netdev->flags & IFF_PROMISC) {
  1883. rctl |= (E1000_RCTL_UPE | E1000_RCTL_MPE);
  1884. rctl &= ~E1000_RCTL_VFE;
  1885. } else {
  1886. if (netdev->flags & IFF_ALLMULTI) {
  1887. rctl |= E1000_RCTL_MPE;
  1888. rctl &= ~E1000_RCTL_UPE;
  1889. } else
  1890. rctl &= ~(E1000_RCTL_UPE | E1000_RCTL_MPE);
  1891. rctl |= E1000_RCTL_VFE;
  1892. }
  1893. wr32(E1000_RCTL, rctl);
  1894. if (!netdev->mc_count) {
  1895. /* nothing to program, so clear mc list */
  1896. igb_update_mc_addr_list_82575(hw, NULL, 0, 1,
  1897. mac->rar_entry_count);
  1898. return;
  1899. }
  1900. mta_list = kzalloc(netdev->mc_count * 6, GFP_ATOMIC);
  1901. if (!mta_list)
  1902. return;
  1903. /* The shared function expects a packed array of only addresses. */
  1904. mc_ptr = netdev->mc_list;
  1905. for (i = 0; i < netdev->mc_count; i++) {
  1906. if (!mc_ptr)
  1907. break;
  1908. memcpy(mta_list + (i*ETH_ALEN), mc_ptr->dmi_addr, ETH_ALEN);
  1909. mc_ptr = mc_ptr->next;
  1910. }
  1911. igb_update_mc_addr_list_82575(hw, mta_list, i, 1,
  1912. mac->rar_entry_count);
  1913. kfree(mta_list);
  1914. }
  1915. /* Need to wait a few seconds after link up to get diagnostic information from
  1916. * the phy */
  1917. static void igb_update_phy_info(unsigned long data)
  1918. {
  1919. struct igb_adapter *adapter = (struct igb_adapter *) data;
  1920. if (adapter->hw.phy.ops.get_phy_info)
  1921. adapter->hw.phy.ops.get_phy_info(&adapter->hw);
  1922. }
  1923. /**
  1924. * igb_watchdog - Timer Call-back
  1925. * @data: pointer to adapter cast into an unsigned long
  1926. **/
  1927. static void igb_watchdog(unsigned long data)
  1928. {
  1929. struct igb_adapter *adapter = (struct igb_adapter *)data;
  1930. /* Do the rest outside of interrupt context */
  1931. schedule_work(&adapter->watchdog_task);
  1932. }
  1933. static void igb_watchdog_task(struct work_struct *work)
  1934. {
  1935. struct igb_adapter *adapter = container_of(work,
  1936. struct igb_adapter, watchdog_task);
  1937. struct e1000_hw *hw = &adapter->hw;
  1938. struct net_device *netdev = adapter->netdev;
  1939. struct igb_ring *tx_ring = adapter->tx_ring;
  1940. struct e1000_mac_info *mac = &adapter->hw.mac;
  1941. u32 link;
  1942. s32 ret_val;
  1943. if ((netif_carrier_ok(netdev)) &&
  1944. (rd32(E1000_STATUS) & E1000_STATUS_LU))
  1945. goto link_up;
  1946. ret_val = hw->mac.ops.check_for_link(&adapter->hw);
  1947. if ((ret_val == E1000_ERR_PHY) &&
  1948. (hw->phy.type == e1000_phy_igp_3) &&
  1949. (rd32(E1000_CTRL) &
  1950. E1000_PHY_CTRL_GBE_DISABLE))
  1951. dev_info(&adapter->pdev->dev,
  1952. "Gigabit has been disabled, downgrading speed\n");
  1953. if ((hw->phy.media_type == e1000_media_type_internal_serdes) &&
  1954. !(rd32(E1000_TXCW) & E1000_TXCW_ANE))
  1955. link = mac->serdes_has_link;
  1956. else
  1957. link = rd32(E1000_STATUS) &
  1958. E1000_STATUS_LU;
  1959. if (link) {
  1960. if (!netif_carrier_ok(netdev)) {
  1961. u32 ctrl;
  1962. hw->mac.ops.get_speed_and_duplex(&adapter->hw,
  1963. &adapter->link_speed,
  1964. &adapter->link_duplex);
  1965. ctrl = rd32(E1000_CTRL);
  1966. dev_info(&adapter->pdev->dev,
  1967. "NIC Link is Up %d Mbps %s, "
  1968. "Flow Control: %s\n",
  1969. adapter->link_speed,
  1970. adapter->link_duplex == FULL_DUPLEX ?
  1971. "Full Duplex" : "Half Duplex",
  1972. ((ctrl & E1000_CTRL_TFCE) && (ctrl &
  1973. E1000_CTRL_RFCE)) ? "RX/TX" : ((ctrl &
  1974. E1000_CTRL_RFCE) ? "RX" : ((ctrl &
  1975. E1000_CTRL_TFCE) ? "TX" : "None")));
  1976. /* tweak tx_queue_len according to speed/duplex and
  1977. * adjust the timeout factor */
  1978. netdev->tx_queue_len = adapter->tx_queue_len;
  1979. adapter->tx_timeout_factor = 1;
  1980. switch (adapter->link_speed) {
  1981. case SPEED_10:
  1982. netdev->tx_queue_len = 10;
  1983. adapter->tx_timeout_factor = 14;
  1984. break;
  1985. case SPEED_100:
  1986. netdev->tx_queue_len = 100;
  1987. /* maybe add some timeout factor ? */
  1988. break;
  1989. }
  1990. netif_carrier_on(netdev);
  1991. netif_tx_wake_all_queues(netdev);
  1992. if (!test_bit(__IGB_DOWN, &adapter->state))
  1993. mod_timer(&adapter->phy_info_timer,
  1994. round_jiffies(jiffies + 2 * HZ));
  1995. }
  1996. } else {
  1997. if (netif_carrier_ok(netdev)) {
  1998. adapter->link_speed = 0;
  1999. adapter->link_duplex = 0;
  2000. dev_info(&adapter->pdev->dev, "NIC Link is Down\n");
  2001. netif_carrier_off(netdev);
  2002. netif_tx_stop_all_queues(netdev);
  2003. if (!test_bit(__IGB_DOWN, &adapter->state))
  2004. mod_timer(&adapter->phy_info_timer,
  2005. round_jiffies(jiffies + 2 * HZ));
  2006. }
  2007. }
  2008. link_up:
  2009. igb_update_stats(adapter);
  2010. mac->tx_packet_delta = adapter->stats.tpt - adapter->tpt_old;
  2011. adapter->tpt_old = adapter->stats.tpt;
  2012. mac->collision_delta = adapter->stats.colc - adapter->colc_old;
  2013. adapter->colc_old = adapter->stats.colc;
  2014. adapter->gorc = adapter->stats.gorc - adapter->gorc_old;
  2015. adapter->gorc_old = adapter->stats.gorc;
  2016. adapter->gotc = adapter->stats.gotc - adapter->gotc_old;
  2017. adapter->gotc_old = adapter->stats.gotc;
  2018. igb_update_adaptive(&adapter->hw);
  2019. if (!netif_carrier_ok(netdev)) {
  2020. if (IGB_DESC_UNUSED(tx_ring) + 1 < tx_ring->count) {
  2021. /* We've lost link, so the controller stops DMA,
  2022. * but we've got queued Tx work that's never going
  2023. * to get done, so reset controller to flush Tx.
  2024. * (Do the reset outside of interrupt context). */
  2025. adapter->tx_timeout_count++;
  2026. schedule_work(&adapter->reset_task);
  2027. }
  2028. }
  2029. /* Cause software interrupt to ensure rx ring is cleaned */
  2030. wr32(E1000_ICS, E1000_ICS_RXDMT0);
  2031. /* Force detection of hung controller every watchdog period */
  2032. tx_ring->detect_tx_hung = true;
  2033. /* Reset the timer */
  2034. if (!test_bit(__IGB_DOWN, &adapter->state))
  2035. mod_timer(&adapter->watchdog_timer,
  2036. round_jiffies(jiffies + 2 * HZ));
  2037. }
  2038. enum latency_range {
  2039. lowest_latency = 0,
  2040. low_latency = 1,
  2041. bulk_latency = 2,
  2042. latency_invalid = 255
  2043. };
  2044. /**
  2045. * igb_update_ring_itr - update the dynamic ITR value based on packet size
  2046. *
  2047. * Stores a new ITR value based on strictly on packet size. This
  2048. * algorithm is less sophisticated than that used in igb_update_itr,
  2049. * due to the difficulty of synchronizing statistics across multiple
  2050. * receive rings. The divisors and thresholds used by this fuction
  2051. * were determined based on theoretical maximum wire speed and testing
  2052. * data, in order to minimize response time while increasing bulk
  2053. * throughput.
  2054. * This functionality is controlled by the InterruptThrottleRate module
  2055. * parameter (see igb_param.c)
  2056. * NOTE: This function is called only when operating in a multiqueue
  2057. * receive environment.
  2058. * @rx_ring: pointer to ring
  2059. **/
  2060. static void igb_update_ring_itr(struct igb_ring *rx_ring)
  2061. {
  2062. int new_val = rx_ring->itr_val;
  2063. int avg_wire_size = 0;
  2064. struct igb_adapter *adapter = rx_ring->adapter;
  2065. if (!rx_ring->total_packets)
  2066. goto clear_counts; /* no packets, so don't do anything */
  2067. /* For non-gigabit speeds, just fix the interrupt rate at 4000
  2068. * ints/sec - ITR timer value of 120 ticks.
  2069. */
  2070. if (adapter->link_speed != SPEED_1000) {
  2071. new_val = 120;
  2072. goto set_itr_val;
  2073. }
  2074. avg_wire_size = rx_ring->total_bytes / rx_ring->total_packets;
  2075. /* Add 24 bytes to size to account for CRC, preamble, and gap */
  2076. avg_wire_size += 24;
  2077. /* Don't starve jumbo frames */
  2078. avg_wire_size = min(avg_wire_size, 3000);
  2079. /* Give a little boost to mid-size frames */
  2080. if ((avg_wire_size > 300) && (avg_wire_size < 1200))
  2081. new_val = avg_wire_size / 3;
  2082. else
  2083. new_val = avg_wire_size / 2;
  2084. set_itr_val:
  2085. if (new_val != rx_ring->itr_val) {
  2086. rx_ring->itr_val = new_val;
  2087. rx_ring->set_itr = 1;
  2088. }
  2089. clear_counts:
  2090. rx_ring->total_bytes = 0;
  2091. rx_ring->total_packets = 0;
  2092. }
  2093. /**
  2094. * igb_update_itr - update the dynamic ITR value based on statistics
  2095. * Stores a new ITR value based on packets and byte
  2096. * counts during the last interrupt. The advantage of per interrupt
  2097. * computation is faster updates and more accurate ITR for the current
  2098. * traffic pattern. Constants in this function were computed
  2099. * based on theoretical maximum wire speed and thresholds were set based
  2100. * on testing data as well as attempting to minimize response time
  2101. * while increasing bulk throughput.
  2102. * this functionality is controlled by the InterruptThrottleRate module
  2103. * parameter (see igb_param.c)
  2104. * NOTE: These calculations are only valid when operating in a single-
  2105. * queue environment.
  2106. * @adapter: pointer to adapter
  2107. * @itr_setting: current adapter->itr
  2108. * @packets: the number of packets during this measurement interval
  2109. * @bytes: the number of bytes during this measurement interval
  2110. **/
  2111. static unsigned int igb_update_itr(struct igb_adapter *adapter, u16 itr_setting,
  2112. int packets, int bytes)
  2113. {
  2114. unsigned int retval = itr_setting;
  2115. if (packets == 0)
  2116. goto update_itr_done;
  2117. switch (itr_setting) {
  2118. case lowest_latency:
  2119. /* handle TSO and jumbo frames */
  2120. if (bytes/packets > 8000)
  2121. retval = bulk_latency;
  2122. else if ((packets < 5) && (bytes > 512))
  2123. retval = low_latency;
  2124. break;
  2125. case low_latency: /* 50 usec aka 20000 ints/s */
  2126. if (bytes > 10000) {
  2127. /* this if handles the TSO accounting */
  2128. if (bytes/packets > 8000) {
  2129. retval = bulk_latency;
  2130. } else if ((packets < 10) || ((bytes/packets) > 1200)) {
  2131. retval = bulk_latency;
  2132. } else if ((packets > 35)) {
  2133. retval = lowest_latency;
  2134. }
  2135. } else if (bytes/packets > 2000) {
  2136. retval = bulk_latency;
  2137. } else if (packets <= 2 && bytes < 512) {
  2138. retval = lowest_latency;
  2139. }
  2140. break;
  2141. case bulk_latency: /* 250 usec aka 4000 ints/s */
  2142. if (bytes > 25000) {
  2143. if (packets > 35)
  2144. retval = low_latency;
  2145. } else if (bytes < 6000) {
  2146. retval = low_latency;
  2147. }
  2148. break;
  2149. }
  2150. update_itr_done:
  2151. return retval;
  2152. }
  2153. static void igb_set_itr(struct igb_adapter *adapter)
  2154. {
  2155. u16 current_itr;
  2156. u32 new_itr = adapter->itr;
  2157. /* for non-gigabit speeds, just fix the interrupt rate at 4000 */
  2158. if (adapter->link_speed != SPEED_1000) {
  2159. current_itr = 0;
  2160. new_itr = 4000;
  2161. goto set_itr_now;
  2162. }
  2163. adapter->rx_itr = igb_update_itr(adapter,
  2164. adapter->rx_itr,
  2165. adapter->rx_ring->total_packets,
  2166. adapter->rx_ring->total_bytes);
  2167. if (adapter->rx_ring->buddy) {
  2168. adapter->tx_itr = igb_update_itr(adapter,
  2169. adapter->tx_itr,
  2170. adapter->tx_ring->total_packets,
  2171. adapter->tx_ring->total_bytes);
  2172. current_itr = max(adapter->rx_itr, adapter->tx_itr);
  2173. } else {
  2174. current_itr = adapter->rx_itr;
  2175. }
  2176. /* conservative mode (itr 3) eliminates the lowest_latency setting */
  2177. if (adapter->itr_setting == 3 &&
  2178. current_itr == lowest_latency)
  2179. current_itr = low_latency;
  2180. switch (current_itr) {
  2181. /* counts and packets in update_itr are dependent on these numbers */
  2182. case lowest_latency:
  2183. new_itr = 70000;
  2184. break;
  2185. case low_latency:
  2186. new_itr = 20000; /* aka hwitr = ~200 */
  2187. break;
  2188. case bulk_latency:
  2189. new_itr = 4000;
  2190. break;
  2191. default:
  2192. break;
  2193. }
  2194. set_itr_now:
  2195. adapter->rx_ring->total_bytes = 0;
  2196. adapter->rx_ring->total_packets = 0;
  2197. if (adapter->rx_ring->buddy) {
  2198. adapter->rx_ring->buddy->total_bytes = 0;
  2199. adapter->rx_ring->buddy->total_packets = 0;
  2200. }
  2201. if (new_itr != adapter->itr) {
  2202. /* this attempts to bias the interrupt rate towards Bulk
  2203. * by adding intermediate steps when interrupt rate is
  2204. * increasing */
  2205. new_itr = new_itr > adapter->itr ?
  2206. min(adapter->itr + (new_itr >> 2), new_itr) :
  2207. new_itr;
  2208. /* Don't write the value here; it resets the adapter's
  2209. * internal timer, and causes us to delay far longer than
  2210. * we should between interrupts. Instead, we write the ITR
  2211. * value at the beginning of the next interrupt so the timing
  2212. * ends up being correct.
  2213. */
  2214. adapter->itr = new_itr;
  2215. adapter->rx_ring->itr_val = 1000000000 / (new_itr * 256);
  2216. adapter->rx_ring->set_itr = 1;
  2217. }
  2218. return;
  2219. }
  2220. #define IGB_TX_FLAGS_CSUM 0x00000001
  2221. #define IGB_TX_FLAGS_VLAN 0x00000002
  2222. #define IGB_TX_FLAGS_TSO 0x00000004
  2223. #define IGB_TX_FLAGS_IPV4 0x00000008
  2224. #define IGB_TX_FLAGS_VLAN_MASK 0xffff0000
  2225. #define IGB_TX_FLAGS_VLAN_SHIFT 16
  2226. static inline int igb_tso_adv(struct igb_adapter *adapter,
  2227. struct igb_ring *tx_ring,
  2228. struct sk_buff *skb, u32 tx_flags, u8 *hdr_len)
  2229. {
  2230. struct e1000_adv_tx_context_desc *context_desc;
  2231. unsigned int i;
  2232. int err;
  2233. struct igb_buffer *buffer_info;
  2234. u32 info = 0, tu_cmd = 0;
  2235. u32 mss_l4len_idx, l4len;
  2236. *hdr_len = 0;
  2237. if (skb_header_cloned(skb)) {
  2238. err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
  2239. if (err)
  2240. return err;
  2241. }
  2242. l4len = tcp_hdrlen(skb);
  2243. *hdr_len += l4len;
  2244. if (skb->protocol == htons(ETH_P_IP)) {
  2245. struct iphdr *iph = ip_hdr(skb);
  2246. iph->tot_len = 0;
  2247. iph->check = 0;
  2248. tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
  2249. iph->daddr, 0,
  2250. IPPROTO_TCP,
  2251. 0);
  2252. } else if (skb_shinfo(skb)->gso_type == SKB_GSO_TCPV6) {
  2253. ipv6_hdr(skb)->payload_len = 0;
  2254. tcp_hdr(skb)->check = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
  2255. &ipv6_hdr(skb)->daddr,
  2256. 0, IPPROTO_TCP, 0);
  2257. }
  2258. i = tx_ring->next_to_use;
  2259. buffer_info = &tx_ring->buffer_info[i];
  2260. context_desc = E1000_TX_CTXTDESC_ADV(*tx_ring, i);
  2261. /* VLAN MACLEN IPLEN */
  2262. if (tx_flags & IGB_TX_FLAGS_VLAN)
  2263. info |= (tx_flags & IGB_TX_FLAGS_VLAN_MASK);
  2264. info |= (skb_network_offset(skb) << E1000_ADVTXD_MACLEN_SHIFT);
  2265. *hdr_len += skb_network_offset(skb);
  2266. info |= skb_network_header_len(skb);
  2267. *hdr_len += skb_network_header_len(skb);
  2268. context_desc->vlan_macip_lens = cpu_to_le32(info);
  2269. /* ADV DTYP TUCMD MKRLOC/ISCSIHEDLEN */
  2270. tu_cmd |= (E1000_TXD_CMD_DEXT | E1000_ADVTXD_DTYP_CTXT);
  2271. if (skb->protocol == htons(ETH_P_IP))
  2272. tu_cmd |= E1000_ADVTXD_TUCMD_IPV4;
  2273. tu_cmd |= E1000_ADVTXD_TUCMD_L4T_TCP;
  2274. context_desc->type_tucmd_mlhl = cpu_to_le32(tu_cmd);
  2275. /* MSS L4LEN IDX */
  2276. mss_l4len_idx = (skb_shinfo(skb)->gso_size << E1000_ADVTXD_MSS_SHIFT);
  2277. mss_l4len_idx |= (l4len << E1000_ADVTXD_L4LEN_SHIFT);
  2278. /* Context index must be unique per ring. */
  2279. if (adapter->flags & IGB_FLAG_NEED_CTX_IDX)
  2280. mss_l4len_idx |= tx_ring->queue_index << 4;
  2281. context_desc->mss_l4len_idx = cpu_to_le32(mss_l4len_idx);
  2282. context_desc->seqnum_seed = 0;
  2283. buffer_info->time_stamp = jiffies;
  2284. buffer_info->dma = 0;
  2285. i++;
  2286. if (i == tx_ring->count)
  2287. i = 0;
  2288. tx_ring->next_to_use = i;
  2289. return true;
  2290. }
  2291. static inline bool igb_tx_csum_adv(struct igb_adapter *adapter,
  2292. struct igb_ring *tx_ring,
  2293. struct sk_buff *skb, u32 tx_flags)
  2294. {
  2295. struct e1000_adv_tx_context_desc *context_desc;
  2296. unsigned int i;
  2297. struct igb_buffer *buffer_info;
  2298. u32 info = 0, tu_cmd = 0;
  2299. if ((skb->ip_summed == CHECKSUM_PARTIAL) ||
  2300. (tx_flags & IGB_TX_FLAGS_VLAN)) {
  2301. i = tx_ring->next_to_use;
  2302. buffer_info = &tx_ring->buffer_info[i];
  2303. context_desc = E1000_TX_CTXTDESC_ADV(*tx_ring, i);
  2304. if (tx_flags & IGB_TX_FLAGS_VLAN)
  2305. info |= (tx_flags & IGB_TX_FLAGS_VLAN_MASK);
  2306. info |= (skb_network_offset(skb) << E1000_ADVTXD_MACLEN_SHIFT);
  2307. if (skb->ip_summed == CHECKSUM_PARTIAL)
  2308. info |= skb_network_header_len(skb);
  2309. context_desc->vlan_macip_lens = cpu_to_le32(info);
  2310. tu_cmd |= (E1000_TXD_CMD_DEXT | E1000_ADVTXD_DTYP_CTXT);
  2311. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  2312. switch (skb->protocol) {
  2313. case __constant_htons(ETH_P_IP):
  2314. tu_cmd |= E1000_ADVTXD_TUCMD_IPV4;
  2315. if (ip_hdr(skb)->protocol == IPPROTO_TCP)
  2316. tu_cmd |= E1000_ADVTXD_TUCMD_L4T_TCP;
  2317. break;
  2318. case __constant_htons(ETH_P_IPV6):
  2319. /* XXX what about other V6 headers?? */
  2320. if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
  2321. tu_cmd |= E1000_ADVTXD_TUCMD_L4T_TCP;
  2322. break;
  2323. default:
  2324. if (unlikely(net_ratelimit()))
  2325. dev_warn(&adapter->pdev->dev,
  2326. "partial checksum but proto=%x!\n",
  2327. skb->protocol);
  2328. break;
  2329. }
  2330. }
  2331. context_desc->type_tucmd_mlhl = cpu_to_le32(tu_cmd);
  2332. context_desc->seqnum_seed = 0;
  2333. if (adapter->flags & IGB_FLAG_NEED_CTX_IDX)
  2334. context_desc->mss_l4len_idx =
  2335. cpu_to_le32(tx_ring->queue_index << 4);
  2336. buffer_info->time_stamp = jiffies;
  2337. buffer_info->dma = 0;
  2338. i++;
  2339. if (i == tx_ring->count)
  2340. i = 0;
  2341. tx_ring->next_to_use = i;
  2342. return true;
  2343. }
  2344. return false;
  2345. }
  2346. #define IGB_MAX_TXD_PWR 16
  2347. #define IGB_MAX_DATA_PER_TXD (1<<IGB_MAX_TXD_PWR)
  2348. static inline int igb_tx_map_adv(struct igb_adapter *adapter,
  2349. struct igb_ring *tx_ring,
  2350. struct sk_buff *skb)
  2351. {
  2352. struct igb_buffer *buffer_info;
  2353. unsigned int len = skb_headlen(skb);
  2354. unsigned int count = 0, i;
  2355. unsigned int f;
  2356. i = tx_ring->next_to_use;
  2357. buffer_info = &tx_ring->buffer_info[i];
  2358. BUG_ON(len >= IGB_MAX_DATA_PER_TXD);
  2359. buffer_info->length = len;
  2360. /* set time_stamp *before* dma to help avoid a possible race */
  2361. buffer_info->time_stamp = jiffies;
  2362. buffer_info->dma = pci_map_single(adapter->pdev, skb->data, len,
  2363. PCI_DMA_TODEVICE);
  2364. count++;
  2365. i++;
  2366. if (i == tx_ring->count)
  2367. i = 0;
  2368. for (f = 0; f < skb_shinfo(skb)->nr_frags; f++) {
  2369. struct skb_frag_struct *frag;
  2370. frag = &skb_shinfo(skb)->frags[f];
  2371. len = frag->size;
  2372. buffer_info = &tx_ring->buffer_info[i];
  2373. BUG_ON(len >= IGB_MAX_DATA_PER_TXD);
  2374. buffer_info->length = len;
  2375. buffer_info->time_stamp = jiffies;
  2376. buffer_info->dma = pci_map_page(adapter->pdev,
  2377. frag->page,
  2378. frag->page_offset,
  2379. len,
  2380. PCI_DMA_TODEVICE);
  2381. count++;
  2382. i++;
  2383. if (i == tx_ring->count)
  2384. i = 0;
  2385. }
  2386. i = (i == 0) ? tx_ring->count - 1 : i - 1;
  2387. tx_ring->buffer_info[i].skb = skb;
  2388. return count;
  2389. }
  2390. static inline void igb_tx_queue_adv(struct igb_adapter *adapter,
  2391. struct igb_ring *tx_ring,
  2392. int tx_flags, int count, u32 paylen,
  2393. u8 hdr_len)
  2394. {
  2395. union e1000_adv_tx_desc *tx_desc = NULL;
  2396. struct igb_buffer *buffer_info;
  2397. u32 olinfo_status = 0, cmd_type_len;
  2398. unsigned int i;
  2399. cmd_type_len = (E1000_ADVTXD_DTYP_DATA | E1000_ADVTXD_DCMD_IFCS |
  2400. E1000_ADVTXD_DCMD_DEXT);
  2401. if (tx_flags & IGB_TX_FLAGS_VLAN)
  2402. cmd_type_len |= E1000_ADVTXD_DCMD_VLE;
  2403. if (tx_flags & IGB_TX_FLAGS_TSO) {
  2404. cmd_type_len |= E1000_ADVTXD_DCMD_TSE;
  2405. /* insert tcp checksum */
  2406. olinfo_status |= E1000_TXD_POPTS_TXSM << 8;
  2407. /* insert ip checksum */
  2408. if (tx_flags & IGB_TX_FLAGS_IPV4)
  2409. olinfo_status |= E1000_TXD_POPTS_IXSM << 8;
  2410. } else if (tx_flags & IGB_TX_FLAGS_CSUM) {
  2411. olinfo_status |= E1000_TXD_POPTS_TXSM << 8;
  2412. }
  2413. if ((adapter->flags & IGB_FLAG_NEED_CTX_IDX) &&
  2414. (tx_flags & (IGB_TX_FLAGS_CSUM | IGB_TX_FLAGS_TSO |
  2415. IGB_TX_FLAGS_VLAN)))
  2416. olinfo_status |= tx_ring->queue_index << 4;
  2417. olinfo_status |= ((paylen - hdr_len) << E1000_ADVTXD_PAYLEN_SHIFT);
  2418. i = tx_ring->next_to_use;
  2419. while (count--) {
  2420. buffer_info = &tx_ring->buffer_info[i];
  2421. tx_desc = E1000_TX_DESC_ADV(*tx_ring, i);
  2422. tx_desc->read.buffer_addr = cpu_to_le64(buffer_info->dma);
  2423. tx_desc->read.cmd_type_len =
  2424. cpu_to_le32(cmd_type_len | buffer_info->length);
  2425. tx_desc->read.olinfo_status = cpu_to_le32(olinfo_status);
  2426. i++;
  2427. if (i == tx_ring->count)
  2428. i = 0;
  2429. }
  2430. tx_desc->read.cmd_type_len |= cpu_to_le32(adapter->txd_cmd);
  2431. /* Force memory writes to complete before letting h/w
  2432. * know there are new descriptors to fetch. (Only
  2433. * applicable for weak-ordered memory model archs,
  2434. * such as IA-64). */
  2435. wmb();
  2436. tx_ring->next_to_use = i;
  2437. writel(i, adapter->hw.hw_addr + tx_ring->tail);
  2438. /* we need this if more than one processor can write to our tail
  2439. * at a time, it syncronizes IO on IA64/Altix systems */
  2440. mmiowb();
  2441. }
  2442. static int __igb_maybe_stop_tx(struct net_device *netdev,
  2443. struct igb_ring *tx_ring, int size)
  2444. {
  2445. struct igb_adapter *adapter = netdev_priv(netdev);
  2446. netif_stop_subqueue(netdev, tx_ring->queue_index);
  2447. /* Herbert's original patch had:
  2448. * smp_mb__after_netif_stop_queue();
  2449. * but since that doesn't exist yet, just open code it. */
  2450. smp_mb();
  2451. /* We need to check again in a case another CPU has just
  2452. * made room available. */
  2453. if (IGB_DESC_UNUSED(tx_ring) < size)
  2454. return -EBUSY;
  2455. /* A reprieve! */
  2456. netif_wake_subqueue(netdev, tx_ring->queue_index);
  2457. ++adapter->restart_queue;
  2458. return 0;
  2459. }
  2460. static int igb_maybe_stop_tx(struct net_device *netdev,
  2461. struct igb_ring *tx_ring, int size)
  2462. {
  2463. if (IGB_DESC_UNUSED(tx_ring) >= size)
  2464. return 0;
  2465. return __igb_maybe_stop_tx(netdev, tx_ring, size);
  2466. }
  2467. #define TXD_USE_COUNT(S) (((S) >> (IGB_MAX_TXD_PWR)) + 1)
  2468. static int igb_xmit_frame_ring_adv(struct sk_buff *skb,
  2469. struct net_device *netdev,
  2470. struct igb_ring *tx_ring)
  2471. {
  2472. struct igb_adapter *adapter = netdev_priv(netdev);
  2473. unsigned int tx_flags = 0;
  2474. unsigned int len;
  2475. u8 hdr_len = 0;
  2476. int tso = 0;
  2477. len = skb_headlen(skb);
  2478. if (test_bit(__IGB_DOWN, &adapter->state)) {
  2479. dev_kfree_skb_any(skb);
  2480. return NETDEV_TX_OK;
  2481. }
  2482. if (skb->len <= 0) {
  2483. dev_kfree_skb_any(skb);
  2484. return NETDEV_TX_OK;
  2485. }
  2486. /* need: 1 descriptor per page,
  2487. * + 2 desc gap to keep tail from touching head,
  2488. * + 1 desc for skb->data,
  2489. * + 1 desc for context descriptor,
  2490. * otherwise try next time */
  2491. if (igb_maybe_stop_tx(netdev, tx_ring, skb_shinfo(skb)->nr_frags + 4)) {
  2492. /* this is a hard error */
  2493. return NETDEV_TX_BUSY;
  2494. }
  2495. skb_orphan(skb);
  2496. if (adapter->vlgrp && vlan_tx_tag_present(skb)) {
  2497. tx_flags |= IGB_TX_FLAGS_VLAN;
  2498. tx_flags |= (vlan_tx_tag_get(skb) << IGB_TX_FLAGS_VLAN_SHIFT);
  2499. }
  2500. if (skb->protocol == htons(ETH_P_IP))
  2501. tx_flags |= IGB_TX_FLAGS_IPV4;
  2502. tso = skb_is_gso(skb) ? igb_tso_adv(adapter, tx_ring, skb, tx_flags,
  2503. &hdr_len) : 0;
  2504. if (tso < 0) {
  2505. dev_kfree_skb_any(skb);
  2506. return NETDEV_TX_OK;
  2507. }
  2508. if (tso)
  2509. tx_flags |= IGB_TX_FLAGS_TSO;
  2510. else if (igb_tx_csum_adv(adapter, tx_ring, skb, tx_flags))
  2511. if (skb->ip_summed == CHECKSUM_PARTIAL)
  2512. tx_flags |= IGB_TX_FLAGS_CSUM;
  2513. igb_tx_queue_adv(adapter, tx_ring, tx_flags,
  2514. igb_tx_map_adv(adapter, tx_ring, skb),
  2515. skb->len, hdr_len);
  2516. netdev->trans_start = jiffies;
  2517. /* Make sure there is space in the ring for the next send. */
  2518. igb_maybe_stop_tx(netdev, tx_ring, MAX_SKB_FRAGS + 4);
  2519. return NETDEV_TX_OK;
  2520. }
  2521. static int igb_xmit_frame_adv(struct sk_buff *skb, struct net_device *netdev)
  2522. {
  2523. struct igb_adapter *adapter = netdev_priv(netdev);
  2524. struct igb_ring *tx_ring;
  2525. int r_idx = 0;
  2526. r_idx = skb->queue_mapping & (IGB_MAX_TX_QUEUES - 1);
  2527. tx_ring = adapter->multi_tx_table[r_idx];
  2528. /* This goes back to the question of how to logically map a tx queue
  2529. * to a flow. Right now, performance is impacted slightly negatively
  2530. * if using multiple tx queues. If the stack breaks away from a
  2531. * single qdisc implementation, we can look at this again. */
  2532. return (igb_xmit_frame_ring_adv(skb, netdev, tx_ring));
  2533. }
  2534. /**
  2535. * igb_tx_timeout - Respond to a Tx Hang
  2536. * @netdev: network interface device structure
  2537. **/
  2538. static void igb_tx_timeout(struct net_device *netdev)
  2539. {
  2540. struct igb_adapter *adapter = netdev_priv(netdev);
  2541. struct e1000_hw *hw = &adapter->hw;
  2542. /* Do the reset outside of interrupt context */
  2543. adapter->tx_timeout_count++;
  2544. schedule_work(&adapter->reset_task);
  2545. wr32(E1000_EICS, adapter->eims_enable_mask &
  2546. ~(E1000_EIMS_TCP_TIMER | E1000_EIMS_OTHER));
  2547. }
  2548. static void igb_reset_task(struct work_struct *work)
  2549. {
  2550. struct igb_adapter *adapter;
  2551. adapter = container_of(work, struct igb_adapter, reset_task);
  2552. igb_reinit_locked(adapter);
  2553. }
  2554. /**
  2555. * igb_get_stats - Get System Network Statistics
  2556. * @netdev: network interface device structure
  2557. *
  2558. * Returns the address of the device statistics structure.
  2559. * The statistics are actually updated from the timer callback.
  2560. **/
  2561. static struct net_device_stats *
  2562. igb_get_stats(struct net_device *netdev)
  2563. {
  2564. struct igb_adapter *adapter = netdev_priv(netdev);
  2565. /* only return the current stats */
  2566. return &adapter->net_stats;
  2567. }
  2568. /**
  2569. * igb_change_mtu - Change the Maximum Transfer Unit
  2570. * @netdev: network interface device structure
  2571. * @new_mtu: new value for maximum frame size
  2572. *
  2573. * Returns 0 on success, negative on failure
  2574. **/
  2575. static int igb_change_mtu(struct net_device *netdev, int new_mtu)
  2576. {
  2577. struct igb_adapter *adapter = netdev_priv(netdev);
  2578. int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN;
  2579. if ((max_frame < ETH_ZLEN + ETH_FCS_LEN) ||
  2580. (max_frame > MAX_JUMBO_FRAME_SIZE)) {
  2581. dev_err(&adapter->pdev->dev, "Invalid MTU setting\n");
  2582. return -EINVAL;
  2583. }
  2584. #define MAX_STD_JUMBO_FRAME_SIZE 9234
  2585. if (max_frame > MAX_STD_JUMBO_FRAME_SIZE) {
  2586. dev_err(&adapter->pdev->dev, "MTU > 9216 not supported.\n");
  2587. return -EINVAL;
  2588. }
  2589. while (test_and_set_bit(__IGB_RESETTING, &adapter->state))
  2590. msleep(1);
  2591. /* igb_down has a dependency on max_frame_size */
  2592. adapter->max_frame_size = max_frame;
  2593. if (netif_running(netdev))
  2594. igb_down(adapter);
  2595. /* NOTE: netdev_alloc_skb reserves 16 bytes, and typically NET_IP_ALIGN
  2596. * means we reserve 2 more, this pushes us to allocate from the next
  2597. * larger slab size.
  2598. * i.e. RXBUFFER_2048 --> size-4096 slab
  2599. */
  2600. if (max_frame <= IGB_RXBUFFER_256)
  2601. adapter->rx_buffer_len = IGB_RXBUFFER_256;
  2602. else if (max_frame <= IGB_RXBUFFER_512)
  2603. adapter->rx_buffer_len = IGB_RXBUFFER_512;
  2604. else if (max_frame <= IGB_RXBUFFER_1024)
  2605. adapter->rx_buffer_len = IGB_RXBUFFER_1024;
  2606. else if (max_frame <= IGB_RXBUFFER_2048)
  2607. adapter->rx_buffer_len = IGB_RXBUFFER_2048;
  2608. else
  2609. #if (PAGE_SIZE / 2) > IGB_RXBUFFER_16384
  2610. adapter->rx_buffer_len = IGB_RXBUFFER_16384;
  2611. #else
  2612. adapter->rx_buffer_len = PAGE_SIZE / 2;
  2613. #endif
  2614. /* adjust allocation if LPE protects us, and we aren't using SBP */
  2615. if ((max_frame == ETH_FRAME_LEN + ETH_FCS_LEN) ||
  2616. (max_frame == MAXIMUM_ETHERNET_VLAN_SIZE))
  2617. adapter->rx_buffer_len = MAXIMUM_ETHERNET_VLAN_SIZE;
  2618. dev_info(&adapter->pdev->dev, "changing MTU from %d to %d\n",
  2619. netdev->mtu, new_mtu);
  2620. netdev->mtu = new_mtu;
  2621. if (netif_running(netdev))
  2622. igb_up(adapter);
  2623. else
  2624. igb_reset(adapter);
  2625. clear_bit(__IGB_RESETTING, &adapter->state);
  2626. return 0;
  2627. }
  2628. /**
  2629. * igb_update_stats - Update the board statistics counters
  2630. * @adapter: board private structure
  2631. **/
  2632. void igb_update_stats(struct igb_adapter *adapter)
  2633. {
  2634. struct e1000_hw *hw = &adapter->hw;
  2635. struct pci_dev *pdev = adapter->pdev;
  2636. u16 phy_tmp;
  2637. #define PHY_IDLE_ERROR_COUNT_MASK 0x00FF
  2638. /*
  2639. * Prevent stats update while adapter is being reset, or if the pci
  2640. * connection is down.
  2641. */
  2642. if (adapter->link_speed == 0)
  2643. return;
  2644. if (pci_channel_offline(pdev))
  2645. return;
  2646. adapter->stats.crcerrs += rd32(E1000_CRCERRS);
  2647. adapter->stats.gprc += rd32(E1000_GPRC);
  2648. adapter->stats.gorc += rd32(E1000_GORCL);
  2649. rd32(E1000_GORCH); /* clear GORCL */
  2650. adapter->stats.bprc += rd32(E1000_BPRC);
  2651. adapter->stats.mprc += rd32(E1000_MPRC);
  2652. adapter->stats.roc += rd32(E1000_ROC);
  2653. adapter->stats.prc64 += rd32(E1000_PRC64);
  2654. adapter->stats.prc127 += rd32(E1000_PRC127);
  2655. adapter->stats.prc255 += rd32(E1000_PRC255);
  2656. adapter->stats.prc511 += rd32(E1000_PRC511);
  2657. adapter->stats.prc1023 += rd32(E1000_PRC1023);
  2658. adapter->stats.prc1522 += rd32(E1000_PRC1522);
  2659. adapter->stats.symerrs += rd32(E1000_SYMERRS);
  2660. adapter->stats.sec += rd32(E1000_SEC);
  2661. adapter->stats.mpc += rd32(E1000_MPC);
  2662. adapter->stats.scc += rd32(E1000_SCC);
  2663. adapter->stats.ecol += rd32(E1000_ECOL);
  2664. adapter->stats.mcc += rd32(E1000_MCC);
  2665. adapter->stats.latecol += rd32(E1000_LATECOL);
  2666. adapter->stats.dc += rd32(E1000_DC);
  2667. adapter->stats.rlec += rd32(E1000_RLEC);
  2668. adapter->stats.xonrxc += rd32(E1000_XONRXC);
  2669. adapter->stats.xontxc += rd32(E1000_XONTXC);
  2670. adapter->stats.xoffrxc += rd32(E1000_XOFFRXC);
  2671. adapter->stats.xofftxc += rd32(E1000_XOFFTXC);
  2672. adapter->stats.fcruc += rd32(E1000_FCRUC);
  2673. adapter->stats.gptc += rd32(E1000_GPTC);
  2674. adapter->stats.gotc += rd32(E1000_GOTCL);
  2675. rd32(E1000_GOTCH); /* clear GOTCL */
  2676. adapter->stats.rnbc += rd32(E1000_RNBC);
  2677. adapter->stats.ruc += rd32(E1000_RUC);
  2678. adapter->stats.rfc += rd32(E1000_RFC);
  2679. adapter->stats.rjc += rd32(E1000_RJC);
  2680. adapter->stats.tor += rd32(E1000_TORH);
  2681. adapter->stats.tot += rd32(E1000_TOTH);
  2682. adapter->stats.tpr += rd32(E1000_TPR);
  2683. adapter->stats.ptc64 += rd32(E1000_PTC64);
  2684. adapter->stats.ptc127 += rd32(E1000_PTC127);
  2685. adapter->stats.ptc255 += rd32(E1000_PTC255);
  2686. adapter->stats.ptc511 += rd32(E1000_PTC511);
  2687. adapter->stats.ptc1023 += rd32(E1000_PTC1023);
  2688. adapter->stats.ptc1522 += rd32(E1000_PTC1522);
  2689. adapter->stats.mptc += rd32(E1000_MPTC);
  2690. adapter->stats.bptc += rd32(E1000_BPTC);
  2691. /* used for adaptive IFS */
  2692. hw->mac.tx_packet_delta = rd32(E1000_TPT);
  2693. adapter->stats.tpt += hw->mac.tx_packet_delta;
  2694. hw->mac.collision_delta = rd32(E1000_COLC);
  2695. adapter->stats.colc += hw->mac.collision_delta;
  2696. adapter->stats.algnerrc += rd32(E1000_ALGNERRC);
  2697. adapter->stats.rxerrc += rd32(E1000_RXERRC);
  2698. adapter->stats.tncrs += rd32(E1000_TNCRS);
  2699. adapter->stats.tsctc += rd32(E1000_TSCTC);
  2700. adapter->stats.tsctfc += rd32(E1000_TSCTFC);
  2701. adapter->stats.iac += rd32(E1000_IAC);
  2702. adapter->stats.icrxoc += rd32(E1000_ICRXOC);
  2703. adapter->stats.icrxptc += rd32(E1000_ICRXPTC);
  2704. adapter->stats.icrxatc += rd32(E1000_ICRXATC);
  2705. adapter->stats.ictxptc += rd32(E1000_ICTXPTC);
  2706. adapter->stats.ictxatc += rd32(E1000_ICTXATC);
  2707. adapter->stats.ictxqec += rd32(E1000_ICTXQEC);
  2708. adapter->stats.ictxqmtc += rd32(E1000_ICTXQMTC);
  2709. adapter->stats.icrxdmtc += rd32(E1000_ICRXDMTC);
  2710. /* Fill out the OS statistics structure */
  2711. adapter->net_stats.multicast = adapter->stats.mprc;
  2712. adapter->net_stats.collisions = adapter->stats.colc;
  2713. /* Rx Errors */
  2714. /* RLEC on some newer hardware can be incorrect so build
  2715. * our own version based on RUC and ROC */
  2716. adapter->net_stats.rx_errors = adapter->stats.rxerrc +
  2717. adapter->stats.crcerrs + adapter->stats.algnerrc +
  2718. adapter->stats.ruc + adapter->stats.roc +
  2719. adapter->stats.cexterr;
  2720. adapter->net_stats.rx_length_errors = adapter->stats.ruc +
  2721. adapter->stats.roc;
  2722. adapter->net_stats.rx_crc_errors = adapter->stats.crcerrs;
  2723. adapter->net_stats.rx_frame_errors = adapter->stats.algnerrc;
  2724. adapter->net_stats.rx_missed_errors = adapter->stats.mpc;
  2725. /* Tx Errors */
  2726. adapter->net_stats.tx_errors = adapter->stats.ecol +
  2727. adapter->stats.latecol;
  2728. adapter->net_stats.tx_aborted_errors = adapter->stats.ecol;
  2729. adapter->net_stats.tx_window_errors = adapter->stats.latecol;
  2730. adapter->net_stats.tx_carrier_errors = adapter->stats.tncrs;
  2731. /* Tx Dropped needs to be maintained elsewhere */
  2732. /* Phy Stats */
  2733. if (hw->phy.media_type == e1000_media_type_copper) {
  2734. if ((adapter->link_speed == SPEED_1000) &&
  2735. (!hw->phy.ops.read_phy_reg(hw, PHY_1000T_STATUS,
  2736. &phy_tmp))) {
  2737. phy_tmp &= PHY_IDLE_ERROR_COUNT_MASK;
  2738. adapter->phy_stats.idle_errors += phy_tmp;
  2739. }
  2740. }
  2741. /* Management Stats */
  2742. adapter->stats.mgptc += rd32(E1000_MGTPTC);
  2743. adapter->stats.mgprc += rd32(E1000_MGTPRC);
  2744. adapter->stats.mgpdc += rd32(E1000_MGTPDC);
  2745. }
  2746. static irqreturn_t igb_msix_other(int irq, void *data)
  2747. {
  2748. struct net_device *netdev = data;
  2749. struct igb_adapter *adapter = netdev_priv(netdev);
  2750. struct e1000_hw *hw = &adapter->hw;
  2751. u32 icr = rd32(E1000_ICR);
  2752. /* reading ICR causes bit 31 of EICR to be cleared */
  2753. if (!(icr & E1000_ICR_LSC))
  2754. goto no_link_interrupt;
  2755. hw->mac.get_link_status = 1;
  2756. /* guard against interrupt when we're going down */
  2757. if (!test_bit(__IGB_DOWN, &adapter->state))
  2758. mod_timer(&adapter->watchdog_timer, jiffies + 1);
  2759. no_link_interrupt:
  2760. wr32(E1000_IMS, E1000_IMS_LSC);
  2761. wr32(E1000_EIMS, adapter->eims_other);
  2762. return IRQ_HANDLED;
  2763. }
  2764. static irqreturn_t igb_msix_tx(int irq, void *data)
  2765. {
  2766. struct igb_ring *tx_ring = data;
  2767. struct igb_adapter *adapter = tx_ring->adapter;
  2768. struct e1000_hw *hw = &adapter->hw;
  2769. #ifdef CONFIG_DCA
  2770. if (adapter->flags & IGB_FLAG_DCA_ENABLED)
  2771. igb_update_tx_dca(tx_ring);
  2772. #endif
  2773. tx_ring->total_bytes = 0;
  2774. tx_ring->total_packets = 0;
  2775. /* auto mask will automatically reenable the interrupt when we write
  2776. * EICS */
  2777. if (!igb_clean_tx_irq(tx_ring))
  2778. /* Ring was not completely cleaned, so fire another interrupt */
  2779. wr32(E1000_EICS, tx_ring->eims_value);
  2780. else
  2781. wr32(E1000_EIMS, tx_ring->eims_value);
  2782. return IRQ_HANDLED;
  2783. }
  2784. static void igb_write_itr(struct igb_ring *ring)
  2785. {
  2786. struct e1000_hw *hw = &ring->adapter->hw;
  2787. if ((ring->adapter->itr_setting & 3) && ring->set_itr) {
  2788. switch (hw->mac.type) {
  2789. case e1000_82576:
  2790. wr32(ring->itr_register,
  2791. ring->itr_val |
  2792. 0x80000000);
  2793. break;
  2794. default:
  2795. wr32(ring->itr_register,
  2796. ring->itr_val |
  2797. (ring->itr_val << 16));
  2798. break;
  2799. }
  2800. ring->set_itr = 0;
  2801. }
  2802. }
  2803. static irqreturn_t igb_msix_rx(int irq, void *data)
  2804. {
  2805. struct igb_ring *rx_ring = data;
  2806. struct igb_adapter *adapter = rx_ring->adapter;
  2807. /* Write the ITR value calculated at the end of the
  2808. * previous interrupt.
  2809. */
  2810. igb_write_itr(rx_ring);
  2811. if (netif_rx_schedule_prep(adapter->netdev, &rx_ring->napi))
  2812. __netif_rx_schedule(adapter->netdev, &rx_ring->napi);
  2813. #ifdef CONFIG_DCA
  2814. if (adapter->flags & IGB_FLAG_DCA_ENABLED)
  2815. igb_update_rx_dca(rx_ring);
  2816. #endif
  2817. return IRQ_HANDLED;
  2818. }
  2819. #ifdef CONFIG_DCA
  2820. static void igb_update_rx_dca(struct igb_ring *rx_ring)
  2821. {
  2822. u32 dca_rxctrl;
  2823. struct igb_adapter *adapter = rx_ring->adapter;
  2824. struct e1000_hw *hw = &adapter->hw;
  2825. int cpu = get_cpu();
  2826. int q = rx_ring - adapter->rx_ring;
  2827. if (rx_ring->cpu != cpu) {
  2828. dca_rxctrl = rd32(E1000_DCA_RXCTRL(q));
  2829. if (hw->mac.type == e1000_82576) {
  2830. dca_rxctrl &= ~E1000_DCA_RXCTRL_CPUID_MASK_82576;
  2831. dca_rxctrl |= dca_get_tag(cpu) <<
  2832. E1000_DCA_RXCTRL_CPUID_SHIFT;
  2833. } else {
  2834. dca_rxctrl &= ~E1000_DCA_RXCTRL_CPUID_MASK;
  2835. dca_rxctrl |= dca_get_tag(cpu);
  2836. }
  2837. dca_rxctrl |= E1000_DCA_RXCTRL_DESC_DCA_EN;
  2838. dca_rxctrl |= E1000_DCA_RXCTRL_HEAD_DCA_EN;
  2839. dca_rxctrl |= E1000_DCA_RXCTRL_DATA_DCA_EN;
  2840. wr32(E1000_DCA_RXCTRL(q), dca_rxctrl);
  2841. rx_ring->cpu = cpu;
  2842. }
  2843. put_cpu();
  2844. }
  2845. static void igb_update_tx_dca(struct igb_ring *tx_ring)
  2846. {
  2847. u32 dca_txctrl;
  2848. struct igb_adapter *adapter = tx_ring->adapter;
  2849. struct e1000_hw *hw = &adapter->hw;
  2850. int cpu = get_cpu();
  2851. int q = tx_ring - adapter->tx_ring;
  2852. if (tx_ring->cpu != cpu) {
  2853. dca_txctrl = rd32(E1000_DCA_TXCTRL(q));
  2854. if (hw->mac.type == e1000_82576) {
  2855. dca_txctrl &= ~E1000_DCA_TXCTRL_CPUID_MASK_82576;
  2856. dca_txctrl |= dca_get_tag(cpu) <<
  2857. E1000_DCA_TXCTRL_CPUID_SHIFT;
  2858. } else {
  2859. dca_txctrl &= ~E1000_DCA_TXCTRL_CPUID_MASK;
  2860. dca_txctrl |= dca_get_tag(cpu);
  2861. }
  2862. dca_txctrl |= E1000_DCA_TXCTRL_DESC_DCA_EN;
  2863. wr32(E1000_DCA_TXCTRL(q), dca_txctrl);
  2864. tx_ring->cpu = cpu;
  2865. }
  2866. put_cpu();
  2867. }
  2868. static void igb_setup_dca(struct igb_adapter *adapter)
  2869. {
  2870. int i;
  2871. if (!(adapter->flags & IGB_FLAG_DCA_ENABLED))
  2872. return;
  2873. for (i = 0; i < adapter->num_tx_queues; i++) {
  2874. adapter->tx_ring[i].cpu = -1;
  2875. igb_update_tx_dca(&adapter->tx_ring[i]);
  2876. }
  2877. for (i = 0; i < adapter->num_rx_queues; i++) {
  2878. adapter->rx_ring[i].cpu = -1;
  2879. igb_update_rx_dca(&adapter->rx_ring[i]);
  2880. }
  2881. }
  2882. static int __igb_notify_dca(struct device *dev, void *data)
  2883. {
  2884. struct net_device *netdev = dev_get_drvdata(dev);
  2885. struct igb_adapter *adapter = netdev_priv(netdev);
  2886. struct e1000_hw *hw = &adapter->hw;
  2887. unsigned long event = *(unsigned long *)data;
  2888. if (!(adapter->flags & IGB_FLAG_HAS_DCA))
  2889. goto out;
  2890. switch (event) {
  2891. case DCA_PROVIDER_ADD:
  2892. /* if already enabled, don't do it again */
  2893. if (adapter->flags & IGB_FLAG_DCA_ENABLED)
  2894. break;
  2895. adapter->flags |= IGB_FLAG_DCA_ENABLED;
  2896. /* Always use CB2 mode, difference is masked
  2897. * in the CB driver. */
  2898. wr32(E1000_DCA_CTRL, 2);
  2899. if (dca_add_requester(dev) == 0) {
  2900. dev_info(&adapter->pdev->dev, "DCA enabled\n");
  2901. igb_setup_dca(adapter);
  2902. break;
  2903. }
  2904. /* Fall Through since DCA is disabled. */
  2905. case DCA_PROVIDER_REMOVE:
  2906. if (adapter->flags & IGB_FLAG_DCA_ENABLED) {
  2907. /* without this a class_device is left
  2908. * hanging around in the sysfs model */
  2909. dca_remove_requester(dev);
  2910. dev_info(&adapter->pdev->dev, "DCA disabled\n");
  2911. adapter->flags &= ~IGB_FLAG_DCA_ENABLED;
  2912. wr32(E1000_DCA_CTRL, 1);
  2913. }
  2914. break;
  2915. }
  2916. out:
  2917. return 0;
  2918. }
  2919. static int igb_notify_dca(struct notifier_block *nb, unsigned long event,
  2920. void *p)
  2921. {
  2922. int ret_val;
  2923. ret_val = driver_for_each_device(&igb_driver.driver, NULL, &event,
  2924. __igb_notify_dca);
  2925. return ret_val ? NOTIFY_BAD : NOTIFY_DONE;
  2926. }
  2927. #endif /* CONFIG_DCA */
  2928. /**
  2929. * igb_intr_msi - Interrupt Handler
  2930. * @irq: interrupt number
  2931. * @data: pointer to a network interface device structure
  2932. **/
  2933. static irqreturn_t igb_intr_msi(int irq, void *data)
  2934. {
  2935. struct net_device *netdev = data;
  2936. struct igb_adapter *adapter = netdev_priv(netdev);
  2937. struct e1000_hw *hw = &adapter->hw;
  2938. /* read ICR disables interrupts using IAM */
  2939. u32 icr = rd32(E1000_ICR);
  2940. igb_write_itr(adapter->rx_ring);
  2941. if (icr & (E1000_ICR_RXSEQ | E1000_ICR_LSC)) {
  2942. hw->mac.get_link_status = 1;
  2943. if (!test_bit(__IGB_DOWN, &adapter->state))
  2944. mod_timer(&adapter->watchdog_timer, jiffies + 1);
  2945. }
  2946. netif_rx_schedule(netdev, &adapter->rx_ring[0].napi);
  2947. return IRQ_HANDLED;
  2948. }
  2949. /**
  2950. * igb_intr - Interrupt Handler
  2951. * @irq: interrupt number
  2952. * @data: pointer to a network interface device structure
  2953. **/
  2954. static irqreturn_t igb_intr(int irq, void *data)
  2955. {
  2956. struct net_device *netdev = data;
  2957. struct igb_adapter *adapter = netdev_priv(netdev);
  2958. struct e1000_hw *hw = &adapter->hw;
  2959. /* Interrupt Auto-Mask...upon reading ICR, interrupts are masked. No
  2960. * need for the IMC write */
  2961. u32 icr = rd32(E1000_ICR);
  2962. u32 eicr = 0;
  2963. if (!icr)
  2964. return IRQ_NONE; /* Not our interrupt */
  2965. igb_write_itr(adapter->rx_ring);
  2966. /* IMS will not auto-mask if INT_ASSERTED is not set, and if it is
  2967. * not set, then the adapter didn't send an interrupt */
  2968. if (!(icr & E1000_ICR_INT_ASSERTED))
  2969. return IRQ_NONE;
  2970. eicr = rd32(E1000_EICR);
  2971. if (icr & (E1000_ICR_RXSEQ | E1000_ICR_LSC)) {
  2972. hw->mac.get_link_status = 1;
  2973. /* guard against interrupt when we're going down */
  2974. if (!test_bit(__IGB_DOWN, &adapter->state))
  2975. mod_timer(&adapter->watchdog_timer, jiffies + 1);
  2976. }
  2977. netif_rx_schedule(netdev, &adapter->rx_ring[0].napi);
  2978. return IRQ_HANDLED;
  2979. }
  2980. /**
  2981. * igb_poll - NAPI Rx polling callback
  2982. * @napi: napi polling structure
  2983. * @budget: count of how many packets we should handle
  2984. **/
  2985. static int igb_poll(struct napi_struct *napi, int budget)
  2986. {
  2987. struct igb_ring *rx_ring = container_of(napi, struct igb_ring, napi);
  2988. struct igb_adapter *adapter = rx_ring->adapter;
  2989. struct net_device *netdev = adapter->netdev;
  2990. int tx_clean_complete, work_done = 0;
  2991. /* this poll routine only supports one tx and one rx queue */
  2992. #ifdef CONFIG_DCA
  2993. if (adapter->flags & IGB_FLAG_DCA_ENABLED)
  2994. igb_update_tx_dca(&adapter->tx_ring[0]);
  2995. #endif
  2996. tx_clean_complete = igb_clean_tx_irq(&adapter->tx_ring[0]);
  2997. #ifdef CONFIG_DCA
  2998. if (adapter->flags & IGB_FLAG_DCA_ENABLED)
  2999. igb_update_rx_dca(&adapter->rx_ring[0]);
  3000. #endif
  3001. igb_clean_rx_irq_adv(&adapter->rx_ring[0], &work_done, budget);
  3002. /* If no Tx and not enough Rx work done, exit the polling mode */
  3003. if ((tx_clean_complete && (work_done < budget)) ||
  3004. !netif_running(netdev)) {
  3005. if (adapter->itr_setting & 3)
  3006. igb_set_itr(adapter);
  3007. netif_rx_complete(netdev, napi);
  3008. if (!test_bit(__IGB_DOWN, &adapter->state))
  3009. igb_irq_enable(adapter);
  3010. return 0;
  3011. }
  3012. return 1;
  3013. }
  3014. static int igb_clean_rx_ring_msix(struct napi_struct *napi, int budget)
  3015. {
  3016. struct igb_ring *rx_ring = container_of(napi, struct igb_ring, napi);
  3017. struct igb_adapter *adapter = rx_ring->adapter;
  3018. struct e1000_hw *hw = &adapter->hw;
  3019. struct net_device *netdev = adapter->netdev;
  3020. int work_done = 0;
  3021. /* Keep link state information with original netdev */
  3022. if (!netif_carrier_ok(netdev))
  3023. goto quit_polling;
  3024. #ifdef CONFIG_DCA
  3025. if (adapter->flags & IGB_FLAG_DCA_ENABLED)
  3026. igb_update_rx_dca(rx_ring);
  3027. #endif
  3028. igb_clean_rx_irq_adv(rx_ring, &work_done, budget);
  3029. /* If not enough Rx work done, exit the polling mode */
  3030. if ((work_done == 0) || !netif_running(netdev)) {
  3031. quit_polling:
  3032. netif_rx_complete(netdev, napi);
  3033. if (adapter->itr_setting & 3) {
  3034. if (adapter->num_rx_queues == 1)
  3035. igb_set_itr(adapter);
  3036. else
  3037. igb_update_ring_itr(rx_ring);
  3038. }
  3039. if (!test_bit(__IGB_DOWN, &adapter->state))
  3040. wr32(E1000_EIMS, rx_ring->eims_value);
  3041. return 0;
  3042. }
  3043. return 1;
  3044. }
  3045. static inline u32 get_head(struct igb_ring *tx_ring)
  3046. {
  3047. void *end = (struct e1000_tx_desc *)tx_ring->desc + tx_ring->count;
  3048. return le32_to_cpu(*(volatile __le32 *)end);
  3049. }
  3050. /**
  3051. * igb_clean_tx_irq - Reclaim resources after transmit completes
  3052. * @adapter: board private structure
  3053. * returns true if ring is completely cleaned
  3054. **/
  3055. static bool igb_clean_tx_irq(struct igb_ring *tx_ring)
  3056. {
  3057. struct igb_adapter *adapter = tx_ring->adapter;
  3058. struct e1000_hw *hw = &adapter->hw;
  3059. struct net_device *netdev = adapter->netdev;
  3060. struct e1000_tx_desc *tx_desc;
  3061. struct igb_buffer *buffer_info;
  3062. struct sk_buff *skb;
  3063. unsigned int i;
  3064. u32 head, oldhead;
  3065. unsigned int count = 0;
  3066. bool cleaned = false;
  3067. bool retval = true;
  3068. unsigned int total_bytes = 0, total_packets = 0;
  3069. rmb();
  3070. head = get_head(tx_ring);
  3071. i = tx_ring->next_to_clean;
  3072. while (1) {
  3073. while (i != head) {
  3074. cleaned = true;
  3075. tx_desc = E1000_TX_DESC(*tx_ring, i);
  3076. buffer_info = &tx_ring->buffer_info[i];
  3077. skb = buffer_info->skb;
  3078. if (skb) {
  3079. unsigned int segs, bytecount;
  3080. /* gso_segs is currently only valid for tcp */
  3081. segs = skb_shinfo(skb)->gso_segs ?: 1;
  3082. /* multiply data chunks by size of headers */
  3083. bytecount = ((segs - 1) * skb_headlen(skb)) +
  3084. skb->len;
  3085. total_packets += segs;
  3086. total_bytes += bytecount;
  3087. }
  3088. igb_unmap_and_free_tx_resource(adapter, buffer_info);
  3089. tx_desc->upper.data = 0;
  3090. i++;
  3091. if (i == tx_ring->count)
  3092. i = 0;
  3093. count++;
  3094. if (count == IGB_MAX_TX_CLEAN) {
  3095. retval = false;
  3096. goto done_cleaning;
  3097. }
  3098. }
  3099. oldhead = head;
  3100. rmb();
  3101. head = get_head(tx_ring);
  3102. if (head == oldhead)
  3103. goto done_cleaning;
  3104. } /* while (1) */
  3105. done_cleaning:
  3106. tx_ring->next_to_clean = i;
  3107. if (unlikely(cleaned &&
  3108. netif_carrier_ok(netdev) &&
  3109. IGB_DESC_UNUSED(tx_ring) >= IGB_TX_QUEUE_WAKE)) {
  3110. /* Make sure that anybody stopping the queue after this
  3111. * sees the new next_to_clean.
  3112. */
  3113. smp_mb();
  3114. if (__netif_subqueue_stopped(netdev, tx_ring->queue_index) &&
  3115. !(test_bit(__IGB_DOWN, &adapter->state))) {
  3116. netif_wake_subqueue(netdev, tx_ring->queue_index);
  3117. ++adapter->restart_queue;
  3118. }
  3119. }
  3120. if (tx_ring->detect_tx_hung) {
  3121. /* Detect a transmit hang in hardware, this serializes the
  3122. * check with the clearing of time_stamp and movement of i */
  3123. tx_ring->detect_tx_hung = false;
  3124. if (tx_ring->buffer_info[i].time_stamp &&
  3125. time_after(jiffies, tx_ring->buffer_info[i].time_stamp +
  3126. (adapter->tx_timeout_factor * HZ))
  3127. && !(rd32(E1000_STATUS) &
  3128. E1000_STATUS_TXOFF)) {
  3129. tx_desc = E1000_TX_DESC(*tx_ring, i);
  3130. /* detected Tx unit hang */
  3131. dev_err(&adapter->pdev->dev,
  3132. "Detected Tx Unit Hang\n"
  3133. " Tx Queue <%d>\n"
  3134. " TDH <%x>\n"
  3135. " TDT <%x>\n"
  3136. " next_to_use <%x>\n"
  3137. " next_to_clean <%x>\n"
  3138. " head (WB) <%x>\n"
  3139. "buffer_info[next_to_clean]\n"
  3140. " time_stamp <%lx>\n"
  3141. " jiffies <%lx>\n"
  3142. " desc.status <%x>\n",
  3143. tx_ring->queue_index,
  3144. readl(adapter->hw.hw_addr + tx_ring->head),
  3145. readl(adapter->hw.hw_addr + tx_ring->tail),
  3146. tx_ring->next_to_use,
  3147. tx_ring->next_to_clean,
  3148. head,
  3149. tx_ring->buffer_info[i].time_stamp,
  3150. jiffies,
  3151. tx_desc->upper.fields.status);
  3152. netif_stop_subqueue(netdev, tx_ring->queue_index);
  3153. }
  3154. }
  3155. tx_ring->total_bytes += total_bytes;
  3156. tx_ring->total_packets += total_packets;
  3157. tx_ring->tx_stats.bytes += total_bytes;
  3158. tx_ring->tx_stats.packets += total_packets;
  3159. adapter->net_stats.tx_bytes += total_bytes;
  3160. adapter->net_stats.tx_packets += total_packets;
  3161. return retval;
  3162. }
  3163. #ifdef CONFIG_IGB_LRO
  3164. /**
  3165. * igb_get_skb_hdr - helper function for LRO header processing
  3166. * @skb: pointer to sk_buff to be added to LRO packet
  3167. * @iphdr: pointer to ip header structure
  3168. * @tcph: pointer to tcp header structure
  3169. * @hdr_flags: pointer to header flags
  3170. * @priv: pointer to the receive descriptor for the current sk_buff
  3171. **/
  3172. static int igb_get_skb_hdr(struct sk_buff *skb, void **iphdr, void **tcph,
  3173. u64 *hdr_flags, void *priv)
  3174. {
  3175. union e1000_adv_rx_desc *rx_desc = priv;
  3176. u16 pkt_type = rx_desc->wb.lower.lo_dword.pkt_info &
  3177. (E1000_RXDADV_PKTTYPE_IPV4 | E1000_RXDADV_PKTTYPE_TCP);
  3178. /* Verify that this is a valid IPv4 TCP packet */
  3179. if (pkt_type != (E1000_RXDADV_PKTTYPE_IPV4 |
  3180. E1000_RXDADV_PKTTYPE_TCP))
  3181. return -1;
  3182. /* Set network headers */
  3183. skb_reset_network_header(skb);
  3184. skb_set_transport_header(skb, ip_hdrlen(skb));
  3185. *iphdr = ip_hdr(skb);
  3186. *tcph = tcp_hdr(skb);
  3187. *hdr_flags = LRO_IPV4 | LRO_TCP;
  3188. return 0;
  3189. }
  3190. #endif /* CONFIG_IGB_LRO */
  3191. /**
  3192. * igb_receive_skb - helper function to handle rx indications
  3193. * @ring: pointer to receive ring receving this packet
  3194. * @status: descriptor status field as written by hardware
  3195. * @vlan: descriptor vlan field as written by hardware (no le/be conversion)
  3196. * @skb: pointer to sk_buff to be indicated to stack
  3197. **/
  3198. static void igb_receive_skb(struct igb_ring *ring, u8 status,
  3199. union e1000_adv_rx_desc * rx_desc,
  3200. struct sk_buff *skb)
  3201. {
  3202. struct igb_adapter * adapter = ring->adapter;
  3203. bool vlan_extracted = (adapter->vlgrp && (status & E1000_RXD_STAT_VP));
  3204. #ifdef CONFIG_IGB_LRO
  3205. if (adapter->netdev->features & NETIF_F_LRO &&
  3206. skb->ip_summed == CHECKSUM_UNNECESSARY) {
  3207. if (vlan_extracted)
  3208. lro_vlan_hwaccel_receive_skb(&ring->lro_mgr, skb,
  3209. adapter->vlgrp,
  3210. le16_to_cpu(rx_desc->wb.upper.vlan),
  3211. rx_desc);
  3212. else
  3213. lro_receive_skb(&ring->lro_mgr,skb, rx_desc);
  3214. ring->lro_used = 1;
  3215. } else {
  3216. #endif
  3217. if (vlan_extracted)
  3218. vlan_hwaccel_receive_skb(skb, adapter->vlgrp,
  3219. le16_to_cpu(rx_desc->wb.upper.vlan));
  3220. else
  3221. netif_receive_skb(skb);
  3222. #ifdef CONFIG_IGB_LRO
  3223. }
  3224. #endif
  3225. }
  3226. static inline void igb_rx_checksum_adv(struct igb_adapter *adapter,
  3227. u32 status_err, struct sk_buff *skb)
  3228. {
  3229. skb->ip_summed = CHECKSUM_NONE;
  3230. /* Ignore Checksum bit is set or checksum is disabled through ethtool */
  3231. if ((status_err & E1000_RXD_STAT_IXSM) || !adapter->rx_csum)
  3232. return;
  3233. /* TCP/UDP checksum error bit is set */
  3234. if (status_err &
  3235. (E1000_RXDEXT_STATERR_TCPE | E1000_RXDEXT_STATERR_IPE)) {
  3236. /* let the stack verify checksum errors */
  3237. adapter->hw_csum_err++;
  3238. return;
  3239. }
  3240. /* It must be a TCP or UDP packet with a valid checksum */
  3241. if (status_err & (E1000_RXD_STAT_TCPCS | E1000_RXD_STAT_UDPCS))
  3242. skb->ip_summed = CHECKSUM_UNNECESSARY;
  3243. adapter->hw_csum_good++;
  3244. }
  3245. static bool igb_clean_rx_irq_adv(struct igb_ring *rx_ring,
  3246. int *work_done, int budget)
  3247. {
  3248. struct igb_adapter *adapter = rx_ring->adapter;
  3249. struct net_device *netdev = adapter->netdev;
  3250. struct pci_dev *pdev = adapter->pdev;
  3251. union e1000_adv_rx_desc *rx_desc , *next_rxd;
  3252. struct igb_buffer *buffer_info , *next_buffer;
  3253. struct sk_buff *skb;
  3254. unsigned int i;
  3255. u32 length, hlen, staterr;
  3256. bool cleaned = false;
  3257. int cleaned_count = 0;
  3258. unsigned int total_bytes = 0, total_packets = 0;
  3259. i = rx_ring->next_to_clean;
  3260. rx_desc = E1000_RX_DESC_ADV(*rx_ring, i);
  3261. staterr = le32_to_cpu(rx_desc->wb.upper.status_error);
  3262. while (staterr & E1000_RXD_STAT_DD) {
  3263. if (*work_done >= budget)
  3264. break;
  3265. (*work_done)++;
  3266. buffer_info = &rx_ring->buffer_info[i];
  3267. /* HW will not DMA in data larger than the given buffer, even
  3268. * if it parses the (NFS, of course) header to be larger. In
  3269. * that case, it fills the header buffer and spills the rest
  3270. * into the page.
  3271. */
  3272. hlen = (le16_to_cpu(rx_desc->wb.lower.lo_dword.hdr_info) &
  3273. E1000_RXDADV_HDRBUFLEN_MASK) >> E1000_RXDADV_HDRBUFLEN_SHIFT;
  3274. if (hlen > adapter->rx_ps_hdr_size)
  3275. hlen = adapter->rx_ps_hdr_size;
  3276. length = le16_to_cpu(rx_desc->wb.upper.length);
  3277. cleaned = true;
  3278. cleaned_count++;
  3279. skb = buffer_info->skb;
  3280. prefetch(skb->data - NET_IP_ALIGN);
  3281. buffer_info->skb = NULL;
  3282. if (!adapter->rx_ps_hdr_size) {
  3283. pci_unmap_single(pdev, buffer_info->dma,
  3284. adapter->rx_buffer_len +
  3285. NET_IP_ALIGN,
  3286. PCI_DMA_FROMDEVICE);
  3287. skb_put(skb, length);
  3288. goto send_up;
  3289. }
  3290. if (!skb_shinfo(skb)->nr_frags) {
  3291. pci_unmap_single(pdev, buffer_info->dma,
  3292. adapter->rx_ps_hdr_size +
  3293. NET_IP_ALIGN,
  3294. PCI_DMA_FROMDEVICE);
  3295. skb_put(skb, hlen);
  3296. }
  3297. if (length) {
  3298. pci_unmap_page(pdev, buffer_info->page_dma,
  3299. PAGE_SIZE / 2, PCI_DMA_FROMDEVICE);
  3300. buffer_info->page_dma = 0;
  3301. skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags++,
  3302. buffer_info->page,
  3303. buffer_info->page_offset,
  3304. length);
  3305. if ((adapter->rx_buffer_len > (PAGE_SIZE / 2)) ||
  3306. (page_count(buffer_info->page) != 1))
  3307. buffer_info->page = NULL;
  3308. else
  3309. get_page(buffer_info->page);
  3310. skb->len += length;
  3311. skb->data_len += length;
  3312. skb->truesize += length;
  3313. }
  3314. send_up:
  3315. i++;
  3316. if (i == rx_ring->count)
  3317. i = 0;
  3318. next_rxd = E1000_RX_DESC_ADV(*rx_ring, i);
  3319. prefetch(next_rxd);
  3320. next_buffer = &rx_ring->buffer_info[i];
  3321. if (!(staterr & E1000_RXD_STAT_EOP)) {
  3322. buffer_info->skb = xchg(&next_buffer->skb, skb);
  3323. buffer_info->dma = xchg(&next_buffer->dma, 0);
  3324. goto next_desc;
  3325. }
  3326. if (staterr & E1000_RXDEXT_ERR_FRAME_ERR_MASK) {
  3327. dev_kfree_skb_irq(skb);
  3328. goto next_desc;
  3329. }
  3330. total_bytes += skb->len;
  3331. total_packets++;
  3332. igb_rx_checksum_adv(adapter, staterr, skb);
  3333. skb->protocol = eth_type_trans(skb, netdev);
  3334. igb_receive_skb(rx_ring, staterr, rx_desc, skb);
  3335. netdev->last_rx = jiffies;
  3336. next_desc:
  3337. rx_desc->wb.upper.status_error = 0;
  3338. /* return some buffers to hardware, one at a time is too slow */
  3339. if (cleaned_count >= IGB_RX_BUFFER_WRITE) {
  3340. igb_alloc_rx_buffers_adv(rx_ring, cleaned_count);
  3341. cleaned_count = 0;
  3342. }
  3343. /* use prefetched values */
  3344. rx_desc = next_rxd;
  3345. buffer_info = next_buffer;
  3346. staterr = le32_to_cpu(rx_desc->wb.upper.status_error);
  3347. }
  3348. rx_ring->next_to_clean = i;
  3349. cleaned_count = IGB_DESC_UNUSED(rx_ring);
  3350. #ifdef CONFIG_IGB_LRO
  3351. if (rx_ring->lro_used) {
  3352. lro_flush_all(&rx_ring->lro_mgr);
  3353. rx_ring->lro_used = 0;
  3354. }
  3355. #endif
  3356. if (cleaned_count)
  3357. igb_alloc_rx_buffers_adv(rx_ring, cleaned_count);
  3358. rx_ring->total_packets += total_packets;
  3359. rx_ring->total_bytes += total_bytes;
  3360. rx_ring->rx_stats.packets += total_packets;
  3361. rx_ring->rx_stats.bytes += total_bytes;
  3362. adapter->net_stats.rx_bytes += total_bytes;
  3363. adapter->net_stats.rx_packets += total_packets;
  3364. return cleaned;
  3365. }
  3366. /**
  3367. * igb_alloc_rx_buffers_adv - Replace used receive buffers; packet split
  3368. * @adapter: address of board private structure
  3369. **/
  3370. static void igb_alloc_rx_buffers_adv(struct igb_ring *rx_ring,
  3371. int cleaned_count)
  3372. {
  3373. struct igb_adapter *adapter = rx_ring->adapter;
  3374. struct net_device *netdev = adapter->netdev;
  3375. struct pci_dev *pdev = adapter->pdev;
  3376. union e1000_adv_rx_desc *rx_desc;
  3377. struct igb_buffer *buffer_info;
  3378. struct sk_buff *skb;
  3379. unsigned int i;
  3380. i = rx_ring->next_to_use;
  3381. buffer_info = &rx_ring->buffer_info[i];
  3382. while (cleaned_count--) {
  3383. rx_desc = E1000_RX_DESC_ADV(*rx_ring, i);
  3384. if (adapter->rx_ps_hdr_size && !buffer_info->page_dma) {
  3385. if (!buffer_info->page) {
  3386. buffer_info->page = alloc_page(GFP_ATOMIC);
  3387. if (!buffer_info->page) {
  3388. adapter->alloc_rx_buff_failed++;
  3389. goto no_buffers;
  3390. }
  3391. buffer_info->page_offset = 0;
  3392. } else {
  3393. buffer_info->page_offset ^= PAGE_SIZE / 2;
  3394. }
  3395. buffer_info->page_dma =
  3396. pci_map_page(pdev,
  3397. buffer_info->page,
  3398. buffer_info->page_offset,
  3399. PAGE_SIZE / 2,
  3400. PCI_DMA_FROMDEVICE);
  3401. }
  3402. if (!buffer_info->skb) {
  3403. int bufsz;
  3404. if (adapter->rx_ps_hdr_size)
  3405. bufsz = adapter->rx_ps_hdr_size;
  3406. else
  3407. bufsz = adapter->rx_buffer_len;
  3408. bufsz += NET_IP_ALIGN;
  3409. skb = netdev_alloc_skb(netdev, bufsz);
  3410. if (!skb) {
  3411. adapter->alloc_rx_buff_failed++;
  3412. goto no_buffers;
  3413. }
  3414. /* Make buffer alignment 2 beyond a 16 byte boundary
  3415. * this will result in a 16 byte aligned IP header after
  3416. * the 14 byte MAC header is removed
  3417. */
  3418. skb_reserve(skb, NET_IP_ALIGN);
  3419. buffer_info->skb = skb;
  3420. buffer_info->dma = pci_map_single(pdev, skb->data,
  3421. bufsz,
  3422. PCI_DMA_FROMDEVICE);
  3423. }
  3424. /* Refresh the desc even if buffer_addrs didn't change because
  3425. * each write-back erases this info. */
  3426. if (adapter->rx_ps_hdr_size) {
  3427. rx_desc->read.pkt_addr =
  3428. cpu_to_le64(buffer_info->page_dma);
  3429. rx_desc->read.hdr_addr = cpu_to_le64(buffer_info->dma);
  3430. } else {
  3431. rx_desc->read.pkt_addr =
  3432. cpu_to_le64(buffer_info->dma);
  3433. rx_desc->read.hdr_addr = 0;
  3434. }
  3435. i++;
  3436. if (i == rx_ring->count)
  3437. i = 0;
  3438. buffer_info = &rx_ring->buffer_info[i];
  3439. }
  3440. no_buffers:
  3441. if (rx_ring->next_to_use != i) {
  3442. rx_ring->next_to_use = i;
  3443. if (i == 0)
  3444. i = (rx_ring->count - 1);
  3445. else
  3446. i--;
  3447. /* Force memory writes to complete before letting h/w
  3448. * know there are new descriptors to fetch. (Only
  3449. * applicable for weak-ordered memory model archs,
  3450. * such as IA-64). */
  3451. wmb();
  3452. writel(i, adapter->hw.hw_addr + rx_ring->tail);
  3453. }
  3454. }
  3455. /**
  3456. * igb_mii_ioctl -
  3457. * @netdev:
  3458. * @ifreq:
  3459. * @cmd:
  3460. **/
  3461. static int igb_mii_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
  3462. {
  3463. struct igb_adapter *adapter = netdev_priv(netdev);
  3464. struct mii_ioctl_data *data = if_mii(ifr);
  3465. if (adapter->hw.phy.media_type != e1000_media_type_copper)
  3466. return -EOPNOTSUPP;
  3467. switch (cmd) {
  3468. case SIOCGMIIPHY:
  3469. data->phy_id = adapter->hw.phy.addr;
  3470. break;
  3471. case SIOCGMIIREG:
  3472. if (!capable(CAP_NET_ADMIN))
  3473. return -EPERM;
  3474. if (adapter->hw.phy.ops.read_phy_reg(&adapter->hw,
  3475. data->reg_num
  3476. & 0x1F, &data->val_out))
  3477. return -EIO;
  3478. break;
  3479. case SIOCSMIIREG:
  3480. default:
  3481. return -EOPNOTSUPP;
  3482. }
  3483. return 0;
  3484. }
  3485. /**
  3486. * igb_ioctl -
  3487. * @netdev:
  3488. * @ifreq:
  3489. * @cmd:
  3490. **/
  3491. static int igb_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
  3492. {
  3493. switch (cmd) {
  3494. case SIOCGMIIPHY:
  3495. case SIOCGMIIREG:
  3496. case SIOCSMIIREG:
  3497. return igb_mii_ioctl(netdev, ifr, cmd);
  3498. default:
  3499. return -EOPNOTSUPP;
  3500. }
  3501. }
  3502. static void igb_vlan_rx_register(struct net_device *netdev,
  3503. struct vlan_group *grp)
  3504. {
  3505. struct igb_adapter *adapter = netdev_priv(netdev);
  3506. struct e1000_hw *hw = &adapter->hw;
  3507. u32 ctrl, rctl;
  3508. igb_irq_disable(adapter);
  3509. adapter->vlgrp = grp;
  3510. if (grp) {
  3511. /* enable VLAN tag insert/strip */
  3512. ctrl = rd32(E1000_CTRL);
  3513. ctrl |= E1000_CTRL_VME;
  3514. wr32(E1000_CTRL, ctrl);
  3515. /* enable VLAN receive filtering */
  3516. rctl = rd32(E1000_RCTL);
  3517. rctl &= ~E1000_RCTL_CFIEN;
  3518. wr32(E1000_RCTL, rctl);
  3519. igb_update_mng_vlan(adapter);
  3520. wr32(E1000_RLPML,
  3521. adapter->max_frame_size + VLAN_TAG_SIZE);
  3522. } else {
  3523. /* disable VLAN tag insert/strip */
  3524. ctrl = rd32(E1000_CTRL);
  3525. ctrl &= ~E1000_CTRL_VME;
  3526. wr32(E1000_CTRL, ctrl);
  3527. if (adapter->mng_vlan_id != (u16)IGB_MNG_VLAN_NONE) {
  3528. igb_vlan_rx_kill_vid(netdev, adapter->mng_vlan_id);
  3529. adapter->mng_vlan_id = IGB_MNG_VLAN_NONE;
  3530. }
  3531. wr32(E1000_RLPML,
  3532. adapter->max_frame_size);
  3533. }
  3534. if (!test_bit(__IGB_DOWN, &adapter->state))
  3535. igb_irq_enable(adapter);
  3536. }
  3537. static void igb_vlan_rx_add_vid(struct net_device *netdev, u16 vid)
  3538. {
  3539. struct igb_adapter *adapter = netdev_priv(netdev);
  3540. struct e1000_hw *hw = &adapter->hw;
  3541. u32 vfta, index;
  3542. if ((adapter->hw.mng_cookie.status &
  3543. E1000_MNG_DHCP_COOKIE_STATUS_VLAN) &&
  3544. (vid == adapter->mng_vlan_id))
  3545. return;
  3546. /* add VID to filter table */
  3547. index = (vid >> 5) & 0x7F;
  3548. vfta = array_rd32(E1000_VFTA, index);
  3549. vfta |= (1 << (vid & 0x1F));
  3550. igb_write_vfta(&adapter->hw, index, vfta);
  3551. }
  3552. static void igb_vlan_rx_kill_vid(struct net_device *netdev, u16 vid)
  3553. {
  3554. struct igb_adapter *adapter = netdev_priv(netdev);
  3555. struct e1000_hw *hw = &adapter->hw;
  3556. u32 vfta, index;
  3557. igb_irq_disable(adapter);
  3558. vlan_group_set_device(adapter->vlgrp, vid, NULL);
  3559. if (!test_bit(__IGB_DOWN, &adapter->state))
  3560. igb_irq_enable(adapter);
  3561. if ((adapter->hw.mng_cookie.status &
  3562. E1000_MNG_DHCP_COOKIE_STATUS_VLAN) &&
  3563. (vid == adapter->mng_vlan_id)) {
  3564. /* release control to f/w */
  3565. igb_release_hw_control(adapter);
  3566. return;
  3567. }
  3568. /* remove VID from filter table */
  3569. index = (vid >> 5) & 0x7F;
  3570. vfta = array_rd32(E1000_VFTA, index);
  3571. vfta &= ~(1 << (vid & 0x1F));
  3572. igb_write_vfta(&adapter->hw, index, vfta);
  3573. }
  3574. static void igb_restore_vlan(struct igb_adapter *adapter)
  3575. {
  3576. igb_vlan_rx_register(adapter->netdev, adapter->vlgrp);
  3577. if (adapter->vlgrp) {
  3578. u16 vid;
  3579. for (vid = 0; vid < VLAN_GROUP_ARRAY_LEN; vid++) {
  3580. if (!vlan_group_get_device(adapter->vlgrp, vid))
  3581. continue;
  3582. igb_vlan_rx_add_vid(adapter->netdev, vid);
  3583. }
  3584. }
  3585. }
  3586. int igb_set_spd_dplx(struct igb_adapter *adapter, u16 spddplx)
  3587. {
  3588. struct e1000_mac_info *mac = &adapter->hw.mac;
  3589. mac->autoneg = 0;
  3590. /* Fiber NICs only allow 1000 gbps Full duplex */
  3591. if ((adapter->hw.phy.media_type == e1000_media_type_fiber) &&
  3592. spddplx != (SPEED_1000 + DUPLEX_FULL)) {
  3593. dev_err(&adapter->pdev->dev,
  3594. "Unsupported Speed/Duplex configuration\n");
  3595. return -EINVAL;
  3596. }
  3597. switch (spddplx) {
  3598. case SPEED_10 + DUPLEX_HALF:
  3599. mac->forced_speed_duplex = ADVERTISE_10_HALF;
  3600. break;
  3601. case SPEED_10 + DUPLEX_FULL:
  3602. mac->forced_speed_duplex = ADVERTISE_10_FULL;
  3603. break;
  3604. case SPEED_100 + DUPLEX_HALF:
  3605. mac->forced_speed_duplex = ADVERTISE_100_HALF;
  3606. break;
  3607. case SPEED_100 + DUPLEX_FULL:
  3608. mac->forced_speed_duplex = ADVERTISE_100_FULL;
  3609. break;
  3610. case SPEED_1000 + DUPLEX_FULL:
  3611. mac->autoneg = 1;
  3612. adapter->hw.phy.autoneg_advertised = ADVERTISE_1000_FULL;
  3613. break;
  3614. case SPEED_1000 + DUPLEX_HALF: /* not supported */
  3615. default:
  3616. dev_err(&adapter->pdev->dev,
  3617. "Unsupported Speed/Duplex configuration\n");
  3618. return -EINVAL;
  3619. }
  3620. return 0;
  3621. }
  3622. static int igb_suspend(struct pci_dev *pdev, pm_message_t state)
  3623. {
  3624. struct net_device *netdev = pci_get_drvdata(pdev);
  3625. struct igb_adapter *adapter = netdev_priv(netdev);
  3626. struct e1000_hw *hw = &adapter->hw;
  3627. u32 ctrl, rctl, status;
  3628. u32 wufc = adapter->wol;
  3629. #ifdef CONFIG_PM
  3630. int retval = 0;
  3631. #endif
  3632. netif_device_detach(netdev);
  3633. if (netif_running(netdev))
  3634. igb_close(netdev);
  3635. igb_reset_interrupt_capability(adapter);
  3636. igb_free_queues(adapter);
  3637. #ifdef CONFIG_PM
  3638. retval = pci_save_state(pdev);
  3639. if (retval)
  3640. return retval;
  3641. #endif
  3642. status = rd32(E1000_STATUS);
  3643. if (status & E1000_STATUS_LU)
  3644. wufc &= ~E1000_WUFC_LNKC;
  3645. if (wufc) {
  3646. igb_setup_rctl(adapter);
  3647. igb_set_multi(netdev);
  3648. /* turn on all-multi mode if wake on multicast is enabled */
  3649. if (wufc & E1000_WUFC_MC) {
  3650. rctl = rd32(E1000_RCTL);
  3651. rctl |= E1000_RCTL_MPE;
  3652. wr32(E1000_RCTL, rctl);
  3653. }
  3654. ctrl = rd32(E1000_CTRL);
  3655. /* advertise wake from D3Cold */
  3656. #define E1000_CTRL_ADVD3WUC 0x00100000
  3657. /* phy power management enable */
  3658. #define E1000_CTRL_EN_PHY_PWR_MGMT 0x00200000
  3659. ctrl |= E1000_CTRL_ADVD3WUC;
  3660. wr32(E1000_CTRL, ctrl);
  3661. /* Allow time for pending master requests to run */
  3662. igb_disable_pcie_master(&adapter->hw);
  3663. wr32(E1000_WUC, E1000_WUC_PME_EN);
  3664. wr32(E1000_WUFC, wufc);
  3665. } else {
  3666. wr32(E1000_WUC, 0);
  3667. wr32(E1000_WUFC, 0);
  3668. }
  3669. /* make sure adapter isn't asleep if manageability/wol is enabled */
  3670. if (wufc || adapter->en_mng_pt) {
  3671. pci_enable_wake(pdev, PCI_D3hot, 1);
  3672. pci_enable_wake(pdev, PCI_D3cold, 1);
  3673. } else {
  3674. igb_shutdown_fiber_serdes_link_82575(hw);
  3675. pci_enable_wake(pdev, PCI_D3hot, 0);
  3676. pci_enable_wake(pdev, PCI_D3cold, 0);
  3677. }
  3678. /* Release control of h/w to f/w. If f/w is AMT enabled, this
  3679. * would have already happened in close and is redundant. */
  3680. igb_release_hw_control(adapter);
  3681. pci_disable_device(pdev);
  3682. pci_set_power_state(pdev, pci_choose_state(pdev, state));
  3683. return 0;
  3684. }
  3685. #ifdef CONFIG_PM
  3686. static int igb_resume(struct pci_dev *pdev)
  3687. {
  3688. struct net_device *netdev = pci_get_drvdata(pdev);
  3689. struct igb_adapter *adapter = netdev_priv(netdev);
  3690. struct e1000_hw *hw = &adapter->hw;
  3691. u32 err;
  3692. pci_set_power_state(pdev, PCI_D0);
  3693. pci_restore_state(pdev);
  3694. if (adapter->need_ioport)
  3695. err = pci_enable_device(pdev);
  3696. else
  3697. err = pci_enable_device_mem(pdev);
  3698. if (err) {
  3699. dev_err(&pdev->dev,
  3700. "igb: Cannot enable PCI device from suspend\n");
  3701. return err;
  3702. }
  3703. pci_set_master(pdev);
  3704. pci_enable_wake(pdev, PCI_D3hot, 0);
  3705. pci_enable_wake(pdev, PCI_D3cold, 0);
  3706. igb_set_interrupt_capability(adapter);
  3707. if (igb_alloc_queues(adapter)) {
  3708. dev_err(&pdev->dev, "Unable to allocate memory for queues\n");
  3709. return -ENOMEM;
  3710. }
  3711. /* e1000_power_up_phy(adapter); */
  3712. igb_reset(adapter);
  3713. wr32(E1000_WUS, ~0);
  3714. if (netif_running(netdev)) {
  3715. err = igb_open(netdev);
  3716. if (err)
  3717. return err;
  3718. }
  3719. netif_device_attach(netdev);
  3720. /* let the f/w know that the h/w is now under the control of the
  3721. * driver. */
  3722. igb_get_hw_control(adapter);
  3723. return 0;
  3724. }
  3725. #endif
  3726. static void igb_shutdown(struct pci_dev *pdev)
  3727. {
  3728. igb_suspend(pdev, PMSG_SUSPEND);
  3729. }
  3730. #ifdef CONFIG_NET_POLL_CONTROLLER
  3731. /*
  3732. * Polling 'interrupt' - used by things like netconsole to send skbs
  3733. * without having to re-enable interrupts. It's not called while
  3734. * the interrupt routine is executing.
  3735. */
  3736. static void igb_netpoll(struct net_device *netdev)
  3737. {
  3738. struct igb_adapter *adapter = netdev_priv(netdev);
  3739. int i;
  3740. int work_done = 0;
  3741. igb_irq_disable(adapter);
  3742. adapter->flags |= IGB_FLAG_IN_NETPOLL;
  3743. for (i = 0; i < adapter->num_tx_queues; i++)
  3744. igb_clean_tx_irq(&adapter->tx_ring[i]);
  3745. for (i = 0; i < adapter->num_rx_queues; i++)
  3746. igb_clean_rx_irq_adv(&adapter->rx_ring[i],
  3747. &work_done,
  3748. adapter->rx_ring[i].napi.weight);
  3749. adapter->flags &= ~IGB_FLAG_IN_NETPOLL;
  3750. igb_irq_enable(adapter);
  3751. }
  3752. #endif /* CONFIG_NET_POLL_CONTROLLER */
  3753. /**
  3754. * igb_io_error_detected - called when PCI error is detected
  3755. * @pdev: Pointer to PCI device
  3756. * @state: The current pci connection state
  3757. *
  3758. * This function is called after a PCI bus error affecting
  3759. * this device has been detected.
  3760. */
  3761. static pci_ers_result_t igb_io_error_detected(struct pci_dev *pdev,
  3762. pci_channel_state_t state)
  3763. {
  3764. struct net_device *netdev = pci_get_drvdata(pdev);
  3765. struct igb_adapter *adapter = netdev_priv(netdev);
  3766. netif_device_detach(netdev);
  3767. if (netif_running(netdev))
  3768. igb_down(adapter);
  3769. pci_disable_device(pdev);
  3770. /* Request a slot slot reset. */
  3771. return PCI_ERS_RESULT_NEED_RESET;
  3772. }
  3773. /**
  3774. * igb_io_slot_reset - called after the pci bus has been reset.
  3775. * @pdev: Pointer to PCI device
  3776. *
  3777. * Restart the card from scratch, as if from a cold-boot. Implementation
  3778. * resembles the first-half of the igb_resume routine.
  3779. */
  3780. static pci_ers_result_t igb_io_slot_reset(struct pci_dev *pdev)
  3781. {
  3782. struct net_device *netdev = pci_get_drvdata(pdev);
  3783. struct igb_adapter *adapter = netdev_priv(netdev);
  3784. struct e1000_hw *hw = &adapter->hw;
  3785. int err;
  3786. if (adapter->need_ioport)
  3787. err = pci_enable_device(pdev);
  3788. else
  3789. err = pci_enable_device_mem(pdev);
  3790. if (err) {
  3791. dev_err(&pdev->dev,
  3792. "Cannot re-enable PCI device after reset.\n");
  3793. return PCI_ERS_RESULT_DISCONNECT;
  3794. }
  3795. pci_set_master(pdev);
  3796. pci_restore_state(pdev);
  3797. pci_enable_wake(pdev, PCI_D3hot, 0);
  3798. pci_enable_wake(pdev, PCI_D3cold, 0);
  3799. igb_reset(adapter);
  3800. wr32(E1000_WUS, ~0);
  3801. return PCI_ERS_RESULT_RECOVERED;
  3802. }
  3803. /**
  3804. * igb_io_resume - called when traffic can start flowing again.
  3805. * @pdev: Pointer to PCI device
  3806. *
  3807. * This callback is called when the error recovery driver tells us that
  3808. * its OK to resume normal operation. Implementation resembles the
  3809. * second-half of the igb_resume routine.
  3810. */
  3811. static void igb_io_resume(struct pci_dev *pdev)
  3812. {
  3813. struct net_device *netdev = pci_get_drvdata(pdev);
  3814. struct igb_adapter *adapter = netdev_priv(netdev);
  3815. if (netif_running(netdev)) {
  3816. if (igb_up(adapter)) {
  3817. dev_err(&pdev->dev, "igb_up failed after reset\n");
  3818. return;
  3819. }
  3820. }
  3821. netif_device_attach(netdev);
  3822. /* let the f/w know that the h/w is now under the control of the
  3823. * driver. */
  3824. igb_get_hw_control(adapter);
  3825. }
  3826. /* igb_main.c */