1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2 /* Copyright (c) 2015-2018 Mellanox Technologies. All rights reserved */
3
4 #include <linux/kernel.h>
5 #include <linux/module.h>
6 #include <linux/types.h>
7 #include <linux/pci.h>
8 #include <linux/netdevice.h>
9 #include <linux/etherdevice.h>
10 #include <linux/ethtool.h>
11 #include <linux/slab.h>
12 #include <linux/device.h>
13 #include <linux/skbuff.h>
14 #include <linux/if_vlan.h>
15 #include <linux/if_bridge.h>
16 #include <linux/workqueue.h>
17 #include <linux/jiffies.h>
18 #include <linux/bitops.h>
19 #include <linux/list.h>
20 #include <linux/notifier.h>
21 #include <linux/dcbnl.h>
22 #include <linux/inetdevice.h>
23 #include <linux/netlink.h>
24 #include <linux/jhash.h>
25 #include <linux/log2.h>
26 #include <linux/refcount.h>
27 #include <linux/rhashtable.h>
28 #include <net/switchdev.h>
29 #include <net/pkt_cls.h>
30 #include <net/netevent.h>
31 #include <net/addrconf.h>
32 #include <linux/ptp_classify.h>
33
34 #include "spectrum.h"
35 #include "pci.h"
36 #include "core.h"
37 #include "core_env.h"
38 #include "reg.h"
39 #include "port.h"
40 #include "trap.h"
41 #include "txheader.h"
42 #include "spectrum_cnt.h"
43 #include "spectrum_dpipe.h"
44 #include "spectrum_acl_flex_actions.h"
45 #include "spectrum_span.h"
46 #include "spectrum_ptp.h"
47 #include "spectrum_trap.h"
48
49 #define MLXSW_SP_FWREV_MINOR 2010
50 #define MLXSW_SP_FWREV_SUBMINOR 1006
51
52 #define MLXSW_SP1_FWREV_MAJOR 13
53 #define MLXSW_SP1_FWREV_CAN_RESET_MINOR 1702
54
55 static const struct mlxsw_fw_rev mlxsw_sp1_fw_rev = {
56 .major = MLXSW_SP1_FWREV_MAJOR,
57 .minor = MLXSW_SP_FWREV_MINOR,
58 .subminor = MLXSW_SP_FWREV_SUBMINOR,
59 .can_reset_minor = MLXSW_SP1_FWREV_CAN_RESET_MINOR,
60 };
61
62 #define MLXSW_SP1_FW_FILENAME \
63 "mellanox/mlxsw_spectrum-" __stringify(MLXSW_SP1_FWREV_MAJOR) \
64 "." __stringify(MLXSW_SP_FWREV_MINOR) \
65 "." __stringify(MLXSW_SP_FWREV_SUBMINOR) ".mfa2"
66
67 #define MLXSW_SP2_FWREV_MAJOR 29
68
69 static const struct mlxsw_fw_rev mlxsw_sp2_fw_rev = {
70 .major = MLXSW_SP2_FWREV_MAJOR,
71 .minor = MLXSW_SP_FWREV_MINOR,
72 .subminor = MLXSW_SP_FWREV_SUBMINOR,
73 };
74
75 #define MLXSW_SP2_FW_FILENAME \
76 "mellanox/mlxsw_spectrum2-" __stringify(MLXSW_SP2_FWREV_MAJOR) \
77 "." __stringify(MLXSW_SP_FWREV_MINOR) \
78 "." __stringify(MLXSW_SP_FWREV_SUBMINOR) ".mfa2"
79
80 #define MLXSW_SP3_FWREV_MAJOR 30
81
82 static const struct mlxsw_fw_rev mlxsw_sp3_fw_rev = {
83 .major = MLXSW_SP3_FWREV_MAJOR,
84 .minor = MLXSW_SP_FWREV_MINOR,
85 .subminor = MLXSW_SP_FWREV_SUBMINOR,
86 };
87
88 #define MLXSW_SP3_FW_FILENAME \
89 "mellanox/mlxsw_spectrum3-" __stringify(MLXSW_SP3_FWREV_MAJOR) \
90 "." __stringify(MLXSW_SP_FWREV_MINOR) \
91 "." __stringify(MLXSW_SP_FWREV_SUBMINOR) ".mfa2"
92
93 #define MLXSW_SP_LINECARDS_INI_BUNDLE_FILENAME \
94 "mellanox/lc_ini_bundle_" \
95 __stringify(MLXSW_SP_FWREV_MINOR) "_" \
96 __stringify(MLXSW_SP_FWREV_SUBMINOR) ".bin"
97
98 static const char mlxsw_sp1_driver_name[] = "mlxsw_spectrum";
99 static const char mlxsw_sp2_driver_name[] = "mlxsw_spectrum2";
100 static const char mlxsw_sp3_driver_name[] = "mlxsw_spectrum3";
101 static const char mlxsw_sp4_driver_name[] = "mlxsw_spectrum4";
102
103 static const unsigned char mlxsw_sp1_mac_mask[ETH_ALEN] = {
104 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00
105 };
106 static const unsigned char mlxsw_sp2_mac_mask[ETH_ALEN] = {
107 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00
108 };
109
110 /* tx_hdr_version
111 * Tx header version.
112 * Must be set to 1.
113 */
114 MLXSW_ITEM32(tx, hdr, version, 0x00, 28, 4);
115
116 /* tx_hdr_ctl
117 * Packet control type.
118 * 0 - Ethernet control (e.g. EMADs, LACP)
119 * 1 - Ethernet data
120 */
121 MLXSW_ITEM32(tx, hdr, ctl, 0x00, 26, 2);
122
123 /* tx_hdr_proto
124 * Packet protocol type. Must be set to 1 (Ethernet).
125 */
126 MLXSW_ITEM32(tx, hdr, proto, 0x00, 21, 3);
127
128 /* tx_hdr_rx_is_router
129 * Packet is sent from the router. Valid for data packets only.
130 */
131 MLXSW_ITEM32(tx, hdr, rx_is_router, 0x00, 19, 1);
132
133 /* tx_hdr_fid_valid
134 * Indicates if the 'fid' field is valid and should be used for
135 * forwarding lookup. Valid for data packets only.
136 */
137 MLXSW_ITEM32(tx, hdr, fid_valid, 0x00, 16, 1);
138
139 /* tx_hdr_swid
140 * Switch partition ID. Must be set to 0.
141 */
142 MLXSW_ITEM32(tx, hdr, swid, 0x00, 12, 3);
143
144 /* tx_hdr_control_tclass
145 * Indicates if the packet should use the control TClass and not one
146 * of the data TClasses.
147 */
148 MLXSW_ITEM32(tx, hdr, control_tclass, 0x00, 6, 1);
149
150 /* tx_hdr_etclass
151 * Egress TClass to be used on the egress device on the egress port.
152 */
153 MLXSW_ITEM32(tx, hdr, etclass, 0x00, 0, 4);
154
155 /* tx_hdr_port_mid
156 * Destination local port for unicast packets.
157 * Destination multicast ID for multicast packets.
158 *
159 * Control packets are directed to a specific egress port, while data
160 * packets are transmitted through the CPU port (0) into the switch partition,
161 * where forwarding rules are applied.
162 */
163 MLXSW_ITEM32(tx, hdr, port_mid, 0x04, 16, 16);
164
165 /* tx_hdr_fid
166 * Forwarding ID used for L2 forwarding lookup. Valid only if 'fid_valid' is
167 * set, otherwise calculated based on the packet's VID using VID to FID mapping.
168 * Valid for data packets only.
169 */
170 MLXSW_ITEM32(tx, hdr, fid, 0x08, 16, 16);
171
172 /* tx_hdr_type
173 * 0 - Data packets
174 * 6 - Control packets
175 */
176 MLXSW_ITEM32(tx, hdr, type, 0x0C, 0, 4);
177
mlxsw_sp_flow_counter_get(struct mlxsw_sp * mlxsw_sp,unsigned int counter_index,bool clear,u64 * packets,u64 * bytes)178 int mlxsw_sp_flow_counter_get(struct mlxsw_sp *mlxsw_sp,
179 unsigned int counter_index, bool clear,
180 u64 *packets, u64 *bytes)
181 {
182 enum mlxsw_reg_mgpc_opcode op = clear ? MLXSW_REG_MGPC_OPCODE_CLEAR :
183 MLXSW_REG_MGPC_OPCODE_NOP;
184 char mgpc_pl[MLXSW_REG_MGPC_LEN];
185 int err;
186
187 mlxsw_reg_mgpc_pack(mgpc_pl, counter_index, op,
188 MLXSW_REG_FLOW_COUNTER_SET_TYPE_PACKETS_BYTES);
189 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(mgpc), mgpc_pl);
190 if (err)
191 return err;
192 if (packets)
193 *packets = mlxsw_reg_mgpc_packet_counter_get(mgpc_pl);
194 if (bytes)
195 *bytes = mlxsw_reg_mgpc_byte_counter_get(mgpc_pl);
196 return 0;
197 }
198
mlxsw_sp_flow_counter_clear(struct mlxsw_sp * mlxsw_sp,unsigned int counter_index)199 static int mlxsw_sp_flow_counter_clear(struct mlxsw_sp *mlxsw_sp,
200 unsigned int counter_index)
201 {
202 char mgpc_pl[MLXSW_REG_MGPC_LEN];
203
204 mlxsw_reg_mgpc_pack(mgpc_pl, counter_index, MLXSW_REG_MGPC_OPCODE_CLEAR,
205 MLXSW_REG_FLOW_COUNTER_SET_TYPE_PACKETS_BYTES);
206 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(mgpc), mgpc_pl);
207 }
208
mlxsw_sp_flow_counter_alloc(struct mlxsw_sp * mlxsw_sp,unsigned int * p_counter_index)209 int mlxsw_sp_flow_counter_alloc(struct mlxsw_sp *mlxsw_sp,
210 unsigned int *p_counter_index)
211 {
212 int err;
213
214 err = mlxsw_sp_counter_alloc(mlxsw_sp, MLXSW_SP_COUNTER_SUB_POOL_FLOW,
215 p_counter_index);
216 if (err)
217 return err;
218 err = mlxsw_sp_flow_counter_clear(mlxsw_sp, *p_counter_index);
219 if (err)
220 goto err_counter_clear;
221 return 0;
222
223 err_counter_clear:
224 mlxsw_sp_counter_free(mlxsw_sp, MLXSW_SP_COUNTER_SUB_POOL_FLOW,
225 *p_counter_index);
226 return err;
227 }
228
mlxsw_sp_flow_counter_free(struct mlxsw_sp * mlxsw_sp,unsigned int counter_index)229 void mlxsw_sp_flow_counter_free(struct mlxsw_sp *mlxsw_sp,
230 unsigned int counter_index)
231 {
232 mlxsw_sp_counter_free(mlxsw_sp, MLXSW_SP_COUNTER_SUB_POOL_FLOW,
233 counter_index);
234 }
235
mlxsw_sp_txhdr_construct(struct sk_buff * skb,const struct mlxsw_tx_info * tx_info)236 void mlxsw_sp_txhdr_construct(struct sk_buff *skb,
237 const struct mlxsw_tx_info *tx_info)
238 {
239 char *txhdr = skb_push(skb, MLXSW_TXHDR_LEN);
240
241 memset(txhdr, 0, MLXSW_TXHDR_LEN);
242
243 mlxsw_tx_hdr_version_set(txhdr, MLXSW_TXHDR_VERSION_1);
244 mlxsw_tx_hdr_ctl_set(txhdr, MLXSW_TXHDR_ETH_CTL);
245 mlxsw_tx_hdr_proto_set(txhdr, MLXSW_TXHDR_PROTO_ETH);
246 mlxsw_tx_hdr_swid_set(txhdr, 0);
247 mlxsw_tx_hdr_control_tclass_set(txhdr, 1);
248 mlxsw_tx_hdr_port_mid_set(txhdr, tx_info->local_port);
249 mlxsw_tx_hdr_type_set(txhdr, MLXSW_TXHDR_TYPE_CONTROL);
250 }
251
252 int
mlxsw_sp_txhdr_ptp_data_construct(struct mlxsw_core * mlxsw_core,struct mlxsw_sp_port * mlxsw_sp_port,struct sk_buff * skb,const struct mlxsw_tx_info * tx_info)253 mlxsw_sp_txhdr_ptp_data_construct(struct mlxsw_core *mlxsw_core,
254 struct mlxsw_sp_port *mlxsw_sp_port,
255 struct sk_buff *skb,
256 const struct mlxsw_tx_info *tx_info)
257 {
258 char *txhdr;
259 u16 max_fid;
260 int err;
261
262 if (skb_cow_head(skb, MLXSW_TXHDR_LEN)) {
263 err = -ENOMEM;
264 goto err_skb_cow_head;
265 }
266
267 if (!MLXSW_CORE_RES_VALID(mlxsw_core, FID)) {
268 err = -EIO;
269 goto err_res_valid;
270 }
271 max_fid = MLXSW_CORE_RES_GET(mlxsw_core, FID);
272
273 txhdr = skb_push(skb, MLXSW_TXHDR_LEN);
274 memset(txhdr, 0, MLXSW_TXHDR_LEN);
275
276 mlxsw_tx_hdr_version_set(txhdr, MLXSW_TXHDR_VERSION_1);
277 mlxsw_tx_hdr_proto_set(txhdr, MLXSW_TXHDR_PROTO_ETH);
278 mlxsw_tx_hdr_rx_is_router_set(txhdr, true);
279 mlxsw_tx_hdr_fid_valid_set(txhdr, true);
280 mlxsw_tx_hdr_fid_set(txhdr, max_fid + tx_info->local_port - 1);
281 mlxsw_tx_hdr_type_set(txhdr, MLXSW_TXHDR_TYPE_DATA);
282 return 0;
283
284 err_res_valid:
285 err_skb_cow_head:
286 this_cpu_inc(mlxsw_sp_port->pcpu_stats->tx_dropped);
287 dev_kfree_skb_any(skb);
288 return err;
289 }
290
mlxsw_sp_skb_requires_ts(struct sk_buff * skb)291 static bool mlxsw_sp_skb_requires_ts(struct sk_buff *skb)
292 {
293 unsigned int type;
294
295 if (!(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP))
296 return false;
297
298 type = ptp_classify_raw(skb);
299 return !!ptp_parse_header(skb, type);
300 }
301
mlxsw_sp_txhdr_handle(struct mlxsw_core * mlxsw_core,struct mlxsw_sp_port * mlxsw_sp_port,struct sk_buff * skb,const struct mlxsw_tx_info * tx_info)302 static int mlxsw_sp_txhdr_handle(struct mlxsw_core *mlxsw_core,
303 struct mlxsw_sp_port *mlxsw_sp_port,
304 struct sk_buff *skb,
305 const struct mlxsw_tx_info *tx_info)
306 {
307 struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
308
309 /* In Spectrum-2 and Spectrum-3, PTP events that require a time stamp
310 * need special handling and cannot be transmitted as regular control
311 * packets.
312 */
313 if (unlikely(mlxsw_sp_skb_requires_ts(skb)))
314 return mlxsw_sp->ptp_ops->txhdr_construct(mlxsw_core,
315 mlxsw_sp_port, skb,
316 tx_info);
317
318 if (skb_cow_head(skb, MLXSW_TXHDR_LEN)) {
319 this_cpu_inc(mlxsw_sp_port->pcpu_stats->tx_dropped);
320 dev_kfree_skb_any(skb);
321 return -ENOMEM;
322 }
323
324 mlxsw_sp_txhdr_construct(skb, tx_info);
325 return 0;
326 }
327
mlxsw_sp_stp_spms_state(u8 state)328 enum mlxsw_reg_spms_state mlxsw_sp_stp_spms_state(u8 state)
329 {
330 switch (state) {
331 case BR_STATE_FORWARDING:
332 return MLXSW_REG_SPMS_STATE_FORWARDING;
333 case BR_STATE_LEARNING:
334 return MLXSW_REG_SPMS_STATE_LEARNING;
335 case BR_STATE_LISTENING:
336 case BR_STATE_DISABLED:
337 case BR_STATE_BLOCKING:
338 return MLXSW_REG_SPMS_STATE_DISCARDING;
339 default:
340 BUG();
341 }
342 }
343
mlxsw_sp_port_vid_stp_set(struct mlxsw_sp_port * mlxsw_sp_port,u16 vid,u8 state)344 int mlxsw_sp_port_vid_stp_set(struct mlxsw_sp_port *mlxsw_sp_port, u16 vid,
345 u8 state)
346 {
347 enum mlxsw_reg_spms_state spms_state = mlxsw_sp_stp_spms_state(state);
348 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
349 char *spms_pl;
350 int err;
351
352 spms_pl = kmalloc(MLXSW_REG_SPMS_LEN, GFP_KERNEL);
353 if (!spms_pl)
354 return -ENOMEM;
355 mlxsw_reg_spms_pack(spms_pl, mlxsw_sp_port->local_port);
356 mlxsw_reg_spms_vid_pack(spms_pl, vid, spms_state);
357
358 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(spms), spms_pl);
359 kfree(spms_pl);
360 return err;
361 }
362
mlxsw_sp_base_mac_get(struct mlxsw_sp * mlxsw_sp)363 static int mlxsw_sp_base_mac_get(struct mlxsw_sp *mlxsw_sp)
364 {
365 char spad_pl[MLXSW_REG_SPAD_LEN] = {0};
366 int err;
367
368 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(spad), spad_pl);
369 if (err)
370 return err;
371 mlxsw_reg_spad_base_mac_memcpy_from(spad_pl, mlxsw_sp->base_mac);
372 return 0;
373 }
374
mlxsw_sp_port_admin_status_set(struct mlxsw_sp_port * mlxsw_sp_port,bool is_up)375 int mlxsw_sp_port_admin_status_set(struct mlxsw_sp_port *mlxsw_sp_port,
376 bool is_up)
377 {
378 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
379 char paos_pl[MLXSW_REG_PAOS_LEN];
380
381 mlxsw_reg_paos_pack(paos_pl, mlxsw_sp_port->local_port,
382 is_up ? MLXSW_PORT_ADMIN_STATUS_UP :
383 MLXSW_PORT_ADMIN_STATUS_DOWN);
384 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(paos), paos_pl);
385 }
386
mlxsw_sp_port_dev_addr_set(struct mlxsw_sp_port * mlxsw_sp_port,const unsigned char * addr)387 static int mlxsw_sp_port_dev_addr_set(struct mlxsw_sp_port *mlxsw_sp_port,
388 const unsigned char *addr)
389 {
390 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
391 char ppad_pl[MLXSW_REG_PPAD_LEN];
392
393 mlxsw_reg_ppad_pack(ppad_pl, true, mlxsw_sp_port->local_port);
394 mlxsw_reg_ppad_mac_memcpy_to(ppad_pl, addr);
395 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ppad), ppad_pl);
396 }
397
mlxsw_sp_port_dev_addr_init(struct mlxsw_sp_port * mlxsw_sp_port)398 static int mlxsw_sp_port_dev_addr_init(struct mlxsw_sp_port *mlxsw_sp_port)
399 {
400 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
401
402 eth_hw_addr_gen(mlxsw_sp_port->dev, mlxsw_sp->base_mac,
403 mlxsw_sp_port->local_port);
404 return mlxsw_sp_port_dev_addr_set(mlxsw_sp_port,
405 mlxsw_sp_port->dev->dev_addr);
406 }
407
mlxsw_sp_port_mtu_set(struct mlxsw_sp_port * mlxsw_sp_port,u16 mtu)408 static int mlxsw_sp_port_mtu_set(struct mlxsw_sp_port *mlxsw_sp_port, u16 mtu)
409 {
410 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
411 char pmtu_pl[MLXSW_REG_PMTU_LEN];
412
413 mtu += MLXSW_PORT_ETH_FRAME_HDR;
414
415 mlxsw_reg_pmtu_pack(pmtu_pl, mlxsw_sp_port->local_port, mtu);
416 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(pmtu), pmtu_pl);
417 }
418
mlxsw_sp_port_swid_set(struct mlxsw_sp * mlxsw_sp,u16 local_port,u8 swid)419 static int mlxsw_sp_port_swid_set(struct mlxsw_sp *mlxsw_sp,
420 u16 local_port, u8 swid)
421 {
422 char pspa_pl[MLXSW_REG_PSPA_LEN];
423
424 mlxsw_reg_pspa_pack(pspa_pl, swid, local_port);
425 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(pspa), pspa_pl);
426 }
427
mlxsw_sp_port_vp_mode_set(struct mlxsw_sp_port * mlxsw_sp_port,bool enable)428 int mlxsw_sp_port_vp_mode_set(struct mlxsw_sp_port *mlxsw_sp_port, bool enable)
429 {
430 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
431 char svpe_pl[MLXSW_REG_SVPE_LEN];
432
433 mlxsw_reg_svpe_pack(svpe_pl, mlxsw_sp_port->local_port, enable);
434 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(svpe), svpe_pl);
435 }
436
mlxsw_sp_port_vid_learning_set(struct mlxsw_sp_port * mlxsw_sp_port,u16 vid,bool learn_enable)437 int mlxsw_sp_port_vid_learning_set(struct mlxsw_sp_port *mlxsw_sp_port, u16 vid,
438 bool learn_enable)
439 {
440 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
441 char *spvmlr_pl;
442 int err;
443
444 spvmlr_pl = kmalloc(MLXSW_REG_SPVMLR_LEN, GFP_KERNEL);
445 if (!spvmlr_pl)
446 return -ENOMEM;
447 mlxsw_reg_spvmlr_pack(spvmlr_pl, mlxsw_sp_port->local_port, vid, vid,
448 learn_enable);
449 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(spvmlr), spvmlr_pl);
450 kfree(spvmlr_pl);
451 return err;
452 }
453
mlxsw_sp_port_security_set(struct mlxsw_sp_port * mlxsw_sp_port,bool enable)454 int mlxsw_sp_port_security_set(struct mlxsw_sp_port *mlxsw_sp_port, bool enable)
455 {
456 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
457 char spfsr_pl[MLXSW_REG_SPFSR_LEN];
458 int err;
459
460 if (mlxsw_sp_port->security == enable)
461 return 0;
462
463 mlxsw_reg_spfsr_pack(spfsr_pl, mlxsw_sp_port->local_port, enable);
464 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(spfsr), spfsr_pl);
465 if (err)
466 return err;
467
468 mlxsw_sp_port->security = enable;
469 return 0;
470 }
471
mlxsw_sp_ethtype_to_sver_type(u16 ethtype,u8 * p_sver_type)472 int mlxsw_sp_ethtype_to_sver_type(u16 ethtype, u8 *p_sver_type)
473 {
474 switch (ethtype) {
475 case ETH_P_8021Q:
476 *p_sver_type = 0;
477 break;
478 case ETH_P_8021AD:
479 *p_sver_type = 1;
480 break;
481 default:
482 return -EINVAL;
483 }
484
485 return 0;
486 }
487
mlxsw_sp_port_egress_ethtype_set(struct mlxsw_sp_port * mlxsw_sp_port,u16 ethtype)488 int mlxsw_sp_port_egress_ethtype_set(struct mlxsw_sp_port *mlxsw_sp_port,
489 u16 ethtype)
490 {
491 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
492 char spevet_pl[MLXSW_REG_SPEVET_LEN];
493 u8 sver_type;
494 int err;
495
496 err = mlxsw_sp_ethtype_to_sver_type(ethtype, &sver_type);
497 if (err)
498 return err;
499
500 mlxsw_reg_spevet_pack(spevet_pl, mlxsw_sp_port->local_port, sver_type);
501 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(spevet), spevet_pl);
502 }
503
__mlxsw_sp_port_pvid_set(struct mlxsw_sp_port * mlxsw_sp_port,u16 vid,u16 ethtype)504 static int __mlxsw_sp_port_pvid_set(struct mlxsw_sp_port *mlxsw_sp_port,
505 u16 vid, u16 ethtype)
506 {
507 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
508 char spvid_pl[MLXSW_REG_SPVID_LEN];
509 u8 sver_type;
510 int err;
511
512 err = mlxsw_sp_ethtype_to_sver_type(ethtype, &sver_type);
513 if (err)
514 return err;
515
516 mlxsw_reg_spvid_pack(spvid_pl, mlxsw_sp_port->local_port, vid,
517 sver_type);
518
519 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(spvid), spvid_pl);
520 }
521
mlxsw_sp_port_allow_untagged_set(struct mlxsw_sp_port * mlxsw_sp_port,bool allow)522 static int mlxsw_sp_port_allow_untagged_set(struct mlxsw_sp_port *mlxsw_sp_port,
523 bool allow)
524 {
525 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
526 char spaft_pl[MLXSW_REG_SPAFT_LEN];
527
528 mlxsw_reg_spaft_pack(spaft_pl, mlxsw_sp_port->local_port, allow);
529 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(spaft), spaft_pl);
530 }
531
mlxsw_sp_port_pvid_set(struct mlxsw_sp_port * mlxsw_sp_port,u16 vid,u16 ethtype)532 int mlxsw_sp_port_pvid_set(struct mlxsw_sp_port *mlxsw_sp_port, u16 vid,
533 u16 ethtype)
534 {
535 int err;
536
537 if (!vid) {
538 err = mlxsw_sp_port_allow_untagged_set(mlxsw_sp_port, false);
539 if (err)
540 return err;
541 } else {
542 err = __mlxsw_sp_port_pvid_set(mlxsw_sp_port, vid, ethtype);
543 if (err)
544 return err;
545 err = mlxsw_sp_port_allow_untagged_set(mlxsw_sp_port, true);
546 if (err)
547 goto err_port_allow_untagged_set;
548 }
549
550 mlxsw_sp_port->pvid = vid;
551 return 0;
552
553 err_port_allow_untagged_set:
554 __mlxsw_sp_port_pvid_set(mlxsw_sp_port, mlxsw_sp_port->pvid, ethtype);
555 return err;
556 }
557
558 static int
mlxsw_sp_port_system_port_mapping_set(struct mlxsw_sp_port * mlxsw_sp_port)559 mlxsw_sp_port_system_port_mapping_set(struct mlxsw_sp_port *mlxsw_sp_port)
560 {
561 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
562 char sspr_pl[MLXSW_REG_SSPR_LEN];
563
564 mlxsw_reg_sspr_pack(sspr_pl, mlxsw_sp_port->local_port);
565 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sspr), sspr_pl);
566 }
567
568 static int
mlxsw_sp_port_module_info_parse(struct mlxsw_sp * mlxsw_sp,u16 local_port,char * pmlp_pl,struct mlxsw_sp_port_mapping * port_mapping)569 mlxsw_sp_port_module_info_parse(struct mlxsw_sp *mlxsw_sp,
570 u16 local_port, char *pmlp_pl,
571 struct mlxsw_sp_port_mapping *port_mapping)
572 {
573 bool separate_rxtx;
574 u8 first_lane;
575 u8 slot_index;
576 u8 module;
577 u8 width;
578 int i;
579
580 module = mlxsw_reg_pmlp_module_get(pmlp_pl, 0);
581 slot_index = mlxsw_reg_pmlp_slot_index_get(pmlp_pl, 0);
582 width = mlxsw_reg_pmlp_width_get(pmlp_pl);
583 separate_rxtx = mlxsw_reg_pmlp_rxtx_get(pmlp_pl);
584 first_lane = mlxsw_reg_pmlp_tx_lane_get(pmlp_pl, 0);
585
586 if (width && !is_power_of_2(width)) {
587 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Unsupported module config: width value is not power of 2\n",
588 local_port);
589 return -EINVAL;
590 }
591
592 for (i = 0; i < width; i++) {
593 if (mlxsw_reg_pmlp_module_get(pmlp_pl, i) != module) {
594 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Unsupported module config: contains multiple modules\n",
595 local_port);
596 return -EINVAL;
597 }
598 if (mlxsw_reg_pmlp_slot_index_get(pmlp_pl, i) != slot_index) {
599 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Unsupported module config: contains multiple slot indexes\n",
600 local_port);
601 return -EINVAL;
602 }
603 if (separate_rxtx &&
604 mlxsw_reg_pmlp_tx_lane_get(pmlp_pl, i) !=
605 mlxsw_reg_pmlp_rx_lane_get(pmlp_pl, i)) {
606 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Unsupported module config: TX and RX lane numbers are different\n",
607 local_port);
608 return -EINVAL;
609 }
610 if (mlxsw_reg_pmlp_tx_lane_get(pmlp_pl, i) != i + first_lane) {
611 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Unsupported module config: TX and RX lane numbers are not sequential\n",
612 local_port);
613 return -EINVAL;
614 }
615 }
616
617 port_mapping->module = module;
618 port_mapping->slot_index = slot_index;
619 port_mapping->width = width;
620 port_mapping->module_width = width;
621 port_mapping->lane = mlxsw_reg_pmlp_tx_lane_get(pmlp_pl, 0);
622 return 0;
623 }
624
625 static int
mlxsw_sp_port_module_info_get(struct mlxsw_sp * mlxsw_sp,u16 local_port,struct mlxsw_sp_port_mapping * port_mapping)626 mlxsw_sp_port_module_info_get(struct mlxsw_sp *mlxsw_sp, u16 local_port,
627 struct mlxsw_sp_port_mapping *port_mapping)
628 {
629 char pmlp_pl[MLXSW_REG_PMLP_LEN];
630 int err;
631
632 mlxsw_reg_pmlp_pack(pmlp_pl, local_port);
633 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(pmlp), pmlp_pl);
634 if (err)
635 return err;
636 return mlxsw_sp_port_module_info_parse(mlxsw_sp, local_port,
637 pmlp_pl, port_mapping);
638 }
639
640 static int
mlxsw_sp_port_module_map(struct mlxsw_sp * mlxsw_sp,u16 local_port,const struct mlxsw_sp_port_mapping * port_mapping)641 mlxsw_sp_port_module_map(struct mlxsw_sp *mlxsw_sp, u16 local_port,
642 const struct mlxsw_sp_port_mapping *port_mapping)
643 {
644 char pmlp_pl[MLXSW_REG_PMLP_LEN];
645 int i, err;
646
647 mlxsw_env_module_port_map(mlxsw_sp->core, port_mapping->slot_index,
648 port_mapping->module);
649
650 mlxsw_reg_pmlp_pack(pmlp_pl, local_port);
651 mlxsw_reg_pmlp_width_set(pmlp_pl, port_mapping->width);
652 for (i = 0; i < port_mapping->width; i++) {
653 mlxsw_reg_pmlp_slot_index_set(pmlp_pl, i,
654 port_mapping->slot_index);
655 mlxsw_reg_pmlp_module_set(pmlp_pl, i, port_mapping->module);
656 mlxsw_reg_pmlp_tx_lane_set(pmlp_pl, i, port_mapping->lane + i); /* Rx & Tx */
657 }
658
659 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(pmlp), pmlp_pl);
660 if (err)
661 goto err_pmlp_write;
662 return 0;
663
664 err_pmlp_write:
665 mlxsw_env_module_port_unmap(mlxsw_sp->core, port_mapping->slot_index,
666 port_mapping->module);
667 return err;
668 }
669
mlxsw_sp_port_module_unmap(struct mlxsw_sp * mlxsw_sp,u16 local_port,u8 slot_index,u8 module)670 static void mlxsw_sp_port_module_unmap(struct mlxsw_sp *mlxsw_sp, u16 local_port,
671 u8 slot_index, u8 module)
672 {
673 char pmlp_pl[MLXSW_REG_PMLP_LEN];
674
675 mlxsw_reg_pmlp_pack(pmlp_pl, local_port);
676 mlxsw_reg_pmlp_width_set(pmlp_pl, 0);
677 mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(pmlp), pmlp_pl);
678 mlxsw_env_module_port_unmap(mlxsw_sp->core, slot_index, module);
679 }
680
mlxsw_sp_port_open(struct net_device * dev)681 static int mlxsw_sp_port_open(struct net_device *dev)
682 {
683 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
684 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
685 int err;
686
687 err = mlxsw_env_module_port_up(mlxsw_sp->core,
688 mlxsw_sp_port->mapping.slot_index,
689 mlxsw_sp_port->mapping.module);
690 if (err)
691 return err;
692 err = mlxsw_sp_port_admin_status_set(mlxsw_sp_port, true);
693 if (err)
694 goto err_port_admin_status_set;
695 netif_start_queue(dev);
696 return 0;
697
698 err_port_admin_status_set:
699 mlxsw_env_module_port_down(mlxsw_sp->core,
700 mlxsw_sp_port->mapping.slot_index,
701 mlxsw_sp_port->mapping.module);
702 return err;
703 }
704
mlxsw_sp_port_stop(struct net_device * dev)705 static int mlxsw_sp_port_stop(struct net_device *dev)
706 {
707 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
708 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
709
710 netif_stop_queue(dev);
711 mlxsw_sp_port_admin_status_set(mlxsw_sp_port, false);
712 mlxsw_env_module_port_down(mlxsw_sp->core,
713 mlxsw_sp_port->mapping.slot_index,
714 mlxsw_sp_port->mapping.module);
715 return 0;
716 }
717
mlxsw_sp_port_xmit(struct sk_buff * skb,struct net_device * dev)718 static netdev_tx_t mlxsw_sp_port_xmit(struct sk_buff *skb,
719 struct net_device *dev)
720 {
721 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
722 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
723 struct mlxsw_sp_port_pcpu_stats *pcpu_stats;
724 const struct mlxsw_tx_info tx_info = {
725 .local_port = mlxsw_sp_port->local_port,
726 .is_emad = false,
727 };
728 u64 len;
729 int err;
730
731 memset(skb->cb, 0, sizeof(struct mlxsw_skb_cb));
732
733 if (mlxsw_core_skb_transmit_busy(mlxsw_sp->core, &tx_info))
734 return NETDEV_TX_BUSY;
735
736 if (eth_skb_pad(skb)) {
737 this_cpu_inc(mlxsw_sp_port->pcpu_stats->tx_dropped);
738 return NETDEV_TX_OK;
739 }
740
741 err = mlxsw_sp_txhdr_handle(mlxsw_sp->core, mlxsw_sp_port, skb,
742 &tx_info);
743 if (err)
744 return NETDEV_TX_OK;
745
746 /* TX header is consumed by HW on the way so we shouldn't count its
747 * bytes as being sent.
748 */
749 len = skb->len - MLXSW_TXHDR_LEN;
750
751 /* Due to a race we might fail here because of a full queue. In that
752 * unlikely case we simply drop the packet.
753 */
754 err = mlxsw_core_skb_transmit(mlxsw_sp->core, skb, &tx_info);
755
756 if (!err) {
757 pcpu_stats = this_cpu_ptr(mlxsw_sp_port->pcpu_stats);
758 u64_stats_update_begin(&pcpu_stats->syncp);
759 pcpu_stats->tx_packets++;
760 pcpu_stats->tx_bytes += len;
761 u64_stats_update_end(&pcpu_stats->syncp);
762 } else {
763 this_cpu_inc(mlxsw_sp_port->pcpu_stats->tx_dropped);
764 dev_kfree_skb_any(skb);
765 }
766 return NETDEV_TX_OK;
767 }
768
mlxsw_sp_set_rx_mode(struct net_device * dev)769 static void mlxsw_sp_set_rx_mode(struct net_device *dev)
770 {
771 }
772
mlxsw_sp_port_set_mac_address(struct net_device * dev,void * p)773 static int mlxsw_sp_port_set_mac_address(struct net_device *dev, void *p)
774 {
775 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
776 struct sockaddr *addr = p;
777 int err;
778
779 if (!is_valid_ether_addr(addr->sa_data))
780 return -EADDRNOTAVAIL;
781
782 err = mlxsw_sp_port_dev_addr_set(mlxsw_sp_port, addr->sa_data);
783 if (err)
784 return err;
785 eth_hw_addr_set(dev, addr->sa_data);
786 return 0;
787 }
788
mlxsw_sp_port_change_mtu(struct net_device * dev,int mtu)789 static int mlxsw_sp_port_change_mtu(struct net_device *dev, int mtu)
790 {
791 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
792 struct mlxsw_sp_hdroom orig_hdroom;
793 struct mlxsw_sp_hdroom hdroom;
794 int err;
795
796 orig_hdroom = *mlxsw_sp_port->hdroom;
797
798 hdroom = orig_hdroom;
799 hdroom.mtu = mtu;
800 mlxsw_sp_hdroom_bufs_reset_sizes(mlxsw_sp_port, &hdroom);
801
802 err = mlxsw_sp_hdroom_configure(mlxsw_sp_port, &hdroom);
803 if (err) {
804 netdev_err(dev, "Failed to configure port's headroom\n");
805 return err;
806 }
807
808 err = mlxsw_sp_port_mtu_set(mlxsw_sp_port, mtu);
809 if (err)
810 goto err_port_mtu_set;
811 WRITE_ONCE(dev->mtu, mtu);
812 return 0;
813
814 err_port_mtu_set:
815 mlxsw_sp_hdroom_configure(mlxsw_sp_port, &orig_hdroom);
816 return err;
817 }
818
819 static int
mlxsw_sp_port_get_sw_stats64(const struct net_device * dev,struct rtnl_link_stats64 * stats)820 mlxsw_sp_port_get_sw_stats64(const struct net_device *dev,
821 struct rtnl_link_stats64 *stats)
822 {
823 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
824 struct mlxsw_sp_port_pcpu_stats *p;
825 u64 rx_packets, rx_bytes, tx_packets, tx_bytes;
826 u32 tx_dropped = 0;
827 unsigned int start;
828 int i;
829
830 for_each_possible_cpu(i) {
831 p = per_cpu_ptr(mlxsw_sp_port->pcpu_stats, i);
832 do {
833 start = u64_stats_fetch_begin(&p->syncp);
834 rx_packets = p->rx_packets;
835 rx_bytes = p->rx_bytes;
836 tx_packets = p->tx_packets;
837 tx_bytes = p->tx_bytes;
838 } while (u64_stats_fetch_retry(&p->syncp, start));
839
840 stats->rx_packets += rx_packets;
841 stats->rx_bytes += rx_bytes;
842 stats->tx_packets += tx_packets;
843 stats->tx_bytes += tx_bytes;
844 /* tx_dropped is u32, updated without syncp protection. */
845 tx_dropped += p->tx_dropped;
846 }
847 stats->tx_dropped = tx_dropped;
848 return 0;
849 }
850
mlxsw_sp_port_has_offload_stats(const struct net_device * dev,int attr_id)851 static bool mlxsw_sp_port_has_offload_stats(const struct net_device *dev, int attr_id)
852 {
853 switch (attr_id) {
854 case IFLA_OFFLOAD_XSTATS_CPU_HIT:
855 return true;
856 }
857
858 return false;
859 }
860
mlxsw_sp_port_get_offload_stats(int attr_id,const struct net_device * dev,void * sp)861 static int mlxsw_sp_port_get_offload_stats(int attr_id, const struct net_device *dev,
862 void *sp)
863 {
864 switch (attr_id) {
865 case IFLA_OFFLOAD_XSTATS_CPU_HIT:
866 return mlxsw_sp_port_get_sw_stats64(dev, sp);
867 }
868
869 return -EINVAL;
870 }
871
mlxsw_sp_port_get_stats_raw(struct net_device * dev,int grp,int prio,char * ppcnt_pl)872 int mlxsw_sp_port_get_stats_raw(struct net_device *dev, int grp,
873 int prio, char *ppcnt_pl)
874 {
875 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
876 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
877
878 mlxsw_reg_ppcnt_pack(ppcnt_pl, mlxsw_sp_port->local_port, grp, prio);
879 return mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(ppcnt), ppcnt_pl);
880 }
881
mlxsw_sp_port_get_hw_stats(struct net_device * dev,struct rtnl_link_stats64 * stats)882 static int mlxsw_sp_port_get_hw_stats(struct net_device *dev,
883 struct rtnl_link_stats64 *stats)
884 {
885 char ppcnt_pl[MLXSW_REG_PPCNT_LEN];
886 int err;
887
888 err = mlxsw_sp_port_get_stats_raw(dev, MLXSW_REG_PPCNT_IEEE_8023_CNT,
889 0, ppcnt_pl);
890 if (err)
891 goto out;
892
893 stats->tx_packets =
894 mlxsw_reg_ppcnt_a_frames_transmitted_ok_get(ppcnt_pl);
895 stats->rx_packets =
896 mlxsw_reg_ppcnt_a_frames_received_ok_get(ppcnt_pl);
897 stats->tx_bytes =
898 mlxsw_reg_ppcnt_a_octets_transmitted_ok_get(ppcnt_pl);
899 stats->rx_bytes =
900 mlxsw_reg_ppcnt_a_octets_received_ok_get(ppcnt_pl);
901 stats->multicast =
902 mlxsw_reg_ppcnt_a_multicast_frames_received_ok_get(ppcnt_pl);
903
904 stats->rx_crc_errors =
905 mlxsw_reg_ppcnt_a_frame_check_sequence_errors_get(ppcnt_pl);
906 stats->rx_frame_errors =
907 mlxsw_reg_ppcnt_a_alignment_errors_get(ppcnt_pl);
908
909 stats->rx_length_errors = (
910 mlxsw_reg_ppcnt_a_in_range_length_errors_get(ppcnt_pl) +
911 mlxsw_reg_ppcnt_a_out_of_range_length_field_get(ppcnt_pl) +
912 mlxsw_reg_ppcnt_a_frame_too_long_errors_get(ppcnt_pl));
913
914 stats->rx_errors = (stats->rx_crc_errors +
915 stats->rx_frame_errors + stats->rx_length_errors);
916
917 out:
918 return err;
919 }
920
921 static void
mlxsw_sp_port_get_hw_xstats(struct net_device * dev,struct mlxsw_sp_port_xstats * xstats)922 mlxsw_sp_port_get_hw_xstats(struct net_device *dev,
923 struct mlxsw_sp_port_xstats *xstats)
924 {
925 char ppcnt_pl[MLXSW_REG_PPCNT_LEN];
926 int err, i;
927
928 err = mlxsw_sp_port_get_stats_raw(dev, MLXSW_REG_PPCNT_EXT_CNT, 0,
929 ppcnt_pl);
930 if (!err)
931 xstats->ecn = mlxsw_reg_ppcnt_ecn_marked_get(ppcnt_pl);
932
933 for (i = 0; i < TC_MAX_QUEUE; i++) {
934 err = mlxsw_sp_port_get_stats_raw(dev,
935 MLXSW_REG_PPCNT_TC_CONG_CNT,
936 i, ppcnt_pl);
937 if (err)
938 goto tc_cnt;
939
940 xstats->wred_drop[i] =
941 mlxsw_reg_ppcnt_wred_discard_get(ppcnt_pl);
942 xstats->tc_ecn[i] = mlxsw_reg_ppcnt_ecn_marked_tc_get(ppcnt_pl);
943
944 tc_cnt:
945 err = mlxsw_sp_port_get_stats_raw(dev, MLXSW_REG_PPCNT_TC_CNT,
946 i, ppcnt_pl);
947 if (err)
948 continue;
949
950 xstats->backlog[i] =
951 mlxsw_reg_ppcnt_tc_transmit_queue_get(ppcnt_pl);
952 xstats->tail_drop[i] =
953 mlxsw_reg_ppcnt_tc_no_buffer_discard_uc_get(ppcnt_pl);
954 }
955
956 for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
957 err = mlxsw_sp_port_get_stats_raw(dev, MLXSW_REG_PPCNT_PRIO_CNT,
958 i, ppcnt_pl);
959 if (err)
960 continue;
961
962 xstats->tx_packets[i] = mlxsw_reg_ppcnt_tx_frames_get(ppcnt_pl);
963 xstats->tx_bytes[i] = mlxsw_reg_ppcnt_tx_octets_get(ppcnt_pl);
964 }
965 }
966
update_stats_cache(struct work_struct * work)967 static void update_stats_cache(struct work_struct *work)
968 {
969 struct mlxsw_sp_port *mlxsw_sp_port =
970 container_of(work, struct mlxsw_sp_port,
971 periodic_hw_stats.update_dw.work);
972
973 if (!netif_carrier_ok(mlxsw_sp_port->dev))
974 /* Note: mlxsw_sp_port_down_wipe_counters() clears the cache as
975 * necessary when port goes down.
976 */
977 goto out;
978
979 mlxsw_sp_port_get_hw_stats(mlxsw_sp_port->dev,
980 &mlxsw_sp_port->periodic_hw_stats.stats);
981 mlxsw_sp_port_get_hw_xstats(mlxsw_sp_port->dev,
982 &mlxsw_sp_port->periodic_hw_stats.xstats);
983
984 out:
985 mlxsw_core_schedule_dw(&mlxsw_sp_port->periodic_hw_stats.update_dw,
986 MLXSW_HW_STATS_UPDATE_TIME);
987 }
988
989 /* Return the stats from a cache that is updated periodically,
990 * as this function might get called in an atomic context.
991 */
992 static void
mlxsw_sp_port_get_stats64(struct net_device * dev,struct rtnl_link_stats64 * stats)993 mlxsw_sp_port_get_stats64(struct net_device *dev,
994 struct rtnl_link_stats64 *stats)
995 {
996 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
997
998 memcpy(stats, &mlxsw_sp_port->periodic_hw_stats.stats, sizeof(*stats));
999 }
1000
__mlxsw_sp_port_vlan_set(struct mlxsw_sp_port * mlxsw_sp_port,u16 vid_begin,u16 vid_end,bool is_member,bool untagged)1001 static int __mlxsw_sp_port_vlan_set(struct mlxsw_sp_port *mlxsw_sp_port,
1002 u16 vid_begin, u16 vid_end,
1003 bool is_member, bool untagged)
1004 {
1005 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1006 char *spvm_pl;
1007 int err;
1008
1009 spvm_pl = kmalloc(MLXSW_REG_SPVM_LEN, GFP_KERNEL);
1010 if (!spvm_pl)
1011 return -ENOMEM;
1012
1013 mlxsw_reg_spvm_pack(spvm_pl, mlxsw_sp_port->local_port, vid_begin,
1014 vid_end, is_member, untagged);
1015 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(spvm), spvm_pl);
1016 kfree(spvm_pl);
1017 return err;
1018 }
1019
mlxsw_sp_port_vlan_set(struct mlxsw_sp_port * mlxsw_sp_port,u16 vid_begin,u16 vid_end,bool is_member,bool untagged)1020 int mlxsw_sp_port_vlan_set(struct mlxsw_sp_port *mlxsw_sp_port, u16 vid_begin,
1021 u16 vid_end, bool is_member, bool untagged)
1022 {
1023 u16 vid, vid_e;
1024 int err;
1025
1026 for (vid = vid_begin; vid <= vid_end;
1027 vid += MLXSW_REG_SPVM_REC_MAX_COUNT) {
1028 vid_e = min((u16) (vid + MLXSW_REG_SPVM_REC_MAX_COUNT - 1),
1029 vid_end);
1030
1031 err = __mlxsw_sp_port_vlan_set(mlxsw_sp_port, vid, vid_e,
1032 is_member, untagged);
1033 if (err)
1034 return err;
1035 }
1036
1037 return 0;
1038 }
1039
mlxsw_sp_port_vlan_flush(struct mlxsw_sp_port * mlxsw_sp_port,bool flush_default)1040 static void mlxsw_sp_port_vlan_flush(struct mlxsw_sp_port *mlxsw_sp_port,
1041 bool flush_default)
1042 {
1043 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan, *tmp;
1044
1045 list_for_each_entry_safe(mlxsw_sp_port_vlan, tmp,
1046 &mlxsw_sp_port->vlans_list, list) {
1047 if (!flush_default &&
1048 mlxsw_sp_port_vlan->vid == MLXSW_SP_DEFAULT_VID)
1049 continue;
1050 mlxsw_sp_port_vlan_destroy(mlxsw_sp_port_vlan);
1051 }
1052 }
1053
1054 static void
mlxsw_sp_port_vlan_cleanup(struct mlxsw_sp_port_vlan * mlxsw_sp_port_vlan)1055 mlxsw_sp_port_vlan_cleanup(struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan)
1056 {
1057 if (mlxsw_sp_port_vlan->bridge_port)
1058 mlxsw_sp_port_vlan_bridge_leave(mlxsw_sp_port_vlan);
1059 else if (mlxsw_sp_port_vlan->fid)
1060 mlxsw_sp_port_vlan_router_leave(mlxsw_sp_port_vlan);
1061 }
1062
1063 struct mlxsw_sp_port_vlan *
mlxsw_sp_port_vlan_create(struct mlxsw_sp_port * mlxsw_sp_port,u16 vid)1064 mlxsw_sp_port_vlan_create(struct mlxsw_sp_port *mlxsw_sp_port, u16 vid)
1065 {
1066 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1067 bool untagged = vid == MLXSW_SP_DEFAULT_VID;
1068 int err;
1069
1070 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port, vid);
1071 if (mlxsw_sp_port_vlan)
1072 return ERR_PTR(-EEXIST);
1073
1074 err = mlxsw_sp_port_vlan_set(mlxsw_sp_port, vid, vid, true, untagged);
1075 if (err)
1076 return ERR_PTR(err);
1077
1078 mlxsw_sp_port_vlan = kzalloc(sizeof(*mlxsw_sp_port_vlan), GFP_KERNEL);
1079 if (!mlxsw_sp_port_vlan) {
1080 err = -ENOMEM;
1081 goto err_port_vlan_alloc;
1082 }
1083
1084 mlxsw_sp_port_vlan->mlxsw_sp_port = mlxsw_sp_port;
1085 mlxsw_sp_port_vlan->vid = vid;
1086 list_add(&mlxsw_sp_port_vlan->list, &mlxsw_sp_port->vlans_list);
1087
1088 return mlxsw_sp_port_vlan;
1089
1090 err_port_vlan_alloc:
1091 mlxsw_sp_port_vlan_set(mlxsw_sp_port, vid, vid, false, false);
1092 return ERR_PTR(err);
1093 }
1094
mlxsw_sp_port_vlan_destroy(struct mlxsw_sp_port_vlan * mlxsw_sp_port_vlan)1095 void mlxsw_sp_port_vlan_destroy(struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan)
1096 {
1097 struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp_port_vlan->mlxsw_sp_port;
1098 u16 vid = mlxsw_sp_port_vlan->vid;
1099
1100 mlxsw_sp_port_vlan_cleanup(mlxsw_sp_port_vlan);
1101 list_del(&mlxsw_sp_port_vlan->list);
1102 kfree(mlxsw_sp_port_vlan);
1103 mlxsw_sp_port_vlan_set(mlxsw_sp_port, vid, vid, false, false);
1104 }
1105
mlxsw_sp_port_add_vid(struct net_device * dev,__be16 __always_unused proto,u16 vid)1106 static int mlxsw_sp_port_add_vid(struct net_device *dev,
1107 __be16 __always_unused proto, u16 vid)
1108 {
1109 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1110
1111 /* VLAN 0 is added to HW filter when device goes up, but it is
1112 * reserved in our case, so simply return.
1113 */
1114 if (!vid)
1115 return 0;
1116
1117 return PTR_ERR_OR_ZERO(mlxsw_sp_port_vlan_create(mlxsw_sp_port, vid));
1118 }
1119
mlxsw_sp_port_kill_vid(struct net_device * dev,__be16 __always_unused proto,u16 vid)1120 int mlxsw_sp_port_kill_vid(struct net_device *dev,
1121 __be16 __always_unused proto, u16 vid)
1122 {
1123 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1124 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1125
1126 /* VLAN 0 is removed from HW filter when device goes down, but
1127 * it is reserved in our case, so simply return.
1128 */
1129 if (!vid)
1130 return 0;
1131
1132 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port, vid);
1133 if (!mlxsw_sp_port_vlan)
1134 return 0;
1135 mlxsw_sp_port_vlan_destroy(mlxsw_sp_port_vlan);
1136
1137 return 0;
1138 }
1139
mlxsw_sp_setup_tc_block(struct mlxsw_sp_port * mlxsw_sp_port,struct flow_block_offload * f)1140 static int mlxsw_sp_setup_tc_block(struct mlxsw_sp_port *mlxsw_sp_port,
1141 struct flow_block_offload *f)
1142 {
1143 switch (f->binder_type) {
1144 case FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS:
1145 return mlxsw_sp_setup_tc_block_clsact(mlxsw_sp_port, f, true);
1146 case FLOW_BLOCK_BINDER_TYPE_CLSACT_EGRESS:
1147 return mlxsw_sp_setup_tc_block_clsact(mlxsw_sp_port, f, false);
1148 case FLOW_BLOCK_BINDER_TYPE_RED_EARLY_DROP:
1149 return mlxsw_sp_setup_tc_block_qevent_early_drop(mlxsw_sp_port, f);
1150 case FLOW_BLOCK_BINDER_TYPE_RED_MARK:
1151 return mlxsw_sp_setup_tc_block_qevent_mark(mlxsw_sp_port, f);
1152 default:
1153 return -EOPNOTSUPP;
1154 }
1155 }
1156
mlxsw_sp_setup_tc(struct net_device * dev,enum tc_setup_type type,void * type_data)1157 static int mlxsw_sp_setup_tc(struct net_device *dev, enum tc_setup_type type,
1158 void *type_data)
1159 {
1160 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1161
1162 switch (type) {
1163 case TC_SETUP_BLOCK:
1164 return mlxsw_sp_setup_tc_block(mlxsw_sp_port, type_data);
1165 case TC_SETUP_QDISC_RED:
1166 return mlxsw_sp_setup_tc_red(mlxsw_sp_port, type_data);
1167 case TC_SETUP_QDISC_PRIO:
1168 return mlxsw_sp_setup_tc_prio(mlxsw_sp_port, type_data);
1169 case TC_SETUP_QDISC_ETS:
1170 return mlxsw_sp_setup_tc_ets(mlxsw_sp_port, type_data);
1171 case TC_SETUP_QDISC_TBF:
1172 return mlxsw_sp_setup_tc_tbf(mlxsw_sp_port, type_data);
1173 case TC_SETUP_QDISC_FIFO:
1174 return mlxsw_sp_setup_tc_fifo(mlxsw_sp_port, type_data);
1175 default:
1176 return -EOPNOTSUPP;
1177 }
1178 }
1179
mlxsw_sp_feature_hw_tc(struct net_device * dev,bool enable)1180 static int mlxsw_sp_feature_hw_tc(struct net_device *dev, bool enable)
1181 {
1182 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1183
1184 if (!enable) {
1185 if (mlxsw_sp_flow_block_rule_count(mlxsw_sp_port->ing_flow_block) ||
1186 mlxsw_sp_flow_block_rule_count(mlxsw_sp_port->eg_flow_block)) {
1187 netdev_err(dev, "Active offloaded tc filters, can't turn hw_tc_offload off\n");
1188 return -EINVAL;
1189 }
1190 mlxsw_sp_flow_block_disable_inc(mlxsw_sp_port->ing_flow_block);
1191 mlxsw_sp_flow_block_disable_inc(mlxsw_sp_port->eg_flow_block);
1192 } else {
1193 mlxsw_sp_flow_block_disable_dec(mlxsw_sp_port->ing_flow_block);
1194 mlxsw_sp_flow_block_disable_dec(mlxsw_sp_port->eg_flow_block);
1195 }
1196 return 0;
1197 }
1198
mlxsw_sp_feature_loopback(struct net_device * dev,bool enable)1199 static int mlxsw_sp_feature_loopback(struct net_device *dev, bool enable)
1200 {
1201 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1202 char pplr_pl[MLXSW_REG_PPLR_LEN];
1203 int err;
1204
1205 if (netif_running(dev))
1206 mlxsw_sp_port_admin_status_set(mlxsw_sp_port, false);
1207
1208 mlxsw_reg_pplr_pack(pplr_pl, mlxsw_sp_port->local_port, enable);
1209 err = mlxsw_reg_write(mlxsw_sp_port->mlxsw_sp->core, MLXSW_REG(pplr),
1210 pplr_pl);
1211
1212 if (netif_running(dev))
1213 mlxsw_sp_port_admin_status_set(mlxsw_sp_port, true);
1214
1215 return err;
1216 }
1217
1218 typedef int (*mlxsw_sp_feature_handler)(struct net_device *dev, bool enable);
1219
mlxsw_sp_handle_feature(struct net_device * dev,netdev_features_t wanted_features,netdev_features_t feature,mlxsw_sp_feature_handler feature_handler)1220 static int mlxsw_sp_handle_feature(struct net_device *dev,
1221 netdev_features_t wanted_features,
1222 netdev_features_t feature,
1223 mlxsw_sp_feature_handler feature_handler)
1224 {
1225 netdev_features_t changes = wanted_features ^ dev->features;
1226 bool enable = !!(wanted_features & feature);
1227 int err;
1228
1229 if (!(changes & feature))
1230 return 0;
1231
1232 err = feature_handler(dev, enable);
1233 if (err) {
1234 netdev_err(dev, "%s feature %pNF failed, err %d\n",
1235 enable ? "Enable" : "Disable", &feature, err);
1236 return err;
1237 }
1238
1239 if (enable)
1240 dev->features |= feature;
1241 else
1242 dev->features &= ~feature;
1243
1244 return 0;
1245 }
mlxsw_sp_set_features(struct net_device * dev,netdev_features_t features)1246 static int mlxsw_sp_set_features(struct net_device *dev,
1247 netdev_features_t features)
1248 {
1249 netdev_features_t oper_features = dev->features;
1250 int err = 0;
1251
1252 err |= mlxsw_sp_handle_feature(dev, features, NETIF_F_HW_TC,
1253 mlxsw_sp_feature_hw_tc);
1254 err |= mlxsw_sp_handle_feature(dev, features, NETIF_F_LOOPBACK,
1255 mlxsw_sp_feature_loopback);
1256
1257 if (err) {
1258 dev->features = oper_features;
1259 return -EINVAL;
1260 }
1261
1262 return 0;
1263 }
1264
mlxsw_sp_port_hwtstamp_set(struct mlxsw_sp_port * mlxsw_sp_port,struct ifreq * ifr)1265 static int mlxsw_sp_port_hwtstamp_set(struct mlxsw_sp_port *mlxsw_sp_port,
1266 struct ifreq *ifr)
1267 {
1268 struct hwtstamp_config config;
1269 int err;
1270
1271 if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
1272 return -EFAULT;
1273
1274 err = mlxsw_sp_port->mlxsw_sp->ptp_ops->hwtstamp_set(mlxsw_sp_port,
1275 &config);
1276 if (err)
1277 return err;
1278
1279 if (copy_to_user(ifr->ifr_data, &config, sizeof(config)))
1280 return -EFAULT;
1281
1282 return 0;
1283 }
1284
mlxsw_sp_port_hwtstamp_get(struct mlxsw_sp_port * mlxsw_sp_port,struct ifreq * ifr)1285 static int mlxsw_sp_port_hwtstamp_get(struct mlxsw_sp_port *mlxsw_sp_port,
1286 struct ifreq *ifr)
1287 {
1288 struct hwtstamp_config config;
1289 int err;
1290
1291 err = mlxsw_sp_port->mlxsw_sp->ptp_ops->hwtstamp_get(mlxsw_sp_port,
1292 &config);
1293 if (err)
1294 return err;
1295
1296 if (copy_to_user(ifr->ifr_data, &config, sizeof(config)))
1297 return -EFAULT;
1298
1299 return 0;
1300 }
1301
mlxsw_sp_port_ptp_clear(struct mlxsw_sp_port * mlxsw_sp_port)1302 static inline void mlxsw_sp_port_ptp_clear(struct mlxsw_sp_port *mlxsw_sp_port)
1303 {
1304 struct hwtstamp_config config = {0};
1305
1306 mlxsw_sp_port->mlxsw_sp->ptp_ops->hwtstamp_set(mlxsw_sp_port, &config);
1307 }
1308
1309 static int
mlxsw_sp_port_ioctl(struct net_device * dev,struct ifreq * ifr,int cmd)1310 mlxsw_sp_port_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1311 {
1312 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1313
1314 switch (cmd) {
1315 case SIOCSHWTSTAMP:
1316 return mlxsw_sp_port_hwtstamp_set(mlxsw_sp_port, ifr);
1317 case SIOCGHWTSTAMP:
1318 return mlxsw_sp_port_hwtstamp_get(mlxsw_sp_port, ifr);
1319 default:
1320 return -EOPNOTSUPP;
1321 }
1322 }
1323
1324 static const struct net_device_ops mlxsw_sp_port_netdev_ops = {
1325 .ndo_open = mlxsw_sp_port_open,
1326 .ndo_stop = mlxsw_sp_port_stop,
1327 .ndo_start_xmit = mlxsw_sp_port_xmit,
1328 .ndo_setup_tc = mlxsw_sp_setup_tc,
1329 .ndo_set_rx_mode = mlxsw_sp_set_rx_mode,
1330 .ndo_set_mac_address = mlxsw_sp_port_set_mac_address,
1331 .ndo_change_mtu = mlxsw_sp_port_change_mtu,
1332 .ndo_get_stats64 = mlxsw_sp_port_get_stats64,
1333 .ndo_has_offload_stats = mlxsw_sp_port_has_offload_stats,
1334 .ndo_get_offload_stats = mlxsw_sp_port_get_offload_stats,
1335 .ndo_vlan_rx_add_vid = mlxsw_sp_port_add_vid,
1336 .ndo_vlan_rx_kill_vid = mlxsw_sp_port_kill_vid,
1337 .ndo_set_features = mlxsw_sp_set_features,
1338 .ndo_eth_ioctl = mlxsw_sp_port_ioctl,
1339 };
1340
1341 static int
mlxsw_sp_port_speed_by_width_set(struct mlxsw_sp_port * mlxsw_sp_port)1342 mlxsw_sp_port_speed_by_width_set(struct mlxsw_sp_port *mlxsw_sp_port)
1343 {
1344 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1345 u32 eth_proto_cap, eth_proto_admin, eth_proto_oper;
1346 const struct mlxsw_sp_port_type_speed_ops *ops;
1347 char ptys_pl[MLXSW_REG_PTYS_LEN];
1348 u32 eth_proto_cap_masked;
1349 int err;
1350
1351 ops = mlxsw_sp->port_type_speed_ops;
1352
1353 /* Set advertised speeds to speeds supported by both the driver
1354 * and the device.
1355 */
1356 ops->reg_ptys_eth_pack(mlxsw_sp, ptys_pl, mlxsw_sp_port->local_port,
1357 0, false);
1358 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(ptys), ptys_pl);
1359 if (err)
1360 return err;
1361
1362 ops->reg_ptys_eth_unpack(mlxsw_sp, ptys_pl, ð_proto_cap,
1363 ð_proto_admin, ð_proto_oper);
1364 eth_proto_cap_masked = ops->ptys_proto_cap_masked_get(eth_proto_cap);
1365 ops->reg_ptys_eth_pack(mlxsw_sp, ptys_pl, mlxsw_sp_port->local_port,
1366 eth_proto_cap_masked,
1367 mlxsw_sp_port->link.autoneg);
1368 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ptys), ptys_pl);
1369 }
1370
mlxsw_sp_port_speed_get(struct mlxsw_sp_port * mlxsw_sp_port,u32 * speed)1371 int mlxsw_sp_port_speed_get(struct mlxsw_sp_port *mlxsw_sp_port, u32 *speed)
1372 {
1373 const struct mlxsw_sp_port_type_speed_ops *port_type_speed_ops;
1374 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1375 char ptys_pl[MLXSW_REG_PTYS_LEN];
1376 u32 eth_proto_oper;
1377 int err;
1378
1379 port_type_speed_ops = mlxsw_sp->port_type_speed_ops;
1380 port_type_speed_ops->reg_ptys_eth_pack(mlxsw_sp, ptys_pl,
1381 mlxsw_sp_port->local_port, 0,
1382 false);
1383 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(ptys), ptys_pl);
1384 if (err)
1385 return err;
1386 port_type_speed_ops->reg_ptys_eth_unpack(mlxsw_sp, ptys_pl, NULL, NULL,
1387 ð_proto_oper);
1388 *speed = port_type_speed_ops->from_ptys_speed(mlxsw_sp, eth_proto_oper);
1389 return 0;
1390 }
1391
mlxsw_sp_port_ets_set(struct mlxsw_sp_port * mlxsw_sp_port,enum mlxsw_reg_qeec_hr hr,u8 index,u8 next_index,bool dwrr,u8 dwrr_weight)1392 int mlxsw_sp_port_ets_set(struct mlxsw_sp_port *mlxsw_sp_port,
1393 enum mlxsw_reg_qeec_hr hr, u8 index, u8 next_index,
1394 bool dwrr, u8 dwrr_weight)
1395 {
1396 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1397 char qeec_pl[MLXSW_REG_QEEC_LEN];
1398
1399 mlxsw_reg_qeec_pack(qeec_pl, mlxsw_sp_port->local_port, hr, index,
1400 next_index);
1401 mlxsw_reg_qeec_de_set(qeec_pl, true);
1402 mlxsw_reg_qeec_dwrr_set(qeec_pl, dwrr);
1403 mlxsw_reg_qeec_dwrr_weight_set(qeec_pl, dwrr_weight);
1404 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(qeec), qeec_pl);
1405 }
1406
mlxsw_sp_port_ets_maxrate_set(struct mlxsw_sp_port * mlxsw_sp_port,enum mlxsw_reg_qeec_hr hr,u8 index,u8 next_index,u32 maxrate,u8 burst_size)1407 int mlxsw_sp_port_ets_maxrate_set(struct mlxsw_sp_port *mlxsw_sp_port,
1408 enum mlxsw_reg_qeec_hr hr, u8 index,
1409 u8 next_index, u32 maxrate, u8 burst_size)
1410 {
1411 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1412 char qeec_pl[MLXSW_REG_QEEC_LEN];
1413
1414 mlxsw_reg_qeec_pack(qeec_pl, mlxsw_sp_port->local_port, hr, index,
1415 next_index);
1416 mlxsw_reg_qeec_mase_set(qeec_pl, true);
1417 mlxsw_reg_qeec_max_shaper_rate_set(qeec_pl, maxrate);
1418 mlxsw_reg_qeec_max_shaper_bs_set(qeec_pl, burst_size);
1419 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(qeec), qeec_pl);
1420 }
1421
mlxsw_sp_port_min_bw_set(struct mlxsw_sp_port * mlxsw_sp_port,enum mlxsw_reg_qeec_hr hr,u8 index,u8 next_index,u32 minrate)1422 static int mlxsw_sp_port_min_bw_set(struct mlxsw_sp_port *mlxsw_sp_port,
1423 enum mlxsw_reg_qeec_hr hr, u8 index,
1424 u8 next_index, u32 minrate)
1425 {
1426 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1427 char qeec_pl[MLXSW_REG_QEEC_LEN];
1428
1429 mlxsw_reg_qeec_pack(qeec_pl, mlxsw_sp_port->local_port, hr, index,
1430 next_index);
1431 mlxsw_reg_qeec_mise_set(qeec_pl, true);
1432 mlxsw_reg_qeec_min_shaper_rate_set(qeec_pl, minrate);
1433
1434 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(qeec), qeec_pl);
1435 }
1436
mlxsw_sp_port_prio_tc_set(struct mlxsw_sp_port * mlxsw_sp_port,u8 switch_prio,u8 tclass)1437 int mlxsw_sp_port_prio_tc_set(struct mlxsw_sp_port *mlxsw_sp_port,
1438 u8 switch_prio, u8 tclass)
1439 {
1440 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1441 char qtct_pl[MLXSW_REG_QTCT_LEN];
1442
1443 mlxsw_reg_qtct_pack(qtct_pl, mlxsw_sp_port->local_port, switch_prio,
1444 tclass);
1445 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(qtct), qtct_pl);
1446 }
1447
mlxsw_sp_port_ets_init(struct mlxsw_sp_port * mlxsw_sp_port)1448 static int mlxsw_sp_port_ets_init(struct mlxsw_sp_port *mlxsw_sp_port)
1449 {
1450 int err, i;
1451
1452 /* Setup the elements hierarcy, so that each TC is linked to
1453 * one subgroup, which are all member in the same group.
1454 */
1455 err = mlxsw_sp_port_ets_set(mlxsw_sp_port,
1456 MLXSW_REG_QEEC_HR_GROUP, 0, 0, false, 0);
1457 if (err)
1458 return err;
1459 for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
1460 err = mlxsw_sp_port_ets_set(mlxsw_sp_port,
1461 MLXSW_REG_QEEC_HR_SUBGROUP, i,
1462 0, false, 0);
1463 if (err)
1464 return err;
1465 }
1466 for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
1467 err = mlxsw_sp_port_ets_set(mlxsw_sp_port,
1468 MLXSW_REG_QEEC_HR_TC, i, i,
1469 false, 0);
1470 if (err)
1471 return err;
1472
1473 err = mlxsw_sp_port_ets_set(mlxsw_sp_port,
1474 MLXSW_REG_QEEC_HR_TC,
1475 i + 8, i,
1476 true, 100);
1477 if (err)
1478 return err;
1479 }
1480
1481 /* Make sure the max shaper is disabled in all hierarchies that support
1482 * it. Note that this disables ptps (PTP shaper), but that is intended
1483 * for the initial configuration.
1484 */
1485 err = mlxsw_sp_port_ets_maxrate_set(mlxsw_sp_port,
1486 MLXSW_REG_QEEC_HR_PORT, 0, 0,
1487 MLXSW_REG_QEEC_MAS_DIS, 0);
1488 if (err)
1489 return err;
1490 for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
1491 err = mlxsw_sp_port_ets_maxrate_set(mlxsw_sp_port,
1492 MLXSW_REG_QEEC_HR_SUBGROUP,
1493 i, 0,
1494 MLXSW_REG_QEEC_MAS_DIS, 0);
1495 if (err)
1496 return err;
1497 }
1498 for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
1499 err = mlxsw_sp_port_ets_maxrate_set(mlxsw_sp_port,
1500 MLXSW_REG_QEEC_HR_TC,
1501 i, i,
1502 MLXSW_REG_QEEC_MAS_DIS, 0);
1503 if (err)
1504 return err;
1505
1506 err = mlxsw_sp_port_ets_maxrate_set(mlxsw_sp_port,
1507 MLXSW_REG_QEEC_HR_TC,
1508 i + 8, i,
1509 MLXSW_REG_QEEC_MAS_DIS, 0);
1510 if (err)
1511 return err;
1512 }
1513
1514 /* Configure the min shaper for multicast TCs. */
1515 for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
1516 err = mlxsw_sp_port_min_bw_set(mlxsw_sp_port,
1517 MLXSW_REG_QEEC_HR_TC,
1518 i + 8, i,
1519 MLXSW_REG_QEEC_MIS_MIN);
1520 if (err)
1521 return err;
1522 }
1523
1524 /* Map all priorities to traffic class 0. */
1525 for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
1526 err = mlxsw_sp_port_prio_tc_set(mlxsw_sp_port, i, 0);
1527 if (err)
1528 return err;
1529 }
1530
1531 return 0;
1532 }
1533
mlxsw_sp_port_tc_mc_mode_set(struct mlxsw_sp_port * mlxsw_sp_port,bool enable)1534 static int mlxsw_sp_port_tc_mc_mode_set(struct mlxsw_sp_port *mlxsw_sp_port,
1535 bool enable)
1536 {
1537 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1538 char qtctm_pl[MLXSW_REG_QTCTM_LEN];
1539
1540 mlxsw_reg_qtctm_pack(qtctm_pl, mlxsw_sp_port->local_port, enable);
1541 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(qtctm), qtctm_pl);
1542 }
1543
mlxsw_sp_port_overheat_init_val_set(struct mlxsw_sp_port * mlxsw_sp_port)1544 static int mlxsw_sp_port_overheat_init_val_set(struct mlxsw_sp_port *mlxsw_sp_port)
1545 {
1546 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1547 u8 slot_index = mlxsw_sp_port->mapping.slot_index;
1548 u8 module = mlxsw_sp_port->mapping.module;
1549 u64 overheat_counter;
1550 int err;
1551
1552 err = mlxsw_env_module_overheat_counter_get(mlxsw_sp->core, slot_index,
1553 module, &overheat_counter);
1554 if (err)
1555 return err;
1556
1557 mlxsw_sp_port->module_overheat_initial_val = overheat_counter;
1558 return 0;
1559 }
1560
1561 int
mlxsw_sp_port_vlan_classification_set(struct mlxsw_sp_port * mlxsw_sp_port,bool is_8021ad_tagged,bool is_8021q_tagged)1562 mlxsw_sp_port_vlan_classification_set(struct mlxsw_sp_port *mlxsw_sp_port,
1563 bool is_8021ad_tagged,
1564 bool is_8021q_tagged)
1565 {
1566 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1567 char spvc_pl[MLXSW_REG_SPVC_LEN];
1568
1569 mlxsw_reg_spvc_pack(spvc_pl, mlxsw_sp_port->local_port,
1570 is_8021ad_tagged, is_8021q_tagged);
1571 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(spvc), spvc_pl);
1572 }
1573
mlxsw_sp_port_label_info_get(struct mlxsw_sp * mlxsw_sp,u16 local_port,u8 * port_number,u8 * split_port_subnumber,u8 * slot_index)1574 static int mlxsw_sp_port_label_info_get(struct mlxsw_sp *mlxsw_sp,
1575 u16 local_port, u8 *port_number,
1576 u8 *split_port_subnumber,
1577 u8 *slot_index)
1578 {
1579 char pllp_pl[MLXSW_REG_PLLP_LEN];
1580 int err;
1581
1582 mlxsw_reg_pllp_pack(pllp_pl, local_port);
1583 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(pllp), pllp_pl);
1584 if (err)
1585 return err;
1586 mlxsw_reg_pllp_unpack(pllp_pl, port_number,
1587 split_port_subnumber, slot_index);
1588 return 0;
1589 }
1590
mlxsw_sp_port_create(struct mlxsw_sp * mlxsw_sp,u16 local_port,bool split,struct mlxsw_sp_port_mapping * port_mapping)1591 static int mlxsw_sp_port_create(struct mlxsw_sp *mlxsw_sp, u16 local_port,
1592 bool split,
1593 struct mlxsw_sp_port_mapping *port_mapping)
1594 {
1595 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
1596 struct mlxsw_sp_port *mlxsw_sp_port;
1597 u32 lanes = port_mapping->width;
1598 u8 split_port_subnumber;
1599 struct net_device *dev;
1600 u8 port_number;
1601 u8 slot_index;
1602 bool splittable;
1603 int err;
1604
1605 err = mlxsw_sp_port_module_map(mlxsw_sp, local_port, port_mapping);
1606 if (err) {
1607 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to map module\n",
1608 local_port);
1609 return err;
1610 }
1611
1612 err = mlxsw_sp_port_swid_set(mlxsw_sp, local_port, 0);
1613 if (err) {
1614 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to set SWID\n",
1615 local_port);
1616 goto err_port_swid_set;
1617 }
1618
1619 err = mlxsw_sp_port_label_info_get(mlxsw_sp, local_port, &port_number,
1620 &split_port_subnumber, &slot_index);
1621 if (err) {
1622 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to get port label information\n",
1623 local_port);
1624 goto err_port_label_info_get;
1625 }
1626
1627 splittable = lanes > 1 && !split;
1628 err = mlxsw_core_port_init(mlxsw_sp->core, local_port, slot_index,
1629 port_number, split, split_port_subnumber,
1630 splittable, lanes, mlxsw_sp->base_mac,
1631 sizeof(mlxsw_sp->base_mac));
1632 if (err) {
1633 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to init core port\n",
1634 local_port);
1635 goto err_core_port_init;
1636 }
1637
1638 dev = alloc_etherdev(sizeof(struct mlxsw_sp_port));
1639 if (!dev) {
1640 err = -ENOMEM;
1641 goto err_alloc_etherdev;
1642 }
1643 SET_NETDEV_DEV(dev, mlxsw_sp->bus_info->dev);
1644 dev_net_set(dev, mlxsw_sp_net(mlxsw_sp));
1645 mlxsw_sp_port = netdev_priv(dev);
1646 mlxsw_core_port_netdev_link(mlxsw_sp->core, local_port,
1647 mlxsw_sp_port, dev);
1648 mlxsw_sp_port->dev = dev;
1649 mlxsw_sp_port->mlxsw_sp = mlxsw_sp;
1650 mlxsw_sp_port->local_port = local_port;
1651 mlxsw_sp_port->pvid = MLXSW_SP_DEFAULT_VID;
1652 mlxsw_sp_port->split = split;
1653 mlxsw_sp_port->mapping = *port_mapping;
1654 mlxsw_sp_port->link.autoneg = 1;
1655 INIT_LIST_HEAD(&mlxsw_sp_port->vlans_list);
1656
1657 mlxsw_sp_port->pcpu_stats =
1658 netdev_alloc_pcpu_stats(struct mlxsw_sp_port_pcpu_stats);
1659 if (!mlxsw_sp_port->pcpu_stats) {
1660 err = -ENOMEM;
1661 goto err_alloc_stats;
1662 }
1663
1664 INIT_DELAYED_WORK(&mlxsw_sp_port->periodic_hw_stats.update_dw,
1665 &update_stats_cache);
1666
1667 dev->netdev_ops = &mlxsw_sp_port_netdev_ops;
1668 dev->ethtool_ops = &mlxsw_sp_port_ethtool_ops;
1669
1670 err = mlxsw_sp_port_dev_addr_init(mlxsw_sp_port);
1671 if (err) {
1672 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Unable to init port mac address\n",
1673 mlxsw_sp_port->local_port);
1674 goto err_dev_addr_init;
1675 }
1676
1677 netif_carrier_off(dev);
1678
1679 dev->features |= NETIF_F_SG | NETIF_F_HW_VLAN_CTAG_FILTER |
1680 NETIF_F_HW_TC;
1681 dev->hw_features |= NETIF_F_HW_TC | NETIF_F_LOOPBACK;
1682 dev->lltx = true;
1683 dev->netns_local = true;
1684
1685 dev->min_mtu = ETH_MIN_MTU;
1686 dev->max_mtu = MLXSW_PORT_MAX_MTU - MLXSW_PORT_ETH_FRAME_HDR;
1687
1688 /* Each packet needs to have a Tx header (metadata) on top all other
1689 * headers.
1690 */
1691 dev->needed_headroom = MLXSW_TXHDR_LEN;
1692
1693 err = mlxsw_sp_port_system_port_mapping_set(mlxsw_sp_port);
1694 if (err) {
1695 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to set system port mapping\n",
1696 mlxsw_sp_port->local_port);
1697 goto err_port_system_port_mapping_set;
1698 }
1699
1700 err = mlxsw_sp_port_speed_by_width_set(mlxsw_sp_port);
1701 if (err) {
1702 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to enable speeds\n",
1703 mlxsw_sp_port->local_port);
1704 goto err_port_speed_by_width_set;
1705 }
1706
1707 err = mlxsw_sp->port_type_speed_ops->ptys_max_speed(mlxsw_sp_port,
1708 &mlxsw_sp_port->max_speed);
1709 if (err) {
1710 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to get maximum speed\n",
1711 mlxsw_sp_port->local_port);
1712 goto err_max_speed_get;
1713 }
1714
1715 err = mlxsw_sp_port_mtu_set(mlxsw_sp_port, ETH_DATA_LEN);
1716 if (err) {
1717 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to set MTU\n",
1718 mlxsw_sp_port->local_port);
1719 goto err_port_mtu_set;
1720 }
1721
1722 err = mlxsw_sp_port_admin_status_set(mlxsw_sp_port, false);
1723 if (err)
1724 goto err_port_admin_status_set;
1725
1726 err = mlxsw_sp_port_buffers_init(mlxsw_sp_port);
1727 if (err) {
1728 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to initialize buffers\n",
1729 mlxsw_sp_port->local_port);
1730 goto err_port_buffers_init;
1731 }
1732
1733 err = mlxsw_sp_port_ets_init(mlxsw_sp_port);
1734 if (err) {
1735 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to initialize ETS\n",
1736 mlxsw_sp_port->local_port);
1737 goto err_port_ets_init;
1738 }
1739
1740 err = mlxsw_sp_port_tc_mc_mode_set(mlxsw_sp_port, true);
1741 if (err) {
1742 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to initialize TC MC mode\n",
1743 mlxsw_sp_port->local_port);
1744 goto err_port_tc_mc_mode;
1745 }
1746
1747 /* ETS and buffers must be initialized before DCB. */
1748 err = mlxsw_sp_port_dcb_init(mlxsw_sp_port);
1749 if (err) {
1750 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to initialize DCB\n",
1751 mlxsw_sp_port->local_port);
1752 goto err_port_dcb_init;
1753 }
1754
1755 err = mlxsw_sp_port_fids_init(mlxsw_sp_port);
1756 if (err) {
1757 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to initialize FIDs\n",
1758 mlxsw_sp_port->local_port);
1759 goto err_port_fids_init;
1760 }
1761
1762 err = mlxsw_sp_tc_qdisc_init(mlxsw_sp_port);
1763 if (err) {
1764 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to initialize TC qdiscs\n",
1765 mlxsw_sp_port->local_port);
1766 goto err_port_qdiscs_init;
1767 }
1768
1769 err = mlxsw_sp_port_vlan_set(mlxsw_sp_port, 0, VLAN_N_VID - 1, false,
1770 false);
1771 if (err) {
1772 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to clear VLAN filter\n",
1773 mlxsw_sp_port->local_port);
1774 goto err_port_vlan_clear;
1775 }
1776
1777 err = mlxsw_sp_port_nve_init(mlxsw_sp_port);
1778 if (err) {
1779 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to initialize NVE\n",
1780 mlxsw_sp_port->local_port);
1781 goto err_port_nve_init;
1782 }
1783
1784 err = mlxsw_sp_port_pvid_set(mlxsw_sp_port, MLXSW_SP_DEFAULT_VID,
1785 ETH_P_8021Q);
1786 if (err) {
1787 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to set PVID\n",
1788 mlxsw_sp_port->local_port);
1789 goto err_port_pvid_set;
1790 }
1791
1792 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_create(mlxsw_sp_port,
1793 MLXSW_SP_DEFAULT_VID);
1794 if (IS_ERR(mlxsw_sp_port_vlan)) {
1795 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to create VID 1\n",
1796 mlxsw_sp_port->local_port);
1797 err = PTR_ERR(mlxsw_sp_port_vlan);
1798 goto err_port_vlan_create;
1799 }
1800 mlxsw_sp_port->default_vlan = mlxsw_sp_port_vlan;
1801
1802 /* Set SPVC.et0=true and SPVC.et1=false to make the local port to treat
1803 * only packets with 802.1q header as tagged packets.
1804 */
1805 err = mlxsw_sp_port_vlan_classification_set(mlxsw_sp_port, false, true);
1806 if (err) {
1807 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to set default VLAN classification\n",
1808 local_port);
1809 goto err_port_vlan_classification_set;
1810 }
1811
1812 INIT_DELAYED_WORK(&mlxsw_sp_port->ptp.shaper_dw,
1813 mlxsw_sp->ptp_ops->shaper_work);
1814
1815 mlxsw_sp->ports[local_port] = mlxsw_sp_port;
1816
1817 err = mlxsw_sp_port_overheat_init_val_set(mlxsw_sp_port);
1818 if (err) {
1819 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to set overheat initial value\n",
1820 mlxsw_sp_port->local_port);
1821 goto err_port_overheat_init_val_set;
1822 }
1823
1824 err = register_netdev(dev);
1825 if (err) {
1826 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to register netdev\n",
1827 mlxsw_sp_port->local_port);
1828 goto err_register_netdev;
1829 }
1830
1831 mlxsw_core_schedule_dw(&mlxsw_sp_port->periodic_hw_stats.update_dw, 0);
1832 return 0;
1833
1834 err_register_netdev:
1835 err_port_overheat_init_val_set:
1836 mlxsw_sp_port_vlan_classification_set(mlxsw_sp_port, true, true);
1837 err_port_vlan_classification_set:
1838 mlxsw_sp->ports[local_port] = NULL;
1839 mlxsw_sp_port_vlan_destroy(mlxsw_sp_port_vlan);
1840 err_port_vlan_create:
1841 err_port_pvid_set:
1842 mlxsw_sp_port_nve_fini(mlxsw_sp_port);
1843 err_port_nve_init:
1844 err_port_vlan_clear:
1845 mlxsw_sp_tc_qdisc_fini(mlxsw_sp_port);
1846 err_port_qdiscs_init:
1847 mlxsw_sp_port_fids_fini(mlxsw_sp_port);
1848 err_port_fids_init:
1849 mlxsw_sp_port_dcb_fini(mlxsw_sp_port);
1850 err_port_dcb_init:
1851 mlxsw_sp_port_tc_mc_mode_set(mlxsw_sp_port, false);
1852 err_port_tc_mc_mode:
1853 err_port_ets_init:
1854 mlxsw_sp_port_buffers_fini(mlxsw_sp_port);
1855 err_port_buffers_init:
1856 err_port_admin_status_set:
1857 err_port_mtu_set:
1858 err_max_speed_get:
1859 err_port_speed_by_width_set:
1860 err_port_system_port_mapping_set:
1861 err_dev_addr_init:
1862 free_percpu(mlxsw_sp_port->pcpu_stats);
1863 err_alloc_stats:
1864 free_netdev(dev);
1865 err_alloc_etherdev:
1866 mlxsw_core_port_fini(mlxsw_sp->core, local_port);
1867 err_core_port_init:
1868 err_port_label_info_get:
1869 mlxsw_sp_port_swid_set(mlxsw_sp, local_port,
1870 MLXSW_PORT_SWID_DISABLED_PORT);
1871 err_port_swid_set:
1872 mlxsw_sp_port_module_unmap(mlxsw_sp, local_port,
1873 port_mapping->slot_index,
1874 port_mapping->module);
1875 return err;
1876 }
1877
mlxsw_sp_port_remove(struct mlxsw_sp * mlxsw_sp,u16 local_port)1878 static void mlxsw_sp_port_remove(struct mlxsw_sp *mlxsw_sp, u16 local_port)
1879 {
1880 struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp->ports[local_port];
1881 u8 slot_index = mlxsw_sp_port->mapping.slot_index;
1882 u8 module = mlxsw_sp_port->mapping.module;
1883
1884 cancel_delayed_work_sync(&mlxsw_sp_port->periodic_hw_stats.update_dw);
1885 cancel_delayed_work_sync(&mlxsw_sp_port->ptp.shaper_dw);
1886 unregister_netdev(mlxsw_sp_port->dev); /* This calls ndo_stop */
1887 mlxsw_sp_port_ptp_clear(mlxsw_sp_port);
1888 mlxsw_sp_port_vlan_classification_set(mlxsw_sp_port, true, true);
1889 mlxsw_sp->ports[local_port] = NULL;
1890 mlxsw_sp_port_vlan_flush(mlxsw_sp_port, true);
1891 mlxsw_sp_port_nve_fini(mlxsw_sp_port);
1892 mlxsw_sp_tc_qdisc_fini(mlxsw_sp_port);
1893 mlxsw_sp_port_fids_fini(mlxsw_sp_port);
1894 mlxsw_sp_port_dcb_fini(mlxsw_sp_port);
1895 mlxsw_sp_port_tc_mc_mode_set(mlxsw_sp_port, false);
1896 mlxsw_sp_port_buffers_fini(mlxsw_sp_port);
1897 free_percpu(mlxsw_sp_port->pcpu_stats);
1898 WARN_ON_ONCE(!list_empty(&mlxsw_sp_port->vlans_list));
1899 free_netdev(mlxsw_sp_port->dev);
1900 mlxsw_core_port_fini(mlxsw_sp->core, local_port);
1901 mlxsw_sp_port_swid_set(mlxsw_sp, local_port,
1902 MLXSW_PORT_SWID_DISABLED_PORT);
1903 mlxsw_sp_port_module_unmap(mlxsw_sp, local_port, slot_index, module);
1904 }
1905
mlxsw_sp_cpu_port_create(struct mlxsw_sp * mlxsw_sp)1906 static int mlxsw_sp_cpu_port_create(struct mlxsw_sp *mlxsw_sp)
1907 {
1908 struct mlxsw_sp_port *mlxsw_sp_port;
1909 int err;
1910
1911 mlxsw_sp_port = kzalloc(sizeof(*mlxsw_sp_port), GFP_KERNEL);
1912 if (!mlxsw_sp_port)
1913 return -ENOMEM;
1914
1915 mlxsw_sp_port->mlxsw_sp = mlxsw_sp;
1916 mlxsw_sp_port->local_port = MLXSW_PORT_CPU_PORT;
1917
1918 err = mlxsw_core_cpu_port_init(mlxsw_sp->core,
1919 mlxsw_sp_port,
1920 mlxsw_sp->base_mac,
1921 sizeof(mlxsw_sp->base_mac));
1922 if (err) {
1923 dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize core CPU port\n");
1924 goto err_core_cpu_port_init;
1925 }
1926
1927 mlxsw_sp->ports[MLXSW_PORT_CPU_PORT] = mlxsw_sp_port;
1928 return 0;
1929
1930 err_core_cpu_port_init:
1931 kfree(mlxsw_sp_port);
1932 return err;
1933 }
1934
mlxsw_sp_cpu_port_remove(struct mlxsw_sp * mlxsw_sp)1935 static void mlxsw_sp_cpu_port_remove(struct mlxsw_sp *mlxsw_sp)
1936 {
1937 struct mlxsw_sp_port *mlxsw_sp_port =
1938 mlxsw_sp->ports[MLXSW_PORT_CPU_PORT];
1939
1940 mlxsw_core_cpu_port_fini(mlxsw_sp->core);
1941 mlxsw_sp->ports[MLXSW_PORT_CPU_PORT] = NULL;
1942 kfree(mlxsw_sp_port);
1943 }
1944
mlxsw_sp_local_port_valid(u16 local_port)1945 static bool mlxsw_sp_local_port_valid(u16 local_port)
1946 {
1947 return local_port != MLXSW_PORT_CPU_PORT;
1948 }
1949
mlxsw_sp_port_created(struct mlxsw_sp * mlxsw_sp,u16 local_port)1950 static bool mlxsw_sp_port_created(struct mlxsw_sp *mlxsw_sp, u16 local_port)
1951 {
1952 if (!mlxsw_sp_local_port_valid(local_port))
1953 return false;
1954 return mlxsw_sp->ports[local_port] != NULL;
1955 }
1956
mlxsw_sp_port_mapping_event_set(struct mlxsw_sp * mlxsw_sp,u16 local_port,bool enable)1957 static int mlxsw_sp_port_mapping_event_set(struct mlxsw_sp *mlxsw_sp,
1958 u16 local_port, bool enable)
1959 {
1960 char pmecr_pl[MLXSW_REG_PMECR_LEN];
1961
1962 mlxsw_reg_pmecr_pack(pmecr_pl, local_port,
1963 enable ? MLXSW_REG_PMECR_E_GENERATE_EVENT :
1964 MLXSW_REG_PMECR_E_DO_NOT_GENERATE_EVENT);
1965 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(pmecr), pmecr_pl);
1966 }
1967
1968 struct mlxsw_sp_port_mapping_event {
1969 struct list_head list;
1970 char pmlp_pl[MLXSW_REG_PMLP_LEN];
1971 };
1972
mlxsw_sp_port_mapping_events_work(struct work_struct * work)1973 static void mlxsw_sp_port_mapping_events_work(struct work_struct *work)
1974 {
1975 struct mlxsw_sp_port_mapping_event *event, *next_event;
1976 struct mlxsw_sp_port_mapping_events *events;
1977 struct mlxsw_sp_port_mapping port_mapping;
1978 struct mlxsw_sp *mlxsw_sp;
1979 struct devlink *devlink;
1980 LIST_HEAD(event_queue);
1981 u16 local_port;
1982 int err;
1983
1984 events = container_of(work, struct mlxsw_sp_port_mapping_events, work);
1985 mlxsw_sp = container_of(events, struct mlxsw_sp, port_mapping_events);
1986 devlink = priv_to_devlink(mlxsw_sp->core);
1987
1988 spin_lock_bh(&events->queue_lock);
1989 list_splice_init(&events->queue, &event_queue);
1990 spin_unlock_bh(&events->queue_lock);
1991
1992 list_for_each_entry_safe(event, next_event, &event_queue, list) {
1993 local_port = mlxsw_reg_pmlp_local_port_get(event->pmlp_pl);
1994 err = mlxsw_sp_port_module_info_parse(mlxsw_sp, local_port,
1995 event->pmlp_pl, &port_mapping);
1996 if (err)
1997 goto out;
1998
1999 if (WARN_ON_ONCE(!port_mapping.width))
2000 goto out;
2001
2002 devl_lock(devlink);
2003
2004 if (!mlxsw_sp_port_created(mlxsw_sp, local_port))
2005 mlxsw_sp_port_create(mlxsw_sp, local_port,
2006 false, &port_mapping);
2007 else
2008 WARN_ON_ONCE(1);
2009
2010 devl_unlock(devlink);
2011
2012 mlxsw_sp->port_mapping[local_port] = port_mapping;
2013
2014 out:
2015 kfree(event);
2016 }
2017 }
2018
2019 static void
mlxsw_sp_port_mapping_listener_func(const struct mlxsw_reg_info * reg,char * pmlp_pl,void * priv)2020 mlxsw_sp_port_mapping_listener_func(const struct mlxsw_reg_info *reg,
2021 char *pmlp_pl, void *priv)
2022 {
2023 struct mlxsw_sp_port_mapping_events *events;
2024 struct mlxsw_sp_port_mapping_event *event;
2025 struct mlxsw_sp *mlxsw_sp = priv;
2026 u16 local_port;
2027
2028 local_port = mlxsw_reg_pmlp_local_port_get(pmlp_pl);
2029 if (WARN_ON_ONCE(!mlxsw_sp_local_port_is_valid(mlxsw_sp, local_port)))
2030 return;
2031
2032 events = &mlxsw_sp->port_mapping_events;
2033 event = kmalloc(sizeof(*event), GFP_ATOMIC);
2034 if (!event)
2035 return;
2036 memcpy(event->pmlp_pl, pmlp_pl, sizeof(event->pmlp_pl));
2037 spin_lock(&events->queue_lock);
2038 list_add_tail(&event->list, &events->queue);
2039 spin_unlock(&events->queue_lock);
2040 mlxsw_core_schedule_work(&events->work);
2041 }
2042
2043 static void
__mlxsw_sp_port_mapping_events_cancel(struct mlxsw_sp * mlxsw_sp)2044 __mlxsw_sp_port_mapping_events_cancel(struct mlxsw_sp *mlxsw_sp)
2045 {
2046 struct mlxsw_sp_port_mapping_event *event, *next_event;
2047 struct mlxsw_sp_port_mapping_events *events;
2048
2049 events = &mlxsw_sp->port_mapping_events;
2050
2051 /* Caller needs to make sure that no new event is going to appear. */
2052 cancel_work_sync(&events->work);
2053 list_for_each_entry_safe(event, next_event, &events->queue, list) {
2054 list_del(&event->list);
2055 kfree(event);
2056 }
2057 }
2058
mlxsw_sp_ports_remove(struct mlxsw_sp * mlxsw_sp)2059 static void mlxsw_sp_ports_remove(struct mlxsw_sp *mlxsw_sp)
2060 {
2061 unsigned int max_ports = mlxsw_core_max_ports(mlxsw_sp->core);
2062 int i;
2063
2064 for (i = 1; i < max_ports; i++)
2065 mlxsw_sp_port_mapping_event_set(mlxsw_sp, i, false);
2066 /* Make sure all scheduled events are processed */
2067 __mlxsw_sp_port_mapping_events_cancel(mlxsw_sp);
2068
2069 for (i = 1; i < max_ports; i++)
2070 if (mlxsw_sp_port_created(mlxsw_sp, i))
2071 mlxsw_sp_port_remove(mlxsw_sp, i);
2072 mlxsw_sp_cpu_port_remove(mlxsw_sp);
2073 kfree(mlxsw_sp->ports);
2074 mlxsw_sp->ports = NULL;
2075 }
2076
2077 static void
mlxsw_sp_ports_remove_selected(struct mlxsw_core * mlxsw_core,bool (* selector)(void * priv,u16 local_port),void * priv)2078 mlxsw_sp_ports_remove_selected(struct mlxsw_core *mlxsw_core,
2079 bool (*selector)(void *priv, u16 local_port),
2080 void *priv)
2081 {
2082 struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
2083 unsigned int max_ports = mlxsw_core_max_ports(mlxsw_core);
2084 int i;
2085
2086 for (i = 1; i < max_ports; i++)
2087 if (mlxsw_sp_port_created(mlxsw_sp, i) && selector(priv, i))
2088 mlxsw_sp_port_remove(mlxsw_sp, i);
2089 }
2090
mlxsw_sp_ports_create(struct mlxsw_sp * mlxsw_sp)2091 static int mlxsw_sp_ports_create(struct mlxsw_sp *mlxsw_sp)
2092 {
2093 unsigned int max_ports = mlxsw_core_max_ports(mlxsw_sp->core);
2094 struct mlxsw_sp_port_mapping_events *events;
2095 struct mlxsw_sp_port_mapping *port_mapping;
2096 size_t alloc_size;
2097 int i;
2098 int err;
2099
2100 alloc_size = sizeof(struct mlxsw_sp_port *) * max_ports;
2101 mlxsw_sp->ports = kzalloc(alloc_size, GFP_KERNEL);
2102 if (!mlxsw_sp->ports)
2103 return -ENOMEM;
2104
2105 events = &mlxsw_sp->port_mapping_events;
2106 INIT_LIST_HEAD(&events->queue);
2107 spin_lock_init(&events->queue_lock);
2108 INIT_WORK(&events->work, mlxsw_sp_port_mapping_events_work);
2109
2110 for (i = 1; i < max_ports; i++) {
2111 err = mlxsw_sp_port_mapping_event_set(mlxsw_sp, i, true);
2112 if (err)
2113 goto err_event_enable;
2114 }
2115
2116 err = mlxsw_sp_cpu_port_create(mlxsw_sp);
2117 if (err)
2118 goto err_cpu_port_create;
2119
2120 for (i = 1; i < max_ports; i++) {
2121 port_mapping = &mlxsw_sp->port_mapping[i];
2122 if (!port_mapping->width)
2123 continue;
2124 err = mlxsw_sp_port_create(mlxsw_sp, i, false, port_mapping);
2125 if (err)
2126 goto err_port_create;
2127 }
2128 return 0;
2129
2130 err_port_create:
2131 for (i--; i >= 1; i--)
2132 if (mlxsw_sp_port_created(mlxsw_sp, i))
2133 mlxsw_sp_port_remove(mlxsw_sp, i);
2134 i = max_ports;
2135 mlxsw_sp_cpu_port_remove(mlxsw_sp);
2136 err_cpu_port_create:
2137 err_event_enable:
2138 for (i--; i >= 1; i--)
2139 mlxsw_sp_port_mapping_event_set(mlxsw_sp, i, false);
2140 /* Make sure all scheduled events are processed */
2141 __mlxsw_sp_port_mapping_events_cancel(mlxsw_sp);
2142 kfree(mlxsw_sp->ports);
2143 mlxsw_sp->ports = NULL;
2144 return err;
2145 }
2146
mlxsw_sp_port_module_info_init(struct mlxsw_sp * mlxsw_sp)2147 static int mlxsw_sp_port_module_info_init(struct mlxsw_sp *mlxsw_sp)
2148 {
2149 unsigned int max_ports = mlxsw_core_max_ports(mlxsw_sp->core);
2150 struct mlxsw_sp_port_mapping *port_mapping;
2151 int i;
2152 int err;
2153
2154 mlxsw_sp->port_mapping = kcalloc(max_ports,
2155 sizeof(struct mlxsw_sp_port_mapping),
2156 GFP_KERNEL);
2157 if (!mlxsw_sp->port_mapping)
2158 return -ENOMEM;
2159
2160 for (i = 1; i < max_ports; i++) {
2161 port_mapping = &mlxsw_sp->port_mapping[i];
2162 err = mlxsw_sp_port_module_info_get(mlxsw_sp, i, port_mapping);
2163 if (err)
2164 goto err_port_module_info_get;
2165 }
2166 return 0;
2167
2168 err_port_module_info_get:
2169 kfree(mlxsw_sp->port_mapping);
2170 return err;
2171 }
2172
mlxsw_sp_port_module_info_fini(struct mlxsw_sp * mlxsw_sp)2173 static void mlxsw_sp_port_module_info_fini(struct mlxsw_sp *mlxsw_sp)
2174 {
2175 kfree(mlxsw_sp->port_mapping);
2176 }
2177
2178 static int
mlxsw_sp_port_split_create(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_port_mapping * port_mapping,unsigned int count,const char * pmtdb_pl)2179 mlxsw_sp_port_split_create(struct mlxsw_sp *mlxsw_sp,
2180 struct mlxsw_sp_port_mapping *port_mapping,
2181 unsigned int count, const char *pmtdb_pl)
2182 {
2183 struct mlxsw_sp_port_mapping split_port_mapping;
2184 int err, i;
2185
2186 split_port_mapping = *port_mapping;
2187 split_port_mapping.width /= count;
2188 for (i = 0; i < count; i++) {
2189 u16 s_local_port = mlxsw_reg_pmtdb_port_num_get(pmtdb_pl, i);
2190
2191 if (!mlxsw_sp_local_port_valid(s_local_port))
2192 continue;
2193
2194 err = mlxsw_sp_port_create(mlxsw_sp, s_local_port,
2195 true, &split_port_mapping);
2196 if (err)
2197 goto err_port_create;
2198 split_port_mapping.lane += split_port_mapping.width;
2199 }
2200
2201 return 0;
2202
2203 err_port_create:
2204 for (i--; i >= 0; i--) {
2205 u16 s_local_port = mlxsw_reg_pmtdb_port_num_get(pmtdb_pl, i);
2206
2207 if (mlxsw_sp_port_created(mlxsw_sp, s_local_port))
2208 mlxsw_sp_port_remove(mlxsw_sp, s_local_port);
2209 }
2210 return err;
2211 }
2212
mlxsw_sp_port_unsplit_create(struct mlxsw_sp * mlxsw_sp,unsigned int count,const char * pmtdb_pl)2213 static void mlxsw_sp_port_unsplit_create(struct mlxsw_sp *mlxsw_sp,
2214 unsigned int count,
2215 const char *pmtdb_pl)
2216 {
2217 struct mlxsw_sp_port_mapping *port_mapping;
2218 int i;
2219
2220 /* Go over original unsplit ports in the gap and recreate them. */
2221 for (i = 0; i < count; i++) {
2222 u16 local_port = mlxsw_reg_pmtdb_port_num_get(pmtdb_pl, i);
2223
2224 port_mapping = &mlxsw_sp->port_mapping[local_port];
2225 if (!port_mapping->width || !mlxsw_sp_local_port_valid(local_port))
2226 continue;
2227 mlxsw_sp_port_create(mlxsw_sp, local_port,
2228 false, port_mapping);
2229 }
2230 }
2231
2232 static struct mlxsw_sp_port *
mlxsw_sp_port_get_by_local_port(struct mlxsw_sp * mlxsw_sp,u16 local_port)2233 mlxsw_sp_port_get_by_local_port(struct mlxsw_sp *mlxsw_sp, u16 local_port)
2234 {
2235 if (mlxsw_sp->ports && mlxsw_sp->ports[local_port])
2236 return mlxsw_sp->ports[local_port];
2237 return NULL;
2238 }
2239
mlxsw_sp_port_split(struct mlxsw_core * mlxsw_core,u16 local_port,unsigned int count,struct netlink_ext_ack * extack)2240 static int mlxsw_sp_port_split(struct mlxsw_core *mlxsw_core, u16 local_port,
2241 unsigned int count,
2242 struct netlink_ext_ack *extack)
2243 {
2244 struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
2245 struct mlxsw_sp_port_mapping port_mapping;
2246 struct mlxsw_sp_port *mlxsw_sp_port;
2247 enum mlxsw_reg_pmtdb_status status;
2248 char pmtdb_pl[MLXSW_REG_PMTDB_LEN];
2249 int i;
2250 int err;
2251
2252 mlxsw_sp_port = mlxsw_sp_port_get_by_local_port(mlxsw_sp, local_port);
2253 if (!mlxsw_sp_port) {
2254 dev_err(mlxsw_sp->bus_info->dev, "Port number \"%d\" does not exist\n",
2255 local_port);
2256 NL_SET_ERR_MSG_MOD(extack, "Port number does not exist");
2257 return -EINVAL;
2258 }
2259
2260 if (mlxsw_sp_port->split) {
2261 NL_SET_ERR_MSG_MOD(extack, "Port is already split");
2262 return -EINVAL;
2263 }
2264
2265 mlxsw_reg_pmtdb_pack(pmtdb_pl, mlxsw_sp_port->mapping.slot_index,
2266 mlxsw_sp_port->mapping.module,
2267 mlxsw_sp_port->mapping.module_width / count,
2268 count);
2269 err = mlxsw_reg_query(mlxsw_core, MLXSW_REG(pmtdb), pmtdb_pl);
2270 if (err) {
2271 NL_SET_ERR_MSG_MOD(extack, "Failed to query split info");
2272 return err;
2273 }
2274
2275 status = mlxsw_reg_pmtdb_status_get(pmtdb_pl);
2276 if (status != MLXSW_REG_PMTDB_STATUS_SUCCESS) {
2277 NL_SET_ERR_MSG_MOD(extack, "Unsupported split configuration");
2278 return -EINVAL;
2279 }
2280
2281 port_mapping = mlxsw_sp_port->mapping;
2282
2283 for (i = 0; i < count; i++) {
2284 u16 s_local_port = mlxsw_reg_pmtdb_port_num_get(pmtdb_pl, i);
2285
2286 if (mlxsw_sp_port_created(mlxsw_sp, s_local_port))
2287 mlxsw_sp_port_remove(mlxsw_sp, s_local_port);
2288 }
2289
2290 err = mlxsw_sp_port_split_create(mlxsw_sp, &port_mapping,
2291 count, pmtdb_pl);
2292 if (err) {
2293 dev_err(mlxsw_sp->bus_info->dev, "Failed to create split ports\n");
2294 goto err_port_split_create;
2295 }
2296
2297 return 0;
2298
2299 err_port_split_create:
2300 mlxsw_sp_port_unsplit_create(mlxsw_sp, count, pmtdb_pl);
2301
2302 return err;
2303 }
2304
mlxsw_sp_port_unsplit(struct mlxsw_core * mlxsw_core,u16 local_port,struct netlink_ext_ack * extack)2305 static int mlxsw_sp_port_unsplit(struct mlxsw_core *mlxsw_core, u16 local_port,
2306 struct netlink_ext_ack *extack)
2307 {
2308 struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
2309 struct mlxsw_sp_port *mlxsw_sp_port;
2310 char pmtdb_pl[MLXSW_REG_PMTDB_LEN];
2311 unsigned int count;
2312 int i;
2313 int err;
2314
2315 mlxsw_sp_port = mlxsw_sp_port_get_by_local_port(mlxsw_sp, local_port);
2316 if (!mlxsw_sp_port) {
2317 dev_err(mlxsw_sp->bus_info->dev, "Port number \"%d\" does not exist\n",
2318 local_port);
2319 NL_SET_ERR_MSG_MOD(extack, "Port number does not exist");
2320 return -EINVAL;
2321 }
2322
2323 if (!mlxsw_sp_port->split) {
2324 NL_SET_ERR_MSG_MOD(extack, "Port was not split");
2325 return -EINVAL;
2326 }
2327
2328 count = mlxsw_sp_port->mapping.module_width /
2329 mlxsw_sp_port->mapping.width;
2330
2331 mlxsw_reg_pmtdb_pack(pmtdb_pl, mlxsw_sp_port->mapping.slot_index,
2332 mlxsw_sp_port->mapping.module,
2333 mlxsw_sp_port->mapping.module_width / count,
2334 count);
2335 err = mlxsw_reg_query(mlxsw_core, MLXSW_REG(pmtdb), pmtdb_pl);
2336 if (err) {
2337 NL_SET_ERR_MSG_MOD(extack, "Failed to query split info");
2338 return err;
2339 }
2340
2341 for (i = 0; i < count; i++) {
2342 u16 s_local_port = mlxsw_reg_pmtdb_port_num_get(pmtdb_pl, i);
2343
2344 if (mlxsw_sp_port_created(mlxsw_sp, s_local_port))
2345 mlxsw_sp_port_remove(mlxsw_sp, s_local_port);
2346 }
2347
2348 mlxsw_sp_port_unsplit_create(mlxsw_sp, count, pmtdb_pl);
2349
2350 return 0;
2351 }
2352
2353 static void
mlxsw_sp_port_down_wipe_counters(struct mlxsw_sp_port * mlxsw_sp_port)2354 mlxsw_sp_port_down_wipe_counters(struct mlxsw_sp_port *mlxsw_sp_port)
2355 {
2356 int i;
2357
2358 for (i = 0; i < TC_MAX_QUEUE; i++)
2359 mlxsw_sp_port->periodic_hw_stats.xstats.backlog[i] = 0;
2360 }
2361
mlxsw_sp_pude_event_func(const struct mlxsw_reg_info * reg,char * pude_pl,void * priv)2362 static void mlxsw_sp_pude_event_func(const struct mlxsw_reg_info *reg,
2363 char *pude_pl, void *priv)
2364 {
2365 struct mlxsw_sp *mlxsw_sp = priv;
2366 struct mlxsw_sp_port *mlxsw_sp_port;
2367 enum mlxsw_reg_pude_oper_status status;
2368 u16 local_port;
2369
2370 local_port = mlxsw_reg_pude_local_port_get(pude_pl);
2371
2372 if (WARN_ON_ONCE(!mlxsw_sp_local_port_is_valid(mlxsw_sp, local_port)))
2373 return;
2374 mlxsw_sp_port = mlxsw_sp->ports[local_port];
2375 if (!mlxsw_sp_port)
2376 return;
2377
2378 status = mlxsw_reg_pude_oper_status_get(pude_pl);
2379 if (status == MLXSW_PORT_OPER_STATUS_UP) {
2380 netdev_info(mlxsw_sp_port->dev, "link up\n");
2381 netif_carrier_on(mlxsw_sp_port->dev);
2382 mlxsw_core_schedule_dw(&mlxsw_sp_port->ptp.shaper_dw, 0);
2383 } else {
2384 netdev_info(mlxsw_sp_port->dev, "link down\n");
2385 netif_carrier_off(mlxsw_sp_port->dev);
2386 mlxsw_sp_port_down_wipe_counters(mlxsw_sp_port);
2387 }
2388 }
2389
mlxsw_sp1_ptp_fifo_event_func(struct mlxsw_sp * mlxsw_sp,char * mtpptr_pl,bool ingress)2390 static void mlxsw_sp1_ptp_fifo_event_func(struct mlxsw_sp *mlxsw_sp,
2391 char *mtpptr_pl, bool ingress)
2392 {
2393 u16 local_port;
2394 u8 num_rec;
2395 int i;
2396
2397 local_port = mlxsw_reg_mtpptr_local_port_get(mtpptr_pl);
2398 num_rec = mlxsw_reg_mtpptr_num_rec_get(mtpptr_pl);
2399 for (i = 0; i < num_rec; i++) {
2400 u8 domain_number;
2401 u8 message_type;
2402 u16 sequence_id;
2403 u64 timestamp;
2404
2405 mlxsw_reg_mtpptr_unpack(mtpptr_pl, i, &message_type,
2406 &domain_number, &sequence_id,
2407 ×tamp);
2408 mlxsw_sp1_ptp_got_timestamp(mlxsw_sp, ingress, local_port,
2409 message_type, domain_number,
2410 sequence_id, timestamp);
2411 }
2412 }
2413
mlxsw_sp1_ptp_ing_fifo_event_func(const struct mlxsw_reg_info * reg,char * mtpptr_pl,void * priv)2414 static void mlxsw_sp1_ptp_ing_fifo_event_func(const struct mlxsw_reg_info *reg,
2415 char *mtpptr_pl, void *priv)
2416 {
2417 struct mlxsw_sp *mlxsw_sp = priv;
2418
2419 mlxsw_sp1_ptp_fifo_event_func(mlxsw_sp, mtpptr_pl, true);
2420 }
2421
mlxsw_sp1_ptp_egr_fifo_event_func(const struct mlxsw_reg_info * reg,char * mtpptr_pl,void * priv)2422 static void mlxsw_sp1_ptp_egr_fifo_event_func(const struct mlxsw_reg_info *reg,
2423 char *mtpptr_pl, void *priv)
2424 {
2425 struct mlxsw_sp *mlxsw_sp = priv;
2426
2427 mlxsw_sp1_ptp_fifo_event_func(mlxsw_sp, mtpptr_pl, false);
2428 }
2429
mlxsw_sp_rx_listener_no_mark_func(struct sk_buff * skb,u16 local_port,void * priv)2430 void mlxsw_sp_rx_listener_no_mark_func(struct sk_buff *skb,
2431 u16 local_port, void *priv)
2432 {
2433 struct mlxsw_sp *mlxsw_sp = priv;
2434 struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp->ports[local_port];
2435 struct mlxsw_sp_port_pcpu_stats *pcpu_stats;
2436
2437 if (unlikely(!mlxsw_sp_port)) {
2438 dev_warn_ratelimited(mlxsw_sp->bus_info->dev, "Port %d: skb received for non-existent port\n",
2439 local_port);
2440 return;
2441 }
2442
2443 skb->dev = mlxsw_sp_port->dev;
2444
2445 pcpu_stats = this_cpu_ptr(mlxsw_sp_port->pcpu_stats);
2446 u64_stats_update_begin(&pcpu_stats->syncp);
2447 pcpu_stats->rx_packets++;
2448 pcpu_stats->rx_bytes += skb->len;
2449 u64_stats_update_end(&pcpu_stats->syncp);
2450
2451 skb->protocol = eth_type_trans(skb, skb->dev);
2452 netif_receive_skb(skb);
2453 }
2454
mlxsw_sp_rx_listener_mark_func(struct sk_buff * skb,u16 local_port,void * priv)2455 static void mlxsw_sp_rx_listener_mark_func(struct sk_buff *skb, u16 local_port,
2456 void *priv)
2457 {
2458 skb->offload_fwd_mark = 1;
2459 return mlxsw_sp_rx_listener_no_mark_func(skb, local_port, priv);
2460 }
2461
mlxsw_sp_rx_listener_l3_mark_func(struct sk_buff * skb,u16 local_port,void * priv)2462 static void mlxsw_sp_rx_listener_l3_mark_func(struct sk_buff *skb,
2463 u16 local_port, void *priv)
2464 {
2465 skb->offload_l3_fwd_mark = 1;
2466 skb->offload_fwd_mark = 1;
2467 return mlxsw_sp_rx_listener_no_mark_func(skb, local_port, priv);
2468 }
2469
mlxsw_sp_ptp_receive(struct mlxsw_sp * mlxsw_sp,struct sk_buff * skb,u16 local_port)2470 void mlxsw_sp_ptp_receive(struct mlxsw_sp *mlxsw_sp, struct sk_buff *skb,
2471 u16 local_port)
2472 {
2473 mlxsw_sp->ptp_ops->receive(mlxsw_sp, skb, local_port);
2474 }
2475
2476 #define MLXSW_SP_RXL_NO_MARK(_trap_id, _action, _trap_group, _is_ctrl) \
2477 MLXSW_RXL(mlxsw_sp_rx_listener_no_mark_func, _trap_id, _action, \
2478 _is_ctrl, SP_##_trap_group, DISCARD)
2479
2480 #define MLXSW_SP_RXL_MARK(_trap_id, _action, _trap_group, _is_ctrl) \
2481 MLXSW_RXL(mlxsw_sp_rx_listener_mark_func, _trap_id, _action, \
2482 _is_ctrl, SP_##_trap_group, DISCARD)
2483
2484 #define MLXSW_SP_RXL_L3_MARK(_trap_id, _action, _trap_group, _is_ctrl) \
2485 MLXSW_RXL(mlxsw_sp_rx_listener_l3_mark_func, _trap_id, _action, \
2486 _is_ctrl, SP_##_trap_group, DISCARD)
2487
2488 #define MLXSW_SP_EVENTL(_func, _trap_id) \
2489 MLXSW_EVENTL(_func, _trap_id, SP_EVENT)
2490
2491 static const struct mlxsw_listener mlxsw_sp_listener[] = {
2492 /* Events */
2493 MLXSW_SP_EVENTL(mlxsw_sp_pude_event_func, PUDE),
2494 /* L2 traps */
2495 MLXSW_SP_RXL_NO_MARK(FID_MISS, TRAP_TO_CPU, FID_MISS, false),
2496 /* L3 traps */
2497 MLXSW_SP_RXL_MARK(IPV6_UNSPECIFIED_ADDRESS, TRAP_TO_CPU, ROUTER_EXP,
2498 false),
2499 MLXSW_SP_RXL_MARK(IPV6_LINK_LOCAL_SRC, TRAP_TO_CPU, ROUTER_EXP, false),
2500 MLXSW_SP_RXL_MARK(IPV6_MC_LINK_LOCAL_DEST, TRAP_TO_CPU, ROUTER_EXP,
2501 false),
2502 MLXSW_SP_RXL_NO_MARK(DISCARD_ING_ROUTER_SIP_CLASS_E, FORWARD,
2503 ROUTER_EXP, false),
2504 MLXSW_SP_RXL_NO_MARK(DISCARD_ING_ROUTER_MC_DMAC, FORWARD,
2505 ROUTER_EXP, false),
2506 MLXSW_SP_RXL_NO_MARK(DISCARD_ING_ROUTER_SIP_DIP, FORWARD,
2507 ROUTER_EXP, false),
2508 MLXSW_SP_RXL_NO_MARK(DISCARD_ING_ROUTER_DIP_LINK_LOCAL, FORWARD,
2509 ROUTER_EXP, false),
2510 MLXSW_SP_RXL_NO_MARK(DISCARD_ING_ROUTER_SIP_LINK_LOCAL, FORWARD,
2511 ROUTER_EXP, false),
2512 /* Multicast Router Traps */
2513 MLXSW_SP_RXL_MARK(ACL1, TRAP_TO_CPU, MULTICAST, false),
2514 MLXSW_SP_RXL_L3_MARK(ACL2, TRAP_TO_CPU, MULTICAST, false),
2515 /* NVE traps */
2516 MLXSW_SP_RXL_MARK(NVE_ENCAP_ARP, TRAP_TO_CPU, NEIGH_DISCOVERY, false),
2517 };
2518
2519 static const struct mlxsw_listener mlxsw_sp1_listener[] = {
2520 /* Events */
2521 MLXSW_EVENTL(mlxsw_sp1_ptp_egr_fifo_event_func, PTP_EGR_FIFO, SP_PTP0),
2522 MLXSW_EVENTL(mlxsw_sp1_ptp_ing_fifo_event_func, PTP_ING_FIFO, SP_PTP0),
2523 };
2524
2525 static const struct mlxsw_listener mlxsw_sp2_listener[] = {
2526 /* Events */
2527 MLXSW_SP_EVENTL(mlxsw_sp_port_mapping_listener_func, PMLPE),
2528 };
2529
mlxsw_sp_cpu_policers_set(struct mlxsw_core * mlxsw_core)2530 static int mlxsw_sp_cpu_policers_set(struct mlxsw_core *mlxsw_core)
2531 {
2532 struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
2533 char qpcr_pl[MLXSW_REG_QPCR_LEN];
2534 enum mlxsw_reg_qpcr_ir_units ir_units;
2535 int max_cpu_policers;
2536 bool is_bytes;
2537 u8 burst_size;
2538 u32 rate;
2539 int i, err;
2540
2541 if (!MLXSW_CORE_RES_VALID(mlxsw_core, MAX_CPU_POLICERS))
2542 return -EIO;
2543
2544 max_cpu_policers = MLXSW_CORE_RES_GET(mlxsw_core, MAX_CPU_POLICERS);
2545
2546 ir_units = MLXSW_REG_QPCR_IR_UNITS_M;
2547 for (i = 0; i < max_cpu_policers; i++) {
2548 is_bytes = false;
2549 switch (i) {
2550 case MLXSW_REG_HTGT_TRAP_GROUP_SP_ROUTER_EXP:
2551 case MLXSW_REG_HTGT_TRAP_GROUP_SP_MULTICAST:
2552 case MLXSW_REG_HTGT_TRAP_GROUP_SP_FID_MISS:
2553 rate = 1024;
2554 burst_size = 7;
2555 break;
2556 default:
2557 continue;
2558 }
2559
2560 __set_bit(i, mlxsw_sp->trap->policers_usage);
2561 mlxsw_reg_qpcr_pack(qpcr_pl, i, ir_units, is_bytes, rate,
2562 burst_size);
2563 err = mlxsw_reg_write(mlxsw_core, MLXSW_REG(qpcr), qpcr_pl);
2564 if (err)
2565 return err;
2566 }
2567
2568 return 0;
2569 }
2570
mlxsw_sp_trap_groups_set(struct mlxsw_core * mlxsw_core)2571 static int mlxsw_sp_trap_groups_set(struct mlxsw_core *mlxsw_core)
2572 {
2573 char htgt_pl[MLXSW_REG_HTGT_LEN];
2574 enum mlxsw_reg_htgt_trap_group i;
2575 int max_cpu_policers;
2576 int max_trap_groups;
2577 u8 priority, tc;
2578 u16 policer_id;
2579 int err;
2580
2581 if (!MLXSW_CORE_RES_VALID(mlxsw_core, MAX_TRAP_GROUPS))
2582 return -EIO;
2583
2584 max_trap_groups = MLXSW_CORE_RES_GET(mlxsw_core, MAX_TRAP_GROUPS);
2585 max_cpu_policers = MLXSW_CORE_RES_GET(mlxsw_core, MAX_CPU_POLICERS);
2586
2587 for (i = 0; i < max_trap_groups; i++) {
2588 policer_id = i;
2589 switch (i) {
2590 case MLXSW_REG_HTGT_TRAP_GROUP_SP_ROUTER_EXP:
2591 case MLXSW_REG_HTGT_TRAP_GROUP_SP_MULTICAST:
2592 case MLXSW_REG_HTGT_TRAP_GROUP_SP_FID_MISS:
2593 priority = 1;
2594 tc = 1;
2595 break;
2596 case MLXSW_REG_HTGT_TRAP_GROUP_SP_EVENT:
2597 priority = MLXSW_REG_HTGT_DEFAULT_PRIORITY;
2598 tc = MLXSW_REG_HTGT_DEFAULT_TC;
2599 policer_id = MLXSW_REG_HTGT_INVALID_POLICER;
2600 break;
2601 default:
2602 continue;
2603 }
2604
2605 if (max_cpu_policers <= policer_id &&
2606 policer_id != MLXSW_REG_HTGT_INVALID_POLICER)
2607 return -EIO;
2608
2609 mlxsw_reg_htgt_pack(htgt_pl, i, policer_id, priority, tc);
2610 err = mlxsw_reg_write(mlxsw_core, MLXSW_REG(htgt), htgt_pl);
2611 if (err)
2612 return err;
2613 }
2614
2615 return 0;
2616 }
2617
mlxsw_sp_traps_init(struct mlxsw_sp * mlxsw_sp)2618 static int mlxsw_sp_traps_init(struct mlxsw_sp *mlxsw_sp)
2619 {
2620 struct mlxsw_sp_trap *trap;
2621 u64 max_policers;
2622 int err;
2623
2624 if (!MLXSW_CORE_RES_VALID(mlxsw_sp->core, MAX_CPU_POLICERS))
2625 return -EIO;
2626 max_policers = MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_CPU_POLICERS);
2627 trap = kzalloc(struct_size(trap, policers_usage,
2628 BITS_TO_LONGS(max_policers)), GFP_KERNEL);
2629 if (!trap)
2630 return -ENOMEM;
2631 trap->max_policers = max_policers;
2632 mlxsw_sp->trap = trap;
2633
2634 err = mlxsw_sp_cpu_policers_set(mlxsw_sp->core);
2635 if (err)
2636 goto err_cpu_policers_set;
2637
2638 err = mlxsw_sp_trap_groups_set(mlxsw_sp->core);
2639 if (err)
2640 goto err_trap_groups_set;
2641
2642 err = mlxsw_core_traps_register(mlxsw_sp->core, mlxsw_sp_listener,
2643 ARRAY_SIZE(mlxsw_sp_listener),
2644 mlxsw_sp);
2645 if (err)
2646 goto err_traps_register;
2647
2648 err = mlxsw_core_traps_register(mlxsw_sp->core, mlxsw_sp->listeners,
2649 mlxsw_sp->listeners_count, mlxsw_sp);
2650 if (err)
2651 goto err_extra_traps_init;
2652
2653 return 0;
2654
2655 err_extra_traps_init:
2656 mlxsw_core_traps_unregister(mlxsw_sp->core, mlxsw_sp_listener,
2657 ARRAY_SIZE(mlxsw_sp_listener),
2658 mlxsw_sp);
2659 err_traps_register:
2660 err_trap_groups_set:
2661 err_cpu_policers_set:
2662 kfree(trap);
2663 return err;
2664 }
2665
mlxsw_sp_traps_fini(struct mlxsw_sp * mlxsw_sp)2666 static void mlxsw_sp_traps_fini(struct mlxsw_sp *mlxsw_sp)
2667 {
2668 mlxsw_core_traps_unregister(mlxsw_sp->core, mlxsw_sp->listeners,
2669 mlxsw_sp->listeners_count,
2670 mlxsw_sp);
2671 mlxsw_core_traps_unregister(mlxsw_sp->core, mlxsw_sp_listener,
2672 ARRAY_SIZE(mlxsw_sp_listener), mlxsw_sp);
2673 kfree(mlxsw_sp->trap);
2674 }
2675
mlxsw_sp_lag_pgt_init(struct mlxsw_sp * mlxsw_sp)2676 static int mlxsw_sp_lag_pgt_init(struct mlxsw_sp *mlxsw_sp)
2677 {
2678 char sgcr_pl[MLXSW_REG_SGCR_LEN];
2679 int err;
2680
2681 if (mlxsw_core_lag_mode(mlxsw_sp->core) !=
2682 MLXSW_CMD_MBOX_CONFIG_PROFILE_LAG_MODE_SW)
2683 return 0;
2684
2685 /* In DDD mode, which we by default use, each LAG entry is 8 PGT
2686 * entries. The LAG table address needs to be 8-aligned, but that ought
2687 * to be the case, since the LAG table is allocated first.
2688 */
2689 err = mlxsw_sp_pgt_mid_alloc_range(mlxsw_sp, &mlxsw_sp->lag_pgt_base,
2690 mlxsw_sp->max_lag * 8);
2691 if (err)
2692 return err;
2693 if (WARN_ON_ONCE(mlxsw_sp->lag_pgt_base % 8)) {
2694 err = -EINVAL;
2695 goto err_mid_alloc_range;
2696 }
2697
2698 mlxsw_reg_sgcr_pack(sgcr_pl, mlxsw_sp->lag_pgt_base);
2699 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sgcr), sgcr_pl);
2700 if (err)
2701 goto err_mid_alloc_range;
2702
2703 return 0;
2704
2705 err_mid_alloc_range:
2706 mlxsw_sp_pgt_mid_free_range(mlxsw_sp, mlxsw_sp->lag_pgt_base,
2707 mlxsw_sp->max_lag * 8);
2708 return err;
2709 }
2710
mlxsw_sp_lag_pgt_fini(struct mlxsw_sp * mlxsw_sp)2711 static void mlxsw_sp_lag_pgt_fini(struct mlxsw_sp *mlxsw_sp)
2712 {
2713 if (mlxsw_core_lag_mode(mlxsw_sp->core) !=
2714 MLXSW_CMD_MBOX_CONFIG_PROFILE_LAG_MODE_SW)
2715 return;
2716
2717 mlxsw_sp_pgt_mid_free_range(mlxsw_sp, mlxsw_sp->lag_pgt_base,
2718 mlxsw_sp->max_lag * 8);
2719 }
2720
2721 #define MLXSW_SP_LAG_SEED_INIT 0xcafecafe
2722
2723 struct mlxsw_sp_lag {
2724 struct net_device *dev;
2725 refcount_t ref_count;
2726 u16 lag_id;
2727 };
2728
mlxsw_sp_lag_init(struct mlxsw_sp * mlxsw_sp)2729 static int mlxsw_sp_lag_init(struct mlxsw_sp *mlxsw_sp)
2730 {
2731 char slcr_pl[MLXSW_REG_SLCR_LEN];
2732 u32 seed;
2733 int err;
2734
2735 seed = jhash(mlxsw_sp->base_mac, sizeof(mlxsw_sp->base_mac),
2736 MLXSW_SP_LAG_SEED_INIT);
2737 mlxsw_reg_slcr_pack(slcr_pl, MLXSW_REG_SLCR_LAG_HASH_SMAC |
2738 MLXSW_REG_SLCR_LAG_HASH_DMAC |
2739 MLXSW_REG_SLCR_LAG_HASH_ETHERTYPE |
2740 MLXSW_REG_SLCR_LAG_HASH_VLANID |
2741 MLXSW_REG_SLCR_LAG_HASH_SIP |
2742 MLXSW_REG_SLCR_LAG_HASH_DIP |
2743 MLXSW_REG_SLCR_LAG_HASH_SPORT |
2744 MLXSW_REG_SLCR_LAG_HASH_DPORT |
2745 MLXSW_REG_SLCR_LAG_HASH_IPPROTO, seed);
2746 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(slcr), slcr_pl);
2747 if (err)
2748 return err;
2749
2750 err = mlxsw_core_max_lag(mlxsw_sp->core, &mlxsw_sp->max_lag);
2751 if (err)
2752 return err;
2753
2754 if (!MLXSW_CORE_RES_VALID(mlxsw_sp->core, MAX_LAG_MEMBERS))
2755 return -EIO;
2756
2757 err = mlxsw_sp_lag_pgt_init(mlxsw_sp);
2758 if (err)
2759 return err;
2760
2761 mlxsw_sp->lags = kcalloc(mlxsw_sp->max_lag, sizeof(struct mlxsw_sp_lag),
2762 GFP_KERNEL);
2763 if (!mlxsw_sp->lags) {
2764 err = -ENOMEM;
2765 goto err_kcalloc;
2766 }
2767
2768 return 0;
2769
2770 err_kcalloc:
2771 mlxsw_sp_lag_pgt_fini(mlxsw_sp);
2772 return err;
2773 }
2774
mlxsw_sp_lag_fini(struct mlxsw_sp * mlxsw_sp)2775 static void mlxsw_sp_lag_fini(struct mlxsw_sp *mlxsw_sp)
2776 {
2777 mlxsw_sp_lag_pgt_fini(mlxsw_sp);
2778 kfree(mlxsw_sp->lags);
2779 }
2780
2781 static const struct mlxsw_sp_ptp_ops mlxsw_sp1_ptp_ops = {
2782 .clock_init = mlxsw_sp1_ptp_clock_init,
2783 .clock_fini = mlxsw_sp1_ptp_clock_fini,
2784 .init = mlxsw_sp1_ptp_init,
2785 .fini = mlxsw_sp1_ptp_fini,
2786 .receive = mlxsw_sp1_ptp_receive,
2787 .transmitted = mlxsw_sp1_ptp_transmitted,
2788 .hwtstamp_get = mlxsw_sp1_ptp_hwtstamp_get,
2789 .hwtstamp_set = mlxsw_sp1_ptp_hwtstamp_set,
2790 .shaper_work = mlxsw_sp1_ptp_shaper_work,
2791 #if IS_REACHABLE(CONFIG_PTP_1588_CLOCK)
2792 .get_ts_info = mlxsw_sp1_ptp_get_ts_info,
2793 #endif
2794 .get_stats_count = mlxsw_sp1_get_stats_count,
2795 .get_stats_strings = mlxsw_sp1_get_stats_strings,
2796 .get_stats = mlxsw_sp1_get_stats,
2797 .txhdr_construct = mlxsw_sp_ptp_txhdr_construct,
2798 };
2799
2800 static const struct mlxsw_sp_ptp_ops mlxsw_sp2_ptp_ops = {
2801 .clock_init = mlxsw_sp2_ptp_clock_init,
2802 .clock_fini = mlxsw_sp2_ptp_clock_fini,
2803 .init = mlxsw_sp2_ptp_init,
2804 .fini = mlxsw_sp2_ptp_fini,
2805 .receive = mlxsw_sp2_ptp_receive,
2806 .transmitted = mlxsw_sp2_ptp_transmitted,
2807 .hwtstamp_get = mlxsw_sp2_ptp_hwtstamp_get,
2808 .hwtstamp_set = mlxsw_sp2_ptp_hwtstamp_set,
2809 .shaper_work = mlxsw_sp2_ptp_shaper_work,
2810 #if IS_REACHABLE(CONFIG_PTP_1588_CLOCK)
2811 .get_ts_info = mlxsw_sp2_ptp_get_ts_info,
2812 #endif
2813 .get_stats_count = mlxsw_sp2_get_stats_count,
2814 .get_stats_strings = mlxsw_sp2_get_stats_strings,
2815 .get_stats = mlxsw_sp2_get_stats,
2816 .txhdr_construct = mlxsw_sp2_ptp_txhdr_construct,
2817 };
2818
2819 static const struct mlxsw_sp_ptp_ops mlxsw_sp4_ptp_ops = {
2820 .clock_init = mlxsw_sp2_ptp_clock_init,
2821 .clock_fini = mlxsw_sp2_ptp_clock_fini,
2822 .init = mlxsw_sp2_ptp_init,
2823 .fini = mlxsw_sp2_ptp_fini,
2824 .receive = mlxsw_sp2_ptp_receive,
2825 .transmitted = mlxsw_sp2_ptp_transmitted,
2826 .hwtstamp_get = mlxsw_sp2_ptp_hwtstamp_get,
2827 .hwtstamp_set = mlxsw_sp2_ptp_hwtstamp_set,
2828 .shaper_work = mlxsw_sp2_ptp_shaper_work,
2829 #if IS_REACHABLE(CONFIG_PTP_1588_CLOCK)
2830 .get_ts_info = mlxsw_sp2_ptp_get_ts_info,
2831 #endif
2832 .get_stats_count = mlxsw_sp2_get_stats_count,
2833 .get_stats_strings = mlxsw_sp2_get_stats_strings,
2834 .get_stats = mlxsw_sp2_get_stats,
2835 .txhdr_construct = mlxsw_sp_ptp_txhdr_construct,
2836 };
2837
2838 struct mlxsw_sp_sample_trigger_node {
2839 struct mlxsw_sp_sample_trigger trigger;
2840 struct mlxsw_sp_sample_params params;
2841 struct rhash_head ht_node;
2842 struct rcu_head rcu;
2843 refcount_t refcount;
2844 };
2845
2846 static const struct rhashtable_params mlxsw_sp_sample_trigger_ht_params = {
2847 .key_offset = offsetof(struct mlxsw_sp_sample_trigger_node, trigger),
2848 .head_offset = offsetof(struct mlxsw_sp_sample_trigger_node, ht_node),
2849 .key_len = sizeof(struct mlxsw_sp_sample_trigger),
2850 .automatic_shrinking = true,
2851 };
2852
2853 static void
mlxsw_sp_sample_trigger_key_init(struct mlxsw_sp_sample_trigger * key,const struct mlxsw_sp_sample_trigger * trigger)2854 mlxsw_sp_sample_trigger_key_init(struct mlxsw_sp_sample_trigger *key,
2855 const struct mlxsw_sp_sample_trigger *trigger)
2856 {
2857 memset(key, 0, sizeof(*key));
2858 key->type = trigger->type;
2859 key->local_port = trigger->local_port;
2860 }
2861
2862 /* RCU read lock must be held */
2863 struct mlxsw_sp_sample_params *
mlxsw_sp_sample_trigger_params_lookup(struct mlxsw_sp * mlxsw_sp,const struct mlxsw_sp_sample_trigger * trigger)2864 mlxsw_sp_sample_trigger_params_lookup(struct mlxsw_sp *mlxsw_sp,
2865 const struct mlxsw_sp_sample_trigger *trigger)
2866 {
2867 struct mlxsw_sp_sample_trigger_node *trigger_node;
2868 struct mlxsw_sp_sample_trigger key;
2869
2870 mlxsw_sp_sample_trigger_key_init(&key, trigger);
2871 trigger_node = rhashtable_lookup(&mlxsw_sp->sample_trigger_ht, &key,
2872 mlxsw_sp_sample_trigger_ht_params);
2873 if (!trigger_node)
2874 return NULL;
2875
2876 return &trigger_node->params;
2877 }
2878
2879 static int
mlxsw_sp_sample_trigger_node_init(struct mlxsw_sp * mlxsw_sp,const struct mlxsw_sp_sample_trigger * trigger,const struct mlxsw_sp_sample_params * params)2880 mlxsw_sp_sample_trigger_node_init(struct mlxsw_sp *mlxsw_sp,
2881 const struct mlxsw_sp_sample_trigger *trigger,
2882 const struct mlxsw_sp_sample_params *params)
2883 {
2884 struct mlxsw_sp_sample_trigger_node *trigger_node;
2885 int err;
2886
2887 trigger_node = kzalloc(sizeof(*trigger_node), GFP_KERNEL);
2888 if (!trigger_node)
2889 return -ENOMEM;
2890
2891 trigger_node->trigger = *trigger;
2892 trigger_node->params = *params;
2893 refcount_set(&trigger_node->refcount, 1);
2894
2895 err = rhashtable_insert_fast(&mlxsw_sp->sample_trigger_ht,
2896 &trigger_node->ht_node,
2897 mlxsw_sp_sample_trigger_ht_params);
2898 if (err)
2899 goto err_rhashtable_insert;
2900
2901 return 0;
2902
2903 err_rhashtable_insert:
2904 kfree(trigger_node);
2905 return err;
2906 }
2907
2908 static void
mlxsw_sp_sample_trigger_node_fini(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_sample_trigger_node * trigger_node)2909 mlxsw_sp_sample_trigger_node_fini(struct mlxsw_sp *mlxsw_sp,
2910 struct mlxsw_sp_sample_trigger_node *trigger_node)
2911 {
2912 rhashtable_remove_fast(&mlxsw_sp->sample_trigger_ht,
2913 &trigger_node->ht_node,
2914 mlxsw_sp_sample_trigger_ht_params);
2915 kfree_rcu(trigger_node, rcu);
2916 }
2917
2918 int
mlxsw_sp_sample_trigger_params_set(struct mlxsw_sp * mlxsw_sp,const struct mlxsw_sp_sample_trigger * trigger,const struct mlxsw_sp_sample_params * params,struct netlink_ext_ack * extack)2919 mlxsw_sp_sample_trigger_params_set(struct mlxsw_sp *mlxsw_sp,
2920 const struct mlxsw_sp_sample_trigger *trigger,
2921 const struct mlxsw_sp_sample_params *params,
2922 struct netlink_ext_ack *extack)
2923 {
2924 struct mlxsw_sp_sample_trigger_node *trigger_node;
2925 struct mlxsw_sp_sample_trigger key;
2926
2927 ASSERT_RTNL();
2928
2929 mlxsw_sp_sample_trigger_key_init(&key, trigger);
2930
2931 trigger_node = rhashtable_lookup_fast(&mlxsw_sp->sample_trigger_ht,
2932 &key,
2933 mlxsw_sp_sample_trigger_ht_params);
2934 if (!trigger_node)
2935 return mlxsw_sp_sample_trigger_node_init(mlxsw_sp, &key,
2936 params);
2937
2938 if (trigger_node->trigger.local_port) {
2939 NL_SET_ERR_MSG_MOD(extack, "Sampling already enabled on port");
2940 return -EINVAL;
2941 }
2942
2943 if (trigger_node->params.psample_group != params->psample_group ||
2944 trigger_node->params.truncate != params->truncate ||
2945 trigger_node->params.rate != params->rate ||
2946 trigger_node->params.trunc_size != params->trunc_size) {
2947 NL_SET_ERR_MSG_MOD(extack, "Sampling parameters do not match for an existing sampling trigger");
2948 return -EINVAL;
2949 }
2950
2951 refcount_inc(&trigger_node->refcount);
2952
2953 return 0;
2954 }
2955
2956 void
mlxsw_sp_sample_trigger_params_unset(struct mlxsw_sp * mlxsw_sp,const struct mlxsw_sp_sample_trigger * trigger)2957 mlxsw_sp_sample_trigger_params_unset(struct mlxsw_sp *mlxsw_sp,
2958 const struct mlxsw_sp_sample_trigger *trigger)
2959 {
2960 struct mlxsw_sp_sample_trigger_node *trigger_node;
2961 struct mlxsw_sp_sample_trigger key;
2962
2963 ASSERT_RTNL();
2964
2965 mlxsw_sp_sample_trigger_key_init(&key, trigger);
2966
2967 trigger_node = rhashtable_lookup_fast(&mlxsw_sp->sample_trigger_ht,
2968 &key,
2969 mlxsw_sp_sample_trigger_ht_params);
2970 if (!trigger_node)
2971 return;
2972
2973 if (!refcount_dec_and_test(&trigger_node->refcount))
2974 return;
2975
2976 mlxsw_sp_sample_trigger_node_fini(mlxsw_sp, trigger_node);
2977 }
2978
2979 static int mlxsw_sp_netdevice_event(struct notifier_block *unused,
2980 unsigned long event, void *ptr);
2981
2982 #define MLXSW_SP_DEFAULT_PARSING_DEPTH 96
2983 #define MLXSW_SP_INCREASED_PARSING_DEPTH 128
2984 #define MLXSW_SP_DEFAULT_VXLAN_UDP_DPORT 4789
2985
mlxsw_sp_parsing_init(struct mlxsw_sp * mlxsw_sp)2986 static void mlxsw_sp_parsing_init(struct mlxsw_sp *mlxsw_sp)
2987 {
2988 refcount_set(&mlxsw_sp->parsing.parsing_depth_ref, 0);
2989 mlxsw_sp->parsing.parsing_depth = MLXSW_SP_DEFAULT_PARSING_DEPTH;
2990 mlxsw_sp->parsing.vxlan_udp_dport = MLXSW_SP_DEFAULT_VXLAN_UDP_DPORT;
2991 mutex_init(&mlxsw_sp->parsing.lock);
2992 }
2993
mlxsw_sp_parsing_fini(struct mlxsw_sp * mlxsw_sp)2994 static void mlxsw_sp_parsing_fini(struct mlxsw_sp *mlxsw_sp)
2995 {
2996 mutex_destroy(&mlxsw_sp->parsing.lock);
2997 WARN_ON_ONCE(refcount_read(&mlxsw_sp->parsing.parsing_depth_ref));
2998 }
2999
3000 struct mlxsw_sp_ipv6_addr_node {
3001 struct in6_addr key;
3002 struct rhash_head ht_node;
3003 u32 kvdl_index;
3004 refcount_t refcount;
3005 };
3006
3007 static const struct rhashtable_params mlxsw_sp_ipv6_addr_ht_params = {
3008 .key_offset = offsetof(struct mlxsw_sp_ipv6_addr_node, key),
3009 .head_offset = offsetof(struct mlxsw_sp_ipv6_addr_node, ht_node),
3010 .key_len = sizeof(struct in6_addr),
3011 .automatic_shrinking = true,
3012 };
3013
3014 static int
mlxsw_sp_ipv6_addr_init(struct mlxsw_sp * mlxsw_sp,const struct in6_addr * addr6,u32 * p_kvdl_index)3015 mlxsw_sp_ipv6_addr_init(struct mlxsw_sp *mlxsw_sp, const struct in6_addr *addr6,
3016 u32 *p_kvdl_index)
3017 {
3018 struct mlxsw_sp_ipv6_addr_node *node;
3019 char rips_pl[MLXSW_REG_RIPS_LEN];
3020 int err;
3021
3022 err = mlxsw_sp_kvdl_alloc(mlxsw_sp,
3023 MLXSW_SP_KVDL_ENTRY_TYPE_IPV6_ADDRESS, 1,
3024 p_kvdl_index);
3025 if (err)
3026 return err;
3027
3028 mlxsw_reg_rips_pack(rips_pl, *p_kvdl_index, addr6);
3029 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(rips), rips_pl);
3030 if (err)
3031 goto err_rips_write;
3032
3033 node = kzalloc(sizeof(*node), GFP_KERNEL);
3034 if (!node) {
3035 err = -ENOMEM;
3036 goto err_node_alloc;
3037 }
3038
3039 node->key = *addr6;
3040 node->kvdl_index = *p_kvdl_index;
3041 refcount_set(&node->refcount, 1);
3042
3043 err = rhashtable_insert_fast(&mlxsw_sp->ipv6_addr_ht,
3044 &node->ht_node,
3045 mlxsw_sp_ipv6_addr_ht_params);
3046 if (err)
3047 goto err_rhashtable_insert;
3048
3049 return 0;
3050
3051 err_rhashtable_insert:
3052 kfree(node);
3053 err_node_alloc:
3054 err_rips_write:
3055 mlxsw_sp_kvdl_free(mlxsw_sp, MLXSW_SP_KVDL_ENTRY_TYPE_IPV6_ADDRESS, 1,
3056 *p_kvdl_index);
3057 return err;
3058 }
3059
mlxsw_sp_ipv6_addr_fini(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_ipv6_addr_node * node)3060 static void mlxsw_sp_ipv6_addr_fini(struct mlxsw_sp *mlxsw_sp,
3061 struct mlxsw_sp_ipv6_addr_node *node)
3062 {
3063 u32 kvdl_index = node->kvdl_index;
3064
3065 rhashtable_remove_fast(&mlxsw_sp->ipv6_addr_ht, &node->ht_node,
3066 mlxsw_sp_ipv6_addr_ht_params);
3067 kfree(node);
3068 mlxsw_sp_kvdl_free(mlxsw_sp, MLXSW_SP_KVDL_ENTRY_TYPE_IPV6_ADDRESS, 1,
3069 kvdl_index);
3070 }
3071
mlxsw_sp_ipv6_addr_kvdl_index_get(struct mlxsw_sp * mlxsw_sp,const struct in6_addr * addr6,u32 * p_kvdl_index)3072 int mlxsw_sp_ipv6_addr_kvdl_index_get(struct mlxsw_sp *mlxsw_sp,
3073 const struct in6_addr *addr6,
3074 u32 *p_kvdl_index)
3075 {
3076 struct mlxsw_sp_ipv6_addr_node *node;
3077 int err = 0;
3078
3079 mutex_lock(&mlxsw_sp->ipv6_addr_ht_lock);
3080 node = rhashtable_lookup_fast(&mlxsw_sp->ipv6_addr_ht, addr6,
3081 mlxsw_sp_ipv6_addr_ht_params);
3082 if (node) {
3083 refcount_inc(&node->refcount);
3084 *p_kvdl_index = node->kvdl_index;
3085 goto out_unlock;
3086 }
3087
3088 err = mlxsw_sp_ipv6_addr_init(mlxsw_sp, addr6, p_kvdl_index);
3089
3090 out_unlock:
3091 mutex_unlock(&mlxsw_sp->ipv6_addr_ht_lock);
3092 return err;
3093 }
3094
3095 void
mlxsw_sp_ipv6_addr_put(struct mlxsw_sp * mlxsw_sp,const struct in6_addr * addr6)3096 mlxsw_sp_ipv6_addr_put(struct mlxsw_sp *mlxsw_sp, const struct in6_addr *addr6)
3097 {
3098 struct mlxsw_sp_ipv6_addr_node *node;
3099
3100 mutex_lock(&mlxsw_sp->ipv6_addr_ht_lock);
3101 node = rhashtable_lookup_fast(&mlxsw_sp->ipv6_addr_ht, addr6,
3102 mlxsw_sp_ipv6_addr_ht_params);
3103 if (WARN_ON(!node))
3104 goto out_unlock;
3105
3106 if (!refcount_dec_and_test(&node->refcount))
3107 goto out_unlock;
3108
3109 mlxsw_sp_ipv6_addr_fini(mlxsw_sp, node);
3110
3111 out_unlock:
3112 mutex_unlock(&mlxsw_sp->ipv6_addr_ht_lock);
3113 }
3114
mlxsw_sp_ipv6_addr_ht_init(struct mlxsw_sp * mlxsw_sp)3115 static int mlxsw_sp_ipv6_addr_ht_init(struct mlxsw_sp *mlxsw_sp)
3116 {
3117 int err;
3118
3119 err = rhashtable_init(&mlxsw_sp->ipv6_addr_ht,
3120 &mlxsw_sp_ipv6_addr_ht_params);
3121 if (err)
3122 return err;
3123
3124 mutex_init(&mlxsw_sp->ipv6_addr_ht_lock);
3125 return 0;
3126 }
3127
mlxsw_sp_ipv6_addr_ht_fini(struct mlxsw_sp * mlxsw_sp)3128 static void mlxsw_sp_ipv6_addr_ht_fini(struct mlxsw_sp *mlxsw_sp)
3129 {
3130 mutex_destroy(&mlxsw_sp->ipv6_addr_ht_lock);
3131 rhashtable_destroy(&mlxsw_sp->ipv6_addr_ht);
3132 }
3133
mlxsw_sp_init(struct mlxsw_core * mlxsw_core,const struct mlxsw_bus_info * mlxsw_bus_info,struct netlink_ext_ack * extack)3134 static int mlxsw_sp_init(struct mlxsw_core *mlxsw_core,
3135 const struct mlxsw_bus_info *mlxsw_bus_info,
3136 struct netlink_ext_ack *extack)
3137 {
3138 struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
3139 int err;
3140
3141 mlxsw_sp->core = mlxsw_core;
3142 mlxsw_sp->bus_info = mlxsw_bus_info;
3143
3144 mlxsw_sp_parsing_init(mlxsw_sp);
3145
3146 err = mlxsw_sp_base_mac_get(mlxsw_sp);
3147 if (err) {
3148 dev_err(mlxsw_sp->bus_info->dev, "Failed to get base mac\n");
3149 return err;
3150 }
3151
3152 err = mlxsw_sp_kvdl_init(mlxsw_sp);
3153 if (err) {
3154 dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize KVDL\n");
3155 return err;
3156 }
3157
3158 err = mlxsw_sp_pgt_init(mlxsw_sp);
3159 if (err) {
3160 dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize PGT\n");
3161 goto err_pgt_init;
3162 }
3163
3164 /* Initialize before FIDs so that the LAG table is at the start of PGT
3165 * and 8-aligned without overallocation.
3166 */
3167 err = mlxsw_sp_lag_init(mlxsw_sp);
3168 if (err) {
3169 dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize LAG\n");
3170 goto err_lag_init;
3171 }
3172
3173 err = mlxsw_sp->fid_core_ops->init(mlxsw_sp);
3174 if (err) {
3175 dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize FIDs\n");
3176 goto err_fid_core_init;
3177 }
3178
3179 err = mlxsw_sp_policers_init(mlxsw_sp);
3180 if (err) {
3181 dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize policers\n");
3182 goto err_policers_init;
3183 }
3184
3185 err = mlxsw_sp_traps_init(mlxsw_sp);
3186 if (err) {
3187 dev_err(mlxsw_sp->bus_info->dev, "Failed to set traps\n");
3188 goto err_traps_init;
3189 }
3190
3191 err = mlxsw_sp_devlink_traps_init(mlxsw_sp);
3192 if (err) {
3193 dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize devlink traps\n");
3194 goto err_devlink_traps_init;
3195 }
3196
3197 err = mlxsw_sp_buffers_init(mlxsw_sp);
3198 if (err) {
3199 dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize buffers\n");
3200 goto err_buffers_init;
3201 }
3202
3203 /* Initialize SPAN before router and switchdev, so that those components
3204 * can call mlxsw_sp_span_respin().
3205 */
3206 err = mlxsw_sp_span_init(mlxsw_sp);
3207 if (err) {
3208 dev_err(mlxsw_sp->bus_info->dev, "Failed to init span system\n");
3209 goto err_span_init;
3210 }
3211
3212 err = mlxsw_sp_switchdev_init(mlxsw_sp);
3213 if (err) {
3214 dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize switchdev\n");
3215 goto err_switchdev_init;
3216 }
3217
3218 err = mlxsw_sp_counter_pool_init(mlxsw_sp);
3219 if (err) {
3220 dev_err(mlxsw_sp->bus_info->dev, "Failed to init counter pool\n");
3221 goto err_counter_pool_init;
3222 }
3223
3224 err = mlxsw_sp_afa_init(mlxsw_sp);
3225 if (err) {
3226 dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize ACL actions\n");
3227 goto err_afa_init;
3228 }
3229
3230 err = mlxsw_sp_ipv6_addr_ht_init(mlxsw_sp);
3231 if (err) {
3232 dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize hash table for IPv6 addresses\n");
3233 goto err_ipv6_addr_ht_init;
3234 }
3235
3236 err = mlxsw_sp_nve_init(mlxsw_sp);
3237 if (err) {
3238 dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize NVE\n");
3239 goto err_nve_init;
3240 }
3241
3242 err = mlxsw_sp_port_range_init(mlxsw_sp);
3243 if (err) {
3244 dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize port ranges\n");
3245 goto err_port_range_init;
3246 }
3247
3248 err = mlxsw_sp_acl_init(mlxsw_sp);
3249 if (err) {
3250 dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize ACL\n");
3251 goto err_acl_init;
3252 }
3253
3254 err = mlxsw_sp_router_init(mlxsw_sp, extack);
3255 if (err) {
3256 dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize router\n");
3257 goto err_router_init;
3258 }
3259
3260 if (mlxsw_sp->bus_info->read_clock_capable) {
3261 /* NULL is a valid return value from clock_init */
3262 mlxsw_sp->clock =
3263 mlxsw_sp->ptp_ops->clock_init(mlxsw_sp,
3264 mlxsw_sp->bus_info->dev);
3265 if (IS_ERR(mlxsw_sp->clock)) {
3266 err = PTR_ERR(mlxsw_sp->clock);
3267 dev_err(mlxsw_sp->bus_info->dev, "Failed to init ptp clock\n");
3268 goto err_ptp_clock_init;
3269 }
3270 }
3271
3272 if (mlxsw_sp->clock) {
3273 /* NULL is a valid return value from ptp_ops->init */
3274 mlxsw_sp->ptp_state = mlxsw_sp->ptp_ops->init(mlxsw_sp);
3275 if (IS_ERR(mlxsw_sp->ptp_state)) {
3276 err = PTR_ERR(mlxsw_sp->ptp_state);
3277 dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize PTP\n");
3278 goto err_ptp_init;
3279 }
3280 }
3281
3282 /* Initialize netdevice notifier after SPAN is initialized, so that the
3283 * event handler can call SPAN respin.
3284 */
3285 mlxsw_sp->netdevice_nb.notifier_call = mlxsw_sp_netdevice_event;
3286 err = register_netdevice_notifier_net(mlxsw_sp_net(mlxsw_sp),
3287 &mlxsw_sp->netdevice_nb);
3288 if (err) {
3289 dev_err(mlxsw_sp->bus_info->dev, "Failed to register netdev notifier\n");
3290 goto err_netdev_notifier;
3291 }
3292
3293 err = mlxsw_sp_dpipe_init(mlxsw_sp);
3294 if (err) {
3295 dev_err(mlxsw_sp->bus_info->dev, "Failed to init pipeline debug\n");
3296 goto err_dpipe_init;
3297 }
3298
3299 err = mlxsw_sp_port_module_info_init(mlxsw_sp);
3300 if (err) {
3301 dev_err(mlxsw_sp->bus_info->dev, "Failed to init port module info\n");
3302 goto err_port_module_info_init;
3303 }
3304
3305 err = rhashtable_init(&mlxsw_sp->sample_trigger_ht,
3306 &mlxsw_sp_sample_trigger_ht_params);
3307 if (err) {
3308 dev_err(mlxsw_sp->bus_info->dev, "Failed to init sampling trigger hashtable\n");
3309 goto err_sample_trigger_init;
3310 }
3311
3312 err = mlxsw_sp_ports_create(mlxsw_sp);
3313 if (err) {
3314 dev_err(mlxsw_sp->bus_info->dev, "Failed to create ports\n");
3315 goto err_ports_create;
3316 }
3317
3318 return 0;
3319
3320 err_ports_create:
3321 rhashtable_destroy(&mlxsw_sp->sample_trigger_ht);
3322 err_sample_trigger_init:
3323 mlxsw_sp_port_module_info_fini(mlxsw_sp);
3324 err_port_module_info_init:
3325 mlxsw_sp_dpipe_fini(mlxsw_sp);
3326 err_dpipe_init:
3327 unregister_netdevice_notifier_net(mlxsw_sp_net(mlxsw_sp),
3328 &mlxsw_sp->netdevice_nb);
3329 err_netdev_notifier:
3330 if (mlxsw_sp->clock)
3331 mlxsw_sp->ptp_ops->fini(mlxsw_sp->ptp_state);
3332 err_ptp_init:
3333 if (mlxsw_sp->clock)
3334 mlxsw_sp->ptp_ops->clock_fini(mlxsw_sp->clock);
3335 err_ptp_clock_init:
3336 mlxsw_sp_router_fini(mlxsw_sp);
3337 err_router_init:
3338 mlxsw_sp_acl_fini(mlxsw_sp);
3339 err_acl_init:
3340 mlxsw_sp_port_range_fini(mlxsw_sp);
3341 err_port_range_init:
3342 mlxsw_sp_nve_fini(mlxsw_sp);
3343 err_nve_init:
3344 mlxsw_sp_ipv6_addr_ht_fini(mlxsw_sp);
3345 err_ipv6_addr_ht_init:
3346 mlxsw_sp_afa_fini(mlxsw_sp);
3347 err_afa_init:
3348 mlxsw_sp_counter_pool_fini(mlxsw_sp);
3349 err_counter_pool_init:
3350 mlxsw_sp_switchdev_fini(mlxsw_sp);
3351 err_switchdev_init:
3352 mlxsw_sp_span_fini(mlxsw_sp);
3353 err_span_init:
3354 mlxsw_sp_buffers_fini(mlxsw_sp);
3355 err_buffers_init:
3356 mlxsw_sp_devlink_traps_fini(mlxsw_sp);
3357 err_devlink_traps_init:
3358 mlxsw_sp_traps_fini(mlxsw_sp);
3359 err_traps_init:
3360 mlxsw_sp_policers_fini(mlxsw_sp);
3361 err_policers_init:
3362 mlxsw_sp->fid_core_ops->fini(mlxsw_sp);
3363 err_fid_core_init:
3364 mlxsw_sp_lag_fini(mlxsw_sp);
3365 err_lag_init:
3366 mlxsw_sp_pgt_fini(mlxsw_sp);
3367 err_pgt_init:
3368 mlxsw_sp_kvdl_fini(mlxsw_sp);
3369 mlxsw_sp_parsing_fini(mlxsw_sp);
3370 return err;
3371 }
3372
mlxsw_sp1_init(struct mlxsw_core * mlxsw_core,const struct mlxsw_bus_info * mlxsw_bus_info,struct netlink_ext_ack * extack)3373 static int mlxsw_sp1_init(struct mlxsw_core *mlxsw_core,
3374 const struct mlxsw_bus_info *mlxsw_bus_info,
3375 struct netlink_ext_ack *extack)
3376 {
3377 struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
3378
3379 mlxsw_sp->switchdev_ops = &mlxsw_sp1_switchdev_ops;
3380 mlxsw_sp->kvdl_ops = &mlxsw_sp1_kvdl_ops;
3381 mlxsw_sp->afa_ops = &mlxsw_sp1_act_afa_ops;
3382 mlxsw_sp->afk_ops = &mlxsw_sp1_afk_ops;
3383 mlxsw_sp->mr_tcam_ops = &mlxsw_sp1_mr_tcam_ops;
3384 mlxsw_sp->acl_rulei_ops = &mlxsw_sp1_acl_rulei_ops;
3385 mlxsw_sp->acl_tcam_ops = &mlxsw_sp1_acl_tcam_ops;
3386 mlxsw_sp->nve_ops_arr = mlxsw_sp1_nve_ops_arr;
3387 mlxsw_sp->mac_mask = mlxsw_sp1_mac_mask;
3388 mlxsw_sp->sb_vals = &mlxsw_sp1_sb_vals;
3389 mlxsw_sp->sb_ops = &mlxsw_sp1_sb_ops;
3390 mlxsw_sp->port_type_speed_ops = &mlxsw_sp1_port_type_speed_ops;
3391 mlxsw_sp->ptp_ops = &mlxsw_sp1_ptp_ops;
3392 mlxsw_sp->span_ops = &mlxsw_sp1_span_ops;
3393 mlxsw_sp->policer_core_ops = &mlxsw_sp1_policer_core_ops;
3394 mlxsw_sp->trap_ops = &mlxsw_sp1_trap_ops;
3395 mlxsw_sp->mall_ops = &mlxsw_sp1_mall_ops;
3396 mlxsw_sp->router_ops = &mlxsw_sp1_router_ops;
3397 mlxsw_sp->listeners = mlxsw_sp1_listener;
3398 mlxsw_sp->listeners_count = ARRAY_SIZE(mlxsw_sp1_listener);
3399 mlxsw_sp->fid_core_ops = &mlxsw_sp1_fid_core_ops;
3400 mlxsw_sp->lowest_shaper_bs = MLXSW_REG_QEEC_LOWEST_SHAPER_BS_SP1;
3401 mlxsw_sp->pgt_smpe_index_valid = true;
3402
3403 return mlxsw_sp_init(mlxsw_core, mlxsw_bus_info, extack);
3404 }
3405
mlxsw_sp2_init(struct mlxsw_core * mlxsw_core,const struct mlxsw_bus_info * mlxsw_bus_info,struct netlink_ext_ack * extack)3406 static int mlxsw_sp2_init(struct mlxsw_core *mlxsw_core,
3407 const struct mlxsw_bus_info *mlxsw_bus_info,
3408 struct netlink_ext_ack *extack)
3409 {
3410 struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
3411
3412 mlxsw_sp->switchdev_ops = &mlxsw_sp2_switchdev_ops;
3413 mlxsw_sp->kvdl_ops = &mlxsw_sp2_kvdl_ops;
3414 mlxsw_sp->afa_ops = &mlxsw_sp2_act_afa_ops;
3415 mlxsw_sp->afk_ops = &mlxsw_sp2_afk_ops;
3416 mlxsw_sp->mr_tcam_ops = &mlxsw_sp2_mr_tcam_ops;
3417 mlxsw_sp->acl_rulei_ops = &mlxsw_sp2_acl_rulei_ops;
3418 mlxsw_sp->acl_tcam_ops = &mlxsw_sp2_acl_tcam_ops;
3419 mlxsw_sp->acl_bf_ops = &mlxsw_sp2_acl_bf_ops;
3420 mlxsw_sp->nve_ops_arr = mlxsw_sp2_nve_ops_arr;
3421 mlxsw_sp->mac_mask = mlxsw_sp2_mac_mask;
3422 mlxsw_sp->sb_vals = &mlxsw_sp2_sb_vals;
3423 mlxsw_sp->sb_ops = &mlxsw_sp2_sb_ops;
3424 mlxsw_sp->port_type_speed_ops = &mlxsw_sp2_port_type_speed_ops;
3425 mlxsw_sp->ptp_ops = &mlxsw_sp2_ptp_ops;
3426 mlxsw_sp->span_ops = &mlxsw_sp2_span_ops;
3427 mlxsw_sp->policer_core_ops = &mlxsw_sp2_policer_core_ops;
3428 mlxsw_sp->trap_ops = &mlxsw_sp2_trap_ops;
3429 mlxsw_sp->mall_ops = &mlxsw_sp2_mall_ops;
3430 mlxsw_sp->router_ops = &mlxsw_sp2_router_ops;
3431 mlxsw_sp->listeners = mlxsw_sp2_listener;
3432 mlxsw_sp->listeners_count = ARRAY_SIZE(mlxsw_sp2_listener);
3433 mlxsw_sp->fid_core_ops = &mlxsw_sp2_fid_core_ops;
3434 mlxsw_sp->lowest_shaper_bs = MLXSW_REG_QEEC_LOWEST_SHAPER_BS_SP2;
3435 mlxsw_sp->pgt_smpe_index_valid = false;
3436
3437 return mlxsw_sp_init(mlxsw_core, mlxsw_bus_info, extack);
3438 }
3439
mlxsw_sp3_init(struct mlxsw_core * mlxsw_core,const struct mlxsw_bus_info * mlxsw_bus_info,struct netlink_ext_ack * extack)3440 static int mlxsw_sp3_init(struct mlxsw_core *mlxsw_core,
3441 const struct mlxsw_bus_info *mlxsw_bus_info,
3442 struct netlink_ext_ack *extack)
3443 {
3444 struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
3445
3446 mlxsw_sp->switchdev_ops = &mlxsw_sp2_switchdev_ops;
3447 mlxsw_sp->kvdl_ops = &mlxsw_sp2_kvdl_ops;
3448 mlxsw_sp->afa_ops = &mlxsw_sp2_act_afa_ops;
3449 mlxsw_sp->afk_ops = &mlxsw_sp2_afk_ops;
3450 mlxsw_sp->mr_tcam_ops = &mlxsw_sp2_mr_tcam_ops;
3451 mlxsw_sp->acl_rulei_ops = &mlxsw_sp2_acl_rulei_ops;
3452 mlxsw_sp->acl_tcam_ops = &mlxsw_sp2_acl_tcam_ops;
3453 mlxsw_sp->acl_bf_ops = &mlxsw_sp2_acl_bf_ops;
3454 mlxsw_sp->nve_ops_arr = mlxsw_sp2_nve_ops_arr;
3455 mlxsw_sp->mac_mask = mlxsw_sp2_mac_mask;
3456 mlxsw_sp->sb_vals = &mlxsw_sp2_sb_vals;
3457 mlxsw_sp->sb_ops = &mlxsw_sp3_sb_ops;
3458 mlxsw_sp->port_type_speed_ops = &mlxsw_sp2_port_type_speed_ops;
3459 mlxsw_sp->ptp_ops = &mlxsw_sp2_ptp_ops;
3460 mlxsw_sp->span_ops = &mlxsw_sp3_span_ops;
3461 mlxsw_sp->policer_core_ops = &mlxsw_sp2_policer_core_ops;
3462 mlxsw_sp->trap_ops = &mlxsw_sp2_trap_ops;
3463 mlxsw_sp->mall_ops = &mlxsw_sp2_mall_ops;
3464 mlxsw_sp->router_ops = &mlxsw_sp2_router_ops;
3465 mlxsw_sp->listeners = mlxsw_sp2_listener;
3466 mlxsw_sp->listeners_count = ARRAY_SIZE(mlxsw_sp2_listener);
3467 mlxsw_sp->fid_core_ops = &mlxsw_sp2_fid_core_ops;
3468 mlxsw_sp->lowest_shaper_bs = MLXSW_REG_QEEC_LOWEST_SHAPER_BS_SP3;
3469 mlxsw_sp->pgt_smpe_index_valid = false;
3470
3471 return mlxsw_sp_init(mlxsw_core, mlxsw_bus_info, extack);
3472 }
3473
mlxsw_sp4_init(struct mlxsw_core * mlxsw_core,const struct mlxsw_bus_info * mlxsw_bus_info,struct netlink_ext_ack * extack)3474 static int mlxsw_sp4_init(struct mlxsw_core *mlxsw_core,
3475 const struct mlxsw_bus_info *mlxsw_bus_info,
3476 struct netlink_ext_ack *extack)
3477 {
3478 struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
3479
3480 mlxsw_sp->switchdev_ops = &mlxsw_sp2_switchdev_ops;
3481 mlxsw_sp->kvdl_ops = &mlxsw_sp2_kvdl_ops;
3482 mlxsw_sp->afa_ops = &mlxsw_sp2_act_afa_ops;
3483 mlxsw_sp->afk_ops = &mlxsw_sp4_afk_ops;
3484 mlxsw_sp->mr_tcam_ops = &mlxsw_sp2_mr_tcam_ops;
3485 mlxsw_sp->acl_rulei_ops = &mlxsw_sp2_acl_rulei_ops;
3486 mlxsw_sp->acl_tcam_ops = &mlxsw_sp2_acl_tcam_ops;
3487 mlxsw_sp->acl_bf_ops = &mlxsw_sp4_acl_bf_ops;
3488 mlxsw_sp->nve_ops_arr = mlxsw_sp2_nve_ops_arr;
3489 mlxsw_sp->mac_mask = mlxsw_sp2_mac_mask;
3490 mlxsw_sp->sb_vals = &mlxsw_sp2_sb_vals;
3491 mlxsw_sp->sb_ops = &mlxsw_sp3_sb_ops;
3492 mlxsw_sp->port_type_speed_ops = &mlxsw_sp2_port_type_speed_ops;
3493 mlxsw_sp->ptp_ops = &mlxsw_sp4_ptp_ops;
3494 mlxsw_sp->span_ops = &mlxsw_sp3_span_ops;
3495 mlxsw_sp->policer_core_ops = &mlxsw_sp2_policer_core_ops;
3496 mlxsw_sp->trap_ops = &mlxsw_sp2_trap_ops;
3497 mlxsw_sp->mall_ops = &mlxsw_sp2_mall_ops;
3498 mlxsw_sp->router_ops = &mlxsw_sp2_router_ops;
3499 mlxsw_sp->listeners = mlxsw_sp2_listener;
3500 mlxsw_sp->listeners_count = ARRAY_SIZE(mlxsw_sp2_listener);
3501 mlxsw_sp->fid_core_ops = &mlxsw_sp2_fid_core_ops;
3502 mlxsw_sp->lowest_shaper_bs = MLXSW_REG_QEEC_LOWEST_SHAPER_BS_SP4;
3503 mlxsw_sp->pgt_smpe_index_valid = false;
3504
3505 return mlxsw_sp_init(mlxsw_core, mlxsw_bus_info, extack);
3506 }
3507
mlxsw_sp_fini(struct mlxsw_core * mlxsw_core)3508 static void mlxsw_sp_fini(struct mlxsw_core *mlxsw_core)
3509 {
3510 struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
3511
3512 mlxsw_sp_ports_remove(mlxsw_sp);
3513 rhashtable_destroy(&mlxsw_sp->sample_trigger_ht);
3514 mlxsw_sp_port_module_info_fini(mlxsw_sp);
3515 mlxsw_sp_dpipe_fini(mlxsw_sp);
3516 unregister_netdevice_notifier_net(mlxsw_sp_net(mlxsw_sp),
3517 &mlxsw_sp->netdevice_nb);
3518 if (mlxsw_sp->clock) {
3519 mlxsw_sp->ptp_ops->fini(mlxsw_sp->ptp_state);
3520 mlxsw_sp->ptp_ops->clock_fini(mlxsw_sp->clock);
3521 }
3522 mlxsw_sp_router_fini(mlxsw_sp);
3523 mlxsw_sp_acl_fini(mlxsw_sp);
3524 mlxsw_sp_port_range_fini(mlxsw_sp);
3525 mlxsw_sp_nve_fini(mlxsw_sp);
3526 mlxsw_sp_ipv6_addr_ht_fini(mlxsw_sp);
3527 mlxsw_sp_afa_fini(mlxsw_sp);
3528 mlxsw_sp_counter_pool_fini(mlxsw_sp);
3529 mlxsw_sp_switchdev_fini(mlxsw_sp);
3530 mlxsw_sp_span_fini(mlxsw_sp);
3531 mlxsw_sp_buffers_fini(mlxsw_sp);
3532 mlxsw_sp_devlink_traps_fini(mlxsw_sp);
3533 mlxsw_sp_traps_fini(mlxsw_sp);
3534 mlxsw_sp_policers_fini(mlxsw_sp);
3535 mlxsw_sp->fid_core_ops->fini(mlxsw_sp);
3536 mlxsw_sp_lag_fini(mlxsw_sp);
3537 mlxsw_sp_pgt_fini(mlxsw_sp);
3538 mlxsw_sp_kvdl_fini(mlxsw_sp);
3539 mlxsw_sp_parsing_fini(mlxsw_sp);
3540 }
3541
3542 static const struct mlxsw_config_profile mlxsw_sp1_config_profile = {
3543 .used_flood_mode = 1,
3544 .flood_mode = MLXSW_CMD_MBOX_CONFIG_PROFILE_FLOOD_MODE_CONTROLLED,
3545 .used_max_ib_mc = 1,
3546 .max_ib_mc = 0,
3547 .used_max_pkey = 1,
3548 .max_pkey = 0,
3549 .used_ubridge = 1,
3550 .ubridge = 1,
3551 .used_kvd_sizes = 1,
3552 .kvd_hash_single_parts = 59,
3553 .kvd_hash_double_parts = 41,
3554 .kvd_linear_size = MLXSW_SP_KVD_LINEAR_SIZE,
3555 .swid_config = {
3556 {
3557 .used_type = 1,
3558 .type = MLXSW_PORT_SWID_TYPE_ETH,
3559 }
3560 },
3561 };
3562
3563 static const struct mlxsw_config_profile mlxsw_sp2_config_profile = {
3564 .used_flood_mode = 1,
3565 .flood_mode = MLXSW_CMD_MBOX_CONFIG_PROFILE_FLOOD_MODE_CONTROLLED,
3566 .used_max_ib_mc = 1,
3567 .max_ib_mc = 0,
3568 .used_max_pkey = 1,
3569 .max_pkey = 0,
3570 .used_ubridge = 1,
3571 .ubridge = 1,
3572 .swid_config = {
3573 {
3574 .used_type = 1,
3575 .type = MLXSW_PORT_SWID_TYPE_ETH,
3576 }
3577 },
3578 .used_cqe_time_stamp_type = 1,
3579 .cqe_time_stamp_type = MLXSW_CMD_MBOX_CONFIG_PROFILE_CQE_TIME_STAMP_TYPE_UTC,
3580 .lag_mode_prefer_sw = true,
3581 .flood_mode_prefer_cff = true,
3582 };
3583
3584 /* Reduce number of LAGs from full capacity (256) to the maximum supported LAGs
3585 * in Spectrum-2/3, to avoid regression in number of free entries in the PGT
3586 * table.
3587 */
3588 #define MLXSW_SP4_CONFIG_PROFILE_MAX_LAG 128
3589
3590 static const struct mlxsw_config_profile mlxsw_sp4_config_profile = {
3591 .used_max_lag = 1,
3592 .max_lag = MLXSW_SP4_CONFIG_PROFILE_MAX_LAG,
3593 .used_flood_mode = 1,
3594 .flood_mode = MLXSW_CMD_MBOX_CONFIG_PROFILE_FLOOD_MODE_CONTROLLED,
3595 .used_max_ib_mc = 1,
3596 .max_ib_mc = 0,
3597 .used_max_pkey = 1,
3598 .max_pkey = 0,
3599 .used_ubridge = 1,
3600 .ubridge = 1,
3601 .swid_config = {
3602 {
3603 .used_type = 1,
3604 .type = MLXSW_PORT_SWID_TYPE_ETH,
3605 }
3606 },
3607 .used_cqe_time_stamp_type = 1,
3608 .cqe_time_stamp_type = MLXSW_CMD_MBOX_CONFIG_PROFILE_CQE_TIME_STAMP_TYPE_UTC,
3609 .lag_mode_prefer_sw = true,
3610 .flood_mode_prefer_cff = true,
3611 };
3612
3613 static void
mlxsw_sp_resource_size_params_prepare(struct mlxsw_core * mlxsw_core,struct devlink_resource_size_params * kvd_size_params,struct devlink_resource_size_params * linear_size_params,struct devlink_resource_size_params * hash_double_size_params,struct devlink_resource_size_params * hash_single_size_params)3614 mlxsw_sp_resource_size_params_prepare(struct mlxsw_core *mlxsw_core,
3615 struct devlink_resource_size_params *kvd_size_params,
3616 struct devlink_resource_size_params *linear_size_params,
3617 struct devlink_resource_size_params *hash_double_size_params,
3618 struct devlink_resource_size_params *hash_single_size_params)
3619 {
3620 u32 single_size_min = MLXSW_CORE_RES_GET(mlxsw_core,
3621 KVD_SINGLE_MIN_SIZE);
3622 u32 double_size_min = MLXSW_CORE_RES_GET(mlxsw_core,
3623 KVD_DOUBLE_MIN_SIZE);
3624 u32 kvd_size = MLXSW_CORE_RES_GET(mlxsw_core, KVD_SIZE);
3625 u32 linear_size_min = 0;
3626
3627 devlink_resource_size_params_init(kvd_size_params, kvd_size, kvd_size,
3628 MLXSW_SP_KVD_GRANULARITY,
3629 DEVLINK_RESOURCE_UNIT_ENTRY);
3630 devlink_resource_size_params_init(linear_size_params, linear_size_min,
3631 kvd_size - single_size_min -
3632 double_size_min,
3633 MLXSW_SP_KVD_GRANULARITY,
3634 DEVLINK_RESOURCE_UNIT_ENTRY);
3635 devlink_resource_size_params_init(hash_double_size_params,
3636 double_size_min,
3637 kvd_size - single_size_min -
3638 linear_size_min,
3639 MLXSW_SP_KVD_GRANULARITY,
3640 DEVLINK_RESOURCE_UNIT_ENTRY);
3641 devlink_resource_size_params_init(hash_single_size_params,
3642 single_size_min,
3643 kvd_size - double_size_min -
3644 linear_size_min,
3645 MLXSW_SP_KVD_GRANULARITY,
3646 DEVLINK_RESOURCE_UNIT_ENTRY);
3647 }
3648
mlxsw_sp1_resources_kvd_register(struct mlxsw_core * mlxsw_core)3649 static int mlxsw_sp1_resources_kvd_register(struct mlxsw_core *mlxsw_core)
3650 {
3651 struct devlink *devlink = priv_to_devlink(mlxsw_core);
3652 struct devlink_resource_size_params hash_single_size_params;
3653 struct devlink_resource_size_params hash_double_size_params;
3654 struct devlink_resource_size_params linear_size_params;
3655 struct devlink_resource_size_params kvd_size_params;
3656 u32 kvd_size, single_size, double_size, linear_size;
3657 const struct mlxsw_config_profile *profile;
3658 int err;
3659
3660 profile = &mlxsw_sp1_config_profile;
3661 if (!MLXSW_CORE_RES_VALID(mlxsw_core, KVD_SIZE))
3662 return -EIO;
3663
3664 mlxsw_sp_resource_size_params_prepare(mlxsw_core, &kvd_size_params,
3665 &linear_size_params,
3666 &hash_double_size_params,
3667 &hash_single_size_params);
3668
3669 kvd_size = MLXSW_CORE_RES_GET(mlxsw_core, KVD_SIZE);
3670 err = devl_resource_register(devlink, MLXSW_SP_RESOURCE_NAME_KVD,
3671 kvd_size, MLXSW_SP_RESOURCE_KVD,
3672 DEVLINK_RESOURCE_ID_PARENT_TOP,
3673 &kvd_size_params);
3674 if (err)
3675 return err;
3676
3677 linear_size = profile->kvd_linear_size;
3678 err = devl_resource_register(devlink, MLXSW_SP_RESOURCE_NAME_KVD_LINEAR,
3679 linear_size,
3680 MLXSW_SP_RESOURCE_KVD_LINEAR,
3681 MLXSW_SP_RESOURCE_KVD,
3682 &linear_size_params);
3683 if (err)
3684 return err;
3685
3686 err = mlxsw_sp1_kvdl_resources_register(mlxsw_core);
3687 if (err)
3688 return err;
3689
3690 double_size = kvd_size - linear_size;
3691 double_size *= profile->kvd_hash_double_parts;
3692 double_size /= profile->kvd_hash_double_parts +
3693 profile->kvd_hash_single_parts;
3694 double_size = rounddown(double_size, MLXSW_SP_KVD_GRANULARITY);
3695 err = devl_resource_register(devlink, MLXSW_SP_RESOURCE_NAME_KVD_HASH_DOUBLE,
3696 double_size,
3697 MLXSW_SP_RESOURCE_KVD_HASH_DOUBLE,
3698 MLXSW_SP_RESOURCE_KVD,
3699 &hash_double_size_params);
3700 if (err)
3701 return err;
3702
3703 single_size = kvd_size - double_size - linear_size;
3704 err = devl_resource_register(devlink, MLXSW_SP_RESOURCE_NAME_KVD_HASH_SINGLE,
3705 single_size,
3706 MLXSW_SP_RESOURCE_KVD_HASH_SINGLE,
3707 MLXSW_SP_RESOURCE_KVD,
3708 &hash_single_size_params);
3709 if (err)
3710 return err;
3711
3712 return 0;
3713 }
3714
mlxsw_sp2_resources_kvd_register(struct mlxsw_core * mlxsw_core)3715 static int mlxsw_sp2_resources_kvd_register(struct mlxsw_core *mlxsw_core)
3716 {
3717 struct devlink *devlink = priv_to_devlink(mlxsw_core);
3718 struct devlink_resource_size_params kvd_size_params;
3719 u32 kvd_size;
3720
3721 if (!MLXSW_CORE_RES_VALID(mlxsw_core, KVD_SIZE))
3722 return -EIO;
3723
3724 kvd_size = MLXSW_CORE_RES_GET(mlxsw_core, KVD_SIZE);
3725 devlink_resource_size_params_init(&kvd_size_params, kvd_size, kvd_size,
3726 MLXSW_SP_KVD_GRANULARITY,
3727 DEVLINK_RESOURCE_UNIT_ENTRY);
3728
3729 return devl_resource_register(devlink, MLXSW_SP_RESOURCE_NAME_KVD,
3730 kvd_size, MLXSW_SP_RESOURCE_KVD,
3731 DEVLINK_RESOURCE_ID_PARENT_TOP,
3732 &kvd_size_params);
3733 }
3734
mlxsw_sp_resources_span_register(struct mlxsw_core * mlxsw_core)3735 static int mlxsw_sp_resources_span_register(struct mlxsw_core *mlxsw_core)
3736 {
3737 struct devlink *devlink = priv_to_devlink(mlxsw_core);
3738 struct devlink_resource_size_params span_size_params;
3739 u32 max_span;
3740
3741 if (!MLXSW_CORE_RES_VALID(mlxsw_core, MAX_SPAN))
3742 return -EIO;
3743
3744 max_span = MLXSW_CORE_RES_GET(mlxsw_core, MAX_SPAN);
3745 devlink_resource_size_params_init(&span_size_params, max_span, max_span,
3746 1, DEVLINK_RESOURCE_UNIT_ENTRY);
3747
3748 return devl_resource_register(devlink, MLXSW_SP_RESOURCE_NAME_SPAN,
3749 max_span, MLXSW_SP_RESOURCE_SPAN,
3750 DEVLINK_RESOURCE_ID_PARENT_TOP,
3751 &span_size_params);
3752 }
3753
3754 static int
mlxsw_sp_resources_rif_mac_profile_register(struct mlxsw_core * mlxsw_core)3755 mlxsw_sp_resources_rif_mac_profile_register(struct mlxsw_core *mlxsw_core)
3756 {
3757 struct devlink *devlink = priv_to_devlink(mlxsw_core);
3758 struct devlink_resource_size_params size_params;
3759 u8 max_rif_mac_profiles;
3760
3761 if (!MLXSW_CORE_RES_VALID(mlxsw_core, MAX_RIF_MAC_PROFILES))
3762 max_rif_mac_profiles = 1;
3763 else
3764 max_rif_mac_profiles = MLXSW_CORE_RES_GET(mlxsw_core,
3765 MAX_RIF_MAC_PROFILES);
3766 devlink_resource_size_params_init(&size_params, max_rif_mac_profiles,
3767 max_rif_mac_profiles, 1,
3768 DEVLINK_RESOURCE_UNIT_ENTRY);
3769
3770 return devl_resource_register(devlink,
3771 "rif_mac_profiles",
3772 max_rif_mac_profiles,
3773 MLXSW_SP_RESOURCE_RIF_MAC_PROFILES,
3774 DEVLINK_RESOURCE_ID_PARENT_TOP,
3775 &size_params);
3776 }
3777
mlxsw_sp_resources_rifs_register(struct mlxsw_core * mlxsw_core)3778 static int mlxsw_sp_resources_rifs_register(struct mlxsw_core *mlxsw_core)
3779 {
3780 struct devlink *devlink = priv_to_devlink(mlxsw_core);
3781 struct devlink_resource_size_params size_params;
3782 u64 max_rifs;
3783
3784 if (!MLXSW_CORE_RES_VALID(mlxsw_core, MAX_RIFS))
3785 return -EIO;
3786
3787 max_rifs = MLXSW_CORE_RES_GET(mlxsw_core, MAX_RIFS);
3788 devlink_resource_size_params_init(&size_params, max_rifs, max_rifs,
3789 1, DEVLINK_RESOURCE_UNIT_ENTRY);
3790
3791 return devl_resource_register(devlink, "rifs", max_rifs,
3792 MLXSW_SP_RESOURCE_RIFS,
3793 DEVLINK_RESOURCE_ID_PARENT_TOP,
3794 &size_params);
3795 }
3796
3797 static int
mlxsw_sp_resources_port_range_register(struct mlxsw_core * mlxsw_core)3798 mlxsw_sp_resources_port_range_register(struct mlxsw_core *mlxsw_core)
3799 {
3800 struct devlink *devlink = priv_to_devlink(mlxsw_core);
3801 struct devlink_resource_size_params size_params;
3802 u64 max;
3803
3804 if (!MLXSW_CORE_RES_VALID(mlxsw_core, ACL_MAX_L4_PORT_RANGE))
3805 return -EIO;
3806
3807 max = MLXSW_CORE_RES_GET(mlxsw_core, ACL_MAX_L4_PORT_RANGE);
3808 devlink_resource_size_params_init(&size_params, max, max, 1,
3809 DEVLINK_RESOURCE_UNIT_ENTRY);
3810
3811 return devl_resource_register(devlink, "port_range_registers", max,
3812 MLXSW_SP_RESOURCE_PORT_RANGE_REGISTERS,
3813 DEVLINK_RESOURCE_ID_PARENT_TOP,
3814 &size_params);
3815 }
3816
mlxsw_sp1_resources_register(struct mlxsw_core * mlxsw_core)3817 static int mlxsw_sp1_resources_register(struct mlxsw_core *mlxsw_core)
3818 {
3819 int err;
3820
3821 err = mlxsw_sp1_resources_kvd_register(mlxsw_core);
3822 if (err)
3823 return err;
3824
3825 err = mlxsw_sp_resources_span_register(mlxsw_core);
3826 if (err)
3827 goto err_resources_span_register;
3828
3829 err = mlxsw_sp_counter_resources_register(mlxsw_core);
3830 if (err)
3831 goto err_resources_counter_register;
3832
3833 err = mlxsw_sp_policer_resources_register(mlxsw_core);
3834 if (err)
3835 goto err_policer_resources_register;
3836
3837 err = mlxsw_sp_resources_rif_mac_profile_register(mlxsw_core);
3838 if (err)
3839 goto err_resources_rif_mac_profile_register;
3840
3841 err = mlxsw_sp_resources_rifs_register(mlxsw_core);
3842 if (err)
3843 goto err_resources_rifs_register;
3844
3845 err = mlxsw_sp_resources_port_range_register(mlxsw_core);
3846 if (err)
3847 goto err_resources_port_range_register;
3848
3849 return 0;
3850
3851 err_resources_port_range_register:
3852 err_resources_rifs_register:
3853 err_resources_rif_mac_profile_register:
3854 err_policer_resources_register:
3855 err_resources_counter_register:
3856 err_resources_span_register:
3857 devl_resources_unregister(priv_to_devlink(mlxsw_core));
3858 return err;
3859 }
3860
mlxsw_sp2_resources_register(struct mlxsw_core * mlxsw_core)3861 static int mlxsw_sp2_resources_register(struct mlxsw_core *mlxsw_core)
3862 {
3863 int err;
3864
3865 err = mlxsw_sp2_resources_kvd_register(mlxsw_core);
3866 if (err)
3867 return err;
3868
3869 err = mlxsw_sp_resources_span_register(mlxsw_core);
3870 if (err)
3871 goto err_resources_span_register;
3872
3873 err = mlxsw_sp_counter_resources_register(mlxsw_core);
3874 if (err)
3875 goto err_resources_counter_register;
3876
3877 err = mlxsw_sp_policer_resources_register(mlxsw_core);
3878 if (err)
3879 goto err_policer_resources_register;
3880
3881 err = mlxsw_sp_resources_rif_mac_profile_register(mlxsw_core);
3882 if (err)
3883 goto err_resources_rif_mac_profile_register;
3884
3885 err = mlxsw_sp_resources_rifs_register(mlxsw_core);
3886 if (err)
3887 goto err_resources_rifs_register;
3888
3889 err = mlxsw_sp_resources_port_range_register(mlxsw_core);
3890 if (err)
3891 goto err_resources_port_range_register;
3892
3893 return 0;
3894
3895 err_resources_port_range_register:
3896 err_resources_rifs_register:
3897 err_resources_rif_mac_profile_register:
3898 err_policer_resources_register:
3899 err_resources_counter_register:
3900 err_resources_span_register:
3901 devl_resources_unregister(priv_to_devlink(mlxsw_core));
3902 return err;
3903 }
3904
mlxsw_sp_kvd_sizes_get(struct mlxsw_core * mlxsw_core,const struct mlxsw_config_profile * profile,u64 * p_single_size,u64 * p_double_size,u64 * p_linear_size)3905 static int mlxsw_sp_kvd_sizes_get(struct mlxsw_core *mlxsw_core,
3906 const struct mlxsw_config_profile *profile,
3907 u64 *p_single_size, u64 *p_double_size,
3908 u64 *p_linear_size)
3909 {
3910 struct devlink *devlink = priv_to_devlink(mlxsw_core);
3911 u32 double_size;
3912 int err;
3913
3914 if (!MLXSW_CORE_RES_VALID(mlxsw_core, KVD_SINGLE_MIN_SIZE) ||
3915 !MLXSW_CORE_RES_VALID(mlxsw_core, KVD_DOUBLE_MIN_SIZE))
3916 return -EIO;
3917
3918 /* The hash part is what left of the kvd without the
3919 * linear part. It is split to the single size and
3920 * double size by the parts ratio from the profile.
3921 * Both sizes must be a multiplications of the
3922 * granularity from the profile. In case the user
3923 * provided the sizes they are obtained via devlink.
3924 */
3925 err = devl_resource_size_get(devlink,
3926 MLXSW_SP_RESOURCE_KVD_LINEAR,
3927 p_linear_size);
3928 if (err)
3929 *p_linear_size = profile->kvd_linear_size;
3930
3931 err = devl_resource_size_get(devlink,
3932 MLXSW_SP_RESOURCE_KVD_HASH_DOUBLE,
3933 p_double_size);
3934 if (err) {
3935 double_size = MLXSW_CORE_RES_GET(mlxsw_core, KVD_SIZE) -
3936 *p_linear_size;
3937 double_size *= profile->kvd_hash_double_parts;
3938 double_size /= profile->kvd_hash_double_parts +
3939 profile->kvd_hash_single_parts;
3940 *p_double_size = rounddown(double_size,
3941 MLXSW_SP_KVD_GRANULARITY);
3942 }
3943
3944 err = devl_resource_size_get(devlink,
3945 MLXSW_SP_RESOURCE_KVD_HASH_SINGLE,
3946 p_single_size);
3947 if (err)
3948 *p_single_size = MLXSW_CORE_RES_GET(mlxsw_core, KVD_SIZE) -
3949 *p_double_size - *p_linear_size;
3950
3951 /* Check results are legal. */
3952 if (*p_single_size < MLXSW_CORE_RES_GET(mlxsw_core, KVD_SINGLE_MIN_SIZE) ||
3953 *p_double_size < MLXSW_CORE_RES_GET(mlxsw_core, KVD_DOUBLE_MIN_SIZE) ||
3954 MLXSW_CORE_RES_GET(mlxsw_core, KVD_SIZE) < *p_linear_size)
3955 return -EIO;
3956
3957 return 0;
3958 }
3959
mlxsw_sp_ptp_transmitted(struct mlxsw_core * mlxsw_core,struct sk_buff * skb,u16 local_port)3960 static void mlxsw_sp_ptp_transmitted(struct mlxsw_core *mlxsw_core,
3961 struct sk_buff *skb, u16 local_port)
3962 {
3963 struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
3964
3965 skb_pull(skb, MLXSW_TXHDR_LEN);
3966 mlxsw_sp->ptp_ops->transmitted(mlxsw_sp, skb, local_port);
3967 }
3968
3969 static struct mlxsw_driver mlxsw_sp1_driver = {
3970 .kind = mlxsw_sp1_driver_name,
3971 .priv_size = sizeof(struct mlxsw_sp),
3972 .fw_req_rev = &mlxsw_sp1_fw_rev,
3973 .fw_filename = MLXSW_SP1_FW_FILENAME,
3974 .init = mlxsw_sp1_init,
3975 .fini = mlxsw_sp_fini,
3976 .port_split = mlxsw_sp_port_split,
3977 .port_unsplit = mlxsw_sp_port_unsplit,
3978 .sb_pool_get = mlxsw_sp_sb_pool_get,
3979 .sb_pool_set = mlxsw_sp_sb_pool_set,
3980 .sb_port_pool_get = mlxsw_sp_sb_port_pool_get,
3981 .sb_port_pool_set = mlxsw_sp_sb_port_pool_set,
3982 .sb_tc_pool_bind_get = mlxsw_sp_sb_tc_pool_bind_get,
3983 .sb_tc_pool_bind_set = mlxsw_sp_sb_tc_pool_bind_set,
3984 .sb_occ_snapshot = mlxsw_sp_sb_occ_snapshot,
3985 .sb_occ_max_clear = mlxsw_sp_sb_occ_max_clear,
3986 .sb_occ_port_pool_get = mlxsw_sp_sb_occ_port_pool_get,
3987 .sb_occ_tc_port_bind_get = mlxsw_sp_sb_occ_tc_port_bind_get,
3988 .trap_init = mlxsw_sp_trap_init,
3989 .trap_fini = mlxsw_sp_trap_fini,
3990 .trap_action_set = mlxsw_sp_trap_action_set,
3991 .trap_group_init = mlxsw_sp_trap_group_init,
3992 .trap_group_set = mlxsw_sp_trap_group_set,
3993 .trap_policer_init = mlxsw_sp_trap_policer_init,
3994 .trap_policer_fini = mlxsw_sp_trap_policer_fini,
3995 .trap_policer_set = mlxsw_sp_trap_policer_set,
3996 .trap_policer_counter_get = mlxsw_sp_trap_policer_counter_get,
3997 .txhdr_construct = mlxsw_sp_txhdr_construct,
3998 .resources_register = mlxsw_sp1_resources_register,
3999 .kvd_sizes_get = mlxsw_sp_kvd_sizes_get,
4000 .ptp_transmitted = mlxsw_sp_ptp_transmitted,
4001 .txhdr_len = MLXSW_TXHDR_LEN,
4002 .profile = &mlxsw_sp1_config_profile,
4003 .sdq_supports_cqe_v2 = false,
4004 };
4005
4006 static struct mlxsw_driver mlxsw_sp2_driver = {
4007 .kind = mlxsw_sp2_driver_name,
4008 .priv_size = sizeof(struct mlxsw_sp),
4009 .fw_req_rev = &mlxsw_sp2_fw_rev,
4010 .fw_filename = MLXSW_SP2_FW_FILENAME,
4011 .init = mlxsw_sp2_init,
4012 .fini = mlxsw_sp_fini,
4013 .port_split = mlxsw_sp_port_split,
4014 .port_unsplit = mlxsw_sp_port_unsplit,
4015 .ports_remove_selected = mlxsw_sp_ports_remove_selected,
4016 .sb_pool_get = mlxsw_sp_sb_pool_get,
4017 .sb_pool_set = mlxsw_sp_sb_pool_set,
4018 .sb_port_pool_get = mlxsw_sp_sb_port_pool_get,
4019 .sb_port_pool_set = mlxsw_sp_sb_port_pool_set,
4020 .sb_tc_pool_bind_get = mlxsw_sp_sb_tc_pool_bind_get,
4021 .sb_tc_pool_bind_set = mlxsw_sp_sb_tc_pool_bind_set,
4022 .sb_occ_snapshot = mlxsw_sp_sb_occ_snapshot,
4023 .sb_occ_max_clear = mlxsw_sp_sb_occ_max_clear,
4024 .sb_occ_port_pool_get = mlxsw_sp_sb_occ_port_pool_get,
4025 .sb_occ_tc_port_bind_get = mlxsw_sp_sb_occ_tc_port_bind_get,
4026 .trap_init = mlxsw_sp_trap_init,
4027 .trap_fini = mlxsw_sp_trap_fini,
4028 .trap_action_set = mlxsw_sp_trap_action_set,
4029 .trap_group_init = mlxsw_sp_trap_group_init,
4030 .trap_group_set = mlxsw_sp_trap_group_set,
4031 .trap_policer_init = mlxsw_sp_trap_policer_init,
4032 .trap_policer_fini = mlxsw_sp_trap_policer_fini,
4033 .trap_policer_set = mlxsw_sp_trap_policer_set,
4034 .trap_policer_counter_get = mlxsw_sp_trap_policer_counter_get,
4035 .txhdr_construct = mlxsw_sp_txhdr_construct,
4036 .resources_register = mlxsw_sp2_resources_register,
4037 .ptp_transmitted = mlxsw_sp_ptp_transmitted,
4038 .txhdr_len = MLXSW_TXHDR_LEN,
4039 .profile = &mlxsw_sp2_config_profile,
4040 .sdq_supports_cqe_v2 = true,
4041 };
4042
4043 static struct mlxsw_driver mlxsw_sp3_driver = {
4044 .kind = mlxsw_sp3_driver_name,
4045 .priv_size = sizeof(struct mlxsw_sp),
4046 .fw_req_rev = &mlxsw_sp3_fw_rev,
4047 .fw_filename = MLXSW_SP3_FW_FILENAME,
4048 .init = mlxsw_sp3_init,
4049 .fini = mlxsw_sp_fini,
4050 .port_split = mlxsw_sp_port_split,
4051 .port_unsplit = mlxsw_sp_port_unsplit,
4052 .ports_remove_selected = mlxsw_sp_ports_remove_selected,
4053 .sb_pool_get = mlxsw_sp_sb_pool_get,
4054 .sb_pool_set = mlxsw_sp_sb_pool_set,
4055 .sb_port_pool_get = mlxsw_sp_sb_port_pool_get,
4056 .sb_port_pool_set = mlxsw_sp_sb_port_pool_set,
4057 .sb_tc_pool_bind_get = mlxsw_sp_sb_tc_pool_bind_get,
4058 .sb_tc_pool_bind_set = mlxsw_sp_sb_tc_pool_bind_set,
4059 .sb_occ_snapshot = mlxsw_sp_sb_occ_snapshot,
4060 .sb_occ_max_clear = mlxsw_sp_sb_occ_max_clear,
4061 .sb_occ_port_pool_get = mlxsw_sp_sb_occ_port_pool_get,
4062 .sb_occ_tc_port_bind_get = mlxsw_sp_sb_occ_tc_port_bind_get,
4063 .trap_init = mlxsw_sp_trap_init,
4064 .trap_fini = mlxsw_sp_trap_fini,
4065 .trap_action_set = mlxsw_sp_trap_action_set,
4066 .trap_group_init = mlxsw_sp_trap_group_init,
4067 .trap_group_set = mlxsw_sp_trap_group_set,
4068 .trap_policer_init = mlxsw_sp_trap_policer_init,
4069 .trap_policer_fini = mlxsw_sp_trap_policer_fini,
4070 .trap_policer_set = mlxsw_sp_trap_policer_set,
4071 .trap_policer_counter_get = mlxsw_sp_trap_policer_counter_get,
4072 .txhdr_construct = mlxsw_sp_txhdr_construct,
4073 .resources_register = mlxsw_sp2_resources_register,
4074 .ptp_transmitted = mlxsw_sp_ptp_transmitted,
4075 .txhdr_len = MLXSW_TXHDR_LEN,
4076 .profile = &mlxsw_sp2_config_profile,
4077 .sdq_supports_cqe_v2 = true,
4078 };
4079
4080 static struct mlxsw_driver mlxsw_sp4_driver = {
4081 .kind = mlxsw_sp4_driver_name,
4082 .priv_size = sizeof(struct mlxsw_sp),
4083 .init = mlxsw_sp4_init,
4084 .fini = mlxsw_sp_fini,
4085 .port_split = mlxsw_sp_port_split,
4086 .port_unsplit = mlxsw_sp_port_unsplit,
4087 .ports_remove_selected = mlxsw_sp_ports_remove_selected,
4088 .sb_pool_get = mlxsw_sp_sb_pool_get,
4089 .sb_pool_set = mlxsw_sp_sb_pool_set,
4090 .sb_port_pool_get = mlxsw_sp_sb_port_pool_get,
4091 .sb_port_pool_set = mlxsw_sp_sb_port_pool_set,
4092 .sb_tc_pool_bind_get = mlxsw_sp_sb_tc_pool_bind_get,
4093 .sb_tc_pool_bind_set = mlxsw_sp_sb_tc_pool_bind_set,
4094 .sb_occ_snapshot = mlxsw_sp_sb_occ_snapshot,
4095 .sb_occ_max_clear = mlxsw_sp_sb_occ_max_clear,
4096 .sb_occ_port_pool_get = mlxsw_sp_sb_occ_port_pool_get,
4097 .sb_occ_tc_port_bind_get = mlxsw_sp_sb_occ_tc_port_bind_get,
4098 .trap_init = mlxsw_sp_trap_init,
4099 .trap_fini = mlxsw_sp_trap_fini,
4100 .trap_action_set = mlxsw_sp_trap_action_set,
4101 .trap_group_init = mlxsw_sp_trap_group_init,
4102 .trap_group_set = mlxsw_sp_trap_group_set,
4103 .trap_policer_init = mlxsw_sp_trap_policer_init,
4104 .trap_policer_fini = mlxsw_sp_trap_policer_fini,
4105 .trap_policer_set = mlxsw_sp_trap_policer_set,
4106 .trap_policer_counter_get = mlxsw_sp_trap_policer_counter_get,
4107 .txhdr_construct = mlxsw_sp_txhdr_construct,
4108 .resources_register = mlxsw_sp2_resources_register,
4109 .ptp_transmitted = mlxsw_sp_ptp_transmitted,
4110 .txhdr_len = MLXSW_TXHDR_LEN,
4111 .profile = &mlxsw_sp4_config_profile,
4112 .sdq_supports_cqe_v2 = true,
4113 };
4114
mlxsw_sp_port_dev_check(const struct net_device * dev)4115 bool mlxsw_sp_port_dev_check(const struct net_device *dev)
4116 {
4117 return dev->netdev_ops == &mlxsw_sp_port_netdev_ops;
4118 }
4119
mlxsw_sp_lower_dev_walk(struct net_device * lower_dev,struct netdev_nested_priv * priv)4120 static int mlxsw_sp_lower_dev_walk(struct net_device *lower_dev,
4121 struct netdev_nested_priv *priv)
4122 {
4123 int ret = 0;
4124
4125 if (mlxsw_sp_port_dev_check(lower_dev)) {
4126 priv->data = (void *)netdev_priv(lower_dev);
4127 ret = 1;
4128 }
4129
4130 return ret;
4131 }
4132
mlxsw_sp_port_dev_lower_find(struct net_device * dev)4133 struct mlxsw_sp_port *mlxsw_sp_port_dev_lower_find(struct net_device *dev)
4134 {
4135 struct netdev_nested_priv priv = {
4136 .data = NULL,
4137 };
4138
4139 if (mlxsw_sp_port_dev_check(dev))
4140 return netdev_priv(dev);
4141
4142 netdev_walk_all_lower_dev(dev, mlxsw_sp_lower_dev_walk, &priv);
4143
4144 return (struct mlxsw_sp_port *)priv.data;
4145 }
4146
mlxsw_sp_lower_get(struct net_device * dev)4147 struct mlxsw_sp *mlxsw_sp_lower_get(struct net_device *dev)
4148 {
4149 struct mlxsw_sp_port *mlxsw_sp_port;
4150
4151 mlxsw_sp_port = mlxsw_sp_port_dev_lower_find(dev);
4152 return mlxsw_sp_port ? mlxsw_sp_port->mlxsw_sp : NULL;
4153 }
4154
mlxsw_sp_port_dev_lower_find_rcu(struct net_device * dev)4155 struct mlxsw_sp_port *mlxsw_sp_port_dev_lower_find_rcu(struct net_device *dev)
4156 {
4157 struct netdev_nested_priv priv = {
4158 .data = NULL,
4159 };
4160
4161 if (mlxsw_sp_port_dev_check(dev))
4162 return netdev_priv(dev);
4163
4164 netdev_walk_all_lower_dev_rcu(dev, mlxsw_sp_lower_dev_walk,
4165 &priv);
4166
4167 return (struct mlxsw_sp_port *)priv.data;
4168 }
4169
mlxsw_sp_parsing_depth_inc(struct mlxsw_sp * mlxsw_sp)4170 int mlxsw_sp_parsing_depth_inc(struct mlxsw_sp *mlxsw_sp)
4171 {
4172 char mprs_pl[MLXSW_REG_MPRS_LEN];
4173 int err = 0;
4174
4175 mutex_lock(&mlxsw_sp->parsing.lock);
4176
4177 if (refcount_inc_not_zero(&mlxsw_sp->parsing.parsing_depth_ref))
4178 goto out_unlock;
4179
4180 mlxsw_reg_mprs_pack(mprs_pl, MLXSW_SP_INCREASED_PARSING_DEPTH,
4181 mlxsw_sp->parsing.vxlan_udp_dport);
4182 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(mprs), mprs_pl);
4183 if (err)
4184 goto out_unlock;
4185
4186 mlxsw_sp->parsing.parsing_depth = MLXSW_SP_INCREASED_PARSING_DEPTH;
4187 refcount_set(&mlxsw_sp->parsing.parsing_depth_ref, 1);
4188
4189 out_unlock:
4190 mutex_unlock(&mlxsw_sp->parsing.lock);
4191 return err;
4192 }
4193
mlxsw_sp_parsing_depth_dec(struct mlxsw_sp * mlxsw_sp)4194 void mlxsw_sp_parsing_depth_dec(struct mlxsw_sp *mlxsw_sp)
4195 {
4196 char mprs_pl[MLXSW_REG_MPRS_LEN];
4197
4198 mutex_lock(&mlxsw_sp->parsing.lock);
4199
4200 if (!refcount_dec_and_test(&mlxsw_sp->parsing.parsing_depth_ref))
4201 goto out_unlock;
4202
4203 mlxsw_reg_mprs_pack(mprs_pl, MLXSW_SP_DEFAULT_PARSING_DEPTH,
4204 mlxsw_sp->parsing.vxlan_udp_dport);
4205 mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(mprs), mprs_pl);
4206 mlxsw_sp->parsing.parsing_depth = MLXSW_SP_DEFAULT_PARSING_DEPTH;
4207
4208 out_unlock:
4209 mutex_unlock(&mlxsw_sp->parsing.lock);
4210 }
4211
mlxsw_sp_parsing_vxlan_udp_dport_set(struct mlxsw_sp * mlxsw_sp,__be16 udp_dport)4212 int mlxsw_sp_parsing_vxlan_udp_dport_set(struct mlxsw_sp *mlxsw_sp,
4213 __be16 udp_dport)
4214 {
4215 char mprs_pl[MLXSW_REG_MPRS_LEN];
4216 int err;
4217
4218 mutex_lock(&mlxsw_sp->parsing.lock);
4219
4220 mlxsw_reg_mprs_pack(mprs_pl, mlxsw_sp->parsing.parsing_depth,
4221 be16_to_cpu(udp_dport));
4222 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(mprs), mprs_pl);
4223 if (err)
4224 goto out_unlock;
4225
4226 mlxsw_sp->parsing.vxlan_udp_dport = be16_to_cpu(udp_dport);
4227
4228 out_unlock:
4229 mutex_unlock(&mlxsw_sp->parsing.lock);
4230 return err;
4231 }
4232
4233 static void
mlxsw_sp_port_lag_uppers_cleanup(struct mlxsw_sp_port * mlxsw_sp_port,struct net_device * lag_dev)4234 mlxsw_sp_port_lag_uppers_cleanup(struct mlxsw_sp_port *mlxsw_sp_port,
4235 struct net_device *lag_dev)
4236 {
4237 struct net_device *br_dev = netdev_master_upper_dev_get(lag_dev);
4238 struct net_device *upper_dev;
4239 struct list_head *iter;
4240
4241 if (netif_is_bridge_port(lag_dev))
4242 mlxsw_sp_port_bridge_leave(mlxsw_sp_port, lag_dev, br_dev);
4243
4244 netdev_for_each_upper_dev_rcu(lag_dev, upper_dev, iter) {
4245 if (!netif_is_bridge_port(upper_dev))
4246 continue;
4247 br_dev = netdev_master_upper_dev_get(upper_dev);
4248 mlxsw_sp_port_bridge_leave(mlxsw_sp_port, upper_dev, br_dev);
4249 }
4250 }
4251
4252 static struct mlxsw_sp_lag *
mlxsw_sp_lag_create(struct mlxsw_sp * mlxsw_sp,struct net_device * lag_dev,struct netlink_ext_ack * extack)4253 mlxsw_sp_lag_create(struct mlxsw_sp *mlxsw_sp, struct net_device *lag_dev,
4254 struct netlink_ext_ack *extack)
4255 {
4256 char sldr_pl[MLXSW_REG_SLDR_LEN];
4257 struct mlxsw_sp_lag *lag;
4258 u16 lag_id;
4259 int i, err;
4260
4261 for (i = 0; i < mlxsw_sp->max_lag; i++) {
4262 if (!mlxsw_sp->lags[i].dev)
4263 break;
4264 }
4265
4266 if (i == mlxsw_sp->max_lag) {
4267 NL_SET_ERR_MSG_MOD(extack,
4268 "Exceeded number of supported LAG devices");
4269 return ERR_PTR(-EBUSY);
4270 }
4271
4272 lag_id = i;
4273 mlxsw_reg_sldr_lag_create_pack(sldr_pl, lag_id);
4274 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sldr), sldr_pl);
4275 if (err)
4276 return ERR_PTR(err);
4277
4278 lag = &mlxsw_sp->lags[lag_id];
4279 lag->lag_id = lag_id;
4280 lag->dev = lag_dev;
4281 refcount_set(&lag->ref_count, 1);
4282
4283 return lag;
4284 }
4285
4286 static int
mlxsw_sp_lag_destroy(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_lag * lag)4287 mlxsw_sp_lag_destroy(struct mlxsw_sp *mlxsw_sp, struct mlxsw_sp_lag *lag)
4288 {
4289 char sldr_pl[MLXSW_REG_SLDR_LEN];
4290
4291 lag->dev = NULL;
4292
4293 mlxsw_reg_sldr_lag_destroy_pack(sldr_pl, lag->lag_id);
4294 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sldr), sldr_pl);
4295 }
4296
mlxsw_sp_lag_col_port_add(struct mlxsw_sp_port * mlxsw_sp_port,u16 lag_id,u8 port_index)4297 static int mlxsw_sp_lag_col_port_add(struct mlxsw_sp_port *mlxsw_sp_port,
4298 u16 lag_id, u8 port_index)
4299 {
4300 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
4301 char slcor_pl[MLXSW_REG_SLCOR_LEN];
4302
4303 mlxsw_reg_slcor_port_add_pack(slcor_pl, mlxsw_sp_port->local_port,
4304 lag_id, port_index);
4305 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(slcor), slcor_pl);
4306 }
4307
mlxsw_sp_lag_col_port_remove(struct mlxsw_sp_port * mlxsw_sp_port,u16 lag_id)4308 static int mlxsw_sp_lag_col_port_remove(struct mlxsw_sp_port *mlxsw_sp_port,
4309 u16 lag_id)
4310 {
4311 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
4312 char slcor_pl[MLXSW_REG_SLCOR_LEN];
4313
4314 mlxsw_reg_slcor_port_remove_pack(slcor_pl, mlxsw_sp_port->local_port,
4315 lag_id);
4316 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(slcor), slcor_pl);
4317 }
4318
mlxsw_sp_lag_col_port_enable(struct mlxsw_sp_port * mlxsw_sp_port,u16 lag_id)4319 static int mlxsw_sp_lag_col_port_enable(struct mlxsw_sp_port *mlxsw_sp_port,
4320 u16 lag_id)
4321 {
4322 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
4323 char slcor_pl[MLXSW_REG_SLCOR_LEN];
4324
4325 mlxsw_reg_slcor_col_enable_pack(slcor_pl, mlxsw_sp_port->local_port,
4326 lag_id);
4327 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(slcor), slcor_pl);
4328 }
4329
mlxsw_sp_lag_col_port_disable(struct mlxsw_sp_port * mlxsw_sp_port,u16 lag_id)4330 static int mlxsw_sp_lag_col_port_disable(struct mlxsw_sp_port *mlxsw_sp_port,
4331 u16 lag_id)
4332 {
4333 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
4334 char slcor_pl[MLXSW_REG_SLCOR_LEN];
4335
4336 mlxsw_reg_slcor_col_disable_pack(slcor_pl, mlxsw_sp_port->local_port,
4337 lag_id);
4338 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(slcor), slcor_pl);
4339 }
4340
4341 static struct mlxsw_sp_lag *
mlxsw_sp_lag_find(struct mlxsw_sp * mlxsw_sp,struct net_device * lag_dev)4342 mlxsw_sp_lag_find(struct mlxsw_sp *mlxsw_sp, struct net_device *lag_dev)
4343 {
4344 int i;
4345
4346 for (i = 0; i < mlxsw_sp->max_lag; i++) {
4347 if (!mlxsw_sp->lags[i].dev)
4348 continue;
4349
4350 if (mlxsw_sp->lags[i].dev == lag_dev)
4351 return &mlxsw_sp->lags[i];
4352 }
4353
4354 return NULL;
4355 }
4356
4357 static struct mlxsw_sp_lag *
mlxsw_sp_lag_get(struct mlxsw_sp * mlxsw_sp,struct net_device * lag_dev,struct netlink_ext_ack * extack)4358 mlxsw_sp_lag_get(struct mlxsw_sp *mlxsw_sp, struct net_device *lag_dev,
4359 struct netlink_ext_ack *extack)
4360 {
4361 struct mlxsw_sp_lag *lag;
4362
4363 lag = mlxsw_sp_lag_find(mlxsw_sp, lag_dev);
4364 if (lag) {
4365 refcount_inc(&lag->ref_count);
4366 return lag;
4367 }
4368
4369 return mlxsw_sp_lag_create(mlxsw_sp, lag_dev, extack);
4370 }
4371
4372 static void
mlxsw_sp_lag_put(struct mlxsw_sp * mlxsw_sp,struct mlxsw_sp_lag * lag)4373 mlxsw_sp_lag_put(struct mlxsw_sp *mlxsw_sp, struct mlxsw_sp_lag *lag)
4374 {
4375 if (!refcount_dec_and_test(&lag->ref_count))
4376 return;
4377
4378 mlxsw_sp_lag_destroy(mlxsw_sp, lag);
4379 }
4380
4381 static bool
mlxsw_sp_master_lag_check(struct mlxsw_sp * mlxsw_sp,struct net_device * lag_dev,struct netdev_lag_upper_info * lag_upper_info,struct netlink_ext_ack * extack)4382 mlxsw_sp_master_lag_check(struct mlxsw_sp *mlxsw_sp,
4383 struct net_device *lag_dev,
4384 struct netdev_lag_upper_info *lag_upper_info,
4385 struct netlink_ext_ack *extack)
4386 {
4387 if (lag_upper_info->tx_type != NETDEV_LAG_TX_TYPE_HASH) {
4388 NL_SET_ERR_MSG_MOD(extack, "LAG device using unsupported Tx type");
4389 return false;
4390 }
4391 return true;
4392 }
4393
mlxsw_sp_port_lag_index_get(struct mlxsw_sp * mlxsw_sp,u16 lag_id,u8 * p_port_index)4394 static int mlxsw_sp_port_lag_index_get(struct mlxsw_sp *mlxsw_sp,
4395 u16 lag_id, u8 *p_port_index)
4396 {
4397 u64 max_lag_members;
4398 int i;
4399
4400 max_lag_members = MLXSW_CORE_RES_GET(mlxsw_sp->core,
4401 MAX_LAG_MEMBERS);
4402 for (i = 0; i < max_lag_members; i++) {
4403 if (!mlxsw_sp_port_lagged_get(mlxsw_sp, lag_id, i)) {
4404 *p_port_index = i;
4405 return 0;
4406 }
4407 }
4408 return -EBUSY;
4409 }
4410
mlxsw_sp_lag_uppers_bridge_join(struct mlxsw_sp_port * mlxsw_sp_port,struct net_device * lag_dev,struct netlink_ext_ack * extack)4411 static int mlxsw_sp_lag_uppers_bridge_join(struct mlxsw_sp_port *mlxsw_sp_port,
4412 struct net_device *lag_dev,
4413 struct netlink_ext_ack *extack)
4414 {
4415 struct net_device *upper_dev;
4416 struct net_device *master;
4417 struct list_head *iter;
4418 int done = 0;
4419 int err;
4420
4421 master = netdev_master_upper_dev_get(lag_dev);
4422 if (master && netif_is_bridge_master(master)) {
4423 err = mlxsw_sp_port_bridge_join(mlxsw_sp_port, lag_dev, master,
4424 extack);
4425 if (err)
4426 return err;
4427 }
4428
4429 netdev_for_each_upper_dev_rcu(lag_dev, upper_dev, iter) {
4430 if (!is_vlan_dev(upper_dev))
4431 continue;
4432
4433 master = netdev_master_upper_dev_get(upper_dev);
4434 if (master && netif_is_bridge_master(master)) {
4435 err = mlxsw_sp_port_bridge_join(mlxsw_sp_port,
4436 upper_dev, master,
4437 extack);
4438 if (err)
4439 goto err_port_bridge_join;
4440 }
4441
4442 ++done;
4443 }
4444
4445 return 0;
4446
4447 err_port_bridge_join:
4448 netdev_for_each_upper_dev_rcu(lag_dev, upper_dev, iter) {
4449 if (!is_vlan_dev(upper_dev))
4450 continue;
4451
4452 master = netdev_master_upper_dev_get(upper_dev);
4453 if (!master || !netif_is_bridge_master(master))
4454 continue;
4455
4456 if (!done--)
4457 break;
4458
4459 mlxsw_sp_port_bridge_leave(mlxsw_sp_port, upper_dev, master);
4460 }
4461
4462 master = netdev_master_upper_dev_get(lag_dev);
4463 if (master && netif_is_bridge_master(master))
4464 mlxsw_sp_port_bridge_leave(mlxsw_sp_port, lag_dev, master);
4465
4466 return err;
4467 }
4468
4469 static void
mlxsw_sp_lag_uppers_bridge_leave(struct mlxsw_sp_port * mlxsw_sp_port,struct net_device * lag_dev)4470 mlxsw_sp_lag_uppers_bridge_leave(struct mlxsw_sp_port *mlxsw_sp_port,
4471 struct net_device *lag_dev)
4472 {
4473 struct net_device *upper_dev;
4474 struct net_device *master;
4475 struct list_head *iter;
4476
4477 netdev_for_each_upper_dev_rcu(lag_dev, upper_dev, iter) {
4478 if (!is_vlan_dev(upper_dev))
4479 continue;
4480
4481 master = netdev_master_upper_dev_get(upper_dev);
4482 if (!master)
4483 continue;
4484
4485 mlxsw_sp_port_bridge_leave(mlxsw_sp_port, upper_dev, master);
4486 }
4487
4488 master = netdev_master_upper_dev_get(lag_dev);
4489 if (master)
4490 mlxsw_sp_port_bridge_leave(mlxsw_sp_port, lag_dev, master);
4491 }
4492
mlxsw_sp_port_lag_join(struct mlxsw_sp_port * mlxsw_sp_port,struct net_device * lag_dev,struct netlink_ext_ack * extack)4493 static int mlxsw_sp_port_lag_join(struct mlxsw_sp_port *mlxsw_sp_port,
4494 struct net_device *lag_dev,
4495 struct netlink_ext_ack *extack)
4496 {
4497 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
4498 struct mlxsw_sp_lag *lag;
4499 u16 lag_id;
4500 u8 port_index;
4501 int err;
4502
4503 lag = mlxsw_sp_lag_get(mlxsw_sp, lag_dev, extack);
4504 if (IS_ERR(lag))
4505 return PTR_ERR(lag);
4506
4507 lag_id = lag->lag_id;
4508 err = mlxsw_sp_port_lag_index_get(mlxsw_sp, lag_id, &port_index);
4509 if (err)
4510 return err;
4511
4512 err = mlxsw_sp_lag_uppers_bridge_join(mlxsw_sp_port, lag_dev,
4513 extack);
4514 if (err)
4515 goto err_lag_uppers_bridge_join;
4516
4517 err = mlxsw_sp_lag_col_port_add(mlxsw_sp_port, lag_id, port_index);
4518 if (err)
4519 goto err_col_port_add;
4520
4521 mlxsw_core_lag_mapping_set(mlxsw_sp->core, lag_id, port_index,
4522 mlxsw_sp_port->local_port);
4523 mlxsw_sp_port->lag_id = lag_id;
4524 mlxsw_sp_port->lagged = 1;
4525
4526 err = mlxsw_sp_fid_port_join_lag(mlxsw_sp_port);
4527 if (err)
4528 goto err_fid_port_join_lag;
4529
4530 /* Port is no longer usable as a router interface */
4531 if (mlxsw_sp_port->default_vlan->fid)
4532 mlxsw_sp_port_vlan_router_leave(mlxsw_sp_port->default_vlan);
4533
4534 /* Join a router interface configured on the LAG, if exists */
4535 err = mlxsw_sp_router_port_join_lag(mlxsw_sp_port, lag_dev,
4536 extack);
4537 if (err)
4538 goto err_router_join;
4539
4540 err = mlxsw_sp_netdevice_enslavement_replay(mlxsw_sp, lag_dev, extack);
4541 if (err)
4542 goto err_replay;
4543
4544 return 0;
4545
4546 err_replay:
4547 mlxsw_sp_router_port_leave_lag(mlxsw_sp_port, lag_dev);
4548 err_router_join:
4549 mlxsw_sp_fid_port_leave_lag(mlxsw_sp_port);
4550 err_fid_port_join_lag:
4551 mlxsw_sp_port->lagged = 0;
4552 mlxsw_core_lag_mapping_clear(mlxsw_sp->core, lag_id,
4553 mlxsw_sp_port->local_port);
4554 mlxsw_sp_lag_col_port_remove(mlxsw_sp_port, lag_id);
4555 err_col_port_add:
4556 mlxsw_sp_lag_uppers_bridge_leave(mlxsw_sp_port, lag_dev);
4557 err_lag_uppers_bridge_join:
4558 mlxsw_sp_lag_put(mlxsw_sp, lag);
4559 return err;
4560 }
4561
mlxsw_sp_port_lag_leave(struct mlxsw_sp_port * mlxsw_sp_port,struct net_device * lag_dev)4562 static void mlxsw_sp_port_lag_leave(struct mlxsw_sp_port *mlxsw_sp_port,
4563 struct net_device *lag_dev)
4564 {
4565 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
4566 u16 lag_id = mlxsw_sp_port->lag_id;
4567 struct mlxsw_sp_lag *lag;
4568
4569 if (!mlxsw_sp_port->lagged)
4570 return;
4571 lag = &mlxsw_sp->lags[lag_id];
4572
4573 mlxsw_sp_lag_col_port_remove(mlxsw_sp_port, lag_id);
4574
4575 /* Any VLANs configured on the port are no longer valid */
4576 mlxsw_sp_port_vlan_flush(mlxsw_sp_port, false);
4577 mlxsw_sp_port_vlan_cleanup(mlxsw_sp_port->default_vlan);
4578 /* Make the LAG and its directly linked uppers leave bridges they
4579 * are memeber in
4580 */
4581 mlxsw_sp_port_lag_uppers_cleanup(mlxsw_sp_port, lag_dev);
4582
4583 mlxsw_sp_fid_port_leave_lag(mlxsw_sp_port);
4584
4585 mlxsw_sp_lag_put(mlxsw_sp, lag);
4586
4587 mlxsw_core_lag_mapping_clear(mlxsw_sp->core, lag_id,
4588 mlxsw_sp_port->local_port);
4589 mlxsw_sp_port->lagged = 0;
4590
4591 /* Make sure untagged frames are allowed to ingress */
4592 mlxsw_sp_port_pvid_set(mlxsw_sp_port, MLXSW_SP_DEFAULT_VID,
4593 ETH_P_8021Q);
4594 }
4595
mlxsw_sp_lag_dist_port_add(struct mlxsw_sp_port * mlxsw_sp_port,u16 lag_id)4596 static int mlxsw_sp_lag_dist_port_add(struct mlxsw_sp_port *mlxsw_sp_port,
4597 u16 lag_id)
4598 {
4599 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
4600 char sldr_pl[MLXSW_REG_SLDR_LEN];
4601
4602 mlxsw_reg_sldr_lag_add_port_pack(sldr_pl, lag_id,
4603 mlxsw_sp_port->local_port);
4604 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sldr), sldr_pl);
4605 }
4606
mlxsw_sp_lag_dist_port_remove(struct mlxsw_sp_port * mlxsw_sp_port,u16 lag_id)4607 static int mlxsw_sp_lag_dist_port_remove(struct mlxsw_sp_port *mlxsw_sp_port,
4608 u16 lag_id)
4609 {
4610 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
4611 char sldr_pl[MLXSW_REG_SLDR_LEN];
4612
4613 mlxsw_reg_sldr_lag_remove_port_pack(sldr_pl, lag_id,
4614 mlxsw_sp_port->local_port);
4615 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sldr), sldr_pl);
4616 }
4617
4618 static int
mlxsw_sp_port_lag_col_dist_enable(struct mlxsw_sp_port * mlxsw_sp_port)4619 mlxsw_sp_port_lag_col_dist_enable(struct mlxsw_sp_port *mlxsw_sp_port)
4620 {
4621 int err;
4622
4623 err = mlxsw_sp_lag_col_port_enable(mlxsw_sp_port,
4624 mlxsw_sp_port->lag_id);
4625 if (err)
4626 return err;
4627
4628 err = mlxsw_sp_lag_dist_port_add(mlxsw_sp_port, mlxsw_sp_port->lag_id);
4629 if (err)
4630 goto err_dist_port_add;
4631
4632 return 0;
4633
4634 err_dist_port_add:
4635 mlxsw_sp_lag_col_port_disable(mlxsw_sp_port, mlxsw_sp_port->lag_id);
4636 return err;
4637 }
4638
4639 static int
mlxsw_sp_port_lag_col_dist_disable(struct mlxsw_sp_port * mlxsw_sp_port)4640 mlxsw_sp_port_lag_col_dist_disable(struct mlxsw_sp_port *mlxsw_sp_port)
4641 {
4642 int err;
4643
4644 err = mlxsw_sp_lag_dist_port_remove(mlxsw_sp_port,
4645 mlxsw_sp_port->lag_id);
4646 if (err)
4647 return err;
4648
4649 err = mlxsw_sp_lag_col_port_disable(mlxsw_sp_port,
4650 mlxsw_sp_port->lag_id);
4651 if (err)
4652 goto err_col_port_disable;
4653
4654 return 0;
4655
4656 err_col_port_disable:
4657 mlxsw_sp_lag_dist_port_add(mlxsw_sp_port, mlxsw_sp_port->lag_id);
4658 return err;
4659 }
4660
mlxsw_sp_port_lag_changed(struct mlxsw_sp_port * mlxsw_sp_port,struct netdev_lag_lower_state_info * info)4661 static int mlxsw_sp_port_lag_changed(struct mlxsw_sp_port *mlxsw_sp_port,
4662 struct netdev_lag_lower_state_info *info)
4663 {
4664 if (info->tx_enabled)
4665 return mlxsw_sp_port_lag_col_dist_enable(mlxsw_sp_port);
4666 else
4667 return mlxsw_sp_port_lag_col_dist_disable(mlxsw_sp_port);
4668 }
4669
mlxsw_sp_port_stp_set(struct mlxsw_sp_port * mlxsw_sp_port,bool enable)4670 static int mlxsw_sp_port_stp_set(struct mlxsw_sp_port *mlxsw_sp_port,
4671 bool enable)
4672 {
4673 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
4674 enum mlxsw_reg_spms_state spms_state;
4675 char *spms_pl;
4676 u16 vid;
4677 int err;
4678
4679 spms_state = enable ? MLXSW_REG_SPMS_STATE_FORWARDING :
4680 MLXSW_REG_SPMS_STATE_DISCARDING;
4681
4682 spms_pl = kmalloc(MLXSW_REG_SPMS_LEN, GFP_KERNEL);
4683 if (!spms_pl)
4684 return -ENOMEM;
4685 mlxsw_reg_spms_pack(spms_pl, mlxsw_sp_port->local_port);
4686
4687 for (vid = 0; vid < VLAN_N_VID; vid++)
4688 mlxsw_reg_spms_vid_pack(spms_pl, vid, spms_state);
4689
4690 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(spms), spms_pl);
4691 kfree(spms_pl);
4692 return err;
4693 }
4694
mlxsw_sp_port_ovs_join(struct mlxsw_sp_port * mlxsw_sp_port)4695 static int mlxsw_sp_port_ovs_join(struct mlxsw_sp_port *mlxsw_sp_port)
4696 {
4697 u16 vid = 1;
4698 int err;
4699
4700 err = mlxsw_sp_port_vp_mode_set(mlxsw_sp_port, true);
4701 if (err)
4702 return err;
4703 err = mlxsw_sp_port_stp_set(mlxsw_sp_port, true);
4704 if (err)
4705 goto err_port_stp_set;
4706 err = mlxsw_sp_port_vlan_set(mlxsw_sp_port, 1, VLAN_N_VID - 2,
4707 true, false);
4708 if (err)
4709 goto err_port_vlan_set;
4710
4711 for (; vid <= VLAN_N_VID - 1; vid++) {
4712 err = mlxsw_sp_port_vid_learning_set(mlxsw_sp_port,
4713 vid, false);
4714 if (err)
4715 goto err_vid_learning_set;
4716 }
4717
4718 return 0;
4719
4720 err_vid_learning_set:
4721 for (vid--; vid >= 1; vid--)
4722 mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid, true);
4723 err_port_vlan_set:
4724 mlxsw_sp_port_stp_set(mlxsw_sp_port, false);
4725 err_port_stp_set:
4726 mlxsw_sp_port_vp_mode_set(mlxsw_sp_port, false);
4727 return err;
4728 }
4729
mlxsw_sp_port_ovs_leave(struct mlxsw_sp_port * mlxsw_sp_port)4730 static void mlxsw_sp_port_ovs_leave(struct mlxsw_sp_port *mlxsw_sp_port)
4731 {
4732 u16 vid;
4733
4734 for (vid = VLAN_N_VID - 1; vid >= 1; vid--)
4735 mlxsw_sp_port_vid_learning_set(mlxsw_sp_port,
4736 vid, true);
4737
4738 mlxsw_sp_port_vlan_set(mlxsw_sp_port, 1, VLAN_N_VID - 2,
4739 false, false);
4740 mlxsw_sp_port_stp_set(mlxsw_sp_port, false);
4741 mlxsw_sp_port_vp_mode_set(mlxsw_sp_port, false);
4742 }
4743
mlxsw_sp_bridge_has_multiple_vxlans(struct net_device * br_dev)4744 static bool mlxsw_sp_bridge_has_multiple_vxlans(struct net_device *br_dev)
4745 {
4746 unsigned int num_vxlans = 0;
4747 struct net_device *dev;
4748 struct list_head *iter;
4749
4750 netdev_for_each_lower_dev(br_dev, dev, iter) {
4751 if (netif_is_vxlan(dev))
4752 num_vxlans++;
4753 }
4754
4755 return num_vxlans > 1;
4756 }
4757
mlxsw_sp_bridge_vxlan_vlan_is_valid(struct net_device * br_dev)4758 static bool mlxsw_sp_bridge_vxlan_vlan_is_valid(struct net_device *br_dev)
4759 {
4760 DECLARE_BITMAP(vlans, VLAN_N_VID) = {0};
4761 struct net_device *dev;
4762 struct list_head *iter;
4763
4764 netdev_for_each_lower_dev(br_dev, dev, iter) {
4765 u16 pvid;
4766 int err;
4767
4768 if (!netif_is_vxlan(dev))
4769 continue;
4770
4771 err = mlxsw_sp_vxlan_mapped_vid(dev, &pvid);
4772 if (err || !pvid)
4773 continue;
4774
4775 if (test_and_set_bit(pvid, vlans))
4776 return false;
4777 }
4778
4779 return true;
4780 }
4781
mlxsw_sp_bridge_vxlan_is_valid(struct net_device * br_dev,struct netlink_ext_ack * extack)4782 static bool mlxsw_sp_bridge_vxlan_is_valid(struct net_device *br_dev,
4783 struct netlink_ext_ack *extack)
4784 {
4785 if (br_multicast_enabled(br_dev)) {
4786 NL_SET_ERR_MSG_MOD(extack, "Multicast can not be enabled on a bridge with a VxLAN device");
4787 return false;
4788 }
4789
4790 if (!br_vlan_enabled(br_dev) &&
4791 mlxsw_sp_bridge_has_multiple_vxlans(br_dev)) {
4792 NL_SET_ERR_MSG_MOD(extack, "Multiple VxLAN devices are not supported in a VLAN-unaware bridge");
4793 return false;
4794 }
4795
4796 if (br_vlan_enabled(br_dev) &&
4797 !mlxsw_sp_bridge_vxlan_vlan_is_valid(br_dev)) {
4798 NL_SET_ERR_MSG_MOD(extack, "Multiple VxLAN devices cannot have the same VLAN as PVID and egress untagged");
4799 return false;
4800 }
4801
4802 return true;
4803 }
4804
mlxsw_sp_netdev_is_master(struct net_device * upper_dev,struct net_device * dev)4805 static bool mlxsw_sp_netdev_is_master(struct net_device *upper_dev,
4806 struct net_device *dev)
4807 {
4808 return upper_dev == netdev_master_upper_dev_get(dev);
4809 }
4810
4811 static int __mlxsw_sp_netdevice_event(struct mlxsw_sp *mlxsw_sp,
4812 unsigned long event, void *ptr,
4813 bool process_foreign);
4814
mlxsw_sp_netdevice_validate_uppers(struct mlxsw_sp * mlxsw_sp,struct net_device * dev,struct netlink_ext_ack * extack)4815 static int mlxsw_sp_netdevice_validate_uppers(struct mlxsw_sp *mlxsw_sp,
4816 struct net_device *dev,
4817 struct netlink_ext_ack *extack)
4818 {
4819 struct net_device *upper_dev;
4820 struct list_head *iter;
4821 int err;
4822
4823 netdev_for_each_upper_dev_rcu(dev, upper_dev, iter) {
4824 struct netdev_notifier_changeupper_info info = {
4825 .info = {
4826 .dev = dev,
4827 .extack = extack,
4828 },
4829 .master = mlxsw_sp_netdev_is_master(upper_dev, dev),
4830 .upper_dev = upper_dev,
4831 .linking = true,
4832
4833 /* upper_info is relevant for LAG devices. But we would
4834 * only need this if LAG were a valid upper above
4835 * another upper (e.g. a bridge that is a member of a
4836 * LAG), and that is never a valid configuration. So we
4837 * can keep this as NULL.
4838 */
4839 .upper_info = NULL,
4840 };
4841
4842 err = __mlxsw_sp_netdevice_event(mlxsw_sp,
4843 NETDEV_PRECHANGEUPPER,
4844 &info, true);
4845 if (err)
4846 return err;
4847
4848 err = mlxsw_sp_netdevice_validate_uppers(mlxsw_sp, upper_dev,
4849 extack);
4850 if (err)
4851 return err;
4852 }
4853
4854 return 0;
4855 }
4856
mlxsw_sp_netdevice_port_upper_event(struct net_device * lower_dev,struct net_device * dev,unsigned long event,void * ptr,bool replay_deslavement)4857 static int mlxsw_sp_netdevice_port_upper_event(struct net_device *lower_dev,
4858 struct net_device *dev,
4859 unsigned long event, void *ptr,
4860 bool replay_deslavement)
4861 {
4862 struct netdev_notifier_changeupper_info *info;
4863 struct mlxsw_sp_port *mlxsw_sp_port;
4864 struct netlink_ext_ack *extack;
4865 struct net_device *upper_dev;
4866 struct mlxsw_sp *mlxsw_sp;
4867 int err = 0;
4868 u16 proto;
4869
4870 mlxsw_sp_port = netdev_priv(dev);
4871 mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
4872 info = ptr;
4873 extack = netdev_notifier_info_to_extack(&info->info);
4874
4875 switch (event) {
4876 case NETDEV_PRECHANGEUPPER:
4877 upper_dev = info->upper_dev;
4878 if (!is_vlan_dev(upper_dev) &&
4879 !netif_is_lag_master(upper_dev) &&
4880 !netif_is_bridge_master(upper_dev) &&
4881 !netif_is_ovs_master(upper_dev) &&
4882 !netif_is_macvlan(upper_dev) &&
4883 !netif_is_l3_master(upper_dev)) {
4884 NL_SET_ERR_MSG_MOD(extack, "Unknown upper device type");
4885 return -EINVAL;
4886 }
4887 if (!info->linking)
4888 break;
4889 if (netif_is_bridge_master(upper_dev) &&
4890 !mlxsw_sp_bridge_device_is_offloaded(mlxsw_sp, upper_dev) &&
4891 mlxsw_sp_bridge_has_vxlan(upper_dev) &&
4892 !mlxsw_sp_bridge_vxlan_is_valid(upper_dev, extack))
4893 return -EOPNOTSUPP;
4894 if (netdev_has_any_upper_dev(upper_dev) &&
4895 (!netif_is_bridge_master(upper_dev) ||
4896 !mlxsw_sp_bridge_device_is_offloaded(mlxsw_sp,
4897 upper_dev))) {
4898 err = mlxsw_sp_netdevice_validate_uppers(mlxsw_sp,
4899 upper_dev,
4900 extack);
4901 if (err)
4902 return err;
4903 }
4904 if (netif_is_lag_master(upper_dev) &&
4905 !mlxsw_sp_master_lag_check(mlxsw_sp, upper_dev,
4906 info->upper_info, extack))
4907 return -EINVAL;
4908 if (netif_is_lag_master(upper_dev) && vlan_uses_dev(dev)) {
4909 NL_SET_ERR_MSG_MOD(extack, "Master device is a LAG master and this device has a VLAN");
4910 return -EINVAL;
4911 }
4912 if (netif_is_lag_port(dev) && is_vlan_dev(upper_dev) &&
4913 !netif_is_lag_master(vlan_dev_real_dev(upper_dev))) {
4914 NL_SET_ERR_MSG_MOD(extack, "Can not put a VLAN on a LAG port");
4915 return -EINVAL;
4916 }
4917 if (netif_is_ovs_master(upper_dev) && vlan_uses_dev(dev)) {
4918 NL_SET_ERR_MSG_MOD(extack, "Master device is an OVS master and this device has a VLAN");
4919 return -EINVAL;
4920 }
4921 if (netif_is_ovs_port(dev) && is_vlan_dev(upper_dev)) {
4922 NL_SET_ERR_MSG_MOD(extack, "Can not put a VLAN on an OVS port");
4923 return -EINVAL;
4924 }
4925 if (netif_is_bridge_master(upper_dev)) {
4926 br_vlan_get_proto(upper_dev, &proto);
4927 if (br_vlan_enabled(upper_dev) &&
4928 proto != ETH_P_8021Q && proto != ETH_P_8021AD) {
4929 NL_SET_ERR_MSG_MOD(extack, "Enslaving a port to a bridge with unknown VLAN protocol is not supported");
4930 return -EOPNOTSUPP;
4931 }
4932 if (vlan_uses_dev(lower_dev) &&
4933 br_vlan_enabled(upper_dev) &&
4934 proto == ETH_P_8021AD) {
4935 NL_SET_ERR_MSG_MOD(extack, "Enslaving a port that already has a VLAN upper to an 802.1ad bridge is not supported");
4936 return -EOPNOTSUPP;
4937 }
4938 }
4939 if (netif_is_bridge_port(lower_dev) && is_vlan_dev(upper_dev)) {
4940 struct net_device *br_dev = netdev_master_upper_dev_get(lower_dev);
4941
4942 if (br_vlan_enabled(br_dev)) {
4943 br_vlan_get_proto(br_dev, &proto);
4944 if (proto == ETH_P_8021AD) {
4945 NL_SET_ERR_MSG_MOD(extack, "VLAN uppers are not supported on a port enslaved to an 802.1ad bridge");
4946 return -EOPNOTSUPP;
4947 }
4948 }
4949 }
4950 if (is_vlan_dev(upper_dev) &&
4951 ntohs(vlan_dev_vlan_proto(upper_dev)) != ETH_P_8021Q) {
4952 NL_SET_ERR_MSG_MOD(extack, "VLAN uppers are only supported with 802.1q VLAN protocol");
4953 return -EOPNOTSUPP;
4954 }
4955 if (is_vlan_dev(upper_dev) && mlxsw_sp_port->security) {
4956 NL_SET_ERR_MSG_MOD(extack, "VLAN uppers are not supported on a locked port");
4957 return -EOPNOTSUPP;
4958 }
4959 break;
4960 case NETDEV_CHANGEUPPER:
4961 upper_dev = info->upper_dev;
4962 if (netif_is_bridge_master(upper_dev)) {
4963 if (info->linking) {
4964 err = mlxsw_sp_port_bridge_join(mlxsw_sp_port,
4965 lower_dev,
4966 upper_dev,
4967 extack);
4968 } else {
4969 mlxsw_sp_port_bridge_leave(mlxsw_sp_port,
4970 lower_dev,
4971 upper_dev);
4972 if (!replay_deslavement)
4973 break;
4974 mlxsw_sp_netdevice_deslavement_replay(mlxsw_sp,
4975 lower_dev);
4976 }
4977 } else if (netif_is_lag_master(upper_dev)) {
4978 if (info->linking) {
4979 err = mlxsw_sp_port_lag_join(mlxsw_sp_port,
4980 upper_dev, extack);
4981 } else {
4982 mlxsw_sp_port_lag_col_dist_disable(mlxsw_sp_port);
4983 mlxsw_sp_port_lag_leave(mlxsw_sp_port,
4984 upper_dev);
4985 mlxsw_sp_netdevice_deslavement_replay(mlxsw_sp,
4986 dev);
4987 }
4988 } else if (netif_is_ovs_master(upper_dev)) {
4989 if (info->linking)
4990 err = mlxsw_sp_port_ovs_join(mlxsw_sp_port);
4991 else
4992 mlxsw_sp_port_ovs_leave(mlxsw_sp_port);
4993 } else if (netif_is_macvlan(upper_dev)) {
4994 if (!info->linking)
4995 mlxsw_sp_rif_macvlan_del(mlxsw_sp, upper_dev);
4996 } else if (is_vlan_dev(upper_dev)) {
4997 struct net_device *br_dev;
4998
4999 if (!netif_is_bridge_port(upper_dev))
5000 break;
5001 if (info->linking)
5002 break;
5003 br_dev = netdev_master_upper_dev_get(upper_dev);
5004 mlxsw_sp_port_bridge_leave(mlxsw_sp_port, upper_dev,
5005 br_dev);
5006 }
5007 break;
5008 }
5009
5010 return err;
5011 }
5012
mlxsw_sp_netdevice_port_lower_event(struct net_device * dev,unsigned long event,void * ptr)5013 static int mlxsw_sp_netdevice_port_lower_event(struct net_device *dev,
5014 unsigned long event, void *ptr)
5015 {
5016 struct netdev_notifier_changelowerstate_info *info;
5017 struct mlxsw_sp_port *mlxsw_sp_port;
5018 int err;
5019
5020 mlxsw_sp_port = netdev_priv(dev);
5021 info = ptr;
5022
5023 switch (event) {
5024 case NETDEV_CHANGELOWERSTATE:
5025 if (netif_is_lag_port(dev) && mlxsw_sp_port->lagged) {
5026 err = mlxsw_sp_port_lag_changed(mlxsw_sp_port,
5027 info->lower_state_info);
5028 if (err)
5029 netdev_err(dev, "Failed to reflect link aggregation lower state change\n");
5030 }
5031 break;
5032 }
5033
5034 return 0;
5035 }
5036
mlxsw_sp_netdevice_port_event(struct net_device * lower_dev,struct net_device * port_dev,unsigned long event,void * ptr,bool replay_deslavement)5037 static int mlxsw_sp_netdevice_port_event(struct net_device *lower_dev,
5038 struct net_device *port_dev,
5039 unsigned long event, void *ptr,
5040 bool replay_deslavement)
5041 {
5042 switch (event) {
5043 case NETDEV_PRECHANGEUPPER:
5044 case NETDEV_CHANGEUPPER:
5045 return mlxsw_sp_netdevice_port_upper_event(lower_dev, port_dev,
5046 event, ptr,
5047 replay_deslavement);
5048 case NETDEV_CHANGELOWERSTATE:
5049 return mlxsw_sp_netdevice_port_lower_event(port_dev, event,
5050 ptr);
5051 }
5052
5053 return 0;
5054 }
5055
5056 /* Called for LAG or its upper VLAN after the per-LAG-lower processing was done,
5057 * to do any per-LAG / per-LAG-upper processing.
5058 */
mlxsw_sp_netdevice_post_lag_event(struct net_device * dev,unsigned long event,void * ptr)5059 static int mlxsw_sp_netdevice_post_lag_event(struct net_device *dev,
5060 unsigned long event,
5061 void *ptr)
5062 {
5063 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(dev);
5064 struct netdev_notifier_changeupper_info *info = ptr;
5065
5066 if (!mlxsw_sp)
5067 return 0;
5068
5069 switch (event) {
5070 case NETDEV_CHANGEUPPER:
5071 if (info->linking)
5072 break;
5073 if (netif_is_bridge_master(info->upper_dev))
5074 mlxsw_sp_netdevice_deslavement_replay(mlxsw_sp, dev);
5075 break;
5076 }
5077 return 0;
5078 }
5079
mlxsw_sp_netdevice_lag_event(struct net_device * lag_dev,unsigned long event,void * ptr)5080 static int mlxsw_sp_netdevice_lag_event(struct net_device *lag_dev,
5081 unsigned long event, void *ptr)
5082 {
5083 struct net_device *dev;
5084 struct list_head *iter;
5085 int ret;
5086
5087 netdev_for_each_lower_dev(lag_dev, dev, iter) {
5088 if (mlxsw_sp_port_dev_check(dev)) {
5089 ret = mlxsw_sp_netdevice_port_event(lag_dev, dev, event,
5090 ptr, false);
5091 if (ret)
5092 return ret;
5093 }
5094 }
5095
5096 return mlxsw_sp_netdevice_post_lag_event(lag_dev, event, ptr);
5097 }
5098
mlxsw_sp_netdevice_port_vlan_event(struct net_device * vlan_dev,struct net_device * dev,unsigned long event,void * ptr,u16 vid,bool replay_deslavement)5099 static int mlxsw_sp_netdevice_port_vlan_event(struct net_device *vlan_dev,
5100 struct net_device *dev,
5101 unsigned long event, void *ptr,
5102 u16 vid, bool replay_deslavement)
5103 {
5104 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
5105 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
5106 struct netdev_notifier_changeupper_info *info = ptr;
5107 struct netlink_ext_ack *extack;
5108 struct net_device *upper_dev;
5109 int err = 0;
5110
5111 extack = netdev_notifier_info_to_extack(&info->info);
5112
5113 switch (event) {
5114 case NETDEV_PRECHANGEUPPER:
5115 upper_dev = info->upper_dev;
5116 if (!netif_is_bridge_master(upper_dev) &&
5117 !netif_is_macvlan(upper_dev) &&
5118 !netif_is_l3_master(upper_dev)) {
5119 NL_SET_ERR_MSG_MOD(extack, "Unknown upper device type");
5120 return -EINVAL;
5121 }
5122 if (!info->linking)
5123 break;
5124 if (netif_is_bridge_master(upper_dev) &&
5125 !mlxsw_sp_bridge_device_is_offloaded(mlxsw_sp, upper_dev) &&
5126 mlxsw_sp_bridge_has_vxlan(upper_dev) &&
5127 !mlxsw_sp_bridge_vxlan_is_valid(upper_dev, extack))
5128 return -EOPNOTSUPP;
5129 if (netdev_has_any_upper_dev(upper_dev) &&
5130 (!netif_is_bridge_master(upper_dev) ||
5131 !mlxsw_sp_bridge_device_is_offloaded(mlxsw_sp,
5132 upper_dev))) {
5133 err = mlxsw_sp_netdevice_validate_uppers(mlxsw_sp,
5134 upper_dev,
5135 extack);
5136 if (err)
5137 return err;
5138 }
5139 break;
5140 case NETDEV_CHANGEUPPER:
5141 upper_dev = info->upper_dev;
5142 if (netif_is_bridge_master(upper_dev)) {
5143 if (info->linking) {
5144 err = mlxsw_sp_port_bridge_join(mlxsw_sp_port,
5145 vlan_dev,
5146 upper_dev,
5147 extack);
5148 } else {
5149 mlxsw_sp_port_bridge_leave(mlxsw_sp_port,
5150 vlan_dev,
5151 upper_dev);
5152 if (!replay_deslavement)
5153 break;
5154 mlxsw_sp_netdevice_deslavement_replay(mlxsw_sp,
5155 vlan_dev);
5156 }
5157 } else if (netif_is_macvlan(upper_dev)) {
5158 if (!info->linking)
5159 mlxsw_sp_rif_macvlan_del(mlxsw_sp, upper_dev);
5160 }
5161 break;
5162 }
5163
5164 return err;
5165 }
5166
mlxsw_sp_netdevice_lag_port_vlan_event(struct net_device * vlan_dev,struct net_device * lag_dev,unsigned long event,void * ptr,u16 vid)5167 static int mlxsw_sp_netdevice_lag_port_vlan_event(struct net_device *vlan_dev,
5168 struct net_device *lag_dev,
5169 unsigned long event,
5170 void *ptr, u16 vid)
5171 {
5172 struct net_device *dev;
5173 struct list_head *iter;
5174 int ret;
5175
5176 netdev_for_each_lower_dev(lag_dev, dev, iter) {
5177 if (mlxsw_sp_port_dev_check(dev)) {
5178 ret = mlxsw_sp_netdevice_port_vlan_event(vlan_dev, dev,
5179 event, ptr,
5180 vid, false);
5181 if (ret)
5182 return ret;
5183 }
5184 }
5185
5186 return mlxsw_sp_netdevice_post_lag_event(vlan_dev, event, ptr);
5187 }
5188
mlxsw_sp_netdevice_bridge_vlan_event(struct mlxsw_sp * mlxsw_sp,struct net_device * vlan_dev,struct net_device * br_dev,unsigned long event,void * ptr,u16 vid,bool process_foreign)5189 static int mlxsw_sp_netdevice_bridge_vlan_event(struct mlxsw_sp *mlxsw_sp,
5190 struct net_device *vlan_dev,
5191 struct net_device *br_dev,
5192 unsigned long event, void *ptr,
5193 u16 vid, bool process_foreign)
5194 {
5195 struct netdev_notifier_changeupper_info *info = ptr;
5196 struct netlink_ext_ack *extack;
5197 struct net_device *upper_dev;
5198
5199 if (!process_foreign && !mlxsw_sp_lower_get(vlan_dev))
5200 return 0;
5201
5202 extack = netdev_notifier_info_to_extack(&info->info);
5203
5204 switch (event) {
5205 case NETDEV_PRECHANGEUPPER:
5206 upper_dev = info->upper_dev;
5207 if (!netif_is_macvlan(upper_dev) &&
5208 !netif_is_l3_master(upper_dev)) {
5209 NL_SET_ERR_MSG_MOD(extack, "Unknown upper device type");
5210 return -EOPNOTSUPP;
5211 }
5212 break;
5213 case NETDEV_CHANGEUPPER:
5214 upper_dev = info->upper_dev;
5215 if (info->linking)
5216 break;
5217 if (netif_is_macvlan(upper_dev))
5218 mlxsw_sp_rif_macvlan_del(mlxsw_sp, upper_dev);
5219 break;
5220 }
5221
5222 return 0;
5223 }
5224
mlxsw_sp_netdevice_vlan_event(struct mlxsw_sp * mlxsw_sp,struct net_device * vlan_dev,unsigned long event,void * ptr,bool process_foreign)5225 static int mlxsw_sp_netdevice_vlan_event(struct mlxsw_sp *mlxsw_sp,
5226 struct net_device *vlan_dev,
5227 unsigned long event, void *ptr,
5228 bool process_foreign)
5229 {
5230 struct net_device *real_dev = vlan_dev_real_dev(vlan_dev);
5231 u16 vid = vlan_dev_vlan_id(vlan_dev);
5232
5233 if (mlxsw_sp_port_dev_check(real_dev))
5234 return mlxsw_sp_netdevice_port_vlan_event(vlan_dev, real_dev,
5235 event, ptr, vid,
5236 true);
5237 else if (netif_is_lag_master(real_dev))
5238 return mlxsw_sp_netdevice_lag_port_vlan_event(vlan_dev,
5239 real_dev, event,
5240 ptr, vid);
5241 else if (netif_is_bridge_master(real_dev))
5242 return mlxsw_sp_netdevice_bridge_vlan_event(mlxsw_sp, vlan_dev,
5243 real_dev, event,
5244 ptr, vid,
5245 process_foreign);
5246
5247 return 0;
5248 }
5249
mlxsw_sp_netdevice_bridge_event(struct mlxsw_sp * mlxsw_sp,struct net_device * br_dev,unsigned long event,void * ptr,bool process_foreign)5250 static int mlxsw_sp_netdevice_bridge_event(struct mlxsw_sp *mlxsw_sp,
5251 struct net_device *br_dev,
5252 unsigned long event, void *ptr,
5253 bool process_foreign)
5254 {
5255 struct netdev_notifier_changeupper_info *info = ptr;
5256 struct netlink_ext_ack *extack;
5257 struct net_device *upper_dev;
5258 u16 proto;
5259
5260 if (!process_foreign && !mlxsw_sp_lower_get(br_dev))
5261 return 0;
5262
5263 extack = netdev_notifier_info_to_extack(&info->info);
5264
5265 switch (event) {
5266 case NETDEV_PRECHANGEUPPER:
5267 upper_dev = info->upper_dev;
5268 if (!is_vlan_dev(upper_dev) &&
5269 !netif_is_macvlan(upper_dev) &&
5270 !netif_is_l3_master(upper_dev)) {
5271 NL_SET_ERR_MSG_MOD(extack, "Unknown upper device type");
5272 return -EOPNOTSUPP;
5273 }
5274 if (!info->linking)
5275 break;
5276 if (br_vlan_enabled(br_dev)) {
5277 br_vlan_get_proto(br_dev, &proto);
5278 if (proto == ETH_P_8021AD) {
5279 NL_SET_ERR_MSG_MOD(extack, "Upper devices are not supported on top of an 802.1ad bridge");
5280 return -EOPNOTSUPP;
5281 }
5282 }
5283 if (is_vlan_dev(upper_dev) &&
5284 ntohs(vlan_dev_vlan_proto(upper_dev)) != ETH_P_8021Q) {
5285 NL_SET_ERR_MSG_MOD(extack, "VLAN uppers are only supported with 802.1q VLAN protocol");
5286 return -EOPNOTSUPP;
5287 }
5288 break;
5289 case NETDEV_CHANGEUPPER:
5290 upper_dev = info->upper_dev;
5291 if (info->linking)
5292 break;
5293 if (is_vlan_dev(upper_dev))
5294 mlxsw_sp_rif_destroy_by_dev(mlxsw_sp, upper_dev);
5295 if (netif_is_macvlan(upper_dev))
5296 mlxsw_sp_rif_macvlan_del(mlxsw_sp, upper_dev);
5297 break;
5298 }
5299
5300 return 0;
5301 }
5302
mlxsw_sp_netdevice_macvlan_event(struct net_device * macvlan_dev,unsigned long event,void * ptr)5303 static int mlxsw_sp_netdevice_macvlan_event(struct net_device *macvlan_dev,
5304 unsigned long event, void *ptr)
5305 {
5306 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(macvlan_dev);
5307 struct netdev_notifier_changeupper_info *info = ptr;
5308 struct netlink_ext_ack *extack;
5309 struct net_device *upper_dev;
5310
5311 if (!mlxsw_sp || event != NETDEV_PRECHANGEUPPER)
5312 return 0;
5313
5314 extack = netdev_notifier_info_to_extack(&info->info);
5315 upper_dev = info->upper_dev;
5316
5317 if (!netif_is_l3_master(upper_dev)) {
5318 NL_SET_ERR_MSG_MOD(extack, "Unknown upper device type");
5319 return -EOPNOTSUPP;
5320 }
5321
5322 return 0;
5323 }
5324
mlxsw_sp_netdevice_vxlan_event(struct mlxsw_sp * mlxsw_sp,struct net_device * dev,unsigned long event,void * ptr)5325 static int mlxsw_sp_netdevice_vxlan_event(struct mlxsw_sp *mlxsw_sp,
5326 struct net_device *dev,
5327 unsigned long event, void *ptr)
5328 {
5329 struct netdev_notifier_changeupper_info *cu_info;
5330 struct netdev_notifier_info *info = ptr;
5331 struct netlink_ext_ack *extack;
5332 struct net_device *upper_dev;
5333
5334 extack = netdev_notifier_info_to_extack(info);
5335
5336 switch (event) {
5337 case NETDEV_CHANGEUPPER:
5338 cu_info = container_of(info,
5339 struct netdev_notifier_changeupper_info,
5340 info);
5341 upper_dev = cu_info->upper_dev;
5342 if (!netif_is_bridge_master(upper_dev))
5343 return 0;
5344 if (!mlxsw_sp_lower_get(upper_dev))
5345 return 0;
5346 if (!mlxsw_sp_bridge_vxlan_is_valid(upper_dev, extack))
5347 return -EOPNOTSUPP;
5348 if (cu_info->linking) {
5349 if (!netif_running(dev))
5350 return 0;
5351 /* When the bridge is VLAN-aware, the VNI of the VxLAN
5352 * device needs to be mapped to a VLAN, but at this
5353 * point no VLANs are configured on the VxLAN device
5354 */
5355 if (br_vlan_enabled(upper_dev))
5356 return 0;
5357 return mlxsw_sp_bridge_vxlan_join(mlxsw_sp, upper_dev,
5358 dev, 0, extack);
5359 } else {
5360 /* VLANs were already flushed, which triggered the
5361 * necessary cleanup
5362 */
5363 if (br_vlan_enabled(upper_dev))
5364 return 0;
5365 mlxsw_sp_bridge_vxlan_leave(mlxsw_sp, dev);
5366 }
5367 break;
5368 case NETDEV_PRE_UP:
5369 upper_dev = netdev_master_upper_dev_get(dev);
5370 if (!upper_dev)
5371 return 0;
5372 if (!netif_is_bridge_master(upper_dev))
5373 return 0;
5374 if (!mlxsw_sp_lower_get(upper_dev))
5375 return 0;
5376 return mlxsw_sp_bridge_vxlan_join(mlxsw_sp, upper_dev, dev, 0,
5377 extack);
5378 case NETDEV_DOWN:
5379 upper_dev = netdev_master_upper_dev_get(dev);
5380 if (!upper_dev)
5381 return 0;
5382 if (!netif_is_bridge_master(upper_dev))
5383 return 0;
5384 if (!mlxsw_sp_lower_get(upper_dev))
5385 return 0;
5386 mlxsw_sp_bridge_vxlan_leave(mlxsw_sp, dev);
5387 break;
5388 }
5389
5390 return 0;
5391 }
5392
__mlxsw_sp_netdevice_event(struct mlxsw_sp * mlxsw_sp,unsigned long event,void * ptr,bool process_foreign)5393 static int __mlxsw_sp_netdevice_event(struct mlxsw_sp *mlxsw_sp,
5394 unsigned long event, void *ptr,
5395 bool process_foreign)
5396 {
5397 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
5398 struct mlxsw_sp_span_entry *span_entry;
5399 int err = 0;
5400
5401 if (event == NETDEV_UNREGISTER) {
5402 span_entry = mlxsw_sp_span_entry_find_by_port(mlxsw_sp, dev);
5403 if (span_entry)
5404 mlxsw_sp_span_entry_invalidate(mlxsw_sp, span_entry);
5405 }
5406
5407 if (netif_is_vxlan(dev))
5408 err = mlxsw_sp_netdevice_vxlan_event(mlxsw_sp, dev, event, ptr);
5409 else if (mlxsw_sp_port_dev_check(dev))
5410 err = mlxsw_sp_netdevice_port_event(dev, dev, event, ptr, true);
5411 else if (netif_is_lag_master(dev))
5412 err = mlxsw_sp_netdevice_lag_event(dev, event, ptr);
5413 else if (is_vlan_dev(dev))
5414 err = mlxsw_sp_netdevice_vlan_event(mlxsw_sp, dev, event, ptr,
5415 process_foreign);
5416 else if (netif_is_bridge_master(dev))
5417 err = mlxsw_sp_netdevice_bridge_event(mlxsw_sp, dev, event, ptr,
5418 process_foreign);
5419 else if (netif_is_macvlan(dev))
5420 err = mlxsw_sp_netdevice_macvlan_event(dev, event, ptr);
5421
5422 return err;
5423 }
5424
mlxsw_sp_netdevice_event(struct notifier_block * nb,unsigned long event,void * ptr)5425 static int mlxsw_sp_netdevice_event(struct notifier_block *nb,
5426 unsigned long event, void *ptr)
5427 {
5428 struct mlxsw_sp *mlxsw_sp;
5429 int err;
5430
5431 mlxsw_sp = container_of(nb, struct mlxsw_sp, netdevice_nb);
5432 mlxsw_sp_span_respin(mlxsw_sp);
5433 err = __mlxsw_sp_netdevice_event(mlxsw_sp, event, ptr, false);
5434
5435 return notifier_from_errno(err);
5436 }
5437
5438 static const struct pci_device_id mlxsw_sp1_pci_id_table[] = {
5439 {PCI_VDEVICE(MELLANOX, PCI_DEVICE_ID_MELLANOX_SPECTRUM), 0},
5440 {0, },
5441 };
5442
5443 static struct pci_driver mlxsw_sp1_pci_driver = {
5444 .name = mlxsw_sp1_driver_name,
5445 .id_table = mlxsw_sp1_pci_id_table,
5446 };
5447
5448 static const struct pci_device_id mlxsw_sp2_pci_id_table[] = {
5449 {PCI_VDEVICE(MELLANOX, PCI_DEVICE_ID_MELLANOX_SPECTRUM2), 0},
5450 {0, },
5451 };
5452
5453 static struct pci_driver mlxsw_sp2_pci_driver = {
5454 .name = mlxsw_sp2_driver_name,
5455 .id_table = mlxsw_sp2_pci_id_table,
5456 };
5457
5458 static const struct pci_device_id mlxsw_sp3_pci_id_table[] = {
5459 {PCI_VDEVICE(MELLANOX, PCI_DEVICE_ID_MELLANOX_SPECTRUM3), 0},
5460 {0, },
5461 };
5462
5463 static struct pci_driver mlxsw_sp3_pci_driver = {
5464 .name = mlxsw_sp3_driver_name,
5465 .id_table = mlxsw_sp3_pci_id_table,
5466 };
5467
5468 static const struct pci_device_id mlxsw_sp4_pci_id_table[] = {
5469 {PCI_VDEVICE(MELLANOX, PCI_DEVICE_ID_MELLANOX_SPECTRUM4), 0},
5470 {0, },
5471 };
5472
5473 static struct pci_driver mlxsw_sp4_pci_driver = {
5474 .name = mlxsw_sp4_driver_name,
5475 .id_table = mlxsw_sp4_pci_id_table,
5476 };
5477
mlxsw_sp_module_init(void)5478 static int __init mlxsw_sp_module_init(void)
5479 {
5480 int err;
5481
5482 err = mlxsw_core_driver_register(&mlxsw_sp1_driver);
5483 if (err)
5484 return err;
5485
5486 err = mlxsw_core_driver_register(&mlxsw_sp2_driver);
5487 if (err)
5488 goto err_sp2_core_driver_register;
5489
5490 err = mlxsw_core_driver_register(&mlxsw_sp3_driver);
5491 if (err)
5492 goto err_sp3_core_driver_register;
5493
5494 err = mlxsw_core_driver_register(&mlxsw_sp4_driver);
5495 if (err)
5496 goto err_sp4_core_driver_register;
5497
5498 err = mlxsw_pci_driver_register(&mlxsw_sp1_pci_driver);
5499 if (err)
5500 goto err_sp1_pci_driver_register;
5501
5502 err = mlxsw_pci_driver_register(&mlxsw_sp2_pci_driver);
5503 if (err)
5504 goto err_sp2_pci_driver_register;
5505
5506 err = mlxsw_pci_driver_register(&mlxsw_sp3_pci_driver);
5507 if (err)
5508 goto err_sp3_pci_driver_register;
5509
5510 err = mlxsw_pci_driver_register(&mlxsw_sp4_pci_driver);
5511 if (err)
5512 goto err_sp4_pci_driver_register;
5513
5514 return 0;
5515
5516 err_sp4_pci_driver_register:
5517 mlxsw_pci_driver_unregister(&mlxsw_sp3_pci_driver);
5518 err_sp3_pci_driver_register:
5519 mlxsw_pci_driver_unregister(&mlxsw_sp2_pci_driver);
5520 err_sp2_pci_driver_register:
5521 mlxsw_pci_driver_unregister(&mlxsw_sp1_pci_driver);
5522 err_sp1_pci_driver_register:
5523 mlxsw_core_driver_unregister(&mlxsw_sp4_driver);
5524 err_sp4_core_driver_register:
5525 mlxsw_core_driver_unregister(&mlxsw_sp3_driver);
5526 err_sp3_core_driver_register:
5527 mlxsw_core_driver_unregister(&mlxsw_sp2_driver);
5528 err_sp2_core_driver_register:
5529 mlxsw_core_driver_unregister(&mlxsw_sp1_driver);
5530 return err;
5531 }
5532
mlxsw_sp_module_exit(void)5533 static void __exit mlxsw_sp_module_exit(void)
5534 {
5535 mlxsw_pci_driver_unregister(&mlxsw_sp4_pci_driver);
5536 mlxsw_pci_driver_unregister(&mlxsw_sp3_pci_driver);
5537 mlxsw_pci_driver_unregister(&mlxsw_sp2_pci_driver);
5538 mlxsw_pci_driver_unregister(&mlxsw_sp1_pci_driver);
5539 mlxsw_core_driver_unregister(&mlxsw_sp4_driver);
5540 mlxsw_core_driver_unregister(&mlxsw_sp3_driver);
5541 mlxsw_core_driver_unregister(&mlxsw_sp2_driver);
5542 mlxsw_core_driver_unregister(&mlxsw_sp1_driver);
5543 }
5544
5545 module_init(mlxsw_sp_module_init);
5546 module_exit(mlxsw_sp_module_exit);
5547
5548 MODULE_LICENSE("Dual BSD/GPL");
5549 MODULE_AUTHOR("Jiri Pirko <jiri@mellanox.com>");
5550 MODULE_DESCRIPTION("Mellanox Spectrum driver");
5551 MODULE_DEVICE_TABLE(pci, mlxsw_sp1_pci_id_table);
5552 MODULE_DEVICE_TABLE(pci, mlxsw_sp2_pci_id_table);
5553 MODULE_DEVICE_TABLE(pci, mlxsw_sp3_pci_id_table);
5554 MODULE_DEVICE_TABLE(pci, mlxsw_sp4_pci_id_table);
5555 MODULE_FIRMWARE(MLXSW_SP1_FW_FILENAME);
5556 MODULE_FIRMWARE(MLXSW_SP2_FW_FILENAME);
5557 MODULE_FIRMWARE(MLXSW_SP3_FW_FILENAME);
5558 MODULE_FIRMWARE(MLXSW_SP_LINECARDS_INI_BUNDLE_FILENAME);
5559