common.c 44 KB

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