mv643xx_eth.c 85 KB

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