2 * drivers/net/ethernet/mellanox/mlxsw/switchib.c
3 * Copyright (c) 2016 Mellanox Technologies. All rights reserved.
4 * Copyright (c) 2016 Elad Raz <eladr@mellanox.com>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the names of the copyright holders nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * Alternatively, this software may be distributed under the terms of the
19 * GNU General Public License ("GPL") version 2 as published by the Free
20 * Software Foundation.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
35 #include <linux/kernel.h>
36 #include <linux/module.h>
37 #include <linux/types.h>
38 #include <linux/pci.h>
39 #include <linux/netdevice.h>
40 #include <linux/etherdevice.h>
41 #include <linux/slab.h>
42 #include <linux/device.h>
43 #include <linux/skbuff.h>
44 #include <linux/if_vlan.h>
45 #include <net/switchdev.h>
55 static const char mlxsw_sib_driver_name
[] = "mlxsw_switchib";
56 static const char mlxsw_sib2_driver_name
[] = "mlxsw_switchib2";
58 struct mlxsw_sib_port
;
61 struct mlxsw_sib_port
**ports
;
62 struct mlxsw_core
*core
;
63 const struct mlxsw_bus_info
*bus_info
;
66 struct mlxsw_sib_port
{
67 struct mlxsw_sib
*mlxsw_sib
;
78 MLXSW_ITEM32(tx_v1
, hdr
, version
, 0x00, 28, 4);
81 * Packet control type.
82 * 0 - Ethernet control (e.g. EMADs, LACP)
85 MLXSW_ITEM32(tx_v1
, hdr
, ctl
, 0x00, 26, 2);
88 * Packet protocol type. Must be set to 1 (Ethernet).
90 MLXSW_ITEM32(tx_v1
, hdr
, proto
, 0x00, 21, 3);
93 * Switch partition ID. Must be set to 0.
95 MLXSW_ITEM32(tx_v1
, hdr
, swid
, 0x00, 12, 3);
97 /* tx_v1_hdr_control_tclass
98 * Indicates if the packet should use the control TClass and not one
99 * of the data TClasses.
101 MLXSW_ITEM32(tx_v1
, hdr
, control_tclass
, 0x00, 6, 1);
103 /* tx_v1_hdr_port_mid
104 * Destination local port for unicast packets.
105 * Destination multicast ID for multicast packets.
107 * Control packets are directed to a specific egress port, while data
108 * packets are transmitted through the CPU port (0) into the switch partition,
109 * where forwarding rules are applied.
111 MLXSW_ITEM32(tx_v1
, hdr
, port_mid
, 0x04, 16, 16);
115 * 6 - Control packets
117 MLXSW_ITEM32(tx_v1
, hdr
, type
, 0x0C, 0, 4);
120 mlxsw_sib_tx_v1_hdr_construct(struct sk_buff
*skb
,
121 const struct mlxsw_tx_info
*tx_info
)
123 char *txhdr
= skb_push(skb
, MLXSW_TXHDR_LEN
);
125 memset(txhdr
, 0, MLXSW_TXHDR_LEN
);
127 mlxsw_tx_v1_hdr_version_set(txhdr
, MLXSW_TXHDR_VERSION_1
);
128 mlxsw_tx_v1_hdr_ctl_set(txhdr
, MLXSW_TXHDR_ETH_CTL
);
129 mlxsw_tx_v1_hdr_proto_set(txhdr
, MLXSW_TXHDR_PROTO_ETH
);
130 mlxsw_tx_v1_hdr_swid_set(txhdr
, 0);
131 mlxsw_tx_v1_hdr_control_tclass_set(txhdr
, 1);
132 mlxsw_tx_v1_hdr_port_mid_set(txhdr
, tx_info
->local_port
);
133 mlxsw_tx_v1_hdr_type_set(txhdr
, MLXSW_TXHDR_TYPE_CONTROL
);
137 mlxsw_sib_port_admin_status_set(struct mlxsw_sib_port
*mlxsw_sib_port
,
140 struct mlxsw_sib
*mlxsw_sib
= mlxsw_sib_port
->mlxsw_sib
;
141 char paos_pl
[MLXSW_REG_PAOS_LEN
];
143 mlxsw_reg_paos_pack(paos_pl
, mlxsw_sib_port
->local_port
,
144 is_up
? MLXSW_PORT_ADMIN_STATUS_UP
:
145 MLXSW_PORT_ADMIN_STATUS_DOWN
);
146 return mlxsw_reg_write(mlxsw_sib
->core
, MLXSW_REG(paos
), paos_pl
);
149 static int mlxsw_sib_port_mtu_set(struct mlxsw_sib_port
*mlxsw_sib_port
,
152 struct mlxsw_sib
*mlxsw_sib
= mlxsw_sib_port
->mlxsw_sib
;
153 char pmtu_pl
[MLXSW_REG_PMTU_LEN
];
157 mlxsw_reg_pmtu_pack(pmtu_pl
, mlxsw_sib_port
->local_port
, 0);
158 err
= mlxsw_reg_query(mlxsw_sib
->core
, MLXSW_REG(pmtu
), pmtu_pl
);
161 max_mtu
= mlxsw_reg_pmtu_max_mtu_get(pmtu_pl
);
166 mlxsw_reg_pmtu_pack(pmtu_pl
, mlxsw_sib_port
->local_port
, mtu
);
167 return mlxsw_reg_write(mlxsw_sib
->core
, MLXSW_REG(pmtu
), pmtu_pl
);
170 static int mlxsw_sib_port_set(struct mlxsw_sib_port
*mlxsw_sib_port
, u8 port
)
172 struct mlxsw_sib
*mlxsw_sib
= mlxsw_sib_port
->mlxsw_sib
;
173 char plib_pl
[MLXSW_REG_PLIB_LEN
] = {0};
176 mlxsw_reg_plib_local_port_set(plib_pl
, mlxsw_sib_port
->local_port
);
177 mlxsw_reg_plib_ib_port_set(plib_pl
, port
);
178 err
= mlxsw_reg_write(mlxsw_sib
->core
, MLXSW_REG(plib
), plib_pl
);
182 static int mlxsw_sib_port_swid_set(struct mlxsw_sib_port
*mlxsw_sib_port
,
185 struct mlxsw_sib
*mlxsw_sib
= mlxsw_sib_port
->mlxsw_sib
;
186 char pspa_pl
[MLXSW_REG_PSPA_LEN
];
188 mlxsw_reg_pspa_pack(pspa_pl
, swid
, mlxsw_sib_port
->local_port
);
189 return mlxsw_reg_write(mlxsw_sib
->core
, MLXSW_REG(pspa
), pspa_pl
);
192 static int mlxsw_sib_port_module_info_get(struct mlxsw_sib
*mlxsw_sib
,
193 u8 local_port
, u8
*p_module
,
196 char pmlp_pl
[MLXSW_REG_PMLP_LEN
];
199 mlxsw_reg_pmlp_pack(pmlp_pl
, local_port
);
200 err
= mlxsw_reg_query(mlxsw_sib
->core
, MLXSW_REG(pmlp
), pmlp_pl
);
203 *p_module
= mlxsw_reg_pmlp_module_get(pmlp_pl
, 0);
204 *p_width
= mlxsw_reg_pmlp_width_get(pmlp_pl
);
208 static int mlxsw_sib_port_speed_set(struct mlxsw_sib_port
*mlxsw_sib_port
,
209 u16 speed
, u16 width
)
211 struct mlxsw_sib
*mlxsw_sib
= mlxsw_sib_port
->mlxsw_sib
;
212 char ptys_pl
[MLXSW_REG_PTYS_LEN
];
214 mlxsw_reg_ptys_ib_pack(ptys_pl
, mlxsw_sib_port
->local_port
, speed
,
216 return mlxsw_reg_write(mlxsw_sib
->core
, MLXSW_REG(ptys
), ptys_pl
);
219 static bool mlxsw_sib_port_created(struct mlxsw_sib
*mlxsw_sib
, u8 local_port
)
221 return mlxsw_sib
->ports
[local_port
] != NULL
;
224 static int __mlxsw_sib_port_create(struct mlxsw_sib
*mlxsw_sib
, u8 local_port
,
227 struct mlxsw_sib_port
*mlxsw_sib_port
;
230 mlxsw_sib_port
= kzalloc(sizeof(*mlxsw_sib_port
), GFP_KERNEL
);
233 mlxsw_sib_port
->mlxsw_sib
= mlxsw_sib
;
234 mlxsw_sib_port
->local_port
= local_port
;
235 mlxsw_sib_port
->mapping
.module
= module
;
237 err
= mlxsw_sib_port_swid_set(mlxsw_sib_port
, 0);
239 dev_err(mlxsw_sib
->bus_info
->dev
, "Port %d: Failed to set SWID\n",
240 mlxsw_sib_port
->local_port
);
241 goto err_port_swid_set
;
244 /* Expose the IB port number as it's front panel name */
245 err
= mlxsw_sib_port_set(mlxsw_sib_port
, module
+ 1);
247 dev_err(mlxsw_sib
->bus_info
->dev
, "Port %d: Failed to set IB port\n",
248 mlxsw_sib_port
->local_port
);
249 goto err_port_ib_set
;
252 /* Supports all speeds from SDR to FDR (bitmask) and support bus width
253 * of 1x, 2x and 4x (3 bits bitmask)
255 err
= mlxsw_sib_port_speed_set(mlxsw_sib_port
,
256 MLXSW_REG_PTYS_IB_SPEED_EDR
- 1,
259 dev_err(mlxsw_sib
->bus_info
->dev
, "Port %d: Failed to set speed\n",
260 mlxsw_sib_port
->local_port
);
261 goto err_port_speed_set
;
264 /* Change to the maximum MTU the device supports, the SMA will take
265 * care of the active MTU
267 err
= mlxsw_sib_port_mtu_set(mlxsw_sib_port
, MLXSW_IB_DEFAULT_MTU
);
269 dev_err(mlxsw_sib
->bus_info
->dev
, "Port %d: Failed to set MTU\n",
270 mlxsw_sib_port
->local_port
);
271 goto err_port_mtu_set
;
274 err
= mlxsw_sib_port_admin_status_set(mlxsw_sib_port
, true);
276 dev_err(mlxsw_sib
->bus_info
->dev
, "Port %d: Failed to change admin state to UP\n",
277 mlxsw_sib_port
->local_port
);
278 goto err_port_admin_set
;
281 mlxsw_core_port_ib_set(mlxsw_sib
->core
, mlxsw_sib_port
->local_port
,
283 mlxsw_sib
->ports
[local_port
] = mlxsw_sib_port
;
290 mlxsw_sib_port_swid_set(mlxsw_sib_port
, MLXSW_PORT_SWID_DISABLED_PORT
);
292 kfree(mlxsw_sib_port
);
296 static int mlxsw_sib_port_create(struct mlxsw_sib
*mlxsw_sib
, u8 local_port
,
301 err
= mlxsw_core_port_init(mlxsw_sib
->core
, local_port
);
303 dev_err(mlxsw_sib
->bus_info
->dev
, "Port %d: Failed to init core port\n",
307 err
= __mlxsw_sib_port_create(mlxsw_sib
, local_port
, module
, width
);
309 goto err_port_create
;
314 mlxsw_core_port_fini(mlxsw_sib
->core
, local_port
);
318 static void __mlxsw_sib_port_remove(struct mlxsw_sib
*mlxsw_sib
, u8 local_port
)
320 struct mlxsw_sib_port
*mlxsw_sib_port
= mlxsw_sib
->ports
[local_port
];
322 mlxsw_core_port_clear(mlxsw_sib
->core
, local_port
, mlxsw_sib
);
323 mlxsw_sib
->ports
[local_port
] = NULL
;
324 mlxsw_sib_port_admin_status_set(mlxsw_sib_port
, false);
325 mlxsw_sib_port_swid_set(mlxsw_sib_port
, MLXSW_PORT_SWID_DISABLED_PORT
);
326 kfree(mlxsw_sib_port
);
329 static void mlxsw_sib_port_remove(struct mlxsw_sib
*mlxsw_sib
, u8 local_port
)
331 __mlxsw_sib_port_remove(mlxsw_sib
, local_port
);
332 mlxsw_core_port_fini(mlxsw_sib
->core
, local_port
);
335 static void mlxsw_sib_ports_remove(struct mlxsw_sib
*mlxsw_sib
)
339 for (i
= 1; i
< MLXSW_PORT_MAX_IB_PORTS
; i
++)
340 if (mlxsw_sib_port_created(mlxsw_sib
, i
))
341 mlxsw_sib_port_remove(mlxsw_sib
, i
);
342 kfree(mlxsw_sib
->ports
);
345 static int mlxsw_sib_ports_create(struct mlxsw_sib
*mlxsw_sib
)
352 alloc_size
= sizeof(struct mlxsw_sib_port
*) * MLXSW_PORT_MAX_IB_PORTS
;
353 mlxsw_sib
->ports
= kzalloc(alloc_size
, GFP_KERNEL
);
354 if (!mlxsw_sib
->ports
)
357 for (i
= 1; i
< MLXSW_PORT_MAX_IB_PORTS
; i
++) {
358 err
= mlxsw_sib_port_module_info_get(mlxsw_sib
, i
, &module
,
361 goto err_port_module_info_get
;
364 err
= mlxsw_sib_port_create(mlxsw_sib
, i
, module
, width
);
366 goto err_port_create
;
371 err_port_module_info_get
:
372 for (i
--; i
>= 1; i
--)
373 if (mlxsw_sib_port_created(mlxsw_sib
, i
))
374 mlxsw_sib_port_remove(mlxsw_sib
, i
);
375 kfree(mlxsw_sib
->ports
);
380 mlxsw_sib_pude_ib_event_func(struct mlxsw_sib_port
*mlxsw_sib_port
,
381 enum mlxsw_reg_pude_oper_status status
)
383 if (status
== MLXSW_PORT_OPER_STATUS_UP
)
384 pr_info("ib link for port %d - up\n",
385 mlxsw_sib_port
->mapping
.module
+ 1);
387 pr_info("ib link for port %d - down\n",
388 mlxsw_sib_port
->mapping
.module
+ 1);
391 static void mlxsw_sib_pude_event_func(const struct mlxsw_reg_info
*reg
,
392 char *pude_pl
, void *priv
)
394 struct mlxsw_sib
*mlxsw_sib
= priv
;
395 struct mlxsw_sib_port
*mlxsw_sib_port
;
396 enum mlxsw_reg_pude_oper_status status
;
399 local_port
= mlxsw_reg_pude_local_port_get(pude_pl
);
400 mlxsw_sib_port
= mlxsw_sib
->ports
[local_port
];
401 if (!mlxsw_sib_port
) {
402 dev_warn(mlxsw_sib
->bus_info
->dev
, "Port %d: Link event received for non-existent port\n",
407 status
= mlxsw_reg_pude_oper_status_get(pude_pl
);
408 mlxsw_sib_pude_ib_event_func(mlxsw_sib_port
, status
);
411 static const struct mlxsw_listener mlxsw_sib_listener
[] = {
412 MLXSW_EVENTL(mlxsw_sib_pude_event_func
, PUDE
, EMAD
),
415 static int mlxsw_sib_taps_init(struct mlxsw_sib
*mlxsw_sib
)
420 for (i
= 0; i
< ARRAY_SIZE(mlxsw_sib_listener
); i
++) {
421 err
= mlxsw_core_trap_register(mlxsw_sib
->core
,
422 &mlxsw_sib_listener
[i
],
425 goto err_rx_listener_register
;
430 err_rx_listener_register
:
431 for (i
--; i
>= 0; i
--) {
432 mlxsw_core_trap_unregister(mlxsw_sib
->core
,
433 &mlxsw_sib_listener
[i
],
440 static void mlxsw_sib_traps_fini(struct mlxsw_sib
*mlxsw_sib
)
444 for (i
= 0; i
< ARRAY_SIZE(mlxsw_sib_listener
); i
++) {
445 mlxsw_core_trap_unregister(mlxsw_sib
->core
,
446 &mlxsw_sib_listener
[i
], mlxsw_sib
);
450 static int mlxsw_sib_basic_trap_groups_set(struct mlxsw_core
*mlxsw_core
)
452 char htgt_pl
[MLXSW_REG_HTGT_LEN
];
454 mlxsw_reg_htgt_pack(htgt_pl
, MLXSW_REG_HTGT_TRAP_GROUP_EMAD
,
455 MLXSW_REG_HTGT_INVALID_POLICER
,
456 MLXSW_REG_HTGT_DEFAULT_PRIORITY
,
457 MLXSW_REG_HTGT_DEFAULT_TC
);
458 mlxsw_reg_htgt_swid_set(htgt_pl
, MLXSW_PORT_SWID_ALL_SWIDS
);
459 mlxsw_reg_htgt_local_path_rdq_set(htgt_pl
,
460 MLXSW_REG_HTGT_LOCAL_PATH_RDQ_SIB_EMAD
);
461 return mlxsw_reg_write(mlxsw_core
, MLXSW_REG(htgt
), htgt_pl
);
464 static int mlxsw_sib_init(struct mlxsw_core
*mlxsw_core
,
465 const struct mlxsw_bus_info
*mlxsw_bus_info
)
467 struct mlxsw_sib
*mlxsw_sib
= mlxsw_core_driver_priv(mlxsw_core
);
470 mlxsw_sib
->core
= mlxsw_core
;
471 mlxsw_sib
->bus_info
= mlxsw_bus_info
;
473 err
= mlxsw_sib_ports_create(mlxsw_sib
);
475 dev_err(mlxsw_sib
->bus_info
->dev
, "Failed to create ports\n");
479 err
= mlxsw_sib_taps_init(mlxsw_sib
);
481 dev_err(mlxsw_sib
->bus_info
->dev
, "Failed to set traps\n");
482 goto err_traps_init_err
;
488 mlxsw_sib_ports_remove(mlxsw_sib
);
492 static void mlxsw_sib_fini(struct mlxsw_core
*mlxsw_core
)
494 struct mlxsw_sib
*mlxsw_sib
= mlxsw_core_driver_priv(mlxsw_core
);
496 mlxsw_sib_traps_fini(mlxsw_sib
);
497 mlxsw_sib_ports_remove(mlxsw_sib
);
500 static const struct mlxsw_config_profile mlxsw_sib_config_profile
= {
501 .used_max_system_port
= 1,
502 .max_system_port
= 48000,
510 .type
= MLXSW_PORT_SWID_TYPE_IB
,
513 .resource_query_enable
= 0,
516 static struct mlxsw_driver mlxsw_sib_driver
= {
517 .kind
= mlxsw_sib_driver_name
,
518 .priv_size
= sizeof(struct mlxsw_sib
),
519 .init
= mlxsw_sib_init
,
520 .fini
= mlxsw_sib_fini
,
521 .basic_trap_groups_set
= mlxsw_sib_basic_trap_groups_set
,
522 .txhdr_construct
= mlxsw_sib_tx_v1_hdr_construct
,
523 .txhdr_len
= MLXSW_TXHDR_LEN
,
524 .profile
= &mlxsw_sib_config_profile
,
527 static struct mlxsw_driver mlxsw_sib2_driver
= {
528 .kind
= mlxsw_sib2_driver_name
,
529 .priv_size
= sizeof(struct mlxsw_sib
),
530 .init
= mlxsw_sib_init
,
531 .fini
= mlxsw_sib_fini
,
532 .basic_trap_groups_set
= mlxsw_sib_basic_trap_groups_set
,
533 .txhdr_construct
= mlxsw_sib_tx_v1_hdr_construct
,
534 .txhdr_len
= MLXSW_TXHDR_LEN
,
535 .profile
= &mlxsw_sib_config_profile
,
538 static const struct pci_device_id mlxsw_sib_pci_id_table
[] = {
539 {PCI_VDEVICE(MELLANOX
, PCI_DEVICE_ID_MELLANOX_SWITCHIB
), 0},
543 static struct pci_driver mlxsw_sib_pci_driver
= {
544 .name
= mlxsw_sib_driver_name
,
545 .id_table
= mlxsw_sib_pci_id_table
,
548 static const struct pci_device_id mlxsw_sib2_pci_id_table
[] = {
549 {PCI_VDEVICE(MELLANOX
, PCI_DEVICE_ID_MELLANOX_SWITCHIB2
), 0},
553 static struct pci_driver mlxsw_sib2_pci_driver
= {
554 .name
= mlxsw_sib2_driver_name
,
555 .id_table
= mlxsw_sib2_pci_id_table
,
558 static int __init
mlxsw_sib_module_init(void)
562 err
= mlxsw_core_driver_register(&mlxsw_sib_driver
);
566 err
= mlxsw_core_driver_register(&mlxsw_sib2_driver
);
568 goto err_sib2_driver_register
;
570 err
= mlxsw_pci_driver_register(&mlxsw_sib_pci_driver
);
572 goto err_sib_pci_driver_register
;
574 err
= mlxsw_pci_driver_register(&mlxsw_sib2_pci_driver
);
576 goto err_sib2_pci_driver_register
;
580 err_sib2_pci_driver_register
:
581 mlxsw_pci_driver_unregister(&mlxsw_sib_pci_driver
);
582 err_sib_pci_driver_register
:
583 mlxsw_core_driver_unregister(&mlxsw_sib2_driver
);
584 err_sib2_driver_register
:
585 mlxsw_core_driver_unregister(&mlxsw_sib_driver
);
589 static void __exit
mlxsw_sib_module_exit(void)
591 mlxsw_pci_driver_unregister(&mlxsw_sib2_pci_driver
);
592 mlxsw_pci_driver_unregister(&mlxsw_sib_pci_driver
);
593 mlxsw_core_driver_unregister(&mlxsw_sib2_driver
);
594 mlxsw_core_driver_unregister(&mlxsw_sib_driver
);
597 module_init(mlxsw_sib_module_init
);
598 module_exit(mlxsw_sib_module_exit
);
600 MODULE_LICENSE("Dual BSD/GPL");
601 MODULE_AUTHOR("Elad Raz <eladr@@mellanox.com>");
602 MODULE_DESCRIPTION("Mellanox SwitchIB and SwitchIB-2 driver");
603 MODULE_ALIAS("mlxsw_switchib2");
604 MODULE_DEVICE_TABLE(pci
, mlxsw_sib_pci_id_table
);
605 MODULE_DEVICE_TABLE(pci
, mlxsw_sib2_pci_id_table
);