mv643xx_eth.c 62 KB

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