et131x.c 118 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121
  1. /* Agere Systems Inc.
  2. * 10/100/1000 Base-T Ethernet Driver for the ET1301 and ET131x series MACs
  3. *
  4. * Copyright © 2005 Agere Systems Inc.
  5. * All rights reserved.
  6. * http://www.agere.com
  7. *
  8. * Copyright (c) 2011 Mark Einon <mark.einon@gmail.com>
  9. *
  10. *------------------------------------------------------------------------------
  11. *
  12. * SOFTWARE LICENSE
  13. *
  14. * This software is provided subject to the following terms and conditions,
  15. * which you should read carefully before using the software. Using this
  16. * software indicates your acceptance of these terms and conditions. If you do
  17. * not agree with these terms and conditions, do not use the software.
  18. *
  19. * Copyright © 2005 Agere Systems Inc.
  20. * All rights reserved.
  21. *
  22. * Redistribution and use in source or binary forms, with or without
  23. * modifications, are permitted provided that the following conditions are met:
  24. *
  25. * . Redistributions of source code must retain the above copyright notice, this
  26. * list of conditions and the following Disclaimer as comments in the code as
  27. * well as in the documentation and/or other materials provided with the
  28. * distribution.
  29. *
  30. * . Redistributions in binary form must reproduce the above copyright notice,
  31. * this list of conditions and the following Disclaimer in the documentation
  32. * and/or other materials provided with the distribution.
  33. *
  34. * . Neither the name of Agere Systems Inc. nor the names of the contributors
  35. * may be used to endorse or promote products derived from this software
  36. * without specific prior written permission.
  37. *
  38. * Disclaimer
  39. *
  40. * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  41. * INCLUDING, BUT NOT LIMITED TO, INFRINGEMENT AND THE IMPLIED WARRANTIES OF
  42. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. ANY
  43. * USE, MODIFICATION OR DISTRIBUTION OF THIS SOFTWARE IS SOLELY AT THE USERS OWN
  44. * RISK. IN NO EVENT SHALL AGERE SYSTEMS INC. OR CONTRIBUTORS BE LIABLE FOR ANY
  45. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  46. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  47. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  48. * ON ANY THEORY OF LIABILITY, INCLUDING, BUT NOT LIMITED TO, CONTRACT, STRICT
  49. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  50. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  51. * DAMAGE.
  52. */
  53. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  54. #include <linux/pci.h>
  55. #include <linux/module.h>
  56. #include <linux/types.h>
  57. #include <linux/kernel.h>
  58. #include <linux/sched.h>
  59. #include <linux/ptrace.h>
  60. #include <linux/slab.h>
  61. #include <linux/ctype.h>
  62. #include <linux/string.h>
  63. #include <linux/timer.h>
  64. #include <linux/interrupt.h>
  65. #include <linux/in.h>
  66. #include <linux/delay.h>
  67. #include <linux/bitops.h>
  68. #include <linux/io.h>
  69. #include <linux/netdevice.h>
  70. #include <linux/etherdevice.h>
  71. #include <linux/skbuff.h>
  72. #include <linux/if_arp.h>
  73. #include <linux/ioport.h>
  74. #include <linux/crc32.h>
  75. #include <linux/random.h>
  76. #include <linux/phy.h>
  77. #include "et131x.h"
  78. MODULE_AUTHOR("Victor Soriano <vjsoriano@agere.com>");
  79. MODULE_AUTHOR("Mark Einon <mark.einon@gmail.com>");
  80. MODULE_LICENSE("Dual BSD/GPL");
  81. MODULE_DESCRIPTION("10/100/1000 Base-T Ethernet Driver for the ET1310 by Agere Systems");
  82. /* EEPROM defines */
  83. #define MAX_NUM_REGISTER_POLLS 1000
  84. #define MAX_NUM_WRITE_RETRIES 2
  85. /* MAC defines */
  86. #define COUNTER_WRAP_16_BIT 0x10000
  87. #define COUNTER_WRAP_12_BIT 0x1000
  88. /* PCI defines */
  89. #define INTERNAL_MEM_SIZE 0x400 /* 1024 of internal memory */
  90. #define INTERNAL_MEM_RX_OFFSET 0x1FF /* 50% Tx, 50% Rx */
  91. /* ISR defines */
  92. /* For interrupts, normal running is:
  93. * rxdma_xfr_done, phy_interrupt, mac_stat_interrupt,
  94. * watchdog_interrupt & txdma_xfer_done
  95. *
  96. * In both cases, when flow control is enabled for either Tx or bi-direction,
  97. * we additional enable rx_fbr0_low and rx_fbr1_low, so we know when the
  98. * buffer rings are running low.
  99. */
  100. #define INT_MASK_DISABLE 0xffffffff
  101. /* NOTE: Masking out MAC_STAT Interrupt for now...
  102. * #define INT_MASK_ENABLE 0xfff6bf17
  103. * #define INT_MASK_ENABLE_NO_FLOW 0xfff6bfd7
  104. */
  105. #define INT_MASK_ENABLE 0xfffebf17
  106. #define INT_MASK_ENABLE_NO_FLOW 0xfffebfd7
  107. /* General defines */
  108. /* Packet and header sizes */
  109. #define NIC_MIN_PACKET_SIZE 60
  110. /* Multicast list size */
  111. #define NIC_MAX_MCAST_LIST 128
  112. /* Supported Filters */
  113. #define ET131X_PACKET_TYPE_DIRECTED 0x0001
  114. #define ET131X_PACKET_TYPE_MULTICAST 0x0002
  115. #define ET131X_PACKET_TYPE_BROADCAST 0x0004
  116. #define ET131X_PACKET_TYPE_PROMISCUOUS 0x0008
  117. #define ET131X_PACKET_TYPE_ALL_MULTICAST 0x0010
  118. /* Tx Timeout */
  119. #define ET131X_TX_TIMEOUT (1 * HZ)
  120. #define NIC_SEND_HANG_THRESHOLD 0
  121. /* MP_ADAPTER flags */
  122. #define FMP_ADAPTER_INTERRUPT_IN_USE 0x00000008
  123. /* MP_SHARED flags */
  124. #define FMP_ADAPTER_LOWER_POWER 0x00200000
  125. #define FMP_ADAPTER_NON_RECOVER_ERROR 0x00800000
  126. #define FMP_ADAPTER_HARDWARE_ERROR 0x04000000
  127. #define FMP_ADAPTER_FAIL_SEND_MASK 0x3ff00000
  128. /* Some offsets in PCI config space that are actually used. */
  129. #define ET1310_PCI_MAC_ADDRESS 0xA4
  130. #define ET1310_PCI_EEPROM_STATUS 0xB2
  131. #define ET1310_PCI_ACK_NACK 0xC0
  132. #define ET1310_PCI_REPLAY 0xC2
  133. #define ET1310_PCI_L0L1LATENCY 0xCF
  134. /* PCI Product IDs */
  135. #define ET131X_PCI_DEVICE_ID_GIG 0xED00 /* ET1310 1000 Base-T 8 */
  136. #define ET131X_PCI_DEVICE_ID_FAST 0xED01 /* ET1310 100 Base-T */
  137. /* Define order of magnitude converter */
  138. #define NANO_IN_A_MICRO 1000
  139. #define PARM_RX_NUM_BUFS_DEF 4
  140. #define PARM_RX_TIME_INT_DEF 10
  141. #define PARM_RX_MEM_END_DEF 0x2bc
  142. #define PARM_TX_TIME_INT_DEF 40
  143. #define PARM_TX_NUM_BUFS_DEF 4
  144. #define PARM_DMA_CACHE_DEF 0
  145. /* RX defines */
  146. #define FBR_CHUNKS 32
  147. #define MAX_DESC_PER_RING_RX 1024
  148. /* number of RFDs - default and min */
  149. #define RFD_LOW_WATER_MARK 40
  150. #define NIC_DEFAULT_NUM_RFD 1024
  151. #define NUM_FBRS 2
  152. #define MAX_PACKETS_HANDLED 256
  153. #define ALCATEL_MULTICAST_PKT 0x01000000
  154. #define ALCATEL_BROADCAST_PKT 0x02000000
  155. /* typedefs for Free Buffer Descriptors */
  156. struct fbr_desc {
  157. u32 addr_lo;
  158. u32 addr_hi;
  159. u32 word2; /* Bits 10-31 reserved, 0-9 descriptor */
  160. };
  161. /* Packet Status Ring Descriptors
  162. *
  163. * Word 0:
  164. *
  165. * top 16 bits are from the Alcatel Status Word as enumerated in
  166. * PE-MCXMAC Data Sheet IPD DS54 0210-1 (also IPD-DS80 0205-2)
  167. *
  168. * 0: hp hash pass
  169. * 1: ipa IP checksum assist
  170. * 2: ipp IP checksum pass
  171. * 3: tcpa TCP checksum assist
  172. * 4: tcpp TCP checksum pass
  173. * 5: wol WOL Event
  174. * 6: rxmac_error RXMAC Error Indicator
  175. * 7: drop Drop packet
  176. * 8: ft Frame Truncated
  177. * 9: jp Jumbo Packet
  178. * 10: vp VLAN Packet
  179. * 11-15: unused
  180. * 16: asw_prev_pkt_dropped e.g. IFG too small on previous
  181. * 17: asw_RX_DV_event short receive event detected
  182. * 18: asw_false_carrier_event bad carrier since last good packet
  183. * 19: asw_code_err one or more nibbles signalled as errors
  184. * 20: asw_CRC_err CRC error
  185. * 21: asw_len_chk_err frame length field incorrect
  186. * 22: asw_too_long frame length > 1518 bytes
  187. * 23: asw_OK valid CRC + no code error
  188. * 24: asw_multicast has a multicast address
  189. * 25: asw_broadcast has a broadcast address
  190. * 26: asw_dribble_nibble spurious bits after EOP
  191. * 27: asw_control_frame is a control frame
  192. * 28: asw_pause_frame is a pause frame
  193. * 29: asw_unsupported_op unsupported OP code
  194. * 30: asw_VLAN_tag VLAN tag detected
  195. * 31: asw_long_evt Rx long event
  196. *
  197. * Word 1:
  198. * 0-15: length length in bytes
  199. * 16-25: bi Buffer Index
  200. * 26-27: ri Ring Index
  201. * 28-31: reserved
  202. */
  203. struct pkt_stat_desc {
  204. u32 word0;
  205. u32 word1;
  206. };
  207. /* Typedefs for the RX DMA status word */
  208. /* rx status word 0 holds part of the status bits of the Rx DMA engine
  209. * that get copied out to memory by the ET-1310. Word 0 is a 32 bit word
  210. * which contains the Free Buffer ring 0 and 1 available offset.
  211. *
  212. * bit 0-9 FBR1 offset
  213. * bit 10 Wrap flag for FBR1
  214. * bit 16-25 FBR0 offset
  215. * bit 26 Wrap flag for FBR0
  216. */
  217. /* RXSTAT_WORD1_t structure holds part of the status bits of the Rx DMA engine
  218. * that get copied out to memory by the ET-1310. Word 3 is a 32 bit word
  219. * which contains the Packet Status Ring available offset.
  220. *
  221. * bit 0-15 reserved
  222. * bit 16-27 PSRoffset
  223. * bit 28 PSRwrap
  224. * bit 29-31 unused
  225. */
  226. /* struct rx_status_block is a structure representing the status of the Rx
  227. * DMA engine it sits in free memory, and is pointed to by 0x101c / 0x1020
  228. */
  229. struct rx_status_block {
  230. u32 word0;
  231. u32 word1;
  232. };
  233. /* Structure for look-up table holding free buffer ring pointers, addresses
  234. * and state.
  235. */
  236. struct fbr_lookup {
  237. void *virt[MAX_DESC_PER_RING_RX];
  238. u32 bus_high[MAX_DESC_PER_RING_RX];
  239. u32 bus_low[MAX_DESC_PER_RING_RX];
  240. void *ring_virtaddr;
  241. dma_addr_t ring_physaddr;
  242. void *mem_virtaddrs[MAX_DESC_PER_RING_RX / FBR_CHUNKS];
  243. dma_addr_t mem_physaddrs[MAX_DESC_PER_RING_RX / FBR_CHUNKS];
  244. u32 local_full;
  245. u32 num_entries;
  246. dma_addr_t buffsize;
  247. };
  248. /* struct rx_ring is the structure representing the adaptor's local
  249. * reference(s) to the rings
  250. */
  251. struct rx_ring {
  252. struct fbr_lookup *fbr[NUM_FBRS];
  253. void *ps_ring_virtaddr;
  254. dma_addr_t ps_ring_physaddr;
  255. u32 local_psr_full;
  256. u32 psr_entries;
  257. struct rx_status_block *rx_status_block;
  258. dma_addr_t rx_status_bus;
  259. struct list_head recv_list;
  260. u32 num_ready_recv;
  261. u32 num_rfd;
  262. bool unfinished_receives;
  263. };
  264. /* TX defines */
  265. /* word 2 of the control bits in the Tx Descriptor ring for the ET-1310
  266. *
  267. * 0-15: length of packet
  268. * 16-27: VLAN tag
  269. * 28: VLAN CFI
  270. * 29-31: VLAN priority
  271. *
  272. * word 3 of the control bits in the Tx Descriptor ring for the ET-1310
  273. *
  274. * 0: last packet in the sequence
  275. * 1: first packet in the sequence
  276. * 2: interrupt the processor when this pkt sent
  277. * 3: Control word - no packet data
  278. * 4: Issue half-duplex backpressure : XON/XOFF
  279. * 5: send pause frame
  280. * 6: Tx frame has error
  281. * 7: append CRC
  282. * 8: MAC override
  283. * 9: pad packet
  284. * 10: Packet is a Huge packet
  285. * 11: append VLAN tag
  286. * 12: IP checksum assist
  287. * 13: TCP checksum assist
  288. * 14: UDP checksum assist
  289. */
  290. #define TXDESC_FLAG_LASTPKT 0x0001
  291. #define TXDESC_FLAG_FIRSTPKT 0x0002
  292. #define TXDESC_FLAG_INTPROC 0x0004
  293. /* struct tx_desc represents each descriptor on the ring */
  294. struct tx_desc {
  295. u32 addr_hi;
  296. u32 addr_lo;
  297. u32 len_vlan; /* control words how to xmit the */
  298. u32 flags; /* data (detailed above) */
  299. };
  300. /* The status of the Tx DMA engine it sits in free memory, and is pointed to
  301. * by 0x101c / 0x1020. This is a DMA10 type
  302. */
  303. /* TCB (Transmit Control Block: Host Side) */
  304. struct tcb {
  305. struct tcb *next; /* Next entry in ring */
  306. u32 count; /* Used to spot stuck/lost packets */
  307. u32 stale; /* Used to spot stuck/lost packets */
  308. struct sk_buff *skb; /* Network skb we are tied to */
  309. u32 index; /* Ring indexes */
  310. u32 index_start;
  311. };
  312. /* Structure representing our local reference(s) to the ring */
  313. struct tx_ring {
  314. /* TCB (Transmit Control Block) memory and lists */
  315. struct tcb *tcb_ring;
  316. /* List of TCBs that are ready to be used */
  317. struct tcb *tcb_qhead;
  318. struct tcb *tcb_qtail;
  319. /* list of TCBs that are currently being sent. */
  320. struct tcb *send_head;
  321. struct tcb *send_tail;
  322. int used;
  323. /* The actual descriptor ring */
  324. struct tx_desc *tx_desc_ring;
  325. dma_addr_t tx_desc_ring_pa;
  326. /* send_idx indicates where we last wrote to in the descriptor ring. */
  327. u32 send_idx;
  328. /* The location of the write-back status block */
  329. u32 *tx_status;
  330. dma_addr_t tx_status_pa;
  331. /* Packets since the last IRQ: used for interrupt coalescing */
  332. int since_irq;
  333. };
  334. /* Do not change these values: if changed, then change also in respective
  335. * TXdma and Rxdma engines
  336. */
  337. #define NUM_DESC_PER_RING_TX 512 /* TX Do not change these values */
  338. #define NUM_TCB 64
  339. /* These values are all superseded by registry entries to facilitate tuning.
  340. * Once the desired performance has been achieved, the optimal registry values
  341. * should be re-populated to these #defines:
  342. */
  343. #define TX_ERROR_PERIOD 1000
  344. #define LO_MARK_PERCENT_FOR_PSR 15
  345. #define LO_MARK_PERCENT_FOR_RX 15
  346. /* RFD (Receive Frame Descriptor) */
  347. struct rfd {
  348. struct list_head list_node;
  349. struct sk_buff *skb;
  350. u32 len; /* total size of receive frame */
  351. u16 bufferindex;
  352. u8 ringindex;
  353. };
  354. /* Flow Control */
  355. #define FLOW_BOTH 0
  356. #define FLOW_TXONLY 1
  357. #define FLOW_RXONLY 2
  358. #define FLOW_NONE 3
  359. /* Struct to define some device statistics */
  360. struct ce_stats {
  361. u32 multicast_pkts_rcvd;
  362. u32 rcvd_pkts_dropped;
  363. u32 tx_underflows;
  364. u32 tx_collisions;
  365. u32 tx_excessive_collisions;
  366. u32 tx_first_collisions;
  367. u32 tx_late_collisions;
  368. u32 tx_max_pkt_errs;
  369. u32 tx_deferred;
  370. u32 rx_overflows;
  371. u32 rx_length_errs;
  372. u32 rx_align_errs;
  373. u32 rx_crc_errs;
  374. u32 rx_code_violations;
  375. u32 rx_other_errs;
  376. u32 interrupt_status;
  377. };
  378. /* The private adapter structure */
  379. struct et131x_adapter {
  380. struct net_device *netdev;
  381. struct pci_dev *pdev;
  382. struct mii_bus *mii_bus;
  383. struct phy_device *phydev;
  384. struct napi_struct napi;
  385. /* Flags that indicate current state of the adapter */
  386. u32 flags;
  387. /* local link state, to determine if a state change has occurred */
  388. int link;
  389. /* Configuration */
  390. u8 rom_addr[ETH_ALEN];
  391. u8 addr[ETH_ALEN];
  392. bool has_eeprom;
  393. u8 eeprom_data[2];
  394. spinlock_t tcb_send_qlock; /* protects the tx_ring send tcb list */
  395. spinlock_t tcb_ready_qlock; /* protects the tx_ring ready tcb list */
  396. spinlock_t rcv_lock; /* protects the rx_ring receive list */
  397. /* Packet Filter and look ahead size */
  398. u32 packet_filter;
  399. /* multicast list */
  400. u32 multicast_addr_count;
  401. u8 multicast_list[NIC_MAX_MCAST_LIST][ETH_ALEN];
  402. /* Pointer to the device's PCI register space */
  403. struct address_map __iomem *regs;
  404. /* Registry parameters */
  405. u8 wanted_flow; /* Flow we want for 802.3x flow control */
  406. u32 registry_jumbo_packet; /* Max supported ethernet packet size */
  407. /* Derived from the registry: */
  408. u8 flow; /* flow control validated by the far-end */
  409. /* Minimize init-time */
  410. struct timer_list error_timer;
  411. /* variable putting the phy into coma mode when boot up with no cable
  412. * plugged in after 5 seconds
  413. */
  414. u8 boot_coma;
  415. /* Tx Memory Variables */
  416. struct tx_ring tx_ring;
  417. /* Rx Memory Variables */
  418. struct rx_ring rx_ring;
  419. struct ce_stats stats;
  420. };
  421. static int eeprom_wait_ready(struct pci_dev *pdev, u32 *status)
  422. {
  423. u32 reg;
  424. int i;
  425. /* 1. Check LBCIF Status Register for bits 6 & 3:2 all equal to 0 and
  426. * bits 7,1:0 both equal to 1, at least once after reset.
  427. * Subsequent operations need only to check that bits 1:0 are equal
  428. * to 1 prior to starting a single byte read/write
  429. */
  430. for (i = 0; i < MAX_NUM_REGISTER_POLLS; i++) {
  431. if (pci_read_config_dword(pdev, LBCIF_DWORD1_GROUP, &reg))
  432. return -EIO;
  433. /* I2C idle and Phy Queue Avail both true */
  434. if ((reg & 0x3000) == 0x3000) {
  435. if (status)
  436. *status = reg;
  437. return reg & 0xFF;
  438. }
  439. }
  440. return -ETIMEDOUT;
  441. }
  442. static int eeprom_write(struct et131x_adapter *adapter, u32 addr, u8 data)
  443. {
  444. struct pci_dev *pdev = adapter->pdev;
  445. int index = 0;
  446. int retries;
  447. int err = 0;
  448. int writeok = 0;
  449. u32 status;
  450. u32 val = 0;
  451. /* For an EEPROM, an I2C single byte write is defined as a START
  452. * condition followed by the device address, EEPROM address, one byte
  453. * of data and a STOP condition. The STOP condition will trigger the
  454. * EEPROM's internally timed write cycle to the nonvolatile memory.
  455. * All inputs are disabled during this write cycle and the EEPROM will
  456. * not respond to any access until the internal write is complete.
  457. */
  458. err = eeprom_wait_ready(pdev, NULL);
  459. if (err < 0)
  460. return err;
  461. /* 2. Write to the LBCIF Control Register: bit 7=1, bit 6=1, bit 3=0,
  462. * and bits 1:0 both =0. Bit 5 should be set according to the
  463. * type of EEPROM being accessed (1=two byte addressing, 0=one
  464. * byte addressing).
  465. */
  466. if (pci_write_config_byte(pdev, LBCIF_CONTROL_REGISTER,
  467. LBCIF_CONTROL_LBCIF_ENABLE |
  468. LBCIF_CONTROL_I2C_WRITE))
  469. return -EIO;
  470. /* Prepare EEPROM address for Step 3 */
  471. for (retries = 0; retries < MAX_NUM_WRITE_RETRIES; retries++) {
  472. if (pci_write_config_dword(pdev, LBCIF_ADDRESS_REGISTER, addr))
  473. break;
  474. /* Write the data to the LBCIF Data Register (the I2C write
  475. * will begin).
  476. */
  477. if (pci_write_config_byte(pdev, LBCIF_DATA_REGISTER, data))
  478. break;
  479. /* Monitor bit 1:0 of the LBCIF Status Register. When bits
  480. * 1:0 are both equal to 1, the I2C write has completed and the
  481. * internal write cycle of the EEPROM is about to start.
  482. * (bits 1:0 = 01 is a legal state while waiting from both
  483. * equal to 1, but bits 1:0 = 10 is invalid and implies that
  484. * something is broken).
  485. */
  486. err = eeprom_wait_ready(pdev, &status);
  487. if (err < 0)
  488. return 0;
  489. /* Check bit 3 of the LBCIF Status Register. If equal to 1,
  490. * an error has occurred.Don't break here if we are revision
  491. * 1, this is so we do a blind write for load bug.
  492. */
  493. if ((status & LBCIF_STATUS_GENERAL_ERROR) &&
  494. adapter->pdev->revision == 0)
  495. break;
  496. /* Check bit 2 of the LBCIF Status Register. If equal to 1 an
  497. * ACK error has occurred on the address phase of the write.
  498. * This could be due to an actual hardware failure or the
  499. * EEPROM may still be in its internal write cycle from a
  500. * previous write. This write operation was ignored and must be
  501. *repeated later.
  502. */
  503. if (status & LBCIF_STATUS_ACK_ERROR) {
  504. /* This could be due to an actual hardware failure
  505. * or the EEPROM may still be in its internal write
  506. * cycle from a previous write. This write operation
  507. * was ignored and must be repeated later.
  508. */
  509. udelay(10);
  510. continue;
  511. }
  512. writeok = 1;
  513. break;
  514. }
  515. udelay(10);
  516. while (1) {
  517. if (pci_write_config_byte(pdev, LBCIF_CONTROL_REGISTER,
  518. LBCIF_CONTROL_LBCIF_ENABLE))
  519. writeok = 0;
  520. /* Do read until internal ACK_ERROR goes away meaning write
  521. * completed
  522. */
  523. do {
  524. pci_write_config_dword(pdev,
  525. LBCIF_ADDRESS_REGISTER,
  526. addr);
  527. do {
  528. pci_read_config_dword(pdev,
  529. LBCIF_DATA_REGISTER,
  530. &val);
  531. } while ((val & 0x00010000) == 0);
  532. } while (val & 0x00040000);
  533. if ((val & 0xFF00) != 0xC000 || index == 10000)
  534. break;
  535. index++;
  536. }
  537. return writeok ? 0 : -EIO;
  538. }
  539. static int eeprom_read(struct et131x_adapter *adapter, u32 addr, u8 *pdata)
  540. {
  541. struct pci_dev *pdev = adapter->pdev;
  542. int err;
  543. u32 status;
  544. /* A single byte read is similar to the single byte write, with the
  545. * exception of the data flow:
  546. */
  547. err = eeprom_wait_ready(pdev, NULL);
  548. if (err < 0)
  549. return err;
  550. /* Write to the LBCIF Control Register: bit 7=1, bit 6=0, bit 3=0,
  551. * and bits 1:0 both =0. Bit 5 should be set according to the type
  552. * of EEPROM being accessed (1=two byte addressing, 0=one byte
  553. * addressing).
  554. */
  555. if (pci_write_config_byte(pdev, LBCIF_CONTROL_REGISTER,
  556. LBCIF_CONTROL_LBCIF_ENABLE))
  557. return -EIO;
  558. /* Write the address to the LBCIF Address Register (I2C read will
  559. * begin).
  560. */
  561. if (pci_write_config_dword(pdev, LBCIF_ADDRESS_REGISTER, addr))
  562. return -EIO;
  563. /* Monitor bit 0 of the LBCIF Status Register. When = 1, I2C read
  564. * is complete. (if bit 1 =1 and bit 0 stays = 0, a hardware failure
  565. * has occurred).
  566. */
  567. err = eeprom_wait_ready(pdev, &status);
  568. if (err < 0)
  569. return err;
  570. /* Regardless of error status, read data byte from LBCIF Data
  571. * Register.
  572. */
  573. *pdata = err;
  574. return (status & LBCIF_STATUS_ACK_ERROR) ? -EIO : 0;
  575. }
  576. static int et131x_init_eeprom(struct et131x_adapter *adapter)
  577. {
  578. struct pci_dev *pdev = adapter->pdev;
  579. u8 eestatus;
  580. pci_read_config_byte(pdev, ET1310_PCI_EEPROM_STATUS, &eestatus);
  581. /* THIS IS A WORKAROUND:
  582. * I need to call this function twice to get my card in a
  583. * LG M1 Express Dual running. I tried also a msleep before this
  584. * function, because I thought there could be some time conditions
  585. * but it didn't work. Call the whole function twice also work.
  586. */
  587. if (pci_read_config_byte(pdev, ET1310_PCI_EEPROM_STATUS, &eestatus)) {
  588. dev_err(&pdev->dev,
  589. "Could not read PCI config space for EEPROM Status\n");
  590. return -EIO;
  591. }
  592. /* Determine if the error(s) we care about are present. If they are
  593. * present we need to fail.
  594. */
  595. if (eestatus & 0x4C) {
  596. int write_failed = 0;
  597. if (pdev->revision == 0x01) {
  598. int i;
  599. static const u8 eedata[4] = { 0xFE, 0x13, 0x10, 0xFF };
  600. /* Re-write the first 4 bytes if we have an eeprom
  601. * present and the revision id is 1, this fixes the
  602. * corruption seen with 1310 B Silicon
  603. */
  604. for (i = 0; i < 3; i++)
  605. if (eeprom_write(adapter, i, eedata[i]) < 0)
  606. write_failed = 1;
  607. }
  608. if (pdev->revision != 0x01 || write_failed) {
  609. dev_err(&pdev->dev,
  610. "Fatal EEPROM Status Error - 0x%04x\n",
  611. eestatus);
  612. /* This error could mean that there was an error
  613. * reading the eeprom or that the eeprom doesn't exist.
  614. * We will treat each case the same and not try to
  615. * gather additional information that normally would
  616. * come from the eeprom, like MAC Address
  617. */
  618. adapter->has_eeprom = 0;
  619. return -EIO;
  620. }
  621. }
  622. adapter->has_eeprom = 1;
  623. /* Read the EEPROM for information regarding LED behavior. Refer to
  624. * et131x_xcvr_init() for its use.
  625. */
  626. eeprom_read(adapter, 0x70, &adapter->eeprom_data[0]);
  627. eeprom_read(adapter, 0x71, &adapter->eeprom_data[1]);
  628. if (adapter->eeprom_data[0] != 0xcd)
  629. /* Disable all optional features */
  630. adapter->eeprom_data[1] = 0x00;
  631. return 0;
  632. }
  633. static void et131x_rx_dma_enable(struct et131x_adapter *adapter)
  634. {
  635. /* Setup the receive dma configuration register for normal operation */
  636. u32 csr = ET_RXDMA_CSR_FBR1_ENABLE;
  637. struct rx_ring *rx_ring = &adapter->rx_ring;
  638. if (rx_ring->fbr[1]->buffsize == 4096)
  639. csr |= ET_RXDMA_CSR_FBR1_SIZE_LO;
  640. else if (rx_ring->fbr[1]->buffsize == 8192)
  641. csr |= ET_RXDMA_CSR_FBR1_SIZE_HI;
  642. else if (rx_ring->fbr[1]->buffsize == 16384)
  643. csr |= ET_RXDMA_CSR_FBR1_SIZE_LO | ET_RXDMA_CSR_FBR1_SIZE_HI;
  644. csr |= ET_RXDMA_CSR_FBR0_ENABLE;
  645. if (rx_ring->fbr[0]->buffsize == 256)
  646. csr |= ET_RXDMA_CSR_FBR0_SIZE_LO;
  647. else if (rx_ring->fbr[0]->buffsize == 512)
  648. csr |= ET_RXDMA_CSR_FBR0_SIZE_HI;
  649. else if (rx_ring->fbr[0]->buffsize == 1024)
  650. csr |= ET_RXDMA_CSR_FBR0_SIZE_LO | ET_RXDMA_CSR_FBR0_SIZE_HI;
  651. writel(csr, &adapter->regs->rxdma.csr);
  652. csr = readl(&adapter->regs->rxdma.csr);
  653. if (csr & ET_RXDMA_CSR_HALT_STATUS) {
  654. udelay(5);
  655. csr = readl(&adapter->regs->rxdma.csr);
  656. if (csr & ET_RXDMA_CSR_HALT_STATUS) {
  657. dev_err(&adapter->pdev->dev,
  658. "RX Dma failed to exit halt state. CSR 0x%08x\n",
  659. csr);
  660. }
  661. }
  662. }
  663. static void et131x_rx_dma_disable(struct et131x_adapter *adapter)
  664. {
  665. u32 csr;
  666. /* Setup the receive dma configuration register */
  667. writel(ET_RXDMA_CSR_HALT | ET_RXDMA_CSR_FBR1_ENABLE,
  668. &adapter->regs->rxdma.csr);
  669. csr = readl(&adapter->regs->rxdma.csr);
  670. if (!(csr & ET_RXDMA_CSR_HALT_STATUS)) {
  671. udelay(5);
  672. csr = readl(&adapter->regs->rxdma.csr);
  673. if (!(csr & ET_RXDMA_CSR_HALT_STATUS))
  674. dev_err(&adapter->pdev->dev,
  675. "RX Dma failed to enter halt state. CSR 0x%08x\n",
  676. csr);
  677. }
  678. }
  679. static void et131x_tx_dma_enable(struct et131x_adapter *adapter)
  680. {
  681. /* Setup the transmit dma configuration register for normal
  682. * operation
  683. */
  684. writel(ET_TXDMA_SNGL_EPKT | (PARM_DMA_CACHE_DEF << ET_TXDMA_CACHE_SHIFT),
  685. &adapter->regs->txdma.csr);
  686. }
  687. static inline void add_10bit(u32 *v, int n)
  688. {
  689. *v = INDEX10(*v + n) | (*v & ET_DMA10_WRAP);
  690. }
  691. static inline void add_12bit(u32 *v, int n)
  692. {
  693. *v = INDEX12(*v + n) | (*v & ET_DMA12_WRAP);
  694. }
  695. static void et1310_config_mac_regs1(struct et131x_adapter *adapter)
  696. {
  697. struct mac_regs __iomem *macregs = &adapter->regs->mac;
  698. u32 station1;
  699. u32 station2;
  700. u32 ipg;
  701. /* First we need to reset everything. Write to MAC configuration
  702. * register 1 to perform reset.
  703. */
  704. writel(ET_MAC_CFG1_SOFT_RESET | ET_MAC_CFG1_SIM_RESET |
  705. ET_MAC_CFG1_RESET_RXMC | ET_MAC_CFG1_RESET_TXMC |
  706. ET_MAC_CFG1_RESET_RXFUNC | ET_MAC_CFG1_RESET_TXFUNC,
  707. &macregs->cfg1);
  708. /* Next lets configure the MAC Inter-packet gap register */
  709. ipg = 0x38005860; /* IPG1 0x38 IPG2 0x58 B2B 0x60 */
  710. ipg |= 0x50 << 8; /* ifg enforce 0x50 */
  711. writel(ipg, &macregs->ipg);
  712. /* Next lets configure the MAC Half Duplex register */
  713. /* BEB trunc 0xA, Ex Defer, Rexmit 0xF Coll 0x37 */
  714. writel(0x00A1F037, &macregs->hfdp);
  715. /* Next lets configure the MAC Interface Control register */
  716. writel(0, &macregs->if_ctrl);
  717. writel(ET_MAC_MIIMGMT_CLK_RST, &macregs->mii_mgmt_cfg);
  718. /* Next lets configure the MAC Station Address register. These
  719. * values are read from the EEPROM during initialization and stored
  720. * in the adapter structure. We write what is stored in the adapter
  721. * structure to the MAC Station Address registers high and low. This
  722. * station address is used for generating and checking pause control
  723. * packets.
  724. */
  725. station2 = (adapter->addr[1] << ET_MAC_STATION_ADDR2_OC2_SHIFT) |
  726. (adapter->addr[0] << ET_MAC_STATION_ADDR2_OC1_SHIFT);
  727. station1 = (adapter->addr[5] << ET_MAC_STATION_ADDR1_OC6_SHIFT) |
  728. (adapter->addr[4] << ET_MAC_STATION_ADDR1_OC5_SHIFT) |
  729. (adapter->addr[3] << ET_MAC_STATION_ADDR1_OC4_SHIFT) |
  730. adapter->addr[2];
  731. writel(station1, &macregs->station_addr_1);
  732. writel(station2, &macregs->station_addr_2);
  733. /* Max ethernet packet in bytes that will be passed by the mac without
  734. * being truncated. Allow the MAC to pass 4 more than our max packet
  735. * size. This is 4 for the Ethernet CRC.
  736. *
  737. * Packets larger than (registry_jumbo_packet) that do not contain a
  738. * VLAN ID will be dropped by the Rx function.
  739. */
  740. writel(adapter->registry_jumbo_packet + 4, &macregs->max_fm_len);
  741. /* clear out MAC config reset */
  742. writel(0, &macregs->cfg1);
  743. }
  744. static void et1310_config_mac_regs2(struct et131x_adapter *adapter)
  745. {
  746. int32_t delay = 0;
  747. struct mac_regs __iomem *mac = &adapter->regs->mac;
  748. struct phy_device *phydev = adapter->phydev;
  749. u32 cfg1;
  750. u32 cfg2;
  751. u32 ifctrl;
  752. u32 ctl;
  753. ctl = readl(&adapter->regs->txmac.ctl);
  754. cfg1 = readl(&mac->cfg1);
  755. cfg2 = readl(&mac->cfg2);
  756. ifctrl = readl(&mac->if_ctrl);
  757. /* Set up the if mode bits */
  758. cfg2 &= ~ET_MAC_CFG2_IFMODE_MASK;
  759. if (phydev->speed == SPEED_1000) {
  760. cfg2 |= ET_MAC_CFG2_IFMODE_1000;
  761. ifctrl &= ~ET_MAC_IFCTRL_PHYMODE;
  762. } else {
  763. cfg2 |= ET_MAC_CFG2_IFMODE_100;
  764. ifctrl |= ET_MAC_IFCTRL_PHYMODE;
  765. }
  766. cfg1 |= ET_MAC_CFG1_RX_ENABLE | ET_MAC_CFG1_TX_ENABLE |
  767. ET_MAC_CFG1_TX_FLOW;
  768. cfg1 &= ~(ET_MAC_CFG1_LOOPBACK | ET_MAC_CFG1_RX_FLOW);
  769. if (adapter->flow == FLOW_RXONLY || adapter->flow == FLOW_BOTH)
  770. cfg1 |= ET_MAC_CFG1_RX_FLOW;
  771. writel(cfg1, &mac->cfg1);
  772. /* Now we need to initialize the MAC Configuration 2 register */
  773. /* preamble 7, check length, huge frame off, pad crc, crc enable
  774. * full duplex off
  775. */
  776. cfg2 |= 0x7 << ET_MAC_CFG2_PREAMBLE_SHIFT;
  777. cfg2 |= ET_MAC_CFG2_IFMODE_LEN_CHECK;
  778. cfg2 |= ET_MAC_CFG2_IFMODE_PAD_CRC;
  779. cfg2 |= ET_MAC_CFG2_IFMODE_CRC_ENABLE;
  780. cfg2 &= ~ET_MAC_CFG2_IFMODE_HUGE_FRAME;
  781. cfg2 &= ~ET_MAC_CFG2_IFMODE_FULL_DPLX;
  782. if (phydev->duplex == DUPLEX_FULL)
  783. cfg2 |= ET_MAC_CFG2_IFMODE_FULL_DPLX;
  784. ifctrl &= ~ET_MAC_IFCTRL_GHDMODE;
  785. if (phydev->duplex == DUPLEX_HALF)
  786. ifctrl |= ET_MAC_IFCTRL_GHDMODE;
  787. writel(ifctrl, &mac->if_ctrl);
  788. writel(cfg2, &mac->cfg2);
  789. do {
  790. udelay(10);
  791. delay++;
  792. cfg1 = readl(&mac->cfg1);
  793. } while ((cfg1 & ET_MAC_CFG1_WAIT) != ET_MAC_CFG1_WAIT && delay < 100);
  794. if (delay == 100) {
  795. dev_warn(&adapter->pdev->dev,
  796. "Syncd bits did not respond correctly cfg1 word 0x%08x\n",
  797. cfg1);
  798. }
  799. ctl |= ET_TX_CTRL_TXMAC_ENABLE | ET_TX_CTRL_FC_DISABLE;
  800. writel(ctl, &adapter->regs->txmac.ctl);
  801. if (adapter->flags & FMP_ADAPTER_LOWER_POWER) {
  802. et131x_rx_dma_enable(adapter);
  803. et131x_tx_dma_enable(adapter);
  804. }
  805. }
  806. static int et1310_in_phy_coma(struct et131x_adapter *adapter)
  807. {
  808. u32 pmcsr = readl(&adapter->regs->global.pm_csr);
  809. return ET_PM_PHY_SW_COMA & pmcsr ? 1 : 0;
  810. }
  811. static void et1310_setup_device_for_multicast(struct et131x_adapter *adapter)
  812. {
  813. struct rxmac_regs __iomem *rxmac = &adapter->regs->rxmac;
  814. u32 hash1 = 0;
  815. u32 hash2 = 0;
  816. u32 hash3 = 0;
  817. u32 hash4 = 0;
  818. u32 pm_csr;
  819. /* If ET131X_PACKET_TYPE_MULTICAST is specified, then we provision
  820. * the multi-cast LIST. If it is NOT specified, (and "ALL" is not
  821. * specified) then we should pass NO multi-cast addresses to the
  822. * driver.
  823. */
  824. if (adapter->packet_filter & ET131X_PACKET_TYPE_MULTICAST) {
  825. int i;
  826. /* Loop through our multicast array and set up the device */
  827. for (i = 0; i < adapter->multicast_addr_count; i++) {
  828. u32 result;
  829. result = ether_crc(6, adapter->multicast_list[i]);
  830. result = (result & 0x3F800000) >> 23;
  831. if (result < 32) {
  832. hash1 |= (1 << result);
  833. } else if ((31 < result) && (result < 64)) {
  834. result -= 32;
  835. hash2 |= (1 << result);
  836. } else if ((63 < result) && (result < 96)) {
  837. result -= 64;
  838. hash3 |= (1 << result);
  839. } else {
  840. result -= 96;
  841. hash4 |= (1 << result);
  842. }
  843. }
  844. }
  845. /* Write out the new hash to the device */
  846. pm_csr = readl(&adapter->regs->global.pm_csr);
  847. if (!et1310_in_phy_coma(adapter)) {
  848. writel(hash1, &rxmac->multi_hash1);
  849. writel(hash2, &rxmac->multi_hash2);
  850. writel(hash3, &rxmac->multi_hash3);
  851. writel(hash4, &rxmac->multi_hash4);
  852. }
  853. }
  854. static void et1310_setup_device_for_unicast(struct et131x_adapter *adapter)
  855. {
  856. struct rxmac_regs __iomem *rxmac = &adapter->regs->rxmac;
  857. u32 uni_pf1;
  858. u32 uni_pf2;
  859. u32 uni_pf3;
  860. u32 pm_csr;
  861. /* Set up unicast packet filter reg 3 to be the first two octets of
  862. * the MAC address for both address
  863. *
  864. * Set up unicast packet filter reg 2 to be the octets 2 - 5 of the
  865. * MAC address for second address
  866. *
  867. * Set up unicast packet filter reg 3 to be the octets 2 - 5 of the
  868. * MAC address for first address
  869. */
  870. uni_pf3 = (adapter->addr[0] << ET_RX_UNI_PF_ADDR2_1_SHIFT) |
  871. (adapter->addr[1] << ET_RX_UNI_PF_ADDR2_2_SHIFT) |
  872. (adapter->addr[0] << ET_RX_UNI_PF_ADDR1_1_SHIFT) |
  873. adapter->addr[1];
  874. uni_pf2 = (adapter->addr[2] << ET_RX_UNI_PF_ADDR2_3_SHIFT) |
  875. (adapter->addr[3] << ET_RX_UNI_PF_ADDR2_4_SHIFT) |
  876. (adapter->addr[4] << ET_RX_UNI_PF_ADDR2_5_SHIFT) |
  877. adapter->addr[5];
  878. uni_pf1 = (adapter->addr[2] << ET_RX_UNI_PF_ADDR1_3_SHIFT) |
  879. (adapter->addr[3] << ET_RX_UNI_PF_ADDR1_4_SHIFT) |
  880. (adapter->addr[4] << ET_RX_UNI_PF_ADDR1_5_SHIFT) |
  881. adapter->addr[5];
  882. pm_csr = readl(&adapter->regs->global.pm_csr);
  883. if (!et1310_in_phy_coma(adapter)) {
  884. writel(uni_pf1, &rxmac->uni_pf_addr1);
  885. writel(uni_pf2, &rxmac->uni_pf_addr2);
  886. writel(uni_pf3, &rxmac->uni_pf_addr3);
  887. }
  888. }
  889. static void et1310_config_rxmac_regs(struct et131x_adapter *adapter)
  890. {
  891. struct rxmac_regs __iomem *rxmac = &adapter->regs->rxmac;
  892. struct phy_device *phydev = adapter->phydev;
  893. u32 sa_lo;
  894. u32 sa_hi = 0;
  895. u32 pf_ctrl = 0;
  896. u32 __iomem *wolw;
  897. /* Disable the MAC while it is being configured (also disable WOL) */
  898. writel(0x8, &rxmac->ctrl);
  899. /* Initialize WOL to disabled. */
  900. writel(0, &rxmac->crc0);
  901. writel(0, &rxmac->crc12);
  902. writel(0, &rxmac->crc34);
  903. /* We need to set the WOL mask0 - mask4 next. We initialize it to
  904. * its default Values of 0x00000000 because there are not WOL masks
  905. * as of this time.
  906. */
  907. for (wolw = &rxmac->mask0_word0; wolw <= &rxmac->mask4_word3; wolw++)
  908. writel(0, wolw);
  909. /* Lets setup the WOL Source Address */
  910. sa_lo = (adapter->addr[2] << ET_RX_WOL_LO_SA3_SHIFT) |
  911. (adapter->addr[3] << ET_RX_WOL_LO_SA4_SHIFT) |
  912. (adapter->addr[4] << ET_RX_WOL_LO_SA5_SHIFT) |
  913. adapter->addr[5];
  914. writel(sa_lo, &rxmac->sa_lo);
  915. sa_hi = (u32)(adapter->addr[0] << ET_RX_WOL_HI_SA1_SHIFT) |
  916. adapter->addr[1];
  917. writel(sa_hi, &rxmac->sa_hi);
  918. /* Disable all Packet Filtering */
  919. writel(0, &rxmac->pf_ctrl);
  920. /* Let's initialize the Unicast Packet filtering address */
  921. if (adapter->packet_filter & ET131X_PACKET_TYPE_DIRECTED) {
  922. et1310_setup_device_for_unicast(adapter);
  923. pf_ctrl |= ET_RX_PFCTRL_UNICST_FILTER_ENABLE;
  924. } else {
  925. writel(0, &rxmac->uni_pf_addr1);
  926. writel(0, &rxmac->uni_pf_addr2);
  927. writel(0, &rxmac->uni_pf_addr3);
  928. }
  929. /* Let's initialize the Multicast hash */
  930. if (!(adapter->packet_filter & ET131X_PACKET_TYPE_ALL_MULTICAST)) {
  931. pf_ctrl |= ET_RX_PFCTRL_MLTCST_FILTER_ENABLE;
  932. et1310_setup_device_for_multicast(adapter);
  933. }
  934. /* Runt packet filtering. Didn't work in version A silicon. */
  935. pf_ctrl |= (NIC_MIN_PACKET_SIZE + 4) << ET_RX_PFCTRL_MIN_PKT_SZ_SHIFT;
  936. pf_ctrl |= ET_RX_PFCTRL_FRAG_FILTER_ENABLE;
  937. if (adapter->registry_jumbo_packet > 8192)
  938. /* In order to transmit jumbo packets greater than 8k, the
  939. * FIFO between RxMAC and RxDMA needs to be reduced in size
  940. * to (16k - Jumbo packet size). In order to implement this,
  941. * we must use "cut through" mode in the RxMAC, which chops
  942. * packets down into segments which are (max_size * 16). In
  943. * this case we selected 256 bytes, since this is the size of
  944. * the PCI-Express TLP's that the 1310 uses.
  945. *
  946. * seg_en on, fc_en off, size 0x10
  947. */
  948. writel(0x41, &rxmac->mcif_ctrl_max_seg);
  949. else
  950. writel(0, &rxmac->mcif_ctrl_max_seg);
  951. writel(0, &rxmac->mcif_water_mark);
  952. writel(0, &rxmac->mif_ctrl);
  953. writel(0, &rxmac->space_avail);
  954. /* Initialize the the mif_ctrl register
  955. * bit 3: Receive code error. One or more nibbles were signaled as
  956. * errors during the reception of the packet. Clear this
  957. * bit in Gigabit, set it in 100Mbit. This was derived
  958. * experimentally at UNH.
  959. * bit 4: Receive CRC error. The packet's CRC did not match the
  960. * internally generated CRC.
  961. * bit 5: Receive length check error. Indicates that frame length
  962. * field value in the packet does not match the actual data
  963. * byte length and is not a type field.
  964. * bit 16: Receive frame truncated.
  965. * bit 17: Drop packet enable
  966. */
  967. if (phydev && phydev->speed == SPEED_100)
  968. writel(0x30038, &rxmac->mif_ctrl);
  969. else
  970. writel(0x30030, &rxmac->mif_ctrl);
  971. /* Finally we initialize RxMac to be enabled & WOL disabled. Packet
  972. * filter is always enabled since it is where the runt packets are
  973. * supposed to be dropped. For version A silicon, runt packet
  974. * dropping doesn't work, so it is disabled in the pf_ctrl register,
  975. * but we still leave the packet filter on.
  976. */
  977. writel(pf_ctrl, &rxmac->pf_ctrl);
  978. writel(ET_RX_CTRL_RXMAC_ENABLE | ET_RX_CTRL_WOL_DISABLE, &rxmac->ctrl);
  979. }
  980. static void et1310_config_txmac_regs(struct et131x_adapter *adapter)
  981. {
  982. struct txmac_regs __iomem *txmac = &adapter->regs->txmac;
  983. /* We need to update the Control Frame Parameters
  984. * cfpt - control frame pause timer set to 64 (0x40)
  985. * cfep - control frame extended pause timer set to 0x0
  986. */
  987. if (adapter->flow == FLOW_NONE)
  988. writel(0, &txmac->cf_param);
  989. else
  990. writel(0x40, &txmac->cf_param);
  991. }
  992. static void et1310_config_macstat_regs(struct et131x_adapter *adapter)
  993. {
  994. struct macstat_regs __iomem *macstat = &adapter->regs->macstat;
  995. u32 __iomem *reg;
  996. /* initialize all the macstat registers to zero on the device */
  997. for (reg = &macstat->txrx_0_64_byte_frames;
  998. reg <= &macstat->carry_reg2; reg++)
  999. writel(0, reg);
  1000. /* Unmask any counters that we want to track the overflow of.
  1001. * Initially this will be all counters. It may become clear later
  1002. * that we do not need to track all counters.
  1003. */
  1004. writel(0xFFFFBE32, &macstat->carry_reg1_mask);
  1005. writel(0xFFFE7E8B, &macstat->carry_reg2_mask);
  1006. }
  1007. static int et131x_phy_mii_read(struct et131x_adapter *adapter, u8 addr,
  1008. u8 reg, u16 *value)
  1009. {
  1010. struct mac_regs __iomem *mac = &adapter->regs->mac;
  1011. int status = 0;
  1012. u32 delay = 0;
  1013. u32 mii_addr;
  1014. u32 mii_cmd;
  1015. u32 mii_indicator;
  1016. /* Save a local copy of the registers we are dealing with so we can
  1017. * set them back
  1018. */
  1019. mii_addr = readl(&mac->mii_mgmt_addr);
  1020. mii_cmd = readl(&mac->mii_mgmt_cmd);
  1021. /* Stop the current operation */
  1022. writel(0, &mac->mii_mgmt_cmd);
  1023. /* Set up the register we need to read from on the correct PHY */
  1024. writel(ET_MAC_MII_ADDR(addr, reg), &mac->mii_mgmt_addr);
  1025. writel(0x1, &mac->mii_mgmt_cmd);
  1026. do {
  1027. udelay(50);
  1028. delay++;
  1029. mii_indicator = readl(&mac->mii_mgmt_indicator);
  1030. } while ((mii_indicator & ET_MAC_MGMT_WAIT) && delay < 50);
  1031. /* If we hit the max delay, we could not read the register */
  1032. if (delay == 50) {
  1033. dev_warn(&adapter->pdev->dev,
  1034. "reg 0x%08x could not be read\n", reg);
  1035. dev_warn(&adapter->pdev->dev, "status is 0x%08x\n",
  1036. mii_indicator);
  1037. status = -EIO;
  1038. goto out;
  1039. }
  1040. /* If we hit here we were able to read the register and we need to
  1041. * return the value to the caller
  1042. */
  1043. *value = readl(&mac->mii_mgmt_stat) & ET_MAC_MIIMGMT_STAT_PHYCRTL_MASK;
  1044. out:
  1045. /* Stop the read operation */
  1046. writel(0, &mac->mii_mgmt_cmd);
  1047. /* set the registers we touched back to the state at which we entered
  1048. * this function
  1049. */
  1050. writel(mii_addr, &mac->mii_mgmt_addr);
  1051. writel(mii_cmd, &mac->mii_mgmt_cmd);
  1052. return status;
  1053. }
  1054. static int et131x_mii_read(struct et131x_adapter *adapter, u8 reg, u16 *value)
  1055. {
  1056. struct phy_device *phydev = adapter->phydev;
  1057. if (!phydev)
  1058. return -EIO;
  1059. return et131x_phy_mii_read(adapter, phydev->addr, reg, value);
  1060. }
  1061. static int et131x_mii_write(struct et131x_adapter *adapter, u8 addr, u8 reg,
  1062. u16 value)
  1063. {
  1064. struct mac_regs __iomem *mac = &adapter->regs->mac;
  1065. int status = 0;
  1066. u32 delay = 0;
  1067. u32 mii_addr;
  1068. u32 mii_cmd;
  1069. u32 mii_indicator;
  1070. /* Save a local copy of the registers we are dealing with so we can
  1071. * set them back
  1072. */
  1073. mii_addr = readl(&mac->mii_mgmt_addr);
  1074. mii_cmd = readl(&mac->mii_mgmt_cmd);
  1075. /* Stop the current operation */
  1076. writel(0, &mac->mii_mgmt_cmd);
  1077. /* Set up the register we need to write to on the correct PHY */
  1078. writel(ET_MAC_MII_ADDR(addr, reg), &mac->mii_mgmt_addr);
  1079. /* Add the value to write to the registers to the mac */
  1080. writel(value, &mac->mii_mgmt_ctrl);
  1081. do {
  1082. udelay(50);
  1083. delay++;
  1084. mii_indicator = readl(&mac->mii_mgmt_indicator);
  1085. } while ((mii_indicator & ET_MAC_MGMT_BUSY) && delay < 100);
  1086. /* If we hit the max delay, we could not write the register */
  1087. if (delay == 100) {
  1088. u16 tmp;
  1089. dev_warn(&adapter->pdev->dev,
  1090. "reg 0x%08x could not be written", reg);
  1091. dev_warn(&adapter->pdev->dev, "status is 0x%08x\n",
  1092. mii_indicator);
  1093. dev_warn(&adapter->pdev->dev, "command is 0x%08x\n",
  1094. readl(&mac->mii_mgmt_cmd));
  1095. et131x_mii_read(adapter, reg, &tmp);
  1096. status = -EIO;
  1097. }
  1098. /* Stop the write operation */
  1099. writel(0, &mac->mii_mgmt_cmd);
  1100. /* set the registers we touched back to the state at which we entered
  1101. * this function
  1102. */
  1103. writel(mii_addr, &mac->mii_mgmt_addr);
  1104. writel(mii_cmd, &mac->mii_mgmt_cmd);
  1105. return status;
  1106. }
  1107. static void et1310_phy_read_mii_bit(struct et131x_adapter *adapter,
  1108. u16 regnum,
  1109. u16 bitnum,
  1110. u8 *value)
  1111. {
  1112. u16 reg;
  1113. u16 mask = 1 << bitnum;
  1114. et131x_mii_read(adapter, regnum, &reg);
  1115. *value = (reg & mask) >> bitnum;
  1116. }
  1117. static void et1310_config_flow_control(struct et131x_adapter *adapter)
  1118. {
  1119. struct phy_device *phydev = adapter->phydev;
  1120. if (phydev->duplex == DUPLEX_HALF) {
  1121. adapter->flow = FLOW_NONE;
  1122. } else {
  1123. char remote_pause, remote_async_pause;
  1124. et1310_phy_read_mii_bit(adapter, 5, 10, &remote_pause);
  1125. et1310_phy_read_mii_bit(adapter, 5, 11, &remote_async_pause);
  1126. if (remote_pause && remote_async_pause) {
  1127. adapter->flow = adapter->wanted_flow;
  1128. } else if (remote_pause && !remote_async_pause) {
  1129. if (adapter->wanted_flow == FLOW_BOTH)
  1130. adapter->flow = FLOW_BOTH;
  1131. else
  1132. adapter->flow = FLOW_NONE;
  1133. } else if (!remote_pause && !remote_async_pause) {
  1134. adapter->flow = FLOW_NONE;
  1135. } else {
  1136. if (adapter->wanted_flow == FLOW_BOTH)
  1137. adapter->flow = FLOW_RXONLY;
  1138. else
  1139. adapter->flow = FLOW_NONE;
  1140. }
  1141. }
  1142. }
  1143. /* et1310_update_macstat_host_counters - Update local copy of the statistics */
  1144. static void et1310_update_macstat_host_counters(struct et131x_adapter *adapter)
  1145. {
  1146. struct ce_stats *stats = &adapter->stats;
  1147. struct macstat_regs __iomem *macstat =
  1148. &adapter->regs->macstat;
  1149. stats->tx_collisions += readl(&macstat->tx_total_collisions);
  1150. stats->tx_first_collisions += readl(&macstat->tx_single_collisions);
  1151. stats->tx_deferred += readl(&macstat->tx_deferred);
  1152. stats->tx_excessive_collisions +=
  1153. readl(&macstat->tx_multiple_collisions);
  1154. stats->tx_late_collisions += readl(&macstat->tx_late_collisions);
  1155. stats->tx_underflows += readl(&macstat->tx_undersize_frames);
  1156. stats->tx_max_pkt_errs += readl(&macstat->tx_oversize_frames);
  1157. stats->rx_align_errs += readl(&macstat->rx_align_errs);
  1158. stats->rx_crc_errs += readl(&macstat->rx_code_errs);
  1159. stats->rcvd_pkts_dropped += readl(&macstat->rx_drops);
  1160. stats->rx_overflows += readl(&macstat->rx_oversize_packets);
  1161. stats->rx_code_violations += readl(&macstat->rx_fcs_errs);
  1162. stats->rx_length_errs += readl(&macstat->rx_frame_len_errs);
  1163. stats->rx_other_errs += readl(&macstat->rx_fragment_packets);
  1164. }
  1165. /* et1310_handle_macstat_interrupt
  1166. *
  1167. * One of the MACSTAT counters has wrapped. Update the local copy of
  1168. * the statistics held in the adapter structure, checking the "wrap"
  1169. * bit for each counter.
  1170. */
  1171. static void et1310_handle_macstat_interrupt(struct et131x_adapter *adapter)
  1172. {
  1173. u32 carry_reg1;
  1174. u32 carry_reg2;
  1175. /* Read the interrupt bits from the register(s). These are Clear On
  1176. * Write.
  1177. */
  1178. carry_reg1 = readl(&adapter->regs->macstat.carry_reg1);
  1179. carry_reg2 = readl(&adapter->regs->macstat.carry_reg2);
  1180. writel(carry_reg1, &adapter->regs->macstat.carry_reg1);
  1181. writel(carry_reg2, &adapter->regs->macstat.carry_reg2);
  1182. /* We need to do update the host copy of all the MAC_STAT counters.
  1183. * For each counter, check it's overflow bit. If the overflow bit is
  1184. * set, then increment the host version of the count by one complete
  1185. * revolution of the counter. This routine is called when the counter
  1186. * block indicates that one of the counters has wrapped.
  1187. */
  1188. if (carry_reg1 & (1 << 14))
  1189. adapter->stats.rx_code_violations += COUNTER_WRAP_16_BIT;
  1190. if (carry_reg1 & (1 << 8))
  1191. adapter->stats.rx_align_errs += COUNTER_WRAP_12_BIT;
  1192. if (carry_reg1 & (1 << 7))
  1193. adapter->stats.rx_length_errs += COUNTER_WRAP_16_BIT;
  1194. if (carry_reg1 & (1 << 2))
  1195. adapter->stats.rx_other_errs += COUNTER_WRAP_16_BIT;
  1196. if (carry_reg1 & (1 << 6))
  1197. adapter->stats.rx_crc_errs += COUNTER_WRAP_16_BIT;
  1198. if (carry_reg1 & (1 << 3))
  1199. adapter->stats.rx_overflows += COUNTER_WRAP_16_BIT;
  1200. if (carry_reg1 & (1 << 0))
  1201. adapter->stats.rcvd_pkts_dropped += COUNTER_WRAP_16_BIT;
  1202. if (carry_reg2 & (1 << 16))
  1203. adapter->stats.tx_max_pkt_errs += COUNTER_WRAP_12_BIT;
  1204. if (carry_reg2 & (1 << 15))
  1205. adapter->stats.tx_underflows += COUNTER_WRAP_12_BIT;
  1206. if (carry_reg2 & (1 << 6))
  1207. adapter->stats.tx_first_collisions += COUNTER_WRAP_12_BIT;
  1208. if (carry_reg2 & (1 << 8))
  1209. adapter->stats.tx_deferred += COUNTER_WRAP_12_BIT;
  1210. if (carry_reg2 & (1 << 5))
  1211. adapter->stats.tx_excessive_collisions += COUNTER_WRAP_12_BIT;
  1212. if (carry_reg2 & (1 << 4))
  1213. adapter->stats.tx_late_collisions += COUNTER_WRAP_12_BIT;
  1214. if (carry_reg2 & (1 << 2))
  1215. adapter->stats.tx_collisions += COUNTER_WRAP_12_BIT;
  1216. }
  1217. static int et131x_mdio_read(struct mii_bus *bus, int phy_addr, int reg)
  1218. {
  1219. struct net_device *netdev = bus->priv;
  1220. struct et131x_adapter *adapter = netdev_priv(netdev);
  1221. u16 value;
  1222. int ret;
  1223. ret = et131x_phy_mii_read(adapter, phy_addr, reg, &value);
  1224. if (ret < 0)
  1225. return ret;
  1226. return value;
  1227. }
  1228. static int et131x_mdio_write(struct mii_bus *bus, int phy_addr,
  1229. int reg, u16 value)
  1230. {
  1231. struct net_device *netdev = bus->priv;
  1232. struct et131x_adapter *adapter = netdev_priv(netdev);
  1233. return et131x_mii_write(adapter, phy_addr, reg, value);
  1234. }
  1235. /* et1310_phy_power_switch - PHY power control
  1236. * @adapter: device to control
  1237. * @down: true for off/false for back on
  1238. *
  1239. * one hundred, ten, one thousand megs
  1240. * How would you like to have your LAN accessed
  1241. * Can't you see that this code processed
  1242. * Phy power, phy power..
  1243. */
  1244. static void et1310_phy_power_switch(struct et131x_adapter *adapter, bool down)
  1245. {
  1246. u16 data;
  1247. struct phy_device *phydev = adapter->phydev;
  1248. et131x_mii_read(adapter, MII_BMCR, &data);
  1249. data &= ~BMCR_PDOWN;
  1250. if (down)
  1251. data |= BMCR_PDOWN;
  1252. et131x_mii_write(adapter, phydev->addr, MII_BMCR, data);
  1253. }
  1254. /* et131x_xcvr_init - Init the phy if we are setting it into force mode */
  1255. static void et131x_xcvr_init(struct et131x_adapter *adapter)
  1256. {
  1257. u16 lcr2;
  1258. struct phy_device *phydev = adapter->phydev;
  1259. /* Set the LED behavior such that LED 1 indicates speed (off =
  1260. * 10Mbits, blink = 100Mbits, on = 1000Mbits) and LED 2 indicates
  1261. * link and activity (on for link, blink off for activity).
  1262. *
  1263. * NOTE: Some customizations have been added here for specific
  1264. * vendors; The LED behavior is now determined by vendor data in the
  1265. * EEPROM. However, the above description is the default.
  1266. */
  1267. if ((adapter->eeprom_data[1] & 0x4) == 0) {
  1268. et131x_mii_read(adapter, PHY_LED_2, &lcr2);
  1269. lcr2 &= (ET_LED2_LED_100TX | ET_LED2_LED_1000T);
  1270. lcr2 |= (LED_VAL_LINKON_ACTIVE << LED_LINK_SHIFT);
  1271. if ((adapter->eeprom_data[1] & 0x8) == 0)
  1272. lcr2 |= (LED_VAL_1000BT_100BTX << LED_TXRX_SHIFT);
  1273. else
  1274. lcr2 |= (LED_VAL_LINKON << LED_TXRX_SHIFT);
  1275. et131x_mii_write(adapter, phydev->addr, PHY_LED_2, lcr2);
  1276. }
  1277. }
  1278. /* et131x_configure_global_regs - configure JAGCore global regs */
  1279. static void et131x_configure_global_regs(struct et131x_adapter *adapter)
  1280. {
  1281. struct global_regs __iomem *regs = &adapter->regs->global;
  1282. writel(0, &regs->rxq_start_addr);
  1283. writel(INTERNAL_MEM_SIZE - 1, &regs->txq_end_addr);
  1284. if (adapter->registry_jumbo_packet < 2048) {
  1285. /* Tx / RxDMA and Tx/Rx MAC interfaces have a 1k word
  1286. * block of RAM that the driver can split between Tx
  1287. * and Rx as it desires. Our default is to split it
  1288. * 50/50:
  1289. */
  1290. writel(PARM_RX_MEM_END_DEF, &regs->rxq_end_addr);
  1291. writel(PARM_RX_MEM_END_DEF + 1, &regs->txq_start_addr);
  1292. } else if (adapter->registry_jumbo_packet < 8192) {
  1293. /* For jumbo packets > 2k but < 8k, split 50-50. */
  1294. writel(INTERNAL_MEM_RX_OFFSET, &regs->rxq_end_addr);
  1295. writel(INTERNAL_MEM_RX_OFFSET + 1, &regs->txq_start_addr);
  1296. } else {
  1297. /* 9216 is the only packet size greater than 8k that
  1298. * is available. The Tx buffer has to be big enough
  1299. * for one whole packet on the Tx side. We'll make
  1300. * the Tx 9408, and give the rest to Rx
  1301. */
  1302. writel(0x01b3, &regs->rxq_end_addr);
  1303. writel(0x01b4, &regs->txq_start_addr);
  1304. }
  1305. /* Initialize the loopback register. Disable all loopbacks. */
  1306. writel(0, &regs->loopback);
  1307. writel(0, &regs->msi_config);
  1308. /* By default, disable the watchdog timer. It will be enabled when
  1309. * a packet is queued.
  1310. */
  1311. writel(0, &regs->watchdog_timer);
  1312. }
  1313. /* et131x_config_rx_dma_regs - Start of Rx_DMA init sequence */
  1314. static void et131x_config_rx_dma_regs(struct et131x_adapter *adapter)
  1315. {
  1316. struct rxdma_regs __iomem *rx_dma = &adapter->regs->rxdma;
  1317. struct rx_ring *rx_local = &adapter->rx_ring;
  1318. struct fbr_desc *fbr_entry;
  1319. u32 entry;
  1320. u32 psr_num_des;
  1321. unsigned long flags;
  1322. u8 id;
  1323. et131x_rx_dma_disable(adapter);
  1324. /* Load the completion writeback physical address */
  1325. writel(upper_32_bits(rx_local->rx_status_bus), &rx_dma->dma_wb_base_hi);
  1326. writel(lower_32_bits(rx_local->rx_status_bus), &rx_dma->dma_wb_base_lo);
  1327. memset(rx_local->rx_status_block, 0, sizeof(struct rx_status_block));
  1328. /* Set the address and parameters of the packet status ring */
  1329. writel(upper_32_bits(rx_local->ps_ring_physaddr), &rx_dma->psr_base_hi);
  1330. writel(lower_32_bits(rx_local->ps_ring_physaddr), &rx_dma->psr_base_lo);
  1331. writel(rx_local->psr_entries - 1, &rx_dma->psr_num_des);
  1332. writel(0, &rx_dma->psr_full_offset);
  1333. psr_num_des = readl(&rx_dma->psr_num_des) & ET_RXDMA_PSR_NUM_DES_MASK;
  1334. writel((psr_num_des * LO_MARK_PERCENT_FOR_PSR) / 100,
  1335. &rx_dma->psr_min_des);
  1336. spin_lock_irqsave(&adapter->rcv_lock, flags);
  1337. /* These local variables track the PSR in the adapter structure */
  1338. rx_local->local_psr_full = 0;
  1339. for (id = 0; id < NUM_FBRS; id++) {
  1340. u32 __iomem *num_des;
  1341. u32 __iomem *full_offset;
  1342. u32 __iomem *min_des;
  1343. u32 __iomem *base_hi;
  1344. u32 __iomem *base_lo;
  1345. struct fbr_lookup *fbr = rx_local->fbr[id];
  1346. if (id == 0) {
  1347. num_des = &rx_dma->fbr0_num_des;
  1348. full_offset = &rx_dma->fbr0_full_offset;
  1349. min_des = &rx_dma->fbr0_min_des;
  1350. base_hi = &rx_dma->fbr0_base_hi;
  1351. base_lo = &rx_dma->fbr0_base_lo;
  1352. } else {
  1353. num_des = &rx_dma->fbr1_num_des;
  1354. full_offset = &rx_dma->fbr1_full_offset;
  1355. min_des = &rx_dma->fbr1_min_des;
  1356. base_hi = &rx_dma->fbr1_base_hi;
  1357. base_lo = &rx_dma->fbr1_base_lo;
  1358. }
  1359. /* Now's the best time to initialize FBR contents */
  1360. fbr_entry = fbr->ring_virtaddr;
  1361. for (entry = 0; entry < fbr->num_entries; entry++) {
  1362. fbr_entry->addr_hi = fbr->bus_high[entry];
  1363. fbr_entry->addr_lo = fbr->bus_low[entry];
  1364. fbr_entry->word2 = entry;
  1365. fbr_entry++;
  1366. }
  1367. /* Set the address and parameters of Free buffer ring 1 and 0 */
  1368. writel(upper_32_bits(fbr->ring_physaddr), base_hi);
  1369. writel(lower_32_bits(fbr->ring_physaddr), base_lo);
  1370. writel(fbr->num_entries - 1, num_des);
  1371. writel(ET_DMA10_WRAP, full_offset);
  1372. /* This variable tracks the free buffer ring 1 full position,
  1373. * so it has to match the above.
  1374. */
  1375. fbr->local_full = ET_DMA10_WRAP;
  1376. writel(((fbr->num_entries * LO_MARK_PERCENT_FOR_RX) / 100) - 1,
  1377. min_des);
  1378. }
  1379. /* Program the number of packets we will receive before generating an
  1380. * interrupt.
  1381. * For version B silicon, this value gets updated once autoneg is
  1382. *complete.
  1383. */
  1384. writel(PARM_RX_NUM_BUFS_DEF, &rx_dma->num_pkt_done);
  1385. /* The "time_done" is not working correctly to coalesce interrupts
  1386. * after a given time period, but rather is giving us an interrupt
  1387. * regardless of whether we have received packets.
  1388. * This value gets updated once autoneg is complete.
  1389. */
  1390. writel(PARM_RX_TIME_INT_DEF, &rx_dma->max_pkt_time);
  1391. spin_unlock_irqrestore(&adapter->rcv_lock, flags);
  1392. }
  1393. /* et131x_config_tx_dma_regs - Set up the tx dma section of the JAGCore.
  1394. *
  1395. * Configure the transmit engine with the ring buffers we have created
  1396. * and prepare it for use.
  1397. */
  1398. static void et131x_config_tx_dma_regs(struct et131x_adapter *adapter)
  1399. {
  1400. struct txdma_regs __iomem *txdma = &adapter->regs->txdma;
  1401. struct tx_ring *tx_ring = &adapter->tx_ring;
  1402. /* Load the hardware with the start of the transmit descriptor ring. */
  1403. writel(upper_32_bits(tx_ring->tx_desc_ring_pa), &txdma->pr_base_hi);
  1404. writel(lower_32_bits(tx_ring->tx_desc_ring_pa), &txdma->pr_base_lo);
  1405. /* Initialise the transmit DMA engine */
  1406. writel(NUM_DESC_PER_RING_TX - 1, &txdma->pr_num_des);
  1407. /* Load the completion writeback physical address */
  1408. writel(upper_32_bits(tx_ring->tx_status_pa), &txdma->dma_wb_base_hi);
  1409. writel(lower_32_bits(tx_ring->tx_status_pa), &txdma->dma_wb_base_lo);
  1410. *tx_ring->tx_status = 0;
  1411. writel(0, &txdma->service_request);
  1412. tx_ring->send_idx = 0;
  1413. }
  1414. /* et131x_adapter_setup - Set the adapter up as per cassini+ documentation */
  1415. static void et131x_adapter_setup(struct et131x_adapter *adapter)
  1416. {
  1417. et131x_configure_global_regs(adapter);
  1418. et1310_config_mac_regs1(adapter);
  1419. /* Configure the MMC registers */
  1420. /* All we need to do is initialize the Memory Control Register */
  1421. writel(ET_MMC_ENABLE, &adapter->regs->mmc.mmc_ctrl);
  1422. et1310_config_rxmac_regs(adapter);
  1423. et1310_config_txmac_regs(adapter);
  1424. et131x_config_rx_dma_regs(adapter);
  1425. et131x_config_tx_dma_regs(adapter);
  1426. et1310_config_macstat_regs(adapter);
  1427. et1310_phy_power_switch(adapter, 0);
  1428. et131x_xcvr_init(adapter);
  1429. }
  1430. /* et131x_soft_reset - Issue soft reset to the hardware, complete for ET1310 */
  1431. static void et131x_soft_reset(struct et131x_adapter *adapter)
  1432. {
  1433. u32 reg;
  1434. /* Disable MAC Core */
  1435. reg = ET_MAC_CFG1_SOFT_RESET | ET_MAC_CFG1_SIM_RESET |
  1436. ET_MAC_CFG1_RESET_RXMC | ET_MAC_CFG1_RESET_TXMC |
  1437. ET_MAC_CFG1_RESET_RXFUNC | ET_MAC_CFG1_RESET_TXFUNC;
  1438. writel(reg, &adapter->regs->mac.cfg1);
  1439. reg = ET_RESET_ALL;
  1440. writel(reg, &adapter->regs->global.sw_reset);
  1441. reg = ET_MAC_CFG1_RESET_RXMC | ET_MAC_CFG1_RESET_TXMC |
  1442. ET_MAC_CFG1_RESET_RXFUNC | ET_MAC_CFG1_RESET_TXFUNC;
  1443. writel(reg, &adapter->regs->mac.cfg1);
  1444. writel(0, &adapter->regs->mac.cfg1);
  1445. }
  1446. static void et131x_enable_interrupts(struct et131x_adapter *adapter)
  1447. {
  1448. u32 mask;
  1449. if (adapter->flow == FLOW_TXONLY || adapter->flow == FLOW_BOTH)
  1450. mask = INT_MASK_ENABLE;
  1451. else
  1452. mask = INT_MASK_ENABLE_NO_FLOW;
  1453. writel(mask, &adapter->regs->global.int_mask);
  1454. }
  1455. static void et131x_disable_interrupts(struct et131x_adapter *adapter)
  1456. {
  1457. writel(INT_MASK_DISABLE, &adapter->regs->global.int_mask);
  1458. }
  1459. static void et131x_tx_dma_disable(struct et131x_adapter *adapter)
  1460. {
  1461. /* Setup the transmit dma configuration register */
  1462. writel(ET_TXDMA_CSR_HALT | ET_TXDMA_SNGL_EPKT,
  1463. &adapter->regs->txdma.csr);
  1464. }
  1465. static void et131x_enable_txrx(struct net_device *netdev)
  1466. {
  1467. struct et131x_adapter *adapter = netdev_priv(netdev);
  1468. et131x_rx_dma_enable(adapter);
  1469. et131x_tx_dma_enable(adapter);
  1470. if (adapter->flags & FMP_ADAPTER_INTERRUPT_IN_USE)
  1471. et131x_enable_interrupts(adapter);
  1472. netif_start_queue(netdev);
  1473. }
  1474. static void et131x_disable_txrx(struct net_device *netdev)
  1475. {
  1476. struct et131x_adapter *adapter = netdev_priv(netdev);
  1477. netif_stop_queue(netdev);
  1478. et131x_rx_dma_disable(adapter);
  1479. et131x_tx_dma_disable(adapter);
  1480. et131x_disable_interrupts(adapter);
  1481. }
  1482. static void et131x_init_send(struct et131x_adapter *adapter)
  1483. {
  1484. int i;
  1485. struct tx_ring *tx_ring = &adapter->tx_ring;
  1486. struct tcb *tcb = tx_ring->tcb_ring;
  1487. tx_ring->tcb_qhead = tcb;
  1488. memset(tcb, 0, sizeof(struct tcb) * NUM_TCB);
  1489. for (i = 0; i < NUM_TCB; i++) {
  1490. tcb->next = tcb + 1;
  1491. tcb++;
  1492. }
  1493. tcb--;
  1494. tx_ring->tcb_qtail = tcb;
  1495. tcb->next = NULL;
  1496. /* Curr send queue should now be empty */
  1497. tx_ring->send_head = NULL;
  1498. tx_ring->send_tail = NULL;
  1499. }
  1500. /* et1310_enable_phy_coma
  1501. *
  1502. * driver receive an phy status change interrupt while in D0 and check that
  1503. * phy_status is down.
  1504. *
  1505. * -- gate off JAGCore;
  1506. * -- set gigE PHY in Coma mode
  1507. * -- wake on phy_interrupt; Perform software reset JAGCore,
  1508. * re-initialize jagcore and gigE PHY
  1509. */
  1510. static void et1310_enable_phy_coma(struct et131x_adapter *adapter)
  1511. {
  1512. u32 pmcsr = readl(&adapter->regs->global.pm_csr);
  1513. /* Stop sending packets. */
  1514. adapter->flags |= FMP_ADAPTER_LOWER_POWER;
  1515. /* Wait for outstanding Receive packets */
  1516. et131x_disable_txrx(adapter->netdev);
  1517. /* Gate off JAGCore 3 clock domains */
  1518. pmcsr &= ~ET_PMCSR_INIT;
  1519. writel(pmcsr, &adapter->regs->global.pm_csr);
  1520. /* Program gigE PHY in to Coma mode */
  1521. pmcsr |= ET_PM_PHY_SW_COMA;
  1522. writel(pmcsr, &adapter->regs->global.pm_csr);
  1523. }
  1524. static void et1310_disable_phy_coma(struct et131x_adapter *adapter)
  1525. {
  1526. u32 pmcsr;
  1527. pmcsr = readl(&adapter->regs->global.pm_csr);
  1528. /* Disable phy_sw_coma register and re-enable JAGCore clocks */
  1529. pmcsr |= ET_PMCSR_INIT;
  1530. pmcsr &= ~ET_PM_PHY_SW_COMA;
  1531. writel(pmcsr, &adapter->regs->global.pm_csr);
  1532. /* Restore the GbE PHY speed and duplex modes;
  1533. * Reset JAGCore; re-configure and initialize JAGCore and gigE PHY
  1534. */
  1535. /* Re-initialize the send structures */
  1536. et131x_init_send(adapter);
  1537. /* Bring the device back to the state it was during init prior to
  1538. * autonegotiation being complete. This way, when we get the auto-neg
  1539. * complete interrupt, we can complete init by calling ConfigMacREGS2.
  1540. */
  1541. et131x_soft_reset(adapter);
  1542. et131x_adapter_setup(adapter);
  1543. /* Allow Tx to restart */
  1544. adapter->flags &= ~FMP_ADAPTER_LOWER_POWER;
  1545. et131x_enable_txrx(adapter->netdev);
  1546. }
  1547. static inline u32 bump_free_buff_ring(u32 *free_buff_ring, u32 limit)
  1548. {
  1549. u32 tmp_free_buff_ring = *free_buff_ring;
  1550. tmp_free_buff_ring++;
  1551. /* This works for all cases where limit < 1024. The 1023 case
  1552. * works because 1023++ is 1024 which means the if condition is not
  1553. * taken but the carry of the bit into the wrap bit toggles the wrap
  1554. * value correctly
  1555. */
  1556. if ((tmp_free_buff_ring & ET_DMA10_MASK) > limit) {
  1557. tmp_free_buff_ring &= ~ET_DMA10_MASK;
  1558. tmp_free_buff_ring ^= ET_DMA10_WRAP;
  1559. }
  1560. /* For the 1023 case */
  1561. tmp_free_buff_ring &= (ET_DMA10_MASK | ET_DMA10_WRAP);
  1562. *free_buff_ring = tmp_free_buff_ring;
  1563. return tmp_free_buff_ring;
  1564. }
  1565. /* et131x_rx_dma_memory_alloc
  1566. *
  1567. * Allocates Free buffer ring 1 for sure, free buffer ring 0 if required,
  1568. * and the Packet Status Ring.
  1569. */
  1570. static int et131x_rx_dma_memory_alloc(struct et131x_adapter *adapter)
  1571. {
  1572. u8 id;
  1573. u32 i, j;
  1574. u32 bufsize;
  1575. u32 psr_size;
  1576. u32 fbr_chunksize;
  1577. struct rx_ring *rx_ring = &adapter->rx_ring;
  1578. struct fbr_lookup *fbr;
  1579. /* Alloc memory for the lookup table */
  1580. rx_ring->fbr[0] = kzalloc(sizeof(*fbr), GFP_KERNEL);
  1581. if (rx_ring->fbr[0] == NULL)
  1582. return -ENOMEM;
  1583. rx_ring->fbr[1] = kzalloc(sizeof(*fbr), GFP_KERNEL);
  1584. if (rx_ring->fbr[1] == NULL)
  1585. return -ENOMEM;
  1586. /* The first thing we will do is configure the sizes of the buffer
  1587. * rings. These will change based on jumbo packet support. Larger
  1588. * jumbo packets increases the size of each entry in FBR0, and the
  1589. * number of entries in FBR0, while at the same time decreasing the
  1590. * number of entries in FBR1.
  1591. *
  1592. * FBR1 holds "large" frames, FBR0 holds "small" frames. If FBR1
  1593. * entries are huge in order to accommodate a "jumbo" frame, then it
  1594. * will have less entries. Conversely, FBR1 will now be relied upon
  1595. * to carry more "normal" frames, thus it's entry size also increases
  1596. * and the number of entries goes up too (since it now carries
  1597. * "small" + "regular" packets.
  1598. *
  1599. * In this scheme, we try to maintain 512 entries between the two
  1600. * rings. Also, FBR1 remains a constant size - when it's size doubles
  1601. * the number of entries halves. FBR0 increases in size, however.
  1602. */
  1603. if (adapter->registry_jumbo_packet < 2048) {
  1604. rx_ring->fbr[0]->buffsize = 256;
  1605. rx_ring->fbr[0]->num_entries = 512;
  1606. rx_ring->fbr[1]->buffsize = 2048;
  1607. rx_ring->fbr[1]->num_entries = 512;
  1608. } else if (adapter->registry_jumbo_packet < 4096) {
  1609. rx_ring->fbr[0]->buffsize = 512;
  1610. rx_ring->fbr[0]->num_entries = 1024;
  1611. rx_ring->fbr[1]->buffsize = 4096;
  1612. rx_ring->fbr[1]->num_entries = 512;
  1613. } else {
  1614. rx_ring->fbr[0]->buffsize = 1024;
  1615. rx_ring->fbr[0]->num_entries = 768;
  1616. rx_ring->fbr[1]->buffsize = 16384;
  1617. rx_ring->fbr[1]->num_entries = 128;
  1618. }
  1619. rx_ring->psr_entries = rx_ring->fbr[0]->num_entries +
  1620. rx_ring->fbr[1]->num_entries;
  1621. for (id = 0; id < NUM_FBRS; id++) {
  1622. fbr = rx_ring->fbr[id];
  1623. /* Allocate an area of memory for Free Buffer Ring */
  1624. bufsize = sizeof(struct fbr_desc) * fbr->num_entries;
  1625. fbr->ring_virtaddr = dma_alloc_coherent(&adapter->pdev->dev,
  1626. bufsize,
  1627. &fbr->ring_physaddr,
  1628. GFP_KERNEL);
  1629. if (!fbr->ring_virtaddr) {
  1630. dev_err(&adapter->pdev->dev,
  1631. "Cannot alloc memory for Free Buffer Ring %d\n",
  1632. id);
  1633. return -ENOMEM;
  1634. }
  1635. }
  1636. for (id = 0; id < NUM_FBRS; id++) {
  1637. fbr = rx_ring->fbr[id];
  1638. fbr_chunksize = (FBR_CHUNKS * fbr->buffsize);
  1639. for (i = 0; i < fbr->num_entries / FBR_CHUNKS; i++) {
  1640. dma_addr_t fbr_physaddr;
  1641. fbr->mem_virtaddrs[i] = dma_alloc_coherent(
  1642. &adapter->pdev->dev, fbr_chunksize,
  1643. &fbr->mem_physaddrs[i],
  1644. GFP_KERNEL);
  1645. if (!fbr->mem_virtaddrs[i]) {
  1646. dev_err(&adapter->pdev->dev,
  1647. "Could not alloc memory\n");
  1648. return -ENOMEM;
  1649. }
  1650. /* See NOTE in "Save Physical Address" comment above */
  1651. fbr_physaddr = fbr->mem_physaddrs[i];
  1652. for (j = 0; j < FBR_CHUNKS; j++) {
  1653. u32 k = (i * FBR_CHUNKS) + j;
  1654. /* Save the Virtual address of this index for
  1655. * quick access later
  1656. */
  1657. fbr->virt[k] = (u8 *)fbr->mem_virtaddrs[i] +
  1658. (j * fbr->buffsize);
  1659. /* now store the physical address in the
  1660. * descriptor so the device can access it
  1661. */
  1662. fbr->bus_high[k] = upper_32_bits(fbr_physaddr);
  1663. fbr->bus_low[k] = lower_32_bits(fbr_physaddr);
  1664. fbr_physaddr += fbr->buffsize;
  1665. }
  1666. }
  1667. }
  1668. /* Allocate an area of memory for FIFO of Packet Status ring entries */
  1669. psr_size = sizeof(struct pkt_stat_desc) * rx_ring->psr_entries;
  1670. rx_ring->ps_ring_virtaddr = dma_alloc_coherent(&adapter->pdev->dev,
  1671. psr_size,
  1672. &rx_ring->ps_ring_physaddr,
  1673. GFP_KERNEL);
  1674. if (!rx_ring->ps_ring_virtaddr) {
  1675. dev_err(&adapter->pdev->dev,
  1676. "Cannot alloc memory for Packet Status Ring\n");
  1677. return -ENOMEM;
  1678. }
  1679. /* Allocate an area of memory for writeback of status information */
  1680. rx_ring->rx_status_block = dma_alloc_coherent(&adapter->pdev->dev,
  1681. sizeof(struct rx_status_block),
  1682. &rx_ring->rx_status_bus,
  1683. GFP_KERNEL);
  1684. if (!rx_ring->rx_status_block) {
  1685. dev_err(&adapter->pdev->dev,
  1686. "Cannot alloc memory for Status Block\n");
  1687. return -ENOMEM;
  1688. }
  1689. rx_ring->num_rfd = NIC_DEFAULT_NUM_RFD;
  1690. /* The RFDs are going to be put on lists later on, so initialize the
  1691. * lists now.
  1692. */
  1693. INIT_LIST_HEAD(&rx_ring->recv_list);
  1694. return 0;
  1695. }
  1696. static void et131x_rx_dma_memory_free(struct et131x_adapter *adapter)
  1697. {
  1698. u8 id;
  1699. u32 ii;
  1700. u32 bufsize;
  1701. u32 psr_size;
  1702. struct rfd *rfd;
  1703. struct rx_ring *rx_ring = &adapter->rx_ring;
  1704. struct fbr_lookup *fbr;
  1705. /* Free RFDs and associated packet descriptors */
  1706. WARN_ON(rx_ring->num_ready_recv != rx_ring->num_rfd);
  1707. while (!list_empty(&rx_ring->recv_list)) {
  1708. rfd = list_entry(rx_ring->recv_list.next,
  1709. struct rfd, list_node);
  1710. list_del(&rfd->list_node);
  1711. rfd->skb = NULL;
  1712. kfree(rfd);
  1713. }
  1714. /* Free Free Buffer Rings */
  1715. for (id = 0; id < NUM_FBRS; id++) {
  1716. fbr = rx_ring->fbr[id];
  1717. if (!fbr || !fbr->ring_virtaddr)
  1718. continue;
  1719. /* First the packet memory */
  1720. for (ii = 0; ii < fbr->num_entries / FBR_CHUNKS; ii++) {
  1721. if (fbr->mem_virtaddrs[ii]) {
  1722. bufsize = fbr->buffsize * FBR_CHUNKS;
  1723. dma_free_coherent(&adapter->pdev->dev,
  1724. bufsize,
  1725. fbr->mem_virtaddrs[ii],
  1726. fbr->mem_physaddrs[ii]);
  1727. fbr->mem_virtaddrs[ii] = NULL;
  1728. }
  1729. }
  1730. bufsize = sizeof(struct fbr_desc) * fbr->num_entries;
  1731. dma_free_coherent(&adapter->pdev->dev,
  1732. bufsize,
  1733. fbr->ring_virtaddr,
  1734. fbr->ring_physaddr);
  1735. fbr->ring_virtaddr = NULL;
  1736. }
  1737. /* Free Packet Status Ring */
  1738. if (rx_ring->ps_ring_virtaddr) {
  1739. psr_size = sizeof(struct pkt_stat_desc) * rx_ring->psr_entries;
  1740. dma_free_coherent(&adapter->pdev->dev, psr_size,
  1741. rx_ring->ps_ring_virtaddr,
  1742. rx_ring->ps_ring_physaddr);
  1743. rx_ring->ps_ring_virtaddr = NULL;
  1744. }
  1745. /* Free area of memory for the writeback of status information */
  1746. if (rx_ring->rx_status_block) {
  1747. dma_free_coherent(&adapter->pdev->dev,
  1748. sizeof(struct rx_status_block),
  1749. rx_ring->rx_status_block,
  1750. rx_ring->rx_status_bus);
  1751. rx_ring->rx_status_block = NULL;
  1752. }
  1753. /* Free the FBR Lookup Table */
  1754. kfree(rx_ring->fbr[0]);
  1755. kfree(rx_ring->fbr[1]);
  1756. /* Reset Counters */
  1757. rx_ring->num_ready_recv = 0;
  1758. }
  1759. /* et131x_init_recv - Initialize receive data structures */
  1760. static int et131x_init_recv(struct et131x_adapter *adapter)
  1761. {
  1762. struct rfd *rfd;
  1763. u32 rfdct;
  1764. struct rx_ring *rx_ring = &adapter->rx_ring;
  1765. /* Setup each RFD */
  1766. for (rfdct = 0; rfdct < rx_ring->num_rfd; rfdct++) {
  1767. rfd = kzalloc(sizeof(*rfd), GFP_ATOMIC | GFP_DMA);
  1768. if (!rfd)
  1769. return -ENOMEM;
  1770. rfd->skb = NULL;
  1771. /* Add this RFD to the recv_list */
  1772. list_add_tail(&rfd->list_node, &rx_ring->recv_list);
  1773. /* Increment the available RFD's */
  1774. rx_ring->num_ready_recv++;
  1775. }
  1776. return 0;
  1777. }
  1778. /* et131x_set_rx_dma_timer - Set the heartbeat timer according to line rate */
  1779. static void et131x_set_rx_dma_timer(struct et131x_adapter *adapter)
  1780. {
  1781. struct phy_device *phydev = adapter->phydev;
  1782. /* For version B silicon, we do not use the RxDMA timer for 10 and 100
  1783. * Mbits/s line rates. We do not enable and RxDMA interrupt coalescing.
  1784. */
  1785. if ((phydev->speed == SPEED_100) || (phydev->speed == SPEED_10)) {
  1786. writel(0, &adapter->regs->rxdma.max_pkt_time);
  1787. writel(1, &adapter->regs->rxdma.num_pkt_done);
  1788. }
  1789. }
  1790. /* nic_return_rfd - Recycle a RFD and put it back onto the receive list */
  1791. static void nic_return_rfd(struct et131x_adapter *adapter, struct rfd *rfd)
  1792. {
  1793. struct rx_ring *rx_local = &adapter->rx_ring;
  1794. struct rxdma_regs __iomem *rx_dma = &adapter->regs->rxdma;
  1795. u16 buff_index = rfd->bufferindex;
  1796. u8 ring_index = rfd->ringindex;
  1797. unsigned long flags;
  1798. struct fbr_lookup *fbr = rx_local->fbr[ring_index];
  1799. /* We don't use any of the OOB data besides status. Otherwise, we
  1800. * need to clean up OOB data
  1801. */
  1802. if (buff_index < fbr->num_entries) {
  1803. u32 free_buff_ring;
  1804. u32 __iomem *offset;
  1805. struct fbr_desc *next;
  1806. if (ring_index == 0)
  1807. offset = &rx_dma->fbr0_full_offset;
  1808. else
  1809. offset = &rx_dma->fbr1_full_offset;
  1810. next = (struct fbr_desc *)(fbr->ring_virtaddr) +
  1811. INDEX10(fbr->local_full);
  1812. /* Handle the Free Buffer Ring advancement here. Write
  1813. * the PA / Buffer Index for the returned buffer into
  1814. * the oldest (next to be freed)FBR entry
  1815. */
  1816. next->addr_hi = fbr->bus_high[buff_index];
  1817. next->addr_lo = fbr->bus_low[buff_index];
  1818. next->word2 = buff_index;
  1819. free_buff_ring = bump_free_buff_ring(&fbr->local_full,
  1820. fbr->num_entries - 1);
  1821. writel(free_buff_ring, offset);
  1822. } else {
  1823. dev_err(&adapter->pdev->dev,
  1824. "%s illegal Buffer Index returned\n", __func__);
  1825. }
  1826. /* The processing on this RFD is done, so put it back on the tail of
  1827. * our list
  1828. */
  1829. spin_lock_irqsave(&adapter->rcv_lock, flags);
  1830. list_add_tail(&rfd->list_node, &rx_local->recv_list);
  1831. rx_local->num_ready_recv++;
  1832. spin_unlock_irqrestore(&adapter->rcv_lock, flags);
  1833. WARN_ON(rx_local->num_ready_recv > rx_local->num_rfd);
  1834. }
  1835. /* nic_rx_pkts - Checks the hardware for available packets
  1836. *
  1837. * Checks the hardware for available packets, using completion ring
  1838. * If packets are available, it gets an RFD from the recv_list, attaches
  1839. * the packet to it, puts the RFD in the RecvPendList, and also returns
  1840. * the pointer to the RFD.
  1841. */
  1842. static struct rfd *nic_rx_pkts(struct et131x_adapter *adapter)
  1843. {
  1844. struct rx_ring *rx_local = &adapter->rx_ring;
  1845. struct rx_status_block *status;
  1846. struct pkt_stat_desc *psr;
  1847. struct rfd *rfd;
  1848. unsigned long flags;
  1849. struct list_head *element;
  1850. u8 ring_index;
  1851. u16 buff_index;
  1852. u32 len;
  1853. u32 word0;
  1854. u32 word1;
  1855. struct sk_buff *skb;
  1856. struct fbr_lookup *fbr;
  1857. /* RX Status block is written by the DMA engine prior to every
  1858. * interrupt. It contains the next to be used entry in the Packet
  1859. * Status Ring, and also the two Free Buffer rings.
  1860. */
  1861. status = rx_local->rx_status_block;
  1862. word1 = status->word1 >> 16;
  1863. /* Check the PSR and wrap bits do not match */
  1864. if ((word1 & 0x1FFF) == (rx_local->local_psr_full & 0x1FFF))
  1865. return NULL; /* Looks like this ring is not updated yet */
  1866. /* The packet status ring indicates that data is available. */
  1867. psr = (struct pkt_stat_desc *)(rx_local->ps_ring_virtaddr) +
  1868. (rx_local->local_psr_full & 0xFFF);
  1869. /* Grab any information that is required once the PSR is advanced,
  1870. * since we can no longer rely on the memory being accurate
  1871. */
  1872. len = psr->word1 & 0xFFFF;
  1873. ring_index = (psr->word1 >> 26) & 0x03;
  1874. fbr = rx_local->fbr[ring_index];
  1875. buff_index = (psr->word1 >> 16) & 0x3FF;
  1876. word0 = psr->word0;
  1877. /* Indicate that we have used this PSR entry. */
  1878. /* FIXME wrap 12 */
  1879. add_12bit(&rx_local->local_psr_full, 1);
  1880. if ((rx_local->local_psr_full & 0xFFF) > rx_local->psr_entries - 1) {
  1881. /* Clear psr full and toggle the wrap bit */
  1882. rx_local->local_psr_full &= ~0xFFF;
  1883. rx_local->local_psr_full ^= 0x1000;
  1884. }
  1885. writel(rx_local->local_psr_full, &adapter->regs->rxdma.psr_full_offset);
  1886. if (ring_index > 1 || buff_index > fbr->num_entries - 1) {
  1887. /* Illegal buffer or ring index cannot be used by S/W*/
  1888. dev_err(&adapter->pdev->dev,
  1889. "NICRxPkts PSR Entry %d indicates length of %d and/or bad bi(%d)\n",
  1890. rx_local->local_psr_full & 0xFFF, len, buff_index);
  1891. return NULL;
  1892. }
  1893. /* Get and fill the RFD. */
  1894. spin_lock_irqsave(&adapter->rcv_lock, flags);
  1895. element = rx_local->recv_list.next;
  1896. rfd = list_entry(element, struct rfd, list_node);
  1897. if (!rfd) {
  1898. spin_unlock_irqrestore(&adapter->rcv_lock, flags);
  1899. return NULL;
  1900. }
  1901. list_del(&rfd->list_node);
  1902. rx_local->num_ready_recv--;
  1903. spin_unlock_irqrestore(&adapter->rcv_lock, flags);
  1904. rfd->bufferindex = buff_index;
  1905. rfd->ringindex = ring_index;
  1906. /* In V1 silicon, there is a bug which screws up filtering of runt
  1907. * packets. Therefore runt packet filtering is disabled in the MAC and
  1908. * the packets are dropped here. They are also counted here.
  1909. */
  1910. if (len < (NIC_MIN_PACKET_SIZE + 4)) {
  1911. adapter->stats.rx_other_errs++;
  1912. rfd->len = 0;
  1913. goto out;
  1914. }
  1915. if ((word0 & ALCATEL_MULTICAST_PKT) && !(word0 & ALCATEL_BROADCAST_PKT))
  1916. adapter->stats.multicast_pkts_rcvd++;
  1917. rfd->len = len;
  1918. skb = dev_alloc_skb(rfd->len + 2);
  1919. if (!skb)
  1920. return NULL;
  1921. adapter->netdev->stats.rx_bytes += rfd->len;
  1922. memcpy(skb_put(skb, rfd->len), fbr->virt[buff_index], rfd->len);
  1923. skb->protocol = eth_type_trans(skb, adapter->netdev);
  1924. skb->ip_summed = CHECKSUM_NONE;
  1925. netif_receive_skb(skb);
  1926. out:
  1927. nic_return_rfd(adapter, rfd);
  1928. return rfd;
  1929. }
  1930. static int et131x_handle_recv_pkts(struct et131x_adapter *adapter, int budget)
  1931. {
  1932. struct rfd *rfd = NULL;
  1933. int count = 0;
  1934. int limit = budget;
  1935. bool done = true;
  1936. struct rx_ring *rx_ring = &adapter->rx_ring;
  1937. if (budget > MAX_PACKETS_HANDLED)
  1938. limit = MAX_PACKETS_HANDLED;
  1939. /* Process up to available RFD's */
  1940. while (count < limit) {
  1941. if (list_empty(&rx_ring->recv_list)) {
  1942. WARN_ON(rx_ring->num_ready_recv != 0);
  1943. done = false;
  1944. break;
  1945. }
  1946. rfd = nic_rx_pkts(adapter);
  1947. if (rfd == NULL)
  1948. break;
  1949. /* Do not receive any packets until a filter has been set.
  1950. * Do not receive any packets until we have link.
  1951. * If length is zero, return the RFD in order to advance the
  1952. * Free buffer ring.
  1953. */
  1954. if (!adapter->packet_filter ||
  1955. !netif_carrier_ok(adapter->netdev) ||
  1956. rfd->len == 0)
  1957. continue;
  1958. adapter->netdev->stats.rx_packets++;
  1959. if (rx_ring->num_ready_recv < RFD_LOW_WATER_MARK)
  1960. dev_warn(&adapter->pdev->dev, "RFD's are running out\n");
  1961. count++;
  1962. }
  1963. if (count == limit || !done) {
  1964. rx_ring->unfinished_receives = true;
  1965. writel(PARM_TX_TIME_INT_DEF * NANO_IN_A_MICRO,
  1966. &adapter->regs->global.watchdog_timer);
  1967. } else {
  1968. /* Watchdog timer will disable itself if appropriate. */
  1969. rx_ring->unfinished_receives = false;
  1970. }
  1971. return count;
  1972. }
  1973. /* et131x_tx_dma_memory_alloc
  1974. *
  1975. * Allocates memory that will be visible both to the device and to the CPU.
  1976. * The OS will pass us packets, pointers to which we will insert in the Tx
  1977. * Descriptor queue. The device will read this queue to find the packets in
  1978. * memory. The device will update the "status" in memory each time it xmits a
  1979. * packet.
  1980. */
  1981. static int et131x_tx_dma_memory_alloc(struct et131x_adapter *adapter)
  1982. {
  1983. int desc_size = 0;
  1984. struct tx_ring *tx_ring = &adapter->tx_ring;
  1985. /* Allocate memory for the TCB's (Transmit Control Block) */
  1986. tx_ring->tcb_ring = kcalloc(NUM_TCB, sizeof(struct tcb),
  1987. GFP_ATOMIC | GFP_DMA);
  1988. if (!tx_ring->tcb_ring)
  1989. return -ENOMEM;
  1990. desc_size = (sizeof(struct tx_desc) * NUM_DESC_PER_RING_TX);
  1991. tx_ring->tx_desc_ring = dma_alloc_coherent(&adapter->pdev->dev,
  1992. desc_size,
  1993. &tx_ring->tx_desc_ring_pa,
  1994. GFP_KERNEL);
  1995. if (!tx_ring->tx_desc_ring) {
  1996. dev_err(&adapter->pdev->dev,
  1997. "Cannot alloc memory for Tx Ring\n");
  1998. return -ENOMEM;
  1999. }
  2000. tx_ring->tx_status = dma_alloc_coherent(&adapter->pdev->dev,
  2001. sizeof(u32),
  2002. &tx_ring->tx_status_pa,
  2003. GFP_KERNEL);
  2004. if (!tx_ring->tx_status_pa) {
  2005. dev_err(&adapter->pdev->dev,
  2006. "Cannot alloc memory for Tx status block\n");
  2007. return -ENOMEM;
  2008. }
  2009. return 0;
  2010. }
  2011. static void et131x_tx_dma_memory_free(struct et131x_adapter *adapter)
  2012. {
  2013. int desc_size = 0;
  2014. struct tx_ring *tx_ring = &adapter->tx_ring;
  2015. if (tx_ring->tx_desc_ring) {
  2016. /* Free memory relating to Tx rings here */
  2017. desc_size = (sizeof(struct tx_desc) * NUM_DESC_PER_RING_TX);
  2018. dma_free_coherent(&adapter->pdev->dev,
  2019. desc_size,
  2020. tx_ring->tx_desc_ring,
  2021. tx_ring->tx_desc_ring_pa);
  2022. tx_ring->tx_desc_ring = NULL;
  2023. }
  2024. /* Free memory for the Tx status block */
  2025. if (tx_ring->tx_status) {
  2026. dma_free_coherent(&adapter->pdev->dev,
  2027. sizeof(u32),
  2028. tx_ring->tx_status,
  2029. tx_ring->tx_status_pa);
  2030. tx_ring->tx_status = NULL;
  2031. }
  2032. /* Free the memory for the tcb structures */
  2033. kfree(tx_ring->tcb_ring);
  2034. }
  2035. /* nic_send_packet - NIC specific send handler for version B silicon. */
  2036. static int nic_send_packet(struct et131x_adapter *adapter, struct tcb *tcb)
  2037. {
  2038. u32 i;
  2039. struct tx_desc desc[24];
  2040. u32 frag = 0;
  2041. u32 thiscopy, remainder;
  2042. struct sk_buff *skb = tcb->skb;
  2043. u32 nr_frags = skb_shinfo(skb)->nr_frags + 1;
  2044. struct skb_frag_struct *frags = &skb_shinfo(skb)->frags[0];
  2045. struct phy_device *phydev = adapter->phydev;
  2046. dma_addr_t dma_addr;
  2047. struct tx_ring *tx_ring = &adapter->tx_ring;
  2048. /* Part of the optimizations of this send routine restrict us to
  2049. * sending 24 fragments at a pass. In practice we should never see
  2050. * more than 5 fragments.
  2051. */
  2052. /* nr_frags should be no more than 18. */
  2053. BUILD_BUG_ON(MAX_SKB_FRAGS + 1 > 23);
  2054. memset(desc, 0, sizeof(struct tx_desc) * (nr_frags + 1));
  2055. for (i = 0; i < nr_frags; i++) {
  2056. /* If there is something in this element, lets get a
  2057. * descriptor from the ring and get the necessary data
  2058. */
  2059. if (i == 0) {
  2060. /* If the fragments are smaller than a standard MTU,
  2061. * then map them to a single descriptor in the Tx
  2062. * Desc ring. However, if they're larger, as is
  2063. * possible with support for jumbo packets, then
  2064. * split them each across 2 descriptors.
  2065. *
  2066. * This will work until we determine why the hardware
  2067. * doesn't seem to like large fragments.
  2068. */
  2069. if (skb_headlen(skb) <= 1514) {
  2070. /* Low 16bits are length, high is vlan and
  2071. * unused currently so zero
  2072. */
  2073. desc[frag].len_vlan = skb_headlen(skb);
  2074. dma_addr = dma_map_single(&adapter->pdev->dev,
  2075. skb->data,
  2076. skb_headlen(skb),
  2077. DMA_TO_DEVICE);
  2078. desc[frag].addr_lo = lower_32_bits(dma_addr);
  2079. desc[frag].addr_hi = upper_32_bits(dma_addr);
  2080. frag++;
  2081. } else {
  2082. desc[frag].len_vlan = skb_headlen(skb) / 2;
  2083. dma_addr = dma_map_single(&adapter->pdev->dev,
  2084. skb->data,
  2085. skb_headlen(skb) / 2,
  2086. DMA_TO_DEVICE);
  2087. desc[frag].addr_lo = lower_32_bits(dma_addr);
  2088. desc[frag].addr_hi = upper_32_bits(dma_addr);
  2089. frag++;
  2090. desc[frag].len_vlan = skb_headlen(skb) / 2;
  2091. dma_addr = dma_map_single(&adapter->pdev->dev,
  2092. skb->data +
  2093. skb_headlen(skb) / 2,
  2094. skb_headlen(skb) / 2,
  2095. DMA_TO_DEVICE);
  2096. desc[frag].addr_lo = lower_32_bits(dma_addr);
  2097. desc[frag].addr_hi = upper_32_bits(dma_addr);
  2098. frag++;
  2099. }
  2100. } else {
  2101. desc[frag].len_vlan = frags[i - 1].size;
  2102. dma_addr = skb_frag_dma_map(&adapter->pdev->dev,
  2103. &frags[i - 1],
  2104. 0,
  2105. frags[i - 1].size,
  2106. DMA_TO_DEVICE);
  2107. desc[frag].addr_lo = lower_32_bits(dma_addr);
  2108. desc[frag].addr_hi = upper_32_bits(dma_addr);
  2109. frag++;
  2110. }
  2111. }
  2112. if (phydev && phydev->speed == SPEED_1000) {
  2113. if (++tx_ring->since_irq == PARM_TX_NUM_BUFS_DEF) {
  2114. /* Last element & Interrupt flag */
  2115. desc[frag - 1].flags =
  2116. TXDESC_FLAG_INTPROC | TXDESC_FLAG_LASTPKT;
  2117. tx_ring->since_irq = 0;
  2118. } else { /* Last element */
  2119. desc[frag - 1].flags = TXDESC_FLAG_LASTPKT;
  2120. }
  2121. } else {
  2122. desc[frag - 1].flags =
  2123. TXDESC_FLAG_INTPROC | TXDESC_FLAG_LASTPKT;
  2124. }
  2125. desc[0].flags |= TXDESC_FLAG_FIRSTPKT;
  2126. tcb->index_start = tx_ring->send_idx;
  2127. tcb->stale = 0;
  2128. thiscopy = NUM_DESC_PER_RING_TX - INDEX10(tx_ring->send_idx);
  2129. if (thiscopy >= frag) {
  2130. remainder = 0;
  2131. thiscopy = frag;
  2132. } else {
  2133. remainder = frag - thiscopy;
  2134. }
  2135. memcpy(tx_ring->tx_desc_ring + INDEX10(tx_ring->send_idx),
  2136. desc,
  2137. sizeof(struct tx_desc) * thiscopy);
  2138. add_10bit(&tx_ring->send_idx, thiscopy);
  2139. if (INDEX10(tx_ring->send_idx) == 0 ||
  2140. INDEX10(tx_ring->send_idx) == NUM_DESC_PER_RING_TX) {
  2141. tx_ring->send_idx &= ~ET_DMA10_MASK;
  2142. tx_ring->send_idx ^= ET_DMA10_WRAP;
  2143. }
  2144. if (remainder) {
  2145. memcpy(tx_ring->tx_desc_ring,
  2146. desc + thiscopy,
  2147. sizeof(struct tx_desc) * remainder);
  2148. add_10bit(&tx_ring->send_idx, remainder);
  2149. }
  2150. if (INDEX10(tx_ring->send_idx) == 0) {
  2151. if (tx_ring->send_idx)
  2152. tcb->index = NUM_DESC_PER_RING_TX - 1;
  2153. else
  2154. tcb->index = ET_DMA10_WRAP|(NUM_DESC_PER_RING_TX - 1);
  2155. } else {
  2156. tcb->index = tx_ring->send_idx - 1;
  2157. }
  2158. spin_lock(&adapter->tcb_send_qlock);
  2159. if (tx_ring->send_tail)
  2160. tx_ring->send_tail->next = tcb;
  2161. else
  2162. tx_ring->send_head = tcb;
  2163. tx_ring->send_tail = tcb;
  2164. WARN_ON(tcb->next != NULL);
  2165. tx_ring->used++;
  2166. spin_unlock(&adapter->tcb_send_qlock);
  2167. /* Write the new write pointer back to the device. */
  2168. writel(tx_ring->send_idx, &adapter->regs->txdma.service_request);
  2169. /* For Gig only, we use Tx Interrupt coalescing. Enable the software
  2170. * timer to wake us up if this packet isn't followed by N more.
  2171. */
  2172. if (phydev && phydev->speed == SPEED_1000) {
  2173. writel(PARM_TX_TIME_INT_DEF * NANO_IN_A_MICRO,
  2174. &adapter->regs->global.watchdog_timer);
  2175. }
  2176. return 0;
  2177. }
  2178. static int send_packet(struct sk_buff *skb, struct et131x_adapter *adapter)
  2179. {
  2180. int status;
  2181. struct tcb *tcb;
  2182. unsigned long flags;
  2183. struct tx_ring *tx_ring = &adapter->tx_ring;
  2184. /* All packets must have at least a MAC address and a protocol type */
  2185. if (skb->len < ETH_HLEN)
  2186. return -EIO;
  2187. spin_lock_irqsave(&adapter->tcb_ready_qlock, flags);
  2188. tcb = tx_ring->tcb_qhead;
  2189. if (tcb == NULL) {
  2190. spin_unlock_irqrestore(&adapter->tcb_ready_qlock, flags);
  2191. return -ENOMEM;
  2192. }
  2193. tx_ring->tcb_qhead = tcb->next;
  2194. if (tx_ring->tcb_qhead == NULL)
  2195. tx_ring->tcb_qtail = NULL;
  2196. spin_unlock_irqrestore(&adapter->tcb_ready_qlock, flags);
  2197. tcb->skb = skb;
  2198. tcb->next = NULL;
  2199. status = nic_send_packet(adapter, tcb);
  2200. if (status != 0) {
  2201. spin_lock_irqsave(&adapter->tcb_ready_qlock, flags);
  2202. if (tx_ring->tcb_qtail)
  2203. tx_ring->tcb_qtail->next = tcb;
  2204. else
  2205. /* Apparently ready Q is empty. */
  2206. tx_ring->tcb_qhead = tcb;
  2207. tx_ring->tcb_qtail = tcb;
  2208. spin_unlock_irqrestore(&adapter->tcb_ready_qlock, flags);
  2209. return status;
  2210. }
  2211. WARN_ON(tx_ring->used > NUM_TCB);
  2212. return 0;
  2213. }
  2214. /* free_send_packet - Recycle a struct tcb */
  2215. static inline void free_send_packet(struct et131x_adapter *adapter,
  2216. struct tcb *tcb)
  2217. {
  2218. unsigned long flags;
  2219. struct tx_desc *desc = NULL;
  2220. struct net_device_stats *stats = &adapter->netdev->stats;
  2221. struct tx_ring *tx_ring = &adapter->tx_ring;
  2222. u64 dma_addr;
  2223. if (tcb->skb) {
  2224. stats->tx_bytes += tcb->skb->len;
  2225. /* Iterate through the TX descriptors on the ring
  2226. * corresponding to this packet and umap the fragments
  2227. * they point to
  2228. */
  2229. do {
  2230. desc = tx_ring->tx_desc_ring +
  2231. INDEX10(tcb->index_start);
  2232. dma_addr = desc->addr_lo;
  2233. dma_addr |= (u64)desc->addr_hi << 32;
  2234. dma_unmap_single(&adapter->pdev->dev,
  2235. dma_addr,
  2236. desc->len_vlan, DMA_TO_DEVICE);
  2237. add_10bit(&tcb->index_start, 1);
  2238. if (INDEX10(tcb->index_start) >=
  2239. NUM_DESC_PER_RING_TX) {
  2240. tcb->index_start &= ~ET_DMA10_MASK;
  2241. tcb->index_start ^= ET_DMA10_WRAP;
  2242. }
  2243. } while (desc != tx_ring->tx_desc_ring + INDEX10(tcb->index));
  2244. dev_kfree_skb_any(tcb->skb);
  2245. }
  2246. memset(tcb, 0, sizeof(struct tcb));
  2247. /* Add the TCB to the Ready Q */
  2248. spin_lock_irqsave(&adapter->tcb_ready_qlock, flags);
  2249. stats->tx_packets++;
  2250. if (tx_ring->tcb_qtail)
  2251. tx_ring->tcb_qtail->next = tcb;
  2252. else /* Apparently ready Q is empty. */
  2253. tx_ring->tcb_qhead = tcb;
  2254. tx_ring->tcb_qtail = tcb;
  2255. spin_unlock_irqrestore(&adapter->tcb_ready_qlock, flags);
  2256. WARN_ON(tx_ring->used < 0);
  2257. }
  2258. /* et131x_free_busy_send_packets - Free and complete the stopped active sends */
  2259. static void et131x_free_busy_send_packets(struct et131x_adapter *adapter)
  2260. {
  2261. struct tcb *tcb;
  2262. unsigned long flags;
  2263. u32 freed = 0;
  2264. struct tx_ring *tx_ring = &adapter->tx_ring;
  2265. /* Any packets being sent? Check the first TCB on the send list */
  2266. spin_lock_irqsave(&adapter->tcb_send_qlock, flags);
  2267. tcb = tx_ring->send_head;
  2268. while (tcb != NULL && freed < NUM_TCB) {
  2269. struct tcb *next = tcb->next;
  2270. tx_ring->send_head = next;
  2271. if (next == NULL)
  2272. tx_ring->send_tail = NULL;
  2273. tx_ring->used--;
  2274. spin_unlock_irqrestore(&adapter->tcb_send_qlock, flags);
  2275. freed++;
  2276. free_send_packet(adapter, tcb);
  2277. spin_lock_irqsave(&adapter->tcb_send_qlock, flags);
  2278. tcb = tx_ring->send_head;
  2279. }
  2280. WARN_ON(freed == NUM_TCB);
  2281. spin_unlock_irqrestore(&adapter->tcb_send_qlock, flags);
  2282. tx_ring->used = 0;
  2283. }
  2284. /* et131x_handle_send_pkts
  2285. *
  2286. * Re-claim the send resources, complete sends and get more to send from
  2287. * the send wait queue.
  2288. */
  2289. static void et131x_handle_send_pkts(struct et131x_adapter *adapter)
  2290. {
  2291. unsigned long flags;
  2292. u32 serviced;
  2293. struct tcb *tcb;
  2294. u32 index;
  2295. struct tx_ring *tx_ring = &adapter->tx_ring;
  2296. serviced = readl(&adapter->regs->txdma.new_service_complete);
  2297. index = INDEX10(serviced);
  2298. /* Has the ring wrapped? Process any descriptors that do not have
  2299. * the same "wrap" indicator as the current completion indicator
  2300. */
  2301. spin_lock_irqsave(&adapter->tcb_send_qlock, flags);
  2302. tcb = tx_ring->send_head;
  2303. while (tcb &&
  2304. ((serviced ^ tcb->index) & ET_DMA10_WRAP) &&
  2305. index < INDEX10(tcb->index)) {
  2306. tx_ring->used--;
  2307. tx_ring->send_head = tcb->next;
  2308. if (tcb->next == NULL)
  2309. tx_ring->send_tail = NULL;
  2310. spin_unlock_irqrestore(&adapter->tcb_send_qlock, flags);
  2311. free_send_packet(adapter, tcb);
  2312. spin_lock_irqsave(&adapter->tcb_send_qlock, flags);
  2313. /* Goto the next packet */
  2314. tcb = tx_ring->send_head;
  2315. }
  2316. while (tcb &&
  2317. !((serviced ^ tcb->index) & ET_DMA10_WRAP) &&
  2318. index > (tcb->index & ET_DMA10_MASK)) {
  2319. tx_ring->used--;
  2320. tx_ring->send_head = tcb->next;
  2321. if (tcb->next == NULL)
  2322. tx_ring->send_tail = NULL;
  2323. spin_unlock_irqrestore(&adapter->tcb_send_qlock, flags);
  2324. free_send_packet(adapter, tcb);
  2325. spin_lock_irqsave(&adapter->tcb_send_qlock, flags);
  2326. /* Goto the next packet */
  2327. tcb = tx_ring->send_head;
  2328. }
  2329. /* Wake up the queue when we hit a low-water mark */
  2330. if (tx_ring->used <= NUM_TCB / 3)
  2331. netif_wake_queue(adapter->netdev);
  2332. spin_unlock_irqrestore(&adapter->tcb_send_qlock, flags);
  2333. }
  2334. static int et131x_get_settings(struct net_device *netdev,
  2335. struct ethtool_cmd *cmd)
  2336. {
  2337. struct et131x_adapter *adapter = netdev_priv(netdev);
  2338. return phy_ethtool_gset(adapter->phydev, cmd);
  2339. }
  2340. static int et131x_set_settings(struct net_device *netdev,
  2341. struct ethtool_cmd *cmd)
  2342. {
  2343. struct et131x_adapter *adapter = netdev_priv(netdev);
  2344. return phy_ethtool_sset(adapter->phydev, cmd);
  2345. }
  2346. static int et131x_get_regs_len(struct net_device *netdev)
  2347. {
  2348. #define ET131X_REGS_LEN 256
  2349. return ET131X_REGS_LEN * sizeof(u32);
  2350. }
  2351. static void et131x_get_regs(struct net_device *netdev,
  2352. struct ethtool_regs *regs, void *regs_data)
  2353. {
  2354. struct et131x_adapter *adapter = netdev_priv(netdev);
  2355. struct address_map __iomem *aregs = adapter->regs;
  2356. u32 *regs_buff = regs_data;
  2357. u32 num = 0;
  2358. u16 tmp;
  2359. memset(regs_data, 0, et131x_get_regs_len(netdev));
  2360. regs->version = (1 << 24) | (adapter->pdev->revision << 16) |
  2361. adapter->pdev->device;
  2362. /* PHY regs */
  2363. et131x_mii_read(adapter, MII_BMCR, &tmp);
  2364. regs_buff[num++] = tmp;
  2365. et131x_mii_read(adapter, MII_BMSR, &tmp);
  2366. regs_buff[num++] = tmp;
  2367. et131x_mii_read(adapter, MII_PHYSID1, &tmp);
  2368. regs_buff[num++] = tmp;
  2369. et131x_mii_read(adapter, MII_PHYSID2, &tmp);
  2370. regs_buff[num++] = tmp;
  2371. et131x_mii_read(adapter, MII_ADVERTISE, &tmp);
  2372. regs_buff[num++] = tmp;
  2373. et131x_mii_read(adapter, MII_LPA, &tmp);
  2374. regs_buff[num++] = tmp;
  2375. et131x_mii_read(adapter, MII_EXPANSION, &tmp);
  2376. regs_buff[num++] = tmp;
  2377. /* Autoneg next page transmit reg */
  2378. et131x_mii_read(adapter, 0x07, &tmp);
  2379. regs_buff[num++] = tmp;
  2380. /* Link partner next page reg */
  2381. et131x_mii_read(adapter, 0x08, &tmp);
  2382. regs_buff[num++] = tmp;
  2383. et131x_mii_read(adapter, MII_CTRL1000, &tmp);
  2384. regs_buff[num++] = tmp;
  2385. et131x_mii_read(adapter, MII_STAT1000, &tmp);
  2386. regs_buff[num++] = tmp;
  2387. et131x_mii_read(adapter, 0x0b, &tmp);
  2388. regs_buff[num++] = tmp;
  2389. et131x_mii_read(adapter, 0x0c, &tmp);
  2390. regs_buff[num++] = tmp;
  2391. et131x_mii_read(adapter, MII_MMD_CTRL, &tmp);
  2392. regs_buff[num++] = tmp;
  2393. et131x_mii_read(adapter, MII_MMD_DATA, &tmp);
  2394. regs_buff[num++] = tmp;
  2395. et131x_mii_read(adapter, MII_ESTATUS, &tmp);
  2396. regs_buff[num++] = tmp;
  2397. et131x_mii_read(adapter, PHY_INDEX_REG, &tmp);
  2398. regs_buff[num++] = tmp;
  2399. et131x_mii_read(adapter, PHY_DATA_REG, &tmp);
  2400. regs_buff[num++] = tmp;
  2401. et131x_mii_read(adapter, PHY_MPHY_CONTROL_REG, &tmp);
  2402. regs_buff[num++] = tmp;
  2403. et131x_mii_read(adapter, PHY_LOOPBACK_CONTROL, &tmp);
  2404. regs_buff[num++] = tmp;
  2405. et131x_mii_read(adapter, PHY_LOOPBACK_CONTROL + 1, &tmp);
  2406. regs_buff[num++] = tmp;
  2407. et131x_mii_read(adapter, PHY_REGISTER_MGMT_CONTROL, &tmp);
  2408. regs_buff[num++] = tmp;
  2409. et131x_mii_read(adapter, PHY_CONFIG, &tmp);
  2410. regs_buff[num++] = tmp;
  2411. et131x_mii_read(adapter, PHY_PHY_CONTROL, &tmp);
  2412. regs_buff[num++] = tmp;
  2413. et131x_mii_read(adapter, PHY_INTERRUPT_MASK, &tmp);
  2414. regs_buff[num++] = tmp;
  2415. et131x_mii_read(adapter, PHY_INTERRUPT_STATUS, &tmp);
  2416. regs_buff[num++] = tmp;
  2417. et131x_mii_read(adapter, PHY_PHY_STATUS, &tmp);
  2418. regs_buff[num++] = tmp;
  2419. et131x_mii_read(adapter, PHY_LED_1, &tmp);
  2420. regs_buff[num++] = tmp;
  2421. et131x_mii_read(adapter, PHY_LED_2, &tmp);
  2422. regs_buff[num++] = tmp;
  2423. /* Global regs */
  2424. regs_buff[num++] = readl(&aregs->global.txq_start_addr);
  2425. regs_buff[num++] = readl(&aregs->global.txq_end_addr);
  2426. regs_buff[num++] = readl(&aregs->global.rxq_start_addr);
  2427. regs_buff[num++] = readl(&aregs->global.rxq_end_addr);
  2428. regs_buff[num++] = readl(&aregs->global.pm_csr);
  2429. regs_buff[num++] = adapter->stats.interrupt_status;
  2430. regs_buff[num++] = readl(&aregs->global.int_mask);
  2431. regs_buff[num++] = readl(&aregs->global.int_alias_clr_en);
  2432. regs_buff[num++] = readl(&aregs->global.int_status_alias);
  2433. regs_buff[num++] = readl(&aregs->global.sw_reset);
  2434. regs_buff[num++] = readl(&aregs->global.slv_timer);
  2435. regs_buff[num++] = readl(&aregs->global.msi_config);
  2436. regs_buff[num++] = readl(&aregs->global.loopback);
  2437. regs_buff[num++] = readl(&aregs->global.watchdog_timer);
  2438. /* TXDMA regs */
  2439. regs_buff[num++] = readl(&aregs->txdma.csr);
  2440. regs_buff[num++] = readl(&aregs->txdma.pr_base_hi);
  2441. regs_buff[num++] = readl(&aregs->txdma.pr_base_lo);
  2442. regs_buff[num++] = readl(&aregs->txdma.pr_num_des);
  2443. regs_buff[num++] = readl(&aregs->txdma.txq_wr_addr);
  2444. regs_buff[num++] = readl(&aregs->txdma.txq_wr_addr_ext);
  2445. regs_buff[num++] = readl(&aregs->txdma.txq_rd_addr);
  2446. regs_buff[num++] = readl(&aregs->txdma.dma_wb_base_hi);
  2447. regs_buff[num++] = readl(&aregs->txdma.dma_wb_base_lo);
  2448. regs_buff[num++] = readl(&aregs->txdma.service_request);
  2449. regs_buff[num++] = readl(&aregs->txdma.service_complete);
  2450. regs_buff[num++] = readl(&aregs->txdma.cache_rd_index);
  2451. regs_buff[num++] = readl(&aregs->txdma.cache_wr_index);
  2452. regs_buff[num++] = readl(&aregs->txdma.tx_dma_error);
  2453. regs_buff[num++] = readl(&aregs->txdma.desc_abort_cnt);
  2454. regs_buff[num++] = readl(&aregs->txdma.payload_abort_cnt);
  2455. regs_buff[num++] = readl(&aregs->txdma.writeback_abort_cnt);
  2456. regs_buff[num++] = readl(&aregs->txdma.desc_timeout_cnt);
  2457. regs_buff[num++] = readl(&aregs->txdma.payload_timeout_cnt);
  2458. regs_buff[num++] = readl(&aregs->txdma.writeback_timeout_cnt);
  2459. regs_buff[num++] = readl(&aregs->txdma.desc_error_cnt);
  2460. regs_buff[num++] = readl(&aregs->txdma.payload_error_cnt);
  2461. regs_buff[num++] = readl(&aregs->txdma.writeback_error_cnt);
  2462. regs_buff[num++] = readl(&aregs->txdma.dropped_tlp_cnt);
  2463. regs_buff[num++] = readl(&aregs->txdma.new_service_complete);
  2464. regs_buff[num++] = readl(&aregs->txdma.ethernet_packet_cnt);
  2465. /* RXDMA regs */
  2466. regs_buff[num++] = readl(&aregs->rxdma.csr);
  2467. regs_buff[num++] = readl(&aregs->rxdma.dma_wb_base_hi);
  2468. regs_buff[num++] = readl(&aregs->rxdma.dma_wb_base_lo);
  2469. regs_buff[num++] = readl(&aregs->rxdma.num_pkt_done);
  2470. regs_buff[num++] = readl(&aregs->rxdma.max_pkt_time);
  2471. regs_buff[num++] = readl(&aregs->rxdma.rxq_rd_addr);
  2472. regs_buff[num++] = readl(&aregs->rxdma.rxq_rd_addr_ext);
  2473. regs_buff[num++] = readl(&aregs->rxdma.rxq_wr_addr);
  2474. regs_buff[num++] = readl(&aregs->rxdma.psr_base_hi);
  2475. regs_buff[num++] = readl(&aregs->rxdma.psr_base_lo);
  2476. regs_buff[num++] = readl(&aregs->rxdma.psr_num_des);
  2477. regs_buff[num++] = readl(&aregs->rxdma.psr_avail_offset);
  2478. regs_buff[num++] = readl(&aregs->rxdma.psr_full_offset);
  2479. regs_buff[num++] = readl(&aregs->rxdma.psr_access_index);
  2480. regs_buff[num++] = readl(&aregs->rxdma.psr_min_des);
  2481. regs_buff[num++] = readl(&aregs->rxdma.fbr0_base_lo);
  2482. regs_buff[num++] = readl(&aregs->rxdma.fbr0_base_hi);
  2483. regs_buff[num++] = readl(&aregs->rxdma.fbr0_num_des);
  2484. regs_buff[num++] = readl(&aregs->rxdma.fbr0_avail_offset);
  2485. regs_buff[num++] = readl(&aregs->rxdma.fbr0_full_offset);
  2486. regs_buff[num++] = readl(&aregs->rxdma.fbr0_rd_index);
  2487. regs_buff[num++] = readl(&aregs->rxdma.fbr0_min_des);
  2488. regs_buff[num++] = readl(&aregs->rxdma.fbr1_base_lo);
  2489. regs_buff[num++] = readl(&aregs->rxdma.fbr1_base_hi);
  2490. regs_buff[num++] = readl(&aregs->rxdma.fbr1_num_des);
  2491. regs_buff[num++] = readl(&aregs->rxdma.fbr1_avail_offset);
  2492. regs_buff[num++] = readl(&aregs->rxdma.fbr1_full_offset);
  2493. regs_buff[num++] = readl(&aregs->rxdma.fbr1_rd_index);
  2494. regs_buff[num++] = readl(&aregs->rxdma.fbr1_min_des);
  2495. }
  2496. static void et131x_get_drvinfo(struct net_device *netdev,
  2497. struct ethtool_drvinfo *info)
  2498. {
  2499. struct et131x_adapter *adapter = netdev_priv(netdev);
  2500. strlcpy(info->driver, DRIVER_NAME, sizeof(info->driver));
  2501. strlcpy(info->version, DRIVER_VERSION, sizeof(info->version));
  2502. strlcpy(info->bus_info, pci_name(adapter->pdev),
  2503. sizeof(info->bus_info));
  2504. }
  2505. static struct ethtool_ops et131x_ethtool_ops = {
  2506. .get_settings = et131x_get_settings,
  2507. .set_settings = et131x_set_settings,
  2508. .get_drvinfo = et131x_get_drvinfo,
  2509. .get_regs_len = et131x_get_regs_len,
  2510. .get_regs = et131x_get_regs,
  2511. .get_link = ethtool_op_get_link,
  2512. };
  2513. /* et131x_hwaddr_init - set up the MAC Address */
  2514. static void et131x_hwaddr_init(struct et131x_adapter *adapter)
  2515. {
  2516. /* If have our default mac from init and no mac address from
  2517. * EEPROM then we need to generate the last octet and set it on the
  2518. * device
  2519. */
  2520. if (is_zero_ether_addr(adapter->rom_addr)) {
  2521. /* We need to randomly generate the last octet so we
  2522. * decrease our chances of setting the mac address to
  2523. * same as another one of our cards in the system
  2524. */
  2525. get_random_bytes(&adapter->addr[5], 1);
  2526. /* We have the default value in the register we are
  2527. * working with so we need to copy the current
  2528. * address into the permanent address
  2529. */
  2530. ether_addr_copy(adapter->rom_addr, adapter->addr);
  2531. } else {
  2532. /* We do not have an override address, so set the
  2533. * current address to the permanent address and add
  2534. * it to the device
  2535. */
  2536. ether_addr_copy(adapter->addr, adapter->rom_addr);
  2537. }
  2538. }
  2539. static int et131x_pci_init(struct et131x_adapter *adapter,
  2540. struct pci_dev *pdev)
  2541. {
  2542. u16 max_payload;
  2543. int i, rc;
  2544. rc = et131x_init_eeprom(adapter);
  2545. if (rc < 0)
  2546. goto out;
  2547. if (!pci_is_pcie(pdev)) {
  2548. dev_err(&pdev->dev, "Missing PCIe capabilities\n");
  2549. goto err_out;
  2550. }
  2551. /* Program the Ack/Nak latency and replay timers */
  2552. max_payload = pdev->pcie_mpss;
  2553. if (max_payload < 2) {
  2554. static const u16 acknak[2] = { 0x76, 0xD0 };
  2555. static const u16 replay[2] = { 0x1E0, 0x2ED };
  2556. if (pci_write_config_word(pdev, ET1310_PCI_ACK_NACK,
  2557. acknak[max_payload])) {
  2558. dev_err(&pdev->dev,
  2559. "Could not write PCI config space for ACK/NAK\n");
  2560. goto err_out;
  2561. }
  2562. if (pci_write_config_word(pdev, ET1310_PCI_REPLAY,
  2563. replay[max_payload])) {
  2564. dev_err(&pdev->dev,
  2565. "Could not write PCI config space for Replay Timer\n");
  2566. goto err_out;
  2567. }
  2568. }
  2569. /* l0s and l1 latency timers. We are using default values.
  2570. * Representing 001 for L0s and 010 for L1
  2571. */
  2572. if (pci_write_config_byte(pdev, ET1310_PCI_L0L1LATENCY, 0x11)) {
  2573. dev_err(&pdev->dev,
  2574. "Could not write PCI config space for Latency Timers\n");
  2575. goto err_out;
  2576. }
  2577. /* Change the max read size to 2k */
  2578. if (pcie_set_readrq(pdev, 2048)) {
  2579. dev_err(&pdev->dev,
  2580. "Couldn't change PCI config space for Max read size\n");
  2581. goto err_out;
  2582. }
  2583. /* Get MAC address from config space if an eeprom exists, otherwise
  2584. * the MAC address there will not be valid
  2585. */
  2586. if (!adapter->has_eeprom) {
  2587. et131x_hwaddr_init(adapter);
  2588. return 0;
  2589. }
  2590. for (i = 0; i < ETH_ALEN; i++) {
  2591. if (pci_read_config_byte(pdev, ET1310_PCI_MAC_ADDRESS + i,
  2592. adapter->rom_addr + i)) {
  2593. dev_err(&pdev->dev, "Could not read PCI config space for MAC address\n");
  2594. goto err_out;
  2595. }
  2596. }
  2597. ether_addr_copy(adapter->addr, adapter->rom_addr);
  2598. out:
  2599. return rc;
  2600. err_out:
  2601. rc = -EIO;
  2602. goto out;
  2603. }
  2604. /* et131x_error_timer_handler
  2605. * @data: timer-specific variable; here a pointer to our adapter structure
  2606. *
  2607. * The routine called when the error timer expires, to track the number of
  2608. * recurring errors.
  2609. */
  2610. static void et131x_error_timer_handler(unsigned long data)
  2611. {
  2612. struct et131x_adapter *adapter = (struct et131x_adapter *)data;
  2613. struct phy_device *phydev = adapter->phydev;
  2614. if (et1310_in_phy_coma(adapter)) {
  2615. /* Bring the device immediately out of coma, to
  2616. * prevent it from sleeping indefinitely, this
  2617. * mechanism could be improved!
  2618. */
  2619. et1310_disable_phy_coma(adapter);
  2620. adapter->boot_coma = 20;
  2621. } else {
  2622. et1310_update_macstat_host_counters(adapter);
  2623. }
  2624. if (!phydev->link && adapter->boot_coma < 11)
  2625. adapter->boot_coma++;
  2626. if (adapter->boot_coma == 10) {
  2627. if (!phydev->link) {
  2628. if (!et1310_in_phy_coma(adapter)) {
  2629. /* NOTE - This was originally a 'sync with
  2630. * interrupt'. How to do that under Linux?
  2631. */
  2632. et131x_enable_interrupts(adapter);
  2633. et1310_enable_phy_coma(adapter);
  2634. }
  2635. }
  2636. }
  2637. /* This is a periodic timer, so reschedule */
  2638. mod_timer(&adapter->error_timer, jiffies + TX_ERROR_PERIOD * HZ / 1000);
  2639. }
  2640. static void et131x_adapter_memory_free(struct et131x_adapter *adapter)
  2641. {
  2642. et131x_tx_dma_memory_free(adapter);
  2643. et131x_rx_dma_memory_free(adapter);
  2644. }
  2645. static int et131x_adapter_memory_alloc(struct et131x_adapter *adapter)
  2646. {
  2647. int status;
  2648. status = et131x_tx_dma_memory_alloc(adapter);
  2649. if (status) {
  2650. dev_err(&adapter->pdev->dev,
  2651. "et131x_tx_dma_memory_alloc FAILED\n");
  2652. et131x_tx_dma_memory_free(adapter);
  2653. return status;
  2654. }
  2655. status = et131x_rx_dma_memory_alloc(adapter);
  2656. if (status) {
  2657. dev_err(&adapter->pdev->dev,
  2658. "et131x_rx_dma_memory_alloc FAILED\n");
  2659. et131x_adapter_memory_free(adapter);
  2660. return status;
  2661. }
  2662. status = et131x_init_recv(adapter);
  2663. if (status) {
  2664. dev_err(&adapter->pdev->dev, "et131x_init_recv FAILED\n");
  2665. et131x_adapter_memory_free(adapter);
  2666. }
  2667. return status;
  2668. }
  2669. static void et131x_adjust_link(struct net_device *netdev)
  2670. {
  2671. struct et131x_adapter *adapter = netdev_priv(netdev);
  2672. struct phy_device *phydev = adapter->phydev;
  2673. if (!phydev)
  2674. return;
  2675. if (phydev->link == adapter->link)
  2676. return;
  2677. /* Check to see if we are in coma mode and if
  2678. * so, disable it because we will not be able
  2679. * to read PHY values until we are out.
  2680. */
  2681. if (et1310_in_phy_coma(adapter))
  2682. et1310_disable_phy_coma(adapter);
  2683. adapter->link = phydev->link;
  2684. phy_print_status(phydev);
  2685. if (phydev->link) {
  2686. adapter->boot_coma = 20;
  2687. if (phydev->speed == SPEED_10) {
  2688. u16 register18;
  2689. et131x_mii_read(adapter, PHY_MPHY_CONTROL_REG,
  2690. &register18);
  2691. et131x_mii_write(adapter, phydev->addr,
  2692. PHY_MPHY_CONTROL_REG,
  2693. register18 | 0x4);
  2694. et131x_mii_write(adapter, phydev->addr, PHY_INDEX_REG,
  2695. register18 | 0x8402);
  2696. et131x_mii_write(adapter, phydev->addr, PHY_DATA_REG,
  2697. register18 | 511);
  2698. et131x_mii_write(adapter, phydev->addr,
  2699. PHY_MPHY_CONTROL_REG, register18);
  2700. }
  2701. et1310_config_flow_control(adapter);
  2702. if (phydev->speed == SPEED_1000 &&
  2703. adapter->registry_jumbo_packet > 2048) {
  2704. u16 reg;
  2705. et131x_mii_read(adapter, PHY_CONFIG, &reg);
  2706. reg &= ~ET_PHY_CONFIG_TX_FIFO_DEPTH;
  2707. reg |= ET_PHY_CONFIG_FIFO_DEPTH_32;
  2708. et131x_mii_write(adapter, phydev->addr, PHY_CONFIG,
  2709. reg);
  2710. }
  2711. et131x_set_rx_dma_timer(adapter);
  2712. et1310_config_mac_regs2(adapter);
  2713. } else {
  2714. adapter->boot_coma = 0;
  2715. if (phydev->speed == SPEED_10) {
  2716. u16 register18;
  2717. et131x_mii_read(adapter, PHY_MPHY_CONTROL_REG,
  2718. &register18);
  2719. et131x_mii_write(adapter, phydev->addr,
  2720. PHY_MPHY_CONTROL_REG,
  2721. register18 | 0x4);
  2722. et131x_mii_write(adapter, phydev->addr,
  2723. PHY_INDEX_REG, register18 | 0x8402);
  2724. et131x_mii_write(adapter, phydev->addr,
  2725. PHY_DATA_REG, register18 | 511);
  2726. et131x_mii_write(adapter, phydev->addr,
  2727. PHY_MPHY_CONTROL_REG, register18);
  2728. }
  2729. et131x_free_busy_send_packets(adapter);
  2730. et131x_init_send(adapter);
  2731. /* Bring the device back to the state it was during
  2732. * init prior to autonegotiation being complete. This
  2733. * way, when we get the auto-neg complete interrupt,
  2734. * we can complete init by calling config_mac_regs2.
  2735. */
  2736. et131x_soft_reset(adapter);
  2737. et131x_adapter_setup(adapter);
  2738. et131x_disable_txrx(netdev);
  2739. et131x_enable_txrx(netdev);
  2740. }
  2741. }
  2742. static int et131x_mii_probe(struct net_device *netdev)
  2743. {
  2744. struct et131x_adapter *adapter = netdev_priv(netdev);
  2745. struct phy_device *phydev = NULL;
  2746. phydev = phy_find_first(adapter->mii_bus);
  2747. if (!phydev) {
  2748. dev_err(&adapter->pdev->dev, "no PHY found\n");
  2749. return -ENODEV;
  2750. }
  2751. phydev = phy_connect(netdev, dev_name(&phydev->dev),
  2752. &et131x_adjust_link, PHY_INTERFACE_MODE_MII);
  2753. if (IS_ERR(phydev)) {
  2754. dev_err(&adapter->pdev->dev, "Could not attach to PHY\n");
  2755. return PTR_ERR(phydev);
  2756. }
  2757. phydev->supported &= (SUPPORTED_10baseT_Half |
  2758. SUPPORTED_10baseT_Full |
  2759. SUPPORTED_100baseT_Half |
  2760. SUPPORTED_100baseT_Full |
  2761. SUPPORTED_Autoneg |
  2762. SUPPORTED_MII |
  2763. SUPPORTED_TP);
  2764. if (adapter->pdev->device != ET131X_PCI_DEVICE_ID_FAST)
  2765. phydev->supported |= SUPPORTED_1000baseT_Half |
  2766. SUPPORTED_1000baseT_Full;
  2767. phydev->advertising = phydev->supported;
  2768. phydev->autoneg = AUTONEG_ENABLE;
  2769. adapter->phydev = phydev;
  2770. dev_info(&adapter->pdev->dev,
  2771. "attached PHY driver [%s] (mii_bus:phy_addr=%s)\n",
  2772. phydev->drv->name, dev_name(&phydev->dev));
  2773. return 0;
  2774. }
  2775. static struct et131x_adapter *et131x_adapter_init(struct net_device *netdev,
  2776. struct pci_dev *pdev)
  2777. {
  2778. static const u8 default_mac[] = { 0x00, 0x05, 0x3d, 0x00, 0x02, 0x00 };
  2779. struct et131x_adapter *adapter;
  2780. adapter = netdev_priv(netdev);
  2781. adapter->pdev = pci_dev_get(pdev);
  2782. adapter->netdev = netdev;
  2783. spin_lock_init(&adapter->tcb_send_qlock);
  2784. spin_lock_init(&adapter->tcb_ready_qlock);
  2785. spin_lock_init(&adapter->rcv_lock);
  2786. adapter->registry_jumbo_packet = 1514; /* 1514-9216 */
  2787. ether_addr_copy(adapter->addr, default_mac);
  2788. return adapter;
  2789. }
  2790. static void et131x_pci_remove(struct pci_dev *pdev)
  2791. {
  2792. struct net_device *netdev = pci_get_drvdata(pdev);
  2793. struct et131x_adapter *adapter = netdev_priv(netdev);
  2794. unregister_netdev(netdev);
  2795. netif_napi_del(&adapter->napi);
  2796. phy_disconnect(adapter->phydev);
  2797. mdiobus_unregister(adapter->mii_bus);
  2798. kfree(adapter->mii_bus->irq);
  2799. mdiobus_free(adapter->mii_bus);
  2800. et131x_adapter_memory_free(adapter);
  2801. iounmap(adapter->regs);
  2802. pci_dev_put(pdev);
  2803. free_netdev(netdev);
  2804. pci_release_regions(pdev);
  2805. pci_disable_device(pdev);
  2806. }
  2807. static void et131x_up(struct net_device *netdev)
  2808. {
  2809. struct et131x_adapter *adapter = netdev_priv(netdev);
  2810. et131x_enable_txrx(netdev);
  2811. phy_start(adapter->phydev);
  2812. }
  2813. static void et131x_down(struct net_device *netdev)
  2814. {
  2815. struct et131x_adapter *adapter = netdev_priv(netdev);
  2816. /* Save the timestamp for the TX watchdog, prevent a timeout */
  2817. netdev->trans_start = jiffies;
  2818. phy_stop(adapter->phydev);
  2819. et131x_disable_txrx(netdev);
  2820. }
  2821. #ifdef CONFIG_PM_SLEEP
  2822. static int et131x_suspend(struct device *dev)
  2823. {
  2824. struct pci_dev *pdev = to_pci_dev(dev);
  2825. struct net_device *netdev = pci_get_drvdata(pdev);
  2826. if (netif_running(netdev)) {
  2827. netif_device_detach(netdev);
  2828. et131x_down(netdev);
  2829. pci_save_state(pdev);
  2830. }
  2831. return 0;
  2832. }
  2833. static int et131x_resume(struct device *dev)
  2834. {
  2835. struct pci_dev *pdev = to_pci_dev(dev);
  2836. struct net_device *netdev = pci_get_drvdata(pdev);
  2837. if (netif_running(netdev)) {
  2838. pci_restore_state(pdev);
  2839. et131x_up(netdev);
  2840. netif_device_attach(netdev);
  2841. }
  2842. return 0;
  2843. }
  2844. #endif
  2845. static SIMPLE_DEV_PM_OPS(et131x_pm_ops, et131x_suspend, et131x_resume);
  2846. static irqreturn_t et131x_isr(int irq, void *dev_id)
  2847. {
  2848. bool handled = true;
  2849. bool enable_interrupts = true;
  2850. struct net_device *netdev = dev_id;
  2851. struct et131x_adapter *adapter = netdev_priv(netdev);
  2852. struct address_map __iomem *iomem = adapter->regs;
  2853. struct rx_ring *rx_ring = &adapter->rx_ring;
  2854. struct tx_ring *tx_ring = &adapter->tx_ring;
  2855. u32 status;
  2856. if (!netif_device_present(netdev)) {
  2857. handled = false;
  2858. enable_interrupts = false;
  2859. goto out;
  2860. }
  2861. et131x_disable_interrupts(adapter);
  2862. status = readl(&adapter->regs->global.int_status);
  2863. if (adapter->flow == FLOW_TXONLY || adapter->flow == FLOW_BOTH)
  2864. status &= ~INT_MASK_ENABLE;
  2865. else
  2866. status &= ~INT_MASK_ENABLE_NO_FLOW;
  2867. /* Make sure this is our interrupt */
  2868. if (!status) {
  2869. handled = false;
  2870. et131x_enable_interrupts(adapter);
  2871. goto out;
  2872. }
  2873. /* This is our interrupt, so process accordingly */
  2874. if (status & ET_INTR_WATCHDOG) {
  2875. struct tcb *tcb = tx_ring->send_head;
  2876. if (tcb)
  2877. if (++tcb->stale > 1)
  2878. status |= ET_INTR_TXDMA_ISR;
  2879. if (rx_ring->unfinished_receives)
  2880. status |= ET_INTR_RXDMA_XFR_DONE;
  2881. else if (tcb == NULL)
  2882. writel(0, &adapter->regs->global.watchdog_timer);
  2883. status &= ~ET_INTR_WATCHDOG;
  2884. }
  2885. if (status & (ET_INTR_RXDMA_XFR_DONE | ET_INTR_TXDMA_ISR)) {
  2886. enable_interrupts = false;
  2887. napi_schedule(&adapter->napi);
  2888. }
  2889. status &= ~(ET_INTR_TXDMA_ISR | ET_INTR_RXDMA_XFR_DONE);
  2890. if (!status)
  2891. goto out;
  2892. if (status & ET_INTR_TXDMA_ERR) {
  2893. /* Following read also clears the register (COR) */
  2894. u32 txdma_err = readl(&iomem->txdma.tx_dma_error);
  2895. dev_warn(&adapter->pdev->dev,
  2896. "TXDMA_ERR interrupt, error = %d\n",
  2897. txdma_err);
  2898. }
  2899. if (status & (ET_INTR_RXDMA_FB_R0_LOW | ET_INTR_RXDMA_FB_R1_LOW)) {
  2900. /* This indicates the number of unused buffers in RXDMA free
  2901. * buffer ring 0 is <= the limit you programmed. Free buffer
  2902. * resources need to be returned. Free buffers are consumed as
  2903. * packets are passed from the network to the host. The host
  2904. * becomes aware of the packets from the contents of the packet
  2905. * status ring. This ring is queried when the packet done
  2906. * interrupt occurs. Packets are then passed to the OS. When
  2907. * the OS is done with the packets the resources can be
  2908. * returned to the ET1310 for re-use. This interrupt is one
  2909. * method of returning resources.
  2910. */
  2911. /* If the user has flow control on, then we will
  2912. * send a pause packet, otherwise just exit
  2913. */
  2914. if (adapter->flow == FLOW_TXONLY || adapter->flow == FLOW_BOTH) {
  2915. u32 pm_csr;
  2916. /* Tell the device to send a pause packet via the back
  2917. * pressure register (bp req and bp xon/xoff)
  2918. */
  2919. pm_csr = readl(&iomem->global.pm_csr);
  2920. if (!et1310_in_phy_coma(adapter))
  2921. writel(3, &iomem->txmac.bp_ctrl);
  2922. }
  2923. }
  2924. /* Handle Packet Status Ring Low Interrupt */
  2925. if (status & ET_INTR_RXDMA_STAT_LOW) {
  2926. /* Same idea as with the two Free Buffer Rings. Packets going
  2927. * from the network to the host each consume a free buffer
  2928. * resource and a packet status resource. These resources are
  2929. * passed to the OS. When the OS is done with the resources,
  2930. * they need to be returned to the ET1310. This is one method
  2931. * of returning the resources.
  2932. */
  2933. }
  2934. if (status & ET_INTR_RXDMA_ERR) {
  2935. /* The rxdma_error interrupt is sent when a time-out on a
  2936. * request issued by the JAGCore has occurred or a completion is
  2937. * returned with an un-successful status. In both cases the
  2938. * request is considered complete. The JAGCore will
  2939. * automatically re-try the request in question. Normally
  2940. * information on events like these are sent to the host using
  2941. * the "Advanced Error Reporting" capability. This interrupt is
  2942. * another way of getting similar information. The only thing
  2943. * required is to clear the interrupt by reading the ISR in the
  2944. * global resources. The JAGCore will do a re-try on the
  2945. * request. Normally you should never see this interrupt. If
  2946. * you start to see this interrupt occurring frequently then
  2947. * something bad has occurred. A reset might be the thing to do.
  2948. */
  2949. /* TRAP();*/
  2950. dev_warn(&adapter->pdev->dev, "RxDMA_ERR interrupt, error %x\n",
  2951. readl(&iomem->txmac.tx_test));
  2952. }
  2953. /* Handle the Wake on LAN Event */
  2954. if (status & ET_INTR_WOL) {
  2955. /* This is a secondary interrupt for wake on LAN. The driver
  2956. * should never see this, if it does, something serious is
  2957. * wrong.
  2958. */
  2959. dev_err(&adapter->pdev->dev, "WAKE_ON_LAN interrupt\n");
  2960. }
  2961. if (status & ET_INTR_TXMAC) {
  2962. u32 err = readl(&iomem->txmac.err);
  2963. /* When any of the errors occur and TXMAC generates an
  2964. * interrupt to report these errors, it usually means that
  2965. * TXMAC has detected an error in the data stream retrieved
  2966. * from the on-chip Tx Q. All of these errors are catastrophic
  2967. * and TXMAC won't be able to recover data when these errors
  2968. * occur. In a nutshell, the whole Tx path will have to be reset
  2969. * and re-configured afterwards.
  2970. */
  2971. dev_warn(&adapter->pdev->dev, "TXMAC interrupt, error 0x%08x\n",
  2972. err);
  2973. /* If we are debugging, we want to see this error, otherwise we
  2974. * just want the device to be reset and continue
  2975. */
  2976. }
  2977. if (status & ET_INTR_RXMAC) {
  2978. /* These interrupts are catastrophic to the device, what we need
  2979. * to do is disable the interrupts and set the flag to cause us
  2980. * to reset so we can solve this issue.
  2981. */
  2982. dev_warn(&adapter->pdev->dev,
  2983. "RXMAC interrupt, error 0x%08x. Requesting reset\n",
  2984. readl(&iomem->rxmac.err_reg));
  2985. dev_warn(&adapter->pdev->dev,
  2986. "Enable 0x%08x, Diag 0x%08x\n",
  2987. readl(&iomem->rxmac.ctrl),
  2988. readl(&iomem->rxmac.rxq_diag));
  2989. /* If we are debugging, we want to see this error, otherwise we
  2990. * just want the device to be reset and continue
  2991. */
  2992. }
  2993. if (status & ET_INTR_MAC_STAT) {
  2994. /* This means at least one of the un-masked counters in the
  2995. * MAC_STAT block has rolled over. Use this to maintain the top,
  2996. * software managed bits of the counter(s).
  2997. */
  2998. et1310_handle_macstat_interrupt(adapter);
  2999. }
  3000. if (status & ET_INTR_SLV_TIMEOUT) {
  3001. /* This means a timeout has occurred on a read or write request
  3002. * to one of the JAGCore registers. The Global Resources block
  3003. * has terminated the request and on a read request, returned a
  3004. * "fake" value. The most likely reasons are: Bad Address or the
  3005. * addressed module is in a power-down state and can't respond.
  3006. */
  3007. }
  3008. out:
  3009. if (enable_interrupts)
  3010. et131x_enable_interrupts(adapter);
  3011. return IRQ_RETVAL(handled);
  3012. }
  3013. static int et131x_poll(struct napi_struct *napi, int budget)
  3014. {
  3015. struct et131x_adapter *adapter =
  3016. container_of(napi, struct et131x_adapter, napi);
  3017. int work_done = et131x_handle_recv_pkts(adapter, budget);
  3018. et131x_handle_send_pkts(adapter);
  3019. if (work_done < budget) {
  3020. napi_complete(&adapter->napi);
  3021. et131x_enable_interrupts(adapter);
  3022. }
  3023. return work_done;
  3024. }
  3025. /* et131x_stats - Return the current device statistics */
  3026. static struct net_device_stats *et131x_stats(struct net_device *netdev)
  3027. {
  3028. struct et131x_adapter *adapter = netdev_priv(netdev);
  3029. struct net_device_stats *stats = &adapter->netdev->stats;
  3030. struct ce_stats *devstat = &adapter->stats;
  3031. stats->rx_errors = devstat->rx_length_errs +
  3032. devstat->rx_align_errs +
  3033. devstat->rx_crc_errs +
  3034. devstat->rx_code_violations +
  3035. devstat->rx_other_errs;
  3036. stats->tx_errors = devstat->tx_max_pkt_errs;
  3037. stats->multicast = devstat->multicast_pkts_rcvd;
  3038. stats->collisions = devstat->tx_collisions;
  3039. stats->rx_length_errors = devstat->rx_length_errs;
  3040. stats->rx_over_errors = devstat->rx_overflows;
  3041. stats->rx_crc_errors = devstat->rx_crc_errs;
  3042. stats->rx_dropped = devstat->rcvd_pkts_dropped;
  3043. /* NOTE: Not used, can't find analogous statistics */
  3044. /* stats->rx_frame_errors = devstat->; */
  3045. /* stats->rx_fifo_errors = devstat->; */
  3046. /* stats->rx_missed_errors = devstat->; */
  3047. /* stats->tx_aborted_errors = devstat->; */
  3048. /* stats->tx_carrier_errors = devstat->; */
  3049. /* stats->tx_fifo_errors = devstat->; */
  3050. /* stats->tx_heartbeat_errors = devstat->; */
  3051. /* stats->tx_window_errors = devstat->; */
  3052. return stats;
  3053. }
  3054. static int et131x_open(struct net_device *netdev)
  3055. {
  3056. struct et131x_adapter *adapter = netdev_priv(netdev);
  3057. struct pci_dev *pdev = adapter->pdev;
  3058. unsigned int irq = pdev->irq;
  3059. int result;
  3060. /* Start the timer to track NIC errors */
  3061. init_timer(&adapter->error_timer);
  3062. adapter->error_timer.expires = jiffies + TX_ERROR_PERIOD * HZ / 1000;
  3063. adapter->error_timer.function = et131x_error_timer_handler;
  3064. adapter->error_timer.data = (unsigned long)adapter;
  3065. add_timer(&adapter->error_timer);
  3066. result = request_irq(irq, et131x_isr,
  3067. IRQF_SHARED, netdev->name, netdev);
  3068. if (result) {
  3069. dev_err(&pdev->dev, "could not register IRQ %d\n", irq);
  3070. return result;
  3071. }
  3072. adapter->flags |= FMP_ADAPTER_INTERRUPT_IN_USE;
  3073. napi_enable(&adapter->napi);
  3074. et131x_up(netdev);
  3075. return result;
  3076. }
  3077. static int et131x_close(struct net_device *netdev)
  3078. {
  3079. struct et131x_adapter *adapter = netdev_priv(netdev);
  3080. et131x_down(netdev);
  3081. napi_disable(&adapter->napi);
  3082. adapter->flags &= ~FMP_ADAPTER_INTERRUPT_IN_USE;
  3083. free_irq(adapter->pdev->irq, netdev);
  3084. /* Stop the error timer */
  3085. return del_timer_sync(&adapter->error_timer);
  3086. }
  3087. static int et131x_ioctl(struct net_device *netdev, struct ifreq *reqbuf,
  3088. int cmd)
  3089. {
  3090. struct et131x_adapter *adapter = netdev_priv(netdev);
  3091. if (!adapter->phydev)
  3092. return -EINVAL;
  3093. return phy_mii_ioctl(adapter->phydev, reqbuf, cmd);
  3094. }
  3095. /* et131x_set_packet_filter - Configures the Rx Packet filtering */
  3096. static int et131x_set_packet_filter(struct et131x_adapter *adapter)
  3097. {
  3098. int filter = adapter->packet_filter;
  3099. u32 ctrl;
  3100. u32 pf_ctrl;
  3101. ctrl = readl(&adapter->regs->rxmac.ctrl);
  3102. pf_ctrl = readl(&adapter->regs->rxmac.pf_ctrl);
  3103. /* Default to disabled packet filtering */
  3104. ctrl |= 0x04;
  3105. /* Set us to be in promiscuous mode so we receive everything, this
  3106. * is also true when we get a packet filter of 0
  3107. */
  3108. if ((filter & ET131X_PACKET_TYPE_PROMISCUOUS) || filter == 0)
  3109. pf_ctrl &= ~7; /* Clear filter bits */
  3110. else {
  3111. /* Set us up with Multicast packet filtering. Three cases are
  3112. * possible - (1) we have a multi-cast list, (2) we receive ALL
  3113. * multicast entries or (3) we receive none.
  3114. */
  3115. if (filter & ET131X_PACKET_TYPE_ALL_MULTICAST)
  3116. pf_ctrl &= ~2; /* Multicast filter bit */
  3117. else {
  3118. et1310_setup_device_for_multicast(adapter);
  3119. pf_ctrl |= 2;
  3120. ctrl &= ~0x04;
  3121. }
  3122. /* Set us up with Unicast packet filtering */
  3123. if (filter & ET131X_PACKET_TYPE_DIRECTED) {
  3124. et1310_setup_device_for_unicast(adapter);
  3125. pf_ctrl |= 4;
  3126. ctrl &= ~0x04;
  3127. }
  3128. /* Set us up with Broadcast packet filtering */
  3129. if (filter & ET131X_PACKET_TYPE_BROADCAST) {
  3130. pf_ctrl |= 1; /* Broadcast filter bit */
  3131. ctrl &= ~0x04;
  3132. } else {
  3133. pf_ctrl &= ~1;
  3134. }
  3135. /* Setup the receive mac configuration registers - Packet
  3136. * Filter control + the enable / disable for packet filter
  3137. * in the control reg.
  3138. */
  3139. writel(pf_ctrl, &adapter->regs->rxmac.pf_ctrl);
  3140. writel(ctrl, &adapter->regs->rxmac.ctrl);
  3141. }
  3142. return 0;
  3143. }
  3144. static void et131x_multicast(struct net_device *netdev)
  3145. {
  3146. struct et131x_adapter *adapter = netdev_priv(netdev);
  3147. int packet_filter;
  3148. struct netdev_hw_addr *ha;
  3149. int i;
  3150. /* Before we modify the platform-independent filter flags, store them
  3151. * locally. This allows us to determine if anything's changed and if
  3152. * we even need to bother the hardware
  3153. */
  3154. packet_filter = adapter->packet_filter;
  3155. /* Clear the 'multicast' flag locally; because we only have a single
  3156. * flag to check multicast, and multiple multicast addresses can be
  3157. * set, this is the easiest way to determine if more than one
  3158. * multicast address is being set.
  3159. */
  3160. packet_filter &= ~ET131X_PACKET_TYPE_MULTICAST;
  3161. /* Check the net_device flags and set the device independent flags
  3162. * accordingly
  3163. */
  3164. if (netdev->flags & IFF_PROMISC)
  3165. adapter->packet_filter |= ET131X_PACKET_TYPE_PROMISCUOUS;
  3166. else
  3167. adapter->packet_filter &= ~ET131X_PACKET_TYPE_PROMISCUOUS;
  3168. if ((netdev->flags & IFF_ALLMULTI) ||
  3169. (netdev_mc_count(netdev) > NIC_MAX_MCAST_LIST))
  3170. adapter->packet_filter |= ET131X_PACKET_TYPE_ALL_MULTICAST;
  3171. if (netdev_mc_count(netdev) < 1) {
  3172. adapter->packet_filter &= ~ET131X_PACKET_TYPE_ALL_MULTICAST;
  3173. adapter->packet_filter &= ~ET131X_PACKET_TYPE_MULTICAST;
  3174. } else {
  3175. adapter->packet_filter |= ET131X_PACKET_TYPE_MULTICAST;
  3176. }
  3177. /* Set values in the private adapter struct */
  3178. i = 0;
  3179. netdev_for_each_mc_addr(ha, netdev) {
  3180. if (i == NIC_MAX_MCAST_LIST)
  3181. break;
  3182. ether_addr_copy(adapter->multicast_list[i++], ha->addr);
  3183. }
  3184. adapter->multicast_addr_count = i;
  3185. /* Are the new flags different from the previous ones? If not, then no
  3186. * action is required
  3187. *
  3188. * NOTE - This block will always update the multicast_list with the
  3189. * hardware, even if the addresses aren't the same.
  3190. */
  3191. if (packet_filter != adapter->packet_filter)
  3192. et131x_set_packet_filter(adapter);
  3193. }
  3194. static netdev_tx_t et131x_tx(struct sk_buff *skb, struct net_device *netdev)
  3195. {
  3196. struct et131x_adapter *adapter = netdev_priv(netdev);
  3197. struct tx_ring *tx_ring = &adapter->tx_ring;
  3198. /* stop the queue if it's getting full */
  3199. if (tx_ring->used >= NUM_TCB - 1 && !netif_queue_stopped(netdev))
  3200. netif_stop_queue(netdev);
  3201. /* Save the timestamp for the TX timeout watchdog */
  3202. netdev->trans_start = jiffies;
  3203. /* TCB is not available */
  3204. if (tx_ring->used >= NUM_TCB)
  3205. goto drop_err;
  3206. if ((adapter->flags & FMP_ADAPTER_FAIL_SEND_MASK) ||
  3207. !netif_carrier_ok(netdev))
  3208. goto drop_err;
  3209. if (send_packet(skb, adapter))
  3210. goto drop_err;
  3211. return NETDEV_TX_OK;
  3212. drop_err:
  3213. dev_kfree_skb_any(skb);
  3214. adapter->netdev->stats.tx_dropped++;
  3215. return NETDEV_TX_OK;
  3216. }
  3217. /* et131x_tx_timeout - Timeout handler
  3218. *
  3219. * The handler called when a Tx request times out. The timeout period is
  3220. * specified by the 'tx_timeo" element in the net_device structure (see
  3221. * et131x_alloc_device() to see how this value is set).
  3222. */
  3223. static void et131x_tx_timeout(struct net_device *netdev)
  3224. {
  3225. struct et131x_adapter *adapter = netdev_priv(netdev);
  3226. struct tx_ring *tx_ring = &adapter->tx_ring;
  3227. struct tcb *tcb;
  3228. unsigned long flags;
  3229. /* If the device is closed, ignore the timeout */
  3230. if (~(adapter->flags & FMP_ADAPTER_INTERRUPT_IN_USE))
  3231. return;
  3232. /* Any nonrecoverable hardware error?
  3233. * Checks adapter->flags for any failure in phy reading
  3234. */
  3235. if (adapter->flags & FMP_ADAPTER_NON_RECOVER_ERROR)
  3236. return;
  3237. /* Hardware failure? */
  3238. if (adapter->flags & FMP_ADAPTER_HARDWARE_ERROR) {
  3239. dev_err(&adapter->pdev->dev, "hardware error - reset\n");
  3240. return;
  3241. }
  3242. /* Is send stuck? */
  3243. spin_lock_irqsave(&adapter->tcb_send_qlock, flags);
  3244. tcb = tx_ring->send_head;
  3245. spin_unlock_irqrestore(&adapter->tcb_send_qlock, flags);
  3246. if (tcb) {
  3247. tcb->count++;
  3248. if (tcb->count > NIC_SEND_HANG_THRESHOLD) {
  3249. dev_warn(&adapter->pdev->dev,
  3250. "Send stuck - reset. tcb->WrIndex %x\n",
  3251. tcb->index);
  3252. adapter->netdev->stats.tx_errors++;
  3253. /* perform reset of tx/rx */
  3254. et131x_disable_txrx(netdev);
  3255. et131x_enable_txrx(netdev);
  3256. }
  3257. }
  3258. }
  3259. static int et131x_change_mtu(struct net_device *netdev, int new_mtu)
  3260. {
  3261. int result = 0;
  3262. struct et131x_adapter *adapter = netdev_priv(netdev);
  3263. if (new_mtu < 64 || new_mtu > 9216)
  3264. return -EINVAL;
  3265. et131x_disable_txrx(netdev);
  3266. netdev->mtu = new_mtu;
  3267. et131x_adapter_memory_free(adapter);
  3268. /* Set the config parameter for Jumbo Packet support */
  3269. adapter->registry_jumbo_packet = new_mtu + 14;
  3270. et131x_soft_reset(adapter);
  3271. result = et131x_adapter_memory_alloc(adapter);
  3272. if (result != 0) {
  3273. dev_warn(&adapter->pdev->dev,
  3274. "Change MTU failed; couldn't re-alloc DMA memory\n");
  3275. return result;
  3276. }
  3277. et131x_init_send(adapter);
  3278. et131x_hwaddr_init(adapter);
  3279. ether_addr_copy(netdev->dev_addr, adapter->addr);
  3280. /* Init the device with the new settings */
  3281. et131x_adapter_setup(adapter);
  3282. et131x_enable_txrx(netdev);
  3283. return result;
  3284. }
  3285. static const struct net_device_ops et131x_netdev_ops = {
  3286. .ndo_open = et131x_open,
  3287. .ndo_stop = et131x_close,
  3288. .ndo_start_xmit = et131x_tx,
  3289. .ndo_set_rx_mode = et131x_multicast,
  3290. .ndo_tx_timeout = et131x_tx_timeout,
  3291. .ndo_change_mtu = et131x_change_mtu,
  3292. .ndo_set_mac_address = eth_mac_addr,
  3293. .ndo_validate_addr = eth_validate_addr,
  3294. .ndo_get_stats = et131x_stats,
  3295. .ndo_do_ioctl = et131x_ioctl,
  3296. };
  3297. static int et131x_pci_setup(struct pci_dev *pdev,
  3298. const struct pci_device_id *ent)
  3299. {
  3300. struct net_device *netdev;
  3301. struct et131x_adapter *adapter;
  3302. int rc;
  3303. int ii;
  3304. rc = pci_enable_device(pdev);
  3305. if (rc < 0) {
  3306. dev_err(&pdev->dev, "pci_enable_device() failed\n");
  3307. goto out;
  3308. }
  3309. /* Perform some basic PCI checks */
  3310. if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
  3311. dev_err(&pdev->dev, "Can't find PCI device's base address\n");
  3312. rc = -ENODEV;
  3313. goto err_disable;
  3314. }
  3315. rc = pci_request_regions(pdev, DRIVER_NAME);
  3316. if (rc < 0) {
  3317. dev_err(&pdev->dev, "Can't get PCI resources\n");
  3318. goto err_disable;
  3319. }
  3320. pci_set_master(pdev);
  3321. /* Check the DMA addressing support of this device */
  3322. if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) &&
  3323. dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32))) {
  3324. dev_err(&pdev->dev, "No usable DMA addressing method\n");
  3325. rc = -EIO;
  3326. goto err_release_res;
  3327. }
  3328. netdev = alloc_etherdev(sizeof(struct et131x_adapter));
  3329. if (!netdev) {
  3330. dev_err(&pdev->dev, "Couldn't alloc netdev struct\n");
  3331. rc = -ENOMEM;
  3332. goto err_release_res;
  3333. }
  3334. netdev->watchdog_timeo = ET131X_TX_TIMEOUT;
  3335. netdev->netdev_ops = &et131x_netdev_ops;
  3336. SET_NETDEV_DEV(netdev, &pdev->dev);
  3337. netdev->ethtool_ops = &et131x_ethtool_ops;
  3338. adapter = et131x_adapter_init(netdev, pdev);
  3339. rc = et131x_pci_init(adapter, pdev);
  3340. if (rc < 0)
  3341. goto err_free_dev;
  3342. /* Map the bus-relative registers to system virtual memory */
  3343. adapter->regs = pci_ioremap_bar(pdev, 0);
  3344. if (!adapter->regs) {
  3345. dev_err(&pdev->dev, "Cannot map device registers\n");
  3346. rc = -ENOMEM;
  3347. goto err_free_dev;
  3348. }
  3349. /* If Phy COMA mode was enabled when we went down, disable it here. */
  3350. writel(ET_PMCSR_INIT, &adapter->regs->global.pm_csr);
  3351. et131x_soft_reset(adapter);
  3352. et131x_disable_interrupts(adapter);
  3353. rc = et131x_adapter_memory_alloc(adapter);
  3354. if (rc < 0) {
  3355. dev_err(&pdev->dev, "Could not alloc adapter memory (DMA)\n");
  3356. goto err_iounmap;
  3357. }
  3358. et131x_init_send(adapter);
  3359. netif_napi_add(netdev, &adapter->napi, et131x_poll, 64);
  3360. ether_addr_copy(netdev->dev_addr, adapter->addr);
  3361. rc = -ENOMEM;
  3362. adapter->mii_bus = mdiobus_alloc();
  3363. if (!adapter->mii_bus) {
  3364. dev_err(&pdev->dev, "Alloc of mii_bus struct failed\n");
  3365. goto err_mem_free;
  3366. }
  3367. adapter->mii_bus->name = "et131x_eth_mii";
  3368. snprintf(adapter->mii_bus->id, MII_BUS_ID_SIZE, "%x",
  3369. (adapter->pdev->bus->number << 8) | adapter->pdev->devfn);
  3370. adapter->mii_bus->priv = netdev;
  3371. adapter->mii_bus->read = et131x_mdio_read;
  3372. adapter->mii_bus->write = et131x_mdio_write;
  3373. adapter->mii_bus->irq = kmalloc_array(PHY_MAX_ADDR, sizeof(int),
  3374. GFP_KERNEL);
  3375. if (!adapter->mii_bus->irq)
  3376. goto err_mdio_free;
  3377. for (ii = 0; ii < PHY_MAX_ADDR; ii++)
  3378. adapter->mii_bus->irq[ii] = PHY_POLL;
  3379. rc = mdiobus_register(adapter->mii_bus);
  3380. if (rc < 0) {
  3381. dev_err(&pdev->dev, "failed to register MII bus\n");
  3382. goto err_mdio_free_irq;
  3383. }
  3384. rc = et131x_mii_probe(netdev);
  3385. if (rc < 0) {
  3386. dev_err(&pdev->dev, "failed to probe MII bus\n");
  3387. goto err_mdio_unregister;
  3388. }
  3389. et131x_adapter_setup(adapter);
  3390. /* Init variable for counting how long we do not have link status */
  3391. adapter->boot_coma = 0;
  3392. et1310_disable_phy_coma(adapter);
  3393. /* We can enable interrupts now
  3394. *
  3395. * NOTE - Because registration of interrupt handler is done in the
  3396. * device's open(), defer enabling device interrupts to that
  3397. * point
  3398. */
  3399. rc = register_netdev(netdev);
  3400. if (rc < 0) {
  3401. dev_err(&pdev->dev, "register_netdev() failed\n");
  3402. goto err_phy_disconnect;
  3403. }
  3404. /* Register the net_device struct with the PCI subsystem. Save a copy
  3405. * of the PCI config space for this device now that the device has
  3406. * been initialized, just in case it needs to be quickly restored.
  3407. */
  3408. pci_set_drvdata(pdev, netdev);
  3409. out:
  3410. return rc;
  3411. err_phy_disconnect:
  3412. phy_disconnect(adapter->phydev);
  3413. err_mdio_unregister:
  3414. mdiobus_unregister(adapter->mii_bus);
  3415. err_mdio_free_irq:
  3416. kfree(adapter->mii_bus->irq);
  3417. err_mdio_free:
  3418. mdiobus_free(adapter->mii_bus);
  3419. err_mem_free:
  3420. et131x_adapter_memory_free(adapter);
  3421. err_iounmap:
  3422. iounmap(adapter->regs);
  3423. err_free_dev:
  3424. pci_dev_put(pdev);
  3425. free_netdev(netdev);
  3426. err_release_res:
  3427. pci_release_regions(pdev);
  3428. err_disable:
  3429. pci_disable_device(pdev);
  3430. goto out;
  3431. }
  3432. static const struct pci_device_id et131x_pci_table[] = {
  3433. { PCI_VDEVICE(ATT, ET131X_PCI_DEVICE_ID_GIG), 0UL},
  3434. { PCI_VDEVICE(ATT, ET131X_PCI_DEVICE_ID_FAST), 0UL},
  3435. { 0,}
  3436. };
  3437. MODULE_DEVICE_TABLE(pci, et131x_pci_table);
  3438. static struct pci_driver et131x_driver = {
  3439. .name = DRIVER_NAME,
  3440. .id_table = et131x_pci_table,
  3441. .probe = et131x_pci_setup,
  3442. .remove = et131x_pci_remove,
  3443. .driver.pm = &et131x_pm_ops,
  3444. };
  3445. module_pci_driver(et131x_driver);