e1000_main.c 136 KB

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