mv643xx_eth.c 96 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283
  1. /*
  2. * Driver for Marvell Discovery (MV643XX) and Marvell Orion ethernet ports
  3. * Copyright (C) 2002 Matthew Dharm <mdharm@momenco.com>
  4. *
  5. * Based on the 64360 driver from:
  6. * Copyright (C) 2002 rabeeh@galileo.co.il
  7. *
  8. * Copyright (C) 2003 PMC-Sierra, Inc.,
  9. * written by Manish Lachwani
  10. *
  11. * Copyright (C) 2003 Ralf Baechle <ralf@linux-mips.org>
  12. *
  13. * Copyright (C) 2004-2006 MontaVista Software, Inc.
  14. * Dale Farnsworth <dale@farnsworth.org>
  15. *
  16. * Copyright (C) 2004 Steven J. Hill <sjhill1@rockwellcollins.com>
  17. * <sjhill@realitydiluted.com>
  18. *
  19. * This program is free software; you can redistribute it and/or
  20. * modify it under the terms of the GNU General Public License
  21. * as published by the Free Software Foundation; either version 2
  22. * of the License, or (at your option) any later version.
  23. *
  24. * This program is distributed in the hope that it will be useful,
  25. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27. * GNU General Public License for more details.
  28. *
  29. * You should have received a copy of the GNU General Public License
  30. * along with this program; if not, write to the Free Software
  31. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  32. */
  33. #include <linux/init.h>
  34. #include <linux/dma-mapping.h>
  35. #include <linux/in.h>
  36. #include <linux/ip.h>
  37. #include <linux/tcp.h>
  38. #include <linux/udp.h>
  39. #include <linux/etherdevice.h>
  40. #include <linux/bitops.h>
  41. #include <linux/delay.h>
  42. #include <linux/ethtool.h>
  43. #include <linux/platform_device.h>
  44. #include <linux/module.h>
  45. #include <linux/kernel.h>
  46. #include <linux/spinlock.h>
  47. #include <linux/workqueue.h>
  48. #include <linux/mii.h>
  49. #include <linux/mv643xx_eth.h>
  50. #include <asm/io.h>
  51. #include <asm/types.h>
  52. #include <asm/pgtable.h>
  53. #include <asm/system.h>
  54. #include <asm/delay.h>
  55. #include <asm/dma-mapping.h>
  56. #define MV643XX_CHECKSUM_OFFLOAD_TX
  57. #define MV643XX_NAPI
  58. #define MV643XX_TX_FAST_REFILL
  59. #undef MV643XX_COAL
  60. /*
  61. * Number of RX / TX descriptors on RX / TX rings.
  62. * Note that allocating RX descriptors is done by allocating the RX
  63. * ring AND a preallocated RX buffers (skb's) for each descriptor.
  64. * The TX descriptors only allocates the TX descriptors ring,
  65. * with no pre allocated TX buffers (skb's are allocated by higher layers.
  66. */
  67. /* Default TX ring size is 1000 descriptors */
  68. #define MV643XX_DEFAULT_TX_QUEUE_SIZE 1000
  69. /* Default RX ring size is 400 descriptors */
  70. #define MV643XX_DEFAULT_RX_QUEUE_SIZE 400
  71. #define MV643XX_TX_COAL 100
  72. #ifdef MV643XX_COAL
  73. #define MV643XX_RX_COAL 100
  74. #endif
  75. #ifdef MV643XX_CHECKSUM_OFFLOAD_TX
  76. #define MAX_DESCS_PER_SKB (MAX_SKB_FRAGS + 1)
  77. #else
  78. #define MAX_DESCS_PER_SKB 1
  79. #endif
  80. #define ETH_VLAN_HLEN 4
  81. #define ETH_FCS_LEN 4
  82. #define ETH_HW_IP_ALIGN 2 /* hw aligns IP header */
  83. #define ETH_WRAPPER_LEN (ETH_HW_IP_ALIGN + ETH_HLEN + \
  84. ETH_VLAN_HLEN + ETH_FCS_LEN)
  85. #define ETH_RX_SKB_SIZE (dev->mtu + ETH_WRAPPER_LEN + \
  86. dma_get_cache_alignment())
  87. /*
  88. * Registers shared between all ports.
  89. */
  90. #define PHY_ADDR_REG 0x0000
  91. #define SMI_REG 0x0004
  92. /*
  93. * Per-port registers.
  94. */
  95. #define PORT_CONFIG_REG(p) (0x0400 + ((p) << 10))
  96. #define PORT_CONFIG_EXTEND_REG(p) (0x0404 + ((p) << 10))
  97. #define MAC_ADDR_LOW(p) (0x0414 + ((p) << 10))
  98. #define MAC_ADDR_HIGH(p) (0x0418 + ((p) << 10))
  99. #define SDMA_CONFIG_REG(p) (0x041c + ((p) << 10))
  100. #define PORT_SERIAL_CONTROL_REG(p) (0x043c + ((p) << 10))
  101. #define PORT_STATUS_REG(p) (0x0444 + ((p) << 10))
  102. #define TRANSMIT_QUEUE_COMMAND_REG(p) (0x0448 + ((p) << 10))
  103. #define MAXIMUM_TRANSMIT_UNIT(p) (0x0458 + ((p) << 10))
  104. #define INTERRUPT_CAUSE_REG(p) (0x0460 + ((p) << 10))
  105. #define INTERRUPT_CAUSE_EXTEND_REG(p) (0x0464 + ((p) << 10))
  106. #define INTERRUPT_MASK_REG(p) (0x0468 + ((p) << 10))
  107. #define INTERRUPT_EXTEND_MASK_REG(p) (0x046c + ((p) << 10))
  108. #define TX_FIFO_URGENT_THRESHOLD_REG(p) (0x0474 + ((p) << 10))
  109. #define RX_CURRENT_QUEUE_DESC_PTR_0(p) (0x060c + ((p) << 10))
  110. #define RECEIVE_QUEUE_COMMAND_REG(p) (0x0680 + ((p) << 10))
  111. #define TX_CURRENT_QUEUE_DESC_PTR_0(p) (0x06c0 + ((p) << 10))
  112. #define MIB_COUNTERS_BASE(p) (0x1000 + ((p) << 7))
  113. #define DA_FILTER_SPECIAL_MULTICAST_TABLE_BASE(p) (0x1400 + ((p) << 10))
  114. #define DA_FILTER_OTHER_MULTICAST_TABLE_BASE(p) (0x1500 + ((p) << 10))
  115. #define DA_FILTER_UNICAST_TABLE_BASE(p) (0x1600 + ((p) << 10))
  116. /* These macros describe Ethernet Port configuration reg (Px_cR) bits */
  117. #define UNICAST_NORMAL_MODE (0 << 0)
  118. #define UNICAST_PROMISCUOUS_MODE (1 << 0)
  119. #define DEFAULT_RX_QUEUE(queue) ((queue) << 1)
  120. #define DEFAULT_RX_ARP_QUEUE(queue) ((queue) << 4)
  121. #define RECEIVE_BC_IF_NOT_IP_OR_ARP (0 << 7)
  122. #define REJECT_BC_IF_NOT_IP_OR_ARP (1 << 7)
  123. #define RECEIVE_BC_IF_IP (0 << 8)
  124. #define REJECT_BC_IF_IP (1 << 8)
  125. #define RECEIVE_BC_IF_ARP (0 << 9)
  126. #define REJECT_BC_IF_ARP (1 << 9)
  127. #define TX_AM_NO_UPDATE_ERROR_SUMMARY (1 << 12)
  128. #define CAPTURE_TCP_FRAMES_DIS (0 << 14)
  129. #define CAPTURE_TCP_FRAMES_EN (1 << 14)
  130. #define CAPTURE_UDP_FRAMES_DIS (0 << 15)
  131. #define CAPTURE_UDP_FRAMES_EN (1 << 15)
  132. #define DEFAULT_RX_TCP_QUEUE(queue) ((queue) << 16)
  133. #define DEFAULT_RX_UDP_QUEUE(queue) ((queue) << 19)
  134. #define DEFAULT_RX_BPDU_QUEUE(queue) ((queue) << 22)
  135. #define PORT_CONFIG_DEFAULT_VALUE \
  136. UNICAST_NORMAL_MODE | \
  137. DEFAULT_RX_QUEUE(0) | \
  138. DEFAULT_RX_ARP_QUEUE(0) | \
  139. RECEIVE_BC_IF_NOT_IP_OR_ARP | \
  140. RECEIVE_BC_IF_IP | \
  141. RECEIVE_BC_IF_ARP | \
  142. CAPTURE_TCP_FRAMES_DIS | \
  143. CAPTURE_UDP_FRAMES_DIS | \
  144. DEFAULT_RX_TCP_QUEUE(0) | \
  145. DEFAULT_RX_UDP_QUEUE(0) | \
  146. DEFAULT_RX_BPDU_QUEUE(0)
  147. /* These macros describe Ethernet Port configuration extend reg (Px_cXR) bits*/
  148. #define CLASSIFY_EN (1 << 0)
  149. #define SPAN_BPDU_PACKETS_AS_NORMAL (0 << 1)
  150. #define SPAN_BPDU_PACKETS_TO_RX_QUEUE_7 (1 << 1)
  151. #define PARTITION_DISABLE (0 << 2)
  152. #define PARTITION_ENABLE (1 << 2)
  153. #define PORT_CONFIG_EXTEND_DEFAULT_VALUE \
  154. SPAN_BPDU_PACKETS_AS_NORMAL | \
  155. PARTITION_DISABLE
  156. /* These macros describe Ethernet Port Sdma configuration reg (SDCR) bits */
  157. #define RIFB (1 << 0)
  158. #define RX_BURST_SIZE_1_64BIT (0 << 1)
  159. #define RX_BURST_SIZE_2_64BIT (1 << 1)
  160. #define RX_BURST_SIZE_4_64BIT (2 << 1)
  161. #define RX_BURST_SIZE_8_64BIT (3 << 1)
  162. #define RX_BURST_SIZE_16_64BIT (4 << 1)
  163. #define BLM_RX_NO_SWAP (1 << 4)
  164. #define BLM_RX_BYTE_SWAP (0 << 4)
  165. #define BLM_TX_NO_SWAP (1 << 5)
  166. #define BLM_TX_BYTE_SWAP (0 << 5)
  167. #define DESCRIPTORS_BYTE_SWAP (1 << 6)
  168. #define DESCRIPTORS_NO_SWAP (0 << 6)
  169. #define IPG_INT_RX(value) (((value) & 0x3fff) << 8)
  170. #define TX_BURST_SIZE_1_64BIT (0 << 22)
  171. #define TX_BURST_SIZE_2_64BIT (1 << 22)
  172. #define TX_BURST_SIZE_4_64BIT (2 << 22)
  173. #define TX_BURST_SIZE_8_64BIT (3 << 22)
  174. #define TX_BURST_SIZE_16_64BIT (4 << 22)
  175. #if defined(__BIG_ENDIAN)
  176. #define PORT_SDMA_CONFIG_DEFAULT_VALUE \
  177. RX_BURST_SIZE_4_64BIT | \
  178. IPG_INT_RX(0) | \
  179. TX_BURST_SIZE_4_64BIT
  180. #elif defined(__LITTLE_ENDIAN)
  181. #define PORT_SDMA_CONFIG_DEFAULT_VALUE \
  182. RX_BURST_SIZE_4_64BIT | \
  183. BLM_RX_NO_SWAP | \
  184. BLM_TX_NO_SWAP | \
  185. IPG_INT_RX(0) | \
  186. TX_BURST_SIZE_4_64BIT
  187. #else
  188. #error One of __BIG_ENDIAN or __LITTLE_ENDIAN must be defined
  189. #endif
  190. /* These macros describe Ethernet Port serial control reg (PSCR) bits */
  191. #define SERIAL_PORT_DISABLE (0 << 0)
  192. #define SERIAL_PORT_ENABLE (1 << 0)
  193. #define DO_NOT_FORCE_LINK_PASS (0 << 1)
  194. #define FORCE_LINK_PASS (1 << 1)
  195. #define ENABLE_AUTO_NEG_FOR_DUPLX (0 << 2)
  196. #define DISABLE_AUTO_NEG_FOR_DUPLX (1 << 2)
  197. #define ENABLE_AUTO_NEG_FOR_FLOW_CTRL (0 << 3)
  198. #define DISABLE_AUTO_NEG_FOR_FLOW_CTRL (1 << 3)
  199. #define ADV_NO_FLOW_CTRL (0 << 4)
  200. #define ADV_SYMMETRIC_FLOW_CTRL (1 << 4)
  201. #define FORCE_FC_MODE_NO_PAUSE_DIS_TX (0 << 5)
  202. #define FORCE_FC_MODE_TX_PAUSE_DIS (1 << 5)
  203. #define FORCE_BP_MODE_NO_JAM (0 << 7)
  204. #define FORCE_BP_MODE_JAM_TX (1 << 7)
  205. #define FORCE_BP_MODE_JAM_TX_ON_RX_ERR (2 << 7)
  206. #define SERIAL_PORT_CONTROL_RESERVED (1 << 9)
  207. #define FORCE_LINK_FAIL (0 << 10)
  208. #define DO_NOT_FORCE_LINK_FAIL (1 << 10)
  209. #define RETRANSMIT_16_ATTEMPTS (0 << 11)
  210. #define RETRANSMIT_FOREVER (1 << 11)
  211. #define ENABLE_AUTO_NEG_SPEED_GMII (0 << 13)
  212. #define DISABLE_AUTO_NEG_SPEED_GMII (1 << 13)
  213. #define DTE_ADV_0 (0 << 14)
  214. #define DTE_ADV_1 (1 << 14)
  215. #define DISABLE_AUTO_NEG_BYPASS (0 << 15)
  216. #define ENABLE_AUTO_NEG_BYPASS (1 << 15)
  217. #define AUTO_NEG_NO_CHANGE (0 << 16)
  218. #define RESTART_AUTO_NEG (1 << 16)
  219. #define MAX_RX_PACKET_1518BYTE (0 << 17)
  220. #define MAX_RX_PACKET_1522BYTE (1 << 17)
  221. #define MAX_RX_PACKET_1552BYTE (2 << 17)
  222. #define MAX_RX_PACKET_9022BYTE (3 << 17)
  223. #define MAX_RX_PACKET_9192BYTE (4 << 17)
  224. #define MAX_RX_PACKET_9700BYTE (5 << 17)
  225. #define MAX_RX_PACKET_MASK (7 << 17)
  226. #define CLR_EXT_LOOPBACK (0 << 20)
  227. #define SET_EXT_LOOPBACK (1 << 20)
  228. #define SET_HALF_DUPLEX_MODE (0 << 21)
  229. #define SET_FULL_DUPLEX_MODE (1 << 21)
  230. #define DISABLE_FLOW_CTRL_TX_RX_IN_FULL_DUPLEX (0 << 22)
  231. #define ENABLE_FLOW_CTRL_TX_RX_IN_FULL_DUPLEX (1 << 22)
  232. #define SET_GMII_SPEED_TO_10_100 (0 << 23)
  233. #define SET_GMII_SPEED_TO_1000 (1 << 23)
  234. #define SET_MII_SPEED_TO_10 (0 << 24)
  235. #define SET_MII_SPEED_TO_100 (1 << 24)
  236. #define PORT_SERIAL_CONTROL_DEFAULT_VALUE \
  237. DO_NOT_FORCE_LINK_PASS | \
  238. ENABLE_AUTO_NEG_FOR_DUPLX | \
  239. DISABLE_AUTO_NEG_FOR_FLOW_CTRL | \
  240. ADV_SYMMETRIC_FLOW_CTRL | \
  241. FORCE_FC_MODE_NO_PAUSE_DIS_TX | \
  242. FORCE_BP_MODE_NO_JAM | \
  243. (1 << 9) /* reserved */ | \
  244. DO_NOT_FORCE_LINK_FAIL | \
  245. RETRANSMIT_16_ATTEMPTS | \
  246. ENABLE_AUTO_NEG_SPEED_GMII | \
  247. DTE_ADV_0 | \
  248. DISABLE_AUTO_NEG_BYPASS | \
  249. AUTO_NEG_NO_CHANGE | \
  250. MAX_RX_PACKET_9700BYTE | \
  251. CLR_EXT_LOOPBACK | \
  252. SET_FULL_DUPLEX_MODE | \
  253. ENABLE_FLOW_CTRL_TX_RX_IN_FULL_DUPLEX
  254. /* These macros describe Ethernet Serial Status reg (PSR) bits */
  255. #define PORT_STATUS_MODE_10_BIT (1 << 0)
  256. #define PORT_STATUS_LINK_UP (1 << 1)
  257. #define PORT_STATUS_FULL_DUPLEX (1 << 2)
  258. #define PORT_STATUS_FLOW_CONTROL (1 << 3)
  259. #define PORT_STATUS_GMII_1000 (1 << 4)
  260. #define PORT_STATUS_MII_100 (1 << 5)
  261. /* PSR bit 6 is undocumented */
  262. #define PORT_STATUS_TX_IN_PROGRESS (1 << 7)
  263. #define PORT_STATUS_AUTONEG_BYPASSED (1 << 8)
  264. #define PORT_STATUS_PARTITION (1 << 9)
  265. #define PORT_STATUS_TX_FIFO_EMPTY (1 << 10)
  266. /* PSR bits 11-31 are reserved */
  267. #define PORT_DEFAULT_TRANSMIT_QUEUE_SIZE 800
  268. #define PORT_DEFAULT_RECEIVE_QUEUE_SIZE 400
  269. #define DESC_SIZE 64
  270. #define ETH_RX_QUEUES_ENABLED (1 << 0) /* use only Q0 for receive */
  271. #define ETH_TX_QUEUES_ENABLED (1 << 0) /* use only Q0 for transmit */
  272. #define ETH_INT_CAUSE_RX_DONE (ETH_RX_QUEUES_ENABLED << 2)
  273. #define ETH_INT_CAUSE_RX_ERROR (ETH_RX_QUEUES_ENABLED << 9)
  274. #define ETH_INT_CAUSE_RX (ETH_INT_CAUSE_RX_DONE | ETH_INT_CAUSE_RX_ERROR)
  275. #define ETH_INT_CAUSE_EXT 0x00000002
  276. #define ETH_INT_UNMASK_ALL (ETH_INT_CAUSE_RX | ETH_INT_CAUSE_EXT)
  277. #define ETH_INT_CAUSE_TX_DONE (ETH_TX_QUEUES_ENABLED << 0)
  278. #define ETH_INT_CAUSE_TX_ERROR (ETH_TX_QUEUES_ENABLED << 8)
  279. #define ETH_INT_CAUSE_TX (ETH_INT_CAUSE_TX_DONE | ETH_INT_CAUSE_TX_ERROR)
  280. #define ETH_INT_CAUSE_PHY 0x00010000
  281. #define ETH_INT_CAUSE_STATE 0x00100000
  282. #define ETH_INT_UNMASK_ALL_EXT (ETH_INT_CAUSE_TX | ETH_INT_CAUSE_PHY | \
  283. ETH_INT_CAUSE_STATE)
  284. #define ETH_INT_MASK_ALL 0x00000000
  285. #define ETH_INT_MASK_ALL_EXT 0x00000000
  286. #define PHY_WAIT_ITERATIONS 1000 /* 1000 iterations * 10uS = 10mS max */
  287. #define PHY_WAIT_MICRO_SECONDS 10
  288. /* Buffer offset from buffer pointer */
  289. #define RX_BUF_OFFSET 0x2
  290. /* Gigabit Ethernet Unit Global Registers */
  291. /* MIB Counters register definitions */
  292. #define ETH_MIB_GOOD_OCTETS_RECEIVED_LOW 0x0
  293. #define ETH_MIB_GOOD_OCTETS_RECEIVED_HIGH 0x4
  294. #define ETH_MIB_BAD_OCTETS_RECEIVED 0x8
  295. #define ETH_MIB_INTERNAL_MAC_TRANSMIT_ERR 0xc
  296. #define ETH_MIB_GOOD_FRAMES_RECEIVED 0x10
  297. #define ETH_MIB_BAD_FRAMES_RECEIVED 0x14
  298. #define ETH_MIB_BROADCAST_FRAMES_RECEIVED 0x18
  299. #define ETH_MIB_MULTICAST_FRAMES_RECEIVED 0x1c
  300. #define ETH_MIB_FRAMES_64_OCTETS 0x20
  301. #define ETH_MIB_FRAMES_65_TO_127_OCTETS 0x24
  302. #define ETH_MIB_FRAMES_128_TO_255_OCTETS 0x28
  303. #define ETH_MIB_FRAMES_256_TO_511_OCTETS 0x2c
  304. #define ETH_MIB_FRAMES_512_TO_1023_OCTETS 0x30
  305. #define ETH_MIB_FRAMES_1024_TO_MAX_OCTETS 0x34
  306. #define ETH_MIB_GOOD_OCTETS_SENT_LOW 0x38
  307. #define ETH_MIB_GOOD_OCTETS_SENT_HIGH 0x3c
  308. #define ETH_MIB_GOOD_FRAMES_SENT 0x40
  309. #define ETH_MIB_EXCESSIVE_COLLISION 0x44
  310. #define ETH_MIB_MULTICAST_FRAMES_SENT 0x48
  311. #define ETH_MIB_BROADCAST_FRAMES_SENT 0x4c
  312. #define ETH_MIB_UNREC_MAC_CONTROL_RECEIVED 0x50
  313. #define ETH_MIB_FC_SENT 0x54
  314. #define ETH_MIB_GOOD_FC_RECEIVED 0x58
  315. #define ETH_MIB_BAD_FC_RECEIVED 0x5c
  316. #define ETH_MIB_UNDERSIZE_RECEIVED 0x60
  317. #define ETH_MIB_FRAGMENTS_RECEIVED 0x64
  318. #define ETH_MIB_OVERSIZE_RECEIVED 0x68
  319. #define ETH_MIB_JABBER_RECEIVED 0x6c
  320. #define ETH_MIB_MAC_RECEIVE_ERROR 0x70
  321. #define ETH_MIB_BAD_CRC_EVENT 0x74
  322. #define ETH_MIB_COLLISION 0x78
  323. #define ETH_MIB_LATE_COLLISION 0x7c
  324. /* Port serial status reg (PSR) */
  325. #define ETH_INTERFACE_PCM 0x00000001
  326. #define ETH_LINK_IS_UP 0x00000002
  327. #define ETH_PORT_AT_FULL_DUPLEX 0x00000004
  328. #define ETH_RX_FLOW_CTRL_ENABLED 0x00000008
  329. #define ETH_GMII_SPEED_1000 0x00000010
  330. #define ETH_MII_SPEED_100 0x00000020
  331. #define ETH_TX_IN_PROGRESS 0x00000080
  332. #define ETH_BYPASS_ACTIVE 0x00000100
  333. #define ETH_PORT_AT_PARTITION_STATE 0x00000200
  334. #define ETH_PORT_TX_FIFO_EMPTY 0x00000400
  335. /* SMI reg */
  336. #define ETH_SMI_BUSY 0x10000000 /* 0 - Write, 1 - Read */
  337. #define ETH_SMI_READ_VALID 0x08000000 /* 0 - Write, 1 - Read */
  338. #define ETH_SMI_OPCODE_WRITE 0 /* Completion of Read */
  339. #define ETH_SMI_OPCODE_READ 0x04000000 /* Operation is in progress */
  340. /* Interrupt Cause Register Bit Definitions */
  341. /* SDMA command status fields macros */
  342. /* Tx & Rx descriptors status */
  343. #define ETH_ERROR_SUMMARY 0x00000001
  344. /* Tx & Rx descriptors command */
  345. #define ETH_BUFFER_OWNED_BY_DMA 0x80000000
  346. /* Tx descriptors status */
  347. #define ETH_LC_ERROR 0
  348. #define ETH_UR_ERROR 0x00000002
  349. #define ETH_RL_ERROR 0x00000004
  350. #define ETH_LLC_SNAP_FORMAT 0x00000200
  351. /* Rx descriptors status */
  352. #define ETH_OVERRUN_ERROR 0x00000002
  353. #define ETH_MAX_FRAME_LENGTH_ERROR 0x00000004
  354. #define ETH_RESOURCE_ERROR 0x00000006
  355. #define ETH_VLAN_TAGGED 0x00080000
  356. #define ETH_BPDU_FRAME 0x00100000
  357. #define ETH_UDP_FRAME_OVER_IP_V_4 0x00200000
  358. #define ETH_OTHER_FRAME_TYPE 0x00400000
  359. #define ETH_LAYER_2_IS_ETH_V_2 0x00800000
  360. #define ETH_FRAME_TYPE_IP_V_4 0x01000000
  361. #define ETH_FRAME_HEADER_OK 0x02000000
  362. #define ETH_RX_LAST_DESC 0x04000000
  363. #define ETH_RX_FIRST_DESC 0x08000000
  364. #define ETH_UNKNOWN_DESTINATION_ADDR 0x10000000
  365. #define ETH_RX_ENABLE_INTERRUPT 0x20000000
  366. #define ETH_LAYER_4_CHECKSUM_OK 0x40000000
  367. /* Rx descriptors byte count */
  368. #define ETH_FRAME_FRAGMENTED 0x00000004
  369. /* Tx descriptors command */
  370. #define ETH_LAYER_4_CHECKSUM_FIRST_DESC 0x00000400
  371. #define ETH_FRAME_SET_TO_VLAN 0x00008000
  372. #define ETH_UDP_FRAME 0x00010000
  373. #define ETH_GEN_TCP_UDP_CHECKSUM 0x00020000
  374. #define ETH_GEN_IP_V_4_CHECKSUM 0x00040000
  375. #define ETH_ZERO_PADDING 0x00080000
  376. #define ETH_TX_LAST_DESC 0x00100000
  377. #define ETH_TX_FIRST_DESC 0x00200000
  378. #define ETH_GEN_CRC 0x00400000
  379. #define ETH_TX_ENABLE_INTERRUPT 0x00800000
  380. #define ETH_AUTO_MODE 0x40000000
  381. #define ETH_TX_IHL_SHIFT 11
  382. /* typedefs */
  383. typedef enum _eth_func_ret_status {
  384. ETH_OK, /* Returned as expected. */
  385. ETH_ERROR, /* Fundamental error. */
  386. ETH_RETRY, /* Could not process request. Try later.*/
  387. ETH_END_OF_JOB, /* Ring has nothing to process. */
  388. ETH_QUEUE_FULL, /* Ring resource error. */
  389. ETH_QUEUE_LAST_RESOURCE /* Ring resources about to exhaust. */
  390. } ETH_FUNC_RET_STATUS;
  391. typedef enum _eth_target {
  392. ETH_TARGET_DRAM,
  393. ETH_TARGET_DEVICE,
  394. ETH_TARGET_CBS,
  395. ETH_TARGET_PCI0,
  396. ETH_TARGET_PCI1
  397. } ETH_TARGET;
  398. /* These are for big-endian machines. Little endian needs different
  399. * definitions.
  400. */
  401. #if defined(__BIG_ENDIAN)
  402. struct eth_rx_desc {
  403. u16 byte_cnt; /* Descriptor buffer byte count */
  404. u16 buf_size; /* Buffer size */
  405. u32 cmd_sts; /* Descriptor command status */
  406. u32 next_desc_ptr; /* Next descriptor pointer */
  407. u32 buf_ptr; /* Descriptor buffer pointer */
  408. };
  409. struct eth_tx_desc {
  410. u16 byte_cnt; /* buffer byte count */
  411. u16 l4i_chk; /* CPU provided TCP checksum */
  412. u32 cmd_sts; /* Command/status field */
  413. u32 next_desc_ptr; /* Pointer to next descriptor */
  414. u32 buf_ptr; /* pointer to buffer for this descriptor*/
  415. };
  416. #elif defined(__LITTLE_ENDIAN)
  417. struct eth_rx_desc {
  418. u32 cmd_sts; /* Descriptor command status */
  419. u16 buf_size; /* Buffer size */
  420. u16 byte_cnt; /* Descriptor buffer byte count */
  421. u32 buf_ptr; /* Descriptor buffer pointer */
  422. u32 next_desc_ptr; /* Next descriptor pointer */
  423. };
  424. struct eth_tx_desc {
  425. u32 cmd_sts; /* Command/status field */
  426. u16 l4i_chk; /* CPU provided TCP checksum */
  427. u16 byte_cnt; /* buffer byte count */
  428. u32 buf_ptr; /* pointer to buffer for this descriptor*/
  429. u32 next_desc_ptr; /* Pointer to next descriptor */
  430. };
  431. #else
  432. #error One of __BIG_ENDIAN or __LITTLE_ENDIAN must be defined
  433. #endif
  434. /* Unified struct for Rx and Tx operations. The user is not required to */
  435. /* be familier with neither Tx nor Rx descriptors. */
  436. struct pkt_info {
  437. unsigned short byte_cnt; /* Descriptor buffer byte count */
  438. unsigned short l4i_chk; /* Tx CPU provided TCP Checksum */
  439. unsigned int cmd_sts; /* Descriptor command status */
  440. dma_addr_t buf_ptr; /* Descriptor buffer pointer */
  441. struct sk_buff *return_info; /* User resource return information */
  442. };
  443. /* Ethernet port specific information */
  444. struct mv643xx_mib_counters {
  445. u64 good_octets_received;
  446. u32 bad_octets_received;
  447. u32 internal_mac_transmit_err;
  448. u32 good_frames_received;
  449. u32 bad_frames_received;
  450. u32 broadcast_frames_received;
  451. u32 multicast_frames_received;
  452. u32 frames_64_octets;
  453. u32 frames_65_to_127_octets;
  454. u32 frames_128_to_255_octets;
  455. u32 frames_256_to_511_octets;
  456. u32 frames_512_to_1023_octets;
  457. u32 frames_1024_to_max_octets;
  458. u64 good_octets_sent;
  459. u32 good_frames_sent;
  460. u32 excessive_collision;
  461. u32 multicast_frames_sent;
  462. u32 broadcast_frames_sent;
  463. u32 unrec_mac_control_received;
  464. u32 fc_sent;
  465. u32 good_fc_received;
  466. u32 bad_fc_received;
  467. u32 undersize_received;
  468. u32 fragments_received;
  469. u32 oversize_received;
  470. u32 jabber_received;
  471. u32 mac_receive_error;
  472. u32 bad_crc_event;
  473. u32 collision;
  474. u32 late_collision;
  475. };
  476. struct mv643xx_private {
  477. int port_num; /* User Ethernet port number */
  478. u32 rx_sram_addr; /* Base address of rx sram area */
  479. u32 rx_sram_size; /* Size of rx sram area */
  480. u32 tx_sram_addr; /* Base address of tx sram area */
  481. u32 tx_sram_size; /* Size of tx sram area */
  482. int rx_resource_err; /* Rx ring resource error flag */
  483. /* Tx/Rx rings managment indexes fields. For driver use */
  484. /* Next available and first returning Rx resource */
  485. int rx_curr_desc_q, rx_used_desc_q;
  486. /* Next available and first returning Tx resource */
  487. int tx_curr_desc_q, tx_used_desc_q;
  488. #ifdef MV643XX_TX_FAST_REFILL
  489. u32 tx_clean_threshold;
  490. #endif
  491. struct eth_rx_desc *p_rx_desc_area;
  492. dma_addr_t rx_desc_dma;
  493. int rx_desc_area_size;
  494. struct sk_buff **rx_skb;
  495. struct eth_tx_desc *p_tx_desc_area;
  496. dma_addr_t tx_desc_dma;
  497. int tx_desc_area_size;
  498. struct sk_buff **tx_skb;
  499. struct work_struct tx_timeout_task;
  500. struct net_device *dev;
  501. struct napi_struct napi;
  502. struct net_device_stats stats;
  503. struct mv643xx_mib_counters mib_counters;
  504. spinlock_t lock;
  505. /* Size of Tx Ring per queue */
  506. int tx_ring_size;
  507. /* Number of tx descriptors in use */
  508. int tx_desc_count;
  509. /* Size of Rx Ring per queue */
  510. int rx_ring_size;
  511. /* Number of rx descriptors in use */
  512. int rx_desc_count;
  513. /*
  514. * Used in case RX Ring is empty, which can be caused when
  515. * system does not have resources (skb's)
  516. */
  517. struct timer_list timeout;
  518. u32 rx_int_coal;
  519. u32 tx_int_coal;
  520. struct mii_if_info mii;
  521. };
  522. /* Static function declarations */
  523. static void eth_port_init(struct mv643xx_private *mp);
  524. static void eth_port_reset(unsigned int eth_port_num);
  525. static void eth_port_start(struct net_device *dev);
  526. static void ethernet_phy_reset(unsigned int eth_port_num);
  527. static void eth_port_write_smi_reg(unsigned int eth_port_num,
  528. unsigned int phy_reg, unsigned int value);
  529. static void eth_port_read_smi_reg(unsigned int eth_port_num,
  530. unsigned int phy_reg, unsigned int *value);
  531. static void eth_clear_mib_counters(unsigned int eth_port_num);
  532. static ETH_FUNC_RET_STATUS eth_port_receive(struct mv643xx_private *mp,
  533. struct pkt_info *p_pkt_info);
  534. static ETH_FUNC_RET_STATUS eth_rx_return_buff(struct mv643xx_private *mp,
  535. struct pkt_info *p_pkt_info);
  536. static void eth_port_uc_addr_get(unsigned int port_num, unsigned char *p_addr);
  537. static void eth_port_uc_addr_set(unsigned int port_num, unsigned char *p_addr);
  538. static void eth_port_set_multicast_list(struct net_device *);
  539. static void mv643xx_eth_port_enable_tx(unsigned int port_num,
  540. unsigned int queues);
  541. static void mv643xx_eth_port_enable_rx(unsigned int port_num,
  542. unsigned int queues);
  543. static unsigned int mv643xx_eth_port_disable_tx(unsigned int port_num);
  544. static unsigned int mv643xx_eth_port_disable_rx(unsigned int port_num);
  545. static int mv643xx_eth_open(struct net_device *);
  546. static int mv643xx_eth_stop(struct net_device *);
  547. static int mv643xx_eth_change_mtu(struct net_device *, int);
  548. static void eth_port_init_mac_tables(unsigned int eth_port_num);
  549. #ifdef MV643XX_NAPI
  550. static int mv643xx_poll(struct napi_struct *napi, int budget);
  551. #endif
  552. static int ethernet_phy_get(unsigned int eth_port_num);
  553. static void ethernet_phy_set(unsigned int eth_port_num, int phy_addr);
  554. static int ethernet_phy_detect(unsigned int eth_port_num);
  555. static int mv643xx_mdio_read(struct net_device *dev, int phy_id, int location);
  556. static void mv643xx_mdio_write(struct net_device *dev, int phy_id, int location, int val);
  557. static int mv643xx_eth_do_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd);
  558. static const struct ethtool_ops mv643xx_ethtool_ops;
  559. static char mv643xx_driver_name[] = "mv643xx_eth";
  560. static char mv643xx_driver_version[] = "1.0";
  561. static void __iomem *mv643xx_eth_base;
  562. /* used to protect SMI_REG, which is shared across ports */
  563. static DEFINE_SPINLOCK(mv643xx_eth_phy_lock);
  564. static inline u32 mv_read(int offset)
  565. {
  566. return readl(mv643xx_eth_base + offset);
  567. }
  568. static inline void mv_write(int offset, u32 data)
  569. {
  570. writel(data, mv643xx_eth_base + offset);
  571. }
  572. /*
  573. * Changes MTU (maximum transfer unit) of the gigabit ethenret port
  574. *
  575. * Input : pointer to ethernet interface network device structure
  576. * new mtu size
  577. * Output : 0 upon success, -EINVAL upon failure
  578. */
  579. static int mv643xx_eth_change_mtu(struct net_device *dev, int new_mtu)
  580. {
  581. if ((new_mtu > 9500) || (new_mtu < 64))
  582. return -EINVAL;
  583. dev->mtu = new_mtu;
  584. /*
  585. * Stop then re-open the interface. This will allocate RX skb's with
  586. * the new MTU.
  587. * There is a possible danger that the open will not successed, due
  588. * to memory is full, which might fail the open function.
  589. */
  590. if (netif_running(dev)) {
  591. mv643xx_eth_stop(dev);
  592. if (mv643xx_eth_open(dev))
  593. printk(KERN_ERR
  594. "%s: Fatal error on opening device\n",
  595. dev->name);
  596. }
  597. return 0;
  598. }
  599. /*
  600. * mv643xx_eth_rx_refill_descs
  601. *
  602. * Fills / refills RX queue on a certain gigabit ethernet port
  603. *
  604. * Input : pointer to ethernet interface network device structure
  605. * Output : N/A
  606. */
  607. static void mv643xx_eth_rx_refill_descs(struct net_device *dev)
  608. {
  609. struct mv643xx_private *mp = netdev_priv(dev);
  610. struct pkt_info pkt_info;
  611. struct sk_buff *skb;
  612. int unaligned;
  613. while (mp->rx_desc_count < mp->rx_ring_size) {
  614. skb = dev_alloc_skb(ETH_RX_SKB_SIZE + dma_get_cache_alignment());
  615. if (!skb)
  616. break;
  617. mp->rx_desc_count++;
  618. unaligned = (u32)skb->data & (dma_get_cache_alignment() - 1);
  619. if (unaligned)
  620. skb_reserve(skb, dma_get_cache_alignment() - unaligned);
  621. pkt_info.cmd_sts = ETH_RX_ENABLE_INTERRUPT;
  622. pkt_info.byte_cnt = ETH_RX_SKB_SIZE;
  623. pkt_info.buf_ptr = dma_map_single(NULL, skb->data,
  624. ETH_RX_SKB_SIZE, DMA_FROM_DEVICE);
  625. pkt_info.return_info = skb;
  626. if (eth_rx_return_buff(mp, &pkt_info) != ETH_OK) {
  627. printk(KERN_ERR
  628. "%s: Error allocating RX Ring\n", dev->name);
  629. break;
  630. }
  631. skb_reserve(skb, ETH_HW_IP_ALIGN);
  632. }
  633. /*
  634. * If RX ring is empty of SKB, set a timer to try allocating
  635. * again at a later time.
  636. */
  637. if (mp->rx_desc_count == 0) {
  638. printk(KERN_INFO "%s: Rx ring is empty\n", dev->name);
  639. mp->timeout.expires = jiffies + (HZ / 10); /* 100 mSec */
  640. add_timer(&mp->timeout);
  641. }
  642. }
  643. /*
  644. * mv643xx_eth_rx_refill_descs_timer_wrapper
  645. *
  646. * Timer routine to wake up RX queue filling task. This function is
  647. * used only in case the RX queue is empty, and all alloc_skb has
  648. * failed (due to out of memory event).
  649. *
  650. * Input : pointer to ethernet interface network device structure
  651. * Output : N/A
  652. */
  653. static inline void mv643xx_eth_rx_refill_descs_timer_wrapper(unsigned long data)
  654. {
  655. mv643xx_eth_rx_refill_descs((struct net_device *)data);
  656. }
  657. /*
  658. * mv643xx_eth_update_mac_address
  659. *
  660. * Update the MAC address of the port in the address table
  661. *
  662. * Input : pointer to ethernet interface network device structure
  663. * Output : N/A
  664. */
  665. static void mv643xx_eth_update_mac_address(struct net_device *dev)
  666. {
  667. struct mv643xx_private *mp = netdev_priv(dev);
  668. unsigned int port_num = mp->port_num;
  669. eth_port_init_mac_tables(port_num);
  670. eth_port_uc_addr_set(port_num, dev->dev_addr);
  671. }
  672. /*
  673. * mv643xx_eth_set_rx_mode
  674. *
  675. * Change from promiscuos to regular rx mode
  676. *
  677. * Input : pointer to ethernet interface network device structure
  678. * Output : N/A
  679. */
  680. static void mv643xx_eth_set_rx_mode(struct net_device *dev)
  681. {
  682. struct mv643xx_private *mp = netdev_priv(dev);
  683. u32 config_reg;
  684. config_reg = mv_read(PORT_CONFIG_REG(mp->port_num));
  685. if (dev->flags & IFF_PROMISC)
  686. config_reg |= (u32) UNICAST_PROMISCUOUS_MODE;
  687. else
  688. config_reg &= ~(u32) UNICAST_PROMISCUOUS_MODE;
  689. mv_write(PORT_CONFIG_REG(mp->port_num), config_reg);
  690. eth_port_set_multicast_list(dev);
  691. }
  692. /*
  693. * mv643xx_eth_set_mac_address
  694. *
  695. * Change the interface's mac address.
  696. * No special hardware thing should be done because interface is always
  697. * put in promiscuous mode.
  698. *
  699. * Input : pointer to ethernet interface network device structure and
  700. * a pointer to the designated entry to be added to the cache.
  701. * Output : zero upon success, negative upon failure
  702. */
  703. static int mv643xx_eth_set_mac_address(struct net_device *dev, void *addr)
  704. {
  705. int i;
  706. for (i = 0; i < 6; i++)
  707. /* +2 is for the offset of the HW addr type */
  708. dev->dev_addr[i] = ((unsigned char *)addr)[i + 2];
  709. mv643xx_eth_update_mac_address(dev);
  710. return 0;
  711. }
  712. /*
  713. * mv643xx_eth_tx_timeout
  714. *
  715. * Called upon a timeout on transmitting a packet
  716. *
  717. * Input : pointer to ethernet interface network device structure.
  718. * Output : N/A
  719. */
  720. static void mv643xx_eth_tx_timeout(struct net_device *dev)
  721. {
  722. struct mv643xx_private *mp = netdev_priv(dev);
  723. printk(KERN_INFO "%s: TX timeout ", dev->name);
  724. /* Do the reset outside of interrupt context */
  725. schedule_work(&mp->tx_timeout_task);
  726. }
  727. /*
  728. * mv643xx_eth_tx_timeout_task
  729. *
  730. * Actual routine to reset the adapter when a timeout on Tx has occurred
  731. */
  732. static void mv643xx_eth_tx_timeout_task(struct work_struct *ugly)
  733. {
  734. struct mv643xx_private *mp = container_of(ugly, struct mv643xx_private,
  735. tx_timeout_task);
  736. struct net_device *dev = mp->mii.dev; /* yuck */
  737. if (!netif_running(dev))
  738. return;
  739. netif_stop_queue(dev);
  740. eth_port_reset(mp->port_num);
  741. eth_port_start(dev);
  742. if (mp->tx_ring_size - mp->tx_desc_count >= MAX_DESCS_PER_SKB)
  743. netif_wake_queue(dev);
  744. }
  745. /**
  746. * mv643xx_eth_free_tx_descs - Free the tx desc data for completed descriptors
  747. *
  748. * If force is non-zero, frees uncompleted descriptors as well
  749. */
  750. int mv643xx_eth_free_tx_descs(struct net_device *dev, int force)
  751. {
  752. struct mv643xx_private *mp = netdev_priv(dev);
  753. struct eth_tx_desc *desc;
  754. u32 cmd_sts;
  755. struct sk_buff *skb;
  756. unsigned long flags;
  757. int tx_index;
  758. dma_addr_t addr;
  759. int count;
  760. int released = 0;
  761. while (mp->tx_desc_count > 0) {
  762. spin_lock_irqsave(&mp->lock, flags);
  763. /* tx_desc_count might have changed before acquiring the lock */
  764. if (mp->tx_desc_count <= 0) {
  765. spin_unlock_irqrestore(&mp->lock, flags);
  766. return released;
  767. }
  768. tx_index = mp->tx_used_desc_q;
  769. desc = &mp->p_tx_desc_area[tx_index];
  770. cmd_sts = desc->cmd_sts;
  771. if (!force && (cmd_sts & ETH_BUFFER_OWNED_BY_DMA)) {
  772. spin_unlock_irqrestore(&mp->lock, flags);
  773. return released;
  774. }
  775. mp->tx_used_desc_q = (tx_index + 1) % mp->tx_ring_size;
  776. mp->tx_desc_count--;
  777. addr = desc->buf_ptr;
  778. count = desc->byte_cnt;
  779. skb = mp->tx_skb[tx_index];
  780. if (skb)
  781. mp->tx_skb[tx_index] = NULL;
  782. if (cmd_sts & ETH_ERROR_SUMMARY) {
  783. printk("%s: Error in TX\n", dev->name);
  784. dev->stats.tx_errors++;
  785. }
  786. spin_unlock_irqrestore(&mp->lock, flags);
  787. if (cmd_sts & ETH_TX_FIRST_DESC)
  788. dma_unmap_single(NULL, addr, count, DMA_TO_DEVICE);
  789. else
  790. dma_unmap_page(NULL, addr, count, DMA_TO_DEVICE);
  791. if (skb)
  792. dev_kfree_skb_irq(skb);
  793. released = 1;
  794. }
  795. return released;
  796. }
  797. static void mv643xx_eth_free_completed_tx_descs(struct net_device *dev)
  798. {
  799. struct mv643xx_private *mp = netdev_priv(dev);
  800. if (mv643xx_eth_free_tx_descs(dev, 0) &&
  801. mp->tx_ring_size - mp->tx_desc_count >= MAX_DESCS_PER_SKB)
  802. netif_wake_queue(dev);
  803. }
  804. static void mv643xx_eth_free_all_tx_descs(struct net_device *dev)
  805. {
  806. mv643xx_eth_free_tx_descs(dev, 1);
  807. }
  808. /*
  809. * mv643xx_eth_receive
  810. *
  811. * This function is forward packets that are received from the port's
  812. * queues toward kernel core or FastRoute them to another interface.
  813. *
  814. * Input : dev - a pointer to the required interface
  815. * max - maximum number to receive (0 means unlimted)
  816. *
  817. * Output : number of served packets
  818. */
  819. static int mv643xx_eth_receive_queue(struct net_device *dev, int budget)
  820. {
  821. struct mv643xx_private *mp = netdev_priv(dev);
  822. struct net_device_stats *stats = &dev->stats;
  823. unsigned int received_packets = 0;
  824. struct sk_buff *skb;
  825. struct pkt_info pkt_info;
  826. while (budget-- > 0 && eth_port_receive(mp, &pkt_info) == ETH_OK) {
  827. dma_unmap_single(NULL, pkt_info.buf_ptr, ETH_RX_SKB_SIZE,
  828. DMA_FROM_DEVICE);
  829. mp->rx_desc_count--;
  830. received_packets++;
  831. /*
  832. * Update statistics.
  833. * Note byte count includes 4 byte CRC count
  834. */
  835. stats->rx_packets++;
  836. stats->rx_bytes += pkt_info.byte_cnt;
  837. skb = pkt_info.return_info;
  838. /*
  839. * In case received a packet without first / last bits on OR
  840. * the error summary bit is on, the packets needs to be dropeed.
  841. */
  842. if (((pkt_info.cmd_sts
  843. & (ETH_RX_FIRST_DESC | ETH_RX_LAST_DESC)) !=
  844. (ETH_RX_FIRST_DESC | ETH_RX_LAST_DESC))
  845. || (pkt_info.cmd_sts & ETH_ERROR_SUMMARY)) {
  846. stats->rx_dropped++;
  847. if ((pkt_info.cmd_sts & (ETH_RX_FIRST_DESC |
  848. ETH_RX_LAST_DESC)) !=
  849. (ETH_RX_FIRST_DESC | ETH_RX_LAST_DESC)) {
  850. if (net_ratelimit())
  851. printk(KERN_ERR
  852. "%s: Received packet spread "
  853. "on multiple descriptors\n",
  854. dev->name);
  855. }
  856. if (pkt_info.cmd_sts & ETH_ERROR_SUMMARY)
  857. stats->rx_errors++;
  858. dev_kfree_skb_irq(skb);
  859. } else {
  860. /*
  861. * The -4 is for the CRC in the trailer of the
  862. * received packet
  863. */
  864. skb_put(skb, pkt_info.byte_cnt - 4);
  865. if (pkt_info.cmd_sts & ETH_LAYER_4_CHECKSUM_OK) {
  866. skb->ip_summed = CHECKSUM_UNNECESSARY;
  867. skb->csum = htons(
  868. (pkt_info.cmd_sts & 0x0007fff8) >> 3);
  869. }
  870. skb->protocol = eth_type_trans(skb, dev);
  871. #ifdef MV643XX_NAPI
  872. netif_receive_skb(skb);
  873. #else
  874. netif_rx(skb);
  875. #endif
  876. }
  877. dev->last_rx = jiffies;
  878. }
  879. mv643xx_eth_rx_refill_descs(dev); /* Fill RX ring with skb's */
  880. return received_packets;
  881. }
  882. /* Set the mv643xx port configuration register for the speed/duplex mode. */
  883. static void mv643xx_eth_update_pscr(struct net_device *dev,
  884. struct ethtool_cmd *ecmd)
  885. {
  886. struct mv643xx_private *mp = netdev_priv(dev);
  887. int port_num = mp->port_num;
  888. u32 o_pscr, n_pscr;
  889. unsigned int queues;
  890. o_pscr = mv_read(PORT_SERIAL_CONTROL_REG(port_num));
  891. n_pscr = o_pscr;
  892. /* clear speed, duplex and rx buffer size fields */
  893. n_pscr &= ~(SET_MII_SPEED_TO_100 |
  894. SET_GMII_SPEED_TO_1000 |
  895. SET_FULL_DUPLEX_MODE |
  896. MAX_RX_PACKET_MASK);
  897. if (ecmd->duplex == DUPLEX_FULL)
  898. n_pscr |= SET_FULL_DUPLEX_MODE;
  899. if (ecmd->speed == SPEED_1000)
  900. n_pscr |= SET_GMII_SPEED_TO_1000 |
  901. MAX_RX_PACKET_9700BYTE;
  902. else {
  903. if (ecmd->speed == SPEED_100)
  904. n_pscr |= SET_MII_SPEED_TO_100;
  905. n_pscr |= MAX_RX_PACKET_1522BYTE;
  906. }
  907. if (n_pscr != o_pscr) {
  908. if ((o_pscr & SERIAL_PORT_ENABLE) == 0)
  909. mv_write(PORT_SERIAL_CONTROL_REG(port_num), n_pscr);
  910. else {
  911. queues = mv643xx_eth_port_disable_tx(port_num);
  912. o_pscr &= ~SERIAL_PORT_ENABLE;
  913. mv_write(PORT_SERIAL_CONTROL_REG(port_num), o_pscr);
  914. mv_write(PORT_SERIAL_CONTROL_REG(port_num), n_pscr);
  915. mv_write(PORT_SERIAL_CONTROL_REG(port_num), n_pscr);
  916. if (queues)
  917. mv643xx_eth_port_enable_tx(port_num, queues);
  918. }
  919. }
  920. }
  921. /*
  922. * mv643xx_eth_int_handler
  923. *
  924. * Main interrupt handler for the gigbit ethernet ports
  925. *
  926. * Input : irq - irq number (not used)
  927. * dev_id - a pointer to the required interface's data structure
  928. * regs - not used
  929. * Output : N/A
  930. */
  931. static irqreturn_t mv643xx_eth_int_handler(int irq, void *dev_id)
  932. {
  933. struct net_device *dev = (struct net_device *)dev_id;
  934. struct mv643xx_private *mp = netdev_priv(dev);
  935. u32 eth_int_cause, eth_int_cause_ext = 0;
  936. unsigned int port_num = mp->port_num;
  937. /* Read interrupt cause registers */
  938. eth_int_cause = mv_read(INTERRUPT_CAUSE_REG(port_num)) &
  939. ETH_INT_UNMASK_ALL;
  940. if (eth_int_cause & ETH_INT_CAUSE_EXT) {
  941. eth_int_cause_ext = mv_read(
  942. INTERRUPT_CAUSE_EXTEND_REG(port_num)) &
  943. ETH_INT_UNMASK_ALL_EXT;
  944. mv_write(INTERRUPT_CAUSE_EXTEND_REG(port_num),
  945. ~eth_int_cause_ext);
  946. }
  947. /* PHY status changed */
  948. if (eth_int_cause_ext & (ETH_INT_CAUSE_PHY | ETH_INT_CAUSE_STATE)) {
  949. struct ethtool_cmd cmd;
  950. if (mii_link_ok(&mp->mii)) {
  951. mii_ethtool_gset(&mp->mii, &cmd);
  952. mv643xx_eth_update_pscr(dev, &cmd);
  953. mv643xx_eth_port_enable_tx(port_num,
  954. ETH_TX_QUEUES_ENABLED);
  955. if (!netif_carrier_ok(dev)) {
  956. netif_carrier_on(dev);
  957. if (mp->tx_ring_size - mp->tx_desc_count >=
  958. MAX_DESCS_PER_SKB)
  959. netif_wake_queue(dev);
  960. }
  961. } else if (netif_carrier_ok(dev)) {
  962. netif_stop_queue(dev);
  963. netif_carrier_off(dev);
  964. }
  965. }
  966. #ifdef MV643XX_NAPI
  967. if (eth_int_cause & ETH_INT_CAUSE_RX) {
  968. /* schedule the NAPI poll routine to maintain port */
  969. mv_write(INTERRUPT_MASK_REG(port_num), ETH_INT_MASK_ALL);
  970. /* wait for previous write to complete */
  971. mv_read(INTERRUPT_MASK_REG(port_num));
  972. netif_rx_schedule(dev, &mp->napi);
  973. }
  974. #else
  975. if (eth_int_cause & ETH_INT_CAUSE_RX)
  976. mv643xx_eth_receive_queue(dev, INT_MAX);
  977. #endif
  978. if (eth_int_cause_ext & ETH_INT_CAUSE_TX)
  979. mv643xx_eth_free_completed_tx_descs(dev);
  980. /*
  981. * If no real interrupt occured, exit.
  982. * This can happen when using gigE interrupt coalescing mechanism.
  983. */
  984. if ((eth_int_cause == 0x0) && (eth_int_cause_ext == 0x0))
  985. return IRQ_NONE;
  986. return IRQ_HANDLED;
  987. }
  988. #ifdef MV643XX_COAL
  989. /*
  990. * eth_port_set_rx_coal - Sets coalescing interrupt mechanism on RX path
  991. *
  992. * DESCRIPTION:
  993. * This routine sets the RX coalescing interrupt mechanism parameter.
  994. * This parameter is a timeout counter, that counts in 64 t_clk
  995. * chunks ; that when timeout event occurs a maskable interrupt
  996. * occurs.
  997. * The parameter is calculated using the tClk of the MV-643xx chip
  998. * , and the required delay of the interrupt in usec.
  999. *
  1000. * INPUT:
  1001. * unsigned int eth_port_num Ethernet port number
  1002. * unsigned int t_clk t_clk of the MV-643xx chip in HZ units
  1003. * unsigned int delay Delay in usec
  1004. *
  1005. * OUTPUT:
  1006. * Interrupt coalescing mechanism value is set in MV-643xx chip.
  1007. *
  1008. * RETURN:
  1009. * The interrupt coalescing value set in the gigE port.
  1010. *
  1011. */
  1012. static unsigned int eth_port_set_rx_coal(unsigned int eth_port_num,
  1013. unsigned int t_clk, unsigned int delay)
  1014. {
  1015. unsigned int coal = ((t_clk / 1000000) * delay) / 64;
  1016. /* Set RX Coalescing mechanism */
  1017. mv_write(SDMA_CONFIG_REG(eth_port_num),
  1018. ((coal & 0x3fff) << 8) |
  1019. (mv_read(SDMA_CONFIG_REG(eth_port_num))
  1020. & 0xffc000ff));
  1021. return coal;
  1022. }
  1023. #endif
  1024. /*
  1025. * eth_port_set_tx_coal - Sets coalescing interrupt mechanism on TX path
  1026. *
  1027. * DESCRIPTION:
  1028. * This routine sets the TX coalescing interrupt mechanism parameter.
  1029. * This parameter is a timeout counter, that counts in 64 t_clk
  1030. * chunks ; that when timeout event occurs a maskable interrupt
  1031. * occurs.
  1032. * The parameter is calculated using the t_cLK frequency of the
  1033. * MV-643xx chip and the required delay in the interrupt in uSec
  1034. *
  1035. * INPUT:
  1036. * unsigned int eth_port_num Ethernet port number
  1037. * unsigned int t_clk t_clk of the MV-643xx chip in HZ units
  1038. * unsigned int delay Delay in uSeconds
  1039. *
  1040. * OUTPUT:
  1041. * Interrupt coalescing mechanism value is set in MV-643xx chip.
  1042. *
  1043. * RETURN:
  1044. * The interrupt coalescing value set in the gigE port.
  1045. *
  1046. */
  1047. static unsigned int eth_port_set_tx_coal(unsigned int eth_port_num,
  1048. unsigned int t_clk, unsigned int delay)
  1049. {
  1050. unsigned int coal;
  1051. coal = ((t_clk / 1000000) * delay) / 64;
  1052. /* Set TX Coalescing mechanism */
  1053. mv_write(TX_FIFO_URGENT_THRESHOLD_REG(eth_port_num), coal << 4);
  1054. return coal;
  1055. }
  1056. /*
  1057. * ether_init_rx_desc_ring - Curve a Rx chain desc list and buffer in memory.
  1058. *
  1059. * DESCRIPTION:
  1060. * This function prepares a Rx chained list of descriptors and packet
  1061. * buffers in a form of a ring. The routine must be called after port
  1062. * initialization routine and before port start routine.
  1063. * The Ethernet SDMA engine uses CPU bus addresses to access the various
  1064. * devices in the system (i.e. DRAM). This function uses the ethernet
  1065. * struct 'virtual to physical' routine (set by the user) to set the ring
  1066. * with physical addresses.
  1067. *
  1068. * INPUT:
  1069. * struct mv643xx_private *mp Ethernet Port Control srtuct.
  1070. *
  1071. * OUTPUT:
  1072. * The routine updates the Ethernet port control struct with information
  1073. * regarding the Rx descriptors and buffers.
  1074. *
  1075. * RETURN:
  1076. * None.
  1077. */
  1078. static void ether_init_rx_desc_ring(struct mv643xx_private *mp)
  1079. {
  1080. volatile struct eth_rx_desc *p_rx_desc;
  1081. int rx_desc_num = mp->rx_ring_size;
  1082. int i;
  1083. /* initialize the next_desc_ptr links in the Rx descriptors ring */
  1084. p_rx_desc = (struct eth_rx_desc *)mp->p_rx_desc_area;
  1085. for (i = 0; i < rx_desc_num; i++) {
  1086. p_rx_desc[i].next_desc_ptr = mp->rx_desc_dma +
  1087. ((i + 1) % rx_desc_num) * sizeof(struct eth_rx_desc);
  1088. }
  1089. /* Save Rx desc pointer to driver struct. */
  1090. mp->rx_curr_desc_q = 0;
  1091. mp->rx_used_desc_q = 0;
  1092. mp->rx_desc_area_size = rx_desc_num * sizeof(struct eth_rx_desc);
  1093. }
  1094. /*
  1095. * ether_init_tx_desc_ring - Curve a Tx chain desc list and buffer in memory.
  1096. *
  1097. * DESCRIPTION:
  1098. * This function prepares a Tx chained list of descriptors and packet
  1099. * buffers in a form of a ring. The routine must be called after port
  1100. * initialization routine and before port start routine.
  1101. * The Ethernet SDMA engine uses CPU bus addresses to access the various
  1102. * devices in the system (i.e. DRAM). This function uses the ethernet
  1103. * struct 'virtual to physical' routine (set by the user) to set the ring
  1104. * with physical addresses.
  1105. *
  1106. * INPUT:
  1107. * struct mv643xx_private *mp Ethernet Port Control srtuct.
  1108. *
  1109. * OUTPUT:
  1110. * The routine updates the Ethernet port control struct with information
  1111. * regarding the Tx descriptors and buffers.
  1112. *
  1113. * RETURN:
  1114. * None.
  1115. */
  1116. static void ether_init_tx_desc_ring(struct mv643xx_private *mp)
  1117. {
  1118. int tx_desc_num = mp->tx_ring_size;
  1119. struct eth_tx_desc *p_tx_desc;
  1120. int i;
  1121. /* Initialize the next_desc_ptr links in the Tx descriptors ring */
  1122. p_tx_desc = (struct eth_tx_desc *)mp->p_tx_desc_area;
  1123. for (i = 0; i < tx_desc_num; i++) {
  1124. p_tx_desc[i].next_desc_ptr = mp->tx_desc_dma +
  1125. ((i + 1) % tx_desc_num) * sizeof(struct eth_tx_desc);
  1126. }
  1127. mp->tx_curr_desc_q = 0;
  1128. mp->tx_used_desc_q = 0;
  1129. mp->tx_desc_area_size = tx_desc_num * sizeof(struct eth_tx_desc);
  1130. }
  1131. static int mv643xx_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  1132. {
  1133. struct mv643xx_private *mp = netdev_priv(dev);
  1134. int err;
  1135. spin_lock_irq(&mp->lock);
  1136. err = mii_ethtool_sset(&mp->mii, cmd);
  1137. spin_unlock_irq(&mp->lock);
  1138. return err;
  1139. }
  1140. static int mv643xx_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  1141. {
  1142. struct mv643xx_private *mp = netdev_priv(dev);
  1143. int err;
  1144. spin_lock_irq(&mp->lock);
  1145. err = mii_ethtool_gset(&mp->mii, cmd);
  1146. spin_unlock_irq(&mp->lock);
  1147. /* The PHY may support 1000baseT_Half, but the mv643xx does not */
  1148. cmd->supported &= ~SUPPORTED_1000baseT_Half;
  1149. cmd->advertising &= ~ADVERTISED_1000baseT_Half;
  1150. return err;
  1151. }
  1152. /*
  1153. * mv643xx_eth_open
  1154. *
  1155. * This function is called when openning the network device. The function
  1156. * should initialize all the hardware, initialize cyclic Rx/Tx
  1157. * descriptors chain and buffers and allocate an IRQ to the network
  1158. * device.
  1159. *
  1160. * Input : a pointer to the network device structure
  1161. *
  1162. * Output : zero of success , nonzero if fails.
  1163. */
  1164. static int mv643xx_eth_open(struct net_device *dev)
  1165. {
  1166. struct mv643xx_private *mp = netdev_priv(dev);
  1167. unsigned int port_num = mp->port_num;
  1168. unsigned int size;
  1169. int err;
  1170. /* Clear any pending ethernet port interrupts */
  1171. mv_write(INTERRUPT_CAUSE_REG(port_num), 0);
  1172. mv_write(INTERRUPT_CAUSE_EXTEND_REG(port_num), 0);
  1173. /* wait for previous write to complete */
  1174. mv_read (INTERRUPT_CAUSE_EXTEND_REG(port_num));
  1175. err = request_irq(dev->irq, mv643xx_eth_int_handler,
  1176. IRQF_SHARED | IRQF_SAMPLE_RANDOM, dev->name, dev);
  1177. if (err) {
  1178. printk(KERN_ERR "Can not assign IRQ number to MV643XX_eth%d\n",
  1179. port_num);
  1180. return -EAGAIN;
  1181. }
  1182. eth_port_init(mp);
  1183. memset(&mp->timeout, 0, sizeof(struct timer_list));
  1184. mp->timeout.function = mv643xx_eth_rx_refill_descs_timer_wrapper;
  1185. mp->timeout.data = (unsigned long)dev;
  1186. /* Allocate RX and TX skb rings */
  1187. mp->rx_skb = kmalloc(sizeof(*mp->rx_skb) * mp->rx_ring_size,
  1188. GFP_KERNEL);
  1189. if (!mp->rx_skb) {
  1190. printk(KERN_ERR "%s: Cannot allocate Rx skb ring\n", dev->name);
  1191. err = -ENOMEM;
  1192. goto out_free_irq;
  1193. }
  1194. mp->tx_skb = kmalloc(sizeof(*mp->tx_skb) * mp->tx_ring_size,
  1195. GFP_KERNEL);
  1196. if (!mp->tx_skb) {
  1197. printk(KERN_ERR "%s: Cannot allocate Tx skb ring\n", dev->name);
  1198. err = -ENOMEM;
  1199. goto out_free_rx_skb;
  1200. }
  1201. /* Allocate TX ring */
  1202. mp->tx_desc_count = 0;
  1203. size = mp->tx_ring_size * sizeof(struct eth_tx_desc);
  1204. mp->tx_desc_area_size = size;
  1205. if (mp->tx_sram_size) {
  1206. mp->p_tx_desc_area = ioremap(mp->tx_sram_addr,
  1207. mp->tx_sram_size);
  1208. mp->tx_desc_dma = mp->tx_sram_addr;
  1209. } else
  1210. mp->p_tx_desc_area = dma_alloc_coherent(NULL, size,
  1211. &mp->tx_desc_dma,
  1212. GFP_KERNEL);
  1213. if (!mp->p_tx_desc_area) {
  1214. printk(KERN_ERR "%s: Cannot allocate Tx Ring (size %d bytes)\n",
  1215. dev->name, size);
  1216. err = -ENOMEM;
  1217. goto out_free_tx_skb;
  1218. }
  1219. BUG_ON((u32) mp->p_tx_desc_area & 0xf); /* check 16-byte alignment */
  1220. memset((void *)mp->p_tx_desc_area, 0, mp->tx_desc_area_size);
  1221. ether_init_tx_desc_ring(mp);
  1222. /* Allocate RX ring */
  1223. mp->rx_desc_count = 0;
  1224. size = mp->rx_ring_size * sizeof(struct eth_rx_desc);
  1225. mp->rx_desc_area_size = size;
  1226. if (mp->rx_sram_size) {
  1227. mp->p_rx_desc_area = ioremap(mp->rx_sram_addr,
  1228. mp->rx_sram_size);
  1229. mp->rx_desc_dma = mp->rx_sram_addr;
  1230. } else
  1231. mp->p_rx_desc_area = dma_alloc_coherent(NULL, size,
  1232. &mp->rx_desc_dma,
  1233. GFP_KERNEL);
  1234. if (!mp->p_rx_desc_area) {
  1235. printk(KERN_ERR "%s: Cannot allocate Rx ring (size %d bytes)\n",
  1236. dev->name, size);
  1237. printk(KERN_ERR "%s: Freeing previously allocated TX queues...",
  1238. dev->name);
  1239. if (mp->rx_sram_size)
  1240. iounmap(mp->p_tx_desc_area);
  1241. else
  1242. dma_free_coherent(NULL, mp->tx_desc_area_size,
  1243. mp->p_tx_desc_area, mp->tx_desc_dma);
  1244. err = -ENOMEM;
  1245. goto out_free_tx_skb;
  1246. }
  1247. memset((void *)mp->p_rx_desc_area, 0, size);
  1248. ether_init_rx_desc_ring(mp);
  1249. mv643xx_eth_rx_refill_descs(dev); /* Fill RX ring with skb's */
  1250. #ifdef MV643XX_NAPI
  1251. napi_enable(&mp->napi);
  1252. #endif
  1253. eth_port_start(dev);
  1254. /* Interrupt Coalescing */
  1255. #ifdef MV643XX_COAL
  1256. mp->rx_int_coal =
  1257. eth_port_set_rx_coal(port_num, 133000000, MV643XX_RX_COAL);
  1258. #endif
  1259. mp->tx_int_coal =
  1260. eth_port_set_tx_coal(port_num, 133000000, MV643XX_TX_COAL);
  1261. /* Unmask phy and link status changes interrupts */
  1262. mv_write(INTERRUPT_EXTEND_MASK_REG(port_num), ETH_INT_UNMASK_ALL_EXT);
  1263. /* Unmask RX buffer and TX end interrupt */
  1264. mv_write(INTERRUPT_MASK_REG(port_num), ETH_INT_UNMASK_ALL);
  1265. return 0;
  1266. out_free_tx_skb:
  1267. kfree(mp->tx_skb);
  1268. out_free_rx_skb:
  1269. kfree(mp->rx_skb);
  1270. out_free_irq:
  1271. free_irq(dev->irq, dev);
  1272. return err;
  1273. }
  1274. static void mv643xx_eth_free_tx_rings(struct net_device *dev)
  1275. {
  1276. struct mv643xx_private *mp = netdev_priv(dev);
  1277. /* Stop Tx Queues */
  1278. mv643xx_eth_port_disable_tx(mp->port_num);
  1279. /* Free outstanding skb's on TX ring */
  1280. mv643xx_eth_free_all_tx_descs(dev);
  1281. BUG_ON(mp->tx_used_desc_q != mp->tx_curr_desc_q);
  1282. /* Free TX ring */
  1283. if (mp->tx_sram_size)
  1284. iounmap(mp->p_tx_desc_area);
  1285. else
  1286. dma_free_coherent(NULL, mp->tx_desc_area_size,
  1287. mp->p_tx_desc_area, mp->tx_desc_dma);
  1288. }
  1289. static void mv643xx_eth_free_rx_rings(struct net_device *dev)
  1290. {
  1291. struct mv643xx_private *mp = netdev_priv(dev);
  1292. unsigned int port_num = mp->port_num;
  1293. int curr;
  1294. /* Stop RX Queues */
  1295. mv643xx_eth_port_disable_rx(port_num);
  1296. /* Free preallocated skb's on RX rings */
  1297. for (curr = 0; mp->rx_desc_count && curr < mp->rx_ring_size; curr++) {
  1298. if (mp->rx_skb[curr]) {
  1299. dev_kfree_skb(mp->rx_skb[curr]);
  1300. mp->rx_desc_count--;
  1301. }
  1302. }
  1303. if (mp->rx_desc_count)
  1304. printk(KERN_ERR
  1305. "%s: Error in freeing Rx Ring. %d skb's still"
  1306. " stuck in RX Ring - ignoring them\n", dev->name,
  1307. mp->rx_desc_count);
  1308. /* Free RX ring */
  1309. if (mp->rx_sram_size)
  1310. iounmap(mp->p_rx_desc_area);
  1311. else
  1312. dma_free_coherent(NULL, mp->rx_desc_area_size,
  1313. mp->p_rx_desc_area, mp->rx_desc_dma);
  1314. }
  1315. /*
  1316. * mv643xx_eth_stop
  1317. *
  1318. * This function is used when closing the network device.
  1319. * It updates the hardware,
  1320. * release all memory that holds buffers and descriptors and release the IRQ.
  1321. * Input : a pointer to the device structure
  1322. * Output : zero if success , nonzero if fails
  1323. */
  1324. static int mv643xx_eth_stop(struct net_device *dev)
  1325. {
  1326. struct mv643xx_private *mp = netdev_priv(dev);
  1327. unsigned int port_num = mp->port_num;
  1328. /* Mask all interrupts on ethernet port */
  1329. mv_write(INTERRUPT_MASK_REG(port_num), ETH_INT_MASK_ALL);
  1330. /* wait for previous write to complete */
  1331. mv_read(INTERRUPT_MASK_REG(port_num));
  1332. #ifdef MV643XX_NAPI
  1333. napi_disable(&mp->napi);
  1334. #endif
  1335. netif_carrier_off(dev);
  1336. netif_stop_queue(dev);
  1337. eth_port_reset(mp->port_num);
  1338. mv643xx_eth_free_tx_rings(dev);
  1339. mv643xx_eth_free_rx_rings(dev);
  1340. free_irq(dev->irq, dev);
  1341. return 0;
  1342. }
  1343. #ifdef MV643XX_NAPI
  1344. /*
  1345. * mv643xx_poll
  1346. *
  1347. * This function is used in case of NAPI
  1348. */
  1349. static int mv643xx_poll(struct napi_struct *napi, int budget)
  1350. {
  1351. struct mv643xx_private *mp = container_of(napi, struct mv643xx_private, napi);
  1352. struct net_device *dev = mp->dev;
  1353. unsigned int port_num = mp->port_num;
  1354. int work_done;
  1355. #ifdef MV643XX_TX_FAST_REFILL
  1356. if (++mp->tx_clean_threshold > 5) {
  1357. mv643xx_eth_free_completed_tx_descs(dev);
  1358. mp->tx_clean_threshold = 0;
  1359. }
  1360. #endif
  1361. work_done = 0;
  1362. if ((mv_read(RX_CURRENT_QUEUE_DESC_PTR_0(port_num)))
  1363. != (u32) mp->rx_used_desc_q)
  1364. work_done = mv643xx_eth_receive_queue(dev, budget);
  1365. if (work_done < budget) {
  1366. netif_rx_complete(dev, napi);
  1367. mv_write(INTERRUPT_CAUSE_REG(port_num), 0);
  1368. mv_write(INTERRUPT_CAUSE_EXTEND_REG(port_num), 0);
  1369. mv_write(INTERRUPT_MASK_REG(port_num), ETH_INT_UNMASK_ALL);
  1370. }
  1371. return work_done;
  1372. }
  1373. #endif
  1374. /**
  1375. * has_tiny_unaligned_frags - check if skb has any small, unaligned fragments
  1376. *
  1377. * Hardware can't handle unaligned fragments smaller than 9 bytes.
  1378. * This helper function detects that case.
  1379. */
  1380. static inline unsigned int has_tiny_unaligned_frags(struct sk_buff *skb)
  1381. {
  1382. unsigned int frag;
  1383. skb_frag_t *fragp;
  1384. for (frag = 0; frag < skb_shinfo(skb)->nr_frags; frag++) {
  1385. fragp = &skb_shinfo(skb)->frags[frag];
  1386. if (fragp->size <= 8 && fragp->page_offset & 0x7)
  1387. return 1;
  1388. }
  1389. return 0;
  1390. }
  1391. /**
  1392. * eth_alloc_tx_desc_index - return the index of the next available tx desc
  1393. */
  1394. static int eth_alloc_tx_desc_index(struct mv643xx_private *mp)
  1395. {
  1396. int tx_desc_curr;
  1397. BUG_ON(mp->tx_desc_count >= mp->tx_ring_size);
  1398. tx_desc_curr = mp->tx_curr_desc_q;
  1399. mp->tx_curr_desc_q = (tx_desc_curr + 1) % mp->tx_ring_size;
  1400. BUG_ON(mp->tx_curr_desc_q == mp->tx_used_desc_q);
  1401. return tx_desc_curr;
  1402. }
  1403. /**
  1404. * eth_tx_fill_frag_descs - fill tx hw descriptors for an skb's fragments.
  1405. *
  1406. * Ensure the data for each fragment to be transmitted is mapped properly,
  1407. * then fill in descriptors in the tx hw queue.
  1408. */
  1409. static void eth_tx_fill_frag_descs(struct mv643xx_private *mp,
  1410. struct sk_buff *skb)
  1411. {
  1412. int frag;
  1413. int tx_index;
  1414. struct eth_tx_desc *desc;
  1415. for (frag = 0; frag < skb_shinfo(skb)->nr_frags; frag++) {
  1416. skb_frag_t *this_frag = &skb_shinfo(skb)->frags[frag];
  1417. tx_index = eth_alloc_tx_desc_index(mp);
  1418. desc = &mp->p_tx_desc_area[tx_index];
  1419. desc->cmd_sts = ETH_BUFFER_OWNED_BY_DMA;
  1420. /* Last Frag enables interrupt and frees the skb */
  1421. if (frag == (skb_shinfo(skb)->nr_frags - 1)) {
  1422. desc->cmd_sts |= ETH_ZERO_PADDING |
  1423. ETH_TX_LAST_DESC |
  1424. ETH_TX_ENABLE_INTERRUPT;
  1425. mp->tx_skb[tx_index] = skb;
  1426. } else
  1427. mp->tx_skb[tx_index] = NULL;
  1428. desc = &mp->p_tx_desc_area[tx_index];
  1429. desc->l4i_chk = 0;
  1430. desc->byte_cnt = this_frag->size;
  1431. desc->buf_ptr = dma_map_page(NULL, this_frag->page,
  1432. this_frag->page_offset,
  1433. this_frag->size,
  1434. DMA_TO_DEVICE);
  1435. }
  1436. }
  1437. /**
  1438. * eth_tx_submit_descs_for_skb - submit data from an skb to the tx hw
  1439. *
  1440. * Ensure the data for an skb to be transmitted is mapped properly,
  1441. * then fill in descriptors in the tx hw queue and start the hardware.
  1442. */
  1443. static void eth_tx_submit_descs_for_skb(struct mv643xx_private *mp,
  1444. struct sk_buff *skb)
  1445. {
  1446. int tx_index;
  1447. struct eth_tx_desc *desc;
  1448. u32 cmd_sts;
  1449. int length;
  1450. int nr_frags = skb_shinfo(skb)->nr_frags;
  1451. cmd_sts = ETH_TX_FIRST_DESC | ETH_GEN_CRC | ETH_BUFFER_OWNED_BY_DMA;
  1452. tx_index = eth_alloc_tx_desc_index(mp);
  1453. desc = &mp->p_tx_desc_area[tx_index];
  1454. if (nr_frags) {
  1455. eth_tx_fill_frag_descs(mp, skb);
  1456. length = skb_headlen(skb);
  1457. mp->tx_skb[tx_index] = NULL;
  1458. } else {
  1459. cmd_sts |= ETH_ZERO_PADDING |
  1460. ETH_TX_LAST_DESC |
  1461. ETH_TX_ENABLE_INTERRUPT;
  1462. length = skb->len;
  1463. mp->tx_skb[tx_index] = skb;
  1464. }
  1465. desc->byte_cnt = length;
  1466. desc->buf_ptr = dma_map_single(NULL, skb->data, length, DMA_TO_DEVICE);
  1467. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  1468. BUG_ON(skb->protocol != ETH_P_IP);
  1469. cmd_sts |= ETH_GEN_TCP_UDP_CHECKSUM |
  1470. ETH_GEN_IP_V_4_CHECKSUM |
  1471. ip_hdr(skb)->ihl << ETH_TX_IHL_SHIFT;
  1472. switch (ip_hdr(skb)->protocol) {
  1473. case IPPROTO_UDP:
  1474. cmd_sts |= ETH_UDP_FRAME;
  1475. desc->l4i_chk = udp_hdr(skb)->check;
  1476. break;
  1477. case IPPROTO_TCP:
  1478. desc->l4i_chk = tcp_hdr(skb)->check;
  1479. break;
  1480. default:
  1481. BUG();
  1482. }
  1483. } else {
  1484. /* Errata BTS #50, IHL must be 5 if no HW checksum */
  1485. cmd_sts |= 5 << ETH_TX_IHL_SHIFT;
  1486. desc->l4i_chk = 0;
  1487. }
  1488. /* ensure all other descriptors are written before first cmd_sts */
  1489. wmb();
  1490. desc->cmd_sts = cmd_sts;
  1491. /* ensure all descriptors are written before poking hardware */
  1492. wmb();
  1493. mv643xx_eth_port_enable_tx(mp->port_num, ETH_TX_QUEUES_ENABLED);
  1494. mp->tx_desc_count += nr_frags + 1;
  1495. }
  1496. /**
  1497. * mv643xx_eth_start_xmit - queue an skb to the hardware for transmission
  1498. *
  1499. */
  1500. static int mv643xx_eth_start_xmit(struct sk_buff *skb, struct net_device *dev)
  1501. {
  1502. struct mv643xx_private *mp = netdev_priv(dev);
  1503. struct net_device_stats *stats = &dev->stats;
  1504. unsigned long flags;
  1505. BUG_ON(netif_queue_stopped(dev));
  1506. BUG_ON(skb == NULL);
  1507. if (mp->tx_ring_size - mp->tx_desc_count < MAX_DESCS_PER_SKB) {
  1508. printk(KERN_ERR "%s: transmit with queue full\n", dev->name);
  1509. netif_stop_queue(dev);
  1510. return 1;
  1511. }
  1512. if (has_tiny_unaligned_frags(skb)) {
  1513. if (__skb_linearize(skb)) {
  1514. stats->tx_dropped++;
  1515. printk(KERN_DEBUG "%s: failed to linearize tiny "
  1516. "unaligned fragment\n", dev->name);
  1517. return 1;
  1518. }
  1519. }
  1520. spin_lock_irqsave(&mp->lock, flags);
  1521. eth_tx_submit_descs_for_skb(mp, skb);
  1522. stats->tx_bytes += skb->len;
  1523. stats->tx_packets++;
  1524. dev->trans_start = jiffies;
  1525. if (mp->tx_ring_size - mp->tx_desc_count < MAX_DESCS_PER_SKB)
  1526. netif_stop_queue(dev);
  1527. spin_unlock_irqrestore(&mp->lock, flags);
  1528. return 0; /* success */
  1529. }
  1530. #ifdef CONFIG_NET_POLL_CONTROLLER
  1531. static void mv643xx_netpoll(struct net_device *netdev)
  1532. {
  1533. struct mv643xx_private *mp = netdev_priv(netdev);
  1534. int port_num = mp->port_num;
  1535. mv_write(INTERRUPT_MASK_REG(port_num), ETH_INT_MASK_ALL);
  1536. /* wait for previous write to complete */
  1537. mv_read(INTERRUPT_MASK_REG(port_num));
  1538. mv643xx_eth_int_handler(netdev->irq, netdev);
  1539. mv_write(INTERRUPT_MASK_REG(port_num), ETH_INT_UNMASK_ALL);
  1540. }
  1541. #endif
  1542. static void mv643xx_init_ethtool_cmd(struct net_device *dev, int phy_address,
  1543. int speed, int duplex,
  1544. struct ethtool_cmd *cmd)
  1545. {
  1546. struct mv643xx_private *mp = netdev_priv(dev);
  1547. memset(cmd, 0, sizeof(*cmd));
  1548. cmd->port = PORT_MII;
  1549. cmd->transceiver = XCVR_INTERNAL;
  1550. cmd->phy_address = phy_address;
  1551. if (speed == 0) {
  1552. cmd->autoneg = AUTONEG_ENABLE;
  1553. /* mii lib checks, but doesn't use speed on AUTONEG_ENABLE */
  1554. cmd->speed = SPEED_100;
  1555. cmd->advertising = ADVERTISED_10baseT_Half |
  1556. ADVERTISED_10baseT_Full |
  1557. ADVERTISED_100baseT_Half |
  1558. ADVERTISED_100baseT_Full;
  1559. if (mp->mii.supports_gmii)
  1560. cmd->advertising |= ADVERTISED_1000baseT_Full;
  1561. } else {
  1562. cmd->autoneg = AUTONEG_DISABLE;
  1563. cmd->speed = speed;
  1564. cmd->duplex = duplex;
  1565. }
  1566. }
  1567. /*/
  1568. * mv643xx_eth_probe
  1569. *
  1570. * First function called after registering the network device.
  1571. * It's purpose is to initialize the device as an ethernet device,
  1572. * fill the ethernet device structure with pointers * to functions,
  1573. * and set the MAC address of the interface
  1574. *
  1575. * Input : struct device *
  1576. * Output : -ENOMEM if failed , 0 if success
  1577. */
  1578. static int mv643xx_eth_probe(struct platform_device *pdev)
  1579. {
  1580. struct mv643xx_eth_platform_data *pd;
  1581. int port_num;
  1582. struct mv643xx_private *mp;
  1583. struct net_device *dev;
  1584. u8 *p;
  1585. struct resource *res;
  1586. int err;
  1587. struct ethtool_cmd cmd;
  1588. int duplex = DUPLEX_HALF;
  1589. int speed = 0; /* default to auto-negotiation */
  1590. DECLARE_MAC_BUF(mac);
  1591. pd = pdev->dev.platform_data;
  1592. if (pd == NULL) {
  1593. printk(KERN_ERR "No mv643xx_eth_platform_data\n");
  1594. return -ENODEV;
  1595. }
  1596. dev = alloc_etherdev(sizeof(struct mv643xx_private));
  1597. if (!dev)
  1598. return -ENOMEM;
  1599. platform_set_drvdata(pdev, dev);
  1600. mp = netdev_priv(dev);
  1601. mp->dev = dev;
  1602. #ifdef MV643XX_NAPI
  1603. netif_napi_add(dev, &mp->napi, mv643xx_poll, 64);
  1604. #endif
  1605. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  1606. BUG_ON(!res);
  1607. dev->irq = res->start;
  1608. dev->open = mv643xx_eth_open;
  1609. dev->stop = mv643xx_eth_stop;
  1610. dev->hard_start_xmit = mv643xx_eth_start_xmit;
  1611. dev->set_mac_address = mv643xx_eth_set_mac_address;
  1612. dev->set_multicast_list = mv643xx_eth_set_rx_mode;
  1613. /* No need to Tx Timeout */
  1614. dev->tx_timeout = mv643xx_eth_tx_timeout;
  1615. #ifdef CONFIG_NET_POLL_CONTROLLER
  1616. dev->poll_controller = mv643xx_netpoll;
  1617. #endif
  1618. dev->watchdog_timeo = 2 * HZ;
  1619. dev->base_addr = 0;
  1620. dev->change_mtu = mv643xx_eth_change_mtu;
  1621. dev->do_ioctl = mv643xx_eth_do_ioctl;
  1622. SET_ETHTOOL_OPS(dev, &mv643xx_ethtool_ops);
  1623. #ifdef MV643XX_CHECKSUM_OFFLOAD_TX
  1624. #ifdef MAX_SKB_FRAGS
  1625. /*
  1626. * Zero copy can only work if we use Discovery II memory. Else, we will
  1627. * have to map the buffers to ISA memory which is only 16 MB
  1628. */
  1629. dev->features = NETIF_F_SG | NETIF_F_IP_CSUM;
  1630. #endif
  1631. #endif
  1632. /* Configure the timeout task */
  1633. INIT_WORK(&mp->tx_timeout_task, mv643xx_eth_tx_timeout_task);
  1634. spin_lock_init(&mp->lock);
  1635. port_num = mp->port_num = pd->port_number;
  1636. /* set default config values */
  1637. eth_port_uc_addr_get(port_num, dev->dev_addr);
  1638. mp->rx_ring_size = PORT_DEFAULT_RECEIVE_QUEUE_SIZE;
  1639. mp->tx_ring_size = PORT_DEFAULT_TRANSMIT_QUEUE_SIZE;
  1640. if (is_valid_ether_addr(pd->mac_addr))
  1641. memcpy(dev->dev_addr, pd->mac_addr, 6);
  1642. if (pd->phy_addr || pd->force_phy_addr)
  1643. ethernet_phy_set(port_num, pd->phy_addr);
  1644. if (pd->rx_queue_size)
  1645. mp->rx_ring_size = pd->rx_queue_size;
  1646. if (pd->tx_queue_size)
  1647. mp->tx_ring_size = pd->tx_queue_size;
  1648. if (pd->tx_sram_size) {
  1649. mp->tx_sram_size = pd->tx_sram_size;
  1650. mp->tx_sram_addr = pd->tx_sram_addr;
  1651. }
  1652. if (pd->rx_sram_size) {
  1653. mp->rx_sram_size = pd->rx_sram_size;
  1654. mp->rx_sram_addr = pd->rx_sram_addr;
  1655. }
  1656. duplex = pd->duplex;
  1657. speed = pd->speed;
  1658. /* Hook up MII support for ethtool */
  1659. mp->mii.dev = dev;
  1660. mp->mii.mdio_read = mv643xx_mdio_read;
  1661. mp->mii.mdio_write = mv643xx_mdio_write;
  1662. mp->mii.phy_id = ethernet_phy_get(port_num);
  1663. mp->mii.phy_id_mask = 0x3f;
  1664. mp->mii.reg_num_mask = 0x1f;
  1665. err = ethernet_phy_detect(port_num);
  1666. if (err) {
  1667. pr_debug("MV643xx ethernet port %d: "
  1668. "No PHY detected at addr %d\n",
  1669. port_num, ethernet_phy_get(port_num));
  1670. goto out;
  1671. }
  1672. ethernet_phy_reset(port_num);
  1673. mp->mii.supports_gmii = mii_check_gmii_support(&mp->mii);
  1674. mv643xx_init_ethtool_cmd(dev, mp->mii.phy_id, speed, duplex, &cmd);
  1675. mv643xx_eth_update_pscr(dev, &cmd);
  1676. mv643xx_set_settings(dev, &cmd);
  1677. SET_NETDEV_DEV(dev, &pdev->dev);
  1678. err = register_netdev(dev);
  1679. if (err)
  1680. goto out;
  1681. p = dev->dev_addr;
  1682. printk(KERN_NOTICE
  1683. "%s: port %d with MAC address %s\n",
  1684. dev->name, port_num, print_mac(mac, p));
  1685. if (dev->features & NETIF_F_SG)
  1686. printk(KERN_NOTICE "%s: Scatter Gather Enabled\n", dev->name);
  1687. if (dev->features & NETIF_F_IP_CSUM)
  1688. printk(KERN_NOTICE "%s: TX TCP/IP Checksumming Supported\n",
  1689. dev->name);
  1690. #ifdef MV643XX_CHECKSUM_OFFLOAD_TX
  1691. printk(KERN_NOTICE "%s: RX TCP/UDP Checksum Offload ON \n", dev->name);
  1692. #endif
  1693. #ifdef MV643XX_COAL
  1694. printk(KERN_NOTICE "%s: TX and RX Interrupt Coalescing ON \n",
  1695. dev->name);
  1696. #endif
  1697. #ifdef MV643XX_NAPI
  1698. printk(KERN_NOTICE "%s: RX NAPI Enabled \n", dev->name);
  1699. #endif
  1700. if (mp->tx_sram_size > 0)
  1701. printk(KERN_NOTICE "%s: Using SRAM\n", dev->name);
  1702. return 0;
  1703. out:
  1704. free_netdev(dev);
  1705. return err;
  1706. }
  1707. static int mv643xx_eth_remove(struct platform_device *pdev)
  1708. {
  1709. struct net_device *dev = platform_get_drvdata(pdev);
  1710. unregister_netdev(dev);
  1711. flush_scheduled_work();
  1712. free_netdev(dev);
  1713. platform_set_drvdata(pdev, NULL);
  1714. return 0;
  1715. }
  1716. static int mv643xx_eth_shared_probe(struct platform_device *pdev)
  1717. {
  1718. struct resource *res;
  1719. printk(KERN_NOTICE "MV-643xx 10/100/1000 Ethernet Driver\n");
  1720. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1721. if (res == NULL)
  1722. return -ENODEV;
  1723. mv643xx_eth_base = ioremap(res->start, res->end - res->start + 1);
  1724. if (mv643xx_eth_base == NULL)
  1725. return -ENOMEM;
  1726. return 0;
  1727. }
  1728. static int mv643xx_eth_shared_remove(struct platform_device *pdev)
  1729. {
  1730. iounmap(mv643xx_eth_base);
  1731. mv643xx_eth_base = NULL;
  1732. return 0;
  1733. }
  1734. static void mv643xx_eth_shutdown(struct platform_device *pdev)
  1735. {
  1736. struct net_device *dev = platform_get_drvdata(pdev);
  1737. struct mv643xx_private *mp = netdev_priv(dev);
  1738. unsigned int port_num = mp->port_num;
  1739. /* Mask all interrupts on ethernet port */
  1740. mv_write(INTERRUPT_MASK_REG(port_num), 0);
  1741. mv_read (INTERRUPT_MASK_REG(port_num));
  1742. eth_port_reset(port_num);
  1743. }
  1744. static struct platform_driver mv643xx_eth_driver = {
  1745. .probe = mv643xx_eth_probe,
  1746. .remove = mv643xx_eth_remove,
  1747. .shutdown = mv643xx_eth_shutdown,
  1748. .driver = {
  1749. .name = MV643XX_ETH_NAME,
  1750. },
  1751. };
  1752. static struct platform_driver mv643xx_eth_shared_driver = {
  1753. .probe = mv643xx_eth_shared_probe,
  1754. .remove = mv643xx_eth_shared_remove,
  1755. .driver = {
  1756. .name = MV643XX_ETH_SHARED_NAME,
  1757. },
  1758. };
  1759. /*
  1760. * mv643xx_init_module
  1761. *
  1762. * Registers the network drivers into the Linux kernel
  1763. *
  1764. * Input : N/A
  1765. *
  1766. * Output : N/A
  1767. */
  1768. static int __init mv643xx_init_module(void)
  1769. {
  1770. int rc;
  1771. rc = platform_driver_register(&mv643xx_eth_shared_driver);
  1772. if (!rc) {
  1773. rc = platform_driver_register(&mv643xx_eth_driver);
  1774. if (rc)
  1775. platform_driver_unregister(&mv643xx_eth_shared_driver);
  1776. }
  1777. return rc;
  1778. }
  1779. /*
  1780. * mv643xx_cleanup_module
  1781. *
  1782. * Registers the network drivers into the Linux kernel
  1783. *
  1784. * Input : N/A
  1785. *
  1786. * Output : N/A
  1787. */
  1788. static void __exit mv643xx_cleanup_module(void)
  1789. {
  1790. platform_driver_unregister(&mv643xx_eth_driver);
  1791. platform_driver_unregister(&mv643xx_eth_shared_driver);
  1792. }
  1793. module_init(mv643xx_init_module);
  1794. module_exit(mv643xx_cleanup_module);
  1795. MODULE_LICENSE("GPL");
  1796. MODULE_AUTHOR( "Rabeeh Khoury, Assaf Hoffman, Matthew Dharm, Manish Lachwani"
  1797. " and Dale Farnsworth");
  1798. MODULE_DESCRIPTION("Ethernet driver for Marvell MV643XX");
  1799. /*
  1800. * The second part is the low level driver of the gigE ethernet ports.
  1801. */
  1802. /*
  1803. * Marvell's Gigabit Ethernet controller low level driver
  1804. *
  1805. * DESCRIPTION:
  1806. * This file introduce low level API to Marvell's Gigabit Ethernet
  1807. * controller. This Gigabit Ethernet Controller driver API controls
  1808. * 1) Operations (i.e. port init, start, reset etc').
  1809. * 2) Data flow (i.e. port send, receive etc').
  1810. * Each Gigabit Ethernet port is controlled via
  1811. * struct mv643xx_private.
  1812. * This struct includes user configuration information as well as
  1813. * driver internal data needed for its operations.
  1814. *
  1815. * Supported Features:
  1816. * - This low level driver is OS independent. Allocating memory for
  1817. * the descriptor rings and buffers are not within the scope of
  1818. * this driver.
  1819. * - The user is free from Rx/Tx queue managing.
  1820. * - This low level driver introduce functionality API that enable
  1821. * the to operate Marvell's Gigabit Ethernet Controller in a
  1822. * convenient way.
  1823. * - Simple Gigabit Ethernet port operation API.
  1824. * - Simple Gigabit Ethernet port data flow API.
  1825. * - Data flow and operation API support per queue functionality.
  1826. * - Support cached descriptors for better performance.
  1827. * - Enable access to all four DRAM banks and internal SRAM memory
  1828. * spaces.
  1829. * - PHY access and control API.
  1830. * - Port control register configuration API.
  1831. * - Full control over Unicast and Multicast MAC configurations.
  1832. *
  1833. * Operation flow:
  1834. *
  1835. * Initialization phase
  1836. * This phase complete the initialization of the the
  1837. * mv643xx_private struct.
  1838. * User information regarding port configuration has to be set
  1839. * prior to calling the port initialization routine.
  1840. *
  1841. * In this phase any port Tx/Rx activity is halted, MIB counters
  1842. * are cleared, PHY address is set according to user parameter and
  1843. * access to DRAM and internal SRAM memory spaces.
  1844. *
  1845. * Driver ring initialization
  1846. * Allocating memory for the descriptor rings and buffers is not
  1847. * within the scope of this driver. Thus, the user is required to
  1848. * allocate memory for the descriptors ring and buffers. Those
  1849. * memory parameters are used by the Rx and Tx ring initialization
  1850. * routines in order to curve the descriptor linked list in a form
  1851. * of a ring.
  1852. * Note: Pay special attention to alignment issues when using
  1853. * cached descriptors/buffers. In this phase the driver store
  1854. * information in the mv643xx_private struct regarding each queue
  1855. * ring.
  1856. *
  1857. * Driver start
  1858. * This phase prepares the Ethernet port for Rx and Tx activity.
  1859. * It uses the information stored in the mv643xx_private struct to
  1860. * initialize the various port registers.
  1861. *
  1862. * Data flow:
  1863. * All packet references to/from the driver are done using
  1864. * struct pkt_info.
  1865. * This struct is a unified struct used with Rx and Tx operations.
  1866. * This way the user is not required to be familiar with neither
  1867. * Tx nor Rx descriptors structures.
  1868. * The driver's descriptors rings are management by indexes.
  1869. * Those indexes controls the ring resources and used to indicate
  1870. * a SW resource error:
  1871. * 'current'
  1872. * This index points to the current available resource for use. For
  1873. * example in Rx process this index will point to the descriptor
  1874. * that will be passed to the user upon calling the receive
  1875. * routine. In Tx process, this index will point to the descriptor
  1876. * that will be assigned with the user packet info and transmitted.
  1877. * 'used'
  1878. * This index points to the descriptor that need to restore its
  1879. * resources. For example in Rx process, using the Rx buffer return
  1880. * API will attach the buffer returned in packet info to the
  1881. * descriptor pointed by 'used'. In Tx process, using the Tx
  1882. * descriptor return will merely return the user packet info with
  1883. * the command status of the transmitted buffer pointed by the
  1884. * 'used' index. Nevertheless, it is essential to use this routine
  1885. * to update the 'used' index.
  1886. * 'first'
  1887. * This index supports Tx Scatter-Gather. It points to the first
  1888. * descriptor of a packet assembled of multiple buffers. For
  1889. * example when in middle of Such packet we have a Tx resource
  1890. * error the 'curr' index get the value of 'first' to indicate
  1891. * that the ring returned to its state before trying to transmit
  1892. * this packet.
  1893. *
  1894. * Receive operation:
  1895. * The eth_port_receive API set the packet information struct,
  1896. * passed by the caller, with received information from the
  1897. * 'current' SDMA descriptor.
  1898. * It is the user responsibility to return this resource back
  1899. * to the Rx descriptor ring to enable the reuse of this source.
  1900. * Return Rx resource is done using the eth_rx_return_buff API.
  1901. *
  1902. * Prior to calling the initialization routine eth_port_init() the user
  1903. * must set the following fields under mv643xx_private struct:
  1904. * port_num User Ethernet port number.
  1905. * port_config User port configuration value.
  1906. * port_config_extend User port config extend value.
  1907. * port_sdma_config User port SDMA config value.
  1908. * port_serial_control User port serial control value.
  1909. *
  1910. * This driver data flow is done using the struct pkt_info which
  1911. * is a unified struct for Rx and Tx operations:
  1912. *
  1913. * byte_cnt Tx/Rx descriptor buffer byte count.
  1914. * l4i_chk CPU provided TCP Checksum. For Tx operation
  1915. * only.
  1916. * cmd_sts Tx/Rx descriptor command status.
  1917. * buf_ptr Tx/Rx descriptor buffer pointer.
  1918. * return_info Tx/Rx user resource return information.
  1919. */
  1920. /* PHY routines */
  1921. static int ethernet_phy_get(unsigned int eth_port_num);
  1922. static void ethernet_phy_set(unsigned int eth_port_num, int phy_addr);
  1923. /* Ethernet Port routines */
  1924. static void eth_port_set_filter_table_entry(int table, unsigned char entry);
  1925. /*
  1926. * eth_port_init - Initialize the Ethernet port driver
  1927. *
  1928. * DESCRIPTION:
  1929. * This function prepares the ethernet port to start its activity:
  1930. * 1) Completes the ethernet port driver struct initialization toward port
  1931. * start routine.
  1932. * 2) Resets the device to a quiescent state in case of warm reboot.
  1933. * 3) Enable SDMA access to all four DRAM banks as well as internal SRAM.
  1934. * 4) Clean MAC tables. The reset status of those tables is unknown.
  1935. * 5) Set PHY address.
  1936. * Note: Call this routine prior to eth_port_start routine and after
  1937. * setting user values in the user fields of Ethernet port control
  1938. * struct.
  1939. *
  1940. * INPUT:
  1941. * struct mv643xx_private *mp Ethernet port control struct
  1942. *
  1943. * OUTPUT:
  1944. * See description.
  1945. *
  1946. * RETURN:
  1947. * None.
  1948. */
  1949. static void eth_port_init(struct mv643xx_private *mp)
  1950. {
  1951. mp->rx_resource_err = 0;
  1952. eth_port_reset(mp->port_num);
  1953. eth_port_init_mac_tables(mp->port_num);
  1954. }
  1955. /*
  1956. * eth_port_start - Start the Ethernet port activity.
  1957. *
  1958. * DESCRIPTION:
  1959. * This routine prepares the Ethernet port for Rx and Tx activity:
  1960. * 1. Initialize Tx and Rx Current Descriptor Pointer for each queue that
  1961. * has been initialized a descriptor's ring (using
  1962. * ether_init_tx_desc_ring for Tx and ether_init_rx_desc_ring for Rx)
  1963. * 2. Initialize and enable the Ethernet configuration port by writing to
  1964. * the port's configuration and command registers.
  1965. * 3. Initialize and enable the SDMA by writing to the SDMA's
  1966. * configuration and command registers. After completing these steps,
  1967. * the ethernet port SDMA can starts to perform Rx and Tx activities.
  1968. *
  1969. * Note: Each Rx and Tx queue descriptor's list must be initialized prior
  1970. * to calling this function (use ether_init_tx_desc_ring for Tx queues
  1971. * and ether_init_rx_desc_ring for Rx queues).
  1972. *
  1973. * INPUT:
  1974. * dev - a pointer to the required interface
  1975. *
  1976. * OUTPUT:
  1977. * Ethernet port is ready to receive and transmit.
  1978. *
  1979. * RETURN:
  1980. * None.
  1981. */
  1982. static void eth_port_start(struct net_device *dev)
  1983. {
  1984. struct mv643xx_private *mp = netdev_priv(dev);
  1985. unsigned int port_num = mp->port_num;
  1986. int tx_curr_desc, rx_curr_desc;
  1987. u32 pscr;
  1988. struct ethtool_cmd ethtool_cmd;
  1989. /* Assignment of Tx CTRP of given queue */
  1990. tx_curr_desc = mp->tx_curr_desc_q;
  1991. mv_write(TX_CURRENT_QUEUE_DESC_PTR_0(port_num),
  1992. (u32)((struct eth_tx_desc *)mp->tx_desc_dma + tx_curr_desc));
  1993. /* Assignment of Rx CRDP of given queue */
  1994. rx_curr_desc = mp->rx_curr_desc_q;
  1995. mv_write(RX_CURRENT_QUEUE_DESC_PTR_0(port_num),
  1996. (u32)((struct eth_rx_desc *)mp->rx_desc_dma + rx_curr_desc));
  1997. /* Add the assigned Ethernet address to the port's address table */
  1998. eth_port_uc_addr_set(port_num, dev->dev_addr);
  1999. /* Assign port configuration and command. */
  2000. mv_write(PORT_CONFIG_REG(port_num),
  2001. PORT_CONFIG_DEFAULT_VALUE);
  2002. mv_write(PORT_CONFIG_EXTEND_REG(port_num),
  2003. PORT_CONFIG_EXTEND_DEFAULT_VALUE);
  2004. pscr = mv_read(PORT_SERIAL_CONTROL_REG(port_num));
  2005. pscr &= ~(SERIAL_PORT_ENABLE | FORCE_LINK_PASS);
  2006. mv_write(PORT_SERIAL_CONTROL_REG(port_num), pscr);
  2007. pscr |= DISABLE_AUTO_NEG_FOR_FLOW_CTRL |
  2008. DISABLE_AUTO_NEG_SPEED_GMII |
  2009. DISABLE_AUTO_NEG_FOR_DUPLX |
  2010. DO_NOT_FORCE_LINK_FAIL |
  2011. SERIAL_PORT_CONTROL_RESERVED;
  2012. mv_write(PORT_SERIAL_CONTROL_REG(port_num), pscr);
  2013. pscr |= SERIAL_PORT_ENABLE;
  2014. mv_write(PORT_SERIAL_CONTROL_REG(port_num), pscr);
  2015. /* Assign port SDMA configuration */
  2016. mv_write(SDMA_CONFIG_REG(port_num),
  2017. PORT_SDMA_CONFIG_DEFAULT_VALUE);
  2018. /* Enable port Rx. */
  2019. mv643xx_eth_port_enable_rx(port_num, ETH_RX_QUEUES_ENABLED);
  2020. /* Disable port bandwidth limits by clearing MTU register */
  2021. mv_write(MAXIMUM_TRANSMIT_UNIT(port_num), 0);
  2022. /* save phy settings across reset */
  2023. mv643xx_get_settings(dev, &ethtool_cmd);
  2024. ethernet_phy_reset(mp->port_num);
  2025. mv643xx_set_settings(dev, &ethtool_cmd);
  2026. }
  2027. /*
  2028. * eth_port_uc_addr_set - Write a MAC address into the port's hw registers
  2029. */
  2030. static void eth_port_uc_addr_set(unsigned int port_num, unsigned char *p_addr)
  2031. {
  2032. unsigned int mac_h;
  2033. unsigned int mac_l;
  2034. int table;
  2035. mac_l = (p_addr[4] << 8) | (p_addr[5]);
  2036. mac_h = (p_addr[0] << 24) | (p_addr[1] << 16) | (p_addr[2] << 8) |
  2037. (p_addr[3] << 0);
  2038. mv_write(MAC_ADDR_LOW(port_num), mac_l);
  2039. mv_write(MAC_ADDR_HIGH(port_num), mac_h);
  2040. /* Accept frames with this address */
  2041. table = DA_FILTER_UNICAST_TABLE_BASE(port_num);
  2042. eth_port_set_filter_table_entry(table, p_addr[5] & 0x0f);
  2043. }
  2044. /*
  2045. * eth_port_uc_addr_get - Read the MAC address from the port's hw registers
  2046. */
  2047. static void eth_port_uc_addr_get(unsigned int port_num, unsigned char *p_addr)
  2048. {
  2049. unsigned int mac_h;
  2050. unsigned int mac_l;
  2051. mac_h = mv_read(MAC_ADDR_HIGH(port_num));
  2052. mac_l = mv_read(MAC_ADDR_LOW(port_num));
  2053. p_addr[0] = (mac_h >> 24) & 0xff;
  2054. p_addr[1] = (mac_h >> 16) & 0xff;
  2055. p_addr[2] = (mac_h >> 8) & 0xff;
  2056. p_addr[3] = mac_h & 0xff;
  2057. p_addr[4] = (mac_l >> 8) & 0xff;
  2058. p_addr[5] = mac_l & 0xff;
  2059. }
  2060. /*
  2061. * The entries in each table are indexed by a hash of a packet's MAC
  2062. * address. One bit in each entry determines whether the packet is
  2063. * accepted. There are 4 entries (each 8 bits wide) in each register
  2064. * of the table. The bits in each entry are defined as follows:
  2065. * 0 Accept=1, Drop=0
  2066. * 3-1 Queue (ETH_Q0=0)
  2067. * 7-4 Reserved = 0;
  2068. */
  2069. static void eth_port_set_filter_table_entry(int table, unsigned char entry)
  2070. {
  2071. unsigned int table_reg;
  2072. unsigned int tbl_offset;
  2073. unsigned int reg_offset;
  2074. tbl_offset = (entry / 4) * 4; /* Register offset of DA table entry */
  2075. reg_offset = entry % 4; /* Entry offset within the register */
  2076. /* Set "accepts frame bit" at specified table entry */
  2077. table_reg = mv_read(table + tbl_offset);
  2078. table_reg |= 0x01 << (8 * reg_offset);
  2079. mv_write(table + tbl_offset, table_reg);
  2080. }
  2081. /*
  2082. * eth_port_mc_addr - Multicast address settings.
  2083. *
  2084. * The MV device supports multicast using two tables:
  2085. * 1) Special Multicast Table for MAC addresses of the form
  2086. * 0x01-00-5E-00-00-XX (where XX is between 0x00 and 0x_FF).
  2087. * The MAC DA[7:0] bits are used as a pointer to the Special Multicast
  2088. * Table entries in the DA-Filter table.
  2089. * 2) Other Multicast Table for multicast of another type. A CRC-8bit
  2090. * is used as an index to the Other Multicast Table entries in the
  2091. * DA-Filter table. This function calculates the CRC-8bit value.
  2092. * In either case, eth_port_set_filter_table_entry() is then called
  2093. * to set to set the actual table entry.
  2094. */
  2095. static void eth_port_mc_addr(unsigned int eth_port_num, unsigned char *p_addr)
  2096. {
  2097. unsigned int mac_h;
  2098. unsigned int mac_l;
  2099. unsigned char crc_result = 0;
  2100. int table;
  2101. int mac_array[48];
  2102. int crc[8];
  2103. int i;
  2104. if ((p_addr[0] == 0x01) && (p_addr[1] == 0x00) &&
  2105. (p_addr[2] == 0x5E) && (p_addr[3] == 0x00) && (p_addr[4] == 0x00)) {
  2106. table = DA_FILTER_SPECIAL_MULTICAST_TABLE_BASE
  2107. (eth_port_num);
  2108. eth_port_set_filter_table_entry(table, p_addr[5]);
  2109. return;
  2110. }
  2111. /* Calculate CRC-8 out of the given address */
  2112. mac_h = (p_addr[0] << 8) | (p_addr[1]);
  2113. mac_l = (p_addr[2] << 24) | (p_addr[3] << 16) |
  2114. (p_addr[4] << 8) | (p_addr[5] << 0);
  2115. for (i = 0; i < 32; i++)
  2116. mac_array[i] = (mac_l >> i) & 0x1;
  2117. for (i = 32; i < 48; i++)
  2118. mac_array[i] = (mac_h >> (i - 32)) & 0x1;
  2119. crc[0] = mac_array[45] ^ mac_array[43] ^ mac_array[40] ^ mac_array[39] ^
  2120. mac_array[35] ^ mac_array[34] ^ mac_array[31] ^ mac_array[30] ^
  2121. mac_array[28] ^ mac_array[23] ^ mac_array[21] ^ mac_array[19] ^
  2122. mac_array[18] ^ mac_array[16] ^ mac_array[14] ^ mac_array[12] ^
  2123. mac_array[8] ^ mac_array[7] ^ mac_array[6] ^ mac_array[0];
  2124. crc[1] = mac_array[46] ^ mac_array[45] ^ mac_array[44] ^ mac_array[43] ^
  2125. mac_array[41] ^ mac_array[39] ^ mac_array[36] ^ mac_array[34] ^
  2126. mac_array[32] ^ mac_array[30] ^ mac_array[29] ^ mac_array[28] ^
  2127. mac_array[24] ^ mac_array[23] ^ mac_array[22] ^ mac_array[21] ^
  2128. mac_array[20] ^ mac_array[18] ^ mac_array[17] ^ mac_array[16] ^
  2129. mac_array[15] ^ mac_array[14] ^ mac_array[13] ^ mac_array[12] ^
  2130. mac_array[9] ^ mac_array[6] ^ mac_array[1] ^ mac_array[0];
  2131. crc[2] = mac_array[47] ^ mac_array[46] ^ mac_array[44] ^ mac_array[43] ^
  2132. mac_array[42] ^ mac_array[39] ^ mac_array[37] ^ mac_array[34] ^
  2133. mac_array[33] ^ mac_array[29] ^ mac_array[28] ^ mac_array[25] ^
  2134. mac_array[24] ^ mac_array[22] ^ mac_array[17] ^ mac_array[15] ^
  2135. mac_array[13] ^ mac_array[12] ^ mac_array[10] ^ mac_array[8] ^
  2136. mac_array[6] ^ mac_array[2] ^ mac_array[1] ^ mac_array[0];
  2137. crc[3] = mac_array[47] ^ mac_array[45] ^ mac_array[44] ^ mac_array[43] ^
  2138. mac_array[40] ^ mac_array[38] ^ mac_array[35] ^ mac_array[34] ^
  2139. mac_array[30] ^ mac_array[29] ^ mac_array[26] ^ mac_array[25] ^
  2140. mac_array[23] ^ mac_array[18] ^ mac_array[16] ^ mac_array[14] ^
  2141. mac_array[13] ^ mac_array[11] ^ mac_array[9] ^ mac_array[7] ^
  2142. mac_array[3] ^ mac_array[2] ^ mac_array[1];
  2143. crc[4] = mac_array[46] ^ mac_array[45] ^ mac_array[44] ^ mac_array[41] ^
  2144. mac_array[39] ^ mac_array[36] ^ mac_array[35] ^ mac_array[31] ^
  2145. mac_array[30] ^ mac_array[27] ^ mac_array[26] ^ mac_array[24] ^
  2146. mac_array[19] ^ mac_array[17] ^ mac_array[15] ^ mac_array[14] ^
  2147. mac_array[12] ^ mac_array[10] ^ mac_array[8] ^ mac_array[4] ^
  2148. mac_array[3] ^ mac_array[2];
  2149. crc[5] = mac_array[47] ^ mac_array[46] ^ mac_array[45] ^ mac_array[42] ^
  2150. mac_array[40] ^ mac_array[37] ^ mac_array[36] ^ mac_array[32] ^
  2151. mac_array[31] ^ mac_array[28] ^ mac_array[27] ^ mac_array[25] ^
  2152. mac_array[20] ^ mac_array[18] ^ mac_array[16] ^ mac_array[15] ^
  2153. mac_array[13] ^ mac_array[11] ^ mac_array[9] ^ mac_array[5] ^
  2154. mac_array[4] ^ mac_array[3];
  2155. crc[6] = mac_array[47] ^ mac_array[46] ^ mac_array[43] ^ mac_array[41] ^
  2156. mac_array[38] ^ mac_array[37] ^ mac_array[33] ^ mac_array[32] ^
  2157. mac_array[29] ^ mac_array[28] ^ mac_array[26] ^ mac_array[21] ^
  2158. mac_array[19] ^ mac_array[17] ^ mac_array[16] ^ mac_array[14] ^
  2159. mac_array[12] ^ mac_array[10] ^ mac_array[6] ^ mac_array[5] ^
  2160. mac_array[4];
  2161. crc[7] = mac_array[47] ^ mac_array[44] ^ mac_array[42] ^ mac_array[39] ^
  2162. mac_array[38] ^ mac_array[34] ^ mac_array[33] ^ mac_array[30] ^
  2163. mac_array[29] ^ mac_array[27] ^ mac_array[22] ^ mac_array[20] ^
  2164. mac_array[18] ^ mac_array[17] ^ mac_array[15] ^ mac_array[13] ^
  2165. mac_array[11] ^ mac_array[7] ^ mac_array[6] ^ mac_array[5];
  2166. for (i = 0; i < 8; i++)
  2167. crc_result = crc_result | (crc[i] << i);
  2168. table = DA_FILTER_OTHER_MULTICAST_TABLE_BASE(eth_port_num);
  2169. eth_port_set_filter_table_entry(table, crc_result);
  2170. }
  2171. /*
  2172. * Set the entire multicast list based on dev->mc_list.
  2173. */
  2174. static void eth_port_set_multicast_list(struct net_device *dev)
  2175. {
  2176. struct dev_mc_list *mc_list;
  2177. int i;
  2178. int table_index;
  2179. struct mv643xx_private *mp = netdev_priv(dev);
  2180. unsigned int eth_port_num = mp->port_num;
  2181. /* If the device is in promiscuous mode or in all multicast mode,
  2182. * we will fully populate both multicast tables with accept.
  2183. * This is guaranteed to yield a match on all multicast addresses...
  2184. */
  2185. if ((dev->flags & IFF_PROMISC) || (dev->flags & IFF_ALLMULTI)) {
  2186. for (table_index = 0; table_index <= 0xFC; table_index += 4) {
  2187. /* Set all entries in DA filter special multicast
  2188. * table (Ex_dFSMT)
  2189. * Set for ETH_Q0 for now
  2190. * Bits
  2191. * 0 Accept=1, Drop=0
  2192. * 3-1 Queue ETH_Q0=0
  2193. * 7-4 Reserved = 0;
  2194. */
  2195. mv_write(DA_FILTER_SPECIAL_MULTICAST_TABLE_BASE(eth_port_num) + table_index, 0x01010101);
  2196. /* Set all entries in DA filter other multicast
  2197. * table (Ex_dFOMT)
  2198. * Set for ETH_Q0 for now
  2199. * Bits
  2200. * 0 Accept=1, Drop=0
  2201. * 3-1 Queue ETH_Q0=0
  2202. * 7-4 Reserved = 0;
  2203. */
  2204. mv_write(DA_FILTER_OTHER_MULTICAST_TABLE_BASE(eth_port_num) + table_index, 0x01010101);
  2205. }
  2206. return;
  2207. }
  2208. /* We will clear out multicast tables every time we get the list.
  2209. * Then add the entire new list...
  2210. */
  2211. for (table_index = 0; table_index <= 0xFC; table_index += 4) {
  2212. /* Clear DA filter special multicast table (Ex_dFSMT) */
  2213. mv_write(DA_FILTER_SPECIAL_MULTICAST_TABLE_BASE
  2214. (eth_port_num) + table_index, 0);
  2215. /* Clear DA filter other multicast table (Ex_dFOMT) */
  2216. mv_write(DA_FILTER_OTHER_MULTICAST_TABLE_BASE
  2217. (eth_port_num) + table_index, 0);
  2218. }
  2219. /* Get pointer to net_device multicast list and add each one... */
  2220. for (i = 0, mc_list = dev->mc_list;
  2221. (i < 256) && (mc_list != NULL) && (i < dev->mc_count);
  2222. i++, mc_list = mc_list->next)
  2223. if (mc_list->dmi_addrlen == 6)
  2224. eth_port_mc_addr(eth_port_num, mc_list->dmi_addr);
  2225. }
  2226. /*
  2227. * eth_port_init_mac_tables - Clear all entrance in the UC, SMC and OMC tables
  2228. *
  2229. * DESCRIPTION:
  2230. * Go through all the DA filter tables (Unicast, Special Multicast &
  2231. * Other Multicast) and set each entry to 0.
  2232. *
  2233. * INPUT:
  2234. * unsigned int eth_port_num Ethernet Port number.
  2235. *
  2236. * OUTPUT:
  2237. * Multicast and Unicast packets are rejected.
  2238. *
  2239. * RETURN:
  2240. * None.
  2241. */
  2242. static void eth_port_init_mac_tables(unsigned int eth_port_num)
  2243. {
  2244. int table_index;
  2245. /* Clear DA filter unicast table (Ex_dFUT) */
  2246. for (table_index = 0; table_index <= 0xC; table_index += 4)
  2247. mv_write(DA_FILTER_UNICAST_TABLE_BASE
  2248. (eth_port_num) + table_index, 0);
  2249. for (table_index = 0; table_index <= 0xFC; table_index += 4) {
  2250. /* Clear DA filter special multicast table (Ex_dFSMT) */
  2251. mv_write(DA_FILTER_SPECIAL_MULTICAST_TABLE_BASE
  2252. (eth_port_num) + table_index, 0);
  2253. /* Clear DA filter other multicast table (Ex_dFOMT) */
  2254. mv_write(DA_FILTER_OTHER_MULTICAST_TABLE_BASE
  2255. (eth_port_num) + table_index, 0);
  2256. }
  2257. }
  2258. /*
  2259. * eth_clear_mib_counters - Clear all MIB counters
  2260. *
  2261. * DESCRIPTION:
  2262. * This function clears all MIB counters of a specific ethernet port.
  2263. * A read from the MIB counter will reset the counter.
  2264. *
  2265. * INPUT:
  2266. * unsigned int eth_port_num Ethernet Port number.
  2267. *
  2268. * OUTPUT:
  2269. * After reading all MIB counters, the counters resets.
  2270. *
  2271. * RETURN:
  2272. * MIB counter value.
  2273. *
  2274. */
  2275. static void eth_clear_mib_counters(unsigned int eth_port_num)
  2276. {
  2277. int i;
  2278. /* Perform dummy reads from MIB counters */
  2279. for (i = ETH_MIB_GOOD_OCTETS_RECEIVED_LOW; i < ETH_MIB_LATE_COLLISION;
  2280. i += 4)
  2281. mv_read(MIB_COUNTERS_BASE(eth_port_num) + i);
  2282. }
  2283. static inline u32 read_mib(struct mv643xx_private *mp, int offset)
  2284. {
  2285. return mv_read(MIB_COUNTERS_BASE(mp->port_num) + offset);
  2286. }
  2287. static void eth_update_mib_counters(struct mv643xx_private *mp)
  2288. {
  2289. struct mv643xx_mib_counters *p = &mp->mib_counters;
  2290. int offset;
  2291. p->good_octets_received +=
  2292. read_mib(mp, ETH_MIB_GOOD_OCTETS_RECEIVED_LOW);
  2293. p->good_octets_received +=
  2294. (u64)read_mib(mp, ETH_MIB_GOOD_OCTETS_RECEIVED_HIGH) << 32;
  2295. for (offset = ETH_MIB_BAD_OCTETS_RECEIVED;
  2296. offset <= ETH_MIB_FRAMES_1024_TO_MAX_OCTETS;
  2297. offset += 4)
  2298. *(u32 *)((char *)p + offset) += read_mib(mp, offset);
  2299. p->good_octets_sent += read_mib(mp, ETH_MIB_GOOD_OCTETS_SENT_LOW);
  2300. p->good_octets_sent +=
  2301. (u64)read_mib(mp, ETH_MIB_GOOD_OCTETS_SENT_HIGH) << 32;
  2302. for (offset = ETH_MIB_GOOD_FRAMES_SENT;
  2303. offset <= ETH_MIB_LATE_COLLISION;
  2304. offset += 4)
  2305. *(u32 *)((char *)p + offset) += read_mib(mp, offset);
  2306. }
  2307. /*
  2308. * ethernet_phy_detect - Detect whether a phy is present
  2309. *
  2310. * DESCRIPTION:
  2311. * This function tests whether there is a PHY present on
  2312. * the specified port.
  2313. *
  2314. * INPUT:
  2315. * unsigned int eth_port_num Ethernet Port number.
  2316. *
  2317. * OUTPUT:
  2318. * None
  2319. *
  2320. * RETURN:
  2321. * 0 on success
  2322. * -ENODEV on failure
  2323. *
  2324. */
  2325. static int ethernet_phy_detect(unsigned int port_num)
  2326. {
  2327. unsigned int phy_reg_data0;
  2328. int auto_neg;
  2329. eth_port_read_smi_reg(port_num, 0, &phy_reg_data0);
  2330. auto_neg = phy_reg_data0 & 0x1000;
  2331. phy_reg_data0 ^= 0x1000; /* invert auto_neg */
  2332. eth_port_write_smi_reg(port_num, 0, phy_reg_data0);
  2333. eth_port_read_smi_reg(port_num, 0, &phy_reg_data0);
  2334. if ((phy_reg_data0 & 0x1000) == auto_neg)
  2335. return -ENODEV; /* change didn't take */
  2336. phy_reg_data0 ^= 0x1000;
  2337. eth_port_write_smi_reg(port_num, 0, phy_reg_data0);
  2338. return 0;
  2339. }
  2340. /*
  2341. * ethernet_phy_get - Get the ethernet port PHY address.
  2342. *
  2343. * DESCRIPTION:
  2344. * This routine returns the given ethernet port PHY address.
  2345. *
  2346. * INPUT:
  2347. * unsigned int eth_port_num Ethernet Port number.
  2348. *
  2349. * OUTPUT:
  2350. * None.
  2351. *
  2352. * RETURN:
  2353. * PHY address.
  2354. *
  2355. */
  2356. static int ethernet_phy_get(unsigned int eth_port_num)
  2357. {
  2358. unsigned int reg_data;
  2359. reg_data = mv_read(PHY_ADDR_REG);
  2360. return ((reg_data >> (5 * eth_port_num)) & 0x1f);
  2361. }
  2362. /*
  2363. * ethernet_phy_set - Set the ethernet port PHY address.
  2364. *
  2365. * DESCRIPTION:
  2366. * This routine sets the given ethernet port PHY address.
  2367. *
  2368. * INPUT:
  2369. * unsigned int eth_port_num Ethernet Port number.
  2370. * int phy_addr PHY address.
  2371. *
  2372. * OUTPUT:
  2373. * None.
  2374. *
  2375. * RETURN:
  2376. * None.
  2377. *
  2378. */
  2379. static void ethernet_phy_set(unsigned int eth_port_num, int phy_addr)
  2380. {
  2381. u32 reg_data;
  2382. int addr_shift = 5 * eth_port_num;
  2383. reg_data = mv_read(PHY_ADDR_REG);
  2384. reg_data &= ~(0x1f << addr_shift);
  2385. reg_data |= (phy_addr & 0x1f) << addr_shift;
  2386. mv_write(PHY_ADDR_REG, reg_data);
  2387. }
  2388. /*
  2389. * ethernet_phy_reset - Reset Ethernet port PHY.
  2390. *
  2391. * DESCRIPTION:
  2392. * This routine utilizes the SMI interface to reset the ethernet port PHY.
  2393. *
  2394. * INPUT:
  2395. * unsigned int eth_port_num Ethernet Port number.
  2396. *
  2397. * OUTPUT:
  2398. * The PHY is reset.
  2399. *
  2400. * RETURN:
  2401. * None.
  2402. *
  2403. */
  2404. static void ethernet_phy_reset(unsigned int eth_port_num)
  2405. {
  2406. unsigned int phy_reg_data;
  2407. /* Reset the PHY */
  2408. eth_port_read_smi_reg(eth_port_num, 0, &phy_reg_data);
  2409. phy_reg_data |= 0x8000; /* Set bit 15 to reset the PHY */
  2410. eth_port_write_smi_reg(eth_port_num, 0, phy_reg_data);
  2411. /* wait for PHY to come out of reset */
  2412. do {
  2413. udelay(1);
  2414. eth_port_read_smi_reg(eth_port_num, 0, &phy_reg_data);
  2415. } while (phy_reg_data & 0x8000);
  2416. }
  2417. static void mv643xx_eth_port_enable_tx(unsigned int port_num,
  2418. unsigned int queues)
  2419. {
  2420. mv_write(TRANSMIT_QUEUE_COMMAND_REG(port_num), queues);
  2421. }
  2422. static void mv643xx_eth_port_enable_rx(unsigned int port_num,
  2423. unsigned int queues)
  2424. {
  2425. mv_write(RECEIVE_QUEUE_COMMAND_REG(port_num), queues);
  2426. }
  2427. static unsigned int mv643xx_eth_port_disable_tx(unsigned int port_num)
  2428. {
  2429. u32 queues;
  2430. /* Stop Tx port activity. Check port Tx activity. */
  2431. queues = mv_read(TRANSMIT_QUEUE_COMMAND_REG(port_num)) & 0xFF;
  2432. if (queues) {
  2433. /* Issue stop command for active queues only */
  2434. mv_write(TRANSMIT_QUEUE_COMMAND_REG(port_num), (queues << 8));
  2435. /* Wait for all Tx activity to terminate. */
  2436. /* Check port cause register that all Tx queues are stopped */
  2437. while (mv_read(TRANSMIT_QUEUE_COMMAND_REG(port_num)) & 0xFF)
  2438. udelay(PHY_WAIT_MICRO_SECONDS);
  2439. /* Wait for Tx FIFO to empty */
  2440. while (mv_read(PORT_STATUS_REG(port_num)) &
  2441. ETH_PORT_TX_FIFO_EMPTY)
  2442. udelay(PHY_WAIT_MICRO_SECONDS);
  2443. }
  2444. return queues;
  2445. }
  2446. static unsigned int mv643xx_eth_port_disable_rx(unsigned int port_num)
  2447. {
  2448. u32 queues;
  2449. /* Stop Rx port activity. Check port Rx activity. */
  2450. queues = mv_read(RECEIVE_QUEUE_COMMAND_REG(port_num)) & 0xFF;
  2451. if (queues) {
  2452. /* Issue stop command for active queues only */
  2453. mv_write(RECEIVE_QUEUE_COMMAND_REG(port_num), (queues << 8));
  2454. /* Wait for all Rx activity to terminate. */
  2455. /* Check port cause register that all Rx queues are stopped */
  2456. while (mv_read(RECEIVE_QUEUE_COMMAND_REG(port_num)) & 0xFF)
  2457. udelay(PHY_WAIT_MICRO_SECONDS);
  2458. }
  2459. return queues;
  2460. }
  2461. /*
  2462. * eth_port_reset - Reset Ethernet port
  2463. *
  2464. * DESCRIPTION:
  2465. * This routine resets the chip by aborting any SDMA engine activity and
  2466. * clearing the MIB counters. The Receiver and the Transmit unit are in
  2467. * idle state after this command is performed and the port is disabled.
  2468. *
  2469. * INPUT:
  2470. * unsigned int eth_port_num Ethernet Port number.
  2471. *
  2472. * OUTPUT:
  2473. * Channel activity is halted.
  2474. *
  2475. * RETURN:
  2476. * None.
  2477. *
  2478. */
  2479. static void eth_port_reset(unsigned int port_num)
  2480. {
  2481. unsigned int reg_data;
  2482. mv643xx_eth_port_disable_tx(port_num);
  2483. mv643xx_eth_port_disable_rx(port_num);
  2484. /* Clear all MIB counters */
  2485. eth_clear_mib_counters(port_num);
  2486. /* Reset the Enable bit in the Configuration Register */
  2487. reg_data = mv_read(PORT_SERIAL_CONTROL_REG(port_num));
  2488. reg_data &= ~(SERIAL_PORT_ENABLE |
  2489. DO_NOT_FORCE_LINK_FAIL |
  2490. FORCE_LINK_PASS);
  2491. mv_write(PORT_SERIAL_CONTROL_REG(port_num), reg_data);
  2492. }
  2493. /*
  2494. * eth_port_read_smi_reg - Read PHY registers
  2495. *
  2496. * DESCRIPTION:
  2497. * This routine utilize the SMI interface to interact with the PHY in
  2498. * order to perform PHY register read.
  2499. *
  2500. * INPUT:
  2501. * unsigned int port_num Ethernet Port number.
  2502. * unsigned int phy_reg PHY register address offset.
  2503. * unsigned int *value Register value buffer.
  2504. *
  2505. * OUTPUT:
  2506. * Write the value of a specified PHY register into given buffer.
  2507. *
  2508. * RETURN:
  2509. * false if the PHY is busy or read data is not in valid state.
  2510. * true otherwise.
  2511. *
  2512. */
  2513. static void eth_port_read_smi_reg(unsigned int port_num,
  2514. unsigned int phy_reg, unsigned int *value)
  2515. {
  2516. int phy_addr = ethernet_phy_get(port_num);
  2517. unsigned long flags;
  2518. int i;
  2519. /* the SMI register is a shared resource */
  2520. spin_lock_irqsave(&mv643xx_eth_phy_lock, flags);
  2521. /* wait for the SMI register to become available */
  2522. for (i = 0; mv_read(SMI_REG) & ETH_SMI_BUSY; i++) {
  2523. if (i == PHY_WAIT_ITERATIONS) {
  2524. printk("mv643xx PHY busy timeout, port %d\n", port_num);
  2525. goto out;
  2526. }
  2527. udelay(PHY_WAIT_MICRO_SECONDS);
  2528. }
  2529. mv_write(SMI_REG,
  2530. (phy_addr << 16) | (phy_reg << 21) | ETH_SMI_OPCODE_READ);
  2531. /* now wait for the data to be valid */
  2532. for (i = 0; !(mv_read(SMI_REG) & ETH_SMI_READ_VALID); i++) {
  2533. if (i == PHY_WAIT_ITERATIONS) {
  2534. printk("mv643xx PHY read timeout, port %d\n", port_num);
  2535. goto out;
  2536. }
  2537. udelay(PHY_WAIT_MICRO_SECONDS);
  2538. }
  2539. *value = mv_read(SMI_REG) & 0xffff;
  2540. out:
  2541. spin_unlock_irqrestore(&mv643xx_eth_phy_lock, flags);
  2542. }
  2543. /*
  2544. * eth_port_write_smi_reg - Write to PHY registers
  2545. *
  2546. * DESCRIPTION:
  2547. * This routine utilize the SMI interface to interact with the PHY in
  2548. * order to perform writes to PHY registers.
  2549. *
  2550. * INPUT:
  2551. * unsigned int eth_port_num Ethernet Port number.
  2552. * unsigned int phy_reg PHY register address offset.
  2553. * unsigned int value Register value.
  2554. *
  2555. * OUTPUT:
  2556. * Write the given value to the specified PHY register.
  2557. *
  2558. * RETURN:
  2559. * false if the PHY is busy.
  2560. * true otherwise.
  2561. *
  2562. */
  2563. static void eth_port_write_smi_reg(unsigned int eth_port_num,
  2564. unsigned int phy_reg, unsigned int value)
  2565. {
  2566. int phy_addr;
  2567. int i;
  2568. unsigned long flags;
  2569. phy_addr = ethernet_phy_get(eth_port_num);
  2570. /* the SMI register is a shared resource */
  2571. spin_lock_irqsave(&mv643xx_eth_phy_lock, flags);
  2572. /* wait for the SMI register to become available */
  2573. for (i = 0; mv_read(SMI_REG) & ETH_SMI_BUSY; i++) {
  2574. if (i == PHY_WAIT_ITERATIONS) {
  2575. printk("mv643xx PHY busy timeout, port %d\n",
  2576. eth_port_num);
  2577. goto out;
  2578. }
  2579. udelay(PHY_WAIT_MICRO_SECONDS);
  2580. }
  2581. mv_write(SMI_REG, (phy_addr << 16) | (phy_reg << 21) |
  2582. ETH_SMI_OPCODE_WRITE | (value & 0xffff));
  2583. out:
  2584. spin_unlock_irqrestore(&mv643xx_eth_phy_lock, flags);
  2585. }
  2586. /*
  2587. * Wrappers for MII support library.
  2588. */
  2589. static int mv643xx_mdio_read(struct net_device *dev, int phy_id, int location)
  2590. {
  2591. int val;
  2592. struct mv643xx_private *mp = netdev_priv(dev);
  2593. eth_port_read_smi_reg(mp->port_num, location, &val);
  2594. return val;
  2595. }
  2596. static void mv643xx_mdio_write(struct net_device *dev, int phy_id, int location, int val)
  2597. {
  2598. struct mv643xx_private *mp = netdev_priv(dev);
  2599. eth_port_write_smi_reg(mp->port_num, location, val);
  2600. }
  2601. /*
  2602. * eth_port_receive - Get received information from Rx ring.
  2603. *
  2604. * DESCRIPTION:
  2605. * This routine returns the received data to the caller. There is no
  2606. * data copying during routine operation. All information is returned
  2607. * using pointer to packet information struct passed from the caller.
  2608. * If the routine exhausts Rx ring resources then the resource error flag
  2609. * is set.
  2610. *
  2611. * INPUT:
  2612. * struct mv643xx_private *mp Ethernet Port Control srtuct.
  2613. * struct pkt_info *p_pkt_info User packet buffer.
  2614. *
  2615. * OUTPUT:
  2616. * Rx ring current and used indexes are updated.
  2617. *
  2618. * RETURN:
  2619. * ETH_ERROR in case the routine can not access Rx desc ring.
  2620. * ETH_QUEUE_FULL if Rx ring resources are exhausted.
  2621. * ETH_END_OF_JOB if there is no received data.
  2622. * ETH_OK otherwise.
  2623. */
  2624. static ETH_FUNC_RET_STATUS eth_port_receive(struct mv643xx_private *mp,
  2625. struct pkt_info *p_pkt_info)
  2626. {
  2627. int rx_next_curr_desc, rx_curr_desc, rx_used_desc;
  2628. volatile struct eth_rx_desc *p_rx_desc;
  2629. unsigned int command_status;
  2630. unsigned long flags;
  2631. /* Do not process Rx ring in case of Rx ring resource error */
  2632. if (mp->rx_resource_err)
  2633. return ETH_QUEUE_FULL;
  2634. spin_lock_irqsave(&mp->lock, flags);
  2635. /* Get the Rx Desc ring 'curr and 'used' indexes */
  2636. rx_curr_desc = mp->rx_curr_desc_q;
  2637. rx_used_desc = mp->rx_used_desc_q;
  2638. p_rx_desc = &mp->p_rx_desc_area[rx_curr_desc];
  2639. /* The following parameters are used to save readings from memory */
  2640. command_status = p_rx_desc->cmd_sts;
  2641. rmb();
  2642. /* Nothing to receive... */
  2643. if (command_status & (ETH_BUFFER_OWNED_BY_DMA)) {
  2644. spin_unlock_irqrestore(&mp->lock, flags);
  2645. return ETH_END_OF_JOB;
  2646. }
  2647. p_pkt_info->byte_cnt = (p_rx_desc->byte_cnt) - RX_BUF_OFFSET;
  2648. p_pkt_info->cmd_sts = command_status;
  2649. p_pkt_info->buf_ptr = (p_rx_desc->buf_ptr) + RX_BUF_OFFSET;
  2650. p_pkt_info->return_info = mp->rx_skb[rx_curr_desc];
  2651. p_pkt_info->l4i_chk = p_rx_desc->buf_size;
  2652. /*
  2653. * Clean the return info field to indicate that the
  2654. * packet has been moved to the upper layers
  2655. */
  2656. mp->rx_skb[rx_curr_desc] = NULL;
  2657. /* Update current index in data structure */
  2658. rx_next_curr_desc = (rx_curr_desc + 1) % mp->rx_ring_size;
  2659. mp->rx_curr_desc_q = rx_next_curr_desc;
  2660. /* Rx descriptors exhausted. Set the Rx ring resource error flag */
  2661. if (rx_next_curr_desc == rx_used_desc)
  2662. mp->rx_resource_err = 1;
  2663. spin_unlock_irqrestore(&mp->lock, flags);
  2664. return ETH_OK;
  2665. }
  2666. /*
  2667. * eth_rx_return_buff - Returns a Rx buffer back to the Rx ring.
  2668. *
  2669. * DESCRIPTION:
  2670. * This routine returns a Rx buffer back to the Rx ring. It retrieves the
  2671. * next 'used' descriptor and attached the returned buffer to it.
  2672. * In case the Rx ring was in "resource error" condition, where there are
  2673. * no available Rx resources, the function resets the resource error flag.
  2674. *
  2675. * INPUT:
  2676. * struct mv643xx_private *mp Ethernet Port Control srtuct.
  2677. * struct pkt_info *p_pkt_info Information on returned buffer.
  2678. *
  2679. * OUTPUT:
  2680. * New available Rx resource in Rx descriptor ring.
  2681. *
  2682. * RETURN:
  2683. * ETH_ERROR in case the routine can not access Rx desc ring.
  2684. * ETH_OK otherwise.
  2685. */
  2686. static ETH_FUNC_RET_STATUS eth_rx_return_buff(struct mv643xx_private *mp,
  2687. struct pkt_info *p_pkt_info)
  2688. {
  2689. int used_rx_desc; /* Where to return Rx resource */
  2690. volatile struct eth_rx_desc *p_used_rx_desc;
  2691. unsigned long flags;
  2692. spin_lock_irqsave(&mp->lock, flags);
  2693. /* Get 'used' Rx descriptor */
  2694. used_rx_desc = mp->rx_used_desc_q;
  2695. p_used_rx_desc = &mp->p_rx_desc_area[used_rx_desc];
  2696. p_used_rx_desc->buf_ptr = p_pkt_info->buf_ptr;
  2697. p_used_rx_desc->buf_size = p_pkt_info->byte_cnt;
  2698. mp->rx_skb[used_rx_desc] = p_pkt_info->return_info;
  2699. /* Flush the write pipe */
  2700. /* Return the descriptor to DMA ownership */
  2701. wmb();
  2702. p_used_rx_desc->cmd_sts =
  2703. ETH_BUFFER_OWNED_BY_DMA | ETH_RX_ENABLE_INTERRUPT;
  2704. wmb();
  2705. /* Move the used descriptor pointer to the next descriptor */
  2706. mp->rx_used_desc_q = (used_rx_desc + 1) % mp->rx_ring_size;
  2707. /* Any Rx return cancels the Rx resource error status */
  2708. mp->rx_resource_err = 0;
  2709. spin_unlock_irqrestore(&mp->lock, flags);
  2710. return ETH_OK;
  2711. }
  2712. /************* Begin ethtool support *************************/
  2713. struct mv643xx_stats {
  2714. char stat_string[ETH_GSTRING_LEN];
  2715. int sizeof_stat;
  2716. int stat_offset;
  2717. };
  2718. #define MV643XX_STAT(m) sizeof(((struct mv643xx_private *)0)->m), \
  2719. offsetof(struct mv643xx_private, m)
  2720. static const struct mv643xx_stats mv643xx_gstrings_stats[] = {
  2721. { "rx_packets", MV643XX_STAT(stats.rx_packets) },
  2722. { "tx_packets", MV643XX_STAT(stats.tx_packets) },
  2723. { "rx_bytes", MV643XX_STAT(stats.rx_bytes) },
  2724. { "tx_bytes", MV643XX_STAT(stats.tx_bytes) },
  2725. { "rx_errors", MV643XX_STAT(stats.rx_errors) },
  2726. { "tx_errors", MV643XX_STAT(stats.tx_errors) },
  2727. { "rx_dropped", MV643XX_STAT(stats.rx_dropped) },
  2728. { "tx_dropped", MV643XX_STAT(stats.tx_dropped) },
  2729. { "good_octets_received", MV643XX_STAT(mib_counters.good_octets_received) },
  2730. { "bad_octets_received", MV643XX_STAT(mib_counters.bad_octets_received) },
  2731. { "internal_mac_transmit_err", MV643XX_STAT(mib_counters.internal_mac_transmit_err) },
  2732. { "good_frames_received", MV643XX_STAT(mib_counters.good_frames_received) },
  2733. { "bad_frames_received", MV643XX_STAT(mib_counters.bad_frames_received) },
  2734. { "broadcast_frames_received", MV643XX_STAT(mib_counters.broadcast_frames_received) },
  2735. { "multicast_frames_received", MV643XX_STAT(mib_counters.multicast_frames_received) },
  2736. { "frames_64_octets", MV643XX_STAT(mib_counters.frames_64_octets) },
  2737. { "frames_65_to_127_octets", MV643XX_STAT(mib_counters.frames_65_to_127_octets) },
  2738. { "frames_128_to_255_octets", MV643XX_STAT(mib_counters.frames_128_to_255_octets) },
  2739. { "frames_256_to_511_octets", MV643XX_STAT(mib_counters.frames_256_to_511_octets) },
  2740. { "frames_512_to_1023_octets", MV643XX_STAT(mib_counters.frames_512_to_1023_octets) },
  2741. { "frames_1024_to_max_octets", MV643XX_STAT(mib_counters.frames_1024_to_max_octets) },
  2742. { "good_octets_sent", MV643XX_STAT(mib_counters.good_octets_sent) },
  2743. { "good_frames_sent", MV643XX_STAT(mib_counters.good_frames_sent) },
  2744. { "excessive_collision", MV643XX_STAT(mib_counters.excessive_collision) },
  2745. { "multicast_frames_sent", MV643XX_STAT(mib_counters.multicast_frames_sent) },
  2746. { "broadcast_frames_sent", MV643XX_STAT(mib_counters.broadcast_frames_sent) },
  2747. { "unrec_mac_control_received", MV643XX_STAT(mib_counters.unrec_mac_control_received) },
  2748. { "fc_sent", MV643XX_STAT(mib_counters.fc_sent) },
  2749. { "good_fc_received", MV643XX_STAT(mib_counters.good_fc_received) },
  2750. { "bad_fc_received", MV643XX_STAT(mib_counters.bad_fc_received) },
  2751. { "undersize_received", MV643XX_STAT(mib_counters.undersize_received) },
  2752. { "fragments_received", MV643XX_STAT(mib_counters.fragments_received) },
  2753. { "oversize_received", MV643XX_STAT(mib_counters.oversize_received) },
  2754. { "jabber_received", MV643XX_STAT(mib_counters.jabber_received) },
  2755. { "mac_receive_error", MV643XX_STAT(mib_counters.mac_receive_error) },
  2756. { "bad_crc_event", MV643XX_STAT(mib_counters.bad_crc_event) },
  2757. { "collision", MV643XX_STAT(mib_counters.collision) },
  2758. { "late_collision", MV643XX_STAT(mib_counters.late_collision) },
  2759. };
  2760. #define MV643XX_STATS_LEN ARRAY_SIZE(mv643xx_gstrings_stats)
  2761. static void mv643xx_get_drvinfo(struct net_device *netdev,
  2762. struct ethtool_drvinfo *drvinfo)
  2763. {
  2764. strncpy(drvinfo->driver, mv643xx_driver_name, 32);
  2765. strncpy(drvinfo->version, mv643xx_driver_version, 32);
  2766. strncpy(drvinfo->fw_version, "N/A", 32);
  2767. strncpy(drvinfo->bus_info, "mv643xx", 32);
  2768. drvinfo->n_stats = MV643XX_STATS_LEN;
  2769. }
  2770. static int mv643xx_get_sset_count(struct net_device *netdev, int sset)
  2771. {
  2772. switch (sset) {
  2773. case ETH_SS_STATS:
  2774. return MV643XX_STATS_LEN;
  2775. default:
  2776. return -EOPNOTSUPP;
  2777. }
  2778. }
  2779. static void mv643xx_get_ethtool_stats(struct net_device *netdev,
  2780. struct ethtool_stats *stats, uint64_t *data)
  2781. {
  2782. struct mv643xx_private *mp = netdev->priv;
  2783. int i;
  2784. eth_update_mib_counters(mp);
  2785. for (i = 0; i < MV643XX_STATS_LEN; i++) {
  2786. char *p = (char *)mp+mv643xx_gstrings_stats[i].stat_offset;
  2787. data[i] = (mv643xx_gstrings_stats[i].sizeof_stat ==
  2788. sizeof(uint64_t)) ? *(uint64_t *)p : *(uint32_t *)p;
  2789. }
  2790. }
  2791. static void mv643xx_get_strings(struct net_device *netdev, uint32_t stringset,
  2792. uint8_t *data)
  2793. {
  2794. int i;
  2795. switch(stringset) {
  2796. case ETH_SS_STATS:
  2797. for (i=0; i < MV643XX_STATS_LEN; i++) {
  2798. memcpy(data + i * ETH_GSTRING_LEN,
  2799. mv643xx_gstrings_stats[i].stat_string,
  2800. ETH_GSTRING_LEN);
  2801. }
  2802. break;
  2803. }
  2804. }
  2805. static u32 mv643xx_eth_get_link(struct net_device *dev)
  2806. {
  2807. struct mv643xx_private *mp = netdev_priv(dev);
  2808. return mii_link_ok(&mp->mii);
  2809. }
  2810. static int mv643xx_eth_nway_restart(struct net_device *dev)
  2811. {
  2812. struct mv643xx_private *mp = netdev_priv(dev);
  2813. return mii_nway_restart(&mp->mii);
  2814. }
  2815. static int mv643xx_eth_do_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  2816. {
  2817. struct mv643xx_private *mp = netdev_priv(dev);
  2818. return generic_mii_ioctl(&mp->mii, if_mii(ifr), cmd, NULL);
  2819. }
  2820. static const struct ethtool_ops mv643xx_ethtool_ops = {
  2821. .get_settings = mv643xx_get_settings,
  2822. .set_settings = mv643xx_set_settings,
  2823. .get_drvinfo = mv643xx_get_drvinfo,
  2824. .get_link = mv643xx_eth_get_link,
  2825. .set_sg = ethtool_op_set_sg,
  2826. .get_sset_count = mv643xx_get_sset_count,
  2827. .get_ethtool_stats = mv643xx_get_ethtool_stats,
  2828. .get_strings = mv643xx_get_strings,
  2829. .nway_reset = mv643xx_eth_nway_restart,
  2830. };
  2831. /************* End ethtool support *************************/