mv643xx_eth.c 77 KB

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