et131x.c 118 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123
  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 +
  2639. msecs_to_jiffies(TX_ERROR_PERIOD));
  2640. }
  2641. static void et131x_adapter_memory_free(struct et131x_adapter *adapter)
  2642. {
  2643. et131x_tx_dma_memory_free(adapter);
  2644. et131x_rx_dma_memory_free(adapter);
  2645. }
  2646. static int et131x_adapter_memory_alloc(struct et131x_adapter *adapter)
  2647. {
  2648. int status;
  2649. status = et131x_tx_dma_memory_alloc(adapter);
  2650. if (status) {
  2651. dev_err(&adapter->pdev->dev,
  2652. "et131x_tx_dma_memory_alloc FAILED\n");
  2653. et131x_tx_dma_memory_free(adapter);
  2654. return status;
  2655. }
  2656. status = et131x_rx_dma_memory_alloc(adapter);
  2657. if (status) {
  2658. dev_err(&adapter->pdev->dev,
  2659. "et131x_rx_dma_memory_alloc FAILED\n");
  2660. et131x_adapter_memory_free(adapter);
  2661. return status;
  2662. }
  2663. status = et131x_init_recv(adapter);
  2664. if (status) {
  2665. dev_err(&adapter->pdev->dev, "et131x_init_recv FAILED\n");
  2666. et131x_adapter_memory_free(adapter);
  2667. }
  2668. return status;
  2669. }
  2670. static void et131x_adjust_link(struct net_device *netdev)
  2671. {
  2672. struct et131x_adapter *adapter = netdev_priv(netdev);
  2673. struct phy_device *phydev = adapter->phydev;
  2674. if (!phydev)
  2675. return;
  2676. if (phydev->link == adapter->link)
  2677. return;
  2678. /* Check to see if we are in coma mode and if
  2679. * so, disable it because we will not be able
  2680. * to read PHY values until we are out.
  2681. */
  2682. if (et1310_in_phy_coma(adapter))
  2683. et1310_disable_phy_coma(adapter);
  2684. adapter->link = phydev->link;
  2685. phy_print_status(phydev);
  2686. if (phydev->link) {
  2687. adapter->boot_coma = 20;
  2688. if (phydev->speed == SPEED_10) {
  2689. u16 register18;
  2690. et131x_mii_read(adapter, PHY_MPHY_CONTROL_REG,
  2691. &register18);
  2692. et131x_mii_write(adapter, phydev->addr,
  2693. PHY_MPHY_CONTROL_REG,
  2694. register18 | 0x4);
  2695. et131x_mii_write(adapter, phydev->addr, PHY_INDEX_REG,
  2696. register18 | 0x8402);
  2697. et131x_mii_write(adapter, phydev->addr, PHY_DATA_REG,
  2698. register18 | 511);
  2699. et131x_mii_write(adapter, phydev->addr,
  2700. PHY_MPHY_CONTROL_REG, register18);
  2701. }
  2702. et1310_config_flow_control(adapter);
  2703. if (phydev->speed == SPEED_1000 &&
  2704. adapter->registry_jumbo_packet > 2048) {
  2705. u16 reg;
  2706. et131x_mii_read(adapter, PHY_CONFIG, &reg);
  2707. reg &= ~ET_PHY_CONFIG_TX_FIFO_DEPTH;
  2708. reg |= ET_PHY_CONFIG_FIFO_DEPTH_32;
  2709. et131x_mii_write(adapter, phydev->addr, PHY_CONFIG,
  2710. reg);
  2711. }
  2712. et131x_set_rx_dma_timer(adapter);
  2713. et1310_config_mac_regs2(adapter);
  2714. } else {
  2715. adapter->boot_coma = 0;
  2716. if (phydev->speed == SPEED_10) {
  2717. u16 register18;
  2718. et131x_mii_read(adapter, PHY_MPHY_CONTROL_REG,
  2719. &register18);
  2720. et131x_mii_write(adapter, phydev->addr,
  2721. PHY_MPHY_CONTROL_REG,
  2722. register18 | 0x4);
  2723. et131x_mii_write(adapter, phydev->addr,
  2724. PHY_INDEX_REG, register18 | 0x8402);
  2725. et131x_mii_write(adapter, phydev->addr,
  2726. PHY_DATA_REG, register18 | 511);
  2727. et131x_mii_write(adapter, phydev->addr,
  2728. PHY_MPHY_CONTROL_REG, register18);
  2729. }
  2730. et131x_free_busy_send_packets(adapter);
  2731. et131x_init_send(adapter);
  2732. /* Bring the device back to the state it was during
  2733. * init prior to autonegotiation being complete. This
  2734. * way, when we get the auto-neg complete interrupt,
  2735. * we can complete init by calling config_mac_regs2.
  2736. */
  2737. et131x_soft_reset(adapter);
  2738. et131x_adapter_setup(adapter);
  2739. et131x_disable_txrx(netdev);
  2740. et131x_enable_txrx(netdev);
  2741. }
  2742. }
  2743. static int et131x_mii_probe(struct net_device *netdev)
  2744. {
  2745. struct et131x_adapter *adapter = netdev_priv(netdev);
  2746. struct phy_device *phydev = NULL;
  2747. phydev = phy_find_first(adapter->mii_bus);
  2748. if (!phydev) {
  2749. dev_err(&adapter->pdev->dev, "no PHY found\n");
  2750. return -ENODEV;
  2751. }
  2752. phydev = phy_connect(netdev, dev_name(&phydev->dev),
  2753. &et131x_adjust_link, PHY_INTERFACE_MODE_MII);
  2754. if (IS_ERR(phydev)) {
  2755. dev_err(&adapter->pdev->dev, "Could not attach to PHY\n");
  2756. return PTR_ERR(phydev);
  2757. }
  2758. phydev->supported &= (SUPPORTED_10baseT_Half |
  2759. SUPPORTED_10baseT_Full |
  2760. SUPPORTED_100baseT_Half |
  2761. SUPPORTED_100baseT_Full |
  2762. SUPPORTED_Autoneg |
  2763. SUPPORTED_MII |
  2764. SUPPORTED_TP);
  2765. if (adapter->pdev->device != ET131X_PCI_DEVICE_ID_FAST)
  2766. phydev->supported |= SUPPORTED_1000baseT_Half |
  2767. SUPPORTED_1000baseT_Full;
  2768. phydev->advertising = phydev->supported;
  2769. phydev->autoneg = AUTONEG_ENABLE;
  2770. adapter->phydev = phydev;
  2771. dev_info(&adapter->pdev->dev,
  2772. "attached PHY driver [%s] (mii_bus:phy_addr=%s)\n",
  2773. phydev->drv->name, dev_name(&phydev->dev));
  2774. return 0;
  2775. }
  2776. static struct et131x_adapter *et131x_adapter_init(struct net_device *netdev,
  2777. struct pci_dev *pdev)
  2778. {
  2779. static const u8 default_mac[] = { 0x00, 0x05, 0x3d, 0x00, 0x02, 0x00 };
  2780. struct et131x_adapter *adapter;
  2781. adapter = netdev_priv(netdev);
  2782. adapter->pdev = pci_dev_get(pdev);
  2783. adapter->netdev = netdev;
  2784. spin_lock_init(&adapter->tcb_send_qlock);
  2785. spin_lock_init(&adapter->tcb_ready_qlock);
  2786. spin_lock_init(&adapter->rcv_lock);
  2787. adapter->registry_jumbo_packet = 1514; /* 1514-9216 */
  2788. ether_addr_copy(adapter->addr, default_mac);
  2789. return adapter;
  2790. }
  2791. static void et131x_pci_remove(struct pci_dev *pdev)
  2792. {
  2793. struct net_device *netdev = pci_get_drvdata(pdev);
  2794. struct et131x_adapter *adapter = netdev_priv(netdev);
  2795. unregister_netdev(netdev);
  2796. netif_napi_del(&adapter->napi);
  2797. phy_disconnect(adapter->phydev);
  2798. mdiobus_unregister(adapter->mii_bus);
  2799. kfree(adapter->mii_bus->irq);
  2800. mdiobus_free(adapter->mii_bus);
  2801. et131x_adapter_memory_free(adapter);
  2802. iounmap(adapter->regs);
  2803. pci_dev_put(pdev);
  2804. free_netdev(netdev);
  2805. pci_release_regions(pdev);
  2806. pci_disable_device(pdev);
  2807. }
  2808. static void et131x_up(struct net_device *netdev)
  2809. {
  2810. struct et131x_adapter *adapter = netdev_priv(netdev);
  2811. et131x_enable_txrx(netdev);
  2812. phy_start(adapter->phydev);
  2813. }
  2814. static void et131x_down(struct net_device *netdev)
  2815. {
  2816. struct et131x_adapter *adapter = netdev_priv(netdev);
  2817. /* Save the timestamp for the TX watchdog, prevent a timeout */
  2818. netdev->trans_start = jiffies;
  2819. phy_stop(adapter->phydev);
  2820. et131x_disable_txrx(netdev);
  2821. }
  2822. #ifdef CONFIG_PM_SLEEP
  2823. static int et131x_suspend(struct device *dev)
  2824. {
  2825. struct pci_dev *pdev = to_pci_dev(dev);
  2826. struct net_device *netdev = pci_get_drvdata(pdev);
  2827. if (netif_running(netdev)) {
  2828. netif_device_detach(netdev);
  2829. et131x_down(netdev);
  2830. pci_save_state(pdev);
  2831. }
  2832. return 0;
  2833. }
  2834. static int et131x_resume(struct device *dev)
  2835. {
  2836. struct pci_dev *pdev = to_pci_dev(dev);
  2837. struct net_device *netdev = pci_get_drvdata(pdev);
  2838. if (netif_running(netdev)) {
  2839. pci_restore_state(pdev);
  2840. et131x_up(netdev);
  2841. netif_device_attach(netdev);
  2842. }
  2843. return 0;
  2844. }
  2845. #endif
  2846. static SIMPLE_DEV_PM_OPS(et131x_pm_ops, et131x_suspend, et131x_resume);
  2847. static irqreturn_t et131x_isr(int irq, void *dev_id)
  2848. {
  2849. bool handled = true;
  2850. bool enable_interrupts = true;
  2851. struct net_device *netdev = dev_id;
  2852. struct et131x_adapter *adapter = netdev_priv(netdev);
  2853. struct address_map __iomem *iomem = adapter->regs;
  2854. struct rx_ring *rx_ring = &adapter->rx_ring;
  2855. struct tx_ring *tx_ring = &adapter->tx_ring;
  2856. u32 status;
  2857. if (!netif_device_present(netdev)) {
  2858. handled = false;
  2859. enable_interrupts = false;
  2860. goto out;
  2861. }
  2862. et131x_disable_interrupts(adapter);
  2863. status = readl(&adapter->regs->global.int_status);
  2864. if (adapter->flow == FLOW_TXONLY || adapter->flow == FLOW_BOTH)
  2865. status &= ~INT_MASK_ENABLE;
  2866. else
  2867. status &= ~INT_MASK_ENABLE_NO_FLOW;
  2868. /* Make sure this is our interrupt */
  2869. if (!status) {
  2870. handled = false;
  2871. et131x_enable_interrupts(adapter);
  2872. goto out;
  2873. }
  2874. /* This is our interrupt, so process accordingly */
  2875. if (status & ET_INTR_WATCHDOG) {
  2876. struct tcb *tcb = tx_ring->send_head;
  2877. if (tcb)
  2878. if (++tcb->stale > 1)
  2879. status |= ET_INTR_TXDMA_ISR;
  2880. if (rx_ring->unfinished_receives)
  2881. status |= ET_INTR_RXDMA_XFR_DONE;
  2882. else if (tcb == NULL)
  2883. writel(0, &adapter->regs->global.watchdog_timer);
  2884. status &= ~ET_INTR_WATCHDOG;
  2885. }
  2886. if (status & (ET_INTR_RXDMA_XFR_DONE | ET_INTR_TXDMA_ISR)) {
  2887. enable_interrupts = false;
  2888. napi_schedule(&adapter->napi);
  2889. }
  2890. status &= ~(ET_INTR_TXDMA_ISR | ET_INTR_RXDMA_XFR_DONE);
  2891. if (!status)
  2892. goto out;
  2893. if (status & ET_INTR_TXDMA_ERR) {
  2894. /* Following read also clears the register (COR) */
  2895. u32 txdma_err = readl(&iomem->txdma.tx_dma_error);
  2896. dev_warn(&adapter->pdev->dev,
  2897. "TXDMA_ERR interrupt, error = %d\n",
  2898. txdma_err);
  2899. }
  2900. if (status & (ET_INTR_RXDMA_FB_R0_LOW | ET_INTR_RXDMA_FB_R1_LOW)) {
  2901. /* This indicates the number of unused buffers in RXDMA free
  2902. * buffer ring 0 is <= the limit you programmed. Free buffer
  2903. * resources need to be returned. Free buffers are consumed as
  2904. * packets are passed from the network to the host. The host
  2905. * becomes aware of the packets from the contents of the packet
  2906. * status ring. This ring is queried when the packet done
  2907. * interrupt occurs. Packets are then passed to the OS. When
  2908. * the OS is done with the packets the resources can be
  2909. * returned to the ET1310 for re-use. This interrupt is one
  2910. * method of returning resources.
  2911. */
  2912. /* If the user has flow control on, then we will
  2913. * send a pause packet, otherwise just exit
  2914. */
  2915. if (adapter->flow == FLOW_TXONLY || adapter->flow == FLOW_BOTH) {
  2916. u32 pm_csr;
  2917. /* Tell the device to send a pause packet via the back
  2918. * pressure register (bp req and bp xon/xoff)
  2919. */
  2920. pm_csr = readl(&iomem->global.pm_csr);
  2921. if (!et1310_in_phy_coma(adapter))
  2922. writel(3, &iomem->txmac.bp_ctrl);
  2923. }
  2924. }
  2925. /* Handle Packet Status Ring Low Interrupt */
  2926. if (status & ET_INTR_RXDMA_STAT_LOW) {
  2927. /* Same idea as with the two Free Buffer Rings. Packets going
  2928. * from the network to the host each consume a free buffer
  2929. * resource and a packet status resource. These resources are
  2930. * passed to the OS. When the OS is done with the resources,
  2931. * they need to be returned to the ET1310. This is one method
  2932. * of returning the resources.
  2933. */
  2934. }
  2935. if (status & ET_INTR_RXDMA_ERR) {
  2936. /* The rxdma_error interrupt is sent when a time-out on a
  2937. * request issued by the JAGCore has occurred or a completion is
  2938. * returned with an un-successful status. In both cases the
  2939. * request is considered complete. The JAGCore will
  2940. * automatically re-try the request in question. Normally
  2941. * information on events like these are sent to the host using
  2942. * the "Advanced Error Reporting" capability. This interrupt is
  2943. * another way of getting similar information. The only thing
  2944. * required is to clear the interrupt by reading the ISR in the
  2945. * global resources. The JAGCore will do a re-try on the
  2946. * request. Normally you should never see this interrupt. If
  2947. * you start to see this interrupt occurring frequently then
  2948. * something bad has occurred. A reset might be the thing to do.
  2949. */
  2950. /* TRAP();*/
  2951. dev_warn(&adapter->pdev->dev, "RxDMA_ERR interrupt, error %x\n",
  2952. readl(&iomem->txmac.tx_test));
  2953. }
  2954. /* Handle the Wake on LAN Event */
  2955. if (status & ET_INTR_WOL) {
  2956. /* This is a secondary interrupt for wake on LAN. The driver
  2957. * should never see this, if it does, something serious is
  2958. * wrong.
  2959. */
  2960. dev_err(&adapter->pdev->dev, "WAKE_ON_LAN interrupt\n");
  2961. }
  2962. if (status & ET_INTR_TXMAC) {
  2963. u32 err = readl(&iomem->txmac.err);
  2964. /* When any of the errors occur and TXMAC generates an
  2965. * interrupt to report these errors, it usually means that
  2966. * TXMAC has detected an error in the data stream retrieved
  2967. * from the on-chip Tx Q. All of these errors are catastrophic
  2968. * and TXMAC won't be able to recover data when these errors
  2969. * occur. In a nutshell, the whole Tx path will have to be reset
  2970. * and re-configured afterwards.
  2971. */
  2972. dev_warn(&adapter->pdev->dev, "TXMAC interrupt, error 0x%08x\n",
  2973. err);
  2974. /* If we are debugging, we want to see this error, otherwise we
  2975. * just want the device to be reset and continue
  2976. */
  2977. }
  2978. if (status & ET_INTR_RXMAC) {
  2979. /* These interrupts are catastrophic to the device, what we need
  2980. * to do is disable the interrupts and set the flag to cause us
  2981. * to reset so we can solve this issue.
  2982. */
  2983. dev_warn(&adapter->pdev->dev,
  2984. "RXMAC interrupt, error 0x%08x. Requesting reset\n",
  2985. readl(&iomem->rxmac.err_reg));
  2986. dev_warn(&adapter->pdev->dev,
  2987. "Enable 0x%08x, Diag 0x%08x\n",
  2988. readl(&iomem->rxmac.ctrl),
  2989. readl(&iomem->rxmac.rxq_diag));
  2990. /* If we are debugging, we want to see this error, otherwise we
  2991. * just want the device to be reset and continue
  2992. */
  2993. }
  2994. if (status & ET_INTR_MAC_STAT) {
  2995. /* This means at least one of the un-masked counters in the
  2996. * MAC_STAT block has rolled over. Use this to maintain the top,
  2997. * software managed bits of the counter(s).
  2998. */
  2999. et1310_handle_macstat_interrupt(adapter);
  3000. }
  3001. if (status & ET_INTR_SLV_TIMEOUT) {
  3002. /* This means a timeout has occurred on a read or write request
  3003. * to one of the JAGCore registers. The Global Resources block
  3004. * has terminated the request and on a read request, returned a
  3005. * "fake" value. The most likely reasons are: Bad Address or the
  3006. * addressed module is in a power-down state and can't respond.
  3007. */
  3008. }
  3009. out:
  3010. if (enable_interrupts)
  3011. et131x_enable_interrupts(adapter);
  3012. return IRQ_RETVAL(handled);
  3013. }
  3014. static int et131x_poll(struct napi_struct *napi, int budget)
  3015. {
  3016. struct et131x_adapter *adapter =
  3017. container_of(napi, struct et131x_adapter, napi);
  3018. int work_done = et131x_handle_recv_pkts(adapter, budget);
  3019. et131x_handle_send_pkts(adapter);
  3020. if (work_done < budget) {
  3021. napi_complete(&adapter->napi);
  3022. et131x_enable_interrupts(adapter);
  3023. }
  3024. return work_done;
  3025. }
  3026. /* et131x_stats - Return the current device statistics */
  3027. static struct net_device_stats *et131x_stats(struct net_device *netdev)
  3028. {
  3029. struct et131x_adapter *adapter = netdev_priv(netdev);
  3030. struct net_device_stats *stats = &adapter->netdev->stats;
  3031. struct ce_stats *devstat = &adapter->stats;
  3032. stats->rx_errors = devstat->rx_length_errs +
  3033. devstat->rx_align_errs +
  3034. devstat->rx_crc_errs +
  3035. devstat->rx_code_violations +
  3036. devstat->rx_other_errs;
  3037. stats->tx_errors = devstat->tx_max_pkt_errs;
  3038. stats->multicast = devstat->multicast_pkts_rcvd;
  3039. stats->collisions = devstat->tx_collisions;
  3040. stats->rx_length_errors = devstat->rx_length_errs;
  3041. stats->rx_over_errors = devstat->rx_overflows;
  3042. stats->rx_crc_errors = devstat->rx_crc_errs;
  3043. stats->rx_dropped = devstat->rcvd_pkts_dropped;
  3044. /* NOTE: Not used, can't find analogous statistics */
  3045. /* stats->rx_frame_errors = devstat->; */
  3046. /* stats->rx_fifo_errors = devstat->; */
  3047. /* stats->rx_missed_errors = devstat->; */
  3048. /* stats->tx_aborted_errors = devstat->; */
  3049. /* stats->tx_carrier_errors = devstat->; */
  3050. /* stats->tx_fifo_errors = devstat->; */
  3051. /* stats->tx_heartbeat_errors = devstat->; */
  3052. /* stats->tx_window_errors = devstat->; */
  3053. return stats;
  3054. }
  3055. static int et131x_open(struct net_device *netdev)
  3056. {
  3057. struct et131x_adapter *adapter = netdev_priv(netdev);
  3058. struct pci_dev *pdev = adapter->pdev;
  3059. unsigned int irq = pdev->irq;
  3060. int result;
  3061. /* Start the timer to track NIC errors */
  3062. init_timer(&adapter->error_timer);
  3063. adapter->error_timer.expires = jiffies +
  3064. msecs_to_jiffies(TX_ERROR_PERIOD);
  3065. adapter->error_timer.function = et131x_error_timer_handler;
  3066. adapter->error_timer.data = (unsigned long)adapter;
  3067. add_timer(&adapter->error_timer);
  3068. result = request_irq(irq, et131x_isr,
  3069. IRQF_SHARED, netdev->name, netdev);
  3070. if (result) {
  3071. dev_err(&pdev->dev, "could not register IRQ %d\n", irq);
  3072. return result;
  3073. }
  3074. adapter->flags |= FMP_ADAPTER_INTERRUPT_IN_USE;
  3075. napi_enable(&adapter->napi);
  3076. et131x_up(netdev);
  3077. return result;
  3078. }
  3079. static int et131x_close(struct net_device *netdev)
  3080. {
  3081. struct et131x_adapter *adapter = netdev_priv(netdev);
  3082. et131x_down(netdev);
  3083. napi_disable(&adapter->napi);
  3084. adapter->flags &= ~FMP_ADAPTER_INTERRUPT_IN_USE;
  3085. free_irq(adapter->pdev->irq, netdev);
  3086. /* Stop the error timer */
  3087. return del_timer_sync(&adapter->error_timer);
  3088. }
  3089. static int et131x_ioctl(struct net_device *netdev, struct ifreq *reqbuf,
  3090. int cmd)
  3091. {
  3092. struct et131x_adapter *adapter = netdev_priv(netdev);
  3093. if (!adapter->phydev)
  3094. return -EINVAL;
  3095. return phy_mii_ioctl(adapter->phydev, reqbuf, cmd);
  3096. }
  3097. /* et131x_set_packet_filter - Configures the Rx Packet filtering */
  3098. static int et131x_set_packet_filter(struct et131x_adapter *adapter)
  3099. {
  3100. int filter = adapter->packet_filter;
  3101. u32 ctrl;
  3102. u32 pf_ctrl;
  3103. ctrl = readl(&adapter->regs->rxmac.ctrl);
  3104. pf_ctrl = readl(&adapter->regs->rxmac.pf_ctrl);
  3105. /* Default to disabled packet filtering */
  3106. ctrl |= 0x04;
  3107. /* Set us to be in promiscuous mode so we receive everything, this
  3108. * is also true when we get a packet filter of 0
  3109. */
  3110. if ((filter & ET131X_PACKET_TYPE_PROMISCUOUS) || filter == 0)
  3111. pf_ctrl &= ~7; /* Clear filter bits */
  3112. else {
  3113. /* Set us up with Multicast packet filtering. Three cases are
  3114. * possible - (1) we have a multi-cast list, (2) we receive ALL
  3115. * multicast entries or (3) we receive none.
  3116. */
  3117. if (filter & ET131X_PACKET_TYPE_ALL_MULTICAST)
  3118. pf_ctrl &= ~2; /* Multicast filter bit */
  3119. else {
  3120. et1310_setup_device_for_multicast(adapter);
  3121. pf_ctrl |= 2;
  3122. ctrl &= ~0x04;
  3123. }
  3124. /* Set us up with Unicast packet filtering */
  3125. if (filter & ET131X_PACKET_TYPE_DIRECTED) {
  3126. et1310_setup_device_for_unicast(adapter);
  3127. pf_ctrl |= 4;
  3128. ctrl &= ~0x04;
  3129. }
  3130. /* Set us up with Broadcast packet filtering */
  3131. if (filter & ET131X_PACKET_TYPE_BROADCAST) {
  3132. pf_ctrl |= 1; /* Broadcast filter bit */
  3133. ctrl &= ~0x04;
  3134. } else {
  3135. pf_ctrl &= ~1;
  3136. }
  3137. /* Setup the receive mac configuration registers - Packet
  3138. * Filter control + the enable / disable for packet filter
  3139. * in the control reg.
  3140. */
  3141. writel(pf_ctrl, &adapter->regs->rxmac.pf_ctrl);
  3142. writel(ctrl, &adapter->regs->rxmac.ctrl);
  3143. }
  3144. return 0;
  3145. }
  3146. static void et131x_multicast(struct net_device *netdev)
  3147. {
  3148. struct et131x_adapter *adapter = netdev_priv(netdev);
  3149. int packet_filter;
  3150. struct netdev_hw_addr *ha;
  3151. int i;
  3152. /* Before we modify the platform-independent filter flags, store them
  3153. * locally. This allows us to determine if anything's changed and if
  3154. * we even need to bother the hardware
  3155. */
  3156. packet_filter = adapter->packet_filter;
  3157. /* Clear the 'multicast' flag locally; because we only have a single
  3158. * flag to check multicast, and multiple multicast addresses can be
  3159. * set, this is the easiest way to determine if more than one
  3160. * multicast address is being set.
  3161. */
  3162. packet_filter &= ~ET131X_PACKET_TYPE_MULTICAST;
  3163. /* Check the net_device flags and set the device independent flags
  3164. * accordingly
  3165. */
  3166. if (netdev->flags & IFF_PROMISC)
  3167. adapter->packet_filter |= ET131X_PACKET_TYPE_PROMISCUOUS;
  3168. else
  3169. adapter->packet_filter &= ~ET131X_PACKET_TYPE_PROMISCUOUS;
  3170. if ((netdev->flags & IFF_ALLMULTI) ||
  3171. (netdev_mc_count(netdev) > NIC_MAX_MCAST_LIST))
  3172. adapter->packet_filter |= ET131X_PACKET_TYPE_ALL_MULTICAST;
  3173. if (netdev_mc_count(netdev) < 1) {
  3174. adapter->packet_filter &= ~ET131X_PACKET_TYPE_ALL_MULTICAST;
  3175. adapter->packet_filter &= ~ET131X_PACKET_TYPE_MULTICAST;
  3176. } else {
  3177. adapter->packet_filter |= ET131X_PACKET_TYPE_MULTICAST;
  3178. }
  3179. /* Set values in the private adapter struct */
  3180. i = 0;
  3181. netdev_for_each_mc_addr(ha, netdev) {
  3182. if (i == NIC_MAX_MCAST_LIST)
  3183. break;
  3184. ether_addr_copy(adapter->multicast_list[i++], ha->addr);
  3185. }
  3186. adapter->multicast_addr_count = i;
  3187. /* Are the new flags different from the previous ones? If not, then no
  3188. * action is required
  3189. *
  3190. * NOTE - This block will always update the multicast_list with the
  3191. * hardware, even if the addresses aren't the same.
  3192. */
  3193. if (packet_filter != adapter->packet_filter)
  3194. et131x_set_packet_filter(adapter);
  3195. }
  3196. static netdev_tx_t et131x_tx(struct sk_buff *skb, struct net_device *netdev)
  3197. {
  3198. struct et131x_adapter *adapter = netdev_priv(netdev);
  3199. struct tx_ring *tx_ring = &adapter->tx_ring;
  3200. /* stop the queue if it's getting full */
  3201. if (tx_ring->used >= NUM_TCB - 1 && !netif_queue_stopped(netdev))
  3202. netif_stop_queue(netdev);
  3203. /* Save the timestamp for the TX timeout watchdog */
  3204. netdev->trans_start = jiffies;
  3205. /* TCB is not available */
  3206. if (tx_ring->used >= NUM_TCB)
  3207. goto drop_err;
  3208. if ((adapter->flags & FMP_ADAPTER_FAIL_SEND_MASK) ||
  3209. !netif_carrier_ok(netdev))
  3210. goto drop_err;
  3211. if (send_packet(skb, adapter))
  3212. goto drop_err;
  3213. return NETDEV_TX_OK;
  3214. drop_err:
  3215. dev_kfree_skb_any(skb);
  3216. adapter->netdev->stats.tx_dropped++;
  3217. return NETDEV_TX_OK;
  3218. }
  3219. /* et131x_tx_timeout - Timeout handler
  3220. *
  3221. * The handler called when a Tx request times out. The timeout period is
  3222. * specified by the 'tx_timeo" element in the net_device structure (see
  3223. * et131x_alloc_device() to see how this value is set).
  3224. */
  3225. static void et131x_tx_timeout(struct net_device *netdev)
  3226. {
  3227. struct et131x_adapter *adapter = netdev_priv(netdev);
  3228. struct tx_ring *tx_ring = &adapter->tx_ring;
  3229. struct tcb *tcb;
  3230. unsigned long flags;
  3231. /* If the device is closed, ignore the timeout */
  3232. if (~(adapter->flags & FMP_ADAPTER_INTERRUPT_IN_USE))
  3233. return;
  3234. /* Any nonrecoverable hardware error?
  3235. * Checks adapter->flags for any failure in phy reading
  3236. */
  3237. if (adapter->flags & FMP_ADAPTER_NON_RECOVER_ERROR)
  3238. return;
  3239. /* Hardware failure? */
  3240. if (adapter->flags & FMP_ADAPTER_HARDWARE_ERROR) {
  3241. dev_err(&adapter->pdev->dev, "hardware error - reset\n");
  3242. return;
  3243. }
  3244. /* Is send stuck? */
  3245. spin_lock_irqsave(&adapter->tcb_send_qlock, flags);
  3246. tcb = tx_ring->send_head;
  3247. spin_unlock_irqrestore(&adapter->tcb_send_qlock, flags);
  3248. if (tcb) {
  3249. tcb->count++;
  3250. if (tcb->count > NIC_SEND_HANG_THRESHOLD) {
  3251. dev_warn(&adapter->pdev->dev,
  3252. "Send stuck - reset. tcb->WrIndex %x\n",
  3253. tcb->index);
  3254. adapter->netdev->stats.tx_errors++;
  3255. /* perform reset of tx/rx */
  3256. et131x_disable_txrx(netdev);
  3257. et131x_enable_txrx(netdev);
  3258. }
  3259. }
  3260. }
  3261. static int et131x_change_mtu(struct net_device *netdev, int new_mtu)
  3262. {
  3263. int result = 0;
  3264. struct et131x_adapter *adapter = netdev_priv(netdev);
  3265. if (new_mtu < 64 || new_mtu > 9216)
  3266. return -EINVAL;
  3267. et131x_disable_txrx(netdev);
  3268. netdev->mtu = new_mtu;
  3269. et131x_adapter_memory_free(adapter);
  3270. /* Set the config parameter for Jumbo Packet support */
  3271. adapter->registry_jumbo_packet = new_mtu + 14;
  3272. et131x_soft_reset(adapter);
  3273. result = et131x_adapter_memory_alloc(adapter);
  3274. if (result != 0) {
  3275. dev_warn(&adapter->pdev->dev,
  3276. "Change MTU failed; couldn't re-alloc DMA memory\n");
  3277. return result;
  3278. }
  3279. et131x_init_send(adapter);
  3280. et131x_hwaddr_init(adapter);
  3281. ether_addr_copy(netdev->dev_addr, adapter->addr);
  3282. /* Init the device with the new settings */
  3283. et131x_adapter_setup(adapter);
  3284. et131x_enable_txrx(netdev);
  3285. return result;
  3286. }
  3287. static const struct net_device_ops et131x_netdev_ops = {
  3288. .ndo_open = et131x_open,
  3289. .ndo_stop = et131x_close,
  3290. .ndo_start_xmit = et131x_tx,
  3291. .ndo_set_rx_mode = et131x_multicast,
  3292. .ndo_tx_timeout = et131x_tx_timeout,
  3293. .ndo_change_mtu = et131x_change_mtu,
  3294. .ndo_set_mac_address = eth_mac_addr,
  3295. .ndo_validate_addr = eth_validate_addr,
  3296. .ndo_get_stats = et131x_stats,
  3297. .ndo_do_ioctl = et131x_ioctl,
  3298. };
  3299. static int et131x_pci_setup(struct pci_dev *pdev,
  3300. const struct pci_device_id *ent)
  3301. {
  3302. struct net_device *netdev;
  3303. struct et131x_adapter *adapter;
  3304. int rc;
  3305. int ii;
  3306. rc = pci_enable_device(pdev);
  3307. if (rc < 0) {
  3308. dev_err(&pdev->dev, "pci_enable_device() failed\n");
  3309. goto out;
  3310. }
  3311. /* Perform some basic PCI checks */
  3312. if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
  3313. dev_err(&pdev->dev, "Can't find PCI device's base address\n");
  3314. rc = -ENODEV;
  3315. goto err_disable;
  3316. }
  3317. rc = pci_request_regions(pdev, DRIVER_NAME);
  3318. if (rc < 0) {
  3319. dev_err(&pdev->dev, "Can't get PCI resources\n");
  3320. goto err_disable;
  3321. }
  3322. pci_set_master(pdev);
  3323. /* Check the DMA addressing support of this device */
  3324. if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) &&
  3325. dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32))) {
  3326. dev_err(&pdev->dev, "No usable DMA addressing method\n");
  3327. rc = -EIO;
  3328. goto err_release_res;
  3329. }
  3330. netdev = alloc_etherdev(sizeof(struct et131x_adapter));
  3331. if (!netdev) {
  3332. dev_err(&pdev->dev, "Couldn't alloc netdev struct\n");
  3333. rc = -ENOMEM;
  3334. goto err_release_res;
  3335. }
  3336. netdev->watchdog_timeo = ET131X_TX_TIMEOUT;
  3337. netdev->netdev_ops = &et131x_netdev_ops;
  3338. SET_NETDEV_DEV(netdev, &pdev->dev);
  3339. netdev->ethtool_ops = &et131x_ethtool_ops;
  3340. adapter = et131x_adapter_init(netdev, pdev);
  3341. rc = et131x_pci_init(adapter, pdev);
  3342. if (rc < 0)
  3343. goto err_free_dev;
  3344. /* Map the bus-relative registers to system virtual memory */
  3345. adapter->regs = pci_ioremap_bar(pdev, 0);
  3346. if (!adapter->regs) {
  3347. dev_err(&pdev->dev, "Cannot map device registers\n");
  3348. rc = -ENOMEM;
  3349. goto err_free_dev;
  3350. }
  3351. /* If Phy COMA mode was enabled when we went down, disable it here. */
  3352. writel(ET_PMCSR_INIT, &adapter->regs->global.pm_csr);
  3353. et131x_soft_reset(adapter);
  3354. et131x_disable_interrupts(adapter);
  3355. rc = et131x_adapter_memory_alloc(adapter);
  3356. if (rc < 0) {
  3357. dev_err(&pdev->dev, "Could not alloc adapter memory (DMA)\n");
  3358. goto err_iounmap;
  3359. }
  3360. et131x_init_send(adapter);
  3361. netif_napi_add(netdev, &adapter->napi, et131x_poll, 64);
  3362. ether_addr_copy(netdev->dev_addr, adapter->addr);
  3363. rc = -ENOMEM;
  3364. adapter->mii_bus = mdiobus_alloc();
  3365. if (!adapter->mii_bus) {
  3366. dev_err(&pdev->dev, "Alloc of mii_bus struct failed\n");
  3367. goto err_mem_free;
  3368. }
  3369. adapter->mii_bus->name = "et131x_eth_mii";
  3370. snprintf(adapter->mii_bus->id, MII_BUS_ID_SIZE, "%x",
  3371. (adapter->pdev->bus->number << 8) | adapter->pdev->devfn);
  3372. adapter->mii_bus->priv = netdev;
  3373. adapter->mii_bus->read = et131x_mdio_read;
  3374. adapter->mii_bus->write = et131x_mdio_write;
  3375. adapter->mii_bus->irq = kmalloc_array(PHY_MAX_ADDR, sizeof(int),
  3376. GFP_KERNEL);
  3377. if (!adapter->mii_bus->irq)
  3378. goto err_mdio_free;
  3379. for (ii = 0; ii < PHY_MAX_ADDR; ii++)
  3380. adapter->mii_bus->irq[ii] = PHY_POLL;
  3381. rc = mdiobus_register(adapter->mii_bus);
  3382. if (rc < 0) {
  3383. dev_err(&pdev->dev, "failed to register MII bus\n");
  3384. goto err_mdio_free_irq;
  3385. }
  3386. rc = et131x_mii_probe(netdev);
  3387. if (rc < 0) {
  3388. dev_err(&pdev->dev, "failed to probe MII bus\n");
  3389. goto err_mdio_unregister;
  3390. }
  3391. et131x_adapter_setup(adapter);
  3392. /* Init variable for counting how long we do not have link status */
  3393. adapter->boot_coma = 0;
  3394. et1310_disable_phy_coma(adapter);
  3395. /* We can enable interrupts now
  3396. *
  3397. * NOTE - Because registration of interrupt handler is done in the
  3398. * device's open(), defer enabling device interrupts to that
  3399. * point
  3400. */
  3401. rc = register_netdev(netdev);
  3402. if (rc < 0) {
  3403. dev_err(&pdev->dev, "register_netdev() failed\n");
  3404. goto err_phy_disconnect;
  3405. }
  3406. /* Register the net_device struct with the PCI subsystem. Save a copy
  3407. * of the PCI config space for this device now that the device has
  3408. * been initialized, just in case it needs to be quickly restored.
  3409. */
  3410. pci_set_drvdata(pdev, netdev);
  3411. out:
  3412. return rc;
  3413. err_phy_disconnect:
  3414. phy_disconnect(adapter->phydev);
  3415. err_mdio_unregister:
  3416. mdiobus_unregister(adapter->mii_bus);
  3417. err_mdio_free_irq:
  3418. kfree(adapter->mii_bus->irq);
  3419. err_mdio_free:
  3420. mdiobus_free(adapter->mii_bus);
  3421. err_mem_free:
  3422. et131x_adapter_memory_free(adapter);
  3423. err_iounmap:
  3424. iounmap(adapter->regs);
  3425. err_free_dev:
  3426. pci_dev_put(pdev);
  3427. free_netdev(netdev);
  3428. err_release_res:
  3429. pci_release_regions(pdev);
  3430. err_disable:
  3431. pci_disable_device(pdev);
  3432. goto out;
  3433. }
  3434. static const struct pci_device_id et131x_pci_table[] = {
  3435. { PCI_VDEVICE(ATT, ET131X_PCI_DEVICE_ID_GIG), 0UL},
  3436. { PCI_VDEVICE(ATT, ET131X_PCI_DEVICE_ID_FAST), 0UL},
  3437. { 0,}
  3438. };
  3439. MODULE_DEVICE_TABLE(pci, et131x_pci_table);
  3440. static struct pci_driver et131x_driver = {
  3441. .name = DRIVER_NAME,
  3442. .id_table = et131x_pci_table,
  3443. .probe = et131x_pci_setup,
  3444. .remove = et131x_pci_remove,
  3445. .driver.pm = &et131x_pm_ops,
  3446. };
  3447. module_pci_driver(et131x_driver);