2
0

common.c 44 KB

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