mv643xx_eth.c 86 KB

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