mv643xx_eth.c 73 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132
  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. * Copyright (C) 2013 Michael Stapelberg <michael@stapelberg.de>
  24. *
  25. * This program is free software; you can redistribute it and/or
  26. * modify it under the terms of the GNU General Public License
  27. * as published by the Free Software Foundation; either version 2
  28. * of the License, or (at your option) any later version.
  29. *
  30. * This program is distributed in the hope that it will be useful,
  31. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  32. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  33. * GNU General Public License for more details.
  34. *
  35. * You should have received a copy of the GNU General Public License
  36. * along with this program; if not, write to the Free Software
  37. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  38. */
  39. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  40. #include <linux/init.h>
  41. #include <linux/dma-mapping.h>
  42. #include <linux/in.h>
  43. #include <linux/ip.h>
  44. #include <linux/tcp.h>
  45. #include <linux/udp.h>
  46. #include <linux/etherdevice.h>
  47. #include <linux/delay.h>
  48. #include <linux/ethtool.h>
  49. #include <linux/platform_device.h>
  50. #include <linux/module.h>
  51. #include <linux/kernel.h>
  52. #include <linux/spinlock.h>
  53. #include <linux/workqueue.h>
  54. #include <linux/phy.h>
  55. #include <linux/mv643xx_eth.h>
  56. #include <linux/io.h>
  57. #include <linux/types.h>
  58. #include <linux/inet_lro.h>
  59. #include <linux/slab.h>
  60. #include <linux/clk.h>
  61. static char mv643xx_eth_driver_name[] = "mv643xx_eth";
  62. static char mv643xx_eth_driver_version[] = "1.4";
  63. /*
  64. * Registers shared between all ports.
  65. */
  66. #define PHY_ADDR 0x0000
  67. #define SMI_REG 0x0004
  68. #define SMI_BUSY 0x10000000
  69. #define SMI_READ_VALID 0x08000000
  70. #define SMI_OPCODE_READ 0x04000000
  71. #define SMI_OPCODE_WRITE 0x00000000
  72. #define ERR_INT_CAUSE 0x0080
  73. #define ERR_INT_SMI_DONE 0x00000010
  74. #define ERR_INT_MASK 0x0084
  75. #define WINDOW_BASE(w) (0x0200 + ((w) << 3))
  76. #define WINDOW_SIZE(w) (0x0204 + ((w) << 3))
  77. #define WINDOW_REMAP_HIGH(w) (0x0280 + ((w) << 2))
  78. #define WINDOW_BAR_ENABLE 0x0290
  79. #define WINDOW_PROTECT(w) (0x0294 + ((w) << 4))
  80. /*
  81. * Main per-port registers. These live at offset 0x0400 for
  82. * port #0, 0x0800 for port #1, and 0x0c00 for port #2.
  83. */
  84. #define PORT_CONFIG 0x0000
  85. #define UNICAST_PROMISCUOUS_MODE 0x00000001
  86. #define PORT_CONFIG_EXT 0x0004
  87. #define MAC_ADDR_LOW 0x0014
  88. #define MAC_ADDR_HIGH 0x0018
  89. #define SDMA_CONFIG 0x001c
  90. #define TX_BURST_SIZE_16_64BIT 0x01000000
  91. #define TX_BURST_SIZE_4_64BIT 0x00800000
  92. #define BLM_TX_NO_SWAP 0x00000020
  93. #define BLM_RX_NO_SWAP 0x00000010
  94. #define RX_BURST_SIZE_16_64BIT 0x00000008
  95. #define RX_BURST_SIZE_4_64BIT 0x00000004
  96. #define PORT_SERIAL_CONTROL 0x003c
  97. #define SET_MII_SPEED_TO_100 0x01000000
  98. #define SET_GMII_SPEED_TO_1000 0x00800000
  99. #define SET_FULL_DUPLEX_MODE 0x00200000
  100. #define MAX_RX_PACKET_9700BYTE 0x000a0000
  101. #define DISABLE_AUTO_NEG_SPEED_GMII 0x00002000
  102. #define DO_NOT_FORCE_LINK_FAIL 0x00000400
  103. #define SERIAL_PORT_CONTROL_RESERVED 0x00000200
  104. #define DISABLE_AUTO_NEG_FOR_FLOW_CTRL 0x00000008
  105. #define DISABLE_AUTO_NEG_FOR_DUPLEX 0x00000004
  106. #define FORCE_LINK_PASS 0x00000002
  107. #define SERIAL_PORT_ENABLE 0x00000001
  108. #define PORT_STATUS 0x0044
  109. #define TX_FIFO_EMPTY 0x00000400
  110. #define TX_IN_PROGRESS 0x00000080
  111. #define PORT_SPEED_MASK 0x00000030
  112. #define PORT_SPEED_1000 0x00000010
  113. #define PORT_SPEED_100 0x00000020
  114. #define PORT_SPEED_10 0x00000000
  115. #define FLOW_CONTROL_ENABLED 0x00000008
  116. #define FULL_DUPLEX 0x00000004
  117. #define LINK_UP 0x00000002
  118. #define TXQ_COMMAND 0x0048
  119. #define TXQ_FIX_PRIO_CONF 0x004c
  120. #define TX_BW_RATE 0x0050
  121. #define TX_BW_MTU 0x0058
  122. #define TX_BW_BURST 0x005c
  123. #define INT_CAUSE 0x0060
  124. #define INT_TX_END 0x07f80000
  125. #define INT_TX_END_0 0x00080000
  126. #define INT_RX 0x000003fc
  127. #define INT_RX_0 0x00000004
  128. #define INT_EXT 0x00000002
  129. #define INT_CAUSE_EXT 0x0064
  130. #define INT_EXT_LINK_PHY 0x00110000
  131. #define INT_EXT_TX 0x000000ff
  132. #define INT_MASK 0x0068
  133. #define INT_MASK_EXT 0x006c
  134. #define TX_FIFO_URGENT_THRESHOLD 0x0074
  135. #define RX_DISCARD_FRAME_CNT 0x0084
  136. #define RX_OVERRUN_FRAME_CNT 0x0088
  137. #define TXQ_FIX_PRIO_CONF_MOVED 0x00dc
  138. #define TX_BW_RATE_MOVED 0x00e0
  139. #define TX_BW_MTU_MOVED 0x00e8
  140. #define TX_BW_BURST_MOVED 0x00ec
  141. #define RXQ_CURRENT_DESC_PTR(q) (0x020c + ((q) << 4))
  142. #define RXQ_COMMAND 0x0280
  143. #define TXQ_CURRENT_DESC_PTR(q) (0x02c0 + ((q) << 2))
  144. #define TXQ_BW_TOKENS(q) (0x0300 + ((q) << 4))
  145. #define TXQ_BW_CONF(q) (0x0304 + ((q) << 4))
  146. #define TXQ_BW_WRR_CONF(q) (0x0308 + ((q) << 4))
  147. /*
  148. * Misc per-port registers.
  149. */
  150. #define MIB_COUNTERS(p) (0x1000 + ((p) << 7))
  151. #define SPECIAL_MCAST_TABLE(p) (0x1400 + ((p) << 10))
  152. #define OTHER_MCAST_TABLE(p) (0x1500 + ((p) << 10))
  153. #define UNICAST_TABLE(p) (0x1600 + ((p) << 10))
  154. /*
  155. * SDMA configuration register default value.
  156. */
  157. #if defined(__BIG_ENDIAN)
  158. #define PORT_SDMA_CONFIG_DEFAULT_VALUE \
  159. (RX_BURST_SIZE_4_64BIT | \
  160. TX_BURST_SIZE_4_64BIT)
  161. #elif defined(__LITTLE_ENDIAN)
  162. #define PORT_SDMA_CONFIG_DEFAULT_VALUE \
  163. (RX_BURST_SIZE_4_64BIT | \
  164. BLM_RX_NO_SWAP | \
  165. BLM_TX_NO_SWAP | \
  166. TX_BURST_SIZE_4_64BIT)
  167. #else
  168. #error One of __BIG_ENDIAN or __LITTLE_ENDIAN must be defined
  169. #endif
  170. /*
  171. * Misc definitions.
  172. */
  173. #define DEFAULT_RX_QUEUE_SIZE 128
  174. #define DEFAULT_TX_QUEUE_SIZE 256
  175. #define SKB_DMA_REALIGN ((PAGE_SIZE - NET_SKB_PAD) % SMP_CACHE_BYTES)
  176. /*
  177. * RX/TX descriptors.
  178. */
  179. #if defined(__BIG_ENDIAN)
  180. struct rx_desc {
  181. u16 byte_cnt; /* Descriptor buffer byte count */
  182. u16 buf_size; /* Buffer size */
  183. u32 cmd_sts; /* Descriptor command status */
  184. u32 next_desc_ptr; /* Next descriptor pointer */
  185. u32 buf_ptr; /* Descriptor buffer pointer */
  186. };
  187. struct tx_desc {
  188. u16 byte_cnt; /* buffer byte count */
  189. u16 l4i_chk; /* CPU provided TCP checksum */
  190. u32 cmd_sts; /* Command/status field */
  191. u32 next_desc_ptr; /* Pointer to next descriptor */
  192. u32 buf_ptr; /* pointer to buffer for this descriptor*/
  193. };
  194. #elif defined(__LITTLE_ENDIAN)
  195. struct rx_desc {
  196. u32 cmd_sts; /* Descriptor command status */
  197. u16 buf_size; /* Buffer size */
  198. u16 byte_cnt; /* Descriptor buffer byte count */
  199. u32 buf_ptr; /* Descriptor buffer pointer */
  200. u32 next_desc_ptr; /* Next descriptor pointer */
  201. };
  202. struct tx_desc {
  203. u32 cmd_sts; /* Command/status field */
  204. u16 l4i_chk; /* CPU provided TCP checksum */
  205. u16 byte_cnt; /* buffer byte count */
  206. u32 buf_ptr; /* pointer to buffer for this descriptor*/
  207. u32 next_desc_ptr; /* Pointer to next descriptor */
  208. };
  209. #else
  210. #error One of __BIG_ENDIAN or __LITTLE_ENDIAN must be defined
  211. #endif
  212. /* RX & TX descriptor command */
  213. #define BUFFER_OWNED_BY_DMA 0x80000000
  214. /* RX & TX descriptor status */
  215. #define ERROR_SUMMARY 0x00000001
  216. /* RX descriptor status */
  217. #define LAYER_4_CHECKSUM_OK 0x40000000
  218. #define RX_ENABLE_INTERRUPT 0x20000000
  219. #define RX_FIRST_DESC 0x08000000
  220. #define RX_LAST_DESC 0x04000000
  221. #define RX_IP_HDR_OK 0x02000000
  222. #define RX_PKT_IS_IPV4 0x01000000
  223. #define RX_PKT_IS_ETHERNETV2 0x00800000
  224. #define RX_PKT_LAYER4_TYPE_MASK 0x00600000
  225. #define RX_PKT_LAYER4_TYPE_TCP_IPV4 0x00000000
  226. #define RX_PKT_IS_VLAN_TAGGED 0x00080000
  227. /* TX descriptor command */
  228. #define TX_ENABLE_INTERRUPT 0x00800000
  229. #define GEN_CRC 0x00400000
  230. #define TX_FIRST_DESC 0x00200000
  231. #define TX_LAST_DESC 0x00100000
  232. #define ZERO_PADDING 0x00080000
  233. #define GEN_IP_V4_CHECKSUM 0x00040000
  234. #define GEN_TCP_UDP_CHECKSUM 0x00020000
  235. #define UDP_FRAME 0x00010000
  236. #define MAC_HDR_EXTRA_4_BYTES 0x00008000
  237. #define MAC_HDR_EXTRA_8_BYTES 0x00000200
  238. #define TX_IHL_SHIFT 11
  239. /* global *******************************************************************/
  240. struct mv643xx_eth_shared_private {
  241. /*
  242. * Ethernet controller base address.
  243. */
  244. void __iomem *base;
  245. /*
  246. * Points at the right SMI instance to use.
  247. */
  248. struct mv643xx_eth_shared_private *smi;
  249. /*
  250. * Provides access to local SMI interface.
  251. */
  252. struct mii_bus *smi_bus;
  253. /*
  254. * If we have access to the error interrupt pin (which is
  255. * somewhat misnamed as it not only reflects internal errors
  256. * but also reflects SMI completion), use that to wait for
  257. * SMI access completion instead of polling the SMI busy bit.
  258. */
  259. int err_interrupt;
  260. wait_queue_head_t smi_busy_wait;
  261. /*
  262. * Per-port MBUS window access register value.
  263. */
  264. u32 win_protect;
  265. /*
  266. * Hardware-specific parameters.
  267. */
  268. int extended_rx_coal_limit;
  269. int tx_bw_control;
  270. int tx_csum_limit;
  271. };
  272. #define TX_BW_CONTROL_ABSENT 0
  273. #define TX_BW_CONTROL_OLD_LAYOUT 1
  274. #define TX_BW_CONTROL_NEW_LAYOUT 2
  275. static int mv643xx_eth_open(struct net_device *dev);
  276. static int mv643xx_eth_stop(struct net_device *dev);
  277. /* per-port *****************************************************************/
  278. struct 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. /* Non MIB hardware counters */
  310. u32 rx_discard;
  311. u32 rx_overrun;
  312. };
  313. struct lro_counters {
  314. u32 lro_aggregated;
  315. u32 lro_flushed;
  316. u32 lro_no_desc;
  317. };
  318. struct rx_queue {
  319. int index;
  320. int rx_ring_size;
  321. int rx_desc_count;
  322. int rx_curr_desc;
  323. int rx_used_desc;
  324. struct rx_desc *rx_desc_area;
  325. dma_addr_t rx_desc_dma;
  326. int rx_desc_area_size;
  327. struct sk_buff **rx_skb;
  328. struct net_lro_mgr lro_mgr;
  329. struct net_lro_desc lro_arr[8];
  330. };
  331. struct tx_queue {
  332. int index;
  333. int tx_ring_size;
  334. int tx_desc_count;
  335. int tx_curr_desc;
  336. int tx_used_desc;
  337. struct tx_desc *tx_desc_area;
  338. dma_addr_t tx_desc_dma;
  339. int tx_desc_area_size;
  340. struct sk_buff_head tx_skb;
  341. unsigned long tx_packets;
  342. unsigned long tx_bytes;
  343. unsigned long tx_dropped;
  344. };
  345. struct mv643xx_eth_private {
  346. struct mv643xx_eth_shared_private *shared;
  347. void __iomem *base;
  348. int port_num;
  349. struct net_device *dev;
  350. struct phy_device *phy;
  351. struct timer_list mib_counters_timer;
  352. spinlock_t mib_counters_lock;
  353. struct mib_counters mib_counters;
  354. struct lro_counters lro_counters;
  355. struct work_struct tx_timeout_task;
  356. struct napi_struct napi;
  357. u32 int_mask;
  358. u8 oom;
  359. u8 work_link;
  360. u8 work_tx;
  361. u8 work_tx_end;
  362. u8 work_rx;
  363. u8 work_rx_refill;
  364. int skb_size;
  365. /*
  366. * RX state.
  367. */
  368. int rx_ring_size;
  369. unsigned long rx_desc_sram_addr;
  370. int rx_desc_sram_size;
  371. int rxq_count;
  372. struct timer_list rx_oom;
  373. struct rx_queue rxq[8];
  374. /*
  375. * TX state.
  376. */
  377. int tx_ring_size;
  378. unsigned long tx_desc_sram_addr;
  379. int tx_desc_sram_size;
  380. int txq_count;
  381. struct tx_queue txq[8];
  382. /*
  383. * Hardware-specific parameters.
  384. */
  385. #if defined(CONFIG_HAVE_CLK)
  386. struct clk *clk;
  387. #endif
  388. unsigned int t_clk;
  389. };
  390. /* port register accessors **************************************************/
  391. static inline u32 rdl(struct mv643xx_eth_private *mp, int offset)
  392. {
  393. return readl(mp->shared->base + offset);
  394. }
  395. static inline u32 rdlp(struct mv643xx_eth_private *mp, int offset)
  396. {
  397. return readl(mp->base + offset);
  398. }
  399. static inline void wrl(struct mv643xx_eth_private *mp, int offset, u32 data)
  400. {
  401. writel(data, mp->shared->base + offset);
  402. }
  403. static inline void wrlp(struct mv643xx_eth_private *mp, int offset, u32 data)
  404. {
  405. writel(data, mp->base + offset);
  406. }
  407. /* rxq/txq helper functions *************************************************/
  408. static struct mv643xx_eth_private *rxq_to_mp(struct rx_queue *rxq)
  409. {
  410. return container_of(rxq, struct mv643xx_eth_private, rxq[rxq->index]);
  411. }
  412. static struct mv643xx_eth_private *txq_to_mp(struct tx_queue *txq)
  413. {
  414. return container_of(txq, struct mv643xx_eth_private, txq[txq->index]);
  415. }
  416. static void rxq_enable(struct rx_queue *rxq)
  417. {
  418. struct mv643xx_eth_private *mp = rxq_to_mp(rxq);
  419. wrlp(mp, RXQ_COMMAND, 1 << rxq->index);
  420. }
  421. static void rxq_disable(struct rx_queue *rxq)
  422. {
  423. struct mv643xx_eth_private *mp = rxq_to_mp(rxq);
  424. u8 mask = 1 << rxq->index;
  425. wrlp(mp, RXQ_COMMAND, mask << 8);
  426. while (rdlp(mp, RXQ_COMMAND) & mask)
  427. udelay(10);
  428. }
  429. static void txq_reset_hw_ptr(struct tx_queue *txq)
  430. {
  431. struct mv643xx_eth_private *mp = txq_to_mp(txq);
  432. u32 addr;
  433. addr = (u32)txq->tx_desc_dma;
  434. addr += txq->tx_curr_desc * sizeof(struct tx_desc);
  435. wrlp(mp, TXQ_CURRENT_DESC_PTR(txq->index), addr);
  436. }
  437. static void txq_enable(struct tx_queue *txq)
  438. {
  439. struct mv643xx_eth_private *mp = txq_to_mp(txq);
  440. wrlp(mp, TXQ_COMMAND, 1 << txq->index);
  441. }
  442. static void txq_disable(struct tx_queue *txq)
  443. {
  444. struct mv643xx_eth_private *mp = txq_to_mp(txq);
  445. u8 mask = 1 << txq->index;
  446. wrlp(mp, TXQ_COMMAND, mask << 8);
  447. while (rdlp(mp, TXQ_COMMAND) & mask)
  448. udelay(10);
  449. }
  450. static void txq_maybe_wake(struct tx_queue *txq)
  451. {
  452. struct mv643xx_eth_private *mp = txq_to_mp(txq);
  453. struct netdev_queue *nq = netdev_get_tx_queue(mp->dev, txq->index);
  454. if (netif_tx_queue_stopped(nq)) {
  455. __netif_tx_lock(nq, smp_processor_id());
  456. if (txq->tx_ring_size - txq->tx_desc_count >= MAX_SKB_FRAGS + 1)
  457. netif_tx_wake_queue(nq);
  458. __netif_tx_unlock(nq);
  459. }
  460. }
  461. /* rx napi ******************************************************************/
  462. static int
  463. mv643xx_get_skb_header(struct sk_buff *skb, void **iphdr, void **tcph,
  464. u64 *hdr_flags, void *priv)
  465. {
  466. unsigned long cmd_sts = (unsigned long)priv;
  467. /*
  468. * Make sure that this packet is Ethernet II, is not VLAN
  469. * tagged, is IPv4, has a valid IP header, and is TCP.
  470. */
  471. if ((cmd_sts & (RX_IP_HDR_OK | RX_PKT_IS_IPV4 |
  472. RX_PKT_IS_ETHERNETV2 | RX_PKT_LAYER4_TYPE_MASK |
  473. RX_PKT_IS_VLAN_TAGGED)) !=
  474. (RX_IP_HDR_OK | RX_PKT_IS_IPV4 |
  475. RX_PKT_IS_ETHERNETV2 | RX_PKT_LAYER4_TYPE_TCP_IPV4))
  476. return -1;
  477. skb_reset_network_header(skb);
  478. skb_set_transport_header(skb, ip_hdrlen(skb));
  479. *iphdr = ip_hdr(skb);
  480. *tcph = tcp_hdr(skb);
  481. *hdr_flags = LRO_IPV4 | LRO_TCP;
  482. return 0;
  483. }
  484. static int rxq_process(struct rx_queue *rxq, int budget)
  485. {
  486. struct mv643xx_eth_private *mp = rxq_to_mp(rxq);
  487. struct net_device_stats *stats = &mp->dev->stats;
  488. int lro_flush_needed;
  489. int rx;
  490. lro_flush_needed = 0;
  491. rx = 0;
  492. while (rx < budget && rxq->rx_desc_count) {
  493. struct rx_desc *rx_desc;
  494. unsigned int cmd_sts;
  495. struct sk_buff *skb;
  496. u16 byte_cnt;
  497. rx_desc = &rxq->rx_desc_area[rxq->rx_curr_desc];
  498. cmd_sts = rx_desc->cmd_sts;
  499. if (cmd_sts & BUFFER_OWNED_BY_DMA)
  500. break;
  501. rmb();
  502. skb = rxq->rx_skb[rxq->rx_curr_desc];
  503. rxq->rx_skb[rxq->rx_curr_desc] = NULL;
  504. rxq->rx_curr_desc++;
  505. if (rxq->rx_curr_desc == rxq->rx_ring_size)
  506. rxq->rx_curr_desc = 0;
  507. dma_unmap_single(mp->dev->dev.parent, rx_desc->buf_ptr,
  508. rx_desc->buf_size, DMA_FROM_DEVICE);
  509. rxq->rx_desc_count--;
  510. rx++;
  511. mp->work_rx_refill |= 1 << rxq->index;
  512. byte_cnt = rx_desc->byte_cnt;
  513. /*
  514. * Update statistics.
  515. *
  516. * Note that the descriptor byte count includes 2 dummy
  517. * bytes automatically inserted by the hardware at the
  518. * start of the packet (which we don't count), and a 4
  519. * byte CRC at the end of the packet (which we do count).
  520. */
  521. stats->rx_packets++;
  522. stats->rx_bytes += byte_cnt - 2;
  523. /*
  524. * In case we received a packet without first / last bits
  525. * on, or the error summary bit is set, the packet needs
  526. * to be dropped.
  527. */
  528. if ((cmd_sts & (RX_FIRST_DESC | RX_LAST_DESC | ERROR_SUMMARY))
  529. != (RX_FIRST_DESC | RX_LAST_DESC))
  530. goto err;
  531. /*
  532. * The -4 is for the CRC in the trailer of the
  533. * received packet
  534. */
  535. skb_put(skb, byte_cnt - 2 - 4);
  536. if (cmd_sts & LAYER_4_CHECKSUM_OK)
  537. skb->ip_summed = CHECKSUM_UNNECESSARY;
  538. skb->protocol = eth_type_trans(skb, mp->dev);
  539. if (skb->dev->features & NETIF_F_LRO &&
  540. skb->ip_summed == CHECKSUM_UNNECESSARY) {
  541. lro_receive_skb(&rxq->lro_mgr, skb, (void *)cmd_sts);
  542. lro_flush_needed = 1;
  543. } else
  544. netif_receive_skb(skb);
  545. continue;
  546. err:
  547. stats->rx_dropped++;
  548. if ((cmd_sts & (RX_FIRST_DESC | RX_LAST_DESC)) !=
  549. (RX_FIRST_DESC | RX_LAST_DESC)) {
  550. if (net_ratelimit())
  551. netdev_err(mp->dev,
  552. "received packet spanning multiple descriptors\n");
  553. }
  554. if (cmd_sts & ERROR_SUMMARY)
  555. stats->rx_errors++;
  556. dev_kfree_skb(skb);
  557. }
  558. if (lro_flush_needed)
  559. lro_flush_all(&rxq->lro_mgr);
  560. if (rx < budget)
  561. mp->work_rx &= ~(1 << rxq->index);
  562. return rx;
  563. }
  564. static int rxq_refill(struct rx_queue *rxq, int budget)
  565. {
  566. struct mv643xx_eth_private *mp = rxq_to_mp(rxq);
  567. int refilled;
  568. refilled = 0;
  569. while (refilled < budget && rxq->rx_desc_count < rxq->rx_ring_size) {
  570. struct sk_buff *skb;
  571. int rx;
  572. struct rx_desc *rx_desc;
  573. int size;
  574. skb = netdev_alloc_skb(mp->dev, mp->skb_size);
  575. if (skb == NULL) {
  576. mp->oom = 1;
  577. goto oom;
  578. }
  579. if (SKB_DMA_REALIGN)
  580. skb_reserve(skb, SKB_DMA_REALIGN);
  581. refilled++;
  582. rxq->rx_desc_count++;
  583. rx = rxq->rx_used_desc++;
  584. if (rxq->rx_used_desc == rxq->rx_ring_size)
  585. rxq->rx_used_desc = 0;
  586. rx_desc = rxq->rx_desc_area + rx;
  587. size = skb->end - skb->data;
  588. rx_desc->buf_ptr = dma_map_single(mp->dev->dev.parent,
  589. skb->data, size,
  590. DMA_FROM_DEVICE);
  591. rx_desc->buf_size = size;
  592. rxq->rx_skb[rx] = skb;
  593. wmb();
  594. rx_desc->cmd_sts = BUFFER_OWNED_BY_DMA | RX_ENABLE_INTERRUPT;
  595. wmb();
  596. /*
  597. * The hardware automatically prepends 2 bytes of
  598. * dummy data to each received packet, so that the
  599. * IP header ends up 16-byte aligned.
  600. */
  601. skb_reserve(skb, 2);
  602. }
  603. if (refilled < budget)
  604. mp->work_rx_refill &= ~(1 << rxq->index);
  605. oom:
  606. return refilled;
  607. }
  608. /* tx ***********************************************************************/
  609. static inline unsigned int has_tiny_unaligned_frags(struct sk_buff *skb)
  610. {
  611. int frag;
  612. for (frag = 0; frag < skb_shinfo(skb)->nr_frags; frag++) {
  613. const skb_frag_t *fragp = &skb_shinfo(skb)->frags[frag];
  614. if (skb_frag_size(fragp) <= 8 && fragp->page_offset & 7)
  615. return 1;
  616. }
  617. return 0;
  618. }
  619. static void txq_submit_frag_skb(struct tx_queue *txq, struct sk_buff *skb)
  620. {
  621. struct mv643xx_eth_private *mp = txq_to_mp(txq);
  622. int nr_frags = skb_shinfo(skb)->nr_frags;
  623. int frag;
  624. for (frag = 0; frag < nr_frags; frag++) {
  625. skb_frag_t *this_frag;
  626. int tx_index;
  627. struct tx_desc *desc;
  628. this_frag = &skb_shinfo(skb)->frags[frag];
  629. tx_index = txq->tx_curr_desc++;
  630. if (txq->tx_curr_desc == txq->tx_ring_size)
  631. txq->tx_curr_desc = 0;
  632. desc = &txq->tx_desc_area[tx_index];
  633. /*
  634. * The last fragment will generate an interrupt
  635. * which will free the skb on TX completion.
  636. */
  637. if (frag == nr_frags - 1) {
  638. desc->cmd_sts = BUFFER_OWNED_BY_DMA |
  639. ZERO_PADDING | TX_LAST_DESC |
  640. TX_ENABLE_INTERRUPT;
  641. } else {
  642. desc->cmd_sts = BUFFER_OWNED_BY_DMA;
  643. }
  644. desc->l4i_chk = 0;
  645. desc->byte_cnt = skb_frag_size(this_frag);
  646. desc->buf_ptr = skb_frag_dma_map(mp->dev->dev.parent,
  647. this_frag, 0,
  648. skb_frag_size(this_frag),
  649. DMA_TO_DEVICE);
  650. }
  651. }
  652. static inline __be16 sum16_as_be(__sum16 sum)
  653. {
  654. return (__force __be16)sum;
  655. }
  656. static int txq_submit_skb(struct tx_queue *txq, struct sk_buff *skb)
  657. {
  658. struct mv643xx_eth_private *mp = txq_to_mp(txq);
  659. int nr_frags = skb_shinfo(skb)->nr_frags;
  660. int tx_index;
  661. struct tx_desc *desc;
  662. u32 cmd_sts;
  663. u16 l4i_chk;
  664. int length;
  665. cmd_sts = TX_FIRST_DESC | GEN_CRC | BUFFER_OWNED_BY_DMA;
  666. l4i_chk = 0;
  667. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  668. int hdr_len;
  669. int tag_bytes;
  670. BUG_ON(skb->protocol != htons(ETH_P_IP) &&
  671. skb->protocol != htons(ETH_P_8021Q));
  672. hdr_len = (void *)ip_hdr(skb) - (void *)skb->data;
  673. tag_bytes = hdr_len - ETH_HLEN;
  674. if (skb->len - hdr_len > mp->shared->tx_csum_limit ||
  675. unlikely(tag_bytes & ~12)) {
  676. if (skb_checksum_help(skb) == 0)
  677. goto no_csum;
  678. kfree_skb(skb);
  679. return 1;
  680. }
  681. if (tag_bytes & 4)
  682. cmd_sts |= MAC_HDR_EXTRA_4_BYTES;
  683. if (tag_bytes & 8)
  684. cmd_sts |= MAC_HDR_EXTRA_8_BYTES;
  685. cmd_sts |= GEN_TCP_UDP_CHECKSUM |
  686. GEN_IP_V4_CHECKSUM |
  687. ip_hdr(skb)->ihl << TX_IHL_SHIFT;
  688. switch (ip_hdr(skb)->protocol) {
  689. case IPPROTO_UDP:
  690. cmd_sts |= UDP_FRAME;
  691. l4i_chk = ntohs(sum16_as_be(udp_hdr(skb)->check));
  692. break;
  693. case IPPROTO_TCP:
  694. l4i_chk = ntohs(sum16_as_be(tcp_hdr(skb)->check));
  695. break;
  696. default:
  697. BUG();
  698. }
  699. } else {
  700. no_csum:
  701. /* Errata BTS #50, IHL must be 5 if no HW checksum */
  702. cmd_sts |= 5 << TX_IHL_SHIFT;
  703. }
  704. tx_index = txq->tx_curr_desc++;
  705. if (txq->tx_curr_desc == txq->tx_ring_size)
  706. txq->tx_curr_desc = 0;
  707. desc = &txq->tx_desc_area[tx_index];
  708. if (nr_frags) {
  709. txq_submit_frag_skb(txq, skb);
  710. length = skb_headlen(skb);
  711. } else {
  712. cmd_sts |= ZERO_PADDING | TX_LAST_DESC | TX_ENABLE_INTERRUPT;
  713. length = skb->len;
  714. }
  715. desc->l4i_chk = l4i_chk;
  716. desc->byte_cnt = length;
  717. desc->buf_ptr = dma_map_single(mp->dev->dev.parent, skb->data,
  718. length, DMA_TO_DEVICE);
  719. __skb_queue_tail(&txq->tx_skb, skb);
  720. skb_tx_timestamp(skb);
  721. /* ensure all other descriptors are written before first cmd_sts */
  722. wmb();
  723. desc->cmd_sts = cmd_sts;
  724. /* clear TX_END status */
  725. mp->work_tx_end &= ~(1 << txq->index);
  726. /* ensure all descriptors are written before poking hardware */
  727. wmb();
  728. txq_enable(txq);
  729. txq->tx_desc_count += nr_frags + 1;
  730. return 0;
  731. }
  732. static netdev_tx_t mv643xx_eth_xmit(struct sk_buff *skb, struct net_device *dev)
  733. {
  734. struct mv643xx_eth_private *mp = netdev_priv(dev);
  735. int length, queue;
  736. struct tx_queue *txq;
  737. struct netdev_queue *nq;
  738. queue = skb_get_queue_mapping(skb);
  739. txq = mp->txq + queue;
  740. nq = netdev_get_tx_queue(dev, queue);
  741. if (has_tiny_unaligned_frags(skb) && __skb_linearize(skb)) {
  742. txq->tx_dropped++;
  743. netdev_printk(KERN_DEBUG, dev,
  744. "failed to linearize skb with tiny unaligned fragment\n");
  745. return NETDEV_TX_BUSY;
  746. }
  747. if (txq->tx_ring_size - txq->tx_desc_count < MAX_SKB_FRAGS + 1) {
  748. if (net_ratelimit())
  749. netdev_err(dev, "tx queue full?!\n");
  750. kfree_skb(skb);
  751. return NETDEV_TX_OK;
  752. }
  753. length = skb->len;
  754. if (!txq_submit_skb(txq, skb)) {
  755. int entries_left;
  756. txq->tx_bytes += length;
  757. txq->tx_packets++;
  758. entries_left = txq->tx_ring_size - txq->tx_desc_count;
  759. if (entries_left < MAX_SKB_FRAGS + 1)
  760. netif_tx_stop_queue(nq);
  761. }
  762. return NETDEV_TX_OK;
  763. }
  764. /* tx napi ******************************************************************/
  765. static void txq_kick(struct tx_queue *txq)
  766. {
  767. struct mv643xx_eth_private *mp = txq_to_mp(txq);
  768. struct netdev_queue *nq = netdev_get_tx_queue(mp->dev, txq->index);
  769. u32 hw_desc_ptr;
  770. u32 expected_ptr;
  771. __netif_tx_lock(nq, smp_processor_id());
  772. if (rdlp(mp, TXQ_COMMAND) & (1 << txq->index))
  773. goto out;
  774. hw_desc_ptr = rdlp(mp, TXQ_CURRENT_DESC_PTR(txq->index));
  775. expected_ptr = (u32)txq->tx_desc_dma +
  776. txq->tx_curr_desc * sizeof(struct tx_desc);
  777. if (hw_desc_ptr != expected_ptr)
  778. txq_enable(txq);
  779. out:
  780. __netif_tx_unlock(nq);
  781. mp->work_tx_end &= ~(1 << txq->index);
  782. }
  783. static int txq_reclaim(struct tx_queue *txq, int budget, int force)
  784. {
  785. struct mv643xx_eth_private *mp = txq_to_mp(txq);
  786. struct netdev_queue *nq = netdev_get_tx_queue(mp->dev, txq->index);
  787. int reclaimed;
  788. __netif_tx_lock(nq, smp_processor_id());
  789. reclaimed = 0;
  790. while (reclaimed < budget && txq->tx_desc_count > 0) {
  791. int tx_index;
  792. struct tx_desc *desc;
  793. u32 cmd_sts;
  794. struct sk_buff *skb;
  795. tx_index = txq->tx_used_desc;
  796. desc = &txq->tx_desc_area[tx_index];
  797. cmd_sts = desc->cmd_sts;
  798. if (cmd_sts & BUFFER_OWNED_BY_DMA) {
  799. if (!force)
  800. break;
  801. desc->cmd_sts = cmd_sts & ~BUFFER_OWNED_BY_DMA;
  802. }
  803. txq->tx_used_desc = tx_index + 1;
  804. if (txq->tx_used_desc == txq->tx_ring_size)
  805. txq->tx_used_desc = 0;
  806. reclaimed++;
  807. txq->tx_desc_count--;
  808. skb = NULL;
  809. if (cmd_sts & TX_LAST_DESC)
  810. skb = __skb_dequeue(&txq->tx_skb);
  811. if (cmd_sts & ERROR_SUMMARY) {
  812. netdev_info(mp->dev, "tx error\n");
  813. mp->dev->stats.tx_errors++;
  814. }
  815. if (cmd_sts & TX_FIRST_DESC) {
  816. dma_unmap_single(mp->dev->dev.parent, desc->buf_ptr,
  817. desc->byte_cnt, DMA_TO_DEVICE);
  818. } else {
  819. dma_unmap_page(mp->dev->dev.parent, desc->buf_ptr,
  820. desc->byte_cnt, DMA_TO_DEVICE);
  821. }
  822. dev_kfree_skb(skb);
  823. }
  824. __netif_tx_unlock(nq);
  825. if (reclaimed < budget)
  826. mp->work_tx &= ~(1 << txq->index);
  827. return reclaimed;
  828. }
  829. /* tx rate control **********************************************************/
  830. /*
  831. * Set total maximum TX rate (shared by all TX queues for this port)
  832. * to 'rate' bits per second, with a maximum burst of 'burst' bytes.
  833. */
  834. static void tx_set_rate(struct mv643xx_eth_private *mp, int rate, int burst)
  835. {
  836. int token_rate;
  837. int mtu;
  838. int bucket_size;
  839. token_rate = ((rate / 1000) * 64) / (mp->t_clk / 1000);
  840. if (token_rate > 1023)
  841. token_rate = 1023;
  842. mtu = (mp->dev->mtu + 255) >> 8;
  843. if (mtu > 63)
  844. mtu = 63;
  845. bucket_size = (burst + 255) >> 8;
  846. if (bucket_size > 65535)
  847. bucket_size = 65535;
  848. switch (mp->shared->tx_bw_control) {
  849. case TX_BW_CONTROL_OLD_LAYOUT:
  850. wrlp(mp, TX_BW_RATE, token_rate);
  851. wrlp(mp, TX_BW_MTU, mtu);
  852. wrlp(mp, TX_BW_BURST, bucket_size);
  853. break;
  854. case TX_BW_CONTROL_NEW_LAYOUT:
  855. wrlp(mp, TX_BW_RATE_MOVED, token_rate);
  856. wrlp(mp, TX_BW_MTU_MOVED, mtu);
  857. wrlp(mp, TX_BW_BURST_MOVED, bucket_size);
  858. break;
  859. }
  860. }
  861. static void txq_set_rate(struct tx_queue *txq, int rate, int burst)
  862. {
  863. struct mv643xx_eth_private *mp = txq_to_mp(txq);
  864. int token_rate;
  865. int bucket_size;
  866. token_rate = ((rate / 1000) * 64) / (mp->t_clk / 1000);
  867. if (token_rate > 1023)
  868. token_rate = 1023;
  869. bucket_size = (burst + 255) >> 8;
  870. if (bucket_size > 65535)
  871. bucket_size = 65535;
  872. wrlp(mp, TXQ_BW_TOKENS(txq->index), token_rate << 14);
  873. wrlp(mp, TXQ_BW_CONF(txq->index), (bucket_size << 10) | token_rate);
  874. }
  875. static void txq_set_fixed_prio_mode(struct tx_queue *txq)
  876. {
  877. struct mv643xx_eth_private *mp = txq_to_mp(txq);
  878. int off;
  879. u32 val;
  880. /*
  881. * Turn on fixed priority mode.
  882. */
  883. off = 0;
  884. switch (mp->shared->tx_bw_control) {
  885. case TX_BW_CONTROL_OLD_LAYOUT:
  886. off = TXQ_FIX_PRIO_CONF;
  887. break;
  888. case TX_BW_CONTROL_NEW_LAYOUT:
  889. off = TXQ_FIX_PRIO_CONF_MOVED;
  890. break;
  891. }
  892. if (off) {
  893. val = rdlp(mp, off);
  894. val |= 1 << txq->index;
  895. wrlp(mp, off, val);
  896. }
  897. }
  898. /* mii management interface *************************************************/
  899. static void mv643xx_adjust_pscr(struct mv643xx_eth_private *mp)
  900. {
  901. u32 pscr = rdlp(mp, PORT_SERIAL_CONTROL);
  902. u32 autoneg_disable = FORCE_LINK_PASS |
  903. DISABLE_AUTO_NEG_SPEED_GMII |
  904. DISABLE_AUTO_NEG_FOR_FLOW_CTRL |
  905. DISABLE_AUTO_NEG_FOR_DUPLEX;
  906. if (mp->phy->autoneg == AUTONEG_ENABLE) {
  907. /* enable auto negotiation */
  908. pscr &= ~autoneg_disable;
  909. goto out_write;
  910. }
  911. pscr |= autoneg_disable;
  912. if (mp->phy->speed == SPEED_1000) {
  913. /* force gigabit, half duplex not supported */
  914. pscr |= SET_GMII_SPEED_TO_1000;
  915. pscr |= SET_FULL_DUPLEX_MODE;
  916. goto out_write;
  917. }
  918. pscr &= ~SET_GMII_SPEED_TO_1000;
  919. if (mp->phy->speed == SPEED_100)
  920. pscr |= SET_MII_SPEED_TO_100;
  921. else
  922. pscr &= ~SET_MII_SPEED_TO_100;
  923. if (mp->phy->duplex == DUPLEX_FULL)
  924. pscr |= SET_FULL_DUPLEX_MODE;
  925. else
  926. pscr &= ~SET_FULL_DUPLEX_MODE;
  927. out_write:
  928. wrlp(mp, PORT_SERIAL_CONTROL, pscr);
  929. }
  930. static irqreturn_t mv643xx_eth_err_irq(int irq, void *dev_id)
  931. {
  932. struct mv643xx_eth_shared_private *msp = dev_id;
  933. if (readl(msp->base + ERR_INT_CAUSE) & ERR_INT_SMI_DONE) {
  934. writel(~ERR_INT_SMI_DONE, msp->base + ERR_INT_CAUSE);
  935. wake_up(&msp->smi_busy_wait);
  936. return IRQ_HANDLED;
  937. }
  938. return IRQ_NONE;
  939. }
  940. static int smi_is_done(struct mv643xx_eth_shared_private *msp)
  941. {
  942. return !(readl(msp->base + SMI_REG) & SMI_BUSY);
  943. }
  944. static int smi_wait_ready(struct mv643xx_eth_shared_private *msp)
  945. {
  946. if (msp->err_interrupt == NO_IRQ) {
  947. int i;
  948. for (i = 0; !smi_is_done(msp); i++) {
  949. if (i == 10)
  950. return -ETIMEDOUT;
  951. msleep(10);
  952. }
  953. return 0;
  954. }
  955. if (!smi_is_done(msp)) {
  956. wait_event_timeout(msp->smi_busy_wait, smi_is_done(msp),
  957. msecs_to_jiffies(100));
  958. if (!smi_is_done(msp))
  959. return -ETIMEDOUT;
  960. }
  961. return 0;
  962. }
  963. static int smi_bus_read(struct mii_bus *bus, int addr, int reg)
  964. {
  965. struct mv643xx_eth_shared_private *msp = bus->priv;
  966. void __iomem *smi_reg = msp->base + SMI_REG;
  967. int ret;
  968. if (smi_wait_ready(msp)) {
  969. pr_warn("SMI bus busy timeout\n");
  970. return -ETIMEDOUT;
  971. }
  972. writel(SMI_OPCODE_READ | (reg << 21) | (addr << 16), smi_reg);
  973. if (smi_wait_ready(msp)) {
  974. pr_warn("SMI bus busy timeout\n");
  975. return -ETIMEDOUT;
  976. }
  977. ret = readl(smi_reg);
  978. if (!(ret & SMI_READ_VALID)) {
  979. pr_warn("SMI bus read not valid\n");
  980. return -ENODEV;
  981. }
  982. return ret & 0xffff;
  983. }
  984. static int smi_bus_write(struct mii_bus *bus, int addr, int reg, u16 val)
  985. {
  986. struct mv643xx_eth_shared_private *msp = bus->priv;
  987. void __iomem *smi_reg = msp->base + SMI_REG;
  988. if (smi_wait_ready(msp)) {
  989. pr_warn("SMI bus busy timeout\n");
  990. return -ETIMEDOUT;
  991. }
  992. writel(SMI_OPCODE_WRITE | (reg << 21) |
  993. (addr << 16) | (val & 0xffff), smi_reg);
  994. if (smi_wait_ready(msp)) {
  995. pr_warn("SMI bus busy timeout\n");
  996. return -ETIMEDOUT;
  997. }
  998. return 0;
  999. }
  1000. /* statistics ***************************************************************/
  1001. static struct net_device_stats *mv643xx_eth_get_stats(struct net_device *dev)
  1002. {
  1003. struct mv643xx_eth_private *mp = netdev_priv(dev);
  1004. struct net_device_stats *stats = &dev->stats;
  1005. unsigned long tx_packets = 0;
  1006. unsigned long tx_bytes = 0;
  1007. unsigned long tx_dropped = 0;
  1008. int i;
  1009. for (i = 0; i < mp->txq_count; i++) {
  1010. struct tx_queue *txq = mp->txq + i;
  1011. tx_packets += txq->tx_packets;
  1012. tx_bytes += txq->tx_bytes;
  1013. tx_dropped += txq->tx_dropped;
  1014. }
  1015. stats->tx_packets = tx_packets;
  1016. stats->tx_bytes = tx_bytes;
  1017. stats->tx_dropped = tx_dropped;
  1018. return stats;
  1019. }
  1020. static void mv643xx_eth_grab_lro_stats(struct mv643xx_eth_private *mp)
  1021. {
  1022. u32 lro_aggregated = 0;
  1023. u32 lro_flushed = 0;
  1024. u32 lro_no_desc = 0;
  1025. int i;
  1026. for (i = 0; i < mp->rxq_count; i++) {
  1027. struct rx_queue *rxq = mp->rxq + i;
  1028. lro_aggregated += rxq->lro_mgr.stats.aggregated;
  1029. lro_flushed += rxq->lro_mgr.stats.flushed;
  1030. lro_no_desc += rxq->lro_mgr.stats.no_desc;
  1031. }
  1032. mp->lro_counters.lro_aggregated = lro_aggregated;
  1033. mp->lro_counters.lro_flushed = lro_flushed;
  1034. mp->lro_counters.lro_no_desc = lro_no_desc;
  1035. }
  1036. static inline u32 mib_read(struct mv643xx_eth_private *mp, int offset)
  1037. {
  1038. return rdl(mp, MIB_COUNTERS(mp->port_num) + offset);
  1039. }
  1040. static void mib_counters_clear(struct mv643xx_eth_private *mp)
  1041. {
  1042. int i;
  1043. for (i = 0; i < 0x80; i += 4)
  1044. mib_read(mp, i);
  1045. /* Clear non MIB hw counters also */
  1046. rdlp(mp, RX_DISCARD_FRAME_CNT);
  1047. rdlp(mp, RX_OVERRUN_FRAME_CNT);
  1048. }
  1049. static void mib_counters_update(struct mv643xx_eth_private *mp)
  1050. {
  1051. struct mib_counters *p = &mp->mib_counters;
  1052. spin_lock_bh(&mp->mib_counters_lock);
  1053. p->good_octets_received += mib_read(mp, 0x00);
  1054. p->bad_octets_received += mib_read(mp, 0x08);
  1055. p->internal_mac_transmit_err += mib_read(mp, 0x0c);
  1056. p->good_frames_received += mib_read(mp, 0x10);
  1057. p->bad_frames_received += mib_read(mp, 0x14);
  1058. p->broadcast_frames_received += mib_read(mp, 0x18);
  1059. p->multicast_frames_received += mib_read(mp, 0x1c);
  1060. p->frames_64_octets += mib_read(mp, 0x20);
  1061. p->frames_65_to_127_octets += mib_read(mp, 0x24);
  1062. p->frames_128_to_255_octets += mib_read(mp, 0x28);
  1063. p->frames_256_to_511_octets += mib_read(mp, 0x2c);
  1064. p->frames_512_to_1023_octets += mib_read(mp, 0x30);
  1065. p->frames_1024_to_max_octets += mib_read(mp, 0x34);
  1066. p->good_octets_sent += mib_read(mp, 0x38);
  1067. p->good_frames_sent += mib_read(mp, 0x40);
  1068. p->excessive_collision += mib_read(mp, 0x44);
  1069. p->multicast_frames_sent += mib_read(mp, 0x48);
  1070. p->broadcast_frames_sent += mib_read(mp, 0x4c);
  1071. p->unrec_mac_control_received += mib_read(mp, 0x50);
  1072. p->fc_sent += mib_read(mp, 0x54);
  1073. p->good_fc_received += mib_read(mp, 0x58);
  1074. p->bad_fc_received += mib_read(mp, 0x5c);
  1075. p->undersize_received += mib_read(mp, 0x60);
  1076. p->fragments_received += mib_read(mp, 0x64);
  1077. p->oversize_received += mib_read(mp, 0x68);
  1078. p->jabber_received += mib_read(mp, 0x6c);
  1079. p->mac_receive_error += mib_read(mp, 0x70);
  1080. p->bad_crc_event += mib_read(mp, 0x74);
  1081. p->collision += mib_read(mp, 0x78);
  1082. p->late_collision += mib_read(mp, 0x7c);
  1083. /* Non MIB hardware counters */
  1084. p->rx_discard += rdlp(mp, RX_DISCARD_FRAME_CNT);
  1085. p->rx_overrun += rdlp(mp, RX_OVERRUN_FRAME_CNT);
  1086. spin_unlock_bh(&mp->mib_counters_lock);
  1087. mod_timer(&mp->mib_counters_timer, jiffies + 30 * HZ);
  1088. }
  1089. static void mib_counters_timer_wrapper(unsigned long _mp)
  1090. {
  1091. struct mv643xx_eth_private *mp = (void *)_mp;
  1092. mib_counters_update(mp);
  1093. }
  1094. /* interrupt coalescing *****************************************************/
  1095. /*
  1096. * Hardware coalescing parameters are set in units of 64 t_clk
  1097. * cycles. I.e.:
  1098. *
  1099. * coal_delay_in_usec = 64000000 * register_value / t_clk_rate
  1100. *
  1101. * register_value = coal_delay_in_usec * t_clk_rate / 64000000
  1102. *
  1103. * In the ->set*() methods, we round the computed register value
  1104. * to the nearest integer.
  1105. */
  1106. static unsigned int get_rx_coal(struct mv643xx_eth_private *mp)
  1107. {
  1108. u32 val = rdlp(mp, SDMA_CONFIG);
  1109. u64 temp;
  1110. if (mp->shared->extended_rx_coal_limit)
  1111. temp = ((val & 0x02000000) >> 10) | ((val & 0x003fff80) >> 7);
  1112. else
  1113. temp = (val & 0x003fff00) >> 8;
  1114. temp *= 64000000;
  1115. do_div(temp, mp->t_clk);
  1116. return (unsigned int)temp;
  1117. }
  1118. static void set_rx_coal(struct mv643xx_eth_private *mp, unsigned int usec)
  1119. {
  1120. u64 temp;
  1121. u32 val;
  1122. temp = (u64)usec * mp->t_clk;
  1123. temp += 31999999;
  1124. do_div(temp, 64000000);
  1125. val = rdlp(mp, SDMA_CONFIG);
  1126. if (mp->shared->extended_rx_coal_limit) {
  1127. if (temp > 0xffff)
  1128. temp = 0xffff;
  1129. val &= ~0x023fff80;
  1130. val |= (temp & 0x8000) << 10;
  1131. val |= (temp & 0x7fff) << 7;
  1132. } else {
  1133. if (temp > 0x3fff)
  1134. temp = 0x3fff;
  1135. val &= ~0x003fff00;
  1136. val |= (temp & 0x3fff) << 8;
  1137. }
  1138. wrlp(mp, SDMA_CONFIG, val);
  1139. }
  1140. static unsigned int get_tx_coal(struct mv643xx_eth_private *mp)
  1141. {
  1142. u64 temp;
  1143. temp = (rdlp(mp, TX_FIFO_URGENT_THRESHOLD) & 0x3fff0) >> 4;
  1144. temp *= 64000000;
  1145. do_div(temp, mp->t_clk);
  1146. return (unsigned int)temp;
  1147. }
  1148. static void set_tx_coal(struct mv643xx_eth_private *mp, unsigned int usec)
  1149. {
  1150. u64 temp;
  1151. temp = (u64)usec * mp->t_clk;
  1152. temp += 31999999;
  1153. do_div(temp, 64000000);
  1154. if (temp > 0x3fff)
  1155. temp = 0x3fff;
  1156. wrlp(mp, TX_FIFO_URGENT_THRESHOLD, temp << 4);
  1157. }
  1158. /* ethtool ******************************************************************/
  1159. struct mv643xx_eth_stats {
  1160. char stat_string[ETH_GSTRING_LEN];
  1161. int sizeof_stat;
  1162. int netdev_off;
  1163. int mp_off;
  1164. };
  1165. #define SSTAT(m) \
  1166. { #m, FIELD_SIZEOF(struct net_device_stats, m), \
  1167. offsetof(struct net_device, stats.m), -1 }
  1168. #define MIBSTAT(m) \
  1169. { #m, FIELD_SIZEOF(struct mib_counters, m), \
  1170. -1, offsetof(struct mv643xx_eth_private, mib_counters.m) }
  1171. #define LROSTAT(m) \
  1172. { #m, FIELD_SIZEOF(struct lro_counters, m), \
  1173. -1, offsetof(struct mv643xx_eth_private, lro_counters.m) }
  1174. static const struct mv643xx_eth_stats mv643xx_eth_stats[] = {
  1175. SSTAT(rx_packets),
  1176. SSTAT(tx_packets),
  1177. SSTAT(rx_bytes),
  1178. SSTAT(tx_bytes),
  1179. SSTAT(rx_errors),
  1180. SSTAT(tx_errors),
  1181. SSTAT(rx_dropped),
  1182. SSTAT(tx_dropped),
  1183. MIBSTAT(good_octets_received),
  1184. MIBSTAT(bad_octets_received),
  1185. MIBSTAT(internal_mac_transmit_err),
  1186. MIBSTAT(good_frames_received),
  1187. MIBSTAT(bad_frames_received),
  1188. MIBSTAT(broadcast_frames_received),
  1189. MIBSTAT(multicast_frames_received),
  1190. MIBSTAT(frames_64_octets),
  1191. MIBSTAT(frames_65_to_127_octets),
  1192. MIBSTAT(frames_128_to_255_octets),
  1193. MIBSTAT(frames_256_to_511_octets),
  1194. MIBSTAT(frames_512_to_1023_octets),
  1195. MIBSTAT(frames_1024_to_max_octets),
  1196. MIBSTAT(good_octets_sent),
  1197. MIBSTAT(good_frames_sent),
  1198. MIBSTAT(excessive_collision),
  1199. MIBSTAT(multicast_frames_sent),
  1200. MIBSTAT(broadcast_frames_sent),
  1201. MIBSTAT(unrec_mac_control_received),
  1202. MIBSTAT(fc_sent),
  1203. MIBSTAT(good_fc_received),
  1204. MIBSTAT(bad_fc_received),
  1205. MIBSTAT(undersize_received),
  1206. MIBSTAT(fragments_received),
  1207. MIBSTAT(oversize_received),
  1208. MIBSTAT(jabber_received),
  1209. MIBSTAT(mac_receive_error),
  1210. MIBSTAT(bad_crc_event),
  1211. MIBSTAT(collision),
  1212. MIBSTAT(late_collision),
  1213. MIBSTAT(rx_discard),
  1214. MIBSTAT(rx_overrun),
  1215. LROSTAT(lro_aggregated),
  1216. LROSTAT(lro_flushed),
  1217. LROSTAT(lro_no_desc),
  1218. };
  1219. static int
  1220. mv643xx_eth_get_settings_phy(struct mv643xx_eth_private *mp,
  1221. struct ethtool_cmd *cmd)
  1222. {
  1223. int err;
  1224. err = phy_read_status(mp->phy);
  1225. if (err == 0)
  1226. err = phy_ethtool_gset(mp->phy, cmd);
  1227. /*
  1228. * The MAC does not support 1000baseT_Half.
  1229. */
  1230. cmd->supported &= ~SUPPORTED_1000baseT_Half;
  1231. cmd->advertising &= ~ADVERTISED_1000baseT_Half;
  1232. return err;
  1233. }
  1234. static int
  1235. mv643xx_eth_get_settings_phyless(struct mv643xx_eth_private *mp,
  1236. struct ethtool_cmd *cmd)
  1237. {
  1238. u32 port_status;
  1239. port_status = rdlp(mp, PORT_STATUS);
  1240. cmd->supported = SUPPORTED_MII;
  1241. cmd->advertising = ADVERTISED_MII;
  1242. switch (port_status & PORT_SPEED_MASK) {
  1243. case PORT_SPEED_10:
  1244. ethtool_cmd_speed_set(cmd, SPEED_10);
  1245. break;
  1246. case PORT_SPEED_100:
  1247. ethtool_cmd_speed_set(cmd, SPEED_100);
  1248. break;
  1249. case PORT_SPEED_1000:
  1250. ethtool_cmd_speed_set(cmd, SPEED_1000);
  1251. break;
  1252. default:
  1253. cmd->speed = -1;
  1254. break;
  1255. }
  1256. cmd->duplex = (port_status & FULL_DUPLEX) ? DUPLEX_FULL : DUPLEX_HALF;
  1257. cmd->port = PORT_MII;
  1258. cmd->phy_address = 0;
  1259. cmd->transceiver = XCVR_INTERNAL;
  1260. cmd->autoneg = AUTONEG_DISABLE;
  1261. cmd->maxtxpkt = 1;
  1262. cmd->maxrxpkt = 1;
  1263. return 0;
  1264. }
  1265. static void
  1266. mv643xx_eth_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
  1267. {
  1268. struct mv643xx_eth_private *mp = netdev_priv(dev);
  1269. wol->supported = 0;
  1270. wol->wolopts = 0;
  1271. if (mp->phy)
  1272. phy_ethtool_get_wol(mp->phy, wol);
  1273. }
  1274. static int
  1275. mv643xx_eth_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
  1276. {
  1277. struct mv643xx_eth_private *mp = netdev_priv(dev);
  1278. int err;
  1279. if (mp->phy == NULL)
  1280. return -EOPNOTSUPP;
  1281. err = phy_ethtool_set_wol(mp->phy, wol);
  1282. /* Given that mv643xx_eth works without the marvell-specific PHY driver,
  1283. * this debugging hint is useful to have.
  1284. */
  1285. if (err == -EOPNOTSUPP)
  1286. netdev_info(dev, "The PHY does not support set_wol, was CONFIG_MARVELL_PHY enabled?\n");
  1287. return err;
  1288. }
  1289. static int
  1290. mv643xx_eth_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  1291. {
  1292. struct mv643xx_eth_private *mp = netdev_priv(dev);
  1293. if (mp->phy != NULL)
  1294. return mv643xx_eth_get_settings_phy(mp, cmd);
  1295. else
  1296. return mv643xx_eth_get_settings_phyless(mp, cmd);
  1297. }
  1298. static int
  1299. mv643xx_eth_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  1300. {
  1301. struct mv643xx_eth_private *mp = netdev_priv(dev);
  1302. int ret;
  1303. if (mp->phy == NULL)
  1304. return -EINVAL;
  1305. /*
  1306. * The MAC does not support 1000baseT_Half.
  1307. */
  1308. cmd->advertising &= ~ADVERTISED_1000baseT_Half;
  1309. ret = phy_ethtool_sset(mp->phy, cmd);
  1310. if (!ret)
  1311. mv643xx_adjust_pscr(mp);
  1312. return ret;
  1313. }
  1314. static void mv643xx_eth_get_drvinfo(struct net_device *dev,
  1315. struct ethtool_drvinfo *drvinfo)
  1316. {
  1317. strlcpy(drvinfo->driver, mv643xx_eth_driver_name,
  1318. sizeof(drvinfo->driver));
  1319. strlcpy(drvinfo->version, mv643xx_eth_driver_version,
  1320. sizeof(drvinfo->version));
  1321. strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
  1322. strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
  1323. drvinfo->n_stats = ARRAY_SIZE(mv643xx_eth_stats);
  1324. }
  1325. static int mv643xx_eth_nway_reset(struct net_device *dev)
  1326. {
  1327. struct mv643xx_eth_private *mp = netdev_priv(dev);
  1328. if (mp->phy == NULL)
  1329. return -EINVAL;
  1330. return genphy_restart_aneg(mp->phy);
  1331. }
  1332. static int
  1333. mv643xx_eth_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
  1334. {
  1335. struct mv643xx_eth_private *mp = netdev_priv(dev);
  1336. ec->rx_coalesce_usecs = get_rx_coal(mp);
  1337. ec->tx_coalesce_usecs = get_tx_coal(mp);
  1338. return 0;
  1339. }
  1340. static int
  1341. mv643xx_eth_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
  1342. {
  1343. struct mv643xx_eth_private *mp = netdev_priv(dev);
  1344. set_rx_coal(mp, ec->rx_coalesce_usecs);
  1345. set_tx_coal(mp, ec->tx_coalesce_usecs);
  1346. return 0;
  1347. }
  1348. static void
  1349. mv643xx_eth_get_ringparam(struct net_device *dev, struct ethtool_ringparam *er)
  1350. {
  1351. struct mv643xx_eth_private *mp = netdev_priv(dev);
  1352. er->rx_max_pending = 4096;
  1353. er->tx_max_pending = 4096;
  1354. er->rx_pending = mp->rx_ring_size;
  1355. er->tx_pending = mp->tx_ring_size;
  1356. }
  1357. static int
  1358. mv643xx_eth_set_ringparam(struct net_device *dev, struct ethtool_ringparam *er)
  1359. {
  1360. struct mv643xx_eth_private *mp = netdev_priv(dev);
  1361. if (er->rx_mini_pending || er->rx_jumbo_pending)
  1362. return -EINVAL;
  1363. mp->rx_ring_size = er->rx_pending < 4096 ? er->rx_pending : 4096;
  1364. mp->tx_ring_size = er->tx_pending < 4096 ? er->tx_pending : 4096;
  1365. if (netif_running(dev)) {
  1366. mv643xx_eth_stop(dev);
  1367. if (mv643xx_eth_open(dev)) {
  1368. netdev_err(dev,
  1369. "fatal error on re-opening device after ring param change\n");
  1370. return -ENOMEM;
  1371. }
  1372. }
  1373. return 0;
  1374. }
  1375. static int
  1376. mv643xx_eth_set_features(struct net_device *dev, netdev_features_t features)
  1377. {
  1378. struct mv643xx_eth_private *mp = netdev_priv(dev);
  1379. bool rx_csum = features & NETIF_F_RXCSUM;
  1380. wrlp(mp, PORT_CONFIG, rx_csum ? 0x02000000 : 0x00000000);
  1381. return 0;
  1382. }
  1383. static void mv643xx_eth_get_strings(struct net_device *dev,
  1384. uint32_t stringset, uint8_t *data)
  1385. {
  1386. int i;
  1387. if (stringset == ETH_SS_STATS) {
  1388. for (i = 0; i < ARRAY_SIZE(mv643xx_eth_stats); i++) {
  1389. memcpy(data + i * ETH_GSTRING_LEN,
  1390. mv643xx_eth_stats[i].stat_string,
  1391. ETH_GSTRING_LEN);
  1392. }
  1393. }
  1394. }
  1395. static void mv643xx_eth_get_ethtool_stats(struct net_device *dev,
  1396. struct ethtool_stats *stats,
  1397. uint64_t *data)
  1398. {
  1399. struct mv643xx_eth_private *mp = netdev_priv(dev);
  1400. int i;
  1401. mv643xx_eth_get_stats(dev);
  1402. mib_counters_update(mp);
  1403. mv643xx_eth_grab_lro_stats(mp);
  1404. for (i = 0; i < ARRAY_SIZE(mv643xx_eth_stats); i++) {
  1405. const struct mv643xx_eth_stats *stat;
  1406. void *p;
  1407. stat = mv643xx_eth_stats + i;
  1408. if (stat->netdev_off >= 0)
  1409. p = ((void *)mp->dev) + stat->netdev_off;
  1410. else
  1411. p = ((void *)mp) + stat->mp_off;
  1412. data[i] = (stat->sizeof_stat == 8) ?
  1413. *(uint64_t *)p : *(uint32_t *)p;
  1414. }
  1415. }
  1416. static int mv643xx_eth_get_sset_count(struct net_device *dev, int sset)
  1417. {
  1418. if (sset == ETH_SS_STATS)
  1419. return ARRAY_SIZE(mv643xx_eth_stats);
  1420. return -EOPNOTSUPP;
  1421. }
  1422. static const struct ethtool_ops mv643xx_eth_ethtool_ops = {
  1423. .get_settings = mv643xx_eth_get_settings,
  1424. .set_settings = mv643xx_eth_set_settings,
  1425. .get_drvinfo = mv643xx_eth_get_drvinfo,
  1426. .nway_reset = mv643xx_eth_nway_reset,
  1427. .get_link = ethtool_op_get_link,
  1428. .get_coalesce = mv643xx_eth_get_coalesce,
  1429. .set_coalesce = mv643xx_eth_set_coalesce,
  1430. .get_ringparam = mv643xx_eth_get_ringparam,
  1431. .set_ringparam = mv643xx_eth_set_ringparam,
  1432. .get_strings = mv643xx_eth_get_strings,
  1433. .get_ethtool_stats = mv643xx_eth_get_ethtool_stats,
  1434. .get_sset_count = mv643xx_eth_get_sset_count,
  1435. .get_ts_info = ethtool_op_get_ts_info,
  1436. .get_wol = mv643xx_eth_get_wol,
  1437. .set_wol = mv643xx_eth_set_wol,
  1438. };
  1439. /* address handling *********************************************************/
  1440. static void uc_addr_get(struct mv643xx_eth_private *mp, unsigned char *addr)
  1441. {
  1442. unsigned int mac_h = rdlp(mp, MAC_ADDR_HIGH);
  1443. unsigned int mac_l = rdlp(mp, MAC_ADDR_LOW);
  1444. addr[0] = (mac_h >> 24) & 0xff;
  1445. addr[1] = (mac_h >> 16) & 0xff;
  1446. addr[2] = (mac_h >> 8) & 0xff;
  1447. addr[3] = mac_h & 0xff;
  1448. addr[4] = (mac_l >> 8) & 0xff;
  1449. addr[5] = mac_l & 0xff;
  1450. }
  1451. static void uc_addr_set(struct mv643xx_eth_private *mp, unsigned char *addr)
  1452. {
  1453. wrlp(mp, MAC_ADDR_HIGH,
  1454. (addr[0] << 24) | (addr[1] << 16) | (addr[2] << 8) | addr[3]);
  1455. wrlp(mp, MAC_ADDR_LOW, (addr[4] << 8) | addr[5]);
  1456. }
  1457. static u32 uc_addr_filter_mask(struct net_device *dev)
  1458. {
  1459. struct netdev_hw_addr *ha;
  1460. u32 nibbles;
  1461. if (dev->flags & IFF_PROMISC)
  1462. return 0;
  1463. nibbles = 1 << (dev->dev_addr[5] & 0x0f);
  1464. netdev_for_each_uc_addr(ha, dev) {
  1465. if (memcmp(dev->dev_addr, ha->addr, 5))
  1466. return 0;
  1467. if ((dev->dev_addr[5] ^ ha->addr[5]) & 0xf0)
  1468. return 0;
  1469. nibbles |= 1 << (ha->addr[5] & 0x0f);
  1470. }
  1471. return nibbles;
  1472. }
  1473. static void mv643xx_eth_program_unicast_filter(struct net_device *dev)
  1474. {
  1475. struct mv643xx_eth_private *mp = netdev_priv(dev);
  1476. u32 port_config;
  1477. u32 nibbles;
  1478. int i;
  1479. uc_addr_set(mp, dev->dev_addr);
  1480. port_config = rdlp(mp, PORT_CONFIG) & ~UNICAST_PROMISCUOUS_MODE;
  1481. nibbles = uc_addr_filter_mask(dev);
  1482. if (!nibbles) {
  1483. port_config |= UNICAST_PROMISCUOUS_MODE;
  1484. nibbles = 0xffff;
  1485. }
  1486. for (i = 0; i < 16; i += 4) {
  1487. int off = UNICAST_TABLE(mp->port_num) + i;
  1488. u32 v;
  1489. v = 0;
  1490. if (nibbles & 1)
  1491. v |= 0x00000001;
  1492. if (nibbles & 2)
  1493. v |= 0x00000100;
  1494. if (nibbles & 4)
  1495. v |= 0x00010000;
  1496. if (nibbles & 8)
  1497. v |= 0x01000000;
  1498. nibbles >>= 4;
  1499. wrl(mp, off, v);
  1500. }
  1501. wrlp(mp, PORT_CONFIG, port_config);
  1502. }
  1503. static int addr_crc(unsigned char *addr)
  1504. {
  1505. int crc = 0;
  1506. int i;
  1507. for (i = 0; i < 6; i++) {
  1508. int j;
  1509. crc = (crc ^ addr[i]) << 8;
  1510. for (j = 7; j >= 0; j--) {
  1511. if (crc & (0x100 << j))
  1512. crc ^= 0x107 << j;
  1513. }
  1514. }
  1515. return crc;
  1516. }
  1517. static void mv643xx_eth_program_multicast_filter(struct net_device *dev)
  1518. {
  1519. struct mv643xx_eth_private *mp = netdev_priv(dev);
  1520. u32 *mc_spec;
  1521. u32 *mc_other;
  1522. struct netdev_hw_addr *ha;
  1523. int i;
  1524. if (dev->flags & (IFF_PROMISC | IFF_ALLMULTI)) {
  1525. int port_num;
  1526. u32 accept;
  1527. oom:
  1528. port_num = mp->port_num;
  1529. accept = 0x01010101;
  1530. for (i = 0; i < 0x100; i += 4) {
  1531. wrl(mp, SPECIAL_MCAST_TABLE(port_num) + i, accept);
  1532. wrl(mp, OTHER_MCAST_TABLE(port_num) + i, accept);
  1533. }
  1534. return;
  1535. }
  1536. mc_spec = kmalloc(0x200, GFP_ATOMIC);
  1537. if (mc_spec == NULL)
  1538. goto oom;
  1539. mc_other = mc_spec + (0x100 >> 2);
  1540. memset(mc_spec, 0, 0x100);
  1541. memset(mc_other, 0, 0x100);
  1542. netdev_for_each_mc_addr(ha, dev) {
  1543. u8 *a = ha->addr;
  1544. u32 *table;
  1545. int entry;
  1546. if (memcmp(a, "\x01\x00\x5e\x00\x00", 5) == 0) {
  1547. table = mc_spec;
  1548. entry = a[5];
  1549. } else {
  1550. table = mc_other;
  1551. entry = addr_crc(a);
  1552. }
  1553. table[entry >> 2] |= 1 << (8 * (entry & 3));
  1554. }
  1555. for (i = 0; i < 0x100; i += 4) {
  1556. wrl(mp, SPECIAL_MCAST_TABLE(mp->port_num) + i, mc_spec[i >> 2]);
  1557. wrl(mp, OTHER_MCAST_TABLE(mp->port_num) + i, mc_other[i >> 2]);
  1558. }
  1559. kfree(mc_spec);
  1560. }
  1561. static void mv643xx_eth_set_rx_mode(struct net_device *dev)
  1562. {
  1563. mv643xx_eth_program_unicast_filter(dev);
  1564. mv643xx_eth_program_multicast_filter(dev);
  1565. }
  1566. static int mv643xx_eth_set_mac_address(struct net_device *dev, void *addr)
  1567. {
  1568. struct sockaddr *sa = addr;
  1569. if (!is_valid_ether_addr(sa->sa_data))
  1570. return -EADDRNOTAVAIL;
  1571. memcpy(dev->dev_addr, sa->sa_data, ETH_ALEN);
  1572. netif_addr_lock_bh(dev);
  1573. mv643xx_eth_program_unicast_filter(dev);
  1574. netif_addr_unlock_bh(dev);
  1575. return 0;
  1576. }
  1577. /* rx/tx queue initialisation ***********************************************/
  1578. static int rxq_init(struct mv643xx_eth_private *mp, int index)
  1579. {
  1580. struct rx_queue *rxq = mp->rxq + index;
  1581. struct rx_desc *rx_desc;
  1582. int size;
  1583. int i;
  1584. rxq->index = index;
  1585. rxq->rx_ring_size = mp->rx_ring_size;
  1586. rxq->rx_desc_count = 0;
  1587. rxq->rx_curr_desc = 0;
  1588. rxq->rx_used_desc = 0;
  1589. size = rxq->rx_ring_size * sizeof(struct rx_desc);
  1590. if (index == 0 && size <= mp->rx_desc_sram_size) {
  1591. rxq->rx_desc_area = ioremap(mp->rx_desc_sram_addr,
  1592. mp->rx_desc_sram_size);
  1593. rxq->rx_desc_dma = mp->rx_desc_sram_addr;
  1594. } else {
  1595. rxq->rx_desc_area = dma_alloc_coherent(mp->dev->dev.parent,
  1596. size, &rxq->rx_desc_dma,
  1597. GFP_KERNEL);
  1598. }
  1599. if (rxq->rx_desc_area == NULL) {
  1600. netdev_err(mp->dev,
  1601. "can't allocate rx ring (%d bytes)\n", size);
  1602. goto out;
  1603. }
  1604. memset(rxq->rx_desc_area, 0, size);
  1605. rxq->rx_desc_area_size = size;
  1606. rxq->rx_skb = kmalloc_array(rxq->rx_ring_size, sizeof(*rxq->rx_skb),
  1607. GFP_KERNEL);
  1608. if (rxq->rx_skb == NULL)
  1609. goto out_free;
  1610. rx_desc = rxq->rx_desc_area;
  1611. for (i = 0; i < rxq->rx_ring_size; i++) {
  1612. int nexti;
  1613. nexti = i + 1;
  1614. if (nexti == rxq->rx_ring_size)
  1615. nexti = 0;
  1616. rx_desc[i].next_desc_ptr = rxq->rx_desc_dma +
  1617. nexti * sizeof(struct rx_desc);
  1618. }
  1619. rxq->lro_mgr.dev = mp->dev;
  1620. memset(&rxq->lro_mgr.stats, 0, sizeof(rxq->lro_mgr.stats));
  1621. rxq->lro_mgr.features = LRO_F_NAPI;
  1622. rxq->lro_mgr.ip_summed = CHECKSUM_UNNECESSARY;
  1623. rxq->lro_mgr.ip_summed_aggr = CHECKSUM_UNNECESSARY;
  1624. rxq->lro_mgr.max_desc = ARRAY_SIZE(rxq->lro_arr);
  1625. rxq->lro_mgr.max_aggr = 32;
  1626. rxq->lro_mgr.frag_align_pad = 0;
  1627. rxq->lro_mgr.lro_arr = rxq->lro_arr;
  1628. rxq->lro_mgr.get_skb_header = mv643xx_get_skb_header;
  1629. memset(&rxq->lro_arr, 0, sizeof(rxq->lro_arr));
  1630. return 0;
  1631. out_free:
  1632. if (index == 0 && size <= mp->rx_desc_sram_size)
  1633. iounmap(rxq->rx_desc_area);
  1634. else
  1635. dma_free_coherent(mp->dev->dev.parent, size,
  1636. rxq->rx_desc_area,
  1637. rxq->rx_desc_dma);
  1638. out:
  1639. return -ENOMEM;
  1640. }
  1641. static void rxq_deinit(struct rx_queue *rxq)
  1642. {
  1643. struct mv643xx_eth_private *mp = rxq_to_mp(rxq);
  1644. int i;
  1645. rxq_disable(rxq);
  1646. for (i = 0; i < rxq->rx_ring_size; i++) {
  1647. if (rxq->rx_skb[i]) {
  1648. dev_kfree_skb(rxq->rx_skb[i]);
  1649. rxq->rx_desc_count--;
  1650. }
  1651. }
  1652. if (rxq->rx_desc_count) {
  1653. netdev_err(mp->dev, "error freeing rx ring -- %d skbs stuck\n",
  1654. rxq->rx_desc_count);
  1655. }
  1656. if (rxq->index == 0 &&
  1657. rxq->rx_desc_area_size <= mp->rx_desc_sram_size)
  1658. iounmap(rxq->rx_desc_area);
  1659. else
  1660. dma_free_coherent(mp->dev->dev.parent, rxq->rx_desc_area_size,
  1661. rxq->rx_desc_area, rxq->rx_desc_dma);
  1662. kfree(rxq->rx_skb);
  1663. }
  1664. static int txq_init(struct mv643xx_eth_private *mp, int index)
  1665. {
  1666. struct tx_queue *txq = mp->txq + index;
  1667. struct tx_desc *tx_desc;
  1668. int size;
  1669. int i;
  1670. txq->index = index;
  1671. txq->tx_ring_size = mp->tx_ring_size;
  1672. txq->tx_desc_count = 0;
  1673. txq->tx_curr_desc = 0;
  1674. txq->tx_used_desc = 0;
  1675. size = txq->tx_ring_size * sizeof(struct tx_desc);
  1676. if (index == 0 && size <= mp->tx_desc_sram_size) {
  1677. txq->tx_desc_area = ioremap(mp->tx_desc_sram_addr,
  1678. mp->tx_desc_sram_size);
  1679. txq->tx_desc_dma = mp->tx_desc_sram_addr;
  1680. } else {
  1681. txq->tx_desc_area = dma_alloc_coherent(mp->dev->dev.parent,
  1682. size, &txq->tx_desc_dma,
  1683. GFP_KERNEL);
  1684. }
  1685. if (txq->tx_desc_area == NULL) {
  1686. netdev_err(mp->dev,
  1687. "can't allocate tx ring (%d bytes)\n", size);
  1688. return -ENOMEM;
  1689. }
  1690. memset(txq->tx_desc_area, 0, size);
  1691. txq->tx_desc_area_size = size;
  1692. tx_desc = txq->tx_desc_area;
  1693. for (i = 0; i < txq->tx_ring_size; i++) {
  1694. struct tx_desc *txd = tx_desc + i;
  1695. int nexti;
  1696. nexti = i + 1;
  1697. if (nexti == txq->tx_ring_size)
  1698. nexti = 0;
  1699. txd->cmd_sts = 0;
  1700. txd->next_desc_ptr = txq->tx_desc_dma +
  1701. nexti * sizeof(struct tx_desc);
  1702. }
  1703. skb_queue_head_init(&txq->tx_skb);
  1704. return 0;
  1705. }
  1706. static void txq_deinit(struct tx_queue *txq)
  1707. {
  1708. struct mv643xx_eth_private *mp = txq_to_mp(txq);
  1709. txq_disable(txq);
  1710. txq_reclaim(txq, txq->tx_ring_size, 1);
  1711. BUG_ON(txq->tx_used_desc != txq->tx_curr_desc);
  1712. if (txq->index == 0 &&
  1713. txq->tx_desc_area_size <= mp->tx_desc_sram_size)
  1714. iounmap(txq->tx_desc_area);
  1715. else
  1716. dma_free_coherent(mp->dev->dev.parent, txq->tx_desc_area_size,
  1717. txq->tx_desc_area, txq->tx_desc_dma);
  1718. }
  1719. /* netdev ops and related ***************************************************/
  1720. static int mv643xx_eth_collect_events(struct mv643xx_eth_private *mp)
  1721. {
  1722. u32 int_cause;
  1723. u32 int_cause_ext;
  1724. int_cause = rdlp(mp, INT_CAUSE) & mp->int_mask;
  1725. if (int_cause == 0)
  1726. return 0;
  1727. int_cause_ext = 0;
  1728. if (int_cause & INT_EXT) {
  1729. int_cause &= ~INT_EXT;
  1730. int_cause_ext = rdlp(mp, INT_CAUSE_EXT);
  1731. }
  1732. if (int_cause) {
  1733. wrlp(mp, INT_CAUSE, ~int_cause);
  1734. mp->work_tx_end |= ((int_cause & INT_TX_END) >> 19) &
  1735. ~(rdlp(mp, TXQ_COMMAND) & 0xff);
  1736. mp->work_rx |= (int_cause & INT_RX) >> 2;
  1737. }
  1738. int_cause_ext &= INT_EXT_LINK_PHY | INT_EXT_TX;
  1739. if (int_cause_ext) {
  1740. wrlp(mp, INT_CAUSE_EXT, ~int_cause_ext);
  1741. if (int_cause_ext & INT_EXT_LINK_PHY)
  1742. mp->work_link = 1;
  1743. mp->work_tx |= int_cause_ext & INT_EXT_TX;
  1744. }
  1745. return 1;
  1746. }
  1747. static irqreturn_t mv643xx_eth_irq(int irq, void *dev_id)
  1748. {
  1749. struct net_device *dev = (struct net_device *)dev_id;
  1750. struct mv643xx_eth_private *mp = netdev_priv(dev);
  1751. if (unlikely(!mv643xx_eth_collect_events(mp)))
  1752. return IRQ_NONE;
  1753. wrlp(mp, INT_MASK, 0);
  1754. napi_schedule(&mp->napi);
  1755. return IRQ_HANDLED;
  1756. }
  1757. static void handle_link_event(struct mv643xx_eth_private *mp)
  1758. {
  1759. struct net_device *dev = mp->dev;
  1760. u32 port_status;
  1761. int speed;
  1762. int duplex;
  1763. int fc;
  1764. port_status = rdlp(mp, PORT_STATUS);
  1765. if (!(port_status & LINK_UP)) {
  1766. if (netif_carrier_ok(dev)) {
  1767. int i;
  1768. netdev_info(dev, "link down\n");
  1769. netif_carrier_off(dev);
  1770. for (i = 0; i < mp->txq_count; i++) {
  1771. struct tx_queue *txq = mp->txq + i;
  1772. txq_reclaim(txq, txq->tx_ring_size, 1);
  1773. txq_reset_hw_ptr(txq);
  1774. }
  1775. }
  1776. return;
  1777. }
  1778. switch (port_status & PORT_SPEED_MASK) {
  1779. case PORT_SPEED_10:
  1780. speed = 10;
  1781. break;
  1782. case PORT_SPEED_100:
  1783. speed = 100;
  1784. break;
  1785. case PORT_SPEED_1000:
  1786. speed = 1000;
  1787. break;
  1788. default:
  1789. speed = -1;
  1790. break;
  1791. }
  1792. duplex = (port_status & FULL_DUPLEX) ? 1 : 0;
  1793. fc = (port_status & FLOW_CONTROL_ENABLED) ? 1 : 0;
  1794. netdev_info(dev, "link up, %d Mb/s, %s duplex, flow control %sabled\n",
  1795. speed, duplex ? "full" : "half", fc ? "en" : "dis");
  1796. if (!netif_carrier_ok(dev))
  1797. netif_carrier_on(dev);
  1798. }
  1799. static int mv643xx_eth_poll(struct napi_struct *napi, int budget)
  1800. {
  1801. struct mv643xx_eth_private *mp;
  1802. int work_done;
  1803. mp = container_of(napi, struct mv643xx_eth_private, napi);
  1804. if (unlikely(mp->oom)) {
  1805. mp->oom = 0;
  1806. del_timer(&mp->rx_oom);
  1807. }
  1808. work_done = 0;
  1809. while (work_done < budget) {
  1810. u8 queue_mask;
  1811. int queue;
  1812. int work_tbd;
  1813. if (mp->work_link) {
  1814. mp->work_link = 0;
  1815. handle_link_event(mp);
  1816. work_done++;
  1817. continue;
  1818. }
  1819. queue_mask = mp->work_tx | mp->work_tx_end | mp->work_rx;
  1820. if (likely(!mp->oom))
  1821. queue_mask |= mp->work_rx_refill;
  1822. if (!queue_mask) {
  1823. if (mv643xx_eth_collect_events(mp))
  1824. continue;
  1825. break;
  1826. }
  1827. queue = fls(queue_mask) - 1;
  1828. queue_mask = 1 << queue;
  1829. work_tbd = budget - work_done;
  1830. if (work_tbd > 16)
  1831. work_tbd = 16;
  1832. if (mp->work_tx_end & queue_mask) {
  1833. txq_kick(mp->txq + queue);
  1834. } else if (mp->work_tx & queue_mask) {
  1835. work_done += txq_reclaim(mp->txq + queue, work_tbd, 0);
  1836. txq_maybe_wake(mp->txq + queue);
  1837. } else if (mp->work_rx & queue_mask) {
  1838. work_done += rxq_process(mp->rxq + queue, work_tbd);
  1839. } else if (!mp->oom && (mp->work_rx_refill & queue_mask)) {
  1840. work_done += rxq_refill(mp->rxq + queue, work_tbd);
  1841. } else {
  1842. BUG();
  1843. }
  1844. }
  1845. if (work_done < budget) {
  1846. if (mp->oom)
  1847. mod_timer(&mp->rx_oom, jiffies + (HZ / 10));
  1848. napi_complete(napi);
  1849. wrlp(mp, INT_MASK, mp->int_mask);
  1850. }
  1851. return work_done;
  1852. }
  1853. static inline void oom_timer_wrapper(unsigned long data)
  1854. {
  1855. struct mv643xx_eth_private *mp = (void *)data;
  1856. napi_schedule(&mp->napi);
  1857. }
  1858. static void phy_reset(struct mv643xx_eth_private *mp)
  1859. {
  1860. int data;
  1861. data = phy_read(mp->phy, MII_BMCR);
  1862. if (data < 0)
  1863. return;
  1864. data |= BMCR_RESET;
  1865. if (phy_write(mp->phy, MII_BMCR, data) < 0)
  1866. return;
  1867. do {
  1868. data = phy_read(mp->phy, MII_BMCR);
  1869. } while (data >= 0 && data & BMCR_RESET);
  1870. }
  1871. static void port_start(struct mv643xx_eth_private *mp)
  1872. {
  1873. u32 pscr;
  1874. int i;
  1875. /*
  1876. * Perform PHY reset, if there is a PHY.
  1877. */
  1878. if (mp->phy != NULL) {
  1879. struct ethtool_cmd cmd;
  1880. mv643xx_eth_get_settings(mp->dev, &cmd);
  1881. phy_reset(mp);
  1882. mv643xx_eth_set_settings(mp->dev, &cmd);
  1883. }
  1884. /*
  1885. * Configure basic link parameters.
  1886. */
  1887. pscr = rdlp(mp, PORT_SERIAL_CONTROL);
  1888. pscr |= SERIAL_PORT_ENABLE;
  1889. wrlp(mp, PORT_SERIAL_CONTROL, pscr);
  1890. pscr |= DO_NOT_FORCE_LINK_FAIL;
  1891. if (mp->phy == NULL)
  1892. pscr |= FORCE_LINK_PASS;
  1893. wrlp(mp, PORT_SERIAL_CONTROL, pscr);
  1894. /*
  1895. * Configure TX path and queues.
  1896. */
  1897. tx_set_rate(mp, 1000000000, 16777216);
  1898. for (i = 0; i < mp->txq_count; i++) {
  1899. struct tx_queue *txq = mp->txq + i;
  1900. txq_reset_hw_ptr(txq);
  1901. txq_set_rate(txq, 1000000000, 16777216);
  1902. txq_set_fixed_prio_mode(txq);
  1903. }
  1904. /*
  1905. * Receive all unmatched unicast, TCP, UDP, BPDU and broadcast
  1906. * frames to RX queue #0, and include the pseudo-header when
  1907. * calculating receive checksums.
  1908. */
  1909. mv643xx_eth_set_features(mp->dev, mp->dev->features);
  1910. /*
  1911. * Treat BPDUs as normal multicasts, and disable partition mode.
  1912. */
  1913. wrlp(mp, PORT_CONFIG_EXT, 0x00000000);
  1914. /*
  1915. * Add configured unicast addresses to address filter table.
  1916. */
  1917. mv643xx_eth_program_unicast_filter(mp->dev);
  1918. /*
  1919. * Enable the receive queues.
  1920. */
  1921. for (i = 0; i < mp->rxq_count; i++) {
  1922. struct rx_queue *rxq = mp->rxq + i;
  1923. u32 addr;
  1924. addr = (u32)rxq->rx_desc_dma;
  1925. addr += rxq->rx_curr_desc * sizeof(struct rx_desc);
  1926. wrlp(mp, RXQ_CURRENT_DESC_PTR(i), addr);
  1927. rxq_enable(rxq);
  1928. }
  1929. }
  1930. static void mv643xx_eth_recalc_skb_size(struct mv643xx_eth_private *mp)
  1931. {
  1932. int skb_size;
  1933. /*
  1934. * Reserve 2+14 bytes for an ethernet header (the hardware
  1935. * automatically prepends 2 bytes of dummy data to each
  1936. * received packet), 16 bytes for up to four VLAN tags, and
  1937. * 4 bytes for the trailing FCS -- 36 bytes total.
  1938. */
  1939. skb_size = mp->dev->mtu + 36;
  1940. /*
  1941. * Make sure that the skb size is a multiple of 8 bytes, as
  1942. * the lower three bits of the receive descriptor's buffer
  1943. * size field are ignored by the hardware.
  1944. */
  1945. mp->skb_size = (skb_size + 7) & ~7;
  1946. /*
  1947. * If NET_SKB_PAD is smaller than a cache line,
  1948. * netdev_alloc_skb() will cause skb->data to be misaligned
  1949. * to a cache line boundary. If this is the case, include
  1950. * some extra space to allow re-aligning the data area.
  1951. */
  1952. mp->skb_size += SKB_DMA_REALIGN;
  1953. }
  1954. static int mv643xx_eth_open(struct net_device *dev)
  1955. {
  1956. struct mv643xx_eth_private *mp = netdev_priv(dev);
  1957. int err;
  1958. int i;
  1959. wrlp(mp, INT_CAUSE, 0);
  1960. wrlp(mp, INT_CAUSE_EXT, 0);
  1961. rdlp(mp, INT_CAUSE_EXT);
  1962. err = request_irq(dev->irq, mv643xx_eth_irq,
  1963. IRQF_SHARED, dev->name, dev);
  1964. if (err) {
  1965. netdev_err(dev, "can't assign irq\n");
  1966. return -EAGAIN;
  1967. }
  1968. mv643xx_eth_recalc_skb_size(mp);
  1969. napi_enable(&mp->napi);
  1970. mp->int_mask = INT_EXT;
  1971. for (i = 0; i < mp->rxq_count; i++) {
  1972. err = rxq_init(mp, i);
  1973. if (err) {
  1974. while (--i >= 0)
  1975. rxq_deinit(mp->rxq + i);
  1976. goto out;
  1977. }
  1978. rxq_refill(mp->rxq + i, INT_MAX);
  1979. mp->int_mask |= INT_RX_0 << i;
  1980. }
  1981. if (mp->oom) {
  1982. mp->rx_oom.expires = jiffies + (HZ / 10);
  1983. add_timer(&mp->rx_oom);
  1984. }
  1985. for (i = 0; i < mp->txq_count; i++) {
  1986. err = txq_init(mp, i);
  1987. if (err) {
  1988. while (--i >= 0)
  1989. txq_deinit(mp->txq + i);
  1990. goto out_free;
  1991. }
  1992. mp->int_mask |= INT_TX_END_0 << i;
  1993. }
  1994. port_start(mp);
  1995. wrlp(mp, INT_MASK_EXT, INT_EXT_LINK_PHY | INT_EXT_TX);
  1996. wrlp(mp, INT_MASK, mp->int_mask);
  1997. return 0;
  1998. out_free:
  1999. for (i = 0; i < mp->rxq_count; i++)
  2000. rxq_deinit(mp->rxq + i);
  2001. out:
  2002. free_irq(dev->irq, dev);
  2003. return err;
  2004. }
  2005. static void port_reset(struct mv643xx_eth_private *mp)
  2006. {
  2007. unsigned int data;
  2008. int i;
  2009. for (i = 0; i < mp->rxq_count; i++)
  2010. rxq_disable(mp->rxq + i);
  2011. for (i = 0; i < mp->txq_count; i++)
  2012. txq_disable(mp->txq + i);
  2013. while (1) {
  2014. u32 ps = rdlp(mp, PORT_STATUS);
  2015. if ((ps & (TX_IN_PROGRESS | TX_FIFO_EMPTY)) == TX_FIFO_EMPTY)
  2016. break;
  2017. udelay(10);
  2018. }
  2019. /* Reset the Enable bit in the Configuration Register */
  2020. data = rdlp(mp, PORT_SERIAL_CONTROL);
  2021. data &= ~(SERIAL_PORT_ENABLE |
  2022. DO_NOT_FORCE_LINK_FAIL |
  2023. FORCE_LINK_PASS);
  2024. wrlp(mp, PORT_SERIAL_CONTROL, data);
  2025. }
  2026. static int mv643xx_eth_stop(struct net_device *dev)
  2027. {
  2028. struct mv643xx_eth_private *mp = netdev_priv(dev);
  2029. int i;
  2030. wrlp(mp, INT_MASK_EXT, 0x00000000);
  2031. wrlp(mp, INT_MASK, 0x00000000);
  2032. rdlp(mp, INT_MASK);
  2033. napi_disable(&mp->napi);
  2034. del_timer_sync(&mp->rx_oom);
  2035. netif_carrier_off(dev);
  2036. free_irq(dev->irq, dev);
  2037. port_reset(mp);
  2038. mv643xx_eth_get_stats(dev);
  2039. mib_counters_update(mp);
  2040. del_timer_sync(&mp->mib_counters_timer);
  2041. for (i = 0; i < mp->rxq_count; i++)
  2042. rxq_deinit(mp->rxq + i);
  2043. for (i = 0; i < mp->txq_count; i++)
  2044. txq_deinit(mp->txq + i);
  2045. return 0;
  2046. }
  2047. static int mv643xx_eth_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  2048. {
  2049. struct mv643xx_eth_private *mp = netdev_priv(dev);
  2050. int ret;
  2051. if (mp->phy == NULL)
  2052. return -ENOTSUPP;
  2053. ret = phy_mii_ioctl(mp->phy, ifr, cmd);
  2054. if (!ret)
  2055. mv643xx_adjust_pscr(mp);
  2056. return ret;
  2057. }
  2058. static int mv643xx_eth_change_mtu(struct net_device *dev, int new_mtu)
  2059. {
  2060. struct mv643xx_eth_private *mp = netdev_priv(dev);
  2061. if (new_mtu < 64 || new_mtu > 9500)
  2062. return -EINVAL;
  2063. dev->mtu = new_mtu;
  2064. mv643xx_eth_recalc_skb_size(mp);
  2065. tx_set_rate(mp, 1000000000, 16777216);
  2066. if (!netif_running(dev))
  2067. return 0;
  2068. /*
  2069. * Stop and then re-open the interface. This will allocate RX
  2070. * skbs of the new MTU.
  2071. * There is a possible danger that the open will not succeed,
  2072. * due to memory being full.
  2073. */
  2074. mv643xx_eth_stop(dev);
  2075. if (mv643xx_eth_open(dev)) {
  2076. netdev_err(dev,
  2077. "fatal error on re-opening device after MTU change\n");
  2078. }
  2079. return 0;
  2080. }
  2081. static void tx_timeout_task(struct work_struct *ugly)
  2082. {
  2083. struct mv643xx_eth_private *mp;
  2084. mp = container_of(ugly, struct mv643xx_eth_private, tx_timeout_task);
  2085. if (netif_running(mp->dev)) {
  2086. netif_tx_stop_all_queues(mp->dev);
  2087. port_reset(mp);
  2088. port_start(mp);
  2089. netif_tx_wake_all_queues(mp->dev);
  2090. }
  2091. }
  2092. static void mv643xx_eth_tx_timeout(struct net_device *dev)
  2093. {
  2094. struct mv643xx_eth_private *mp = netdev_priv(dev);
  2095. netdev_info(dev, "tx timeout\n");
  2096. schedule_work(&mp->tx_timeout_task);
  2097. }
  2098. #ifdef CONFIG_NET_POLL_CONTROLLER
  2099. static void mv643xx_eth_netpoll(struct net_device *dev)
  2100. {
  2101. struct mv643xx_eth_private *mp = netdev_priv(dev);
  2102. wrlp(mp, INT_MASK, 0x00000000);
  2103. rdlp(mp, INT_MASK);
  2104. mv643xx_eth_irq(dev->irq, dev);
  2105. wrlp(mp, INT_MASK, mp->int_mask);
  2106. }
  2107. #endif
  2108. /* platform glue ************************************************************/
  2109. static void
  2110. mv643xx_eth_conf_mbus_windows(struct mv643xx_eth_shared_private *msp,
  2111. const struct mbus_dram_target_info *dram)
  2112. {
  2113. void __iomem *base = msp->base;
  2114. u32 win_enable;
  2115. u32 win_protect;
  2116. int i;
  2117. for (i = 0; i < 6; i++) {
  2118. writel(0, base + WINDOW_BASE(i));
  2119. writel(0, base + WINDOW_SIZE(i));
  2120. if (i < 4)
  2121. writel(0, base + WINDOW_REMAP_HIGH(i));
  2122. }
  2123. win_enable = 0x3f;
  2124. win_protect = 0;
  2125. for (i = 0; i < dram->num_cs; i++) {
  2126. const struct mbus_dram_window *cs = dram->cs + i;
  2127. writel((cs->base & 0xffff0000) |
  2128. (cs->mbus_attr << 8) |
  2129. dram->mbus_dram_target_id, base + WINDOW_BASE(i));
  2130. writel((cs->size - 1) & 0xffff0000, base + WINDOW_SIZE(i));
  2131. win_enable &= ~(1 << i);
  2132. win_protect |= 3 << (2 * i);
  2133. }
  2134. writel(win_enable, base + WINDOW_BAR_ENABLE);
  2135. msp->win_protect = win_protect;
  2136. }
  2137. static void infer_hw_params(struct mv643xx_eth_shared_private *msp)
  2138. {
  2139. /*
  2140. * Check whether we have a 14-bit coal limit field in bits
  2141. * [21:8], or a 16-bit coal limit in bits [25,21:7] of the
  2142. * SDMA config register.
  2143. */
  2144. writel(0x02000000, msp->base + 0x0400 + SDMA_CONFIG);
  2145. if (readl(msp->base + 0x0400 + SDMA_CONFIG) & 0x02000000)
  2146. msp->extended_rx_coal_limit = 1;
  2147. else
  2148. msp->extended_rx_coal_limit = 0;
  2149. /*
  2150. * Check whether the MAC supports TX rate control, and if
  2151. * yes, whether its associated registers are in the old or
  2152. * the new place.
  2153. */
  2154. writel(1, msp->base + 0x0400 + TX_BW_MTU_MOVED);
  2155. if (readl(msp->base + 0x0400 + TX_BW_MTU_MOVED) & 1) {
  2156. msp->tx_bw_control = TX_BW_CONTROL_NEW_LAYOUT;
  2157. } else {
  2158. writel(7, msp->base + 0x0400 + TX_BW_RATE);
  2159. if (readl(msp->base + 0x0400 + TX_BW_RATE) & 7)
  2160. msp->tx_bw_control = TX_BW_CONTROL_OLD_LAYOUT;
  2161. else
  2162. msp->tx_bw_control = TX_BW_CONTROL_ABSENT;
  2163. }
  2164. }
  2165. static int mv643xx_eth_shared_probe(struct platform_device *pdev)
  2166. {
  2167. static int mv643xx_eth_version_printed;
  2168. struct mv643xx_eth_shared_platform_data *pd = pdev->dev.platform_data;
  2169. struct mv643xx_eth_shared_private *msp;
  2170. const struct mbus_dram_target_info *dram;
  2171. struct resource *res;
  2172. int ret;
  2173. if (!mv643xx_eth_version_printed++)
  2174. pr_notice("MV-643xx 10/100/1000 ethernet driver version %s\n",
  2175. mv643xx_eth_driver_version);
  2176. ret = -EINVAL;
  2177. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  2178. if (res == NULL)
  2179. goto out;
  2180. ret = -ENOMEM;
  2181. msp = kzalloc(sizeof(*msp), GFP_KERNEL);
  2182. if (msp == NULL)
  2183. goto out;
  2184. msp->base = ioremap(res->start, resource_size(res));
  2185. if (msp->base == NULL)
  2186. goto out_free;
  2187. /*
  2188. * Set up and register SMI bus.
  2189. */
  2190. if (pd == NULL || pd->shared_smi == NULL) {
  2191. msp->smi_bus = mdiobus_alloc();
  2192. if (msp->smi_bus == NULL)
  2193. goto out_unmap;
  2194. msp->smi_bus->priv = msp;
  2195. msp->smi_bus->name = "mv643xx_eth smi";
  2196. msp->smi_bus->read = smi_bus_read;
  2197. msp->smi_bus->write = smi_bus_write,
  2198. snprintf(msp->smi_bus->id, MII_BUS_ID_SIZE, "%s-%d",
  2199. pdev->name, pdev->id);
  2200. msp->smi_bus->parent = &pdev->dev;
  2201. msp->smi_bus->phy_mask = 0xffffffff;
  2202. if (mdiobus_register(msp->smi_bus) < 0)
  2203. goto out_free_mii_bus;
  2204. msp->smi = msp;
  2205. } else {
  2206. msp->smi = platform_get_drvdata(pd->shared_smi);
  2207. }
  2208. msp->err_interrupt = NO_IRQ;
  2209. init_waitqueue_head(&msp->smi_busy_wait);
  2210. /*
  2211. * Check whether the error interrupt is hooked up.
  2212. */
  2213. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  2214. if (res != NULL) {
  2215. int err;
  2216. err = request_irq(res->start, mv643xx_eth_err_irq,
  2217. IRQF_SHARED, "mv643xx_eth", msp);
  2218. if (!err) {
  2219. writel(ERR_INT_SMI_DONE, msp->base + ERR_INT_MASK);
  2220. msp->err_interrupt = res->start;
  2221. }
  2222. }
  2223. /*
  2224. * (Re-)program MBUS remapping windows if we are asked to.
  2225. */
  2226. dram = mv_mbus_dram_info();
  2227. if (dram)
  2228. mv643xx_eth_conf_mbus_windows(msp, dram);
  2229. msp->tx_csum_limit = (pd != NULL && pd->tx_csum_limit) ?
  2230. pd->tx_csum_limit : 9 * 1024;
  2231. infer_hw_params(msp);
  2232. platform_set_drvdata(pdev, msp);
  2233. return 0;
  2234. out_free_mii_bus:
  2235. mdiobus_free(msp->smi_bus);
  2236. out_unmap:
  2237. iounmap(msp->base);
  2238. out_free:
  2239. kfree(msp);
  2240. out:
  2241. return ret;
  2242. }
  2243. static int mv643xx_eth_shared_remove(struct platform_device *pdev)
  2244. {
  2245. struct mv643xx_eth_shared_private *msp = platform_get_drvdata(pdev);
  2246. struct mv643xx_eth_shared_platform_data *pd = pdev->dev.platform_data;
  2247. if (pd == NULL || pd->shared_smi == NULL) {
  2248. mdiobus_unregister(msp->smi_bus);
  2249. mdiobus_free(msp->smi_bus);
  2250. }
  2251. if (msp->err_interrupt != NO_IRQ)
  2252. free_irq(msp->err_interrupt, msp);
  2253. iounmap(msp->base);
  2254. kfree(msp);
  2255. return 0;
  2256. }
  2257. static struct platform_driver mv643xx_eth_shared_driver = {
  2258. .probe = mv643xx_eth_shared_probe,
  2259. .remove = mv643xx_eth_shared_remove,
  2260. .driver = {
  2261. .name = MV643XX_ETH_SHARED_NAME,
  2262. .owner = THIS_MODULE,
  2263. },
  2264. };
  2265. static void phy_addr_set(struct mv643xx_eth_private *mp, int phy_addr)
  2266. {
  2267. int addr_shift = 5 * mp->port_num;
  2268. u32 data;
  2269. data = rdl(mp, PHY_ADDR);
  2270. data &= ~(0x1f << addr_shift);
  2271. data |= (phy_addr & 0x1f) << addr_shift;
  2272. wrl(mp, PHY_ADDR, data);
  2273. }
  2274. static int phy_addr_get(struct mv643xx_eth_private *mp)
  2275. {
  2276. unsigned int data;
  2277. data = rdl(mp, PHY_ADDR);
  2278. return (data >> (5 * mp->port_num)) & 0x1f;
  2279. }
  2280. static void set_params(struct mv643xx_eth_private *mp,
  2281. struct mv643xx_eth_platform_data *pd)
  2282. {
  2283. struct net_device *dev = mp->dev;
  2284. if (is_valid_ether_addr(pd->mac_addr))
  2285. memcpy(dev->dev_addr, pd->mac_addr, 6);
  2286. else
  2287. uc_addr_get(mp, dev->dev_addr);
  2288. mp->rx_ring_size = DEFAULT_RX_QUEUE_SIZE;
  2289. if (pd->rx_queue_size)
  2290. mp->rx_ring_size = pd->rx_queue_size;
  2291. mp->rx_desc_sram_addr = pd->rx_sram_addr;
  2292. mp->rx_desc_sram_size = pd->rx_sram_size;
  2293. mp->rxq_count = pd->rx_queue_count ? : 1;
  2294. mp->tx_ring_size = DEFAULT_TX_QUEUE_SIZE;
  2295. if (pd->tx_queue_size)
  2296. mp->tx_ring_size = pd->tx_queue_size;
  2297. mp->tx_desc_sram_addr = pd->tx_sram_addr;
  2298. mp->tx_desc_sram_size = pd->tx_sram_size;
  2299. mp->txq_count = pd->tx_queue_count ? : 1;
  2300. }
  2301. static struct phy_device *phy_scan(struct mv643xx_eth_private *mp,
  2302. int phy_addr)
  2303. {
  2304. struct mii_bus *bus = mp->shared->smi->smi_bus;
  2305. struct phy_device *phydev;
  2306. int start;
  2307. int num;
  2308. int i;
  2309. if (phy_addr == MV643XX_ETH_PHY_ADDR_DEFAULT) {
  2310. start = phy_addr_get(mp) & 0x1f;
  2311. num = 32;
  2312. } else {
  2313. start = phy_addr & 0x1f;
  2314. num = 1;
  2315. }
  2316. phydev = NULL;
  2317. for (i = 0; i < num; i++) {
  2318. int addr = (start + i) & 0x1f;
  2319. if (bus->phy_map[addr] == NULL)
  2320. mdiobus_scan(bus, addr);
  2321. if (phydev == NULL) {
  2322. phydev = bus->phy_map[addr];
  2323. if (phydev != NULL)
  2324. phy_addr_set(mp, addr);
  2325. }
  2326. }
  2327. return phydev;
  2328. }
  2329. static void phy_init(struct mv643xx_eth_private *mp, int speed, int duplex)
  2330. {
  2331. struct phy_device *phy = mp->phy;
  2332. phy_reset(mp);
  2333. phy_attach(mp->dev, dev_name(&phy->dev), PHY_INTERFACE_MODE_GMII);
  2334. if (speed == 0) {
  2335. phy->autoneg = AUTONEG_ENABLE;
  2336. phy->speed = 0;
  2337. phy->duplex = 0;
  2338. phy->advertising = phy->supported | ADVERTISED_Autoneg;
  2339. } else {
  2340. phy->autoneg = AUTONEG_DISABLE;
  2341. phy->advertising = 0;
  2342. phy->speed = speed;
  2343. phy->duplex = duplex;
  2344. }
  2345. phy_start_aneg(phy);
  2346. }
  2347. static void init_pscr(struct mv643xx_eth_private *mp, int speed, int duplex)
  2348. {
  2349. u32 pscr;
  2350. pscr = rdlp(mp, PORT_SERIAL_CONTROL);
  2351. if (pscr & SERIAL_PORT_ENABLE) {
  2352. pscr &= ~SERIAL_PORT_ENABLE;
  2353. wrlp(mp, PORT_SERIAL_CONTROL, pscr);
  2354. }
  2355. pscr = MAX_RX_PACKET_9700BYTE | SERIAL_PORT_CONTROL_RESERVED;
  2356. if (mp->phy == NULL) {
  2357. pscr |= DISABLE_AUTO_NEG_SPEED_GMII;
  2358. if (speed == SPEED_1000)
  2359. pscr |= SET_GMII_SPEED_TO_1000;
  2360. else if (speed == SPEED_100)
  2361. pscr |= SET_MII_SPEED_TO_100;
  2362. pscr |= DISABLE_AUTO_NEG_FOR_FLOW_CTRL;
  2363. pscr |= DISABLE_AUTO_NEG_FOR_DUPLEX;
  2364. if (duplex == DUPLEX_FULL)
  2365. pscr |= SET_FULL_DUPLEX_MODE;
  2366. }
  2367. wrlp(mp, PORT_SERIAL_CONTROL, pscr);
  2368. }
  2369. static const struct net_device_ops mv643xx_eth_netdev_ops = {
  2370. .ndo_open = mv643xx_eth_open,
  2371. .ndo_stop = mv643xx_eth_stop,
  2372. .ndo_start_xmit = mv643xx_eth_xmit,
  2373. .ndo_set_rx_mode = mv643xx_eth_set_rx_mode,
  2374. .ndo_set_mac_address = mv643xx_eth_set_mac_address,
  2375. .ndo_validate_addr = eth_validate_addr,
  2376. .ndo_do_ioctl = mv643xx_eth_ioctl,
  2377. .ndo_change_mtu = mv643xx_eth_change_mtu,
  2378. .ndo_set_features = mv643xx_eth_set_features,
  2379. .ndo_tx_timeout = mv643xx_eth_tx_timeout,
  2380. .ndo_get_stats = mv643xx_eth_get_stats,
  2381. #ifdef CONFIG_NET_POLL_CONTROLLER
  2382. .ndo_poll_controller = mv643xx_eth_netpoll,
  2383. #endif
  2384. };
  2385. static int mv643xx_eth_probe(struct platform_device *pdev)
  2386. {
  2387. struct mv643xx_eth_platform_data *pd;
  2388. struct mv643xx_eth_private *mp;
  2389. struct net_device *dev;
  2390. struct resource *res;
  2391. int err;
  2392. pd = pdev->dev.platform_data;
  2393. if (pd == NULL) {
  2394. dev_err(&pdev->dev, "no mv643xx_eth_platform_data\n");
  2395. return -ENODEV;
  2396. }
  2397. if (pd->shared == NULL) {
  2398. dev_err(&pdev->dev, "no mv643xx_eth_platform_data->shared\n");
  2399. return -ENODEV;
  2400. }
  2401. dev = alloc_etherdev_mq(sizeof(struct mv643xx_eth_private), 8);
  2402. if (!dev)
  2403. return -ENOMEM;
  2404. mp = netdev_priv(dev);
  2405. platform_set_drvdata(pdev, mp);
  2406. mp->shared = platform_get_drvdata(pd->shared);
  2407. mp->base = mp->shared->base + 0x0400 + (pd->port_number << 10);
  2408. mp->port_num = pd->port_number;
  2409. mp->dev = dev;
  2410. /*
  2411. * Start with a default rate, and if there is a clock, allow
  2412. * it to override the default.
  2413. */
  2414. mp->t_clk = 133000000;
  2415. #if defined(CONFIG_HAVE_CLK)
  2416. mp->clk = clk_get(&pdev->dev, (pdev->id ? "1" : "0"));
  2417. if (!IS_ERR(mp->clk)) {
  2418. clk_prepare_enable(mp->clk);
  2419. mp->t_clk = clk_get_rate(mp->clk);
  2420. }
  2421. #endif
  2422. set_params(mp, pd);
  2423. netif_set_real_num_tx_queues(dev, mp->txq_count);
  2424. netif_set_real_num_rx_queues(dev, mp->rxq_count);
  2425. if (pd->phy_addr != MV643XX_ETH_PHY_NONE)
  2426. mp->phy = phy_scan(mp, pd->phy_addr);
  2427. if (mp->phy != NULL)
  2428. phy_init(mp, pd->speed, pd->duplex);
  2429. SET_ETHTOOL_OPS(dev, &mv643xx_eth_ethtool_ops);
  2430. init_pscr(mp, pd->speed, pd->duplex);
  2431. mib_counters_clear(mp);
  2432. init_timer(&mp->mib_counters_timer);
  2433. mp->mib_counters_timer.data = (unsigned long)mp;
  2434. mp->mib_counters_timer.function = mib_counters_timer_wrapper;
  2435. mp->mib_counters_timer.expires = jiffies + 30 * HZ;
  2436. add_timer(&mp->mib_counters_timer);
  2437. spin_lock_init(&mp->mib_counters_lock);
  2438. INIT_WORK(&mp->tx_timeout_task, tx_timeout_task);
  2439. netif_napi_add(dev, &mp->napi, mv643xx_eth_poll, 128);
  2440. init_timer(&mp->rx_oom);
  2441. mp->rx_oom.data = (unsigned long)mp;
  2442. mp->rx_oom.function = oom_timer_wrapper;
  2443. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  2444. BUG_ON(!res);
  2445. dev->irq = res->start;
  2446. dev->netdev_ops = &mv643xx_eth_netdev_ops;
  2447. dev->watchdog_timeo = 2 * HZ;
  2448. dev->base_addr = 0;
  2449. dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM |
  2450. NETIF_F_RXCSUM | NETIF_F_LRO;
  2451. dev->features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
  2452. dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM;
  2453. dev->priv_flags |= IFF_UNICAST_FLT;
  2454. SET_NETDEV_DEV(dev, &pdev->dev);
  2455. if (mp->shared->win_protect)
  2456. wrl(mp, WINDOW_PROTECT(mp->port_num), mp->shared->win_protect);
  2457. netif_carrier_off(dev);
  2458. wrlp(mp, SDMA_CONFIG, PORT_SDMA_CONFIG_DEFAULT_VALUE);
  2459. set_rx_coal(mp, 250);
  2460. set_tx_coal(mp, 0);
  2461. err = register_netdev(dev);
  2462. if (err)
  2463. goto out;
  2464. netdev_notice(dev, "port %d with MAC address %pM\n",
  2465. mp->port_num, dev->dev_addr);
  2466. if (mp->tx_desc_sram_size > 0)
  2467. netdev_notice(dev, "configured with sram\n");
  2468. return 0;
  2469. out:
  2470. #if defined(CONFIG_HAVE_CLK)
  2471. if (!IS_ERR(mp->clk)) {
  2472. clk_disable_unprepare(mp->clk);
  2473. clk_put(mp->clk);
  2474. }
  2475. #endif
  2476. free_netdev(dev);
  2477. return err;
  2478. }
  2479. static int mv643xx_eth_remove(struct platform_device *pdev)
  2480. {
  2481. struct mv643xx_eth_private *mp = platform_get_drvdata(pdev);
  2482. unregister_netdev(mp->dev);
  2483. if (mp->phy != NULL)
  2484. phy_detach(mp->phy);
  2485. cancel_work_sync(&mp->tx_timeout_task);
  2486. #if defined(CONFIG_HAVE_CLK)
  2487. if (!IS_ERR(mp->clk)) {
  2488. clk_disable_unprepare(mp->clk);
  2489. clk_put(mp->clk);
  2490. }
  2491. #endif
  2492. free_netdev(mp->dev);
  2493. platform_set_drvdata(pdev, NULL);
  2494. return 0;
  2495. }
  2496. static void mv643xx_eth_shutdown(struct platform_device *pdev)
  2497. {
  2498. struct mv643xx_eth_private *mp = platform_get_drvdata(pdev);
  2499. /* Mask all interrupts on ethernet port */
  2500. wrlp(mp, INT_MASK, 0);
  2501. rdlp(mp, INT_MASK);
  2502. if (netif_running(mp->dev))
  2503. port_reset(mp);
  2504. }
  2505. static struct platform_driver mv643xx_eth_driver = {
  2506. .probe = mv643xx_eth_probe,
  2507. .remove = mv643xx_eth_remove,
  2508. .shutdown = mv643xx_eth_shutdown,
  2509. .driver = {
  2510. .name = MV643XX_ETH_NAME,
  2511. .owner = THIS_MODULE,
  2512. },
  2513. };
  2514. static int __init mv643xx_eth_init_module(void)
  2515. {
  2516. int rc;
  2517. rc = platform_driver_register(&mv643xx_eth_shared_driver);
  2518. if (!rc) {
  2519. rc = platform_driver_register(&mv643xx_eth_driver);
  2520. if (rc)
  2521. platform_driver_unregister(&mv643xx_eth_shared_driver);
  2522. }
  2523. return rc;
  2524. }
  2525. module_init(mv643xx_eth_init_module);
  2526. static void __exit mv643xx_eth_cleanup_module(void)
  2527. {
  2528. platform_driver_unregister(&mv643xx_eth_driver);
  2529. platform_driver_unregister(&mv643xx_eth_shared_driver);
  2530. }
  2531. module_exit(mv643xx_eth_cleanup_module);
  2532. MODULE_AUTHOR("Rabeeh Khoury, Assaf Hoffman, Matthew Dharm, "
  2533. "Manish Lachwani, Dale Farnsworth and Lennert Buytenhek");
  2534. MODULE_DESCRIPTION("Ethernet driver for Marvell MV643XX");
  2535. MODULE_LICENSE("GPL");
  2536. MODULE_ALIAS("platform:" MV643XX_ETH_SHARED_NAME);
  2537. MODULE_ALIAS("platform:" MV643XX_ETH_NAME);