Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1 | /* |
| 2 | * drivers/net/ethernet/mellanox/mlxsw/spectrum.c |
| 3 | * Copyright (c) 2015 Mellanox Technologies. All rights reserved. |
| 4 | * Copyright (c) 2015 Jiri Pirko <jiri@mellanox.com> |
| 5 | * Copyright (c) 2015 Ido Schimmel <idosch@mellanox.com> |
| 6 | * Copyright (c) 2015 Elad Raz <eladr@mellanox.com> |
| 7 | * |
| 8 | * Redistribution and use in source and binary forms, with or without |
| 9 | * modification, are permitted provided that the following conditions are met: |
| 10 | * |
| 11 | * 1. Redistributions of source code must retain the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer. |
| 13 | * 2. Redistributions in binary form must reproduce the above copyright |
| 14 | * notice, this list of conditions and the following disclaimer in the |
| 15 | * documentation and/or other materials provided with the distribution. |
| 16 | * 3. Neither the names of the copyright holders nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
| 19 | * |
| 20 | * Alternatively, this software may be distributed under the terms of the |
| 21 | * GNU General Public License ("GPL") version 2 as published by the Free |
| 22 | * Software Foundation. |
| 23 | * |
| 24 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 25 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 27 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 28 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 29 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 30 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 31 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 32 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 33 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 34 | * POSSIBILITY OF SUCH DAMAGE. |
| 35 | */ |
| 36 | |
| 37 | #include <linux/kernel.h> |
| 38 | #include <linux/module.h> |
| 39 | #include <linux/types.h> |
| 40 | #include <linux/netdevice.h> |
| 41 | #include <linux/etherdevice.h> |
| 42 | #include <linux/ethtool.h> |
| 43 | #include <linux/slab.h> |
| 44 | #include <linux/device.h> |
| 45 | #include <linux/skbuff.h> |
| 46 | #include <linux/if_vlan.h> |
| 47 | #include <linux/if_bridge.h> |
| 48 | #include <linux/workqueue.h> |
| 49 | #include <linux/jiffies.h> |
| 50 | #include <linux/bitops.h> |
Ido Schimmel | 7f71eb4 | 2015-12-15 16:03:37 +0100 | [diff] [blame] | 51 | #include <linux/list.h> |
Ido Schimmel | 90183b9 | 2016-04-06 17:10:08 +0200 | [diff] [blame] | 52 | #include <linux/dcbnl.h> |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 53 | #include <net/switchdev.h> |
| 54 | #include <generated/utsrelease.h> |
| 55 | |
| 56 | #include "spectrum.h" |
| 57 | #include "core.h" |
| 58 | #include "reg.h" |
| 59 | #include "port.h" |
| 60 | #include "trap.h" |
| 61 | #include "txheader.h" |
| 62 | |
| 63 | static const char mlxsw_sp_driver_name[] = "mlxsw_spectrum"; |
| 64 | static const char mlxsw_sp_driver_version[] = "1.0"; |
| 65 | |
| 66 | /* tx_hdr_version |
| 67 | * Tx header version. |
| 68 | * Must be set to 1. |
| 69 | */ |
| 70 | MLXSW_ITEM32(tx, hdr, version, 0x00, 28, 4); |
| 71 | |
| 72 | /* tx_hdr_ctl |
| 73 | * Packet control type. |
| 74 | * 0 - Ethernet control (e.g. EMADs, LACP) |
| 75 | * 1 - Ethernet data |
| 76 | */ |
| 77 | MLXSW_ITEM32(tx, hdr, ctl, 0x00, 26, 2); |
| 78 | |
| 79 | /* tx_hdr_proto |
| 80 | * Packet protocol type. Must be set to 1 (Ethernet). |
| 81 | */ |
| 82 | MLXSW_ITEM32(tx, hdr, proto, 0x00, 21, 3); |
| 83 | |
| 84 | /* tx_hdr_rx_is_router |
| 85 | * Packet is sent from the router. Valid for data packets only. |
| 86 | */ |
| 87 | MLXSW_ITEM32(tx, hdr, rx_is_router, 0x00, 19, 1); |
| 88 | |
| 89 | /* tx_hdr_fid_valid |
| 90 | * Indicates if the 'fid' field is valid and should be used for |
| 91 | * forwarding lookup. Valid for data packets only. |
| 92 | */ |
| 93 | MLXSW_ITEM32(tx, hdr, fid_valid, 0x00, 16, 1); |
| 94 | |
| 95 | /* tx_hdr_swid |
| 96 | * Switch partition ID. Must be set to 0. |
| 97 | */ |
| 98 | MLXSW_ITEM32(tx, hdr, swid, 0x00, 12, 3); |
| 99 | |
| 100 | /* tx_hdr_control_tclass |
| 101 | * Indicates if the packet should use the control TClass and not one |
| 102 | * of the data TClasses. |
| 103 | */ |
| 104 | MLXSW_ITEM32(tx, hdr, control_tclass, 0x00, 6, 1); |
| 105 | |
| 106 | /* tx_hdr_etclass |
| 107 | * Egress TClass to be used on the egress device on the egress port. |
| 108 | */ |
| 109 | MLXSW_ITEM32(tx, hdr, etclass, 0x00, 0, 4); |
| 110 | |
| 111 | /* tx_hdr_port_mid |
| 112 | * Destination local port for unicast packets. |
| 113 | * Destination multicast ID for multicast packets. |
| 114 | * |
| 115 | * Control packets are directed to a specific egress port, while data |
| 116 | * packets are transmitted through the CPU port (0) into the switch partition, |
| 117 | * where forwarding rules are applied. |
| 118 | */ |
| 119 | MLXSW_ITEM32(tx, hdr, port_mid, 0x04, 16, 16); |
| 120 | |
| 121 | /* tx_hdr_fid |
| 122 | * Forwarding ID used for L2 forwarding lookup. Valid only if 'fid_valid' is |
| 123 | * set, otherwise calculated based on the packet's VID using VID to FID mapping. |
| 124 | * Valid for data packets only. |
| 125 | */ |
| 126 | MLXSW_ITEM32(tx, hdr, fid, 0x08, 0, 16); |
| 127 | |
| 128 | /* tx_hdr_type |
| 129 | * 0 - Data packets |
| 130 | * 6 - Control packets |
| 131 | */ |
| 132 | MLXSW_ITEM32(tx, hdr, type, 0x0C, 0, 4); |
| 133 | |
| 134 | static void mlxsw_sp_txhdr_construct(struct sk_buff *skb, |
| 135 | const struct mlxsw_tx_info *tx_info) |
| 136 | { |
| 137 | char *txhdr = skb_push(skb, MLXSW_TXHDR_LEN); |
| 138 | |
| 139 | memset(txhdr, 0, MLXSW_TXHDR_LEN); |
| 140 | |
| 141 | mlxsw_tx_hdr_version_set(txhdr, MLXSW_TXHDR_VERSION_1); |
| 142 | mlxsw_tx_hdr_ctl_set(txhdr, MLXSW_TXHDR_ETH_CTL); |
| 143 | mlxsw_tx_hdr_proto_set(txhdr, MLXSW_TXHDR_PROTO_ETH); |
| 144 | mlxsw_tx_hdr_swid_set(txhdr, 0); |
| 145 | mlxsw_tx_hdr_control_tclass_set(txhdr, 1); |
| 146 | mlxsw_tx_hdr_port_mid_set(txhdr, tx_info->local_port); |
| 147 | mlxsw_tx_hdr_type_set(txhdr, MLXSW_TXHDR_TYPE_CONTROL); |
| 148 | } |
| 149 | |
| 150 | static int mlxsw_sp_base_mac_get(struct mlxsw_sp *mlxsw_sp) |
| 151 | { |
| 152 | char spad_pl[MLXSW_REG_SPAD_LEN]; |
| 153 | int err; |
| 154 | |
| 155 | err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(spad), spad_pl); |
| 156 | if (err) |
| 157 | return err; |
| 158 | mlxsw_reg_spad_base_mac_memcpy_from(spad_pl, mlxsw_sp->base_mac); |
| 159 | return 0; |
| 160 | } |
| 161 | |
| 162 | static int mlxsw_sp_port_admin_status_set(struct mlxsw_sp_port *mlxsw_sp_port, |
| 163 | bool is_up) |
| 164 | { |
| 165 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
| 166 | char paos_pl[MLXSW_REG_PAOS_LEN]; |
| 167 | |
| 168 | mlxsw_reg_paos_pack(paos_pl, mlxsw_sp_port->local_port, |
| 169 | is_up ? MLXSW_PORT_ADMIN_STATUS_UP : |
| 170 | MLXSW_PORT_ADMIN_STATUS_DOWN); |
| 171 | return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(paos), paos_pl); |
| 172 | } |
| 173 | |
| 174 | static int mlxsw_sp_port_oper_status_get(struct mlxsw_sp_port *mlxsw_sp_port, |
| 175 | bool *p_is_up) |
| 176 | { |
| 177 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
| 178 | char paos_pl[MLXSW_REG_PAOS_LEN]; |
| 179 | u8 oper_status; |
| 180 | int err; |
| 181 | |
| 182 | mlxsw_reg_paos_pack(paos_pl, mlxsw_sp_port->local_port, 0); |
| 183 | err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(paos), paos_pl); |
| 184 | if (err) |
| 185 | return err; |
| 186 | oper_status = mlxsw_reg_paos_oper_status_get(paos_pl); |
| 187 | *p_is_up = oper_status == MLXSW_PORT_ADMIN_STATUS_UP ? true : false; |
| 188 | return 0; |
| 189 | } |
| 190 | |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 191 | static int mlxsw_sp_port_dev_addr_set(struct mlxsw_sp_port *mlxsw_sp_port, |
| 192 | unsigned char *addr) |
| 193 | { |
| 194 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
| 195 | char ppad_pl[MLXSW_REG_PPAD_LEN]; |
| 196 | |
| 197 | mlxsw_reg_ppad_pack(ppad_pl, true, mlxsw_sp_port->local_port); |
| 198 | mlxsw_reg_ppad_mac_memcpy_to(ppad_pl, addr); |
| 199 | return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ppad), ppad_pl); |
| 200 | } |
| 201 | |
| 202 | static int mlxsw_sp_port_dev_addr_init(struct mlxsw_sp_port *mlxsw_sp_port) |
| 203 | { |
| 204 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
| 205 | unsigned char *addr = mlxsw_sp_port->dev->dev_addr; |
| 206 | |
| 207 | ether_addr_copy(addr, mlxsw_sp->base_mac); |
| 208 | addr[ETH_ALEN - 1] += mlxsw_sp_port->local_port; |
| 209 | return mlxsw_sp_port_dev_addr_set(mlxsw_sp_port, addr); |
| 210 | } |
| 211 | |
| 212 | static int mlxsw_sp_port_stp_state_set(struct mlxsw_sp_port *mlxsw_sp_port, |
| 213 | u16 vid, enum mlxsw_reg_spms_state state) |
| 214 | { |
| 215 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
| 216 | char *spms_pl; |
| 217 | int err; |
| 218 | |
| 219 | spms_pl = kmalloc(MLXSW_REG_SPMS_LEN, GFP_KERNEL); |
| 220 | if (!spms_pl) |
| 221 | return -ENOMEM; |
| 222 | mlxsw_reg_spms_pack(spms_pl, mlxsw_sp_port->local_port); |
| 223 | mlxsw_reg_spms_vid_pack(spms_pl, vid, state); |
| 224 | err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(spms), spms_pl); |
| 225 | kfree(spms_pl); |
| 226 | return err; |
| 227 | } |
| 228 | |
| 229 | static int mlxsw_sp_port_mtu_set(struct mlxsw_sp_port *mlxsw_sp_port, u16 mtu) |
| 230 | { |
| 231 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
| 232 | char pmtu_pl[MLXSW_REG_PMTU_LEN]; |
| 233 | int max_mtu; |
| 234 | int err; |
| 235 | |
| 236 | mtu += MLXSW_TXHDR_LEN + ETH_HLEN; |
| 237 | mlxsw_reg_pmtu_pack(pmtu_pl, mlxsw_sp_port->local_port, 0); |
| 238 | err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(pmtu), pmtu_pl); |
| 239 | if (err) |
| 240 | return err; |
| 241 | max_mtu = mlxsw_reg_pmtu_max_mtu_get(pmtu_pl); |
| 242 | |
| 243 | if (mtu > max_mtu) |
| 244 | return -EINVAL; |
| 245 | |
| 246 | mlxsw_reg_pmtu_pack(pmtu_pl, mlxsw_sp_port->local_port, mtu); |
| 247 | return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(pmtu), pmtu_pl); |
| 248 | } |
| 249 | |
| 250 | static int mlxsw_sp_port_swid_set(struct mlxsw_sp_port *mlxsw_sp_port, u8 swid) |
| 251 | { |
| 252 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
| 253 | char pspa_pl[MLXSW_REG_PSPA_LEN]; |
| 254 | |
| 255 | mlxsw_reg_pspa_pack(pspa_pl, swid, mlxsw_sp_port->local_port); |
| 256 | return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(pspa), pspa_pl); |
| 257 | } |
| 258 | |
| 259 | static int mlxsw_sp_port_vp_mode_set(struct mlxsw_sp_port *mlxsw_sp_port, |
| 260 | bool enable) |
| 261 | { |
| 262 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
| 263 | char svpe_pl[MLXSW_REG_SVPE_LEN]; |
| 264 | |
| 265 | mlxsw_reg_svpe_pack(svpe_pl, mlxsw_sp_port->local_port, enable); |
| 266 | return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(svpe), svpe_pl); |
| 267 | } |
| 268 | |
| 269 | int mlxsw_sp_port_vid_to_fid_set(struct mlxsw_sp_port *mlxsw_sp_port, |
| 270 | enum mlxsw_reg_svfa_mt mt, bool valid, u16 fid, |
| 271 | u16 vid) |
| 272 | { |
| 273 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
| 274 | char svfa_pl[MLXSW_REG_SVFA_LEN]; |
| 275 | |
| 276 | mlxsw_reg_svfa_pack(svfa_pl, mlxsw_sp_port->local_port, mt, valid, |
| 277 | fid, vid); |
| 278 | return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(svfa), svfa_pl); |
| 279 | } |
| 280 | |
| 281 | static int mlxsw_sp_port_vid_learning_set(struct mlxsw_sp_port *mlxsw_sp_port, |
| 282 | u16 vid, bool learn_enable) |
| 283 | { |
| 284 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
| 285 | char *spvmlr_pl; |
| 286 | int err; |
| 287 | |
| 288 | spvmlr_pl = kmalloc(MLXSW_REG_SPVMLR_LEN, GFP_KERNEL); |
| 289 | if (!spvmlr_pl) |
| 290 | return -ENOMEM; |
| 291 | mlxsw_reg_spvmlr_pack(spvmlr_pl, mlxsw_sp_port->local_port, vid, vid, |
| 292 | learn_enable); |
| 293 | err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(spvmlr), spvmlr_pl); |
| 294 | kfree(spvmlr_pl); |
| 295 | return err; |
| 296 | } |
| 297 | |
| 298 | static int |
| 299 | mlxsw_sp_port_system_port_mapping_set(struct mlxsw_sp_port *mlxsw_sp_port) |
| 300 | { |
| 301 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
| 302 | char sspr_pl[MLXSW_REG_SSPR_LEN]; |
| 303 | |
| 304 | mlxsw_reg_sspr_pack(sspr_pl, mlxsw_sp_port->local_port); |
| 305 | return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sspr), sspr_pl); |
| 306 | } |
| 307 | |
Ido Schimmel | 2bf9a58 | 2016-04-05 10:20:04 +0200 | [diff] [blame] | 308 | static int __mlxsw_sp_port_module_info_get(struct mlxsw_sp *mlxsw_sp, |
| 309 | u8 local_port, u8 *p_module, |
| 310 | u8 *p_width, u8 *p_lane) |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 311 | { |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 312 | char pmlp_pl[MLXSW_REG_PMLP_LEN]; |
| 313 | int err; |
| 314 | |
Ido Schimmel | 558c2d5 | 2016-02-26 17:32:29 +0100 | [diff] [blame] | 315 | mlxsw_reg_pmlp_pack(pmlp_pl, local_port); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 316 | err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(pmlp), pmlp_pl); |
| 317 | if (err) |
| 318 | return err; |
Ido Schimmel | 558c2d5 | 2016-02-26 17:32:29 +0100 | [diff] [blame] | 319 | *p_module = mlxsw_reg_pmlp_module_get(pmlp_pl, 0); |
| 320 | *p_width = mlxsw_reg_pmlp_width_get(pmlp_pl); |
Ido Schimmel | 2bf9a58 | 2016-04-05 10:20:04 +0200 | [diff] [blame] | 321 | *p_lane = mlxsw_reg_pmlp_tx_lane_get(pmlp_pl, 0); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 322 | return 0; |
| 323 | } |
| 324 | |
Ido Schimmel | 2bf9a58 | 2016-04-05 10:20:04 +0200 | [diff] [blame] | 325 | static int mlxsw_sp_port_module_info_get(struct mlxsw_sp *mlxsw_sp, |
| 326 | u8 local_port, u8 *p_module, |
| 327 | u8 *p_width) |
| 328 | { |
| 329 | u8 lane; |
| 330 | |
| 331 | return __mlxsw_sp_port_module_info_get(mlxsw_sp, local_port, p_module, |
| 332 | p_width, &lane); |
| 333 | } |
| 334 | |
Ido Schimmel | 18f1e70 | 2016-02-26 17:32:31 +0100 | [diff] [blame] | 335 | static int mlxsw_sp_port_module_map(struct mlxsw_sp *mlxsw_sp, u8 local_port, |
| 336 | u8 module, u8 width, u8 lane) |
| 337 | { |
| 338 | char pmlp_pl[MLXSW_REG_PMLP_LEN]; |
| 339 | int i; |
| 340 | |
| 341 | mlxsw_reg_pmlp_pack(pmlp_pl, local_port); |
| 342 | mlxsw_reg_pmlp_width_set(pmlp_pl, width); |
| 343 | for (i = 0; i < width; i++) { |
| 344 | mlxsw_reg_pmlp_module_set(pmlp_pl, i, module); |
| 345 | mlxsw_reg_pmlp_tx_lane_set(pmlp_pl, i, lane + i); /* Rx & Tx */ |
| 346 | } |
| 347 | |
| 348 | return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(pmlp), pmlp_pl); |
| 349 | } |
| 350 | |
Ido Schimmel | 3e9b27b | 2016-02-26 17:32:28 +0100 | [diff] [blame] | 351 | static int mlxsw_sp_port_module_unmap(struct mlxsw_sp *mlxsw_sp, u8 local_port) |
| 352 | { |
| 353 | char pmlp_pl[MLXSW_REG_PMLP_LEN]; |
| 354 | |
| 355 | mlxsw_reg_pmlp_pack(pmlp_pl, local_port); |
| 356 | mlxsw_reg_pmlp_width_set(pmlp_pl, 0); |
| 357 | return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(pmlp), pmlp_pl); |
| 358 | } |
| 359 | |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 360 | static int mlxsw_sp_port_open(struct net_device *dev) |
| 361 | { |
| 362 | struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev); |
| 363 | int err; |
| 364 | |
| 365 | err = mlxsw_sp_port_admin_status_set(mlxsw_sp_port, true); |
| 366 | if (err) |
| 367 | return err; |
| 368 | netif_start_queue(dev); |
| 369 | return 0; |
| 370 | } |
| 371 | |
| 372 | static int mlxsw_sp_port_stop(struct net_device *dev) |
| 373 | { |
| 374 | struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev); |
| 375 | |
| 376 | netif_stop_queue(dev); |
| 377 | return mlxsw_sp_port_admin_status_set(mlxsw_sp_port, false); |
| 378 | } |
| 379 | |
| 380 | static netdev_tx_t mlxsw_sp_port_xmit(struct sk_buff *skb, |
| 381 | struct net_device *dev) |
| 382 | { |
| 383 | struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev); |
| 384 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
| 385 | struct mlxsw_sp_port_pcpu_stats *pcpu_stats; |
| 386 | const struct mlxsw_tx_info tx_info = { |
| 387 | .local_port = mlxsw_sp_port->local_port, |
| 388 | .is_emad = false, |
| 389 | }; |
| 390 | u64 len; |
| 391 | int err; |
| 392 | |
Jiri Pirko | 307c243 | 2016-04-08 19:11:22 +0200 | [diff] [blame^] | 393 | if (mlxsw_core_skb_transmit_busy(mlxsw_sp->core, &tx_info)) |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 394 | return NETDEV_TX_BUSY; |
| 395 | |
| 396 | if (unlikely(skb_headroom(skb) < MLXSW_TXHDR_LEN)) { |
| 397 | struct sk_buff *skb_orig = skb; |
| 398 | |
| 399 | skb = skb_realloc_headroom(skb, MLXSW_TXHDR_LEN); |
| 400 | if (!skb) { |
| 401 | this_cpu_inc(mlxsw_sp_port->pcpu_stats->tx_dropped); |
| 402 | dev_kfree_skb_any(skb_orig); |
| 403 | return NETDEV_TX_OK; |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | if (eth_skb_pad(skb)) { |
| 408 | this_cpu_inc(mlxsw_sp_port->pcpu_stats->tx_dropped); |
| 409 | return NETDEV_TX_OK; |
| 410 | } |
| 411 | |
| 412 | mlxsw_sp_txhdr_construct(skb, &tx_info); |
| 413 | len = skb->len; |
| 414 | /* Due to a race we might fail here because of a full queue. In that |
| 415 | * unlikely case we simply drop the packet. |
| 416 | */ |
Jiri Pirko | 307c243 | 2016-04-08 19:11:22 +0200 | [diff] [blame^] | 417 | err = mlxsw_core_skb_transmit(mlxsw_sp->core, skb, &tx_info); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 418 | |
| 419 | if (!err) { |
| 420 | pcpu_stats = this_cpu_ptr(mlxsw_sp_port->pcpu_stats); |
| 421 | u64_stats_update_begin(&pcpu_stats->syncp); |
| 422 | pcpu_stats->tx_packets++; |
| 423 | pcpu_stats->tx_bytes += len; |
| 424 | u64_stats_update_end(&pcpu_stats->syncp); |
| 425 | } else { |
| 426 | this_cpu_inc(mlxsw_sp_port->pcpu_stats->tx_dropped); |
| 427 | dev_kfree_skb_any(skb); |
| 428 | } |
| 429 | return NETDEV_TX_OK; |
| 430 | } |
| 431 | |
Jiri Pirko | c5b9b51 | 2015-12-03 12:12:22 +0100 | [diff] [blame] | 432 | static void mlxsw_sp_set_rx_mode(struct net_device *dev) |
| 433 | { |
| 434 | } |
| 435 | |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 436 | static int mlxsw_sp_port_set_mac_address(struct net_device *dev, void *p) |
| 437 | { |
| 438 | struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev); |
| 439 | struct sockaddr *addr = p; |
| 440 | int err; |
| 441 | |
| 442 | if (!is_valid_ether_addr(addr->sa_data)) |
| 443 | return -EADDRNOTAVAIL; |
| 444 | |
| 445 | err = mlxsw_sp_port_dev_addr_set(mlxsw_sp_port, addr->sa_data); |
| 446 | if (err) |
| 447 | return err; |
| 448 | memcpy(dev->dev_addr, addr->sa_data, dev->addr_len); |
| 449 | return 0; |
| 450 | } |
| 451 | |
Ido Schimmel | 9f7ec05 | 2016-04-06 17:10:14 +0200 | [diff] [blame] | 452 | static void mlxsw_sp_pg_buf_pack(char *pbmc_pl, int pg_index, int mtu, |
Ido Schimmel | d81a6bd | 2016-04-06 17:10:16 +0200 | [diff] [blame] | 453 | bool pause_en, bool pfc_en, u16 delay) |
Ido Schimmel | 8e8dfe9 | 2016-04-06 17:10:10 +0200 | [diff] [blame] | 454 | { |
| 455 | u16 pg_size = 2 * MLXSW_SP_BYTES_TO_CELLS(mtu); |
| 456 | |
Ido Schimmel | d81a6bd | 2016-04-06 17:10:16 +0200 | [diff] [blame] | 457 | delay = pfc_en ? mlxsw_sp_pfc_delay_get(mtu, delay) : |
| 458 | MLXSW_SP_PAUSE_DELAY; |
Ido Schimmel | 9f7ec05 | 2016-04-06 17:10:14 +0200 | [diff] [blame] | 459 | |
Ido Schimmel | d81a6bd | 2016-04-06 17:10:16 +0200 | [diff] [blame] | 460 | if (pause_en || pfc_en) |
Ido Schimmel | 9f7ec05 | 2016-04-06 17:10:14 +0200 | [diff] [blame] | 461 | mlxsw_reg_pbmc_lossless_buffer_pack(pbmc_pl, pg_index, |
Ido Schimmel | d81a6bd | 2016-04-06 17:10:16 +0200 | [diff] [blame] | 462 | pg_size + delay, pg_size); |
| 463 | else |
Ido Schimmel | 9f7ec05 | 2016-04-06 17:10:14 +0200 | [diff] [blame] | 464 | mlxsw_reg_pbmc_lossy_buffer_pack(pbmc_pl, pg_index, pg_size); |
Ido Schimmel | 8e8dfe9 | 2016-04-06 17:10:10 +0200 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | int __mlxsw_sp_port_headroom_set(struct mlxsw_sp_port *mlxsw_sp_port, int mtu, |
Ido Schimmel | d81a6bd | 2016-04-06 17:10:16 +0200 | [diff] [blame] | 468 | u8 *prio_tc, bool pause_en, |
| 469 | struct ieee_pfc *my_pfc) |
Ido Schimmel | ff6551e | 2016-04-06 17:10:03 +0200 | [diff] [blame] | 470 | { |
| 471 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
Ido Schimmel | d81a6bd | 2016-04-06 17:10:16 +0200 | [diff] [blame] | 472 | u8 pfc_en = !!my_pfc ? my_pfc->pfc_en : 0; |
| 473 | u16 delay = !!my_pfc ? my_pfc->delay : 0; |
Ido Schimmel | ff6551e | 2016-04-06 17:10:03 +0200 | [diff] [blame] | 474 | char pbmc_pl[MLXSW_REG_PBMC_LEN]; |
Ido Schimmel | 8e8dfe9 | 2016-04-06 17:10:10 +0200 | [diff] [blame] | 475 | int i, j, err; |
Ido Schimmel | ff6551e | 2016-04-06 17:10:03 +0200 | [diff] [blame] | 476 | |
| 477 | mlxsw_reg_pbmc_pack(pbmc_pl, mlxsw_sp_port->local_port, 0, 0); |
| 478 | err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(pbmc), pbmc_pl); |
| 479 | if (err) |
| 480 | return err; |
Ido Schimmel | 8e8dfe9 | 2016-04-06 17:10:10 +0200 | [diff] [blame] | 481 | |
| 482 | for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) { |
| 483 | bool configure = false; |
Ido Schimmel | d81a6bd | 2016-04-06 17:10:16 +0200 | [diff] [blame] | 484 | bool pfc = false; |
Ido Schimmel | 8e8dfe9 | 2016-04-06 17:10:10 +0200 | [diff] [blame] | 485 | |
| 486 | for (j = 0; j < IEEE_8021QAZ_MAX_TCS; j++) { |
| 487 | if (prio_tc[j] == i) { |
Ido Schimmel | d81a6bd | 2016-04-06 17:10:16 +0200 | [diff] [blame] | 488 | pfc = pfc_en & BIT(j); |
Ido Schimmel | 8e8dfe9 | 2016-04-06 17:10:10 +0200 | [diff] [blame] | 489 | configure = true; |
| 490 | break; |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | if (!configure) |
| 495 | continue; |
Ido Schimmel | d81a6bd | 2016-04-06 17:10:16 +0200 | [diff] [blame] | 496 | mlxsw_sp_pg_buf_pack(pbmc_pl, i, mtu, pause_en, pfc, delay); |
Ido Schimmel | 8e8dfe9 | 2016-04-06 17:10:10 +0200 | [diff] [blame] | 497 | } |
| 498 | |
Ido Schimmel | ff6551e | 2016-04-06 17:10:03 +0200 | [diff] [blame] | 499 | return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(pbmc), pbmc_pl); |
| 500 | } |
| 501 | |
Ido Schimmel | 8e8dfe9 | 2016-04-06 17:10:10 +0200 | [diff] [blame] | 502 | static int mlxsw_sp_port_headroom_set(struct mlxsw_sp_port *mlxsw_sp_port, |
Ido Schimmel | 9f7ec05 | 2016-04-06 17:10:14 +0200 | [diff] [blame] | 503 | int mtu, bool pause_en) |
Ido Schimmel | 8e8dfe9 | 2016-04-06 17:10:10 +0200 | [diff] [blame] | 504 | { |
| 505 | u8 def_prio_tc[IEEE_8021QAZ_MAX_TCS] = {0}; |
| 506 | bool dcb_en = !!mlxsw_sp_port->dcb.ets; |
Ido Schimmel | d81a6bd | 2016-04-06 17:10:16 +0200 | [diff] [blame] | 507 | struct ieee_pfc *my_pfc; |
Ido Schimmel | 8e8dfe9 | 2016-04-06 17:10:10 +0200 | [diff] [blame] | 508 | u8 *prio_tc; |
| 509 | |
| 510 | prio_tc = dcb_en ? mlxsw_sp_port->dcb.ets->prio_tc : def_prio_tc; |
Ido Schimmel | d81a6bd | 2016-04-06 17:10:16 +0200 | [diff] [blame] | 511 | my_pfc = dcb_en ? mlxsw_sp_port->dcb.pfc : NULL; |
Ido Schimmel | 8e8dfe9 | 2016-04-06 17:10:10 +0200 | [diff] [blame] | 512 | |
Ido Schimmel | 9f7ec05 | 2016-04-06 17:10:14 +0200 | [diff] [blame] | 513 | return __mlxsw_sp_port_headroom_set(mlxsw_sp_port, mtu, prio_tc, |
Ido Schimmel | d81a6bd | 2016-04-06 17:10:16 +0200 | [diff] [blame] | 514 | pause_en, my_pfc); |
Ido Schimmel | 8e8dfe9 | 2016-04-06 17:10:10 +0200 | [diff] [blame] | 515 | } |
| 516 | |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 517 | static int mlxsw_sp_port_change_mtu(struct net_device *dev, int mtu) |
| 518 | { |
| 519 | struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev); |
Ido Schimmel | 9f7ec05 | 2016-04-06 17:10:14 +0200 | [diff] [blame] | 520 | bool pause_en = mlxsw_sp_port_is_pause_en(mlxsw_sp_port); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 521 | int err; |
| 522 | |
Ido Schimmel | 9f7ec05 | 2016-04-06 17:10:14 +0200 | [diff] [blame] | 523 | err = mlxsw_sp_port_headroom_set(mlxsw_sp_port, mtu, pause_en); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 524 | if (err) |
| 525 | return err; |
Ido Schimmel | ff6551e | 2016-04-06 17:10:03 +0200 | [diff] [blame] | 526 | err = mlxsw_sp_port_mtu_set(mlxsw_sp_port, mtu); |
| 527 | if (err) |
| 528 | goto err_port_mtu_set; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 529 | dev->mtu = mtu; |
| 530 | return 0; |
Ido Schimmel | ff6551e | 2016-04-06 17:10:03 +0200 | [diff] [blame] | 531 | |
| 532 | err_port_mtu_set: |
Ido Schimmel | 9f7ec05 | 2016-04-06 17:10:14 +0200 | [diff] [blame] | 533 | mlxsw_sp_port_headroom_set(mlxsw_sp_port, dev->mtu, pause_en); |
Ido Schimmel | ff6551e | 2016-04-06 17:10:03 +0200 | [diff] [blame] | 534 | return err; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | static struct rtnl_link_stats64 * |
| 538 | mlxsw_sp_port_get_stats64(struct net_device *dev, |
| 539 | struct rtnl_link_stats64 *stats) |
| 540 | { |
| 541 | struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev); |
| 542 | struct mlxsw_sp_port_pcpu_stats *p; |
| 543 | u64 rx_packets, rx_bytes, tx_packets, tx_bytes; |
| 544 | u32 tx_dropped = 0; |
| 545 | unsigned int start; |
| 546 | int i; |
| 547 | |
| 548 | for_each_possible_cpu(i) { |
| 549 | p = per_cpu_ptr(mlxsw_sp_port->pcpu_stats, i); |
| 550 | do { |
| 551 | start = u64_stats_fetch_begin_irq(&p->syncp); |
| 552 | rx_packets = p->rx_packets; |
| 553 | rx_bytes = p->rx_bytes; |
| 554 | tx_packets = p->tx_packets; |
| 555 | tx_bytes = p->tx_bytes; |
| 556 | } while (u64_stats_fetch_retry_irq(&p->syncp, start)); |
| 557 | |
| 558 | stats->rx_packets += rx_packets; |
| 559 | stats->rx_bytes += rx_bytes; |
| 560 | stats->tx_packets += tx_packets; |
| 561 | stats->tx_bytes += tx_bytes; |
| 562 | /* tx_dropped is u32, updated without syncp protection. */ |
| 563 | tx_dropped += p->tx_dropped; |
| 564 | } |
| 565 | stats->tx_dropped = tx_dropped; |
| 566 | return stats; |
| 567 | } |
| 568 | |
| 569 | int mlxsw_sp_port_vlan_set(struct mlxsw_sp_port *mlxsw_sp_port, u16 vid_begin, |
| 570 | u16 vid_end, bool is_member, bool untagged) |
| 571 | { |
| 572 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
| 573 | char *spvm_pl; |
| 574 | int err; |
| 575 | |
| 576 | spvm_pl = kmalloc(MLXSW_REG_SPVM_LEN, GFP_KERNEL); |
| 577 | if (!spvm_pl) |
| 578 | return -ENOMEM; |
| 579 | |
| 580 | mlxsw_reg_spvm_pack(spvm_pl, mlxsw_sp_port->local_port, vid_begin, |
| 581 | vid_end, is_member, untagged); |
| 582 | err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(spvm), spvm_pl); |
| 583 | kfree(spvm_pl); |
| 584 | return err; |
| 585 | } |
| 586 | |
| 587 | static int mlxsw_sp_port_vp_mode_trans(struct mlxsw_sp_port *mlxsw_sp_port) |
| 588 | { |
| 589 | enum mlxsw_reg_svfa_mt mt = MLXSW_REG_SVFA_MT_PORT_VID_TO_FID; |
| 590 | u16 vid, last_visited_vid; |
| 591 | int err; |
| 592 | |
| 593 | for_each_set_bit(vid, mlxsw_sp_port->active_vlans, VLAN_N_VID) { |
| 594 | err = mlxsw_sp_port_vid_to_fid_set(mlxsw_sp_port, mt, true, vid, |
| 595 | vid); |
| 596 | if (err) { |
| 597 | last_visited_vid = vid; |
| 598 | goto err_port_vid_to_fid_set; |
| 599 | } |
| 600 | } |
| 601 | |
| 602 | err = mlxsw_sp_port_vp_mode_set(mlxsw_sp_port, true); |
| 603 | if (err) { |
| 604 | last_visited_vid = VLAN_N_VID; |
| 605 | goto err_port_vid_to_fid_set; |
| 606 | } |
| 607 | |
| 608 | return 0; |
| 609 | |
| 610 | err_port_vid_to_fid_set: |
| 611 | for_each_set_bit(vid, mlxsw_sp_port->active_vlans, last_visited_vid) |
| 612 | mlxsw_sp_port_vid_to_fid_set(mlxsw_sp_port, mt, false, vid, |
| 613 | vid); |
| 614 | return err; |
| 615 | } |
| 616 | |
| 617 | static int mlxsw_sp_port_vlan_mode_trans(struct mlxsw_sp_port *mlxsw_sp_port) |
| 618 | { |
| 619 | enum mlxsw_reg_svfa_mt mt = MLXSW_REG_SVFA_MT_PORT_VID_TO_FID; |
| 620 | u16 vid; |
| 621 | int err; |
| 622 | |
| 623 | err = mlxsw_sp_port_vp_mode_set(mlxsw_sp_port, false); |
| 624 | if (err) |
| 625 | return err; |
| 626 | |
| 627 | for_each_set_bit(vid, mlxsw_sp_port->active_vlans, VLAN_N_VID) { |
| 628 | err = mlxsw_sp_port_vid_to_fid_set(mlxsw_sp_port, mt, false, |
| 629 | vid, vid); |
| 630 | if (err) |
| 631 | return err; |
| 632 | } |
| 633 | |
| 634 | return 0; |
| 635 | } |
| 636 | |
Ido Schimmel | 7f71eb4 | 2015-12-15 16:03:37 +0100 | [diff] [blame] | 637 | static struct mlxsw_sp_vfid * |
| 638 | mlxsw_sp_vfid_find(const struct mlxsw_sp *mlxsw_sp, u16 vid) |
| 639 | { |
| 640 | struct mlxsw_sp_vfid *vfid; |
| 641 | |
| 642 | list_for_each_entry(vfid, &mlxsw_sp->port_vfids.list, list) { |
| 643 | if (vfid->vid == vid) |
| 644 | return vfid; |
| 645 | } |
| 646 | |
| 647 | return NULL; |
| 648 | } |
| 649 | |
| 650 | static u16 mlxsw_sp_avail_vfid_get(const struct mlxsw_sp *mlxsw_sp) |
| 651 | { |
| 652 | return find_first_zero_bit(mlxsw_sp->port_vfids.mapped, |
| 653 | MLXSW_SP_VFID_PORT_MAX); |
| 654 | } |
| 655 | |
| 656 | static int __mlxsw_sp_vfid_create(struct mlxsw_sp *mlxsw_sp, u16 vfid) |
| 657 | { |
| 658 | u16 fid = mlxsw_sp_vfid_to_fid(vfid); |
| 659 | char sfmr_pl[MLXSW_REG_SFMR_LEN]; |
| 660 | |
| 661 | mlxsw_reg_sfmr_pack(sfmr_pl, MLXSW_REG_SFMR_OP_CREATE_FID, fid, 0); |
| 662 | return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfmr), sfmr_pl); |
| 663 | } |
| 664 | |
| 665 | static void __mlxsw_sp_vfid_destroy(struct mlxsw_sp *mlxsw_sp, u16 vfid) |
| 666 | { |
| 667 | u16 fid = mlxsw_sp_vfid_to_fid(vfid); |
| 668 | char sfmr_pl[MLXSW_REG_SFMR_LEN]; |
| 669 | |
| 670 | mlxsw_reg_sfmr_pack(sfmr_pl, MLXSW_REG_SFMR_OP_DESTROY_FID, fid, 0); |
| 671 | mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfmr), sfmr_pl); |
| 672 | } |
| 673 | |
| 674 | static struct mlxsw_sp_vfid *mlxsw_sp_vfid_create(struct mlxsw_sp *mlxsw_sp, |
| 675 | u16 vid) |
| 676 | { |
| 677 | struct device *dev = mlxsw_sp->bus_info->dev; |
| 678 | struct mlxsw_sp_vfid *vfid; |
| 679 | u16 n_vfid; |
| 680 | int err; |
| 681 | |
| 682 | n_vfid = mlxsw_sp_avail_vfid_get(mlxsw_sp); |
| 683 | if (n_vfid == MLXSW_SP_VFID_PORT_MAX) { |
| 684 | dev_err(dev, "No available vFIDs\n"); |
| 685 | return ERR_PTR(-ERANGE); |
| 686 | } |
| 687 | |
| 688 | err = __mlxsw_sp_vfid_create(mlxsw_sp, n_vfid); |
| 689 | if (err) { |
| 690 | dev_err(dev, "Failed to create vFID=%d\n", n_vfid); |
| 691 | return ERR_PTR(err); |
| 692 | } |
| 693 | |
| 694 | vfid = kzalloc(sizeof(*vfid), GFP_KERNEL); |
| 695 | if (!vfid) |
| 696 | goto err_allocate_vfid; |
| 697 | |
| 698 | vfid->vfid = n_vfid; |
| 699 | vfid->vid = vid; |
| 700 | |
| 701 | list_add(&vfid->list, &mlxsw_sp->port_vfids.list); |
| 702 | set_bit(n_vfid, mlxsw_sp->port_vfids.mapped); |
| 703 | |
| 704 | return vfid; |
| 705 | |
| 706 | err_allocate_vfid: |
| 707 | __mlxsw_sp_vfid_destroy(mlxsw_sp, n_vfid); |
| 708 | return ERR_PTR(-ENOMEM); |
| 709 | } |
| 710 | |
| 711 | static void mlxsw_sp_vfid_destroy(struct mlxsw_sp *mlxsw_sp, |
| 712 | struct mlxsw_sp_vfid *vfid) |
| 713 | { |
| 714 | clear_bit(vfid->vfid, mlxsw_sp->port_vfids.mapped); |
| 715 | list_del(&vfid->list); |
| 716 | |
| 717 | __mlxsw_sp_vfid_destroy(mlxsw_sp, vfid->vfid); |
| 718 | |
| 719 | kfree(vfid); |
| 720 | } |
| 721 | |
| 722 | static struct mlxsw_sp_port * |
| 723 | mlxsw_sp_port_vport_create(struct mlxsw_sp_port *mlxsw_sp_port, |
| 724 | struct mlxsw_sp_vfid *vfid) |
| 725 | { |
| 726 | struct mlxsw_sp_port *mlxsw_sp_vport; |
| 727 | |
| 728 | mlxsw_sp_vport = kzalloc(sizeof(*mlxsw_sp_vport), GFP_KERNEL); |
| 729 | if (!mlxsw_sp_vport) |
| 730 | return NULL; |
| 731 | |
| 732 | /* dev will be set correctly after the VLAN device is linked |
| 733 | * with the real device. In case of bridge SELF invocation, dev |
| 734 | * will remain as is. |
| 735 | */ |
| 736 | mlxsw_sp_vport->dev = mlxsw_sp_port->dev; |
| 737 | mlxsw_sp_vport->mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
| 738 | mlxsw_sp_vport->local_port = mlxsw_sp_port->local_port; |
| 739 | mlxsw_sp_vport->stp_state = BR_STATE_FORWARDING; |
Ido Schimmel | 272c447 | 2015-12-15 16:03:47 +0100 | [diff] [blame] | 740 | mlxsw_sp_vport->lagged = mlxsw_sp_port->lagged; |
| 741 | mlxsw_sp_vport->lag_id = mlxsw_sp_port->lag_id; |
Ido Schimmel | 7f71eb4 | 2015-12-15 16:03:37 +0100 | [diff] [blame] | 742 | mlxsw_sp_vport->vport.vfid = vfid; |
| 743 | mlxsw_sp_vport->vport.vid = vfid->vid; |
| 744 | |
| 745 | list_add(&mlxsw_sp_vport->vport.list, &mlxsw_sp_port->vports_list); |
| 746 | |
| 747 | return mlxsw_sp_vport; |
| 748 | } |
| 749 | |
| 750 | static void mlxsw_sp_port_vport_destroy(struct mlxsw_sp_port *mlxsw_sp_vport) |
| 751 | { |
| 752 | list_del(&mlxsw_sp_vport->vport.list); |
| 753 | kfree(mlxsw_sp_vport); |
| 754 | } |
| 755 | |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 756 | int mlxsw_sp_port_add_vid(struct net_device *dev, __be16 __always_unused proto, |
| 757 | u16 vid) |
| 758 | { |
| 759 | struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev); |
| 760 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
Ido Schimmel | 7f71eb4 | 2015-12-15 16:03:37 +0100 | [diff] [blame] | 761 | struct mlxsw_sp_port *mlxsw_sp_vport; |
| 762 | struct mlxsw_sp_vfid *vfid; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 763 | int err; |
| 764 | |
| 765 | /* VLAN 0 is added to HW filter when device goes up, but it is |
| 766 | * reserved in our case, so simply return. |
| 767 | */ |
| 768 | if (!vid) |
| 769 | return 0; |
| 770 | |
Ido Schimmel | 7f71eb4 | 2015-12-15 16:03:37 +0100 | [diff] [blame] | 771 | if (mlxsw_sp_port_vport_find(mlxsw_sp_port, vid)) { |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 772 | netdev_warn(dev, "VID=%d already configured\n", vid); |
| 773 | return 0; |
| 774 | } |
| 775 | |
Ido Schimmel | 7f71eb4 | 2015-12-15 16:03:37 +0100 | [diff] [blame] | 776 | vfid = mlxsw_sp_vfid_find(mlxsw_sp, vid); |
| 777 | if (!vfid) { |
| 778 | vfid = mlxsw_sp_vfid_create(mlxsw_sp, vid); |
| 779 | if (IS_ERR(vfid)) { |
| 780 | netdev_err(dev, "Failed to create vFID for VID=%d\n", |
| 781 | vid); |
| 782 | return PTR_ERR(vfid); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 783 | } |
| 784 | } |
| 785 | |
Ido Schimmel | 7f71eb4 | 2015-12-15 16:03:37 +0100 | [diff] [blame] | 786 | mlxsw_sp_vport = mlxsw_sp_port_vport_create(mlxsw_sp_port, vfid); |
| 787 | if (!mlxsw_sp_vport) { |
| 788 | netdev_err(dev, "Failed to create vPort for VID=%d\n", vid); |
| 789 | err = -ENOMEM; |
| 790 | goto err_port_vport_create; |
| 791 | } |
| 792 | |
| 793 | if (!vfid->nr_vports) { |
| 794 | err = mlxsw_sp_vport_flood_set(mlxsw_sp_vport, vfid->vfid, |
Ido Schimmel | 19ae612 | 2015-12-15 16:03:39 +0100 | [diff] [blame] | 795 | true, false); |
Ido Schimmel | 7f71eb4 | 2015-12-15 16:03:37 +0100 | [diff] [blame] | 796 | if (err) { |
| 797 | netdev_err(dev, "Failed to setup flooding for vFID=%d\n", |
| 798 | vfid->vfid); |
| 799 | goto err_vport_flood_set; |
| 800 | } |
| 801 | } |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 802 | |
| 803 | /* When adding the first VLAN interface on a bridged port we need to |
| 804 | * transition all the active 802.1Q bridge VLANs to use explicit |
| 805 | * {Port, VID} to FID mappings and set the port's mode to Virtual mode. |
| 806 | */ |
Ido Schimmel | 7f71eb4 | 2015-12-15 16:03:37 +0100 | [diff] [blame] | 807 | if (list_is_singular(&mlxsw_sp_port->vports_list)) { |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 808 | err = mlxsw_sp_port_vp_mode_trans(mlxsw_sp_port); |
| 809 | if (err) { |
| 810 | netdev_err(dev, "Failed to set to Virtual mode\n"); |
Ido Schimmel | 7f71eb4 | 2015-12-15 16:03:37 +0100 | [diff] [blame] | 811 | goto err_port_vp_mode_trans; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 812 | } |
| 813 | } |
| 814 | |
Ido Schimmel | 7f71eb4 | 2015-12-15 16:03:37 +0100 | [diff] [blame] | 815 | err = mlxsw_sp_port_vid_to_fid_set(mlxsw_sp_vport, |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 816 | MLXSW_REG_SVFA_MT_PORT_VID_TO_FID, |
Ido Schimmel | 7f71eb4 | 2015-12-15 16:03:37 +0100 | [diff] [blame] | 817 | true, |
| 818 | mlxsw_sp_vfid_to_fid(vfid->vfid), |
| 819 | vid); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 820 | if (err) { |
| 821 | netdev_err(dev, "Failed to map {Port, VID=%d} to vFID=%d\n", |
Ido Schimmel | 7f71eb4 | 2015-12-15 16:03:37 +0100 | [diff] [blame] | 822 | vid, vfid->vfid); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 823 | goto err_port_vid_to_fid_set; |
| 824 | } |
| 825 | |
Ido Schimmel | 7f71eb4 | 2015-12-15 16:03:37 +0100 | [diff] [blame] | 826 | err = mlxsw_sp_port_vid_learning_set(mlxsw_sp_vport, vid, false); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 827 | if (err) { |
| 828 | netdev_err(dev, "Failed to disable learning for VID=%d\n", vid); |
| 829 | goto err_port_vid_learning_set; |
| 830 | } |
| 831 | |
Ido Schimmel | 7f71eb4 | 2015-12-15 16:03:37 +0100 | [diff] [blame] | 832 | err = mlxsw_sp_port_vlan_set(mlxsw_sp_vport, vid, vid, true, false); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 833 | if (err) { |
| 834 | netdev_err(dev, "Failed to set VLAN membership for VID=%d\n", |
| 835 | vid); |
| 836 | goto err_port_add_vid; |
| 837 | } |
| 838 | |
Ido Schimmel | 7f71eb4 | 2015-12-15 16:03:37 +0100 | [diff] [blame] | 839 | err = mlxsw_sp_port_stp_state_set(mlxsw_sp_vport, vid, |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 840 | MLXSW_REG_SPMS_STATE_FORWARDING); |
| 841 | if (err) { |
| 842 | netdev_err(dev, "Failed to set STP state for VID=%d\n", vid); |
| 843 | goto err_port_stp_state_set; |
| 844 | } |
| 845 | |
Ido Schimmel | 7f71eb4 | 2015-12-15 16:03:37 +0100 | [diff] [blame] | 846 | vfid->nr_vports++; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 847 | |
| 848 | return 0; |
| 849 | |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 850 | err_port_stp_state_set: |
Ido Schimmel | 7f71eb4 | 2015-12-15 16:03:37 +0100 | [diff] [blame] | 851 | mlxsw_sp_port_vlan_set(mlxsw_sp_vport, vid, vid, false, false); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 852 | err_port_add_vid: |
Ido Schimmel | 7f71eb4 | 2015-12-15 16:03:37 +0100 | [diff] [blame] | 853 | mlxsw_sp_port_vid_learning_set(mlxsw_sp_vport, vid, true); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 854 | err_port_vid_learning_set: |
Ido Schimmel | 7f71eb4 | 2015-12-15 16:03:37 +0100 | [diff] [blame] | 855 | mlxsw_sp_port_vid_to_fid_set(mlxsw_sp_vport, |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 856 | MLXSW_REG_SVFA_MT_PORT_VID_TO_FID, false, |
Ido Schimmel | 7f71eb4 | 2015-12-15 16:03:37 +0100 | [diff] [blame] | 857 | mlxsw_sp_vfid_to_fid(vfid->vfid), vid); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 858 | err_port_vid_to_fid_set: |
Ido Schimmel | 7f71eb4 | 2015-12-15 16:03:37 +0100 | [diff] [blame] | 859 | if (list_is_singular(&mlxsw_sp_port->vports_list)) |
| 860 | mlxsw_sp_port_vlan_mode_trans(mlxsw_sp_port); |
| 861 | err_port_vp_mode_trans: |
| 862 | if (!vfid->nr_vports) |
Ido Schimmel | 19ae612 | 2015-12-15 16:03:39 +0100 | [diff] [blame] | 863 | mlxsw_sp_vport_flood_set(mlxsw_sp_vport, vfid->vfid, false, |
| 864 | false); |
Ido Schimmel | 7f71eb4 | 2015-12-15 16:03:37 +0100 | [diff] [blame] | 865 | err_vport_flood_set: |
| 866 | mlxsw_sp_port_vport_destroy(mlxsw_sp_vport); |
| 867 | err_port_vport_create: |
| 868 | if (!vfid->nr_vports) |
| 869 | mlxsw_sp_vfid_destroy(mlxsw_sp, vfid); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 870 | return err; |
| 871 | } |
| 872 | |
| 873 | int mlxsw_sp_port_kill_vid(struct net_device *dev, |
| 874 | __be16 __always_unused proto, u16 vid) |
| 875 | { |
| 876 | struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev); |
Ido Schimmel | 7f71eb4 | 2015-12-15 16:03:37 +0100 | [diff] [blame] | 877 | struct mlxsw_sp_port *mlxsw_sp_vport; |
| 878 | struct mlxsw_sp_vfid *vfid; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 879 | int err; |
| 880 | |
| 881 | /* VLAN 0 is removed from HW filter when device goes down, but |
| 882 | * it is reserved in our case, so simply return. |
| 883 | */ |
| 884 | if (!vid) |
| 885 | return 0; |
| 886 | |
Ido Schimmel | 7f71eb4 | 2015-12-15 16:03:37 +0100 | [diff] [blame] | 887 | mlxsw_sp_vport = mlxsw_sp_port_vport_find(mlxsw_sp_port, vid); |
| 888 | if (!mlxsw_sp_vport) { |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 889 | netdev_warn(dev, "VID=%d does not exist\n", vid); |
| 890 | return 0; |
| 891 | } |
| 892 | |
Ido Schimmel | 7f71eb4 | 2015-12-15 16:03:37 +0100 | [diff] [blame] | 893 | vfid = mlxsw_sp_vport->vport.vfid; |
| 894 | |
| 895 | err = mlxsw_sp_port_stp_state_set(mlxsw_sp_vport, vid, |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 896 | MLXSW_REG_SPMS_STATE_DISCARDING); |
| 897 | if (err) { |
| 898 | netdev_err(dev, "Failed to set STP state for VID=%d\n", vid); |
| 899 | return err; |
| 900 | } |
| 901 | |
Ido Schimmel | 7f71eb4 | 2015-12-15 16:03:37 +0100 | [diff] [blame] | 902 | err = mlxsw_sp_port_vlan_set(mlxsw_sp_vport, vid, vid, false, false); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 903 | if (err) { |
| 904 | netdev_err(dev, "Failed to set VLAN membership for VID=%d\n", |
| 905 | vid); |
| 906 | return err; |
| 907 | } |
| 908 | |
Ido Schimmel | 7f71eb4 | 2015-12-15 16:03:37 +0100 | [diff] [blame] | 909 | err = mlxsw_sp_port_vid_learning_set(mlxsw_sp_vport, vid, true); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 910 | if (err) { |
| 911 | netdev_err(dev, "Failed to enable learning for VID=%d\n", vid); |
| 912 | return err; |
| 913 | } |
| 914 | |
Ido Schimmel | 7f71eb4 | 2015-12-15 16:03:37 +0100 | [diff] [blame] | 915 | err = mlxsw_sp_port_vid_to_fid_set(mlxsw_sp_vport, |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 916 | MLXSW_REG_SVFA_MT_PORT_VID_TO_FID, |
Ido Schimmel | 7f71eb4 | 2015-12-15 16:03:37 +0100 | [diff] [blame] | 917 | false, |
| 918 | mlxsw_sp_vfid_to_fid(vfid->vfid), |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 919 | vid); |
| 920 | if (err) { |
| 921 | netdev_err(dev, "Failed to invalidate {Port, VID=%d} to vFID=%d mapping\n", |
Ido Schimmel | 7f71eb4 | 2015-12-15 16:03:37 +0100 | [diff] [blame] | 922 | vid, vfid->vfid); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 923 | return err; |
| 924 | } |
| 925 | |
| 926 | /* When removing the last VLAN interface on a bridged port we need to |
| 927 | * transition all active 802.1Q bridge VLANs to use VID to FID |
| 928 | * mappings and set port's mode to VLAN mode. |
| 929 | */ |
Ido Schimmel | 7f71eb4 | 2015-12-15 16:03:37 +0100 | [diff] [blame] | 930 | if (list_is_singular(&mlxsw_sp_port->vports_list)) { |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 931 | err = mlxsw_sp_port_vlan_mode_trans(mlxsw_sp_port); |
| 932 | if (err) { |
| 933 | netdev_err(dev, "Failed to set to VLAN mode\n"); |
| 934 | return err; |
| 935 | } |
| 936 | } |
| 937 | |
Ido Schimmel | 7f71eb4 | 2015-12-15 16:03:37 +0100 | [diff] [blame] | 938 | vfid->nr_vports--; |
| 939 | mlxsw_sp_port_vport_destroy(mlxsw_sp_vport); |
| 940 | |
| 941 | /* Destroy the vFID if no vPorts are assigned to it anymore. */ |
| 942 | if (!vfid->nr_vports) |
| 943 | mlxsw_sp_vfid_destroy(mlxsw_sp_port->mlxsw_sp, vfid); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 944 | |
| 945 | return 0; |
| 946 | } |
| 947 | |
Ido Schimmel | 2bf9a58 | 2016-04-05 10:20:04 +0200 | [diff] [blame] | 948 | static int mlxsw_sp_port_get_phys_port_name(struct net_device *dev, char *name, |
| 949 | size_t len) |
| 950 | { |
| 951 | struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev); |
| 952 | u8 module, width, lane; |
| 953 | int err; |
| 954 | |
| 955 | err = __mlxsw_sp_port_module_info_get(mlxsw_sp_port->mlxsw_sp, |
| 956 | mlxsw_sp_port->local_port, |
| 957 | &module, &width, &lane); |
| 958 | if (err) { |
| 959 | netdev_err(dev, "Failed to retrieve module information\n"); |
| 960 | return err; |
| 961 | } |
| 962 | |
| 963 | if (!mlxsw_sp_port->split) |
| 964 | err = snprintf(name, len, "p%d", module + 1); |
| 965 | else |
| 966 | err = snprintf(name, len, "p%ds%d", module + 1, |
| 967 | lane / width); |
| 968 | |
| 969 | if (err >= len) |
| 970 | return -EINVAL; |
| 971 | |
| 972 | return 0; |
| 973 | } |
| 974 | |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 975 | static const struct net_device_ops mlxsw_sp_port_netdev_ops = { |
| 976 | .ndo_open = mlxsw_sp_port_open, |
| 977 | .ndo_stop = mlxsw_sp_port_stop, |
| 978 | .ndo_start_xmit = mlxsw_sp_port_xmit, |
Jiri Pirko | c5b9b51 | 2015-12-03 12:12:22 +0100 | [diff] [blame] | 979 | .ndo_set_rx_mode = mlxsw_sp_set_rx_mode, |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 980 | .ndo_set_mac_address = mlxsw_sp_port_set_mac_address, |
| 981 | .ndo_change_mtu = mlxsw_sp_port_change_mtu, |
| 982 | .ndo_get_stats64 = mlxsw_sp_port_get_stats64, |
| 983 | .ndo_vlan_rx_add_vid = mlxsw_sp_port_add_vid, |
| 984 | .ndo_vlan_rx_kill_vid = mlxsw_sp_port_kill_vid, |
| 985 | .ndo_fdb_add = switchdev_port_fdb_add, |
| 986 | .ndo_fdb_del = switchdev_port_fdb_del, |
| 987 | .ndo_fdb_dump = switchdev_port_fdb_dump, |
| 988 | .ndo_bridge_setlink = switchdev_port_bridge_setlink, |
| 989 | .ndo_bridge_getlink = switchdev_port_bridge_getlink, |
| 990 | .ndo_bridge_dellink = switchdev_port_bridge_dellink, |
Ido Schimmel | 2bf9a58 | 2016-04-05 10:20:04 +0200 | [diff] [blame] | 991 | .ndo_get_phys_port_name = mlxsw_sp_port_get_phys_port_name, |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 992 | }; |
| 993 | |
| 994 | static void mlxsw_sp_port_get_drvinfo(struct net_device *dev, |
| 995 | struct ethtool_drvinfo *drvinfo) |
| 996 | { |
| 997 | struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev); |
| 998 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
| 999 | |
| 1000 | strlcpy(drvinfo->driver, mlxsw_sp_driver_name, sizeof(drvinfo->driver)); |
| 1001 | strlcpy(drvinfo->version, mlxsw_sp_driver_version, |
| 1002 | sizeof(drvinfo->version)); |
| 1003 | snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version), |
| 1004 | "%d.%d.%d", |
| 1005 | mlxsw_sp->bus_info->fw_rev.major, |
| 1006 | mlxsw_sp->bus_info->fw_rev.minor, |
| 1007 | mlxsw_sp->bus_info->fw_rev.subminor); |
| 1008 | strlcpy(drvinfo->bus_info, mlxsw_sp->bus_info->device_name, |
| 1009 | sizeof(drvinfo->bus_info)); |
| 1010 | } |
| 1011 | |
Ido Schimmel | 9f7ec05 | 2016-04-06 17:10:14 +0200 | [diff] [blame] | 1012 | static void mlxsw_sp_port_get_pauseparam(struct net_device *dev, |
| 1013 | struct ethtool_pauseparam *pause) |
| 1014 | { |
| 1015 | struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev); |
| 1016 | |
| 1017 | pause->rx_pause = mlxsw_sp_port->link.rx_pause; |
| 1018 | pause->tx_pause = mlxsw_sp_port->link.tx_pause; |
| 1019 | } |
| 1020 | |
| 1021 | static int mlxsw_sp_port_pause_set(struct mlxsw_sp_port *mlxsw_sp_port, |
| 1022 | struct ethtool_pauseparam *pause) |
| 1023 | { |
| 1024 | char pfcc_pl[MLXSW_REG_PFCC_LEN]; |
| 1025 | |
| 1026 | mlxsw_reg_pfcc_pack(pfcc_pl, mlxsw_sp_port->local_port); |
| 1027 | mlxsw_reg_pfcc_pprx_set(pfcc_pl, pause->rx_pause); |
| 1028 | mlxsw_reg_pfcc_pptx_set(pfcc_pl, pause->tx_pause); |
| 1029 | |
| 1030 | return mlxsw_reg_write(mlxsw_sp_port->mlxsw_sp->core, MLXSW_REG(pfcc), |
| 1031 | pfcc_pl); |
| 1032 | } |
| 1033 | |
| 1034 | static int mlxsw_sp_port_set_pauseparam(struct net_device *dev, |
| 1035 | struct ethtool_pauseparam *pause) |
| 1036 | { |
| 1037 | struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev); |
| 1038 | bool pause_en = pause->tx_pause || pause->rx_pause; |
| 1039 | int err; |
| 1040 | |
Ido Schimmel | d81a6bd | 2016-04-06 17:10:16 +0200 | [diff] [blame] | 1041 | if (mlxsw_sp_port->dcb.pfc && mlxsw_sp_port->dcb.pfc->pfc_en) { |
| 1042 | netdev_err(dev, "PFC already enabled on port\n"); |
| 1043 | return -EINVAL; |
| 1044 | } |
| 1045 | |
Ido Schimmel | 9f7ec05 | 2016-04-06 17:10:14 +0200 | [diff] [blame] | 1046 | if (pause->autoneg) { |
| 1047 | netdev_err(dev, "PAUSE frames autonegotiation isn't supported\n"); |
| 1048 | return -EINVAL; |
| 1049 | } |
| 1050 | |
| 1051 | err = mlxsw_sp_port_headroom_set(mlxsw_sp_port, dev->mtu, pause_en); |
| 1052 | if (err) { |
| 1053 | netdev_err(dev, "Failed to configure port's headroom\n"); |
| 1054 | return err; |
| 1055 | } |
| 1056 | |
| 1057 | err = mlxsw_sp_port_pause_set(mlxsw_sp_port, pause); |
| 1058 | if (err) { |
| 1059 | netdev_err(dev, "Failed to set PAUSE parameters\n"); |
| 1060 | goto err_port_pause_configure; |
| 1061 | } |
| 1062 | |
| 1063 | mlxsw_sp_port->link.rx_pause = pause->rx_pause; |
| 1064 | mlxsw_sp_port->link.tx_pause = pause->tx_pause; |
| 1065 | |
| 1066 | return 0; |
| 1067 | |
| 1068 | err_port_pause_configure: |
| 1069 | pause_en = mlxsw_sp_port_is_pause_en(mlxsw_sp_port); |
| 1070 | mlxsw_sp_port_headroom_set(mlxsw_sp_port, dev->mtu, pause_en); |
| 1071 | return err; |
| 1072 | } |
| 1073 | |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1074 | struct mlxsw_sp_port_hw_stats { |
| 1075 | char str[ETH_GSTRING_LEN]; |
| 1076 | u64 (*getter)(char *payload); |
| 1077 | }; |
| 1078 | |
| 1079 | static const struct mlxsw_sp_port_hw_stats mlxsw_sp_port_hw_stats[] = { |
| 1080 | { |
| 1081 | .str = "a_frames_transmitted_ok", |
| 1082 | .getter = mlxsw_reg_ppcnt_a_frames_transmitted_ok_get, |
| 1083 | }, |
| 1084 | { |
| 1085 | .str = "a_frames_received_ok", |
| 1086 | .getter = mlxsw_reg_ppcnt_a_frames_received_ok_get, |
| 1087 | }, |
| 1088 | { |
| 1089 | .str = "a_frame_check_sequence_errors", |
| 1090 | .getter = mlxsw_reg_ppcnt_a_frame_check_sequence_errors_get, |
| 1091 | }, |
| 1092 | { |
| 1093 | .str = "a_alignment_errors", |
| 1094 | .getter = mlxsw_reg_ppcnt_a_alignment_errors_get, |
| 1095 | }, |
| 1096 | { |
| 1097 | .str = "a_octets_transmitted_ok", |
| 1098 | .getter = mlxsw_reg_ppcnt_a_octets_transmitted_ok_get, |
| 1099 | }, |
| 1100 | { |
| 1101 | .str = "a_octets_received_ok", |
| 1102 | .getter = mlxsw_reg_ppcnt_a_octets_received_ok_get, |
| 1103 | }, |
| 1104 | { |
| 1105 | .str = "a_multicast_frames_xmitted_ok", |
| 1106 | .getter = mlxsw_reg_ppcnt_a_multicast_frames_xmitted_ok_get, |
| 1107 | }, |
| 1108 | { |
| 1109 | .str = "a_broadcast_frames_xmitted_ok", |
| 1110 | .getter = mlxsw_reg_ppcnt_a_broadcast_frames_xmitted_ok_get, |
| 1111 | }, |
| 1112 | { |
| 1113 | .str = "a_multicast_frames_received_ok", |
| 1114 | .getter = mlxsw_reg_ppcnt_a_multicast_frames_received_ok_get, |
| 1115 | }, |
| 1116 | { |
| 1117 | .str = "a_broadcast_frames_received_ok", |
| 1118 | .getter = mlxsw_reg_ppcnt_a_broadcast_frames_received_ok_get, |
| 1119 | }, |
| 1120 | { |
| 1121 | .str = "a_in_range_length_errors", |
| 1122 | .getter = mlxsw_reg_ppcnt_a_in_range_length_errors_get, |
| 1123 | }, |
| 1124 | { |
| 1125 | .str = "a_out_of_range_length_field", |
| 1126 | .getter = mlxsw_reg_ppcnt_a_out_of_range_length_field_get, |
| 1127 | }, |
| 1128 | { |
| 1129 | .str = "a_frame_too_long_errors", |
| 1130 | .getter = mlxsw_reg_ppcnt_a_frame_too_long_errors_get, |
| 1131 | }, |
| 1132 | { |
| 1133 | .str = "a_symbol_error_during_carrier", |
| 1134 | .getter = mlxsw_reg_ppcnt_a_symbol_error_during_carrier_get, |
| 1135 | }, |
| 1136 | { |
| 1137 | .str = "a_mac_control_frames_transmitted", |
| 1138 | .getter = mlxsw_reg_ppcnt_a_mac_control_frames_transmitted_get, |
| 1139 | }, |
| 1140 | { |
| 1141 | .str = "a_mac_control_frames_received", |
| 1142 | .getter = mlxsw_reg_ppcnt_a_mac_control_frames_received_get, |
| 1143 | }, |
| 1144 | { |
| 1145 | .str = "a_unsupported_opcodes_received", |
| 1146 | .getter = mlxsw_reg_ppcnt_a_unsupported_opcodes_received_get, |
| 1147 | }, |
| 1148 | { |
| 1149 | .str = "a_pause_mac_ctrl_frames_received", |
| 1150 | .getter = mlxsw_reg_ppcnt_a_pause_mac_ctrl_frames_received_get, |
| 1151 | }, |
| 1152 | { |
| 1153 | .str = "a_pause_mac_ctrl_frames_xmitted", |
| 1154 | .getter = mlxsw_reg_ppcnt_a_pause_mac_ctrl_frames_transmitted_get, |
| 1155 | }, |
| 1156 | }; |
| 1157 | |
| 1158 | #define MLXSW_SP_PORT_HW_STATS_LEN ARRAY_SIZE(mlxsw_sp_port_hw_stats) |
| 1159 | |
| 1160 | static void mlxsw_sp_port_get_strings(struct net_device *dev, |
| 1161 | u32 stringset, u8 *data) |
| 1162 | { |
| 1163 | u8 *p = data; |
| 1164 | int i; |
| 1165 | |
| 1166 | switch (stringset) { |
| 1167 | case ETH_SS_STATS: |
| 1168 | for (i = 0; i < MLXSW_SP_PORT_HW_STATS_LEN; i++) { |
| 1169 | memcpy(p, mlxsw_sp_port_hw_stats[i].str, |
| 1170 | ETH_GSTRING_LEN); |
| 1171 | p += ETH_GSTRING_LEN; |
| 1172 | } |
| 1173 | break; |
| 1174 | } |
| 1175 | } |
| 1176 | |
Ido Schimmel | 3a66ee3 | 2015-11-27 13:45:55 +0100 | [diff] [blame] | 1177 | static int mlxsw_sp_port_set_phys_id(struct net_device *dev, |
| 1178 | enum ethtool_phys_id_state state) |
| 1179 | { |
| 1180 | struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev); |
| 1181 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
| 1182 | char mlcr_pl[MLXSW_REG_MLCR_LEN]; |
| 1183 | bool active; |
| 1184 | |
| 1185 | switch (state) { |
| 1186 | case ETHTOOL_ID_ACTIVE: |
| 1187 | active = true; |
| 1188 | break; |
| 1189 | case ETHTOOL_ID_INACTIVE: |
| 1190 | active = false; |
| 1191 | break; |
| 1192 | default: |
| 1193 | return -EOPNOTSUPP; |
| 1194 | } |
| 1195 | |
| 1196 | mlxsw_reg_mlcr_pack(mlcr_pl, mlxsw_sp_port->local_port, active); |
| 1197 | return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(mlcr), mlcr_pl); |
| 1198 | } |
| 1199 | |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1200 | static void mlxsw_sp_port_get_stats(struct net_device *dev, |
| 1201 | struct ethtool_stats *stats, u64 *data) |
| 1202 | { |
| 1203 | struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev); |
| 1204 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
| 1205 | char ppcnt_pl[MLXSW_REG_PPCNT_LEN]; |
| 1206 | int i; |
| 1207 | int err; |
| 1208 | |
Ido Schimmel | 34dba0a | 2016-04-06 17:10:15 +0200 | [diff] [blame] | 1209 | mlxsw_reg_ppcnt_pack(ppcnt_pl, mlxsw_sp_port->local_port, |
| 1210 | MLXSW_REG_PPCNT_IEEE_8023_CNT, 0); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1211 | err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(ppcnt), ppcnt_pl); |
| 1212 | for (i = 0; i < MLXSW_SP_PORT_HW_STATS_LEN; i++) |
| 1213 | data[i] = !err ? mlxsw_sp_port_hw_stats[i].getter(ppcnt_pl) : 0; |
| 1214 | } |
| 1215 | |
| 1216 | static int mlxsw_sp_port_get_sset_count(struct net_device *dev, int sset) |
| 1217 | { |
| 1218 | switch (sset) { |
| 1219 | case ETH_SS_STATS: |
| 1220 | return MLXSW_SP_PORT_HW_STATS_LEN; |
| 1221 | default: |
| 1222 | return -EOPNOTSUPP; |
| 1223 | } |
| 1224 | } |
| 1225 | |
| 1226 | struct mlxsw_sp_port_link_mode { |
| 1227 | u32 mask; |
| 1228 | u32 supported; |
| 1229 | u32 advertised; |
| 1230 | u32 speed; |
| 1231 | }; |
| 1232 | |
| 1233 | static const struct mlxsw_sp_port_link_mode mlxsw_sp_port_link_mode[] = { |
| 1234 | { |
| 1235 | .mask = MLXSW_REG_PTYS_ETH_SPEED_100BASE_T, |
| 1236 | .supported = SUPPORTED_100baseT_Full, |
| 1237 | .advertised = ADVERTISED_100baseT_Full, |
| 1238 | .speed = 100, |
| 1239 | }, |
| 1240 | { |
| 1241 | .mask = MLXSW_REG_PTYS_ETH_SPEED_100BASE_TX, |
| 1242 | .speed = 100, |
| 1243 | }, |
| 1244 | { |
| 1245 | .mask = MLXSW_REG_PTYS_ETH_SPEED_SGMII | |
| 1246 | MLXSW_REG_PTYS_ETH_SPEED_1000BASE_KX, |
| 1247 | .supported = SUPPORTED_1000baseKX_Full, |
| 1248 | .advertised = ADVERTISED_1000baseKX_Full, |
| 1249 | .speed = 1000, |
| 1250 | }, |
| 1251 | { |
| 1252 | .mask = MLXSW_REG_PTYS_ETH_SPEED_10GBASE_T, |
| 1253 | .supported = SUPPORTED_10000baseT_Full, |
| 1254 | .advertised = ADVERTISED_10000baseT_Full, |
| 1255 | .speed = 10000, |
| 1256 | }, |
| 1257 | { |
| 1258 | .mask = MLXSW_REG_PTYS_ETH_SPEED_10GBASE_CX4 | |
| 1259 | MLXSW_REG_PTYS_ETH_SPEED_10GBASE_KX4, |
| 1260 | .supported = SUPPORTED_10000baseKX4_Full, |
| 1261 | .advertised = ADVERTISED_10000baseKX4_Full, |
| 1262 | .speed = 10000, |
| 1263 | }, |
| 1264 | { |
| 1265 | .mask = MLXSW_REG_PTYS_ETH_SPEED_10GBASE_KR | |
| 1266 | MLXSW_REG_PTYS_ETH_SPEED_10GBASE_CR | |
| 1267 | MLXSW_REG_PTYS_ETH_SPEED_10GBASE_SR | |
| 1268 | MLXSW_REG_PTYS_ETH_SPEED_10GBASE_ER_LR, |
| 1269 | .supported = SUPPORTED_10000baseKR_Full, |
| 1270 | .advertised = ADVERTISED_10000baseKR_Full, |
| 1271 | .speed = 10000, |
| 1272 | }, |
| 1273 | { |
| 1274 | .mask = MLXSW_REG_PTYS_ETH_SPEED_20GBASE_KR2, |
| 1275 | .supported = SUPPORTED_20000baseKR2_Full, |
| 1276 | .advertised = ADVERTISED_20000baseKR2_Full, |
| 1277 | .speed = 20000, |
| 1278 | }, |
| 1279 | { |
| 1280 | .mask = MLXSW_REG_PTYS_ETH_SPEED_40GBASE_CR4, |
| 1281 | .supported = SUPPORTED_40000baseCR4_Full, |
| 1282 | .advertised = ADVERTISED_40000baseCR4_Full, |
| 1283 | .speed = 40000, |
| 1284 | }, |
| 1285 | { |
| 1286 | .mask = MLXSW_REG_PTYS_ETH_SPEED_40GBASE_KR4, |
| 1287 | .supported = SUPPORTED_40000baseKR4_Full, |
| 1288 | .advertised = ADVERTISED_40000baseKR4_Full, |
| 1289 | .speed = 40000, |
| 1290 | }, |
| 1291 | { |
| 1292 | .mask = MLXSW_REG_PTYS_ETH_SPEED_40GBASE_SR4, |
| 1293 | .supported = SUPPORTED_40000baseSR4_Full, |
| 1294 | .advertised = ADVERTISED_40000baseSR4_Full, |
| 1295 | .speed = 40000, |
| 1296 | }, |
| 1297 | { |
| 1298 | .mask = MLXSW_REG_PTYS_ETH_SPEED_40GBASE_LR4_ER4, |
| 1299 | .supported = SUPPORTED_40000baseLR4_Full, |
| 1300 | .advertised = ADVERTISED_40000baseLR4_Full, |
| 1301 | .speed = 40000, |
| 1302 | }, |
| 1303 | { |
| 1304 | .mask = MLXSW_REG_PTYS_ETH_SPEED_25GBASE_CR | |
| 1305 | MLXSW_REG_PTYS_ETH_SPEED_25GBASE_KR | |
| 1306 | MLXSW_REG_PTYS_ETH_SPEED_25GBASE_SR, |
| 1307 | .speed = 25000, |
| 1308 | }, |
| 1309 | { |
| 1310 | .mask = MLXSW_REG_PTYS_ETH_SPEED_50GBASE_KR4 | |
| 1311 | MLXSW_REG_PTYS_ETH_SPEED_50GBASE_CR2 | |
| 1312 | MLXSW_REG_PTYS_ETH_SPEED_50GBASE_KR2, |
| 1313 | .speed = 50000, |
| 1314 | }, |
| 1315 | { |
| 1316 | .mask = MLXSW_REG_PTYS_ETH_SPEED_56GBASE_R4, |
| 1317 | .supported = SUPPORTED_56000baseKR4_Full, |
| 1318 | .advertised = ADVERTISED_56000baseKR4_Full, |
| 1319 | .speed = 56000, |
| 1320 | }, |
| 1321 | { |
| 1322 | .mask = MLXSW_REG_PTYS_ETH_SPEED_100GBASE_CR4 | |
| 1323 | MLXSW_REG_PTYS_ETH_SPEED_100GBASE_SR4 | |
| 1324 | MLXSW_REG_PTYS_ETH_SPEED_100GBASE_KR4 | |
| 1325 | MLXSW_REG_PTYS_ETH_SPEED_100GBASE_LR4_ER4, |
| 1326 | .speed = 100000, |
| 1327 | }, |
| 1328 | }; |
| 1329 | |
| 1330 | #define MLXSW_SP_PORT_LINK_MODE_LEN ARRAY_SIZE(mlxsw_sp_port_link_mode) |
| 1331 | |
| 1332 | static u32 mlxsw_sp_from_ptys_supported_port(u32 ptys_eth_proto) |
| 1333 | { |
| 1334 | if (ptys_eth_proto & (MLXSW_REG_PTYS_ETH_SPEED_10GBASE_CR | |
| 1335 | MLXSW_REG_PTYS_ETH_SPEED_10GBASE_SR | |
| 1336 | MLXSW_REG_PTYS_ETH_SPEED_40GBASE_CR4 | |
| 1337 | MLXSW_REG_PTYS_ETH_SPEED_40GBASE_SR4 | |
| 1338 | MLXSW_REG_PTYS_ETH_SPEED_100GBASE_SR4 | |
| 1339 | MLXSW_REG_PTYS_ETH_SPEED_SGMII)) |
| 1340 | return SUPPORTED_FIBRE; |
| 1341 | |
| 1342 | if (ptys_eth_proto & (MLXSW_REG_PTYS_ETH_SPEED_10GBASE_KR | |
| 1343 | MLXSW_REG_PTYS_ETH_SPEED_10GBASE_KX4 | |
| 1344 | MLXSW_REG_PTYS_ETH_SPEED_40GBASE_KR4 | |
| 1345 | MLXSW_REG_PTYS_ETH_SPEED_100GBASE_KR4 | |
| 1346 | MLXSW_REG_PTYS_ETH_SPEED_1000BASE_KX)) |
| 1347 | return SUPPORTED_Backplane; |
| 1348 | return 0; |
| 1349 | } |
| 1350 | |
| 1351 | static u32 mlxsw_sp_from_ptys_supported_link(u32 ptys_eth_proto) |
| 1352 | { |
| 1353 | u32 modes = 0; |
| 1354 | int i; |
| 1355 | |
| 1356 | for (i = 0; i < MLXSW_SP_PORT_LINK_MODE_LEN; i++) { |
| 1357 | if (ptys_eth_proto & mlxsw_sp_port_link_mode[i].mask) |
| 1358 | modes |= mlxsw_sp_port_link_mode[i].supported; |
| 1359 | } |
| 1360 | return modes; |
| 1361 | } |
| 1362 | |
| 1363 | static u32 mlxsw_sp_from_ptys_advert_link(u32 ptys_eth_proto) |
| 1364 | { |
| 1365 | u32 modes = 0; |
| 1366 | int i; |
| 1367 | |
| 1368 | for (i = 0; i < MLXSW_SP_PORT_LINK_MODE_LEN; i++) { |
| 1369 | if (ptys_eth_proto & mlxsw_sp_port_link_mode[i].mask) |
| 1370 | modes |= mlxsw_sp_port_link_mode[i].advertised; |
| 1371 | } |
| 1372 | return modes; |
| 1373 | } |
| 1374 | |
| 1375 | static void mlxsw_sp_from_ptys_speed_duplex(bool carrier_ok, u32 ptys_eth_proto, |
| 1376 | struct ethtool_cmd *cmd) |
| 1377 | { |
| 1378 | u32 speed = SPEED_UNKNOWN; |
| 1379 | u8 duplex = DUPLEX_UNKNOWN; |
| 1380 | int i; |
| 1381 | |
| 1382 | if (!carrier_ok) |
| 1383 | goto out; |
| 1384 | |
| 1385 | for (i = 0; i < MLXSW_SP_PORT_LINK_MODE_LEN; i++) { |
| 1386 | if (ptys_eth_proto & mlxsw_sp_port_link_mode[i].mask) { |
| 1387 | speed = mlxsw_sp_port_link_mode[i].speed; |
| 1388 | duplex = DUPLEX_FULL; |
| 1389 | break; |
| 1390 | } |
| 1391 | } |
| 1392 | out: |
| 1393 | ethtool_cmd_speed_set(cmd, speed); |
| 1394 | cmd->duplex = duplex; |
| 1395 | } |
| 1396 | |
| 1397 | static u8 mlxsw_sp_port_connector_port(u32 ptys_eth_proto) |
| 1398 | { |
| 1399 | if (ptys_eth_proto & (MLXSW_REG_PTYS_ETH_SPEED_10GBASE_SR | |
| 1400 | MLXSW_REG_PTYS_ETH_SPEED_40GBASE_SR4 | |
| 1401 | MLXSW_REG_PTYS_ETH_SPEED_100GBASE_SR4 | |
| 1402 | MLXSW_REG_PTYS_ETH_SPEED_SGMII)) |
| 1403 | return PORT_FIBRE; |
| 1404 | |
| 1405 | if (ptys_eth_proto & (MLXSW_REG_PTYS_ETH_SPEED_10GBASE_CR | |
| 1406 | MLXSW_REG_PTYS_ETH_SPEED_40GBASE_CR4 | |
| 1407 | MLXSW_REG_PTYS_ETH_SPEED_100GBASE_CR4)) |
| 1408 | return PORT_DA; |
| 1409 | |
| 1410 | if (ptys_eth_proto & (MLXSW_REG_PTYS_ETH_SPEED_10GBASE_KR | |
| 1411 | MLXSW_REG_PTYS_ETH_SPEED_10GBASE_KX4 | |
| 1412 | MLXSW_REG_PTYS_ETH_SPEED_40GBASE_KR4 | |
| 1413 | MLXSW_REG_PTYS_ETH_SPEED_100GBASE_KR4)) |
| 1414 | return PORT_NONE; |
| 1415 | |
| 1416 | return PORT_OTHER; |
| 1417 | } |
| 1418 | |
| 1419 | static int mlxsw_sp_port_get_settings(struct net_device *dev, |
| 1420 | struct ethtool_cmd *cmd) |
| 1421 | { |
| 1422 | struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev); |
| 1423 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
| 1424 | char ptys_pl[MLXSW_REG_PTYS_LEN]; |
| 1425 | u32 eth_proto_cap; |
| 1426 | u32 eth_proto_admin; |
| 1427 | u32 eth_proto_oper; |
| 1428 | int err; |
| 1429 | |
| 1430 | mlxsw_reg_ptys_pack(ptys_pl, mlxsw_sp_port->local_port, 0); |
| 1431 | err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(ptys), ptys_pl); |
| 1432 | if (err) { |
| 1433 | netdev_err(dev, "Failed to get proto"); |
| 1434 | return err; |
| 1435 | } |
| 1436 | mlxsw_reg_ptys_unpack(ptys_pl, ð_proto_cap, |
| 1437 | ð_proto_admin, ð_proto_oper); |
| 1438 | |
| 1439 | cmd->supported = mlxsw_sp_from_ptys_supported_port(eth_proto_cap) | |
| 1440 | mlxsw_sp_from_ptys_supported_link(eth_proto_cap) | |
| 1441 | SUPPORTED_Pause | SUPPORTED_Asym_Pause; |
| 1442 | cmd->advertising = mlxsw_sp_from_ptys_advert_link(eth_proto_admin); |
| 1443 | mlxsw_sp_from_ptys_speed_duplex(netif_carrier_ok(dev), |
| 1444 | eth_proto_oper, cmd); |
| 1445 | |
| 1446 | eth_proto_oper = eth_proto_oper ? eth_proto_oper : eth_proto_cap; |
| 1447 | cmd->port = mlxsw_sp_port_connector_port(eth_proto_oper); |
| 1448 | cmd->lp_advertising = mlxsw_sp_from_ptys_advert_link(eth_proto_oper); |
| 1449 | |
| 1450 | cmd->transceiver = XCVR_INTERNAL; |
| 1451 | return 0; |
| 1452 | } |
| 1453 | |
| 1454 | static u32 mlxsw_sp_to_ptys_advert_link(u32 advertising) |
| 1455 | { |
| 1456 | u32 ptys_proto = 0; |
| 1457 | int i; |
| 1458 | |
| 1459 | for (i = 0; i < MLXSW_SP_PORT_LINK_MODE_LEN; i++) { |
| 1460 | if (advertising & mlxsw_sp_port_link_mode[i].advertised) |
| 1461 | ptys_proto |= mlxsw_sp_port_link_mode[i].mask; |
| 1462 | } |
| 1463 | return ptys_proto; |
| 1464 | } |
| 1465 | |
| 1466 | static u32 mlxsw_sp_to_ptys_speed(u32 speed) |
| 1467 | { |
| 1468 | u32 ptys_proto = 0; |
| 1469 | int i; |
| 1470 | |
| 1471 | for (i = 0; i < MLXSW_SP_PORT_LINK_MODE_LEN; i++) { |
| 1472 | if (speed == mlxsw_sp_port_link_mode[i].speed) |
| 1473 | ptys_proto |= mlxsw_sp_port_link_mode[i].mask; |
| 1474 | } |
| 1475 | return ptys_proto; |
| 1476 | } |
| 1477 | |
Ido Schimmel | 18f1e70 | 2016-02-26 17:32:31 +0100 | [diff] [blame] | 1478 | static u32 mlxsw_sp_to_ptys_upper_speed(u32 upper_speed) |
| 1479 | { |
| 1480 | u32 ptys_proto = 0; |
| 1481 | int i; |
| 1482 | |
| 1483 | for (i = 0; i < MLXSW_SP_PORT_LINK_MODE_LEN; i++) { |
| 1484 | if (mlxsw_sp_port_link_mode[i].speed <= upper_speed) |
| 1485 | ptys_proto |= mlxsw_sp_port_link_mode[i].mask; |
| 1486 | } |
| 1487 | return ptys_proto; |
| 1488 | } |
| 1489 | |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1490 | static int mlxsw_sp_port_set_settings(struct net_device *dev, |
| 1491 | struct ethtool_cmd *cmd) |
| 1492 | { |
| 1493 | struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev); |
| 1494 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
| 1495 | char ptys_pl[MLXSW_REG_PTYS_LEN]; |
| 1496 | u32 speed; |
| 1497 | u32 eth_proto_new; |
| 1498 | u32 eth_proto_cap; |
| 1499 | u32 eth_proto_admin; |
| 1500 | bool is_up; |
| 1501 | int err; |
| 1502 | |
| 1503 | speed = ethtool_cmd_speed(cmd); |
| 1504 | |
| 1505 | eth_proto_new = cmd->autoneg == AUTONEG_ENABLE ? |
| 1506 | mlxsw_sp_to_ptys_advert_link(cmd->advertising) : |
| 1507 | mlxsw_sp_to_ptys_speed(speed); |
| 1508 | |
| 1509 | mlxsw_reg_ptys_pack(ptys_pl, mlxsw_sp_port->local_port, 0); |
| 1510 | err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(ptys), ptys_pl); |
| 1511 | if (err) { |
| 1512 | netdev_err(dev, "Failed to get proto"); |
| 1513 | return err; |
| 1514 | } |
| 1515 | mlxsw_reg_ptys_unpack(ptys_pl, ð_proto_cap, ð_proto_admin, NULL); |
| 1516 | |
| 1517 | eth_proto_new = eth_proto_new & eth_proto_cap; |
| 1518 | if (!eth_proto_new) { |
| 1519 | netdev_err(dev, "Not supported proto admin requested"); |
| 1520 | return -EINVAL; |
| 1521 | } |
| 1522 | if (eth_proto_new == eth_proto_admin) |
| 1523 | return 0; |
| 1524 | |
| 1525 | mlxsw_reg_ptys_pack(ptys_pl, mlxsw_sp_port->local_port, eth_proto_new); |
| 1526 | err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ptys), ptys_pl); |
| 1527 | if (err) { |
| 1528 | netdev_err(dev, "Failed to set proto admin"); |
| 1529 | return err; |
| 1530 | } |
| 1531 | |
| 1532 | err = mlxsw_sp_port_oper_status_get(mlxsw_sp_port, &is_up); |
| 1533 | if (err) { |
| 1534 | netdev_err(dev, "Failed to get oper status"); |
| 1535 | return err; |
| 1536 | } |
| 1537 | if (!is_up) |
| 1538 | return 0; |
| 1539 | |
| 1540 | err = mlxsw_sp_port_admin_status_set(mlxsw_sp_port, false); |
| 1541 | if (err) { |
| 1542 | netdev_err(dev, "Failed to set admin status"); |
| 1543 | return err; |
| 1544 | } |
| 1545 | |
| 1546 | err = mlxsw_sp_port_admin_status_set(mlxsw_sp_port, true); |
| 1547 | if (err) { |
| 1548 | netdev_err(dev, "Failed to set admin status"); |
| 1549 | return err; |
| 1550 | } |
| 1551 | |
| 1552 | return 0; |
| 1553 | } |
| 1554 | |
| 1555 | static const struct ethtool_ops mlxsw_sp_port_ethtool_ops = { |
| 1556 | .get_drvinfo = mlxsw_sp_port_get_drvinfo, |
| 1557 | .get_link = ethtool_op_get_link, |
Ido Schimmel | 9f7ec05 | 2016-04-06 17:10:14 +0200 | [diff] [blame] | 1558 | .get_pauseparam = mlxsw_sp_port_get_pauseparam, |
| 1559 | .set_pauseparam = mlxsw_sp_port_set_pauseparam, |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1560 | .get_strings = mlxsw_sp_port_get_strings, |
Ido Schimmel | 3a66ee3 | 2015-11-27 13:45:55 +0100 | [diff] [blame] | 1561 | .set_phys_id = mlxsw_sp_port_set_phys_id, |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1562 | .get_ethtool_stats = mlxsw_sp_port_get_stats, |
| 1563 | .get_sset_count = mlxsw_sp_port_get_sset_count, |
| 1564 | .get_settings = mlxsw_sp_port_get_settings, |
| 1565 | .set_settings = mlxsw_sp_port_set_settings, |
| 1566 | }; |
| 1567 | |
Ido Schimmel | 18f1e70 | 2016-02-26 17:32:31 +0100 | [diff] [blame] | 1568 | static int |
| 1569 | mlxsw_sp_port_speed_by_width_set(struct mlxsw_sp_port *mlxsw_sp_port, u8 width) |
| 1570 | { |
| 1571 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
| 1572 | u32 upper_speed = MLXSW_SP_PORT_BASE_SPEED * width; |
| 1573 | char ptys_pl[MLXSW_REG_PTYS_LEN]; |
| 1574 | u32 eth_proto_admin; |
| 1575 | |
| 1576 | eth_proto_admin = mlxsw_sp_to_ptys_upper_speed(upper_speed); |
| 1577 | mlxsw_reg_ptys_pack(ptys_pl, mlxsw_sp_port->local_port, |
| 1578 | eth_proto_admin); |
| 1579 | return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ptys), ptys_pl); |
| 1580 | } |
| 1581 | |
Ido Schimmel | 8e8dfe9 | 2016-04-06 17:10:10 +0200 | [diff] [blame] | 1582 | int mlxsw_sp_port_ets_set(struct mlxsw_sp_port *mlxsw_sp_port, |
| 1583 | enum mlxsw_reg_qeec_hr hr, u8 index, u8 next_index, |
| 1584 | bool dwrr, u8 dwrr_weight) |
Ido Schimmel | 90183b9 | 2016-04-06 17:10:08 +0200 | [diff] [blame] | 1585 | { |
| 1586 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
| 1587 | char qeec_pl[MLXSW_REG_QEEC_LEN]; |
| 1588 | |
| 1589 | mlxsw_reg_qeec_pack(qeec_pl, mlxsw_sp_port->local_port, hr, index, |
| 1590 | next_index); |
| 1591 | mlxsw_reg_qeec_de_set(qeec_pl, true); |
| 1592 | mlxsw_reg_qeec_dwrr_set(qeec_pl, dwrr); |
| 1593 | mlxsw_reg_qeec_dwrr_weight_set(qeec_pl, dwrr_weight); |
| 1594 | return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(qeec), qeec_pl); |
| 1595 | } |
| 1596 | |
Ido Schimmel | cc7cf51 | 2016-04-06 17:10:11 +0200 | [diff] [blame] | 1597 | int mlxsw_sp_port_ets_maxrate_set(struct mlxsw_sp_port *mlxsw_sp_port, |
| 1598 | enum mlxsw_reg_qeec_hr hr, u8 index, |
| 1599 | u8 next_index, u32 maxrate) |
Ido Schimmel | 90183b9 | 2016-04-06 17:10:08 +0200 | [diff] [blame] | 1600 | { |
| 1601 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
| 1602 | char qeec_pl[MLXSW_REG_QEEC_LEN]; |
| 1603 | |
| 1604 | mlxsw_reg_qeec_pack(qeec_pl, mlxsw_sp_port->local_port, hr, index, |
| 1605 | next_index); |
| 1606 | mlxsw_reg_qeec_mase_set(qeec_pl, true); |
| 1607 | mlxsw_reg_qeec_max_shaper_rate_set(qeec_pl, maxrate); |
| 1608 | return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(qeec), qeec_pl); |
| 1609 | } |
| 1610 | |
Ido Schimmel | 8e8dfe9 | 2016-04-06 17:10:10 +0200 | [diff] [blame] | 1611 | int mlxsw_sp_port_prio_tc_set(struct mlxsw_sp_port *mlxsw_sp_port, |
| 1612 | u8 switch_prio, u8 tclass) |
Ido Schimmel | 90183b9 | 2016-04-06 17:10:08 +0200 | [diff] [blame] | 1613 | { |
| 1614 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
| 1615 | char qtct_pl[MLXSW_REG_QTCT_LEN]; |
| 1616 | |
| 1617 | mlxsw_reg_qtct_pack(qtct_pl, mlxsw_sp_port->local_port, switch_prio, |
| 1618 | tclass); |
| 1619 | return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(qtct), qtct_pl); |
| 1620 | } |
| 1621 | |
| 1622 | static int mlxsw_sp_port_ets_init(struct mlxsw_sp_port *mlxsw_sp_port) |
| 1623 | { |
| 1624 | int err, i; |
| 1625 | |
| 1626 | /* Setup the elements hierarcy, so that each TC is linked to |
| 1627 | * one subgroup, which are all member in the same group. |
| 1628 | */ |
| 1629 | err = mlxsw_sp_port_ets_set(mlxsw_sp_port, |
| 1630 | MLXSW_REG_QEEC_HIERARCY_GROUP, 0, 0, false, |
| 1631 | 0); |
| 1632 | if (err) |
| 1633 | return err; |
| 1634 | for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) { |
| 1635 | err = mlxsw_sp_port_ets_set(mlxsw_sp_port, |
| 1636 | MLXSW_REG_QEEC_HIERARCY_SUBGROUP, i, |
| 1637 | 0, false, 0); |
| 1638 | if (err) |
| 1639 | return err; |
| 1640 | } |
| 1641 | for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) { |
| 1642 | err = mlxsw_sp_port_ets_set(mlxsw_sp_port, |
| 1643 | MLXSW_REG_QEEC_HIERARCY_TC, i, i, |
| 1644 | false, 0); |
| 1645 | if (err) |
| 1646 | return err; |
| 1647 | } |
| 1648 | |
| 1649 | /* Make sure the max shaper is disabled in all hierarcies that |
| 1650 | * support it. |
| 1651 | */ |
| 1652 | err = mlxsw_sp_port_ets_maxrate_set(mlxsw_sp_port, |
| 1653 | MLXSW_REG_QEEC_HIERARCY_PORT, 0, 0, |
| 1654 | MLXSW_REG_QEEC_MAS_DIS); |
| 1655 | if (err) |
| 1656 | return err; |
| 1657 | for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) { |
| 1658 | err = mlxsw_sp_port_ets_maxrate_set(mlxsw_sp_port, |
| 1659 | MLXSW_REG_QEEC_HIERARCY_SUBGROUP, |
| 1660 | i, 0, |
| 1661 | MLXSW_REG_QEEC_MAS_DIS); |
| 1662 | if (err) |
| 1663 | return err; |
| 1664 | } |
| 1665 | for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) { |
| 1666 | err = mlxsw_sp_port_ets_maxrate_set(mlxsw_sp_port, |
| 1667 | MLXSW_REG_QEEC_HIERARCY_TC, |
| 1668 | i, i, |
| 1669 | MLXSW_REG_QEEC_MAS_DIS); |
| 1670 | if (err) |
| 1671 | return err; |
| 1672 | } |
| 1673 | |
| 1674 | /* Map all priorities to traffic class 0. */ |
| 1675 | for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) { |
| 1676 | err = mlxsw_sp_port_prio_tc_set(mlxsw_sp_port, i, 0); |
| 1677 | if (err) |
| 1678 | return err; |
| 1679 | } |
| 1680 | |
| 1681 | return 0; |
| 1682 | } |
| 1683 | |
Ido Schimmel | 18f1e70 | 2016-02-26 17:32:31 +0100 | [diff] [blame] | 1684 | static int __mlxsw_sp_port_create(struct mlxsw_sp *mlxsw_sp, u8 local_port, |
| 1685 | bool split, u8 module, u8 width) |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1686 | { |
| 1687 | struct mlxsw_sp_port *mlxsw_sp_port; |
| 1688 | struct net_device *dev; |
Ido Schimmel | bd40e9d | 2015-12-15 16:03:36 +0100 | [diff] [blame] | 1689 | size_t bytes; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1690 | int err; |
| 1691 | |
| 1692 | dev = alloc_etherdev(sizeof(struct mlxsw_sp_port)); |
| 1693 | if (!dev) |
| 1694 | return -ENOMEM; |
| 1695 | mlxsw_sp_port = netdev_priv(dev); |
| 1696 | mlxsw_sp_port->dev = dev; |
| 1697 | mlxsw_sp_port->mlxsw_sp = mlxsw_sp; |
| 1698 | mlxsw_sp_port->local_port = local_port; |
Ido Schimmel | 18f1e70 | 2016-02-26 17:32:31 +0100 | [diff] [blame] | 1699 | mlxsw_sp_port->split = split; |
Ido Schimmel | bd40e9d | 2015-12-15 16:03:36 +0100 | [diff] [blame] | 1700 | bytes = DIV_ROUND_UP(VLAN_N_VID, BITS_PER_BYTE); |
| 1701 | mlxsw_sp_port->active_vlans = kzalloc(bytes, GFP_KERNEL); |
| 1702 | if (!mlxsw_sp_port->active_vlans) { |
| 1703 | err = -ENOMEM; |
| 1704 | goto err_port_active_vlans_alloc; |
| 1705 | } |
Elad Raz | fc1273a | 2016-01-06 13:01:11 +0100 | [diff] [blame] | 1706 | mlxsw_sp_port->untagged_vlans = kzalloc(bytes, GFP_KERNEL); |
| 1707 | if (!mlxsw_sp_port->untagged_vlans) { |
| 1708 | err = -ENOMEM; |
| 1709 | goto err_port_untagged_vlans_alloc; |
| 1710 | } |
Ido Schimmel | 7f71eb4 | 2015-12-15 16:03:37 +0100 | [diff] [blame] | 1711 | INIT_LIST_HEAD(&mlxsw_sp_port->vports_list); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1712 | |
| 1713 | mlxsw_sp_port->pcpu_stats = |
| 1714 | netdev_alloc_pcpu_stats(struct mlxsw_sp_port_pcpu_stats); |
| 1715 | if (!mlxsw_sp_port->pcpu_stats) { |
| 1716 | err = -ENOMEM; |
| 1717 | goto err_alloc_stats; |
| 1718 | } |
| 1719 | |
| 1720 | dev->netdev_ops = &mlxsw_sp_port_netdev_ops; |
| 1721 | dev->ethtool_ops = &mlxsw_sp_port_ethtool_ops; |
| 1722 | |
| 1723 | err = mlxsw_sp_port_dev_addr_init(mlxsw_sp_port); |
| 1724 | if (err) { |
| 1725 | dev_err(mlxsw_sp->bus_info->dev, "Port %d: Unable to init port mac address\n", |
| 1726 | mlxsw_sp_port->local_port); |
| 1727 | goto err_dev_addr_init; |
| 1728 | } |
| 1729 | |
| 1730 | netif_carrier_off(dev); |
| 1731 | |
| 1732 | dev->features |= NETIF_F_NETNS_LOCAL | NETIF_F_LLTX | NETIF_F_SG | |
| 1733 | NETIF_F_HW_VLAN_CTAG_FILTER; |
| 1734 | |
| 1735 | /* Each packet needs to have a Tx header (metadata) on top all other |
| 1736 | * headers. |
| 1737 | */ |
| 1738 | dev->hard_header_len += MLXSW_TXHDR_LEN; |
| 1739 | |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1740 | err = mlxsw_sp_port_system_port_mapping_set(mlxsw_sp_port); |
| 1741 | if (err) { |
| 1742 | dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to set system port mapping\n", |
| 1743 | mlxsw_sp_port->local_port); |
| 1744 | goto err_port_system_port_mapping_set; |
| 1745 | } |
| 1746 | |
| 1747 | err = mlxsw_sp_port_swid_set(mlxsw_sp_port, 0); |
| 1748 | if (err) { |
| 1749 | dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to set SWID\n", |
| 1750 | mlxsw_sp_port->local_port); |
| 1751 | goto err_port_swid_set; |
| 1752 | } |
| 1753 | |
Ido Schimmel | 18f1e70 | 2016-02-26 17:32:31 +0100 | [diff] [blame] | 1754 | err = mlxsw_sp_port_speed_by_width_set(mlxsw_sp_port, width); |
| 1755 | if (err) { |
| 1756 | dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to enable speeds\n", |
| 1757 | mlxsw_sp_port->local_port); |
| 1758 | goto err_port_speed_by_width_set; |
| 1759 | } |
| 1760 | |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1761 | err = mlxsw_sp_port_mtu_set(mlxsw_sp_port, ETH_DATA_LEN); |
| 1762 | if (err) { |
| 1763 | dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to set MTU\n", |
| 1764 | mlxsw_sp_port->local_port); |
| 1765 | goto err_port_mtu_set; |
| 1766 | } |
| 1767 | |
| 1768 | err = mlxsw_sp_port_admin_status_set(mlxsw_sp_port, false); |
| 1769 | if (err) |
| 1770 | goto err_port_admin_status_set; |
| 1771 | |
| 1772 | err = mlxsw_sp_port_buffers_init(mlxsw_sp_port); |
| 1773 | if (err) { |
| 1774 | dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to initialize buffers\n", |
| 1775 | mlxsw_sp_port->local_port); |
| 1776 | goto err_port_buffers_init; |
| 1777 | } |
| 1778 | |
Ido Schimmel | 90183b9 | 2016-04-06 17:10:08 +0200 | [diff] [blame] | 1779 | err = mlxsw_sp_port_ets_init(mlxsw_sp_port); |
| 1780 | if (err) { |
| 1781 | dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to initialize ETS\n", |
| 1782 | mlxsw_sp_port->local_port); |
| 1783 | goto err_port_ets_init; |
| 1784 | } |
| 1785 | |
Ido Schimmel | f00817d | 2016-04-06 17:10:09 +0200 | [diff] [blame] | 1786 | /* ETS and buffers must be initialized before DCB. */ |
| 1787 | err = mlxsw_sp_port_dcb_init(mlxsw_sp_port); |
| 1788 | if (err) { |
| 1789 | dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to initialize DCB\n", |
| 1790 | mlxsw_sp_port->local_port); |
| 1791 | goto err_port_dcb_init; |
| 1792 | } |
| 1793 | |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1794 | mlxsw_sp_port_switchdev_init(mlxsw_sp_port); |
| 1795 | err = register_netdev(dev); |
| 1796 | if (err) { |
| 1797 | dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to register netdev\n", |
| 1798 | mlxsw_sp_port->local_port); |
| 1799 | goto err_register_netdev; |
| 1800 | } |
| 1801 | |
Jiri Pirko | 932762b | 2016-04-08 19:11:21 +0200 | [diff] [blame] | 1802 | err = mlxsw_core_port_init(mlxsw_sp->core, &mlxsw_sp_port->core_port, |
| 1803 | mlxsw_sp_port->local_port, dev, |
| 1804 | mlxsw_sp_port->split, module); |
| 1805 | if (err) { |
| 1806 | dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to init core port\n", |
| 1807 | mlxsw_sp_port->local_port); |
| 1808 | goto err_core_port_init; |
| 1809 | } |
Jiri Pirko | c474550 | 2016-02-26 17:32:26 +0100 | [diff] [blame] | 1810 | |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1811 | err = mlxsw_sp_port_vlan_init(mlxsw_sp_port); |
| 1812 | if (err) |
| 1813 | goto err_port_vlan_init; |
| 1814 | |
| 1815 | mlxsw_sp->ports[local_port] = mlxsw_sp_port; |
| 1816 | return 0; |
| 1817 | |
| 1818 | err_port_vlan_init: |
Jiri Pirko | 932762b | 2016-04-08 19:11:21 +0200 | [diff] [blame] | 1819 | mlxsw_core_port_fini(&mlxsw_sp_port->core_port); |
| 1820 | err_core_port_init: |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1821 | unregister_netdev(dev); |
| 1822 | err_register_netdev: |
Ido Schimmel | f00817d | 2016-04-06 17:10:09 +0200 | [diff] [blame] | 1823 | err_port_dcb_init: |
Ido Schimmel | 90183b9 | 2016-04-06 17:10:08 +0200 | [diff] [blame] | 1824 | err_port_ets_init: |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1825 | err_port_buffers_init: |
| 1826 | err_port_admin_status_set: |
| 1827 | err_port_mtu_set: |
Ido Schimmel | 18f1e70 | 2016-02-26 17:32:31 +0100 | [diff] [blame] | 1828 | err_port_speed_by_width_set: |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1829 | err_port_swid_set: |
| 1830 | err_port_system_port_mapping_set: |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1831 | err_dev_addr_init: |
| 1832 | free_percpu(mlxsw_sp_port->pcpu_stats); |
| 1833 | err_alloc_stats: |
Elad Raz | fc1273a | 2016-01-06 13:01:11 +0100 | [diff] [blame] | 1834 | kfree(mlxsw_sp_port->untagged_vlans); |
| 1835 | err_port_untagged_vlans_alloc: |
Ido Schimmel | bd40e9d | 2015-12-15 16:03:36 +0100 | [diff] [blame] | 1836 | kfree(mlxsw_sp_port->active_vlans); |
| 1837 | err_port_active_vlans_alloc: |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1838 | free_netdev(dev); |
| 1839 | return err; |
| 1840 | } |
| 1841 | |
Ido Schimmel | 18f1e70 | 2016-02-26 17:32:31 +0100 | [diff] [blame] | 1842 | static int mlxsw_sp_port_create(struct mlxsw_sp *mlxsw_sp, u8 local_port, |
| 1843 | bool split, u8 module, u8 width, u8 lane) |
| 1844 | { |
| 1845 | int err; |
| 1846 | |
| 1847 | err = mlxsw_sp_port_module_map(mlxsw_sp, local_port, module, width, |
| 1848 | lane); |
| 1849 | if (err) |
| 1850 | return err; |
| 1851 | |
| 1852 | err = __mlxsw_sp_port_create(mlxsw_sp, local_port, split, module, |
| 1853 | width); |
| 1854 | if (err) |
| 1855 | goto err_port_create; |
| 1856 | |
| 1857 | return 0; |
| 1858 | |
| 1859 | err_port_create: |
| 1860 | mlxsw_sp_port_module_unmap(mlxsw_sp, local_port); |
| 1861 | return err; |
| 1862 | } |
| 1863 | |
Ido Schimmel | 7f71eb4 | 2015-12-15 16:03:37 +0100 | [diff] [blame] | 1864 | static void mlxsw_sp_port_vports_fini(struct mlxsw_sp_port *mlxsw_sp_port) |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1865 | { |
Ido Schimmel | 7f71eb4 | 2015-12-15 16:03:37 +0100 | [diff] [blame] | 1866 | struct net_device *dev = mlxsw_sp_port->dev; |
| 1867 | struct mlxsw_sp_port *mlxsw_sp_vport, *tmp; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1868 | |
Ido Schimmel | 7f71eb4 | 2015-12-15 16:03:37 +0100 | [diff] [blame] | 1869 | list_for_each_entry_safe(mlxsw_sp_vport, tmp, |
| 1870 | &mlxsw_sp_port->vports_list, vport.list) { |
| 1871 | u16 vid = mlxsw_sp_vport_vid_get(mlxsw_sp_vport); |
| 1872 | |
| 1873 | /* vPorts created for VLAN devices should already be gone |
| 1874 | * by now, since we unregistered the port netdev. |
| 1875 | */ |
| 1876 | WARN_ON(is_vlan_dev(mlxsw_sp_vport->dev)); |
| 1877 | mlxsw_sp_port_kill_vid(dev, 0, vid); |
| 1878 | } |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1879 | } |
| 1880 | |
| 1881 | static void mlxsw_sp_port_remove(struct mlxsw_sp *mlxsw_sp, u8 local_port) |
| 1882 | { |
| 1883 | struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp->ports[local_port]; |
| 1884 | |
| 1885 | if (!mlxsw_sp_port) |
| 1886 | return; |
Ido Schimmel | a133318 | 2016-02-26 17:32:30 +0100 | [diff] [blame] | 1887 | mlxsw_sp->ports[local_port] = NULL; |
Jiri Pirko | 932762b | 2016-04-08 19:11:21 +0200 | [diff] [blame] | 1888 | mlxsw_core_port_fini(&mlxsw_sp_port->core_port); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1889 | unregister_netdev(mlxsw_sp_port->dev); /* This calls ndo_stop */ |
Ido Schimmel | f00817d | 2016-04-06 17:10:09 +0200 | [diff] [blame] | 1890 | mlxsw_sp_port_dcb_fini(mlxsw_sp_port); |
Ido Schimmel | 7f71eb4 | 2015-12-15 16:03:37 +0100 | [diff] [blame] | 1891 | mlxsw_sp_port_vports_fini(mlxsw_sp_port); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1892 | mlxsw_sp_port_switchdev_fini(mlxsw_sp_port); |
Ido Schimmel | 3e9b27b | 2016-02-26 17:32:28 +0100 | [diff] [blame] | 1893 | mlxsw_sp_port_swid_set(mlxsw_sp_port, MLXSW_PORT_SWID_DISABLED_PORT); |
| 1894 | mlxsw_sp_port_module_unmap(mlxsw_sp, mlxsw_sp_port->local_port); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1895 | free_percpu(mlxsw_sp_port->pcpu_stats); |
Elad Raz | fc1273a | 2016-01-06 13:01:11 +0100 | [diff] [blame] | 1896 | kfree(mlxsw_sp_port->untagged_vlans); |
Ido Schimmel | bd40e9d | 2015-12-15 16:03:36 +0100 | [diff] [blame] | 1897 | kfree(mlxsw_sp_port->active_vlans); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1898 | free_netdev(mlxsw_sp_port->dev); |
| 1899 | } |
| 1900 | |
| 1901 | static void mlxsw_sp_ports_remove(struct mlxsw_sp *mlxsw_sp) |
| 1902 | { |
| 1903 | int i; |
| 1904 | |
| 1905 | for (i = 1; i < MLXSW_PORT_MAX_PORTS; i++) |
| 1906 | mlxsw_sp_port_remove(mlxsw_sp, i); |
| 1907 | kfree(mlxsw_sp->ports); |
| 1908 | } |
| 1909 | |
| 1910 | static int mlxsw_sp_ports_create(struct mlxsw_sp *mlxsw_sp) |
| 1911 | { |
| 1912 | size_t alloc_size; |
Ido Schimmel | 558c2d5 | 2016-02-26 17:32:29 +0100 | [diff] [blame] | 1913 | u8 module, width; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1914 | int i; |
| 1915 | int err; |
| 1916 | |
| 1917 | alloc_size = sizeof(struct mlxsw_sp_port *) * MLXSW_PORT_MAX_PORTS; |
| 1918 | mlxsw_sp->ports = kzalloc(alloc_size, GFP_KERNEL); |
| 1919 | if (!mlxsw_sp->ports) |
| 1920 | return -ENOMEM; |
| 1921 | |
| 1922 | for (i = 1; i < MLXSW_PORT_MAX_PORTS; i++) { |
Ido Schimmel | 558c2d5 | 2016-02-26 17:32:29 +0100 | [diff] [blame] | 1923 | err = mlxsw_sp_port_module_info_get(mlxsw_sp, i, &module, |
| 1924 | &width); |
| 1925 | if (err) |
| 1926 | goto err_port_module_info_get; |
| 1927 | if (!width) |
| 1928 | continue; |
| 1929 | mlxsw_sp->port_to_module[i] = module; |
Ido Schimmel | 18f1e70 | 2016-02-26 17:32:31 +0100 | [diff] [blame] | 1930 | err = __mlxsw_sp_port_create(mlxsw_sp, i, false, module, width); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1931 | if (err) |
| 1932 | goto err_port_create; |
| 1933 | } |
| 1934 | return 0; |
| 1935 | |
| 1936 | err_port_create: |
Ido Schimmel | 558c2d5 | 2016-02-26 17:32:29 +0100 | [diff] [blame] | 1937 | err_port_module_info_get: |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1938 | for (i--; i >= 1; i--) |
| 1939 | mlxsw_sp_port_remove(mlxsw_sp, i); |
| 1940 | kfree(mlxsw_sp->ports); |
| 1941 | return err; |
| 1942 | } |
| 1943 | |
Ido Schimmel | 18f1e70 | 2016-02-26 17:32:31 +0100 | [diff] [blame] | 1944 | static u8 mlxsw_sp_cluster_base_port_get(u8 local_port) |
| 1945 | { |
| 1946 | u8 offset = (local_port - 1) % MLXSW_SP_PORTS_PER_CLUSTER_MAX; |
| 1947 | |
| 1948 | return local_port - offset; |
| 1949 | } |
| 1950 | |
| 1951 | static int mlxsw_sp_port_split(void *priv, u8 local_port, unsigned int count) |
| 1952 | { |
| 1953 | struct mlxsw_sp *mlxsw_sp = priv; |
| 1954 | struct mlxsw_sp_port *mlxsw_sp_port; |
| 1955 | u8 width = MLXSW_PORT_MODULE_MAX_WIDTH / count; |
| 1956 | u8 module, cur_width, base_port; |
| 1957 | int i; |
| 1958 | int err; |
| 1959 | |
| 1960 | mlxsw_sp_port = mlxsw_sp->ports[local_port]; |
| 1961 | if (!mlxsw_sp_port) { |
| 1962 | dev_err(mlxsw_sp->bus_info->dev, "Port number \"%d\" does not exist\n", |
| 1963 | local_port); |
| 1964 | return -EINVAL; |
| 1965 | } |
| 1966 | |
| 1967 | if (count != 2 && count != 4) { |
| 1968 | netdev_err(mlxsw_sp_port->dev, "Port can only be split into 2 or 4 ports\n"); |
| 1969 | return -EINVAL; |
| 1970 | } |
| 1971 | |
| 1972 | err = mlxsw_sp_port_module_info_get(mlxsw_sp, local_port, &module, |
| 1973 | &cur_width); |
| 1974 | if (err) { |
| 1975 | netdev_err(mlxsw_sp_port->dev, "Failed to get port's width\n"); |
| 1976 | return err; |
| 1977 | } |
| 1978 | |
| 1979 | if (cur_width != MLXSW_PORT_MODULE_MAX_WIDTH) { |
| 1980 | netdev_err(mlxsw_sp_port->dev, "Port cannot be split further\n"); |
| 1981 | return -EINVAL; |
| 1982 | } |
| 1983 | |
| 1984 | /* Make sure we have enough slave (even) ports for the split. */ |
| 1985 | if (count == 2) { |
| 1986 | base_port = local_port; |
| 1987 | if (mlxsw_sp->ports[base_port + 1]) { |
| 1988 | netdev_err(mlxsw_sp_port->dev, "Invalid split configuration\n"); |
| 1989 | return -EINVAL; |
| 1990 | } |
| 1991 | } else { |
| 1992 | base_port = mlxsw_sp_cluster_base_port_get(local_port); |
| 1993 | if (mlxsw_sp->ports[base_port + 1] || |
| 1994 | mlxsw_sp->ports[base_port + 3]) { |
| 1995 | netdev_err(mlxsw_sp_port->dev, "Invalid split configuration\n"); |
| 1996 | return -EINVAL; |
| 1997 | } |
| 1998 | } |
| 1999 | |
| 2000 | for (i = 0; i < count; i++) |
| 2001 | mlxsw_sp_port_remove(mlxsw_sp, base_port + i); |
| 2002 | |
| 2003 | for (i = 0; i < count; i++) { |
| 2004 | err = mlxsw_sp_port_create(mlxsw_sp, base_port + i, true, |
| 2005 | module, width, i * width); |
| 2006 | if (err) { |
| 2007 | dev_err(mlxsw_sp->bus_info->dev, "Failed to create split port\n"); |
| 2008 | goto err_port_create; |
| 2009 | } |
| 2010 | } |
| 2011 | |
| 2012 | return 0; |
| 2013 | |
| 2014 | err_port_create: |
| 2015 | for (i--; i >= 0; i--) |
| 2016 | mlxsw_sp_port_remove(mlxsw_sp, base_port + i); |
| 2017 | for (i = 0; i < count / 2; i++) { |
| 2018 | module = mlxsw_sp->port_to_module[base_port + i * 2]; |
| 2019 | mlxsw_sp_port_create(mlxsw_sp, base_port + i * 2, false, |
| 2020 | module, MLXSW_PORT_MODULE_MAX_WIDTH, 0); |
| 2021 | } |
| 2022 | return err; |
| 2023 | } |
| 2024 | |
| 2025 | static int mlxsw_sp_port_unsplit(void *priv, u8 local_port) |
| 2026 | { |
| 2027 | struct mlxsw_sp *mlxsw_sp = priv; |
| 2028 | struct mlxsw_sp_port *mlxsw_sp_port; |
| 2029 | u8 module, cur_width, base_port; |
| 2030 | unsigned int count; |
| 2031 | int i; |
| 2032 | int err; |
| 2033 | |
| 2034 | mlxsw_sp_port = mlxsw_sp->ports[local_port]; |
| 2035 | if (!mlxsw_sp_port) { |
| 2036 | dev_err(mlxsw_sp->bus_info->dev, "Port number \"%d\" does not exist\n", |
| 2037 | local_port); |
| 2038 | return -EINVAL; |
| 2039 | } |
| 2040 | |
| 2041 | if (!mlxsw_sp_port->split) { |
| 2042 | netdev_err(mlxsw_sp_port->dev, "Port wasn't split\n"); |
| 2043 | return -EINVAL; |
| 2044 | } |
| 2045 | |
| 2046 | err = mlxsw_sp_port_module_info_get(mlxsw_sp, local_port, &module, |
| 2047 | &cur_width); |
| 2048 | if (err) { |
| 2049 | netdev_err(mlxsw_sp_port->dev, "Failed to get port's width\n"); |
| 2050 | return err; |
| 2051 | } |
| 2052 | count = cur_width == 1 ? 4 : 2; |
| 2053 | |
| 2054 | base_port = mlxsw_sp_cluster_base_port_get(local_port); |
| 2055 | |
| 2056 | /* Determine which ports to remove. */ |
| 2057 | if (count == 2 && local_port >= base_port + 2) |
| 2058 | base_port = base_port + 2; |
| 2059 | |
| 2060 | for (i = 0; i < count; i++) |
| 2061 | mlxsw_sp_port_remove(mlxsw_sp, base_port + i); |
| 2062 | |
| 2063 | for (i = 0; i < count / 2; i++) { |
| 2064 | module = mlxsw_sp->port_to_module[base_port + i * 2]; |
| 2065 | err = mlxsw_sp_port_create(mlxsw_sp, base_port + i * 2, false, |
| 2066 | module, MLXSW_PORT_MODULE_MAX_WIDTH, |
| 2067 | 0); |
| 2068 | if (err) |
| 2069 | dev_err(mlxsw_sp->bus_info->dev, "Failed to reinstantiate port\n"); |
| 2070 | } |
| 2071 | |
| 2072 | return 0; |
| 2073 | } |
| 2074 | |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 2075 | static void mlxsw_sp_pude_event_func(const struct mlxsw_reg_info *reg, |
| 2076 | char *pude_pl, void *priv) |
| 2077 | { |
| 2078 | struct mlxsw_sp *mlxsw_sp = priv; |
| 2079 | struct mlxsw_sp_port *mlxsw_sp_port; |
| 2080 | enum mlxsw_reg_pude_oper_status status; |
| 2081 | u8 local_port; |
| 2082 | |
| 2083 | local_port = mlxsw_reg_pude_local_port_get(pude_pl); |
| 2084 | mlxsw_sp_port = mlxsw_sp->ports[local_port]; |
| 2085 | if (!mlxsw_sp_port) { |
| 2086 | dev_warn(mlxsw_sp->bus_info->dev, "Port %d: Link event received for non-existent port\n", |
| 2087 | local_port); |
| 2088 | return; |
| 2089 | } |
| 2090 | |
| 2091 | status = mlxsw_reg_pude_oper_status_get(pude_pl); |
| 2092 | if (status == MLXSW_PORT_OPER_STATUS_UP) { |
| 2093 | netdev_info(mlxsw_sp_port->dev, "link up\n"); |
| 2094 | netif_carrier_on(mlxsw_sp_port->dev); |
| 2095 | } else { |
| 2096 | netdev_info(mlxsw_sp_port->dev, "link down\n"); |
| 2097 | netif_carrier_off(mlxsw_sp_port->dev); |
| 2098 | } |
| 2099 | } |
| 2100 | |
| 2101 | static struct mlxsw_event_listener mlxsw_sp_pude_event = { |
| 2102 | .func = mlxsw_sp_pude_event_func, |
| 2103 | .trap_id = MLXSW_TRAP_ID_PUDE, |
| 2104 | }; |
| 2105 | |
| 2106 | static int mlxsw_sp_event_register(struct mlxsw_sp *mlxsw_sp, |
| 2107 | enum mlxsw_event_trap_id trap_id) |
| 2108 | { |
| 2109 | struct mlxsw_event_listener *el; |
| 2110 | char hpkt_pl[MLXSW_REG_HPKT_LEN]; |
| 2111 | int err; |
| 2112 | |
| 2113 | switch (trap_id) { |
| 2114 | case MLXSW_TRAP_ID_PUDE: |
| 2115 | el = &mlxsw_sp_pude_event; |
| 2116 | break; |
| 2117 | } |
| 2118 | err = mlxsw_core_event_listener_register(mlxsw_sp->core, el, mlxsw_sp); |
| 2119 | if (err) |
| 2120 | return err; |
| 2121 | |
| 2122 | mlxsw_reg_hpkt_pack(hpkt_pl, MLXSW_REG_HPKT_ACTION_FORWARD, trap_id); |
| 2123 | err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(hpkt), hpkt_pl); |
| 2124 | if (err) |
| 2125 | goto err_event_trap_set; |
| 2126 | |
| 2127 | return 0; |
| 2128 | |
| 2129 | err_event_trap_set: |
| 2130 | mlxsw_core_event_listener_unregister(mlxsw_sp->core, el, mlxsw_sp); |
| 2131 | return err; |
| 2132 | } |
| 2133 | |
| 2134 | static void mlxsw_sp_event_unregister(struct mlxsw_sp *mlxsw_sp, |
| 2135 | enum mlxsw_event_trap_id trap_id) |
| 2136 | { |
| 2137 | struct mlxsw_event_listener *el; |
| 2138 | |
| 2139 | switch (trap_id) { |
| 2140 | case MLXSW_TRAP_ID_PUDE: |
| 2141 | el = &mlxsw_sp_pude_event; |
| 2142 | break; |
| 2143 | } |
| 2144 | mlxsw_core_event_listener_unregister(mlxsw_sp->core, el, mlxsw_sp); |
| 2145 | } |
| 2146 | |
| 2147 | static void mlxsw_sp_rx_listener_func(struct sk_buff *skb, u8 local_port, |
| 2148 | void *priv) |
| 2149 | { |
| 2150 | struct mlxsw_sp *mlxsw_sp = priv; |
| 2151 | struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp->ports[local_port]; |
| 2152 | struct mlxsw_sp_port_pcpu_stats *pcpu_stats; |
| 2153 | |
| 2154 | if (unlikely(!mlxsw_sp_port)) { |
| 2155 | dev_warn_ratelimited(mlxsw_sp->bus_info->dev, "Port %d: skb received for non-existent port\n", |
| 2156 | local_port); |
| 2157 | return; |
| 2158 | } |
| 2159 | |
| 2160 | skb->dev = mlxsw_sp_port->dev; |
| 2161 | |
| 2162 | pcpu_stats = this_cpu_ptr(mlxsw_sp_port->pcpu_stats); |
| 2163 | u64_stats_update_begin(&pcpu_stats->syncp); |
| 2164 | pcpu_stats->rx_packets++; |
| 2165 | pcpu_stats->rx_bytes += skb->len; |
| 2166 | u64_stats_update_end(&pcpu_stats->syncp); |
| 2167 | |
| 2168 | skb->protocol = eth_type_trans(skb, skb->dev); |
| 2169 | netif_receive_skb(skb); |
| 2170 | } |
| 2171 | |
| 2172 | static const struct mlxsw_rx_listener mlxsw_sp_rx_listener[] = { |
| 2173 | { |
| 2174 | .func = mlxsw_sp_rx_listener_func, |
| 2175 | .local_port = MLXSW_PORT_DONT_CARE, |
| 2176 | .trap_id = MLXSW_TRAP_ID_FDB_MC, |
| 2177 | }, |
| 2178 | /* Traps for specific L2 packet types, not trapped as FDB MC */ |
| 2179 | { |
| 2180 | .func = mlxsw_sp_rx_listener_func, |
| 2181 | .local_port = MLXSW_PORT_DONT_CARE, |
| 2182 | .trap_id = MLXSW_TRAP_ID_STP, |
| 2183 | }, |
| 2184 | { |
| 2185 | .func = mlxsw_sp_rx_listener_func, |
| 2186 | .local_port = MLXSW_PORT_DONT_CARE, |
| 2187 | .trap_id = MLXSW_TRAP_ID_LACP, |
| 2188 | }, |
| 2189 | { |
| 2190 | .func = mlxsw_sp_rx_listener_func, |
| 2191 | .local_port = MLXSW_PORT_DONT_CARE, |
| 2192 | .trap_id = MLXSW_TRAP_ID_EAPOL, |
| 2193 | }, |
| 2194 | { |
| 2195 | .func = mlxsw_sp_rx_listener_func, |
| 2196 | .local_port = MLXSW_PORT_DONT_CARE, |
| 2197 | .trap_id = MLXSW_TRAP_ID_LLDP, |
| 2198 | }, |
| 2199 | { |
| 2200 | .func = mlxsw_sp_rx_listener_func, |
| 2201 | .local_port = MLXSW_PORT_DONT_CARE, |
| 2202 | .trap_id = MLXSW_TRAP_ID_MMRP, |
| 2203 | }, |
| 2204 | { |
| 2205 | .func = mlxsw_sp_rx_listener_func, |
| 2206 | .local_port = MLXSW_PORT_DONT_CARE, |
| 2207 | .trap_id = MLXSW_TRAP_ID_MVRP, |
| 2208 | }, |
| 2209 | { |
| 2210 | .func = mlxsw_sp_rx_listener_func, |
| 2211 | .local_port = MLXSW_PORT_DONT_CARE, |
| 2212 | .trap_id = MLXSW_TRAP_ID_RPVST, |
| 2213 | }, |
| 2214 | { |
| 2215 | .func = mlxsw_sp_rx_listener_func, |
| 2216 | .local_port = MLXSW_PORT_DONT_CARE, |
| 2217 | .trap_id = MLXSW_TRAP_ID_DHCP, |
| 2218 | }, |
| 2219 | { |
| 2220 | .func = mlxsw_sp_rx_listener_func, |
| 2221 | .local_port = MLXSW_PORT_DONT_CARE, |
| 2222 | .trap_id = MLXSW_TRAP_ID_IGMP_QUERY, |
| 2223 | }, |
| 2224 | { |
| 2225 | .func = mlxsw_sp_rx_listener_func, |
| 2226 | .local_port = MLXSW_PORT_DONT_CARE, |
| 2227 | .trap_id = MLXSW_TRAP_ID_IGMP_V1_REPORT, |
| 2228 | }, |
| 2229 | { |
| 2230 | .func = mlxsw_sp_rx_listener_func, |
| 2231 | .local_port = MLXSW_PORT_DONT_CARE, |
| 2232 | .trap_id = MLXSW_TRAP_ID_IGMP_V2_REPORT, |
| 2233 | }, |
| 2234 | { |
| 2235 | .func = mlxsw_sp_rx_listener_func, |
| 2236 | .local_port = MLXSW_PORT_DONT_CARE, |
| 2237 | .trap_id = MLXSW_TRAP_ID_IGMP_V2_LEAVE, |
| 2238 | }, |
| 2239 | { |
| 2240 | .func = mlxsw_sp_rx_listener_func, |
| 2241 | .local_port = MLXSW_PORT_DONT_CARE, |
| 2242 | .trap_id = MLXSW_TRAP_ID_IGMP_V3_REPORT, |
| 2243 | }, |
| 2244 | }; |
| 2245 | |
| 2246 | static int mlxsw_sp_traps_init(struct mlxsw_sp *mlxsw_sp) |
| 2247 | { |
| 2248 | char htgt_pl[MLXSW_REG_HTGT_LEN]; |
| 2249 | char hpkt_pl[MLXSW_REG_HPKT_LEN]; |
| 2250 | int i; |
| 2251 | int err; |
| 2252 | |
| 2253 | mlxsw_reg_htgt_pack(htgt_pl, MLXSW_REG_HTGT_TRAP_GROUP_RX); |
| 2254 | err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(htgt), htgt_pl); |
| 2255 | if (err) |
| 2256 | return err; |
| 2257 | |
| 2258 | mlxsw_reg_htgt_pack(htgt_pl, MLXSW_REG_HTGT_TRAP_GROUP_CTRL); |
| 2259 | err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(htgt), htgt_pl); |
| 2260 | if (err) |
| 2261 | return err; |
| 2262 | |
| 2263 | for (i = 0; i < ARRAY_SIZE(mlxsw_sp_rx_listener); i++) { |
| 2264 | err = mlxsw_core_rx_listener_register(mlxsw_sp->core, |
| 2265 | &mlxsw_sp_rx_listener[i], |
| 2266 | mlxsw_sp); |
| 2267 | if (err) |
| 2268 | goto err_rx_listener_register; |
| 2269 | |
| 2270 | mlxsw_reg_hpkt_pack(hpkt_pl, MLXSW_REG_HPKT_ACTION_TRAP_TO_CPU, |
| 2271 | mlxsw_sp_rx_listener[i].trap_id); |
| 2272 | err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(hpkt), hpkt_pl); |
| 2273 | if (err) |
| 2274 | goto err_rx_trap_set; |
| 2275 | } |
| 2276 | return 0; |
| 2277 | |
| 2278 | err_rx_trap_set: |
| 2279 | mlxsw_core_rx_listener_unregister(mlxsw_sp->core, |
| 2280 | &mlxsw_sp_rx_listener[i], |
| 2281 | mlxsw_sp); |
| 2282 | err_rx_listener_register: |
| 2283 | for (i--; i >= 0; i--) { |
| 2284 | mlxsw_reg_hpkt_pack(hpkt_pl, MLXSW_REG_HPKT_ACTION_FORWARD, |
| 2285 | mlxsw_sp_rx_listener[i].trap_id); |
| 2286 | mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(hpkt), hpkt_pl); |
| 2287 | |
| 2288 | mlxsw_core_rx_listener_unregister(mlxsw_sp->core, |
| 2289 | &mlxsw_sp_rx_listener[i], |
| 2290 | mlxsw_sp); |
| 2291 | } |
| 2292 | return err; |
| 2293 | } |
| 2294 | |
| 2295 | static void mlxsw_sp_traps_fini(struct mlxsw_sp *mlxsw_sp) |
| 2296 | { |
| 2297 | char hpkt_pl[MLXSW_REG_HPKT_LEN]; |
| 2298 | int i; |
| 2299 | |
| 2300 | for (i = 0; i < ARRAY_SIZE(mlxsw_sp_rx_listener); i++) { |
| 2301 | mlxsw_reg_hpkt_pack(hpkt_pl, MLXSW_REG_HPKT_ACTION_FORWARD, |
| 2302 | mlxsw_sp_rx_listener[i].trap_id); |
| 2303 | mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(hpkt), hpkt_pl); |
| 2304 | |
| 2305 | mlxsw_core_rx_listener_unregister(mlxsw_sp->core, |
| 2306 | &mlxsw_sp_rx_listener[i], |
| 2307 | mlxsw_sp); |
| 2308 | } |
| 2309 | } |
| 2310 | |
| 2311 | static int __mlxsw_sp_flood_init(struct mlxsw_core *mlxsw_core, |
| 2312 | enum mlxsw_reg_sfgc_type type, |
| 2313 | enum mlxsw_reg_sfgc_bridge_type bridge_type) |
| 2314 | { |
| 2315 | enum mlxsw_flood_table_type table_type; |
| 2316 | enum mlxsw_sp_flood_table flood_table; |
| 2317 | char sfgc_pl[MLXSW_REG_SFGC_LEN]; |
| 2318 | |
Ido Schimmel | 19ae612 | 2015-12-15 16:03:39 +0100 | [diff] [blame] | 2319 | if (bridge_type == MLXSW_REG_SFGC_BRIDGE_TYPE_VFID) |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 2320 | table_type = MLXSW_REG_SFGC_TABLE_TYPE_FID; |
Ido Schimmel | 19ae612 | 2015-12-15 16:03:39 +0100 | [diff] [blame] | 2321 | else |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 2322 | table_type = MLXSW_REG_SFGC_TABLE_TYPE_FID_OFFEST; |
Ido Schimmel | 19ae612 | 2015-12-15 16:03:39 +0100 | [diff] [blame] | 2323 | |
| 2324 | if (type == MLXSW_REG_SFGC_TYPE_UNKNOWN_UNICAST) |
| 2325 | flood_table = MLXSW_SP_FLOOD_TABLE_UC; |
| 2326 | else |
| 2327 | flood_table = MLXSW_SP_FLOOD_TABLE_BM; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 2328 | |
| 2329 | mlxsw_reg_sfgc_pack(sfgc_pl, type, bridge_type, table_type, |
| 2330 | flood_table); |
| 2331 | return mlxsw_reg_write(mlxsw_core, MLXSW_REG(sfgc), sfgc_pl); |
| 2332 | } |
| 2333 | |
| 2334 | static int mlxsw_sp_flood_init(struct mlxsw_sp *mlxsw_sp) |
| 2335 | { |
| 2336 | int type, err; |
| 2337 | |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 2338 | for (type = 0; type < MLXSW_REG_SFGC_TYPE_MAX; type++) { |
| 2339 | if (type == MLXSW_REG_SFGC_TYPE_RESERVED) |
| 2340 | continue; |
| 2341 | |
| 2342 | err = __mlxsw_sp_flood_init(mlxsw_sp->core, type, |
| 2343 | MLXSW_REG_SFGC_BRIDGE_TYPE_VFID); |
| 2344 | if (err) |
| 2345 | return err; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 2346 | |
| 2347 | err = __mlxsw_sp_flood_init(mlxsw_sp->core, type, |
| 2348 | MLXSW_REG_SFGC_BRIDGE_TYPE_1Q_FID); |
| 2349 | if (err) |
| 2350 | return err; |
| 2351 | } |
| 2352 | |
| 2353 | return 0; |
| 2354 | } |
| 2355 | |
Jiri Pirko | 0d65fc1 | 2015-12-03 12:12:28 +0100 | [diff] [blame] | 2356 | static int mlxsw_sp_lag_init(struct mlxsw_sp *mlxsw_sp) |
| 2357 | { |
| 2358 | char slcr_pl[MLXSW_REG_SLCR_LEN]; |
| 2359 | |
| 2360 | mlxsw_reg_slcr_pack(slcr_pl, MLXSW_REG_SLCR_LAG_HASH_SMAC | |
| 2361 | MLXSW_REG_SLCR_LAG_HASH_DMAC | |
| 2362 | MLXSW_REG_SLCR_LAG_HASH_ETHERTYPE | |
| 2363 | MLXSW_REG_SLCR_LAG_HASH_VLANID | |
| 2364 | MLXSW_REG_SLCR_LAG_HASH_SIP | |
| 2365 | MLXSW_REG_SLCR_LAG_HASH_DIP | |
| 2366 | MLXSW_REG_SLCR_LAG_HASH_SPORT | |
| 2367 | MLXSW_REG_SLCR_LAG_HASH_DPORT | |
| 2368 | MLXSW_REG_SLCR_LAG_HASH_IPPROTO); |
| 2369 | return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(slcr), slcr_pl); |
| 2370 | } |
| 2371 | |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 2372 | static int mlxsw_sp_init(void *priv, struct mlxsw_core *mlxsw_core, |
| 2373 | const struct mlxsw_bus_info *mlxsw_bus_info) |
| 2374 | { |
| 2375 | struct mlxsw_sp *mlxsw_sp = priv; |
| 2376 | int err; |
| 2377 | |
| 2378 | mlxsw_sp->core = mlxsw_core; |
| 2379 | mlxsw_sp->bus_info = mlxsw_bus_info; |
Ido Schimmel | 7f71eb4 | 2015-12-15 16:03:37 +0100 | [diff] [blame] | 2380 | INIT_LIST_HEAD(&mlxsw_sp->port_vfids.list); |
Ido Schimmel | 26f0e7f | 2015-12-15 16:03:44 +0100 | [diff] [blame] | 2381 | INIT_LIST_HEAD(&mlxsw_sp->br_vfids.list); |
Elad Raz | 3a49b4f | 2016-01-10 21:06:28 +0100 | [diff] [blame] | 2382 | INIT_LIST_HEAD(&mlxsw_sp->br_mids.list); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 2383 | |
| 2384 | err = mlxsw_sp_base_mac_get(mlxsw_sp); |
| 2385 | if (err) { |
| 2386 | dev_err(mlxsw_sp->bus_info->dev, "Failed to get base mac\n"); |
| 2387 | return err; |
| 2388 | } |
| 2389 | |
| 2390 | err = mlxsw_sp_ports_create(mlxsw_sp); |
| 2391 | if (err) { |
| 2392 | dev_err(mlxsw_sp->bus_info->dev, "Failed to create ports\n"); |
Ido Schimmel | 7f71eb4 | 2015-12-15 16:03:37 +0100 | [diff] [blame] | 2393 | return err; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 2394 | } |
| 2395 | |
| 2396 | err = mlxsw_sp_event_register(mlxsw_sp, MLXSW_TRAP_ID_PUDE); |
| 2397 | if (err) { |
| 2398 | dev_err(mlxsw_sp->bus_info->dev, "Failed to register for PUDE events\n"); |
| 2399 | goto err_event_register; |
| 2400 | } |
| 2401 | |
| 2402 | err = mlxsw_sp_traps_init(mlxsw_sp); |
| 2403 | if (err) { |
| 2404 | dev_err(mlxsw_sp->bus_info->dev, "Failed to set traps for RX\n"); |
| 2405 | goto err_rx_listener_register; |
| 2406 | } |
| 2407 | |
| 2408 | err = mlxsw_sp_flood_init(mlxsw_sp); |
| 2409 | if (err) { |
| 2410 | dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize flood tables\n"); |
| 2411 | goto err_flood_init; |
| 2412 | } |
| 2413 | |
| 2414 | err = mlxsw_sp_buffers_init(mlxsw_sp); |
| 2415 | if (err) { |
| 2416 | dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize buffers\n"); |
| 2417 | goto err_buffers_init; |
| 2418 | } |
| 2419 | |
Jiri Pirko | 0d65fc1 | 2015-12-03 12:12:28 +0100 | [diff] [blame] | 2420 | err = mlxsw_sp_lag_init(mlxsw_sp); |
| 2421 | if (err) { |
| 2422 | dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize LAG\n"); |
| 2423 | goto err_lag_init; |
| 2424 | } |
| 2425 | |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 2426 | err = mlxsw_sp_switchdev_init(mlxsw_sp); |
| 2427 | if (err) { |
| 2428 | dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize switchdev\n"); |
| 2429 | goto err_switchdev_init; |
| 2430 | } |
| 2431 | |
| 2432 | return 0; |
| 2433 | |
| 2434 | err_switchdev_init: |
Jiri Pirko | 0d65fc1 | 2015-12-03 12:12:28 +0100 | [diff] [blame] | 2435 | err_lag_init: |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 2436 | err_buffers_init: |
| 2437 | err_flood_init: |
| 2438 | mlxsw_sp_traps_fini(mlxsw_sp); |
| 2439 | err_rx_listener_register: |
| 2440 | mlxsw_sp_event_unregister(mlxsw_sp, MLXSW_TRAP_ID_PUDE); |
| 2441 | err_event_register: |
| 2442 | mlxsw_sp_ports_remove(mlxsw_sp); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 2443 | return err; |
| 2444 | } |
| 2445 | |
| 2446 | static void mlxsw_sp_fini(void *priv) |
| 2447 | { |
| 2448 | struct mlxsw_sp *mlxsw_sp = priv; |
| 2449 | |
| 2450 | mlxsw_sp_switchdev_fini(mlxsw_sp); |
| 2451 | mlxsw_sp_traps_fini(mlxsw_sp); |
| 2452 | mlxsw_sp_event_unregister(mlxsw_sp, MLXSW_TRAP_ID_PUDE); |
| 2453 | mlxsw_sp_ports_remove(mlxsw_sp); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 2454 | } |
| 2455 | |
| 2456 | static struct mlxsw_config_profile mlxsw_sp_config_profile = { |
| 2457 | .used_max_vepa_channels = 1, |
| 2458 | .max_vepa_channels = 0, |
| 2459 | .used_max_lag = 1, |
Jiri Pirko | 0d65fc1 | 2015-12-03 12:12:28 +0100 | [diff] [blame] | 2460 | .max_lag = MLXSW_SP_LAG_MAX, |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 2461 | .used_max_port_per_lag = 1, |
Jiri Pirko | 0d65fc1 | 2015-12-03 12:12:28 +0100 | [diff] [blame] | 2462 | .max_port_per_lag = MLXSW_SP_PORT_PER_LAG_MAX, |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 2463 | .used_max_mid = 1, |
Elad Raz | 53ae628 | 2016-01-10 21:06:26 +0100 | [diff] [blame] | 2464 | .max_mid = MLXSW_SP_MID_MAX, |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 2465 | .used_max_pgt = 1, |
| 2466 | .max_pgt = 0, |
| 2467 | .used_max_system_port = 1, |
| 2468 | .max_system_port = 64, |
| 2469 | .used_max_vlan_groups = 1, |
| 2470 | .max_vlan_groups = 127, |
| 2471 | .used_max_regions = 1, |
| 2472 | .max_regions = 400, |
| 2473 | .used_flood_tables = 1, |
| 2474 | .used_flood_mode = 1, |
| 2475 | .flood_mode = 3, |
| 2476 | .max_fid_offset_flood_tables = 2, |
| 2477 | .fid_offset_flood_table_size = VLAN_N_VID - 1, |
Ido Schimmel | 19ae612 | 2015-12-15 16:03:39 +0100 | [diff] [blame] | 2478 | .max_fid_flood_tables = 2, |
| 2479 | .fid_flood_table_size = MLXSW_SP_VFID_MAX, |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 2480 | .used_max_ib_mc = 1, |
| 2481 | .max_ib_mc = 0, |
| 2482 | .used_max_pkey = 1, |
| 2483 | .max_pkey = 0, |
| 2484 | .swid_config = { |
| 2485 | { |
| 2486 | .used_type = 1, |
| 2487 | .type = MLXSW_PORT_SWID_TYPE_ETH, |
| 2488 | } |
| 2489 | }, |
| 2490 | }; |
| 2491 | |
| 2492 | static struct mlxsw_driver mlxsw_sp_driver = { |
| 2493 | .kind = MLXSW_DEVICE_KIND_SPECTRUM, |
| 2494 | .owner = THIS_MODULE, |
| 2495 | .priv_size = sizeof(struct mlxsw_sp), |
| 2496 | .init = mlxsw_sp_init, |
| 2497 | .fini = mlxsw_sp_fini, |
Ido Schimmel | 18f1e70 | 2016-02-26 17:32:31 +0100 | [diff] [blame] | 2498 | .port_split = mlxsw_sp_port_split, |
| 2499 | .port_unsplit = mlxsw_sp_port_unsplit, |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 2500 | .txhdr_construct = mlxsw_sp_txhdr_construct, |
| 2501 | .txhdr_len = MLXSW_TXHDR_LEN, |
| 2502 | .profile = &mlxsw_sp_config_profile, |
| 2503 | }; |
| 2504 | |
Ido Schimmel | 039c49a | 2016-01-27 15:20:18 +0100 | [diff] [blame] | 2505 | static int |
| 2506 | mlxsw_sp_port_fdb_flush_by_port(const struct mlxsw_sp_port *mlxsw_sp_port) |
| 2507 | { |
| 2508 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
| 2509 | char sfdf_pl[MLXSW_REG_SFDF_LEN]; |
| 2510 | |
| 2511 | mlxsw_reg_sfdf_pack(sfdf_pl, MLXSW_REG_SFDF_FLUSH_PER_PORT); |
| 2512 | mlxsw_reg_sfdf_system_port_set(sfdf_pl, mlxsw_sp_port->local_port); |
| 2513 | |
| 2514 | return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfdf), sfdf_pl); |
| 2515 | } |
| 2516 | |
| 2517 | static int |
| 2518 | mlxsw_sp_port_fdb_flush_by_port_fid(const struct mlxsw_sp_port *mlxsw_sp_port, |
| 2519 | u16 fid) |
| 2520 | { |
| 2521 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
| 2522 | char sfdf_pl[MLXSW_REG_SFDF_LEN]; |
| 2523 | |
| 2524 | mlxsw_reg_sfdf_pack(sfdf_pl, MLXSW_REG_SFDF_FLUSH_PER_PORT_AND_FID); |
| 2525 | mlxsw_reg_sfdf_fid_set(sfdf_pl, fid); |
| 2526 | mlxsw_reg_sfdf_port_fid_system_port_set(sfdf_pl, |
| 2527 | mlxsw_sp_port->local_port); |
| 2528 | |
| 2529 | return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfdf), sfdf_pl); |
| 2530 | } |
| 2531 | |
| 2532 | static int |
| 2533 | mlxsw_sp_port_fdb_flush_by_lag_id(const struct mlxsw_sp_port *mlxsw_sp_port) |
| 2534 | { |
| 2535 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
| 2536 | char sfdf_pl[MLXSW_REG_SFDF_LEN]; |
| 2537 | |
| 2538 | mlxsw_reg_sfdf_pack(sfdf_pl, MLXSW_REG_SFDF_FLUSH_PER_LAG); |
| 2539 | mlxsw_reg_sfdf_lag_id_set(sfdf_pl, mlxsw_sp_port->lag_id); |
| 2540 | |
| 2541 | return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfdf), sfdf_pl); |
| 2542 | } |
| 2543 | |
| 2544 | static int |
| 2545 | mlxsw_sp_port_fdb_flush_by_lag_id_fid(const struct mlxsw_sp_port *mlxsw_sp_port, |
| 2546 | u16 fid) |
| 2547 | { |
| 2548 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
| 2549 | char sfdf_pl[MLXSW_REG_SFDF_LEN]; |
| 2550 | |
| 2551 | mlxsw_reg_sfdf_pack(sfdf_pl, MLXSW_REG_SFDF_FLUSH_PER_LAG_AND_FID); |
| 2552 | mlxsw_reg_sfdf_fid_set(sfdf_pl, fid); |
| 2553 | mlxsw_reg_sfdf_lag_fid_lag_id_set(sfdf_pl, mlxsw_sp_port->lag_id); |
| 2554 | |
| 2555 | return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfdf), sfdf_pl); |
| 2556 | } |
| 2557 | |
| 2558 | static int |
| 2559 | __mlxsw_sp_port_fdb_flush(const struct mlxsw_sp_port *mlxsw_sp_port) |
| 2560 | { |
| 2561 | int err, last_err = 0; |
| 2562 | u16 vid; |
| 2563 | |
| 2564 | for (vid = 1; vid < VLAN_N_VID - 1; vid++) { |
| 2565 | err = mlxsw_sp_port_fdb_flush_by_port_fid(mlxsw_sp_port, vid); |
| 2566 | if (err) |
| 2567 | last_err = err; |
| 2568 | } |
| 2569 | |
| 2570 | return last_err; |
| 2571 | } |
| 2572 | |
| 2573 | static int |
| 2574 | __mlxsw_sp_port_fdb_flush_lagged(const struct mlxsw_sp_port *mlxsw_sp_port) |
| 2575 | { |
| 2576 | int err, last_err = 0; |
| 2577 | u16 vid; |
| 2578 | |
| 2579 | for (vid = 1; vid < VLAN_N_VID - 1; vid++) { |
| 2580 | err = mlxsw_sp_port_fdb_flush_by_lag_id_fid(mlxsw_sp_port, vid); |
| 2581 | if (err) |
| 2582 | last_err = err; |
| 2583 | } |
| 2584 | |
| 2585 | return last_err; |
| 2586 | } |
| 2587 | |
| 2588 | static int mlxsw_sp_port_fdb_flush(struct mlxsw_sp_port *mlxsw_sp_port) |
| 2589 | { |
| 2590 | if (!list_empty(&mlxsw_sp_port->vports_list)) |
| 2591 | if (mlxsw_sp_port->lagged) |
| 2592 | return __mlxsw_sp_port_fdb_flush_lagged(mlxsw_sp_port); |
| 2593 | else |
| 2594 | return __mlxsw_sp_port_fdb_flush(mlxsw_sp_port); |
| 2595 | else |
| 2596 | if (mlxsw_sp_port->lagged) |
| 2597 | return mlxsw_sp_port_fdb_flush_by_lag_id(mlxsw_sp_port); |
| 2598 | else |
| 2599 | return mlxsw_sp_port_fdb_flush_by_port(mlxsw_sp_port); |
| 2600 | } |
| 2601 | |
| 2602 | static int mlxsw_sp_vport_fdb_flush(struct mlxsw_sp_port *mlxsw_sp_vport) |
| 2603 | { |
| 2604 | u16 vfid = mlxsw_sp_vport_vfid_get(mlxsw_sp_vport); |
| 2605 | u16 fid = mlxsw_sp_vfid_to_fid(vfid); |
| 2606 | |
| 2607 | if (mlxsw_sp_vport->lagged) |
| 2608 | return mlxsw_sp_port_fdb_flush_by_lag_id_fid(mlxsw_sp_vport, |
| 2609 | fid); |
| 2610 | else |
| 2611 | return mlxsw_sp_port_fdb_flush_by_port_fid(mlxsw_sp_vport, fid); |
| 2612 | } |
| 2613 | |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 2614 | static bool mlxsw_sp_port_dev_check(const struct net_device *dev) |
| 2615 | { |
| 2616 | return dev->netdev_ops == &mlxsw_sp_port_netdev_ops; |
| 2617 | } |
| 2618 | |
| 2619 | static int mlxsw_sp_port_bridge_join(struct mlxsw_sp_port *mlxsw_sp_port) |
| 2620 | { |
| 2621 | struct net_device *dev = mlxsw_sp_port->dev; |
| 2622 | int err; |
| 2623 | |
| 2624 | /* When port is not bridged untagged packets are tagged with |
| 2625 | * PVID=VID=1, thereby creating an implicit VLAN interface in |
| 2626 | * the device. Remove it and let bridge code take care of its |
| 2627 | * own VLANs. |
| 2628 | */ |
| 2629 | err = mlxsw_sp_port_kill_vid(dev, 0, 1); |
Ido Schimmel | 6c72a3d | 2016-01-04 10:42:26 +0100 | [diff] [blame] | 2630 | if (err) |
| 2631 | return err; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 2632 | |
Ido Schimmel | 6c72a3d | 2016-01-04 10:42:26 +0100 | [diff] [blame] | 2633 | mlxsw_sp_port->learning = 1; |
| 2634 | mlxsw_sp_port->learning_sync = 1; |
| 2635 | mlxsw_sp_port->uc_flood = 1; |
| 2636 | mlxsw_sp_port->bridged = 1; |
| 2637 | |
| 2638 | return 0; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 2639 | } |
| 2640 | |
Ido Schimmel | 039c49a | 2016-01-27 15:20:18 +0100 | [diff] [blame] | 2641 | static int mlxsw_sp_port_bridge_leave(struct mlxsw_sp_port *mlxsw_sp_port, |
| 2642 | bool flush_fdb) |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 2643 | { |
| 2644 | struct net_device *dev = mlxsw_sp_port->dev; |
Ido Schimmel | 5a8f452 | 2016-01-04 10:42:25 +0100 | [diff] [blame] | 2645 | |
Ido Schimmel | 039c49a | 2016-01-27 15:20:18 +0100 | [diff] [blame] | 2646 | if (flush_fdb && mlxsw_sp_port_fdb_flush(mlxsw_sp_port)) |
| 2647 | netdev_err(mlxsw_sp_port->dev, "Failed to flush FDB\n"); |
| 2648 | |
Ido Schimmel | 28a01d2 | 2016-02-18 11:30:02 +0100 | [diff] [blame] | 2649 | mlxsw_sp_port_pvid_set(mlxsw_sp_port, 1); |
| 2650 | |
Ido Schimmel | 6c72a3d | 2016-01-04 10:42:26 +0100 | [diff] [blame] | 2651 | mlxsw_sp_port->learning = 0; |
| 2652 | mlxsw_sp_port->learning_sync = 0; |
| 2653 | mlxsw_sp_port->uc_flood = 0; |
Ido Schimmel | 5a8f452 | 2016-01-04 10:42:25 +0100 | [diff] [blame] | 2654 | mlxsw_sp_port->bridged = 0; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 2655 | |
| 2656 | /* Add implicit VLAN interface in the device, so that untagged |
| 2657 | * packets will be classified to the default vFID. |
| 2658 | */ |
Ido Schimmel | 5a8f452 | 2016-01-04 10:42:25 +0100 | [diff] [blame] | 2659 | return mlxsw_sp_port_add_vid(dev, 0, 1); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 2660 | } |
| 2661 | |
| 2662 | static bool mlxsw_sp_master_bridge_check(struct mlxsw_sp *mlxsw_sp, |
| 2663 | struct net_device *br_dev) |
| 2664 | { |
| 2665 | return !mlxsw_sp->master_bridge.dev || |
| 2666 | mlxsw_sp->master_bridge.dev == br_dev; |
| 2667 | } |
| 2668 | |
| 2669 | static void mlxsw_sp_master_bridge_inc(struct mlxsw_sp *mlxsw_sp, |
| 2670 | struct net_device *br_dev) |
| 2671 | { |
| 2672 | mlxsw_sp->master_bridge.dev = br_dev; |
| 2673 | mlxsw_sp->master_bridge.ref_count++; |
| 2674 | } |
| 2675 | |
| 2676 | static void mlxsw_sp_master_bridge_dec(struct mlxsw_sp *mlxsw_sp, |
| 2677 | struct net_device *br_dev) |
| 2678 | { |
| 2679 | if (--mlxsw_sp->master_bridge.ref_count == 0) |
| 2680 | mlxsw_sp->master_bridge.dev = NULL; |
| 2681 | } |
| 2682 | |
Jiri Pirko | 0d65fc1 | 2015-12-03 12:12:28 +0100 | [diff] [blame] | 2683 | static int mlxsw_sp_lag_create(struct mlxsw_sp *mlxsw_sp, u16 lag_id) |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 2684 | { |
Jiri Pirko | 0d65fc1 | 2015-12-03 12:12:28 +0100 | [diff] [blame] | 2685 | char sldr_pl[MLXSW_REG_SLDR_LEN]; |
| 2686 | |
| 2687 | mlxsw_reg_sldr_lag_create_pack(sldr_pl, lag_id); |
| 2688 | return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sldr), sldr_pl); |
| 2689 | } |
| 2690 | |
| 2691 | static int mlxsw_sp_lag_destroy(struct mlxsw_sp *mlxsw_sp, u16 lag_id) |
| 2692 | { |
| 2693 | char sldr_pl[MLXSW_REG_SLDR_LEN]; |
| 2694 | |
| 2695 | mlxsw_reg_sldr_lag_destroy_pack(sldr_pl, lag_id); |
| 2696 | return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sldr), sldr_pl); |
| 2697 | } |
| 2698 | |
| 2699 | static int mlxsw_sp_lag_col_port_add(struct mlxsw_sp_port *mlxsw_sp_port, |
| 2700 | u16 lag_id, u8 port_index) |
| 2701 | { |
| 2702 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
| 2703 | char slcor_pl[MLXSW_REG_SLCOR_LEN]; |
| 2704 | |
| 2705 | mlxsw_reg_slcor_port_add_pack(slcor_pl, mlxsw_sp_port->local_port, |
| 2706 | lag_id, port_index); |
| 2707 | return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(slcor), slcor_pl); |
| 2708 | } |
| 2709 | |
| 2710 | static int mlxsw_sp_lag_col_port_remove(struct mlxsw_sp_port *mlxsw_sp_port, |
| 2711 | u16 lag_id) |
| 2712 | { |
| 2713 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
| 2714 | char slcor_pl[MLXSW_REG_SLCOR_LEN]; |
| 2715 | |
| 2716 | mlxsw_reg_slcor_port_remove_pack(slcor_pl, mlxsw_sp_port->local_port, |
| 2717 | lag_id); |
| 2718 | return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(slcor), slcor_pl); |
| 2719 | } |
| 2720 | |
| 2721 | static int mlxsw_sp_lag_col_port_enable(struct mlxsw_sp_port *mlxsw_sp_port, |
| 2722 | u16 lag_id) |
| 2723 | { |
| 2724 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
| 2725 | char slcor_pl[MLXSW_REG_SLCOR_LEN]; |
| 2726 | |
| 2727 | mlxsw_reg_slcor_col_enable_pack(slcor_pl, mlxsw_sp_port->local_port, |
| 2728 | lag_id); |
| 2729 | return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(slcor), slcor_pl); |
| 2730 | } |
| 2731 | |
| 2732 | static int mlxsw_sp_lag_col_port_disable(struct mlxsw_sp_port *mlxsw_sp_port, |
| 2733 | u16 lag_id) |
| 2734 | { |
| 2735 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
| 2736 | char slcor_pl[MLXSW_REG_SLCOR_LEN]; |
| 2737 | |
| 2738 | mlxsw_reg_slcor_col_disable_pack(slcor_pl, mlxsw_sp_port->local_port, |
| 2739 | lag_id); |
| 2740 | return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(slcor), slcor_pl); |
| 2741 | } |
| 2742 | |
| 2743 | static int mlxsw_sp_lag_index_get(struct mlxsw_sp *mlxsw_sp, |
| 2744 | struct net_device *lag_dev, |
| 2745 | u16 *p_lag_id) |
| 2746 | { |
| 2747 | struct mlxsw_sp_upper *lag; |
| 2748 | int free_lag_id = -1; |
| 2749 | int i; |
| 2750 | |
| 2751 | for (i = 0; i < MLXSW_SP_LAG_MAX; i++) { |
| 2752 | lag = mlxsw_sp_lag_get(mlxsw_sp, i); |
| 2753 | if (lag->ref_count) { |
| 2754 | if (lag->dev == lag_dev) { |
| 2755 | *p_lag_id = i; |
| 2756 | return 0; |
| 2757 | } |
| 2758 | } else if (free_lag_id < 0) { |
| 2759 | free_lag_id = i; |
| 2760 | } |
| 2761 | } |
| 2762 | if (free_lag_id < 0) |
| 2763 | return -EBUSY; |
| 2764 | *p_lag_id = free_lag_id; |
| 2765 | return 0; |
| 2766 | } |
| 2767 | |
| 2768 | static bool |
| 2769 | mlxsw_sp_master_lag_check(struct mlxsw_sp *mlxsw_sp, |
| 2770 | struct net_device *lag_dev, |
| 2771 | struct netdev_lag_upper_info *lag_upper_info) |
| 2772 | { |
| 2773 | u16 lag_id; |
| 2774 | |
| 2775 | if (mlxsw_sp_lag_index_get(mlxsw_sp, lag_dev, &lag_id) != 0) |
| 2776 | return false; |
| 2777 | if (lag_upper_info->tx_type != NETDEV_LAG_TX_TYPE_HASH) |
| 2778 | return false; |
| 2779 | return true; |
| 2780 | } |
| 2781 | |
| 2782 | static int mlxsw_sp_port_lag_index_get(struct mlxsw_sp *mlxsw_sp, |
| 2783 | u16 lag_id, u8 *p_port_index) |
| 2784 | { |
| 2785 | int i; |
| 2786 | |
| 2787 | for (i = 0; i < MLXSW_SP_PORT_PER_LAG_MAX; i++) { |
| 2788 | if (!mlxsw_sp_port_lagged_get(mlxsw_sp, lag_id, i)) { |
| 2789 | *p_port_index = i; |
| 2790 | return 0; |
| 2791 | } |
| 2792 | } |
| 2793 | return -EBUSY; |
| 2794 | } |
| 2795 | |
| 2796 | static int mlxsw_sp_port_lag_join(struct mlxsw_sp_port *mlxsw_sp_port, |
| 2797 | struct net_device *lag_dev) |
| 2798 | { |
| 2799 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
| 2800 | struct mlxsw_sp_upper *lag; |
| 2801 | u16 lag_id; |
| 2802 | u8 port_index; |
| 2803 | int err; |
| 2804 | |
| 2805 | err = mlxsw_sp_lag_index_get(mlxsw_sp, lag_dev, &lag_id); |
| 2806 | if (err) |
| 2807 | return err; |
| 2808 | lag = mlxsw_sp_lag_get(mlxsw_sp, lag_id); |
| 2809 | if (!lag->ref_count) { |
| 2810 | err = mlxsw_sp_lag_create(mlxsw_sp, lag_id); |
| 2811 | if (err) |
| 2812 | return err; |
| 2813 | lag->dev = lag_dev; |
| 2814 | } |
| 2815 | |
| 2816 | err = mlxsw_sp_port_lag_index_get(mlxsw_sp, lag_id, &port_index); |
| 2817 | if (err) |
| 2818 | return err; |
| 2819 | err = mlxsw_sp_lag_col_port_add(mlxsw_sp_port, lag_id, port_index); |
| 2820 | if (err) |
| 2821 | goto err_col_port_add; |
| 2822 | err = mlxsw_sp_lag_col_port_enable(mlxsw_sp_port, lag_id); |
| 2823 | if (err) |
| 2824 | goto err_col_port_enable; |
| 2825 | |
| 2826 | mlxsw_core_lag_mapping_set(mlxsw_sp->core, lag_id, port_index, |
| 2827 | mlxsw_sp_port->local_port); |
| 2828 | mlxsw_sp_port->lag_id = lag_id; |
| 2829 | mlxsw_sp_port->lagged = 1; |
| 2830 | lag->ref_count++; |
| 2831 | return 0; |
| 2832 | |
| 2833 | err_col_port_add: |
| 2834 | if (!lag->ref_count) |
| 2835 | mlxsw_sp_lag_destroy(mlxsw_sp, lag_id); |
| 2836 | err_col_port_enable: |
| 2837 | mlxsw_sp_lag_col_port_remove(mlxsw_sp_port, lag_id); |
| 2838 | return err; |
| 2839 | } |
| 2840 | |
Ido Schimmel | 4dc236c | 2016-01-27 15:20:16 +0100 | [diff] [blame] | 2841 | static int mlxsw_sp_vport_bridge_leave(struct mlxsw_sp_port *mlxsw_sp_vport, |
Ido Schimmel | 039c49a | 2016-01-27 15:20:18 +0100 | [diff] [blame] | 2842 | struct net_device *br_dev, |
| 2843 | bool flush_fdb); |
Ido Schimmel | 4dc236c | 2016-01-27 15:20:16 +0100 | [diff] [blame] | 2844 | |
Jiri Pirko | 0d65fc1 | 2015-12-03 12:12:28 +0100 | [diff] [blame] | 2845 | static int mlxsw_sp_port_lag_leave(struct mlxsw_sp_port *mlxsw_sp_port, |
| 2846 | struct net_device *lag_dev) |
| 2847 | { |
| 2848 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
Ido Schimmel | 4dc236c | 2016-01-27 15:20:16 +0100 | [diff] [blame] | 2849 | struct mlxsw_sp_port *mlxsw_sp_vport; |
Jiri Pirko | 0d65fc1 | 2015-12-03 12:12:28 +0100 | [diff] [blame] | 2850 | struct mlxsw_sp_upper *lag; |
| 2851 | u16 lag_id = mlxsw_sp_port->lag_id; |
| 2852 | int err; |
| 2853 | |
| 2854 | if (!mlxsw_sp_port->lagged) |
| 2855 | return 0; |
| 2856 | lag = mlxsw_sp_lag_get(mlxsw_sp, lag_id); |
| 2857 | WARN_ON(lag->ref_count == 0); |
| 2858 | |
| 2859 | err = mlxsw_sp_lag_col_port_disable(mlxsw_sp_port, lag_id); |
| 2860 | if (err) |
| 2861 | return err; |
Dan Carpenter | 82a0642 | 2015-12-09 13:33:51 +0300 | [diff] [blame] | 2862 | err = mlxsw_sp_lag_col_port_remove(mlxsw_sp_port, lag_id); |
Jiri Pirko | 0d65fc1 | 2015-12-03 12:12:28 +0100 | [diff] [blame] | 2863 | if (err) |
| 2864 | return err; |
| 2865 | |
Ido Schimmel | 4dc236c | 2016-01-27 15:20:16 +0100 | [diff] [blame] | 2866 | /* In case we leave a LAG device that has bridges built on top, |
| 2867 | * then their teardown sequence is never issued and we need to |
| 2868 | * invoke the necessary cleanup routines ourselves. |
| 2869 | */ |
| 2870 | list_for_each_entry(mlxsw_sp_vport, &mlxsw_sp_port->vports_list, |
| 2871 | vport.list) { |
| 2872 | struct net_device *br_dev; |
| 2873 | |
| 2874 | if (!mlxsw_sp_vport->bridged) |
| 2875 | continue; |
| 2876 | |
| 2877 | br_dev = mlxsw_sp_vport_br_get(mlxsw_sp_vport); |
Ido Schimmel | 039c49a | 2016-01-27 15:20:18 +0100 | [diff] [blame] | 2878 | mlxsw_sp_vport_bridge_leave(mlxsw_sp_vport, br_dev, false); |
Ido Schimmel | 4dc236c | 2016-01-27 15:20:16 +0100 | [diff] [blame] | 2879 | } |
| 2880 | |
| 2881 | if (mlxsw_sp_port->bridged) { |
| 2882 | mlxsw_sp_port_active_vlans_del(mlxsw_sp_port); |
Ido Schimmel | 039c49a | 2016-01-27 15:20:18 +0100 | [diff] [blame] | 2883 | mlxsw_sp_port_bridge_leave(mlxsw_sp_port, false); |
Ido Schimmel | 912b1c8 | 2016-03-07 15:15:29 +0100 | [diff] [blame] | 2884 | mlxsw_sp_master_bridge_dec(mlxsw_sp, NULL); |
Ido Schimmel | 4dc236c | 2016-01-27 15:20:16 +0100 | [diff] [blame] | 2885 | } |
| 2886 | |
Jiri Pirko | 0d65fc1 | 2015-12-03 12:12:28 +0100 | [diff] [blame] | 2887 | if (lag->ref_count == 1) { |
Ido Schimmel | 039c49a | 2016-01-27 15:20:18 +0100 | [diff] [blame] | 2888 | if (mlxsw_sp_port_fdb_flush_by_lag_id(mlxsw_sp_port)) |
| 2889 | netdev_err(mlxsw_sp_port->dev, "Failed to flush FDB\n"); |
Jiri Pirko | 0d65fc1 | 2015-12-03 12:12:28 +0100 | [diff] [blame] | 2890 | err = mlxsw_sp_lag_destroy(mlxsw_sp, lag_id); |
| 2891 | if (err) |
| 2892 | return err; |
| 2893 | } |
| 2894 | |
| 2895 | mlxsw_core_lag_mapping_clear(mlxsw_sp->core, lag_id, |
| 2896 | mlxsw_sp_port->local_port); |
| 2897 | mlxsw_sp_port->lagged = 0; |
| 2898 | lag->ref_count--; |
| 2899 | return 0; |
| 2900 | } |
| 2901 | |
Jiri Pirko | 7458120 | 2015-12-03 12:12:30 +0100 | [diff] [blame] | 2902 | static int mlxsw_sp_lag_dist_port_add(struct mlxsw_sp_port *mlxsw_sp_port, |
| 2903 | u16 lag_id) |
| 2904 | { |
| 2905 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
| 2906 | char sldr_pl[MLXSW_REG_SLDR_LEN]; |
| 2907 | |
| 2908 | mlxsw_reg_sldr_lag_add_port_pack(sldr_pl, lag_id, |
| 2909 | mlxsw_sp_port->local_port); |
| 2910 | return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sldr), sldr_pl); |
| 2911 | } |
| 2912 | |
| 2913 | static int mlxsw_sp_lag_dist_port_remove(struct mlxsw_sp_port *mlxsw_sp_port, |
| 2914 | u16 lag_id) |
| 2915 | { |
| 2916 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
| 2917 | char sldr_pl[MLXSW_REG_SLDR_LEN]; |
| 2918 | |
| 2919 | mlxsw_reg_sldr_lag_remove_port_pack(sldr_pl, lag_id, |
| 2920 | mlxsw_sp_port->local_port); |
| 2921 | return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sldr), sldr_pl); |
| 2922 | } |
| 2923 | |
| 2924 | static int mlxsw_sp_port_lag_tx_en_set(struct mlxsw_sp_port *mlxsw_sp_port, |
| 2925 | bool lag_tx_enabled) |
| 2926 | { |
| 2927 | if (lag_tx_enabled) |
| 2928 | return mlxsw_sp_lag_dist_port_add(mlxsw_sp_port, |
| 2929 | mlxsw_sp_port->lag_id); |
| 2930 | else |
| 2931 | return mlxsw_sp_lag_dist_port_remove(mlxsw_sp_port, |
| 2932 | mlxsw_sp_port->lag_id); |
| 2933 | } |
| 2934 | |
| 2935 | static int mlxsw_sp_port_lag_changed(struct mlxsw_sp_port *mlxsw_sp_port, |
| 2936 | struct netdev_lag_lower_state_info *info) |
| 2937 | { |
| 2938 | return mlxsw_sp_port_lag_tx_en_set(mlxsw_sp_port, info->tx_enabled); |
| 2939 | } |
| 2940 | |
Ido Schimmel | 9589a7b5 | 2015-12-15 16:03:43 +0100 | [diff] [blame] | 2941 | static int mlxsw_sp_port_vlan_link(struct mlxsw_sp_port *mlxsw_sp_port, |
| 2942 | struct net_device *vlan_dev) |
| 2943 | { |
| 2944 | struct mlxsw_sp_port *mlxsw_sp_vport; |
| 2945 | u16 vid = vlan_dev_vlan_id(vlan_dev); |
| 2946 | |
| 2947 | mlxsw_sp_vport = mlxsw_sp_port_vport_find(mlxsw_sp_port, vid); |
| 2948 | if (!mlxsw_sp_vport) { |
| 2949 | WARN_ON(!mlxsw_sp_vport); |
| 2950 | return -EINVAL; |
| 2951 | } |
| 2952 | |
| 2953 | mlxsw_sp_vport->dev = vlan_dev; |
| 2954 | |
| 2955 | return 0; |
| 2956 | } |
| 2957 | |
| 2958 | static int mlxsw_sp_port_vlan_unlink(struct mlxsw_sp_port *mlxsw_sp_port, |
| 2959 | struct net_device *vlan_dev) |
| 2960 | { |
| 2961 | struct mlxsw_sp_port *mlxsw_sp_vport; |
| 2962 | u16 vid = vlan_dev_vlan_id(vlan_dev); |
| 2963 | |
| 2964 | mlxsw_sp_vport = mlxsw_sp_port_vport_find(mlxsw_sp_port, vid); |
| 2965 | if (!mlxsw_sp_vport) { |
| 2966 | WARN_ON(!mlxsw_sp_vport); |
| 2967 | return -EINVAL; |
| 2968 | } |
| 2969 | |
Ido Schimmel | 26f0e7f | 2015-12-15 16:03:44 +0100 | [diff] [blame] | 2970 | /* When removing a VLAN device while still bridged we should first |
| 2971 | * remove it from the bridge, as we receive the bridge's notification |
| 2972 | * when the vPort is already gone. |
| 2973 | */ |
| 2974 | if (mlxsw_sp_vport->bridged) { |
| 2975 | struct net_device *br_dev; |
| 2976 | |
| 2977 | br_dev = mlxsw_sp_vport_br_get(mlxsw_sp_vport); |
Ido Schimmel | 039c49a | 2016-01-27 15:20:18 +0100 | [diff] [blame] | 2978 | mlxsw_sp_vport_bridge_leave(mlxsw_sp_vport, br_dev, true); |
Ido Schimmel | 26f0e7f | 2015-12-15 16:03:44 +0100 | [diff] [blame] | 2979 | } |
| 2980 | |
Ido Schimmel | 9589a7b5 | 2015-12-15 16:03:43 +0100 | [diff] [blame] | 2981 | mlxsw_sp_vport->dev = mlxsw_sp_port->dev; |
| 2982 | |
| 2983 | return 0; |
| 2984 | } |
| 2985 | |
Jiri Pirko | 7458120 | 2015-12-03 12:12:30 +0100 | [diff] [blame] | 2986 | static int mlxsw_sp_netdevice_port_upper_event(struct net_device *dev, |
| 2987 | unsigned long event, void *ptr) |
Jiri Pirko | 0d65fc1 | 2015-12-03 12:12:28 +0100 | [diff] [blame] | 2988 | { |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 2989 | struct netdev_notifier_changeupper_info *info; |
| 2990 | struct mlxsw_sp_port *mlxsw_sp_port; |
| 2991 | struct net_device *upper_dev; |
| 2992 | struct mlxsw_sp *mlxsw_sp; |
| 2993 | int err; |
| 2994 | |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 2995 | mlxsw_sp_port = netdev_priv(dev); |
| 2996 | mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
| 2997 | info = ptr; |
| 2998 | |
| 2999 | switch (event) { |
| 3000 | case NETDEV_PRECHANGEUPPER: |
| 3001 | upper_dev = info->upper_dev; |
Jiri Pirko | 0d65fc1 | 2015-12-03 12:12:28 +0100 | [diff] [blame] | 3002 | if (!info->master || !info->linking) |
| 3003 | break; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 3004 | /* HW limitation forbids to put ports to multiple bridges. */ |
Jiri Pirko | 0d65fc1 | 2015-12-03 12:12:28 +0100 | [diff] [blame] | 3005 | if (netif_is_bridge_master(upper_dev) && |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 3006 | !mlxsw_sp_master_bridge_check(mlxsw_sp, upper_dev)) |
| 3007 | return NOTIFY_BAD; |
Jiri Pirko | 0d65fc1 | 2015-12-03 12:12:28 +0100 | [diff] [blame] | 3008 | if (netif_is_lag_master(upper_dev) && |
| 3009 | !mlxsw_sp_master_lag_check(mlxsw_sp, upper_dev, |
| 3010 | info->upper_info)) |
| 3011 | return NOTIFY_BAD; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 3012 | break; |
| 3013 | case NETDEV_CHANGEUPPER: |
| 3014 | upper_dev = info->upper_dev; |
Ido Schimmel | 9589a7b5 | 2015-12-15 16:03:43 +0100 | [diff] [blame] | 3015 | if (is_vlan_dev(upper_dev)) { |
| 3016 | if (info->linking) { |
| 3017 | err = mlxsw_sp_port_vlan_link(mlxsw_sp_port, |
| 3018 | upper_dev); |
| 3019 | if (err) { |
| 3020 | netdev_err(dev, "Failed to link VLAN device\n"); |
| 3021 | return NOTIFY_BAD; |
| 3022 | } |
| 3023 | } else { |
| 3024 | err = mlxsw_sp_port_vlan_unlink(mlxsw_sp_port, |
| 3025 | upper_dev); |
| 3026 | if (err) { |
| 3027 | netdev_err(dev, "Failed to unlink VLAN device\n"); |
| 3028 | return NOTIFY_BAD; |
| 3029 | } |
| 3030 | } |
| 3031 | } else if (netif_is_bridge_master(upper_dev)) { |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 3032 | if (info->linking) { |
| 3033 | err = mlxsw_sp_port_bridge_join(mlxsw_sp_port); |
Ido Schimmel | 7812407 | 2016-01-04 10:42:24 +0100 | [diff] [blame] | 3034 | if (err) { |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 3035 | netdev_err(dev, "Failed to join bridge\n"); |
Ido Schimmel | 7812407 | 2016-01-04 10:42:24 +0100 | [diff] [blame] | 3036 | return NOTIFY_BAD; |
| 3037 | } |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 3038 | mlxsw_sp_master_bridge_inc(mlxsw_sp, upper_dev); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 3039 | } else { |
Ido Schimmel | 039c49a | 2016-01-27 15:20:18 +0100 | [diff] [blame] | 3040 | err = mlxsw_sp_port_bridge_leave(mlxsw_sp_port, |
| 3041 | true); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 3042 | mlxsw_sp_master_bridge_dec(mlxsw_sp, upper_dev); |
Ido Schimmel | 7812407 | 2016-01-04 10:42:24 +0100 | [diff] [blame] | 3043 | if (err) { |
| 3044 | netdev_err(dev, "Failed to leave bridge\n"); |
| 3045 | return NOTIFY_BAD; |
| 3046 | } |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 3047 | } |
Jiri Pirko | 0d65fc1 | 2015-12-03 12:12:28 +0100 | [diff] [blame] | 3048 | } else if (netif_is_lag_master(upper_dev)) { |
| 3049 | if (info->linking) { |
| 3050 | err = mlxsw_sp_port_lag_join(mlxsw_sp_port, |
| 3051 | upper_dev); |
| 3052 | if (err) { |
| 3053 | netdev_err(dev, "Failed to join link aggregation\n"); |
| 3054 | return NOTIFY_BAD; |
| 3055 | } |
| 3056 | } else { |
| 3057 | err = mlxsw_sp_port_lag_leave(mlxsw_sp_port, |
| 3058 | upper_dev); |
| 3059 | if (err) { |
| 3060 | netdev_err(dev, "Failed to leave link aggregation\n"); |
| 3061 | return NOTIFY_BAD; |
| 3062 | } |
| 3063 | } |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 3064 | } |
| 3065 | break; |
| 3066 | } |
| 3067 | |
| 3068 | return NOTIFY_DONE; |
| 3069 | } |
| 3070 | |
Jiri Pirko | 7458120 | 2015-12-03 12:12:30 +0100 | [diff] [blame] | 3071 | static int mlxsw_sp_netdevice_port_lower_event(struct net_device *dev, |
| 3072 | unsigned long event, void *ptr) |
| 3073 | { |
| 3074 | struct netdev_notifier_changelowerstate_info *info; |
| 3075 | struct mlxsw_sp_port *mlxsw_sp_port; |
| 3076 | int err; |
| 3077 | |
| 3078 | mlxsw_sp_port = netdev_priv(dev); |
| 3079 | info = ptr; |
| 3080 | |
| 3081 | switch (event) { |
| 3082 | case NETDEV_CHANGELOWERSTATE: |
| 3083 | if (netif_is_lag_port(dev) && mlxsw_sp_port->lagged) { |
| 3084 | err = mlxsw_sp_port_lag_changed(mlxsw_sp_port, |
| 3085 | info->lower_state_info); |
| 3086 | if (err) |
| 3087 | netdev_err(dev, "Failed to reflect link aggregation lower state change\n"); |
| 3088 | } |
| 3089 | break; |
| 3090 | } |
| 3091 | |
| 3092 | return NOTIFY_DONE; |
| 3093 | } |
| 3094 | |
| 3095 | static int mlxsw_sp_netdevice_port_event(struct net_device *dev, |
| 3096 | unsigned long event, void *ptr) |
| 3097 | { |
| 3098 | switch (event) { |
| 3099 | case NETDEV_PRECHANGEUPPER: |
| 3100 | case NETDEV_CHANGEUPPER: |
| 3101 | return mlxsw_sp_netdevice_port_upper_event(dev, event, ptr); |
| 3102 | case NETDEV_CHANGELOWERSTATE: |
| 3103 | return mlxsw_sp_netdevice_port_lower_event(dev, event, ptr); |
| 3104 | } |
| 3105 | |
| 3106 | return NOTIFY_DONE; |
| 3107 | } |
| 3108 | |
Jiri Pirko | 0d65fc1 | 2015-12-03 12:12:28 +0100 | [diff] [blame] | 3109 | static int mlxsw_sp_netdevice_lag_event(struct net_device *lag_dev, |
| 3110 | unsigned long event, void *ptr) |
| 3111 | { |
| 3112 | struct net_device *dev; |
| 3113 | struct list_head *iter; |
| 3114 | int ret; |
| 3115 | |
| 3116 | netdev_for_each_lower_dev(lag_dev, dev, iter) { |
| 3117 | if (mlxsw_sp_port_dev_check(dev)) { |
| 3118 | ret = mlxsw_sp_netdevice_port_event(dev, event, ptr); |
| 3119 | if (ret == NOTIFY_BAD) |
| 3120 | return ret; |
| 3121 | } |
| 3122 | } |
| 3123 | |
| 3124 | return NOTIFY_DONE; |
| 3125 | } |
| 3126 | |
Ido Schimmel | 26f0e7f | 2015-12-15 16:03:44 +0100 | [diff] [blame] | 3127 | static struct mlxsw_sp_vfid * |
| 3128 | mlxsw_sp_br_vfid_find(const struct mlxsw_sp *mlxsw_sp, |
| 3129 | const struct net_device *br_dev) |
| 3130 | { |
| 3131 | struct mlxsw_sp_vfid *vfid; |
| 3132 | |
| 3133 | list_for_each_entry(vfid, &mlxsw_sp->br_vfids.list, list) { |
| 3134 | if (vfid->br_dev == br_dev) |
| 3135 | return vfid; |
| 3136 | } |
| 3137 | |
| 3138 | return NULL; |
| 3139 | } |
| 3140 | |
| 3141 | static u16 mlxsw_sp_vfid_to_br_vfid(u16 vfid) |
| 3142 | { |
| 3143 | return vfid - MLXSW_SP_VFID_PORT_MAX; |
| 3144 | } |
| 3145 | |
| 3146 | static u16 mlxsw_sp_br_vfid_to_vfid(u16 br_vfid) |
| 3147 | { |
| 3148 | return MLXSW_SP_VFID_PORT_MAX + br_vfid; |
| 3149 | } |
| 3150 | |
| 3151 | static u16 mlxsw_sp_avail_br_vfid_get(const struct mlxsw_sp *mlxsw_sp) |
| 3152 | { |
| 3153 | return find_first_zero_bit(mlxsw_sp->br_vfids.mapped, |
| 3154 | MLXSW_SP_VFID_BR_MAX); |
| 3155 | } |
| 3156 | |
| 3157 | static struct mlxsw_sp_vfid *mlxsw_sp_br_vfid_create(struct mlxsw_sp *mlxsw_sp, |
| 3158 | struct net_device *br_dev) |
| 3159 | { |
| 3160 | struct device *dev = mlxsw_sp->bus_info->dev; |
| 3161 | struct mlxsw_sp_vfid *vfid; |
| 3162 | u16 n_vfid; |
| 3163 | int err; |
| 3164 | |
| 3165 | n_vfid = mlxsw_sp_br_vfid_to_vfid(mlxsw_sp_avail_br_vfid_get(mlxsw_sp)); |
| 3166 | if (n_vfid == MLXSW_SP_VFID_MAX) { |
| 3167 | dev_err(dev, "No available vFIDs\n"); |
| 3168 | return ERR_PTR(-ERANGE); |
| 3169 | } |
| 3170 | |
| 3171 | err = __mlxsw_sp_vfid_create(mlxsw_sp, n_vfid); |
| 3172 | if (err) { |
| 3173 | dev_err(dev, "Failed to create vFID=%d\n", n_vfid); |
| 3174 | return ERR_PTR(err); |
| 3175 | } |
| 3176 | |
| 3177 | vfid = kzalloc(sizeof(*vfid), GFP_KERNEL); |
| 3178 | if (!vfid) |
| 3179 | goto err_allocate_vfid; |
| 3180 | |
| 3181 | vfid->vfid = n_vfid; |
| 3182 | vfid->br_dev = br_dev; |
| 3183 | |
| 3184 | list_add(&vfid->list, &mlxsw_sp->br_vfids.list); |
| 3185 | set_bit(mlxsw_sp_vfid_to_br_vfid(n_vfid), mlxsw_sp->br_vfids.mapped); |
| 3186 | |
| 3187 | return vfid; |
| 3188 | |
| 3189 | err_allocate_vfid: |
| 3190 | __mlxsw_sp_vfid_destroy(mlxsw_sp, n_vfid); |
| 3191 | return ERR_PTR(-ENOMEM); |
| 3192 | } |
| 3193 | |
| 3194 | static void mlxsw_sp_br_vfid_destroy(struct mlxsw_sp *mlxsw_sp, |
| 3195 | struct mlxsw_sp_vfid *vfid) |
| 3196 | { |
| 3197 | u16 br_vfid = mlxsw_sp_vfid_to_br_vfid(vfid->vfid); |
| 3198 | |
| 3199 | clear_bit(br_vfid, mlxsw_sp->br_vfids.mapped); |
| 3200 | list_del(&vfid->list); |
| 3201 | |
| 3202 | __mlxsw_sp_vfid_destroy(mlxsw_sp, vfid->vfid); |
| 3203 | |
| 3204 | kfree(vfid); |
| 3205 | } |
| 3206 | |
| 3207 | static int mlxsw_sp_vport_bridge_leave(struct mlxsw_sp_port *mlxsw_sp_vport, |
Ido Schimmel | 039c49a | 2016-01-27 15:20:18 +0100 | [diff] [blame] | 3208 | struct net_device *br_dev, |
| 3209 | bool flush_fdb) |
Ido Schimmel | 26f0e7f | 2015-12-15 16:03:44 +0100 | [diff] [blame] | 3210 | { |
| 3211 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_vport->mlxsw_sp; |
| 3212 | u16 vid = mlxsw_sp_vport_vid_get(mlxsw_sp_vport); |
| 3213 | struct net_device *dev = mlxsw_sp_vport->dev; |
| 3214 | struct mlxsw_sp_vfid *vfid, *new_vfid; |
| 3215 | int err; |
| 3216 | |
| 3217 | vfid = mlxsw_sp_br_vfid_find(mlxsw_sp, br_dev); |
| 3218 | if (!vfid) { |
| 3219 | WARN_ON(!vfid); |
| 3220 | return -EINVAL; |
| 3221 | } |
| 3222 | |
| 3223 | /* We need a vFID to go back to after leaving the bridge's vFID. */ |
| 3224 | new_vfid = mlxsw_sp_vfid_find(mlxsw_sp, vid); |
| 3225 | if (!new_vfid) { |
| 3226 | new_vfid = mlxsw_sp_vfid_create(mlxsw_sp, vid); |
| 3227 | if (IS_ERR(new_vfid)) { |
| 3228 | netdev_err(dev, "Failed to create vFID for VID=%d\n", |
| 3229 | vid); |
| 3230 | return PTR_ERR(new_vfid); |
| 3231 | } |
| 3232 | } |
| 3233 | |
| 3234 | /* Invalidate existing {Port, VID} to vFID mapping and create a new |
| 3235 | * one for the new vFID. |
| 3236 | */ |
| 3237 | err = mlxsw_sp_port_vid_to_fid_set(mlxsw_sp_vport, |
| 3238 | MLXSW_REG_SVFA_MT_PORT_VID_TO_FID, |
| 3239 | false, |
| 3240 | mlxsw_sp_vfid_to_fid(vfid->vfid), |
| 3241 | vid); |
| 3242 | if (err) { |
| 3243 | netdev_err(dev, "Failed to invalidate {Port, VID} to vFID=%d mapping\n", |
| 3244 | vfid->vfid); |
| 3245 | goto err_port_vid_to_fid_invalidate; |
| 3246 | } |
| 3247 | |
| 3248 | err = mlxsw_sp_port_vid_to_fid_set(mlxsw_sp_vport, |
| 3249 | MLXSW_REG_SVFA_MT_PORT_VID_TO_FID, |
| 3250 | true, |
| 3251 | mlxsw_sp_vfid_to_fid(new_vfid->vfid), |
| 3252 | vid); |
| 3253 | if (err) { |
| 3254 | netdev_err(dev, "Failed to map {Port, VID} to vFID=%d\n", |
| 3255 | new_vfid->vfid); |
| 3256 | goto err_port_vid_to_fid_validate; |
| 3257 | } |
| 3258 | |
| 3259 | err = mlxsw_sp_port_vid_learning_set(mlxsw_sp_vport, vid, false); |
| 3260 | if (err) { |
| 3261 | netdev_err(dev, "Failed to disable learning\n"); |
| 3262 | goto err_port_vid_learning_set; |
| 3263 | } |
| 3264 | |
| 3265 | err = mlxsw_sp_vport_flood_set(mlxsw_sp_vport, vfid->vfid, false, |
| 3266 | false); |
| 3267 | if (err) { |
| 3268 | netdev_err(dev, "Failed clear to clear flooding\n"); |
| 3269 | goto err_vport_flood_set; |
| 3270 | } |
| 3271 | |
Ido Schimmel | 6a9863a | 2016-02-15 13:19:54 +0100 | [diff] [blame] | 3272 | err = mlxsw_sp_port_stp_state_set(mlxsw_sp_vport, vid, |
| 3273 | MLXSW_REG_SPMS_STATE_FORWARDING); |
| 3274 | if (err) { |
| 3275 | netdev_err(dev, "Failed to set STP state\n"); |
| 3276 | goto err_port_stp_state_set; |
| 3277 | } |
| 3278 | |
Ido Schimmel | 039c49a | 2016-01-27 15:20:18 +0100 | [diff] [blame] | 3279 | if (flush_fdb && mlxsw_sp_vport_fdb_flush(mlxsw_sp_vport)) |
| 3280 | netdev_err(dev, "Failed to flush FDB\n"); |
| 3281 | |
Ido Schimmel | 26f0e7f | 2015-12-15 16:03:44 +0100 | [diff] [blame] | 3282 | /* Switch between the vFIDs and destroy the old one if needed. */ |
| 3283 | new_vfid->nr_vports++; |
| 3284 | mlxsw_sp_vport->vport.vfid = new_vfid; |
| 3285 | vfid->nr_vports--; |
| 3286 | if (!vfid->nr_vports) |
| 3287 | mlxsw_sp_br_vfid_destroy(mlxsw_sp, vfid); |
| 3288 | |
| 3289 | mlxsw_sp_vport->learning = 0; |
| 3290 | mlxsw_sp_vport->learning_sync = 0; |
| 3291 | mlxsw_sp_vport->uc_flood = 0; |
| 3292 | mlxsw_sp_vport->bridged = 0; |
| 3293 | |
| 3294 | return 0; |
| 3295 | |
Ido Schimmel | 6a9863a | 2016-02-15 13:19:54 +0100 | [diff] [blame] | 3296 | err_port_stp_state_set: |
Ido Schimmel | 26f0e7f | 2015-12-15 16:03:44 +0100 | [diff] [blame] | 3297 | err_vport_flood_set: |
| 3298 | err_port_vid_learning_set: |
| 3299 | err_port_vid_to_fid_validate: |
| 3300 | err_port_vid_to_fid_invalidate: |
| 3301 | /* Rollback vFID only if new. */ |
| 3302 | if (!new_vfid->nr_vports) |
| 3303 | mlxsw_sp_vfid_destroy(mlxsw_sp, new_vfid); |
| 3304 | return err; |
| 3305 | } |
| 3306 | |
| 3307 | static int mlxsw_sp_vport_bridge_join(struct mlxsw_sp_port *mlxsw_sp_vport, |
| 3308 | struct net_device *br_dev) |
| 3309 | { |
| 3310 | struct mlxsw_sp_vfid *old_vfid = mlxsw_sp_vport->vport.vfid; |
| 3311 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_vport->mlxsw_sp; |
| 3312 | u16 vid = mlxsw_sp_vport_vid_get(mlxsw_sp_vport); |
| 3313 | struct net_device *dev = mlxsw_sp_vport->dev; |
| 3314 | struct mlxsw_sp_vfid *vfid; |
| 3315 | int err; |
| 3316 | |
| 3317 | vfid = mlxsw_sp_br_vfid_find(mlxsw_sp, br_dev); |
| 3318 | if (!vfid) { |
| 3319 | vfid = mlxsw_sp_br_vfid_create(mlxsw_sp, br_dev); |
| 3320 | if (IS_ERR(vfid)) { |
| 3321 | netdev_err(dev, "Failed to create bridge vFID\n"); |
| 3322 | return PTR_ERR(vfid); |
| 3323 | } |
| 3324 | } |
| 3325 | |
| 3326 | err = mlxsw_sp_vport_flood_set(mlxsw_sp_vport, vfid->vfid, true, false); |
| 3327 | if (err) { |
| 3328 | netdev_err(dev, "Failed to setup flooding for vFID=%d\n", |
| 3329 | vfid->vfid); |
| 3330 | goto err_port_flood_set; |
| 3331 | } |
| 3332 | |
| 3333 | err = mlxsw_sp_port_vid_learning_set(mlxsw_sp_vport, vid, true); |
| 3334 | if (err) { |
| 3335 | netdev_err(dev, "Failed to enable learning\n"); |
| 3336 | goto err_port_vid_learning_set; |
| 3337 | } |
| 3338 | |
| 3339 | /* We need to invalidate existing {Port, VID} to vFID mapping and |
| 3340 | * create a new one for the bridge's vFID. |
| 3341 | */ |
| 3342 | err = mlxsw_sp_port_vid_to_fid_set(mlxsw_sp_vport, |
| 3343 | MLXSW_REG_SVFA_MT_PORT_VID_TO_FID, |
| 3344 | false, |
| 3345 | mlxsw_sp_vfid_to_fid(old_vfid->vfid), |
| 3346 | vid); |
| 3347 | if (err) { |
| 3348 | netdev_err(dev, "Failed to invalidate {Port, VID} to vFID=%d mapping\n", |
| 3349 | old_vfid->vfid); |
| 3350 | goto err_port_vid_to_fid_invalidate; |
| 3351 | } |
| 3352 | |
| 3353 | err = mlxsw_sp_port_vid_to_fid_set(mlxsw_sp_vport, |
| 3354 | MLXSW_REG_SVFA_MT_PORT_VID_TO_FID, |
| 3355 | true, |
| 3356 | mlxsw_sp_vfid_to_fid(vfid->vfid), |
| 3357 | vid); |
| 3358 | if (err) { |
| 3359 | netdev_err(dev, "Failed to map {Port, VID} to vFID=%d\n", |
| 3360 | vfid->vfid); |
| 3361 | goto err_port_vid_to_fid_validate; |
| 3362 | } |
| 3363 | |
| 3364 | /* Switch between the vFIDs and destroy the old one if needed. */ |
| 3365 | vfid->nr_vports++; |
| 3366 | mlxsw_sp_vport->vport.vfid = vfid; |
| 3367 | old_vfid->nr_vports--; |
| 3368 | if (!old_vfid->nr_vports) |
| 3369 | mlxsw_sp_vfid_destroy(mlxsw_sp, old_vfid); |
| 3370 | |
| 3371 | mlxsw_sp_vport->learning = 1; |
| 3372 | mlxsw_sp_vport->learning_sync = 1; |
| 3373 | mlxsw_sp_vport->uc_flood = 1; |
| 3374 | mlxsw_sp_vport->bridged = 1; |
| 3375 | |
| 3376 | return 0; |
| 3377 | |
| 3378 | err_port_vid_to_fid_validate: |
| 3379 | mlxsw_sp_port_vid_to_fid_set(mlxsw_sp_vport, |
| 3380 | MLXSW_REG_SVFA_MT_PORT_VID_TO_FID, false, |
| 3381 | mlxsw_sp_vfid_to_fid(old_vfid->vfid), vid); |
| 3382 | err_port_vid_to_fid_invalidate: |
| 3383 | mlxsw_sp_port_vid_learning_set(mlxsw_sp_vport, vid, false); |
| 3384 | err_port_vid_learning_set: |
| 3385 | mlxsw_sp_vport_flood_set(mlxsw_sp_vport, vfid->vfid, false, false); |
| 3386 | err_port_flood_set: |
| 3387 | if (!vfid->nr_vports) |
| 3388 | mlxsw_sp_br_vfid_destroy(mlxsw_sp, vfid); |
| 3389 | return err; |
| 3390 | } |
| 3391 | |
| 3392 | static bool |
| 3393 | mlxsw_sp_port_master_bridge_check(const struct mlxsw_sp_port *mlxsw_sp_port, |
| 3394 | const struct net_device *br_dev) |
| 3395 | { |
| 3396 | struct mlxsw_sp_port *mlxsw_sp_vport; |
| 3397 | |
| 3398 | list_for_each_entry(mlxsw_sp_vport, &mlxsw_sp_port->vports_list, |
| 3399 | vport.list) { |
| 3400 | if (mlxsw_sp_vport_br_get(mlxsw_sp_vport) == br_dev) |
| 3401 | return false; |
| 3402 | } |
| 3403 | |
| 3404 | return true; |
| 3405 | } |
| 3406 | |
| 3407 | static int mlxsw_sp_netdevice_vport_event(struct net_device *dev, |
| 3408 | unsigned long event, void *ptr, |
| 3409 | u16 vid) |
| 3410 | { |
| 3411 | struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev); |
| 3412 | struct netdev_notifier_changeupper_info *info = ptr; |
| 3413 | struct mlxsw_sp_port *mlxsw_sp_vport; |
| 3414 | struct net_device *upper_dev; |
| 3415 | int err; |
| 3416 | |
| 3417 | mlxsw_sp_vport = mlxsw_sp_port_vport_find(mlxsw_sp_port, vid); |
| 3418 | |
| 3419 | switch (event) { |
| 3420 | case NETDEV_PRECHANGEUPPER: |
| 3421 | upper_dev = info->upper_dev; |
| 3422 | if (!info->master || !info->linking) |
| 3423 | break; |
| 3424 | if (!netif_is_bridge_master(upper_dev)) |
| 3425 | return NOTIFY_BAD; |
| 3426 | /* We can't have multiple VLAN interfaces configured on |
| 3427 | * the same port and being members in the same bridge. |
| 3428 | */ |
| 3429 | if (!mlxsw_sp_port_master_bridge_check(mlxsw_sp_port, |
| 3430 | upper_dev)) |
| 3431 | return NOTIFY_BAD; |
| 3432 | break; |
| 3433 | case NETDEV_CHANGEUPPER: |
| 3434 | upper_dev = info->upper_dev; |
| 3435 | if (!info->master) |
| 3436 | break; |
| 3437 | if (info->linking) { |
| 3438 | if (!mlxsw_sp_vport) { |
| 3439 | WARN_ON(!mlxsw_sp_vport); |
| 3440 | return NOTIFY_BAD; |
| 3441 | } |
| 3442 | err = mlxsw_sp_vport_bridge_join(mlxsw_sp_vport, |
| 3443 | upper_dev); |
| 3444 | if (err) { |
| 3445 | netdev_err(dev, "Failed to join bridge\n"); |
| 3446 | return NOTIFY_BAD; |
| 3447 | } |
| 3448 | } else { |
| 3449 | /* We ignore bridge's unlinking notifications if vPort |
| 3450 | * is gone, since we already left the bridge when the |
| 3451 | * VLAN device was unlinked from the real device. |
| 3452 | */ |
| 3453 | if (!mlxsw_sp_vport) |
| 3454 | return NOTIFY_DONE; |
| 3455 | err = mlxsw_sp_vport_bridge_leave(mlxsw_sp_vport, |
Ido Schimmel | 039c49a | 2016-01-27 15:20:18 +0100 | [diff] [blame] | 3456 | upper_dev, true); |
Ido Schimmel | 26f0e7f | 2015-12-15 16:03:44 +0100 | [diff] [blame] | 3457 | if (err) { |
| 3458 | netdev_err(dev, "Failed to leave bridge\n"); |
| 3459 | return NOTIFY_BAD; |
| 3460 | } |
| 3461 | } |
| 3462 | } |
| 3463 | |
| 3464 | return NOTIFY_DONE; |
| 3465 | } |
| 3466 | |
Ido Schimmel | 272c447 | 2015-12-15 16:03:47 +0100 | [diff] [blame] | 3467 | static int mlxsw_sp_netdevice_lag_vport_event(struct net_device *lag_dev, |
| 3468 | unsigned long event, void *ptr, |
| 3469 | u16 vid) |
| 3470 | { |
| 3471 | struct net_device *dev; |
| 3472 | struct list_head *iter; |
| 3473 | int ret; |
| 3474 | |
| 3475 | netdev_for_each_lower_dev(lag_dev, dev, iter) { |
| 3476 | if (mlxsw_sp_port_dev_check(dev)) { |
| 3477 | ret = mlxsw_sp_netdevice_vport_event(dev, event, ptr, |
| 3478 | vid); |
| 3479 | if (ret == NOTIFY_BAD) |
| 3480 | return ret; |
| 3481 | } |
| 3482 | } |
| 3483 | |
| 3484 | return NOTIFY_DONE; |
| 3485 | } |
| 3486 | |
Ido Schimmel | 26f0e7f | 2015-12-15 16:03:44 +0100 | [diff] [blame] | 3487 | static int mlxsw_sp_netdevice_vlan_event(struct net_device *vlan_dev, |
| 3488 | unsigned long event, void *ptr) |
| 3489 | { |
| 3490 | struct net_device *real_dev = vlan_dev_real_dev(vlan_dev); |
| 3491 | u16 vid = vlan_dev_vlan_id(vlan_dev); |
| 3492 | |
Ido Schimmel | 272c447 | 2015-12-15 16:03:47 +0100 | [diff] [blame] | 3493 | if (mlxsw_sp_port_dev_check(real_dev)) |
| 3494 | return mlxsw_sp_netdevice_vport_event(real_dev, event, ptr, |
| 3495 | vid); |
| 3496 | else if (netif_is_lag_master(real_dev)) |
| 3497 | return mlxsw_sp_netdevice_lag_vport_event(real_dev, event, ptr, |
| 3498 | vid); |
Ido Schimmel | 26f0e7f | 2015-12-15 16:03:44 +0100 | [diff] [blame] | 3499 | |
Ido Schimmel | 272c447 | 2015-12-15 16:03:47 +0100 | [diff] [blame] | 3500 | return NOTIFY_DONE; |
Ido Schimmel | 26f0e7f | 2015-12-15 16:03:44 +0100 | [diff] [blame] | 3501 | } |
| 3502 | |
Jiri Pirko | 0d65fc1 | 2015-12-03 12:12:28 +0100 | [diff] [blame] | 3503 | static int mlxsw_sp_netdevice_event(struct notifier_block *unused, |
| 3504 | unsigned long event, void *ptr) |
| 3505 | { |
| 3506 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); |
| 3507 | |
| 3508 | if (mlxsw_sp_port_dev_check(dev)) |
| 3509 | return mlxsw_sp_netdevice_port_event(dev, event, ptr); |
| 3510 | |
| 3511 | if (netif_is_lag_master(dev)) |
| 3512 | return mlxsw_sp_netdevice_lag_event(dev, event, ptr); |
| 3513 | |
Ido Schimmel | 26f0e7f | 2015-12-15 16:03:44 +0100 | [diff] [blame] | 3514 | if (is_vlan_dev(dev)) |
| 3515 | return mlxsw_sp_netdevice_vlan_event(dev, event, ptr); |
| 3516 | |
Jiri Pirko | 0d65fc1 | 2015-12-03 12:12:28 +0100 | [diff] [blame] | 3517 | return NOTIFY_DONE; |
| 3518 | } |
| 3519 | |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 3520 | static struct notifier_block mlxsw_sp_netdevice_nb __read_mostly = { |
| 3521 | .notifier_call = mlxsw_sp_netdevice_event, |
| 3522 | }; |
| 3523 | |
| 3524 | static int __init mlxsw_sp_module_init(void) |
| 3525 | { |
| 3526 | int err; |
| 3527 | |
| 3528 | register_netdevice_notifier(&mlxsw_sp_netdevice_nb); |
| 3529 | err = mlxsw_core_driver_register(&mlxsw_sp_driver); |
| 3530 | if (err) |
| 3531 | goto err_core_driver_register; |
| 3532 | return 0; |
| 3533 | |
| 3534 | err_core_driver_register: |
| 3535 | unregister_netdevice_notifier(&mlxsw_sp_netdevice_nb); |
| 3536 | return err; |
| 3537 | } |
| 3538 | |
| 3539 | static void __exit mlxsw_sp_module_exit(void) |
| 3540 | { |
| 3541 | mlxsw_core_driver_unregister(&mlxsw_sp_driver); |
| 3542 | unregister_netdevice_notifier(&mlxsw_sp_netdevice_nb); |
| 3543 | } |
| 3544 | |
| 3545 | module_init(mlxsw_sp_module_init); |
| 3546 | module_exit(mlxsw_sp_module_exit); |
| 3547 | |
| 3548 | MODULE_LICENSE("Dual BSD/GPL"); |
| 3549 | MODULE_AUTHOR("Jiri Pirko <jiri@mellanox.com>"); |
| 3550 | MODULE_DESCRIPTION("Mellanox Spectrum driver"); |
| 3551 | MODULE_MLXSW_DRIVER_ALIAS(MLXSW_DEVICE_KIND_SPECTRUM); |