blob: e99c8fb3cb15748a2a35507af1e3ee06d9ab0cf8 [file] [log] [blame]
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001// SPDX-License-Identifier: (GPL-2.0 OR MIT)
2/* Microsemi Ocelot Switch driver
3 *
Vladimir Olteanf59fd9c2021-01-15 04:11:20 +02004 * This contains glue logic between the switchdev driver operations and the
5 * mscc_ocelot_switch_lib.
6 *
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03007 * Copyright (c) 2017, 2019 Microsemi Corporation
Vladimir Olteanf59fd9c2021-01-15 04:11:20 +02008 * Copyright 2020-2021 NXP Semiconductors
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03009 */
10
11#include <linux/if_bridge.h>
Vladimir Oltean0e9bb4e2021-01-29 03:00:00 +020012#include <net/pkt_cls.h>
Vladimir Oltean9c90eea2020-06-20 18:43:44 +030013#include "ocelot.h"
Vladimir Oltean3c836542020-06-20 18:43:45 +030014#include "ocelot_vcap.h"
Vladimir Oltean9c90eea2020-06-20 18:43:44 +030015
Vladimir Olteanf59fd9c2021-01-15 04:11:20 +020016static 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
Vladimir Oltean6c30384e2021-01-15 04:11:18 +0200142const struct devlink_ops ocelot_devlink_ops = {
Vladimir Olteanf59fd9c2021-01-15 04:11:20 +0200143 .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,
Vladimir Oltean6c30384e2021-01-15 04:11:18 +0200153};
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
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300189int 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
Baowen Zheng6a56e192021-03-12 15:08:30 +0100254 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
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300260 pol.rate = (u32)div_u64(action->police.rate_bytes_ps, 1000) * 8;
Po Liu5f035af2020-06-29 14:54:16 +0800261 pol.burst = action->police.burst;
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300262
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
Vladimir Oltean2f0402f2020-10-31 12:29:15 +0200393static 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
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300404static 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;
452 struct ocelot *ocelot = ocelot_port->ocelot;
453 int port = priv->chip_port;
454 int err;
455
456 if (priv->serdes) {
457 err = phy_set_mode_ext(priv->serdes, PHY_MODE_ETHERNET,
458 ocelot_port->phy_mode);
459 if (err) {
460 netdev_err(dev, "Could not set mode of SerDes\n");
461 return err;
462 }
463 }
464
465 err = phy_connect_direct(dev, priv->phy, &ocelot_port_adjust_link,
466 ocelot_port->phy_mode);
467 if (err) {
468 netdev_err(dev, "Could not attach to PHY\n");
469 return err;
470 }
471
472 dev->phydev = priv->phy;
473
474 phy_attached_info(priv->phy);
475 phy_start(priv->phy);
476
477 ocelot_port_enable(ocelot, port, priv->phy);
478
479 return 0;
480}
481
482static int ocelot_port_stop(struct net_device *dev)
483{
484 struct ocelot_port_private *priv = netdev_priv(dev);
485 struct ocelot *ocelot = priv->port.ocelot;
486 int port = priv->chip_port;
487
488 phy_disconnect(priv->phy);
489
490 dev->phydev = NULL;
491
492 ocelot_port_disable(ocelot, port);
493
494 return 0;
495}
496
Vladimir Oltean137ffbc2021-02-14 00:37:54 +0200497static netdev_tx_t ocelot_port_xmit(struct sk_buff *skb, struct net_device *dev)
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300498{
499 struct ocelot_port_private *priv = netdev_priv(dev);
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300500 struct ocelot_port *ocelot_port = &priv->port;
501 struct ocelot *ocelot = ocelot_port->ocelot;
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300502 int port = priv->chip_port;
Vladimir Oltean137ffbc2021-02-14 00:37:54 +0200503 u32 rew_op = 0;
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300504
Vladimir Oltean137ffbc2021-02-14 00:37:54 +0200505 if (!ocelot_can_inject(ocelot, 0))
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300506 return NETDEV_TX_BUSY;
507
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300508 /* Check if timestamping is needed */
Vladimir Oltean137ffbc2021-02-14 00:37:54 +0200509 if (ocelot->ptp && (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) {
Yangbo Lu682eaad2021-04-27 12:22:02 +0800510 struct sk_buff *clone = NULL;
511
512 if (ocelot_port_txtstamp_request(ocelot, port, skb, &clone)) {
513 kfree_skb(skb);
514 return NETDEV_TX_OK;
515 }
Vladimir Olteane2f9a8f2020-09-23 14:24:20 +0300516
517 if (ocelot_port->ptp_cmd == IFH_REW_OP_TWO_STEP_PTP) {
Yangbo Lu682eaad2021-04-27 12:22:02 +0800518 rew_op = ocelot_port->ptp_cmd;
Yangbo Luc4b364c2021-04-27 12:22:00 +0800519 rew_op |= OCELOT_SKB_CB(clone)->ts_id << 3;
Vladimir Olteane2f9a8f2020-09-23 14:24:20 +0300520 }
521 }
Vladimir Oltean9dda66a2020-09-18 04:07:23 +0300522
Vladimir Oltean137ffbc2021-02-14 00:37:54 +0200523 ocelot_port_inject_frame(ocelot, port, 0, rew_op, skb);
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300524
Vladimir Olteane2f9a8f2020-09-23 14:24:20 +0300525 kfree_skb(skb);
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300526
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300527 return NETDEV_TX_OK;
528}
529
Vladimir Olteanca0b2722020-12-12 21:16:12 +0200530enum ocelot_action_type {
531 OCELOT_MACT_LEARN,
532 OCELOT_MACT_FORGET,
533};
534
535struct ocelot_mact_work_ctx {
536 struct work_struct work;
537 struct ocelot *ocelot;
538 enum ocelot_action_type type;
539 union {
540 /* OCELOT_MACT_LEARN */
541 struct {
542 unsigned char addr[ETH_ALEN];
543 u16 vid;
544 enum macaccess_entry_type entry_type;
545 int pgid;
546 } learn;
547 /* OCELOT_MACT_FORGET */
548 struct {
549 unsigned char addr[ETH_ALEN];
550 u16 vid;
551 } forget;
552 };
553};
554
555#define ocelot_work_to_ctx(x) \
556 container_of((x), struct ocelot_mact_work_ctx, work)
557
558static void ocelot_mact_work(struct work_struct *work)
559{
560 struct ocelot_mact_work_ctx *w = ocelot_work_to_ctx(work);
561 struct ocelot *ocelot = w->ocelot;
562
563 switch (w->type) {
564 case OCELOT_MACT_LEARN:
565 ocelot_mact_learn(ocelot, w->learn.pgid, w->learn.addr,
566 w->learn.vid, w->learn.entry_type);
567 break;
568 case OCELOT_MACT_FORGET:
569 ocelot_mact_forget(ocelot, w->forget.addr, w->forget.vid);
570 break;
571 default:
572 break;
Xu Wang20efd2c2021-01-15 09:55:44 +0000573 }
Vladimir Olteanca0b2722020-12-12 21:16:12 +0200574
575 kfree(w);
576}
577
578static int ocelot_enqueue_mact_action(struct ocelot *ocelot,
579 const struct ocelot_mact_work_ctx *ctx)
580{
581 struct ocelot_mact_work_ctx *w = kmemdup(ctx, sizeof(*w), GFP_ATOMIC);
582
583 if (!w)
584 return -ENOMEM;
585
586 w->ocelot = ocelot;
587 INIT_WORK(&w->work, ocelot_mact_work);
588 queue_work(ocelot->owq, &w->work);
589
590 return 0;
591}
592
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300593static int ocelot_mc_unsync(struct net_device *dev, const unsigned char *addr)
594{
595 struct ocelot_port_private *priv = netdev_priv(dev);
596 struct ocelot_port *ocelot_port = &priv->port;
597 struct ocelot *ocelot = ocelot_port->ocelot;
Vladimir Olteanca0b2722020-12-12 21:16:12 +0200598 struct ocelot_mact_work_ctx w;
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300599
Vladimir Olteanca0b2722020-12-12 21:16:12 +0200600 ether_addr_copy(w.forget.addr, addr);
601 w.forget.vid = ocelot_port->pvid_vlan.vid;
602 w.type = OCELOT_MACT_FORGET;
603
604 return ocelot_enqueue_mact_action(ocelot, &w);
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300605}
606
607static int ocelot_mc_sync(struct net_device *dev, const unsigned char *addr)
608{
609 struct ocelot_port_private *priv = netdev_priv(dev);
610 struct ocelot_port *ocelot_port = &priv->port;
611 struct ocelot *ocelot = ocelot_port->ocelot;
Vladimir Olteanca0b2722020-12-12 21:16:12 +0200612 struct ocelot_mact_work_ctx w;
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300613
Vladimir Olteanca0b2722020-12-12 21:16:12 +0200614 ether_addr_copy(w.learn.addr, addr);
615 w.learn.vid = ocelot_port->pvid_vlan.vid;
616 w.learn.pgid = PGID_CPU;
617 w.learn.entry_type = ENTRYTYPE_LOCKED;
618 w.type = OCELOT_MACT_LEARN;
619
620 return ocelot_enqueue_mact_action(ocelot, &w);
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300621}
622
623static void ocelot_set_rx_mode(struct net_device *dev)
624{
625 struct ocelot_port_private *priv = netdev_priv(dev);
626 struct ocelot *ocelot = priv->port.ocelot;
627 u32 val;
628 int i;
629
630 /* This doesn't handle promiscuous mode because the bridge core is
631 * setting IFF_PROMISC on all slave interfaces and all frames would be
632 * forwarded to the CPU port.
633 */
634 val = GENMASK(ocelot->num_phys_ports - 1, 0);
Vladimir Oltean96b029b2020-06-21 14:46:02 +0300635 for_each_nonreserved_multicast_dest_pgid(ocelot, i)
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300636 ocelot_write_rix(ocelot, val, ANA_PGID_PGID, i);
637
638 __dev_mc_sync(dev, ocelot_mc_sync, ocelot_mc_unsync);
639}
640
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300641static int ocelot_port_set_mac_address(struct net_device *dev, void *p)
642{
643 struct ocelot_port_private *priv = netdev_priv(dev);
644 struct ocelot_port *ocelot_port = &priv->port;
645 struct ocelot *ocelot = ocelot_port->ocelot;
646 const struct sockaddr *addr = p;
647
648 /* Learn the new net device MAC address in the mac table. */
Vladimir Olteanc3e58a752020-10-31 12:29:12 +0200649 ocelot_mact_learn(ocelot, PGID_CPU, addr->sa_data,
650 ocelot_port->pvid_vlan.vid, ENTRYTYPE_LOCKED);
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300651 /* Then forget the previous one. */
Vladimir Olteanc3e58a752020-10-31 12:29:12 +0200652 ocelot_mact_forget(ocelot, dev->dev_addr, ocelot_port->pvid_vlan.vid);
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300653
654 ether_addr_copy(dev->dev_addr, addr->sa_data);
655 return 0;
656}
657
658static void ocelot_get_stats64(struct net_device *dev,
659 struct rtnl_link_stats64 *stats)
660{
661 struct ocelot_port_private *priv = netdev_priv(dev);
662 struct ocelot *ocelot = priv->port.ocelot;
663 int port = priv->chip_port;
664
665 /* Configure the port to read the stats from */
666 ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(port),
667 SYS_STAT_CFG);
668
669 /* Get Rx stats */
670 stats->rx_bytes = ocelot_read(ocelot, SYS_COUNT_RX_OCTETS);
671 stats->rx_packets = ocelot_read(ocelot, SYS_COUNT_RX_SHORTS) +
672 ocelot_read(ocelot, SYS_COUNT_RX_FRAGMENTS) +
673 ocelot_read(ocelot, SYS_COUNT_RX_JABBERS) +
674 ocelot_read(ocelot, SYS_COUNT_RX_LONGS) +
675 ocelot_read(ocelot, SYS_COUNT_RX_64) +
676 ocelot_read(ocelot, SYS_COUNT_RX_65_127) +
677 ocelot_read(ocelot, SYS_COUNT_RX_128_255) +
678 ocelot_read(ocelot, SYS_COUNT_RX_256_1023) +
679 ocelot_read(ocelot, SYS_COUNT_RX_1024_1526) +
680 ocelot_read(ocelot, SYS_COUNT_RX_1527_MAX);
681 stats->multicast = ocelot_read(ocelot, SYS_COUNT_RX_MULTICAST);
682 stats->rx_dropped = dev->stats.rx_dropped;
683
684 /* Get Tx stats */
685 stats->tx_bytes = ocelot_read(ocelot, SYS_COUNT_TX_OCTETS);
686 stats->tx_packets = ocelot_read(ocelot, SYS_COUNT_TX_64) +
687 ocelot_read(ocelot, SYS_COUNT_TX_65_127) +
688 ocelot_read(ocelot, SYS_COUNT_TX_128_511) +
689 ocelot_read(ocelot, SYS_COUNT_TX_512_1023) +
690 ocelot_read(ocelot, SYS_COUNT_TX_1024_1526) +
691 ocelot_read(ocelot, SYS_COUNT_TX_1527_MAX);
692 stats->tx_dropped = ocelot_read(ocelot, SYS_COUNT_TX_DROPS) +
693 ocelot_read(ocelot, SYS_COUNT_TX_AGING);
694 stats->collisions = ocelot_read(ocelot, SYS_COUNT_TX_COLLISION);
695}
696
697static int ocelot_port_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
698 struct net_device *dev,
699 const unsigned char *addr,
700 u16 vid, u16 flags,
701 struct netlink_ext_ack *extack)
702{
703 struct ocelot_port_private *priv = netdev_priv(dev);
704 struct ocelot *ocelot = priv->port.ocelot;
705 int port = priv->chip_port;
706
707 return ocelot_fdb_add(ocelot, port, addr, vid);
708}
709
710static int ocelot_port_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
711 struct net_device *dev,
712 const unsigned char *addr, u16 vid)
713{
714 struct ocelot_port_private *priv = netdev_priv(dev);
715 struct ocelot *ocelot = priv->port.ocelot;
716 int port = priv->chip_port;
717
718 return ocelot_fdb_del(ocelot, port, addr, vid);
719}
720
721static int ocelot_port_fdb_dump(struct sk_buff *skb,
722 struct netlink_callback *cb,
723 struct net_device *dev,
724 struct net_device *filter_dev, int *idx)
725{
726 struct ocelot_port_private *priv = netdev_priv(dev);
727 struct ocelot *ocelot = priv->port.ocelot;
728 struct ocelot_dump_ctx dump = {
729 .dev = dev,
730 .skb = skb,
731 .cb = cb,
732 .idx = *idx,
733 };
734 int port = priv->chip_port;
735 int ret;
736
737 ret = ocelot_fdb_dump(ocelot, port, ocelot_port_fdb_do_dump, &dump);
738
739 *idx = dump.idx;
740
741 return ret;
742}
743
744static int ocelot_vlan_rx_add_vid(struct net_device *dev, __be16 proto,
745 u16 vid)
746{
747 return ocelot_vlan_vid_add(dev, vid, false, false);
748}
749
750static int ocelot_vlan_rx_kill_vid(struct net_device *dev, __be16 proto,
751 u16 vid)
752{
753 return ocelot_vlan_vid_del(dev, vid);
754}
755
756static void ocelot_vlan_mode(struct ocelot *ocelot, int port,
757 netdev_features_t features)
758{
759 u32 val;
760
761 /* Filtering */
762 val = ocelot_read(ocelot, ANA_VLANMASK);
763 if (features & NETIF_F_HW_VLAN_CTAG_FILTER)
764 val |= BIT(port);
765 else
766 val &= ~BIT(port);
767 ocelot_write(ocelot, val, ANA_VLANMASK);
768}
769
770static int ocelot_set_features(struct net_device *dev,
771 netdev_features_t features)
772{
773 netdev_features_t changed = dev->features ^ features;
774 struct ocelot_port_private *priv = netdev_priv(dev);
775 struct ocelot *ocelot = priv->port.ocelot;
776 int port = priv->chip_port;
777
778 if ((dev->features & NETIF_F_HW_TC) > (features & NETIF_F_HW_TC) &&
779 priv->tc.offload_cnt) {
780 netdev_err(dev,
781 "Cannot disable HW TC offload while offloads active\n");
782 return -EBUSY;
783 }
784
785 if (changed & NETIF_F_HW_VLAN_CTAG_FILTER)
786 ocelot_vlan_mode(ocelot, port, features);
787
788 return 0;
789}
790
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300791static int ocelot_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
792{
793 struct ocelot_port_private *priv = netdev_priv(dev);
794 struct ocelot *ocelot = priv->port.ocelot;
795 int port = priv->chip_port;
796
797 /* If the attached PHY device isn't capable of timestamping operations,
798 * use our own (when possible).
799 */
800 if (!phy_has_hwtstamp(dev->phydev) && ocelot->ptp) {
801 switch (cmd) {
802 case SIOCSHWTSTAMP:
803 return ocelot_hwstamp_set(ocelot, port, ifr);
804 case SIOCGHWTSTAMP:
805 return ocelot_hwstamp_get(ocelot, port, ifr);
806 }
807 }
808
809 return phy_mii_ioctl(dev->phydev, ifr, cmd);
810}
811
812static const struct net_device_ops ocelot_port_netdev_ops = {
813 .ndo_open = ocelot_port_open,
814 .ndo_stop = ocelot_port_stop,
815 .ndo_start_xmit = ocelot_port_xmit,
816 .ndo_set_rx_mode = ocelot_set_rx_mode,
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300817 .ndo_set_mac_address = ocelot_port_set_mac_address,
818 .ndo_get_stats64 = ocelot_get_stats64,
819 .ndo_fdb_add = ocelot_port_fdb_add,
820 .ndo_fdb_del = ocelot_port_fdb_del,
821 .ndo_fdb_dump = ocelot_port_fdb_dump,
822 .ndo_vlan_rx_add_vid = ocelot_vlan_rx_add_vid,
823 .ndo_vlan_rx_kill_vid = ocelot_vlan_rx_kill_vid,
824 .ndo_set_features = ocelot_set_features,
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300825 .ndo_setup_tc = ocelot_setup_tc,
826 .ndo_do_ioctl = ocelot_ioctl,
Vladimir Oltean6c30384e2021-01-15 04:11:18 +0200827 .ndo_get_devlink_port = ocelot_get_devlink_port,
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300828};
829
Vladimir Oltean319e4dd2020-10-02 15:02:21 +0300830struct net_device *ocelot_port_to_netdev(struct ocelot *ocelot, int port)
831{
832 struct ocelot_port *ocelot_port = ocelot->ports[port];
833 struct ocelot_port_private *priv;
834
835 if (!ocelot_port)
836 return NULL;
837
838 priv = container_of(ocelot_port, struct ocelot_port_private, port);
839
840 return priv->dev;
841}
842
Vladimir Oltean7e38b032020-10-11 12:20:41 +0300843/* Checks if the net_device instance given to us originates from our driver */
844static bool ocelot_netdevice_dev_check(const struct net_device *dev)
Vladimir Oltean319e4dd2020-10-02 15:02:21 +0300845{
846 return dev->netdev_ops == &ocelot_port_netdev_ops;
847}
848
849int ocelot_netdev_to_port(struct net_device *dev)
850{
851 struct ocelot_port_private *priv;
852
Vladimir Oltean7e38b032020-10-11 12:20:41 +0300853 if (!dev || !ocelot_netdevice_dev_check(dev))
Vladimir Oltean319e4dd2020-10-02 15:02:21 +0300854 return -EINVAL;
855
856 priv = netdev_priv(dev);
857
858 return priv->chip_port;
859}
860
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300861static void ocelot_port_get_strings(struct net_device *netdev, u32 sset,
862 u8 *data)
863{
864 struct ocelot_port_private *priv = netdev_priv(netdev);
865 struct ocelot *ocelot = priv->port.ocelot;
866 int port = priv->chip_port;
867
868 ocelot_get_strings(ocelot, port, sset, data);
869}
870
871static void ocelot_port_get_ethtool_stats(struct net_device *dev,
872 struct ethtool_stats *stats,
873 u64 *data)
874{
875 struct ocelot_port_private *priv = netdev_priv(dev);
876 struct ocelot *ocelot = priv->port.ocelot;
877 int port = priv->chip_port;
878
879 ocelot_get_ethtool_stats(ocelot, port, data);
880}
881
882static int ocelot_port_get_sset_count(struct net_device *dev, int sset)
883{
884 struct ocelot_port_private *priv = netdev_priv(dev);
885 struct ocelot *ocelot = priv->port.ocelot;
886 int port = priv->chip_port;
887
888 return ocelot_get_sset_count(ocelot, port, sset);
889}
890
891static int ocelot_port_get_ts_info(struct net_device *dev,
892 struct ethtool_ts_info *info)
893{
894 struct ocelot_port_private *priv = netdev_priv(dev);
895 struct ocelot *ocelot = priv->port.ocelot;
896 int port = priv->chip_port;
897
898 if (!ocelot->ptp)
899 return ethtool_op_get_ts_info(dev, info);
900
901 return ocelot_get_ts_info(ocelot, port, info);
902}
903
904static const struct ethtool_ops ocelot_ethtool_ops = {
905 .get_strings = ocelot_port_get_strings,
906 .get_ethtool_stats = ocelot_port_get_ethtool_stats,
907 .get_sset_count = ocelot_port_get_sset_count,
908 .get_link_ksettings = phy_ethtool_get_link_ksettings,
909 .set_link_ksettings = phy_ethtool_set_link_ksettings,
910 .get_ts_info = ocelot_port_get_ts_info,
911};
912
913static void ocelot_port_attr_stp_state_set(struct ocelot *ocelot, int port,
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300914 u8 state)
915{
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300916 ocelot_bridge_stp_state_set(ocelot, port, state);
917}
918
919static void ocelot_port_attr_ageing_set(struct ocelot *ocelot, int port,
920 unsigned long ageing_clock_t)
921{
922 unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock_t);
923 u32 ageing_time = jiffies_to_msecs(ageing_jiffies);
924
925 ocelot_set_ageing_time(ocelot, ageing_time);
926}
927
928static void ocelot_port_attr_mc_set(struct ocelot *ocelot, int port, bool mc)
929{
930 u32 cpu_fwd_mcast = ANA_PORT_CPU_FWD_CFG_CPU_IGMP_REDIR_ENA |
931 ANA_PORT_CPU_FWD_CFG_CPU_MLD_REDIR_ENA |
932 ANA_PORT_CPU_FWD_CFG_CPU_IPMC_CTRL_COPY_ENA;
933 u32 val = 0;
934
935 if (mc)
936 val = cpu_fwd_mcast;
937
938 ocelot_rmw_gix(ocelot, val, cpu_fwd_mcast,
939 ANA_PORT_CPU_FWD_CFG, port);
940}
941
942static int ocelot_port_attr_set(struct net_device *dev,
Vladimir Oltean4c08c582021-02-12 17:15:51 +0200943 const struct switchdev_attr *attr,
944 struct netlink_ext_ack *extack)
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300945{
946 struct ocelot_port_private *priv = netdev_priv(dev);
947 struct ocelot *ocelot = priv->port.ocelot;
948 int port = priv->chip_port;
949 int err = 0;
950
951 switch (attr->id) {
952 case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
Vladimir Olteanbae33f22021-01-09 02:01:50 +0200953 ocelot_port_attr_stp_state_set(ocelot, port, attr->u.stp_state);
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300954 break;
955 case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
956 ocelot_port_attr_ageing_set(ocelot, port, attr->u.ageing_time);
957 break;
958 case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
Vladimir Olteanbae33f22021-01-09 02:01:50 +0200959 ocelot_port_vlan_filtering(ocelot, port, attr->u.vlan_filtering);
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300960 break;
961 case SWITCHDEV_ATTR_ID_BRIDGE_MC_DISABLED:
962 ocelot_port_attr_mc_set(ocelot, port, !attr->u.mc_disabled);
963 break;
Vladimir Oltean421741e2021-02-12 17:15:59 +0200964 case SWITCHDEV_ATTR_ID_PORT_PRE_BRIDGE_FLAGS:
965 err = ocelot_port_pre_bridge_flags(ocelot, port,
966 attr->u.brport_flags);
967 break;
968 case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS:
969 ocelot_port_bridge_flags(ocelot, port, attr->u.brport_flags);
970 break;
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300971 default:
972 err = -EOPNOTSUPP;
973 break;
974 }
975
976 return err;
977}
978
979static int ocelot_port_obj_add_vlan(struct net_device *dev,
Vladimir Olteanffb68fc2021-01-09 02:01:48 +0200980 const struct switchdev_obj_port_vlan *vlan)
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300981{
Vladimir Olteanb7a9e0d2021-01-09 02:01:46 +0200982 bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
983 bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300984 int ret;
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300985
Vladimir Olteanffb68fc2021-01-09 02:01:48 +0200986 ret = ocelot_vlan_vid_prepare(dev, vlan->vid, pvid, untagged);
987 if (ret)
988 return ret;
Vladimir Oltean2f0402f2020-10-31 12:29:15 +0200989
Vladimir Olteanffb68fc2021-01-09 02:01:48 +0200990 return ocelot_vlan_vid_add(dev, vlan->vid, pvid, untagged);
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300991}
992
Vladimir Oltean209edf92020-06-21 14:46:01 +0300993static int ocelot_port_obj_add_mdb(struct net_device *dev,
Vladimir Olteanffb68fc2021-01-09 02:01:48 +0200994 const struct switchdev_obj_port_mdb *mdb)
Vladimir Oltean209edf92020-06-21 14:46:01 +0300995{
996 struct ocelot_port_private *priv = netdev_priv(dev);
997 struct ocelot_port *ocelot_port = &priv->port;
998 struct ocelot *ocelot = ocelot_port->ocelot;
999 int port = priv->chip_port;
1000
Vladimir Oltean209edf92020-06-21 14:46:01 +03001001 return ocelot_port_mdb_add(ocelot, port, mdb);
1002}
1003
1004static int ocelot_port_obj_del_mdb(struct net_device *dev,
1005 const struct switchdev_obj_port_mdb *mdb)
1006{
1007 struct ocelot_port_private *priv = netdev_priv(dev);
1008 struct ocelot_port *ocelot_port = &priv->port;
1009 struct ocelot *ocelot = ocelot_port->ocelot;
1010 int port = priv->chip_port;
1011
1012 return ocelot_port_mdb_del(ocelot, port, mdb);
1013}
1014
Horatiu Vulturd8ea7ff2021-02-16 22:42:03 +01001015static int ocelot_port_obj_mrp_add(struct net_device *dev,
1016 const struct switchdev_obj_mrp *mrp)
1017{
1018 struct ocelot_port_private *priv = netdev_priv(dev);
1019 struct ocelot_port *ocelot_port = &priv->port;
1020 struct ocelot *ocelot = ocelot_port->ocelot;
1021 int port = priv->chip_port;
1022
1023 return ocelot_mrp_add(ocelot, port, mrp);
1024}
1025
1026static int ocelot_port_obj_mrp_del(struct net_device *dev,
1027 const struct switchdev_obj_mrp *mrp)
1028{
1029 struct ocelot_port_private *priv = netdev_priv(dev);
1030 struct ocelot_port *ocelot_port = &priv->port;
1031 struct ocelot *ocelot = ocelot_port->ocelot;
1032 int port = priv->chip_port;
1033
1034 return ocelot_mrp_del(ocelot, port, mrp);
1035}
1036
1037static int
1038ocelot_port_obj_mrp_add_ring_role(struct net_device *dev,
1039 const struct switchdev_obj_ring_role_mrp *mrp)
1040{
1041 struct ocelot_port_private *priv = netdev_priv(dev);
1042 struct ocelot_port *ocelot_port = &priv->port;
1043 struct ocelot *ocelot = ocelot_port->ocelot;
1044 int port = priv->chip_port;
1045
1046 return ocelot_mrp_add_ring_role(ocelot, port, mrp);
1047}
1048
1049static int
1050ocelot_port_obj_mrp_del_ring_role(struct net_device *dev,
1051 const struct switchdev_obj_ring_role_mrp *mrp)
1052{
1053 struct ocelot_port_private *priv = netdev_priv(dev);
1054 struct ocelot_port *ocelot_port = &priv->port;
1055 struct ocelot *ocelot = ocelot_port->ocelot;
1056 int port = priv->chip_port;
1057
1058 return ocelot_mrp_del_ring_role(ocelot, port, mrp);
1059}
1060
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001061static int ocelot_port_obj_add(struct net_device *dev,
1062 const struct switchdev_obj *obj,
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001063 struct netlink_ext_ack *extack)
1064{
1065 int ret = 0;
1066
1067 switch (obj->id) {
1068 case SWITCHDEV_OBJ_ID_PORT_VLAN:
1069 ret = ocelot_port_obj_add_vlan(dev,
Vladimir Olteanffb68fc2021-01-09 02:01:48 +02001070 SWITCHDEV_OBJ_PORT_VLAN(obj));
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001071 break;
1072 case SWITCHDEV_OBJ_ID_PORT_MDB:
Vladimir Olteanffb68fc2021-01-09 02:01:48 +02001073 ret = ocelot_port_obj_add_mdb(dev, SWITCHDEV_OBJ_PORT_MDB(obj));
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001074 break;
Horatiu Vulturd8ea7ff2021-02-16 22:42:03 +01001075 case SWITCHDEV_OBJ_ID_MRP:
1076 ret = ocelot_port_obj_mrp_add(dev, SWITCHDEV_OBJ_MRP(obj));
1077 break;
1078 case SWITCHDEV_OBJ_ID_RING_ROLE_MRP:
1079 ret = ocelot_port_obj_mrp_add_ring_role(dev,
1080 SWITCHDEV_OBJ_RING_ROLE_MRP(obj));
1081 break;
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001082 default:
1083 return -EOPNOTSUPP;
1084 }
1085
1086 return ret;
1087}
1088
1089static int ocelot_port_obj_del(struct net_device *dev,
1090 const struct switchdev_obj *obj)
1091{
1092 int ret = 0;
1093
1094 switch (obj->id) {
1095 case SWITCHDEV_OBJ_ID_PORT_VLAN:
Vladimir Olteanb7a9e0d2021-01-09 02:01:46 +02001096 ret = ocelot_vlan_vid_del(dev,
1097 SWITCHDEV_OBJ_PORT_VLAN(obj)->vid);
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001098 break;
1099 case SWITCHDEV_OBJ_ID_PORT_MDB:
1100 ret = ocelot_port_obj_del_mdb(dev, SWITCHDEV_OBJ_PORT_MDB(obj));
1101 break;
Horatiu Vulturd8ea7ff2021-02-16 22:42:03 +01001102 case SWITCHDEV_OBJ_ID_MRP:
1103 ret = ocelot_port_obj_mrp_del(dev, SWITCHDEV_OBJ_MRP(obj));
1104 break;
1105 case SWITCHDEV_OBJ_ID_RING_ROLE_MRP:
1106 ret = ocelot_port_obj_mrp_del_ring_role(dev,
1107 SWITCHDEV_OBJ_RING_ROLE_MRP(obj));
1108 break;
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001109 default:
1110 return -EOPNOTSUPP;
1111 }
1112
1113 return ret;
1114}
1115
Vladimir Olteane4bd44e2021-03-23 01:51:52 +02001116static void ocelot_inherit_brport_flags(struct ocelot *ocelot, int port,
1117 struct net_device *brport_dev)
1118{
1119 struct switchdev_brport_flags flags = {0};
1120 int flag;
1121
1122 flags.mask = BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD | BR_BCAST_FLOOD;
1123
1124 for_each_set_bit(flag, &flags.mask, 32)
1125 if (br_port_flag_is_set(brport_dev, BIT(flag)))
1126 flags.val |= BIT(flag);
1127
1128 ocelot_port_bridge_flags(ocelot, port, flags);
1129}
1130
1131static void ocelot_clear_brport_flags(struct ocelot *ocelot, int port)
1132{
1133 struct switchdev_brport_flags flags;
1134
1135 flags.mask = BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD | BR_BCAST_FLOOD;
1136 flags.val = flags.mask & ~BR_LEARNING;
1137
1138 ocelot_port_bridge_flags(ocelot, port, flags);
1139}
1140
1141static int ocelot_switchdev_sync(struct ocelot *ocelot, int port,
1142 struct net_device *brport_dev,
1143 struct net_device *bridge_dev,
1144 struct netlink_ext_ack *extack)
1145{
1146 clock_t ageing_time;
1147 u8 stp_state;
1148 int err;
1149
1150 ocelot_inherit_brport_flags(ocelot, port, brport_dev);
1151
1152 stp_state = br_port_get_stp_state(brport_dev);
1153 ocelot_bridge_stp_state_set(ocelot, port, stp_state);
1154
1155 err = ocelot_port_vlan_filtering(ocelot, port,
1156 br_vlan_enabled(bridge_dev));
1157 if (err)
1158 return err;
1159
1160 ageing_time = br_get_ageing_time(bridge_dev);
1161 ocelot_port_attr_ageing_set(ocelot, port, ageing_time);
1162
1163 err = br_mdb_replay(bridge_dev, brport_dev,
1164 &ocelot_switchdev_blocking_nb, extack);
1165 if (err && err != -EOPNOTSUPP)
1166 return err;
1167
1168 err = br_fdb_replay(bridge_dev, brport_dev, &ocelot_switchdev_nb);
1169 if (err)
1170 return err;
1171
1172 err = br_vlan_replay(bridge_dev, brport_dev,
1173 &ocelot_switchdev_blocking_nb, extack);
1174 if (err && err != -EOPNOTSUPP)
1175 return err;
1176
1177 return 0;
1178}
1179
1180static int ocelot_switchdev_unsync(struct ocelot *ocelot, int port)
1181{
1182 int err;
1183
1184 err = ocelot_port_vlan_filtering(ocelot, port, false);
1185 if (err)
1186 return err;
1187
1188 ocelot_clear_brport_flags(ocelot, port);
1189
1190 ocelot_bridge_stp_state_set(ocelot, port, BR_STATE_FORWARDING);
1191
1192 return 0;
1193}
1194
Vladimir Oltean81ef35e2021-03-23 01:51:51 +02001195static int ocelot_netdevice_bridge_join(struct net_device *dev,
Vladimir Olteane4bd44e2021-03-23 01:51:52 +02001196 struct net_device *brport_dev,
Vladimir Oltean81ef35e2021-03-23 01:51:51 +02001197 struct net_device *bridge,
1198 struct netlink_ext_ack *extack)
Vladimir Oltean421741e2021-02-12 17:15:59 +02001199{
Vladimir Oltean81ef35e2021-03-23 01:51:51 +02001200 struct ocelot_port_private *priv = netdev_priv(dev);
1201 struct ocelot_port *ocelot_port = &priv->port;
1202 struct ocelot *ocelot = ocelot_port->ocelot;
Vladimir Oltean81ef35e2021-03-23 01:51:51 +02001203 int port = priv->chip_port;
Vladimir Oltean421741e2021-02-12 17:15:59 +02001204 int err;
1205
Vladimir Olteane4bd44e2021-03-23 01:51:52 +02001206 ocelot_port_bridge_join(ocelot, port, bridge);
Vladimir Oltean421741e2021-02-12 17:15:59 +02001207
Vladimir Olteane4bd44e2021-03-23 01:51:52 +02001208 err = ocelot_switchdev_sync(ocelot, port, brport_dev, bridge, extack);
Vladimir Oltean421741e2021-02-12 17:15:59 +02001209 if (err)
Vladimir Olteane4bd44e2021-03-23 01:51:52 +02001210 goto err_switchdev_sync;
Vladimir Oltean421741e2021-02-12 17:15:59 +02001211
1212 return 0;
Vladimir Olteane4bd44e2021-03-23 01:51:52 +02001213
1214err_switchdev_sync:
1215 ocelot_port_bridge_leave(ocelot, port, bridge);
1216 return err;
Vladimir Oltean421741e2021-02-12 17:15:59 +02001217}
1218
Vladimir Oltean81ef35e2021-03-23 01:51:51 +02001219static int ocelot_netdevice_bridge_leave(struct net_device *dev,
Vladimir Olteane4bd44e2021-03-23 01:51:52 +02001220 struct net_device *brport_dev,
Vladimir Oltean421741e2021-02-12 17:15:59 +02001221 struct net_device *bridge)
1222{
Vladimir Oltean81ef35e2021-03-23 01:51:51 +02001223 struct ocelot_port_private *priv = netdev_priv(dev);
1224 struct ocelot_port *ocelot_port = &priv->port;
1225 struct ocelot *ocelot = ocelot_port->ocelot;
Vladimir Oltean81ef35e2021-03-23 01:51:51 +02001226 int port = priv->chip_port;
Vladimir Oltean421741e2021-02-12 17:15:59 +02001227 int err;
1228
Vladimir Olteane4bd44e2021-03-23 01:51:52 +02001229 err = ocelot_switchdev_unsync(ocelot, port);
1230 if (err)
1231 return err;
Vladimir Oltean421741e2021-02-12 17:15:59 +02001232
Vladimir Olteane4bd44e2021-03-23 01:51:52 +02001233 ocelot_port_bridge_leave(ocelot, port, bridge);
Vladimir Oltean421741e2021-02-12 17:15:59 +02001234
Vladimir Olteane4bd44e2021-03-23 01:51:52 +02001235 return 0;
Vladimir Oltean421741e2021-02-12 17:15:59 +02001236}
1237
Vladimir Oltean81ef35e2021-03-23 01:51:51 +02001238static int ocelot_netdevice_lag_join(struct net_device *dev,
1239 struct net_device *bond,
1240 struct netdev_lag_upper_info *info,
1241 struct netlink_ext_ack *extack)
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001242{
1243 struct ocelot_port_private *priv = netdev_priv(dev);
1244 struct ocelot_port *ocelot_port = &priv->port;
1245 struct ocelot *ocelot = ocelot_port->ocelot;
Vladimir Oltean81ef35e2021-03-23 01:51:51 +02001246 struct net_device *bridge_dev;
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001247 int port = priv->chip_port;
Vladimir Oltean81ef35e2021-03-23 01:51:51 +02001248 int err;
1249
1250 err = ocelot_port_lag_join(ocelot, port, bond, info);
1251 if (err == -EOPNOTSUPP) {
1252 NL_SET_ERR_MSG_MOD(extack, "Offloading not supported");
1253 return 0;
1254 }
1255
1256 bridge_dev = netdev_master_upper_dev_get(bond);
1257 if (!bridge_dev || !netif_is_bridge_master(bridge_dev))
1258 return 0;
1259
Vladimir Olteane4bd44e2021-03-23 01:51:52 +02001260 err = ocelot_netdevice_bridge_join(dev, bond, bridge_dev, extack);
Vladimir Oltean81ef35e2021-03-23 01:51:51 +02001261 if (err)
1262 goto err_bridge_join;
1263
1264 return 0;
1265
1266err_bridge_join:
1267 ocelot_port_lag_leave(ocelot, port, bond);
1268 return err;
1269}
1270
1271static int ocelot_netdevice_lag_leave(struct net_device *dev,
1272 struct net_device *bond)
1273{
1274 struct ocelot_port_private *priv = netdev_priv(dev);
1275 struct ocelot_port *ocelot_port = &priv->port;
1276 struct ocelot *ocelot = ocelot_port->ocelot;
1277 struct net_device *bridge_dev;
1278 int port = priv->chip_port;
1279
1280 ocelot_port_lag_leave(ocelot, port, bond);
1281
1282 bridge_dev = netdev_master_upper_dev_get(bond);
1283 if (!bridge_dev || !netif_is_bridge_master(bridge_dev))
1284 return 0;
1285
Vladimir Olteane4bd44e2021-03-23 01:51:52 +02001286 return ocelot_netdevice_bridge_leave(dev, bond, bridge_dev);
Vladimir Oltean81ef35e2021-03-23 01:51:51 +02001287}
1288
1289static int ocelot_netdevice_changeupper(struct net_device *dev,
1290 struct netdev_notifier_changeupper_info *info)
1291{
1292 struct netlink_ext_ack *extack;
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001293 int err = 0;
1294
Vladimir Oltean81ef35e2021-03-23 01:51:51 +02001295 extack = netdev_notifier_info_to_extack(&info->info);
1296
Vladimir Oltean662981b2021-02-06 00:02:10 +02001297 if (netif_is_bridge_master(info->upper_dev)) {
Vladimir Oltean81ef35e2021-03-23 01:51:51 +02001298 if (info->linking)
Vladimir Olteane4bd44e2021-03-23 01:51:52 +02001299 err = ocelot_netdevice_bridge_join(dev, dev,
1300 info->upper_dev,
Vladimir Oltean81ef35e2021-03-23 01:51:51 +02001301 extack);
1302 else
Vladimir Olteane4bd44e2021-03-23 01:51:52 +02001303 err = ocelot_netdevice_bridge_leave(dev, dev,
1304 info->upper_dev);
Vladimir Oltean662981b2021-02-06 00:02:10 +02001305 }
1306 if (netif_is_lag_master(info->upper_dev)) {
Vladimir Oltean81ef35e2021-03-23 01:51:51 +02001307 if (info->linking)
1308 err = ocelot_netdevice_lag_join(dev, info->upper_dev,
1309 info->upper_info, extack);
1310 else
1311 ocelot_netdevice_lag_leave(dev, info->upper_dev);
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001312 }
1313
Vladimir Oltean41e66fa2021-02-06 00:02:11 +02001314 return notifier_from_errno(err);
1315}
1316
Vladimir Oltean81ef35e2021-03-23 01:51:51 +02001317/* Treat CHANGEUPPER events on an offloaded LAG as individual CHANGEUPPER
1318 * events for the lower physical ports of the LAG.
1319 * If the LAG upper isn't offloaded, ignore its CHANGEUPPER events.
1320 * In case the LAG joined a bridge, notify that we are offloading it and can do
1321 * forwarding in hardware towards it.
1322 */
Vladimir Oltean41e66fa2021-02-06 00:02:11 +02001323static int
1324ocelot_netdevice_lag_changeupper(struct net_device *dev,
1325 struct netdev_notifier_changeupper_info *info)
1326{
1327 struct net_device *lower;
1328 struct list_head *iter;
1329 int err = NOTIFY_DONE;
1330
1331 netdev_for_each_lower_dev(dev, lower, iter) {
Vladimir Oltean81ef35e2021-03-23 01:51:51 +02001332 struct ocelot_port_private *priv = netdev_priv(lower);
1333 struct ocelot_port *ocelot_port = &priv->port;
1334
1335 if (ocelot_port->bond != dev)
1336 return NOTIFY_OK;
1337
Vladimir Oltean41e66fa2021-02-06 00:02:11 +02001338 err = ocelot_netdevice_changeupper(lower, info);
1339 if (err)
1340 return notifier_from_errno(err);
1341 }
1342
1343 return NOTIFY_DONE;
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001344}
1345
Vladimir Oltean23ca3b72021-02-06 00:02:19 +02001346static int
1347ocelot_netdevice_changelowerstate(struct net_device *dev,
1348 struct netdev_lag_lower_state_info *info)
1349{
1350 struct ocelot_port_private *priv = netdev_priv(dev);
1351 bool is_active = info->link_up && info->tx_enabled;
1352 struct ocelot_port *ocelot_port = &priv->port;
1353 struct ocelot *ocelot = ocelot_port->ocelot;
1354 int port = priv->chip_port;
1355
1356 if (!ocelot_port->bond)
1357 return NOTIFY_DONE;
1358
1359 if (ocelot_port->lag_tx_active == is_active)
1360 return NOTIFY_DONE;
1361
1362 ocelot_port_lag_change(ocelot, port, is_active);
1363
1364 return NOTIFY_OK;
1365}
1366
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001367static int ocelot_netdevice_event(struct notifier_block *unused,
1368 unsigned long event, void *ptr)
1369{
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001370 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001371
Vladimir Oltean41e66fa2021-02-06 00:02:11 +02001372 switch (event) {
Vladimir Oltean41e66fa2021-02-06 00:02:11 +02001373 case NETDEV_CHANGEUPPER: {
1374 struct netdev_notifier_changeupper_info *info = ptr;
1375
1376 if (ocelot_netdevice_dev_check(dev))
1377 return ocelot_netdevice_changeupper(dev, info);
1378
1379 if (netif_is_lag_master(dev))
1380 return ocelot_netdevice_lag_changeupper(dev, info);
1381
1382 break;
1383 }
Vladimir Oltean23ca3b72021-02-06 00:02:19 +02001384 case NETDEV_CHANGELOWERSTATE: {
1385 struct netdev_notifier_changelowerstate_info *info = ptr;
1386
1387 if (!ocelot_netdevice_dev_check(dev))
1388 break;
1389
1390 return ocelot_netdevice_changelowerstate(dev,
1391 info->lower_state_info);
1392 }
Vladimir Oltean41e66fa2021-02-06 00:02:11 +02001393 default:
1394 break;
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001395 }
1396
Vladimir Oltean41e66fa2021-02-06 00:02:11 +02001397 return NOTIFY_DONE;
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001398}
1399
1400struct notifier_block ocelot_netdevice_nb __read_mostly = {
1401 .notifier_call = ocelot_netdevice_event,
1402};
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001403
1404static int ocelot_switchdev_event(struct notifier_block *unused,
1405 unsigned long event, void *ptr)
1406{
1407 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
1408 int err;
1409
1410 switch (event) {
1411 case SWITCHDEV_PORT_ATTR_SET:
1412 err = switchdev_handle_port_attr_set(dev, ptr,
1413 ocelot_netdevice_dev_check,
1414 ocelot_port_attr_set);
1415 return notifier_from_errno(err);
1416 }
1417
1418 return NOTIFY_DONE;
1419}
1420
1421struct notifier_block ocelot_switchdev_nb __read_mostly = {
1422 .notifier_call = ocelot_switchdev_event,
1423};
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001424
1425static int ocelot_switchdev_blocking_event(struct notifier_block *unused,
1426 unsigned long event, void *ptr)
1427{
1428 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
1429 int err;
1430
1431 switch (event) {
1432 /* Blocking events. */
1433 case SWITCHDEV_PORT_OBJ_ADD:
1434 err = switchdev_handle_port_obj_add(dev, ptr,
1435 ocelot_netdevice_dev_check,
1436 ocelot_port_obj_add);
1437 return notifier_from_errno(err);
1438 case SWITCHDEV_PORT_OBJ_DEL:
1439 err = switchdev_handle_port_obj_del(dev, ptr,
1440 ocelot_netdevice_dev_check,
1441 ocelot_port_obj_del);
1442 return notifier_from_errno(err);
1443 case SWITCHDEV_PORT_ATTR_SET:
1444 err = switchdev_handle_port_attr_set(dev, ptr,
1445 ocelot_netdevice_dev_check,
1446 ocelot_port_attr_set);
1447 return notifier_from_errno(err);
1448 }
1449
1450 return NOTIFY_DONE;
1451}
1452
1453struct notifier_block ocelot_switchdev_blocking_nb __read_mostly = {
1454 .notifier_call = ocelot_switchdev_blocking_event,
1455};
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001456
Vladimir Oltean91c724c2020-07-13 19:57:01 +03001457int ocelot_probe_port(struct ocelot *ocelot, int port, struct regmap *target,
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001458 struct phy_device *phy)
1459{
1460 struct ocelot_port_private *priv;
1461 struct ocelot_port *ocelot_port;
1462 struct net_device *dev;
1463 int err;
1464
1465 dev = alloc_etherdev(sizeof(struct ocelot_port_private));
1466 if (!dev)
1467 return -ENOMEM;
1468 SET_NETDEV_DEV(dev, ocelot->dev);
1469 priv = netdev_priv(dev);
1470 priv->dev = dev;
1471 priv->phy = phy;
1472 priv->chip_port = port;
1473 ocelot_port = &priv->port;
1474 ocelot_port->ocelot = ocelot;
Vladimir Oltean91c724c2020-07-13 19:57:01 +03001475 ocelot_port->target = target;
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001476 ocelot->ports[port] = ocelot_port;
1477
1478 dev->netdev_ops = &ocelot_port_netdev_ops;
1479 dev->ethtool_ops = &ocelot_ethtool_ops;
1480
1481 dev->hw_features |= NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_RXFCS |
1482 NETIF_F_HW_TC;
1483 dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_TC;
1484
1485 memcpy(dev->dev_addr, ocelot->base_mac, ETH_ALEN);
1486 dev->dev_addr[ETH_ALEN - 1] += port;
Vladimir Olteanc3e58a752020-10-31 12:29:12 +02001487 ocelot_mact_learn(ocelot, PGID_CPU, dev->dev_addr,
1488 ocelot_port->pvid_vlan.vid, ENTRYTYPE_LOCKED);
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001489
1490 ocelot_init_port(ocelot, port);
1491
1492 err = register_netdev(dev);
1493 if (err) {
1494 dev_err(ocelot->dev, "register_netdev failed\n");
1495 free_netdev(dev);
Dan Carpentere0c16232021-02-02 12:12:38 +03001496 ocelot->ports[port] = NULL;
1497 return err;
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001498 }
1499
Dan Carpentere0c16232021-02-02 12:12:38 +03001500 return 0;
1501}
1502
1503void ocelot_release_port(struct ocelot_port *ocelot_port)
1504{
1505 struct ocelot_port_private *priv = container_of(ocelot_port,
1506 struct ocelot_port_private,
1507 port);
1508
1509 unregister_netdev(priv->dev);
1510 free_netdev(priv->dev);
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001511}