bcmsysport.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103
  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/ip.h>
  24. #include <net/ipv6.h>
  25. #include "bcmsysport.h"
  26. /* I/O accessors register helpers */
  27. #define BCM_SYSPORT_IO_MACRO(name, offset) \
  28. static inline u32 name##_readl(struct bcm_sysport_priv *priv, u32 off) \
  29. { \
  30. u32 reg = __raw_readl(priv->base + offset + off); \
  31. return reg; \
  32. } \
  33. static inline void name##_writel(struct bcm_sysport_priv *priv, \
  34. u32 val, u32 off) \
  35. { \
  36. __raw_writel(val, priv->base + offset + off); \
  37. } \
  38. BCM_SYSPORT_IO_MACRO(intrl2_0, SYS_PORT_INTRL2_0_OFFSET);
  39. BCM_SYSPORT_IO_MACRO(intrl2_1, SYS_PORT_INTRL2_1_OFFSET);
  40. BCM_SYSPORT_IO_MACRO(umac, SYS_PORT_UMAC_OFFSET);
  41. BCM_SYSPORT_IO_MACRO(tdma, SYS_PORT_TDMA_OFFSET);
  42. BCM_SYSPORT_IO_MACRO(rdma, SYS_PORT_RDMA_OFFSET);
  43. BCM_SYSPORT_IO_MACRO(rxchk, SYS_PORT_RXCHK_OFFSET);
  44. BCM_SYSPORT_IO_MACRO(txchk, SYS_PORT_TXCHK_OFFSET);
  45. BCM_SYSPORT_IO_MACRO(rbuf, SYS_PORT_RBUF_OFFSET);
  46. BCM_SYSPORT_IO_MACRO(tbuf, SYS_PORT_TBUF_OFFSET);
  47. BCM_SYSPORT_IO_MACRO(topctrl, SYS_PORT_TOPCTRL_OFFSET);
  48. /* L2-interrupt masking/unmasking helpers, does automatic saving of the applied
  49. * mask in a software copy to avoid CPU_MASK_STATUS reads in hot-paths.
  50. */
  51. #define BCM_SYSPORT_INTR_L2(which) \
  52. static inline void intrl2_##which##_mask_clear(struct bcm_sysport_priv *priv, \
  53. u32 mask) \
  54. { \
  55. intrl2_##which##_writel(priv, mask, INTRL2_CPU_MASK_CLEAR); \
  56. priv->irq##which##_mask &= ~(mask); \
  57. } \
  58. static inline void intrl2_##which##_mask_set(struct bcm_sysport_priv *priv, \
  59. u32 mask) \
  60. { \
  61. intrl2_## which##_writel(priv, mask, INTRL2_CPU_MASK_SET); \
  62. priv->irq##which##_mask |= (mask); \
  63. } \
  64. BCM_SYSPORT_INTR_L2(0)
  65. BCM_SYSPORT_INTR_L2(1)
  66. /* Register accesses to GISB/RBUS registers are expensive (few hundred
  67. * nanoseconds), so keep the check for 64-bits explicit here to save
  68. * one register write per-packet on 32-bits platforms.
  69. */
  70. static inline void dma_desc_set_addr(struct bcm_sysport_priv *priv,
  71. void __iomem *d,
  72. dma_addr_t addr)
  73. {
  74. #ifdef CONFIG_PHYS_ADDR_T_64BIT
  75. __raw_writel(upper_32_bits(addr) & DESC_ADDR_HI_MASK,
  76. d + DESC_ADDR_HI_STATUS_LEN);
  77. #endif
  78. __raw_writel(lower_32_bits(addr), d + DESC_ADDR_LO);
  79. }
  80. static inline void tdma_port_write_desc_addr(struct bcm_sysport_priv *priv,
  81. struct dma_desc *desc,
  82. unsigned int port)
  83. {
  84. /* Ports are latched, so write upper address first */
  85. tdma_writel(priv, desc->addr_status_len, TDMA_WRITE_PORT_HI(port));
  86. tdma_writel(priv, desc->addr_lo, TDMA_WRITE_PORT_LO(port));
  87. }
  88. /* Ethtool operations */
  89. static int bcm_sysport_set_settings(struct net_device *dev,
  90. struct ethtool_cmd *cmd)
  91. {
  92. struct bcm_sysport_priv *priv = netdev_priv(dev);
  93. if (!netif_running(dev))
  94. return -EINVAL;
  95. return phy_ethtool_sset(priv->phydev, cmd);
  96. }
  97. static int bcm_sysport_get_settings(struct net_device *dev,
  98. struct ethtool_cmd *cmd)
  99. {
  100. struct bcm_sysport_priv *priv = netdev_priv(dev);
  101. if (!netif_running(dev))
  102. return -EINVAL;
  103. return phy_ethtool_gset(priv->phydev, cmd);
  104. }
  105. static int bcm_sysport_set_rx_csum(struct net_device *dev,
  106. netdev_features_t wanted)
  107. {
  108. struct bcm_sysport_priv *priv = netdev_priv(dev);
  109. u32 reg;
  110. priv->rx_chk_en = !!(wanted & NETIF_F_RXCSUM);
  111. reg = rxchk_readl(priv, RXCHK_CONTROL);
  112. /* Clear L2 header checks, which would prevent BPDUs
  113. * from being received.
  114. */
  115. reg &= ~RXCHK_L2_HDR_DIS;
  116. if (priv->rx_chk_en)
  117. reg |= RXCHK_EN;
  118. else
  119. reg &= ~RXCHK_EN;
  120. /* If UniMAC forwards CRC, we need to skip over it to get
  121. * a valid CHK bit to be set in the per-packet status word
  122. */
  123. if (priv->rx_chk_en && priv->crc_fwd)
  124. reg |= RXCHK_SKIP_FCS;
  125. else
  126. reg &= ~RXCHK_SKIP_FCS;
  127. /* If Broadcom tags are enabled (e.g: using a switch), make
  128. * sure we tell the RXCHK hardware to expect a 4-bytes Broadcom
  129. * tag after the Ethernet MAC Source Address.
  130. */
  131. if (netdev_uses_dsa(dev))
  132. reg |= RXCHK_BRCM_TAG_EN;
  133. else
  134. reg &= ~RXCHK_BRCM_TAG_EN;
  135. rxchk_writel(priv, reg, RXCHK_CONTROL);
  136. return 0;
  137. }
  138. static int bcm_sysport_set_tx_csum(struct net_device *dev,
  139. netdev_features_t wanted)
  140. {
  141. struct bcm_sysport_priv *priv = netdev_priv(dev);
  142. u32 reg;
  143. /* Hardware transmit checksum requires us to enable the Transmit status
  144. * block prepended to the packet contents
  145. */
  146. priv->tsb_en = !!(wanted & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM));
  147. reg = tdma_readl(priv, TDMA_CONTROL);
  148. if (priv->tsb_en)
  149. reg |= TSB_EN;
  150. else
  151. reg &= ~TSB_EN;
  152. tdma_writel(priv, reg, TDMA_CONTROL);
  153. return 0;
  154. }
  155. static int bcm_sysport_set_features(struct net_device *dev,
  156. netdev_features_t features)
  157. {
  158. netdev_features_t changed = features ^ dev->features;
  159. netdev_features_t wanted = dev->wanted_features;
  160. int ret = 0;
  161. if (changed & NETIF_F_RXCSUM)
  162. ret = bcm_sysport_set_rx_csum(dev, wanted);
  163. if (changed & (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM))
  164. ret = bcm_sysport_set_tx_csum(dev, wanted);
  165. return ret;
  166. }
  167. /* Hardware counters must be kept in sync because the order/offset
  168. * is important here (order in structure declaration = order in hardware)
  169. */
  170. static const struct bcm_sysport_stats bcm_sysport_gstrings_stats[] = {
  171. /* general stats */
  172. STAT_NETDEV(rx_packets),
  173. STAT_NETDEV(tx_packets),
  174. STAT_NETDEV(rx_bytes),
  175. STAT_NETDEV(tx_bytes),
  176. STAT_NETDEV(rx_errors),
  177. STAT_NETDEV(tx_errors),
  178. STAT_NETDEV(rx_dropped),
  179. STAT_NETDEV(tx_dropped),
  180. STAT_NETDEV(multicast),
  181. /* UniMAC RSV counters */
  182. STAT_MIB_RX("rx_64_octets", mib.rx.pkt_cnt.cnt_64),
  183. STAT_MIB_RX("rx_65_127_oct", mib.rx.pkt_cnt.cnt_127),
  184. STAT_MIB_RX("rx_128_255_oct", mib.rx.pkt_cnt.cnt_255),
  185. STAT_MIB_RX("rx_256_511_oct", mib.rx.pkt_cnt.cnt_511),
  186. STAT_MIB_RX("rx_512_1023_oct", mib.rx.pkt_cnt.cnt_1023),
  187. STAT_MIB_RX("rx_1024_1518_oct", mib.rx.pkt_cnt.cnt_1518),
  188. STAT_MIB_RX("rx_vlan_1519_1522_oct", mib.rx.pkt_cnt.cnt_mgv),
  189. STAT_MIB_RX("rx_1522_2047_oct", mib.rx.pkt_cnt.cnt_2047),
  190. STAT_MIB_RX("rx_2048_4095_oct", mib.rx.pkt_cnt.cnt_4095),
  191. STAT_MIB_RX("rx_4096_9216_oct", mib.rx.pkt_cnt.cnt_9216),
  192. STAT_MIB_RX("rx_pkts", mib.rx.pkt),
  193. STAT_MIB_RX("rx_bytes", mib.rx.bytes),
  194. STAT_MIB_RX("rx_multicast", mib.rx.mca),
  195. STAT_MIB_RX("rx_broadcast", mib.rx.bca),
  196. STAT_MIB_RX("rx_fcs", mib.rx.fcs),
  197. STAT_MIB_RX("rx_control", mib.rx.cf),
  198. STAT_MIB_RX("rx_pause", mib.rx.pf),
  199. STAT_MIB_RX("rx_unknown", mib.rx.uo),
  200. STAT_MIB_RX("rx_align", mib.rx.aln),
  201. STAT_MIB_RX("rx_outrange", mib.rx.flr),
  202. STAT_MIB_RX("rx_code", mib.rx.cde),
  203. STAT_MIB_RX("rx_carrier", mib.rx.fcr),
  204. STAT_MIB_RX("rx_oversize", mib.rx.ovr),
  205. STAT_MIB_RX("rx_jabber", mib.rx.jbr),
  206. STAT_MIB_RX("rx_mtu_err", mib.rx.mtue),
  207. STAT_MIB_RX("rx_good_pkts", mib.rx.pok),
  208. STAT_MIB_RX("rx_unicast", mib.rx.uc),
  209. STAT_MIB_RX("rx_ppp", mib.rx.ppp),
  210. STAT_MIB_RX("rx_crc", mib.rx.rcrc),
  211. /* UniMAC TSV counters */
  212. STAT_MIB_TX("tx_64_octets", mib.tx.pkt_cnt.cnt_64),
  213. STAT_MIB_TX("tx_65_127_oct", mib.tx.pkt_cnt.cnt_127),
  214. STAT_MIB_TX("tx_128_255_oct", mib.tx.pkt_cnt.cnt_255),
  215. STAT_MIB_TX("tx_256_511_oct", mib.tx.pkt_cnt.cnt_511),
  216. STAT_MIB_TX("tx_512_1023_oct", mib.tx.pkt_cnt.cnt_1023),
  217. STAT_MIB_TX("tx_1024_1518_oct", mib.tx.pkt_cnt.cnt_1518),
  218. STAT_MIB_TX("tx_vlan_1519_1522_oct", mib.tx.pkt_cnt.cnt_mgv),
  219. STAT_MIB_TX("tx_1522_2047_oct", mib.tx.pkt_cnt.cnt_2047),
  220. STAT_MIB_TX("tx_2048_4095_oct", mib.tx.pkt_cnt.cnt_4095),
  221. STAT_MIB_TX("tx_4096_9216_oct", mib.tx.pkt_cnt.cnt_9216),
  222. STAT_MIB_TX("tx_pkts", mib.tx.pkts),
  223. STAT_MIB_TX("tx_multicast", mib.tx.mca),
  224. STAT_MIB_TX("tx_broadcast", mib.tx.bca),
  225. STAT_MIB_TX("tx_pause", mib.tx.pf),
  226. STAT_MIB_TX("tx_control", mib.tx.cf),
  227. STAT_MIB_TX("tx_fcs_err", mib.tx.fcs),
  228. STAT_MIB_TX("tx_oversize", mib.tx.ovr),
  229. STAT_MIB_TX("tx_defer", mib.tx.drf),
  230. STAT_MIB_TX("tx_excess_defer", mib.tx.edf),
  231. STAT_MIB_TX("tx_single_col", mib.tx.scl),
  232. STAT_MIB_TX("tx_multi_col", mib.tx.mcl),
  233. STAT_MIB_TX("tx_late_col", mib.tx.lcl),
  234. STAT_MIB_TX("tx_excess_col", mib.tx.ecl),
  235. STAT_MIB_TX("tx_frags", mib.tx.frg),
  236. STAT_MIB_TX("tx_total_col", mib.tx.ncl),
  237. STAT_MIB_TX("tx_jabber", mib.tx.jbr),
  238. STAT_MIB_TX("tx_bytes", mib.tx.bytes),
  239. STAT_MIB_TX("tx_good_pkts", mib.tx.pok),
  240. STAT_MIB_TX("tx_unicast", mib.tx.uc),
  241. /* UniMAC RUNT counters */
  242. STAT_RUNT("rx_runt_pkts", mib.rx_runt_cnt),
  243. STAT_RUNT("rx_runt_valid_fcs", mib.rx_runt_fcs),
  244. STAT_RUNT("rx_runt_inval_fcs_align", mib.rx_runt_fcs_align),
  245. STAT_RUNT("rx_runt_bytes", mib.rx_runt_bytes),
  246. /* RXCHK misc statistics */
  247. STAT_RXCHK("rxchk_bad_csum", mib.rxchk_bad_csum, RXCHK_BAD_CSUM_CNTR),
  248. STAT_RXCHK("rxchk_other_pkt_disc", mib.rxchk_other_pkt_disc,
  249. RXCHK_OTHER_DISC_CNTR),
  250. /* RBUF misc statistics */
  251. STAT_RBUF("rbuf_ovflow_cnt", mib.rbuf_ovflow_cnt, RBUF_OVFL_DISC_CNTR),
  252. STAT_RBUF("rbuf_err_cnt", mib.rbuf_err_cnt, RBUF_ERR_PKT_CNTR),
  253. STAT_MIB_SOFT("alloc_rx_buff_failed", mib.alloc_rx_buff_failed),
  254. STAT_MIB_SOFT("rx_dma_failed", mib.rx_dma_failed),
  255. STAT_MIB_SOFT("tx_dma_failed", mib.tx_dma_failed),
  256. };
  257. #define BCM_SYSPORT_STATS_LEN ARRAY_SIZE(bcm_sysport_gstrings_stats)
  258. static void bcm_sysport_get_drvinfo(struct net_device *dev,
  259. struct ethtool_drvinfo *info)
  260. {
  261. strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
  262. strlcpy(info->version, "0.1", sizeof(info->version));
  263. strlcpy(info->bus_info, "platform", sizeof(info->bus_info));
  264. }
  265. static u32 bcm_sysport_get_msglvl(struct net_device *dev)
  266. {
  267. struct bcm_sysport_priv *priv = netdev_priv(dev);
  268. return priv->msg_enable;
  269. }
  270. static void bcm_sysport_set_msglvl(struct net_device *dev, u32 enable)
  271. {
  272. struct bcm_sysport_priv *priv = netdev_priv(dev);
  273. priv->msg_enable = enable;
  274. }
  275. static int bcm_sysport_get_sset_count(struct net_device *dev, int string_set)
  276. {
  277. switch (string_set) {
  278. case ETH_SS_STATS:
  279. return BCM_SYSPORT_STATS_LEN;
  280. default:
  281. return -EOPNOTSUPP;
  282. }
  283. }
  284. static void bcm_sysport_get_strings(struct net_device *dev,
  285. u32 stringset, u8 *data)
  286. {
  287. int i;
  288. switch (stringset) {
  289. case ETH_SS_STATS:
  290. for (i = 0; i < BCM_SYSPORT_STATS_LEN; i++) {
  291. memcpy(data + i * ETH_GSTRING_LEN,
  292. bcm_sysport_gstrings_stats[i].stat_string,
  293. ETH_GSTRING_LEN);
  294. }
  295. break;
  296. default:
  297. break;
  298. }
  299. }
  300. static void bcm_sysport_update_mib_counters(struct bcm_sysport_priv *priv)
  301. {
  302. int i, j = 0;
  303. for (i = 0; i < BCM_SYSPORT_STATS_LEN; i++) {
  304. const struct bcm_sysport_stats *s;
  305. u8 offset = 0;
  306. u32 val = 0;
  307. char *p;
  308. s = &bcm_sysport_gstrings_stats[i];
  309. switch (s->type) {
  310. case BCM_SYSPORT_STAT_NETDEV:
  311. case BCM_SYSPORT_STAT_SOFT:
  312. continue;
  313. case BCM_SYSPORT_STAT_MIB_RX:
  314. case BCM_SYSPORT_STAT_MIB_TX:
  315. case BCM_SYSPORT_STAT_RUNT:
  316. if (s->type != BCM_SYSPORT_STAT_MIB_RX)
  317. offset = UMAC_MIB_STAT_OFFSET;
  318. val = umac_readl(priv, UMAC_MIB_START + j + offset);
  319. break;
  320. case BCM_SYSPORT_STAT_RXCHK:
  321. val = rxchk_readl(priv, s->reg_offset);
  322. if (val == ~0)
  323. rxchk_writel(priv, 0, s->reg_offset);
  324. break;
  325. case BCM_SYSPORT_STAT_RBUF:
  326. val = rbuf_readl(priv, s->reg_offset);
  327. if (val == ~0)
  328. rbuf_writel(priv, 0, s->reg_offset);
  329. break;
  330. }
  331. j += s->stat_sizeof;
  332. p = (char *)priv + s->stat_offset;
  333. *(u32 *)p = val;
  334. }
  335. netif_dbg(priv, hw, priv->netdev, "updated MIB counters\n");
  336. }
  337. static void bcm_sysport_get_stats(struct net_device *dev,
  338. struct ethtool_stats *stats, u64 *data)
  339. {
  340. struct bcm_sysport_priv *priv = netdev_priv(dev);
  341. int i;
  342. if (netif_running(dev))
  343. bcm_sysport_update_mib_counters(priv);
  344. for (i = 0; i < BCM_SYSPORT_STATS_LEN; i++) {
  345. const struct bcm_sysport_stats *s;
  346. char *p;
  347. s = &bcm_sysport_gstrings_stats[i];
  348. if (s->type == BCM_SYSPORT_STAT_NETDEV)
  349. p = (char *)&dev->stats;
  350. else
  351. p = (char *)priv;
  352. p += s->stat_offset;
  353. data[i] = *(u32 *)p;
  354. }
  355. }
  356. static void bcm_sysport_get_wol(struct net_device *dev,
  357. struct ethtool_wolinfo *wol)
  358. {
  359. struct bcm_sysport_priv *priv = netdev_priv(dev);
  360. wol->supported = WAKE_MAGIC | WAKE_MAGICSECURE;
  361. wol->wolopts = priv->wolopts;
  362. if (!(priv->wolopts & WAKE_MAGICSECURE))
  363. return;
  364. memcpy(wol->sopass, priv->sopass, sizeof(priv->sopass));
  365. }
  366. static int bcm_sysport_set_wol(struct net_device *dev,
  367. struct ethtool_wolinfo *wol)
  368. {
  369. struct bcm_sysport_priv *priv = netdev_priv(dev);
  370. struct device *kdev = &priv->pdev->dev;
  371. u32 supported = WAKE_MAGIC | WAKE_MAGICSECURE;
  372. if (!device_can_wakeup(kdev))
  373. return -ENOTSUPP;
  374. if (wol->wolopts & ~supported)
  375. return -EINVAL;
  376. if (wol->wolopts & WAKE_MAGICSECURE)
  377. memcpy(priv->sopass, wol->sopass, sizeof(priv->sopass));
  378. /* Flag the device and relevant IRQ as wakeup capable */
  379. if (wol->wolopts) {
  380. device_set_wakeup_enable(kdev, 1);
  381. if (priv->wol_irq_disabled)
  382. enable_irq_wake(priv->wol_irq);
  383. priv->wol_irq_disabled = 0;
  384. } else {
  385. device_set_wakeup_enable(kdev, 0);
  386. /* Avoid unbalanced disable_irq_wake calls */
  387. if (!priv->wol_irq_disabled)
  388. disable_irq_wake(priv->wol_irq);
  389. priv->wol_irq_disabled = 1;
  390. }
  391. priv->wolopts = wol->wolopts;
  392. return 0;
  393. }
  394. static int bcm_sysport_get_coalesce(struct net_device *dev,
  395. struct ethtool_coalesce *ec)
  396. {
  397. struct bcm_sysport_priv *priv = netdev_priv(dev);
  398. u32 reg;
  399. reg = tdma_readl(priv, TDMA_DESC_RING_INTR_CONTROL(0));
  400. ec->tx_coalesce_usecs = (reg >> RING_TIMEOUT_SHIFT) * 8192 / 1000;
  401. ec->tx_max_coalesced_frames = reg & RING_INTR_THRESH_MASK;
  402. reg = rdma_readl(priv, RDMA_MBDONE_INTR);
  403. ec->rx_coalesce_usecs = (reg >> RDMA_TIMEOUT_SHIFT) * 8192 / 1000;
  404. ec->rx_max_coalesced_frames = reg & RDMA_INTR_THRESH_MASK;
  405. return 0;
  406. }
  407. static int bcm_sysport_set_coalesce(struct net_device *dev,
  408. struct ethtool_coalesce *ec)
  409. {
  410. struct bcm_sysport_priv *priv = netdev_priv(dev);
  411. unsigned int i;
  412. u32 reg;
  413. /* Base system clock is 125Mhz, DMA timeout is this reference clock
  414. * divided by 1024, which yield roughly 8.192 us, our maximum value has
  415. * to fit in the RING_TIMEOUT_MASK (16 bits).
  416. */
  417. if (ec->tx_max_coalesced_frames > RING_INTR_THRESH_MASK ||
  418. ec->tx_coalesce_usecs > (RING_TIMEOUT_MASK * 8) + 1 ||
  419. ec->rx_max_coalesced_frames > RDMA_INTR_THRESH_MASK ||
  420. ec->rx_coalesce_usecs > (RDMA_TIMEOUT_MASK * 8) + 1)
  421. return -EINVAL;
  422. if ((ec->tx_coalesce_usecs == 0 && ec->tx_max_coalesced_frames == 0) ||
  423. (ec->rx_coalesce_usecs == 0 && ec->rx_max_coalesced_frames == 0))
  424. return -EINVAL;
  425. for (i = 0; i < dev->num_tx_queues; i++) {
  426. reg = tdma_readl(priv, TDMA_DESC_RING_INTR_CONTROL(i));
  427. reg &= ~(RING_INTR_THRESH_MASK |
  428. RING_TIMEOUT_MASK << RING_TIMEOUT_SHIFT);
  429. reg |= ec->tx_max_coalesced_frames;
  430. reg |= DIV_ROUND_UP(ec->tx_coalesce_usecs * 1000, 8192) <<
  431. RING_TIMEOUT_SHIFT;
  432. tdma_writel(priv, reg, TDMA_DESC_RING_INTR_CONTROL(i));
  433. }
  434. reg = rdma_readl(priv, RDMA_MBDONE_INTR);
  435. reg &= ~(RDMA_INTR_THRESH_MASK |
  436. RDMA_TIMEOUT_MASK << RDMA_TIMEOUT_SHIFT);
  437. reg |= ec->rx_max_coalesced_frames;
  438. reg |= DIV_ROUND_UP(ec->rx_coalesce_usecs * 1000, 8192) <<
  439. RDMA_TIMEOUT_SHIFT;
  440. rdma_writel(priv, reg, RDMA_MBDONE_INTR);
  441. return 0;
  442. }
  443. static void bcm_sysport_free_cb(struct bcm_sysport_cb *cb)
  444. {
  445. dev_kfree_skb_any(cb->skb);
  446. cb->skb = NULL;
  447. dma_unmap_addr_set(cb, dma_addr, 0);
  448. }
  449. static struct sk_buff *bcm_sysport_rx_refill(struct bcm_sysport_priv *priv,
  450. struct bcm_sysport_cb *cb)
  451. {
  452. struct device *kdev = &priv->pdev->dev;
  453. struct net_device *ndev = priv->netdev;
  454. struct sk_buff *skb, *rx_skb;
  455. dma_addr_t mapping;
  456. /* Allocate a new SKB for a new packet */
  457. skb = netdev_alloc_skb(priv->netdev, RX_BUF_LENGTH);
  458. if (!skb) {
  459. priv->mib.alloc_rx_buff_failed++;
  460. netif_err(priv, rx_err, ndev, "SKB alloc failed\n");
  461. return NULL;
  462. }
  463. mapping = dma_map_single(kdev, skb->data,
  464. RX_BUF_LENGTH, DMA_FROM_DEVICE);
  465. if (dma_mapping_error(kdev, mapping)) {
  466. priv->mib.rx_dma_failed++;
  467. dev_kfree_skb_any(skb);
  468. netif_err(priv, rx_err, ndev, "DMA mapping failure\n");
  469. return NULL;
  470. }
  471. /* Grab the current SKB on the ring */
  472. rx_skb = cb->skb;
  473. if (likely(rx_skb))
  474. dma_unmap_single(kdev, dma_unmap_addr(cb, dma_addr),
  475. RX_BUF_LENGTH, DMA_FROM_DEVICE);
  476. /* Put the new SKB on the ring */
  477. cb->skb = skb;
  478. dma_unmap_addr_set(cb, dma_addr, mapping);
  479. dma_desc_set_addr(priv, cb->bd_addr, mapping);
  480. netif_dbg(priv, rx_status, ndev, "RX refill\n");
  481. /* Return the current SKB to the caller */
  482. return rx_skb;
  483. }
  484. static int bcm_sysport_alloc_rx_bufs(struct bcm_sysport_priv *priv)
  485. {
  486. struct bcm_sysport_cb *cb;
  487. struct sk_buff *skb;
  488. unsigned int i;
  489. for (i = 0; i < priv->num_rx_bds; i++) {
  490. cb = &priv->rx_cbs[i];
  491. skb = bcm_sysport_rx_refill(priv, cb);
  492. if (skb)
  493. dev_kfree_skb(skb);
  494. if (!cb->skb)
  495. return -ENOMEM;
  496. }
  497. return 0;
  498. }
  499. /* Poll the hardware for up to budget packets to process */
  500. static unsigned int bcm_sysport_desc_rx(struct bcm_sysport_priv *priv,
  501. unsigned int budget)
  502. {
  503. struct net_device *ndev = priv->netdev;
  504. unsigned int processed = 0, to_process;
  505. struct bcm_sysport_cb *cb;
  506. struct sk_buff *skb;
  507. unsigned int p_index;
  508. u16 len, status;
  509. struct bcm_rsb *rsb;
  510. /* Determine how much we should process since last call */
  511. p_index = rdma_readl(priv, RDMA_PROD_INDEX);
  512. p_index &= RDMA_PROD_INDEX_MASK;
  513. if (p_index < priv->rx_c_index)
  514. to_process = (RDMA_CONS_INDEX_MASK + 1) -
  515. priv->rx_c_index + p_index;
  516. else
  517. to_process = p_index - priv->rx_c_index;
  518. netif_dbg(priv, rx_status, ndev,
  519. "p_index=%d rx_c_index=%d to_process=%d\n",
  520. p_index, priv->rx_c_index, to_process);
  521. while ((processed < to_process) && (processed < budget)) {
  522. cb = &priv->rx_cbs[priv->rx_read_ptr];
  523. skb = bcm_sysport_rx_refill(priv, cb);
  524. /* We do not have a backing SKB, so we do not a corresponding
  525. * DMA mapping for this incoming packet since
  526. * bcm_sysport_rx_refill always either has both skb and mapping
  527. * or none.
  528. */
  529. if (unlikely(!skb)) {
  530. netif_err(priv, rx_err, ndev, "out of memory!\n");
  531. ndev->stats.rx_dropped++;
  532. ndev->stats.rx_errors++;
  533. goto next;
  534. }
  535. /* Extract the Receive Status Block prepended */
  536. rsb = (struct bcm_rsb *)skb->data;
  537. len = (rsb->rx_status_len >> DESC_LEN_SHIFT) & DESC_LEN_MASK;
  538. status = (rsb->rx_status_len >> DESC_STATUS_SHIFT) &
  539. DESC_STATUS_MASK;
  540. netif_dbg(priv, rx_status, ndev,
  541. "p=%d, c=%d, rd_ptr=%d, len=%d, flag=0x%04x\n",
  542. p_index, priv->rx_c_index, priv->rx_read_ptr,
  543. len, status);
  544. if (unlikely(len > RX_BUF_LENGTH)) {
  545. netif_err(priv, rx_status, ndev, "oversized packet\n");
  546. ndev->stats.rx_length_errors++;
  547. ndev->stats.rx_errors++;
  548. dev_kfree_skb_any(skb);
  549. goto next;
  550. }
  551. if (unlikely(!(status & DESC_EOP) || !(status & DESC_SOP))) {
  552. netif_err(priv, rx_status, ndev, "fragmented packet!\n");
  553. ndev->stats.rx_dropped++;
  554. ndev->stats.rx_errors++;
  555. dev_kfree_skb_any(skb);
  556. goto next;
  557. }
  558. if (unlikely(status & (RX_STATUS_ERR | RX_STATUS_OVFLOW))) {
  559. netif_err(priv, rx_err, ndev, "error packet\n");
  560. if (status & RX_STATUS_OVFLOW)
  561. ndev->stats.rx_over_errors++;
  562. ndev->stats.rx_dropped++;
  563. ndev->stats.rx_errors++;
  564. dev_kfree_skb_any(skb);
  565. goto next;
  566. }
  567. skb_put(skb, len);
  568. /* Hardware validated our checksum */
  569. if (likely(status & DESC_L4_CSUM))
  570. skb->ip_summed = CHECKSUM_UNNECESSARY;
  571. /* Hardware pre-pends packets with 2bytes before Ethernet
  572. * header plus we have the Receive Status Block, strip off all
  573. * of this from the SKB.
  574. */
  575. skb_pull(skb, sizeof(*rsb) + 2);
  576. len -= (sizeof(*rsb) + 2);
  577. /* UniMAC may forward CRC */
  578. if (priv->crc_fwd) {
  579. skb_trim(skb, len - ETH_FCS_LEN);
  580. len -= ETH_FCS_LEN;
  581. }
  582. skb->protocol = eth_type_trans(skb, ndev);
  583. ndev->stats.rx_packets++;
  584. ndev->stats.rx_bytes += len;
  585. napi_gro_receive(&priv->napi, skb);
  586. next:
  587. processed++;
  588. priv->rx_read_ptr++;
  589. if (priv->rx_read_ptr == priv->num_rx_bds)
  590. priv->rx_read_ptr = 0;
  591. }
  592. return processed;
  593. }
  594. static void bcm_sysport_tx_reclaim_one(struct bcm_sysport_priv *priv,
  595. struct bcm_sysport_cb *cb,
  596. unsigned int *bytes_compl,
  597. unsigned int *pkts_compl)
  598. {
  599. struct device *kdev = &priv->pdev->dev;
  600. struct net_device *ndev = priv->netdev;
  601. if (cb->skb) {
  602. ndev->stats.tx_bytes += cb->skb->len;
  603. *bytes_compl += cb->skb->len;
  604. dma_unmap_single(kdev, dma_unmap_addr(cb, dma_addr),
  605. dma_unmap_len(cb, dma_len),
  606. DMA_TO_DEVICE);
  607. ndev->stats.tx_packets++;
  608. (*pkts_compl)++;
  609. bcm_sysport_free_cb(cb);
  610. /* SKB fragment */
  611. } else if (dma_unmap_addr(cb, dma_addr)) {
  612. ndev->stats.tx_bytes += dma_unmap_len(cb, dma_len);
  613. dma_unmap_page(kdev, dma_unmap_addr(cb, dma_addr),
  614. dma_unmap_len(cb, dma_len), DMA_TO_DEVICE);
  615. dma_unmap_addr_set(cb, dma_addr, 0);
  616. }
  617. }
  618. /* Reclaim queued SKBs for transmission completion, lockless version */
  619. static unsigned int __bcm_sysport_tx_reclaim(struct bcm_sysport_priv *priv,
  620. struct bcm_sysport_tx_ring *ring)
  621. {
  622. struct net_device *ndev = priv->netdev;
  623. unsigned int pkts_compl = 0, bytes_compl = 0;
  624. unsigned int txbds_processed = 0;
  625. struct bcm_sysport_cb *cb;
  626. unsigned int txbds_ready;
  627. unsigned int c_index;
  628. u32 hw_ind;
  629. /* Compute how many descriptors have been processed since last call */
  630. hw_ind = tdma_readl(priv, TDMA_DESC_RING_PROD_CONS_INDEX(ring->index));
  631. c_index = (hw_ind >> RING_CONS_INDEX_SHIFT) & RING_CONS_INDEX_MASK;
  632. txbds_ready = (c_index - ring->c_index) & RING_CONS_INDEX_MASK;
  633. netif_dbg(priv, tx_done, ndev,
  634. "ring=%d old_c_index=%u c_index=%u txbds_ready=%u\n",
  635. ring->index, ring->c_index, c_index, txbds_ready);
  636. while (txbds_processed < txbds_ready) {
  637. cb = &ring->cbs[ring->clean_index];
  638. bcm_sysport_tx_reclaim_one(priv, cb, &bytes_compl, &pkts_compl);
  639. ring->desc_count++;
  640. txbds_processed++;
  641. if (likely(ring->clean_index < ring->size - 1))
  642. ring->clean_index++;
  643. else
  644. ring->clean_index = 0;
  645. }
  646. ring->c_index = c_index;
  647. netif_dbg(priv, tx_done, ndev,
  648. "ring=%d c_index=%d pkts_compl=%d, bytes_compl=%d\n",
  649. ring->index, ring->c_index, pkts_compl, bytes_compl);
  650. return pkts_compl;
  651. }
  652. /* Locked version of the per-ring TX reclaim routine */
  653. static unsigned int bcm_sysport_tx_reclaim(struct bcm_sysport_priv *priv,
  654. struct bcm_sysport_tx_ring *ring)
  655. {
  656. struct netdev_queue *txq;
  657. unsigned int released;
  658. unsigned long flags;
  659. txq = netdev_get_tx_queue(priv->netdev, ring->index);
  660. spin_lock_irqsave(&ring->lock, flags);
  661. released = __bcm_sysport_tx_reclaim(priv, ring);
  662. if (released)
  663. netif_tx_wake_queue(txq);
  664. spin_unlock_irqrestore(&ring->lock, flags);
  665. return released;
  666. }
  667. /* Locked version of the per-ring TX reclaim, but does not wake the queue */
  668. static void bcm_sysport_tx_clean(struct bcm_sysport_priv *priv,
  669. struct bcm_sysport_tx_ring *ring)
  670. {
  671. unsigned long flags;
  672. spin_lock_irqsave(&ring->lock, flags);
  673. __bcm_sysport_tx_reclaim(priv, ring);
  674. spin_unlock_irqrestore(&ring->lock, flags);
  675. }
  676. static int bcm_sysport_tx_poll(struct napi_struct *napi, int budget)
  677. {
  678. struct bcm_sysport_tx_ring *ring =
  679. container_of(napi, struct bcm_sysport_tx_ring, napi);
  680. unsigned int work_done = 0;
  681. work_done = bcm_sysport_tx_reclaim(ring->priv, ring);
  682. if (work_done == 0) {
  683. napi_complete(napi);
  684. /* re-enable TX interrupt */
  685. intrl2_1_mask_clear(ring->priv, BIT(ring->index));
  686. return 0;
  687. }
  688. return budget;
  689. }
  690. static void bcm_sysport_tx_reclaim_all(struct bcm_sysport_priv *priv)
  691. {
  692. unsigned int q;
  693. for (q = 0; q < priv->netdev->num_tx_queues; q++)
  694. bcm_sysport_tx_reclaim(priv, &priv->tx_rings[q]);
  695. }
  696. static int bcm_sysport_poll(struct napi_struct *napi, int budget)
  697. {
  698. struct bcm_sysport_priv *priv =
  699. container_of(napi, struct bcm_sysport_priv, napi);
  700. unsigned int work_done = 0;
  701. work_done = bcm_sysport_desc_rx(priv, budget);
  702. priv->rx_c_index += work_done;
  703. priv->rx_c_index &= RDMA_CONS_INDEX_MASK;
  704. rdma_writel(priv, priv->rx_c_index, RDMA_CONS_INDEX);
  705. if (work_done < budget) {
  706. napi_complete(napi);
  707. /* re-enable RX interrupts */
  708. intrl2_0_mask_clear(priv, INTRL2_0_RDMA_MBDONE);
  709. }
  710. return work_done;
  711. }
  712. static void bcm_sysport_resume_from_wol(struct bcm_sysport_priv *priv)
  713. {
  714. u32 reg;
  715. /* Clear the MagicPacket detection logic */
  716. reg = umac_readl(priv, UMAC_MPD_CTRL);
  717. reg &= ~MPD_EN;
  718. umac_writel(priv, reg, UMAC_MPD_CTRL);
  719. reg = intrl2_0_readl(priv, INTRL2_CPU_STATUS);
  720. if (reg & INTRL2_0_MPD)
  721. netdev_info(priv->netdev, "Wake-on-LAN (MPD) interrupt!\n");
  722. if (reg & INTRL2_0_BRCM_MATCH_TAG) {
  723. reg = rxchk_readl(priv, RXCHK_BRCM_TAG_MATCH_STATUS) &
  724. RXCHK_BRCM_TAG_MATCH_MASK;
  725. netdev_info(priv->netdev,
  726. "Wake-on-LAN (filters 0x%02x) interrupt!\n", reg);
  727. }
  728. netif_dbg(priv, wol, priv->netdev, "resumed from WOL\n");
  729. }
  730. /* RX and misc interrupt routine */
  731. static irqreturn_t bcm_sysport_rx_isr(int irq, void *dev_id)
  732. {
  733. struct net_device *dev = dev_id;
  734. struct bcm_sysport_priv *priv = netdev_priv(dev);
  735. priv->irq0_stat = intrl2_0_readl(priv, INTRL2_CPU_STATUS) &
  736. ~intrl2_0_readl(priv, INTRL2_CPU_MASK_STATUS);
  737. intrl2_0_writel(priv, priv->irq0_stat, INTRL2_CPU_CLEAR);
  738. if (unlikely(priv->irq0_stat == 0)) {
  739. netdev_warn(priv->netdev, "spurious RX interrupt\n");
  740. return IRQ_NONE;
  741. }
  742. if (priv->irq0_stat & INTRL2_0_RDMA_MBDONE) {
  743. if (likely(napi_schedule_prep(&priv->napi))) {
  744. /* disable RX interrupts */
  745. intrl2_0_mask_set(priv, INTRL2_0_RDMA_MBDONE);
  746. __napi_schedule(&priv->napi);
  747. }
  748. }
  749. /* TX ring is full, perform a full reclaim since we do not know
  750. * which one would trigger this interrupt
  751. */
  752. if (priv->irq0_stat & INTRL2_0_TX_RING_FULL)
  753. bcm_sysport_tx_reclaim_all(priv);
  754. return IRQ_HANDLED;
  755. }
  756. /* TX interrupt service routine */
  757. static irqreturn_t bcm_sysport_tx_isr(int irq, void *dev_id)
  758. {
  759. struct net_device *dev = dev_id;
  760. struct bcm_sysport_priv *priv = netdev_priv(dev);
  761. struct bcm_sysport_tx_ring *txr;
  762. unsigned int ring;
  763. priv->irq1_stat = intrl2_1_readl(priv, INTRL2_CPU_STATUS) &
  764. ~intrl2_1_readl(priv, INTRL2_CPU_MASK_STATUS);
  765. intrl2_1_writel(priv, 0xffffffff, INTRL2_CPU_CLEAR);
  766. if (unlikely(priv->irq1_stat == 0)) {
  767. netdev_warn(priv->netdev, "spurious TX interrupt\n");
  768. return IRQ_NONE;
  769. }
  770. for (ring = 0; ring < dev->num_tx_queues; ring++) {
  771. if (!(priv->irq1_stat & BIT(ring)))
  772. continue;
  773. txr = &priv->tx_rings[ring];
  774. if (likely(napi_schedule_prep(&txr->napi))) {
  775. intrl2_1_mask_set(priv, BIT(ring));
  776. __napi_schedule(&txr->napi);
  777. }
  778. }
  779. return IRQ_HANDLED;
  780. }
  781. static irqreturn_t bcm_sysport_wol_isr(int irq, void *dev_id)
  782. {
  783. struct bcm_sysport_priv *priv = dev_id;
  784. pm_wakeup_event(&priv->pdev->dev, 0);
  785. return IRQ_HANDLED;
  786. }
  787. #ifdef CONFIG_NET_POLL_CONTROLLER
  788. static void bcm_sysport_poll_controller(struct net_device *dev)
  789. {
  790. struct bcm_sysport_priv *priv = netdev_priv(dev);
  791. disable_irq(priv->irq0);
  792. bcm_sysport_rx_isr(priv->irq0, priv);
  793. enable_irq(priv->irq0);
  794. disable_irq(priv->irq1);
  795. bcm_sysport_tx_isr(priv->irq1, priv);
  796. enable_irq(priv->irq1);
  797. }
  798. #endif
  799. static struct sk_buff *bcm_sysport_insert_tsb(struct sk_buff *skb,
  800. struct net_device *dev)
  801. {
  802. struct sk_buff *nskb;
  803. struct bcm_tsb *tsb;
  804. u32 csum_info;
  805. u8 ip_proto;
  806. u16 csum_start;
  807. u16 ip_ver;
  808. /* Re-allocate SKB if needed */
  809. if (unlikely(skb_headroom(skb) < sizeof(*tsb))) {
  810. nskb = skb_realloc_headroom(skb, sizeof(*tsb));
  811. dev_kfree_skb(skb);
  812. if (!nskb) {
  813. dev->stats.tx_errors++;
  814. dev->stats.tx_dropped++;
  815. return NULL;
  816. }
  817. skb = nskb;
  818. }
  819. tsb = (struct bcm_tsb *)skb_push(skb, sizeof(*tsb));
  820. /* Zero-out TSB by default */
  821. memset(tsb, 0, sizeof(*tsb));
  822. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  823. ip_ver = htons(skb->protocol);
  824. switch (ip_ver) {
  825. case ETH_P_IP:
  826. ip_proto = ip_hdr(skb)->protocol;
  827. break;
  828. case ETH_P_IPV6:
  829. ip_proto = ipv6_hdr(skb)->nexthdr;
  830. break;
  831. default:
  832. return skb;
  833. }
  834. /* Get the checksum offset and the L4 (transport) offset */
  835. csum_start = skb_checksum_start_offset(skb) - sizeof(*tsb);
  836. csum_info = (csum_start + skb->csum_offset) & L4_CSUM_PTR_MASK;
  837. csum_info |= (csum_start << L4_PTR_SHIFT);
  838. if (ip_proto == IPPROTO_TCP || ip_proto == IPPROTO_UDP) {
  839. csum_info |= L4_LENGTH_VALID;
  840. if (ip_proto == IPPROTO_UDP && ip_ver == ETH_P_IP)
  841. csum_info |= L4_UDP;
  842. } else {
  843. csum_info = 0;
  844. }
  845. tsb->l4_ptr_dest_map = csum_info;
  846. }
  847. return skb;
  848. }
  849. static netdev_tx_t bcm_sysport_xmit(struct sk_buff *skb,
  850. struct net_device *dev)
  851. {
  852. struct bcm_sysport_priv *priv = netdev_priv(dev);
  853. struct device *kdev = &priv->pdev->dev;
  854. struct bcm_sysport_tx_ring *ring;
  855. struct bcm_sysport_cb *cb;
  856. struct netdev_queue *txq;
  857. struct dma_desc *desc;
  858. unsigned int skb_len;
  859. unsigned long flags;
  860. dma_addr_t mapping;
  861. u32 len_status;
  862. u16 queue;
  863. int ret;
  864. queue = skb_get_queue_mapping(skb);
  865. txq = netdev_get_tx_queue(dev, queue);
  866. ring = &priv->tx_rings[queue];
  867. /* lock against tx reclaim in BH context and TX ring full interrupt */
  868. spin_lock_irqsave(&ring->lock, flags);
  869. if (unlikely(ring->desc_count == 0)) {
  870. netif_tx_stop_queue(txq);
  871. netdev_err(dev, "queue %d awake and ring full!\n", queue);
  872. ret = NETDEV_TX_BUSY;
  873. goto out;
  874. }
  875. /* The Ethernet switch we are interfaced with needs packets to be at
  876. * least 64 bytes (including FCS) otherwise they will be discarded when
  877. * they enter the switch port logic. When Broadcom tags are enabled, we
  878. * need to make sure that packets are at least 68 bytes
  879. * (including FCS and tag) because the length verification is done after
  880. * the Broadcom tag is stripped off the ingress packet.
  881. */
  882. if (skb_put_padto(skb, ETH_ZLEN + ENET_BRCM_TAG_LEN)) {
  883. ret = NETDEV_TX_OK;
  884. goto out;
  885. }
  886. /* Insert TSB and checksum infos */
  887. if (priv->tsb_en) {
  888. skb = bcm_sysport_insert_tsb(skb, dev);
  889. if (!skb) {
  890. ret = NETDEV_TX_OK;
  891. goto out;
  892. }
  893. }
  894. skb_len = skb->len;
  895. mapping = dma_map_single(kdev, skb->data, skb_len, DMA_TO_DEVICE);
  896. if (dma_mapping_error(kdev, mapping)) {
  897. priv->mib.tx_dma_failed++;
  898. netif_err(priv, tx_err, dev, "DMA map failed at %p (len=%d)\n",
  899. skb->data, skb_len);
  900. ret = NETDEV_TX_OK;
  901. goto out;
  902. }
  903. /* Remember the SKB for future freeing */
  904. cb = &ring->cbs[ring->curr_desc];
  905. cb->skb = skb;
  906. dma_unmap_addr_set(cb, dma_addr, mapping);
  907. dma_unmap_len_set(cb, dma_len, skb_len);
  908. /* Fetch a descriptor entry from our pool */
  909. desc = ring->desc_cpu;
  910. desc->addr_lo = lower_32_bits(mapping);
  911. len_status = upper_32_bits(mapping) & DESC_ADDR_HI_MASK;
  912. len_status |= (skb_len << DESC_LEN_SHIFT);
  913. len_status |= (DESC_SOP | DESC_EOP | TX_STATUS_APP_CRC) <<
  914. DESC_STATUS_SHIFT;
  915. if (skb->ip_summed == CHECKSUM_PARTIAL)
  916. len_status |= (DESC_L4_CSUM << DESC_STATUS_SHIFT);
  917. ring->curr_desc++;
  918. if (ring->curr_desc == ring->size)
  919. ring->curr_desc = 0;
  920. ring->desc_count--;
  921. /* Ensure write completion of the descriptor status/length
  922. * in DRAM before the System Port WRITE_PORT register latches
  923. * the value
  924. */
  925. wmb();
  926. desc->addr_status_len = len_status;
  927. wmb();
  928. /* Write this descriptor address to the RING write port */
  929. tdma_port_write_desc_addr(priv, desc, ring->index);
  930. /* Check ring space and update SW control flow */
  931. if (ring->desc_count == 0)
  932. netif_tx_stop_queue(txq);
  933. netif_dbg(priv, tx_queued, dev, "ring=%d desc_count=%d, curr_desc=%d\n",
  934. ring->index, ring->desc_count, ring->curr_desc);
  935. ret = NETDEV_TX_OK;
  936. out:
  937. spin_unlock_irqrestore(&ring->lock, flags);
  938. return ret;
  939. }
  940. static void bcm_sysport_tx_timeout(struct net_device *dev)
  941. {
  942. netdev_warn(dev, "transmit timeout!\n");
  943. dev->trans_start = jiffies;
  944. dev->stats.tx_errors++;
  945. netif_tx_wake_all_queues(dev);
  946. }
  947. /* phylib adjust link callback */
  948. static void bcm_sysport_adj_link(struct net_device *dev)
  949. {
  950. struct bcm_sysport_priv *priv = netdev_priv(dev);
  951. struct phy_device *phydev = priv->phydev;
  952. unsigned int changed = 0;
  953. u32 cmd_bits = 0, reg;
  954. if (priv->old_link != phydev->link) {
  955. changed = 1;
  956. priv->old_link = phydev->link;
  957. }
  958. if (priv->old_duplex != phydev->duplex) {
  959. changed = 1;
  960. priv->old_duplex = phydev->duplex;
  961. }
  962. switch (phydev->speed) {
  963. case SPEED_2500:
  964. cmd_bits = CMD_SPEED_2500;
  965. break;
  966. case SPEED_1000:
  967. cmd_bits = CMD_SPEED_1000;
  968. break;
  969. case SPEED_100:
  970. cmd_bits = CMD_SPEED_100;
  971. break;
  972. case SPEED_10:
  973. cmd_bits = CMD_SPEED_10;
  974. break;
  975. default:
  976. break;
  977. }
  978. cmd_bits <<= CMD_SPEED_SHIFT;
  979. if (phydev->duplex == DUPLEX_HALF)
  980. cmd_bits |= CMD_HD_EN;
  981. if (priv->old_pause != phydev->pause) {
  982. changed = 1;
  983. priv->old_pause = phydev->pause;
  984. }
  985. if (!phydev->pause)
  986. cmd_bits |= CMD_RX_PAUSE_IGNORE | CMD_TX_PAUSE_IGNORE;
  987. if (!changed)
  988. return;
  989. if (phydev->link) {
  990. reg = umac_readl(priv, UMAC_CMD);
  991. reg &= ~((CMD_SPEED_MASK << CMD_SPEED_SHIFT) |
  992. CMD_HD_EN | CMD_RX_PAUSE_IGNORE |
  993. CMD_TX_PAUSE_IGNORE);
  994. reg |= cmd_bits;
  995. umac_writel(priv, reg, UMAC_CMD);
  996. }
  997. phy_print_status(priv->phydev);
  998. }
  999. static int bcm_sysport_init_tx_ring(struct bcm_sysport_priv *priv,
  1000. unsigned int index)
  1001. {
  1002. struct bcm_sysport_tx_ring *ring = &priv->tx_rings[index];
  1003. struct device *kdev = &priv->pdev->dev;
  1004. size_t size;
  1005. void *p;
  1006. u32 reg;
  1007. /* Simple descriptors partitioning for now */
  1008. size = 256;
  1009. /* We just need one DMA descriptor which is DMA-able, since writing to
  1010. * the port will allocate a new descriptor in its internal linked-list
  1011. */
  1012. p = dma_zalloc_coherent(kdev, sizeof(struct dma_desc), &ring->desc_dma,
  1013. GFP_KERNEL);
  1014. if (!p) {
  1015. netif_err(priv, hw, priv->netdev, "DMA alloc failed\n");
  1016. return -ENOMEM;
  1017. }
  1018. ring->cbs = kcalloc(size, sizeof(struct bcm_sysport_cb), GFP_KERNEL);
  1019. if (!ring->cbs) {
  1020. netif_err(priv, hw, priv->netdev, "CB allocation failed\n");
  1021. return -ENOMEM;
  1022. }
  1023. /* Initialize SW view of the ring */
  1024. spin_lock_init(&ring->lock);
  1025. ring->priv = priv;
  1026. netif_napi_add(priv->netdev, &ring->napi, bcm_sysport_tx_poll, 64);
  1027. ring->index = index;
  1028. ring->size = size;
  1029. ring->clean_index = 0;
  1030. ring->alloc_size = ring->size;
  1031. ring->desc_cpu = p;
  1032. ring->desc_count = ring->size;
  1033. ring->curr_desc = 0;
  1034. /* Initialize HW ring */
  1035. tdma_writel(priv, RING_EN, TDMA_DESC_RING_HEAD_TAIL_PTR(index));
  1036. tdma_writel(priv, 0, TDMA_DESC_RING_COUNT(index));
  1037. tdma_writel(priv, 1, TDMA_DESC_RING_INTR_CONTROL(index));
  1038. tdma_writel(priv, 0, TDMA_DESC_RING_PROD_CONS_INDEX(index));
  1039. tdma_writel(priv, RING_IGNORE_STATUS, TDMA_DESC_RING_MAPPING(index));
  1040. tdma_writel(priv, 0, TDMA_DESC_RING_PCP_DEI_VID(index));
  1041. /* Program the number of descriptors as MAX_THRESHOLD and half of
  1042. * its size for the hysteresis trigger
  1043. */
  1044. tdma_writel(priv, ring->size |
  1045. 1 << RING_HYST_THRESH_SHIFT,
  1046. TDMA_DESC_RING_MAX_HYST(index));
  1047. /* Enable the ring queue in the arbiter */
  1048. reg = tdma_readl(priv, TDMA_TIER1_ARB_0_QUEUE_EN);
  1049. reg |= (1 << index);
  1050. tdma_writel(priv, reg, TDMA_TIER1_ARB_0_QUEUE_EN);
  1051. napi_enable(&ring->napi);
  1052. netif_dbg(priv, hw, priv->netdev,
  1053. "TDMA cfg, size=%d, desc_cpu=%p\n",
  1054. ring->size, ring->desc_cpu);
  1055. return 0;
  1056. }
  1057. static void bcm_sysport_fini_tx_ring(struct bcm_sysport_priv *priv,
  1058. unsigned int index)
  1059. {
  1060. struct bcm_sysport_tx_ring *ring = &priv->tx_rings[index];
  1061. struct device *kdev = &priv->pdev->dev;
  1062. u32 reg;
  1063. /* Caller should stop the TDMA engine */
  1064. reg = tdma_readl(priv, TDMA_STATUS);
  1065. if (!(reg & TDMA_DISABLED))
  1066. netdev_warn(priv->netdev, "TDMA not stopped!\n");
  1067. /* ring->cbs is the last part in bcm_sysport_init_tx_ring which could
  1068. * fail, so by checking this pointer we know whether the TX ring was
  1069. * fully initialized or not.
  1070. */
  1071. if (!ring->cbs)
  1072. return;
  1073. napi_disable(&ring->napi);
  1074. netif_napi_del(&ring->napi);
  1075. bcm_sysport_tx_clean(priv, ring);
  1076. kfree(ring->cbs);
  1077. ring->cbs = NULL;
  1078. if (ring->desc_dma) {
  1079. dma_free_coherent(kdev, sizeof(struct dma_desc),
  1080. ring->desc_cpu, ring->desc_dma);
  1081. ring->desc_dma = 0;
  1082. }
  1083. ring->size = 0;
  1084. ring->alloc_size = 0;
  1085. netif_dbg(priv, hw, priv->netdev, "TDMA fini done\n");
  1086. }
  1087. /* RDMA helper */
  1088. static inline int rdma_enable_set(struct bcm_sysport_priv *priv,
  1089. unsigned int enable)
  1090. {
  1091. unsigned int timeout = 1000;
  1092. u32 reg;
  1093. reg = rdma_readl(priv, RDMA_CONTROL);
  1094. if (enable)
  1095. reg |= RDMA_EN;
  1096. else
  1097. reg &= ~RDMA_EN;
  1098. rdma_writel(priv, reg, RDMA_CONTROL);
  1099. /* Poll for RMDA disabling completion */
  1100. do {
  1101. reg = rdma_readl(priv, RDMA_STATUS);
  1102. if (!!(reg & RDMA_DISABLED) == !enable)
  1103. return 0;
  1104. usleep_range(1000, 2000);
  1105. } while (timeout-- > 0);
  1106. netdev_err(priv->netdev, "timeout waiting for RDMA to finish\n");
  1107. return -ETIMEDOUT;
  1108. }
  1109. /* TDMA helper */
  1110. static inline int tdma_enable_set(struct bcm_sysport_priv *priv,
  1111. unsigned int enable)
  1112. {
  1113. unsigned int timeout = 1000;
  1114. u32 reg;
  1115. reg = tdma_readl(priv, TDMA_CONTROL);
  1116. if (enable)
  1117. reg |= TDMA_EN;
  1118. else
  1119. reg &= ~TDMA_EN;
  1120. tdma_writel(priv, reg, TDMA_CONTROL);
  1121. /* Poll for TMDA disabling completion */
  1122. do {
  1123. reg = tdma_readl(priv, TDMA_STATUS);
  1124. if (!!(reg & TDMA_DISABLED) == !enable)
  1125. return 0;
  1126. usleep_range(1000, 2000);
  1127. } while (timeout-- > 0);
  1128. netdev_err(priv->netdev, "timeout waiting for TDMA to finish\n");
  1129. return -ETIMEDOUT;
  1130. }
  1131. static int bcm_sysport_init_rx_ring(struct bcm_sysport_priv *priv)
  1132. {
  1133. struct bcm_sysport_cb *cb;
  1134. u32 reg;
  1135. int ret;
  1136. int i;
  1137. /* Initialize SW view of the RX ring */
  1138. priv->num_rx_bds = NUM_RX_DESC;
  1139. priv->rx_bds = priv->base + SYS_PORT_RDMA_OFFSET;
  1140. priv->rx_c_index = 0;
  1141. priv->rx_read_ptr = 0;
  1142. priv->rx_cbs = kcalloc(priv->num_rx_bds, sizeof(struct bcm_sysport_cb),
  1143. GFP_KERNEL);
  1144. if (!priv->rx_cbs) {
  1145. netif_err(priv, hw, priv->netdev, "CB allocation failed\n");
  1146. return -ENOMEM;
  1147. }
  1148. for (i = 0; i < priv->num_rx_bds; i++) {
  1149. cb = priv->rx_cbs + i;
  1150. cb->bd_addr = priv->rx_bds + i * DESC_SIZE;
  1151. }
  1152. ret = bcm_sysport_alloc_rx_bufs(priv);
  1153. if (ret) {
  1154. netif_err(priv, hw, priv->netdev, "SKB allocation failed\n");
  1155. return ret;
  1156. }
  1157. /* Initialize HW, ensure RDMA is disabled */
  1158. reg = rdma_readl(priv, RDMA_STATUS);
  1159. if (!(reg & RDMA_DISABLED))
  1160. rdma_enable_set(priv, 0);
  1161. rdma_writel(priv, 0, RDMA_WRITE_PTR_LO);
  1162. rdma_writel(priv, 0, RDMA_WRITE_PTR_HI);
  1163. rdma_writel(priv, 0, RDMA_PROD_INDEX);
  1164. rdma_writel(priv, 0, RDMA_CONS_INDEX);
  1165. rdma_writel(priv, priv->num_rx_bds << RDMA_RING_SIZE_SHIFT |
  1166. RX_BUF_LENGTH, RDMA_RING_BUF_SIZE);
  1167. /* Operate the queue in ring mode */
  1168. rdma_writel(priv, 0, RDMA_START_ADDR_HI);
  1169. rdma_writel(priv, 0, RDMA_START_ADDR_LO);
  1170. rdma_writel(priv, 0, RDMA_END_ADDR_HI);
  1171. rdma_writel(priv, NUM_HW_RX_DESC_WORDS - 1, RDMA_END_ADDR_LO);
  1172. rdma_writel(priv, 1, RDMA_MBDONE_INTR);
  1173. netif_dbg(priv, hw, priv->netdev,
  1174. "RDMA cfg, num_rx_bds=%d, rx_bds=%p\n",
  1175. priv->num_rx_bds, priv->rx_bds);
  1176. return 0;
  1177. }
  1178. static void bcm_sysport_fini_rx_ring(struct bcm_sysport_priv *priv)
  1179. {
  1180. struct bcm_sysport_cb *cb;
  1181. unsigned int i;
  1182. u32 reg;
  1183. /* Caller should ensure RDMA is disabled */
  1184. reg = rdma_readl(priv, RDMA_STATUS);
  1185. if (!(reg & RDMA_DISABLED))
  1186. netdev_warn(priv->netdev, "RDMA not stopped!\n");
  1187. for (i = 0; i < priv->num_rx_bds; i++) {
  1188. cb = &priv->rx_cbs[i];
  1189. if (dma_unmap_addr(cb, dma_addr))
  1190. dma_unmap_single(&priv->pdev->dev,
  1191. dma_unmap_addr(cb, dma_addr),
  1192. RX_BUF_LENGTH, DMA_FROM_DEVICE);
  1193. bcm_sysport_free_cb(cb);
  1194. }
  1195. kfree(priv->rx_cbs);
  1196. priv->rx_cbs = NULL;
  1197. netif_dbg(priv, hw, priv->netdev, "RDMA fini done\n");
  1198. }
  1199. static void bcm_sysport_set_rx_mode(struct net_device *dev)
  1200. {
  1201. struct bcm_sysport_priv *priv = netdev_priv(dev);
  1202. u32 reg;
  1203. reg = umac_readl(priv, UMAC_CMD);
  1204. if (dev->flags & IFF_PROMISC)
  1205. reg |= CMD_PROMISC;
  1206. else
  1207. reg &= ~CMD_PROMISC;
  1208. umac_writel(priv, reg, UMAC_CMD);
  1209. /* No support for ALLMULTI */
  1210. if (dev->flags & IFF_ALLMULTI)
  1211. return;
  1212. }
  1213. static inline void umac_enable_set(struct bcm_sysport_priv *priv,
  1214. u32 mask, unsigned int enable)
  1215. {
  1216. u32 reg;
  1217. reg = umac_readl(priv, UMAC_CMD);
  1218. if (enable)
  1219. reg |= mask;
  1220. else
  1221. reg &= ~mask;
  1222. umac_writel(priv, reg, UMAC_CMD);
  1223. /* UniMAC stops on a packet boundary, wait for a full-sized packet
  1224. * to be processed (1 msec).
  1225. */
  1226. if (enable == 0)
  1227. usleep_range(1000, 2000);
  1228. }
  1229. static inline void umac_reset(struct bcm_sysport_priv *priv)
  1230. {
  1231. u32 reg;
  1232. reg = umac_readl(priv, UMAC_CMD);
  1233. reg |= CMD_SW_RESET;
  1234. umac_writel(priv, reg, UMAC_CMD);
  1235. udelay(10);
  1236. reg = umac_readl(priv, UMAC_CMD);
  1237. reg &= ~CMD_SW_RESET;
  1238. umac_writel(priv, reg, UMAC_CMD);
  1239. }
  1240. static void umac_set_hw_addr(struct bcm_sysport_priv *priv,
  1241. unsigned char *addr)
  1242. {
  1243. umac_writel(priv, (addr[0] << 24) | (addr[1] << 16) |
  1244. (addr[2] << 8) | addr[3], UMAC_MAC0);
  1245. umac_writel(priv, (addr[4] << 8) | addr[5], UMAC_MAC1);
  1246. }
  1247. static void topctrl_flush(struct bcm_sysport_priv *priv)
  1248. {
  1249. topctrl_writel(priv, RX_FLUSH, RX_FLUSH_CNTL);
  1250. topctrl_writel(priv, TX_FLUSH, TX_FLUSH_CNTL);
  1251. mdelay(1);
  1252. topctrl_writel(priv, 0, RX_FLUSH_CNTL);
  1253. topctrl_writel(priv, 0, TX_FLUSH_CNTL);
  1254. }
  1255. static int bcm_sysport_change_mac(struct net_device *dev, void *p)
  1256. {
  1257. struct bcm_sysport_priv *priv = netdev_priv(dev);
  1258. struct sockaddr *addr = p;
  1259. if (!is_valid_ether_addr(addr->sa_data))
  1260. return -EINVAL;
  1261. memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
  1262. /* interface is disabled, changes to MAC will be reflected on next
  1263. * open call
  1264. */
  1265. if (!netif_running(dev))
  1266. return 0;
  1267. umac_set_hw_addr(priv, dev->dev_addr);
  1268. return 0;
  1269. }
  1270. static void bcm_sysport_netif_start(struct net_device *dev)
  1271. {
  1272. struct bcm_sysport_priv *priv = netdev_priv(dev);
  1273. /* Enable NAPI */
  1274. napi_enable(&priv->napi);
  1275. /* Enable RX interrupt and TX ring full interrupt */
  1276. intrl2_0_mask_clear(priv, INTRL2_0_RDMA_MBDONE | INTRL2_0_TX_RING_FULL);
  1277. phy_start(priv->phydev);
  1278. /* Enable TX interrupts for the 32 TXQs */
  1279. intrl2_1_mask_clear(priv, 0xffffffff);
  1280. /* Last call before we start the real business */
  1281. netif_tx_start_all_queues(dev);
  1282. }
  1283. static void rbuf_init(struct bcm_sysport_priv *priv)
  1284. {
  1285. u32 reg;
  1286. reg = rbuf_readl(priv, RBUF_CONTROL);
  1287. reg |= RBUF_4B_ALGN | RBUF_RSB_EN;
  1288. rbuf_writel(priv, reg, RBUF_CONTROL);
  1289. }
  1290. static int bcm_sysport_open(struct net_device *dev)
  1291. {
  1292. struct bcm_sysport_priv *priv = netdev_priv(dev);
  1293. unsigned int i;
  1294. int ret;
  1295. /* Reset UniMAC */
  1296. umac_reset(priv);
  1297. /* Flush TX and RX FIFOs at TOPCTRL level */
  1298. topctrl_flush(priv);
  1299. /* Disable the UniMAC RX/TX */
  1300. umac_enable_set(priv, CMD_RX_EN | CMD_TX_EN, 0);
  1301. /* Enable RBUF 2bytes alignment and Receive Status Block */
  1302. rbuf_init(priv);
  1303. /* Set maximum frame length */
  1304. umac_writel(priv, UMAC_MAX_MTU_SIZE, UMAC_MAX_FRAME_LEN);
  1305. /* Set MAC address */
  1306. umac_set_hw_addr(priv, dev->dev_addr);
  1307. /* Read CRC forward */
  1308. priv->crc_fwd = !!(umac_readl(priv, UMAC_CMD) & CMD_CRC_FWD);
  1309. priv->phydev = of_phy_connect(dev, priv->phy_dn, bcm_sysport_adj_link,
  1310. 0, priv->phy_interface);
  1311. if (!priv->phydev) {
  1312. netdev_err(dev, "could not attach to PHY\n");
  1313. return -ENODEV;
  1314. }
  1315. /* Reset house keeping link status */
  1316. priv->old_duplex = -1;
  1317. priv->old_link = -1;
  1318. priv->old_pause = -1;
  1319. /* mask all interrupts and request them */
  1320. intrl2_0_writel(priv, 0xffffffff, INTRL2_CPU_MASK_SET);
  1321. intrl2_0_writel(priv, 0xffffffff, INTRL2_CPU_CLEAR);
  1322. intrl2_0_writel(priv, 0, INTRL2_CPU_MASK_CLEAR);
  1323. intrl2_1_writel(priv, 0xffffffff, INTRL2_CPU_MASK_SET);
  1324. intrl2_1_writel(priv, 0xffffffff, INTRL2_CPU_CLEAR);
  1325. intrl2_1_writel(priv, 0, INTRL2_CPU_MASK_CLEAR);
  1326. ret = request_irq(priv->irq0, bcm_sysport_rx_isr, 0, dev->name, dev);
  1327. if (ret) {
  1328. netdev_err(dev, "failed to request RX interrupt\n");
  1329. goto out_phy_disconnect;
  1330. }
  1331. ret = request_irq(priv->irq1, bcm_sysport_tx_isr, 0, dev->name, dev);
  1332. if (ret) {
  1333. netdev_err(dev, "failed to request TX interrupt\n");
  1334. goto out_free_irq0;
  1335. }
  1336. /* Initialize both hardware and software ring */
  1337. for (i = 0; i < dev->num_tx_queues; i++) {
  1338. ret = bcm_sysport_init_tx_ring(priv, i);
  1339. if (ret) {
  1340. netdev_err(dev, "failed to initialize TX ring %d\n",
  1341. i);
  1342. goto out_free_tx_ring;
  1343. }
  1344. }
  1345. /* Initialize linked-list */
  1346. tdma_writel(priv, TDMA_LL_RAM_INIT_BUSY, TDMA_STATUS);
  1347. /* Initialize RX ring */
  1348. ret = bcm_sysport_init_rx_ring(priv);
  1349. if (ret) {
  1350. netdev_err(dev, "failed to initialize RX ring\n");
  1351. goto out_free_rx_ring;
  1352. }
  1353. /* Turn on RDMA */
  1354. ret = rdma_enable_set(priv, 1);
  1355. if (ret)
  1356. goto out_free_rx_ring;
  1357. /* Turn on TDMA */
  1358. ret = tdma_enable_set(priv, 1);
  1359. if (ret)
  1360. goto out_clear_rx_int;
  1361. /* Turn on UniMAC TX/RX */
  1362. umac_enable_set(priv, CMD_RX_EN | CMD_TX_EN, 1);
  1363. bcm_sysport_netif_start(dev);
  1364. return 0;
  1365. out_clear_rx_int:
  1366. intrl2_0_mask_set(priv, INTRL2_0_RDMA_MBDONE | INTRL2_0_TX_RING_FULL);
  1367. out_free_rx_ring:
  1368. bcm_sysport_fini_rx_ring(priv);
  1369. out_free_tx_ring:
  1370. for (i = 0; i < dev->num_tx_queues; i++)
  1371. bcm_sysport_fini_tx_ring(priv, i);
  1372. free_irq(priv->irq1, dev);
  1373. out_free_irq0:
  1374. free_irq(priv->irq0, dev);
  1375. out_phy_disconnect:
  1376. phy_disconnect(priv->phydev);
  1377. return ret;
  1378. }
  1379. static void bcm_sysport_netif_stop(struct net_device *dev)
  1380. {
  1381. struct bcm_sysport_priv *priv = netdev_priv(dev);
  1382. /* stop all software from updating hardware */
  1383. netif_tx_stop_all_queues(dev);
  1384. napi_disable(&priv->napi);
  1385. phy_stop(priv->phydev);
  1386. /* mask all interrupts */
  1387. intrl2_0_mask_set(priv, 0xffffffff);
  1388. intrl2_0_writel(priv, 0xffffffff, INTRL2_CPU_CLEAR);
  1389. intrl2_1_mask_set(priv, 0xffffffff);
  1390. intrl2_1_writel(priv, 0xffffffff, INTRL2_CPU_CLEAR);
  1391. }
  1392. static int bcm_sysport_stop(struct net_device *dev)
  1393. {
  1394. struct bcm_sysport_priv *priv = netdev_priv(dev);
  1395. unsigned int i;
  1396. int ret;
  1397. bcm_sysport_netif_stop(dev);
  1398. /* Disable UniMAC RX */
  1399. umac_enable_set(priv, CMD_RX_EN, 0);
  1400. ret = tdma_enable_set(priv, 0);
  1401. if (ret) {
  1402. netdev_err(dev, "timeout disabling RDMA\n");
  1403. return ret;
  1404. }
  1405. /* Wait for a maximum packet size to be drained */
  1406. usleep_range(2000, 3000);
  1407. ret = rdma_enable_set(priv, 0);
  1408. if (ret) {
  1409. netdev_err(dev, "timeout disabling TDMA\n");
  1410. return ret;
  1411. }
  1412. /* Disable UniMAC TX */
  1413. umac_enable_set(priv, CMD_TX_EN, 0);
  1414. /* Free RX/TX rings SW structures */
  1415. for (i = 0; i < dev->num_tx_queues; i++)
  1416. bcm_sysport_fini_tx_ring(priv, i);
  1417. bcm_sysport_fini_rx_ring(priv);
  1418. free_irq(priv->irq0, dev);
  1419. free_irq(priv->irq1, dev);
  1420. /* Disconnect from PHY */
  1421. phy_disconnect(priv->phydev);
  1422. return 0;
  1423. }
  1424. static struct ethtool_ops bcm_sysport_ethtool_ops = {
  1425. .get_settings = bcm_sysport_get_settings,
  1426. .set_settings = bcm_sysport_set_settings,
  1427. .get_drvinfo = bcm_sysport_get_drvinfo,
  1428. .get_msglevel = bcm_sysport_get_msglvl,
  1429. .set_msglevel = bcm_sysport_set_msglvl,
  1430. .get_link = ethtool_op_get_link,
  1431. .get_strings = bcm_sysport_get_strings,
  1432. .get_ethtool_stats = bcm_sysport_get_stats,
  1433. .get_sset_count = bcm_sysport_get_sset_count,
  1434. .get_wol = bcm_sysport_get_wol,
  1435. .set_wol = bcm_sysport_set_wol,
  1436. .get_coalesce = bcm_sysport_get_coalesce,
  1437. .set_coalesce = bcm_sysport_set_coalesce,
  1438. };
  1439. static const struct net_device_ops bcm_sysport_netdev_ops = {
  1440. .ndo_start_xmit = bcm_sysport_xmit,
  1441. .ndo_tx_timeout = bcm_sysport_tx_timeout,
  1442. .ndo_open = bcm_sysport_open,
  1443. .ndo_stop = bcm_sysport_stop,
  1444. .ndo_set_features = bcm_sysport_set_features,
  1445. .ndo_set_rx_mode = bcm_sysport_set_rx_mode,
  1446. .ndo_set_mac_address = bcm_sysport_change_mac,
  1447. #ifdef CONFIG_NET_POLL_CONTROLLER
  1448. .ndo_poll_controller = bcm_sysport_poll_controller,
  1449. #endif
  1450. };
  1451. #define REV_FMT "v%2x.%02x"
  1452. static int bcm_sysport_probe(struct platform_device *pdev)
  1453. {
  1454. struct bcm_sysport_priv *priv;
  1455. struct device_node *dn;
  1456. struct net_device *dev;
  1457. const void *macaddr;
  1458. struct resource *r;
  1459. u32 txq, rxq;
  1460. int ret;
  1461. dn = pdev->dev.of_node;
  1462. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1463. /* Read the Transmit/Receive Queue properties */
  1464. if (of_property_read_u32(dn, "systemport,num-txq", &txq))
  1465. txq = TDMA_NUM_RINGS;
  1466. if (of_property_read_u32(dn, "systemport,num-rxq", &rxq))
  1467. rxq = 1;
  1468. dev = alloc_etherdev_mqs(sizeof(*priv), txq, rxq);
  1469. if (!dev)
  1470. return -ENOMEM;
  1471. /* Initialize private members */
  1472. priv = netdev_priv(dev);
  1473. priv->irq0 = platform_get_irq(pdev, 0);
  1474. priv->irq1 = platform_get_irq(pdev, 1);
  1475. priv->wol_irq = platform_get_irq(pdev, 2);
  1476. if (priv->irq0 <= 0 || priv->irq1 <= 0) {
  1477. dev_err(&pdev->dev, "invalid interrupts\n");
  1478. ret = -EINVAL;
  1479. goto err;
  1480. }
  1481. priv->base = devm_ioremap_resource(&pdev->dev, r);
  1482. if (IS_ERR(priv->base)) {
  1483. ret = PTR_ERR(priv->base);
  1484. goto err;
  1485. }
  1486. priv->netdev = dev;
  1487. priv->pdev = pdev;
  1488. priv->phy_interface = of_get_phy_mode(dn);
  1489. /* Default to GMII interface mode */
  1490. if (priv->phy_interface < 0)
  1491. priv->phy_interface = PHY_INTERFACE_MODE_GMII;
  1492. /* In the case of a fixed PHY, the DT node associated
  1493. * to the PHY is the Ethernet MAC DT node.
  1494. */
  1495. if (of_phy_is_fixed_link(dn)) {
  1496. ret = of_phy_register_fixed_link(dn);
  1497. if (ret) {
  1498. dev_err(&pdev->dev, "failed to register fixed PHY\n");
  1499. goto err;
  1500. }
  1501. priv->phy_dn = dn;
  1502. }
  1503. /* Initialize netdevice members */
  1504. macaddr = of_get_mac_address(dn);
  1505. if (!macaddr || !is_valid_ether_addr(macaddr)) {
  1506. dev_warn(&pdev->dev, "using random Ethernet MAC\n");
  1507. eth_hw_addr_random(dev);
  1508. } else {
  1509. ether_addr_copy(dev->dev_addr, macaddr);
  1510. }
  1511. SET_NETDEV_DEV(dev, &pdev->dev);
  1512. dev_set_drvdata(&pdev->dev, dev);
  1513. dev->ethtool_ops = &bcm_sysport_ethtool_ops;
  1514. dev->netdev_ops = &bcm_sysport_netdev_ops;
  1515. netif_napi_add(dev, &priv->napi, bcm_sysport_poll, 64);
  1516. /* HW supported features, none enabled by default */
  1517. dev->hw_features |= NETIF_F_RXCSUM | NETIF_F_HIGHDMA |
  1518. NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
  1519. /* Request the WOL interrupt and advertise suspend if available */
  1520. priv->wol_irq_disabled = 1;
  1521. ret = devm_request_irq(&pdev->dev, priv->wol_irq,
  1522. bcm_sysport_wol_isr, 0, dev->name, priv);
  1523. if (!ret)
  1524. device_set_wakeup_capable(&pdev->dev, 1);
  1525. /* Set the needed headroom once and for all */
  1526. BUILD_BUG_ON(sizeof(struct bcm_tsb) != 8);
  1527. dev->needed_headroom += sizeof(struct bcm_tsb);
  1528. /* libphy will adjust the link state accordingly */
  1529. netif_carrier_off(dev);
  1530. ret = register_netdev(dev);
  1531. if (ret) {
  1532. dev_err(&pdev->dev, "failed to register net_device\n");
  1533. goto err;
  1534. }
  1535. priv->rev = topctrl_readl(priv, REV_CNTL) & REV_MASK;
  1536. dev_info(&pdev->dev,
  1537. "Broadcom SYSTEMPORT" REV_FMT
  1538. " at 0x%p (irqs: %d, %d, TXQs: %d, RXQs: %d)\n",
  1539. (priv->rev >> 8) & 0xff, priv->rev & 0xff,
  1540. priv->base, priv->irq0, priv->irq1, txq, rxq);
  1541. return 0;
  1542. err:
  1543. free_netdev(dev);
  1544. return ret;
  1545. }
  1546. static int bcm_sysport_remove(struct platform_device *pdev)
  1547. {
  1548. struct net_device *dev = dev_get_drvdata(&pdev->dev);
  1549. /* Not much to do, ndo_close has been called
  1550. * and we use managed allocations
  1551. */
  1552. unregister_netdev(dev);
  1553. free_netdev(dev);
  1554. dev_set_drvdata(&pdev->dev, NULL);
  1555. return 0;
  1556. }
  1557. #ifdef CONFIG_PM_SLEEP
  1558. static int bcm_sysport_suspend_to_wol(struct bcm_sysport_priv *priv)
  1559. {
  1560. struct net_device *ndev = priv->netdev;
  1561. unsigned int timeout = 1000;
  1562. u32 reg;
  1563. reg = umac_readl(priv, UMAC_MPD_CTRL);
  1564. reg |= MPD_EN;
  1565. reg &= ~PSW_EN;
  1566. if (priv->wolopts & WAKE_MAGICSECURE) {
  1567. /* Program the SecureOn password */
  1568. umac_writel(priv, get_unaligned_be16(&priv->sopass[0]),
  1569. UMAC_PSW_MS);
  1570. umac_writel(priv, get_unaligned_be32(&priv->sopass[2]),
  1571. UMAC_PSW_LS);
  1572. reg |= PSW_EN;
  1573. }
  1574. umac_writel(priv, reg, UMAC_MPD_CTRL);
  1575. /* Make sure RBUF entered WoL mode as result */
  1576. do {
  1577. reg = rbuf_readl(priv, RBUF_STATUS);
  1578. if (reg & RBUF_WOL_MODE)
  1579. break;
  1580. udelay(10);
  1581. } while (timeout-- > 0);
  1582. /* Do not leave the UniMAC RBUF matching only MPD packets */
  1583. if (!timeout) {
  1584. reg = umac_readl(priv, UMAC_MPD_CTRL);
  1585. reg &= ~MPD_EN;
  1586. umac_writel(priv, reg, UMAC_MPD_CTRL);
  1587. netif_err(priv, wol, ndev, "failed to enter WOL mode\n");
  1588. return -ETIMEDOUT;
  1589. }
  1590. /* UniMAC receive needs to be turned on */
  1591. umac_enable_set(priv, CMD_RX_EN, 1);
  1592. netif_dbg(priv, wol, ndev, "entered WOL mode\n");
  1593. return 0;
  1594. }
  1595. static int bcm_sysport_suspend(struct device *d)
  1596. {
  1597. struct net_device *dev = dev_get_drvdata(d);
  1598. struct bcm_sysport_priv *priv = netdev_priv(dev);
  1599. unsigned int i;
  1600. int ret = 0;
  1601. u32 reg;
  1602. if (!netif_running(dev))
  1603. return 0;
  1604. bcm_sysport_netif_stop(dev);
  1605. phy_suspend(priv->phydev);
  1606. netif_device_detach(dev);
  1607. /* Disable UniMAC RX */
  1608. umac_enable_set(priv, CMD_RX_EN, 0);
  1609. ret = rdma_enable_set(priv, 0);
  1610. if (ret) {
  1611. netdev_err(dev, "RDMA timeout!\n");
  1612. return ret;
  1613. }
  1614. /* Disable RXCHK if enabled */
  1615. if (priv->rx_chk_en) {
  1616. reg = rxchk_readl(priv, RXCHK_CONTROL);
  1617. reg &= ~RXCHK_EN;
  1618. rxchk_writel(priv, reg, RXCHK_CONTROL);
  1619. }
  1620. /* Flush RX pipe */
  1621. if (!priv->wolopts)
  1622. topctrl_writel(priv, RX_FLUSH, RX_FLUSH_CNTL);
  1623. ret = tdma_enable_set(priv, 0);
  1624. if (ret) {
  1625. netdev_err(dev, "TDMA timeout!\n");
  1626. return ret;
  1627. }
  1628. /* Wait for a packet boundary */
  1629. usleep_range(2000, 3000);
  1630. umac_enable_set(priv, CMD_TX_EN, 0);
  1631. topctrl_writel(priv, TX_FLUSH, TX_FLUSH_CNTL);
  1632. /* Free RX/TX rings SW structures */
  1633. for (i = 0; i < dev->num_tx_queues; i++)
  1634. bcm_sysport_fini_tx_ring(priv, i);
  1635. bcm_sysport_fini_rx_ring(priv);
  1636. /* Get prepared for Wake-on-LAN */
  1637. if (device_may_wakeup(d) && priv->wolopts)
  1638. ret = bcm_sysport_suspend_to_wol(priv);
  1639. return ret;
  1640. }
  1641. static int bcm_sysport_resume(struct device *d)
  1642. {
  1643. struct net_device *dev = dev_get_drvdata(d);
  1644. struct bcm_sysport_priv *priv = netdev_priv(dev);
  1645. unsigned int i;
  1646. u32 reg;
  1647. int ret;
  1648. if (!netif_running(dev))
  1649. return 0;
  1650. umac_reset(priv);
  1651. /* We may have been suspended and never received a WOL event that
  1652. * would turn off MPD detection, take care of that now
  1653. */
  1654. bcm_sysport_resume_from_wol(priv);
  1655. /* Initialize both hardware and software ring */
  1656. for (i = 0; i < dev->num_tx_queues; i++) {
  1657. ret = bcm_sysport_init_tx_ring(priv, i);
  1658. if (ret) {
  1659. netdev_err(dev, "failed to initialize TX ring %d\n",
  1660. i);
  1661. goto out_free_tx_rings;
  1662. }
  1663. }
  1664. /* Initialize linked-list */
  1665. tdma_writel(priv, TDMA_LL_RAM_INIT_BUSY, TDMA_STATUS);
  1666. /* Initialize RX ring */
  1667. ret = bcm_sysport_init_rx_ring(priv);
  1668. if (ret) {
  1669. netdev_err(dev, "failed to initialize RX ring\n");
  1670. goto out_free_rx_ring;
  1671. }
  1672. netif_device_attach(dev);
  1673. /* RX pipe enable */
  1674. topctrl_writel(priv, 0, RX_FLUSH_CNTL);
  1675. ret = rdma_enable_set(priv, 1);
  1676. if (ret) {
  1677. netdev_err(dev, "failed to enable RDMA\n");
  1678. goto out_free_rx_ring;
  1679. }
  1680. /* Enable rxhck */
  1681. if (priv->rx_chk_en) {
  1682. reg = rxchk_readl(priv, RXCHK_CONTROL);
  1683. reg |= RXCHK_EN;
  1684. rxchk_writel(priv, reg, RXCHK_CONTROL);
  1685. }
  1686. rbuf_init(priv);
  1687. /* Set maximum frame length */
  1688. umac_writel(priv, UMAC_MAX_MTU_SIZE, UMAC_MAX_FRAME_LEN);
  1689. /* Set MAC address */
  1690. umac_set_hw_addr(priv, dev->dev_addr);
  1691. umac_enable_set(priv, CMD_RX_EN, 1);
  1692. /* TX pipe enable */
  1693. topctrl_writel(priv, 0, TX_FLUSH_CNTL);
  1694. umac_enable_set(priv, CMD_TX_EN, 1);
  1695. ret = tdma_enable_set(priv, 1);
  1696. if (ret) {
  1697. netdev_err(dev, "TDMA timeout!\n");
  1698. goto out_free_rx_ring;
  1699. }
  1700. phy_resume(priv->phydev);
  1701. bcm_sysport_netif_start(dev);
  1702. return 0;
  1703. out_free_rx_ring:
  1704. bcm_sysport_fini_rx_ring(priv);
  1705. out_free_tx_rings:
  1706. for (i = 0; i < dev->num_tx_queues; i++)
  1707. bcm_sysport_fini_tx_ring(priv, i);
  1708. return ret;
  1709. }
  1710. #endif
  1711. static SIMPLE_DEV_PM_OPS(bcm_sysport_pm_ops,
  1712. bcm_sysport_suspend, bcm_sysport_resume);
  1713. static const struct of_device_id bcm_sysport_of_match[] = {
  1714. { .compatible = "brcm,systemport-v1.00" },
  1715. { .compatible = "brcm,systemport" },
  1716. { /* sentinel */ }
  1717. };
  1718. MODULE_DEVICE_TABLE(of, bcm_sysport_of_match);
  1719. static struct platform_driver bcm_sysport_driver = {
  1720. .probe = bcm_sysport_probe,
  1721. .remove = bcm_sysport_remove,
  1722. .driver = {
  1723. .name = "brcm-systemport",
  1724. .of_match_table = bcm_sysport_of_match,
  1725. .pm = &bcm_sysport_pm_ops,
  1726. },
  1727. };
  1728. module_platform_driver(bcm_sysport_driver);
  1729. MODULE_AUTHOR("Broadcom Corporation");
  1730. MODULE_DESCRIPTION("Broadcom System Port Ethernet MAC driver");
  1731. MODULE_ALIAS("platform:brcm-systemport");
  1732. MODULE_LICENSE("GPL");