1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * DSA tagging protocol handling
5 * Copyright (c) 2008-2009 Marvell Semiconductor
10 #include <linux/netdevice.h>
11 #include <linux/ptp_classify.h>
12 #include <linux/skbuff.h>
14 #include <net/dst_metadata.h>
19 static LIST_HEAD(dsa_tag_drivers_list);
20 static DEFINE_MUTEX(dsa_tag_drivers_lock);
22 /* Determine if we should defer delivery of skb until we have a rx timestamp.
24 * Called from dsa_switch_rcv. For now, this will only work if tagging is
25 * enabled on the switch. Normally the MAC driver would retrieve the hardware
26 * timestamp when it reads the packet out of the hardware. However in a DSA
27 * switch, the DSA driver owning the interface to which the packet is
28 * delivered is never notified unless we do so here.
30 static bool dsa_skb_defer_rx_timestamp(struct dsa_user_priv *p,
33 struct dsa_switch *ds = p->dp->ds;
36 if (!ds->ops->port_rxtstamp)
39 if (skb_headroom(skb) < ETH_HLEN)
42 __skb_push(skb, ETH_HLEN);
44 type = ptp_classify_raw(skb);
46 __skb_pull(skb, ETH_HLEN);
48 if (type == PTP_CLASS_NONE)
51 return ds->ops->port_rxtstamp(ds, p->dp->index, skb, type);
54 static int dsa_switch_rcv(struct sk_buff *skb, struct net_device *dev,
55 struct packet_type *pt, struct net_device *unused)
57 struct metadata_dst *md_dst = skb_metadata_dst(skb);
58 struct dsa_port *cpu_dp = dev->dsa_ptr;
59 struct sk_buff *nskb = NULL;
60 struct dsa_user_priv *p;
62 if (unlikely(!cpu_dp)) {
67 skb = skb_unshare(skb, GFP_ATOMIC);
71 if (md_dst && md_dst->type == METADATA_HW_PORT_MUX) {
72 unsigned int port = md_dst->u.port_info.port_id;
75 if (!skb_has_extensions(skb))
78 skb->dev = dsa_conduit_find_user(dev, 0, port);
79 if (likely(skb->dev)) {
80 dsa_default_offload_fwd_mark(skb);
84 nskb = cpu_dp->rcv(skb, dev);
93 skb_push(skb, ETH_HLEN);
94 skb->pkt_type = PACKET_HOST;
95 skb->protocol = eth_type_trans(skb, skb->dev);
97 if (unlikely(!dsa_user_dev_check(skb->dev))) {
98 /* Packet is to be injected directly on an upper
99 * device, e.g. a team/bond, so skip all DSA-port
106 p = netdev_priv(skb->dev);
108 if (unlikely(cpu_dp->ds->untag_bridge_pvid)) {
109 nskb = dsa_untag_bridge_pvid(skb);
117 dev_sw_netstats_rx_add(skb->dev, skb->len + ETH_HLEN);
119 if (dsa_skb_defer_rx_timestamp(p, skb))
122 gro_cells_receive(&p->gcells, skb);
127 struct packet_type dsa_pack_type __read_mostly = {
128 .type = cpu_to_be16(ETH_P_XDSA),
129 .func = dsa_switch_rcv,
132 static void dsa_tag_driver_register(struct dsa_tag_driver *dsa_tag_driver,
133 struct module *owner)
135 dsa_tag_driver->owner = owner;
137 mutex_lock(&dsa_tag_drivers_lock);
138 list_add_tail(&dsa_tag_driver->list, &dsa_tag_drivers_list);
139 mutex_unlock(&dsa_tag_drivers_lock);
142 void dsa_tag_drivers_register(struct dsa_tag_driver *dsa_tag_driver_array[],
143 unsigned int count, struct module *owner)
147 for (i = 0; i < count; i++)
148 dsa_tag_driver_register(dsa_tag_driver_array[i], owner);
151 static void dsa_tag_driver_unregister(struct dsa_tag_driver *dsa_tag_driver)
153 mutex_lock(&dsa_tag_drivers_lock);
154 list_del(&dsa_tag_driver->list);
155 mutex_unlock(&dsa_tag_drivers_lock);
157 EXPORT_SYMBOL_GPL(dsa_tag_drivers_register);
159 void dsa_tag_drivers_unregister(struct dsa_tag_driver *dsa_tag_driver_array[],
164 for (i = 0; i < count; i++)
165 dsa_tag_driver_unregister(dsa_tag_driver_array[i]);
167 EXPORT_SYMBOL_GPL(dsa_tag_drivers_unregister);
169 const char *dsa_tag_protocol_to_str(const struct dsa_device_ops *ops)
174 /* Function takes a reference on the module owning the tagger,
175 * so dsa_tag_driver_put must be called afterwards.
177 const struct dsa_device_ops *dsa_tag_driver_get_by_name(const char *name)
179 const struct dsa_device_ops *ops = ERR_PTR(-ENOPROTOOPT);
180 struct dsa_tag_driver *dsa_tag_driver;
182 request_module("%s%s", DSA_TAG_DRIVER_ALIAS, name);
184 mutex_lock(&dsa_tag_drivers_lock);
185 list_for_each_entry(dsa_tag_driver, &dsa_tag_drivers_list, list) {
186 const struct dsa_device_ops *tmp = dsa_tag_driver->ops;
188 if (strcmp(name, tmp->name))
191 if (!try_module_get(dsa_tag_driver->owner))
197 mutex_unlock(&dsa_tag_drivers_lock);
202 const struct dsa_device_ops *dsa_tag_driver_get_by_id(int tag_protocol)
204 struct dsa_tag_driver *dsa_tag_driver;
205 const struct dsa_device_ops *ops;
208 request_module("%sid-%d", DSA_TAG_DRIVER_ALIAS, tag_protocol);
210 mutex_lock(&dsa_tag_drivers_lock);
211 list_for_each_entry(dsa_tag_driver, &dsa_tag_drivers_list, list) {
212 ops = dsa_tag_driver->ops;
213 if (ops->proto == tag_protocol) {
220 if (!try_module_get(dsa_tag_driver->owner))
221 ops = ERR_PTR(-ENOPROTOOPT);
223 ops = ERR_PTR(-ENOPROTOOPT);
226 mutex_unlock(&dsa_tag_drivers_lock);
231 void dsa_tag_driver_put(const struct dsa_device_ops *ops)
233 struct dsa_tag_driver *dsa_tag_driver;
235 mutex_lock(&dsa_tag_drivers_lock);
236 list_for_each_entry(dsa_tag_driver, &dsa_tag_drivers_list, list) {
237 if (dsa_tag_driver->ops == ops) {
238 module_put(dsa_tag_driver->owner);
242 mutex_unlock(&dsa_tag_drivers_lock);