igb_main.c 123 KB

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