mv643xx_eth.c 71 KB

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