et131x.c 117 KB

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