1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * net/dsa/dsa.c - Hardware switch handling
4 * Copyright (c) 2008-2009 Marvell Semiconductor
5 * Copyright (c) 2013 Florian Fainelli <florian@openwrt.org>
8 #include <linux/device.h>
9 #include <linux/list.h>
10 #include <linux/module.h>
11 #include <linux/netdevice.h>
12 #include <linux/sysfs.h>
13 #include <linux/ptp_classify.h>
17 static LIST_HEAD(dsa_tag_drivers_list);
18 static DEFINE_MUTEX(dsa_tag_drivers_lock);
20 static struct sk_buff *dsa_slave_notag_xmit(struct sk_buff *skb,
21 struct net_device *dev)
23 /* Just return the original SKB */
27 static const struct dsa_device_ops none_ops = {
29 .proto = DSA_TAG_PROTO_NONE,
30 .xmit = dsa_slave_notag_xmit,
34 DSA_TAG_DRIVER(none_ops);
36 static void dsa_tag_driver_register(struct dsa_tag_driver *dsa_tag_driver,
39 dsa_tag_driver->owner = owner;
41 mutex_lock(&dsa_tag_drivers_lock);
42 list_add_tail(&dsa_tag_driver->list, &dsa_tag_drivers_list);
43 mutex_unlock(&dsa_tag_drivers_lock);
46 void dsa_tag_drivers_register(struct dsa_tag_driver *dsa_tag_driver_array[],
47 unsigned int count, struct module *owner)
51 for (i = 0; i < count; i++)
52 dsa_tag_driver_register(dsa_tag_driver_array[i], owner);
55 static void dsa_tag_driver_unregister(struct dsa_tag_driver *dsa_tag_driver)
57 mutex_lock(&dsa_tag_drivers_lock);
58 list_del(&dsa_tag_driver->list);
59 mutex_unlock(&dsa_tag_drivers_lock);
61 EXPORT_SYMBOL_GPL(dsa_tag_drivers_register);
63 void dsa_tag_drivers_unregister(struct dsa_tag_driver *dsa_tag_driver_array[],
68 for (i = 0; i < count; i++)
69 dsa_tag_driver_unregister(dsa_tag_driver_array[i]);
71 EXPORT_SYMBOL_GPL(dsa_tag_drivers_unregister);
73 const char *dsa_tag_protocol_to_str(const struct dsa_device_ops *ops)
78 /* Function takes a reference on the module owning the tagger,
79 * so dsa_tag_driver_put must be called afterwards.
81 const struct dsa_device_ops *dsa_find_tagger_by_name(const char *buf)
83 const struct dsa_device_ops *ops = ERR_PTR(-ENOPROTOOPT);
84 struct dsa_tag_driver *dsa_tag_driver;
86 mutex_lock(&dsa_tag_drivers_lock);
87 list_for_each_entry(dsa_tag_driver, &dsa_tag_drivers_list, list) {
88 const struct dsa_device_ops *tmp = dsa_tag_driver->ops;
90 if (!sysfs_streq(buf, tmp->name))
93 if (!try_module_get(dsa_tag_driver->owner))
99 mutex_unlock(&dsa_tag_drivers_lock);
104 const struct dsa_device_ops *dsa_tag_driver_get(int tag_protocol)
106 struct dsa_tag_driver *dsa_tag_driver;
107 const struct dsa_device_ops *ops;
110 request_module("%s%d", DSA_TAG_DRIVER_ALIAS, tag_protocol);
112 mutex_lock(&dsa_tag_drivers_lock);
113 list_for_each_entry(dsa_tag_driver, &dsa_tag_drivers_list, list) {
114 ops = dsa_tag_driver->ops;
115 if (ops->proto == tag_protocol) {
122 if (!try_module_get(dsa_tag_driver->owner))
123 ops = ERR_PTR(-ENOPROTOOPT);
125 ops = ERR_PTR(-ENOPROTOOPT);
128 mutex_unlock(&dsa_tag_drivers_lock);
133 void dsa_tag_driver_put(const struct dsa_device_ops *ops)
135 struct dsa_tag_driver *dsa_tag_driver;
137 mutex_lock(&dsa_tag_drivers_lock);
138 list_for_each_entry(dsa_tag_driver, &dsa_tag_drivers_list, list) {
139 if (dsa_tag_driver->ops == ops) {
140 module_put(dsa_tag_driver->owner);
144 mutex_unlock(&dsa_tag_drivers_lock);
147 static int dev_is_class(struct device *dev, void *class)
149 if (dev->class != NULL && !strcmp(dev->class->name, class))
155 static struct device *dev_find_class(struct device *parent, char *class)
157 if (dev_is_class(parent, class)) {
162 return device_find_child(parent, class, dev_is_class);
165 struct net_device *dsa_dev_to_net_device(struct device *dev)
169 d = dev_find_class(dev, "net");
171 struct net_device *nd;
182 EXPORT_SYMBOL_GPL(dsa_dev_to_net_device);
184 /* Determine if we should defer delivery of skb until we have a rx timestamp.
186 * Called from dsa_switch_rcv. For now, this will only work if tagging is
187 * enabled on the switch. Normally the MAC driver would retrieve the hardware
188 * timestamp when it reads the packet out of the hardware. However in a DSA
189 * switch, the DSA driver owning the interface to which the packet is
190 * delivered is never notified unless we do so here.
192 static bool dsa_skb_defer_rx_timestamp(struct dsa_slave_priv *p,
195 struct dsa_switch *ds = p->dp->ds;
198 if (skb_headroom(skb) < ETH_HLEN)
201 __skb_push(skb, ETH_HLEN);
203 type = ptp_classify_raw(skb);
205 __skb_pull(skb, ETH_HLEN);
207 if (type == PTP_CLASS_NONE)
210 if (likely(ds->ops->port_rxtstamp))
211 return ds->ops->port_rxtstamp(ds, p->dp->index, skb, type);
216 static int dsa_switch_rcv(struct sk_buff *skb, struct net_device *dev,
217 struct packet_type *pt, struct net_device *unused)
219 struct dsa_port *cpu_dp = dev->dsa_ptr;
220 struct sk_buff *nskb = NULL;
221 struct dsa_slave_priv *p;
223 if (unlikely(!cpu_dp)) {
228 skb = skb_unshare(skb, GFP_ATOMIC);
232 nskb = cpu_dp->rcv(skb, dev);
239 skb_push(skb, ETH_HLEN);
240 skb->pkt_type = PACKET_HOST;
241 skb->protocol = eth_type_trans(skb, skb->dev);
243 if (unlikely(!dsa_slave_dev_check(skb->dev))) {
244 /* Packet is to be injected directly on an upper
245 * device, e.g. a team/bond, so skip all DSA-port
252 p = netdev_priv(skb->dev);
254 if (unlikely(cpu_dp->ds->untag_bridge_pvid)) {
255 nskb = dsa_untag_bridge_pvid(skb);
263 dev_sw_netstats_rx_add(skb->dev, skb->len);
265 if (dsa_skb_defer_rx_timestamp(p, skb))
268 gro_cells_receive(&p->gcells, skb);
273 #ifdef CONFIG_PM_SLEEP
274 static bool dsa_port_is_initialized(const struct dsa_port *dp)
276 return dp->type == DSA_PORT_TYPE_USER && dp->slave;
279 int dsa_switch_suspend(struct dsa_switch *ds)
284 /* Suspend slave network devices */
285 dsa_switch_for_each_port(dp, ds) {
286 if (!dsa_port_is_initialized(dp))
289 ret = dsa_slave_suspend(dp->slave);
294 if (ds->ops->suspend)
295 ret = ds->ops->suspend(ds);
299 EXPORT_SYMBOL_GPL(dsa_switch_suspend);
301 int dsa_switch_resume(struct dsa_switch *ds)
307 ret = ds->ops->resume(ds);
312 /* Resume slave network devices */
313 dsa_switch_for_each_port(dp, ds) {
314 if (!dsa_port_is_initialized(dp))
317 ret = dsa_slave_resume(dp->slave);
324 EXPORT_SYMBOL_GPL(dsa_switch_resume);
327 static struct packet_type dsa_pack_type __read_mostly = {
328 .type = cpu_to_be16(ETH_P_XDSA),
329 .func = dsa_switch_rcv,
332 static struct workqueue_struct *dsa_owq;
334 bool dsa_schedule_work(struct work_struct *work)
336 return queue_work(dsa_owq, work);
339 void dsa_flush_workqueue(void)
341 flush_workqueue(dsa_owq);
343 EXPORT_SYMBOL_GPL(dsa_flush_workqueue);
345 int dsa_devlink_param_get(struct devlink *dl, u32 id,
346 struct devlink_param_gset_ctx *ctx)
348 struct dsa_switch *ds = dsa_devlink_to_ds(dl);
350 if (!ds->ops->devlink_param_get)
353 return ds->ops->devlink_param_get(ds, id, ctx);
355 EXPORT_SYMBOL_GPL(dsa_devlink_param_get);
357 int dsa_devlink_param_set(struct devlink *dl, u32 id,
358 struct devlink_param_gset_ctx *ctx)
360 struct dsa_switch *ds = dsa_devlink_to_ds(dl);
362 if (!ds->ops->devlink_param_set)
365 return ds->ops->devlink_param_set(ds, id, ctx);
367 EXPORT_SYMBOL_GPL(dsa_devlink_param_set);
369 int dsa_devlink_params_register(struct dsa_switch *ds,
370 const struct devlink_param *params,
373 return devlink_params_register(ds->devlink, params, params_count);
375 EXPORT_SYMBOL_GPL(dsa_devlink_params_register);
377 void dsa_devlink_params_unregister(struct dsa_switch *ds,
378 const struct devlink_param *params,
381 devlink_params_unregister(ds->devlink, params, params_count);
383 EXPORT_SYMBOL_GPL(dsa_devlink_params_unregister);
385 int dsa_devlink_resource_register(struct dsa_switch *ds,
386 const char *resource_name,
389 u64 parent_resource_id,
390 const struct devlink_resource_size_params *size_params)
392 return devlink_resource_register(ds->devlink, resource_name,
393 resource_size, resource_id,
397 EXPORT_SYMBOL_GPL(dsa_devlink_resource_register);
399 void dsa_devlink_resources_unregister(struct dsa_switch *ds)
401 devlink_resources_unregister(ds->devlink);
403 EXPORT_SYMBOL_GPL(dsa_devlink_resources_unregister);
405 void dsa_devlink_resource_occ_get_register(struct dsa_switch *ds,
407 devlink_resource_occ_get_t *occ_get,
410 return devlink_resource_occ_get_register(ds->devlink, resource_id,
411 occ_get, occ_get_priv);
413 EXPORT_SYMBOL_GPL(dsa_devlink_resource_occ_get_register);
415 void dsa_devlink_resource_occ_get_unregister(struct dsa_switch *ds,
418 devlink_resource_occ_get_unregister(ds->devlink, resource_id);
420 EXPORT_SYMBOL_GPL(dsa_devlink_resource_occ_get_unregister);
422 struct devlink_region *
423 dsa_devlink_region_create(struct dsa_switch *ds,
424 const struct devlink_region_ops *ops,
425 u32 region_max_snapshots, u64 region_size)
427 return devlink_region_create(ds->devlink, ops, region_max_snapshots,
430 EXPORT_SYMBOL_GPL(dsa_devlink_region_create);
432 struct devlink_region *
433 dsa_devlink_port_region_create(struct dsa_switch *ds,
435 const struct devlink_port_region_ops *ops,
436 u32 region_max_snapshots, u64 region_size)
438 struct dsa_port *dp = dsa_to_port(ds, port);
440 return devlink_port_region_create(&dp->devlink_port, ops,
441 region_max_snapshots,
444 EXPORT_SYMBOL_GPL(dsa_devlink_port_region_create);
446 void dsa_devlink_region_destroy(struct devlink_region *region)
448 devlink_region_destroy(region);
450 EXPORT_SYMBOL_GPL(dsa_devlink_region_destroy);
452 struct dsa_port *dsa_port_from_netdev(struct net_device *netdev)
454 if (!netdev || !dsa_slave_dev_check(netdev))
455 return ERR_PTR(-ENODEV);
457 return dsa_slave_to_port(netdev);
459 EXPORT_SYMBOL_GPL(dsa_port_from_netdev);
461 bool dsa_db_equal(const struct dsa_db *a, const struct dsa_db *b)
463 if (a->type != b->type)
468 return a->dp == b->dp;
470 return a->lag.dev == b->lag.dev;
472 return a->bridge.num == b->bridge.num;
479 bool dsa_fdb_present_in_other_db(struct dsa_switch *ds, int port,
480 const unsigned char *addr, u16 vid,
483 struct dsa_port *dp = dsa_to_port(ds, port);
484 struct dsa_mac_addr *a;
486 lockdep_assert_held(&dp->addr_lists_lock);
488 list_for_each_entry(a, &dp->fdbs, list) {
489 if (!ether_addr_equal(a->addr, addr) || a->vid != vid)
492 if (a->db.type == db.type && !dsa_db_equal(&a->db, &db))
498 EXPORT_SYMBOL_GPL(dsa_fdb_present_in_other_db);
500 bool dsa_mdb_present_in_other_db(struct dsa_switch *ds, int port,
501 const struct switchdev_obj_port_mdb *mdb,
504 struct dsa_port *dp = dsa_to_port(ds, port);
505 struct dsa_mac_addr *a;
507 lockdep_assert_held(&dp->addr_lists_lock);
509 list_for_each_entry(a, &dp->mdbs, list) {
510 if (!ether_addr_equal(a->addr, mdb->addr) || a->vid != mdb->vid)
513 if (a->db.type == db.type && !dsa_db_equal(&a->db, &db))
519 EXPORT_SYMBOL_GPL(dsa_mdb_present_in_other_db);
521 static int __init dsa_init_module(void)
525 dsa_owq = alloc_ordered_workqueue("dsa_ordered",
530 rc = dsa_slave_register_notifier();
532 goto register_notifier_fail;
534 dev_add_pack(&dsa_pack_type);
536 dsa_tag_driver_register(&DSA_TAG_DRIVER_NAME(none_ops),
539 rc = rtnl_link_register(&dsa_link_ops);
541 goto netlink_register_fail;
545 netlink_register_fail:
546 dsa_tag_driver_unregister(&DSA_TAG_DRIVER_NAME(none_ops));
547 dsa_slave_unregister_notifier();
548 dev_remove_pack(&dsa_pack_type);
549 register_notifier_fail:
550 destroy_workqueue(dsa_owq);
554 module_init(dsa_init_module);
556 static void __exit dsa_cleanup_module(void)
558 rtnl_link_unregister(&dsa_link_ops);
559 dsa_tag_driver_unregister(&DSA_TAG_DRIVER_NAME(none_ops));
561 dsa_slave_unregister_notifier();
562 dev_remove_pack(&dsa_pack_type);
563 destroy_workqueue(dsa_owq);
565 module_exit(dsa_cleanup_module);
567 MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
568 MODULE_DESCRIPTION("Driver for Distributed Switch Architecture switch chips");
569 MODULE_LICENSE("GPL");
570 MODULE_ALIAS("platform:dsa");