igb_main.c 149 KB

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