2 * drivers/net/ethernet/mellanox/mlxsw/spectrum.c
3 * Copyright (c) 2015-2017 Mellanox Technologies. All rights reserved.
4 * Copyright (c) 2015-2017 Jiri Pirko <jiri@mellanox.com>
5 * Copyright (c) 2015 Ido Schimmel <idosch@mellanox.com>
6 * Copyright (c) 2015 Elad Raz <eladr@mellanox.com>
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 <linux/netlink.h>
57 #include <net/switchdev.h>
58 #include <net/pkt_cls.h>
59 #include <net/tc_act/tc_mirred.h>
60 #include <net/netevent.h>
61 #include <net/tc_act/tc_sample.h>
62 #include <net/addrconf.h>
71 #include "spectrum_cnt.h"
72 #include "spectrum_dpipe.h"
73 #include "spectrum_acl_flex_actions.h"
74 #include "../mlxfw/mlxfw.h"
76 #define MLXSW_FWREV_MAJOR 13
77 #define MLXSW_FWREV_MINOR 1530
78 #define MLXSW_FWREV_SUBMINOR 152
79 #define MLXSW_FWREV_MINOR_TO_BRANCH(minor) ((minor) / 100)
81 #define MLXSW_SP_FW_FILENAME \
82 "mellanox/mlxsw_spectrum-" __stringify(MLXSW_FWREV_MAJOR) \
83 "." __stringify(MLXSW_FWREV_MINOR) \
84 "." __stringify(MLXSW_FWREV_SUBMINOR) ".mfa2"
86 static const char mlxsw_sp_driver_name[] = "mlxsw_spectrum";
87 static const char mlxsw_sp_driver_version[] = "1.0";
93 MLXSW_ITEM32(tx, hdr, version, 0x00, 28, 4);
96 * Packet control type.
97 * 0 - Ethernet control (e.g. EMADs, LACP)
100 MLXSW_ITEM32(tx, hdr, ctl, 0x00, 26, 2);
103 * Packet protocol type. Must be set to 1 (Ethernet).
105 MLXSW_ITEM32(tx, hdr, proto, 0x00, 21, 3);
107 /* tx_hdr_rx_is_router
108 * Packet is sent from the router. Valid for data packets only.
110 MLXSW_ITEM32(tx, hdr, rx_is_router, 0x00, 19, 1);
113 * Indicates if the 'fid' field is valid and should be used for
114 * forwarding lookup. Valid for data packets only.
116 MLXSW_ITEM32(tx, hdr, fid_valid, 0x00, 16, 1);
119 * Switch partition ID. Must be set to 0.
121 MLXSW_ITEM32(tx, hdr, swid, 0x00, 12, 3);
123 /* tx_hdr_control_tclass
124 * Indicates if the packet should use the control TClass and not one
125 * of the data TClasses.
127 MLXSW_ITEM32(tx, hdr, control_tclass, 0x00, 6, 1);
130 * Egress TClass to be used on the egress device on the egress port.
132 MLXSW_ITEM32(tx, hdr, etclass, 0x00, 0, 4);
135 * Destination local port for unicast packets.
136 * Destination multicast ID for multicast packets.
138 * Control packets are directed to a specific egress port, while data
139 * packets are transmitted through the CPU port (0) into the switch partition,
140 * where forwarding rules are applied.
142 MLXSW_ITEM32(tx, hdr, port_mid, 0x04, 16, 16);
145 * Forwarding ID used for L2 forwarding lookup. Valid only if 'fid_valid' is
146 * set, otherwise calculated based on the packet's VID using VID to FID mapping.
147 * Valid for data packets only.
149 MLXSW_ITEM32(tx, hdr, fid, 0x08, 0, 16);
153 * 6 - Control packets
155 MLXSW_ITEM32(tx, hdr, type, 0x0C, 0, 4);
157 struct mlxsw_sp_mlxfw_dev {
158 struct mlxfw_dev mlxfw_dev;
159 struct mlxsw_sp *mlxsw_sp;
162 static int mlxsw_sp_component_query(struct mlxfw_dev *mlxfw_dev,
163 u16 component_index, u32 *p_max_size,
164 u8 *p_align_bits, u16 *p_max_write_size)
166 struct mlxsw_sp_mlxfw_dev *mlxsw_sp_mlxfw_dev =
167 container_of(mlxfw_dev, struct mlxsw_sp_mlxfw_dev, mlxfw_dev);
168 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_mlxfw_dev->mlxsw_sp;
169 char mcqi_pl[MLXSW_REG_MCQI_LEN];
172 mlxsw_reg_mcqi_pack(mcqi_pl, component_index);
173 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(mcqi), mcqi_pl);
176 mlxsw_reg_mcqi_unpack(mcqi_pl, p_max_size, p_align_bits,
179 *p_align_bits = max_t(u8, *p_align_bits, 2);
180 *p_max_write_size = min_t(u16, *p_max_write_size,
181 MLXSW_REG_MCDA_MAX_DATA_LEN);
185 static int mlxsw_sp_fsm_lock(struct mlxfw_dev *mlxfw_dev, u32 *fwhandle)
187 struct mlxsw_sp_mlxfw_dev *mlxsw_sp_mlxfw_dev =
188 container_of(mlxfw_dev, struct mlxsw_sp_mlxfw_dev, mlxfw_dev);
189 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_mlxfw_dev->mlxsw_sp;
190 char mcc_pl[MLXSW_REG_MCC_LEN];
194 mlxsw_reg_mcc_pack(mcc_pl, 0, 0, 0, 0);
195 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(mcc), mcc_pl);
199 mlxsw_reg_mcc_unpack(mcc_pl, fwhandle, NULL, &control_state);
200 if (control_state != MLXFW_FSM_STATE_IDLE)
203 mlxsw_reg_mcc_pack(mcc_pl,
204 MLXSW_REG_MCC_INSTRUCTION_LOCK_UPDATE_HANDLE,
206 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(mcc), mcc_pl);
209 static int mlxsw_sp_fsm_component_update(struct mlxfw_dev *mlxfw_dev,
210 u32 fwhandle, u16 component_index,
213 struct mlxsw_sp_mlxfw_dev *mlxsw_sp_mlxfw_dev =
214 container_of(mlxfw_dev, struct mlxsw_sp_mlxfw_dev, mlxfw_dev);
215 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_mlxfw_dev->mlxsw_sp;
216 char mcc_pl[MLXSW_REG_MCC_LEN];
218 mlxsw_reg_mcc_pack(mcc_pl, MLXSW_REG_MCC_INSTRUCTION_UPDATE_COMPONENT,
219 component_index, fwhandle, component_size);
220 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(mcc), mcc_pl);
223 static int mlxsw_sp_fsm_block_download(struct mlxfw_dev *mlxfw_dev,
224 u32 fwhandle, u8 *data, u16 size,
227 struct mlxsw_sp_mlxfw_dev *mlxsw_sp_mlxfw_dev =
228 container_of(mlxfw_dev, struct mlxsw_sp_mlxfw_dev, mlxfw_dev);
229 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_mlxfw_dev->mlxsw_sp;
230 char mcda_pl[MLXSW_REG_MCDA_LEN];
232 mlxsw_reg_mcda_pack(mcda_pl, fwhandle, offset, size, data);
233 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(mcda), mcda_pl);
236 static int mlxsw_sp_fsm_component_verify(struct mlxfw_dev *mlxfw_dev,
237 u32 fwhandle, u16 component_index)
239 struct mlxsw_sp_mlxfw_dev *mlxsw_sp_mlxfw_dev =
240 container_of(mlxfw_dev, struct mlxsw_sp_mlxfw_dev, mlxfw_dev);
241 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_mlxfw_dev->mlxsw_sp;
242 char mcc_pl[MLXSW_REG_MCC_LEN];
244 mlxsw_reg_mcc_pack(mcc_pl, MLXSW_REG_MCC_INSTRUCTION_VERIFY_COMPONENT,
245 component_index, fwhandle, 0);
246 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(mcc), mcc_pl);
249 static int mlxsw_sp_fsm_activate(struct mlxfw_dev *mlxfw_dev, u32 fwhandle)
251 struct mlxsw_sp_mlxfw_dev *mlxsw_sp_mlxfw_dev =
252 container_of(mlxfw_dev, struct mlxsw_sp_mlxfw_dev, mlxfw_dev);
253 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_mlxfw_dev->mlxsw_sp;
254 char mcc_pl[MLXSW_REG_MCC_LEN];
256 mlxsw_reg_mcc_pack(mcc_pl, MLXSW_REG_MCC_INSTRUCTION_ACTIVATE, 0,
258 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(mcc), mcc_pl);
261 static int mlxsw_sp_fsm_query_state(struct mlxfw_dev *mlxfw_dev, u32 fwhandle,
262 enum mlxfw_fsm_state *fsm_state,
263 enum mlxfw_fsm_state_err *fsm_state_err)
265 struct mlxsw_sp_mlxfw_dev *mlxsw_sp_mlxfw_dev =
266 container_of(mlxfw_dev, struct mlxsw_sp_mlxfw_dev, mlxfw_dev);
267 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_mlxfw_dev->mlxsw_sp;
268 char mcc_pl[MLXSW_REG_MCC_LEN];
273 mlxsw_reg_mcc_pack(mcc_pl, 0, 0, fwhandle, 0);
274 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(mcc), mcc_pl);
278 mlxsw_reg_mcc_unpack(mcc_pl, NULL, &error_code, &control_state);
279 *fsm_state = control_state;
280 *fsm_state_err = min_t(enum mlxfw_fsm_state_err, error_code,
281 MLXFW_FSM_STATE_ERR_MAX);
285 static void mlxsw_sp_fsm_cancel(struct mlxfw_dev *mlxfw_dev, u32 fwhandle)
287 struct mlxsw_sp_mlxfw_dev *mlxsw_sp_mlxfw_dev =
288 container_of(mlxfw_dev, struct mlxsw_sp_mlxfw_dev, mlxfw_dev);
289 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_mlxfw_dev->mlxsw_sp;
290 char mcc_pl[MLXSW_REG_MCC_LEN];
292 mlxsw_reg_mcc_pack(mcc_pl, MLXSW_REG_MCC_INSTRUCTION_CANCEL, 0,
294 mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(mcc), mcc_pl);
297 static void mlxsw_sp_fsm_release(struct mlxfw_dev *mlxfw_dev, u32 fwhandle)
299 struct mlxsw_sp_mlxfw_dev *mlxsw_sp_mlxfw_dev =
300 container_of(mlxfw_dev, struct mlxsw_sp_mlxfw_dev, mlxfw_dev);
301 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_mlxfw_dev->mlxsw_sp;
302 char mcc_pl[MLXSW_REG_MCC_LEN];
304 mlxsw_reg_mcc_pack(mcc_pl,
305 MLXSW_REG_MCC_INSTRUCTION_RELEASE_UPDATE_HANDLE, 0,
307 mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(mcc), mcc_pl);
310 static const struct mlxfw_dev_ops mlxsw_sp_mlxfw_dev_ops = {
311 .component_query = mlxsw_sp_component_query,
312 .fsm_lock = mlxsw_sp_fsm_lock,
313 .fsm_component_update = mlxsw_sp_fsm_component_update,
314 .fsm_block_download = mlxsw_sp_fsm_block_download,
315 .fsm_component_verify = mlxsw_sp_fsm_component_verify,
316 .fsm_activate = mlxsw_sp_fsm_activate,
317 .fsm_query_state = mlxsw_sp_fsm_query_state,
318 .fsm_cancel = mlxsw_sp_fsm_cancel,
319 .fsm_release = mlxsw_sp_fsm_release
322 static int mlxsw_sp_firmware_flash(struct mlxsw_sp *mlxsw_sp,
323 const struct firmware *firmware)
325 struct mlxsw_sp_mlxfw_dev mlxsw_sp_mlxfw_dev = {
327 .ops = &mlxsw_sp_mlxfw_dev_ops,
328 .psid = mlxsw_sp->bus_info->psid,
329 .psid_size = strlen(mlxsw_sp->bus_info->psid),
334 return mlxfw_firmware_flash(&mlxsw_sp_mlxfw_dev.mlxfw_dev, firmware);
337 static int mlxsw_sp_fw_rev_validate(struct mlxsw_sp *mlxsw_sp)
339 const struct mlxsw_fw_rev *rev = &mlxsw_sp->bus_info->fw_rev;
340 const struct firmware *firmware;
343 /* Validate driver & FW are compatible */
344 if (rev->major != MLXSW_FWREV_MAJOR) {
345 WARN(1, "Mismatch in major FW version [%d:%d] is never expected; Please contact support\n",
346 rev->major, MLXSW_FWREV_MAJOR);
349 if (MLXSW_FWREV_MINOR_TO_BRANCH(rev->minor) ==
350 MLXSW_FWREV_MINOR_TO_BRANCH(MLXSW_FWREV_MINOR))
353 dev_info(mlxsw_sp->bus_info->dev, "The firmware version %d.%d.%d is incompatible with the driver\n",
354 rev->major, rev->minor, rev->subminor);
355 dev_info(mlxsw_sp->bus_info->dev, "Flashing firmware using file %s\n",
356 MLXSW_SP_FW_FILENAME);
358 err = request_firmware_direct(&firmware, MLXSW_SP_FW_FILENAME,
359 mlxsw_sp->bus_info->dev);
361 dev_err(mlxsw_sp->bus_info->dev, "Could not request firmware file %s\n",
362 MLXSW_SP_FW_FILENAME);
366 err = mlxsw_sp_firmware_flash(mlxsw_sp, firmware);
367 release_firmware(firmware);
371 int mlxsw_sp_flow_counter_get(struct mlxsw_sp *mlxsw_sp,
372 unsigned int counter_index, u64 *packets,
375 char mgpc_pl[MLXSW_REG_MGPC_LEN];
378 mlxsw_reg_mgpc_pack(mgpc_pl, counter_index, MLXSW_REG_MGPC_OPCODE_NOP,
379 MLXSW_REG_FLOW_COUNTER_SET_TYPE_PACKETS_BYTES);
380 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(mgpc), mgpc_pl);
384 *packets = mlxsw_reg_mgpc_packet_counter_get(mgpc_pl);
386 *bytes = mlxsw_reg_mgpc_byte_counter_get(mgpc_pl);
390 static int mlxsw_sp_flow_counter_clear(struct mlxsw_sp *mlxsw_sp,
391 unsigned int counter_index)
393 char mgpc_pl[MLXSW_REG_MGPC_LEN];
395 mlxsw_reg_mgpc_pack(mgpc_pl, counter_index, MLXSW_REG_MGPC_OPCODE_CLEAR,
396 MLXSW_REG_FLOW_COUNTER_SET_TYPE_PACKETS_BYTES);
397 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(mgpc), mgpc_pl);
400 int mlxsw_sp_flow_counter_alloc(struct mlxsw_sp *mlxsw_sp,
401 unsigned int *p_counter_index)
405 err = mlxsw_sp_counter_alloc(mlxsw_sp, MLXSW_SP_COUNTER_SUB_POOL_FLOW,
409 err = mlxsw_sp_flow_counter_clear(mlxsw_sp, *p_counter_index);
411 goto err_counter_clear;
415 mlxsw_sp_counter_free(mlxsw_sp, MLXSW_SP_COUNTER_SUB_POOL_FLOW,
420 void mlxsw_sp_flow_counter_free(struct mlxsw_sp *mlxsw_sp,
421 unsigned int counter_index)
423 mlxsw_sp_counter_free(mlxsw_sp, MLXSW_SP_COUNTER_SUB_POOL_FLOW,
427 static void mlxsw_sp_txhdr_construct(struct sk_buff *skb,
428 const struct mlxsw_tx_info *tx_info)
430 char *txhdr = skb_push(skb, MLXSW_TXHDR_LEN);
432 memset(txhdr, 0, MLXSW_TXHDR_LEN);
434 mlxsw_tx_hdr_version_set(txhdr, MLXSW_TXHDR_VERSION_1);
435 mlxsw_tx_hdr_ctl_set(txhdr, MLXSW_TXHDR_ETH_CTL);
436 mlxsw_tx_hdr_proto_set(txhdr, MLXSW_TXHDR_PROTO_ETH);
437 mlxsw_tx_hdr_swid_set(txhdr, 0);
438 mlxsw_tx_hdr_control_tclass_set(txhdr, 1);
439 mlxsw_tx_hdr_port_mid_set(txhdr, tx_info->local_port);
440 mlxsw_tx_hdr_type_set(txhdr, MLXSW_TXHDR_TYPE_CONTROL);
443 int mlxsw_sp_port_vid_stp_set(struct mlxsw_sp_port *mlxsw_sp_port, u16 vid,
446 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
447 enum mlxsw_reg_spms_state spms_state;
452 case BR_STATE_FORWARDING:
453 spms_state = MLXSW_REG_SPMS_STATE_FORWARDING;
455 case BR_STATE_LEARNING:
456 spms_state = MLXSW_REG_SPMS_STATE_LEARNING;
458 case BR_STATE_LISTENING: /* fall-through */
459 case BR_STATE_DISABLED: /* fall-through */
460 case BR_STATE_BLOCKING:
461 spms_state = MLXSW_REG_SPMS_STATE_DISCARDING;
467 spms_pl = kmalloc(MLXSW_REG_SPMS_LEN, GFP_KERNEL);
470 mlxsw_reg_spms_pack(spms_pl, mlxsw_sp_port->local_port);
471 mlxsw_reg_spms_vid_pack(spms_pl, vid, spms_state);
473 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(spms), spms_pl);
478 static int mlxsw_sp_base_mac_get(struct mlxsw_sp *mlxsw_sp)
480 char spad_pl[MLXSW_REG_SPAD_LEN] = {0};
483 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(spad), spad_pl);
486 mlxsw_reg_spad_base_mac_memcpy_from(spad_pl, mlxsw_sp->base_mac);
490 static int mlxsw_sp_span_init(struct mlxsw_sp *mlxsw_sp)
494 if (!MLXSW_CORE_RES_VALID(mlxsw_sp->core, MAX_SPAN))
497 mlxsw_sp->span.entries_count = MLXSW_CORE_RES_GET(mlxsw_sp->core,
499 mlxsw_sp->span.entries = kcalloc(mlxsw_sp->span.entries_count,
500 sizeof(struct mlxsw_sp_span_entry),
502 if (!mlxsw_sp->span.entries)
505 for (i = 0; i < mlxsw_sp->span.entries_count; i++)
506 INIT_LIST_HEAD(&mlxsw_sp->span.entries[i].bound_ports_list);
511 static void mlxsw_sp_span_fini(struct mlxsw_sp *mlxsw_sp)
515 for (i = 0; i < mlxsw_sp->span.entries_count; i++) {
516 struct mlxsw_sp_span_entry *curr = &mlxsw_sp->span.entries[i];
518 WARN_ON_ONCE(!list_empty(&curr->bound_ports_list));
520 kfree(mlxsw_sp->span.entries);
523 static struct mlxsw_sp_span_entry *
524 mlxsw_sp_span_entry_create(struct mlxsw_sp_port *port)
526 struct mlxsw_sp *mlxsw_sp = port->mlxsw_sp;
527 struct mlxsw_sp_span_entry *span_entry;
528 char mpat_pl[MLXSW_REG_MPAT_LEN];
529 u8 local_port = port->local_port;
534 /* find a free entry to use */
536 for (i = 0; i < mlxsw_sp->span.entries_count; i++) {
537 if (!mlxsw_sp->span.entries[i].used) {
539 span_entry = &mlxsw_sp->span.entries[i];
546 /* create a new port analayzer entry for local_port */
547 mlxsw_reg_mpat_pack(mpat_pl, index, local_port, true);
548 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(mpat), mpat_pl);
552 span_entry->used = true;
553 span_entry->id = index;
554 span_entry->ref_count = 1;
555 span_entry->local_port = local_port;
559 static void mlxsw_sp_span_entry_destroy(struct mlxsw_sp *mlxsw_sp,
560 struct mlxsw_sp_span_entry *span_entry)
562 u8 local_port = span_entry->local_port;
563 char mpat_pl[MLXSW_REG_MPAT_LEN];
564 int pa_id = span_entry->id;
566 mlxsw_reg_mpat_pack(mpat_pl, pa_id, local_port, false);
567 mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(mpat), mpat_pl);
568 span_entry->used = false;
571 struct mlxsw_sp_span_entry *
572 mlxsw_sp_span_entry_find(struct mlxsw_sp *mlxsw_sp, u8 local_port)
576 for (i = 0; i < mlxsw_sp->span.entries_count; i++) {
577 struct mlxsw_sp_span_entry *curr = &mlxsw_sp->span.entries[i];
579 if (curr->used && curr->local_port == local_port)
585 static struct mlxsw_sp_span_entry
586 *mlxsw_sp_span_entry_get(struct mlxsw_sp_port *port)
588 struct mlxsw_sp_span_entry *span_entry;
590 span_entry = mlxsw_sp_span_entry_find(port->mlxsw_sp,
593 /* Already exists, just take a reference */
594 span_entry->ref_count++;
598 return mlxsw_sp_span_entry_create(port);
601 static int mlxsw_sp_span_entry_put(struct mlxsw_sp *mlxsw_sp,
602 struct mlxsw_sp_span_entry *span_entry)
604 WARN_ON(!span_entry->ref_count);
605 if (--span_entry->ref_count == 0)
606 mlxsw_sp_span_entry_destroy(mlxsw_sp, span_entry);
610 static bool mlxsw_sp_span_is_egress_mirror(struct mlxsw_sp_port *port)
612 struct mlxsw_sp *mlxsw_sp = port->mlxsw_sp;
613 struct mlxsw_sp_span_inspected_port *p;
616 for (i = 0; i < mlxsw_sp->span.entries_count; i++) {
617 struct mlxsw_sp_span_entry *curr = &mlxsw_sp->span.entries[i];
619 list_for_each_entry(p, &curr->bound_ports_list, list)
620 if (p->local_port == port->local_port &&
621 p->type == MLXSW_SP_SPAN_EGRESS)
628 static int mlxsw_sp_span_mtu_to_buffsize(const struct mlxsw_sp *mlxsw_sp,
631 return mlxsw_sp_bytes_cells(mlxsw_sp, mtu * 5 / 2) + 1;
634 static int mlxsw_sp_span_port_mtu_update(struct mlxsw_sp_port *port, u16 mtu)
636 struct mlxsw_sp *mlxsw_sp = port->mlxsw_sp;
637 char sbib_pl[MLXSW_REG_SBIB_LEN];
640 /* If port is egress mirrored, the shared buffer size should be
641 * updated according to the mtu value
643 if (mlxsw_sp_span_is_egress_mirror(port)) {
644 u32 buffsize = mlxsw_sp_span_mtu_to_buffsize(mlxsw_sp, mtu);
646 mlxsw_reg_sbib_pack(sbib_pl, port->local_port, buffsize);
647 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sbib), sbib_pl);
649 netdev_err(port->dev, "Could not update shared buffer for mirroring\n");
657 static struct mlxsw_sp_span_inspected_port *
658 mlxsw_sp_span_entry_bound_port_find(struct mlxsw_sp_span_entry *span_entry,
659 enum mlxsw_sp_span_type type,
660 struct mlxsw_sp_port *port,
663 struct mlxsw_sp_span_inspected_port *p;
665 list_for_each_entry(p, &span_entry->bound_ports_list, list)
666 if (type == p->type &&
667 port->local_port == p->local_port &&
674 mlxsw_sp_span_inspected_port_bind(struct mlxsw_sp_port *port,
675 struct mlxsw_sp_span_entry *span_entry,
676 enum mlxsw_sp_span_type type,
679 struct mlxsw_sp *mlxsw_sp = port->mlxsw_sp;
680 char mpar_pl[MLXSW_REG_MPAR_LEN];
681 int pa_id = span_entry->id;
683 /* bind the port to the SPAN entry */
684 mlxsw_reg_mpar_pack(mpar_pl, port->local_port,
685 (enum mlxsw_reg_mpar_i_e) type, bind, pa_id);
686 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(mpar), mpar_pl);
690 mlxsw_sp_span_inspected_port_add(struct mlxsw_sp_port *port,
691 struct mlxsw_sp_span_entry *span_entry,
692 enum mlxsw_sp_span_type type,
695 struct mlxsw_sp_span_inspected_port *inspected_port;
696 struct mlxsw_sp *mlxsw_sp = port->mlxsw_sp;
697 char sbib_pl[MLXSW_REG_SBIB_LEN];
701 /* A given (source port, direction) can only be bound to one analyzer,
702 * so if a binding is requested, check for conflicts.
705 for (i = 0; i < mlxsw_sp->span.entries_count; i++) {
706 struct mlxsw_sp_span_entry *curr =
707 &mlxsw_sp->span.entries[i];
709 if (mlxsw_sp_span_entry_bound_port_find(curr, type,
714 /* if it is an egress SPAN, bind a shared buffer to it */
715 if (type == MLXSW_SP_SPAN_EGRESS) {
716 u32 buffsize = mlxsw_sp_span_mtu_to_buffsize(mlxsw_sp,
719 mlxsw_reg_sbib_pack(sbib_pl, port->local_port, buffsize);
720 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sbib), sbib_pl);
722 netdev_err(port->dev, "Could not create shared buffer for mirroring\n");
728 err = mlxsw_sp_span_inspected_port_bind(port, span_entry, type,
734 inspected_port = kzalloc(sizeof(*inspected_port), GFP_KERNEL);
735 if (!inspected_port) {
737 goto err_inspected_port_alloc;
739 inspected_port->local_port = port->local_port;
740 inspected_port->type = type;
741 inspected_port->bound = bind;
742 list_add_tail(&inspected_port->list, &span_entry->bound_ports_list);
746 err_inspected_port_alloc:
748 mlxsw_sp_span_inspected_port_bind(port, span_entry, type,
751 if (type == MLXSW_SP_SPAN_EGRESS) {
752 mlxsw_reg_sbib_pack(sbib_pl, port->local_port, 0);
753 mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sbib), sbib_pl);
759 mlxsw_sp_span_inspected_port_del(struct mlxsw_sp_port *port,
760 struct mlxsw_sp_span_entry *span_entry,
761 enum mlxsw_sp_span_type type,
764 struct mlxsw_sp_span_inspected_port *inspected_port;
765 struct mlxsw_sp *mlxsw_sp = port->mlxsw_sp;
766 char sbib_pl[MLXSW_REG_SBIB_LEN];
768 inspected_port = mlxsw_sp_span_entry_bound_port_find(span_entry, type,
774 mlxsw_sp_span_inspected_port_bind(port, span_entry, type,
776 /* remove the SBIB buffer if it was egress SPAN */
777 if (type == MLXSW_SP_SPAN_EGRESS) {
778 mlxsw_reg_sbib_pack(sbib_pl, port->local_port, 0);
779 mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sbib), sbib_pl);
782 mlxsw_sp_span_entry_put(mlxsw_sp, span_entry);
784 list_del(&inspected_port->list);
785 kfree(inspected_port);
788 int mlxsw_sp_span_mirror_add(struct mlxsw_sp_port *from,
789 struct mlxsw_sp_port *to,
790 enum mlxsw_sp_span_type type, bool bind)
792 struct mlxsw_sp *mlxsw_sp = from->mlxsw_sp;
793 struct mlxsw_sp_span_entry *span_entry;
796 span_entry = mlxsw_sp_span_entry_get(to);
800 netdev_dbg(from->dev, "Adding inspected port to SPAN entry %d\n",
803 err = mlxsw_sp_span_inspected_port_add(from, span_entry, type, bind);
810 mlxsw_sp_span_entry_put(mlxsw_sp, span_entry);
814 void mlxsw_sp_span_mirror_del(struct mlxsw_sp_port *from, u8 destination_port,
815 enum mlxsw_sp_span_type type, bool bind)
817 struct mlxsw_sp_span_entry *span_entry;
819 span_entry = mlxsw_sp_span_entry_find(from->mlxsw_sp,
822 netdev_err(from->dev, "no span entry found\n");
826 netdev_dbg(from->dev, "removing inspected port from SPAN entry %d\n",
828 mlxsw_sp_span_inspected_port_del(from, span_entry, type, bind);
831 static int mlxsw_sp_port_sample_set(struct mlxsw_sp_port *mlxsw_sp_port,
832 bool enable, u32 rate)
834 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
835 char mpsc_pl[MLXSW_REG_MPSC_LEN];
837 mlxsw_reg_mpsc_pack(mpsc_pl, mlxsw_sp_port->local_port, enable, rate);
838 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(mpsc), mpsc_pl);
841 static int mlxsw_sp_port_admin_status_set(struct mlxsw_sp_port *mlxsw_sp_port,
844 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
845 char paos_pl[MLXSW_REG_PAOS_LEN];
847 mlxsw_reg_paos_pack(paos_pl, mlxsw_sp_port->local_port,
848 is_up ? MLXSW_PORT_ADMIN_STATUS_UP :
849 MLXSW_PORT_ADMIN_STATUS_DOWN);
850 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(paos), paos_pl);
853 static int mlxsw_sp_port_dev_addr_set(struct mlxsw_sp_port *mlxsw_sp_port,
856 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
857 char ppad_pl[MLXSW_REG_PPAD_LEN];
859 mlxsw_reg_ppad_pack(ppad_pl, true, mlxsw_sp_port->local_port);
860 mlxsw_reg_ppad_mac_memcpy_to(ppad_pl, addr);
861 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ppad), ppad_pl);
864 static int mlxsw_sp_port_dev_addr_init(struct mlxsw_sp_port *mlxsw_sp_port)
866 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
867 unsigned char *addr = mlxsw_sp_port->dev->dev_addr;
869 ether_addr_copy(addr, mlxsw_sp->base_mac);
870 addr[ETH_ALEN - 1] += mlxsw_sp_port->local_port;
871 return mlxsw_sp_port_dev_addr_set(mlxsw_sp_port, addr);
874 static int mlxsw_sp_port_mtu_set(struct mlxsw_sp_port *mlxsw_sp_port, u16 mtu)
876 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
877 char pmtu_pl[MLXSW_REG_PMTU_LEN];
881 mtu += MLXSW_TXHDR_LEN + ETH_HLEN;
882 mlxsw_reg_pmtu_pack(pmtu_pl, mlxsw_sp_port->local_port, 0);
883 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(pmtu), pmtu_pl);
886 max_mtu = mlxsw_reg_pmtu_max_mtu_get(pmtu_pl);
891 mlxsw_reg_pmtu_pack(pmtu_pl, mlxsw_sp_port->local_port, mtu);
892 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(pmtu), pmtu_pl);
895 static int mlxsw_sp_port_swid_set(struct mlxsw_sp_port *mlxsw_sp_port, u8 swid)
897 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
898 char pspa_pl[MLXSW_REG_PSPA_LEN];
900 mlxsw_reg_pspa_pack(pspa_pl, swid, mlxsw_sp_port->local_port);
901 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(pspa), pspa_pl);
904 int mlxsw_sp_port_vp_mode_set(struct mlxsw_sp_port *mlxsw_sp_port, bool enable)
906 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
907 char svpe_pl[MLXSW_REG_SVPE_LEN];
909 mlxsw_reg_svpe_pack(svpe_pl, mlxsw_sp_port->local_port, enable);
910 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(svpe), svpe_pl);
913 int mlxsw_sp_port_vid_learning_set(struct mlxsw_sp_port *mlxsw_sp_port, u16 vid,
916 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
920 spvmlr_pl = kmalloc(MLXSW_REG_SPVMLR_LEN, GFP_KERNEL);
923 mlxsw_reg_spvmlr_pack(spvmlr_pl, mlxsw_sp_port->local_port, vid, vid,
925 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(spvmlr), spvmlr_pl);
930 static int __mlxsw_sp_port_pvid_set(struct mlxsw_sp_port *mlxsw_sp_port,
933 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
934 char spvid_pl[MLXSW_REG_SPVID_LEN];
936 mlxsw_reg_spvid_pack(spvid_pl, mlxsw_sp_port->local_port, vid);
937 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(spvid), spvid_pl);
940 static int mlxsw_sp_port_allow_untagged_set(struct mlxsw_sp_port *mlxsw_sp_port,
943 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
944 char spaft_pl[MLXSW_REG_SPAFT_LEN];
946 mlxsw_reg_spaft_pack(spaft_pl, mlxsw_sp_port->local_port, allow);
947 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(spaft), spaft_pl);
950 int mlxsw_sp_port_pvid_set(struct mlxsw_sp_port *mlxsw_sp_port, u16 vid)
955 err = mlxsw_sp_port_allow_untagged_set(mlxsw_sp_port, false);
959 err = __mlxsw_sp_port_pvid_set(mlxsw_sp_port, vid);
962 err = mlxsw_sp_port_allow_untagged_set(mlxsw_sp_port, true);
964 goto err_port_allow_untagged_set;
967 mlxsw_sp_port->pvid = vid;
970 err_port_allow_untagged_set:
971 __mlxsw_sp_port_pvid_set(mlxsw_sp_port, mlxsw_sp_port->pvid);
976 mlxsw_sp_port_system_port_mapping_set(struct mlxsw_sp_port *mlxsw_sp_port)
978 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
979 char sspr_pl[MLXSW_REG_SSPR_LEN];
981 mlxsw_reg_sspr_pack(sspr_pl, mlxsw_sp_port->local_port);
982 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sspr), sspr_pl);
985 static int mlxsw_sp_port_module_info_get(struct mlxsw_sp *mlxsw_sp,
986 u8 local_port, u8 *p_module,
987 u8 *p_width, u8 *p_lane)
989 char pmlp_pl[MLXSW_REG_PMLP_LEN];
992 mlxsw_reg_pmlp_pack(pmlp_pl, local_port);
993 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(pmlp), pmlp_pl);
996 *p_module = mlxsw_reg_pmlp_module_get(pmlp_pl, 0);
997 *p_width = mlxsw_reg_pmlp_width_get(pmlp_pl);
998 *p_lane = mlxsw_reg_pmlp_tx_lane_get(pmlp_pl, 0);
1002 static int mlxsw_sp_port_module_map(struct mlxsw_sp_port *mlxsw_sp_port,
1003 u8 module, u8 width, u8 lane)
1005 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1006 char pmlp_pl[MLXSW_REG_PMLP_LEN];
1009 mlxsw_reg_pmlp_pack(pmlp_pl, mlxsw_sp_port->local_port);
1010 mlxsw_reg_pmlp_width_set(pmlp_pl, width);
1011 for (i = 0; i < width; i++) {
1012 mlxsw_reg_pmlp_module_set(pmlp_pl, i, module);
1013 mlxsw_reg_pmlp_tx_lane_set(pmlp_pl, i, lane + i); /* Rx & Tx */
1016 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(pmlp), pmlp_pl);
1019 static int mlxsw_sp_port_module_unmap(struct mlxsw_sp_port *mlxsw_sp_port)
1021 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1022 char pmlp_pl[MLXSW_REG_PMLP_LEN];
1024 mlxsw_reg_pmlp_pack(pmlp_pl, mlxsw_sp_port->local_port);
1025 mlxsw_reg_pmlp_width_set(pmlp_pl, 0);
1026 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(pmlp), pmlp_pl);
1029 static int mlxsw_sp_port_open(struct net_device *dev)
1031 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1034 err = mlxsw_sp_port_admin_status_set(mlxsw_sp_port, true);
1037 netif_start_queue(dev);
1041 static int mlxsw_sp_port_stop(struct net_device *dev)
1043 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1045 netif_stop_queue(dev);
1046 return mlxsw_sp_port_admin_status_set(mlxsw_sp_port, false);
1049 static netdev_tx_t mlxsw_sp_port_xmit(struct sk_buff *skb,
1050 struct net_device *dev)
1052 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1053 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1054 struct mlxsw_sp_port_pcpu_stats *pcpu_stats;
1055 const struct mlxsw_tx_info tx_info = {
1056 .local_port = mlxsw_sp_port->local_port,
1062 if (mlxsw_core_skb_transmit_busy(mlxsw_sp->core, &tx_info))
1063 return NETDEV_TX_BUSY;
1065 if (unlikely(skb_headroom(skb) < MLXSW_TXHDR_LEN)) {
1066 struct sk_buff *skb_orig = skb;
1068 skb = skb_realloc_headroom(skb, MLXSW_TXHDR_LEN);
1070 this_cpu_inc(mlxsw_sp_port->pcpu_stats->tx_dropped);
1071 dev_kfree_skb_any(skb_orig);
1072 return NETDEV_TX_OK;
1074 dev_consume_skb_any(skb_orig);
1077 if (eth_skb_pad(skb)) {
1078 this_cpu_inc(mlxsw_sp_port->pcpu_stats->tx_dropped);
1079 return NETDEV_TX_OK;
1082 mlxsw_sp_txhdr_construct(skb, &tx_info);
1083 /* TX header is consumed by HW on the way so we shouldn't count its
1084 * bytes as being sent.
1086 len = skb->len - MLXSW_TXHDR_LEN;
1088 /* Due to a race we might fail here because of a full queue. In that
1089 * unlikely case we simply drop the packet.
1091 err = mlxsw_core_skb_transmit(mlxsw_sp->core, skb, &tx_info);
1094 pcpu_stats = this_cpu_ptr(mlxsw_sp_port->pcpu_stats);
1095 u64_stats_update_begin(&pcpu_stats->syncp);
1096 pcpu_stats->tx_packets++;
1097 pcpu_stats->tx_bytes += len;
1098 u64_stats_update_end(&pcpu_stats->syncp);
1100 this_cpu_inc(mlxsw_sp_port->pcpu_stats->tx_dropped);
1101 dev_kfree_skb_any(skb);
1103 return NETDEV_TX_OK;
1106 static void mlxsw_sp_set_rx_mode(struct net_device *dev)
1110 static int mlxsw_sp_port_set_mac_address(struct net_device *dev, void *p)
1112 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1113 struct sockaddr *addr = p;
1116 if (!is_valid_ether_addr(addr->sa_data))
1117 return -EADDRNOTAVAIL;
1119 err = mlxsw_sp_port_dev_addr_set(mlxsw_sp_port, addr->sa_data);
1122 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
1126 static u16 mlxsw_sp_pg_buf_threshold_get(const struct mlxsw_sp *mlxsw_sp,
1129 return 2 * mlxsw_sp_bytes_cells(mlxsw_sp, mtu);
1132 #define MLXSW_SP_CELL_FACTOR 2 /* 2 * cell_size / (IPG + cell_size + 1) */
1134 static u16 mlxsw_sp_pfc_delay_get(const struct mlxsw_sp *mlxsw_sp, int mtu,
1137 delay = mlxsw_sp_bytes_cells(mlxsw_sp, DIV_ROUND_UP(delay,
1139 return MLXSW_SP_CELL_FACTOR * delay + mlxsw_sp_bytes_cells(mlxsw_sp,
1143 /* Maximum delay buffer needed in case of PAUSE frames, in bytes.
1144 * Assumes 100m cable and maximum MTU.
1146 #define MLXSW_SP_PAUSE_DELAY 58752
1148 static u16 mlxsw_sp_pg_buf_delay_get(const struct mlxsw_sp *mlxsw_sp, int mtu,
1149 u16 delay, bool pfc, bool pause)
1152 return mlxsw_sp_pfc_delay_get(mlxsw_sp, mtu, delay);
1154 return mlxsw_sp_bytes_cells(mlxsw_sp, MLXSW_SP_PAUSE_DELAY);
1159 static void mlxsw_sp_pg_buf_pack(char *pbmc_pl, int index, u16 size, u16 thres,
1163 mlxsw_reg_pbmc_lossy_buffer_pack(pbmc_pl, index, size);
1165 mlxsw_reg_pbmc_lossless_buffer_pack(pbmc_pl, index, size,
1169 int __mlxsw_sp_port_headroom_set(struct mlxsw_sp_port *mlxsw_sp_port, int mtu,
1170 u8 *prio_tc, bool pause_en,
1171 struct ieee_pfc *my_pfc)
1173 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1174 u8 pfc_en = !!my_pfc ? my_pfc->pfc_en : 0;
1175 u16 delay = !!my_pfc ? my_pfc->delay : 0;
1176 char pbmc_pl[MLXSW_REG_PBMC_LEN];
1179 mlxsw_reg_pbmc_pack(pbmc_pl, mlxsw_sp_port->local_port, 0, 0);
1180 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(pbmc), pbmc_pl);
1184 for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
1185 bool configure = false;
1190 for (j = 0; j < IEEE_8021QAZ_MAX_TCS; j++) {
1191 if (prio_tc[j] == i) {
1192 pfc = pfc_en & BIT(j);
1201 lossy = !(pfc || pause_en);
1202 thres = mlxsw_sp_pg_buf_threshold_get(mlxsw_sp, mtu);
1203 delay = mlxsw_sp_pg_buf_delay_get(mlxsw_sp, mtu, delay, pfc,
1205 mlxsw_sp_pg_buf_pack(pbmc_pl, i, thres + delay, thres, lossy);
1208 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(pbmc), pbmc_pl);
1211 static int mlxsw_sp_port_headroom_set(struct mlxsw_sp_port *mlxsw_sp_port,
1212 int mtu, bool pause_en)
1214 u8 def_prio_tc[IEEE_8021QAZ_MAX_TCS] = {0};
1215 bool dcb_en = !!mlxsw_sp_port->dcb.ets;
1216 struct ieee_pfc *my_pfc;
1219 prio_tc = dcb_en ? mlxsw_sp_port->dcb.ets->prio_tc : def_prio_tc;
1220 my_pfc = dcb_en ? mlxsw_sp_port->dcb.pfc : NULL;
1222 return __mlxsw_sp_port_headroom_set(mlxsw_sp_port, mtu, prio_tc,
1226 static int mlxsw_sp_port_change_mtu(struct net_device *dev, int mtu)
1228 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1229 bool pause_en = mlxsw_sp_port_is_pause_en(mlxsw_sp_port);
1232 err = mlxsw_sp_port_headroom_set(mlxsw_sp_port, mtu, pause_en);
1235 err = mlxsw_sp_span_port_mtu_update(mlxsw_sp_port, mtu);
1237 goto err_span_port_mtu_update;
1238 err = mlxsw_sp_port_mtu_set(mlxsw_sp_port, mtu);
1240 goto err_port_mtu_set;
1245 mlxsw_sp_span_port_mtu_update(mlxsw_sp_port, dev->mtu);
1246 err_span_port_mtu_update:
1247 mlxsw_sp_port_headroom_set(mlxsw_sp_port, dev->mtu, pause_en);
1252 mlxsw_sp_port_get_sw_stats64(const struct net_device *dev,
1253 struct rtnl_link_stats64 *stats)
1255 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1256 struct mlxsw_sp_port_pcpu_stats *p;
1257 u64 rx_packets, rx_bytes, tx_packets, tx_bytes;
1262 for_each_possible_cpu(i) {
1263 p = per_cpu_ptr(mlxsw_sp_port->pcpu_stats, i);
1265 start = u64_stats_fetch_begin_irq(&p->syncp);
1266 rx_packets = p->rx_packets;
1267 rx_bytes = p->rx_bytes;
1268 tx_packets = p->tx_packets;
1269 tx_bytes = p->tx_bytes;
1270 } while (u64_stats_fetch_retry_irq(&p->syncp, start));
1272 stats->rx_packets += rx_packets;
1273 stats->rx_bytes += rx_bytes;
1274 stats->tx_packets += tx_packets;
1275 stats->tx_bytes += tx_bytes;
1276 /* tx_dropped is u32, updated without syncp protection. */
1277 tx_dropped += p->tx_dropped;
1279 stats->tx_dropped = tx_dropped;
1283 static bool mlxsw_sp_port_has_offload_stats(const struct net_device *dev, int attr_id)
1286 case IFLA_OFFLOAD_XSTATS_CPU_HIT:
1293 static int mlxsw_sp_port_get_offload_stats(int attr_id, const struct net_device *dev,
1297 case IFLA_OFFLOAD_XSTATS_CPU_HIT:
1298 return mlxsw_sp_port_get_sw_stats64(dev, sp);
1304 static int mlxsw_sp_port_get_stats_raw(struct net_device *dev, int grp,
1305 int prio, char *ppcnt_pl)
1307 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1308 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1310 mlxsw_reg_ppcnt_pack(ppcnt_pl, mlxsw_sp_port->local_port, grp, prio);
1311 return mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(ppcnt), ppcnt_pl);
1314 static int mlxsw_sp_port_get_hw_stats(struct net_device *dev,
1315 struct rtnl_link_stats64 *stats)
1317 char ppcnt_pl[MLXSW_REG_PPCNT_LEN];
1320 err = mlxsw_sp_port_get_stats_raw(dev, MLXSW_REG_PPCNT_IEEE_8023_CNT,
1326 mlxsw_reg_ppcnt_a_frames_transmitted_ok_get(ppcnt_pl);
1328 mlxsw_reg_ppcnt_a_frames_received_ok_get(ppcnt_pl);
1330 mlxsw_reg_ppcnt_a_octets_transmitted_ok_get(ppcnt_pl);
1332 mlxsw_reg_ppcnt_a_octets_received_ok_get(ppcnt_pl);
1334 mlxsw_reg_ppcnt_a_multicast_frames_received_ok_get(ppcnt_pl);
1336 stats->rx_crc_errors =
1337 mlxsw_reg_ppcnt_a_frame_check_sequence_errors_get(ppcnt_pl);
1338 stats->rx_frame_errors =
1339 mlxsw_reg_ppcnt_a_alignment_errors_get(ppcnt_pl);
1341 stats->rx_length_errors = (
1342 mlxsw_reg_ppcnt_a_in_range_length_errors_get(ppcnt_pl) +
1343 mlxsw_reg_ppcnt_a_out_of_range_length_field_get(ppcnt_pl) +
1344 mlxsw_reg_ppcnt_a_frame_too_long_errors_get(ppcnt_pl));
1346 stats->rx_errors = (stats->rx_crc_errors +
1347 stats->rx_frame_errors + stats->rx_length_errors);
1354 mlxsw_sp_port_get_hw_xstats(struct net_device *dev,
1355 struct mlxsw_sp_port_xstats *xstats)
1357 char ppcnt_pl[MLXSW_REG_PPCNT_LEN];
1360 err = mlxsw_sp_port_get_stats_raw(dev, MLXSW_REG_PPCNT_EXT_CNT, 0,
1363 xstats->ecn = mlxsw_reg_ppcnt_ecn_marked_get(ppcnt_pl);
1365 for (i = 0; i < TC_MAX_QUEUE; i++) {
1366 err = mlxsw_sp_port_get_stats_raw(dev,
1367 MLXSW_REG_PPCNT_TC_CONG_TC,
1370 xstats->wred_drop[i] =
1371 mlxsw_reg_ppcnt_wred_discard_get(ppcnt_pl);
1373 err = mlxsw_sp_port_get_stats_raw(dev, MLXSW_REG_PPCNT_TC_CNT,
1378 xstats->backlog[i] =
1379 mlxsw_reg_ppcnt_tc_transmit_queue_get(ppcnt_pl);
1380 xstats->tail_drop[i] =
1381 mlxsw_reg_ppcnt_tc_no_buffer_discard_uc_get(ppcnt_pl);
1385 static void update_stats_cache(struct work_struct *work)
1387 struct mlxsw_sp_port *mlxsw_sp_port =
1388 container_of(work, struct mlxsw_sp_port,
1389 periodic_hw_stats.update_dw.work);
1391 if (!netif_carrier_ok(mlxsw_sp_port->dev))
1394 mlxsw_sp_port_get_hw_stats(mlxsw_sp_port->dev,
1395 &mlxsw_sp_port->periodic_hw_stats.stats);
1396 mlxsw_sp_port_get_hw_xstats(mlxsw_sp_port->dev,
1397 &mlxsw_sp_port->periodic_hw_stats.xstats);
1400 mlxsw_core_schedule_dw(&mlxsw_sp_port->periodic_hw_stats.update_dw,
1401 MLXSW_HW_STATS_UPDATE_TIME);
1404 /* Return the stats from a cache that is updated periodically,
1405 * as this function might get called in an atomic context.
1408 mlxsw_sp_port_get_stats64(struct net_device *dev,
1409 struct rtnl_link_stats64 *stats)
1411 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1413 memcpy(stats, &mlxsw_sp_port->periodic_hw_stats.stats, sizeof(*stats));
1416 static int __mlxsw_sp_port_vlan_set(struct mlxsw_sp_port *mlxsw_sp_port,
1417 u16 vid_begin, u16 vid_end,
1418 bool is_member, bool untagged)
1420 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1424 spvm_pl = kmalloc(MLXSW_REG_SPVM_LEN, GFP_KERNEL);
1428 mlxsw_reg_spvm_pack(spvm_pl, mlxsw_sp_port->local_port, vid_begin,
1429 vid_end, is_member, untagged);
1430 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(spvm), spvm_pl);
1435 int mlxsw_sp_port_vlan_set(struct mlxsw_sp_port *mlxsw_sp_port, u16 vid_begin,
1436 u16 vid_end, bool is_member, bool untagged)
1441 for (vid = vid_begin; vid <= vid_end;
1442 vid += MLXSW_REG_SPVM_REC_MAX_COUNT) {
1443 vid_e = min((u16) (vid + MLXSW_REG_SPVM_REC_MAX_COUNT - 1),
1446 err = __mlxsw_sp_port_vlan_set(mlxsw_sp_port, vid, vid_e,
1447 is_member, untagged);
1455 static void mlxsw_sp_port_vlan_flush(struct mlxsw_sp_port *mlxsw_sp_port)
1457 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan, *tmp;
1459 list_for_each_entry_safe(mlxsw_sp_port_vlan, tmp,
1460 &mlxsw_sp_port->vlans_list, list)
1461 mlxsw_sp_port_vlan_put(mlxsw_sp_port_vlan);
1464 static struct mlxsw_sp_port_vlan *
1465 mlxsw_sp_port_vlan_create(struct mlxsw_sp_port *mlxsw_sp_port, u16 vid)
1467 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1468 bool untagged = vid == 1;
1471 err = mlxsw_sp_port_vlan_set(mlxsw_sp_port, vid, vid, true, untagged);
1473 return ERR_PTR(err);
1475 mlxsw_sp_port_vlan = kzalloc(sizeof(*mlxsw_sp_port_vlan), GFP_KERNEL);
1476 if (!mlxsw_sp_port_vlan) {
1478 goto err_port_vlan_alloc;
1481 mlxsw_sp_port_vlan->mlxsw_sp_port = mlxsw_sp_port;
1482 mlxsw_sp_port_vlan->ref_count = 1;
1483 mlxsw_sp_port_vlan->vid = vid;
1484 list_add(&mlxsw_sp_port_vlan->list, &mlxsw_sp_port->vlans_list);
1486 return mlxsw_sp_port_vlan;
1488 err_port_vlan_alloc:
1489 mlxsw_sp_port_vlan_set(mlxsw_sp_port, vid, vid, false, false);
1490 return ERR_PTR(err);
1494 mlxsw_sp_port_vlan_destroy(struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan)
1496 struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp_port_vlan->mlxsw_sp_port;
1497 u16 vid = mlxsw_sp_port_vlan->vid;
1499 list_del(&mlxsw_sp_port_vlan->list);
1500 kfree(mlxsw_sp_port_vlan);
1501 mlxsw_sp_port_vlan_set(mlxsw_sp_port, vid, vid, false, false);
1504 struct mlxsw_sp_port_vlan *
1505 mlxsw_sp_port_vlan_get(struct mlxsw_sp_port *mlxsw_sp_port, u16 vid)
1507 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1509 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port, vid);
1510 if (mlxsw_sp_port_vlan) {
1511 mlxsw_sp_port_vlan->ref_count++;
1512 return mlxsw_sp_port_vlan;
1515 return mlxsw_sp_port_vlan_create(mlxsw_sp_port, vid);
1518 void mlxsw_sp_port_vlan_put(struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan)
1520 struct mlxsw_sp_fid *fid = mlxsw_sp_port_vlan->fid;
1522 if (--mlxsw_sp_port_vlan->ref_count != 0)
1525 if (mlxsw_sp_port_vlan->bridge_port)
1526 mlxsw_sp_port_vlan_bridge_leave(mlxsw_sp_port_vlan);
1528 mlxsw_sp_port_vlan_router_leave(mlxsw_sp_port_vlan);
1530 mlxsw_sp_port_vlan_destroy(mlxsw_sp_port_vlan);
1533 static int mlxsw_sp_port_add_vid(struct net_device *dev,
1534 __be16 __always_unused proto, u16 vid)
1536 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1538 /* VLAN 0 is added to HW filter when device goes up, but it is
1539 * reserved in our case, so simply return.
1544 return PTR_ERR_OR_ZERO(mlxsw_sp_port_vlan_get(mlxsw_sp_port, vid));
1547 static int mlxsw_sp_port_kill_vid(struct net_device *dev,
1548 __be16 __always_unused proto, u16 vid)
1550 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1551 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1553 /* VLAN 0 is removed from HW filter when device goes down, but
1554 * it is reserved in our case, so simply return.
1559 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port, vid);
1560 if (!mlxsw_sp_port_vlan)
1562 mlxsw_sp_port_vlan_put(mlxsw_sp_port_vlan);
1567 static int mlxsw_sp_port_get_phys_port_name(struct net_device *dev, char *name,
1570 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1571 u8 module = mlxsw_sp_port->mapping.module;
1572 u8 width = mlxsw_sp_port->mapping.width;
1573 u8 lane = mlxsw_sp_port->mapping.lane;
1576 if (!mlxsw_sp_port->split)
1577 err = snprintf(name, len, "p%d", module + 1);
1579 err = snprintf(name, len, "p%ds%d", module + 1,
1588 static struct mlxsw_sp_port_mall_tc_entry *
1589 mlxsw_sp_port_mall_tc_entry_find(struct mlxsw_sp_port *port,
1590 unsigned long cookie) {
1591 struct mlxsw_sp_port_mall_tc_entry *mall_tc_entry;
1593 list_for_each_entry(mall_tc_entry, &port->mall_tc_list, list)
1594 if (mall_tc_entry->cookie == cookie)
1595 return mall_tc_entry;
1601 mlxsw_sp_port_add_cls_matchall_mirror(struct mlxsw_sp_port *mlxsw_sp_port,
1602 struct mlxsw_sp_port_mall_mirror_tc_entry *mirror,
1603 const struct tc_action *a,
1606 enum mlxsw_sp_span_type span_type;
1607 struct mlxsw_sp_port *to_port;
1608 struct net_device *to_dev;
1610 to_dev = tcf_mirred_dev(a);
1612 netdev_err(mlxsw_sp_port->dev, "Could not find requested device\n");
1616 if (!mlxsw_sp_port_dev_check(to_dev)) {
1617 netdev_err(mlxsw_sp_port->dev, "Cannot mirror to a non-spectrum port");
1620 to_port = netdev_priv(to_dev);
1622 mirror->to_local_port = to_port->local_port;
1623 mirror->ingress = ingress;
1624 span_type = ingress ? MLXSW_SP_SPAN_INGRESS : MLXSW_SP_SPAN_EGRESS;
1625 return mlxsw_sp_span_mirror_add(mlxsw_sp_port, to_port, span_type,
1630 mlxsw_sp_port_del_cls_matchall_mirror(struct mlxsw_sp_port *mlxsw_sp_port,
1631 struct mlxsw_sp_port_mall_mirror_tc_entry *mirror)
1633 enum mlxsw_sp_span_type span_type;
1635 span_type = mirror->ingress ?
1636 MLXSW_SP_SPAN_INGRESS : MLXSW_SP_SPAN_EGRESS;
1637 mlxsw_sp_span_mirror_del(mlxsw_sp_port, mirror->to_local_port,
1642 mlxsw_sp_port_add_cls_matchall_sample(struct mlxsw_sp_port *mlxsw_sp_port,
1643 struct tc_cls_matchall_offload *cls,
1644 const struct tc_action *a,
1649 if (!mlxsw_sp_port->sample)
1651 if (rtnl_dereference(mlxsw_sp_port->sample->psample_group)) {
1652 netdev_err(mlxsw_sp_port->dev, "sample already active\n");
1655 if (tcf_sample_rate(a) > MLXSW_REG_MPSC_RATE_MAX) {
1656 netdev_err(mlxsw_sp_port->dev, "sample rate not supported\n");
1660 rcu_assign_pointer(mlxsw_sp_port->sample->psample_group,
1661 tcf_sample_psample_group(a));
1662 mlxsw_sp_port->sample->truncate = tcf_sample_truncate(a);
1663 mlxsw_sp_port->sample->trunc_size = tcf_sample_trunc_size(a);
1664 mlxsw_sp_port->sample->rate = tcf_sample_rate(a);
1666 err = mlxsw_sp_port_sample_set(mlxsw_sp_port, true, tcf_sample_rate(a));
1668 goto err_port_sample_set;
1671 err_port_sample_set:
1672 RCU_INIT_POINTER(mlxsw_sp_port->sample->psample_group, NULL);
1677 mlxsw_sp_port_del_cls_matchall_sample(struct mlxsw_sp_port *mlxsw_sp_port)
1679 if (!mlxsw_sp_port->sample)
1682 mlxsw_sp_port_sample_set(mlxsw_sp_port, false, 1);
1683 RCU_INIT_POINTER(mlxsw_sp_port->sample->psample_group, NULL);
1686 static int mlxsw_sp_port_add_cls_matchall(struct mlxsw_sp_port *mlxsw_sp_port,
1687 struct tc_cls_matchall_offload *f,
1690 struct mlxsw_sp_port_mall_tc_entry *mall_tc_entry;
1691 __be16 protocol = f->common.protocol;
1692 const struct tc_action *a;
1696 if (!tcf_exts_has_one_action(f->exts)) {
1697 netdev_err(mlxsw_sp_port->dev, "only singular actions are supported\n");
1701 mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
1704 mall_tc_entry->cookie = f->cookie;
1706 tcf_exts_to_list(f->exts, &actions);
1707 a = list_first_entry(&actions, struct tc_action, list);
1709 if (is_tcf_mirred_egress_mirror(a) && protocol == htons(ETH_P_ALL)) {
1710 struct mlxsw_sp_port_mall_mirror_tc_entry *mirror;
1712 mall_tc_entry->type = MLXSW_SP_PORT_MALL_MIRROR;
1713 mirror = &mall_tc_entry->mirror;
1714 err = mlxsw_sp_port_add_cls_matchall_mirror(mlxsw_sp_port,
1715 mirror, a, ingress);
1716 } else if (is_tcf_sample(a) && protocol == htons(ETH_P_ALL)) {
1717 mall_tc_entry->type = MLXSW_SP_PORT_MALL_SAMPLE;
1718 err = mlxsw_sp_port_add_cls_matchall_sample(mlxsw_sp_port, f,
1725 goto err_add_action;
1727 list_add_tail(&mall_tc_entry->list, &mlxsw_sp_port->mall_tc_list);
1731 kfree(mall_tc_entry);
1735 static void mlxsw_sp_port_del_cls_matchall(struct mlxsw_sp_port *mlxsw_sp_port,
1736 struct tc_cls_matchall_offload *f)
1738 struct mlxsw_sp_port_mall_tc_entry *mall_tc_entry;
1740 mall_tc_entry = mlxsw_sp_port_mall_tc_entry_find(mlxsw_sp_port,
1742 if (!mall_tc_entry) {
1743 netdev_dbg(mlxsw_sp_port->dev, "tc entry not found on port\n");
1746 list_del(&mall_tc_entry->list);
1748 switch (mall_tc_entry->type) {
1749 case MLXSW_SP_PORT_MALL_MIRROR:
1750 mlxsw_sp_port_del_cls_matchall_mirror(mlxsw_sp_port,
1751 &mall_tc_entry->mirror);
1753 case MLXSW_SP_PORT_MALL_SAMPLE:
1754 mlxsw_sp_port_del_cls_matchall_sample(mlxsw_sp_port);
1760 kfree(mall_tc_entry);
1763 static int mlxsw_sp_setup_tc_cls_matchall(struct mlxsw_sp_port *mlxsw_sp_port,
1764 struct tc_cls_matchall_offload *f,
1767 switch (f->command) {
1768 case TC_CLSMATCHALL_REPLACE:
1769 return mlxsw_sp_port_add_cls_matchall(mlxsw_sp_port, f,
1771 case TC_CLSMATCHALL_DESTROY:
1772 mlxsw_sp_port_del_cls_matchall(mlxsw_sp_port, f);
1780 mlxsw_sp_setup_tc_cls_flower(struct mlxsw_sp_acl_block *acl_block,
1781 struct tc_cls_flower_offload *f)
1783 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_acl_block_mlxsw_sp(acl_block);
1785 switch (f->command) {
1786 case TC_CLSFLOWER_REPLACE:
1787 return mlxsw_sp_flower_replace(mlxsw_sp, acl_block, f);
1788 case TC_CLSFLOWER_DESTROY:
1789 mlxsw_sp_flower_destroy(mlxsw_sp, acl_block, f);
1791 case TC_CLSFLOWER_STATS:
1792 return mlxsw_sp_flower_stats(mlxsw_sp, acl_block, f);
1798 static int mlxsw_sp_setup_tc_block_cb_matchall(enum tc_setup_type type,
1800 void *cb_priv, bool ingress)
1802 struct mlxsw_sp_port *mlxsw_sp_port = cb_priv;
1805 case TC_SETUP_CLSMATCHALL:
1806 if (!tc_cls_can_offload_and_chain0(mlxsw_sp_port->dev,
1810 return mlxsw_sp_setup_tc_cls_matchall(mlxsw_sp_port, type_data,
1812 case TC_SETUP_CLSFLOWER:
1819 static int mlxsw_sp_setup_tc_block_cb_matchall_ig(enum tc_setup_type type,
1823 return mlxsw_sp_setup_tc_block_cb_matchall(type, type_data,
1827 static int mlxsw_sp_setup_tc_block_cb_matchall_eg(enum tc_setup_type type,
1831 return mlxsw_sp_setup_tc_block_cb_matchall(type, type_data,
1835 static int mlxsw_sp_setup_tc_block_cb_flower(enum tc_setup_type type,
1836 void *type_data, void *cb_priv)
1838 struct mlxsw_sp_acl_block *acl_block = cb_priv;
1841 case TC_SETUP_CLSMATCHALL:
1843 case TC_SETUP_CLSFLOWER:
1844 if (mlxsw_sp_acl_block_disabled(acl_block))
1847 return mlxsw_sp_setup_tc_cls_flower(acl_block, type_data);
1854 mlxsw_sp_setup_tc_block_flower_bind(struct mlxsw_sp_port *mlxsw_sp_port,
1855 struct tcf_block *block, bool ingress)
1857 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1858 struct mlxsw_sp_acl_block *acl_block;
1859 struct tcf_block_cb *block_cb;
1862 block_cb = tcf_block_cb_lookup(block, mlxsw_sp_setup_tc_block_cb_flower,
1865 acl_block = mlxsw_sp_acl_block_create(mlxsw_sp, block->net);
1868 block_cb = __tcf_block_cb_register(block,
1869 mlxsw_sp_setup_tc_block_cb_flower,
1870 mlxsw_sp, acl_block);
1871 if (IS_ERR(block_cb)) {
1872 err = PTR_ERR(block_cb);
1873 goto err_cb_register;
1876 acl_block = tcf_block_cb_priv(block_cb);
1878 tcf_block_cb_incref(block_cb);
1879 err = mlxsw_sp_acl_block_bind(mlxsw_sp, acl_block,
1880 mlxsw_sp_port, ingress);
1882 goto err_block_bind;
1885 mlxsw_sp_port->ing_acl_block = acl_block;
1887 mlxsw_sp_port->eg_acl_block = acl_block;
1892 if (!tcf_block_cb_decref(block_cb)) {
1893 __tcf_block_cb_unregister(block_cb);
1895 mlxsw_sp_acl_block_destroy(acl_block);
1901 mlxsw_sp_setup_tc_block_flower_unbind(struct mlxsw_sp_port *mlxsw_sp_port,
1902 struct tcf_block *block, bool ingress)
1904 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1905 struct mlxsw_sp_acl_block *acl_block;
1906 struct tcf_block_cb *block_cb;
1909 block_cb = tcf_block_cb_lookup(block, mlxsw_sp_setup_tc_block_cb_flower,
1915 mlxsw_sp_port->ing_acl_block = NULL;
1917 mlxsw_sp_port->eg_acl_block = NULL;
1919 acl_block = tcf_block_cb_priv(block_cb);
1920 err = mlxsw_sp_acl_block_unbind(mlxsw_sp, acl_block,
1921 mlxsw_sp_port, ingress);
1922 if (!err && !tcf_block_cb_decref(block_cb)) {
1923 __tcf_block_cb_unregister(block_cb);
1924 mlxsw_sp_acl_block_destroy(acl_block);
1928 static int mlxsw_sp_setup_tc_block(struct mlxsw_sp_port *mlxsw_sp_port,
1929 struct tc_block_offload *f)
1935 if (f->binder_type == TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS) {
1936 cb = mlxsw_sp_setup_tc_block_cb_matchall_ig;
1938 } else if (f->binder_type == TCF_BLOCK_BINDER_TYPE_CLSACT_EGRESS) {
1939 cb = mlxsw_sp_setup_tc_block_cb_matchall_eg;
1945 switch (f->command) {
1947 err = tcf_block_cb_register(f->block, cb, mlxsw_sp_port,
1951 err = mlxsw_sp_setup_tc_block_flower_bind(mlxsw_sp_port,
1954 tcf_block_cb_unregister(f->block, cb, mlxsw_sp_port);
1958 case TC_BLOCK_UNBIND:
1959 mlxsw_sp_setup_tc_block_flower_unbind(mlxsw_sp_port,
1961 tcf_block_cb_unregister(f->block, cb, mlxsw_sp_port);
1968 static int mlxsw_sp_setup_tc(struct net_device *dev, enum tc_setup_type type,
1971 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1974 case TC_SETUP_BLOCK:
1975 return mlxsw_sp_setup_tc_block(mlxsw_sp_port, type_data);
1976 case TC_SETUP_QDISC_RED:
1977 return mlxsw_sp_setup_tc_red(mlxsw_sp_port, type_data);
1978 case TC_SETUP_QDISC_PRIO:
1979 return mlxsw_sp_setup_tc_prio(mlxsw_sp_port, type_data);
1986 static int mlxsw_sp_feature_hw_tc(struct net_device *dev, bool enable)
1988 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1991 if (mlxsw_sp_acl_block_rule_count(mlxsw_sp_port->ing_acl_block) ||
1992 mlxsw_sp_acl_block_rule_count(mlxsw_sp_port->eg_acl_block) ||
1993 !list_empty(&mlxsw_sp_port->mall_tc_list)) {
1994 netdev_err(dev, "Active offloaded tc filters, can't turn hw_tc_offload off\n");
1997 mlxsw_sp_acl_block_disable_inc(mlxsw_sp_port->ing_acl_block);
1998 mlxsw_sp_acl_block_disable_inc(mlxsw_sp_port->eg_acl_block);
2000 mlxsw_sp_acl_block_disable_dec(mlxsw_sp_port->ing_acl_block);
2001 mlxsw_sp_acl_block_disable_dec(mlxsw_sp_port->eg_acl_block);
2006 typedef int (*mlxsw_sp_feature_handler)(struct net_device *dev, bool enable);
2008 static int mlxsw_sp_handle_feature(struct net_device *dev,
2009 netdev_features_t wanted_features,
2010 netdev_features_t feature,
2011 mlxsw_sp_feature_handler feature_handler)
2013 netdev_features_t changes = wanted_features ^ dev->features;
2014 bool enable = !!(wanted_features & feature);
2017 if (!(changes & feature))
2020 err = feature_handler(dev, enable);
2022 netdev_err(dev, "%s feature %pNF failed, err %d\n",
2023 enable ? "Enable" : "Disable", &feature, err);
2028 dev->features |= feature;
2030 dev->features &= ~feature;
2034 static int mlxsw_sp_set_features(struct net_device *dev,
2035 netdev_features_t features)
2037 return mlxsw_sp_handle_feature(dev, features, NETIF_F_HW_TC,
2038 mlxsw_sp_feature_hw_tc);
2041 static const struct net_device_ops mlxsw_sp_port_netdev_ops = {
2042 .ndo_open = mlxsw_sp_port_open,
2043 .ndo_stop = mlxsw_sp_port_stop,
2044 .ndo_start_xmit = mlxsw_sp_port_xmit,
2045 .ndo_setup_tc = mlxsw_sp_setup_tc,
2046 .ndo_set_rx_mode = mlxsw_sp_set_rx_mode,
2047 .ndo_set_mac_address = mlxsw_sp_port_set_mac_address,
2048 .ndo_change_mtu = mlxsw_sp_port_change_mtu,
2049 .ndo_get_stats64 = mlxsw_sp_port_get_stats64,
2050 .ndo_has_offload_stats = mlxsw_sp_port_has_offload_stats,
2051 .ndo_get_offload_stats = mlxsw_sp_port_get_offload_stats,
2052 .ndo_vlan_rx_add_vid = mlxsw_sp_port_add_vid,
2053 .ndo_vlan_rx_kill_vid = mlxsw_sp_port_kill_vid,
2054 .ndo_get_phys_port_name = mlxsw_sp_port_get_phys_port_name,
2055 .ndo_set_features = mlxsw_sp_set_features,
2058 static void mlxsw_sp_port_get_drvinfo(struct net_device *dev,
2059 struct ethtool_drvinfo *drvinfo)
2061 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
2062 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
2064 strlcpy(drvinfo->driver, mlxsw_sp_driver_name, sizeof(drvinfo->driver));
2065 strlcpy(drvinfo->version, mlxsw_sp_driver_version,
2066 sizeof(drvinfo->version));
2067 snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version),
2069 mlxsw_sp->bus_info->fw_rev.major,
2070 mlxsw_sp->bus_info->fw_rev.minor,
2071 mlxsw_sp->bus_info->fw_rev.subminor);
2072 strlcpy(drvinfo->bus_info, mlxsw_sp->bus_info->device_name,
2073 sizeof(drvinfo->bus_info));
2076 static void mlxsw_sp_port_get_pauseparam(struct net_device *dev,
2077 struct ethtool_pauseparam *pause)
2079 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
2081 pause->rx_pause = mlxsw_sp_port->link.rx_pause;
2082 pause->tx_pause = mlxsw_sp_port->link.tx_pause;
2085 static int mlxsw_sp_port_pause_set(struct mlxsw_sp_port *mlxsw_sp_port,
2086 struct ethtool_pauseparam *pause)
2088 char pfcc_pl[MLXSW_REG_PFCC_LEN];
2090 mlxsw_reg_pfcc_pack(pfcc_pl, mlxsw_sp_port->local_port);
2091 mlxsw_reg_pfcc_pprx_set(pfcc_pl, pause->rx_pause);
2092 mlxsw_reg_pfcc_pptx_set(pfcc_pl, pause->tx_pause);
2094 return mlxsw_reg_write(mlxsw_sp_port->mlxsw_sp->core, MLXSW_REG(pfcc),
2098 static int mlxsw_sp_port_set_pauseparam(struct net_device *dev,
2099 struct ethtool_pauseparam *pause)
2101 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
2102 bool pause_en = pause->tx_pause || pause->rx_pause;
2105 if (mlxsw_sp_port->dcb.pfc && mlxsw_sp_port->dcb.pfc->pfc_en) {
2106 netdev_err(dev, "PFC already enabled on port\n");
2110 if (pause->autoneg) {
2111 netdev_err(dev, "PAUSE frames autonegotiation isn't supported\n");
2115 err = mlxsw_sp_port_headroom_set(mlxsw_sp_port, dev->mtu, pause_en);
2117 netdev_err(dev, "Failed to configure port's headroom\n");
2121 err = mlxsw_sp_port_pause_set(mlxsw_sp_port, pause);
2123 netdev_err(dev, "Failed to set PAUSE parameters\n");
2124 goto err_port_pause_configure;
2127 mlxsw_sp_port->link.rx_pause = pause->rx_pause;
2128 mlxsw_sp_port->link.tx_pause = pause->tx_pause;
2132 err_port_pause_configure:
2133 pause_en = mlxsw_sp_port_is_pause_en(mlxsw_sp_port);
2134 mlxsw_sp_port_headroom_set(mlxsw_sp_port, dev->mtu, pause_en);
2138 struct mlxsw_sp_port_hw_stats {
2139 char str[ETH_GSTRING_LEN];
2140 u64 (*getter)(const char *payload);
2144 static struct mlxsw_sp_port_hw_stats mlxsw_sp_port_hw_stats[] = {
2146 .str = "a_frames_transmitted_ok",
2147 .getter = mlxsw_reg_ppcnt_a_frames_transmitted_ok_get,
2150 .str = "a_frames_received_ok",
2151 .getter = mlxsw_reg_ppcnt_a_frames_received_ok_get,
2154 .str = "a_frame_check_sequence_errors",
2155 .getter = mlxsw_reg_ppcnt_a_frame_check_sequence_errors_get,
2158 .str = "a_alignment_errors",
2159 .getter = mlxsw_reg_ppcnt_a_alignment_errors_get,
2162 .str = "a_octets_transmitted_ok",
2163 .getter = mlxsw_reg_ppcnt_a_octets_transmitted_ok_get,
2166 .str = "a_octets_received_ok",
2167 .getter = mlxsw_reg_ppcnt_a_octets_received_ok_get,
2170 .str = "a_multicast_frames_xmitted_ok",
2171 .getter = mlxsw_reg_ppcnt_a_multicast_frames_xmitted_ok_get,
2174 .str = "a_broadcast_frames_xmitted_ok",
2175 .getter = mlxsw_reg_ppcnt_a_broadcast_frames_xmitted_ok_get,
2178 .str = "a_multicast_frames_received_ok",
2179 .getter = mlxsw_reg_ppcnt_a_multicast_frames_received_ok_get,
2182 .str = "a_broadcast_frames_received_ok",
2183 .getter = mlxsw_reg_ppcnt_a_broadcast_frames_received_ok_get,
2186 .str = "a_in_range_length_errors",
2187 .getter = mlxsw_reg_ppcnt_a_in_range_length_errors_get,
2190 .str = "a_out_of_range_length_field",
2191 .getter = mlxsw_reg_ppcnt_a_out_of_range_length_field_get,
2194 .str = "a_frame_too_long_errors",
2195 .getter = mlxsw_reg_ppcnt_a_frame_too_long_errors_get,
2198 .str = "a_symbol_error_during_carrier",
2199 .getter = mlxsw_reg_ppcnt_a_symbol_error_during_carrier_get,
2202 .str = "a_mac_control_frames_transmitted",
2203 .getter = mlxsw_reg_ppcnt_a_mac_control_frames_transmitted_get,
2206 .str = "a_mac_control_frames_received",
2207 .getter = mlxsw_reg_ppcnt_a_mac_control_frames_received_get,
2210 .str = "a_unsupported_opcodes_received",
2211 .getter = mlxsw_reg_ppcnt_a_unsupported_opcodes_received_get,
2214 .str = "a_pause_mac_ctrl_frames_received",
2215 .getter = mlxsw_reg_ppcnt_a_pause_mac_ctrl_frames_received_get,
2218 .str = "a_pause_mac_ctrl_frames_xmitted",
2219 .getter = mlxsw_reg_ppcnt_a_pause_mac_ctrl_frames_transmitted_get,
2223 #define MLXSW_SP_PORT_HW_STATS_LEN ARRAY_SIZE(mlxsw_sp_port_hw_stats)
2225 static struct mlxsw_sp_port_hw_stats mlxsw_sp_port_hw_prio_stats[] = {
2227 .str = "rx_octets_prio",
2228 .getter = mlxsw_reg_ppcnt_rx_octets_get,
2231 .str = "rx_frames_prio",
2232 .getter = mlxsw_reg_ppcnt_rx_frames_get,
2235 .str = "tx_octets_prio",
2236 .getter = mlxsw_reg_ppcnt_tx_octets_get,
2239 .str = "tx_frames_prio",
2240 .getter = mlxsw_reg_ppcnt_tx_frames_get,
2243 .str = "rx_pause_prio",
2244 .getter = mlxsw_reg_ppcnt_rx_pause_get,
2247 .str = "rx_pause_duration_prio",
2248 .getter = mlxsw_reg_ppcnt_rx_pause_duration_get,
2251 .str = "tx_pause_prio",
2252 .getter = mlxsw_reg_ppcnt_tx_pause_get,
2255 .str = "tx_pause_duration_prio",
2256 .getter = mlxsw_reg_ppcnt_tx_pause_duration_get,
2260 #define MLXSW_SP_PORT_HW_PRIO_STATS_LEN ARRAY_SIZE(mlxsw_sp_port_hw_prio_stats)
2262 static struct mlxsw_sp_port_hw_stats mlxsw_sp_port_hw_tc_stats[] = {
2264 .str = "tc_transmit_queue_tc",
2265 .getter = mlxsw_reg_ppcnt_tc_transmit_queue_get,
2266 .cells_bytes = true,
2269 .str = "tc_no_buffer_discard_uc_tc",
2270 .getter = mlxsw_reg_ppcnt_tc_no_buffer_discard_uc_get,
2274 #define MLXSW_SP_PORT_HW_TC_STATS_LEN ARRAY_SIZE(mlxsw_sp_port_hw_tc_stats)
2276 #define MLXSW_SP_PORT_ETHTOOL_STATS_LEN (MLXSW_SP_PORT_HW_STATS_LEN + \
2277 (MLXSW_SP_PORT_HW_PRIO_STATS_LEN + \
2278 MLXSW_SP_PORT_HW_TC_STATS_LEN) * \
2279 IEEE_8021QAZ_MAX_TCS)
2281 static void mlxsw_sp_port_get_prio_strings(u8 **p, int prio)
2285 for (i = 0; i < MLXSW_SP_PORT_HW_PRIO_STATS_LEN; i++) {
2286 snprintf(*p, ETH_GSTRING_LEN, "%s_%d",
2287 mlxsw_sp_port_hw_prio_stats[i].str, prio);
2288 *p += ETH_GSTRING_LEN;
2292 static void mlxsw_sp_port_get_tc_strings(u8 **p, int tc)
2296 for (i = 0; i < MLXSW_SP_PORT_HW_TC_STATS_LEN; i++) {
2297 snprintf(*p, ETH_GSTRING_LEN, "%s_%d",
2298 mlxsw_sp_port_hw_tc_stats[i].str, tc);
2299 *p += ETH_GSTRING_LEN;
2303 static void mlxsw_sp_port_get_strings(struct net_device *dev,
2304 u32 stringset, u8 *data)
2309 switch (stringset) {
2311 for (i = 0; i < MLXSW_SP_PORT_HW_STATS_LEN; i++) {
2312 memcpy(p, mlxsw_sp_port_hw_stats[i].str,
2314 p += ETH_GSTRING_LEN;
2317 for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++)
2318 mlxsw_sp_port_get_prio_strings(&p, i);
2320 for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++)
2321 mlxsw_sp_port_get_tc_strings(&p, i);
2327 static int mlxsw_sp_port_set_phys_id(struct net_device *dev,
2328 enum ethtool_phys_id_state state)
2330 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
2331 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
2332 char mlcr_pl[MLXSW_REG_MLCR_LEN];
2336 case ETHTOOL_ID_ACTIVE:
2339 case ETHTOOL_ID_INACTIVE:
2346 mlxsw_reg_mlcr_pack(mlcr_pl, mlxsw_sp_port->local_port, active);
2347 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(mlcr), mlcr_pl);
2351 mlxsw_sp_get_hw_stats_by_group(struct mlxsw_sp_port_hw_stats **p_hw_stats,
2352 int *p_len, enum mlxsw_reg_ppcnt_grp grp)
2355 case MLXSW_REG_PPCNT_IEEE_8023_CNT:
2356 *p_hw_stats = mlxsw_sp_port_hw_stats;
2357 *p_len = MLXSW_SP_PORT_HW_STATS_LEN;
2359 case MLXSW_REG_PPCNT_PRIO_CNT:
2360 *p_hw_stats = mlxsw_sp_port_hw_prio_stats;
2361 *p_len = MLXSW_SP_PORT_HW_PRIO_STATS_LEN;
2363 case MLXSW_REG_PPCNT_TC_CNT:
2364 *p_hw_stats = mlxsw_sp_port_hw_tc_stats;
2365 *p_len = MLXSW_SP_PORT_HW_TC_STATS_LEN;
2374 static void __mlxsw_sp_port_get_stats(struct net_device *dev,
2375 enum mlxsw_reg_ppcnt_grp grp, int prio,
2376 u64 *data, int data_index)
2378 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
2379 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
2380 struct mlxsw_sp_port_hw_stats *hw_stats;
2381 char ppcnt_pl[MLXSW_REG_PPCNT_LEN];
2385 err = mlxsw_sp_get_hw_stats_by_group(&hw_stats, &len, grp);
2388 mlxsw_sp_port_get_stats_raw(dev, grp, prio, ppcnt_pl);
2389 for (i = 0; i < len; i++) {
2390 data[data_index + i] = hw_stats[i].getter(ppcnt_pl);
2391 if (!hw_stats[i].cells_bytes)
2393 data[data_index + i] = mlxsw_sp_cells_bytes(mlxsw_sp,
2394 data[data_index + i]);
2398 static void mlxsw_sp_port_get_stats(struct net_device *dev,
2399 struct ethtool_stats *stats, u64 *data)
2401 int i, data_index = 0;
2403 /* IEEE 802.3 Counters */
2404 __mlxsw_sp_port_get_stats(dev, MLXSW_REG_PPCNT_IEEE_8023_CNT, 0,
2406 data_index = MLXSW_SP_PORT_HW_STATS_LEN;
2408 /* Per-Priority Counters */
2409 for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
2410 __mlxsw_sp_port_get_stats(dev, MLXSW_REG_PPCNT_PRIO_CNT, i,
2412 data_index += MLXSW_SP_PORT_HW_PRIO_STATS_LEN;
2415 /* Per-TC Counters */
2416 for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
2417 __mlxsw_sp_port_get_stats(dev, MLXSW_REG_PPCNT_TC_CNT, i,
2419 data_index += MLXSW_SP_PORT_HW_TC_STATS_LEN;
2423 static int mlxsw_sp_port_get_sset_count(struct net_device *dev, int sset)
2427 return MLXSW_SP_PORT_ETHTOOL_STATS_LEN;
2433 struct mlxsw_sp_port_link_mode {
2434 enum ethtool_link_mode_bit_indices mask_ethtool;
2439 static const struct mlxsw_sp_port_link_mode mlxsw_sp_port_link_mode[] = {
2441 .mask = MLXSW_REG_PTYS_ETH_SPEED_100BASE_T,
2442 .mask_ethtool = ETHTOOL_LINK_MODE_100baseT_Full_BIT,
2446 .mask = MLXSW_REG_PTYS_ETH_SPEED_SGMII |
2447 MLXSW_REG_PTYS_ETH_SPEED_1000BASE_KX,
2448 .mask_ethtool = ETHTOOL_LINK_MODE_1000baseKX_Full_BIT,
2449 .speed = SPEED_1000,
2452 .mask = MLXSW_REG_PTYS_ETH_SPEED_10GBASE_T,
2453 .mask_ethtool = ETHTOOL_LINK_MODE_10000baseT_Full_BIT,
2454 .speed = SPEED_10000,
2457 .mask = MLXSW_REG_PTYS_ETH_SPEED_10GBASE_CX4 |
2458 MLXSW_REG_PTYS_ETH_SPEED_10GBASE_KX4,
2459 .mask_ethtool = ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT,
2460 .speed = SPEED_10000,
2463 .mask = MLXSW_REG_PTYS_ETH_SPEED_10GBASE_KR |
2464 MLXSW_REG_PTYS_ETH_SPEED_10GBASE_CR |
2465 MLXSW_REG_PTYS_ETH_SPEED_10GBASE_SR |
2466 MLXSW_REG_PTYS_ETH_SPEED_10GBASE_ER_LR,
2467 .mask_ethtool = ETHTOOL_LINK_MODE_10000baseKR_Full_BIT,
2468 .speed = SPEED_10000,
2471 .mask = MLXSW_REG_PTYS_ETH_SPEED_20GBASE_KR2,
2472 .mask_ethtool = ETHTOOL_LINK_MODE_20000baseKR2_Full_BIT,
2473 .speed = SPEED_20000,
2476 .mask = MLXSW_REG_PTYS_ETH_SPEED_40GBASE_CR4,
2477 .mask_ethtool = ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT,
2478 .speed = SPEED_40000,
2481 .mask = MLXSW_REG_PTYS_ETH_SPEED_40GBASE_KR4,
2482 .mask_ethtool = ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT,
2483 .speed = SPEED_40000,
2486 .mask = MLXSW_REG_PTYS_ETH_SPEED_40GBASE_SR4,
2487 .mask_ethtool = ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT,
2488 .speed = SPEED_40000,
2491 .mask = MLXSW_REG_PTYS_ETH_SPEED_40GBASE_LR4_ER4,
2492 .mask_ethtool = ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT,
2493 .speed = SPEED_40000,
2496 .mask = MLXSW_REG_PTYS_ETH_SPEED_25GBASE_CR,
2497 .mask_ethtool = ETHTOOL_LINK_MODE_25000baseCR_Full_BIT,
2498 .speed = SPEED_25000,
2501 .mask = MLXSW_REG_PTYS_ETH_SPEED_25GBASE_KR,
2502 .mask_ethtool = ETHTOOL_LINK_MODE_25000baseKR_Full_BIT,
2503 .speed = SPEED_25000,
2506 .mask = MLXSW_REG_PTYS_ETH_SPEED_25GBASE_SR,
2507 .mask_ethtool = ETHTOOL_LINK_MODE_25000baseSR_Full_BIT,
2508 .speed = SPEED_25000,
2511 .mask = MLXSW_REG_PTYS_ETH_SPEED_25GBASE_SR,
2512 .mask_ethtool = ETHTOOL_LINK_MODE_25000baseSR_Full_BIT,
2513 .speed = SPEED_25000,
2516 .mask = MLXSW_REG_PTYS_ETH_SPEED_50GBASE_CR2,
2517 .mask_ethtool = ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT,
2518 .speed = SPEED_50000,
2521 .mask = MLXSW_REG_PTYS_ETH_SPEED_50GBASE_KR2,
2522 .mask_ethtool = ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT,
2523 .speed = SPEED_50000,
2526 .mask = MLXSW_REG_PTYS_ETH_SPEED_50GBASE_SR2,
2527 .mask_ethtool = ETHTOOL_LINK_MODE_50000baseSR2_Full_BIT,
2528 .speed = SPEED_50000,
2531 .mask = MLXSW_REG_PTYS_ETH_SPEED_56GBASE_R4,
2532 .mask_ethtool = ETHTOOL_LINK_MODE_56000baseKR4_Full_BIT,
2533 .speed = SPEED_56000,
2536 .mask = MLXSW_REG_PTYS_ETH_SPEED_56GBASE_R4,
2537 .mask_ethtool = ETHTOOL_LINK_MODE_56000baseCR4_Full_BIT,
2538 .speed = SPEED_56000,
2541 .mask = MLXSW_REG_PTYS_ETH_SPEED_56GBASE_R4,
2542 .mask_ethtool = ETHTOOL_LINK_MODE_56000baseSR4_Full_BIT,
2543 .speed = SPEED_56000,
2546 .mask = MLXSW_REG_PTYS_ETH_SPEED_56GBASE_R4,
2547 .mask_ethtool = ETHTOOL_LINK_MODE_56000baseLR4_Full_BIT,
2548 .speed = SPEED_56000,
2551 .mask = MLXSW_REG_PTYS_ETH_SPEED_100GBASE_CR4,
2552 .mask_ethtool = ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT,
2553 .speed = SPEED_100000,
2556 .mask = MLXSW_REG_PTYS_ETH_SPEED_100GBASE_SR4,
2557 .mask_ethtool = ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT,
2558 .speed = SPEED_100000,
2561 .mask = MLXSW_REG_PTYS_ETH_SPEED_100GBASE_KR4,
2562 .mask_ethtool = ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT,
2563 .speed = SPEED_100000,
2566 .mask = MLXSW_REG_PTYS_ETH_SPEED_100GBASE_LR4_ER4,
2567 .mask_ethtool = ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT,
2568 .speed = SPEED_100000,
2572 #define MLXSW_SP_PORT_LINK_MODE_LEN ARRAY_SIZE(mlxsw_sp_port_link_mode)
2575 mlxsw_sp_from_ptys_supported_port(u32 ptys_eth_proto,
2576 struct ethtool_link_ksettings *cmd)
2578 if (ptys_eth_proto & (MLXSW_REG_PTYS_ETH_SPEED_10GBASE_CR |
2579 MLXSW_REG_PTYS_ETH_SPEED_10GBASE_SR |
2580 MLXSW_REG_PTYS_ETH_SPEED_40GBASE_CR4 |
2581 MLXSW_REG_PTYS_ETH_SPEED_40GBASE_SR4 |
2582 MLXSW_REG_PTYS_ETH_SPEED_100GBASE_SR4 |
2583 MLXSW_REG_PTYS_ETH_SPEED_SGMII))
2584 ethtool_link_ksettings_add_link_mode(cmd, supported, FIBRE);
2586 if (ptys_eth_proto & (MLXSW_REG_PTYS_ETH_SPEED_10GBASE_KR |
2587 MLXSW_REG_PTYS_ETH_SPEED_10GBASE_KX4 |
2588 MLXSW_REG_PTYS_ETH_SPEED_40GBASE_KR4 |
2589 MLXSW_REG_PTYS_ETH_SPEED_100GBASE_KR4 |
2590 MLXSW_REG_PTYS_ETH_SPEED_1000BASE_KX))
2591 ethtool_link_ksettings_add_link_mode(cmd, supported, Backplane);
2594 static void mlxsw_sp_from_ptys_link(u32 ptys_eth_proto, unsigned long *mode)
2598 for (i = 0; i < MLXSW_SP_PORT_LINK_MODE_LEN; i++) {
2599 if (ptys_eth_proto & mlxsw_sp_port_link_mode[i].mask)
2600 __set_bit(mlxsw_sp_port_link_mode[i].mask_ethtool,
2605 static void mlxsw_sp_from_ptys_speed_duplex(bool carrier_ok, u32 ptys_eth_proto,
2606 struct ethtool_link_ksettings *cmd)
2608 u32 speed = SPEED_UNKNOWN;
2609 u8 duplex = DUPLEX_UNKNOWN;
2615 for (i = 0; i < MLXSW_SP_PORT_LINK_MODE_LEN; i++) {
2616 if (ptys_eth_proto & mlxsw_sp_port_link_mode[i].mask) {
2617 speed = mlxsw_sp_port_link_mode[i].speed;
2618 duplex = DUPLEX_FULL;
2623 cmd->base.speed = speed;
2624 cmd->base.duplex = duplex;
2627 static u8 mlxsw_sp_port_connector_port(u32 ptys_eth_proto)
2629 if (ptys_eth_proto & (MLXSW_REG_PTYS_ETH_SPEED_10GBASE_SR |
2630 MLXSW_REG_PTYS_ETH_SPEED_40GBASE_SR4 |
2631 MLXSW_REG_PTYS_ETH_SPEED_100GBASE_SR4 |
2632 MLXSW_REG_PTYS_ETH_SPEED_SGMII))
2635 if (ptys_eth_proto & (MLXSW_REG_PTYS_ETH_SPEED_10GBASE_CR |
2636 MLXSW_REG_PTYS_ETH_SPEED_40GBASE_CR4 |
2637 MLXSW_REG_PTYS_ETH_SPEED_100GBASE_CR4))
2640 if (ptys_eth_proto & (MLXSW_REG_PTYS_ETH_SPEED_10GBASE_KR |
2641 MLXSW_REG_PTYS_ETH_SPEED_10GBASE_KX4 |
2642 MLXSW_REG_PTYS_ETH_SPEED_40GBASE_KR4 |
2643 MLXSW_REG_PTYS_ETH_SPEED_100GBASE_KR4))
2650 mlxsw_sp_to_ptys_advert_link(const struct ethtool_link_ksettings *cmd)
2655 for (i = 0; i < MLXSW_SP_PORT_LINK_MODE_LEN; i++) {
2656 if (test_bit(mlxsw_sp_port_link_mode[i].mask_ethtool,
2657 cmd->link_modes.advertising))
2658 ptys_proto |= mlxsw_sp_port_link_mode[i].mask;
2663 static u32 mlxsw_sp_to_ptys_speed(u32 speed)
2668 for (i = 0; i < MLXSW_SP_PORT_LINK_MODE_LEN; i++) {
2669 if (speed == mlxsw_sp_port_link_mode[i].speed)
2670 ptys_proto |= mlxsw_sp_port_link_mode[i].mask;
2675 static u32 mlxsw_sp_to_ptys_upper_speed(u32 upper_speed)
2680 for (i = 0; i < MLXSW_SP_PORT_LINK_MODE_LEN; i++) {
2681 if (mlxsw_sp_port_link_mode[i].speed <= upper_speed)
2682 ptys_proto |= mlxsw_sp_port_link_mode[i].mask;
2687 static void mlxsw_sp_port_get_link_supported(u32 eth_proto_cap,
2688 struct ethtool_link_ksettings *cmd)
2690 ethtool_link_ksettings_add_link_mode(cmd, supported, Asym_Pause);
2691 ethtool_link_ksettings_add_link_mode(cmd, supported, Autoneg);
2692 ethtool_link_ksettings_add_link_mode(cmd, supported, Pause);
2694 mlxsw_sp_from_ptys_supported_port(eth_proto_cap, cmd);
2695 mlxsw_sp_from_ptys_link(eth_proto_cap, cmd->link_modes.supported);
2698 static void mlxsw_sp_port_get_link_advertise(u32 eth_proto_admin, bool autoneg,
2699 struct ethtool_link_ksettings *cmd)
2704 ethtool_link_ksettings_add_link_mode(cmd, advertising, Autoneg);
2705 mlxsw_sp_from_ptys_link(eth_proto_admin, cmd->link_modes.advertising);
2709 mlxsw_sp_port_get_link_lp_advertise(u32 eth_proto_lp, u8 autoneg_status,
2710 struct ethtool_link_ksettings *cmd)
2712 if (autoneg_status != MLXSW_REG_PTYS_AN_STATUS_OK || !eth_proto_lp)
2715 ethtool_link_ksettings_add_link_mode(cmd, lp_advertising, Autoneg);
2716 mlxsw_sp_from_ptys_link(eth_proto_lp, cmd->link_modes.lp_advertising);
2719 static int mlxsw_sp_port_get_link_ksettings(struct net_device *dev,
2720 struct ethtool_link_ksettings *cmd)
2722 u32 eth_proto_cap, eth_proto_admin, eth_proto_oper, eth_proto_lp;
2723 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
2724 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
2725 char ptys_pl[MLXSW_REG_PTYS_LEN];
2730 autoneg = mlxsw_sp_port->link.autoneg;
2731 mlxsw_reg_ptys_eth_pack(ptys_pl, mlxsw_sp_port->local_port, 0);
2732 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(ptys), ptys_pl);
2735 mlxsw_reg_ptys_eth_unpack(ptys_pl, ð_proto_cap, ð_proto_admin,
2738 mlxsw_sp_port_get_link_supported(eth_proto_cap, cmd);
2740 mlxsw_sp_port_get_link_advertise(eth_proto_admin, autoneg, cmd);
2742 eth_proto_lp = mlxsw_reg_ptys_eth_proto_lp_advertise_get(ptys_pl);
2743 autoneg_status = mlxsw_reg_ptys_an_status_get(ptys_pl);
2744 mlxsw_sp_port_get_link_lp_advertise(eth_proto_lp, autoneg_status, cmd);
2746 cmd->base.autoneg = autoneg ? AUTONEG_ENABLE : AUTONEG_DISABLE;
2747 cmd->base.port = mlxsw_sp_port_connector_port(eth_proto_oper);
2748 mlxsw_sp_from_ptys_speed_duplex(netif_carrier_ok(dev), eth_proto_oper,
2755 mlxsw_sp_port_set_link_ksettings(struct net_device *dev,
2756 const struct ethtool_link_ksettings *cmd)
2758 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
2759 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
2760 char ptys_pl[MLXSW_REG_PTYS_LEN];
2761 u32 eth_proto_cap, eth_proto_new;
2765 mlxsw_reg_ptys_eth_pack(ptys_pl, mlxsw_sp_port->local_port, 0);
2766 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(ptys), ptys_pl);
2769 mlxsw_reg_ptys_eth_unpack(ptys_pl, ð_proto_cap, NULL, NULL);
2771 autoneg = cmd->base.autoneg == AUTONEG_ENABLE;
2772 eth_proto_new = autoneg ?
2773 mlxsw_sp_to_ptys_advert_link(cmd) :
2774 mlxsw_sp_to_ptys_speed(cmd->base.speed);
2776 eth_proto_new = eth_proto_new & eth_proto_cap;
2777 if (!eth_proto_new) {
2778 netdev_err(dev, "No supported speed requested\n");
2782 mlxsw_reg_ptys_eth_pack(ptys_pl, mlxsw_sp_port->local_port,
2784 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ptys), ptys_pl);
2788 if (!netif_running(dev))
2791 mlxsw_sp_port->link.autoneg = autoneg;
2793 mlxsw_sp_port_admin_status_set(mlxsw_sp_port, false);
2794 mlxsw_sp_port_admin_status_set(mlxsw_sp_port, true);
2799 static int mlxsw_sp_flash_device(struct net_device *dev,
2800 struct ethtool_flash *flash)
2802 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
2803 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
2804 const struct firmware *firmware;
2807 if (flash->region != ETHTOOL_FLASH_ALL_REGIONS)
2813 err = request_firmware_direct(&firmware, flash->data, &dev->dev);
2816 err = mlxsw_sp_firmware_flash(mlxsw_sp, firmware);
2817 release_firmware(firmware);
2824 #define MLXSW_SP_I2C_ADDR_LOW 0x50
2825 #define MLXSW_SP_I2C_ADDR_HIGH 0x51
2826 #define MLXSW_SP_EEPROM_PAGE_LENGTH 256
2828 static int mlxsw_sp_query_module_eeprom(struct mlxsw_sp_port *mlxsw_sp_port,
2829 u16 offset, u16 size, void *data,
2830 unsigned int *p_read_size)
2832 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
2833 char eeprom_tmp[MLXSW_SP_REG_MCIA_EEPROM_SIZE];
2834 char mcia_pl[MLXSW_REG_MCIA_LEN];
2839 size = min_t(u16, size, MLXSW_SP_REG_MCIA_EEPROM_SIZE);
2841 if (offset < MLXSW_SP_EEPROM_PAGE_LENGTH &&
2842 offset + size > MLXSW_SP_EEPROM_PAGE_LENGTH)
2843 /* Cross pages read, read until offset 256 in low page */
2844 size = MLXSW_SP_EEPROM_PAGE_LENGTH - offset;
2846 i2c_addr = MLXSW_SP_I2C_ADDR_LOW;
2847 if (offset >= MLXSW_SP_EEPROM_PAGE_LENGTH) {
2848 i2c_addr = MLXSW_SP_I2C_ADDR_HIGH;
2849 offset -= MLXSW_SP_EEPROM_PAGE_LENGTH;
2852 mlxsw_reg_mcia_pack(mcia_pl, mlxsw_sp_port->mapping.module,
2853 0, 0, offset, size, i2c_addr);
2855 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(mcia), mcia_pl);
2859 status = mlxsw_reg_mcia_status_get(mcia_pl);
2863 mlxsw_reg_mcia_eeprom_memcpy_from(mcia_pl, eeprom_tmp);
2864 memcpy(data, eeprom_tmp, size);
2865 *p_read_size = size;
2870 enum mlxsw_sp_eeprom_module_info_rev_id {
2871 MLXSW_SP_EEPROM_MODULE_INFO_REV_ID_UNSPC = 0x00,
2872 MLXSW_SP_EEPROM_MODULE_INFO_REV_ID_8436 = 0x01,
2873 MLXSW_SP_EEPROM_MODULE_INFO_REV_ID_8636 = 0x03,
2876 enum mlxsw_sp_eeprom_module_info_id {
2877 MLXSW_SP_EEPROM_MODULE_INFO_ID_SFP = 0x03,
2878 MLXSW_SP_EEPROM_MODULE_INFO_ID_QSFP = 0x0C,
2879 MLXSW_SP_EEPROM_MODULE_INFO_ID_QSFP_PLUS = 0x0D,
2880 MLXSW_SP_EEPROM_MODULE_INFO_ID_QSFP28 = 0x11,
2883 enum mlxsw_sp_eeprom_module_info {
2884 MLXSW_SP_EEPROM_MODULE_INFO_ID,
2885 MLXSW_SP_EEPROM_MODULE_INFO_REV_ID,
2886 MLXSW_SP_EEPROM_MODULE_INFO_SIZE,
2889 static int mlxsw_sp_get_module_info(struct net_device *netdev,
2890 struct ethtool_modinfo *modinfo)
2892 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(netdev);
2893 u8 module_info[MLXSW_SP_EEPROM_MODULE_INFO_SIZE];
2894 u8 module_rev_id, module_id;
2895 unsigned int read_size;
2898 err = mlxsw_sp_query_module_eeprom(mlxsw_sp_port, 0,
2899 MLXSW_SP_EEPROM_MODULE_INFO_SIZE,
2900 module_info, &read_size);
2904 if (read_size < MLXSW_SP_EEPROM_MODULE_INFO_SIZE)
2907 module_rev_id = module_info[MLXSW_SP_EEPROM_MODULE_INFO_REV_ID];
2908 module_id = module_info[MLXSW_SP_EEPROM_MODULE_INFO_ID];
2910 switch (module_id) {
2911 case MLXSW_SP_EEPROM_MODULE_INFO_ID_QSFP:
2912 modinfo->type = ETH_MODULE_SFF_8436;
2913 modinfo->eeprom_len = ETH_MODULE_SFF_8436_LEN;
2915 case MLXSW_SP_EEPROM_MODULE_INFO_ID_QSFP_PLUS:
2916 case MLXSW_SP_EEPROM_MODULE_INFO_ID_QSFP28:
2917 if (module_id == MLXSW_SP_EEPROM_MODULE_INFO_ID_QSFP28 ||
2918 module_rev_id >= MLXSW_SP_EEPROM_MODULE_INFO_REV_ID_8636) {
2919 modinfo->type = ETH_MODULE_SFF_8636;
2920 modinfo->eeprom_len = ETH_MODULE_SFF_8636_LEN;
2922 modinfo->type = ETH_MODULE_SFF_8436;
2923 modinfo->eeprom_len = ETH_MODULE_SFF_8436_LEN;
2926 case MLXSW_SP_EEPROM_MODULE_INFO_ID_SFP:
2927 modinfo->type = ETH_MODULE_SFF_8472;
2928 modinfo->eeprom_len = ETH_MODULE_SFF_8472_LEN;
2937 static int mlxsw_sp_get_module_eeprom(struct net_device *netdev,
2938 struct ethtool_eeprom *ee,
2941 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(netdev);
2942 int offset = ee->offset;
2943 unsigned int read_size;
2950 memset(data, 0, ee->len);
2952 while (i < ee->len) {
2953 err = mlxsw_sp_query_module_eeprom(mlxsw_sp_port, offset,
2954 ee->len - i, data + i,
2957 netdev_err(mlxsw_sp_port->dev, "Eeprom query failed\n");
2962 offset += read_size;
2968 static const struct ethtool_ops mlxsw_sp_port_ethtool_ops = {
2969 .get_drvinfo = mlxsw_sp_port_get_drvinfo,
2970 .get_link = ethtool_op_get_link,
2971 .get_pauseparam = mlxsw_sp_port_get_pauseparam,
2972 .set_pauseparam = mlxsw_sp_port_set_pauseparam,
2973 .get_strings = mlxsw_sp_port_get_strings,
2974 .set_phys_id = mlxsw_sp_port_set_phys_id,
2975 .get_ethtool_stats = mlxsw_sp_port_get_stats,
2976 .get_sset_count = mlxsw_sp_port_get_sset_count,
2977 .get_link_ksettings = mlxsw_sp_port_get_link_ksettings,
2978 .set_link_ksettings = mlxsw_sp_port_set_link_ksettings,
2979 .flash_device = mlxsw_sp_flash_device,
2980 .get_module_info = mlxsw_sp_get_module_info,
2981 .get_module_eeprom = mlxsw_sp_get_module_eeprom,
2985 mlxsw_sp_port_speed_by_width_set(struct mlxsw_sp_port *mlxsw_sp_port, u8 width)
2987 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
2988 u32 upper_speed = MLXSW_SP_PORT_BASE_SPEED * width;
2989 char ptys_pl[MLXSW_REG_PTYS_LEN];
2990 u32 eth_proto_admin;
2992 eth_proto_admin = mlxsw_sp_to_ptys_upper_speed(upper_speed);
2993 mlxsw_reg_ptys_eth_pack(ptys_pl, mlxsw_sp_port->local_port,
2995 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ptys), ptys_pl);
2998 int mlxsw_sp_port_ets_set(struct mlxsw_sp_port *mlxsw_sp_port,
2999 enum mlxsw_reg_qeec_hr hr, u8 index, u8 next_index,
3000 bool dwrr, u8 dwrr_weight)
3002 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
3003 char qeec_pl[MLXSW_REG_QEEC_LEN];
3005 mlxsw_reg_qeec_pack(qeec_pl, mlxsw_sp_port->local_port, hr, index,
3007 mlxsw_reg_qeec_de_set(qeec_pl, true);
3008 mlxsw_reg_qeec_dwrr_set(qeec_pl, dwrr);
3009 mlxsw_reg_qeec_dwrr_weight_set(qeec_pl, dwrr_weight);
3010 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(qeec), qeec_pl);
3013 int mlxsw_sp_port_ets_maxrate_set(struct mlxsw_sp_port *mlxsw_sp_port,
3014 enum mlxsw_reg_qeec_hr hr, u8 index,
3015 u8 next_index, u32 maxrate)
3017 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
3018 char qeec_pl[MLXSW_REG_QEEC_LEN];
3020 mlxsw_reg_qeec_pack(qeec_pl, mlxsw_sp_port->local_port, hr, index,
3022 mlxsw_reg_qeec_mase_set(qeec_pl, true);
3023 mlxsw_reg_qeec_max_shaper_rate_set(qeec_pl, maxrate);
3024 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(qeec), qeec_pl);
3027 int mlxsw_sp_port_prio_tc_set(struct mlxsw_sp_port *mlxsw_sp_port,
3028 u8 switch_prio, u8 tclass)
3030 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
3031 char qtct_pl[MLXSW_REG_QTCT_LEN];
3033 mlxsw_reg_qtct_pack(qtct_pl, mlxsw_sp_port->local_port, switch_prio,
3035 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(qtct), qtct_pl);
3038 static int mlxsw_sp_port_ets_init(struct mlxsw_sp_port *mlxsw_sp_port)
3042 /* Setup the elements hierarcy, so that each TC is linked to
3043 * one subgroup, which are all member in the same group.
3045 err = mlxsw_sp_port_ets_set(mlxsw_sp_port,
3046 MLXSW_REG_QEEC_HIERARCY_GROUP, 0, 0, false,
3050 for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
3051 err = mlxsw_sp_port_ets_set(mlxsw_sp_port,
3052 MLXSW_REG_QEEC_HIERARCY_SUBGROUP, i,
3057 for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
3058 err = mlxsw_sp_port_ets_set(mlxsw_sp_port,
3059 MLXSW_REG_QEEC_HIERARCY_TC, i, i,
3065 /* Make sure the max shaper is disabled in all hierarcies that
3068 err = mlxsw_sp_port_ets_maxrate_set(mlxsw_sp_port,
3069 MLXSW_REG_QEEC_HIERARCY_PORT, 0, 0,
3070 MLXSW_REG_QEEC_MAS_DIS);
3073 for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
3074 err = mlxsw_sp_port_ets_maxrate_set(mlxsw_sp_port,
3075 MLXSW_REG_QEEC_HIERARCY_SUBGROUP,
3077 MLXSW_REG_QEEC_MAS_DIS);
3081 for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
3082 err = mlxsw_sp_port_ets_maxrate_set(mlxsw_sp_port,
3083 MLXSW_REG_QEEC_HIERARCY_TC,
3085 MLXSW_REG_QEEC_MAS_DIS);
3090 /* Map all priorities to traffic class 0. */
3091 for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
3092 err = mlxsw_sp_port_prio_tc_set(mlxsw_sp_port, i, 0);
3100 static int mlxsw_sp_port_create(struct mlxsw_sp *mlxsw_sp, u8 local_port,
3101 bool split, u8 module, u8 width, u8 lane)
3103 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
3104 struct mlxsw_sp_port *mlxsw_sp_port;
3105 struct net_device *dev;
3108 err = mlxsw_core_port_init(mlxsw_sp->core, local_port);
3110 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to init core port\n",
3115 dev = alloc_etherdev(sizeof(struct mlxsw_sp_port));
3118 goto err_alloc_etherdev;
3120 SET_NETDEV_DEV(dev, mlxsw_sp->bus_info->dev);
3121 mlxsw_sp_port = netdev_priv(dev);
3122 mlxsw_sp_port->dev = dev;
3123 mlxsw_sp_port->mlxsw_sp = mlxsw_sp;
3124 mlxsw_sp_port->local_port = local_port;
3125 mlxsw_sp_port->pvid = 1;
3126 mlxsw_sp_port->split = split;
3127 mlxsw_sp_port->mapping.module = module;
3128 mlxsw_sp_port->mapping.width = width;
3129 mlxsw_sp_port->mapping.lane = lane;
3130 mlxsw_sp_port->link.autoneg = 1;
3131 INIT_LIST_HEAD(&mlxsw_sp_port->vlans_list);
3132 INIT_LIST_HEAD(&mlxsw_sp_port->mall_tc_list);
3134 mlxsw_sp_port->pcpu_stats =
3135 netdev_alloc_pcpu_stats(struct mlxsw_sp_port_pcpu_stats);
3136 if (!mlxsw_sp_port->pcpu_stats) {
3138 goto err_alloc_stats;
3141 mlxsw_sp_port->sample = kzalloc(sizeof(*mlxsw_sp_port->sample),
3143 if (!mlxsw_sp_port->sample) {
3145 goto err_alloc_sample;
3148 INIT_DELAYED_WORK(&mlxsw_sp_port->periodic_hw_stats.update_dw,
3149 &update_stats_cache);
3151 dev->netdev_ops = &mlxsw_sp_port_netdev_ops;
3152 dev->ethtool_ops = &mlxsw_sp_port_ethtool_ops;
3154 err = mlxsw_sp_port_module_map(mlxsw_sp_port, module, width, lane);
3156 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to map module\n",
3157 mlxsw_sp_port->local_port);
3158 goto err_port_module_map;
3161 err = mlxsw_sp_port_swid_set(mlxsw_sp_port, 0);
3163 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to set SWID\n",
3164 mlxsw_sp_port->local_port);
3165 goto err_port_swid_set;
3168 err = mlxsw_sp_port_dev_addr_init(mlxsw_sp_port);
3170 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Unable to init port mac address\n",
3171 mlxsw_sp_port->local_port);
3172 goto err_dev_addr_init;
3175 netif_carrier_off(dev);
3177 dev->features |= NETIF_F_NETNS_LOCAL | NETIF_F_LLTX | NETIF_F_SG |
3178 NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_TC;
3179 dev->hw_features |= NETIF_F_HW_TC;
3182 dev->max_mtu = ETH_MAX_MTU;
3184 /* Each packet needs to have a Tx header (metadata) on top all other
3187 dev->needed_headroom = MLXSW_TXHDR_LEN;
3189 err = mlxsw_sp_port_system_port_mapping_set(mlxsw_sp_port);
3191 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to set system port mapping\n",
3192 mlxsw_sp_port->local_port);
3193 goto err_port_system_port_mapping_set;
3196 err = mlxsw_sp_port_speed_by_width_set(mlxsw_sp_port, width);
3198 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to enable speeds\n",
3199 mlxsw_sp_port->local_port);
3200 goto err_port_speed_by_width_set;
3203 err = mlxsw_sp_port_mtu_set(mlxsw_sp_port, ETH_DATA_LEN);
3205 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to set MTU\n",
3206 mlxsw_sp_port->local_port);
3207 goto err_port_mtu_set;
3210 err = mlxsw_sp_port_admin_status_set(mlxsw_sp_port, false);
3212 goto err_port_admin_status_set;
3214 err = mlxsw_sp_port_buffers_init(mlxsw_sp_port);
3216 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to initialize buffers\n",
3217 mlxsw_sp_port->local_port);
3218 goto err_port_buffers_init;
3221 err = mlxsw_sp_port_ets_init(mlxsw_sp_port);
3223 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to initialize ETS\n",
3224 mlxsw_sp_port->local_port);
3225 goto err_port_ets_init;
3228 /* ETS and buffers must be initialized before DCB. */
3229 err = mlxsw_sp_port_dcb_init(mlxsw_sp_port);
3231 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to initialize DCB\n",
3232 mlxsw_sp_port->local_port);
3233 goto err_port_dcb_init;
3236 err = mlxsw_sp_port_fids_init(mlxsw_sp_port);
3238 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to initialize FIDs\n",
3239 mlxsw_sp_port->local_port);
3240 goto err_port_fids_init;
3243 err = mlxsw_sp_tc_qdisc_init(mlxsw_sp_port);
3245 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to initialize TC qdiscs\n",
3246 mlxsw_sp_port->local_port);
3247 goto err_port_qdiscs_init;
3250 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_get(mlxsw_sp_port, 1);
3251 if (IS_ERR(mlxsw_sp_port_vlan)) {
3252 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to create VID 1\n",
3253 mlxsw_sp_port->local_port);
3254 err = PTR_ERR(mlxsw_sp_port_vlan);
3255 goto err_port_vlan_get;
3258 mlxsw_sp_port_switchdev_init(mlxsw_sp_port);
3259 mlxsw_sp->ports[local_port] = mlxsw_sp_port;
3260 err = register_netdev(dev);
3262 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to register netdev\n",
3263 mlxsw_sp_port->local_port);
3264 goto err_register_netdev;
3267 mlxsw_core_port_eth_set(mlxsw_sp->core, mlxsw_sp_port->local_port,
3268 mlxsw_sp_port, dev, mlxsw_sp_port->split,
3270 mlxsw_core_schedule_dw(&mlxsw_sp_port->periodic_hw_stats.update_dw, 0);
3273 err_register_netdev:
3274 mlxsw_sp->ports[local_port] = NULL;
3275 mlxsw_sp_port_switchdev_fini(mlxsw_sp_port);
3276 mlxsw_sp_port_vlan_put(mlxsw_sp_port_vlan);
3278 mlxsw_sp_tc_qdisc_fini(mlxsw_sp_port);
3279 err_port_qdiscs_init:
3280 mlxsw_sp_port_fids_fini(mlxsw_sp_port);
3282 mlxsw_sp_port_dcb_fini(mlxsw_sp_port);
3285 err_port_buffers_init:
3286 err_port_admin_status_set:
3288 err_port_speed_by_width_set:
3289 err_port_system_port_mapping_set:
3291 mlxsw_sp_port_swid_set(mlxsw_sp_port, MLXSW_PORT_SWID_DISABLED_PORT);
3293 mlxsw_sp_port_module_unmap(mlxsw_sp_port);
3294 err_port_module_map:
3295 kfree(mlxsw_sp_port->sample);
3297 free_percpu(mlxsw_sp_port->pcpu_stats);
3301 mlxsw_core_port_fini(mlxsw_sp->core, local_port);
3305 static void mlxsw_sp_port_remove(struct mlxsw_sp *mlxsw_sp, u8 local_port)
3307 struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp->ports[local_port];
3309 cancel_delayed_work_sync(&mlxsw_sp_port->periodic_hw_stats.update_dw);
3310 mlxsw_core_port_clear(mlxsw_sp->core, local_port, mlxsw_sp);
3311 unregister_netdev(mlxsw_sp_port->dev); /* This calls ndo_stop */
3312 mlxsw_sp->ports[local_port] = NULL;
3313 mlxsw_sp_port_switchdev_fini(mlxsw_sp_port);
3314 mlxsw_sp_port_vlan_flush(mlxsw_sp_port);
3315 mlxsw_sp_tc_qdisc_fini(mlxsw_sp_port);
3316 mlxsw_sp_port_fids_fini(mlxsw_sp_port);
3317 mlxsw_sp_port_dcb_fini(mlxsw_sp_port);
3318 mlxsw_sp_port_swid_set(mlxsw_sp_port, MLXSW_PORT_SWID_DISABLED_PORT);
3319 mlxsw_sp_port_module_unmap(mlxsw_sp_port);
3320 kfree(mlxsw_sp_port->sample);
3321 free_percpu(mlxsw_sp_port->pcpu_stats);
3322 WARN_ON_ONCE(!list_empty(&mlxsw_sp_port->vlans_list));
3323 free_netdev(mlxsw_sp_port->dev);
3324 mlxsw_core_port_fini(mlxsw_sp->core, local_port);
3327 static bool mlxsw_sp_port_created(struct mlxsw_sp *mlxsw_sp, u8 local_port)
3329 return mlxsw_sp->ports[local_port] != NULL;
3332 static void mlxsw_sp_ports_remove(struct mlxsw_sp *mlxsw_sp)
3336 for (i = 1; i < mlxsw_core_max_ports(mlxsw_sp->core); i++)
3337 if (mlxsw_sp_port_created(mlxsw_sp, i))
3338 mlxsw_sp_port_remove(mlxsw_sp, i);
3339 kfree(mlxsw_sp->port_to_module);
3340 kfree(mlxsw_sp->ports);
3343 static int mlxsw_sp_ports_create(struct mlxsw_sp *mlxsw_sp)
3345 unsigned int max_ports = mlxsw_core_max_ports(mlxsw_sp->core);
3346 u8 module, width, lane;
3351 alloc_size = sizeof(struct mlxsw_sp_port *) * max_ports;
3352 mlxsw_sp->ports = kzalloc(alloc_size, GFP_KERNEL);
3353 if (!mlxsw_sp->ports)
3356 mlxsw_sp->port_to_module = kmalloc_array(max_ports, sizeof(int),
3358 if (!mlxsw_sp->port_to_module) {
3360 goto err_port_to_module_alloc;
3363 for (i = 1; i < max_ports; i++) {
3364 /* Mark as invalid */
3365 mlxsw_sp->port_to_module[i] = -1;
3367 err = mlxsw_sp_port_module_info_get(mlxsw_sp, i, &module,
3370 goto err_port_module_info_get;
3373 mlxsw_sp->port_to_module[i] = module;
3374 err = mlxsw_sp_port_create(mlxsw_sp, i, false,
3375 module, width, lane);
3377 goto err_port_create;
3382 err_port_module_info_get:
3383 for (i--; i >= 1; i--)
3384 if (mlxsw_sp_port_created(mlxsw_sp, i))
3385 mlxsw_sp_port_remove(mlxsw_sp, i);
3386 kfree(mlxsw_sp->port_to_module);
3387 err_port_to_module_alloc:
3388 kfree(mlxsw_sp->ports);
3392 static u8 mlxsw_sp_cluster_base_port_get(u8 local_port)
3394 u8 offset = (local_port - 1) % MLXSW_SP_PORTS_PER_CLUSTER_MAX;
3396 return local_port - offset;
3399 static int mlxsw_sp_port_split_create(struct mlxsw_sp *mlxsw_sp, u8 base_port,
3400 u8 module, unsigned int count)
3402 u8 width = MLXSW_PORT_MODULE_MAX_WIDTH / count;
3405 for (i = 0; i < count; i++) {
3406 err = mlxsw_sp_port_create(mlxsw_sp, base_port + i, true,
3407 module, width, i * width);
3409 goto err_port_create;
3415 for (i--; i >= 0; i--)
3416 if (mlxsw_sp_port_created(mlxsw_sp, base_port + i))
3417 mlxsw_sp_port_remove(mlxsw_sp, base_port + i);
3421 static void mlxsw_sp_port_unsplit_create(struct mlxsw_sp *mlxsw_sp,
3422 u8 base_port, unsigned int count)
3424 u8 local_port, module, width = MLXSW_PORT_MODULE_MAX_WIDTH;
3427 /* Split by four means we need to re-create two ports, otherwise
3432 for (i = 0; i < count; i++) {
3433 local_port = base_port + i * 2;
3434 if (mlxsw_sp->port_to_module[local_port] < 0)
3436 module = mlxsw_sp->port_to_module[local_port];
3438 mlxsw_sp_port_create(mlxsw_sp, local_port, false, module,
3443 static int mlxsw_sp_port_split(struct mlxsw_core *mlxsw_core, u8 local_port,
3446 struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
3447 struct mlxsw_sp_port *mlxsw_sp_port;
3448 u8 module, cur_width, base_port;
3452 mlxsw_sp_port = mlxsw_sp->ports[local_port];
3453 if (!mlxsw_sp_port) {
3454 dev_err(mlxsw_sp->bus_info->dev, "Port number \"%d\" does not exist\n",
3459 module = mlxsw_sp_port->mapping.module;
3460 cur_width = mlxsw_sp_port->mapping.width;
3462 if (count != 2 && count != 4) {
3463 netdev_err(mlxsw_sp_port->dev, "Port can only be split into 2 or 4 ports\n");
3467 if (cur_width != MLXSW_PORT_MODULE_MAX_WIDTH) {
3468 netdev_err(mlxsw_sp_port->dev, "Port cannot be split further\n");
3472 /* Make sure we have enough slave (even) ports for the split. */
3474 base_port = local_port;
3475 if (mlxsw_sp->ports[base_port + 1]) {
3476 netdev_err(mlxsw_sp_port->dev, "Invalid split configuration\n");
3480 base_port = mlxsw_sp_cluster_base_port_get(local_port);
3481 if (mlxsw_sp->ports[base_port + 1] ||
3482 mlxsw_sp->ports[base_port + 3]) {
3483 netdev_err(mlxsw_sp_port->dev, "Invalid split configuration\n");
3488 for (i = 0; i < count; i++)
3489 if (mlxsw_sp_port_created(mlxsw_sp, base_port + i))
3490 mlxsw_sp_port_remove(mlxsw_sp, base_port + i);
3492 err = mlxsw_sp_port_split_create(mlxsw_sp, base_port, module, count);
3494 dev_err(mlxsw_sp->bus_info->dev, "Failed to create split ports\n");
3495 goto err_port_split_create;
3500 err_port_split_create:
3501 mlxsw_sp_port_unsplit_create(mlxsw_sp, base_port, count);
3505 static int mlxsw_sp_port_unsplit(struct mlxsw_core *mlxsw_core, u8 local_port)
3507 struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
3508 struct mlxsw_sp_port *mlxsw_sp_port;
3509 u8 cur_width, base_port;
3513 mlxsw_sp_port = mlxsw_sp->ports[local_port];
3514 if (!mlxsw_sp_port) {
3515 dev_err(mlxsw_sp->bus_info->dev, "Port number \"%d\" does not exist\n",
3520 if (!mlxsw_sp_port->split) {
3521 netdev_err(mlxsw_sp_port->dev, "Port wasn't split\n");
3525 cur_width = mlxsw_sp_port->mapping.width;
3526 count = cur_width == 1 ? 4 : 2;
3528 base_port = mlxsw_sp_cluster_base_port_get(local_port);
3530 /* Determine which ports to remove. */
3531 if (count == 2 && local_port >= base_port + 2)
3532 base_port = base_port + 2;
3534 for (i = 0; i < count; i++)
3535 if (mlxsw_sp_port_created(mlxsw_sp, base_port + i))
3536 mlxsw_sp_port_remove(mlxsw_sp, base_port + i);
3538 mlxsw_sp_port_unsplit_create(mlxsw_sp, base_port, count);
3543 static void mlxsw_sp_pude_event_func(const struct mlxsw_reg_info *reg,
3544 char *pude_pl, void *priv)
3546 struct mlxsw_sp *mlxsw_sp = priv;
3547 struct mlxsw_sp_port *mlxsw_sp_port;
3548 enum mlxsw_reg_pude_oper_status status;
3551 local_port = mlxsw_reg_pude_local_port_get(pude_pl);
3552 mlxsw_sp_port = mlxsw_sp->ports[local_port];
3556 status = mlxsw_reg_pude_oper_status_get(pude_pl);
3557 if (status == MLXSW_PORT_OPER_STATUS_UP) {
3558 netdev_info(mlxsw_sp_port->dev, "link up\n");
3559 netif_carrier_on(mlxsw_sp_port->dev);
3561 netdev_info(mlxsw_sp_port->dev, "link down\n");
3562 netif_carrier_off(mlxsw_sp_port->dev);
3566 static void mlxsw_sp_rx_listener_no_mark_func(struct sk_buff *skb,
3567 u8 local_port, void *priv)
3569 struct mlxsw_sp *mlxsw_sp = priv;
3570 struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp->ports[local_port];
3571 struct mlxsw_sp_port_pcpu_stats *pcpu_stats;
3573 if (unlikely(!mlxsw_sp_port)) {
3574 dev_warn_ratelimited(mlxsw_sp->bus_info->dev, "Port %d: skb received for non-existent port\n",
3579 skb->dev = mlxsw_sp_port->dev;
3581 pcpu_stats = this_cpu_ptr(mlxsw_sp_port->pcpu_stats);
3582 u64_stats_update_begin(&pcpu_stats->syncp);
3583 pcpu_stats->rx_packets++;
3584 pcpu_stats->rx_bytes += skb->len;
3585 u64_stats_update_end(&pcpu_stats->syncp);
3587 skb->protocol = eth_type_trans(skb, skb->dev);
3588 netif_receive_skb(skb);
3591 static void mlxsw_sp_rx_listener_mark_func(struct sk_buff *skb, u8 local_port,
3594 skb->offload_fwd_mark = 1;
3595 return mlxsw_sp_rx_listener_no_mark_func(skb, local_port, priv);
3598 static void mlxsw_sp_rx_listener_mr_mark_func(struct sk_buff *skb,
3599 u8 local_port, void *priv)
3601 skb->offload_mr_fwd_mark = 1;
3602 skb->offload_fwd_mark = 1;
3603 return mlxsw_sp_rx_listener_no_mark_func(skb, local_port, priv);
3606 static void mlxsw_sp_rx_listener_sample_func(struct sk_buff *skb, u8 local_port,
3609 struct mlxsw_sp *mlxsw_sp = priv;
3610 struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp->ports[local_port];
3611 struct psample_group *psample_group;
3614 if (unlikely(!mlxsw_sp_port)) {
3615 dev_warn_ratelimited(mlxsw_sp->bus_info->dev, "Port %d: sample skb received for non-existent port\n",
3619 if (unlikely(!mlxsw_sp_port->sample)) {
3620 dev_warn_ratelimited(mlxsw_sp->bus_info->dev, "Port %d: sample skb received on unsupported port\n",
3625 size = mlxsw_sp_port->sample->truncate ?
3626 mlxsw_sp_port->sample->trunc_size : skb->len;
3629 psample_group = rcu_dereference(mlxsw_sp_port->sample->psample_group);
3632 psample_sample_packet(psample_group, skb, size,
3633 mlxsw_sp_port->dev->ifindex, 0,
3634 mlxsw_sp_port->sample->rate);
3641 #define MLXSW_SP_RXL_NO_MARK(_trap_id, _action, _trap_group, _is_ctrl) \
3642 MLXSW_RXL(mlxsw_sp_rx_listener_no_mark_func, _trap_id, _action, \
3643 _is_ctrl, SP_##_trap_group, DISCARD)
3645 #define MLXSW_SP_RXL_MARK(_trap_id, _action, _trap_group, _is_ctrl) \
3646 MLXSW_RXL(mlxsw_sp_rx_listener_mark_func, _trap_id, _action, \
3647 _is_ctrl, SP_##_trap_group, DISCARD)
3649 #define MLXSW_SP_RXL_MR_MARK(_trap_id, _action, _trap_group, _is_ctrl) \
3650 MLXSW_RXL(mlxsw_sp_rx_listener_mr_mark_func, _trap_id, _action, \
3651 _is_ctrl, SP_##_trap_group, DISCARD)
3653 #define MLXSW_SP_EVENTL(_func, _trap_id) \
3654 MLXSW_EVENTL(_func, _trap_id, SP_EVENT)
3656 static const struct mlxsw_listener mlxsw_sp_listener[] = {
3658 MLXSW_SP_EVENTL(mlxsw_sp_pude_event_func, PUDE),
3660 MLXSW_SP_RXL_NO_MARK(STP, TRAP_TO_CPU, STP, true),
3661 MLXSW_SP_RXL_NO_MARK(LACP, TRAP_TO_CPU, LACP, true),
3662 MLXSW_SP_RXL_NO_MARK(LLDP, TRAP_TO_CPU, LLDP, true),
3663 MLXSW_SP_RXL_MARK(DHCP, MIRROR_TO_CPU, DHCP, false),
3664 MLXSW_SP_RXL_MARK(IGMP_QUERY, MIRROR_TO_CPU, IGMP, false),
3665 MLXSW_SP_RXL_NO_MARK(IGMP_V1_REPORT, TRAP_TO_CPU, IGMP, false),
3666 MLXSW_SP_RXL_NO_MARK(IGMP_V2_REPORT, TRAP_TO_CPU, IGMP, false),
3667 MLXSW_SP_RXL_NO_MARK(IGMP_V2_LEAVE, TRAP_TO_CPU, IGMP, false),
3668 MLXSW_SP_RXL_NO_MARK(IGMP_V3_REPORT, TRAP_TO_CPU, IGMP, false),
3669 MLXSW_SP_RXL_MARK(ARPBC, MIRROR_TO_CPU, ARP, false),
3670 MLXSW_SP_RXL_MARK(ARPUC, MIRROR_TO_CPU, ARP, false),
3671 MLXSW_SP_RXL_NO_MARK(FID_MISS, TRAP_TO_CPU, IP2ME, false),
3672 MLXSW_SP_RXL_MARK(IPV6_MLDV12_LISTENER_QUERY, MIRROR_TO_CPU, IPV6_MLD,
3674 MLXSW_SP_RXL_NO_MARK(IPV6_MLDV1_LISTENER_REPORT, TRAP_TO_CPU, IPV6_MLD,
3676 MLXSW_SP_RXL_NO_MARK(IPV6_MLDV1_LISTENER_DONE, TRAP_TO_CPU, IPV6_MLD,
3678 MLXSW_SP_RXL_NO_MARK(IPV6_MLDV2_LISTENER_REPORT, TRAP_TO_CPU, IPV6_MLD,
3681 MLXSW_SP_RXL_MARK(MTUERROR, TRAP_TO_CPU, ROUTER_EXP, false),
3682 MLXSW_SP_RXL_MARK(TTLERROR, TRAP_TO_CPU, ROUTER_EXP, false),
3683 MLXSW_SP_RXL_MARK(LBERROR, TRAP_TO_CPU, ROUTER_EXP, false),
3684 MLXSW_SP_RXL_MARK(IP2ME, TRAP_TO_CPU, IP2ME, false),
3685 MLXSW_SP_RXL_MARK(IPV6_UNSPECIFIED_ADDRESS, TRAP_TO_CPU, ROUTER_EXP,
3687 MLXSW_SP_RXL_MARK(IPV6_LINK_LOCAL_DEST, TRAP_TO_CPU, ROUTER_EXP, false),
3688 MLXSW_SP_RXL_MARK(IPV6_LINK_LOCAL_SRC, TRAP_TO_CPU, ROUTER_EXP, false),
3689 MLXSW_SP_RXL_MARK(IPV6_ALL_NODES_LINK, TRAP_TO_CPU, ROUTER_EXP, false),
3690 MLXSW_SP_RXL_MARK(IPV6_ALL_ROUTERS_LINK, TRAP_TO_CPU, ROUTER_EXP,
3692 MLXSW_SP_RXL_MARK(IPV4_OSPF, TRAP_TO_CPU, OSPF, false),
3693 MLXSW_SP_RXL_MARK(IPV6_OSPF, TRAP_TO_CPU, OSPF, false),
3694 MLXSW_SP_RXL_MARK(IPV6_DHCP, TRAP_TO_CPU, DHCP, false),
3695 MLXSW_SP_RXL_MARK(RTR_INGRESS0, TRAP_TO_CPU, REMOTE_ROUTE, false),
3696 MLXSW_SP_RXL_MARK(IPV4_BGP, TRAP_TO_CPU, BGP, false),
3697 MLXSW_SP_RXL_MARK(IPV6_BGP, TRAP_TO_CPU, BGP, false),
3698 MLXSW_SP_RXL_MARK(L3_IPV6_ROUTER_SOLICITATION, TRAP_TO_CPU, IPV6_ND,
3700 MLXSW_SP_RXL_MARK(L3_IPV6_ROUTER_ADVERTISMENT, TRAP_TO_CPU, IPV6_ND,
3702 MLXSW_SP_RXL_MARK(L3_IPV6_NEIGHBOR_SOLICITATION, TRAP_TO_CPU, IPV6_ND,
3704 MLXSW_SP_RXL_MARK(L3_IPV6_NEIGHBOR_ADVERTISMENT, TRAP_TO_CPU, IPV6_ND,
3706 MLXSW_SP_RXL_MARK(L3_IPV6_REDIRECTION, TRAP_TO_CPU, IPV6_ND, false),
3707 MLXSW_SP_RXL_MARK(IPV6_MC_LINK_LOCAL_DEST, TRAP_TO_CPU, ROUTER_EXP,
3709 MLXSW_SP_RXL_MARK(HOST_MISS_IPV4, TRAP_TO_CPU, HOST_MISS, false),
3710 MLXSW_SP_RXL_MARK(HOST_MISS_IPV6, TRAP_TO_CPU, HOST_MISS, false),
3711 MLXSW_SP_RXL_MARK(ROUTER_ALERT_IPV4, TRAP_TO_CPU, ROUTER_EXP, false),
3712 MLXSW_SP_RXL_MARK(ROUTER_ALERT_IPV6, TRAP_TO_CPU, ROUTER_EXP, false),
3713 MLXSW_SP_RXL_MARK(IPIP_DECAP_ERROR, TRAP_TO_CPU, ROUTER_EXP, false),
3714 /* PKT Sample trap */
3715 MLXSW_RXL(mlxsw_sp_rx_listener_sample_func, PKT_SAMPLE, MIRROR_TO_CPU,
3716 false, SP_IP2ME, DISCARD),
3718 MLXSW_SP_RXL_NO_MARK(ACL0, TRAP_TO_CPU, IP2ME, false),
3719 /* Multicast Router Traps */
3720 MLXSW_SP_RXL_MARK(IPV4_PIM, TRAP_TO_CPU, PIM, false),
3721 MLXSW_SP_RXL_MARK(RPF, TRAP_TO_CPU, RPF, false),
3722 MLXSW_SP_RXL_MARK(ACL1, TRAP_TO_CPU, MULTICAST, false),
3723 MLXSW_SP_RXL_MR_MARK(ACL2, TRAP_TO_CPU, MULTICAST, false),
3726 static int mlxsw_sp_cpu_policers_set(struct mlxsw_core *mlxsw_core)
3728 char qpcr_pl[MLXSW_REG_QPCR_LEN];
3729 enum mlxsw_reg_qpcr_ir_units ir_units;
3730 int max_cpu_policers;
3736 if (!MLXSW_CORE_RES_VALID(mlxsw_core, MAX_CPU_POLICERS))
3739 max_cpu_policers = MLXSW_CORE_RES_GET(mlxsw_core, MAX_CPU_POLICERS);
3741 ir_units = MLXSW_REG_QPCR_IR_UNITS_M;
3742 for (i = 0; i < max_cpu_policers; i++) {
3745 case MLXSW_REG_HTGT_TRAP_GROUP_SP_STP:
3746 case MLXSW_REG_HTGT_TRAP_GROUP_SP_LACP:
3747 case MLXSW_REG_HTGT_TRAP_GROUP_SP_LLDP:
3748 case MLXSW_REG_HTGT_TRAP_GROUP_SP_OSPF:
3749 case MLXSW_REG_HTGT_TRAP_GROUP_SP_PIM:
3750 case MLXSW_REG_HTGT_TRAP_GROUP_SP_RPF:
3754 case MLXSW_REG_HTGT_TRAP_GROUP_SP_IGMP:
3755 case MLXSW_REG_HTGT_TRAP_GROUP_SP_IPV6_MLD:
3759 case MLXSW_REG_HTGT_TRAP_GROUP_SP_BGP:
3760 case MLXSW_REG_HTGT_TRAP_GROUP_SP_ARP:
3761 case MLXSW_REG_HTGT_TRAP_GROUP_SP_DHCP:
3762 case MLXSW_REG_HTGT_TRAP_GROUP_SP_HOST_MISS:
3763 case MLXSW_REG_HTGT_TRAP_GROUP_SP_ROUTER_EXP:
3764 case MLXSW_REG_HTGT_TRAP_GROUP_SP_REMOTE_ROUTE:
3765 case MLXSW_REG_HTGT_TRAP_GROUP_SP_IPV6_ND:
3766 case MLXSW_REG_HTGT_TRAP_GROUP_SP_MULTICAST:
3770 case MLXSW_REG_HTGT_TRAP_GROUP_SP_IP2ME:
3779 mlxsw_reg_qpcr_pack(qpcr_pl, i, ir_units, is_bytes, rate,
3781 err = mlxsw_reg_write(mlxsw_core, MLXSW_REG(qpcr), qpcr_pl);
3789 static int mlxsw_sp_trap_groups_set(struct mlxsw_core *mlxsw_core)
3791 char htgt_pl[MLXSW_REG_HTGT_LEN];
3792 enum mlxsw_reg_htgt_trap_group i;
3793 int max_cpu_policers;
3794 int max_trap_groups;
3799 if (!MLXSW_CORE_RES_VALID(mlxsw_core, MAX_TRAP_GROUPS))
3802 max_trap_groups = MLXSW_CORE_RES_GET(mlxsw_core, MAX_TRAP_GROUPS);
3803 max_cpu_policers = MLXSW_CORE_RES_GET(mlxsw_core, MAX_CPU_POLICERS);
3805 for (i = 0; i < max_trap_groups; i++) {
3808 case MLXSW_REG_HTGT_TRAP_GROUP_SP_STP:
3809 case MLXSW_REG_HTGT_TRAP_GROUP_SP_LACP:
3810 case MLXSW_REG_HTGT_TRAP_GROUP_SP_LLDP:
3811 case MLXSW_REG_HTGT_TRAP_GROUP_SP_OSPF:
3812 case MLXSW_REG_HTGT_TRAP_GROUP_SP_PIM:
3816 case MLXSW_REG_HTGT_TRAP_GROUP_SP_BGP:
3817 case MLXSW_REG_HTGT_TRAP_GROUP_SP_DHCP:
3821 case MLXSW_REG_HTGT_TRAP_GROUP_SP_IGMP:
3822 case MLXSW_REG_HTGT_TRAP_GROUP_SP_IP2ME:
3823 case MLXSW_REG_HTGT_TRAP_GROUP_SP_IPV6_MLD:
3827 case MLXSW_REG_HTGT_TRAP_GROUP_SP_ARP:
3828 case MLXSW_REG_HTGT_TRAP_GROUP_SP_IPV6_ND:
3829 case MLXSW_REG_HTGT_TRAP_GROUP_SP_RPF:
3833 case MLXSW_REG_HTGT_TRAP_GROUP_SP_HOST_MISS:
3834 case MLXSW_REG_HTGT_TRAP_GROUP_SP_ROUTER_EXP:
3835 case MLXSW_REG_HTGT_TRAP_GROUP_SP_REMOTE_ROUTE:
3836 case MLXSW_REG_HTGT_TRAP_GROUP_SP_MULTICAST:
3840 case MLXSW_REG_HTGT_TRAP_GROUP_SP_EVENT:
3841 priority = MLXSW_REG_HTGT_DEFAULT_PRIORITY;
3842 tc = MLXSW_REG_HTGT_DEFAULT_TC;
3843 policer_id = MLXSW_REG_HTGT_INVALID_POLICER;
3849 if (max_cpu_policers <= policer_id &&
3850 policer_id != MLXSW_REG_HTGT_INVALID_POLICER)
3853 mlxsw_reg_htgt_pack(htgt_pl, i, policer_id, priority, tc);
3854 err = mlxsw_reg_write(mlxsw_core, MLXSW_REG(htgt), htgt_pl);
3862 static int mlxsw_sp_traps_init(struct mlxsw_sp *mlxsw_sp)
3867 err = mlxsw_sp_cpu_policers_set(mlxsw_sp->core);
3871 err = mlxsw_sp_trap_groups_set(mlxsw_sp->core);
3875 for (i = 0; i < ARRAY_SIZE(mlxsw_sp_listener); i++) {
3876 err = mlxsw_core_trap_register(mlxsw_sp->core,
3877 &mlxsw_sp_listener[i],
3880 goto err_listener_register;
3885 err_listener_register:
3886 for (i--; i >= 0; i--) {
3887 mlxsw_core_trap_unregister(mlxsw_sp->core,
3888 &mlxsw_sp_listener[i],
3894 static void mlxsw_sp_traps_fini(struct mlxsw_sp *mlxsw_sp)
3898 for (i = 0; i < ARRAY_SIZE(mlxsw_sp_listener); i++) {
3899 mlxsw_core_trap_unregister(mlxsw_sp->core,
3900 &mlxsw_sp_listener[i],
3905 static int mlxsw_sp_lag_init(struct mlxsw_sp *mlxsw_sp)
3907 char slcr_pl[MLXSW_REG_SLCR_LEN];
3910 mlxsw_reg_slcr_pack(slcr_pl, MLXSW_REG_SLCR_LAG_HASH_SMAC |
3911 MLXSW_REG_SLCR_LAG_HASH_DMAC |
3912 MLXSW_REG_SLCR_LAG_HASH_ETHERTYPE |
3913 MLXSW_REG_SLCR_LAG_HASH_VLANID |
3914 MLXSW_REG_SLCR_LAG_HASH_SIP |
3915 MLXSW_REG_SLCR_LAG_HASH_DIP |
3916 MLXSW_REG_SLCR_LAG_HASH_SPORT |
3917 MLXSW_REG_SLCR_LAG_HASH_DPORT |
3918 MLXSW_REG_SLCR_LAG_HASH_IPPROTO);
3919 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(slcr), slcr_pl);
3923 if (!MLXSW_CORE_RES_VALID(mlxsw_sp->core, MAX_LAG) ||
3924 !MLXSW_CORE_RES_VALID(mlxsw_sp->core, MAX_LAG_MEMBERS))
3927 mlxsw_sp->lags = kcalloc(MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_LAG),
3928 sizeof(struct mlxsw_sp_upper),
3930 if (!mlxsw_sp->lags)
3936 static void mlxsw_sp_lag_fini(struct mlxsw_sp *mlxsw_sp)
3938 kfree(mlxsw_sp->lags);
3941 static int mlxsw_sp_basic_trap_groups_set(struct mlxsw_core *mlxsw_core)
3943 char htgt_pl[MLXSW_REG_HTGT_LEN];
3945 mlxsw_reg_htgt_pack(htgt_pl, MLXSW_REG_HTGT_TRAP_GROUP_EMAD,
3946 MLXSW_REG_HTGT_INVALID_POLICER,
3947 MLXSW_REG_HTGT_DEFAULT_PRIORITY,
3948 MLXSW_REG_HTGT_DEFAULT_TC);
3949 return mlxsw_reg_write(mlxsw_core, MLXSW_REG(htgt), htgt_pl);
3952 static int mlxsw_sp_netdevice_event(struct notifier_block *unused,
3953 unsigned long event, void *ptr);
3955 static int mlxsw_sp_init(struct mlxsw_core *mlxsw_core,
3956 const struct mlxsw_bus_info *mlxsw_bus_info)
3958 struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
3961 mlxsw_sp->core = mlxsw_core;
3962 mlxsw_sp->bus_info = mlxsw_bus_info;
3964 err = mlxsw_sp_fw_rev_validate(mlxsw_sp);
3966 dev_err(mlxsw_sp->bus_info->dev, "Could not upgrade firmware\n");
3970 err = mlxsw_sp_base_mac_get(mlxsw_sp);
3972 dev_err(mlxsw_sp->bus_info->dev, "Failed to get base mac\n");
3976 err = mlxsw_sp_kvdl_init(mlxsw_sp);
3978 dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize KVDL\n");
3982 err = mlxsw_sp_fids_init(mlxsw_sp);
3984 dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize FIDs\n");
3988 err = mlxsw_sp_traps_init(mlxsw_sp);
3990 dev_err(mlxsw_sp->bus_info->dev, "Failed to set traps\n");
3991 goto err_traps_init;
3994 err = mlxsw_sp_buffers_init(mlxsw_sp);
3996 dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize buffers\n");
3997 goto err_buffers_init;
4000 err = mlxsw_sp_lag_init(mlxsw_sp);
4002 dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize LAG\n");
4006 err = mlxsw_sp_switchdev_init(mlxsw_sp);
4008 dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize switchdev\n");
4009 goto err_switchdev_init;
4012 err = mlxsw_sp_counter_pool_init(mlxsw_sp);
4014 dev_err(mlxsw_sp->bus_info->dev, "Failed to init counter pool\n");
4015 goto err_counter_pool_init;
4018 err = mlxsw_sp_afa_init(mlxsw_sp);
4020 dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize ACL actions\n");
4024 err = mlxsw_sp_router_init(mlxsw_sp);
4026 dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize router\n");
4027 goto err_router_init;
4030 /* Initialize netdevice notifier after router is initialized, so that
4031 * the event handler can use router structures.
4033 mlxsw_sp->netdevice_nb.notifier_call = mlxsw_sp_netdevice_event;
4034 err = register_netdevice_notifier(&mlxsw_sp->netdevice_nb);
4036 dev_err(mlxsw_sp->bus_info->dev, "Failed to register netdev notifier\n");
4037 goto err_netdev_notifier;
4040 err = mlxsw_sp_span_init(mlxsw_sp);
4042 dev_err(mlxsw_sp->bus_info->dev, "Failed to init span system\n");
4046 err = mlxsw_sp_acl_init(mlxsw_sp);
4048 dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize ACL\n");
4052 err = mlxsw_sp_dpipe_init(mlxsw_sp);
4054 dev_err(mlxsw_sp->bus_info->dev, "Failed to init pipeline debug\n");
4055 goto err_dpipe_init;
4058 err = mlxsw_sp_ports_create(mlxsw_sp);
4060 dev_err(mlxsw_sp->bus_info->dev, "Failed to create ports\n");
4061 goto err_ports_create;
4067 mlxsw_sp_dpipe_fini(mlxsw_sp);
4069 mlxsw_sp_acl_fini(mlxsw_sp);
4071 mlxsw_sp_span_fini(mlxsw_sp);
4073 unregister_netdevice_notifier(&mlxsw_sp->netdevice_nb);
4074 err_netdev_notifier:
4075 mlxsw_sp_router_fini(mlxsw_sp);
4077 mlxsw_sp_afa_fini(mlxsw_sp);
4079 mlxsw_sp_counter_pool_fini(mlxsw_sp);
4080 err_counter_pool_init:
4081 mlxsw_sp_switchdev_fini(mlxsw_sp);
4083 mlxsw_sp_lag_fini(mlxsw_sp);
4085 mlxsw_sp_buffers_fini(mlxsw_sp);
4087 mlxsw_sp_traps_fini(mlxsw_sp);
4089 mlxsw_sp_fids_fini(mlxsw_sp);
4091 mlxsw_sp_kvdl_fini(mlxsw_sp);
4095 static void mlxsw_sp_fini(struct mlxsw_core *mlxsw_core)
4097 struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
4099 mlxsw_sp_ports_remove(mlxsw_sp);
4100 mlxsw_sp_dpipe_fini(mlxsw_sp);
4101 mlxsw_sp_acl_fini(mlxsw_sp);
4102 mlxsw_sp_span_fini(mlxsw_sp);
4103 unregister_netdevice_notifier(&mlxsw_sp->netdevice_nb);
4104 mlxsw_sp_router_fini(mlxsw_sp);
4105 mlxsw_sp_afa_fini(mlxsw_sp);
4106 mlxsw_sp_counter_pool_fini(mlxsw_sp);
4107 mlxsw_sp_switchdev_fini(mlxsw_sp);
4108 mlxsw_sp_lag_fini(mlxsw_sp);
4109 mlxsw_sp_buffers_fini(mlxsw_sp);
4110 mlxsw_sp_traps_fini(mlxsw_sp);
4111 mlxsw_sp_fids_fini(mlxsw_sp);
4112 mlxsw_sp_kvdl_fini(mlxsw_sp);
4115 static const struct mlxsw_config_profile mlxsw_sp_config_profile = {
4116 .used_max_vepa_channels = 1,
4117 .max_vepa_channels = 0,
4119 .max_mid = MLXSW_SP_MID_MAX,
4122 .used_flood_tables = 1,
4123 .used_flood_mode = 1,
4125 .max_fid_offset_flood_tables = 3,
4126 .fid_offset_flood_table_size = VLAN_N_VID - 1,
4127 .max_fid_flood_tables = 3,
4128 .fid_flood_table_size = MLXSW_SP_FID_8021D_MAX,
4129 .used_max_ib_mc = 1,
4133 .used_kvd_split_data = 1,
4134 .kvd_hash_granularity = MLXSW_SP_KVD_GRANULARITY,
4135 .kvd_hash_single_parts = 59,
4136 .kvd_hash_double_parts = 41,
4137 .kvd_linear_size = MLXSW_SP_KVD_LINEAR_SIZE,
4141 .type = MLXSW_PORT_SWID_TYPE_ETH,
4144 .resource_query_enable = 1,
4148 mlxsw_sp_resource_kvd_granularity_validate(struct netlink_ext_ack *extack,
4151 const struct mlxsw_config_profile *profile;
4153 profile = &mlxsw_sp_config_profile;
4154 if (size % profile->kvd_hash_granularity) {
4155 NL_SET_ERR_MSG_MOD(extack, "resource set with wrong granularity");
4162 mlxsw_sp_resource_kvd_size_validate(struct devlink *devlink, u64 size,
4163 struct netlink_ext_ack *extack)
4165 NL_SET_ERR_MSG_MOD(extack, "kvd size cannot be changed");
4170 mlxsw_sp_resource_kvd_linear_size_validate(struct devlink *devlink, u64 size,
4171 struct netlink_ext_ack *extack)
4173 if (!mlxsw_sp_resource_kvd_granularity_validate(extack, size))
4180 mlxsw_sp_resource_kvd_hash_single_size_validate(struct devlink *devlink, u64 size,
4181 struct netlink_ext_ack *extack)
4183 struct mlxsw_core *mlxsw_core = devlink_priv(devlink);
4185 if (!mlxsw_sp_resource_kvd_granularity_validate(extack, size))
4188 if (size < MLXSW_CORE_RES_GET(mlxsw_core, KVD_SINGLE_MIN_SIZE)) {
4189 NL_SET_ERR_MSG_MOD(extack, "hash single size is smaller than minimum");
4196 mlxsw_sp_resource_kvd_hash_double_size_validate(struct devlink *devlink, u64 size,
4197 struct netlink_ext_ack *extack)
4199 struct mlxsw_core *mlxsw_core = devlink_priv(devlink);
4201 if (!mlxsw_sp_resource_kvd_granularity_validate(extack, size))
4204 if (size < MLXSW_CORE_RES_GET(mlxsw_core, KVD_DOUBLE_MIN_SIZE)) {
4205 NL_SET_ERR_MSG_MOD(extack, "hash double size is smaller than minimum");
4211 static u64 mlxsw_sp_resource_kvd_linear_occ_get(struct devlink *devlink)
4213 struct mlxsw_core *mlxsw_core = devlink_priv(devlink);
4214 struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
4216 return mlxsw_sp_kvdl_occ_get(mlxsw_sp);
4219 static struct devlink_resource_ops mlxsw_sp_resource_kvd_ops = {
4220 .size_validate = mlxsw_sp_resource_kvd_size_validate,
4223 static struct devlink_resource_ops mlxsw_sp_resource_kvd_linear_ops = {
4224 .size_validate = mlxsw_sp_resource_kvd_linear_size_validate,
4225 .occ_get = mlxsw_sp_resource_kvd_linear_occ_get,
4228 static struct devlink_resource_ops mlxsw_sp_resource_kvd_hash_single_ops = {
4229 .size_validate = mlxsw_sp_resource_kvd_hash_single_size_validate,
4232 static struct devlink_resource_ops mlxsw_sp_resource_kvd_hash_double_ops = {
4233 .size_validate = mlxsw_sp_resource_kvd_hash_double_size_validate,
4237 mlxsw_sp_resource_size_params_prepare(struct mlxsw_core *mlxsw_core,
4238 struct devlink_resource_size_params *kvd_size_params,
4239 struct devlink_resource_size_params *linear_size_params,
4240 struct devlink_resource_size_params *hash_double_size_params,
4241 struct devlink_resource_size_params *hash_single_size_params)
4243 u32 single_size_min = MLXSW_CORE_RES_GET(mlxsw_core,
4244 KVD_SINGLE_MIN_SIZE);
4245 u32 double_size_min = MLXSW_CORE_RES_GET(mlxsw_core,
4246 KVD_DOUBLE_MIN_SIZE);
4247 u32 kvd_size = MLXSW_CORE_RES_GET(mlxsw_core, KVD_SIZE);
4248 u32 linear_size_min = 0;
4250 devlink_resource_size_params_init(kvd_size_params, kvd_size, kvd_size,
4251 MLXSW_SP_KVD_GRANULARITY,
4252 DEVLINK_RESOURCE_UNIT_ENTRY);
4253 devlink_resource_size_params_init(linear_size_params, linear_size_min,
4254 kvd_size - single_size_min -
4256 MLXSW_SP_KVD_GRANULARITY,
4257 DEVLINK_RESOURCE_UNIT_ENTRY);
4258 devlink_resource_size_params_init(hash_double_size_params,
4260 kvd_size - single_size_min -
4262 MLXSW_SP_KVD_GRANULARITY,
4263 DEVLINK_RESOURCE_UNIT_ENTRY);
4264 devlink_resource_size_params_init(hash_single_size_params,
4266 kvd_size - double_size_min -
4268 MLXSW_SP_KVD_GRANULARITY,
4269 DEVLINK_RESOURCE_UNIT_ENTRY);
4272 static int mlxsw_sp_resources_register(struct mlxsw_core *mlxsw_core)
4274 struct devlink *devlink = priv_to_devlink(mlxsw_core);
4275 struct devlink_resource_size_params hash_single_size_params;
4276 struct devlink_resource_size_params hash_double_size_params;
4277 struct devlink_resource_size_params linear_size_params;
4278 struct devlink_resource_size_params kvd_size_params;
4279 u32 kvd_size, single_size, double_size, linear_size;
4280 const struct mlxsw_config_profile *profile;
4283 profile = &mlxsw_sp_config_profile;
4284 if (!MLXSW_CORE_RES_VALID(mlxsw_core, KVD_SIZE))
4287 mlxsw_sp_resource_size_params_prepare(mlxsw_core, &kvd_size_params,
4288 &linear_size_params,
4289 &hash_double_size_params,
4290 &hash_single_size_params);
4292 kvd_size = MLXSW_CORE_RES_GET(mlxsw_core, KVD_SIZE);
4293 err = devlink_resource_register(devlink, MLXSW_SP_RESOURCE_NAME_KVD,
4295 MLXSW_SP_RESOURCE_KVD,
4296 DEVLINK_RESOURCE_ID_PARENT_TOP,
4298 &mlxsw_sp_resource_kvd_ops);
4302 linear_size = profile->kvd_linear_size;
4303 err = devlink_resource_register(devlink, MLXSW_SP_RESOURCE_NAME_KVD_LINEAR,
4305 MLXSW_SP_RESOURCE_KVD_LINEAR,
4306 MLXSW_SP_RESOURCE_KVD,
4307 &linear_size_params,
4308 &mlxsw_sp_resource_kvd_linear_ops);
4312 double_size = kvd_size - linear_size;
4313 double_size *= profile->kvd_hash_double_parts;
4314 double_size /= profile->kvd_hash_double_parts +
4315 profile->kvd_hash_single_parts;
4316 double_size = rounddown(double_size, profile->kvd_hash_granularity);
4317 err = devlink_resource_register(devlink, MLXSW_SP_RESOURCE_NAME_KVD_HASH_DOUBLE,
4319 MLXSW_SP_RESOURCE_KVD_HASH_DOUBLE,
4320 MLXSW_SP_RESOURCE_KVD,
4321 &hash_double_size_params,
4322 &mlxsw_sp_resource_kvd_hash_double_ops);
4326 single_size = kvd_size - double_size - linear_size;
4327 err = devlink_resource_register(devlink, MLXSW_SP_RESOURCE_NAME_KVD_HASH_SINGLE,
4329 MLXSW_SP_RESOURCE_KVD_HASH_SINGLE,
4330 MLXSW_SP_RESOURCE_KVD,
4331 &hash_single_size_params,
4332 &mlxsw_sp_resource_kvd_hash_single_ops);
4339 static int mlxsw_sp_kvd_sizes_get(struct mlxsw_core *mlxsw_core,
4340 const struct mlxsw_config_profile *profile,
4341 u64 *p_single_size, u64 *p_double_size,
4344 struct devlink *devlink = priv_to_devlink(mlxsw_core);
4348 if (!MLXSW_CORE_RES_VALID(mlxsw_core, KVD_SINGLE_MIN_SIZE) ||
4349 !MLXSW_CORE_RES_VALID(mlxsw_core, KVD_DOUBLE_MIN_SIZE) ||
4350 !profile->used_kvd_split_data)
4353 /* The hash part is what left of the kvd without the
4354 * linear part. It is split to the single size and
4355 * double size by the parts ratio from the profile.
4356 * Both sizes must be a multiplications of the
4357 * granularity from the profile. In case the user
4358 * provided the sizes they are obtained via devlink.
4360 err = devlink_resource_size_get(devlink,
4361 MLXSW_SP_RESOURCE_KVD_LINEAR,
4364 *p_linear_size = profile->kvd_linear_size;
4366 err = devlink_resource_size_get(devlink,
4367 MLXSW_SP_RESOURCE_KVD_HASH_DOUBLE,
4370 double_size = MLXSW_CORE_RES_GET(mlxsw_core, KVD_SIZE) -
4372 double_size *= profile->kvd_hash_double_parts;
4373 double_size /= profile->kvd_hash_double_parts +
4374 profile->kvd_hash_single_parts;
4375 *p_double_size = rounddown(double_size,
4376 profile->kvd_hash_granularity);
4379 err = devlink_resource_size_get(devlink,
4380 MLXSW_SP_RESOURCE_KVD_HASH_SINGLE,
4383 *p_single_size = MLXSW_CORE_RES_GET(mlxsw_core, KVD_SIZE) -
4384 *p_double_size - *p_linear_size;
4386 /* Check results are legal. */
4387 if (*p_single_size < MLXSW_CORE_RES_GET(mlxsw_core, KVD_SINGLE_MIN_SIZE) ||
4388 *p_double_size < MLXSW_CORE_RES_GET(mlxsw_core, KVD_DOUBLE_MIN_SIZE) ||
4389 MLXSW_CORE_RES_GET(mlxsw_core, KVD_SIZE) < *p_linear_size)
4395 static struct mlxsw_driver mlxsw_sp_driver = {
4396 .kind = mlxsw_sp_driver_name,
4397 .priv_size = sizeof(struct mlxsw_sp),
4398 .init = mlxsw_sp_init,
4399 .fini = mlxsw_sp_fini,
4400 .basic_trap_groups_set = mlxsw_sp_basic_trap_groups_set,
4401 .port_split = mlxsw_sp_port_split,
4402 .port_unsplit = mlxsw_sp_port_unsplit,
4403 .sb_pool_get = mlxsw_sp_sb_pool_get,
4404 .sb_pool_set = mlxsw_sp_sb_pool_set,
4405 .sb_port_pool_get = mlxsw_sp_sb_port_pool_get,
4406 .sb_port_pool_set = mlxsw_sp_sb_port_pool_set,
4407 .sb_tc_pool_bind_get = mlxsw_sp_sb_tc_pool_bind_get,
4408 .sb_tc_pool_bind_set = mlxsw_sp_sb_tc_pool_bind_set,
4409 .sb_occ_snapshot = mlxsw_sp_sb_occ_snapshot,
4410 .sb_occ_max_clear = mlxsw_sp_sb_occ_max_clear,
4411 .sb_occ_port_pool_get = mlxsw_sp_sb_occ_port_pool_get,
4412 .sb_occ_tc_port_bind_get = mlxsw_sp_sb_occ_tc_port_bind_get,
4413 .txhdr_construct = mlxsw_sp_txhdr_construct,
4414 .resources_register = mlxsw_sp_resources_register,
4415 .kvd_sizes_get = mlxsw_sp_kvd_sizes_get,
4416 .txhdr_len = MLXSW_TXHDR_LEN,
4417 .profile = &mlxsw_sp_config_profile,
4420 bool mlxsw_sp_port_dev_check(const struct net_device *dev)
4422 return dev->netdev_ops == &mlxsw_sp_port_netdev_ops;
4425 static int mlxsw_sp_lower_dev_walk(struct net_device *lower_dev, void *data)
4427 struct mlxsw_sp_port **p_mlxsw_sp_port = data;
4430 if (mlxsw_sp_port_dev_check(lower_dev)) {
4431 *p_mlxsw_sp_port = netdev_priv(lower_dev);
4438 struct mlxsw_sp_port *mlxsw_sp_port_dev_lower_find(struct net_device *dev)
4440 struct mlxsw_sp_port *mlxsw_sp_port;
4442 if (mlxsw_sp_port_dev_check(dev))
4443 return netdev_priv(dev);
4445 mlxsw_sp_port = NULL;
4446 netdev_walk_all_lower_dev(dev, mlxsw_sp_lower_dev_walk, &mlxsw_sp_port);
4448 return mlxsw_sp_port;
4451 struct mlxsw_sp *mlxsw_sp_lower_get(struct net_device *dev)
4453 struct mlxsw_sp_port *mlxsw_sp_port;
4455 mlxsw_sp_port = mlxsw_sp_port_dev_lower_find(dev);
4456 return mlxsw_sp_port ? mlxsw_sp_port->mlxsw_sp : NULL;
4459 struct mlxsw_sp_port *mlxsw_sp_port_dev_lower_find_rcu(struct net_device *dev)
4461 struct mlxsw_sp_port *mlxsw_sp_port;
4463 if (mlxsw_sp_port_dev_check(dev))
4464 return netdev_priv(dev);
4466 mlxsw_sp_port = NULL;
4467 netdev_walk_all_lower_dev_rcu(dev, mlxsw_sp_lower_dev_walk,
4470 return mlxsw_sp_port;
4473 struct mlxsw_sp_port *mlxsw_sp_port_lower_dev_hold(struct net_device *dev)
4475 struct mlxsw_sp_port *mlxsw_sp_port;
4478 mlxsw_sp_port = mlxsw_sp_port_dev_lower_find_rcu(dev);
4480 dev_hold(mlxsw_sp_port->dev);
4482 return mlxsw_sp_port;
4485 void mlxsw_sp_port_dev_put(struct mlxsw_sp_port *mlxsw_sp_port)
4487 dev_put(mlxsw_sp_port->dev);
4490 static int mlxsw_sp_lag_create(struct mlxsw_sp *mlxsw_sp, u16 lag_id)
4492 char sldr_pl[MLXSW_REG_SLDR_LEN];
4494 mlxsw_reg_sldr_lag_create_pack(sldr_pl, lag_id);
4495 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sldr), sldr_pl);
4498 static int mlxsw_sp_lag_destroy(struct mlxsw_sp *mlxsw_sp, u16 lag_id)
4500 char sldr_pl[MLXSW_REG_SLDR_LEN];
4502 mlxsw_reg_sldr_lag_destroy_pack(sldr_pl, lag_id);
4503 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sldr), sldr_pl);
4506 static int mlxsw_sp_lag_col_port_add(struct mlxsw_sp_port *mlxsw_sp_port,
4507 u16 lag_id, u8 port_index)
4509 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
4510 char slcor_pl[MLXSW_REG_SLCOR_LEN];
4512 mlxsw_reg_slcor_port_add_pack(slcor_pl, mlxsw_sp_port->local_port,
4513 lag_id, port_index);
4514 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(slcor), slcor_pl);
4517 static int mlxsw_sp_lag_col_port_remove(struct mlxsw_sp_port *mlxsw_sp_port,
4520 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
4521 char slcor_pl[MLXSW_REG_SLCOR_LEN];
4523 mlxsw_reg_slcor_port_remove_pack(slcor_pl, mlxsw_sp_port->local_port,
4525 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(slcor), slcor_pl);
4528 static int mlxsw_sp_lag_col_port_enable(struct mlxsw_sp_port *mlxsw_sp_port,
4531 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
4532 char slcor_pl[MLXSW_REG_SLCOR_LEN];
4534 mlxsw_reg_slcor_col_enable_pack(slcor_pl, mlxsw_sp_port->local_port,
4536 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(slcor), slcor_pl);
4539 static int mlxsw_sp_lag_col_port_disable(struct mlxsw_sp_port *mlxsw_sp_port,
4542 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
4543 char slcor_pl[MLXSW_REG_SLCOR_LEN];
4545 mlxsw_reg_slcor_col_disable_pack(slcor_pl, mlxsw_sp_port->local_port,
4547 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(slcor), slcor_pl);
4550 static int mlxsw_sp_lag_index_get(struct mlxsw_sp *mlxsw_sp,
4551 struct net_device *lag_dev,
4554 struct mlxsw_sp_upper *lag;
4555 int free_lag_id = -1;
4559 max_lag = MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_LAG);
4560 for (i = 0; i < max_lag; i++) {
4561 lag = mlxsw_sp_lag_get(mlxsw_sp, i);
4562 if (lag->ref_count) {
4563 if (lag->dev == lag_dev) {
4567 } else if (free_lag_id < 0) {
4571 if (free_lag_id < 0)
4573 *p_lag_id = free_lag_id;
4578 mlxsw_sp_master_lag_check(struct mlxsw_sp *mlxsw_sp,
4579 struct net_device *lag_dev,
4580 struct netdev_lag_upper_info *lag_upper_info,
4581 struct netlink_ext_ack *extack)
4585 if (mlxsw_sp_lag_index_get(mlxsw_sp, lag_dev, &lag_id) != 0) {
4586 NL_SET_ERR_MSG(extack,
4587 "spectrum: Exceeded number of supported LAG devices");
4590 if (lag_upper_info->tx_type != NETDEV_LAG_TX_TYPE_HASH) {
4591 NL_SET_ERR_MSG(extack,
4592 "spectrum: LAG device using unsupported Tx type");
4598 static int mlxsw_sp_port_lag_index_get(struct mlxsw_sp *mlxsw_sp,
4599 u16 lag_id, u8 *p_port_index)
4601 u64 max_lag_members;
4604 max_lag_members = MLXSW_CORE_RES_GET(mlxsw_sp->core,
4606 for (i = 0; i < max_lag_members; i++) {
4607 if (!mlxsw_sp_port_lagged_get(mlxsw_sp, lag_id, i)) {
4615 static int mlxsw_sp_port_lag_join(struct mlxsw_sp_port *mlxsw_sp_port,
4616 struct net_device *lag_dev)
4618 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
4619 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
4620 struct mlxsw_sp_upper *lag;
4625 err = mlxsw_sp_lag_index_get(mlxsw_sp, lag_dev, &lag_id);
4628 lag = mlxsw_sp_lag_get(mlxsw_sp, lag_id);
4629 if (!lag->ref_count) {
4630 err = mlxsw_sp_lag_create(mlxsw_sp, lag_id);
4636 err = mlxsw_sp_port_lag_index_get(mlxsw_sp, lag_id, &port_index);
4639 err = mlxsw_sp_lag_col_port_add(mlxsw_sp_port, lag_id, port_index);
4641 goto err_col_port_add;
4642 err = mlxsw_sp_lag_col_port_enable(mlxsw_sp_port, lag_id);
4644 goto err_col_port_enable;
4646 mlxsw_core_lag_mapping_set(mlxsw_sp->core, lag_id, port_index,
4647 mlxsw_sp_port->local_port);
4648 mlxsw_sp_port->lag_id = lag_id;
4649 mlxsw_sp_port->lagged = 1;
4652 /* Port is no longer usable as a router interface */
4653 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port, 1);
4654 if (mlxsw_sp_port_vlan->fid)
4655 mlxsw_sp_port_vlan_router_leave(mlxsw_sp_port_vlan);
4659 err_col_port_enable:
4660 mlxsw_sp_lag_col_port_remove(mlxsw_sp_port, lag_id);
4662 if (!lag->ref_count)
4663 mlxsw_sp_lag_destroy(mlxsw_sp, lag_id);
4667 static void mlxsw_sp_port_lag_leave(struct mlxsw_sp_port *mlxsw_sp_port,
4668 struct net_device *lag_dev)
4670 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
4671 u16 lag_id = mlxsw_sp_port->lag_id;
4672 struct mlxsw_sp_upper *lag;
4674 if (!mlxsw_sp_port->lagged)
4676 lag = mlxsw_sp_lag_get(mlxsw_sp, lag_id);
4677 WARN_ON(lag->ref_count == 0);
4679 mlxsw_sp_lag_col_port_disable(mlxsw_sp_port, lag_id);
4680 mlxsw_sp_lag_col_port_remove(mlxsw_sp_port, lag_id);
4682 /* Any VLANs configured on the port are no longer valid */
4683 mlxsw_sp_port_vlan_flush(mlxsw_sp_port);
4685 if (lag->ref_count == 1)
4686 mlxsw_sp_lag_destroy(mlxsw_sp, lag_id);
4688 mlxsw_core_lag_mapping_clear(mlxsw_sp->core, lag_id,
4689 mlxsw_sp_port->local_port);
4690 mlxsw_sp_port->lagged = 0;
4693 mlxsw_sp_port_vlan_get(mlxsw_sp_port, 1);
4694 /* Make sure untagged frames are allowed to ingress */
4695 mlxsw_sp_port_pvid_set(mlxsw_sp_port, 1);
4698 static int mlxsw_sp_lag_dist_port_add(struct mlxsw_sp_port *mlxsw_sp_port,
4701 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
4702 char sldr_pl[MLXSW_REG_SLDR_LEN];
4704 mlxsw_reg_sldr_lag_add_port_pack(sldr_pl, lag_id,
4705 mlxsw_sp_port->local_port);
4706 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sldr), sldr_pl);
4709 static int mlxsw_sp_lag_dist_port_remove(struct mlxsw_sp_port *mlxsw_sp_port,
4712 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
4713 char sldr_pl[MLXSW_REG_SLDR_LEN];
4715 mlxsw_reg_sldr_lag_remove_port_pack(sldr_pl, lag_id,
4716 mlxsw_sp_port->local_port);
4717 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sldr), sldr_pl);
4720 static int mlxsw_sp_port_lag_tx_en_set(struct mlxsw_sp_port *mlxsw_sp_port,
4721 bool lag_tx_enabled)
4724 return mlxsw_sp_lag_dist_port_add(mlxsw_sp_port,
4725 mlxsw_sp_port->lag_id);
4727 return mlxsw_sp_lag_dist_port_remove(mlxsw_sp_port,
4728 mlxsw_sp_port->lag_id);
4731 static int mlxsw_sp_port_lag_changed(struct mlxsw_sp_port *mlxsw_sp_port,
4732 struct netdev_lag_lower_state_info *info)
4734 return mlxsw_sp_port_lag_tx_en_set(mlxsw_sp_port, info->tx_enabled);
4737 static int mlxsw_sp_port_stp_set(struct mlxsw_sp_port *mlxsw_sp_port,
4740 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
4741 enum mlxsw_reg_spms_state spms_state;
4746 spms_state = enable ? MLXSW_REG_SPMS_STATE_FORWARDING :
4747 MLXSW_REG_SPMS_STATE_DISCARDING;
4749 spms_pl = kmalloc(MLXSW_REG_SPMS_LEN, GFP_KERNEL);
4752 mlxsw_reg_spms_pack(spms_pl, mlxsw_sp_port->local_port);
4754 for (vid = 0; vid < VLAN_N_VID; vid++)
4755 mlxsw_reg_spms_vid_pack(spms_pl, vid, spms_state);
4757 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(spms), spms_pl);
4762 static int mlxsw_sp_port_ovs_join(struct mlxsw_sp_port *mlxsw_sp_port)
4767 err = mlxsw_sp_port_vp_mode_set(mlxsw_sp_port, true);
4770 err = mlxsw_sp_port_stp_set(mlxsw_sp_port, true);
4772 goto err_port_stp_set;
4773 err = mlxsw_sp_port_vlan_set(mlxsw_sp_port, 2, VLAN_N_VID - 1,
4776 goto err_port_vlan_set;
4778 for (; vid <= VLAN_N_VID - 1; vid++) {
4779 err = mlxsw_sp_port_vid_learning_set(mlxsw_sp_port,
4782 goto err_vid_learning_set;
4787 err_vid_learning_set:
4788 for (vid--; vid >= 1; vid--)
4789 mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid, true);
4791 mlxsw_sp_port_stp_set(mlxsw_sp_port, false);
4793 mlxsw_sp_port_vp_mode_set(mlxsw_sp_port, false);
4797 static void mlxsw_sp_port_ovs_leave(struct mlxsw_sp_port *mlxsw_sp_port)
4801 for (vid = VLAN_N_VID - 1; vid >= 1; vid--)
4802 mlxsw_sp_port_vid_learning_set(mlxsw_sp_port,
4805 mlxsw_sp_port_vlan_set(mlxsw_sp_port, 2, VLAN_N_VID - 1,
4807 mlxsw_sp_port_stp_set(mlxsw_sp_port, false);
4808 mlxsw_sp_port_vp_mode_set(mlxsw_sp_port, false);
4811 static int mlxsw_sp_netdevice_port_upper_event(struct net_device *lower_dev,
4812 struct net_device *dev,
4813 unsigned long event, void *ptr)
4815 struct netdev_notifier_changeupper_info *info;
4816 struct mlxsw_sp_port *mlxsw_sp_port;
4817 struct netlink_ext_ack *extack;
4818 struct net_device *upper_dev;
4819 struct mlxsw_sp *mlxsw_sp;
4822 mlxsw_sp_port = netdev_priv(dev);
4823 mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
4825 extack = netdev_notifier_info_to_extack(&info->info);
4828 case NETDEV_PRECHANGEUPPER:
4829 upper_dev = info->upper_dev;
4830 if (!is_vlan_dev(upper_dev) &&
4831 !netif_is_lag_master(upper_dev) &&
4832 !netif_is_bridge_master(upper_dev) &&
4833 !netif_is_ovs_master(upper_dev)) {
4834 NL_SET_ERR_MSG(extack,
4835 "spectrum: Unknown upper device type");
4840 if (netdev_has_any_upper_dev(upper_dev) &&
4841 (!netif_is_bridge_master(upper_dev) ||
4842 !mlxsw_sp_bridge_device_is_offloaded(mlxsw_sp,
4844 NL_SET_ERR_MSG(extack,
4845 "spectrum: Enslaving a port to a device that already has an upper device is not supported");
4848 if (netif_is_lag_master(upper_dev) &&
4849 !mlxsw_sp_master_lag_check(mlxsw_sp, upper_dev,
4850 info->upper_info, extack))
4852 if (netif_is_lag_master(upper_dev) && vlan_uses_dev(dev)) {
4853 NL_SET_ERR_MSG(extack,
4854 "spectrum: Master device is a LAG master and this device has a VLAN");
4857 if (netif_is_lag_port(dev) && is_vlan_dev(upper_dev) &&
4858 !netif_is_lag_master(vlan_dev_real_dev(upper_dev))) {
4859 NL_SET_ERR_MSG(extack,
4860 "spectrum: Can not put a VLAN on a LAG port");
4863 if (netif_is_ovs_master(upper_dev) && vlan_uses_dev(dev)) {
4864 NL_SET_ERR_MSG(extack,
4865 "spectrum: Master device is an OVS master and this device has a VLAN");
4868 if (netif_is_ovs_port(dev) && is_vlan_dev(upper_dev)) {
4869 NL_SET_ERR_MSG(extack,
4870 "spectrum: Can not put a VLAN on an OVS port");
4874 case NETDEV_CHANGEUPPER:
4875 upper_dev = info->upper_dev;
4876 if (netif_is_bridge_master(upper_dev)) {
4878 err = mlxsw_sp_port_bridge_join(mlxsw_sp_port,
4883 mlxsw_sp_port_bridge_leave(mlxsw_sp_port,
4886 } else if (netif_is_lag_master(upper_dev)) {
4888 err = mlxsw_sp_port_lag_join(mlxsw_sp_port,
4891 mlxsw_sp_port_lag_leave(mlxsw_sp_port,
4893 } else if (netif_is_ovs_master(upper_dev)) {
4895 err = mlxsw_sp_port_ovs_join(mlxsw_sp_port);
4897 mlxsw_sp_port_ovs_leave(mlxsw_sp_port);
4905 static int mlxsw_sp_netdevice_port_lower_event(struct net_device *dev,
4906 unsigned long event, void *ptr)
4908 struct netdev_notifier_changelowerstate_info *info;
4909 struct mlxsw_sp_port *mlxsw_sp_port;
4912 mlxsw_sp_port = netdev_priv(dev);
4916 case NETDEV_CHANGELOWERSTATE:
4917 if (netif_is_lag_port(dev) && mlxsw_sp_port->lagged) {
4918 err = mlxsw_sp_port_lag_changed(mlxsw_sp_port,
4919 info->lower_state_info);
4921 netdev_err(dev, "Failed to reflect link aggregation lower state change\n");
4929 static int mlxsw_sp_netdevice_port_event(struct net_device *lower_dev,
4930 struct net_device *port_dev,
4931 unsigned long event, void *ptr)
4934 case NETDEV_PRECHANGEUPPER:
4935 case NETDEV_CHANGEUPPER:
4936 return mlxsw_sp_netdevice_port_upper_event(lower_dev, port_dev,
4938 case NETDEV_CHANGELOWERSTATE:
4939 return mlxsw_sp_netdevice_port_lower_event(port_dev, event,
4946 static int mlxsw_sp_netdevice_lag_event(struct net_device *lag_dev,
4947 unsigned long event, void *ptr)
4949 struct net_device *dev;
4950 struct list_head *iter;
4953 netdev_for_each_lower_dev(lag_dev, dev, iter) {
4954 if (mlxsw_sp_port_dev_check(dev)) {
4955 ret = mlxsw_sp_netdevice_port_event(lag_dev, dev, event,
4965 static int mlxsw_sp_netdevice_port_vlan_event(struct net_device *vlan_dev,
4966 struct net_device *dev,
4967 unsigned long event, void *ptr,
4970 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
4971 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
4972 struct netdev_notifier_changeupper_info *info = ptr;
4973 struct netlink_ext_ack *extack;
4974 struct net_device *upper_dev;
4977 extack = netdev_notifier_info_to_extack(&info->info);
4980 case NETDEV_PRECHANGEUPPER:
4981 upper_dev = info->upper_dev;
4982 if (!netif_is_bridge_master(upper_dev)) {
4983 NL_SET_ERR_MSG(extack, "spectrum: VLAN devices only support bridge and VRF uppers");
4988 if (netdev_has_any_upper_dev(upper_dev) &&
4989 (!netif_is_bridge_master(upper_dev) ||
4990 !mlxsw_sp_bridge_device_is_offloaded(mlxsw_sp,
4992 NL_SET_ERR_MSG(extack, "spectrum: Enslaving a port to a device that already has an upper device is not supported");
4996 case NETDEV_CHANGEUPPER:
4997 upper_dev = info->upper_dev;
4998 if (netif_is_bridge_master(upper_dev)) {
5000 err = mlxsw_sp_port_bridge_join(mlxsw_sp_port,
5005 mlxsw_sp_port_bridge_leave(mlxsw_sp_port,
5018 static int mlxsw_sp_netdevice_lag_port_vlan_event(struct net_device *vlan_dev,
5019 struct net_device *lag_dev,
5020 unsigned long event,
5023 struct net_device *dev;
5024 struct list_head *iter;
5027 netdev_for_each_lower_dev(lag_dev, dev, iter) {
5028 if (mlxsw_sp_port_dev_check(dev)) {
5029 ret = mlxsw_sp_netdevice_port_vlan_event(vlan_dev, dev,
5040 static int mlxsw_sp_netdevice_vlan_event(struct net_device *vlan_dev,
5041 unsigned long event, void *ptr)
5043 struct net_device *real_dev = vlan_dev_real_dev(vlan_dev);
5044 u16 vid = vlan_dev_vlan_id(vlan_dev);
5046 if (mlxsw_sp_port_dev_check(real_dev))
5047 return mlxsw_sp_netdevice_port_vlan_event(vlan_dev, real_dev,
5049 else if (netif_is_lag_master(real_dev))
5050 return mlxsw_sp_netdevice_lag_port_vlan_event(vlan_dev,
5057 static bool mlxsw_sp_is_vrf_event(unsigned long event, void *ptr)
5059 struct netdev_notifier_changeupper_info *info = ptr;
5061 if (event != NETDEV_PRECHANGEUPPER && event != NETDEV_CHANGEUPPER)
5063 return netif_is_l3_master(info->upper_dev);
5066 static int mlxsw_sp_netdevice_event(struct notifier_block *nb,
5067 unsigned long event, void *ptr)
5069 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
5070 struct mlxsw_sp *mlxsw_sp;
5073 mlxsw_sp = container_of(nb, struct mlxsw_sp, netdevice_nb);
5074 if (mlxsw_sp_netdev_is_ipip_ol(mlxsw_sp, dev))
5075 err = mlxsw_sp_netdevice_ipip_ol_event(mlxsw_sp, dev,
5077 else if (mlxsw_sp_netdev_is_ipip_ul(mlxsw_sp, dev))
5078 err = mlxsw_sp_netdevice_ipip_ul_event(mlxsw_sp, dev,
5080 else if (event == NETDEV_CHANGEADDR || event == NETDEV_CHANGEMTU)
5081 err = mlxsw_sp_netdevice_router_port_event(dev);
5082 else if (mlxsw_sp_is_vrf_event(event, ptr))
5083 err = mlxsw_sp_netdevice_vrf_event(dev, event, ptr);
5084 else if (mlxsw_sp_port_dev_check(dev))
5085 err = mlxsw_sp_netdevice_port_event(dev, dev, event, ptr);
5086 else if (netif_is_lag_master(dev))
5087 err = mlxsw_sp_netdevice_lag_event(dev, event, ptr);
5088 else if (is_vlan_dev(dev))
5089 err = mlxsw_sp_netdevice_vlan_event(dev, event, ptr);
5091 return notifier_from_errno(err);
5094 static struct notifier_block mlxsw_sp_inetaddr_valid_nb __read_mostly = {
5095 .notifier_call = mlxsw_sp_inetaddr_valid_event,
5098 static struct notifier_block mlxsw_sp_inetaddr_nb __read_mostly = {
5099 .notifier_call = mlxsw_sp_inetaddr_event,
5102 static struct notifier_block mlxsw_sp_inet6addr_valid_nb __read_mostly = {
5103 .notifier_call = mlxsw_sp_inet6addr_valid_event,
5106 static struct notifier_block mlxsw_sp_inet6addr_nb __read_mostly = {
5107 .notifier_call = mlxsw_sp_inet6addr_event,
5110 static const struct pci_device_id mlxsw_sp_pci_id_table[] = {
5111 {PCI_VDEVICE(MELLANOX, PCI_DEVICE_ID_MELLANOX_SPECTRUM), 0},
5115 static struct pci_driver mlxsw_sp_pci_driver = {
5116 .name = mlxsw_sp_driver_name,
5117 .id_table = mlxsw_sp_pci_id_table,
5120 static int __init mlxsw_sp_module_init(void)
5124 register_inetaddr_validator_notifier(&mlxsw_sp_inetaddr_valid_nb);
5125 register_inetaddr_notifier(&mlxsw_sp_inetaddr_nb);
5126 register_inet6addr_validator_notifier(&mlxsw_sp_inet6addr_valid_nb);
5127 register_inet6addr_notifier(&mlxsw_sp_inet6addr_nb);
5129 err = mlxsw_core_driver_register(&mlxsw_sp_driver);
5131 goto err_core_driver_register;
5133 err = mlxsw_pci_driver_register(&mlxsw_sp_pci_driver);
5135 goto err_pci_driver_register;
5139 err_pci_driver_register:
5140 mlxsw_core_driver_unregister(&mlxsw_sp_driver);
5141 err_core_driver_register:
5142 unregister_inet6addr_notifier(&mlxsw_sp_inet6addr_nb);
5143 unregister_inet6addr_validator_notifier(&mlxsw_sp_inet6addr_valid_nb);
5144 unregister_inetaddr_notifier(&mlxsw_sp_inetaddr_nb);
5145 unregister_inetaddr_validator_notifier(&mlxsw_sp_inetaddr_valid_nb);
5149 static void __exit mlxsw_sp_module_exit(void)
5151 mlxsw_pci_driver_unregister(&mlxsw_sp_pci_driver);
5152 mlxsw_core_driver_unregister(&mlxsw_sp_driver);
5153 unregister_inet6addr_notifier(&mlxsw_sp_inet6addr_nb);
5154 unregister_inet6addr_validator_notifier(&mlxsw_sp_inet6addr_valid_nb);
5155 unregister_inetaddr_notifier(&mlxsw_sp_inetaddr_nb);
5156 unregister_inetaddr_validator_notifier(&mlxsw_sp_inetaddr_valid_nb);
5159 module_init(mlxsw_sp_module_init);
5160 module_exit(mlxsw_sp_module_exit);
5162 MODULE_LICENSE("Dual BSD/GPL");
5163 MODULE_AUTHOR("Jiri Pirko <jiri@mellanox.com>");
5164 MODULE_DESCRIPTION("Mellanox Spectrum driver");
5165 MODULE_DEVICE_TABLE(pci, mlxsw_sp_pci_id_table);
5166 MODULE_FIRMWARE(MLXSW_SP_FW_FILENAME);