mv643xx_eth.c 84 KB

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