mv643xx_eth.c 86 KB

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