mv643xx_eth.c 66 KB

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