]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/net/ethernet/mellanox/mlxsw/spectrum.c
b8238ed9ae7354f388e643c9295ed7543e7f8e3f
[karo-tx-linux.git] / drivers / net / ethernet / mellanox / mlxsw / spectrum.c
1 /*
2  * drivers/net/ethernet/mellanox/mlxsw/spectrum.c
3  * Copyright (c) 2015-2017 Mellanox Technologies. All rights reserved.
4  * Copyright (c) 2015-2017 Jiri Pirko <jiri@mellanox.com>
5  * Copyright (c) 2015 Ido Schimmel <idosch@mellanox.com>
6  * Copyright (c) 2015 Elad Raz <eladr@mellanox.com>
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the names of the copyright holders nor the names of its
17  *    contributors may be used to endorse or promote products derived from
18  *    this software without specific prior written permission.
19  *
20  * Alternatively, this software may be distributed under the terms of the
21  * GNU General Public License ("GPL") version 2 as published by the Free
22  * Software Foundation.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGE.
35  */
36
37 #include <linux/kernel.h>
38 #include <linux/module.h>
39 #include <linux/types.h>
40 #include <linux/pci.h>
41 #include <linux/netdevice.h>
42 #include <linux/etherdevice.h>
43 #include <linux/ethtool.h>
44 #include <linux/slab.h>
45 #include <linux/device.h>
46 #include <linux/skbuff.h>
47 #include <linux/if_vlan.h>
48 #include <linux/if_bridge.h>
49 #include <linux/workqueue.h>
50 #include <linux/jiffies.h>
51 #include <linux/bitops.h>
52 #include <linux/list.h>
53 #include <linux/notifier.h>
54 #include <linux/dcbnl.h>
55 #include <linux/inetdevice.h>
56 #include <net/switchdev.h>
57 #include <net/pkt_cls.h>
58 #include <net/tc_act/tc_mirred.h>
59 #include <net/netevent.h>
60 #include <net/tc_act/tc_sample.h>
61
62 #include "spectrum.h"
63 #include "pci.h"
64 #include "core.h"
65 #include "reg.h"
66 #include "port.h"
67 #include "trap.h"
68 #include "txheader.h"
69
70 static const char mlxsw_sp_driver_name[] = "mlxsw_spectrum";
71 static const char mlxsw_sp_driver_version[] = "1.0";
72
73 /* tx_hdr_version
74  * Tx header version.
75  * Must be set to 1.
76  */
77 MLXSW_ITEM32(tx, hdr, version, 0x00, 28, 4);
78
79 /* tx_hdr_ctl
80  * Packet control type.
81  * 0 - Ethernet control (e.g. EMADs, LACP)
82  * 1 - Ethernet data
83  */
84 MLXSW_ITEM32(tx, hdr, ctl, 0x00, 26, 2);
85
86 /* tx_hdr_proto
87  * Packet protocol type. Must be set to 1 (Ethernet).
88  */
89 MLXSW_ITEM32(tx, hdr, proto, 0x00, 21, 3);
90
91 /* tx_hdr_rx_is_router
92  * Packet is sent from the router. Valid for data packets only.
93  */
94 MLXSW_ITEM32(tx, hdr, rx_is_router, 0x00, 19, 1);
95
96 /* tx_hdr_fid_valid
97  * Indicates if the 'fid' field is valid and should be used for
98  * forwarding lookup. Valid for data packets only.
99  */
100 MLXSW_ITEM32(tx, hdr, fid_valid, 0x00, 16, 1);
101
102 /* tx_hdr_swid
103  * Switch partition ID. Must be set to 0.
104  */
105 MLXSW_ITEM32(tx, hdr, swid, 0x00, 12, 3);
106
107 /* tx_hdr_control_tclass
108  * Indicates if the packet should use the control TClass and not one
109  * of the data TClasses.
110  */
111 MLXSW_ITEM32(tx, hdr, control_tclass, 0x00, 6, 1);
112
113 /* tx_hdr_etclass
114  * Egress TClass to be used on the egress device on the egress port.
115  */
116 MLXSW_ITEM32(tx, hdr, etclass, 0x00, 0, 4);
117
118 /* tx_hdr_port_mid
119  * Destination local port for unicast packets.
120  * Destination multicast ID for multicast packets.
121  *
122  * Control packets are directed to a specific egress port, while data
123  * packets are transmitted through the CPU port (0) into the switch partition,
124  * where forwarding rules are applied.
125  */
126 MLXSW_ITEM32(tx, hdr, port_mid, 0x04, 16, 16);
127
128 /* tx_hdr_fid
129  * Forwarding ID used for L2 forwarding lookup. Valid only if 'fid_valid' is
130  * set, otherwise calculated based on the packet's VID using VID to FID mapping.
131  * Valid for data packets only.
132  */
133 MLXSW_ITEM32(tx, hdr, fid, 0x08, 0, 16);
134
135 /* tx_hdr_type
136  * 0 - Data packets
137  * 6 - Control packets
138  */
139 MLXSW_ITEM32(tx, hdr, type, 0x0C, 0, 4);
140
141 static void mlxsw_sp_txhdr_construct(struct sk_buff *skb,
142                                      const struct mlxsw_tx_info *tx_info)
143 {
144         char *txhdr = skb_push(skb, MLXSW_TXHDR_LEN);
145
146         memset(txhdr, 0, MLXSW_TXHDR_LEN);
147
148         mlxsw_tx_hdr_version_set(txhdr, MLXSW_TXHDR_VERSION_1);
149         mlxsw_tx_hdr_ctl_set(txhdr, MLXSW_TXHDR_ETH_CTL);
150         mlxsw_tx_hdr_proto_set(txhdr, MLXSW_TXHDR_PROTO_ETH);
151         mlxsw_tx_hdr_swid_set(txhdr, 0);
152         mlxsw_tx_hdr_control_tclass_set(txhdr, 1);
153         mlxsw_tx_hdr_port_mid_set(txhdr, tx_info->local_port);
154         mlxsw_tx_hdr_type_set(txhdr, MLXSW_TXHDR_TYPE_CONTROL);
155 }
156
157 static int mlxsw_sp_base_mac_get(struct mlxsw_sp *mlxsw_sp)
158 {
159         char spad_pl[MLXSW_REG_SPAD_LEN] = {0};
160         int err;
161
162         err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(spad), spad_pl);
163         if (err)
164                 return err;
165         mlxsw_reg_spad_base_mac_memcpy_from(spad_pl, mlxsw_sp->base_mac);
166         return 0;
167 }
168
169 static int mlxsw_sp_span_init(struct mlxsw_sp *mlxsw_sp)
170 {
171         int i;
172
173         if (!MLXSW_CORE_RES_VALID(mlxsw_sp->core, MAX_SPAN))
174                 return -EIO;
175
176         mlxsw_sp->span.entries_count = MLXSW_CORE_RES_GET(mlxsw_sp->core,
177                                                           MAX_SPAN);
178         mlxsw_sp->span.entries = kcalloc(mlxsw_sp->span.entries_count,
179                                          sizeof(struct mlxsw_sp_span_entry),
180                                          GFP_KERNEL);
181         if (!mlxsw_sp->span.entries)
182                 return -ENOMEM;
183
184         for (i = 0; i < mlxsw_sp->span.entries_count; i++)
185                 INIT_LIST_HEAD(&mlxsw_sp->span.entries[i].bound_ports_list);
186
187         return 0;
188 }
189
190 static void mlxsw_sp_span_fini(struct mlxsw_sp *mlxsw_sp)
191 {
192         int i;
193
194         for (i = 0; i < mlxsw_sp->span.entries_count; i++) {
195                 struct mlxsw_sp_span_entry *curr = &mlxsw_sp->span.entries[i];
196
197                 WARN_ON_ONCE(!list_empty(&curr->bound_ports_list));
198         }
199         kfree(mlxsw_sp->span.entries);
200 }
201
202 static struct mlxsw_sp_span_entry *
203 mlxsw_sp_span_entry_create(struct mlxsw_sp_port *port)
204 {
205         struct mlxsw_sp *mlxsw_sp = port->mlxsw_sp;
206         struct mlxsw_sp_span_entry *span_entry;
207         char mpat_pl[MLXSW_REG_MPAT_LEN];
208         u8 local_port = port->local_port;
209         int index;
210         int i;
211         int err;
212
213         /* find a free entry to use */
214         index = -1;
215         for (i = 0; i < mlxsw_sp->span.entries_count; i++) {
216                 if (!mlxsw_sp->span.entries[i].used) {
217                         index = i;
218                         span_entry = &mlxsw_sp->span.entries[i];
219                         break;
220                 }
221         }
222         if (index < 0)
223                 return NULL;
224
225         /* create a new port analayzer entry for local_port */
226         mlxsw_reg_mpat_pack(mpat_pl, index, local_port, true);
227         err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(mpat), mpat_pl);
228         if (err)
229                 return NULL;
230
231         span_entry->used = true;
232         span_entry->id = index;
233         span_entry->ref_count = 1;
234         span_entry->local_port = local_port;
235         return span_entry;
236 }
237
238 static void mlxsw_sp_span_entry_destroy(struct mlxsw_sp *mlxsw_sp,
239                                         struct mlxsw_sp_span_entry *span_entry)
240 {
241         u8 local_port = span_entry->local_port;
242         char mpat_pl[MLXSW_REG_MPAT_LEN];
243         int pa_id = span_entry->id;
244
245         mlxsw_reg_mpat_pack(mpat_pl, pa_id, local_port, false);
246         mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(mpat), mpat_pl);
247         span_entry->used = false;
248 }
249
250 static struct mlxsw_sp_span_entry *
251 mlxsw_sp_span_entry_find(struct mlxsw_sp_port *port)
252 {
253         struct mlxsw_sp *mlxsw_sp = port->mlxsw_sp;
254         int i;
255
256         for (i = 0; i < mlxsw_sp->span.entries_count; i++) {
257                 struct mlxsw_sp_span_entry *curr = &mlxsw_sp->span.entries[i];
258
259                 if (curr->used && curr->local_port == port->local_port)
260                         return curr;
261         }
262         return NULL;
263 }
264
265 static struct mlxsw_sp_span_entry
266 *mlxsw_sp_span_entry_get(struct mlxsw_sp_port *port)
267 {
268         struct mlxsw_sp_span_entry *span_entry;
269
270         span_entry = mlxsw_sp_span_entry_find(port);
271         if (span_entry) {
272                 /* Already exists, just take a reference */
273                 span_entry->ref_count++;
274                 return span_entry;
275         }
276
277         return mlxsw_sp_span_entry_create(port);
278 }
279
280 static int mlxsw_sp_span_entry_put(struct mlxsw_sp *mlxsw_sp,
281                                    struct mlxsw_sp_span_entry *span_entry)
282 {
283         WARN_ON(!span_entry->ref_count);
284         if (--span_entry->ref_count == 0)
285                 mlxsw_sp_span_entry_destroy(mlxsw_sp, span_entry);
286         return 0;
287 }
288
289 static bool mlxsw_sp_span_is_egress_mirror(struct mlxsw_sp_port *port)
290 {
291         struct mlxsw_sp *mlxsw_sp = port->mlxsw_sp;
292         struct mlxsw_sp_span_inspected_port *p;
293         int i;
294
295         for (i = 0; i < mlxsw_sp->span.entries_count; i++) {
296                 struct mlxsw_sp_span_entry *curr = &mlxsw_sp->span.entries[i];
297
298                 list_for_each_entry(p, &curr->bound_ports_list, list)
299                         if (p->local_port == port->local_port &&
300                             p->type == MLXSW_SP_SPAN_EGRESS)
301                                 return true;
302         }
303
304         return false;
305 }
306
307 static int mlxsw_sp_span_mtu_to_buffsize(int mtu)
308 {
309         return MLXSW_SP_BYTES_TO_CELLS(mtu * 5 / 2) + 1;
310 }
311
312 static int mlxsw_sp_span_port_mtu_update(struct mlxsw_sp_port *port, u16 mtu)
313 {
314         struct mlxsw_sp *mlxsw_sp = port->mlxsw_sp;
315         char sbib_pl[MLXSW_REG_SBIB_LEN];
316         int err;
317
318         /* If port is egress mirrored, the shared buffer size should be
319          * updated according to the mtu value
320          */
321         if (mlxsw_sp_span_is_egress_mirror(port)) {
322                 mlxsw_reg_sbib_pack(sbib_pl, port->local_port,
323                                     mlxsw_sp_span_mtu_to_buffsize(mtu));
324                 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sbib), sbib_pl);
325                 if (err) {
326                         netdev_err(port->dev, "Could not update shared buffer for mirroring\n");
327                         return err;
328                 }
329         }
330
331         return 0;
332 }
333
334 static struct mlxsw_sp_span_inspected_port *
335 mlxsw_sp_span_entry_bound_port_find(struct mlxsw_sp_port *port,
336                                     struct mlxsw_sp_span_entry *span_entry)
337 {
338         struct mlxsw_sp_span_inspected_port *p;
339
340         list_for_each_entry(p, &span_entry->bound_ports_list, list)
341                 if (port->local_port == p->local_port)
342                         return p;
343         return NULL;
344 }
345
346 static int
347 mlxsw_sp_span_inspected_port_bind(struct mlxsw_sp_port *port,
348                                   struct mlxsw_sp_span_entry *span_entry,
349                                   enum mlxsw_sp_span_type type)
350 {
351         struct mlxsw_sp_span_inspected_port *inspected_port;
352         struct mlxsw_sp *mlxsw_sp = port->mlxsw_sp;
353         char mpar_pl[MLXSW_REG_MPAR_LEN];
354         char sbib_pl[MLXSW_REG_SBIB_LEN];
355         int pa_id = span_entry->id;
356         int err;
357
358         /* if it is an egress SPAN, bind a shared buffer to it */
359         if (type == MLXSW_SP_SPAN_EGRESS) {
360                 mlxsw_reg_sbib_pack(sbib_pl, port->local_port,
361                                     mlxsw_sp_span_mtu_to_buffsize(port->dev->mtu));
362                 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sbib), sbib_pl);
363                 if (err) {
364                         netdev_err(port->dev, "Could not create shared buffer for mirroring\n");
365                         return err;
366                 }
367         }
368
369         /* bind the port to the SPAN entry */
370         mlxsw_reg_mpar_pack(mpar_pl, port->local_port,
371                             (enum mlxsw_reg_mpar_i_e) type, true, pa_id);
372         err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(mpar), mpar_pl);
373         if (err)
374                 goto err_mpar_reg_write;
375
376         inspected_port = kzalloc(sizeof(*inspected_port), GFP_KERNEL);
377         if (!inspected_port) {
378                 err = -ENOMEM;
379                 goto err_inspected_port_alloc;
380         }
381         inspected_port->local_port = port->local_port;
382         inspected_port->type = type;
383         list_add_tail(&inspected_port->list, &span_entry->bound_ports_list);
384
385         return 0;
386
387 err_mpar_reg_write:
388 err_inspected_port_alloc:
389         if (type == MLXSW_SP_SPAN_EGRESS) {
390                 mlxsw_reg_sbib_pack(sbib_pl, port->local_port, 0);
391                 mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sbib), sbib_pl);
392         }
393         return err;
394 }
395
396 static void
397 mlxsw_sp_span_inspected_port_unbind(struct mlxsw_sp_port *port,
398                                     struct mlxsw_sp_span_entry *span_entry,
399                                     enum mlxsw_sp_span_type type)
400 {
401         struct mlxsw_sp_span_inspected_port *inspected_port;
402         struct mlxsw_sp *mlxsw_sp = port->mlxsw_sp;
403         char mpar_pl[MLXSW_REG_MPAR_LEN];
404         char sbib_pl[MLXSW_REG_SBIB_LEN];
405         int pa_id = span_entry->id;
406
407         inspected_port = mlxsw_sp_span_entry_bound_port_find(port, span_entry);
408         if (!inspected_port)
409                 return;
410
411         /* remove the inspected port */
412         mlxsw_reg_mpar_pack(mpar_pl, port->local_port,
413                             (enum mlxsw_reg_mpar_i_e) type, false, pa_id);
414         mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(mpar), mpar_pl);
415
416         /* remove the SBIB buffer if it was egress SPAN */
417         if (type == MLXSW_SP_SPAN_EGRESS) {
418                 mlxsw_reg_sbib_pack(sbib_pl, port->local_port, 0);
419                 mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sbib), sbib_pl);
420         }
421
422         mlxsw_sp_span_entry_put(mlxsw_sp, span_entry);
423
424         list_del(&inspected_port->list);
425         kfree(inspected_port);
426 }
427
428 static int mlxsw_sp_span_mirror_add(struct mlxsw_sp_port *from,
429                                     struct mlxsw_sp_port *to,
430                                     enum mlxsw_sp_span_type type)
431 {
432         struct mlxsw_sp *mlxsw_sp = from->mlxsw_sp;
433         struct mlxsw_sp_span_entry *span_entry;
434         int err;
435
436         span_entry = mlxsw_sp_span_entry_get(to);
437         if (!span_entry)
438                 return -ENOENT;
439
440         netdev_dbg(from->dev, "Adding inspected port to SPAN entry %d\n",
441                    span_entry->id);
442
443         err = mlxsw_sp_span_inspected_port_bind(from, span_entry, type);
444         if (err)
445                 goto err_port_bind;
446
447         return 0;
448
449 err_port_bind:
450         mlxsw_sp_span_entry_put(mlxsw_sp, span_entry);
451         return err;
452 }
453
454 static void mlxsw_sp_span_mirror_remove(struct mlxsw_sp_port *from,
455                                         struct mlxsw_sp_port *to,
456                                         enum mlxsw_sp_span_type type)
457 {
458         struct mlxsw_sp_span_entry *span_entry;
459
460         span_entry = mlxsw_sp_span_entry_find(to);
461         if (!span_entry) {
462                 netdev_err(from->dev, "no span entry found\n");
463                 return;
464         }
465
466         netdev_dbg(from->dev, "removing inspected port from SPAN entry %d\n",
467                    span_entry->id);
468         mlxsw_sp_span_inspected_port_unbind(from, span_entry, type);
469 }
470
471 static int mlxsw_sp_port_sample_set(struct mlxsw_sp_port *mlxsw_sp_port,
472                                     bool enable, u32 rate)
473 {
474         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
475         char mpsc_pl[MLXSW_REG_MPSC_LEN];
476
477         mlxsw_reg_mpsc_pack(mpsc_pl, mlxsw_sp_port->local_port, enable, rate);
478         return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(mpsc), mpsc_pl);
479 }
480
481 static int mlxsw_sp_port_admin_status_set(struct mlxsw_sp_port *mlxsw_sp_port,
482                                           bool is_up)
483 {
484         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
485         char paos_pl[MLXSW_REG_PAOS_LEN];
486
487         mlxsw_reg_paos_pack(paos_pl, mlxsw_sp_port->local_port,
488                             is_up ? MLXSW_PORT_ADMIN_STATUS_UP :
489                             MLXSW_PORT_ADMIN_STATUS_DOWN);
490         return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(paos), paos_pl);
491 }
492
493 static int mlxsw_sp_port_dev_addr_set(struct mlxsw_sp_port *mlxsw_sp_port,
494                                       unsigned char *addr)
495 {
496         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
497         char ppad_pl[MLXSW_REG_PPAD_LEN];
498
499         mlxsw_reg_ppad_pack(ppad_pl, true, mlxsw_sp_port->local_port);
500         mlxsw_reg_ppad_mac_memcpy_to(ppad_pl, addr);
501         return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ppad), ppad_pl);
502 }
503
504 static int mlxsw_sp_port_dev_addr_init(struct mlxsw_sp_port *mlxsw_sp_port)
505 {
506         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
507         unsigned char *addr = mlxsw_sp_port->dev->dev_addr;
508
509         ether_addr_copy(addr, mlxsw_sp->base_mac);
510         addr[ETH_ALEN - 1] += mlxsw_sp_port->local_port;
511         return mlxsw_sp_port_dev_addr_set(mlxsw_sp_port, addr);
512 }
513
514 static int mlxsw_sp_port_mtu_set(struct mlxsw_sp_port *mlxsw_sp_port, u16 mtu)
515 {
516         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
517         char pmtu_pl[MLXSW_REG_PMTU_LEN];
518         int max_mtu;
519         int err;
520
521         mtu += MLXSW_TXHDR_LEN + ETH_HLEN;
522         mlxsw_reg_pmtu_pack(pmtu_pl, mlxsw_sp_port->local_port, 0);
523         err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(pmtu), pmtu_pl);
524         if (err)
525                 return err;
526         max_mtu = mlxsw_reg_pmtu_max_mtu_get(pmtu_pl);
527
528         if (mtu > max_mtu)
529                 return -EINVAL;
530
531         mlxsw_reg_pmtu_pack(pmtu_pl, mlxsw_sp_port->local_port, mtu);
532         return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(pmtu), pmtu_pl);
533 }
534
535 static int __mlxsw_sp_port_swid_set(struct mlxsw_sp *mlxsw_sp, u8 local_port,
536                                     u8 swid)
537 {
538         char pspa_pl[MLXSW_REG_PSPA_LEN];
539
540         mlxsw_reg_pspa_pack(pspa_pl, swid, local_port);
541         return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(pspa), pspa_pl);
542 }
543
544 static int mlxsw_sp_port_swid_set(struct mlxsw_sp_port *mlxsw_sp_port, u8 swid)
545 {
546         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
547
548         return __mlxsw_sp_port_swid_set(mlxsw_sp, mlxsw_sp_port->local_port,
549                                         swid);
550 }
551
552 static int mlxsw_sp_port_vp_mode_set(struct mlxsw_sp_port *mlxsw_sp_port,
553                                      bool enable)
554 {
555         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
556         char svpe_pl[MLXSW_REG_SVPE_LEN];
557
558         mlxsw_reg_svpe_pack(svpe_pl, mlxsw_sp_port->local_port, enable);
559         return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(svpe), svpe_pl);
560 }
561
562 int mlxsw_sp_port_vid_to_fid_set(struct mlxsw_sp_port *mlxsw_sp_port,
563                                  enum mlxsw_reg_svfa_mt mt, bool valid, u16 fid,
564                                  u16 vid)
565 {
566         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
567         char svfa_pl[MLXSW_REG_SVFA_LEN];
568
569         mlxsw_reg_svfa_pack(svfa_pl, mlxsw_sp_port->local_port, mt, valid,
570                             fid, vid);
571         return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(svfa), svfa_pl);
572 }
573
574 int __mlxsw_sp_port_vid_learning_set(struct mlxsw_sp_port *mlxsw_sp_port,
575                                      u16 vid_begin, u16 vid_end,
576                                      bool learn_enable)
577 {
578         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
579         char *spvmlr_pl;
580         int err;
581
582         spvmlr_pl = kmalloc(MLXSW_REG_SPVMLR_LEN, GFP_KERNEL);
583         if (!spvmlr_pl)
584                 return -ENOMEM;
585         mlxsw_reg_spvmlr_pack(spvmlr_pl, mlxsw_sp_port->local_port, vid_begin,
586                               vid_end, learn_enable);
587         err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(spvmlr), spvmlr_pl);
588         kfree(spvmlr_pl);
589         return err;
590 }
591
592 static int mlxsw_sp_port_vid_learning_set(struct mlxsw_sp_port *mlxsw_sp_port,
593                                           u16 vid, bool learn_enable)
594 {
595         return __mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid, vid,
596                                                 learn_enable);
597 }
598
599 static int
600 mlxsw_sp_port_system_port_mapping_set(struct mlxsw_sp_port *mlxsw_sp_port)
601 {
602         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
603         char sspr_pl[MLXSW_REG_SSPR_LEN];
604
605         mlxsw_reg_sspr_pack(sspr_pl, mlxsw_sp_port->local_port);
606         return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sspr), sspr_pl);
607 }
608
609 static int mlxsw_sp_port_module_info_get(struct mlxsw_sp *mlxsw_sp,
610                                          u8 local_port, u8 *p_module,
611                                          u8 *p_width, u8 *p_lane)
612 {
613         char pmlp_pl[MLXSW_REG_PMLP_LEN];
614         int err;
615
616         mlxsw_reg_pmlp_pack(pmlp_pl, local_port);
617         err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(pmlp), pmlp_pl);
618         if (err)
619                 return err;
620         *p_module = mlxsw_reg_pmlp_module_get(pmlp_pl, 0);
621         *p_width = mlxsw_reg_pmlp_width_get(pmlp_pl);
622         *p_lane = mlxsw_reg_pmlp_tx_lane_get(pmlp_pl, 0);
623         return 0;
624 }
625
626 static int mlxsw_sp_port_module_map(struct mlxsw_sp *mlxsw_sp, u8 local_port,
627                                     u8 module, u8 width, u8 lane)
628 {
629         char pmlp_pl[MLXSW_REG_PMLP_LEN];
630         int i;
631
632         mlxsw_reg_pmlp_pack(pmlp_pl, local_port);
633         mlxsw_reg_pmlp_width_set(pmlp_pl, width);
634         for (i = 0; i < width; i++) {
635                 mlxsw_reg_pmlp_module_set(pmlp_pl, i, module);
636                 mlxsw_reg_pmlp_tx_lane_set(pmlp_pl, i, lane + i);  /* Rx & Tx */
637         }
638
639         return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(pmlp), pmlp_pl);
640 }
641
642 static int mlxsw_sp_port_module_unmap(struct mlxsw_sp *mlxsw_sp, u8 local_port)
643 {
644         char pmlp_pl[MLXSW_REG_PMLP_LEN];
645
646         mlxsw_reg_pmlp_pack(pmlp_pl, local_port);
647         mlxsw_reg_pmlp_width_set(pmlp_pl, 0);
648         return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(pmlp), pmlp_pl);
649 }
650
651 static int mlxsw_sp_port_open(struct net_device *dev)
652 {
653         struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
654         int err;
655
656         err = mlxsw_sp_port_admin_status_set(mlxsw_sp_port, true);
657         if (err)
658                 return err;
659         netif_start_queue(dev);
660         return 0;
661 }
662
663 static int mlxsw_sp_port_stop(struct net_device *dev)
664 {
665         struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
666
667         netif_stop_queue(dev);
668         return mlxsw_sp_port_admin_status_set(mlxsw_sp_port, false);
669 }
670
671 static netdev_tx_t mlxsw_sp_port_xmit(struct sk_buff *skb,
672                                       struct net_device *dev)
673 {
674         struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
675         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
676         struct mlxsw_sp_port_pcpu_stats *pcpu_stats;
677         const struct mlxsw_tx_info tx_info = {
678                 .local_port = mlxsw_sp_port->local_port,
679                 .is_emad = false,
680         };
681         u64 len;
682         int err;
683
684         if (mlxsw_core_skb_transmit_busy(mlxsw_sp->core, &tx_info))
685                 return NETDEV_TX_BUSY;
686
687         if (unlikely(skb_headroom(skb) < MLXSW_TXHDR_LEN)) {
688                 struct sk_buff *skb_orig = skb;
689
690                 skb = skb_realloc_headroom(skb, MLXSW_TXHDR_LEN);
691                 if (!skb) {
692                         this_cpu_inc(mlxsw_sp_port->pcpu_stats->tx_dropped);
693                         dev_kfree_skb_any(skb_orig);
694                         return NETDEV_TX_OK;
695                 }
696                 dev_consume_skb_any(skb_orig);
697         }
698
699         if (eth_skb_pad(skb)) {
700                 this_cpu_inc(mlxsw_sp_port->pcpu_stats->tx_dropped);
701                 return NETDEV_TX_OK;
702         }
703
704         mlxsw_sp_txhdr_construct(skb, &tx_info);
705         /* TX header is consumed by HW on the way so we shouldn't count its
706          * bytes as being sent.
707          */
708         len = skb->len - MLXSW_TXHDR_LEN;
709
710         /* Due to a race we might fail here because of a full queue. In that
711          * unlikely case we simply drop the packet.
712          */
713         err = mlxsw_core_skb_transmit(mlxsw_sp->core, skb, &tx_info);
714
715         if (!err) {
716                 pcpu_stats = this_cpu_ptr(mlxsw_sp_port->pcpu_stats);
717                 u64_stats_update_begin(&pcpu_stats->syncp);
718                 pcpu_stats->tx_packets++;
719                 pcpu_stats->tx_bytes += len;
720                 u64_stats_update_end(&pcpu_stats->syncp);
721         } else {
722                 this_cpu_inc(mlxsw_sp_port->pcpu_stats->tx_dropped);
723                 dev_kfree_skb_any(skb);
724         }
725         return NETDEV_TX_OK;
726 }
727
728 static void mlxsw_sp_set_rx_mode(struct net_device *dev)
729 {
730 }
731
732 static int mlxsw_sp_port_set_mac_address(struct net_device *dev, void *p)
733 {
734         struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
735         struct sockaddr *addr = p;
736         int err;
737
738         if (!is_valid_ether_addr(addr->sa_data))
739                 return -EADDRNOTAVAIL;
740
741         err = mlxsw_sp_port_dev_addr_set(mlxsw_sp_port, addr->sa_data);
742         if (err)
743                 return err;
744         memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
745         return 0;
746 }
747
748 static void mlxsw_sp_pg_buf_pack(char *pbmc_pl, int pg_index, int mtu,
749                                  bool pause_en, bool pfc_en, u16 delay)
750 {
751         u16 pg_size = 2 * MLXSW_SP_BYTES_TO_CELLS(mtu);
752
753         delay = pfc_en ? mlxsw_sp_pfc_delay_get(mtu, delay) :
754                          MLXSW_SP_PAUSE_DELAY;
755
756         if (pause_en || pfc_en)
757                 mlxsw_reg_pbmc_lossless_buffer_pack(pbmc_pl, pg_index,
758                                                     pg_size + delay, pg_size);
759         else
760                 mlxsw_reg_pbmc_lossy_buffer_pack(pbmc_pl, pg_index, pg_size);
761 }
762
763 int __mlxsw_sp_port_headroom_set(struct mlxsw_sp_port *mlxsw_sp_port, int mtu,
764                                  u8 *prio_tc, bool pause_en,
765                                  struct ieee_pfc *my_pfc)
766 {
767         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
768         u8 pfc_en = !!my_pfc ? my_pfc->pfc_en : 0;
769         u16 delay = !!my_pfc ? my_pfc->delay : 0;
770         char pbmc_pl[MLXSW_REG_PBMC_LEN];
771         int i, j, err;
772
773         mlxsw_reg_pbmc_pack(pbmc_pl, mlxsw_sp_port->local_port, 0, 0);
774         err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(pbmc), pbmc_pl);
775         if (err)
776                 return err;
777
778         for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
779                 bool configure = false;
780                 bool pfc = false;
781
782                 for (j = 0; j < IEEE_8021QAZ_MAX_TCS; j++) {
783                         if (prio_tc[j] == i) {
784                                 pfc = pfc_en & BIT(j);
785                                 configure = true;
786                                 break;
787                         }
788                 }
789
790                 if (!configure)
791                         continue;
792                 mlxsw_sp_pg_buf_pack(pbmc_pl, i, mtu, pause_en, pfc, delay);
793         }
794
795         return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(pbmc), pbmc_pl);
796 }
797
798 static int mlxsw_sp_port_headroom_set(struct mlxsw_sp_port *mlxsw_sp_port,
799                                       int mtu, bool pause_en)
800 {
801         u8 def_prio_tc[IEEE_8021QAZ_MAX_TCS] = {0};
802         bool dcb_en = !!mlxsw_sp_port->dcb.ets;
803         struct ieee_pfc *my_pfc;
804         u8 *prio_tc;
805
806         prio_tc = dcb_en ? mlxsw_sp_port->dcb.ets->prio_tc : def_prio_tc;
807         my_pfc = dcb_en ? mlxsw_sp_port->dcb.pfc : NULL;
808
809         return __mlxsw_sp_port_headroom_set(mlxsw_sp_port, mtu, prio_tc,
810                                             pause_en, my_pfc);
811 }
812
813 static int mlxsw_sp_port_change_mtu(struct net_device *dev, int mtu)
814 {
815         struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
816         bool pause_en = mlxsw_sp_port_is_pause_en(mlxsw_sp_port);
817         int err;
818
819         err = mlxsw_sp_port_headroom_set(mlxsw_sp_port, mtu, pause_en);
820         if (err)
821                 return err;
822         err = mlxsw_sp_span_port_mtu_update(mlxsw_sp_port, mtu);
823         if (err)
824                 goto err_span_port_mtu_update;
825         err = mlxsw_sp_port_mtu_set(mlxsw_sp_port, mtu);
826         if (err)
827                 goto err_port_mtu_set;
828         dev->mtu = mtu;
829         return 0;
830
831 err_port_mtu_set:
832         mlxsw_sp_span_port_mtu_update(mlxsw_sp_port, dev->mtu);
833 err_span_port_mtu_update:
834         mlxsw_sp_port_headroom_set(mlxsw_sp_port, dev->mtu, pause_en);
835         return err;
836 }
837
838 static int
839 mlxsw_sp_port_get_sw_stats64(const struct net_device *dev,
840                              struct rtnl_link_stats64 *stats)
841 {
842         struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
843         struct mlxsw_sp_port_pcpu_stats *p;
844         u64 rx_packets, rx_bytes, tx_packets, tx_bytes;
845         u32 tx_dropped = 0;
846         unsigned int start;
847         int i;
848
849         for_each_possible_cpu(i) {
850                 p = per_cpu_ptr(mlxsw_sp_port->pcpu_stats, i);
851                 do {
852                         start = u64_stats_fetch_begin_irq(&p->syncp);
853                         rx_packets      = p->rx_packets;
854                         rx_bytes        = p->rx_bytes;
855                         tx_packets      = p->tx_packets;
856                         tx_bytes        = p->tx_bytes;
857                 } while (u64_stats_fetch_retry_irq(&p->syncp, start));
858
859                 stats->rx_packets       += rx_packets;
860                 stats->rx_bytes         += rx_bytes;
861                 stats->tx_packets       += tx_packets;
862                 stats->tx_bytes         += tx_bytes;
863                 /* tx_dropped is u32, updated without syncp protection. */
864                 tx_dropped      += p->tx_dropped;
865         }
866         stats->tx_dropped       = tx_dropped;
867         return 0;
868 }
869
870 static bool mlxsw_sp_port_has_offload_stats(const struct net_device *dev, int attr_id)
871 {
872         switch (attr_id) {
873         case IFLA_OFFLOAD_XSTATS_CPU_HIT:
874                 return true;
875         }
876
877         return false;
878 }
879
880 static int mlxsw_sp_port_get_offload_stats(int attr_id, const struct net_device *dev,
881                                            void *sp)
882 {
883         switch (attr_id) {
884         case IFLA_OFFLOAD_XSTATS_CPU_HIT:
885                 return mlxsw_sp_port_get_sw_stats64(dev, sp);
886         }
887
888         return -EINVAL;
889 }
890
891 static int mlxsw_sp_port_get_stats_raw(struct net_device *dev, int grp,
892                                        int prio, char *ppcnt_pl)
893 {
894         struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
895         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
896
897         mlxsw_reg_ppcnt_pack(ppcnt_pl, mlxsw_sp_port->local_port, grp, prio);
898         return mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(ppcnt), ppcnt_pl);
899 }
900
901 static int mlxsw_sp_port_get_hw_stats(struct net_device *dev,
902                                       struct rtnl_link_stats64 *stats)
903 {
904         char ppcnt_pl[MLXSW_REG_PPCNT_LEN];
905         int err;
906
907         err = mlxsw_sp_port_get_stats_raw(dev, MLXSW_REG_PPCNT_IEEE_8023_CNT,
908                                           0, ppcnt_pl);
909         if (err)
910                 goto out;
911
912         stats->tx_packets =
913                 mlxsw_reg_ppcnt_a_frames_transmitted_ok_get(ppcnt_pl);
914         stats->rx_packets =
915                 mlxsw_reg_ppcnt_a_frames_received_ok_get(ppcnt_pl);
916         stats->tx_bytes =
917                 mlxsw_reg_ppcnt_a_octets_transmitted_ok_get(ppcnt_pl);
918         stats->rx_bytes =
919                 mlxsw_reg_ppcnt_a_octets_received_ok_get(ppcnt_pl);
920         stats->multicast =
921                 mlxsw_reg_ppcnt_a_multicast_frames_received_ok_get(ppcnt_pl);
922
923         stats->rx_crc_errors =
924                 mlxsw_reg_ppcnt_a_frame_check_sequence_errors_get(ppcnt_pl);
925         stats->rx_frame_errors =
926                 mlxsw_reg_ppcnt_a_alignment_errors_get(ppcnt_pl);
927
928         stats->rx_length_errors = (
929                 mlxsw_reg_ppcnt_a_in_range_length_errors_get(ppcnt_pl) +
930                 mlxsw_reg_ppcnt_a_out_of_range_length_field_get(ppcnt_pl) +
931                 mlxsw_reg_ppcnt_a_frame_too_long_errors_get(ppcnt_pl));
932
933         stats->rx_errors = (stats->rx_crc_errors +
934                 stats->rx_frame_errors + stats->rx_length_errors);
935
936 out:
937         return err;
938 }
939
940 static void update_stats_cache(struct work_struct *work)
941 {
942         struct mlxsw_sp_port *mlxsw_sp_port =
943                 container_of(work, struct mlxsw_sp_port,
944                              hw_stats.update_dw.work);
945
946         if (!netif_carrier_ok(mlxsw_sp_port->dev))
947                 goto out;
948
949         mlxsw_sp_port_get_hw_stats(mlxsw_sp_port->dev,
950                                    mlxsw_sp_port->hw_stats.cache);
951
952 out:
953         mlxsw_core_schedule_dw(&mlxsw_sp_port->hw_stats.update_dw,
954                                MLXSW_HW_STATS_UPDATE_TIME);
955 }
956
957 /* Return the stats from a cache that is updated periodically,
958  * as this function might get called in an atomic context.
959  */
960 static void
961 mlxsw_sp_port_get_stats64(struct net_device *dev,
962                           struct rtnl_link_stats64 *stats)
963 {
964         struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
965
966         memcpy(stats, mlxsw_sp_port->hw_stats.cache, sizeof(*stats));
967 }
968
969 int mlxsw_sp_port_vlan_set(struct mlxsw_sp_port *mlxsw_sp_port, u16 vid_begin,
970                            u16 vid_end, bool is_member, bool untagged)
971 {
972         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
973         char *spvm_pl;
974         int err;
975
976         spvm_pl = kmalloc(MLXSW_REG_SPVM_LEN, GFP_KERNEL);
977         if (!spvm_pl)
978                 return -ENOMEM;
979
980         mlxsw_reg_spvm_pack(spvm_pl, mlxsw_sp_port->local_port, vid_begin,
981                             vid_end, is_member, untagged);
982         err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(spvm), spvm_pl);
983         kfree(spvm_pl);
984         return err;
985 }
986
987 static int mlxsw_sp_port_vp_mode_trans(struct mlxsw_sp_port *mlxsw_sp_port)
988 {
989         enum mlxsw_reg_svfa_mt mt = MLXSW_REG_SVFA_MT_PORT_VID_TO_FID;
990         u16 vid, last_visited_vid;
991         int err;
992
993         for_each_set_bit(vid, mlxsw_sp_port->active_vlans, VLAN_N_VID) {
994                 err = mlxsw_sp_port_vid_to_fid_set(mlxsw_sp_port, mt, true, vid,
995                                                    vid);
996                 if (err) {
997                         last_visited_vid = vid;
998                         goto err_port_vid_to_fid_set;
999                 }
1000         }
1001
1002         err = mlxsw_sp_port_vp_mode_set(mlxsw_sp_port, true);
1003         if (err) {
1004                 last_visited_vid = VLAN_N_VID;
1005                 goto err_port_vid_to_fid_set;
1006         }
1007
1008         return 0;
1009
1010 err_port_vid_to_fid_set:
1011         for_each_set_bit(vid, mlxsw_sp_port->active_vlans, last_visited_vid)
1012                 mlxsw_sp_port_vid_to_fid_set(mlxsw_sp_port, mt, false, vid,
1013                                              vid);
1014         return err;
1015 }
1016
1017 static int mlxsw_sp_port_vlan_mode_trans(struct mlxsw_sp_port *mlxsw_sp_port)
1018 {
1019         enum mlxsw_reg_svfa_mt mt = MLXSW_REG_SVFA_MT_PORT_VID_TO_FID;
1020         u16 vid;
1021         int err;
1022
1023         err = mlxsw_sp_port_vp_mode_set(mlxsw_sp_port, false);
1024         if (err)
1025                 return err;
1026
1027         for_each_set_bit(vid, mlxsw_sp_port->active_vlans, VLAN_N_VID) {
1028                 err = mlxsw_sp_port_vid_to_fid_set(mlxsw_sp_port, mt, false,
1029                                                    vid, vid);
1030                 if (err)
1031                         return err;
1032         }
1033
1034         return 0;
1035 }
1036
1037 static struct mlxsw_sp_port *
1038 mlxsw_sp_port_vport_create(struct mlxsw_sp_port *mlxsw_sp_port, u16 vid)
1039 {
1040         struct mlxsw_sp_port *mlxsw_sp_vport;
1041
1042         mlxsw_sp_vport = kzalloc(sizeof(*mlxsw_sp_vport), GFP_KERNEL);
1043         if (!mlxsw_sp_vport)
1044                 return NULL;
1045
1046         /* dev will be set correctly after the VLAN device is linked
1047          * with the real device. In case of bridge SELF invocation, dev
1048          * will remain as is.
1049          */
1050         mlxsw_sp_vport->dev = mlxsw_sp_port->dev;
1051         mlxsw_sp_vport->mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1052         mlxsw_sp_vport->local_port = mlxsw_sp_port->local_port;
1053         mlxsw_sp_vport->stp_state = BR_STATE_FORWARDING;
1054         mlxsw_sp_vport->lagged = mlxsw_sp_port->lagged;
1055         mlxsw_sp_vport->lag_id = mlxsw_sp_port->lag_id;
1056         mlxsw_sp_vport->vport.vid = vid;
1057
1058         list_add(&mlxsw_sp_vport->vport.list, &mlxsw_sp_port->vports_list);
1059
1060         return mlxsw_sp_vport;
1061 }
1062
1063 static void mlxsw_sp_port_vport_destroy(struct mlxsw_sp_port *mlxsw_sp_vport)
1064 {
1065         list_del(&mlxsw_sp_vport->vport.list);
1066         kfree(mlxsw_sp_vport);
1067 }
1068
1069 static int mlxsw_sp_port_add_vid(struct net_device *dev,
1070                                  __be16 __always_unused proto, u16 vid)
1071 {
1072         struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1073         struct mlxsw_sp_port *mlxsw_sp_vport;
1074         bool untagged = vid == 1;
1075         int err;
1076
1077         /* VLAN 0 is added to HW filter when device goes up, but it is
1078          * reserved in our case, so simply return.
1079          */
1080         if (!vid)
1081                 return 0;
1082
1083         if (mlxsw_sp_port_vport_find(mlxsw_sp_port, vid))
1084                 return 0;
1085
1086         mlxsw_sp_vport = mlxsw_sp_port_vport_create(mlxsw_sp_port, vid);
1087         if (!mlxsw_sp_vport)
1088                 return -ENOMEM;
1089
1090         /* When adding the first VLAN interface on a bridged port we need to
1091          * transition all the active 802.1Q bridge VLANs to use explicit
1092          * {Port, VID} to FID mappings and set the port's mode to Virtual mode.
1093          */
1094         if (list_is_singular(&mlxsw_sp_port->vports_list)) {
1095                 err = mlxsw_sp_port_vp_mode_trans(mlxsw_sp_port);
1096                 if (err)
1097                         goto err_port_vp_mode_trans;
1098         }
1099
1100         err = mlxsw_sp_port_vlan_set(mlxsw_sp_vport, vid, vid, true, untagged);
1101         if (err)
1102                 goto err_port_add_vid;
1103
1104         return 0;
1105
1106 err_port_add_vid:
1107         if (list_is_singular(&mlxsw_sp_port->vports_list))
1108                 mlxsw_sp_port_vlan_mode_trans(mlxsw_sp_port);
1109 err_port_vp_mode_trans:
1110         mlxsw_sp_port_vport_destroy(mlxsw_sp_vport);
1111         return err;
1112 }
1113
1114 static int mlxsw_sp_port_kill_vid(struct net_device *dev,
1115                                   __be16 __always_unused proto, u16 vid)
1116 {
1117         struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1118         struct mlxsw_sp_port *mlxsw_sp_vport;
1119         struct mlxsw_sp_fid *f;
1120
1121         /* VLAN 0 is removed from HW filter when device goes down, but
1122          * it is reserved in our case, so simply return.
1123          */
1124         if (!vid)
1125                 return 0;
1126
1127         mlxsw_sp_vport = mlxsw_sp_port_vport_find(mlxsw_sp_port, vid);
1128         if (WARN_ON(!mlxsw_sp_vport))
1129                 return 0;
1130
1131         mlxsw_sp_port_vlan_set(mlxsw_sp_vport, vid, vid, false, false);
1132
1133         /* Drop FID reference. If this was the last reference the
1134          * resources will be freed.
1135          */
1136         f = mlxsw_sp_vport_fid_get(mlxsw_sp_vport);
1137         if (f && !WARN_ON(!f->leave))
1138                 f->leave(mlxsw_sp_vport);
1139
1140         /* When removing the last VLAN interface on a bridged port we need to
1141          * transition all active 802.1Q bridge VLANs to use VID to FID
1142          * mappings and set port's mode to VLAN mode.
1143          */
1144         if (list_is_singular(&mlxsw_sp_port->vports_list))
1145                 mlxsw_sp_port_vlan_mode_trans(mlxsw_sp_port);
1146
1147         mlxsw_sp_port_vport_destroy(mlxsw_sp_vport);
1148
1149         return 0;
1150 }
1151
1152 static int mlxsw_sp_port_get_phys_port_name(struct net_device *dev, char *name,
1153                                             size_t len)
1154 {
1155         struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1156         u8 module = mlxsw_sp_port->mapping.module;
1157         u8 width = mlxsw_sp_port->mapping.width;
1158         u8 lane = mlxsw_sp_port->mapping.lane;
1159         int err;
1160
1161         if (!mlxsw_sp_port->split)
1162                 err = snprintf(name, len, "p%d", module + 1);
1163         else
1164                 err = snprintf(name, len, "p%ds%d", module + 1,
1165                                lane / width);
1166
1167         if (err >= len)
1168                 return -EINVAL;
1169
1170         return 0;
1171 }
1172
1173 static struct mlxsw_sp_port_mall_tc_entry *
1174 mlxsw_sp_port_mall_tc_entry_find(struct mlxsw_sp_port *port,
1175                                  unsigned long cookie) {
1176         struct mlxsw_sp_port_mall_tc_entry *mall_tc_entry;
1177
1178         list_for_each_entry(mall_tc_entry, &port->mall_tc_list, list)
1179                 if (mall_tc_entry->cookie == cookie)
1180                         return mall_tc_entry;
1181
1182         return NULL;
1183 }
1184
1185 static int
1186 mlxsw_sp_port_add_cls_matchall_mirror(struct mlxsw_sp_port *mlxsw_sp_port,
1187                                       struct mlxsw_sp_port_mall_mirror_tc_entry *mirror,
1188                                       const struct tc_action *a,
1189                                       bool ingress)
1190 {
1191         struct net *net = dev_net(mlxsw_sp_port->dev);
1192         enum mlxsw_sp_span_type span_type;
1193         struct mlxsw_sp_port *to_port;
1194         struct net_device *to_dev;
1195         int ifindex;
1196
1197         ifindex = tcf_mirred_ifindex(a);
1198         to_dev = __dev_get_by_index(net, ifindex);
1199         if (!to_dev) {
1200                 netdev_err(mlxsw_sp_port->dev, "Could not find requested device\n");
1201                 return -EINVAL;
1202         }
1203
1204         if (!mlxsw_sp_port_dev_check(to_dev)) {
1205                 netdev_err(mlxsw_sp_port->dev, "Cannot mirror to a non-spectrum port");
1206                 return -EOPNOTSUPP;
1207         }
1208         to_port = netdev_priv(to_dev);
1209
1210         mirror->to_local_port = to_port->local_port;
1211         mirror->ingress = ingress;
1212         span_type = ingress ? MLXSW_SP_SPAN_INGRESS : MLXSW_SP_SPAN_EGRESS;
1213         return mlxsw_sp_span_mirror_add(mlxsw_sp_port, to_port, span_type);
1214 }
1215
1216 static void
1217 mlxsw_sp_port_del_cls_matchall_mirror(struct mlxsw_sp_port *mlxsw_sp_port,
1218                                       struct mlxsw_sp_port_mall_mirror_tc_entry *mirror)
1219 {
1220         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1221         enum mlxsw_sp_span_type span_type;
1222         struct mlxsw_sp_port *to_port;
1223
1224         to_port = mlxsw_sp->ports[mirror->to_local_port];
1225         span_type = mirror->ingress ?
1226                         MLXSW_SP_SPAN_INGRESS : MLXSW_SP_SPAN_EGRESS;
1227         mlxsw_sp_span_mirror_remove(mlxsw_sp_port, to_port, span_type);
1228 }
1229
1230 static int
1231 mlxsw_sp_port_add_cls_matchall_sample(struct mlxsw_sp_port *mlxsw_sp_port,
1232                                       struct tc_cls_matchall_offload *cls,
1233                                       const struct tc_action *a,
1234                                       bool ingress)
1235 {
1236         int err;
1237
1238         if (!mlxsw_sp_port->sample)
1239                 return -EOPNOTSUPP;
1240         if (rtnl_dereference(mlxsw_sp_port->sample->psample_group)) {
1241                 netdev_err(mlxsw_sp_port->dev, "sample already active\n");
1242                 return -EEXIST;
1243         }
1244         if (tcf_sample_rate(a) > MLXSW_REG_MPSC_RATE_MAX) {
1245                 netdev_err(mlxsw_sp_port->dev, "sample rate not supported\n");
1246                 return -EOPNOTSUPP;
1247         }
1248
1249         rcu_assign_pointer(mlxsw_sp_port->sample->psample_group,
1250                            tcf_sample_psample_group(a));
1251         mlxsw_sp_port->sample->truncate = tcf_sample_truncate(a);
1252         mlxsw_sp_port->sample->trunc_size = tcf_sample_trunc_size(a);
1253         mlxsw_sp_port->sample->rate = tcf_sample_rate(a);
1254
1255         err = mlxsw_sp_port_sample_set(mlxsw_sp_port, true, tcf_sample_rate(a));
1256         if (err)
1257                 goto err_port_sample_set;
1258         return 0;
1259
1260 err_port_sample_set:
1261         RCU_INIT_POINTER(mlxsw_sp_port->sample->psample_group, NULL);
1262         return err;
1263 }
1264
1265 static void
1266 mlxsw_sp_port_del_cls_matchall_sample(struct mlxsw_sp_port *mlxsw_sp_port)
1267 {
1268         if (!mlxsw_sp_port->sample)
1269                 return;
1270
1271         mlxsw_sp_port_sample_set(mlxsw_sp_port, false, 1);
1272         RCU_INIT_POINTER(mlxsw_sp_port->sample->psample_group, NULL);
1273 }
1274
1275 static int mlxsw_sp_port_add_cls_matchall(struct mlxsw_sp_port *mlxsw_sp_port,
1276                                           __be16 protocol,
1277                                           struct tc_cls_matchall_offload *cls,
1278                                           bool ingress)
1279 {
1280         struct mlxsw_sp_port_mall_tc_entry *mall_tc_entry;
1281         const struct tc_action *a;
1282         LIST_HEAD(actions);
1283         int err;
1284
1285         if (!tc_single_action(cls->exts)) {
1286                 netdev_err(mlxsw_sp_port->dev, "only singular actions are supported\n");
1287                 return -EOPNOTSUPP;
1288         }
1289
1290         mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
1291         if (!mall_tc_entry)
1292                 return -ENOMEM;
1293         mall_tc_entry->cookie = cls->cookie;
1294
1295         tcf_exts_to_list(cls->exts, &actions);
1296         a = list_first_entry(&actions, struct tc_action, list);
1297
1298         if (is_tcf_mirred_egress_mirror(a) && protocol == htons(ETH_P_ALL)) {
1299                 struct mlxsw_sp_port_mall_mirror_tc_entry *mirror;
1300
1301                 mall_tc_entry->type = MLXSW_SP_PORT_MALL_MIRROR;
1302                 mirror = &mall_tc_entry->mirror;
1303                 err = mlxsw_sp_port_add_cls_matchall_mirror(mlxsw_sp_port,
1304                                                             mirror, a, ingress);
1305         } else if (is_tcf_sample(a) && protocol == htons(ETH_P_ALL)) {
1306                 mall_tc_entry->type = MLXSW_SP_PORT_MALL_SAMPLE;
1307                 err = mlxsw_sp_port_add_cls_matchall_sample(mlxsw_sp_port, cls,
1308                                                             a, ingress);
1309         } else {
1310                 err = -EOPNOTSUPP;
1311         }
1312
1313         if (err)
1314                 goto err_add_action;
1315
1316         list_add_tail(&mall_tc_entry->list, &mlxsw_sp_port->mall_tc_list);
1317         return 0;
1318
1319 err_add_action:
1320         kfree(mall_tc_entry);
1321         return err;
1322 }
1323
1324 static void mlxsw_sp_port_del_cls_matchall(struct mlxsw_sp_port *mlxsw_sp_port,
1325                                            struct tc_cls_matchall_offload *cls)
1326 {
1327         struct mlxsw_sp_port_mall_tc_entry *mall_tc_entry;
1328
1329         mall_tc_entry = mlxsw_sp_port_mall_tc_entry_find(mlxsw_sp_port,
1330                                                          cls->cookie);
1331         if (!mall_tc_entry) {
1332                 netdev_dbg(mlxsw_sp_port->dev, "tc entry not found on port\n");
1333                 return;
1334         }
1335         list_del(&mall_tc_entry->list);
1336
1337         switch (mall_tc_entry->type) {
1338         case MLXSW_SP_PORT_MALL_MIRROR:
1339                 mlxsw_sp_port_del_cls_matchall_mirror(mlxsw_sp_port,
1340                                                       &mall_tc_entry->mirror);
1341                 break;
1342         case MLXSW_SP_PORT_MALL_SAMPLE:
1343                 mlxsw_sp_port_del_cls_matchall_sample(mlxsw_sp_port);
1344                 break;
1345         default:
1346                 WARN_ON(1);
1347         }
1348
1349         kfree(mall_tc_entry);
1350 }
1351
1352 static int mlxsw_sp_setup_tc(struct net_device *dev, u32 handle,
1353                              __be16 proto, struct tc_to_netdev *tc)
1354 {
1355         struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1356         bool ingress = TC_H_MAJ(handle) == TC_H_MAJ(TC_H_INGRESS);
1357
1358         switch (tc->type) {
1359         case TC_SETUP_MATCHALL:
1360                 switch (tc->cls_mall->command) {
1361                 case TC_CLSMATCHALL_REPLACE:
1362                         return mlxsw_sp_port_add_cls_matchall(mlxsw_sp_port,
1363                                                               proto,
1364                                                               tc->cls_mall,
1365                                                               ingress);
1366                 case TC_CLSMATCHALL_DESTROY:
1367                         mlxsw_sp_port_del_cls_matchall(mlxsw_sp_port,
1368                                                        tc->cls_mall);
1369                         return 0;
1370                 default:
1371                         return -EINVAL;
1372                 }
1373         case TC_SETUP_CLSFLOWER:
1374                 switch (tc->cls_flower->command) {
1375                 case TC_CLSFLOWER_REPLACE:
1376                         return mlxsw_sp_flower_replace(mlxsw_sp_port, ingress,
1377                                                        proto, tc->cls_flower);
1378                 case TC_CLSFLOWER_DESTROY:
1379                         mlxsw_sp_flower_destroy(mlxsw_sp_port, ingress,
1380                                                 tc->cls_flower);
1381                         return 0;
1382                 default:
1383                         return -EOPNOTSUPP;
1384                 }
1385         }
1386
1387         return -EOPNOTSUPP;
1388 }
1389
1390 static const struct net_device_ops mlxsw_sp_port_netdev_ops = {
1391         .ndo_open               = mlxsw_sp_port_open,
1392         .ndo_stop               = mlxsw_sp_port_stop,
1393         .ndo_start_xmit         = mlxsw_sp_port_xmit,
1394         .ndo_setup_tc           = mlxsw_sp_setup_tc,
1395         .ndo_set_rx_mode        = mlxsw_sp_set_rx_mode,
1396         .ndo_set_mac_address    = mlxsw_sp_port_set_mac_address,
1397         .ndo_change_mtu         = mlxsw_sp_port_change_mtu,
1398         .ndo_get_stats64        = mlxsw_sp_port_get_stats64,
1399         .ndo_has_offload_stats  = mlxsw_sp_port_has_offload_stats,
1400         .ndo_get_offload_stats  = mlxsw_sp_port_get_offload_stats,
1401         .ndo_vlan_rx_add_vid    = mlxsw_sp_port_add_vid,
1402         .ndo_vlan_rx_kill_vid   = mlxsw_sp_port_kill_vid,
1403         .ndo_fdb_add            = switchdev_port_fdb_add,
1404         .ndo_fdb_del            = switchdev_port_fdb_del,
1405         .ndo_fdb_dump           = switchdev_port_fdb_dump,
1406         .ndo_bridge_setlink     = switchdev_port_bridge_setlink,
1407         .ndo_bridge_getlink     = switchdev_port_bridge_getlink,
1408         .ndo_bridge_dellink     = switchdev_port_bridge_dellink,
1409         .ndo_get_phys_port_name = mlxsw_sp_port_get_phys_port_name,
1410 };
1411
1412 static void mlxsw_sp_port_get_drvinfo(struct net_device *dev,
1413                                       struct ethtool_drvinfo *drvinfo)
1414 {
1415         struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1416         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1417
1418         strlcpy(drvinfo->driver, mlxsw_sp_driver_name, sizeof(drvinfo->driver));
1419         strlcpy(drvinfo->version, mlxsw_sp_driver_version,
1420                 sizeof(drvinfo->version));
1421         snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version),
1422                  "%d.%d.%d",
1423                  mlxsw_sp->bus_info->fw_rev.major,
1424                  mlxsw_sp->bus_info->fw_rev.minor,
1425                  mlxsw_sp->bus_info->fw_rev.subminor);
1426         strlcpy(drvinfo->bus_info, mlxsw_sp->bus_info->device_name,
1427                 sizeof(drvinfo->bus_info));
1428 }
1429
1430 static void mlxsw_sp_port_get_pauseparam(struct net_device *dev,
1431                                          struct ethtool_pauseparam *pause)
1432 {
1433         struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1434
1435         pause->rx_pause = mlxsw_sp_port->link.rx_pause;
1436         pause->tx_pause = mlxsw_sp_port->link.tx_pause;
1437 }
1438
1439 static int mlxsw_sp_port_pause_set(struct mlxsw_sp_port *mlxsw_sp_port,
1440                                    struct ethtool_pauseparam *pause)
1441 {
1442         char pfcc_pl[MLXSW_REG_PFCC_LEN];
1443
1444         mlxsw_reg_pfcc_pack(pfcc_pl, mlxsw_sp_port->local_port);
1445         mlxsw_reg_pfcc_pprx_set(pfcc_pl, pause->rx_pause);
1446         mlxsw_reg_pfcc_pptx_set(pfcc_pl, pause->tx_pause);
1447
1448         return mlxsw_reg_write(mlxsw_sp_port->mlxsw_sp->core, MLXSW_REG(pfcc),
1449                                pfcc_pl);
1450 }
1451
1452 static int mlxsw_sp_port_set_pauseparam(struct net_device *dev,
1453                                         struct ethtool_pauseparam *pause)
1454 {
1455         struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1456         bool pause_en = pause->tx_pause || pause->rx_pause;
1457         int err;
1458
1459         if (mlxsw_sp_port->dcb.pfc && mlxsw_sp_port->dcb.pfc->pfc_en) {
1460                 netdev_err(dev, "PFC already enabled on port\n");
1461                 return -EINVAL;
1462         }
1463
1464         if (pause->autoneg) {
1465                 netdev_err(dev, "PAUSE frames autonegotiation isn't supported\n");
1466                 return -EINVAL;
1467         }
1468
1469         err = mlxsw_sp_port_headroom_set(mlxsw_sp_port, dev->mtu, pause_en);
1470         if (err) {
1471                 netdev_err(dev, "Failed to configure port's headroom\n");
1472                 return err;
1473         }
1474
1475         err = mlxsw_sp_port_pause_set(mlxsw_sp_port, pause);
1476         if (err) {
1477                 netdev_err(dev, "Failed to set PAUSE parameters\n");
1478                 goto err_port_pause_configure;
1479         }
1480
1481         mlxsw_sp_port->link.rx_pause = pause->rx_pause;
1482         mlxsw_sp_port->link.tx_pause = pause->tx_pause;
1483
1484         return 0;
1485
1486 err_port_pause_configure:
1487         pause_en = mlxsw_sp_port_is_pause_en(mlxsw_sp_port);
1488         mlxsw_sp_port_headroom_set(mlxsw_sp_port, dev->mtu, pause_en);
1489         return err;
1490 }
1491
1492 struct mlxsw_sp_port_hw_stats {
1493         char str[ETH_GSTRING_LEN];
1494         u64 (*getter)(const char *payload);
1495 };
1496
1497 static struct mlxsw_sp_port_hw_stats mlxsw_sp_port_hw_stats[] = {
1498         {
1499                 .str = "a_frames_transmitted_ok",
1500                 .getter = mlxsw_reg_ppcnt_a_frames_transmitted_ok_get,
1501         },
1502         {
1503                 .str = "a_frames_received_ok",
1504                 .getter = mlxsw_reg_ppcnt_a_frames_received_ok_get,
1505         },
1506         {
1507                 .str = "a_frame_check_sequence_errors",
1508                 .getter = mlxsw_reg_ppcnt_a_frame_check_sequence_errors_get,
1509         },
1510         {
1511                 .str = "a_alignment_errors",
1512                 .getter = mlxsw_reg_ppcnt_a_alignment_errors_get,
1513         },
1514         {
1515                 .str = "a_octets_transmitted_ok",
1516                 .getter = mlxsw_reg_ppcnt_a_octets_transmitted_ok_get,
1517         },
1518         {
1519                 .str = "a_octets_received_ok",
1520                 .getter = mlxsw_reg_ppcnt_a_octets_received_ok_get,
1521         },
1522         {
1523                 .str = "a_multicast_frames_xmitted_ok",
1524                 .getter = mlxsw_reg_ppcnt_a_multicast_frames_xmitted_ok_get,
1525         },
1526         {
1527                 .str = "a_broadcast_frames_xmitted_ok",
1528                 .getter = mlxsw_reg_ppcnt_a_broadcast_frames_xmitted_ok_get,
1529         },
1530         {
1531                 .str = "a_multicast_frames_received_ok",
1532                 .getter = mlxsw_reg_ppcnt_a_multicast_frames_received_ok_get,
1533         },
1534         {
1535                 .str = "a_broadcast_frames_received_ok",
1536                 .getter = mlxsw_reg_ppcnt_a_broadcast_frames_received_ok_get,
1537         },
1538         {
1539                 .str = "a_in_range_length_errors",
1540                 .getter = mlxsw_reg_ppcnt_a_in_range_length_errors_get,
1541         },
1542         {
1543                 .str = "a_out_of_range_length_field",
1544                 .getter = mlxsw_reg_ppcnt_a_out_of_range_length_field_get,
1545         },
1546         {
1547                 .str = "a_frame_too_long_errors",
1548                 .getter = mlxsw_reg_ppcnt_a_frame_too_long_errors_get,
1549         },
1550         {
1551                 .str = "a_symbol_error_during_carrier",
1552                 .getter = mlxsw_reg_ppcnt_a_symbol_error_during_carrier_get,
1553         },
1554         {
1555                 .str = "a_mac_control_frames_transmitted",
1556                 .getter = mlxsw_reg_ppcnt_a_mac_control_frames_transmitted_get,
1557         },
1558         {
1559                 .str = "a_mac_control_frames_received",
1560                 .getter = mlxsw_reg_ppcnt_a_mac_control_frames_received_get,
1561         },
1562         {
1563                 .str = "a_unsupported_opcodes_received",
1564                 .getter = mlxsw_reg_ppcnt_a_unsupported_opcodes_received_get,
1565         },
1566         {
1567                 .str = "a_pause_mac_ctrl_frames_received",
1568                 .getter = mlxsw_reg_ppcnt_a_pause_mac_ctrl_frames_received_get,
1569         },
1570         {
1571                 .str = "a_pause_mac_ctrl_frames_xmitted",
1572                 .getter = mlxsw_reg_ppcnt_a_pause_mac_ctrl_frames_transmitted_get,
1573         },
1574 };
1575
1576 #define MLXSW_SP_PORT_HW_STATS_LEN ARRAY_SIZE(mlxsw_sp_port_hw_stats)
1577
1578 static struct mlxsw_sp_port_hw_stats mlxsw_sp_port_hw_prio_stats[] = {
1579         {
1580                 .str = "rx_octets_prio",
1581                 .getter = mlxsw_reg_ppcnt_rx_octets_get,
1582         },
1583         {
1584                 .str = "rx_frames_prio",
1585                 .getter = mlxsw_reg_ppcnt_rx_frames_get,
1586         },
1587         {
1588                 .str = "tx_octets_prio",
1589                 .getter = mlxsw_reg_ppcnt_tx_octets_get,
1590         },
1591         {
1592                 .str = "tx_frames_prio",
1593                 .getter = mlxsw_reg_ppcnt_tx_frames_get,
1594         },
1595         {
1596                 .str = "rx_pause_prio",
1597                 .getter = mlxsw_reg_ppcnt_rx_pause_get,
1598         },
1599         {
1600                 .str = "rx_pause_duration_prio",
1601                 .getter = mlxsw_reg_ppcnt_rx_pause_duration_get,
1602         },
1603         {
1604                 .str = "tx_pause_prio",
1605                 .getter = mlxsw_reg_ppcnt_tx_pause_get,
1606         },
1607         {
1608                 .str = "tx_pause_duration_prio",
1609                 .getter = mlxsw_reg_ppcnt_tx_pause_duration_get,
1610         },
1611 };
1612
1613 #define MLXSW_SP_PORT_HW_PRIO_STATS_LEN ARRAY_SIZE(mlxsw_sp_port_hw_prio_stats)
1614
1615 static u64 mlxsw_reg_ppcnt_tc_transmit_queue_bytes_get(const char *ppcnt_pl)
1616 {
1617         u64 transmit_queue = mlxsw_reg_ppcnt_tc_transmit_queue_get(ppcnt_pl);
1618
1619         return MLXSW_SP_CELLS_TO_BYTES(transmit_queue);
1620 }
1621
1622 static struct mlxsw_sp_port_hw_stats mlxsw_sp_port_hw_tc_stats[] = {
1623         {
1624                 .str = "tc_transmit_queue_tc",
1625                 .getter = mlxsw_reg_ppcnt_tc_transmit_queue_bytes_get,
1626         },
1627         {
1628                 .str = "tc_no_buffer_discard_uc_tc",
1629                 .getter = mlxsw_reg_ppcnt_tc_no_buffer_discard_uc_get,
1630         },
1631 };
1632
1633 #define MLXSW_SP_PORT_HW_TC_STATS_LEN ARRAY_SIZE(mlxsw_sp_port_hw_tc_stats)
1634
1635 #define MLXSW_SP_PORT_ETHTOOL_STATS_LEN (MLXSW_SP_PORT_HW_STATS_LEN + \
1636                                          (MLXSW_SP_PORT_HW_PRIO_STATS_LEN + \
1637                                           MLXSW_SP_PORT_HW_TC_STATS_LEN) * \
1638                                          IEEE_8021QAZ_MAX_TCS)
1639
1640 static void mlxsw_sp_port_get_prio_strings(u8 **p, int prio)
1641 {
1642         int i;
1643
1644         for (i = 0; i < MLXSW_SP_PORT_HW_PRIO_STATS_LEN; i++) {
1645                 snprintf(*p, ETH_GSTRING_LEN, "%s_%d",
1646                          mlxsw_sp_port_hw_prio_stats[i].str, prio);
1647                 *p += ETH_GSTRING_LEN;
1648         }
1649 }
1650
1651 static void mlxsw_sp_port_get_tc_strings(u8 **p, int tc)
1652 {
1653         int i;
1654
1655         for (i = 0; i < MLXSW_SP_PORT_HW_TC_STATS_LEN; i++) {
1656                 snprintf(*p, ETH_GSTRING_LEN, "%s_%d",
1657                          mlxsw_sp_port_hw_tc_stats[i].str, tc);
1658                 *p += ETH_GSTRING_LEN;
1659         }
1660 }
1661
1662 static void mlxsw_sp_port_get_strings(struct net_device *dev,
1663                                       u32 stringset, u8 *data)
1664 {
1665         u8 *p = data;
1666         int i;
1667
1668         switch (stringset) {
1669         case ETH_SS_STATS:
1670                 for (i = 0; i < MLXSW_SP_PORT_HW_STATS_LEN; i++) {
1671                         memcpy(p, mlxsw_sp_port_hw_stats[i].str,
1672                                ETH_GSTRING_LEN);
1673                         p += ETH_GSTRING_LEN;
1674                 }
1675
1676                 for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++)
1677                         mlxsw_sp_port_get_prio_strings(&p, i);
1678
1679                 for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++)
1680                         mlxsw_sp_port_get_tc_strings(&p, i);
1681
1682                 break;
1683         }
1684 }
1685
1686 static int mlxsw_sp_port_set_phys_id(struct net_device *dev,
1687                                      enum ethtool_phys_id_state state)
1688 {
1689         struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1690         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1691         char mlcr_pl[MLXSW_REG_MLCR_LEN];
1692         bool active;
1693
1694         switch (state) {
1695         case ETHTOOL_ID_ACTIVE:
1696                 active = true;
1697                 break;
1698         case ETHTOOL_ID_INACTIVE:
1699                 active = false;
1700                 break;
1701         default:
1702                 return -EOPNOTSUPP;
1703         }
1704
1705         mlxsw_reg_mlcr_pack(mlcr_pl, mlxsw_sp_port->local_port, active);
1706         return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(mlcr), mlcr_pl);
1707 }
1708
1709 static int
1710 mlxsw_sp_get_hw_stats_by_group(struct mlxsw_sp_port_hw_stats **p_hw_stats,
1711                                int *p_len, enum mlxsw_reg_ppcnt_grp grp)
1712 {
1713         switch (grp) {
1714         case  MLXSW_REG_PPCNT_IEEE_8023_CNT:
1715                 *p_hw_stats = mlxsw_sp_port_hw_stats;
1716                 *p_len = MLXSW_SP_PORT_HW_STATS_LEN;
1717                 break;
1718         case MLXSW_REG_PPCNT_PRIO_CNT:
1719                 *p_hw_stats = mlxsw_sp_port_hw_prio_stats;
1720                 *p_len = MLXSW_SP_PORT_HW_PRIO_STATS_LEN;
1721                 break;
1722         case MLXSW_REG_PPCNT_TC_CNT:
1723                 *p_hw_stats = mlxsw_sp_port_hw_tc_stats;
1724                 *p_len = MLXSW_SP_PORT_HW_TC_STATS_LEN;
1725                 break;
1726         default:
1727                 WARN_ON(1);
1728                 return -EOPNOTSUPP;
1729         }
1730         return 0;
1731 }
1732
1733 static void __mlxsw_sp_port_get_stats(struct net_device *dev,
1734                                       enum mlxsw_reg_ppcnt_grp grp, int prio,
1735                                       u64 *data, int data_index)
1736 {
1737         struct mlxsw_sp_port_hw_stats *hw_stats;
1738         char ppcnt_pl[MLXSW_REG_PPCNT_LEN];
1739         int i, len;
1740         int err;
1741
1742         err = mlxsw_sp_get_hw_stats_by_group(&hw_stats, &len, grp);
1743         if (err)
1744                 return;
1745         mlxsw_sp_port_get_stats_raw(dev, grp, prio, ppcnt_pl);
1746         for (i = 0; i < len; i++)
1747                 data[data_index + i] = hw_stats[i].getter(ppcnt_pl);
1748 }
1749
1750 static void mlxsw_sp_port_get_stats(struct net_device *dev,
1751                                     struct ethtool_stats *stats, u64 *data)
1752 {
1753         int i, data_index = 0;
1754
1755         /* IEEE 802.3 Counters */
1756         __mlxsw_sp_port_get_stats(dev, MLXSW_REG_PPCNT_IEEE_8023_CNT, 0,
1757                                   data, data_index);
1758         data_index = MLXSW_SP_PORT_HW_STATS_LEN;
1759
1760         /* Per-Priority Counters */
1761         for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
1762                 __mlxsw_sp_port_get_stats(dev, MLXSW_REG_PPCNT_PRIO_CNT, i,
1763                                           data, data_index);
1764                 data_index += MLXSW_SP_PORT_HW_PRIO_STATS_LEN;
1765         }
1766
1767         /* Per-TC Counters */
1768         for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
1769                 __mlxsw_sp_port_get_stats(dev, MLXSW_REG_PPCNT_TC_CNT, i,
1770                                           data, data_index);
1771                 data_index += MLXSW_SP_PORT_HW_TC_STATS_LEN;
1772         }
1773 }
1774
1775 static int mlxsw_sp_port_get_sset_count(struct net_device *dev, int sset)
1776 {
1777         switch (sset) {
1778         case ETH_SS_STATS:
1779                 return MLXSW_SP_PORT_ETHTOOL_STATS_LEN;
1780         default:
1781                 return -EOPNOTSUPP;
1782         }
1783 }
1784
1785 struct mlxsw_sp_port_link_mode {
1786         enum ethtool_link_mode_bit_indices mask_ethtool;
1787         u32 mask;
1788         u32 speed;
1789 };
1790
1791 static const struct mlxsw_sp_port_link_mode mlxsw_sp_port_link_mode[] = {
1792         {
1793                 .mask           = MLXSW_REG_PTYS_ETH_SPEED_100BASE_T,
1794                 .mask_ethtool   = ETHTOOL_LINK_MODE_100baseT_Full_BIT,
1795                 .speed          = SPEED_100,
1796         },
1797         {
1798                 .mask           = MLXSW_REG_PTYS_ETH_SPEED_SGMII |
1799                                   MLXSW_REG_PTYS_ETH_SPEED_1000BASE_KX,
1800                 .mask_ethtool   = ETHTOOL_LINK_MODE_1000baseKX_Full_BIT,
1801                 .speed          = SPEED_1000,
1802         },
1803         {
1804                 .mask           = MLXSW_REG_PTYS_ETH_SPEED_10GBASE_T,
1805                 .mask_ethtool   = ETHTOOL_LINK_MODE_10000baseT_Full_BIT,
1806                 .speed          = SPEED_10000,
1807         },
1808         {
1809                 .mask           = MLXSW_REG_PTYS_ETH_SPEED_10GBASE_CX4 |
1810                                   MLXSW_REG_PTYS_ETH_SPEED_10GBASE_KX4,
1811                 .mask_ethtool   = ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT,
1812                 .speed          = SPEED_10000,
1813         },
1814         {
1815                 .mask           = MLXSW_REG_PTYS_ETH_SPEED_10GBASE_KR |
1816                                   MLXSW_REG_PTYS_ETH_SPEED_10GBASE_CR |
1817                                   MLXSW_REG_PTYS_ETH_SPEED_10GBASE_SR |
1818                                   MLXSW_REG_PTYS_ETH_SPEED_10GBASE_ER_LR,
1819                 .mask_ethtool   = ETHTOOL_LINK_MODE_10000baseKR_Full_BIT,
1820                 .speed          = SPEED_10000,
1821         },
1822         {
1823                 .mask           = MLXSW_REG_PTYS_ETH_SPEED_20GBASE_KR2,
1824                 .mask_ethtool   = ETHTOOL_LINK_MODE_20000baseKR2_Full_BIT,
1825                 .speed          = SPEED_20000,
1826         },
1827         {
1828                 .mask           = MLXSW_REG_PTYS_ETH_SPEED_40GBASE_CR4,
1829                 .mask_ethtool   = ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT,
1830                 .speed          = SPEED_40000,
1831         },
1832         {
1833                 .mask           = MLXSW_REG_PTYS_ETH_SPEED_40GBASE_KR4,
1834                 .mask_ethtool   = ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT,
1835                 .speed          = SPEED_40000,
1836         },
1837         {
1838                 .mask           = MLXSW_REG_PTYS_ETH_SPEED_40GBASE_SR4,
1839                 .mask_ethtool   = ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT,
1840                 .speed          = SPEED_40000,
1841         },
1842         {
1843                 .mask           = MLXSW_REG_PTYS_ETH_SPEED_40GBASE_LR4_ER4,
1844                 .mask_ethtool   = ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT,
1845                 .speed          = SPEED_40000,
1846         },
1847         {
1848                 .mask           = MLXSW_REG_PTYS_ETH_SPEED_25GBASE_CR,
1849                 .mask_ethtool   = ETHTOOL_LINK_MODE_25000baseCR_Full_BIT,
1850                 .speed          = SPEED_25000,
1851         },
1852         {
1853                 .mask           = MLXSW_REG_PTYS_ETH_SPEED_25GBASE_KR,
1854                 .mask_ethtool   = ETHTOOL_LINK_MODE_25000baseKR_Full_BIT,
1855                 .speed          = SPEED_25000,
1856         },
1857         {
1858                 .mask           = MLXSW_REG_PTYS_ETH_SPEED_25GBASE_SR,
1859                 .mask_ethtool   = ETHTOOL_LINK_MODE_25000baseSR_Full_BIT,
1860                 .speed          = SPEED_25000,
1861         },
1862         {
1863                 .mask           = MLXSW_REG_PTYS_ETH_SPEED_25GBASE_SR,
1864                 .mask_ethtool   = ETHTOOL_LINK_MODE_25000baseSR_Full_BIT,
1865                 .speed          = SPEED_25000,
1866         },
1867         {
1868                 .mask           = MLXSW_REG_PTYS_ETH_SPEED_50GBASE_CR2,
1869                 .mask_ethtool   = ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT,
1870                 .speed          = SPEED_50000,
1871         },
1872         {
1873                 .mask           = MLXSW_REG_PTYS_ETH_SPEED_50GBASE_KR2,
1874                 .mask_ethtool   = ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT,
1875                 .speed          = SPEED_50000,
1876         },
1877         {
1878                 .mask           = MLXSW_REG_PTYS_ETH_SPEED_50GBASE_SR2,
1879                 .mask_ethtool   = ETHTOOL_LINK_MODE_50000baseSR2_Full_BIT,
1880                 .speed          = SPEED_50000,
1881         },
1882         {
1883                 .mask           = MLXSW_REG_PTYS_ETH_SPEED_56GBASE_R4,
1884                 .mask_ethtool   = ETHTOOL_LINK_MODE_56000baseKR4_Full_BIT,
1885                 .speed          = SPEED_56000,
1886         },
1887         {
1888                 .mask           = MLXSW_REG_PTYS_ETH_SPEED_56GBASE_R4,
1889                 .mask_ethtool   = ETHTOOL_LINK_MODE_56000baseCR4_Full_BIT,
1890                 .speed          = SPEED_56000,
1891         },
1892         {
1893                 .mask           = MLXSW_REG_PTYS_ETH_SPEED_56GBASE_R4,
1894                 .mask_ethtool   = ETHTOOL_LINK_MODE_56000baseSR4_Full_BIT,
1895                 .speed          = SPEED_56000,
1896         },
1897         {
1898                 .mask           = MLXSW_REG_PTYS_ETH_SPEED_56GBASE_R4,
1899                 .mask_ethtool   = ETHTOOL_LINK_MODE_56000baseLR4_Full_BIT,
1900                 .speed          = SPEED_56000,
1901         },
1902         {
1903                 .mask           = MLXSW_REG_PTYS_ETH_SPEED_100GBASE_CR4,
1904                 .mask_ethtool   = ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT,
1905                 .speed          = SPEED_100000,
1906         },
1907         {
1908                 .mask           = MLXSW_REG_PTYS_ETH_SPEED_100GBASE_SR4,
1909                 .mask_ethtool   = ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT,
1910                 .speed          = SPEED_100000,
1911         },
1912         {
1913                 .mask           = MLXSW_REG_PTYS_ETH_SPEED_100GBASE_KR4,
1914                 .mask_ethtool   = ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT,
1915                 .speed          = SPEED_100000,
1916         },
1917         {
1918                 .mask           = MLXSW_REG_PTYS_ETH_SPEED_100GBASE_LR4_ER4,
1919                 .mask_ethtool   = ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT,
1920                 .speed          = SPEED_100000,
1921         },
1922 };
1923
1924 #define MLXSW_SP_PORT_LINK_MODE_LEN ARRAY_SIZE(mlxsw_sp_port_link_mode)
1925
1926 static void
1927 mlxsw_sp_from_ptys_supported_port(u32 ptys_eth_proto,
1928                                   struct ethtool_link_ksettings *cmd)
1929 {
1930         if (ptys_eth_proto & (MLXSW_REG_PTYS_ETH_SPEED_10GBASE_CR |
1931                               MLXSW_REG_PTYS_ETH_SPEED_10GBASE_SR |
1932                               MLXSW_REG_PTYS_ETH_SPEED_40GBASE_CR4 |
1933                               MLXSW_REG_PTYS_ETH_SPEED_40GBASE_SR4 |
1934                               MLXSW_REG_PTYS_ETH_SPEED_100GBASE_SR4 |
1935                               MLXSW_REG_PTYS_ETH_SPEED_SGMII))
1936                 ethtool_link_ksettings_add_link_mode(cmd, supported, FIBRE);
1937
1938         if (ptys_eth_proto & (MLXSW_REG_PTYS_ETH_SPEED_10GBASE_KR |
1939                               MLXSW_REG_PTYS_ETH_SPEED_10GBASE_KX4 |
1940                               MLXSW_REG_PTYS_ETH_SPEED_40GBASE_KR4 |
1941                               MLXSW_REG_PTYS_ETH_SPEED_100GBASE_KR4 |
1942                               MLXSW_REG_PTYS_ETH_SPEED_1000BASE_KX))
1943                 ethtool_link_ksettings_add_link_mode(cmd, supported, Backplane);
1944 }
1945
1946 static void mlxsw_sp_from_ptys_link(u32 ptys_eth_proto, unsigned long *mode)
1947 {
1948         int i;
1949
1950         for (i = 0; i < MLXSW_SP_PORT_LINK_MODE_LEN; i++) {
1951                 if (ptys_eth_proto & mlxsw_sp_port_link_mode[i].mask)
1952                         __set_bit(mlxsw_sp_port_link_mode[i].mask_ethtool,
1953                                   mode);
1954         }
1955 }
1956
1957 static void mlxsw_sp_from_ptys_speed_duplex(bool carrier_ok, u32 ptys_eth_proto,
1958                                             struct ethtool_link_ksettings *cmd)
1959 {
1960         u32 speed = SPEED_UNKNOWN;
1961         u8 duplex = DUPLEX_UNKNOWN;
1962         int i;
1963
1964         if (!carrier_ok)
1965                 goto out;
1966
1967         for (i = 0; i < MLXSW_SP_PORT_LINK_MODE_LEN; i++) {
1968                 if (ptys_eth_proto & mlxsw_sp_port_link_mode[i].mask) {
1969                         speed = mlxsw_sp_port_link_mode[i].speed;
1970                         duplex = DUPLEX_FULL;
1971                         break;
1972                 }
1973         }
1974 out:
1975         cmd->base.speed = speed;
1976         cmd->base.duplex = duplex;
1977 }
1978
1979 static u8 mlxsw_sp_port_connector_port(u32 ptys_eth_proto)
1980 {
1981         if (ptys_eth_proto & (MLXSW_REG_PTYS_ETH_SPEED_10GBASE_SR |
1982                               MLXSW_REG_PTYS_ETH_SPEED_40GBASE_SR4 |
1983                               MLXSW_REG_PTYS_ETH_SPEED_100GBASE_SR4 |
1984                               MLXSW_REG_PTYS_ETH_SPEED_SGMII))
1985                 return PORT_FIBRE;
1986
1987         if (ptys_eth_proto & (MLXSW_REG_PTYS_ETH_SPEED_10GBASE_CR |
1988                               MLXSW_REG_PTYS_ETH_SPEED_40GBASE_CR4 |
1989                               MLXSW_REG_PTYS_ETH_SPEED_100GBASE_CR4))
1990                 return PORT_DA;
1991
1992         if (ptys_eth_proto & (MLXSW_REG_PTYS_ETH_SPEED_10GBASE_KR |
1993                               MLXSW_REG_PTYS_ETH_SPEED_10GBASE_KX4 |
1994                               MLXSW_REG_PTYS_ETH_SPEED_40GBASE_KR4 |
1995                               MLXSW_REG_PTYS_ETH_SPEED_100GBASE_KR4))
1996                 return PORT_NONE;
1997
1998         return PORT_OTHER;
1999 }
2000
2001 static u32
2002 mlxsw_sp_to_ptys_advert_link(const struct ethtool_link_ksettings *cmd)
2003 {
2004         u32 ptys_proto = 0;
2005         int i;
2006
2007         for (i = 0; i < MLXSW_SP_PORT_LINK_MODE_LEN; i++) {
2008                 if (test_bit(mlxsw_sp_port_link_mode[i].mask_ethtool,
2009                              cmd->link_modes.advertising))
2010                         ptys_proto |= mlxsw_sp_port_link_mode[i].mask;
2011         }
2012         return ptys_proto;
2013 }
2014
2015 static u32 mlxsw_sp_to_ptys_speed(u32 speed)
2016 {
2017         u32 ptys_proto = 0;
2018         int i;
2019
2020         for (i = 0; i < MLXSW_SP_PORT_LINK_MODE_LEN; i++) {
2021                 if (speed == mlxsw_sp_port_link_mode[i].speed)
2022                         ptys_proto |= mlxsw_sp_port_link_mode[i].mask;
2023         }
2024         return ptys_proto;
2025 }
2026
2027 static u32 mlxsw_sp_to_ptys_upper_speed(u32 upper_speed)
2028 {
2029         u32 ptys_proto = 0;
2030         int i;
2031
2032         for (i = 0; i < MLXSW_SP_PORT_LINK_MODE_LEN; i++) {
2033                 if (mlxsw_sp_port_link_mode[i].speed <= upper_speed)
2034                         ptys_proto |= mlxsw_sp_port_link_mode[i].mask;
2035         }
2036         return ptys_proto;
2037 }
2038
2039 static void mlxsw_sp_port_get_link_supported(u32 eth_proto_cap,
2040                                              struct ethtool_link_ksettings *cmd)
2041 {
2042         ethtool_link_ksettings_add_link_mode(cmd, supported, Asym_Pause);
2043         ethtool_link_ksettings_add_link_mode(cmd, supported, Autoneg);
2044         ethtool_link_ksettings_add_link_mode(cmd, supported, Pause);
2045
2046         mlxsw_sp_from_ptys_supported_port(eth_proto_cap, cmd);
2047         mlxsw_sp_from_ptys_link(eth_proto_cap, cmd->link_modes.supported);
2048 }
2049
2050 static void mlxsw_sp_port_get_link_advertise(u32 eth_proto_admin, bool autoneg,
2051                                              struct ethtool_link_ksettings *cmd)
2052 {
2053         if (!autoneg)
2054                 return;
2055
2056         ethtool_link_ksettings_add_link_mode(cmd, advertising, Autoneg);
2057         mlxsw_sp_from_ptys_link(eth_proto_admin, cmd->link_modes.advertising);
2058 }
2059
2060 static void
2061 mlxsw_sp_port_get_link_lp_advertise(u32 eth_proto_lp, u8 autoneg_status,
2062                                     struct ethtool_link_ksettings *cmd)
2063 {
2064         if (autoneg_status != MLXSW_REG_PTYS_AN_STATUS_OK || !eth_proto_lp)
2065                 return;
2066
2067         ethtool_link_ksettings_add_link_mode(cmd, lp_advertising, Autoneg);
2068         mlxsw_sp_from_ptys_link(eth_proto_lp, cmd->link_modes.lp_advertising);
2069 }
2070
2071 static int mlxsw_sp_port_get_link_ksettings(struct net_device *dev,
2072                                             struct ethtool_link_ksettings *cmd)
2073 {
2074         u32 eth_proto_cap, eth_proto_admin, eth_proto_oper, eth_proto_lp;
2075         struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
2076         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
2077         char ptys_pl[MLXSW_REG_PTYS_LEN];
2078         u8 autoneg_status;
2079         bool autoneg;
2080         int err;
2081
2082         autoneg = mlxsw_sp_port->link.autoneg;
2083         mlxsw_reg_ptys_eth_pack(ptys_pl, mlxsw_sp_port->local_port, 0);
2084         err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(ptys), ptys_pl);
2085         if (err)
2086                 return err;
2087         mlxsw_reg_ptys_eth_unpack(ptys_pl, &eth_proto_cap, &eth_proto_admin,
2088                                   &eth_proto_oper);
2089
2090         mlxsw_sp_port_get_link_supported(eth_proto_cap, cmd);
2091
2092         mlxsw_sp_port_get_link_advertise(eth_proto_admin, autoneg, cmd);
2093
2094         eth_proto_lp = mlxsw_reg_ptys_eth_proto_lp_advertise_get(ptys_pl);
2095         autoneg_status = mlxsw_reg_ptys_an_status_get(ptys_pl);
2096         mlxsw_sp_port_get_link_lp_advertise(eth_proto_lp, autoneg_status, cmd);
2097
2098         cmd->base.autoneg = autoneg ? AUTONEG_ENABLE : AUTONEG_DISABLE;
2099         cmd->base.port = mlxsw_sp_port_connector_port(eth_proto_oper);
2100         mlxsw_sp_from_ptys_speed_duplex(netif_carrier_ok(dev), eth_proto_oper,
2101                                         cmd);
2102
2103         return 0;
2104 }
2105
2106 static int
2107 mlxsw_sp_port_set_link_ksettings(struct net_device *dev,
2108                                  const struct ethtool_link_ksettings *cmd)
2109 {
2110         struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
2111         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
2112         char ptys_pl[MLXSW_REG_PTYS_LEN];
2113         u32 eth_proto_cap, eth_proto_new;
2114         bool autoneg;
2115         int err;
2116
2117         mlxsw_reg_ptys_eth_pack(ptys_pl, mlxsw_sp_port->local_port, 0);
2118         err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(ptys), ptys_pl);
2119         if (err)
2120                 return err;
2121         mlxsw_reg_ptys_eth_unpack(ptys_pl, &eth_proto_cap, NULL, NULL);
2122
2123         autoneg = cmd->base.autoneg == AUTONEG_ENABLE;
2124         eth_proto_new = autoneg ?
2125                 mlxsw_sp_to_ptys_advert_link(cmd) :
2126                 mlxsw_sp_to_ptys_speed(cmd->base.speed);
2127
2128         eth_proto_new = eth_proto_new & eth_proto_cap;
2129         if (!eth_proto_new) {
2130                 netdev_err(dev, "No supported speed requested\n");
2131                 return -EINVAL;
2132         }
2133
2134         mlxsw_reg_ptys_eth_pack(ptys_pl, mlxsw_sp_port->local_port,
2135                                 eth_proto_new);
2136         err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ptys), ptys_pl);
2137         if (err)
2138                 return err;
2139
2140         if (!netif_running(dev))
2141                 return 0;
2142
2143         mlxsw_sp_port->link.autoneg = autoneg;
2144
2145         mlxsw_sp_port_admin_status_set(mlxsw_sp_port, false);
2146         mlxsw_sp_port_admin_status_set(mlxsw_sp_port, true);
2147
2148         return 0;
2149 }
2150
2151 static const struct ethtool_ops mlxsw_sp_port_ethtool_ops = {
2152         .get_drvinfo            = mlxsw_sp_port_get_drvinfo,
2153         .get_link               = ethtool_op_get_link,
2154         .get_pauseparam         = mlxsw_sp_port_get_pauseparam,
2155         .set_pauseparam         = mlxsw_sp_port_set_pauseparam,
2156         .get_strings            = mlxsw_sp_port_get_strings,
2157         .set_phys_id            = mlxsw_sp_port_set_phys_id,
2158         .get_ethtool_stats      = mlxsw_sp_port_get_stats,
2159         .get_sset_count         = mlxsw_sp_port_get_sset_count,
2160         .get_link_ksettings     = mlxsw_sp_port_get_link_ksettings,
2161         .set_link_ksettings     = mlxsw_sp_port_set_link_ksettings,
2162 };
2163
2164 static int
2165 mlxsw_sp_port_speed_by_width_set(struct mlxsw_sp_port *mlxsw_sp_port, u8 width)
2166 {
2167         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
2168         u32 upper_speed = MLXSW_SP_PORT_BASE_SPEED * width;
2169         char ptys_pl[MLXSW_REG_PTYS_LEN];
2170         u32 eth_proto_admin;
2171
2172         eth_proto_admin = mlxsw_sp_to_ptys_upper_speed(upper_speed);
2173         mlxsw_reg_ptys_eth_pack(ptys_pl, mlxsw_sp_port->local_port,
2174                                 eth_proto_admin);
2175         return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ptys), ptys_pl);
2176 }
2177
2178 int mlxsw_sp_port_ets_set(struct mlxsw_sp_port *mlxsw_sp_port,
2179                           enum mlxsw_reg_qeec_hr hr, u8 index, u8 next_index,
2180                           bool dwrr, u8 dwrr_weight)
2181 {
2182         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
2183         char qeec_pl[MLXSW_REG_QEEC_LEN];
2184
2185         mlxsw_reg_qeec_pack(qeec_pl, mlxsw_sp_port->local_port, hr, index,
2186                             next_index);
2187         mlxsw_reg_qeec_de_set(qeec_pl, true);
2188         mlxsw_reg_qeec_dwrr_set(qeec_pl, dwrr);
2189         mlxsw_reg_qeec_dwrr_weight_set(qeec_pl, dwrr_weight);
2190         return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(qeec), qeec_pl);
2191 }
2192
2193 int mlxsw_sp_port_ets_maxrate_set(struct mlxsw_sp_port *mlxsw_sp_port,
2194                                   enum mlxsw_reg_qeec_hr hr, u8 index,
2195                                   u8 next_index, u32 maxrate)
2196 {
2197         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
2198         char qeec_pl[MLXSW_REG_QEEC_LEN];
2199
2200         mlxsw_reg_qeec_pack(qeec_pl, mlxsw_sp_port->local_port, hr, index,
2201                             next_index);
2202         mlxsw_reg_qeec_mase_set(qeec_pl, true);
2203         mlxsw_reg_qeec_max_shaper_rate_set(qeec_pl, maxrate);
2204         return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(qeec), qeec_pl);
2205 }
2206
2207 int mlxsw_sp_port_prio_tc_set(struct mlxsw_sp_port *mlxsw_sp_port,
2208                               u8 switch_prio, u8 tclass)
2209 {
2210         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
2211         char qtct_pl[MLXSW_REG_QTCT_LEN];
2212
2213         mlxsw_reg_qtct_pack(qtct_pl, mlxsw_sp_port->local_port, switch_prio,
2214                             tclass);
2215         return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(qtct), qtct_pl);
2216 }
2217
2218 static int mlxsw_sp_port_ets_init(struct mlxsw_sp_port *mlxsw_sp_port)
2219 {
2220         int err, i;
2221
2222         /* Setup the elements hierarcy, so that each TC is linked to
2223          * one subgroup, which are all member in the same group.
2224          */
2225         err = mlxsw_sp_port_ets_set(mlxsw_sp_port,
2226                                     MLXSW_REG_QEEC_HIERARCY_GROUP, 0, 0, false,
2227                                     0);
2228         if (err)
2229                 return err;
2230         for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
2231                 err = mlxsw_sp_port_ets_set(mlxsw_sp_port,
2232                                             MLXSW_REG_QEEC_HIERARCY_SUBGROUP, i,
2233                                             0, false, 0);
2234                 if (err)
2235                         return err;
2236         }
2237         for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
2238                 err = mlxsw_sp_port_ets_set(mlxsw_sp_port,
2239                                             MLXSW_REG_QEEC_HIERARCY_TC, i, i,
2240                                             false, 0);
2241                 if (err)
2242                         return err;
2243         }
2244
2245         /* Make sure the max shaper is disabled in all hierarcies that
2246          * support it.
2247          */
2248         err = mlxsw_sp_port_ets_maxrate_set(mlxsw_sp_port,
2249                                             MLXSW_REG_QEEC_HIERARCY_PORT, 0, 0,
2250                                             MLXSW_REG_QEEC_MAS_DIS);
2251         if (err)
2252                 return err;
2253         for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
2254                 err = mlxsw_sp_port_ets_maxrate_set(mlxsw_sp_port,
2255                                                     MLXSW_REG_QEEC_HIERARCY_SUBGROUP,
2256                                                     i, 0,
2257                                                     MLXSW_REG_QEEC_MAS_DIS);
2258                 if (err)
2259                         return err;
2260         }
2261         for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
2262                 err = mlxsw_sp_port_ets_maxrate_set(mlxsw_sp_port,
2263                                                     MLXSW_REG_QEEC_HIERARCY_TC,
2264                                                     i, i,
2265                                                     MLXSW_REG_QEEC_MAS_DIS);
2266                 if (err)
2267                         return err;
2268         }
2269
2270         /* Map all priorities to traffic class 0. */
2271         for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
2272                 err = mlxsw_sp_port_prio_tc_set(mlxsw_sp_port, i, 0);
2273                 if (err)
2274                         return err;
2275         }
2276
2277         return 0;
2278 }
2279
2280 static int mlxsw_sp_port_pvid_vport_create(struct mlxsw_sp_port *mlxsw_sp_port)
2281 {
2282         mlxsw_sp_port->pvid = 1;
2283
2284         return mlxsw_sp_port_add_vid(mlxsw_sp_port->dev, 0, 1);
2285 }
2286
2287 static int mlxsw_sp_port_pvid_vport_destroy(struct mlxsw_sp_port *mlxsw_sp_port)
2288 {
2289         return mlxsw_sp_port_kill_vid(mlxsw_sp_port->dev, 0, 1);
2290 }
2291
2292 static int __mlxsw_sp_port_create(struct mlxsw_sp *mlxsw_sp, u8 local_port,
2293                                   bool split, u8 module, u8 width, u8 lane)
2294 {
2295         struct mlxsw_sp_port *mlxsw_sp_port;
2296         struct net_device *dev;
2297         size_t bytes;
2298         int err;
2299
2300         dev = alloc_etherdev(sizeof(struct mlxsw_sp_port));
2301         if (!dev)
2302                 return -ENOMEM;
2303         SET_NETDEV_DEV(dev, mlxsw_sp->bus_info->dev);
2304         mlxsw_sp_port = netdev_priv(dev);
2305         mlxsw_sp_port->dev = dev;
2306         mlxsw_sp_port->mlxsw_sp = mlxsw_sp;
2307         mlxsw_sp_port->local_port = local_port;
2308         mlxsw_sp_port->split = split;
2309         mlxsw_sp_port->mapping.module = module;
2310         mlxsw_sp_port->mapping.width = width;
2311         mlxsw_sp_port->mapping.lane = lane;
2312         mlxsw_sp_port->link.autoneg = 1;
2313         bytes = DIV_ROUND_UP(VLAN_N_VID, BITS_PER_BYTE);
2314         mlxsw_sp_port->active_vlans = kzalloc(bytes, GFP_KERNEL);
2315         if (!mlxsw_sp_port->active_vlans) {
2316                 err = -ENOMEM;
2317                 goto err_port_active_vlans_alloc;
2318         }
2319         mlxsw_sp_port->untagged_vlans = kzalloc(bytes, GFP_KERNEL);
2320         if (!mlxsw_sp_port->untagged_vlans) {
2321                 err = -ENOMEM;
2322                 goto err_port_untagged_vlans_alloc;
2323         }
2324         INIT_LIST_HEAD(&mlxsw_sp_port->vports_list);
2325         INIT_LIST_HEAD(&mlxsw_sp_port->mall_tc_list);
2326
2327         mlxsw_sp_port->pcpu_stats =
2328                 netdev_alloc_pcpu_stats(struct mlxsw_sp_port_pcpu_stats);
2329         if (!mlxsw_sp_port->pcpu_stats) {
2330                 err = -ENOMEM;
2331                 goto err_alloc_stats;
2332         }
2333
2334         mlxsw_sp_port->sample = kzalloc(sizeof(*mlxsw_sp_port->sample),
2335                                         GFP_KERNEL);
2336         if (!mlxsw_sp_port->sample) {
2337                 err = -ENOMEM;
2338                 goto err_alloc_sample;
2339         }
2340
2341         mlxsw_sp_port->hw_stats.cache =
2342                 kzalloc(sizeof(*mlxsw_sp_port->hw_stats.cache), GFP_KERNEL);
2343
2344         if (!mlxsw_sp_port->hw_stats.cache) {
2345                 err = -ENOMEM;
2346                 goto err_alloc_hw_stats;
2347         }
2348         INIT_DELAYED_WORK(&mlxsw_sp_port->hw_stats.update_dw,
2349                           &update_stats_cache);
2350
2351         dev->netdev_ops = &mlxsw_sp_port_netdev_ops;
2352         dev->ethtool_ops = &mlxsw_sp_port_ethtool_ops;
2353
2354         err = mlxsw_sp_port_swid_set(mlxsw_sp_port, 0);
2355         if (err) {
2356                 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to set SWID\n",
2357                         mlxsw_sp_port->local_port);
2358                 goto err_port_swid_set;
2359         }
2360
2361         err = mlxsw_sp_port_dev_addr_init(mlxsw_sp_port);
2362         if (err) {
2363                 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Unable to init port mac address\n",
2364                         mlxsw_sp_port->local_port);
2365                 goto err_dev_addr_init;
2366         }
2367
2368         netif_carrier_off(dev);
2369
2370         dev->features |= NETIF_F_NETNS_LOCAL | NETIF_F_LLTX | NETIF_F_SG |
2371                          NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_TC;
2372         dev->hw_features |= NETIF_F_HW_TC;
2373
2374         dev->min_mtu = 0;
2375         dev->max_mtu = ETH_MAX_MTU;
2376
2377         /* Each packet needs to have a Tx header (metadata) on top all other
2378          * headers.
2379          */
2380         dev->needed_headroom = MLXSW_TXHDR_LEN;
2381
2382         err = mlxsw_sp_port_system_port_mapping_set(mlxsw_sp_port);
2383         if (err) {
2384                 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to set system port mapping\n",
2385                         mlxsw_sp_port->local_port);
2386                 goto err_port_system_port_mapping_set;
2387         }
2388
2389         err = mlxsw_sp_port_speed_by_width_set(mlxsw_sp_port, width);
2390         if (err) {
2391                 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to enable speeds\n",
2392                         mlxsw_sp_port->local_port);
2393                 goto err_port_speed_by_width_set;
2394         }
2395
2396         err = mlxsw_sp_port_mtu_set(mlxsw_sp_port, ETH_DATA_LEN);
2397         if (err) {
2398                 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to set MTU\n",
2399                         mlxsw_sp_port->local_port);
2400                 goto err_port_mtu_set;
2401         }
2402
2403         err = mlxsw_sp_port_admin_status_set(mlxsw_sp_port, false);
2404         if (err)
2405                 goto err_port_admin_status_set;
2406
2407         err = mlxsw_sp_port_buffers_init(mlxsw_sp_port);
2408         if (err) {
2409                 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to initialize buffers\n",
2410                         mlxsw_sp_port->local_port);
2411                 goto err_port_buffers_init;
2412         }
2413
2414         err = mlxsw_sp_port_ets_init(mlxsw_sp_port);
2415         if (err) {
2416                 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to initialize ETS\n",
2417                         mlxsw_sp_port->local_port);
2418                 goto err_port_ets_init;
2419         }
2420
2421         /* ETS and buffers must be initialized before DCB. */
2422         err = mlxsw_sp_port_dcb_init(mlxsw_sp_port);
2423         if (err) {
2424                 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to initialize DCB\n",
2425                         mlxsw_sp_port->local_port);
2426                 goto err_port_dcb_init;
2427         }
2428
2429         err = mlxsw_sp_port_pvid_vport_create(mlxsw_sp_port);
2430         if (err) {
2431                 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to create PVID vPort\n",
2432                         mlxsw_sp_port->local_port);
2433                 goto err_port_pvid_vport_create;
2434         }
2435
2436         mlxsw_sp_port_switchdev_init(mlxsw_sp_port);
2437         mlxsw_sp->ports[local_port] = mlxsw_sp_port;
2438         err = register_netdev(dev);
2439         if (err) {
2440                 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to register netdev\n",
2441                         mlxsw_sp_port->local_port);
2442                 goto err_register_netdev;
2443         }
2444
2445         mlxsw_core_port_eth_set(mlxsw_sp->core, mlxsw_sp_port->local_port,
2446                                 mlxsw_sp_port, dev, mlxsw_sp_port->split,
2447                                 module);
2448         mlxsw_core_schedule_dw(&mlxsw_sp_port->hw_stats.update_dw, 0);
2449         return 0;
2450
2451 err_register_netdev:
2452         mlxsw_sp->ports[local_port] = NULL;
2453         mlxsw_sp_port_switchdev_fini(mlxsw_sp_port);
2454         mlxsw_sp_port_pvid_vport_destroy(mlxsw_sp_port);
2455 err_port_pvid_vport_create:
2456         mlxsw_sp_port_dcb_fini(mlxsw_sp_port);
2457 err_port_dcb_init:
2458 err_port_ets_init:
2459 err_port_buffers_init:
2460 err_port_admin_status_set:
2461 err_port_mtu_set:
2462 err_port_speed_by_width_set:
2463 err_port_system_port_mapping_set:
2464 err_dev_addr_init:
2465         mlxsw_sp_port_swid_set(mlxsw_sp_port, MLXSW_PORT_SWID_DISABLED_PORT);
2466 err_port_swid_set:
2467         kfree(mlxsw_sp_port->hw_stats.cache);
2468 err_alloc_hw_stats:
2469         kfree(mlxsw_sp_port->sample);
2470 err_alloc_sample:
2471         free_percpu(mlxsw_sp_port->pcpu_stats);
2472 err_alloc_stats:
2473         kfree(mlxsw_sp_port->untagged_vlans);
2474 err_port_untagged_vlans_alloc:
2475         kfree(mlxsw_sp_port->active_vlans);
2476 err_port_active_vlans_alloc:
2477         free_netdev(dev);
2478         return err;
2479 }
2480
2481 static int mlxsw_sp_port_create(struct mlxsw_sp *mlxsw_sp, u8 local_port,
2482                                 bool split, u8 module, u8 width, u8 lane)
2483 {
2484         int err;
2485
2486         err = mlxsw_core_port_init(mlxsw_sp->core, local_port);
2487         if (err) {
2488                 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to init core port\n",
2489                         local_port);
2490                 return err;
2491         }
2492         err = __mlxsw_sp_port_create(mlxsw_sp, local_port, split,
2493                                      module, width, lane);
2494         if (err)
2495                 goto err_port_create;
2496         return 0;
2497
2498 err_port_create:
2499         mlxsw_core_port_fini(mlxsw_sp->core, local_port);
2500         return err;
2501 }
2502
2503 static void __mlxsw_sp_port_remove(struct mlxsw_sp *mlxsw_sp, u8 local_port)
2504 {
2505         struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp->ports[local_port];
2506
2507         cancel_delayed_work_sync(&mlxsw_sp_port->hw_stats.update_dw);
2508         mlxsw_core_port_clear(mlxsw_sp->core, local_port, mlxsw_sp);
2509         unregister_netdev(mlxsw_sp_port->dev); /* This calls ndo_stop */
2510         mlxsw_sp->ports[local_port] = NULL;
2511         mlxsw_sp_port_switchdev_fini(mlxsw_sp_port);
2512         mlxsw_sp_port_pvid_vport_destroy(mlxsw_sp_port);
2513         mlxsw_sp_port_dcb_fini(mlxsw_sp_port);
2514         mlxsw_sp_port_swid_set(mlxsw_sp_port, MLXSW_PORT_SWID_DISABLED_PORT);
2515         mlxsw_sp_port_module_unmap(mlxsw_sp, mlxsw_sp_port->local_port);
2516         kfree(mlxsw_sp_port->hw_stats.cache);
2517         kfree(mlxsw_sp_port->sample);
2518         free_percpu(mlxsw_sp_port->pcpu_stats);
2519         kfree(mlxsw_sp_port->untagged_vlans);
2520         kfree(mlxsw_sp_port->active_vlans);
2521         WARN_ON_ONCE(!list_empty(&mlxsw_sp_port->vports_list));
2522         free_netdev(mlxsw_sp_port->dev);
2523 }
2524
2525 static void mlxsw_sp_port_remove(struct mlxsw_sp *mlxsw_sp, u8 local_port)
2526 {
2527         __mlxsw_sp_port_remove(mlxsw_sp, local_port);
2528         mlxsw_core_port_fini(mlxsw_sp->core, local_port);
2529 }
2530
2531 static bool mlxsw_sp_port_created(struct mlxsw_sp *mlxsw_sp, u8 local_port)
2532 {
2533         return mlxsw_sp->ports[local_port] != NULL;
2534 }
2535
2536 static void mlxsw_sp_ports_remove(struct mlxsw_sp *mlxsw_sp)
2537 {
2538         int i;
2539
2540         for (i = 1; i < MLXSW_PORT_MAX_PORTS; i++)
2541                 if (mlxsw_sp_port_created(mlxsw_sp, i))
2542                         mlxsw_sp_port_remove(mlxsw_sp, i);
2543         kfree(mlxsw_sp->ports);
2544 }
2545
2546 static int mlxsw_sp_ports_create(struct mlxsw_sp *mlxsw_sp)
2547 {
2548         u8 module, width, lane;
2549         size_t alloc_size;
2550         int i;
2551         int err;
2552
2553         alloc_size = sizeof(struct mlxsw_sp_port *) * MLXSW_PORT_MAX_PORTS;
2554         mlxsw_sp->ports = kzalloc(alloc_size, GFP_KERNEL);
2555         if (!mlxsw_sp->ports)
2556                 return -ENOMEM;
2557
2558         for (i = 1; i < MLXSW_PORT_MAX_PORTS; i++) {
2559                 err = mlxsw_sp_port_module_info_get(mlxsw_sp, i, &module,
2560                                                     &width, &lane);
2561                 if (err)
2562                         goto err_port_module_info_get;
2563                 if (!width)
2564                         continue;
2565                 mlxsw_sp->port_to_module[i] = module;
2566                 err = mlxsw_sp_port_create(mlxsw_sp, i, false,
2567                                            module, width, lane);
2568                 if (err)
2569                         goto err_port_create;
2570         }
2571         return 0;
2572
2573 err_port_create:
2574 err_port_module_info_get:
2575         for (i--; i >= 1; i--)
2576                 if (mlxsw_sp_port_created(mlxsw_sp, i))
2577                         mlxsw_sp_port_remove(mlxsw_sp, i);
2578         kfree(mlxsw_sp->ports);
2579         return err;
2580 }
2581
2582 static u8 mlxsw_sp_cluster_base_port_get(u8 local_port)
2583 {
2584         u8 offset = (local_port - 1) % MLXSW_SP_PORTS_PER_CLUSTER_MAX;
2585
2586         return local_port - offset;
2587 }
2588
2589 static int mlxsw_sp_port_split_create(struct mlxsw_sp *mlxsw_sp, u8 base_port,
2590                                       u8 module, unsigned int count)
2591 {
2592         u8 width = MLXSW_PORT_MODULE_MAX_WIDTH / count;
2593         int err, i;
2594
2595         for (i = 0; i < count; i++) {
2596                 err = mlxsw_sp_port_module_map(mlxsw_sp, base_port + i, module,
2597                                                width, i * width);
2598                 if (err)
2599                         goto err_port_module_map;
2600         }
2601
2602         for (i = 0; i < count; i++) {
2603                 err = __mlxsw_sp_port_swid_set(mlxsw_sp, base_port + i, 0);
2604                 if (err)
2605                         goto err_port_swid_set;
2606         }
2607
2608         for (i = 0; i < count; i++) {
2609                 err = mlxsw_sp_port_create(mlxsw_sp, base_port + i, true,
2610                                            module, width, i * width);
2611                 if (err)
2612                         goto err_port_create;
2613         }
2614
2615         return 0;
2616
2617 err_port_create:
2618         for (i--; i >= 0; i--)
2619                 if (mlxsw_sp_port_created(mlxsw_sp, base_port + i))
2620                         mlxsw_sp_port_remove(mlxsw_sp, base_port + i);
2621         i = count;
2622 err_port_swid_set:
2623         for (i--; i >= 0; i--)
2624                 __mlxsw_sp_port_swid_set(mlxsw_sp, base_port + i,
2625                                          MLXSW_PORT_SWID_DISABLED_PORT);
2626         i = count;
2627 err_port_module_map:
2628         for (i--; i >= 0; i--)
2629                 mlxsw_sp_port_module_unmap(mlxsw_sp, base_port + i);
2630         return err;
2631 }
2632
2633 static void mlxsw_sp_port_unsplit_create(struct mlxsw_sp *mlxsw_sp,
2634                                          u8 base_port, unsigned int count)
2635 {
2636         u8 local_port, module, width = MLXSW_PORT_MODULE_MAX_WIDTH;
2637         int i;
2638
2639         /* Split by four means we need to re-create two ports, otherwise
2640          * only one.
2641          */
2642         count = count / 2;
2643
2644         for (i = 0; i < count; i++) {
2645                 local_port = base_port + i * 2;
2646                 module = mlxsw_sp->port_to_module[local_port];
2647
2648                 mlxsw_sp_port_module_map(mlxsw_sp, local_port, module, width,
2649                                          0);
2650         }
2651
2652         for (i = 0; i < count; i++)
2653                 __mlxsw_sp_port_swid_set(mlxsw_sp, base_port + i * 2, 0);
2654
2655         for (i = 0; i < count; i++) {
2656                 local_port = base_port + i * 2;
2657                 module = mlxsw_sp->port_to_module[local_port];
2658
2659                 mlxsw_sp_port_create(mlxsw_sp, local_port, false, module,
2660                                      width, 0);
2661         }
2662 }
2663
2664 static int mlxsw_sp_port_split(struct mlxsw_core *mlxsw_core, u8 local_port,
2665                                unsigned int count)
2666 {
2667         struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
2668         struct mlxsw_sp_port *mlxsw_sp_port;
2669         u8 module, cur_width, base_port;
2670         int i;
2671         int err;
2672
2673         mlxsw_sp_port = mlxsw_sp->ports[local_port];
2674         if (!mlxsw_sp_port) {
2675                 dev_err(mlxsw_sp->bus_info->dev, "Port number \"%d\" does not exist\n",
2676                         local_port);
2677                 return -EINVAL;
2678         }
2679
2680         module = mlxsw_sp_port->mapping.module;
2681         cur_width = mlxsw_sp_port->mapping.width;
2682
2683         if (count != 2 && count != 4) {
2684                 netdev_err(mlxsw_sp_port->dev, "Port can only be split into 2 or 4 ports\n");
2685                 return -EINVAL;
2686         }
2687
2688         if (cur_width != MLXSW_PORT_MODULE_MAX_WIDTH) {
2689                 netdev_err(mlxsw_sp_port->dev, "Port cannot be split further\n");
2690                 return -EINVAL;
2691         }
2692
2693         /* Make sure we have enough slave (even) ports for the split. */
2694         if (count == 2) {
2695                 base_port = local_port;
2696                 if (mlxsw_sp->ports[base_port + 1]) {
2697                         netdev_err(mlxsw_sp_port->dev, "Invalid split configuration\n");
2698                         return -EINVAL;
2699                 }
2700         } else {
2701                 base_port = mlxsw_sp_cluster_base_port_get(local_port);
2702                 if (mlxsw_sp->ports[base_port + 1] ||
2703                     mlxsw_sp->ports[base_port + 3]) {
2704                         netdev_err(mlxsw_sp_port->dev, "Invalid split configuration\n");
2705                         return -EINVAL;
2706                 }
2707         }
2708
2709         for (i = 0; i < count; i++)
2710                 if (mlxsw_sp_port_created(mlxsw_sp, base_port + i))
2711                         mlxsw_sp_port_remove(mlxsw_sp, base_port + i);
2712
2713         err = mlxsw_sp_port_split_create(mlxsw_sp, base_port, module, count);
2714         if (err) {
2715                 dev_err(mlxsw_sp->bus_info->dev, "Failed to create split ports\n");
2716                 goto err_port_split_create;
2717         }
2718
2719         return 0;
2720
2721 err_port_split_create:
2722         mlxsw_sp_port_unsplit_create(mlxsw_sp, base_port, count);
2723         return err;
2724 }
2725
2726 static int mlxsw_sp_port_unsplit(struct mlxsw_core *mlxsw_core, u8 local_port)
2727 {
2728         struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
2729         struct mlxsw_sp_port *mlxsw_sp_port;
2730         u8 cur_width, base_port;
2731         unsigned int count;
2732         int i;
2733
2734         mlxsw_sp_port = mlxsw_sp->ports[local_port];
2735         if (!mlxsw_sp_port) {
2736                 dev_err(mlxsw_sp->bus_info->dev, "Port number \"%d\" does not exist\n",
2737                         local_port);
2738                 return -EINVAL;
2739         }
2740
2741         if (!mlxsw_sp_port->split) {
2742                 netdev_err(mlxsw_sp_port->dev, "Port wasn't split\n");
2743                 return -EINVAL;
2744         }
2745
2746         cur_width = mlxsw_sp_port->mapping.width;
2747         count = cur_width == 1 ? 4 : 2;
2748
2749         base_port = mlxsw_sp_cluster_base_port_get(local_port);
2750
2751         /* Determine which ports to remove. */
2752         if (count == 2 && local_port >= base_port + 2)
2753                 base_port = base_port + 2;
2754
2755         for (i = 0; i < count; i++)
2756                 if (mlxsw_sp_port_created(mlxsw_sp, base_port + i))
2757                         mlxsw_sp_port_remove(mlxsw_sp, base_port + i);
2758
2759         mlxsw_sp_port_unsplit_create(mlxsw_sp, base_port, count);
2760
2761         return 0;
2762 }
2763
2764 static void mlxsw_sp_pude_event_func(const struct mlxsw_reg_info *reg,
2765                                      char *pude_pl, void *priv)
2766 {
2767         struct mlxsw_sp *mlxsw_sp = priv;
2768         struct mlxsw_sp_port *mlxsw_sp_port;
2769         enum mlxsw_reg_pude_oper_status status;
2770         u8 local_port;
2771
2772         local_port = mlxsw_reg_pude_local_port_get(pude_pl);
2773         mlxsw_sp_port = mlxsw_sp->ports[local_port];
2774         if (!mlxsw_sp_port)
2775                 return;
2776
2777         status = mlxsw_reg_pude_oper_status_get(pude_pl);
2778         if (status == MLXSW_PORT_OPER_STATUS_UP) {
2779                 netdev_info(mlxsw_sp_port->dev, "link up\n");
2780                 netif_carrier_on(mlxsw_sp_port->dev);
2781         } else {
2782                 netdev_info(mlxsw_sp_port->dev, "link down\n");
2783                 netif_carrier_off(mlxsw_sp_port->dev);
2784         }
2785 }
2786
2787 static void mlxsw_sp_rx_listener_no_mark_func(struct sk_buff *skb,
2788                                               u8 local_port, void *priv)
2789 {
2790         struct mlxsw_sp *mlxsw_sp = priv;
2791         struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp->ports[local_port];
2792         struct mlxsw_sp_port_pcpu_stats *pcpu_stats;
2793
2794         if (unlikely(!mlxsw_sp_port)) {
2795                 dev_warn_ratelimited(mlxsw_sp->bus_info->dev, "Port %d: skb received for non-existent port\n",
2796                                      local_port);
2797                 return;
2798         }
2799
2800         skb->dev = mlxsw_sp_port->dev;
2801
2802         pcpu_stats = this_cpu_ptr(mlxsw_sp_port->pcpu_stats);
2803         u64_stats_update_begin(&pcpu_stats->syncp);
2804         pcpu_stats->rx_packets++;
2805         pcpu_stats->rx_bytes += skb->len;
2806         u64_stats_update_end(&pcpu_stats->syncp);
2807
2808         skb->protocol = eth_type_trans(skb, skb->dev);
2809         netif_receive_skb(skb);
2810 }
2811
2812 static void mlxsw_sp_rx_listener_mark_func(struct sk_buff *skb, u8 local_port,
2813                                            void *priv)
2814 {
2815         skb->offload_fwd_mark = 1;
2816         return mlxsw_sp_rx_listener_no_mark_func(skb, local_port, priv);
2817 }
2818
2819 static void mlxsw_sp_rx_listener_sample_func(struct sk_buff *skb, u8 local_port,
2820                                              void *priv)
2821 {
2822         struct mlxsw_sp *mlxsw_sp = priv;
2823         struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp->ports[local_port];
2824         struct psample_group *psample_group;
2825         u32 size;
2826
2827         if (unlikely(!mlxsw_sp_port)) {
2828                 dev_warn_ratelimited(mlxsw_sp->bus_info->dev, "Port %d: sample skb received for non-existent port\n",
2829                                      local_port);
2830                 goto out;
2831         }
2832         if (unlikely(!mlxsw_sp_port->sample)) {
2833                 dev_warn_ratelimited(mlxsw_sp->bus_info->dev, "Port %d: sample skb received on unsupported port\n",
2834                                      local_port);
2835                 goto out;
2836         }
2837
2838         size = mlxsw_sp_port->sample->truncate ?
2839                   mlxsw_sp_port->sample->trunc_size : skb->len;
2840
2841         rcu_read_lock();
2842         psample_group = rcu_dereference(mlxsw_sp_port->sample->psample_group);
2843         if (!psample_group)
2844                 goto out_unlock;
2845         psample_sample_packet(psample_group, skb, size,
2846                               mlxsw_sp_port->dev->ifindex, 0,
2847                               mlxsw_sp_port->sample->rate);
2848 out_unlock:
2849         rcu_read_unlock();
2850 out:
2851         consume_skb(skb);
2852 }
2853
2854 #define MLXSW_SP_RXL_NO_MARK(_trap_id, _action, _trap_group, _is_ctrl)  \
2855         MLXSW_RXL(mlxsw_sp_rx_listener_no_mark_func, _trap_id, _action, \
2856                   _is_ctrl, SP_##_trap_group, DISCARD)
2857
2858 #define MLXSW_SP_RXL_MARK(_trap_id, _action, _trap_group, _is_ctrl)     \
2859         MLXSW_RXL(mlxsw_sp_rx_listener_mark_func, _trap_id, _action,    \
2860                 _is_ctrl, SP_##_trap_group, DISCARD)
2861
2862 #define MLXSW_SP_EVENTL(_func, _trap_id)                \
2863         MLXSW_EVENTL(_func, _trap_id, SP_EVENT)
2864
2865 static const struct mlxsw_listener mlxsw_sp_listener[] = {
2866         /* Events */
2867         MLXSW_SP_EVENTL(mlxsw_sp_pude_event_func, PUDE),
2868         /* L2 traps */
2869         MLXSW_SP_RXL_NO_MARK(STP, TRAP_TO_CPU, STP, true),
2870         MLXSW_SP_RXL_NO_MARK(LACP, TRAP_TO_CPU, LACP, true),
2871         MLXSW_SP_RXL_NO_MARK(LLDP, TRAP_TO_CPU, LLDP, true),
2872         MLXSW_SP_RXL_MARK(DHCP, MIRROR_TO_CPU, DHCP, false),
2873         MLXSW_SP_RXL_MARK(IGMP_QUERY, MIRROR_TO_CPU, IGMP, false),
2874         MLXSW_SP_RXL_NO_MARK(IGMP_V1_REPORT, TRAP_TO_CPU, IGMP, false),
2875         MLXSW_SP_RXL_NO_MARK(IGMP_V2_REPORT, TRAP_TO_CPU, IGMP, false),
2876         MLXSW_SP_RXL_NO_MARK(IGMP_V2_LEAVE, TRAP_TO_CPU, IGMP, false),
2877         MLXSW_SP_RXL_NO_MARK(IGMP_V3_REPORT, TRAP_TO_CPU, IGMP, false),
2878         MLXSW_SP_RXL_MARK(ARPBC, MIRROR_TO_CPU, ARP, false),
2879         MLXSW_SP_RXL_MARK(ARPUC, MIRROR_TO_CPU, ARP, false),
2880         /* L3 traps */
2881         MLXSW_SP_RXL_NO_MARK(MTUERROR, TRAP_TO_CPU, ROUTER_EXP, false),
2882         MLXSW_SP_RXL_NO_MARK(TTLERROR, TRAP_TO_CPU, ROUTER_EXP, false),
2883         MLXSW_SP_RXL_NO_MARK(LBERROR, TRAP_TO_CPU, ROUTER_EXP, false),
2884         MLXSW_SP_RXL_MARK(OSPF, TRAP_TO_CPU, OSPF, false),
2885         MLXSW_SP_RXL_NO_MARK(IP2ME, TRAP_TO_CPU, IP2ME, false),
2886         MLXSW_SP_RXL_NO_MARK(RTR_INGRESS0, TRAP_TO_CPU, REMOTE_ROUTE, false),
2887         MLXSW_SP_RXL_NO_MARK(HOST_MISS_IPV4, TRAP_TO_CPU, ARP_MISS, false),
2888         MLXSW_SP_RXL_NO_MARK(BGP_IPV4, TRAP_TO_CPU, BGP_IPV4, false),
2889         /* PKT Sample trap */
2890         MLXSW_RXL(mlxsw_sp_rx_listener_sample_func, PKT_SAMPLE, MIRROR_TO_CPU,
2891                   false, SP_IP2ME, DISCARD)
2892 };
2893
2894 static int mlxsw_sp_cpu_policers_set(struct mlxsw_core *mlxsw_core)
2895 {
2896         char qpcr_pl[MLXSW_REG_QPCR_LEN];
2897         enum mlxsw_reg_qpcr_ir_units ir_units;
2898         int max_cpu_policers;
2899         bool is_bytes;
2900         u8 burst_size;
2901         u32 rate;
2902         int i, err;
2903
2904         if (!MLXSW_CORE_RES_VALID(mlxsw_core, MAX_CPU_POLICERS))
2905                 return -EIO;
2906
2907         max_cpu_policers = MLXSW_CORE_RES_GET(mlxsw_core, MAX_CPU_POLICERS);
2908
2909         ir_units = MLXSW_REG_QPCR_IR_UNITS_M;
2910         for (i = 0; i < max_cpu_policers; i++) {
2911                 is_bytes = false;
2912                 switch (i) {
2913                 case MLXSW_REG_HTGT_TRAP_GROUP_SP_STP:
2914                 case MLXSW_REG_HTGT_TRAP_GROUP_SP_LACP:
2915                 case MLXSW_REG_HTGT_TRAP_GROUP_SP_LLDP:
2916                 case MLXSW_REG_HTGT_TRAP_GROUP_SP_OSPF:
2917                         rate = 128;
2918                         burst_size = 7;
2919                         break;
2920                 case MLXSW_REG_HTGT_TRAP_GROUP_SP_IGMP:
2921                         rate = 16 * 1024;
2922                         burst_size = 10;
2923                         break;
2924                 case MLXSW_REG_HTGT_TRAP_GROUP_SP_BGP_IPV4:
2925                 case MLXSW_REG_HTGT_TRAP_GROUP_SP_ARP:
2926                 case MLXSW_REG_HTGT_TRAP_GROUP_SP_DHCP:
2927                 case MLXSW_REG_HTGT_TRAP_GROUP_SP_ARP_MISS:
2928                 case MLXSW_REG_HTGT_TRAP_GROUP_SP_ROUTER_EXP:
2929                 case MLXSW_REG_HTGT_TRAP_GROUP_SP_REMOTE_ROUTE:
2930                         rate = 1024;
2931                         burst_size = 7;
2932                         break;
2933                 case MLXSW_REG_HTGT_TRAP_GROUP_SP_IP2ME:
2934                         is_bytes = true;
2935                         rate = 4 * 1024;
2936                         burst_size = 4;
2937                         break;
2938                 default:
2939                         continue;
2940                 }
2941
2942                 mlxsw_reg_qpcr_pack(qpcr_pl, i, ir_units, is_bytes, rate,
2943                                     burst_size);
2944                 err = mlxsw_reg_write(mlxsw_core, MLXSW_REG(qpcr), qpcr_pl);
2945                 if (err)
2946                         return err;
2947         }
2948
2949         return 0;
2950 }
2951
2952 static int mlxsw_sp_trap_groups_set(struct mlxsw_core *mlxsw_core)
2953 {
2954         char htgt_pl[MLXSW_REG_HTGT_LEN];
2955         enum mlxsw_reg_htgt_trap_group i;
2956         int max_cpu_policers;
2957         int max_trap_groups;
2958         u8 priority, tc;
2959         u16 policer_id;
2960         int err;
2961
2962         if (!MLXSW_CORE_RES_VALID(mlxsw_core, MAX_TRAP_GROUPS))
2963                 return -EIO;
2964
2965         max_trap_groups = MLXSW_CORE_RES_GET(mlxsw_core, MAX_TRAP_GROUPS);
2966         max_cpu_policers = MLXSW_CORE_RES_GET(mlxsw_core, MAX_CPU_POLICERS);
2967
2968         for (i = 0; i < max_trap_groups; i++) {
2969                 policer_id = i;
2970                 switch (i) {
2971                 case MLXSW_REG_HTGT_TRAP_GROUP_SP_STP:
2972                 case MLXSW_REG_HTGT_TRAP_GROUP_SP_LACP:
2973                 case MLXSW_REG_HTGT_TRAP_GROUP_SP_LLDP:
2974                 case MLXSW_REG_HTGT_TRAP_GROUP_SP_OSPF:
2975                         priority = 5;
2976                         tc = 5;
2977                         break;
2978                 case MLXSW_REG_HTGT_TRAP_GROUP_SP_BGP_IPV4:
2979                 case MLXSW_REG_HTGT_TRAP_GROUP_SP_DHCP:
2980                         priority = 4;
2981                         tc = 4;
2982                         break;
2983                 case MLXSW_REG_HTGT_TRAP_GROUP_SP_IGMP:
2984                 case MLXSW_REG_HTGT_TRAP_GROUP_SP_IP2ME:
2985                         priority = 3;
2986                         tc = 3;
2987                         break;
2988                 case MLXSW_REG_HTGT_TRAP_GROUP_SP_ARP:
2989                         priority = 2;
2990                         tc = 2;
2991                         break;
2992                 case MLXSW_REG_HTGT_TRAP_GROUP_SP_ARP_MISS:
2993                 case MLXSW_REG_HTGT_TRAP_GROUP_SP_ROUTER_EXP:
2994                 case MLXSW_REG_HTGT_TRAP_GROUP_SP_REMOTE_ROUTE:
2995                         priority = 1;
2996                         tc = 1;
2997                         break;
2998                 case MLXSW_REG_HTGT_TRAP_GROUP_SP_EVENT:
2999                         priority = MLXSW_REG_HTGT_DEFAULT_PRIORITY;
3000                         tc = MLXSW_REG_HTGT_DEFAULT_TC;
3001                         policer_id = MLXSW_REG_HTGT_INVALID_POLICER;
3002                         break;
3003                 default:
3004                         continue;
3005                 }
3006
3007                 if (max_cpu_policers <= policer_id &&
3008                     policer_id != MLXSW_REG_HTGT_INVALID_POLICER)
3009                         return -EIO;
3010
3011                 mlxsw_reg_htgt_pack(htgt_pl, i, policer_id, priority, tc);
3012                 err = mlxsw_reg_write(mlxsw_core, MLXSW_REG(htgt), htgt_pl);
3013                 if (err)
3014                         return err;
3015         }
3016
3017         return 0;
3018 }
3019
3020 static int mlxsw_sp_traps_init(struct mlxsw_sp *mlxsw_sp)
3021 {
3022         int i;
3023         int err;
3024
3025         err = mlxsw_sp_cpu_policers_set(mlxsw_sp->core);
3026         if (err)
3027                 return err;
3028
3029         err = mlxsw_sp_trap_groups_set(mlxsw_sp->core);
3030         if (err)
3031                 return err;
3032
3033         for (i = 0; i < ARRAY_SIZE(mlxsw_sp_listener); i++) {
3034                 err = mlxsw_core_trap_register(mlxsw_sp->core,
3035                                                &mlxsw_sp_listener[i],
3036                                                mlxsw_sp);
3037                 if (err)
3038                         goto err_listener_register;
3039
3040         }
3041         return 0;
3042
3043 err_listener_register:
3044         for (i--; i >= 0; i--) {
3045                 mlxsw_core_trap_unregister(mlxsw_sp->core,
3046                                            &mlxsw_sp_listener[i],
3047                                            mlxsw_sp);
3048         }
3049         return err;
3050 }
3051
3052 static void mlxsw_sp_traps_fini(struct mlxsw_sp *mlxsw_sp)
3053 {
3054         int i;
3055
3056         for (i = 0; i < ARRAY_SIZE(mlxsw_sp_listener); i++) {
3057                 mlxsw_core_trap_unregister(mlxsw_sp->core,
3058                                            &mlxsw_sp_listener[i],
3059                                            mlxsw_sp);
3060         }
3061 }
3062
3063 static int __mlxsw_sp_flood_init(struct mlxsw_core *mlxsw_core,
3064                                  enum mlxsw_reg_sfgc_type type,
3065                                  enum mlxsw_reg_sfgc_bridge_type bridge_type)
3066 {
3067         enum mlxsw_flood_table_type table_type;
3068         enum mlxsw_sp_flood_table flood_table;
3069         char sfgc_pl[MLXSW_REG_SFGC_LEN];
3070
3071         if (bridge_type == MLXSW_REG_SFGC_BRIDGE_TYPE_VFID)
3072                 table_type = MLXSW_REG_SFGC_TABLE_TYPE_FID;
3073         else
3074                 table_type = MLXSW_REG_SFGC_TABLE_TYPE_FID_OFFEST;
3075
3076         switch (type) {
3077         case MLXSW_REG_SFGC_TYPE_UNKNOWN_UNICAST:
3078                 flood_table = MLXSW_SP_FLOOD_TABLE_UC;
3079                 break;
3080         case MLXSW_REG_SFGC_TYPE_UNREGISTERED_MULTICAST_IPV4:
3081                 flood_table = MLXSW_SP_FLOOD_TABLE_MC;
3082                 break;
3083         default:
3084                 flood_table = MLXSW_SP_FLOOD_TABLE_BC;
3085         }
3086
3087         mlxsw_reg_sfgc_pack(sfgc_pl, type, bridge_type, table_type,
3088                             flood_table);
3089         return mlxsw_reg_write(mlxsw_core, MLXSW_REG(sfgc), sfgc_pl);
3090 }
3091
3092 static int mlxsw_sp_flood_init(struct mlxsw_sp *mlxsw_sp)
3093 {
3094         int type, err;
3095
3096         for (type = 0; type < MLXSW_REG_SFGC_TYPE_MAX; type++) {
3097                 if (type == MLXSW_REG_SFGC_TYPE_RESERVED)
3098                         continue;
3099
3100                 err = __mlxsw_sp_flood_init(mlxsw_sp->core, type,
3101                                             MLXSW_REG_SFGC_BRIDGE_TYPE_VFID);
3102                 if (err)
3103                         return err;
3104
3105                 err = __mlxsw_sp_flood_init(mlxsw_sp->core, type,
3106                                             MLXSW_REG_SFGC_BRIDGE_TYPE_1Q_FID);
3107                 if (err)
3108                         return err;
3109         }
3110
3111         return 0;
3112 }
3113
3114 static int mlxsw_sp_lag_init(struct mlxsw_sp *mlxsw_sp)
3115 {
3116         char slcr_pl[MLXSW_REG_SLCR_LEN];
3117         int err;
3118
3119         mlxsw_reg_slcr_pack(slcr_pl, MLXSW_REG_SLCR_LAG_HASH_SMAC |
3120                                      MLXSW_REG_SLCR_LAG_HASH_DMAC |
3121                                      MLXSW_REG_SLCR_LAG_HASH_ETHERTYPE |
3122                                      MLXSW_REG_SLCR_LAG_HASH_VLANID |
3123                                      MLXSW_REG_SLCR_LAG_HASH_SIP |
3124                                      MLXSW_REG_SLCR_LAG_HASH_DIP |
3125                                      MLXSW_REG_SLCR_LAG_HASH_SPORT |
3126                                      MLXSW_REG_SLCR_LAG_HASH_DPORT |
3127                                      MLXSW_REG_SLCR_LAG_HASH_IPPROTO);
3128         err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(slcr), slcr_pl);
3129         if (err)
3130                 return err;
3131
3132         if (!MLXSW_CORE_RES_VALID(mlxsw_sp->core, MAX_LAG) ||
3133             !MLXSW_CORE_RES_VALID(mlxsw_sp->core, MAX_LAG_MEMBERS))
3134                 return -EIO;
3135
3136         mlxsw_sp->lags = kcalloc(MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_LAG),
3137                                  sizeof(struct mlxsw_sp_upper),
3138                                  GFP_KERNEL);
3139         if (!mlxsw_sp->lags)
3140                 return -ENOMEM;
3141
3142         return 0;
3143 }
3144
3145 static void mlxsw_sp_lag_fini(struct mlxsw_sp *mlxsw_sp)
3146 {
3147         kfree(mlxsw_sp->lags);
3148 }
3149
3150 static int mlxsw_sp_basic_trap_groups_set(struct mlxsw_core *mlxsw_core)
3151 {
3152         char htgt_pl[MLXSW_REG_HTGT_LEN];
3153
3154         mlxsw_reg_htgt_pack(htgt_pl, MLXSW_REG_HTGT_TRAP_GROUP_EMAD,
3155                             MLXSW_REG_HTGT_INVALID_POLICER,
3156                             MLXSW_REG_HTGT_DEFAULT_PRIORITY,
3157                             MLXSW_REG_HTGT_DEFAULT_TC);
3158         return mlxsw_reg_write(mlxsw_core, MLXSW_REG(htgt), htgt_pl);
3159 }
3160
3161 static int mlxsw_sp_init(struct mlxsw_core *mlxsw_core,
3162                          const struct mlxsw_bus_info *mlxsw_bus_info)
3163 {
3164         struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
3165         int err;
3166
3167         mlxsw_sp->core = mlxsw_core;
3168         mlxsw_sp->bus_info = mlxsw_bus_info;
3169         INIT_LIST_HEAD(&mlxsw_sp->fids);
3170         INIT_LIST_HEAD(&mlxsw_sp->vfids.list);
3171         INIT_LIST_HEAD(&mlxsw_sp->br_mids.list);
3172
3173         err = mlxsw_sp_base_mac_get(mlxsw_sp);
3174         if (err) {
3175                 dev_err(mlxsw_sp->bus_info->dev, "Failed to get base mac\n");
3176                 return err;
3177         }
3178
3179         err = mlxsw_sp_traps_init(mlxsw_sp);
3180         if (err) {
3181                 dev_err(mlxsw_sp->bus_info->dev, "Failed to set traps\n");
3182                 return err;
3183         }
3184
3185         err = mlxsw_sp_flood_init(mlxsw_sp);
3186         if (err) {
3187                 dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize flood tables\n");
3188                 goto err_flood_init;
3189         }
3190
3191         err = mlxsw_sp_buffers_init(mlxsw_sp);
3192         if (err) {
3193                 dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize buffers\n");
3194                 goto err_buffers_init;
3195         }
3196
3197         err = mlxsw_sp_lag_init(mlxsw_sp);
3198         if (err) {
3199                 dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize LAG\n");
3200                 goto err_lag_init;
3201         }
3202
3203         err = mlxsw_sp_switchdev_init(mlxsw_sp);
3204         if (err) {
3205                 dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize switchdev\n");
3206                 goto err_switchdev_init;
3207         }
3208
3209         err = mlxsw_sp_router_init(mlxsw_sp);
3210         if (err) {
3211                 dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize router\n");
3212                 goto err_router_init;
3213         }
3214
3215         err = mlxsw_sp_span_init(mlxsw_sp);
3216         if (err) {
3217                 dev_err(mlxsw_sp->bus_info->dev, "Failed to init span system\n");
3218                 goto err_span_init;
3219         }
3220
3221         err = mlxsw_sp_acl_init(mlxsw_sp);
3222         if (err) {
3223                 dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize ACL\n");
3224                 goto err_acl_init;
3225         }
3226
3227         err = mlxsw_sp_ports_create(mlxsw_sp);
3228         if (err) {
3229                 dev_err(mlxsw_sp->bus_info->dev, "Failed to create ports\n");
3230                 goto err_ports_create;
3231         }
3232
3233         return 0;
3234
3235 err_ports_create:
3236         mlxsw_sp_acl_fini(mlxsw_sp);
3237 err_acl_init:
3238         mlxsw_sp_span_fini(mlxsw_sp);
3239 err_span_init:
3240         mlxsw_sp_router_fini(mlxsw_sp);
3241 err_router_init:
3242         mlxsw_sp_switchdev_fini(mlxsw_sp);
3243 err_switchdev_init:
3244         mlxsw_sp_lag_fini(mlxsw_sp);
3245 err_lag_init:
3246         mlxsw_sp_buffers_fini(mlxsw_sp);
3247 err_buffers_init:
3248 err_flood_init:
3249         mlxsw_sp_traps_fini(mlxsw_sp);
3250         return err;
3251 }
3252
3253 static void mlxsw_sp_fini(struct mlxsw_core *mlxsw_core)
3254 {
3255         struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
3256
3257         mlxsw_sp_ports_remove(mlxsw_sp);
3258         mlxsw_sp_acl_fini(mlxsw_sp);
3259         mlxsw_sp_span_fini(mlxsw_sp);
3260         mlxsw_sp_router_fini(mlxsw_sp);
3261         mlxsw_sp_switchdev_fini(mlxsw_sp);
3262         mlxsw_sp_lag_fini(mlxsw_sp);
3263         mlxsw_sp_buffers_fini(mlxsw_sp);
3264         mlxsw_sp_traps_fini(mlxsw_sp);
3265         WARN_ON(!list_empty(&mlxsw_sp->vfids.list));
3266         WARN_ON(!list_empty(&mlxsw_sp->fids));
3267 }
3268
3269 static struct mlxsw_config_profile mlxsw_sp_config_profile = {
3270         .used_max_vepa_channels         = 1,
3271         .max_vepa_channels              = 0,
3272         .used_max_mid                   = 1,
3273         .max_mid                        = MLXSW_SP_MID_MAX,
3274         .used_max_pgt                   = 1,
3275         .max_pgt                        = 0,
3276         .used_flood_tables              = 1,
3277         .used_flood_mode                = 1,
3278         .flood_mode                     = 3,
3279         .max_fid_offset_flood_tables    = 3,
3280         .fid_offset_flood_table_size    = VLAN_N_VID - 1,
3281         .max_fid_flood_tables           = 3,
3282         .fid_flood_table_size           = MLXSW_SP_VFID_MAX,
3283         .used_max_ib_mc                 = 1,
3284         .max_ib_mc                      = 0,
3285         .used_max_pkey                  = 1,
3286         .max_pkey                       = 0,
3287         .used_kvd_split_data            = 1,
3288         .kvd_hash_granularity           = MLXSW_SP_KVD_GRANULARITY,
3289         .kvd_hash_single_parts          = 2,
3290         .kvd_hash_double_parts          = 1,
3291         .kvd_linear_size                = MLXSW_SP_KVD_LINEAR_SIZE,
3292         .swid_config                    = {
3293                 {
3294                         .used_type      = 1,
3295                         .type           = MLXSW_PORT_SWID_TYPE_ETH,
3296                 }
3297         },
3298         .resource_query_enable          = 1,
3299 };
3300
3301 static struct mlxsw_driver mlxsw_sp_driver = {
3302         .kind                           = mlxsw_sp_driver_name,
3303         .priv_size                      = sizeof(struct mlxsw_sp),
3304         .init                           = mlxsw_sp_init,
3305         .fini                           = mlxsw_sp_fini,
3306         .basic_trap_groups_set          = mlxsw_sp_basic_trap_groups_set,
3307         .port_split                     = mlxsw_sp_port_split,
3308         .port_unsplit                   = mlxsw_sp_port_unsplit,
3309         .sb_pool_get                    = mlxsw_sp_sb_pool_get,
3310         .sb_pool_set                    = mlxsw_sp_sb_pool_set,
3311         .sb_port_pool_get               = mlxsw_sp_sb_port_pool_get,
3312         .sb_port_pool_set               = mlxsw_sp_sb_port_pool_set,
3313         .sb_tc_pool_bind_get            = mlxsw_sp_sb_tc_pool_bind_get,
3314         .sb_tc_pool_bind_set            = mlxsw_sp_sb_tc_pool_bind_set,
3315         .sb_occ_snapshot                = mlxsw_sp_sb_occ_snapshot,
3316         .sb_occ_max_clear               = mlxsw_sp_sb_occ_max_clear,
3317         .sb_occ_port_pool_get           = mlxsw_sp_sb_occ_port_pool_get,
3318         .sb_occ_tc_port_bind_get        = mlxsw_sp_sb_occ_tc_port_bind_get,
3319         .txhdr_construct                = mlxsw_sp_txhdr_construct,
3320         .txhdr_len                      = MLXSW_TXHDR_LEN,
3321         .profile                        = &mlxsw_sp_config_profile,
3322 };
3323
3324 bool mlxsw_sp_port_dev_check(const struct net_device *dev)
3325 {
3326         return dev->netdev_ops == &mlxsw_sp_port_netdev_ops;
3327 }
3328
3329 static int mlxsw_sp_lower_dev_walk(struct net_device *lower_dev, void *data)
3330 {
3331         struct mlxsw_sp_port **p_mlxsw_sp_port = data;
3332         int ret = 0;
3333
3334         if (mlxsw_sp_port_dev_check(lower_dev)) {
3335                 *p_mlxsw_sp_port = netdev_priv(lower_dev);
3336                 ret = 1;
3337         }
3338
3339         return ret;
3340 }
3341
3342 static struct mlxsw_sp_port *mlxsw_sp_port_dev_lower_find(struct net_device *dev)
3343 {
3344         struct mlxsw_sp_port *mlxsw_sp_port;
3345
3346         if (mlxsw_sp_port_dev_check(dev))
3347                 return netdev_priv(dev);
3348
3349         mlxsw_sp_port = NULL;
3350         netdev_walk_all_lower_dev(dev, mlxsw_sp_lower_dev_walk, &mlxsw_sp_port);
3351
3352         return mlxsw_sp_port;
3353 }
3354
3355 static struct mlxsw_sp *mlxsw_sp_lower_get(struct net_device *dev)
3356 {
3357         struct mlxsw_sp_port *mlxsw_sp_port;
3358
3359         mlxsw_sp_port = mlxsw_sp_port_dev_lower_find(dev);
3360         return mlxsw_sp_port ? mlxsw_sp_port->mlxsw_sp : NULL;
3361 }
3362
3363 static struct mlxsw_sp_port *mlxsw_sp_port_dev_lower_find_rcu(struct net_device *dev)
3364 {
3365         struct mlxsw_sp_port *mlxsw_sp_port;
3366
3367         if (mlxsw_sp_port_dev_check(dev))
3368                 return netdev_priv(dev);
3369
3370         mlxsw_sp_port = NULL;
3371         netdev_walk_all_lower_dev_rcu(dev, mlxsw_sp_lower_dev_walk,
3372                                       &mlxsw_sp_port);
3373
3374         return mlxsw_sp_port;
3375 }
3376
3377 struct mlxsw_sp_port *mlxsw_sp_port_lower_dev_hold(struct net_device *dev)
3378 {
3379         struct mlxsw_sp_port *mlxsw_sp_port;
3380
3381         rcu_read_lock();
3382         mlxsw_sp_port = mlxsw_sp_port_dev_lower_find_rcu(dev);
3383         if (mlxsw_sp_port)
3384                 dev_hold(mlxsw_sp_port->dev);
3385         rcu_read_unlock();
3386         return mlxsw_sp_port;
3387 }
3388
3389 void mlxsw_sp_port_dev_put(struct mlxsw_sp_port *mlxsw_sp_port)
3390 {
3391         dev_put(mlxsw_sp_port->dev);
3392 }
3393
3394 static bool mlxsw_sp_rif_should_config(struct mlxsw_sp_rif *r,
3395                                        unsigned long event)
3396 {
3397         switch (event) {
3398         case NETDEV_UP:
3399                 if (!r)
3400                         return true;
3401                 r->ref_count++;
3402                 return false;
3403         case NETDEV_DOWN:
3404                 if (r && --r->ref_count == 0)
3405                         return true;
3406                 /* It is possible we already removed the RIF ourselves
3407                  * if it was assigned to a netdev that is now a bridge
3408                  * or LAG slave.
3409                  */
3410                 return false;
3411         }
3412
3413         return false;
3414 }
3415
3416 static int mlxsw_sp_avail_rif_get(struct mlxsw_sp *mlxsw_sp)
3417 {
3418         int i;
3419
3420         for (i = 0; i < MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_RIFS); i++)
3421                 if (!mlxsw_sp->rifs[i])
3422                         return i;
3423
3424         return MLXSW_SP_INVALID_RIF;
3425 }
3426
3427 static void mlxsw_sp_vport_rif_sp_attr_get(struct mlxsw_sp_port *mlxsw_sp_vport,
3428                                            bool *p_lagged, u16 *p_system_port)
3429 {
3430         u8 local_port = mlxsw_sp_vport->local_port;
3431
3432         *p_lagged = mlxsw_sp_vport->lagged;
3433         *p_system_port = *p_lagged ? mlxsw_sp_vport->lag_id : local_port;
3434 }
3435
3436 static int mlxsw_sp_vport_rif_sp_op(struct mlxsw_sp_port *mlxsw_sp_vport,
3437                                     struct net_device *l3_dev, u16 rif,
3438                                     bool create)
3439 {
3440         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_vport->mlxsw_sp;
3441         bool lagged = mlxsw_sp_vport->lagged;
3442         char ritr_pl[MLXSW_REG_RITR_LEN];
3443         u16 system_port;
3444
3445         mlxsw_reg_ritr_pack(ritr_pl, create, MLXSW_REG_RITR_SP_IF, rif,
3446                             l3_dev->mtu, l3_dev->dev_addr);
3447
3448         mlxsw_sp_vport_rif_sp_attr_get(mlxsw_sp_vport, &lagged, &system_port);
3449         mlxsw_reg_ritr_sp_if_pack(ritr_pl, lagged, system_port,
3450                                   mlxsw_sp_vport_vid_get(mlxsw_sp_vport));
3451
3452         return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl);
3453 }
3454
3455 static void mlxsw_sp_vport_rif_sp_leave(struct mlxsw_sp_port *mlxsw_sp_vport);
3456
3457 static struct mlxsw_sp_fid *
3458 mlxsw_sp_rfid_alloc(u16 fid, struct net_device *l3_dev)
3459 {
3460         struct mlxsw_sp_fid *f;
3461
3462         f = kzalloc(sizeof(*f), GFP_KERNEL);
3463         if (!f)
3464                 return NULL;
3465
3466         f->leave = mlxsw_sp_vport_rif_sp_leave;
3467         f->ref_count = 0;
3468         f->dev = l3_dev;
3469         f->fid = fid;
3470
3471         return f;
3472 }
3473
3474 static struct mlxsw_sp_rif *
3475 mlxsw_sp_rif_alloc(u16 rif, struct net_device *l3_dev, struct mlxsw_sp_fid *f)
3476 {
3477         struct mlxsw_sp_rif *r;
3478
3479         r = kzalloc(sizeof(*r), GFP_KERNEL);
3480         if (!r)
3481                 return NULL;
3482
3483         INIT_LIST_HEAD(&r->nexthop_list);
3484         INIT_LIST_HEAD(&r->neigh_list);
3485         ether_addr_copy(r->addr, l3_dev->dev_addr);
3486         r->mtu = l3_dev->mtu;
3487         r->ref_count = 1;
3488         r->dev = l3_dev;
3489         r->rif = rif;
3490         r->f = f;
3491
3492         return r;
3493 }
3494
3495 static struct mlxsw_sp_rif *
3496 mlxsw_sp_vport_rif_sp_create(struct mlxsw_sp_port *mlxsw_sp_vport,
3497                              struct net_device *l3_dev)
3498 {
3499         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_vport->mlxsw_sp;
3500         struct mlxsw_sp_fid *f;
3501         struct mlxsw_sp_rif *r;
3502         u16 fid, rif;
3503         int err;
3504
3505         rif = mlxsw_sp_avail_rif_get(mlxsw_sp);
3506         if (rif == MLXSW_SP_INVALID_RIF)
3507                 return ERR_PTR(-ERANGE);
3508
3509         err = mlxsw_sp_vport_rif_sp_op(mlxsw_sp_vport, l3_dev, rif, true);
3510         if (err)
3511                 return ERR_PTR(err);
3512
3513         fid = mlxsw_sp_rif_sp_to_fid(rif);
3514         err = mlxsw_sp_rif_fdb_op(mlxsw_sp, l3_dev->dev_addr, fid, true);
3515         if (err)
3516                 goto err_rif_fdb_op;
3517
3518         f = mlxsw_sp_rfid_alloc(fid, l3_dev);
3519         if (!f) {
3520                 err = -ENOMEM;
3521                 goto err_rfid_alloc;
3522         }
3523
3524         r = mlxsw_sp_rif_alloc(rif, l3_dev, f);
3525         if (!r) {
3526                 err = -ENOMEM;
3527                 goto err_rif_alloc;
3528         }
3529
3530         f->r = r;
3531         mlxsw_sp->rifs[rif] = r;
3532
3533         return r;
3534
3535 err_rif_alloc:
3536         kfree(f);
3537 err_rfid_alloc:
3538         mlxsw_sp_rif_fdb_op(mlxsw_sp, l3_dev->dev_addr, fid, false);
3539 err_rif_fdb_op:
3540         mlxsw_sp_vport_rif_sp_op(mlxsw_sp_vport, l3_dev, rif, false);
3541         return ERR_PTR(err);
3542 }
3543
3544 static void mlxsw_sp_vport_rif_sp_destroy(struct mlxsw_sp_port *mlxsw_sp_vport,
3545                                           struct mlxsw_sp_rif *r)
3546 {
3547         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_vport->mlxsw_sp;
3548         struct net_device *l3_dev = r->dev;
3549         struct mlxsw_sp_fid *f = r->f;
3550         u16 fid = f->fid;
3551         u16 rif = r->rif;
3552
3553         mlxsw_sp_router_rif_gone_sync(mlxsw_sp, r);
3554
3555         mlxsw_sp->rifs[rif] = NULL;
3556         f->r = NULL;
3557
3558         kfree(r);
3559
3560         kfree(f);
3561
3562         mlxsw_sp_rif_fdb_op(mlxsw_sp, l3_dev->dev_addr, fid, false);
3563
3564         mlxsw_sp_vport_rif_sp_op(mlxsw_sp_vport, l3_dev, rif, false);
3565 }
3566
3567 static int mlxsw_sp_vport_rif_sp_join(struct mlxsw_sp_port *mlxsw_sp_vport,
3568                                       struct net_device *l3_dev)
3569 {
3570         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_vport->mlxsw_sp;
3571         struct mlxsw_sp_rif *r;
3572
3573         r = mlxsw_sp_rif_find_by_dev(mlxsw_sp, l3_dev);
3574         if (!r) {
3575                 r = mlxsw_sp_vport_rif_sp_create(mlxsw_sp_vport, l3_dev);
3576                 if (IS_ERR(r))
3577                         return PTR_ERR(r);
3578         }
3579
3580         mlxsw_sp_vport_fid_set(mlxsw_sp_vport, r->f);
3581         r->f->ref_count++;
3582
3583         netdev_dbg(mlxsw_sp_vport->dev, "Joined FID=%d\n", r->f->fid);
3584
3585         return 0;
3586 }
3587
3588 static void mlxsw_sp_vport_rif_sp_leave(struct mlxsw_sp_port *mlxsw_sp_vport)
3589 {
3590         struct mlxsw_sp_fid *f = mlxsw_sp_vport_fid_get(mlxsw_sp_vport);
3591
3592         netdev_dbg(mlxsw_sp_vport->dev, "Left FID=%d\n", f->fid);
3593
3594         mlxsw_sp_vport_fid_set(mlxsw_sp_vport, NULL);
3595         if (--f->ref_count == 0)
3596                 mlxsw_sp_vport_rif_sp_destroy(mlxsw_sp_vport, f->r);
3597 }
3598
3599 static int mlxsw_sp_inetaddr_vport_event(struct net_device *l3_dev,
3600                                          struct net_device *port_dev,
3601                                          unsigned long event, u16 vid)
3602 {
3603         struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(port_dev);
3604         struct mlxsw_sp_port *mlxsw_sp_vport;
3605
3606         mlxsw_sp_vport = mlxsw_sp_port_vport_find(mlxsw_sp_port, vid);
3607         if (WARN_ON(!mlxsw_sp_vport))
3608                 return -EINVAL;
3609
3610         switch (event) {
3611         case NETDEV_UP:
3612                 return mlxsw_sp_vport_rif_sp_join(mlxsw_sp_vport, l3_dev);
3613         case NETDEV_DOWN:
3614                 mlxsw_sp_vport_rif_sp_leave(mlxsw_sp_vport);
3615                 break;
3616         }
3617
3618         return 0;
3619 }
3620
3621 static int mlxsw_sp_inetaddr_port_event(struct net_device *port_dev,
3622                                         unsigned long event)
3623 {
3624         if (netif_is_bridge_port(port_dev) || netif_is_lag_port(port_dev))
3625                 return 0;
3626
3627         return mlxsw_sp_inetaddr_vport_event(port_dev, port_dev, event, 1);
3628 }
3629
3630 static int __mlxsw_sp_inetaddr_lag_event(struct net_device *l3_dev,
3631                                          struct net_device *lag_dev,
3632                                          unsigned long event, u16 vid)
3633 {
3634         struct net_device *port_dev;
3635         struct list_head *iter;
3636         int err;
3637
3638         netdev_for_each_lower_dev(lag_dev, port_dev, iter) {
3639                 if (mlxsw_sp_port_dev_check(port_dev)) {
3640                         err = mlxsw_sp_inetaddr_vport_event(l3_dev, port_dev,
3641                                                             event, vid);
3642                         if (err)
3643                                 return err;
3644                 }
3645         }
3646
3647         return 0;
3648 }
3649
3650 static int mlxsw_sp_inetaddr_lag_event(struct net_device *lag_dev,
3651                                        unsigned long event)
3652 {
3653         if (netif_is_bridge_port(lag_dev))
3654                 return 0;
3655
3656         return __mlxsw_sp_inetaddr_lag_event(lag_dev, lag_dev, event, 1);
3657 }
3658
3659 static struct mlxsw_sp_fid *mlxsw_sp_bridge_fid_get(struct mlxsw_sp *mlxsw_sp,
3660                                                     struct net_device *l3_dev)
3661 {
3662         u16 fid;
3663
3664         if (is_vlan_dev(l3_dev))
3665                 fid = vlan_dev_vlan_id(l3_dev);
3666         else if (mlxsw_sp->master_bridge.dev == l3_dev)
3667                 fid = 1;
3668         else
3669                 return mlxsw_sp_vfid_find(mlxsw_sp, l3_dev);
3670
3671         return mlxsw_sp_fid_find(mlxsw_sp, fid);
3672 }
3673
3674 static enum mlxsw_flood_table_type mlxsw_sp_flood_table_type_get(u16 fid)
3675 {
3676         return mlxsw_sp_fid_is_vfid(fid) ? MLXSW_REG_SFGC_TABLE_TYPE_FID :
3677                MLXSW_REG_SFGC_TABLE_TYPE_FID_OFFEST;
3678 }
3679
3680 static u16 mlxsw_sp_flood_table_index_get(u16 fid)
3681 {
3682         return mlxsw_sp_fid_is_vfid(fid) ? mlxsw_sp_fid_to_vfid(fid) : fid;
3683 }
3684
3685 static int mlxsw_sp_router_port_flood_set(struct mlxsw_sp *mlxsw_sp, u16 fid,
3686                                           bool set)
3687 {
3688         enum mlxsw_flood_table_type table_type;
3689         char *sftr_pl;
3690         u16 index;
3691         int err;
3692
3693         sftr_pl = kmalloc(MLXSW_REG_SFTR_LEN, GFP_KERNEL);
3694         if (!sftr_pl)
3695                 return -ENOMEM;
3696
3697         table_type = mlxsw_sp_flood_table_type_get(fid);
3698         index = mlxsw_sp_flood_table_index_get(fid);
3699         mlxsw_reg_sftr_pack(sftr_pl, MLXSW_SP_FLOOD_TABLE_BC, index, table_type,
3700                             1, MLXSW_PORT_ROUTER_PORT, set);
3701         err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sftr), sftr_pl);
3702
3703         kfree(sftr_pl);
3704         return err;
3705 }
3706
3707 static enum mlxsw_reg_ritr_if_type mlxsw_sp_rif_type_get(u16 fid)
3708 {
3709         if (mlxsw_sp_fid_is_vfid(fid))
3710                 return MLXSW_REG_RITR_FID_IF;
3711         else
3712                 return MLXSW_REG_RITR_VLAN_IF;
3713 }
3714
3715 static int mlxsw_sp_rif_bridge_op(struct mlxsw_sp *mlxsw_sp,
3716                                   struct net_device *l3_dev,
3717                                   u16 fid, u16 rif,
3718                                   bool create)
3719 {
3720         enum mlxsw_reg_ritr_if_type rif_type;
3721         char ritr_pl[MLXSW_REG_RITR_LEN];
3722
3723         rif_type = mlxsw_sp_rif_type_get(fid);
3724         mlxsw_reg_ritr_pack(ritr_pl, create, rif_type, rif, l3_dev->mtu,
3725                             l3_dev->dev_addr);
3726         mlxsw_reg_ritr_fid_set(ritr_pl, rif_type, fid);
3727
3728         return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl);
3729 }
3730
3731 static int mlxsw_sp_rif_bridge_create(struct mlxsw_sp *mlxsw_sp,
3732                                       struct net_device *l3_dev,
3733                                       struct mlxsw_sp_fid *f)
3734 {
3735         struct mlxsw_sp_rif *r;
3736         u16 rif;
3737         int err;
3738
3739         rif = mlxsw_sp_avail_rif_get(mlxsw_sp);
3740         if (rif == MLXSW_SP_INVALID_RIF)
3741                 return -ERANGE;
3742
3743         err = mlxsw_sp_router_port_flood_set(mlxsw_sp, f->fid, true);
3744         if (err)
3745                 return err;
3746
3747         err = mlxsw_sp_rif_bridge_op(mlxsw_sp, l3_dev, f->fid, rif, true);
3748         if (err)
3749                 goto err_rif_bridge_op;
3750
3751         err = mlxsw_sp_rif_fdb_op(mlxsw_sp, l3_dev->dev_addr, f->fid, true);
3752         if (err)
3753                 goto err_rif_fdb_op;
3754
3755         r = mlxsw_sp_rif_alloc(rif, l3_dev, f);
3756         if (!r) {
3757                 err = -ENOMEM;
3758                 goto err_rif_alloc;
3759         }
3760
3761         f->r = r;
3762         mlxsw_sp->rifs[rif] = r;
3763
3764         netdev_dbg(l3_dev, "RIF=%d created\n", rif);
3765
3766         return 0;
3767
3768 err_rif_alloc:
3769         mlxsw_sp_rif_fdb_op(mlxsw_sp, l3_dev->dev_addr, f->fid, false);
3770 err_rif_fdb_op:
3771         mlxsw_sp_rif_bridge_op(mlxsw_sp, l3_dev, f->fid, rif, false);
3772 err_rif_bridge_op:
3773         mlxsw_sp_router_port_flood_set(mlxsw_sp, f->fid, false);
3774         return err;
3775 }
3776
3777 void mlxsw_sp_rif_bridge_destroy(struct mlxsw_sp *mlxsw_sp,
3778                                  struct mlxsw_sp_rif *r)
3779 {
3780         struct net_device *l3_dev = r->dev;
3781         struct mlxsw_sp_fid *f = r->f;
3782         u16 rif = r->rif;
3783
3784         mlxsw_sp_router_rif_gone_sync(mlxsw_sp, r);
3785
3786         mlxsw_sp->rifs[rif] = NULL;
3787         f->r = NULL;
3788
3789         kfree(r);
3790
3791         mlxsw_sp_rif_fdb_op(mlxsw_sp, l3_dev->dev_addr, f->fid, false);
3792
3793         mlxsw_sp_rif_bridge_op(mlxsw_sp, l3_dev, f->fid, rif, false);
3794
3795         mlxsw_sp_router_port_flood_set(mlxsw_sp, f->fid, false);
3796
3797         netdev_dbg(l3_dev, "RIF=%d destroyed\n", rif);
3798 }
3799
3800 static int mlxsw_sp_inetaddr_bridge_event(struct net_device *l3_dev,
3801                                           struct net_device *br_dev,
3802                                           unsigned long event)
3803 {
3804         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(l3_dev);
3805         struct mlxsw_sp_fid *f;
3806
3807         /* FID can either be an actual FID if the L3 device is the
3808          * VLAN-aware bridge or a VLAN device on top. Otherwise, the
3809          * L3 device is a VLAN-unaware bridge and we get a vFID.
3810          */
3811         f = mlxsw_sp_bridge_fid_get(mlxsw_sp, l3_dev);
3812         if (WARN_ON(!f))
3813                 return -EINVAL;
3814
3815         switch (event) {
3816         case NETDEV_UP:
3817                 return mlxsw_sp_rif_bridge_create(mlxsw_sp, l3_dev, f);
3818         case NETDEV_DOWN:
3819                 mlxsw_sp_rif_bridge_destroy(mlxsw_sp, f->r);
3820                 break;
3821         }
3822
3823         return 0;
3824 }
3825
3826 static int mlxsw_sp_inetaddr_vlan_event(struct net_device *vlan_dev,
3827                                         unsigned long event)
3828 {
3829         struct net_device *real_dev = vlan_dev_real_dev(vlan_dev);
3830         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(vlan_dev);
3831         u16 vid = vlan_dev_vlan_id(vlan_dev);
3832
3833         if (mlxsw_sp_port_dev_check(real_dev))
3834                 return mlxsw_sp_inetaddr_vport_event(vlan_dev, real_dev, event,
3835                                                      vid);
3836         else if (netif_is_lag_master(real_dev))
3837                 return __mlxsw_sp_inetaddr_lag_event(vlan_dev, real_dev, event,
3838                                                      vid);
3839         else if (netif_is_bridge_master(real_dev) &&
3840                  mlxsw_sp->master_bridge.dev == real_dev)
3841                 return mlxsw_sp_inetaddr_bridge_event(vlan_dev, real_dev,
3842                                                       event);
3843
3844         return 0;
3845 }
3846
3847 static int mlxsw_sp_inetaddr_event(struct notifier_block *unused,
3848                                    unsigned long event, void *ptr)
3849 {
3850         struct in_ifaddr *ifa = (struct in_ifaddr *) ptr;
3851         struct net_device *dev = ifa->ifa_dev->dev;
3852         struct mlxsw_sp *mlxsw_sp;
3853         struct mlxsw_sp_rif *r;
3854         int err = 0;
3855
3856         mlxsw_sp = mlxsw_sp_lower_get(dev);
3857         if (!mlxsw_sp)
3858                 goto out;
3859
3860         r = mlxsw_sp_rif_find_by_dev(mlxsw_sp, dev);
3861         if (!mlxsw_sp_rif_should_config(r, event))
3862                 goto out;
3863
3864         if (mlxsw_sp_port_dev_check(dev))
3865                 err = mlxsw_sp_inetaddr_port_event(dev, event);
3866         else if (netif_is_lag_master(dev))
3867                 err = mlxsw_sp_inetaddr_lag_event(dev, event);
3868         else if (netif_is_bridge_master(dev))
3869                 err = mlxsw_sp_inetaddr_bridge_event(dev, dev, event);
3870         else if (is_vlan_dev(dev))
3871                 err = mlxsw_sp_inetaddr_vlan_event(dev, event);
3872
3873 out:
3874         return notifier_from_errno(err);
3875 }
3876
3877 static int mlxsw_sp_rif_edit(struct mlxsw_sp *mlxsw_sp, u16 rif,
3878                              const char *mac, int mtu)
3879 {
3880         char ritr_pl[MLXSW_REG_RITR_LEN];
3881         int err;
3882
3883         mlxsw_reg_ritr_rif_pack(ritr_pl, rif);
3884         err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl);
3885         if (err)
3886                 return err;
3887
3888         mlxsw_reg_ritr_mtu_set(ritr_pl, mtu);
3889         mlxsw_reg_ritr_if_mac_memcpy_to(ritr_pl, mac);
3890         mlxsw_reg_ritr_op_set(ritr_pl, MLXSW_REG_RITR_RIF_CREATE);
3891         return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl);
3892 }
3893
3894 static int mlxsw_sp_netdevice_router_port_event(struct net_device *dev)
3895 {
3896         struct mlxsw_sp *mlxsw_sp;
3897         struct mlxsw_sp_rif *r;
3898         int err;
3899
3900         mlxsw_sp = mlxsw_sp_lower_get(dev);
3901         if (!mlxsw_sp)
3902                 return 0;
3903
3904         r = mlxsw_sp_rif_find_by_dev(mlxsw_sp, dev);
3905         if (!r)
3906                 return 0;
3907
3908         err = mlxsw_sp_rif_fdb_op(mlxsw_sp, r->addr, r->f->fid, false);
3909         if (err)
3910                 return err;
3911
3912         err = mlxsw_sp_rif_edit(mlxsw_sp, r->rif, dev->dev_addr, dev->mtu);
3913         if (err)
3914                 goto err_rif_edit;
3915
3916         err = mlxsw_sp_rif_fdb_op(mlxsw_sp, dev->dev_addr, r->f->fid, true);
3917         if (err)
3918                 goto err_rif_fdb_op;
3919
3920         ether_addr_copy(r->addr, dev->dev_addr);
3921         r->mtu = dev->mtu;
3922
3923         netdev_dbg(dev, "Updated RIF=%d\n", r->rif);
3924
3925         return 0;
3926
3927 err_rif_fdb_op:
3928         mlxsw_sp_rif_edit(mlxsw_sp, r->rif, r->addr, r->mtu);
3929 err_rif_edit:
3930         mlxsw_sp_rif_fdb_op(mlxsw_sp, r->addr, r->f->fid, true);
3931         return err;
3932 }
3933
3934 static bool mlxsw_sp_lag_port_fid_member(struct mlxsw_sp_port *lag_port,
3935                                          u16 fid)
3936 {
3937         if (mlxsw_sp_fid_is_vfid(fid))
3938                 return mlxsw_sp_port_vport_find_by_fid(lag_port, fid);
3939         else
3940                 return test_bit(fid, lag_port->active_vlans);
3941 }
3942
3943 static bool mlxsw_sp_port_fdb_should_flush(struct mlxsw_sp_port *mlxsw_sp_port,
3944                                            u16 fid)
3945 {
3946         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
3947         u8 local_port = mlxsw_sp_port->local_port;
3948         u16 lag_id = mlxsw_sp_port->lag_id;
3949         u64 max_lag_members;
3950         int i, count = 0;
3951
3952         if (!mlxsw_sp_port->lagged)
3953                 return true;
3954
3955         max_lag_members = MLXSW_CORE_RES_GET(mlxsw_sp->core,
3956                                              MAX_LAG_MEMBERS);
3957         for (i = 0; i < max_lag_members; i++) {
3958                 struct mlxsw_sp_port *lag_port;
3959
3960                 lag_port = mlxsw_sp_port_lagged_get(mlxsw_sp, lag_id, i);
3961                 if (!lag_port || lag_port->local_port == local_port)
3962                         continue;
3963                 if (mlxsw_sp_lag_port_fid_member(lag_port, fid))
3964                         count++;
3965         }
3966
3967         return !count;
3968 }
3969
3970 static int
3971 mlxsw_sp_port_fdb_flush_by_port_fid(const struct mlxsw_sp_port *mlxsw_sp_port,
3972                                     u16 fid)
3973 {
3974         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
3975         char sfdf_pl[MLXSW_REG_SFDF_LEN];
3976
3977         mlxsw_reg_sfdf_pack(sfdf_pl, MLXSW_REG_SFDF_FLUSH_PER_PORT_AND_FID);
3978         mlxsw_reg_sfdf_fid_set(sfdf_pl, fid);
3979         mlxsw_reg_sfdf_port_fid_system_port_set(sfdf_pl,
3980                                                 mlxsw_sp_port->local_port);
3981
3982         netdev_dbg(mlxsw_sp_port->dev, "FDB flushed using Port=%d, FID=%d\n",
3983                    mlxsw_sp_port->local_port, fid);
3984
3985         return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfdf), sfdf_pl);
3986 }
3987
3988 static int
3989 mlxsw_sp_port_fdb_flush_by_lag_id_fid(const struct mlxsw_sp_port *mlxsw_sp_port,
3990                                       u16 fid)
3991 {
3992         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
3993         char sfdf_pl[MLXSW_REG_SFDF_LEN];
3994
3995         mlxsw_reg_sfdf_pack(sfdf_pl, MLXSW_REG_SFDF_FLUSH_PER_LAG_AND_FID);
3996         mlxsw_reg_sfdf_fid_set(sfdf_pl, fid);
3997         mlxsw_reg_sfdf_lag_fid_lag_id_set(sfdf_pl, mlxsw_sp_port->lag_id);
3998
3999         netdev_dbg(mlxsw_sp_port->dev, "FDB flushed using LAG ID=%d, FID=%d\n",
4000                    mlxsw_sp_port->lag_id, fid);
4001
4002         return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfdf), sfdf_pl);
4003 }
4004
4005 int mlxsw_sp_port_fdb_flush(struct mlxsw_sp_port *mlxsw_sp_port, u16 fid)
4006 {
4007         if (!mlxsw_sp_port_fdb_should_flush(mlxsw_sp_port, fid))
4008                 return 0;
4009
4010         if (mlxsw_sp_port->lagged)
4011                 return mlxsw_sp_port_fdb_flush_by_lag_id_fid(mlxsw_sp_port,
4012                                                              fid);
4013         else
4014                 return mlxsw_sp_port_fdb_flush_by_port_fid(mlxsw_sp_port, fid);
4015 }
4016
4017 static void mlxsw_sp_master_bridge_gone_sync(struct mlxsw_sp *mlxsw_sp)
4018 {
4019         struct mlxsw_sp_fid *f, *tmp;
4020
4021         list_for_each_entry_safe(f, tmp, &mlxsw_sp->fids, list)
4022                 if (--f->ref_count == 0)
4023                         mlxsw_sp_fid_destroy(mlxsw_sp, f);
4024                 else
4025                         WARN_ON_ONCE(1);
4026 }
4027
4028 static bool mlxsw_sp_master_bridge_check(struct mlxsw_sp *mlxsw_sp,
4029                                          struct net_device *br_dev)
4030 {
4031         return !mlxsw_sp->master_bridge.dev ||
4032                mlxsw_sp->master_bridge.dev == br_dev;
4033 }
4034
4035 static void mlxsw_sp_master_bridge_inc(struct mlxsw_sp *mlxsw_sp,
4036                                        struct net_device *br_dev)
4037 {
4038         mlxsw_sp->master_bridge.dev = br_dev;
4039         mlxsw_sp->master_bridge.ref_count++;
4040 }
4041
4042 static void mlxsw_sp_master_bridge_dec(struct mlxsw_sp *mlxsw_sp)
4043 {
4044         if (--mlxsw_sp->master_bridge.ref_count == 0) {
4045                 mlxsw_sp->master_bridge.dev = NULL;
4046                 /* It's possible upper VLAN devices are still holding
4047                  * references to underlying FIDs. Drop the reference
4048                  * and release the resources if it was the last one.
4049                  * If it wasn't, then something bad happened.
4050                  */
4051                 mlxsw_sp_master_bridge_gone_sync(mlxsw_sp);
4052         }
4053 }
4054
4055 static int mlxsw_sp_port_bridge_join(struct mlxsw_sp_port *mlxsw_sp_port,
4056                                      struct net_device *br_dev)
4057 {
4058         struct net_device *dev = mlxsw_sp_port->dev;
4059         int err;
4060
4061         /* When port is not bridged untagged packets are tagged with
4062          * PVID=VID=1, thereby creating an implicit VLAN interface in
4063          * the device. Remove it and let bridge code take care of its
4064          * own VLANs.
4065          */
4066         err = mlxsw_sp_port_kill_vid(dev, 0, 1);
4067         if (err)
4068                 return err;
4069
4070         mlxsw_sp_master_bridge_inc(mlxsw_sp_port->mlxsw_sp, br_dev);
4071
4072         mlxsw_sp_port->learning = 1;
4073         mlxsw_sp_port->learning_sync = 1;
4074         mlxsw_sp_port->uc_flood = 1;
4075         mlxsw_sp_port->mc_flood = 1;
4076         mlxsw_sp_port->mc_router = 0;
4077         mlxsw_sp_port->mc_disabled = 1;
4078         mlxsw_sp_port->bridged = 1;
4079
4080         return 0;
4081 }
4082
4083 static void mlxsw_sp_port_bridge_leave(struct mlxsw_sp_port *mlxsw_sp_port)
4084 {
4085         struct net_device *dev = mlxsw_sp_port->dev;
4086
4087         mlxsw_sp_port_pvid_set(mlxsw_sp_port, 1);
4088
4089         mlxsw_sp_master_bridge_dec(mlxsw_sp_port->mlxsw_sp);
4090
4091         mlxsw_sp_port->learning = 0;
4092         mlxsw_sp_port->learning_sync = 0;
4093         mlxsw_sp_port->uc_flood = 0;
4094         mlxsw_sp_port->mc_flood = 0;
4095         mlxsw_sp_port->mc_router = 0;
4096         mlxsw_sp_port->bridged = 0;
4097
4098         /* Add implicit VLAN interface in the device, so that untagged
4099          * packets will be classified to the default vFID.
4100          */
4101         mlxsw_sp_port_add_vid(dev, 0, 1);
4102 }
4103
4104 static int mlxsw_sp_lag_create(struct mlxsw_sp *mlxsw_sp, u16 lag_id)
4105 {
4106         char sldr_pl[MLXSW_REG_SLDR_LEN];
4107
4108         mlxsw_reg_sldr_lag_create_pack(sldr_pl, lag_id);
4109         return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sldr), sldr_pl);
4110 }
4111
4112 static int mlxsw_sp_lag_destroy(struct mlxsw_sp *mlxsw_sp, u16 lag_id)
4113 {
4114         char sldr_pl[MLXSW_REG_SLDR_LEN];
4115
4116         mlxsw_reg_sldr_lag_destroy_pack(sldr_pl, lag_id);
4117         return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sldr), sldr_pl);
4118 }
4119
4120 static int mlxsw_sp_lag_col_port_add(struct mlxsw_sp_port *mlxsw_sp_port,
4121                                      u16 lag_id, u8 port_index)
4122 {
4123         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
4124         char slcor_pl[MLXSW_REG_SLCOR_LEN];
4125
4126         mlxsw_reg_slcor_port_add_pack(slcor_pl, mlxsw_sp_port->local_port,
4127                                       lag_id, port_index);
4128         return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(slcor), slcor_pl);
4129 }
4130
4131 static int mlxsw_sp_lag_col_port_remove(struct mlxsw_sp_port *mlxsw_sp_port,
4132                                         u16 lag_id)
4133 {
4134         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
4135         char slcor_pl[MLXSW_REG_SLCOR_LEN];
4136
4137         mlxsw_reg_slcor_port_remove_pack(slcor_pl, mlxsw_sp_port->local_port,
4138                                          lag_id);
4139         return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(slcor), slcor_pl);
4140 }
4141
4142 static int mlxsw_sp_lag_col_port_enable(struct mlxsw_sp_port *mlxsw_sp_port,
4143                                         u16 lag_id)
4144 {
4145         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
4146         char slcor_pl[MLXSW_REG_SLCOR_LEN];
4147
4148         mlxsw_reg_slcor_col_enable_pack(slcor_pl, mlxsw_sp_port->local_port,
4149                                         lag_id);
4150         return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(slcor), slcor_pl);
4151 }
4152
4153 static int mlxsw_sp_lag_col_port_disable(struct mlxsw_sp_port *mlxsw_sp_port,
4154                                          u16 lag_id)
4155 {
4156         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
4157         char slcor_pl[MLXSW_REG_SLCOR_LEN];
4158
4159         mlxsw_reg_slcor_col_disable_pack(slcor_pl, mlxsw_sp_port->local_port,
4160                                          lag_id);
4161         return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(slcor), slcor_pl);
4162 }
4163
4164 static int mlxsw_sp_lag_index_get(struct mlxsw_sp *mlxsw_sp,
4165                                   struct net_device *lag_dev,
4166                                   u16 *p_lag_id)
4167 {
4168         struct mlxsw_sp_upper *lag;
4169         int free_lag_id = -1;
4170         u64 max_lag;
4171         int i;
4172
4173         max_lag = MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_LAG);
4174         for (i = 0; i < max_lag; i++) {
4175                 lag = mlxsw_sp_lag_get(mlxsw_sp, i);
4176                 if (lag->ref_count) {
4177                         if (lag->dev == lag_dev) {
4178                                 *p_lag_id = i;
4179                                 return 0;
4180                         }
4181                 } else if (free_lag_id < 0) {
4182                         free_lag_id = i;
4183                 }
4184         }
4185         if (free_lag_id < 0)
4186                 return -EBUSY;
4187         *p_lag_id = free_lag_id;
4188         return 0;
4189 }
4190
4191 static bool
4192 mlxsw_sp_master_lag_check(struct mlxsw_sp *mlxsw_sp,
4193                           struct net_device *lag_dev,
4194                           struct netdev_lag_upper_info *lag_upper_info)
4195 {
4196         u16 lag_id;
4197
4198         if (mlxsw_sp_lag_index_get(mlxsw_sp, lag_dev, &lag_id) != 0)
4199                 return false;
4200         if (lag_upper_info->tx_type != NETDEV_LAG_TX_TYPE_HASH)
4201                 return false;
4202         return true;
4203 }
4204
4205 static int mlxsw_sp_port_lag_index_get(struct mlxsw_sp *mlxsw_sp,
4206                                        u16 lag_id, u8 *p_port_index)
4207 {
4208         u64 max_lag_members;
4209         int i;
4210
4211         max_lag_members = MLXSW_CORE_RES_GET(mlxsw_sp->core,
4212                                              MAX_LAG_MEMBERS);
4213         for (i = 0; i < max_lag_members; i++) {
4214                 if (!mlxsw_sp_port_lagged_get(mlxsw_sp, lag_id, i)) {
4215                         *p_port_index = i;
4216                         return 0;
4217                 }
4218         }
4219         return -EBUSY;
4220 }
4221
4222 static void
4223 mlxsw_sp_port_pvid_vport_lag_join(struct mlxsw_sp_port *mlxsw_sp_port,
4224                                   u16 lag_id)
4225 {
4226         struct mlxsw_sp_port *mlxsw_sp_vport;
4227         struct mlxsw_sp_fid *f;
4228
4229         mlxsw_sp_vport = mlxsw_sp_port_vport_find(mlxsw_sp_port, 1);
4230         if (WARN_ON(!mlxsw_sp_vport))
4231                 return;
4232
4233         /* If vPort is assigned a RIF, then leave it since it's no
4234          * longer valid.
4235          */
4236         f = mlxsw_sp_vport_fid_get(mlxsw_sp_vport);
4237         if (f)
4238                 f->leave(mlxsw_sp_vport);
4239
4240         mlxsw_sp_vport->lag_id = lag_id;
4241         mlxsw_sp_vport->lagged = 1;
4242 }
4243
4244 static void
4245 mlxsw_sp_port_pvid_vport_lag_leave(struct mlxsw_sp_port *mlxsw_sp_port)
4246 {
4247         struct mlxsw_sp_port *mlxsw_sp_vport;
4248         struct mlxsw_sp_fid *f;
4249
4250         mlxsw_sp_vport = mlxsw_sp_port_vport_find(mlxsw_sp_port, 1);
4251         if (WARN_ON(!mlxsw_sp_vport))
4252                 return;
4253
4254         f = mlxsw_sp_vport_fid_get(mlxsw_sp_vport);
4255         if (f)
4256                 f->leave(mlxsw_sp_vport);
4257
4258         mlxsw_sp_vport->lagged = 0;
4259 }
4260
4261 static int mlxsw_sp_port_lag_join(struct mlxsw_sp_port *mlxsw_sp_port,
4262                                   struct net_device *lag_dev)
4263 {
4264         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
4265         struct mlxsw_sp_upper *lag;
4266         u16 lag_id;
4267         u8 port_index;
4268         int err;
4269
4270         err = mlxsw_sp_lag_index_get(mlxsw_sp, lag_dev, &lag_id);
4271         if (err)
4272                 return err;
4273         lag = mlxsw_sp_lag_get(mlxsw_sp, lag_id);
4274         if (!lag->ref_count) {
4275                 err = mlxsw_sp_lag_create(mlxsw_sp, lag_id);
4276                 if (err)
4277                         return err;
4278                 lag->dev = lag_dev;
4279         }
4280
4281         err = mlxsw_sp_port_lag_index_get(mlxsw_sp, lag_id, &port_index);
4282         if (err)
4283                 return err;
4284         err = mlxsw_sp_lag_col_port_add(mlxsw_sp_port, lag_id, port_index);
4285         if (err)
4286                 goto err_col_port_add;
4287         err = mlxsw_sp_lag_col_port_enable(mlxsw_sp_port, lag_id);
4288         if (err)
4289                 goto err_col_port_enable;
4290
4291         mlxsw_core_lag_mapping_set(mlxsw_sp->core, lag_id, port_index,
4292                                    mlxsw_sp_port->local_port);
4293         mlxsw_sp_port->lag_id = lag_id;
4294         mlxsw_sp_port->lagged = 1;
4295         lag->ref_count++;
4296
4297         mlxsw_sp_port_pvid_vport_lag_join(mlxsw_sp_port, lag_id);
4298
4299         return 0;
4300
4301 err_col_port_enable:
4302         mlxsw_sp_lag_col_port_remove(mlxsw_sp_port, lag_id);
4303 err_col_port_add:
4304         if (!lag->ref_count)
4305                 mlxsw_sp_lag_destroy(mlxsw_sp, lag_id);
4306         return err;
4307 }
4308
4309 static void mlxsw_sp_port_lag_leave(struct mlxsw_sp_port *mlxsw_sp_port,
4310                                     struct net_device *lag_dev)
4311 {
4312         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
4313         u16 lag_id = mlxsw_sp_port->lag_id;
4314         struct mlxsw_sp_upper *lag;
4315
4316         if (!mlxsw_sp_port->lagged)
4317                 return;
4318         lag = mlxsw_sp_lag_get(mlxsw_sp, lag_id);
4319         WARN_ON(lag->ref_count == 0);
4320
4321         mlxsw_sp_lag_col_port_disable(mlxsw_sp_port, lag_id);
4322         mlxsw_sp_lag_col_port_remove(mlxsw_sp_port, lag_id);
4323
4324         if (mlxsw_sp_port->bridged) {
4325                 mlxsw_sp_port_active_vlans_del(mlxsw_sp_port);
4326                 mlxsw_sp_port_bridge_leave(mlxsw_sp_port);
4327         }
4328
4329         if (lag->ref_count == 1)
4330                 mlxsw_sp_lag_destroy(mlxsw_sp, lag_id);
4331
4332         mlxsw_core_lag_mapping_clear(mlxsw_sp->core, lag_id,
4333                                      mlxsw_sp_port->local_port);
4334         mlxsw_sp_port->lagged = 0;
4335         lag->ref_count--;
4336
4337         mlxsw_sp_port_pvid_vport_lag_leave(mlxsw_sp_port);
4338 }
4339
4340 static int mlxsw_sp_lag_dist_port_add(struct mlxsw_sp_port *mlxsw_sp_port,
4341                                       u16 lag_id)
4342 {
4343         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
4344         char sldr_pl[MLXSW_REG_SLDR_LEN];
4345
4346         mlxsw_reg_sldr_lag_add_port_pack(sldr_pl, lag_id,
4347                                          mlxsw_sp_port->local_port);
4348         return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sldr), sldr_pl);
4349 }
4350
4351 static int mlxsw_sp_lag_dist_port_remove(struct mlxsw_sp_port *mlxsw_sp_port,
4352                                          u16 lag_id)
4353 {
4354         struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
4355         char sldr_pl[MLXSW_REG_SLDR_LEN];
4356
4357         mlxsw_reg_sldr_lag_remove_port_pack(sldr_pl, lag_id,
4358                                             mlxsw_sp_port->local_port);
4359         return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sldr), sldr_pl);
4360 }
4361
4362 static int mlxsw_sp_port_lag_tx_en_set(struct mlxsw_sp_port *mlxsw_sp_port,
4363                                        bool lag_tx_enabled)
4364 {
4365         if (lag_tx_enabled)
4366                 return mlxsw_sp_lag_dist_port_add(mlxsw_sp_port,
4367                                                   mlxsw_sp_port->lag_id);
4368         else
4369                 return mlxsw_sp_lag_dist_port_remove(mlxsw_sp_port,
4370                                                      mlxsw_sp_port->lag_id);
4371 }
4372
4373 static int mlxsw_sp_port_lag_changed(struct mlxsw_sp_port *mlxsw_sp_port,
4374                                      struct netdev_lag_lower_state_info *info)
4375 {
4376         return mlxsw_sp_port_lag_tx_en_set(mlxsw_sp_port, info->tx_enabled);
4377 }
4378
4379 static int mlxsw_sp_port_vlan_link(struct mlxsw_sp_port *mlxsw_sp_port,
4380                                    struct net_device *vlan_dev)
4381 {
4382         struct mlxsw_sp_port *mlxsw_sp_vport;
4383         u16 vid = vlan_dev_vlan_id(vlan_dev);
4384
4385         mlxsw_sp_vport = mlxsw_sp_port_vport_find(mlxsw_sp_port, vid);
4386         if (WARN_ON(!mlxsw_sp_vport))
4387                 return -EINVAL;
4388
4389         mlxsw_sp_vport->dev = vlan_dev;
4390
4391         return 0;
4392 }
4393
4394 static void mlxsw_sp_port_vlan_unlink(struct mlxsw_sp_port *mlxsw_sp_port,
4395                                       struct net_device *vlan_dev)
4396 {
4397         struct mlxsw_sp_port *mlxsw_sp_vport;
4398         u16 vid = vlan_dev_vlan_id(vlan_dev);
4399
4400         mlxsw_sp_vport = mlxsw_sp_port_vport_find(mlxsw_sp_port, vid);
4401         if (WARN_ON(!mlxsw_sp_vport))
4402                 return;
4403
4404         mlxsw_sp_vport->dev = mlxsw_sp_port->dev;
4405 }
4406
4407 static int mlxsw_sp_netdevice_port_upper_event(struct net_device *dev,
4408                                                unsigned long event, void *ptr)
4409 {
4410         struct netdev_notifier_changeupper_info *info;
4411         struct mlxsw_sp_port *mlxsw_sp_port;
4412         struct net_device *upper_dev;
4413         struct mlxsw_sp *mlxsw_sp;
4414         int err = 0;
4415
4416         mlxsw_sp_port = netdev_priv(dev);
4417         mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
4418         info = ptr;
4419
4420         switch (event) {
4421         case NETDEV_PRECHANGEUPPER:
4422                 upper_dev = info->upper_dev;
4423                 if (!is_vlan_dev(upper_dev) &&
4424                     !netif_is_lag_master(upper_dev) &&
4425                     !netif_is_bridge_master(upper_dev))
4426                         return -EINVAL;
4427                 if (!info->linking)
4428                         break;
4429                 /* HW limitation forbids to put ports to multiple bridges. */
4430                 if (netif_is_bridge_master(upper_dev) &&
4431                     !mlxsw_sp_master_bridge_check(mlxsw_sp, upper_dev))
4432                         return -EINVAL;
4433                 if (netif_is_lag_master(upper_dev) &&
4434                     !mlxsw_sp_master_lag_check(mlxsw_sp, upper_dev,
4435                                                info->upper_info))
4436                         return -EINVAL;
4437                 if (netif_is_lag_master(upper_dev) && vlan_uses_dev(dev))
4438                         return -EINVAL;
4439                 if (netif_is_lag_port(dev) && is_vlan_dev(upper_dev) &&
4440                     !netif_is_lag_master(vlan_dev_real_dev(upper_dev)))
4441                         return -EINVAL;
4442                 break;
4443         case NETDEV_CHANGEUPPER:
4444                 upper_dev = info->upper_dev;
4445                 if (is_vlan_dev(upper_dev)) {
4446                         if (info->linking)
4447                                 err = mlxsw_sp_port_vlan_link(mlxsw_sp_port,
4448                                                               upper_dev);
4449                         else
4450                                  mlxsw_sp_port_vlan_unlink(mlxsw_sp_port,
4451                                                            upper_dev);
4452                 } else if (netif_is_bridge_master(upper_dev)) {
4453                         if (info->linking)
4454                                 err = mlxsw_sp_port_bridge_join(mlxsw_sp_port,
4455                                                                 upper_dev);
4456                         else
4457                                 mlxsw_sp_port_bridge_leave(mlxsw_sp_port);
4458                 } else if (netif_is_lag_master(upper_dev)) {
4459                         if (info->linking)
4460                                 err = mlxsw_sp_port_lag_join(mlxsw_sp_port,
4461                                                              upper_dev);
4462                         else
4463                                 mlxsw_sp_port_lag_leave(mlxsw_sp_port,
4464                                                         upper_dev);
4465                 } else {
4466                         err = -EINVAL;
4467                         WARN_ON(1);
4468                 }
4469                 break;
4470         }
4471
4472         return err;
4473 }
4474
4475 static int mlxsw_sp_netdevice_port_lower_event(struct net_device *dev,
4476                                                unsigned long event, void *ptr)
4477 {
4478         struct netdev_notifier_changelowerstate_info *info;
4479         struct mlxsw_sp_port *mlxsw_sp_port;
4480         int err;
4481
4482         mlxsw_sp_port = netdev_priv(dev);
4483         info = ptr;
4484
4485         switch (event) {
4486         case NETDEV_CHANGELOWERSTATE:
4487                 if (netif_is_lag_port(dev) && mlxsw_sp_port->lagged) {
4488                         err = mlxsw_sp_port_lag_changed(mlxsw_sp_port,
4489                                                         info->lower_state_info);
4490                         if (err)
4491                                 netdev_err(dev, "Failed to reflect link aggregation lower state change\n");
4492                 }
4493                 break;
4494         }
4495
4496         return 0;
4497 }
4498
4499 static int mlxsw_sp_netdevice_port_event(struct net_device *dev,
4500                                          unsigned long event, void *ptr)
4501 {
4502         switch (event) {
4503         case NETDEV_PRECHANGEUPPER:
4504         case NETDEV_CHANGEUPPER:
4505                 return mlxsw_sp_netdevice_port_upper_event(dev, event, ptr);
4506         case NETDEV_CHANGELOWERSTATE:
4507                 return mlxsw_sp_netdevice_port_lower_event(dev, event, ptr);
4508         }
4509
4510         return 0;
4511 }
4512
4513 static int mlxsw_sp_netdevice_lag_event(struct net_device *lag_dev,
4514                                         unsigned long event, void *ptr)
4515 {
4516         struct net_device *dev;
4517         struct list_head *iter;
4518         int ret;
4519
4520         netdev_for_each_lower_dev(lag_dev, dev, iter) {
4521                 if (mlxsw_sp_port_dev_check(dev)) {
4522                         ret = mlxsw_sp_netdevice_port_event(dev, event, ptr);
4523                         if (ret)
4524                                 return ret;
4525                 }
4526         }
4527
4528         return 0;
4529 }
4530
4531 static int mlxsw_sp_master_bridge_vlan_link(struct mlxsw_sp *mlxsw_sp,
4532                                             struct net_device *vlan_dev)
4533 {
4534         u16 fid = vlan_dev_vlan_id(vlan_dev);
4535         struct mlxsw_sp_fid *f;
4536
4537         f = mlxsw_sp_fid_find(mlxsw_sp, fid);
4538         if (!f) {
4539                 f = mlxsw_sp_fid_create(mlxsw_sp, fid);
4540                 if (IS_ERR(f))
4541                         return PTR_ERR(f);
4542         }
4543
4544         f->ref_count++;
4545
4546         return 0;
4547 }
4548
4549 static void mlxsw_sp_master_bridge_vlan_unlink(struct mlxsw_sp *mlxsw_sp,
4550                                                struct net_device *vlan_dev)
4551 {
4552         u16 fid = vlan_dev_vlan_id(vlan_dev);
4553         struct mlxsw_sp_fid *f;
4554
4555         f = mlxsw_sp_fid_find(mlxsw_sp, fid);
4556         if (f && f->r)
4557                 mlxsw_sp_rif_bridge_destroy(mlxsw_sp, f->r);
4558         if (f && --f->ref_count == 0)
4559                 mlxsw_sp_fid_destroy(mlxsw_sp, f);
4560 }
4561
4562 static int mlxsw_sp_netdevice_bridge_event(struct net_device *br_dev,
4563                                            unsigned long event, void *ptr)
4564 {
4565         struct netdev_notifier_changeupper_info *info;
4566         struct net_device *upper_dev;
4567         struct mlxsw_sp *mlxsw_sp;
4568         int err = 0;
4569
4570         mlxsw_sp = mlxsw_sp_lower_get(br_dev);
4571         if (!mlxsw_sp)
4572                 return 0;
4573
4574         info = ptr;
4575
4576         switch (event) {
4577         case NETDEV_PRECHANGEUPPER:
4578                 upper_dev = info->upper_dev;
4579                 if (!is_vlan_dev(upper_dev))
4580                         return -EINVAL;
4581                 if (is_vlan_dev(upper_dev) &&
4582                     br_dev != mlxsw_sp->master_bridge.dev)
4583                         return -EINVAL;
4584                 break;
4585         case NETDEV_CHANGEUPPER:
4586                 upper_dev = info->upper_dev;
4587                 if (is_vlan_dev(upper_dev)) {
4588                         if (info->linking)
4589                                 err = mlxsw_sp_master_bridge_vlan_link(mlxsw_sp,
4590                                                                        upper_dev);
4591                         else
4592                                 mlxsw_sp_master_bridge_vlan_unlink(mlxsw_sp,
4593                                                                    upper_dev);
4594                 } else {
4595                         err = -EINVAL;
4596                         WARN_ON(1);
4597                 }
4598                 break;
4599         }
4600
4601         return err;
4602 }
4603
4604 static u16 mlxsw_sp_avail_vfid_get(const struct mlxsw_sp *mlxsw_sp)
4605 {
4606         return find_first_zero_bit(mlxsw_sp->vfids.mapped,
4607                                    MLXSW_SP_VFID_MAX);
4608 }
4609
4610 static int mlxsw_sp_vfid_op(struct mlxsw_sp *mlxsw_sp, u16 fid, bool create)
4611 {
4612         char sfmr_pl[MLXSW_REG_SFMR_LEN];
4613
4614         mlxsw_reg_sfmr_pack(sfmr_pl, !create, fid, 0);
4615         return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfmr), sfmr_pl);
4616 }
4617
4618 static void mlxsw_sp_vport_vfid_leave(struct mlxsw_sp_port *mlxsw_sp_vport);
4619
4620 static struct mlxsw_sp_fid *mlxsw_sp_vfid_create(struct mlxsw_sp *mlxsw_sp,
4621                                                  struct net_device *br_dev)
4622 {
4623         struct device *dev = mlxsw_sp->bus_info->dev;
4624         struct mlxsw_sp_fid *f;
4625         u16 vfid, fid;
4626         int err;
4627
4628         vfid = mlxsw_sp_avail_vfid_get(mlxsw_sp);
4629         if (vfid == MLXSW_SP_VFID_MAX) {
4630                 dev_err(dev, "No available vFIDs\n");
4631                 return ERR_PTR(-ERANGE);
4632         }
4633
4634         fid = mlxsw_sp_vfid_to_fid(vfid);
4635         err = mlxsw_sp_vfid_op(mlxsw_sp, fid, true);
4636         if (err) {
4637                 dev_err(dev, "Failed to create FID=%d\n", fid);
4638                 return ERR_PTR(err);
4639         }
4640
4641         f = kzalloc(sizeof(*f), GFP_KERNEL);
4642         if (!f)
4643                 goto err_allocate_vfid;
4644
4645         f->leave = mlxsw_sp_vport_vfid_leave;
4646         f->fid = fid;
4647         f->dev = br_dev;
4648
4649         list_add(&f->list, &mlxsw_sp->vfids.list);
4650         set_bit(vfid, mlxsw_sp->vfids.mapped);
4651
4652         return f;
4653
4654 err_allocate_vfid:
4655         mlxsw_sp_vfid_op(mlxsw_sp, fid, false);
4656         return ERR_PTR(-ENOMEM);
4657 }
4658
4659 static void mlxsw_sp_vfid_destroy(struct mlxsw_sp *mlxsw_sp,
4660                                   struct mlxsw_sp_fid *f)
4661 {
4662         u16 vfid = mlxsw_sp_fid_to_vfid(f->fid);
4663         u16 fid = f->fid;
4664
4665         clear_bit(vfid, mlxsw_sp->vfids.mapped);
4666         list_del(&f->list);
4667
4668         if (f->r)
4669                 mlxsw_sp_rif_bridge_destroy(mlxsw_sp, f->r);
4670
4671         kfree(f);
4672
4673         mlxsw_sp_vfid_op(mlxsw_sp, fid, false);
4674 }
4675
4676 static int mlxsw_sp_vport_fid_map(struct mlxsw_sp_port *mlxsw_sp_vport, u16 fid,
4677                                   bool valid)
4678 {
4679         enum mlxsw_reg_svfa_mt mt = MLXSW_REG_SVFA_MT_PORT_VID_TO_FID;
4680         u16 vid = mlxsw_sp_vport_vid_get(mlxsw_sp_vport);
4681
4682         return mlxsw_sp_port_vid_to_fid_set(mlxsw_sp_vport, mt, valid, fid,
4683                                             vid);
4684 }
4685
4686 static int mlxsw_sp_vport_vfid_join(struct mlxsw_sp_port *mlxsw_sp_vport,
4687                                     struct net_device *br_dev)
4688 {
4689         struct mlxsw_sp_fid *f;
4690         int err;
4691
4692         f = mlxsw_sp_vfid_find(mlxsw_sp_vport->mlxsw_sp, br_dev);
4693         if (!f) {
4694                 f = mlxsw_sp_vfid_create(mlxsw_sp_vport->mlxsw_sp, br_dev);
4695                 if (IS_ERR(f))
4696                         return PTR_ERR(f);
4697         }
4698
4699         err = mlxsw_sp_vport_flood_set(mlxsw_sp_vport, f->fid, true);
4700         if (err)
4701                 goto err_vport_flood_set;
4702
4703         err = mlxsw_sp_vport_fid_map(mlxsw_sp_vport, f->fid, true);
4704         if (err)
4705                 goto err_vport_fid_map;
4706
4707         mlxsw_sp_vport_fid_set(mlxsw_sp_vport, f);
4708         f->ref_count++;
4709
4710         netdev_dbg(mlxsw_sp_vport->dev, "Joined FID=%d\n", f->fid);
4711
4712         return 0;
4713
4714 err_vport_fid_map:
4715         mlxsw_sp_vport_flood_set(mlxsw_sp_vport, f->fid, false);
4716 err_vport_flood_set:
4717         if (!f->ref_count)
4718                 mlxsw_sp_vfid_destroy(mlxsw_sp_vport->mlxsw_sp, f);
4719         return err;
4720 }
4721
4722 static void mlxsw_sp_vport_vfid_leave(struct mlxsw_sp_port *mlxsw_sp_vport)
4723 {
4724         struct mlxsw_sp_fid *f = mlxsw_sp_vport_fid_get(mlxsw_sp_vport);
4725
4726         netdev_dbg(mlxsw_sp_vport->dev, "Left FID=%d\n", f->fid);
4727
4728         mlxsw_sp_vport_fid_map(mlxsw_sp_vport, f->fid, false);
4729
4730         mlxsw_sp_vport_flood_set(mlxsw_sp_vport, f->fid, false);
4731
4732         mlxsw_sp_port_fdb_flush(mlxsw_sp_vport, f->fid);
4733
4734         mlxsw_sp_vport_fid_set(mlxsw_sp_vport, NULL);
4735         if (--f->ref_count == 0)
4736                 mlxsw_sp_vfid_destroy(mlxsw_sp_vport->mlxsw_sp, f);
4737 }
4738
4739 static int mlxsw_sp_vport_bridge_join(struct mlxsw_sp_port *mlxsw_sp_vport,
4740                                       struct net_device *br_dev)
4741 {
4742         struct mlxsw_sp_fid *f = mlxsw_sp_vport_fid_get(mlxsw_sp_vport);
4743         u16 vid = mlxsw_sp_vport_vid_get(mlxsw_sp_vport);
4744         struct net_device *dev = mlxsw_sp_vport->dev;
4745         int err;
4746
4747         if (f && !WARN_ON(!f->leave))
4748                 f->leave(mlxsw_sp_vport);
4749
4750         err = mlxsw_sp_vport_vfid_join(mlxsw_sp_vport, br_dev);
4751         if (err) {
4752                 netdev_err(dev, "Failed to join vFID\n");
4753                 return err;
4754         }
4755
4756         err = mlxsw_sp_port_vid_learning_set(mlxsw_sp_vport, vid, true);
4757         if (err) {
4758                 netdev_err(dev, "Failed to enable learning\n");
4759                 goto err_port_vid_learning_set;
4760         }
4761
4762         mlxsw_sp_vport->learning = 1;
4763         mlxsw_sp_vport->learning_sync = 1;
4764         mlxsw_sp_vport->uc_flood = 1;
4765         mlxsw_sp_vport->mc_flood = 1;
4766         mlxsw_sp_vport->mc_router = 0;
4767         mlxsw_sp_vport->mc_disabled = 1;
4768         mlxsw_sp_vport->bridged = 1;
4769
4770         return 0;
4771
4772 err_port_vid_learning_set:
4773         mlxsw_sp_vport_vfid_leave(mlxsw_sp_vport);
4774         return err;
4775 }
4776
4777 static void mlxsw_sp_vport_bridge_leave(struct mlxsw_sp_port *mlxsw_sp_vport)
4778 {
4779         u16 vid = mlxsw_sp_vport_vid_get(mlxsw_sp_vport);
4780
4781         mlxsw_sp_port_vid_learning_set(mlxsw_sp_vport, vid, false);
4782
4783         mlxsw_sp_vport_vfid_leave(mlxsw_sp_vport);
4784
4785         mlxsw_sp_vport->learning = 0;
4786         mlxsw_sp_vport->learning_sync = 0;
4787         mlxsw_sp_vport->uc_flood = 0;
4788         mlxsw_sp_vport->mc_flood = 0;
4789         mlxsw_sp_vport->mc_router = 0;
4790         mlxsw_sp_vport->bridged = 0;
4791 }
4792
4793 static bool
4794 mlxsw_sp_port_master_bridge_check(const struct mlxsw_sp_port *mlxsw_sp_port,
4795                                   const struct net_device *br_dev)
4796 {
4797         struct mlxsw_sp_port *mlxsw_sp_vport;
4798
4799         list_for_each_entry(mlxsw_sp_vport, &mlxsw_sp_port->vports_list,
4800                             vport.list) {
4801                 struct net_device *dev = mlxsw_sp_vport_dev_get(mlxsw_sp_vport);
4802
4803                 if (dev && dev == br_dev)
4804                         return false;
4805         }
4806
4807         return true;
4808 }
4809
4810 static int mlxsw_sp_netdevice_vport_event(struct net_device *dev,
4811                                           unsigned long event, void *ptr,
4812                                           u16 vid)
4813 {
4814         struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
4815         struct netdev_notifier_changeupper_info *info = ptr;
4816         struct mlxsw_sp_port *mlxsw_sp_vport;
4817         struct net_device *upper_dev;
4818         int err = 0;
4819
4820         mlxsw_sp_vport = mlxsw_sp_port_vport_find(mlxsw_sp_port, vid);
4821         if (!mlxsw_sp_vport)
4822                 return 0;
4823
4824         switch (event) {
4825         case NETDEV_PRECHANGEUPPER:
4826                 upper_dev = info->upper_dev;
4827                 if (!netif_is_bridge_master(upper_dev))
4828                         return -EINVAL;
4829                 if (!info->linking)
4830                         break;
4831                 /* We can't have multiple VLAN interfaces configured on
4832                  * the same port and being members in the same bridge.
4833                  */
4834                 if (!mlxsw_sp_port_master_bridge_check(mlxsw_sp_port,
4835                                                        upper_dev))
4836                         return -EINVAL;
4837                 break;
4838         case NETDEV_CHANGEUPPER:
4839                 upper_dev = info->upper_dev;
4840                 if (netif_is_bridge_master(upper_dev)) {
4841                         if (info->linking)
4842                                 err = mlxsw_sp_vport_bridge_join(mlxsw_sp_vport,
4843                                                                  upper_dev);
4844                         else
4845                                 mlxsw_sp_vport_bridge_leave(mlxsw_sp_vport);
4846                 } else {
4847                         err = -EINVAL;
4848                         WARN_ON(1);
4849                 }
4850                 break;
4851         }
4852
4853         return err;
4854 }
4855
4856 static int mlxsw_sp_netdevice_lag_vport_event(struct net_device *lag_dev,
4857                                               unsigned long event, void *ptr,
4858                                               u16 vid)
4859 {
4860         struct net_device *dev;
4861         struct list_head *iter;
4862         int ret;
4863
4864         netdev_for_each_lower_dev(lag_dev, dev, iter) {
4865                 if (mlxsw_sp_port_dev_check(dev)) {
4866                         ret = mlxsw_sp_netdevice_vport_event(dev, event, ptr,
4867                                                              vid);
4868                         if (ret)
4869                                 return ret;
4870                 }
4871         }
4872
4873         return 0;
4874 }
4875
4876 static int mlxsw_sp_netdevice_vlan_event(struct net_device *vlan_dev,
4877                                          unsigned long event, void *ptr)
4878 {
4879         struct net_device *real_dev = vlan_dev_real_dev(vlan_dev);
4880         u16 vid = vlan_dev_vlan_id(vlan_dev);
4881
4882         if (mlxsw_sp_port_dev_check(real_dev))
4883                 return mlxsw_sp_netdevice_vport_event(real_dev, event, ptr,
4884                                                       vid);
4885         else if (netif_is_lag_master(real_dev))
4886                 return mlxsw_sp_netdevice_lag_vport_event(real_dev, event, ptr,
4887                                                           vid);
4888
4889         return 0;
4890 }
4891
4892 static int mlxsw_sp_netdevice_event(struct notifier_block *unused,
4893                                     unsigned long event, void *ptr)
4894 {
4895         struct net_device *dev = netdev_notifier_info_to_dev(ptr);
4896         int err = 0;
4897
4898         if (event == NETDEV_CHANGEADDR || event == NETDEV_CHANGEMTU)
4899                 err = mlxsw_sp_netdevice_router_port_event(dev);
4900         else if (mlxsw_sp_port_dev_check(dev))
4901                 err = mlxsw_sp_netdevice_port_event(dev, event, ptr);
4902         else if (netif_is_lag_master(dev))
4903                 err = mlxsw_sp_netdevice_lag_event(dev, event, ptr);
4904         else if (netif_is_bridge_master(dev))
4905                 err = mlxsw_sp_netdevice_bridge_event(dev, event, ptr);
4906         else if (is_vlan_dev(dev))
4907                 err = mlxsw_sp_netdevice_vlan_event(dev, event, ptr);
4908
4909         return notifier_from_errno(err);
4910 }
4911
4912 static struct notifier_block mlxsw_sp_netdevice_nb __read_mostly = {
4913         .notifier_call = mlxsw_sp_netdevice_event,
4914 };
4915
4916 static struct notifier_block mlxsw_sp_inetaddr_nb __read_mostly = {
4917         .notifier_call = mlxsw_sp_inetaddr_event,
4918         .priority = 10, /* Must be called before FIB notifier block */
4919 };
4920
4921 static struct notifier_block mlxsw_sp_router_netevent_nb __read_mostly = {
4922         .notifier_call = mlxsw_sp_router_netevent_event,
4923 };
4924
4925 static const struct pci_device_id mlxsw_sp_pci_id_table[] = {
4926         {PCI_VDEVICE(MELLANOX, PCI_DEVICE_ID_MELLANOX_SPECTRUM), 0},
4927         {0, },
4928 };
4929
4930 static struct pci_driver mlxsw_sp_pci_driver = {
4931         .name = mlxsw_sp_driver_name,
4932         .id_table = mlxsw_sp_pci_id_table,
4933 };
4934
4935 static int __init mlxsw_sp_module_init(void)
4936 {
4937         int err;
4938
4939         register_netdevice_notifier(&mlxsw_sp_netdevice_nb);
4940         register_inetaddr_notifier(&mlxsw_sp_inetaddr_nb);
4941         register_netevent_notifier(&mlxsw_sp_router_netevent_nb);
4942
4943         err = mlxsw_core_driver_register(&mlxsw_sp_driver);
4944         if (err)
4945                 goto err_core_driver_register;
4946
4947         err = mlxsw_pci_driver_register(&mlxsw_sp_pci_driver);
4948         if (err)
4949                 goto err_pci_driver_register;
4950
4951         return 0;
4952
4953 err_pci_driver_register:
4954         mlxsw_core_driver_unregister(&mlxsw_sp_driver);
4955 err_core_driver_register:
4956         unregister_netevent_notifier(&mlxsw_sp_router_netevent_nb);
4957         unregister_inetaddr_notifier(&mlxsw_sp_inetaddr_nb);
4958         unregister_netdevice_notifier(&mlxsw_sp_netdevice_nb);
4959         return err;
4960 }
4961
4962 static void __exit mlxsw_sp_module_exit(void)
4963 {
4964         mlxsw_pci_driver_unregister(&mlxsw_sp_pci_driver);
4965         mlxsw_core_driver_unregister(&mlxsw_sp_driver);
4966         unregister_netevent_notifier(&mlxsw_sp_router_netevent_nb);
4967         unregister_inetaddr_notifier(&mlxsw_sp_inetaddr_nb);
4968         unregister_netdevice_notifier(&mlxsw_sp_netdevice_nb);
4969 }
4970
4971 module_init(mlxsw_sp_module_init);
4972 module_exit(mlxsw_sp_module_exit);
4973
4974 MODULE_LICENSE("Dual BSD/GPL");
4975 MODULE_AUTHOR("Jiri Pirko <jiri@mellanox.com>");
4976 MODULE_DESCRIPTION("Mellanox Spectrum driver");
4977 MODULE_DEVICE_TABLE(pci, mlxsw_sp_pci_id_table);