]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/net/ethernet/mscc/ocelot_net.c
net: dsa: felix: stop calling ocelot_port_{enable,disable}
[mirror_ubuntu-jammy-kernel.git] / drivers / net / ethernet / mscc / ocelot_net.c
CommitLineData
9c90eea3
VO
1// SPDX-License-Identifier: (GPL-2.0 OR MIT)
2/* Microsemi Ocelot Switch driver
f59fd9ca
VO
3 *
4 * This contains glue logic between the switchdev driver operations and the
5 * mscc_ocelot_switch_lib.
9c90eea3
VO
6 *
7 * Copyright (c) 2017, 2019 Microsemi Corporation
f59fd9ca 8 * Copyright 2020-2021 NXP Semiconductors
9c90eea3
VO
9 */
10
11#include <linux/if_bridge.h>
0e9bb4e9 12#include <net/pkt_cls.h>
9c90eea3 13#include "ocelot.h"
3c83654f 14#include "ocelot_vcap.h"
9c90eea3 15
f59fd9ca
VO
16static struct ocelot *devlink_port_to_ocelot(struct devlink_port *dlp)
17{
18 return devlink_priv(dlp->devlink);
19}
20
21static int devlink_port_to_port(struct devlink_port *dlp)
22{
23 struct ocelot *ocelot = devlink_port_to_ocelot(dlp);
24
25 return dlp - ocelot->devlink_ports;
26}
27
28static int ocelot_devlink_sb_pool_get(struct devlink *dl,
29 unsigned int sb_index, u16 pool_index,
30 struct devlink_sb_pool_info *pool_info)
31{
32 struct ocelot *ocelot = devlink_priv(dl);
33
34 return ocelot_sb_pool_get(ocelot, sb_index, pool_index, pool_info);
35}
36
37static int ocelot_devlink_sb_pool_set(struct devlink *dl, unsigned int sb_index,
38 u16 pool_index, u32 size,
39 enum devlink_sb_threshold_type threshold_type,
40 struct netlink_ext_ack *extack)
41{
42 struct ocelot *ocelot = devlink_priv(dl);
43
44 return ocelot_sb_pool_set(ocelot, sb_index, pool_index, size,
45 threshold_type, extack);
46}
47
48static int ocelot_devlink_sb_port_pool_get(struct devlink_port *dlp,
49 unsigned int sb_index, u16 pool_index,
50 u32 *p_threshold)
51{
52 struct ocelot *ocelot = devlink_port_to_ocelot(dlp);
53 int port = devlink_port_to_port(dlp);
54
55 return ocelot_sb_port_pool_get(ocelot, port, sb_index, pool_index,
56 p_threshold);
57}
58
59static int ocelot_devlink_sb_port_pool_set(struct devlink_port *dlp,
60 unsigned int sb_index, u16 pool_index,
61 u32 threshold,
62 struct netlink_ext_ack *extack)
63{
64 struct ocelot *ocelot = devlink_port_to_ocelot(dlp);
65 int port = devlink_port_to_port(dlp);
66
67 return ocelot_sb_port_pool_set(ocelot, port, sb_index, pool_index,
68 threshold, extack);
69}
70
71static int
72ocelot_devlink_sb_tc_pool_bind_get(struct devlink_port *dlp,
73 unsigned int sb_index, u16 tc_index,
74 enum devlink_sb_pool_type pool_type,
75 u16 *p_pool_index, u32 *p_threshold)
76{
77 struct ocelot *ocelot = devlink_port_to_ocelot(dlp);
78 int port = devlink_port_to_port(dlp);
79
80 return ocelot_sb_tc_pool_bind_get(ocelot, port, sb_index, tc_index,
81 pool_type, p_pool_index,
82 p_threshold);
83}
84
85static int
86ocelot_devlink_sb_tc_pool_bind_set(struct devlink_port *dlp,
87 unsigned int sb_index, u16 tc_index,
88 enum devlink_sb_pool_type pool_type,
89 u16 pool_index, u32 threshold,
90 struct netlink_ext_ack *extack)
91{
92 struct ocelot *ocelot = devlink_port_to_ocelot(dlp);
93 int port = devlink_port_to_port(dlp);
94
95 return ocelot_sb_tc_pool_bind_set(ocelot, port, sb_index, tc_index,
96 pool_type, pool_index, threshold,
97 extack);
98}
99
100static int ocelot_devlink_sb_occ_snapshot(struct devlink *dl,
101 unsigned int sb_index)
102{
103 struct ocelot *ocelot = devlink_priv(dl);
104
105 return ocelot_sb_occ_snapshot(ocelot, sb_index);
106}
107
108static int ocelot_devlink_sb_occ_max_clear(struct devlink *dl,
109 unsigned int sb_index)
110{
111 struct ocelot *ocelot = devlink_priv(dl);
112
113 return ocelot_sb_occ_max_clear(ocelot, sb_index);
114}
115
116static int ocelot_devlink_sb_occ_port_pool_get(struct devlink_port *dlp,
117 unsigned int sb_index,
118 u16 pool_index, u32 *p_cur,
119 u32 *p_max)
120{
121 struct ocelot *ocelot = devlink_port_to_ocelot(dlp);
122 int port = devlink_port_to_port(dlp);
123
124 return ocelot_sb_occ_port_pool_get(ocelot, port, sb_index, pool_index,
125 p_cur, p_max);
126}
127
128static int
129ocelot_devlink_sb_occ_tc_port_bind_get(struct devlink_port *dlp,
130 unsigned int sb_index, u16 tc_index,
131 enum devlink_sb_pool_type pool_type,
132 u32 *p_cur, u32 *p_max)
133{
134 struct ocelot *ocelot = devlink_port_to_ocelot(dlp);
135 int port = devlink_port_to_port(dlp);
136
137 return ocelot_sb_occ_tc_port_bind_get(ocelot, port, sb_index,
138 tc_index, pool_type,
139 p_cur, p_max);
140}
141
6c30384e 142const struct devlink_ops ocelot_devlink_ops = {
f59fd9ca
VO
143 .sb_pool_get = ocelot_devlink_sb_pool_get,
144 .sb_pool_set = ocelot_devlink_sb_pool_set,
145 .sb_port_pool_get = ocelot_devlink_sb_port_pool_get,
146 .sb_port_pool_set = ocelot_devlink_sb_port_pool_set,
147 .sb_tc_pool_bind_get = ocelot_devlink_sb_tc_pool_bind_get,
148 .sb_tc_pool_bind_set = ocelot_devlink_sb_tc_pool_bind_set,
149 .sb_occ_snapshot = ocelot_devlink_sb_occ_snapshot,
150 .sb_occ_max_clear = ocelot_devlink_sb_occ_max_clear,
151 .sb_occ_port_pool_get = ocelot_devlink_sb_occ_port_pool_get,
152 .sb_occ_tc_port_bind_get = ocelot_devlink_sb_occ_tc_port_bind_get,
6c30384e
VO
153};
154
155int ocelot_port_devlink_init(struct ocelot *ocelot, int port,
156 enum devlink_port_flavour flavour)
157{
158 struct devlink_port *dlp = &ocelot->devlink_ports[port];
159 int id_len = sizeof(ocelot->base_mac);
160 struct devlink *dl = ocelot->devlink;
161 struct devlink_port_attrs attrs = {};
162
163 memcpy(attrs.switch_id.id, &ocelot->base_mac, id_len);
164 attrs.switch_id.id_len = id_len;
165 attrs.phys.port_number = port;
166 attrs.flavour = flavour;
167
168 devlink_port_attrs_set(dlp, &attrs);
169
170 return devlink_port_register(dl, dlp, port);
171}
172
173void ocelot_port_devlink_teardown(struct ocelot *ocelot, int port)
174{
175 struct devlink_port *dlp = &ocelot->devlink_ports[port];
176
177 devlink_port_unregister(dlp);
178}
179
180static struct devlink_port *ocelot_get_devlink_port(struct net_device *dev)
181{
182 struct ocelot_port_private *priv = netdev_priv(dev);
183 struct ocelot *ocelot = priv->port.ocelot;
184 int port = priv->chip_port;
185
186 return &ocelot->devlink_ports[port];
187}
188
9c90eea3
VO
189int ocelot_setup_tc_cls_flower(struct ocelot_port_private *priv,
190 struct flow_cls_offload *f,
191 bool ingress)
192{
193 struct ocelot *ocelot = priv->port.ocelot;
194 int port = priv->chip_port;
195
196 if (!ingress)
197 return -EOPNOTSUPP;
198
199 switch (f->command) {
200 case FLOW_CLS_REPLACE:
201 return ocelot_cls_flower_replace(ocelot, port, f, ingress);
202 case FLOW_CLS_DESTROY:
203 return ocelot_cls_flower_destroy(ocelot, port, f, ingress);
204 case FLOW_CLS_STATS:
205 return ocelot_cls_flower_stats(ocelot, port, f, ingress);
206 default:
207 return -EOPNOTSUPP;
208 }
209}
210
211static int ocelot_setup_tc_cls_matchall(struct ocelot_port_private *priv,
212 struct tc_cls_matchall_offload *f,
213 bool ingress)
214{
215 struct netlink_ext_ack *extack = f->common.extack;
216 struct ocelot *ocelot = priv->port.ocelot;
217 struct ocelot_policer pol = { 0 };
218 struct flow_action_entry *action;
219 int port = priv->chip_port;
220 int err;
221
222 if (!ingress) {
223 NL_SET_ERR_MSG_MOD(extack, "Only ingress is supported");
224 return -EOPNOTSUPP;
225 }
226
227 switch (f->command) {
228 case TC_CLSMATCHALL_REPLACE:
229 if (!flow_offload_has_one_action(&f->rule->action)) {
230 NL_SET_ERR_MSG_MOD(extack,
231 "Only one action is supported");
232 return -EOPNOTSUPP;
233 }
234
235 if (priv->tc.block_shared) {
236 NL_SET_ERR_MSG_MOD(extack,
237 "Rate limit is not supported on shared blocks");
238 return -EOPNOTSUPP;
239 }
240
241 action = &f->rule->action.entries[0];
242
243 if (action->id != FLOW_ACTION_POLICE) {
244 NL_SET_ERR_MSG_MOD(extack, "Unsupported action");
245 return -EOPNOTSUPP;
246 }
247
248 if (priv->tc.police_id && priv->tc.police_id != f->cookie) {
249 NL_SET_ERR_MSG_MOD(extack,
250 "Only one policer per port is supported");
251 return -EEXIST;
252 }
253
6a56e199
BZ
254 if (action->police.rate_pkt_ps) {
255 NL_SET_ERR_MSG_MOD(extack,
256 "QoS offload not support packets per second");
257 return -EOPNOTSUPP;
258 }
259
9c90eea3 260 pol.rate = (u32)div_u64(action->police.rate_bytes_ps, 1000) * 8;
5f035af7 261 pol.burst = action->police.burst;
9c90eea3
VO
262
263 err = ocelot_port_policer_add(ocelot, port, &pol);
264 if (err) {
265 NL_SET_ERR_MSG_MOD(extack, "Could not add policer");
266 return err;
267 }
268
269 priv->tc.police_id = f->cookie;
270 priv->tc.offload_cnt++;
271 return 0;
272 case TC_CLSMATCHALL_DESTROY:
273 if (priv->tc.police_id != f->cookie)
274 return -ENOENT;
275
276 err = ocelot_port_policer_del(ocelot, port);
277 if (err) {
278 NL_SET_ERR_MSG_MOD(extack,
279 "Could not delete policer");
280 return err;
281 }
282 priv->tc.police_id = 0;
283 priv->tc.offload_cnt--;
284 return 0;
285 case TC_CLSMATCHALL_STATS:
286 default:
287 return -EOPNOTSUPP;
288 }
289}
290
291static int ocelot_setup_tc_block_cb(enum tc_setup_type type,
292 void *type_data,
293 void *cb_priv, bool ingress)
294{
295 struct ocelot_port_private *priv = cb_priv;
296
297 if (!tc_cls_can_offload_and_chain0(priv->dev, type_data))
298 return -EOPNOTSUPP;
299
300 switch (type) {
301 case TC_SETUP_CLSMATCHALL:
302 return ocelot_setup_tc_cls_matchall(priv, type_data, ingress);
303 case TC_SETUP_CLSFLOWER:
304 return ocelot_setup_tc_cls_flower(priv, type_data, ingress);
305 default:
306 return -EOPNOTSUPP;
307 }
308}
309
310static int ocelot_setup_tc_block_cb_ig(enum tc_setup_type type,
311 void *type_data,
312 void *cb_priv)
313{
314 return ocelot_setup_tc_block_cb(type, type_data,
315 cb_priv, true);
316}
317
318static int ocelot_setup_tc_block_cb_eg(enum tc_setup_type type,
319 void *type_data,
320 void *cb_priv)
321{
322 return ocelot_setup_tc_block_cb(type, type_data,
323 cb_priv, false);
324}
325
326static LIST_HEAD(ocelot_block_cb_list);
327
328static int ocelot_setup_tc_block(struct ocelot_port_private *priv,
329 struct flow_block_offload *f)
330{
331 struct flow_block_cb *block_cb;
332 flow_setup_cb_t *cb;
333
334 if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS) {
335 cb = ocelot_setup_tc_block_cb_ig;
336 priv->tc.block_shared = f->block_shared;
337 } else if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_EGRESS) {
338 cb = ocelot_setup_tc_block_cb_eg;
339 } else {
340 return -EOPNOTSUPP;
341 }
342
343 f->driver_block_list = &ocelot_block_cb_list;
344
345 switch (f->command) {
346 case FLOW_BLOCK_BIND:
347 if (flow_block_cb_is_busy(cb, priv, &ocelot_block_cb_list))
348 return -EBUSY;
349
350 block_cb = flow_block_cb_alloc(cb, priv, priv, NULL);
351 if (IS_ERR(block_cb))
352 return PTR_ERR(block_cb);
353
354 flow_block_cb_add(block_cb, f);
355 list_add_tail(&block_cb->driver_list, f->driver_block_list);
356 return 0;
357 case FLOW_BLOCK_UNBIND:
358 block_cb = flow_block_cb_lookup(f->block, cb, priv);
359 if (!block_cb)
360 return -ENOENT;
361
362 flow_block_cb_remove(block_cb, f);
363 list_del(&block_cb->driver_list);
364 return 0;
365 default:
366 return -EOPNOTSUPP;
367 }
368}
369
370static int ocelot_setup_tc(struct net_device *dev, enum tc_setup_type type,
371 void *type_data)
372{
373 struct ocelot_port_private *priv = netdev_priv(dev);
374
375 switch (type) {
376 case TC_SETUP_BLOCK:
377 return ocelot_setup_tc_block(priv, type_data);
378 default:
379 return -EOPNOTSUPP;
380 }
381 return 0;
382}
383
384static void ocelot_port_adjust_link(struct net_device *dev)
385{
386 struct ocelot_port_private *priv = netdev_priv(dev);
387 struct ocelot *ocelot = priv->port.ocelot;
388 int port = priv->chip_port;
389
390 ocelot_adjust_link(ocelot, port, dev->phydev);
391}
392
2f0402fe
VO
393static int ocelot_vlan_vid_prepare(struct net_device *dev, u16 vid, bool pvid,
394 bool untagged)
395{
396 struct ocelot_port_private *priv = netdev_priv(dev);
397 struct ocelot_port *ocelot_port = &priv->port;
398 struct ocelot *ocelot = ocelot_port->ocelot;
399 int port = priv->chip_port;
400
401 return ocelot_vlan_prepare(ocelot, port, vid, pvid, untagged);
402}
403
9c90eea3
VO
404static int ocelot_vlan_vid_add(struct net_device *dev, u16 vid, bool pvid,
405 bool untagged)
406{
407 struct ocelot_port_private *priv = netdev_priv(dev);
408 struct ocelot_port *ocelot_port = &priv->port;
409 struct ocelot *ocelot = ocelot_port->ocelot;
410 int port = priv->chip_port;
411 int ret;
412
413 ret = ocelot_vlan_add(ocelot, port, vid, pvid, untagged);
414 if (ret)
415 return ret;
416
417 /* Add the port MAC address to with the right VLAN information */
418 ocelot_mact_learn(ocelot, PGID_CPU, dev->dev_addr, vid,
419 ENTRYTYPE_LOCKED);
420
421 return 0;
422}
423
424static int ocelot_vlan_vid_del(struct net_device *dev, u16 vid)
425{
426 struct ocelot_port_private *priv = netdev_priv(dev);
427 struct ocelot *ocelot = priv->port.ocelot;
428 int port = priv->chip_port;
429 int ret;
430
431 /* 8021q removes VID 0 on module unload for all interfaces
432 * with VLAN filtering feature. We need to keep it to receive
433 * untagged traffic.
434 */
435 if (vid == 0)
436 return 0;
437
438 ret = ocelot_vlan_del(ocelot, port, vid);
439 if (ret)
440 return ret;
441
442 /* Del the port MAC address to with the right VLAN information */
443 ocelot_mact_forget(ocelot, dev->dev_addr, vid);
444
445 return 0;
446}
447
448static int ocelot_port_open(struct net_device *dev)
449{
450 struct ocelot_port_private *priv = netdev_priv(dev);
451 struct ocelot_port *ocelot_port = &priv->port;
9c90eea3
VO
452 int err;
453
454 if (priv->serdes) {
455 err = phy_set_mode_ext(priv->serdes, PHY_MODE_ETHERNET,
456 ocelot_port->phy_mode);
457 if (err) {
458 netdev_err(dev, "Could not set mode of SerDes\n");
459 return err;
460 }
461 }
462
463 err = phy_connect_direct(dev, priv->phy, &ocelot_port_adjust_link,
464 ocelot_port->phy_mode);
465 if (err) {
466 netdev_err(dev, "Could not attach to PHY\n");
467 return err;
468 }
469
470 dev->phydev = priv->phy;
471
472 phy_attached_info(priv->phy);
473 phy_start(priv->phy);
474
9c90eea3
VO
475 return 0;
476}
477
478static int ocelot_port_stop(struct net_device *dev)
479{
480 struct ocelot_port_private *priv = netdev_priv(dev);
481 struct ocelot *ocelot = priv->port.ocelot;
482 int port = priv->chip_port;
483
484 phy_disconnect(priv->phy);
485
486 dev->phydev = NULL;
487
488 ocelot_port_disable(ocelot, port);
489
490 return 0;
491}
492
137ffbc4 493static netdev_tx_t ocelot_port_xmit(struct sk_buff *skb, struct net_device *dev)
9c90eea3
VO
494{
495 struct ocelot_port_private *priv = netdev_priv(dev);
9c90eea3
VO
496 struct ocelot_port *ocelot_port = &priv->port;
497 struct ocelot *ocelot = ocelot_port->ocelot;
9c90eea3 498 int port = priv->chip_port;
137ffbc4 499 u32 rew_op = 0;
9c90eea3 500
137ffbc4 501 if (!ocelot_can_inject(ocelot, 0))
9c90eea3
VO
502 return NETDEV_TX_BUSY;
503
9c90eea3 504 /* Check if timestamping is needed */
137ffbc4 505 if (ocelot->ptp && (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) {
682eaad9 506 struct sk_buff *clone = NULL;
e2f9a8fe 507
682eaad9
YL
508 if (ocelot_port_txtstamp_request(ocelot, port, skb, &clone)) {
509 kfree_skb(skb);
510 return NETDEV_TX_OK;
511 }
e2f9a8fe 512
39e5308b
YL
513 if (clone)
514 OCELOT_SKB_CB(skb)->clone = clone;
515
516 rew_op = ocelot_ptp_rew_op(skb);
e2f9a8fe 517 }
9dda66ac 518
137ffbc4 519 ocelot_port_inject_frame(ocelot, port, 0, rew_op, skb);
9c90eea3 520
e2f9a8fe 521 kfree_skb(skb);
9c90eea3 522
9c90eea3
VO
523 return NETDEV_TX_OK;
524}
525
ca0b272b
VO
526enum ocelot_action_type {
527 OCELOT_MACT_LEARN,
528 OCELOT_MACT_FORGET,
529};
530
531struct ocelot_mact_work_ctx {
532 struct work_struct work;
533 struct ocelot *ocelot;
534 enum ocelot_action_type type;
535 union {
536 /* OCELOT_MACT_LEARN */
537 struct {
538 unsigned char addr[ETH_ALEN];
539 u16 vid;
540 enum macaccess_entry_type entry_type;
541 int pgid;
542 } learn;
543 /* OCELOT_MACT_FORGET */
544 struct {
545 unsigned char addr[ETH_ALEN];
546 u16 vid;
547 } forget;
548 };
549};
550
551#define ocelot_work_to_ctx(x) \
552 container_of((x), struct ocelot_mact_work_ctx, work)
553
554static void ocelot_mact_work(struct work_struct *work)
555{
556 struct ocelot_mact_work_ctx *w = ocelot_work_to_ctx(work);
557 struct ocelot *ocelot = w->ocelot;
558
559 switch (w->type) {
560 case OCELOT_MACT_LEARN:
561 ocelot_mact_learn(ocelot, w->learn.pgid, w->learn.addr,
562 w->learn.vid, w->learn.entry_type);
563 break;
564 case OCELOT_MACT_FORGET:
565 ocelot_mact_forget(ocelot, w->forget.addr, w->forget.vid);
566 break;
567 default:
568 break;
20efd2c7 569 }
ca0b272b
VO
570
571 kfree(w);
572}
573
574static int ocelot_enqueue_mact_action(struct ocelot *ocelot,
575 const struct ocelot_mact_work_ctx *ctx)
576{
577 struct ocelot_mact_work_ctx *w = kmemdup(ctx, sizeof(*w), GFP_ATOMIC);
578
579 if (!w)
580 return -ENOMEM;
581
582 w->ocelot = ocelot;
583 INIT_WORK(&w->work, ocelot_mact_work);
584 queue_work(ocelot->owq, &w->work);
585
586 return 0;
587}
588
9c90eea3
VO
589static int ocelot_mc_unsync(struct net_device *dev, const unsigned char *addr)
590{
591 struct ocelot_port_private *priv = netdev_priv(dev);
592 struct ocelot_port *ocelot_port = &priv->port;
593 struct ocelot *ocelot = ocelot_port->ocelot;
ca0b272b
VO
594 struct ocelot_mact_work_ctx w;
595
596 ether_addr_copy(w.forget.addr, addr);
597 w.forget.vid = ocelot_port->pvid_vlan.vid;
598 w.type = OCELOT_MACT_FORGET;
9c90eea3 599
ca0b272b 600 return ocelot_enqueue_mact_action(ocelot, &w);
9c90eea3
VO
601}
602
603static int ocelot_mc_sync(struct net_device *dev, const unsigned char *addr)
604{
605 struct ocelot_port_private *priv = netdev_priv(dev);
606 struct ocelot_port *ocelot_port = &priv->port;
607 struct ocelot *ocelot = ocelot_port->ocelot;
ca0b272b
VO
608 struct ocelot_mact_work_ctx w;
609
610 ether_addr_copy(w.learn.addr, addr);
611 w.learn.vid = ocelot_port->pvid_vlan.vid;
612 w.learn.pgid = PGID_CPU;
613 w.learn.entry_type = ENTRYTYPE_LOCKED;
614 w.type = OCELOT_MACT_LEARN;
9c90eea3 615
ca0b272b 616 return ocelot_enqueue_mact_action(ocelot, &w);
9c90eea3
VO
617}
618
619static void ocelot_set_rx_mode(struct net_device *dev)
620{
621 struct ocelot_port_private *priv = netdev_priv(dev);
622 struct ocelot *ocelot = priv->port.ocelot;
623 u32 val;
624 int i;
625
626 /* This doesn't handle promiscuous mode because the bridge core is
627 * setting IFF_PROMISC on all slave interfaces and all frames would be
628 * forwarded to the CPU port.
629 */
630 val = GENMASK(ocelot->num_phys_ports - 1, 0);
96b029b0 631 for_each_nonreserved_multicast_dest_pgid(ocelot, i)
9c90eea3
VO
632 ocelot_write_rix(ocelot, val, ANA_PGID_PGID, i);
633
634 __dev_mc_sync(dev, ocelot_mc_sync, ocelot_mc_unsync);
635}
636
9c90eea3
VO
637static int ocelot_port_set_mac_address(struct net_device *dev, void *p)
638{
639 struct ocelot_port_private *priv = netdev_priv(dev);
640 struct ocelot_port *ocelot_port = &priv->port;
641 struct ocelot *ocelot = ocelot_port->ocelot;
642 const struct sockaddr *addr = p;
643
644 /* Learn the new net device MAC address in the mac table. */
c3e58a75
VO
645 ocelot_mact_learn(ocelot, PGID_CPU, addr->sa_data,
646 ocelot_port->pvid_vlan.vid, ENTRYTYPE_LOCKED);
9c90eea3 647 /* Then forget the previous one. */
c3e58a75 648 ocelot_mact_forget(ocelot, dev->dev_addr, ocelot_port->pvid_vlan.vid);
9c90eea3
VO
649
650 ether_addr_copy(dev->dev_addr, addr->sa_data);
651 return 0;
652}
653
654static void ocelot_get_stats64(struct net_device *dev,
655 struct rtnl_link_stats64 *stats)
656{
657 struct ocelot_port_private *priv = netdev_priv(dev);
658 struct ocelot *ocelot = priv->port.ocelot;
659 int port = priv->chip_port;
660
661 /* Configure the port to read the stats from */
662 ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(port),
663 SYS_STAT_CFG);
664
665 /* Get Rx stats */
666 stats->rx_bytes = ocelot_read(ocelot, SYS_COUNT_RX_OCTETS);
667 stats->rx_packets = ocelot_read(ocelot, SYS_COUNT_RX_SHORTS) +
668 ocelot_read(ocelot, SYS_COUNT_RX_FRAGMENTS) +
669 ocelot_read(ocelot, SYS_COUNT_RX_JABBERS) +
670 ocelot_read(ocelot, SYS_COUNT_RX_LONGS) +
671 ocelot_read(ocelot, SYS_COUNT_RX_64) +
672 ocelot_read(ocelot, SYS_COUNT_RX_65_127) +
673 ocelot_read(ocelot, SYS_COUNT_RX_128_255) +
674 ocelot_read(ocelot, SYS_COUNT_RX_256_1023) +
675 ocelot_read(ocelot, SYS_COUNT_RX_1024_1526) +
676 ocelot_read(ocelot, SYS_COUNT_RX_1527_MAX);
677 stats->multicast = ocelot_read(ocelot, SYS_COUNT_RX_MULTICAST);
678 stats->rx_dropped = dev->stats.rx_dropped;
679
680 /* Get Tx stats */
681 stats->tx_bytes = ocelot_read(ocelot, SYS_COUNT_TX_OCTETS);
682 stats->tx_packets = ocelot_read(ocelot, SYS_COUNT_TX_64) +
683 ocelot_read(ocelot, SYS_COUNT_TX_65_127) +
684 ocelot_read(ocelot, SYS_COUNT_TX_128_511) +
685 ocelot_read(ocelot, SYS_COUNT_TX_512_1023) +
686 ocelot_read(ocelot, SYS_COUNT_TX_1024_1526) +
687 ocelot_read(ocelot, SYS_COUNT_TX_1527_MAX);
688 stats->tx_dropped = ocelot_read(ocelot, SYS_COUNT_TX_DROPS) +
689 ocelot_read(ocelot, SYS_COUNT_TX_AGING);
690 stats->collisions = ocelot_read(ocelot, SYS_COUNT_TX_COLLISION);
691}
692
693static int ocelot_port_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
694 struct net_device *dev,
695 const unsigned char *addr,
696 u16 vid, u16 flags,
697 struct netlink_ext_ack *extack)
698{
699 struct ocelot_port_private *priv = netdev_priv(dev);
700 struct ocelot *ocelot = priv->port.ocelot;
701 int port = priv->chip_port;
702
703 return ocelot_fdb_add(ocelot, port, addr, vid);
704}
705
706static int ocelot_port_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
707 struct net_device *dev,
708 const unsigned char *addr, u16 vid)
709{
710 struct ocelot_port_private *priv = netdev_priv(dev);
711 struct ocelot *ocelot = priv->port.ocelot;
712 int port = priv->chip_port;
713
714 return ocelot_fdb_del(ocelot, port, addr, vid);
715}
716
717static int ocelot_port_fdb_dump(struct sk_buff *skb,
718 struct netlink_callback *cb,
719 struct net_device *dev,
720 struct net_device *filter_dev, int *idx)
721{
722 struct ocelot_port_private *priv = netdev_priv(dev);
723 struct ocelot *ocelot = priv->port.ocelot;
724 struct ocelot_dump_ctx dump = {
725 .dev = dev,
726 .skb = skb,
727 .cb = cb,
728 .idx = *idx,
729 };
730 int port = priv->chip_port;
731 int ret;
732
733 ret = ocelot_fdb_dump(ocelot, port, ocelot_port_fdb_do_dump, &dump);
734
735 *idx = dump.idx;
736
737 return ret;
738}
739
740static int ocelot_vlan_rx_add_vid(struct net_device *dev, __be16 proto,
741 u16 vid)
742{
743 return ocelot_vlan_vid_add(dev, vid, false, false);
744}
745
746static int ocelot_vlan_rx_kill_vid(struct net_device *dev, __be16 proto,
747 u16 vid)
748{
749 return ocelot_vlan_vid_del(dev, vid);
750}
751
752static void ocelot_vlan_mode(struct ocelot *ocelot, int port,
753 netdev_features_t features)
754{
755 u32 val;
756
757 /* Filtering */
758 val = ocelot_read(ocelot, ANA_VLANMASK);
759 if (features & NETIF_F_HW_VLAN_CTAG_FILTER)
760 val |= BIT(port);
761 else
762 val &= ~BIT(port);
763 ocelot_write(ocelot, val, ANA_VLANMASK);
764}
765
766static int ocelot_set_features(struct net_device *dev,
767 netdev_features_t features)
768{
769 netdev_features_t changed = dev->features ^ features;
770 struct ocelot_port_private *priv = netdev_priv(dev);
771 struct ocelot *ocelot = priv->port.ocelot;
772 int port = priv->chip_port;
773
774 if ((dev->features & NETIF_F_HW_TC) > (features & NETIF_F_HW_TC) &&
775 priv->tc.offload_cnt) {
776 netdev_err(dev,
777 "Cannot disable HW TC offload while offloads active\n");
778 return -EBUSY;
779 }
780
781 if (changed & NETIF_F_HW_VLAN_CTAG_FILTER)
782 ocelot_vlan_mode(ocelot, port, features);
783
784 return 0;
785}
786
9c90eea3
VO
787static int ocelot_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
788{
789 struct ocelot_port_private *priv = netdev_priv(dev);
790 struct ocelot *ocelot = priv->port.ocelot;
791 int port = priv->chip_port;
792
793 /* If the attached PHY device isn't capable of timestamping operations,
794 * use our own (when possible).
795 */
796 if (!phy_has_hwtstamp(dev->phydev) && ocelot->ptp) {
797 switch (cmd) {
798 case SIOCSHWTSTAMP:
799 return ocelot_hwstamp_set(ocelot, port, ifr);
800 case SIOCGHWTSTAMP:
801 return ocelot_hwstamp_get(ocelot, port, ifr);
802 }
803 }
804
805 return phy_mii_ioctl(dev->phydev, ifr, cmd);
806}
807
808static const struct net_device_ops ocelot_port_netdev_ops = {
809 .ndo_open = ocelot_port_open,
810 .ndo_stop = ocelot_port_stop,
811 .ndo_start_xmit = ocelot_port_xmit,
812 .ndo_set_rx_mode = ocelot_set_rx_mode,
9c90eea3
VO
813 .ndo_set_mac_address = ocelot_port_set_mac_address,
814 .ndo_get_stats64 = ocelot_get_stats64,
815 .ndo_fdb_add = ocelot_port_fdb_add,
816 .ndo_fdb_del = ocelot_port_fdb_del,
817 .ndo_fdb_dump = ocelot_port_fdb_dump,
818 .ndo_vlan_rx_add_vid = ocelot_vlan_rx_add_vid,
819 .ndo_vlan_rx_kill_vid = ocelot_vlan_rx_kill_vid,
820 .ndo_set_features = ocelot_set_features,
9c90eea3 821 .ndo_setup_tc = ocelot_setup_tc,
a7605370 822 .ndo_eth_ioctl = ocelot_ioctl,
6c30384e 823 .ndo_get_devlink_port = ocelot_get_devlink_port,
9c90eea3
VO
824};
825
319e4dd1
VO
826struct net_device *ocelot_port_to_netdev(struct ocelot *ocelot, int port)
827{
828 struct ocelot_port *ocelot_port = ocelot->ports[port];
829 struct ocelot_port_private *priv;
830
831 if (!ocelot_port)
832 return NULL;
833
834 priv = container_of(ocelot_port, struct ocelot_port_private, port);
835
836 return priv->dev;
837}
838
7e38b03f
VO
839/* Checks if the net_device instance given to us originates from our driver */
840static bool ocelot_netdevice_dev_check(const struct net_device *dev)
319e4dd1
VO
841{
842 return dev->netdev_ops == &ocelot_port_netdev_ops;
843}
844
845int ocelot_netdev_to_port(struct net_device *dev)
846{
847 struct ocelot_port_private *priv;
848
7e38b03f 849 if (!dev || !ocelot_netdevice_dev_check(dev))
319e4dd1
VO
850 return -EINVAL;
851
852 priv = netdev_priv(dev);
853
854 return priv->chip_port;
855}
856
9c90eea3
VO
857static void ocelot_port_get_strings(struct net_device *netdev, u32 sset,
858 u8 *data)
859{
860 struct ocelot_port_private *priv = netdev_priv(netdev);
861 struct ocelot *ocelot = priv->port.ocelot;
862 int port = priv->chip_port;
863
864 ocelot_get_strings(ocelot, port, sset, data);
865}
866
867static void ocelot_port_get_ethtool_stats(struct net_device *dev,
868 struct ethtool_stats *stats,
869 u64 *data)
870{
871 struct ocelot_port_private *priv = netdev_priv(dev);
872 struct ocelot *ocelot = priv->port.ocelot;
873 int port = priv->chip_port;
874
875 ocelot_get_ethtool_stats(ocelot, port, data);
876}
877
878static int ocelot_port_get_sset_count(struct net_device *dev, int sset)
879{
880 struct ocelot_port_private *priv = netdev_priv(dev);
881 struct ocelot *ocelot = priv->port.ocelot;
882 int port = priv->chip_port;
883
884 return ocelot_get_sset_count(ocelot, port, sset);
885}
886
887static int ocelot_port_get_ts_info(struct net_device *dev,
888 struct ethtool_ts_info *info)
889{
890 struct ocelot_port_private *priv = netdev_priv(dev);
891 struct ocelot *ocelot = priv->port.ocelot;
892 int port = priv->chip_port;
893
894 if (!ocelot->ptp)
895 return ethtool_op_get_ts_info(dev, info);
896
897 return ocelot_get_ts_info(ocelot, port, info);
898}
899
900static const struct ethtool_ops ocelot_ethtool_ops = {
901 .get_strings = ocelot_port_get_strings,
902 .get_ethtool_stats = ocelot_port_get_ethtool_stats,
903 .get_sset_count = ocelot_port_get_sset_count,
904 .get_link_ksettings = phy_ethtool_get_link_ksettings,
905 .set_link_ksettings = phy_ethtool_set_link_ksettings,
906 .get_ts_info = ocelot_port_get_ts_info,
907};
908
909static void ocelot_port_attr_stp_state_set(struct ocelot *ocelot, int port,
9c90eea3
VO
910 u8 state)
911{
9c90eea3
VO
912 ocelot_bridge_stp_state_set(ocelot, port, state);
913}
914
915static void ocelot_port_attr_ageing_set(struct ocelot *ocelot, int port,
916 unsigned long ageing_clock_t)
917{
918 unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock_t);
919 u32 ageing_time = jiffies_to_msecs(ageing_jiffies);
920
921 ocelot_set_ageing_time(ocelot, ageing_time);
922}
923
924static void ocelot_port_attr_mc_set(struct ocelot *ocelot, int port, bool mc)
925{
926 u32 cpu_fwd_mcast = ANA_PORT_CPU_FWD_CFG_CPU_IGMP_REDIR_ENA |
927 ANA_PORT_CPU_FWD_CFG_CPU_MLD_REDIR_ENA |
928 ANA_PORT_CPU_FWD_CFG_CPU_IPMC_CTRL_COPY_ENA;
929 u32 val = 0;
930
931 if (mc)
932 val = cpu_fwd_mcast;
933
934 ocelot_rmw_gix(ocelot, val, cpu_fwd_mcast,
935 ANA_PORT_CPU_FWD_CFG, port);
936}
937
69bfac96 938static int ocelot_port_attr_set(struct net_device *dev, const void *ctx,
4c08c586
VO
939 const struct switchdev_attr *attr,
940 struct netlink_ext_ack *extack)
9c90eea3
VO
941{
942 struct ocelot_port_private *priv = netdev_priv(dev);
943 struct ocelot *ocelot = priv->port.ocelot;
944 int port = priv->chip_port;
945 int err = 0;
946
0d2cfbd4
VO
947 if (ctx && ctx != priv)
948 return 0;
949
9c90eea3
VO
950 switch (attr->id) {
951 case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
bae33f2b 952 ocelot_port_attr_stp_state_set(ocelot, port, attr->u.stp_state);
9c90eea3
VO
953 break;
954 case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
955 ocelot_port_attr_ageing_set(ocelot, port, attr->u.ageing_time);
956 break;
957 case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
bae33f2b 958 ocelot_port_vlan_filtering(ocelot, port, attr->u.vlan_filtering);
9c90eea3
VO
959 break;
960 case SWITCHDEV_ATTR_ID_BRIDGE_MC_DISABLED:
961 ocelot_port_attr_mc_set(ocelot, port, !attr->u.mc_disabled);
962 break;
421741ea
VO
963 case SWITCHDEV_ATTR_ID_PORT_PRE_BRIDGE_FLAGS:
964 err = ocelot_port_pre_bridge_flags(ocelot, port,
965 attr->u.brport_flags);
966 break;
967 case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS:
968 ocelot_port_bridge_flags(ocelot, port, attr->u.brport_flags);
969 break;
9c90eea3
VO
970 default:
971 err = -EOPNOTSUPP;
972 break;
973 }
974
975 return err;
976}
977
978static int ocelot_port_obj_add_vlan(struct net_device *dev,
ffb68fc5 979 const struct switchdev_obj_port_vlan *vlan)
9c90eea3 980{
b7a9e0da
VO
981 bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
982 bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
9c90eea3 983 int ret;
9c90eea3 984
ffb68fc5
VO
985 ret = ocelot_vlan_vid_prepare(dev, vlan->vid, pvid, untagged);
986 if (ret)
987 return ret;
9c90eea3 988
ffb68fc5 989 return ocelot_vlan_vid_add(dev, vlan->vid, pvid, untagged);
9c90eea3
VO
990}
991
209edf95 992static int ocelot_port_obj_add_mdb(struct net_device *dev,
ffb68fc5 993 const struct switchdev_obj_port_mdb *mdb)
209edf95
VO
994{
995 struct ocelot_port_private *priv = netdev_priv(dev);
996 struct ocelot_port *ocelot_port = &priv->port;
997 struct ocelot *ocelot = ocelot_port->ocelot;
998 int port = priv->chip_port;
999
209edf95
VO
1000 return ocelot_port_mdb_add(ocelot, port, mdb);
1001}
1002
1003static int ocelot_port_obj_del_mdb(struct net_device *dev,
1004 const struct switchdev_obj_port_mdb *mdb)
1005{
1006 struct ocelot_port_private *priv = netdev_priv(dev);
1007 struct ocelot_port *ocelot_port = &priv->port;
1008 struct ocelot *ocelot = ocelot_port->ocelot;
1009 int port = priv->chip_port;
1010
1011 return ocelot_port_mdb_del(ocelot, port, mdb);
1012}
1013
d8ea7ff3
HV
1014static int ocelot_port_obj_mrp_add(struct net_device *dev,
1015 const struct switchdev_obj_mrp *mrp)
1016{
1017 struct ocelot_port_private *priv = netdev_priv(dev);
1018 struct ocelot_port *ocelot_port = &priv->port;
1019 struct ocelot *ocelot = ocelot_port->ocelot;
1020 int port = priv->chip_port;
1021
1022 return ocelot_mrp_add(ocelot, port, mrp);
1023}
1024
1025static int ocelot_port_obj_mrp_del(struct net_device *dev,
1026 const struct switchdev_obj_mrp *mrp)
1027{
1028 struct ocelot_port_private *priv = netdev_priv(dev);
1029 struct ocelot_port *ocelot_port = &priv->port;
1030 struct ocelot *ocelot = ocelot_port->ocelot;
1031 int port = priv->chip_port;
1032
1033 return ocelot_mrp_del(ocelot, port, mrp);
1034}
1035
1036static int
1037ocelot_port_obj_mrp_add_ring_role(struct net_device *dev,
1038 const struct switchdev_obj_ring_role_mrp *mrp)
1039{
1040 struct ocelot_port_private *priv = netdev_priv(dev);
1041 struct ocelot_port *ocelot_port = &priv->port;
1042 struct ocelot *ocelot = ocelot_port->ocelot;
1043 int port = priv->chip_port;
1044
1045 return ocelot_mrp_add_ring_role(ocelot, port, mrp);
1046}
1047
1048static int
1049ocelot_port_obj_mrp_del_ring_role(struct net_device *dev,
1050 const struct switchdev_obj_ring_role_mrp *mrp)
1051{
1052 struct ocelot_port_private *priv = netdev_priv(dev);
1053 struct ocelot_port *ocelot_port = &priv->port;
1054 struct ocelot *ocelot = ocelot_port->ocelot;
1055 int port = priv->chip_port;
1056
1057 return ocelot_mrp_del_ring_role(ocelot, port, mrp);
1058}
1059
69bfac96 1060static int ocelot_port_obj_add(struct net_device *dev, const void *ctx,
9c90eea3 1061 const struct switchdev_obj *obj,
9c90eea3
VO
1062 struct netlink_ext_ack *extack)
1063{
0d2cfbd4 1064 struct ocelot_port_private *priv = netdev_priv(dev);
9c90eea3
VO
1065 int ret = 0;
1066
0d2cfbd4
VO
1067 if (ctx && ctx != priv)
1068 return 0;
1069
9c90eea3
VO
1070 switch (obj->id) {
1071 case SWITCHDEV_OBJ_ID_PORT_VLAN:
1072 ret = ocelot_port_obj_add_vlan(dev,
ffb68fc5 1073 SWITCHDEV_OBJ_PORT_VLAN(obj));
9c90eea3
VO
1074 break;
1075 case SWITCHDEV_OBJ_ID_PORT_MDB:
ffb68fc5 1076 ret = ocelot_port_obj_add_mdb(dev, SWITCHDEV_OBJ_PORT_MDB(obj));
9c90eea3 1077 break;
d8ea7ff3
HV
1078 case SWITCHDEV_OBJ_ID_MRP:
1079 ret = ocelot_port_obj_mrp_add(dev, SWITCHDEV_OBJ_MRP(obj));
1080 break;
1081 case SWITCHDEV_OBJ_ID_RING_ROLE_MRP:
1082 ret = ocelot_port_obj_mrp_add_ring_role(dev,
1083 SWITCHDEV_OBJ_RING_ROLE_MRP(obj));
1084 break;
9c90eea3
VO
1085 default:
1086 return -EOPNOTSUPP;
1087 }
1088
1089 return ret;
1090}
1091
69bfac96 1092static int ocelot_port_obj_del(struct net_device *dev, const void *ctx,
9c90eea3
VO
1093 const struct switchdev_obj *obj)
1094{
0d2cfbd4 1095 struct ocelot_port_private *priv = netdev_priv(dev);
9c90eea3
VO
1096 int ret = 0;
1097
0d2cfbd4
VO
1098 if (ctx && ctx != priv)
1099 return 0;
1100
9c90eea3
VO
1101 switch (obj->id) {
1102 case SWITCHDEV_OBJ_ID_PORT_VLAN:
b7a9e0da
VO
1103 ret = ocelot_vlan_vid_del(dev,
1104 SWITCHDEV_OBJ_PORT_VLAN(obj)->vid);
9c90eea3
VO
1105 break;
1106 case SWITCHDEV_OBJ_ID_PORT_MDB:
1107 ret = ocelot_port_obj_del_mdb(dev, SWITCHDEV_OBJ_PORT_MDB(obj));
1108 break;
d8ea7ff3
HV
1109 case SWITCHDEV_OBJ_ID_MRP:
1110 ret = ocelot_port_obj_mrp_del(dev, SWITCHDEV_OBJ_MRP(obj));
1111 break;
1112 case SWITCHDEV_OBJ_ID_RING_ROLE_MRP:
1113 ret = ocelot_port_obj_mrp_del_ring_role(dev,
1114 SWITCHDEV_OBJ_RING_ROLE_MRP(obj));
1115 break;
9c90eea3
VO
1116 default:
1117 return -EOPNOTSUPP;
1118 }
1119
1120 return ret;
1121}
1122
e4bd44e8
VO
1123static void ocelot_inherit_brport_flags(struct ocelot *ocelot, int port,
1124 struct net_device *brport_dev)
1125{
1126 struct switchdev_brport_flags flags = {0};
1127 int flag;
1128
1129 flags.mask = BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD | BR_BCAST_FLOOD;
1130
1131 for_each_set_bit(flag, &flags.mask, 32)
1132 if (br_port_flag_is_set(brport_dev, BIT(flag)))
1133 flags.val |= BIT(flag);
1134
1135 ocelot_port_bridge_flags(ocelot, port, flags);
1136}
1137
1138static void ocelot_clear_brport_flags(struct ocelot *ocelot, int port)
1139{
1140 struct switchdev_brport_flags flags;
1141
1142 flags.mask = BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD | BR_BCAST_FLOOD;
1143 flags.val = flags.mask & ~BR_LEARNING;
1144
1145 ocelot_port_bridge_flags(ocelot, port, flags);
1146}
1147
1148static int ocelot_switchdev_sync(struct ocelot *ocelot, int port,
1149 struct net_device *brport_dev,
1150 struct net_device *bridge_dev,
1151 struct netlink_ext_ack *extack)
1152{
1153 clock_t ageing_time;
1154 u8 stp_state;
0d2cfbd4 1155
e4bd44e8
VO
1156 ocelot_inherit_brport_flags(ocelot, port, brport_dev);
1157
1158 stp_state = br_port_get_stp_state(brport_dev);
1159 ocelot_bridge_stp_state_set(ocelot, port, stp_state);
1160
e4bd44e8
VO
1161 ageing_time = br_get_ageing_time(bridge_dev);
1162 ocelot_port_attr_ageing_set(ocelot, port, ageing_time);
1163
4e51bf44
VO
1164 return ocelot_port_vlan_filtering(ocelot, port,
1165 br_vlan_enabled(bridge_dev));
e4bd44e8
VO
1166}
1167
1168static int ocelot_switchdev_unsync(struct ocelot *ocelot, int port)
1169{
1170 int err;
1171
1172 err = ocelot_port_vlan_filtering(ocelot, port, false);
1173 if (err)
1174 return err;
1175
1176 ocelot_clear_brport_flags(ocelot, port);
1177
1178 ocelot_bridge_stp_state_set(ocelot, port, BR_STATE_FORWARDING);
1179
1180 return 0;
1181}
1182
81ef35e7 1183static int ocelot_netdevice_bridge_join(struct net_device *dev,
e4bd44e8 1184 struct net_device *brport_dev,
81ef35e7
VO
1185 struct net_device *bridge,
1186 struct netlink_ext_ack *extack)
421741ea 1187{
81ef35e7
VO
1188 struct ocelot_port_private *priv = netdev_priv(dev);
1189 struct ocelot_port *ocelot_port = &priv->port;
1190 struct ocelot *ocelot = ocelot_port->ocelot;
81ef35e7 1191 int port = priv->chip_port;
421741ea
VO
1192 int err;
1193
e4bd44e8 1194 ocelot_port_bridge_join(ocelot, port, bridge);
421741ea 1195
4e51bf44
VO
1196 err = switchdev_bridge_port_offload(brport_dev, dev, priv,
1197 &ocelot_netdevice_nb,
1198 &ocelot_switchdev_blocking_nb,
47211192 1199 false, extack);
2f5dc00f
VO
1200 if (err)
1201 goto err_switchdev_offload;
1202
e4bd44e8 1203 err = ocelot_switchdev_sync(ocelot, port, brport_dev, bridge, extack);
421741ea 1204 if (err)
e4bd44e8 1205 goto err_switchdev_sync;
421741ea
VO
1206
1207 return 0;
e4bd44e8
VO
1208
1209err_switchdev_sync:
4e51bf44
VO
1210 switchdev_bridge_port_unoffload(brport_dev, priv,
1211 &ocelot_netdevice_nb,
1212 &ocelot_switchdev_blocking_nb);
2f5dc00f 1213err_switchdev_offload:
e4bd44e8
VO
1214 ocelot_port_bridge_leave(ocelot, port, bridge);
1215 return err;
421741ea
VO
1216}
1217
4e51bf44
VO
1218static void ocelot_netdevice_pre_bridge_leave(struct net_device *dev,
1219 struct net_device *brport_dev)
2f5dc00f 1220{
4e51bf44
VO
1221 struct ocelot_port_private *priv = netdev_priv(dev);
1222
1223 switchdev_bridge_port_unoffload(brport_dev, priv,
1224 &ocelot_netdevice_nb,
1225 &ocelot_switchdev_blocking_nb);
2f5dc00f
VO
1226}
1227
81ef35e7 1228static int ocelot_netdevice_bridge_leave(struct net_device *dev,
e4bd44e8 1229 struct net_device *brport_dev,
421741ea
VO
1230 struct net_device *bridge)
1231{
81ef35e7
VO
1232 struct ocelot_port_private *priv = netdev_priv(dev);
1233 struct ocelot_port *ocelot_port = &priv->port;
1234 struct ocelot *ocelot = ocelot_port->ocelot;
81ef35e7 1235 int port = priv->chip_port;
421741ea
VO
1236 int err;
1237
e4bd44e8
VO
1238 err = ocelot_switchdev_unsync(ocelot, port);
1239 if (err)
1240 return err;
421741ea 1241
e4bd44e8 1242 ocelot_port_bridge_leave(ocelot, port, bridge);
421741ea 1243
e4bd44e8 1244 return 0;
421741ea
VO
1245}
1246
81ef35e7
VO
1247static int ocelot_netdevice_lag_join(struct net_device *dev,
1248 struct net_device *bond,
1249 struct netdev_lag_upper_info *info,
1250 struct netlink_ext_ack *extack)
9c90eea3
VO
1251{
1252 struct ocelot_port_private *priv = netdev_priv(dev);
1253 struct ocelot_port *ocelot_port = &priv->port;
1254 struct ocelot *ocelot = ocelot_port->ocelot;
81ef35e7 1255 struct net_device *bridge_dev;
9c90eea3 1256 int port = priv->chip_port;
81ef35e7
VO
1257 int err;
1258
1259 err = ocelot_port_lag_join(ocelot, port, bond, info);
1260 if (err == -EOPNOTSUPP) {
1261 NL_SET_ERR_MSG_MOD(extack, "Offloading not supported");
1262 return 0;
1263 }
1264
1265 bridge_dev = netdev_master_upper_dev_get(bond);
1266 if (!bridge_dev || !netif_is_bridge_master(bridge_dev))
1267 return 0;
1268
e4bd44e8 1269 err = ocelot_netdevice_bridge_join(dev, bond, bridge_dev, extack);
81ef35e7
VO
1270 if (err)
1271 goto err_bridge_join;
1272
1273 return 0;
1274
1275err_bridge_join:
1276 ocelot_port_lag_leave(ocelot, port, bond);
1277 return err;
1278}
1279
2f5dc00f
VO
1280static void ocelot_netdevice_pre_lag_leave(struct net_device *dev,
1281 struct net_device *bond)
1282{
1283 struct net_device *bridge_dev;
1284
1285 bridge_dev = netdev_master_upper_dev_get(bond);
1286 if (!bridge_dev || !netif_is_bridge_master(bridge_dev))
1287 return;
1288
4e51bf44 1289 ocelot_netdevice_pre_bridge_leave(dev, bond);
2f5dc00f
VO
1290}
1291
81ef35e7
VO
1292static int ocelot_netdevice_lag_leave(struct net_device *dev,
1293 struct net_device *bond)
1294{
1295 struct ocelot_port_private *priv = netdev_priv(dev);
1296 struct ocelot_port *ocelot_port = &priv->port;
1297 struct ocelot *ocelot = ocelot_port->ocelot;
1298 struct net_device *bridge_dev;
1299 int port = priv->chip_port;
1300
1301 ocelot_port_lag_leave(ocelot, port, bond);
1302
1303 bridge_dev = netdev_master_upper_dev_get(bond);
1304 if (!bridge_dev || !netif_is_bridge_master(bridge_dev))
1305 return 0;
1306
e4bd44e8 1307 return ocelot_netdevice_bridge_leave(dev, bond, bridge_dev);
81ef35e7
VO
1308}
1309
1310static int ocelot_netdevice_changeupper(struct net_device *dev,
e56c6bbd 1311 struct net_device *brport_dev,
81ef35e7
VO
1312 struct netdev_notifier_changeupper_info *info)
1313{
1314 struct netlink_ext_ack *extack;
9c90eea3
VO
1315 int err = 0;
1316
81ef35e7
VO
1317 extack = netdev_notifier_info_to_extack(&info->info);
1318
662981bb 1319 if (netif_is_bridge_master(info->upper_dev)) {
81ef35e7 1320 if (info->linking)
e56c6bbd 1321 err = ocelot_netdevice_bridge_join(dev, brport_dev,
e4bd44e8 1322 info->upper_dev,
81ef35e7
VO
1323 extack);
1324 else
e56c6bbd 1325 err = ocelot_netdevice_bridge_leave(dev, brport_dev,
e4bd44e8 1326 info->upper_dev);
662981bb
VO
1327 }
1328 if (netif_is_lag_master(info->upper_dev)) {
81ef35e7
VO
1329 if (info->linking)
1330 err = ocelot_netdevice_lag_join(dev, info->upper_dev,
1331 info->upper_info, extack);
1332 else
1333 ocelot_netdevice_lag_leave(dev, info->upper_dev);
9c90eea3
VO
1334 }
1335
41e66fa2
VO
1336 return notifier_from_errno(err);
1337}
1338
81ef35e7
VO
1339/* Treat CHANGEUPPER events on an offloaded LAG as individual CHANGEUPPER
1340 * events for the lower physical ports of the LAG.
1341 * If the LAG upper isn't offloaded, ignore its CHANGEUPPER events.
1342 * In case the LAG joined a bridge, notify that we are offloading it and can do
1343 * forwarding in hardware towards it.
1344 */
41e66fa2
VO
1345static int
1346ocelot_netdevice_lag_changeupper(struct net_device *dev,
1347 struct netdev_notifier_changeupper_info *info)
1348{
1349 struct net_device *lower;
1350 struct list_head *iter;
1351 int err = NOTIFY_DONE;
1352
1353 netdev_for_each_lower_dev(dev, lower, iter) {
81ef35e7
VO
1354 struct ocelot_port_private *priv = netdev_priv(lower);
1355 struct ocelot_port *ocelot_port = &priv->port;
1356
1357 if (ocelot_port->bond != dev)
1358 return NOTIFY_OK;
1359
e56c6bbd 1360 err = ocelot_netdevice_changeupper(lower, dev, info);
41e66fa2
VO
1361 if (err)
1362 return notifier_from_errno(err);
1363 }
1364
1365 return NOTIFY_DONE;
9c90eea3
VO
1366}
1367
2f5dc00f
VO
1368static int
1369ocelot_netdevice_prechangeupper(struct net_device *dev,
1370 struct net_device *brport_dev,
1371 struct netdev_notifier_changeupper_info *info)
1372{
1373 if (netif_is_bridge_master(info->upper_dev) && !info->linking)
4e51bf44 1374 ocelot_netdevice_pre_bridge_leave(dev, brport_dev);
2f5dc00f
VO
1375
1376 if (netif_is_lag_master(info->upper_dev) && !info->linking)
1377 ocelot_netdevice_pre_lag_leave(dev, info->upper_dev);
1378
1379 return NOTIFY_DONE;
1380}
1381
1382static int
1383ocelot_netdevice_lag_prechangeupper(struct net_device *dev,
1384 struct netdev_notifier_changeupper_info *info)
1385{
1386 struct net_device *lower;
1387 struct list_head *iter;
1388 int err = NOTIFY_DONE;
1389
1390 netdev_for_each_lower_dev(dev, lower, iter) {
1391 struct ocelot_port_private *priv = netdev_priv(lower);
1392 struct ocelot_port *ocelot_port = &priv->port;
1393
1394 if (ocelot_port->bond != dev)
1395 return NOTIFY_OK;
1396
1397 err = ocelot_netdevice_prechangeupper(dev, lower, info);
1398 if (err)
1399 return err;
1400 }
1401
1402 return NOTIFY_DONE;
1403}
1404
23ca3b72
VO
1405static int
1406ocelot_netdevice_changelowerstate(struct net_device *dev,
1407 struct netdev_lag_lower_state_info *info)
1408{
1409 struct ocelot_port_private *priv = netdev_priv(dev);
1410 bool is_active = info->link_up && info->tx_enabled;
1411 struct ocelot_port *ocelot_port = &priv->port;
1412 struct ocelot *ocelot = ocelot_port->ocelot;
1413 int port = priv->chip_port;
1414
1415 if (!ocelot_port->bond)
1416 return NOTIFY_DONE;
1417
1418 if (ocelot_port->lag_tx_active == is_active)
1419 return NOTIFY_DONE;
1420
1421 ocelot_port_lag_change(ocelot, port, is_active);
1422
1423 return NOTIFY_OK;
1424}
1425
9c90eea3
VO
1426static int ocelot_netdevice_event(struct notifier_block *unused,
1427 unsigned long event, void *ptr)
1428{
9c90eea3 1429 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
9c90eea3 1430
41e66fa2 1431 switch (event) {
2f5dc00f
VO
1432 case NETDEV_PRECHANGEUPPER: {
1433 struct netdev_notifier_changeupper_info *info = ptr;
1434
1435 if (ocelot_netdevice_dev_check(dev))
1436 return ocelot_netdevice_prechangeupper(dev, dev, info);
1437
1438 if (netif_is_lag_master(dev))
1439 return ocelot_netdevice_lag_prechangeupper(dev, info);
1440
1441 break;
1442 }
41e66fa2
VO
1443 case NETDEV_CHANGEUPPER: {
1444 struct netdev_notifier_changeupper_info *info = ptr;
9c90eea3 1445
41e66fa2 1446 if (ocelot_netdevice_dev_check(dev))
e56c6bbd 1447 return ocelot_netdevice_changeupper(dev, dev, info);
9c90eea3 1448
41e66fa2
VO
1449 if (netif_is_lag_master(dev))
1450 return ocelot_netdevice_lag_changeupper(dev, info);
1451
1452 break;
1453 }
23ca3b72
VO
1454 case NETDEV_CHANGELOWERSTATE: {
1455 struct netdev_notifier_changelowerstate_info *info = ptr;
1456
1457 if (!ocelot_netdevice_dev_check(dev))
1458 break;
1459
1460 return ocelot_netdevice_changelowerstate(dev,
1461 info->lower_state_info);
1462 }
41e66fa2
VO
1463 default:
1464 break;
9c90eea3
VO
1465 }
1466
41e66fa2 1467 return NOTIFY_DONE;
9c90eea3
VO
1468}
1469
1470struct notifier_block ocelot_netdevice_nb __read_mostly = {
1471 .notifier_call = ocelot_netdevice_event,
1472};
9c90eea3
VO
1473
1474static int ocelot_switchdev_event(struct notifier_block *unused,
1475 unsigned long event, void *ptr)
1476{
1477 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
1478 int err;
1479
1480 switch (event) {
1481 case SWITCHDEV_PORT_ATTR_SET:
1482 err = switchdev_handle_port_attr_set(dev, ptr,
1483 ocelot_netdevice_dev_check,
1484 ocelot_port_attr_set);
1485 return notifier_from_errno(err);
1486 }
1487
1488 return NOTIFY_DONE;
1489}
1490
1491struct notifier_block ocelot_switchdev_nb __read_mostly = {
1492 .notifier_call = ocelot_switchdev_event,
1493};
9c90eea3
VO
1494
1495static int ocelot_switchdev_blocking_event(struct notifier_block *unused,
1496 unsigned long event, void *ptr)
1497{
1498 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
1499 int err;
1500
1501 switch (event) {
1502 /* Blocking events. */
1503 case SWITCHDEV_PORT_OBJ_ADD:
1504 err = switchdev_handle_port_obj_add(dev, ptr,
1505 ocelot_netdevice_dev_check,
1506 ocelot_port_obj_add);
1507 return notifier_from_errno(err);
1508 case SWITCHDEV_PORT_OBJ_DEL:
1509 err = switchdev_handle_port_obj_del(dev, ptr,
1510 ocelot_netdevice_dev_check,
1511 ocelot_port_obj_del);
1512 return notifier_from_errno(err);
1513 case SWITCHDEV_PORT_ATTR_SET:
1514 err = switchdev_handle_port_attr_set(dev, ptr,
1515 ocelot_netdevice_dev_check,
1516 ocelot_port_attr_set);
1517 return notifier_from_errno(err);
1518 }
1519
1520 return NOTIFY_DONE;
1521}
1522
1523struct notifier_block ocelot_switchdev_blocking_nb __read_mostly = {
1524 .notifier_call = ocelot_switchdev_blocking_event,
1525};
9c90eea3 1526
91c724cf 1527int ocelot_probe_port(struct ocelot *ocelot, int port, struct regmap *target,
9c90eea3
VO
1528 struct phy_device *phy)
1529{
1530 struct ocelot_port_private *priv;
1531 struct ocelot_port *ocelot_port;
1532 struct net_device *dev;
1533 int err;
1534
1535 dev = alloc_etherdev(sizeof(struct ocelot_port_private));
1536 if (!dev)
1537 return -ENOMEM;
1538 SET_NETDEV_DEV(dev, ocelot->dev);
1539 priv = netdev_priv(dev);
1540 priv->dev = dev;
1541 priv->phy = phy;
1542 priv->chip_port = port;
1543 ocelot_port = &priv->port;
1544 ocelot_port->ocelot = ocelot;
91c724cf 1545 ocelot_port->target = target;
9c90eea3
VO
1546 ocelot->ports[port] = ocelot_port;
1547
1548 dev->netdev_ops = &ocelot_port_netdev_ops;
1549 dev->ethtool_ops = &ocelot_ethtool_ops;
1550
1551 dev->hw_features |= NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_RXFCS |
1552 NETIF_F_HW_TC;
1553 dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_TC;
1554
1555 memcpy(dev->dev_addr, ocelot->base_mac, ETH_ALEN);
1556 dev->dev_addr[ETH_ALEN - 1] += port;
c3e58a75
VO
1557 ocelot_mact_learn(ocelot, PGID_CPU, dev->dev_addr,
1558 ocelot_port->pvid_vlan.vid, ENTRYTYPE_LOCKED);
9c90eea3
VO
1559
1560 ocelot_init_port(ocelot, port);
1561
1562 err = register_netdev(dev);
1563 if (err) {
1564 dev_err(ocelot->dev, "register_netdev failed\n");
1565 free_netdev(dev);
e0c16233
DC
1566 ocelot->ports[port] = NULL;
1567 return err;
9c90eea3
VO
1568 }
1569
e0c16233
DC
1570 return 0;
1571}
1572
1573void ocelot_release_port(struct ocelot_port *ocelot_port)
1574{
1575 struct ocelot_port_private *priv = container_of(ocelot_port,
1576 struct ocelot_port_private,
1577 port);
1578
1579 unregister_netdev(priv->dev);
1580 free_netdev(priv->dev);
9c90eea3 1581}