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/pci.h>
41 #include <linux/netdevice.h>
42 #include <linux/etherdevice.h>
43 #include <linux/ethtool.h>
44 #include <linux/slab.h>
45 #include <linux/device.h>
46 #include <linux/skbuff.h>
47 #include <linux/if_vlan.h>
48 #include <linux/if_bridge.h>
49 #include <linux/workqueue.h>
50 #include <linux/jiffies.h>
51 #include <linux/bitops.h>
52 #include <linux/list.h>
53 #include <linux/notifier.h>
54 #include <linux/dcbnl.h>
55 #include <linux/inetdevice.h>
56 #include <net/switchdev.h>
57 #include <net/pkt_cls.h>
58 #include <net/tc_act/tc_mirred.h>
59 #include <net/netevent.h>
69 static const char mlxsw_sp_driver_name
[] = "mlxsw_spectrum";
70 static const char mlxsw_sp_driver_version
[] = "1.0";
76 MLXSW_ITEM32(tx
, hdr
, version
, 0x00, 28, 4);
79 * Packet control type.
80 * 0 - Ethernet control (e.g. EMADs, LACP)
83 MLXSW_ITEM32(tx
, hdr
, ctl
, 0x00, 26, 2);
86 * Packet protocol type. Must be set to 1 (Ethernet).
88 MLXSW_ITEM32(tx
, hdr
, proto
, 0x00, 21, 3);
90 /* tx_hdr_rx_is_router
91 * Packet is sent from the router. Valid for data packets only.
93 MLXSW_ITEM32(tx
, hdr
, rx_is_router
, 0x00, 19, 1);
96 * Indicates if the 'fid' field is valid and should be used for
97 * forwarding lookup. Valid for data packets only.
99 MLXSW_ITEM32(tx
, hdr
, fid_valid
, 0x00, 16, 1);
102 * Switch partition ID. Must be set to 0.
104 MLXSW_ITEM32(tx
, hdr
, swid
, 0x00, 12, 3);
106 /* tx_hdr_control_tclass
107 * Indicates if the packet should use the control TClass and not one
108 * of the data TClasses.
110 MLXSW_ITEM32(tx
, hdr
, control_tclass
, 0x00, 6, 1);
113 * Egress TClass to be used on the egress device on the egress port.
115 MLXSW_ITEM32(tx
, hdr
, etclass
, 0x00, 0, 4);
118 * Destination local port for unicast packets.
119 * Destination multicast ID for multicast packets.
121 * Control packets are directed to a specific egress port, while data
122 * packets are transmitted through the CPU port (0) into the switch partition,
123 * where forwarding rules are applied.
125 MLXSW_ITEM32(tx
, hdr
, port_mid
, 0x04, 16, 16);
128 * Forwarding ID used for L2 forwarding lookup. Valid only if 'fid_valid' is
129 * set, otherwise calculated based on the packet's VID using VID to FID mapping.
130 * Valid for data packets only.
132 MLXSW_ITEM32(tx
, hdr
, fid
, 0x08, 0, 16);
136 * 6 - Control packets
138 MLXSW_ITEM32(tx
, hdr
, type
, 0x0C, 0, 4);
140 static bool mlxsw_sp_port_dev_check(const struct net_device
*dev
);
142 static void mlxsw_sp_txhdr_construct(struct sk_buff
*skb
,
143 const struct mlxsw_tx_info
*tx_info
)
145 char *txhdr
= skb_push(skb
, MLXSW_TXHDR_LEN
);
147 memset(txhdr
, 0, MLXSW_TXHDR_LEN
);
149 mlxsw_tx_hdr_version_set(txhdr
, MLXSW_TXHDR_VERSION_1
);
150 mlxsw_tx_hdr_ctl_set(txhdr
, MLXSW_TXHDR_ETH_CTL
);
151 mlxsw_tx_hdr_proto_set(txhdr
, MLXSW_TXHDR_PROTO_ETH
);
152 mlxsw_tx_hdr_swid_set(txhdr
, 0);
153 mlxsw_tx_hdr_control_tclass_set(txhdr
, 1);
154 mlxsw_tx_hdr_port_mid_set(txhdr
, tx_info
->local_port
);
155 mlxsw_tx_hdr_type_set(txhdr
, MLXSW_TXHDR_TYPE_CONTROL
);
158 static int mlxsw_sp_base_mac_get(struct mlxsw_sp
*mlxsw_sp
)
160 char spad_pl
[MLXSW_REG_SPAD_LEN
] = {0};
163 err
= mlxsw_reg_query(mlxsw_sp
->core
, MLXSW_REG(spad
), spad_pl
);
166 mlxsw_reg_spad_base_mac_memcpy_from(spad_pl
, mlxsw_sp
->base_mac
);
170 static int mlxsw_sp_span_init(struct mlxsw_sp
*mlxsw_sp
)
174 if (!MLXSW_CORE_RES_VALID(mlxsw_sp
->core
, MAX_SPAN
))
177 mlxsw_sp
->span
.entries_count
= MLXSW_CORE_RES_GET(mlxsw_sp
->core
,
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
= 1;
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 static struct mlxsw_sp_span_entry
*
252 mlxsw_sp_span_entry_find(struct mlxsw_sp_port
*port
)
254 struct mlxsw_sp
*mlxsw_sp
= port
->mlxsw_sp
;
257 for (i
= 0; i
< mlxsw_sp
->span
.entries_count
; i
++) {
258 struct mlxsw_sp_span_entry
*curr
= &mlxsw_sp
->span
.entries
[i
];
260 if (curr
->used
&& curr
->local_port
== port
->local_port
)
266 static struct mlxsw_sp_span_entry
267 *mlxsw_sp_span_entry_get(struct mlxsw_sp_port
*port
)
269 struct mlxsw_sp_span_entry
*span_entry
;
271 span_entry
= mlxsw_sp_span_entry_find(port
);
273 /* Already exists, just take a reference */
274 span_entry
->ref_count
++;
278 return mlxsw_sp_span_entry_create(port
);
281 static int mlxsw_sp_span_entry_put(struct mlxsw_sp
*mlxsw_sp
,
282 struct mlxsw_sp_span_entry
*span_entry
)
284 WARN_ON(!span_entry
->ref_count
);
285 if (--span_entry
->ref_count
== 0)
286 mlxsw_sp_span_entry_destroy(mlxsw_sp
, span_entry
);
290 static bool mlxsw_sp_span_is_egress_mirror(struct mlxsw_sp_port
*port
)
292 struct mlxsw_sp
*mlxsw_sp
= port
->mlxsw_sp
;
293 struct mlxsw_sp_span_inspected_port
*p
;
296 for (i
= 0; i
< mlxsw_sp
->span
.entries_count
; i
++) {
297 struct mlxsw_sp_span_entry
*curr
= &mlxsw_sp
->span
.entries
[i
];
299 list_for_each_entry(p
, &curr
->bound_ports_list
, list
)
300 if (p
->local_port
== port
->local_port
&&
301 p
->type
== MLXSW_SP_SPAN_EGRESS
)
308 static int mlxsw_sp_span_mtu_to_buffsize(int mtu
)
310 return MLXSW_SP_BYTES_TO_CELLS(mtu
* 5 / 2) + 1;
313 static int mlxsw_sp_span_port_mtu_update(struct mlxsw_sp_port
*port
, u16 mtu
)
315 struct mlxsw_sp
*mlxsw_sp
= port
->mlxsw_sp
;
316 char sbib_pl
[MLXSW_REG_SBIB_LEN
];
319 /* If port is egress mirrored, the shared buffer size should be
320 * updated according to the mtu value
322 if (mlxsw_sp_span_is_egress_mirror(port
)) {
323 mlxsw_reg_sbib_pack(sbib_pl
, port
->local_port
,
324 mlxsw_sp_span_mtu_to_buffsize(mtu
));
325 err
= mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(sbib
), sbib_pl
);
327 netdev_err(port
->dev
, "Could not update shared buffer for mirroring\n");
335 static struct mlxsw_sp_span_inspected_port
*
336 mlxsw_sp_span_entry_bound_port_find(struct mlxsw_sp_port
*port
,
337 struct mlxsw_sp_span_entry
*span_entry
)
339 struct mlxsw_sp_span_inspected_port
*p
;
341 list_for_each_entry(p
, &span_entry
->bound_ports_list
, list
)
342 if (port
->local_port
== p
->local_port
)
348 mlxsw_sp_span_inspected_port_bind(struct mlxsw_sp_port
*port
,
349 struct mlxsw_sp_span_entry
*span_entry
,
350 enum mlxsw_sp_span_type type
)
352 struct mlxsw_sp_span_inspected_port
*inspected_port
;
353 struct mlxsw_sp
*mlxsw_sp
= port
->mlxsw_sp
;
354 char mpar_pl
[MLXSW_REG_MPAR_LEN
];
355 char sbib_pl
[MLXSW_REG_SBIB_LEN
];
356 int pa_id
= span_entry
->id
;
359 /* if it is an egress SPAN, bind a shared buffer to it */
360 if (type
== MLXSW_SP_SPAN_EGRESS
) {
361 mlxsw_reg_sbib_pack(sbib_pl
, port
->local_port
,
362 mlxsw_sp_span_mtu_to_buffsize(port
->dev
->mtu
));
363 err
= mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(sbib
), sbib_pl
);
365 netdev_err(port
->dev
, "Could not create shared buffer for mirroring\n");
370 /* bind the port to the SPAN entry */
371 mlxsw_reg_mpar_pack(mpar_pl
, port
->local_port
,
372 (enum mlxsw_reg_mpar_i_e
) type
, true, pa_id
);
373 err
= mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(mpar
), mpar_pl
);
375 goto err_mpar_reg_write
;
377 inspected_port
= kzalloc(sizeof(*inspected_port
), GFP_KERNEL
);
378 if (!inspected_port
) {
380 goto err_inspected_port_alloc
;
382 inspected_port
->local_port
= port
->local_port
;
383 inspected_port
->type
= type
;
384 list_add_tail(&inspected_port
->list
, &span_entry
->bound_ports_list
);
389 err_inspected_port_alloc
:
390 if (type
== MLXSW_SP_SPAN_EGRESS
) {
391 mlxsw_reg_sbib_pack(sbib_pl
, port
->local_port
, 0);
392 mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(sbib
), sbib_pl
);
398 mlxsw_sp_span_inspected_port_unbind(struct mlxsw_sp_port
*port
,
399 struct mlxsw_sp_span_entry
*span_entry
,
400 enum mlxsw_sp_span_type type
)
402 struct mlxsw_sp_span_inspected_port
*inspected_port
;
403 struct mlxsw_sp
*mlxsw_sp
= port
->mlxsw_sp
;
404 char mpar_pl
[MLXSW_REG_MPAR_LEN
];
405 char sbib_pl
[MLXSW_REG_SBIB_LEN
];
406 int pa_id
= span_entry
->id
;
408 inspected_port
= mlxsw_sp_span_entry_bound_port_find(port
, span_entry
);
412 /* remove the inspected port */
413 mlxsw_reg_mpar_pack(mpar_pl
, port
->local_port
,
414 (enum mlxsw_reg_mpar_i_e
) type
, false, pa_id
);
415 mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(mpar
), mpar_pl
);
417 /* remove the SBIB buffer if it was egress SPAN */
418 if (type
== MLXSW_SP_SPAN_EGRESS
) {
419 mlxsw_reg_sbib_pack(sbib_pl
, port
->local_port
, 0);
420 mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(sbib
), sbib_pl
);
423 mlxsw_sp_span_entry_put(mlxsw_sp
, span_entry
);
425 list_del(&inspected_port
->list
);
426 kfree(inspected_port
);
429 static int mlxsw_sp_span_mirror_add(struct mlxsw_sp_port
*from
,
430 struct mlxsw_sp_port
*to
,
431 enum mlxsw_sp_span_type type
)
433 struct mlxsw_sp
*mlxsw_sp
= from
->mlxsw_sp
;
434 struct mlxsw_sp_span_entry
*span_entry
;
437 span_entry
= mlxsw_sp_span_entry_get(to
);
441 netdev_dbg(from
->dev
, "Adding inspected port to SPAN entry %d\n",
444 err
= mlxsw_sp_span_inspected_port_bind(from
, span_entry
, type
);
451 mlxsw_sp_span_entry_put(mlxsw_sp
, span_entry
);
455 static void mlxsw_sp_span_mirror_remove(struct mlxsw_sp_port
*from
,
456 struct mlxsw_sp_port
*to
,
457 enum mlxsw_sp_span_type type
)
459 struct mlxsw_sp_span_entry
*span_entry
;
461 span_entry
= mlxsw_sp_span_entry_find(to
);
463 netdev_err(from
->dev
, "no span entry found\n");
467 netdev_dbg(from
->dev
, "removing inspected port from SPAN entry %d\n",
469 mlxsw_sp_span_inspected_port_unbind(from
, span_entry
, type
);
472 static int mlxsw_sp_port_admin_status_set(struct mlxsw_sp_port
*mlxsw_sp_port
,
475 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
476 char paos_pl
[MLXSW_REG_PAOS_LEN
];
478 mlxsw_reg_paos_pack(paos_pl
, mlxsw_sp_port
->local_port
,
479 is_up
? MLXSW_PORT_ADMIN_STATUS_UP
:
480 MLXSW_PORT_ADMIN_STATUS_DOWN
);
481 return mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(paos
), paos_pl
);
484 static int mlxsw_sp_port_dev_addr_set(struct mlxsw_sp_port
*mlxsw_sp_port
,
487 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
488 char ppad_pl
[MLXSW_REG_PPAD_LEN
];
490 mlxsw_reg_ppad_pack(ppad_pl
, true, mlxsw_sp_port
->local_port
);
491 mlxsw_reg_ppad_mac_memcpy_to(ppad_pl
, addr
);
492 return mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(ppad
), ppad_pl
);
495 static int mlxsw_sp_port_dev_addr_init(struct mlxsw_sp_port
*mlxsw_sp_port
)
497 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
498 unsigned char *addr
= mlxsw_sp_port
->dev
->dev_addr
;
500 ether_addr_copy(addr
, mlxsw_sp
->base_mac
);
501 addr
[ETH_ALEN
- 1] += mlxsw_sp_port
->local_port
;
502 return mlxsw_sp_port_dev_addr_set(mlxsw_sp_port
, addr
);
505 static int mlxsw_sp_port_mtu_set(struct mlxsw_sp_port
*mlxsw_sp_port
, u16 mtu
)
507 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
508 char pmtu_pl
[MLXSW_REG_PMTU_LEN
];
512 mtu
+= MLXSW_TXHDR_LEN
+ ETH_HLEN
;
513 mlxsw_reg_pmtu_pack(pmtu_pl
, mlxsw_sp_port
->local_port
, 0);
514 err
= mlxsw_reg_query(mlxsw_sp
->core
, MLXSW_REG(pmtu
), pmtu_pl
);
517 max_mtu
= mlxsw_reg_pmtu_max_mtu_get(pmtu_pl
);
522 mlxsw_reg_pmtu_pack(pmtu_pl
, mlxsw_sp_port
->local_port
, mtu
);
523 return mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(pmtu
), pmtu_pl
);
526 static int __mlxsw_sp_port_swid_set(struct mlxsw_sp
*mlxsw_sp
, u8 local_port
,
529 char pspa_pl
[MLXSW_REG_PSPA_LEN
];
531 mlxsw_reg_pspa_pack(pspa_pl
, swid
, local_port
);
532 return mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(pspa
), pspa_pl
);
535 static int mlxsw_sp_port_swid_set(struct mlxsw_sp_port
*mlxsw_sp_port
, u8 swid
)
537 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
539 return __mlxsw_sp_port_swid_set(mlxsw_sp
, mlxsw_sp_port
->local_port
,
543 static int mlxsw_sp_port_vp_mode_set(struct mlxsw_sp_port
*mlxsw_sp_port
,
546 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
547 char svpe_pl
[MLXSW_REG_SVPE_LEN
];
549 mlxsw_reg_svpe_pack(svpe_pl
, mlxsw_sp_port
->local_port
, enable
);
550 return mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(svpe
), svpe_pl
);
553 int mlxsw_sp_port_vid_to_fid_set(struct mlxsw_sp_port
*mlxsw_sp_port
,
554 enum mlxsw_reg_svfa_mt mt
, bool valid
, u16 fid
,
557 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
558 char svfa_pl
[MLXSW_REG_SVFA_LEN
];
560 mlxsw_reg_svfa_pack(svfa_pl
, mlxsw_sp_port
->local_port
, mt
, valid
,
562 return mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(svfa
), svfa_pl
);
565 int __mlxsw_sp_port_vid_learning_set(struct mlxsw_sp_port
*mlxsw_sp_port
,
566 u16 vid_begin
, u16 vid_end
,
569 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
573 spvmlr_pl
= kmalloc(MLXSW_REG_SPVMLR_LEN
, GFP_KERNEL
);
576 mlxsw_reg_spvmlr_pack(spvmlr_pl
, mlxsw_sp_port
->local_port
, vid_begin
,
577 vid_end
, learn_enable
);
578 err
= mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(spvmlr
), spvmlr_pl
);
583 static int mlxsw_sp_port_vid_learning_set(struct mlxsw_sp_port
*mlxsw_sp_port
,
584 u16 vid
, bool learn_enable
)
586 return __mlxsw_sp_port_vid_learning_set(mlxsw_sp_port
, vid
, vid
,
591 mlxsw_sp_port_system_port_mapping_set(struct mlxsw_sp_port
*mlxsw_sp_port
)
593 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
594 char sspr_pl
[MLXSW_REG_SSPR_LEN
];
596 mlxsw_reg_sspr_pack(sspr_pl
, mlxsw_sp_port
->local_port
);
597 return mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(sspr
), sspr_pl
);
600 static int mlxsw_sp_port_module_info_get(struct mlxsw_sp
*mlxsw_sp
,
601 u8 local_port
, u8
*p_module
,
602 u8
*p_width
, u8
*p_lane
)
604 char pmlp_pl
[MLXSW_REG_PMLP_LEN
];
607 mlxsw_reg_pmlp_pack(pmlp_pl
, local_port
);
608 err
= mlxsw_reg_query(mlxsw_sp
->core
, MLXSW_REG(pmlp
), pmlp_pl
);
611 *p_module
= mlxsw_reg_pmlp_module_get(pmlp_pl
, 0);
612 *p_width
= mlxsw_reg_pmlp_width_get(pmlp_pl
);
613 *p_lane
= mlxsw_reg_pmlp_tx_lane_get(pmlp_pl
, 0);
617 static int mlxsw_sp_port_module_map(struct mlxsw_sp
*mlxsw_sp
, u8 local_port
,
618 u8 module
, u8 width
, u8 lane
)
620 char pmlp_pl
[MLXSW_REG_PMLP_LEN
];
623 mlxsw_reg_pmlp_pack(pmlp_pl
, local_port
);
624 mlxsw_reg_pmlp_width_set(pmlp_pl
, width
);
625 for (i
= 0; i
< width
; i
++) {
626 mlxsw_reg_pmlp_module_set(pmlp_pl
, i
, module
);
627 mlxsw_reg_pmlp_tx_lane_set(pmlp_pl
, i
, lane
+ i
); /* Rx & Tx */
630 return mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(pmlp
), pmlp_pl
);
633 static int mlxsw_sp_port_module_unmap(struct mlxsw_sp
*mlxsw_sp
, u8 local_port
)
635 char pmlp_pl
[MLXSW_REG_PMLP_LEN
];
637 mlxsw_reg_pmlp_pack(pmlp_pl
, local_port
);
638 mlxsw_reg_pmlp_width_set(pmlp_pl
, 0);
639 return mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(pmlp
), pmlp_pl
);
642 static int mlxsw_sp_port_open(struct net_device
*dev
)
644 struct mlxsw_sp_port
*mlxsw_sp_port
= netdev_priv(dev
);
647 err
= mlxsw_sp_port_admin_status_set(mlxsw_sp_port
, true);
650 netif_start_queue(dev
);
654 static int mlxsw_sp_port_stop(struct net_device
*dev
)
656 struct mlxsw_sp_port
*mlxsw_sp_port
= netdev_priv(dev
);
658 netif_stop_queue(dev
);
659 return mlxsw_sp_port_admin_status_set(mlxsw_sp_port
, false);
662 static netdev_tx_t
mlxsw_sp_port_xmit(struct sk_buff
*skb
,
663 struct net_device
*dev
)
665 struct mlxsw_sp_port
*mlxsw_sp_port
= netdev_priv(dev
);
666 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
667 struct mlxsw_sp_port_pcpu_stats
*pcpu_stats
;
668 const struct mlxsw_tx_info tx_info
= {
669 .local_port
= mlxsw_sp_port
->local_port
,
675 if (mlxsw_core_skb_transmit_busy(mlxsw_sp
->core
, &tx_info
))
676 return NETDEV_TX_BUSY
;
678 if (unlikely(skb_headroom(skb
) < MLXSW_TXHDR_LEN
)) {
679 struct sk_buff
*skb_orig
= skb
;
681 skb
= skb_realloc_headroom(skb
, MLXSW_TXHDR_LEN
);
683 this_cpu_inc(mlxsw_sp_port
->pcpu_stats
->tx_dropped
);
684 dev_kfree_skb_any(skb_orig
);
687 dev_consume_skb_any(skb_orig
);
690 if (eth_skb_pad(skb
)) {
691 this_cpu_inc(mlxsw_sp_port
->pcpu_stats
->tx_dropped
);
695 mlxsw_sp_txhdr_construct(skb
, &tx_info
);
696 /* TX header is consumed by HW on the way so we shouldn't count its
697 * bytes as being sent.
699 len
= skb
->len
- MLXSW_TXHDR_LEN
;
701 /* Due to a race we might fail here because of a full queue. In that
702 * unlikely case we simply drop the packet.
704 err
= mlxsw_core_skb_transmit(mlxsw_sp
->core
, skb
, &tx_info
);
707 pcpu_stats
= this_cpu_ptr(mlxsw_sp_port
->pcpu_stats
);
708 u64_stats_update_begin(&pcpu_stats
->syncp
);
709 pcpu_stats
->tx_packets
++;
710 pcpu_stats
->tx_bytes
+= len
;
711 u64_stats_update_end(&pcpu_stats
->syncp
);
713 this_cpu_inc(mlxsw_sp_port
->pcpu_stats
->tx_dropped
);
714 dev_kfree_skb_any(skb
);
719 static void mlxsw_sp_set_rx_mode(struct net_device
*dev
)
723 static int mlxsw_sp_port_set_mac_address(struct net_device
*dev
, void *p
)
725 struct mlxsw_sp_port
*mlxsw_sp_port
= netdev_priv(dev
);
726 struct sockaddr
*addr
= p
;
729 if (!is_valid_ether_addr(addr
->sa_data
))
730 return -EADDRNOTAVAIL
;
732 err
= mlxsw_sp_port_dev_addr_set(mlxsw_sp_port
, addr
->sa_data
);
735 memcpy(dev
->dev_addr
, addr
->sa_data
, dev
->addr_len
);
739 static void mlxsw_sp_pg_buf_pack(char *pbmc_pl
, int pg_index
, int mtu
,
740 bool pause_en
, bool pfc_en
, u16 delay
)
742 u16 pg_size
= 2 * MLXSW_SP_BYTES_TO_CELLS(mtu
);
744 delay
= pfc_en
? mlxsw_sp_pfc_delay_get(mtu
, delay
) :
745 MLXSW_SP_PAUSE_DELAY
;
747 if (pause_en
|| pfc_en
)
748 mlxsw_reg_pbmc_lossless_buffer_pack(pbmc_pl
, pg_index
,
749 pg_size
+ delay
, pg_size
);
751 mlxsw_reg_pbmc_lossy_buffer_pack(pbmc_pl
, pg_index
, pg_size
);
754 int __mlxsw_sp_port_headroom_set(struct mlxsw_sp_port
*mlxsw_sp_port
, int mtu
,
755 u8
*prio_tc
, bool pause_en
,
756 struct ieee_pfc
*my_pfc
)
758 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
759 u8 pfc_en
= !!my_pfc
? my_pfc
->pfc_en
: 0;
760 u16 delay
= !!my_pfc
? my_pfc
->delay
: 0;
761 char pbmc_pl
[MLXSW_REG_PBMC_LEN
];
764 mlxsw_reg_pbmc_pack(pbmc_pl
, mlxsw_sp_port
->local_port
, 0, 0);
765 err
= mlxsw_reg_query(mlxsw_sp
->core
, MLXSW_REG(pbmc
), pbmc_pl
);
769 for (i
= 0; i
< IEEE_8021QAZ_MAX_TCS
; i
++) {
770 bool configure
= false;
773 for (j
= 0; j
< IEEE_8021QAZ_MAX_TCS
; j
++) {
774 if (prio_tc
[j
] == i
) {
775 pfc
= pfc_en
& BIT(j
);
783 mlxsw_sp_pg_buf_pack(pbmc_pl
, i
, mtu
, pause_en
, pfc
, delay
);
786 return mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(pbmc
), pbmc_pl
);
789 static int mlxsw_sp_port_headroom_set(struct mlxsw_sp_port
*mlxsw_sp_port
,
790 int mtu
, bool pause_en
)
792 u8 def_prio_tc
[IEEE_8021QAZ_MAX_TCS
] = {0};
793 bool dcb_en
= !!mlxsw_sp_port
->dcb
.ets
;
794 struct ieee_pfc
*my_pfc
;
797 prio_tc
= dcb_en
? mlxsw_sp_port
->dcb
.ets
->prio_tc
: def_prio_tc
;
798 my_pfc
= dcb_en
? mlxsw_sp_port
->dcb
.pfc
: NULL
;
800 return __mlxsw_sp_port_headroom_set(mlxsw_sp_port
, mtu
, prio_tc
,
804 static int mlxsw_sp_port_change_mtu(struct net_device
*dev
, int mtu
)
806 struct mlxsw_sp_port
*mlxsw_sp_port
= netdev_priv(dev
);
807 bool pause_en
= mlxsw_sp_port_is_pause_en(mlxsw_sp_port
);
810 err
= mlxsw_sp_port_headroom_set(mlxsw_sp_port
, mtu
, pause_en
);
813 err
= mlxsw_sp_span_port_mtu_update(mlxsw_sp_port
, mtu
);
815 goto err_span_port_mtu_update
;
816 err
= mlxsw_sp_port_mtu_set(mlxsw_sp_port
, mtu
);
818 goto err_port_mtu_set
;
823 mlxsw_sp_span_port_mtu_update(mlxsw_sp_port
, dev
->mtu
);
824 err_span_port_mtu_update
:
825 mlxsw_sp_port_headroom_set(mlxsw_sp_port
, dev
->mtu
, pause_en
);
830 mlxsw_sp_port_get_sw_stats64(const struct net_device
*dev
,
831 struct rtnl_link_stats64
*stats
)
833 struct mlxsw_sp_port
*mlxsw_sp_port
= netdev_priv(dev
);
834 struct mlxsw_sp_port_pcpu_stats
*p
;
835 u64 rx_packets
, rx_bytes
, tx_packets
, tx_bytes
;
840 for_each_possible_cpu(i
) {
841 p
= per_cpu_ptr(mlxsw_sp_port
->pcpu_stats
, i
);
843 start
= u64_stats_fetch_begin_irq(&p
->syncp
);
844 rx_packets
= p
->rx_packets
;
845 rx_bytes
= p
->rx_bytes
;
846 tx_packets
= p
->tx_packets
;
847 tx_bytes
= p
->tx_bytes
;
848 } while (u64_stats_fetch_retry_irq(&p
->syncp
, start
));
850 stats
->rx_packets
+= rx_packets
;
851 stats
->rx_bytes
+= rx_bytes
;
852 stats
->tx_packets
+= tx_packets
;
853 stats
->tx_bytes
+= tx_bytes
;
854 /* tx_dropped is u32, updated without syncp protection. */
855 tx_dropped
+= p
->tx_dropped
;
857 stats
->tx_dropped
= tx_dropped
;
861 static bool mlxsw_sp_port_has_offload_stats(const struct net_device
*dev
, int attr_id
)
864 case IFLA_OFFLOAD_XSTATS_CPU_HIT
:
871 static int mlxsw_sp_port_get_offload_stats(int attr_id
, const struct net_device
*dev
,
875 case IFLA_OFFLOAD_XSTATS_CPU_HIT
:
876 return mlxsw_sp_port_get_sw_stats64(dev
, sp
);
882 static int mlxsw_sp_port_get_stats_raw(struct net_device
*dev
, int grp
,
883 int prio
, char *ppcnt_pl
)
885 struct mlxsw_sp_port
*mlxsw_sp_port
= netdev_priv(dev
);
886 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
888 mlxsw_reg_ppcnt_pack(ppcnt_pl
, mlxsw_sp_port
->local_port
, grp
, prio
);
889 return mlxsw_reg_query(mlxsw_sp
->core
, MLXSW_REG(ppcnt
), ppcnt_pl
);
892 static int mlxsw_sp_port_get_hw_stats(struct net_device
*dev
,
893 struct rtnl_link_stats64
*stats
)
895 char ppcnt_pl
[MLXSW_REG_PPCNT_LEN
];
898 err
= mlxsw_sp_port_get_stats_raw(dev
, MLXSW_REG_PPCNT_IEEE_8023_CNT
,
904 mlxsw_reg_ppcnt_a_frames_transmitted_ok_get(ppcnt_pl
);
906 mlxsw_reg_ppcnt_a_frames_received_ok_get(ppcnt_pl
);
908 mlxsw_reg_ppcnt_a_octets_transmitted_ok_get(ppcnt_pl
);
910 mlxsw_reg_ppcnt_a_octets_received_ok_get(ppcnt_pl
);
912 mlxsw_reg_ppcnt_a_multicast_frames_received_ok_get(ppcnt_pl
);
914 stats
->rx_crc_errors
=
915 mlxsw_reg_ppcnt_a_frame_check_sequence_errors_get(ppcnt_pl
);
916 stats
->rx_frame_errors
=
917 mlxsw_reg_ppcnt_a_alignment_errors_get(ppcnt_pl
);
919 stats
->rx_length_errors
= (
920 mlxsw_reg_ppcnt_a_in_range_length_errors_get(ppcnt_pl
) +
921 mlxsw_reg_ppcnt_a_out_of_range_length_field_get(ppcnt_pl
) +
922 mlxsw_reg_ppcnt_a_frame_too_long_errors_get(ppcnt_pl
));
924 stats
->rx_errors
= (stats
->rx_crc_errors
+
925 stats
->rx_frame_errors
+ stats
->rx_length_errors
);
931 static void update_stats_cache(struct work_struct
*work
)
933 struct mlxsw_sp_port
*mlxsw_sp_port
=
934 container_of(work
, struct mlxsw_sp_port
,
935 hw_stats
.update_dw
.work
);
937 if (!netif_carrier_ok(mlxsw_sp_port
->dev
))
940 mlxsw_sp_port_get_hw_stats(mlxsw_sp_port
->dev
,
941 mlxsw_sp_port
->hw_stats
.cache
);
944 mlxsw_core_schedule_dw(&mlxsw_sp_port
->hw_stats
.update_dw
,
945 MLXSW_HW_STATS_UPDATE_TIME
);
948 /* Return the stats from a cache that is updated periodically,
949 * as this function might get called in an atomic context.
951 static struct rtnl_link_stats64
*
952 mlxsw_sp_port_get_stats64(struct net_device
*dev
,
953 struct rtnl_link_stats64
*stats
)
955 struct mlxsw_sp_port
*mlxsw_sp_port
= netdev_priv(dev
);
957 memcpy(stats
, mlxsw_sp_port
->hw_stats
.cache
, sizeof(*stats
));
962 int mlxsw_sp_port_vlan_set(struct mlxsw_sp_port
*mlxsw_sp_port
, u16 vid_begin
,
963 u16 vid_end
, bool is_member
, bool untagged
)
965 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
969 spvm_pl
= kmalloc(MLXSW_REG_SPVM_LEN
, GFP_KERNEL
);
973 mlxsw_reg_spvm_pack(spvm_pl
, mlxsw_sp_port
->local_port
, vid_begin
,
974 vid_end
, is_member
, untagged
);
975 err
= mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(spvm
), spvm_pl
);
980 static int mlxsw_sp_port_vp_mode_trans(struct mlxsw_sp_port
*mlxsw_sp_port
)
982 enum mlxsw_reg_svfa_mt mt
= MLXSW_REG_SVFA_MT_PORT_VID_TO_FID
;
983 u16 vid
, last_visited_vid
;
986 for_each_set_bit(vid
, mlxsw_sp_port
->active_vlans
, VLAN_N_VID
) {
987 err
= mlxsw_sp_port_vid_to_fid_set(mlxsw_sp_port
, mt
, true, vid
,
990 last_visited_vid
= vid
;
991 goto err_port_vid_to_fid_set
;
995 err
= mlxsw_sp_port_vp_mode_set(mlxsw_sp_port
, true);
997 last_visited_vid
= VLAN_N_VID
;
998 goto err_port_vid_to_fid_set
;
1003 err_port_vid_to_fid_set
:
1004 for_each_set_bit(vid
, mlxsw_sp_port
->active_vlans
, last_visited_vid
)
1005 mlxsw_sp_port_vid_to_fid_set(mlxsw_sp_port
, mt
, false, vid
,
1010 static int mlxsw_sp_port_vlan_mode_trans(struct mlxsw_sp_port
*mlxsw_sp_port
)
1012 enum mlxsw_reg_svfa_mt mt
= MLXSW_REG_SVFA_MT_PORT_VID_TO_FID
;
1016 err
= mlxsw_sp_port_vp_mode_set(mlxsw_sp_port
, false);
1020 for_each_set_bit(vid
, mlxsw_sp_port
->active_vlans
, VLAN_N_VID
) {
1021 err
= mlxsw_sp_port_vid_to_fid_set(mlxsw_sp_port
, mt
, false,
1030 static struct mlxsw_sp_port
*
1031 mlxsw_sp_port_vport_create(struct mlxsw_sp_port
*mlxsw_sp_port
, u16 vid
)
1033 struct mlxsw_sp_port
*mlxsw_sp_vport
;
1035 mlxsw_sp_vport
= kzalloc(sizeof(*mlxsw_sp_vport
), GFP_KERNEL
);
1036 if (!mlxsw_sp_vport
)
1039 /* dev will be set correctly after the VLAN device is linked
1040 * with the real device. In case of bridge SELF invocation, dev
1041 * will remain as is.
1043 mlxsw_sp_vport
->dev
= mlxsw_sp_port
->dev
;
1044 mlxsw_sp_vport
->mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
1045 mlxsw_sp_vport
->local_port
= mlxsw_sp_port
->local_port
;
1046 mlxsw_sp_vport
->stp_state
= BR_STATE_FORWARDING
;
1047 mlxsw_sp_vport
->lagged
= mlxsw_sp_port
->lagged
;
1048 mlxsw_sp_vport
->lag_id
= mlxsw_sp_port
->lag_id
;
1049 mlxsw_sp_vport
->vport
.vid
= vid
;
1051 list_add(&mlxsw_sp_vport
->vport
.list
, &mlxsw_sp_port
->vports_list
);
1053 return mlxsw_sp_vport
;
1056 static void mlxsw_sp_port_vport_destroy(struct mlxsw_sp_port
*mlxsw_sp_vport
)
1058 list_del(&mlxsw_sp_vport
->vport
.list
);
1059 kfree(mlxsw_sp_vport
);
1062 static int mlxsw_sp_port_add_vid(struct net_device
*dev
,
1063 __be16 __always_unused proto
, u16 vid
)
1065 struct mlxsw_sp_port
*mlxsw_sp_port
= netdev_priv(dev
);
1066 struct mlxsw_sp_port
*mlxsw_sp_vport
;
1067 bool untagged
= vid
== 1;
1070 /* VLAN 0 is added to HW filter when device goes up, but it is
1071 * reserved in our case, so simply return.
1076 if (mlxsw_sp_port_vport_find(mlxsw_sp_port
, vid
))
1079 mlxsw_sp_vport
= mlxsw_sp_port_vport_create(mlxsw_sp_port
, vid
);
1080 if (!mlxsw_sp_vport
)
1083 /* When adding the first VLAN interface on a bridged port we need to
1084 * transition all the active 802.1Q bridge VLANs to use explicit
1085 * {Port, VID} to FID mappings and set the port's mode to Virtual mode.
1087 if (list_is_singular(&mlxsw_sp_port
->vports_list
)) {
1088 err
= mlxsw_sp_port_vp_mode_trans(mlxsw_sp_port
);
1090 goto err_port_vp_mode_trans
;
1093 err
= mlxsw_sp_port_vlan_set(mlxsw_sp_vport
, vid
, vid
, true, untagged
);
1095 goto err_port_add_vid
;
1100 if (list_is_singular(&mlxsw_sp_port
->vports_list
))
1101 mlxsw_sp_port_vlan_mode_trans(mlxsw_sp_port
);
1102 err_port_vp_mode_trans
:
1103 mlxsw_sp_port_vport_destroy(mlxsw_sp_vport
);
1107 static int mlxsw_sp_port_kill_vid(struct net_device
*dev
,
1108 __be16 __always_unused proto
, u16 vid
)
1110 struct mlxsw_sp_port
*mlxsw_sp_port
= netdev_priv(dev
);
1111 struct mlxsw_sp_port
*mlxsw_sp_vport
;
1112 struct mlxsw_sp_fid
*f
;
1114 /* VLAN 0 is removed from HW filter when device goes down, but
1115 * it is reserved in our case, so simply return.
1120 mlxsw_sp_vport
= mlxsw_sp_port_vport_find(mlxsw_sp_port
, vid
);
1121 if (WARN_ON(!mlxsw_sp_vport
))
1124 mlxsw_sp_port_vlan_set(mlxsw_sp_vport
, vid
, vid
, false, false);
1126 /* Drop FID reference. If this was the last reference the
1127 * resources will be freed.
1129 f
= mlxsw_sp_vport_fid_get(mlxsw_sp_vport
);
1130 if (f
&& !WARN_ON(!f
->leave
))
1131 f
->leave(mlxsw_sp_vport
);
1133 /* When removing the last VLAN interface on a bridged port we need to
1134 * transition all active 802.1Q bridge VLANs to use VID to FID
1135 * mappings and set port's mode to VLAN mode.
1137 if (list_is_singular(&mlxsw_sp_port
->vports_list
))
1138 mlxsw_sp_port_vlan_mode_trans(mlxsw_sp_port
);
1140 mlxsw_sp_port_vport_destroy(mlxsw_sp_vport
);
1145 static int mlxsw_sp_port_get_phys_port_name(struct net_device
*dev
, char *name
,
1148 struct mlxsw_sp_port
*mlxsw_sp_port
= netdev_priv(dev
);
1149 u8 module
= mlxsw_sp_port
->mapping
.module
;
1150 u8 width
= mlxsw_sp_port
->mapping
.width
;
1151 u8 lane
= mlxsw_sp_port
->mapping
.lane
;
1154 if (!mlxsw_sp_port
->split
)
1155 err
= snprintf(name
, len
, "p%d", module
+ 1);
1157 err
= snprintf(name
, len
, "p%ds%d", module
+ 1,
1166 static struct mlxsw_sp_port_mall_tc_entry
*
1167 mlxsw_sp_port_mirror_entry_find(struct mlxsw_sp_port
*port
,
1168 unsigned long cookie
) {
1169 struct mlxsw_sp_port_mall_tc_entry
*mall_tc_entry
;
1171 list_for_each_entry(mall_tc_entry
, &port
->mall_tc_list
, list
)
1172 if (mall_tc_entry
->cookie
== cookie
)
1173 return mall_tc_entry
;
1179 mlxsw_sp_port_add_cls_matchall_mirror(struct mlxsw_sp_port
*mlxsw_sp_port
,
1180 struct tc_cls_matchall_offload
*cls
,
1181 const struct tc_action
*a
,
1184 struct mlxsw_sp_port_mall_tc_entry
*mall_tc_entry
;
1185 struct net
*net
= dev_net(mlxsw_sp_port
->dev
);
1186 enum mlxsw_sp_span_type span_type
;
1187 struct mlxsw_sp_port
*to_port
;
1188 struct net_device
*to_dev
;
1192 ifindex
= tcf_mirred_ifindex(a
);
1193 to_dev
= __dev_get_by_index(net
, ifindex
);
1195 netdev_err(mlxsw_sp_port
->dev
, "Could not find requested device\n");
1199 if (!mlxsw_sp_port_dev_check(to_dev
)) {
1200 netdev_err(mlxsw_sp_port
->dev
, "Cannot mirror to a non-spectrum port");
1203 to_port
= netdev_priv(to_dev
);
1205 mall_tc_entry
= kzalloc(sizeof(*mall_tc_entry
), GFP_KERNEL
);
1209 mall_tc_entry
->cookie
= cls
->cookie
;
1210 mall_tc_entry
->type
= MLXSW_SP_PORT_MALL_MIRROR
;
1211 mall_tc_entry
->mirror
.to_local_port
= to_port
->local_port
;
1212 mall_tc_entry
->mirror
.ingress
= ingress
;
1213 list_add_tail(&mall_tc_entry
->list
, &mlxsw_sp_port
->mall_tc_list
);
1215 span_type
= ingress
? MLXSW_SP_SPAN_INGRESS
: MLXSW_SP_SPAN_EGRESS
;
1216 err
= mlxsw_sp_span_mirror_add(mlxsw_sp_port
, to_port
, span_type
);
1218 goto err_mirror_add
;
1222 list_del(&mall_tc_entry
->list
);
1223 kfree(mall_tc_entry
);
1227 static int mlxsw_sp_port_add_cls_matchall(struct mlxsw_sp_port
*mlxsw_sp_port
,
1229 struct tc_cls_matchall_offload
*cls
,
1232 const struct tc_action
*a
;
1236 if (!tc_single_action(cls
->exts
)) {
1237 netdev_err(mlxsw_sp_port
->dev
, "only singular actions are supported\n");
1241 tcf_exts_to_list(cls
->exts
, &actions
);
1242 list_for_each_entry(a
, &actions
, list
) {
1243 if (!is_tcf_mirred_egress_mirror(a
) ||
1244 protocol
!= htons(ETH_P_ALL
)) {
1248 err
= mlxsw_sp_port_add_cls_matchall_mirror(mlxsw_sp_port
, cls
,
1257 static void mlxsw_sp_port_del_cls_matchall(struct mlxsw_sp_port
*mlxsw_sp_port
,
1258 struct tc_cls_matchall_offload
*cls
)
1260 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
1261 struct mlxsw_sp_port_mall_tc_entry
*mall_tc_entry
;
1262 enum mlxsw_sp_span_type span_type
;
1263 struct mlxsw_sp_port
*to_port
;
1265 mall_tc_entry
= mlxsw_sp_port_mirror_entry_find(mlxsw_sp_port
,
1267 if (!mall_tc_entry
) {
1268 netdev_dbg(mlxsw_sp_port
->dev
, "tc entry not found on port\n");
1272 switch (mall_tc_entry
->type
) {
1273 case MLXSW_SP_PORT_MALL_MIRROR
:
1274 to_port
= mlxsw_sp
->ports
[mall_tc_entry
->mirror
.to_local_port
];
1275 span_type
= mall_tc_entry
->mirror
.ingress
?
1276 MLXSW_SP_SPAN_INGRESS
: MLXSW_SP_SPAN_EGRESS
;
1278 mlxsw_sp_span_mirror_remove(mlxsw_sp_port
, to_port
, span_type
);
1284 list_del(&mall_tc_entry
->list
);
1285 kfree(mall_tc_entry
);
1288 static int mlxsw_sp_setup_tc(struct net_device
*dev
, u32 handle
,
1289 __be16 proto
, struct tc_to_netdev
*tc
)
1291 struct mlxsw_sp_port
*mlxsw_sp_port
= netdev_priv(dev
);
1292 bool ingress
= TC_H_MAJ(handle
) == TC_H_MAJ(TC_H_INGRESS
);
1294 if (tc
->type
== TC_SETUP_MATCHALL
) {
1295 switch (tc
->cls_mall
->command
) {
1296 case TC_CLSMATCHALL_REPLACE
:
1297 return mlxsw_sp_port_add_cls_matchall(mlxsw_sp_port
,
1301 case TC_CLSMATCHALL_DESTROY
:
1302 mlxsw_sp_port_del_cls_matchall(mlxsw_sp_port
,
1313 static const struct net_device_ops mlxsw_sp_port_netdev_ops
= {
1314 .ndo_open
= mlxsw_sp_port_open
,
1315 .ndo_stop
= mlxsw_sp_port_stop
,
1316 .ndo_start_xmit
= mlxsw_sp_port_xmit
,
1317 .ndo_setup_tc
= mlxsw_sp_setup_tc
,
1318 .ndo_set_rx_mode
= mlxsw_sp_set_rx_mode
,
1319 .ndo_set_mac_address
= mlxsw_sp_port_set_mac_address
,
1320 .ndo_change_mtu
= mlxsw_sp_port_change_mtu
,
1321 .ndo_get_stats64
= mlxsw_sp_port_get_stats64
,
1322 .ndo_has_offload_stats
= mlxsw_sp_port_has_offload_stats
,
1323 .ndo_get_offload_stats
= mlxsw_sp_port_get_offload_stats
,
1324 .ndo_vlan_rx_add_vid
= mlxsw_sp_port_add_vid
,
1325 .ndo_vlan_rx_kill_vid
= mlxsw_sp_port_kill_vid
,
1326 .ndo_neigh_construct
= mlxsw_sp_router_neigh_construct
,
1327 .ndo_neigh_destroy
= mlxsw_sp_router_neigh_destroy
,
1328 .ndo_fdb_add
= switchdev_port_fdb_add
,
1329 .ndo_fdb_del
= switchdev_port_fdb_del
,
1330 .ndo_fdb_dump
= switchdev_port_fdb_dump
,
1331 .ndo_bridge_setlink
= switchdev_port_bridge_setlink
,
1332 .ndo_bridge_getlink
= switchdev_port_bridge_getlink
,
1333 .ndo_bridge_dellink
= switchdev_port_bridge_dellink
,
1334 .ndo_get_phys_port_name
= mlxsw_sp_port_get_phys_port_name
,
1337 static void mlxsw_sp_port_get_drvinfo(struct net_device
*dev
,
1338 struct ethtool_drvinfo
*drvinfo
)
1340 struct mlxsw_sp_port
*mlxsw_sp_port
= netdev_priv(dev
);
1341 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
1343 strlcpy(drvinfo
->driver
, mlxsw_sp_driver_name
, sizeof(drvinfo
->driver
));
1344 strlcpy(drvinfo
->version
, mlxsw_sp_driver_version
,
1345 sizeof(drvinfo
->version
));
1346 snprintf(drvinfo
->fw_version
, sizeof(drvinfo
->fw_version
),
1348 mlxsw_sp
->bus_info
->fw_rev
.major
,
1349 mlxsw_sp
->bus_info
->fw_rev
.minor
,
1350 mlxsw_sp
->bus_info
->fw_rev
.subminor
);
1351 strlcpy(drvinfo
->bus_info
, mlxsw_sp
->bus_info
->device_name
,
1352 sizeof(drvinfo
->bus_info
));
1355 static void mlxsw_sp_port_get_pauseparam(struct net_device
*dev
,
1356 struct ethtool_pauseparam
*pause
)
1358 struct mlxsw_sp_port
*mlxsw_sp_port
= netdev_priv(dev
);
1360 pause
->rx_pause
= mlxsw_sp_port
->link
.rx_pause
;
1361 pause
->tx_pause
= mlxsw_sp_port
->link
.tx_pause
;
1364 static int mlxsw_sp_port_pause_set(struct mlxsw_sp_port
*mlxsw_sp_port
,
1365 struct ethtool_pauseparam
*pause
)
1367 char pfcc_pl
[MLXSW_REG_PFCC_LEN
];
1369 mlxsw_reg_pfcc_pack(pfcc_pl
, mlxsw_sp_port
->local_port
);
1370 mlxsw_reg_pfcc_pprx_set(pfcc_pl
, pause
->rx_pause
);
1371 mlxsw_reg_pfcc_pptx_set(pfcc_pl
, pause
->tx_pause
);
1373 return mlxsw_reg_write(mlxsw_sp_port
->mlxsw_sp
->core
, MLXSW_REG(pfcc
),
1377 static int mlxsw_sp_port_set_pauseparam(struct net_device
*dev
,
1378 struct ethtool_pauseparam
*pause
)
1380 struct mlxsw_sp_port
*mlxsw_sp_port
= netdev_priv(dev
);
1381 bool pause_en
= pause
->tx_pause
|| pause
->rx_pause
;
1384 if (mlxsw_sp_port
->dcb
.pfc
&& mlxsw_sp_port
->dcb
.pfc
->pfc_en
) {
1385 netdev_err(dev
, "PFC already enabled on port\n");
1389 if (pause
->autoneg
) {
1390 netdev_err(dev
, "PAUSE frames autonegotiation isn't supported\n");
1394 err
= mlxsw_sp_port_headroom_set(mlxsw_sp_port
, dev
->mtu
, pause_en
);
1396 netdev_err(dev
, "Failed to configure port's headroom\n");
1400 err
= mlxsw_sp_port_pause_set(mlxsw_sp_port
, pause
);
1402 netdev_err(dev
, "Failed to set PAUSE parameters\n");
1403 goto err_port_pause_configure
;
1406 mlxsw_sp_port
->link
.rx_pause
= pause
->rx_pause
;
1407 mlxsw_sp_port
->link
.tx_pause
= pause
->tx_pause
;
1411 err_port_pause_configure
:
1412 pause_en
= mlxsw_sp_port_is_pause_en(mlxsw_sp_port
);
1413 mlxsw_sp_port_headroom_set(mlxsw_sp_port
, dev
->mtu
, pause_en
);
1417 struct mlxsw_sp_port_hw_stats
{
1418 char str
[ETH_GSTRING_LEN
];
1419 u64 (*getter
)(const char *payload
);
1422 static struct mlxsw_sp_port_hw_stats mlxsw_sp_port_hw_stats
[] = {
1424 .str
= "a_frames_transmitted_ok",
1425 .getter
= mlxsw_reg_ppcnt_a_frames_transmitted_ok_get
,
1428 .str
= "a_frames_received_ok",
1429 .getter
= mlxsw_reg_ppcnt_a_frames_received_ok_get
,
1432 .str
= "a_frame_check_sequence_errors",
1433 .getter
= mlxsw_reg_ppcnt_a_frame_check_sequence_errors_get
,
1436 .str
= "a_alignment_errors",
1437 .getter
= mlxsw_reg_ppcnt_a_alignment_errors_get
,
1440 .str
= "a_octets_transmitted_ok",
1441 .getter
= mlxsw_reg_ppcnt_a_octets_transmitted_ok_get
,
1444 .str
= "a_octets_received_ok",
1445 .getter
= mlxsw_reg_ppcnt_a_octets_received_ok_get
,
1448 .str
= "a_multicast_frames_xmitted_ok",
1449 .getter
= mlxsw_reg_ppcnt_a_multicast_frames_xmitted_ok_get
,
1452 .str
= "a_broadcast_frames_xmitted_ok",
1453 .getter
= mlxsw_reg_ppcnt_a_broadcast_frames_xmitted_ok_get
,
1456 .str
= "a_multicast_frames_received_ok",
1457 .getter
= mlxsw_reg_ppcnt_a_multicast_frames_received_ok_get
,
1460 .str
= "a_broadcast_frames_received_ok",
1461 .getter
= mlxsw_reg_ppcnt_a_broadcast_frames_received_ok_get
,
1464 .str
= "a_in_range_length_errors",
1465 .getter
= mlxsw_reg_ppcnt_a_in_range_length_errors_get
,
1468 .str
= "a_out_of_range_length_field",
1469 .getter
= mlxsw_reg_ppcnt_a_out_of_range_length_field_get
,
1472 .str
= "a_frame_too_long_errors",
1473 .getter
= mlxsw_reg_ppcnt_a_frame_too_long_errors_get
,
1476 .str
= "a_symbol_error_during_carrier",
1477 .getter
= mlxsw_reg_ppcnt_a_symbol_error_during_carrier_get
,
1480 .str
= "a_mac_control_frames_transmitted",
1481 .getter
= mlxsw_reg_ppcnt_a_mac_control_frames_transmitted_get
,
1484 .str
= "a_mac_control_frames_received",
1485 .getter
= mlxsw_reg_ppcnt_a_mac_control_frames_received_get
,
1488 .str
= "a_unsupported_opcodes_received",
1489 .getter
= mlxsw_reg_ppcnt_a_unsupported_opcodes_received_get
,
1492 .str
= "a_pause_mac_ctrl_frames_received",
1493 .getter
= mlxsw_reg_ppcnt_a_pause_mac_ctrl_frames_received_get
,
1496 .str
= "a_pause_mac_ctrl_frames_xmitted",
1497 .getter
= mlxsw_reg_ppcnt_a_pause_mac_ctrl_frames_transmitted_get
,
1501 #define MLXSW_SP_PORT_HW_STATS_LEN ARRAY_SIZE(mlxsw_sp_port_hw_stats)
1503 static struct mlxsw_sp_port_hw_stats mlxsw_sp_port_hw_prio_stats
[] = {
1505 .str
= "rx_octets_prio",
1506 .getter
= mlxsw_reg_ppcnt_rx_octets_get
,
1509 .str
= "rx_frames_prio",
1510 .getter
= mlxsw_reg_ppcnt_rx_frames_get
,
1513 .str
= "tx_octets_prio",
1514 .getter
= mlxsw_reg_ppcnt_tx_octets_get
,
1517 .str
= "tx_frames_prio",
1518 .getter
= mlxsw_reg_ppcnt_tx_frames_get
,
1521 .str
= "rx_pause_prio",
1522 .getter
= mlxsw_reg_ppcnt_rx_pause_get
,
1525 .str
= "rx_pause_duration_prio",
1526 .getter
= mlxsw_reg_ppcnt_rx_pause_duration_get
,
1529 .str
= "tx_pause_prio",
1530 .getter
= mlxsw_reg_ppcnt_tx_pause_get
,
1533 .str
= "tx_pause_duration_prio",
1534 .getter
= mlxsw_reg_ppcnt_tx_pause_duration_get
,
1538 #define MLXSW_SP_PORT_HW_PRIO_STATS_LEN ARRAY_SIZE(mlxsw_sp_port_hw_prio_stats)
1540 static u64
mlxsw_reg_ppcnt_tc_transmit_queue_bytes_get(const char *ppcnt_pl
)
1542 u64 transmit_queue
= mlxsw_reg_ppcnt_tc_transmit_queue_get(ppcnt_pl
);
1544 return MLXSW_SP_CELLS_TO_BYTES(transmit_queue
);
1547 static struct mlxsw_sp_port_hw_stats mlxsw_sp_port_hw_tc_stats
[] = {
1549 .str
= "tc_transmit_queue_tc",
1550 .getter
= mlxsw_reg_ppcnt_tc_transmit_queue_bytes_get
,
1553 .str
= "tc_no_buffer_discard_uc_tc",
1554 .getter
= mlxsw_reg_ppcnt_tc_no_buffer_discard_uc_get
,
1558 #define MLXSW_SP_PORT_HW_TC_STATS_LEN ARRAY_SIZE(mlxsw_sp_port_hw_tc_stats)
1560 #define MLXSW_SP_PORT_ETHTOOL_STATS_LEN (MLXSW_SP_PORT_HW_STATS_LEN + \
1561 (MLXSW_SP_PORT_HW_PRIO_STATS_LEN + \
1562 MLXSW_SP_PORT_HW_TC_STATS_LEN) * \
1563 IEEE_8021QAZ_MAX_TCS)
1565 static void mlxsw_sp_port_get_prio_strings(u8
**p
, int prio
)
1569 for (i
= 0; i
< MLXSW_SP_PORT_HW_PRIO_STATS_LEN
; i
++) {
1570 snprintf(*p
, ETH_GSTRING_LEN
, "%s_%d",
1571 mlxsw_sp_port_hw_prio_stats
[i
].str
, prio
);
1572 *p
+= ETH_GSTRING_LEN
;
1576 static void mlxsw_sp_port_get_tc_strings(u8
**p
, int tc
)
1580 for (i
= 0; i
< MLXSW_SP_PORT_HW_TC_STATS_LEN
; i
++) {
1581 snprintf(*p
, ETH_GSTRING_LEN
, "%s_%d",
1582 mlxsw_sp_port_hw_tc_stats
[i
].str
, tc
);
1583 *p
+= ETH_GSTRING_LEN
;
1587 static void mlxsw_sp_port_get_strings(struct net_device
*dev
,
1588 u32 stringset
, u8
*data
)
1593 switch (stringset
) {
1595 for (i
= 0; i
< MLXSW_SP_PORT_HW_STATS_LEN
; i
++) {
1596 memcpy(p
, mlxsw_sp_port_hw_stats
[i
].str
,
1598 p
+= ETH_GSTRING_LEN
;
1601 for (i
= 0; i
< IEEE_8021QAZ_MAX_TCS
; i
++)
1602 mlxsw_sp_port_get_prio_strings(&p
, i
);
1604 for (i
= 0; i
< IEEE_8021QAZ_MAX_TCS
; i
++)
1605 mlxsw_sp_port_get_tc_strings(&p
, i
);
1611 static int mlxsw_sp_port_set_phys_id(struct net_device
*dev
,
1612 enum ethtool_phys_id_state state
)
1614 struct mlxsw_sp_port
*mlxsw_sp_port
= netdev_priv(dev
);
1615 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
1616 char mlcr_pl
[MLXSW_REG_MLCR_LEN
];
1620 case ETHTOOL_ID_ACTIVE
:
1623 case ETHTOOL_ID_INACTIVE
:
1630 mlxsw_reg_mlcr_pack(mlcr_pl
, mlxsw_sp_port
->local_port
, active
);
1631 return mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(mlcr
), mlcr_pl
);
1635 mlxsw_sp_get_hw_stats_by_group(struct mlxsw_sp_port_hw_stats
**p_hw_stats
,
1636 int *p_len
, enum mlxsw_reg_ppcnt_grp grp
)
1639 case MLXSW_REG_PPCNT_IEEE_8023_CNT
:
1640 *p_hw_stats
= mlxsw_sp_port_hw_stats
;
1641 *p_len
= MLXSW_SP_PORT_HW_STATS_LEN
;
1643 case MLXSW_REG_PPCNT_PRIO_CNT
:
1644 *p_hw_stats
= mlxsw_sp_port_hw_prio_stats
;
1645 *p_len
= MLXSW_SP_PORT_HW_PRIO_STATS_LEN
;
1647 case MLXSW_REG_PPCNT_TC_CNT
:
1648 *p_hw_stats
= mlxsw_sp_port_hw_tc_stats
;
1649 *p_len
= MLXSW_SP_PORT_HW_TC_STATS_LEN
;
1658 static void __mlxsw_sp_port_get_stats(struct net_device
*dev
,
1659 enum mlxsw_reg_ppcnt_grp grp
, int prio
,
1660 u64
*data
, int data_index
)
1662 struct mlxsw_sp_port_hw_stats
*hw_stats
;
1663 char ppcnt_pl
[MLXSW_REG_PPCNT_LEN
];
1667 err
= mlxsw_sp_get_hw_stats_by_group(&hw_stats
, &len
, grp
);
1670 mlxsw_sp_port_get_stats_raw(dev
, grp
, prio
, ppcnt_pl
);
1671 for (i
= 0; i
< len
; i
++)
1672 data
[data_index
+ i
] = hw_stats
[i
].getter(ppcnt_pl
);
1675 static void mlxsw_sp_port_get_stats(struct net_device
*dev
,
1676 struct ethtool_stats
*stats
, u64
*data
)
1678 int i
, data_index
= 0;
1680 /* IEEE 802.3 Counters */
1681 __mlxsw_sp_port_get_stats(dev
, MLXSW_REG_PPCNT_IEEE_8023_CNT
, 0,
1683 data_index
= MLXSW_SP_PORT_HW_STATS_LEN
;
1685 /* Per-Priority Counters */
1686 for (i
= 0; i
< IEEE_8021QAZ_MAX_TCS
; i
++) {
1687 __mlxsw_sp_port_get_stats(dev
, MLXSW_REG_PPCNT_PRIO_CNT
, i
,
1689 data_index
+= MLXSW_SP_PORT_HW_PRIO_STATS_LEN
;
1692 /* Per-TC Counters */
1693 for (i
= 0; i
< IEEE_8021QAZ_MAX_TCS
; i
++) {
1694 __mlxsw_sp_port_get_stats(dev
, MLXSW_REG_PPCNT_TC_CNT
, i
,
1696 data_index
+= MLXSW_SP_PORT_HW_TC_STATS_LEN
;
1700 static int mlxsw_sp_port_get_sset_count(struct net_device
*dev
, int sset
)
1704 return MLXSW_SP_PORT_ETHTOOL_STATS_LEN
;
1710 struct mlxsw_sp_port_link_mode
{
1711 enum ethtool_link_mode_bit_indices mask_ethtool
;
1716 static const struct mlxsw_sp_port_link_mode mlxsw_sp_port_link_mode
[] = {
1718 .mask
= MLXSW_REG_PTYS_ETH_SPEED_100BASE_T
,
1719 .mask_ethtool
= ETHTOOL_LINK_MODE_100baseT_Full_BIT
,
1723 .mask
= MLXSW_REG_PTYS_ETH_SPEED_SGMII
|
1724 MLXSW_REG_PTYS_ETH_SPEED_1000BASE_KX
,
1725 .mask_ethtool
= ETHTOOL_LINK_MODE_1000baseKX_Full_BIT
,
1726 .speed
= SPEED_1000
,
1729 .mask
= MLXSW_REG_PTYS_ETH_SPEED_10GBASE_T
,
1730 .mask_ethtool
= ETHTOOL_LINK_MODE_10000baseT_Full_BIT
,
1731 .speed
= SPEED_10000
,
1734 .mask
= MLXSW_REG_PTYS_ETH_SPEED_10GBASE_CX4
|
1735 MLXSW_REG_PTYS_ETH_SPEED_10GBASE_KX4
,
1736 .mask_ethtool
= ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT
,
1737 .speed
= SPEED_10000
,
1740 .mask
= MLXSW_REG_PTYS_ETH_SPEED_10GBASE_KR
|
1741 MLXSW_REG_PTYS_ETH_SPEED_10GBASE_CR
|
1742 MLXSW_REG_PTYS_ETH_SPEED_10GBASE_SR
|
1743 MLXSW_REG_PTYS_ETH_SPEED_10GBASE_ER_LR
,
1744 .mask_ethtool
= ETHTOOL_LINK_MODE_10000baseKR_Full_BIT
,
1745 .speed
= SPEED_10000
,
1748 .mask
= MLXSW_REG_PTYS_ETH_SPEED_20GBASE_KR2
,
1749 .mask_ethtool
= ETHTOOL_LINK_MODE_20000baseKR2_Full_BIT
,
1750 .speed
= SPEED_20000
,
1753 .mask
= MLXSW_REG_PTYS_ETH_SPEED_40GBASE_CR4
,
1754 .mask_ethtool
= ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT
,
1755 .speed
= SPEED_40000
,
1758 .mask
= MLXSW_REG_PTYS_ETH_SPEED_40GBASE_KR4
,
1759 .mask_ethtool
= ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT
,
1760 .speed
= SPEED_40000
,
1763 .mask
= MLXSW_REG_PTYS_ETH_SPEED_40GBASE_SR4
,
1764 .mask_ethtool
= ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT
,
1765 .speed
= SPEED_40000
,
1768 .mask
= MLXSW_REG_PTYS_ETH_SPEED_40GBASE_LR4_ER4
,
1769 .mask_ethtool
= ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT
,
1770 .speed
= SPEED_40000
,
1773 .mask
= MLXSW_REG_PTYS_ETH_SPEED_25GBASE_CR
,
1774 .mask_ethtool
= ETHTOOL_LINK_MODE_25000baseCR_Full_BIT
,
1775 .speed
= SPEED_25000
,
1778 .mask
= MLXSW_REG_PTYS_ETH_SPEED_25GBASE_KR
,
1779 .mask_ethtool
= ETHTOOL_LINK_MODE_25000baseKR_Full_BIT
,
1780 .speed
= SPEED_25000
,
1783 .mask
= MLXSW_REG_PTYS_ETH_SPEED_25GBASE_SR
,
1784 .mask_ethtool
= ETHTOOL_LINK_MODE_25000baseSR_Full_BIT
,
1785 .speed
= SPEED_25000
,
1788 .mask
= MLXSW_REG_PTYS_ETH_SPEED_25GBASE_SR
,
1789 .mask_ethtool
= ETHTOOL_LINK_MODE_25000baseSR_Full_BIT
,
1790 .speed
= SPEED_25000
,
1793 .mask
= MLXSW_REG_PTYS_ETH_SPEED_50GBASE_CR2
,
1794 .mask_ethtool
= ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT
,
1795 .speed
= SPEED_50000
,
1798 .mask
= MLXSW_REG_PTYS_ETH_SPEED_50GBASE_KR2
,
1799 .mask_ethtool
= ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT
,
1800 .speed
= SPEED_50000
,
1803 .mask
= MLXSW_REG_PTYS_ETH_SPEED_50GBASE_SR2
,
1804 .mask_ethtool
= ETHTOOL_LINK_MODE_50000baseSR2_Full_BIT
,
1805 .speed
= SPEED_50000
,
1808 .mask
= MLXSW_REG_PTYS_ETH_SPEED_56GBASE_R4
,
1809 .mask_ethtool
= ETHTOOL_LINK_MODE_56000baseKR4_Full_BIT
,
1810 .speed
= SPEED_56000
,
1813 .mask
= MLXSW_REG_PTYS_ETH_SPEED_56GBASE_R4
,
1814 .mask_ethtool
= ETHTOOL_LINK_MODE_56000baseCR4_Full_BIT
,
1815 .speed
= SPEED_56000
,
1818 .mask
= MLXSW_REG_PTYS_ETH_SPEED_56GBASE_R4
,
1819 .mask_ethtool
= ETHTOOL_LINK_MODE_56000baseSR4_Full_BIT
,
1820 .speed
= SPEED_56000
,
1823 .mask
= MLXSW_REG_PTYS_ETH_SPEED_56GBASE_R4
,
1824 .mask_ethtool
= ETHTOOL_LINK_MODE_56000baseLR4_Full_BIT
,
1825 .speed
= SPEED_56000
,
1828 .mask
= MLXSW_REG_PTYS_ETH_SPEED_100GBASE_CR4
,
1829 .mask_ethtool
= ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT
,
1830 .speed
= SPEED_100000
,
1833 .mask
= MLXSW_REG_PTYS_ETH_SPEED_100GBASE_SR4
,
1834 .mask_ethtool
= ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT
,
1835 .speed
= SPEED_100000
,
1838 .mask
= MLXSW_REG_PTYS_ETH_SPEED_100GBASE_KR4
,
1839 .mask_ethtool
= ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT
,
1840 .speed
= SPEED_100000
,
1843 .mask
= MLXSW_REG_PTYS_ETH_SPEED_100GBASE_LR4_ER4
,
1844 .mask_ethtool
= ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT
,
1845 .speed
= SPEED_100000
,
1849 #define MLXSW_SP_PORT_LINK_MODE_LEN ARRAY_SIZE(mlxsw_sp_port_link_mode)
1852 mlxsw_sp_from_ptys_supported_port(u32 ptys_eth_proto
,
1853 struct ethtool_link_ksettings
*cmd
)
1855 if (ptys_eth_proto
& (MLXSW_REG_PTYS_ETH_SPEED_10GBASE_CR
|
1856 MLXSW_REG_PTYS_ETH_SPEED_10GBASE_SR
|
1857 MLXSW_REG_PTYS_ETH_SPEED_40GBASE_CR4
|
1858 MLXSW_REG_PTYS_ETH_SPEED_40GBASE_SR4
|
1859 MLXSW_REG_PTYS_ETH_SPEED_100GBASE_SR4
|
1860 MLXSW_REG_PTYS_ETH_SPEED_SGMII
))
1861 ethtool_link_ksettings_add_link_mode(cmd
, supported
, FIBRE
);
1863 if (ptys_eth_proto
& (MLXSW_REG_PTYS_ETH_SPEED_10GBASE_KR
|
1864 MLXSW_REG_PTYS_ETH_SPEED_10GBASE_KX4
|
1865 MLXSW_REG_PTYS_ETH_SPEED_40GBASE_KR4
|
1866 MLXSW_REG_PTYS_ETH_SPEED_100GBASE_KR4
|
1867 MLXSW_REG_PTYS_ETH_SPEED_1000BASE_KX
))
1868 ethtool_link_ksettings_add_link_mode(cmd
, supported
, Backplane
);
1871 static void mlxsw_sp_from_ptys_link(u32 ptys_eth_proto
, unsigned long *mode
)
1875 for (i
= 0; i
< MLXSW_SP_PORT_LINK_MODE_LEN
; i
++) {
1876 if (ptys_eth_proto
& mlxsw_sp_port_link_mode
[i
].mask
)
1877 __set_bit(mlxsw_sp_port_link_mode
[i
].mask_ethtool
,
1882 static void mlxsw_sp_from_ptys_speed_duplex(bool carrier_ok
, u32 ptys_eth_proto
,
1883 struct ethtool_link_ksettings
*cmd
)
1885 u32 speed
= SPEED_UNKNOWN
;
1886 u8 duplex
= DUPLEX_UNKNOWN
;
1892 for (i
= 0; i
< MLXSW_SP_PORT_LINK_MODE_LEN
; i
++) {
1893 if (ptys_eth_proto
& mlxsw_sp_port_link_mode
[i
].mask
) {
1894 speed
= mlxsw_sp_port_link_mode
[i
].speed
;
1895 duplex
= DUPLEX_FULL
;
1900 cmd
->base
.speed
= speed
;
1901 cmd
->base
.duplex
= duplex
;
1904 static u8
mlxsw_sp_port_connector_port(u32 ptys_eth_proto
)
1906 if (ptys_eth_proto
& (MLXSW_REG_PTYS_ETH_SPEED_10GBASE_SR
|
1907 MLXSW_REG_PTYS_ETH_SPEED_40GBASE_SR4
|
1908 MLXSW_REG_PTYS_ETH_SPEED_100GBASE_SR4
|
1909 MLXSW_REG_PTYS_ETH_SPEED_SGMII
))
1912 if (ptys_eth_proto
& (MLXSW_REG_PTYS_ETH_SPEED_10GBASE_CR
|
1913 MLXSW_REG_PTYS_ETH_SPEED_40GBASE_CR4
|
1914 MLXSW_REG_PTYS_ETH_SPEED_100GBASE_CR4
))
1917 if (ptys_eth_proto
& (MLXSW_REG_PTYS_ETH_SPEED_10GBASE_KR
|
1918 MLXSW_REG_PTYS_ETH_SPEED_10GBASE_KX4
|
1919 MLXSW_REG_PTYS_ETH_SPEED_40GBASE_KR4
|
1920 MLXSW_REG_PTYS_ETH_SPEED_100GBASE_KR4
))
1927 mlxsw_sp_to_ptys_advert_link(const struct ethtool_link_ksettings
*cmd
)
1932 for (i
= 0; i
< MLXSW_SP_PORT_LINK_MODE_LEN
; i
++) {
1933 if (test_bit(mlxsw_sp_port_link_mode
[i
].mask_ethtool
,
1934 cmd
->link_modes
.advertising
))
1935 ptys_proto
|= mlxsw_sp_port_link_mode
[i
].mask
;
1940 static u32
mlxsw_sp_to_ptys_speed(u32 speed
)
1945 for (i
= 0; i
< MLXSW_SP_PORT_LINK_MODE_LEN
; i
++) {
1946 if (speed
== mlxsw_sp_port_link_mode
[i
].speed
)
1947 ptys_proto
|= mlxsw_sp_port_link_mode
[i
].mask
;
1952 static u32
mlxsw_sp_to_ptys_upper_speed(u32 upper_speed
)
1957 for (i
= 0; i
< MLXSW_SP_PORT_LINK_MODE_LEN
; i
++) {
1958 if (mlxsw_sp_port_link_mode
[i
].speed
<= upper_speed
)
1959 ptys_proto
|= mlxsw_sp_port_link_mode
[i
].mask
;
1964 static void mlxsw_sp_port_get_link_supported(u32 eth_proto_cap
,
1965 struct ethtool_link_ksettings
*cmd
)
1967 ethtool_link_ksettings_add_link_mode(cmd
, supported
, Asym_Pause
);
1968 ethtool_link_ksettings_add_link_mode(cmd
, supported
, Autoneg
);
1969 ethtool_link_ksettings_add_link_mode(cmd
, supported
, Pause
);
1971 mlxsw_sp_from_ptys_supported_port(eth_proto_cap
, cmd
);
1972 mlxsw_sp_from_ptys_link(eth_proto_cap
, cmd
->link_modes
.supported
);
1975 static void mlxsw_sp_port_get_link_advertise(u32 eth_proto_admin
, bool autoneg
,
1976 struct ethtool_link_ksettings
*cmd
)
1981 ethtool_link_ksettings_add_link_mode(cmd
, advertising
, Autoneg
);
1982 mlxsw_sp_from_ptys_link(eth_proto_admin
, cmd
->link_modes
.advertising
);
1986 mlxsw_sp_port_get_link_lp_advertise(u32 eth_proto_lp
, u8 autoneg_status
,
1987 struct ethtool_link_ksettings
*cmd
)
1989 if (autoneg_status
!= MLXSW_REG_PTYS_AN_STATUS_OK
|| !eth_proto_lp
)
1992 ethtool_link_ksettings_add_link_mode(cmd
, lp_advertising
, Autoneg
);
1993 mlxsw_sp_from_ptys_link(eth_proto_lp
, cmd
->link_modes
.lp_advertising
);
1996 static int mlxsw_sp_port_get_link_ksettings(struct net_device
*dev
,
1997 struct ethtool_link_ksettings
*cmd
)
1999 u32 eth_proto_cap
, eth_proto_admin
, eth_proto_oper
, eth_proto_lp
;
2000 struct mlxsw_sp_port
*mlxsw_sp_port
= netdev_priv(dev
);
2001 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
2002 char ptys_pl
[MLXSW_REG_PTYS_LEN
];
2007 autoneg
= mlxsw_sp_port
->link
.autoneg
;
2008 mlxsw_reg_ptys_eth_pack(ptys_pl
, mlxsw_sp_port
->local_port
, 0);
2009 err
= mlxsw_reg_query(mlxsw_sp
->core
, MLXSW_REG(ptys
), ptys_pl
);
2012 mlxsw_reg_ptys_eth_unpack(ptys_pl
, ð_proto_cap
, ð_proto_admin
,
2015 mlxsw_sp_port_get_link_supported(eth_proto_cap
, cmd
);
2017 mlxsw_sp_port_get_link_advertise(eth_proto_admin
, autoneg
, cmd
);
2019 eth_proto_lp
= mlxsw_reg_ptys_eth_proto_lp_advertise_get(ptys_pl
);
2020 autoneg_status
= mlxsw_reg_ptys_an_status_get(ptys_pl
);
2021 mlxsw_sp_port_get_link_lp_advertise(eth_proto_lp
, autoneg_status
, cmd
);
2023 cmd
->base
.autoneg
= autoneg
? AUTONEG_ENABLE
: AUTONEG_DISABLE
;
2024 cmd
->base
.port
= mlxsw_sp_port_connector_port(eth_proto_oper
);
2025 mlxsw_sp_from_ptys_speed_duplex(netif_carrier_ok(dev
), eth_proto_oper
,
2032 mlxsw_sp_port_set_link_ksettings(struct net_device
*dev
,
2033 const struct ethtool_link_ksettings
*cmd
)
2035 struct mlxsw_sp_port
*mlxsw_sp_port
= netdev_priv(dev
);
2036 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
2037 char ptys_pl
[MLXSW_REG_PTYS_LEN
];
2038 u32 eth_proto_cap
, eth_proto_new
;
2042 mlxsw_reg_ptys_eth_pack(ptys_pl
, mlxsw_sp_port
->local_port
, 0);
2043 err
= mlxsw_reg_query(mlxsw_sp
->core
, MLXSW_REG(ptys
), ptys_pl
);
2046 mlxsw_reg_ptys_eth_unpack(ptys_pl
, ð_proto_cap
, NULL
, NULL
);
2048 autoneg
= cmd
->base
.autoneg
== AUTONEG_ENABLE
;
2049 eth_proto_new
= autoneg
?
2050 mlxsw_sp_to_ptys_advert_link(cmd
) :
2051 mlxsw_sp_to_ptys_speed(cmd
->base
.speed
);
2053 eth_proto_new
= eth_proto_new
& eth_proto_cap
;
2054 if (!eth_proto_new
) {
2055 netdev_err(dev
, "No supported speed requested\n");
2059 mlxsw_reg_ptys_eth_pack(ptys_pl
, mlxsw_sp_port
->local_port
,
2061 err
= mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(ptys
), ptys_pl
);
2065 if (!netif_running(dev
))
2068 mlxsw_sp_port
->link
.autoneg
= autoneg
;
2070 mlxsw_sp_port_admin_status_set(mlxsw_sp_port
, false);
2071 mlxsw_sp_port_admin_status_set(mlxsw_sp_port
, true);
2076 static const struct ethtool_ops mlxsw_sp_port_ethtool_ops
= {
2077 .get_drvinfo
= mlxsw_sp_port_get_drvinfo
,
2078 .get_link
= ethtool_op_get_link
,
2079 .get_pauseparam
= mlxsw_sp_port_get_pauseparam
,
2080 .set_pauseparam
= mlxsw_sp_port_set_pauseparam
,
2081 .get_strings
= mlxsw_sp_port_get_strings
,
2082 .set_phys_id
= mlxsw_sp_port_set_phys_id
,
2083 .get_ethtool_stats
= mlxsw_sp_port_get_stats
,
2084 .get_sset_count
= mlxsw_sp_port_get_sset_count
,
2085 .get_link_ksettings
= mlxsw_sp_port_get_link_ksettings
,
2086 .set_link_ksettings
= mlxsw_sp_port_set_link_ksettings
,
2090 mlxsw_sp_port_speed_by_width_set(struct mlxsw_sp_port
*mlxsw_sp_port
, u8 width
)
2092 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
2093 u32 upper_speed
= MLXSW_SP_PORT_BASE_SPEED
* width
;
2094 char ptys_pl
[MLXSW_REG_PTYS_LEN
];
2095 u32 eth_proto_admin
;
2097 eth_proto_admin
= mlxsw_sp_to_ptys_upper_speed(upper_speed
);
2098 mlxsw_reg_ptys_eth_pack(ptys_pl
, mlxsw_sp_port
->local_port
,
2100 return mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(ptys
), ptys_pl
);
2103 int mlxsw_sp_port_ets_set(struct mlxsw_sp_port
*mlxsw_sp_port
,
2104 enum mlxsw_reg_qeec_hr hr
, u8 index
, u8 next_index
,
2105 bool dwrr
, u8 dwrr_weight
)
2107 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
2108 char qeec_pl
[MLXSW_REG_QEEC_LEN
];
2110 mlxsw_reg_qeec_pack(qeec_pl
, mlxsw_sp_port
->local_port
, hr
, index
,
2112 mlxsw_reg_qeec_de_set(qeec_pl
, true);
2113 mlxsw_reg_qeec_dwrr_set(qeec_pl
, dwrr
);
2114 mlxsw_reg_qeec_dwrr_weight_set(qeec_pl
, dwrr_weight
);
2115 return mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(qeec
), qeec_pl
);
2118 int mlxsw_sp_port_ets_maxrate_set(struct mlxsw_sp_port
*mlxsw_sp_port
,
2119 enum mlxsw_reg_qeec_hr hr
, u8 index
,
2120 u8 next_index
, u32 maxrate
)
2122 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
2123 char qeec_pl
[MLXSW_REG_QEEC_LEN
];
2125 mlxsw_reg_qeec_pack(qeec_pl
, mlxsw_sp_port
->local_port
, hr
, index
,
2127 mlxsw_reg_qeec_mase_set(qeec_pl
, true);
2128 mlxsw_reg_qeec_max_shaper_rate_set(qeec_pl
, maxrate
);
2129 return mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(qeec
), qeec_pl
);
2132 int mlxsw_sp_port_prio_tc_set(struct mlxsw_sp_port
*mlxsw_sp_port
,
2133 u8 switch_prio
, u8 tclass
)
2135 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
2136 char qtct_pl
[MLXSW_REG_QTCT_LEN
];
2138 mlxsw_reg_qtct_pack(qtct_pl
, mlxsw_sp_port
->local_port
, switch_prio
,
2140 return mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(qtct
), qtct_pl
);
2143 static int mlxsw_sp_port_ets_init(struct mlxsw_sp_port
*mlxsw_sp_port
)
2147 /* Setup the elements hierarcy, so that each TC is linked to
2148 * one subgroup, which are all member in the same group.
2150 err
= mlxsw_sp_port_ets_set(mlxsw_sp_port
,
2151 MLXSW_REG_QEEC_HIERARCY_GROUP
, 0, 0, false,
2155 for (i
= 0; i
< IEEE_8021QAZ_MAX_TCS
; i
++) {
2156 err
= mlxsw_sp_port_ets_set(mlxsw_sp_port
,
2157 MLXSW_REG_QEEC_HIERARCY_SUBGROUP
, i
,
2162 for (i
= 0; i
< IEEE_8021QAZ_MAX_TCS
; i
++) {
2163 err
= mlxsw_sp_port_ets_set(mlxsw_sp_port
,
2164 MLXSW_REG_QEEC_HIERARCY_TC
, i
, i
,
2170 /* Make sure the max shaper is disabled in all hierarcies that
2173 err
= mlxsw_sp_port_ets_maxrate_set(mlxsw_sp_port
,
2174 MLXSW_REG_QEEC_HIERARCY_PORT
, 0, 0,
2175 MLXSW_REG_QEEC_MAS_DIS
);
2178 for (i
= 0; i
< IEEE_8021QAZ_MAX_TCS
; i
++) {
2179 err
= mlxsw_sp_port_ets_maxrate_set(mlxsw_sp_port
,
2180 MLXSW_REG_QEEC_HIERARCY_SUBGROUP
,
2182 MLXSW_REG_QEEC_MAS_DIS
);
2186 for (i
= 0; i
< IEEE_8021QAZ_MAX_TCS
; i
++) {
2187 err
= mlxsw_sp_port_ets_maxrate_set(mlxsw_sp_port
,
2188 MLXSW_REG_QEEC_HIERARCY_TC
,
2190 MLXSW_REG_QEEC_MAS_DIS
);
2195 /* Map all priorities to traffic class 0. */
2196 for (i
= 0; i
< IEEE_8021QAZ_MAX_TCS
; i
++) {
2197 err
= mlxsw_sp_port_prio_tc_set(mlxsw_sp_port
, i
, 0);
2205 static int mlxsw_sp_port_pvid_vport_create(struct mlxsw_sp_port
*mlxsw_sp_port
)
2207 mlxsw_sp_port
->pvid
= 1;
2209 return mlxsw_sp_port_add_vid(mlxsw_sp_port
->dev
, 0, 1);
2212 static int mlxsw_sp_port_pvid_vport_destroy(struct mlxsw_sp_port
*mlxsw_sp_port
)
2214 return mlxsw_sp_port_kill_vid(mlxsw_sp_port
->dev
, 0, 1);
2217 static int __mlxsw_sp_port_create(struct mlxsw_sp
*mlxsw_sp
, u8 local_port
,
2218 bool split
, u8 module
, u8 width
, u8 lane
)
2220 struct mlxsw_sp_port
*mlxsw_sp_port
;
2221 struct net_device
*dev
;
2225 dev
= alloc_etherdev(sizeof(struct mlxsw_sp_port
));
2228 SET_NETDEV_DEV(dev
, mlxsw_sp
->bus_info
->dev
);
2229 mlxsw_sp_port
= netdev_priv(dev
);
2230 mlxsw_sp_port
->dev
= dev
;
2231 mlxsw_sp_port
->mlxsw_sp
= mlxsw_sp
;
2232 mlxsw_sp_port
->local_port
= local_port
;
2233 mlxsw_sp_port
->split
= split
;
2234 mlxsw_sp_port
->mapping
.module
= module
;
2235 mlxsw_sp_port
->mapping
.width
= width
;
2236 mlxsw_sp_port
->mapping
.lane
= lane
;
2237 mlxsw_sp_port
->link
.autoneg
= 1;
2238 bytes
= DIV_ROUND_UP(VLAN_N_VID
, BITS_PER_BYTE
);
2239 mlxsw_sp_port
->active_vlans
= kzalloc(bytes
, GFP_KERNEL
);
2240 if (!mlxsw_sp_port
->active_vlans
) {
2242 goto err_port_active_vlans_alloc
;
2244 mlxsw_sp_port
->untagged_vlans
= kzalloc(bytes
, GFP_KERNEL
);
2245 if (!mlxsw_sp_port
->untagged_vlans
) {
2247 goto err_port_untagged_vlans_alloc
;
2249 INIT_LIST_HEAD(&mlxsw_sp_port
->vports_list
);
2250 INIT_LIST_HEAD(&mlxsw_sp_port
->mall_tc_list
);
2252 mlxsw_sp_port
->pcpu_stats
=
2253 netdev_alloc_pcpu_stats(struct mlxsw_sp_port_pcpu_stats
);
2254 if (!mlxsw_sp_port
->pcpu_stats
) {
2256 goto err_alloc_stats
;
2259 mlxsw_sp_port
->hw_stats
.cache
=
2260 kzalloc(sizeof(*mlxsw_sp_port
->hw_stats
.cache
), GFP_KERNEL
);
2262 if (!mlxsw_sp_port
->hw_stats
.cache
) {
2264 goto err_alloc_hw_stats
;
2266 INIT_DELAYED_WORK(&mlxsw_sp_port
->hw_stats
.update_dw
,
2267 &update_stats_cache
);
2269 dev
->netdev_ops
= &mlxsw_sp_port_netdev_ops
;
2270 dev
->ethtool_ops
= &mlxsw_sp_port_ethtool_ops
;
2272 err
= mlxsw_sp_port_swid_set(mlxsw_sp_port
, 0);
2274 dev_err(mlxsw_sp
->bus_info
->dev
, "Port %d: Failed to set SWID\n",
2275 mlxsw_sp_port
->local_port
);
2276 goto err_port_swid_set
;
2279 err
= mlxsw_sp_port_dev_addr_init(mlxsw_sp_port
);
2281 dev_err(mlxsw_sp
->bus_info
->dev
, "Port %d: Unable to init port mac address\n",
2282 mlxsw_sp_port
->local_port
);
2283 goto err_dev_addr_init
;
2286 netif_carrier_off(dev
);
2288 dev
->features
|= NETIF_F_NETNS_LOCAL
| NETIF_F_LLTX
| NETIF_F_SG
|
2289 NETIF_F_HW_VLAN_CTAG_FILTER
| NETIF_F_HW_TC
;
2290 dev
->hw_features
|= NETIF_F_HW_TC
;
2293 dev
->max_mtu
= ETH_MAX_MTU
;
2295 /* Each packet needs to have a Tx header (metadata) on top all other
2298 dev
->needed_headroom
= MLXSW_TXHDR_LEN
;
2300 err
= mlxsw_sp_port_system_port_mapping_set(mlxsw_sp_port
);
2302 dev_err(mlxsw_sp
->bus_info
->dev
, "Port %d: Failed to set system port mapping\n",
2303 mlxsw_sp_port
->local_port
);
2304 goto err_port_system_port_mapping_set
;
2307 err
= mlxsw_sp_port_speed_by_width_set(mlxsw_sp_port
, width
);
2309 dev_err(mlxsw_sp
->bus_info
->dev
, "Port %d: Failed to enable speeds\n",
2310 mlxsw_sp_port
->local_port
);
2311 goto err_port_speed_by_width_set
;
2314 err
= mlxsw_sp_port_mtu_set(mlxsw_sp_port
, ETH_DATA_LEN
);
2316 dev_err(mlxsw_sp
->bus_info
->dev
, "Port %d: Failed to set MTU\n",
2317 mlxsw_sp_port
->local_port
);
2318 goto err_port_mtu_set
;
2321 err
= mlxsw_sp_port_admin_status_set(mlxsw_sp_port
, false);
2323 goto err_port_admin_status_set
;
2325 err
= mlxsw_sp_port_buffers_init(mlxsw_sp_port
);
2327 dev_err(mlxsw_sp
->bus_info
->dev
, "Port %d: Failed to initialize buffers\n",
2328 mlxsw_sp_port
->local_port
);
2329 goto err_port_buffers_init
;
2332 err
= mlxsw_sp_port_ets_init(mlxsw_sp_port
);
2334 dev_err(mlxsw_sp
->bus_info
->dev
, "Port %d: Failed to initialize ETS\n",
2335 mlxsw_sp_port
->local_port
);
2336 goto err_port_ets_init
;
2339 /* ETS and buffers must be initialized before DCB. */
2340 err
= mlxsw_sp_port_dcb_init(mlxsw_sp_port
);
2342 dev_err(mlxsw_sp
->bus_info
->dev
, "Port %d: Failed to initialize DCB\n",
2343 mlxsw_sp_port
->local_port
);
2344 goto err_port_dcb_init
;
2347 err
= mlxsw_sp_port_pvid_vport_create(mlxsw_sp_port
);
2349 dev_err(mlxsw_sp
->bus_info
->dev
, "Port %d: Failed to create PVID vPort\n",
2350 mlxsw_sp_port
->local_port
);
2351 goto err_port_pvid_vport_create
;
2354 mlxsw_sp_port_switchdev_init(mlxsw_sp_port
);
2355 mlxsw_sp
->ports
[local_port
] = mlxsw_sp_port
;
2356 err
= register_netdev(dev
);
2358 dev_err(mlxsw_sp
->bus_info
->dev
, "Port %d: Failed to register netdev\n",
2359 mlxsw_sp_port
->local_port
);
2360 goto err_register_netdev
;
2363 mlxsw_core_port_eth_set(mlxsw_sp
->core
, mlxsw_sp_port
->local_port
,
2364 mlxsw_sp_port
, dev
, mlxsw_sp_port
->split
,
2366 mlxsw_core_schedule_dw(&mlxsw_sp_port
->hw_stats
.update_dw
, 0);
2369 err_register_netdev
:
2370 mlxsw_sp
->ports
[local_port
] = NULL
;
2371 mlxsw_sp_port_switchdev_fini(mlxsw_sp_port
);
2372 mlxsw_sp_port_pvid_vport_destroy(mlxsw_sp_port
);
2373 err_port_pvid_vport_create
:
2374 mlxsw_sp_port_dcb_fini(mlxsw_sp_port
);
2377 err_port_buffers_init
:
2378 err_port_admin_status_set
:
2380 err_port_speed_by_width_set
:
2381 err_port_system_port_mapping_set
:
2383 mlxsw_sp_port_swid_set(mlxsw_sp_port
, MLXSW_PORT_SWID_DISABLED_PORT
);
2385 kfree(mlxsw_sp_port
->hw_stats
.cache
);
2387 free_percpu(mlxsw_sp_port
->pcpu_stats
);
2389 kfree(mlxsw_sp_port
->untagged_vlans
);
2390 err_port_untagged_vlans_alloc
:
2391 kfree(mlxsw_sp_port
->active_vlans
);
2392 err_port_active_vlans_alloc
:
2397 static int mlxsw_sp_port_create(struct mlxsw_sp
*mlxsw_sp
, u8 local_port
,
2398 bool split
, u8 module
, u8 width
, u8 lane
)
2402 err
= mlxsw_core_port_init(mlxsw_sp
->core
, local_port
);
2404 dev_err(mlxsw_sp
->bus_info
->dev
, "Port %d: Failed to init core port\n",
2408 err
= __mlxsw_sp_port_create(mlxsw_sp
, local_port
, split
,
2409 module
, width
, lane
);
2411 goto err_port_create
;
2415 mlxsw_core_port_fini(mlxsw_sp
->core
, local_port
);
2419 static void __mlxsw_sp_port_remove(struct mlxsw_sp
*mlxsw_sp
, u8 local_port
)
2421 struct mlxsw_sp_port
*mlxsw_sp_port
= mlxsw_sp
->ports
[local_port
];
2423 cancel_delayed_work_sync(&mlxsw_sp_port
->hw_stats
.update_dw
);
2424 mlxsw_core_port_clear(mlxsw_sp
->core
, local_port
, mlxsw_sp
);
2425 unregister_netdev(mlxsw_sp_port
->dev
); /* This calls ndo_stop */
2426 mlxsw_sp
->ports
[local_port
] = NULL
;
2427 mlxsw_sp_port_switchdev_fini(mlxsw_sp_port
);
2428 mlxsw_sp_port_pvid_vport_destroy(mlxsw_sp_port
);
2429 mlxsw_sp_port_dcb_fini(mlxsw_sp_port
);
2430 mlxsw_sp_port_swid_set(mlxsw_sp_port
, MLXSW_PORT_SWID_DISABLED_PORT
);
2431 mlxsw_sp_port_module_unmap(mlxsw_sp
, mlxsw_sp_port
->local_port
);
2432 free_percpu(mlxsw_sp_port
->pcpu_stats
);
2433 kfree(mlxsw_sp_port
->hw_stats
.cache
);
2434 kfree(mlxsw_sp_port
->untagged_vlans
);
2435 kfree(mlxsw_sp_port
->active_vlans
);
2436 WARN_ON_ONCE(!list_empty(&mlxsw_sp_port
->vports_list
));
2437 free_netdev(mlxsw_sp_port
->dev
);
2440 static void mlxsw_sp_port_remove(struct mlxsw_sp
*mlxsw_sp
, u8 local_port
)
2442 __mlxsw_sp_port_remove(mlxsw_sp
, local_port
);
2443 mlxsw_core_port_fini(mlxsw_sp
->core
, local_port
);
2446 static bool mlxsw_sp_port_created(struct mlxsw_sp
*mlxsw_sp
, u8 local_port
)
2448 return mlxsw_sp
->ports
[local_port
] != NULL
;
2451 static void mlxsw_sp_ports_remove(struct mlxsw_sp
*mlxsw_sp
)
2455 for (i
= 1; i
< MLXSW_PORT_MAX_PORTS
; i
++)
2456 if (mlxsw_sp_port_created(mlxsw_sp
, i
))
2457 mlxsw_sp_port_remove(mlxsw_sp
, i
);
2458 kfree(mlxsw_sp
->ports
);
2461 static int mlxsw_sp_ports_create(struct mlxsw_sp
*mlxsw_sp
)
2463 u8 module
, width
, lane
;
2468 alloc_size
= sizeof(struct mlxsw_sp_port
*) * MLXSW_PORT_MAX_PORTS
;
2469 mlxsw_sp
->ports
= kzalloc(alloc_size
, GFP_KERNEL
);
2470 if (!mlxsw_sp
->ports
)
2473 for (i
= 1; i
< MLXSW_PORT_MAX_PORTS
; i
++) {
2474 err
= mlxsw_sp_port_module_info_get(mlxsw_sp
, i
, &module
,
2477 goto err_port_module_info_get
;
2480 mlxsw_sp
->port_to_module
[i
] = module
;
2481 err
= mlxsw_sp_port_create(mlxsw_sp
, i
, false,
2482 module
, width
, lane
);
2484 goto err_port_create
;
2489 err_port_module_info_get
:
2490 for (i
--; i
>= 1; i
--)
2491 if (mlxsw_sp_port_created(mlxsw_sp
, i
))
2492 mlxsw_sp_port_remove(mlxsw_sp
, i
);
2493 kfree(mlxsw_sp
->ports
);
2497 static u8
mlxsw_sp_cluster_base_port_get(u8 local_port
)
2499 u8 offset
= (local_port
- 1) % MLXSW_SP_PORTS_PER_CLUSTER_MAX
;
2501 return local_port
- offset
;
2504 static int mlxsw_sp_port_split_create(struct mlxsw_sp
*mlxsw_sp
, u8 base_port
,
2505 u8 module
, unsigned int count
)
2507 u8 width
= MLXSW_PORT_MODULE_MAX_WIDTH
/ count
;
2510 for (i
= 0; i
< count
; i
++) {
2511 err
= mlxsw_sp_port_module_map(mlxsw_sp
, base_port
+ i
, module
,
2514 goto err_port_module_map
;
2517 for (i
= 0; i
< count
; i
++) {
2518 err
= __mlxsw_sp_port_swid_set(mlxsw_sp
, base_port
+ i
, 0);
2520 goto err_port_swid_set
;
2523 for (i
= 0; i
< count
; i
++) {
2524 err
= mlxsw_sp_port_create(mlxsw_sp
, base_port
+ i
, true,
2525 module
, width
, i
* width
);
2527 goto err_port_create
;
2533 for (i
--; i
>= 0; i
--)
2534 if (mlxsw_sp_port_created(mlxsw_sp
, base_port
+ i
))
2535 mlxsw_sp_port_remove(mlxsw_sp
, base_port
+ i
);
2538 for (i
--; i
>= 0; i
--)
2539 __mlxsw_sp_port_swid_set(mlxsw_sp
, base_port
+ i
,
2540 MLXSW_PORT_SWID_DISABLED_PORT
);
2542 err_port_module_map
:
2543 for (i
--; i
>= 0; i
--)
2544 mlxsw_sp_port_module_unmap(mlxsw_sp
, base_port
+ i
);
2548 static void mlxsw_sp_port_unsplit_create(struct mlxsw_sp
*mlxsw_sp
,
2549 u8 base_port
, unsigned int count
)
2551 u8 local_port
, module
, width
= MLXSW_PORT_MODULE_MAX_WIDTH
;
2554 /* Split by four means we need to re-create two ports, otherwise
2559 for (i
= 0; i
< count
; i
++) {
2560 local_port
= base_port
+ i
* 2;
2561 module
= mlxsw_sp
->port_to_module
[local_port
];
2563 mlxsw_sp_port_module_map(mlxsw_sp
, local_port
, module
, width
,
2567 for (i
= 0; i
< count
; i
++)
2568 __mlxsw_sp_port_swid_set(mlxsw_sp
, base_port
+ i
* 2, 0);
2570 for (i
= 0; i
< count
; i
++) {
2571 local_port
= base_port
+ i
* 2;
2572 module
= mlxsw_sp
->port_to_module
[local_port
];
2574 mlxsw_sp_port_create(mlxsw_sp
, local_port
, false, module
,
2579 static int mlxsw_sp_port_split(struct mlxsw_core
*mlxsw_core
, u8 local_port
,
2582 struct mlxsw_sp
*mlxsw_sp
= mlxsw_core_driver_priv(mlxsw_core
);
2583 struct mlxsw_sp_port
*mlxsw_sp_port
;
2584 u8 module
, cur_width
, base_port
;
2588 mlxsw_sp_port
= mlxsw_sp
->ports
[local_port
];
2589 if (!mlxsw_sp_port
) {
2590 dev_err(mlxsw_sp
->bus_info
->dev
, "Port number \"%d\" does not exist\n",
2595 module
= mlxsw_sp_port
->mapping
.module
;
2596 cur_width
= mlxsw_sp_port
->mapping
.width
;
2598 if (count
!= 2 && count
!= 4) {
2599 netdev_err(mlxsw_sp_port
->dev
, "Port can only be split into 2 or 4 ports\n");
2603 if (cur_width
!= MLXSW_PORT_MODULE_MAX_WIDTH
) {
2604 netdev_err(mlxsw_sp_port
->dev
, "Port cannot be split further\n");
2608 /* Make sure we have enough slave (even) ports for the split. */
2610 base_port
= local_port
;
2611 if (mlxsw_sp
->ports
[base_port
+ 1]) {
2612 netdev_err(mlxsw_sp_port
->dev
, "Invalid split configuration\n");
2616 base_port
= mlxsw_sp_cluster_base_port_get(local_port
);
2617 if (mlxsw_sp
->ports
[base_port
+ 1] ||
2618 mlxsw_sp
->ports
[base_port
+ 3]) {
2619 netdev_err(mlxsw_sp_port
->dev
, "Invalid split configuration\n");
2624 for (i
= 0; i
< count
; i
++)
2625 if (mlxsw_sp_port_created(mlxsw_sp
, base_port
+ i
))
2626 mlxsw_sp_port_remove(mlxsw_sp
, base_port
+ i
);
2628 err
= mlxsw_sp_port_split_create(mlxsw_sp
, base_port
, module
, count
);
2630 dev_err(mlxsw_sp
->bus_info
->dev
, "Failed to create split ports\n");
2631 goto err_port_split_create
;
2636 err_port_split_create
:
2637 mlxsw_sp_port_unsplit_create(mlxsw_sp
, base_port
, count
);
2641 static int mlxsw_sp_port_unsplit(struct mlxsw_core
*mlxsw_core
, u8 local_port
)
2643 struct mlxsw_sp
*mlxsw_sp
= mlxsw_core_driver_priv(mlxsw_core
);
2644 struct mlxsw_sp_port
*mlxsw_sp_port
;
2645 u8 cur_width
, base_port
;
2649 mlxsw_sp_port
= mlxsw_sp
->ports
[local_port
];
2650 if (!mlxsw_sp_port
) {
2651 dev_err(mlxsw_sp
->bus_info
->dev
, "Port number \"%d\" does not exist\n",
2656 if (!mlxsw_sp_port
->split
) {
2657 netdev_err(mlxsw_sp_port
->dev
, "Port wasn't split\n");
2661 cur_width
= mlxsw_sp_port
->mapping
.width
;
2662 count
= cur_width
== 1 ? 4 : 2;
2664 base_port
= mlxsw_sp_cluster_base_port_get(local_port
);
2666 /* Determine which ports to remove. */
2667 if (count
== 2 && local_port
>= base_port
+ 2)
2668 base_port
= base_port
+ 2;
2670 for (i
= 0; i
< count
; i
++)
2671 if (mlxsw_sp_port_created(mlxsw_sp
, base_port
+ i
))
2672 mlxsw_sp_port_remove(mlxsw_sp
, base_port
+ i
);
2674 mlxsw_sp_port_unsplit_create(mlxsw_sp
, base_port
, count
);
2679 static void mlxsw_sp_pude_event_func(const struct mlxsw_reg_info
*reg
,
2680 char *pude_pl
, void *priv
)
2682 struct mlxsw_sp
*mlxsw_sp
= priv
;
2683 struct mlxsw_sp_port
*mlxsw_sp_port
;
2684 enum mlxsw_reg_pude_oper_status status
;
2687 local_port
= mlxsw_reg_pude_local_port_get(pude_pl
);
2688 mlxsw_sp_port
= mlxsw_sp
->ports
[local_port
];
2692 status
= mlxsw_reg_pude_oper_status_get(pude_pl
);
2693 if (status
== MLXSW_PORT_OPER_STATUS_UP
) {
2694 netdev_info(mlxsw_sp_port
->dev
, "link up\n");
2695 netif_carrier_on(mlxsw_sp_port
->dev
);
2697 netdev_info(mlxsw_sp_port
->dev
, "link down\n");
2698 netif_carrier_off(mlxsw_sp_port
->dev
);
2702 static void mlxsw_sp_rx_listener_no_mark_func(struct sk_buff
*skb
,
2703 u8 local_port
, void *priv
)
2705 struct mlxsw_sp
*mlxsw_sp
= priv
;
2706 struct mlxsw_sp_port
*mlxsw_sp_port
= mlxsw_sp
->ports
[local_port
];
2707 struct mlxsw_sp_port_pcpu_stats
*pcpu_stats
;
2709 if (unlikely(!mlxsw_sp_port
)) {
2710 dev_warn_ratelimited(mlxsw_sp
->bus_info
->dev
, "Port %d: skb received for non-existent port\n",
2715 skb
->dev
= mlxsw_sp_port
->dev
;
2717 pcpu_stats
= this_cpu_ptr(mlxsw_sp_port
->pcpu_stats
);
2718 u64_stats_update_begin(&pcpu_stats
->syncp
);
2719 pcpu_stats
->rx_packets
++;
2720 pcpu_stats
->rx_bytes
+= skb
->len
;
2721 u64_stats_update_end(&pcpu_stats
->syncp
);
2723 skb
->protocol
= eth_type_trans(skb
, skb
->dev
);
2724 netif_receive_skb(skb
);
2727 static void mlxsw_sp_rx_listener_mark_func(struct sk_buff
*skb
, u8 local_port
,
2730 skb
->offload_fwd_mark
= 1;
2731 return mlxsw_sp_rx_listener_no_mark_func(skb
, local_port
, priv
);
2734 #define MLXSW_SP_RXL_NO_MARK(_trap_id, _action, _trap_group, _is_ctrl) \
2735 MLXSW_RXL(mlxsw_sp_rx_listener_no_mark_func, _trap_id, _action, \
2736 _is_ctrl, SP_##_trap_group, DISCARD)
2738 #define MLXSW_SP_RXL_MARK(_trap_id, _action, _trap_group, _is_ctrl) \
2739 MLXSW_RXL(mlxsw_sp_rx_listener_mark_func, _trap_id, _action, \
2740 _is_ctrl, SP_##_trap_group, DISCARD)
2742 #define MLXSW_SP_EVENTL(_func, _trap_id) \
2743 MLXSW_EVENTL(_func, _trap_id, SP_EVENT)
2745 static const struct mlxsw_listener mlxsw_sp_listener
[] = {
2747 MLXSW_SP_EVENTL(mlxsw_sp_pude_event_func
, PUDE
),
2749 MLXSW_SP_RXL_NO_MARK(STP
, TRAP_TO_CPU
, STP
, true),
2750 MLXSW_SP_RXL_NO_MARK(LACP
, TRAP_TO_CPU
, LACP
, true),
2751 MLXSW_SP_RXL_NO_MARK(LLDP
, TRAP_TO_CPU
, LLDP
, true),
2752 MLXSW_SP_RXL_MARK(DHCP
, MIRROR_TO_CPU
, DHCP
, false),
2753 MLXSW_SP_RXL_MARK(IGMP_QUERY
, MIRROR_TO_CPU
, IGMP
, false),
2754 MLXSW_SP_RXL_NO_MARK(IGMP_V1_REPORT
, TRAP_TO_CPU
, IGMP
, false),
2755 MLXSW_SP_RXL_NO_MARK(IGMP_V2_REPORT
, TRAP_TO_CPU
, IGMP
, false),
2756 MLXSW_SP_RXL_NO_MARK(IGMP_V2_LEAVE
, TRAP_TO_CPU
, IGMP
, false),
2757 MLXSW_SP_RXL_NO_MARK(IGMP_V3_REPORT
, TRAP_TO_CPU
, IGMP
, false),
2758 MLXSW_SP_RXL_MARK(ARPBC
, MIRROR_TO_CPU
, ARP
, false),
2759 MLXSW_SP_RXL_MARK(ARPUC
, MIRROR_TO_CPU
, ARP
, false),
2761 MLXSW_SP_RXL_NO_MARK(MTUERROR
, TRAP_TO_CPU
, ROUTER_EXP
, false),
2762 MLXSW_SP_RXL_NO_MARK(TTLERROR
, TRAP_TO_CPU
, ROUTER_EXP
, false),
2763 MLXSW_SP_RXL_NO_MARK(LBERROR
, TRAP_TO_CPU
, ROUTER_EXP
, false),
2764 MLXSW_SP_RXL_MARK(OSPF
, TRAP_TO_CPU
, OSPF
, false),
2765 MLXSW_SP_RXL_NO_MARK(IP2ME
, TRAP_TO_CPU
, IP2ME
, false),
2766 MLXSW_SP_RXL_NO_MARK(RTR_INGRESS0
, TRAP_TO_CPU
, REMOTE_ROUTE
, false),
2767 MLXSW_SP_RXL_NO_MARK(HOST_MISS_IPV4
, TRAP_TO_CPU
, ARP_MISS
, false),
2768 MLXSW_SP_RXL_NO_MARK(BGP_IPV4
, TRAP_TO_CPU
, BGP_IPV4
, false),
2771 static int mlxsw_sp_cpu_policers_set(struct mlxsw_core
*mlxsw_core
)
2773 char qpcr_pl
[MLXSW_REG_QPCR_LEN
];
2774 enum mlxsw_reg_qpcr_ir_units ir_units
;
2775 int max_cpu_policers
;
2781 if (!MLXSW_CORE_RES_VALID(mlxsw_core
, MAX_CPU_POLICERS
))
2784 max_cpu_policers
= MLXSW_CORE_RES_GET(mlxsw_core
, MAX_CPU_POLICERS
);
2786 ir_units
= MLXSW_REG_QPCR_IR_UNITS_M
;
2787 for (i
= 0; i
< max_cpu_policers
; i
++) {
2790 case MLXSW_REG_HTGT_TRAP_GROUP_SP_STP
:
2791 case MLXSW_REG_HTGT_TRAP_GROUP_SP_LACP
:
2792 case MLXSW_REG_HTGT_TRAP_GROUP_SP_LLDP
:
2793 case MLXSW_REG_HTGT_TRAP_GROUP_SP_OSPF
:
2797 case MLXSW_REG_HTGT_TRAP_GROUP_SP_IGMP
:
2801 case MLXSW_REG_HTGT_TRAP_GROUP_SP_BGP_IPV4
:
2802 case MLXSW_REG_HTGT_TRAP_GROUP_SP_ARP
:
2803 case MLXSW_REG_HTGT_TRAP_GROUP_SP_DHCP
:
2804 case MLXSW_REG_HTGT_TRAP_GROUP_SP_ARP_MISS
:
2805 case MLXSW_REG_HTGT_TRAP_GROUP_SP_ROUTER_EXP
:
2806 case MLXSW_REG_HTGT_TRAP_GROUP_SP_REMOTE_ROUTE
:
2810 case MLXSW_REG_HTGT_TRAP_GROUP_SP_IP2ME
:
2819 mlxsw_reg_qpcr_pack(qpcr_pl
, i
, ir_units
, is_bytes
, rate
,
2821 err
= mlxsw_reg_write(mlxsw_core
, MLXSW_REG(qpcr
), qpcr_pl
);
2829 static int mlxsw_sp_trap_groups_set(struct mlxsw_core
*mlxsw_core
)
2831 char htgt_pl
[MLXSW_REG_HTGT_LEN
];
2832 enum mlxsw_reg_htgt_trap_group i
;
2833 int max_cpu_policers
;
2834 int max_trap_groups
;
2839 if (!MLXSW_CORE_RES_VALID(mlxsw_core
, MAX_TRAP_GROUPS
))
2842 max_trap_groups
= MLXSW_CORE_RES_GET(mlxsw_core
, MAX_TRAP_GROUPS
);
2843 max_cpu_policers
= MLXSW_CORE_RES_GET(mlxsw_core
, MAX_CPU_POLICERS
);
2845 for (i
= 0; i
< max_trap_groups
; i
++) {
2848 case MLXSW_REG_HTGT_TRAP_GROUP_SP_STP
:
2849 case MLXSW_REG_HTGT_TRAP_GROUP_SP_LACP
:
2850 case MLXSW_REG_HTGT_TRAP_GROUP_SP_LLDP
:
2851 case MLXSW_REG_HTGT_TRAP_GROUP_SP_OSPF
:
2855 case MLXSW_REG_HTGT_TRAP_GROUP_SP_BGP_IPV4
:
2856 case MLXSW_REG_HTGT_TRAP_GROUP_SP_DHCP
:
2860 case MLXSW_REG_HTGT_TRAP_GROUP_SP_IGMP
:
2861 case MLXSW_REG_HTGT_TRAP_GROUP_SP_IP2ME
:
2865 case MLXSW_REG_HTGT_TRAP_GROUP_SP_ARP
:
2869 case MLXSW_REG_HTGT_TRAP_GROUP_SP_ARP_MISS
:
2870 case MLXSW_REG_HTGT_TRAP_GROUP_SP_ROUTER_EXP
:
2871 case MLXSW_REG_HTGT_TRAP_GROUP_SP_REMOTE_ROUTE
:
2875 case MLXSW_REG_HTGT_TRAP_GROUP_SP_EVENT
:
2876 priority
= MLXSW_REG_HTGT_DEFAULT_PRIORITY
;
2877 tc
= MLXSW_REG_HTGT_DEFAULT_TC
;
2878 policer_id
= MLXSW_REG_HTGT_INVALID_POLICER
;
2884 if (max_cpu_policers
<= policer_id
&&
2885 policer_id
!= MLXSW_REG_HTGT_INVALID_POLICER
)
2888 mlxsw_reg_htgt_pack(htgt_pl
, i
, policer_id
, priority
, tc
);
2889 err
= mlxsw_reg_write(mlxsw_core
, MLXSW_REG(htgt
), htgt_pl
);
2897 static int mlxsw_sp_traps_init(struct mlxsw_sp
*mlxsw_sp
)
2902 err
= mlxsw_sp_cpu_policers_set(mlxsw_sp
->core
);
2906 err
= mlxsw_sp_trap_groups_set(mlxsw_sp
->core
);
2910 for (i
= 0; i
< ARRAY_SIZE(mlxsw_sp_listener
); i
++) {
2911 err
= mlxsw_core_trap_register(mlxsw_sp
->core
,
2912 &mlxsw_sp_listener
[i
],
2915 goto err_listener_register
;
2920 err_listener_register
:
2921 for (i
--; i
>= 0; i
--) {
2922 mlxsw_core_trap_unregister(mlxsw_sp
->core
,
2923 &mlxsw_sp_listener
[i
],
2929 static void mlxsw_sp_traps_fini(struct mlxsw_sp
*mlxsw_sp
)
2933 for (i
= 0; i
< ARRAY_SIZE(mlxsw_sp_listener
); i
++) {
2934 mlxsw_core_trap_unregister(mlxsw_sp
->core
,
2935 &mlxsw_sp_listener
[i
],
2940 static int __mlxsw_sp_flood_init(struct mlxsw_core
*mlxsw_core
,
2941 enum mlxsw_reg_sfgc_type type
,
2942 enum mlxsw_reg_sfgc_bridge_type bridge_type
)
2944 enum mlxsw_flood_table_type table_type
;
2945 enum mlxsw_sp_flood_table flood_table
;
2946 char sfgc_pl
[MLXSW_REG_SFGC_LEN
];
2948 if (bridge_type
== MLXSW_REG_SFGC_BRIDGE_TYPE_VFID
)
2949 table_type
= MLXSW_REG_SFGC_TABLE_TYPE_FID
;
2951 table_type
= MLXSW_REG_SFGC_TABLE_TYPE_FID_OFFEST
;
2953 if (type
== MLXSW_REG_SFGC_TYPE_UNKNOWN_UNICAST
)
2954 flood_table
= MLXSW_SP_FLOOD_TABLE_UC
;
2956 flood_table
= MLXSW_SP_FLOOD_TABLE_BM
;
2958 mlxsw_reg_sfgc_pack(sfgc_pl
, type
, bridge_type
, table_type
,
2960 return mlxsw_reg_write(mlxsw_core
, MLXSW_REG(sfgc
), sfgc_pl
);
2963 static int mlxsw_sp_flood_init(struct mlxsw_sp
*mlxsw_sp
)
2967 for (type
= 0; type
< MLXSW_REG_SFGC_TYPE_MAX
; type
++) {
2968 if (type
== MLXSW_REG_SFGC_TYPE_RESERVED
)
2971 err
= __mlxsw_sp_flood_init(mlxsw_sp
->core
, type
,
2972 MLXSW_REG_SFGC_BRIDGE_TYPE_VFID
);
2976 err
= __mlxsw_sp_flood_init(mlxsw_sp
->core
, type
,
2977 MLXSW_REG_SFGC_BRIDGE_TYPE_1Q_FID
);
2985 static int mlxsw_sp_lag_init(struct mlxsw_sp
*mlxsw_sp
)
2987 char slcr_pl
[MLXSW_REG_SLCR_LEN
];
2990 mlxsw_reg_slcr_pack(slcr_pl
, MLXSW_REG_SLCR_LAG_HASH_SMAC
|
2991 MLXSW_REG_SLCR_LAG_HASH_DMAC
|
2992 MLXSW_REG_SLCR_LAG_HASH_ETHERTYPE
|
2993 MLXSW_REG_SLCR_LAG_HASH_VLANID
|
2994 MLXSW_REG_SLCR_LAG_HASH_SIP
|
2995 MLXSW_REG_SLCR_LAG_HASH_DIP
|
2996 MLXSW_REG_SLCR_LAG_HASH_SPORT
|
2997 MLXSW_REG_SLCR_LAG_HASH_DPORT
|
2998 MLXSW_REG_SLCR_LAG_HASH_IPPROTO
);
2999 err
= mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(slcr
), slcr_pl
);
3003 if (!MLXSW_CORE_RES_VALID(mlxsw_sp
->core
, MAX_LAG
) ||
3004 !MLXSW_CORE_RES_VALID(mlxsw_sp
->core
, MAX_LAG_MEMBERS
))
3007 mlxsw_sp
->lags
= kcalloc(MLXSW_CORE_RES_GET(mlxsw_sp
->core
, MAX_LAG
),
3008 sizeof(struct mlxsw_sp_upper
),
3010 if (!mlxsw_sp
->lags
)
3016 static void mlxsw_sp_lag_fini(struct mlxsw_sp
*mlxsw_sp
)
3018 kfree(mlxsw_sp
->lags
);
3021 static int mlxsw_sp_basic_trap_groups_set(struct mlxsw_core
*mlxsw_core
)
3023 char htgt_pl
[MLXSW_REG_HTGT_LEN
];
3025 mlxsw_reg_htgt_pack(htgt_pl
, MLXSW_REG_HTGT_TRAP_GROUP_EMAD
,
3026 MLXSW_REG_HTGT_INVALID_POLICER
,
3027 MLXSW_REG_HTGT_DEFAULT_PRIORITY
,
3028 MLXSW_REG_HTGT_DEFAULT_TC
);
3029 return mlxsw_reg_write(mlxsw_core
, MLXSW_REG(htgt
), htgt_pl
);
3032 static int mlxsw_sp_init(struct mlxsw_core
*mlxsw_core
,
3033 const struct mlxsw_bus_info
*mlxsw_bus_info
)
3035 struct mlxsw_sp
*mlxsw_sp
= mlxsw_core_driver_priv(mlxsw_core
);
3038 mlxsw_sp
->core
= mlxsw_core
;
3039 mlxsw_sp
->bus_info
= mlxsw_bus_info
;
3040 INIT_LIST_HEAD(&mlxsw_sp
->fids
);
3041 INIT_LIST_HEAD(&mlxsw_sp
->vfids
.list
);
3042 INIT_LIST_HEAD(&mlxsw_sp
->br_mids
.list
);
3044 err
= mlxsw_sp_base_mac_get(mlxsw_sp
);
3046 dev_err(mlxsw_sp
->bus_info
->dev
, "Failed to get base mac\n");
3050 err
= mlxsw_sp_traps_init(mlxsw_sp
);
3052 dev_err(mlxsw_sp
->bus_info
->dev
, "Failed to set traps\n");
3056 err
= mlxsw_sp_flood_init(mlxsw_sp
);
3058 dev_err(mlxsw_sp
->bus_info
->dev
, "Failed to initialize flood tables\n");
3059 goto err_flood_init
;
3062 err
= mlxsw_sp_buffers_init(mlxsw_sp
);
3064 dev_err(mlxsw_sp
->bus_info
->dev
, "Failed to initialize buffers\n");
3065 goto err_buffers_init
;
3068 err
= mlxsw_sp_lag_init(mlxsw_sp
);
3070 dev_err(mlxsw_sp
->bus_info
->dev
, "Failed to initialize LAG\n");
3074 err
= mlxsw_sp_switchdev_init(mlxsw_sp
);
3076 dev_err(mlxsw_sp
->bus_info
->dev
, "Failed to initialize switchdev\n");
3077 goto err_switchdev_init
;
3080 err
= mlxsw_sp_router_init(mlxsw_sp
);
3082 dev_err(mlxsw_sp
->bus_info
->dev
, "Failed to initialize router\n");
3083 goto err_router_init
;
3086 err
= mlxsw_sp_span_init(mlxsw_sp
);
3088 dev_err(mlxsw_sp
->bus_info
->dev
, "Failed to init span system\n");
3092 err
= mlxsw_sp_ports_create(mlxsw_sp
);
3094 dev_err(mlxsw_sp
->bus_info
->dev
, "Failed to create ports\n");
3095 goto err_ports_create
;
3101 mlxsw_sp_span_fini(mlxsw_sp
);
3103 mlxsw_sp_router_fini(mlxsw_sp
);
3105 mlxsw_sp_switchdev_fini(mlxsw_sp
);
3107 mlxsw_sp_lag_fini(mlxsw_sp
);
3109 mlxsw_sp_buffers_fini(mlxsw_sp
);
3112 mlxsw_sp_traps_fini(mlxsw_sp
);
3116 static void mlxsw_sp_fini(struct mlxsw_core
*mlxsw_core
)
3118 struct mlxsw_sp
*mlxsw_sp
= mlxsw_core_driver_priv(mlxsw_core
);
3120 mlxsw_sp_ports_remove(mlxsw_sp
);
3121 mlxsw_sp_span_fini(mlxsw_sp
);
3122 mlxsw_sp_router_fini(mlxsw_sp
);
3123 mlxsw_sp_switchdev_fini(mlxsw_sp
);
3124 mlxsw_sp_lag_fini(mlxsw_sp
);
3125 mlxsw_sp_buffers_fini(mlxsw_sp
);
3126 mlxsw_sp_traps_fini(mlxsw_sp
);
3127 WARN_ON(!list_empty(&mlxsw_sp
->vfids
.list
));
3128 WARN_ON(!list_empty(&mlxsw_sp
->fids
));
3131 static struct mlxsw_config_profile mlxsw_sp_config_profile
= {
3132 .used_max_vepa_channels
= 1,
3133 .max_vepa_channels
= 0,
3135 .max_mid
= MLXSW_SP_MID_MAX
,
3138 .used_flood_tables
= 1,
3139 .used_flood_mode
= 1,
3141 .max_fid_offset_flood_tables
= 2,
3142 .fid_offset_flood_table_size
= VLAN_N_VID
- 1,
3143 .max_fid_flood_tables
= 2,
3144 .fid_flood_table_size
= MLXSW_SP_VFID_MAX
,
3145 .used_max_ib_mc
= 1,
3149 .used_kvd_split_data
= 1,
3150 .kvd_hash_granularity
= MLXSW_SP_KVD_GRANULARITY
,
3151 .kvd_hash_single_parts
= 2,
3152 .kvd_hash_double_parts
= 1,
3153 .kvd_linear_size
= MLXSW_SP_KVD_LINEAR_SIZE
,
3157 .type
= MLXSW_PORT_SWID_TYPE_ETH
,
3160 .resource_query_enable
= 1,
3163 static struct mlxsw_driver mlxsw_sp_driver
= {
3164 .kind
= mlxsw_sp_driver_name
,
3165 .priv_size
= sizeof(struct mlxsw_sp
),
3166 .init
= mlxsw_sp_init
,
3167 .fini
= mlxsw_sp_fini
,
3168 .basic_trap_groups_set
= mlxsw_sp_basic_trap_groups_set
,
3169 .port_split
= mlxsw_sp_port_split
,
3170 .port_unsplit
= mlxsw_sp_port_unsplit
,
3171 .sb_pool_get
= mlxsw_sp_sb_pool_get
,
3172 .sb_pool_set
= mlxsw_sp_sb_pool_set
,
3173 .sb_port_pool_get
= mlxsw_sp_sb_port_pool_get
,
3174 .sb_port_pool_set
= mlxsw_sp_sb_port_pool_set
,
3175 .sb_tc_pool_bind_get
= mlxsw_sp_sb_tc_pool_bind_get
,
3176 .sb_tc_pool_bind_set
= mlxsw_sp_sb_tc_pool_bind_set
,
3177 .sb_occ_snapshot
= mlxsw_sp_sb_occ_snapshot
,
3178 .sb_occ_max_clear
= mlxsw_sp_sb_occ_max_clear
,
3179 .sb_occ_port_pool_get
= mlxsw_sp_sb_occ_port_pool_get
,
3180 .sb_occ_tc_port_bind_get
= mlxsw_sp_sb_occ_tc_port_bind_get
,
3181 .txhdr_construct
= mlxsw_sp_txhdr_construct
,
3182 .txhdr_len
= MLXSW_TXHDR_LEN
,
3183 .profile
= &mlxsw_sp_config_profile
,
3186 static bool mlxsw_sp_port_dev_check(const struct net_device
*dev
)
3188 return dev
->netdev_ops
== &mlxsw_sp_port_netdev_ops
;
3191 static int mlxsw_lower_dev_walk(struct net_device
*lower_dev
, void *data
)
3193 struct mlxsw_sp_port
**port
= data
;
3196 if (mlxsw_sp_port_dev_check(lower_dev
)) {
3197 *port
= netdev_priv(lower_dev
);
3204 static struct mlxsw_sp_port
*mlxsw_sp_port_dev_lower_find(struct net_device
*dev
)
3206 struct mlxsw_sp_port
*port
;
3208 if (mlxsw_sp_port_dev_check(dev
))
3209 return netdev_priv(dev
);
3212 netdev_walk_all_lower_dev(dev
, mlxsw_lower_dev_walk
, &port
);
3217 static struct mlxsw_sp
*mlxsw_sp_lower_get(struct net_device
*dev
)
3219 struct mlxsw_sp_port
*mlxsw_sp_port
;
3221 mlxsw_sp_port
= mlxsw_sp_port_dev_lower_find(dev
);
3222 return mlxsw_sp_port
? mlxsw_sp_port
->mlxsw_sp
: NULL
;
3225 static struct mlxsw_sp_port
*mlxsw_sp_port_dev_lower_find_rcu(struct net_device
*dev
)
3227 struct mlxsw_sp_port
*port
;
3229 if (mlxsw_sp_port_dev_check(dev
))
3230 return netdev_priv(dev
);
3233 netdev_walk_all_lower_dev_rcu(dev
, mlxsw_lower_dev_walk
, &port
);
3238 struct mlxsw_sp_port
*mlxsw_sp_port_lower_dev_hold(struct net_device
*dev
)
3240 struct mlxsw_sp_port
*mlxsw_sp_port
;
3243 mlxsw_sp_port
= mlxsw_sp_port_dev_lower_find_rcu(dev
);
3245 dev_hold(mlxsw_sp_port
->dev
);
3247 return mlxsw_sp_port
;
3250 void mlxsw_sp_port_dev_put(struct mlxsw_sp_port
*mlxsw_sp_port
)
3252 dev_put(mlxsw_sp_port
->dev
);
3255 static bool mlxsw_sp_rif_should_config(struct mlxsw_sp_rif
*r
,
3256 unsigned long event
)
3265 if (r
&& --r
->ref_count
== 0)
3267 /* It is possible we already removed the RIF ourselves
3268 * if it was assigned to a netdev that is now a bridge
3277 static int mlxsw_sp_avail_rif_get(struct mlxsw_sp
*mlxsw_sp
)
3281 for (i
= 0; i
< MLXSW_CORE_RES_GET(mlxsw_sp
->core
, MAX_RIFS
); i
++)
3282 if (!mlxsw_sp
->rifs
[i
])
3285 return MLXSW_SP_INVALID_RIF
;
3288 static void mlxsw_sp_vport_rif_sp_attr_get(struct mlxsw_sp_port
*mlxsw_sp_vport
,
3289 bool *p_lagged
, u16
*p_system_port
)
3291 u8 local_port
= mlxsw_sp_vport
->local_port
;
3293 *p_lagged
= mlxsw_sp_vport
->lagged
;
3294 *p_system_port
= *p_lagged
? mlxsw_sp_vport
->lag_id
: local_port
;
3297 static int mlxsw_sp_vport_rif_sp_op(struct mlxsw_sp_port
*mlxsw_sp_vport
,
3298 struct net_device
*l3_dev
, u16 rif
,
3301 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_vport
->mlxsw_sp
;
3302 bool lagged
= mlxsw_sp_vport
->lagged
;
3303 char ritr_pl
[MLXSW_REG_RITR_LEN
];
3306 mlxsw_reg_ritr_pack(ritr_pl
, create
, MLXSW_REG_RITR_SP_IF
, rif
,
3307 l3_dev
->mtu
, l3_dev
->dev_addr
);
3309 mlxsw_sp_vport_rif_sp_attr_get(mlxsw_sp_vport
, &lagged
, &system_port
);
3310 mlxsw_reg_ritr_sp_if_pack(ritr_pl
, lagged
, system_port
,
3311 mlxsw_sp_vport_vid_get(mlxsw_sp_vport
));
3313 return mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(ritr
), ritr_pl
);
3316 static void mlxsw_sp_vport_rif_sp_leave(struct mlxsw_sp_port
*mlxsw_sp_vport
);
3318 static struct mlxsw_sp_fid
*
3319 mlxsw_sp_rfid_alloc(u16 fid
, struct net_device
*l3_dev
)
3321 struct mlxsw_sp_fid
*f
;
3323 f
= kzalloc(sizeof(*f
), GFP_KERNEL
);
3327 f
->leave
= mlxsw_sp_vport_rif_sp_leave
;
3335 static struct mlxsw_sp_rif
*
3336 mlxsw_sp_rif_alloc(u16 rif
, struct net_device
*l3_dev
, struct mlxsw_sp_fid
*f
)
3338 struct mlxsw_sp_rif
*r
;
3340 r
= kzalloc(sizeof(*r
), GFP_KERNEL
);
3344 ether_addr_copy(r
->addr
, l3_dev
->dev_addr
);
3345 r
->mtu
= l3_dev
->mtu
;
3354 static struct mlxsw_sp_rif
*
3355 mlxsw_sp_vport_rif_sp_create(struct mlxsw_sp_port
*mlxsw_sp_vport
,
3356 struct net_device
*l3_dev
)
3358 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_vport
->mlxsw_sp
;
3359 struct mlxsw_sp_fid
*f
;
3360 struct mlxsw_sp_rif
*r
;
3364 rif
= mlxsw_sp_avail_rif_get(mlxsw_sp
);
3365 if (rif
== MLXSW_SP_INVALID_RIF
)
3366 return ERR_PTR(-ERANGE
);
3368 err
= mlxsw_sp_vport_rif_sp_op(mlxsw_sp_vport
, l3_dev
, rif
, true);
3370 return ERR_PTR(err
);
3372 fid
= mlxsw_sp_rif_sp_to_fid(rif
);
3373 err
= mlxsw_sp_rif_fdb_op(mlxsw_sp
, l3_dev
->dev_addr
, fid
, true);
3375 goto err_rif_fdb_op
;
3377 f
= mlxsw_sp_rfid_alloc(fid
, l3_dev
);
3380 goto err_rfid_alloc
;
3383 r
= mlxsw_sp_rif_alloc(rif
, l3_dev
, f
);
3390 mlxsw_sp
->rifs
[rif
] = r
;
3397 mlxsw_sp_rif_fdb_op(mlxsw_sp
, l3_dev
->dev_addr
, fid
, false);
3399 mlxsw_sp_vport_rif_sp_op(mlxsw_sp_vport
, l3_dev
, rif
, false);
3400 return ERR_PTR(err
);
3403 static void mlxsw_sp_vport_rif_sp_destroy(struct mlxsw_sp_port
*mlxsw_sp_vport
,
3404 struct mlxsw_sp_rif
*r
)
3406 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_vport
->mlxsw_sp
;
3407 struct net_device
*l3_dev
= r
->dev
;
3408 struct mlxsw_sp_fid
*f
= r
->f
;
3412 mlxsw_sp
->rifs
[rif
] = NULL
;
3419 mlxsw_sp_rif_fdb_op(mlxsw_sp
, l3_dev
->dev_addr
, fid
, false);
3421 mlxsw_sp_vport_rif_sp_op(mlxsw_sp_vport
, l3_dev
, rif
, false);
3424 static int mlxsw_sp_vport_rif_sp_join(struct mlxsw_sp_port
*mlxsw_sp_vport
,
3425 struct net_device
*l3_dev
)
3427 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_vport
->mlxsw_sp
;
3428 struct mlxsw_sp_rif
*r
;
3430 r
= mlxsw_sp_rif_find_by_dev(mlxsw_sp
, l3_dev
);
3432 r
= mlxsw_sp_vport_rif_sp_create(mlxsw_sp_vport
, l3_dev
);
3437 mlxsw_sp_vport_fid_set(mlxsw_sp_vport
, r
->f
);
3440 netdev_dbg(mlxsw_sp_vport
->dev
, "Joined FID=%d\n", r
->f
->fid
);
3445 static void mlxsw_sp_vport_rif_sp_leave(struct mlxsw_sp_port
*mlxsw_sp_vport
)
3447 struct mlxsw_sp_fid
*f
= mlxsw_sp_vport_fid_get(mlxsw_sp_vport
);
3449 netdev_dbg(mlxsw_sp_vport
->dev
, "Left FID=%d\n", f
->fid
);
3451 mlxsw_sp_vport_fid_set(mlxsw_sp_vport
, NULL
);
3452 if (--f
->ref_count
== 0)
3453 mlxsw_sp_vport_rif_sp_destroy(mlxsw_sp_vport
, f
->r
);
3456 static int mlxsw_sp_inetaddr_vport_event(struct net_device
*l3_dev
,
3457 struct net_device
*port_dev
,
3458 unsigned long event
, u16 vid
)
3460 struct mlxsw_sp_port
*mlxsw_sp_port
= netdev_priv(port_dev
);
3461 struct mlxsw_sp_port
*mlxsw_sp_vport
;
3463 mlxsw_sp_vport
= mlxsw_sp_port_vport_find(mlxsw_sp_port
, vid
);
3464 if (WARN_ON(!mlxsw_sp_vport
))
3469 return mlxsw_sp_vport_rif_sp_join(mlxsw_sp_vport
, l3_dev
);
3471 mlxsw_sp_vport_rif_sp_leave(mlxsw_sp_vport
);
3478 static int mlxsw_sp_inetaddr_port_event(struct net_device
*port_dev
,
3479 unsigned long event
)
3481 if (netif_is_bridge_port(port_dev
) || netif_is_lag_port(port_dev
))
3484 return mlxsw_sp_inetaddr_vport_event(port_dev
, port_dev
, event
, 1);
3487 static int __mlxsw_sp_inetaddr_lag_event(struct net_device
*l3_dev
,
3488 struct net_device
*lag_dev
,
3489 unsigned long event
, u16 vid
)
3491 struct net_device
*port_dev
;
3492 struct list_head
*iter
;
3495 netdev_for_each_lower_dev(lag_dev
, port_dev
, iter
) {
3496 if (mlxsw_sp_port_dev_check(port_dev
)) {
3497 err
= mlxsw_sp_inetaddr_vport_event(l3_dev
, port_dev
,
3507 static int mlxsw_sp_inetaddr_lag_event(struct net_device
*lag_dev
,
3508 unsigned long event
)
3510 if (netif_is_bridge_port(lag_dev
))
3513 return __mlxsw_sp_inetaddr_lag_event(lag_dev
, lag_dev
, event
, 1);
3516 static struct mlxsw_sp_fid
*mlxsw_sp_bridge_fid_get(struct mlxsw_sp
*mlxsw_sp
,
3517 struct net_device
*l3_dev
)
3521 if (is_vlan_dev(l3_dev
))
3522 fid
= vlan_dev_vlan_id(l3_dev
);
3523 else if (mlxsw_sp
->master_bridge
.dev
== l3_dev
)
3526 return mlxsw_sp_vfid_find(mlxsw_sp
, l3_dev
);
3528 return mlxsw_sp_fid_find(mlxsw_sp
, fid
);
3531 static enum mlxsw_flood_table_type
mlxsw_sp_flood_table_type_get(u16 fid
)
3533 return mlxsw_sp_fid_is_vfid(fid
) ? MLXSW_REG_SFGC_TABLE_TYPE_FID
:
3534 MLXSW_REG_SFGC_TABLE_TYPE_FID_OFFEST
;
3537 static u16
mlxsw_sp_flood_table_index_get(u16 fid
)
3539 return mlxsw_sp_fid_is_vfid(fid
) ? mlxsw_sp_fid_to_vfid(fid
) : fid
;
3542 static int mlxsw_sp_router_port_flood_set(struct mlxsw_sp
*mlxsw_sp
, u16 fid
,
3545 enum mlxsw_flood_table_type table_type
;
3550 sftr_pl
= kmalloc(MLXSW_REG_SFTR_LEN
, GFP_KERNEL
);
3554 table_type
= mlxsw_sp_flood_table_type_get(fid
);
3555 index
= mlxsw_sp_flood_table_index_get(fid
);
3556 mlxsw_reg_sftr_pack(sftr_pl
, MLXSW_SP_FLOOD_TABLE_BM
, index
, table_type
,
3557 1, MLXSW_PORT_ROUTER_PORT
, set
);
3558 err
= mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(sftr
), sftr_pl
);
3564 static enum mlxsw_reg_ritr_if_type
mlxsw_sp_rif_type_get(u16 fid
)
3566 if (mlxsw_sp_fid_is_vfid(fid
))
3567 return MLXSW_REG_RITR_FID_IF
;
3569 return MLXSW_REG_RITR_VLAN_IF
;
3572 static int mlxsw_sp_rif_bridge_op(struct mlxsw_sp
*mlxsw_sp
,
3573 struct net_device
*l3_dev
,
3577 enum mlxsw_reg_ritr_if_type rif_type
;
3578 char ritr_pl
[MLXSW_REG_RITR_LEN
];
3580 rif_type
= mlxsw_sp_rif_type_get(fid
);
3581 mlxsw_reg_ritr_pack(ritr_pl
, create
, rif_type
, rif
, l3_dev
->mtu
,
3583 mlxsw_reg_ritr_fid_set(ritr_pl
, rif_type
, fid
);
3585 return mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(ritr
), ritr_pl
);
3588 static int mlxsw_sp_rif_bridge_create(struct mlxsw_sp
*mlxsw_sp
,
3589 struct net_device
*l3_dev
,
3590 struct mlxsw_sp_fid
*f
)
3592 struct mlxsw_sp_rif
*r
;
3596 rif
= mlxsw_sp_avail_rif_get(mlxsw_sp
);
3597 if (rif
== MLXSW_SP_INVALID_RIF
)
3600 err
= mlxsw_sp_router_port_flood_set(mlxsw_sp
, f
->fid
, true);
3604 err
= mlxsw_sp_rif_bridge_op(mlxsw_sp
, l3_dev
, f
->fid
, rif
, true);
3606 goto err_rif_bridge_op
;
3608 err
= mlxsw_sp_rif_fdb_op(mlxsw_sp
, l3_dev
->dev_addr
, f
->fid
, true);
3610 goto err_rif_fdb_op
;
3612 r
= mlxsw_sp_rif_alloc(rif
, l3_dev
, f
);
3619 mlxsw_sp
->rifs
[rif
] = r
;
3621 netdev_dbg(l3_dev
, "RIF=%d created\n", rif
);
3626 mlxsw_sp_rif_fdb_op(mlxsw_sp
, l3_dev
->dev_addr
, f
->fid
, false);
3628 mlxsw_sp_rif_bridge_op(mlxsw_sp
, l3_dev
, f
->fid
, rif
, false);
3630 mlxsw_sp_router_port_flood_set(mlxsw_sp
, f
->fid
, false);
3634 void mlxsw_sp_rif_bridge_destroy(struct mlxsw_sp
*mlxsw_sp
,
3635 struct mlxsw_sp_rif
*r
)
3637 struct net_device
*l3_dev
= r
->dev
;
3638 struct mlxsw_sp_fid
*f
= r
->f
;
3641 mlxsw_sp
->rifs
[rif
] = NULL
;
3646 mlxsw_sp_rif_fdb_op(mlxsw_sp
, l3_dev
->dev_addr
, f
->fid
, false);
3648 mlxsw_sp_rif_bridge_op(mlxsw_sp
, l3_dev
, f
->fid
, rif
, false);
3650 mlxsw_sp_router_port_flood_set(mlxsw_sp
, f
->fid
, false);
3652 netdev_dbg(l3_dev
, "RIF=%d destroyed\n", rif
);
3655 static int mlxsw_sp_inetaddr_bridge_event(struct net_device
*l3_dev
,
3656 struct net_device
*br_dev
,
3657 unsigned long event
)
3659 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_lower_get(l3_dev
);
3660 struct mlxsw_sp_fid
*f
;
3662 /* FID can either be an actual FID if the L3 device is the
3663 * VLAN-aware bridge or a VLAN device on top. Otherwise, the
3664 * L3 device is a VLAN-unaware bridge and we get a vFID.
3666 f
= mlxsw_sp_bridge_fid_get(mlxsw_sp
, l3_dev
);
3672 return mlxsw_sp_rif_bridge_create(mlxsw_sp
, l3_dev
, f
);
3674 mlxsw_sp_rif_bridge_destroy(mlxsw_sp
, f
->r
);
3681 static int mlxsw_sp_inetaddr_vlan_event(struct net_device
*vlan_dev
,
3682 unsigned long event
)
3684 struct net_device
*real_dev
= vlan_dev_real_dev(vlan_dev
);
3685 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_lower_get(vlan_dev
);
3686 u16 vid
= vlan_dev_vlan_id(vlan_dev
);
3688 if (mlxsw_sp_port_dev_check(real_dev
))
3689 return mlxsw_sp_inetaddr_vport_event(vlan_dev
, real_dev
, event
,
3691 else if (netif_is_lag_master(real_dev
))
3692 return __mlxsw_sp_inetaddr_lag_event(vlan_dev
, real_dev
, event
,
3694 else if (netif_is_bridge_master(real_dev
) &&
3695 mlxsw_sp
->master_bridge
.dev
== real_dev
)
3696 return mlxsw_sp_inetaddr_bridge_event(vlan_dev
, real_dev
,
3702 static int mlxsw_sp_inetaddr_event(struct notifier_block
*unused
,
3703 unsigned long event
, void *ptr
)
3705 struct in_ifaddr
*ifa
= (struct in_ifaddr
*) ptr
;
3706 struct net_device
*dev
= ifa
->ifa_dev
->dev
;
3707 struct mlxsw_sp
*mlxsw_sp
;
3708 struct mlxsw_sp_rif
*r
;
3711 mlxsw_sp
= mlxsw_sp_lower_get(dev
);
3715 r
= mlxsw_sp_rif_find_by_dev(mlxsw_sp
, dev
);
3716 if (!mlxsw_sp_rif_should_config(r
, event
))
3719 if (mlxsw_sp_port_dev_check(dev
))
3720 err
= mlxsw_sp_inetaddr_port_event(dev
, event
);
3721 else if (netif_is_lag_master(dev
))
3722 err
= mlxsw_sp_inetaddr_lag_event(dev
, event
);
3723 else if (netif_is_bridge_master(dev
))
3724 err
= mlxsw_sp_inetaddr_bridge_event(dev
, dev
, event
);
3725 else if (is_vlan_dev(dev
))
3726 err
= mlxsw_sp_inetaddr_vlan_event(dev
, event
);
3729 return notifier_from_errno(err
);
3732 static int mlxsw_sp_rif_edit(struct mlxsw_sp
*mlxsw_sp
, u16 rif
,
3733 const char *mac
, int mtu
)
3735 char ritr_pl
[MLXSW_REG_RITR_LEN
];
3738 mlxsw_reg_ritr_rif_pack(ritr_pl
, rif
);
3739 err
= mlxsw_reg_query(mlxsw_sp
->core
, MLXSW_REG(ritr
), ritr_pl
);
3743 mlxsw_reg_ritr_mtu_set(ritr_pl
, mtu
);
3744 mlxsw_reg_ritr_if_mac_memcpy_to(ritr_pl
, mac
);
3745 mlxsw_reg_ritr_op_set(ritr_pl
, MLXSW_REG_RITR_RIF_CREATE
);
3746 return mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(ritr
), ritr_pl
);
3749 static int mlxsw_sp_netdevice_router_port_event(struct net_device
*dev
)
3751 struct mlxsw_sp
*mlxsw_sp
;
3752 struct mlxsw_sp_rif
*r
;
3755 mlxsw_sp
= mlxsw_sp_lower_get(dev
);
3759 r
= mlxsw_sp_rif_find_by_dev(mlxsw_sp
, dev
);
3763 err
= mlxsw_sp_rif_fdb_op(mlxsw_sp
, r
->addr
, r
->f
->fid
, false);
3767 err
= mlxsw_sp_rif_edit(mlxsw_sp
, r
->rif
, dev
->dev_addr
, dev
->mtu
);
3771 err
= mlxsw_sp_rif_fdb_op(mlxsw_sp
, dev
->dev_addr
, r
->f
->fid
, true);
3773 goto err_rif_fdb_op
;
3775 ether_addr_copy(r
->addr
, dev
->dev_addr
);
3778 netdev_dbg(dev
, "Updated RIF=%d\n", r
->rif
);
3783 mlxsw_sp_rif_edit(mlxsw_sp
, r
->rif
, r
->addr
, r
->mtu
);
3785 mlxsw_sp_rif_fdb_op(mlxsw_sp
, r
->addr
, r
->f
->fid
, true);
3789 static bool mlxsw_sp_lag_port_fid_member(struct mlxsw_sp_port
*lag_port
,
3792 if (mlxsw_sp_fid_is_vfid(fid
))
3793 return mlxsw_sp_port_vport_find_by_fid(lag_port
, fid
);
3795 return test_bit(fid
, lag_port
->active_vlans
);
3798 static bool mlxsw_sp_port_fdb_should_flush(struct mlxsw_sp_port
*mlxsw_sp_port
,
3801 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
3802 u8 local_port
= mlxsw_sp_port
->local_port
;
3803 u16 lag_id
= mlxsw_sp_port
->lag_id
;
3804 u64 max_lag_members
;
3807 if (!mlxsw_sp_port
->lagged
)
3810 max_lag_members
= MLXSW_CORE_RES_GET(mlxsw_sp
->core
,
3812 for (i
= 0; i
< max_lag_members
; i
++) {
3813 struct mlxsw_sp_port
*lag_port
;
3815 lag_port
= mlxsw_sp_port_lagged_get(mlxsw_sp
, lag_id
, i
);
3816 if (!lag_port
|| lag_port
->local_port
== local_port
)
3818 if (mlxsw_sp_lag_port_fid_member(lag_port
, fid
))
3826 mlxsw_sp_port_fdb_flush_by_port_fid(const struct mlxsw_sp_port
*mlxsw_sp_port
,
3829 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
3830 char sfdf_pl
[MLXSW_REG_SFDF_LEN
];
3832 mlxsw_reg_sfdf_pack(sfdf_pl
, MLXSW_REG_SFDF_FLUSH_PER_PORT_AND_FID
);
3833 mlxsw_reg_sfdf_fid_set(sfdf_pl
, fid
);
3834 mlxsw_reg_sfdf_port_fid_system_port_set(sfdf_pl
,
3835 mlxsw_sp_port
->local_port
);
3837 netdev_dbg(mlxsw_sp_port
->dev
, "FDB flushed using Port=%d, FID=%d\n",
3838 mlxsw_sp_port
->local_port
, fid
);
3840 return mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(sfdf
), sfdf_pl
);
3844 mlxsw_sp_port_fdb_flush_by_lag_id_fid(const struct mlxsw_sp_port
*mlxsw_sp_port
,
3847 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
3848 char sfdf_pl
[MLXSW_REG_SFDF_LEN
];
3850 mlxsw_reg_sfdf_pack(sfdf_pl
, MLXSW_REG_SFDF_FLUSH_PER_LAG_AND_FID
);
3851 mlxsw_reg_sfdf_fid_set(sfdf_pl
, fid
);
3852 mlxsw_reg_sfdf_lag_fid_lag_id_set(sfdf_pl
, mlxsw_sp_port
->lag_id
);
3854 netdev_dbg(mlxsw_sp_port
->dev
, "FDB flushed using LAG ID=%d, FID=%d\n",
3855 mlxsw_sp_port
->lag_id
, fid
);
3857 return mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(sfdf
), sfdf_pl
);
3860 int mlxsw_sp_port_fdb_flush(struct mlxsw_sp_port
*mlxsw_sp_port
, u16 fid
)
3862 if (!mlxsw_sp_port_fdb_should_flush(mlxsw_sp_port
, fid
))
3865 if (mlxsw_sp_port
->lagged
)
3866 return mlxsw_sp_port_fdb_flush_by_lag_id_fid(mlxsw_sp_port
,
3869 return mlxsw_sp_port_fdb_flush_by_port_fid(mlxsw_sp_port
, fid
);
3872 static void mlxsw_sp_master_bridge_gone_sync(struct mlxsw_sp
*mlxsw_sp
)
3874 struct mlxsw_sp_fid
*f
, *tmp
;
3876 list_for_each_entry_safe(f
, tmp
, &mlxsw_sp
->fids
, list
)
3877 if (--f
->ref_count
== 0)
3878 mlxsw_sp_fid_destroy(mlxsw_sp
, f
);
3883 static bool mlxsw_sp_master_bridge_check(struct mlxsw_sp
*mlxsw_sp
,
3884 struct net_device
*br_dev
)
3886 return !mlxsw_sp
->master_bridge
.dev
||
3887 mlxsw_sp
->master_bridge
.dev
== br_dev
;
3890 static void mlxsw_sp_master_bridge_inc(struct mlxsw_sp
*mlxsw_sp
,
3891 struct net_device
*br_dev
)
3893 mlxsw_sp
->master_bridge
.dev
= br_dev
;
3894 mlxsw_sp
->master_bridge
.ref_count
++;
3897 static void mlxsw_sp_master_bridge_dec(struct mlxsw_sp
*mlxsw_sp
)
3899 if (--mlxsw_sp
->master_bridge
.ref_count
== 0) {
3900 mlxsw_sp
->master_bridge
.dev
= NULL
;
3901 /* It's possible upper VLAN devices are still holding
3902 * references to underlying FIDs. Drop the reference
3903 * and release the resources if it was the last one.
3904 * If it wasn't, then something bad happened.
3906 mlxsw_sp_master_bridge_gone_sync(mlxsw_sp
);
3910 static int mlxsw_sp_port_bridge_join(struct mlxsw_sp_port
*mlxsw_sp_port
,
3911 struct net_device
*br_dev
)
3913 struct net_device
*dev
= mlxsw_sp_port
->dev
;
3916 /* When port is not bridged untagged packets are tagged with
3917 * PVID=VID=1, thereby creating an implicit VLAN interface in
3918 * the device. Remove it and let bridge code take care of its
3921 err
= mlxsw_sp_port_kill_vid(dev
, 0, 1);
3925 mlxsw_sp_master_bridge_inc(mlxsw_sp_port
->mlxsw_sp
, br_dev
);
3927 mlxsw_sp_port
->learning
= 1;
3928 mlxsw_sp_port
->learning_sync
= 1;
3929 mlxsw_sp_port
->uc_flood
= 1;
3930 mlxsw_sp_port
->bridged
= 1;
3935 static void mlxsw_sp_port_bridge_leave(struct mlxsw_sp_port
*mlxsw_sp_port
)
3937 struct net_device
*dev
= mlxsw_sp_port
->dev
;
3939 mlxsw_sp_port_pvid_set(mlxsw_sp_port
, 1);
3941 mlxsw_sp_master_bridge_dec(mlxsw_sp_port
->mlxsw_sp
);
3943 mlxsw_sp_port
->learning
= 0;
3944 mlxsw_sp_port
->learning_sync
= 0;
3945 mlxsw_sp_port
->uc_flood
= 0;
3946 mlxsw_sp_port
->bridged
= 0;
3948 /* Add implicit VLAN interface in the device, so that untagged
3949 * packets will be classified to the default vFID.
3951 mlxsw_sp_port_add_vid(dev
, 0, 1);
3954 static int mlxsw_sp_lag_create(struct mlxsw_sp
*mlxsw_sp
, u16 lag_id
)
3956 char sldr_pl
[MLXSW_REG_SLDR_LEN
];
3958 mlxsw_reg_sldr_lag_create_pack(sldr_pl
, lag_id
);
3959 return mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(sldr
), sldr_pl
);
3962 static int mlxsw_sp_lag_destroy(struct mlxsw_sp
*mlxsw_sp
, u16 lag_id
)
3964 char sldr_pl
[MLXSW_REG_SLDR_LEN
];
3966 mlxsw_reg_sldr_lag_destroy_pack(sldr_pl
, lag_id
);
3967 return mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(sldr
), sldr_pl
);
3970 static int mlxsw_sp_lag_col_port_add(struct mlxsw_sp_port
*mlxsw_sp_port
,
3971 u16 lag_id
, u8 port_index
)
3973 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
3974 char slcor_pl
[MLXSW_REG_SLCOR_LEN
];
3976 mlxsw_reg_slcor_port_add_pack(slcor_pl
, mlxsw_sp_port
->local_port
,
3977 lag_id
, port_index
);
3978 return mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(slcor
), slcor_pl
);
3981 static int mlxsw_sp_lag_col_port_remove(struct mlxsw_sp_port
*mlxsw_sp_port
,
3984 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
3985 char slcor_pl
[MLXSW_REG_SLCOR_LEN
];
3987 mlxsw_reg_slcor_port_remove_pack(slcor_pl
, mlxsw_sp_port
->local_port
,
3989 return mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(slcor
), slcor_pl
);
3992 static int mlxsw_sp_lag_col_port_enable(struct mlxsw_sp_port
*mlxsw_sp_port
,
3995 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
3996 char slcor_pl
[MLXSW_REG_SLCOR_LEN
];
3998 mlxsw_reg_slcor_col_enable_pack(slcor_pl
, mlxsw_sp_port
->local_port
,
4000 return mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(slcor
), slcor_pl
);
4003 static int mlxsw_sp_lag_col_port_disable(struct mlxsw_sp_port
*mlxsw_sp_port
,
4006 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
4007 char slcor_pl
[MLXSW_REG_SLCOR_LEN
];
4009 mlxsw_reg_slcor_col_disable_pack(slcor_pl
, mlxsw_sp_port
->local_port
,
4011 return mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(slcor
), slcor_pl
);
4014 static int mlxsw_sp_lag_index_get(struct mlxsw_sp
*mlxsw_sp
,
4015 struct net_device
*lag_dev
,
4018 struct mlxsw_sp_upper
*lag
;
4019 int free_lag_id
= -1;
4023 max_lag
= MLXSW_CORE_RES_GET(mlxsw_sp
->core
, MAX_LAG
);
4024 for (i
= 0; i
< max_lag
; i
++) {
4025 lag
= mlxsw_sp_lag_get(mlxsw_sp
, i
);
4026 if (lag
->ref_count
) {
4027 if (lag
->dev
== lag_dev
) {
4031 } else if (free_lag_id
< 0) {
4035 if (free_lag_id
< 0)
4037 *p_lag_id
= free_lag_id
;
4042 mlxsw_sp_master_lag_check(struct mlxsw_sp
*mlxsw_sp
,
4043 struct net_device
*lag_dev
,
4044 struct netdev_lag_upper_info
*lag_upper_info
)
4048 if (mlxsw_sp_lag_index_get(mlxsw_sp
, lag_dev
, &lag_id
) != 0)
4050 if (lag_upper_info
->tx_type
!= NETDEV_LAG_TX_TYPE_HASH
)
4055 static int mlxsw_sp_port_lag_index_get(struct mlxsw_sp
*mlxsw_sp
,
4056 u16 lag_id
, u8
*p_port_index
)
4058 u64 max_lag_members
;
4061 max_lag_members
= MLXSW_CORE_RES_GET(mlxsw_sp
->core
,
4063 for (i
= 0; i
< max_lag_members
; i
++) {
4064 if (!mlxsw_sp_port_lagged_get(mlxsw_sp
, lag_id
, i
)) {
4073 mlxsw_sp_port_pvid_vport_lag_join(struct mlxsw_sp_port
*mlxsw_sp_port
,
4076 struct mlxsw_sp_port
*mlxsw_sp_vport
;
4077 struct mlxsw_sp_fid
*f
;
4079 mlxsw_sp_vport
= mlxsw_sp_port_vport_find(mlxsw_sp_port
, 1);
4080 if (WARN_ON(!mlxsw_sp_vport
))
4083 /* If vPort is assigned a RIF, then leave it since it's no
4086 f
= mlxsw_sp_vport_fid_get(mlxsw_sp_vport
);
4088 f
->leave(mlxsw_sp_vport
);
4090 mlxsw_sp_vport
->lag_id
= lag_id
;
4091 mlxsw_sp_vport
->lagged
= 1;
4095 mlxsw_sp_port_pvid_vport_lag_leave(struct mlxsw_sp_port
*mlxsw_sp_port
)
4097 struct mlxsw_sp_port
*mlxsw_sp_vport
;
4098 struct mlxsw_sp_fid
*f
;
4100 mlxsw_sp_vport
= mlxsw_sp_port_vport_find(mlxsw_sp_port
, 1);
4101 if (WARN_ON(!mlxsw_sp_vport
))
4104 f
= mlxsw_sp_vport_fid_get(mlxsw_sp_vport
);
4106 f
->leave(mlxsw_sp_vport
);
4108 mlxsw_sp_vport
->lagged
= 0;
4111 static int mlxsw_sp_port_lag_join(struct mlxsw_sp_port
*mlxsw_sp_port
,
4112 struct net_device
*lag_dev
)
4114 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
4115 struct mlxsw_sp_upper
*lag
;
4120 err
= mlxsw_sp_lag_index_get(mlxsw_sp
, lag_dev
, &lag_id
);
4123 lag
= mlxsw_sp_lag_get(mlxsw_sp
, lag_id
);
4124 if (!lag
->ref_count
) {
4125 err
= mlxsw_sp_lag_create(mlxsw_sp
, lag_id
);
4131 err
= mlxsw_sp_port_lag_index_get(mlxsw_sp
, lag_id
, &port_index
);
4134 err
= mlxsw_sp_lag_col_port_add(mlxsw_sp_port
, lag_id
, port_index
);
4136 goto err_col_port_add
;
4137 err
= mlxsw_sp_lag_col_port_enable(mlxsw_sp_port
, lag_id
);
4139 goto err_col_port_enable
;
4141 mlxsw_core_lag_mapping_set(mlxsw_sp
->core
, lag_id
, port_index
,
4142 mlxsw_sp_port
->local_port
);
4143 mlxsw_sp_port
->lag_id
= lag_id
;
4144 mlxsw_sp_port
->lagged
= 1;
4147 mlxsw_sp_port_pvid_vport_lag_join(mlxsw_sp_port
, lag_id
);
4151 err_col_port_enable
:
4152 mlxsw_sp_lag_col_port_remove(mlxsw_sp_port
, lag_id
);
4154 if (!lag
->ref_count
)
4155 mlxsw_sp_lag_destroy(mlxsw_sp
, lag_id
);
4159 static void mlxsw_sp_port_lag_leave(struct mlxsw_sp_port
*mlxsw_sp_port
,
4160 struct net_device
*lag_dev
)
4162 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
4163 u16 lag_id
= mlxsw_sp_port
->lag_id
;
4164 struct mlxsw_sp_upper
*lag
;
4166 if (!mlxsw_sp_port
->lagged
)
4168 lag
= mlxsw_sp_lag_get(mlxsw_sp
, lag_id
);
4169 WARN_ON(lag
->ref_count
== 0);
4171 mlxsw_sp_lag_col_port_disable(mlxsw_sp_port
, lag_id
);
4172 mlxsw_sp_lag_col_port_remove(mlxsw_sp_port
, lag_id
);
4174 if (mlxsw_sp_port
->bridged
) {
4175 mlxsw_sp_port_active_vlans_del(mlxsw_sp_port
);
4176 mlxsw_sp_port_bridge_leave(mlxsw_sp_port
);
4179 if (lag
->ref_count
== 1)
4180 mlxsw_sp_lag_destroy(mlxsw_sp
, lag_id
);
4182 mlxsw_core_lag_mapping_clear(mlxsw_sp
->core
, lag_id
,
4183 mlxsw_sp_port
->local_port
);
4184 mlxsw_sp_port
->lagged
= 0;
4187 mlxsw_sp_port_pvid_vport_lag_leave(mlxsw_sp_port
);
4190 static int mlxsw_sp_lag_dist_port_add(struct mlxsw_sp_port
*mlxsw_sp_port
,
4193 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
4194 char sldr_pl
[MLXSW_REG_SLDR_LEN
];
4196 mlxsw_reg_sldr_lag_add_port_pack(sldr_pl
, lag_id
,
4197 mlxsw_sp_port
->local_port
);
4198 return mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(sldr
), sldr_pl
);
4201 static int mlxsw_sp_lag_dist_port_remove(struct mlxsw_sp_port
*mlxsw_sp_port
,
4204 struct mlxsw_sp
*mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
4205 char sldr_pl
[MLXSW_REG_SLDR_LEN
];
4207 mlxsw_reg_sldr_lag_remove_port_pack(sldr_pl
, lag_id
,
4208 mlxsw_sp_port
->local_port
);
4209 return mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(sldr
), sldr_pl
);
4212 static int mlxsw_sp_port_lag_tx_en_set(struct mlxsw_sp_port
*mlxsw_sp_port
,
4213 bool lag_tx_enabled
)
4216 return mlxsw_sp_lag_dist_port_add(mlxsw_sp_port
,
4217 mlxsw_sp_port
->lag_id
);
4219 return mlxsw_sp_lag_dist_port_remove(mlxsw_sp_port
,
4220 mlxsw_sp_port
->lag_id
);
4223 static int mlxsw_sp_port_lag_changed(struct mlxsw_sp_port
*mlxsw_sp_port
,
4224 struct netdev_lag_lower_state_info
*info
)
4226 return mlxsw_sp_port_lag_tx_en_set(mlxsw_sp_port
, info
->tx_enabled
);
4229 static int mlxsw_sp_port_vlan_link(struct mlxsw_sp_port
*mlxsw_sp_port
,
4230 struct net_device
*vlan_dev
)
4232 struct mlxsw_sp_port
*mlxsw_sp_vport
;
4233 u16 vid
= vlan_dev_vlan_id(vlan_dev
);
4235 mlxsw_sp_vport
= mlxsw_sp_port_vport_find(mlxsw_sp_port
, vid
);
4236 if (WARN_ON(!mlxsw_sp_vport
))
4239 mlxsw_sp_vport
->dev
= vlan_dev
;
4244 static void mlxsw_sp_port_vlan_unlink(struct mlxsw_sp_port
*mlxsw_sp_port
,
4245 struct net_device
*vlan_dev
)
4247 struct mlxsw_sp_port
*mlxsw_sp_vport
;
4248 u16 vid
= vlan_dev_vlan_id(vlan_dev
);
4250 mlxsw_sp_vport
= mlxsw_sp_port_vport_find(mlxsw_sp_port
, vid
);
4251 if (WARN_ON(!mlxsw_sp_vport
))
4254 mlxsw_sp_vport
->dev
= mlxsw_sp_port
->dev
;
4257 static int mlxsw_sp_netdevice_port_upper_event(struct net_device
*dev
,
4258 unsigned long event
, void *ptr
)
4260 struct netdev_notifier_changeupper_info
*info
;
4261 struct mlxsw_sp_port
*mlxsw_sp_port
;
4262 struct net_device
*upper_dev
;
4263 struct mlxsw_sp
*mlxsw_sp
;
4266 mlxsw_sp_port
= netdev_priv(dev
);
4267 mlxsw_sp
= mlxsw_sp_port
->mlxsw_sp
;
4271 case NETDEV_PRECHANGEUPPER
:
4272 upper_dev
= info
->upper_dev
;
4273 if (!is_vlan_dev(upper_dev
) &&
4274 !netif_is_lag_master(upper_dev
) &&
4275 !netif_is_bridge_master(upper_dev
))
4279 /* HW limitation forbids to put ports to multiple bridges. */
4280 if (netif_is_bridge_master(upper_dev
) &&
4281 !mlxsw_sp_master_bridge_check(mlxsw_sp
, upper_dev
))
4283 if (netif_is_lag_master(upper_dev
) &&
4284 !mlxsw_sp_master_lag_check(mlxsw_sp
, upper_dev
,
4287 if (netif_is_lag_master(upper_dev
) && vlan_uses_dev(dev
))
4289 if (netif_is_lag_port(dev
) && is_vlan_dev(upper_dev
) &&
4290 !netif_is_lag_master(vlan_dev_real_dev(upper_dev
)))
4293 case NETDEV_CHANGEUPPER
:
4294 upper_dev
= info
->upper_dev
;
4295 if (is_vlan_dev(upper_dev
)) {
4297 err
= mlxsw_sp_port_vlan_link(mlxsw_sp_port
,
4300 mlxsw_sp_port_vlan_unlink(mlxsw_sp_port
,
4302 } else if (netif_is_bridge_master(upper_dev
)) {
4304 err
= mlxsw_sp_port_bridge_join(mlxsw_sp_port
,
4307 mlxsw_sp_port_bridge_leave(mlxsw_sp_port
);
4308 } else if (netif_is_lag_master(upper_dev
)) {
4310 err
= mlxsw_sp_port_lag_join(mlxsw_sp_port
,
4313 mlxsw_sp_port_lag_leave(mlxsw_sp_port
,
4325 static int mlxsw_sp_netdevice_port_lower_event(struct net_device
*dev
,
4326 unsigned long event
, void *ptr
)
4328 struct netdev_notifier_changelowerstate_info
*info
;
4329 struct mlxsw_sp_port
*mlxsw_sp_port
;
4332 mlxsw_sp_port
= netdev_priv(dev
);
4336 case NETDEV_CHANGELOWERSTATE
:
4337 if (netif_is_lag_port(dev
) && mlxsw_sp_port
->lagged
) {
4338 err
= mlxsw_sp_port_lag_changed(mlxsw_sp_port
,
4339 info
->lower_state_info
);
4341 netdev_err(dev
, "Failed to reflect link aggregation lower state change\n");
4349 static int mlxsw_sp_netdevice_port_event(struct net_device
*dev
,
4350 unsigned long event
, void *ptr
)
4353 case NETDEV_PRECHANGEUPPER
:
4354 case NETDEV_CHANGEUPPER
:
4355 return mlxsw_sp_netdevice_port_upper_event(dev
, event
, ptr
);
4356 case NETDEV_CHANGELOWERSTATE
:
4357 return mlxsw_sp_netdevice_port_lower_event(dev
, event
, ptr
);
4363 static int mlxsw_sp_netdevice_lag_event(struct net_device
*lag_dev
,
4364 unsigned long event
, void *ptr
)
4366 struct net_device
*dev
;
4367 struct list_head
*iter
;
4370 netdev_for_each_lower_dev(lag_dev
, dev
, iter
) {
4371 if (mlxsw_sp_port_dev_check(dev
)) {
4372 ret
= mlxsw_sp_netdevice_port_event(dev
, event
, ptr
);
4381 static int mlxsw_sp_master_bridge_vlan_link(struct mlxsw_sp
*mlxsw_sp
,
4382 struct net_device
*vlan_dev
)
4384 u16 fid
= vlan_dev_vlan_id(vlan_dev
);
4385 struct mlxsw_sp_fid
*f
;
4387 f
= mlxsw_sp_fid_find(mlxsw_sp
, fid
);
4389 f
= mlxsw_sp_fid_create(mlxsw_sp
, fid
);
4399 static void mlxsw_sp_master_bridge_vlan_unlink(struct mlxsw_sp
*mlxsw_sp
,
4400 struct net_device
*vlan_dev
)
4402 u16 fid
= vlan_dev_vlan_id(vlan_dev
);
4403 struct mlxsw_sp_fid
*f
;
4405 f
= mlxsw_sp_fid_find(mlxsw_sp
, fid
);
4407 mlxsw_sp_rif_bridge_destroy(mlxsw_sp
, f
->r
);
4408 if (f
&& --f
->ref_count
== 0)
4409 mlxsw_sp_fid_destroy(mlxsw_sp
, f
);
4412 static int mlxsw_sp_netdevice_bridge_event(struct net_device
*br_dev
,
4413 unsigned long event
, void *ptr
)
4415 struct netdev_notifier_changeupper_info
*info
;
4416 struct net_device
*upper_dev
;
4417 struct mlxsw_sp
*mlxsw_sp
;
4420 mlxsw_sp
= mlxsw_sp_lower_get(br_dev
);
4423 if (br_dev
!= mlxsw_sp
->master_bridge
.dev
)
4429 case NETDEV_CHANGEUPPER
:
4430 upper_dev
= info
->upper_dev
;
4431 if (!is_vlan_dev(upper_dev
))
4433 if (info
->linking
) {
4434 err
= mlxsw_sp_master_bridge_vlan_link(mlxsw_sp
,
4439 mlxsw_sp_master_bridge_vlan_unlink(mlxsw_sp
, upper_dev
);
4447 static u16
mlxsw_sp_avail_vfid_get(const struct mlxsw_sp
*mlxsw_sp
)
4449 return find_first_zero_bit(mlxsw_sp
->vfids
.mapped
,
4453 static int mlxsw_sp_vfid_op(struct mlxsw_sp
*mlxsw_sp
, u16 fid
, bool create
)
4455 char sfmr_pl
[MLXSW_REG_SFMR_LEN
];
4457 mlxsw_reg_sfmr_pack(sfmr_pl
, !create
, fid
, 0);
4458 return mlxsw_reg_write(mlxsw_sp
->core
, MLXSW_REG(sfmr
), sfmr_pl
);
4461 static void mlxsw_sp_vport_vfid_leave(struct mlxsw_sp_port
*mlxsw_sp_vport
);
4463 static struct mlxsw_sp_fid
*mlxsw_sp_vfid_create(struct mlxsw_sp
*mlxsw_sp
,
4464 struct net_device
*br_dev
)
4466 struct device
*dev
= mlxsw_sp
->bus_info
->dev
;
4467 struct mlxsw_sp_fid
*f
;
4471 vfid
= mlxsw_sp_avail_vfid_get(mlxsw_sp
);
4472 if (vfid
== MLXSW_SP_VFID_MAX
) {
4473 dev_err(dev
, "No available vFIDs\n");
4474 return ERR_PTR(-ERANGE
);
4477 fid
= mlxsw_sp_vfid_to_fid(vfid
);
4478 err
= mlxsw_sp_vfid_op(mlxsw_sp
, fid
, true);
4480 dev_err(dev
, "Failed to create FID=%d\n", fid
);
4481 return ERR_PTR(err
);
4484 f
= kzalloc(sizeof(*f
), GFP_KERNEL
);
4486 goto err_allocate_vfid
;
4488 f
->leave
= mlxsw_sp_vport_vfid_leave
;
4492 list_add(&f
->list
, &mlxsw_sp
->vfids
.list
);
4493 set_bit(vfid
, mlxsw_sp
->vfids
.mapped
);
4498 mlxsw_sp_vfid_op(mlxsw_sp
, fid
, false);
4499 return ERR_PTR(-ENOMEM
);
4502 static void mlxsw_sp_vfid_destroy(struct mlxsw_sp
*mlxsw_sp
,
4503 struct mlxsw_sp_fid
*f
)
4505 u16 vfid
= mlxsw_sp_fid_to_vfid(f
->fid
);
4508 clear_bit(vfid
, mlxsw_sp
->vfids
.mapped
);
4512 mlxsw_sp_rif_bridge_destroy(mlxsw_sp
, f
->r
);
4516 mlxsw_sp_vfid_op(mlxsw_sp
, fid
, false);
4519 static int mlxsw_sp_vport_fid_map(struct mlxsw_sp_port
*mlxsw_sp_vport
, u16 fid
,
4522 enum mlxsw_reg_svfa_mt mt
= MLXSW_REG_SVFA_MT_PORT_VID_TO_FID
;
4523 u16 vid
= mlxsw_sp_vport_vid_get(mlxsw_sp_vport
);
4525 return mlxsw_sp_port_vid_to_fid_set(mlxsw_sp_vport
, mt
, valid
, fid
,
4529 static int mlxsw_sp_vport_vfid_join(struct mlxsw_sp_port
*mlxsw_sp_vport
,
4530 struct net_device
*br_dev
)
4532 struct mlxsw_sp_fid
*f
;
4535 f
= mlxsw_sp_vfid_find(mlxsw_sp_vport
->mlxsw_sp
, br_dev
);
4537 f
= mlxsw_sp_vfid_create(mlxsw_sp_vport
->mlxsw_sp
, br_dev
);
4542 err
= mlxsw_sp_vport_flood_set(mlxsw_sp_vport
, f
->fid
, true);
4544 goto err_vport_flood_set
;
4546 err
= mlxsw_sp_vport_fid_map(mlxsw_sp_vport
, f
->fid
, true);
4548 goto err_vport_fid_map
;
4550 mlxsw_sp_vport_fid_set(mlxsw_sp_vport
, f
);
4553 netdev_dbg(mlxsw_sp_vport
->dev
, "Joined FID=%d\n", f
->fid
);
4558 mlxsw_sp_vport_flood_set(mlxsw_sp_vport
, f
->fid
, false);
4559 err_vport_flood_set
:
4561 mlxsw_sp_vfid_destroy(mlxsw_sp_vport
->mlxsw_sp
, f
);
4565 static void mlxsw_sp_vport_vfid_leave(struct mlxsw_sp_port
*mlxsw_sp_vport
)
4567 struct mlxsw_sp_fid
*f
= mlxsw_sp_vport_fid_get(mlxsw_sp_vport
);
4569 netdev_dbg(mlxsw_sp_vport
->dev
, "Left FID=%d\n", f
->fid
);
4571 mlxsw_sp_vport_fid_map(mlxsw_sp_vport
, f
->fid
, false);
4573 mlxsw_sp_vport_flood_set(mlxsw_sp_vport
, f
->fid
, false);
4575 mlxsw_sp_port_fdb_flush(mlxsw_sp_vport
, f
->fid
);
4577 mlxsw_sp_vport_fid_set(mlxsw_sp_vport
, NULL
);
4578 if (--f
->ref_count
== 0)
4579 mlxsw_sp_vfid_destroy(mlxsw_sp_vport
->mlxsw_sp
, f
);
4582 static int mlxsw_sp_vport_bridge_join(struct mlxsw_sp_port
*mlxsw_sp_vport
,
4583 struct net_device
*br_dev
)
4585 struct mlxsw_sp_fid
*f
= mlxsw_sp_vport_fid_get(mlxsw_sp_vport
);
4586 u16 vid
= mlxsw_sp_vport_vid_get(mlxsw_sp_vport
);
4587 struct net_device
*dev
= mlxsw_sp_vport
->dev
;
4590 if (f
&& !WARN_ON(!f
->leave
))
4591 f
->leave(mlxsw_sp_vport
);
4593 err
= mlxsw_sp_vport_vfid_join(mlxsw_sp_vport
, br_dev
);
4595 netdev_err(dev
, "Failed to join vFID\n");
4599 err
= mlxsw_sp_port_vid_learning_set(mlxsw_sp_vport
, vid
, true);
4601 netdev_err(dev
, "Failed to enable learning\n");
4602 goto err_port_vid_learning_set
;
4605 mlxsw_sp_vport
->learning
= 1;
4606 mlxsw_sp_vport
->learning_sync
= 1;
4607 mlxsw_sp_vport
->uc_flood
= 1;
4608 mlxsw_sp_vport
->bridged
= 1;
4612 err_port_vid_learning_set
:
4613 mlxsw_sp_vport_vfid_leave(mlxsw_sp_vport
);
4617 static void mlxsw_sp_vport_bridge_leave(struct mlxsw_sp_port
*mlxsw_sp_vport
)
4619 u16 vid
= mlxsw_sp_vport_vid_get(mlxsw_sp_vport
);
4621 mlxsw_sp_port_vid_learning_set(mlxsw_sp_vport
, vid
, false);
4623 mlxsw_sp_vport_vfid_leave(mlxsw_sp_vport
);
4625 mlxsw_sp_vport
->learning
= 0;
4626 mlxsw_sp_vport
->learning_sync
= 0;
4627 mlxsw_sp_vport
->uc_flood
= 0;
4628 mlxsw_sp_vport
->bridged
= 0;
4632 mlxsw_sp_port_master_bridge_check(const struct mlxsw_sp_port
*mlxsw_sp_port
,
4633 const struct net_device
*br_dev
)
4635 struct mlxsw_sp_port
*mlxsw_sp_vport
;
4637 list_for_each_entry(mlxsw_sp_vport
, &mlxsw_sp_port
->vports_list
,
4639 struct net_device
*dev
= mlxsw_sp_vport_dev_get(mlxsw_sp_vport
);
4641 if (dev
&& dev
== br_dev
)
4648 static int mlxsw_sp_netdevice_vport_event(struct net_device
*dev
,
4649 unsigned long event
, void *ptr
,
4652 struct mlxsw_sp_port
*mlxsw_sp_port
= netdev_priv(dev
);
4653 struct netdev_notifier_changeupper_info
*info
= ptr
;
4654 struct mlxsw_sp_port
*mlxsw_sp_vport
;
4655 struct net_device
*upper_dev
;
4658 mlxsw_sp_vport
= mlxsw_sp_port_vport_find(mlxsw_sp_port
, vid
);
4661 case NETDEV_PRECHANGEUPPER
:
4662 upper_dev
= info
->upper_dev
;
4663 if (!netif_is_bridge_master(upper_dev
))
4667 /* We can't have multiple VLAN interfaces configured on
4668 * the same port and being members in the same bridge.
4670 if (!mlxsw_sp_port_master_bridge_check(mlxsw_sp_port
,
4674 case NETDEV_CHANGEUPPER
:
4675 upper_dev
= info
->upper_dev
;
4676 if (info
->linking
) {
4677 if (WARN_ON(!mlxsw_sp_vport
))
4679 err
= mlxsw_sp_vport_bridge_join(mlxsw_sp_vport
,
4682 if (!mlxsw_sp_vport
)
4684 mlxsw_sp_vport_bridge_leave(mlxsw_sp_vport
);
4691 static int mlxsw_sp_netdevice_lag_vport_event(struct net_device
*lag_dev
,
4692 unsigned long event
, void *ptr
,
4695 struct net_device
*dev
;
4696 struct list_head
*iter
;
4699 netdev_for_each_lower_dev(lag_dev
, dev
, iter
) {
4700 if (mlxsw_sp_port_dev_check(dev
)) {
4701 ret
= mlxsw_sp_netdevice_vport_event(dev
, event
, ptr
,
4711 static int mlxsw_sp_netdevice_vlan_event(struct net_device
*vlan_dev
,
4712 unsigned long event
, void *ptr
)
4714 struct net_device
*real_dev
= vlan_dev_real_dev(vlan_dev
);
4715 u16 vid
= vlan_dev_vlan_id(vlan_dev
);
4717 if (mlxsw_sp_port_dev_check(real_dev
))
4718 return mlxsw_sp_netdevice_vport_event(real_dev
, event
, ptr
,
4720 else if (netif_is_lag_master(real_dev
))
4721 return mlxsw_sp_netdevice_lag_vport_event(real_dev
, event
, ptr
,
4727 static int mlxsw_sp_netdevice_event(struct notifier_block
*unused
,
4728 unsigned long event
, void *ptr
)
4730 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
4733 if (event
== NETDEV_CHANGEADDR
|| event
== NETDEV_CHANGEMTU
)
4734 err
= mlxsw_sp_netdevice_router_port_event(dev
);
4735 else if (mlxsw_sp_port_dev_check(dev
))
4736 err
= mlxsw_sp_netdevice_port_event(dev
, event
, ptr
);
4737 else if (netif_is_lag_master(dev
))
4738 err
= mlxsw_sp_netdevice_lag_event(dev
, event
, ptr
);
4739 else if (netif_is_bridge_master(dev
))
4740 err
= mlxsw_sp_netdevice_bridge_event(dev
, event
, ptr
);
4741 else if (is_vlan_dev(dev
))
4742 err
= mlxsw_sp_netdevice_vlan_event(dev
, event
, ptr
);
4744 return notifier_from_errno(err
);
4747 static struct notifier_block mlxsw_sp_netdevice_nb __read_mostly
= {
4748 .notifier_call
= mlxsw_sp_netdevice_event
,
4751 static struct notifier_block mlxsw_sp_inetaddr_nb __read_mostly
= {
4752 .notifier_call
= mlxsw_sp_inetaddr_event
,
4753 .priority
= 10, /* Must be called before FIB notifier block */
4756 static struct notifier_block mlxsw_sp_router_netevent_nb __read_mostly
= {
4757 .notifier_call
= mlxsw_sp_router_netevent_event
,
4760 static const struct pci_device_id mlxsw_sp_pci_id_table
[] = {
4761 {PCI_VDEVICE(MELLANOX
, PCI_DEVICE_ID_MELLANOX_SPECTRUM
), 0},
4765 static struct pci_driver mlxsw_sp_pci_driver
= {
4766 .name
= mlxsw_sp_driver_name
,
4767 .id_table
= mlxsw_sp_pci_id_table
,
4770 static int __init
mlxsw_sp_module_init(void)
4774 register_netdevice_notifier(&mlxsw_sp_netdevice_nb
);
4775 register_inetaddr_notifier(&mlxsw_sp_inetaddr_nb
);
4776 register_netevent_notifier(&mlxsw_sp_router_netevent_nb
);
4778 err
= mlxsw_core_driver_register(&mlxsw_sp_driver
);
4780 goto err_core_driver_register
;
4782 err
= mlxsw_pci_driver_register(&mlxsw_sp_pci_driver
);
4784 goto err_pci_driver_register
;
4788 err_pci_driver_register
:
4789 mlxsw_core_driver_unregister(&mlxsw_sp_driver
);
4790 err_core_driver_register
:
4791 unregister_netevent_notifier(&mlxsw_sp_router_netevent_nb
);
4792 unregister_inetaddr_notifier(&mlxsw_sp_inetaddr_nb
);
4793 unregister_netdevice_notifier(&mlxsw_sp_netdevice_nb
);
4797 static void __exit
mlxsw_sp_module_exit(void)
4799 mlxsw_pci_driver_unregister(&mlxsw_sp_pci_driver
);
4800 mlxsw_core_driver_unregister(&mlxsw_sp_driver
);
4801 unregister_netevent_notifier(&mlxsw_sp_router_netevent_nb
);
4802 unregister_inetaddr_notifier(&mlxsw_sp_inetaddr_nb
);
4803 unregister_netdevice_notifier(&mlxsw_sp_netdevice_nb
);
4806 module_init(mlxsw_sp_module_init
);
4807 module_exit(mlxsw_sp_module_exit
);
4809 MODULE_LICENSE("Dual BSD/GPL");
4810 MODULE_AUTHOR("Jiri Pirko <jiri@mellanox.com>");
4811 MODULE_DESCRIPTION("Mellanox Spectrum driver");
4812 MODULE_DEVICE_TABLE(pci
, mlxsw_sp_pci_id_table
);