mv643xx_eth.c 67 KB

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