mv643xx_eth.c 78 KB

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