mv643xx_eth.c 79 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Driver for Marvell Discovery (MV643XX) and Marvell Orion ethernet ports
  4. * Copyright (C) 2002 Matthew Dharm <mdharm@momenco.com>
  5. *
  6. * Based on the 64360 driver from:
  7. * Copyright (C) 2002 Rabeeh Khoury <rabeeh@galileo.co.il>
  8. * Rabeeh Khoury <rabeeh@marvell.com>
  9. *
  10. * Copyright (C) 2003 PMC-Sierra, Inc.,
  11. * written by Manish Lachwani
  12. *
  13. * Copyright (C) 2003 Ralf Baechle <ralf@linux-mips.org>
  14. *
  15. * Copyright (C) 2004-2006 MontaVista Software, Inc.
  16. * Dale Farnsworth <dale@farnsworth.org>
  17. *
  18. * Copyright (C) 2004 Steven J. Hill <sjhill1@rockwellcollins.com>
  19. * <sjhill@realitydiluted.com>
  20. *
  21. * Copyright (C) 2007-2008 Marvell Semiconductor
  22. * Lennert Buytenhek <buytenh@marvell.com>
  23. *
  24. * Copyright (C) 2013 Michael Stapelberg <michael@stapelberg.de>
  25. */
  26. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  27. #include <linux/init.h>
  28. #include <linux/dma-mapping.h>
  29. #include <linux/in.h>
  30. #include <linux/ip.h>
  31. #include <net/tso.h>
  32. #include <linux/tcp.h>
  33. #include <linux/udp.h>
  34. #include <linux/etherdevice.h>
  35. #include <linux/delay.h>
  36. #include <linux/ethtool.h>
  37. #include <linux/platform_device.h>
  38. #include <linux/module.h>
  39. #include <linux/kernel.h>
  40. #include <linux/spinlock.h>
  41. #include <linux/workqueue.h>
  42. #include <linux/phy.h>
  43. #include <linux/mv643xx_eth.h>
  44. #include <linux/io.h>
  45. #include <linux/interrupt.h>
  46. #include <linux/types.h>
  47. #include <linux/slab.h>
  48. #include <linux/clk.h>
  49. #include <linux/of.h>
  50. #include <linux/of_irq.h>
  51. #include <linux/of_net.h>
  52. #include <linux/of_mdio.h>
  53. static char mv643xx_eth_driver_name[] = "mv643xx_eth";
  54. static char mv643xx_eth_driver_version[] = "1.4";
  55. /*
  56. * Registers shared between all ports.
  57. */
  58. #define PHY_ADDR 0x0000
  59. #define WINDOW_BASE(w) (0x0200 + ((w) << 3))
  60. #define WINDOW_SIZE(w) (0x0204 + ((w) << 3))
  61. #define WINDOW_REMAP_HIGH(w) (0x0280 + ((w) << 2))
  62. #define WINDOW_BAR_ENABLE 0x0290
  63. #define WINDOW_PROTECT(w) (0x0294 + ((w) << 4))
  64. /*
  65. * Main per-port registers. These live at offset 0x0400 for
  66. * port #0, 0x0800 for port #1, and 0x0c00 for port #2.
  67. */
  68. #define PORT_CONFIG 0x0000
  69. #define UNICAST_PROMISCUOUS_MODE 0x00000001
  70. #define PORT_CONFIG_EXT 0x0004
  71. #define MAC_ADDR_LOW 0x0014
  72. #define MAC_ADDR_HIGH 0x0018
  73. #define SDMA_CONFIG 0x001c
  74. #define TX_BURST_SIZE_16_64BIT 0x01000000
  75. #define TX_BURST_SIZE_4_64BIT 0x00800000
  76. #define BLM_TX_NO_SWAP 0x00000020
  77. #define BLM_RX_NO_SWAP 0x00000010
  78. #define RX_BURST_SIZE_16_64BIT 0x00000008
  79. #define RX_BURST_SIZE_4_64BIT 0x00000004
  80. #define PORT_SERIAL_CONTROL 0x003c
  81. #define SET_MII_SPEED_TO_100 0x01000000
  82. #define SET_GMII_SPEED_TO_1000 0x00800000
  83. #define SET_FULL_DUPLEX_MODE 0x00200000
  84. #define MAX_RX_PACKET_9700BYTE 0x000a0000
  85. #define DISABLE_AUTO_NEG_SPEED_GMII 0x00002000
  86. #define DO_NOT_FORCE_LINK_FAIL 0x00000400
  87. #define SERIAL_PORT_CONTROL_RESERVED 0x00000200
  88. #define DISABLE_AUTO_NEG_FOR_FLOW_CTRL 0x00000008
  89. #define DISABLE_AUTO_NEG_FOR_DUPLEX 0x00000004
  90. #define FORCE_LINK_PASS 0x00000002
  91. #define SERIAL_PORT_ENABLE 0x00000001
  92. #define PORT_STATUS 0x0044
  93. #define TX_FIFO_EMPTY 0x00000400
  94. #define TX_IN_PROGRESS 0x00000080
  95. #define PORT_SPEED_MASK 0x00000030
  96. #define PORT_SPEED_1000 0x00000010
  97. #define PORT_SPEED_100 0x00000020
  98. #define PORT_SPEED_10 0x00000000
  99. #define FLOW_CONTROL_ENABLED 0x00000008
  100. #define FULL_DUPLEX 0x00000004
  101. #define LINK_UP 0x00000002
  102. #define TXQ_COMMAND 0x0048
  103. #define TXQ_FIX_PRIO_CONF 0x004c
  104. #define PORT_SERIAL_CONTROL1 0x004c
  105. #define RGMII_EN 0x00000008
  106. #define CLK125_BYPASS_EN 0x00000010
  107. #define TX_BW_RATE 0x0050
  108. #define TX_BW_MTU 0x0058
  109. #define TX_BW_BURST 0x005c
  110. #define INT_CAUSE 0x0060
  111. #define INT_TX_END 0x07f80000
  112. #define INT_TX_END_0 0x00080000
  113. #define INT_RX 0x000003fc
  114. #define INT_RX_0 0x00000004
  115. #define INT_EXT 0x00000002
  116. #define INT_CAUSE_EXT 0x0064
  117. #define INT_EXT_LINK_PHY 0x00110000
  118. #define INT_EXT_TX 0x000000ff
  119. #define INT_MASK 0x0068
  120. #define INT_MASK_EXT 0x006c
  121. #define TX_FIFO_URGENT_THRESHOLD 0x0074
  122. #define RX_DISCARD_FRAME_CNT 0x0084
  123. #define RX_OVERRUN_FRAME_CNT 0x0088
  124. #define TXQ_FIX_PRIO_CONF_MOVED 0x00dc
  125. #define TX_BW_RATE_MOVED 0x00e0
  126. #define TX_BW_MTU_MOVED 0x00e8
  127. #define TX_BW_BURST_MOVED 0x00ec
  128. #define RXQ_CURRENT_DESC_PTR(q) (0x020c + ((q) << 4))
  129. #define RXQ_COMMAND 0x0280
  130. #define TXQ_CURRENT_DESC_PTR(q) (0x02c0 + ((q) << 2))
  131. #define TXQ_BW_TOKENS(q) (0x0300 + ((q) << 4))
  132. #define TXQ_BW_CONF(q) (0x0304 + ((q) << 4))
  133. #define TXQ_BW_WRR_CONF(q) (0x0308 + ((q) << 4))
  134. /*
  135. * Misc per-port registers.
  136. */
  137. #define MIB_COUNTERS(p) (0x1000 + ((p) << 7))
  138. #define SPECIAL_MCAST_TABLE(p) (0x1400 + ((p) << 10))
  139. #define OTHER_MCAST_TABLE(p) (0x1500 + ((p) << 10))
  140. #define UNICAST_TABLE(p) (0x1600 + ((p) << 10))
  141. /*
  142. * SDMA configuration register default value.
  143. */
  144. #if defined(__BIG_ENDIAN)
  145. #define PORT_SDMA_CONFIG_DEFAULT_VALUE \
  146. (RX_BURST_SIZE_4_64BIT | \
  147. TX_BURST_SIZE_4_64BIT)
  148. #elif defined(__LITTLE_ENDIAN)
  149. #define PORT_SDMA_CONFIG_DEFAULT_VALUE \
  150. (RX_BURST_SIZE_4_64BIT | \
  151. BLM_RX_NO_SWAP | \
  152. BLM_TX_NO_SWAP | \
  153. TX_BURST_SIZE_4_64BIT)
  154. #else
  155. #error One of __BIG_ENDIAN or __LITTLE_ENDIAN must be defined
  156. #endif
  157. /*
  158. * Misc definitions.
  159. */
  160. #define DEFAULT_RX_QUEUE_SIZE 128
  161. #define DEFAULT_TX_QUEUE_SIZE 512
  162. #define SKB_DMA_REALIGN ((PAGE_SIZE - NET_SKB_PAD) % SMP_CACHE_BYTES)
  163. /* Max number of allowed TCP segments for software TSO */
  164. #define MV643XX_MAX_TSO_SEGS 100
  165. #define MV643XX_MAX_SKB_DESCS (MV643XX_MAX_TSO_SEGS * 2 + MAX_SKB_FRAGS)
  166. #define IS_TSO_HEADER(txq, addr) \
  167. ((addr >= txq->tso_hdrs_dma) && \
  168. (addr < txq->tso_hdrs_dma + txq->tx_ring_size * TSO_HEADER_SIZE))
  169. #define DESC_DMA_MAP_SINGLE 0
  170. #define DESC_DMA_MAP_PAGE 1
  171. /*
  172. * RX/TX descriptors.
  173. */
  174. #if defined(__BIG_ENDIAN)
  175. struct rx_desc {
  176. u16 byte_cnt; /* Descriptor buffer byte count */
  177. u16 buf_size; /* Buffer size */
  178. u32 cmd_sts; /* Descriptor command status */
  179. u32 next_desc_ptr; /* Next descriptor pointer */
  180. u32 buf_ptr; /* Descriptor buffer pointer */
  181. };
  182. struct tx_desc {
  183. u16 byte_cnt; /* buffer byte count */
  184. u16 l4i_chk; /* CPU provided TCP checksum */
  185. u32 cmd_sts; /* Command/status field */
  186. u32 next_desc_ptr; /* Pointer to next descriptor */
  187. u32 buf_ptr; /* pointer to buffer for this descriptor*/
  188. };
  189. #elif defined(__LITTLE_ENDIAN)
  190. struct rx_desc {
  191. u32 cmd_sts; /* Descriptor command status */
  192. u16 buf_size; /* Buffer size */
  193. u16 byte_cnt; /* Descriptor buffer byte count */
  194. u32 buf_ptr; /* Descriptor buffer pointer */
  195. u32 next_desc_ptr; /* Next descriptor pointer */
  196. };
  197. struct tx_desc {
  198. u32 cmd_sts; /* Command/status field */
  199. u16 l4i_chk; /* CPU provided TCP checksum */
  200. u16 byte_cnt; /* buffer byte count */
  201. u32 buf_ptr; /* pointer to buffer for this descriptor*/
  202. u32 next_desc_ptr; /* Pointer to next descriptor */
  203. };
  204. #else
  205. #error One of __BIG_ENDIAN or __LITTLE_ENDIAN must be defined
  206. #endif
  207. /* RX & TX descriptor command */
  208. #define BUFFER_OWNED_BY_DMA 0x80000000
  209. /* RX & TX descriptor status */
  210. #define ERROR_SUMMARY 0x00000001
  211. /* RX descriptor status */
  212. #define LAYER_4_CHECKSUM_OK 0x40000000
  213. #define RX_ENABLE_INTERRUPT 0x20000000
  214. #define RX_FIRST_DESC 0x08000000
  215. #define RX_LAST_DESC 0x04000000
  216. #define RX_IP_HDR_OK 0x02000000
  217. #define RX_PKT_IS_IPV4 0x01000000
  218. #define RX_PKT_IS_ETHERNETV2 0x00800000
  219. #define RX_PKT_LAYER4_TYPE_MASK 0x00600000
  220. #define RX_PKT_LAYER4_TYPE_TCP_IPV4 0x00000000
  221. #define RX_PKT_IS_VLAN_TAGGED 0x00080000
  222. /* TX descriptor command */
  223. #define TX_ENABLE_INTERRUPT 0x00800000
  224. #define GEN_CRC 0x00400000
  225. #define TX_FIRST_DESC 0x00200000
  226. #define TX_LAST_DESC 0x00100000
  227. #define ZERO_PADDING 0x00080000
  228. #define GEN_IP_V4_CHECKSUM 0x00040000
  229. #define GEN_TCP_UDP_CHECKSUM 0x00020000
  230. #define UDP_FRAME 0x00010000
  231. #define MAC_HDR_EXTRA_4_BYTES 0x00008000
  232. #define GEN_TCP_UDP_CHK_FULL 0x00000400
  233. #define MAC_HDR_EXTRA_8_BYTES 0x00000200
  234. #define TX_IHL_SHIFT 11
  235. /* global *******************************************************************/
  236. struct mv643xx_eth_shared_private {
  237. /*
  238. * Ethernet controller base address.
  239. */
  240. void __iomem *base;
  241. /*
  242. * Per-port MBUS window access register value.
  243. */
  244. u32 win_protect;
  245. /*
  246. * Hardware-specific parameters.
  247. */
  248. int extended_rx_coal_limit;
  249. int tx_bw_control;
  250. int tx_csum_limit;
  251. struct clk *clk;
  252. };
  253. #define TX_BW_CONTROL_ABSENT 0
  254. #define TX_BW_CONTROL_OLD_LAYOUT 1
  255. #define TX_BW_CONTROL_NEW_LAYOUT 2
  256. static int mv643xx_eth_open(struct net_device *dev);
  257. static int mv643xx_eth_stop(struct net_device *dev);
  258. /* per-port *****************************************************************/
  259. struct mib_counters {
  260. u64 good_octets_received;
  261. u32 bad_octets_received;
  262. u32 internal_mac_transmit_err;
  263. u32 good_frames_received;
  264. u32 bad_frames_received;
  265. u32 broadcast_frames_received;
  266. u32 multicast_frames_received;
  267. u32 frames_64_octets;
  268. u32 frames_65_to_127_octets;
  269. u32 frames_128_to_255_octets;
  270. u32 frames_256_to_511_octets;
  271. u32 frames_512_to_1023_octets;
  272. u32 frames_1024_to_max_octets;
  273. u64 good_octets_sent;
  274. u32 good_frames_sent;
  275. u32 excessive_collision;
  276. u32 multicast_frames_sent;
  277. u32 broadcast_frames_sent;
  278. u32 unrec_mac_control_received;
  279. u32 fc_sent;
  280. u32 good_fc_received;
  281. u32 bad_fc_received;
  282. u32 undersize_received;
  283. u32 fragments_received;
  284. u32 oversize_received;
  285. u32 jabber_received;
  286. u32 mac_receive_error;
  287. u32 bad_crc_event;
  288. u32 collision;
  289. u32 late_collision;
  290. /* Non MIB hardware counters */
  291. u32 rx_discard;
  292. u32 rx_overrun;
  293. };
  294. struct rx_queue {
  295. int index;
  296. int rx_ring_size;
  297. int rx_desc_count;
  298. int rx_curr_desc;
  299. int rx_used_desc;
  300. struct rx_desc *rx_desc_area;
  301. dma_addr_t rx_desc_dma;
  302. int rx_desc_area_size;
  303. struct sk_buff **rx_skb;
  304. };
  305. struct tx_queue {
  306. int index;
  307. int tx_ring_size;
  308. int tx_desc_count;
  309. int tx_curr_desc;
  310. int tx_used_desc;
  311. int tx_stop_threshold;
  312. int tx_wake_threshold;
  313. char *tso_hdrs;
  314. dma_addr_t tso_hdrs_dma;
  315. struct tx_desc *tx_desc_area;
  316. char *tx_desc_mapping; /* array to track the type of the dma mapping */
  317. dma_addr_t tx_desc_dma;
  318. int tx_desc_area_size;
  319. struct sk_buff_head tx_skb;
  320. unsigned long tx_packets;
  321. unsigned long tx_bytes;
  322. unsigned long tx_dropped;
  323. };
  324. struct mv643xx_eth_private {
  325. struct mv643xx_eth_shared_private *shared;
  326. void __iomem *base;
  327. int port_num;
  328. struct net_device *dev;
  329. struct timer_list mib_counters_timer;
  330. spinlock_t mib_counters_lock;
  331. struct mib_counters mib_counters;
  332. struct work_struct tx_timeout_task;
  333. struct napi_struct napi;
  334. u32 int_mask;
  335. u8 oom;
  336. u8 work_link;
  337. u8 work_tx;
  338. u8 work_tx_end;
  339. u8 work_rx;
  340. u8 work_rx_refill;
  341. int skb_size;
  342. /*
  343. * RX state.
  344. */
  345. int rx_ring_size;
  346. unsigned long rx_desc_sram_addr;
  347. int rx_desc_sram_size;
  348. int rxq_count;
  349. struct timer_list rx_oom;
  350. struct rx_queue rxq[8];
  351. /*
  352. * TX state.
  353. */
  354. int tx_ring_size;
  355. unsigned long tx_desc_sram_addr;
  356. int tx_desc_sram_size;
  357. int txq_count;
  358. struct tx_queue txq[8];
  359. /*
  360. * Hardware-specific parameters.
  361. */
  362. struct clk *clk;
  363. unsigned int t_clk;
  364. };
  365. /* port register accessors **************************************************/
  366. static inline u32 rdl(struct mv643xx_eth_private *mp, int offset)
  367. {
  368. return readl(mp->shared->base + offset);
  369. }
  370. static inline u32 rdlp(struct mv643xx_eth_private *mp, int offset)
  371. {
  372. return readl(mp->base + offset);
  373. }
  374. static inline void wrl(struct mv643xx_eth_private *mp, int offset, u32 data)
  375. {
  376. writel(data, mp->shared->base + offset);
  377. }
  378. static inline void wrlp(struct mv643xx_eth_private *mp, int offset, u32 data)
  379. {
  380. writel(data, mp->base + offset);
  381. }
  382. /* rxq/txq helper functions *************************************************/
  383. static struct mv643xx_eth_private *rxq_to_mp(struct rx_queue *rxq)
  384. {
  385. return container_of(rxq, struct mv643xx_eth_private, rxq[rxq->index]);
  386. }
  387. static struct mv643xx_eth_private *txq_to_mp(struct tx_queue *txq)
  388. {
  389. return container_of(txq, struct mv643xx_eth_private, txq[txq->index]);
  390. }
  391. static void rxq_enable(struct rx_queue *rxq)
  392. {
  393. struct mv643xx_eth_private *mp = rxq_to_mp(rxq);
  394. wrlp(mp, RXQ_COMMAND, 1 << rxq->index);
  395. }
  396. static void rxq_disable(struct rx_queue *rxq)
  397. {
  398. struct mv643xx_eth_private *mp = rxq_to_mp(rxq);
  399. u8 mask = 1 << rxq->index;
  400. wrlp(mp, RXQ_COMMAND, mask << 8);
  401. while (rdlp(mp, RXQ_COMMAND) & mask)
  402. udelay(10);
  403. }
  404. static void txq_reset_hw_ptr(struct tx_queue *txq)
  405. {
  406. struct mv643xx_eth_private *mp = txq_to_mp(txq);
  407. u32 addr;
  408. addr = (u32)txq->tx_desc_dma;
  409. addr += txq->tx_curr_desc * sizeof(struct tx_desc);
  410. wrlp(mp, TXQ_CURRENT_DESC_PTR(txq->index), addr);
  411. }
  412. static void txq_enable(struct tx_queue *txq)
  413. {
  414. struct mv643xx_eth_private *mp = txq_to_mp(txq);
  415. wrlp(mp, TXQ_COMMAND, 1 << txq->index);
  416. }
  417. static void txq_disable(struct tx_queue *txq)
  418. {
  419. struct mv643xx_eth_private *mp = txq_to_mp(txq);
  420. u8 mask = 1 << txq->index;
  421. wrlp(mp, TXQ_COMMAND, mask << 8);
  422. while (rdlp(mp, TXQ_COMMAND) & mask)
  423. udelay(10);
  424. }
  425. static void txq_maybe_wake(struct tx_queue *txq)
  426. {
  427. struct mv643xx_eth_private *mp = txq_to_mp(txq);
  428. struct netdev_queue *nq = netdev_get_tx_queue(mp->dev, txq->index);
  429. if (netif_tx_queue_stopped(nq)) {
  430. __netif_tx_lock(nq, smp_processor_id());
  431. if (txq->tx_desc_count <= txq->tx_wake_threshold)
  432. netif_tx_wake_queue(nq);
  433. __netif_tx_unlock(nq);
  434. }
  435. }
  436. static int rxq_process(struct rx_queue *rxq, int budget)
  437. {
  438. struct mv643xx_eth_private *mp = rxq_to_mp(rxq);
  439. struct net_device_stats *stats = &mp->dev->stats;
  440. int rx;
  441. rx = 0;
  442. while (rx < budget && rxq->rx_desc_count) {
  443. struct rx_desc *rx_desc;
  444. unsigned int cmd_sts;
  445. struct sk_buff *skb;
  446. u16 byte_cnt;
  447. rx_desc = &rxq->rx_desc_area[rxq->rx_curr_desc];
  448. cmd_sts = rx_desc->cmd_sts;
  449. if (cmd_sts & BUFFER_OWNED_BY_DMA)
  450. break;
  451. rmb();
  452. skb = rxq->rx_skb[rxq->rx_curr_desc];
  453. rxq->rx_skb[rxq->rx_curr_desc] = NULL;
  454. rxq->rx_curr_desc++;
  455. if (rxq->rx_curr_desc == rxq->rx_ring_size)
  456. rxq->rx_curr_desc = 0;
  457. dma_unmap_single(mp->dev->dev.parent, rx_desc->buf_ptr,
  458. rx_desc->buf_size, DMA_FROM_DEVICE);
  459. rxq->rx_desc_count--;
  460. rx++;
  461. mp->work_rx_refill |= 1 << rxq->index;
  462. byte_cnt = rx_desc->byte_cnt;
  463. /*
  464. * Update statistics.
  465. *
  466. * Note that the descriptor byte count includes 2 dummy
  467. * bytes automatically inserted by the hardware at the
  468. * start of the packet (which we don't count), and a 4
  469. * byte CRC at the end of the packet (which we do count).
  470. */
  471. stats->rx_packets++;
  472. stats->rx_bytes += byte_cnt - 2;
  473. /*
  474. * In case we received a packet without first / last bits
  475. * on, or the error summary bit is set, the packet needs
  476. * to be dropped.
  477. */
  478. if ((cmd_sts & (RX_FIRST_DESC | RX_LAST_DESC | ERROR_SUMMARY))
  479. != (RX_FIRST_DESC | RX_LAST_DESC))
  480. goto err;
  481. /*
  482. * The -4 is for the CRC in the trailer of the
  483. * received packet
  484. */
  485. skb_put(skb, byte_cnt - 2 - 4);
  486. if (cmd_sts & LAYER_4_CHECKSUM_OK)
  487. skb->ip_summed = CHECKSUM_UNNECESSARY;
  488. skb->protocol = eth_type_trans(skb, mp->dev);
  489. napi_gro_receive(&mp->napi, skb);
  490. continue;
  491. err:
  492. stats->rx_dropped++;
  493. if ((cmd_sts & (RX_FIRST_DESC | RX_LAST_DESC)) !=
  494. (RX_FIRST_DESC | RX_LAST_DESC)) {
  495. if (net_ratelimit())
  496. netdev_err(mp->dev,
  497. "received packet spanning multiple descriptors\n");
  498. }
  499. if (cmd_sts & ERROR_SUMMARY)
  500. stats->rx_errors++;
  501. dev_kfree_skb(skb);
  502. }
  503. if (rx < budget)
  504. mp->work_rx &= ~(1 << rxq->index);
  505. return rx;
  506. }
  507. static int rxq_refill(struct rx_queue *rxq, int budget)
  508. {
  509. struct mv643xx_eth_private *mp = rxq_to_mp(rxq);
  510. int refilled;
  511. refilled = 0;
  512. while (refilled < budget && rxq->rx_desc_count < rxq->rx_ring_size) {
  513. struct sk_buff *skb;
  514. int rx;
  515. struct rx_desc *rx_desc;
  516. int size;
  517. skb = netdev_alloc_skb(mp->dev, mp->skb_size);
  518. if (skb == NULL) {
  519. mp->oom = 1;
  520. goto oom;
  521. }
  522. if (SKB_DMA_REALIGN)
  523. skb_reserve(skb, SKB_DMA_REALIGN);
  524. refilled++;
  525. rxq->rx_desc_count++;
  526. rx = rxq->rx_used_desc++;
  527. if (rxq->rx_used_desc == rxq->rx_ring_size)
  528. rxq->rx_used_desc = 0;
  529. rx_desc = rxq->rx_desc_area + rx;
  530. size = skb_end_pointer(skb) - skb->data;
  531. rx_desc->buf_ptr = dma_map_single(mp->dev->dev.parent,
  532. skb->data, size,
  533. DMA_FROM_DEVICE);
  534. rx_desc->buf_size = size;
  535. rxq->rx_skb[rx] = skb;
  536. wmb();
  537. rx_desc->cmd_sts = BUFFER_OWNED_BY_DMA | RX_ENABLE_INTERRUPT;
  538. wmb();
  539. /*
  540. * The hardware automatically prepends 2 bytes of
  541. * dummy data to each received packet, so that the
  542. * IP header ends up 16-byte aligned.
  543. */
  544. skb_reserve(skb, 2);
  545. }
  546. if (refilled < budget)
  547. mp->work_rx_refill &= ~(1 << rxq->index);
  548. oom:
  549. return refilled;
  550. }
  551. /* tx ***********************************************************************/
  552. static inline unsigned int has_tiny_unaligned_frags(struct sk_buff *skb)
  553. {
  554. int frag;
  555. for (frag = 0; frag < skb_shinfo(skb)->nr_frags; frag++) {
  556. const skb_frag_t *fragp = &skb_shinfo(skb)->frags[frag];
  557. if (skb_frag_size(fragp) <= 8 && skb_frag_off(fragp) & 7)
  558. return 1;
  559. }
  560. return 0;
  561. }
  562. static int skb_tx_csum(struct mv643xx_eth_private *mp, struct sk_buff *skb,
  563. u16 *l4i_chk, u32 *command, int length)
  564. {
  565. int ret;
  566. u32 cmd = 0;
  567. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  568. int hdr_len;
  569. int tag_bytes;
  570. BUG_ON(skb->protocol != htons(ETH_P_IP) &&
  571. skb->protocol != htons(ETH_P_8021Q));
  572. hdr_len = (void *)ip_hdr(skb) - (void *)skb->data;
  573. tag_bytes = hdr_len - ETH_HLEN;
  574. if (length - hdr_len > mp->shared->tx_csum_limit ||
  575. unlikely(tag_bytes & ~12)) {
  576. ret = skb_checksum_help(skb);
  577. if (!ret)
  578. goto no_csum;
  579. return ret;
  580. }
  581. if (tag_bytes & 4)
  582. cmd |= MAC_HDR_EXTRA_4_BYTES;
  583. if (tag_bytes & 8)
  584. cmd |= MAC_HDR_EXTRA_8_BYTES;
  585. cmd |= GEN_TCP_UDP_CHECKSUM | GEN_TCP_UDP_CHK_FULL |
  586. GEN_IP_V4_CHECKSUM |
  587. ip_hdr(skb)->ihl << TX_IHL_SHIFT;
  588. /* TODO: Revisit this. With the usage of GEN_TCP_UDP_CHK_FULL
  589. * it seems we don't need to pass the initial checksum.
  590. */
  591. switch (ip_hdr(skb)->protocol) {
  592. case IPPROTO_UDP:
  593. cmd |= UDP_FRAME;
  594. *l4i_chk = 0;
  595. break;
  596. case IPPROTO_TCP:
  597. *l4i_chk = 0;
  598. break;
  599. default:
  600. WARN(1, "protocol not supported");
  601. }
  602. } else {
  603. no_csum:
  604. /* Errata BTS #50, IHL must be 5 if no HW checksum */
  605. cmd |= 5 << TX_IHL_SHIFT;
  606. }
  607. *command = cmd;
  608. return 0;
  609. }
  610. static inline int
  611. txq_put_data_tso(struct net_device *dev, struct tx_queue *txq,
  612. struct sk_buff *skb, char *data, int length,
  613. bool last_tcp, bool is_last)
  614. {
  615. int tx_index;
  616. u32 cmd_sts;
  617. struct tx_desc *desc;
  618. tx_index = txq->tx_curr_desc++;
  619. if (txq->tx_curr_desc == txq->tx_ring_size)
  620. txq->tx_curr_desc = 0;
  621. desc = &txq->tx_desc_area[tx_index];
  622. txq->tx_desc_mapping[tx_index] = DESC_DMA_MAP_SINGLE;
  623. desc->l4i_chk = 0;
  624. desc->byte_cnt = length;
  625. if (length <= 8 && (uintptr_t)data & 0x7) {
  626. /* Copy unaligned small data fragment to TSO header data area */
  627. memcpy(txq->tso_hdrs + tx_index * TSO_HEADER_SIZE,
  628. data, length);
  629. desc->buf_ptr = txq->tso_hdrs_dma
  630. + tx_index * TSO_HEADER_SIZE;
  631. } else {
  632. /* Alignment is okay, map buffer and hand off to hardware */
  633. txq->tx_desc_mapping[tx_index] = DESC_DMA_MAP_SINGLE;
  634. desc->buf_ptr = dma_map_single(dev->dev.parent, data,
  635. length, DMA_TO_DEVICE);
  636. if (unlikely(dma_mapping_error(dev->dev.parent,
  637. desc->buf_ptr))) {
  638. WARN(1, "dma_map_single failed!\n");
  639. return -ENOMEM;
  640. }
  641. }
  642. cmd_sts = BUFFER_OWNED_BY_DMA;
  643. if (last_tcp) {
  644. /* last descriptor in the TCP packet */
  645. cmd_sts |= ZERO_PADDING | TX_LAST_DESC;
  646. /* last descriptor in SKB */
  647. if (is_last)
  648. cmd_sts |= TX_ENABLE_INTERRUPT;
  649. }
  650. desc->cmd_sts = cmd_sts;
  651. return 0;
  652. }
  653. static inline void
  654. txq_put_hdr_tso(struct sk_buff *skb, struct tx_queue *txq, int length,
  655. u32 *first_cmd_sts, bool first_desc)
  656. {
  657. struct mv643xx_eth_private *mp = txq_to_mp(txq);
  658. int hdr_len = skb_tcp_all_headers(skb);
  659. int tx_index;
  660. struct tx_desc *desc;
  661. int ret;
  662. u32 cmd_csum = 0;
  663. u16 l4i_chk = 0;
  664. u32 cmd_sts;
  665. tx_index = txq->tx_curr_desc;
  666. desc = &txq->tx_desc_area[tx_index];
  667. ret = skb_tx_csum(mp, skb, &l4i_chk, &cmd_csum, length);
  668. if (ret)
  669. WARN(1, "failed to prepare checksum!");
  670. /* Should we set this? Can't use the value from skb_tx_csum()
  671. * as it's not the correct initial L4 checksum to use.
  672. */
  673. desc->l4i_chk = 0;
  674. desc->byte_cnt = hdr_len;
  675. desc->buf_ptr = txq->tso_hdrs_dma +
  676. txq->tx_curr_desc * TSO_HEADER_SIZE;
  677. cmd_sts = cmd_csum | BUFFER_OWNED_BY_DMA | TX_FIRST_DESC |
  678. GEN_CRC;
  679. /* Defer updating the first command descriptor until all
  680. * following descriptors have been written.
  681. */
  682. if (first_desc)
  683. *first_cmd_sts = cmd_sts;
  684. else
  685. desc->cmd_sts = cmd_sts;
  686. txq->tx_curr_desc++;
  687. if (txq->tx_curr_desc == txq->tx_ring_size)
  688. txq->tx_curr_desc = 0;
  689. }
  690. static int txq_submit_tso(struct tx_queue *txq, struct sk_buff *skb,
  691. struct net_device *dev)
  692. {
  693. struct mv643xx_eth_private *mp = txq_to_mp(txq);
  694. int hdr_len, total_len, data_left, ret;
  695. int desc_count = 0;
  696. struct tso_t tso;
  697. struct tx_desc *first_tx_desc;
  698. u32 first_cmd_sts = 0;
  699. /* Count needed descriptors */
  700. if ((txq->tx_desc_count + tso_count_descs(skb)) >= txq->tx_ring_size) {
  701. netdev_dbg(dev, "not enough descriptors for TSO!\n");
  702. return -EBUSY;
  703. }
  704. first_tx_desc = &txq->tx_desc_area[txq->tx_curr_desc];
  705. /* Initialize the TSO handler, and prepare the first payload */
  706. hdr_len = tso_start(skb, &tso);
  707. total_len = skb->len - hdr_len;
  708. while (total_len > 0) {
  709. bool first_desc = (desc_count == 0);
  710. char *hdr;
  711. data_left = min_t(int, skb_shinfo(skb)->gso_size, total_len);
  712. total_len -= data_left;
  713. desc_count++;
  714. /* prepare packet headers: MAC + IP + TCP */
  715. hdr = txq->tso_hdrs + txq->tx_curr_desc * TSO_HEADER_SIZE;
  716. tso_build_hdr(skb, hdr, &tso, data_left, total_len == 0);
  717. txq_put_hdr_tso(skb, txq, data_left, &first_cmd_sts,
  718. first_desc);
  719. while (data_left > 0) {
  720. int size;
  721. desc_count++;
  722. size = min_t(int, tso.size, data_left);
  723. ret = txq_put_data_tso(dev, txq, skb, tso.data, size,
  724. size == data_left,
  725. total_len == 0);
  726. if (ret)
  727. goto err_release;
  728. data_left -= size;
  729. tso_build_data(skb, &tso, size);
  730. }
  731. }
  732. __skb_queue_tail(&txq->tx_skb, skb);
  733. skb_tx_timestamp(skb);
  734. /* ensure all other descriptors are written before first cmd_sts */
  735. wmb();
  736. first_tx_desc->cmd_sts = first_cmd_sts;
  737. /* clear TX_END status */
  738. mp->work_tx_end &= ~(1 << txq->index);
  739. /* ensure all descriptors are written before poking hardware */
  740. wmb();
  741. txq_enable(txq);
  742. txq->tx_desc_count += desc_count;
  743. return 0;
  744. err_release:
  745. /* TODO: Release all used data descriptors; header descriptors must not
  746. * be DMA-unmapped.
  747. */
  748. return ret;
  749. }
  750. static void txq_submit_frag_skb(struct tx_queue *txq, struct sk_buff *skb)
  751. {
  752. struct mv643xx_eth_private *mp = txq_to_mp(txq);
  753. int nr_frags = skb_shinfo(skb)->nr_frags;
  754. int frag;
  755. for (frag = 0; frag < nr_frags; frag++) {
  756. skb_frag_t *this_frag;
  757. int tx_index;
  758. struct tx_desc *desc;
  759. this_frag = &skb_shinfo(skb)->frags[frag];
  760. tx_index = txq->tx_curr_desc++;
  761. if (txq->tx_curr_desc == txq->tx_ring_size)
  762. txq->tx_curr_desc = 0;
  763. desc = &txq->tx_desc_area[tx_index];
  764. txq->tx_desc_mapping[tx_index] = DESC_DMA_MAP_PAGE;
  765. /*
  766. * The last fragment will generate an interrupt
  767. * which will free the skb on TX completion.
  768. */
  769. if (frag == nr_frags - 1) {
  770. desc->cmd_sts = BUFFER_OWNED_BY_DMA |
  771. ZERO_PADDING | TX_LAST_DESC |
  772. TX_ENABLE_INTERRUPT;
  773. } else {
  774. desc->cmd_sts = BUFFER_OWNED_BY_DMA;
  775. }
  776. desc->l4i_chk = 0;
  777. desc->byte_cnt = skb_frag_size(this_frag);
  778. desc->buf_ptr = skb_frag_dma_map(mp->dev->dev.parent,
  779. this_frag, 0, desc->byte_cnt,
  780. DMA_TO_DEVICE);
  781. }
  782. }
  783. static int txq_submit_skb(struct tx_queue *txq, struct sk_buff *skb,
  784. struct net_device *dev)
  785. {
  786. struct mv643xx_eth_private *mp = txq_to_mp(txq);
  787. int nr_frags = skb_shinfo(skb)->nr_frags;
  788. int tx_index;
  789. struct tx_desc *desc;
  790. u32 cmd_sts;
  791. u16 l4i_chk;
  792. int length, ret;
  793. cmd_sts = 0;
  794. l4i_chk = 0;
  795. if (txq->tx_ring_size - txq->tx_desc_count < MAX_SKB_FRAGS + 1) {
  796. if (net_ratelimit())
  797. netdev_err(dev, "tx queue full?!\n");
  798. return -EBUSY;
  799. }
  800. ret = skb_tx_csum(mp, skb, &l4i_chk, &cmd_sts, skb->len);
  801. if (ret)
  802. return ret;
  803. cmd_sts |= TX_FIRST_DESC | GEN_CRC | BUFFER_OWNED_BY_DMA;
  804. tx_index = txq->tx_curr_desc++;
  805. if (txq->tx_curr_desc == txq->tx_ring_size)
  806. txq->tx_curr_desc = 0;
  807. desc = &txq->tx_desc_area[tx_index];
  808. txq->tx_desc_mapping[tx_index] = DESC_DMA_MAP_SINGLE;
  809. if (nr_frags) {
  810. txq_submit_frag_skb(txq, skb);
  811. length = skb_headlen(skb);
  812. } else {
  813. cmd_sts |= ZERO_PADDING | TX_LAST_DESC | TX_ENABLE_INTERRUPT;
  814. length = skb->len;
  815. }
  816. desc->l4i_chk = l4i_chk;
  817. desc->byte_cnt = length;
  818. desc->buf_ptr = dma_map_single(mp->dev->dev.parent, skb->data,
  819. length, DMA_TO_DEVICE);
  820. __skb_queue_tail(&txq->tx_skb, skb);
  821. skb_tx_timestamp(skb);
  822. /* ensure all other descriptors are written before first cmd_sts */
  823. wmb();
  824. desc->cmd_sts = cmd_sts;
  825. /* clear TX_END status */
  826. mp->work_tx_end &= ~(1 << txq->index);
  827. /* ensure all descriptors are written before poking hardware */
  828. wmb();
  829. txq_enable(txq);
  830. txq->tx_desc_count += nr_frags + 1;
  831. return 0;
  832. }
  833. static netdev_tx_t mv643xx_eth_xmit(struct sk_buff *skb, struct net_device *dev)
  834. {
  835. struct mv643xx_eth_private *mp = netdev_priv(dev);
  836. int length, queue, ret;
  837. struct tx_queue *txq;
  838. struct netdev_queue *nq;
  839. queue = skb_get_queue_mapping(skb);
  840. txq = mp->txq + queue;
  841. nq = netdev_get_tx_queue(dev, queue);
  842. if (has_tiny_unaligned_frags(skb) && __skb_linearize(skb)) {
  843. netdev_printk(KERN_DEBUG, dev,
  844. "failed to linearize skb with tiny unaligned fragment\n");
  845. return NETDEV_TX_BUSY;
  846. }
  847. length = skb->len;
  848. if (skb_is_gso(skb))
  849. ret = txq_submit_tso(txq, skb, dev);
  850. else
  851. ret = txq_submit_skb(txq, skb, dev);
  852. if (!ret) {
  853. txq->tx_bytes += length;
  854. txq->tx_packets++;
  855. if (txq->tx_desc_count >= txq->tx_stop_threshold)
  856. netif_tx_stop_queue(nq);
  857. } else {
  858. txq->tx_dropped++;
  859. dev_kfree_skb_any(skb);
  860. }
  861. return NETDEV_TX_OK;
  862. }
  863. /* tx napi ******************************************************************/
  864. static void txq_kick(struct tx_queue *txq)
  865. {
  866. struct mv643xx_eth_private *mp = txq_to_mp(txq);
  867. struct netdev_queue *nq = netdev_get_tx_queue(mp->dev, txq->index);
  868. u32 hw_desc_ptr;
  869. u32 expected_ptr;
  870. __netif_tx_lock(nq, smp_processor_id());
  871. if (rdlp(mp, TXQ_COMMAND) & (1 << txq->index))
  872. goto out;
  873. hw_desc_ptr = rdlp(mp, TXQ_CURRENT_DESC_PTR(txq->index));
  874. expected_ptr = (u32)txq->tx_desc_dma +
  875. txq->tx_curr_desc * sizeof(struct tx_desc);
  876. if (hw_desc_ptr != expected_ptr)
  877. txq_enable(txq);
  878. out:
  879. __netif_tx_unlock(nq);
  880. mp->work_tx_end &= ~(1 << txq->index);
  881. }
  882. static int txq_reclaim(struct tx_queue *txq, int budget, int force)
  883. {
  884. struct mv643xx_eth_private *mp = txq_to_mp(txq);
  885. struct netdev_queue *nq = netdev_get_tx_queue(mp->dev, txq->index);
  886. int reclaimed;
  887. __netif_tx_lock_bh(nq);
  888. reclaimed = 0;
  889. while (reclaimed < budget && txq->tx_desc_count > 0) {
  890. int tx_index;
  891. struct tx_desc *desc;
  892. u32 cmd_sts;
  893. char desc_dma_map;
  894. tx_index = txq->tx_used_desc;
  895. desc = &txq->tx_desc_area[tx_index];
  896. desc_dma_map = txq->tx_desc_mapping[tx_index];
  897. cmd_sts = desc->cmd_sts;
  898. if (cmd_sts & BUFFER_OWNED_BY_DMA) {
  899. if (!force)
  900. break;
  901. desc->cmd_sts = cmd_sts & ~BUFFER_OWNED_BY_DMA;
  902. }
  903. txq->tx_used_desc = tx_index + 1;
  904. if (txq->tx_used_desc == txq->tx_ring_size)
  905. txq->tx_used_desc = 0;
  906. reclaimed++;
  907. txq->tx_desc_count--;
  908. if (!IS_TSO_HEADER(txq, desc->buf_ptr)) {
  909. if (desc_dma_map == DESC_DMA_MAP_PAGE)
  910. dma_unmap_page(mp->dev->dev.parent,
  911. desc->buf_ptr,
  912. desc->byte_cnt,
  913. DMA_TO_DEVICE);
  914. else
  915. dma_unmap_single(mp->dev->dev.parent,
  916. desc->buf_ptr,
  917. desc->byte_cnt,
  918. DMA_TO_DEVICE);
  919. }
  920. if (cmd_sts & TX_ENABLE_INTERRUPT) {
  921. struct sk_buff *skb = __skb_dequeue(&txq->tx_skb);
  922. if (!WARN_ON(!skb))
  923. dev_consume_skb_any(skb);
  924. }
  925. if (cmd_sts & ERROR_SUMMARY) {
  926. netdev_info(mp->dev, "tx error\n");
  927. mp->dev->stats.tx_errors++;
  928. }
  929. }
  930. __netif_tx_unlock_bh(nq);
  931. if (reclaimed < budget)
  932. mp->work_tx &= ~(1 << txq->index);
  933. return reclaimed;
  934. }
  935. /* tx rate control **********************************************************/
  936. /*
  937. * Set total maximum TX rate (shared by all TX queues for this port)
  938. * to 'rate' bits per second, with a maximum burst of 'burst' bytes.
  939. */
  940. static void tx_set_rate(struct mv643xx_eth_private *mp, int rate, int burst)
  941. {
  942. int token_rate;
  943. int mtu;
  944. int bucket_size;
  945. token_rate = ((rate / 1000) * 64) / (mp->t_clk / 1000);
  946. if (token_rate > 1023)
  947. token_rate = 1023;
  948. mtu = (mp->dev->mtu + 255) >> 8;
  949. if (mtu > 63)
  950. mtu = 63;
  951. bucket_size = (burst + 255) >> 8;
  952. if (bucket_size > 65535)
  953. bucket_size = 65535;
  954. switch (mp->shared->tx_bw_control) {
  955. case TX_BW_CONTROL_OLD_LAYOUT:
  956. wrlp(mp, TX_BW_RATE, token_rate);
  957. wrlp(mp, TX_BW_MTU, mtu);
  958. wrlp(mp, TX_BW_BURST, bucket_size);
  959. break;
  960. case TX_BW_CONTROL_NEW_LAYOUT:
  961. wrlp(mp, TX_BW_RATE_MOVED, token_rate);
  962. wrlp(mp, TX_BW_MTU_MOVED, mtu);
  963. wrlp(mp, TX_BW_BURST_MOVED, bucket_size);
  964. break;
  965. }
  966. }
  967. static void txq_set_rate(struct tx_queue *txq, int rate, int burst)
  968. {
  969. struct mv643xx_eth_private *mp = txq_to_mp(txq);
  970. int token_rate;
  971. int bucket_size;
  972. token_rate = ((rate / 1000) * 64) / (mp->t_clk / 1000);
  973. if (token_rate > 1023)
  974. token_rate = 1023;
  975. bucket_size = (burst + 255) >> 8;
  976. if (bucket_size > 65535)
  977. bucket_size = 65535;
  978. wrlp(mp, TXQ_BW_TOKENS(txq->index), token_rate << 14);
  979. wrlp(mp, TXQ_BW_CONF(txq->index), (bucket_size << 10) | token_rate);
  980. }
  981. static void txq_set_fixed_prio_mode(struct tx_queue *txq)
  982. {
  983. struct mv643xx_eth_private *mp = txq_to_mp(txq);
  984. int off;
  985. u32 val;
  986. /*
  987. * Turn on fixed priority mode.
  988. */
  989. off = 0;
  990. switch (mp->shared->tx_bw_control) {
  991. case TX_BW_CONTROL_OLD_LAYOUT:
  992. off = TXQ_FIX_PRIO_CONF;
  993. break;
  994. case TX_BW_CONTROL_NEW_LAYOUT:
  995. off = TXQ_FIX_PRIO_CONF_MOVED;
  996. break;
  997. }
  998. if (off) {
  999. val = rdlp(mp, off);
  1000. val |= 1 << txq->index;
  1001. wrlp(mp, off, val);
  1002. }
  1003. }
  1004. /* mii management interface *************************************************/
  1005. static void mv643xx_eth_adjust_link(struct net_device *dev)
  1006. {
  1007. struct mv643xx_eth_private *mp = netdev_priv(dev);
  1008. u32 pscr = rdlp(mp, PORT_SERIAL_CONTROL);
  1009. u32 autoneg_disable = FORCE_LINK_PASS |
  1010. DISABLE_AUTO_NEG_SPEED_GMII |
  1011. DISABLE_AUTO_NEG_FOR_FLOW_CTRL |
  1012. DISABLE_AUTO_NEG_FOR_DUPLEX;
  1013. if (dev->phydev->autoneg == AUTONEG_ENABLE) {
  1014. /* enable auto negotiation */
  1015. pscr &= ~autoneg_disable;
  1016. goto out_write;
  1017. }
  1018. pscr |= autoneg_disable;
  1019. if (dev->phydev->speed == SPEED_1000) {
  1020. /* force gigabit, half duplex not supported */
  1021. pscr |= SET_GMII_SPEED_TO_1000;
  1022. pscr |= SET_FULL_DUPLEX_MODE;
  1023. goto out_write;
  1024. }
  1025. pscr &= ~SET_GMII_SPEED_TO_1000;
  1026. if (dev->phydev->speed == SPEED_100)
  1027. pscr |= SET_MII_SPEED_TO_100;
  1028. else
  1029. pscr &= ~SET_MII_SPEED_TO_100;
  1030. if (dev->phydev->duplex == DUPLEX_FULL)
  1031. pscr |= SET_FULL_DUPLEX_MODE;
  1032. else
  1033. pscr &= ~SET_FULL_DUPLEX_MODE;
  1034. out_write:
  1035. wrlp(mp, PORT_SERIAL_CONTROL, pscr);
  1036. }
  1037. /* statistics ***************************************************************/
  1038. static struct net_device_stats *mv643xx_eth_get_stats(struct net_device *dev)
  1039. {
  1040. struct mv643xx_eth_private *mp = netdev_priv(dev);
  1041. struct net_device_stats *stats = &dev->stats;
  1042. unsigned long tx_packets = 0;
  1043. unsigned long tx_bytes = 0;
  1044. unsigned long tx_dropped = 0;
  1045. int i;
  1046. for (i = 0; i < mp->txq_count; i++) {
  1047. struct tx_queue *txq = mp->txq + i;
  1048. tx_packets += txq->tx_packets;
  1049. tx_bytes += txq->tx_bytes;
  1050. tx_dropped += txq->tx_dropped;
  1051. }
  1052. stats->tx_packets = tx_packets;
  1053. stats->tx_bytes = tx_bytes;
  1054. stats->tx_dropped = tx_dropped;
  1055. return stats;
  1056. }
  1057. static inline u32 mib_read(struct mv643xx_eth_private *mp, int offset)
  1058. {
  1059. return rdl(mp, MIB_COUNTERS(mp->port_num) + offset);
  1060. }
  1061. static void mib_counters_clear(struct mv643xx_eth_private *mp)
  1062. {
  1063. int i;
  1064. for (i = 0; i < 0x80; i += 4)
  1065. mib_read(mp, i);
  1066. /* Clear non MIB hw counters also */
  1067. rdlp(mp, RX_DISCARD_FRAME_CNT);
  1068. rdlp(mp, RX_OVERRUN_FRAME_CNT);
  1069. }
  1070. static void mib_counters_update(struct mv643xx_eth_private *mp)
  1071. {
  1072. struct mib_counters *p = &mp->mib_counters;
  1073. spin_lock_bh(&mp->mib_counters_lock);
  1074. p->good_octets_received += mib_read(mp, 0x00);
  1075. p->bad_octets_received += mib_read(mp, 0x08);
  1076. p->internal_mac_transmit_err += mib_read(mp, 0x0c);
  1077. p->good_frames_received += mib_read(mp, 0x10);
  1078. p->bad_frames_received += mib_read(mp, 0x14);
  1079. p->broadcast_frames_received += mib_read(mp, 0x18);
  1080. p->multicast_frames_received += mib_read(mp, 0x1c);
  1081. p->frames_64_octets += mib_read(mp, 0x20);
  1082. p->frames_65_to_127_octets += mib_read(mp, 0x24);
  1083. p->frames_128_to_255_octets += mib_read(mp, 0x28);
  1084. p->frames_256_to_511_octets += mib_read(mp, 0x2c);
  1085. p->frames_512_to_1023_octets += mib_read(mp, 0x30);
  1086. p->frames_1024_to_max_octets += mib_read(mp, 0x34);
  1087. p->good_octets_sent += mib_read(mp, 0x38);
  1088. p->good_frames_sent += mib_read(mp, 0x40);
  1089. p->excessive_collision += mib_read(mp, 0x44);
  1090. p->multicast_frames_sent += mib_read(mp, 0x48);
  1091. p->broadcast_frames_sent += mib_read(mp, 0x4c);
  1092. p->unrec_mac_control_received += mib_read(mp, 0x50);
  1093. p->fc_sent += mib_read(mp, 0x54);
  1094. p->good_fc_received += mib_read(mp, 0x58);
  1095. p->bad_fc_received += mib_read(mp, 0x5c);
  1096. p->undersize_received += mib_read(mp, 0x60);
  1097. p->fragments_received += mib_read(mp, 0x64);
  1098. p->oversize_received += mib_read(mp, 0x68);
  1099. p->jabber_received += mib_read(mp, 0x6c);
  1100. p->mac_receive_error += mib_read(mp, 0x70);
  1101. p->bad_crc_event += mib_read(mp, 0x74);
  1102. p->collision += mib_read(mp, 0x78);
  1103. p->late_collision += mib_read(mp, 0x7c);
  1104. /* Non MIB hardware counters */
  1105. p->rx_discard += rdlp(mp, RX_DISCARD_FRAME_CNT);
  1106. p->rx_overrun += rdlp(mp, RX_OVERRUN_FRAME_CNT);
  1107. spin_unlock_bh(&mp->mib_counters_lock);
  1108. }
  1109. static void mib_counters_timer_wrapper(struct timer_list *t)
  1110. {
  1111. struct mv643xx_eth_private *mp = timer_container_of(mp, t,
  1112. mib_counters_timer);
  1113. mib_counters_update(mp);
  1114. mod_timer(&mp->mib_counters_timer, jiffies + 30 * HZ);
  1115. }
  1116. /* interrupt coalescing *****************************************************/
  1117. /*
  1118. * Hardware coalescing parameters are set in units of 64 t_clk
  1119. * cycles. I.e.:
  1120. *
  1121. * coal_delay_in_usec = 64000000 * register_value / t_clk_rate
  1122. *
  1123. * register_value = coal_delay_in_usec * t_clk_rate / 64000000
  1124. *
  1125. * In the ->set*() methods, we round the computed register value
  1126. * to the nearest integer.
  1127. */
  1128. static unsigned int get_rx_coal(struct mv643xx_eth_private *mp)
  1129. {
  1130. u32 val = rdlp(mp, SDMA_CONFIG);
  1131. u64 temp;
  1132. if (mp->shared->extended_rx_coal_limit)
  1133. temp = ((val & 0x02000000) >> 10) | ((val & 0x003fff80) >> 7);
  1134. else
  1135. temp = (val & 0x003fff00) >> 8;
  1136. temp *= 64000000;
  1137. temp += mp->t_clk / 2;
  1138. do_div(temp, mp->t_clk);
  1139. return (unsigned int)temp;
  1140. }
  1141. static void set_rx_coal(struct mv643xx_eth_private *mp, unsigned int usec)
  1142. {
  1143. u64 temp;
  1144. u32 val;
  1145. temp = (u64)usec * mp->t_clk;
  1146. temp += 31999999;
  1147. do_div(temp, 64000000);
  1148. val = rdlp(mp, SDMA_CONFIG);
  1149. if (mp->shared->extended_rx_coal_limit) {
  1150. if (temp > 0xffff)
  1151. temp = 0xffff;
  1152. val &= ~0x023fff80;
  1153. val |= (temp & 0x8000) << 10;
  1154. val |= (temp & 0x7fff) << 7;
  1155. } else {
  1156. if (temp > 0x3fff)
  1157. temp = 0x3fff;
  1158. val &= ~0x003fff00;
  1159. val |= (temp & 0x3fff) << 8;
  1160. }
  1161. wrlp(mp, SDMA_CONFIG, val);
  1162. }
  1163. static unsigned int get_tx_coal(struct mv643xx_eth_private *mp)
  1164. {
  1165. u64 temp;
  1166. temp = (rdlp(mp, TX_FIFO_URGENT_THRESHOLD) & 0x3fff0) >> 4;
  1167. temp *= 64000000;
  1168. temp += mp->t_clk / 2;
  1169. do_div(temp, mp->t_clk);
  1170. return (unsigned int)temp;
  1171. }
  1172. static void set_tx_coal(struct mv643xx_eth_private *mp, unsigned int usec)
  1173. {
  1174. u64 temp;
  1175. temp = (u64)usec * mp->t_clk;
  1176. temp += 31999999;
  1177. do_div(temp, 64000000);
  1178. if (temp > 0x3fff)
  1179. temp = 0x3fff;
  1180. wrlp(mp, TX_FIFO_URGENT_THRESHOLD, temp << 4);
  1181. }
  1182. /* ethtool ******************************************************************/
  1183. struct mv643xx_eth_stats {
  1184. char stat_string[ETH_GSTRING_LEN];
  1185. int sizeof_stat;
  1186. int netdev_off;
  1187. int mp_off;
  1188. };
  1189. #define SSTAT(m) \
  1190. { #m, sizeof_field(struct net_device_stats, m), \
  1191. offsetof(struct net_device, stats.m), -1 }
  1192. #define MIBSTAT(m) \
  1193. { #m, sizeof_field(struct mib_counters, m), \
  1194. -1, offsetof(struct mv643xx_eth_private, mib_counters.m) }
  1195. static const struct mv643xx_eth_stats mv643xx_eth_stats[] = {
  1196. SSTAT(rx_packets),
  1197. SSTAT(tx_packets),
  1198. SSTAT(rx_bytes),
  1199. SSTAT(tx_bytes),
  1200. SSTAT(rx_errors),
  1201. SSTAT(tx_errors),
  1202. SSTAT(rx_dropped),
  1203. SSTAT(tx_dropped),
  1204. MIBSTAT(good_octets_received),
  1205. MIBSTAT(bad_octets_received),
  1206. MIBSTAT(internal_mac_transmit_err),
  1207. MIBSTAT(good_frames_received),
  1208. MIBSTAT(bad_frames_received),
  1209. MIBSTAT(broadcast_frames_received),
  1210. MIBSTAT(multicast_frames_received),
  1211. MIBSTAT(frames_64_octets),
  1212. MIBSTAT(frames_65_to_127_octets),
  1213. MIBSTAT(frames_128_to_255_octets),
  1214. MIBSTAT(frames_256_to_511_octets),
  1215. MIBSTAT(frames_512_to_1023_octets),
  1216. MIBSTAT(frames_1024_to_max_octets),
  1217. MIBSTAT(good_octets_sent),
  1218. MIBSTAT(good_frames_sent),
  1219. MIBSTAT(excessive_collision),
  1220. MIBSTAT(multicast_frames_sent),
  1221. MIBSTAT(broadcast_frames_sent),
  1222. MIBSTAT(unrec_mac_control_received),
  1223. MIBSTAT(fc_sent),
  1224. MIBSTAT(good_fc_received),
  1225. MIBSTAT(bad_fc_received),
  1226. MIBSTAT(undersize_received),
  1227. MIBSTAT(fragments_received),
  1228. MIBSTAT(oversize_received),
  1229. MIBSTAT(jabber_received),
  1230. MIBSTAT(mac_receive_error),
  1231. MIBSTAT(bad_crc_event),
  1232. MIBSTAT(collision),
  1233. MIBSTAT(late_collision),
  1234. MIBSTAT(rx_discard),
  1235. MIBSTAT(rx_overrun),
  1236. };
  1237. static int
  1238. mv643xx_eth_get_link_ksettings_phy(struct mv643xx_eth_private *mp,
  1239. struct ethtool_link_ksettings *cmd)
  1240. {
  1241. struct net_device *dev = mp->dev;
  1242. phy_ethtool_ksettings_get(dev->phydev, cmd);
  1243. /*
  1244. * The MAC does not support 1000baseT_Half.
  1245. */
  1246. linkmode_clear_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT,
  1247. cmd->link_modes.supported);
  1248. linkmode_clear_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT,
  1249. cmd->link_modes.advertising);
  1250. return 0;
  1251. }
  1252. static int
  1253. mv643xx_eth_get_link_ksettings_phyless(struct mv643xx_eth_private *mp,
  1254. struct ethtool_link_ksettings *cmd)
  1255. {
  1256. u32 port_status;
  1257. u32 supported, advertising;
  1258. port_status = rdlp(mp, PORT_STATUS);
  1259. supported = SUPPORTED_MII;
  1260. advertising = ADVERTISED_MII;
  1261. switch (port_status & PORT_SPEED_MASK) {
  1262. case PORT_SPEED_10:
  1263. cmd->base.speed = SPEED_10;
  1264. break;
  1265. case PORT_SPEED_100:
  1266. cmd->base.speed = SPEED_100;
  1267. break;
  1268. case PORT_SPEED_1000:
  1269. cmd->base.speed = SPEED_1000;
  1270. break;
  1271. default:
  1272. cmd->base.speed = -1;
  1273. break;
  1274. }
  1275. cmd->base.duplex = (port_status & FULL_DUPLEX) ?
  1276. DUPLEX_FULL : DUPLEX_HALF;
  1277. cmd->base.port = PORT_MII;
  1278. cmd->base.phy_address = 0;
  1279. cmd->base.autoneg = AUTONEG_DISABLE;
  1280. ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
  1281. supported);
  1282. ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
  1283. advertising);
  1284. return 0;
  1285. }
  1286. static void
  1287. mv643xx_eth_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
  1288. {
  1289. wol->supported = 0;
  1290. wol->wolopts = 0;
  1291. if (dev->phydev)
  1292. phy_ethtool_get_wol(dev->phydev, wol);
  1293. }
  1294. static int
  1295. mv643xx_eth_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
  1296. {
  1297. int err;
  1298. if (!dev->phydev)
  1299. return -EOPNOTSUPP;
  1300. err = phy_ethtool_set_wol(dev->phydev, wol);
  1301. /* Given that mv643xx_eth works without the marvell-specific PHY driver,
  1302. * this debugging hint is useful to have.
  1303. */
  1304. if (err == -EOPNOTSUPP)
  1305. netdev_info(dev, "The PHY does not support set_wol, was CONFIG_MARVELL_PHY enabled?\n");
  1306. return err;
  1307. }
  1308. static int
  1309. mv643xx_eth_get_link_ksettings(struct net_device *dev,
  1310. struct ethtool_link_ksettings *cmd)
  1311. {
  1312. struct mv643xx_eth_private *mp = netdev_priv(dev);
  1313. if (dev->phydev)
  1314. return mv643xx_eth_get_link_ksettings_phy(mp, cmd);
  1315. else
  1316. return mv643xx_eth_get_link_ksettings_phyless(mp, cmd);
  1317. }
  1318. static int
  1319. mv643xx_eth_set_link_ksettings(struct net_device *dev,
  1320. const struct ethtool_link_ksettings *cmd)
  1321. {
  1322. struct ethtool_link_ksettings c = *cmd;
  1323. u32 advertising;
  1324. int ret;
  1325. if (!dev->phydev)
  1326. return -EINVAL;
  1327. /*
  1328. * The MAC does not support 1000baseT_Half.
  1329. */
  1330. ethtool_convert_link_mode_to_legacy_u32(&advertising,
  1331. c.link_modes.advertising);
  1332. advertising &= ~ADVERTISED_1000baseT_Half;
  1333. ethtool_convert_legacy_u32_to_link_mode(c.link_modes.advertising,
  1334. advertising);
  1335. ret = phy_ethtool_ksettings_set(dev->phydev, &c);
  1336. if (!ret)
  1337. mv643xx_eth_adjust_link(dev);
  1338. return ret;
  1339. }
  1340. static void mv643xx_eth_get_drvinfo(struct net_device *dev,
  1341. struct ethtool_drvinfo *drvinfo)
  1342. {
  1343. strscpy(drvinfo->driver, mv643xx_eth_driver_name,
  1344. sizeof(drvinfo->driver));
  1345. strscpy(drvinfo->version, mv643xx_eth_driver_version,
  1346. sizeof(drvinfo->version));
  1347. strscpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
  1348. strscpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
  1349. }
  1350. static int mv643xx_eth_get_coalesce(struct net_device *dev,
  1351. struct ethtool_coalesce *ec,
  1352. struct kernel_ethtool_coalesce *kernel_coal,
  1353. struct netlink_ext_ack *extack)
  1354. {
  1355. struct mv643xx_eth_private *mp = netdev_priv(dev);
  1356. ec->rx_coalesce_usecs = get_rx_coal(mp);
  1357. ec->tx_coalesce_usecs = get_tx_coal(mp);
  1358. return 0;
  1359. }
  1360. static int mv643xx_eth_set_coalesce(struct net_device *dev,
  1361. struct ethtool_coalesce *ec,
  1362. struct kernel_ethtool_coalesce *kernel_coal,
  1363. struct netlink_ext_ack *extack)
  1364. {
  1365. struct mv643xx_eth_private *mp = netdev_priv(dev);
  1366. set_rx_coal(mp, ec->rx_coalesce_usecs);
  1367. set_tx_coal(mp, ec->tx_coalesce_usecs);
  1368. return 0;
  1369. }
  1370. static void
  1371. mv643xx_eth_get_ringparam(struct net_device *dev, struct ethtool_ringparam *er,
  1372. struct kernel_ethtool_ringparam *kernel_er,
  1373. struct netlink_ext_ack *extack)
  1374. {
  1375. struct mv643xx_eth_private *mp = netdev_priv(dev);
  1376. er->rx_max_pending = 4096;
  1377. er->tx_max_pending = 4096;
  1378. er->rx_pending = mp->rx_ring_size;
  1379. er->tx_pending = mp->tx_ring_size;
  1380. }
  1381. static int
  1382. mv643xx_eth_set_ringparam(struct net_device *dev, struct ethtool_ringparam *er,
  1383. struct kernel_ethtool_ringparam *kernel_er,
  1384. struct netlink_ext_ack *extack)
  1385. {
  1386. struct mv643xx_eth_private *mp = netdev_priv(dev);
  1387. if (er->rx_mini_pending || er->rx_jumbo_pending)
  1388. return -EINVAL;
  1389. mp->rx_ring_size = min(er->rx_pending, 4096U);
  1390. mp->tx_ring_size = clamp_t(unsigned int, er->tx_pending,
  1391. MV643XX_MAX_SKB_DESCS * 2, 4096);
  1392. if (mp->tx_ring_size != er->tx_pending)
  1393. netdev_warn(dev, "TX queue size set to %u (requested %u)\n",
  1394. mp->tx_ring_size, er->tx_pending);
  1395. if (netif_running(dev)) {
  1396. mv643xx_eth_stop(dev);
  1397. if (mv643xx_eth_open(dev)) {
  1398. netdev_err(dev,
  1399. "fatal error on re-opening device after ring param change\n");
  1400. return -ENOMEM;
  1401. }
  1402. }
  1403. return 0;
  1404. }
  1405. static int
  1406. mv643xx_eth_set_features(struct net_device *dev, netdev_features_t features)
  1407. {
  1408. struct mv643xx_eth_private *mp = netdev_priv(dev);
  1409. bool rx_csum = features & NETIF_F_RXCSUM;
  1410. wrlp(mp, PORT_CONFIG, rx_csum ? 0x02000000 : 0x00000000);
  1411. return 0;
  1412. }
  1413. static void mv643xx_eth_get_strings(struct net_device *dev,
  1414. uint32_t stringset, uint8_t *data)
  1415. {
  1416. int i;
  1417. if (stringset == ETH_SS_STATS)
  1418. for (i = 0; i < ARRAY_SIZE(mv643xx_eth_stats); i++)
  1419. ethtool_puts(&data, mv643xx_eth_stats[i].stat_string);
  1420. }
  1421. static void mv643xx_eth_get_ethtool_stats(struct net_device *dev,
  1422. struct ethtool_stats *stats,
  1423. uint64_t *data)
  1424. {
  1425. struct mv643xx_eth_private *mp = netdev_priv(dev);
  1426. int i;
  1427. mv643xx_eth_get_stats(dev);
  1428. mib_counters_update(mp);
  1429. for (i = 0; i < ARRAY_SIZE(mv643xx_eth_stats); i++) {
  1430. const struct mv643xx_eth_stats *stat;
  1431. void *p;
  1432. stat = mv643xx_eth_stats + i;
  1433. if (stat->netdev_off >= 0)
  1434. p = ((void *)mp->dev) + stat->netdev_off;
  1435. else
  1436. p = ((void *)mp) + stat->mp_off;
  1437. data[i] = (stat->sizeof_stat == 8) ?
  1438. *(uint64_t *)p : *(uint32_t *)p;
  1439. }
  1440. }
  1441. static int mv643xx_eth_get_sset_count(struct net_device *dev, int sset)
  1442. {
  1443. if (sset == ETH_SS_STATS)
  1444. return ARRAY_SIZE(mv643xx_eth_stats);
  1445. return -EOPNOTSUPP;
  1446. }
  1447. static const struct ethtool_ops mv643xx_eth_ethtool_ops = {
  1448. .supported_coalesce_params = ETHTOOL_COALESCE_USECS,
  1449. .get_drvinfo = mv643xx_eth_get_drvinfo,
  1450. .nway_reset = phy_ethtool_nway_reset,
  1451. .get_link = ethtool_op_get_link,
  1452. .get_coalesce = mv643xx_eth_get_coalesce,
  1453. .set_coalesce = mv643xx_eth_set_coalesce,
  1454. .get_ringparam = mv643xx_eth_get_ringparam,
  1455. .set_ringparam = mv643xx_eth_set_ringparam,
  1456. .get_strings = mv643xx_eth_get_strings,
  1457. .get_ethtool_stats = mv643xx_eth_get_ethtool_stats,
  1458. .get_sset_count = mv643xx_eth_get_sset_count,
  1459. .get_ts_info = ethtool_op_get_ts_info,
  1460. .get_wol = mv643xx_eth_get_wol,
  1461. .set_wol = mv643xx_eth_set_wol,
  1462. .get_link_ksettings = mv643xx_eth_get_link_ksettings,
  1463. .set_link_ksettings = mv643xx_eth_set_link_ksettings,
  1464. };
  1465. /* address handling *********************************************************/
  1466. static void uc_addr_get(struct mv643xx_eth_private *mp, unsigned char *addr)
  1467. {
  1468. unsigned int mac_h = rdlp(mp, MAC_ADDR_HIGH);
  1469. unsigned int mac_l = rdlp(mp, MAC_ADDR_LOW);
  1470. addr[0] = (mac_h >> 24) & 0xff;
  1471. addr[1] = (mac_h >> 16) & 0xff;
  1472. addr[2] = (mac_h >> 8) & 0xff;
  1473. addr[3] = mac_h & 0xff;
  1474. addr[4] = (mac_l >> 8) & 0xff;
  1475. addr[5] = mac_l & 0xff;
  1476. }
  1477. static void uc_addr_set(struct mv643xx_eth_private *mp, const u8 *addr)
  1478. {
  1479. wrlp(mp, MAC_ADDR_HIGH,
  1480. (addr[0] << 24) | (addr[1] << 16) | (addr[2] << 8) | addr[3]);
  1481. wrlp(mp, MAC_ADDR_LOW, (addr[4] << 8) | addr[5]);
  1482. }
  1483. static u32 uc_addr_filter_mask(struct net_device *dev)
  1484. {
  1485. struct netdev_hw_addr *ha;
  1486. u32 nibbles;
  1487. if (dev->flags & IFF_PROMISC)
  1488. return 0;
  1489. nibbles = 1 << (dev->dev_addr[5] & 0x0f);
  1490. netdev_for_each_uc_addr(ha, dev) {
  1491. if (memcmp(dev->dev_addr, ha->addr, 5))
  1492. return 0;
  1493. if ((dev->dev_addr[5] ^ ha->addr[5]) & 0xf0)
  1494. return 0;
  1495. nibbles |= 1 << (ha->addr[5] & 0x0f);
  1496. }
  1497. return nibbles;
  1498. }
  1499. static void mv643xx_eth_program_unicast_filter(struct net_device *dev)
  1500. {
  1501. struct mv643xx_eth_private *mp = netdev_priv(dev);
  1502. u32 port_config;
  1503. u32 nibbles;
  1504. int i;
  1505. uc_addr_set(mp, dev->dev_addr);
  1506. port_config = rdlp(mp, PORT_CONFIG) & ~UNICAST_PROMISCUOUS_MODE;
  1507. nibbles = uc_addr_filter_mask(dev);
  1508. if (!nibbles) {
  1509. port_config |= UNICAST_PROMISCUOUS_MODE;
  1510. nibbles = 0xffff;
  1511. }
  1512. for (i = 0; i < 16; i += 4) {
  1513. int off = UNICAST_TABLE(mp->port_num) + i;
  1514. u32 v;
  1515. v = 0;
  1516. if (nibbles & 1)
  1517. v |= 0x00000001;
  1518. if (nibbles & 2)
  1519. v |= 0x00000100;
  1520. if (nibbles & 4)
  1521. v |= 0x00010000;
  1522. if (nibbles & 8)
  1523. v |= 0x01000000;
  1524. nibbles >>= 4;
  1525. wrl(mp, off, v);
  1526. }
  1527. wrlp(mp, PORT_CONFIG, port_config);
  1528. }
  1529. static int addr_crc(unsigned char *addr)
  1530. {
  1531. int crc = 0;
  1532. int i;
  1533. for (i = 0; i < 6; i++) {
  1534. int j;
  1535. crc = (crc ^ addr[i]) << 8;
  1536. for (j = 7; j >= 0; j--) {
  1537. if (crc & (0x100 << j))
  1538. crc ^= 0x107 << j;
  1539. }
  1540. }
  1541. return crc;
  1542. }
  1543. static void mv643xx_eth_program_multicast_filter(struct net_device *dev)
  1544. {
  1545. struct mv643xx_eth_private *mp = netdev_priv(dev);
  1546. u32 *mc_spec;
  1547. u32 *mc_other;
  1548. struct netdev_hw_addr *ha;
  1549. int i;
  1550. if (dev->flags & (IFF_PROMISC | IFF_ALLMULTI))
  1551. goto promiscuous;
  1552. /* Allocate both mc_spec and mc_other tables */
  1553. mc_spec = kcalloc(128, sizeof(u32), GFP_ATOMIC);
  1554. if (!mc_spec)
  1555. goto promiscuous;
  1556. mc_other = &mc_spec[64];
  1557. netdev_for_each_mc_addr(ha, dev) {
  1558. u8 *a = ha->addr;
  1559. u32 *table;
  1560. u8 entry;
  1561. if (memcmp(a, "\x01\x00\x5e\x00\x00", 5) == 0) {
  1562. table = mc_spec;
  1563. entry = a[5];
  1564. } else {
  1565. table = mc_other;
  1566. entry = addr_crc(a);
  1567. }
  1568. table[entry >> 2] |= 1 << (8 * (entry & 3));
  1569. }
  1570. for (i = 0; i < 64; i++) {
  1571. wrl(mp, SPECIAL_MCAST_TABLE(mp->port_num) + i * sizeof(u32),
  1572. mc_spec[i]);
  1573. wrl(mp, OTHER_MCAST_TABLE(mp->port_num) + i * sizeof(u32),
  1574. mc_other[i]);
  1575. }
  1576. kfree(mc_spec);
  1577. return;
  1578. promiscuous:
  1579. for (i = 0; i < 64; i++) {
  1580. wrl(mp, SPECIAL_MCAST_TABLE(mp->port_num) + i * sizeof(u32),
  1581. 0x01010101u);
  1582. wrl(mp, OTHER_MCAST_TABLE(mp->port_num) + i * sizeof(u32),
  1583. 0x01010101u);
  1584. }
  1585. }
  1586. static void mv643xx_eth_set_rx_mode(struct net_device *dev)
  1587. {
  1588. mv643xx_eth_program_unicast_filter(dev);
  1589. mv643xx_eth_program_multicast_filter(dev);
  1590. }
  1591. static int mv643xx_eth_set_mac_address(struct net_device *dev, void *addr)
  1592. {
  1593. struct sockaddr *sa = addr;
  1594. if (!is_valid_ether_addr(sa->sa_data))
  1595. return -EADDRNOTAVAIL;
  1596. eth_hw_addr_set(dev, sa->sa_data);
  1597. netif_addr_lock_bh(dev);
  1598. mv643xx_eth_program_unicast_filter(dev);
  1599. netif_addr_unlock_bh(dev);
  1600. return 0;
  1601. }
  1602. /* rx/tx queue initialisation ***********************************************/
  1603. static int rxq_init(struct mv643xx_eth_private *mp, int index)
  1604. {
  1605. struct rx_queue *rxq = mp->rxq + index;
  1606. struct rx_desc *rx_desc;
  1607. int size;
  1608. int i;
  1609. rxq->index = index;
  1610. rxq->rx_ring_size = mp->rx_ring_size;
  1611. rxq->rx_desc_count = 0;
  1612. rxq->rx_curr_desc = 0;
  1613. rxq->rx_used_desc = 0;
  1614. size = rxq->rx_ring_size * sizeof(struct rx_desc);
  1615. if (index == 0 && size <= mp->rx_desc_sram_size) {
  1616. rxq->rx_desc_area = ioremap(mp->rx_desc_sram_addr,
  1617. mp->rx_desc_sram_size);
  1618. rxq->rx_desc_dma = mp->rx_desc_sram_addr;
  1619. } else {
  1620. rxq->rx_desc_area = dma_alloc_coherent(mp->dev->dev.parent,
  1621. size, &rxq->rx_desc_dma,
  1622. GFP_KERNEL);
  1623. }
  1624. if (rxq->rx_desc_area == NULL) {
  1625. netdev_err(mp->dev,
  1626. "can't allocate rx ring (%d bytes)\n", size);
  1627. goto out;
  1628. }
  1629. memset(rxq->rx_desc_area, 0, size);
  1630. rxq->rx_desc_area_size = size;
  1631. rxq->rx_skb = kzalloc_objs(*rxq->rx_skb, rxq->rx_ring_size);
  1632. if (rxq->rx_skb == NULL)
  1633. goto out_free;
  1634. rx_desc = rxq->rx_desc_area;
  1635. for (i = 0; i < rxq->rx_ring_size; i++) {
  1636. int nexti;
  1637. nexti = i + 1;
  1638. if (nexti == rxq->rx_ring_size)
  1639. nexti = 0;
  1640. rx_desc[i].next_desc_ptr = rxq->rx_desc_dma +
  1641. nexti * sizeof(struct rx_desc);
  1642. }
  1643. return 0;
  1644. out_free:
  1645. if (index == 0 && size <= mp->rx_desc_sram_size)
  1646. iounmap(rxq->rx_desc_area);
  1647. else
  1648. dma_free_coherent(mp->dev->dev.parent, size,
  1649. rxq->rx_desc_area,
  1650. rxq->rx_desc_dma);
  1651. out:
  1652. return -ENOMEM;
  1653. }
  1654. static void rxq_deinit(struct rx_queue *rxq)
  1655. {
  1656. struct mv643xx_eth_private *mp = rxq_to_mp(rxq);
  1657. int i;
  1658. rxq_disable(rxq);
  1659. for (i = 0; i < rxq->rx_ring_size; i++) {
  1660. if (rxq->rx_skb[i]) {
  1661. dev_consume_skb_any(rxq->rx_skb[i]);
  1662. rxq->rx_desc_count--;
  1663. }
  1664. }
  1665. if (rxq->rx_desc_count) {
  1666. netdev_err(mp->dev, "error freeing rx ring -- %d skbs stuck\n",
  1667. rxq->rx_desc_count);
  1668. }
  1669. if (rxq->index == 0 &&
  1670. rxq->rx_desc_area_size <= mp->rx_desc_sram_size)
  1671. iounmap(rxq->rx_desc_area);
  1672. else
  1673. dma_free_coherent(mp->dev->dev.parent, rxq->rx_desc_area_size,
  1674. rxq->rx_desc_area, rxq->rx_desc_dma);
  1675. kfree(rxq->rx_skb);
  1676. }
  1677. static int txq_init(struct mv643xx_eth_private *mp, int index)
  1678. {
  1679. struct tx_queue *txq = mp->txq + index;
  1680. struct tx_desc *tx_desc;
  1681. int size;
  1682. int ret;
  1683. int i;
  1684. txq->index = index;
  1685. txq->tx_ring_size = mp->tx_ring_size;
  1686. /* A queue must always have room for at least one skb.
  1687. * Therefore, stop the queue when the free entries reaches
  1688. * the maximum number of descriptors per skb.
  1689. */
  1690. txq->tx_stop_threshold = txq->tx_ring_size - MV643XX_MAX_SKB_DESCS;
  1691. txq->tx_wake_threshold = txq->tx_stop_threshold / 2;
  1692. txq->tx_desc_count = 0;
  1693. txq->tx_curr_desc = 0;
  1694. txq->tx_used_desc = 0;
  1695. size = txq->tx_ring_size * sizeof(struct tx_desc);
  1696. if (index == 0 && size <= mp->tx_desc_sram_size) {
  1697. txq->tx_desc_area = ioremap(mp->tx_desc_sram_addr,
  1698. mp->tx_desc_sram_size);
  1699. txq->tx_desc_dma = mp->tx_desc_sram_addr;
  1700. } else {
  1701. txq->tx_desc_area = dma_alloc_coherent(mp->dev->dev.parent,
  1702. size, &txq->tx_desc_dma,
  1703. GFP_KERNEL);
  1704. }
  1705. if (txq->tx_desc_area == NULL) {
  1706. netdev_err(mp->dev,
  1707. "can't allocate tx ring (%d bytes)\n", size);
  1708. return -ENOMEM;
  1709. }
  1710. memset(txq->tx_desc_area, 0, size);
  1711. txq->tx_desc_area_size = size;
  1712. tx_desc = txq->tx_desc_area;
  1713. for (i = 0; i < txq->tx_ring_size; i++) {
  1714. struct tx_desc *txd = tx_desc + i;
  1715. int nexti;
  1716. nexti = i + 1;
  1717. if (nexti == txq->tx_ring_size)
  1718. nexti = 0;
  1719. txd->cmd_sts = 0;
  1720. txd->next_desc_ptr = txq->tx_desc_dma +
  1721. nexti * sizeof(struct tx_desc);
  1722. }
  1723. txq->tx_desc_mapping = kcalloc(txq->tx_ring_size, sizeof(char),
  1724. GFP_KERNEL);
  1725. if (!txq->tx_desc_mapping) {
  1726. ret = -ENOMEM;
  1727. goto err_free_desc_area;
  1728. }
  1729. /* Allocate DMA buffers for TSO MAC/IP/TCP headers */
  1730. txq->tso_hdrs = dma_alloc_coherent(mp->dev->dev.parent,
  1731. txq->tx_ring_size * TSO_HEADER_SIZE,
  1732. &txq->tso_hdrs_dma, GFP_KERNEL);
  1733. if (txq->tso_hdrs == NULL) {
  1734. ret = -ENOMEM;
  1735. goto err_free_desc_mapping;
  1736. }
  1737. skb_queue_head_init(&txq->tx_skb);
  1738. return 0;
  1739. err_free_desc_mapping:
  1740. kfree(txq->tx_desc_mapping);
  1741. err_free_desc_area:
  1742. if (index == 0 && size <= mp->tx_desc_sram_size)
  1743. iounmap(txq->tx_desc_area);
  1744. else
  1745. dma_free_coherent(mp->dev->dev.parent, txq->tx_desc_area_size,
  1746. txq->tx_desc_area, txq->tx_desc_dma);
  1747. return ret;
  1748. }
  1749. static void txq_deinit(struct tx_queue *txq)
  1750. {
  1751. struct mv643xx_eth_private *mp = txq_to_mp(txq);
  1752. txq_disable(txq);
  1753. txq_reclaim(txq, txq->tx_ring_size, 1);
  1754. BUG_ON(txq->tx_used_desc != txq->tx_curr_desc);
  1755. if (txq->index == 0 &&
  1756. txq->tx_desc_area_size <= mp->tx_desc_sram_size)
  1757. iounmap(txq->tx_desc_area);
  1758. else
  1759. dma_free_coherent(mp->dev->dev.parent, txq->tx_desc_area_size,
  1760. txq->tx_desc_area, txq->tx_desc_dma);
  1761. kfree(txq->tx_desc_mapping);
  1762. if (txq->tso_hdrs)
  1763. dma_free_coherent(mp->dev->dev.parent,
  1764. txq->tx_ring_size * TSO_HEADER_SIZE,
  1765. txq->tso_hdrs, txq->tso_hdrs_dma);
  1766. }
  1767. /* netdev ops and related ***************************************************/
  1768. static int mv643xx_eth_collect_events(struct mv643xx_eth_private *mp)
  1769. {
  1770. u32 int_cause;
  1771. u32 int_cause_ext;
  1772. int_cause = rdlp(mp, INT_CAUSE) & mp->int_mask;
  1773. if (int_cause == 0)
  1774. return 0;
  1775. int_cause_ext = 0;
  1776. if (int_cause & INT_EXT) {
  1777. int_cause &= ~INT_EXT;
  1778. int_cause_ext = rdlp(mp, INT_CAUSE_EXT);
  1779. }
  1780. if (int_cause) {
  1781. wrlp(mp, INT_CAUSE, ~int_cause);
  1782. mp->work_tx_end |= ((int_cause & INT_TX_END) >> 19) &
  1783. ~(rdlp(mp, TXQ_COMMAND) & 0xff);
  1784. mp->work_rx |= (int_cause & INT_RX) >> 2;
  1785. }
  1786. int_cause_ext &= INT_EXT_LINK_PHY | INT_EXT_TX;
  1787. if (int_cause_ext) {
  1788. wrlp(mp, INT_CAUSE_EXT, ~int_cause_ext);
  1789. if (int_cause_ext & INT_EXT_LINK_PHY)
  1790. mp->work_link = 1;
  1791. mp->work_tx |= int_cause_ext & INT_EXT_TX;
  1792. }
  1793. return 1;
  1794. }
  1795. static irqreturn_t mv643xx_eth_irq(int irq, void *dev_id)
  1796. {
  1797. struct net_device *dev = (struct net_device *)dev_id;
  1798. struct mv643xx_eth_private *mp = netdev_priv(dev);
  1799. if (unlikely(!mv643xx_eth_collect_events(mp)))
  1800. return IRQ_NONE;
  1801. wrlp(mp, INT_MASK, 0);
  1802. napi_schedule(&mp->napi);
  1803. return IRQ_HANDLED;
  1804. }
  1805. static void handle_link_event(struct mv643xx_eth_private *mp)
  1806. {
  1807. struct net_device *dev = mp->dev;
  1808. u32 port_status;
  1809. int speed;
  1810. int duplex;
  1811. int fc;
  1812. port_status = rdlp(mp, PORT_STATUS);
  1813. if (!(port_status & LINK_UP)) {
  1814. if (netif_carrier_ok(dev)) {
  1815. int i;
  1816. netdev_info(dev, "link down\n");
  1817. netif_carrier_off(dev);
  1818. for (i = 0; i < mp->txq_count; i++) {
  1819. struct tx_queue *txq = mp->txq + i;
  1820. txq_reclaim(txq, txq->tx_ring_size, 1);
  1821. txq_reset_hw_ptr(txq);
  1822. }
  1823. }
  1824. return;
  1825. }
  1826. switch (port_status & PORT_SPEED_MASK) {
  1827. case PORT_SPEED_10:
  1828. speed = 10;
  1829. break;
  1830. case PORT_SPEED_100:
  1831. speed = 100;
  1832. break;
  1833. case PORT_SPEED_1000:
  1834. speed = 1000;
  1835. break;
  1836. default:
  1837. speed = -1;
  1838. break;
  1839. }
  1840. duplex = (port_status & FULL_DUPLEX) ? 1 : 0;
  1841. fc = (port_status & FLOW_CONTROL_ENABLED) ? 1 : 0;
  1842. netdev_info(dev, "link up, %d Mb/s, %s duplex, flow control %sabled\n",
  1843. speed, duplex ? "full" : "half", fc ? "en" : "dis");
  1844. if (!netif_carrier_ok(dev))
  1845. netif_carrier_on(dev);
  1846. }
  1847. static int mv643xx_eth_poll(struct napi_struct *napi, int budget)
  1848. {
  1849. struct mv643xx_eth_private *mp;
  1850. int work_done;
  1851. mp = container_of(napi, struct mv643xx_eth_private, napi);
  1852. if (unlikely(mp->oom)) {
  1853. mp->oom = 0;
  1854. timer_delete(&mp->rx_oom);
  1855. }
  1856. work_done = 0;
  1857. while (work_done < budget) {
  1858. u8 queue_mask;
  1859. int queue;
  1860. int work_tbd;
  1861. if (mp->work_link) {
  1862. mp->work_link = 0;
  1863. handle_link_event(mp);
  1864. work_done++;
  1865. continue;
  1866. }
  1867. queue_mask = mp->work_tx | mp->work_tx_end | mp->work_rx;
  1868. if (likely(!mp->oom))
  1869. queue_mask |= mp->work_rx_refill;
  1870. if (!queue_mask) {
  1871. if (mv643xx_eth_collect_events(mp))
  1872. continue;
  1873. break;
  1874. }
  1875. queue = fls(queue_mask) - 1;
  1876. queue_mask = 1 << queue;
  1877. work_tbd = budget - work_done;
  1878. if (work_tbd > 16)
  1879. work_tbd = 16;
  1880. if (mp->work_tx_end & queue_mask) {
  1881. txq_kick(mp->txq + queue);
  1882. } else if (mp->work_tx & queue_mask) {
  1883. work_done += txq_reclaim(mp->txq + queue, work_tbd, 0);
  1884. txq_maybe_wake(mp->txq + queue);
  1885. } else if (mp->work_rx & queue_mask) {
  1886. work_done += rxq_process(mp->rxq + queue, work_tbd);
  1887. } else if (!mp->oom && (mp->work_rx_refill & queue_mask)) {
  1888. work_done += rxq_refill(mp->rxq + queue, work_tbd);
  1889. } else {
  1890. BUG();
  1891. }
  1892. }
  1893. if (work_done < budget) {
  1894. if (mp->oom)
  1895. mod_timer(&mp->rx_oom, jiffies + (HZ / 10));
  1896. napi_complete_done(napi, work_done);
  1897. wrlp(mp, INT_MASK, mp->int_mask);
  1898. }
  1899. return work_done;
  1900. }
  1901. static inline void oom_timer_wrapper(struct timer_list *t)
  1902. {
  1903. struct mv643xx_eth_private *mp = timer_container_of(mp, t, rx_oom);
  1904. napi_schedule(&mp->napi);
  1905. }
  1906. static void port_start(struct mv643xx_eth_private *mp)
  1907. {
  1908. struct net_device *dev = mp->dev;
  1909. u32 pscr;
  1910. int i;
  1911. /*
  1912. * Perform PHY reset, if there is a PHY.
  1913. */
  1914. if (dev->phydev) {
  1915. struct ethtool_link_ksettings cmd;
  1916. mv643xx_eth_get_link_ksettings(dev, &cmd);
  1917. phy_init_hw(dev->phydev);
  1918. mv643xx_eth_set_link_ksettings(
  1919. dev, (const struct ethtool_link_ksettings *)&cmd);
  1920. phy_start(dev->phydev);
  1921. }
  1922. /*
  1923. * Configure basic link parameters.
  1924. */
  1925. pscr = rdlp(mp, PORT_SERIAL_CONTROL);
  1926. pscr |= SERIAL_PORT_ENABLE;
  1927. wrlp(mp, PORT_SERIAL_CONTROL, pscr);
  1928. pscr |= DO_NOT_FORCE_LINK_FAIL;
  1929. if (!dev->phydev)
  1930. pscr |= FORCE_LINK_PASS;
  1931. wrlp(mp, PORT_SERIAL_CONTROL, pscr);
  1932. /*
  1933. * Configure TX path and queues.
  1934. */
  1935. tx_set_rate(mp, 1000000000, 16777216);
  1936. for (i = 0; i < mp->txq_count; i++) {
  1937. struct tx_queue *txq = mp->txq + i;
  1938. txq_reset_hw_ptr(txq);
  1939. txq_set_rate(txq, 1000000000, 16777216);
  1940. txq_set_fixed_prio_mode(txq);
  1941. }
  1942. /*
  1943. * Receive all unmatched unicast, TCP, UDP, BPDU and broadcast
  1944. * frames to RX queue #0, and include the pseudo-header when
  1945. * calculating receive checksums.
  1946. */
  1947. mv643xx_eth_set_features(mp->dev, mp->dev->features);
  1948. /*
  1949. * Treat BPDUs as normal multicasts, and disable partition mode.
  1950. */
  1951. wrlp(mp, PORT_CONFIG_EXT, 0x00000000);
  1952. /*
  1953. * Add configured unicast addresses to address filter table.
  1954. */
  1955. mv643xx_eth_program_unicast_filter(mp->dev);
  1956. /*
  1957. * Enable the receive queues.
  1958. */
  1959. for (i = 0; i < mp->rxq_count; i++) {
  1960. struct rx_queue *rxq = mp->rxq + i;
  1961. u32 addr;
  1962. addr = (u32)rxq->rx_desc_dma;
  1963. addr += rxq->rx_curr_desc * sizeof(struct rx_desc);
  1964. wrlp(mp, RXQ_CURRENT_DESC_PTR(i), addr);
  1965. rxq_enable(rxq);
  1966. }
  1967. }
  1968. static void mv643xx_eth_recalc_skb_size(struct mv643xx_eth_private *mp)
  1969. {
  1970. int skb_size;
  1971. /*
  1972. * Reserve 2+14 bytes for an ethernet header (the hardware
  1973. * automatically prepends 2 bytes of dummy data to each
  1974. * received packet), 16 bytes for up to four VLAN tags, and
  1975. * 4 bytes for the trailing FCS -- 36 bytes total.
  1976. */
  1977. skb_size = mp->dev->mtu + 36;
  1978. /*
  1979. * Make sure that the skb size is a multiple of 8 bytes, as
  1980. * the lower three bits of the receive descriptor's buffer
  1981. * size field are ignored by the hardware.
  1982. */
  1983. mp->skb_size = (skb_size + 7) & ~7;
  1984. /*
  1985. * If NET_SKB_PAD is smaller than a cache line,
  1986. * netdev_alloc_skb() will cause skb->data to be misaligned
  1987. * to a cache line boundary. If this is the case, include
  1988. * some extra space to allow re-aligning the data area.
  1989. */
  1990. mp->skb_size += SKB_DMA_REALIGN;
  1991. }
  1992. static int mv643xx_eth_open(struct net_device *dev)
  1993. {
  1994. struct mv643xx_eth_private *mp = netdev_priv(dev);
  1995. int err;
  1996. int i;
  1997. wrlp(mp, INT_CAUSE, 0);
  1998. wrlp(mp, INT_CAUSE_EXT, 0);
  1999. rdlp(mp, INT_CAUSE_EXT);
  2000. err = request_irq(dev->irq, mv643xx_eth_irq,
  2001. IRQF_SHARED, dev->name, dev);
  2002. if (err) {
  2003. netdev_err(dev, "can't assign irq\n");
  2004. return -EAGAIN;
  2005. }
  2006. mv643xx_eth_recalc_skb_size(mp);
  2007. napi_enable(&mp->napi);
  2008. mp->int_mask = INT_EXT;
  2009. for (i = 0; i < mp->rxq_count; i++) {
  2010. err = rxq_init(mp, i);
  2011. if (err) {
  2012. while (--i >= 0)
  2013. rxq_deinit(mp->rxq + i);
  2014. goto out;
  2015. }
  2016. rxq_refill(mp->rxq + i, INT_MAX);
  2017. mp->int_mask |= INT_RX_0 << i;
  2018. }
  2019. if (mp->oom) {
  2020. mp->rx_oom.expires = jiffies + (HZ / 10);
  2021. add_timer(&mp->rx_oom);
  2022. }
  2023. for (i = 0; i < mp->txq_count; i++) {
  2024. err = txq_init(mp, i);
  2025. if (err) {
  2026. while (--i >= 0)
  2027. txq_deinit(mp->txq + i);
  2028. goto out_free;
  2029. }
  2030. mp->int_mask |= INT_TX_END_0 << i;
  2031. }
  2032. add_timer(&mp->mib_counters_timer);
  2033. port_start(mp);
  2034. wrlp(mp, INT_MASK_EXT, INT_EXT_LINK_PHY | INT_EXT_TX);
  2035. wrlp(mp, INT_MASK, mp->int_mask);
  2036. return 0;
  2037. out_free:
  2038. for (i = 0; i < mp->rxq_count; i++)
  2039. rxq_deinit(mp->rxq + i);
  2040. out:
  2041. napi_disable(&mp->napi);
  2042. free_irq(dev->irq, dev);
  2043. return err;
  2044. }
  2045. static void port_reset(struct mv643xx_eth_private *mp)
  2046. {
  2047. unsigned int data;
  2048. int i;
  2049. for (i = 0; i < mp->rxq_count; i++)
  2050. rxq_disable(mp->rxq + i);
  2051. for (i = 0; i < mp->txq_count; i++)
  2052. txq_disable(mp->txq + i);
  2053. while (1) {
  2054. u32 ps = rdlp(mp, PORT_STATUS);
  2055. if ((ps & (TX_IN_PROGRESS | TX_FIFO_EMPTY)) == TX_FIFO_EMPTY)
  2056. break;
  2057. udelay(10);
  2058. }
  2059. /* Reset the Enable bit in the Configuration Register */
  2060. data = rdlp(mp, PORT_SERIAL_CONTROL);
  2061. data &= ~(SERIAL_PORT_ENABLE |
  2062. DO_NOT_FORCE_LINK_FAIL |
  2063. FORCE_LINK_PASS);
  2064. wrlp(mp, PORT_SERIAL_CONTROL, data);
  2065. }
  2066. static int mv643xx_eth_stop(struct net_device *dev)
  2067. {
  2068. struct mv643xx_eth_private *mp = netdev_priv(dev);
  2069. int i;
  2070. wrlp(mp, INT_MASK_EXT, 0x00000000);
  2071. wrlp(mp, INT_MASK, 0x00000000);
  2072. rdlp(mp, INT_MASK);
  2073. napi_disable(&mp->napi);
  2074. timer_delete_sync(&mp->rx_oom);
  2075. netif_carrier_off(dev);
  2076. if (dev->phydev)
  2077. phy_stop(dev->phydev);
  2078. free_irq(dev->irq, dev);
  2079. port_reset(mp);
  2080. mv643xx_eth_get_stats(dev);
  2081. mib_counters_update(mp);
  2082. timer_delete_sync(&mp->mib_counters_timer);
  2083. for (i = 0; i < mp->rxq_count; i++)
  2084. rxq_deinit(mp->rxq + i);
  2085. for (i = 0; i < mp->txq_count; i++)
  2086. txq_deinit(mp->txq + i);
  2087. return 0;
  2088. }
  2089. static int mv643xx_eth_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  2090. {
  2091. int ret;
  2092. if (!dev->phydev)
  2093. return -ENOTSUPP;
  2094. ret = phy_mii_ioctl(dev->phydev, ifr, cmd);
  2095. if (!ret)
  2096. mv643xx_eth_adjust_link(dev);
  2097. return ret;
  2098. }
  2099. static int mv643xx_eth_change_mtu(struct net_device *dev, int new_mtu)
  2100. {
  2101. struct mv643xx_eth_private *mp = netdev_priv(dev);
  2102. WRITE_ONCE(dev->mtu, new_mtu);
  2103. mv643xx_eth_recalc_skb_size(mp);
  2104. tx_set_rate(mp, 1000000000, 16777216);
  2105. if (!netif_running(dev))
  2106. return 0;
  2107. /*
  2108. * Stop and then re-open the interface. This will allocate RX
  2109. * skbs of the new MTU.
  2110. * There is a possible danger that the open will not succeed,
  2111. * due to memory being full.
  2112. */
  2113. mv643xx_eth_stop(dev);
  2114. if (mv643xx_eth_open(dev)) {
  2115. netdev_err(dev,
  2116. "fatal error on re-opening device after MTU change\n");
  2117. }
  2118. return 0;
  2119. }
  2120. static void tx_timeout_task(struct work_struct *ugly)
  2121. {
  2122. struct mv643xx_eth_private *mp;
  2123. mp = container_of(ugly, struct mv643xx_eth_private, tx_timeout_task);
  2124. if (netif_running(mp->dev)) {
  2125. netif_tx_stop_all_queues(mp->dev);
  2126. port_reset(mp);
  2127. port_start(mp);
  2128. netif_tx_wake_all_queues(mp->dev);
  2129. }
  2130. }
  2131. static void mv643xx_eth_tx_timeout(struct net_device *dev, unsigned int txqueue)
  2132. {
  2133. struct mv643xx_eth_private *mp = netdev_priv(dev);
  2134. netdev_info(dev, "tx timeout\n");
  2135. schedule_work(&mp->tx_timeout_task);
  2136. }
  2137. #ifdef CONFIG_NET_POLL_CONTROLLER
  2138. static void mv643xx_eth_netpoll(struct net_device *dev)
  2139. {
  2140. struct mv643xx_eth_private *mp = netdev_priv(dev);
  2141. wrlp(mp, INT_MASK, 0x00000000);
  2142. rdlp(mp, INT_MASK);
  2143. mv643xx_eth_irq(dev->irq, dev);
  2144. wrlp(mp, INT_MASK, mp->int_mask);
  2145. }
  2146. #endif
  2147. /* platform glue ************************************************************/
  2148. static void
  2149. mv643xx_eth_conf_mbus_windows(struct mv643xx_eth_shared_private *msp,
  2150. const struct mbus_dram_target_info *dram)
  2151. {
  2152. void __iomem *base = msp->base;
  2153. u32 win_enable;
  2154. u32 win_protect;
  2155. int i;
  2156. for (i = 0; i < 6; i++) {
  2157. writel(0, base + WINDOW_BASE(i));
  2158. writel(0, base + WINDOW_SIZE(i));
  2159. if (i < 4)
  2160. writel(0, base + WINDOW_REMAP_HIGH(i));
  2161. }
  2162. win_enable = 0x3f;
  2163. win_protect = 0;
  2164. for (i = 0; i < dram->num_cs; i++) {
  2165. const struct mbus_dram_window *cs = dram->cs + i;
  2166. writel((cs->base & 0xffff0000) |
  2167. (cs->mbus_attr << 8) |
  2168. dram->mbus_dram_target_id, base + WINDOW_BASE(i));
  2169. writel((cs->size - 1) & 0xffff0000, base + WINDOW_SIZE(i));
  2170. win_enable &= ~(1 << i);
  2171. win_protect |= 3 << (2 * i);
  2172. }
  2173. writel(win_enable, base + WINDOW_BAR_ENABLE);
  2174. msp->win_protect = win_protect;
  2175. }
  2176. static void infer_hw_params(struct mv643xx_eth_shared_private *msp)
  2177. {
  2178. /*
  2179. * Check whether we have a 14-bit coal limit field in bits
  2180. * [21:8], or a 16-bit coal limit in bits [25,21:7] of the
  2181. * SDMA config register.
  2182. */
  2183. writel(0x02000000, msp->base + 0x0400 + SDMA_CONFIG);
  2184. if (readl(msp->base + 0x0400 + SDMA_CONFIG) & 0x02000000)
  2185. msp->extended_rx_coal_limit = 1;
  2186. else
  2187. msp->extended_rx_coal_limit = 0;
  2188. /*
  2189. * Check whether the MAC supports TX rate control, and if
  2190. * yes, whether its associated registers are in the old or
  2191. * the new place.
  2192. */
  2193. writel(1, msp->base + 0x0400 + TX_BW_MTU_MOVED);
  2194. if (readl(msp->base + 0x0400 + TX_BW_MTU_MOVED) & 1) {
  2195. msp->tx_bw_control = TX_BW_CONTROL_NEW_LAYOUT;
  2196. } else {
  2197. writel(7, msp->base + 0x0400 + TX_BW_RATE);
  2198. if (readl(msp->base + 0x0400 + TX_BW_RATE) & 7)
  2199. msp->tx_bw_control = TX_BW_CONTROL_OLD_LAYOUT;
  2200. else
  2201. msp->tx_bw_control = TX_BW_CONTROL_ABSENT;
  2202. }
  2203. }
  2204. #if defined(CONFIG_OF)
  2205. static const struct of_device_id mv643xx_eth_shared_ids[] = {
  2206. { .compatible = "marvell,orion-eth", },
  2207. { .compatible = "marvell,kirkwood-eth", },
  2208. { }
  2209. };
  2210. MODULE_DEVICE_TABLE(of, mv643xx_eth_shared_ids);
  2211. #endif
  2212. #ifdef CONFIG_OF_IRQ
  2213. #define mv643xx_eth_property(_np, _name, _v) \
  2214. do { \
  2215. u32 tmp; \
  2216. if (!of_property_read_u32(_np, "marvell," _name, &tmp)) \
  2217. _v = tmp; \
  2218. } while (0)
  2219. static struct platform_device *port_platdev[3];
  2220. static void mv643xx_eth_shared_of_remove(void)
  2221. {
  2222. struct mv643xx_eth_platform_data *pd;
  2223. int n;
  2224. for (n = 0; n < 3; n++) {
  2225. if (!port_platdev[n])
  2226. continue;
  2227. pd = dev_get_platdata(&port_platdev[n]->dev);
  2228. if (pd)
  2229. of_node_put(pd->phy_node);
  2230. platform_device_del(port_platdev[n]);
  2231. port_platdev[n] = NULL;
  2232. }
  2233. }
  2234. static int mv643xx_eth_shared_of_add_port(struct platform_device *pdev,
  2235. struct device_node *pnp)
  2236. {
  2237. struct platform_device *ppdev;
  2238. struct mv643xx_eth_platform_data ppd;
  2239. struct resource res;
  2240. int ret;
  2241. int dev_num = 0;
  2242. memset(&ppd, 0, sizeof(ppd));
  2243. ppd.shared = pdev;
  2244. memset(&res, 0, sizeof(res));
  2245. if (of_irq_to_resource(pnp, 0, &res) <= 0) {
  2246. dev_err(&pdev->dev, "missing interrupt on %pOFn\n", pnp);
  2247. return -EINVAL;
  2248. }
  2249. if (of_property_read_u32(pnp, "reg", &ppd.port_number)) {
  2250. dev_err(&pdev->dev, "missing reg property on %pOFn\n", pnp);
  2251. return -EINVAL;
  2252. }
  2253. if (ppd.port_number >= 3) {
  2254. dev_err(&pdev->dev, "invalid reg property on %pOFn\n", pnp);
  2255. return -EINVAL;
  2256. }
  2257. while (dev_num < 3 && port_platdev[dev_num])
  2258. dev_num++;
  2259. if (dev_num == 3) {
  2260. dev_err(&pdev->dev, "too many ports registered\n");
  2261. return -EINVAL;
  2262. }
  2263. ret = of_get_mac_address(pnp, ppd.mac_addr);
  2264. if (ret == -EPROBE_DEFER)
  2265. return ret;
  2266. mv643xx_eth_property(pnp, "tx-queue-size", ppd.tx_queue_size);
  2267. mv643xx_eth_property(pnp, "tx-sram-addr", ppd.tx_sram_addr);
  2268. mv643xx_eth_property(pnp, "tx-sram-size", ppd.tx_sram_size);
  2269. mv643xx_eth_property(pnp, "rx-queue-size", ppd.rx_queue_size);
  2270. mv643xx_eth_property(pnp, "rx-sram-addr", ppd.rx_sram_addr);
  2271. mv643xx_eth_property(pnp, "rx-sram-size", ppd.rx_sram_size);
  2272. of_get_phy_mode(pnp, &ppd.interface);
  2273. ppd.phy_node = of_parse_phandle(pnp, "phy-handle", 0);
  2274. if (!ppd.phy_node) {
  2275. ppd.phy_addr = MV643XX_ETH_PHY_NONE;
  2276. of_property_read_u32(pnp, "speed", &ppd.speed);
  2277. of_property_read_u32(pnp, "duplex", &ppd.duplex);
  2278. }
  2279. ppdev = platform_device_alloc(MV643XX_ETH_NAME, dev_num);
  2280. if (!ppdev) {
  2281. ret = -ENOMEM;
  2282. goto put_err;
  2283. }
  2284. ppdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
  2285. ppdev->dev.of_node = pnp;
  2286. ret = platform_device_add_resources(ppdev, &res, 1);
  2287. if (ret)
  2288. goto port_err;
  2289. ret = platform_device_add_data(ppdev, &ppd, sizeof(ppd));
  2290. if (ret)
  2291. goto port_err;
  2292. ret = platform_device_add(ppdev);
  2293. if (ret)
  2294. goto port_err;
  2295. port_platdev[dev_num] = ppdev;
  2296. return 0;
  2297. port_err:
  2298. platform_device_put(ppdev);
  2299. put_err:
  2300. of_node_put(ppd.phy_node);
  2301. return ret;
  2302. }
  2303. static int mv643xx_eth_shared_of_probe(struct platform_device *pdev)
  2304. {
  2305. struct mv643xx_eth_shared_platform_data *pd;
  2306. struct device_node *np = pdev->dev.of_node;
  2307. int ret;
  2308. /* bail out if not registered from DT */
  2309. if (!np)
  2310. return 0;
  2311. pd = devm_kzalloc(&pdev->dev, sizeof(*pd), GFP_KERNEL);
  2312. if (!pd)
  2313. return -ENOMEM;
  2314. pdev->dev.platform_data = pd;
  2315. mv643xx_eth_property(np, "tx-checksum-limit", pd->tx_csum_limit);
  2316. for_each_available_child_of_node_scoped(np, pnp) {
  2317. ret = mv643xx_eth_shared_of_add_port(pdev, pnp);
  2318. if (ret) {
  2319. mv643xx_eth_shared_of_remove();
  2320. return ret;
  2321. }
  2322. }
  2323. return 0;
  2324. }
  2325. #else
  2326. static inline int mv643xx_eth_shared_of_probe(struct platform_device *pdev)
  2327. {
  2328. return 0;
  2329. }
  2330. static inline void mv643xx_eth_shared_of_remove(void)
  2331. {
  2332. }
  2333. #endif
  2334. static int mv643xx_eth_shared_probe(struct platform_device *pdev)
  2335. {
  2336. static int mv643xx_eth_version_printed;
  2337. struct mv643xx_eth_shared_platform_data *pd;
  2338. struct mv643xx_eth_shared_private *msp;
  2339. const struct mbus_dram_target_info *dram;
  2340. int ret;
  2341. if (!mv643xx_eth_version_printed++)
  2342. pr_notice("MV-643xx 10/100/1000 ethernet driver version %s\n",
  2343. mv643xx_eth_driver_version);
  2344. msp = devm_kzalloc(&pdev->dev, sizeof(*msp), GFP_KERNEL);
  2345. if (msp == NULL)
  2346. return -ENOMEM;
  2347. platform_set_drvdata(pdev, msp);
  2348. msp->base = devm_platform_ioremap_resource(pdev, 0);
  2349. if (IS_ERR(msp->base))
  2350. return PTR_ERR(msp->base);
  2351. msp->clk = devm_clk_get_optional_enabled(&pdev->dev, NULL);
  2352. if (IS_ERR(msp->clk))
  2353. return PTR_ERR(msp->clk);
  2354. /*
  2355. * (Re-)program MBUS remapping windows if we are asked to.
  2356. */
  2357. dram = mv_mbus_dram_info();
  2358. if (dram)
  2359. mv643xx_eth_conf_mbus_windows(msp, dram);
  2360. ret = mv643xx_eth_shared_of_probe(pdev);
  2361. if (ret)
  2362. return ret;
  2363. pd = dev_get_platdata(&pdev->dev);
  2364. msp->tx_csum_limit = (pd != NULL && pd->tx_csum_limit) ?
  2365. pd->tx_csum_limit : 9 * 1024;
  2366. infer_hw_params(msp);
  2367. return 0;
  2368. }
  2369. static void mv643xx_eth_shared_remove(struct platform_device *pdev)
  2370. {
  2371. mv643xx_eth_shared_of_remove();
  2372. }
  2373. static struct platform_driver mv643xx_eth_shared_driver = {
  2374. .probe = mv643xx_eth_shared_probe,
  2375. .remove = mv643xx_eth_shared_remove,
  2376. .driver = {
  2377. .name = MV643XX_ETH_SHARED_NAME,
  2378. .of_match_table = of_match_ptr(mv643xx_eth_shared_ids),
  2379. },
  2380. };
  2381. static void phy_addr_set(struct mv643xx_eth_private *mp, int phy_addr)
  2382. {
  2383. int addr_shift = 5 * mp->port_num;
  2384. u32 data;
  2385. data = rdl(mp, PHY_ADDR);
  2386. data &= ~(0x1f << addr_shift);
  2387. data |= (phy_addr & 0x1f) << addr_shift;
  2388. wrl(mp, PHY_ADDR, data);
  2389. }
  2390. static int phy_addr_get(struct mv643xx_eth_private *mp)
  2391. {
  2392. unsigned int data;
  2393. data = rdl(mp, PHY_ADDR);
  2394. return (data >> (5 * mp->port_num)) & 0x1f;
  2395. }
  2396. static void set_params(struct mv643xx_eth_private *mp,
  2397. struct mv643xx_eth_platform_data *pd)
  2398. {
  2399. struct net_device *dev = mp->dev;
  2400. unsigned int tx_ring_size;
  2401. if (is_valid_ether_addr(pd->mac_addr)) {
  2402. eth_hw_addr_set(dev, pd->mac_addr);
  2403. } else {
  2404. u8 addr[ETH_ALEN];
  2405. uc_addr_get(mp, addr);
  2406. eth_hw_addr_set(dev, addr);
  2407. }
  2408. mp->rx_ring_size = DEFAULT_RX_QUEUE_SIZE;
  2409. if (pd->rx_queue_size)
  2410. mp->rx_ring_size = pd->rx_queue_size;
  2411. mp->rx_desc_sram_addr = pd->rx_sram_addr;
  2412. mp->rx_desc_sram_size = pd->rx_sram_size;
  2413. mp->rxq_count = pd->rx_queue_count ? : 1;
  2414. tx_ring_size = DEFAULT_TX_QUEUE_SIZE;
  2415. if (pd->tx_queue_size)
  2416. tx_ring_size = pd->tx_queue_size;
  2417. mp->tx_ring_size = clamp_t(unsigned int, tx_ring_size,
  2418. MV643XX_MAX_SKB_DESCS * 2, 4096);
  2419. if (mp->tx_ring_size != tx_ring_size)
  2420. netdev_warn(dev, "TX queue size set to %u (requested %u)\n",
  2421. mp->tx_ring_size, tx_ring_size);
  2422. mp->tx_desc_sram_addr = pd->tx_sram_addr;
  2423. mp->tx_desc_sram_size = pd->tx_sram_size;
  2424. mp->txq_count = pd->tx_queue_count ? : 1;
  2425. }
  2426. static int get_phy_mode(struct mv643xx_eth_private *mp)
  2427. {
  2428. struct device *dev = mp->dev->dev.parent;
  2429. phy_interface_t iface;
  2430. int err;
  2431. if (dev->of_node)
  2432. err = of_get_phy_mode(dev->of_node, &iface);
  2433. /* Historical default if unspecified. We could also read/write
  2434. * the interface state in the PSC1
  2435. */
  2436. if (!dev->of_node || err)
  2437. iface = PHY_INTERFACE_MODE_GMII;
  2438. return iface;
  2439. }
  2440. static struct phy_device *phy_scan(struct mv643xx_eth_private *mp,
  2441. int phy_addr)
  2442. {
  2443. struct phy_device *phydev;
  2444. int start;
  2445. int num;
  2446. int i;
  2447. char phy_id[MII_BUS_ID_SIZE + 3];
  2448. if (phy_addr == MV643XX_ETH_PHY_ADDR_DEFAULT) {
  2449. start = phy_addr_get(mp) & 0x1f;
  2450. num = 32;
  2451. } else {
  2452. start = phy_addr & 0x1f;
  2453. num = 1;
  2454. }
  2455. /* Attempt to connect to the PHY using orion-mdio */
  2456. phydev = ERR_PTR(-ENODEV);
  2457. for (i = 0; i < num; i++) {
  2458. int addr = (start + i) & 0x1f;
  2459. snprintf(phy_id, sizeof(phy_id), PHY_ID_FMT,
  2460. "orion-mdio-mii", addr);
  2461. phydev = phy_connect(mp->dev, phy_id, mv643xx_eth_adjust_link,
  2462. get_phy_mode(mp));
  2463. if (!IS_ERR(phydev)) {
  2464. phy_addr_set(mp, addr);
  2465. break;
  2466. }
  2467. }
  2468. return phydev;
  2469. }
  2470. static void phy_init(struct mv643xx_eth_private *mp, int speed, int duplex)
  2471. {
  2472. struct net_device *dev = mp->dev;
  2473. struct phy_device *phy = dev->phydev;
  2474. if (speed == 0) {
  2475. phy->autoneg = AUTONEG_ENABLE;
  2476. phy->speed = 0;
  2477. phy->duplex = 0;
  2478. linkmode_copy(phy->advertising, phy->supported);
  2479. linkmode_set_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
  2480. phy->advertising);
  2481. } else {
  2482. phy->autoneg = AUTONEG_DISABLE;
  2483. linkmode_zero(phy->advertising);
  2484. phy->speed = speed;
  2485. phy->duplex = duplex;
  2486. }
  2487. phy_start_aneg(phy);
  2488. }
  2489. static void init_pscr(struct mv643xx_eth_private *mp, int speed, int duplex)
  2490. {
  2491. struct net_device *dev = mp->dev;
  2492. u32 pscr;
  2493. pscr = rdlp(mp, PORT_SERIAL_CONTROL);
  2494. if (pscr & SERIAL_PORT_ENABLE) {
  2495. pscr &= ~SERIAL_PORT_ENABLE;
  2496. wrlp(mp, PORT_SERIAL_CONTROL, pscr);
  2497. }
  2498. pscr = MAX_RX_PACKET_9700BYTE | SERIAL_PORT_CONTROL_RESERVED;
  2499. if (!dev->phydev) {
  2500. pscr |= DISABLE_AUTO_NEG_SPEED_GMII;
  2501. if (speed == SPEED_1000)
  2502. pscr |= SET_GMII_SPEED_TO_1000;
  2503. else if (speed == SPEED_100)
  2504. pscr |= SET_MII_SPEED_TO_100;
  2505. pscr |= DISABLE_AUTO_NEG_FOR_FLOW_CTRL;
  2506. pscr |= DISABLE_AUTO_NEG_FOR_DUPLEX;
  2507. if (duplex == DUPLEX_FULL)
  2508. pscr |= SET_FULL_DUPLEX_MODE;
  2509. }
  2510. wrlp(mp, PORT_SERIAL_CONTROL, pscr);
  2511. }
  2512. static const struct net_device_ops mv643xx_eth_netdev_ops = {
  2513. .ndo_open = mv643xx_eth_open,
  2514. .ndo_stop = mv643xx_eth_stop,
  2515. .ndo_start_xmit = mv643xx_eth_xmit,
  2516. .ndo_set_rx_mode = mv643xx_eth_set_rx_mode,
  2517. .ndo_set_mac_address = mv643xx_eth_set_mac_address,
  2518. .ndo_validate_addr = eth_validate_addr,
  2519. .ndo_eth_ioctl = mv643xx_eth_ioctl,
  2520. .ndo_change_mtu = mv643xx_eth_change_mtu,
  2521. .ndo_set_features = mv643xx_eth_set_features,
  2522. .ndo_tx_timeout = mv643xx_eth_tx_timeout,
  2523. .ndo_get_stats = mv643xx_eth_get_stats,
  2524. #ifdef CONFIG_NET_POLL_CONTROLLER
  2525. .ndo_poll_controller = mv643xx_eth_netpoll,
  2526. #endif
  2527. };
  2528. static int mv643xx_eth_probe(struct platform_device *pdev)
  2529. {
  2530. struct mv643xx_eth_platform_data *pd;
  2531. struct mv643xx_eth_private *mp;
  2532. struct net_device *dev;
  2533. struct phy_device *phydev = NULL;
  2534. u32 psc1r;
  2535. int err, irq;
  2536. pd = dev_get_platdata(&pdev->dev);
  2537. if (pd == NULL) {
  2538. dev_err(&pdev->dev, "no mv643xx_eth_platform_data\n");
  2539. return -ENODEV;
  2540. }
  2541. if (pd->shared == NULL) {
  2542. dev_err(&pdev->dev, "no mv643xx_eth_platform_data->shared\n");
  2543. return -ENODEV;
  2544. }
  2545. dev = alloc_etherdev_mq(sizeof(struct mv643xx_eth_private), 8);
  2546. if (!dev)
  2547. return -ENOMEM;
  2548. SET_NETDEV_DEV(dev, &pdev->dev);
  2549. mp = netdev_priv(dev);
  2550. platform_set_drvdata(pdev, mp);
  2551. mp->shared = platform_get_drvdata(pd->shared);
  2552. mp->base = mp->shared->base + 0x0400 + (pd->port_number << 10);
  2553. mp->port_num = pd->port_number;
  2554. mp->dev = dev;
  2555. if (of_device_is_compatible(pdev->dev.of_node,
  2556. "marvell,kirkwood-eth-port")) {
  2557. psc1r = rdlp(mp, PORT_SERIAL_CONTROL1);
  2558. /* Kirkwood resets some registers on gated clocks. Especially
  2559. * CLK125_BYPASS_EN must be cleared but is not available on
  2560. * all other SoCs/System Controllers using this driver.
  2561. */
  2562. psc1r &= ~CLK125_BYPASS_EN;
  2563. /* On Kirkwood with two Ethernet controllers, if both of them
  2564. * have RGMII_EN disabled, the first controller will be in GMII
  2565. * mode and the second one is effectively disabled, instead of
  2566. * two MII interfaces.
  2567. *
  2568. * To enable GMII in the first controller, the second one must
  2569. * also be configured (and may be enabled) with RGMII_EN
  2570. * disabled too, even though it cannot be used at all.
  2571. */
  2572. switch (pd->interface) {
  2573. /* Use internal to denote second controller being disabled */
  2574. case PHY_INTERFACE_MODE_INTERNAL:
  2575. case PHY_INTERFACE_MODE_MII:
  2576. case PHY_INTERFACE_MODE_GMII:
  2577. psc1r &= ~RGMII_EN;
  2578. break;
  2579. case PHY_INTERFACE_MODE_RGMII:
  2580. case PHY_INTERFACE_MODE_RGMII_ID:
  2581. case PHY_INTERFACE_MODE_RGMII_RXID:
  2582. case PHY_INTERFACE_MODE_RGMII_TXID:
  2583. psc1r |= RGMII_EN;
  2584. break;
  2585. default:
  2586. /* Unknown; don't touch */
  2587. break;
  2588. }
  2589. wrlp(mp, PORT_SERIAL_CONTROL1, psc1r);
  2590. }
  2591. /*
  2592. * Start with a default rate, and if there is a clock, allow
  2593. * it to override the default.
  2594. */
  2595. mp->t_clk = 133000000;
  2596. mp->clk = devm_clk_get(&pdev->dev, NULL);
  2597. if (!IS_ERR(mp->clk)) {
  2598. clk_prepare_enable(mp->clk);
  2599. mp->t_clk = clk_get_rate(mp->clk);
  2600. } else if (!IS_ERR(mp->shared->clk)) {
  2601. mp->t_clk = clk_get_rate(mp->shared->clk);
  2602. }
  2603. set_params(mp, pd);
  2604. netif_set_real_num_tx_queues(dev, mp->txq_count);
  2605. netif_set_real_num_rx_queues(dev, mp->rxq_count);
  2606. err = 0;
  2607. if (pd->phy_node) {
  2608. phydev = of_phy_connect(mp->dev, pd->phy_node,
  2609. mv643xx_eth_adjust_link, 0,
  2610. get_phy_mode(mp));
  2611. if (!phydev)
  2612. err = -ENODEV;
  2613. else
  2614. phy_addr_set(mp, phydev->mdio.addr);
  2615. } else if (pd->phy_addr != MV643XX_ETH_PHY_NONE) {
  2616. phydev = phy_scan(mp, pd->phy_addr);
  2617. if (IS_ERR(phydev))
  2618. err = PTR_ERR(phydev);
  2619. else
  2620. phy_init(mp, pd->speed, pd->duplex);
  2621. }
  2622. if (err == -ENODEV) {
  2623. err = -EPROBE_DEFER;
  2624. goto out;
  2625. }
  2626. if (err)
  2627. goto out;
  2628. dev->ethtool_ops = &mv643xx_eth_ethtool_ops;
  2629. init_pscr(mp, pd->speed, pd->duplex);
  2630. mib_counters_clear(mp);
  2631. timer_setup(&mp->mib_counters_timer, mib_counters_timer_wrapper, 0);
  2632. mp->mib_counters_timer.expires = jiffies + 30 * HZ;
  2633. spin_lock_init(&mp->mib_counters_lock);
  2634. INIT_WORK(&mp->tx_timeout_task, tx_timeout_task);
  2635. netif_napi_add(dev, &mp->napi, mv643xx_eth_poll);
  2636. timer_setup(&mp->rx_oom, oom_timer_wrapper, 0);
  2637. irq = platform_get_irq(pdev, 0);
  2638. if (WARN_ON(irq < 0)) {
  2639. err = irq;
  2640. goto out;
  2641. }
  2642. dev->irq = irq;
  2643. dev->netdev_ops = &mv643xx_eth_netdev_ops;
  2644. dev->watchdog_timeo = 2 * HZ;
  2645. dev->base_addr = 0;
  2646. dev->features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO;
  2647. dev->vlan_features = dev->features;
  2648. dev->features |= NETIF_F_RXCSUM;
  2649. dev->hw_features = dev->features;
  2650. dev->priv_flags |= IFF_UNICAST_FLT;
  2651. netif_set_tso_max_segs(dev, MV643XX_MAX_TSO_SEGS);
  2652. /* MTU range: 64 - 9500 */
  2653. dev->min_mtu = 64;
  2654. dev->max_mtu = 9500;
  2655. if (mp->shared->win_protect)
  2656. wrl(mp, WINDOW_PROTECT(mp->port_num), mp->shared->win_protect);
  2657. netif_carrier_off(dev);
  2658. wrlp(mp, SDMA_CONFIG, PORT_SDMA_CONFIG_DEFAULT_VALUE);
  2659. set_rx_coal(mp, 250);
  2660. set_tx_coal(mp, 0);
  2661. err = register_netdev(dev);
  2662. if (err)
  2663. goto out;
  2664. netdev_notice(dev, "port %d with MAC address %pM\n",
  2665. mp->port_num, dev->dev_addr);
  2666. if (mp->tx_desc_sram_size > 0)
  2667. netdev_notice(dev, "configured with sram\n");
  2668. return 0;
  2669. out:
  2670. if (!IS_ERR(mp->clk))
  2671. clk_disable_unprepare(mp->clk);
  2672. free_netdev(dev);
  2673. return err;
  2674. }
  2675. static void mv643xx_eth_remove(struct platform_device *pdev)
  2676. {
  2677. struct mv643xx_eth_private *mp = platform_get_drvdata(pdev);
  2678. struct net_device *dev = mp->dev;
  2679. unregister_netdev(mp->dev);
  2680. if (dev->phydev)
  2681. phy_disconnect(dev->phydev);
  2682. cancel_work_sync(&mp->tx_timeout_task);
  2683. if (!IS_ERR(mp->clk))
  2684. clk_disable_unprepare(mp->clk);
  2685. free_netdev(mp->dev);
  2686. }
  2687. static void mv643xx_eth_shutdown(struct platform_device *pdev)
  2688. {
  2689. struct mv643xx_eth_private *mp = platform_get_drvdata(pdev);
  2690. /* Mask all interrupts on ethernet port */
  2691. wrlp(mp, INT_MASK, 0);
  2692. rdlp(mp, INT_MASK);
  2693. if (netif_running(mp->dev))
  2694. port_reset(mp);
  2695. }
  2696. static struct platform_driver mv643xx_eth_driver = {
  2697. .probe = mv643xx_eth_probe,
  2698. .remove = mv643xx_eth_remove,
  2699. .shutdown = mv643xx_eth_shutdown,
  2700. .driver = {
  2701. .name = MV643XX_ETH_NAME,
  2702. },
  2703. };
  2704. static struct platform_driver * const drivers[] = {
  2705. &mv643xx_eth_shared_driver,
  2706. &mv643xx_eth_driver,
  2707. };
  2708. static int __init mv643xx_eth_init_module(void)
  2709. {
  2710. return platform_register_drivers(drivers, ARRAY_SIZE(drivers));
  2711. }
  2712. module_init(mv643xx_eth_init_module);
  2713. static void __exit mv643xx_eth_cleanup_module(void)
  2714. {
  2715. platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
  2716. }
  2717. module_exit(mv643xx_eth_cleanup_module);
  2718. MODULE_AUTHOR("Rabeeh Khoury, Assaf Hoffman, Matthew Dharm, "
  2719. "Manish Lachwani, Dale Farnsworth and Lennert Buytenhek");
  2720. MODULE_DESCRIPTION("Ethernet driver for Marvell MV643XX");
  2721. MODULE_LICENSE("GPL");
  2722. MODULE_ALIAS("platform:" MV643XX_ETH_SHARED_NAME);
  2723. MODULE_ALIAS("platform:" MV643XX_ETH_NAME);