common.c 41 KB

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