mv643xx_eth.c 96 KB

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