mv643xx_eth.c 79 KB

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