common.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <linux/of_mdio.h>
  3. #include <linux/of_platform.h>
  4. #include <net/arp.h>
  5. #include <net/nexthop.h>
  6. #include <net/neighbour.h>
  7. #include <net/netevent.h>
  8. #include <linux/etherdevice.h>
  9. #include <linux/if_vlan.h>
  10. #include <linux/inetdevice.h>
  11. #include <linux/rhashtable.h>
  12. #include <linux/of_net.h>
  13. #include <asm/mach-rtl838x/mach-rtl83xx.h>
  14. #include "rtl83xx.h"
  15. extern struct rtl83xx_soc_info soc_info;
  16. extern const struct rtl838x_reg rtl838x_reg;
  17. extern const struct rtl838x_reg rtl839x_reg;
  18. extern const struct rtl838x_reg rtl930x_reg;
  19. extern const struct rtl838x_reg rtl931x_reg;
  20. extern const struct dsa_switch_ops rtl83xx_switch_ops;
  21. extern const struct dsa_switch_ops rtl930x_switch_ops;
  22. DEFINE_MUTEX(smi_lock);
  23. int rtl83xx_port_get_stp_state(struct rtl838x_switch_priv *priv, int port)
  24. {
  25. u32 msti = 0;
  26. u32 port_state[4];
  27. int index, bit;
  28. int pos = port;
  29. int n = priv->port_width << 1;
  30. /* Ports above or equal CPU port can never be configured */
  31. if (port >= priv->cpu_port)
  32. return -1;
  33. mutex_lock(&priv->reg_mutex);
  34. /* For the RTL839x and following, the bits are left-aligned in the 64/128 bit field */
  35. if (priv->family_id == RTL8390_FAMILY_ID)
  36. pos += 12;
  37. if (priv->family_id == RTL9300_FAMILY_ID)
  38. pos += 3;
  39. if (priv->family_id == RTL9310_FAMILY_ID)
  40. pos += 8;
  41. index = n - (pos >> 4) - 1;
  42. bit = (pos << 1) % 32;
  43. priv->r->stp_get(priv, msti, port_state);
  44. mutex_unlock(&priv->reg_mutex);
  45. return (port_state[index] >> bit) & 3;
  46. }
  47. static struct table_reg rtl838x_tbl_regs[] = {
  48. TBL_DESC(0x6900, 0x6908, 3, 15, 13, 1), /* RTL8380_TBL_L2 */
  49. TBL_DESC(0x6914, 0x6918, 18, 14, 12, 1), /* RTL8380_TBL_0 */
  50. TBL_DESC(0xA4C8, 0xA4CC, 6, 14, 12, 1), /* RTL8380_TBL_1 */
  51. TBL_DESC(0x1180, 0x1184, 3, 16, 14, 0), /* RTL8390_TBL_L2 */
  52. TBL_DESC(0x1190, 0x1194, 17, 15, 12, 0), /* RTL8390_TBL_0 */
  53. TBL_DESC(0x6B80, 0x6B84, 4, 14, 12, 0), /* RTL8390_TBL_1 */
  54. TBL_DESC(0x611C, 0x6120, 9, 8, 6, 0), /* RTL8390_TBL_2 */
  55. TBL_DESC(0xB320, 0xB334, 3, 18, 16, 0), /* RTL9300_TBL_L2 */
  56. TBL_DESC(0xB340, 0xB344, 19, 16, 12, 0), /* RTL9300_TBL_0 */
  57. TBL_DESC(0xB3A0, 0xB3A4, 20, 16, 13, 0), /* RTL9300_TBL_1 */
  58. TBL_DESC(0xCE04, 0xCE08, 6, 14, 12, 0), /* RTL9300_TBL_2 */
  59. TBL_DESC(0xD600, 0xD604, 30, 7, 6, 0), /* RTL9300_TBL_HSB */
  60. TBL_DESC(0x7880, 0x7884, 22, 9, 8, 0), /* RTL9300_TBL_HSA */
  61. TBL_DESC(0x8500, 0x8508, 8, 19, 15, 0), /* RTL9310_TBL_0 */
  62. TBL_DESC(0x40C0, 0x40C4, 22, 16, 14, 0), /* RTL9310_TBL_1 */
  63. TBL_DESC(0x8528, 0x852C, 6, 18, 14, 0), /* RTL9310_TBL_2 */
  64. TBL_DESC(0x0200, 0x0204, 9, 15, 12, 0), /* RTL9310_TBL_3 */
  65. TBL_DESC(0x20dc, 0x20e0, 29, 7, 6, 0), /* RTL9310_TBL_4 */
  66. TBL_DESC(0x7e1c, 0x7e20, 53, 8, 6, 0), /* RTL9310_TBL_5 */
  67. };
  68. void rtl_table_init(void)
  69. {
  70. for (int i = 0; i < RTL_TBL_END; i++)
  71. mutex_init(&rtl838x_tbl_regs[i].lock);
  72. }
  73. /* Request access to table t in table access register r
  74. * Returns a handle to a lock for that table
  75. */
  76. struct table_reg *rtl_table_get(rtl838x_tbl_reg_t r, int t)
  77. {
  78. if (r >= RTL_TBL_END)
  79. return NULL;
  80. if (t >= BIT(rtl838x_tbl_regs[r].c_bit-rtl838x_tbl_regs[r].t_bit))
  81. return NULL;
  82. mutex_lock(&rtl838x_tbl_regs[r].lock);
  83. rtl838x_tbl_regs[r].tbl = t;
  84. return &rtl838x_tbl_regs[r];
  85. }
  86. /* Release a table r, unlock the corresponding lock */
  87. void rtl_table_release(struct table_reg *r)
  88. {
  89. if (!r)
  90. return;
  91. /* pr_info("Unlocking %08x\n", (u32)r); */
  92. mutex_unlock(&r->lock);
  93. /* pr_info("Unlock done\n"); */
  94. }
  95. static int rtl_table_exec(struct table_reg *r, bool is_write, int idx)
  96. {
  97. int ret = 0;
  98. u32 cmd, val;
  99. /* Read/write bit has inverted meaning on RTL838x */
  100. if (r->rmode)
  101. cmd = is_write ? 0 : BIT(r->c_bit);
  102. else
  103. cmd = is_write ? BIT(r->c_bit) : 0;
  104. cmd |= BIT(r->c_bit + 1); /* Execute bit */
  105. cmd |= r->tbl << r->t_bit; /* Table type */
  106. cmd |= idx & (BIT(r->t_bit) - 1); /* Index */
  107. sw_w32(cmd, r->addr);
  108. ret = readx_poll_timeout(sw_r32, r->addr, val,
  109. !(val & BIT(r->c_bit + 1)), 20, 10000);
  110. if (ret)
  111. pr_err("%s: timeout\n", __func__);
  112. return ret;
  113. }
  114. /* Reads table index idx into the data registers of the table */
  115. int rtl_table_read(struct table_reg *r, int idx)
  116. {
  117. return rtl_table_exec(r, false, idx);
  118. }
  119. /* Writes the content of the table data registers into the table at index idx */
  120. int rtl_table_write(struct table_reg *r, int idx)
  121. {
  122. return rtl_table_exec(r, true, idx);
  123. }
  124. /* Returns the address of the ith data register of table register r
  125. * the address is relative to the beginning of the Switch-IO block at 0xbb000000
  126. */
  127. inline u16 rtl_table_data(struct table_reg *r, int i)
  128. {
  129. if (i >= r->max_data)
  130. i = r->max_data - 1;
  131. return r->data + i * 4;
  132. }
  133. inline u32 rtl_table_data_r(struct table_reg *r, int i)
  134. {
  135. return sw_r32(rtl_table_data(r, i));
  136. }
  137. inline void rtl_table_data_w(struct table_reg *r, u32 v, int i)
  138. {
  139. sw_w32(v, rtl_table_data(r, i));
  140. }
  141. /* Port register accessor functions for the RTL838x and RTL930X SoCs */
  142. void rtl838x_mask_port_reg(u64 clear, u64 set, int reg)
  143. {
  144. sw_w32_mask((u32)clear, (u32)set, reg);
  145. }
  146. void rtl838x_set_port_reg(u64 set, int reg)
  147. {
  148. sw_w32((u32)set, reg);
  149. }
  150. u64 rtl838x_get_port_reg(int reg)
  151. {
  152. return ((u64)sw_r32(reg));
  153. }
  154. /* Port register accessor functions for the RTL839x and RTL931X SoCs */
  155. void rtl839x_mask_port_reg_be(u64 clear, u64 set, int reg)
  156. {
  157. sw_w32_mask((u32)(clear >> 32), (u32)(set >> 32), reg);
  158. sw_w32_mask((u32)(clear & 0xffffffff), (u32)(set & 0xffffffff), reg + 4);
  159. }
  160. u64 rtl839x_get_port_reg_be(int reg)
  161. {
  162. u64 v = sw_r32(reg);
  163. v <<= 32;
  164. v |= sw_r32(reg + 4);
  165. return v;
  166. }
  167. void rtl839x_set_port_reg_be(u64 set, int reg)
  168. {
  169. sw_w32(set >> 32, reg);
  170. sw_w32(set & 0xffffffff, reg + 4);
  171. }
  172. void rtl839x_mask_port_reg_le(u64 clear, u64 set, int reg)
  173. {
  174. sw_w32_mask((u32)clear, (u32)set, reg);
  175. sw_w32_mask((u32)(clear >> 32), (u32)(set >> 32), reg + 4);
  176. }
  177. void rtl839x_set_port_reg_le(u64 set, int reg)
  178. {
  179. sw_w32(set, reg);
  180. sw_w32(set >> 32, reg + 4);
  181. }
  182. u64 rtl839x_get_port_reg_le(int reg)
  183. {
  184. u64 v = sw_r32(reg + 4);
  185. v <<= 32;
  186. v |= sw_r32(reg);
  187. return v;
  188. }
  189. int read_phy(u32 port, u32 page, u32 reg, u32 *val)
  190. {
  191. switch (soc_info.family) {
  192. case RTL8380_FAMILY_ID:
  193. return rtl838x_read_phy(port, page, reg, val);
  194. case RTL8390_FAMILY_ID:
  195. return rtl839x_read_phy(port, page, reg, val);
  196. case RTL9300_FAMILY_ID:
  197. return rtl930x_read_phy(port, page, reg, val);
  198. case RTL9310_FAMILY_ID:
  199. return rtl931x_read_phy(port, page, reg, val);
  200. }
  201. return -1;
  202. }
  203. int write_phy(u32 port, u32 page, u32 reg, u32 val)
  204. {
  205. switch (soc_info.family) {
  206. case RTL8380_FAMILY_ID:
  207. return rtl838x_write_phy(port, page, reg, val);
  208. case RTL8390_FAMILY_ID:
  209. return rtl839x_write_phy(port, page, reg, val);
  210. case RTL9300_FAMILY_ID:
  211. return rtl930x_write_phy(port, page, reg, val);
  212. case RTL9310_FAMILY_ID:
  213. return rtl931x_write_phy(port, page, reg, val);
  214. }
  215. return -1;
  216. }
  217. static int __init rtl83xx_mdio_probe(struct rtl838x_switch_priv *priv)
  218. {
  219. struct device *dev = priv->dev;
  220. struct device_node *dn, *phy_node, *mii_np = dev->of_node;
  221. struct mii_bus *bus;
  222. int ret;
  223. u32 pn;
  224. pr_debug("In %s\n", __func__);
  225. mii_np = of_find_compatible_node(NULL, NULL, "realtek,rtl838x-mdio");
  226. if (mii_np) {
  227. pr_debug("Found compatible MDIO node!\n");
  228. } else {
  229. dev_err(priv->dev, "no %s child node found", "mdio-bus");
  230. return -ENODEV;
  231. }
  232. priv->mii_bus = of_mdio_find_bus(mii_np);
  233. if (!priv->mii_bus) {
  234. pr_debug("Deferring probe of mdio bus\n");
  235. return -EPROBE_DEFER;
  236. }
  237. if (!of_device_is_available(mii_np))
  238. ret = -ENODEV;
  239. bus = devm_mdiobus_alloc(priv->ds->dev);
  240. if (!bus)
  241. return -ENOMEM;
  242. bus->name = "rtl838x slave mii";
  243. /* Since the NIC driver is loaded first, we can use the mdio rw functions
  244. * assigned there.
  245. */
  246. bus->read = priv->mii_bus->read;
  247. bus->write = priv->mii_bus->write;
  248. bus->read_paged = priv->mii_bus->read_paged;
  249. bus->write_paged = priv->mii_bus->write_paged;
  250. snprintf(bus->id, MII_BUS_ID_SIZE, "%s-%d", bus->name, dev->id);
  251. bus->parent = dev;
  252. priv->ds->slave_mii_bus = bus;
  253. priv->ds->slave_mii_bus->priv = priv->mii_bus->priv;
  254. priv->ds->slave_mii_bus->access_capabilities = priv->mii_bus->access_capabilities;
  255. ret = mdiobus_register(priv->ds->slave_mii_bus);
  256. if (ret && mii_np) {
  257. of_node_put(dn);
  258. return ret;
  259. }
  260. dn = of_find_compatible_node(NULL, NULL, "realtek,rtl83xx-switch");
  261. if (!dn) {
  262. dev_err(priv->dev, "No RTL switch node in DTS\n");
  263. return -ENODEV;
  264. }
  265. for_each_node_by_name(dn, "port") {
  266. phy_interface_t interface;
  267. u32 led_set;
  268. if (!of_device_is_available(dn))
  269. continue;
  270. if (of_property_read_u32(dn, "reg", &pn))
  271. continue;
  272. phy_node = of_parse_phandle(dn, "phy-handle", 0);
  273. if (!phy_node) {
  274. if (pn != priv->cpu_port)
  275. dev_err(priv->dev, "Port node %d misses phy-handle\n", pn);
  276. continue;
  277. }
  278. if (of_property_read_u32(phy_node, "sds", &priv->ports[pn].sds_num))
  279. priv->ports[pn].sds_num = -1;
  280. pr_debug("%s port %d has SDS %d\n", __func__, pn, priv->ports[pn].sds_num);
  281. if (of_get_phy_mode(dn, &interface))
  282. interface = PHY_INTERFACE_MODE_NA;
  283. if (interface == PHY_INTERFACE_MODE_HSGMII)
  284. priv->ports[pn].is2G5 = true;
  285. if (interface == PHY_INTERFACE_MODE_USXGMII)
  286. priv->ports[pn].is2G5 = priv->ports[pn].is10G = true;
  287. if (interface == PHY_INTERFACE_MODE_10GBASER)
  288. priv->ports[pn].is10G = true;
  289. if (of_property_read_u32(dn, "led-set", &led_set))
  290. led_set = 0;
  291. priv->ports[pn].led_set = led_set;
  292. /* Check for the integrated SerDes of the RTL8380M first */
  293. if (of_property_read_bool(phy_node, "phy-is-integrated")
  294. && priv->id == 0x8380 && pn >= 24) {
  295. pr_debug("----> FÓUND A SERDES\n");
  296. priv->ports[pn].phy = PHY_RTL838X_SDS;
  297. continue;
  298. }
  299. if (priv->id >= 0x9300) {
  300. priv->ports[pn].phy_is_integrated = false;
  301. if (of_property_read_bool(phy_node, "phy-is-integrated")) {
  302. priv->ports[pn].phy_is_integrated = true;
  303. priv->ports[pn].phy = PHY_RTL930X_SDS;
  304. }
  305. } else {
  306. if (of_property_read_bool(phy_node, "phy-is-integrated") &&
  307. !of_property_read_bool(phy_node, "sfp")) {
  308. priv->ports[pn].phy = PHY_RTL8218B_INT;
  309. continue;
  310. }
  311. }
  312. if (!of_property_read_bool(phy_node, "phy-is-integrated") &&
  313. of_property_read_bool(phy_node, "sfp")) {
  314. priv->ports[pn].phy = PHY_RTL8214FC;
  315. continue;
  316. }
  317. if (!of_property_read_bool(phy_node, "phy-is-integrated") &&
  318. !of_property_read_bool(phy_node, "sfp")) {
  319. priv->ports[pn].phy = PHY_RTL8218B_EXT;
  320. continue;
  321. }
  322. }
  323. /* Disable MAC polling the PHY so that we can start configuration */
  324. priv->r->set_port_reg_le(0ULL, priv->r->smi_poll_ctrl);
  325. /* Enable PHY control via SoC */
  326. if (priv->family_id == RTL8380_FAMILY_ID) {
  327. /* Enable SerDes NWAY and PHY control via SoC */
  328. sw_w32_mask(BIT(7), BIT(15), RTL838X_SMI_GLB_CTRL);
  329. } else if (priv->family_id == RTL8390_FAMILY_ID) {
  330. /* Disable PHY polling via SoC */
  331. sw_w32_mask(BIT(7), 0, RTL839X_SMI_GLB_CTRL);
  332. }
  333. /* Power on fibre ports and reset them if necessary */
  334. if (priv->ports[24].phy == PHY_RTL838X_SDS) {
  335. pr_debug("Powering on fibre ports & reset\n");
  336. rtl8380_sds_power(24, 1);
  337. rtl8380_sds_power(26, 1);
  338. }
  339. pr_debug("%s done\n", __func__);
  340. return 0;
  341. }
  342. static int __init rtl83xx_get_l2aging(struct rtl838x_switch_priv *priv)
  343. {
  344. int t = sw_r32(priv->r->l2_ctrl_1);
  345. t &= priv->family_id == RTL8380_FAMILY_ID ? 0x7fffff : 0x1FFFFF;
  346. if (priv->family_id == RTL8380_FAMILY_ID)
  347. t = t * 128 / 625; /* Aging time in seconds. 0: L2 aging disabled */
  348. else
  349. t = (t * 3) / 5;
  350. pr_debug("L2 AGING time: %d sec\n", t);
  351. pr_debug("Dynamic aging for ports: %x\n", sw_r32(priv->r->l2_port_aging_out));
  352. return t;
  353. }
  354. /* Caller must hold priv->reg_mutex */
  355. int rtl83xx_lag_add(struct dsa_switch *ds, int group, int port, struct netdev_lag_upper_info *info)
  356. {
  357. struct rtl838x_switch_priv *priv = ds->priv;
  358. int i;
  359. u32 algomsk = 0;
  360. u32 algoidx = 0;
  361. if (info->tx_type != NETDEV_LAG_TX_TYPE_HASH) {
  362. pr_err("%s: Only mode LACP 802.3ad (4) allowed.\n", __func__);
  363. return -EINVAL;
  364. }
  365. if (group >= priv->n_lags) {
  366. pr_err("%s: LAG %d invalid.\n", __func__, group);
  367. return -EINVAL;
  368. }
  369. if (port >= priv->cpu_port) {
  370. pr_err("%s: Port %d invalid.\n", __func__, port);
  371. return -EINVAL;
  372. }
  373. for (i = 0; i < priv->n_lags; i++) {
  374. if (priv->lags_port_members[i] & BIT_ULL(port))
  375. break;
  376. }
  377. if (i != priv->n_lags) {
  378. pr_err("%s: Port %d already member of LAG %d.\n", __func__, port, i);
  379. return -ENOSPC;
  380. }
  381. switch(info->hash_type) {
  382. case NETDEV_LAG_HASH_L2:
  383. algomsk |= TRUNK_DISTRIBUTION_ALGO_DMAC_BIT;
  384. algomsk |= TRUNK_DISTRIBUTION_ALGO_SMAC_BIT;
  385. break;
  386. case NETDEV_LAG_HASH_L23:
  387. algomsk |= TRUNK_DISTRIBUTION_ALGO_DMAC_BIT;
  388. algomsk |= TRUNK_DISTRIBUTION_ALGO_SMAC_BIT;
  389. algomsk |= TRUNK_DISTRIBUTION_ALGO_SIP_BIT; /* source ip */
  390. algomsk |= TRUNK_DISTRIBUTION_ALGO_DIP_BIT; /* dest ip */
  391. algoidx = 1;
  392. break;
  393. case NETDEV_LAG_HASH_L34:
  394. algomsk |= TRUNK_DISTRIBUTION_ALGO_SRC_L4PORT_BIT; /* sport */
  395. algomsk |= TRUNK_DISTRIBUTION_ALGO_DST_L4PORT_BIT; /* dport */
  396. algomsk |= TRUNK_DISTRIBUTION_ALGO_SIP_BIT; /* source ip */
  397. algomsk |= TRUNK_DISTRIBUTION_ALGO_DIP_BIT; /* dest ip */
  398. algoidx = 2;
  399. break;
  400. default:
  401. algomsk |= 0x7f;
  402. }
  403. priv->r->set_distribution_algorithm(group, algoidx, algomsk);
  404. priv->r->mask_port_reg_be(0, BIT_ULL(port), priv->r->trk_mbr_ctr(group));
  405. priv->lags_port_members[group] |= BIT_ULL(port);
  406. pr_info("%s: Added port %d to LAG %d. Members now %016llx.\n",
  407. __func__, port, group, priv->lags_port_members[group]);
  408. return 0;
  409. }
  410. /* Caller must hold priv->reg_mutex */
  411. int rtl83xx_lag_del(struct dsa_switch *ds, int group, int port)
  412. {
  413. struct rtl838x_switch_priv *priv = ds->priv;
  414. if (group >= priv->n_lags) {
  415. pr_err("%s: LAG %d invalid.\n", __func__, group);
  416. return -EINVAL;
  417. }
  418. if (port >= priv->cpu_port) {
  419. pr_err("%s: Port %d invalid.\n", __func__, port);
  420. return -EINVAL;
  421. }
  422. if (!(priv->lags_port_members[group] & BIT_ULL(port))) {
  423. pr_err("%s: Port %d not member of LAG %d.\n", __func__, port, group);
  424. return -ENOSPC;
  425. }
  426. /* 0x7f algo mask all */
  427. priv->r->mask_port_reg_be(BIT_ULL(port), 0, priv->r->trk_mbr_ctr(group));
  428. priv->lags_port_members[group] &= ~BIT_ULL(port);
  429. pr_info("%s: Removed port %d from LAG %d. Members now %016llx.\n",
  430. __func__, port, group, priv->lags_port_members[group]);
  431. return 0;
  432. }
  433. // Currently Unused
  434. // /* Allocate a 64 bit octet counter located in the LOG HW table */
  435. // static int rtl83xx_octet_cntr_alloc(struct rtl838x_switch_priv *priv)
  436. // {
  437. // int idx;
  438. // mutex_lock(&priv->reg_mutex);
  439. // idx = find_first_zero_bit(priv->octet_cntr_use_bm, MAX_COUNTERS);
  440. // if (idx >= priv->n_counters) {
  441. // mutex_unlock(&priv->reg_mutex);
  442. // return -1;
  443. // }
  444. // set_bit(idx, priv->octet_cntr_use_bm);
  445. // mutex_unlock(&priv->reg_mutex);
  446. // return idx;
  447. // }
  448. /* Allocate a 32-bit packet counter
  449. * 2 32-bit packet counters share the location of a 64-bit octet counter
  450. * Initially there are no free packet counters and 2 new ones need to be freed
  451. * by allocating the corresponding octet counter
  452. */
  453. int rtl83xx_packet_cntr_alloc(struct rtl838x_switch_priv *priv)
  454. {
  455. int idx, j;
  456. mutex_lock(&priv->reg_mutex);
  457. /* Because initially no packet counters are free, the logic is reversed:
  458. * a 0-bit means the counter is already allocated (for octets)
  459. */
  460. idx = find_first_bit(priv->packet_cntr_use_bm, MAX_COUNTERS * 2);
  461. if (idx >= priv->n_counters * 2) {
  462. j = find_first_zero_bit(priv->octet_cntr_use_bm, MAX_COUNTERS);
  463. if (j >= priv->n_counters) {
  464. mutex_unlock(&priv->reg_mutex);
  465. return -1;
  466. }
  467. set_bit(j, priv->octet_cntr_use_bm);
  468. idx = j * 2;
  469. set_bit(j * 2 + 1, priv->packet_cntr_use_bm);
  470. } else {
  471. clear_bit(idx, priv->packet_cntr_use_bm);
  472. }
  473. mutex_unlock(&priv->reg_mutex);
  474. return idx;
  475. }
  476. /* Add an L2 nexthop entry for the L3 routing system / PIE forwarding in the SoC
  477. * Use VID and MAC in rtl838x_l2_entry to identify either a free slot in the L2 hash table
  478. * or mark an existing entry as a nexthop by setting it's nexthop bit
  479. * Called from the L3 layer
  480. * The index in the L2 hash table is filled into nh->l2_id;
  481. */
  482. int rtl83xx_l2_nexthop_add(struct rtl838x_switch_priv *priv, struct rtl83xx_nexthop *nh)
  483. {
  484. struct rtl838x_l2_entry e;
  485. u64 seed = priv->r->l2_hash_seed(nh->mac, nh->rvid);
  486. u32 key = priv->r->l2_hash_key(priv, seed);
  487. int idx = -1;
  488. u64 entry;
  489. pr_debug("%s searching for %08llx vid %d with key %d, seed: %016llx\n",
  490. __func__, nh->mac, nh->rvid, key, seed);
  491. e.type = L2_UNICAST;
  492. u64_to_ether_addr(nh->mac, &e.mac[0]);
  493. e.port = nh->port;
  494. /* Loop over all entries in the hash-bucket and over the second block on 93xx SoCs */
  495. for (int i = 0; i < priv->l2_bucket_size; i++) {
  496. entry = priv->r->read_l2_entry_using_hash(key, i, &e);
  497. if (!e.valid || ((entry & 0x0fffffffffffffffULL) == seed)) {
  498. idx = i > 3 ? ((key >> 14) & 0xffff) | i >> 1
  499. : ((key << 2) | i) & 0xffff;
  500. break;
  501. }
  502. }
  503. if (idx < 0) {
  504. pr_err("%s: No more L2 forwarding entries available\n", __func__);
  505. return -1;
  506. }
  507. /* Found an existing (e->valid is true) or empty entry, make it a nexthop entry */
  508. nh->l2_id = idx;
  509. if (e.valid) {
  510. nh->port = e.port;
  511. nh->vid = e.vid; /* Save VID */
  512. nh->rvid = e.rvid;
  513. nh->dev_id = e.stack_dev;
  514. /* If the entry is already a valid next hop entry, don't change it */
  515. if (e.next_hop)
  516. return 0;
  517. } else {
  518. e.valid = true;
  519. e.is_static = true;
  520. e.rvid = nh->rvid;
  521. e.is_ip_mc = false;
  522. e.is_ipv6_mc = false;
  523. e.block_da = false;
  524. e.block_sa = false;
  525. e.suspended = false;
  526. e.age = 0; /* With port-ignore */
  527. e.port = priv->port_ignore;
  528. u64_to_ether_addr(nh->mac, &e.mac[0]);
  529. }
  530. e.next_hop = true;
  531. e.nh_route_id = nh->id; /* NH route ID takes place of VID */
  532. e.nh_vlan_target = false;
  533. priv->r->write_l2_entry_using_hash(idx >> 2, idx & 0x3, &e);
  534. return 0;
  535. }
  536. /* Removes a Layer 2 next hop entry in the forwarding database
  537. * If it was static, the entire entry is removed, otherwise the nexthop bit is cleared
  538. * and we wait until the entry ages out
  539. */
  540. int rtl83xx_l2_nexthop_rm(struct rtl838x_switch_priv *priv, struct rtl83xx_nexthop *nh)
  541. {
  542. struct rtl838x_l2_entry e;
  543. u32 key = nh->l2_id >> 2;
  544. int i = nh->l2_id & 0x3;
  545. u64 entry = entry = priv->r->read_l2_entry_using_hash(key, i, &e);
  546. pr_debug("%s: id %d, key %d, index %d\n", __func__, nh->l2_id, key, i);
  547. if (!e.valid) {
  548. dev_err(priv->dev, "unknown nexthop, id %x\n", nh->l2_id);
  549. return -1;
  550. }
  551. if (e.is_static)
  552. e.valid = false;
  553. e.next_hop = false;
  554. e.vid = nh->vid; /* Restore VID */
  555. e.rvid = nh->rvid;
  556. priv->r->write_l2_entry_using_hash(key, i, &e);
  557. return 0;
  558. }
  559. static int rtl83xx_handle_changeupper(struct rtl838x_switch_priv *priv,
  560. struct net_device *ndev,
  561. struct netdev_notifier_changeupper_info *info)
  562. {
  563. struct net_device *upper = info->upper_dev;
  564. struct netdev_lag_upper_info *lag_upper_info = NULL;
  565. int i, j, err;
  566. if (!netif_is_lag_master(upper))
  567. return 0;
  568. mutex_lock(&priv->reg_mutex);
  569. for (i = 0; i < priv->n_lags; i++) {
  570. if ((!priv->lag_devs[i]) || (priv->lag_devs[i] == upper))
  571. break;
  572. }
  573. for (j = 0; j < priv->cpu_port; j++) {
  574. if (priv->ports[j].dp->slave == ndev)
  575. break;
  576. }
  577. if (j >= priv->cpu_port) {
  578. err = -EINVAL;
  579. goto out;
  580. }
  581. if (info->linking) {
  582. lag_upper_info = info->upper_info;
  583. if (!priv->lag_devs[i])
  584. priv->lag_devs[i] = upper;
  585. err = rtl83xx_lag_add(priv->ds, i, priv->ports[j].dp->index, lag_upper_info);
  586. if (err) {
  587. err = -EINVAL;
  588. goto out;
  589. }
  590. } else {
  591. if (!priv->lag_devs[i])
  592. err = -EINVAL;
  593. err = rtl83xx_lag_del(priv->ds, i, priv->ports[j].dp->index);
  594. if (err) {
  595. err = -EINVAL;
  596. goto out;
  597. }
  598. if (!priv->lags_port_members[i])
  599. priv->lag_devs[i] = NULL;
  600. }
  601. out:
  602. mutex_unlock(&priv->reg_mutex);
  603. return 0;
  604. }
  605. /* Is the lower network device a DSA slave network device of our RTL930X-switch?
  606. * Unfortunately we cannot just follow dev->dsa_prt as this is only set for the
  607. * DSA master device.
  608. */
  609. int rtl83xx_port_is_under(const struct net_device * dev, struct rtl838x_switch_priv *priv)
  610. {
  611. /* TODO: On 5.12:
  612. * if(!dsa_slave_dev_check(dev)) {
  613. * netdev_info(dev, "%s: not a DSA device.\n", __func__);
  614. * return -EINVAL;
  615. * }
  616. */
  617. for (int i = 0; i < priv->cpu_port; i++) {
  618. if (!priv->ports[i].dp)
  619. continue;
  620. if (priv->ports[i].dp->slave == dev)
  621. return i;
  622. }
  623. return -EINVAL;
  624. }
  625. static int rtl83xx_netdevice_event(struct notifier_block *this,
  626. unsigned long event, void *ptr)
  627. {
  628. struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
  629. struct rtl838x_switch_priv *priv;
  630. int err;
  631. pr_debug("In: %s, event: %lu\n", __func__, event);
  632. if ((event != NETDEV_CHANGEUPPER) && (event != NETDEV_CHANGELOWERSTATE))
  633. return NOTIFY_DONE;
  634. priv = container_of(this, struct rtl838x_switch_priv, nb);
  635. switch (event) {
  636. case NETDEV_CHANGEUPPER:
  637. err = rtl83xx_handle_changeupper(priv, ndev, ptr);
  638. break;
  639. }
  640. if (err)
  641. return err;
  642. return NOTIFY_DONE;
  643. }
  644. const static struct rhashtable_params route_ht_params = {
  645. .key_len = sizeof(u32),
  646. .key_offset = offsetof(struct rtl83xx_route, gw_ip),
  647. .head_offset = offsetof(struct rtl83xx_route, linkage),
  648. };
  649. /* Updates an L3 next hop entry in the ROUTING table */
  650. static int rtl83xx_l3_nexthop_update(struct rtl838x_switch_priv *priv, __be32 ip_addr, u64 mac)
  651. {
  652. struct rtl83xx_route *r;
  653. struct rhlist_head *tmp, *list;
  654. rcu_read_lock();
  655. list = rhltable_lookup(&priv->routes, &ip_addr, route_ht_params);
  656. if (!list) {
  657. rcu_read_unlock();
  658. return -ENOENT;
  659. }
  660. rhl_for_each_entry_rcu(r, tmp, list, linkage) {
  661. pr_info("%s: Setting up fwding: ip %pI4, GW mac %016llx\n",
  662. __func__, &ip_addr, mac);
  663. /* Reads the ROUTING table entry associated with the route */
  664. priv->r->route_read(r->id, r);
  665. pr_info("Route with id %d to %pI4 / %d\n", r->id, &r->dst_ip, r->prefix_len);
  666. r->nh.mac = r->nh.gw = mac;
  667. r->nh.port = priv->port_ignore;
  668. r->nh.id = r->id;
  669. /* Do we need to explicitly add a DMAC entry with the route's nh index? */
  670. if (priv->r->set_l3_egress_mac)
  671. priv->r->set_l3_egress_mac(r->id, mac);
  672. /* Update ROUTING table: map gateway-mac and switch-mac id to route id */
  673. rtl83xx_l2_nexthop_add(priv, &r->nh);
  674. r->attr.valid = true;
  675. r->attr.action = ROUTE_ACT_FORWARD;
  676. r->attr.type = 0;
  677. r->attr.hit = false; /* Reset route-used indicator */
  678. /* Add PIE entry with dst_ip and prefix_len */
  679. r->pr.dip = r->dst_ip;
  680. r->pr.dip_m = inet_make_mask(r->prefix_len);
  681. if (r->is_host_route) {
  682. int slot = priv->r->find_l3_slot(r, false);
  683. pr_info("%s: Got slot for route: %d\n", __func__, slot);
  684. priv->r->host_route_write(slot, r);
  685. } else {
  686. priv->r->route_write(r->id, r);
  687. r->pr.fwd_sel = true;
  688. r->pr.fwd_data = r->nh.l2_id;
  689. r->pr.fwd_act = PIE_ACT_ROUTE_UC;
  690. }
  691. if (priv->r->set_l3_nexthop)
  692. priv->r->set_l3_nexthop(r->nh.id, r->nh.l2_id, r->nh.if_id);
  693. if (r->pr.id < 0) {
  694. r->pr.packet_cntr = rtl83xx_packet_cntr_alloc(priv);
  695. if (r->pr.packet_cntr >= 0) {
  696. pr_info("Using packet counter %d\n", r->pr.packet_cntr);
  697. r->pr.log_sel = true;
  698. r->pr.log_data = r->pr.packet_cntr;
  699. }
  700. priv->r->pie_rule_add(priv, &r->pr);
  701. } else {
  702. int pkts = priv->r->packet_cntr_read(r->pr.packet_cntr);
  703. pr_info("%s: total packets: %d\n", __func__, pkts);
  704. priv->r->pie_rule_write(priv, r->pr.id, &r->pr);
  705. }
  706. }
  707. rcu_read_unlock();
  708. return 0;
  709. }
  710. static int rtl83xx_port_ipv4_resolve(struct rtl838x_switch_priv *priv,
  711. struct net_device *dev, __be32 ip_addr)
  712. {
  713. struct neighbour *n = neigh_lookup(&arp_tbl, &ip_addr, dev);
  714. int err = 0;
  715. u64 mac;
  716. if (!n) {
  717. n = neigh_create(&arp_tbl, &ip_addr, dev);
  718. if (IS_ERR(n))
  719. return PTR_ERR(n);
  720. }
  721. /* If the neigh is already resolved, then go ahead and
  722. * install the entry, otherwise start the ARP process to
  723. * resolve the neigh.
  724. */
  725. if (n->nud_state & NUD_VALID) {
  726. mac = ether_addr_to_u64(n->ha);
  727. pr_info("%s: resolved mac: %016llx\n", __func__, mac);
  728. rtl83xx_l3_nexthop_update(priv, ip_addr, mac);
  729. } else {
  730. pr_info("%s: need to wait\n", __func__);
  731. neigh_event_send(n, NULL);
  732. }
  733. neigh_release(n);
  734. return err;
  735. }
  736. struct rtl83xx_walk_data {
  737. struct rtl838x_switch_priv *priv;
  738. int port;
  739. };
  740. static int rtl83xx_port_lower_walk(struct net_device *lower, struct netdev_nested_priv *_priv)
  741. {
  742. struct rtl83xx_walk_data *data = (struct rtl83xx_walk_data *)_priv->data;
  743. struct rtl838x_switch_priv *priv = data->priv;
  744. int ret = 0;
  745. int index;
  746. index = rtl83xx_port_is_under(lower, priv);
  747. data->port = index;
  748. if (index >= 0) {
  749. pr_debug("Found DSA-port, index %d\n", index);
  750. ret = 1;
  751. }
  752. return ret;
  753. }
  754. int rtl83xx_port_dev_lower_find(struct net_device *dev, struct rtl838x_switch_priv *priv)
  755. {
  756. struct rtl83xx_walk_data data;
  757. struct netdev_nested_priv _priv;
  758. data.priv = priv;
  759. data.port = 0;
  760. _priv.data = (void *)&data;
  761. netdev_walk_all_lower_dev(dev, rtl83xx_port_lower_walk, &_priv);
  762. return data.port;
  763. }
  764. static struct rtl83xx_route *rtl83xx_route_alloc(struct rtl838x_switch_priv *priv, u32 ip)
  765. {
  766. struct rtl83xx_route *r;
  767. int idx = 0, err;
  768. mutex_lock(&priv->reg_mutex);
  769. idx = find_first_zero_bit(priv->route_use_bm, MAX_ROUTES);
  770. pr_debug("%s id: %d, ip %pI4\n", __func__, idx, &ip);
  771. r = kzalloc(sizeof(*r), GFP_KERNEL);
  772. if (!r) {
  773. mutex_unlock(&priv->reg_mutex);
  774. return r;
  775. }
  776. r->id = idx;
  777. r->gw_ip = ip;
  778. r->pr.id = -1; /* We still need to allocate a rule in HW */
  779. r->is_host_route = false;
  780. err = rhltable_insert(&priv->routes, &r->linkage, route_ht_params);
  781. if (err) {
  782. pr_err("Could not insert new rule\n");
  783. mutex_unlock(&priv->reg_mutex);
  784. goto out_free;
  785. }
  786. set_bit(idx, priv->route_use_bm);
  787. mutex_unlock(&priv->reg_mutex);
  788. return r;
  789. out_free:
  790. kfree(r);
  791. return NULL;
  792. }
  793. static struct rtl83xx_route *rtl83xx_host_route_alloc(struct rtl838x_switch_priv *priv, u32 ip)
  794. {
  795. struct rtl83xx_route *r;
  796. int idx = 0, err;
  797. mutex_lock(&priv->reg_mutex);
  798. idx = find_first_zero_bit(priv->host_route_use_bm, MAX_HOST_ROUTES);
  799. pr_debug("%s id: %d, ip %pI4\n", __func__, idx, &ip);
  800. r = kzalloc(sizeof(*r), GFP_KERNEL);
  801. if (!r) {
  802. mutex_unlock(&priv->reg_mutex);
  803. return r;
  804. }
  805. /* We require a unique route ID irrespective of whether it is a prefix or host
  806. * route (on RTL93xx) as we use this ID to associate a DMAC and next-hop entry
  807. */
  808. r->id = idx + MAX_ROUTES;
  809. r->gw_ip = ip;
  810. r->pr.id = -1; /* We still need to allocate a rule in HW */
  811. r->is_host_route = true;
  812. err = rhltable_insert(&priv->routes, &r->linkage, route_ht_params);
  813. if (err) {
  814. pr_err("Could not insert new rule\n");
  815. mutex_unlock(&priv->reg_mutex);
  816. goto out_free;
  817. }
  818. set_bit(idx, priv->host_route_use_bm);
  819. mutex_unlock(&priv->reg_mutex);
  820. return r;
  821. out_free:
  822. kfree(r);
  823. return NULL;
  824. }
  825. static void rtl83xx_route_rm(struct rtl838x_switch_priv *priv, struct rtl83xx_route *r)
  826. {
  827. int id;
  828. if (rhltable_remove(&priv->routes, &r->linkage, route_ht_params))
  829. dev_warn(priv->dev, "Could not remove route\n");
  830. if (r->is_host_route) {
  831. id = priv->r->find_l3_slot(r, false);
  832. pr_debug("%s: Got id for host route: %d\n", __func__, id);
  833. r->attr.valid = false;
  834. priv->r->host_route_write(id, r);
  835. clear_bit(r->id - MAX_ROUTES, priv->host_route_use_bm);
  836. } else {
  837. /* If there is a HW representation of the route, delete it */
  838. if (priv->r->route_lookup_hw) {
  839. id = priv->r->route_lookup_hw(r);
  840. pr_info("%s: Got id for prefix route: %d\n", __func__, id);
  841. r->attr.valid = false;
  842. priv->r->route_write(id, r);
  843. }
  844. clear_bit(r->id, priv->route_use_bm);
  845. }
  846. kfree(r);
  847. }
  848. static int rtl83xx_fib4_del(struct rtl838x_switch_priv *priv,
  849. struct fib_entry_notifier_info *info)
  850. {
  851. struct fib_nh *nh = fib_info_nh(info->fi, 0);
  852. struct rtl83xx_route *r;
  853. struct rhlist_head *tmp, *list;
  854. pr_debug("In %s, ip %pI4, len %d\n", __func__, &info->dst, info->dst_len);
  855. rcu_read_lock();
  856. list = rhltable_lookup(&priv->routes, &nh->fib_nh_gw4, route_ht_params);
  857. if (!list) {
  858. rcu_read_unlock();
  859. pr_err("%s: no such gateway: %pI4\n", __func__, &nh->fib_nh_gw4);
  860. return -ENOENT;
  861. }
  862. rhl_for_each_entry_rcu(r, tmp, list, linkage) {
  863. if (r->dst_ip == info->dst && r->prefix_len == info->dst_len) {
  864. pr_info("%s: found a route with id %d, nh-id %d\n",
  865. __func__, r->id, r->nh.id);
  866. break;
  867. }
  868. }
  869. rcu_read_unlock();
  870. rtl83xx_l2_nexthop_rm(priv, &r->nh);
  871. pr_debug("%s: Releasing packet counter %d\n", __func__, r->pr.packet_cntr);
  872. set_bit(r->pr.packet_cntr, priv->packet_cntr_use_bm);
  873. priv->r->pie_rule_rm(priv, &r->pr);
  874. rtl83xx_route_rm(priv, r);
  875. nh->fib_nh_flags &= ~RTNH_F_OFFLOAD;
  876. return 0;
  877. }
  878. /* On the RTL93xx, an L3 termination endpoint MAC address on which the router waits
  879. * for packets to be routed needs to be allocated.
  880. */
  881. static int rtl83xx_alloc_router_mac(struct rtl838x_switch_priv *priv, u64 mac)
  882. {
  883. int free_mac = -1;
  884. struct rtl93xx_rt_mac m;
  885. mutex_lock(&priv->reg_mutex);
  886. for (int i = 0; i < MAX_ROUTER_MACS; i++) {
  887. priv->r->get_l3_router_mac(i, &m);
  888. if (free_mac < 0 && !m.valid) {
  889. free_mac = i;
  890. continue;
  891. }
  892. if (m.valid && m.mac == mac) {
  893. free_mac = i;
  894. break;
  895. }
  896. }
  897. if (free_mac < 0) {
  898. pr_err("No free router MACs, cannot offload\n");
  899. mutex_unlock(&priv->reg_mutex);
  900. return -1;
  901. }
  902. m.valid = true;
  903. m.mac = mac;
  904. m.p_type = 0; /* An individual port, not a trunk port */
  905. m.p_id = 0x3f; /* Listen on any port */
  906. m.p_id_mask = 0;
  907. m.vid = 0; /* Listen on any VLAN... */
  908. m.vid_mask = 0; /* ... so mask needs to be 0 */
  909. m.mac_mask = 0xffffffffffffULL; /* We want an exact match of the interface MAC */
  910. m.action = L3_FORWARD; /* Route the packet */
  911. priv->r->set_l3_router_mac(free_mac, &m);
  912. mutex_unlock(&priv->reg_mutex);
  913. return 0;
  914. }
  915. static int rtl83xx_alloc_egress_intf(struct rtl838x_switch_priv *priv, u64 mac, int vlan)
  916. {
  917. int free_mac = -1;
  918. struct rtl838x_l3_intf intf;
  919. u64 m;
  920. mutex_lock(&priv->reg_mutex);
  921. for (int i = 0; i < MAX_SMACS; i++) {
  922. m = priv->r->get_l3_egress_mac(L3_EGRESS_DMACS + i);
  923. if (free_mac < 0 && !m) {
  924. free_mac = i;
  925. continue;
  926. }
  927. if (m == mac) {
  928. mutex_unlock(&priv->reg_mutex);
  929. return i;
  930. }
  931. }
  932. if (free_mac < 0) {
  933. pr_err("No free egress interface, cannot offload\n");
  934. return -1;
  935. }
  936. /* Set up default egress interface 1 */
  937. intf.vid = vlan;
  938. intf.smac_idx = free_mac;
  939. intf.ip4_mtu_id = 1;
  940. intf.ip6_mtu_id = 1;
  941. intf.ttl_scope = 1; /* TTL */
  942. intf.hl_scope = 1; /* Hop Limit */
  943. intf.ip4_icmp_redirect = intf.ip6_icmp_redirect = 2; /* FORWARD */
  944. intf.ip4_pbr_icmp_redirect = intf.ip6_pbr_icmp_redirect = 2; /* FORWARD; */
  945. priv->r->set_l3_egress_intf(free_mac, &intf);
  946. priv->r->set_l3_egress_mac(L3_EGRESS_DMACS + free_mac, mac);
  947. mutex_unlock(&priv->reg_mutex);
  948. return free_mac;
  949. }
  950. static int rtl83xx_fib4_add(struct rtl838x_switch_priv *priv,
  951. struct fib_entry_notifier_info *info)
  952. {
  953. struct fib_nh *nh = fib_info_nh(info->fi, 0);
  954. struct net_device *dev = fib_info_nh(info->fi, 0)->fib_nh_dev;
  955. int port;
  956. struct rtl83xx_route *r;
  957. bool to_localhost;
  958. int vlan = is_vlan_dev(dev) ? vlan_dev_vlan_id(dev) : 0;
  959. pr_debug("In %s, ip %pI4, len %d\n", __func__, &info->dst, info->dst_len);
  960. if (!info->dst) {
  961. pr_info("Not offloading default route for now\n");
  962. return 0;
  963. }
  964. pr_debug("GW: %pI4, interface name %s, mac %016llx, vlan %d\n", &nh->fib_nh_gw4, dev->name,
  965. ether_addr_to_u64(dev->dev_addr), vlan
  966. );
  967. port = rtl83xx_port_dev_lower_find(dev, priv);
  968. if (port < 0)
  969. return -1;
  970. /* For now we only work with routes that have a gateway and are not ourself */
  971. /* if ((!nh->fib_nh_gw4) && (info->dst_len != 32)) */
  972. /* return 0; */
  973. if ((info->dst & 0xff) == 0xff)
  974. return 0;
  975. /* Do not offload routes to 192.168.100.x */
  976. if ((info->dst & 0xffffff00) == 0xc0a86400)
  977. return 0;
  978. /* Do not offload routes to 127.x.x.x */
  979. if ((info->dst & 0xff000000) == 0x7f000000)
  980. return 0;
  981. /* Allocate route or host-route (entry if hardware supports this) */
  982. if (info->dst_len == 32 && priv->r->host_route_write)
  983. r = rtl83xx_host_route_alloc(priv, nh->fib_nh_gw4);
  984. else
  985. r = rtl83xx_route_alloc(priv, nh->fib_nh_gw4);
  986. if (!r) {
  987. pr_err("%s: No more free route entries\n", __func__);
  988. return -1;
  989. }
  990. r->dst_ip = info->dst;
  991. r->prefix_len = info->dst_len;
  992. r->nh.rvid = vlan;
  993. to_localhost = !nh->fib_nh_gw4;
  994. if (priv->r->set_l3_router_mac) {
  995. u64 mac = ether_addr_to_u64(dev->dev_addr);
  996. pr_debug("Local route and router mac %016llx\n", mac);
  997. if (rtl83xx_alloc_router_mac(priv, mac))
  998. goto out_free_rt;
  999. /* vid = 0: Do not care about VID */
  1000. r->nh.if_id = rtl83xx_alloc_egress_intf(priv, mac, vlan);
  1001. if (r->nh.if_id < 0)
  1002. goto out_free_rmac;
  1003. if (to_localhost) {
  1004. int slot;
  1005. r->nh.mac = mac;
  1006. r->nh.port = priv->port_ignore;
  1007. r->attr.valid = true;
  1008. r->attr.action = ROUTE_ACT_TRAP2CPU;
  1009. r->attr.type = 0;
  1010. slot = priv->r->find_l3_slot(r, false);
  1011. pr_debug("%s: Got slot for route: %d\n", __func__, slot);
  1012. priv->r->host_route_write(slot, r);
  1013. }
  1014. }
  1015. /* We need to resolve the mac address of the GW */
  1016. if (!to_localhost)
  1017. rtl83xx_port_ipv4_resolve(priv, dev, nh->fib_nh_gw4);
  1018. nh->fib_nh_flags |= RTNH_F_OFFLOAD;
  1019. return 0;
  1020. out_free_rmac:
  1021. out_free_rt:
  1022. return 0;
  1023. }
  1024. static int rtl83xx_fib6_add(struct rtl838x_switch_priv *priv,
  1025. struct fib6_entry_notifier_info *info)
  1026. {
  1027. pr_debug("In %s\n", __func__);
  1028. /* nh->fib_nh_flags |= RTNH_F_OFFLOAD; */
  1029. return 0;
  1030. }
  1031. struct net_event_work {
  1032. struct work_struct work;
  1033. struct rtl838x_switch_priv *priv;
  1034. u64 mac;
  1035. u32 gw_addr;
  1036. };
  1037. static void rtl83xx_net_event_work_do(struct work_struct *work)
  1038. {
  1039. struct net_event_work *net_work =
  1040. container_of(work, struct net_event_work, work);
  1041. struct rtl838x_switch_priv *priv = net_work->priv;
  1042. rtl83xx_l3_nexthop_update(priv, net_work->gw_addr, net_work->mac);
  1043. kfree(net_work);
  1044. }
  1045. static int rtl83xx_netevent_event(struct notifier_block *this,
  1046. unsigned long event, void *ptr)
  1047. {
  1048. struct rtl838x_switch_priv *priv;
  1049. struct net_device *dev;
  1050. struct neighbour *n = ptr;
  1051. int err, port;
  1052. struct net_event_work *net_work;
  1053. priv = container_of(this, struct rtl838x_switch_priv, ne_nb);
  1054. switch (event) {
  1055. case NETEVENT_NEIGH_UPDATE:
  1056. if (n->tbl != &arp_tbl)
  1057. return NOTIFY_DONE;
  1058. dev = n->dev;
  1059. port = rtl83xx_port_dev_lower_find(dev, priv);
  1060. if (port < 0 || !(n->nud_state & NUD_VALID)) {
  1061. pr_debug("%s: Neigbour invalid, not updating\n", __func__);
  1062. return NOTIFY_DONE;
  1063. }
  1064. net_work = kzalloc(sizeof(*net_work), GFP_ATOMIC);
  1065. if (!net_work)
  1066. return NOTIFY_BAD;
  1067. INIT_WORK(&net_work->work, rtl83xx_net_event_work_do);
  1068. net_work->priv = priv;
  1069. net_work->mac = ether_addr_to_u64(n->ha);
  1070. net_work->gw_addr = *(__be32 *) n->primary_key;
  1071. pr_debug("%s: updating neighbour on port %d, mac %016llx\n",
  1072. __func__, port, net_work->mac);
  1073. schedule_work(&net_work->work);
  1074. if (err)
  1075. netdev_warn(dev, "failed to handle neigh update (err %d)\n", err);
  1076. break;
  1077. }
  1078. return NOTIFY_DONE;
  1079. }
  1080. struct rtl83xx_fib_event_work {
  1081. struct work_struct work;
  1082. union {
  1083. struct fib_entry_notifier_info fen_info;
  1084. struct fib6_entry_notifier_info fen6_info;
  1085. struct fib_rule_notifier_info fr_info;
  1086. };
  1087. struct rtl838x_switch_priv *priv;
  1088. bool is_fib6;
  1089. unsigned long event;
  1090. };
  1091. static void rtl83xx_fib_event_work_do(struct work_struct *work)
  1092. {
  1093. struct rtl83xx_fib_event_work *fib_work =
  1094. container_of(work, struct rtl83xx_fib_event_work, work);
  1095. struct rtl838x_switch_priv *priv = fib_work->priv;
  1096. struct fib_rule *rule;
  1097. int err;
  1098. /* Protect internal structures from changes */
  1099. rtnl_lock();
  1100. pr_debug("%s: doing work, event %ld\n", __func__, fib_work->event);
  1101. switch (fib_work->event) {
  1102. case FIB_EVENT_ENTRY_ADD:
  1103. case FIB_EVENT_ENTRY_REPLACE:
  1104. case FIB_EVENT_ENTRY_APPEND:
  1105. if (fib_work->is_fib6) {
  1106. err = rtl83xx_fib6_add(priv, &fib_work->fen6_info);
  1107. } else {
  1108. err = rtl83xx_fib4_add(priv, &fib_work->fen_info);
  1109. fib_info_put(fib_work->fen_info.fi);
  1110. }
  1111. if (err)
  1112. pr_err("%s: FIB4 failed\n", __func__);
  1113. break;
  1114. case FIB_EVENT_ENTRY_DEL:
  1115. rtl83xx_fib4_del(priv, &fib_work->fen_info);
  1116. fib_info_put(fib_work->fen_info.fi);
  1117. break;
  1118. case FIB_EVENT_RULE_ADD:
  1119. case FIB_EVENT_RULE_DEL:
  1120. rule = fib_work->fr_info.rule;
  1121. if (!fib4_rule_default(rule))
  1122. pr_err("%s: FIB4 default rule failed\n", __func__);
  1123. fib_rule_put(rule);
  1124. break;
  1125. }
  1126. rtnl_unlock();
  1127. kfree(fib_work);
  1128. }
  1129. /* Called with rcu_read_lock() */
  1130. static int rtl83xx_fib_event(struct notifier_block *this, unsigned long event, void *ptr)
  1131. {
  1132. struct fib_notifier_info *info = ptr;
  1133. struct rtl838x_switch_priv *priv;
  1134. struct rtl83xx_fib_event_work *fib_work;
  1135. if ((info->family != AF_INET && info->family != AF_INET6 &&
  1136. info->family != RTNL_FAMILY_IPMR &&
  1137. info->family != RTNL_FAMILY_IP6MR))
  1138. return NOTIFY_DONE;
  1139. priv = container_of(this, struct rtl838x_switch_priv, fib_nb);
  1140. fib_work = kzalloc(sizeof(*fib_work), GFP_ATOMIC);
  1141. if (!fib_work)
  1142. return NOTIFY_BAD;
  1143. INIT_WORK(&fib_work->work, rtl83xx_fib_event_work_do);
  1144. fib_work->priv = priv;
  1145. fib_work->event = event;
  1146. fib_work->is_fib6 = false;
  1147. switch (event) {
  1148. case FIB_EVENT_ENTRY_ADD:
  1149. case FIB_EVENT_ENTRY_REPLACE:
  1150. case FIB_EVENT_ENTRY_APPEND:
  1151. case FIB_EVENT_ENTRY_DEL:
  1152. pr_debug("%s: FIB_ENTRY ADD/DEL, event %ld\n", __func__, event);
  1153. if (info->family == AF_INET) {
  1154. struct fib_entry_notifier_info *fen_info = ptr;
  1155. if (fen_info->fi->fib_nh_is_v6) {
  1156. NL_SET_ERR_MSG_MOD(info->extack,
  1157. "IPv6 gateway with IPv4 route is not supported");
  1158. kfree(fib_work);
  1159. return notifier_from_errno(-EINVAL);
  1160. }
  1161. memcpy(&fib_work->fen_info, ptr, sizeof(fib_work->fen_info));
  1162. /* Take referece on fib_info to prevent it from being
  1163. * freed while work is queued. Release it afterwards.
  1164. */
  1165. fib_info_hold(fib_work->fen_info.fi);
  1166. } else if (info->family == AF_INET6) {
  1167. //struct fib6_entry_notifier_info *fen6_info = ptr;
  1168. pr_warn("%s: FIB_RULE ADD/DEL for IPv6 not supported\n", __func__);
  1169. kfree(fib_work);
  1170. return NOTIFY_DONE;
  1171. }
  1172. break;
  1173. case FIB_EVENT_RULE_ADD:
  1174. case FIB_EVENT_RULE_DEL:
  1175. pr_debug("%s: FIB_RULE ADD/DEL, event: %ld\n", __func__, event);
  1176. memcpy(&fib_work->fr_info, ptr, sizeof(fib_work->fr_info));
  1177. fib_rule_get(fib_work->fr_info.rule);
  1178. break;
  1179. }
  1180. schedule_work(&fib_work->work);
  1181. return NOTIFY_DONE;
  1182. }
  1183. static int __init rtl83xx_sw_probe(struct platform_device *pdev)
  1184. {
  1185. int err = 0;
  1186. struct rtl838x_switch_priv *priv;
  1187. struct device *dev = &pdev->dev;
  1188. u64 bpdu_mask;
  1189. pr_debug("Probing RTL838X switch device\n");
  1190. if (!pdev->dev.of_node) {
  1191. dev_err(dev, "No DT found\n");
  1192. return -EINVAL;
  1193. }
  1194. /* Initialize access to RTL switch tables */
  1195. rtl_table_init();
  1196. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  1197. if (!priv)
  1198. return -ENOMEM;
  1199. priv->ds = devm_kzalloc(dev, sizeof(*priv->ds), GFP_KERNEL);
  1200. if (!priv->ds)
  1201. return -ENOMEM;
  1202. priv->ds->dev = dev;
  1203. priv->ds->priv = priv;
  1204. priv->ds->ops = &rtl83xx_switch_ops;
  1205. priv->ds->needs_standalone_vlan_filtering = true;
  1206. priv->dev = dev;
  1207. mutex_init(&priv->reg_mutex);
  1208. priv->family_id = soc_info.family;
  1209. priv->id = soc_info.id;
  1210. switch(soc_info.family) {
  1211. case RTL8380_FAMILY_ID:
  1212. priv->ds->ops = &rtl83xx_switch_ops;
  1213. priv->cpu_port = RTL838X_CPU_PORT;
  1214. priv->port_mask = 0x1f;
  1215. priv->port_width = 1;
  1216. priv->irq_mask = 0x0FFFFFFF;
  1217. priv->r = &rtl838x_reg;
  1218. priv->ds->num_ports = 29;
  1219. priv->fib_entries = 8192;
  1220. rtl8380_get_version(priv);
  1221. priv->n_lags = 8;
  1222. priv->l2_bucket_size = 4;
  1223. priv->n_pie_blocks = 12;
  1224. priv->port_ignore = 0x1f;
  1225. priv->n_counters = 128;
  1226. break;
  1227. case RTL8390_FAMILY_ID:
  1228. priv->ds->ops = &rtl83xx_switch_ops;
  1229. priv->cpu_port = RTL839X_CPU_PORT;
  1230. priv->port_mask = 0x3f;
  1231. priv->port_width = 2;
  1232. priv->irq_mask = 0xFFFFFFFFFFFFFULL;
  1233. priv->r = &rtl839x_reg;
  1234. priv->ds->num_ports = 53;
  1235. priv->fib_entries = 16384;
  1236. rtl8390_get_version(priv);
  1237. priv->n_lags = 16;
  1238. priv->l2_bucket_size = 4;
  1239. priv->n_pie_blocks = 18;
  1240. priv->port_ignore = 0x3f;
  1241. priv->n_counters = 1024;
  1242. break;
  1243. case RTL9300_FAMILY_ID:
  1244. priv->ds->ops = &rtl930x_switch_ops;
  1245. priv->cpu_port = RTL930X_CPU_PORT;
  1246. priv->port_mask = 0x1f;
  1247. priv->port_width = 1;
  1248. priv->irq_mask = 0x0FFFFFFF;
  1249. priv->r = &rtl930x_reg;
  1250. priv->ds->num_ports = 29;
  1251. priv->fib_entries = 16384;
  1252. priv->version = RTL8390_VERSION_A;
  1253. priv->n_lags = 16;
  1254. sw_w32(1, RTL930X_ST_CTRL);
  1255. priv->l2_bucket_size = 8;
  1256. priv->n_pie_blocks = 16;
  1257. priv->port_ignore = 0x3f;
  1258. priv->n_counters = 2048;
  1259. break;
  1260. case RTL9310_FAMILY_ID:
  1261. priv->ds->ops = &rtl930x_switch_ops;
  1262. priv->cpu_port = RTL931X_CPU_PORT;
  1263. priv->port_mask = 0x3f;
  1264. priv->port_width = 2;
  1265. priv->irq_mask = 0xFFFFFFFFFFFFFULL;
  1266. priv->r = &rtl931x_reg;
  1267. priv->ds->num_ports = 57;
  1268. priv->fib_entries = 16384;
  1269. priv->version = RTL8390_VERSION_A;
  1270. priv->n_lags = 16;
  1271. priv->l2_bucket_size = 8;
  1272. break;
  1273. }
  1274. pr_debug("Chip version %c\n", priv->version);
  1275. err = rtl83xx_mdio_probe(priv);
  1276. if (err) {
  1277. /* Probing fails the 1st time because of missing ethernet driver
  1278. * initialization. Use this to disable traffic in case the bootloader left if on
  1279. */
  1280. return err;
  1281. }
  1282. err = dsa_register_switch(priv->ds);
  1283. if (err) {
  1284. dev_err(dev, "Error registering switch: %d\n", err);
  1285. return err;
  1286. }
  1287. /* dsa_to_port returns dsa_port from the port list in
  1288. * dsa_switch_tree, the tree is built when the switch
  1289. * is registered by dsa_register_switch
  1290. */
  1291. for (int i = 0; i <= priv->cpu_port; i++)
  1292. priv->ports[i].dp = dsa_to_port(priv->ds, i);
  1293. /* Enable link and media change interrupts. Are the SERDES masks needed? */
  1294. sw_w32_mask(0, 3, priv->r->isr_glb_src);
  1295. priv->r->set_port_reg_le(priv->irq_mask, priv->r->isr_port_link_sts_chg);
  1296. priv->r->set_port_reg_le(priv->irq_mask, priv->r->imr_port_link_sts_chg);
  1297. priv->link_state_irq = platform_get_irq(pdev, 0);
  1298. pr_info("LINK state irq: %d\n", priv->link_state_irq);
  1299. switch (priv->family_id) {
  1300. case RTL8380_FAMILY_ID:
  1301. err = request_irq(priv->link_state_irq, rtl838x_switch_irq,
  1302. IRQF_SHARED, "rtl838x-link-state", priv->ds);
  1303. break;
  1304. case RTL8390_FAMILY_ID:
  1305. err = request_irq(priv->link_state_irq, rtl839x_switch_irq,
  1306. IRQF_SHARED, "rtl839x-link-state", priv->ds);
  1307. break;
  1308. case RTL9300_FAMILY_ID:
  1309. err = request_irq(priv->link_state_irq, rtl930x_switch_irq,
  1310. IRQF_SHARED, "rtl930x-link-state", priv->ds);
  1311. break;
  1312. case RTL9310_FAMILY_ID:
  1313. err = request_irq(priv->link_state_irq, rtl931x_switch_irq,
  1314. IRQF_SHARED, "rtl931x-link-state", priv->ds);
  1315. break;
  1316. }
  1317. if (err) {
  1318. dev_err(dev, "Error setting up switch interrupt.\n");
  1319. /* Need to free allocated switch here */
  1320. }
  1321. /* Enable interrupts for switch, on RTL931x, the IRQ is always on globally */
  1322. if (soc_info.family != RTL9310_FAMILY_ID)
  1323. sw_w32(0x1, priv->r->imr_glb);
  1324. rtl83xx_get_l2aging(priv);
  1325. rtl83xx_setup_qos(priv);
  1326. priv->r->l3_setup(priv);
  1327. /* Clear all destination ports for mirror groups */
  1328. for (int i = 0; i < 4; i++)
  1329. priv->mirror_group_ports[i] = -1;
  1330. /* Register netdevice event callback to catch changes in link aggregation groups */
  1331. priv->nb.notifier_call = rtl83xx_netdevice_event;
  1332. if (register_netdevice_notifier(&priv->nb)) {
  1333. priv->nb.notifier_call = NULL;
  1334. dev_err(dev, "Failed to register LAG netdev notifier\n");
  1335. goto err_register_nb;
  1336. }
  1337. /* Initialize hash table for L3 routing */
  1338. rhltable_init(&priv->routes, &route_ht_params);
  1339. /* Register netevent notifier callback to catch notifications about neighboring
  1340. * changes to update nexthop entries for L3 routing.
  1341. */
  1342. priv->ne_nb.notifier_call = rtl83xx_netevent_event;
  1343. if (register_netevent_notifier(&priv->ne_nb)) {
  1344. priv->ne_nb.notifier_call = NULL;
  1345. dev_err(dev, "Failed to register netevent notifier\n");
  1346. goto err_register_ne_nb;
  1347. }
  1348. priv->fib_nb.notifier_call = rtl83xx_fib_event;
  1349. /* Register Forwarding Information Base notifier to offload routes where
  1350. * where possible
  1351. * Only FIBs pointing to our own netdevs are programmed into
  1352. * the device, so no need to pass a callback.
  1353. */
  1354. err = register_fib_notifier(&init_net, &priv->fib_nb, NULL, NULL);
  1355. if (err)
  1356. goto err_register_fib_nb;
  1357. /* TODO: put this into l2_setup() */
  1358. /* Flood BPDUs to all ports including cpu-port */
  1359. if (soc_info.family != RTL9300_FAMILY_ID) {
  1360. bpdu_mask = soc_info.family == RTL8380_FAMILY_ID ? 0x1FFFFFFF : 0x1FFFFFFFFFFFFF;
  1361. priv->r->set_port_reg_be(bpdu_mask, priv->r->rma_bpdu_fld_pmask);
  1362. /* TRAP 802.1X frames (EAPOL) to the CPU-Port, bypass STP and VLANs */
  1363. sw_w32(7, priv->r->spcl_trap_eapol_ctrl);
  1364. rtl838x_dbgfs_init(priv);
  1365. } else {
  1366. rtl930x_dbgfs_init(priv);
  1367. }
  1368. return 0;
  1369. err_register_fib_nb:
  1370. unregister_netevent_notifier(&priv->ne_nb);
  1371. err_register_ne_nb:
  1372. unregister_netdevice_notifier(&priv->nb);
  1373. err_register_nb:
  1374. return err;
  1375. }
  1376. static int rtl83xx_sw_remove(struct platform_device *pdev)
  1377. {
  1378. /* TODO: */
  1379. pr_debug("Removing platform driver for rtl83xx-sw\n");
  1380. return 0;
  1381. }
  1382. static const struct of_device_id rtl83xx_switch_of_ids[] = {
  1383. { .compatible = "realtek,rtl83xx-switch"},
  1384. { /* sentinel */ }
  1385. };
  1386. MODULE_DEVICE_TABLE(of, rtl83xx_switch_of_ids);
  1387. static struct platform_driver rtl83xx_switch_driver = {
  1388. .probe = rtl83xx_sw_probe,
  1389. .remove = rtl83xx_sw_remove,
  1390. .driver = {
  1391. .name = "rtl83xx-switch",
  1392. .pm = NULL,
  1393. .of_match_table = rtl83xx_switch_of_ids,
  1394. },
  1395. };
  1396. module_platform_driver(rtl83xx_switch_driver);
  1397. MODULE_AUTHOR("B. Koblitz");
  1398. MODULE_DESCRIPTION("RTL83XX SoC Switch Driver");
  1399. MODULE_LICENSE("GPL");