1 // SPDX-License-Identifier: GPL-2.0+
2 
3 #include <linux/if_bridge.h>
4 #include <net/switchdev.h>
5 
6 #include "lan966x_main.h"
7 
8 static struct notifier_block lan966x_netdevice_nb __read_mostly;
9 static struct notifier_block lan966x_switchdev_nb __read_mostly;
10 static struct notifier_block lan966x_switchdev_blocking_nb __read_mostly;
11 
12 static void lan966x_port_set_mcast_flood(struct lan966x_port *port,
13 					 bool enabled)
14 {
15 	u32 val = lan_rd(port->lan966x, ANA_PGID(PGID_MC));
16 
17 	val = ANA_PGID_PGID_GET(val);
18 	if (enabled)
19 		val |= BIT(port->chip_port);
20 	else
21 		val &= ~BIT(port->chip_port);
22 
23 	lan_rmw(ANA_PGID_PGID_SET(val),
24 		ANA_PGID_PGID,
25 		port->lan966x, ANA_PGID(PGID_MC));
26 }
27 
28 static void lan966x_port_bridge_flags(struct lan966x_port *port,
29 				      struct switchdev_brport_flags flags)
30 {
31 	if (flags.mask & BR_MCAST_FLOOD)
32 		lan966x_port_set_mcast_flood(port,
33 					     !!(flags.val & BR_MCAST_FLOOD));
34 }
35 
36 static int lan966x_port_pre_bridge_flags(struct lan966x_port *port,
37 					 struct switchdev_brport_flags flags)
38 {
39 	if (flags.mask & ~BR_MCAST_FLOOD)
40 		return -EINVAL;
41 
42 	return 0;
43 }
44 
45 static void lan966x_update_fwd_mask(struct lan966x *lan966x)
46 {
47 	int i;
48 
49 	for (i = 0; i < lan966x->num_phys_ports; i++) {
50 		struct lan966x_port *port = lan966x->ports[i];
51 		unsigned long mask = 0;
52 
53 		if (port && lan966x->bridge_fwd_mask & BIT(i))
54 			mask = lan966x->bridge_fwd_mask & ~BIT(i);
55 
56 		mask |= BIT(CPU_PORT);
57 
58 		lan_wr(ANA_PGID_PGID_SET(mask),
59 		       lan966x, ANA_PGID(PGID_SRC + i));
60 	}
61 }
62 
63 static void lan966x_port_stp_state_set(struct lan966x_port *port, u8 state)
64 {
65 	struct lan966x *lan966x = port->lan966x;
66 	bool learn_ena = false;
67 
68 	if (state == BR_STATE_FORWARDING || state == BR_STATE_LEARNING)
69 		learn_ena = true;
70 
71 	if (state == BR_STATE_FORWARDING)
72 		lan966x->bridge_fwd_mask |= BIT(port->chip_port);
73 	else
74 		lan966x->bridge_fwd_mask &= ~BIT(port->chip_port);
75 
76 	lan_rmw(ANA_PORT_CFG_LEARN_ENA_SET(learn_ena),
77 		ANA_PORT_CFG_LEARN_ENA,
78 		lan966x, ANA_PORT_CFG(port->chip_port));
79 
80 	lan966x_update_fwd_mask(lan966x);
81 }
82 
83 static void lan966x_port_ageing_set(struct lan966x_port *port,
84 				    unsigned long ageing_clock_t)
85 {
86 	unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock_t);
87 	u32 ageing_time = jiffies_to_msecs(ageing_jiffies) / 1000;
88 
89 	lan966x_mac_set_ageing(port->lan966x, ageing_time);
90 }
91 
92 static int lan966x_port_attr_set(struct net_device *dev, const void *ctx,
93 				 const struct switchdev_attr *attr,
94 				 struct netlink_ext_ack *extack)
95 {
96 	struct lan966x_port *port = netdev_priv(dev);
97 	int err = 0;
98 
99 	if (ctx && ctx != port)
100 		return 0;
101 
102 	switch (attr->id) {
103 	case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS:
104 		lan966x_port_bridge_flags(port, attr->u.brport_flags);
105 		break;
106 	case SWITCHDEV_ATTR_ID_PORT_PRE_BRIDGE_FLAGS:
107 		err = lan966x_port_pre_bridge_flags(port, attr->u.brport_flags);
108 		break;
109 	case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
110 		lan966x_port_stp_state_set(port, attr->u.stp_state);
111 		break;
112 	case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
113 		lan966x_port_ageing_set(port, attr->u.ageing_time);
114 		break;
115 	case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
116 		lan966x_vlan_port_set_vlan_aware(port, attr->u.vlan_filtering);
117 		lan966x_vlan_port_apply(port);
118 		break;
119 	default:
120 		err = -EOPNOTSUPP;
121 		break;
122 	}
123 
124 	return err;
125 }
126 
127 static int lan966x_port_bridge_join(struct lan966x_port *port,
128 				    struct net_device *bridge,
129 				    struct netlink_ext_ack *extack)
130 {
131 	struct lan966x *lan966x = port->lan966x;
132 	struct net_device *dev = port->dev;
133 	int err;
134 
135 	if (!lan966x->bridge_mask) {
136 		lan966x->bridge = bridge;
137 	} else {
138 		if (lan966x->bridge != bridge) {
139 			NL_SET_ERR_MSG_MOD(extack, "Not allow to add port to different bridge");
140 			return -ENODEV;
141 		}
142 	}
143 
144 	err = switchdev_bridge_port_offload(dev, dev, port,
145 					    &lan966x_switchdev_nb,
146 					    &lan966x_switchdev_blocking_nb,
147 					    false, extack);
148 	if (err)
149 		return err;
150 
151 	lan966x->bridge_mask |= BIT(port->chip_port);
152 
153 	return 0;
154 }
155 
156 static void lan966x_port_bridge_leave(struct lan966x_port *port,
157 				      struct net_device *bridge)
158 {
159 	struct lan966x *lan966x = port->lan966x;
160 
161 	lan966x->bridge_mask &= ~BIT(port->chip_port);
162 
163 	if (!lan966x->bridge_mask)
164 		lan966x->bridge = NULL;
165 
166 	/* Set the port back to host mode */
167 	lan966x_vlan_port_set_vlan_aware(port, false);
168 	lan966x_vlan_port_set_vid(port, HOST_PVID, false, false);
169 	lan966x_vlan_port_apply(port);
170 }
171 
172 static int lan966x_port_changeupper(struct net_device *dev,
173 				    struct netdev_notifier_changeupper_info *info)
174 {
175 	struct lan966x_port *port = netdev_priv(dev);
176 	struct netlink_ext_ack *extack;
177 	int err = 0;
178 
179 	extack = netdev_notifier_info_to_extack(&info->info);
180 
181 	if (netif_is_bridge_master(info->upper_dev)) {
182 		if (info->linking)
183 			err = lan966x_port_bridge_join(port, info->upper_dev,
184 						       extack);
185 		else
186 			lan966x_port_bridge_leave(port, info->upper_dev);
187 	}
188 
189 	return err;
190 }
191 
192 static int lan966x_port_prechangeupper(struct net_device *dev,
193 				       struct netdev_notifier_changeupper_info *info)
194 {
195 	struct lan966x_port *port = netdev_priv(dev);
196 
197 	if (netif_is_bridge_master(info->upper_dev) && !info->linking)
198 		switchdev_bridge_port_unoffload(port->dev, port,
199 						&lan966x_switchdev_nb,
200 						&lan966x_switchdev_blocking_nb);
201 
202 	return NOTIFY_DONE;
203 }
204 
205 static int lan966x_foreign_bridging_check(struct net_device *bridge,
206 					  struct netlink_ext_ack *extack)
207 {
208 	struct lan966x *lan966x = NULL;
209 	bool has_foreign = false;
210 	struct net_device *dev;
211 	struct list_head *iter;
212 
213 	if (!netif_is_bridge_master(bridge))
214 		return 0;
215 
216 	netdev_for_each_lower_dev(bridge, dev, iter) {
217 		if (lan966x_netdevice_check(dev)) {
218 			struct lan966x_port *port = netdev_priv(dev);
219 
220 			if (lan966x) {
221 				/* Bridge already has at least one port of a
222 				 * lan966x switch inside it, check that it's
223 				 * the same instance of the driver.
224 				 */
225 				if (port->lan966x != lan966x) {
226 					NL_SET_ERR_MSG_MOD(extack,
227 							   "Bridging between multiple lan966x switches disallowed");
228 					return -EINVAL;
229 				}
230 			} else {
231 				/* This is the first lan966x port inside this
232 				 * bridge
233 				 */
234 				lan966x = port->lan966x;
235 			}
236 		} else {
237 			has_foreign = true;
238 		}
239 
240 		if (lan966x && has_foreign) {
241 			NL_SET_ERR_MSG_MOD(extack,
242 					   "Bridging lan966x ports with foreign interfaces disallowed");
243 			return -EINVAL;
244 		}
245 	}
246 
247 	return 0;
248 }
249 
250 static int lan966x_bridge_check(struct net_device *dev,
251 				struct netdev_notifier_changeupper_info *info)
252 {
253 	return lan966x_foreign_bridging_check(info->upper_dev,
254 					      info->info.extack);
255 }
256 
257 static int lan966x_netdevice_port_event(struct net_device *dev,
258 					struct notifier_block *nb,
259 					unsigned long event, void *ptr)
260 {
261 	int err = 0;
262 
263 	if (!lan966x_netdevice_check(dev)) {
264 		if (event == NETDEV_CHANGEUPPER)
265 			return lan966x_bridge_check(dev, ptr);
266 		return 0;
267 	}
268 
269 	switch (event) {
270 	case NETDEV_PRECHANGEUPPER:
271 		err = lan966x_port_prechangeupper(dev, ptr);
272 		break;
273 	case NETDEV_CHANGEUPPER:
274 		err = lan966x_bridge_check(dev, ptr);
275 		if (err)
276 			return err;
277 
278 		err = lan966x_port_changeupper(dev, ptr);
279 		break;
280 	}
281 
282 	return err;
283 }
284 
285 static int lan966x_netdevice_event(struct notifier_block *nb,
286 				   unsigned long event, void *ptr)
287 {
288 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
289 	int ret;
290 
291 	ret = lan966x_netdevice_port_event(dev, nb, event, ptr);
292 
293 	return notifier_from_errno(ret);
294 }
295 
296 static bool lan966x_foreign_dev_check(const struct net_device *dev,
297 				      const struct net_device *foreign_dev)
298 {
299 	struct lan966x_port *port = netdev_priv(dev);
300 	struct lan966x *lan966x = port->lan966x;
301 
302 	if (netif_is_bridge_master(foreign_dev))
303 		if (lan966x->bridge != foreign_dev)
304 			return true;
305 
306 	return false;
307 }
308 
309 static int lan966x_switchdev_event(struct notifier_block *nb,
310 				   unsigned long event, void *ptr)
311 {
312 	struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
313 	int err;
314 
315 	switch (event) {
316 	case SWITCHDEV_PORT_ATTR_SET:
317 		err = switchdev_handle_port_attr_set(dev, ptr,
318 						     lan966x_netdevice_check,
319 						     lan966x_port_attr_set);
320 		return notifier_from_errno(err);
321 	case SWITCHDEV_FDB_ADD_TO_DEVICE:
322 	case SWITCHDEV_FDB_DEL_TO_DEVICE:
323 		err = switchdev_handle_fdb_event_to_device(dev, event, ptr,
324 							   lan966x_netdevice_check,
325 							   lan966x_foreign_dev_check,
326 							   lan966x_handle_fdb,
327 							   NULL);
328 		return notifier_from_errno(err);
329 	}
330 
331 	return NOTIFY_DONE;
332 }
333 
334 static int lan966x_handle_port_vlan_add(struct lan966x_port *port,
335 					const struct switchdev_obj *obj)
336 {
337 	const struct switchdev_obj_port_vlan *v = SWITCHDEV_OBJ_PORT_VLAN(obj);
338 	struct lan966x *lan966x = port->lan966x;
339 
340 	/* When adding a port to a vlan, we get a callback for the port but
341 	 * also for the bridge. When get the callback for the bridge just bail
342 	 * out. Then when the bridge is added to the vlan, then we get a
343 	 * callback here but in this case the flags has set:
344 	 * BRIDGE_VLAN_INFO_BRENTRY. In this case it means that the CPU
345 	 * port is added to the vlan, so the broadcast frames and unicast frames
346 	 * with dmac of the bridge should be foward to CPU.
347 	 */
348 	if (netif_is_bridge_master(obj->orig_dev) &&
349 	    !(v->flags & BRIDGE_VLAN_INFO_BRENTRY))
350 		return 0;
351 
352 	if (!netif_is_bridge_master(obj->orig_dev))
353 		lan966x_vlan_port_add_vlan(port, v->vid,
354 					   v->flags & BRIDGE_VLAN_INFO_PVID,
355 					   v->flags & BRIDGE_VLAN_INFO_UNTAGGED);
356 	else
357 		lan966x_vlan_cpu_add_vlan(lan966x, v->vid);
358 
359 	return 0;
360 }
361 
362 static int lan966x_handle_port_obj_add(struct net_device *dev, const void *ctx,
363 				       const struct switchdev_obj *obj,
364 				       struct netlink_ext_ack *extack)
365 {
366 	struct lan966x_port *port = netdev_priv(dev);
367 	int err;
368 
369 	if (ctx && ctx != port)
370 		return 0;
371 
372 	switch (obj->id) {
373 	case SWITCHDEV_OBJ_ID_PORT_VLAN:
374 		err = lan966x_handle_port_vlan_add(port, obj);
375 		break;
376 	default:
377 		err = -EOPNOTSUPP;
378 		break;
379 	}
380 
381 	return err;
382 }
383 
384 static int lan966x_handle_port_vlan_del(struct lan966x_port *port,
385 					const struct switchdev_obj *obj)
386 {
387 	const struct switchdev_obj_port_vlan *v = SWITCHDEV_OBJ_PORT_VLAN(obj);
388 	struct lan966x *lan966x = port->lan966x;
389 
390 	if (!netif_is_bridge_master(obj->orig_dev))
391 		lan966x_vlan_port_del_vlan(port, v->vid);
392 	else
393 		lan966x_vlan_cpu_del_vlan(lan966x, v->vid);
394 
395 	return 0;
396 }
397 
398 static int lan966x_handle_port_obj_del(struct net_device *dev, const void *ctx,
399 				       const struct switchdev_obj *obj)
400 {
401 	struct lan966x_port *port = netdev_priv(dev);
402 	int err;
403 
404 	if (ctx && ctx != port)
405 		return 0;
406 
407 	switch (obj->id) {
408 	case SWITCHDEV_OBJ_ID_PORT_VLAN:
409 		err = lan966x_handle_port_vlan_del(port, obj);
410 		break;
411 	default:
412 		err = -EOPNOTSUPP;
413 		break;
414 	}
415 
416 	return err;
417 }
418 
419 static int lan966x_switchdev_blocking_event(struct notifier_block *nb,
420 					    unsigned long event,
421 					    void *ptr)
422 {
423 	struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
424 	int err;
425 
426 	switch (event) {
427 	case SWITCHDEV_PORT_OBJ_ADD:
428 		err = switchdev_handle_port_obj_add(dev, ptr,
429 						    lan966x_netdevice_check,
430 						    lan966x_handle_port_obj_add);
431 		return notifier_from_errno(err);
432 	case SWITCHDEV_PORT_OBJ_DEL:
433 		err = switchdev_handle_port_obj_del(dev, ptr,
434 						    lan966x_netdevice_check,
435 						    lan966x_handle_port_obj_del);
436 		return notifier_from_errno(err);
437 	case SWITCHDEV_PORT_ATTR_SET:
438 		err = switchdev_handle_port_attr_set(dev, ptr,
439 						     lan966x_netdevice_check,
440 						     lan966x_port_attr_set);
441 		return notifier_from_errno(err);
442 	}
443 
444 	return NOTIFY_DONE;
445 }
446 
447 static struct notifier_block lan966x_netdevice_nb __read_mostly = {
448 	.notifier_call = lan966x_netdevice_event,
449 };
450 
451 static struct notifier_block lan966x_switchdev_nb __read_mostly = {
452 	.notifier_call = lan966x_switchdev_event,
453 };
454 
455 static struct notifier_block lan966x_switchdev_blocking_nb __read_mostly = {
456 	.notifier_call = lan966x_switchdev_blocking_event,
457 };
458 
459 void lan966x_register_notifier_blocks(void)
460 {
461 	register_netdevice_notifier(&lan966x_netdevice_nb);
462 	register_switchdev_notifier(&lan966x_switchdev_nb);
463 	register_switchdev_blocking_notifier(&lan966x_switchdev_blocking_nb);
464 }
465 
466 void lan966x_unregister_notifier_blocks(void)
467 {
468 	unregister_switchdev_blocking_notifier(&lan966x_switchdev_blocking_nb);
469 	unregister_switchdev_notifier(&lan966x_switchdev_nb);
470 	unregister_netdevice_notifier(&lan966x_netdevice_nb);
471 }
472