mv643xx_eth.c 63 KB

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