netdev.c 130 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835
  1. /*******************************************************************************
  2. Intel PRO/1000 Linux driver
  3. Copyright(c) 1999 - 2008 Intel Corporation.
  4. This program is free software; you can redistribute it and/or modify it
  5. under the terms and conditions of the GNU General Public License,
  6. version 2, as published by the Free Software Foundation.
  7. This program is distributed in the hope it will be useful, but WITHOUT
  8. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  9. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  10. more details.
  11. You should have received a copy of the GNU General Public License along with
  12. this program; if not, write to the Free Software Foundation, Inc.,
  13. 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  14. The full GNU General Public License is included in this distribution in
  15. the file called "COPYING".
  16. Contact Information:
  17. Linux NICS <linux.nics@intel.com>
  18. e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
  19. Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  20. *******************************************************************************/
  21. #include <linux/module.h>
  22. #include <linux/types.h>
  23. #include <linux/init.h>
  24. #include <linux/pci.h>
  25. #include <linux/vmalloc.h>
  26. #include <linux/pagemap.h>
  27. #include <linux/delay.h>
  28. #include <linux/netdevice.h>
  29. #include <linux/tcp.h>
  30. #include <linux/ipv6.h>
  31. #include <net/checksum.h>
  32. #include <net/ip6_checksum.h>
  33. #include <linux/mii.h>
  34. #include <linux/ethtool.h>
  35. #include <linux/if_vlan.h>
  36. #include <linux/cpu.h>
  37. #include <linux/smp.h>
  38. #include <linux/pm_qos_params.h>
  39. #include "e1000.h"
  40. #define DRV_VERSION "0.3.3.3-k6"
  41. char e1000e_driver_name[] = "e1000e";
  42. const char e1000e_driver_version[] = DRV_VERSION;
  43. static const struct e1000_info *e1000_info_tbl[] = {
  44. [board_82571] = &e1000_82571_info,
  45. [board_82572] = &e1000_82572_info,
  46. [board_82573] = &e1000_82573_info,
  47. [board_80003es2lan] = &e1000_es2_info,
  48. [board_ich8lan] = &e1000_ich8_info,
  49. [board_ich9lan] = &e1000_ich9_info,
  50. };
  51. #ifdef DEBUG
  52. /**
  53. * e1000_get_hw_dev_name - return device name string
  54. * used by hardware layer to print debugging information
  55. **/
  56. char *e1000e_get_hw_dev_name(struct e1000_hw *hw)
  57. {
  58. return hw->adapter->netdev->name;
  59. }
  60. #endif
  61. /**
  62. * e1000_desc_unused - calculate if we have unused descriptors
  63. **/
  64. static int e1000_desc_unused(struct e1000_ring *ring)
  65. {
  66. if (ring->next_to_clean > ring->next_to_use)
  67. return ring->next_to_clean - ring->next_to_use - 1;
  68. return ring->count + ring->next_to_clean - ring->next_to_use - 1;
  69. }
  70. /**
  71. * e1000_receive_skb - helper function to handle Rx indications
  72. * @adapter: board private structure
  73. * @status: descriptor status field as written by hardware
  74. * @vlan: descriptor vlan field as written by hardware (no le/be conversion)
  75. * @skb: pointer to sk_buff to be indicated to stack
  76. **/
  77. static void e1000_receive_skb(struct e1000_adapter *adapter,
  78. struct net_device *netdev,
  79. struct sk_buff *skb,
  80. u8 status, __le16 vlan)
  81. {
  82. skb->protocol = eth_type_trans(skb, netdev);
  83. if (adapter->vlgrp && (status & E1000_RXD_STAT_VP))
  84. vlan_hwaccel_receive_skb(skb, adapter->vlgrp,
  85. le16_to_cpu(vlan));
  86. else
  87. netif_receive_skb(skb);
  88. netdev->last_rx = jiffies;
  89. }
  90. /**
  91. * e1000_rx_checksum - Receive Checksum Offload for 82543
  92. * @adapter: board private structure
  93. * @status_err: receive descriptor status and error fields
  94. * @csum: receive descriptor csum field
  95. * @sk_buff: socket buffer with received data
  96. **/
  97. static void e1000_rx_checksum(struct e1000_adapter *adapter, u32 status_err,
  98. u32 csum, struct sk_buff *skb)
  99. {
  100. u16 status = (u16)status_err;
  101. u8 errors = (u8)(status_err >> 24);
  102. skb->ip_summed = CHECKSUM_NONE;
  103. /* Ignore Checksum bit is set */
  104. if (status & E1000_RXD_STAT_IXSM)
  105. return;
  106. /* TCP/UDP checksum error bit is set */
  107. if (errors & E1000_RXD_ERR_TCPE) {
  108. /* let the stack verify checksum errors */
  109. adapter->hw_csum_err++;
  110. return;
  111. }
  112. /* TCP/UDP Checksum has not been calculated */
  113. if (!(status & (E1000_RXD_STAT_TCPCS | E1000_RXD_STAT_UDPCS)))
  114. return;
  115. /* It must be a TCP or UDP packet with a valid checksum */
  116. if (status & E1000_RXD_STAT_TCPCS) {
  117. /* TCP checksum is good */
  118. skb->ip_summed = CHECKSUM_UNNECESSARY;
  119. } else {
  120. /*
  121. * IP fragment with UDP payload
  122. * Hardware complements the payload checksum, so we undo it
  123. * and then put the value in host order for further stack use.
  124. */
  125. __sum16 sum = (__force __sum16)htons(csum);
  126. skb->csum = csum_unfold(~sum);
  127. skb->ip_summed = CHECKSUM_COMPLETE;
  128. }
  129. adapter->hw_csum_good++;
  130. }
  131. /**
  132. * e1000_alloc_rx_buffers - Replace used receive buffers; legacy & extended
  133. * @adapter: address of board private structure
  134. **/
  135. static void e1000_alloc_rx_buffers(struct e1000_adapter *adapter,
  136. int cleaned_count)
  137. {
  138. struct net_device *netdev = adapter->netdev;
  139. struct pci_dev *pdev = adapter->pdev;
  140. struct e1000_ring *rx_ring = adapter->rx_ring;
  141. struct e1000_rx_desc *rx_desc;
  142. struct e1000_buffer *buffer_info;
  143. struct sk_buff *skb;
  144. unsigned int i;
  145. unsigned int bufsz = adapter->rx_buffer_len + NET_IP_ALIGN;
  146. i = rx_ring->next_to_use;
  147. buffer_info = &rx_ring->buffer_info[i];
  148. while (cleaned_count--) {
  149. skb = buffer_info->skb;
  150. if (skb) {
  151. skb_trim(skb, 0);
  152. goto map_skb;
  153. }
  154. skb = netdev_alloc_skb(netdev, bufsz);
  155. if (!skb) {
  156. /* Better luck next round */
  157. adapter->alloc_rx_buff_failed++;
  158. break;
  159. }
  160. /*
  161. * Make buffer alignment 2 beyond a 16 byte boundary
  162. * this will result in a 16 byte aligned IP header after
  163. * the 14 byte MAC header is removed
  164. */
  165. skb_reserve(skb, NET_IP_ALIGN);
  166. buffer_info->skb = skb;
  167. map_skb:
  168. buffer_info->dma = pci_map_single(pdev, skb->data,
  169. adapter->rx_buffer_len,
  170. PCI_DMA_FROMDEVICE);
  171. if (pci_dma_mapping_error(pdev, buffer_info->dma)) {
  172. dev_err(&pdev->dev, "RX DMA map failed\n");
  173. adapter->rx_dma_failed++;
  174. break;
  175. }
  176. rx_desc = E1000_RX_DESC(*rx_ring, i);
  177. rx_desc->buffer_addr = cpu_to_le64(buffer_info->dma);
  178. i++;
  179. if (i == rx_ring->count)
  180. i = 0;
  181. buffer_info = &rx_ring->buffer_info[i];
  182. }
  183. if (rx_ring->next_to_use != i) {
  184. rx_ring->next_to_use = i;
  185. if (i-- == 0)
  186. i = (rx_ring->count - 1);
  187. /*
  188. * Force memory writes to complete before letting h/w
  189. * know there are new descriptors to fetch. (Only
  190. * applicable for weak-ordered memory model archs,
  191. * such as IA-64).
  192. */
  193. wmb();
  194. writel(i, adapter->hw.hw_addr + rx_ring->tail);
  195. }
  196. }
  197. /**
  198. * e1000_alloc_rx_buffers_ps - Replace used receive buffers; packet split
  199. * @adapter: address of board private structure
  200. **/
  201. static void e1000_alloc_rx_buffers_ps(struct e1000_adapter *adapter,
  202. int cleaned_count)
  203. {
  204. struct net_device *netdev = adapter->netdev;
  205. struct pci_dev *pdev = adapter->pdev;
  206. union e1000_rx_desc_packet_split *rx_desc;
  207. struct e1000_ring *rx_ring = adapter->rx_ring;
  208. struct e1000_buffer *buffer_info;
  209. struct e1000_ps_page *ps_page;
  210. struct sk_buff *skb;
  211. unsigned int i, j;
  212. i = rx_ring->next_to_use;
  213. buffer_info = &rx_ring->buffer_info[i];
  214. while (cleaned_count--) {
  215. rx_desc = E1000_RX_DESC_PS(*rx_ring, i);
  216. for (j = 0; j < PS_PAGE_BUFFERS; j++) {
  217. ps_page = &buffer_info->ps_pages[j];
  218. if (j >= adapter->rx_ps_pages) {
  219. /* all unused desc entries get hw null ptr */
  220. rx_desc->read.buffer_addr[j+1] = ~cpu_to_le64(0);
  221. continue;
  222. }
  223. if (!ps_page->page) {
  224. ps_page->page = alloc_page(GFP_ATOMIC);
  225. if (!ps_page->page) {
  226. adapter->alloc_rx_buff_failed++;
  227. goto no_buffers;
  228. }
  229. ps_page->dma = pci_map_page(pdev,
  230. ps_page->page,
  231. 0, PAGE_SIZE,
  232. PCI_DMA_FROMDEVICE);
  233. if (pci_dma_mapping_error(pdev, ps_page->dma)) {
  234. dev_err(&adapter->pdev->dev,
  235. "RX DMA page map failed\n");
  236. adapter->rx_dma_failed++;
  237. goto no_buffers;
  238. }
  239. }
  240. /*
  241. * Refresh the desc even if buffer_addrs
  242. * didn't change because each write-back
  243. * erases this info.
  244. */
  245. rx_desc->read.buffer_addr[j+1] =
  246. cpu_to_le64(ps_page->dma);
  247. }
  248. skb = netdev_alloc_skb(netdev,
  249. adapter->rx_ps_bsize0 + NET_IP_ALIGN);
  250. if (!skb) {
  251. adapter->alloc_rx_buff_failed++;
  252. break;
  253. }
  254. /*
  255. * Make buffer alignment 2 beyond a 16 byte boundary
  256. * this will result in a 16 byte aligned IP header after
  257. * the 14 byte MAC header is removed
  258. */
  259. skb_reserve(skb, NET_IP_ALIGN);
  260. buffer_info->skb = skb;
  261. buffer_info->dma = pci_map_single(pdev, skb->data,
  262. adapter->rx_ps_bsize0,
  263. PCI_DMA_FROMDEVICE);
  264. if (pci_dma_mapping_error(pdev, buffer_info->dma)) {
  265. dev_err(&pdev->dev, "RX DMA map failed\n");
  266. adapter->rx_dma_failed++;
  267. /* cleanup skb */
  268. dev_kfree_skb_any(skb);
  269. buffer_info->skb = NULL;
  270. break;
  271. }
  272. rx_desc->read.buffer_addr[0] = cpu_to_le64(buffer_info->dma);
  273. i++;
  274. if (i == rx_ring->count)
  275. i = 0;
  276. buffer_info = &rx_ring->buffer_info[i];
  277. }
  278. no_buffers:
  279. if (rx_ring->next_to_use != i) {
  280. rx_ring->next_to_use = i;
  281. if (!(i--))
  282. i = (rx_ring->count - 1);
  283. /*
  284. * Force memory writes to complete before letting h/w
  285. * know there are new descriptors to fetch. (Only
  286. * applicable for weak-ordered memory model archs,
  287. * such as IA-64).
  288. */
  289. wmb();
  290. /*
  291. * Hardware increments by 16 bytes, but packet split
  292. * descriptors are 32 bytes...so we increment tail
  293. * twice as much.
  294. */
  295. writel(i<<1, adapter->hw.hw_addr + rx_ring->tail);
  296. }
  297. }
  298. /**
  299. * e1000_alloc_jumbo_rx_buffers - Replace used jumbo receive buffers
  300. * @adapter: address of board private structure
  301. * @rx_ring: pointer to receive ring structure
  302. * @cleaned_count: number of buffers to allocate this pass
  303. **/
  304. static void e1000_alloc_jumbo_rx_buffers(struct e1000_adapter *adapter,
  305. int cleaned_count)
  306. {
  307. struct net_device *netdev = adapter->netdev;
  308. struct pci_dev *pdev = adapter->pdev;
  309. struct e1000_rx_desc *rx_desc;
  310. struct e1000_ring *rx_ring = adapter->rx_ring;
  311. struct e1000_buffer *buffer_info;
  312. struct sk_buff *skb;
  313. unsigned int i;
  314. unsigned int bufsz = 256 -
  315. 16 /* for skb_reserve */ -
  316. NET_IP_ALIGN;
  317. i = rx_ring->next_to_use;
  318. buffer_info = &rx_ring->buffer_info[i];
  319. while (cleaned_count--) {
  320. skb = buffer_info->skb;
  321. if (skb) {
  322. skb_trim(skb, 0);
  323. goto check_page;
  324. }
  325. skb = netdev_alloc_skb(netdev, bufsz);
  326. if (unlikely(!skb)) {
  327. /* Better luck next round */
  328. adapter->alloc_rx_buff_failed++;
  329. break;
  330. }
  331. /* Make buffer alignment 2 beyond a 16 byte boundary
  332. * this will result in a 16 byte aligned IP header after
  333. * the 14 byte MAC header is removed
  334. */
  335. skb_reserve(skb, NET_IP_ALIGN);
  336. buffer_info->skb = skb;
  337. check_page:
  338. /* allocate a new page if necessary */
  339. if (!buffer_info->page) {
  340. buffer_info->page = alloc_page(GFP_ATOMIC);
  341. if (unlikely(!buffer_info->page)) {
  342. adapter->alloc_rx_buff_failed++;
  343. break;
  344. }
  345. }
  346. if (!buffer_info->dma)
  347. buffer_info->dma = pci_map_page(pdev,
  348. buffer_info->page, 0,
  349. PAGE_SIZE,
  350. PCI_DMA_FROMDEVICE);
  351. rx_desc = E1000_RX_DESC(*rx_ring, i);
  352. rx_desc->buffer_addr = cpu_to_le64(buffer_info->dma);
  353. if (unlikely(++i == rx_ring->count))
  354. i = 0;
  355. buffer_info = &rx_ring->buffer_info[i];
  356. }
  357. if (likely(rx_ring->next_to_use != i)) {
  358. rx_ring->next_to_use = i;
  359. if (unlikely(i-- == 0))
  360. i = (rx_ring->count - 1);
  361. /* Force memory writes to complete before letting h/w
  362. * know there are new descriptors to fetch. (Only
  363. * applicable for weak-ordered memory model archs,
  364. * such as IA-64). */
  365. wmb();
  366. writel(i, adapter->hw.hw_addr + rx_ring->tail);
  367. }
  368. }
  369. /**
  370. * e1000_clean_rx_irq - Send received data up the network stack; legacy
  371. * @adapter: board private structure
  372. *
  373. * the return value indicates whether actual cleaning was done, there
  374. * is no guarantee that everything was cleaned
  375. **/
  376. static bool e1000_clean_rx_irq(struct e1000_adapter *adapter,
  377. int *work_done, int work_to_do)
  378. {
  379. struct net_device *netdev = adapter->netdev;
  380. struct pci_dev *pdev = adapter->pdev;
  381. struct e1000_ring *rx_ring = adapter->rx_ring;
  382. struct e1000_rx_desc *rx_desc, *next_rxd;
  383. struct e1000_buffer *buffer_info, *next_buffer;
  384. u32 length;
  385. unsigned int i;
  386. int cleaned_count = 0;
  387. bool cleaned = 0;
  388. unsigned int total_rx_bytes = 0, total_rx_packets = 0;
  389. i = rx_ring->next_to_clean;
  390. rx_desc = E1000_RX_DESC(*rx_ring, i);
  391. buffer_info = &rx_ring->buffer_info[i];
  392. while (rx_desc->status & E1000_RXD_STAT_DD) {
  393. struct sk_buff *skb;
  394. u8 status;
  395. if (*work_done >= work_to_do)
  396. break;
  397. (*work_done)++;
  398. status = rx_desc->status;
  399. skb = buffer_info->skb;
  400. buffer_info->skb = NULL;
  401. prefetch(skb->data - NET_IP_ALIGN);
  402. i++;
  403. if (i == rx_ring->count)
  404. i = 0;
  405. next_rxd = E1000_RX_DESC(*rx_ring, i);
  406. prefetch(next_rxd);
  407. next_buffer = &rx_ring->buffer_info[i];
  408. cleaned = 1;
  409. cleaned_count++;
  410. pci_unmap_single(pdev,
  411. buffer_info->dma,
  412. adapter->rx_buffer_len,
  413. PCI_DMA_FROMDEVICE);
  414. buffer_info->dma = 0;
  415. length = le16_to_cpu(rx_desc->length);
  416. /* !EOP means multiple descriptors were used to store a single
  417. * packet, also make sure the frame isn't just CRC only */
  418. if (!(status & E1000_RXD_STAT_EOP) || (length <= 4)) {
  419. /* All receives must fit into a single buffer */
  420. e_dbg("%s: Receive packet consumed multiple buffers\n",
  421. netdev->name);
  422. /* recycle */
  423. buffer_info->skb = skb;
  424. goto next_desc;
  425. }
  426. if (rx_desc->errors & E1000_RXD_ERR_FRAME_ERR_MASK) {
  427. /* recycle */
  428. buffer_info->skb = skb;
  429. goto next_desc;
  430. }
  431. total_rx_bytes += length;
  432. total_rx_packets++;
  433. /*
  434. * code added for copybreak, this should improve
  435. * performance for small packets with large amounts
  436. * of reassembly being done in the stack
  437. */
  438. if (length < copybreak) {
  439. struct sk_buff *new_skb =
  440. netdev_alloc_skb(netdev, length + NET_IP_ALIGN);
  441. if (new_skb) {
  442. skb_reserve(new_skb, NET_IP_ALIGN);
  443. skb_copy_to_linear_data_offset(new_skb,
  444. -NET_IP_ALIGN,
  445. (skb->data -
  446. NET_IP_ALIGN),
  447. (length +
  448. NET_IP_ALIGN));
  449. /* save the skb in buffer_info as good */
  450. buffer_info->skb = skb;
  451. skb = new_skb;
  452. }
  453. /* else just continue with the old one */
  454. }
  455. /* end copybreak code */
  456. skb_put(skb, length);
  457. /* Receive Checksum Offload */
  458. e1000_rx_checksum(adapter,
  459. (u32)(status) |
  460. ((u32)(rx_desc->errors) << 24),
  461. le16_to_cpu(rx_desc->csum), skb);
  462. e1000_receive_skb(adapter, netdev, skb,status,rx_desc->special);
  463. next_desc:
  464. rx_desc->status = 0;
  465. /* return some buffers to hardware, one at a time is too slow */
  466. if (cleaned_count >= E1000_RX_BUFFER_WRITE) {
  467. adapter->alloc_rx_buf(adapter, cleaned_count);
  468. cleaned_count = 0;
  469. }
  470. /* use prefetched values */
  471. rx_desc = next_rxd;
  472. buffer_info = next_buffer;
  473. }
  474. rx_ring->next_to_clean = i;
  475. cleaned_count = e1000_desc_unused(rx_ring);
  476. if (cleaned_count)
  477. adapter->alloc_rx_buf(adapter, cleaned_count);
  478. adapter->total_rx_bytes += total_rx_bytes;
  479. adapter->total_rx_packets += total_rx_packets;
  480. adapter->net_stats.rx_bytes += total_rx_bytes;
  481. adapter->net_stats.rx_packets += total_rx_packets;
  482. return cleaned;
  483. }
  484. static void e1000_put_txbuf(struct e1000_adapter *adapter,
  485. struct e1000_buffer *buffer_info)
  486. {
  487. if (buffer_info->dma) {
  488. pci_unmap_page(adapter->pdev, buffer_info->dma,
  489. buffer_info->length, PCI_DMA_TODEVICE);
  490. buffer_info->dma = 0;
  491. }
  492. if (buffer_info->skb) {
  493. dev_kfree_skb_any(buffer_info->skb);
  494. buffer_info->skb = NULL;
  495. }
  496. }
  497. static void e1000_print_tx_hang(struct e1000_adapter *adapter)
  498. {
  499. struct e1000_ring *tx_ring = adapter->tx_ring;
  500. unsigned int i = tx_ring->next_to_clean;
  501. unsigned int eop = tx_ring->buffer_info[i].next_to_watch;
  502. struct e1000_tx_desc *eop_desc = E1000_TX_DESC(*tx_ring, eop);
  503. /* detected Tx unit hang */
  504. e_err("Detected Tx Unit Hang:\n"
  505. " TDH <%x>\n"
  506. " TDT <%x>\n"
  507. " next_to_use <%x>\n"
  508. " next_to_clean <%x>\n"
  509. "buffer_info[next_to_clean]:\n"
  510. " time_stamp <%lx>\n"
  511. " next_to_watch <%x>\n"
  512. " jiffies <%lx>\n"
  513. " next_to_watch.status <%x>\n",
  514. readl(adapter->hw.hw_addr + tx_ring->head),
  515. readl(adapter->hw.hw_addr + tx_ring->tail),
  516. tx_ring->next_to_use,
  517. tx_ring->next_to_clean,
  518. tx_ring->buffer_info[eop].time_stamp,
  519. eop,
  520. jiffies,
  521. eop_desc->upper.fields.status);
  522. }
  523. /**
  524. * e1000_clean_tx_irq - Reclaim resources after transmit completes
  525. * @adapter: board private structure
  526. *
  527. * the return value indicates whether actual cleaning was done, there
  528. * is no guarantee that everything was cleaned
  529. **/
  530. static bool e1000_clean_tx_irq(struct e1000_adapter *adapter)
  531. {
  532. struct net_device *netdev = adapter->netdev;
  533. struct e1000_hw *hw = &adapter->hw;
  534. struct e1000_ring *tx_ring = adapter->tx_ring;
  535. struct e1000_tx_desc *tx_desc, *eop_desc;
  536. struct e1000_buffer *buffer_info;
  537. unsigned int i, eop;
  538. unsigned int count = 0;
  539. bool cleaned = 0;
  540. unsigned int total_tx_bytes = 0, total_tx_packets = 0;
  541. i = tx_ring->next_to_clean;
  542. eop = tx_ring->buffer_info[i].next_to_watch;
  543. eop_desc = E1000_TX_DESC(*tx_ring, eop);
  544. while (eop_desc->upper.data & cpu_to_le32(E1000_TXD_STAT_DD)) {
  545. for (cleaned = 0; !cleaned; ) {
  546. tx_desc = E1000_TX_DESC(*tx_ring, i);
  547. buffer_info = &tx_ring->buffer_info[i];
  548. cleaned = (i == eop);
  549. if (cleaned) {
  550. struct sk_buff *skb = buffer_info->skb;
  551. unsigned int segs, bytecount;
  552. segs = skb_shinfo(skb)->gso_segs ?: 1;
  553. /* multiply data chunks by size of headers */
  554. bytecount = ((segs - 1) * skb_headlen(skb)) +
  555. skb->len;
  556. total_tx_packets += segs;
  557. total_tx_bytes += bytecount;
  558. }
  559. e1000_put_txbuf(adapter, buffer_info);
  560. tx_desc->upper.data = 0;
  561. i++;
  562. if (i == tx_ring->count)
  563. i = 0;
  564. }
  565. eop = tx_ring->buffer_info[i].next_to_watch;
  566. eop_desc = E1000_TX_DESC(*tx_ring, eop);
  567. #define E1000_TX_WEIGHT 64
  568. /* weight of a sort for tx, to avoid endless transmit cleanup */
  569. if (count++ == E1000_TX_WEIGHT)
  570. break;
  571. }
  572. tx_ring->next_to_clean = i;
  573. #define TX_WAKE_THRESHOLD 32
  574. if (cleaned && netif_carrier_ok(netdev) &&
  575. e1000_desc_unused(tx_ring) >= TX_WAKE_THRESHOLD) {
  576. /* Make sure that anybody stopping the queue after this
  577. * sees the new next_to_clean.
  578. */
  579. smp_mb();
  580. if (netif_queue_stopped(netdev) &&
  581. !(test_bit(__E1000_DOWN, &adapter->state))) {
  582. netif_wake_queue(netdev);
  583. ++adapter->restart_queue;
  584. }
  585. }
  586. if (adapter->detect_tx_hung) {
  587. /*
  588. * Detect a transmit hang in hardware, this serializes the
  589. * check with the clearing of time_stamp and movement of i
  590. */
  591. adapter->detect_tx_hung = 0;
  592. if (tx_ring->buffer_info[eop].dma &&
  593. time_after(jiffies, tx_ring->buffer_info[eop].time_stamp
  594. + (adapter->tx_timeout_factor * HZ))
  595. && !(er32(STATUS) & E1000_STATUS_TXOFF)) {
  596. e1000_print_tx_hang(adapter);
  597. netif_stop_queue(netdev);
  598. }
  599. }
  600. adapter->total_tx_bytes += total_tx_bytes;
  601. adapter->total_tx_packets += total_tx_packets;
  602. adapter->net_stats.tx_bytes += total_tx_bytes;
  603. adapter->net_stats.tx_packets += total_tx_packets;
  604. return cleaned;
  605. }
  606. /**
  607. * e1000_clean_rx_irq_ps - Send received data up the network stack; packet split
  608. * @adapter: board private structure
  609. *
  610. * the return value indicates whether actual cleaning was done, there
  611. * is no guarantee that everything was cleaned
  612. **/
  613. static bool e1000_clean_rx_irq_ps(struct e1000_adapter *adapter,
  614. int *work_done, int work_to_do)
  615. {
  616. union e1000_rx_desc_packet_split *rx_desc, *next_rxd;
  617. struct net_device *netdev = adapter->netdev;
  618. struct pci_dev *pdev = adapter->pdev;
  619. struct e1000_ring *rx_ring = adapter->rx_ring;
  620. struct e1000_buffer *buffer_info, *next_buffer;
  621. struct e1000_ps_page *ps_page;
  622. struct sk_buff *skb;
  623. unsigned int i, j;
  624. u32 length, staterr;
  625. int cleaned_count = 0;
  626. bool cleaned = 0;
  627. unsigned int total_rx_bytes = 0, total_rx_packets = 0;
  628. i = rx_ring->next_to_clean;
  629. rx_desc = E1000_RX_DESC_PS(*rx_ring, i);
  630. staterr = le32_to_cpu(rx_desc->wb.middle.status_error);
  631. buffer_info = &rx_ring->buffer_info[i];
  632. while (staterr & E1000_RXD_STAT_DD) {
  633. if (*work_done >= work_to_do)
  634. break;
  635. (*work_done)++;
  636. skb = buffer_info->skb;
  637. /* in the packet split case this is header only */
  638. prefetch(skb->data - NET_IP_ALIGN);
  639. i++;
  640. if (i == rx_ring->count)
  641. i = 0;
  642. next_rxd = E1000_RX_DESC_PS(*rx_ring, i);
  643. prefetch(next_rxd);
  644. next_buffer = &rx_ring->buffer_info[i];
  645. cleaned = 1;
  646. cleaned_count++;
  647. pci_unmap_single(pdev, buffer_info->dma,
  648. adapter->rx_ps_bsize0,
  649. PCI_DMA_FROMDEVICE);
  650. buffer_info->dma = 0;
  651. if (!(staterr & E1000_RXD_STAT_EOP)) {
  652. e_dbg("%s: Packet Split buffers didn't pick up the "
  653. "full packet\n", netdev->name);
  654. dev_kfree_skb_irq(skb);
  655. goto next_desc;
  656. }
  657. if (staterr & E1000_RXDEXT_ERR_FRAME_ERR_MASK) {
  658. dev_kfree_skb_irq(skb);
  659. goto next_desc;
  660. }
  661. length = le16_to_cpu(rx_desc->wb.middle.length0);
  662. if (!length) {
  663. e_dbg("%s: Last part of the packet spanning multiple "
  664. "descriptors\n", netdev->name);
  665. dev_kfree_skb_irq(skb);
  666. goto next_desc;
  667. }
  668. /* Good Receive */
  669. skb_put(skb, length);
  670. {
  671. /*
  672. * this looks ugly, but it seems compiler issues make it
  673. * more efficient than reusing j
  674. */
  675. int l1 = le16_to_cpu(rx_desc->wb.upper.length[0]);
  676. /*
  677. * page alloc/put takes too long and effects small packet
  678. * throughput, so unsplit small packets and save the alloc/put
  679. * only valid in softirq (napi) context to call kmap_*
  680. */
  681. if (l1 && (l1 <= copybreak) &&
  682. ((length + l1) <= adapter->rx_ps_bsize0)) {
  683. u8 *vaddr;
  684. ps_page = &buffer_info->ps_pages[0];
  685. /*
  686. * there is no documentation about how to call
  687. * kmap_atomic, so we can't hold the mapping
  688. * very long
  689. */
  690. pci_dma_sync_single_for_cpu(pdev, ps_page->dma,
  691. PAGE_SIZE, PCI_DMA_FROMDEVICE);
  692. vaddr = kmap_atomic(ps_page->page, KM_SKB_DATA_SOFTIRQ);
  693. memcpy(skb_tail_pointer(skb), vaddr, l1);
  694. kunmap_atomic(vaddr, KM_SKB_DATA_SOFTIRQ);
  695. pci_dma_sync_single_for_device(pdev, ps_page->dma,
  696. PAGE_SIZE, PCI_DMA_FROMDEVICE);
  697. skb_put(skb, l1);
  698. goto copydone;
  699. } /* if */
  700. }
  701. for (j = 0; j < PS_PAGE_BUFFERS; j++) {
  702. length = le16_to_cpu(rx_desc->wb.upper.length[j]);
  703. if (!length)
  704. break;
  705. ps_page = &buffer_info->ps_pages[j];
  706. pci_unmap_page(pdev, ps_page->dma, PAGE_SIZE,
  707. PCI_DMA_FROMDEVICE);
  708. ps_page->dma = 0;
  709. skb_fill_page_desc(skb, j, ps_page->page, 0, length);
  710. ps_page->page = NULL;
  711. skb->len += length;
  712. skb->data_len += length;
  713. skb->truesize += length;
  714. }
  715. copydone:
  716. total_rx_bytes += skb->len;
  717. total_rx_packets++;
  718. e1000_rx_checksum(adapter, staterr, le16_to_cpu(
  719. rx_desc->wb.lower.hi_dword.csum_ip.csum), skb);
  720. if (rx_desc->wb.upper.header_status &
  721. cpu_to_le16(E1000_RXDPS_HDRSTAT_HDRSP))
  722. adapter->rx_hdr_split++;
  723. e1000_receive_skb(adapter, netdev, skb,
  724. staterr, rx_desc->wb.middle.vlan);
  725. next_desc:
  726. rx_desc->wb.middle.status_error &= cpu_to_le32(~0xFF);
  727. buffer_info->skb = NULL;
  728. /* return some buffers to hardware, one at a time is too slow */
  729. if (cleaned_count >= E1000_RX_BUFFER_WRITE) {
  730. adapter->alloc_rx_buf(adapter, cleaned_count);
  731. cleaned_count = 0;
  732. }
  733. /* use prefetched values */
  734. rx_desc = next_rxd;
  735. buffer_info = next_buffer;
  736. staterr = le32_to_cpu(rx_desc->wb.middle.status_error);
  737. }
  738. rx_ring->next_to_clean = i;
  739. cleaned_count = e1000_desc_unused(rx_ring);
  740. if (cleaned_count)
  741. adapter->alloc_rx_buf(adapter, cleaned_count);
  742. adapter->total_rx_bytes += total_rx_bytes;
  743. adapter->total_rx_packets += total_rx_packets;
  744. adapter->net_stats.rx_bytes += total_rx_bytes;
  745. adapter->net_stats.rx_packets += total_rx_packets;
  746. return cleaned;
  747. }
  748. /**
  749. * e1000_consume_page - helper function
  750. **/
  751. static void e1000_consume_page(struct e1000_buffer *bi, struct sk_buff *skb,
  752. u16 length)
  753. {
  754. bi->page = NULL;
  755. skb->len += length;
  756. skb->data_len += length;
  757. skb->truesize += length;
  758. }
  759. /**
  760. * e1000_clean_jumbo_rx_irq - Send received data up the network stack; legacy
  761. * @adapter: board private structure
  762. *
  763. * the return value indicates whether actual cleaning was done, there
  764. * is no guarantee that everything was cleaned
  765. **/
  766. static bool e1000_clean_jumbo_rx_irq(struct e1000_adapter *adapter,
  767. int *work_done, int work_to_do)
  768. {
  769. struct net_device *netdev = adapter->netdev;
  770. struct pci_dev *pdev = adapter->pdev;
  771. struct e1000_ring *rx_ring = adapter->rx_ring;
  772. struct e1000_rx_desc *rx_desc, *next_rxd;
  773. struct e1000_buffer *buffer_info, *next_buffer;
  774. u32 length;
  775. unsigned int i;
  776. int cleaned_count = 0;
  777. bool cleaned = false;
  778. unsigned int total_rx_bytes=0, total_rx_packets=0;
  779. i = rx_ring->next_to_clean;
  780. rx_desc = E1000_RX_DESC(*rx_ring, i);
  781. buffer_info = &rx_ring->buffer_info[i];
  782. while (rx_desc->status & E1000_RXD_STAT_DD) {
  783. struct sk_buff *skb;
  784. u8 status;
  785. if (*work_done >= work_to_do)
  786. break;
  787. (*work_done)++;
  788. status = rx_desc->status;
  789. skb = buffer_info->skb;
  790. buffer_info->skb = NULL;
  791. ++i;
  792. if (i == rx_ring->count)
  793. i = 0;
  794. next_rxd = E1000_RX_DESC(*rx_ring, i);
  795. prefetch(next_rxd);
  796. next_buffer = &rx_ring->buffer_info[i];
  797. cleaned = true;
  798. cleaned_count++;
  799. pci_unmap_page(pdev, buffer_info->dma, PAGE_SIZE,
  800. PCI_DMA_FROMDEVICE);
  801. buffer_info->dma = 0;
  802. length = le16_to_cpu(rx_desc->length);
  803. /* errors is only valid for DD + EOP descriptors */
  804. if (unlikely((status & E1000_RXD_STAT_EOP) &&
  805. (rx_desc->errors & E1000_RXD_ERR_FRAME_ERR_MASK))) {
  806. /* recycle both page and skb */
  807. buffer_info->skb = skb;
  808. /* an error means any chain goes out the window
  809. * too */
  810. if (rx_ring->rx_skb_top)
  811. dev_kfree_skb(rx_ring->rx_skb_top);
  812. rx_ring->rx_skb_top = NULL;
  813. goto next_desc;
  814. }
  815. #define rxtop rx_ring->rx_skb_top
  816. if (!(status & E1000_RXD_STAT_EOP)) {
  817. /* this descriptor is only the beginning (or middle) */
  818. if (!rxtop) {
  819. /* this is the beginning of a chain */
  820. rxtop = skb;
  821. skb_fill_page_desc(rxtop, 0, buffer_info->page,
  822. 0, length);
  823. } else {
  824. /* this is the middle of a chain */
  825. skb_fill_page_desc(rxtop,
  826. skb_shinfo(rxtop)->nr_frags,
  827. buffer_info->page, 0, length);
  828. /* re-use the skb, only consumed the page */
  829. buffer_info->skb = skb;
  830. }
  831. e1000_consume_page(buffer_info, rxtop, length);
  832. goto next_desc;
  833. } else {
  834. if (rxtop) {
  835. /* end of the chain */
  836. skb_fill_page_desc(rxtop,
  837. skb_shinfo(rxtop)->nr_frags,
  838. buffer_info->page, 0, length);
  839. /* re-use the current skb, we only consumed the
  840. * page */
  841. buffer_info->skb = skb;
  842. skb = rxtop;
  843. rxtop = NULL;
  844. e1000_consume_page(buffer_info, skb, length);
  845. } else {
  846. /* no chain, got EOP, this buf is the packet
  847. * copybreak to save the put_page/alloc_page */
  848. if (length <= copybreak &&
  849. skb_tailroom(skb) >= length) {
  850. u8 *vaddr;
  851. vaddr = kmap_atomic(buffer_info->page,
  852. KM_SKB_DATA_SOFTIRQ);
  853. memcpy(skb_tail_pointer(skb), vaddr,
  854. length);
  855. kunmap_atomic(vaddr,
  856. KM_SKB_DATA_SOFTIRQ);
  857. /* re-use the page, so don't erase
  858. * buffer_info->page */
  859. skb_put(skb, length);
  860. } else {
  861. skb_fill_page_desc(skb, 0,
  862. buffer_info->page, 0,
  863. length);
  864. e1000_consume_page(buffer_info, skb,
  865. length);
  866. }
  867. }
  868. }
  869. /* Receive Checksum Offload XXX recompute due to CRC strip? */
  870. e1000_rx_checksum(adapter,
  871. (u32)(status) |
  872. ((u32)(rx_desc->errors) << 24),
  873. le16_to_cpu(rx_desc->csum), skb);
  874. /* probably a little skewed due to removing CRC */
  875. total_rx_bytes += skb->len;
  876. total_rx_packets++;
  877. /* eth type trans needs skb->data to point to something */
  878. if (!pskb_may_pull(skb, ETH_HLEN)) {
  879. e_err("pskb_may_pull failed.\n");
  880. dev_kfree_skb(skb);
  881. goto next_desc;
  882. }
  883. e1000_receive_skb(adapter, netdev, skb, status,
  884. rx_desc->special);
  885. next_desc:
  886. rx_desc->status = 0;
  887. /* return some buffers to hardware, one at a time is too slow */
  888. if (unlikely(cleaned_count >= E1000_RX_BUFFER_WRITE)) {
  889. adapter->alloc_rx_buf(adapter, cleaned_count);
  890. cleaned_count = 0;
  891. }
  892. /* use prefetched values */
  893. rx_desc = next_rxd;
  894. buffer_info = next_buffer;
  895. }
  896. rx_ring->next_to_clean = i;
  897. cleaned_count = e1000_desc_unused(rx_ring);
  898. if (cleaned_count)
  899. adapter->alloc_rx_buf(adapter, cleaned_count);
  900. adapter->total_rx_bytes += total_rx_bytes;
  901. adapter->total_rx_packets += total_rx_packets;
  902. adapter->net_stats.rx_bytes += total_rx_bytes;
  903. adapter->net_stats.rx_packets += total_rx_packets;
  904. return cleaned;
  905. }
  906. /**
  907. * e1000_clean_rx_ring - Free Rx Buffers per Queue
  908. * @adapter: board private structure
  909. **/
  910. static void e1000_clean_rx_ring(struct e1000_adapter *adapter)
  911. {
  912. struct e1000_ring *rx_ring = adapter->rx_ring;
  913. struct e1000_buffer *buffer_info;
  914. struct e1000_ps_page *ps_page;
  915. struct pci_dev *pdev = adapter->pdev;
  916. unsigned int i, j;
  917. /* Free all the Rx ring sk_buffs */
  918. for (i = 0; i < rx_ring->count; i++) {
  919. buffer_info = &rx_ring->buffer_info[i];
  920. if (buffer_info->dma) {
  921. if (adapter->clean_rx == e1000_clean_rx_irq)
  922. pci_unmap_single(pdev, buffer_info->dma,
  923. adapter->rx_buffer_len,
  924. PCI_DMA_FROMDEVICE);
  925. else if (adapter->clean_rx == e1000_clean_jumbo_rx_irq)
  926. pci_unmap_page(pdev, buffer_info->dma,
  927. PAGE_SIZE,
  928. PCI_DMA_FROMDEVICE);
  929. else if (adapter->clean_rx == e1000_clean_rx_irq_ps)
  930. pci_unmap_single(pdev, buffer_info->dma,
  931. adapter->rx_ps_bsize0,
  932. PCI_DMA_FROMDEVICE);
  933. buffer_info->dma = 0;
  934. }
  935. if (buffer_info->page) {
  936. put_page(buffer_info->page);
  937. buffer_info->page = NULL;
  938. }
  939. if (buffer_info->skb) {
  940. dev_kfree_skb(buffer_info->skb);
  941. buffer_info->skb = NULL;
  942. }
  943. for (j = 0; j < PS_PAGE_BUFFERS; j++) {
  944. ps_page = &buffer_info->ps_pages[j];
  945. if (!ps_page->page)
  946. break;
  947. pci_unmap_page(pdev, ps_page->dma, PAGE_SIZE,
  948. PCI_DMA_FROMDEVICE);
  949. ps_page->dma = 0;
  950. put_page(ps_page->page);
  951. ps_page->page = NULL;
  952. }
  953. }
  954. /* there also may be some cached data from a chained receive */
  955. if (rx_ring->rx_skb_top) {
  956. dev_kfree_skb(rx_ring->rx_skb_top);
  957. rx_ring->rx_skb_top = NULL;
  958. }
  959. /* Zero out the descriptor ring */
  960. memset(rx_ring->desc, 0, rx_ring->size);
  961. rx_ring->next_to_clean = 0;
  962. rx_ring->next_to_use = 0;
  963. writel(0, adapter->hw.hw_addr + rx_ring->head);
  964. writel(0, adapter->hw.hw_addr + rx_ring->tail);
  965. }
  966. static void e1000e_downshift_workaround(struct work_struct *work)
  967. {
  968. struct e1000_adapter *adapter = container_of(work,
  969. struct e1000_adapter, downshift_task);
  970. e1000e_gig_downshift_workaround_ich8lan(&adapter->hw);
  971. }
  972. /**
  973. * e1000_intr_msi - Interrupt Handler
  974. * @irq: interrupt number
  975. * @data: pointer to a network interface device structure
  976. **/
  977. static irqreturn_t e1000_intr_msi(int irq, void *data)
  978. {
  979. struct net_device *netdev = data;
  980. struct e1000_adapter *adapter = netdev_priv(netdev);
  981. struct e1000_hw *hw = &adapter->hw;
  982. u32 icr = er32(ICR);
  983. /*
  984. * read ICR disables interrupts using IAM
  985. */
  986. if (icr & (E1000_ICR_RXSEQ | E1000_ICR_LSC)) {
  987. hw->mac.get_link_status = 1;
  988. /*
  989. * ICH8 workaround-- Call gig speed drop workaround on cable
  990. * disconnect (LSC) before accessing any PHY registers
  991. */
  992. if ((adapter->flags & FLAG_LSC_GIG_SPEED_DROP) &&
  993. (!(er32(STATUS) & E1000_STATUS_LU)))
  994. schedule_work(&adapter->downshift_task);
  995. /*
  996. * 80003ES2LAN workaround-- For packet buffer work-around on
  997. * link down event; disable receives here in the ISR and reset
  998. * adapter in watchdog
  999. */
  1000. if (netif_carrier_ok(netdev) &&
  1001. adapter->flags & FLAG_RX_NEEDS_RESTART) {
  1002. /* disable receives */
  1003. u32 rctl = er32(RCTL);
  1004. ew32(RCTL, rctl & ~E1000_RCTL_EN);
  1005. adapter->flags |= FLAG_RX_RESTART_NOW;
  1006. }
  1007. /* guard against interrupt when we're going down */
  1008. if (!test_bit(__E1000_DOWN, &adapter->state))
  1009. mod_timer(&adapter->watchdog_timer, jiffies + 1);
  1010. }
  1011. if (netif_rx_schedule_prep(netdev, &adapter->napi)) {
  1012. adapter->total_tx_bytes = 0;
  1013. adapter->total_tx_packets = 0;
  1014. adapter->total_rx_bytes = 0;
  1015. adapter->total_rx_packets = 0;
  1016. __netif_rx_schedule(netdev, &adapter->napi);
  1017. }
  1018. return IRQ_HANDLED;
  1019. }
  1020. /**
  1021. * e1000_intr - Interrupt Handler
  1022. * @irq: interrupt number
  1023. * @data: pointer to a network interface device structure
  1024. **/
  1025. static irqreturn_t e1000_intr(int irq, void *data)
  1026. {
  1027. struct net_device *netdev = data;
  1028. struct e1000_adapter *adapter = netdev_priv(netdev);
  1029. struct e1000_hw *hw = &adapter->hw;
  1030. u32 rctl, icr = er32(ICR);
  1031. if (!icr)
  1032. return IRQ_NONE; /* Not our interrupt */
  1033. /*
  1034. * IMS will not auto-mask if INT_ASSERTED is not set, and if it is
  1035. * not set, then the adapter didn't send an interrupt
  1036. */
  1037. if (!(icr & E1000_ICR_INT_ASSERTED))
  1038. return IRQ_NONE;
  1039. /*
  1040. * Interrupt Auto-Mask...upon reading ICR,
  1041. * interrupts are masked. No need for the
  1042. * IMC write
  1043. */
  1044. if (icr & (E1000_ICR_RXSEQ | E1000_ICR_LSC)) {
  1045. hw->mac.get_link_status = 1;
  1046. /*
  1047. * ICH8 workaround-- Call gig speed drop workaround on cable
  1048. * disconnect (LSC) before accessing any PHY registers
  1049. */
  1050. if ((adapter->flags & FLAG_LSC_GIG_SPEED_DROP) &&
  1051. (!(er32(STATUS) & E1000_STATUS_LU)))
  1052. schedule_work(&adapter->downshift_task);
  1053. /*
  1054. * 80003ES2LAN workaround--
  1055. * For packet buffer work-around on link down event;
  1056. * disable receives here in the ISR and
  1057. * reset adapter in watchdog
  1058. */
  1059. if (netif_carrier_ok(netdev) &&
  1060. (adapter->flags & FLAG_RX_NEEDS_RESTART)) {
  1061. /* disable receives */
  1062. rctl = er32(RCTL);
  1063. ew32(RCTL, rctl & ~E1000_RCTL_EN);
  1064. adapter->flags |= FLAG_RX_RESTART_NOW;
  1065. }
  1066. /* guard against interrupt when we're going down */
  1067. if (!test_bit(__E1000_DOWN, &adapter->state))
  1068. mod_timer(&adapter->watchdog_timer, jiffies + 1);
  1069. }
  1070. if (netif_rx_schedule_prep(netdev, &adapter->napi)) {
  1071. adapter->total_tx_bytes = 0;
  1072. adapter->total_tx_packets = 0;
  1073. adapter->total_rx_bytes = 0;
  1074. adapter->total_rx_packets = 0;
  1075. __netif_rx_schedule(netdev, &adapter->napi);
  1076. }
  1077. return IRQ_HANDLED;
  1078. }
  1079. /**
  1080. * e1000_request_irq - initialize interrupts
  1081. *
  1082. * Attempts to configure interrupts using the best available
  1083. * capabilities of the hardware and kernel.
  1084. **/
  1085. static int e1000_request_irq(struct e1000_adapter *adapter)
  1086. {
  1087. struct net_device *netdev = adapter->netdev;
  1088. int irq_flags = IRQF_SHARED;
  1089. int err;
  1090. if (!(adapter->flags & FLAG_MSI_TEST_FAILED)) {
  1091. err = pci_enable_msi(adapter->pdev);
  1092. if (!err) {
  1093. adapter->flags |= FLAG_MSI_ENABLED;
  1094. irq_flags = 0;
  1095. }
  1096. }
  1097. err = request_irq(adapter->pdev->irq,
  1098. ((adapter->flags & FLAG_MSI_ENABLED) ?
  1099. &e1000_intr_msi : &e1000_intr),
  1100. irq_flags, netdev->name, netdev);
  1101. if (err) {
  1102. if (adapter->flags & FLAG_MSI_ENABLED) {
  1103. pci_disable_msi(adapter->pdev);
  1104. adapter->flags &= ~FLAG_MSI_ENABLED;
  1105. }
  1106. e_err("Unable to allocate interrupt, Error: %d\n", err);
  1107. }
  1108. return err;
  1109. }
  1110. static void e1000_free_irq(struct e1000_adapter *adapter)
  1111. {
  1112. struct net_device *netdev = adapter->netdev;
  1113. free_irq(adapter->pdev->irq, netdev);
  1114. if (adapter->flags & FLAG_MSI_ENABLED) {
  1115. pci_disable_msi(adapter->pdev);
  1116. adapter->flags &= ~FLAG_MSI_ENABLED;
  1117. }
  1118. }
  1119. /**
  1120. * e1000_irq_disable - Mask off interrupt generation on the NIC
  1121. **/
  1122. static void e1000_irq_disable(struct e1000_adapter *adapter)
  1123. {
  1124. struct e1000_hw *hw = &adapter->hw;
  1125. ew32(IMC, ~0);
  1126. e1e_flush();
  1127. synchronize_irq(adapter->pdev->irq);
  1128. }
  1129. /**
  1130. * e1000_irq_enable - Enable default interrupt generation settings
  1131. **/
  1132. static void e1000_irq_enable(struct e1000_adapter *adapter)
  1133. {
  1134. struct e1000_hw *hw = &adapter->hw;
  1135. ew32(IMS, IMS_ENABLE_MASK);
  1136. e1e_flush();
  1137. }
  1138. /**
  1139. * e1000_get_hw_control - get control of the h/w from f/w
  1140. * @adapter: address of board private structure
  1141. *
  1142. * e1000_get_hw_control sets {CTRL_EXT|SWSM}:DRV_LOAD bit.
  1143. * For ASF and Pass Through versions of f/w this means that
  1144. * the driver is loaded. For AMT version (only with 82573)
  1145. * of the f/w this means that the network i/f is open.
  1146. **/
  1147. static void e1000_get_hw_control(struct e1000_adapter *adapter)
  1148. {
  1149. struct e1000_hw *hw = &adapter->hw;
  1150. u32 ctrl_ext;
  1151. u32 swsm;
  1152. /* Let firmware know the driver has taken over */
  1153. if (adapter->flags & FLAG_HAS_SWSM_ON_LOAD) {
  1154. swsm = er32(SWSM);
  1155. ew32(SWSM, swsm | E1000_SWSM_DRV_LOAD);
  1156. } else if (adapter->flags & FLAG_HAS_CTRLEXT_ON_LOAD) {
  1157. ctrl_ext = er32(CTRL_EXT);
  1158. ew32(CTRL_EXT, ctrl_ext | E1000_CTRL_EXT_DRV_LOAD);
  1159. }
  1160. }
  1161. /**
  1162. * e1000_release_hw_control - release control of the h/w to f/w
  1163. * @adapter: address of board private structure
  1164. *
  1165. * e1000_release_hw_control resets {CTRL_EXT|SWSM}:DRV_LOAD bit.
  1166. * For ASF and Pass Through versions of f/w this means that the
  1167. * driver is no longer loaded. For AMT version (only with 82573) i
  1168. * of the f/w this means that the network i/f is closed.
  1169. *
  1170. **/
  1171. static void e1000_release_hw_control(struct e1000_adapter *adapter)
  1172. {
  1173. struct e1000_hw *hw = &adapter->hw;
  1174. u32 ctrl_ext;
  1175. u32 swsm;
  1176. /* Let firmware taken over control of h/w */
  1177. if (adapter->flags & FLAG_HAS_SWSM_ON_LOAD) {
  1178. swsm = er32(SWSM);
  1179. ew32(SWSM, swsm & ~E1000_SWSM_DRV_LOAD);
  1180. } else if (adapter->flags & FLAG_HAS_CTRLEXT_ON_LOAD) {
  1181. ctrl_ext = er32(CTRL_EXT);
  1182. ew32(CTRL_EXT, ctrl_ext & ~E1000_CTRL_EXT_DRV_LOAD);
  1183. }
  1184. }
  1185. /**
  1186. * @e1000_alloc_ring - allocate memory for a ring structure
  1187. **/
  1188. static int e1000_alloc_ring_dma(struct e1000_adapter *adapter,
  1189. struct e1000_ring *ring)
  1190. {
  1191. struct pci_dev *pdev = adapter->pdev;
  1192. ring->desc = dma_alloc_coherent(&pdev->dev, ring->size, &ring->dma,
  1193. GFP_KERNEL);
  1194. if (!ring->desc)
  1195. return -ENOMEM;
  1196. return 0;
  1197. }
  1198. /**
  1199. * e1000e_setup_tx_resources - allocate Tx resources (Descriptors)
  1200. * @adapter: board private structure
  1201. *
  1202. * Return 0 on success, negative on failure
  1203. **/
  1204. int e1000e_setup_tx_resources(struct e1000_adapter *adapter)
  1205. {
  1206. struct e1000_ring *tx_ring = adapter->tx_ring;
  1207. int err = -ENOMEM, size;
  1208. size = sizeof(struct e1000_buffer) * tx_ring->count;
  1209. tx_ring->buffer_info = vmalloc(size);
  1210. if (!tx_ring->buffer_info)
  1211. goto err;
  1212. memset(tx_ring->buffer_info, 0, size);
  1213. /* round up to nearest 4K */
  1214. tx_ring->size = tx_ring->count * sizeof(struct e1000_tx_desc);
  1215. tx_ring->size = ALIGN(tx_ring->size, 4096);
  1216. err = e1000_alloc_ring_dma(adapter, tx_ring);
  1217. if (err)
  1218. goto err;
  1219. tx_ring->next_to_use = 0;
  1220. tx_ring->next_to_clean = 0;
  1221. spin_lock_init(&adapter->tx_queue_lock);
  1222. return 0;
  1223. err:
  1224. vfree(tx_ring->buffer_info);
  1225. e_err("Unable to allocate memory for the transmit descriptor ring\n");
  1226. return err;
  1227. }
  1228. /**
  1229. * e1000e_setup_rx_resources - allocate Rx resources (Descriptors)
  1230. * @adapter: board private structure
  1231. *
  1232. * Returns 0 on success, negative on failure
  1233. **/
  1234. int e1000e_setup_rx_resources(struct e1000_adapter *adapter)
  1235. {
  1236. struct e1000_ring *rx_ring = adapter->rx_ring;
  1237. struct e1000_buffer *buffer_info;
  1238. int i, size, desc_len, err = -ENOMEM;
  1239. size = sizeof(struct e1000_buffer) * rx_ring->count;
  1240. rx_ring->buffer_info = vmalloc(size);
  1241. if (!rx_ring->buffer_info)
  1242. goto err;
  1243. memset(rx_ring->buffer_info, 0, size);
  1244. for (i = 0; i < rx_ring->count; i++) {
  1245. buffer_info = &rx_ring->buffer_info[i];
  1246. buffer_info->ps_pages = kcalloc(PS_PAGE_BUFFERS,
  1247. sizeof(struct e1000_ps_page),
  1248. GFP_KERNEL);
  1249. if (!buffer_info->ps_pages)
  1250. goto err_pages;
  1251. }
  1252. desc_len = sizeof(union e1000_rx_desc_packet_split);
  1253. /* Round up to nearest 4K */
  1254. rx_ring->size = rx_ring->count * desc_len;
  1255. rx_ring->size = ALIGN(rx_ring->size, 4096);
  1256. err = e1000_alloc_ring_dma(adapter, rx_ring);
  1257. if (err)
  1258. goto err_pages;
  1259. rx_ring->next_to_clean = 0;
  1260. rx_ring->next_to_use = 0;
  1261. rx_ring->rx_skb_top = NULL;
  1262. return 0;
  1263. err_pages:
  1264. for (i = 0; i < rx_ring->count; i++) {
  1265. buffer_info = &rx_ring->buffer_info[i];
  1266. kfree(buffer_info->ps_pages);
  1267. }
  1268. err:
  1269. vfree(rx_ring->buffer_info);
  1270. e_err("Unable to allocate memory for the transmit descriptor ring\n");
  1271. return err;
  1272. }
  1273. /**
  1274. * e1000_clean_tx_ring - Free Tx Buffers
  1275. * @adapter: board private structure
  1276. **/
  1277. static void e1000_clean_tx_ring(struct e1000_adapter *adapter)
  1278. {
  1279. struct e1000_ring *tx_ring = adapter->tx_ring;
  1280. struct e1000_buffer *buffer_info;
  1281. unsigned long size;
  1282. unsigned int i;
  1283. for (i = 0; i < tx_ring->count; i++) {
  1284. buffer_info = &tx_ring->buffer_info[i];
  1285. e1000_put_txbuf(adapter, buffer_info);
  1286. }
  1287. size = sizeof(struct e1000_buffer) * tx_ring->count;
  1288. memset(tx_ring->buffer_info, 0, size);
  1289. memset(tx_ring->desc, 0, tx_ring->size);
  1290. tx_ring->next_to_use = 0;
  1291. tx_ring->next_to_clean = 0;
  1292. writel(0, adapter->hw.hw_addr + tx_ring->head);
  1293. writel(0, adapter->hw.hw_addr + tx_ring->tail);
  1294. }
  1295. /**
  1296. * e1000e_free_tx_resources - Free Tx Resources per Queue
  1297. * @adapter: board private structure
  1298. *
  1299. * Free all transmit software resources
  1300. **/
  1301. void e1000e_free_tx_resources(struct e1000_adapter *adapter)
  1302. {
  1303. struct pci_dev *pdev = adapter->pdev;
  1304. struct e1000_ring *tx_ring = adapter->tx_ring;
  1305. e1000_clean_tx_ring(adapter);
  1306. vfree(tx_ring->buffer_info);
  1307. tx_ring->buffer_info = NULL;
  1308. dma_free_coherent(&pdev->dev, tx_ring->size, tx_ring->desc,
  1309. tx_ring->dma);
  1310. tx_ring->desc = NULL;
  1311. }
  1312. /**
  1313. * e1000e_free_rx_resources - Free Rx Resources
  1314. * @adapter: board private structure
  1315. *
  1316. * Free all receive software resources
  1317. **/
  1318. void e1000e_free_rx_resources(struct e1000_adapter *adapter)
  1319. {
  1320. struct pci_dev *pdev = adapter->pdev;
  1321. struct e1000_ring *rx_ring = adapter->rx_ring;
  1322. int i;
  1323. e1000_clean_rx_ring(adapter);
  1324. for (i = 0; i < rx_ring->count; i++) {
  1325. kfree(rx_ring->buffer_info[i].ps_pages);
  1326. }
  1327. vfree(rx_ring->buffer_info);
  1328. rx_ring->buffer_info = NULL;
  1329. dma_free_coherent(&pdev->dev, rx_ring->size, rx_ring->desc,
  1330. rx_ring->dma);
  1331. rx_ring->desc = NULL;
  1332. }
  1333. /**
  1334. * e1000_update_itr - update the dynamic ITR value based on statistics
  1335. * @adapter: pointer to adapter
  1336. * @itr_setting: current adapter->itr
  1337. * @packets: the number of packets during this measurement interval
  1338. * @bytes: the number of bytes during this measurement interval
  1339. *
  1340. * Stores a new ITR value based on packets and byte
  1341. * counts during the last interrupt. The advantage of per interrupt
  1342. * computation is faster updates and more accurate ITR for the current
  1343. * traffic pattern. Constants in this function were computed
  1344. * based on theoretical maximum wire speed and thresholds were set based
  1345. * on testing data as well as attempting to minimize response time
  1346. * while increasing bulk throughput.
  1347. * this functionality is controlled by the InterruptThrottleRate module
  1348. * parameter (see e1000_param.c)
  1349. **/
  1350. static unsigned int e1000_update_itr(struct e1000_adapter *adapter,
  1351. u16 itr_setting, int packets,
  1352. int bytes)
  1353. {
  1354. unsigned int retval = itr_setting;
  1355. if (packets == 0)
  1356. goto update_itr_done;
  1357. switch (itr_setting) {
  1358. case lowest_latency:
  1359. /* handle TSO and jumbo frames */
  1360. if (bytes/packets > 8000)
  1361. retval = bulk_latency;
  1362. else if ((packets < 5) && (bytes > 512)) {
  1363. retval = low_latency;
  1364. }
  1365. break;
  1366. case low_latency: /* 50 usec aka 20000 ints/s */
  1367. if (bytes > 10000) {
  1368. /* this if handles the TSO accounting */
  1369. if (bytes/packets > 8000) {
  1370. retval = bulk_latency;
  1371. } else if ((packets < 10) || ((bytes/packets) > 1200)) {
  1372. retval = bulk_latency;
  1373. } else if ((packets > 35)) {
  1374. retval = lowest_latency;
  1375. }
  1376. } else if (bytes/packets > 2000) {
  1377. retval = bulk_latency;
  1378. } else if (packets <= 2 && bytes < 512) {
  1379. retval = lowest_latency;
  1380. }
  1381. break;
  1382. case bulk_latency: /* 250 usec aka 4000 ints/s */
  1383. if (bytes > 25000) {
  1384. if (packets > 35) {
  1385. retval = low_latency;
  1386. }
  1387. } else if (bytes < 6000) {
  1388. retval = low_latency;
  1389. }
  1390. break;
  1391. }
  1392. update_itr_done:
  1393. return retval;
  1394. }
  1395. static void e1000_set_itr(struct e1000_adapter *adapter)
  1396. {
  1397. struct e1000_hw *hw = &adapter->hw;
  1398. u16 current_itr;
  1399. u32 new_itr = adapter->itr;
  1400. /* for non-gigabit speeds, just fix the interrupt rate at 4000 */
  1401. if (adapter->link_speed != SPEED_1000) {
  1402. current_itr = 0;
  1403. new_itr = 4000;
  1404. goto set_itr_now;
  1405. }
  1406. adapter->tx_itr = e1000_update_itr(adapter,
  1407. adapter->tx_itr,
  1408. adapter->total_tx_packets,
  1409. adapter->total_tx_bytes);
  1410. /* conservative mode (itr 3) eliminates the lowest_latency setting */
  1411. if (adapter->itr_setting == 3 && adapter->tx_itr == lowest_latency)
  1412. adapter->tx_itr = low_latency;
  1413. adapter->rx_itr = e1000_update_itr(adapter,
  1414. adapter->rx_itr,
  1415. adapter->total_rx_packets,
  1416. adapter->total_rx_bytes);
  1417. /* conservative mode (itr 3) eliminates the lowest_latency setting */
  1418. if (adapter->itr_setting == 3 && adapter->rx_itr == lowest_latency)
  1419. adapter->rx_itr = low_latency;
  1420. current_itr = max(adapter->rx_itr, adapter->tx_itr);
  1421. switch (current_itr) {
  1422. /* counts and packets in update_itr are dependent on these numbers */
  1423. case lowest_latency:
  1424. new_itr = 70000;
  1425. break;
  1426. case low_latency:
  1427. new_itr = 20000; /* aka hwitr = ~200 */
  1428. break;
  1429. case bulk_latency:
  1430. new_itr = 4000;
  1431. break;
  1432. default:
  1433. break;
  1434. }
  1435. set_itr_now:
  1436. if (new_itr != adapter->itr) {
  1437. /*
  1438. * this attempts to bias the interrupt rate towards Bulk
  1439. * by adding intermediate steps when interrupt rate is
  1440. * increasing
  1441. */
  1442. new_itr = new_itr > adapter->itr ?
  1443. min(adapter->itr + (new_itr >> 2), new_itr) :
  1444. new_itr;
  1445. adapter->itr = new_itr;
  1446. ew32(ITR, 1000000000 / (new_itr * 256));
  1447. }
  1448. }
  1449. /**
  1450. * e1000_clean - NAPI Rx polling callback
  1451. * @napi: struct associated with this polling callback
  1452. * @budget: amount of packets driver is allowed to process this poll
  1453. **/
  1454. static int e1000_clean(struct napi_struct *napi, int budget)
  1455. {
  1456. struct e1000_adapter *adapter = container_of(napi, struct e1000_adapter, napi);
  1457. struct net_device *poll_dev = adapter->netdev;
  1458. int tx_cleaned = 0, work_done = 0;
  1459. /* Must NOT use netdev_priv macro here. */
  1460. adapter = poll_dev->priv;
  1461. /*
  1462. * e1000_clean is called per-cpu. This lock protects
  1463. * tx_ring from being cleaned by multiple cpus
  1464. * simultaneously. A failure obtaining the lock means
  1465. * tx_ring is currently being cleaned anyway.
  1466. */
  1467. if (spin_trylock(&adapter->tx_queue_lock)) {
  1468. tx_cleaned = e1000_clean_tx_irq(adapter);
  1469. spin_unlock(&adapter->tx_queue_lock);
  1470. }
  1471. adapter->clean_rx(adapter, &work_done, budget);
  1472. if (tx_cleaned)
  1473. work_done = budget;
  1474. /* If budget not fully consumed, exit the polling mode */
  1475. if (work_done < budget) {
  1476. if (adapter->itr_setting & 3)
  1477. e1000_set_itr(adapter);
  1478. netif_rx_complete(poll_dev, napi);
  1479. e1000_irq_enable(adapter);
  1480. }
  1481. return work_done;
  1482. }
  1483. static void e1000_vlan_rx_add_vid(struct net_device *netdev, u16 vid)
  1484. {
  1485. struct e1000_adapter *adapter = netdev_priv(netdev);
  1486. struct e1000_hw *hw = &adapter->hw;
  1487. u32 vfta, index;
  1488. /* don't update vlan cookie if already programmed */
  1489. if ((adapter->hw.mng_cookie.status &
  1490. E1000_MNG_DHCP_COOKIE_STATUS_VLAN) &&
  1491. (vid == adapter->mng_vlan_id))
  1492. return;
  1493. /* add VID to filter table */
  1494. index = (vid >> 5) & 0x7F;
  1495. vfta = E1000_READ_REG_ARRAY(hw, E1000_VFTA, index);
  1496. vfta |= (1 << (vid & 0x1F));
  1497. e1000e_write_vfta(hw, index, vfta);
  1498. }
  1499. static void e1000_vlan_rx_kill_vid(struct net_device *netdev, u16 vid)
  1500. {
  1501. struct e1000_adapter *adapter = netdev_priv(netdev);
  1502. struct e1000_hw *hw = &adapter->hw;
  1503. u32 vfta, index;
  1504. if (!test_bit(__E1000_DOWN, &adapter->state))
  1505. e1000_irq_disable(adapter);
  1506. vlan_group_set_device(adapter->vlgrp, vid, NULL);
  1507. if (!test_bit(__E1000_DOWN, &adapter->state))
  1508. e1000_irq_enable(adapter);
  1509. if ((adapter->hw.mng_cookie.status &
  1510. E1000_MNG_DHCP_COOKIE_STATUS_VLAN) &&
  1511. (vid == adapter->mng_vlan_id)) {
  1512. /* release control to f/w */
  1513. e1000_release_hw_control(adapter);
  1514. return;
  1515. }
  1516. /* remove VID from filter table */
  1517. index = (vid >> 5) & 0x7F;
  1518. vfta = E1000_READ_REG_ARRAY(hw, E1000_VFTA, index);
  1519. vfta &= ~(1 << (vid & 0x1F));
  1520. e1000e_write_vfta(hw, index, vfta);
  1521. }
  1522. static void e1000_update_mng_vlan(struct e1000_adapter *adapter)
  1523. {
  1524. struct net_device *netdev = adapter->netdev;
  1525. u16 vid = adapter->hw.mng_cookie.vlan_id;
  1526. u16 old_vid = adapter->mng_vlan_id;
  1527. if (!adapter->vlgrp)
  1528. return;
  1529. if (!vlan_group_get_device(adapter->vlgrp, vid)) {
  1530. adapter->mng_vlan_id = E1000_MNG_VLAN_NONE;
  1531. if (adapter->hw.mng_cookie.status &
  1532. E1000_MNG_DHCP_COOKIE_STATUS_VLAN) {
  1533. e1000_vlan_rx_add_vid(netdev, vid);
  1534. adapter->mng_vlan_id = vid;
  1535. }
  1536. if ((old_vid != (u16)E1000_MNG_VLAN_NONE) &&
  1537. (vid != old_vid) &&
  1538. !vlan_group_get_device(adapter->vlgrp, old_vid))
  1539. e1000_vlan_rx_kill_vid(netdev, old_vid);
  1540. } else {
  1541. adapter->mng_vlan_id = vid;
  1542. }
  1543. }
  1544. static void e1000_vlan_rx_register(struct net_device *netdev,
  1545. struct vlan_group *grp)
  1546. {
  1547. struct e1000_adapter *adapter = netdev_priv(netdev);
  1548. struct e1000_hw *hw = &adapter->hw;
  1549. u32 ctrl, rctl;
  1550. if (!test_bit(__E1000_DOWN, &adapter->state))
  1551. e1000_irq_disable(adapter);
  1552. adapter->vlgrp = grp;
  1553. if (grp) {
  1554. /* enable VLAN tag insert/strip */
  1555. ctrl = er32(CTRL);
  1556. ctrl |= E1000_CTRL_VME;
  1557. ew32(CTRL, ctrl);
  1558. if (adapter->flags & FLAG_HAS_HW_VLAN_FILTER) {
  1559. /* enable VLAN receive filtering */
  1560. rctl = er32(RCTL);
  1561. rctl &= ~E1000_RCTL_CFIEN;
  1562. ew32(RCTL, rctl);
  1563. e1000_update_mng_vlan(adapter);
  1564. }
  1565. } else {
  1566. /* disable VLAN tag insert/strip */
  1567. ctrl = er32(CTRL);
  1568. ctrl &= ~E1000_CTRL_VME;
  1569. ew32(CTRL, ctrl);
  1570. if (adapter->flags & FLAG_HAS_HW_VLAN_FILTER) {
  1571. if (adapter->mng_vlan_id !=
  1572. (u16)E1000_MNG_VLAN_NONE) {
  1573. e1000_vlan_rx_kill_vid(netdev,
  1574. adapter->mng_vlan_id);
  1575. adapter->mng_vlan_id = E1000_MNG_VLAN_NONE;
  1576. }
  1577. }
  1578. }
  1579. if (!test_bit(__E1000_DOWN, &adapter->state))
  1580. e1000_irq_enable(adapter);
  1581. }
  1582. static void e1000_restore_vlan(struct e1000_adapter *adapter)
  1583. {
  1584. u16 vid;
  1585. e1000_vlan_rx_register(adapter->netdev, adapter->vlgrp);
  1586. if (!adapter->vlgrp)
  1587. return;
  1588. for (vid = 0; vid < VLAN_GROUP_ARRAY_LEN; vid++) {
  1589. if (!vlan_group_get_device(adapter->vlgrp, vid))
  1590. continue;
  1591. e1000_vlan_rx_add_vid(adapter->netdev, vid);
  1592. }
  1593. }
  1594. static void e1000_init_manageability(struct e1000_adapter *adapter)
  1595. {
  1596. struct e1000_hw *hw = &adapter->hw;
  1597. u32 manc, manc2h;
  1598. if (!(adapter->flags & FLAG_MNG_PT_ENABLED))
  1599. return;
  1600. manc = er32(MANC);
  1601. /*
  1602. * enable receiving management packets to the host. this will probably
  1603. * generate destination unreachable messages from the host OS, but
  1604. * the packets will be handled on SMBUS
  1605. */
  1606. manc |= E1000_MANC_EN_MNG2HOST;
  1607. manc2h = er32(MANC2H);
  1608. #define E1000_MNG2HOST_PORT_623 (1 << 5)
  1609. #define E1000_MNG2HOST_PORT_664 (1 << 6)
  1610. manc2h |= E1000_MNG2HOST_PORT_623;
  1611. manc2h |= E1000_MNG2HOST_PORT_664;
  1612. ew32(MANC2H, manc2h);
  1613. ew32(MANC, manc);
  1614. }
  1615. /**
  1616. * e1000_configure_tx - Configure 8254x Transmit Unit after Reset
  1617. * @adapter: board private structure
  1618. *
  1619. * Configure the Tx unit of the MAC after a reset.
  1620. **/
  1621. static void e1000_configure_tx(struct e1000_adapter *adapter)
  1622. {
  1623. struct e1000_hw *hw = &adapter->hw;
  1624. struct e1000_ring *tx_ring = adapter->tx_ring;
  1625. u64 tdba;
  1626. u32 tdlen, tctl, tipg, tarc;
  1627. u32 ipgr1, ipgr2;
  1628. /* Setup the HW Tx Head and Tail descriptor pointers */
  1629. tdba = tx_ring->dma;
  1630. tdlen = tx_ring->count * sizeof(struct e1000_tx_desc);
  1631. ew32(TDBAL, (tdba & DMA_32BIT_MASK));
  1632. ew32(TDBAH, (tdba >> 32));
  1633. ew32(TDLEN, tdlen);
  1634. ew32(TDH, 0);
  1635. ew32(TDT, 0);
  1636. tx_ring->head = E1000_TDH;
  1637. tx_ring->tail = E1000_TDT;
  1638. /* Set the default values for the Tx Inter Packet Gap timer */
  1639. tipg = DEFAULT_82543_TIPG_IPGT_COPPER; /* 8 */
  1640. ipgr1 = DEFAULT_82543_TIPG_IPGR1; /* 8 */
  1641. ipgr2 = DEFAULT_82543_TIPG_IPGR2; /* 6 */
  1642. if (adapter->flags & FLAG_TIPG_MEDIUM_FOR_80003ESLAN)
  1643. ipgr2 = DEFAULT_80003ES2LAN_TIPG_IPGR2; /* 7 */
  1644. tipg |= ipgr1 << E1000_TIPG_IPGR1_SHIFT;
  1645. tipg |= ipgr2 << E1000_TIPG_IPGR2_SHIFT;
  1646. ew32(TIPG, tipg);
  1647. /* Set the Tx Interrupt Delay register */
  1648. ew32(TIDV, adapter->tx_int_delay);
  1649. /* Tx irq moderation */
  1650. ew32(TADV, adapter->tx_abs_int_delay);
  1651. /* Program the Transmit Control Register */
  1652. tctl = er32(TCTL);
  1653. tctl &= ~E1000_TCTL_CT;
  1654. tctl |= E1000_TCTL_PSP | E1000_TCTL_RTLC |
  1655. (E1000_COLLISION_THRESHOLD << E1000_CT_SHIFT);
  1656. if (adapter->flags & FLAG_TARC_SPEED_MODE_BIT) {
  1657. tarc = er32(TARC(0));
  1658. /*
  1659. * set the speed mode bit, we'll clear it if we're not at
  1660. * gigabit link later
  1661. */
  1662. #define SPEED_MODE_BIT (1 << 21)
  1663. tarc |= SPEED_MODE_BIT;
  1664. ew32(TARC(0), tarc);
  1665. }
  1666. /* errata: program both queues to unweighted RR */
  1667. if (adapter->flags & FLAG_TARC_SET_BIT_ZERO) {
  1668. tarc = er32(TARC(0));
  1669. tarc |= 1;
  1670. ew32(TARC(0), tarc);
  1671. tarc = er32(TARC(1));
  1672. tarc |= 1;
  1673. ew32(TARC(1), tarc);
  1674. }
  1675. e1000e_config_collision_dist(hw);
  1676. /* Setup Transmit Descriptor Settings for eop descriptor */
  1677. adapter->txd_cmd = E1000_TXD_CMD_EOP | E1000_TXD_CMD_IFCS;
  1678. /* only set IDE if we are delaying interrupts using the timers */
  1679. if (adapter->tx_int_delay)
  1680. adapter->txd_cmd |= E1000_TXD_CMD_IDE;
  1681. /* enable Report Status bit */
  1682. adapter->txd_cmd |= E1000_TXD_CMD_RS;
  1683. ew32(TCTL, tctl);
  1684. adapter->tx_queue_len = adapter->netdev->tx_queue_len;
  1685. }
  1686. /**
  1687. * e1000_setup_rctl - configure the receive control registers
  1688. * @adapter: Board private structure
  1689. **/
  1690. #define PAGE_USE_COUNT(S) (((S) >> PAGE_SHIFT) + \
  1691. (((S) & (PAGE_SIZE - 1)) ? 1 : 0))
  1692. static void e1000_setup_rctl(struct e1000_adapter *adapter)
  1693. {
  1694. struct e1000_hw *hw = &adapter->hw;
  1695. u32 rctl, rfctl;
  1696. u32 psrctl = 0;
  1697. u32 pages = 0;
  1698. /* Program MC offset vector base */
  1699. rctl = er32(RCTL);
  1700. rctl &= ~(3 << E1000_RCTL_MO_SHIFT);
  1701. rctl |= E1000_RCTL_EN | E1000_RCTL_BAM |
  1702. E1000_RCTL_LBM_NO | E1000_RCTL_RDMTS_HALF |
  1703. (adapter->hw.mac.mc_filter_type << E1000_RCTL_MO_SHIFT);
  1704. /* Do not Store bad packets */
  1705. rctl &= ~E1000_RCTL_SBP;
  1706. /* Enable Long Packet receive */
  1707. if (adapter->netdev->mtu <= ETH_DATA_LEN)
  1708. rctl &= ~E1000_RCTL_LPE;
  1709. else
  1710. rctl |= E1000_RCTL_LPE;
  1711. /* Enable hardware CRC frame stripping */
  1712. rctl |= E1000_RCTL_SECRC;
  1713. /* Setup buffer sizes */
  1714. rctl &= ~E1000_RCTL_SZ_4096;
  1715. rctl |= E1000_RCTL_BSEX;
  1716. switch (adapter->rx_buffer_len) {
  1717. case 256:
  1718. rctl |= E1000_RCTL_SZ_256;
  1719. rctl &= ~E1000_RCTL_BSEX;
  1720. break;
  1721. case 512:
  1722. rctl |= E1000_RCTL_SZ_512;
  1723. rctl &= ~E1000_RCTL_BSEX;
  1724. break;
  1725. case 1024:
  1726. rctl |= E1000_RCTL_SZ_1024;
  1727. rctl &= ~E1000_RCTL_BSEX;
  1728. break;
  1729. case 2048:
  1730. default:
  1731. rctl |= E1000_RCTL_SZ_2048;
  1732. rctl &= ~E1000_RCTL_BSEX;
  1733. break;
  1734. case 4096:
  1735. rctl |= E1000_RCTL_SZ_4096;
  1736. break;
  1737. case 8192:
  1738. rctl |= E1000_RCTL_SZ_8192;
  1739. break;
  1740. case 16384:
  1741. rctl |= E1000_RCTL_SZ_16384;
  1742. break;
  1743. }
  1744. /*
  1745. * 82571 and greater support packet-split where the protocol
  1746. * header is placed in skb->data and the packet data is
  1747. * placed in pages hanging off of skb_shinfo(skb)->nr_frags.
  1748. * In the case of a non-split, skb->data is linearly filled,
  1749. * followed by the page buffers. Therefore, skb->data is
  1750. * sized to hold the largest protocol header.
  1751. *
  1752. * allocations using alloc_page take too long for regular MTU
  1753. * so only enable packet split for jumbo frames
  1754. *
  1755. * Using pages when the page size is greater than 16k wastes
  1756. * a lot of memory, since we allocate 3 pages at all times
  1757. * per packet.
  1758. */
  1759. pages = PAGE_USE_COUNT(adapter->netdev->mtu);
  1760. if (!(adapter->flags & FLAG_IS_ICH) && (pages <= 3) &&
  1761. (PAGE_SIZE <= 16384) && (rctl & E1000_RCTL_LPE))
  1762. adapter->rx_ps_pages = pages;
  1763. else
  1764. adapter->rx_ps_pages = 0;
  1765. if (adapter->rx_ps_pages) {
  1766. /* Configure extra packet-split registers */
  1767. rfctl = er32(RFCTL);
  1768. rfctl |= E1000_RFCTL_EXTEN;
  1769. /*
  1770. * disable packet split support for IPv6 extension headers,
  1771. * because some malformed IPv6 headers can hang the Rx
  1772. */
  1773. rfctl |= (E1000_RFCTL_IPV6_EX_DIS |
  1774. E1000_RFCTL_NEW_IPV6_EXT_DIS);
  1775. ew32(RFCTL, rfctl);
  1776. /* Enable Packet split descriptors */
  1777. rctl |= E1000_RCTL_DTYP_PS;
  1778. psrctl |= adapter->rx_ps_bsize0 >>
  1779. E1000_PSRCTL_BSIZE0_SHIFT;
  1780. switch (adapter->rx_ps_pages) {
  1781. case 3:
  1782. psrctl |= PAGE_SIZE <<
  1783. E1000_PSRCTL_BSIZE3_SHIFT;
  1784. case 2:
  1785. psrctl |= PAGE_SIZE <<
  1786. E1000_PSRCTL_BSIZE2_SHIFT;
  1787. case 1:
  1788. psrctl |= PAGE_SIZE >>
  1789. E1000_PSRCTL_BSIZE1_SHIFT;
  1790. break;
  1791. }
  1792. ew32(PSRCTL, psrctl);
  1793. }
  1794. ew32(RCTL, rctl);
  1795. /* just started the receive unit, no need to restart */
  1796. adapter->flags &= ~FLAG_RX_RESTART_NOW;
  1797. }
  1798. /**
  1799. * e1000_configure_rx - Configure Receive Unit after Reset
  1800. * @adapter: board private structure
  1801. *
  1802. * Configure the Rx unit of the MAC after a reset.
  1803. **/
  1804. static void e1000_configure_rx(struct e1000_adapter *adapter)
  1805. {
  1806. struct e1000_hw *hw = &adapter->hw;
  1807. struct e1000_ring *rx_ring = adapter->rx_ring;
  1808. u64 rdba;
  1809. u32 rdlen, rctl, rxcsum, ctrl_ext;
  1810. if (adapter->rx_ps_pages) {
  1811. /* this is a 32 byte descriptor */
  1812. rdlen = rx_ring->count *
  1813. sizeof(union e1000_rx_desc_packet_split);
  1814. adapter->clean_rx = e1000_clean_rx_irq_ps;
  1815. adapter->alloc_rx_buf = e1000_alloc_rx_buffers_ps;
  1816. } else if (adapter->netdev->mtu > ETH_FRAME_LEN + ETH_FCS_LEN) {
  1817. rdlen = rx_ring->count * sizeof(struct e1000_rx_desc);
  1818. adapter->clean_rx = e1000_clean_jumbo_rx_irq;
  1819. adapter->alloc_rx_buf = e1000_alloc_jumbo_rx_buffers;
  1820. } else {
  1821. rdlen = rx_ring->count * sizeof(struct e1000_rx_desc);
  1822. adapter->clean_rx = e1000_clean_rx_irq;
  1823. adapter->alloc_rx_buf = e1000_alloc_rx_buffers;
  1824. }
  1825. /* disable receives while setting up the descriptors */
  1826. rctl = er32(RCTL);
  1827. ew32(RCTL, rctl & ~E1000_RCTL_EN);
  1828. e1e_flush();
  1829. msleep(10);
  1830. /* set the Receive Delay Timer Register */
  1831. ew32(RDTR, adapter->rx_int_delay);
  1832. /* irq moderation */
  1833. ew32(RADV, adapter->rx_abs_int_delay);
  1834. if (adapter->itr_setting != 0)
  1835. ew32(ITR, 1000000000 / (adapter->itr * 256));
  1836. ctrl_ext = er32(CTRL_EXT);
  1837. /* Reset delay timers after every interrupt */
  1838. ctrl_ext |= E1000_CTRL_EXT_INT_TIMER_CLR;
  1839. /* Auto-Mask interrupts upon ICR access */
  1840. ctrl_ext |= E1000_CTRL_EXT_IAME;
  1841. ew32(IAM, 0xffffffff);
  1842. ew32(CTRL_EXT, ctrl_ext);
  1843. e1e_flush();
  1844. /*
  1845. * Setup the HW Rx Head and Tail Descriptor Pointers and
  1846. * the Base and Length of the Rx Descriptor Ring
  1847. */
  1848. rdba = rx_ring->dma;
  1849. ew32(RDBAL, (rdba & DMA_32BIT_MASK));
  1850. ew32(RDBAH, (rdba >> 32));
  1851. ew32(RDLEN, rdlen);
  1852. ew32(RDH, 0);
  1853. ew32(RDT, 0);
  1854. rx_ring->head = E1000_RDH;
  1855. rx_ring->tail = E1000_RDT;
  1856. /* Enable Receive Checksum Offload for TCP and UDP */
  1857. rxcsum = er32(RXCSUM);
  1858. if (adapter->flags & FLAG_RX_CSUM_ENABLED) {
  1859. rxcsum |= E1000_RXCSUM_TUOFL;
  1860. /*
  1861. * IPv4 payload checksum for UDP fragments must be
  1862. * used in conjunction with packet-split.
  1863. */
  1864. if (adapter->rx_ps_pages)
  1865. rxcsum |= E1000_RXCSUM_IPPCSE;
  1866. } else {
  1867. rxcsum &= ~E1000_RXCSUM_TUOFL;
  1868. /* no need to clear IPPCSE as it defaults to 0 */
  1869. }
  1870. ew32(RXCSUM, rxcsum);
  1871. /*
  1872. * Enable early receives on supported devices, only takes effect when
  1873. * packet size is equal or larger than the specified value (in 8 byte
  1874. * units), e.g. using jumbo frames when setting to E1000_ERT_2048
  1875. */
  1876. if ((adapter->flags & FLAG_HAS_ERT) &&
  1877. (adapter->netdev->mtu > ETH_DATA_LEN)) {
  1878. u32 rxdctl = er32(RXDCTL(0));
  1879. ew32(RXDCTL(0), rxdctl | 0x3);
  1880. ew32(ERT, E1000_ERT_2048 | (1 << 13));
  1881. /*
  1882. * With jumbo frames and early-receive enabled, excessive
  1883. * C4->C2 latencies result in dropped transactions.
  1884. */
  1885. pm_qos_update_requirement(PM_QOS_CPU_DMA_LATENCY,
  1886. e1000e_driver_name, 55);
  1887. } else {
  1888. pm_qos_update_requirement(PM_QOS_CPU_DMA_LATENCY,
  1889. e1000e_driver_name,
  1890. PM_QOS_DEFAULT_VALUE);
  1891. }
  1892. /* Enable Receives */
  1893. ew32(RCTL, rctl);
  1894. }
  1895. /**
  1896. * e1000_update_mc_addr_list - Update Multicast addresses
  1897. * @hw: pointer to the HW structure
  1898. * @mc_addr_list: array of multicast addresses to program
  1899. * @mc_addr_count: number of multicast addresses to program
  1900. * @rar_used_count: the first RAR register free to program
  1901. * @rar_count: total number of supported Receive Address Registers
  1902. *
  1903. * Updates the Receive Address Registers and Multicast Table Array.
  1904. * The caller must have a packed mc_addr_list of multicast addresses.
  1905. * The parameter rar_count will usually be hw->mac.rar_entry_count
  1906. * unless there are workarounds that change this. Currently no func pointer
  1907. * exists and all implementations are handled in the generic version of this
  1908. * function.
  1909. **/
  1910. static void e1000_update_mc_addr_list(struct e1000_hw *hw, u8 *mc_addr_list,
  1911. u32 mc_addr_count, u32 rar_used_count,
  1912. u32 rar_count)
  1913. {
  1914. hw->mac.ops.update_mc_addr_list(hw, mc_addr_list, mc_addr_count,
  1915. rar_used_count, rar_count);
  1916. }
  1917. /**
  1918. * e1000_set_multi - Multicast and Promiscuous mode set
  1919. * @netdev: network interface device structure
  1920. *
  1921. * The set_multi entry point is called whenever the multicast address
  1922. * list or the network interface flags are updated. This routine is
  1923. * responsible for configuring the hardware for proper multicast,
  1924. * promiscuous mode, and all-multi behavior.
  1925. **/
  1926. static void e1000_set_multi(struct net_device *netdev)
  1927. {
  1928. struct e1000_adapter *adapter = netdev_priv(netdev);
  1929. struct e1000_hw *hw = &adapter->hw;
  1930. struct e1000_mac_info *mac = &hw->mac;
  1931. struct dev_mc_list *mc_ptr;
  1932. u8 *mta_list;
  1933. u32 rctl;
  1934. int i;
  1935. /* Check for Promiscuous and All Multicast modes */
  1936. rctl = er32(RCTL);
  1937. if (netdev->flags & IFF_PROMISC) {
  1938. rctl |= (E1000_RCTL_UPE | E1000_RCTL_MPE);
  1939. rctl &= ~E1000_RCTL_VFE;
  1940. } else {
  1941. if (netdev->flags & IFF_ALLMULTI) {
  1942. rctl |= E1000_RCTL_MPE;
  1943. rctl &= ~E1000_RCTL_UPE;
  1944. } else {
  1945. rctl &= ~(E1000_RCTL_UPE | E1000_RCTL_MPE);
  1946. }
  1947. if (adapter->flags & FLAG_HAS_HW_VLAN_FILTER)
  1948. rctl |= E1000_RCTL_VFE;
  1949. }
  1950. ew32(RCTL, rctl);
  1951. if (netdev->mc_count) {
  1952. mta_list = kmalloc(netdev->mc_count * 6, GFP_ATOMIC);
  1953. if (!mta_list)
  1954. return;
  1955. /* prepare a packed array of only addresses. */
  1956. mc_ptr = netdev->mc_list;
  1957. for (i = 0; i < netdev->mc_count; i++) {
  1958. if (!mc_ptr)
  1959. break;
  1960. memcpy(mta_list + (i*ETH_ALEN), mc_ptr->dmi_addr,
  1961. ETH_ALEN);
  1962. mc_ptr = mc_ptr->next;
  1963. }
  1964. e1000_update_mc_addr_list(hw, mta_list, i, 1,
  1965. mac->rar_entry_count);
  1966. kfree(mta_list);
  1967. } else {
  1968. /*
  1969. * if we're called from probe, we might not have
  1970. * anything to do here, so clear out the list
  1971. */
  1972. e1000_update_mc_addr_list(hw, NULL, 0, 1, mac->rar_entry_count);
  1973. }
  1974. }
  1975. /**
  1976. * e1000_configure - configure the hardware for Rx and Tx
  1977. * @adapter: private board structure
  1978. **/
  1979. static void e1000_configure(struct e1000_adapter *adapter)
  1980. {
  1981. e1000_set_multi(adapter->netdev);
  1982. e1000_restore_vlan(adapter);
  1983. e1000_init_manageability(adapter);
  1984. e1000_configure_tx(adapter);
  1985. e1000_setup_rctl(adapter);
  1986. e1000_configure_rx(adapter);
  1987. adapter->alloc_rx_buf(adapter, e1000_desc_unused(adapter->rx_ring));
  1988. }
  1989. /**
  1990. * e1000e_power_up_phy - restore link in case the phy was powered down
  1991. * @adapter: address of board private structure
  1992. *
  1993. * The phy may be powered down to save power and turn off link when the
  1994. * driver is unloaded and wake on lan is not enabled (among others)
  1995. * *** this routine MUST be followed by a call to e1000e_reset ***
  1996. **/
  1997. void e1000e_power_up_phy(struct e1000_adapter *adapter)
  1998. {
  1999. u16 mii_reg = 0;
  2000. /* Just clear the power down bit to wake the phy back up */
  2001. if (adapter->hw.phy.media_type == e1000_media_type_copper) {
  2002. /*
  2003. * According to the manual, the phy will retain its
  2004. * settings across a power-down/up cycle
  2005. */
  2006. e1e_rphy(&adapter->hw, PHY_CONTROL, &mii_reg);
  2007. mii_reg &= ~MII_CR_POWER_DOWN;
  2008. e1e_wphy(&adapter->hw, PHY_CONTROL, mii_reg);
  2009. }
  2010. adapter->hw.mac.ops.setup_link(&adapter->hw);
  2011. }
  2012. /**
  2013. * e1000_power_down_phy - Power down the PHY
  2014. *
  2015. * Power down the PHY so no link is implied when interface is down
  2016. * The PHY cannot be powered down is management or WoL is active
  2017. */
  2018. static void e1000_power_down_phy(struct e1000_adapter *adapter)
  2019. {
  2020. struct e1000_hw *hw = &adapter->hw;
  2021. u16 mii_reg;
  2022. /* WoL is enabled */
  2023. if (adapter->wol)
  2024. return;
  2025. /* non-copper PHY? */
  2026. if (adapter->hw.phy.media_type != e1000_media_type_copper)
  2027. return;
  2028. /* reset is blocked because of a SoL/IDER session */
  2029. if (e1000e_check_mng_mode(hw) || e1000_check_reset_block(hw))
  2030. return;
  2031. /* manageability (AMT) is enabled */
  2032. if (er32(MANC) & E1000_MANC_SMBUS_EN)
  2033. return;
  2034. /* power down the PHY */
  2035. e1e_rphy(hw, PHY_CONTROL, &mii_reg);
  2036. mii_reg |= MII_CR_POWER_DOWN;
  2037. e1e_wphy(hw, PHY_CONTROL, mii_reg);
  2038. mdelay(1);
  2039. }
  2040. /**
  2041. * e1000e_reset - bring the hardware into a known good state
  2042. *
  2043. * This function boots the hardware and enables some settings that
  2044. * require a configuration cycle of the hardware - those cannot be
  2045. * set/changed during runtime. After reset the device needs to be
  2046. * properly configured for Rx, Tx etc.
  2047. */
  2048. void e1000e_reset(struct e1000_adapter *adapter)
  2049. {
  2050. struct e1000_mac_info *mac = &adapter->hw.mac;
  2051. struct e1000_fc_info *fc = &adapter->hw.fc;
  2052. struct e1000_hw *hw = &adapter->hw;
  2053. u32 tx_space, min_tx_space, min_rx_space;
  2054. u32 pba = adapter->pba;
  2055. u16 hwm;
  2056. /* reset Packet Buffer Allocation to default */
  2057. ew32(PBA, pba);
  2058. if (adapter->max_frame_size > ETH_FRAME_LEN + ETH_FCS_LEN) {
  2059. /*
  2060. * To maintain wire speed transmits, the Tx FIFO should be
  2061. * large enough to accommodate two full transmit packets,
  2062. * rounded up to the next 1KB and expressed in KB. Likewise,
  2063. * the Rx FIFO should be large enough to accommodate at least
  2064. * one full receive packet and is similarly rounded up and
  2065. * expressed in KB.
  2066. */
  2067. pba = er32(PBA);
  2068. /* upper 16 bits has Tx packet buffer allocation size in KB */
  2069. tx_space = pba >> 16;
  2070. /* lower 16 bits has Rx packet buffer allocation size in KB */
  2071. pba &= 0xffff;
  2072. /*
  2073. * the Tx fifo also stores 16 bytes of information about the tx
  2074. * but don't include ethernet FCS because hardware appends it
  2075. */
  2076. min_tx_space = (adapter->max_frame_size +
  2077. sizeof(struct e1000_tx_desc) -
  2078. ETH_FCS_LEN) * 2;
  2079. min_tx_space = ALIGN(min_tx_space, 1024);
  2080. min_tx_space >>= 10;
  2081. /* software strips receive CRC, so leave room for it */
  2082. min_rx_space = adapter->max_frame_size;
  2083. min_rx_space = ALIGN(min_rx_space, 1024);
  2084. min_rx_space >>= 10;
  2085. /*
  2086. * If current Tx allocation is less than the min Tx FIFO size,
  2087. * and the min Tx FIFO size is less than the current Rx FIFO
  2088. * allocation, take space away from current Rx allocation
  2089. */
  2090. if ((tx_space < min_tx_space) &&
  2091. ((min_tx_space - tx_space) < pba)) {
  2092. pba -= min_tx_space - tx_space;
  2093. /*
  2094. * if short on Rx space, Rx wins and must trump tx
  2095. * adjustment or use Early Receive if available
  2096. */
  2097. if ((pba < min_rx_space) &&
  2098. (!(adapter->flags & FLAG_HAS_ERT)))
  2099. /* ERT enabled in e1000_configure_rx */
  2100. pba = min_rx_space;
  2101. }
  2102. ew32(PBA, pba);
  2103. }
  2104. /*
  2105. * flow control settings
  2106. *
  2107. * The high water mark must be low enough to fit one full frame
  2108. * (or the size used for early receive) above it in the Rx FIFO.
  2109. * Set it to the lower of:
  2110. * - 90% of the Rx FIFO size, and
  2111. * - the full Rx FIFO size minus the early receive size (for parts
  2112. * with ERT support assuming ERT set to E1000_ERT_2048), or
  2113. * - the full Rx FIFO size minus one full frame
  2114. */
  2115. if (adapter->flags & FLAG_HAS_ERT)
  2116. hwm = min(((pba << 10) * 9 / 10),
  2117. ((pba << 10) - (E1000_ERT_2048 << 3)));
  2118. else
  2119. hwm = min(((pba << 10) * 9 / 10),
  2120. ((pba << 10) - adapter->max_frame_size));
  2121. fc->high_water = hwm & 0xFFF8; /* 8-byte granularity */
  2122. fc->low_water = fc->high_water - 8;
  2123. if (adapter->flags & FLAG_DISABLE_FC_PAUSE_TIME)
  2124. fc->pause_time = 0xFFFF;
  2125. else
  2126. fc->pause_time = E1000_FC_PAUSE_TIME;
  2127. fc->send_xon = 1;
  2128. fc->type = fc->original_type;
  2129. /* Allow time for pending master requests to run */
  2130. mac->ops.reset_hw(hw);
  2131. /*
  2132. * For parts with AMT enabled, let the firmware know
  2133. * that the network interface is in control
  2134. */
  2135. if (adapter->flags & FLAG_HAS_AMT)
  2136. e1000_get_hw_control(adapter);
  2137. ew32(WUC, 0);
  2138. if (mac->ops.init_hw(hw))
  2139. e_err("Hardware Error\n");
  2140. e1000_update_mng_vlan(adapter);
  2141. /* Enable h/w to recognize an 802.1Q VLAN Ethernet packet */
  2142. ew32(VET, ETH_P_8021Q);
  2143. e1000e_reset_adaptive(hw);
  2144. e1000_get_phy_info(hw);
  2145. if (!(adapter->flags & FLAG_SMART_POWER_DOWN)) {
  2146. u16 phy_data = 0;
  2147. /*
  2148. * speed up time to link by disabling smart power down, ignore
  2149. * the return value of this function because there is nothing
  2150. * different we would do if it failed
  2151. */
  2152. e1e_rphy(hw, IGP02E1000_PHY_POWER_MGMT, &phy_data);
  2153. phy_data &= ~IGP02E1000_PM_SPD;
  2154. e1e_wphy(hw, IGP02E1000_PHY_POWER_MGMT, phy_data);
  2155. }
  2156. }
  2157. int e1000e_up(struct e1000_adapter *adapter)
  2158. {
  2159. struct e1000_hw *hw = &adapter->hw;
  2160. /* hardware has been reset, we need to reload some things */
  2161. e1000_configure(adapter);
  2162. clear_bit(__E1000_DOWN, &adapter->state);
  2163. napi_enable(&adapter->napi);
  2164. e1000_irq_enable(adapter);
  2165. /* fire a link change interrupt to start the watchdog */
  2166. ew32(ICS, E1000_ICS_LSC);
  2167. return 0;
  2168. }
  2169. void e1000e_down(struct e1000_adapter *adapter)
  2170. {
  2171. struct net_device *netdev = adapter->netdev;
  2172. struct e1000_hw *hw = &adapter->hw;
  2173. u32 tctl, rctl;
  2174. /*
  2175. * signal that we're down so the interrupt handler does not
  2176. * reschedule our watchdog timer
  2177. */
  2178. set_bit(__E1000_DOWN, &adapter->state);
  2179. /* disable receives in the hardware */
  2180. rctl = er32(RCTL);
  2181. ew32(RCTL, rctl & ~E1000_RCTL_EN);
  2182. /* flush and sleep below */
  2183. netif_tx_stop_all_queues(netdev);
  2184. /* disable transmits in the hardware */
  2185. tctl = er32(TCTL);
  2186. tctl &= ~E1000_TCTL_EN;
  2187. ew32(TCTL, tctl);
  2188. /* flush both disables and wait for them to finish */
  2189. e1e_flush();
  2190. msleep(10);
  2191. napi_disable(&adapter->napi);
  2192. e1000_irq_disable(adapter);
  2193. del_timer_sync(&adapter->watchdog_timer);
  2194. del_timer_sync(&adapter->phy_info_timer);
  2195. netdev->tx_queue_len = adapter->tx_queue_len;
  2196. netif_carrier_off(netdev);
  2197. adapter->link_speed = 0;
  2198. adapter->link_duplex = 0;
  2199. if (!pci_channel_offline(adapter->pdev))
  2200. e1000e_reset(adapter);
  2201. e1000_clean_tx_ring(adapter);
  2202. e1000_clean_rx_ring(adapter);
  2203. /*
  2204. * TODO: for power management, we could drop the link and
  2205. * pci_disable_device here.
  2206. */
  2207. }
  2208. void e1000e_reinit_locked(struct e1000_adapter *adapter)
  2209. {
  2210. might_sleep();
  2211. while (test_and_set_bit(__E1000_RESETTING, &adapter->state))
  2212. msleep(1);
  2213. e1000e_down(adapter);
  2214. e1000e_up(adapter);
  2215. clear_bit(__E1000_RESETTING, &adapter->state);
  2216. }
  2217. /**
  2218. * e1000_sw_init - Initialize general software structures (struct e1000_adapter)
  2219. * @adapter: board private structure to initialize
  2220. *
  2221. * e1000_sw_init initializes the Adapter private data structure.
  2222. * Fields are initialized based on PCI device information and
  2223. * OS network device settings (MTU size).
  2224. **/
  2225. static int __devinit e1000_sw_init(struct e1000_adapter *adapter)
  2226. {
  2227. struct net_device *netdev = adapter->netdev;
  2228. adapter->rx_buffer_len = ETH_FRAME_LEN + VLAN_HLEN + ETH_FCS_LEN;
  2229. adapter->rx_ps_bsize0 = 128;
  2230. adapter->max_frame_size = netdev->mtu + ETH_HLEN + ETH_FCS_LEN;
  2231. adapter->min_frame_size = ETH_ZLEN + ETH_FCS_LEN;
  2232. adapter->tx_ring = kzalloc(sizeof(struct e1000_ring), GFP_KERNEL);
  2233. if (!adapter->tx_ring)
  2234. goto err;
  2235. adapter->rx_ring = kzalloc(sizeof(struct e1000_ring), GFP_KERNEL);
  2236. if (!adapter->rx_ring)
  2237. goto err;
  2238. spin_lock_init(&adapter->tx_queue_lock);
  2239. /* Explicitly disable IRQ since the NIC can be in any state. */
  2240. e1000_irq_disable(adapter);
  2241. set_bit(__E1000_DOWN, &adapter->state);
  2242. return 0;
  2243. err:
  2244. e_err("Unable to allocate memory for queues\n");
  2245. kfree(adapter->rx_ring);
  2246. kfree(adapter->tx_ring);
  2247. return -ENOMEM;
  2248. }
  2249. /**
  2250. * e1000_intr_msi_test - Interrupt Handler
  2251. * @irq: interrupt number
  2252. * @data: pointer to a network interface device structure
  2253. **/
  2254. static irqreturn_t e1000_intr_msi_test(int irq, void *data)
  2255. {
  2256. struct net_device *netdev = data;
  2257. struct e1000_adapter *adapter = netdev_priv(netdev);
  2258. struct e1000_hw *hw = &adapter->hw;
  2259. u32 icr = er32(ICR);
  2260. e_dbg("%s: icr is %08X\n", netdev->name, icr);
  2261. if (icr & E1000_ICR_RXSEQ) {
  2262. adapter->flags &= ~FLAG_MSI_TEST_FAILED;
  2263. wmb();
  2264. }
  2265. return IRQ_HANDLED;
  2266. }
  2267. /**
  2268. * e1000_test_msi_interrupt - Returns 0 for successful test
  2269. * @adapter: board private struct
  2270. *
  2271. * code flow taken from tg3.c
  2272. **/
  2273. static int e1000_test_msi_interrupt(struct e1000_adapter *adapter)
  2274. {
  2275. struct net_device *netdev = adapter->netdev;
  2276. struct e1000_hw *hw = &adapter->hw;
  2277. int err;
  2278. /* poll_enable hasn't been called yet, so don't need disable */
  2279. /* clear any pending events */
  2280. er32(ICR);
  2281. /* free the real vector and request a test handler */
  2282. e1000_free_irq(adapter);
  2283. /* Assume that the test fails, if it succeeds then the test
  2284. * MSI irq handler will unset this flag */
  2285. adapter->flags |= FLAG_MSI_TEST_FAILED;
  2286. err = pci_enable_msi(adapter->pdev);
  2287. if (err)
  2288. goto msi_test_failed;
  2289. err = request_irq(adapter->pdev->irq, &e1000_intr_msi_test, 0,
  2290. netdev->name, netdev);
  2291. if (err) {
  2292. pci_disable_msi(adapter->pdev);
  2293. goto msi_test_failed;
  2294. }
  2295. wmb();
  2296. e1000_irq_enable(adapter);
  2297. /* fire an unusual interrupt on the test handler */
  2298. ew32(ICS, E1000_ICS_RXSEQ);
  2299. e1e_flush();
  2300. msleep(50);
  2301. e1000_irq_disable(adapter);
  2302. rmb();
  2303. if (adapter->flags & FLAG_MSI_TEST_FAILED) {
  2304. err = -EIO;
  2305. e_info("MSI interrupt test failed!\n");
  2306. }
  2307. free_irq(adapter->pdev->irq, netdev);
  2308. pci_disable_msi(adapter->pdev);
  2309. if (err == -EIO)
  2310. goto msi_test_failed;
  2311. /* okay so the test worked, restore settings */
  2312. e_dbg("%s: MSI interrupt test succeeded!\n", netdev->name);
  2313. msi_test_failed:
  2314. /* restore the original vector, even if it failed */
  2315. e1000_request_irq(adapter);
  2316. return err;
  2317. }
  2318. /**
  2319. * e1000_test_msi - Returns 0 if MSI test succeeds or INTx mode is restored
  2320. * @adapter: board private struct
  2321. *
  2322. * code flow taken from tg3.c, called with e1000 interrupts disabled.
  2323. **/
  2324. static int e1000_test_msi(struct e1000_adapter *adapter)
  2325. {
  2326. int err;
  2327. u16 pci_cmd;
  2328. if (!(adapter->flags & FLAG_MSI_ENABLED))
  2329. return 0;
  2330. /* disable SERR in case the MSI write causes a master abort */
  2331. pci_read_config_word(adapter->pdev, PCI_COMMAND, &pci_cmd);
  2332. pci_write_config_word(adapter->pdev, PCI_COMMAND,
  2333. pci_cmd & ~PCI_COMMAND_SERR);
  2334. err = e1000_test_msi_interrupt(adapter);
  2335. /* restore previous setting of command word */
  2336. pci_write_config_word(adapter->pdev, PCI_COMMAND, pci_cmd);
  2337. /* success ! */
  2338. if (!err)
  2339. return 0;
  2340. /* EIO means MSI test failed */
  2341. if (err != -EIO)
  2342. return err;
  2343. /* back to INTx mode */
  2344. e_warn("MSI interrupt test failed, using legacy interrupt.\n");
  2345. e1000_free_irq(adapter);
  2346. err = e1000_request_irq(adapter);
  2347. return err;
  2348. }
  2349. /**
  2350. * e1000_open - Called when a network interface is made active
  2351. * @netdev: network interface device structure
  2352. *
  2353. * Returns 0 on success, negative value on failure
  2354. *
  2355. * The open entry point is called when a network interface is made
  2356. * active by the system (IFF_UP). At this point all resources needed
  2357. * for transmit and receive operations are allocated, the interrupt
  2358. * handler is registered with the OS, the watchdog timer is started,
  2359. * and the stack is notified that the interface is ready.
  2360. **/
  2361. static int e1000_open(struct net_device *netdev)
  2362. {
  2363. struct e1000_adapter *adapter = netdev_priv(netdev);
  2364. struct e1000_hw *hw = &adapter->hw;
  2365. int err;
  2366. /* disallow open during test */
  2367. if (test_bit(__E1000_TESTING, &adapter->state))
  2368. return -EBUSY;
  2369. /* allocate transmit descriptors */
  2370. err = e1000e_setup_tx_resources(adapter);
  2371. if (err)
  2372. goto err_setup_tx;
  2373. /* allocate receive descriptors */
  2374. err = e1000e_setup_rx_resources(adapter);
  2375. if (err)
  2376. goto err_setup_rx;
  2377. e1000e_power_up_phy(adapter);
  2378. adapter->mng_vlan_id = E1000_MNG_VLAN_NONE;
  2379. if ((adapter->hw.mng_cookie.status &
  2380. E1000_MNG_DHCP_COOKIE_STATUS_VLAN))
  2381. e1000_update_mng_vlan(adapter);
  2382. /*
  2383. * If AMT is enabled, let the firmware know that the network
  2384. * interface is now open
  2385. */
  2386. if (adapter->flags & FLAG_HAS_AMT)
  2387. e1000_get_hw_control(adapter);
  2388. /*
  2389. * before we allocate an interrupt, we must be ready to handle it.
  2390. * Setting DEBUG_SHIRQ in the kernel makes it fire an interrupt
  2391. * as soon as we call pci_request_irq, so we have to setup our
  2392. * clean_rx handler before we do so.
  2393. */
  2394. e1000_configure(adapter);
  2395. err = e1000_request_irq(adapter);
  2396. if (err)
  2397. goto err_req_irq;
  2398. /*
  2399. * Work around PCIe errata with MSI interrupts causing some chipsets to
  2400. * ignore e1000e MSI messages, which means we need to test our MSI
  2401. * interrupt now
  2402. */
  2403. {
  2404. err = e1000_test_msi(adapter);
  2405. if (err) {
  2406. e_err("Interrupt allocation failed\n");
  2407. goto err_req_irq;
  2408. }
  2409. }
  2410. /* From here on the code is the same as e1000e_up() */
  2411. clear_bit(__E1000_DOWN, &adapter->state);
  2412. napi_enable(&adapter->napi);
  2413. e1000_irq_enable(adapter);
  2414. netif_tx_start_all_queues(netdev);
  2415. /* fire a link status change interrupt to start the watchdog */
  2416. ew32(ICS, E1000_ICS_LSC);
  2417. return 0;
  2418. err_req_irq:
  2419. e1000_release_hw_control(adapter);
  2420. e1000_power_down_phy(adapter);
  2421. e1000e_free_rx_resources(adapter);
  2422. err_setup_rx:
  2423. e1000e_free_tx_resources(adapter);
  2424. err_setup_tx:
  2425. e1000e_reset(adapter);
  2426. return err;
  2427. }
  2428. /**
  2429. * e1000_close - Disables a network interface
  2430. * @netdev: network interface device structure
  2431. *
  2432. * Returns 0, this is not allowed to fail
  2433. *
  2434. * The close entry point is called when an interface is de-activated
  2435. * by the OS. The hardware is still under the drivers control, but
  2436. * needs to be disabled. A global MAC reset is issued to stop the
  2437. * hardware, and all transmit and receive resources are freed.
  2438. **/
  2439. static int e1000_close(struct net_device *netdev)
  2440. {
  2441. struct e1000_adapter *adapter = netdev_priv(netdev);
  2442. WARN_ON(test_bit(__E1000_RESETTING, &adapter->state));
  2443. e1000e_down(adapter);
  2444. e1000_power_down_phy(adapter);
  2445. e1000_free_irq(adapter);
  2446. e1000e_free_tx_resources(adapter);
  2447. e1000e_free_rx_resources(adapter);
  2448. /*
  2449. * kill manageability vlan ID if supported, but not if a vlan with
  2450. * the same ID is registered on the host OS (let 8021q kill it)
  2451. */
  2452. if ((adapter->hw.mng_cookie.status &
  2453. E1000_MNG_DHCP_COOKIE_STATUS_VLAN) &&
  2454. !(adapter->vlgrp &&
  2455. vlan_group_get_device(adapter->vlgrp, adapter->mng_vlan_id)))
  2456. e1000_vlan_rx_kill_vid(netdev, adapter->mng_vlan_id);
  2457. /*
  2458. * If AMT is enabled, let the firmware know that the network
  2459. * interface is now closed
  2460. */
  2461. if (adapter->flags & FLAG_HAS_AMT)
  2462. e1000_release_hw_control(adapter);
  2463. return 0;
  2464. }
  2465. /**
  2466. * e1000_set_mac - Change the Ethernet Address of the NIC
  2467. * @netdev: network interface device structure
  2468. * @p: pointer to an address structure
  2469. *
  2470. * Returns 0 on success, negative on failure
  2471. **/
  2472. static int e1000_set_mac(struct net_device *netdev, void *p)
  2473. {
  2474. struct e1000_adapter *adapter = netdev_priv(netdev);
  2475. struct sockaddr *addr = p;
  2476. if (!is_valid_ether_addr(addr->sa_data))
  2477. return -EADDRNOTAVAIL;
  2478. memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
  2479. memcpy(adapter->hw.mac.addr, addr->sa_data, netdev->addr_len);
  2480. e1000e_rar_set(&adapter->hw, adapter->hw.mac.addr, 0);
  2481. if (adapter->flags & FLAG_RESET_OVERWRITES_LAA) {
  2482. /* activate the work around */
  2483. e1000e_set_laa_state_82571(&adapter->hw, 1);
  2484. /*
  2485. * Hold a copy of the LAA in RAR[14] This is done so that
  2486. * between the time RAR[0] gets clobbered and the time it
  2487. * gets fixed (in e1000_watchdog), the actual LAA is in one
  2488. * of the RARs and no incoming packets directed to this port
  2489. * are dropped. Eventually the LAA will be in RAR[0] and
  2490. * RAR[14]
  2491. */
  2492. e1000e_rar_set(&adapter->hw,
  2493. adapter->hw.mac.addr,
  2494. adapter->hw.mac.rar_entry_count - 1);
  2495. }
  2496. return 0;
  2497. }
  2498. /**
  2499. * e1000e_update_phy_task - work thread to update phy
  2500. * @work: pointer to our work struct
  2501. *
  2502. * this worker thread exists because we must acquire a
  2503. * semaphore to read the phy, which we could msleep while
  2504. * waiting for it, and we can't msleep in a timer.
  2505. **/
  2506. static void e1000e_update_phy_task(struct work_struct *work)
  2507. {
  2508. struct e1000_adapter *adapter = container_of(work,
  2509. struct e1000_adapter, update_phy_task);
  2510. e1000_get_phy_info(&adapter->hw);
  2511. }
  2512. /*
  2513. * Need to wait a few seconds after link up to get diagnostic information from
  2514. * the phy
  2515. */
  2516. static void e1000_update_phy_info(unsigned long data)
  2517. {
  2518. struct e1000_adapter *adapter = (struct e1000_adapter *) data;
  2519. schedule_work(&adapter->update_phy_task);
  2520. }
  2521. /**
  2522. * e1000e_update_stats - Update the board statistics counters
  2523. * @adapter: board private structure
  2524. **/
  2525. void e1000e_update_stats(struct e1000_adapter *adapter)
  2526. {
  2527. struct e1000_hw *hw = &adapter->hw;
  2528. struct pci_dev *pdev = adapter->pdev;
  2529. /*
  2530. * Prevent stats update while adapter is being reset, or if the pci
  2531. * connection is down.
  2532. */
  2533. if (adapter->link_speed == 0)
  2534. return;
  2535. if (pci_channel_offline(pdev))
  2536. return;
  2537. adapter->stats.crcerrs += er32(CRCERRS);
  2538. adapter->stats.gprc += er32(GPRC);
  2539. adapter->stats.gorc += er32(GORCL);
  2540. er32(GORCH); /* Clear gorc */
  2541. adapter->stats.bprc += er32(BPRC);
  2542. adapter->stats.mprc += er32(MPRC);
  2543. adapter->stats.roc += er32(ROC);
  2544. adapter->stats.mpc += er32(MPC);
  2545. adapter->stats.scc += er32(SCC);
  2546. adapter->stats.ecol += er32(ECOL);
  2547. adapter->stats.mcc += er32(MCC);
  2548. adapter->stats.latecol += er32(LATECOL);
  2549. adapter->stats.dc += er32(DC);
  2550. adapter->stats.xonrxc += er32(XONRXC);
  2551. adapter->stats.xontxc += er32(XONTXC);
  2552. adapter->stats.xoffrxc += er32(XOFFRXC);
  2553. adapter->stats.xofftxc += er32(XOFFTXC);
  2554. adapter->stats.gptc += er32(GPTC);
  2555. adapter->stats.gotc += er32(GOTCL);
  2556. er32(GOTCH); /* Clear gotc */
  2557. adapter->stats.rnbc += er32(RNBC);
  2558. adapter->stats.ruc += er32(RUC);
  2559. adapter->stats.mptc += er32(MPTC);
  2560. adapter->stats.bptc += er32(BPTC);
  2561. /* used for adaptive IFS */
  2562. hw->mac.tx_packet_delta = er32(TPT);
  2563. adapter->stats.tpt += hw->mac.tx_packet_delta;
  2564. hw->mac.collision_delta = er32(COLC);
  2565. adapter->stats.colc += hw->mac.collision_delta;
  2566. adapter->stats.algnerrc += er32(ALGNERRC);
  2567. adapter->stats.rxerrc += er32(RXERRC);
  2568. adapter->stats.tncrs += er32(TNCRS);
  2569. adapter->stats.cexterr += er32(CEXTERR);
  2570. adapter->stats.tsctc += er32(TSCTC);
  2571. adapter->stats.tsctfc += er32(TSCTFC);
  2572. /* Fill out the OS statistics structure */
  2573. adapter->net_stats.multicast = adapter->stats.mprc;
  2574. adapter->net_stats.collisions = adapter->stats.colc;
  2575. /* Rx Errors */
  2576. /*
  2577. * RLEC on some newer hardware can be incorrect so build
  2578. * our own version based on RUC and ROC
  2579. */
  2580. adapter->net_stats.rx_errors = adapter->stats.rxerrc +
  2581. adapter->stats.crcerrs + adapter->stats.algnerrc +
  2582. adapter->stats.ruc + adapter->stats.roc +
  2583. adapter->stats.cexterr;
  2584. adapter->net_stats.rx_length_errors = adapter->stats.ruc +
  2585. adapter->stats.roc;
  2586. adapter->net_stats.rx_crc_errors = adapter->stats.crcerrs;
  2587. adapter->net_stats.rx_frame_errors = adapter->stats.algnerrc;
  2588. adapter->net_stats.rx_missed_errors = adapter->stats.mpc;
  2589. /* Tx Errors */
  2590. adapter->net_stats.tx_errors = adapter->stats.ecol +
  2591. adapter->stats.latecol;
  2592. adapter->net_stats.tx_aborted_errors = adapter->stats.ecol;
  2593. adapter->net_stats.tx_window_errors = adapter->stats.latecol;
  2594. adapter->net_stats.tx_carrier_errors = adapter->stats.tncrs;
  2595. /* Tx Dropped needs to be maintained elsewhere */
  2596. /* Management Stats */
  2597. adapter->stats.mgptc += er32(MGTPTC);
  2598. adapter->stats.mgprc += er32(MGTPRC);
  2599. adapter->stats.mgpdc += er32(MGTPDC);
  2600. }
  2601. /**
  2602. * e1000_phy_read_status - Update the PHY register status snapshot
  2603. * @adapter: board private structure
  2604. **/
  2605. static void e1000_phy_read_status(struct e1000_adapter *adapter)
  2606. {
  2607. struct e1000_hw *hw = &adapter->hw;
  2608. struct e1000_phy_regs *phy = &adapter->phy_regs;
  2609. int ret_val;
  2610. if ((er32(STATUS) & E1000_STATUS_LU) &&
  2611. (adapter->hw.phy.media_type == e1000_media_type_copper)) {
  2612. ret_val = e1e_rphy(hw, PHY_CONTROL, &phy->bmcr);
  2613. ret_val |= e1e_rphy(hw, PHY_STATUS, &phy->bmsr);
  2614. ret_val |= e1e_rphy(hw, PHY_AUTONEG_ADV, &phy->advertise);
  2615. ret_val |= e1e_rphy(hw, PHY_LP_ABILITY, &phy->lpa);
  2616. ret_val |= e1e_rphy(hw, PHY_AUTONEG_EXP, &phy->expansion);
  2617. ret_val |= e1e_rphy(hw, PHY_1000T_CTRL, &phy->ctrl1000);
  2618. ret_val |= e1e_rphy(hw, PHY_1000T_STATUS, &phy->stat1000);
  2619. ret_val |= e1e_rphy(hw, PHY_EXT_STATUS, &phy->estatus);
  2620. if (ret_val)
  2621. e_warn("Error reading PHY register\n");
  2622. } else {
  2623. /*
  2624. * Do not read PHY registers if link is not up
  2625. * Set values to typical power-on defaults
  2626. */
  2627. phy->bmcr = (BMCR_SPEED1000 | BMCR_ANENABLE | BMCR_FULLDPLX);
  2628. phy->bmsr = (BMSR_100FULL | BMSR_100HALF | BMSR_10FULL |
  2629. BMSR_10HALF | BMSR_ESTATEN | BMSR_ANEGCAPABLE |
  2630. BMSR_ERCAP);
  2631. phy->advertise = (ADVERTISE_PAUSE_ASYM | ADVERTISE_PAUSE_CAP |
  2632. ADVERTISE_ALL | ADVERTISE_CSMA);
  2633. phy->lpa = 0;
  2634. phy->expansion = EXPANSION_ENABLENPAGE;
  2635. phy->ctrl1000 = ADVERTISE_1000FULL;
  2636. phy->stat1000 = 0;
  2637. phy->estatus = (ESTATUS_1000_TFULL | ESTATUS_1000_THALF);
  2638. }
  2639. }
  2640. static void e1000_print_link_info(struct e1000_adapter *adapter)
  2641. {
  2642. struct e1000_hw *hw = &adapter->hw;
  2643. u32 ctrl = er32(CTRL);
  2644. e_info("Link is Up %d Mbps %s, Flow Control: %s\n",
  2645. adapter->link_speed,
  2646. (adapter->link_duplex == FULL_DUPLEX) ?
  2647. "Full Duplex" : "Half Duplex",
  2648. ((ctrl & E1000_CTRL_TFCE) && (ctrl & E1000_CTRL_RFCE)) ?
  2649. "RX/TX" :
  2650. ((ctrl & E1000_CTRL_RFCE) ? "RX" :
  2651. ((ctrl & E1000_CTRL_TFCE) ? "TX" : "None" )));
  2652. }
  2653. static bool e1000_has_link(struct e1000_adapter *adapter)
  2654. {
  2655. struct e1000_hw *hw = &adapter->hw;
  2656. bool link_active = 0;
  2657. s32 ret_val = 0;
  2658. /*
  2659. * get_link_status is set on LSC (link status) interrupt or
  2660. * Rx sequence error interrupt. get_link_status will stay
  2661. * false until the check_for_link establishes link
  2662. * for copper adapters ONLY
  2663. */
  2664. switch (hw->phy.media_type) {
  2665. case e1000_media_type_copper:
  2666. if (hw->mac.get_link_status) {
  2667. ret_val = hw->mac.ops.check_for_link(hw);
  2668. link_active = !hw->mac.get_link_status;
  2669. } else {
  2670. link_active = 1;
  2671. }
  2672. break;
  2673. case e1000_media_type_fiber:
  2674. ret_val = hw->mac.ops.check_for_link(hw);
  2675. link_active = !!(er32(STATUS) & E1000_STATUS_LU);
  2676. break;
  2677. case e1000_media_type_internal_serdes:
  2678. ret_val = hw->mac.ops.check_for_link(hw);
  2679. link_active = adapter->hw.mac.serdes_has_link;
  2680. break;
  2681. default:
  2682. case e1000_media_type_unknown:
  2683. break;
  2684. }
  2685. if ((ret_val == E1000_ERR_PHY) && (hw->phy.type == e1000_phy_igp_3) &&
  2686. (er32(CTRL) & E1000_PHY_CTRL_GBE_DISABLE)) {
  2687. /* See e1000_kmrn_lock_loss_workaround_ich8lan() */
  2688. e_info("Gigabit has been disabled, downgrading speed\n");
  2689. }
  2690. return link_active;
  2691. }
  2692. static void e1000e_enable_receives(struct e1000_adapter *adapter)
  2693. {
  2694. /* make sure the receive unit is started */
  2695. if ((adapter->flags & FLAG_RX_NEEDS_RESTART) &&
  2696. (adapter->flags & FLAG_RX_RESTART_NOW)) {
  2697. struct e1000_hw *hw = &adapter->hw;
  2698. u32 rctl = er32(RCTL);
  2699. ew32(RCTL, rctl | E1000_RCTL_EN);
  2700. adapter->flags &= ~FLAG_RX_RESTART_NOW;
  2701. }
  2702. }
  2703. /**
  2704. * e1000_watchdog - Timer Call-back
  2705. * @data: pointer to adapter cast into an unsigned long
  2706. **/
  2707. static void e1000_watchdog(unsigned long data)
  2708. {
  2709. struct e1000_adapter *adapter = (struct e1000_adapter *) data;
  2710. /* Do the rest outside of interrupt context */
  2711. schedule_work(&adapter->watchdog_task);
  2712. /* TODO: make this use queue_delayed_work() */
  2713. }
  2714. static void e1000_watchdog_task(struct work_struct *work)
  2715. {
  2716. struct e1000_adapter *adapter = container_of(work,
  2717. struct e1000_adapter, watchdog_task);
  2718. struct net_device *netdev = adapter->netdev;
  2719. struct e1000_mac_info *mac = &adapter->hw.mac;
  2720. struct e1000_ring *tx_ring = adapter->tx_ring;
  2721. struct e1000_hw *hw = &adapter->hw;
  2722. u32 link, tctl;
  2723. int tx_pending = 0;
  2724. link = e1000_has_link(adapter);
  2725. if ((netif_carrier_ok(netdev)) && link) {
  2726. e1000e_enable_receives(adapter);
  2727. goto link_up;
  2728. }
  2729. if ((e1000e_enable_tx_pkt_filtering(hw)) &&
  2730. (adapter->mng_vlan_id != adapter->hw.mng_cookie.vlan_id))
  2731. e1000_update_mng_vlan(adapter);
  2732. if (link) {
  2733. if (!netif_carrier_ok(netdev)) {
  2734. bool txb2b = 1;
  2735. /* update snapshot of PHY registers on LSC */
  2736. e1000_phy_read_status(adapter);
  2737. mac->ops.get_link_up_info(&adapter->hw,
  2738. &adapter->link_speed,
  2739. &adapter->link_duplex);
  2740. e1000_print_link_info(adapter);
  2741. /*
  2742. * tweak tx_queue_len according to speed/duplex
  2743. * and adjust the timeout factor
  2744. */
  2745. netdev->tx_queue_len = adapter->tx_queue_len;
  2746. adapter->tx_timeout_factor = 1;
  2747. switch (adapter->link_speed) {
  2748. case SPEED_10:
  2749. txb2b = 0;
  2750. netdev->tx_queue_len = 10;
  2751. adapter->tx_timeout_factor = 16;
  2752. break;
  2753. case SPEED_100:
  2754. txb2b = 0;
  2755. netdev->tx_queue_len = 100;
  2756. /* maybe add some timeout factor ? */
  2757. break;
  2758. }
  2759. /*
  2760. * workaround: re-program speed mode bit after
  2761. * link-up event
  2762. */
  2763. if ((adapter->flags & FLAG_TARC_SPEED_MODE_BIT) &&
  2764. !txb2b) {
  2765. u32 tarc0;
  2766. tarc0 = er32(TARC(0));
  2767. tarc0 &= ~SPEED_MODE_BIT;
  2768. ew32(TARC(0), tarc0);
  2769. }
  2770. /*
  2771. * disable TSO for pcie and 10/100 speeds, to avoid
  2772. * some hardware issues
  2773. */
  2774. if (!(adapter->flags & FLAG_TSO_FORCE)) {
  2775. switch (adapter->link_speed) {
  2776. case SPEED_10:
  2777. case SPEED_100:
  2778. e_info("10/100 speed: disabling TSO\n");
  2779. netdev->features &= ~NETIF_F_TSO;
  2780. netdev->features &= ~NETIF_F_TSO6;
  2781. break;
  2782. case SPEED_1000:
  2783. netdev->features |= NETIF_F_TSO;
  2784. netdev->features |= NETIF_F_TSO6;
  2785. break;
  2786. default:
  2787. /* oops */
  2788. break;
  2789. }
  2790. }
  2791. /*
  2792. * enable transmits in the hardware, need to do this
  2793. * after setting TARC(0)
  2794. */
  2795. tctl = er32(TCTL);
  2796. tctl |= E1000_TCTL_EN;
  2797. ew32(TCTL, tctl);
  2798. netif_carrier_on(netdev);
  2799. netif_tx_wake_all_queues(netdev);
  2800. if (!test_bit(__E1000_DOWN, &adapter->state))
  2801. mod_timer(&adapter->phy_info_timer,
  2802. round_jiffies(jiffies + 2 * HZ));
  2803. }
  2804. } else {
  2805. if (netif_carrier_ok(netdev)) {
  2806. adapter->link_speed = 0;
  2807. adapter->link_duplex = 0;
  2808. e_info("Link is Down\n");
  2809. netif_carrier_off(netdev);
  2810. netif_tx_stop_all_queues(netdev);
  2811. if (!test_bit(__E1000_DOWN, &adapter->state))
  2812. mod_timer(&adapter->phy_info_timer,
  2813. round_jiffies(jiffies + 2 * HZ));
  2814. if (adapter->flags & FLAG_RX_NEEDS_RESTART)
  2815. schedule_work(&adapter->reset_task);
  2816. }
  2817. }
  2818. link_up:
  2819. e1000e_update_stats(adapter);
  2820. mac->tx_packet_delta = adapter->stats.tpt - adapter->tpt_old;
  2821. adapter->tpt_old = adapter->stats.tpt;
  2822. mac->collision_delta = adapter->stats.colc - adapter->colc_old;
  2823. adapter->colc_old = adapter->stats.colc;
  2824. adapter->gorc = adapter->stats.gorc - adapter->gorc_old;
  2825. adapter->gorc_old = adapter->stats.gorc;
  2826. adapter->gotc = adapter->stats.gotc - adapter->gotc_old;
  2827. adapter->gotc_old = adapter->stats.gotc;
  2828. e1000e_update_adaptive(&adapter->hw);
  2829. if (!netif_carrier_ok(netdev)) {
  2830. tx_pending = (e1000_desc_unused(tx_ring) + 1 <
  2831. tx_ring->count);
  2832. if (tx_pending) {
  2833. /*
  2834. * We've lost link, so the controller stops DMA,
  2835. * but we've got queued Tx work that's never going
  2836. * to get done, so reset controller to flush Tx.
  2837. * (Do the reset outside of interrupt context).
  2838. */
  2839. adapter->tx_timeout_count++;
  2840. schedule_work(&adapter->reset_task);
  2841. }
  2842. }
  2843. /* Cause software interrupt to ensure Rx ring is cleaned */
  2844. ew32(ICS, E1000_ICS_RXDMT0);
  2845. /* Force detection of hung controller every watchdog period */
  2846. adapter->detect_tx_hung = 1;
  2847. /*
  2848. * With 82571 controllers, LAA may be overwritten due to controller
  2849. * reset from the other port. Set the appropriate LAA in RAR[0]
  2850. */
  2851. if (e1000e_get_laa_state_82571(hw))
  2852. e1000e_rar_set(hw, adapter->hw.mac.addr, 0);
  2853. /* Reset the timer */
  2854. if (!test_bit(__E1000_DOWN, &adapter->state))
  2855. mod_timer(&adapter->watchdog_timer,
  2856. round_jiffies(jiffies + 2 * HZ));
  2857. }
  2858. #define E1000_TX_FLAGS_CSUM 0x00000001
  2859. #define E1000_TX_FLAGS_VLAN 0x00000002
  2860. #define E1000_TX_FLAGS_TSO 0x00000004
  2861. #define E1000_TX_FLAGS_IPV4 0x00000008
  2862. #define E1000_TX_FLAGS_VLAN_MASK 0xffff0000
  2863. #define E1000_TX_FLAGS_VLAN_SHIFT 16
  2864. static int e1000_tso(struct e1000_adapter *adapter,
  2865. struct sk_buff *skb)
  2866. {
  2867. struct e1000_ring *tx_ring = adapter->tx_ring;
  2868. struct e1000_context_desc *context_desc;
  2869. struct e1000_buffer *buffer_info;
  2870. unsigned int i;
  2871. u32 cmd_length = 0;
  2872. u16 ipcse = 0, tucse, mss;
  2873. u8 ipcss, ipcso, tucss, tucso, hdr_len;
  2874. int err;
  2875. if (skb_is_gso(skb)) {
  2876. if (skb_header_cloned(skb)) {
  2877. err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
  2878. if (err)
  2879. return err;
  2880. }
  2881. hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
  2882. mss = skb_shinfo(skb)->gso_size;
  2883. if (skb->protocol == htons(ETH_P_IP)) {
  2884. struct iphdr *iph = ip_hdr(skb);
  2885. iph->tot_len = 0;
  2886. iph->check = 0;
  2887. tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
  2888. iph->daddr, 0,
  2889. IPPROTO_TCP,
  2890. 0);
  2891. cmd_length = E1000_TXD_CMD_IP;
  2892. ipcse = skb_transport_offset(skb) - 1;
  2893. } else if (skb_shinfo(skb)->gso_type == SKB_GSO_TCPV6) {
  2894. ipv6_hdr(skb)->payload_len = 0;
  2895. tcp_hdr(skb)->check =
  2896. ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
  2897. &ipv6_hdr(skb)->daddr,
  2898. 0, IPPROTO_TCP, 0);
  2899. ipcse = 0;
  2900. }
  2901. ipcss = skb_network_offset(skb);
  2902. ipcso = (void *)&(ip_hdr(skb)->check) - (void *)skb->data;
  2903. tucss = skb_transport_offset(skb);
  2904. tucso = (void *)&(tcp_hdr(skb)->check) - (void *)skb->data;
  2905. tucse = 0;
  2906. cmd_length |= (E1000_TXD_CMD_DEXT | E1000_TXD_CMD_TSE |
  2907. E1000_TXD_CMD_TCP | (skb->len - (hdr_len)));
  2908. i = tx_ring->next_to_use;
  2909. context_desc = E1000_CONTEXT_DESC(*tx_ring, i);
  2910. buffer_info = &tx_ring->buffer_info[i];
  2911. context_desc->lower_setup.ip_fields.ipcss = ipcss;
  2912. context_desc->lower_setup.ip_fields.ipcso = ipcso;
  2913. context_desc->lower_setup.ip_fields.ipcse = cpu_to_le16(ipcse);
  2914. context_desc->upper_setup.tcp_fields.tucss = tucss;
  2915. context_desc->upper_setup.tcp_fields.tucso = tucso;
  2916. context_desc->upper_setup.tcp_fields.tucse = cpu_to_le16(tucse);
  2917. context_desc->tcp_seg_setup.fields.mss = cpu_to_le16(mss);
  2918. context_desc->tcp_seg_setup.fields.hdr_len = hdr_len;
  2919. context_desc->cmd_and_length = cpu_to_le32(cmd_length);
  2920. buffer_info->time_stamp = jiffies;
  2921. buffer_info->next_to_watch = i;
  2922. i++;
  2923. if (i == tx_ring->count)
  2924. i = 0;
  2925. tx_ring->next_to_use = i;
  2926. return 1;
  2927. }
  2928. return 0;
  2929. }
  2930. static bool e1000_tx_csum(struct e1000_adapter *adapter, struct sk_buff *skb)
  2931. {
  2932. struct e1000_ring *tx_ring = adapter->tx_ring;
  2933. struct e1000_context_desc *context_desc;
  2934. struct e1000_buffer *buffer_info;
  2935. unsigned int i;
  2936. u8 css;
  2937. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  2938. css = skb_transport_offset(skb);
  2939. i = tx_ring->next_to_use;
  2940. buffer_info = &tx_ring->buffer_info[i];
  2941. context_desc = E1000_CONTEXT_DESC(*tx_ring, i);
  2942. context_desc->lower_setup.ip_config = 0;
  2943. context_desc->upper_setup.tcp_fields.tucss = css;
  2944. context_desc->upper_setup.tcp_fields.tucso =
  2945. css + skb->csum_offset;
  2946. context_desc->upper_setup.tcp_fields.tucse = 0;
  2947. context_desc->tcp_seg_setup.data = 0;
  2948. context_desc->cmd_and_length = cpu_to_le32(E1000_TXD_CMD_DEXT);
  2949. buffer_info->time_stamp = jiffies;
  2950. buffer_info->next_to_watch = i;
  2951. i++;
  2952. if (i == tx_ring->count)
  2953. i = 0;
  2954. tx_ring->next_to_use = i;
  2955. return 1;
  2956. }
  2957. return 0;
  2958. }
  2959. #define E1000_MAX_PER_TXD 8192
  2960. #define E1000_MAX_TXD_PWR 12
  2961. static int e1000_tx_map(struct e1000_adapter *adapter,
  2962. struct sk_buff *skb, unsigned int first,
  2963. unsigned int max_per_txd, unsigned int nr_frags,
  2964. unsigned int mss)
  2965. {
  2966. struct e1000_ring *tx_ring = adapter->tx_ring;
  2967. struct e1000_buffer *buffer_info;
  2968. unsigned int len = skb->len - skb->data_len;
  2969. unsigned int offset = 0, size, count = 0, i;
  2970. unsigned int f;
  2971. i = tx_ring->next_to_use;
  2972. while (len) {
  2973. buffer_info = &tx_ring->buffer_info[i];
  2974. size = min(len, max_per_txd);
  2975. /* Workaround for premature desc write-backs
  2976. * in TSO mode. Append 4-byte sentinel desc */
  2977. if (mss && !nr_frags && size == len && size > 8)
  2978. size -= 4;
  2979. buffer_info->length = size;
  2980. /* set time_stamp *before* dma to help avoid a possible race */
  2981. buffer_info->time_stamp = jiffies;
  2982. buffer_info->dma =
  2983. pci_map_single(adapter->pdev,
  2984. skb->data + offset,
  2985. size,
  2986. PCI_DMA_TODEVICE);
  2987. if (pci_dma_mapping_error(adapter->pdev, buffer_info->dma)) {
  2988. dev_err(&adapter->pdev->dev, "TX DMA map failed\n");
  2989. adapter->tx_dma_failed++;
  2990. return -1;
  2991. }
  2992. buffer_info->next_to_watch = i;
  2993. len -= size;
  2994. offset += size;
  2995. count++;
  2996. i++;
  2997. if (i == tx_ring->count)
  2998. i = 0;
  2999. }
  3000. for (f = 0; f < nr_frags; f++) {
  3001. struct skb_frag_struct *frag;
  3002. frag = &skb_shinfo(skb)->frags[f];
  3003. len = frag->size;
  3004. offset = frag->page_offset;
  3005. while (len) {
  3006. buffer_info = &tx_ring->buffer_info[i];
  3007. size = min(len, max_per_txd);
  3008. /* Workaround for premature desc write-backs
  3009. * in TSO mode. Append 4-byte sentinel desc */
  3010. if (mss && f == (nr_frags-1) && size == len && size > 8)
  3011. size -= 4;
  3012. buffer_info->length = size;
  3013. buffer_info->time_stamp = jiffies;
  3014. buffer_info->dma =
  3015. pci_map_page(adapter->pdev,
  3016. frag->page,
  3017. offset,
  3018. size,
  3019. PCI_DMA_TODEVICE);
  3020. if (pci_dma_mapping_error(adapter->pdev,
  3021. buffer_info->dma)) {
  3022. dev_err(&adapter->pdev->dev,
  3023. "TX DMA page map failed\n");
  3024. adapter->tx_dma_failed++;
  3025. return -1;
  3026. }
  3027. buffer_info->next_to_watch = i;
  3028. len -= size;
  3029. offset += size;
  3030. count++;
  3031. i++;
  3032. if (i == tx_ring->count)
  3033. i = 0;
  3034. }
  3035. }
  3036. if (i == 0)
  3037. i = tx_ring->count - 1;
  3038. else
  3039. i--;
  3040. tx_ring->buffer_info[i].skb = skb;
  3041. tx_ring->buffer_info[first].next_to_watch = i;
  3042. return count;
  3043. }
  3044. static void e1000_tx_queue(struct e1000_adapter *adapter,
  3045. int tx_flags, int count)
  3046. {
  3047. struct e1000_ring *tx_ring = adapter->tx_ring;
  3048. struct e1000_tx_desc *tx_desc = NULL;
  3049. struct e1000_buffer *buffer_info;
  3050. u32 txd_upper = 0, txd_lower = E1000_TXD_CMD_IFCS;
  3051. unsigned int i;
  3052. if (tx_flags & E1000_TX_FLAGS_TSO) {
  3053. txd_lower |= E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D |
  3054. E1000_TXD_CMD_TSE;
  3055. txd_upper |= E1000_TXD_POPTS_TXSM << 8;
  3056. if (tx_flags & E1000_TX_FLAGS_IPV4)
  3057. txd_upper |= E1000_TXD_POPTS_IXSM << 8;
  3058. }
  3059. if (tx_flags & E1000_TX_FLAGS_CSUM) {
  3060. txd_lower |= E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D;
  3061. txd_upper |= E1000_TXD_POPTS_TXSM << 8;
  3062. }
  3063. if (tx_flags & E1000_TX_FLAGS_VLAN) {
  3064. txd_lower |= E1000_TXD_CMD_VLE;
  3065. txd_upper |= (tx_flags & E1000_TX_FLAGS_VLAN_MASK);
  3066. }
  3067. i = tx_ring->next_to_use;
  3068. while (count--) {
  3069. buffer_info = &tx_ring->buffer_info[i];
  3070. tx_desc = E1000_TX_DESC(*tx_ring, i);
  3071. tx_desc->buffer_addr = cpu_to_le64(buffer_info->dma);
  3072. tx_desc->lower.data =
  3073. cpu_to_le32(txd_lower | buffer_info->length);
  3074. tx_desc->upper.data = cpu_to_le32(txd_upper);
  3075. i++;
  3076. if (i == tx_ring->count)
  3077. i = 0;
  3078. }
  3079. tx_desc->lower.data |= cpu_to_le32(adapter->txd_cmd);
  3080. /*
  3081. * Force memory writes to complete before letting h/w
  3082. * know there are new descriptors to fetch. (Only
  3083. * applicable for weak-ordered memory model archs,
  3084. * such as IA-64).
  3085. */
  3086. wmb();
  3087. tx_ring->next_to_use = i;
  3088. writel(i, adapter->hw.hw_addr + tx_ring->tail);
  3089. /*
  3090. * we need this if more than one processor can write to our tail
  3091. * at a time, it synchronizes IO on IA64/Altix systems
  3092. */
  3093. mmiowb();
  3094. }
  3095. #define MINIMUM_DHCP_PACKET_SIZE 282
  3096. static int e1000_transfer_dhcp_info(struct e1000_adapter *adapter,
  3097. struct sk_buff *skb)
  3098. {
  3099. struct e1000_hw *hw = &adapter->hw;
  3100. u16 length, offset;
  3101. if (vlan_tx_tag_present(skb)) {
  3102. if (!((vlan_tx_tag_get(skb) == adapter->hw.mng_cookie.vlan_id)
  3103. && (adapter->hw.mng_cookie.status &
  3104. E1000_MNG_DHCP_COOKIE_STATUS_VLAN)))
  3105. return 0;
  3106. }
  3107. if (skb->len <= MINIMUM_DHCP_PACKET_SIZE)
  3108. return 0;
  3109. if (((struct ethhdr *) skb->data)->h_proto != htons(ETH_P_IP))
  3110. return 0;
  3111. {
  3112. const struct iphdr *ip = (struct iphdr *)((u8 *)skb->data+14);
  3113. struct udphdr *udp;
  3114. if (ip->protocol != IPPROTO_UDP)
  3115. return 0;
  3116. udp = (struct udphdr *)((u8 *)ip + (ip->ihl << 2));
  3117. if (ntohs(udp->dest) != 67)
  3118. return 0;
  3119. offset = (u8 *)udp + 8 - skb->data;
  3120. length = skb->len - offset;
  3121. return e1000e_mng_write_dhcp_info(hw, (u8 *)udp + 8, length);
  3122. }
  3123. return 0;
  3124. }
  3125. static int __e1000_maybe_stop_tx(struct net_device *netdev, int size)
  3126. {
  3127. struct e1000_adapter *adapter = netdev_priv(netdev);
  3128. netif_stop_queue(netdev);
  3129. /*
  3130. * Herbert's original patch had:
  3131. * smp_mb__after_netif_stop_queue();
  3132. * but since that doesn't exist yet, just open code it.
  3133. */
  3134. smp_mb();
  3135. /*
  3136. * We need to check again in a case another CPU has just
  3137. * made room available.
  3138. */
  3139. if (e1000_desc_unused(adapter->tx_ring) < size)
  3140. return -EBUSY;
  3141. /* A reprieve! */
  3142. netif_start_queue(netdev);
  3143. ++adapter->restart_queue;
  3144. return 0;
  3145. }
  3146. static int e1000_maybe_stop_tx(struct net_device *netdev, int size)
  3147. {
  3148. struct e1000_adapter *adapter = netdev_priv(netdev);
  3149. if (e1000_desc_unused(adapter->tx_ring) >= size)
  3150. return 0;
  3151. return __e1000_maybe_stop_tx(netdev, size);
  3152. }
  3153. #define TXD_USE_COUNT(S, X) (((S) >> (X)) + 1 )
  3154. static int e1000_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
  3155. {
  3156. struct e1000_adapter *adapter = netdev_priv(netdev);
  3157. struct e1000_ring *tx_ring = adapter->tx_ring;
  3158. unsigned int first;
  3159. unsigned int max_per_txd = E1000_MAX_PER_TXD;
  3160. unsigned int max_txd_pwr = E1000_MAX_TXD_PWR;
  3161. unsigned int tx_flags = 0;
  3162. unsigned int len = skb->len - skb->data_len;
  3163. unsigned long irq_flags;
  3164. unsigned int nr_frags;
  3165. unsigned int mss;
  3166. int count = 0;
  3167. int tso;
  3168. unsigned int f;
  3169. if (test_bit(__E1000_DOWN, &adapter->state)) {
  3170. dev_kfree_skb_any(skb);
  3171. return NETDEV_TX_OK;
  3172. }
  3173. if (skb->len <= 0) {
  3174. dev_kfree_skb_any(skb);
  3175. return NETDEV_TX_OK;
  3176. }
  3177. mss = skb_shinfo(skb)->gso_size;
  3178. /*
  3179. * The controller does a simple calculation to
  3180. * make sure there is enough room in the FIFO before
  3181. * initiating the DMA for each buffer. The calc is:
  3182. * 4 = ceil(buffer len/mss). To make sure we don't
  3183. * overrun the FIFO, adjust the max buffer len if mss
  3184. * drops.
  3185. */
  3186. if (mss) {
  3187. u8 hdr_len;
  3188. max_per_txd = min(mss << 2, max_per_txd);
  3189. max_txd_pwr = fls(max_per_txd) - 1;
  3190. /*
  3191. * TSO Workaround for 82571/2/3 Controllers -- if skb->data
  3192. * points to just header, pull a few bytes of payload from
  3193. * frags into skb->data
  3194. */
  3195. hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
  3196. /*
  3197. * we do this workaround for ES2LAN, but it is un-necessary,
  3198. * avoiding it could save a lot of cycles
  3199. */
  3200. if (skb->data_len && (hdr_len == len)) {
  3201. unsigned int pull_size;
  3202. pull_size = min((unsigned int)4, skb->data_len);
  3203. if (!__pskb_pull_tail(skb, pull_size)) {
  3204. e_err("__pskb_pull_tail failed.\n");
  3205. dev_kfree_skb_any(skb);
  3206. return NETDEV_TX_OK;
  3207. }
  3208. len = skb->len - skb->data_len;
  3209. }
  3210. }
  3211. /* reserve a descriptor for the offload context */
  3212. if ((mss) || (skb->ip_summed == CHECKSUM_PARTIAL))
  3213. count++;
  3214. count++;
  3215. count += TXD_USE_COUNT(len, max_txd_pwr);
  3216. nr_frags = skb_shinfo(skb)->nr_frags;
  3217. for (f = 0; f < nr_frags; f++)
  3218. count += TXD_USE_COUNT(skb_shinfo(skb)->frags[f].size,
  3219. max_txd_pwr);
  3220. if (adapter->hw.mac.tx_pkt_filtering)
  3221. e1000_transfer_dhcp_info(adapter, skb);
  3222. if (!spin_trylock_irqsave(&adapter->tx_queue_lock, irq_flags))
  3223. /* Collision - tell upper layer to requeue */
  3224. return NETDEV_TX_LOCKED;
  3225. /*
  3226. * need: count + 2 desc gap to keep tail from touching
  3227. * head, otherwise try next time
  3228. */
  3229. if (e1000_maybe_stop_tx(netdev, count + 2)) {
  3230. spin_unlock_irqrestore(&adapter->tx_queue_lock, irq_flags);
  3231. return NETDEV_TX_BUSY;
  3232. }
  3233. if (adapter->vlgrp && vlan_tx_tag_present(skb)) {
  3234. tx_flags |= E1000_TX_FLAGS_VLAN;
  3235. tx_flags |= (vlan_tx_tag_get(skb) << E1000_TX_FLAGS_VLAN_SHIFT);
  3236. }
  3237. first = tx_ring->next_to_use;
  3238. tso = e1000_tso(adapter, skb);
  3239. if (tso < 0) {
  3240. dev_kfree_skb_any(skb);
  3241. spin_unlock_irqrestore(&adapter->tx_queue_lock, irq_flags);
  3242. return NETDEV_TX_OK;
  3243. }
  3244. if (tso)
  3245. tx_flags |= E1000_TX_FLAGS_TSO;
  3246. else if (e1000_tx_csum(adapter, skb))
  3247. tx_flags |= E1000_TX_FLAGS_CSUM;
  3248. /*
  3249. * Old method was to assume IPv4 packet by default if TSO was enabled.
  3250. * 82571 hardware supports TSO capabilities for IPv6 as well...
  3251. * no longer assume, we must.
  3252. */
  3253. if (skb->protocol == htons(ETH_P_IP))
  3254. tx_flags |= E1000_TX_FLAGS_IPV4;
  3255. count = e1000_tx_map(adapter, skb, first, max_per_txd, nr_frags, mss);
  3256. if (count < 0) {
  3257. /* handle pci_map_single() error in e1000_tx_map */
  3258. dev_kfree_skb_any(skb);
  3259. spin_unlock_irqrestore(&adapter->tx_queue_lock, irq_flags);
  3260. return NETDEV_TX_OK;
  3261. }
  3262. e1000_tx_queue(adapter, tx_flags, count);
  3263. netdev->trans_start = jiffies;
  3264. /* Make sure there is space in the ring for the next send. */
  3265. e1000_maybe_stop_tx(netdev, MAX_SKB_FRAGS + 2);
  3266. spin_unlock_irqrestore(&adapter->tx_queue_lock, irq_flags);
  3267. return NETDEV_TX_OK;
  3268. }
  3269. /**
  3270. * e1000_tx_timeout - Respond to a Tx Hang
  3271. * @netdev: network interface device structure
  3272. **/
  3273. static void e1000_tx_timeout(struct net_device *netdev)
  3274. {
  3275. struct e1000_adapter *adapter = netdev_priv(netdev);
  3276. /* Do the reset outside of interrupt context */
  3277. adapter->tx_timeout_count++;
  3278. schedule_work(&adapter->reset_task);
  3279. }
  3280. static void e1000_reset_task(struct work_struct *work)
  3281. {
  3282. struct e1000_adapter *adapter;
  3283. adapter = container_of(work, struct e1000_adapter, reset_task);
  3284. e1000e_reinit_locked(adapter);
  3285. }
  3286. /**
  3287. * e1000_get_stats - Get System Network Statistics
  3288. * @netdev: network interface device structure
  3289. *
  3290. * Returns the address of the device statistics structure.
  3291. * The statistics are actually updated from the timer callback.
  3292. **/
  3293. static struct net_device_stats *e1000_get_stats(struct net_device *netdev)
  3294. {
  3295. struct e1000_adapter *adapter = netdev_priv(netdev);
  3296. /* only return the current stats */
  3297. return &adapter->net_stats;
  3298. }
  3299. /**
  3300. * e1000_change_mtu - Change the Maximum Transfer Unit
  3301. * @netdev: network interface device structure
  3302. * @new_mtu: new value for maximum frame size
  3303. *
  3304. * Returns 0 on success, negative on failure
  3305. **/
  3306. static int e1000_change_mtu(struct net_device *netdev, int new_mtu)
  3307. {
  3308. struct e1000_adapter *adapter = netdev_priv(netdev);
  3309. int max_frame = new_mtu + ETH_HLEN + ETH_FCS_LEN;
  3310. if ((new_mtu < ETH_ZLEN + ETH_FCS_LEN + VLAN_HLEN) ||
  3311. (max_frame > MAX_JUMBO_FRAME_SIZE)) {
  3312. e_err("Invalid MTU setting\n");
  3313. return -EINVAL;
  3314. }
  3315. /* Jumbo frame size limits */
  3316. if (max_frame > ETH_FRAME_LEN + ETH_FCS_LEN) {
  3317. if (!(adapter->flags & FLAG_HAS_JUMBO_FRAMES)) {
  3318. e_err("Jumbo Frames not supported.\n");
  3319. return -EINVAL;
  3320. }
  3321. if (adapter->hw.phy.type == e1000_phy_ife) {
  3322. e_err("Jumbo Frames not supported.\n");
  3323. return -EINVAL;
  3324. }
  3325. }
  3326. #define MAX_STD_JUMBO_FRAME_SIZE 9234
  3327. if (max_frame > MAX_STD_JUMBO_FRAME_SIZE) {
  3328. e_err("MTU > 9216 not supported.\n");
  3329. return -EINVAL;
  3330. }
  3331. while (test_and_set_bit(__E1000_RESETTING, &adapter->state))
  3332. msleep(1);
  3333. /* e1000e_down has a dependency on max_frame_size */
  3334. adapter->max_frame_size = max_frame;
  3335. if (netif_running(netdev))
  3336. e1000e_down(adapter);
  3337. /*
  3338. * NOTE: netdev_alloc_skb reserves 16 bytes, and typically NET_IP_ALIGN
  3339. * means we reserve 2 more, this pushes us to allocate from the next
  3340. * larger slab size.
  3341. * i.e. RXBUFFER_2048 --> size-4096 slab
  3342. * However with the new *_jumbo_rx* routines, jumbo receives will use
  3343. * fragmented skbs
  3344. */
  3345. if (max_frame <= 256)
  3346. adapter->rx_buffer_len = 256;
  3347. else if (max_frame <= 512)
  3348. adapter->rx_buffer_len = 512;
  3349. else if (max_frame <= 1024)
  3350. adapter->rx_buffer_len = 1024;
  3351. else if (max_frame <= 2048)
  3352. adapter->rx_buffer_len = 2048;
  3353. else
  3354. adapter->rx_buffer_len = 4096;
  3355. /* adjust allocation if LPE protects us, and we aren't using SBP */
  3356. if ((max_frame == ETH_FRAME_LEN + ETH_FCS_LEN) ||
  3357. (max_frame == ETH_FRAME_LEN + VLAN_HLEN + ETH_FCS_LEN))
  3358. adapter->rx_buffer_len = ETH_FRAME_LEN + VLAN_HLEN
  3359. + ETH_FCS_LEN;
  3360. e_info("changing MTU from %d to %d\n", netdev->mtu, new_mtu);
  3361. netdev->mtu = new_mtu;
  3362. if (netif_running(netdev))
  3363. e1000e_up(adapter);
  3364. else
  3365. e1000e_reset(adapter);
  3366. clear_bit(__E1000_RESETTING, &adapter->state);
  3367. return 0;
  3368. }
  3369. static int e1000_mii_ioctl(struct net_device *netdev, struct ifreq *ifr,
  3370. int cmd)
  3371. {
  3372. struct e1000_adapter *adapter = netdev_priv(netdev);
  3373. struct mii_ioctl_data *data = if_mii(ifr);
  3374. if (adapter->hw.phy.media_type != e1000_media_type_copper)
  3375. return -EOPNOTSUPP;
  3376. switch (cmd) {
  3377. case SIOCGMIIPHY:
  3378. data->phy_id = adapter->hw.phy.addr;
  3379. break;
  3380. case SIOCGMIIREG:
  3381. if (!capable(CAP_NET_ADMIN))
  3382. return -EPERM;
  3383. switch (data->reg_num & 0x1F) {
  3384. case MII_BMCR:
  3385. data->val_out = adapter->phy_regs.bmcr;
  3386. break;
  3387. case MII_BMSR:
  3388. data->val_out = adapter->phy_regs.bmsr;
  3389. break;
  3390. case MII_PHYSID1:
  3391. data->val_out = (adapter->hw.phy.id >> 16);
  3392. break;
  3393. case MII_PHYSID2:
  3394. data->val_out = (adapter->hw.phy.id & 0xFFFF);
  3395. break;
  3396. case MII_ADVERTISE:
  3397. data->val_out = adapter->phy_regs.advertise;
  3398. break;
  3399. case MII_LPA:
  3400. data->val_out = adapter->phy_regs.lpa;
  3401. break;
  3402. case MII_EXPANSION:
  3403. data->val_out = adapter->phy_regs.expansion;
  3404. break;
  3405. case MII_CTRL1000:
  3406. data->val_out = adapter->phy_regs.ctrl1000;
  3407. break;
  3408. case MII_STAT1000:
  3409. data->val_out = adapter->phy_regs.stat1000;
  3410. break;
  3411. case MII_ESTATUS:
  3412. data->val_out = adapter->phy_regs.estatus;
  3413. break;
  3414. default:
  3415. return -EIO;
  3416. }
  3417. break;
  3418. case SIOCSMIIREG:
  3419. default:
  3420. return -EOPNOTSUPP;
  3421. }
  3422. return 0;
  3423. }
  3424. static int e1000_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
  3425. {
  3426. switch (cmd) {
  3427. case SIOCGMIIPHY:
  3428. case SIOCGMIIREG:
  3429. case SIOCSMIIREG:
  3430. return e1000_mii_ioctl(netdev, ifr, cmd);
  3431. default:
  3432. return -EOPNOTSUPP;
  3433. }
  3434. }
  3435. static int e1000_suspend(struct pci_dev *pdev, pm_message_t state)
  3436. {
  3437. struct net_device *netdev = pci_get_drvdata(pdev);
  3438. struct e1000_adapter *adapter = netdev_priv(netdev);
  3439. struct e1000_hw *hw = &adapter->hw;
  3440. u32 ctrl, ctrl_ext, rctl, status;
  3441. u32 wufc = adapter->wol;
  3442. int retval = 0;
  3443. netif_device_detach(netdev);
  3444. if (netif_running(netdev)) {
  3445. WARN_ON(test_bit(__E1000_RESETTING, &adapter->state));
  3446. e1000e_down(adapter);
  3447. e1000_free_irq(adapter);
  3448. }
  3449. retval = pci_save_state(pdev);
  3450. if (retval)
  3451. return retval;
  3452. status = er32(STATUS);
  3453. if (status & E1000_STATUS_LU)
  3454. wufc &= ~E1000_WUFC_LNKC;
  3455. if (wufc) {
  3456. e1000_setup_rctl(adapter);
  3457. e1000_set_multi(netdev);
  3458. /* turn on all-multi mode if wake on multicast is enabled */
  3459. if (wufc & E1000_WUFC_MC) {
  3460. rctl = er32(RCTL);
  3461. rctl |= E1000_RCTL_MPE;
  3462. ew32(RCTL, rctl);
  3463. }
  3464. ctrl = er32(CTRL);
  3465. /* advertise wake from D3Cold */
  3466. #define E1000_CTRL_ADVD3WUC 0x00100000
  3467. /* phy power management enable */
  3468. #define E1000_CTRL_EN_PHY_PWR_MGMT 0x00200000
  3469. ctrl |= E1000_CTRL_ADVD3WUC |
  3470. E1000_CTRL_EN_PHY_PWR_MGMT;
  3471. ew32(CTRL, ctrl);
  3472. if (adapter->hw.phy.media_type == e1000_media_type_fiber ||
  3473. adapter->hw.phy.media_type ==
  3474. e1000_media_type_internal_serdes) {
  3475. /* keep the laser running in D3 */
  3476. ctrl_ext = er32(CTRL_EXT);
  3477. ctrl_ext |= E1000_CTRL_EXT_SDP7_DATA;
  3478. ew32(CTRL_EXT, ctrl_ext);
  3479. }
  3480. if (adapter->flags & FLAG_IS_ICH)
  3481. e1000e_disable_gig_wol_ich8lan(&adapter->hw);
  3482. /* Allow time for pending master requests to run */
  3483. e1000e_disable_pcie_master(&adapter->hw);
  3484. ew32(WUC, E1000_WUC_PME_EN);
  3485. ew32(WUFC, wufc);
  3486. pci_enable_wake(pdev, PCI_D3hot, 1);
  3487. pci_enable_wake(pdev, PCI_D3cold, 1);
  3488. } else {
  3489. ew32(WUC, 0);
  3490. ew32(WUFC, 0);
  3491. pci_enable_wake(pdev, PCI_D3hot, 0);
  3492. pci_enable_wake(pdev, PCI_D3cold, 0);
  3493. }
  3494. /* make sure adapter isn't asleep if manageability is enabled */
  3495. if (adapter->flags & FLAG_MNG_PT_ENABLED) {
  3496. pci_enable_wake(pdev, PCI_D3hot, 1);
  3497. pci_enable_wake(pdev, PCI_D3cold, 1);
  3498. }
  3499. if (adapter->hw.phy.type == e1000_phy_igp_3)
  3500. e1000e_igp3_phy_powerdown_workaround_ich8lan(&adapter->hw);
  3501. /*
  3502. * Release control of h/w to f/w. If f/w is AMT enabled, this
  3503. * would have already happened in close and is redundant.
  3504. */
  3505. e1000_release_hw_control(adapter);
  3506. pci_disable_device(pdev);
  3507. pci_set_power_state(pdev, pci_choose_state(pdev, state));
  3508. return 0;
  3509. }
  3510. static void e1000e_disable_l1aspm(struct pci_dev *pdev)
  3511. {
  3512. int pos;
  3513. u16 val;
  3514. /*
  3515. * 82573 workaround - disable L1 ASPM on mobile chipsets
  3516. *
  3517. * L1 ASPM on various mobile (ich7) chipsets do not behave properly
  3518. * resulting in lost data or garbage information on the pci-e link
  3519. * level. This could result in (false) bad EEPROM checksum errors,
  3520. * long ping times (up to 2s) or even a system freeze/hang.
  3521. *
  3522. * Unfortunately this feature saves about 1W power consumption when
  3523. * active.
  3524. */
  3525. pos = pci_find_capability(pdev, PCI_CAP_ID_EXP);
  3526. pci_read_config_word(pdev, pos + PCI_EXP_LNKCTL, &val);
  3527. if (val & 0x2) {
  3528. dev_warn(&pdev->dev, "Disabling L1 ASPM\n");
  3529. val &= ~0x2;
  3530. pci_write_config_word(pdev, pos + PCI_EXP_LNKCTL, val);
  3531. }
  3532. }
  3533. #ifdef CONFIG_PM
  3534. static int e1000_resume(struct pci_dev *pdev)
  3535. {
  3536. struct net_device *netdev = pci_get_drvdata(pdev);
  3537. struct e1000_adapter *adapter = netdev_priv(netdev);
  3538. struct e1000_hw *hw = &adapter->hw;
  3539. u32 err;
  3540. pci_set_power_state(pdev, PCI_D0);
  3541. pci_restore_state(pdev);
  3542. e1000e_disable_l1aspm(pdev);
  3543. err = pci_enable_device_mem(pdev);
  3544. if (err) {
  3545. dev_err(&pdev->dev,
  3546. "Cannot enable PCI device from suspend\n");
  3547. return err;
  3548. }
  3549. pci_set_master(pdev);
  3550. pci_enable_wake(pdev, PCI_D3hot, 0);
  3551. pci_enable_wake(pdev, PCI_D3cold, 0);
  3552. if (netif_running(netdev)) {
  3553. err = e1000_request_irq(adapter);
  3554. if (err)
  3555. return err;
  3556. }
  3557. e1000e_power_up_phy(adapter);
  3558. e1000e_reset(adapter);
  3559. ew32(WUS, ~0);
  3560. e1000_init_manageability(adapter);
  3561. if (netif_running(netdev))
  3562. e1000e_up(adapter);
  3563. netif_device_attach(netdev);
  3564. /*
  3565. * If the controller has AMT, do not set DRV_LOAD until the interface
  3566. * is up. For all other cases, let the f/w know that the h/w is now
  3567. * under the control of the driver.
  3568. */
  3569. if (!(adapter->flags & FLAG_HAS_AMT))
  3570. e1000_get_hw_control(adapter);
  3571. return 0;
  3572. }
  3573. #endif
  3574. static void e1000_shutdown(struct pci_dev *pdev)
  3575. {
  3576. e1000_suspend(pdev, PMSG_SUSPEND);
  3577. }
  3578. #ifdef CONFIG_NET_POLL_CONTROLLER
  3579. /*
  3580. * Polling 'interrupt' - used by things like netconsole to send skbs
  3581. * without having to re-enable interrupts. It's not called while
  3582. * the interrupt routine is executing.
  3583. */
  3584. static void e1000_netpoll(struct net_device *netdev)
  3585. {
  3586. struct e1000_adapter *adapter = netdev_priv(netdev);
  3587. disable_irq(adapter->pdev->irq);
  3588. e1000_intr(adapter->pdev->irq, netdev);
  3589. enable_irq(adapter->pdev->irq);
  3590. }
  3591. #endif
  3592. /**
  3593. * e1000_io_error_detected - called when PCI error is detected
  3594. * @pdev: Pointer to PCI device
  3595. * @state: The current pci connection state
  3596. *
  3597. * This function is called after a PCI bus error affecting
  3598. * this device has been detected.
  3599. */
  3600. static pci_ers_result_t e1000_io_error_detected(struct pci_dev *pdev,
  3601. pci_channel_state_t state)
  3602. {
  3603. struct net_device *netdev = pci_get_drvdata(pdev);
  3604. struct e1000_adapter *adapter = netdev_priv(netdev);
  3605. netif_device_detach(netdev);
  3606. if (netif_running(netdev))
  3607. e1000e_down(adapter);
  3608. pci_disable_device(pdev);
  3609. /* Request a slot slot reset. */
  3610. return PCI_ERS_RESULT_NEED_RESET;
  3611. }
  3612. /**
  3613. * e1000_io_slot_reset - called after the pci bus has been reset.
  3614. * @pdev: Pointer to PCI device
  3615. *
  3616. * Restart the card from scratch, as if from a cold-boot. Implementation
  3617. * resembles the first-half of the e1000_resume routine.
  3618. */
  3619. static pci_ers_result_t e1000_io_slot_reset(struct pci_dev *pdev)
  3620. {
  3621. struct net_device *netdev = pci_get_drvdata(pdev);
  3622. struct e1000_adapter *adapter = netdev_priv(netdev);
  3623. struct e1000_hw *hw = &adapter->hw;
  3624. int err;
  3625. e1000e_disable_l1aspm(pdev);
  3626. err = pci_enable_device_mem(pdev);
  3627. if (err) {
  3628. dev_err(&pdev->dev,
  3629. "Cannot re-enable PCI device after reset.\n");
  3630. return PCI_ERS_RESULT_DISCONNECT;
  3631. }
  3632. pci_set_master(pdev);
  3633. pci_restore_state(pdev);
  3634. pci_enable_wake(pdev, PCI_D3hot, 0);
  3635. pci_enable_wake(pdev, PCI_D3cold, 0);
  3636. e1000e_reset(adapter);
  3637. ew32(WUS, ~0);
  3638. return PCI_ERS_RESULT_RECOVERED;
  3639. }
  3640. /**
  3641. * e1000_io_resume - called when traffic can start flowing again.
  3642. * @pdev: Pointer to PCI device
  3643. *
  3644. * This callback is called when the error recovery driver tells us that
  3645. * its OK to resume normal operation. Implementation resembles the
  3646. * second-half of the e1000_resume routine.
  3647. */
  3648. static void e1000_io_resume(struct pci_dev *pdev)
  3649. {
  3650. struct net_device *netdev = pci_get_drvdata(pdev);
  3651. struct e1000_adapter *adapter = netdev_priv(netdev);
  3652. e1000_init_manageability(adapter);
  3653. if (netif_running(netdev)) {
  3654. if (e1000e_up(adapter)) {
  3655. dev_err(&pdev->dev,
  3656. "can't bring device back up after reset\n");
  3657. return;
  3658. }
  3659. }
  3660. netif_device_attach(netdev);
  3661. /*
  3662. * If the controller has AMT, do not set DRV_LOAD until the interface
  3663. * is up. For all other cases, let the f/w know that the h/w is now
  3664. * under the control of the driver.
  3665. */
  3666. if (!(adapter->flags & FLAG_HAS_AMT))
  3667. e1000_get_hw_control(adapter);
  3668. }
  3669. static void e1000_print_device_info(struct e1000_adapter *adapter)
  3670. {
  3671. struct e1000_hw *hw = &adapter->hw;
  3672. struct net_device *netdev = adapter->netdev;
  3673. u32 pba_num;
  3674. /* print bus type/speed/width info */
  3675. e_info("(PCI Express:2.5GB/s:%s) %02x:%02x:%02x:%02x:%02x:%02x\n",
  3676. /* bus width */
  3677. ((hw->bus.width == e1000_bus_width_pcie_x4) ? "Width x4" :
  3678. "Width x1"),
  3679. /* MAC address */
  3680. netdev->dev_addr[0], netdev->dev_addr[1],
  3681. netdev->dev_addr[2], netdev->dev_addr[3],
  3682. netdev->dev_addr[4], netdev->dev_addr[5]);
  3683. e_info("Intel(R) PRO/%s Network Connection\n",
  3684. (hw->phy.type == e1000_phy_ife) ? "10/100" : "1000");
  3685. e1000e_read_pba_num(hw, &pba_num);
  3686. e_info("MAC: %d, PHY: %d, PBA No: %06x-%03x\n",
  3687. hw->mac.type, hw->phy.type, (pba_num >> 8), (pba_num & 0xff));
  3688. }
  3689. static void e1000_eeprom_checks(struct e1000_adapter *adapter)
  3690. {
  3691. struct e1000_hw *hw = &adapter->hw;
  3692. int ret_val;
  3693. u16 buf = 0;
  3694. if (hw->mac.type != e1000_82573)
  3695. return;
  3696. ret_val = e1000_read_nvm(hw, NVM_INIT_CONTROL2_REG, 1, &buf);
  3697. if (!(le16_to_cpu(buf) & (1 << 0))) {
  3698. /* Deep Smart Power Down (DSPD) */
  3699. e_warn("Warning: detected DSPD enabled in EEPROM\n");
  3700. }
  3701. ret_val = e1000_read_nvm(hw, NVM_INIT_3GIO_3, 1, &buf);
  3702. if (le16_to_cpu(buf) & (3 << 2)) {
  3703. /* ASPM enable */
  3704. e_warn("Warning: detected ASPM enabled in EEPROM\n");
  3705. }
  3706. }
  3707. /**
  3708. * e1000_probe - Device Initialization Routine
  3709. * @pdev: PCI device information struct
  3710. * @ent: entry in e1000_pci_tbl
  3711. *
  3712. * Returns 0 on success, negative on failure
  3713. *
  3714. * e1000_probe initializes an adapter identified by a pci_dev structure.
  3715. * The OS initialization, configuring of the adapter private structure,
  3716. * and a hardware reset occur.
  3717. **/
  3718. static int __devinit e1000_probe(struct pci_dev *pdev,
  3719. const struct pci_device_id *ent)
  3720. {
  3721. struct net_device *netdev;
  3722. struct e1000_adapter *adapter;
  3723. struct e1000_hw *hw;
  3724. const struct e1000_info *ei = e1000_info_tbl[ent->driver_data];
  3725. resource_size_t mmio_start, mmio_len;
  3726. resource_size_t flash_start, flash_len;
  3727. static int cards_found;
  3728. int i, err, pci_using_dac;
  3729. u16 eeprom_data = 0;
  3730. u16 eeprom_apme_mask = E1000_EEPROM_APME;
  3731. e1000e_disable_l1aspm(pdev);
  3732. err = pci_enable_device_mem(pdev);
  3733. if (err)
  3734. return err;
  3735. pci_using_dac = 0;
  3736. err = pci_set_dma_mask(pdev, DMA_64BIT_MASK);
  3737. if (!err) {
  3738. err = pci_set_consistent_dma_mask(pdev, DMA_64BIT_MASK);
  3739. if (!err)
  3740. pci_using_dac = 1;
  3741. } else {
  3742. err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
  3743. if (err) {
  3744. err = pci_set_consistent_dma_mask(pdev,
  3745. DMA_32BIT_MASK);
  3746. if (err) {
  3747. dev_err(&pdev->dev, "No usable DMA "
  3748. "configuration, aborting\n");
  3749. goto err_dma;
  3750. }
  3751. }
  3752. }
  3753. err = pci_request_selected_regions(pdev,
  3754. pci_select_bars(pdev, IORESOURCE_MEM),
  3755. e1000e_driver_name);
  3756. if (err)
  3757. goto err_pci_reg;
  3758. pci_set_master(pdev);
  3759. pci_save_state(pdev);
  3760. err = -ENOMEM;
  3761. netdev = alloc_etherdev(sizeof(struct e1000_adapter));
  3762. if (!netdev)
  3763. goto err_alloc_etherdev;
  3764. SET_NETDEV_DEV(netdev, &pdev->dev);
  3765. pci_set_drvdata(pdev, netdev);
  3766. adapter = netdev_priv(netdev);
  3767. hw = &adapter->hw;
  3768. adapter->netdev = netdev;
  3769. adapter->pdev = pdev;
  3770. adapter->ei = ei;
  3771. adapter->pba = ei->pba;
  3772. adapter->flags = ei->flags;
  3773. adapter->hw.adapter = adapter;
  3774. adapter->hw.mac.type = ei->mac;
  3775. adapter->msg_enable = (1 << NETIF_MSG_DRV | NETIF_MSG_PROBE) - 1;
  3776. mmio_start = pci_resource_start(pdev, 0);
  3777. mmio_len = pci_resource_len(pdev, 0);
  3778. err = -EIO;
  3779. adapter->hw.hw_addr = ioremap(mmio_start, mmio_len);
  3780. if (!adapter->hw.hw_addr)
  3781. goto err_ioremap;
  3782. if ((adapter->flags & FLAG_HAS_FLASH) &&
  3783. (pci_resource_flags(pdev, 1) & IORESOURCE_MEM)) {
  3784. flash_start = pci_resource_start(pdev, 1);
  3785. flash_len = pci_resource_len(pdev, 1);
  3786. adapter->hw.flash_address = ioremap(flash_start, flash_len);
  3787. if (!adapter->hw.flash_address)
  3788. goto err_flashmap;
  3789. }
  3790. /* construct the net_device struct */
  3791. netdev->open = &e1000_open;
  3792. netdev->stop = &e1000_close;
  3793. netdev->hard_start_xmit = &e1000_xmit_frame;
  3794. netdev->get_stats = &e1000_get_stats;
  3795. netdev->set_multicast_list = &e1000_set_multi;
  3796. netdev->set_mac_address = &e1000_set_mac;
  3797. netdev->change_mtu = &e1000_change_mtu;
  3798. netdev->do_ioctl = &e1000_ioctl;
  3799. e1000e_set_ethtool_ops(netdev);
  3800. netdev->tx_timeout = &e1000_tx_timeout;
  3801. netdev->watchdog_timeo = 5 * HZ;
  3802. netif_napi_add(netdev, &adapter->napi, e1000_clean, 64);
  3803. netdev->vlan_rx_register = e1000_vlan_rx_register;
  3804. netdev->vlan_rx_add_vid = e1000_vlan_rx_add_vid;
  3805. netdev->vlan_rx_kill_vid = e1000_vlan_rx_kill_vid;
  3806. #ifdef CONFIG_NET_POLL_CONTROLLER
  3807. netdev->poll_controller = e1000_netpoll;
  3808. #endif
  3809. strncpy(netdev->name, pci_name(pdev), sizeof(netdev->name) - 1);
  3810. netdev->mem_start = mmio_start;
  3811. netdev->mem_end = mmio_start + mmio_len;
  3812. adapter->bd_number = cards_found++;
  3813. e1000e_check_options(adapter);
  3814. /* setup adapter struct */
  3815. err = e1000_sw_init(adapter);
  3816. if (err)
  3817. goto err_sw_init;
  3818. err = -EIO;
  3819. memcpy(&hw->mac.ops, ei->mac_ops, sizeof(hw->mac.ops));
  3820. memcpy(&hw->nvm.ops, ei->nvm_ops, sizeof(hw->nvm.ops));
  3821. memcpy(&hw->phy.ops, ei->phy_ops, sizeof(hw->phy.ops));
  3822. err = ei->get_variants(adapter);
  3823. if (err)
  3824. goto err_hw_init;
  3825. if ((adapter->flags & FLAG_IS_ICH) &&
  3826. (adapter->flags & FLAG_READ_ONLY_NVM))
  3827. e1000e_write_protect_nvm_ich8lan(&adapter->hw);
  3828. hw->mac.ops.get_bus_info(&adapter->hw);
  3829. adapter->hw.phy.autoneg_wait_to_complete = 0;
  3830. /* Copper options */
  3831. if (adapter->hw.phy.media_type == e1000_media_type_copper) {
  3832. adapter->hw.phy.mdix = AUTO_ALL_MODES;
  3833. adapter->hw.phy.disable_polarity_correction = 0;
  3834. adapter->hw.phy.ms_type = e1000_ms_hw_default;
  3835. }
  3836. if (e1000_check_reset_block(&adapter->hw))
  3837. e_info("PHY reset is blocked due to SOL/IDER session.\n");
  3838. netdev->features = NETIF_F_SG |
  3839. NETIF_F_HW_CSUM |
  3840. NETIF_F_HW_VLAN_TX |
  3841. NETIF_F_HW_VLAN_RX;
  3842. if (adapter->flags & FLAG_HAS_HW_VLAN_FILTER)
  3843. netdev->features |= NETIF_F_HW_VLAN_FILTER;
  3844. netdev->features |= NETIF_F_TSO;
  3845. netdev->features |= NETIF_F_TSO6;
  3846. netdev->vlan_features |= NETIF_F_TSO;
  3847. netdev->vlan_features |= NETIF_F_TSO6;
  3848. netdev->vlan_features |= NETIF_F_HW_CSUM;
  3849. netdev->vlan_features |= NETIF_F_SG;
  3850. if (pci_using_dac)
  3851. netdev->features |= NETIF_F_HIGHDMA;
  3852. /*
  3853. * We should not be using LLTX anymore, but we are still Tx faster with
  3854. * it.
  3855. */
  3856. netdev->features |= NETIF_F_LLTX;
  3857. if (e1000e_enable_mng_pass_thru(&adapter->hw))
  3858. adapter->flags |= FLAG_MNG_PT_ENABLED;
  3859. /*
  3860. * before reading the NVM, reset the controller to
  3861. * put the device in a known good starting state
  3862. */
  3863. adapter->hw.mac.ops.reset_hw(&adapter->hw);
  3864. /*
  3865. * systems with ASPM and others may see the checksum fail on the first
  3866. * attempt. Let's give it a few tries
  3867. */
  3868. for (i = 0;; i++) {
  3869. if (e1000_validate_nvm_checksum(&adapter->hw) >= 0)
  3870. break;
  3871. if (i == 2) {
  3872. e_err("The NVM Checksum Is Not Valid\n");
  3873. err = -EIO;
  3874. goto err_eeprom;
  3875. }
  3876. }
  3877. e1000_eeprom_checks(adapter);
  3878. /* copy the MAC address out of the NVM */
  3879. if (e1000e_read_mac_addr(&adapter->hw))
  3880. e_err("NVM Read Error while reading MAC address\n");
  3881. memcpy(netdev->dev_addr, adapter->hw.mac.addr, netdev->addr_len);
  3882. memcpy(netdev->perm_addr, adapter->hw.mac.addr, netdev->addr_len);
  3883. if (!is_valid_ether_addr(netdev->perm_addr)) {
  3884. e_err("Invalid MAC Address: %02x:%02x:%02x:%02x:%02x:%02x\n",
  3885. netdev->perm_addr[0], netdev->perm_addr[1],
  3886. netdev->perm_addr[2], netdev->perm_addr[3],
  3887. netdev->perm_addr[4], netdev->perm_addr[5]);
  3888. err = -EIO;
  3889. goto err_eeprom;
  3890. }
  3891. init_timer(&adapter->watchdog_timer);
  3892. adapter->watchdog_timer.function = &e1000_watchdog;
  3893. adapter->watchdog_timer.data = (unsigned long) adapter;
  3894. init_timer(&adapter->phy_info_timer);
  3895. adapter->phy_info_timer.function = &e1000_update_phy_info;
  3896. adapter->phy_info_timer.data = (unsigned long) adapter;
  3897. INIT_WORK(&adapter->reset_task, e1000_reset_task);
  3898. INIT_WORK(&adapter->watchdog_task, e1000_watchdog_task);
  3899. INIT_WORK(&adapter->downshift_task, e1000e_downshift_workaround);
  3900. INIT_WORK(&adapter->update_phy_task, e1000e_update_phy_task);
  3901. /* Initialize link parameters. User can change them with ethtool */
  3902. adapter->hw.mac.autoneg = 1;
  3903. adapter->fc_autoneg = 1;
  3904. adapter->hw.fc.original_type = e1000_fc_default;
  3905. adapter->hw.fc.type = e1000_fc_default;
  3906. adapter->hw.phy.autoneg_advertised = 0x2f;
  3907. /* ring size defaults */
  3908. adapter->rx_ring->count = 256;
  3909. adapter->tx_ring->count = 256;
  3910. /*
  3911. * Initial Wake on LAN setting - If APM wake is enabled in
  3912. * the EEPROM, enable the ACPI Magic Packet filter
  3913. */
  3914. if (adapter->flags & FLAG_APME_IN_WUC) {
  3915. /* APME bit in EEPROM is mapped to WUC.APME */
  3916. eeprom_data = er32(WUC);
  3917. eeprom_apme_mask = E1000_WUC_APME;
  3918. } else if (adapter->flags & FLAG_APME_IN_CTRL3) {
  3919. if (adapter->flags & FLAG_APME_CHECK_PORT_B &&
  3920. (adapter->hw.bus.func == 1))
  3921. e1000_read_nvm(&adapter->hw,
  3922. NVM_INIT_CONTROL3_PORT_B, 1, &eeprom_data);
  3923. else
  3924. e1000_read_nvm(&adapter->hw,
  3925. NVM_INIT_CONTROL3_PORT_A, 1, &eeprom_data);
  3926. }
  3927. /* fetch WoL from EEPROM */
  3928. if (eeprom_data & eeprom_apme_mask)
  3929. adapter->eeprom_wol |= E1000_WUFC_MAG;
  3930. /*
  3931. * now that we have the eeprom settings, apply the special cases
  3932. * where the eeprom may be wrong or the board simply won't support
  3933. * wake on lan on a particular port
  3934. */
  3935. if (!(adapter->flags & FLAG_HAS_WOL))
  3936. adapter->eeprom_wol = 0;
  3937. /* initialize the wol settings based on the eeprom settings */
  3938. adapter->wol = adapter->eeprom_wol;
  3939. /* reset the hardware with the new settings */
  3940. e1000e_reset(adapter);
  3941. /*
  3942. * If the controller has AMT, do not set DRV_LOAD until the interface
  3943. * is up. For all other cases, let the f/w know that the h/w is now
  3944. * under the control of the driver.
  3945. */
  3946. if (!(adapter->flags & FLAG_HAS_AMT))
  3947. e1000_get_hw_control(adapter);
  3948. /* tell the stack to leave us alone until e1000_open() is called */
  3949. netif_carrier_off(netdev);
  3950. netif_tx_stop_all_queues(netdev);
  3951. strcpy(netdev->name, "eth%d");
  3952. err = register_netdev(netdev);
  3953. if (err)
  3954. goto err_register;
  3955. e1000_print_device_info(adapter);
  3956. return 0;
  3957. err_register:
  3958. if (!(adapter->flags & FLAG_HAS_AMT))
  3959. e1000_release_hw_control(adapter);
  3960. err_eeprom:
  3961. if (!e1000_check_reset_block(&adapter->hw))
  3962. e1000_phy_hw_reset(&adapter->hw);
  3963. err_hw_init:
  3964. kfree(adapter->tx_ring);
  3965. kfree(adapter->rx_ring);
  3966. err_sw_init:
  3967. if (adapter->hw.flash_address)
  3968. iounmap(adapter->hw.flash_address);
  3969. err_flashmap:
  3970. iounmap(adapter->hw.hw_addr);
  3971. err_ioremap:
  3972. free_netdev(netdev);
  3973. err_alloc_etherdev:
  3974. pci_release_selected_regions(pdev,
  3975. pci_select_bars(pdev, IORESOURCE_MEM));
  3976. err_pci_reg:
  3977. err_dma:
  3978. pci_disable_device(pdev);
  3979. return err;
  3980. }
  3981. /**
  3982. * e1000_remove - Device Removal Routine
  3983. * @pdev: PCI device information struct
  3984. *
  3985. * e1000_remove is called by the PCI subsystem to alert the driver
  3986. * that it should release a PCI device. The could be caused by a
  3987. * Hot-Plug event, or because the driver is going to be removed from
  3988. * memory.
  3989. **/
  3990. static void __devexit e1000_remove(struct pci_dev *pdev)
  3991. {
  3992. struct net_device *netdev = pci_get_drvdata(pdev);
  3993. struct e1000_adapter *adapter = netdev_priv(netdev);
  3994. /*
  3995. * flush_scheduled work may reschedule our watchdog task, so
  3996. * explicitly disable watchdog tasks from being rescheduled
  3997. */
  3998. set_bit(__E1000_DOWN, &adapter->state);
  3999. del_timer_sync(&adapter->watchdog_timer);
  4000. del_timer_sync(&adapter->phy_info_timer);
  4001. flush_scheduled_work();
  4002. /*
  4003. * Release control of h/w to f/w. If f/w is AMT enabled, this
  4004. * would have already happened in close and is redundant.
  4005. */
  4006. e1000_release_hw_control(adapter);
  4007. unregister_netdev(netdev);
  4008. if (!e1000_check_reset_block(&adapter->hw))
  4009. e1000_phy_hw_reset(&adapter->hw);
  4010. kfree(adapter->tx_ring);
  4011. kfree(adapter->rx_ring);
  4012. iounmap(adapter->hw.hw_addr);
  4013. if (adapter->hw.flash_address)
  4014. iounmap(adapter->hw.flash_address);
  4015. pci_release_selected_regions(pdev,
  4016. pci_select_bars(pdev, IORESOURCE_MEM));
  4017. free_netdev(netdev);
  4018. pci_disable_device(pdev);
  4019. }
  4020. /* PCI Error Recovery (ERS) */
  4021. static struct pci_error_handlers e1000_err_handler = {
  4022. .error_detected = e1000_io_error_detected,
  4023. .slot_reset = e1000_io_slot_reset,
  4024. .resume = e1000_io_resume,
  4025. };
  4026. static struct pci_device_id e1000_pci_tbl[] = {
  4027. { PCI_VDEVICE(INTEL, E1000_DEV_ID_82571EB_COPPER), board_82571 },
  4028. { PCI_VDEVICE(INTEL, E1000_DEV_ID_82571EB_FIBER), board_82571 },
  4029. { PCI_VDEVICE(INTEL, E1000_DEV_ID_82571EB_QUAD_COPPER), board_82571 },
  4030. { PCI_VDEVICE(INTEL, E1000_DEV_ID_82571EB_QUAD_COPPER_LP), board_82571 },
  4031. { PCI_VDEVICE(INTEL, E1000_DEV_ID_82571EB_QUAD_FIBER), board_82571 },
  4032. { PCI_VDEVICE(INTEL, E1000_DEV_ID_82571EB_SERDES), board_82571 },
  4033. { PCI_VDEVICE(INTEL, E1000_DEV_ID_82571EB_SERDES_DUAL), board_82571 },
  4034. { PCI_VDEVICE(INTEL, E1000_DEV_ID_82571EB_SERDES_QUAD), board_82571 },
  4035. { PCI_VDEVICE(INTEL, E1000_DEV_ID_82571PT_QUAD_COPPER), board_82571 },
  4036. { PCI_VDEVICE(INTEL, E1000_DEV_ID_82572EI), board_82572 },
  4037. { PCI_VDEVICE(INTEL, E1000_DEV_ID_82572EI_COPPER), board_82572 },
  4038. { PCI_VDEVICE(INTEL, E1000_DEV_ID_82572EI_FIBER), board_82572 },
  4039. { PCI_VDEVICE(INTEL, E1000_DEV_ID_82572EI_SERDES), board_82572 },
  4040. { PCI_VDEVICE(INTEL, E1000_DEV_ID_82573E), board_82573 },
  4041. { PCI_VDEVICE(INTEL, E1000_DEV_ID_82573E_IAMT), board_82573 },
  4042. { PCI_VDEVICE(INTEL, E1000_DEV_ID_82573L), board_82573 },
  4043. { PCI_VDEVICE(INTEL, E1000_DEV_ID_80003ES2LAN_COPPER_DPT),
  4044. board_80003es2lan },
  4045. { PCI_VDEVICE(INTEL, E1000_DEV_ID_80003ES2LAN_COPPER_SPT),
  4046. board_80003es2lan },
  4047. { PCI_VDEVICE(INTEL, E1000_DEV_ID_80003ES2LAN_SERDES_DPT),
  4048. board_80003es2lan },
  4049. { PCI_VDEVICE(INTEL, E1000_DEV_ID_80003ES2LAN_SERDES_SPT),
  4050. board_80003es2lan },
  4051. { PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH8_IFE), board_ich8lan },
  4052. { PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH8_IFE_G), board_ich8lan },
  4053. { PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH8_IFE_GT), board_ich8lan },
  4054. { PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH8_IGP_AMT), board_ich8lan },
  4055. { PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH8_IGP_C), board_ich8lan },
  4056. { PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH8_IGP_M), board_ich8lan },
  4057. { PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH8_IGP_M_AMT), board_ich8lan },
  4058. { PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH9_IFE), board_ich9lan },
  4059. { PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH9_IFE_G), board_ich9lan },
  4060. { PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH9_IFE_GT), board_ich9lan },
  4061. { PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH9_IGP_AMT), board_ich9lan },
  4062. { PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH9_IGP_C), board_ich9lan },
  4063. { PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH9_IGP_M), board_ich9lan },
  4064. { PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH9_IGP_M_AMT), board_ich9lan },
  4065. { PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH9_IGP_M_V), board_ich9lan },
  4066. { PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH10_R_BM_LM), board_ich9lan },
  4067. { PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH10_R_BM_LF), board_ich9lan },
  4068. { PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH10_R_BM_V), board_ich9lan },
  4069. { } /* terminate list */
  4070. };
  4071. MODULE_DEVICE_TABLE(pci, e1000_pci_tbl);
  4072. /* PCI Device API Driver */
  4073. static struct pci_driver e1000_driver = {
  4074. .name = e1000e_driver_name,
  4075. .id_table = e1000_pci_tbl,
  4076. .probe = e1000_probe,
  4077. .remove = __devexit_p(e1000_remove),
  4078. #ifdef CONFIG_PM
  4079. /* Power Management Hooks */
  4080. .suspend = e1000_suspend,
  4081. .resume = e1000_resume,
  4082. #endif
  4083. .shutdown = e1000_shutdown,
  4084. .err_handler = &e1000_err_handler
  4085. };
  4086. /**
  4087. * e1000_init_module - Driver Registration Routine
  4088. *
  4089. * e1000_init_module is the first routine called when the driver is
  4090. * loaded. All it does is register with the PCI subsystem.
  4091. **/
  4092. static int __init e1000_init_module(void)
  4093. {
  4094. int ret;
  4095. printk(KERN_INFO "%s: Intel(R) PRO/1000 Network Driver - %s\n",
  4096. e1000e_driver_name, e1000e_driver_version);
  4097. printk(KERN_INFO "%s: Copyright (c) 1999-2008 Intel Corporation.\n",
  4098. e1000e_driver_name);
  4099. ret = pci_register_driver(&e1000_driver);
  4100. pm_qos_add_requirement(PM_QOS_CPU_DMA_LATENCY, e1000e_driver_name,
  4101. PM_QOS_DEFAULT_VALUE);
  4102. return ret;
  4103. }
  4104. module_init(e1000_init_module);
  4105. /**
  4106. * e1000_exit_module - Driver Exit Cleanup Routine
  4107. *
  4108. * e1000_exit_module is called just before the driver is removed
  4109. * from memory.
  4110. **/
  4111. static void __exit e1000_exit_module(void)
  4112. {
  4113. pci_unregister_driver(&e1000_driver);
  4114. pm_qos_remove_requirement(PM_QOS_CPU_DMA_LATENCY, e1000e_driver_name);
  4115. }
  4116. module_exit(e1000_exit_module);
  4117. MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
  4118. MODULE_DESCRIPTION("Intel(R) PRO/1000 Network Driver");
  4119. MODULE_LICENSE("GPL");
  4120. MODULE_VERSION(DRV_VERSION);
  4121. /* e1000_main.c */