bcmsysport.c 74 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821
  1. /*
  2. * Broadcom BCM7xxx System Port Ethernet MAC driver
  3. *
  4. * Copyright (C) 2014 Broadcom Corporation
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  11. #include <linux/init.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/module.h>
  14. #include <linux/kernel.h>
  15. #include <linux/netdevice.h>
  16. #include <linux/etherdevice.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/of.h>
  19. #include <linux/of_net.h>
  20. #include <linux/of_mdio.h>
  21. #include <linux/phy.h>
  22. #include <linux/phy_fixed.h>
  23. #include <net/dsa.h>
  24. #include <net/ip.h>
  25. #include <net/ipv6.h>
  26. #include "bcmsysport.h"
  27. /* I/O accessors register helpers */
  28. #define BCM_SYSPORT_IO_MACRO(name, offset) \
  29. static inline u32 name##_readl(struct bcm_sysport_priv *priv, u32 off) \
  30. { \
  31. u32 reg = readl_relaxed(priv->base + offset + off); \
  32. return reg; \
  33. } \
  34. static inline void name##_writel(struct bcm_sysport_priv *priv, \
  35. u32 val, u32 off) \
  36. { \
  37. writel_relaxed(val, priv->base + offset + off); \
  38. } \
  39. BCM_SYSPORT_IO_MACRO(intrl2_0, SYS_PORT_INTRL2_0_OFFSET);
  40. BCM_SYSPORT_IO_MACRO(intrl2_1, SYS_PORT_INTRL2_1_OFFSET);
  41. BCM_SYSPORT_IO_MACRO(umac, SYS_PORT_UMAC_OFFSET);
  42. BCM_SYSPORT_IO_MACRO(gib, SYS_PORT_GIB_OFFSET);
  43. BCM_SYSPORT_IO_MACRO(tdma, SYS_PORT_TDMA_OFFSET);
  44. BCM_SYSPORT_IO_MACRO(rxchk, SYS_PORT_RXCHK_OFFSET);
  45. BCM_SYSPORT_IO_MACRO(txchk, SYS_PORT_TXCHK_OFFSET);
  46. BCM_SYSPORT_IO_MACRO(rbuf, SYS_PORT_RBUF_OFFSET);
  47. BCM_SYSPORT_IO_MACRO(tbuf, SYS_PORT_TBUF_OFFSET);
  48. BCM_SYSPORT_IO_MACRO(topctrl, SYS_PORT_TOPCTRL_OFFSET);
  49. /* On SYSTEMPORT Lite, any register after RDMA_STATUS has the exact
  50. * same layout, except it has been moved by 4 bytes up, *sigh*
  51. */
  52. static inline u32 rdma_readl(struct bcm_sysport_priv *priv, u32 off)
  53. {
  54. if (priv->is_lite && off >= RDMA_STATUS)
  55. off += 4;
  56. return readl_relaxed(priv->base + SYS_PORT_RDMA_OFFSET + off);
  57. }
  58. static inline void rdma_writel(struct bcm_sysport_priv *priv, u32 val, u32 off)
  59. {
  60. if (priv->is_lite && off >= RDMA_STATUS)
  61. off += 4;
  62. writel_relaxed(val, priv->base + SYS_PORT_RDMA_OFFSET + off);
  63. }
  64. static inline u32 tdma_control_bit(struct bcm_sysport_priv *priv, u32 bit)
  65. {
  66. if (!priv->is_lite) {
  67. return BIT(bit);
  68. } else {
  69. if (bit >= ACB_ALGO)
  70. return BIT(bit + 1);
  71. else
  72. return BIT(bit);
  73. }
  74. }
  75. /* L2-interrupt masking/unmasking helpers, does automatic saving of the applied
  76. * mask in a software copy to avoid CPU_MASK_STATUS reads in hot-paths.
  77. */
  78. #define BCM_SYSPORT_INTR_L2(which) \
  79. static inline void intrl2_##which##_mask_clear(struct bcm_sysport_priv *priv, \
  80. u32 mask) \
  81. { \
  82. priv->irq##which##_mask &= ~(mask); \
  83. intrl2_##which##_writel(priv, mask, INTRL2_CPU_MASK_CLEAR); \
  84. } \
  85. static inline void intrl2_##which##_mask_set(struct bcm_sysport_priv *priv, \
  86. u32 mask) \
  87. { \
  88. intrl2_## which##_writel(priv, mask, INTRL2_CPU_MASK_SET); \
  89. priv->irq##which##_mask |= (mask); \
  90. } \
  91. BCM_SYSPORT_INTR_L2(0)
  92. BCM_SYSPORT_INTR_L2(1)
  93. /* Register accesses to GISB/RBUS registers are expensive (few hundred
  94. * nanoseconds), so keep the check for 64-bits explicit here to save
  95. * one register write per-packet on 32-bits platforms.
  96. */
  97. static inline void dma_desc_set_addr(struct bcm_sysport_priv *priv,
  98. void __iomem *d,
  99. dma_addr_t addr)
  100. {
  101. #ifdef CONFIG_PHYS_ADDR_T_64BIT
  102. writel_relaxed(upper_32_bits(addr) & DESC_ADDR_HI_MASK,
  103. d + DESC_ADDR_HI_STATUS_LEN);
  104. #endif
  105. writel_relaxed(lower_32_bits(addr), d + DESC_ADDR_LO);
  106. }
  107. static inline void tdma_port_write_desc_addr(struct bcm_sysport_priv *priv,
  108. struct dma_desc *desc,
  109. unsigned int port)
  110. {
  111. /* Ports are latched, so write upper address first */
  112. tdma_writel(priv, desc->addr_status_len, TDMA_WRITE_PORT_HI(port));
  113. tdma_writel(priv, desc->addr_lo, TDMA_WRITE_PORT_LO(port));
  114. }
  115. /* Ethtool operations */
  116. static int bcm_sysport_set_rx_csum(struct net_device *dev,
  117. netdev_features_t wanted)
  118. {
  119. struct bcm_sysport_priv *priv = netdev_priv(dev);
  120. u32 reg;
  121. priv->rx_chk_en = !!(wanted & NETIF_F_RXCSUM);
  122. reg = rxchk_readl(priv, RXCHK_CONTROL);
  123. /* Clear L2 header checks, which would prevent BPDUs
  124. * from being received.
  125. */
  126. reg &= ~RXCHK_L2_HDR_DIS;
  127. if (priv->rx_chk_en)
  128. reg |= RXCHK_EN;
  129. else
  130. reg &= ~RXCHK_EN;
  131. /* If UniMAC forwards CRC, we need to skip over it to get
  132. * a valid CHK bit to be set in the per-packet status word
  133. */
  134. if (priv->rx_chk_en && priv->crc_fwd)
  135. reg |= RXCHK_SKIP_FCS;
  136. else
  137. reg &= ~RXCHK_SKIP_FCS;
  138. /* If Broadcom tags are enabled (e.g: using a switch), make
  139. * sure we tell the RXCHK hardware to expect a 4-bytes Broadcom
  140. * tag after the Ethernet MAC Source Address.
  141. */
  142. if (netdev_uses_dsa(dev))
  143. reg |= RXCHK_BRCM_TAG_EN;
  144. else
  145. reg &= ~RXCHK_BRCM_TAG_EN;
  146. rxchk_writel(priv, reg, RXCHK_CONTROL);
  147. return 0;
  148. }
  149. static int bcm_sysport_set_tx_csum(struct net_device *dev,
  150. netdev_features_t wanted)
  151. {
  152. struct bcm_sysport_priv *priv = netdev_priv(dev);
  153. u32 reg;
  154. /* Hardware transmit checksum requires us to enable the Transmit status
  155. * block prepended to the packet contents
  156. */
  157. priv->tsb_en = !!(wanted & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM));
  158. reg = tdma_readl(priv, TDMA_CONTROL);
  159. if (priv->tsb_en)
  160. reg |= tdma_control_bit(priv, TSB_EN);
  161. else
  162. reg &= ~tdma_control_bit(priv, TSB_EN);
  163. tdma_writel(priv, reg, TDMA_CONTROL);
  164. return 0;
  165. }
  166. static int bcm_sysport_set_features(struct net_device *dev,
  167. netdev_features_t features)
  168. {
  169. netdev_features_t changed = features ^ dev->features;
  170. netdev_features_t wanted = dev->wanted_features;
  171. int ret = 0;
  172. if (changed & NETIF_F_RXCSUM)
  173. ret = bcm_sysport_set_rx_csum(dev, wanted);
  174. if (changed & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM))
  175. ret = bcm_sysport_set_tx_csum(dev, wanted);
  176. return ret;
  177. }
  178. /* Hardware counters must be kept in sync because the order/offset
  179. * is important here (order in structure declaration = order in hardware)
  180. */
  181. static const struct bcm_sysport_stats bcm_sysport_gstrings_stats[] = {
  182. /* general stats */
  183. STAT_NETDEV64(rx_packets),
  184. STAT_NETDEV64(tx_packets),
  185. STAT_NETDEV64(rx_bytes),
  186. STAT_NETDEV64(tx_bytes),
  187. STAT_NETDEV(rx_errors),
  188. STAT_NETDEV(tx_errors),
  189. STAT_NETDEV(rx_dropped),
  190. STAT_NETDEV(tx_dropped),
  191. STAT_NETDEV(multicast),
  192. /* UniMAC RSV counters */
  193. STAT_MIB_RX("rx_64_octets", mib.rx.pkt_cnt.cnt_64),
  194. STAT_MIB_RX("rx_65_127_oct", mib.rx.pkt_cnt.cnt_127),
  195. STAT_MIB_RX("rx_128_255_oct", mib.rx.pkt_cnt.cnt_255),
  196. STAT_MIB_RX("rx_256_511_oct", mib.rx.pkt_cnt.cnt_511),
  197. STAT_MIB_RX("rx_512_1023_oct", mib.rx.pkt_cnt.cnt_1023),
  198. STAT_MIB_RX("rx_1024_1518_oct", mib.rx.pkt_cnt.cnt_1518),
  199. STAT_MIB_RX("rx_vlan_1519_1522_oct", mib.rx.pkt_cnt.cnt_mgv),
  200. STAT_MIB_RX("rx_1522_2047_oct", mib.rx.pkt_cnt.cnt_2047),
  201. STAT_MIB_RX("rx_2048_4095_oct", mib.rx.pkt_cnt.cnt_4095),
  202. STAT_MIB_RX("rx_4096_9216_oct", mib.rx.pkt_cnt.cnt_9216),
  203. STAT_MIB_RX("rx_pkts", mib.rx.pkt),
  204. STAT_MIB_RX("rx_bytes", mib.rx.bytes),
  205. STAT_MIB_RX("rx_multicast", mib.rx.mca),
  206. STAT_MIB_RX("rx_broadcast", mib.rx.bca),
  207. STAT_MIB_RX("rx_fcs", mib.rx.fcs),
  208. STAT_MIB_RX("rx_control", mib.rx.cf),
  209. STAT_MIB_RX("rx_pause", mib.rx.pf),
  210. STAT_MIB_RX("rx_unknown", mib.rx.uo),
  211. STAT_MIB_RX("rx_align", mib.rx.aln),
  212. STAT_MIB_RX("rx_outrange", mib.rx.flr),
  213. STAT_MIB_RX("rx_code", mib.rx.cde),
  214. STAT_MIB_RX("rx_carrier", mib.rx.fcr),
  215. STAT_MIB_RX("rx_oversize", mib.rx.ovr),
  216. STAT_MIB_RX("rx_jabber", mib.rx.jbr),
  217. STAT_MIB_RX("rx_mtu_err", mib.rx.mtue),
  218. STAT_MIB_RX("rx_good_pkts", mib.rx.pok),
  219. STAT_MIB_RX("rx_unicast", mib.rx.uc),
  220. STAT_MIB_RX("rx_ppp", mib.rx.ppp),
  221. STAT_MIB_RX("rx_crc", mib.rx.rcrc),
  222. /* UniMAC TSV counters */
  223. STAT_MIB_TX("tx_64_octets", mib.tx.pkt_cnt.cnt_64),
  224. STAT_MIB_TX("tx_65_127_oct", mib.tx.pkt_cnt.cnt_127),
  225. STAT_MIB_TX("tx_128_255_oct", mib.tx.pkt_cnt.cnt_255),
  226. STAT_MIB_TX("tx_256_511_oct", mib.tx.pkt_cnt.cnt_511),
  227. STAT_MIB_TX("tx_512_1023_oct", mib.tx.pkt_cnt.cnt_1023),
  228. STAT_MIB_TX("tx_1024_1518_oct", mib.tx.pkt_cnt.cnt_1518),
  229. STAT_MIB_TX("tx_vlan_1519_1522_oct", mib.tx.pkt_cnt.cnt_mgv),
  230. STAT_MIB_TX("tx_1522_2047_oct", mib.tx.pkt_cnt.cnt_2047),
  231. STAT_MIB_TX("tx_2048_4095_oct", mib.tx.pkt_cnt.cnt_4095),
  232. STAT_MIB_TX("tx_4096_9216_oct", mib.tx.pkt_cnt.cnt_9216),
  233. STAT_MIB_TX("tx_pkts", mib.tx.pkts),
  234. STAT_MIB_TX("tx_multicast", mib.tx.mca),
  235. STAT_MIB_TX("tx_broadcast", mib.tx.bca),
  236. STAT_MIB_TX("tx_pause", mib.tx.pf),
  237. STAT_MIB_TX("tx_control", mib.tx.cf),
  238. STAT_MIB_TX("tx_fcs_err", mib.tx.fcs),
  239. STAT_MIB_TX("tx_oversize", mib.tx.ovr),
  240. STAT_MIB_TX("tx_defer", mib.tx.drf),
  241. STAT_MIB_TX("tx_excess_defer", mib.tx.edf),
  242. STAT_MIB_TX("tx_single_col", mib.tx.scl),
  243. STAT_MIB_TX("tx_multi_col", mib.tx.mcl),
  244. STAT_MIB_TX("tx_late_col", mib.tx.lcl),
  245. STAT_MIB_TX("tx_excess_col", mib.tx.ecl),
  246. STAT_MIB_TX("tx_frags", mib.tx.frg),
  247. STAT_MIB_TX("tx_total_col", mib.tx.ncl),
  248. STAT_MIB_TX("tx_jabber", mib.tx.jbr),
  249. STAT_MIB_TX("tx_bytes", mib.tx.bytes),
  250. STAT_MIB_TX("tx_good_pkts", mib.tx.pok),
  251. STAT_MIB_TX("tx_unicast", mib.tx.uc),
  252. /* UniMAC RUNT counters */
  253. STAT_RUNT("rx_runt_pkts", mib.rx_runt_cnt),
  254. STAT_RUNT("rx_runt_valid_fcs", mib.rx_runt_fcs),
  255. STAT_RUNT("rx_runt_inval_fcs_align", mib.rx_runt_fcs_align),
  256. STAT_RUNT("rx_runt_bytes", mib.rx_runt_bytes),
  257. /* RXCHK misc statistics */
  258. STAT_RXCHK("rxchk_bad_csum", mib.rxchk_bad_csum, RXCHK_BAD_CSUM_CNTR),
  259. STAT_RXCHK("rxchk_other_pkt_disc", mib.rxchk_other_pkt_disc,
  260. RXCHK_OTHER_DISC_CNTR),
  261. /* RBUF misc statistics */
  262. STAT_RBUF("rbuf_ovflow_cnt", mib.rbuf_ovflow_cnt, RBUF_OVFL_DISC_CNTR),
  263. STAT_RBUF("rbuf_err_cnt", mib.rbuf_err_cnt, RBUF_ERR_PKT_CNTR),
  264. STAT_MIB_SOFT("alloc_rx_buff_failed", mib.alloc_rx_buff_failed),
  265. STAT_MIB_SOFT("rx_dma_failed", mib.rx_dma_failed),
  266. STAT_MIB_SOFT("tx_dma_failed", mib.tx_dma_failed),
  267. /* Per TX-queue statistics are dynamically appended */
  268. };
  269. #define BCM_SYSPORT_STATS_LEN ARRAY_SIZE(bcm_sysport_gstrings_stats)
  270. static void bcm_sysport_get_drvinfo(struct net_device *dev,
  271. struct ethtool_drvinfo *info)
  272. {
  273. strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
  274. strlcpy(info->version, "0.1", sizeof(info->version));
  275. strlcpy(info->bus_info, "platform", sizeof(info->bus_info));
  276. }
  277. static u32 bcm_sysport_get_msglvl(struct net_device *dev)
  278. {
  279. struct bcm_sysport_priv *priv = netdev_priv(dev);
  280. return priv->msg_enable;
  281. }
  282. static void bcm_sysport_set_msglvl(struct net_device *dev, u32 enable)
  283. {
  284. struct bcm_sysport_priv *priv = netdev_priv(dev);
  285. priv->msg_enable = enable;
  286. }
  287. static inline bool bcm_sysport_lite_stat_valid(enum bcm_sysport_stat_type type)
  288. {
  289. switch (type) {
  290. case BCM_SYSPORT_STAT_NETDEV:
  291. case BCM_SYSPORT_STAT_NETDEV64:
  292. case BCM_SYSPORT_STAT_RXCHK:
  293. case BCM_SYSPORT_STAT_RBUF:
  294. case BCM_SYSPORT_STAT_SOFT:
  295. return true;
  296. default:
  297. return false;
  298. }
  299. }
  300. static int bcm_sysport_get_sset_count(struct net_device *dev, int string_set)
  301. {
  302. struct bcm_sysport_priv *priv = netdev_priv(dev);
  303. const struct bcm_sysport_stats *s;
  304. unsigned int i, j;
  305. switch (string_set) {
  306. case ETH_SS_STATS:
  307. for (i = 0, j = 0; i < BCM_SYSPORT_STATS_LEN; i++) {
  308. s = &bcm_sysport_gstrings_stats[i];
  309. if (priv->is_lite &&
  310. !bcm_sysport_lite_stat_valid(s->type))
  311. continue;
  312. j++;
  313. }
  314. /* Include per-queue statistics */
  315. return j + dev->num_tx_queues * NUM_SYSPORT_TXQ_STAT;
  316. default:
  317. return -EOPNOTSUPP;
  318. }
  319. }
  320. static void bcm_sysport_get_strings(struct net_device *dev,
  321. u32 stringset, u8 *data)
  322. {
  323. struct bcm_sysport_priv *priv = netdev_priv(dev);
  324. const struct bcm_sysport_stats *s;
  325. char buf[128];
  326. int i, j;
  327. switch (stringset) {
  328. case ETH_SS_STATS:
  329. for (i = 0, j = 0; i < BCM_SYSPORT_STATS_LEN; i++) {
  330. s = &bcm_sysport_gstrings_stats[i];
  331. if (priv->is_lite &&
  332. !bcm_sysport_lite_stat_valid(s->type))
  333. continue;
  334. memcpy(data + j * ETH_GSTRING_LEN, s->stat_string,
  335. ETH_GSTRING_LEN);
  336. j++;
  337. }
  338. for (i = 0; i < dev->num_tx_queues; i++) {
  339. snprintf(buf, sizeof(buf), "txq%d_packets", i);
  340. memcpy(data + j * ETH_GSTRING_LEN, buf,
  341. ETH_GSTRING_LEN);
  342. j++;
  343. snprintf(buf, sizeof(buf), "txq%d_bytes", i);
  344. memcpy(data + j * ETH_GSTRING_LEN, buf,
  345. ETH_GSTRING_LEN);
  346. j++;
  347. }
  348. break;
  349. default:
  350. break;
  351. }
  352. }
  353. static void bcm_sysport_update_mib_counters(struct bcm_sysport_priv *priv)
  354. {
  355. int i, j = 0;
  356. for (i = 0; i < BCM_SYSPORT_STATS_LEN; i++) {
  357. const struct bcm_sysport_stats *s;
  358. u8 offset = 0;
  359. u32 val = 0;
  360. char *p;
  361. s = &bcm_sysport_gstrings_stats[i];
  362. switch (s->type) {
  363. case BCM_SYSPORT_STAT_NETDEV:
  364. case BCM_SYSPORT_STAT_NETDEV64:
  365. case BCM_SYSPORT_STAT_SOFT:
  366. continue;
  367. case BCM_SYSPORT_STAT_MIB_RX:
  368. case BCM_SYSPORT_STAT_MIB_TX:
  369. case BCM_SYSPORT_STAT_RUNT:
  370. if (priv->is_lite)
  371. continue;
  372. if (s->type != BCM_SYSPORT_STAT_MIB_RX)
  373. offset = UMAC_MIB_STAT_OFFSET;
  374. val = umac_readl(priv, UMAC_MIB_START + j + offset);
  375. break;
  376. case BCM_SYSPORT_STAT_RXCHK:
  377. val = rxchk_readl(priv, s->reg_offset);
  378. if (val == ~0)
  379. rxchk_writel(priv, 0, s->reg_offset);
  380. break;
  381. case BCM_SYSPORT_STAT_RBUF:
  382. val = rbuf_readl(priv, s->reg_offset);
  383. if (val == ~0)
  384. rbuf_writel(priv, 0, s->reg_offset);
  385. break;
  386. }
  387. j += s->stat_sizeof;
  388. p = (char *)priv + s->stat_offset;
  389. *(u32 *)p = val;
  390. }
  391. netif_dbg(priv, hw, priv->netdev, "updated MIB counters\n");
  392. }
  393. static void bcm_sysport_update_tx_stats(struct bcm_sysport_priv *priv,
  394. u64 *tx_bytes, u64 *tx_packets)
  395. {
  396. struct bcm_sysport_tx_ring *ring;
  397. u64 bytes = 0, packets = 0;
  398. unsigned int start;
  399. unsigned int q;
  400. for (q = 0; q < priv->netdev->num_tx_queues; q++) {
  401. ring = &priv->tx_rings[q];
  402. do {
  403. start = u64_stats_fetch_begin_irq(&priv->syncp);
  404. bytes = ring->bytes;
  405. packets = ring->packets;
  406. } while (u64_stats_fetch_retry_irq(&priv->syncp, start));
  407. *tx_bytes += bytes;
  408. *tx_packets += packets;
  409. }
  410. }
  411. static void bcm_sysport_get_stats(struct net_device *dev,
  412. struct ethtool_stats *stats, u64 *data)
  413. {
  414. struct bcm_sysport_priv *priv = netdev_priv(dev);
  415. struct bcm_sysport_stats64 *stats64 = &priv->stats64;
  416. struct u64_stats_sync *syncp = &priv->syncp;
  417. struct bcm_sysport_tx_ring *ring;
  418. u64 tx_bytes = 0, tx_packets = 0;
  419. unsigned int start;
  420. int i, j;
  421. if (netif_running(dev)) {
  422. bcm_sysport_update_mib_counters(priv);
  423. bcm_sysport_update_tx_stats(priv, &tx_bytes, &tx_packets);
  424. stats64->tx_bytes = tx_bytes;
  425. stats64->tx_packets = tx_packets;
  426. }
  427. for (i = 0, j = 0; i < BCM_SYSPORT_STATS_LEN; i++) {
  428. const struct bcm_sysport_stats *s;
  429. char *p;
  430. s = &bcm_sysport_gstrings_stats[i];
  431. if (s->type == BCM_SYSPORT_STAT_NETDEV)
  432. p = (char *)&dev->stats;
  433. else if (s->type == BCM_SYSPORT_STAT_NETDEV64)
  434. p = (char *)stats64;
  435. else
  436. p = (char *)priv;
  437. if (priv->is_lite && !bcm_sysport_lite_stat_valid(s->type))
  438. continue;
  439. p += s->stat_offset;
  440. if (s->stat_sizeof == sizeof(u64) &&
  441. s->type == BCM_SYSPORT_STAT_NETDEV64) {
  442. do {
  443. start = u64_stats_fetch_begin_irq(syncp);
  444. data[i] = *(u64 *)p;
  445. } while (u64_stats_fetch_retry_irq(syncp, start));
  446. } else
  447. data[i] = *(u32 *)p;
  448. j++;
  449. }
  450. /* For SYSTEMPORT Lite since we have holes in our statistics, j would
  451. * be equal to BCM_SYSPORT_STATS_LEN at the end of the loop, but it
  452. * needs to point to how many total statistics we have minus the
  453. * number of per TX queue statistics
  454. */
  455. j = bcm_sysport_get_sset_count(dev, ETH_SS_STATS) -
  456. dev->num_tx_queues * NUM_SYSPORT_TXQ_STAT;
  457. for (i = 0; i < dev->num_tx_queues; i++) {
  458. ring = &priv->tx_rings[i];
  459. data[j] = ring->packets;
  460. j++;
  461. data[j] = ring->bytes;
  462. j++;
  463. }
  464. }
  465. static void bcm_sysport_get_wol(struct net_device *dev,
  466. struct ethtool_wolinfo *wol)
  467. {
  468. struct bcm_sysport_priv *priv = netdev_priv(dev);
  469. wol->supported = WAKE_MAGIC | WAKE_MAGICSECURE | WAKE_FILTER;
  470. wol->wolopts = priv->wolopts;
  471. if (!(priv->wolopts & WAKE_MAGICSECURE))
  472. return;
  473. memcpy(wol->sopass, priv->sopass, sizeof(priv->sopass));
  474. }
  475. static int bcm_sysport_set_wol(struct net_device *dev,
  476. struct ethtool_wolinfo *wol)
  477. {
  478. struct bcm_sysport_priv *priv = netdev_priv(dev);
  479. struct device *kdev = &priv->pdev->dev;
  480. u32 supported = WAKE_MAGIC | WAKE_MAGICSECURE | WAKE_FILTER;
  481. if (!device_can_wakeup(kdev))
  482. return -ENOTSUPP;
  483. if (wol->wolopts & ~supported)
  484. return -EINVAL;
  485. if (wol->wolopts & WAKE_MAGICSECURE)
  486. memcpy(priv->sopass, wol->sopass, sizeof(priv->sopass));
  487. /* Flag the device and relevant IRQ as wakeup capable */
  488. if (wol->wolopts) {
  489. device_set_wakeup_enable(kdev, 1);
  490. if (priv->wol_irq_disabled)
  491. enable_irq_wake(priv->wol_irq);
  492. priv->wol_irq_disabled = 0;
  493. } else {
  494. device_set_wakeup_enable(kdev, 0);
  495. /* Avoid unbalanced disable_irq_wake calls */
  496. if (!priv->wol_irq_disabled)
  497. disable_irq_wake(priv->wol_irq);
  498. priv->wol_irq_disabled = 1;
  499. }
  500. priv->wolopts = wol->wolopts;
  501. return 0;
  502. }
  503. static void bcm_sysport_set_rx_coalesce(struct bcm_sysport_priv *priv,
  504. u32 usecs, u32 pkts)
  505. {
  506. u32 reg;
  507. reg = rdma_readl(priv, RDMA_MBDONE_INTR);
  508. reg &= ~(RDMA_INTR_THRESH_MASK |
  509. RDMA_TIMEOUT_MASK << RDMA_TIMEOUT_SHIFT);
  510. reg |= pkts;
  511. reg |= DIV_ROUND_UP(usecs * 1000, 8192) << RDMA_TIMEOUT_SHIFT;
  512. rdma_writel(priv, reg, RDMA_MBDONE_INTR);
  513. }
  514. static void bcm_sysport_set_tx_coalesce(struct bcm_sysport_tx_ring *ring,
  515. struct ethtool_coalesce *ec)
  516. {
  517. struct bcm_sysport_priv *priv = ring->priv;
  518. u32 reg;
  519. reg = tdma_readl(priv, TDMA_DESC_RING_INTR_CONTROL(ring->index));
  520. reg &= ~(RING_INTR_THRESH_MASK |
  521. RING_TIMEOUT_MASK << RING_TIMEOUT_SHIFT);
  522. reg |= ec->tx_max_coalesced_frames;
  523. reg |= DIV_ROUND_UP(ec->tx_coalesce_usecs * 1000, 8192) <<
  524. RING_TIMEOUT_SHIFT;
  525. tdma_writel(priv, reg, TDMA_DESC_RING_INTR_CONTROL(ring->index));
  526. }
  527. static int bcm_sysport_get_coalesce(struct net_device *dev,
  528. struct ethtool_coalesce *ec)
  529. {
  530. struct bcm_sysport_priv *priv = netdev_priv(dev);
  531. u32 reg;
  532. reg = tdma_readl(priv, TDMA_DESC_RING_INTR_CONTROL(0));
  533. ec->tx_coalesce_usecs = (reg >> RING_TIMEOUT_SHIFT) * 8192 / 1000;
  534. ec->tx_max_coalesced_frames = reg & RING_INTR_THRESH_MASK;
  535. reg = rdma_readl(priv, RDMA_MBDONE_INTR);
  536. ec->rx_coalesce_usecs = (reg >> RDMA_TIMEOUT_SHIFT) * 8192 / 1000;
  537. ec->rx_max_coalesced_frames = reg & RDMA_INTR_THRESH_MASK;
  538. ec->use_adaptive_rx_coalesce = priv->dim.use_dim;
  539. return 0;
  540. }
  541. static int bcm_sysport_set_coalesce(struct net_device *dev,
  542. struct ethtool_coalesce *ec)
  543. {
  544. struct bcm_sysport_priv *priv = netdev_priv(dev);
  545. struct net_dim_cq_moder moder;
  546. u32 usecs, pkts;
  547. unsigned int i;
  548. /* Base system clock is 125Mhz, DMA timeout is this reference clock
  549. * divided by 1024, which yield roughly 8.192 us, our maximum value has
  550. * to fit in the RING_TIMEOUT_MASK (16 bits).
  551. */
  552. if (ec->tx_max_coalesced_frames > RING_INTR_THRESH_MASK ||
  553. ec->tx_coalesce_usecs > (RING_TIMEOUT_MASK * 8) + 1 ||
  554. ec->rx_max_coalesced_frames > RDMA_INTR_THRESH_MASK ||
  555. ec->rx_coalesce_usecs > (RDMA_TIMEOUT_MASK * 8) + 1)
  556. return -EINVAL;
  557. if ((ec->tx_coalesce_usecs == 0 && ec->tx_max_coalesced_frames == 0) ||
  558. (ec->rx_coalesce_usecs == 0 && ec->rx_max_coalesced_frames == 0) ||
  559. ec->use_adaptive_tx_coalesce)
  560. return -EINVAL;
  561. for (i = 0; i < dev->num_tx_queues; i++)
  562. bcm_sysport_set_tx_coalesce(&priv->tx_rings[i], ec);
  563. priv->rx_coalesce_usecs = ec->rx_coalesce_usecs;
  564. priv->rx_max_coalesced_frames = ec->rx_max_coalesced_frames;
  565. usecs = priv->rx_coalesce_usecs;
  566. pkts = priv->rx_max_coalesced_frames;
  567. if (ec->use_adaptive_rx_coalesce && !priv->dim.use_dim) {
  568. moder = net_dim_get_def_rx_moderation(priv->dim.dim.mode);
  569. usecs = moder.usec;
  570. pkts = moder.pkts;
  571. }
  572. priv->dim.use_dim = ec->use_adaptive_rx_coalesce;
  573. /* Apply desired coalescing parameters */
  574. bcm_sysport_set_rx_coalesce(priv, usecs, pkts);
  575. return 0;
  576. }
  577. static void bcm_sysport_free_cb(struct bcm_sysport_cb *cb)
  578. {
  579. dev_consume_skb_any(cb->skb);
  580. cb->skb = NULL;
  581. dma_unmap_addr_set(cb, dma_addr, 0);
  582. }
  583. static struct sk_buff *bcm_sysport_rx_refill(struct bcm_sysport_priv *priv,
  584. struct bcm_sysport_cb *cb)
  585. {
  586. struct device *kdev = &priv->pdev->dev;
  587. struct net_device *ndev = priv->netdev;
  588. struct sk_buff *skb, *rx_skb;
  589. dma_addr_t mapping;
  590. /* Allocate a new SKB for a new packet */
  591. skb = netdev_alloc_skb(priv->netdev, RX_BUF_LENGTH);
  592. if (!skb) {
  593. priv->mib.alloc_rx_buff_failed++;
  594. netif_err(priv, rx_err, ndev, "SKB alloc failed\n");
  595. return NULL;
  596. }
  597. mapping = dma_map_single(kdev, skb->data,
  598. RX_BUF_LENGTH, DMA_FROM_DEVICE);
  599. if (dma_mapping_error(kdev, mapping)) {
  600. priv->mib.rx_dma_failed++;
  601. dev_kfree_skb_any(skb);
  602. netif_err(priv, rx_err, ndev, "DMA mapping failure\n");
  603. return NULL;
  604. }
  605. /* Grab the current SKB on the ring */
  606. rx_skb = cb->skb;
  607. if (likely(rx_skb))
  608. dma_unmap_single(kdev, dma_unmap_addr(cb, dma_addr),
  609. RX_BUF_LENGTH, DMA_FROM_DEVICE);
  610. /* Put the new SKB on the ring */
  611. cb->skb = skb;
  612. dma_unmap_addr_set(cb, dma_addr, mapping);
  613. dma_desc_set_addr(priv, cb->bd_addr, mapping);
  614. netif_dbg(priv, rx_status, ndev, "RX refill\n");
  615. /* Return the current SKB to the caller */
  616. return rx_skb;
  617. }
  618. static int bcm_sysport_alloc_rx_bufs(struct bcm_sysport_priv *priv)
  619. {
  620. struct bcm_sysport_cb *cb;
  621. struct sk_buff *skb;
  622. unsigned int i;
  623. for (i = 0; i < priv->num_rx_bds; i++) {
  624. cb = &priv->rx_cbs[i];
  625. skb = bcm_sysport_rx_refill(priv, cb);
  626. if (skb)
  627. dev_kfree_skb(skb);
  628. if (!cb->skb)
  629. return -ENOMEM;
  630. }
  631. return 0;
  632. }
  633. /* Poll the hardware for up to budget packets to process */
  634. static unsigned int bcm_sysport_desc_rx(struct bcm_sysport_priv *priv,
  635. unsigned int budget)
  636. {
  637. struct bcm_sysport_stats64 *stats64 = &priv->stats64;
  638. struct net_device *ndev = priv->netdev;
  639. unsigned int processed = 0, to_process;
  640. unsigned int processed_bytes = 0;
  641. struct bcm_sysport_cb *cb;
  642. struct sk_buff *skb;
  643. unsigned int p_index;
  644. u16 len, status;
  645. struct bcm_rsb *rsb;
  646. /* Clear status before servicing to reduce spurious interrupts */
  647. intrl2_0_writel(priv, INTRL2_0_RDMA_MBDONE, INTRL2_CPU_CLEAR);
  648. /* Determine how much we should process since last call, SYSTEMPORT Lite
  649. * groups the producer and consumer indexes into the same 32-bit
  650. * which we access using RDMA_CONS_INDEX
  651. */
  652. if (!priv->is_lite)
  653. p_index = rdma_readl(priv, RDMA_PROD_INDEX);
  654. else
  655. p_index = rdma_readl(priv, RDMA_CONS_INDEX);
  656. p_index &= RDMA_PROD_INDEX_MASK;
  657. to_process = (p_index - priv->rx_c_index) & RDMA_CONS_INDEX_MASK;
  658. netif_dbg(priv, rx_status, ndev,
  659. "p_index=%d rx_c_index=%d to_process=%d\n",
  660. p_index, priv->rx_c_index, to_process);
  661. while ((processed < to_process) && (processed < budget)) {
  662. cb = &priv->rx_cbs[priv->rx_read_ptr];
  663. skb = bcm_sysport_rx_refill(priv, cb);
  664. /* We do not have a backing SKB, so we do not a corresponding
  665. * DMA mapping for this incoming packet since
  666. * bcm_sysport_rx_refill always either has both skb and mapping
  667. * or none.
  668. */
  669. if (unlikely(!skb)) {
  670. netif_err(priv, rx_err, ndev, "out of memory!\n");
  671. ndev->stats.rx_dropped++;
  672. ndev->stats.rx_errors++;
  673. goto next;
  674. }
  675. /* Extract the Receive Status Block prepended */
  676. rsb = (struct bcm_rsb *)skb->data;
  677. len = (rsb->rx_status_len >> DESC_LEN_SHIFT) & DESC_LEN_MASK;
  678. status = (rsb->rx_status_len >> DESC_STATUS_SHIFT) &
  679. DESC_STATUS_MASK;
  680. netif_dbg(priv, rx_status, ndev,
  681. "p=%d, c=%d, rd_ptr=%d, len=%d, flag=0x%04x\n",
  682. p_index, priv->rx_c_index, priv->rx_read_ptr,
  683. len, status);
  684. if (unlikely(len > RX_BUF_LENGTH)) {
  685. netif_err(priv, rx_status, ndev, "oversized packet\n");
  686. ndev->stats.rx_length_errors++;
  687. ndev->stats.rx_errors++;
  688. dev_kfree_skb_any(skb);
  689. goto next;
  690. }
  691. if (unlikely(!(status & DESC_EOP) || !(status & DESC_SOP))) {
  692. netif_err(priv, rx_status, ndev, "fragmented packet!\n");
  693. ndev->stats.rx_dropped++;
  694. ndev->stats.rx_errors++;
  695. dev_kfree_skb_any(skb);
  696. goto next;
  697. }
  698. if (unlikely(status & (RX_STATUS_ERR | RX_STATUS_OVFLOW))) {
  699. netif_err(priv, rx_err, ndev, "error packet\n");
  700. if (status & RX_STATUS_OVFLOW)
  701. ndev->stats.rx_over_errors++;
  702. ndev->stats.rx_dropped++;
  703. ndev->stats.rx_errors++;
  704. dev_kfree_skb_any(skb);
  705. goto next;
  706. }
  707. skb_put(skb, len);
  708. /* Hardware validated our checksum */
  709. if (likely(status & DESC_L4_CSUM))
  710. skb->ip_summed = CHECKSUM_UNNECESSARY;
  711. /* Hardware pre-pends packets with 2bytes before Ethernet
  712. * header plus we have the Receive Status Block, strip off all
  713. * of this from the SKB.
  714. */
  715. skb_pull(skb, sizeof(*rsb) + 2);
  716. len -= (sizeof(*rsb) + 2);
  717. processed_bytes += len;
  718. /* UniMAC may forward CRC */
  719. if (priv->crc_fwd) {
  720. skb_trim(skb, len - ETH_FCS_LEN);
  721. len -= ETH_FCS_LEN;
  722. }
  723. skb->protocol = eth_type_trans(skb, ndev);
  724. ndev->stats.rx_packets++;
  725. ndev->stats.rx_bytes += len;
  726. u64_stats_update_begin(&priv->syncp);
  727. stats64->rx_packets++;
  728. stats64->rx_bytes += len;
  729. u64_stats_update_end(&priv->syncp);
  730. napi_gro_receive(&priv->napi, skb);
  731. next:
  732. processed++;
  733. priv->rx_read_ptr++;
  734. if (priv->rx_read_ptr == priv->num_rx_bds)
  735. priv->rx_read_ptr = 0;
  736. }
  737. priv->dim.packets = processed;
  738. priv->dim.bytes = processed_bytes;
  739. return processed;
  740. }
  741. static void bcm_sysport_tx_reclaim_one(struct bcm_sysport_tx_ring *ring,
  742. struct bcm_sysport_cb *cb,
  743. unsigned int *bytes_compl,
  744. unsigned int *pkts_compl)
  745. {
  746. struct bcm_sysport_priv *priv = ring->priv;
  747. struct device *kdev = &priv->pdev->dev;
  748. if (cb->skb) {
  749. *bytes_compl += cb->skb->len;
  750. dma_unmap_single(kdev, dma_unmap_addr(cb, dma_addr),
  751. dma_unmap_len(cb, dma_len),
  752. DMA_TO_DEVICE);
  753. (*pkts_compl)++;
  754. bcm_sysport_free_cb(cb);
  755. /* SKB fragment */
  756. } else if (dma_unmap_addr(cb, dma_addr)) {
  757. *bytes_compl += dma_unmap_len(cb, dma_len);
  758. dma_unmap_page(kdev, dma_unmap_addr(cb, dma_addr),
  759. dma_unmap_len(cb, dma_len), DMA_TO_DEVICE);
  760. dma_unmap_addr_set(cb, dma_addr, 0);
  761. }
  762. }
  763. /* Reclaim queued SKBs for transmission completion, lockless version */
  764. static unsigned int __bcm_sysport_tx_reclaim(struct bcm_sysport_priv *priv,
  765. struct bcm_sysport_tx_ring *ring)
  766. {
  767. unsigned int pkts_compl = 0, bytes_compl = 0;
  768. struct net_device *ndev = priv->netdev;
  769. unsigned int txbds_processed = 0;
  770. struct bcm_sysport_cb *cb;
  771. unsigned int txbds_ready;
  772. unsigned int c_index;
  773. u32 hw_ind;
  774. /* Clear status before servicing to reduce spurious interrupts */
  775. if (!ring->priv->is_lite)
  776. intrl2_1_writel(ring->priv, BIT(ring->index), INTRL2_CPU_CLEAR);
  777. else
  778. intrl2_0_writel(ring->priv, BIT(ring->index +
  779. INTRL2_0_TDMA_MBDONE_SHIFT), INTRL2_CPU_CLEAR);
  780. /* Compute how many descriptors have been processed since last call */
  781. hw_ind = tdma_readl(priv, TDMA_DESC_RING_PROD_CONS_INDEX(ring->index));
  782. c_index = (hw_ind >> RING_CONS_INDEX_SHIFT) & RING_CONS_INDEX_MASK;
  783. txbds_ready = (c_index - ring->c_index) & RING_CONS_INDEX_MASK;
  784. netif_dbg(priv, tx_done, ndev,
  785. "ring=%d old_c_index=%u c_index=%u txbds_ready=%u\n",
  786. ring->index, ring->c_index, c_index, txbds_ready);
  787. while (txbds_processed < txbds_ready) {
  788. cb = &ring->cbs[ring->clean_index];
  789. bcm_sysport_tx_reclaim_one(ring, cb, &bytes_compl, &pkts_compl);
  790. ring->desc_count++;
  791. txbds_processed++;
  792. if (likely(ring->clean_index < ring->size - 1))
  793. ring->clean_index++;
  794. else
  795. ring->clean_index = 0;
  796. }
  797. u64_stats_update_begin(&priv->syncp);
  798. ring->packets += pkts_compl;
  799. ring->bytes += bytes_compl;
  800. u64_stats_update_end(&priv->syncp);
  801. ring->c_index = c_index;
  802. netif_dbg(priv, tx_done, ndev,
  803. "ring=%d c_index=%d pkts_compl=%d, bytes_compl=%d\n",
  804. ring->index, ring->c_index, pkts_compl, bytes_compl);
  805. return pkts_compl;
  806. }
  807. /* Locked version of the per-ring TX reclaim routine */
  808. static unsigned int bcm_sysport_tx_reclaim(struct bcm_sysport_priv *priv,
  809. struct bcm_sysport_tx_ring *ring)
  810. {
  811. struct netdev_queue *txq;
  812. unsigned int released;
  813. unsigned long flags;
  814. txq = netdev_get_tx_queue(priv->netdev, ring->index);
  815. spin_lock_irqsave(&ring->lock, flags);
  816. released = __bcm_sysport_tx_reclaim(priv, ring);
  817. if (released)
  818. netif_tx_wake_queue(txq);
  819. spin_unlock_irqrestore(&ring->lock, flags);
  820. return released;
  821. }
  822. /* Locked version of the per-ring TX reclaim, but does not wake the queue */
  823. static void bcm_sysport_tx_clean(struct bcm_sysport_priv *priv,
  824. struct bcm_sysport_tx_ring *ring)
  825. {
  826. unsigned long flags;
  827. spin_lock_irqsave(&ring->lock, flags);
  828. __bcm_sysport_tx_reclaim(priv, ring);
  829. spin_unlock_irqrestore(&ring->lock, flags);
  830. }
  831. static int bcm_sysport_tx_poll(struct napi_struct *napi, int budget)
  832. {
  833. struct bcm_sysport_tx_ring *ring =
  834. container_of(napi, struct bcm_sysport_tx_ring, napi);
  835. unsigned int work_done = 0;
  836. work_done = bcm_sysport_tx_reclaim(ring->priv, ring);
  837. if (work_done == 0) {
  838. napi_complete(napi);
  839. /* re-enable TX interrupt */
  840. if (!ring->priv->is_lite)
  841. intrl2_1_mask_clear(ring->priv, BIT(ring->index));
  842. else
  843. intrl2_0_mask_clear(ring->priv, BIT(ring->index +
  844. INTRL2_0_TDMA_MBDONE_SHIFT));
  845. return 0;
  846. }
  847. return budget;
  848. }
  849. static void bcm_sysport_tx_reclaim_all(struct bcm_sysport_priv *priv)
  850. {
  851. unsigned int q;
  852. for (q = 0; q < priv->netdev->num_tx_queues; q++)
  853. bcm_sysport_tx_reclaim(priv, &priv->tx_rings[q]);
  854. }
  855. static int bcm_sysport_poll(struct napi_struct *napi, int budget)
  856. {
  857. struct bcm_sysport_priv *priv =
  858. container_of(napi, struct bcm_sysport_priv, napi);
  859. struct net_dim_sample dim_sample;
  860. unsigned int work_done = 0;
  861. work_done = bcm_sysport_desc_rx(priv, budget);
  862. priv->rx_c_index += work_done;
  863. priv->rx_c_index &= RDMA_CONS_INDEX_MASK;
  864. /* SYSTEMPORT Lite groups the producer/consumer index, producer is
  865. * maintained by HW, but writes to it will be ignore while RDMA
  866. * is active
  867. */
  868. if (!priv->is_lite)
  869. rdma_writel(priv, priv->rx_c_index, RDMA_CONS_INDEX);
  870. else
  871. rdma_writel(priv, priv->rx_c_index << 16, RDMA_CONS_INDEX);
  872. if (work_done < budget) {
  873. napi_complete_done(napi, work_done);
  874. /* re-enable RX interrupts */
  875. intrl2_0_mask_clear(priv, INTRL2_0_RDMA_MBDONE);
  876. }
  877. if (priv->dim.use_dim) {
  878. net_dim_sample(priv->dim.event_ctr, priv->dim.packets,
  879. priv->dim.bytes, &dim_sample);
  880. net_dim(&priv->dim.dim, dim_sample);
  881. }
  882. return work_done;
  883. }
  884. static void mpd_enable_set(struct bcm_sysport_priv *priv, bool enable)
  885. {
  886. u32 reg, bit;
  887. reg = umac_readl(priv, UMAC_MPD_CTRL);
  888. if (enable)
  889. reg |= MPD_EN;
  890. else
  891. reg &= ~MPD_EN;
  892. umac_writel(priv, reg, UMAC_MPD_CTRL);
  893. if (priv->is_lite)
  894. bit = RBUF_ACPI_EN_LITE;
  895. else
  896. bit = RBUF_ACPI_EN;
  897. reg = rbuf_readl(priv, RBUF_CONTROL);
  898. if (enable)
  899. reg |= bit;
  900. else
  901. reg &= ~bit;
  902. rbuf_writel(priv, reg, RBUF_CONTROL);
  903. }
  904. static void bcm_sysport_resume_from_wol(struct bcm_sysport_priv *priv)
  905. {
  906. u32 reg;
  907. /* Disable RXCHK, active filters and Broadcom tag matching */
  908. reg = rxchk_readl(priv, RXCHK_CONTROL);
  909. reg &= ~(RXCHK_BRCM_TAG_MATCH_MASK <<
  910. RXCHK_BRCM_TAG_MATCH_SHIFT | RXCHK_EN | RXCHK_BRCM_TAG_EN);
  911. rxchk_writel(priv, reg, RXCHK_CONTROL);
  912. /* Clear the MagicPacket detection logic */
  913. mpd_enable_set(priv, false);
  914. reg = intrl2_0_readl(priv, INTRL2_CPU_STATUS);
  915. if (reg & INTRL2_0_MPD)
  916. netdev_info(priv->netdev, "Wake-on-LAN (MPD) interrupt!\n");
  917. if (reg & INTRL2_0_BRCM_MATCH_TAG) {
  918. reg = rxchk_readl(priv, RXCHK_BRCM_TAG_MATCH_STATUS) &
  919. RXCHK_BRCM_TAG_MATCH_MASK;
  920. netdev_info(priv->netdev,
  921. "Wake-on-LAN (filters 0x%02x) interrupt!\n", reg);
  922. }
  923. netif_dbg(priv, wol, priv->netdev, "resumed from WOL\n");
  924. }
  925. static void bcm_sysport_dim_work(struct work_struct *work)
  926. {
  927. struct net_dim *dim = container_of(work, struct net_dim, work);
  928. struct bcm_sysport_net_dim *ndim =
  929. container_of(dim, struct bcm_sysport_net_dim, dim);
  930. struct bcm_sysport_priv *priv =
  931. container_of(ndim, struct bcm_sysport_priv, dim);
  932. struct net_dim_cq_moder cur_profile =
  933. net_dim_get_rx_moderation(dim->mode, dim->profile_ix);
  934. bcm_sysport_set_rx_coalesce(priv, cur_profile.usec, cur_profile.pkts);
  935. dim->state = NET_DIM_START_MEASURE;
  936. }
  937. /* RX and misc interrupt routine */
  938. static irqreturn_t bcm_sysport_rx_isr(int irq, void *dev_id)
  939. {
  940. struct net_device *dev = dev_id;
  941. struct bcm_sysport_priv *priv = netdev_priv(dev);
  942. struct bcm_sysport_tx_ring *txr;
  943. unsigned int ring, ring_bit;
  944. priv->irq0_stat = intrl2_0_readl(priv, INTRL2_CPU_STATUS) &
  945. ~intrl2_0_readl(priv, INTRL2_CPU_MASK_STATUS);
  946. intrl2_0_writel(priv, priv->irq0_stat, INTRL2_CPU_CLEAR);
  947. if (unlikely(priv->irq0_stat == 0)) {
  948. netdev_warn(priv->netdev, "spurious RX interrupt\n");
  949. return IRQ_NONE;
  950. }
  951. if (priv->irq0_stat & INTRL2_0_RDMA_MBDONE) {
  952. priv->dim.event_ctr++;
  953. if (likely(napi_schedule_prep(&priv->napi))) {
  954. /* disable RX interrupts */
  955. intrl2_0_mask_set(priv, INTRL2_0_RDMA_MBDONE);
  956. __napi_schedule_irqoff(&priv->napi);
  957. }
  958. }
  959. /* TX ring is full, perform a full reclaim since we do not know
  960. * which one would trigger this interrupt
  961. */
  962. if (priv->irq0_stat & INTRL2_0_TX_RING_FULL)
  963. bcm_sysport_tx_reclaim_all(priv);
  964. if (!priv->is_lite)
  965. goto out;
  966. for (ring = 0; ring < dev->num_tx_queues; ring++) {
  967. ring_bit = BIT(ring + INTRL2_0_TDMA_MBDONE_SHIFT);
  968. if (!(priv->irq0_stat & ring_bit))
  969. continue;
  970. txr = &priv->tx_rings[ring];
  971. if (likely(napi_schedule_prep(&txr->napi))) {
  972. intrl2_0_mask_set(priv, ring_bit);
  973. __napi_schedule(&txr->napi);
  974. }
  975. }
  976. out:
  977. return IRQ_HANDLED;
  978. }
  979. /* TX interrupt service routine */
  980. static irqreturn_t bcm_sysport_tx_isr(int irq, void *dev_id)
  981. {
  982. struct net_device *dev = dev_id;
  983. struct bcm_sysport_priv *priv = netdev_priv(dev);
  984. struct bcm_sysport_tx_ring *txr;
  985. unsigned int ring;
  986. priv->irq1_stat = intrl2_1_readl(priv, INTRL2_CPU_STATUS) &
  987. ~intrl2_1_readl(priv, INTRL2_CPU_MASK_STATUS);
  988. intrl2_1_writel(priv, 0xffffffff, INTRL2_CPU_CLEAR);
  989. if (unlikely(priv->irq1_stat == 0)) {
  990. netdev_warn(priv->netdev, "spurious TX interrupt\n");
  991. return IRQ_NONE;
  992. }
  993. for (ring = 0; ring < dev->num_tx_queues; ring++) {
  994. if (!(priv->irq1_stat & BIT(ring)))
  995. continue;
  996. txr = &priv->tx_rings[ring];
  997. if (likely(napi_schedule_prep(&txr->napi))) {
  998. intrl2_1_mask_set(priv, BIT(ring));
  999. __napi_schedule_irqoff(&txr->napi);
  1000. }
  1001. }
  1002. return IRQ_HANDLED;
  1003. }
  1004. static irqreturn_t bcm_sysport_wol_isr(int irq, void *dev_id)
  1005. {
  1006. struct bcm_sysport_priv *priv = dev_id;
  1007. pm_wakeup_event(&priv->pdev->dev, 0);
  1008. return IRQ_HANDLED;
  1009. }
  1010. #ifdef CONFIG_NET_POLL_CONTROLLER
  1011. static void bcm_sysport_poll_controller(struct net_device *dev)
  1012. {
  1013. struct bcm_sysport_priv *priv = netdev_priv(dev);
  1014. disable_irq(priv->irq0);
  1015. bcm_sysport_rx_isr(priv->irq0, priv);
  1016. enable_irq(priv->irq0);
  1017. if (!priv->is_lite) {
  1018. disable_irq(priv->irq1);
  1019. bcm_sysport_tx_isr(priv->irq1, priv);
  1020. enable_irq(priv->irq1);
  1021. }
  1022. }
  1023. #endif
  1024. static struct sk_buff *bcm_sysport_insert_tsb(struct sk_buff *skb,
  1025. struct net_device *dev)
  1026. {
  1027. struct sk_buff *nskb;
  1028. struct bcm_tsb *tsb;
  1029. u32 csum_info;
  1030. u8 ip_proto;
  1031. u16 csum_start;
  1032. __be16 ip_ver;
  1033. /* Re-allocate SKB if needed */
  1034. if (unlikely(skb_headroom(skb) < sizeof(*tsb))) {
  1035. nskb = skb_realloc_headroom(skb, sizeof(*tsb));
  1036. dev_kfree_skb(skb);
  1037. if (!nskb) {
  1038. dev->stats.tx_errors++;
  1039. dev->stats.tx_dropped++;
  1040. return NULL;
  1041. }
  1042. skb = nskb;
  1043. }
  1044. tsb = skb_push(skb, sizeof(*tsb));
  1045. /* Zero-out TSB by default */
  1046. memset(tsb, 0, sizeof(*tsb));
  1047. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  1048. ip_ver = skb->protocol;
  1049. switch (ip_ver) {
  1050. case htons(ETH_P_IP):
  1051. ip_proto = ip_hdr(skb)->protocol;
  1052. break;
  1053. case htons(ETH_P_IPV6):
  1054. ip_proto = ipv6_hdr(skb)->nexthdr;
  1055. break;
  1056. default:
  1057. return skb;
  1058. }
  1059. /* Get the checksum offset and the L4 (transport) offset */
  1060. csum_start = skb_checksum_start_offset(skb) - sizeof(*tsb);
  1061. csum_info = (csum_start + skb->csum_offset) & L4_CSUM_PTR_MASK;
  1062. csum_info |= (csum_start << L4_PTR_SHIFT);
  1063. if (ip_proto == IPPROTO_TCP || ip_proto == IPPROTO_UDP) {
  1064. csum_info |= L4_LENGTH_VALID;
  1065. if (ip_proto == IPPROTO_UDP &&
  1066. ip_ver == htons(ETH_P_IP))
  1067. csum_info |= L4_UDP;
  1068. } else {
  1069. csum_info = 0;
  1070. }
  1071. tsb->l4_ptr_dest_map = csum_info;
  1072. }
  1073. return skb;
  1074. }
  1075. static netdev_tx_t bcm_sysport_xmit(struct sk_buff *skb,
  1076. struct net_device *dev)
  1077. {
  1078. struct bcm_sysport_priv *priv = netdev_priv(dev);
  1079. struct device *kdev = &priv->pdev->dev;
  1080. struct bcm_sysport_tx_ring *ring;
  1081. struct bcm_sysport_cb *cb;
  1082. struct netdev_queue *txq;
  1083. struct dma_desc *desc;
  1084. unsigned int skb_len;
  1085. unsigned long flags;
  1086. dma_addr_t mapping;
  1087. u32 len_status;
  1088. u16 queue;
  1089. int ret;
  1090. queue = skb_get_queue_mapping(skb);
  1091. txq = netdev_get_tx_queue(dev, queue);
  1092. ring = &priv->tx_rings[queue];
  1093. /* lock against tx reclaim in BH context and TX ring full interrupt */
  1094. spin_lock_irqsave(&ring->lock, flags);
  1095. if (unlikely(ring->desc_count == 0)) {
  1096. netif_tx_stop_queue(txq);
  1097. netdev_err(dev, "queue %d awake and ring full!\n", queue);
  1098. ret = NETDEV_TX_BUSY;
  1099. goto out;
  1100. }
  1101. /* Insert TSB and checksum infos */
  1102. if (priv->tsb_en) {
  1103. skb = bcm_sysport_insert_tsb(skb, dev);
  1104. if (!skb) {
  1105. ret = NETDEV_TX_OK;
  1106. goto out;
  1107. }
  1108. }
  1109. skb_len = skb->len;
  1110. mapping = dma_map_single(kdev, skb->data, skb_len, DMA_TO_DEVICE);
  1111. if (dma_mapping_error(kdev, mapping)) {
  1112. priv->mib.tx_dma_failed++;
  1113. netif_err(priv, tx_err, dev, "DMA map failed at %p (len=%d)\n",
  1114. skb->data, skb_len);
  1115. ret = NETDEV_TX_OK;
  1116. goto out;
  1117. }
  1118. /* Remember the SKB for future freeing */
  1119. cb = &ring->cbs[ring->curr_desc];
  1120. cb->skb = skb;
  1121. dma_unmap_addr_set(cb, dma_addr, mapping);
  1122. dma_unmap_len_set(cb, dma_len, skb_len);
  1123. /* Fetch a descriptor entry from our pool */
  1124. desc = ring->desc_cpu;
  1125. desc->addr_lo = lower_32_bits(mapping);
  1126. len_status = upper_32_bits(mapping) & DESC_ADDR_HI_MASK;
  1127. len_status |= (skb_len << DESC_LEN_SHIFT);
  1128. len_status |= (DESC_SOP | DESC_EOP | TX_STATUS_APP_CRC) <<
  1129. DESC_STATUS_SHIFT;
  1130. if (skb->ip_summed == CHECKSUM_PARTIAL)
  1131. len_status |= (DESC_L4_CSUM << DESC_STATUS_SHIFT);
  1132. ring->curr_desc++;
  1133. if (ring->curr_desc == ring->size)
  1134. ring->curr_desc = 0;
  1135. ring->desc_count--;
  1136. /* Ensure write completion of the descriptor status/length
  1137. * in DRAM before the System Port WRITE_PORT register latches
  1138. * the value
  1139. */
  1140. wmb();
  1141. desc->addr_status_len = len_status;
  1142. wmb();
  1143. /* Write this descriptor address to the RING write port */
  1144. tdma_port_write_desc_addr(priv, desc, ring->index);
  1145. /* Check ring space and update SW control flow */
  1146. if (ring->desc_count == 0)
  1147. netif_tx_stop_queue(txq);
  1148. netif_dbg(priv, tx_queued, dev, "ring=%d desc_count=%d, curr_desc=%d\n",
  1149. ring->index, ring->desc_count, ring->curr_desc);
  1150. ret = NETDEV_TX_OK;
  1151. out:
  1152. spin_unlock_irqrestore(&ring->lock, flags);
  1153. return ret;
  1154. }
  1155. static void bcm_sysport_tx_timeout(struct net_device *dev)
  1156. {
  1157. netdev_warn(dev, "transmit timeout!\n");
  1158. netif_trans_update(dev);
  1159. dev->stats.tx_errors++;
  1160. netif_tx_wake_all_queues(dev);
  1161. }
  1162. /* phylib adjust link callback */
  1163. static void bcm_sysport_adj_link(struct net_device *dev)
  1164. {
  1165. struct bcm_sysport_priv *priv = netdev_priv(dev);
  1166. struct phy_device *phydev = dev->phydev;
  1167. unsigned int changed = 0;
  1168. u32 cmd_bits = 0, reg;
  1169. if (priv->old_link != phydev->link) {
  1170. changed = 1;
  1171. priv->old_link = phydev->link;
  1172. }
  1173. if (priv->old_duplex != phydev->duplex) {
  1174. changed = 1;
  1175. priv->old_duplex = phydev->duplex;
  1176. }
  1177. if (priv->is_lite)
  1178. goto out;
  1179. switch (phydev->speed) {
  1180. case SPEED_2500:
  1181. cmd_bits = CMD_SPEED_2500;
  1182. break;
  1183. case SPEED_1000:
  1184. cmd_bits = CMD_SPEED_1000;
  1185. break;
  1186. case SPEED_100:
  1187. cmd_bits = CMD_SPEED_100;
  1188. break;
  1189. case SPEED_10:
  1190. cmd_bits = CMD_SPEED_10;
  1191. break;
  1192. default:
  1193. break;
  1194. }
  1195. cmd_bits <<= CMD_SPEED_SHIFT;
  1196. if (phydev->duplex == DUPLEX_HALF)
  1197. cmd_bits |= CMD_HD_EN;
  1198. if (priv->old_pause != phydev->pause) {
  1199. changed = 1;
  1200. priv->old_pause = phydev->pause;
  1201. }
  1202. if (!phydev->pause)
  1203. cmd_bits |= CMD_RX_PAUSE_IGNORE | CMD_TX_PAUSE_IGNORE;
  1204. if (!changed)
  1205. return;
  1206. if (phydev->link) {
  1207. reg = umac_readl(priv, UMAC_CMD);
  1208. reg &= ~((CMD_SPEED_MASK << CMD_SPEED_SHIFT) |
  1209. CMD_HD_EN | CMD_RX_PAUSE_IGNORE |
  1210. CMD_TX_PAUSE_IGNORE);
  1211. reg |= cmd_bits;
  1212. umac_writel(priv, reg, UMAC_CMD);
  1213. }
  1214. out:
  1215. if (changed)
  1216. phy_print_status(phydev);
  1217. }
  1218. static void bcm_sysport_init_dim(struct bcm_sysport_priv *priv,
  1219. void (*cb)(struct work_struct *work))
  1220. {
  1221. struct bcm_sysport_net_dim *dim = &priv->dim;
  1222. INIT_WORK(&dim->dim.work, cb);
  1223. dim->dim.mode = NET_DIM_CQ_PERIOD_MODE_START_FROM_EQE;
  1224. dim->event_ctr = 0;
  1225. dim->packets = 0;
  1226. dim->bytes = 0;
  1227. }
  1228. static void bcm_sysport_init_rx_coalesce(struct bcm_sysport_priv *priv)
  1229. {
  1230. struct bcm_sysport_net_dim *dim = &priv->dim;
  1231. struct net_dim_cq_moder moder;
  1232. u32 usecs, pkts;
  1233. usecs = priv->rx_coalesce_usecs;
  1234. pkts = priv->rx_max_coalesced_frames;
  1235. /* If DIM was enabled, re-apply default parameters */
  1236. if (dim->use_dim) {
  1237. moder = net_dim_get_def_rx_moderation(dim->dim.mode);
  1238. usecs = moder.usec;
  1239. pkts = moder.pkts;
  1240. }
  1241. bcm_sysport_set_rx_coalesce(priv, usecs, pkts);
  1242. }
  1243. static int bcm_sysport_init_tx_ring(struct bcm_sysport_priv *priv,
  1244. unsigned int index)
  1245. {
  1246. struct bcm_sysport_tx_ring *ring = &priv->tx_rings[index];
  1247. struct device *kdev = &priv->pdev->dev;
  1248. size_t size;
  1249. void *p;
  1250. u32 reg;
  1251. /* Simple descriptors partitioning for now */
  1252. size = 256;
  1253. /* We just need one DMA descriptor which is DMA-able, since writing to
  1254. * the port will allocate a new descriptor in its internal linked-list
  1255. */
  1256. p = dma_zalloc_coherent(kdev, sizeof(struct dma_desc), &ring->desc_dma,
  1257. GFP_KERNEL);
  1258. if (!p) {
  1259. netif_err(priv, hw, priv->netdev, "DMA alloc failed\n");
  1260. return -ENOMEM;
  1261. }
  1262. ring->cbs = kcalloc(size, sizeof(struct bcm_sysport_cb), GFP_KERNEL);
  1263. if (!ring->cbs) {
  1264. dma_free_coherent(kdev, sizeof(struct dma_desc),
  1265. ring->desc_cpu, ring->desc_dma);
  1266. netif_err(priv, hw, priv->netdev, "CB allocation failed\n");
  1267. return -ENOMEM;
  1268. }
  1269. /* Initialize SW view of the ring */
  1270. spin_lock_init(&ring->lock);
  1271. ring->priv = priv;
  1272. netif_tx_napi_add(priv->netdev, &ring->napi, bcm_sysport_tx_poll, 64);
  1273. ring->index = index;
  1274. ring->size = size;
  1275. ring->clean_index = 0;
  1276. ring->alloc_size = ring->size;
  1277. ring->desc_cpu = p;
  1278. ring->desc_count = ring->size;
  1279. ring->curr_desc = 0;
  1280. /* Initialize HW ring */
  1281. tdma_writel(priv, RING_EN, TDMA_DESC_RING_HEAD_TAIL_PTR(index));
  1282. tdma_writel(priv, 0, TDMA_DESC_RING_COUNT(index));
  1283. tdma_writel(priv, 1, TDMA_DESC_RING_INTR_CONTROL(index));
  1284. tdma_writel(priv, 0, TDMA_DESC_RING_PROD_CONS_INDEX(index));
  1285. /* Configure QID and port mapping */
  1286. reg = tdma_readl(priv, TDMA_DESC_RING_MAPPING(index));
  1287. reg &= ~(RING_QID_MASK | RING_PORT_ID_MASK << RING_PORT_ID_SHIFT);
  1288. if (ring->inspect) {
  1289. reg |= ring->switch_queue & RING_QID_MASK;
  1290. reg |= ring->switch_port << RING_PORT_ID_SHIFT;
  1291. } else {
  1292. reg |= RING_IGNORE_STATUS;
  1293. }
  1294. tdma_writel(priv, reg, TDMA_DESC_RING_MAPPING(index));
  1295. tdma_writel(priv, 0, TDMA_DESC_RING_PCP_DEI_VID(index));
  1296. /* Enable ACB algorithm 2 */
  1297. reg = tdma_readl(priv, TDMA_CONTROL);
  1298. reg |= tdma_control_bit(priv, ACB_ALGO);
  1299. tdma_writel(priv, reg, TDMA_CONTROL);
  1300. /* Do not use tdma_control_bit() here because TSB_SWAP1 collides
  1301. * with the original definition of ACB_ALGO
  1302. */
  1303. reg = tdma_readl(priv, TDMA_CONTROL);
  1304. if (priv->is_lite)
  1305. reg &= ~BIT(TSB_SWAP1);
  1306. /* Set a correct TSB format based on host endian */
  1307. if (!IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
  1308. reg |= tdma_control_bit(priv, TSB_SWAP0);
  1309. else
  1310. reg &= ~tdma_control_bit(priv, TSB_SWAP0);
  1311. tdma_writel(priv, reg, TDMA_CONTROL);
  1312. /* Program the number of descriptors as MAX_THRESHOLD and half of
  1313. * its size for the hysteresis trigger
  1314. */
  1315. tdma_writel(priv, ring->size |
  1316. 1 << RING_HYST_THRESH_SHIFT,
  1317. TDMA_DESC_RING_MAX_HYST(index));
  1318. /* Enable the ring queue in the arbiter */
  1319. reg = tdma_readl(priv, TDMA_TIER1_ARB_0_QUEUE_EN);
  1320. reg |= (1 << index);
  1321. tdma_writel(priv, reg, TDMA_TIER1_ARB_0_QUEUE_EN);
  1322. napi_enable(&ring->napi);
  1323. netif_dbg(priv, hw, priv->netdev,
  1324. "TDMA cfg, size=%d, desc_cpu=%p switch q=%d,port=%d\n",
  1325. ring->size, ring->desc_cpu, ring->switch_queue,
  1326. ring->switch_port);
  1327. return 0;
  1328. }
  1329. static void bcm_sysport_fini_tx_ring(struct bcm_sysport_priv *priv,
  1330. unsigned int index)
  1331. {
  1332. struct bcm_sysport_tx_ring *ring = &priv->tx_rings[index];
  1333. struct device *kdev = &priv->pdev->dev;
  1334. u32 reg;
  1335. /* Caller should stop the TDMA engine */
  1336. reg = tdma_readl(priv, TDMA_STATUS);
  1337. if (!(reg & TDMA_DISABLED))
  1338. netdev_warn(priv->netdev, "TDMA not stopped!\n");
  1339. /* ring->cbs is the last part in bcm_sysport_init_tx_ring which could
  1340. * fail, so by checking this pointer we know whether the TX ring was
  1341. * fully initialized or not.
  1342. */
  1343. if (!ring->cbs)
  1344. return;
  1345. napi_disable(&ring->napi);
  1346. netif_napi_del(&ring->napi);
  1347. bcm_sysport_tx_clean(priv, ring);
  1348. kfree(ring->cbs);
  1349. ring->cbs = NULL;
  1350. if (ring->desc_dma) {
  1351. dma_free_coherent(kdev, sizeof(struct dma_desc),
  1352. ring->desc_cpu, ring->desc_dma);
  1353. ring->desc_dma = 0;
  1354. }
  1355. ring->size = 0;
  1356. ring->alloc_size = 0;
  1357. netif_dbg(priv, hw, priv->netdev, "TDMA fini done\n");
  1358. }
  1359. /* RDMA helper */
  1360. static inline int rdma_enable_set(struct bcm_sysport_priv *priv,
  1361. unsigned int enable)
  1362. {
  1363. unsigned int timeout = 1000;
  1364. u32 reg;
  1365. reg = rdma_readl(priv, RDMA_CONTROL);
  1366. if (enable)
  1367. reg |= RDMA_EN;
  1368. else
  1369. reg &= ~RDMA_EN;
  1370. rdma_writel(priv, reg, RDMA_CONTROL);
  1371. /* Poll for RMDA disabling completion */
  1372. do {
  1373. reg = rdma_readl(priv, RDMA_STATUS);
  1374. if (!!(reg & RDMA_DISABLED) == !enable)
  1375. return 0;
  1376. usleep_range(1000, 2000);
  1377. } while (timeout-- > 0);
  1378. netdev_err(priv->netdev, "timeout waiting for RDMA to finish\n");
  1379. return -ETIMEDOUT;
  1380. }
  1381. /* TDMA helper */
  1382. static inline int tdma_enable_set(struct bcm_sysport_priv *priv,
  1383. unsigned int enable)
  1384. {
  1385. unsigned int timeout = 1000;
  1386. u32 reg;
  1387. reg = tdma_readl(priv, TDMA_CONTROL);
  1388. if (enable)
  1389. reg |= tdma_control_bit(priv, TDMA_EN);
  1390. else
  1391. reg &= ~tdma_control_bit(priv, TDMA_EN);
  1392. tdma_writel(priv, reg, TDMA_CONTROL);
  1393. /* Poll for TMDA disabling completion */
  1394. do {
  1395. reg = tdma_readl(priv, TDMA_STATUS);
  1396. if (!!(reg & TDMA_DISABLED) == !enable)
  1397. return 0;
  1398. usleep_range(1000, 2000);
  1399. } while (timeout-- > 0);
  1400. netdev_err(priv->netdev, "timeout waiting for TDMA to finish\n");
  1401. return -ETIMEDOUT;
  1402. }
  1403. static int bcm_sysport_init_rx_ring(struct bcm_sysport_priv *priv)
  1404. {
  1405. struct bcm_sysport_cb *cb;
  1406. u32 reg;
  1407. int ret;
  1408. int i;
  1409. /* Initialize SW view of the RX ring */
  1410. priv->num_rx_bds = priv->num_rx_desc_words / WORDS_PER_DESC;
  1411. priv->rx_bds = priv->base + SYS_PORT_RDMA_OFFSET;
  1412. priv->rx_c_index = 0;
  1413. priv->rx_read_ptr = 0;
  1414. priv->rx_cbs = kcalloc(priv->num_rx_bds, sizeof(struct bcm_sysport_cb),
  1415. GFP_KERNEL);
  1416. if (!priv->rx_cbs) {
  1417. netif_err(priv, hw, priv->netdev, "CB allocation failed\n");
  1418. return -ENOMEM;
  1419. }
  1420. for (i = 0; i < priv->num_rx_bds; i++) {
  1421. cb = priv->rx_cbs + i;
  1422. cb->bd_addr = priv->rx_bds + i * DESC_SIZE;
  1423. }
  1424. ret = bcm_sysport_alloc_rx_bufs(priv);
  1425. if (ret) {
  1426. netif_err(priv, hw, priv->netdev, "SKB allocation failed\n");
  1427. return ret;
  1428. }
  1429. /* Initialize HW, ensure RDMA is disabled */
  1430. reg = rdma_readl(priv, RDMA_STATUS);
  1431. if (!(reg & RDMA_DISABLED))
  1432. rdma_enable_set(priv, 0);
  1433. rdma_writel(priv, 0, RDMA_WRITE_PTR_LO);
  1434. rdma_writel(priv, 0, RDMA_WRITE_PTR_HI);
  1435. rdma_writel(priv, 0, RDMA_PROD_INDEX);
  1436. rdma_writel(priv, 0, RDMA_CONS_INDEX);
  1437. rdma_writel(priv, priv->num_rx_bds << RDMA_RING_SIZE_SHIFT |
  1438. RX_BUF_LENGTH, RDMA_RING_BUF_SIZE);
  1439. /* Operate the queue in ring mode */
  1440. rdma_writel(priv, 0, RDMA_START_ADDR_HI);
  1441. rdma_writel(priv, 0, RDMA_START_ADDR_LO);
  1442. rdma_writel(priv, 0, RDMA_END_ADDR_HI);
  1443. rdma_writel(priv, priv->num_rx_desc_words - 1, RDMA_END_ADDR_LO);
  1444. netif_dbg(priv, hw, priv->netdev,
  1445. "RDMA cfg, num_rx_bds=%d, rx_bds=%p\n",
  1446. priv->num_rx_bds, priv->rx_bds);
  1447. return 0;
  1448. }
  1449. static void bcm_sysport_fini_rx_ring(struct bcm_sysport_priv *priv)
  1450. {
  1451. struct bcm_sysport_cb *cb;
  1452. unsigned int i;
  1453. u32 reg;
  1454. /* Caller should ensure RDMA is disabled */
  1455. reg = rdma_readl(priv, RDMA_STATUS);
  1456. if (!(reg & RDMA_DISABLED))
  1457. netdev_warn(priv->netdev, "RDMA not stopped!\n");
  1458. for (i = 0; i < priv->num_rx_bds; i++) {
  1459. cb = &priv->rx_cbs[i];
  1460. if (dma_unmap_addr(cb, dma_addr))
  1461. dma_unmap_single(&priv->pdev->dev,
  1462. dma_unmap_addr(cb, dma_addr),
  1463. RX_BUF_LENGTH, DMA_FROM_DEVICE);
  1464. bcm_sysport_free_cb(cb);
  1465. }
  1466. kfree(priv->rx_cbs);
  1467. priv->rx_cbs = NULL;
  1468. netif_dbg(priv, hw, priv->netdev, "RDMA fini done\n");
  1469. }
  1470. static void bcm_sysport_set_rx_mode(struct net_device *dev)
  1471. {
  1472. struct bcm_sysport_priv *priv = netdev_priv(dev);
  1473. u32 reg;
  1474. if (priv->is_lite)
  1475. return;
  1476. reg = umac_readl(priv, UMAC_CMD);
  1477. if (dev->flags & IFF_PROMISC)
  1478. reg |= CMD_PROMISC;
  1479. else
  1480. reg &= ~CMD_PROMISC;
  1481. umac_writel(priv, reg, UMAC_CMD);
  1482. /* No support for ALLMULTI */
  1483. if (dev->flags & IFF_ALLMULTI)
  1484. return;
  1485. }
  1486. static inline void umac_enable_set(struct bcm_sysport_priv *priv,
  1487. u32 mask, unsigned int enable)
  1488. {
  1489. u32 reg;
  1490. if (!priv->is_lite) {
  1491. reg = umac_readl(priv, UMAC_CMD);
  1492. if (enable)
  1493. reg |= mask;
  1494. else
  1495. reg &= ~mask;
  1496. umac_writel(priv, reg, UMAC_CMD);
  1497. } else {
  1498. reg = gib_readl(priv, GIB_CONTROL);
  1499. if (enable)
  1500. reg |= mask;
  1501. else
  1502. reg &= ~mask;
  1503. gib_writel(priv, reg, GIB_CONTROL);
  1504. }
  1505. /* UniMAC stops on a packet boundary, wait for a full-sized packet
  1506. * to be processed (1 msec).
  1507. */
  1508. if (enable == 0)
  1509. usleep_range(1000, 2000);
  1510. }
  1511. static inline void umac_reset(struct bcm_sysport_priv *priv)
  1512. {
  1513. u32 reg;
  1514. if (priv->is_lite)
  1515. return;
  1516. reg = umac_readl(priv, UMAC_CMD);
  1517. reg |= CMD_SW_RESET;
  1518. umac_writel(priv, reg, UMAC_CMD);
  1519. udelay(10);
  1520. reg = umac_readl(priv, UMAC_CMD);
  1521. reg &= ~CMD_SW_RESET;
  1522. umac_writel(priv, reg, UMAC_CMD);
  1523. }
  1524. static void umac_set_hw_addr(struct bcm_sysport_priv *priv,
  1525. unsigned char *addr)
  1526. {
  1527. u32 mac0 = (addr[0] << 24) | (addr[1] << 16) | (addr[2] << 8) |
  1528. addr[3];
  1529. u32 mac1 = (addr[4] << 8) | addr[5];
  1530. if (!priv->is_lite) {
  1531. umac_writel(priv, mac0, UMAC_MAC0);
  1532. umac_writel(priv, mac1, UMAC_MAC1);
  1533. } else {
  1534. gib_writel(priv, mac0, GIB_MAC0);
  1535. gib_writel(priv, mac1, GIB_MAC1);
  1536. }
  1537. }
  1538. static void topctrl_flush(struct bcm_sysport_priv *priv)
  1539. {
  1540. topctrl_writel(priv, RX_FLUSH, RX_FLUSH_CNTL);
  1541. topctrl_writel(priv, TX_FLUSH, TX_FLUSH_CNTL);
  1542. mdelay(1);
  1543. topctrl_writel(priv, 0, RX_FLUSH_CNTL);
  1544. topctrl_writel(priv, 0, TX_FLUSH_CNTL);
  1545. }
  1546. static int bcm_sysport_change_mac(struct net_device *dev, void *p)
  1547. {
  1548. struct bcm_sysport_priv *priv = netdev_priv(dev);
  1549. struct sockaddr *addr = p;
  1550. if (!is_valid_ether_addr(addr->sa_data))
  1551. return -EINVAL;
  1552. memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
  1553. /* interface is disabled, changes to MAC will be reflected on next
  1554. * open call
  1555. */
  1556. if (!netif_running(dev))
  1557. return 0;
  1558. umac_set_hw_addr(priv, dev->dev_addr);
  1559. return 0;
  1560. }
  1561. static void bcm_sysport_get_stats64(struct net_device *dev,
  1562. struct rtnl_link_stats64 *stats)
  1563. {
  1564. struct bcm_sysport_priv *priv = netdev_priv(dev);
  1565. struct bcm_sysport_stats64 *stats64 = &priv->stats64;
  1566. unsigned int start;
  1567. netdev_stats_to_stats64(stats, &dev->stats);
  1568. bcm_sysport_update_tx_stats(priv, &stats->tx_bytes,
  1569. &stats->tx_packets);
  1570. do {
  1571. start = u64_stats_fetch_begin_irq(&priv->syncp);
  1572. stats->rx_packets = stats64->rx_packets;
  1573. stats->rx_bytes = stats64->rx_bytes;
  1574. } while (u64_stats_fetch_retry_irq(&priv->syncp, start));
  1575. }
  1576. static void bcm_sysport_netif_start(struct net_device *dev)
  1577. {
  1578. struct bcm_sysport_priv *priv = netdev_priv(dev);
  1579. /* Enable NAPI */
  1580. bcm_sysport_init_dim(priv, bcm_sysport_dim_work);
  1581. bcm_sysport_init_rx_coalesce(priv);
  1582. napi_enable(&priv->napi);
  1583. /* Enable RX interrupt and TX ring full interrupt */
  1584. intrl2_0_mask_clear(priv, INTRL2_0_RDMA_MBDONE | INTRL2_0_TX_RING_FULL);
  1585. phy_start(dev->phydev);
  1586. /* Enable TX interrupts for the TXQs */
  1587. if (!priv->is_lite)
  1588. intrl2_1_mask_clear(priv, 0xffffffff);
  1589. else
  1590. intrl2_0_mask_clear(priv, INTRL2_0_TDMA_MBDONE_MASK);
  1591. }
  1592. static void rbuf_init(struct bcm_sysport_priv *priv)
  1593. {
  1594. u32 reg;
  1595. reg = rbuf_readl(priv, RBUF_CONTROL);
  1596. reg |= RBUF_4B_ALGN | RBUF_RSB_EN;
  1597. /* Set a correct RSB format on SYSTEMPORT Lite */
  1598. if (priv->is_lite)
  1599. reg &= ~RBUF_RSB_SWAP1;
  1600. /* Set a correct RSB format based on host endian */
  1601. if (!IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
  1602. reg |= RBUF_RSB_SWAP0;
  1603. else
  1604. reg &= ~RBUF_RSB_SWAP0;
  1605. rbuf_writel(priv, reg, RBUF_CONTROL);
  1606. }
  1607. static inline void bcm_sysport_mask_all_intrs(struct bcm_sysport_priv *priv)
  1608. {
  1609. intrl2_0_mask_set(priv, 0xffffffff);
  1610. intrl2_0_writel(priv, 0xffffffff, INTRL2_CPU_CLEAR);
  1611. if (!priv->is_lite) {
  1612. intrl2_1_mask_set(priv, 0xffffffff);
  1613. intrl2_1_writel(priv, 0xffffffff, INTRL2_CPU_CLEAR);
  1614. }
  1615. }
  1616. static inline void gib_set_pad_extension(struct bcm_sysport_priv *priv)
  1617. {
  1618. u32 reg;
  1619. reg = gib_readl(priv, GIB_CONTROL);
  1620. /* Include Broadcom tag in pad extension and fix up IPG_LENGTH */
  1621. if (netdev_uses_dsa(priv->netdev)) {
  1622. reg &= ~(GIB_PAD_EXTENSION_MASK << GIB_PAD_EXTENSION_SHIFT);
  1623. reg |= ENET_BRCM_TAG_LEN << GIB_PAD_EXTENSION_SHIFT;
  1624. }
  1625. reg &= ~(GIB_IPG_LEN_MASK << GIB_IPG_LEN_SHIFT);
  1626. reg |= 12 << GIB_IPG_LEN_SHIFT;
  1627. gib_writel(priv, reg, GIB_CONTROL);
  1628. }
  1629. static int bcm_sysport_open(struct net_device *dev)
  1630. {
  1631. struct bcm_sysport_priv *priv = netdev_priv(dev);
  1632. struct phy_device *phydev;
  1633. unsigned int i;
  1634. int ret;
  1635. /* Reset UniMAC */
  1636. umac_reset(priv);
  1637. /* Flush TX and RX FIFOs at TOPCTRL level */
  1638. topctrl_flush(priv);
  1639. /* Disable the UniMAC RX/TX */
  1640. umac_enable_set(priv, CMD_RX_EN | CMD_TX_EN, 0);
  1641. /* Enable RBUF 2bytes alignment and Receive Status Block */
  1642. rbuf_init(priv);
  1643. /* Set maximum frame length */
  1644. if (!priv->is_lite)
  1645. umac_writel(priv, UMAC_MAX_MTU_SIZE, UMAC_MAX_FRAME_LEN);
  1646. else
  1647. gib_set_pad_extension(priv);
  1648. /* Set MAC address */
  1649. umac_set_hw_addr(priv, dev->dev_addr);
  1650. /* Read CRC forward */
  1651. if (!priv->is_lite)
  1652. priv->crc_fwd = !!(umac_readl(priv, UMAC_CMD) & CMD_CRC_FWD);
  1653. else
  1654. priv->crc_fwd = !((gib_readl(priv, GIB_CONTROL) &
  1655. GIB_FCS_STRIP) >> GIB_FCS_STRIP_SHIFT);
  1656. phydev = of_phy_connect(dev, priv->phy_dn, bcm_sysport_adj_link,
  1657. 0, priv->phy_interface);
  1658. if (!phydev) {
  1659. netdev_err(dev, "could not attach to PHY\n");
  1660. return -ENODEV;
  1661. }
  1662. /* Reset house keeping link status */
  1663. priv->old_duplex = -1;
  1664. priv->old_link = -1;
  1665. priv->old_pause = -1;
  1666. /* mask all interrupts and request them */
  1667. bcm_sysport_mask_all_intrs(priv);
  1668. ret = request_irq(priv->irq0, bcm_sysport_rx_isr, 0, dev->name, dev);
  1669. if (ret) {
  1670. netdev_err(dev, "failed to request RX interrupt\n");
  1671. goto out_phy_disconnect;
  1672. }
  1673. if (!priv->is_lite) {
  1674. ret = request_irq(priv->irq1, bcm_sysport_tx_isr, 0,
  1675. dev->name, dev);
  1676. if (ret) {
  1677. netdev_err(dev, "failed to request TX interrupt\n");
  1678. goto out_free_irq0;
  1679. }
  1680. }
  1681. /* Initialize both hardware and software ring */
  1682. for (i = 0; i < dev->num_tx_queues; i++) {
  1683. ret = bcm_sysport_init_tx_ring(priv, i);
  1684. if (ret) {
  1685. netdev_err(dev, "failed to initialize TX ring %d\n",
  1686. i);
  1687. goto out_free_tx_ring;
  1688. }
  1689. }
  1690. /* Initialize linked-list */
  1691. tdma_writel(priv, TDMA_LL_RAM_INIT_BUSY, TDMA_STATUS);
  1692. /* Initialize RX ring */
  1693. ret = bcm_sysport_init_rx_ring(priv);
  1694. if (ret) {
  1695. netdev_err(dev, "failed to initialize RX ring\n");
  1696. goto out_free_rx_ring;
  1697. }
  1698. /* Turn on RDMA */
  1699. ret = rdma_enable_set(priv, 1);
  1700. if (ret)
  1701. goto out_free_rx_ring;
  1702. /* Turn on TDMA */
  1703. ret = tdma_enable_set(priv, 1);
  1704. if (ret)
  1705. goto out_clear_rx_int;
  1706. /* Turn on UniMAC TX/RX */
  1707. umac_enable_set(priv, CMD_RX_EN | CMD_TX_EN, 1);
  1708. bcm_sysport_netif_start(dev);
  1709. netif_tx_start_all_queues(dev);
  1710. return 0;
  1711. out_clear_rx_int:
  1712. intrl2_0_mask_set(priv, INTRL2_0_RDMA_MBDONE | INTRL2_0_TX_RING_FULL);
  1713. out_free_rx_ring:
  1714. bcm_sysport_fini_rx_ring(priv);
  1715. out_free_tx_ring:
  1716. for (i = 0; i < dev->num_tx_queues; i++)
  1717. bcm_sysport_fini_tx_ring(priv, i);
  1718. if (!priv->is_lite)
  1719. free_irq(priv->irq1, dev);
  1720. out_free_irq0:
  1721. free_irq(priv->irq0, dev);
  1722. out_phy_disconnect:
  1723. phy_disconnect(phydev);
  1724. return ret;
  1725. }
  1726. static void bcm_sysport_netif_stop(struct net_device *dev)
  1727. {
  1728. struct bcm_sysport_priv *priv = netdev_priv(dev);
  1729. /* stop all software from updating hardware */
  1730. netif_tx_disable(dev);
  1731. napi_disable(&priv->napi);
  1732. cancel_work_sync(&priv->dim.dim.work);
  1733. phy_stop(dev->phydev);
  1734. /* mask all interrupts */
  1735. bcm_sysport_mask_all_intrs(priv);
  1736. }
  1737. static int bcm_sysport_stop(struct net_device *dev)
  1738. {
  1739. struct bcm_sysport_priv *priv = netdev_priv(dev);
  1740. unsigned int i;
  1741. int ret;
  1742. bcm_sysport_netif_stop(dev);
  1743. /* Disable UniMAC RX */
  1744. umac_enable_set(priv, CMD_RX_EN, 0);
  1745. ret = tdma_enable_set(priv, 0);
  1746. if (ret) {
  1747. netdev_err(dev, "timeout disabling RDMA\n");
  1748. return ret;
  1749. }
  1750. /* Wait for a maximum packet size to be drained */
  1751. usleep_range(2000, 3000);
  1752. ret = rdma_enable_set(priv, 0);
  1753. if (ret) {
  1754. netdev_err(dev, "timeout disabling TDMA\n");
  1755. return ret;
  1756. }
  1757. /* Disable UniMAC TX */
  1758. umac_enable_set(priv, CMD_TX_EN, 0);
  1759. /* Free RX/TX rings SW structures */
  1760. for (i = 0; i < dev->num_tx_queues; i++)
  1761. bcm_sysport_fini_tx_ring(priv, i);
  1762. bcm_sysport_fini_rx_ring(priv);
  1763. free_irq(priv->irq0, dev);
  1764. if (!priv->is_lite)
  1765. free_irq(priv->irq1, dev);
  1766. /* Disconnect from PHY */
  1767. phy_disconnect(dev->phydev);
  1768. return 0;
  1769. }
  1770. static int bcm_sysport_rule_find(struct bcm_sysport_priv *priv,
  1771. u64 location)
  1772. {
  1773. unsigned int index;
  1774. u32 reg;
  1775. for_each_set_bit(index, priv->filters, RXCHK_BRCM_TAG_MAX) {
  1776. reg = rxchk_readl(priv, RXCHK_BRCM_TAG(index));
  1777. reg >>= RXCHK_BRCM_TAG_CID_SHIFT;
  1778. reg &= RXCHK_BRCM_TAG_CID_MASK;
  1779. if (reg == location)
  1780. return index;
  1781. }
  1782. return -EINVAL;
  1783. }
  1784. static int bcm_sysport_rule_get(struct bcm_sysport_priv *priv,
  1785. struct ethtool_rxnfc *nfc)
  1786. {
  1787. int index;
  1788. /* This is not a rule that we know about */
  1789. index = bcm_sysport_rule_find(priv, nfc->fs.location);
  1790. if (index < 0)
  1791. return -EOPNOTSUPP;
  1792. nfc->fs.ring_cookie = RX_CLS_FLOW_WAKE;
  1793. return 0;
  1794. }
  1795. static int bcm_sysport_rule_set(struct bcm_sysport_priv *priv,
  1796. struct ethtool_rxnfc *nfc)
  1797. {
  1798. unsigned int index;
  1799. u32 reg;
  1800. /* We cannot match locations greater than what the classification ID
  1801. * permits (256 entries)
  1802. */
  1803. if (nfc->fs.location > RXCHK_BRCM_TAG_CID_MASK)
  1804. return -E2BIG;
  1805. /* We cannot support flows that are not destined for a wake-up */
  1806. if (nfc->fs.ring_cookie != RX_CLS_FLOW_WAKE)
  1807. return -EOPNOTSUPP;
  1808. /* All filters are already in use, we cannot match more rules */
  1809. if (bitmap_weight(priv->filters, RXCHK_BRCM_TAG_MAX) ==
  1810. RXCHK_BRCM_TAG_MAX)
  1811. return -ENOSPC;
  1812. index = find_first_zero_bit(priv->filters, RXCHK_BRCM_TAG_MAX);
  1813. if (index >= RXCHK_BRCM_TAG_MAX)
  1814. return -ENOSPC;
  1815. /* Location is the classification ID, and index is the position
  1816. * within one of our 8 possible filters to be programmed
  1817. */
  1818. reg = rxchk_readl(priv, RXCHK_BRCM_TAG(index));
  1819. reg &= ~(RXCHK_BRCM_TAG_CID_MASK << RXCHK_BRCM_TAG_CID_SHIFT);
  1820. reg |= nfc->fs.location << RXCHK_BRCM_TAG_CID_SHIFT;
  1821. rxchk_writel(priv, reg, RXCHK_BRCM_TAG(index));
  1822. rxchk_writel(priv, 0xff00ffff, RXCHK_BRCM_TAG_MASK(index));
  1823. set_bit(index, priv->filters);
  1824. return 0;
  1825. }
  1826. static int bcm_sysport_rule_del(struct bcm_sysport_priv *priv,
  1827. u64 location)
  1828. {
  1829. int index;
  1830. /* This is not a rule that we know about */
  1831. index = bcm_sysport_rule_find(priv, location);
  1832. if (index < 0)
  1833. return -EOPNOTSUPP;
  1834. /* No need to disable this filter if it was enabled, this will
  1835. * be taken care of during suspend time by bcm_sysport_suspend_to_wol
  1836. */
  1837. clear_bit(index, priv->filters);
  1838. return 0;
  1839. }
  1840. static int bcm_sysport_get_rxnfc(struct net_device *dev,
  1841. struct ethtool_rxnfc *nfc, u32 *rule_locs)
  1842. {
  1843. struct bcm_sysport_priv *priv = netdev_priv(dev);
  1844. int ret = -EOPNOTSUPP;
  1845. switch (nfc->cmd) {
  1846. case ETHTOOL_GRXCLSRULE:
  1847. ret = bcm_sysport_rule_get(priv, nfc);
  1848. break;
  1849. default:
  1850. break;
  1851. }
  1852. return ret;
  1853. }
  1854. static int bcm_sysport_set_rxnfc(struct net_device *dev,
  1855. struct ethtool_rxnfc *nfc)
  1856. {
  1857. struct bcm_sysport_priv *priv = netdev_priv(dev);
  1858. int ret = -EOPNOTSUPP;
  1859. switch (nfc->cmd) {
  1860. case ETHTOOL_SRXCLSRLINS:
  1861. ret = bcm_sysport_rule_set(priv, nfc);
  1862. break;
  1863. case ETHTOOL_SRXCLSRLDEL:
  1864. ret = bcm_sysport_rule_del(priv, nfc->fs.location);
  1865. break;
  1866. default:
  1867. break;
  1868. }
  1869. return ret;
  1870. }
  1871. static const struct ethtool_ops bcm_sysport_ethtool_ops = {
  1872. .get_drvinfo = bcm_sysport_get_drvinfo,
  1873. .get_msglevel = bcm_sysport_get_msglvl,
  1874. .set_msglevel = bcm_sysport_set_msglvl,
  1875. .get_link = ethtool_op_get_link,
  1876. .get_strings = bcm_sysport_get_strings,
  1877. .get_ethtool_stats = bcm_sysport_get_stats,
  1878. .get_sset_count = bcm_sysport_get_sset_count,
  1879. .get_wol = bcm_sysport_get_wol,
  1880. .set_wol = bcm_sysport_set_wol,
  1881. .get_coalesce = bcm_sysport_get_coalesce,
  1882. .set_coalesce = bcm_sysport_set_coalesce,
  1883. .get_link_ksettings = phy_ethtool_get_link_ksettings,
  1884. .set_link_ksettings = phy_ethtool_set_link_ksettings,
  1885. .get_rxnfc = bcm_sysport_get_rxnfc,
  1886. .set_rxnfc = bcm_sysport_set_rxnfc,
  1887. };
  1888. static u16 bcm_sysport_select_queue(struct net_device *dev, struct sk_buff *skb,
  1889. struct net_device *sb_dev,
  1890. select_queue_fallback_t fallback)
  1891. {
  1892. struct bcm_sysport_priv *priv = netdev_priv(dev);
  1893. u16 queue = skb_get_queue_mapping(skb);
  1894. struct bcm_sysport_tx_ring *tx_ring;
  1895. unsigned int q, port;
  1896. if (!netdev_uses_dsa(dev))
  1897. return fallback(dev, skb, NULL);
  1898. /* DSA tagging layer will have configured the correct queue */
  1899. q = BRCM_TAG_GET_QUEUE(queue);
  1900. port = BRCM_TAG_GET_PORT(queue);
  1901. tx_ring = priv->ring_map[q + port * priv->per_port_num_tx_queues];
  1902. if (unlikely(!tx_ring))
  1903. return fallback(dev, skb, NULL);
  1904. return tx_ring->index;
  1905. }
  1906. static const struct net_device_ops bcm_sysport_netdev_ops = {
  1907. .ndo_start_xmit = bcm_sysport_xmit,
  1908. .ndo_tx_timeout = bcm_sysport_tx_timeout,
  1909. .ndo_open = bcm_sysport_open,
  1910. .ndo_stop = bcm_sysport_stop,
  1911. .ndo_set_features = bcm_sysport_set_features,
  1912. .ndo_set_rx_mode = bcm_sysport_set_rx_mode,
  1913. .ndo_set_mac_address = bcm_sysport_change_mac,
  1914. #ifdef CONFIG_NET_POLL_CONTROLLER
  1915. .ndo_poll_controller = bcm_sysport_poll_controller,
  1916. #endif
  1917. .ndo_get_stats64 = bcm_sysport_get_stats64,
  1918. .ndo_select_queue = bcm_sysport_select_queue,
  1919. };
  1920. static int bcm_sysport_map_queues(struct notifier_block *nb,
  1921. struct dsa_notifier_register_info *info)
  1922. {
  1923. struct bcm_sysport_tx_ring *ring;
  1924. struct bcm_sysport_priv *priv;
  1925. struct net_device *slave_dev;
  1926. unsigned int num_tx_queues;
  1927. unsigned int q, start, port;
  1928. struct net_device *dev;
  1929. priv = container_of(nb, struct bcm_sysport_priv, dsa_notifier);
  1930. if (priv->netdev != info->master)
  1931. return 0;
  1932. dev = info->master;
  1933. /* We can't be setting up queue inspection for non directly attached
  1934. * switches
  1935. */
  1936. if (info->switch_number)
  1937. return 0;
  1938. if (dev->netdev_ops != &bcm_sysport_netdev_ops)
  1939. return 0;
  1940. port = info->port_number;
  1941. slave_dev = info->info.dev;
  1942. /* On SYSTEMPORT Lite we have twice as less queues, so we cannot do a
  1943. * 1:1 mapping, we can only do a 2:1 mapping. By reducing the number of
  1944. * per-port (slave_dev) network devices queue, we achieve just that.
  1945. * This need to happen now before any slave network device is used such
  1946. * it accurately reflects the number of real TX queues.
  1947. */
  1948. if (priv->is_lite)
  1949. netif_set_real_num_tx_queues(slave_dev,
  1950. slave_dev->num_tx_queues / 2);
  1951. num_tx_queues = slave_dev->real_num_tx_queues;
  1952. if (priv->per_port_num_tx_queues &&
  1953. priv->per_port_num_tx_queues != num_tx_queues)
  1954. netdev_warn(slave_dev, "asymmetric number of per-port queues\n");
  1955. priv->per_port_num_tx_queues = num_tx_queues;
  1956. start = find_first_zero_bit(&priv->queue_bitmap, dev->num_tx_queues);
  1957. for (q = 0; q < num_tx_queues; q++) {
  1958. ring = &priv->tx_rings[q + start];
  1959. /* Just remember the mapping actual programming done
  1960. * during bcm_sysport_init_tx_ring
  1961. */
  1962. ring->switch_queue = q;
  1963. ring->switch_port = port;
  1964. ring->inspect = true;
  1965. priv->ring_map[q + port * num_tx_queues] = ring;
  1966. /* Set all queues as being used now */
  1967. set_bit(q + start, &priv->queue_bitmap);
  1968. }
  1969. return 0;
  1970. }
  1971. static int bcm_sysport_dsa_notifier(struct notifier_block *nb,
  1972. unsigned long event, void *ptr)
  1973. {
  1974. struct dsa_notifier_register_info *info;
  1975. if (event != DSA_PORT_REGISTER)
  1976. return NOTIFY_DONE;
  1977. info = ptr;
  1978. return notifier_from_errno(bcm_sysport_map_queues(nb, info));
  1979. }
  1980. #define REV_FMT "v%2x.%02x"
  1981. static const struct bcm_sysport_hw_params bcm_sysport_params[] = {
  1982. [SYSTEMPORT] = {
  1983. .is_lite = false,
  1984. .num_rx_desc_words = SP_NUM_HW_RX_DESC_WORDS,
  1985. },
  1986. [SYSTEMPORT_LITE] = {
  1987. .is_lite = true,
  1988. .num_rx_desc_words = SP_LT_NUM_HW_RX_DESC_WORDS,
  1989. },
  1990. };
  1991. static const struct of_device_id bcm_sysport_of_match[] = {
  1992. { .compatible = "brcm,systemportlite-v1.00",
  1993. .data = &bcm_sysport_params[SYSTEMPORT_LITE] },
  1994. { .compatible = "brcm,systemport-v1.00",
  1995. .data = &bcm_sysport_params[SYSTEMPORT] },
  1996. { .compatible = "brcm,systemport",
  1997. .data = &bcm_sysport_params[SYSTEMPORT] },
  1998. { /* sentinel */ }
  1999. };
  2000. MODULE_DEVICE_TABLE(of, bcm_sysport_of_match);
  2001. static int bcm_sysport_probe(struct platform_device *pdev)
  2002. {
  2003. const struct bcm_sysport_hw_params *params;
  2004. const struct of_device_id *of_id = NULL;
  2005. struct bcm_sysport_priv *priv;
  2006. struct device_node *dn;
  2007. struct net_device *dev;
  2008. const void *macaddr;
  2009. struct resource *r;
  2010. u32 txq, rxq;
  2011. int ret;
  2012. dn = pdev->dev.of_node;
  2013. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  2014. of_id = of_match_node(bcm_sysport_of_match, dn);
  2015. if (!of_id || !of_id->data)
  2016. return -EINVAL;
  2017. /* Fairly quickly we need to know the type of adapter we have */
  2018. params = of_id->data;
  2019. /* Read the Transmit/Receive Queue properties */
  2020. if (of_property_read_u32(dn, "systemport,num-txq", &txq))
  2021. txq = TDMA_NUM_RINGS;
  2022. if (of_property_read_u32(dn, "systemport,num-rxq", &rxq))
  2023. rxq = 1;
  2024. /* Sanity check the number of transmit queues */
  2025. if (!txq || txq > TDMA_NUM_RINGS)
  2026. return -EINVAL;
  2027. dev = alloc_etherdev_mqs(sizeof(*priv), txq, rxq);
  2028. if (!dev)
  2029. return -ENOMEM;
  2030. /* Initialize private members */
  2031. priv = netdev_priv(dev);
  2032. /* Allocate number of TX rings */
  2033. priv->tx_rings = devm_kcalloc(&pdev->dev, txq,
  2034. sizeof(struct bcm_sysport_tx_ring),
  2035. GFP_KERNEL);
  2036. if (!priv->tx_rings)
  2037. return -ENOMEM;
  2038. priv->is_lite = params->is_lite;
  2039. priv->num_rx_desc_words = params->num_rx_desc_words;
  2040. priv->irq0 = platform_get_irq(pdev, 0);
  2041. if (!priv->is_lite) {
  2042. priv->irq1 = platform_get_irq(pdev, 1);
  2043. priv->wol_irq = platform_get_irq(pdev, 2);
  2044. } else {
  2045. priv->wol_irq = platform_get_irq(pdev, 1);
  2046. }
  2047. if (priv->irq0 <= 0 || (priv->irq1 <= 0 && !priv->is_lite)) {
  2048. dev_err(&pdev->dev, "invalid interrupts\n");
  2049. ret = -EINVAL;
  2050. goto err_free_netdev;
  2051. }
  2052. priv->base = devm_ioremap_resource(&pdev->dev, r);
  2053. if (IS_ERR(priv->base)) {
  2054. ret = PTR_ERR(priv->base);
  2055. goto err_free_netdev;
  2056. }
  2057. priv->netdev = dev;
  2058. priv->pdev = pdev;
  2059. priv->phy_interface = of_get_phy_mode(dn);
  2060. /* Default to GMII interface mode */
  2061. if ((int)priv->phy_interface < 0)
  2062. priv->phy_interface = PHY_INTERFACE_MODE_GMII;
  2063. /* In the case of a fixed PHY, the DT node associated
  2064. * to the PHY is the Ethernet MAC DT node.
  2065. */
  2066. if (of_phy_is_fixed_link(dn)) {
  2067. ret = of_phy_register_fixed_link(dn);
  2068. if (ret) {
  2069. dev_err(&pdev->dev, "failed to register fixed PHY\n");
  2070. goto err_free_netdev;
  2071. }
  2072. priv->phy_dn = dn;
  2073. }
  2074. /* Initialize netdevice members */
  2075. macaddr = of_get_mac_address(dn);
  2076. if (!macaddr || !is_valid_ether_addr(macaddr)) {
  2077. dev_warn(&pdev->dev, "using random Ethernet MAC\n");
  2078. eth_hw_addr_random(dev);
  2079. } else {
  2080. ether_addr_copy(dev->dev_addr, macaddr);
  2081. }
  2082. SET_NETDEV_DEV(dev, &pdev->dev);
  2083. dev_set_drvdata(&pdev->dev, dev);
  2084. dev->ethtool_ops = &bcm_sysport_ethtool_ops;
  2085. dev->netdev_ops = &bcm_sysport_netdev_ops;
  2086. netif_napi_add(dev, &priv->napi, bcm_sysport_poll, 64);
  2087. /* HW supported features, none enabled by default */
  2088. dev->hw_features |= NETIF_F_RXCSUM | NETIF_F_HIGHDMA |
  2089. NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
  2090. /* Request the WOL interrupt and advertise suspend if available */
  2091. priv->wol_irq_disabled = 1;
  2092. ret = devm_request_irq(&pdev->dev, priv->wol_irq,
  2093. bcm_sysport_wol_isr, 0, dev->name, priv);
  2094. if (!ret)
  2095. device_set_wakeup_capable(&pdev->dev, 1);
  2096. /* Set the needed headroom once and for all */
  2097. BUILD_BUG_ON(sizeof(struct bcm_tsb) != 8);
  2098. dev->needed_headroom += sizeof(struct bcm_tsb);
  2099. /* libphy will adjust the link state accordingly */
  2100. netif_carrier_off(dev);
  2101. priv->rx_max_coalesced_frames = 1;
  2102. u64_stats_init(&priv->syncp);
  2103. priv->dsa_notifier.notifier_call = bcm_sysport_dsa_notifier;
  2104. ret = register_dsa_notifier(&priv->dsa_notifier);
  2105. if (ret) {
  2106. dev_err(&pdev->dev, "failed to register DSA notifier\n");
  2107. goto err_deregister_fixed_link;
  2108. }
  2109. ret = register_netdev(dev);
  2110. if (ret) {
  2111. dev_err(&pdev->dev, "failed to register net_device\n");
  2112. goto err_deregister_notifier;
  2113. }
  2114. priv->rev = topctrl_readl(priv, REV_CNTL) & REV_MASK;
  2115. dev_info(&pdev->dev,
  2116. "Broadcom SYSTEMPORT%s" REV_FMT
  2117. " at 0x%p (irqs: %d, %d, TXQs: %d, RXQs: %d)\n",
  2118. priv->is_lite ? " Lite" : "",
  2119. (priv->rev >> 8) & 0xff, priv->rev & 0xff,
  2120. priv->base, priv->irq0, priv->irq1, txq, rxq);
  2121. return 0;
  2122. err_deregister_notifier:
  2123. unregister_dsa_notifier(&priv->dsa_notifier);
  2124. err_deregister_fixed_link:
  2125. if (of_phy_is_fixed_link(dn))
  2126. of_phy_deregister_fixed_link(dn);
  2127. err_free_netdev:
  2128. free_netdev(dev);
  2129. return ret;
  2130. }
  2131. static int bcm_sysport_remove(struct platform_device *pdev)
  2132. {
  2133. struct net_device *dev = dev_get_drvdata(&pdev->dev);
  2134. struct bcm_sysport_priv *priv = netdev_priv(dev);
  2135. struct device_node *dn = pdev->dev.of_node;
  2136. /* Not much to do, ndo_close has been called
  2137. * and we use managed allocations
  2138. */
  2139. unregister_dsa_notifier(&priv->dsa_notifier);
  2140. unregister_netdev(dev);
  2141. if (of_phy_is_fixed_link(dn))
  2142. of_phy_deregister_fixed_link(dn);
  2143. free_netdev(dev);
  2144. dev_set_drvdata(&pdev->dev, NULL);
  2145. return 0;
  2146. }
  2147. static int bcm_sysport_suspend_to_wol(struct bcm_sysport_priv *priv)
  2148. {
  2149. struct net_device *ndev = priv->netdev;
  2150. unsigned int timeout = 1000;
  2151. unsigned int index, i = 0;
  2152. u32 reg;
  2153. reg = umac_readl(priv, UMAC_MPD_CTRL);
  2154. if (priv->wolopts & (WAKE_MAGIC | WAKE_MAGICSECURE))
  2155. reg |= MPD_EN;
  2156. reg &= ~PSW_EN;
  2157. if (priv->wolopts & WAKE_MAGICSECURE) {
  2158. /* Program the SecureOn password */
  2159. umac_writel(priv, get_unaligned_be16(&priv->sopass[0]),
  2160. UMAC_PSW_MS);
  2161. umac_writel(priv, get_unaligned_be32(&priv->sopass[2]),
  2162. UMAC_PSW_LS);
  2163. reg |= PSW_EN;
  2164. }
  2165. umac_writel(priv, reg, UMAC_MPD_CTRL);
  2166. if (priv->wolopts & WAKE_FILTER) {
  2167. /* Turn on ACPI matching to steal packets from RBUF */
  2168. reg = rbuf_readl(priv, RBUF_CONTROL);
  2169. if (priv->is_lite)
  2170. reg |= RBUF_ACPI_EN_LITE;
  2171. else
  2172. reg |= RBUF_ACPI_EN;
  2173. rbuf_writel(priv, reg, RBUF_CONTROL);
  2174. /* Enable RXCHK, active filters and Broadcom tag matching */
  2175. reg = rxchk_readl(priv, RXCHK_CONTROL);
  2176. reg &= ~(RXCHK_BRCM_TAG_MATCH_MASK <<
  2177. RXCHK_BRCM_TAG_MATCH_SHIFT);
  2178. for_each_set_bit(index, priv->filters, RXCHK_BRCM_TAG_MAX) {
  2179. reg |= BIT(RXCHK_BRCM_TAG_MATCH_SHIFT + i);
  2180. i++;
  2181. }
  2182. reg |= RXCHK_EN | RXCHK_BRCM_TAG_EN;
  2183. rxchk_writel(priv, reg, RXCHK_CONTROL);
  2184. }
  2185. /* Make sure RBUF entered WoL mode as result */
  2186. do {
  2187. reg = rbuf_readl(priv, RBUF_STATUS);
  2188. if (reg & RBUF_WOL_MODE)
  2189. break;
  2190. udelay(10);
  2191. } while (timeout-- > 0);
  2192. /* Do not leave the UniMAC RBUF matching only MPD packets */
  2193. if (!timeout) {
  2194. mpd_enable_set(priv, false);
  2195. netif_err(priv, wol, ndev, "failed to enter WOL mode\n");
  2196. return -ETIMEDOUT;
  2197. }
  2198. /* UniMAC receive needs to be turned on */
  2199. umac_enable_set(priv, CMD_RX_EN, 1);
  2200. netif_dbg(priv, wol, ndev, "entered WOL mode\n");
  2201. return 0;
  2202. }
  2203. static int __maybe_unused bcm_sysport_suspend(struct device *d)
  2204. {
  2205. struct net_device *dev = dev_get_drvdata(d);
  2206. struct bcm_sysport_priv *priv = netdev_priv(dev);
  2207. unsigned int i;
  2208. int ret = 0;
  2209. u32 reg;
  2210. if (!netif_running(dev))
  2211. return 0;
  2212. netif_device_detach(dev);
  2213. bcm_sysport_netif_stop(dev);
  2214. phy_suspend(dev->phydev);
  2215. /* Disable UniMAC RX */
  2216. umac_enable_set(priv, CMD_RX_EN, 0);
  2217. ret = rdma_enable_set(priv, 0);
  2218. if (ret) {
  2219. netdev_err(dev, "RDMA timeout!\n");
  2220. return ret;
  2221. }
  2222. /* Disable RXCHK if enabled */
  2223. if (priv->rx_chk_en) {
  2224. reg = rxchk_readl(priv, RXCHK_CONTROL);
  2225. reg &= ~RXCHK_EN;
  2226. rxchk_writel(priv, reg, RXCHK_CONTROL);
  2227. }
  2228. /* Flush RX pipe */
  2229. if (!priv->wolopts)
  2230. topctrl_writel(priv, RX_FLUSH, RX_FLUSH_CNTL);
  2231. ret = tdma_enable_set(priv, 0);
  2232. if (ret) {
  2233. netdev_err(dev, "TDMA timeout!\n");
  2234. return ret;
  2235. }
  2236. /* Wait for a packet boundary */
  2237. usleep_range(2000, 3000);
  2238. umac_enable_set(priv, CMD_TX_EN, 0);
  2239. topctrl_writel(priv, TX_FLUSH, TX_FLUSH_CNTL);
  2240. /* Free RX/TX rings SW structures */
  2241. for (i = 0; i < dev->num_tx_queues; i++)
  2242. bcm_sysport_fini_tx_ring(priv, i);
  2243. bcm_sysport_fini_rx_ring(priv);
  2244. /* Get prepared for Wake-on-LAN */
  2245. if (device_may_wakeup(d) && priv->wolopts)
  2246. ret = bcm_sysport_suspend_to_wol(priv);
  2247. return ret;
  2248. }
  2249. static int __maybe_unused bcm_sysport_resume(struct device *d)
  2250. {
  2251. struct net_device *dev = dev_get_drvdata(d);
  2252. struct bcm_sysport_priv *priv = netdev_priv(dev);
  2253. unsigned int i;
  2254. u32 reg;
  2255. int ret;
  2256. if (!netif_running(dev))
  2257. return 0;
  2258. umac_reset(priv);
  2259. /* Disable the UniMAC RX/TX */
  2260. umac_enable_set(priv, CMD_RX_EN | CMD_TX_EN, 0);
  2261. /* We may have been suspended and never received a WOL event that
  2262. * would turn off MPD detection, take care of that now
  2263. */
  2264. bcm_sysport_resume_from_wol(priv);
  2265. /* Initialize both hardware and software ring */
  2266. for (i = 0; i < dev->num_tx_queues; i++) {
  2267. ret = bcm_sysport_init_tx_ring(priv, i);
  2268. if (ret) {
  2269. netdev_err(dev, "failed to initialize TX ring %d\n",
  2270. i);
  2271. goto out_free_tx_rings;
  2272. }
  2273. }
  2274. /* Initialize linked-list */
  2275. tdma_writel(priv, TDMA_LL_RAM_INIT_BUSY, TDMA_STATUS);
  2276. /* Initialize RX ring */
  2277. ret = bcm_sysport_init_rx_ring(priv);
  2278. if (ret) {
  2279. netdev_err(dev, "failed to initialize RX ring\n");
  2280. goto out_free_rx_ring;
  2281. }
  2282. /* RX pipe enable */
  2283. topctrl_writel(priv, 0, RX_FLUSH_CNTL);
  2284. ret = rdma_enable_set(priv, 1);
  2285. if (ret) {
  2286. netdev_err(dev, "failed to enable RDMA\n");
  2287. goto out_free_rx_ring;
  2288. }
  2289. /* Enable rxhck */
  2290. if (priv->rx_chk_en) {
  2291. reg = rxchk_readl(priv, RXCHK_CONTROL);
  2292. reg |= RXCHK_EN;
  2293. rxchk_writel(priv, reg, RXCHK_CONTROL);
  2294. }
  2295. rbuf_init(priv);
  2296. /* Set maximum frame length */
  2297. if (!priv->is_lite)
  2298. umac_writel(priv, UMAC_MAX_MTU_SIZE, UMAC_MAX_FRAME_LEN);
  2299. else
  2300. gib_set_pad_extension(priv);
  2301. /* Set MAC address */
  2302. umac_set_hw_addr(priv, dev->dev_addr);
  2303. umac_enable_set(priv, CMD_RX_EN, 1);
  2304. /* TX pipe enable */
  2305. topctrl_writel(priv, 0, TX_FLUSH_CNTL);
  2306. umac_enable_set(priv, CMD_TX_EN, 1);
  2307. ret = tdma_enable_set(priv, 1);
  2308. if (ret) {
  2309. netdev_err(dev, "TDMA timeout!\n");
  2310. goto out_free_rx_ring;
  2311. }
  2312. phy_resume(dev->phydev);
  2313. bcm_sysport_netif_start(dev);
  2314. netif_device_attach(dev);
  2315. return 0;
  2316. out_free_rx_ring:
  2317. bcm_sysport_fini_rx_ring(priv);
  2318. out_free_tx_rings:
  2319. for (i = 0; i < dev->num_tx_queues; i++)
  2320. bcm_sysport_fini_tx_ring(priv, i);
  2321. return ret;
  2322. }
  2323. static SIMPLE_DEV_PM_OPS(bcm_sysport_pm_ops,
  2324. bcm_sysport_suspend, bcm_sysport_resume);
  2325. static struct platform_driver bcm_sysport_driver = {
  2326. .probe = bcm_sysport_probe,
  2327. .remove = bcm_sysport_remove,
  2328. .driver = {
  2329. .name = "brcm-systemport",
  2330. .of_match_table = bcm_sysport_of_match,
  2331. .pm = &bcm_sysport_pm_ops,
  2332. },
  2333. };
  2334. module_platform_driver(bcm_sysport_driver);
  2335. MODULE_AUTHOR("Broadcom Corporation");
  2336. MODULE_DESCRIPTION("Broadcom System Port Ethernet MAC driver");
  2337. MODULE_ALIAS("platform:brcm-systemport");
  2338. MODULE_LICENSE("GPL");