mv643xx_eth.c 63 KB

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