2 * drivers/net/ethernet/mellanox/mlxsw/spectrum.c
3 * Copyright (c) 2015 Mellanox Technologies. All rights reserved.
4 * Copyright (c) 2015 Jiri Pirko <jiri@mellanox.com>
5 * Copyright (c) 2015 Ido Schimmel <idosch@mellanox.com>
6 * Copyright (c) 2015 Elad Raz <eladr@mellanox.com>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
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.
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.
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.
37 #include <linux/kernel.h>
38 #include <linux/module.h>
39 #include <linux/types.h>
40 #include <linux/netdevice.h>
41 #include <linux/etherdevice.h>
42 #include <linux/ethtool.h>
43 #include <linux/slab.h>
44 #include <linux/device.h>
45 #include <linux/skbuff.h>
46 #include <linux/if_vlan.h>
47 #include <linux/if_bridge.h>
48 #include <linux/workqueue.h>
49 #include <linux/jiffies.h>
50 #include <linux/bitops.h>
51 #include <linux/list.h>
52 #include <linux/notifier.h>
53 #include <linux/dcbnl.h>
54 #include <linux/inetdevice.h>
55 #include <net/switchdev.h>
56 #include <generated/utsrelease.h>
57 #include <net/pkt_cls.h>
58 #include <net/tc_act/tc_mirred.h>
59 #include <net/netevent.h>
68 static const char mlxsw_sp_driver_name
[] = "mlxsw_spectrum";
69 static const char mlxsw_sp_driver_version
[] = "1.0";
75 MLXSW_ITEM32(tx
, hdr
, version
, 0x00, 28, 4);
78 * Packet control type.
79 * 0 - Ethernet control (e.g. EMADs, LACP)
82 MLXSW_ITEM32(tx
, hdr
, ctl
, 0x00, 26, 2);
85 * Packet protocol type. Must be set to 1 (Ethernet).
87 MLXSW_ITEM32(tx
, hdr
, proto
, 0x00, 21, 3);
89 /* tx_hdr_rx_is_router
90 * Packet is sent from the router. Valid for data packets only.
92 MLXSW_ITEM32(tx
, hdr
, rx_is_router
, 0x00, 19, 1);
95 * Indicates if the 'fid' field is valid and should be used for
96 * forwarding lookup. Valid for data packets only.
98 MLXSW_ITEM32(tx
, hdr
, fid_valid
, 0x00, 16, 1);
101 * Switch partition ID. Must be set to 0.
103 MLXSW_ITEM32(tx
, hdr
, swid
, 0x00, 12, 3);
105 /* tx_hdr_control_tclass
106 * Indicates if the packet should use the control TClass and not one
107 * of the data TClasses.
109 MLXSW_ITEM32(tx
, hdr
, control_tclass
, 0x00, 6, 1);
112 * Egress TClass to be used on the egress device on the egress port.
114 MLXSW_ITEM32(tx
, hdr
, etclass
, 0x00, 0, 4);
117 * Destination local port for unicast packets.
118 * Destination multicast ID for multicast packets.
120 * Control packets are directed to a specific egress port, while data
121 * packets are transmitted through the CPU port (0) into the switch partition,
122 * where forwarding rules are applied.
124 MLXSW_ITEM32(tx
, hdr
, port_mid
, 0x04, 16, 16);
127 * Forwarding ID used for L2 forwarding lookup. Valid only if 'fid_valid' is
128 * set, otherwise calculated based on the packet's VID using VID to FID mapping.
129 * Valid for data packets only.
131 MLXSW_ITEM32(tx
, hdr
, fid
, 0x08, 0, 16);
135 * 6 - Control packets
137 MLXSW_ITEM32(tx
, hdr
, type
, 0x0C, 0, 4);
139 static bool mlxsw_sp_port_dev_check(const struct net_device
*dev
);
141 static void mlxsw_sp_txhdr_construct(struct sk_buff
*skb
,
142 const struct mlxsw_tx_info
*tx_info
)
144 char *txhdr
= skb_push(skb
, MLXSW_TXHDR_LEN
);
146 memset(txhdr
, 0, MLXSW_TXHDR_LEN
);
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
);
157 static int mlxsw_sp_base_mac_get(struct mlxsw_sp
*mlxsw_sp
)
159 char spad_pl
[MLXSW_REG_SPAD_LEN
];
162 err
= mlxsw_reg_query(mlxsw_sp
->core
, MLXSW_REG(spad
), spad_pl
);
165 mlxsw_reg_spad_base_mac_memcpy_from(spad_pl
, mlxsw_sp
->base_mac
);
169 static int mlxsw_sp_span_init(struct mlxsw_sp
*mlxsw_sp
)
171 struct mlxsw_resources
*resources
;
174 resources
= mlxsw_core_resources_get(mlxsw_sp
->core
);
175 if (!resources
->max_span_valid
)
178 mlxsw_sp
->span
.entries_count
= resources
->max_span
;
179 mlxsw_sp
->span
.entries
= kcalloc(mlxsw_sp
->span
.entries_count
,
180 sizeof(struct mlxsw_sp_span_entry
),
182 if (!mlxsw_sp
->span
.entries
)
185 for (i
= 0; i
< mlxsw_sp
->span
.entries_count
; i
++)
186 INIT_LIST_HEAD(&mlxsw_sp
->span
.entries
[i
].bound_ports_list
);
191 static void mlxsw_sp_span_fini(struct mlxsw_sp
*mlxsw_sp
)
195 for (i
= 0; i
< mlxsw_sp
->span
.entries_count
; i
++) {
196 struct mlxsw_sp_span_entry
*curr
= &mlxsw_sp
->span
.entries
[i
];
198 WARN_ON_ONCE(!list_empty(&curr
->bound_ports_list
));
200 kfree(mlxsw_sp
->span
.entries
);
203 static struct mlxsw_sp_span_entry
*
204 mlxsw_sp_span_entry_create(struct mlxsw_sp_port
*port
)
206 struct mlxsw_sp
*mlxsw_sp
= port
->mlxsw_sp
;
207 struct mlxsw_sp_span_entry
*span_entry
;
208 char mpat_pl
[MLXSW_REG_MPAT_LEN
];
209 u8 local_port
= port
->local_port
;
214 /* find a free entry to use */
216 for (i
= 0; i
< mlxsw_sp
->span
.entries_count
; i
++) {
217 if (!mlxsw_sp
->span
.entries
[i
].used
) {
219 span_entry
= &mlxsw_sp
->span
.entries
[i
];
226 /* create a new port analayzer entry for local_port */
227 mlxsw_reg_mpat_pack(mpat_pl
, index
, local_port
, true);
228 err
= mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(mpat
), mpat_pl
);
232 span_entry
->used
= true;
233 span_entry
->id
= index
;
234 span_entry
->ref_count
= 0;
235 span_entry
->local_port
= local_port
;
239 static void mlxsw_sp_span_entry_destroy(struct mlxsw_sp
*mlxsw_sp
,
240 struct mlxsw_sp_span_entry
*span_entry
)
242 u8 local_port
= span_entry
->local_port
;
243 char mpat_pl
[MLXSW_REG_MPAT_LEN
];
244 int pa_id
= span_entry
->id
;
246 mlxsw_reg_mpat_pack(mpat_pl
, pa_id
, local_port
, false);
247 mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(mpat
), mpat_pl
);
248 span_entry
->used
= false;
251 struct mlxsw_sp_span_entry
*mlxsw_sp_span_entry_find(struct mlxsw_sp_port
*port
)
253 struct mlxsw_sp
*mlxsw_sp
= port
->mlxsw_sp
;
256 for (i
= 0; i
< mlxsw_sp
->span
.entries_count
; i
++) {
257 struct mlxsw_sp_span_entry
*curr
= &mlxsw_sp
->span
.entries
[i
];
259 if (curr
->used
&& curr
->local_port
== port
->local_port
)
265 struct mlxsw_sp_span_entry
*mlxsw_sp_span_entry_get(struct mlxsw_sp_port
*port
)
267 struct mlxsw_sp_span_entry
*span_entry
;
269 span_entry
= mlxsw_sp_span_entry_find(port
);
271 span_entry
->ref_count
++;
275 return mlxsw_sp_span_entry_create(port
);
278 static int mlxsw_sp_span_entry_put(struct mlxsw_sp
*mlxsw_sp
,
279 struct mlxsw_sp_span_entry
*span_entry
)
281 if (--span_entry
->ref_count
== 0)
282 mlxsw_sp_span_entry_destroy(mlxsw_sp
, span_entry
);
286 static bool mlxsw_sp_span_is_egress_mirror(struct mlxsw_sp_port
*port
)
288 struct mlxsw_sp
*mlxsw_sp
= port
->mlxsw_sp
;
289 struct mlxsw_sp_span_inspected_port
*p
;
292 for (i
= 0; i
< mlxsw_sp
->span
.entries_count
; i
++) {
293 struct mlxsw_sp_span_entry
*curr
= &mlxsw_sp
->span
.entries
[i
];
295 list_for_each_entry(p
, &curr
->bound_ports_list
, list
)
296 if (p
->local_port
== port
->local_port
&&
297 p
->type
== MLXSW_SP_SPAN_EGRESS
)
304 static int mlxsw_sp_span_mtu_to_buffsize(int mtu
)
306 return MLXSW_SP_BYTES_TO_CELLS(mtu
* 5 / 2) + 1;
309 static int mlxsw_sp_span_port_mtu_update(struct mlxsw_sp_port
*port
, u16 mtu
)
311 struct mlxsw_sp
*mlxsw_sp
= port
->mlxsw_sp
;
312 char sbib_pl
[MLXSW_REG_SBIB_LEN
];
315 /* If port is egress mirrored, the shared buffer size should be
316 * updated according to the mtu value
318 if (mlxsw_sp_span_is_egress_mirror(port
)) {
319 mlxsw_reg_sbib_pack(sbib_pl
, port
->local_port
,
320 mlxsw_sp_span_mtu_to_buffsize(mtu
));
321 err
= mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(sbib
), sbib_pl
);
323 netdev_err(port
->dev
, "Could not update shared buffer for mirroring\n");
331 static struct mlxsw_sp_span_inspected_port
*
332 mlxsw_sp_span_entry_bound_port_find(struct mlxsw_sp_port
*port
,
333 struct mlxsw_sp_span_entry
*span_entry
)
335 struct mlxsw_sp_span_inspected_port
*p
;
337 list_for_each_entry(p
, &span_entry
->bound_ports_list
, list
)
338 if (port
->local_port
== p
->local_port
)
344 mlxsw_sp_span_inspected_port_bind(struct mlxsw_sp_port
*port
,
345 struct mlxsw_sp_span_entry
*span_entry
,
346 enum mlxsw_sp_span_type type
)
348 struct mlxsw_sp_span_inspected_port
*inspected_port
;
349 struct mlxsw_sp
*mlxsw_sp
= port
->mlxsw_sp
;
350 char mpar_pl
[MLXSW_REG_MPAR_LEN
];
351 char sbib_pl
[MLXSW_REG_SBIB_LEN
];
352 int pa_id
= span_entry
->id
;
355 /* if it is an egress SPAN, bind a shared buffer to it */
356 if (type
== MLXSW_SP_SPAN_EGRESS
) {
357 mlxsw_reg_sbib_pack(sbib_pl
, port
->local_port
,
358 mlxsw_sp_span_mtu_to_buffsize(port
->dev
->mtu
));
359 err
= mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(sbib
), sbib_pl
);
361 netdev_err(port
->dev
, "Could not create shared buffer for mirroring\n");
366 /* bind the port to the SPAN entry */
367 mlxsw_reg_mpar_pack(mpar_pl
, port
->local_port
, type
, true, pa_id
);
368 err
= mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(mpar
), mpar_pl
);
370 goto err_mpar_reg_write
;
372 inspected_port
= kzalloc(sizeof(*inspected_port
), GFP_KERNEL
);
373 if (!inspected_port
) {
375 goto err_inspected_port_alloc
;
377 inspected_port
->local_port
= port
->local_port
;
378 inspected_port
->type
= type
;
379 list_add_tail(&inspected_port
->list
, &span_entry
->bound_ports_list
);
384 err_inspected_port_alloc
:
385 if (type
== MLXSW_SP_SPAN_EGRESS
) {
386 mlxsw_reg_sbib_pack(sbib_pl
, port
->local_port
, 0);
387 mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(sbib
), sbib_pl
);
393 mlxsw_sp_span_inspected_port_unbind(struct mlxsw_sp_port
*port
,
394 struct mlxsw_sp_span_entry
*span_entry
,
395 enum mlxsw_sp_span_type type
)
397 struct mlxsw_sp_span_inspected_port
*inspected_port
;
398 struct mlxsw_sp
*mlxsw_sp
= port
->mlxsw_sp
;
399 char mpar_pl
[MLXSW_REG_MPAR_LEN
];
400 char sbib_pl
[MLXSW_REG_SBIB_LEN
];
401 int pa_id
= span_entry
->id
;
403 inspected_port
= mlxsw_sp_span_entry_bound_port_find(port
, span_entry
);
407 /* remove the inspected port */
408 mlxsw_reg_mpar_pack(mpar_pl
, port
->local_port
, type
, false, pa_id
);
409 mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(mpar
), mpar_pl
);
411 /* remove the SBIB buffer if it was egress SPAN */
412 if (type
== MLXSW_SP_SPAN_EGRESS
) {
413 mlxsw_reg_sbib_pack(sbib_pl
, port
->local_port
, 0);
414 mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(sbib
), sbib_pl
);
417 mlxsw_sp_span_entry_put(mlxsw_sp
, span_entry
);
419 list_del(&inspected_port
->list
);
420 kfree(inspected_port
);
423 static int mlxsw_sp_span_mirror_add(struct mlxsw_sp_port
*from
,
424 struct mlxsw_sp_port
*to
,
425 enum mlxsw_sp_span_type type
)
427 struct mlxsw_sp
*mlxsw_sp
= from
->mlxsw_sp
;
428 struct mlxsw_sp_span_entry
*span_entry
;
431 span_entry
= mlxsw_sp_span_entry_get(to
);
435 netdev_dbg(from
->dev
, "Adding inspected port to SPAN entry %d\n",
438 err
= mlxsw_sp_span_inspected_port_bind(from
, span_entry
, type
);
445 mlxsw_sp_span_entry_put(mlxsw_sp
, span_entry
);
449 static void mlxsw_sp_span_mirror_remove(struct mlxsw_sp_port
*from
,
450 struct mlxsw_sp_port
*to
,
451 enum mlxsw_sp_span_type type
)
453 struct mlxsw_sp_span_entry
*span_entry
;
455 span_entry
= mlxsw_sp_span_entry_find(to
);
457 netdev_err(from
->dev
, "no span entry found\n");
461 netdev_dbg(from
->dev
, "removing inspected port from SPAN entry %d\n",
463 mlxsw_sp_span_inspected_port_unbind(from
, span_entry
, type
);
466 static int mlxsw_sp_port_admin_status_set(struct mlxsw_sp_port
*mlxsw_sp_port
,
469 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
470 char paos_pl
[MLXSW_REG_PAOS_LEN
];
472 mlxsw_reg_paos_pack(paos_pl
, mlxsw_sp_port
->local_port
,
473 is_up
? MLXSW_PORT_ADMIN_STATUS_UP
:
474 MLXSW_PORT_ADMIN_STATUS_DOWN
);
475 return mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(paos
), paos_pl
);
478 static int mlxsw_sp_port_dev_addr_set(struct mlxsw_sp_port
*mlxsw_sp_port
,
481 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
482 char ppad_pl
[MLXSW_REG_PPAD_LEN
];
484 mlxsw_reg_ppad_pack(ppad_pl
, true, mlxsw_sp_port
->local_port
);
485 mlxsw_reg_ppad_mac_memcpy_to(ppad_pl
, addr
);
486 return mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(ppad
), ppad_pl
);
489 static int mlxsw_sp_port_dev_addr_init(struct mlxsw_sp_port
*mlxsw_sp_port
)
491 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
492 unsigned char *addr
= mlxsw_sp_port
->dev
->dev_addr
;
494 ether_addr_copy(addr
, mlxsw_sp
->base_mac
);
495 addr
[ETH_ALEN
- 1] += mlxsw_sp_port
->local_port
;
496 return mlxsw_sp_port_dev_addr_set(mlxsw_sp_port
, addr
);
499 static int mlxsw_sp_port_mtu_set(struct mlxsw_sp_port
*mlxsw_sp_port
, u16 mtu
)
501 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
502 char pmtu_pl
[MLXSW_REG_PMTU_LEN
];
506 mtu
+= MLXSW_TXHDR_LEN
+ ETH_HLEN
;
507 mlxsw_reg_pmtu_pack(pmtu_pl
, mlxsw_sp_port
->local_port
, 0);
508 err
= mlxsw_reg_query(mlxsw_sp
->core
, MLXSW_REG(pmtu
), pmtu_pl
);
511 max_mtu
= mlxsw_reg_pmtu_max_mtu_get(pmtu_pl
);
516 mlxsw_reg_pmtu_pack(pmtu_pl
, mlxsw_sp_port
->local_port
, mtu
);
517 return mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(pmtu
), pmtu_pl
);
520 static int __mlxsw_sp_port_swid_set(struct mlxsw_sp
*mlxsw_sp
, u8 local_port
,
523 char pspa_pl
[MLXSW_REG_PSPA_LEN
];
525 mlxsw_reg_pspa_pack(pspa_pl
, swid
, local_port
);
526 return mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(pspa
), pspa_pl
);
529 static int mlxsw_sp_port_swid_set(struct mlxsw_sp_port
*mlxsw_sp_port
, u8 swid
)
531 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
533 return __mlxsw_sp_port_swid_set(mlxsw_sp
, mlxsw_sp_port
->local_port
,
537 static int mlxsw_sp_port_vp_mode_set(struct mlxsw_sp_port
*mlxsw_sp_port
,
540 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
541 char svpe_pl
[MLXSW_REG_SVPE_LEN
];
543 mlxsw_reg_svpe_pack(svpe_pl
, mlxsw_sp_port
->local_port
, enable
);
544 return mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(svpe
), svpe_pl
);
547 int mlxsw_sp_port_vid_to_fid_set(struct mlxsw_sp_port
*mlxsw_sp_port
,
548 enum mlxsw_reg_svfa_mt mt
, bool valid
, u16 fid
,
551 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
552 char svfa_pl
[MLXSW_REG_SVFA_LEN
];
554 mlxsw_reg_svfa_pack(svfa_pl
, mlxsw_sp_port
->local_port
, mt
, valid
,
556 return mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(svfa
), svfa_pl
);
559 int __mlxsw_sp_port_vid_learning_set(struct mlxsw_sp_port
*mlxsw_sp_port
,
560 u16 vid_begin
, u16 vid_end
,
563 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
567 spvmlr_pl
= kmalloc(MLXSW_REG_SPVMLR_LEN
, GFP_KERNEL
);
570 mlxsw_reg_spvmlr_pack(spvmlr_pl
, mlxsw_sp_port
->local_port
, vid_begin
,
571 vid_end
, learn_enable
);
572 err
= mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(spvmlr
), spvmlr_pl
);
577 static int mlxsw_sp_port_vid_learning_set(struct mlxsw_sp_port
*mlxsw_sp_port
,
578 u16 vid
, bool learn_enable
)
580 return __mlxsw_sp_port_vid_learning_set(mlxsw_sp_port
, vid
, vid
,
585 mlxsw_sp_port_system_port_mapping_set(struct mlxsw_sp_port
*mlxsw_sp_port
)
587 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
588 char sspr_pl
[MLXSW_REG_SSPR_LEN
];
590 mlxsw_reg_sspr_pack(sspr_pl
, mlxsw_sp_port
->local_port
);
591 return mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(sspr
), sspr_pl
);
594 static int mlxsw_sp_port_module_info_get(struct mlxsw_sp
*mlxsw_sp
,
595 u8 local_port
, u8
*p_module
,
596 u8
*p_width
, u8
*p_lane
)
598 char pmlp_pl
[MLXSW_REG_PMLP_LEN
];
601 mlxsw_reg_pmlp_pack(pmlp_pl
, local_port
);
602 err
= mlxsw_reg_query(mlxsw_sp
->core
, MLXSW_REG(pmlp
), pmlp_pl
);
605 *p_module
= mlxsw_reg_pmlp_module_get(pmlp_pl
, 0);
606 *p_width
= mlxsw_reg_pmlp_width_get(pmlp_pl
);
607 *p_lane
= mlxsw_reg_pmlp_tx_lane_get(pmlp_pl
, 0);
611 static int mlxsw_sp_port_module_map(struct mlxsw_sp
*mlxsw_sp
, u8 local_port
,
612 u8 module
, u8 width
, u8 lane
)
614 char pmlp_pl
[MLXSW_REG_PMLP_LEN
];
617 mlxsw_reg_pmlp_pack(pmlp_pl
, local_port
);
618 mlxsw_reg_pmlp_width_set(pmlp_pl
, width
);
619 for (i
= 0; i
< width
; i
++) {
620 mlxsw_reg_pmlp_module_set(pmlp_pl
, i
, module
);
621 mlxsw_reg_pmlp_tx_lane_set(pmlp_pl
, i
, lane
+ i
); /* Rx & Tx */
624 return mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(pmlp
), pmlp_pl
);
627 static int mlxsw_sp_port_module_unmap(struct mlxsw_sp
*mlxsw_sp
, u8 local_port
)
629 char pmlp_pl
[MLXSW_REG_PMLP_LEN
];
631 mlxsw_reg_pmlp_pack(pmlp_pl
, local_port
);
632 mlxsw_reg_pmlp_width_set(pmlp_pl
, 0);
633 return mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(pmlp
), pmlp_pl
);
636 static int mlxsw_sp_port_open(struct net_device
*dev
)
638 struct mlxsw_sp_port
*mlxsw_sp_port
= netdev_priv(dev
);
641 err
= mlxsw_sp_port_admin_status_set(mlxsw_sp_port
, true);
644 netif_start_queue(dev
);
648 static int mlxsw_sp_port_stop(struct net_device
*dev
)
650 struct mlxsw_sp_port
*mlxsw_sp_port
= netdev_priv(dev
);
652 netif_stop_queue(dev
);
653 return mlxsw_sp_port_admin_status_set(mlxsw_sp_port
, false);
656 static netdev_tx_t
mlxsw_sp_port_xmit(struct sk_buff
*skb
,
657 struct net_device
*dev
)
659 struct mlxsw_sp_port
*mlxsw_sp_port
= netdev_priv(dev
);
660 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
661 struct mlxsw_sp_port_pcpu_stats
*pcpu_stats
;
662 const struct mlxsw_tx_info tx_info
= {
663 .local_port
= mlxsw_sp_port
->local_port
,
669 if (mlxsw_core_skb_transmit_busy(mlxsw_sp
->core
, &tx_info
))
670 return NETDEV_TX_BUSY
;
672 if (unlikely(skb_headroom(skb
) < MLXSW_TXHDR_LEN
)) {
673 struct sk_buff
*skb_orig
= skb
;
675 skb
= skb_realloc_headroom(skb
, MLXSW_TXHDR_LEN
);
677 this_cpu_inc(mlxsw_sp_port
->pcpu_stats
->tx_dropped
);
678 dev_kfree_skb_any(skb_orig
);
683 if (eth_skb_pad(skb
)) {
684 this_cpu_inc(mlxsw_sp_port
->pcpu_stats
->tx_dropped
);
688 mlxsw_sp_txhdr_construct(skb
, &tx_info
);
689 /* TX header is consumed by HW on the way so we shouldn't count its
690 * bytes as being sent.
692 len
= skb
->len
- MLXSW_TXHDR_LEN
;
694 /* Due to a race we might fail here because of a full queue. In that
695 * unlikely case we simply drop the packet.
697 err
= mlxsw_core_skb_transmit(mlxsw_sp
->core
, skb
, &tx_info
);
700 pcpu_stats
= this_cpu_ptr(mlxsw_sp_port
->pcpu_stats
);
701 u64_stats_update_begin(&pcpu_stats
->syncp
);
702 pcpu_stats
->tx_packets
++;
703 pcpu_stats
->tx_bytes
+= len
;
704 u64_stats_update_end(&pcpu_stats
->syncp
);
706 this_cpu_inc(mlxsw_sp_port
->pcpu_stats
->tx_dropped
);
707 dev_kfree_skb_any(skb
);
712 static void mlxsw_sp_set_rx_mode(struct net_device
*dev
)
716 static int mlxsw_sp_port_set_mac_address(struct net_device
*dev
, void *p
)
718 struct mlxsw_sp_port
*mlxsw_sp_port
= netdev_priv(dev
);
719 struct sockaddr
*addr
= p
;
722 if (!is_valid_ether_addr(addr
->sa_data
))
723 return -EADDRNOTAVAIL
;
725 err
= mlxsw_sp_port_dev_addr_set(mlxsw_sp_port
, addr
->sa_data
);
728 memcpy(dev
->dev_addr
, addr
->sa_data
, dev
->addr_len
);
732 static void mlxsw_sp_pg_buf_pack(char *pbmc_pl
, int pg_index
, int mtu
,
733 bool pause_en
, bool pfc_en
, u16 delay
)
735 u16 pg_size
= 2 * MLXSW_SP_BYTES_TO_CELLS(mtu
);
737 delay
= pfc_en
? mlxsw_sp_pfc_delay_get(mtu
, delay
) :
738 MLXSW_SP_PAUSE_DELAY
;
740 if (pause_en
|| pfc_en
)
741 mlxsw_reg_pbmc_lossless_buffer_pack(pbmc_pl
, pg_index
,
742 pg_size
+ delay
, pg_size
);
744 mlxsw_reg_pbmc_lossy_buffer_pack(pbmc_pl
, pg_index
, pg_size
);
747 int __mlxsw_sp_port_headroom_set(struct mlxsw_sp_port
*mlxsw_sp_port
, int mtu
,
748 u8
*prio_tc
, bool pause_en
,
749 struct ieee_pfc
*my_pfc
)
751 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
752 u8 pfc_en
= !!my_pfc
? my_pfc
->pfc_en
: 0;
753 u16 delay
= !!my_pfc
? my_pfc
->delay
: 0;
754 char pbmc_pl
[MLXSW_REG_PBMC_LEN
];
757 mlxsw_reg_pbmc_pack(pbmc_pl
, mlxsw_sp_port
->local_port
, 0, 0);
758 err
= mlxsw_reg_query(mlxsw_sp
->core
, MLXSW_REG(pbmc
), pbmc_pl
);
762 for (i
= 0; i
< IEEE_8021QAZ_MAX_TCS
; i
++) {
763 bool configure
= false;
766 for (j
= 0; j
< IEEE_8021QAZ_MAX_TCS
; j
++) {
767 if (prio_tc
[j
] == i
) {
768 pfc
= pfc_en
& BIT(j
);
776 mlxsw_sp_pg_buf_pack(pbmc_pl
, i
, mtu
, pause_en
, pfc
, delay
);
779 return mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(pbmc
), pbmc_pl
);
782 static int mlxsw_sp_port_headroom_set(struct mlxsw_sp_port
*mlxsw_sp_port
,
783 int mtu
, bool pause_en
)
785 u8 def_prio_tc
[IEEE_8021QAZ_MAX_TCS
] = {0};
786 bool dcb_en
= !!mlxsw_sp_port
->dcb
.ets
;
787 struct ieee_pfc
*my_pfc
;
790 prio_tc
= dcb_en
? mlxsw_sp_port
->dcb
.ets
->prio_tc
: def_prio_tc
;
791 my_pfc
= dcb_en
? mlxsw_sp_port
->dcb
.pfc
: NULL
;
793 return __mlxsw_sp_port_headroom_set(mlxsw_sp_port
, mtu
, prio_tc
,
797 static int mlxsw_sp_port_change_mtu(struct net_device
*dev
, int mtu
)
799 struct mlxsw_sp_port
*mlxsw_sp_port
= netdev_priv(dev
);
800 bool pause_en
= mlxsw_sp_port_is_pause_en(mlxsw_sp_port
);
803 err
= mlxsw_sp_port_headroom_set(mlxsw_sp_port
, mtu
, pause_en
);
806 err
= mlxsw_sp_span_port_mtu_update(mlxsw_sp_port
, mtu
);
808 goto err_span_port_mtu_update
;
809 err
= mlxsw_sp_port_mtu_set(mlxsw_sp_port
, mtu
);
811 goto err_port_mtu_set
;
816 mlxsw_sp_span_port_mtu_update(mlxsw_sp_port
, dev
->mtu
);
817 err_span_port_mtu_update
:
818 mlxsw_sp_port_headroom_set(mlxsw_sp_port
, dev
->mtu
, pause_en
);
822 static struct rtnl_link_stats64
*
823 mlxsw_sp_port_get_stats64(struct net_device
*dev
,
824 struct rtnl_link_stats64
*stats
)
826 struct mlxsw_sp_port
*mlxsw_sp_port
= netdev_priv(dev
);
827 struct mlxsw_sp_port_pcpu_stats
*p
;
828 u64 rx_packets
, rx_bytes
, tx_packets
, tx_bytes
;
833 for_each_possible_cpu(i
) {
834 p
= per_cpu_ptr(mlxsw_sp_port
->pcpu_stats
, i
);
836 start
= u64_stats_fetch_begin_irq(&p
->syncp
);
837 rx_packets
= p
->rx_packets
;
838 rx_bytes
= p
->rx_bytes
;
839 tx_packets
= p
->tx_packets
;
840 tx_bytes
= p
->tx_bytes
;
841 } while (u64_stats_fetch_retry_irq(&p
->syncp
, start
));
843 stats
->rx_packets
+= rx_packets
;
844 stats
->rx_bytes
+= rx_bytes
;
845 stats
->tx_packets
+= tx_packets
;
846 stats
->tx_bytes
+= tx_bytes
;
847 /* tx_dropped is u32, updated without syncp protection. */
848 tx_dropped
+= p
->tx_dropped
;
850 stats
->tx_dropped
= tx_dropped
;
854 int mlxsw_sp_port_vlan_set(struct mlxsw_sp_port
*mlxsw_sp_port
, u16 vid_begin
,
855 u16 vid_end
, bool is_member
, bool untagged
)
857 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
861 spvm_pl
= kmalloc(MLXSW_REG_SPVM_LEN
, GFP_KERNEL
);
865 mlxsw_reg_spvm_pack(spvm_pl
, mlxsw_sp_port
->local_port
, vid_begin
,
866 vid_end
, is_member
, untagged
);
867 err
= mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(spvm
), spvm_pl
);
872 static int mlxsw_sp_port_vp_mode_trans(struct mlxsw_sp_port
*mlxsw_sp_port
)
874 enum mlxsw_reg_svfa_mt mt
= MLXSW_REG_SVFA_MT_PORT_VID_TO_FID
;
875 u16 vid
, last_visited_vid
;
878 for_each_set_bit(vid
, mlxsw_sp_port
->active_vlans
, VLAN_N_VID
) {
879 err
= mlxsw_sp_port_vid_to_fid_set(mlxsw_sp_port
, mt
, true, vid
,
882 last_visited_vid
= vid
;
883 goto err_port_vid_to_fid_set
;
887 err
= mlxsw_sp_port_vp_mode_set(mlxsw_sp_port
, true);
889 last_visited_vid
= VLAN_N_VID
;
890 goto err_port_vid_to_fid_set
;
895 err_port_vid_to_fid_set
:
896 for_each_set_bit(vid
, mlxsw_sp_port
->active_vlans
, last_visited_vid
)
897 mlxsw_sp_port_vid_to_fid_set(mlxsw_sp_port
, mt
, false, vid
,
902 static int mlxsw_sp_port_vlan_mode_trans(struct mlxsw_sp_port
*mlxsw_sp_port
)
904 enum mlxsw_reg_svfa_mt mt
= MLXSW_REG_SVFA_MT_PORT_VID_TO_FID
;
908 err
= mlxsw_sp_port_vp_mode_set(mlxsw_sp_port
, false);
912 for_each_set_bit(vid
, mlxsw_sp_port
->active_vlans
, VLAN_N_VID
) {
913 err
= mlxsw_sp_port_vid_to_fid_set(mlxsw_sp_port
, mt
, false,
922 static struct mlxsw_sp_port
*
923 mlxsw_sp_port_vport_create(struct mlxsw_sp_port
*mlxsw_sp_port
, u16 vid
)
925 struct mlxsw_sp_port
*mlxsw_sp_vport
;
927 mlxsw_sp_vport
= kzalloc(sizeof(*mlxsw_sp_vport
), GFP_KERNEL
);
931 /* dev will be set correctly after the VLAN device is linked
932 * with the real device. In case of bridge SELF invocation, dev
935 mlxsw_sp_vport
->dev
= mlxsw_sp_port
->dev
;
936 mlxsw_sp_vport
->mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
937 mlxsw_sp_vport
->local_port
= mlxsw_sp_port
->local_port
;
938 mlxsw_sp_vport
->stp_state
= BR_STATE_FORWARDING
;
939 mlxsw_sp_vport
->lagged
= mlxsw_sp_port
->lagged
;
940 mlxsw_sp_vport
->lag_id
= mlxsw_sp_port
->lag_id
;
941 mlxsw_sp_vport
->vport
.vid
= vid
;
943 list_add(&mlxsw_sp_vport
->vport
.list
, &mlxsw_sp_port
->vports_list
);
945 return mlxsw_sp_vport
;
948 static void mlxsw_sp_port_vport_destroy(struct mlxsw_sp_port
*mlxsw_sp_vport
)
950 list_del(&mlxsw_sp_vport
->vport
.list
);
951 kfree(mlxsw_sp_vport
);
954 static int mlxsw_sp_port_add_vid(struct net_device
*dev
,
955 __be16 __always_unused proto
, u16 vid
)
957 struct mlxsw_sp_port
*mlxsw_sp_port
= netdev_priv(dev
);
958 struct mlxsw_sp_port
*mlxsw_sp_vport
;
959 bool untagged
= vid
== 1;
962 /* VLAN 0 is added to HW filter when device goes up, but it is
963 * reserved in our case, so simply return.
968 if (mlxsw_sp_port_vport_find(mlxsw_sp_port
, vid
))
971 mlxsw_sp_vport
= mlxsw_sp_port_vport_create(mlxsw_sp_port
, vid
);
975 /* When adding the first VLAN interface on a bridged port we need to
976 * transition all the active 802.1Q bridge VLANs to use explicit
977 * {Port, VID} to FID mappings and set the port's mode to Virtual mode.
979 if (list_is_singular(&mlxsw_sp_port
->vports_list
)) {
980 err
= mlxsw_sp_port_vp_mode_trans(mlxsw_sp_port
);
982 goto err_port_vp_mode_trans
;
985 err
= mlxsw_sp_port_vlan_set(mlxsw_sp_vport
, vid
, vid
, true, untagged
);
987 goto err_port_add_vid
;
992 if (list_is_singular(&mlxsw_sp_port
->vports_list
))
993 mlxsw_sp_port_vlan_mode_trans(mlxsw_sp_port
);
994 err_port_vp_mode_trans
:
995 mlxsw_sp_port_vport_destroy(mlxsw_sp_vport
);
999 static int mlxsw_sp_port_kill_vid(struct net_device
*dev
,
1000 __be16 __always_unused proto
, u16 vid
)
1002 struct mlxsw_sp_port
*mlxsw_sp_port
= netdev_priv(dev
);
1003 struct mlxsw_sp_port
*mlxsw_sp_vport
;
1004 struct mlxsw_sp_fid
*f
;
1006 /* VLAN 0 is removed from HW filter when device goes down, but
1007 * it is reserved in our case, so simply return.
1012 mlxsw_sp_vport
= mlxsw_sp_port_vport_find(mlxsw_sp_port
, vid
);
1013 if (WARN_ON(!mlxsw_sp_vport
))
1016 mlxsw_sp_port_vlan_set(mlxsw_sp_vport
, vid
, vid
, false, false);
1018 /* Drop FID reference. If this was the last reference the
1019 * resources will be freed.
1021 f
= mlxsw_sp_vport_fid_get(mlxsw_sp_vport
);
1022 if (f
&& !WARN_ON(!f
->leave
))
1023 f
->leave(mlxsw_sp_vport
);
1025 /* When removing the last VLAN interface on a bridged port we need to
1026 * transition all active 802.1Q bridge VLANs to use VID to FID
1027 * mappings and set port's mode to VLAN mode.
1029 if (list_is_singular(&mlxsw_sp_port
->vports_list
))
1030 mlxsw_sp_port_vlan_mode_trans(mlxsw_sp_port
);
1032 mlxsw_sp_port_vport_destroy(mlxsw_sp_vport
);
1037 static int mlxsw_sp_port_get_phys_port_name(struct net_device
*dev
, char *name
,
1040 struct mlxsw_sp_port
*mlxsw_sp_port
= netdev_priv(dev
);
1041 u8 module
= mlxsw_sp_port
->mapping
.module
;
1042 u8 width
= mlxsw_sp_port
->mapping
.width
;
1043 u8 lane
= mlxsw_sp_port
->mapping
.lane
;
1046 if (!mlxsw_sp_port
->split
)
1047 err
= snprintf(name
, len
, "p%d", module
+ 1);
1049 err
= snprintf(name
, len
, "p%ds%d", module
+ 1,
1058 static struct mlxsw_sp_port_mall_tc_entry
*
1059 mlxsw_sp_port_mirror_entry_find(struct mlxsw_sp_port
*port
,
1060 unsigned long cookie
) {
1061 struct mlxsw_sp_port_mall_tc_entry
*mall_tc_entry
;
1063 list_for_each_entry(mall_tc_entry
, &port
->mall_tc_list
, list
)
1064 if (mall_tc_entry
->cookie
== cookie
)
1065 return mall_tc_entry
;
1071 mlxsw_sp_port_add_cls_matchall_mirror(struct mlxsw_sp_port
*mlxsw_sp_port
,
1072 struct tc_cls_matchall_offload
*cls
,
1073 const struct tc_action
*a
,
1076 struct mlxsw_sp_port_mall_tc_entry
*mall_tc_entry
;
1077 struct net
*net
= dev_net(mlxsw_sp_port
->dev
);
1078 enum mlxsw_sp_span_type span_type
;
1079 struct mlxsw_sp_port
*to_port
;
1080 struct net_device
*to_dev
;
1084 ifindex
= tcf_mirred_ifindex(a
);
1085 to_dev
= __dev_get_by_index(net
, ifindex
);
1087 netdev_err(mlxsw_sp_port
->dev
, "Could not find requested device\n");
1091 if (!mlxsw_sp_port_dev_check(to_dev
)) {
1092 netdev_err(mlxsw_sp_port
->dev
, "Cannot mirror to a non-spectrum port");
1095 to_port
= netdev_priv(to_dev
);
1097 mall_tc_entry
= kzalloc(sizeof(*mall_tc_entry
), GFP_KERNEL
);
1101 mall_tc_entry
->cookie
= cls
->cookie
;
1102 mall_tc_entry
->type
= MLXSW_SP_PORT_MALL_MIRROR
;
1103 mall_tc_entry
->mirror
.to_local_port
= to_port
->local_port
;
1104 mall_tc_entry
->mirror
.ingress
= ingress
;
1105 list_add_tail(&mall_tc_entry
->list
, &mlxsw_sp_port
->mall_tc_list
);
1107 span_type
= ingress
? MLXSW_SP_SPAN_INGRESS
: MLXSW_SP_SPAN_EGRESS
;
1108 err
= mlxsw_sp_span_mirror_add(mlxsw_sp_port
, to_port
, span_type
);
1110 goto err_mirror_add
;
1114 list_del(&mall_tc_entry
->list
);
1115 kfree(mall_tc_entry
);
1119 static int mlxsw_sp_port_add_cls_matchall(struct mlxsw_sp_port
*mlxsw_sp_port
,
1121 struct tc_cls_matchall_offload
*cls
,
1124 const struct tc_action
*a
;
1128 if (!tc_single_action(cls
->exts
)) {
1129 netdev_err(mlxsw_sp_port
->dev
, "only singular actions are supported\n");
1133 tcf_exts_to_list(cls
->exts
, &actions
);
1134 list_for_each_entry(a
, &actions
, list
) {
1135 if (!is_tcf_mirred_mirror(a
) || protocol
!= htons(ETH_P_ALL
))
1138 err
= mlxsw_sp_port_add_cls_matchall_mirror(mlxsw_sp_port
, cls
,
1147 static void mlxsw_sp_port_del_cls_matchall(struct mlxsw_sp_port
*mlxsw_sp_port
,
1148 struct tc_cls_matchall_offload
*cls
)
1150 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
1151 struct mlxsw_sp_port_mall_tc_entry
*mall_tc_entry
;
1152 enum mlxsw_sp_span_type span_type
;
1153 struct mlxsw_sp_port
*to_port
;
1155 mall_tc_entry
= mlxsw_sp_port_mirror_entry_find(mlxsw_sp_port
,
1157 if (!mall_tc_entry
) {
1158 netdev_dbg(mlxsw_sp_port
->dev
, "tc entry not found on port\n");
1162 switch (mall_tc_entry
->type
) {
1163 case MLXSW_SP_PORT_MALL_MIRROR
:
1164 to_port
= mlxsw_sp
->ports
[mall_tc_entry
->mirror
.to_local_port
];
1165 span_type
= mall_tc_entry
->mirror
.ingress
?
1166 MLXSW_SP_SPAN_INGRESS
: MLXSW_SP_SPAN_EGRESS
;
1168 mlxsw_sp_span_mirror_remove(mlxsw_sp_port
, to_port
, span_type
);
1174 list_del(&mall_tc_entry
->list
);
1175 kfree(mall_tc_entry
);
1178 static int mlxsw_sp_setup_tc(struct net_device
*dev
, u32 handle
,
1179 __be16 proto
, struct tc_to_netdev
*tc
)
1181 struct mlxsw_sp_port
*mlxsw_sp_port
= netdev_priv(dev
);
1182 bool ingress
= TC_H_MAJ(handle
) == TC_H_MAJ(TC_H_INGRESS
);
1184 if (tc
->type
== TC_SETUP_MATCHALL
) {
1185 switch (tc
->cls_mall
->command
) {
1186 case TC_CLSMATCHALL_REPLACE
:
1187 return mlxsw_sp_port_add_cls_matchall(mlxsw_sp_port
,
1191 case TC_CLSMATCHALL_DESTROY
:
1192 mlxsw_sp_port_del_cls_matchall(mlxsw_sp_port
,
1203 static const struct net_device_ops mlxsw_sp_port_netdev_ops
= {
1204 .ndo_open
= mlxsw_sp_port_open
,
1205 .ndo_stop
= mlxsw_sp_port_stop
,
1206 .ndo_start_xmit
= mlxsw_sp_port_xmit
,
1207 .ndo_setup_tc
= mlxsw_sp_setup_tc
,
1208 .ndo_set_rx_mode
= mlxsw_sp_set_rx_mode
,
1209 .ndo_set_mac_address
= mlxsw_sp_port_set_mac_address
,
1210 .ndo_change_mtu
= mlxsw_sp_port_change_mtu
,
1211 .ndo_get_stats64
= mlxsw_sp_port_get_stats64
,
1212 .ndo_vlan_rx_add_vid
= mlxsw_sp_port_add_vid
,
1213 .ndo_vlan_rx_kill_vid
= mlxsw_sp_port_kill_vid
,
1214 .ndo_neigh_construct
= mlxsw_sp_router_neigh_construct
,
1215 .ndo_neigh_destroy
= mlxsw_sp_router_neigh_destroy
,
1216 .ndo_fdb_add
= switchdev_port_fdb_add
,
1217 .ndo_fdb_del
= switchdev_port_fdb_del
,
1218 .ndo_fdb_dump
= switchdev_port_fdb_dump
,
1219 .ndo_bridge_setlink
= switchdev_port_bridge_setlink
,
1220 .ndo_bridge_getlink
= switchdev_port_bridge_getlink
,
1221 .ndo_bridge_dellink
= switchdev_port_bridge_dellink
,
1222 .ndo_get_phys_port_name
= mlxsw_sp_port_get_phys_port_name
,
1225 static void mlxsw_sp_port_get_drvinfo(struct net_device
*dev
,
1226 struct ethtool_drvinfo
*drvinfo
)
1228 struct mlxsw_sp_port
*mlxsw_sp_port
= netdev_priv(dev
);
1229 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
1231 strlcpy(drvinfo
->driver
, mlxsw_sp_driver_name
, sizeof(drvinfo
->driver
));
1232 strlcpy(drvinfo
->version
, mlxsw_sp_driver_version
,
1233 sizeof(drvinfo
->version
));
1234 snprintf(drvinfo
->fw_version
, sizeof(drvinfo
->fw_version
),
1236 mlxsw_sp
->bus_info
->fw_rev
.major
,
1237 mlxsw_sp
->bus_info
->fw_rev
.minor
,
1238 mlxsw_sp
->bus_info
->fw_rev
.subminor
);
1239 strlcpy(drvinfo
->bus_info
, mlxsw_sp
->bus_info
->device_name
,
1240 sizeof(drvinfo
->bus_info
));
1243 static void mlxsw_sp_port_get_pauseparam(struct net_device
*dev
,
1244 struct ethtool_pauseparam
*pause
)
1246 struct mlxsw_sp_port
*mlxsw_sp_port
= netdev_priv(dev
);
1248 pause
->rx_pause
= mlxsw_sp_port
->link
.rx_pause
;
1249 pause
->tx_pause
= mlxsw_sp_port
->link
.tx_pause
;
1252 static int mlxsw_sp_port_pause_set(struct mlxsw_sp_port
*mlxsw_sp_port
,
1253 struct ethtool_pauseparam
*pause
)
1255 char pfcc_pl
[MLXSW_REG_PFCC_LEN
];
1257 mlxsw_reg_pfcc_pack(pfcc_pl
, mlxsw_sp_port
->local_port
);
1258 mlxsw_reg_pfcc_pprx_set(pfcc_pl
, pause
->rx_pause
);
1259 mlxsw_reg_pfcc_pptx_set(pfcc_pl
, pause
->tx_pause
);
1261 return mlxsw_reg_write(mlxsw_sp_port
->mlxsw_sp
->core
, MLXSW_REG(pfcc
),
1265 static int mlxsw_sp_port_set_pauseparam(struct net_device
*dev
,
1266 struct ethtool_pauseparam
*pause
)
1268 struct mlxsw_sp_port
*mlxsw_sp_port
= netdev_priv(dev
);
1269 bool pause_en
= pause
->tx_pause
|| pause
->rx_pause
;
1272 if (mlxsw_sp_port
->dcb
.pfc
&& mlxsw_sp_port
->dcb
.pfc
->pfc_en
) {
1273 netdev_err(dev
, "PFC already enabled on port\n");
1277 if (pause
->autoneg
) {
1278 netdev_err(dev
, "PAUSE frames autonegotiation isn't supported\n");
1282 err
= mlxsw_sp_port_headroom_set(mlxsw_sp_port
, dev
->mtu
, pause_en
);
1284 netdev_err(dev
, "Failed to configure port's headroom\n");
1288 err
= mlxsw_sp_port_pause_set(mlxsw_sp_port
, pause
);
1290 netdev_err(dev
, "Failed to set PAUSE parameters\n");
1291 goto err_port_pause_configure
;
1294 mlxsw_sp_port
->link
.rx_pause
= pause
->rx_pause
;
1295 mlxsw_sp_port
->link
.tx_pause
= pause
->tx_pause
;
1299 err_port_pause_configure
:
1300 pause_en
= mlxsw_sp_port_is_pause_en(mlxsw_sp_port
);
1301 mlxsw_sp_port_headroom_set(mlxsw_sp_port
, dev
->mtu
, pause_en
);
1305 struct mlxsw_sp_port_hw_stats
{
1306 char str
[ETH_GSTRING_LEN
];
1307 u64 (*getter
)(char *payload
);
1310 static struct mlxsw_sp_port_hw_stats mlxsw_sp_port_hw_stats
[] = {
1312 .str
= "a_frames_transmitted_ok",
1313 .getter
= mlxsw_reg_ppcnt_a_frames_transmitted_ok_get
,
1316 .str
= "a_frames_received_ok",
1317 .getter
= mlxsw_reg_ppcnt_a_frames_received_ok_get
,
1320 .str
= "a_frame_check_sequence_errors",
1321 .getter
= mlxsw_reg_ppcnt_a_frame_check_sequence_errors_get
,
1324 .str
= "a_alignment_errors",
1325 .getter
= mlxsw_reg_ppcnt_a_alignment_errors_get
,
1328 .str
= "a_octets_transmitted_ok",
1329 .getter
= mlxsw_reg_ppcnt_a_octets_transmitted_ok_get
,
1332 .str
= "a_octets_received_ok",
1333 .getter
= mlxsw_reg_ppcnt_a_octets_received_ok_get
,
1336 .str
= "a_multicast_frames_xmitted_ok",
1337 .getter
= mlxsw_reg_ppcnt_a_multicast_frames_xmitted_ok_get
,
1340 .str
= "a_broadcast_frames_xmitted_ok",
1341 .getter
= mlxsw_reg_ppcnt_a_broadcast_frames_xmitted_ok_get
,
1344 .str
= "a_multicast_frames_received_ok",
1345 .getter
= mlxsw_reg_ppcnt_a_multicast_frames_received_ok_get
,
1348 .str
= "a_broadcast_frames_received_ok",
1349 .getter
= mlxsw_reg_ppcnt_a_broadcast_frames_received_ok_get
,
1352 .str
= "a_in_range_length_errors",
1353 .getter
= mlxsw_reg_ppcnt_a_in_range_length_errors_get
,
1356 .str
= "a_out_of_range_length_field",
1357 .getter
= mlxsw_reg_ppcnt_a_out_of_range_length_field_get
,
1360 .str
= "a_frame_too_long_errors",
1361 .getter
= mlxsw_reg_ppcnt_a_frame_too_long_errors_get
,
1364 .str
= "a_symbol_error_during_carrier",
1365 .getter
= mlxsw_reg_ppcnt_a_symbol_error_during_carrier_get
,
1368 .str
= "a_mac_control_frames_transmitted",
1369 .getter
= mlxsw_reg_ppcnt_a_mac_control_frames_transmitted_get
,
1372 .str
= "a_mac_control_frames_received",
1373 .getter
= mlxsw_reg_ppcnt_a_mac_control_frames_received_get
,
1376 .str
= "a_unsupported_opcodes_received",
1377 .getter
= mlxsw_reg_ppcnt_a_unsupported_opcodes_received_get
,
1380 .str
= "a_pause_mac_ctrl_frames_received",
1381 .getter
= mlxsw_reg_ppcnt_a_pause_mac_ctrl_frames_received_get
,
1384 .str
= "a_pause_mac_ctrl_frames_xmitted",
1385 .getter
= mlxsw_reg_ppcnt_a_pause_mac_ctrl_frames_transmitted_get
,
1389 #define MLXSW_SP_PORT_HW_STATS_LEN ARRAY_SIZE(mlxsw_sp_port_hw_stats)
1391 static struct mlxsw_sp_port_hw_stats mlxsw_sp_port_hw_prio_stats
[] = {
1393 .str
= "rx_octets_prio",
1394 .getter
= mlxsw_reg_ppcnt_rx_octets_get
,
1397 .str
= "rx_frames_prio",
1398 .getter
= mlxsw_reg_ppcnt_rx_frames_get
,
1401 .str
= "tx_octets_prio",
1402 .getter
= mlxsw_reg_ppcnt_tx_octets_get
,
1405 .str
= "tx_frames_prio",
1406 .getter
= mlxsw_reg_ppcnt_tx_frames_get
,
1409 .str
= "rx_pause_prio",
1410 .getter
= mlxsw_reg_ppcnt_rx_pause_get
,
1413 .str
= "rx_pause_duration_prio",
1414 .getter
= mlxsw_reg_ppcnt_rx_pause_duration_get
,
1417 .str
= "tx_pause_prio",
1418 .getter
= mlxsw_reg_ppcnt_tx_pause_get
,
1421 .str
= "tx_pause_duration_prio",
1422 .getter
= mlxsw_reg_ppcnt_tx_pause_duration_get
,
1426 #define MLXSW_SP_PORT_HW_PRIO_STATS_LEN ARRAY_SIZE(mlxsw_sp_port_hw_prio_stats)
1428 static u64
mlxsw_reg_ppcnt_tc_transmit_queue_bytes_get(char *ppcnt_pl
)
1430 u64 transmit_queue
= mlxsw_reg_ppcnt_tc_transmit_queue_get(ppcnt_pl
);
1432 return MLXSW_SP_CELLS_TO_BYTES(transmit_queue
);
1435 static struct mlxsw_sp_port_hw_stats mlxsw_sp_port_hw_tc_stats
[] = {
1437 .str
= "tc_transmit_queue_tc",
1438 .getter
= mlxsw_reg_ppcnt_tc_transmit_queue_bytes_get
,
1441 .str
= "tc_no_buffer_discard_uc_tc",
1442 .getter
= mlxsw_reg_ppcnt_tc_no_buffer_discard_uc_get
,
1446 #define MLXSW_SP_PORT_HW_TC_STATS_LEN ARRAY_SIZE(mlxsw_sp_port_hw_tc_stats)
1448 #define MLXSW_SP_PORT_ETHTOOL_STATS_LEN (MLXSW_SP_PORT_HW_STATS_LEN + \
1449 (MLXSW_SP_PORT_HW_PRIO_STATS_LEN + \
1450 MLXSW_SP_PORT_HW_TC_STATS_LEN) * \
1451 IEEE_8021QAZ_MAX_TCS)
1453 static void mlxsw_sp_port_get_prio_strings(u8
**p
, int prio
)
1457 for (i
= 0; i
< MLXSW_SP_PORT_HW_PRIO_STATS_LEN
; i
++) {
1458 snprintf(*p
, ETH_GSTRING_LEN
, "%s_%d",
1459 mlxsw_sp_port_hw_prio_stats
[i
].str
, prio
);
1460 *p
+= ETH_GSTRING_LEN
;
1464 static void mlxsw_sp_port_get_tc_strings(u8
**p
, int tc
)
1468 for (i
= 0; i
< MLXSW_SP_PORT_HW_TC_STATS_LEN
; i
++) {
1469 snprintf(*p
, ETH_GSTRING_LEN
, "%s_%d",
1470 mlxsw_sp_port_hw_tc_stats
[i
].str
, tc
);
1471 *p
+= ETH_GSTRING_LEN
;
1475 static void mlxsw_sp_port_get_strings(struct net_device
*dev
,
1476 u32 stringset
, u8
*data
)
1481 switch (stringset
) {
1483 for (i
= 0; i
< MLXSW_SP_PORT_HW_STATS_LEN
; i
++) {
1484 memcpy(p
, mlxsw_sp_port_hw_stats
[i
].str
,
1486 p
+= ETH_GSTRING_LEN
;
1489 for (i
= 0; i
< IEEE_8021QAZ_MAX_TCS
; i
++)
1490 mlxsw_sp_port_get_prio_strings(&p
, i
);
1492 for (i
= 0; i
< IEEE_8021QAZ_MAX_TCS
; i
++)
1493 mlxsw_sp_port_get_tc_strings(&p
, i
);
1499 static int mlxsw_sp_port_set_phys_id(struct net_device
*dev
,
1500 enum ethtool_phys_id_state state
)
1502 struct mlxsw_sp_port
*mlxsw_sp_port
= netdev_priv(dev
);
1503 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
1504 char mlcr_pl
[MLXSW_REG_MLCR_LEN
];
1508 case ETHTOOL_ID_ACTIVE
:
1511 case ETHTOOL_ID_INACTIVE
:
1518 mlxsw_reg_mlcr_pack(mlcr_pl
, mlxsw_sp_port
->local_port
, active
);
1519 return mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(mlcr
), mlcr_pl
);
1523 mlxsw_sp_get_hw_stats_by_group(struct mlxsw_sp_port_hw_stats
**p_hw_stats
,
1524 int *p_len
, enum mlxsw_reg_ppcnt_grp grp
)
1527 case MLXSW_REG_PPCNT_IEEE_8023_CNT
:
1528 *p_hw_stats
= mlxsw_sp_port_hw_stats
;
1529 *p_len
= MLXSW_SP_PORT_HW_STATS_LEN
;
1531 case MLXSW_REG_PPCNT_PRIO_CNT
:
1532 *p_hw_stats
= mlxsw_sp_port_hw_prio_stats
;
1533 *p_len
= MLXSW_SP_PORT_HW_PRIO_STATS_LEN
;
1535 case MLXSW_REG_PPCNT_TC_CNT
:
1536 *p_hw_stats
= mlxsw_sp_port_hw_tc_stats
;
1537 *p_len
= MLXSW_SP_PORT_HW_TC_STATS_LEN
;
1546 static void __mlxsw_sp_port_get_stats(struct net_device
*dev
,
1547 enum mlxsw_reg_ppcnt_grp grp
, int prio
,
1548 u64
*data
, int data_index
)
1550 struct mlxsw_sp_port
*mlxsw_sp_port
= netdev_priv(dev
);
1551 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
1552 struct mlxsw_sp_port_hw_stats
*hw_stats
;
1553 char ppcnt_pl
[MLXSW_REG_PPCNT_LEN
];
1557 err
= mlxsw_sp_get_hw_stats_by_group(&hw_stats
, &len
, grp
);
1560 mlxsw_reg_ppcnt_pack(ppcnt_pl
, mlxsw_sp_port
->local_port
, grp
, prio
);
1561 err
= mlxsw_reg_query(mlxsw_sp
->core
, MLXSW_REG(ppcnt
), ppcnt_pl
);
1562 for (i
= 0; i
< len
; i
++)
1563 data
[data_index
+ i
] = !err
? hw_stats
[i
].getter(ppcnt_pl
) : 0;
1566 static void mlxsw_sp_port_get_stats(struct net_device
*dev
,
1567 struct ethtool_stats
*stats
, u64
*data
)
1569 int i
, data_index
= 0;
1571 /* IEEE 802.3 Counters */
1572 __mlxsw_sp_port_get_stats(dev
, MLXSW_REG_PPCNT_IEEE_8023_CNT
, 0,
1574 data_index
= MLXSW_SP_PORT_HW_STATS_LEN
;
1576 /* Per-Priority Counters */
1577 for (i
= 0; i
< IEEE_8021QAZ_MAX_TCS
; i
++) {
1578 __mlxsw_sp_port_get_stats(dev
, MLXSW_REG_PPCNT_PRIO_CNT
, i
,
1580 data_index
+= MLXSW_SP_PORT_HW_PRIO_STATS_LEN
;
1583 /* Per-TC Counters */
1584 for (i
= 0; i
< IEEE_8021QAZ_MAX_TCS
; i
++) {
1585 __mlxsw_sp_port_get_stats(dev
, MLXSW_REG_PPCNT_TC_CNT
, i
,
1587 data_index
+= MLXSW_SP_PORT_HW_TC_STATS_LEN
;
1591 static int mlxsw_sp_port_get_sset_count(struct net_device
*dev
, int sset
)
1595 return MLXSW_SP_PORT_ETHTOOL_STATS_LEN
;
1601 struct mlxsw_sp_port_link_mode
{
1608 static const struct mlxsw_sp_port_link_mode mlxsw_sp_port_link_mode
[] = {
1610 .mask
= MLXSW_REG_PTYS_ETH_SPEED_100BASE_T
,
1611 .supported
= SUPPORTED_100baseT_Full
,
1612 .advertised
= ADVERTISED_100baseT_Full
,
1616 .mask
= MLXSW_REG_PTYS_ETH_SPEED_100BASE_TX
,
1620 .mask
= MLXSW_REG_PTYS_ETH_SPEED_SGMII
|
1621 MLXSW_REG_PTYS_ETH_SPEED_1000BASE_KX
,
1622 .supported
= SUPPORTED_1000baseKX_Full
,
1623 .advertised
= ADVERTISED_1000baseKX_Full
,
1627 .mask
= MLXSW_REG_PTYS_ETH_SPEED_10GBASE_T
,
1628 .supported
= SUPPORTED_10000baseT_Full
,
1629 .advertised
= ADVERTISED_10000baseT_Full
,
1633 .mask
= MLXSW_REG_PTYS_ETH_SPEED_10GBASE_CX4
|
1634 MLXSW_REG_PTYS_ETH_SPEED_10GBASE_KX4
,
1635 .supported
= SUPPORTED_10000baseKX4_Full
,
1636 .advertised
= ADVERTISED_10000baseKX4_Full
,
1640 .mask
= MLXSW_REG_PTYS_ETH_SPEED_10GBASE_KR
|
1641 MLXSW_REG_PTYS_ETH_SPEED_10GBASE_CR
|
1642 MLXSW_REG_PTYS_ETH_SPEED_10GBASE_SR
|
1643 MLXSW_REG_PTYS_ETH_SPEED_10GBASE_ER_LR
,
1644 .supported
= SUPPORTED_10000baseKR_Full
,
1645 .advertised
= ADVERTISED_10000baseKR_Full
,
1649 .mask
= MLXSW_REG_PTYS_ETH_SPEED_20GBASE_KR2
,
1650 .supported
= SUPPORTED_20000baseKR2_Full
,
1651 .advertised
= ADVERTISED_20000baseKR2_Full
,
1655 .mask
= MLXSW_REG_PTYS_ETH_SPEED_40GBASE_CR4
,
1656 .supported
= SUPPORTED_40000baseCR4_Full
,
1657 .advertised
= ADVERTISED_40000baseCR4_Full
,
1661 .mask
= MLXSW_REG_PTYS_ETH_SPEED_40GBASE_KR4
,
1662 .supported
= SUPPORTED_40000baseKR4_Full
,
1663 .advertised
= ADVERTISED_40000baseKR4_Full
,
1667 .mask
= MLXSW_REG_PTYS_ETH_SPEED_40GBASE_SR4
,
1668 .supported
= SUPPORTED_40000baseSR4_Full
,
1669 .advertised
= ADVERTISED_40000baseSR4_Full
,
1673 .mask
= MLXSW_REG_PTYS_ETH_SPEED_40GBASE_LR4_ER4
,
1674 .supported
= SUPPORTED_40000baseLR4_Full
,
1675 .advertised
= ADVERTISED_40000baseLR4_Full
,
1679 .mask
= MLXSW_REG_PTYS_ETH_SPEED_25GBASE_CR
|
1680 MLXSW_REG_PTYS_ETH_SPEED_25GBASE_KR
|
1681 MLXSW_REG_PTYS_ETH_SPEED_25GBASE_SR
,
1685 .mask
= MLXSW_REG_PTYS_ETH_SPEED_50GBASE_KR4
|
1686 MLXSW_REG_PTYS_ETH_SPEED_50GBASE_CR2
|
1687 MLXSW_REG_PTYS_ETH_SPEED_50GBASE_KR2
,
1691 .mask
= MLXSW_REG_PTYS_ETH_SPEED_56GBASE_R4
,
1692 .supported
= SUPPORTED_56000baseKR4_Full
,
1693 .advertised
= ADVERTISED_56000baseKR4_Full
,
1697 .mask
= MLXSW_REG_PTYS_ETH_SPEED_100GBASE_CR4
|
1698 MLXSW_REG_PTYS_ETH_SPEED_100GBASE_SR4
|
1699 MLXSW_REG_PTYS_ETH_SPEED_100GBASE_KR4
|
1700 MLXSW_REG_PTYS_ETH_SPEED_100GBASE_LR4_ER4
,
1705 #define MLXSW_SP_PORT_LINK_MODE_LEN ARRAY_SIZE(mlxsw_sp_port_link_mode)
1707 static u32
mlxsw_sp_from_ptys_supported_port(u32 ptys_eth_proto
)
1709 if (ptys_eth_proto
& (MLXSW_REG_PTYS_ETH_SPEED_10GBASE_CR
|
1710 MLXSW_REG_PTYS_ETH_SPEED_10GBASE_SR
|
1711 MLXSW_REG_PTYS_ETH_SPEED_40GBASE_CR4
|
1712 MLXSW_REG_PTYS_ETH_SPEED_40GBASE_SR4
|
1713 MLXSW_REG_PTYS_ETH_SPEED_100GBASE_SR4
|
1714 MLXSW_REG_PTYS_ETH_SPEED_SGMII
))
1715 return SUPPORTED_FIBRE
;
1717 if (ptys_eth_proto
& (MLXSW_REG_PTYS_ETH_SPEED_10GBASE_KR
|
1718 MLXSW_REG_PTYS_ETH_SPEED_10GBASE_KX4
|
1719 MLXSW_REG_PTYS_ETH_SPEED_40GBASE_KR4
|
1720 MLXSW_REG_PTYS_ETH_SPEED_100GBASE_KR4
|
1721 MLXSW_REG_PTYS_ETH_SPEED_1000BASE_KX
))
1722 return SUPPORTED_Backplane
;
1726 static u32
mlxsw_sp_from_ptys_supported_link(u32 ptys_eth_proto
)
1731 for (i
= 0; i
< MLXSW_SP_PORT_LINK_MODE_LEN
; i
++) {
1732 if (ptys_eth_proto
& mlxsw_sp_port_link_mode
[i
].mask
)
1733 modes
|= mlxsw_sp_port_link_mode
[i
].supported
;
1738 static u32
mlxsw_sp_from_ptys_advert_link(u32 ptys_eth_proto
)
1743 for (i
= 0; i
< MLXSW_SP_PORT_LINK_MODE_LEN
; i
++) {
1744 if (ptys_eth_proto
& mlxsw_sp_port_link_mode
[i
].mask
)
1745 modes
|= mlxsw_sp_port_link_mode
[i
].advertised
;
1750 static void mlxsw_sp_from_ptys_speed_duplex(bool carrier_ok
, u32 ptys_eth_proto
,
1751 struct ethtool_cmd
*cmd
)
1753 u32 speed
= SPEED_UNKNOWN
;
1754 u8 duplex
= DUPLEX_UNKNOWN
;
1760 for (i
= 0; i
< MLXSW_SP_PORT_LINK_MODE_LEN
; i
++) {
1761 if (ptys_eth_proto
& mlxsw_sp_port_link_mode
[i
].mask
) {
1762 speed
= mlxsw_sp_port_link_mode
[i
].speed
;
1763 duplex
= DUPLEX_FULL
;
1768 ethtool_cmd_speed_set(cmd
, speed
);
1769 cmd
->duplex
= duplex
;
1772 static u8
mlxsw_sp_port_connector_port(u32 ptys_eth_proto
)
1774 if (ptys_eth_proto
& (MLXSW_REG_PTYS_ETH_SPEED_10GBASE_SR
|
1775 MLXSW_REG_PTYS_ETH_SPEED_40GBASE_SR4
|
1776 MLXSW_REG_PTYS_ETH_SPEED_100GBASE_SR4
|
1777 MLXSW_REG_PTYS_ETH_SPEED_SGMII
))
1780 if (ptys_eth_proto
& (MLXSW_REG_PTYS_ETH_SPEED_10GBASE_CR
|
1781 MLXSW_REG_PTYS_ETH_SPEED_40GBASE_CR4
|
1782 MLXSW_REG_PTYS_ETH_SPEED_100GBASE_CR4
))
1785 if (ptys_eth_proto
& (MLXSW_REG_PTYS_ETH_SPEED_10GBASE_KR
|
1786 MLXSW_REG_PTYS_ETH_SPEED_10GBASE_KX4
|
1787 MLXSW_REG_PTYS_ETH_SPEED_40GBASE_KR4
|
1788 MLXSW_REG_PTYS_ETH_SPEED_100GBASE_KR4
))
1794 static int mlxsw_sp_port_get_settings(struct net_device
*dev
,
1795 struct ethtool_cmd
*cmd
)
1797 struct mlxsw_sp_port
*mlxsw_sp_port
= netdev_priv(dev
);
1798 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
1799 char ptys_pl
[MLXSW_REG_PTYS_LEN
];
1801 u32 eth_proto_admin
;
1805 mlxsw_reg_ptys_pack(ptys_pl
, mlxsw_sp_port
->local_port
, 0);
1806 err
= mlxsw_reg_query(mlxsw_sp
->core
, MLXSW_REG(ptys
), ptys_pl
);
1808 netdev_err(dev
, "Failed to get proto");
1811 mlxsw_reg_ptys_unpack(ptys_pl
, ð_proto_cap
,
1812 ð_proto_admin
, ð_proto_oper
);
1814 cmd
->supported
= mlxsw_sp_from_ptys_supported_port(eth_proto_cap
) |
1815 mlxsw_sp_from_ptys_supported_link(eth_proto_cap
) |
1816 SUPPORTED_Pause
| SUPPORTED_Asym_Pause
|
1818 cmd
->advertising
= mlxsw_sp_from_ptys_advert_link(eth_proto_admin
);
1819 mlxsw_sp_from_ptys_speed_duplex(netif_carrier_ok(dev
),
1820 eth_proto_oper
, cmd
);
1822 eth_proto_oper
= eth_proto_oper
? eth_proto_oper
: eth_proto_cap
;
1823 cmd
->port
= mlxsw_sp_port_connector_port(eth_proto_oper
);
1824 cmd
->lp_advertising
= mlxsw_sp_from_ptys_advert_link(eth_proto_oper
);
1826 cmd
->transceiver
= XCVR_INTERNAL
;
1830 static u32
mlxsw_sp_to_ptys_advert_link(u32 advertising
)
1835 for (i
= 0; i
< MLXSW_SP_PORT_LINK_MODE_LEN
; i
++) {
1836 if (advertising
& mlxsw_sp_port_link_mode
[i
].advertised
)
1837 ptys_proto
|= mlxsw_sp_port_link_mode
[i
].mask
;
1842 static u32
mlxsw_sp_to_ptys_speed(u32 speed
)
1847 for (i
= 0; i
< MLXSW_SP_PORT_LINK_MODE_LEN
; i
++) {
1848 if (speed
== mlxsw_sp_port_link_mode
[i
].speed
)
1849 ptys_proto
|= mlxsw_sp_port_link_mode
[i
].mask
;
1854 static u32
mlxsw_sp_to_ptys_upper_speed(u32 upper_speed
)
1859 for (i
= 0; i
< MLXSW_SP_PORT_LINK_MODE_LEN
; i
++) {
1860 if (mlxsw_sp_port_link_mode
[i
].speed
<= upper_speed
)
1861 ptys_proto
|= mlxsw_sp_port_link_mode
[i
].mask
;
1866 static int mlxsw_sp_port_set_settings(struct net_device
*dev
,
1867 struct ethtool_cmd
*cmd
)
1869 struct mlxsw_sp_port
*mlxsw_sp_port
= netdev_priv(dev
);
1870 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
1871 char ptys_pl
[MLXSW_REG_PTYS_LEN
];
1875 u32 eth_proto_admin
;
1878 speed
= ethtool_cmd_speed(cmd
);
1880 eth_proto_new
= cmd
->autoneg
== AUTONEG_ENABLE
?
1881 mlxsw_sp_to_ptys_advert_link(cmd
->advertising
) :
1882 mlxsw_sp_to_ptys_speed(speed
);
1884 mlxsw_reg_ptys_pack(ptys_pl
, mlxsw_sp_port
->local_port
, 0);
1885 err
= mlxsw_reg_query(mlxsw_sp
->core
, MLXSW_REG(ptys
), ptys_pl
);
1887 netdev_err(dev
, "Failed to get proto");
1890 mlxsw_reg_ptys_unpack(ptys_pl
, ð_proto_cap
, ð_proto_admin
, NULL
);
1892 eth_proto_new
= eth_proto_new
& eth_proto_cap
;
1893 if (!eth_proto_new
) {
1894 netdev_err(dev
, "Not supported proto admin requested");
1897 if (eth_proto_new
== eth_proto_admin
)
1900 mlxsw_reg_ptys_pack(ptys_pl
, mlxsw_sp_port
->local_port
, eth_proto_new
);
1901 err
= mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(ptys
), ptys_pl
);
1903 netdev_err(dev
, "Failed to set proto admin");
1907 if (!netif_running(dev
))
1910 err
= mlxsw_sp_port_admin_status_set(mlxsw_sp_port
, false);
1912 netdev_err(dev
, "Failed to set admin status");
1916 err
= mlxsw_sp_port_admin_status_set(mlxsw_sp_port
, true);
1918 netdev_err(dev
, "Failed to set admin status");
1925 static const struct ethtool_ops mlxsw_sp_port_ethtool_ops
= {
1926 .get_drvinfo
= mlxsw_sp_port_get_drvinfo
,
1927 .get_link
= ethtool_op_get_link
,
1928 .get_pauseparam
= mlxsw_sp_port_get_pauseparam
,
1929 .set_pauseparam
= mlxsw_sp_port_set_pauseparam
,
1930 .get_strings
= mlxsw_sp_port_get_strings
,
1931 .set_phys_id
= mlxsw_sp_port_set_phys_id
,
1932 .get_ethtool_stats
= mlxsw_sp_port_get_stats
,
1933 .get_sset_count
= mlxsw_sp_port_get_sset_count
,
1934 .get_settings
= mlxsw_sp_port_get_settings
,
1935 .set_settings
= mlxsw_sp_port_set_settings
,
1939 mlxsw_sp_port_speed_by_width_set(struct mlxsw_sp_port
*mlxsw_sp_port
, u8 width
)
1941 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
1942 u32 upper_speed
= MLXSW_SP_PORT_BASE_SPEED
* width
;
1943 char ptys_pl
[MLXSW_REG_PTYS_LEN
];
1944 u32 eth_proto_admin
;
1946 eth_proto_admin
= mlxsw_sp_to_ptys_upper_speed(upper_speed
);
1947 mlxsw_reg_ptys_pack(ptys_pl
, mlxsw_sp_port
->local_port
,
1949 return mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(ptys
), ptys_pl
);
1952 int mlxsw_sp_port_ets_set(struct mlxsw_sp_port
*mlxsw_sp_port
,
1953 enum mlxsw_reg_qeec_hr hr
, u8 index
, u8 next_index
,
1954 bool dwrr
, u8 dwrr_weight
)
1956 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
1957 char qeec_pl
[MLXSW_REG_QEEC_LEN
];
1959 mlxsw_reg_qeec_pack(qeec_pl
, mlxsw_sp_port
->local_port
, hr
, index
,
1961 mlxsw_reg_qeec_de_set(qeec_pl
, true);
1962 mlxsw_reg_qeec_dwrr_set(qeec_pl
, dwrr
);
1963 mlxsw_reg_qeec_dwrr_weight_set(qeec_pl
, dwrr_weight
);
1964 return mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(qeec
), qeec_pl
);
1967 int mlxsw_sp_port_ets_maxrate_set(struct mlxsw_sp_port
*mlxsw_sp_port
,
1968 enum mlxsw_reg_qeec_hr hr
, u8 index
,
1969 u8 next_index
, u32 maxrate
)
1971 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
1972 char qeec_pl
[MLXSW_REG_QEEC_LEN
];
1974 mlxsw_reg_qeec_pack(qeec_pl
, mlxsw_sp_port
->local_port
, hr
, index
,
1976 mlxsw_reg_qeec_mase_set(qeec_pl
, true);
1977 mlxsw_reg_qeec_max_shaper_rate_set(qeec_pl
, maxrate
);
1978 return mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(qeec
), qeec_pl
);
1981 int mlxsw_sp_port_prio_tc_set(struct mlxsw_sp_port
*mlxsw_sp_port
,
1982 u8 switch_prio
, u8 tclass
)
1984 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
1985 char qtct_pl
[MLXSW_REG_QTCT_LEN
];
1987 mlxsw_reg_qtct_pack(qtct_pl
, mlxsw_sp_port
->local_port
, switch_prio
,
1989 return mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(qtct
), qtct_pl
);
1992 static int mlxsw_sp_port_ets_init(struct mlxsw_sp_port
*mlxsw_sp_port
)
1996 /* Setup the elements hierarcy, so that each TC is linked to
1997 * one subgroup, which are all member in the same group.
1999 err
= mlxsw_sp_port_ets_set(mlxsw_sp_port
,
2000 MLXSW_REG_QEEC_HIERARCY_GROUP
, 0, 0, false,
2004 for (i
= 0; i
< IEEE_8021QAZ_MAX_TCS
; i
++) {
2005 err
= mlxsw_sp_port_ets_set(mlxsw_sp_port
,
2006 MLXSW_REG_QEEC_HIERARCY_SUBGROUP
, i
,
2011 for (i
= 0; i
< IEEE_8021QAZ_MAX_TCS
; i
++) {
2012 err
= mlxsw_sp_port_ets_set(mlxsw_sp_port
,
2013 MLXSW_REG_QEEC_HIERARCY_TC
, i
, i
,
2019 /* Make sure the max shaper is disabled in all hierarcies that
2022 err
= mlxsw_sp_port_ets_maxrate_set(mlxsw_sp_port
,
2023 MLXSW_REG_QEEC_HIERARCY_PORT
, 0, 0,
2024 MLXSW_REG_QEEC_MAS_DIS
);
2027 for (i
= 0; i
< IEEE_8021QAZ_MAX_TCS
; i
++) {
2028 err
= mlxsw_sp_port_ets_maxrate_set(mlxsw_sp_port
,
2029 MLXSW_REG_QEEC_HIERARCY_SUBGROUP
,
2031 MLXSW_REG_QEEC_MAS_DIS
);
2035 for (i
= 0; i
< IEEE_8021QAZ_MAX_TCS
; i
++) {
2036 err
= mlxsw_sp_port_ets_maxrate_set(mlxsw_sp_port
,
2037 MLXSW_REG_QEEC_HIERARCY_TC
,
2039 MLXSW_REG_QEEC_MAS_DIS
);
2044 /* Map all priorities to traffic class 0. */
2045 for (i
= 0; i
< IEEE_8021QAZ_MAX_TCS
; i
++) {
2046 err
= mlxsw_sp_port_prio_tc_set(mlxsw_sp_port
, i
, 0);
2054 static int mlxsw_sp_port_pvid_vport_create(struct mlxsw_sp_port
*mlxsw_sp_port
)
2056 mlxsw_sp_port
->pvid
= 1;
2058 return mlxsw_sp_port_add_vid(mlxsw_sp_port
->dev
, 0, 1);
2061 static int mlxsw_sp_port_pvid_vport_destroy(struct mlxsw_sp_port
*mlxsw_sp_port
)
2063 return mlxsw_sp_port_kill_vid(mlxsw_sp_port
->dev
, 0, 1);
2066 static int mlxsw_sp_port_create(struct mlxsw_sp
*mlxsw_sp
, u8 local_port
,
2067 bool split
, u8 module
, u8 width
, u8 lane
)
2069 struct mlxsw_sp_port
*mlxsw_sp_port
;
2070 struct net_device
*dev
;
2074 dev
= alloc_etherdev(sizeof(struct mlxsw_sp_port
));
2077 mlxsw_sp_port
= netdev_priv(dev
);
2078 mlxsw_sp_port
->dev
= dev
;
2079 mlxsw_sp_port
->mlxsw_sp
= mlxsw_sp
;
2080 mlxsw_sp_port
->local_port
= local_port
;
2081 mlxsw_sp_port
->split
= split
;
2082 mlxsw_sp_port
->mapping
.module
= module
;
2083 mlxsw_sp_port
->mapping
.width
= width
;
2084 mlxsw_sp_port
->mapping
.lane
= lane
;
2085 bytes
= DIV_ROUND_UP(VLAN_N_VID
, BITS_PER_BYTE
);
2086 mlxsw_sp_port
->active_vlans
= kzalloc(bytes
, GFP_KERNEL
);
2087 if (!mlxsw_sp_port
->active_vlans
) {
2089 goto err_port_active_vlans_alloc
;
2091 mlxsw_sp_port
->untagged_vlans
= kzalloc(bytes
, GFP_KERNEL
);
2092 if (!mlxsw_sp_port
->untagged_vlans
) {
2094 goto err_port_untagged_vlans_alloc
;
2096 INIT_LIST_HEAD(&mlxsw_sp_port
->vports_list
);
2097 INIT_LIST_HEAD(&mlxsw_sp_port
->mall_tc_list
);
2099 mlxsw_sp_port
->pcpu_stats
=
2100 netdev_alloc_pcpu_stats(struct mlxsw_sp_port_pcpu_stats
);
2101 if (!mlxsw_sp_port
->pcpu_stats
) {
2103 goto err_alloc_stats
;
2106 dev
->netdev_ops
= &mlxsw_sp_port_netdev_ops
;
2107 dev
->ethtool_ops
= &mlxsw_sp_port_ethtool_ops
;
2109 err
= mlxsw_sp_port_swid_set(mlxsw_sp_port
, 0);
2111 dev_err(mlxsw_sp
->bus_info
->dev
, "Port %d: Failed to set SWID\n",
2112 mlxsw_sp_port
->local_port
);
2113 goto err_port_swid_set
;
2116 err
= mlxsw_sp_port_dev_addr_init(mlxsw_sp_port
);
2118 dev_err(mlxsw_sp
->bus_info
->dev
, "Port %d: Unable to init port mac address\n",
2119 mlxsw_sp_port
->local_port
);
2120 goto err_dev_addr_init
;
2123 netif_carrier_off(dev
);
2125 dev
->features
|= NETIF_F_NETNS_LOCAL
| NETIF_F_LLTX
| NETIF_F_SG
|
2126 NETIF_F_HW_VLAN_CTAG_FILTER
| NETIF_F_HW_TC
;
2127 dev
->hw_features
|= NETIF_F_HW_TC
;
2129 /* Each packet needs to have a Tx header (metadata) on top all other
2132 dev
->hard_header_len
+= MLXSW_TXHDR_LEN
;
2134 err
= mlxsw_sp_port_system_port_mapping_set(mlxsw_sp_port
);
2136 dev_err(mlxsw_sp
->bus_info
->dev
, "Port %d: Failed to set system port mapping\n",
2137 mlxsw_sp_port
->local_port
);
2138 goto err_port_system_port_mapping_set
;
2141 err
= mlxsw_sp_port_speed_by_width_set(mlxsw_sp_port
, width
);
2143 dev_err(mlxsw_sp
->bus_info
->dev
, "Port %d: Failed to enable speeds\n",
2144 mlxsw_sp_port
->local_port
);
2145 goto err_port_speed_by_width_set
;
2148 err
= mlxsw_sp_port_mtu_set(mlxsw_sp_port
, ETH_DATA_LEN
);
2150 dev_err(mlxsw_sp
->bus_info
->dev
, "Port %d: Failed to set MTU\n",
2151 mlxsw_sp_port
->local_port
);
2152 goto err_port_mtu_set
;
2155 err
= mlxsw_sp_port_admin_status_set(mlxsw_sp_port
, false);
2157 goto err_port_admin_status_set
;
2159 err
= mlxsw_sp_port_buffers_init(mlxsw_sp_port
);
2161 dev_err(mlxsw_sp
->bus_info
->dev
, "Port %d: Failed to initialize buffers\n",
2162 mlxsw_sp_port
->local_port
);
2163 goto err_port_buffers_init
;
2166 err
= mlxsw_sp_port_ets_init(mlxsw_sp_port
);
2168 dev_err(mlxsw_sp
->bus_info
->dev
, "Port %d: Failed to initialize ETS\n",
2169 mlxsw_sp_port
->local_port
);
2170 goto err_port_ets_init
;
2173 /* ETS and buffers must be initialized before DCB. */
2174 err
= mlxsw_sp_port_dcb_init(mlxsw_sp_port
);
2176 dev_err(mlxsw_sp
->bus_info
->dev
, "Port %d: Failed to initialize DCB\n",
2177 mlxsw_sp_port
->local_port
);
2178 goto err_port_dcb_init
;
2181 err
= mlxsw_sp_port_pvid_vport_create(mlxsw_sp_port
);
2183 dev_err(mlxsw_sp
->bus_info
->dev
, "Port %d: Failed to create PVID vPort\n",
2184 mlxsw_sp_port
->local_port
);
2185 goto err_port_pvid_vport_create
;
2188 mlxsw_sp_port_switchdev_init(mlxsw_sp_port
);
2189 mlxsw_sp
->ports
[local_port
] = mlxsw_sp_port
;
2190 err
= register_netdev(dev
);
2192 dev_err(mlxsw_sp
->bus_info
->dev
, "Port %d: Failed to register netdev\n",
2193 mlxsw_sp_port
->local_port
);
2194 goto err_register_netdev
;
2197 err
= mlxsw_core_port_init(mlxsw_sp
->core
, &mlxsw_sp_port
->core_port
,
2198 mlxsw_sp_port
->local_port
, dev
,
2199 mlxsw_sp_port
->split
, module
);
2201 dev_err(mlxsw_sp
->bus_info
->dev
, "Port %d: Failed to init core port\n",
2202 mlxsw_sp_port
->local_port
);
2203 goto err_core_port_init
;
2209 unregister_netdev(dev
);
2210 err_register_netdev
:
2211 mlxsw_sp
->ports
[local_port
] = NULL
;
2212 mlxsw_sp_port_switchdev_fini(mlxsw_sp_port
);
2213 mlxsw_sp_port_pvid_vport_destroy(mlxsw_sp_port
);
2214 err_port_pvid_vport_create
:
2215 mlxsw_sp_port_dcb_fini(mlxsw_sp_port
);
2218 err_port_buffers_init
:
2219 err_port_admin_status_set
:
2221 err_port_speed_by_width_set
:
2222 err_port_system_port_mapping_set
:
2224 mlxsw_sp_port_swid_set(mlxsw_sp_port
, MLXSW_PORT_SWID_DISABLED_PORT
);
2226 free_percpu(mlxsw_sp_port
->pcpu_stats
);
2228 kfree(mlxsw_sp_port
->untagged_vlans
);
2229 err_port_untagged_vlans_alloc
:
2230 kfree(mlxsw_sp_port
->active_vlans
);
2231 err_port_active_vlans_alloc
:
2236 static void mlxsw_sp_port_remove(struct mlxsw_sp
*mlxsw_sp
, u8 local_port
)
2238 struct mlxsw_sp_port
*mlxsw_sp_port
= mlxsw_sp
->ports
[local_port
];
2242 mlxsw_core_port_fini(&mlxsw_sp_port
->core_port
);
2243 unregister_netdev(mlxsw_sp_port
->dev
); /* This calls ndo_stop */
2244 mlxsw_sp
->ports
[local_port
] = NULL
;
2245 mlxsw_sp_port_switchdev_fini(mlxsw_sp_port
);
2246 mlxsw_sp_port_pvid_vport_destroy(mlxsw_sp_port
);
2247 mlxsw_sp_port_dcb_fini(mlxsw_sp_port
);
2248 mlxsw_sp_port_swid_set(mlxsw_sp_port
, MLXSW_PORT_SWID_DISABLED_PORT
);
2249 mlxsw_sp_port_module_unmap(mlxsw_sp
, mlxsw_sp_port
->local_port
);
2250 free_percpu(mlxsw_sp_port
->pcpu_stats
);
2251 kfree(mlxsw_sp_port
->untagged_vlans
);
2252 kfree(mlxsw_sp_port
->active_vlans
);
2253 WARN_ON_ONCE(!list_empty(&mlxsw_sp_port
->vports_list
));
2254 free_netdev(mlxsw_sp_port
->dev
);
2257 static void mlxsw_sp_ports_remove(struct mlxsw_sp
*mlxsw_sp
)
2261 for (i
= 1; i
< MLXSW_PORT_MAX_PORTS
; i
++)
2262 mlxsw_sp_port_remove(mlxsw_sp
, i
);
2263 kfree(mlxsw_sp
->ports
);
2266 static int mlxsw_sp_ports_create(struct mlxsw_sp
*mlxsw_sp
)
2268 u8 module
, width
, lane
;
2273 alloc_size
= sizeof(struct mlxsw_sp_port
*) * MLXSW_PORT_MAX_PORTS
;
2274 mlxsw_sp
->ports
= kzalloc(alloc_size
, GFP_KERNEL
);
2275 if (!mlxsw_sp
->ports
)
2278 for (i
= 1; i
< MLXSW_PORT_MAX_PORTS
; i
++) {
2279 err
= mlxsw_sp_port_module_info_get(mlxsw_sp
, i
, &module
,
2282 goto err_port_module_info_get
;
2285 mlxsw_sp
->port_to_module
[i
] = module
;
2286 err
= mlxsw_sp_port_create(mlxsw_sp
, i
, false, module
, width
,
2289 goto err_port_create
;
2294 err_port_module_info_get
:
2295 for (i
--; i
>= 1; i
--)
2296 mlxsw_sp_port_remove(mlxsw_sp
, i
);
2297 kfree(mlxsw_sp
->ports
);
2301 static u8
mlxsw_sp_cluster_base_port_get(u8 local_port
)
2303 u8 offset
= (local_port
- 1) % MLXSW_SP_PORTS_PER_CLUSTER_MAX
;
2305 return local_port
- offset
;
2308 static int mlxsw_sp_port_split_create(struct mlxsw_sp
*mlxsw_sp
, u8 base_port
,
2309 u8 module
, unsigned int count
)
2311 u8 width
= MLXSW_PORT_MODULE_MAX_WIDTH
/ count
;
2314 for (i
= 0; i
< count
; i
++) {
2315 err
= mlxsw_sp_port_module_map(mlxsw_sp
, base_port
+ i
, module
,
2318 goto err_port_module_map
;
2321 for (i
= 0; i
< count
; i
++) {
2322 err
= __mlxsw_sp_port_swid_set(mlxsw_sp
, base_port
+ i
, 0);
2324 goto err_port_swid_set
;
2327 for (i
= 0; i
< count
; i
++) {
2328 err
= mlxsw_sp_port_create(mlxsw_sp
, base_port
+ i
, true,
2329 module
, width
, i
* width
);
2331 goto err_port_create
;
2337 for (i
--; i
>= 0; i
--)
2338 mlxsw_sp_port_remove(mlxsw_sp
, base_port
+ i
);
2341 for (i
--; i
>= 0; i
--)
2342 __mlxsw_sp_port_swid_set(mlxsw_sp
, base_port
+ i
,
2343 MLXSW_PORT_SWID_DISABLED_PORT
);
2345 err_port_module_map
:
2346 for (i
--; i
>= 0; i
--)
2347 mlxsw_sp_port_module_unmap(mlxsw_sp
, base_port
+ i
);
2351 static void mlxsw_sp_port_unsplit_create(struct mlxsw_sp
*mlxsw_sp
,
2352 u8 base_port
, unsigned int count
)
2354 u8 local_port
, module
, width
= MLXSW_PORT_MODULE_MAX_WIDTH
;
2357 /* Split by four means we need to re-create two ports, otherwise
2362 for (i
= 0; i
< count
; i
++) {
2363 local_port
= base_port
+ i
* 2;
2364 module
= mlxsw_sp
->port_to_module
[local_port
];
2366 mlxsw_sp_port_module_map(mlxsw_sp
, local_port
, module
, width
,
2370 for (i
= 0; i
< count
; i
++)
2371 __mlxsw_sp_port_swid_set(mlxsw_sp
, base_port
+ i
* 2, 0);
2373 for (i
= 0; i
< count
; i
++) {
2374 local_port
= base_port
+ i
* 2;
2375 module
= mlxsw_sp
->port_to_module
[local_port
];
2377 mlxsw_sp_port_create(mlxsw_sp
, local_port
, false, module
,
2382 static int mlxsw_sp_port_split(struct mlxsw_core
*mlxsw_core
, u8 local_port
,
2385 struct mlxsw_sp
*mlxsw_sp
= mlxsw_core_driver_priv(mlxsw_core
);
2386 struct mlxsw_sp_port
*mlxsw_sp_port
;
2387 u8 module
, cur_width
, base_port
;
2391 mlxsw_sp_port
= mlxsw_sp
->ports
[local_port
];
2392 if (!mlxsw_sp_port
) {
2393 dev_err(mlxsw_sp
->bus_info
->dev
, "Port number \"%d\" does not exist\n",
2398 module
= mlxsw_sp_port
->mapping
.module
;
2399 cur_width
= mlxsw_sp_port
->mapping
.width
;
2401 if (count
!= 2 && count
!= 4) {
2402 netdev_err(mlxsw_sp_port
->dev
, "Port can only be split into 2 or 4 ports\n");
2406 if (cur_width
!= MLXSW_PORT_MODULE_MAX_WIDTH
) {
2407 netdev_err(mlxsw_sp_port
->dev
, "Port cannot be split further\n");
2411 /* Make sure we have enough slave (even) ports for the split. */
2413 base_port
= local_port
;
2414 if (mlxsw_sp
->ports
[base_port
+ 1]) {
2415 netdev_err(mlxsw_sp_port
->dev
, "Invalid split configuration\n");
2419 base_port
= mlxsw_sp_cluster_base_port_get(local_port
);
2420 if (mlxsw_sp
->ports
[base_port
+ 1] ||
2421 mlxsw_sp
->ports
[base_port
+ 3]) {
2422 netdev_err(mlxsw_sp_port
->dev
, "Invalid split configuration\n");
2427 for (i
= 0; i
< count
; i
++)
2428 mlxsw_sp_port_remove(mlxsw_sp
, base_port
+ i
);
2430 err
= mlxsw_sp_port_split_create(mlxsw_sp
, base_port
, module
, count
);
2432 dev_err(mlxsw_sp
->bus_info
->dev
, "Failed to create split ports\n");
2433 goto err_port_split_create
;
2438 err_port_split_create
:
2439 mlxsw_sp_port_unsplit_create(mlxsw_sp
, base_port
, count
);
2443 static int mlxsw_sp_port_unsplit(struct mlxsw_core
*mlxsw_core
, u8 local_port
)
2445 struct mlxsw_sp
*mlxsw_sp
= mlxsw_core_driver_priv(mlxsw_core
);
2446 struct mlxsw_sp_port
*mlxsw_sp_port
;
2447 u8 cur_width
, base_port
;
2451 mlxsw_sp_port
= mlxsw_sp
->ports
[local_port
];
2452 if (!mlxsw_sp_port
) {
2453 dev_err(mlxsw_sp
->bus_info
->dev
, "Port number \"%d\" does not exist\n",
2458 if (!mlxsw_sp_port
->split
) {
2459 netdev_err(mlxsw_sp_port
->dev
, "Port wasn't split\n");
2463 cur_width
= mlxsw_sp_port
->mapping
.width
;
2464 count
= cur_width
== 1 ? 4 : 2;
2466 base_port
= mlxsw_sp_cluster_base_port_get(local_port
);
2468 /* Determine which ports to remove. */
2469 if (count
== 2 && local_port
>= base_port
+ 2)
2470 base_port
= base_port
+ 2;
2472 for (i
= 0; i
< count
; i
++)
2473 mlxsw_sp_port_remove(mlxsw_sp
, base_port
+ i
);
2475 mlxsw_sp_port_unsplit_create(mlxsw_sp
, base_port
, count
);
2480 static void mlxsw_sp_pude_event_func(const struct mlxsw_reg_info
*reg
,
2481 char *pude_pl
, void *priv
)
2483 struct mlxsw_sp
*mlxsw_sp
= priv
;
2484 struct mlxsw_sp_port
*mlxsw_sp_port
;
2485 enum mlxsw_reg_pude_oper_status status
;
2488 local_port
= mlxsw_reg_pude_local_port_get(pude_pl
);
2489 mlxsw_sp_port
= mlxsw_sp
->ports
[local_port
];
2493 status
= mlxsw_reg_pude_oper_status_get(pude_pl
);
2494 if (status
== MLXSW_PORT_OPER_STATUS_UP
) {
2495 netdev_info(mlxsw_sp_port
->dev
, "link up\n");
2496 netif_carrier_on(mlxsw_sp_port
->dev
);
2498 netdev_info(mlxsw_sp_port
->dev
, "link down\n");
2499 netif_carrier_off(mlxsw_sp_port
->dev
);
2503 static struct mlxsw_event_listener mlxsw_sp_pude_event
= {
2504 .func
= mlxsw_sp_pude_event_func
,
2505 .trap_id
= MLXSW_TRAP_ID_PUDE
,
2508 static int mlxsw_sp_event_register(struct mlxsw_sp
*mlxsw_sp
,
2509 enum mlxsw_event_trap_id trap_id
)
2511 struct mlxsw_event_listener
*el
;
2512 char hpkt_pl
[MLXSW_REG_HPKT_LEN
];
2516 case MLXSW_TRAP_ID_PUDE
:
2517 el
= &mlxsw_sp_pude_event
;
2520 err
= mlxsw_core_event_listener_register(mlxsw_sp
->core
, el
, mlxsw_sp
);
2524 mlxsw_reg_hpkt_pack(hpkt_pl
, MLXSW_REG_HPKT_ACTION_FORWARD
, trap_id
);
2525 err
= mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(hpkt
), hpkt_pl
);
2527 goto err_event_trap_set
;
2532 mlxsw_core_event_listener_unregister(mlxsw_sp
->core
, el
, mlxsw_sp
);
2536 static void mlxsw_sp_event_unregister(struct mlxsw_sp
*mlxsw_sp
,
2537 enum mlxsw_event_trap_id trap_id
)
2539 struct mlxsw_event_listener
*el
;
2542 case MLXSW_TRAP_ID_PUDE
:
2543 el
= &mlxsw_sp_pude_event
;
2546 mlxsw_core_event_listener_unregister(mlxsw_sp
->core
, el
, mlxsw_sp
);
2549 static void mlxsw_sp_rx_listener_func(struct sk_buff
*skb
, u8 local_port
,
2552 struct mlxsw_sp
*mlxsw_sp
= priv
;
2553 struct mlxsw_sp_port
*mlxsw_sp_port
= mlxsw_sp
->ports
[local_port
];
2554 struct mlxsw_sp_port_pcpu_stats
*pcpu_stats
;
2556 if (unlikely(!mlxsw_sp_port
)) {
2557 dev_warn_ratelimited(mlxsw_sp
->bus_info
->dev
, "Port %d: skb received for non-existent port\n",
2562 skb
->dev
= mlxsw_sp_port
->dev
;
2564 pcpu_stats
= this_cpu_ptr(mlxsw_sp_port
->pcpu_stats
);
2565 u64_stats_update_begin(&pcpu_stats
->syncp
);
2566 pcpu_stats
->rx_packets
++;
2567 pcpu_stats
->rx_bytes
+= skb
->len
;
2568 u64_stats_update_end(&pcpu_stats
->syncp
);
2570 skb
->protocol
= eth_type_trans(skb
, skb
->dev
);
2571 netif_receive_skb(skb
);
2574 static void mlxsw_sp_rx_listener_mark_func(struct sk_buff
*skb
, u8 local_port
,
2577 skb
->offload_fwd_mark
= 1;
2578 return mlxsw_sp_rx_listener_func(skb
, local_port
, priv
);
2581 #define MLXSW_SP_RXL(_func, _trap_id, _action) \
2584 .local_port = MLXSW_PORT_DONT_CARE, \
2585 .trap_id = MLXSW_TRAP_ID_##_trap_id, \
2586 .action = MLXSW_REG_HPKT_ACTION_##_action, \
2589 static const struct mlxsw_rx_listener mlxsw_sp_rx_listener
[] = {
2590 MLXSW_SP_RXL(mlxsw_sp_rx_listener_func
, FDB_MC
, TRAP_TO_CPU
),
2591 /* Traps for specific L2 packet types, not trapped as FDB MC */
2592 MLXSW_SP_RXL(mlxsw_sp_rx_listener_func
, STP
, TRAP_TO_CPU
),
2593 MLXSW_SP_RXL(mlxsw_sp_rx_listener_func
, LACP
, TRAP_TO_CPU
),
2594 MLXSW_SP_RXL(mlxsw_sp_rx_listener_func
, EAPOL
, TRAP_TO_CPU
),
2595 MLXSW_SP_RXL(mlxsw_sp_rx_listener_func
, LLDP
, TRAP_TO_CPU
),
2596 MLXSW_SP_RXL(mlxsw_sp_rx_listener_func
, MMRP
, TRAP_TO_CPU
),
2597 MLXSW_SP_RXL(mlxsw_sp_rx_listener_func
, MVRP
, TRAP_TO_CPU
),
2598 MLXSW_SP_RXL(mlxsw_sp_rx_listener_func
, RPVST
, TRAP_TO_CPU
),
2599 MLXSW_SP_RXL(mlxsw_sp_rx_listener_mark_func
, DHCP
, MIRROR_TO_CPU
),
2600 MLXSW_SP_RXL(mlxsw_sp_rx_listener_mark_func
, IGMP_QUERY
, MIRROR_TO_CPU
),
2601 MLXSW_SP_RXL(mlxsw_sp_rx_listener_func
, IGMP_V1_REPORT
, TRAP_TO_CPU
),
2602 MLXSW_SP_RXL(mlxsw_sp_rx_listener_func
, IGMP_V2_REPORT
, TRAP_TO_CPU
),
2603 MLXSW_SP_RXL(mlxsw_sp_rx_listener_func
, IGMP_V2_LEAVE
, TRAP_TO_CPU
),
2604 MLXSW_SP_RXL(mlxsw_sp_rx_listener_func
, IGMP_V3_REPORT
, TRAP_TO_CPU
),
2605 MLXSW_SP_RXL(mlxsw_sp_rx_listener_mark_func
, ARPBC
, MIRROR_TO_CPU
),
2606 MLXSW_SP_RXL(mlxsw_sp_rx_listener_mark_func
, ARPUC
, MIRROR_TO_CPU
),
2608 MLXSW_SP_RXL(mlxsw_sp_rx_listener_func
, MTUERROR
, TRAP_TO_CPU
),
2609 MLXSW_SP_RXL(mlxsw_sp_rx_listener_func
, TTLERROR
, TRAP_TO_CPU
),
2610 MLXSW_SP_RXL(mlxsw_sp_rx_listener_func
, LBERROR
, TRAP_TO_CPU
),
2611 MLXSW_SP_RXL(mlxsw_sp_rx_listener_mark_func
, OSPF
, TRAP_TO_CPU
),
2612 MLXSW_SP_RXL(mlxsw_sp_rx_listener_func
, IP2ME
, TRAP_TO_CPU
),
2613 MLXSW_SP_RXL(mlxsw_sp_rx_listener_func
, RTR_INGRESS0
, TRAP_TO_CPU
),
2614 MLXSW_SP_RXL(mlxsw_sp_rx_listener_func
, HOST_MISS_IPV4
, TRAP_TO_CPU
),
2617 static int mlxsw_sp_traps_init(struct mlxsw_sp
*mlxsw_sp
)
2619 char htgt_pl
[MLXSW_REG_HTGT_LEN
];
2620 char hpkt_pl
[MLXSW_REG_HPKT_LEN
];
2624 mlxsw_reg_htgt_pack(htgt_pl
, MLXSW_REG_HTGT_TRAP_GROUP_RX
);
2625 err
= mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(htgt
), htgt_pl
);
2629 mlxsw_reg_htgt_pack(htgt_pl
, MLXSW_REG_HTGT_TRAP_GROUP_CTRL
);
2630 err
= mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(htgt
), htgt_pl
);
2634 for (i
= 0; i
< ARRAY_SIZE(mlxsw_sp_rx_listener
); i
++) {
2635 err
= mlxsw_core_rx_listener_register(mlxsw_sp
->core
,
2636 &mlxsw_sp_rx_listener
[i
],
2639 goto err_rx_listener_register
;
2641 mlxsw_reg_hpkt_pack(hpkt_pl
, mlxsw_sp_rx_listener
[i
].action
,
2642 mlxsw_sp_rx_listener
[i
].trap_id
);
2643 err
= mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(hpkt
), hpkt_pl
);
2645 goto err_rx_trap_set
;
2650 mlxsw_core_rx_listener_unregister(mlxsw_sp
->core
,
2651 &mlxsw_sp_rx_listener
[i
],
2653 err_rx_listener_register
:
2654 for (i
--; i
>= 0; i
--) {
2655 mlxsw_reg_hpkt_pack(hpkt_pl
, MLXSW_REG_HPKT_ACTION_DISCARD
,
2656 mlxsw_sp_rx_listener
[i
].trap_id
);
2657 mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(hpkt
), hpkt_pl
);
2659 mlxsw_core_rx_listener_unregister(mlxsw_sp
->core
,
2660 &mlxsw_sp_rx_listener
[i
],
2666 static void mlxsw_sp_traps_fini(struct mlxsw_sp
*mlxsw_sp
)
2668 char hpkt_pl
[MLXSW_REG_HPKT_LEN
];
2671 for (i
= 0; i
< ARRAY_SIZE(mlxsw_sp_rx_listener
); i
++) {
2672 mlxsw_reg_hpkt_pack(hpkt_pl
, MLXSW_REG_HPKT_ACTION_DISCARD
,
2673 mlxsw_sp_rx_listener
[i
].trap_id
);
2674 mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(hpkt
), hpkt_pl
);
2676 mlxsw_core_rx_listener_unregister(mlxsw_sp
->core
,
2677 &mlxsw_sp_rx_listener
[i
],
2682 static int __mlxsw_sp_flood_init(struct mlxsw_core
*mlxsw_core
,
2683 enum mlxsw_reg_sfgc_type type
,
2684 enum mlxsw_reg_sfgc_bridge_type bridge_type
)
2686 enum mlxsw_flood_table_type table_type
;
2687 enum mlxsw_sp_flood_table flood_table
;
2688 char sfgc_pl
[MLXSW_REG_SFGC_LEN
];
2690 if (bridge_type
== MLXSW_REG_SFGC_BRIDGE_TYPE_VFID
)
2691 table_type
= MLXSW_REG_SFGC_TABLE_TYPE_FID
;
2693 table_type
= MLXSW_REG_SFGC_TABLE_TYPE_FID_OFFEST
;
2695 if (type
== MLXSW_REG_SFGC_TYPE_UNKNOWN_UNICAST
)
2696 flood_table
= MLXSW_SP_FLOOD_TABLE_UC
;
2698 flood_table
= MLXSW_SP_FLOOD_TABLE_BM
;
2700 mlxsw_reg_sfgc_pack(sfgc_pl
, type
, bridge_type
, table_type
,
2702 return mlxsw_reg_write(mlxsw_core
, MLXSW_REG(sfgc
), sfgc_pl
);
2705 static int mlxsw_sp_flood_init(struct mlxsw_sp
*mlxsw_sp
)
2709 for (type
= 0; type
< MLXSW_REG_SFGC_TYPE_MAX
; type
++) {
2710 if (type
== MLXSW_REG_SFGC_TYPE_RESERVED
)
2713 err
= __mlxsw_sp_flood_init(mlxsw_sp
->core
, type
,
2714 MLXSW_REG_SFGC_BRIDGE_TYPE_VFID
);
2718 err
= __mlxsw_sp_flood_init(mlxsw_sp
->core
, type
,
2719 MLXSW_REG_SFGC_BRIDGE_TYPE_1Q_FID
);
2727 static int mlxsw_sp_lag_init(struct mlxsw_sp
*mlxsw_sp
)
2729 char slcr_pl
[MLXSW_REG_SLCR_LEN
];
2731 mlxsw_reg_slcr_pack(slcr_pl
, MLXSW_REG_SLCR_LAG_HASH_SMAC
|
2732 MLXSW_REG_SLCR_LAG_HASH_DMAC
|
2733 MLXSW_REG_SLCR_LAG_HASH_ETHERTYPE
|
2734 MLXSW_REG_SLCR_LAG_HASH_VLANID
|
2735 MLXSW_REG_SLCR_LAG_HASH_SIP
|
2736 MLXSW_REG_SLCR_LAG_HASH_DIP
|
2737 MLXSW_REG_SLCR_LAG_HASH_SPORT
|
2738 MLXSW_REG_SLCR_LAG_HASH_DPORT
|
2739 MLXSW_REG_SLCR_LAG_HASH_IPPROTO
);
2740 return mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(slcr
), slcr_pl
);
2743 static int mlxsw_sp_init(struct mlxsw_core
*mlxsw_core
,
2744 const struct mlxsw_bus_info
*mlxsw_bus_info
)
2746 struct mlxsw_sp
*mlxsw_sp
= mlxsw_core_driver_priv(mlxsw_core
);
2749 mlxsw_sp
->core
= mlxsw_core
;
2750 mlxsw_sp
->bus_info
= mlxsw_bus_info
;
2751 INIT_LIST_HEAD(&mlxsw_sp
->fids
);
2752 INIT_LIST_HEAD(&mlxsw_sp
->vfids
.list
);
2753 INIT_LIST_HEAD(&mlxsw_sp
->br_mids
.list
);
2755 err
= mlxsw_sp_base_mac_get(mlxsw_sp
);
2757 dev_err(mlxsw_sp
->bus_info
->dev
, "Failed to get base mac\n");
2761 err
= mlxsw_sp_event_register(mlxsw_sp
, MLXSW_TRAP_ID_PUDE
);
2763 dev_err(mlxsw_sp
->bus_info
->dev
, "Failed to register for PUDE events\n");
2767 err
= mlxsw_sp_traps_init(mlxsw_sp
);
2769 dev_err(mlxsw_sp
->bus_info
->dev
, "Failed to set traps for RX\n");
2770 goto err_rx_listener_register
;
2773 err
= mlxsw_sp_flood_init(mlxsw_sp
);
2775 dev_err(mlxsw_sp
->bus_info
->dev
, "Failed to initialize flood tables\n");
2776 goto err_flood_init
;
2779 err
= mlxsw_sp_buffers_init(mlxsw_sp
);
2781 dev_err(mlxsw_sp
->bus_info
->dev
, "Failed to initialize buffers\n");
2782 goto err_buffers_init
;
2785 err
= mlxsw_sp_lag_init(mlxsw_sp
);
2787 dev_err(mlxsw_sp
->bus_info
->dev
, "Failed to initialize LAG\n");
2791 err
= mlxsw_sp_switchdev_init(mlxsw_sp
);
2793 dev_err(mlxsw_sp
->bus_info
->dev
, "Failed to initialize switchdev\n");
2794 goto err_switchdev_init
;
2797 err
= mlxsw_sp_router_init(mlxsw_sp
);
2799 dev_err(mlxsw_sp
->bus_info
->dev
, "Failed to initialize router\n");
2800 goto err_router_init
;
2803 err
= mlxsw_sp_span_init(mlxsw_sp
);
2805 dev_err(mlxsw_sp
->bus_info
->dev
, "Failed to init span system\n");
2809 err
= mlxsw_sp_ports_create(mlxsw_sp
);
2811 dev_err(mlxsw_sp
->bus_info
->dev
, "Failed to create ports\n");
2812 goto err_ports_create
;
2818 mlxsw_sp_span_fini(mlxsw_sp
);
2820 mlxsw_sp_router_fini(mlxsw_sp
);
2822 mlxsw_sp_switchdev_fini(mlxsw_sp
);
2825 mlxsw_sp_buffers_fini(mlxsw_sp
);
2828 mlxsw_sp_traps_fini(mlxsw_sp
);
2829 err_rx_listener_register
:
2830 mlxsw_sp_event_unregister(mlxsw_sp
, MLXSW_TRAP_ID_PUDE
);
2834 static void mlxsw_sp_fini(struct mlxsw_core
*mlxsw_core
)
2836 struct mlxsw_sp
*mlxsw_sp
= mlxsw_core_driver_priv(mlxsw_core
);
2839 mlxsw_sp_ports_remove(mlxsw_sp
);
2840 mlxsw_sp_span_fini(mlxsw_sp
);
2841 mlxsw_sp_router_fini(mlxsw_sp
);
2842 mlxsw_sp_switchdev_fini(mlxsw_sp
);
2843 mlxsw_sp_buffers_fini(mlxsw_sp
);
2844 mlxsw_sp_traps_fini(mlxsw_sp
);
2845 mlxsw_sp_event_unregister(mlxsw_sp
, MLXSW_TRAP_ID_PUDE
);
2846 WARN_ON(!list_empty(&mlxsw_sp
->vfids
.list
));
2847 WARN_ON(!list_empty(&mlxsw_sp
->fids
));
2848 for (i
= 0; i
< MLXSW_SP_RIF_MAX
; i
++)
2849 WARN_ON_ONCE(mlxsw_sp
->rifs
[i
]);
2852 static struct mlxsw_config_profile mlxsw_sp_config_profile
= {
2853 .used_max_vepa_channels
= 1,
2854 .max_vepa_channels
= 0,
2856 .max_lag
= MLXSW_SP_LAG_MAX
,
2857 .used_max_port_per_lag
= 1,
2858 .max_port_per_lag
= MLXSW_SP_PORT_PER_LAG_MAX
,
2860 .max_mid
= MLXSW_SP_MID_MAX
,
2863 .used_max_system_port
= 1,
2864 .max_system_port
= 64,
2865 .used_max_vlan_groups
= 1,
2866 .max_vlan_groups
= 127,
2867 .used_max_regions
= 1,
2869 .used_flood_tables
= 1,
2870 .used_flood_mode
= 1,
2872 .max_fid_offset_flood_tables
= 2,
2873 .fid_offset_flood_table_size
= VLAN_N_VID
- 1,
2874 .max_fid_flood_tables
= 2,
2875 .fid_flood_table_size
= MLXSW_SP_VFID_MAX
,
2876 .used_max_ib_mc
= 1,
2880 .used_kvd_sizes
= 1,
2881 .kvd_linear_size
= MLXSW_SP_KVD_LINEAR_SIZE
,
2882 .kvd_hash_single_size
= MLXSW_SP_KVD_HASH_SINGLE_SIZE
,
2883 .kvd_hash_double_size
= MLXSW_SP_KVD_HASH_DOUBLE_SIZE
,
2887 .type
= MLXSW_PORT_SWID_TYPE_ETH
,
2890 .resource_query_enable
= 1,
2893 static struct mlxsw_driver mlxsw_sp_driver
= {
2894 .kind
= MLXSW_DEVICE_KIND_SPECTRUM
,
2895 .owner
= THIS_MODULE
,
2896 .priv_size
= sizeof(struct mlxsw_sp
),
2897 .init
= mlxsw_sp_init
,
2898 .fini
= mlxsw_sp_fini
,
2899 .port_split
= mlxsw_sp_port_split
,
2900 .port_unsplit
= mlxsw_sp_port_unsplit
,
2901 .sb_pool_get
= mlxsw_sp_sb_pool_get
,
2902 .sb_pool_set
= mlxsw_sp_sb_pool_set
,
2903 .sb_port_pool_get
= mlxsw_sp_sb_port_pool_get
,
2904 .sb_port_pool_set
= mlxsw_sp_sb_port_pool_set
,
2905 .sb_tc_pool_bind_get
= mlxsw_sp_sb_tc_pool_bind_get
,
2906 .sb_tc_pool_bind_set
= mlxsw_sp_sb_tc_pool_bind_set
,
2907 .sb_occ_snapshot
= mlxsw_sp_sb_occ_snapshot
,
2908 .sb_occ_max_clear
= mlxsw_sp_sb_occ_max_clear
,
2909 .sb_occ_port_pool_get
= mlxsw_sp_sb_occ_port_pool_get
,
2910 .sb_occ_tc_port_bind_get
= mlxsw_sp_sb_occ_tc_port_bind_get
,
2911 .txhdr_construct
= mlxsw_sp_txhdr_construct
,
2912 .txhdr_len
= MLXSW_TXHDR_LEN
,
2913 .profile
= &mlxsw_sp_config_profile
,
2916 static bool mlxsw_sp_port_dev_check(const struct net_device
*dev
)
2918 return dev
->netdev_ops
== &mlxsw_sp_port_netdev_ops
;
2921 static struct mlxsw_sp_port
*mlxsw_sp_port_dev_lower_find(struct net_device
*dev
)
2923 struct net_device
*lower_dev
;
2924 struct list_head
*iter
;
2926 if (mlxsw_sp_port_dev_check(dev
))
2927 return netdev_priv(dev
);
2929 netdev_for_each_all_lower_dev(dev
, lower_dev
, iter
) {
2930 if (mlxsw_sp_port_dev_check(lower_dev
))
2931 return netdev_priv(lower_dev
);
2936 static struct mlxsw_sp
*mlxsw_sp_lower_get(struct net_device
*dev
)
2938 struct mlxsw_sp_port
*mlxsw_sp_port
;
2940 mlxsw_sp_port
= mlxsw_sp_port_dev_lower_find(dev
);
2941 return mlxsw_sp_port
? mlxsw_sp_port
->mlxsw_sp
: NULL
;
2944 static struct mlxsw_sp_port
*mlxsw_sp_port_dev_lower_find_rcu(struct net_device
*dev
)
2946 struct net_device
*lower_dev
;
2947 struct list_head
*iter
;
2949 if (mlxsw_sp_port_dev_check(dev
))
2950 return netdev_priv(dev
);
2952 netdev_for_each_all_lower_dev_rcu(dev
, lower_dev
, iter
) {
2953 if (mlxsw_sp_port_dev_check(lower_dev
))
2954 return netdev_priv(lower_dev
);
2959 struct mlxsw_sp_port
*mlxsw_sp_port_lower_dev_hold(struct net_device
*dev
)
2961 struct mlxsw_sp_port
*mlxsw_sp_port
;
2964 mlxsw_sp_port
= mlxsw_sp_port_dev_lower_find_rcu(dev
);
2966 dev_hold(mlxsw_sp_port
->dev
);
2968 return mlxsw_sp_port
;
2971 void mlxsw_sp_port_dev_put(struct mlxsw_sp_port
*mlxsw_sp_port
)
2973 dev_put(mlxsw_sp_port
->dev
);
2976 static bool mlxsw_sp_rif_should_config(struct mlxsw_sp_rif
*r
,
2977 unsigned long event
)
2986 if (r
&& --r
->ref_count
== 0)
2988 /* It is possible we already removed the RIF ourselves
2989 * if it was assigned to a netdev that is now a bridge
2998 static int mlxsw_sp_avail_rif_get(struct mlxsw_sp
*mlxsw_sp
)
3002 for (i
= 0; i
< MLXSW_SP_RIF_MAX
; i
++)
3003 if (!mlxsw_sp
->rifs
[i
])
3006 return MLXSW_SP_RIF_MAX
;
3009 static void mlxsw_sp_vport_rif_sp_attr_get(struct mlxsw_sp_port
*mlxsw_sp_vport
,
3010 bool *p_lagged
, u16
*p_system_port
)
3012 u8 local_port
= mlxsw_sp_vport
->local_port
;
3014 *p_lagged
= mlxsw_sp_vport
->lagged
;
3015 *p_system_port
= *p_lagged
? mlxsw_sp_vport
->lag_id
: local_port
;
3018 static int mlxsw_sp_vport_rif_sp_op(struct mlxsw_sp_port
*mlxsw_sp_vport
,
3019 struct net_device
*l3_dev
, u16 rif
,
3022 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_vport
->mlxsw_sp
;
3023 bool lagged
= mlxsw_sp_vport
->lagged
;
3024 char ritr_pl
[MLXSW_REG_RITR_LEN
];
3027 mlxsw_reg_ritr_pack(ritr_pl
, create
, MLXSW_REG_RITR_SP_IF
, rif
,
3028 l3_dev
->mtu
, l3_dev
->dev_addr
);
3030 mlxsw_sp_vport_rif_sp_attr_get(mlxsw_sp_vport
, &lagged
, &system_port
);
3031 mlxsw_reg_ritr_sp_if_pack(ritr_pl
, lagged
, system_port
,
3032 mlxsw_sp_vport_vid_get(mlxsw_sp_vport
));
3034 return mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(ritr
), ritr_pl
);
3037 static void mlxsw_sp_vport_rif_sp_leave(struct mlxsw_sp_port
*mlxsw_sp_vport
);
3039 static struct mlxsw_sp_fid
*
3040 mlxsw_sp_rfid_alloc(u16 fid
, struct net_device
*l3_dev
)
3042 struct mlxsw_sp_fid
*f
;
3044 f
= kzalloc(sizeof(*f
), GFP_KERNEL
);
3048 f
->leave
= mlxsw_sp_vport_rif_sp_leave
;
3056 static struct mlxsw_sp_rif
*
3057 mlxsw_sp_rif_alloc(u16 rif
, struct net_device
*l3_dev
, struct mlxsw_sp_fid
*f
)
3059 struct mlxsw_sp_rif
*r
;
3061 r
= kzalloc(sizeof(*r
), GFP_KERNEL
);
3065 ether_addr_copy(r
->addr
, l3_dev
->dev_addr
);
3066 r
->mtu
= l3_dev
->mtu
;
3075 static struct mlxsw_sp_rif
*
3076 mlxsw_sp_vport_rif_sp_create(struct mlxsw_sp_port
*mlxsw_sp_vport
,
3077 struct net_device
*l3_dev
)
3079 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_vport
->mlxsw_sp
;
3080 struct mlxsw_sp_fid
*f
;
3081 struct mlxsw_sp_rif
*r
;
3085 rif
= mlxsw_sp_avail_rif_get(mlxsw_sp
);
3086 if (rif
== MLXSW_SP_RIF_MAX
)
3087 return ERR_PTR(-ERANGE
);
3089 err
= mlxsw_sp_vport_rif_sp_op(mlxsw_sp_vport
, l3_dev
, rif
, true);
3091 return ERR_PTR(err
);
3093 fid
= mlxsw_sp_rif_sp_to_fid(rif
);
3094 err
= mlxsw_sp_rif_fdb_op(mlxsw_sp
, l3_dev
->dev_addr
, fid
, true);
3096 goto err_rif_fdb_op
;
3098 f
= mlxsw_sp_rfid_alloc(fid
, l3_dev
);
3101 goto err_rfid_alloc
;
3104 r
= mlxsw_sp_rif_alloc(rif
, l3_dev
, f
);
3111 mlxsw_sp
->rifs
[rif
] = r
;
3118 mlxsw_sp_rif_fdb_op(mlxsw_sp
, l3_dev
->dev_addr
, fid
, false);
3120 mlxsw_sp_vport_rif_sp_op(mlxsw_sp_vport
, l3_dev
, rif
, false);
3121 return ERR_PTR(err
);
3124 static void mlxsw_sp_vport_rif_sp_destroy(struct mlxsw_sp_port
*mlxsw_sp_vport
,
3125 struct mlxsw_sp_rif
*r
)
3127 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_vport
->mlxsw_sp
;
3128 struct net_device
*l3_dev
= r
->dev
;
3129 struct mlxsw_sp_fid
*f
= r
->f
;
3133 mlxsw_sp
->rifs
[rif
] = NULL
;
3140 mlxsw_sp_rif_fdb_op(mlxsw_sp
, l3_dev
->dev_addr
, fid
, false);
3142 mlxsw_sp_vport_rif_sp_op(mlxsw_sp_vport
, l3_dev
, rif
, false);
3145 static int mlxsw_sp_vport_rif_sp_join(struct mlxsw_sp_port
*mlxsw_sp_vport
,
3146 struct net_device
*l3_dev
)
3148 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_vport
->mlxsw_sp
;
3149 struct mlxsw_sp_rif
*r
;
3151 r
= mlxsw_sp_rif_find_by_dev(mlxsw_sp
, l3_dev
);
3153 r
= mlxsw_sp_vport_rif_sp_create(mlxsw_sp_vport
, l3_dev
);
3158 mlxsw_sp_vport_fid_set(mlxsw_sp_vport
, r
->f
);
3161 netdev_dbg(mlxsw_sp_vport
->dev
, "Joined FID=%d\n", r
->f
->fid
);
3166 static void mlxsw_sp_vport_rif_sp_leave(struct mlxsw_sp_port
*mlxsw_sp_vport
)
3168 struct mlxsw_sp_fid
*f
= mlxsw_sp_vport_fid_get(mlxsw_sp_vport
);
3170 netdev_dbg(mlxsw_sp_vport
->dev
, "Left FID=%d\n", f
->fid
);
3172 mlxsw_sp_vport_fid_set(mlxsw_sp_vport
, NULL
);
3173 if (--f
->ref_count
== 0)
3174 mlxsw_sp_vport_rif_sp_destroy(mlxsw_sp_vport
, f
->r
);
3177 static int mlxsw_sp_inetaddr_vport_event(struct net_device
*l3_dev
,
3178 struct net_device
*port_dev
,
3179 unsigned long event
, u16 vid
)
3181 struct mlxsw_sp_port
*mlxsw_sp_port
= netdev_priv(port_dev
);
3182 struct mlxsw_sp_port
*mlxsw_sp_vport
;
3184 mlxsw_sp_vport
= mlxsw_sp_port_vport_find(mlxsw_sp_port
, vid
);
3185 if (WARN_ON(!mlxsw_sp_vport
))
3190 return mlxsw_sp_vport_rif_sp_join(mlxsw_sp_vport
, l3_dev
);
3192 mlxsw_sp_vport_rif_sp_leave(mlxsw_sp_vport
);
3199 static int mlxsw_sp_inetaddr_port_event(struct net_device
*port_dev
,
3200 unsigned long event
)
3202 if (netif_is_bridge_port(port_dev
) || netif_is_lag_port(port_dev
))
3205 return mlxsw_sp_inetaddr_vport_event(port_dev
, port_dev
, event
, 1);
3208 static int __mlxsw_sp_inetaddr_lag_event(struct net_device
*l3_dev
,
3209 struct net_device
*lag_dev
,
3210 unsigned long event
, u16 vid
)
3212 struct net_device
*port_dev
;
3213 struct list_head
*iter
;
3216 netdev_for_each_lower_dev(lag_dev
, port_dev
, iter
) {
3217 if (mlxsw_sp_port_dev_check(port_dev
)) {
3218 err
= mlxsw_sp_inetaddr_vport_event(l3_dev
, port_dev
,
3228 static int mlxsw_sp_inetaddr_lag_event(struct net_device
*lag_dev
,
3229 unsigned long event
)
3231 if (netif_is_bridge_port(lag_dev
))
3234 return __mlxsw_sp_inetaddr_lag_event(lag_dev
, lag_dev
, event
, 1);
3237 static struct mlxsw_sp_fid
*mlxsw_sp_bridge_fid_get(struct mlxsw_sp
*mlxsw_sp
,
3238 struct net_device
*l3_dev
)
3242 if (is_vlan_dev(l3_dev
))
3243 fid
= vlan_dev_vlan_id(l3_dev
);
3244 else if (mlxsw_sp
->master_bridge
.dev
== l3_dev
)
3247 return mlxsw_sp_vfid_find(mlxsw_sp
, l3_dev
);
3249 return mlxsw_sp_fid_find(mlxsw_sp
, fid
);
3252 static enum mlxsw_flood_table_type
mlxsw_sp_flood_table_type_get(u16 fid
)
3254 return mlxsw_sp_fid_is_vfid(fid
) ? MLXSW_REG_SFGC_TABLE_TYPE_FID
:
3255 MLXSW_REG_SFGC_TABLE_TYPE_FID_OFFEST
;
3258 static u16
mlxsw_sp_flood_table_index_get(u16 fid
)
3260 return mlxsw_sp_fid_is_vfid(fid
) ? mlxsw_sp_fid_to_vfid(fid
) : fid
;
3263 static int mlxsw_sp_router_port_flood_set(struct mlxsw_sp
*mlxsw_sp
, u16 fid
,
3266 enum mlxsw_flood_table_type table_type
;
3271 sftr_pl
= kmalloc(MLXSW_REG_SFTR_LEN
, GFP_KERNEL
);
3275 table_type
= mlxsw_sp_flood_table_type_get(fid
);
3276 index
= mlxsw_sp_flood_table_index_get(fid
);
3277 mlxsw_reg_sftr_pack(sftr_pl
, MLXSW_SP_FLOOD_TABLE_BM
, index
, table_type
,
3278 1, MLXSW_PORT_ROUTER_PORT
, set
);
3279 err
= mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(sftr
), sftr_pl
);
3285 static enum mlxsw_reg_ritr_if_type
mlxsw_sp_rif_type_get(u16 fid
)
3287 if (mlxsw_sp_fid_is_vfid(fid
))
3288 return MLXSW_REG_RITR_FID_IF
;
3290 return MLXSW_REG_RITR_VLAN_IF
;
3293 static int mlxsw_sp_rif_bridge_op(struct mlxsw_sp
*mlxsw_sp
,
3294 struct net_device
*l3_dev
,
3298 enum mlxsw_reg_ritr_if_type rif_type
;
3299 char ritr_pl
[MLXSW_REG_RITR_LEN
];
3301 rif_type
= mlxsw_sp_rif_type_get(fid
);
3302 mlxsw_reg_ritr_pack(ritr_pl
, create
, rif_type
, rif
, l3_dev
->mtu
,
3304 mlxsw_reg_ritr_fid_set(ritr_pl
, rif_type
, fid
);
3306 return mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(ritr
), ritr_pl
);
3309 static int mlxsw_sp_rif_bridge_create(struct mlxsw_sp
*mlxsw_sp
,
3310 struct net_device
*l3_dev
,
3311 struct mlxsw_sp_fid
*f
)
3313 struct mlxsw_sp_rif
*r
;
3317 rif
= mlxsw_sp_avail_rif_get(mlxsw_sp
);
3318 if (rif
== MLXSW_SP_RIF_MAX
)
3321 err
= mlxsw_sp_router_port_flood_set(mlxsw_sp
, f
->fid
, true);
3325 err
= mlxsw_sp_rif_bridge_op(mlxsw_sp
, l3_dev
, f
->fid
, rif
, true);
3327 goto err_rif_bridge_op
;
3329 err
= mlxsw_sp_rif_fdb_op(mlxsw_sp
, l3_dev
->dev_addr
, f
->fid
, true);
3331 goto err_rif_fdb_op
;
3333 r
= mlxsw_sp_rif_alloc(rif
, l3_dev
, f
);
3340 mlxsw_sp
->rifs
[rif
] = r
;
3342 netdev_dbg(l3_dev
, "RIF=%d created\n", rif
);
3347 mlxsw_sp_rif_fdb_op(mlxsw_sp
, l3_dev
->dev_addr
, f
->fid
, false);
3349 mlxsw_sp_rif_bridge_op(mlxsw_sp
, l3_dev
, f
->fid
, rif
, false);
3351 mlxsw_sp_router_port_flood_set(mlxsw_sp
, f
->fid
, false);
3355 void mlxsw_sp_rif_bridge_destroy(struct mlxsw_sp
*mlxsw_sp
,
3356 struct mlxsw_sp_rif
*r
)
3358 struct net_device
*l3_dev
= r
->dev
;
3359 struct mlxsw_sp_fid
*f
= r
->f
;
3362 mlxsw_sp
->rifs
[rif
] = NULL
;
3367 mlxsw_sp_rif_fdb_op(mlxsw_sp
, l3_dev
->dev_addr
, f
->fid
, false);
3369 mlxsw_sp_rif_bridge_op(mlxsw_sp
, l3_dev
, f
->fid
, rif
, false);
3371 mlxsw_sp_router_port_flood_set(mlxsw_sp
, f
->fid
, false);
3373 netdev_dbg(l3_dev
, "RIF=%d destroyed\n", rif
);
3376 static int mlxsw_sp_inetaddr_bridge_event(struct net_device
*l3_dev
,
3377 struct net_device
*br_dev
,
3378 unsigned long event
)
3380 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_lower_get(l3_dev
);
3381 struct mlxsw_sp_fid
*f
;
3383 /* FID can either be an actual FID if the L3 device is the
3384 * VLAN-aware bridge or a VLAN device on top. Otherwise, the
3385 * L3 device is a VLAN-unaware bridge and we get a vFID.
3387 f
= mlxsw_sp_bridge_fid_get(mlxsw_sp
, l3_dev
);
3393 return mlxsw_sp_rif_bridge_create(mlxsw_sp
, l3_dev
, f
);
3395 mlxsw_sp_rif_bridge_destroy(mlxsw_sp
, f
->r
);
3402 static int mlxsw_sp_inetaddr_vlan_event(struct net_device
*vlan_dev
,
3403 unsigned long event
)
3405 struct net_device
*real_dev
= vlan_dev_real_dev(vlan_dev
);
3406 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_lower_get(vlan_dev
);
3407 u16 vid
= vlan_dev_vlan_id(vlan_dev
);
3409 if (mlxsw_sp_port_dev_check(real_dev
))
3410 return mlxsw_sp_inetaddr_vport_event(vlan_dev
, real_dev
, event
,
3412 else if (netif_is_lag_master(real_dev
))
3413 return __mlxsw_sp_inetaddr_lag_event(vlan_dev
, real_dev
, event
,
3415 else if (netif_is_bridge_master(real_dev
) &&
3416 mlxsw_sp
->master_bridge
.dev
== real_dev
)
3417 return mlxsw_sp_inetaddr_bridge_event(vlan_dev
, real_dev
,
3423 static int mlxsw_sp_inetaddr_event(struct notifier_block
*unused
,
3424 unsigned long event
, void *ptr
)
3426 struct in_ifaddr
*ifa
= (struct in_ifaddr
*) ptr
;
3427 struct net_device
*dev
= ifa
->ifa_dev
->dev
;
3428 struct mlxsw_sp
*mlxsw_sp
;
3429 struct mlxsw_sp_rif
*r
;
3432 mlxsw_sp
= mlxsw_sp_lower_get(dev
);
3436 r
= mlxsw_sp_rif_find_by_dev(mlxsw_sp
, dev
);
3437 if (!mlxsw_sp_rif_should_config(r
, event
))
3440 if (mlxsw_sp_port_dev_check(dev
))
3441 err
= mlxsw_sp_inetaddr_port_event(dev
, event
);
3442 else if (netif_is_lag_master(dev
))
3443 err
= mlxsw_sp_inetaddr_lag_event(dev
, event
);
3444 else if (netif_is_bridge_master(dev
))
3445 err
= mlxsw_sp_inetaddr_bridge_event(dev
, dev
, event
);
3446 else if (is_vlan_dev(dev
))
3447 err
= mlxsw_sp_inetaddr_vlan_event(dev
, event
);
3450 return notifier_from_errno(err
);
3453 static int mlxsw_sp_rif_edit(struct mlxsw_sp
*mlxsw_sp
, u16 rif
,
3454 const char *mac
, int mtu
)
3456 char ritr_pl
[MLXSW_REG_RITR_LEN
];
3459 mlxsw_reg_ritr_rif_pack(ritr_pl
, rif
);
3460 err
= mlxsw_reg_query(mlxsw_sp
->core
, MLXSW_REG(ritr
), ritr_pl
);
3464 mlxsw_reg_ritr_mtu_set(ritr_pl
, mtu
);
3465 mlxsw_reg_ritr_if_mac_memcpy_to(ritr_pl
, mac
);
3466 mlxsw_reg_ritr_op_set(ritr_pl
, MLXSW_REG_RITR_RIF_CREATE
);
3467 return mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(ritr
), ritr_pl
);
3470 static int mlxsw_sp_netdevice_router_port_event(struct net_device
*dev
)
3472 struct mlxsw_sp
*mlxsw_sp
;
3473 struct mlxsw_sp_rif
*r
;
3476 mlxsw_sp
= mlxsw_sp_lower_get(dev
);
3480 r
= mlxsw_sp_rif_find_by_dev(mlxsw_sp
, dev
);
3484 err
= mlxsw_sp_rif_fdb_op(mlxsw_sp
, r
->addr
, r
->f
->fid
, false);
3488 err
= mlxsw_sp_rif_edit(mlxsw_sp
, r
->rif
, dev
->dev_addr
, dev
->mtu
);
3492 err
= mlxsw_sp_rif_fdb_op(mlxsw_sp
, dev
->dev_addr
, r
->f
->fid
, true);
3494 goto err_rif_fdb_op
;
3496 ether_addr_copy(r
->addr
, dev
->dev_addr
);
3499 netdev_dbg(dev
, "Updated RIF=%d\n", r
->rif
);
3504 mlxsw_sp_rif_edit(mlxsw_sp
, r
->rif
, r
->addr
, r
->mtu
);
3506 mlxsw_sp_rif_fdb_op(mlxsw_sp
, r
->addr
, r
->f
->fid
, true);
3510 static bool mlxsw_sp_lag_port_fid_member(struct mlxsw_sp_port
*lag_port
,
3513 if (mlxsw_sp_fid_is_vfid(fid
))
3514 return mlxsw_sp_port_vport_find_by_fid(lag_port
, fid
);
3516 return test_bit(fid
, lag_port
->active_vlans
);
3519 static bool mlxsw_sp_port_fdb_should_flush(struct mlxsw_sp_port
*mlxsw_sp_port
,
3522 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
3523 u8 local_port
= mlxsw_sp_port
->local_port
;
3524 u16 lag_id
= mlxsw_sp_port
->lag_id
;
3527 if (!mlxsw_sp_port
->lagged
)
3530 for (i
= 0; i
< MLXSW_SP_PORT_PER_LAG_MAX
; i
++) {
3531 struct mlxsw_sp_port
*lag_port
;
3533 lag_port
= mlxsw_sp_port_lagged_get(mlxsw_sp
, lag_id
, i
);
3534 if (!lag_port
|| lag_port
->local_port
== local_port
)
3536 if (mlxsw_sp_lag_port_fid_member(lag_port
, fid
))
3544 mlxsw_sp_port_fdb_flush_by_port_fid(const struct mlxsw_sp_port
*mlxsw_sp_port
,
3547 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
3548 char sfdf_pl
[MLXSW_REG_SFDF_LEN
];
3550 mlxsw_reg_sfdf_pack(sfdf_pl
, MLXSW_REG_SFDF_FLUSH_PER_PORT_AND_FID
);
3551 mlxsw_reg_sfdf_fid_set(sfdf_pl
, fid
);
3552 mlxsw_reg_sfdf_port_fid_system_port_set(sfdf_pl
,
3553 mlxsw_sp_port
->local_port
);
3555 netdev_dbg(mlxsw_sp_port
->dev
, "FDB flushed using Port=%d, FID=%d\n",
3556 mlxsw_sp_port
->local_port
, fid
);
3558 return mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(sfdf
), sfdf_pl
);
3562 mlxsw_sp_port_fdb_flush_by_lag_id_fid(const struct mlxsw_sp_port
*mlxsw_sp_port
,
3565 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
3566 char sfdf_pl
[MLXSW_REG_SFDF_LEN
];
3568 mlxsw_reg_sfdf_pack(sfdf_pl
, MLXSW_REG_SFDF_FLUSH_PER_LAG_AND_FID
);
3569 mlxsw_reg_sfdf_fid_set(sfdf_pl
, fid
);
3570 mlxsw_reg_sfdf_lag_fid_lag_id_set(sfdf_pl
, mlxsw_sp_port
->lag_id
);
3572 netdev_dbg(mlxsw_sp_port
->dev
, "FDB flushed using LAG ID=%d, FID=%d\n",
3573 mlxsw_sp_port
->lag_id
, fid
);
3575 return mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(sfdf
), sfdf_pl
);
3578 int mlxsw_sp_port_fdb_flush(struct mlxsw_sp_port
*mlxsw_sp_port
, u16 fid
)
3580 if (!mlxsw_sp_port_fdb_should_flush(mlxsw_sp_port
, fid
))
3583 if (mlxsw_sp_port
->lagged
)
3584 return mlxsw_sp_port_fdb_flush_by_lag_id_fid(mlxsw_sp_port
,
3587 return mlxsw_sp_port_fdb_flush_by_port_fid(mlxsw_sp_port
, fid
);
3590 static void mlxsw_sp_master_bridge_gone_sync(struct mlxsw_sp
*mlxsw_sp
)
3592 struct mlxsw_sp_fid
*f
, *tmp
;
3594 list_for_each_entry_safe(f
, tmp
, &mlxsw_sp
->fids
, list
)
3595 if (--f
->ref_count
== 0)
3596 mlxsw_sp_fid_destroy(mlxsw_sp
, f
);
3601 static bool mlxsw_sp_master_bridge_check(struct mlxsw_sp
*mlxsw_sp
,
3602 struct net_device
*br_dev
)
3604 return !mlxsw_sp
->master_bridge
.dev
||
3605 mlxsw_sp
->master_bridge
.dev
== br_dev
;
3608 static void mlxsw_sp_master_bridge_inc(struct mlxsw_sp
*mlxsw_sp
,
3609 struct net_device
*br_dev
)
3611 mlxsw_sp
->master_bridge
.dev
= br_dev
;
3612 mlxsw_sp
->master_bridge
.ref_count
++;
3615 static void mlxsw_sp_master_bridge_dec(struct mlxsw_sp
*mlxsw_sp
)
3617 if (--mlxsw_sp
->master_bridge
.ref_count
== 0) {
3618 mlxsw_sp
->master_bridge
.dev
= NULL
;
3619 /* It's possible upper VLAN devices are still holding
3620 * references to underlying FIDs. Drop the reference
3621 * and release the resources if it was the last one.
3622 * If it wasn't, then something bad happened.
3624 mlxsw_sp_master_bridge_gone_sync(mlxsw_sp
);
3628 static int mlxsw_sp_port_bridge_join(struct mlxsw_sp_port
*mlxsw_sp_port
,
3629 struct net_device
*br_dev
)
3631 struct net_device
*dev
= mlxsw_sp_port
->dev
;
3634 /* When port is not bridged untagged packets are tagged with
3635 * PVID=VID=1, thereby creating an implicit VLAN interface in
3636 * the device. Remove it and let bridge code take care of its
3639 err
= mlxsw_sp_port_kill_vid(dev
, 0, 1);
3643 mlxsw_sp_master_bridge_inc(mlxsw_sp_port
->mlxsw_sp
, br_dev
);
3645 mlxsw_sp_port
->learning
= 1;
3646 mlxsw_sp_port
->learning_sync
= 1;
3647 mlxsw_sp_port
->uc_flood
= 1;
3648 mlxsw_sp_port
->bridged
= 1;
3653 static void mlxsw_sp_port_bridge_leave(struct mlxsw_sp_port
*mlxsw_sp_port
)
3655 struct net_device
*dev
= mlxsw_sp_port
->dev
;
3657 mlxsw_sp_port_pvid_set(mlxsw_sp_port
, 1);
3659 mlxsw_sp_master_bridge_dec(mlxsw_sp_port
->mlxsw_sp
);
3661 mlxsw_sp_port
->learning
= 0;
3662 mlxsw_sp_port
->learning_sync
= 0;
3663 mlxsw_sp_port
->uc_flood
= 0;
3664 mlxsw_sp_port
->bridged
= 0;
3666 /* Add implicit VLAN interface in the device, so that untagged
3667 * packets will be classified to the default vFID.
3669 mlxsw_sp_port_add_vid(dev
, 0, 1);
3672 static int mlxsw_sp_lag_create(struct mlxsw_sp
*mlxsw_sp
, u16 lag_id
)
3674 char sldr_pl
[MLXSW_REG_SLDR_LEN
];
3676 mlxsw_reg_sldr_lag_create_pack(sldr_pl
, lag_id
);
3677 return mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(sldr
), sldr_pl
);
3680 static int mlxsw_sp_lag_destroy(struct mlxsw_sp
*mlxsw_sp
, u16 lag_id
)
3682 char sldr_pl
[MLXSW_REG_SLDR_LEN
];
3684 mlxsw_reg_sldr_lag_destroy_pack(sldr_pl
, lag_id
);
3685 return mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(sldr
), sldr_pl
);
3688 static int mlxsw_sp_lag_col_port_add(struct mlxsw_sp_port
*mlxsw_sp_port
,
3689 u16 lag_id
, u8 port_index
)
3691 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
3692 char slcor_pl
[MLXSW_REG_SLCOR_LEN
];
3694 mlxsw_reg_slcor_port_add_pack(slcor_pl
, mlxsw_sp_port
->local_port
,
3695 lag_id
, port_index
);
3696 return mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(slcor
), slcor_pl
);
3699 static int mlxsw_sp_lag_col_port_remove(struct mlxsw_sp_port
*mlxsw_sp_port
,
3702 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
3703 char slcor_pl
[MLXSW_REG_SLCOR_LEN
];
3705 mlxsw_reg_slcor_port_remove_pack(slcor_pl
, mlxsw_sp_port
->local_port
,
3707 return mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(slcor
), slcor_pl
);
3710 static int mlxsw_sp_lag_col_port_enable(struct mlxsw_sp_port
*mlxsw_sp_port
,
3713 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
3714 char slcor_pl
[MLXSW_REG_SLCOR_LEN
];
3716 mlxsw_reg_slcor_col_enable_pack(slcor_pl
, mlxsw_sp_port
->local_port
,
3718 return mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(slcor
), slcor_pl
);
3721 static int mlxsw_sp_lag_col_port_disable(struct mlxsw_sp_port
*mlxsw_sp_port
,
3724 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
3725 char slcor_pl
[MLXSW_REG_SLCOR_LEN
];
3727 mlxsw_reg_slcor_col_disable_pack(slcor_pl
, mlxsw_sp_port
->local_port
,
3729 return mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(slcor
), slcor_pl
);
3732 static int mlxsw_sp_lag_index_get(struct mlxsw_sp
*mlxsw_sp
,
3733 struct net_device
*lag_dev
,
3736 struct mlxsw_sp_upper
*lag
;
3737 int free_lag_id
= -1;
3740 for (i
= 0; i
< MLXSW_SP_LAG_MAX
; i
++) {
3741 lag
= mlxsw_sp_lag_get(mlxsw_sp
, i
);
3742 if (lag
->ref_count
) {
3743 if (lag
->dev
== lag_dev
) {
3747 } else if (free_lag_id
< 0) {
3751 if (free_lag_id
< 0)
3753 *p_lag_id
= free_lag_id
;
3758 mlxsw_sp_master_lag_check(struct mlxsw_sp
*mlxsw_sp
,
3759 struct net_device
*lag_dev
,
3760 struct netdev_lag_upper_info
*lag_upper_info
)
3764 if (mlxsw_sp_lag_index_get(mlxsw_sp
, lag_dev
, &lag_id
) != 0)
3766 if (lag_upper_info
->tx_type
!= NETDEV_LAG_TX_TYPE_HASH
)
3771 static int mlxsw_sp_port_lag_index_get(struct mlxsw_sp
*mlxsw_sp
,
3772 u16 lag_id
, u8
*p_port_index
)
3776 for (i
= 0; i
< MLXSW_SP_PORT_PER_LAG_MAX
; i
++) {
3777 if (!mlxsw_sp_port_lagged_get(mlxsw_sp
, lag_id
, i
)) {
3786 mlxsw_sp_port_pvid_vport_lag_join(struct mlxsw_sp_port
*mlxsw_sp_port
,
3789 struct mlxsw_sp_port
*mlxsw_sp_vport
;
3790 struct mlxsw_sp_fid
*f
;
3792 mlxsw_sp_vport
= mlxsw_sp_port_vport_find(mlxsw_sp_port
, 1);
3793 if (WARN_ON(!mlxsw_sp_vport
))
3796 /* If vPort is assigned a RIF, then leave it since it's no
3799 f
= mlxsw_sp_vport_fid_get(mlxsw_sp_vport
);
3801 f
->leave(mlxsw_sp_vport
);
3803 mlxsw_sp_vport
->lag_id
= lag_id
;
3804 mlxsw_sp_vport
->lagged
= 1;
3808 mlxsw_sp_port_pvid_vport_lag_leave(struct mlxsw_sp_port
*mlxsw_sp_port
)
3810 struct mlxsw_sp_port
*mlxsw_sp_vport
;
3811 struct mlxsw_sp_fid
*f
;
3813 mlxsw_sp_vport
= mlxsw_sp_port_vport_find(mlxsw_sp_port
, 1);
3814 if (WARN_ON(!mlxsw_sp_vport
))
3817 f
= mlxsw_sp_vport_fid_get(mlxsw_sp_vport
);
3819 f
->leave(mlxsw_sp_vport
);
3821 mlxsw_sp_vport
->lagged
= 0;
3824 static int mlxsw_sp_port_lag_join(struct mlxsw_sp_port
*mlxsw_sp_port
,
3825 struct net_device
*lag_dev
)
3827 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
3828 struct mlxsw_sp_upper
*lag
;
3833 err
= mlxsw_sp_lag_index_get(mlxsw_sp
, lag_dev
, &lag_id
);
3836 lag
= mlxsw_sp_lag_get(mlxsw_sp
, lag_id
);
3837 if (!lag
->ref_count
) {
3838 err
= mlxsw_sp_lag_create(mlxsw_sp
, lag_id
);
3844 err
= mlxsw_sp_port_lag_index_get(mlxsw_sp
, lag_id
, &port_index
);
3847 err
= mlxsw_sp_lag_col_port_add(mlxsw_sp_port
, lag_id
, port_index
);
3849 goto err_col_port_add
;
3850 err
= mlxsw_sp_lag_col_port_enable(mlxsw_sp_port
, lag_id
);
3852 goto err_col_port_enable
;
3854 mlxsw_core_lag_mapping_set(mlxsw_sp
->core
, lag_id
, port_index
,
3855 mlxsw_sp_port
->local_port
);
3856 mlxsw_sp_port
->lag_id
= lag_id
;
3857 mlxsw_sp_port
->lagged
= 1;
3860 mlxsw_sp_port_pvid_vport_lag_join(mlxsw_sp_port
, lag_id
);
3864 err_col_port_enable
:
3865 mlxsw_sp_lag_col_port_remove(mlxsw_sp_port
, lag_id
);
3867 if (!lag
->ref_count
)
3868 mlxsw_sp_lag_destroy(mlxsw_sp
, lag_id
);
3872 static void mlxsw_sp_port_lag_leave(struct mlxsw_sp_port
*mlxsw_sp_port
,
3873 struct net_device
*lag_dev
)
3875 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
3876 u16 lag_id
= mlxsw_sp_port
->lag_id
;
3877 struct mlxsw_sp_upper
*lag
;
3879 if (!mlxsw_sp_port
->lagged
)
3881 lag
= mlxsw_sp_lag_get(mlxsw_sp
, lag_id
);
3882 WARN_ON(lag
->ref_count
== 0);
3884 mlxsw_sp_lag_col_port_disable(mlxsw_sp_port
, lag_id
);
3885 mlxsw_sp_lag_col_port_remove(mlxsw_sp_port
, lag_id
);
3887 if (mlxsw_sp_port
->bridged
) {
3888 mlxsw_sp_port_active_vlans_del(mlxsw_sp_port
);
3889 mlxsw_sp_port_bridge_leave(mlxsw_sp_port
);
3892 if (lag
->ref_count
== 1)
3893 mlxsw_sp_lag_destroy(mlxsw_sp
, lag_id
);
3895 mlxsw_core_lag_mapping_clear(mlxsw_sp
->core
, lag_id
,
3896 mlxsw_sp_port
->local_port
);
3897 mlxsw_sp_port
->lagged
= 0;
3900 mlxsw_sp_port_pvid_vport_lag_leave(mlxsw_sp_port
);
3903 static int mlxsw_sp_lag_dist_port_add(struct mlxsw_sp_port
*mlxsw_sp_port
,
3906 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
3907 char sldr_pl
[MLXSW_REG_SLDR_LEN
];
3909 mlxsw_reg_sldr_lag_add_port_pack(sldr_pl
, lag_id
,
3910 mlxsw_sp_port
->local_port
);
3911 return mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(sldr
), sldr_pl
);
3914 static int mlxsw_sp_lag_dist_port_remove(struct mlxsw_sp_port
*mlxsw_sp_port
,
3917 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
3918 char sldr_pl
[MLXSW_REG_SLDR_LEN
];
3920 mlxsw_reg_sldr_lag_remove_port_pack(sldr_pl
, lag_id
,
3921 mlxsw_sp_port
->local_port
);
3922 return mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(sldr
), sldr_pl
);
3925 static int mlxsw_sp_port_lag_tx_en_set(struct mlxsw_sp_port
*mlxsw_sp_port
,
3926 bool lag_tx_enabled
)
3929 return mlxsw_sp_lag_dist_port_add(mlxsw_sp_port
,
3930 mlxsw_sp_port
->lag_id
);
3932 return mlxsw_sp_lag_dist_port_remove(mlxsw_sp_port
,
3933 mlxsw_sp_port
->lag_id
);
3936 static int mlxsw_sp_port_lag_changed(struct mlxsw_sp_port
*mlxsw_sp_port
,
3937 struct netdev_lag_lower_state_info
*info
)
3939 return mlxsw_sp_port_lag_tx_en_set(mlxsw_sp_port
, info
->tx_enabled
);
3942 static int mlxsw_sp_port_vlan_link(struct mlxsw_sp_port
*mlxsw_sp_port
,
3943 struct net_device
*vlan_dev
)
3945 struct mlxsw_sp_port
*mlxsw_sp_vport
;
3946 u16 vid
= vlan_dev_vlan_id(vlan_dev
);
3948 mlxsw_sp_vport
= mlxsw_sp_port_vport_find(mlxsw_sp_port
, vid
);
3949 if (WARN_ON(!mlxsw_sp_vport
))
3952 mlxsw_sp_vport
->dev
= vlan_dev
;
3957 static void mlxsw_sp_port_vlan_unlink(struct mlxsw_sp_port
*mlxsw_sp_port
,
3958 struct net_device
*vlan_dev
)
3960 struct mlxsw_sp_port
*mlxsw_sp_vport
;
3961 u16 vid
= vlan_dev_vlan_id(vlan_dev
);
3963 mlxsw_sp_vport
= mlxsw_sp_port_vport_find(mlxsw_sp_port
, vid
);
3964 if (WARN_ON(!mlxsw_sp_vport
))
3967 mlxsw_sp_vport
->dev
= mlxsw_sp_port
->dev
;
3970 static int mlxsw_sp_netdevice_port_upper_event(struct net_device
*dev
,
3971 unsigned long event
, void *ptr
)
3973 struct netdev_notifier_changeupper_info
*info
;
3974 struct mlxsw_sp_port
*mlxsw_sp_port
;
3975 struct net_device
*upper_dev
;
3976 struct mlxsw_sp
*mlxsw_sp
;
3979 mlxsw_sp_port
= netdev_priv(dev
);
3980 mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
3984 case NETDEV_PRECHANGEUPPER
:
3985 upper_dev
= info
->upper_dev
;
3986 if (!is_vlan_dev(upper_dev
) &&
3987 !netif_is_lag_master(upper_dev
) &&
3988 !netif_is_bridge_master(upper_dev
))
3992 /* HW limitation forbids to put ports to multiple bridges. */
3993 if (netif_is_bridge_master(upper_dev
) &&
3994 !mlxsw_sp_master_bridge_check(mlxsw_sp
, upper_dev
))
3996 if (netif_is_lag_master(upper_dev
) &&
3997 !mlxsw_sp_master_lag_check(mlxsw_sp
, upper_dev
,
4000 if (netif_is_lag_master(upper_dev
) && vlan_uses_dev(dev
))
4002 if (netif_is_lag_port(dev
) && is_vlan_dev(upper_dev
) &&
4003 !netif_is_lag_master(vlan_dev_real_dev(upper_dev
)))
4006 case NETDEV_CHANGEUPPER
:
4007 upper_dev
= info
->upper_dev
;
4008 if (is_vlan_dev(upper_dev
)) {
4010 err
= mlxsw_sp_port_vlan_link(mlxsw_sp_port
,
4013 mlxsw_sp_port_vlan_unlink(mlxsw_sp_port
,
4015 } else if (netif_is_bridge_master(upper_dev
)) {
4017 err
= mlxsw_sp_port_bridge_join(mlxsw_sp_port
,
4020 mlxsw_sp_port_bridge_leave(mlxsw_sp_port
);
4021 } else if (netif_is_lag_master(upper_dev
)) {
4023 err
= mlxsw_sp_port_lag_join(mlxsw_sp_port
,
4026 mlxsw_sp_port_lag_leave(mlxsw_sp_port
,
4038 static int mlxsw_sp_netdevice_port_lower_event(struct net_device
*dev
,
4039 unsigned long event
, void *ptr
)
4041 struct netdev_notifier_changelowerstate_info
*info
;
4042 struct mlxsw_sp_port
*mlxsw_sp_port
;
4045 mlxsw_sp_port
= netdev_priv(dev
);
4049 case NETDEV_CHANGELOWERSTATE
:
4050 if (netif_is_lag_port(dev
) && mlxsw_sp_port
->lagged
) {
4051 err
= mlxsw_sp_port_lag_changed(mlxsw_sp_port
,
4052 info
->lower_state_info
);
4054 netdev_err(dev
, "Failed to reflect link aggregation lower state change\n");
4062 static int mlxsw_sp_netdevice_port_event(struct net_device
*dev
,
4063 unsigned long event
, void *ptr
)
4066 case NETDEV_PRECHANGEUPPER
:
4067 case NETDEV_CHANGEUPPER
:
4068 return mlxsw_sp_netdevice_port_upper_event(dev
, event
, ptr
);
4069 case NETDEV_CHANGELOWERSTATE
:
4070 return mlxsw_sp_netdevice_port_lower_event(dev
, event
, ptr
);
4076 static int mlxsw_sp_netdevice_lag_event(struct net_device
*lag_dev
,
4077 unsigned long event
, void *ptr
)
4079 struct net_device
*dev
;
4080 struct list_head
*iter
;
4083 netdev_for_each_lower_dev(lag_dev
, dev
, iter
) {
4084 if (mlxsw_sp_port_dev_check(dev
)) {
4085 ret
= mlxsw_sp_netdevice_port_event(dev
, event
, ptr
);
4094 static int mlxsw_sp_master_bridge_vlan_link(struct mlxsw_sp
*mlxsw_sp
,
4095 struct net_device
*vlan_dev
)
4097 u16 fid
= vlan_dev_vlan_id(vlan_dev
);
4098 struct mlxsw_sp_fid
*f
;
4100 f
= mlxsw_sp_fid_find(mlxsw_sp
, fid
);
4102 f
= mlxsw_sp_fid_create(mlxsw_sp
, fid
);
4112 static void mlxsw_sp_master_bridge_vlan_unlink(struct mlxsw_sp
*mlxsw_sp
,
4113 struct net_device
*vlan_dev
)
4115 u16 fid
= vlan_dev_vlan_id(vlan_dev
);
4116 struct mlxsw_sp_fid
*f
;
4118 f
= mlxsw_sp_fid_find(mlxsw_sp
, fid
);
4120 mlxsw_sp_rif_bridge_destroy(mlxsw_sp
, f
->r
);
4121 if (f
&& --f
->ref_count
== 0)
4122 mlxsw_sp_fid_destroy(mlxsw_sp
, f
);
4125 static int mlxsw_sp_netdevice_bridge_event(struct net_device
*br_dev
,
4126 unsigned long event
, void *ptr
)
4128 struct netdev_notifier_changeupper_info
*info
;
4129 struct net_device
*upper_dev
;
4130 struct mlxsw_sp
*mlxsw_sp
;
4133 mlxsw_sp
= mlxsw_sp_lower_get(br_dev
);
4136 if (br_dev
!= mlxsw_sp
->master_bridge
.dev
)
4142 case NETDEV_CHANGEUPPER
:
4143 upper_dev
= info
->upper_dev
;
4144 if (!is_vlan_dev(upper_dev
))
4146 if (info
->linking
) {
4147 err
= mlxsw_sp_master_bridge_vlan_link(mlxsw_sp
,
4152 mlxsw_sp_master_bridge_vlan_unlink(mlxsw_sp
, upper_dev
);
4160 static u16
mlxsw_sp_avail_vfid_get(const struct mlxsw_sp
*mlxsw_sp
)
4162 return find_first_zero_bit(mlxsw_sp
->vfids
.mapped
,
4166 static int mlxsw_sp_vfid_op(struct mlxsw_sp
*mlxsw_sp
, u16 fid
, bool create
)
4168 char sfmr_pl
[MLXSW_REG_SFMR_LEN
];
4170 mlxsw_reg_sfmr_pack(sfmr_pl
, !create
, fid
, 0);
4171 return mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(sfmr
), sfmr_pl
);
4174 static void mlxsw_sp_vport_vfid_leave(struct mlxsw_sp_port
*mlxsw_sp_vport
);
4176 static struct mlxsw_sp_fid
*mlxsw_sp_vfid_create(struct mlxsw_sp
*mlxsw_sp
,
4177 struct net_device
*br_dev
)
4179 struct device
*dev
= mlxsw_sp
->bus_info
->dev
;
4180 struct mlxsw_sp_fid
*f
;
4184 vfid
= mlxsw_sp_avail_vfid_get(mlxsw_sp
);
4185 if (vfid
== MLXSW_SP_VFID_MAX
) {
4186 dev_err(dev
, "No available vFIDs\n");
4187 return ERR_PTR(-ERANGE
);
4190 fid
= mlxsw_sp_vfid_to_fid(vfid
);
4191 err
= mlxsw_sp_vfid_op(mlxsw_sp
, fid
, true);
4193 dev_err(dev
, "Failed to create FID=%d\n", fid
);
4194 return ERR_PTR(err
);
4197 f
= kzalloc(sizeof(*f
), GFP_KERNEL
);
4199 goto err_allocate_vfid
;
4201 f
->leave
= mlxsw_sp_vport_vfid_leave
;
4205 list_add(&f
->list
, &mlxsw_sp
->vfids
.list
);
4206 set_bit(vfid
, mlxsw_sp
->vfids
.mapped
);
4211 mlxsw_sp_vfid_op(mlxsw_sp
, fid
, false);
4212 return ERR_PTR(-ENOMEM
);
4215 static void mlxsw_sp_vfid_destroy(struct mlxsw_sp
*mlxsw_sp
,
4216 struct mlxsw_sp_fid
*f
)
4218 u16 vfid
= mlxsw_sp_fid_to_vfid(f
->fid
);
4221 clear_bit(vfid
, mlxsw_sp
->vfids
.mapped
);
4225 mlxsw_sp_rif_bridge_destroy(mlxsw_sp
, f
->r
);
4229 mlxsw_sp_vfid_op(mlxsw_sp
, fid
, false);
4232 static int mlxsw_sp_vport_fid_map(struct mlxsw_sp_port
*mlxsw_sp_vport
, u16 fid
,
4235 enum mlxsw_reg_svfa_mt mt
= MLXSW_REG_SVFA_MT_PORT_VID_TO_FID
;
4236 u16 vid
= mlxsw_sp_vport_vid_get(mlxsw_sp_vport
);
4238 return mlxsw_sp_port_vid_to_fid_set(mlxsw_sp_vport
, mt
, valid
, fid
,
4242 static int mlxsw_sp_vport_vfid_join(struct mlxsw_sp_port
*mlxsw_sp_vport
,
4243 struct net_device
*br_dev
)
4245 struct mlxsw_sp_fid
*f
;
4248 f
= mlxsw_sp_vfid_find(mlxsw_sp_vport
->mlxsw_sp
, br_dev
);
4250 f
= mlxsw_sp_vfid_create(mlxsw_sp_vport
->mlxsw_sp
, br_dev
);
4255 err
= mlxsw_sp_vport_flood_set(mlxsw_sp_vport
, f
->fid
, true);
4257 goto err_vport_flood_set
;
4259 err
= mlxsw_sp_vport_fid_map(mlxsw_sp_vport
, f
->fid
, true);
4261 goto err_vport_fid_map
;
4263 mlxsw_sp_vport_fid_set(mlxsw_sp_vport
, f
);
4266 netdev_dbg(mlxsw_sp_vport
->dev
, "Joined FID=%d\n", f
->fid
);
4271 mlxsw_sp_vport_flood_set(mlxsw_sp_vport
, f
->fid
, false);
4272 err_vport_flood_set
:
4274 mlxsw_sp_vfid_destroy(mlxsw_sp_vport
->mlxsw_sp
, f
);
4278 static void mlxsw_sp_vport_vfid_leave(struct mlxsw_sp_port
*mlxsw_sp_vport
)
4280 struct mlxsw_sp_fid
*f
= mlxsw_sp_vport_fid_get(mlxsw_sp_vport
);
4282 netdev_dbg(mlxsw_sp_vport
->dev
, "Left FID=%d\n", f
->fid
);
4284 mlxsw_sp_vport_fid_map(mlxsw_sp_vport
, f
->fid
, false);
4286 mlxsw_sp_vport_flood_set(mlxsw_sp_vport
, f
->fid
, false);
4288 mlxsw_sp_port_fdb_flush(mlxsw_sp_vport
, f
->fid
);
4290 mlxsw_sp_vport_fid_set(mlxsw_sp_vport
, NULL
);
4291 if (--f
->ref_count
== 0)
4292 mlxsw_sp_vfid_destroy(mlxsw_sp_vport
->mlxsw_sp
, f
);
4295 static int mlxsw_sp_vport_bridge_join(struct mlxsw_sp_port
*mlxsw_sp_vport
,
4296 struct net_device
*br_dev
)
4298 struct mlxsw_sp_fid
*f
= mlxsw_sp_vport_fid_get(mlxsw_sp_vport
);
4299 u16 vid
= mlxsw_sp_vport_vid_get(mlxsw_sp_vport
);
4300 struct net_device
*dev
= mlxsw_sp_vport
->dev
;
4303 if (f
&& !WARN_ON(!f
->leave
))
4304 f
->leave(mlxsw_sp_vport
);
4306 err
= mlxsw_sp_vport_vfid_join(mlxsw_sp_vport
, br_dev
);
4308 netdev_err(dev
, "Failed to join vFID\n");
4312 err
= mlxsw_sp_port_vid_learning_set(mlxsw_sp_vport
, vid
, true);
4314 netdev_err(dev
, "Failed to enable learning\n");
4315 goto err_port_vid_learning_set
;
4318 mlxsw_sp_vport
->learning
= 1;
4319 mlxsw_sp_vport
->learning_sync
= 1;
4320 mlxsw_sp_vport
->uc_flood
= 1;
4321 mlxsw_sp_vport
->bridged
= 1;
4325 err_port_vid_learning_set
:
4326 mlxsw_sp_vport_vfid_leave(mlxsw_sp_vport
);
4330 static void mlxsw_sp_vport_bridge_leave(struct mlxsw_sp_port
*mlxsw_sp_vport
)
4332 u16 vid
= mlxsw_sp_vport_vid_get(mlxsw_sp_vport
);
4334 mlxsw_sp_port_vid_learning_set(mlxsw_sp_vport
, vid
, false);
4336 mlxsw_sp_vport_vfid_leave(mlxsw_sp_vport
);
4338 mlxsw_sp_vport
->learning
= 0;
4339 mlxsw_sp_vport
->learning_sync
= 0;
4340 mlxsw_sp_vport
->uc_flood
= 0;
4341 mlxsw_sp_vport
->bridged
= 0;
4345 mlxsw_sp_port_master_bridge_check(const struct mlxsw_sp_port
*mlxsw_sp_port
,
4346 const struct net_device
*br_dev
)
4348 struct mlxsw_sp_port
*mlxsw_sp_vport
;
4350 list_for_each_entry(mlxsw_sp_vport
, &mlxsw_sp_port
->vports_list
,
4352 struct net_device
*dev
= mlxsw_sp_vport_dev_get(mlxsw_sp_vport
);
4354 if (dev
&& dev
== br_dev
)
4361 static int mlxsw_sp_netdevice_vport_event(struct net_device
*dev
,
4362 unsigned long event
, void *ptr
,
4365 struct mlxsw_sp_port
*mlxsw_sp_port
= netdev_priv(dev
);
4366 struct netdev_notifier_changeupper_info
*info
= ptr
;
4367 struct mlxsw_sp_port
*mlxsw_sp_vport
;
4368 struct net_device
*upper_dev
;
4371 mlxsw_sp_vport
= mlxsw_sp_port_vport_find(mlxsw_sp_port
, vid
);
4374 case NETDEV_PRECHANGEUPPER
:
4375 upper_dev
= info
->upper_dev
;
4376 if (!netif_is_bridge_master(upper_dev
))
4380 /* We can't have multiple VLAN interfaces configured on
4381 * the same port and being members in the same bridge.
4383 if (!mlxsw_sp_port_master_bridge_check(mlxsw_sp_port
,
4387 case NETDEV_CHANGEUPPER
:
4388 upper_dev
= info
->upper_dev
;
4389 if (info
->linking
) {
4390 if (WARN_ON(!mlxsw_sp_vport
))
4392 err
= mlxsw_sp_vport_bridge_join(mlxsw_sp_vport
,
4395 if (!mlxsw_sp_vport
)
4397 mlxsw_sp_vport_bridge_leave(mlxsw_sp_vport
);
4404 static int mlxsw_sp_netdevice_lag_vport_event(struct net_device
*lag_dev
,
4405 unsigned long event
, void *ptr
,
4408 struct net_device
*dev
;
4409 struct list_head
*iter
;
4412 netdev_for_each_lower_dev(lag_dev
, dev
, iter
) {
4413 if (mlxsw_sp_port_dev_check(dev
)) {
4414 ret
= mlxsw_sp_netdevice_vport_event(dev
, event
, ptr
,
4424 static int mlxsw_sp_netdevice_vlan_event(struct net_device
*vlan_dev
,
4425 unsigned long event
, void *ptr
)
4427 struct net_device
*real_dev
= vlan_dev_real_dev(vlan_dev
);
4428 u16 vid
= vlan_dev_vlan_id(vlan_dev
);
4430 if (mlxsw_sp_port_dev_check(real_dev
))
4431 return mlxsw_sp_netdevice_vport_event(real_dev
, event
, ptr
,
4433 else if (netif_is_lag_master(real_dev
))
4434 return mlxsw_sp_netdevice_lag_vport_event(real_dev
, event
, ptr
,
4440 static int mlxsw_sp_netdevice_event(struct notifier_block
*unused
,
4441 unsigned long event
, void *ptr
)
4443 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
4446 if (event
== NETDEV_CHANGEADDR
|| event
== NETDEV_CHANGEMTU
)
4447 err
= mlxsw_sp_netdevice_router_port_event(dev
);
4448 else if (mlxsw_sp_port_dev_check(dev
))
4449 err
= mlxsw_sp_netdevice_port_event(dev
, event
, ptr
);
4450 else if (netif_is_lag_master(dev
))
4451 err
= mlxsw_sp_netdevice_lag_event(dev
, event
, ptr
);
4452 else if (netif_is_bridge_master(dev
))
4453 err
= mlxsw_sp_netdevice_bridge_event(dev
, event
, ptr
);
4454 else if (is_vlan_dev(dev
))
4455 err
= mlxsw_sp_netdevice_vlan_event(dev
, event
, ptr
);
4457 return notifier_from_errno(err
);
4460 static struct notifier_block mlxsw_sp_netdevice_nb __read_mostly
= {
4461 .notifier_call
= mlxsw_sp_netdevice_event
,
4464 static struct notifier_block mlxsw_sp_inetaddr_nb __read_mostly
= {
4465 .notifier_call
= mlxsw_sp_inetaddr_event
,
4466 .priority
= 10, /* Must be called before FIB notifier block */
4469 static struct notifier_block mlxsw_sp_router_netevent_nb __read_mostly
= {
4470 .notifier_call
= mlxsw_sp_router_netevent_event
,
4473 static int __init
mlxsw_sp_module_init(void)
4477 register_netdevice_notifier(&mlxsw_sp_netdevice_nb
);
4478 register_inetaddr_notifier(&mlxsw_sp_inetaddr_nb
);
4479 register_netevent_notifier(&mlxsw_sp_router_netevent_nb
);
4481 err
= mlxsw_core_driver_register(&mlxsw_sp_driver
);
4483 goto err_core_driver_register
;
4486 err_core_driver_register
:
4487 unregister_netevent_notifier(&mlxsw_sp_router_netevent_nb
);
4488 unregister_inetaddr_notifier(&mlxsw_sp_inetaddr_nb
);
4489 unregister_netdevice_notifier(&mlxsw_sp_netdevice_nb
);
4493 static void __exit
mlxsw_sp_module_exit(void)
4495 mlxsw_core_driver_unregister(&mlxsw_sp_driver
);
4496 unregister_netevent_notifier(&mlxsw_sp_router_netevent_nb
);
4497 unregister_inetaddr_notifier(&mlxsw_sp_inetaddr_nb
);
4498 unregister_netdevice_notifier(&mlxsw_sp_netdevice_nb
);
4501 module_init(mlxsw_sp_module_init
);
4502 module_exit(mlxsw_sp_module_exit
);
4504 MODULE_LICENSE("Dual BSD/GPL");
4505 MODULE_AUTHOR("Jiri Pirko <jiri@mellanox.com>");
4506 MODULE_DESCRIPTION("Mellanox Spectrum driver");
4507 MODULE_MLXSW_DRIVER_ALIAS(MLXSW_DEVICE_KIND_SPECTRUM
);