mtk_eth_soc.c 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546
  1. /* This program is free software; you can redistribute it and/or modify
  2. * it under the terms of the GNU General Public License as published by
  3. * the Free Software Foundation; version 2 of the License
  4. *
  5. * This program is distributed in the hope that it will be useful,
  6. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  7. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8. * GNU General Public License for more details.
  9. *
  10. * Copyright (C) 2009-2016 John Crispin <blogic@openwrt.org>
  11. * Copyright (C) 2009-2016 Felix Fietkau <nbd@openwrt.org>
  12. * Copyright (C) 2013-2016 Michael Lee <igvtee@gmail.com>
  13. */
  14. #include <linux/of_device.h>
  15. #include <linux/of_mdio.h>
  16. #include <linux/of_net.h>
  17. #include <linux/mfd/syscon.h>
  18. #include <linux/regmap.h>
  19. #include <linux/clk.h>
  20. #include <linux/pm_runtime.h>
  21. #include <linux/if_vlan.h>
  22. #include <linux/reset.h>
  23. #include <linux/tcp.h>
  24. #include "mtk_eth_soc.h"
  25. static int mtk_msg_level = -1;
  26. module_param_named(msg_level, mtk_msg_level, int, 0);
  27. MODULE_PARM_DESC(msg_level, "Message level (-1=defaults,0=none,...,16=all)");
  28. #define MTK_ETHTOOL_STAT(x) { #x, \
  29. offsetof(struct mtk_hw_stats, x) / sizeof(u64) }
  30. /* strings used by ethtool */
  31. static const struct mtk_ethtool_stats {
  32. char str[ETH_GSTRING_LEN];
  33. u32 offset;
  34. } mtk_ethtool_stats[] = {
  35. MTK_ETHTOOL_STAT(tx_bytes),
  36. MTK_ETHTOOL_STAT(tx_packets),
  37. MTK_ETHTOOL_STAT(tx_skip),
  38. MTK_ETHTOOL_STAT(tx_collisions),
  39. MTK_ETHTOOL_STAT(rx_bytes),
  40. MTK_ETHTOOL_STAT(rx_packets),
  41. MTK_ETHTOOL_STAT(rx_overflow),
  42. MTK_ETHTOOL_STAT(rx_fcs_errors),
  43. MTK_ETHTOOL_STAT(rx_short_errors),
  44. MTK_ETHTOOL_STAT(rx_long_errors),
  45. MTK_ETHTOOL_STAT(rx_checksum_errors),
  46. MTK_ETHTOOL_STAT(rx_flow_control_packets),
  47. };
  48. static const char * const mtk_clks_source_name[] = {
  49. "ethif", "esw", "gp1", "gp2", "trgpll"
  50. };
  51. void mtk_w32(struct mtk_eth *eth, u32 val, unsigned reg)
  52. {
  53. __raw_writel(val, eth->base + reg);
  54. }
  55. u32 mtk_r32(struct mtk_eth *eth, unsigned reg)
  56. {
  57. return __raw_readl(eth->base + reg);
  58. }
  59. static int mtk_mdio_busy_wait(struct mtk_eth *eth)
  60. {
  61. unsigned long t_start = jiffies;
  62. while (1) {
  63. if (!(mtk_r32(eth, MTK_PHY_IAC) & PHY_IAC_ACCESS))
  64. return 0;
  65. if (time_after(jiffies, t_start + PHY_IAC_TIMEOUT))
  66. break;
  67. usleep_range(10, 20);
  68. }
  69. dev_err(eth->dev, "mdio: MDIO timeout\n");
  70. return -1;
  71. }
  72. static u32 _mtk_mdio_write(struct mtk_eth *eth, u32 phy_addr,
  73. u32 phy_register, u32 write_data)
  74. {
  75. if (mtk_mdio_busy_wait(eth))
  76. return -1;
  77. write_data &= 0xffff;
  78. mtk_w32(eth, PHY_IAC_ACCESS | PHY_IAC_START | PHY_IAC_WRITE |
  79. (phy_register << PHY_IAC_REG_SHIFT) |
  80. (phy_addr << PHY_IAC_ADDR_SHIFT) | write_data,
  81. MTK_PHY_IAC);
  82. if (mtk_mdio_busy_wait(eth))
  83. return -1;
  84. return 0;
  85. }
  86. static u32 _mtk_mdio_read(struct mtk_eth *eth, int phy_addr, int phy_reg)
  87. {
  88. u32 d;
  89. if (mtk_mdio_busy_wait(eth))
  90. return 0xffff;
  91. mtk_w32(eth, PHY_IAC_ACCESS | PHY_IAC_START | PHY_IAC_READ |
  92. (phy_reg << PHY_IAC_REG_SHIFT) |
  93. (phy_addr << PHY_IAC_ADDR_SHIFT),
  94. MTK_PHY_IAC);
  95. if (mtk_mdio_busy_wait(eth))
  96. return 0xffff;
  97. d = mtk_r32(eth, MTK_PHY_IAC) & 0xffff;
  98. return d;
  99. }
  100. static int mtk_mdio_write(struct mii_bus *bus, int phy_addr,
  101. int phy_reg, u16 val)
  102. {
  103. struct mtk_eth *eth = bus->priv;
  104. return _mtk_mdio_write(eth, phy_addr, phy_reg, val);
  105. }
  106. static int mtk_mdio_read(struct mii_bus *bus, int phy_addr, int phy_reg)
  107. {
  108. struct mtk_eth *eth = bus->priv;
  109. return _mtk_mdio_read(eth, phy_addr, phy_reg);
  110. }
  111. static void mtk_gmac0_rgmii_adjust(struct mtk_eth *eth, int speed)
  112. {
  113. u32 val;
  114. int ret;
  115. val = (speed == SPEED_1000) ?
  116. INTF_MODE_RGMII_1000 : INTF_MODE_RGMII_10_100;
  117. mtk_w32(eth, val, INTF_MODE);
  118. regmap_update_bits(eth->ethsys, ETHSYS_CLKCFG0,
  119. ETHSYS_TRGMII_CLK_SEL362_5,
  120. ETHSYS_TRGMII_CLK_SEL362_5);
  121. val = (speed == SPEED_1000) ? 250000000 : 500000000;
  122. ret = clk_set_rate(eth->clks[MTK_CLK_TRGPLL], val);
  123. if (ret)
  124. dev_err(eth->dev, "Failed to set trgmii pll: %d\n", ret);
  125. val = (speed == SPEED_1000) ?
  126. RCK_CTRL_RGMII_1000 : RCK_CTRL_RGMII_10_100;
  127. mtk_w32(eth, val, TRGMII_RCK_CTRL);
  128. val = (speed == SPEED_1000) ?
  129. TCK_CTRL_RGMII_1000 : TCK_CTRL_RGMII_10_100;
  130. mtk_w32(eth, val, TRGMII_TCK_CTRL);
  131. }
  132. static void mtk_phy_link_adjust(struct net_device *dev)
  133. {
  134. struct mtk_mac *mac = netdev_priv(dev);
  135. u16 lcl_adv = 0, rmt_adv = 0;
  136. u8 flowctrl;
  137. u32 mcr = MAC_MCR_MAX_RX_1536 | MAC_MCR_IPG_CFG |
  138. MAC_MCR_FORCE_MODE | MAC_MCR_TX_EN |
  139. MAC_MCR_RX_EN | MAC_MCR_BACKOFF_EN |
  140. MAC_MCR_BACKPR_EN;
  141. if (unlikely(test_bit(MTK_RESETTING, &mac->hw->state)))
  142. return;
  143. switch (dev->phydev->speed) {
  144. case SPEED_1000:
  145. mcr |= MAC_MCR_SPEED_1000;
  146. break;
  147. case SPEED_100:
  148. mcr |= MAC_MCR_SPEED_100;
  149. break;
  150. };
  151. if (mac->id == 0 && !mac->trgmii)
  152. mtk_gmac0_rgmii_adjust(mac->hw, dev->phydev->speed);
  153. if (dev->phydev->link)
  154. mcr |= MAC_MCR_FORCE_LINK;
  155. if (dev->phydev->duplex) {
  156. mcr |= MAC_MCR_FORCE_DPX;
  157. if (dev->phydev->pause)
  158. rmt_adv = LPA_PAUSE_CAP;
  159. if (dev->phydev->asym_pause)
  160. rmt_adv |= LPA_PAUSE_ASYM;
  161. if (dev->phydev->advertising & ADVERTISED_Pause)
  162. lcl_adv |= ADVERTISE_PAUSE_CAP;
  163. if (dev->phydev->advertising & ADVERTISED_Asym_Pause)
  164. lcl_adv |= ADVERTISE_PAUSE_ASYM;
  165. flowctrl = mii_resolve_flowctrl_fdx(lcl_adv, rmt_adv);
  166. if (flowctrl & FLOW_CTRL_TX)
  167. mcr |= MAC_MCR_FORCE_TX_FC;
  168. if (flowctrl & FLOW_CTRL_RX)
  169. mcr |= MAC_MCR_FORCE_RX_FC;
  170. netif_dbg(mac->hw, link, dev, "rx pause %s, tx pause %s\n",
  171. flowctrl & FLOW_CTRL_RX ? "enabled" : "disabled",
  172. flowctrl & FLOW_CTRL_TX ? "enabled" : "disabled");
  173. }
  174. mtk_w32(mac->hw, mcr, MTK_MAC_MCR(mac->id));
  175. if (dev->phydev->link)
  176. netif_carrier_on(dev);
  177. else
  178. netif_carrier_off(dev);
  179. }
  180. static int mtk_phy_connect_node(struct mtk_eth *eth, struct mtk_mac *mac,
  181. struct device_node *phy_node)
  182. {
  183. struct phy_device *phydev;
  184. int phy_mode;
  185. phy_mode = of_get_phy_mode(phy_node);
  186. if (phy_mode < 0) {
  187. dev_err(eth->dev, "incorrect phy-mode %d\n", phy_mode);
  188. return -EINVAL;
  189. }
  190. phydev = of_phy_connect(eth->netdev[mac->id], phy_node,
  191. mtk_phy_link_adjust, 0, phy_mode);
  192. if (!phydev) {
  193. dev_err(eth->dev, "could not connect to PHY\n");
  194. return -ENODEV;
  195. }
  196. dev_info(eth->dev,
  197. "connected mac %d to PHY at %s [uid=%08x, driver=%s]\n",
  198. mac->id, phydev_name(phydev), phydev->phy_id,
  199. phydev->drv->name);
  200. return 0;
  201. }
  202. static int mtk_phy_connect(struct net_device *dev)
  203. {
  204. struct mtk_mac *mac = netdev_priv(dev);
  205. struct mtk_eth *eth;
  206. struct device_node *np;
  207. u32 val;
  208. eth = mac->hw;
  209. np = of_parse_phandle(mac->of_node, "phy-handle", 0);
  210. if (!np && of_phy_is_fixed_link(mac->of_node))
  211. if (!of_phy_register_fixed_link(mac->of_node))
  212. np = of_node_get(mac->of_node);
  213. if (!np)
  214. return -ENODEV;
  215. switch (of_get_phy_mode(np)) {
  216. case PHY_INTERFACE_MODE_TRGMII:
  217. mac->trgmii = true;
  218. case PHY_INTERFACE_MODE_RGMII_TXID:
  219. case PHY_INTERFACE_MODE_RGMII_RXID:
  220. case PHY_INTERFACE_MODE_RGMII_ID:
  221. case PHY_INTERFACE_MODE_RGMII:
  222. mac->ge_mode = 0;
  223. break;
  224. case PHY_INTERFACE_MODE_MII:
  225. mac->ge_mode = 1;
  226. break;
  227. case PHY_INTERFACE_MODE_REVMII:
  228. mac->ge_mode = 2;
  229. break;
  230. case PHY_INTERFACE_MODE_RMII:
  231. if (!mac->id)
  232. goto err_phy;
  233. mac->ge_mode = 3;
  234. break;
  235. default:
  236. goto err_phy;
  237. }
  238. /* put the gmac into the right mode */
  239. regmap_read(eth->ethsys, ETHSYS_SYSCFG0, &val);
  240. val &= ~SYSCFG0_GE_MODE(SYSCFG0_GE_MASK, mac->id);
  241. val |= SYSCFG0_GE_MODE(mac->ge_mode, mac->id);
  242. regmap_write(eth->ethsys, ETHSYS_SYSCFG0, val);
  243. /* couple phydev to net_device */
  244. if (mtk_phy_connect_node(eth, mac, np))
  245. goto err_phy;
  246. dev->phydev->autoneg = AUTONEG_ENABLE;
  247. dev->phydev->speed = 0;
  248. dev->phydev->duplex = 0;
  249. if (of_phy_is_fixed_link(mac->of_node))
  250. dev->phydev->supported |=
  251. SUPPORTED_Pause | SUPPORTED_Asym_Pause;
  252. dev->phydev->supported &= PHY_GBIT_FEATURES | SUPPORTED_Pause |
  253. SUPPORTED_Asym_Pause;
  254. dev->phydev->advertising = dev->phydev->supported |
  255. ADVERTISED_Autoneg;
  256. phy_start_aneg(dev->phydev);
  257. of_node_put(np);
  258. return 0;
  259. err_phy:
  260. if (of_phy_is_fixed_link(mac->of_node))
  261. of_phy_deregister_fixed_link(mac->of_node);
  262. of_node_put(np);
  263. dev_err(eth->dev, "%s: invalid phy\n", __func__);
  264. return -EINVAL;
  265. }
  266. static int mtk_mdio_init(struct mtk_eth *eth)
  267. {
  268. struct device_node *mii_np;
  269. int ret;
  270. mii_np = of_get_child_by_name(eth->dev->of_node, "mdio-bus");
  271. if (!mii_np) {
  272. dev_err(eth->dev, "no %s child node found", "mdio-bus");
  273. return -ENODEV;
  274. }
  275. if (!of_device_is_available(mii_np)) {
  276. ret = -ENODEV;
  277. goto err_put_node;
  278. }
  279. eth->mii_bus = devm_mdiobus_alloc(eth->dev);
  280. if (!eth->mii_bus) {
  281. ret = -ENOMEM;
  282. goto err_put_node;
  283. }
  284. eth->mii_bus->name = "mdio";
  285. eth->mii_bus->read = mtk_mdio_read;
  286. eth->mii_bus->write = mtk_mdio_write;
  287. eth->mii_bus->priv = eth;
  288. eth->mii_bus->parent = eth->dev;
  289. snprintf(eth->mii_bus->id, MII_BUS_ID_SIZE, "%s", mii_np->name);
  290. ret = of_mdiobus_register(eth->mii_bus, mii_np);
  291. err_put_node:
  292. of_node_put(mii_np);
  293. return ret;
  294. }
  295. static void mtk_mdio_cleanup(struct mtk_eth *eth)
  296. {
  297. if (!eth->mii_bus)
  298. return;
  299. mdiobus_unregister(eth->mii_bus);
  300. }
  301. static inline void mtk_irq_disable(struct mtk_eth *eth,
  302. unsigned reg, u32 mask)
  303. {
  304. unsigned long flags;
  305. u32 val;
  306. spin_lock_irqsave(&eth->irq_lock, flags);
  307. val = mtk_r32(eth, reg);
  308. mtk_w32(eth, val & ~mask, reg);
  309. spin_unlock_irqrestore(&eth->irq_lock, flags);
  310. }
  311. static inline void mtk_irq_enable(struct mtk_eth *eth,
  312. unsigned reg, u32 mask)
  313. {
  314. unsigned long flags;
  315. u32 val;
  316. spin_lock_irqsave(&eth->irq_lock, flags);
  317. val = mtk_r32(eth, reg);
  318. mtk_w32(eth, val | mask, reg);
  319. spin_unlock_irqrestore(&eth->irq_lock, flags);
  320. }
  321. static int mtk_set_mac_address(struct net_device *dev, void *p)
  322. {
  323. int ret = eth_mac_addr(dev, p);
  324. struct mtk_mac *mac = netdev_priv(dev);
  325. const char *macaddr = dev->dev_addr;
  326. if (ret)
  327. return ret;
  328. if (unlikely(test_bit(MTK_RESETTING, &mac->hw->state)))
  329. return -EBUSY;
  330. spin_lock_bh(&mac->hw->page_lock);
  331. mtk_w32(mac->hw, (macaddr[0] << 8) | macaddr[1],
  332. MTK_GDMA_MAC_ADRH(mac->id));
  333. mtk_w32(mac->hw, (macaddr[2] << 24) | (macaddr[3] << 16) |
  334. (macaddr[4] << 8) | macaddr[5],
  335. MTK_GDMA_MAC_ADRL(mac->id));
  336. spin_unlock_bh(&mac->hw->page_lock);
  337. return 0;
  338. }
  339. void mtk_stats_update_mac(struct mtk_mac *mac)
  340. {
  341. struct mtk_hw_stats *hw_stats = mac->hw_stats;
  342. unsigned int base = MTK_GDM1_TX_GBCNT;
  343. u64 stats;
  344. base += hw_stats->reg_offset;
  345. u64_stats_update_begin(&hw_stats->syncp);
  346. hw_stats->rx_bytes += mtk_r32(mac->hw, base);
  347. stats = mtk_r32(mac->hw, base + 0x04);
  348. if (stats)
  349. hw_stats->rx_bytes += (stats << 32);
  350. hw_stats->rx_packets += mtk_r32(mac->hw, base + 0x08);
  351. hw_stats->rx_overflow += mtk_r32(mac->hw, base + 0x10);
  352. hw_stats->rx_fcs_errors += mtk_r32(mac->hw, base + 0x14);
  353. hw_stats->rx_short_errors += mtk_r32(mac->hw, base + 0x18);
  354. hw_stats->rx_long_errors += mtk_r32(mac->hw, base + 0x1c);
  355. hw_stats->rx_checksum_errors += mtk_r32(mac->hw, base + 0x20);
  356. hw_stats->rx_flow_control_packets +=
  357. mtk_r32(mac->hw, base + 0x24);
  358. hw_stats->tx_skip += mtk_r32(mac->hw, base + 0x28);
  359. hw_stats->tx_collisions += mtk_r32(mac->hw, base + 0x2c);
  360. hw_stats->tx_bytes += mtk_r32(mac->hw, base + 0x30);
  361. stats = mtk_r32(mac->hw, base + 0x34);
  362. if (stats)
  363. hw_stats->tx_bytes += (stats << 32);
  364. hw_stats->tx_packets += mtk_r32(mac->hw, base + 0x38);
  365. u64_stats_update_end(&hw_stats->syncp);
  366. }
  367. static void mtk_stats_update(struct mtk_eth *eth)
  368. {
  369. int i;
  370. for (i = 0; i < MTK_MAC_COUNT; i++) {
  371. if (!eth->mac[i] || !eth->mac[i]->hw_stats)
  372. continue;
  373. if (spin_trylock(&eth->mac[i]->hw_stats->stats_lock)) {
  374. mtk_stats_update_mac(eth->mac[i]);
  375. spin_unlock(&eth->mac[i]->hw_stats->stats_lock);
  376. }
  377. }
  378. }
  379. static void mtk_get_stats64(struct net_device *dev,
  380. struct rtnl_link_stats64 *storage)
  381. {
  382. struct mtk_mac *mac = netdev_priv(dev);
  383. struct mtk_hw_stats *hw_stats = mac->hw_stats;
  384. unsigned int start;
  385. if (netif_running(dev) && netif_device_present(dev)) {
  386. if (spin_trylock(&hw_stats->stats_lock)) {
  387. mtk_stats_update_mac(mac);
  388. spin_unlock(&hw_stats->stats_lock);
  389. }
  390. }
  391. do {
  392. start = u64_stats_fetch_begin_irq(&hw_stats->syncp);
  393. storage->rx_packets = hw_stats->rx_packets;
  394. storage->tx_packets = hw_stats->tx_packets;
  395. storage->rx_bytes = hw_stats->rx_bytes;
  396. storage->tx_bytes = hw_stats->tx_bytes;
  397. storage->collisions = hw_stats->tx_collisions;
  398. storage->rx_length_errors = hw_stats->rx_short_errors +
  399. hw_stats->rx_long_errors;
  400. storage->rx_over_errors = hw_stats->rx_overflow;
  401. storage->rx_crc_errors = hw_stats->rx_fcs_errors;
  402. storage->rx_errors = hw_stats->rx_checksum_errors;
  403. storage->tx_aborted_errors = hw_stats->tx_skip;
  404. } while (u64_stats_fetch_retry_irq(&hw_stats->syncp, start));
  405. storage->tx_errors = dev->stats.tx_errors;
  406. storage->rx_dropped = dev->stats.rx_dropped;
  407. storage->tx_dropped = dev->stats.tx_dropped;
  408. }
  409. static inline int mtk_max_frag_size(int mtu)
  410. {
  411. /* make sure buf_size will be at least MTK_MAX_RX_LENGTH */
  412. if (mtu + MTK_RX_ETH_HLEN < MTK_MAX_RX_LENGTH)
  413. mtu = MTK_MAX_RX_LENGTH - MTK_RX_ETH_HLEN;
  414. return SKB_DATA_ALIGN(MTK_RX_HLEN + mtu) +
  415. SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  416. }
  417. static inline int mtk_max_buf_size(int frag_size)
  418. {
  419. int buf_size = frag_size - NET_SKB_PAD - NET_IP_ALIGN -
  420. SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  421. WARN_ON(buf_size < MTK_MAX_RX_LENGTH);
  422. return buf_size;
  423. }
  424. static inline void mtk_rx_get_desc(struct mtk_rx_dma *rxd,
  425. struct mtk_rx_dma *dma_rxd)
  426. {
  427. rxd->rxd1 = READ_ONCE(dma_rxd->rxd1);
  428. rxd->rxd2 = READ_ONCE(dma_rxd->rxd2);
  429. rxd->rxd3 = READ_ONCE(dma_rxd->rxd3);
  430. rxd->rxd4 = READ_ONCE(dma_rxd->rxd4);
  431. }
  432. /* the qdma core needs scratch memory to be setup */
  433. static int mtk_init_fq_dma(struct mtk_eth *eth)
  434. {
  435. dma_addr_t phy_ring_tail;
  436. int cnt = MTK_DMA_SIZE;
  437. dma_addr_t dma_addr;
  438. int i;
  439. eth->scratch_ring = dma_alloc_coherent(eth->dev,
  440. cnt * sizeof(struct mtk_tx_dma),
  441. &eth->phy_scratch_ring,
  442. GFP_ATOMIC | __GFP_ZERO);
  443. if (unlikely(!eth->scratch_ring))
  444. return -ENOMEM;
  445. eth->scratch_head = kcalloc(cnt, MTK_QDMA_PAGE_SIZE,
  446. GFP_KERNEL);
  447. if (unlikely(!eth->scratch_head))
  448. return -ENOMEM;
  449. dma_addr = dma_map_single(eth->dev,
  450. eth->scratch_head, cnt * MTK_QDMA_PAGE_SIZE,
  451. DMA_FROM_DEVICE);
  452. if (unlikely(dma_mapping_error(eth->dev, dma_addr)))
  453. return -ENOMEM;
  454. memset(eth->scratch_ring, 0x0, sizeof(struct mtk_tx_dma) * cnt);
  455. phy_ring_tail = eth->phy_scratch_ring +
  456. (sizeof(struct mtk_tx_dma) * (cnt - 1));
  457. for (i = 0; i < cnt; i++) {
  458. eth->scratch_ring[i].txd1 =
  459. (dma_addr + (i * MTK_QDMA_PAGE_SIZE));
  460. if (i < cnt - 1)
  461. eth->scratch_ring[i].txd2 = (eth->phy_scratch_ring +
  462. ((i + 1) * sizeof(struct mtk_tx_dma)));
  463. eth->scratch_ring[i].txd3 = TX_DMA_SDL(MTK_QDMA_PAGE_SIZE);
  464. }
  465. mtk_w32(eth, eth->phy_scratch_ring, MTK_QDMA_FQ_HEAD);
  466. mtk_w32(eth, phy_ring_tail, MTK_QDMA_FQ_TAIL);
  467. mtk_w32(eth, (cnt << 16) | cnt, MTK_QDMA_FQ_CNT);
  468. mtk_w32(eth, MTK_QDMA_PAGE_SIZE << 16, MTK_QDMA_FQ_BLEN);
  469. return 0;
  470. }
  471. static inline void *mtk_qdma_phys_to_virt(struct mtk_tx_ring *ring, u32 desc)
  472. {
  473. void *ret = ring->dma;
  474. return ret + (desc - ring->phys);
  475. }
  476. static inline struct mtk_tx_buf *mtk_desc_to_tx_buf(struct mtk_tx_ring *ring,
  477. struct mtk_tx_dma *txd)
  478. {
  479. int idx = txd - ring->dma;
  480. return &ring->buf[idx];
  481. }
  482. static void mtk_tx_unmap(struct mtk_eth *eth, struct mtk_tx_buf *tx_buf)
  483. {
  484. if (tx_buf->flags & MTK_TX_FLAGS_SINGLE0) {
  485. dma_unmap_single(eth->dev,
  486. dma_unmap_addr(tx_buf, dma_addr0),
  487. dma_unmap_len(tx_buf, dma_len0),
  488. DMA_TO_DEVICE);
  489. } else if (tx_buf->flags & MTK_TX_FLAGS_PAGE0) {
  490. dma_unmap_page(eth->dev,
  491. dma_unmap_addr(tx_buf, dma_addr0),
  492. dma_unmap_len(tx_buf, dma_len0),
  493. DMA_TO_DEVICE);
  494. }
  495. tx_buf->flags = 0;
  496. if (tx_buf->skb &&
  497. (tx_buf->skb != (struct sk_buff *)MTK_DMA_DUMMY_DESC))
  498. dev_kfree_skb_any(tx_buf->skb);
  499. tx_buf->skb = NULL;
  500. }
  501. static int mtk_tx_map(struct sk_buff *skb, struct net_device *dev,
  502. int tx_num, struct mtk_tx_ring *ring, bool gso)
  503. {
  504. struct mtk_mac *mac = netdev_priv(dev);
  505. struct mtk_eth *eth = mac->hw;
  506. struct mtk_tx_dma *itxd, *txd;
  507. struct mtk_tx_buf *itx_buf, *tx_buf;
  508. dma_addr_t mapped_addr;
  509. unsigned int nr_frags;
  510. int i, n_desc = 1;
  511. u32 txd4 = 0, fport;
  512. itxd = ring->next_free;
  513. if (itxd == ring->last_free)
  514. return -ENOMEM;
  515. /* set the forward port */
  516. fport = (mac->id + 1) << TX_DMA_FPORT_SHIFT;
  517. txd4 |= fport;
  518. itx_buf = mtk_desc_to_tx_buf(ring, itxd);
  519. memset(itx_buf, 0, sizeof(*itx_buf));
  520. if (gso)
  521. txd4 |= TX_DMA_TSO;
  522. /* TX Checksum offload */
  523. if (skb->ip_summed == CHECKSUM_PARTIAL)
  524. txd4 |= TX_DMA_CHKSUM;
  525. /* VLAN header offload */
  526. if (skb_vlan_tag_present(skb))
  527. txd4 |= TX_DMA_INS_VLAN | skb_vlan_tag_get(skb);
  528. mapped_addr = dma_map_single(eth->dev, skb->data,
  529. skb_headlen(skb), DMA_TO_DEVICE);
  530. if (unlikely(dma_mapping_error(eth->dev, mapped_addr)))
  531. return -ENOMEM;
  532. WRITE_ONCE(itxd->txd1, mapped_addr);
  533. itx_buf->flags |= MTK_TX_FLAGS_SINGLE0;
  534. itx_buf->flags |= (!mac->id) ? MTK_TX_FLAGS_FPORT0 :
  535. MTK_TX_FLAGS_FPORT1;
  536. dma_unmap_addr_set(itx_buf, dma_addr0, mapped_addr);
  537. dma_unmap_len_set(itx_buf, dma_len0, skb_headlen(skb));
  538. /* TX SG offload */
  539. txd = itxd;
  540. nr_frags = skb_shinfo(skb)->nr_frags;
  541. for (i = 0; i < nr_frags; i++) {
  542. struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i];
  543. unsigned int offset = 0;
  544. int frag_size = skb_frag_size(frag);
  545. while (frag_size) {
  546. bool last_frag = false;
  547. unsigned int frag_map_size;
  548. txd = mtk_qdma_phys_to_virt(ring, txd->txd2);
  549. if (txd == ring->last_free)
  550. goto err_dma;
  551. n_desc++;
  552. frag_map_size = min(frag_size, MTK_TX_DMA_BUF_LEN);
  553. mapped_addr = skb_frag_dma_map(eth->dev, frag, offset,
  554. frag_map_size,
  555. DMA_TO_DEVICE);
  556. if (unlikely(dma_mapping_error(eth->dev, mapped_addr)))
  557. goto err_dma;
  558. if (i == nr_frags - 1 &&
  559. (frag_size - frag_map_size) == 0)
  560. last_frag = true;
  561. WRITE_ONCE(txd->txd1, mapped_addr);
  562. WRITE_ONCE(txd->txd3, (TX_DMA_SWC |
  563. TX_DMA_PLEN0(frag_map_size) |
  564. last_frag * TX_DMA_LS0));
  565. WRITE_ONCE(txd->txd4, fport);
  566. tx_buf = mtk_desc_to_tx_buf(ring, txd);
  567. memset(tx_buf, 0, sizeof(*tx_buf));
  568. tx_buf->skb = (struct sk_buff *)MTK_DMA_DUMMY_DESC;
  569. tx_buf->flags |= MTK_TX_FLAGS_PAGE0;
  570. tx_buf->flags |= (!mac->id) ? MTK_TX_FLAGS_FPORT0 :
  571. MTK_TX_FLAGS_FPORT1;
  572. dma_unmap_addr_set(tx_buf, dma_addr0, mapped_addr);
  573. dma_unmap_len_set(tx_buf, dma_len0, frag_map_size);
  574. frag_size -= frag_map_size;
  575. offset += frag_map_size;
  576. }
  577. }
  578. /* store skb to cleanup */
  579. itx_buf->skb = skb;
  580. WRITE_ONCE(itxd->txd4, txd4);
  581. WRITE_ONCE(itxd->txd3, (TX_DMA_SWC | TX_DMA_PLEN0(skb_headlen(skb)) |
  582. (!nr_frags * TX_DMA_LS0)));
  583. netdev_sent_queue(dev, skb->len);
  584. skb_tx_timestamp(skb);
  585. ring->next_free = mtk_qdma_phys_to_virt(ring, txd->txd2);
  586. atomic_sub(n_desc, &ring->free_count);
  587. /* make sure that all changes to the dma ring are flushed before we
  588. * continue
  589. */
  590. wmb();
  591. if (netif_xmit_stopped(netdev_get_tx_queue(dev, 0)) || !skb->xmit_more)
  592. mtk_w32(eth, txd->txd2, MTK_QTX_CTX_PTR);
  593. return 0;
  594. err_dma:
  595. do {
  596. tx_buf = mtk_desc_to_tx_buf(ring, itxd);
  597. /* unmap dma */
  598. mtk_tx_unmap(eth, tx_buf);
  599. itxd->txd3 = TX_DMA_LS0 | TX_DMA_OWNER_CPU;
  600. itxd = mtk_qdma_phys_to_virt(ring, itxd->txd2);
  601. } while (itxd != txd);
  602. return -ENOMEM;
  603. }
  604. static inline int mtk_cal_txd_req(struct sk_buff *skb)
  605. {
  606. int i, nfrags;
  607. struct skb_frag_struct *frag;
  608. nfrags = 1;
  609. if (skb_is_gso(skb)) {
  610. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  611. frag = &skb_shinfo(skb)->frags[i];
  612. nfrags += DIV_ROUND_UP(frag->size, MTK_TX_DMA_BUF_LEN);
  613. }
  614. } else {
  615. nfrags += skb_shinfo(skb)->nr_frags;
  616. }
  617. return nfrags;
  618. }
  619. static int mtk_queue_stopped(struct mtk_eth *eth)
  620. {
  621. int i;
  622. for (i = 0; i < MTK_MAC_COUNT; i++) {
  623. if (!eth->netdev[i])
  624. continue;
  625. if (netif_queue_stopped(eth->netdev[i]))
  626. return 1;
  627. }
  628. return 0;
  629. }
  630. static void mtk_wake_queue(struct mtk_eth *eth)
  631. {
  632. int i;
  633. for (i = 0; i < MTK_MAC_COUNT; i++) {
  634. if (!eth->netdev[i])
  635. continue;
  636. netif_wake_queue(eth->netdev[i]);
  637. }
  638. }
  639. static void mtk_stop_queue(struct mtk_eth *eth)
  640. {
  641. int i;
  642. for (i = 0; i < MTK_MAC_COUNT; i++) {
  643. if (!eth->netdev[i])
  644. continue;
  645. netif_stop_queue(eth->netdev[i]);
  646. }
  647. }
  648. static int mtk_start_xmit(struct sk_buff *skb, struct net_device *dev)
  649. {
  650. struct mtk_mac *mac = netdev_priv(dev);
  651. struct mtk_eth *eth = mac->hw;
  652. struct mtk_tx_ring *ring = &eth->tx_ring;
  653. struct net_device_stats *stats = &dev->stats;
  654. bool gso = false;
  655. int tx_num;
  656. /* normally we can rely on the stack not calling this more than once,
  657. * however we have 2 queues running on the same ring so we need to lock
  658. * the ring access
  659. */
  660. spin_lock(&eth->page_lock);
  661. if (unlikely(test_bit(MTK_RESETTING, &eth->state)))
  662. goto drop;
  663. tx_num = mtk_cal_txd_req(skb);
  664. if (unlikely(atomic_read(&ring->free_count) <= tx_num)) {
  665. mtk_stop_queue(eth);
  666. netif_err(eth, tx_queued, dev,
  667. "Tx Ring full when queue awake!\n");
  668. spin_unlock(&eth->page_lock);
  669. return NETDEV_TX_BUSY;
  670. }
  671. /* TSO: fill MSS info in tcp checksum field */
  672. if (skb_is_gso(skb)) {
  673. if (skb_cow_head(skb, 0)) {
  674. netif_warn(eth, tx_err, dev,
  675. "GSO expand head fail.\n");
  676. goto drop;
  677. }
  678. if (skb_shinfo(skb)->gso_type &
  679. (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)) {
  680. gso = true;
  681. tcp_hdr(skb)->check = htons(skb_shinfo(skb)->gso_size);
  682. }
  683. }
  684. if (mtk_tx_map(skb, dev, tx_num, ring, gso) < 0)
  685. goto drop;
  686. if (unlikely(atomic_read(&ring->free_count) <= ring->thresh))
  687. mtk_stop_queue(eth);
  688. spin_unlock(&eth->page_lock);
  689. return NETDEV_TX_OK;
  690. drop:
  691. spin_unlock(&eth->page_lock);
  692. stats->tx_dropped++;
  693. dev_kfree_skb_any(skb);
  694. return NETDEV_TX_OK;
  695. }
  696. static struct mtk_rx_ring *mtk_get_rx_ring(struct mtk_eth *eth)
  697. {
  698. int i;
  699. struct mtk_rx_ring *ring;
  700. int idx;
  701. if (!eth->hwlro)
  702. return &eth->rx_ring[0];
  703. for (i = 0; i < MTK_MAX_RX_RING_NUM; i++) {
  704. ring = &eth->rx_ring[i];
  705. idx = NEXT_RX_DESP_IDX(ring->calc_idx, ring->dma_size);
  706. if (ring->dma[idx].rxd2 & RX_DMA_DONE) {
  707. ring->calc_idx_update = true;
  708. return ring;
  709. }
  710. }
  711. return NULL;
  712. }
  713. static void mtk_update_rx_cpu_idx(struct mtk_eth *eth)
  714. {
  715. struct mtk_rx_ring *ring;
  716. int i;
  717. if (!eth->hwlro) {
  718. ring = &eth->rx_ring[0];
  719. mtk_w32(eth, ring->calc_idx, ring->crx_idx_reg);
  720. } else {
  721. for (i = 0; i < MTK_MAX_RX_RING_NUM; i++) {
  722. ring = &eth->rx_ring[i];
  723. if (ring->calc_idx_update) {
  724. ring->calc_idx_update = false;
  725. mtk_w32(eth, ring->calc_idx, ring->crx_idx_reg);
  726. }
  727. }
  728. }
  729. }
  730. static int mtk_poll_rx(struct napi_struct *napi, int budget,
  731. struct mtk_eth *eth)
  732. {
  733. struct mtk_rx_ring *ring;
  734. int idx;
  735. struct sk_buff *skb;
  736. u8 *data, *new_data;
  737. struct mtk_rx_dma *rxd, trxd;
  738. int done = 0;
  739. while (done < budget) {
  740. struct net_device *netdev;
  741. unsigned int pktlen;
  742. dma_addr_t dma_addr;
  743. int mac = 0;
  744. ring = mtk_get_rx_ring(eth);
  745. if (unlikely(!ring))
  746. goto rx_done;
  747. idx = NEXT_RX_DESP_IDX(ring->calc_idx, ring->dma_size);
  748. rxd = &ring->dma[idx];
  749. data = ring->data[idx];
  750. mtk_rx_get_desc(&trxd, rxd);
  751. if (!(trxd.rxd2 & RX_DMA_DONE))
  752. break;
  753. /* find out which mac the packet come from. values start at 1 */
  754. mac = (trxd.rxd4 >> RX_DMA_FPORT_SHIFT) &
  755. RX_DMA_FPORT_MASK;
  756. mac--;
  757. netdev = eth->netdev[mac];
  758. if (unlikely(test_bit(MTK_RESETTING, &eth->state)))
  759. goto release_desc;
  760. /* alloc new buffer */
  761. new_data = napi_alloc_frag(ring->frag_size);
  762. if (unlikely(!new_data)) {
  763. netdev->stats.rx_dropped++;
  764. goto release_desc;
  765. }
  766. dma_addr = dma_map_single(eth->dev,
  767. new_data + NET_SKB_PAD,
  768. ring->buf_size,
  769. DMA_FROM_DEVICE);
  770. if (unlikely(dma_mapping_error(eth->dev, dma_addr))) {
  771. skb_free_frag(new_data);
  772. netdev->stats.rx_dropped++;
  773. goto release_desc;
  774. }
  775. /* receive data */
  776. skb = build_skb(data, ring->frag_size);
  777. if (unlikely(!skb)) {
  778. skb_free_frag(new_data);
  779. netdev->stats.rx_dropped++;
  780. goto release_desc;
  781. }
  782. skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
  783. dma_unmap_single(eth->dev, trxd.rxd1,
  784. ring->buf_size, DMA_FROM_DEVICE);
  785. pktlen = RX_DMA_GET_PLEN0(trxd.rxd2);
  786. skb->dev = netdev;
  787. skb_put(skb, pktlen);
  788. if (trxd.rxd4 & RX_DMA_L4_VALID)
  789. skb->ip_summed = CHECKSUM_UNNECESSARY;
  790. else
  791. skb_checksum_none_assert(skb);
  792. skb->protocol = eth_type_trans(skb, netdev);
  793. if (netdev->features & NETIF_F_HW_VLAN_CTAG_RX &&
  794. RX_DMA_VID(trxd.rxd3))
  795. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
  796. RX_DMA_VID(trxd.rxd3));
  797. napi_gro_receive(napi, skb);
  798. ring->data[idx] = new_data;
  799. rxd->rxd1 = (unsigned int)dma_addr;
  800. release_desc:
  801. rxd->rxd2 = RX_DMA_PLEN0(ring->buf_size);
  802. ring->calc_idx = idx;
  803. done++;
  804. }
  805. rx_done:
  806. if (done) {
  807. /* make sure that all changes to the dma ring are flushed before
  808. * we continue
  809. */
  810. wmb();
  811. mtk_update_rx_cpu_idx(eth);
  812. }
  813. return done;
  814. }
  815. static int mtk_poll_tx(struct mtk_eth *eth, int budget)
  816. {
  817. struct mtk_tx_ring *ring = &eth->tx_ring;
  818. struct mtk_tx_dma *desc;
  819. struct sk_buff *skb;
  820. struct mtk_tx_buf *tx_buf;
  821. unsigned int done[MTK_MAX_DEVS];
  822. unsigned int bytes[MTK_MAX_DEVS];
  823. u32 cpu, dma;
  824. static int condition;
  825. int total = 0, i;
  826. memset(done, 0, sizeof(done));
  827. memset(bytes, 0, sizeof(bytes));
  828. cpu = mtk_r32(eth, MTK_QTX_CRX_PTR);
  829. dma = mtk_r32(eth, MTK_QTX_DRX_PTR);
  830. desc = mtk_qdma_phys_to_virt(ring, cpu);
  831. while ((cpu != dma) && budget) {
  832. u32 next_cpu = desc->txd2;
  833. int mac = 0;
  834. desc = mtk_qdma_phys_to_virt(ring, desc->txd2);
  835. if ((desc->txd3 & TX_DMA_OWNER_CPU) == 0)
  836. break;
  837. tx_buf = mtk_desc_to_tx_buf(ring, desc);
  838. if (tx_buf->flags & MTK_TX_FLAGS_FPORT1)
  839. mac = 1;
  840. skb = tx_buf->skb;
  841. if (!skb) {
  842. condition = 1;
  843. break;
  844. }
  845. if (skb != (struct sk_buff *)MTK_DMA_DUMMY_DESC) {
  846. bytes[mac] += skb->len;
  847. done[mac]++;
  848. budget--;
  849. }
  850. mtk_tx_unmap(eth, tx_buf);
  851. ring->last_free = desc;
  852. atomic_inc(&ring->free_count);
  853. cpu = next_cpu;
  854. }
  855. mtk_w32(eth, cpu, MTK_QTX_CRX_PTR);
  856. for (i = 0; i < MTK_MAC_COUNT; i++) {
  857. if (!eth->netdev[i] || !done[i])
  858. continue;
  859. netdev_completed_queue(eth->netdev[i], done[i], bytes[i]);
  860. total += done[i];
  861. }
  862. if (mtk_queue_stopped(eth) &&
  863. (atomic_read(&ring->free_count) > ring->thresh))
  864. mtk_wake_queue(eth);
  865. return total;
  866. }
  867. static void mtk_handle_status_irq(struct mtk_eth *eth)
  868. {
  869. u32 status2 = mtk_r32(eth, MTK_INT_STATUS2);
  870. if (unlikely(status2 & (MTK_GDM1_AF | MTK_GDM2_AF))) {
  871. mtk_stats_update(eth);
  872. mtk_w32(eth, (MTK_GDM1_AF | MTK_GDM2_AF),
  873. MTK_INT_STATUS2);
  874. }
  875. }
  876. static int mtk_napi_tx(struct napi_struct *napi, int budget)
  877. {
  878. struct mtk_eth *eth = container_of(napi, struct mtk_eth, tx_napi);
  879. u32 status, mask;
  880. int tx_done = 0;
  881. mtk_handle_status_irq(eth);
  882. mtk_w32(eth, MTK_TX_DONE_INT, MTK_QMTK_INT_STATUS);
  883. tx_done = mtk_poll_tx(eth, budget);
  884. if (unlikely(netif_msg_intr(eth))) {
  885. status = mtk_r32(eth, MTK_QMTK_INT_STATUS);
  886. mask = mtk_r32(eth, MTK_QDMA_INT_MASK);
  887. dev_info(eth->dev,
  888. "done tx %d, intr 0x%08x/0x%x\n",
  889. tx_done, status, mask);
  890. }
  891. if (tx_done == budget)
  892. return budget;
  893. status = mtk_r32(eth, MTK_QMTK_INT_STATUS);
  894. if (status & MTK_TX_DONE_INT)
  895. return budget;
  896. napi_complete(napi);
  897. mtk_irq_enable(eth, MTK_QDMA_INT_MASK, MTK_TX_DONE_INT);
  898. return tx_done;
  899. }
  900. static int mtk_napi_rx(struct napi_struct *napi, int budget)
  901. {
  902. struct mtk_eth *eth = container_of(napi, struct mtk_eth, rx_napi);
  903. u32 status, mask;
  904. int rx_done = 0;
  905. int remain_budget = budget;
  906. mtk_handle_status_irq(eth);
  907. poll_again:
  908. mtk_w32(eth, MTK_RX_DONE_INT, MTK_PDMA_INT_STATUS);
  909. rx_done = mtk_poll_rx(napi, remain_budget, eth);
  910. if (unlikely(netif_msg_intr(eth))) {
  911. status = mtk_r32(eth, MTK_PDMA_INT_STATUS);
  912. mask = mtk_r32(eth, MTK_PDMA_INT_MASK);
  913. dev_info(eth->dev,
  914. "done rx %d, intr 0x%08x/0x%x\n",
  915. rx_done, status, mask);
  916. }
  917. if (rx_done == remain_budget)
  918. return budget;
  919. status = mtk_r32(eth, MTK_PDMA_INT_STATUS);
  920. if (status & MTK_RX_DONE_INT) {
  921. remain_budget -= rx_done;
  922. goto poll_again;
  923. }
  924. napi_complete(napi);
  925. mtk_irq_enable(eth, MTK_PDMA_INT_MASK, MTK_RX_DONE_INT);
  926. return rx_done + budget - remain_budget;
  927. }
  928. static int mtk_tx_alloc(struct mtk_eth *eth)
  929. {
  930. struct mtk_tx_ring *ring = &eth->tx_ring;
  931. int i, sz = sizeof(*ring->dma);
  932. ring->buf = kcalloc(MTK_DMA_SIZE, sizeof(*ring->buf),
  933. GFP_KERNEL);
  934. if (!ring->buf)
  935. goto no_tx_mem;
  936. ring->dma = dma_alloc_coherent(eth->dev,
  937. MTK_DMA_SIZE * sz,
  938. &ring->phys,
  939. GFP_ATOMIC | __GFP_ZERO);
  940. if (!ring->dma)
  941. goto no_tx_mem;
  942. memset(ring->dma, 0, MTK_DMA_SIZE * sz);
  943. for (i = 0; i < MTK_DMA_SIZE; i++) {
  944. int next = (i + 1) % MTK_DMA_SIZE;
  945. u32 next_ptr = ring->phys + next * sz;
  946. ring->dma[i].txd2 = next_ptr;
  947. ring->dma[i].txd3 = TX_DMA_LS0 | TX_DMA_OWNER_CPU;
  948. }
  949. atomic_set(&ring->free_count, MTK_DMA_SIZE - 2);
  950. ring->next_free = &ring->dma[0];
  951. ring->last_free = &ring->dma[MTK_DMA_SIZE - 1];
  952. ring->thresh = MAX_SKB_FRAGS;
  953. /* make sure that all changes to the dma ring are flushed before we
  954. * continue
  955. */
  956. wmb();
  957. mtk_w32(eth, ring->phys, MTK_QTX_CTX_PTR);
  958. mtk_w32(eth, ring->phys, MTK_QTX_DTX_PTR);
  959. mtk_w32(eth,
  960. ring->phys + ((MTK_DMA_SIZE - 1) * sz),
  961. MTK_QTX_CRX_PTR);
  962. mtk_w32(eth,
  963. ring->phys + ((MTK_DMA_SIZE - 1) * sz),
  964. MTK_QTX_DRX_PTR);
  965. mtk_w32(eth, (QDMA_RES_THRES << 8) | QDMA_RES_THRES, MTK_QTX_CFG(0));
  966. return 0;
  967. no_tx_mem:
  968. return -ENOMEM;
  969. }
  970. static void mtk_tx_clean(struct mtk_eth *eth)
  971. {
  972. struct mtk_tx_ring *ring = &eth->tx_ring;
  973. int i;
  974. if (ring->buf) {
  975. for (i = 0; i < MTK_DMA_SIZE; i++)
  976. mtk_tx_unmap(eth, &ring->buf[i]);
  977. kfree(ring->buf);
  978. ring->buf = NULL;
  979. }
  980. if (ring->dma) {
  981. dma_free_coherent(eth->dev,
  982. MTK_DMA_SIZE * sizeof(*ring->dma),
  983. ring->dma,
  984. ring->phys);
  985. ring->dma = NULL;
  986. }
  987. }
  988. static int mtk_rx_alloc(struct mtk_eth *eth, int ring_no, int rx_flag)
  989. {
  990. struct mtk_rx_ring *ring = &eth->rx_ring[ring_no];
  991. int rx_data_len, rx_dma_size;
  992. int i;
  993. if (rx_flag == MTK_RX_FLAGS_HWLRO) {
  994. rx_data_len = MTK_MAX_LRO_RX_LENGTH;
  995. rx_dma_size = MTK_HW_LRO_DMA_SIZE;
  996. } else {
  997. rx_data_len = ETH_DATA_LEN;
  998. rx_dma_size = MTK_DMA_SIZE;
  999. }
  1000. ring->frag_size = mtk_max_frag_size(rx_data_len);
  1001. ring->buf_size = mtk_max_buf_size(ring->frag_size);
  1002. ring->data = kcalloc(rx_dma_size, sizeof(*ring->data),
  1003. GFP_KERNEL);
  1004. if (!ring->data)
  1005. return -ENOMEM;
  1006. for (i = 0; i < rx_dma_size; i++) {
  1007. ring->data[i] = netdev_alloc_frag(ring->frag_size);
  1008. if (!ring->data[i])
  1009. return -ENOMEM;
  1010. }
  1011. ring->dma = dma_alloc_coherent(eth->dev,
  1012. rx_dma_size * sizeof(*ring->dma),
  1013. &ring->phys,
  1014. GFP_ATOMIC | __GFP_ZERO);
  1015. if (!ring->dma)
  1016. return -ENOMEM;
  1017. for (i = 0; i < rx_dma_size; i++) {
  1018. dma_addr_t dma_addr = dma_map_single(eth->dev,
  1019. ring->data[i] + NET_SKB_PAD,
  1020. ring->buf_size,
  1021. DMA_FROM_DEVICE);
  1022. if (unlikely(dma_mapping_error(eth->dev, dma_addr)))
  1023. return -ENOMEM;
  1024. ring->dma[i].rxd1 = (unsigned int)dma_addr;
  1025. ring->dma[i].rxd2 = RX_DMA_PLEN0(ring->buf_size);
  1026. }
  1027. ring->dma_size = rx_dma_size;
  1028. ring->calc_idx_update = false;
  1029. ring->calc_idx = rx_dma_size - 1;
  1030. ring->crx_idx_reg = MTK_PRX_CRX_IDX_CFG(ring_no);
  1031. /* make sure that all changes to the dma ring are flushed before we
  1032. * continue
  1033. */
  1034. wmb();
  1035. mtk_w32(eth, ring->phys, MTK_PRX_BASE_PTR_CFG(ring_no));
  1036. mtk_w32(eth, rx_dma_size, MTK_PRX_MAX_CNT_CFG(ring_no));
  1037. mtk_w32(eth, ring->calc_idx, ring->crx_idx_reg);
  1038. mtk_w32(eth, MTK_PST_DRX_IDX_CFG(ring_no), MTK_PDMA_RST_IDX);
  1039. return 0;
  1040. }
  1041. static void mtk_rx_clean(struct mtk_eth *eth, int ring_no)
  1042. {
  1043. struct mtk_rx_ring *ring = &eth->rx_ring[ring_no];
  1044. int i;
  1045. if (ring->data && ring->dma) {
  1046. for (i = 0; i < ring->dma_size; i++) {
  1047. if (!ring->data[i])
  1048. continue;
  1049. if (!ring->dma[i].rxd1)
  1050. continue;
  1051. dma_unmap_single(eth->dev,
  1052. ring->dma[i].rxd1,
  1053. ring->buf_size,
  1054. DMA_FROM_DEVICE);
  1055. skb_free_frag(ring->data[i]);
  1056. }
  1057. kfree(ring->data);
  1058. ring->data = NULL;
  1059. }
  1060. if (ring->dma) {
  1061. dma_free_coherent(eth->dev,
  1062. ring->dma_size * sizeof(*ring->dma),
  1063. ring->dma,
  1064. ring->phys);
  1065. ring->dma = NULL;
  1066. }
  1067. }
  1068. static int mtk_hwlro_rx_init(struct mtk_eth *eth)
  1069. {
  1070. int i;
  1071. u32 ring_ctrl_dw1 = 0, ring_ctrl_dw2 = 0, ring_ctrl_dw3 = 0;
  1072. u32 lro_ctrl_dw0 = 0, lro_ctrl_dw3 = 0;
  1073. /* set LRO rings to auto-learn modes */
  1074. ring_ctrl_dw2 |= MTK_RING_AUTO_LERAN_MODE;
  1075. /* validate LRO ring */
  1076. ring_ctrl_dw2 |= MTK_RING_VLD;
  1077. /* set AGE timer (unit: 20us) */
  1078. ring_ctrl_dw2 |= MTK_RING_AGE_TIME_H;
  1079. ring_ctrl_dw1 |= MTK_RING_AGE_TIME_L;
  1080. /* set max AGG timer (unit: 20us) */
  1081. ring_ctrl_dw2 |= MTK_RING_MAX_AGG_TIME;
  1082. /* set max LRO AGG count */
  1083. ring_ctrl_dw2 |= MTK_RING_MAX_AGG_CNT_L;
  1084. ring_ctrl_dw3 |= MTK_RING_MAX_AGG_CNT_H;
  1085. for (i = 1; i < MTK_MAX_RX_RING_NUM; i++) {
  1086. mtk_w32(eth, ring_ctrl_dw1, MTK_LRO_CTRL_DW1_CFG(i));
  1087. mtk_w32(eth, ring_ctrl_dw2, MTK_LRO_CTRL_DW2_CFG(i));
  1088. mtk_w32(eth, ring_ctrl_dw3, MTK_LRO_CTRL_DW3_CFG(i));
  1089. }
  1090. /* IPv4 checksum update enable */
  1091. lro_ctrl_dw0 |= MTK_L3_CKS_UPD_EN;
  1092. /* switch priority comparison to packet count mode */
  1093. lro_ctrl_dw0 |= MTK_LRO_ALT_PKT_CNT_MODE;
  1094. /* bandwidth threshold setting */
  1095. mtk_w32(eth, MTK_HW_LRO_BW_THRE, MTK_PDMA_LRO_CTRL_DW2);
  1096. /* auto-learn score delta setting */
  1097. mtk_w32(eth, MTK_HW_LRO_REPLACE_DELTA, MTK_PDMA_LRO_ALT_SCORE_DELTA);
  1098. /* set refresh timer for altering flows to 1 sec. (unit: 20us) */
  1099. mtk_w32(eth, (MTK_HW_LRO_TIMER_UNIT << 16) | MTK_HW_LRO_REFRESH_TIME,
  1100. MTK_PDMA_LRO_ALT_REFRESH_TIMER);
  1101. /* set HW LRO mode & the max aggregation count for rx packets */
  1102. lro_ctrl_dw3 |= MTK_ADMA_MODE | (MTK_HW_LRO_MAX_AGG_CNT & 0xff);
  1103. /* the minimal remaining room of SDL0 in RXD for lro aggregation */
  1104. lro_ctrl_dw3 |= MTK_LRO_MIN_RXD_SDL;
  1105. /* enable HW LRO */
  1106. lro_ctrl_dw0 |= MTK_LRO_EN;
  1107. mtk_w32(eth, lro_ctrl_dw3, MTK_PDMA_LRO_CTRL_DW3);
  1108. mtk_w32(eth, lro_ctrl_dw0, MTK_PDMA_LRO_CTRL_DW0);
  1109. return 0;
  1110. }
  1111. static void mtk_hwlro_rx_uninit(struct mtk_eth *eth)
  1112. {
  1113. int i;
  1114. u32 val;
  1115. /* relinquish lro rings, flush aggregated packets */
  1116. mtk_w32(eth, MTK_LRO_RING_RELINQUISH_REQ, MTK_PDMA_LRO_CTRL_DW0);
  1117. /* wait for relinquishments done */
  1118. for (i = 0; i < 10; i++) {
  1119. val = mtk_r32(eth, MTK_PDMA_LRO_CTRL_DW0);
  1120. if (val & MTK_LRO_RING_RELINQUISH_DONE) {
  1121. msleep(20);
  1122. continue;
  1123. }
  1124. break;
  1125. }
  1126. /* invalidate lro rings */
  1127. for (i = 1; i < MTK_MAX_RX_RING_NUM; i++)
  1128. mtk_w32(eth, 0, MTK_LRO_CTRL_DW2_CFG(i));
  1129. /* disable HW LRO */
  1130. mtk_w32(eth, 0, MTK_PDMA_LRO_CTRL_DW0);
  1131. }
  1132. static void mtk_hwlro_val_ipaddr(struct mtk_eth *eth, int idx, __be32 ip)
  1133. {
  1134. u32 reg_val;
  1135. reg_val = mtk_r32(eth, MTK_LRO_CTRL_DW2_CFG(idx));
  1136. /* invalidate the IP setting */
  1137. mtk_w32(eth, (reg_val & ~MTK_RING_MYIP_VLD), MTK_LRO_CTRL_DW2_CFG(idx));
  1138. mtk_w32(eth, ip, MTK_LRO_DIP_DW0_CFG(idx));
  1139. /* validate the IP setting */
  1140. mtk_w32(eth, (reg_val | MTK_RING_MYIP_VLD), MTK_LRO_CTRL_DW2_CFG(idx));
  1141. }
  1142. static void mtk_hwlro_inval_ipaddr(struct mtk_eth *eth, int idx)
  1143. {
  1144. u32 reg_val;
  1145. reg_val = mtk_r32(eth, MTK_LRO_CTRL_DW2_CFG(idx));
  1146. /* invalidate the IP setting */
  1147. mtk_w32(eth, (reg_val & ~MTK_RING_MYIP_VLD), MTK_LRO_CTRL_DW2_CFG(idx));
  1148. mtk_w32(eth, 0, MTK_LRO_DIP_DW0_CFG(idx));
  1149. }
  1150. static int mtk_hwlro_get_ip_cnt(struct mtk_mac *mac)
  1151. {
  1152. int cnt = 0;
  1153. int i;
  1154. for (i = 0; i < MTK_MAX_LRO_IP_CNT; i++) {
  1155. if (mac->hwlro_ip[i])
  1156. cnt++;
  1157. }
  1158. return cnt;
  1159. }
  1160. static int mtk_hwlro_add_ipaddr(struct net_device *dev,
  1161. struct ethtool_rxnfc *cmd)
  1162. {
  1163. struct ethtool_rx_flow_spec *fsp =
  1164. (struct ethtool_rx_flow_spec *)&cmd->fs;
  1165. struct mtk_mac *mac = netdev_priv(dev);
  1166. struct mtk_eth *eth = mac->hw;
  1167. int hwlro_idx;
  1168. if ((fsp->flow_type != TCP_V4_FLOW) ||
  1169. (!fsp->h_u.tcp_ip4_spec.ip4dst) ||
  1170. (fsp->location > 1))
  1171. return -EINVAL;
  1172. mac->hwlro_ip[fsp->location] = htonl(fsp->h_u.tcp_ip4_spec.ip4dst);
  1173. hwlro_idx = (mac->id * MTK_MAX_LRO_IP_CNT) + fsp->location;
  1174. mac->hwlro_ip_cnt = mtk_hwlro_get_ip_cnt(mac);
  1175. mtk_hwlro_val_ipaddr(eth, hwlro_idx, mac->hwlro_ip[fsp->location]);
  1176. return 0;
  1177. }
  1178. static int mtk_hwlro_del_ipaddr(struct net_device *dev,
  1179. struct ethtool_rxnfc *cmd)
  1180. {
  1181. struct ethtool_rx_flow_spec *fsp =
  1182. (struct ethtool_rx_flow_spec *)&cmd->fs;
  1183. struct mtk_mac *mac = netdev_priv(dev);
  1184. struct mtk_eth *eth = mac->hw;
  1185. int hwlro_idx;
  1186. if (fsp->location > 1)
  1187. return -EINVAL;
  1188. mac->hwlro_ip[fsp->location] = 0;
  1189. hwlro_idx = (mac->id * MTK_MAX_LRO_IP_CNT) + fsp->location;
  1190. mac->hwlro_ip_cnt = mtk_hwlro_get_ip_cnt(mac);
  1191. mtk_hwlro_inval_ipaddr(eth, hwlro_idx);
  1192. return 0;
  1193. }
  1194. static void mtk_hwlro_netdev_disable(struct net_device *dev)
  1195. {
  1196. struct mtk_mac *mac = netdev_priv(dev);
  1197. struct mtk_eth *eth = mac->hw;
  1198. int i, hwlro_idx;
  1199. for (i = 0; i < MTK_MAX_LRO_IP_CNT; i++) {
  1200. mac->hwlro_ip[i] = 0;
  1201. hwlro_idx = (mac->id * MTK_MAX_LRO_IP_CNT) + i;
  1202. mtk_hwlro_inval_ipaddr(eth, hwlro_idx);
  1203. }
  1204. mac->hwlro_ip_cnt = 0;
  1205. }
  1206. static int mtk_hwlro_get_fdir_entry(struct net_device *dev,
  1207. struct ethtool_rxnfc *cmd)
  1208. {
  1209. struct mtk_mac *mac = netdev_priv(dev);
  1210. struct ethtool_rx_flow_spec *fsp =
  1211. (struct ethtool_rx_flow_spec *)&cmd->fs;
  1212. /* only tcp dst ipv4 is meaningful, others are meaningless */
  1213. fsp->flow_type = TCP_V4_FLOW;
  1214. fsp->h_u.tcp_ip4_spec.ip4dst = ntohl(mac->hwlro_ip[fsp->location]);
  1215. fsp->m_u.tcp_ip4_spec.ip4dst = 0;
  1216. fsp->h_u.tcp_ip4_spec.ip4src = 0;
  1217. fsp->m_u.tcp_ip4_spec.ip4src = 0xffffffff;
  1218. fsp->h_u.tcp_ip4_spec.psrc = 0;
  1219. fsp->m_u.tcp_ip4_spec.psrc = 0xffff;
  1220. fsp->h_u.tcp_ip4_spec.pdst = 0;
  1221. fsp->m_u.tcp_ip4_spec.pdst = 0xffff;
  1222. fsp->h_u.tcp_ip4_spec.tos = 0;
  1223. fsp->m_u.tcp_ip4_spec.tos = 0xff;
  1224. return 0;
  1225. }
  1226. static int mtk_hwlro_get_fdir_all(struct net_device *dev,
  1227. struct ethtool_rxnfc *cmd,
  1228. u32 *rule_locs)
  1229. {
  1230. struct mtk_mac *mac = netdev_priv(dev);
  1231. int cnt = 0;
  1232. int i;
  1233. for (i = 0; i < MTK_MAX_LRO_IP_CNT; i++) {
  1234. if (mac->hwlro_ip[i]) {
  1235. rule_locs[cnt] = i;
  1236. cnt++;
  1237. }
  1238. }
  1239. cmd->rule_cnt = cnt;
  1240. return 0;
  1241. }
  1242. static netdev_features_t mtk_fix_features(struct net_device *dev,
  1243. netdev_features_t features)
  1244. {
  1245. if (!(features & NETIF_F_LRO)) {
  1246. struct mtk_mac *mac = netdev_priv(dev);
  1247. int ip_cnt = mtk_hwlro_get_ip_cnt(mac);
  1248. if (ip_cnt) {
  1249. netdev_info(dev, "RX flow is programmed, LRO should keep on\n");
  1250. features |= NETIF_F_LRO;
  1251. }
  1252. }
  1253. return features;
  1254. }
  1255. static int mtk_set_features(struct net_device *dev, netdev_features_t features)
  1256. {
  1257. int err = 0;
  1258. if (!((dev->features ^ features) & NETIF_F_LRO))
  1259. return 0;
  1260. if (!(features & NETIF_F_LRO))
  1261. mtk_hwlro_netdev_disable(dev);
  1262. return err;
  1263. }
  1264. /* wait for DMA to finish whatever it is doing before we start using it again */
  1265. static int mtk_dma_busy_wait(struct mtk_eth *eth)
  1266. {
  1267. unsigned long t_start = jiffies;
  1268. while (1) {
  1269. if (!(mtk_r32(eth, MTK_QDMA_GLO_CFG) &
  1270. (MTK_RX_DMA_BUSY | MTK_TX_DMA_BUSY)))
  1271. return 0;
  1272. if (time_after(jiffies, t_start + MTK_DMA_BUSY_TIMEOUT))
  1273. break;
  1274. }
  1275. dev_err(eth->dev, "DMA init timeout\n");
  1276. return -1;
  1277. }
  1278. static int mtk_dma_init(struct mtk_eth *eth)
  1279. {
  1280. int err;
  1281. u32 i;
  1282. if (mtk_dma_busy_wait(eth))
  1283. return -EBUSY;
  1284. /* QDMA needs scratch memory for internal reordering of the
  1285. * descriptors
  1286. */
  1287. err = mtk_init_fq_dma(eth);
  1288. if (err)
  1289. return err;
  1290. err = mtk_tx_alloc(eth);
  1291. if (err)
  1292. return err;
  1293. err = mtk_rx_alloc(eth, 0, MTK_RX_FLAGS_NORMAL);
  1294. if (err)
  1295. return err;
  1296. if (eth->hwlro) {
  1297. for (i = 1; i < MTK_MAX_RX_RING_NUM; i++) {
  1298. err = mtk_rx_alloc(eth, i, MTK_RX_FLAGS_HWLRO);
  1299. if (err)
  1300. return err;
  1301. }
  1302. err = mtk_hwlro_rx_init(eth);
  1303. if (err)
  1304. return err;
  1305. }
  1306. /* Enable random early drop and set drop threshold automatically */
  1307. mtk_w32(eth, FC_THRES_DROP_MODE | FC_THRES_DROP_EN | FC_THRES_MIN,
  1308. MTK_QDMA_FC_THRES);
  1309. mtk_w32(eth, 0x0, MTK_QDMA_HRED2);
  1310. return 0;
  1311. }
  1312. static void mtk_dma_free(struct mtk_eth *eth)
  1313. {
  1314. int i;
  1315. for (i = 0; i < MTK_MAC_COUNT; i++)
  1316. if (eth->netdev[i])
  1317. netdev_reset_queue(eth->netdev[i]);
  1318. if (eth->scratch_ring) {
  1319. dma_free_coherent(eth->dev,
  1320. MTK_DMA_SIZE * sizeof(struct mtk_tx_dma),
  1321. eth->scratch_ring,
  1322. eth->phy_scratch_ring);
  1323. eth->scratch_ring = NULL;
  1324. eth->phy_scratch_ring = 0;
  1325. }
  1326. mtk_tx_clean(eth);
  1327. mtk_rx_clean(eth, 0);
  1328. if (eth->hwlro) {
  1329. mtk_hwlro_rx_uninit(eth);
  1330. for (i = 1; i < MTK_MAX_RX_RING_NUM; i++)
  1331. mtk_rx_clean(eth, i);
  1332. }
  1333. kfree(eth->scratch_head);
  1334. }
  1335. static void mtk_tx_timeout(struct net_device *dev)
  1336. {
  1337. struct mtk_mac *mac = netdev_priv(dev);
  1338. struct mtk_eth *eth = mac->hw;
  1339. eth->netdev[mac->id]->stats.tx_errors++;
  1340. netif_err(eth, tx_err, dev,
  1341. "transmit timed out\n");
  1342. schedule_work(&eth->pending_work);
  1343. }
  1344. static irqreturn_t mtk_handle_irq_rx(int irq, void *_eth)
  1345. {
  1346. struct mtk_eth *eth = _eth;
  1347. if (likely(napi_schedule_prep(&eth->rx_napi))) {
  1348. __napi_schedule(&eth->rx_napi);
  1349. mtk_irq_disable(eth, MTK_PDMA_INT_MASK, MTK_RX_DONE_INT);
  1350. }
  1351. return IRQ_HANDLED;
  1352. }
  1353. static irqreturn_t mtk_handle_irq_tx(int irq, void *_eth)
  1354. {
  1355. struct mtk_eth *eth = _eth;
  1356. if (likely(napi_schedule_prep(&eth->tx_napi))) {
  1357. __napi_schedule(&eth->tx_napi);
  1358. mtk_irq_disable(eth, MTK_QDMA_INT_MASK, MTK_TX_DONE_INT);
  1359. }
  1360. return IRQ_HANDLED;
  1361. }
  1362. #ifdef CONFIG_NET_POLL_CONTROLLER
  1363. static void mtk_poll_controller(struct net_device *dev)
  1364. {
  1365. struct mtk_mac *mac = netdev_priv(dev);
  1366. struct mtk_eth *eth = mac->hw;
  1367. mtk_irq_disable(eth, MTK_QDMA_INT_MASK, MTK_TX_DONE_INT);
  1368. mtk_irq_disable(eth, MTK_PDMA_INT_MASK, MTK_RX_DONE_INT);
  1369. mtk_handle_irq_rx(eth->irq[2], dev);
  1370. mtk_irq_enable(eth, MTK_QDMA_INT_MASK, MTK_TX_DONE_INT);
  1371. mtk_irq_enable(eth, MTK_PDMA_INT_MASK, MTK_RX_DONE_INT);
  1372. }
  1373. #endif
  1374. static int mtk_start_dma(struct mtk_eth *eth)
  1375. {
  1376. int err;
  1377. err = mtk_dma_init(eth);
  1378. if (err) {
  1379. mtk_dma_free(eth);
  1380. return err;
  1381. }
  1382. mtk_w32(eth,
  1383. MTK_TX_WB_DDONE | MTK_TX_DMA_EN |
  1384. MTK_DMA_SIZE_16DWORDS | MTK_NDP_CO_PRO,
  1385. MTK_QDMA_GLO_CFG);
  1386. mtk_w32(eth,
  1387. MTK_RX_DMA_EN | MTK_RX_2B_OFFSET |
  1388. MTK_RX_BT_32DWORDS | MTK_MULTI_EN,
  1389. MTK_PDMA_GLO_CFG);
  1390. return 0;
  1391. }
  1392. static int mtk_open(struct net_device *dev)
  1393. {
  1394. struct mtk_mac *mac = netdev_priv(dev);
  1395. struct mtk_eth *eth = mac->hw;
  1396. /* we run 2 netdevs on the same dma ring so we only bring it up once */
  1397. if (!atomic_read(&eth->dma_refcnt)) {
  1398. int err = mtk_start_dma(eth);
  1399. if (err)
  1400. return err;
  1401. napi_enable(&eth->tx_napi);
  1402. napi_enable(&eth->rx_napi);
  1403. mtk_irq_enable(eth, MTK_QDMA_INT_MASK, MTK_TX_DONE_INT);
  1404. mtk_irq_enable(eth, MTK_PDMA_INT_MASK, MTK_RX_DONE_INT);
  1405. }
  1406. atomic_inc(&eth->dma_refcnt);
  1407. phy_start(dev->phydev);
  1408. netif_start_queue(dev);
  1409. return 0;
  1410. }
  1411. static void mtk_stop_dma(struct mtk_eth *eth, u32 glo_cfg)
  1412. {
  1413. u32 val;
  1414. int i;
  1415. /* stop the dma engine */
  1416. spin_lock_bh(&eth->page_lock);
  1417. val = mtk_r32(eth, glo_cfg);
  1418. mtk_w32(eth, val & ~(MTK_TX_WB_DDONE | MTK_RX_DMA_EN | MTK_TX_DMA_EN),
  1419. glo_cfg);
  1420. spin_unlock_bh(&eth->page_lock);
  1421. /* wait for dma stop */
  1422. for (i = 0; i < 10; i++) {
  1423. val = mtk_r32(eth, glo_cfg);
  1424. if (val & (MTK_TX_DMA_BUSY | MTK_RX_DMA_BUSY)) {
  1425. msleep(20);
  1426. continue;
  1427. }
  1428. break;
  1429. }
  1430. }
  1431. static int mtk_stop(struct net_device *dev)
  1432. {
  1433. struct mtk_mac *mac = netdev_priv(dev);
  1434. struct mtk_eth *eth = mac->hw;
  1435. netif_tx_disable(dev);
  1436. phy_stop(dev->phydev);
  1437. /* only shutdown DMA if this is the last user */
  1438. if (!atomic_dec_and_test(&eth->dma_refcnt))
  1439. return 0;
  1440. mtk_irq_disable(eth, MTK_QDMA_INT_MASK, MTK_TX_DONE_INT);
  1441. mtk_irq_disable(eth, MTK_PDMA_INT_MASK, MTK_RX_DONE_INT);
  1442. napi_disable(&eth->tx_napi);
  1443. napi_disable(&eth->rx_napi);
  1444. mtk_stop_dma(eth, MTK_QDMA_GLO_CFG);
  1445. mtk_stop_dma(eth, MTK_PDMA_GLO_CFG);
  1446. mtk_dma_free(eth);
  1447. return 0;
  1448. }
  1449. static void ethsys_reset(struct mtk_eth *eth, u32 reset_bits)
  1450. {
  1451. regmap_update_bits(eth->ethsys, ETHSYS_RSTCTRL,
  1452. reset_bits,
  1453. reset_bits);
  1454. usleep_range(1000, 1100);
  1455. regmap_update_bits(eth->ethsys, ETHSYS_RSTCTRL,
  1456. reset_bits,
  1457. ~reset_bits);
  1458. mdelay(10);
  1459. }
  1460. static int mtk_hw_init(struct mtk_eth *eth)
  1461. {
  1462. int i, val;
  1463. if (test_and_set_bit(MTK_HW_INIT, &eth->state))
  1464. return 0;
  1465. pm_runtime_enable(eth->dev);
  1466. pm_runtime_get_sync(eth->dev);
  1467. clk_prepare_enable(eth->clks[MTK_CLK_ETHIF]);
  1468. clk_prepare_enable(eth->clks[MTK_CLK_ESW]);
  1469. clk_prepare_enable(eth->clks[MTK_CLK_GP1]);
  1470. clk_prepare_enable(eth->clks[MTK_CLK_GP2]);
  1471. ethsys_reset(eth, RSTCTRL_FE);
  1472. ethsys_reset(eth, RSTCTRL_PPE);
  1473. regmap_read(eth->ethsys, ETHSYS_SYSCFG0, &val);
  1474. for (i = 0; i < MTK_MAC_COUNT; i++) {
  1475. if (!eth->mac[i])
  1476. continue;
  1477. val &= ~SYSCFG0_GE_MODE(SYSCFG0_GE_MASK, eth->mac[i]->id);
  1478. val |= SYSCFG0_GE_MODE(eth->mac[i]->ge_mode, eth->mac[i]->id);
  1479. }
  1480. regmap_write(eth->ethsys, ETHSYS_SYSCFG0, val);
  1481. /* Set GE2 driving and slew rate */
  1482. regmap_write(eth->pctl, GPIO_DRV_SEL10, 0xa00);
  1483. /* set GE2 TDSEL */
  1484. regmap_write(eth->pctl, GPIO_OD33_CTRL8, 0x5);
  1485. /* set GE2 TUNE */
  1486. regmap_write(eth->pctl, GPIO_BIAS_CTRL, 0x0);
  1487. /* GE1, Force 1000M/FD, FC ON */
  1488. mtk_w32(eth, MAC_MCR_FIXED_LINK, MTK_MAC_MCR(0));
  1489. /* GE2, Force 1000M/FD, FC ON */
  1490. mtk_w32(eth, MAC_MCR_FIXED_LINK, MTK_MAC_MCR(1));
  1491. /* Indicates CDM to parse the MTK special tag from CPU
  1492. * which also is working out for untag packets.
  1493. */
  1494. val = mtk_r32(eth, MTK_CDMQ_IG_CTRL);
  1495. mtk_w32(eth, val | MTK_CDMQ_STAG_EN, MTK_CDMQ_IG_CTRL);
  1496. /* Enable RX VLan Offloading */
  1497. mtk_w32(eth, 1, MTK_CDMP_EG_CTRL);
  1498. /* disable delay and normal interrupt */
  1499. mtk_w32(eth, 0, MTK_QDMA_DELAY_INT);
  1500. mtk_w32(eth, 0, MTK_PDMA_DELAY_INT);
  1501. mtk_irq_disable(eth, MTK_QDMA_INT_MASK, ~0);
  1502. mtk_irq_disable(eth, MTK_PDMA_INT_MASK, ~0);
  1503. mtk_w32(eth, RST_GL_PSE, MTK_RST_GL);
  1504. mtk_w32(eth, 0, MTK_RST_GL);
  1505. /* FE int grouping */
  1506. mtk_w32(eth, MTK_TX_DONE_INT, MTK_PDMA_INT_GRP1);
  1507. mtk_w32(eth, MTK_RX_DONE_INT, MTK_PDMA_INT_GRP2);
  1508. mtk_w32(eth, MTK_TX_DONE_INT, MTK_QDMA_INT_GRP1);
  1509. mtk_w32(eth, MTK_RX_DONE_INT, MTK_QDMA_INT_GRP2);
  1510. mtk_w32(eth, 0x21021000, MTK_FE_INT_GRP);
  1511. for (i = 0; i < 2; i++) {
  1512. u32 val = mtk_r32(eth, MTK_GDMA_FWD_CFG(i));
  1513. /* setup the forward port to send frame to PDMA */
  1514. val &= ~0xffff;
  1515. /* Enable RX checksum */
  1516. val |= MTK_GDMA_ICS_EN | MTK_GDMA_TCS_EN | MTK_GDMA_UCS_EN;
  1517. /* setup the mac dma */
  1518. mtk_w32(eth, val, MTK_GDMA_FWD_CFG(i));
  1519. }
  1520. return 0;
  1521. }
  1522. static int mtk_hw_deinit(struct mtk_eth *eth)
  1523. {
  1524. if (!test_and_clear_bit(MTK_HW_INIT, &eth->state))
  1525. return 0;
  1526. clk_disable_unprepare(eth->clks[MTK_CLK_GP2]);
  1527. clk_disable_unprepare(eth->clks[MTK_CLK_GP1]);
  1528. clk_disable_unprepare(eth->clks[MTK_CLK_ESW]);
  1529. clk_disable_unprepare(eth->clks[MTK_CLK_ETHIF]);
  1530. pm_runtime_put_sync(eth->dev);
  1531. pm_runtime_disable(eth->dev);
  1532. return 0;
  1533. }
  1534. static int __init mtk_init(struct net_device *dev)
  1535. {
  1536. struct mtk_mac *mac = netdev_priv(dev);
  1537. struct mtk_eth *eth = mac->hw;
  1538. const char *mac_addr;
  1539. mac_addr = of_get_mac_address(mac->of_node);
  1540. if (mac_addr)
  1541. ether_addr_copy(dev->dev_addr, mac_addr);
  1542. /* If the mac address is invalid, use random mac address */
  1543. if (!is_valid_ether_addr(dev->dev_addr)) {
  1544. eth_hw_addr_random(dev);
  1545. dev_err(eth->dev, "generated random MAC address %pM\n",
  1546. dev->dev_addr);
  1547. }
  1548. return mtk_phy_connect(dev);
  1549. }
  1550. static void mtk_uninit(struct net_device *dev)
  1551. {
  1552. struct mtk_mac *mac = netdev_priv(dev);
  1553. struct mtk_eth *eth = mac->hw;
  1554. phy_disconnect(dev->phydev);
  1555. if (of_phy_is_fixed_link(mac->of_node))
  1556. of_phy_deregister_fixed_link(mac->of_node);
  1557. mtk_irq_disable(eth, MTK_QDMA_INT_MASK, ~0);
  1558. mtk_irq_disable(eth, MTK_PDMA_INT_MASK, ~0);
  1559. }
  1560. static int mtk_do_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  1561. {
  1562. switch (cmd) {
  1563. case SIOCGMIIPHY:
  1564. case SIOCGMIIREG:
  1565. case SIOCSMIIREG:
  1566. return phy_mii_ioctl(dev->phydev, ifr, cmd);
  1567. default:
  1568. break;
  1569. }
  1570. return -EOPNOTSUPP;
  1571. }
  1572. static void mtk_pending_work(struct work_struct *work)
  1573. {
  1574. struct mtk_eth *eth = container_of(work, struct mtk_eth, pending_work);
  1575. int err, i;
  1576. unsigned long restart = 0;
  1577. rtnl_lock();
  1578. dev_dbg(eth->dev, "[%s][%d] reset\n", __func__, __LINE__);
  1579. while (test_and_set_bit_lock(MTK_RESETTING, &eth->state))
  1580. cpu_relax();
  1581. dev_dbg(eth->dev, "[%s][%d] mtk_stop starts\n", __func__, __LINE__);
  1582. /* stop all devices to make sure that dma is properly shut down */
  1583. for (i = 0; i < MTK_MAC_COUNT; i++) {
  1584. if (!eth->netdev[i])
  1585. continue;
  1586. mtk_stop(eth->netdev[i]);
  1587. __set_bit(i, &restart);
  1588. }
  1589. dev_dbg(eth->dev, "[%s][%d] mtk_stop ends\n", __func__, __LINE__);
  1590. /* restart underlying hardware such as power, clock, pin mux
  1591. * and the connected phy
  1592. */
  1593. mtk_hw_deinit(eth);
  1594. if (eth->dev->pins)
  1595. pinctrl_select_state(eth->dev->pins->p,
  1596. eth->dev->pins->default_state);
  1597. mtk_hw_init(eth);
  1598. for (i = 0; i < MTK_MAC_COUNT; i++) {
  1599. if (!eth->mac[i] ||
  1600. of_phy_is_fixed_link(eth->mac[i]->of_node))
  1601. continue;
  1602. err = phy_init_hw(eth->netdev[i]->phydev);
  1603. if (err)
  1604. dev_err(eth->dev, "%s: PHY init failed.\n",
  1605. eth->netdev[i]->name);
  1606. }
  1607. /* restart DMA and enable IRQs */
  1608. for (i = 0; i < MTK_MAC_COUNT; i++) {
  1609. if (!test_bit(i, &restart))
  1610. continue;
  1611. err = mtk_open(eth->netdev[i]);
  1612. if (err) {
  1613. netif_alert(eth, ifup, eth->netdev[i],
  1614. "Driver up/down cycle failed, closing device.\n");
  1615. dev_close(eth->netdev[i]);
  1616. }
  1617. }
  1618. dev_dbg(eth->dev, "[%s][%d] reset done\n", __func__, __LINE__);
  1619. clear_bit_unlock(MTK_RESETTING, &eth->state);
  1620. rtnl_unlock();
  1621. }
  1622. static int mtk_free_dev(struct mtk_eth *eth)
  1623. {
  1624. int i;
  1625. for (i = 0; i < MTK_MAC_COUNT; i++) {
  1626. if (!eth->netdev[i])
  1627. continue;
  1628. free_netdev(eth->netdev[i]);
  1629. }
  1630. return 0;
  1631. }
  1632. static int mtk_unreg_dev(struct mtk_eth *eth)
  1633. {
  1634. int i;
  1635. for (i = 0; i < MTK_MAC_COUNT; i++) {
  1636. if (!eth->netdev[i])
  1637. continue;
  1638. unregister_netdev(eth->netdev[i]);
  1639. }
  1640. return 0;
  1641. }
  1642. static int mtk_cleanup(struct mtk_eth *eth)
  1643. {
  1644. mtk_unreg_dev(eth);
  1645. mtk_free_dev(eth);
  1646. cancel_work_sync(&eth->pending_work);
  1647. return 0;
  1648. }
  1649. static int mtk_get_link_ksettings(struct net_device *ndev,
  1650. struct ethtool_link_ksettings *cmd)
  1651. {
  1652. struct mtk_mac *mac = netdev_priv(ndev);
  1653. if (unlikely(test_bit(MTK_RESETTING, &mac->hw->state)))
  1654. return -EBUSY;
  1655. return phy_ethtool_ksettings_get(ndev->phydev, cmd);
  1656. }
  1657. static int mtk_set_link_ksettings(struct net_device *ndev,
  1658. const struct ethtool_link_ksettings *cmd)
  1659. {
  1660. struct mtk_mac *mac = netdev_priv(ndev);
  1661. if (unlikely(test_bit(MTK_RESETTING, &mac->hw->state)))
  1662. return -EBUSY;
  1663. return phy_ethtool_ksettings_set(ndev->phydev, cmd);
  1664. }
  1665. static void mtk_get_drvinfo(struct net_device *dev,
  1666. struct ethtool_drvinfo *info)
  1667. {
  1668. struct mtk_mac *mac = netdev_priv(dev);
  1669. strlcpy(info->driver, mac->hw->dev->driver->name, sizeof(info->driver));
  1670. strlcpy(info->bus_info, dev_name(mac->hw->dev), sizeof(info->bus_info));
  1671. info->n_stats = ARRAY_SIZE(mtk_ethtool_stats);
  1672. }
  1673. static u32 mtk_get_msglevel(struct net_device *dev)
  1674. {
  1675. struct mtk_mac *mac = netdev_priv(dev);
  1676. return mac->hw->msg_enable;
  1677. }
  1678. static void mtk_set_msglevel(struct net_device *dev, u32 value)
  1679. {
  1680. struct mtk_mac *mac = netdev_priv(dev);
  1681. mac->hw->msg_enable = value;
  1682. }
  1683. static int mtk_nway_reset(struct net_device *dev)
  1684. {
  1685. struct mtk_mac *mac = netdev_priv(dev);
  1686. if (unlikely(test_bit(MTK_RESETTING, &mac->hw->state)))
  1687. return -EBUSY;
  1688. return genphy_restart_aneg(dev->phydev);
  1689. }
  1690. static u32 mtk_get_link(struct net_device *dev)
  1691. {
  1692. struct mtk_mac *mac = netdev_priv(dev);
  1693. int err;
  1694. if (unlikely(test_bit(MTK_RESETTING, &mac->hw->state)))
  1695. return -EBUSY;
  1696. err = genphy_update_link(dev->phydev);
  1697. if (err)
  1698. return ethtool_op_get_link(dev);
  1699. return dev->phydev->link;
  1700. }
  1701. static void mtk_get_strings(struct net_device *dev, u32 stringset, u8 *data)
  1702. {
  1703. int i;
  1704. switch (stringset) {
  1705. case ETH_SS_STATS:
  1706. for (i = 0; i < ARRAY_SIZE(mtk_ethtool_stats); i++) {
  1707. memcpy(data, mtk_ethtool_stats[i].str, ETH_GSTRING_LEN);
  1708. data += ETH_GSTRING_LEN;
  1709. }
  1710. break;
  1711. }
  1712. }
  1713. static int mtk_get_sset_count(struct net_device *dev, int sset)
  1714. {
  1715. switch (sset) {
  1716. case ETH_SS_STATS:
  1717. return ARRAY_SIZE(mtk_ethtool_stats);
  1718. default:
  1719. return -EOPNOTSUPP;
  1720. }
  1721. }
  1722. static void mtk_get_ethtool_stats(struct net_device *dev,
  1723. struct ethtool_stats *stats, u64 *data)
  1724. {
  1725. struct mtk_mac *mac = netdev_priv(dev);
  1726. struct mtk_hw_stats *hwstats = mac->hw_stats;
  1727. u64 *data_src, *data_dst;
  1728. unsigned int start;
  1729. int i;
  1730. if (unlikely(test_bit(MTK_RESETTING, &mac->hw->state)))
  1731. return;
  1732. if (netif_running(dev) && netif_device_present(dev)) {
  1733. if (spin_trylock(&hwstats->stats_lock)) {
  1734. mtk_stats_update_mac(mac);
  1735. spin_unlock(&hwstats->stats_lock);
  1736. }
  1737. }
  1738. data_src = (u64 *)hwstats;
  1739. do {
  1740. data_dst = data;
  1741. start = u64_stats_fetch_begin_irq(&hwstats->syncp);
  1742. for (i = 0; i < ARRAY_SIZE(mtk_ethtool_stats); i++)
  1743. *data_dst++ = *(data_src + mtk_ethtool_stats[i].offset);
  1744. } while (u64_stats_fetch_retry_irq(&hwstats->syncp, start));
  1745. }
  1746. static int mtk_get_rxnfc(struct net_device *dev, struct ethtool_rxnfc *cmd,
  1747. u32 *rule_locs)
  1748. {
  1749. int ret = -EOPNOTSUPP;
  1750. switch (cmd->cmd) {
  1751. case ETHTOOL_GRXRINGS:
  1752. if (dev->features & NETIF_F_LRO) {
  1753. cmd->data = MTK_MAX_RX_RING_NUM;
  1754. ret = 0;
  1755. }
  1756. break;
  1757. case ETHTOOL_GRXCLSRLCNT:
  1758. if (dev->features & NETIF_F_LRO) {
  1759. struct mtk_mac *mac = netdev_priv(dev);
  1760. cmd->rule_cnt = mac->hwlro_ip_cnt;
  1761. ret = 0;
  1762. }
  1763. break;
  1764. case ETHTOOL_GRXCLSRULE:
  1765. if (dev->features & NETIF_F_LRO)
  1766. ret = mtk_hwlro_get_fdir_entry(dev, cmd);
  1767. break;
  1768. case ETHTOOL_GRXCLSRLALL:
  1769. if (dev->features & NETIF_F_LRO)
  1770. ret = mtk_hwlro_get_fdir_all(dev, cmd,
  1771. rule_locs);
  1772. break;
  1773. default:
  1774. break;
  1775. }
  1776. return ret;
  1777. }
  1778. static int mtk_set_rxnfc(struct net_device *dev, struct ethtool_rxnfc *cmd)
  1779. {
  1780. int ret = -EOPNOTSUPP;
  1781. switch (cmd->cmd) {
  1782. case ETHTOOL_SRXCLSRLINS:
  1783. if (dev->features & NETIF_F_LRO)
  1784. ret = mtk_hwlro_add_ipaddr(dev, cmd);
  1785. break;
  1786. case ETHTOOL_SRXCLSRLDEL:
  1787. if (dev->features & NETIF_F_LRO)
  1788. ret = mtk_hwlro_del_ipaddr(dev, cmd);
  1789. break;
  1790. default:
  1791. break;
  1792. }
  1793. return ret;
  1794. }
  1795. static const struct ethtool_ops mtk_ethtool_ops = {
  1796. .get_link_ksettings = mtk_get_link_ksettings,
  1797. .set_link_ksettings = mtk_set_link_ksettings,
  1798. .get_drvinfo = mtk_get_drvinfo,
  1799. .get_msglevel = mtk_get_msglevel,
  1800. .set_msglevel = mtk_set_msglevel,
  1801. .nway_reset = mtk_nway_reset,
  1802. .get_link = mtk_get_link,
  1803. .get_strings = mtk_get_strings,
  1804. .get_sset_count = mtk_get_sset_count,
  1805. .get_ethtool_stats = mtk_get_ethtool_stats,
  1806. .get_rxnfc = mtk_get_rxnfc,
  1807. .set_rxnfc = mtk_set_rxnfc,
  1808. };
  1809. static const struct net_device_ops mtk_netdev_ops = {
  1810. .ndo_init = mtk_init,
  1811. .ndo_uninit = mtk_uninit,
  1812. .ndo_open = mtk_open,
  1813. .ndo_stop = mtk_stop,
  1814. .ndo_start_xmit = mtk_start_xmit,
  1815. .ndo_set_mac_address = mtk_set_mac_address,
  1816. .ndo_validate_addr = eth_validate_addr,
  1817. .ndo_do_ioctl = mtk_do_ioctl,
  1818. .ndo_tx_timeout = mtk_tx_timeout,
  1819. .ndo_get_stats64 = mtk_get_stats64,
  1820. .ndo_fix_features = mtk_fix_features,
  1821. .ndo_set_features = mtk_set_features,
  1822. #ifdef CONFIG_NET_POLL_CONTROLLER
  1823. .ndo_poll_controller = mtk_poll_controller,
  1824. #endif
  1825. };
  1826. static int mtk_add_mac(struct mtk_eth *eth, struct device_node *np)
  1827. {
  1828. struct mtk_mac *mac;
  1829. const __be32 *_id = of_get_property(np, "reg", NULL);
  1830. int id, err;
  1831. if (!_id) {
  1832. dev_err(eth->dev, "missing mac id\n");
  1833. return -EINVAL;
  1834. }
  1835. id = be32_to_cpup(_id);
  1836. if (id >= MTK_MAC_COUNT) {
  1837. dev_err(eth->dev, "%d is not a valid mac id\n", id);
  1838. return -EINVAL;
  1839. }
  1840. if (eth->netdev[id]) {
  1841. dev_err(eth->dev, "duplicate mac id found: %d\n", id);
  1842. return -EINVAL;
  1843. }
  1844. eth->netdev[id] = alloc_etherdev(sizeof(*mac));
  1845. if (!eth->netdev[id]) {
  1846. dev_err(eth->dev, "alloc_etherdev failed\n");
  1847. return -ENOMEM;
  1848. }
  1849. mac = netdev_priv(eth->netdev[id]);
  1850. eth->mac[id] = mac;
  1851. mac->id = id;
  1852. mac->hw = eth;
  1853. mac->of_node = np;
  1854. memset(mac->hwlro_ip, 0, sizeof(mac->hwlro_ip));
  1855. mac->hwlro_ip_cnt = 0;
  1856. mac->hw_stats = devm_kzalloc(eth->dev,
  1857. sizeof(*mac->hw_stats),
  1858. GFP_KERNEL);
  1859. if (!mac->hw_stats) {
  1860. dev_err(eth->dev, "failed to allocate counter memory\n");
  1861. err = -ENOMEM;
  1862. goto free_netdev;
  1863. }
  1864. spin_lock_init(&mac->hw_stats->stats_lock);
  1865. u64_stats_init(&mac->hw_stats->syncp);
  1866. mac->hw_stats->reg_offset = id * MTK_STAT_OFFSET;
  1867. SET_NETDEV_DEV(eth->netdev[id], eth->dev);
  1868. eth->netdev[id]->watchdog_timeo = 5 * HZ;
  1869. eth->netdev[id]->netdev_ops = &mtk_netdev_ops;
  1870. eth->netdev[id]->base_addr = (unsigned long)eth->base;
  1871. eth->netdev[id]->hw_features = MTK_HW_FEATURES;
  1872. if (eth->hwlro)
  1873. eth->netdev[id]->hw_features |= NETIF_F_LRO;
  1874. eth->netdev[id]->vlan_features = MTK_HW_FEATURES &
  1875. ~(NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX);
  1876. eth->netdev[id]->features |= MTK_HW_FEATURES;
  1877. eth->netdev[id]->ethtool_ops = &mtk_ethtool_ops;
  1878. eth->netdev[id]->irq = eth->irq[0];
  1879. eth->netdev[id]->dev.of_node = np;
  1880. return 0;
  1881. free_netdev:
  1882. free_netdev(eth->netdev[id]);
  1883. return err;
  1884. }
  1885. static int mtk_get_chip_id(struct mtk_eth *eth, u32 *chip_id)
  1886. {
  1887. u32 val[2], id[4];
  1888. regmap_read(eth->ethsys, ETHSYS_CHIPID0_3, &val[0]);
  1889. regmap_read(eth->ethsys, ETHSYS_CHIPID4_7, &val[1]);
  1890. id[3] = ((val[0] >> 16) & 0xff) - '0';
  1891. id[2] = ((val[0] >> 24) & 0xff) - '0';
  1892. id[1] = (val[1] & 0xff) - '0';
  1893. id[0] = ((val[1] >> 8) & 0xff) - '0';
  1894. *chip_id = (id[3] * 1000) + (id[2] * 100) +
  1895. (id[1] * 10) + id[0];
  1896. if (!(*chip_id)) {
  1897. dev_err(eth->dev, "failed to get chip id\n");
  1898. return -ENODEV;
  1899. }
  1900. dev_info(eth->dev, "chip id = %d\n", *chip_id);
  1901. return 0;
  1902. }
  1903. static bool mtk_is_hwlro_supported(struct mtk_eth *eth)
  1904. {
  1905. switch (eth->chip_id) {
  1906. case MT7623_ETH:
  1907. return true;
  1908. }
  1909. return false;
  1910. }
  1911. static int mtk_probe(struct platform_device *pdev)
  1912. {
  1913. struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1914. struct device_node *mac_np;
  1915. const struct of_device_id *match;
  1916. struct mtk_soc_data *soc;
  1917. struct mtk_eth *eth;
  1918. int err;
  1919. int i;
  1920. match = of_match_device(of_mtk_match, &pdev->dev);
  1921. soc = (struct mtk_soc_data *)match->data;
  1922. eth = devm_kzalloc(&pdev->dev, sizeof(*eth), GFP_KERNEL);
  1923. if (!eth)
  1924. return -ENOMEM;
  1925. eth->dev = &pdev->dev;
  1926. eth->base = devm_ioremap_resource(&pdev->dev, res);
  1927. if (IS_ERR(eth->base))
  1928. return PTR_ERR(eth->base);
  1929. spin_lock_init(&eth->page_lock);
  1930. spin_lock_init(&eth->irq_lock);
  1931. eth->ethsys = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
  1932. "mediatek,ethsys");
  1933. if (IS_ERR(eth->ethsys)) {
  1934. dev_err(&pdev->dev, "no ethsys regmap found\n");
  1935. return PTR_ERR(eth->ethsys);
  1936. }
  1937. eth->pctl = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
  1938. "mediatek,pctl");
  1939. if (IS_ERR(eth->pctl)) {
  1940. dev_err(&pdev->dev, "no pctl regmap found\n");
  1941. return PTR_ERR(eth->pctl);
  1942. }
  1943. for (i = 0; i < 3; i++) {
  1944. eth->irq[i] = platform_get_irq(pdev, i);
  1945. if (eth->irq[i] < 0) {
  1946. dev_err(&pdev->dev, "no IRQ%d resource found\n", i);
  1947. return -ENXIO;
  1948. }
  1949. }
  1950. for (i = 0; i < ARRAY_SIZE(eth->clks); i++) {
  1951. eth->clks[i] = devm_clk_get(eth->dev,
  1952. mtk_clks_source_name[i]);
  1953. if (IS_ERR(eth->clks[i])) {
  1954. if (PTR_ERR(eth->clks[i]) == -EPROBE_DEFER)
  1955. return -EPROBE_DEFER;
  1956. return -ENODEV;
  1957. }
  1958. }
  1959. eth->msg_enable = netif_msg_init(mtk_msg_level, MTK_DEFAULT_MSG_ENABLE);
  1960. INIT_WORK(&eth->pending_work, mtk_pending_work);
  1961. err = mtk_hw_init(eth);
  1962. if (err)
  1963. return err;
  1964. err = mtk_get_chip_id(eth, &eth->chip_id);
  1965. if (err)
  1966. return err;
  1967. eth->hwlro = mtk_is_hwlro_supported(eth);
  1968. for_each_child_of_node(pdev->dev.of_node, mac_np) {
  1969. if (!of_device_is_compatible(mac_np,
  1970. "mediatek,eth-mac"))
  1971. continue;
  1972. if (!of_device_is_available(mac_np))
  1973. continue;
  1974. err = mtk_add_mac(eth, mac_np);
  1975. if (err)
  1976. goto err_deinit_hw;
  1977. }
  1978. err = devm_request_irq(eth->dev, eth->irq[1], mtk_handle_irq_tx, 0,
  1979. dev_name(eth->dev), eth);
  1980. if (err)
  1981. goto err_free_dev;
  1982. err = devm_request_irq(eth->dev, eth->irq[2], mtk_handle_irq_rx, 0,
  1983. dev_name(eth->dev), eth);
  1984. if (err)
  1985. goto err_free_dev;
  1986. err = mtk_mdio_init(eth);
  1987. if (err)
  1988. goto err_free_dev;
  1989. for (i = 0; i < MTK_MAX_DEVS; i++) {
  1990. if (!eth->netdev[i])
  1991. continue;
  1992. err = register_netdev(eth->netdev[i]);
  1993. if (err) {
  1994. dev_err(eth->dev, "error bringing up device\n");
  1995. goto err_deinit_mdio;
  1996. } else
  1997. netif_info(eth, probe, eth->netdev[i],
  1998. "mediatek frame engine at 0x%08lx, irq %d\n",
  1999. eth->netdev[i]->base_addr, eth->irq[0]);
  2000. }
  2001. /* we run 2 devices on the same DMA ring so we need a dummy device
  2002. * for NAPI to work
  2003. */
  2004. init_dummy_netdev(&eth->dummy_dev);
  2005. netif_napi_add(&eth->dummy_dev, &eth->tx_napi, mtk_napi_tx,
  2006. MTK_NAPI_WEIGHT);
  2007. netif_napi_add(&eth->dummy_dev, &eth->rx_napi, mtk_napi_rx,
  2008. MTK_NAPI_WEIGHT);
  2009. platform_set_drvdata(pdev, eth);
  2010. return 0;
  2011. err_deinit_mdio:
  2012. mtk_mdio_cleanup(eth);
  2013. err_free_dev:
  2014. mtk_free_dev(eth);
  2015. err_deinit_hw:
  2016. mtk_hw_deinit(eth);
  2017. return err;
  2018. }
  2019. static int mtk_remove(struct platform_device *pdev)
  2020. {
  2021. struct mtk_eth *eth = platform_get_drvdata(pdev);
  2022. int i;
  2023. /* stop all devices to make sure that dma is properly shut down */
  2024. for (i = 0; i < MTK_MAC_COUNT; i++) {
  2025. if (!eth->netdev[i])
  2026. continue;
  2027. mtk_stop(eth->netdev[i]);
  2028. }
  2029. mtk_hw_deinit(eth);
  2030. netif_napi_del(&eth->tx_napi);
  2031. netif_napi_del(&eth->rx_napi);
  2032. mtk_cleanup(eth);
  2033. mtk_mdio_cleanup(eth);
  2034. return 0;
  2035. }
  2036. const struct of_device_id of_mtk_match[] = {
  2037. { .compatible = "mediatek,mt2701-eth" },
  2038. {},
  2039. };
  2040. MODULE_DEVICE_TABLE(of, of_mtk_match);
  2041. static struct platform_driver mtk_driver = {
  2042. .probe = mtk_probe,
  2043. .remove = mtk_remove,
  2044. .driver = {
  2045. .name = "mtk_soc_eth",
  2046. .of_match_table = of_mtk_match,
  2047. },
  2048. };
  2049. module_platform_driver(mtk_driver);
  2050. MODULE_LICENSE("GPL");
  2051. MODULE_AUTHOR("John Crispin <blogic@openwrt.org>");
  2052. MODULE_DESCRIPTION("Ethernet driver for MediaTek SoC");