Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1 | /* |
| 2 | * drivers/net/ethernet/mellanox/mlxsw/spectrum_switchdev.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/types.h> |
| 39 | #include <linux/netdevice.h> |
| 40 | #include <linux/etherdevice.h> |
| 41 | #include <linux/slab.h> |
| 42 | #include <linux/device.h> |
| 43 | #include <linux/skbuff.h> |
| 44 | #include <linux/if_vlan.h> |
| 45 | #include <linux/if_bridge.h> |
| 46 | #include <linux/workqueue.h> |
| 47 | #include <linux/jiffies.h> |
Ido Schimmel | 4f2c6ae | 2016-01-27 15:16:43 +0100 | [diff] [blame] | 48 | #include <linux/rtnetlink.h> |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 49 | #include <net/switchdev.h> |
| 50 | |
| 51 | #include "spectrum.h" |
| 52 | #include "core.h" |
| 53 | #include "reg.h" |
| 54 | |
Elad Raz | e4b6f69 | 2016-01-10 21:06:27 +0100 | [diff] [blame] | 55 | static u16 mlxsw_sp_port_vid_to_fid_get(struct mlxsw_sp_port *mlxsw_sp_port, |
| 56 | u16 vid) |
| 57 | { |
Ido Schimmel | 56918b6 | 2016-06-20 23:04:18 +0200 | [diff] [blame] | 58 | struct mlxsw_sp_fid *f = mlxsw_sp_vport_fid_get(mlxsw_sp_port); |
Elad Raz | e4b6f69 | 2016-01-10 21:06:27 +0100 | [diff] [blame] | 59 | u16 fid = vid; |
| 60 | |
Ido Schimmel | 56918b6 | 2016-06-20 23:04:18 +0200 | [diff] [blame] | 61 | fid = f ? f->fid : fid; |
Elad Raz | e4b6f69 | 2016-01-10 21:06:27 +0100 | [diff] [blame] | 62 | |
| 63 | if (!fid) |
| 64 | fid = mlxsw_sp_port->pvid; |
| 65 | |
| 66 | return fid; |
| 67 | } |
| 68 | |
Ido Schimmel | 54a7320 | 2015-12-15 16:03:41 +0100 | [diff] [blame] | 69 | static struct mlxsw_sp_port * |
| 70 | mlxsw_sp_port_orig_get(struct net_device *dev, |
| 71 | struct mlxsw_sp_port *mlxsw_sp_port) |
| 72 | { |
| 73 | struct mlxsw_sp_port *mlxsw_sp_vport; |
| 74 | u16 vid; |
| 75 | |
| 76 | if (!is_vlan_dev(dev)) |
| 77 | return mlxsw_sp_port; |
| 78 | |
| 79 | vid = vlan_dev_vlan_id(dev); |
| 80 | mlxsw_sp_vport = mlxsw_sp_port_vport_find(mlxsw_sp_port, vid); |
| 81 | WARN_ON(!mlxsw_sp_vport); |
| 82 | |
| 83 | return mlxsw_sp_vport; |
| 84 | } |
| 85 | |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 86 | static int mlxsw_sp_port_attr_get(struct net_device *dev, |
| 87 | struct switchdev_attr *attr) |
| 88 | { |
| 89 | struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev); |
| 90 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
| 91 | |
Ido Schimmel | 54a7320 | 2015-12-15 16:03:41 +0100 | [diff] [blame] | 92 | mlxsw_sp_port = mlxsw_sp_port_orig_get(attr->orig_dev, mlxsw_sp_port); |
| 93 | if (!mlxsw_sp_port) |
| 94 | return -EINVAL; |
| 95 | |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 96 | switch (attr->id) { |
| 97 | case SWITCHDEV_ATTR_ID_PORT_PARENT_ID: |
| 98 | attr->u.ppid.id_len = sizeof(mlxsw_sp->base_mac); |
| 99 | memcpy(&attr->u.ppid.id, &mlxsw_sp->base_mac, |
| 100 | attr->u.ppid.id_len); |
| 101 | break; |
| 102 | case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS: |
| 103 | attr->u.brport_flags = |
| 104 | (mlxsw_sp_port->learning ? BR_LEARNING : 0) | |
Ido Schimmel | 0293038 | 2015-10-28 10:16:58 +0100 | [diff] [blame] | 105 | (mlxsw_sp_port->learning_sync ? BR_LEARNING_SYNC : 0) | |
| 106 | (mlxsw_sp_port->uc_flood ? BR_FLOOD : 0); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 107 | break; |
| 108 | default: |
| 109 | return -EOPNOTSUPP; |
| 110 | } |
| 111 | |
| 112 | return 0; |
| 113 | } |
| 114 | |
| 115 | static int mlxsw_sp_port_stp_state_set(struct mlxsw_sp_port *mlxsw_sp_port, |
| 116 | u8 state) |
| 117 | { |
| 118 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
| 119 | enum mlxsw_reg_spms_state spms_state; |
| 120 | char *spms_pl; |
| 121 | u16 vid; |
| 122 | int err; |
| 123 | |
| 124 | switch (state) { |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 125 | case BR_STATE_FORWARDING: |
| 126 | spms_state = MLXSW_REG_SPMS_STATE_FORWARDING; |
| 127 | break; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 128 | case BR_STATE_LEARNING: |
| 129 | spms_state = MLXSW_REG_SPMS_STATE_LEARNING; |
| 130 | break; |
Ido Schimmel | 4549113 | 2016-01-27 15:20:20 +0100 | [diff] [blame] | 131 | case BR_STATE_LISTENING: /* fall-through */ |
Ido Schimmel | 9cb026e | 2016-01-27 15:20:19 +0100 | [diff] [blame] | 132 | case BR_STATE_DISABLED: /* fall-through */ |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 133 | case BR_STATE_BLOCKING: |
| 134 | spms_state = MLXSW_REG_SPMS_STATE_DISCARDING; |
| 135 | break; |
| 136 | default: |
| 137 | BUG(); |
| 138 | } |
| 139 | |
| 140 | spms_pl = kmalloc(MLXSW_REG_SPMS_LEN, GFP_KERNEL); |
| 141 | if (!spms_pl) |
| 142 | return -ENOMEM; |
| 143 | mlxsw_reg_spms_pack(spms_pl, mlxsw_sp_port->local_port); |
Ido Schimmel | 54a7320 | 2015-12-15 16:03:41 +0100 | [diff] [blame] | 144 | |
| 145 | if (mlxsw_sp_port_is_vport(mlxsw_sp_port)) { |
| 146 | vid = mlxsw_sp_vport_vid_get(mlxsw_sp_port); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 147 | mlxsw_reg_spms_vid_pack(spms_pl, vid, spms_state); |
Ido Schimmel | 54a7320 | 2015-12-15 16:03:41 +0100 | [diff] [blame] | 148 | } else { |
| 149 | for_each_set_bit(vid, mlxsw_sp_port->active_vlans, VLAN_N_VID) |
| 150 | mlxsw_reg_spms_vid_pack(spms_pl, vid, spms_state); |
| 151 | } |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 152 | |
| 153 | err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(spms), spms_pl); |
| 154 | kfree(spms_pl); |
| 155 | return err; |
| 156 | } |
| 157 | |
| 158 | static int mlxsw_sp_port_attr_stp_state_set(struct mlxsw_sp_port *mlxsw_sp_port, |
| 159 | struct switchdev_trans *trans, |
| 160 | u8 state) |
| 161 | { |
| 162 | if (switchdev_trans_ph_prepare(trans)) |
| 163 | return 0; |
| 164 | |
| 165 | mlxsw_sp_port->stp_state = state; |
| 166 | return mlxsw_sp_port_stp_state_set(mlxsw_sp_port, state); |
| 167 | } |
| 168 | |
Ido Schimmel | 0293038 | 2015-10-28 10:16:58 +0100 | [diff] [blame] | 169 | static int __mlxsw_sp_port_flood_set(struct mlxsw_sp_port *mlxsw_sp_port, |
Ido Schimmel | c06a94e | 2015-12-15 16:03:38 +0100 | [diff] [blame] | 170 | u16 idx_begin, u16 idx_end, bool set, |
Ido Schimmel | 0293038 | 2015-10-28 10:16:58 +0100 | [diff] [blame] | 171 | bool only_uc) |
| 172 | { |
| 173 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
Ido Schimmel | 7f71eb4 | 2015-12-15 16:03:37 +0100 | [diff] [blame] | 174 | u16 local_port = mlxsw_sp_port->local_port; |
| 175 | enum mlxsw_flood_table_type table_type; |
Ido Schimmel | c06a94e | 2015-12-15 16:03:38 +0100 | [diff] [blame] | 176 | u16 range = idx_end - idx_begin + 1; |
Ido Schimmel | 0293038 | 2015-10-28 10:16:58 +0100 | [diff] [blame] | 177 | char *sftr_pl; |
| 178 | int err; |
| 179 | |
Ido Schimmel | 99724c1 | 2016-07-04 08:23:14 +0200 | [diff] [blame] | 180 | if (mlxsw_sp_port_is_vport(mlxsw_sp_port)) |
Ido Schimmel | 7f71eb4 | 2015-12-15 16:03:37 +0100 | [diff] [blame] | 181 | table_type = MLXSW_REG_SFGC_TABLE_TYPE_FID; |
Ido Schimmel | 99724c1 | 2016-07-04 08:23:14 +0200 | [diff] [blame] | 182 | else |
Ido Schimmel | 7f71eb4 | 2015-12-15 16:03:37 +0100 | [diff] [blame] | 183 | table_type = MLXSW_REG_SFGC_TABLE_TYPE_FID_OFFEST; |
Ido Schimmel | 7f71eb4 | 2015-12-15 16:03:37 +0100 | [diff] [blame] | 184 | |
Ido Schimmel | 0293038 | 2015-10-28 10:16:58 +0100 | [diff] [blame] | 185 | sftr_pl = kmalloc(MLXSW_REG_SFTR_LEN, GFP_KERNEL); |
| 186 | if (!sftr_pl) |
| 187 | return -ENOMEM; |
| 188 | |
Ido Schimmel | c06a94e | 2015-12-15 16:03:38 +0100 | [diff] [blame] | 189 | mlxsw_reg_sftr_pack(sftr_pl, MLXSW_SP_FLOOD_TABLE_UC, idx_begin, |
Ido Schimmel | 7f71eb4 | 2015-12-15 16:03:37 +0100 | [diff] [blame] | 190 | table_type, range, local_port, set); |
Ido Schimmel | 0293038 | 2015-10-28 10:16:58 +0100 | [diff] [blame] | 191 | err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sftr), sftr_pl); |
| 192 | if (err) |
| 193 | goto buffer_out; |
| 194 | |
| 195 | /* Flooding control allows one to decide whether a given port will |
| 196 | * flood unicast traffic for which there is no FDB entry. |
| 197 | */ |
| 198 | if (only_uc) |
| 199 | goto buffer_out; |
| 200 | |
Ido Schimmel | c06a94e | 2015-12-15 16:03:38 +0100 | [diff] [blame] | 201 | mlxsw_reg_sftr_pack(sftr_pl, MLXSW_SP_FLOOD_TABLE_BM, idx_begin, |
Ido Schimmel | 7f71eb4 | 2015-12-15 16:03:37 +0100 | [diff] [blame] | 202 | table_type, range, local_port, set); |
Ido Schimmel | 0293038 | 2015-10-28 10:16:58 +0100 | [diff] [blame] | 203 | err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sftr), sftr_pl); |
Ido Schimmel | 2889286 | 2016-05-06 22:18:40 +0200 | [diff] [blame] | 204 | if (err) |
| 205 | goto err_flood_bm_set; |
| 206 | else |
| 207 | goto buffer_out; |
Ido Schimmel | 0293038 | 2015-10-28 10:16:58 +0100 | [diff] [blame] | 208 | |
Ido Schimmel | 2889286 | 2016-05-06 22:18:40 +0200 | [diff] [blame] | 209 | err_flood_bm_set: |
| 210 | mlxsw_reg_sftr_pack(sftr_pl, MLXSW_SP_FLOOD_TABLE_UC, idx_begin, |
| 211 | table_type, range, local_port, !set); |
| 212 | mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sftr), sftr_pl); |
Ido Schimmel | 0293038 | 2015-10-28 10:16:58 +0100 | [diff] [blame] | 213 | buffer_out: |
| 214 | kfree(sftr_pl); |
| 215 | return err; |
| 216 | } |
| 217 | |
| 218 | static int mlxsw_sp_port_uc_flood_set(struct mlxsw_sp_port *mlxsw_sp_port, |
| 219 | bool set) |
| 220 | { |
| 221 | struct net_device *dev = mlxsw_sp_port->dev; |
| 222 | u16 vid, last_visited_vid; |
| 223 | int err; |
| 224 | |
Ido Schimmel | 54a7320 | 2015-12-15 16:03:41 +0100 | [diff] [blame] | 225 | if (mlxsw_sp_port_is_vport(mlxsw_sp_port)) { |
Ido Schimmel | 41b996c | 2016-06-20 23:04:17 +0200 | [diff] [blame] | 226 | u16 fid = mlxsw_sp_vport_fid_get(mlxsw_sp_port)->fid; |
| 227 | u16 vfid = mlxsw_sp_fid_to_vfid(fid); |
Ido Schimmel | 54a7320 | 2015-12-15 16:03:41 +0100 | [diff] [blame] | 228 | |
| 229 | return __mlxsw_sp_port_flood_set(mlxsw_sp_port, vfid, vfid, |
| 230 | set, true); |
| 231 | } |
| 232 | |
Ido Schimmel | 0293038 | 2015-10-28 10:16:58 +0100 | [diff] [blame] | 233 | for_each_set_bit(vid, mlxsw_sp_port->active_vlans, VLAN_N_VID) { |
| 234 | err = __mlxsw_sp_port_flood_set(mlxsw_sp_port, vid, vid, set, |
| 235 | true); |
| 236 | if (err) { |
| 237 | last_visited_vid = vid; |
| 238 | goto err_port_flood_set; |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | return 0; |
| 243 | |
| 244 | err_port_flood_set: |
| 245 | for_each_set_bit(vid, mlxsw_sp_port->active_vlans, last_visited_vid) |
| 246 | __mlxsw_sp_port_flood_set(mlxsw_sp_port, vid, vid, !set, true); |
| 247 | netdev_err(dev, "Failed to configure unicast flooding\n"); |
| 248 | return err; |
| 249 | } |
| 250 | |
Ido Schimmel | e606002 | 2016-06-20 23:04:11 +0200 | [diff] [blame] | 251 | int mlxsw_sp_vport_flood_set(struct mlxsw_sp_port *mlxsw_sp_vport, u16 fid, |
Ido Schimmel | 47a0a9e | 2016-06-20 23:04:08 +0200 | [diff] [blame] | 252 | bool set) |
Ido Schimmel | 7f71eb4 | 2015-12-15 16:03:37 +0100 | [diff] [blame] | 253 | { |
Ido Schimmel | e606002 | 2016-06-20 23:04:11 +0200 | [diff] [blame] | 254 | u16 vfid; |
| 255 | |
Ido Schimmel | 7f71eb4 | 2015-12-15 16:03:37 +0100 | [diff] [blame] | 256 | /* In case of vFIDs, index into the flooding table is relative to |
| 257 | * the start of the vFIDs range. |
| 258 | */ |
Ido Schimmel | e606002 | 2016-06-20 23:04:11 +0200 | [diff] [blame] | 259 | vfid = mlxsw_sp_fid_to_vfid(fid); |
Ido Schimmel | 19ae612 | 2015-12-15 16:03:39 +0100 | [diff] [blame] | 260 | return __mlxsw_sp_port_flood_set(mlxsw_sp_vport, vfid, vfid, set, |
Ido Schimmel | 47a0a9e | 2016-06-20 23:04:08 +0200 | [diff] [blame] | 261 | false); |
Ido Schimmel | 7f71eb4 | 2015-12-15 16:03:37 +0100 | [diff] [blame] | 262 | } |
| 263 | |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 264 | static int mlxsw_sp_port_attr_br_flags_set(struct mlxsw_sp_port *mlxsw_sp_port, |
| 265 | struct switchdev_trans *trans, |
| 266 | unsigned long brport_flags) |
| 267 | { |
Ido Schimmel | 0293038 | 2015-10-28 10:16:58 +0100 | [diff] [blame] | 268 | unsigned long uc_flood = mlxsw_sp_port->uc_flood ? BR_FLOOD : 0; |
| 269 | bool set; |
| 270 | int err; |
| 271 | |
Ido Schimmel | 6c72a3d | 2016-01-04 10:42:26 +0100 | [diff] [blame] | 272 | if (!mlxsw_sp_port->bridged) |
| 273 | return -EINVAL; |
| 274 | |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 275 | if (switchdev_trans_ph_prepare(trans)) |
| 276 | return 0; |
| 277 | |
Ido Schimmel | 0293038 | 2015-10-28 10:16:58 +0100 | [diff] [blame] | 278 | if ((uc_flood ^ brport_flags) & BR_FLOOD) { |
| 279 | set = mlxsw_sp_port->uc_flood ? false : true; |
| 280 | err = mlxsw_sp_port_uc_flood_set(mlxsw_sp_port, set); |
| 281 | if (err) |
| 282 | return err; |
| 283 | } |
| 284 | |
| 285 | mlxsw_sp_port->uc_flood = brport_flags & BR_FLOOD ? 1 : 0; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 286 | mlxsw_sp_port->learning = brport_flags & BR_LEARNING ? 1 : 0; |
| 287 | mlxsw_sp_port->learning_sync = brport_flags & BR_LEARNING_SYNC ? 1 : 0; |
Ido Schimmel | 0293038 | 2015-10-28 10:16:58 +0100 | [diff] [blame] | 288 | |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 289 | return 0; |
| 290 | } |
| 291 | |
| 292 | static int mlxsw_sp_ageing_set(struct mlxsw_sp *mlxsw_sp, u32 ageing_time) |
| 293 | { |
| 294 | char sfdat_pl[MLXSW_REG_SFDAT_LEN]; |
| 295 | int err; |
| 296 | |
| 297 | mlxsw_reg_sfdat_pack(sfdat_pl, ageing_time); |
| 298 | err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfdat), sfdat_pl); |
| 299 | if (err) |
| 300 | return err; |
| 301 | mlxsw_sp->ageing_time = ageing_time; |
| 302 | return 0; |
| 303 | } |
| 304 | |
| 305 | static int mlxsw_sp_port_attr_br_ageing_set(struct mlxsw_sp_port *mlxsw_sp_port, |
| 306 | struct switchdev_trans *trans, |
Jiri Pirko | 135f9ec | 2015-10-28 10:17:02 +0100 | [diff] [blame] | 307 | unsigned long ageing_clock_t) |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 308 | { |
| 309 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
Jiri Pirko | 135f9ec | 2015-10-28 10:17:02 +0100 | [diff] [blame] | 310 | unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock_t); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 311 | u32 ageing_time = jiffies_to_msecs(ageing_jiffies) / 1000; |
| 312 | |
Ido Schimmel | 869f63a | 2016-03-08 12:59:33 -0800 | [diff] [blame] | 313 | if (switchdev_trans_ph_prepare(trans)) { |
| 314 | if (ageing_time < MLXSW_SP_MIN_AGEING_TIME || |
| 315 | ageing_time > MLXSW_SP_MAX_AGEING_TIME) |
| 316 | return -ERANGE; |
| 317 | else |
| 318 | return 0; |
| 319 | } |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 320 | |
| 321 | return mlxsw_sp_ageing_set(mlxsw_sp, ageing_time); |
| 322 | } |
| 323 | |
Elad Raz | 26a4ea0 | 2016-01-06 13:01:10 +0100 | [diff] [blame] | 324 | static int mlxsw_sp_port_attr_br_vlan_set(struct mlxsw_sp_port *mlxsw_sp_port, |
| 325 | struct switchdev_trans *trans, |
| 326 | struct net_device *orig_dev, |
| 327 | bool vlan_enabled) |
| 328 | { |
| 329 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
| 330 | |
| 331 | /* SWITCHDEV_TRANS_PREPARE phase */ |
| 332 | if ((!vlan_enabled) && (mlxsw_sp->master_bridge.dev == orig_dev)) { |
| 333 | netdev_err(mlxsw_sp_port->dev, "Bridge must be vlan-aware\n"); |
| 334 | return -EINVAL; |
| 335 | } |
| 336 | |
| 337 | return 0; |
| 338 | } |
| 339 | |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 340 | static int mlxsw_sp_port_attr_set(struct net_device *dev, |
| 341 | const struct switchdev_attr *attr, |
| 342 | struct switchdev_trans *trans) |
| 343 | { |
| 344 | struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev); |
| 345 | int err = 0; |
| 346 | |
Ido Schimmel | 54a7320 | 2015-12-15 16:03:41 +0100 | [diff] [blame] | 347 | mlxsw_sp_port = mlxsw_sp_port_orig_get(attr->orig_dev, mlxsw_sp_port); |
| 348 | if (!mlxsw_sp_port) |
| 349 | return -EINVAL; |
| 350 | |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 351 | switch (attr->id) { |
| 352 | case SWITCHDEV_ATTR_ID_PORT_STP_STATE: |
| 353 | err = mlxsw_sp_port_attr_stp_state_set(mlxsw_sp_port, trans, |
| 354 | attr->u.stp_state); |
| 355 | break; |
| 356 | case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS: |
| 357 | err = mlxsw_sp_port_attr_br_flags_set(mlxsw_sp_port, trans, |
| 358 | attr->u.brport_flags); |
| 359 | break; |
| 360 | case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME: |
| 361 | err = mlxsw_sp_port_attr_br_ageing_set(mlxsw_sp_port, trans, |
| 362 | attr->u.ageing_time); |
| 363 | break; |
Elad Raz | 26a4ea0 | 2016-01-06 13:01:10 +0100 | [diff] [blame] | 364 | case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING: |
| 365 | err = mlxsw_sp_port_attr_br_vlan_set(mlxsw_sp_port, trans, |
| 366 | attr->orig_dev, |
| 367 | attr->u.vlan_filtering); |
| 368 | break; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 369 | default: |
| 370 | err = -EOPNOTSUPP; |
| 371 | break; |
| 372 | } |
| 373 | |
| 374 | return err; |
| 375 | } |
| 376 | |
Ido Schimmel | 14d3946 | 2016-06-20 23:04:15 +0200 | [diff] [blame] | 377 | static int mlxsw_sp_fid_op(struct mlxsw_sp *mlxsw_sp, u16 fid, bool create) |
| 378 | { |
| 379 | char sfmr_pl[MLXSW_REG_SFMR_LEN]; |
| 380 | |
| 381 | mlxsw_reg_sfmr_pack(sfmr_pl, !create, fid, fid); |
| 382 | return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfmr), sfmr_pl); |
| 383 | } |
| 384 | |
| 385 | static int mlxsw_sp_fid_map(struct mlxsw_sp *mlxsw_sp, u16 fid, bool valid) |
| 386 | { |
| 387 | enum mlxsw_reg_svfa_mt mt = MLXSW_REG_SVFA_MT_VID_TO_FID; |
| 388 | char svfa_pl[MLXSW_REG_SVFA_LEN]; |
| 389 | |
| 390 | mlxsw_reg_svfa_pack(svfa_pl, 0, mt, valid, fid, fid); |
| 391 | return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(svfa), svfa_pl); |
| 392 | } |
| 393 | |
| 394 | static struct mlxsw_sp_fid *mlxsw_sp_fid_alloc(u16 fid) |
| 395 | { |
| 396 | struct mlxsw_sp_fid *f; |
| 397 | |
| 398 | f = kzalloc(sizeof(*f), GFP_KERNEL); |
| 399 | if (!f) |
| 400 | return NULL; |
| 401 | |
| 402 | f->fid = fid; |
| 403 | |
| 404 | return f; |
| 405 | } |
| 406 | |
Ido Schimmel | 701b186 | 2016-07-04 08:23:16 +0200 | [diff] [blame] | 407 | struct mlxsw_sp_fid *mlxsw_sp_fid_create(struct mlxsw_sp *mlxsw_sp, u16 fid) |
Ido Schimmel | 14d3946 | 2016-06-20 23:04:15 +0200 | [diff] [blame] | 408 | { |
| 409 | struct mlxsw_sp_fid *f; |
| 410 | int err; |
| 411 | |
| 412 | err = mlxsw_sp_fid_op(mlxsw_sp, fid, true); |
| 413 | if (err) |
| 414 | return ERR_PTR(err); |
| 415 | |
| 416 | /* Although all the ports member in the FID might be using a |
| 417 | * {Port, VID} to FID mapping, we create a global VID-to-FID |
| 418 | * mapping. This allows a port to transition to VLAN mode, |
| 419 | * knowing the global mapping exists. |
| 420 | */ |
| 421 | err = mlxsw_sp_fid_map(mlxsw_sp, fid, true); |
| 422 | if (err) |
| 423 | goto err_fid_map; |
| 424 | |
| 425 | f = mlxsw_sp_fid_alloc(fid); |
| 426 | if (!f) { |
| 427 | err = -ENOMEM; |
| 428 | goto err_allocate_fid; |
| 429 | } |
| 430 | |
| 431 | list_add(&f->list, &mlxsw_sp->fids); |
| 432 | |
| 433 | return f; |
| 434 | |
| 435 | err_allocate_fid: |
| 436 | mlxsw_sp_fid_map(mlxsw_sp, fid, false); |
| 437 | err_fid_map: |
| 438 | mlxsw_sp_fid_op(mlxsw_sp, fid, false); |
| 439 | return ERR_PTR(err); |
| 440 | } |
| 441 | |
Ido Schimmel | 701b186 | 2016-07-04 08:23:16 +0200 | [diff] [blame] | 442 | void mlxsw_sp_fid_destroy(struct mlxsw_sp *mlxsw_sp, struct mlxsw_sp_fid *f) |
Ido Schimmel | 14d3946 | 2016-06-20 23:04:15 +0200 | [diff] [blame] | 443 | { |
| 444 | u16 fid = f->fid; |
| 445 | |
| 446 | list_del(&f->list); |
| 447 | |
Ido Schimmel | 99f44bb | 2016-07-04 08:23:17 +0200 | [diff] [blame] | 448 | if (f->r) |
| 449 | mlxsw_sp_rif_bridge_destroy(mlxsw_sp, f->r); |
| 450 | |
Ido Schimmel | 14d3946 | 2016-06-20 23:04:15 +0200 | [diff] [blame] | 451 | kfree(f); |
| 452 | |
| 453 | mlxsw_sp_fid_op(mlxsw_sp, fid, false); |
| 454 | } |
| 455 | |
| 456 | static int __mlxsw_sp_port_fid_join(struct mlxsw_sp_port *mlxsw_sp_port, |
| 457 | u16 fid) |
| 458 | { |
| 459 | struct mlxsw_sp_fid *f; |
| 460 | |
| 461 | f = mlxsw_sp_fid_find(mlxsw_sp_port->mlxsw_sp, fid); |
| 462 | if (!f) { |
| 463 | f = mlxsw_sp_fid_create(mlxsw_sp_port->mlxsw_sp, fid); |
| 464 | if (IS_ERR(f)) |
| 465 | return PTR_ERR(f); |
| 466 | } |
| 467 | |
| 468 | f->ref_count++; |
| 469 | |
Ido Schimmel | 2230537 | 2016-06-20 23:04:21 +0200 | [diff] [blame] | 470 | netdev_dbg(mlxsw_sp_port->dev, "Joined FID=%d\n", fid); |
| 471 | |
Ido Schimmel | 14d3946 | 2016-06-20 23:04:15 +0200 | [diff] [blame] | 472 | return 0; |
| 473 | } |
| 474 | |
| 475 | static void __mlxsw_sp_port_fid_leave(struct mlxsw_sp_port *mlxsw_sp_port, |
| 476 | u16 fid) |
| 477 | { |
| 478 | struct mlxsw_sp_fid *f; |
| 479 | |
| 480 | f = mlxsw_sp_fid_find(mlxsw_sp_port->mlxsw_sp, fid); |
| 481 | if (WARN_ON(!f)) |
| 482 | return; |
| 483 | |
Ido Schimmel | 2230537 | 2016-06-20 23:04:21 +0200 | [diff] [blame] | 484 | netdev_dbg(mlxsw_sp_port->dev, "Left FID=%d\n", fid); |
| 485 | |
Ido Schimmel | fe3f6d1 | 2016-06-20 23:04:19 +0200 | [diff] [blame] | 486 | mlxsw_sp_port_fdb_flush(mlxsw_sp_port, fid); |
| 487 | |
Ido Schimmel | 14d3946 | 2016-06-20 23:04:15 +0200 | [diff] [blame] | 488 | if (--f->ref_count == 0) |
| 489 | mlxsw_sp_fid_destroy(mlxsw_sp_port->mlxsw_sp, f); |
| 490 | } |
| 491 | |
| 492 | static int mlxsw_sp_port_fid_map(struct mlxsw_sp_port *mlxsw_sp_port, u16 fid, |
| 493 | bool valid) |
| 494 | { |
| 495 | enum mlxsw_reg_svfa_mt mt = MLXSW_REG_SVFA_MT_PORT_VID_TO_FID; |
| 496 | |
| 497 | /* If port doesn't have vPorts, then it can use the global |
| 498 | * VID-to-FID mapping. |
| 499 | */ |
| 500 | if (list_empty(&mlxsw_sp_port->vports_list)) |
| 501 | return 0; |
| 502 | |
| 503 | return mlxsw_sp_port_vid_to_fid_set(mlxsw_sp_port, mt, valid, fid, fid); |
| 504 | } |
| 505 | |
| 506 | static int mlxsw_sp_port_fid_join(struct mlxsw_sp_port *mlxsw_sp_port, |
| 507 | u16 fid_begin, u16 fid_end) |
| 508 | { |
| 509 | int fid, err; |
| 510 | |
| 511 | for (fid = fid_begin; fid <= fid_end; fid++) { |
| 512 | err = __mlxsw_sp_port_fid_join(mlxsw_sp_port, fid); |
| 513 | if (err) |
| 514 | goto err_port_fid_join; |
| 515 | } |
| 516 | |
| 517 | err = __mlxsw_sp_port_flood_set(mlxsw_sp_port, fid_begin, fid_end, |
| 518 | true, false); |
| 519 | if (err) |
| 520 | goto err_port_flood_set; |
| 521 | |
| 522 | for (fid = fid_begin; fid <= fid_end; fid++) { |
| 523 | err = mlxsw_sp_port_fid_map(mlxsw_sp_port, fid, true); |
| 524 | if (err) |
| 525 | goto err_port_fid_map; |
| 526 | } |
| 527 | |
| 528 | return 0; |
| 529 | |
| 530 | err_port_fid_map: |
| 531 | for (fid--; fid >= fid_begin; fid--) |
| 532 | mlxsw_sp_port_fid_map(mlxsw_sp_port, fid, false); |
| 533 | __mlxsw_sp_port_flood_set(mlxsw_sp_port, fid_begin, fid_end, false, |
| 534 | false); |
| 535 | err_port_flood_set: |
| 536 | fid = fid_end; |
| 537 | err_port_fid_join: |
| 538 | for (fid--; fid >= fid_begin; fid--) |
| 539 | __mlxsw_sp_port_fid_leave(mlxsw_sp_port, fid); |
| 540 | return err; |
| 541 | } |
| 542 | |
| 543 | static void mlxsw_sp_port_fid_leave(struct mlxsw_sp_port *mlxsw_sp_port, |
| 544 | u16 fid_begin, u16 fid_end) |
| 545 | { |
| 546 | int fid; |
| 547 | |
| 548 | for (fid = fid_begin; fid <= fid_end; fid++) |
| 549 | mlxsw_sp_port_fid_map(mlxsw_sp_port, fid, false); |
| 550 | |
| 551 | __mlxsw_sp_port_flood_set(mlxsw_sp_port, fid_begin, fid_end, false, |
| 552 | false); |
| 553 | |
| 554 | for (fid = fid_begin; fid <= fid_end; fid++) |
| 555 | __mlxsw_sp_port_fid_leave(mlxsw_sp_port, fid); |
| 556 | } |
| 557 | |
Ido Schimmel | 28a01d2 | 2016-02-18 11:30:02 +0100 | [diff] [blame] | 558 | static int __mlxsw_sp_port_pvid_set(struct mlxsw_sp_port *mlxsw_sp_port, |
| 559 | u16 vid) |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 560 | { |
| 561 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
| 562 | char spvid_pl[MLXSW_REG_SPVID_LEN]; |
| 563 | |
| 564 | mlxsw_reg_spvid_pack(spvid_pl, mlxsw_sp_port->local_port, vid); |
| 565 | return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(spvid), spvid_pl); |
| 566 | } |
| 567 | |
Ido Schimmel | 28a01d2 | 2016-02-18 11:30:02 +0100 | [diff] [blame] | 568 | static int mlxsw_sp_port_allow_untagged_set(struct mlxsw_sp_port *mlxsw_sp_port, |
| 569 | bool allow) |
| 570 | { |
| 571 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
| 572 | char spaft_pl[MLXSW_REG_SPAFT_LEN]; |
| 573 | |
| 574 | mlxsw_reg_spaft_pack(spaft_pl, mlxsw_sp_port->local_port, allow); |
| 575 | return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(spaft), spaft_pl); |
| 576 | } |
| 577 | |
| 578 | int mlxsw_sp_port_pvid_set(struct mlxsw_sp_port *mlxsw_sp_port, u16 vid) |
| 579 | { |
| 580 | struct net_device *dev = mlxsw_sp_port->dev; |
| 581 | int err; |
| 582 | |
| 583 | if (!vid) { |
| 584 | err = mlxsw_sp_port_allow_untagged_set(mlxsw_sp_port, false); |
| 585 | if (err) { |
| 586 | netdev_err(dev, "Failed to disallow untagged traffic\n"); |
| 587 | return err; |
| 588 | } |
| 589 | } else { |
| 590 | err = __mlxsw_sp_port_pvid_set(mlxsw_sp_port, vid); |
| 591 | if (err) { |
| 592 | netdev_err(dev, "Failed to set PVID\n"); |
| 593 | return err; |
| 594 | } |
| 595 | |
| 596 | /* Only allow if not already allowed. */ |
| 597 | if (!mlxsw_sp_port->pvid) { |
| 598 | err = mlxsw_sp_port_allow_untagged_set(mlxsw_sp_port, |
| 599 | true); |
| 600 | if (err) { |
| 601 | netdev_err(dev, "Failed to allow untagged traffic\n"); |
| 602 | goto err_port_allow_untagged_set; |
| 603 | } |
| 604 | } |
| 605 | } |
| 606 | |
| 607 | mlxsw_sp_port->pvid = vid; |
| 608 | return 0; |
| 609 | |
| 610 | err_port_allow_untagged_set: |
| 611 | __mlxsw_sp_port_pvid_set(mlxsw_sp_port, mlxsw_sp_port->pvid); |
| 612 | return err; |
| 613 | } |
| 614 | |
Ido Schimmel | 3b7ad5e | 2015-11-19 12:27:39 +0100 | [diff] [blame] | 615 | static int __mlxsw_sp_port_vlans_set(struct mlxsw_sp_port *mlxsw_sp_port, |
| 616 | u16 vid_begin, u16 vid_end, bool is_member, |
| 617 | bool untagged) |
| 618 | { |
| 619 | u16 vid, vid_e; |
| 620 | int err; |
| 621 | |
| 622 | for (vid = vid_begin; vid <= vid_end; |
| 623 | vid += MLXSW_REG_SPVM_REC_MAX_COUNT) { |
| 624 | vid_e = min((u16) (vid + MLXSW_REG_SPVM_REC_MAX_COUNT - 1), |
| 625 | vid_end); |
| 626 | |
| 627 | err = mlxsw_sp_port_vlan_set(mlxsw_sp_port, vid, vid_e, |
| 628 | is_member, untagged); |
| 629 | if (err) |
| 630 | return err; |
| 631 | } |
| 632 | |
| 633 | return 0; |
| 634 | } |
| 635 | |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 636 | static int __mlxsw_sp_port_vlans_add(struct mlxsw_sp_port *mlxsw_sp_port, |
| 637 | u16 vid_begin, u16 vid_end, |
| 638 | bool flag_untagged, bool flag_pvid) |
| 639 | { |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 640 | struct net_device *dev = mlxsw_sp_port->dev; |
Ido Schimmel | 14d3946 | 2016-06-20 23:04:15 +0200 | [diff] [blame] | 641 | u16 vid, old_pvid; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 642 | int err; |
| 643 | |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 644 | if (!mlxsw_sp_port->bridged) |
Ido Schimmel | 32d863f | 2016-07-02 11:00:10 +0200 | [diff] [blame] | 645 | return -EINVAL; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 646 | |
Ido Schimmel | 14d3946 | 2016-06-20 23:04:15 +0200 | [diff] [blame] | 647 | err = mlxsw_sp_port_fid_join(mlxsw_sp_port, vid_begin, vid_end); |
Ido Schimmel | 1b3433a | 2015-10-28 10:16:57 +0100 | [diff] [blame] | 648 | if (err) { |
Ido Schimmel | 14d3946 | 2016-06-20 23:04:15 +0200 | [diff] [blame] | 649 | netdev_err(dev, "Failed to join FIDs\n"); |
| 650 | return err; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 651 | } |
| 652 | |
Ido Schimmel | 3b7ad5e | 2015-11-19 12:27:39 +0100 | [diff] [blame] | 653 | err = __mlxsw_sp_port_vlans_set(mlxsw_sp_port, vid_begin, vid_end, |
| 654 | true, flag_untagged); |
| 655 | if (err) { |
| 656 | netdev_err(dev, "Unable to add VIDs %d-%d\n", vid_begin, |
| 657 | vid_end); |
Ido Schimmel | b07a966 | 2015-11-19 12:27:40 +0100 | [diff] [blame] | 658 | goto err_port_vlans_set; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 659 | } |
| 660 | |
Ido Schimmel | b07a966 | 2015-11-19 12:27:40 +0100 | [diff] [blame] | 661 | old_pvid = mlxsw_sp_port->pvid; |
| 662 | if (flag_pvid && old_pvid != vid_begin) { |
| 663 | err = mlxsw_sp_port_pvid_set(mlxsw_sp_port, vid_begin); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 664 | if (err) { |
Ido Schimmel | b07a966 | 2015-11-19 12:27:40 +0100 | [diff] [blame] | 665 | netdev_err(dev, "Unable to add PVID %d\n", vid_begin); |
| 666 | goto err_port_pvid_set; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 667 | } |
Ido Schimmel | 28a01d2 | 2016-02-18 11:30:02 +0100 | [diff] [blame] | 668 | } else if (!flag_pvid && old_pvid >= vid_begin && old_pvid <= vid_end) { |
| 669 | err = mlxsw_sp_port_pvid_set(mlxsw_sp_port, 0); |
| 670 | if (err) { |
| 671 | netdev_err(dev, "Unable to del PVID\n"); |
| 672 | goto err_port_pvid_set; |
| 673 | } |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 674 | } |
| 675 | |
| 676 | /* Changing activity bits only if HW operation succeded */ |
Elad Raz | fc1273a | 2016-01-06 13:01:11 +0100 | [diff] [blame] | 677 | for (vid = vid_begin; vid <= vid_end; vid++) { |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 678 | set_bit(vid, mlxsw_sp_port->active_vlans); |
Elad Raz | fc1273a | 2016-01-06 13:01:11 +0100 | [diff] [blame] | 679 | if (flag_untagged) |
| 680 | set_bit(vid, mlxsw_sp_port->untagged_vlans); |
| 681 | else |
| 682 | clear_bit(vid, mlxsw_sp_port->untagged_vlans); |
| 683 | } |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 684 | |
Ido Schimmel | b07a966 | 2015-11-19 12:27:40 +0100 | [diff] [blame] | 685 | /* STP state change must be done after we set active VLANs */ |
| 686 | err = mlxsw_sp_port_stp_state_set(mlxsw_sp_port, |
| 687 | mlxsw_sp_port->stp_state); |
| 688 | if (err) { |
| 689 | netdev_err(dev, "Failed to set STP state\n"); |
| 690 | goto err_port_stp_state_set; |
| 691 | } |
| 692 | |
| 693 | return 0; |
| 694 | |
Ido Schimmel | b07a966 | 2015-11-19 12:27:40 +0100 | [diff] [blame] | 695 | err_port_stp_state_set: |
| 696 | for (vid = vid_begin; vid <= vid_end; vid++) |
| 697 | clear_bit(vid, mlxsw_sp_port->active_vlans); |
| 698 | if (old_pvid != mlxsw_sp_port->pvid) |
| 699 | mlxsw_sp_port_pvid_set(mlxsw_sp_port, old_pvid); |
| 700 | err_port_pvid_set: |
| 701 | __mlxsw_sp_port_vlans_set(mlxsw_sp_port, vid_begin, vid_end, false, |
| 702 | false); |
| 703 | err_port_vlans_set: |
Ido Schimmel | 14d3946 | 2016-06-20 23:04:15 +0200 | [diff] [blame] | 704 | mlxsw_sp_port_fid_leave(mlxsw_sp_port, vid_begin, vid_end); |
Ido Schimmel | b07a966 | 2015-11-19 12:27:40 +0100 | [diff] [blame] | 705 | return err; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 706 | } |
| 707 | |
| 708 | static int mlxsw_sp_port_vlans_add(struct mlxsw_sp_port *mlxsw_sp_port, |
| 709 | const struct switchdev_obj_port_vlan *vlan, |
| 710 | struct switchdev_trans *trans) |
| 711 | { |
Elad Raz | e4a1305 | 2016-01-06 13:01:09 +0100 | [diff] [blame] | 712 | bool flag_untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED; |
| 713 | bool flag_pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 714 | |
| 715 | if (switchdev_trans_ph_prepare(trans)) |
| 716 | return 0; |
| 717 | |
| 718 | return __mlxsw_sp_port_vlans_add(mlxsw_sp_port, |
| 719 | vlan->vid_begin, vlan->vid_end, |
Elad Raz | e4a1305 | 2016-01-06 13:01:09 +0100 | [diff] [blame] | 720 | flag_untagged, flag_pvid); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 721 | } |
| 722 | |
Jiri Pirko | 8a1ab5d | 2015-12-03 12:12:29 +0100 | [diff] [blame] | 723 | static enum mlxsw_reg_sfd_rec_policy mlxsw_sp_sfd_rec_policy(bool dynamic) |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 724 | { |
Jiri Pirko | 8a1ab5d | 2015-12-03 12:12:29 +0100 | [diff] [blame] | 725 | return dynamic ? MLXSW_REG_SFD_REC_POLICY_DYNAMIC_ENTRY_INGRESS : |
| 726 | MLXSW_REG_SFD_REC_POLICY_STATIC_ENTRY; |
| 727 | } |
| 728 | |
| 729 | static enum mlxsw_reg_sfd_op mlxsw_sp_sfd_op(bool adding) |
| 730 | { |
| 731 | return adding ? MLXSW_REG_SFD_OP_WRITE_EDIT : |
| 732 | MLXSW_REG_SFD_OP_WRITE_REMOVE; |
| 733 | } |
| 734 | |
Ido Schimmel | 6e095fd | 2016-07-04 08:23:13 +0200 | [diff] [blame] | 735 | static int __mlxsw_sp_port_fdb_uc_op(struct mlxsw_sp *mlxsw_sp, u8 local_port, |
| 736 | const char *mac, u16 fid, bool adding, |
| 737 | enum mlxsw_reg_sfd_rec_action action, |
| 738 | bool dynamic) |
Jiri Pirko | 8a1ab5d | 2015-12-03 12:12:29 +0100 | [diff] [blame] | 739 | { |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 740 | char *sfd_pl; |
| 741 | int err; |
| 742 | |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 743 | sfd_pl = kmalloc(MLXSW_REG_SFD_LEN, GFP_KERNEL); |
| 744 | if (!sfd_pl) |
| 745 | return -ENOMEM; |
| 746 | |
Jiri Pirko | 8a1ab5d | 2015-12-03 12:12:29 +0100 | [diff] [blame] | 747 | mlxsw_reg_sfd_pack(sfd_pl, mlxsw_sp_sfd_op(adding), 0); |
| 748 | mlxsw_reg_sfd_uc_pack(sfd_pl, 0, mlxsw_sp_sfd_rec_policy(dynamic), |
Ido Schimmel | 6e095fd | 2016-07-04 08:23:13 +0200 | [diff] [blame] | 749 | mac, fid, action, local_port); |
Jiri Pirko | 8a1ab5d | 2015-12-03 12:12:29 +0100 | [diff] [blame] | 750 | err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfd), sfd_pl); |
| 751 | kfree(sfd_pl); |
| 752 | |
| 753 | return err; |
| 754 | } |
| 755 | |
Ido Schimmel | 6e095fd | 2016-07-04 08:23:13 +0200 | [diff] [blame] | 756 | static int mlxsw_sp_port_fdb_uc_op(struct mlxsw_sp *mlxsw_sp, u8 local_port, |
| 757 | const char *mac, u16 fid, bool adding, |
| 758 | bool dynamic) |
| 759 | { |
| 760 | return __mlxsw_sp_port_fdb_uc_op(mlxsw_sp, local_port, mac, fid, adding, |
| 761 | MLXSW_REG_SFD_REC_ACTION_NOP, dynamic); |
| 762 | } |
| 763 | |
| 764 | int mlxsw_sp_rif_fdb_op(struct mlxsw_sp *mlxsw_sp, const char *mac, u16 fid, |
| 765 | bool adding) |
| 766 | { |
| 767 | return __mlxsw_sp_port_fdb_uc_op(mlxsw_sp, 0, mac, fid, adding, |
| 768 | MLXSW_REG_SFD_REC_ACTION_FORWARD_IP_ROUTER, |
| 769 | false); |
| 770 | } |
| 771 | |
Jiri Pirko | 8a1ab5d | 2015-12-03 12:12:29 +0100 | [diff] [blame] | 772 | static int mlxsw_sp_port_fdb_uc_lag_op(struct mlxsw_sp *mlxsw_sp, u16 lag_id, |
Ido Schimmel | 64771e3 | 2015-12-15 16:03:46 +0100 | [diff] [blame] | 773 | const char *mac, u16 fid, u16 lag_vid, |
| 774 | bool adding, bool dynamic) |
Jiri Pirko | 8a1ab5d | 2015-12-03 12:12:29 +0100 | [diff] [blame] | 775 | { |
| 776 | char *sfd_pl; |
| 777 | int err; |
| 778 | |
| 779 | sfd_pl = kmalloc(MLXSW_REG_SFD_LEN, GFP_KERNEL); |
| 780 | if (!sfd_pl) |
| 781 | return -ENOMEM; |
| 782 | |
| 783 | mlxsw_reg_sfd_pack(sfd_pl, mlxsw_sp_sfd_op(adding), 0); |
| 784 | mlxsw_reg_sfd_uc_lag_pack(sfd_pl, 0, mlxsw_sp_sfd_rec_policy(dynamic), |
Ido Schimmel | 64771e3 | 2015-12-15 16:03:46 +0100 | [diff] [blame] | 785 | mac, fid, MLXSW_REG_SFD_REC_ACTION_NOP, |
| 786 | lag_vid, lag_id); |
Jiri Pirko | 8a1ab5d | 2015-12-03 12:12:29 +0100 | [diff] [blame] | 787 | err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfd), sfd_pl); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 788 | kfree(sfd_pl); |
| 789 | |
| 790 | return err; |
| 791 | } |
| 792 | |
| 793 | static int |
| 794 | mlxsw_sp_port_fdb_static_add(struct mlxsw_sp_port *mlxsw_sp_port, |
| 795 | const struct switchdev_obj_port_fdb *fdb, |
| 796 | struct switchdev_trans *trans) |
| 797 | { |
Elad Raz | e4b6f69 | 2016-01-10 21:06:27 +0100 | [diff] [blame] | 798 | u16 fid = mlxsw_sp_port_vid_to_fid_get(mlxsw_sp_port, fdb->vid); |
Ido Schimmel | 64771e3 | 2015-12-15 16:03:46 +0100 | [diff] [blame] | 799 | u16 lag_vid = 0; |
Jiri Pirko | 8a1ab5d | 2015-12-03 12:12:29 +0100 | [diff] [blame] | 800 | |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 801 | if (switchdev_trans_ph_prepare(trans)) |
| 802 | return 0; |
| 803 | |
Ido Schimmel | 54a7320 | 2015-12-15 16:03:41 +0100 | [diff] [blame] | 804 | if (mlxsw_sp_port_is_vport(mlxsw_sp_port)) { |
Ido Schimmel | 64771e3 | 2015-12-15 16:03:46 +0100 | [diff] [blame] | 805 | lag_vid = mlxsw_sp_vport_vid_get(mlxsw_sp_port); |
Ido Schimmel | 54a7320 | 2015-12-15 16:03:41 +0100 | [diff] [blame] | 806 | } |
| 807 | |
Jiri Pirko | 8a1ab5d | 2015-12-03 12:12:29 +0100 | [diff] [blame] | 808 | if (!mlxsw_sp_port->lagged) |
Jiri Pirko | 2fa9d45 | 2016-01-07 11:50:29 +0100 | [diff] [blame] | 809 | return mlxsw_sp_port_fdb_uc_op(mlxsw_sp_port->mlxsw_sp, |
| 810 | mlxsw_sp_port->local_port, |
Ido Schimmel | 9de6a80 | 2015-12-15 16:03:40 +0100 | [diff] [blame] | 811 | fdb->addr, fid, true, false); |
Jiri Pirko | 8a1ab5d | 2015-12-03 12:12:29 +0100 | [diff] [blame] | 812 | else |
| 813 | return mlxsw_sp_port_fdb_uc_lag_op(mlxsw_sp_port->mlxsw_sp, |
| 814 | mlxsw_sp_port->lag_id, |
Ido Schimmel | 64771e3 | 2015-12-15 16:03:46 +0100 | [diff] [blame] | 815 | fdb->addr, fid, lag_vid, |
| 816 | true, false); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 817 | } |
| 818 | |
Elad Raz | 3a49b4f | 2016-01-10 21:06:28 +0100 | [diff] [blame] | 819 | static int mlxsw_sp_port_mdb_op(struct mlxsw_sp *mlxsw_sp, const char *addr, |
| 820 | u16 fid, u16 mid, bool adding) |
| 821 | { |
| 822 | char *sfd_pl; |
| 823 | int err; |
| 824 | |
| 825 | sfd_pl = kmalloc(MLXSW_REG_SFD_LEN, GFP_KERNEL); |
| 826 | if (!sfd_pl) |
| 827 | return -ENOMEM; |
| 828 | |
| 829 | mlxsw_reg_sfd_pack(sfd_pl, mlxsw_sp_sfd_op(adding), 0); |
| 830 | mlxsw_reg_sfd_mc_pack(sfd_pl, 0, addr, fid, |
| 831 | MLXSW_REG_SFD_REC_ACTION_NOP, mid); |
| 832 | err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfd), sfd_pl); |
| 833 | kfree(sfd_pl); |
| 834 | return err; |
| 835 | } |
| 836 | |
| 837 | static int mlxsw_sp_port_smid_set(struct mlxsw_sp_port *mlxsw_sp_port, u16 mid, |
| 838 | bool add, bool clear_all_ports) |
| 839 | { |
| 840 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
| 841 | char *smid_pl; |
| 842 | int err, i; |
| 843 | |
| 844 | smid_pl = kmalloc(MLXSW_REG_SMID_LEN, GFP_KERNEL); |
| 845 | if (!smid_pl) |
| 846 | return -ENOMEM; |
| 847 | |
| 848 | mlxsw_reg_smid_pack(smid_pl, mid, mlxsw_sp_port->local_port, add); |
| 849 | if (clear_all_ports) { |
| 850 | for (i = 1; i < MLXSW_PORT_MAX_PORTS; i++) |
| 851 | if (mlxsw_sp->ports[i]) |
| 852 | mlxsw_reg_smid_port_mask_set(smid_pl, i, 1); |
| 853 | } |
| 854 | err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(smid), smid_pl); |
| 855 | kfree(smid_pl); |
| 856 | return err; |
| 857 | } |
| 858 | |
| 859 | static struct mlxsw_sp_mid *__mlxsw_sp_mc_get(struct mlxsw_sp *mlxsw_sp, |
| 860 | const unsigned char *addr, |
| 861 | u16 vid) |
| 862 | { |
| 863 | struct mlxsw_sp_mid *mid; |
| 864 | |
| 865 | list_for_each_entry(mid, &mlxsw_sp->br_mids.list, list) { |
| 866 | if (ether_addr_equal(mid->addr, addr) && mid->vid == vid) |
| 867 | return mid; |
| 868 | } |
| 869 | return NULL; |
| 870 | } |
| 871 | |
| 872 | static struct mlxsw_sp_mid *__mlxsw_sp_mc_alloc(struct mlxsw_sp *mlxsw_sp, |
| 873 | const unsigned char *addr, |
| 874 | u16 vid) |
| 875 | { |
| 876 | struct mlxsw_sp_mid *mid; |
| 877 | u16 mid_idx; |
| 878 | |
| 879 | mid_idx = find_first_zero_bit(mlxsw_sp->br_mids.mapped, |
| 880 | MLXSW_SP_MID_MAX); |
| 881 | if (mid_idx == MLXSW_SP_MID_MAX) |
| 882 | return NULL; |
| 883 | |
| 884 | mid = kzalloc(sizeof(*mid), GFP_KERNEL); |
| 885 | if (!mid) |
| 886 | return NULL; |
| 887 | |
| 888 | set_bit(mid_idx, mlxsw_sp->br_mids.mapped); |
| 889 | ether_addr_copy(mid->addr, addr); |
| 890 | mid->vid = vid; |
| 891 | mid->mid = mid_idx; |
| 892 | mid->ref_count = 0; |
| 893 | list_add_tail(&mid->list, &mlxsw_sp->br_mids.list); |
| 894 | |
| 895 | return mid; |
| 896 | } |
| 897 | |
| 898 | static int __mlxsw_sp_mc_dec_ref(struct mlxsw_sp *mlxsw_sp, |
| 899 | struct mlxsw_sp_mid *mid) |
| 900 | { |
| 901 | if (--mid->ref_count == 0) { |
| 902 | list_del(&mid->list); |
| 903 | clear_bit(mid->mid, mlxsw_sp->br_mids.mapped); |
| 904 | kfree(mid); |
| 905 | return 1; |
| 906 | } |
| 907 | return 0; |
| 908 | } |
| 909 | |
| 910 | static int mlxsw_sp_port_mdb_add(struct mlxsw_sp_port *mlxsw_sp_port, |
| 911 | const struct switchdev_obj_port_mdb *mdb, |
| 912 | struct switchdev_trans *trans) |
| 913 | { |
| 914 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
| 915 | struct net_device *dev = mlxsw_sp_port->dev; |
| 916 | struct mlxsw_sp_mid *mid; |
| 917 | u16 fid = mlxsw_sp_port_vid_to_fid_get(mlxsw_sp_port, mdb->vid); |
| 918 | int err = 0; |
| 919 | |
| 920 | if (switchdev_trans_ph_prepare(trans)) |
| 921 | return 0; |
| 922 | |
| 923 | mid = __mlxsw_sp_mc_get(mlxsw_sp, mdb->addr, mdb->vid); |
| 924 | if (!mid) { |
| 925 | mid = __mlxsw_sp_mc_alloc(mlxsw_sp, mdb->addr, mdb->vid); |
| 926 | if (!mid) { |
| 927 | netdev_err(dev, "Unable to allocate MC group\n"); |
| 928 | return -ENOMEM; |
| 929 | } |
| 930 | } |
| 931 | mid->ref_count++; |
| 932 | |
| 933 | err = mlxsw_sp_port_smid_set(mlxsw_sp_port, mid->mid, true, |
| 934 | mid->ref_count == 1); |
| 935 | if (err) { |
| 936 | netdev_err(dev, "Unable to set SMID\n"); |
| 937 | goto err_out; |
| 938 | } |
| 939 | |
| 940 | if (mid->ref_count == 1) { |
| 941 | err = mlxsw_sp_port_mdb_op(mlxsw_sp, mdb->addr, fid, mid->mid, |
| 942 | true); |
| 943 | if (err) { |
| 944 | netdev_err(dev, "Unable to set MC SFD\n"); |
| 945 | goto err_out; |
| 946 | } |
| 947 | } |
| 948 | |
| 949 | return 0; |
| 950 | |
| 951 | err_out: |
| 952 | __mlxsw_sp_mc_dec_ref(mlxsw_sp, mid); |
| 953 | return err; |
| 954 | } |
| 955 | |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 956 | static int mlxsw_sp_port_obj_add(struct net_device *dev, |
| 957 | const struct switchdev_obj *obj, |
| 958 | struct switchdev_trans *trans) |
| 959 | { |
| 960 | struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev); |
| 961 | int err = 0; |
| 962 | |
Ido Schimmel | 54a7320 | 2015-12-15 16:03:41 +0100 | [diff] [blame] | 963 | mlxsw_sp_port = mlxsw_sp_port_orig_get(obj->orig_dev, mlxsw_sp_port); |
| 964 | if (!mlxsw_sp_port) |
| 965 | return -EINVAL; |
| 966 | |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 967 | switch (obj->id) { |
| 968 | case SWITCHDEV_OBJ_ID_PORT_VLAN: |
Ido Schimmel | 54a7320 | 2015-12-15 16:03:41 +0100 | [diff] [blame] | 969 | if (mlxsw_sp_port_is_vport(mlxsw_sp_port)) |
| 970 | return 0; |
| 971 | |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 972 | err = mlxsw_sp_port_vlans_add(mlxsw_sp_port, |
| 973 | SWITCHDEV_OBJ_PORT_VLAN(obj), |
| 974 | trans); |
| 975 | break; |
Jiri Pirko | 61c503f | 2016-07-04 08:23:11 +0200 | [diff] [blame] | 976 | case SWITCHDEV_OBJ_ID_IPV4_FIB: |
| 977 | err = mlxsw_sp_router_fib4_add(mlxsw_sp_port, |
| 978 | SWITCHDEV_OBJ_IPV4_FIB(obj), |
| 979 | trans); |
| 980 | break; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 981 | case SWITCHDEV_OBJ_ID_PORT_FDB: |
| 982 | err = mlxsw_sp_port_fdb_static_add(mlxsw_sp_port, |
| 983 | SWITCHDEV_OBJ_PORT_FDB(obj), |
| 984 | trans); |
| 985 | break; |
Elad Raz | 3a49b4f | 2016-01-10 21:06:28 +0100 | [diff] [blame] | 986 | case SWITCHDEV_OBJ_ID_PORT_MDB: |
| 987 | err = mlxsw_sp_port_mdb_add(mlxsw_sp_port, |
| 988 | SWITCHDEV_OBJ_PORT_MDB(obj), |
| 989 | trans); |
| 990 | break; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 991 | default: |
| 992 | err = -EOPNOTSUPP; |
| 993 | break; |
| 994 | } |
| 995 | |
| 996 | return err; |
| 997 | } |
| 998 | |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 999 | static int __mlxsw_sp_port_vlans_del(struct mlxsw_sp_port *mlxsw_sp_port, |
| 1000 | u16 vid_begin, u16 vid_end, bool init) |
| 1001 | { |
| 1002 | struct net_device *dev = mlxsw_sp_port->dev; |
Ido Schimmel | 3b7ad5e | 2015-11-19 12:27:39 +0100 | [diff] [blame] | 1003 | u16 vid, pvid; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1004 | int err; |
| 1005 | |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1006 | if (!init && !mlxsw_sp_port->bridged) |
Ido Schimmel | 32d863f | 2016-07-02 11:00:10 +0200 | [diff] [blame] | 1007 | return -EINVAL; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1008 | |
Ido Schimmel | 3b7ad5e | 2015-11-19 12:27:39 +0100 | [diff] [blame] | 1009 | err = __mlxsw_sp_port_vlans_set(mlxsw_sp_port, vid_begin, vid_end, |
| 1010 | false, false); |
| 1011 | if (err) { |
| 1012 | netdev_err(dev, "Unable to del VIDs %d-%d\n", vid_begin, |
| 1013 | vid_end); |
| 1014 | return err; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1015 | } |
| 1016 | |
Ido Schimmel | 28a01d2 | 2016-02-18 11:30:02 +0100 | [diff] [blame] | 1017 | if (init) |
| 1018 | goto out; |
| 1019 | |
Ido Schimmel | 06c071f | 2015-11-19 12:27:38 +0100 | [diff] [blame] | 1020 | pvid = mlxsw_sp_port->pvid; |
Ido Schimmel | 28a01d2 | 2016-02-18 11:30:02 +0100 | [diff] [blame] | 1021 | if (pvid >= vid_begin && pvid <= vid_end) { |
| 1022 | err = mlxsw_sp_port_pvid_set(mlxsw_sp_port, 0); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1023 | if (err) { |
Ido Schimmel | 06c071f | 2015-11-19 12:27:38 +0100 | [diff] [blame] | 1024 | netdev_err(dev, "Unable to del PVID %d\n", pvid); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1025 | return err; |
| 1026 | } |
| 1027 | } |
| 1028 | |
Ido Schimmel | 14d3946 | 2016-06-20 23:04:15 +0200 | [diff] [blame] | 1029 | mlxsw_sp_port_fid_leave(mlxsw_sp_port, vid_begin, vid_end); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1030 | |
| 1031 | out: |
| 1032 | /* Changing activity bits only if HW operation succeded */ |
| 1033 | for (vid = vid_begin; vid <= vid_end; vid++) |
| 1034 | clear_bit(vid, mlxsw_sp_port->active_vlans); |
| 1035 | |
| 1036 | return 0; |
| 1037 | } |
| 1038 | |
| 1039 | static int mlxsw_sp_port_vlans_del(struct mlxsw_sp_port *mlxsw_sp_port, |
| 1040 | const struct switchdev_obj_port_vlan *vlan) |
| 1041 | { |
| 1042 | return __mlxsw_sp_port_vlans_del(mlxsw_sp_port, |
| 1043 | vlan->vid_begin, vlan->vid_end, false); |
| 1044 | } |
| 1045 | |
Ido Schimmel | 4dc236c | 2016-01-27 15:20:16 +0100 | [diff] [blame] | 1046 | void mlxsw_sp_port_active_vlans_del(struct mlxsw_sp_port *mlxsw_sp_port) |
| 1047 | { |
| 1048 | u16 vid; |
| 1049 | |
| 1050 | for_each_set_bit(vid, mlxsw_sp_port->active_vlans, VLAN_N_VID) |
| 1051 | __mlxsw_sp_port_vlans_del(mlxsw_sp_port, vid, vid, false); |
| 1052 | } |
| 1053 | |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1054 | static int |
| 1055 | mlxsw_sp_port_fdb_static_del(struct mlxsw_sp_port *mlxsw_sp_port, |
| 1056 | const struct switchdev_obj_port_fdb *fdb) |
| 1057 | { |
Elad Raz | e4b6f69 | 2016-01-10 21:06:27 +0100 | [diff] [blame] | 1058 | u16 fid = mlxsw_sp_port_vid_to_fid_get(mlxsw_sp_port, fdb->vid); |
Ido Schimmel | 64771e3 | 2015-12-15 16:03:46 +0100 | [diff] [blame] | 1059 | u16 lag_vid = 0; |
Ido Schimmel | 9de6a80 | 2015-12-15 16:03:40 +0100 | [diff] [blame] | 1060 | |
Ido Schimmel | 54a7320 | 2015-12-15 16:03:41 +0100 | [diff] [blame] | 1061 | if (mlxsw_sp_port_is_vport(mlxsw_sp_port)) { |
Ido Schimmel | 64771e3 | 2015-12-15 16:03:46 +0100 | [diff] [blame] | 1062 | lag_vid = mlxsw_sp_vport_vid_get(mlxsw_sp_port); |
Ido Schimmel | 54a7320 | 2015-12-15 16:03:41 +0100 | [diff] [blame] | 1063 | } |
| 1064 | |
Jiri Pirko | 8a1ab5d | 2015-12-03 12:12:29 +0100 | [diff] [blame] | 1065 | if (!mlxsw_sp_port->lagged) |
Jiri Pirko | 2fa9d45 | 2016-01-07 11:50:29 +0100 | [diff] [blame] | 1066 | return mlxsw_sp_port_fdb_uc_op(mlxsw_sp_port->mlxsw_sp, |
| 1067 | mlxsw_sp_port->local_port, |
Ido Schimmel | 9de6a80 | 2015-12-15 16:03:40 +0100 | [diff] [blame] | 1068 | fdb->addr, fid, |
Jiri Pirko | 8a1ab5d | 2015-12-03 12:12:29 +0100 | [diff] [blame] | 1069 | false, false); |
| 1070 | else |
| 1071 | return mlxsw_sp_port_fdb_uc_lag_op(mlxsw_sp_port->mlxsw_sp, |
| 1072 | mlxsw_sp_port->lag_id, |
Ido Schimmel | 64771e3 | 2015-12-15 16:03:46 +0100 | [diff] [blame] | 1073 | fdb->addr, fid, lag_vid, |
Jiri Pirko | 8a1ab5d | 2015-12-03 12:12:29 +0100 | [diff] [blame] | 1074 | false, false); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1075 | } |
| 1076 | |
Elad Raz | 3a49b4f | 2016-01-10 21:06:28 +0100 | [diff] [blame] | 1077 | static int mlxsw_sp_port_mdb_del(struct mlxsw_sp_port *mlxsw_sp_port, |
| 1078 | const struct switchdev_obj_port_mdb *mdb) |
| 1079 | { |
| 1080 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
| 1081 | struct net_device *dev = mlxsw_sp_port->dev; |
| 1082 | struct mlxsw_sp_mid *mid; |
| 1083 | u16 fid = mlxsw_sp_port_vid_to_fid_get(mlxsw_sp_port, mdb->vid); |
| 1084 | u16 mid_idx; |
| 1085 | int err = 0; |
| 1086 | |
| 1087 | mid = __mlxsw_sp_mc_get(mlxsw_sp, mdb->addr, mdb->vid); |
| 1088 | if (!mid) { |
| 1089 | netdev_err(dev, "Unable to remove port from MC DB\n"); |
| 1090 | return -EINVAL; |
| 1091 | } |
| 1092 | |
| 1093 | err = mlxsw_sp_port_smid_set(mlxsw_sp_port, mid->mid, false, false); |
| 1094 | if (err) |
| 1095 | netdev_err(dev, "Unable to remove port from SMID\n"); |
| 1096 | |
| 1097 | mid_idx = mid->mid; |
| 1098 | if (__mlxsw_sp_mc_dec_ref(mlxsw_sp, mid)) { |
| 1099 | err = mlxsw_sp_port_mdb_op(mlxsw_sp, mdb->addr, fid, mid_idx, |
| 1100 | false); |
| 1101 | if (err) |
| 1102 | netdev_err(dev, "Unable to remove MC SFD\n"); |
| 1103 | } |
| 1104 | |
| 1105 | return err; |
| 1106 | } |
| 1107 | |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1108 | static int mlxsw_sp_port_obj_del(struct net_device *dev, |
| 1109 | const struct switchdev_obj *obj) |
| 1110 | { |
| 1111 | struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev); |
| 1112 | int err = 0; |
| 1113 | |
Ido Schimmel | 54a7320 | 2015-12-15 16:03:41 +0100 | [diff] [blame] | 1114 | mlxsw_sp_port = mlxsw_sp_port_orig_get(obj->orig_dev, mlxsw_sp_port); |
| 1115 | if (!mlxsw_sp_port) |
| 1116 | return -EINVAL; |
| 1117 | |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1118 | switch (obj->id) { |
| 1119 | case SWITCHDEV_OBJ_ID_PORT_VLAN: |
Ido Schimmel | 54a7320 | 2015-12-15 16:03:41 +0100 | [diff] [blame] | 1120 | if (mlxsw_sp_port_is_vport(mlxsw_sp_port)) |
| 1121 | return 0; |
| 1122 | |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1123 | err = mlxsw_sp_port_vlans_del(mlxsw_sp_port, |
| 1124 | SWITCHDEV_OBJ_PORT_VLAN(obj)); |
| 1125 | break; |
Jiri Pirko | 61c503f | 2016-07-04 08:23:11 +0200 | [diff] [blame] | 1126 | case SWITCHDEV_OBJ_ID_IPV4_FIB: |
| 1127 | err = mlxsw_sp_router_fib4_del(mlxsw_sp_port, |
| 1128 | SWITCHDEV_OBJ_IPV4_FIB(obj)); |
| 1129 | break; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1130 | case SWITCHDEV_OBJ_ID_PORT_FDB: |
| 1131 | err = mlxsw_sp_port_fdb_static_del(mlxsw_sp_port, |
| 1132 | SWITCHDEV_OBJ_PORT_FDB(obj)); |
| 1133 | break; |
Elad Raz | 3a49b4f | 2016-01-10 21:06:28 +0100 | [diff] [blame] | 1134 | case SWITCHDEV_OBJ_ID_PORT_MDB: |
| 1135 | err = mlxsw_sp_port_mdb_del(mlxsw_sp_port, |
| 1136 | SWITCHDEV_OBJ_PORT_MDB(obj)); |
Dan Carpenter | 00ae40e | 2016-01-13 15:28:23 +0300 | [diff] [blame] | 1137 | break; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1138 | default: |
| 1139 | err = -EOPNOTSUPP; |
| 1140 | break; |
| 1141 | } |
| 1142 | |
| 1143 | return err; |
| 1144 | } |
| 1145 | |
Jiri Pirko | 8a1ab5d | 2015-12-03 12:12:29 +0100 | [diff] [blame] | 1146 | static struct mlxsw_sp_port *mlxsw_sp_lag_rep_port(struct mlxsw_sp *mlxsw_sp, |
| 1147 | u16 lag_id) |
| 1148 | { |
| 1149 | struct mlxsw_sp_port *mlxsw_sp_port; |
| 1150 | int i; |
| 1151 | |
| 1152 | for (i = 0; i < MLXSW_SP_PORT_PER_LAG_MAX; i++) { |
| 1153 | mlxsw_sp_port = mlxsw_sp_port_lagged_get(mlxsw_sp, lag_id, i); |
| 1154 | if (mlxsw_sp_port) |
| 1155 | return mlxsw_sp_port; |
| 1156 | } |
| 1157 | return NULL; |
| 1158 | } |
| 1159 | |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1160 | static int mlxsw_sp_port_fdb_dump(struct mlxsw_sp_port *mlxsw_sp_port, |
| 1161 | struct switchdev_obj_port_fdb *fdb, |
Ido Schimmel | 304f515 | 2016-01-27 15:20:24 +0100 | [diff] [blame] | 1162 | switchdev_obj_dump_cb_t *cb, |
| 1163 | struct net_device *orig_dev) |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1164 | { |
Jiri Pirko | 8a1ab5d | 2015-12-03 12:12:29 +0100 | [diff] [blame] | 1165 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
Ido Schimmel | 3f47f86 | 2016-01-27 15:20:25 +0100 | [diff] [blame] | 1166 | struct mlxsw_sp_port *tmp; |
Ido Schimmel | 56918b6 | 2016-06-20 23:04:18 +0200 | [diff] [blame] | 1167 | struct mlxsw_sp_fid *f; |
| 1168 | u16 vport_fid; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1169 | char *sfd_pl; |
| 1170 | char mac[ETH_ALEN]; |
Ido Schimmel | 9de6a80 | 2015-12-15 16:03:40 +0100 | [diff] [blame] | 1171 | u16 fid; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1172 | u8 local_port; |
Jiri Pirko | 8a1ab5d | 2015-12-03 12:12:29 +0100 | [diff] [blame] | 1173 | u16 lag_id; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1174 | u8 num_rec; |
| 1175 | int stored_err = 0; |
| 1176 | int i; |
| 1177 | int err; |
| 1178 | |
| 1179 | sfd_pl = kmalloc(MLXSW_REG_SFD_LEN, GFP_KERNEL); |
| 1180 | if (!sfd_pl) |
| 1181 | return -ENOMEM; |
| 1182 | |
Ido Schimmel | 56918b6 | 2016-06-20 23:04:18 +0200 | [diff] [blame] | 1183 | f = mlxsw_sp_vport_fid_get(mlxsw_sp_port); |
| 1184 | vport_fid = f ? f->fid : 0; |
Ido Schimmel | 54a7320 | 2015-12-15 16:03:41 +0100 | [diff] [blame] | 1185 | |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1186 | mlxsw_reg_sfd_pack(sfd_pl, MLXSW_REG_SFD_OP_QUERY_DUMP, 0); |
| 1187 | do { |
| 1188 | mlxsw_reg_sfd_num_rec_set(sfd_pl, MLXSW_REG_SFD_REC_MAX_COUNT); |
Jiri Pirko | 8a1ab5d | 2015-12-03 12:12:29 +0100 | [diff] [blame] | 1189 | err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(sfd), sfd_pl); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1190 | if (err) |
| 1191 | goto out; |
| 1192 | |
| 1193 | num_rec = mlxsw_reg_sfd_num_rec_get(sfd_pl); |
| 1194 | |
| 1195 | /* Even in case of error, we have to run the dump to the end |
| 1196 | * so the session in firmware is finished. |
| 1197 | */ |
| 1198 | if (stored_err) |
| 1199 | continue; |
| 1200 | |
| 1201 | for (i = 0; i < num_rec; i++) { |
| 1202 | switch (mlxsw_reg_sfd_rec_type_get(sfd_pl, i)) { |
| 1203 | case MLXSW_REG_SFD_REC_TYPE_UNICAST: |
Ido Schimmel | 9de6a80 | 2015-12-15 16:03:40 +0100 | [diff] [blame] | 1204 | mlxsw_reg_sfd_uc_unpack(sfd_pl, i, mac, &fid, |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1205 | &local_port); |
| 1206 | if (local_port == mlxsw_sp_port->local_port) { |
Ido Schimmel | 004f85e | 2016-01-27 15:20:22 +0100 | [diff] [blame] | 1207 | if (vport_fid && vport_fid == fid) |
| 1208 | fdb->vid = 0; |
| 1209 | else if (!vport_fid && |
| 1210 | !mlxsw_sp_fid_is_vfid(fid)) |
Ido Schimmel | 54a7320 | 2015-12-15 16:03:41 +0100 | [diff] [blame] | 1211 | fdb->vid = fid; |
Ido Schimmel | 004f85e | 2016-01-27 15:20:22 +0100 | [diff] [blame] | 1212 | else |
| 1213 | continue; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1214 | ether_addr_copy(fdb->addr, mac); |
| 1215 | fdb->ndm_state = NUD_REACHABLE; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1216 | err = cb(&fdb->obj); |
| 1217 | if (err) |
| 1218 | stored_err = err; |
| 1219 | } |
Jiri Pirko | 8a1ab5d | 2015-12-03 12:12:29 +0100 | [diff] [blame] | 1220 | break; |
| 1221 | case MLXSW_REG_SFD_REC_TYPE_UNICAST_LAG: |
| 1222 | mlxsw_reg_sfd_uc_lag_unpack(sfd_pl, i, |
Ido Schimmel | 9de6a80 | 2015-12-15 16:03:40 +0100 | [diff] [blame] | 1223 | mac, &fid, &lag_id); |
Ido Schimmel | 3f47f86 | 2016-01-27 15:20:25 +0100 | [diff] [blame] | 1224 | tmp = mlxsw_sp_lag_rep_port(mlxsw_sp, lag_id); |
| 1225 | if (tmp && tmp->local_port == |
| 1226 | mlxsw_sp_port->local_port) { |
Ido Schimmel | 304f515 | 2016-01-27 15:20:24 +0100 | [diff] [blame] | 1227 | /* LAG records can only point to LAG |
| 1228 | * devices or VLAN devices on top. |
| 1229 | */ |
| 1230 | if (!netif_is_lag_master(orig_dev) && |
| 1231 | !is_vlan_dev(orig_dev)) |
| 1232 | continue; |
Ido Schimmel | 004f85e | 2016-01-27 15:20:22 +0100 | [diff] [blame] | 1233 | if (vport_fid && vport_fid == fid) |
| 1234 | fdb->vid = 0; |
| 1235 | else if (!vport_fid && |
| 1236 | !mlxsw_sp_fid_is_vfid(fid)) |
Ido Schimmel | 54a7320 | 2015-12-15 16:03:41 +0100 | [diff] [blame] | 1237 | fdb->vid = fid; |
Ido Schimmel | 004f85e | 2016-01-27 15:20:22 +0100 | [diff] [blame] | 1238 | else |
| 1239 | continue; |
Jiri Pirko | 8a1ab5d | 2015-12-03 12:12:29 +0100 | [diff] [blame] | 1240 | ether_addr_copy(fdb->addr, mac); |
| 1241 | fdb->ndm_state = NUD_REACHABLE; |
Jiri Pirko | 8a1ab5d | 2015-12-03 12:12:29 +0100 | [diff] [blame] | 1242 | err = cb(&fdb->obj); |
| 1243 | if (err) |
| 1244 | stored_err = err; |
| 1245 | } |
| 1246 | break; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1247 | } |
| 1248 | } |
| 1249 | } while (num_rec == MLXSW_REG_SFD_REC_MAX_COUNT); |
| 1250 | |
| 1251 | out: |
| 1252 | kfree(sfd_pl); |
| 1253 | return stored_err ? stored_err : err; |
| 1254 | } |
| 1255 | |
| 1256 | static int mlxsw_sp_port_vlan_dump(struct mlxsw_sp_port *mlxsw_sp_port, |
| 1257 | struct switchdev_obj_port_vlan *vlan, |
| 1258 | switchdev_obj_dump_cb_t *cb) |
| 1259 | { |
| 1260 | u16 vid; |
| 1261 | int err = 0; |
| 1262 | |
Ido Schimmel | 54a7320 | 2015-12-15 16:03:41 +0100 | [diff] [blame] | 1263 | if (mlxsw_sp_port_is_vport(mlxsw_sp_port)) { |
| 1264 | vlan->flags = 0; |
| 1265 | vlan->vid_begin = mlxsw_sp_vport_vid_get(mlxsw_sp_port); |
| 1266 | vlan->vid_end = mlxsw_sp_vport_vid_get(mlxsw_sp_port); |
| 1267 | return cb(&vlan->obj); |
| 1268 | } |
| 1269 | |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1270 | for_each_set_bit(vid, mlxsw_sp_port->active_vlans, VLAN_N_VID) { |
| 1271 | vlan->flags = 0; |
| 1272 | if (vid == mlxsw_sp_port->pvid) |
| 1273 | vlan->flags |= BRIDGE_VLAN_INFO_PVID; |
Elad Raz | fc1273a | 2016-01-06 13:01:11 +0100 | [diff] [blame] | 1274 | if (test_bit(vid, mlxsw_sp_port->untagged_vlans)) |
| 1275 | vlan->flags |= BRIDGE_VLAN_INFO_UNTAGGED; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1276 | vlan->vid_begin = vid; |
| 1277 | vlan->vid_end = vid; |
| 1278 | err = cb(&vlan->obj); |
| 1279 | if (err) |
| 1280 | break; |
| 1281 | } |
| 1282 | return err; |
| 1283 | } |
| 1284 | |
| 1285 | static int mlxsw_sp_port_obj_dump(struct net_device *dev, |
| 1286 | struct switchdev_obj *obj, |
| 1287 | switchdev_obj_dump_cb_t *cb) |
| 1288 | { |
| 1289 | struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev); |
| 1290 | int err = 0; |
| 1291 | |
Ido Schimmel | 54a7320 | 2015-12-15 16:03:41 +0100 | [diff] [blame] | 1292 | mlxsw_sp_port = mlxsw_sp_port_orig_get(obj->orig_dev, mlxsw_sp_port); |
| 1293 | if (!mlxsw_sp_port) |
| 1294 | return -EINVAL; |
| 1295 | |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1296 | switch (obj->id) { |
| 1297 | case SWITCHDEV_OBJ_ID_PORT_VLAN: |
| 1298 | err = mlxsw_sp_port_vlan_dump(mlxsw_sp_port, |
| 1299 | SWITCHDEV_OBJ_PORT_VLAN(obj), cb); |
| 1300 | break; |
| 1301 | case SWITCHDEV_OBJ_ID_PORT_FDB: |
| 1302 | err = mlxsw_sp_port_fdb_dump(mlxsw_sp_port, |
Ido Schimmel | 304f515 | 2016-01-27 15:20:24 +0100 | [diff] [blame] | 1303 | SWITCHDEV_OBJ_PORT_FDB(obj), cb, |
| 1304 | obj->orig_dev); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1305 | break; |
| 1306 | default: |
| 1307 | err = -EOPNOTSUPP; |
| 1308 | break; |
| 1309 | } |
| 1310 | |
| 1311 | return err; |
| 1312 | } |
| 1313 | |
Jiri Pirko | c7070fc | 2015-10-28 10:17:05 +0100 | [diff] [blame] | 1314 | static const struct switchdev_ops mlxsw_sp_port_switchdev_ops = { |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1315 | .switchdev_port_attr_get = mlxsw_sp_port_attr_get, |
| 1316 | .switchdev_port_attr_set = mlxsw_sp_port_attr_set, |
| 1317 | .switchdev_port_obj_add = mlxsw_sp_port_obj_add, |
| 1318 | .switchdev_port_obj_del = mlxsw_sp_port_obj_del, |
| 1319 | .switchdev_port_obj_dump = mlxsw_sp_port_obj_dump, |
| 1320 | }; |
| 1321 | |
Ido Schimmel | 45827d7 | 2016-01-27 15:20:21 +0100 | [diff] [blame] | 1322 | static void mlxsw_sp_fdb_call_notifiers(bool learning_sync, bool adding, |
| 1323 | char *mac, u16 vid, |
Jiri Pirko | 8a1ab5d | 2015-12-03 12:12:29 +0100 | [diff] [blame] | 1324 | struct net_device *dev) |
| 1325 | { |
| 1326 | struct switchdev_notifier_fdb_info info; |
| 1327 | unsigned long notifier_type; |
| 1328 | |
Ido Schimmel | 45827d7 | 2016-01-27 15:20:21 +0100 | [diff] [blame] | 1329 | if (learning_sync) { |
Jiri Pirko | 8a1ab5d | 2015-12-03 12:12:29 +0100 | [diff] [blame] | 1330 | info.addr = mac; |
| 1331 | info.vid = vid; |
| 1332 | notifier_type = adding ? SWITCHDEV_FDB_ADD : SWITCHDEV_FDB_DEL; |
| 1333 | call_switchdev_notifiers(notifier_type, dev, &info.info); |
| 1334 | } |
| 1335 | } |
| 1336 | |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1337 | static void mlxsw_sp_fdb_notify_mac_process(struct mlxsw_sp *mlxsw_sp, |
| 1338 | char *sfn_pl, int rec_index, |
| 1339 | bool adding) |
| 1340 | { |
| 1341 | struct mlxsw_sp_port *mlxsw_sp_port; |
| 1342 | char mac[ETH_ALEN]; |
| 1343 | u8 local_port; |
Ido Schimmel | 9de6a80 | 2015-12-15 16:03:40 +0100 | [diff] [blame] | 1344 | u16 vid, fid; |
Jiri Pirko | 12f1501 | 2016-01-07 11:50:30 +0100 | [diff] [blame] | 1345 | bool do_notification = true; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1346 | int err; |
| 1347 | |
Ido Schimmel | 9de6a80 | 2015-12-15 16:03:40 +0100 | [diff] [blame] | 1348 | mlxsw_reg_sfn_mac_unpack(sfn_pl, rec_index, mac, &fid, &local_port); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1349 | mlxsw_sp_port = mlxsw_sp->ports[local_port]; |
| 1350 | if (!mlxsw_sp_port) { |
| 1351 | dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Incorrect local port in FDB notification\n"); |
Jiri Pirko | 12f1501 | 2016-01-07 11:50:30 +0100 | [diff] [blame] | 1352 | goto just_remove; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1353 | } |
| 1354 | |
Ido Schimmel | aac78a4 | 2015-12-15 16:03:42 +0100 | [diff] [blame] | 1355 | if (mlxsw_sp_fid_is_vfid(fid)) { |
Ido Schimmel | aac78a4 | 2015-12-15 16:03:42 +0100 | [diff] [blame] | 1356 | struct mlxsw_sp_port *mlxsw_sp_vport; |
| 1357 | |
Ido Schimmel | d0ec875 | 2016-06-20 23:04:12 +0200 | [diff] [blame] | 1358 | mlxsw_sp_vport = mlxsw_sp_port_vport_find_by_fid(mlxsw_sp_port, |
| 1359 | fid); |
Ido Schimmel | aac78a4 | 2015-12-15 16:03:42 +0100 | [diff] [blame] | 1360 | if (!mlxsw_sp_vport) { |
| 1361 | netdev_err(mlxsw_sp_port->dev, "Failed to find a matching vPort following FDB notification\n"); |
Jiri Pirko | 12f1501 | 2016-01-07 11:50:30 +0100 | [diff] [blame] | 1362 | goto just_remove; |
Ido Schimmel | aac78a4 | 2015-12-15 16:03:42 +0100 | [diff] [blame] | 1363 | } |
Ido Schimmel | 004f85e | 2016-01-27 15:20:22 +0100 | [diff] [blame] | 1364 | vid = 0; |
Ido Schimmel | aac78a4 | 2015-12-15 16:03:42 +0100 | [diff] [blame] | 1365 | /* Override the physical port with the vPort. */ |
| 1366 | mlxsw_sp_port = mlxsw_sp_vport; |
| 1367 | } else { |
| 1368 | vid = fid; |
| 1369 | } |
| 1370 | |
Jiri Pirko | 12f1501 | 2016-01-07 11:50:30 +0100 | [diff] [blame] | 1371 | adding = adding && mlxsw_sp_port->learning; |
| 1372 | |
| 1373 | do_fdb_op: |
Jiri Pirko | 2fa9d45 | 2016-01-07 11:50:29 +0100 | [diff] [blame] | 1374 | err = mlxsw_sp_port_fdb_uc_op(mlxsw_sp, local_port, mac, fid, |
Jiri Pirko | 12f1501 | 2016-01-07 11:50:30 +0100 | [diff] [blame] | 1375 | adding, true); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1376 | if (err) { |
| 1377 | if (net_ratelimit()) |
| 1378 | netdev_err(mlxsw_sp_port->dev, "Failed to set FDB entry\n"); |
| 1379 | return; |
| 1380 | } |
| 1381 | |
Jiri Pirko | 12f1501 | 2016-01-07 11:50:30 +0100 | [diff] [blame] | 1382 | if (!do_notification) |
| 1383 | return; |
Ido Schimmel | 45827d7 | 2016-01-27 15:20:21 +0100 | [diff] [blame] | 1384 | mlxsw_sp_fdb_call_notifiers(mlxsw_sp_port->learning_sync, |
Jiri Pirko | 8a1ab5d | 2015-12-03 12:12:29 +0100 | [diff] [blame] | 1385 | adding, mac, vid, mlxsw_sp_port->dev); |
Jiri Pirko | 12f1501 | 2016-01-07 11:50:30 +0100 | [diff] [blame] | 1386 | return; |
| 1387 | |
| 1388 | just_remove: |
| 1389 | adding = false; |
| 1390 | do_notification = false; |
| 1391 | goto do_fdb_op; |
Jiri Pirko | 8a1ab5d | 2015-12-03 12:12:29 +0100 | [diff] [blame] | 1392 | } |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1393 | |
Jiri Pirko | 8a1ab5d | 2015-12-03 12:12:29 +0100 | [diff] [blame] | 1394 | static void mlxsw_sp_fdb_notify_mac_lag_process(struct mlxsw_sp *mlxsw_sp, |
| 1395 | char *sfn_pl, int rec_index, |
| 1396 | bool adding) |
| 1397 | { |
| 1398 | struct mlxsw_sp_port *mlxsw_sp_port; |
Ido Schimmel | e43aca2 | 2016-01-27 15:20:23 +0100 | [diff] [blame] | 1399 | struct net_device *dev; |
Jiri Pirko | 8a1ab5d | 2015-12-03 12:12:29 +0100 | [diff] [blame] | 1400 | char mac[ETH_ALEN]; |
Ido Schimmel | 64771e3 | 2015-12-15 16:03:46 +0100 | [diff] [blame] | 1401 | u16 lag_vid = 0; |
Jiri Pirko | 8a1ab5d | 2015-12-03 12:12:29 +0100 | [diff] [blame] | 1402 | u16 lag_id; |
Ido Schimmel | 9de6a80 | 2015-12-15 16:03:40 +0100 | [diff] [blame] | 1403 | u16 vid, fid; |
Jiri Pirko | 12f1501 | 2016-01-07 11:50:30 +0100 | [diff] [blame] | 1404 | bool do_notification = true; |
Jiri Pirko | 8a1ab5d | 2015-12-03 12:12:29 +0100 | [diff] [blame] | 1405 | int err; |
| 1406 | |
Ido Schimmel | 9de6a80 | 2015-12-15 16:03:40 +0100 | [diff] [blame] | 1407 | mlxsw_reg_sfn_mac_lag_unpack(sfn_pl, rec_index, mac, &fid, &lag_id); |
Jiri Pirko | 8a1ab5d | 2015-12-03 12:12:29 +0100 | [diff] [blame] | 1408 | mlxsw_sp_port = mlxsw_sp_lag_rep_port(mlxsw_sp, lag_id); |
| 1409 | if (!mlxsw_sp_port) { |
| 1410 | dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Cannot find port representor for LAG\n"); |
Jiri Pirko | 12f1501 | 2016-01-07 11:50:30 +0100 | [diff] [blame] | 1411 | goto just_remove; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1412 | } |
Jiri Pirko | 8a1ab5d | 2015-12-03 12:12:29 +0100 | [diff] [blame] | 1413 | |
Ido Schimmel | aac78a4 | 2015-12-15 16:03:42 +0100 | [diff] [blame] | 1414 | if (mlxsw_sp_fid_is_vfid(fid)) { |
Ido Schimmel | aac78a4 | 2015-12-15 16:03:42 +0100 | [diff] [blame] | 1415 | struct mlxsw_sp_port *mlxsw_sp_vport; |
| 1416 | |
Ido Schimmel | d0ec875 | 2016-06-20 23:04:12 +0200 | [diff] [blame] | 1417 | mlxsw_sp_vport = mlxsw_sp_port_vport_find_by_fid(mlxsw_sp_port, |
| 1418 | fid); |
Ido Schimmel | aac78a4 | 2015-12-15 16:03:42 +0100 | [diff] [blame] | 1419 | if (!mlxsw_sp_vport) { |
| 1420 | netdev_err(mlxsw_sp_port->dev, "Failed to find a matching vPort following FDB notification\n"); |
Jiri Pirko | 12f1501 | 2016-01-07 11:50:30 +0100 | [diff] [blame] | 1421 | goto just_remove; |
Ido Schimmel | aac78a4 | 2015-12-15 16:03:42 +0100 | [diff] [blame] | 1422 | } |
| 1423 | |
Ido Schimmel | 004f85e | 2016-01-27 15:20:22 +0100 | [diff] [blame] | 1424 | lag_vid = mlxsw_sp_vport_vid_get(mlxsw_sp_vport); |
Ido Schimmel | e43aca2 | 2016-01-27 15:20:23 +0100 | [diff] [blame] | 1425 | dev = mlxsw_sp_vport->dev; |
Ido Schimmel | 004f85e | 2016-01-27 15:20:22 +0100 | [diff] [blame] | 1426 | vid = 0; |
Ido Schimmel | aac78a4 | 2015-12-15 16:03:42 +0100 | [diff] [blame] | 1427 | /* Override the physical port with the vPort. */ |
| 1428 | mlxsw_sp_port = mlxsw_sp_vport; |
| 1429 | } else { |
Ido Schimmel | e43aca2 | 2016-01-27 15:20:23 +0100 | [diff] [blame] | 1430 | dev = mlxsw_sp_lag_get(mlxsw_sp, lag_id)->dev; |
Ido Schimmel | aac78a4 | 2015-12-15 16:03:42 +0100 | [diff] [blame] | 1431 | vid = fid; |
| 1432 | } |
| 1433 | |
Jiri Pirko | 12f1501 | 2016-01-07 11:50:30 +0100 | [diff] [blame] | 1434 | adding = adding && mlxsw_sp_port->learning; |
| 1435 | |
| 1436 | do_fdb_op: |
Ido Schimmel | 64771e3 | 2015-12-15 16:03:46 +0100 | [diff] [blame] | 1437 | err = mlxsw_sp_port_fdb_uc_lag_op(mlxsw_sp, lag_id, mac, fid, lag_vid, |
Jiri Pirko | 12f1501 | 2016-01-07 11:50:30 +0100 | [diff] [blame] | 1438 | adding, true); |
Jiri Pirko | 8a1ab5d | 2015-12-03 12:12:29 +0100 | [diff] [blame] | 1439 | if (err) { |
| 1440 | if (net_ratelimit()) |
| 1441 | netdev_err(mlxsw_sp_port->dev, "Failed to set FDB entry\n"); |
| 1442 | return; |
| 1443 | } |
| 1444 | |
Jiri Pirko | 12f1501 | 2016-01-07 11:50:30 +0100 | [diff] [blame] | 1445 | if (!do_notification) |
| 1446 | return; |
Ido Schimmel | 45827d7 | 2016-01-27 15:20:21 +0100 | [diff] [blame] | 1447 | mlxsw_sp_fdb_call_notifiers(mlxsw_sp_port->learning_sync, adding, mac, |
Ido Schimmel | e43aca2 | 2016-01-27 15:20:23 +0100 | [diff] [blame] | 1448 | vid, dev); |
Jiri Pirko | 12f1501 | 2016-01-07 11:50:30 +0100 | [diff] [blame] | 1449 | return; |
| 1450 | |
| 1451 | just_remove: |
| 1452 | adding = false; |
| 1453 | do_notification = false; |
| 1454 | goto do_fdb_op; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1455 | } |
| 1456 | |
| 1457 | static void mlxsw_sp_fdb_notify_rec_process(struct mlxsw_sp *mlxsw_sp, |
| 1458 | char *sfn_pl, int rec_index) |
| 1459 | { |
| 1460 | switch (mlxsw_reg_sfn_rec_type_get(sfn_pl, rec_index)) { |
| 1461 | case MLXSW_REG_SFN_REC_TYPE_LEARNED_MAC: |
| 1462 | mlxsw_sp_fdb_notify_mac_process(mlxsw_sp, sfn_pl, |
| 1463 | rec_index, true); |
| 1464 | break; |
| 1465 | case MLXSW_REG_SFN_REC_TYPE_AGED_OUT_MAC: |
| 1466 | mlxsw_sp_fdb_notify_mac_process(mlxsw_sp, sfn_pl, |
| 1467 | rec_index, false); |
| 1468 | break; |
Jiri Pirko | 8a1ab5d | 2015-12-03 12:12:29 +0100 | [diff] [blame] | 1469 | case MLXSW_REG_SFN_REC_TYPE_LEARNED_MAC_LAG: |
| 1470 | mlxsw_sp_fdb_notify_mac_lag_process(mlxsw_sp, sfn_pl, |
| 1471 | rec_index, true); |
| 1472 | break; |
| 1473 | case MLXSW_REG_SFN_REC_TYPE_AGED_OUT_MAC_LAG: |
| 1474 | mlxsw_sp_fdb_notify_mac_lag_process(mlxsw_sp, sfn_pl, |
| 1475 | rec_index, false); |
| 1476 | break; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1477 | } |
| 1478 | } |
| 1479 | |
| 1480 | static void mlxsw_sp_fdb_notify_work_schedule(struct mlxsw_sp *mlxsw_sp) |
| 1481 | { |
Jiri Pirko | dd9bdb0 | 2016-04-14 18:19:28 +0200 | [diff] [blame] | 1482 | mlxsw_core_schedule_dw(&mlxsw_sp->fdb_notify.dw, |
| 1483 | msecs_to_jiffies(mlxsw_sp->fdb_notify.interval)); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1484 | } |
| 1485 | |
| 1486 | static void mlxsw_sp_fdb_notify_work(struct work_struct *work) |
| 1487 | { |
| 1488 | struct mlxsw_sp *mlxsw_sp; |
| 1489 | char *sfn_pl; |
| 1490 | u8 num_rec; |
| 1491 | int i; |
| 1492 | int err; |
| 1493 | |
| 1494 | sfn_pl = kmalloc(MLXSW_REG_SFN_LEN, GFP_KERNEL); |
| 1495 | if (!sfn_pl) |
| 1496 | return; |
| 1497 | |
| 1498 | mlxsw_sp = container_of(work, struct mlxsw_sp, fdb_notify.dw.work); |
| 1499 | |
Ido Schimmel | 4f2c6ae | 2016-01-27 15:16:43 +0100 | [diff] [blame] | 1500 | rtnl_lock(); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1501 | do { |
| 1502 | mlxsw_reg_sfn_pack(sfn_pl); |
| 1503 | err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(sfn), sfn_pl); |
| 1504 | if (err) { |
| 1505 | dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Failed to get FDB notifications\n"); |
| 1506 | break; |
| 1507 | } |
| 1508 | num_rec = mlxsw_reg_sfn_num_rec_get(sfn_pl); |
| 1509 | for (i = 0; i < num_rec; i++) |
| 1510 | mlxsw_sp_fdb_notify_rec_process(mlxsw_sp, sfn_pl, i); |
| 1511 | |
| 1512 | } while (num_rec); |
Ido Schimmel | 4f2c6ae | 2016-01-27 15:16:43 +0100 | [diff] [blame] | 1513 | rtnl_unlock(); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1514 | |
| 1515 | kfree(sfn_pl); |
| 1516 | mlxsw_sp_fdb_notify_work_schedule(mlxsw_sp); |
| 1517 | } |
| 1518 | |
| 1519 | static int mlxsw_sp_fdb_init(struct mlxsw_sp *mlxsw_sp) |
| 1520 | { |
| 1521 | int err; |
| 1522 | |
| 1523 | err = mlxsw_sp_ageing_set(mlxsw_sp, MLXSW_SP_DEFAULT_AGEING_TIME); |
| 1524 | if (err) { |
| 1525 | dev_err(mlxsw_sp->bus_info->dev, "Failed to set default ageing time\n"); |
| 1526 | return err; |
| 1527 | } |
| 1528 | INIT_DELAYED_WORK(&mlxsw_sp->fdb_notify.dw, mlxsw_sp_fdb_notify_work); |
| 1529 | mlxsw_sp->fdb_notify.interval = MLXSW_SP_DEFAULT_LEARNING_INTERVAL; |
| 1530 | mlxsw_sp_fdb_notify_work_schedule(mlxsw_sp); |
| 1531 | return 0; |
| 1532 | } |
| 1533 | |
| 1534 | static void mlxsw_sp_fdb_fini(struct mlxsw_sp *mlxsw_sp) |
| 1535 | { |
| 1536 | cancel_delayed_work_sync(&mlxsw_sp->fdb_notify.dw); |
| 1537 | } |
| 1538 | |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1539 | int mlxsw_sp_switchdev_init(struct mlxsw_sp *mlxsw_sp) |
| 1540 | { |
| 1541 | return mlxsw_sp_fdb_init(mlxsw_sp); |
| 1542 | } |
| 1543 | |
| 1544 | void mlxsw_sp_switchdev_fini(struct mlxsw_sp *mlxsw_sp) |
| 1545 | { |
| 1546 | mlxsw_sp_fdb_fini(mlxsw_sp); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1547 | } |
| 1548 | |
| 1549 | int mlxsw_sp_port_vlan_init(struct mlxsw_sp_port *mlxsw_sp_port) |
| 1550 | { |
| 1551 | struct net_device *dev = mlxsw_sp_port->dev; |
| 1552 | int err; |
| 1553 | |
| 1554 | /* Allow only untagged packets to ingress and tag them internally |
| 1555 | * with VID 1. |
| 1556 | */ |
| 1557 | mlxsw_sp_port->pvid = 1; |
Elad Raz | 29edf44 | 2016-01-06 13:01:08 +0100 | [diff] [blame] | 1558 | err = __mlxsw_sp_port_vlans_del(mlxsw_sp_port, 0, VLAN_N_VID - 1, |
| 1559 | true); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1560 | if (err) { |
| 1561 | netdev_err(dev, "Unable to init VLANs\n"); |
| 1562 | return err; |
| 1563 | } |
| 1564 | |
| 1565 | /* Add implicit VLAN interface in the device, so that untagged |
| 1566 | * packets will be classified to the default vFID. |
| 1567 | */ |
| 1568 | err = mlxsw_sp_port_add_vid(dev, 0, 1); |
| 1569 | if (err) |
| 1570 | netdev_err(dev, "Failed to configure default vFID\n"); |
| 1571 | |
| 1572 | return err; |
| 1573 | } |
| 1574 | |
| 1575 | void mlxsw_sp_port_switchdev_init(struct mlxsw_sp_port *mlxsw_sp_port) |
| 1576 | { |
| 1577 | mlxsw_sp_port->dev->switchdev_ops = &mlxsw_sp_port_switchdev_ops; |
| 1578 | } |
| 1579 | |
| 1580 | void mlxsw_sp_port_switchdev_fini(struct mlxsw_sp_port *mlxsw_sp_port) |
| 1581 | { |
| 1582 | } |