Merge tag 'libnvdimm-for-5.8' of git://git.kernel.org/pub/scm/linux/kernel/git/nvdimm...
[linux-2.6-microblaze.git] / drivers / staging / fsl-dpaa2 / ethsw / ethsw.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * DPAA2 Ethernet Switch driver
4  *
5  * Copyright 2014-2016 Freescale Semiconductor Inc.
6  * Copyright 2017-2018 NXP
7  *
8  */
9
10 #include <linux/module.h>
11
12 #include <linux/interrupt.h>
13 #include <linux/msi.h>
14 #include <linux/kthread.h>
15 #include <linux/workqueue.h>
16
17 #include <linux/fsl/mc.h>
18
19 #include "ethsw.h"
20
21 /* Minimal supported DPSW version */
22 #define DPSW_MIN_VER_MAJOR              8
23 #define DPSW_MIN_VER_MINOR              1
24
25 #define DEFAULT_VLAN_ID                 1
26
27 static int ethsw_add_vlan(struct ethsw_core *ethsw, u16 vid)
28 {
29         int err;
30
31         struct dpsw_vlan_cfg    vcfg = {
32                 .fdb_id = 0,
33         };
34
35         err = dpsw_vlan_add(ethsw->mc_io, 0,
36                             ethsw->dpsw_handle, vid, &vcfg);
37         if (err) {
38                 dev_err(ethsw->dev, "dpsw_vlan_add err %d\n", err);
39                 return err;
40         }
41         ethsw->vlans[vid] = ETHSW_VLAN_MEMBER;
42
43         return 0;
44 }
45
46 static int ethsw_port_set_pvid(struct ethsw_port_priv *port_priv, u16 pvid)
47 {
48         struct ethsw_core *ethsw = port_priv->ethsw_data;
49         struct net_device *netdev = port_priv->netdev;
50         struct dpsw_tci_cfg tci_cfg = { 0 };
51         bool is_oper;
52         int err, ret;
53
54         err = dpsw_if_get_tci(ethsw->mc_io, 0, ethsw->dpsw_handle,
55                               port_priv->idx, &tci_cfg);
56         if (err) {
57                 netdev_err(netdev, "dpsw_if_get_tci err %d\n", err);
58                 return err;
59         }
60
61         tci_cfg.vlan_id = pvid;
62
63         /* Interface needs to be down to change PVID */
64         is_oper = netif_oper_up(netdev);
65         if (is_oper) {
66                 err = dpsw_if_disable(ethsw->mc_io, 0,
67                                       ethsw->dpsw_handle,
68                                       port_priv->idx);
69                 if (err) {
70                         netdev_err(netdev, "dpsw_if_disable err %d\n", err);
71                         return err;
72                 }
73         }
74
75         err = dpsw_if_set_tci(ethsw->mc_io, 0, ethsw->dpsw_handle,
76                               port_priv->idx, &tci_cfg);
77         if (err) {
78                 netdev_err(netdev, "dpsw_if_set_tci err %d\n", err);
79                 goto set_tci_error;
80         }
81
82         /* Delete previous PVID info and mark the new one */
83         port_priv->vlans[port_priv->pvid] &= ~ETHSW_VLAN_PVID;
84         port_priv->vlans[pvid] |= ETHSW_VLAN_PVID;
85         port_priv->pvid = pvid;
86
87 set_tci_error:
88         if (is_oper) {
89                 ret = dpsw_if_enable(ethsw->mc_io, 0,
90                                      ethsw->dpsw_handle,
91                                      port_priv->idx);
92                 if (ret) {
93                         netdev_err(netdev, "dpsw_if_enable err %d\n", ret);
94                         return ret;
95                 }
96         }
97
98         return err;
99 }
100
101 static int ethsw_port_add_vlan(struct ethsw_port_priv *port_priv,
102                                u16 vid, u16 flags)
103 {
104         struct ethsw_core *ethsw = port_priv->ethsw_data;
105         struct net_device *netdev = port_priv->netdev;
106         struct dpsw_vlan_if_cfg vcfg;
107         int err;
108
109         if (port_priv->vlans[vid]) {
110                 netdev_warn(netdev, "VLAN %d already configured\n", vid);
111                 return -EEXIST;
112         }
113
114         vcfg.num_ifs = 1;
115         vcfg.if_id[0] = port_priv->idx;
116         err = dpsw_vlan_add_if(ethsw->mc_io, 0, ethsw->dpsw_handle, vid, &vcfg);
117         if (err) {
118                 netdev_err(netdev, "dpsw_vlan_add_if err %d\n", err);
119                 return err;
120         }
121
122         port_priv->vlans[vid] = ETHSW_VLAN_MEMBER;
123
124         if (flags & BRIDGE_VLAN_INFO_UNTAGGED) {
125                 err = dpsw_vlan_add_if_untagged(ethsw->mc_io, 0,
126                                                 ethsw->dpsw_handle,
127                                                 vid, &vcfg);
128                 if (err) {
129                         netdev_err(netdev,
130                                    "dpsw_vlan_add_if_untagged err %d\n", err);
131                         return err;
132                 }
133                 port_priv->vlans[vid] |= ETHSW_VLAN_UNTAGGED;
134         }
135
136         if (flags & BRIDGE_VLAN_INFO_PVID) {
137                 err = ethsw_port_set_pvid(port_priv, vid);
138                 if (err)
139                         return err;
140         }
141
142         return 0;
143 }
144
145 static int ethsw_set_learning(struct ethsw_core *ethsw, bool enable)
146 {
147         enum dpsw_fdb_learning_mode learn_mode;
148         int err;
149
150         if (enable)
151                 learn_mode = DPSW_FDB_LEARNING_MODE_HW;
152         else
153                 learn_mode = DPSW_FDB_LEARNING_MODE_DIS;
154
155         err = dpsw_fdb_set_learning_mode(ethsw->mc_io, 0, ethsw->dpsw_handle, 0,
156                                          learn_mode);
157         if (err) {
158                 dev_err(ethsw->dev, "dpsw_fdb_set_learning_mode err %d\n", err);
159                 return err;
160         }
161         ethsw->learning = enable;
162
163         return 0;
164 }
165
166 static int ethsw_port_set_flood(struct ethsw_port_priv *port_priv, bool enable)
167 {
168         int err;
169
170         err = dpsw_if_set_flooding(port_priv->ethsw_data->mc_io, 0,
171                                    port_priv->ethsw_data->dpsw_handle,
172                                    port_priv->idx, enable);
173         if (err) {
174                 netdev_err(port_priv->netdev,
175                            "dpsw_if_set_flooding err %d\n", err);
176                 return err;
177         }
178         port_priv->flood = enable;
179
180         return 0;
181 }
182
183 static int ethsw_port_set_stp_state(struct ethsw_port_priv *port_priv, u8 state)
184 {
185         struct dpsw_stp_cfg stp_cfg = {
186                 .vlan_id = DEFAULT_VLAN_ID,
187                 .state = state,
188         };
189         int err;
190
191         if (!netif_oper_up(port_priv->netdev) || state == port_priv->stp_state)
192                 return 0;       /* Nothing to do */
193
194         err = dpsw_if_set_stp(port_priv->ethsw_data->mc_io, 0,
195                               port_priv->ethsw_data->dpsw_handle,
196                               port_priv->idx, &stp_cfg);
197         if (err) {
198                 netdev_err(port_priv->netdev,
199                            "dpsw_if_set_stp err %d\n", err);
200                 return err;
201         }
202
203         port_priv->stp_state = state;
204
205         return 0;
206 }
207
208 static int ethsw_dellink_switch(struct ethsw_core *ethsw, u16 vid)
209 {
210         struct ethsw_port_priv *ppriv_local = NULL;
211         int i, err;
212
213         if (!ethsw->vlans[vid])
214                 return -ENOENT;
215
216         err = dpsw_vlan_remove(ethsw->mc_io, 0, ethsw->dpsw_handle, vid);
217         if (err) {
218                 dev_err(ethsw->dev, "dpsw_vlan_remove err %d\n", err);
219                 return err;
220         }
221         ethsw->vlans[vid] = 0;
222
223         for (i = 0; i < ethsw->sw_attr.num_ifs; i++) {
224                 ppriv_local = ethsw->ports[i];
225                 ppriv_local->vlans[vid] = 0;
226         }
227
228         return 0;
229 }
230
231 static int ethsw_port_fdb_add_uc(struct ethsw_port_priv *port_priv,
232                                  const unsigned char *addr)
233 {
234         struct dpsw_fdb_unicast_cfg entry = {0};
235         int err;
236
237         entry.if_egress = port_priv->idx;
238         entry.type = DPSW_FDB_ENTRY_STATIC;
239         ether_addr_copy(entry.mac_addr, addr);
240
241         err = dpsw_fdb_add_unicast(port_priv->ethsw_data->mc_io, 0,
242                                    port_priv->ethsw_data->dpsw_handle,
243                                    0, &entry);
244         if (err)
245                 netdev_err(port_priv->netdev,
246                            "dpsw_fdb_add_unicast err %d\n", err);
247         return err;
248 }
249
250 static int ethsw_port_fdb_del_uc(struct ethsw_port_priv *port_priv,
251                                  const unsigned char *addr)
252 {
253         struct dpsw_fdb_unicast_cfg entry = {0};
254         int err;
255
256         entry.if_egress = port_priv->idx;
257         entry.type = DPSW_FDB_ENTRY_STATIC;
258         ether_addr_copy(entry.mac_addr, addr);
259
260         err = dpsw_fdb_remove_unicast(port_priv->ethsw_data->mc_io, 0,
261                                       port_priv->ethsw_data->dpsw_handle,
262                                       0, &entry);
263         /* Silently discard error for calling multiple times the del command */
264         if (err && err != -ENXIO)
265                 netdev_err(port_priv->netdev,
266                            "dpsw_fdb_remove_unicast err %d\n", err);
267         return err;
268 }
269
270 static int ethsw_port_fdb_add_mc(struct ethsw_port_priv *port_priv,
271                                  const unsigned char *addr)
272 {
273         struct dpsw_fdb_multicast_cfg entry = {0};
274         int err;
275
276         ether_addr_copy(entry.mac_addr, addr);
277         entry.type = DPSW_FDB_ENTRY_STATIC;
278         entry.num_ifs = 1;
279         entry.if_id[0] = port_priv->idx;
280
281         err = dpsw_fdb_add_multicast(port_priv->ethsw_data->mc_io, 0,
282                                      port_priv->ethsw_data->dpsw_handle,
283                                      0, &entry);
284         /* Silently discard error for calling multiple times the add command */
285         if (err && err != -ENXIO)
286                 netdev_err(port_priv->netdev, "dpsw_fdb_add_multicast err %d\n",
287                            err);
288         return err;
289 }
290
291 static int ethsw_port_fdb_del_mc(struct ethsw_port_priv *port_priv,
292                                  const unsigned char *addr)
293 {
294         struct dpsw_fdb_multicast_cfg entry = {0};
295         int err;
296
297         ether_addr_copy(entry.mac_addr, addr);
298         entry.type = DPSW_FDB_ENTRY_STATIC;
299         entry.num_ifs = 1;
300         entry.if_id[0] = port_priv->idx;
301
302         err = dpsw_fdb_remove_multicast(port_priv->ethsw_data->mc_io, 0,
303                                         port_priv->ethsw_data->dpsw_handle,
304                                         0, &entry);
305         /* Silently discard error for calling multiple times the del command */
306         if (err && err != -ENAVAIL)
307                 netdev_err(port_priv->netdev,
308                            "dpsw_fdb_remove_multicast err %d\n", err);
309         return err;
310 }
311
312 static int port_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
313                         struct net_device *dev, const unsigned char *addr,
314                         u16 vid, u16 flags,
315                         struct netlink_ext_ack *extack)
316 {
317         if (is_unicast_ether_addr(addr))
318                 return ethsw_port_fdb_add_uc(netdev_priv(dev),
319                                              addr);
320         else
321                 return ethsw_port_fdb_add_mc(netdev_priv(dev),
322                                              addr);
323 }
324
325 static int port_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
326                         struct net_device *dev,
327                         const unsigned char *addr, u16 vid)
328 {
329         if (is_unicast_ether_addr(addr))
330                 return ethsw_port_fdb_del_uc(netdev_priv(dev),
331                                              addr);
332         else
333                 return ethsw_port_fdb_del_mc(netdev_priv(dev),
334                                              addr);
335 }
336
337 static void port_get_stats(struct net_device *netdev,
338                            struct rtnl_link_stats64 *stats)
339 {
340         struct ethsw_port_priv *port_priv = netdev_priv(netdev);
341         u64 tmp;
342         int err;
343
344         err = dpsw_if_get_counter(port_priv->ethsw_data->mc_io, 0,
345                                   port_priv->ethsw_data->dpsw_handle,
346                                   port_priv->idx,
347                                   DPSW_CNT_ING_FRAME, &stats->rx_packets);
348         if (err)
349                 goto error;
350
351         err = dpsw_if_get_counter(port_priv->ethsw_data->mc_io, 0,
352                                   port_priv->ethsw_data->dpsw_handle,
353                                   port_priv->idx,
354                                   DPSW_CNT_EGR_FRAME, &stats->tx_packets);
355         if (err)
356                 goto error;
357
358         err = dpsw_if_get_counter(port_priv->ethsw_data->mc_io, 0,
359                                   port_priv->ethsw_data->dpsw_handle,
360                                   port_priv->idx,
361                                   DPSW_CNT_ING_BYTE, &stats->rx_bytes);
362         if (err)
363                 goto error;
364
365         err = dpsw_if_get_counter(port_priv->ethsw_data->mc_io, 0,
366                                   port_priv->ethsw_data->dpsw_handle,
367                                   port_priv->idx,
368                                   DPSW_CNT_EGR_BYTE, &stats->tx_bytes);
369         if (err)
370                 goto error;
371
372         err = dpsw_if_get_counter(port_priv->ethsw_data->mc_io, 0,
373                                   port_priv->ethsw_data->dpsw_handle,
374                                   port_priv->idx,
375                                   DPSW_CNT_ING_FRAME_DISCARD,
376                                   &stats->rx_dropped);
377         if (err)
378                 goto error;
379
380         err = dpsw_if_get_counter(port_priv->ethsw_data->mc_io, 0,
381                                   port_priv->ethsw_data->dpsw_handle,
382                                   port_priv->idx,
383                                   DPSW_CNT_ING_FLTR_FRAME,
384                                   &tmp);
385         if (err)
386                 goto error;
387         stats->rx_dropped += tmp;
388
389         err = dpsw_if_get_counter(port_priv->ethsw_data->mc_io, 0,
390                                   port_priv->ethsw_data->dpsw_handle,
391                                   port_priv->idx,
392                                   DPSW_CNT_EGR_FRAME_DISCARD,
393                                   &stats->tx_dropped);
394         if (err)
395                 goto error;
396
397         return;
398
399 error:
400         netdev_err(netdev, "dpsw_if_get_counter err %d\n", err);
401 }
402
403 static bool port_has_offload_stats(const struct net_device *netdev,
404                                    int attr_id)
405 {
406         return (attr_id == IFLA_OFFLOAD_XSTATS_CPU_HIT);
407 }
408
409 static int port_get_offload_stats(int attr_id,
410                                   const struct net_device *netdev,
411                                   void *sp)
412 {
413         switch (attr_id) {
414         case IFLA_OFFLOAD_XSTATS_CPU_HIT:
415                 port_get_stats((struct net_device *)netdev, sp);
416                 return 0;
417         }
418
419         return -EINVAL;
420 }
421
422 static int port_change_mtu(struct net_device *netdev, int mtu)
423 {
424         struct ethsw_port_priv *port_priv = netdev_priv(netdev);
425         int err;
426
427         err = dpsw_if_set_max_frame_length(port_priv->ethsw_data->mc_io,
428                                            0,
429                                            port_priv->ethsw_data->dpsw_handle,
430                                            port_priv->idx,
431                                            (u16)ETHSW_L2_MAX_FRM(mtu));
432         if (err) {
433                 netdev_err(netdev,
434                            "dpsw_if_set_max_frame_length() err %d\n", err);
435                 return err;
436         }
437
438         netdev->mtu = mtu;
439         return 0;
440 }
441
442 static int port_carrier_state_sync(struct net_device *netdev)
443 {
444         struct ethsw_port_priv *port_priv = netdev_priv(netdev);
445         struct dpsw_link_state state;
446         int err;
447
448         err = dpsw_if_get_link_state(port_priv->ethsw_data->mc_io, 0,
449                                      port_priv->ethsw_data->dpsw_handle,
450                                      port_priv->idx, &state);
451         if (err) {
452                 netdev_err(netdev, "dpsw_if_get_link_state() err %d\n", err);
453                 return err;
454         }
455
456         WARN_ONCE(state.up > 1, "Garbage read into link_state");
457
458         if (state.up != port_priv->link_state) {
459                 if (state.up)
460                         netif_carrier_on(netdev);
461                 else
462                         netif_carrier_off(netdev);
463                 port_priv->link_state = state.up;
464         }
465         return 0;
466 }
467
468 static int port_open(struct net_device *netdev)
469 {
470         struct ethsw_port_priv *port_priv = netdev_priv(netdev);
471         int err;
472
473         /* No need to allow Tx as control interface is disabled */
474         netif_tx_stop_all_queues(netdev);
475
476         err = dpsw_if_enable(port_priv->ethsw_data->mc_io, 0,
477                              port_priv->ethsw_data->dpsw_handle,
478                              port_priv->idx);
479         if (err) {
480                 netdev_err(netdev, "dpsw_if_enable err %d\n", err);
481                 return err;
482         }
483
484         /* sync carrier state */
485         err = port_carrier_state_sync(netdev);
486         if (err) {
487                 netdev_err(netdev,
488                            "port_carrier_state_sync err %d\n", err);
489                 goto err_carrier_sync;
490         }
491
492         return 0;
493
494 err_carrier_sync:
495         dpsw_if_disable(port_priv->ethsw_data->mc_io, 0,
496                         port_priv->ethsw_data->dpsw_handle,
497                         port_priv->idx);
498         return err;
499 }
500
501 static int port_stop(struct net_device *netdev)
502 {
503         struct ethsw_port_priv *port_priv = netdev_priv(netdev);
504         int err;
505
506         err = dpsw_if_disable(port_priv->ethsw_data->mc_io, 0,
507                               port_priv->ethsw_data->dpsw_handle,
508                               port_priv->idx);
509         if (err) {
510                 netdev_err(netdev, "dpsw_if_disable err %d\n", err);
511                 return err;
512         }
513
514         return 0;
515 }
516
517 static netdev_tx_t port_dropframe(struct sk_buff *skb,
518                                   struct net_device *netdev)
519 {
520         /* we don't support I/O for now, drop the frame */
521         dev_kfree_skb_any(skb);
522
523         return NETDEV_TX_OK;
524 }
525
526 static int swdev_get_port_parent_id(struct net_device *dev,
527                                     struct netdev_phys_item_id *ppid)
528 {
529         struct ethsw_port_priv *port_priv = netdev_priv(dev);
530
531         ppid->id_len = 1;
532         ppid->id[0] = port_priv->ethsw_data->dev_id;
533
534         return 0;
535 }
536
537 static int port_get_phys_name(struct net_device *netdev, char *name,
538                               size_t len)
539 {
540         struct ethsw_port_priv *port_priv = netdev_priv(netdev);
541         int err;
542
543         err = snprintf(name, len, "p%d", port_priv->idx);
544         if (err >= len)
545                 return -EINVAL;
546
547         return 0;
548 }
549
550 struct ethsw_dump_ctx {
551         struct net_device *dev;
552         struct sk_buff *skb;
553         struct netlink_callback *cb;
554         int idx;
555 };
556
557 static int ethsw_fdb_do_dump(struct fdb_dump_entry *entry,
558                              struct ethsw_dump_ctx *dump)
559 {
560         int is_dynamic = entry->type & DPSW_FDB_ENTRY_DINAMIC;
561         u32 portid = NETLINK_CB(dump->cb->skb).portid;
562         u32 seq = dump->cb->nlh->nlmsg_seq;
563         struct nlmsghdr *nlh;
564         struct ndmsg *ndm;
565
566         if (dump->idx < dump->cb->args[2])
567                 goto skip;
568
569         nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
570                         sizeof(*ndm), NLM_F_MULTI);
571         if (!nlh)
572                 return -EMSGSIZE;
573
574         ndm = nlmsg_data(nlh);
575         ndm->ndm_family  = AF_BRIDGE;
576         ndm->ndm_pad1    = 0;
577         ndm->ndm_pad2    = 0;
578         ndm->ndm_flags   = NTF_SELF;
579         ndm->ndm_type    = 0;
580         ndm->ndm_ifindex = dump->dev->ifindex;
581         ndm->ndm_state   = is_dynamic ? NUD_REACHABLE : NUD_NOARP;
582
583         if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, entry->mac_addr))
584                 goto nla_put_failure;
585
586         nlmsg_end(dump->skb, nlh);
587
588 skip:
589         dump->idx++;
590         return 0;
591
592 nla_put_failure:
593         nlmsg_cancel(dump->skb, nlh);
594         return -EMSGSIZE;
595 }
596
597 static int port_fdb_valid_entry(struct fdb_dump_entry *entry,
598                                 struct ethsw_port_priv *port_priv)
599 {
600         int idx = port_priv->idx;
601         int valid;
602
603         if (entry->type & DPSW_FDB_ENTRY_TYPE_UNICAST)
604                 valid = entry->if_info == port_priv->idx;
605         else
606                 valid = entry->if_mask[idx / 8] & BIT(idx % 8);
607
608         return valid;
609 }
610
611 static int port_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
612                          struct net_device *net_dev,
613                          struct net_device *filter_dev, int *idx)
614 {
615         struct ethsw_port_priv *port_priv = netdev_priv(net_dev);
616         struct ethsw_core *ethsw = port_priv->ethsw_data;
617         struct device *dev = net_dev->dev.parent;
618         struct fdb_dump_entry *fdb_entries;
619         struct fdb_dump_entry fdb_entry;
620         struct ethsw_dump_ctx dump = {
621                 .dev = net_dev,
622                 .skb = skb,
623                 .cb = cb,
624                 .idx = *idx,
625         };
626         dma_addr_t fdb_dump_iova;
627         u16 num_fdb_entries;
628         u32 fdb_dump_size;
629         int err = 0, i;
630         u8 *dma_mem;
631
632         fdb_dump_size = ethsw->sw_attr.max_fdb_entries * sizeof(fdb_entry);
633         dma_mem = kzalloc(fdb_dump_size, GFP_KERNEL);
634         if (!dma_mem)
635                 return -ENOMEM;
636
637         fdb_dump_iova = dma_map_single(dev, dma_mem, fdb_dump_size,
638                                        DMA_FROM_DEVICE);
639         if (dma_mapping_error(dev, fdb_dump_iova)) {
640                 netdev_err(net_dev, "dma_map_single() failed\n");
641                 err = -ENOMEM;
642                 goto err_map;
643         }
644
645         err = dpsw_fdb_dump(ethsw->mc_io, 0, ethsw->dpsw_handle, 0,
646                             fdb_dump_iova, fdb_dump_size, &num_fdb_entries);
647         if (err) {
648                 netdev_err(net_dev, "dpsw_fdb_dump() = %d\n", err);
649                 goto err_dump;
650         }
651
652         dma_unmap_single(dev, fdb_dump_iova, fdb_dump_size, DMA_FROM_DEVICE);
653
654         fdb_entries = (struct fdb_dump_entry *)dma_mem;
655         for (i = 0; i < num_fdb_entries; i++) {
656                 fdb_entry = fdb_entries[i];
657
658                 if (!port_fdb_valid_entry(&fdb_entry, port_priv))
659                         continue;
660
661                 err = ethsw_fdb_do_dump(&fdb_entry, &dump);
662                 if (err)
663                         goto end;
664         }
665
666 end:
667         *idx = dump.idx;
668
669         kfree(dma_mem);
670
671         return 0;
672
673 err_dump:
674         dma_unmap_single(dev, fdb_dump_iova, fdb_dump_size, DMA_TO_DEVICE);
675 err_map:
676         kfree(dma_mem);
677         return err;
678 }
679
680 static const struct net_device_ops ethsw_port_ops = {
681         .ndo_open               = port_open,
682         .ndo_stop               = port_stop,
683
684         .ndo_set_mac_address    = eth_mac_addr,
685         .ndo_get_stats64        = port_get_stats,
686         .ndo_change_mtu         = port_change_mtu,
687         .ndo_has_offload_stats  = port_has_offload_stats,
688         .ndo_get_offload_stats  = port_get_offload_stats,
689         .ndo_fdb_add            = port_fdb_add,
690         .ndo_fdb_del            = port_fdb_del,
691         .ndo_fdb_dump           = port_fdb_dump,
692
693         .ndo_start_xmit         = port_dropframe,
694         .ndo_get_port_parent_id = swdev_get_port_parent_id,
695         .ndo_get_phys_port_name = port_get_phys_name,
696 };
697
698 static void ethsw_links_state_update(struct ethsw_core *ethsw)
699 {
700         int i;
701
702         for (i = 0; i < ethsw->sw_attr.num_ifs; i++)
703                 port_carrier_state_sync(ethsw->ports[i]->netdev);
704 }
705
706 static irqreturn_t ethsw_irq0_handler_thread(int irq_num, void *arg)
707 {
708         struct device *dev = (struct device *)arg;
709         struct ethsw_core *ethsw = dev_get_drvdata(dev);
710
711         /* Mask the events and the if_id reserved bits to be cleared on read */
712         u32 status = DPSW_IRQ_EVENT_LINK_CHANGED | 0xFFFF0000;
713         int err;
714
715         err = dpsw_get_irq_status(ethsw->mc_io, 0, ethsw->dpsw_handle,
716                                   DPSW_IRQ_INDEX_IF, &status);
717         if (err) {
718                 dev_err(dev, "Can't get irq status (err %d)\n", err);
719
720                 err = dpsw_clear_irq_status(ethsw->mc_io, 0, ethsw->dpsw_handle,
721                                             DPSW_IRQ_INDEX_IF, 0xFFFFFFFF);
722                 if (err)
723                         dev_err(dev, "Can't clear irq status (err %d)\n", err);
724                 goto out;
725         }
726
727         if (status & DPSW_IRQ_EVENT_LINK_CHANGED)
728                 ethsw_links_state_update(ethsw);
729
730 out:
731         return IRQ_HANDLED;
732 }
733
734 static int ethsw_setup_irqs(struct fsl_mc_device *sw_dev)
735 {
736         struct device *dev = &sw_dev->dev;
737         struct ethsw_core *ethsw = dev_get_drvdata(dev);
738         u32 mask = DPSW_IRQ_EVENT_LINK_CHANGED;
739         struct fsl_mc_device_irq *irq;
740         int err;
741
742         err = fsl_mc_allocate_irqs(sw_dev);
743         if (err) {
744                 dev_err(dev, "MC irqs allocation failed\n");
745                 return err;
746         }
747
748         if (WARN_ON(sw_dev->obj_desc.irq_count != DPSW_IRQ_NUM)) {
749                 err = -EINVAL;
750                 goto free_irq;
751         }
752
753         err = dpsw_set_irq_enable(ethsw->mc_io, 0, ethsw->dpsw_handle,
754                                   DPSW_IRQ_INDEX_IF, 0);
755         if (err) {
756                 dev_err(dev, "dpsw_set_irq_enable err %d\n", err);
757                 goto free_irq;
758         }
759
760         irq = sw_dev->irqs[DPSW_IRQ_INDEX_IF];
761
762         err = devm_request_threaded_irq(dev, irq->msi_desc->irq,
763                                         NULL,
764                                         ethsw_irq0_handler_thread,
765                                         IRQF_NO_SUSPEND | IRQF_ONESHOT,
766                                         dev_name(dev), dev);
767         if (err) {
768                 dev_err(dev, "devm_request_threaded_irq(): %d\n", err);
769                 goto free_irq;
770         }
771
772         err = dpsw_set_irq_mask(ethsw->mc_io, 0, ethsw->dpsw_handle,
773                                 DPSW_IRQ_INDEX_IF, mask);
774         if (err) {
775                 dev_err(dev, "dpsw_set_irq_mask(): %d\n", err);
776                 goto free_devm_irq;
777         }
778
779         err = dpsw_set_irq_enable(ethsw->mc_io, 0, ethsw->dpsw_handle,
780                                   DPSW_IRQ_INDEX_IF, 1);
781         if (err) {
782                 dev_err(dev, "dpsw_set_irq_enable(): %d\n", err);
783                 goto free_devm_irq;
784         }
785
786         return 0;
787
788 free_devm_irq:
789         devm_free_irq(dev, irq->msi_desc->irq, dev);
790 free_irq:
791         fsl_mc_free_irqs(sw_dev);
792         return err;
793 }
794
795 static void ethsw_teardown_irqs(struct fsl_mc_device *sw_dev)
796 {
797         struct device *dev = &sw_dev->dev;
798         struct ethsw_core *ethsw = dev_get_drvdata(dev);
799         int err;
800
801         err = dpsw_set_irq_enable(ethsw->mc_io, 0, ethsw->dpsw_handle,
802                                   DPSW_IRQ_INDEX_IF, 0);
803         if (err)
804                 dev_err(dev, "dpsw_set_irq_enable err %d\n", err);
805
806         fsl_mc_free_irqs(sw_dev);
807 }
808
809 static int port_attr_stp_state_set(struct net_device *netdev,
810                                    struct switchdev_trans *trans,
811                                    u8 state)
812 {
813         struct ethsw_port_priv *port_priv = netdev_priv(netdev);
814
815         if (switchdev_trans_ph_prepare(trans))
816                 return 0;
817
818         return ethsw_port_set_stp_state(port_priv, state);
819 }
820
821 static int port_attr_br_flags_pre_set(struct net_device *netdev,
822                                       struct switchdev_trans *trans,
823                                       unsigned long flags)
824 {
825         if (flags & ~(BR_LEARNING | BR_FLOOD))
826                 return -EINVAL;
827
828         return 0;
829 }
830
831 static int port_attr_br_flags_set(struct net_device *netdev,
832                                   struct switchdev_trans *trans,
833                                   unsigned long flags)
834 {
835         struct ethsw_port_priv *port_priv = netdev_priv(netdev);
836         int err = 0;
837
838         if (switchdev_trans_ph_prepare(trans))
839                 return 0;
840
841         /* Learning is enabled per switch */
842         err = ethsw_set_learning(port_priv->ethsw_data,
843                                  !!(flags & BR_LEARNING));
844         if (err)
845                 goto exit;
846
847         err = ethsw_port_set_flood(port_priv, !!(flags & BR_FLOOD));
848
849 exit:
850         return err;
851 }
852
853 static int swdev_port_attr_set(struct net_device *netdev,
854                                const struct switchdev_attr *attr,
855                                struct switchdev_trans *trans)
856 {
857         int err = 0;
858
859         switch (attr->id) {
860         case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
861                 err = port_attr_stp_state_set(netdev, trans,
862                                               attr->u.stp_state);
863                 break;
864         case SWITCHDEV_ATTR_ID_PORT_PRE_BRIDGE_FLAGS:
865                 err = port_attr_br_flags_pre_set(netdev, trans,
866                                                  attr->u.brport_flags);
867                 break;
868         case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS:
869                 err = port_attr_br_flags_set(netdev, trans,
870                                              attr->u.brport_flags);
871                 break;
872         case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
873                 /* VLANs are supported by default  */
874                 break;
875         default:
876                 err = -EOPNOTSUPP;
877                 break;
878         }
879
880         return err;
881 }
882
883 static int port_vlans_add(struct net_device *netdev,
884                           const struct switchdev_obj_port_vlan *vlan,
885                           struct switchdev_trans *trans)
886 {
887         struct ethsw_port_priv *port_priv = netdev_priv(netdev);
888         int vid, err = 0;
889
890         if (switchdev_trans_ph_prepare(trans))
891                 return 0;
892
893         for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
894                 if (!port_priv->ethsw_data->vlans[vid]) {
895                         /* this is a new VLAN */
896                         err = ethsw_add_vlan(port_priv->ethsw_data, vid);
897                         if (err)
898                                 return err;
899
900                         port_priv->ethsw_data->vlans[vid] |= ETHSW_VLAN_GLOBAL;
901                 }
902                 err = ethsw_port_add_vlan(port_priv, vid, vlan->flags);
903                 if (err)
904                         break;
905         }
906
907         return err;
908 }
909
910 static int port_lookup_address(struct net_device *netdev, int is_uc,
911                                const unsigned char *addr)
912 {
913         struct netdev_hw_addr_list *list = (is_uc) ? &netdev->uc : &netdev->mc;
914         struct netdev_hw_addr *ha;
915
916         netif_addr_lock_bh(netdev);
917         list_for_each_entry(ha, &list->list, list) {
918                 if (ether_addr_equal(ha->addr, addr)) {
919                         netif_addr_unlock_bh(netdev);
920                         return 1;
921                 }
922         }
923         netif_addr_unlock_bh(netdev);
924         return 0;
925 }
926
927 static int port_mdb_add(struct net_device *netdev,
928                         const struct switchdev_obj_port_mdb *mdb,
929                         struct switchdev_trans *trans)
930 {
931         struct ethsw_port_priv *port_priv = netdev_priv(netdev);
932         int err;
933
934         if (switchdev_trans_ph_prepare(trans))
935                 return 0;
936
937         /* Check if address is already set on this port */
938         if (port_lookup_address(netdev, 0, mdb->addr))
939                 return -EEXIST;
940
941         err = ethsw_port_fdb_add_mc(port_priv, mdb->addr);
942         if (err)
943                 return err;
944
945         err = dev_mc_add(netdev, mdb->addr);
946         if (err) {
947                 netdev_err(netdev, "dev_mc_add err %d\n", err);
948                 ethsw_port_fdb_del_mc(port_priv, mdb->addr);
949         }
950
951         return err;
952 }
953
954 static int swdev_port_obj_add(struct net_device *netdev,
955                               const struct switchdev_obj *obj,
956                               struct switchdev_trans *trans)
957 {
958         int err;
959
960         switch (obj->id) {
961         case SWITCHDEV_OBJ_ID_PORT_VLAN:
962                 err = port_vlans_add(netdev,
963                                      SWITCHDEV_OBJ_PORT_VLAN(obj),
964                                      trans);
965                 break;
966         case SWITCHDEV_OBJ_ID_PORT_MDB:
967                 err = port_mdb_add(netdev,
968                                    SWITCHDEV_OBJ_PORT_MDB(obj),
969                                    trans);
970                 break;
971         default:
972                 err = -EOPNOTSUPP;
973                 break;
974         }
975
976         return err;
977 }
978
979 static int ethsw_port_del_vlan(struct ethsw_port_priv *port_priv, u16 vid)
980 {
981         struct ethsw_core *ethsw = port_priv->ethsw_data;
982         struct net_device *netdev = port_priv->netdev;
983         struct dpsw_vlan_if_cfg vcfg;
984         int i, err;
985
986         if (!port_priv->vlans[vid])
987                 return -ENOENT;
988
989         if (port_priv->vlans[vid] & ETHSW_VLAN_PVID) {
990                 err = ethsw_port_set_pvid(port_priv, 0);
991                 if (err)
992                         return err;
993         }
994
995         vcfg.num_ifs = 1;
996         vcfg.if_id[0] = port_priv->idx;
997         if (port_priv->vlans[vid] & ETHSW_VLAN_UNTAGGED) {
998                 err = dpsw_vlan_remove_if_untagged(ethsw->mc_io, 0,
999                                                    ethsw->dpsw_handle,
1000                                                    vid, &vcfg);
1001                 if (err) {
1002                         netdev_err(netdev,
1003                                    "dpsw_vlan_remove_if_untagged err %d\n",
1004                                    err);
1005                 }
1006                 port_priv->vlans[vid] &= ~ETHSW_VLAN_UNTAGGED;
1007         }
1008
1009         if (port_priv->vlans[vid] & ETHSW_VLAN_MEMBER) {
1010                 err = dpsw_vlan_remove_if(ethsw->mc_io, 0, ethsw->dpsw_handle,
1011                                           vid, &vcfg);
1012                 if (err) {
1013                         netdev_err(netdev,
1014                                    "dpsw_vlan_remove_if err %d\n", err);
1015                         return err;
1016                 }
1017                 port_priv->vlans[vid] &= ~ETHSW_VLAN_MEMBER;
1018
1019                 /* Delete VLAN from switch if it is no longer configured on
1020                  * any port
1021                  */
1022                 for (i = 0; i < ethsw->sw_attr.num_ifs; i++)
1023                         if (ethsw->ports[i]->vlans[vid] & ETHSW_VLAN_MEMBER)
1024                                 return 0; /* Found a port member in VID */
1025
1026                 ethsw->vlans[vid] &= ~ETHSW_VLAN_GLOBAL;
1027
1028                 err = ethsw_dellink_switch(ethsw, vid);
1029                 if (err)
1030                         return err;
1031         }
1032
1033         return 0;
1034 }
1035
1036 static int port_vlans_del(struct net_device *netdev,
1037                           const struct switchdev_obj_port_vlan *vlan)
1038 {
1039         struct ethsw_port_priv *port_priv = netdev_priv(netdev);
1040         int vid, err = 0;
1041
1042         if (netif_is_bridge_master(vlan->obj.orig_dev))
1043                 return -EOPNOTSUPP;
1044
1045         for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
1046                 err = ethsw_port_del_vlan(port_priv, vid);
1047                 if (err)
1048                         break;
1049         }
1050
1051         return err;
1052 }
1053
1054 static int port_mdb_del(struct net_device *netdev,
1055                         const struct switchdev_obj_port_mdb *mdb)
1056 {
1057         struct ethsw_port_priv *port_priv = netdev_priv(netdev);
1058         int err;
1059
1060         if (!port_lookup_address(netdev, 0, mdb->addr))
1061                 return -ENOENT;
1062
1063         err = ethsw_port_fdb_del_mc(port_priv, mdb->addr);
1064         if (err)
1065                 return err;
1066
1067         err = dev_mc_del(netdev, mdb->addr);
1068         if (err) {
1069                 netdev_err(netdev, "dev_mc_del err %d\n", err);
1070                 return err;
1071         }
1072
1073         return err;
1074 }
1075
1076 static int swdev_port_obj_del(struct net_device *netdev,
1077                               const struct switchdev_obj *obj)
1078 {
1079         int err;
1080
1081         switch (obj->id) {
1082         case SWITCHDEV_OBJ_ID_PORT_VLAN:
1083                 err = port_vlans_del(netdev, SWITCHDEV_OBJ_PORT_VLAN(obj));
1084                 break;
1085         case SWITCHDEV_OBJ_ID_PORT_MDB:
1086                 err = port_mdb_del(netdev, SWITCHDEV_OBJ_PORT_MDB(obj));
1087                 break;
1088         default:
1089                 err = -EOPNOTSUPP;
1090                 break;
1091         }
1092         return err;
1093 }
1094
1095 static int
1096 ethsw_switchdev_port_attr_set_event(struct net_device *netdev,
1097                                     struct switchdev_notifier_port_attr_info
1098                                     *port_attr_info)
1099 {
1100         int err;
1101
1102         err = swdev_port_attr_set(netdev, port_attr_info->attr,
1103                                   port_attr_info->trans);
1104
1105         port_attr_info->handled = true;
1106         return notifier_from_errno(err);
1107 }
1108
1109 /* For the moment, only flood setting needs to be updated */
1110 static int port_bridge_join(struct net_device *netdev,
1111                             struct net_device *upper_dev)
1112 {
1113         struct ethsw_port_priv *port_priv = netdev_priv(netdev);
1114         struct ethsw_core *ethsw = port_priv->ethsw_data;
1115         int i, err;
1116
1117         for (i = 0; i < ethsw->sw_attr.num_ifs; i++)
1118                 if (ethsw->ports[i]->bridge_dev &&
1119                     (ethsw->ports[i]->bridge_dev != upper_dev)) {
1120                         netdev_err(netdev,
1121                                    "Only one bridge supported per DPSW object!\n");
1122                         return -EINVAL;
1123                 }
1124
1125         /* Enable flooding */
1126         err = ethsw_port_set_flood(port_priv, 1);
1127         if (!err)
1128                 port_priv->bridge_dev = upper_dev;
1129
1130         return err;
1131 }
1132
1133 static int port_bridge_leave(struct net_device *netdev)
1134 {
1135         struct ethsw_port_priv *port_priv = netdev_priv(netdev);
1136         int err;
1137
1138         /* Disable flooding */
1139         err = ethsw_port_set_flood(port_priv, 0);
1140         if (!err)
1141                 port_priv->bridge_dev = NULL;
1142
1143         return err;
1144 }
1145
1146 static bool ethsw_port_dev_check(const struct net_device *netdev)
1147 {
1148         return netdev->netdev_ops == &ethsw_port_ops;
1149 }
1150
1151 static int port_netdevice_event(struct notifier_block *unused,
1152                                 unsigned long event, void *ptr)
1153 {
1154         struct net_device *netdev = netdev_notifier_info_to_dev(ptr);
1155         struct netdev_notifier_changeupper_info *info = ptr;
1156         struct net_device *upper_dev;
1157         int err = 0;
1158
1159         if (!ethsw_port_dev_check(netdev))
1160                 return NOTIFY_DONE;
1161
1162         /* Handle just upper dev link/unlink for the moment */
1163         if (event == NETDEV_CHANGEUPPER) {
1164                 upper_dev = info->upper_dev;
1165                 if (netif_is_bridge_master(upper_dev)) {
1166                         if (info->linking)
1167                                 err = port_bridge_join(netdev, upper_dev);
1168                         else
1169                                 err = port_bridge_leave(netdev);
1170                 }
1171         }
1172
1173         return notifier_from_errno(err);
1174 }
1175
1176 struct ethsw_switchdev_event_work {
1177         struct work_struct work;
1178         struct switchdev_notifier_fdb_info fdb_info;
1179         struct net_device *dev;
1180         unsigned long event;
1181 };
1182
1183 static void ethsw_switchdev_event_work(struct work_struct *work)
1184 {
1185         struct ethsw_switchdev_event_work *switchdev_work =
1186                 container_of(work, struct ethsw_switchdev_event_work, work);
1187         struct net_device *dev = switchdev_work->dev;
1188         struct switchdev_notifier_fdb_info *fdb_info;
1189         int err;
1190
1191         rtnl_lock();
1192         fdb_info = &switchdev_work->fdb_info;
1193
1194         switch (switchdev_work->event) {
1195         case SWITCHDEV_FDB_ADD_TO_DEVICE:
1196                 if (!fdb_info->added_by_user)
1197                         break;
1198                 if (is_unicast_ether_addr(fdb_info->addr))
1199                         err = ethsw_port_fdb_add_uc(netdev_priv(dev),
1200                                                     fdb_info->addr);
1201                 else
1202                         err = ethsw_port_fdb_add_mc(netdev_priv(dev),
1203                                                     fdb_info->addr);
1204                 if (err)
1205                         break;
1206                 fdb_info->offloaded = true;
1207                 call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED, dev,
1208                                          &fdb_info->info, NULL);
1209                 break;
1210         case SWITCHDEV_FDB_DEL_TO_DEVICE:
1211                 if (!fdb_info->added_by_user)
1212                         break;
1213                 if (is_unicast_ether_addr(fdb_info->addr))
1214                         ethsw_port_fdb_del_uc(netdev_priv(dev), fdb_info->addr);
1215                 else
1216                         ethsw_port_fdb_del_mc(netdev_priv(dev), fdb_info->addr);
1217                 break;
1218         }
1219
1220         rtnl_unlock();
1221         kfree(switchdev_work->fdb_info.addr);
1222         kfree(switchdev_work);
1223         dev_put(dev);
1224 }
1225
1226 /* Called under rcu_read_lock() */
1227 static int port_switchdev_event(struct notifier_block *unused,
1228                                 unsigned long event, void *ptr)
1229 {
1230         struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
1231         struct ethsw_port_priv *port_priv = netdev_priv(dev);
1232         struct ethsw_switchdev_event_work *switchdev_work;
1233         struct switchdev_notifier_fdb_info *fdb_info = ptr;
1234         struct ethsw_core *ethsw = port_priv->ethsw_data;
1235
1236         if (!ethsw_port_dev_check(dev))
1237                 return NOTIFY_DONE;
1238
1239         if (event == SWITCHDEV_PORT_ATTR_SET)
1240                 return ethsw_switchdev_port_attr_set_event(dev, ptr);
1241
1242         switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC);
1243         if (!switchdev_work)
1244                 return NOTIFY_BAD;
1245
1246         INIT_WORK(&switchdev_work->work, ethsw_switchdev_event_work);
1247         switchdev_work->dev = dev;
1248         switchdev_work->event = event;
1249
1250         switch (event) {
1251         case SWITCHDEV_FDB_ADD_TO_DEVICE:
1252         case SWITCHDEV_FDB_DEL_TO_DEVICE:
1253                 memcpy(&switchdev_work->fdb_info, ptr,
1254                        sizeof(switchdev_work->fdb_info));
1255                 switchdev_work->fdb_info.addr = kzalloc(ETH_ALEN, GFP_ATOMIC);
1256                 if (!switchdev_work->fdb_info.addr)
1257                         goto err_addr_alloc;
1258
1259                 ether_addr_copy((u8 *)switchdev_work->fdb_info.addr,
1260                                 fdb_info->addr);
1261
1262                 /* Take a reference on the device to avoid being freed. */
1263                 dev_hold(dev);
1264                 break;
1265         default:
1266                 kfree(switchdev_work);
1267                 return NOTIFY_DONE;
1268         }
1269
1270         queue_work(ethsw->workqueue, &switchdev_work->work);
1271
1272         return NOTIFY_DONE;
1273
1274 err_addr_alloc:
1275         kfree(switchdev_work);
1276         return NOTIFY_BAD;
1277 }
1278
1279 static int
1280 ethsw_switchdev_port_obj_event(unsigned long event, struct net_device *netdev,
1281                                struct switchdev_notifier_port_obj_info
1282                                *port_obj_info)
1283 {
1284         int err = -EOPNOTSUPP;
1285
1286         switch (event) {
1287         case SWITCHDEV_PORT_OBJ_ADD:
1288                 err = swdev_port_obj_add(netdev, port_obj_info->obj,
1289                                          port_obj_info->trans);
1290                 break;
1291         case SWITCHDEV_PORT_OBJ_DEL:
1292                 err = swdev_port_obj_del(netdev, port_obj_info->obj);
1293                 break;
1294         }
1295
1296         port_obj_info->handled = true;
1297         return notifier_from_errno(err);
1298 }
1299
1300 static int port_switchdev_blocking_event(struct notifier_block *unused,
1301                                          unsigned long event, void *ptr)
1302 {
1303         struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
1304
1305         if (!ethsw_port_dev_check(dev))
1306                 return NOTIFY_DONE;
1307
1308         switch (event) {
1309         case SWITCHDEV_PORT_OBJ_ADD: /* fall through */
1310         case SWITCHDEV_PORT_OBJ_DEL:
1311                 return ethsw_switchdev_port_obj_event(event, dev, ptr);
1312         case SWITCHDEV_PORT_ATTR_SET:
1313                 return ethsw_switchdev_port_attr_set_event(dev, ptr);
1314         }
1315
1316         return NOTIFY_DONE;
1317 }
1318
1319 static int ethsw_register_notifier(struct device *dev)
1320 {
1321         struct ethsw_core *ethsw = dev_get_drvdata(dev);
1322         int err;
1323
1324         ethsw->port_nb.notifier_call = port_netdevice_event;
1325         err = register_netdevice_notifier(&ethsw->port_nb);
1326         if (err) {
1327                 dev_err(dev, "Failed to register netdev notifier\n");
1328                 return err;
1329         }
1330
1331         ethsw->port_switchdev_nb.notifier_call = port_switchdev_event;
1332         err = register_switchdev_notifier(&ethsw->port_switchdev_nb);
1333         if (err) {
1334                 dev_err(dev, "Failed to register switchdev notifier\n");
1335                 goto err_switchdev_nb;
1336         }
1337
1338         ethsw->port_switchdevb_nb.notifier_call = port_switchdev_blocking_event;
1339         err = register_switchdev_blocking_notifier(&ethsw->port_switchdevb_nb);
1340         if (err) {
1341                 dev_err(dev, "Failed to register switchdev blocking notifier\n");
1342                 goto err_switchdev_blocking_nb;
1343         }
1344
1345         return 0;
1346
1347 err_switchdev_blocking_nb:
1348         unregister_switchdev_notifier(&ethsw->port_switchdev_nb);
1349 err_switchdev_nb:
1350         unregister_netdevice_notifier(&ethsw->port_nb);
1351         return err;
1352 }
1353
1354 static int ethsw_init(struct fsl_mc_device *sw_dev)
1355 {
1356         struct device *dev = &sw_dev->dev;
1357         struct ethsw_core *ethsw = dev_get_drvdata(dev);
1358         u16 version_major, version_minor, i;
1359         struct dpsw_stp_cfg stp_cfg;
1360         int err;
1361
1362         ethsw->dev_id = sw_dev->obj_desc.id;
1363
1364         err = dpsw_open(ethsw->mc_io, 0, ethsw->dev_id, &ethsw->dpsw_handle);
1365         if (err) {
1366                 dev_err(dev, "dpsw_open err %d\n", err);
1367                 return err;
1368         }
1369
1370         err = dpsw_get_attributes(ethsw->mc_io, 0, ethsw->dpsw_handle,
1371                                   &ethsw->sw_attr);
1372         if (err) {
1373                 dev_err(dev, "dpsw_get_attributes err %d\n", err);
1374                 goto err_close;
1375         }
1376
1377         err = dpsw_get_api_version(ethsw->mc_io, 0,
1378                                    &version_major,
1379                                    &version_minor);
1380         if (err) {
1381                 dev_err(dev, "dpsw_get_api_version err %d\n", err);
1382                 goto err_close;
1383         }
1384
1385         /* Minimum supported DPSW version check */
1386         if (version_major < DPSW_MIN_VER_MAJOR ||
1387             (version_major == DPSW_MIN_VER_MAJOR &&
1388              version_minor < DPSW_MIN_VER_MINOR)) {
1389                 dev_err(dev, "DPSW version %d:%d not supported. Use %d.%d or greater.\n",
1390                         version_major,
1391                         version_minor,
1392                         DPSW_MIN_VER_MAJOR, DPSW_MIN_VER_MINOR);
1393                 err = -ENOTSUPP;
1394                 goto err_close;
1395         }
1396
1397         err = dpsw_reset(ethsw->mc_io, 0, ethsw->dpsw_handle);
1398         if (err) {
1399                 dev_err(dev, "dpsw_reset err %d\n", err);
1400                 goto err_close;
1401         }
1402
1403         err = dpsw_fdb_set_learning_mode(ethsw->mc_io, 0, ethsw->dpsw_handle, 0,
1404                                          DPSW_FDB_LEARNING_MODE_HW);
1405         if (err) {
1406                 dev_err(dev, "dpsw_fdb_set_learning_mode err %d\n", err);
1407                 goto err_close;
1408         }
1409
1410         stp_cfg.vlan_id = DEFAULT_VLAN_ID;
1411         stp_cfg.state = DPSW_STP_STATE_FORWARDING;
1412
1413         for (i = 0; i < ethsw->sw_attr.num_ifs; i++) {
1414                 err = dpsw_if_set_stp(ethsw->mc_io, 0, ethsw->dpsw_handle, i,
1415                                       &stp_cfg);
1416                 if (err) {
1417                         dev_err(dev, "dpsw_if_set_stp err %d for port %d\n",
1418                                 err, i);
1419                         goto err_close;
1420                 }
1421
1422                 err = dpsw_if_set_broadcast(ethsw->mc_io, 0,
1423                                             ethsw->dpsw_handle, i, 1);
1424                 if (err) {
1425                         dev_err(dev,
1426                                 "dpsw_if_set_broadcast err %d for port %d\n",
1427                                 err, i);
1428                         goto err_close;
1429                 }
1430         }
1431
1432         ethsw->workqueue = alloc_ordered_workqueue("%s_%d_ordered",
1433                                                    WQ_MEM_RECLAIM, "ethsw",
1434                                                    ethsw->sw_attr.id);
1435         if (!ethsw->workqueue) {
1436                 err = -ENOMEM;
1437                 goto err_close;
1438         }
1439
1440         err = ethsw_register_notifier(dev);
1441         if (err)
1442                 goto err_destroy_ordered_workqueue;
1443
1444         return 0;
1445
1446 err_destroy_ordered_workqueue:
1447         destroy_workqueue(ethsw->workqueue);
1448
1449 err_close:
1450         dpsw_close(ethsw->mc_io, 0, ethsw->dpsw_handle);
1451         return err;
1452 }
1453
1454 static int ethsw_port_init(struct ethsw_port_priv *port_priv, u16 port)
1455 {
1456         struct net_device *netdev = port_priv->netdev;
1457         struct ethsw_core *ethsw = port_priv->ethsw_data;
1458         struct dpsw_vlan_if_cfg vcfg;
1459         int err;
1460
1461         /* Switch starts with all ports configured to VLAN 1. Need to
1462          * remove this setting to allow configuration at bridge join
1463          */
1464         vcfg.num_ifs = 1;
1465         vcfg.if_id[0] = port_priv->idx;
1466
1467         err = dpsw_vlan_remove_if_untagged(ethsw->mc_io, 0, ethsw->dpsw_handle,
1468                                            DEFAULT_VLAN_ID, &vcfg);
1469         if (err) {
1470                 netdev_err(netdev, "dpsw_vlan_remove_if_untagged err %d\n",
1471                            err);
1472                 return err;
1473         }
1474
1475         err = ethsw_port_set_pvid(port_priv, 0);
1476         if (err)
1477                 return err;
1478
1479         err = dpsw_vlan_remove_if(ethsw->mc_io, 0, ethsw->dpsw_handle,
1480                                   DEFAULT_VLAN_ID, &vcfg);
1481         if (err)
1482                 netdev_err(netdev, "dpsw_vlan_remove_if err %d\n", err);
1483
1484         return err;
1485 }
1486
1487 static void ethsw_unregister_notifier(struct device *dev)
1488 {
1489         struct ethsw_core *ethsw = dev_get_drvdata(dev);
1490         struct notifier_block *nb;
1491         int err;
1492
1493         nb = &ethsw->port_switchdevb_nb;
1494         err = unregister_switchdev_blocking_notifier(nb);
1495         if (err)
1496                 dev_err(dev,
1497                         "Failed to unregister switchdev blocking notifier (%d)\n",
1498                         err);
1499
1500         err = unregister_switchdev_notifier(&ethsw->port_switchdev_nb);
1501         if (err)
1502                 dev_err(dev,
1503                         "Failed to unregister switchdev notifier (%d)\n", err);
1504
1505         err = unregister_netdevice_notifier(&ethsw->port_nb);
1506         if (err)
1507                 dev_err(dev,
1508                         "Failed to unregister netdev notifier (%d)\n", err);
1509 }
1510
1511 static void ethsw_takedown(struct fsl_mc_device *sw_dev)
1512 {
1513         struct device *dev = &sw_dev->dev;
1514         struct ethsw_core *ethsw = dev_get_drvdata(dev);
1515         int err;
1516
1517         ethsw_unregister_notifier(dev);
1518
1519         err = dpsw_close(ethsw->mc_io, 0, ethsw->dpsw_handle);
1520         if (err)
1521                 dev_warn(dev, "dpsw_close err %d\n", err);
1522 }
1523
1524 static int ethsw_remove(struct fsl_mc_device *sw_dev)
1525 {
1526         struct ethsw_port_priv *port_priv;
1527         struct ethsw_core *ethsw;
1528         struct device *dev;
1529         int i;
1530
1531         dev = &sw_dev->dev;
1532         ethsw = dev_get_drvdata(dev);
1533
1534         ethsw_teardown_irqs(sw_dev);
1535
1536         destroy_workqueue(ethsw->workqueue);
1537
1538         dpsw_disable(ethsw->mc_io, 0, ethsw->dpsw_handle);
1539
1540         for (i = 0; i < ethsw->sw_attr.num_ifs; i++) {
1541                 port_priv = ethsw->ports[i];
1542                 unregister_netdev(port_priv->netdev);
1543                 free_netdev(port_priv->netdev);
1544         }
1545         kfree(ethsw->ports);
1546
1547         ethsw_takedown(sw_dev);
1548         fsl_mc_portal_free(ethsw->mc_io);
1549
1550         kfree(ethsw);
1551
1552         dev_set_drvdata(dev, NULL);
1553
1554         return 0;
1555 }
1556
1557 static int ethsw_probe_port(struct ethsw_core *ethsw, u16 port_idx)
1558 {
1559         struct ethsw_port_priv *port_priv;
1560         struct device *dev = ethsw->dev;
1561         struct net_device *port_netdev;
1562         int err;
1563
1564         port_netdev = alloc_etherdev(sizeof(struct ethsw_port_priv));
1565         if (!port_netdev) {
1566                 dev_err(dev, "alloc_etherdev error\n");
1567                 return -ENOMEM;
1568         }
1569
1570         port_priv = netdev_priv(port_netdev);
1571         port_priv->netdev = port_netdev;
1572         port_priv->ethsw_data = ethsw;
1573
1574         port_priv->idx = port_idx;
1575         port_priv->stp_state = BR_STATE_FORWARDING;
1576
1577         /* Flooding is implicitly enabled */
1578         port_priv->flood = true;
1579
1580         SET_NETDEV_DEV(port_netdev, dev);
1581         port_netdev->netdev_ops = &ethsw_port_ops;
1582         port_netdev->ethtool_ops = &ethsw_port_ethtool_ops;
1583
1584         /* Set MTU limits */
1585         port_netdev->min_mtu = ETH_MIN_MTU;
1586         port_netdev->max_mtu = ETHSW_MAX_FRAME_LENGTH;
1587
1588         err = ethsw_port_init(port_priv, port_idx);
1589         if (err)
1590                 goto err_port_probe;
1591
1592         err = register_netdev(port_netdev);
1593         if (err < 0) {
1594                 dev_err(dev, "register_netdev error %d\n", err);
1595                 goto err_port_probe;
1596         }
1597
1598         ethsw->ports[port_idx] = port_priv;
1599
1600         return 0;
1601
1602 err_port_probe:
1603         free_netdev(port_netdev);
1604
1605         return err;
1606 }
1607
1608 static int ethsw_probe(struct fsl_mc_device *sw_dev)
1609 {
1610         struct device *dev = &sw_dev->dev;
1611         struct ethsw_core *ethsw;
1612         int i, err;
1613
1614         /* Allocate switch core*/
1615         ethsw = kzalloc(sizeof(*ethsw), GFP_KERNEL);
1616
1617         if (!ethsw)
1618                 return -ENOMEM;
1619
1620         ethsw->dev = dev;
1621         dev_set_drvdata(dev, ethsw);
1622
1623         err = fsl_mc_portal_allocate(sw_dev, FSL_MC_IO_ATOMIC_CONTEXT_PORTAL,
1624                                      &ethsw->mc_io);
1625         if (err) {
1626                 if (err == -ENXIO)
1627                         err = -EPROBE_DEFER;
1628                 else
1629                         dev_err(dev, "fsl_mc_portal_allocate err %d\n", err);
1630                 goto err_free_drvdata;
1631         }
1632
1633         err = ethsw_init(sw_dev);
1634         if (err)
1635                 goto err_free_cmdport;
1636
1637         /* DEFAULT_VLAN_ID is implicitly configured on the switch */
1638         ethsw->vlans[DEFAULT_VLAN_ID] = ETHSW_VLAN_MEMBER;
1639
1640         /* Learning is implicitly enabled */
1641         ethsw->learning = true;
1642
1643         ethsw->ports = kcalloc(ethsw->sw_attr.num_ifs, sizeof(*ethsw->ports),
1644                                GFP_KERNEL);
1645         if (!(ethsw->ports)) {
1646                 err = -ENOMEM;
1647                 goto err_takedown;
1648         }
1649
1650         for (i = 0; i < ethsw->sw_attr.num_ifs; i++) {
1651                 err = ethsw_probe_port(ethsw, i);
1652                 if (err)
1653                         goto err_free_ports;
1654         }
1655
1656         err = dpsw_enable(ethsw->mc_io, 0, ethsw->dpsw_handle);
1657         if (err) {
1658                 dev_err(ethsw->dev, "dpsw_enable err %d\n", err);
1659                 goto err_free_ports;
1660         }
1661
1662         /* Setup IRQs */
1663         err = ethsw_setup_irqs(sw_dev);
1664         if (err)
1665                 goto err_stop;
1666
1667         dev_info(dev, "probed %d port switch\n", ethsw->sw_attr.num_ifs);
1668         return 0;
1669
1670 err_stop:
1671         dpsw_disable(ethsw->mc_io, 0, ethsw->dpsw_handle);
1672
1673 err_free_ports:
1674         /* Cleanup registered ports only */
1675         for (i--; i >= 0; i--) {
1676                 unregister_netdev(ethsw->ports[i]->netdev);
1677                 free_netdev(ethsw->ports[i]->netdev);
1678         }
1679         kfree(ethsw->ports);
1680
1681 err_takedown:
1682         ethsw_takedown(sw_dev);
1683
1684 err_free_cmdport:
1685         fsl_mc_portal_free(ethsw->mc_io);
1686
1687 err_free_drvdata:
1688         kfree(ethsw);
1689         dev_set_drvdata(dev, NULL);
1690
1691         return err;
1692 }
1693
1694 static const struct fsl_mc_device_id ethsw_match_id_table[] = {
1695         {
1696                 .vendor = FSL_MC_VENDOR_FREESCALE,
1697                 .obj_type = "dpsw",
1698         },
1699         { .vendor = 0x0 }
1700 };
1701 MODULE_DEVICE_TABLE(fslmc, ethsw_match_id_table);
1702
1703 static struct fsl_mc_driver eth_sw_drv = {
1704         .driver = {
1705                 .name = KBUILD_MODNAME,
1706                 .owner = THIS_MODULE,
1707         },
1708         .probe = ethsw_probe,
1709         .remove = ethsw_remove,
1710         .match_id_table = ethsw_match_id_table
1711 };
1712
1713 module_fsl_mc_driver(eth_sw_drv);
1714
1715 MODULE_LICENSE("GPL v2");
1716 MODULE_DESCRIPTION("DPAA2 Ethernet Switch Driver");