common.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703
  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. // Currently Unused
  432. // /* Allocate a 64 bit octet counter located in the LOG HW table */
  433. // static int rtl83xx_octet_cntr_alloc(struct rtl838x_switch_priv *priv)
  434. // {
  435. // int idx;
  436. // mutex_lock(&priv->reg_mutex);
  437. // idx = find_first_zero_bit(priv->octet_cntr_use_bm, MAX_COUNTERS);
  438. // if (idx >= priv->n_counters) {
  439. // mutex_unlock(&priv->reg_mutex);
  440. // return -1;
  441. // }
  442. // set_bit(idx, priv->octet_cntr_use_bm);
  443. // mutex_unlock(&priv->reg_mutex);
  444. // return idx;
  445. // }
  446. /* Allocate a 32-bit packet counter
  447. * 2 32-bit packet counters share the location of a 64-bit octet counter
  448. * Initially there are no free packet counters and 2 new ones need to be freed
  449. * by allocating the corresponding octet counter
  450. */
  451. int rtl83xx_packet_cntr_alloc(struct rtl838x_switch_priv *priv)
  452. {
  453. int idx, j;
  454. mutex_lock(&priv->reg_mutex);
  455. /* Because initially no packet counters are free, the logic is reversed:
  456. * a 0-bit means the counter is already allocated (for octets)
  457. */
  458. idx = find_first_bit(priv->packet_cntr_use_bm, MAX_COUNTERS * 2);
  459. if (idx >= priv->n_counters * 2) {
  460. j = find_first_zero_bit(priv->octet_cntr_use_bm, MAX_COUNTERS);
  461. if (j >= priv->n_counters) {
  462. mutex_unlock(&priv->reg_mutex);
  463. return -1;
  464. }
  465. set_bit(j, priv->octet_cntr_use_bm);
  466. idx = j * 2;
  467. set_bit(j * 2 + 1, priv->packet_cntr_use_bm);
  468. } else {
  469. clear_bit(idx, priv->packet_cntr_use_bm);
  470. }
  471. mutex_unlock(&priv->reg_mutex);
  472. return idx;
  473. }
  474. /* Add an L2 nexthop entry for the L3 routing system / PIE forwarding in the SoC
  475. * Use VID and MAC in rtl838x_l2_entry to identify either a free slot in the L2 hash table
  476. * or mark an existing entry as a nexthop by setting it's nexthop bit
  477. * Called from the L3 layer
  478. * The index in the L2 hash table is filled into nh->l2_id;
  479. */
  480. int rtl83xx_l2_nexthop_add(struct rtl838x_switch_priv *priv, struct rtl83xx_nexthop *nh)
  481. {
  482. struct rtl838x_l2_entry e;
  483. u64 seed = priv->r->l2_hash_seed(nh->mac, nh->rvid);
  484. u32 key = priv->r->l2_hash_key(priv, seed);
  485. int idx = -1;
  486. u64 entry;
  487. pr_debug("%s searching for %08llx vid %d with key %d, seed: %016llx\n",
  488. __func__, nh->mac, nh->rvid, key, seed);
  489. e.type = L2_UNICAST;
  490. u64_to_ether_addr(nh->mac, &e.mac[0]);
  491. e.port = nh->port;
  492. /* Loop over all entries in the hash-bucket and over the second block on 93xx SoCs */
  493. for (int i = 0; i < priv->l2_bucket_size; i++) {
  494. entry = priv->r->read_l2_entry_using_hash(key, i, &e);
  495. if (!e.valid || ((entry & 0x0fffffffffffffffULL) == seed)) {
  496. idx = i > 3 ? ((key >> 14) & 0xffff) | i >> 1
  497. : ((key << 2) | i) & 0xffff;
  498. break;
  499. }
  500. }
  501. if (idx < 0) {
  502. pr_err("%s: No more L2 forwarding entries available\n", __func__);
  503. return -1;
  504. }
  505. /* Found an existing (e->valid is true) or empty entry, make it a nexthop entry */
  506. nh->l2_id = idx;
  507. if (e.valid) {
  508. nh->port = e.port;
  509. nh->vid = e.vid; /* Save VID */
  510. nh->rvid = e.rvid;
  511. nh->dev_id = e.stack_dev;
  512. /* If the entry is already a valid next hop entry, don't change it */
  513. if (e.next_hop)
  514. return 0;
  515. } else {
  516. e.valid = true;
  517. e.is_static = true;
  518. e.rvid = nh->rvid;
  519. e.is_ip_mc = false;
  520. e.is_ipv6_mc = false;
  521. e.block_da = false;
  522. e.block_sa = false;
  523. e.suspended = false;
  524. e.age = 0; /* With port-ignore */
  525. e.port = priv->port_ignore;
  526. u64_to_ether_addr(nh->mac, &e.mac[0]);
  527. }
  528. e.next_hop = true;
  529. e.nh_route_id = nh->id; /* NH route ID takes place of VID */
  530. e.nh_vlan_target = false;
  531. priv->r->write_l2_entry_using_hash(idx >> 2, idx & 0x3, &e);
  532. return 0;
  533. }
  534. /* Removes a Layer 2 next hop entry in the forwarding database
  535. * If it was static, the entire entry is removed, otherwise the nexthop bit is cleared
  536. * and we wait until the entry ages out
  537. */
  538. int rtl83xx_l2_nexthop_rm(struct rtl838x_switch_priv *priv, struct rtl83xx_nexthop *nh)
  539. {
  540. struct rtl838x_l2_entry e;
  541. u32 key = nh->l2_id >> 2;
  542. int i = nh->l2_id & 0x3;
  543. u64 entry = entry = priv->r->read_l2_entry_using_hash(key, i, &e);
  544. pr_debug("%s: id %d, key %d, index %d\n", __func__, nh->l2_id, key, i);
  545. if (!e.valid) {
  546. dev_err(priv->dev, "unknown nexthop, id %x\n", nh->l2_id);
  547. return -1;
  548. }
  549. if (e.is_static)
  550. e.valid = false;
  551. e.next_hop = false;
  552. e.vid = nh->vid; /* Restore VID */
  553. e.rvid = nh->rvid;
  554. priv->r->write_l2_entry_using_hash(key, i, &e);
  555. return 0;
  556. }
  557. static int rtl83xx_handle_changeupper(struct rtl838x_switch_priv *priv,
  558. struct net_device *ndev,
  559. struct netdev_notifier_changeupper_info *info)
  560. {
  561. struct net_device *upper = info->upper_dev;
  562. struct netdev_lag_upper_info *lag_upper_info = NULL;
  563. int i, j, err;
  564. if (!netif_is_lag_master(upper))
  565. return 0;
  566. mutex_lock(&priv->reg_mutex);
  567. for (i = 0; i < priv->n_lags; i++) {
  568. if ((!priv->lag_devs[i]) || (priv->lag_devs[i] == upper))
  569. break;
  570. }
  571. for (j = 0; j < priv->cpu_port; j++) {
  572. if (priv->ports[j].dp->slave == ndev)
  573. break;
  574. }
  575. if (j >= priv->cpu_port) {
  576. err = -EINVAL;
  577. goto out;
  578. }
  579. if (info->linking) {
  580. lag_upper_info = info->upper_info;
  581. if (!priv->lag_devs[i])
  582. priv->lag_devs[i] = upper;
  583. err = rtl83xx_lag_add(priv->ds, i, priv->ports[j].dp->index, lag_upper_info);
  584. if (err) {
  585. err = -EINVAL;
  586. goto out;
  587. }
  588. } else {
  589. if (!priv->lag_devs[i])
  590. err = -EINVAL;
  591. err = rtl83xx_lag_del(priv->ds, i, priv->ports[j].dp->index);
  592. if (err) {
  593. err = -EINVAL;
  594. goto out;
  595. }
  596. if (!priv->lags_port_members[i])
  597. priv->lag_devs[i] = NULL;
  598. }
  599. out:
  600. mutex_unlock(&priv->reg_mutex);
  601. return 0;
  602. }
  603. /* Is the lower network device a DSA slave network device of our RTL930X-switch?
  604. * Unfortunately we cannot just follow dev->dsa_prt as this is only set for the
  605. * DSA master device.
  606. */
  607. int rtl83xx_port_is_under(const struct net_device * dev, struct rtl838x_switch_priv *priv)
  608. {
  609. /* TODO: On 5.12:
  610. * if(!dsa_slave_dev_check(dev)) {
  611. * netdev_info(dev, "%s: not a DSA device.\n", __func__);
  612. * return -EINVAL;
  613. * }
  614. */
  615. for (int i = 0; i < priv->cpu_port; i++) {
  616. if (!priv->ports[i].dp)
  617. continue;
  618. if (priv->ports[i].dp->slave == dev)
  619. return i;
  620. }
  621. return -EINVAL;
  622. }
  623. static int rtl83xx_netdevice_event(struct notifier_block *this,
  624. unsigned long event, void *ptr)
  625. {
  626. struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
  627. struct rtl838x_switch_priv *priv;
  628. int err;
  629. pr_debug("In: %s, event: %lu\n", __func__, event);
  630. if ((event != NETDEV_CHANGEUPPER) && (event != NETDEV_CHANGELOWERSTATE))
  631. return NOTIFY_DONE;
  632. priv = container_of(this, struct rtl838x_switch_priv, nb);
  633. switch (event) {
  634. case NETDEV_CHANGEUPPER:
  635. err = rtl83xx_handle_changeupper(priv, ndev, ptr);
  636. break;
  637. }
  638. if (err)
  639. return err;
  640. return NOTIFY_DONE;
  641. }
  642. const static struct rhashtable_params route_ht_params = {
  643. .key_len = sizeof(u32),
  644. .key_offset = offsetof(struct rtl83xx_route, gw_ip),
  645. .head_offset = offsetof(struct rtl83xx_route, linkage),
  646. };
  647. /* Updates an L3 next hop entry in the ROUTING table */
  648. static int rtl83xx_l3_nexthop_update(struct rtl838x_switch_priv *priv, __be32 ip_addr, u64 mac)
  649. {
  650. struct rtl83xx_route *r;
  651. struct rhlist_head *tmp, *list;
  652. rcu_read_lock();
  653. list = rhltable_lookup(&priv->routes, &ip_addr, route_ht_params);
  654. if (!list) {
  655. rcu_read_unlock();
  656. return -ENOENT;
  657. }
  658. rhl_for_each_entry_rcu(r, tmp, list, linkage) {
  659. pr_info("%s: Setting up fwding: ip %pI4, GW mac %016llx\n",
  660. __func__, &ip_addr, mac);
  661. /* Reads the ROUTING table entry associated with the route */
  662. priv->r->route_read(r->id, r);
  663. pr_info("Route with id %d to %pI4 / %d\n", r->id, &r->dst_ip, r->prefix_len);
  664. r->nh.mac = r->nh.gw = mac;
  665. r->nh.port = priv->port_ignore;
  666. r->nh.id = r->id;
  667. /* Do we need to explicitly add a DMAC entry with the route's nh index? */
  668. if (priv->r->set_l3_egress_mac)
  669. priv->r->set_l3_egress_mac(r->id, mac);
  670. /* Update ROUTING table: map gateway-mac and switch-mac id to route id */
  671. rtl83xx_l2_nexthop_add(priv, &r->nh);
  672. r->attr.valid = true;
  673. r->attr.action = ROUTE_ACT_FORWARD;
  674. r->attr.type = 0;
  675. r->attr.hit = false; /* Reset route-used indicator */
  676. /* Add PIE entry with dst_ip and prefix_len */
  677. r->pr.dip = r->dst_ip;
  678. r->pr.dip_m = inet_make_mask(r->prefix_len);
  679. if (r->is_host_route) {
  680. int slot = priv->r->find_l3_slot(r, false);
  681. pr_info("%s: Got slot for route: %d\n", __func__, slot);
  682. priv->r->host_route_write(slot, r);
  683. } else {
  684. priv->r->route_write(r->id, r);
  685. r->pr.fwd_sel = true;
  686. r->pr.fwd_data = r->nh.l2_id;
  687. r->pr.fwd_act = PIE_ACT_ROUTE_UC;
  688. }
  689. if (priv->r->set_l3_nexthop)
  690. priv->r->set_l3_nexthop(r->nh.id, r->nh.l2_id, r->nh.if_id);
  691. if (r->pr.id < 0) {
  692. r->pr.packet_cntr = rtl83xx_packet_cntr_alloc(priv);
  693. if (r->pr.packet_cntr >= 0) {
  694. pr_info("Using packet counter %d\n", r->pr.packet_cntr);
  695. r->pr.log_sel = true;
  696. r->pr.log_data = r->pr.packet_cntr;
  697. }
  698. priv->r->pie_rule_add(priv, &r->pr);
  699. } else {
  700. int pkts = priv->r->packet_cntr_read(r->pr.packet_cntr);
  701. pr_info("%s: total packets: %d\n", __func__, pkts);
  702. priv->r->pie_rule_write(priv, r->pr.id, &r->pr);
  703. }
  704. }
  705. rcu_read_unlock();
  706. return 0;
  707. }
  708. static int rtl83xx_port_ipv4_resolve(struct rtl838x_switch_priv *priv,
  709. struct net_device *dev, __be32 ip_addr)
  710. {
  711. struct neighbour *n = neigh_lookup(&arp_tbl, &ip_addr, dev);
  712. int err = 0;
  713. u64 mac;
  714. if (!n) {
  715. n = neigh_create(&arp_tbl, &ip_addr, dev);
  716. if (IS_ERR(n))
  717. return PTR_ERR(n);
  718. }
  719. /* If the neigh is already resolved, then go ahead and
  720. * install the entry, otherwise start the ARP process to
  721. * resolve the neigh.
  722. */
  723. if (n->nud_state & NUD_VALID) {
  724. mac = ether_addr_to_u64(n->ha);
  725. pr_info("%s: resolved mac: %016llx\n", __func__, mac);
  726. rtl83xx_l3_nexthop_update(priv, ip_addr, mac);
  727. } else {
  728. pr_info("%s: need to wait\n", __func__);
  729. neigh_event_send(n, NULL);
  730. }
  731. neigh_release(n);
  732. return err;
  733. }
  734. struct rtl83xx_walk_data {
  735. struct rtl838x_switch_priv *priv;
  736. int port;
  737. };
  738. static int rtl83xx_port_lower_walk(struct net_device *lower, struct netdev_nested_priv *_priv)
  739. {
  740. struct rtl83xx_walk_data *data = (struct rtl83xx_walk_data *)_priv->data;
  741. struct rtl838x_switch_priv *priv = data->priv;
  742. int ret = 0;
  743. int index;
  744. index = rtl83xx_port_is_under(lower, priv);
  745. data->port = index;
  746. if (index >= 0) {
  747. pr_debug("Found DSA-port, index %d\n", index);
  748. ret = 1;
  749. }
  750. return ret;
  751. }
  752. int rtl83xx_port_dev_lower_find(struct net_device *dev, struct rtl838x_switch_priv *priv)
  753. {
  754. struct rtl83xx_walk_data data;
  755. struct netdev_nested_priv _priv;
  756. data.priv = priv;
  757. data.port = 0;
  758. _priv.data = (void *)&data;
  759. netdev_walk_all_lower_dev(dev, rtl83xx_port_lower_walk, &_priv);
  760. return data.port;
  761. }
  762. static struct rtl83xx_route *rtl83xx_route_alloc(struct rtl838x_switch_priv *priv, u32 ip)
  763. {
  764. struct rtl83xx_route *r;
  765. int idx = 0, err;
  766. mutex_lock(&priv->reg_mutex);
  767. idx = find_first_zero_bit(priv->route_use_bm, MAX_ROUTES);
  768. pr_debug("%s id: %d, ip %pI4\n", __func__, idx, &ip);
  769. r = kzalloc(sizeof(*r), GFP_KERNEL);
  770. if (!r) {
  771. mutex_unlock(&priv->reg_mutex);
  772. return r;
  773. }
  774. r->id = idx;
  775. r->gw_ip = ip;
  776. r->pr.id = -1; /* We still need to allocate a rule in HW */
  777. r->is_host_route = false;
  778. err = rhltable_insert(&priv->routes, &r->linkage, route_ht_params);
  779. if (err) {
  780. pr_err("Could not insert new rule\n");
  781. mutex_unlock(&priv->reg_mutex);
  782. goto out_free;
  783. }
  784. set_bit(idx, priv->route_use_bm);
  785. mutex_unlock(&priv->reg_mutex);
  786. return r;
  787. out_free:
  788. kfree(r);
  789. return NULL;
  790. }
  791. static struct rtl83xx_route *rtl83xx_host_route_alloc(struct rtl838x_switch_priv *priv, u32 ip)
  792. {
  793. struct rtl83xx_route *r;
  794. int idx = 0, err;
  795. mutex_lock(&priv->reg_mutex);
  796. idx = find_first_zero_bit(priv->host_route_use_bm, MAX_HOST_ROUTES);
  797. pr_debug("%s id: %d, ip %pI4\n", __func__, idx, &ip);
  798. r = kzalloc(sizeof(*r), GFP_KERNEL);
  799. if (!r) {
  800. mutex_unlock(&priv->reg_mutex);
  801. return r;
  802. }
  803. /* We require a unique route ID irrespective of whether it is a prefix or host
  804. * route (on RTL93xx) as we use this ID to associate a DMAC and next-hop entry
  805. */
  806. r->id = idx + MAX_ROUTES;
  807. r->gw_ip = ip;
  808. r->pr.id = -1; /* We still need to allocate a rule in HW */
  809. r->is_host_route = true;
  810. err = rhltable_insert(&priv->routes, &r->linkage, route_ht_params);
  811. if (err) {
  812. pr_err("Could not insert new rule\n");
  813. mutex_unlock(&priv->reg_mutex);
  814. goto out_free;
  815. }
  816. set_bit(idx, priv->host_route_use_bm);
  817. mutex_unlock(&priv->reg_mutex);
  818. return r;
  819. out_free:
  820. kfree(r);
  821. return NULL;
  822. }
  823. static void rtl83xx_route_rm(struct rtl838x_switch_priv *priv, struct rtl83xx_route *r)
  824. {
  825. int id;
  826. if (rhltable_remove(&priv->routes, &r->linkage, route_ht_params))
  827. dev_warn(priv->dev, "Could not remove route\n");
  828. if (r->is_host_route) {
  829. id = priv->r->find_l3_slot(r, false);
  830. pr_debug("%s: Got id for host route: %d\n", __func__, id);
  831. r->attr.valid = false;
  832. priv->r->host_route_write(id, r);
  833. clear_bit(r->id - MAX_ROUTES, priv->host_route_use_bm);
  834. } else {
  835. /* If there is a HW representation of the route, delete it */
  836. if (priv->r->route_lookup_hw) {
  837. id = priv->r->route_lookup_hw(r);
  838. pr_info("%s: Got id for prefix route: %d\n", __func__, id);
  839. r->attr.valid = false;
  840. priv->r->route_write(id, r);
  841. }
  842. clear_bit(r->id, priv->route_use_bm);
  843. }
  844. kfree(r);
  845. }
  846. static int rtl83xx_fib4_del(struct rtl838x_switch_priv *priv,
  847. struct fib_entry_notifier_info *info)
  848. {
  849. struct fib_nh *nh = fib_info_nh(info->fi, 0);
  850. struct rtl83xx_route *r;
  851. struct rhlist_head *tmp, *list;
  852. pr_debug("In %s, ip %pI4, len %d\n", __func__, &info->dst, info->dst_len);
  853. rcu_read_lock();
  854. list = rhltable_lookup(&priv->routes, &nh->fib_nh_gw4, route_ht_params);
  855. if (!list) {
  856. rcu_read_unlock();
  857. pr_err("%s: no such gateway: %pI4\n", __func__, &nh->fib_nh_gw4);
  858. return -ENOENT;
  859. }
  860. rhl_for_each_entry_rcu(r, tmp, list, linkage) {
  861. if (r->dst_ip == info->dst && r->prefix_len == info->dst_len) {
  862. pr_info("%s: found a route with id %d, nh-id %d\n",
  863. __func__, r->id, r->nh.id);
  864. break;
  865. }
  866. }
  867. rcu_read_unlock();
  868. rtl83xx_l2_nexthop_rm(priv, &r->nh);
  869. pr_debug("%s: Releasing packet counter %d\n", __func__, r->pr.packet_cntr);
  870. set_bit(r->pr.packet_cntr, priv->packet_cntr_use_bm);
  871. priv->r->pie_rule_rm(priv, &r->pr);
  872. rtl83xx_route_rm(priv, r);
  873. nh->fib_nh_flags &= ~RTNH_F_OFFLOAD;
  874. return 0;
  875. }
  876. /* On the RTL93xx, an L3 termination endpoint MAC address on which the router waits
  877. * for packets to be routed needs to be allocated.
  878. */
  879. static int rtl83xx_alloc_router_mac(struct rtl838x_switch_priv *priv, u64 mac)
  880. {
  881. int free_mac = -1;
  882. struct rtl93xx_rt_mac m;
  883. mutex_lock(&priv->reg_mutex);
  884. for (int i = 0; i < MAX_ROUTER_MACS; i++) {
  885. priv->r->get_l3_router_mac(i, &m);
  886. if (free_mac < 0 && !m.valid) {
  887. free_mac = i;
  888. continue;
  889. }
  890. if (m.valid && m.mac == mac) {
  891. free_mac = i;
  892. break;
  893. }
  894. }
  895. if (free_mac < 0) {
  896. pr_err("No free router MACs, cannot offload\n");
  897. mutex_unlock(&priv->reg_mutex);
  898. return -1;
  899. }
  900. m.valid = true;
  901. m.mac = mac;
  902. m.p_type = 0; /* An individual port, not a trunk port */
  903. m.p_id = 0x3f; /* Listen on any port */
  904. m.p_id_mask = 0;
  905. m.vid = 0; /* Listen on any VLAN... */
  906. m.vid_mask = 0; /* ... so mask needs to be 0 */
  907. m.mac_mask = 0xffffffffffffULL; /* We want an exact match of the interface MAC */
  908. m.action = L3_FORWARD; /* Route the packet */
  909. priv->r->set_l3_router_mac(free_mac, &m);
  910. mutex_unlock(&priv->reg_mutex);
  911. return 0;
  912. }
  913. static int rtl83xx_alloc_egress_intf(struct rtl838x_switch_priv *priv, u64 mac, int vlan)
  914. {
  915. int free_mac = -1;
  916. struct rtl838x_l3_intf intf;
  917. u64 m;
  918. mutex_lock(&priv->reg_mutex);
  919. for (int i = 0; i < MAX_SMACS; i++) {
  920. m = priv->r->get_l3_egress_mac(L3_EGRESS_DMACS + i);
  921. if (free_mac < 0 && !m) {
  922. free_mac = i;
  923. continue;
  924. }
  925. if (m == mac) {
  926. mutex_unlock(&priv->reg_mutex);
  927. return i;
  928. }
  929. }
  930. if (free_mac < 0) {
  931. pr_err("No free egress interface, cannot offload\n");
  932. return -1;
  933. }
  934. /* Set up default egress interface 1 */
  935. intf.vid = vlan;
  936. intf.smac_idx = free_mac;
  937. intf.ip4_mtu_id = 1;
  938. intf.ip6_mtu_id = 1;
  939. intf.ttl_scope = 1; /* TTL */
  940. intf.hl_scope = 1; /* Hop Limit */
  941. intf.ip4_icmp_redirect = intf.ip6_icmp_redirect = 2; /* FORWARD */
  942. intf.ip4_pbr_icmp_redirect = intf.ip6_pbr_icmp_redirect = 2; /* FORWARD; */
  943. priv->r->set_l3_egress_intf(free_mac, &intf);
  944. priv->r->set_l3_egress_mac(L3_EGRESS_DMACS + free_mac, mac);
  945. mutex_unlock(&priv->reg_mutex);
  946. return free_mac;
  947. }
  948. static int rtl83xx_fib4_add(struct rtl838x_switch_priv *priv,
  949. struct fib_entry_notifier_info *info)
  950. {
  951. struct fib_nh *nh = fib_info_nh(info->fi, 0);
  952. struct net_device *dev = fib_info_nh(info->fi, 0)->fib_nh_dev;
  953. int port;
  954. struct rtl83xx_route *r;
  955. bool to_localhost;
  956. int vlan = is_vlan_dev(dev) ? vlan_dev_vlan_id(dev) : 0;
  957. pr_debug("In %s, ip %pI4, len %d\n", __func__, &info->dst, info->dst_len);
  958. if (!info->dst) {
  959. pr_info("Not offloading default route for now\n");
  960. return 0;
  961. }
  962. pr_debug("GW: %pI4, interface name %s, mac %016llx, vlan %d\n", &nh->fib_nh_gw4, dev->name,
  963. ether_addr_to_u64(dev->dev_addr), vlan
  964. );
  965. port = rtl83xx_port_dev_lower_find(dev, priv);
  966. if (port < 0)
  967. return -1;
  968. /* For now we only work with routes that have a gateway and are not ourself */
  969. /* if ((!nh->fib_nh_gw4) && (info->dst_len != 32)) */
  970. /* return 0; */
  971. if ((info->dst & 0xff) == 0xff)
  972. return 0;
  973. /* Do not offload routes to 192.168.100.x */
  974. if ((info->dst & 0xffffff00) == 0xc0a86400)
  975. return 0;
  976. /* Do not offload routes to 127.x.x.x */
  977. if ((info->dst & 0xff000000) == 0x7f000000)
  978. return 0;
  979. /* Allocate route or host-route (entry if hardware supports this) */
  980. if (info->dst_len == 32 && priv->r->host_route_write)
  981. r = rtl83xx_host_route_alloc(priv, nh->fib_nh_gw4);
  982. else
  983. r = rtl83xx_route_alloc(priv, nh->fib_nh_gw4);
  984. if (!r) {
  985. pr_err("%s: No more free route entries\n", __func__);
  986. return -1;
  987. }
  988. r->dst_ip = info->dst;
  989. r->prefix_len = info->dst_len;
  990. r->nh.rvid = vlan;
  991. to_localhost = !nh->fib_nh_gw4;
  992. if (priv->r->set_l3_router_mac) {
  993. u64 mac = ether_addr_to_u64(dev->dev_addr);
  994. pr_debug("Local route and router mac %016llx\n", mac);
  995. if (rtl83xx_alloc_router_mac(priv, mac))
  996. goto out_free_rt;
  997. /* vid = 0: Do not care about VID */
  998. r->nh.if_id = rtl83xx_alloc_egress_intf(priv, mac, vlan);
  999. if (r->nh.if_id < 0)
  1000. goto out_free_rmac;
  1001. if (to_localhost) {
  1002. int slot;
  1003. r->nh.mac = mac;
  1004. r->nh.port = priv->port_ignore;
  1005. r->attr.valid = true;
  1006. r->attr.action = ROUTE_ACT_TRAP2CPU;
  1007. r->attr.type = 0;
  1008. slot = priv->r->find_l3_slot(r, false);
  1009. pr_debug("%s: Got slot for route: %d\n", __func__, slot);
  1010. priv->r->host_route_write(slot, r);
  1011. }
  1012. }
  1013. /* We need to resolve the mac address of the GW */
  1014. if (!to_localhost)
  1015. rtl83xx_port_ipv4_resolve(priv, dev, nh->fib_nh_gw4);
  1016. nh->fib_nh_flags |= RTNH_F_OFFLOAD;
  1017. return 0;
  1018. out_free_rmac:
  1019. out_free_rt:
  1020. return 0;
  1021. }
  1022. static int rtl83xx_fib6_add(struct rtl838x_switch_priv *priv,
  1023. struct fib6_entry_notifier_info *info)
  1024. {
  1025. pr_debug("In %s\n", __func__);
  1026. /* nh->fib_nh_flags |= RTNH_F_OFFLOAD; */
  1027. return 0;
  1028. }
  1029. struct net_event_work {
  1030. struct work_struct work;
  1031. struct rtl838x_switch_priv *priv;
  1032. u64 mac;
  1033. u32 gw_addr;
  1034. };
  1035. static void rtl83xx_net_event_work_do(struct work_struct *work)
  1036. {
  1037. struct net_event_work *net_work =
  1038. container_of(work, struct net_event_work, work);
  1039. struct rtl838x_switch_priv *priv = net_work->priv;
  1040. rtl83xx_l3_nexthop_update(priv, net_work->gw_addr, net_work->mac);
  1041. kfree(net_work);
  1042. }
  1043. static int rtl83xx_netevent_event(struct notifier_block *this,
  1044. unsigned long event, void *ptr)
  1045. {
  1046. struct rtl838x_switch_priv *priv;
  1047. struct net_device *dev;
  1048. struct neighbour *n = ptr;
  1049. int err, port;
  1050. struct net_event_work *net_work;
  1051. priv = container_of(this, struct rtl838x_switch_priv, ne_nb);
  1052. switch (event) {
  1053. case NETEVENT_NEIGH_UPDATE:
  1054. if (n->tbl != &arp_tbl)
  1055. return NOTIFY_DONE;
  1056. dev = n->dev;
  1057. port = rtl83xx_port_dev_lower_find(dev, priv);
  1058. if (port < 0 || !(n->nud_state & NUD_VALID)) {
  1059. pr_debug("%s: Neigbour invalid, not updating\n", __func__);
  1060. return NOTIFY_DONE;
  1061. }
  1062. net_work = kzalloc(sizeof(*net_work), GFP_ATOMIC);
  1063. if (!net_work)
  1064. return NOTIFY_BAD;
  1065. INIT_WORK(&net_work->work, rtl83xx_net_event_work_do);
  1066. net_work->priv = priv;
  1067. net_work->mac = ether_addr_to_u64(n->ha);
  1068. net_work->gw_addr = *(__be32 *) n->primary_key;
  1069. pr_debug("%s: updating neighbour on port %d, mac %016llx\n",
  1070. __func__, port, net_work->mac);
  1071. schedule_work(&net_work->work);
  1072. if (err)
  1073. netdev_warn(dev, "failed to handle neigh update (err %d)\n", err);
  1074. break;
  1075. }
  1076. return NOTIFY_DONE;
  1077. }
  1078. struct rtl83xx_fib_event_work {
  1079. struct work_struct work;
  1080. union {
  1081. struct fib_entry_notifier_info fen_info;
  1082. struct fib6_entry_notifier_info fen6_info;
  1083. struct fib_rule_notifier_info fr_info;
  1084. };
  1085. struct rtl838x_switch_priv *priv;
  1086. bool is_fib6;
  1087. unsigned long event;
  1088. };
  1089. static void rtl83xx_fib_event_work_do(struct work_struct *work)
  1090. {
  1091. struct rtl83xx_fib_event_work *fib_work =
  1092. container_of(work, struct rtl83xx_fib_event_work, work);
  1093. struct rtl838x_switch_priv *priv = fib_work->priv;
  1094. struct fib_rule *rule;
  1095. int err;
  1096. /* Protect internal structures from changes */
  1097. rtnl_lock();
  1098. pr_debug("%s: doing work, event %ld\n", __func__, fib_work->event);
  1099. switch (fib_work->event) {
  1100. case FIB_EVENT_ENTRY_ADD:
  1101. case FIB_EVENT_ENTRY_REPLACE:
  1102. case FIB_EVENT_ENTRY_APPEND:
  1103. if (fib_work->is_fib6) {
  1104. err = rtl83xx_fib6_add(priv, &fib_work->fen6_info);
  1105. } else {
  1106. err = rtl83xx_fib4_add(priv, &fib_work->fen_info);
  1107. fib_info_put(fib_work->fen_info.fi);
  1108. }
  1109. if (err)
  1110. pr_err("%s: FIB4 failed\n", __func__);
  1111. break;
  1112. case FIB_EVENT_ENTRY_DEL:
  1113. rtl83xx_fib4_del(priv, &fib_work->fen_info);
  1114. fib_info_put(fib_work->fen_info.fi);
  1115. break;
  1116. case FIB_EVENT_RULE_ADD:
  1117. case FIB_EVENT_RULE_DEL:
  1118. rule = fib_work->fr_info.rule;
  1119. if (!fib4_rule_default(rule))
  1120. pr_err("%s: FIB4 default rule failed\n", __func__);
  1121. fib_rule_put(rule);
  1122. break;
  1123. }
  1124. rtnl_unlock();
  1125. kfree(fib_work);
  1126. }
  1127. /* Called with rcu_read_lock() */
  1128. static int rtl83xx_fib_event(struct notifier_block *this, unsigned long event, void *ptr)
  1129. {
  1130. struct fib_notifier_info *info = ptr;
  1131. struct rtl838x_switch_priv *priv;
  1132. struct rtl83xx_fib_event_work *fib_work;
  1133. if ((info->family != AF_INET && info->family != AF_INET6 &&
  1134. info->family != RTNL_FAMILY_IPMR &&
  1135. info->family != RTNL_FAMILY_IP6MR))
  1136. return NOTIFY_DONE;
  1137. priv = container_of(this, struct rtl838x_switch_priv, fib_nb);
  1138. fib_work = kzalloc(sizeof(*fib_work), GFP_ATOMIC);
  1139. if (!fib_work)
  1140. return NOTIFY_BAD;
  1141. INIT_WORK(&fib_work->work, rtl83xx_fib_event_work_do);
  1142. fib_work->priv = priv;
  1143. fib_work->event = event;
  1144. fib_work->is_fib6 = false;
  1145. switch (event) {
  1146. case FIB_EVENT_ENTRY_ADD:
  1147. case FIB_EVENT_ENTRY_REPLACE:
  1148. case FIB_EVENT_ENTRY_APPEND:
  1149. case FIB_EVENT_ENTRY_DEL:
  1150. pr_debug("%s: FIB_ENTRY ADD/DEL, event %ld\n", __func__, event);
  1151. if (info->family == AF_INET) {
  1152. struct fib_entry_notifier_info *fen_info = ptr;
  1153. if (fen_info->fi->fib_nh_is_v6) {
  1154. NL_SET_ERR_MSG_MOD(info->extack,
  1155. "IPv6 gateway with IPv4 route is not supported");
  1156. kfree(fib_work);
  1157. return notifier_from_errno(-EINVAL);
  1158. }
  1159. memcpy(&fib_work->fen_info, ptr, sizeof(fib_work->fen_info));
  1160. /* Take referece on fib_info to prevent it from being
  1161. * freed while work is queued. Release it afterwards.
  1162. */
  1163. fib_info_hold(fib_work->fen_info.fi);
  1164. } else if (info->family == AF_INET6) {
  1165. //struct fib6_entry_notifier_info *fen6_info = ptr;
  1166. pr_warn("%s: FIB_RULE ADD/DEL for IPv6 not supported\n", __func__);
  1167. kfree(fib_work);
  1168. return NOTIFY_DONE;
  1169. }
  1170. break;
  1171. case FIB_EVENT_RULE_ADD:
  1172. case FIB_EVENT_RULE_DEL:
  1173. pr_debug("%s: FIB_RULE ADD/DEL, event: %ld\n", __func__, event);
  1174. memcpy(&fib_work->fr_info, ptr, sizeof(fib_work->fr_info));
  1175. fib_rule_get(fib_work->fr_info.rule);
  1176. break;
  1177. }
  1178. schedule_work(&fib_work->work);
  1179. return NOTIFY_DONE;
  1180. }
  1181. static int __init rtl83xx_sw_probe(struct platform_device *pdev)
  1182. {
  1183. int err = 0;
  1184. struct rtl838x_switch_priv *priv;
  1185. struct device *dev = &pdev->dev;
  1186. u64 bpdu_mask;
  1187. pr_debug("Probing RTL838X switch device\n");
  1188. if (!pdev->dev.of_node) {
  1189. dev_err(dev, "No DT found\n");
  1190. return -EINVAL;
  1191. }
  1192. /* Initialize access to RTL switch tables */
  1193. rtl_table_init();
  1194. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  1195. if (!priv)
  1196. return -ENOMEM;
  1197. priv->ds = devm_kzalloc(dev, sizeof(*priv->ds), GFP_KERNEL);
  1198. if (!priv->ds)
  1199. return -ENOMEM;
  1200. priv->ds->dev = dev;
  1201. priv->ds->priv = priv;
  1202. priv->ds->ops = &rtl83xx_switch_ops;
  1203. priv->ds->needs_standalone_vlan_filtering = true;
  1204. priv->dev = dev;
  1205. mutex_init(&priv->reg_mutex);
  1206. priv->family_id = soc_info.family;
  1207. priv->id = soc_info.id;
  1208. switch(soc_info.family) {
  1209. case RTL8380_FAMILY_ID:
  1210. priv->ds->ops = &rtl83xx_switch_ops;
  1211. priv->cpu_port = RTL838X_CPU_PORT;
  1212. priv->port_mask = 0x1f;
  1213. priv->port_width = 1;
  1214. priv->irq_mask = 0x0FFFFFFF;
  1215. priv->r = &rtl838x_reg;
  1216. priv->ds->num_ports = 29;
  1217. priv->fib_entries = 8192;
  1218. rtl8380_get_version(priv);
  1219. priv->n_lags = 8;
  1220. priv->l2_bucket_size = 4;
  1221. priv->n_pie_blocks = 12;
  1222. priv->port_ignore = 0x1f;
  1223. priv->n_counters = 128;
  1224. break;
  1225. case RTL8390_FAMILY_ID:
  1226. priv->ds->ops = &rtl83xx_switch_ops;
  1227. priv->cpu_port = RTL839X_CPU_PORT;
  1228. priv->port_mask = 0x3f;
  1229. priv->port_width = 2;
  1230. priv->irq_mask = 0xFFFFFFFFFFFFFULL;
  1231. priv->r = &rtl839x_reg;
  1232. priv->ds->num_ports = 53;
  1233. priv->fib_entries = 16384;
  1234. rtl8390_get_version(priv);
  1235. priv->n_lags = 16;
  1236. priv->l2_bucket_size = 4;
  1237. priv->n_pie_blocks = 18;
  1238. priv->port_ignore = 0x3f;
  1239. priv->n_counters = 1024;
  1240. break;
  1241. case RTL9300_FAMILY_ID:
  1242. priv->ds->ops = &rtl930x_switch_ops;
  1243. priv->cpu_port = RTL930X_CPU_PORT;
  1244. priv->port_mask = 0x1f;
  1245. priv->port_width = 1;
  1246. priv->irq_mask = 0x0FFFFFFF;
  1247. priv->r = &rtl930x_reg;
  1248. priv->ds->num_ports = 29;
  1249. priv->fib_entries = 16384;
  1250. priv->version = RTL8390_VERSION_A;
  1251. priv->n_lags = 16;
  1252. sw_w32(1, RTL930X_ST_CTRL);
  1253. priv->l2_bucket_size = 8;
  1254. priv->n_pie_blocks = 16;
  1255. priv->port_ignore = 0x3f;
  1256. priv->n_counters = 2048;
  1257. break;
  1258. case RTL9310_FAMILY_ID:
  1259. priv->ds->ops = &rtl930x_switch_ops;
  1260. priv->cpu_port = RTL931X_CPU_PORT;
  1261. priv->port_mask = 0x3f;
  1262. priv->port_width = 2;
  1263. priv->irq_mask = 0xFFFFFFFFFFFFFULL;
  1264. priv->r = &rtl931x_reg;
  1265. priv->ds->num_ports = 57;
  1266. priv->fib_entries = 16384;
  1267. priv->version = RTL8390_VERSION_A;
  1268. priv->n_lags = 16;
  1269. priv->l2_bucket_size = 8;
  1270. break;
  1271. }
  1272. pr_debug("Chip version %c\n", priv->version);
  1273. err = rtl83xx_mdio_probe(priv);
  1274. if (err) {
  1275. /* Probing fails the 1st time because of missing ethernet driver
  1276. * initialization. Use this to disable traffic in case the bootloader left if on
  1277. */
  1278. return err;
  1279. }
  1280. err = dsa_register_switch(priv->ds);
  1281. if (err) {
  1282. dev_err(dev, "Error registering switch: %d\n", err);
  1283. return err;
  1284. }
  1285. /* dsa_to_port returns dsa_port from the port list in
  1286. * dsa_switch_tree, the tree is built when the switch
  1287. * is registered by dsa_register_switch
  1288. */
  1289. for (int i = 0; i <= priv->cpu_port; i++)
  1290. priv->ports[i].dp = dsa_to_port(priv->ds, i);
  1291. /* Enable link and media change interrupts. Are the SERDES masks needed? */
  1292. sw_w32_mask(0, 3, priv->r->isr_glb_src);
  1293. priv->r->set_port_reg_le(priv->irq_mask, priv->r->isr_port_link_sts_chg);
  1294. priv->r->set_port_reg_le(priv->irq_mask, priv->r->imr_port_link_sts_chg);
  1295. priv->link_state_irq = platform_get_irq(pdev, 0);
  1296. pr_info("LINK state irq: %d\n", priv->link_state_irq);
  1297. switch (priv->family_id) {
  1298. case RTL8380_FAMILY_ID:
  1299. err = request_irq(priv->link_state_irq, rtl838x_switch_irq,
  1300. IRQF_SHARED, "rtl838x-link-state", priv->ds);
  1301. break;
  1302. case RTL8390_FAMILY_ID:
  1303. err = request_irq(priv->link_state_irq, rtl839x_switch_irq,
  1304. IRQF_SHARED, "rtl839x-link-state", priv->ds);
  1305. break;
  1306. case RTL9300_FAMILY_ID:
  1307. err = request_irq(priv->link_state_irq, rtl930x_switch_irq,
  1308. IRQF_SHARED, "rtl930x-link-state", priv->ds);
  1309. break;
  1310. case RTL9310_FAMILY_ID:
  1311. err = request_irq(priv->link_state_irq, rtl931x_switch_irq,
  1312. IRQF_SHARED, "rtl931x-link-state", priv->ds);
  1313. break;
  1314. }
  1315. if (err) {
  1316. dev_err(dev, "Error setting up switch interrupt.\n");
  1317. /* Need to free allocated switch here */
  1318. }
  1319. /* Enable interrupts for switch, on RTL931x, the IRQ is always on globally */
  1320. if (soc_info.family != RTL9310_FAMILY_ID)
  1321. sw_w32(0x1, priv->r->imr_glb);
  1322. rtl83xx_get_l2aging(priv);
  1323. rtl83xx_setup_qos(priv);
  1324. priv->r->l3_setup(priv);
  1325. /* Clear all destination ports for mirror groups */
  1326. for (int i = 0; i < 4; i++)
  1327. priv->mirror_group_ports[i] = -1;
  1328. /* Register netdevice event callback to catch changes in link aggregation groups */
  1329. priv->nb.notifier_call = rtl83xx_netdevice_event;
  1330. if (register_netdevice_notifier(&priv->nb)) {
  1331. priv->nb.notifier_call = NULL;
  1332. dev_err(dev, "Failed to register LAG netdev notifier\n");
  1333. goto err_register_nb;
  1334. }
  1335. /* Initialize hash table for L3 routing */
  1336. rhltable_init(&priv->routes, &route_ht_params);
  1337. /* Register netevent notifier callback to catch notifications about neighboring
  1338. * changes to update nexthop entries for L3 routing.
  1339. */
  1340. priv->ne_nb.notifier_call = rtl83xx_netevent_event;
  1341. if (register_netevent_notifier(&priv->ne_nb)) {
  1342. priv->ne_nb.notifier_call = NULL;
  1343. dev_err(dev, "Failed to register netevent notifier\n");
  1344. goto err_register_ne_nb;
  1345. }
  1346. priv->fib_nb.notifier_call = rtl83xx_fib_event;
  1347. /* Register Forwarding Information Base notifier to offload routes where
  1348. * where possible
  1349. * Only FIBs pointing to our own netdevs are programmed into
  1350. * the device, so no need to pass a callback.
  1351. */
  1352. err = register_fib_notifier(&init_net, &priv->fib_nb, NULL, NULL);
  1353. if (err)
  1354. goto err_register_fib_nb;
  1355. /* TODO: put this into l2_setup() */
  1356. /* Flood BPDUs to all ports including cpu-port */
  1357. if (soc_info.family != RTL9300_FAMILY_ID) {
  1358. bpdu_mask = soc_info.family == RTL8380_FAMILY_ID ? 0x1FFFFFFF : 0x1FFFFFFFFFFFFF;
  1359. priv->r->set_port_reg_be(bpdu_mask, priv->r->rma_bpdu_fld_pmask);
  1360. /* TRAP 802.1X frames (EAPOL) to the CPU-Port, bypass STP and VLANs */
  1361. sw_w32(7, priv->r->spcl_trap_eapol_ctrl);
  1362. rtl838x_dbgfs_init(priv);
  1363. } else {
  1364. rtl930x_dbgfs_init(priv);
  1365. }
  1366. return 0;
  1367. err_register_fib_nb:
  1368. unregister_netevent_notifier(&priv->ne_nb);
  1369. err_register_ne_nb:
  1370. unregister_netdevice_notifier(&priv->nb);
  1371. err_register_nb:
  1372. return err;
  1373. }
  1374. static int rtl83xx_sw_remove(struct platform_device *pdev)
  1375. {
  1376. /* TODO: */
  1377. pr_debug("Removing platform driver for rtl83xx-sw\n");
  1378. return 0;
  1379. }
  1380. static const struct of_device_id rtl83xx_switch_of_ids[] = {
  1381. { .compatible = "realtek,rtl83xx-switch"},
  1382. { /* sentinel */ }
  1383. };
  1384. MODULE_DEVICE_TABLE(of, rtl83xx_switch_of_ids);
  1385. static struct platform_driver rtl83xx_switch_driver = {
  1386. .probe = rtl83xx_sw_probe,
  1387. .remove = rtl83xx_sw_remove,
  1388. .driver = {
  1389. .name = "rtl83xx-switch",
  1390. .pm = NULL,
  1391. .of_match_table = rtl83xx_switch_of_ids,
  1392. },
  1393. };
  1394. module_platform_driver(rtl83xx_switch_driver);
  1395. MODULE_AUTHOR("B. Koblitz");
  1396. MODULE_DESCRIPTION("RTL83XX SoC Switch Driver");
  1397. MODULE_LICENSE("GPL");