mv643xx_eth.c 79 KB

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