blob: eaeba60b1bba5ef47a0bbb60a3e9a48e8c92cdb6 [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 Oltean3c9cfb52021-09-17 14:17:35 +03008 * Copyright 2020-2021 NXP
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03009 */
10
Vladimir Olteandeab6b12021-10-12 14:40:40 +030011#include <linux/dsa/ocelot.h>
Vladimir Oltean9c90eea2020-06-20 18:43:44 +030012#include <linux/if_bridge.h>
Vladimir Olteane6e12df2021-08-15 04:47:48 +030013#include <linux/of_net.h>
14#include <linux/phy/phy.h>
Vladimir Oltean0e9bb4e2021-01-29 03:00:00 +020015#include <net/pkt_cls.h>
Vladimir Oltean9c90eea2020-06-20 18:43:44 +030016#include "ocelot.h"
Vladimir Oltean3c836542020-06-20 18:43:45 +030017#include "ocelot_vcap.h"
Vladimir Oltean9c90eea2020-06-20 18:43:44 +030018
Vladimir Olteane6e12df2021-08-15 04:47:48 +030019#define OCELOT_MAC_QUIRKS OCELOT_QUIRK_QSGMII_PORTS_MUST_BE_UP
20
Vladimir Olteanf59fd9c2021-01-15 04:11:20 +020021static struct ocelot *devlink_port_to_ocelot(struct devlink_port *dlp)
22{
23 return devlink_priv(dlp->devlink);
24}
25
26static int devlink_port_to_port(struct devlink_port *dlp)
27{
28 struct ocelot *ocelot = devlink_port_to_ocelot(dlp);
29
30 return dlp - ocelot->devlink_ports;
31}
32
33static int ocelot_devlink_sb_pool_get(struct devlink *dl,
34 unsigned int sb_index, u16 pool_index,
35 struct devlink_sb_pool_info *pool_info)
36{
37 struct ocelot *ocelot = devlink_priv(dl);
38
39 return ocelot_sb_pool_get(ocelot, sb_index, pool_index, pool_info);
40}
41
42static int ocelot_devlink_sb_pool_set(struct devlink *dl, unsigned int sb_index,
43 u16 pool_index, u32 size,
44 enum devlink_sb_threshold_type threshold_type,
45 struct netlink_ext_ack *extack)
46{
47 struct ocelot *ocelot = devlink_priv(dl);
48
49 return ocelot_sb_pool_set(ocelot, sb_index, pool_index, size,
50 threshold_type, extack);
51}
52
53static int ocelot_devlink_sb_port_pool_get(struct devlink_port *dlp,
54 unsigned int sb_index, u16 pool_index,
55 u32 *p_threshold)
56{
57 struct ocelot *ocelot = devlink_port_to_ocelot(dlp);
58 int port = devlink_port_to_port(dlp);
59
60 return ocelot_sb_port_pool_get(ocelot, port, sb_index, pool_index,
61 p_threshold);
62}
63
64static int ocelot_devlink_sb_port_pool_set(struct devlink_port *dlp,
65 unsigned int sb_index, u16 pool_index,
66 u32 threshold,
67 struct netlink_ext_ack *extack)
68{
69 struct ocelot *ocelot = devlink_port_to_ocelot(dlp);
70 int port = devlink_port_to_port(dlp);
71
72 return ocelot_sb_port_pool_set(ocelot, port, sb_index, pool_index,
73 threshold, extack);
74}
75
76static int
77ocelot_devlink_sb_tc_pool_bind_get(struct devlink_port *dlp,
78 unsigned int sb_index, u16 tc_index,
79 enum devlink_sb_pool_type pool_type,
80 u16 *p_pool_index, u32 *p_threshold)
81{
82 struct ocelot *ocelot = devlink_port_to_ocelot(dlp);
83 int port = devlink_port_to_port(dlp);
84
85 return ocelot_sb_tc_pool_bind_get(ocelot, port, sb_index, tc_index,
86 pool_type, p_pool_index,
87 p_threshold);
88}
89
90static int
91ocelot_devlink_sb_tc_pool_bind_set(struct devlink_port *dlp,
92 unsigned int sb_index, u16 tc_index,
93 enum devlink_sb_pool_type pool_type,
94 u16 pool_index, u32 threshold,
95 struct netlink_ext_ack *extack)
96{
97 struct ocelot *ocelot = devlink_port_to_ocelot(dlp);
98 int port = devlink_port_to_port(dlp);
99
100 return ocelot_sb_tc_pool_bind_set(ocelot, port, sb_index, tc_index,
101 pool_type, pool_index, threshold,
102 extack);
103}
104
105static int ocelot_devlink_sb_occ_snapshot(struct devlink *dl,
106 unsigned int sb_index)
107{
108 struct ocelot *ocelot = devlink_priv(dl);
109
110 return ocelot_sb_occ_snapshot(ocelot, sb_index);
111}
112
113static int ocelot_devlink_sb_occ_max_clear(struct devlink *dl,
114 unsigned int sb_index)
115{
116 struct ocelot *ocelot = devlink_priv(dl);
117
118 return ocelot_sb_occ_max_clear(ocelot, sb_index);
119}
120
121static int ocelot_devlink_sb_occ_port_pool_get(struct devlink_port *dlp,
122 unsigned int sb_index,
123 u16 pool_index, u32 *p_cur,
124 u32 *p_max)
125{
126 struct ocelot *ocelot = devlink_port_to_ocelot(dlp);
127 int port = devlink_port_to_port(dlp);
128
129 return ocelot_sb_occ_port_pool_get(ocelot, port, sb_index, pool_index,
130 p_cur, p_max);
131}
132
133static int
134ocelot_devlink_sb_occ_tc_port_bind_get(struct devlink_port *dlp,
135 unsigned int sb_index, u16 tc_index,
136 enum devlink_sb_pool_type pool_type,
137 u32 *p_cur, u32 *p_max)
138{
139 struct ocelot *ocelot = devlink_port_to_ocelot(dlp);
140 int port = devlink_port_to_port(dlp);
141
142 return ocelot_sb_occ_tc_port_bind_get(ocelot, port, sb_index,
143 tc_index, pool_type,
144 p_cur, p_max);
145}
146
Vladimir Oltean6c30384e2021-01-15 04:11:18 +0200147const struct devlink_ops ocelot_devlink_ops = {
Vladimir Olteanf59fd9c2021-01-15 04:11:20 +0200148 .sb_pool_get = ocelot_devlink_sb_pool_get,
149 .sb_pool_set = ocelot_devlink_sb_pool_set,
150 .sb_port_pool_get = ocelot_devlink_sb_port_pool_get,
151 .sb_port_pool_set = ocelot_devlink_sb_port_pool_set,
152 .sb_tc_pool_bind_get = ocelot_devlink_sb_tc_pool_bind_get,
153 .sb_tc_pool_bind_set = ocelot_devlink_sb_tc_pool_bind_set,
154 .sb_occ_snapshot = ocelot_devlink_sb_occ_snapshot,
155 .sb_occ_max_clear = ocelot_devlink_sb_occ_max_clear,
156 .sb_occ_port_pool_get = ocelot_devlink_sb_occ_port_pool_get,
157 .sb_occ_tc_port_bind_get = ocelot_devlink_sb_occ_tc_port_bind_get,
Vladimir Oltean6c30384e2021-01-15 04:11:18 +0200158};
159
160int ocelot_port_devlink_init(struct ocelot *ocelot, int port,
161 enum devlink_port_flavour flavour)
162{
163 struct devlink_port *dlp = &ocelot->devlink_ports[port];
164 int id_len = sizeof(ocelot->base_mac);
165 struct devlink *dl = ocelot->devlink;
166 struct devlink_port_attrs attrs = {};
167
Horatiu Vulturb5e33a12021-08-19 19:49:57 +0300168 memset(dlp, 0, sizeof(*dlp));
Vladimir Oltean6c30384e2021-01-15 04:11:18 +0200169 memcpy(attrs.switch_id.id, &ocelot->base_mac, id_len);
170 attrs.switch_id.id_len = id_len;
171 attrs.phys.port_number = port;
172 attrs.flavour = flavour;
173
174 devlink_port_attrs_set(dlp, &attrs);
175
176 return devlink_port_register(dl, dlp, port);
177}
178
179void ocelot_port_devlink_teardown(struct ocelot *ocelot, int port)
180{
181 struct devlink_port *dlp = &ocelot->devlink_ports[port];
182
183 devlink_port_unregister(dlp);
184}
185
186static struct devlink_port *ocelot_get_devlink_port(struct net_device *dev)
187{
188 struct ocelot_port_private *priv = netdev_priv(dev);
189 struct ocelot *ocelot = priv->port.ocelot;
190 int port = priv->chip_port;
191
192 return &ocelot->devlink_ports[port];
193}
194
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300195int ocelot_setup_tc_cls_flower(struct ocelot_port_private *priv,
196 struct flow_cls_offload *f,
197 bool ingress)
198{
199 struct ocelot *ocelot = priv->port.ocelot;
200 int port = priv->chip_port;
201
202 if (!ingress)
203 return -EOPNOTSUPP;
204
205 switch (f->command) {
206 case FLOW_CLS_REPLACE:
207 return ocelot_cls_flower_replace(ocelot, port, f, ingress);
208 case FLOW_CLS_DESTROY:
209 return ocelot_cls_flower_destroy(ocelot, port, f, ingress);
210 case FLOW_CLS_STATS:
211 return ocelot_cls_flower_stats(ocelot, port, f, ingress);
212 default:
213 return -EOPNOTSUPP;
214 }
215}
216
217static int ocelot_setup_tc_cls_matchall(struct ocelot_port_private *priv,
218 struct tc_cls_matchall_offload *f,
219 bool ingress)
220{
221 struct netlink_ext_ack *extack = f->common.extack;
222 struct ocelot *ocelot = priv->port.ocelot;
223 struct ocelot_policer pol = { 0 };
224 struct flow_action_entry *action;
225 int port = priv->chip_port;
226 int err;
227
228 if (!ingress) {
229 NL_SET_ERR_MSG_MOD(extack, "Only ingress is supported");
230 return -EOPNOTSUPP;
231 }
232
233 switch (f->command) {
234 case TC_CLSMATCHALL_REPLACE:
235 if (!flow_offload_has_one_action(&f->rule->action)) {
236 NL_SET_ERR_MSG_MOD(extack,
237 "Only one action is supported");
238 return -EOPNOTSUPP;
239 }
240
241 if (priv->tc.block_shared) {
242 NL_SET_ERR_MSG_MOD(extack,
243 "Rate limit is not supported on shared blocks");
244 return -EOPNOTSUPP;
245 }
246
247 action = &f->rule->action.entries[0];
248
249 if (action->id != FLOW_ACTION_POLICE) {
250 NL_SET_ERR_MSG_MOD(extack, "Unsupported action");
251 return -EOPNOTSUPP;
252 }
253
254 if (priv->tc.police_id && priv->tc.police_id != f->cookie) {
255 NL_SET_ERR_MSG_MOD(extack,
256 "Only one policer per port is supported");
257 return -EEXIST;
258 }
259
Baowen Zheng6a56e192021-03-12 15:08:30 +0100260 if (action->police.rate_pkt_ps) {
261 NL_SET_ERR_MSG_MOD(extack,
262 "QoS offload not support packets per second");
263 return -EOPNOTSUPP;
264 }
265
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300266 pol.rate = (u32)div_u64(action->police.rate_bytes_ps, 1000) * 8;
Po Liu5f035af2020-06-29 14:54:16 +0800267 pol.burst = action->police.burst;
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300268
269 err = ocelot_port_policer_add(ocelot, port, &pol);
270 if (err) {
271 NL_SET_ERR_MSG_MOD(extack, "Could not add policer");
272 return err;
273 }
274
275 priv->tc.police_id = f->cookie;
276 priv->tc.offload_cnt++;
277 return 0;
278 case TC_CLSMATCHALL_DESTROY:
279 if (priv->tc.police_id != f->cookie)
280 return -ENOENT;
281
282 err = ocelot_port_policer_del(ocelot, port);
283 if (err) {
284 NL_SET_ERR_MSG_MOD(extack,
285 "Could not delete policer");
286 return err;
287 }
288 priv->tc.police_id = 0;
289 priv->tc.offload_cnt--;
290 return 0;
291 case TC_CLSMATCHALL_STATS:
292 default:
293 return -EOPNOTSUPP;
294 }
295}
296
297static int ocelot_setup_tc_block_cb(enum tc_setup_type type,
298 void *type_data,
299 void *cb_priv, bool ingress)
300{
301 struct ocelot_port_private *priv = cb_priv;
302
303 if (!tc_cls_can_offload_and_chain0(priv->dev, type_data))
304 return -EOPNOTSUPP;
305
306 switch (type) {
307 case TC_SETUP_CLSMATCHALL:
308 return ocelot_setup_tc_cls_matchall(priv, type_data, ingress);
309 case TC_SETUP_CLSFLOWER:
310 return ocelot_setup_tc_cls_flower(priv, type_data, ingress);
311 default:
312 return -EOPNOTSUPP;
313 }
314}
315
316static int ocelot_setup_tc_block_cb_ig(enum tc_setup_type type,
317 void *type_data,
318 void *cb_priv)
319{
320 return ocelot_setup_tc_block_cb(type, type_data,
321 cb_priv, true);
322}
323
324static int ocelot_setup_tc_block_cb_eg(enum tc_setup_type type,
325 void *type_data,
326 void *cb_priv)
327{
328 return ocelot_setup_tc_block_cb(type, type_data,
329 cb_priv, false);
330}
331
332static LIST_HEAD(ocelot_block_cb_list);
333
334static int ocelot_setup_tc_block(struct ocelot_port_private *priv,
335 struct flow_block_offload *f)
336{
337 struct flow_block_cb *block_cb;
338 flow_setup_cb_t *cb;
339
340 if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS) {
341 cb = ocelot_setup_tc_block_cb_ig;
342 priv->tc.block_shared = f->block_shared;
343 } else if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_EGRESS) {
344 cb = ocelot_setup_tc_block_cb_eg;
345 } else {
346 return -EOPNOTSUPP;
347 }
348
349 f->driver_block_list = &ocelot_block_cb_list;
350
351 switch (f->command) {
352 case FLOW_BLOCK_BIND:
353 if (flow_block_cb_is_busy(cb, priv, &ocelot_block_cb_list))
354 return -EBUSY;
355
356 block_cb = flow_block_cb_alloc(cb, priv, priv, NULL);
357 if (IS_ERR(block_cb))
358 return PTR_ERR(block_cb);
359
360 flow_block_cb_add(block_cb, f);
361 list_add_tail(&block_cb->driver_list, f->driver_block_list);
362 return 0;
363 case FLOW_BLOCK_UNBIND:
364 block_cb = flow_block_cb_lookup(f->block, cb, priv);
365 if (!block_cb)
366 return -ENOENT;
367
368 flow_block_cb_remove(block_cb, f);
369 list_del(&block_cb->driver_list);
370 return 0;
371 default:
372 return -EOPNOTSUPP;
373 }
374}
375
376static int ocelot_setup_tc(struct net_device *dev, enum tc_setup_type type,
377 void *type_data)
378{
379 struct ocelot_port_private *priv = netdev_priv(dev);
380
381 switch (type) {
382 case TC_SETUP_BLOCK:
383 return ocelot_setup_tc_block(priv, type_data);
384 default:
385 return -EOPNOTSUPP;
386 }
387 return 0;
388}
389
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300390static int ocelot_vlan_vid_add(struct net_device *dev, u16 vid, bool pvid,
391 bool untagged)
392{
393 struct ocelot_port_private *priv = netdev_priv(dev);
394 struct ocelot_port *ocelot_port = &priv->port;
395 struct ocelot *ocelot = ocelot_port->ocelot;
396 int port = priv->chip_port;
397 int ret;
398
399 ret = ocelot_vlan_add(ocelot, port, vid, pvid, untagged);
400 if (ret)
401 return ret;
402
403 /* Add the port MAC address to with the right VLAN information */
404 ocelot_mact_learn(ocelot, PGID_CPU, dev->dev_addr, vid,
405 ENTRYTYPE_LOCKED);
406
407 return 0;
408}
409
410static int ocelot_vlan_vid_del(struct net_device *dev, u16 vid)
411{
412 struct ocelot_port_private *priv = netdev_priv(dev);
413 struct ocelot *ocelot = priv->port.ocelot;
414 int port = priv->chip_port;
415 int ret;
416
417 /* 8021q removes VID 0 on module unload for all interfaces
418 * with VLAN filtering feature. We need to keep it to receive
419 * untagged traffic.
420 */
Vladimir Olteanbfbab312021-10-20 20:58:51 +0300421 if (vid == OCELOT_VLAN_UNAWARE_PVID)
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300422 return 0;
423
424 ret = ocelot_vlan_del(ocelot, port, vid);
425 if (ret)
426 return ret;
427
428 /* Del the port MAC address to with the right VLAN information */
429 ocelot_mact_forget(ocelot, dev->dev_addr, vid);
430
431 return 0;
432}
433
434static int ocelot_port_open(struct net_device *dev)
435{
436 struct ocelot_port_private *priv = netdev_priv(dev);
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300437
Vladimir Olteane6e12df2021-08-15 04:47:48 +0300438 phylink_start(priv->phylink);
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300439
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300440 return 0;
441}
442
443static int ocelot_port_stop(struct net_device *dev)
444{
445 struct ocelot_port_private *priv = netdev_priv(dev);
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300446
Vladimir Olteane6e12df2021-08-15 04:47:48 +0300447 phylink_stop(priv->phylink);
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300448
449 return 0;
450}
451
Vladimir Oltean137ffbc2021-02-14 00:37:54 +0200452static netdev_tx_t ocelot_port_xmit(struct sk_buff *skb, struct net_device *dev)
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300453{
454 struct ocelot_port_private *priv = netdev_priv(dev);
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300455 struct ocelot_port *ocelot_port = &priv->port;
456 struct ocelot *ocelot = ocelot_port->ocelot;
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300457 int port = priv->chip_port;
Vladimir Oltean137ffbc2021-02-14 00:37:54 +0200458 u32 rew_op = 0;
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300459
Vladimir Oltean137ffbc2021-02-14 00:37:54 +0200460 if (!ocelot_can_inject(ocelot, 0))
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300461 return NETDEV_TX_BUSY;
462
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300463 /* Check if timestamping is needed */
Vladimir Oltean137ffbc2021-02-14 00:37:54 +0200464 if (ocelot->ptp && (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) {
Yangbo Lu682eaad2021-04-27 12:22:02 +0800465 struct sk_buff *clone = NULL;
466
467 if (ocelot_port_txtstamp_request(ocelot, port, skb, &clone)) {
468 kfree_skb(skb);
469 return NETDEV_TX_OK;
470 }
Vladimir Olteane2f9a8f2020-09-23 14:24:20 +0300471
Yangbo Lu39e53082021-04-27 12:22:03 +0800472 if (clone)
473 OCELOT_SKB_CB(skb)->clone = clone;
474
475 rew_op = ocelot_ptp_rew_op(skb);
Vladimir Olteane2f9a8f2020-09-23 14:24:20 +0300476 }
Vladimir Oltean9dda66a2020-09-18 04:07:23 +0300477
Vladimir Oltean137ffbc2021-02-14 00:37:54 +0200478 ocelot_port_inject_frame(ocelot, port, 0, rew_op, skb);
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300479
Vladimir Olteane2f9a8f2020-09-23 14:24:20 +0300480 kfree_skb(skb);
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300481
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300482 return NETDEV_TX_OK;
483}
484
Vladimir Olteanca0b2722020-12-12 21:16:12 +0200485enum ocelot_action_type {
486 OCELOT_MACT_LEARN,
487 OCELOT_MACT_FORGET,
488};
489
490struct ocelot_mact_work_ctx {
491 struct work_struct work;
492 struct ocelot *ocelot;
493 enum ocelot_action_type type;
494 union {
495 /* OCELOT_MACT_LEARN */
496 struct {
497 unsigned char addr[ETH_ALEN];
498 u16 vid;
499 enum macaccess_entry_type entry_type;
500 int pgid;
501 } learn;
502 /* OCELOT_MACT_FORGET */
503 struct {
504 unsigned char addr[ETH_ALEN];
505 u16 vid;
506 } forget;
507 };
508};
509
510#define ocelot_work_to_ctx(x) \
511 container_of((x), struct ocelot_mact_work_ctx, work)
512
513static void ocelot_mact_work(struct work_struct *work)
514{
515 struct ocelot_mact_work_ctx *w = ocelot_work_to_ctx(work);
516 struct ocelot *ocelot = w->ocelot;
517
518 switch (w->type) {
519 case OCELOT_MACT_LEARN:
520 ocelot_mact_learn(ocelot, w->learn.pgid, w->learn.addr,
521 w->learn.vid, w->learn.entry_type);
522 break;
523 case OCELOT_MACT_FORGET:
524 ocelot_mact_forget(ocelot, w->forget.addr, w->forget.vid);
525 break;
526 default:
527 break;
Xu Wang20efd2c2021-01-15 09:55:44 +0000528 }
Vladimir Olteanca0b2722020-12-12 21:16:12 +0200529
530 kfree(w);
531}
532
533static int ocelot_enqueue_mact_action(struct ocelot *ocelot,
534 const struct ocelot_mact_work_ctx *ctx)
535{
536 struct ocelot_mact_work_ctx *w = kmemdup(ctx, sizeof(*w), GFP_ATOMIC);
537
538 if (!w)
539 return -ENOMEM;
540
541 w->ocelot = ocelot;
542 INIT_WORK(&w->work, ocelot_mact_work);
543 queue_work(ocelot->owq, &w->work);
544
545 return 0;
546}
547
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300548static int ocelot_mc_unsync(struct net_device *dev, const unsigned char *addr)
549{
550 struct ocelot_port_private *priv = netdev_priv(dev);
551 struct ocelot_port *ocelot_port = &priv->port;
552 struct ocelot *ocelot = ocelot_port->ocelot;
Vladimir Olteanca0b2722020-12-12 21:16:12 +0200553 struct ocelot_mact_work_ctx w;
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300554
Vladimir Olteanca0b2722020-12-12 21:16:12 +0200555 ether_addr_copy(w.forget.addr, addr);
Vladimir Olteanbfbab312021-10-20 20:58:51 +0300556 w.forget.vid = OCELOT_VLAN_UNAWARE_PVID;
Vladimir Olteanca0b2722020-12-12 21:16:12 +0200557 w.type = OCELOT_MACT_FORGET;
558
559 return ocelot_enqueue_mact_action(ocelot, &w);
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300560}
561
562static int ocelot_mc_sync(struct net_device *dev, const unsigned char *addr)
563{
564 struct ocelot_port_private *priv = netdev_priv(dev);
565 struct ocelot_port *ocelot_port = &priv->port;
566 struct ocelot *ocelot = ocelot_port->ocelot;
Vladimir Olteanca0b2722020-12-12 21:16:12 +0200567 struct ocelot_mact_work_ctx w;
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300568
Vladimir Olteanca0b2722020-12-12 21:16:12 +0200569 ether_addr_copy(w.learn.addr, addr);
Vladimir Olteanbfbab312021-10-20 20:58:51 +0300570 w.learn.vid = OCELOT_VLAN_UNAWARE_PVID;
Vladimir Olteanca0b2722020-12-12 21:16:12 +0200571 w.learn.pgid = PGID_CPU;
572 w.learn.entry_type = ENTRYTYPE_LOCKED;
573 w.type = OCELOT_MACT_LEARN;
574
575 return ocelot_enqueue_mact_action(ocelot, &w);
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300576}
577
578static void ocelot_set_rx_mode(struct net_device *dev)
579{
580 struct ocelot_port_private *priv = netdev_priv(dev);
581 struct ocelot *ocelot = priv->port.ocelot;
582 u32 val;
583 int i;
584
585 /* This doesn't handle promiscuous mode because the bridge core is
586 * setting IFF_PROMISC on all slave interfaces and all frames would be
587 * forwarded to the CPU port.
588 */
589 val = GENMASK(ocelot->num_phys_ports - 1, 0);
Vladimir Oltean96b029b2020-06-21 14:46:02 +0300590 for_each_nonreserved_multicast_dest_pgid(ocelot, i)
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300591 ocelot_write_rix(ocelot, val, ANA_PGID_PGID, i);
592
593 __dev_mc_sync(dev, ocelot_mc_sync, ocelot_mc_unsync);
594}
595
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300596static int ocelot_port_set_mac_address(struct net_device *dev, void *p)
597{
598 struct ocelot_port_private *priv = netdev_priv(dev);
599 struct ocelot_port *ocelot_port = &priv->port;
600 struct ocelot *ocelot = ocelot_port->ocelot;
601 const struct sockaddr *addr = p;
602
603 /* Learn the new net device MAC address in the mac table. */
Vladimir Olteanc3e58a752020-10-31 12:29:12 +0200604 ocelot_mact_learn(ocelot, PGID_CPU, addr->sa_data,
Vladimir Olteanbfbab312021-10-20 20:58:51 +0300605 OCELOT_VLAN_UNAWARE_PVID, ENTRYTYPE_LOCKED);
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300606 /* Then forget the previous one. */
Vladimir Olteanbfbab312021-10-20 20:58:51 +0300607 ocelot_mact_forget(ocelot, dev->dev_addr, OCELOT_VLAN_UNAWARE_PVID);
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300608
Jakub Kicinskif3956eb2021-10-01 14:32:23 -0700609 eth_hw_addr_set(dev, addr->sa_data);
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300610 return 0;
611}
612
613static void ocelot_get_stats64(struct net_device *dev,
614 struct rtnl_link_stats64 *stats)
615{
616 struct ocelot_port_private *priv = netdev_priv(dev);
617 struct ocelot *ocelot = priv->port.ocelot;
618 int port = priv->chip_port;
619
620 /* Configure the port to read the stats from */
621 ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(port),
622 SYS_STAT_CFG);
623
624 /* Get Rx stats */
625 stats->rx_bytes = ocelot_read(ocelot, SYS_COUNT_RX_OCTETS);
626 stats->rx_packets = ocelot_read(ocelot, SYS_COUNT_RX_SHORTS) +
627 ocelot_read(ocelot, SYS_COUNT_RX_FRAGMENTS) +
628 ocelot_read(ocelot, SYS_COUNT_RX_JABBERS) +
629 ocelot_read(ocelot, SYS_COUNT_RX_LONGS) +
630 ocelot_read(ocelot, SYS_COUNT_RX_64) +
631 ocelot_read(ocelot, SYS_COUNT_RX_65_127) +
632 ocelot_read(ocelot, SYS_COUNT_RX_128_255) +
633 ocelot_read(ocelot, SYS_COUNT_RX_256_1023) +
634 ocelot_read(ocelot, SYS_COUNT_RX_1024_1526) +
635 ocelot_read(ocelot, SYS_COUNT_RX_1527_MAX);
636 stats->multicast = ocelot_read(ocelot, SYS_COUNT_RX_MULTICAST);
637 stats->rx_dropped = dev->stats.rx_dropped;
638
639 /* Get Tx stats */
640 stats->tx_bytes = ocelot_read(ocelot, SYS_COUNT_TX_OCTETS);
641 stats->tx_packets = ocelot_read(ocelot, SYS_COUNT_TX_64) +
642 ocelot_read(ocelot, SYS_COUNT_TX_65_127) +
643 ocelot_read(ocelot, SYS_COUNT_TX_128_511) +
644 ocelot_read(ocelot, SYS_COUNT_TX_512_1023) +
645 ocelot_read(ocelot, SYS_COUNT_TX_1024_1526) +
646 ocelot_read(ocelot, SYS_COUNT_TX_1527_MAX);
647 stats->tx_dropped = ocelot_read(ocelot, SYS_COUNT_TX_DROPS) +
648 ocelot_read(ocelot, SYS_COUNT_TX_AGING);
649 stats->collisions = ocelot_read(ocelot, SYS_COUNT_TX_COLLISION);
650}
651
652static int ocelot_port_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
653 struct net_device *dev,
654 const unsigned char *addr,
655 u16 vid, u16 flags,
656 struct netlink_ext_ack *extack)
657{
658 struct ocelot_port_private *priv = netdev_priv(dev);
659 struct ocelot *ocelot = priv->port.ocelot;
660 int port = priv->chip_port;
661
662 return ocelot_fdb_add(ocelot, port, addr, vid);
663}
664
665static int ocelot_port_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
666 struct net_device *dev,
667 const unsigned char *addr, u16 vid)
668{
669 struct ocelot_port_private *priv = netdev_priv(dev);
670 struct ocelot *ocelot = priv->port.ocelot;
671 int port = priv->chip_port;
672
673 return ocelot_fdb_del(ocelot, port, addr, vid);
674}
675
676static int ocelot_port_fdb_dump(struct sk_buff *skb,
677 struct netlink_callback *cb,
678 struct net_device *dev,
679 struct net_device *filter_dev, int *idx)
680{
681 struct ocelot_port_private *priv = netdev_priv(dev);
682 struct ocelot *ocelot = priv->port.ocelot;
683 struct ocelot_dump_ctx dump = {
684 .dev = dev,
685 .skb = skb,
686 .cb = cb,
687 .idx = *idx,
688 };
689 int port = priv->chip_port;
690 int ret;
691
692 ret = ocelot_fdb_dump(ocelot, port, ocelot_port_fdb_do_dump, &dump);
693
694 *idx = dump.idx;
695
696 return ret;
697}
698
699static int ocelot_vlan_rx_add_vid(struct net_device *dev, __be16 proto,
700 u16 vid)
701{
702 return ocelot_vlan_vid_add(dev, vid, false, false);
703}
704
705static int ocelot_vlan_rx_kill_vid(struct net_device *dev, __be16 proto,
706 u16 vid)
707{
708 return ocelot_vlan_vid_del(dev, vid);
709}
710
711static void ocelot_vlan_mode(struct ocelot *ocelot, int port,
712 netdev_features_t features)
713{
714 u32 val;
715
716 /* Filtering */
717 val = ocelot_read(ocelot, ANA_VLANMASK);
718 if (features & NETIF_F_HW_VLAN_CTAG_FILTER)
719 val |= BIT(port);
720 else
721 val &= ~BIT(port);
722 ocelot_write(ocelot, val, ANA_VLANMASK);
723}
724
725static int ocelot_set_features(struct net_device *dev,
726 netdev_features_t features)
727{
728 netdev_features_t changed = dev->features ^ features;
729 struct ocelot_port_private *priv = netdev_priv(dev);
730 struct ocelot *ocelot = priv->port.ocelot;
731 int port = priv->chip_port;
732
733 if ((dev->features & NETIF_F_HW_TC) > (features & NETIF_F_HW_TC) &&
734 priv->tc.offload_cnt) {
735 netdev_err(dev,
736 "Cannot disable HW TC offload while offloads active\n");
737 return -EBUSY;
738 }
739
740 if (changed & NETIF_F_HW_VLAN_CTAG_FILTER)
741 ocelot_vlan_mode(ocelot, port, features);
742
743 return 0;
744}
745
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300746static int ocelot_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
747{
748 struct ocelot_port_private *priv = netdev_priv(dev);
749 struct ocelot *ocelot = priv->port.ocelot;
750 int port = priv->chip_port;
751
752 /* If the attached PHY device isn't capable of timestamping operations,
753 * use our own (when possible).
754 */
755 if (!phy_has_hwtstamp(dev->phydev) && ocelot->ptp) {
756 switch (cmd) {
757 case SIOCSHWTSTAMP:
758 return ocelot_hwstamp_set(ocelot, port, ifr);
759 case SIOCGHWTSTAMP:
760 return ocelot_hwstamp_get(ocelot, port, ifr);
761 }
762 }
763
764 return phy_mii_ioctl(dev->phydev, ifr, cmd);
765}
766
767static const struct net_device_ops ocelot_port_netdev_ops = {
768 .ndo_open = ocelot_port_open,
769 .ndo_stop = ocelot_port_stop,
770 .ndo_start_xmit = ocelot_port_xmit,
771 .ndo_set_rx_mode = ocelot_set_rx_mode,
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300772 .ndo_set_mac_address = ocelot_port_set_mac_address,
773 .ndo_get_stats64 = ocelot_get_stats64,
774 .ndo_fdb_add = ocelot_port_fdb_add,
775 .ndo_fdb_del = ocelot_port_fdb_del,
776 .ndo_fdb_dump = ocelot_port_fdb_dump,
777 .ndo_vlan_rx_add_vid = ocelot_vlan_rx_add_vid,
778 .ndo_vlan_rx_kill_vid = ocelot_vlan_rx_kill_vid,
779 .ndo_set_features = ocelot_set_features,
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300780 .ndo_setup_tc = ocelot_setup_tc,
Arnd Bergmanna7605372021-07-27 15:45:13 +0200781 .ndo_eth_ioctl = ocelot_ioctl,
Vladimir Oltean6c30384e2021-01-15 04:11:18 +0200782 .ndo_get_devlink_port = ocelot_get_devlink_port,
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300783};
784
Vladimir Oltean319e4dd2020-10-02 15:02:21 +0300785struct net_device *ocelot_port_to_netdev(struct ocelot *ocelot, int port)
786{
787 struct ocelot_port *ocelot_port = ocelot->ports[port];
788 struct ocelot_port_private *priv;
789
790 if (!ocelot_port)
791 return NULL;
792
793 priv = container_of(ocelot_port, struct ocelot_port_private, port);
794
795 return priv->dev;
796}
797
Vladimir Oltean7e38b032020-10-11 12:20:41 +0300798/* Checks if the net_device instance given to us originates from our driver */
799static bool ocelot_netdevice_dev_check(const struct net_device *dev)
Vladimir Oltean319e4dd2020-10-02 15:02:21 +0300800{
801 return dev->netdev_ops == &ocelot_port_netdev_ops;
802}
803
804int ocelot_netdev_to_port(struct net_device *dev)
805{
806 struct ocelot_port_private *priv;
807
Vladimir Oltean7e38b032020-10-11 12:20:41 +0300808 if (!dev || !ocelot_netdevice_dev_check(dev))
Vladimir Oltean319e4dd2020-10-02 15:02:21 +0300809 return -EINVAL;
810
811 priv = netdev_priv(dev);
812
813 return priv->chip_port;
814}
815
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300816static void ocelot_port_get_strings(struct net_device *netdev, u32 sset,
817 u8 *data)
818{
819 struct ocelot_port_private *priv = netdev_priv(netdev);
820 struct ocelot *ocelot = priv->port.ocelot;
821 int port = priv->chip_port;
822
823 ocelot_get_strings(ocelot, port, sset, data);
824}
825
826static void ocelot_port_get_ethtool_stats(struct net_device *dev,
827 struct ethtool_stats *stats,
828 u64 *data)
829{
830 struct ocelot_port_private *priv = netdev_priv(dev);
831 struct ocelot *ocelot = priv->port.ocelot;
832 int port = priv->chip_port;
833
834 ocelot_get_ethtool_stats(ocelot, port, data);
835}
836
837static int ocelot_port_get_sset_count(struct net_device *dev, int sset)
838{
839 struct ocelot_port_private *priv = netdev_priv(dev);
840 struct ocelot *ocelot = priv->port.ocelot;
841 int port = priv->chip_port;
842
843 return ocelot_get_sset_count(ocelot, port, sset);
844}
845
846static int ocelot_port_get_ts_info(struct net_device *dev,
847 struct ethtool_ts_info *info)
848{
849 struct ocelot_port_private *priv = netdev_priv(dev);
850 struct ocelot *ocelot = priv->port.ocelot;
851 int port = priv->chip_port;
852
853 if (!ocelot->ptp)
854 return ethtool_op_get_ts_info(dev, info);
855
856 return ocelot_get_ts_info(ocelot, port, info);
857}
858
859static const struct ethtool_ops ocelot_ethtool_ops = {
860 .get_strings = ocelot_port_get_strings,
861 .get_ethtool_stats = ocelot_port_get_ethtool_stats,
862 .get_sset_count = ocelot_port_get_sset_count,
863 .get_link_ksettings = phy_ethtool_get_link_ksettings,
864 .set_link_ksettings = phy_ethtool_set_link_ksettings,
865 .get_ts_info = ocelot_port_get_ts_info,
866};
867
868static void ocelot_port_attr_stp_state_set(struct ocelot *ocelot, int port,
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300869 u8 state)
870{
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300871 ocelot_bridge_stp_state_set(ocelot, port, state);
872}
873
874static void ocelot_port_attr_ageing_set(struct ocelot *ocelot, int port,
875 unsigned long ageing_clock_t)
876{
877 unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock_t);
878 u32 ageing_time = jiffies_to_msecs(ageing_jiffies);
879
880 ocelot_set_ageing_time(ocelot, ageing_time);
881}
882
883static void ocelot_port_attr_mc_set(struct ocelot *ocelot, int port, bool mc)
884{
885 u32 cpu_fwd_mcast = ANA_PORT_CPU_FWD_CFG_CPU_IGMP_REDIR_ENA |
886 ANA_PORT_CPU_FWD_CFG_CPU_MLD_REDIR_ENA |
887 ANA_PORT_CPU_FWD_CFG_CPU_IPMC_CTRL_COPY_ENA;
888 u32 val = 0;
889
890 if (mc)
891 val = cpu_fwd_mcast;
892
893 ocelot_rmw_gix(ocelot, val, cpu_fwd_mcast,
894 ANA_PORT_CPU_FWD_CFG, port);
895}
896
Vladimir Oltean69bfac92021-06-27 14:54:24 +0300897static int ocelot_port_attr_set(struct net_device *dev, const void *ctx,
Vladimir Oltean4c08c582021-02-12 17:15:51 +0200898 const struct switchdev_attr *attr,
899 struct netlink_ext_ack *extack)
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300900{
901 struct ocelot_port_private *priv = netdev_priv(dev);
902 struct ocelot *ocelot = priv->port.ocelot;
903 int port = priv->chip_port;
904 int err = 0;
905
Vladimir Oltean0d2cfbd2021-06-27 14:54:25 +0300906 if (ctx && ctx != priv)
907 return 0;
908
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300909 switch (attr->id) {
910 case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
Vladimir Olteanbae33f22021-01-09 02:01:50 +0200911 ocelot_port_attr_stp_state_set(ocelot, port, attr->u.stp_state);
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300912 break;
913 case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
914 ocelot_port_attr_ageing_set(ocelot, port, attr->u.ageing_time);
915 break;
916 case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
Vladimir Oltean3b95d1b2021-08-19 20:40:07 +0300917 ocelot_port_vlan_filtering(ocelot, port, attr->u.vlan_filtering,
918 extack);
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300919 break;
920 case SWITCHDEV_ATTR_ID_BRIDGE_MC_DISABLED:
921 ocelot_port_attr_mc_set(ocelot, port, !attr->u.mc_disabled);
922 break;
Vladimir Oltean421741e2021-02-12 17:15:59 +0200923 case SWITCHDEV_ATTR_ID_PORT_PRE_BRIDGE_FLAGS:
924 err = ocelot_port_pre_bridge_flags(ocelot, port,
925 attr->u.brport_flags);
926 break;
927 case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS:
928 ocelot_port_bridge_flags(ocelot, port, attr->u.brport_flags);
929 break;
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300930 default:
931 err = -EOPNOTSUPP;
932 break;
933 }
934
935 return err;
936}
937
Vladimir Oltean01af9402021-08-19 20:40:06 +0300938static int ocelot_vlan_vid_prepare(struct net_device *dev, u16 vid, bool pvid,
939 bool untagged, struct netlink_ext_ack *extack)
940{
941 struct ocelot_port_private *priv = netdev_priv(dev);
942 struct ocelot_port *ocelot_port = &priv->port;
943 struct ocelot *ocelot = ocelot_port->ocelot;
944 int port = priv->chip_port;
945
946 return ocelot_vlan_prepare(ocelot, port, vid, pvid, untagged, extack);
947}
948
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300949static int ocelot_port_obj_add_vlan(struct net_device *dev,
Vladimir Oltean01af9402021-08-19 20:40:06 +0300950 const struct switchdev_obj_port_vlan *vlan,
951 struct netlink_ext_ack *extack)
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300952{
Vladimir Olteanb7a9e0d2021-01-09 02:01:46 +0200953 bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
954 bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300955 int ret;
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300956
Vladimir Oltean01af9402021-08-19 20:40:06 +0300957 ret = ocelot_vlan_vid_prepare(dev, vlan->vid, pvid, untagged, extack);
Vladimir Olteanffb68fc2021-01-09 02:01:48 +0200958 if (ret)
959 return ret;
Vladimir Oltean2f0402f2020-10-31 12:29:15 +0200960
Vladimir Olteanffb68fc2021-01-09 02:01:48 +0200961 return ocelot_vlan_vid_add(dev, vlan->vid, pvid, untagged);
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300962}
963
Vladimir Oltean209edf92020-06-21 14:46:01 +0300964static int ocelot_port_obj_add_mdb(struct net_device *dev,
Vladimir Olteanffb68fc2021-01-09 02:01:48 +0200965 const struct switchdev_obj_port_mdb *mdb)
Vladimir Oltean209edf92020-06-21 14:46:01 +0300966{
967 struct ocelot_port_private *priv = netdev_priv(dev);
968 struct ocelot_port *ocelot_port = &priv->port;
969 struct ocelot *ocelot = ocelot_port->ocelot;
970 int port = priv->chip_port;
971
Vladimir Oltean209edf92020-06-21 14:46:01 +0300972 return ocelot_port_mdb_add(ocelot, port, mdb);
973}
974
975static int ocelot_port_obj_del_mdb(struct net_device *dev,
976 const struct switchdev_obj_port_mdb *mdb)
977{
978 struct ocelot_port_private *priv = netdev_priv(dev);
979 struct ocelot_port *ocelot_port = &priv->port;
980 struct ocelot *ocelot = ocelot_port->ocelot;
981 int port = priv->chip_port;
982
983 return ocelot_port_mdb_del(ocelot, port, mdb);
984}
985
Horatiu Vulturd8ea7ff2021-02-16 22:42:03 +0100986static int ocelot_port_obj_mrp_add(struct net_device *dev,
987 const struct switchdev_obj_mrp *mrp)
988{
989 struct ocelot_port_private *priv = netdev_priv(dev);
990 struct ocelot_port *ocelot_port = &priv->port;
991 struct ocelot *ocelot = ocelot_port->ocelot;
992 int port = priv->chip_port;
993
994 return ocelot_mrp_add(ocelot, port, mrp);
995}
996
997static int ocelot_port_obj_mrp_del(struct net_device *dev,
998 const struct switchdev_obj_mrp *mrp)
999{
1000 struct ocelot_port_private *priv = netdev_priv(dev);
1001 struct ocelot_port *ocelot_port = &priv->port;
1002 struct ocelot *ocelot = ocelot_port->ocelot;
1003 int port = priv->chip_port;
1004
1005 return ocelot_mrp_del(ocelot, port, mrp);
1006}
1007
1008static int
1009ocelot_port_obj_mrp_add_ring_role(struct net_device *dev,
1010 const struct switchdev_obj_ring_role_mrp *mrp)
1011{
1012 struct ocelot_port_private *priv = netdev_priv(dev);
1013 struct ocelot_port *ocelot_port = &priv->port;
1014 struct ocelot *ocelot = ocelot_port->ocelot;
1015 int port = priv->chip_port;
1016
1017 return ocelot_mrp_add_ring_role(ocelot, port, mrp);
1018}
1019
1020static int
1021ocelot_port_obj_mrp_del_ring_role(struct net_device *dev,
1022 const struct switchdev_obj_ring_role_mrp *mrp)
1023{
1024 struct ocelot_port_private *priv = netdev_priv(dev);
1025 struct ocelot_port *ocelot_port = &priv->port;
1026 struct ocelot *ocelot = ocelot_port->ocelot;
1027 int port = priv->chip_port;
1028
1029 return ocelot_mrp_del_ring_role(ocelot, port, mrp);
1030}
1031
Vladimir Oltean69bfac92021-06-27 14:54:24 +03001032static int ocelot_port_obj_add(struct net_device *dev, const void *ctx,
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001033 const struct switchdev_obj *obj,
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001034 struct netlink_ext_ack *extack)
1035{
Vladimir Oltean0d2cfbd2021-06-27 14:54:25 +03001036 struct ocelot_port_private *priv = netdev_priv(dev);
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001037 int ret = 0;
1038
Vladimir Oltean0d2cfbd2021-06-27 14:54:25 +03001039 if (ctx && ctx != priv)
1040 return 0;
1041
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001042 switch (obj->id) {
1043 case SWITCHDEV_OBJ_ID_PORT_VLAN:
1044 ret = ocelot_port_obj_add_vlan(dev,
Vladimir Oltean01af9402021-08-19 20:40:06 +03001045 SWITCHDEV_OBJ_PORT_VLAN(obj),
1046 extack);
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001047 break;
1048 case SWITCHDEV_OBJ_ID_PORT_MDB:
Vladimir Olteanffb68fc2021-01-09 02:01:48 +02001049 ret = ocelot_port_obj_add_mdb(dev, SWITCHDEV_OBJ_PORT_MDB(obj));
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001050 break;
Horatiu Vulturd8ea7ff2021-02-16 22:42:03 +01001051 case SWITCHDEV_OBJ_ID_MRP:
1052 ret = ocelot_port_obj_mrp_add(dev, SWITCHDEV_OBJ_MRP(obj));
1053 break;
1054 case SWITCHDEV_OBJ_ID_RING_ROLE_MRP:
1055 ret = ocelot_port_obj_mrp_add_ring_role(dev,
1056 SWITCHDEV_OBJ_RING_ROLE_MRP(obj));
1057 break;
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001058 default:
1059 return -EOPNOTSUPP;
1060 }
1061
1062 return ret;
1063}
1064
Vladimir Oltean69bfac92021-06-27 14:54:24 +03001065static int ocelot_port_obj_del(struct net_device *dev, const void *ctx,
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001066 const struct switchdev_obj *obj)
1067{
Vladimir Oltean0d2cfbd2021-06-27 14:54:25 +03001068 struct ocelot_port_private *priv = netdev_priv(dev);
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001069 int ret = 0;
1070
Vladimir Oltean0d2cfbd2021-06-27 14:54:25 +03001071 if (ctx && ctx != priv)
1072 return 0;
1073
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001074 switch (obj->id) {
1075 case SWITCHDEV_OBJ_ID_PORT_VLAN:
Vladimir Olteanb7a9e0d2021-01-09 02:01:46 +02001076 ret = ocelot_vlan_vid_del(dev,
1077 SWITCHDEV_OBJ_PORT_VLAN(obj)->vid);
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001078 break;
1079 case SWITCHDEV_OBJ_ID_PORT_MDB:
1080 ret = ocelot_port_obj_del_mdb(dev, SWITCHDEV_OBJ_PORT_MDB(obj));
1081 break;
Horatiu Vulturd8ea7ff2021-02-16 22:42:03 +01001082 case SWITCHDEV_OBJ_ID_MRP:
1083 ret = ocelot_port_obj_mrp_del(dev, SWITCHDEV_OBJ_MRP(obj));
1084 break;
1085 case SWITCHDEV_OBJ_ID_RING_ROLE_MRP:
1086 ret = ocelot_port_obj_mrp_del_ring_role(dev,
1087 SWITCHDEV_OBJ_RING_ROLE_MRP(obj));
1088 break;
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001089 default:
1090 return -EOPNOTSUPP;
1091 }
1092
1093 return ret;
1094}
1095
Vladimir Olteane4bd44e2021-03-23 01:51:52 +02001096static void ocelot_inherit_brport_flags(struct ocelot *ocelot, int port,
1097 struct net_device *brport_dev)
1098{
1099 struct switchdev_brport_flags flags = {0};
1100 int flag;
1101
1102 flags.mask = BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD | BR_BCAST_FLOOD;
1103
1104 for_each_set_bit(flag, &flags.mask, 32)
1105 if (br_port_flag_is_set(brport_dev, BIT(flag)))
1106 flags.val |= BIT(flag);
1107
1108 ocelot_port_bridge_flags(ocelot, port, flags);
1109}
1110
1111static void ocelot_clear_brport_flags(struct ocelot *ocelot, int port)
1112{
1113 struct switchdev_brport_flags flags;
1114
1115 flags.mask = BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD | BR_BCAST_FLOOD;
1116 flags.val = flags.mask & ~BR_LEARNING;
1117
1118 ocelot_port_bridge_flags(ocelot, port, flags);
1119}
1120
1121static int ocelot_switchdev_sync(struct ocelot *ocelot, int port,
1122 struct net_device *brport_dev,
1123 struct net_device *bridge_dev,
1124 struct netlink_ext_ack *extack)
1125{
1126 clock_t ageing_time;
1127 u8 stp_state;
Vladimir Oltean0d2cfbd2021-06-27 14:54:25 +03001128
Vladimir Olteane4bd44e2021-03-23 01:51:52 +02001129 ocelot_inherit_brport_flags(ocelot, port, brport_dev);
1130
1131 stp_state = br_port_get_stp_state(brport_dev);
1132 ocelot_bridge_stp_state_set(ocelot, port, stp_state);
1133
Vladimir Olteane4bd44e2021-03-23 01:51:52 +02001134 ageing_time = br_get_ageing_time(bridge_dev);
1135 ocelot_port_attr_ageing_set(ocelot, port, ageing_time);
1136
Vladimir Oltean4e51bf42021-07-21 19:24:03 +03001137 return ocelot_port_vlan_filtering(ocelot, port,
Vladimir Oltean3b95d1b2021-08-19 20:40:07 +03001138 br_vlan_enabled(bridge_dev),
1139 extack);
Vladimir Olteane4bd44e2021-03-23 01:51:52 +02001140}
1141
1142static int ocelot_switchdev_unsync(struct ocelot *ocelot, int port)
1143{
1144 int err;
1145
Vladimir Oltean3b95d1b2021-08-19 20:40:07 +03001146 err = ocelot_port_vlan_filtering(ocelot, port, false, NULL);
Vladimir Olteane4bd44e2021-03-23 01:51:52 +02001147 if (err)
1148 return err;
1149
1150 ocelot_clear_brport_flags(ocelot, port);
1151
1152 ocelot_bridge_stp_state_set(ocelot, port, BR_STATE_FORWARDING);
1153
1154 return 0;
1155}
1156
Vladimir Oltean81ef35e2021-03-23 01:51:51 +02001157static int ocelot_netdevice_bridge_join(struct net_device *dev,
Vladimir Olteane4bd44e2021-03-23 01:51:52 +02001158 struct net_device *brport_dev,
Vladimir Oltean81ef35e2021-03-23 01:51:51 +02001159 struct net_device *bridge,
1160 struct netlink_ext_ack *extack)
Vladimir Oltean421741e2021-02-12 17:15:59 +02001161{
Vladimir Oltean81ef35e2021-03-23 01:51:51 +02001162 struct ocelot_port_private *priv = netdev_priv(dev);
1163 struct ocelot_port *ocelot_port = &priv->port;
1164 struct ocelot *ocelot = ocelot_port->ocelot;
Vladimir Oltean81ef35e2021-03-23 01:51:51 +02001165 int port = priv->chip_port;
Vladimir Oltean421741e2021-02-12 17:15:59 +02001166 int err;
1167
Vladimir Olteane4bd44e2021-03-23 01:51:52 +02001168 ocelot_port_bridge_join(ocelot, port, bridge);
Vladimir Oltean421741e2021-02-12 17:15:59 +02001169
Vladimir Oltean4e51bf42021-07-21 19:24:03 +03001170 err = switchdev_bridge_port_offload(brport_dev, dev, priv,
1171 &ocelot_netdevice_nb,
1172 &ocelot_switchdev_blocking_nb,
Tobias Waldekranz47211192021-07-22 18:55:38 +03001173 false, extack);
Vladimir Oltean2f5dc002021-07-21 19:24:01 +03001174 if (err)
1175 goto err_switchdev_offload;
1176
Vladimir Olteane4bd44e2021-03-23 01:51:52 +02001177 err = ocelot_switchdev_sync(ocelot, port, brport_dev, bridge, extack);
Vladimir Oltean421741e2021-02-12 17:15:59 +02001178 if (err)
Vladimir Olteane4bd44e2021-03-23 01:51:52 +02001179 goto err_switchdev_sync;
Vladimir Oltean421741e2021-02-12 17:15:59 +02001180
1181 return 0;
Vladimir Olteane4bd44e2021-03-23 01:51:52 +02001182
1183err_switchdev_sync:
Vladimir Oltean4e51bf42021-07-21 19:24:03 +03001184 switchdev_bridge_port_unoffload(brport_dev, priv,
1185 &ocelot_netdevice_nb,
1186 &ocelot_switchdev_blocking_nb);
Vladimir Oltean2f5dc002021-07-21 19:24:01 +03001187err_switchdev_offload:
Vladimir Olteane4bd44e2021-03-23 01:51:52 +02001188 ocelot_port_bridge_leave(ocelot, port, bridge);
1189 return err;
Vladimir Oltean421741e2021-02-12 17:15:59 +02001190}
1191
Vladimir Oltean4e51bf42021-07-21 19:24:03 +03001192static void ocelot_netdevice_pre_bridge_leave(struct net_device *dev,
1193 struct net_device *brport_dev)
Vladimir Oltean2f5dc002021-07-21 19:24:01 +03001194{
Vladimir Oltean4e51bf42021-07-21 19:24:03 +03001195 struct ocelot_port_private *priv = netdev_priv(dev);
1196
1197 switchdev_bridge_port_unoffload(brport_dev, priv,
1198 &ocelot_netdevice_nb,
1199 &ocelot_switchdev_blocking_nb);
Vladimir Oltean2f5dc002021-07-21 19:24:01 +03001200}
1201
Vladimir Oltean81ef35e2021-03-23 01:51:51 +02001202static int ocelot_netdevice_bridge_leave(struct net_device *dev,
Vladimir Olteane4bd44e2021-03-23 01:51:52 +02001203 struct net_device *brport_dev,
Vladimir Oltean421741e2021-02-12 17:15:59 +02001204 struct net_device *bridge)
1205{
Vladimir Oltean81ef35e2021-03-23 01:51:51 +02001206 struct ocelot_port_private *priv = netdev_priv(dev);
1207 struct ocelot_port *ocelot_port = &priv->port;
1208 struct ocelot *ocelot = ocelot_port->ocelot;
Vladimir Oltean81ef35e2021-03-23 01:51:51 +02001209 int port = priv->chip_port;
Vladimir Oltean421741e2021-02-12 17:15:59 +02001210 int err;
1211
Vladimir Olteane4bd44e2021-03-23 01:51:52 +02001212 err = ocelot_switchdev_unsync(ocelot, port);
1213 if (err)
1214 return err;
Vladimir Oltean421741e2021-02-12 17:15:59 +02001215
Vladimir Olteane4bd44e2021-03-23 01:51:52 +02001216 ocelot_port_bridge_leave(ocelot, port, bridge);
Vladimir Oltean421741e2021-02-12 17:15:59 +02001217
Vladimir Olteane4bd44e2021-03-23 01:51:52 +02001218 return 0;
Vladimir Oltean421741e2021-02-12 17:15:59 +02001219}
1220
Vladimir Oltean81ef35e2021-03-23 01:51:51 +02001221static int ocelot_netdevice_lag_join(struct net_device *dev,
1222 struct net_device *bond,
1223 struct netdev_lag_upper_info *info,
1224 struct netlink_ext_ack *extack)
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001225{
1226 struct ocelot_port_private *priv = netdev_priv(dev);
1227 struct ocelot_port *ocelot_port = &priv->port;
1228 struct ocelot *ocelot = ocelot_port->ocelot;
Vladimir Oltean81ef35e2021-03-23 01:51:51 +02001229 struct net_device *bridge_dev;
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001230 int port = priv->chip_port;
Vladimir Oltean81ef35e2021-03-23 01:51:51 +02001231 int err;
1232
1233 err = ocelot_port_lag_join(ocelot, port, bond, info);
1234 if (err == -EOPNOTSUPP) {
1235 NL_SET_ERR_MSG_MOD(extack, "Offloading not supported");
1236 return 0;
1237 }
1238
1239 bridge_dev = netdev_master_upper_dev_get(bond);
1240 if (!bridge_dev || !netif_is_bridge_master(bridge_dev))
1241 return 0;
1242
Vladimir Olteane4bd44e2021-03-23 01:51:52 +02001243 err = ocelot_netdevice_bridge_join(dev, bond, bridge_dev, extack);
Vladimir Oltean81ef35e2021-03-23 01:51:51 +02001244 if (err)
1245 goto err_bridge_join;
1246
1247 return 0;
1248
1249err_bridge_join:
1250 ocelot_port_lag_leave(ocelot, port, bond);
1251 return err;
1252}
1253
Vladimir Oltean2f5dc002021-07-21 19:24:01 +03001254static void ocelot_netdevice_pre_lag_leave(struct net_device *dev,
1255 struct net_device *bond)
1256{
1257 struct net_device *bridge_dev;
1258
1259 bridge_dev = netdev_master_upper_dev_get(bond);
1260 if (!bridge_dev || !netif_is_bridge_master(bridge_dev))
1261 return;
1262
Vladimir Oltean4e51bf42021-07-21 19:24:03 +03001263 ocelot_netdevice_pre_bridge_leave(dev, bond);
Vladimir Oltean2f5dc002021-07-21 19:24:01 +03001264}
1265
Vladimir Oltean81ef35e2021-03-23 01:51:51 +02001266static int ocelot_netdevice_lag_leave(struct net_device *dev,
1267 struct net_device *bond)
1268{
1269 struct ocelot_port_private *priv = netdev_priv(dev);
1270 struct ocelot_port *ocelot_port = &priv->port;
1271 struct ocelot *ocelot = ocelot_port->ocelot;
1272 struct net_device *bridge_dev;
1273 int port = priv->chip_port;
1274
1275 ocelot_port_lag_leave(ocelot, port, bond);
1276
1277 bridge_dev = netdev_master_upper_dev_get(bond);
1278 if (!bridge_dev || !netif_is_bridge_master(bridge_dev))
1279 return 0;
1280
Vladimir Olteane4bd44e2021-03-23 01:51:52 +02001281 return ocelot_netdevice_bridge_leave(dev, bond, bridge_dev);
Vladimir Oltean81ef35e2021-03-23 01:51:51 +02001282}
1283
1284static int ocelot_netdevice_changeupper(struct net_device *dev,
Vladimir Olteane56c6bb2021-07-13 12:33:50 +03001285 struct net_device *brport_dev,
Vladimir Oltean81ef35e2021-03-23 01:51:51 +02001286 struct netdev_notifier_changeupper_info *info)
1287{
1288 struct netlink_ext_ack *extack;
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001289 int err = 0;
1290
Vladimir Oltean81ef35e2021-03-23 01:51:51 +02001291 extack = netdev_notifier_info_to_extack(&info->info);
1292
Vladimir Oltean662981b2021-02-06 00:02:10 +02001293 if (netif_is_bridge_master(info->upper_dev)) {
Vladimir Oltean81ef35e2021-03-23 01:51:51 +02001294 if (info->linking)
Vladimir Olteane56c6bb2021-07-13 12:33:50 +03001295 err = ocelot_netdevice_bridge_join(dev, brport_dev,
Vladimir Olteane4bd44e2021-03-23 01:51:52 +02001296 info->upper_dev,
Vladimir Oltean81ef35e2021-03-23 01:51:51 +02001297 extack);
1298 else
Vladimir Olteane56c6bb2021-07-13 12:33:50 +03001299 err = ocelot_netdevice_bridge_leave(dev, brport_dev,
Vladimir Olteane4bd44e2021-03-23 01:51:52 +02001300 info->upper_dev);
Vladimir Oltean662981b2021-02-06 00:02:10 +02001301 }
1302 if (netif_is_lag_master(info->upper_dev)) {
Vladimir Oltean81ef35e2021-03-23 01:51:51 +02001303 if (info->linking)
1304 err = ocelot_netdevice_lag_join(dev, info->upper_dev,
1305 info->upper_info, extack);
1306 else
1307 ocelot_netdevice_lag_leave(dev, info->upper_dev);
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001308 }
1309
Vladimir Oltean41e66fa2021-02-06 00:02:11 +02001310 return notifier_from_errno(err);
1311}
1312
Vladimir Oltean81ef35e2021-03-23 01:51:51 +02001313/* Treat CHANGEUPPER events on an offloaded LAG as individual CHANGEUPPER
1314 * events for the lower physical ports of the LAG.
1315 * If the LAG upper isn't offloaded, ignore its CHANGEUPPER events.
1316 * In case the LAG joined a bridge, notify that we are offloading it and can do
1317 * forwarding in hardware towards it.
1318 */
Vladimir Oltean41e66fa2021-02-06 00:02:11 +02001319static int
1320ocelot_netdevice_lag_changeupper(struct net_device *dev,
1321 struct netdev_notifier_changeupper_info *info)
1322{
1323 struct net_device *lower;
1324 struct list_head *iter;
1325 int err = NOTIFY_DONE;
1326
1327 netdev_for_each_lower_dev(dev, lower, iter) {
Vladimir Oltean81ef35e2021-03-23 01:51:51 +02001328 struct ocelot_port_private *priv = netdev_priv(lower);
1329 struct ocelot_port *ocelot_port = &priv->port;
1330
1331 if (ocelot_port->bond != dev)
1332 return NOTIFY_OK;
1333
Vladimir Olteane56c6bb2021-07-13 12:33:50 +03001334 err = ocelot_netdevice_changeupper(lower, dev, info);
Vladimir Oltean41e66fa2021-02-06 00:02:11 +02001335 if (err)
1336 return notifier_from_errno(err);
1337 }
1338
1339 return NOTIFY_DONE;
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001340}
1341
Vladimir Oltean23ca3b72021-02-06 00:02:19 +02001342static int
Vladimir Oltean2f5dc002021-07-21 19:24:01 +03001343ocelot_netdevice_prechangeupper(struct net_device *dev,
1344 struct net_device *brport_dev,
1345 struct netdev_notifier_changeupper_info *info)
1346{
1347 if (netif_is_bridge_master(info->upper_dev) && !info->linking)
Vladimir Oltean4e51bf42021-07-21 19:24:03 +03001348 ocelot_netdevice_pre_bridge_leave(dev, brport_dev);
Vladimir Oltean2f5dc002021-07-21 19:24:01 +03001349
1350 if (netif_is_lag_master(info->upper_dev) && !info->linking)
1351 ocelot_netdevice_pre_lag_leave(dev, info->upper_dev);
1352
1353 return NOTIFY_DONE;
1354}
1355
1356static int
1357ocelot_netdevice_lag_prechangeupper(struct net_device *dev,
1358 struct netdev_notifier_changeupper_info *info)
1359{
1360 struct net_device *lower;
1361 struct list_head *iter;
1362 int err = NOTIFY_DONE;
1363
1364 netdev_for_each_lower_dev(dev, lower, iter) {
1365 struct ocelot_port_private *priv = netdev_priv(lower);
1366 struct ocelot_port *ocelot_port = &priv->port;
1367
1368 if (ocelot_port->bond != dev)
1369 return NOTIFY_OK;
1370
1371 err = ocelot_netdevice_prechangeupper(dev, lower, info);
1372 if (err)
1373 return err;
1374 }
1375
1376 return NOTIFY_DONE;
1377}
1378
1379static int
Vladimir Oltean23ca3b72021-02-06 00:02:19 +02001380ocelot_netdevice_changelowerstate(struct net_device *dev,
1381 struct netdev_lag_lower_state_info *info)
1382{
1383 struct ocelot_port_private *priv = netdev_priv(dev);
1384 bool is_active = info->link_up && info->tx_enabled;
1385 struct ocelot_port *ocelot_port = &priv->port;
1386 struct ocelot *ocelot = ocelot_port->ocelot;
1387 int port = priv->chip_port;
1388
1389 if (!ocelot_port->bond)
1390 return NOTIFY_DONE;
1391
1392 if (ocelot_port->lag_tx_active == is_active)
1393 return NOTIFY_DONE;
1394
1395 ocelot_port_lag_change(ocelot, port, is_active);
1396
1397 return NOTIFY_OK;
1398}
1399
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001400static int ocelot_netdevice_event(struct notifier_block *unused,
1401 unsigned long event, void *ptr)
1402{
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001403 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001404
Vladimir Oltean41e66fa2021-02-06 00:02:11 +02001405 switch (event) {
Vladimir Oltean2f5dc002021-07-21 19:24:01 +03001406 case NETDEV_PRECHANGEUPPER: {
1407 struct netdev_notifier_changeupper_info *info = ptr;
1408
1409 if (ocelot_netdevice_dev_check(dev))
1410 return ocelot_netdevice_prechangeupper(dev, dev, info);
1411
1412 if (netif_is_lag_master(dev))
1413 return ocelot_netdevice_lag_prechangeupper(dev, info);
1414
1415 break;
1416 }
Vladimir Oltean41e66fa2021-02-06 00:02:11 +02001417 case NETDEV_CHANGEUPPER: {
1418 struct netdev_notifier_changeupper_info *info = ptr;
1419
1420 if (ocelot_netdevice_dev_check(dev))
Vladimir Olteane56c6bb2021-07-13 12:33:50 +03001421 return ocelot_netdevice_changeupper(dev, dev, info);
Vladimir Oltean41e66fa2021-02-06 00:02:11 +02001422
1423 if (netif_is_lag_master(dev))
1424 return ocelot_netdevice_lag_changeupper(dev, info);
1425
1426 break;
1427 }
Vladimir Oltean23ca3b72021-02-06 00:02:19 +02001428 case NETDEV_CHANGELOWERSTATE: {
1429 struct netdev_notifier_changelowerstate_info *info = ptr;
1430
1431 if (!ocelot_netdevice_dev_check(dev))
1432 break;
1433
1434 return ocelot_netdevice_changelowerstate(dev,
1435 info->lower_state_info);
1436 }
Vladimir Oltean41e66fa2021-02-06 00:02:11 +02001437 default:
1438 break;
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001439 }
1440
Vladimir Oltean41e66fa2021-02-06 00:02:11 +02001441 return NOTIFY_DONE;
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001442}
1443
1444struct notifier_block ocelot_netdevice_nb __read_mostly = {
1445 .notifier_call = ocelot_netdevice_event,
1446};
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001447
1448static int ocelot_switchdev_event(struct notifier_block *unused,
1449 unsigned long event, void *ptr)
1450{
1451 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
1452 int err;
1453
1454 switch (event) {
1455 case SWITCHDEV_PORT_ATTR_SET:
1456 err = switchdev_handle_port_attr_set(dev, ptr,
1457 ocelot_netdevice_dev_check,
1458 ocelot_port_attr_set);
1459 return notifier_from_errno(err);
1460 }
1461
1462 return NOTIFY_DONE;
1463}
1464
1465struct notifier_block ocelot_switchdev_nb __read_mostly = {
1466 .notifier_call = ocelot_switchdev_event,
1467};
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001468
1469static int ocelot_switchdev_blocking_event(struct notifier_block *unused,
1470 unsigned long event, void *ptr)
1471{
1472 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
1473 int err;
1474
1475 switch (event) {
1476 /* Blocking events. */
1477 case SWITCHDEV_PORT_OBJ_ADD:
1478 err = switchdev_handle_port_obj_add(dev, ptr,
1479 ocelot_netdevice_dev_check,
1480 ocelot_port_obj_add);
1481 return notifier_from_errno(err);
1482 case SWITCHDEV_PORT_OBJ_DEL:
1483 err = switchdev_handle_port_obj_del(dev, ptr,
1484 ocelot_netdevice_dev_check,
1485 ocelot_port_obj_del);
1486 return notifier_from_errno(err);
1487 case SWITCHDEV_PORT_ATTR_SET:
1488 err = switchdev_handle_port_attr_set(dev, ptr,
1489 ocelot_netdevice_dev_check,
1490 ocelot_port_attr_set);
1491 return notifier_from_errno(err);
1492 }
1493
1494 return NOTIFY_DONE;
1495}
1496
1497struct notifier_block ocelot_switchdev_blocking_nb __read_mostly = {
1498 .notifier_call = ocelot_switchdev_blocking_event,
1499};
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001500
Vladimir Olteane6e12df2021-08-15 04:47:48 +03001501static void vsc7514_phylink_validate(struct phylink_config *config,
1502 unsigned long *supported,
1503 struct phylink_link_state *state)
1504{
1505 struct net_device *ndev = to_net_dev(config->dev);
1506 struct ocelot_port_private *priv = netdev_priv(ndev);
1507 struct ocelot_port *ocelot_port = &priv->port;
1508 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = {};
1509
1510 if (state->interface != PHY_INTERFACE_MODE_NA &&
1511 state->interface != ocelot_port->phy_mode) {
Sean Anderson49730562021-10-22 18:41:04 -04001512 linkmode_zero(supported);
Vladimir Olteane6e12df2021-08-15 04:47:48 +03001513 return;
1514 }
1515
1516 phylink_set_port_modes(mask);
1517
1518 phylink_set(mask, Pause);
1519 phylink_set(mask, Autoneg);
1520 phylink_set(mask, Asym_Pause);
1521 phylink_set(mask, 10baseT_Half);
1522 phylink_set(mask, 10baseT_Full);
1523 phylink_set(mask, 100baseT_Half);
1524 phylink_set(mask, 100baseT_Full);
1525 phylink_set(mask, 1000baseT_Half);
1526 phylink_set(mask, 1000baseT_Full);
1527 phylink_set(mask, 1000baseX_Full);
1528 phylink_set(mask, 2500baseT_Full);
1529 phylink_set(mask, 2500baseX_Full);
1530
Sean Anderson49730562021-10-22 18:41:04 -04001531 linkmode_and(supported, supported, mask);
1532 linkmode_and(state->advertising, state->advertising, mask);
Vladimir Olteane6e12df2021-08-15 04:47:48 +03001533}
1534
1535static void vsc7514_phylink_mac_config(struct phylink_config *config,
1536 unsigned int link_an_mode,
1537 const struct phylink_link_state *state)
1538{
1539 struct net_device *ndev = to_net_dev(config->dev);
1540 struct ocelot_port_private *priv = netdev_priv(ndev);
1541 struct ocelot_port *ocelot_port = &priv->port;
1542
1543 /* Disable HDX fast control */
1544 ocelot_port_writel(ocelot_port, DEV_PORT_MISC_HDX_FAST_DIS,
1545 DEV_PORT_MISC);
1546
1547 /* SGMII only for now */
1548 ocelot_port_writel(ocelot_port, PCS1G_MODE_CFG_SGMII_MODE_ENA,
1549 PCS1G_MODE_CFG);
1550 ocelot_port_writel(ocelot_port, PCS1G_SD_CFG_SD_SEL, PCS1G_SD_CFG);
1551
1552 /* Enable PCS */
1553 ocelot_port_writel(ocelot_port, PCS1G_CFG_PCS_ENA, PCS1G_CFG);
1554
1555 /* No aneg on SGMII */
1556 ocelot_port_writel(ocelot_port, 0, PCS1G_ANEG_CFG);
1557
1558 /* No loopback */
1559 ocelot_port_writel(ocelot_port, 0, PCS1G_LB_CFG);
1560}
1561
1562static void vsc7514_phylink_mac_link_down(struct phylink_config *config,
1563 unsigned int link_an_mode,
1564 phy_interface_t interface)
1565{
1566 struct net_device *ndev = to_net_dev(config->dev);
1567 struct ocelot_port_private *priv = netdev_priv(ndev);
1568 struct ocelot *ocelot = priv->port.ocelot;
1569 int port = priv->chip_port;
1570
1571 ocelot_phylink_mac_link_down(ocelot, port, link_an_mode, interface,
1572 OCELOT_MAC_QUIRKS);
1573}
1574
1575static void vsc7514_phylink_mac_link_up(struct phylink_config *config,
1576 struct phy_device *phydev,
1577 unsigned int link_an_mode,
1578 phy_interface_t interface,
1579 int speed, int duplex,
1580 bool tx_pause, bool rx_pause)
1581{
1582 struct net_device *ndev = to_net_dev(config->dev);
1583 struct ocelot_port_private *priv = netdev_priv(ndev);
1584 struct ocelot *ocelot = priv->port.ocelot;
1585 int port = priv->chip_port;
1586
1587 ocelot_phylink_mac_link_up(ocelot, port, phydev, link_an_mode,
1588 interface, speed, duplex,
1589 tx_pause, rx_pause, OCELOT_MAC_QUIRKS);
1590}
1591
1592static const struct phylink_mac_ops ocelot_phylink_ops = {
1593 .validate = vsc7514_phylink_validate,
1594 .mac_config = vsc7514_phylink_mac_config,
1595 .mac_link_down = vsc7514_phylink_mac_link_down,
1596 .mac_link_up = vsc7514_phylink_mac_link_up,
1597};
1598
1599static int ocelot_port_phylink_create(struct ocelot *ocelot, int port,
1600 struct device_node *portnp)
1601{
1602 struct ocelot_port *ocelot_port = ocelot->ports[port];
1603 struct ocelot_port_private *priv;
1604 struct device *dev = ocelot->dev;
1605 phy_interface_t phy_mode;
1606 struct phylink *phylink;
1607 int err;
1608
1609 of_get_phy_mode(portnp, &phy_mode);
1610 /* DT bindings of internal PHY ports are broken and don't
1611 * specify a phy-mode
1612 */
1613 if (phy_mode == PHY_INTERFACE_MODE_NA)
1614 phy_mode = PHY_INTERFACE_MODE_INTERNAL;
1615
1616 if (phy_mode != PHY_INTERFACE_MODE_SGMII &&
1617 phy_mode != PHY_INTERFACE_MODE_QSGMII &&
1618 phy_mode != PHY_INTERFACE_MODE_INTERNAL) {
1619 dev_err(dev, "unsupported phy mode %s for port %d\n",
1620 phy_modes(phy_mode), port);
1621 return -EINVAL;
1622 }
1623
1624 /* Ensure clock signals and speed are set on all QSGMII links */
1625 if (phy_mode == PHY_INTERFACE_MODE_QSGMII)
1626 ocelot_port_rmwl(ocelot_port, 0,
1627 DEV_CLOCK_CFG_MAC_TX_RST |
Wan Jiabing74a3bc42021-10-11 10:27:41 +08001628 DEV_CLOCK_CFG_MAC_RX_RST,
Vladimir Olteane6e12df2021-08-15 04:47:48 +03001629 DEV_CLOCK_CFG);
1630
1631 ocelot_port->phy_mode = phy_mode;
1632
1633 if (phy_mode != PHY_INTERFACE_MODE_INTERNAL) {
1634 struct phy *serdes = of_phy_get(portnp, NULL);
1635
1636 if (IS_ERR(serdes)) {
1637 err = PTR_ERR(serdes);
1638 dev_err_probe(dev, err,
1639 "missing SerDes phys for port %d\n",
1640 port);
1641 return err;
1642 }
1643
1644 err = phy_set_mode_ext(serdes, PHY_MODE_ETHERNET, phy_mode);
1645 of_phy_put(serdes);
1646 if (err) {
1647 dev_err(dev, "Could not SerDes mode on port %d: %pe\n",
1648 port, ERR_PTR(err));
1649 return err;
1650 }
1651 }
1652
1653 priv = container_of(ocelot_port, struct ocelot_port_private, port);
1654
1655 priv->phylink_config.dev = &priv->dev->dev;
1656 priv->phylink_config.type = PHYLINK_NETDEV;
1657
1658 phylink = phylink_create(&priv->phylink_config,
1659 of_fwnode_handle(portnp),
1660 phy_mode, &ocelot_phylink_ops);
1661 if (IS_ERR(phylink)) {
1662 err = PTR_ERR(phylink);
1663 dev_err(dev, "Could not create phylink (%pe)\n", phylink);
1664 return err;
1665 }
1666
1667 priv->phylink = phylink;
1668
1669 err = phylink_of_phy_connect(phylink, portnp, 0);
1670 if (err) {
1671 dev_err(dev, "Could not connect to PHY: %pe\n", ERR_PTR(err));
1672 phylink_destroy(phylink);
1673 priv->phylink = NULL;
1674 return err;
1675 }
1676
1677 return 0;
1678}
1679
Vladimir Oltean91c724c2020-07-13 19:57:01 +03001680int ocelot_probe_port(struct ocelot *ocelot, int port, struct regmap *target,
Vladimir Olteane6e12df2021-08-15 04:47:48 +03001681 struct device_node *portnp)
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001682{
1683 struct ocelot_port_private *priv;
1684 struct ocelot_port *ocelot_port;
1685 struct net_device *dev;
1686 int err;
1687
1688 dev = alloc_etherdev(sizeof(struct ocelot_port_private));
1689 if (!dev)
1690 return -ENOMEM;
1691 SET_NETDEV_DEV(dev, ocelot->dev);
1692 priv = netdev_priv(dev);
1693 priv->dev = dev;
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001694 priv->chip_port = port;
1695 ocelot_port = &priv->port;
1696 ocelot_port->ocelot = ocelot;
Vladimir Oltean91c724c2020-07-13 19:57:01 +03001697 ocelot_port->target = target;
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001698 ocelot->ports[port] = ocelot_port;
1699
1700 dev->netdev_ops = &ocelot_port_netdev_ops;
1701 dev->ethtool_ops = &ocelot_ethtool_ops;
1702
1703 dev->hw_features |= NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_RXFCS |
1704 NETIF_F_HW_TC;
1705 dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_TC;
1706
Jakub Kicinski53fdcce2021-10-18 14:10:03 -07001707 eth_hw_addr_gen(dev, ocelot->base_mac, port);
Vladimir Olteanc3e58a752020-10-31 12:29:12 +02001708 ocelot_mact_learn(ocelot, PGID_CPU, dev->dev_addr,
Vladimir Olteanbfbab312021-10-20 20:58:51 +03001709 OCELOT_VLAN_UNAWARE_PVID, ENTRYTYPE_LOCKED);
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001710
1711 ocelot_init_port(ocelot, port);
1712
Vladimir Olteane6e12df2021-08-15 04:47:48 +03001713 err = ocelot_port_phylink_create(ocelot, port, portnp);
1714 if (err)
1715 goto out;
1716
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001717 err = register_netdev(dev);
1718 if (err) {
1719 dev_err(ocelot->dev, "register_netdev failed\n");
Vladimir Olteane6e12df2021-08-15 04:47:48 +03001720 goto out;
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001721 }
1722
Dan Carpentere0c16232021-02-02 12:12:38 +03001723 return 0;
Vladimir Olteane6e12df2021-08-15 04:47:48 +03001724
1725out:
1726 ocelot->ports[port] = NULL;
1727 free_netdev(dev);
1728
1729 return err;
Dan Carpentere0c16232021-02-02 12:12:38 +03001730}
1731
1732void ocelot_release_port(struct ocelot_port *ocelot_port)
1733{
1734 struct ocelot_port_private *priv = container_of(ocelot_port,
1735 struct ocelot_port_private,
1736 port);
1737
1738 unregister_netdev(priv->dev);
Vladimir Olteane6e12df2021-08-15 04:47:48 +03001739
1740 if (priv->phylink) {
1741 rtnl_lock();
1742 phylink_disconnect_phy(priv->phylink);
1743 rtnl_unlock();
1744
1745 phylink_destroy(priv->phylink);
1746 }
1747
Dan Carpentere0c16232021-02-02 12:12:38 +03001748 free_netdev(priv->dev);
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001749}