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