mv643xx_eth.c 64 KB

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