mv643xx_eth.c 63 KB

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