Merge tag 'for-5.12-rc1-tag' of git://git.kernel.org/pub/scm/linux/kernel/git/kdave...
[linux-2.6-microblaze.git] / drivers / net / ethernet / mscc / ocelot_net.c
1 // SPDX-License-Identifier: (GPL-2.0 OR MIT)
2 /* Microsemi Ocelot Switch driver
3  *
4  * This contains glue logic between the switchdev driver operations and the
5  * mscc_ocelot_switch_lib.
6  *
7  * Copyright (c) 2017, 2019 Microsemi Corporation
8  * Copyright 2020-2021 NXP Semiconductors
9  */
10
11 #include <linux/if_bridge.h>
12 #include <net/pkt_cls.h>
13 #include "ocelot.h"
14 #include "ocelot_vcap.h"
15
16 static struct ocelot *devlink_port_to_ocelot(struct devlink_port *dlp)
17 {
18         return devlink_priv(dlp->devlink);
19 }
20
21 static 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
28 static 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
37 static 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
48 static 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
59 static 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
71 static int
72 ocelot_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
85 static int
86 ocelot_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
100 static 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
108 static 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
116 static 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
128 static int
129 ocelot_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
142 const struct devlink_ops ocelot_devlink_ops = {
143         .sb_pool_get                    = ocelot_devlink_sb_pool_get,
144         .sb_pool_set                    = ocelot_devlink_sb_pool_set,
145         .sb_port_pool_get               = ocelot_devlink_sb_port_pool_get,
146         .sb_port_pool_set               = ocelot_devlink_sb_port_pool_set,
147         .sb_tc_pool_bind_get            = ocelot_devlink_sb_tc_pool_bind_get,
148         .sb_tc_pool_bind_set            = ocelot_devlink_sb_tc_pool_bind_set,
149         .sb_occ_snapshot                = ocelot_devlink_sb_occ_snapshot,
150         .sb_occ_max_clear               = ocelot_devlink_sb_occ_max_clear,
151         .sb_occ_port_pool_get           = ocelot_devlink_sb_occ_port_pool_get,
152         .sb_occ_tc_port_bind_get        = ocelot_devlink_sb_occ_tc_port_bind_get,
153 };
154
155 int 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
173 void 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
180 static 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
189 int 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
211 static 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
254                 pol.rate = (u32)div_u64(action->police.rate_bytes_ps, 1000) * 8;
255                 pol.burst = action->police.burst;
256
257                 err = ocelot_port_policer_add(ocelot, port, &pol);
258                 if (err) {
259                         NL_SET_ERR_MSG_MOD(extack, "Could not add policer");
260                         return err;
261                 }
262
263                 priv->tc.police_id = f->cookie;
264                 priv->tc.offload_cnt++;
265                 return 0;
266         case TC_CLSMATCHALL_DESTROY:
267                 if (priv->tc.police_id != f->cookie)
268                         return -ENOENT;
269
270                 err = ocelot_port_policer_del(ocelot, port);
271                 if (err) {
272                         NL_SET_ERR_MSG_MOD(extack,
273                                            "Could not delete policer");
274                         return err;
275                 }
276                 priv->tc.police_id = 0;
277                 priv->tc.offload_cnt--;
278                 return 0;
279         case TC_CLSMATCHALL_STATS:
280         default:
281                 return -EOPNOTSUPP;
282         }
283 }
284
285 static int ocelot_setup_tc_block_cb(enum tc_setup_type type,
286                                     void *type_data,
287                                     void *cb_priv, bool ingress)
288 {
289         struct ocelot_port_private *priv = cb_priv;
290
291         if (!tc_cls_can_offload_and_chain0(priv->dev, type_data))
292                 return -EOPNOTSUPP;
293
294         switch (type) {
295         case TC_SETUP_CLSMATCHALL:
296                 return ocelot_setup_tc_cls_matchall(priv, type_data, ingress);
297         case TC_SETUP_CLSFLOWER:
298                 return ocelot_setup_tc_cls_flower(priv, type_data, ingress);
299         default:
300                 return -EOPNOTSUPP;
301         }
302 }
303
304 static int ocelot_setup_tc_block_cb_ig(enum tc_setup_type type,
305                                        void *type_data,
306                                        void *cb_priv)
307 {
308         return ocelot_setup_tc_block_cb(type, type_data,
309                                         cb_priv, true);
310 }
311
312 static int ocelot_setup_tc_block_cb_eg(enum tc_setup_type type,
313                                        void *type_data,
314                                        void *cb_priv)
315 {
316         return ocelot_setup_tc_block_cb(type, type_data,
317                                         cb_priv, false);
318 }
319
320 static LIST_HEAD(ocelot_block_cb_list);
321
322 static int ocelot_setup_tc_block(struct ocelot_port_private *priv,
323                                  struct flow_block_offload *f)
324 {
325         struct flow_block_cb *block_cb;
326         flow_setup_cb_t *cb;
327
328         if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS) {
329                 cb = ocelot_setup_tc_block_cb_ig;
330                 priv->tc.block_shared = f->block_shared;
331         } else if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_EGRESS) {
332                 cb = ocelot_setup_tc_block_cb_eg;
333         } else {
334                 return -EOPNOTSUPP;
335         }
336
337         f->driver_block_list = &ocelot_block_cb_list;
338
339         switch (f->command) {
340         case FLOW_BLOCK_BIND:
341                 if (flow_block_cb_is_busy(cb, priv, &ocelot_block_cb_list))
342                         return -EBUSY;
343
344                 block_cb = flow_block_cb_alloc(cb, priv, priv, NULL);
345                 if (IS_ERR(block_cb))
346                         return PTR_ERR(block_cb);
347
348                 flow_block_cb_add(block_cb, f);
349                 list_add_tail(&block_cb->driver_list, f->driver_block_list);
350                 return 0;
351         case FLOW_BLOCK_UNBIND:
352                 block_cb = flow_block_cb_lookup(f->block, cb, priv);
353                 if (!block_cb)
354                         return -ENOENT;
355
356                 flow_block_cb_remove(block_cb, f);
357                 list_del(&block_cb->driver_list);
358                 return 0;
359         default:
360                 return -EOPNOTSUPP;
361         }
362 }
363
364 static int ocelot_setup_tc(struct net_device *dev, enum tc_setup_type type,
365                            void *type_data)
366 {
367         struct ocelot_port_private *priv = netdev_priv(dev);
368
369         switch (type) {
370         case TC_SETUP_BLOCK:
371                 return ocelot_setup_tc_block(priv, type_data);
372         default:
373                 return -EOPNOTSUPP;
374         }
375         return 0;
376 }
377
378 static void ocelot_port_adjust_link(struct net_device *dev)
379 {
380         struct ocelot_port_private *priv = netdev_priv(dev);
381         struct ocelot *ocelot = priv->port.ocelot;
382         int port = priv->chip_port;
383
384         ocelot_adjust_link(ocelot, port, dev->phydev);
385 }
386
387 static int ocelot_vlan_vid_prepare(struct net_device *dev, u16 vid, bool pvid,
388                                    bool untagged)
389 {
390         struct ocelot_port_private *priv = netdev_priv(dev);
391         struct ocelot_port *ocelot_port = &priv->port;
392         struct ocelot *ocelot = ocelot_port->ocelot;
393         int port = priv->chip_port;
394
395         return ocelot_vlan_prepare(ocelot, port, vid, pvid, untagged);
396 }
397
398 static int ocelot_vlan_vid_add(struct net_device *dev, u16 vid, bool pvid,
399                                bool untagged)
400 {
401         struct ocelot_port_private *priv = netdev_priv(dev);
402         struct ocelot_port *ocelot_port = &priv->port;
403         struct ocelot *ocelot = ocelot_port->ocelot;
404         int port = priv->chip_port;
405         int ret;
406
407         ret = ocelot_vlan_add(ocelot, port, vid, pvid, untagged);
408         if (ret)
409                 return ret;
410
411         /* Add the port MAC address to with the right VLAN information */
412         ocelot_mact_learn(ocelot, PGID_CPU, dev->dev_addr, vid,
413                           ENTRYTYPE_LOCKED);
414
415         return 0;
416 }
417
418 static int ocelot_vlan_vid_del(struct net_device *dev, u16 vid)
419 {
420         struct ocelot_port_private *priv = netdev_priv(dev);
421         struct ocelot *ocelot = priv->port.ocelot;
422         int port = priv->chip_port;
423         int ret;
424
425         /* 8021q removes VID 0 on module unload for all interfaces
426          * with VLAN filtering feature. We need to keep it to receive
427          * untagged traffic.
428          */
429         if (vid == 0)
430                 return 0;
431
432         ret = ocelot_vlan_del(ocelot, port, vid);
433         if (ret)
434                 return ret;
435
436         /* Del the port MAC address to with the right VLAN information */
437         ocelot_mact_forget(ocelot, dev->dev_addr, vid);
438
439         return 0;
440 }
441
442 static int ocelot_port_open(struct net_device *dev)
443 {
444         struct ocelot_port_private *priv = netdev_priv(dev);
445         struct ocelot_port *ocelot_port = &priv->port;
446         struct ocelot *ocelot = ocelot_port->ocelot;
447         int port = priv->chip_port;
448         int err;
449
450         if (priv->serdes) {
451                 err = phy_set_mode_ext(priv->serdes, PHY_MODE_ETHERNET,
452                                        ocelot_port->phy_mode);
453                 if (err) {
454                         netdev_err(dev, "Could not set mode of SerDes\n");
455                         return err;
456                 }
457         }
458
459         err = phy_connect_direct(dev, priv->phy, &ocelot_port_adjust_link,
460                                  ocelot_port->phy_mode);
461         if (err) {
462                 netdev_err(dev, "Could not attach to PHY\n");
463                 return err;
464         }
465
466         dev->phydev = priv->phy;
467
468         phy_attached_info(priv->phy);
469         phy_start(priv->phy);
470
471         ocelot_port_enable(ocelot, port, priv->phy);
472
473         return 0;
474 }
475
476 static int ocelot_port_stop(struct net_device *dev)
477 {
478         struct ocelot_port_private *priv = netdev_priv(dev);
479         struct ocelot *ocelot = priv->port.ocelot;
480         int port = priv->chip_port;
481
482         phy_disconnect(priv->phy);
483
484         dev->phydev = NULL;
485
486         ocelot_port_disable(ocelot, port);
487
488         return 0;
489 }
490
491 static netdev_tx_t ocelot_port_xmit(struct sk_buff *skb, struct net_device *dev)
492 {
493         struct ocelot_port_private *priv = netdev_priv(dev);
494         struct ocelot_port *ocelot_port = &priv->port;
495         struct ocelot *ocelot = ocelot_port->ocelot;
496         int port = priv->chip_port;
497         u32 rew_op = 0;
498
499         if (!ocelot_can_inject(ocelot, 0))
500                 return NETDEV_TX_BUSY;
501
502         /* Check if timestamping is needed */
503         if (ocelot->ptp && (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) {
504                 rew_op = ocelot_port->ptp_cmd;
505
506                 if (ocelot_port->ptp_cmd == IFH_REW_OP_TWO_STEP_PTP) {
507                         struct sk_buff *clone;
508
509                         clone = skb_clone_sk(skb);
510                         if (!clone) {
511                                 kfree_skb(skb);
512                                 return NETDEV_TX_OK;
513                         }
514
515                         ocelot_port_add_txtstamp_skb(ocelot, port, clone);
516
517                         rew_op |= clone->cb[0] << 3;
518                 }
519         }
520
521         ocelot_port_inject_frame(ocelot, port, 0, rew_op, skb);
522
523         kfree_skb(skb);
524
525         return NETDEV_TX_OK;
526 }
527
528 enum ocelot_action_type {
529         OCELOT_MACT_LEARN,
530         OCELOT_MACT_FORGET,
531 };
532
533 struct ocelot_mact_work_ctx {
534         struct work_struct work;
535         struct ocelot *ocelot;
536         enum ocelot_action_type type;
537         union {
538                 /* OCELOT_MACT_LEARN */
539                 struct {
540                         unsigned char addr[ETH_ALEN];
541                         u16 vid;
542                         enum macaccess_entry_type entry_type;
543                         int pgid;
544                 } learn;
545                 /* OCELOT_MACT_FORGET */
546                 struct {
547                         unsigned char addr[ETH_ALEN];
548                         u16 vid;
549                 } forget;
550         };
551 };
552
553 #define ocelot_work_to_ctx(x) \
554         container_of((x), struct ocelot_mact_work_ctx, work)
555
556 static void ocelot_mact_work(struct work_struct *work)
557 {
558         struct ocelot_mact_work_ctx *w = ocelot_work_to_ctx(work);
559         struct ocelot *ocelot = w->ocelot;
560
561         switch (w->type) {
562         case OCELOT_MACT_LEARN:
563                 ocelot_mact_learn(ocelot, w->learn.pgid, w->learn.addr,
564                                   w->learn.vid, w->learn.entry_type);
565                 break;
566         case OCELOT_MACT_FORGET:
567                 ocelot_mact_forget(ocelot, w->forget.addr, w->forget.vid);
568                 break;
569         default:
570                 break;
571         }
572
573         kfree(w);
574 }
575
576 static int ocelot_enqueue_mact_action(struct ocelot *ocelot,
577                                       const struct ocelot_mact_work_ctx *ctx)
578 {
579         struct ocelot_mact_work_ctx *w = kmemdup(ctx, sizeof(*w), GFP_ATOMIC);
580
581         if (!w)
582                 return -ENOMEM;
583
584         w->ocelot = ocelot;
585         INIT_WORK(&w->work, ocelot_mact_work);
586         queue_work(ocelot->owq, &w->work);
587
588         return 0;
589 }
590
591 static int ocelot_mc_unsync(struct net_device *dev, const unsigned char *addr)
592 {
593         struct ocelot_port_private *priv = netdev_priv(dev);
594         struct ocelot_port *ocelot_port = &priv->port;
595         struct ocelot *ocelot = ocelot_port->ocelot;
596         struct ocelot_mact_work_ctx w;
597
598         ether_addr_copy(w.forget.addr, addr);
599         w.forget.vid = ocelot_port->pvid_vlan.vid;
600         w.type = OCELOT_MACT_FORGET;
601
602         return ocelot_enqueue_mact_action(ocelot, &w);
603 }
604
605 static int ocelot_mc_sync(struct net_device *dev, const unsigned char *addr)
606 {
607         struct ocelot_port_private *priv = netdev_priv(dev);
608         struct ocelot_port *ocelot_port = &priv->port;
609         struct ocelot *ocelot = ocelot_port->ocelot;
610         struct ocelot_mact_work_ctx w;
611
612         ether_addr_copy(w.learn.addr, addr);
613         w.learn.vid = ocelot_port->pvid_vlan.vid;
614         w.learn.pgid = PGID_CPU;
615         w.learn.entry_type = ENTRYTYPE_LOCKED;
616         w.type = OCELOT_MACT_LEARN;
617
618         return ocelot_enqueue_mact_action(ocelot, &w);
619 }
620
621 static void ocelot_set_rx_mode(struct net_device *dev)
622 {
623         struct ocelot_port_private *priv = netdev_priv(dev);
624         struct ocelot *ocelot = priv->port.ocelot;
625         u32 val;
626         int i;
627
628         /* This doesn't handle promiscuous mode because the bridge core is
629          * setting IFF_PROMISC on all slave interfaces and all frames would be
630          * forwarded to the CPU port.
631          */
632         val = GENMASK(ocelot->num_phys_ports - 1, 0);
633         for_each_nonreserved_multicast_dest_pgid(ocelot, i)
634                 ocelot_write_rix(ocelot, val, ANA_PGID_PGID, i);
635
636         __dev_mc_sync(dev, ocelot_mc_sync, ocelot_mc_unsync);
637 }
638
639 static int ocelot_port_set_mac_address(struct net_device *dev, void *p)
640 {
641         struct ocelot_port_private *priv = netdev_priv(dev);
642         struct ocelot_port *ocelot_port = &priv->port;
643         struct ocelot *ocelot = ocelot_port->ocelot;
644         const struct sockaddr *addr = p;
645
646         /* Learn the new net device MAC address in the mac table. */
647         ocelot_mact_learn(ocelot, PGID_CPU, addr->sa_data,
648                           ocelot_port->pvid_vlan.vid, ENTRYTYPE_LOCKED);
649         /* Then forget the previous one. */
650         ocelot_mact_forget(ocelot, dev->dev_addr, ocelot_port->pvid_vlan.vid);
651
652         ether_addr_copy(dev->dev_addr, addr->sa_data);
653         return 0;
654 }
655
656 static void ocelot_get_stats64(struct net_device *dev,
657                                struct rtnl_link_stats64 *stats)
658 {
659         struct ocelot_port_private *priv = netdev_priv(dev);
660         struct ocelot *ocelot = priv->port.ocelot;
661         int port = priv->chip_port;
662
663         /* Configure the port to read the stats from */
664         ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(port),
665                      SYS_STAT_CFG);
666
667         /* Get Rx stats */
668         stats->rx_bytes = ocelot_read(ocelot, SYS_COUNT_RX_OCTETS);
669         stats->rx_packets = ocelot_read(ocelot, SYS_COUNT_RX_SHORTS) +
670                             ocelot_read(ocelot, SYS_COUNT_RX_FRAGMENTS) +
671                             ocelot_read(ocelot, SYS_COUNT_RX_JABBERS) +
672                             ocelot_read(ocelot, SYS_COUNT_RX_LONGS) +
673                             ocelot_read(ocelot, SYS_COUNT_RX_64) +
674                             ocelot_read(ocelot, SYS_COUNT_RX_65_127) +
675                             ocelot_read(ocelot, SYS_COUNT_RX_128_255) +
676                             ocelot_read(ocelot, SYS_COUNT_RX_256_1023) +
677                             ocelot_read(ocelot, SYS_COUNT_RX_1024_1526) +
678                             ocelot_read(ocelot, SYS_COUNT_RX_1527_MAX);
679         stats->multicast = ocelot_read(ocelot, SYS_COUNT_RX_MULTICAST);
680         stats->rx_dropped = dev->stats.rx_dropped;
681
682         /* Get Tx stats */
683         stats->tx_bytes = ocelot_read(ocelot, SYS_COUNT_TX_OCTETS);
684         stats->tx_packets = ocelot_read(ocelot, SYS_COUNT_TX_64) +
685                             ocelot_read(ocelot, SYS_COUNT_TX_65_127) +
686                             ocelot_read(ocelot, SYS_COUNT_TX_128_511) +
687                             ocelot_read(ocelot, SYS_COUNT_TX_512_1023) +
688                             ocelot_read(ocelot, SYS_COUNT_TX_1024_1526) +
689                             ocelot_read(ocelot, SYS_COUNT_TX_1527_MAX);
690         stats->tx_dropped = ocelot_read(ocelot, SYS_COUNT_TX_DROPS) +
691                             ocelot_read(ocelot, SYS_COUNT_TX_AGING);
692         stats->collisions = ocelot_read(ocelot, SYS_COUNT_TX_COLLISION);
693 }
694
695 static int ocelot_port_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
696                                struct net_device *dev,
697                                const unsigned char *addr,
698                                u16 vid, u16 flags,
699                                struct netlink_ext_ack *extack)
700 {
701         struct ocelot_port_private *priv = netdev_priv(dev);
702         struct ocelot *ocelot = priv->port.ocelot;
703         int port = priv->chip_port;
704
705         return ocelot_fdb_add(ocelot, port, addr, vid);
706 }
707
708 static int ocelot_port_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
709                                struct net_device *dev,
710                                const unsigned char *addr, u16 vid)
711 {
712         struct ocelot_port_private *priv = netdev_priv(dev);
713         struct ocelot *ocelot = priv->port.ocelot;
714         int port = priv->chip_port;
715
716         return ocelot_fdb_del(ocelot, port, addr, vid);
717 }
718
719 static int ocelot_port_fdb_dump(struct sk_buff *skb,
720                                 struct netlink_callback *cb,
721                                 struct net_device *dev,
722                                 struct net_device *filter_dev, int *idx)
723 {
724         struct ocelot_port_private *priv = netdev_priv(dev);
725         struct ocelot *ocelot = priv->port.ocelot;
726         struct ocelot_dump_ctx dump = {
727                 .dev = dev,
728                 .skb = skb,
729                 .cb = cb,
730                 .idx = *idx,
731         };
732         int port = priv->chip_port;
733         int ret;
734
735         ret = ocelot_fdb_dump(ocelot, port, ocelot_port_fdb_do_dump, &dump);
736
737         *idx = dump.idx;
738
739         return ret;
740 }
741
742 static int ocelot_vlan_rx_add_vid(struct net_device *dev, __be16 proto,
743                                   u16 vid)
744 {
745         return ocelot_vlan_vid_add(dev, vid, false, false);
746 }
747
748 static int ocelot_vlan_rx_kill_vid(struct net_device *dev, __be16 proto,
749                                    u16 vid)
750 {
751         return ocelot_vlan_vid_del(dev, vid);
752 }
753
754 static void ocelot_vlan_mode(struct ocelot *ocelot, int port,
755                              netdev_features_t features)
756 {
757         u32 val;
758
759         /* Filtering */
760         val = ocelot_read(ocelot, ANA_VLANMASK);
761         if (features & NETIF_F_HW_VLAN_CTAG_FILTER)
762                 val |= BIT(port);
763         else
764                 val &= ~BIT(port);
765         ocelot_write(ocelot, val, ANA_VLANMASK);
766 }
767
768 static int ocelot_set_features(struct net_device *dev,
769                                netdev_features_t features)
770 {
771         netdev_features_t changed = dev->features ^ features;
772         struct ocelot_port_private *priv = netdev_priv(dev);
773         struct ocelot *ocelot = priv->port.ocelot;
774         int port = priv->chip_port;
775
776         if ((dev->features & NETIF_F_HW_TC) > (features & NETIF_F_HW_TC) &&
777             priv->tc.offload_cnt) {
778                 netdev_err(dev,
779                            "Cannot disable HW TC offload while offloads active\n");
780                 return -EBUSY;
781         }
782
783         if (changed & NETIF_F_HW_VLAN_CTAG_FILTER)
784                 ocelot_vlan_mode(ocelot, port, features);
785
786         return 0;
787 }
788
789 static int ocelot_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
790 {
791         struct ocelot_port_private *priv = netdev_priv(dev);
792         struct ocelot *ocelot = priv->port.ocelot;
793         int port = priv->chip_port;
794
795         /* If the attached PHY device isn't capable of timestamping operations,
796          * use our own (when possible).
797          */
798         if (!phy_has_hwtstamp(dev->phydev) && ocelot->ptp) {
799                 switch (cmd) {
800                 case SIOCSHWTSTAMP:
801                         return ocelot_hwstamp_set(ocelot, port, ifr);
802                 case SIOCGHWTSTAMP:
803                         return ocelot_hwstamp_get(ocelot, port, ifr);
804                 }
805         }
806
807         return phy_mii_ioctl(dev->phydev, ifr, cmd);
808 }
809
810 static const struct net_device_ops ocelot_port_netdev_ops = {
811         .ndo_open                       = ocelot_port_open,
812         .ndo_stop                       = ocelot_port_stop,
813         .ndo_start_xmit                 = ocelot_port_xmit,
814         .ndo_set_rx_mode                = ocelot_set_rx_mode,
815         .ndo_set_mac_address            = ocelot_port_set_mac_address,
816         .ndo_get_stats64                = ocelot_get_stats64,
817         .ndo_fdb_add                    = ocelot_port_fdb_add,
818         .ndo_fdb_del                    = ocelot_port_fdb_del,
819         .ndo_fdb_dump                   = ocelot_port_fdb_dump,
820         .ndo_vlan_rx_add_vid            = ocelot_vlan_rx_add_vid,
821         .ndo_vlan_rx_kill_vid           = ocelot_vlan_rx_kill_vid,
822         .ndo_set_features               = ocelot_set_features,
823         .ndo_setup_tc                   = ocelot_setup_tc,
824         .ndo_do_ioctl                   = ocelot_ioctl,
825         .ndo_get_devlink_port           = ocelot_get_devlink_port,
826 };
827
828 struct net_device *ocelot_port_to_netdev(struct ocelot *ocelot, int port)
829 {
830         struct ocelot_port *ocelot_port = ocelot->ports[port];
831         struct ocelot_port_private *priv;
832
833         if (!ocelot_port)
834                 return NULL;
835
836         priv = container_of(ocelot_port, struct ocelot_port_private, port);
837
838         return priv->dev;
839 }
840
841 /* Checks if the net_device instance given to us originates from our driver */
842 static bool ocelot_netdevice_dev_check(const struct net_device *dev)
843 {
844         return dev->netdev_ops == &ocelot_port_netdev_ops;
845 }
846
847 int ocelot_netdev_to_port(struct net_device *dev)
848 {
849         struct ocelot_port_private *priv;
850
851         if (!dev || !ocelot_netdevice_dev_check(dev))
852                 return -EINVAL;
853
854         priv = netdev_priv(dev);
855
856         return priv->chip_port;
857 }
858
859 static void ocelot_port_get_strings(struct net_device *netdev, u32 sset,
860                                     u8 *data)
861 {
862         struct ocelot_port_private *priv = netdev_priv(netdev);
863         struct ocelot *ocelot = priv->port.ocelot;
864         int port = priv->chip_port;
865
866         ocelot_get_strings(ocelot, port, sset, data);
867 }
868
869 static void ocelot_port_get_ethtool_stats(struct net_device *dev,
870                                           struct ethtool_stats *stats,
871                                           u64 *data)
872 {
873         struct ocelot_port_private *priv = netdev_priv(dev);
874         struct ocelot *ocelot = priv->port.ocelot;
875         int port = priv->chip_port;
876
877         ocelot_get_ethtool_stats(ocelot, port, data);
878 }
879
880 static int ocelot_port_get_sset_count(struct net_device *dev, int sset)
881 {
882         struct ocelot_port_private *priv = netdev_priv(dev);
883         struct ocelot *ocelot = priv->port.ocelot;
884         int port = priv->chip_port;
885
886         return ocelot_get_sset_count(ocelot, port, sset);
887 }
888
889 static int ocelot_port_get_ts_info(struct net_device *dev,
890                                    struct ethtool_ts_info *info)
891 {
892         struct ocelot_port_private *priv = netdev_priv(dev);
893         struct ocelot *ocelot = priv->port.ocelot;
894         int port = priv->chip_port;
895
896         if (!ocelot->ptp)
897                 return ethtool_op_get_ts_info(dev, info);
898
899         return ocelot_get_ts_info(ocelot, port, info);
900 }
901
902 static const struct ethtool_ops ocelot_ethtool_ops = {
903         .get_strings            = ocelot_port_get_strings,
904         .get_ethtool_stats      = ocelot_port_get_ethtool_stats,
905         .get_sset_count         = ocelot_port_get_sset_count,
906         .get_link_ksettings     = phy_ethtool_get_link_ksettings,
907         .set_link_ksettings     = phy_ethtool_set_link_ksettings,
908         .get_ts_info            = ocelot_port_get_ts_info,
909 };
910
911 static void ocelot_port_attr_stp_state_set(struct ocelot *ocelot, int port,
912                                            u8 state)
913 {
914         ocelot_bridge_stp_state_set(ocelot, port, state);
915 }
916
917 static void ocelot_port_attr_ageing_set(struct ocelot *ocelot, int port,
918                                         unsigned long ageing_clock_t)
919 {
920         unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock_t);
921         u32 ageing_time = jiffies_to_msecs(ageing_jiffies);
922
923         ocelot_set_ageing_time(ocelot, ageing_time);
924 }
925
926 static void ocelot_port_attr_mc_set(struct ocelot *ocelot, int port, bool mc)
927 {
928         u32 cpu_fwd_mcast = ANA_PORT_CPU_FWD_CFG_CPU_IGMP_REDIR_ENA |
929                             ANA_PORT_CPU_FWD_CFG_CPU_MLD_REDIR_ENA |
930                             ANA_PORT_CPU_FWD_CFG_CPU_IPMC_CTRL_COPY_ENA;
931         u32 val = 0;
932
933         if (mc)
934                 val = cpu_fwd_mcast;
935
936         ocelot_rmw_gix(ocelot, val, cpu_fwd_mcast,
937                        ANA_PORT_CPU_FWD_CFG, port);
938 }
939
940 static int ocelot_port_attr_set(struct net_device *dev,
941                                 const struct switchdev_attr *attr,
942                                 struct netlink_ext_ack *extack)
943 {
944         struct ocelot_port_private *priv = netdev_priv(dev);
945         struct ocelot *ocelot = priv->port.ocelot;
946         int port = priv->chip_port;
947         int err = 0;
948
949         switch (attr->id) {
950         case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
951                 ocelot_port_attr_stp_state_set(ocelot, port, attr->u.stp_state);
952                 break;
953         case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
954                 ocelot_port_attr_ageing_set(ocelot, port, attr->u.ageing_time);
955                 break;
956         case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
957                 ocelot_port_vlan_filtering(ocelot, port, attr->u.vlan_filtering);
958                 break;
959         case SWITCHDEV_ATTR_ID_BRIDGE_MC_DISABLED:
960                 ocelot_port_attr_mc_set(ocelot, port, !attr->u.mc_disabled);
961                 break;
962         case SWITCHDEV_ATTR_ID_PORT_PRE_BRIDGE_FLAGS:
963                 err = ocelot_port_pre_bridge_flags(ocelot, port,
964                                                    attr->u.brport_flags);
965                 break;
966         case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS:
967                 ocelot_port_bridge_flags(ocelot, port, attr->u.brport_flags);
968                 break;
969         default:
970                 err = -EOPNOTSUPP;
971                 break;
972         }
973
974         return err;
975 }
976
977 static int ocelot_port_obj_add_vlan(struct net_device *dev,
978                                     const struct switchdev_obj_port_vlan *vlan)
979 {
980         bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
981         bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
982         int ret;
983
984         ret = ocelot_vlan_vid_prepare(dev, vlan->vid, pvid, untagged);
985         if (ret)
986                 return ret;
987
988         return ocelot_vlan_vid_add(dev, vlan->vid, pvid, untagged);
989 }
990
991 static int ocelot_port_obj_add_mdb(struct net_device *dev,
992                                    const struct switchdev_obj_port_mdb *mdb)
993 {
994         struct ocelot_port_private *priv = netdev_priv(dev);
995         struct ocelot_port *ocelot_port = &priv->port;
996         struct ocelot *ocelot = ocelot_port->ocelot;
997         int port = priv->chip_port;
998
999         return ocelot_port_mdb_add(ocelot, port, mdb);
1000 }
1001
1002 static int ocelot_port_obj_del_mdb(struct net_device *dev,
1003                                    const struct switchdev_obj_port_mdb *mdb)
1004 {
1005         struct ocelot_port_private *priv = netdev_priv(dev);
1006         struct ocelot_port *ocelot_port = &priv->port;
1007         struct ocelot *ocelot = ocelot_port->ocelot;
1008         int port = priv->chip_port;
1009
1010         return ocelot_port_mdb_del(ocelot, port, mdb);
1011 }
1012
1013 static int ocelot_port_obj_mrp_add(struct net_device *dev,
1014                                    const struct switchdev_obj_mrp *mrp)
1015 {
1016         struct ocelot_port_private *priv = netdev_priv(dev);
1017         struct ocelot_port *ocelot_port = &priv->port;
1018         struct ocelot *ocelot = ocelot_port->ocelot;
1019         int port = priv->chip_port;
1020
1021         return ocelot_mrp_add(ocelot, port, mrp);
1022 }
1023
1024 static int ocelot_port_obj_mrp_del(struct net_device *dev,
1025                                    const struct switchdev_obj_mrp *mrp)
1026 {
1027         struct ocelot_port_private *priv = netdev_priv(dev);
1028         struct ocelot_port *ocelot_port = &priv->port;
1029         struct ocelot *ocelot = ocelot_port->ocelot;
1030         int port = priv->chip_port;
1031
1032         return ocelot_mrp_del(ocelot, port, mrp);
1033 }
1034
1035 static int
1036 ocelot_port_obj_mrp_add_ring_role(struct net_device *dev,
1037                                   const struct switchdev_obj_ring_role_mrp *mrp)
1038 {
1039         struct ocelot_port_private *priv = netdev_priv(dev);
1040         struct ocelot_port *ocelot_port = &priv->port;
1041         struct ocelot *ocelot = ocelot_port->ocelot;
1042         int port = priv->chip_port;
1043
1044         return ocelot_mrp_add_ring_role(ocelot, port, mrp);
1045 }
1046
1047 static int
1048 ocelot_port_obj_mrp_del_ring_role(struct net_device *dev,
1049                                   const struct switchdev_obj_ring_role_mrp *mrp)
1050 {
1051         struct ocelot_port_private *priv = netdev_priv(dev);
1052         struct ocelot_port *ocelot_port = &priv->port;
1053         struct ocelot *ocelot = ocelot_port->ocelot;
1054         int port = priv->chip_port;
1055
1056         return ocelot_mrp_del_ring_role(ocelot, port, mrp);
1057 }
1058
1059 static int ocelot_port_obj_add(struct net_device *dev,
1060                                const struct switchdev_obj *obj,
1061                                struct netlink_ext_ack *extack)
1062 {
1063         int ret = 0;
1064
1065         switch (obj->id) {
1066         case SWITCHDEV_OBJ_ID_PORT_VLAN:
1067                 ret = ocelot_port_obj_add_vlan(dev,
1068                                                SWITCHDEV_OBJ_PORT_VLAN(obj));
1069                 break;
1070         case SWITCHDEV_OBJ_ID_PORT_MDB:
1071                 ret = ocelot_port_obj_add_mdb(dev, SWITCHDEV_OBJ_PORT_MDB(obj));
1072                 break;
1073         case SWITCHDEV_OBJ_ID_MRP:
1074                 ret = ocelot_port_obj_mrp_add(dev, SWITCHDEV_OBJ_MRP(obj));
1075                 break;
1076         case SWITCHDEV_OBJ_ID_RING_ROLE_MRP:
1077                 ret = ocelot_port_obj_mrp_add_ring_role(dev,
1078                                                         SWITCHDEV_OBJ_RING_ROLE_MRP(obj));
1079                 break;
1080         default:
1081                 return -EOPNOTSUPP;
1082         }
1083
1084         return ret;
1085 }
1086
1087 static int ocelot_port_obj_del(struct net_device *dev,
1088                                const struct switchdev_obj *obj)
1089 {
1090         int ret = 0;
1091
1092         switch (obj->id) {
1093         case SWITCHDEV_OBJ_ID_PORT_VLAN:
1094                 ret = ocelot_vlan_vid_del(dev,
1095                                           SWITCHDEV_OBJ_PORT_VLAN(obj)->vid);
1096                 break;
1097         case SWITCHDEV_OBJ_ID_PORT_MDB:
1098                 ret = ocelot_port_obj_del_mdb(dev, SWITCHDEV_OBJ_PORT_MDB(obj));
1099                 break;
1100         case SWITCHDEV_OBJ_ID_MRP:
1101                 ret = ocelot_port_obj_mrp_del(dev, SWITCHDEV_OBJ_MRP(obj));
1102                 break;
1103         case SWITCHDEV_OBJ_ID_RING_ROLE_MRP:
1104                 ret = ocelot_port_obj_mrp_del_ring_role(dev,
1105                                                         SWITCHDEV_OBJ_RING_ROLE_MRP(obj));
1106                 break;
1107         default:
1108                 return -EOPNOTSUPP;
1109         }
1110
1111         return ret;
1112 }
1113
1114 static int ocelot_netdevice_bridge_join(struct ocelot *ocelot, int port,
1115                                         struct net_device *bridge)
1116 {
1117         struct switchdev_brport_flags flags;
1118         int err;
1119
1120         flags.mask = BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD | BR_BCAST_FLOOD;
1121         flags.val = flags.mask;
1122
1123         err = ocelot_port_bridge_join(ocelot, port, bridge);
1124         if (err)
1125                 return err;
1126
1127         ocelot_port_bridge_flags(ocelot, port, flags);
1128
1129         return 0;
1130 }
1131
1132 static int ocelot_netdevice_bridge_leave(struct ocelot *ocelot, int port,
1133                                          struct net_device *bridge)
1134 {
1135         struct switchdev_brport_flags flags;
1136         int err;
1137
1138         flags.mask = BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD | BR_BCAST_FLOOD;
1139         flags.val = flags.mask & ~BR_LEARNING;
1140
1141         err = ocelot_port_bridge_leave(ocelot, port, bridge);
1142
1143         ocelot_port_bridge_flags(ocelot, port, flags);
1144
1145         return err;
1146 }
1147
1148 static int ocelot_netdevice_changeupper(struct net_device *dev,
1149                                         struct netdev_notifier_changeupper_info *info)
1150 {
1151         struct ocelot_port_private *priv = netdev_priv(dev);
1152         struct ocelot_port *ocelot_port = &priv->port;
1153         struct ocelot *ocelot = ocelot_port->ocelot;
1154         int port = priv->chip_port;
1155         int err = 0;
1156
1157         if (netif_is_bridge_master(info->upper_dev)) {
1158                 if (info->linking) {
1159                         err = ocelot_netdevice_bridge_join(ocelot, port,
1160                                                            info->upper_dev);
1161                 } else {
1162                         err = ocelot_netdevice_bridge_leave(ocelot, port,
1163                                                             info->upper_dev);
1164                 }
1165         }
1166         if (netif_is_lag_master(info->upper_dev)) {
1167                 if (info->linking) {
1168                         err = ocelot_port_lag_join(ocelot, port,
1169                                                    info->upper_dev,
1170                                                    info->upper_info);
1171                         if (err == -EOPNOTSUPP) {
1172                                 NL_SET_ERR_MSG_MOD(info->info.extack,
1173                                                    "Offloading not supported");
1174                                 err = 0;
1175                         }
1176                 } else {
1177                         ocelot_port_lag_leave(ocelot, port,
1178                                               info->upper_dev);
1179                 }
1180         }
1181
1182         return notifier_from_errno(err);
1183 }
1184
1185 static int
1186 ocelot_netdevice_lag_changeupper(struct net_device *dev,
1187                                  struct netdev_notifier_changeupper_info *info)
1188 {
1189         struct net_device *lower;
1190         struct list_head *iter;
1191         int err = NOTIFY_DONE;
1192
1193         netdev_for_each_lower_dev(dev, lower, iter) {
1194                 err = ocelot_netdevice_changeupper(lower, info);
1195                 if (err)
1196                         return notifier_from_errno(err);
1197         }
1198
1199         return NOTIFY_DONE;
1200 }
1201
1202 static int
1203 ocelot_netdevice_changelowerstate(struct net_device *dev,
1204                                   struct netdev_lag_lower_state_info *info)
1205 {
1206         struct ocelot_port_private *priv = netdev_priv(dev);
1207         bool is_active = info->link_up && info->tx_enabled;
1208         struct ocelot_port *ocelot_port = &priv->port;
1209         struct ocelot *ocelot = ocelot_port->ocelot;
1210         int port = priv->chip_port;
1211
1212         if (!ocelot_port->bond)
1213                 return NOTIFY_DONE;
1214
1215         if (ocelot_port->lag_tx_active == is_active)
1216                 return NOTIFY_DONE;
1217
1218         ocelot_port_lag_change(ocelot, port, is_active);
1219
1220         return NOTIFY_OK;
1221 }
1222
1223 static int ocelot_netdevice_event(struct notifier_block *unused,
1224                                   unsigned long event, void *ptr)
1225 {
1226         struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1227
1228         switch (event) {
1229         case NETDEV_CHANGEUPPER: {
1230                 struct netdev_notifier_changeupper_info *info = ptr;
1231
1232                 if (ocelot_netdevice_dev_check(dev))
1233                         return ocelot_netdevice_changeupper(dev, info);
1234
1235                 if (netif_is_lag_master(dev))
1236                         return ocelot_netdevice_lag_changeupper(dev, info);
1237
1238                 break;
1239         }
1240         case NETDEV_CHANGELOWERSTATE: {
1241                 struct netdev_notifier_changelowerstate_info *info = ptr;
1242
1243                 if (!ocelot_netdevice_dev_check(dev))
1244                         break;
1245
1246                 return ocelot_netdevice_changelowerstate(dev,
1247                                                          info->lower_state_info);
1248         }
1249         default:
1250                 break;
1251         }
1252
1253         return NOTIFY_DONE;
1254 }
1255
1256 struct notifier_block ocelot_netdevice_nb __read_mostly = {
1257         .notifier_call = ocelot_netdevice_event,
1258 };
1259
1260 static int ocelot_switchdev_event(struct notifier_block *unused,
1261                                   unsigned long event, void *ptr)
1262 {
1263         struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
1264         int err;
1265
1266         switch (event) {
1267         case SWITCHDEV_PORT_ATTR_SET:
1268                 err = switchdev_handle_port_attr_set(dev, ptr,
1269                                                      ocelot_netdevice_dev_check,
1270                                                      ocelot_port_attr_set);
1271                 return notifier_from_errno(err);
1272         }
1273
1274         return NOTIFY_DONE;
1275 }
1276
1277 struct notifier_block ocelot_switchdev_nb __read_mostly = {
1278         .notifier_call = ocelot_switchdev_event,
1279 };
1280
1281 static int ocelot_switchdev_blocking_event(struct notifier_block *unused,
1282                                            unsigned long event, void *ptr)
1283 {
1284         struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
1285         int err;
1286
1287         switch (event) {
1288                 /* Blocking events. */
1289         case SWITCHDEV_PORT_OBJ_ADD:
1290                 err = switchdev_handle_port_obj_add(dev, ptr,
1291                                                     ocelot_netdevice_dev_check,
1292                                                     ocelot_port_obj_add);
1293                 return notifier_from_errno(err);
1294         case SWITCHDEV_PORT_OBJ_DEL:
1295                 err = switchdev_handle_port_obj_del(dev, ptr,
1296                                                     ocelot_netdevice_dev_check,
1297                                                     ocelot_port_obj_del);
1298                 return notifier_from_errno(err);
1299         case SWITCHDEV_PORT_ATTR_SET:
1300                 err = switchdev_handle_port_attr_set(dev, ptr,
1301                                                      ocelot_netdevice_dev_check,
1302                                                      ocelot_port_attr_set);
1303                 return notifier_from_errno(err);
1304         }
1305
1306         return NOTIFY_DONE;
1307 }
1308
1309 struct notifier_block ocelot_switchdev_blocking_nb __read_mostly = {
1310         .notifier_call = ocelot_switchdev_blocking_event,
1311 };
1312
1313 int ocelot_probe_port(struct ocelot *ocelot, int port, struct regmap *target,
1314                       struct phy_device *phy)
1315 {
1316         struct ocelot_port_private *priv;
1317         struct ocelot_port *ocelot_port;
1318         struct net_device *dev;
1319         int err;
1320
1321         dev = alloc_etherdev(sizeof(struct ocelot_port_private));
1322         if (!dev)
1323                 return -ENOMEM;
1324         SET_NETDEV_DEV(dev, ocelot->dev);
1325         priv = netdev_priv(dev);
1326         priv->dev = dev;
1327         priv->phy = phy;
1328         priv->chip_port = port;
1329         ocelot_port = &priv->port;
1330         ocelot_port->ocelot = ocelot;
1331         ocelot_port->target = target;
1332         ocelot->ports[port] = ocelot_port;
1333
1334         dev->netdev_ops = &ocelot_port_netdev_ops;
1335         dev->ethtool_ops = &ocelot_ethtool_ops;
1336
1337         dev->hw_features |= NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_RXFCS |
1338                 NETIF_F_HW_TC;
1339         dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_TC;
1340
1341         memcpy(dev->dev_addr, ocelot->base_mac, ETH_ALEN);
1342         dev->dev_addr[ETH_ALEN - 1] += port;
1343         ocelot_mact_learn(ocelot, PGID_CPU, dev->dev_addr,
1344                           ocelot_port->pvid_vlan.vid, ENTRYTYPE_LOCKED);
1345
1346         ocelot_init_port(ocelot, port);
1347
1348         err = register_netdev(dev);
1349         if (err) {
1350                 dev_err(ocelot->dev, "register_netdev failed\n");
1351                 free_netdev(dev);
1352                 ocelot->ports[port] = NULL;
1353                 return err;
1354         }
1355
1356         return 0;
1357 }
1358
1359 void ocelot_release_port(struct ocelot_port *ocelot_port)
1360 {
1361         struct ocelot_port_private *priv = container_of(ocelot_port,
1362                                                 struct ocelot_port_private,
1363                                                 port);
1364
1365         unregister_netdev(priv->dev);
1366         free_netdev(priv->dev);
1367 }