mv643xx_eth.c 65 KB

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