mv643xx_eth.c 84 KB

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