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> |
Jiri Pirko | 1d20d23 | 2016-10-27 15:12:59 +0200 | [diff] [blame] | 40 | #include <linux/pci.h> |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 41 | #include <linux/netdevice.h> |
| 42 | #include <linux/etherdevice.h> |
| 43 | #include <linux/ethtool.h> |
| 44 | #include <linux/slab.h> |
| 45 | #include <linux/device.h> |
| 46 | #include <linux/skbuff.h> |
| 47 | #include <linux/if_vlan.h> |
| 48 | #include <linux/if_bridge.h> |
| 49 | #include <linux/workqueue.h> |
| 50 | #include <linux/jiffies.h> |
| 51 | #include <linux/bitops.h> |
Ido Schimmel | 7f71eb4 | 2015-12-15 16:03:37 +0100 | [diff] [blame] | 52 | #include <linux/list.h> |
Ido Schimmel | 80bedf1 | 2016-06-20 23:03:59 +0200 | [diff] [blame] | 53 | #include <linux/notifier.h> |
Ido Schimmel | 90183b9 | 2016-04-06 17:10:08 +0200 | [diff] [blame] | 54 | #include <linux/dcbnl.h> |
Ido Schimmel | 99724c1 | 2016-07-04 08:23:14 +0200 | [diff] [blame] | 55 | #include <linux/inetdevice.h> |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 56 | #include <net/switchdev.h> |
Yotam Gigi | 763b4b7 | 2016-07-21 12:03:17 +0200 | [diff] [blame] | 57 | #include <net/pkt_cls.h> |
| 58 | #include <net/tc_act/tc_mirred.h> |
Jiri Pirko | e732263 | 2016-09-01 10:37:43 +0200 | [diff] [blame] | 59 | #include <net/netevent.h> |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 60 | |
| 61 | #include "spectrum.h" |
Jiri Pirko | 1d20d23 | 2016-10-27 15:12:59 +0200 | [diff] [blame] | 62 | #include "pci.h" |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 63 | #include "core.h" |
| 64 | #include "reg.h" |
| 65 | #include "port.h" |
| 66 | #include "trap.h" |
| 67 | #include "txheader.h" |
| 68 | |
| 69 | static const char mlxsw_sp_driver_name[] = "mlxsw_spectrum"; |
| 70 | static const char mlxsw_sp_driver_version[] = "1.0"; |
| 71 | |
| 72 | /* tx_hdr_version |
| 73 | * Tx header version. |
| 74 | * Must be set to 1. |
| 75 | */ |
| 76 | MLXSW_ITEM32(tx, hdr, version, 0x00, 28, 4); |
| 77 | |
| 78 | /* tx_hdr_ctl |
| 79 | * Packet control type. |
| 80 | * 0 - Ethernet control (e.g. EMADs, LACP) |
| 81 | * 1 - Ethernet data |
| 82 | */ |
| 83 | MLXSW_ITEM32(tx, hdr, ctl, 0x00, 26, 2); |
| 84 | |
| 85 | /* tx_hdr_proto |
| 86 | * Packet protocol type. Must be set to 1 (Ethernet). |
| 87 | */ |
| 88 | MLXSW_ITEM32(tx, hdr, proto, 0x00, 21, 3); |
| 89 | |
| 90 | /* tx_hdr_rx_is_router |
| 91 | * Packet is sent from the router. Valid for data packets only. |
| 92 | */ |
| 93 | MLXSW_ITEM32(tx, hdr, rx_is_router, 0x00, 19, 1); |
| 94 | |
| 95 | /* tx_hdr_fid_valid |
| 96 | * Indicates if the 'fid' field is valid and should be used for |
| 97 | * forwarding lookup. Valid for data packets only. |
| 98 | */ |
| 99 | MLXSW_ITEM32(tx, hdr, fid_valid, 0x00, 16, 1); |
| 100 | |
| 101 | /* tx_hdr_swid |
| 102 | * Switch partition ID. Must be set to 0. |
| 103 | */ |
| 104 | MLXSW_ITEM32(tx, hdr, swid, 0x00, 12, 3); |
| 105 | |
| 106 | /* tx_hdr_control_tclass |
| 107 | * Indicates if the packet should use the control TClass and not one |
| 108 | * of the data TClasses. |
| 109 | */ |
| 110 | MLXSW_ITEM32(tx, hdr, control_tclass, 0x00, 6, 1); |
| 111 | |
| 112 | /* tx_hdr_etclass |
| 113 | * Egress TClass to be used on the egress device on the egress port. |
| 114 | */ |
| 115 | MLXSW_ITEM32(tx, hdr, etclass, 0x00, 0, 4); |
| 116 | |
| 117 | /* tx_hdr_port_mid |
| 118 | * Destination local port for unicast packets. |
| 119 | * Destination multicast ID for multicast packets. |
| 120 | * |
| 121 | * Control packets are directed to a specific egress port, while data |
| 122 | * packets are transmitted through the CPU port (0) into the switch partition, |
| 123 | * where forwarding rules are applied. |
| 124 | */ |
| 125 | MLXSW_ITEM32(tx, hdr, port_mid, 0x04, 16, 16); |
| 126 | |
| 127 | /* tx_hdr_fid |
| 128 | * Forwarding ID used for L2 forwarding lookup. Valid only if 'fid_valid' is |
| 129 | * set, otherwise calculated based on the packet's VID using VID to FID mapping. |
| 130 | * Valid for data packets only. |
| 131 | */ |
| 132 | MLXSW_ITEM32(tx, hdr, fid, 0x08, 0, 16); |
| 133 | |
| 134 | /* tx_hdr_type |
| 135 | * 0 - Data packets |
| 136 | * 6 - Control packets |
| 137 | */ |
| 138 | MLXSW_ITEM32(tx, hdr, type, 0x0C, 0, 4); |
| 139 | |
Yotam Gigi | 763b4b7 | 2016-07-21 12:03:17 +0200 | [diff] [blame] | 140 | static bool mlxsw_sp_port_dev_check(const struct net_device *dev); |
| 141 | |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 142 | static void mlxsw_sp_txhdr_construct(struct sk_buff *skb, |
| 143 | const struct mlxsw_tx_info *tx_info) |
| 144 | { |
| 145 | char *txhdr = skb_push(skb, MLXSW_TXHDR_LEN); |
| 146 | |
| 147 | memset(txhdr, 0, MLXSW_TXHDR_LEN); |
| 148 | |
| 149 | mlxsw_tx_hdr_version_set(txhdr, MLXSW_TXHDR_VERSION_1); |
| 150 | mlxsw_tx_hdr_ctl_set(txhdr, MLXSW_TXHDR_ETH_CTL); |
| 151 | mlxsw_tx_hdr_proto_set(txhdr, MLXSW_TXHDR_PROTO_ETH); |
| 152 | mlxsw_tx_hdr_swid_set(txhdr, 0); |
| 153 | mlxsw_tx_hdr_control_tclass_set(txhdr, 1); |
| 154 | mlxsw_tx_hdr_port_mid_set(txhdr, tx_info->local_port); |
| 155 | mlxsw_tx_hdr_type_set(txhdr, MLXSW_TXHDR_TYPE_CONTROL); |
| 156 | } |
| 157 | |
| 158 | static int mlxsw_sp_base_mac_get(struct mlxsw_sp *mlxsw_sp) |
| 159 | { |
Elad Raz | 5b09074 | 2016-10-28 21:35:46 +0200 | [diff] [blame] | 160 | char spad_pl[MLXSW_REG_SPAD_LEN] = {0}; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 161 | int err; |
| 162 | |
| 163 | err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(spad), spad_pl); |
| 164 | if (err) |
| 165 | return err; |
| 166 | mlxsw_reg_spad_base_mac_memcpy_from(spad_pl, mlxsw_sp->base_mac); |
| 167 | return 0; |
| 168 | } |
| 169 | |
Yotam Gigi | 763b4b7 | 2016-07-21 12:03:17 +0200 | [diff] [blame] | 170 | static int mlxsw_sp_span_init(struct mlxsw_sp *mlxsw_sp) |
| 171 | { |
Yotam Gigi | 763b4b7 | 2016-07-21 12:03:17 +0200 | [diff] [blame] | 172 | int i; |
| 173 | |
Jiri Pirko | c1a3831 | 2016-10-21 16:07:23 +0200 | [diff] [blame] | 174 | if (!MLXSW_CORE_RES_VALID(mlxsw_sp->core, MAX_SPAN)) |
Yotam Gigi | 763b4b7 | 2016-07-21 12:03:17 +0200 | [diff] [blame] | 175 | return -EIO; |
| 176 | |
Jiri Pirko | c1a3831 | 2016-10-21 16:07:23 +0200 | [diff] [blame] | 177 | mlxsw_sp->span.entries_count = MLXSW_CORE_RES_GET(mlxsw_sp->core, |
| 178 | MAX_SPAN); |
Yotam Gigi | 763b4b7 | 2016-07-21 12:03:17 +0200 | [diff] [blame] | 179 | mlxsw_sp->span.entries = kcalloc(mlxsw_sp->span.entries_count, |
| 180 | sizeof(struct mlxsw_sp_span_entry), |
| 181 | GFP_KERNEL); |
| 182 | if (!mlxsw_sp->span.entries) |
| 183 | return -ENOMEM; |
| 184 | |
| 185 | for (i = 0; i < mlxsw_sp->span.entries_count; i++) |
| 186 | INIT_LIST_HEAD(&mlxsw_sp->span.entries[i].bound_ports_list); |
| 187 | |
| 188 | return 0; |
| 189 | } |
| 190 | |
| 191 | static void mlxsw_sp_span_fini(struct mlxsw_sp *mlxsw_sp) |
| 192 | { |
| 193 | int i; |
| 194 | |
| 195 | for (i = 0; i < mlxsw_sp->span.entries_count; i++) { |
| 196 | struct mlxsw_sp_span_entry *curr = &mlxsw_sp->span.entries[i]; |
| 197 | |
| 198 | WARN_ON_ONCE(!list_empty(&curr->bound_ports_list)); |
| 199 | } |
| 200 | kfree(mlxsw_sp->span.entries); |
| 201 | } |
| 202 | |
| 203 | static struct mlxsw_sp_span_entry * |
| 204 | mlxsw_sp_span_entry_create(struct mlxsw_sp_port *port) |
| 205 | { |
| 206 | struct mlxsw_sp *mlxsw_sp = port->mlxsw_sp; |
| 207 | struct mlxsw_sp_span_entry *span_entry; |
| 208 | char mpat_pl[MLXSW_REG_MPAT_LEN]; |
| 209 | u8 local_port = port->local_port; |
| 210 | int index; |
| 211 | int i; |
| 212 | int err; |
| 213 | |
| 214 | /* find a free entry to use */ |
| 215 | index = -1; |
| 216 | for (i = 0; i < mlxsw_sp->span.entries_count; i++) { |
| 217 | if (!mlxsw_sp->span.entries[i].used) { |
| 218 | index = i; |
| 219 | span_entry = &mlxsw_sp->span.entries[i]; |
| 220 | break; |
| 221 | } |
| 222 | } |
| 223 | if (index < 0) |
| 224 | return NULL; |
| 225 | |
| 226 | /* create a new port analayzer entry for local_port */ |
| 227 | mlxsw_reg_mpat_pack(mpat_pl, index, local_port, true); |
| 228 | err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(mpat), mpat_pl); |
| 229 | if (err) |
| 230 | return NULL; |
| 231 | |
| 232 | span_entry->used = true; |
| 233 | span_entry->id = index; |
Yotam Gigi | 2d644d4 | 2016-11-11 16:34:25 +0100 | [diff] [blame] | 234 | span_entry->ref_count = 1; |
Yotam Gigi | 763b4b7 | 2016-07-21 12:03:17 +0200 | [diff] [blame] | 235 | span_entry->local_port = local_port; |
| 236 | return span_entry; |
| 237 | } |
| 238 | |
| 239 | static void mlxsw_sp_span_entry_destroy(struct mlxsw_sp *mlxsw_sp, |
| 240 | struct mlxsw_sp_span_entry *span_entry) |
| 241 | { |
| 242 | u8 local_port = span_entry->local_port; |
| 243 | char mpat_pl[MLXSW_REG_MPAT_LEN]; |
| 244 | int pa_id = span_entry->id; |
| 245 | |
| 246 | mlxsw_reg_mpat_pack(mpat_pl, pa_id, local_port, false); |
| 247 | mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(mpat), mpat_pl); |
| 248 | span_entry->used = false; |
| 249 | } |
| 250 | |
Ido Schimmel | 1a9234e66 | 2016-09-19 08:29:26 +0200 | [diff] [blame] | 251 | static struct mlxsw_sp_span_entry * |
| 252 | mlxsw_sp_span_entry_find(struct mlxsw_sp_port *port) |
Yotam Gigi | 763b4b7 | 2016-07-21 12:03:17 +0200 | [diff] [blame] | 253 | { |
| 254 | struct mlxsw_sp *mlxsw_sp = port->mlxsw_sp; |
| 255 | int i; |
| 256 | |
| 257 | for (i = 0; i < mlxsw_sp->span.entries_count; i++) { |
| 258 | struct mlxsw_sp_span_entry *curr = &mlxsw_sp->span.entries[i]; |
| 259 | |
| 260 | if (curr->used && curr->local_port == port->local_port) |
| 261 | return curr; |
| 262 | } |
| 263 | return NULL; |
| 264 | } |
| 265 | |
Ido Schimmel | 1a9234e66 | 2016-09-19 08:29:26 +0200 | [diff] [blame] | 266 | static struct mlxsw_sp_span_entry |
| 267 | *mlxsw_sp_span_entry_get(struct mlxsw_sp_port *port) |
Yotam Gigi | 763b4b7 | 2016-07-21 12:03:17 +0200 | [diff] [blame] | 268 | { |
| 269 | struct mlxsw_sp_span_entry *span_entry; |
| 270 | |
| 271 | span_entry = mlxsw_sp_span_entry_find(port); |
| 272 | if (span_entry) { |
Yotam Gigi | 2d644d4 | 2016-11-11 16:34:25 +0100 | [diff] [blame] | 273 | /* Already exists, just take a reference */ |
Yotam Gigi | 763b4b7 | 2016-07-21 12:03:17 +0200 | [diff] [blame] | 274 | span_entry->ref_count++; |
| 275 | return span_entry; |
| 276 | } |
| 277 | |
| 278 | return mlxsw_sp_span_entry_create(port); |
| 279 | } |
| 280 | |
| 281 | static int mlxsw_sp_span_entry_put(struct mlxsw_sp *mlxsw_sp, |
| 282 | struct mlxsw_sp_span_entry *span_entry) |
| 283 | { |
Yotam Gigi | 2d644d4 | 2016-11-11 16:34:25 +0100 | [diff] [blame] | 284 | WARN_ON(!span_entry->ref_count); |
Yotam Gigi | 763b4b7 | 2016-07-21 12:03:17 +0200 | [diff] [blame] | 285 | if (--span_entry->ref_count == 0) |
| 286 | mlxsw_sp_span_entry_destroy(mlxsw_sp, span_entry); |
| 287 | return 0; |
| 288 | } |
| 289 | |
| 290 | static bool mlxsw_sp_span_is_egress_mirror(struct mlxsw_sp_port *port) |
| 291 | { |
| 292 | struct mlxsw_sp *mlxsw_sp = port->mlxsw_sp; |
| 293 | struct mlxsw_sp_span_inspected_port *p; |
| 294 | int i; |
| 295 | |
| 296 | for (i = 0; i < mlxsw_sp->span.entries_count; i++) { |
| 297 | struct mlxsw_sp_span_entry *curr = &mlxsw_sp->span.entries[i]; |
| 298 | |
| 299 | list_for_each_entry(p, &curr->bound_ports_list, list) |
| 300 | if (p->local_port == port->local_port && |
| 301 | p->type == MLXSW_SP_SPAN_EGRESS) |
| 302 | return true; |
| 303 | } |
| 304 | |
| 305 | return false; |
| 306 | } |
| 307 | |
| 308 | static int mlxsw_sp_span_mtu_to_buffsize(int mtu) |
| 309 | { |
| 310 | return MLXSW_SP_BYTES_TO_CELLS(mtu * 5 / 2) + 1; |
| 311 | } |
| 312 | |
| 313 | static int mlxsw_sp_span_port_mtu_update(struct mlxsw_sp_port *port, u16 mtu) |
| 314 | { |
| 315 | struct mlxsw_sp *mlxsw_sp = port->mlxsw_sp; |
| 316 | char sbib_pl[MLXSW_REG_SBIB_LEN]; |
| 317 | int err; |
| 318 | |
| 319 | /* If port is egress mirrored, the shared buffer size should be |
| 320 | * updated according to the mtu value |
| 321 | */ |
| 322 | if (mlxsw_sp_span_is_egress_mirror(port)) { |
| 323 | mlxsw_reg_sbib_pack(sbib_pl, port->local_port, |
| 324 | mlxsw_sp_span_mtu_to_buffsize(mtu)); |
| 325 | err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sbib), sbib_pl); |
| 326 | if (err) { |
| 327 | netdev_err(port->dev, "Could not update shared buffer for mirroring\n"); |
| 328 | return err; |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | return 0; |
| 333 | } |
| 334 | |
| 335 | static struct mlxsw_sp_span_inspected_port * |
| 336 | mlxsw_sp_span_entry_bound_port_find(struct mlxsw_sp_port *port, |
| 337 | struct mlxsw_sp_span_entry *span_entry) |
| 338 | { |
| 339 | struct mlxsw_sp_span_inspected_port *p; |
| 340 | |
| 341 | list_for_each_entry(p, &span_entry->bound_ports_list, list) |
| 342 | if (port->local_port == p->local_port) |
| 343 | return p; |
| 344 | return NULL; |
| 345 | } |
| 346 | |
| 347 | static int |
| 348 | mlxsw_sp_span_inspected_port_bind(struct mlxsw_sp_port *port, |
| 349 | struct mlxsw_sp_span_entry *span_entry, |
| 350 | enum mlxsw_sp_span_type type) |
| 351 | { |
| 352 | struct mlxsw_sp_span_inspected_port *inspected_port; |
| 353 | struct mlxsw_sp *mlxsw_sp = port->mlxsw_sp; |
| 354 | char mpar_pl[MLXSW_REG_MPAR_LEN]; |
| 355 | char sbib_pl[MLXSW_REG_SBIB_LEN]; |
| 356 | int pa_id = span_entry->id; |
| 357 | int err; |
| 358 | |
| 359 | /* if it is an egress SPAN, bind a shared buffer to it */ |
| 360 | if (type == MLXSW_SP_SPAN_EGRESS) { |
| 361 | mlxsw_reg_sbib_pack(sbib_pl, port->local_port, |
| 362 | mlxsw_sp_span_mtu_to_buffsize(port->dev->mtu)); |
| 363 | err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sbib), sbib_pl); |
| 364 | if (err) { |
| 365 | netdev_err(port->dev, "Could not create shared buffer for mirroring\n"); |
| 366 | return err; |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | /* bind the port to the SPAN entry */ |
Ido Schimmel | 1a9234e66 | 2016-09-19 08:29:26 +0200 | [diff] [blame] | 371 | mlxsw_reg_mpar_pack(mpar_pl, port->local_port, |
| 372 | (enum mlxsw_reg_mpar_i_e) type, true, pa_id); |
Yotam Gigi | 763b4b7 | 2016-07-21 12:03:17 +0200 | [diff] [blame] | 373 | err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(mpar), mpar_pl); |
| 374 | if (err) |
| 375 | goto err_mpar_reg_write; |
| 376 | |
| 377 | inspected_port = kzalloc(sizeof(*inspected_port), GFP_KERNEL); |
| 378 | if (!inspected_port) { |
| 379 | err = -ENOMEM; |
| 380 | goto err_inspected_port_alloc; |
| 381 | } |
| 382 | inspected_port->local_port = port->local_port; |
| 383 | inspected_port->type = type; |
| 384 | list_add_tail(&inspected_port->list, &span_entry->bound_ports_list); |
| 385 | |
| 386 | return 0; |
| 387 | |
| 388 | err_mpar_reg_write: |
| 389 | err_inspected_port_alloc: |
| 390 | if (type == MLXSW_SP_SPAN_EGRESS) { |
| 391 | mlxsw_reg_sbib_pack(sbib_pl, port->local_port, 0); |
| 392 | mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sbib), sbib_pl); |
| 393 | } |
| 394 | return err; |
| 395 | } |
| 396 | |
| 397 | static void |
| 398 | mlxsw_sp_span_inspected_port_unbind(struct mlxsw_sp_port *port, |
| 399 | struct mlxsw_sp_span_entry *span_entry, |
| 400 | enum mlxsw_sp_span_type type) |
| 401 | { |
| 402 | struct mlxsw_sp_span_inspected_port *inspected_port; |
| 403 | struct mlxsw_sp *mlxsw_sp = port->mlxsw_sp; |
| 404 | char mpar_pl[MLXSW_REG_MPAR_LEN]; |
| 405 | char sbib_pl[MLXSW_REG_SBIB_LEN]; |
| 406 | int pa_id = span_entry->id; |
| 407 | |
| 408 | inspected_port = mlxsw_sp_span_entry_bound_port_find(port, span_entry); |
| 409 | if (!inspected_port) |
| 410 | return; |
| 411 | |
| 412 | /* remove the inspected port */ |
Ido Schimmel | 1a9234e66 | 2016-09-19 08:29:26 +0200 | [diff] [blame] | 413 | mlxsw_reg_mpar_pack(mpar_pl, port->local_port, |
| 414 | (enum mlxsw_reg_mpar_i_e) type, false, pa_id); |
Yotam Gigi | 763b4b7 | 2016-07-21 12:03:17 +0200 | [diff] [blame] | 415 | mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(mpar), mpar_pl); |
| 416 | |
| 417 | /* remove the SBIB buffer if it was egress SPAN */ |
| 418 | if (type == MLXSW_SP_SPAN_EGRESS) { |
| 419 | mlxsw_reg_sbib_pack(sbib_pl, port->local_port, 0); |
| 420 | mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sbib), sbib_pl); |
| 421 | } |
| 422 | |
| 423 | mlxsw_sp_span_entry_put(mlxsw_sp, span_entry); |
| 424 | |
| 425 | list_del(&inspected_port->list); |
| 426 | kfree(inspected_port); |
| 427 | } |
| 428 | |
| 429 | static int mlxsw_sp_span_mirror_add(struct mlxsw_sp_port *from, |
| 430 | struct mlxsw_sp_port *to, |
| 431 | enum mlxsw_sp_span_type type) |
| 432 | { |
| 433 | struct mlxsw_sp *mlxsw_sp = from->mlxsw_sp; |
| 434 | struct mlxsw_sp_span_entry *span_entry; |
| 435 | int err; |
| 436 | |
| 437 | span_entry = mlxsw_sp_span_entry_get(to); |
| 438 | if (!span_entry) |
| 439 | return -ENOENT; |
| 440 | |
| 441 | netdev_dbg(from->dev, "Adding inspected port to SPAN entry %d\n", |
| 442 | span_entry->id); |
| 443 | |
| 444 | err = mlxsw_sp_span_inspected_port_bind(from, span_entry, type); |
| 445 | if (err) |
| 446 | goto err_port_bind; |
| 447 | |
| 448 | return 0; |
| 449 | |
| 450 | err_port_bind: |
| 451 | mlxsw_sp_span_entry_put(mlxsw_sp, span_entry); |
| 452 | return err; |
| 453 | } |
| 454 | |
| 455 | static void mlxsw_sp_span_mirror_remove(struct mlxsw_sp_port *from, |
| 456 | struct mlxsw_sp_port *to, |
| 457 | enum mlxsw_sp_span_type type) |
| 458 | { |
| 459 | struct mlxsw_sp_span_entry *span_entry; |
| 460 | |
| 461 | span_entry = mlxsw_sp_span_entry_find(to); |
| 462 | if (!span_entry) { |
| 463 | netdev_err(from->dev, "no span entry found\n"); |
| 464 | return; |
| 465 | } |
| 466 | |
| 467 | netdev_dbg(from->dev, "removing inspected port from SPAN entry %d\n", |
| 468 | span_entry->id); |
| 469 | mlxsw_sp_span_inspected_port_unbind(from, span_entry, type); |
| 470 | } |
| 471 | |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 472 | static int mlxsw_sp_port_admin_status_set(struct mlxsw_sp_port *mlxsw_sp_port, |
| 473 | bool is_up) |
| 474 | { |
| 475 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
| 476 | char paos_pl[MLXSW_REG_PAOS_LEN]; |
| 477 | |
| 478 | mlxsw_reg_paos_pack(paos_pl, mlxsw_sp_port->local_port, |
| 479 | is_up ? MLXSW_PORT_ADMIN_STATUS_UP : |
| 480 | MLXSW_PORT_ADMIN_STATUS_DOWN); |
| 481 | return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(paos), paos_pl); |
| 482 | } |
| 483 | |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 484 | static int mlxsw_sp_port_dev_addr_set(struct mlxsw_sp_port *mlxsw_sp_port, |
| 485 | unsigned char *addr) |
| 486 | { |
| 487 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
| 488 | char ppad_pl[MLXSW_REG_PPAD_LEN]; |
| 489 | |
| 490 | mlxsw_reg_ppad_pack(ppad_pl, true, mlxsw_sp_port->local_port); |
| 491 | mlxsw_reg_ppad_mac_memcpy_to(ppad_pl, addr); |
| 492 | return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ppad), ppad_pl); |
| 493 | } |
| 494 | |
| 495 | static int mlxsw_sp_port_dev_addr_init(struct mlxsw_sp_port *mlxsw_sp_port) |
| 496 | { |
| 497 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
| 498 | unsigned char *addr = mlxsw_sp_port->dev->dev_addr; |
| 499 | |
| 500 | ether_addr_copy(addr, mlxsw_sp->base_mac); |
| 501 | addr[ETH_ALEN - 1] += mlxsw_sp_port->local_port; |
| 502 | return mlxsw_sp_port_dev_addr_set(mlxsw_sp_port, addr); |
| 503 | } |
| 504 | |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 505 | static int mlxsw_sp_port_mtu_set(struct mlxsw_sp_port *mlxsw_sp_port, u16 mtu) |
| 506 | { |
| 507 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
| 508 | char pmtu_pl[MLXSW_REG_PMTU_LEN]; |
| 509 | int max_mtu; |
| 510 | int err; |
| 511 | |
| 512 | mtu += MLXSW_TXHDR_LEN + ETH_HLEN; |
| 513 | mlxsw_reg_pmtu_pack(pmtu_pl, mlxsw_sp_port->local_port, 0); |
| 514 | err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(pmtu), pmtu_pl); |
| 515 | if (err) |
| 516 | return err; |
| 517 | max_mtu = mlxsw_reg_pmtu_max_mtu_get(pmtu_pl); |
| 518 | |
| 519 | if (mtu > max_mtu) |
| 520 | return -EINVAL; |
| 521 | |
| 522 | mlxsw_reg_pmtu_pack(pmtu_pl, mlxsw_sp_port->local_port, mtu); |
| 523 | return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(pmtu), pmtu_pl); |
| 524 | } |
| 525 | |
Ido Schimmel | be94535 | 2016-06-09 09:51:39 +0200 | [diff] [blame] | 526 | static int __mlxsw_sp_port_swid_set(struct mlxsw_sp *mlxsw_sp, u8 local_port, |
| 527 | u8 swid) |
| 528 | { |
| 529 | char pspa_pl[MLXSW_REG_PSPA_LEN]; |
| 530 | |
| 531 | mlxsw_reg_pspa_pack(pspa_pl, swid, local_port); |
| 532 | return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(pspa), pspa_pl); |
| 533 | } |
| 534 | |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 535 | static int mlxsw_sp_port_swid_set(struct mlxsw_sp_port *mlxsw_sp_port, u8 swid) |
| 536 | { |
| 537 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 538 | |
Ido Schimmel | be94535 | 2016-06-09 09:51:39 +0200 | [diff] [blame] | 539 | return __mlxsw_sp_port_swid_set(mlxsw_sp, mlxsw_sp_port->local_port, |
| 540 | swid); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | static int mlxsw_sp_port_vp_mode_set(struct mlxsw_sp_port *mlxsw_sp_port, |
| 544 | bool enable) |
| 545 | { |
| 546 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
| 547 | char svpe_pl[MLXSW_REG_SVPE_LEN]; |
| 548 | |
| 549 | mlxsw_reg_svpe_pack(svpe_pl, mlxsw_sp_port->local_port, enable); |
| 550 | return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(svpe), svpe_pl); |
| 551 | } |
| 552 | |
| 553 | int mlxsw_sp_port_vid_to_fid_set(struct mlxsw_sp_port *mlxsw_sp_port, |
| 554 | enum mlxsw_reg_svfa_mt mt, bool valid, u16 fid, |
| 555 | u16 vid) |
| 556 | { |
| 557 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
| 558 | char svfa_pl[MLXSW_REG_SVFA_LEN]; |
| 559 | |
| 560 | mlxsw_reg_svfa_pack(svfa_pl, mlxsw_sp_port->local_port, mt, valid, |
| 561 | fid, vid); |
| 562 | return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(svfa), svfa_pl); |
| 563 | } |
| 564 | |
Ido Schimmel | 584d73d | 2016-08-24 12:00:26 +0200 | [diff] [blame] | 565 | int __mlxsw_sp_port_vid_learning_set(struct mlxsw_sp_port *mlxsw_sp_port, |
| 566 | u16 vid_begin, u16 vid_end, |
| 567 | bool learn_enable) |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 568 | { |
| 569 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
| 570 | char *spvmlr_pl; |
| 571 | int err; |
| 572 | |
| 573 | spvmlr_pl = kmalloc(MLXSW_REG_SPVMLR_LEN, GFP_KERNEL); |
| 574 | if (!spvmlr_pl) |
| 575 | return -ENOMEM; |
Ido Schimmel | 584d73d | 2016-08-24 12:00:26 +0200 | [diff] [blame] | 576 | mlxsw_reg_spvmlr_pack(spvmlr_pl, mlxsw_sp_port->local_port, vid_begin, |
| 577 | vid_end, learn_enable); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 578 | err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(spvmlr), spvmlr_pl); |
| 579 | kfree(spvmlr_pl); |
| 580 | return err; |
| 581 | } |
| 582 | |
Ido Schimmel | 584d73d | 2016-08-24 12:00:26 +0200 | [diff] [blame] | 583 | static int mlxsw_sp_port_vid_learning_set(struct mlxsw_sp_port *mlxsw_sp_port, |
| 584 | u16 vid, bool learn_enable) |
| 585 | { |
| 586 | return __mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid, vid, |
| 587 | learn_enable); |
| 588 | } |
| 589 | |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 590 | static int |
| 591 | mlxsw_sp_port_system_port_mapping_set(struct mlxsw_sp_port *mlxsw_sp_port) |
| 592 | { |
| 593 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
| 594 | char sspr_pl[MLXSW_REG_SSPR_LEN]; |
| 595 | |
| 596 | mlxsw_reg_sspr_pack(sspr_pl, mlxsw_sp_port->local_port); |
| 597 | return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sspr), sspr_pl); |
| 598 | } |
| 599 | |
Ido Schimmel | d664b41 | 2016-06-09 09:51:40 +0200 | [diff] [blame] | 600 | static int mlxsw_sp_port_module_info_get(struct mlxsw_sp *mlxsw_sp, |
| 601 | u8 local_port, u8 *p_module, |
| 602 | u8 *p_width, u8 *p_lane) |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 603 | { |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 604 | char pmlp_pl[MLXSW_REG_PMLP_LEN]; |
| 605 | int err; |
| 606 | |
Ido Schimmel | 558c2d5 | 2016-02-26 17:32:29 +0100 | [diff] [blame] | 607 | mlxsw_reg_pmlp_pack(pmlp_pl, local_port); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 608 | err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(pmlp), pmlp_pl); |
| 609 | if (err) |
| 610 | return err; |
Ido Schimmel | 558c2d5 | 2016-02-26 17:32:29 +0100 | [diff] [blame] | 611 | *p_module = mlxsw_reg_pmlp_module_get(pmlp_pl, 0); |
| 612 | *p_width = mlxsw_reg_pmlp_width_get(pmlp_pl); |
Ido Schimmel | 2bf9a58 | 2016-04-05 10:20:04 +0200 | [diff] [blame] | 613 | *p_lane = mlxsw_reg_pmlp_tx_lane_get(pmlp_pl, 0); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 614 | return 0; |
| 615 | } |
| 616 | |
Ido Schimmel | 18f1e70 | 2016-02-26 17:32:31 +0100 | [diff] [blame] | 617 | static int mlxsw_sp_port_module_map(struct mlxsw_sp *mlxsw_sp, u8 local_port, |
| 618 | u8 module, u8 width, u8 lane) |
| 619 | { |
| 620 | char pmlp_pl[MLXSW_REG_PMLP_LEN]; |
| 621 | int i; |
| 622 | |
| 623 | mlxsw_reg_pmlp_pack(pmlp_pl, local_port); |
| 624 | mlxsw_reg_pmlp_width_set(pmlp_pl, width); |
| 625 | for (i = 0; i < width; i++) { |
| 626 | mlxsw_reg_pmlp_module_set(pmlp_pl, i, module); |
| 627 | mlxsw_reg_pmlp_tx_lane_set(pmlp_pl, i, lane + i); /* Rx & Tx */ |
| 628 | } |
| 629 | |
| 630 | return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(pmlp), pmlp_pl); |
| 631 | } |
| 632 | |
Ido Schimmel | 3e9b27b | 2016-02-26 17:32:28 +0100 | [diff] [blame] | 633 | static int mlxsw_sp_port_module_unmap(struct mlxsw_sp *mlxsw_sp, u8 local_port) |
| 634 | { |
| 635 | char pmlp_pl[MLXSW_REG_PMLP_LEN]; |
| 636 | |
| 637 | mlxsw_reg_pmlp_pack(pmlp_pl, local_port); |
| 638 | mlxsw_reg_pmlp_width_set(pmlp_pl, 0); |
| 639 | return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(pmlp), pmlp_pl); |
| 640 | } |
| 641 | |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 642 | static int mlxsw_sp_port_open(struct net_device *dev) |
| 643 | { |
| 644 | struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev); |
| 645 | int err; |
| 646 | |
| 647 | err = mlxsw_sp_port_admin_status_set(mlxsw_sp_port, true); |
| 648 | if (err) |
| 649 | return err; |
| 650 | netif_start_queue(dev); |
| 651 | return 0; |
| 652 | } |
| 653 | |
| 654 | static int mlxsw_sp_port_stop(struct net_device *dev) |
| 655 | { |
| 656 | struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev); |
| 657 | |
| 658 | netif_stop_queue(dev); |
| 659 | return mlxsw_sp_port_admin_status_set(mlxsw_sp_port, false); |
| 660 | } |
| 661 | |
| 662 | static netdev_tx_t mlxsw_sp_port_xmit(struct sk_buff *skb, |
| 663 | struct net_device *dev) |
| 664 | { |
| 665 | struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev); |
| 666 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
| 667 | struct mlxsw_sp_port_pcpu_stats *pcpu_stats; |
| 668 | const struct mlxsw_tx_info tx_info = { |
| 669 | .local_port = mlxsw_sp_port->local_port, |
| 670 | .is_emad = false, |
| 671 | }; |
| 672 | u64 len; |
| 673 | int err; |
| 674 | |
Jiri Pirko | 307c243 | 2016-04-08 19:11:22 +0200 | [diff] [blame] | 675 | if (mlxsw_core_skb_transmit_busy(mlxsw_sp->core, &tx_info)) |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 676 | return NETDEV_TX_BUSY; |
| 677 | |
| 678 | if (unlikely(skb_headroom(skb) < MLXSW_TXHDR_LEN)) { |
| 679 | struct sk_buff *skb_orig = skb; |
| 680 | |
| 681 | skb = skb_realloc_headroom(skb, MLXSW_TXHDR_LEN); |
| 682 | if (!skb) { |
| 683 | this_cpu_inc(mlxsw_sp_port->pcpu_stats->tx_dropped); |
| 684 | dev_kfree_skb_any(skb_orig); |
| 685 | return NETDEV_TX_OK; |
| 686 | } |
| 687 | } |
| 688 | |
| 689 | if (eth_skb_pad(skb)) { |
| 690 | this_cpu_inc(mlxsw_sp_port->pcpu_stats->tx_dropped); |
| 691 | return NETDEV_TX_OK; |
| 692 | } |
| 693 | |
| 694 | mlxsw_sp_txhdr_construct(skb, &tx_info); |
Nogah Frankel | 63dcdd3 | 2016-06-17 15:09:05 +0200 | [diff] [blame] | 695 | /* TX header is consumed by HW on the way so we shouldn't count its |
| 696 | * bytes as being sent. |
| 697 | */ |
| 698 | len = skb->len - MLXSW_TXHDR_LEN; |
| 699 | |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 700 | /* Due to a race we might fail here because of a full queue. In that |
| 701 | * unlikely case we simply drop the packet. |
| 702 | */ |
Jiri Pirko | 307c243 | 2016-04-08 19:11:22 +0200 | [diff] [blame] | 703 | err = mlxsw_core_skb_transmit(mlxsw_sp->core, skb, &tx_info); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 704 | |
| 705 | if (!err) { |
| 706 | pcpu_stats = this_cpu_ptr(mlxsw_sp_port->pcpu_stats); |
| 707 | u64_stats_update_begin(&pcpu_stats->syncp); |
| 708 | pcpu_stats->tx_packets++; |
| 709 | pcpu_stats->tx_bytes += len; |
| 710 | u64_stats_update_end(&pcpu_stats->syncp); |
| 711 | } else { |
| 712 | this_cpu_inc(mlxsw_sp_port->pcpu_stats->tx_dropped); |
| 713 | dev_kfree_skb_any(skb); |
| 714 | } |
| 715 | return NETDEV_TX_OK; |
| 716 | } |
| 717 | |
Jiri Pirko | c5b9b51 | 2015-12-03 12:12:22 +0100 | [diff] [blame] | 718 | static void mlxsw_sp_set_rx_mode(struct net_device *dev) |
| 719 | { |
| 720 | } |
| 721 | |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 722 | static int mlxsw_sp_port_set_mac_address(struct net_device *dev, void *p) |
| 723 | { |
| 724 | struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev); |
| 725 | struct sockaddr *addr = p; |
| 726 | int err; |
| 727 | |
| 728 | if (!is_valid_ether_addr(addr->sa_data)) |
| 729 | return -EADDRNOTAVAIL; |
| 730 | |
| 731 | err = mlxsw_sp_port_dev_addr_set(mlxsw_sp_port, addr->sa_data); |
| 732 | if (err) |
| 733 | return err; |
| 734 | memcpy(dev->dev_addr, addr->sa_data, dev->addr_len); |
| 735 | return 0; |
| 736 | } |
| 737 | |
Ido Schimmel | 9f7ec05 | 2016-04-06 17:10:14 +0200 | [diff] [blame] | 738 | 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] | 739 | bool pause_en, bool pfc_en, u16 delay) |
Ido Schimmel | 8e8dfe9 | 2016-04-06 17:10:10 +0200 | [diff] [blame] | 740 | { |
| 741 | u16 pg_size = 2 * MLXSW_SP_BYTES_TO_CELLS(mtu); |
| 742 | |
Ido Schimmel | d81a6bd | 2016-04-06 17:10:16 +0200 | [diff] [blame] | 743 | delay = pfc_en ? mlxsw_sp_pfc_delay_get(mtu, delay) : |
| 744 | MLXSW_SP_PAUSE_DELAY; |
Ido Schimmel | 9f7ec05 | 2016-04-06 17:10:14 +0200 | [diff] [blame] | 745 | |
Ido Schimmel | d81a6bd | 2016-04-06 17:10:16 +0200 | [diff] [blame] | 746 | if (pause_en || pfc_en) |
Ido Schimmel | 9f7ec05 | 2016-04-06 17:10:14 +0200 | [diff] [blame] | 747 | mlxsw_reg_pbmc_lossless_buffer_pack(pbmc_pl, pg_index, |
Ido Schimmel | d81a6bd | 2016-04-06 17:10:16 +0200 | [diff] [blame] | 748 | pg_size + delay, pg_size); |
| 749 | else |
Ido Schimmel | 9f7ec05 | 2016-04-06 17:10:14 +0200 | [diff] [blame] | 750 | mlxsw_reg_pbmc_lossy_buffer_pack(pbmc_pl, pg_index, pg_size); |
Ido Schimmel | 8e8dfe9 | 2016-04-06 17:10:10 +0200 | [diff] [blame] | 751 | } |
| 752 | |
| 753 | 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] | 754 | u8 *prio_tc, bool pause_en, |
| 755 | struct ieee_pfc *my_pfc) |
Ido Schimmel | ff6551e | 2016-04-06 17:10:03 +0200 | [diff] [blame] | 756 | { |
| 757 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
Ido Schimmel | d81a6bd | 2016-04-06 17:10:16 +0200 | [diff] [blame] | 758 | u8 pfc_en = !!my_pfc ? my_pfc->pfc_en : 0; |
| 759 | u16 delay = !!my_pfc ? my_pfc->delay : 0; |
Ido Schimmel | ff6551e | 2016-04-06 17:10:03 +0200 | [diff] [blame] | 760 | char pbmc_pl[MLXSW_REG_PBMC_LEN]; |
Ido Schimmel | 8e8dfe9 | 2016-04-06 17:10:10 +0200 | [diff] [blame] | 761 | int i, j, err; |
Ido Schimmel | ff6551e | 2016-04-06 17:10:03 +0200 | [diff] [blame] | 762 | |
| 763 | mlxsw_reg_pbmc_pack(pbmc_pl, mlxsw_sp_port->local_port, 0, 0); |
| 764 | err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(pbmc), pbmc_pl); |
| 765 | if (err) |
| 766 | return err; |
Ido Schimmel | 8e8dfe9 | 2016-04-06 17:10:10 +0200 | [diff] [blame] | 767 | |
| 768 | for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) { |
| 769 | bool configure = false; |
Ido Schimmel | d81a6bd | 2016-04-06 17:10:16 +0200 | [diff] [blame] | 770 | bool pfc = false; |
Ido Schimmel | 8e8dfe9 | 2016-04-06 17:10:10 +0200 | [diff] [blame] | 771 | |
| 772 | for (j = 0; j < IEEE_8021QAZ_MAX_TCS; j++) { |
| 773 | if (prio_tc[j] == i) { |
Ido Schimmel | d81a6bd | 2016-04-06 17:10:16 +0200 | [diff] [blame] | 774 | pfc = pfc_en & BIT(j); |
Ido Schimmel | 8e8dfe9 | 2016-04-06 17:10:10 +0200 | [diff] [blame] | 775 | configure = true; |
| 776 | break; |
| 777 | } |
| 778 | } |
| 779 | |
| 780 | if (!configure) |
| 781 | continue; |
Ido Schimmel | d81a6bd | 2016-04-06 17:10:16 +0200 | [diff] [blame] | 782 | 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] | 783 | } |
| 784 | |
Ido Schimmel | ff6551e | 2016-04-06 17:10:03 +0200 | [diff] [blame] | 785 | return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(pbmc), pbmc_pl); |
| 786 | } |
| 787 | |
Ido Schimmel | 8e8dfe9 | 2016-04-06 17:10:10 +0200 | [diff] [blame] | 788 | 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] | 789 | int mtu, bool pause_en) |
Ido Schimmel | 8e8dfe9 | 2016-04-06 17:10:10 +0200 | [diff] [blame] | 790 | { |
| 791 | u8 def_prio_tc[IEEE_8021QAZ_MAX_TCS] = {0}; |
| 792 | bool dcb_en = !!mlxsw_sp_port->dcb.ets; |
Ido Schimmel | d81a6bd | 2016-04-06 17:10:16 +0200 | [diff] [blame] | 793 | struct ieee_pfc *my_pfc; |
Ido Schimmel | 8e8dfe9 | 2016-04-06 17:10:10 +0200 | [diff] [blame] | 794 | u8 *prio_tc; |
| 795 | |
| 796 | 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] | 797 | my_pfc = dcb_en ? mlxsw_sp_port->dcb.pfc : NULL; |
Ido Schimmel | 8e8dfe9 | 2016-04-06 17:10:10 +0200 | [diff] [blame] | 798 | |
Ido Schimmel | 9f7ec05 | 2016-04-06 17:10:14 +0200 | [diff] [blame] | 799 | return __mlxsw_sp_port_headroom_set(mlxsw_sp_port, mtu, prio_tc, |
Ido Schimmel | d81a6bd | 2016-04-06 17:10:16 +0200 | [diff] [blame] | 800 | pause_en, my_pfc); |
Ido Schimmel | 8e8dfe9 | 2016-04-06 17:10:10 +0200 | [diff] [blame] | 801 | } |
| 802 | |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 803 | static int mlxsw_sp_port_change_mtu(struct net_device *dev, int mtu) |
| 804 | { |
| 805 | struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev); |
Ido Schimmel | 9f7ec05 | 2016-04-06 17:10:14 +0200 | [diff] [blame] | 806 | bool pause_en = mlxsw_sp_port_is_pause_en(mlxsw_sp_port); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 807 | int err; |
| 808 | |
Ido Schimmel | 9f7ec05 | 2016-04-06 17:10:14 +0200 | [diff] [blame] | 809 | err = mlxsw_sp_port_headroom_set(mlxsw_sp_port, mtu, pause_en); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 810 | if (err) |
| 811 | return err; |
Yotam Gigi | 763b4b7 | 2016-07-21 12:03:17 +0200 | [diff] [blame] | 812 | err = mlxsw_sp_span_port_mtu_update(mlxsw_sp_port, mtu); |
| 813 | if (err) |
| 814 | goto err_span_port_mtu_update; |
Ido Schimmel | ff6551e | 2016-04-06 17:10:03 +0200 | [diff] [blame] | 815 | err = mlxsw_sp_port_mtu_set(mlxsw_sp_port, mtu); |
| 816 | if (err) |
| 817 | goto err_port_mtu_set; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 818 | dev->mtu = mtu; |
| 819 | return 0; |
Ido Schimmel | ff6551e | 2016-04-06 17:10:03 +0200 | [diff] [blame] | 820 | |
| 821 | err_port_mtu_set: |
Yotam Gigi | 763b4b7 | 2016-07-21 12:03:17 +0200 | [diff] [blame] | 822 | mlxsw_sp_span_port_mtu_update(mlxsw_sp_port, dev->mtu); |
| 823 | err_span_port_mtu_update: |
Ido Schimmel | 9f7ec05 | 2016-04-06 17:10:14 +0200 | [diff] [blame] | 824 | mlxsw_sp_port_headroom_set(mlxsw_sp_port, dev->mtu, pause_en); |
Ido Schimmel | ff6551e | 2016-04-06 17:10:03 +0200 | [diff] [blame] | 825 | return err; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 826 | } |
| 827 | |
Or Gerlitz | 4bdcc6c | 2016-09-20 08:14:08 +0300 | [diff] [blame] | 828 | static int |
Nogah Frankel | fc1bbb0 | 2016-09-16 15:05:38 +0200 | [diff] [blame] | 829 | mlxsw_sp_port_get_sw_stats64(const struct net_device *dev, |
| 830 | struct rtnl_link_stats64 *stats) |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 831 | { |
| 832 | struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev); |
| 833 | struct mlxsw_sp_port_pcpu_stats *p; |
| 834 | u64 rx_packets, rx_bytes, tx_packets, tx_bytes; |
| 835 | u32 tx_dropped = 0; |
| 836 | unsigned int start; |
| 837 | int i; |
| 838 | |
| 839 | for_each_possible_cpu(i) { |
| 840 | p = per_cpu_ptr(mlxsw_sp_port->pcpu_stats, i); |
| 841 | do { |
| 842 | start = u64_stats_fetch_begin_irq(&p->syncp); |
| 843 | rx_packets = p->rx_packets; |
| 844 | rx_bytes = p->rx_bytes; |
| 845 | tx_packets = p->tx_packets; |
| 846 | tx_bytes = p->tx_bytes; |
| 847 | } while (u64_stats_fetch_retry_irq(&p->syncp, start)); |
| 848 | |
| 849 | stats->rx_packets += rx_packets; |
| 850 | stats->rx_bytes += rx_bytes; |
| 851 | stats->tx_packets += tx_packets; |
| 852 | stats->tx_bytes += tx_bytes; |
| 853 | /* tx_dropped is u32, updated without syncp protection. */ |
| 854 | tx_dropped += p->tx_dropped; |
| 855 | } |
| 856 | stats->tx_dropped = tx_dropped; |
Nogah Frankel | fc1bbb0 | 2016-09-16 15:05:38 +0200 | [diff] [blame] | 857 | return 0; |
| 858 | } |
| 859 | |
Or Gerlitz | 3df5b3c | 2016-11-22 23:09:54 +0200 | [diff] [blame] | 860 | static bool mlxsw_sp_port_has_offload_stats(const struct net_device *dev, int attr_id) |
Nogah Frankel | fc1bbb0 | 2016-09-16 15:05:38 +0200 | [diff] [blame] | 861 | { |
| 862 | switch (attr_id) { |
| 863 | case IFLA_OFFLOAD_XSTATS_CPU_HIT: |
| 864 | return true; |
| 865 | } |
| 866 | |
| 867 | return false; |
| 868 | } |
| 869 | |
Or Gerlitz | 4bdcc6c | 2016-09-20 08:14:08 +0300 | [diff] [blame] | 870 | static int mlxsw_sp_port_get_offload_stats(int attr_id, const struct net_device *dev, |
| 871 | void *sp) |
Nogah Frankel | fc1bbb0 | 2016-09-16 15:05:38 +0200 | [diff] [blame] | 872 | { |
| 873 | switch (attr_id) { |
| 874 | case IFLA_OFFLOAD_XSTATS_CPU_HIT: |
| 875 | return mlxsw_sp_port_get_sw_stats64(dev, sp); |
| 876 | } |
| 877 | |
| 878 | return -EINVAL; |
| 879 | } |
| 880 | |
| 881 | static int mlxsw_sp_port_get_stats_raw(struct net_device *dev, int grp, |
| 882 | int prio, char *ppcnt_pl) |
| 883 | { |
| 884 | struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev); |
| 885 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
| 886 | |
| 887 | mlxsw_reg_ppcnt_pack(ppcnt_pl, mlxsw_sp_port->local_port, grp, prio); |
| 888 | return mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(ppcnt), ppcnt_pl); |
| 889 | } |
| 890 | |
| 891 | static int mlxsw_sp_port_get_hw_stats(struct net_device *dev, |
| 892 | struct rtnl_link_stats64 *stats) |
| 893 | { |
| 894 | char ppcnt_pl[MLXSW_REG_PPCNT_LEN]; |
| 895 | int err; |
| 896 | |
| 897 | err = mlxsw_sp_port_get_stats_raw(dev, MLXSW_REG_PPCNT_IEEE_8023_CNT, |
| 898 | 0, ppcnt_pl); |
| 899 | if (err) |
| 900 | goto out; |
| 901 | |
| 902 | stats->tx_packets = |
| 903 | mlxsw_reg_ppcnt_a_frames_transmitted_ok_get(ppcnt_pl); |
| 904 | stats->rx_packets = |
| 905 | mlxsw_reg_ppcnt_a_frames_received_ok_get(ppcnt_pl); |
| 906 | stats->tx_bytes = |
| 907 | mlxsw_reg_ppcnt_a_octets_transmitted_ok_get(ppcnt_pl); |
| 908 | stats->rx_bytes = |
| 909 | mlxsw_reg_ppcnt_a_octets_received_ok_get(ppcnt_pl); |
| 910 | stats->multicast = |
| 911 | mlxsw_reg_ppcnt_a_multicast_frames_received_ok_get(ppcnt_pl); |
| 912 | |
| 913 | stats->rx_crc_errors = |
| 914 | mlxsw_reg_ppcnt_a_frame_check_sequence_errors_get(ppcnt_pl); |
| 915 | stats->rx_frame_errors = |
| 916 | mlxsw_reg_ppcnt_a_alignment_errors_get(ppcnt_pl); |
| 917 | |
| 918 | stats->rx_length_errors = ( |
| 919 | mlxsw_reg_ppcnt_a_in_range_length_errors_get(ppcnt_pl) + |
| 920 | mlxsw_reg_ppcnt_a_out_of_range_length_field_get(ppcnt_pl) + |
| 921 | mlxsw_reg_ppcnt_a_frame_too_long_errors_get(ppcnt_pl)); |
| 922 | |
| 923 | stats->rx_errors = (stats->rx_crc_errors + |
| 924 | stats->rx_frame_errors + stats->rx_length_errors); |
| 925 | |
| 926 | out: |
| 927 | return err; |
| 928 | } |
| 929 | |
| 930 | static void update_stats_cache(struct work_struct *work) |
| 931 | { |
| 932 | struct mlxsw_sp_port *mlxsw_sp_port = |
| 933 | container_of(work, struct mlxsw_sp_port, |
| 934 | hw_stats.update_dw.work); |
| 935 | |
| 936 | if (!netif_carrier_ok(mlxsw_sp_port->dev)) |
| 937 | goto out; |
| 938 | |
| 939 | mlxsw_sp_port_get_hw_stats(mlxsw_sp_port->dev, |
| 940 | mlxsw_sp_port->hw_stats.cache); |
| 941 | |
| 942 | out: |
| 943 | mlxsw_core_schedule_dw(&mlxsw_sp_port->hw_stats.update_dw, |
| 944 | MLXSW_HW_STATS_UPDATE_TIME); |
| 945 | } |
| 946 | |
| 947 | /* Return the stats from a cache that is updated periodically, |
| 948 | * as this function might get called in an atomic context. |
| 949 | */ |
| 950 | static struct rtnl_link_stats64 * |
| 951 | mlxsw_sp_port_get_stats64(struct net_device *dev, |
| 952 | struct rtnl_link_stats64 *stats) |
| 953 | { |
| 954 | struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev); |
| 955 | |
| 956 | memcpy(stats, mlxsw_sp_port->hw_stats.cache, sizeof(*stats)); |
| 957 | |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 958 | return stats; |
| 959 | } |
| 960 | |
| 961 | int mlxsw_sp_port_vlan_set(struct mlxsw_sp_port *mlxsw_sp_port, u16 vid_begin, |
| 962 | u16 vid_end, bool is_member, bool untagged) |
| 963 | { |
| 964 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
| 965 | char *spvm_pl; |
| 966 | int err; |
| 967 | |
| 968 | spvm_pl = kmalloc(MLXSW_REG_SPVM_LEN, GFP_KERNEL); |
| 969 | if (!spvm_pl) |
| 970 | return -ENOMEM; |
| 971 | |
| 972 | mlxsw_reg_spvm_pack(spvm_pl, mlxsw_sp_port->local_port, vid_begin, |
| 973 | vid_end, is_member, untagged); |
| 974 | err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(spvm), spvm_pl); |
| 975 | kfree(spvm_pl); |
| 976 | return err; |
| 977 | } |
| 978 | |
| 979 | static int mlxsw_sp_port_vp_mode_trans(struct mlxsw_sp_port *mlxsw_sp_port) |
| 980 | { |
| 981 | enum mlxsw_reg_svfa_mt mt = MLXSW_REG_SVFA_MT_PORT_VID_TO_FID; |
| 982 | u16 vid, last_visited_vid; |
| 983 | int err; |
| 984 | |
| 985 | for_each_set_bit(vid, mlxsw_sp_port->active_vlans, VLAN_N_VID) { |
| 986 | err = mlxsw_sp_port_vid_to_fid_set(mlxsw_sp_port, mt, true, vid, |
| 987 | vid); |
| 988 | if (err) { |
| 989 | last_visited_vid = vid; |
| 990 | goto err_port_vid_to_fid_set; |
| 991 | } |
| 992 | } |
| 993 | |
| 994 | err = mlxsw_sp_port_vp_mode_set(mlxsw_sp_port, true); |
| 995 | if (err) { |
| 996 | last_visited_vid = VLAN_N_VID; |
| 997 | goto err_port_vid_to_fid_set; |
| 998 | } |
| 999 | |
| 1000 | return 0; |
| 1001 | |
| 1002 | err_port_vid_to_fid_set: |
| 1003 | for_each_set_bit(vid, mlxsw_sp_port->active_vlans, last_visited_vid) |
| 1004 | mlxsw_sp_port_vid_to_fid_set(mlxsw_sp_port, mt, false, vid, |
| 1005 | vid); |
| 1006 | return err; |
| 1007 | } |
| 1008 | |
| 1009 | static int mlxsw_sp_port_vlan_mode_trans(struct mlxsw_sp_port *mlxsw_sp_port) |
| 1010 | { |
| 1011 | enum mlxsw_reg_svfa_mt mt = MLXSW_REG_SVFA_MT_PORT_VID_TO_FID; |
| 1012 | u16 vid; |
| 1013 | int err; |
| 1014 | |
| 1015 | err = mlxsw_sp_port_vp_mode_set(mlxsw_sp_port, false); |
| 1016 | if (err) |
| 1017 | return err; |
| 1018 | |
| 1019 | for_each_set_bit(vid, mlxsw_sp_port->active_vlans, VLAN_N_VID) { |
| 1020 | err = mlxsw_sp_port_vid_to_fid_set(mlxsw_sp_port, mt, false, |
| 1021 | vid, vid); |
| 1022 | if (err) |
| 1023 | return err; |
| 1024 | } |
| 1025 | |
| 1026 | return 0; |
| 1027 | } |
| 1028 | |
Ido Schimmel | 7f71eb4 | 2015-12-15 16:03:37 +0100 | [diff] [blame] | 1029 | static struct mlxsw_sp_port * |
Ido Schimmel | 0355b59 | 2016-06-20 23:04:13 +0200 | [diff] [blame] | 1030 | mlxsw_sp_port_vport_create(struct mlxsw_sp_port *mlxsw_sp_port, u16 vid) |
Ido Schimmel | 7f71eb4 | 2015-12-15 16:03:37 +0100 | [diff] [blame] | 1031 | { |
| 1032 | struct mlxsw_sp_port *mlxsw_sp_vport; |
| 1033 | |
| 1034 | mlxsw_sp_vport = kzalloc(sizeof(*mlxsw_sp_vport), GFP_KERNEL); |
| 1035 | if (!mlxsw_sp_vport) |
| 1036 | return NULL; |
| 1037 | |
| 1038 | /* dev will be set correctly after the VLAN device is linked |
| 1039 | * with the real device. In case of bridge SELF invocation, dev |
| 1040 | * will remain as is. |
| 1041 | */ |
| 1042 | mlxsw_sp_vport->dev = mlxsw_sp_port->dev; |
| 1043 | mlxsw_sp_vport->mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
| 1044 | mlxsw_sp_vport->local_port = mlxsw_sp_port->local_port; |
| 1045 | mlxsw_sp_vport->stp_state = BR_STATE_FORWARDING; |
Ido Schimmel | 272c447 | 2015-12-15 16:03:47 +0100 | [diff] [blame] | 1046 | mlxsw_sp_vport->lagged = mlxsw_sp_port->lagged; |
| 1047 | mlxsw_sp_vport->lag_id = mlxsw_sp_port->lag_id; |
Ido Schimmel | 0355b59 | 2016-06-20 23:04:13 +0200 | [diff] [blame] | 1048 | mlxsw_sp_vport->vport.vid = vid; |
Ido Schimmel | 7f71eb4 | 2015-12-15 16:03:37 +0100 | [diff] [blame] | 1049 | |
| 1050 | list_add(&mlxsw_sp_vport->vport.list, &mlxsw_sp_port->vports_list); |
| 1051 | |
| 1052 | return mlxsw_sp_vport; |
| 1053 | } |
| 1054 | |
| 1055 | static void mlxsw_sp_port_vport_destroy(struct mlxsw_sp_port *mlxsw_sp_vport) |
| 1056 | { |
| 1057 | list_del(&mlxsw_sp_vport->vport.list); |
| 1058 | kfree(mlxsw_sp_vport); |
| 1059 | } |
| 1060 | |
Ido Schimmel | 0597848 | 2016-08-17 16:39:30 +0200 | [diff] [blame] | 1061 | static int mlxsw_sp_port_add_vid(struct net_device *dev, |
| 1062 | __be16 __always_unused proto, u16 vid) |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1063 | { |
| 1064 | struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev); |
Ido Schimmel | 7f71eb4 | 2015-12-15 16:03:37 +0100 | [diff] [blame] | 1065 | struct mlxsw_sp_port *mlxsw_sp_vport; |
Ido Schimmel | 52697a9 | 2016-07-02 11:00:09 +0200 | [diff] [blame] | 1066 | bool untagged = vid == 1; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1067 | int err; |
| 1068 | |
| 1069 | /* VLAN 0 is added to HW filter when device goes up, but it is |
| 1070 | * reserved in our case, so simply return. |
| 1071 | */ |
| 1072 | if (!vid) |
| 1073 | return 0; |
| 1074 | |
Ido Schimmel | fa66d7e | 2016-08-17 16:39:29 +0200 | [diff] [blame] | 1075 | if (mlxsw_sp_port_vport_find(mlxsw_sp_port, vid)) |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1076 | return 0; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1077 | |
Ido Schimmel | 0355b59 | 2016-06-20 23:04:13 +0200 | [diff] [blame] | 1078 | mlxsw_sp_vport = mlxsw_sp_port_vport_create(mlxsw_sp_port, vid); |
Ido Schimmel | fa66d7e | 2016-08-17 16:39:29 +0200 | [diff] [blame] | 1079 | if (!mlxsw_sp_vport) |
Ido Schimmel | 0355b59 | 2016-06-20 23:04:13 +0200 | [diff] [blame] | 1080 | return -ENOMEM; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1081 | |
| 1082 | /* When adding the first VLAN interface on a bridged port we need to |
| 1083 | * transition all the active 802.1Q bridge VLANs to use explicit |
| 1084 | * {Port, VID} to FID mappings and set the port's mode to Virtual mode. |
| 1085 | */ |
Ido Schimmel | 7f71eb4 | 2015-12-15 16:03:37 +0100 | [diff] [blame] | 1086 | if (list_is_singular(&mlxsw_sp_port->vports_list)) { |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1087 | err = mlxsw_sp_port_vp_mode_trans(mlxsw_sp_port); |
Ido Schimmel | fa66d7e | 2016-08-17 16:39:29 +0200 | [diff] [blame] | 1088 | if (err) |
Ido Schimmel | 7f71eb4 | 2015-12-15 16:03:37 +0100 | [diff] [blame] | 1089 | goto err_port_vp_mode_trans; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1090 | } |
| 1091 | |
Ido Schimmel | 52697a9 | 2016-07-02 11:00:09 +0200 | [diff] [blame] | 1092 | err = mlxsw_sp_port_vlan_set(mlxsw_sp_vport, vid, vid, true, untagged); |
Ido Schimmel | fa66d7e | 2016-08-17 16:39:29 +0200 | [diff] [blame] | 1093 | if (err) |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1094 | goto err_port_add_vid; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1095 | |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1096 | return 0; |
| 1097 | |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1098 | err_port_add_vid: |
Ido Schimmel | 7f71eb4 | 2015-12-15 16:03:37 +0100 | [diff] [blame] | 1099 | if (list_is_singular(&mlxsw_sp_port->vports_list)) |
| 1100 | mlxsw_sp_port_vlan_mode_trans(mlxsw_sp_port); |
| 1101 | err_port_vp_mode_trans: |
Ido Schimmel | 7f71eb4 | 2015-12-15 16:03:37 +0100 | [diff] [blame] | 1102 | mlxsw_sp_port_vport_destroy(mlxsw_sp_vport); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1103 | return err; |
| 1104 | } |
| 1105 | |
Ido Schimmel | 32d863f | 2016-07-02 11:00:10 +0200 | [diff] [blame] | 1106 | static int mlxsw_sp_port_kill_vid(struct net_device *dev, |
| 1107 | __be16 __always_unused proto, u16 vid) |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1108 | { |
| 1109 | struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev); |
Ido Schimmel | 7f71eb4 | 2015-12-15 16:03:37 +0100 | [diff] [blame] | 1110 | struct mlxsw_sp_port *mlxsw_sp_vport; |
Ido Schimmel | 1c80075 | 2016-06-20 23:04:20 +0200 | [diff] [blame] | 1111 | struct mlxsw_sp_fid *f; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1112 | |
| 1113 | /* VLAN 0 is removed from HW filter when device goes down, but |
| 1114 | * it is reserved in our case, so simply return. |
| 1115 | */ |
| 1116 | if (!vid) |
| 1117 | return 0; |
| 1118 | |
Ido Schimmel | 7f71eb4 | 2015-12-15 16:03:37 +0100 | [diff] [blame] | 1119 | mlxsw_sp_vport = mlxsw_sp_port_vport_find(mlxsw_sp_port, vid); |
Ido Schimmel | 7a35583 | 2016-08-17 16:39:28 +0200 | [diff] [blame] | 1120 | if (WARN_ON(!mlxsw_sp_vport)) |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1121 | return 0; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1122 | |
Ido Schimmel | 7a35583 | 2016-08-17 16:39:28 +0200 | [diff] [blame] | 1123 | mlxsw_sp_port_vlan_set(mlxsw_sp_vport, vid, vid, false, false); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1124 | |
Ido Schimmel | 1c80075 | 2016-06-20 23:04:20 +0200 | [diff] [blame] | 1125 | /* Drop FID reference. If this was the last reference the |
| 1126 | * resources will be freed. |
| 1127 | */ |
| 1128 | f = mlxsw_sp_vport_fid_get(mlxsw_sp_vport); |
| 1129 | if (f && !WARN_ON(!f->leave)) |
| 1130 | f->leave(mlxsw_sp_vport); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1131 | |
| 1132 | /* When removing the last VLAN interface on a bridged port we need to |
| 1133 | * transition all active 802.1Q bridge VLANs to use VID to FID |
| 1134 | * mappings and set port's mode to VLAN mode. |
| 1135 | */ |
Ido Schimmel | 7a35583 | 2016-08-17 16:39:28 +0200 | [diff] [blame] | 1136 | if (list_is_singular(&mlxsw_sp_port->vports_list)) |
| 1137 | mlxsw_sp_port_vlan_mode_trans(mlxsw_sp_port); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1138 | |
Ido Schimmel | 7f71eb4 | 2015-12-15 16:03:37 +0100 | [diff] [blame] | 1139 | mlxsw_sp_port_vport_destroy(mlxsw_sp_vport); |
| 1140 | |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1141 | return 0; |
| 1142 | } |
| 1143 | |
Ido Schimmel | 2bf9a58 | 2016-04-05 10:20:04 +0200 | [diff] [blame] | 1144 | static int mlxsw_sp_port_get_phys_port_name(struct net_device *dev, char *name, |
| 1145 | size_t len) |
| 1146 | { |
| 1147 | struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev); |
Ido Schimmel | d664b41 | 2016-06-09 09:51:40 +0200 | [diff] [blame] | 1148 | u8 module = mlxsw_sp_port->mapping.module; |
| 1149 | u8 width = mlxsw_sp_port->mapping.width; |
| 1150 | u8 lane = mlxsw_sp_port->mapping.lane; |
Ido Schimmel | 2bf9a58 | 2016-04-05 10:20:04 +0200 | [diff] [blame] | 1151 | int err; |
| 1152 | |
Ido Schimmel | 2bf9a58 | 2016-04-05 10:20:04 +0200 | [diff] [blame] | 1153 | if (!mlxsw_sp_port->split) |
| 1154 | err = snprintf(name, len, "p%d", module + 1); |
| 1155 | else |
| 1156 | err = snprintf(name, len, "p%ds%d", module + 1, |
| 1157 | lane / width); |
| 1158 | |
| 1159 | if (err >= len) |
| 1160 | return -EINVAL; |
| 1161 | |
| 1162 | return 0; |
| 1163 | } |
| 1164 | |
Yotam Gigi | 763b4b7 | 2016-07-21 12:03:17 +0200 | [diff] [blame] | 1165 | static struct mlxsw_sp_port_mall_tc_entry * |
| 1166 | mlxsw_sp_port_mirror_entry_find(struct mlxsw_sp_port *port, |
| 1167 | unsigned long cookie) { |
| 1168 | struct mlxsw_sp_port_mall_tc_entry *mall_tc_entry; |
| 1169 | |
| 1170 | list_for_each_entry(mall_tc_entry, &port->mall_tc_list, list) |
| 1171 | if (mall_tc_entry->cookie == cookie) |
| 1172 | return mall_tc_entry; |
| 1173 | |
| 1174 | return NULL; |
| 1175 | } |
| 1176 | |
| 1177 | static int |
| 1178 | mlxsw_sp_port_add_cls_matchall_mirror(struct mlxsw_sp_port *mlxsw_sp_port, |
| 1179 | struct tc_cls_matchall_offload *cls, |
| 1180 | const struct tc_action *a, |
| 1181 | bool ingress) |
| 1182 | { |
| 1183 | struct mlxsw_sp_port_mall_tc_entry *mall_tc_entry; |
| 1184 | struct net *net = dev_net(mlxsw_sp_port->dev); |
| 1185 | enum mlxsw_sp_span_type span_type; |
| 1186 | struct mlxsw_sp_port *to_port; |
| 1187 | struct net_device *to_dev; |
| 1188 | int ifindex; |
| 1189 | int err; |
| 1190 | |
| 1191 | ifindex = tcf_mirred_ifindex(a); |
| 1192 | to_dev = __dev_get_by_index(net, ifindex); |
| 1193 | if (!to_dev) { |
| 1194 | netdev_err(mlxsw_sp_port->dev, "Could not find requested device\n"); |
| 1195 | return -EINVAL; |
| 1196 | } |
| 1197 | |
| 1198 | if (!mlxsw_sp_port_dev_check(to_dev)) { |
| 1199 | netdev_err(mlxsw_sp_port->dev, "Cannot mirror to a non-spectrum port"); |
| 1200 | return -ENOTSUPP; |
| 1201 | } |
| 1202 | to_port = netdev_priv(to_dev); |
| 1203 | |
| 1204 | mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL); |
| 1205 | if (!mall_tc_entry) |
| 1206 | return -ENOMEM; |
| 1207 | |
| 1208 | mall_tc_entry->cookie = cls->cookie; |
| 1209 | mall_tc_entry->type = MLXSW_SP_PORT_MALL_MIRROR; |
| 1210 | mall_tc_entry->mirror.to_local_port = to_port->local_port; |
| 1211 | mall_tc_entry->mirror.ingress = ingress; |
| 1212 | list_add_tail(&mall_tc_entry->list, &mlxsw_sp_port->mall_tc_list); |
| 1213 | |
| 1214 | span_type = ingress ? MLXSW_SP_SPAN_INGRESS : MLXSW_SP_SPAN_EGRESS; |
| 1215 | err = mlxsw_sp_span_mirror_add(mlxsw_sp_port, to_port, span_type); |
| 1216 | if (err) |
| 1217 | goto err_mirror_add; |
| 1218 | return 0; |
| 1219 | |
| 1220 | err_mirror_add: |
| 1221 | list_del(&mall_tc_entry->list); |
| 1222 | kfree(mall_tc_entry); |
| 1223 | return err; |
| 1224 | } |
| 1225 | |
| 1226 | static int mlxsw_sp_port_add_cls_matchall(struct mlxsw_sp_port *mlxsw_sp_port, |
| 1227 | __be16 protocol, |
| 1228 | struct tc_cls_matchall_offload *cls, |
| 1229 | bool ingress) |
| 1230 | { |
Yotam Gigi | 763b4b7 | 2016-07-21 12:03:17 +0200 | [diff] [blame] | 1231 | const struct tc_action *a; |
WANG Cong | 22dc13c | 2016-08-13 22:35:00 -0700 | [diff] [blame] | 1232 | LIST_HEAD(actions); |
Yotam Gigi | 763b4b7 | 2016-07-21 12:03:17 +0200 | [diff] [blame] | 1233 | int err; |
| 1234 | |
Ido Schimmel | 86cb13e | 2016-07-25 13:12:33 +0300 | [diff] [blame] | 1235 | if (!tc_single_action(cls->exts)) { |
Yotam Gigi | 763b4b7 | 2016-07-21 12:03:17 +0200 | [diff] [blame] | 1236 | netdev_err(mlxsw_sp_port->dev, "only singular actions are supported\n"); |
| 1237 | return -ENOTSUPP; |
| 1238 | } |
| 1239 | |
WANG Cong | 22dc13c | 2016-08-13 22:35:00 -0700 | [diff] [blame] | 1240 | tcf_exts_to_list(cls->exts, &actions); |
| 1241 | list_for_each_entry(a, &actions, list) { |
Shmulik Ladkani | 5724b8b | 2016-10-13 09:06:43 +0300 | [diff] [blame] | 1242 | if (!is_tcf_mirred_egress_mirror(a) || |
| 1243 | protocol != htons(ETH_P_ALL)) { |
Ido Schimmel | 86cb13e | 2016-07-25 13:12:33 +0300 | [diff] [blame] | 1244 | return -ENOTSUPP; |
Shmulik Ladkani | 5724b8b | 2016-10-13 09:06:43 +0300 | [diff] [blame] | 1245 | } |
Ido Schimmel | 86cb13e | 2016-07-25 13:12:33 +0300 | [diff] [blame] | 1246 | |
Yotam Gigi | 763b4b7 | 2016-07-21 12:03:17 +0200 | [diff] [blame] | 1247 | err = mlxsw_sp_port_add_cls_matchall_mirror(mlxsw_sp_port, cls, |
| 1248 | a, ingress); |
| 1249 | if (err) |
| 1250 | return err; |
Yotam Gigi | 763b4b7 | 2016-07-21 12:03:17 +0200 | [diff] [blame] | 1251 | } |
| 1252 | |
| 1253 | return 0; |
| 1254 | } |
| 1255 | |
| 1256 | static void mlxsw_sp_port_del_cls_matchall(struct mlxsw_sp_port *mlxsw_sp_port, |
| 1257 | struct tc_cls_matchall_offload *cls) |
| 1258 | { |
| 1259 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
| 1260 | struct mlxsw_sp_port_mall_tc_entry *mall_tc_entry; |
| 1261 | enum mlxsw_sp_span_type span_type; |
| 1262 | struct mlxsw_sp_port *to_port; |
| 1263 | |
| 1264 | mall_tc_entry = mlxsw_sp_port_mirror_entry_find(mlxsw_sp_port, |
| 1265 | cls->cookie); |
| 1266 | if (!mall_tc_entry) { |
| 1267 | netdev_dbg(mlxsw_sp_port->dev, "tc entry not found on port\n"); |
| 1268 | return; |
| 1269 | } |
| 1270 | |
| 1271 | switch (mall_tc_entry->type) { |
| 1272 | case MLXSW_SP_PORT_MALL_MIRROR: |
| 1273 | to_port = mlxsw_sp->ports[mall_tc_entry->mirror.to_local_port]; |
| 1274 | span_type = mall_tc_entry->mirror.ingress ? |
| 1275 | MLXSW_SP_SPAN_INGRESS : MLXSW_SP_SPAN_EGRESS; |
| 1276 | |
| 1277 | mlxsw_sp_span_mirror_remove(mlxsw_sp_port, to_port, span_type); |
| 1278 | break; |
| 1279 | default: |
| 1280 | WARN_ON(1); |
| 1281 | } |
| 1282 | |
| 1283 | list_del(&mall_tc_entry->list); |
| 1284 | kfree(mall_tc_entry); |
| 1285 | } |
| 1286 | |
| 1287 | static int mlxsw_sp_setup_tc(struct net_device *dev, u32 handle, |
| 1288 | __be16 proto, struct tc_to_netdev *tc) |
| 1289 | { |
| 1290 | struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev); |
| 1291 | bool ingress = TC_H_MAJ(handle) == TC_H_MAJ(TC_H_INGRESS); |
| 1292 | |
| 1293 | if (tc->type == TC_SETUP_MATCHALL) { |
| 1294 | switch (tc->cls_mall->command) { |
| 1295 | case TC_CLSMATCHALL_REPLACE: |
| 1296 | return mlxsw_sp_port_add_cls_matchall(mlxsw_sp_port, |
| 1297 | proto, |
| 1298 | tc->cls_mall, |
| 1299 | ingress); |
| 1300 | case TC_CLSMATCHALL_DESTROY: |
| 1301 | mlxsw_sp_port_del_cls_matchall(mlxsw_sp_port, |
| 1302 | tc->cls_mall); |
| 1303 | return 0; |
| 1304 | default: |
| 1305 | return -EINVAL; |
| 1306 | } |
| 1307 | } |
| 1308 | |
| 1309 | return -ENOTSUPP; |
| 1310 | } |
| 1311 | |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1312 | static const struct net_device_ops mlxsw_sp_port_netdev_ops = { |
| 1313 | .ndo_open = mlxsw_sp_port_open, |
| 1314 | .ndo_stop = mlxsw_sp_port_stop, |
| 1315 | .ndo_start_xmit = mlxsw_sp_port_xmit, |
Yotam Gigi | 763b4b7 | 2016-07-21 12:03:17 +0200 | [diff] [blame] | 1316 | .ndo_setup_tc = mlxsw_sp_setup_tc, |
Jiri Pirko | c5b9b51 | 2015-12-03 12:12:22 +0100 | [diff] [blame] | 1317 | .ndo_set_rx_mode = mlxsw_sp_set_rx_mode, |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1318 | .ndo_set_mac_address = mlxsw_sp_port_set_mac_address, |
| 1319 | .ndo_change_mtu = mlxsw_sp_port_change_mtu, |
| 1320 | .ndo_get_stats64 = mlxsw_sp_port_get_stats64, |
Nogah Frankel | fc1bbb0 | 2016-09-16 15:05:38 +0200 | [diff] [blame] | 1321 | .ndo_has_offload_stats = mlxsw_sp_port_has_offload_stats, |
| 1322 | .ndo_get_offload_stats = mlxsw_sp_port_get_offload_stats, |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1323 | .ndo_vlan_rx_add_vid = mlxsw_sp_port_add_vid, |
| 1324 | .ndo_vlan_rx_kill_vid = mlxsw_sp_port_kill_vid, |
Jiri Pirko | 6cf3c97 | 2016-07-05 11:27:39 +0200 | [diff] [blame] | 1325 | .ndo_neigh_construct = mlxsw_sp_router_neigh_construct, |
| 1326 | .ndo_neigh_destroy = mlxsw_sp_router_neigh_destroy, |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1327 | .ndo_fdb_add = switchdev_port_fdb_add, |
| 1328 | .ndo_fdb_del = switchdev_port_fdb_del, |
| 1329 | .ndo_fdb_dump = switchdev_port_fdb_dump, |
| 1330 | .ndo_bridge_setlink = switchdev_port_bridge_setlink, |
| 1331 | .ndo_bridge_getlink = switchdev_port_bridge_getlink, |
| 1332 | .ndo_bridge_dellink = switchdev_port_bridge_dellink, |
Ido Schimmel | 2bf9a58 | 2016-04-05 10:20:04 +0200 | [diff] [blame] | 1333 | .ndo_get_phys_port_name = mlxsw_sp_port_get_phys_port_name, |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1334 | }; |
| 1335 | |
| 1336 | static void mlxsw_sp_port_get_drvinfo(struct net_device *dev, |
| 1337 | struct ethtool_drvinfo *drvinfo) |
| 1338 | { |
| 1339 | struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev); |
| 1340 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
| 1341 | |
| 1342 | strlcpy(drvinfo->driver, mlxsw_sp_driver_name, sizeof(drvinfo->driver)); |
| 1343 | strlcpy(drvinfo->version, mlxsw_sp_driver_version, |
| 1344 | sizeof(drvinfo->version)); |
| 1345 | snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version), |
| 1346 | "%d.%d.%d", |
| 1347 | mlxsw_sp->bus_info->fw_rev.major, |
| 1348 | mlxsw_sp->bus_info->fw_rev.minor, |
| 1349 | mlxsw_sp->bus_info->fw_rev.subminor); |
| 1350 | strlcpy(drvinfo->bus_info, mlxsw_sp->bus_info->device_name, |
| 1351 | sizeof(drvinfo->bus_info)); |
| 1352 | } |
| 1353 | |
Ido Schimmel | 9f7ec05 | 2016-04-06 17:10:14 +0200 | [diff] [blame] | 1354 | static void mlxsw_sp_port_get_pauseparam(struct net_device *dev, |
| 1355 | struct ethtool_pauseparam *pause) |
| 1356 | { |
| 1357 | struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev); |
| 1358 | |
| 1359 | pause->rx_pause = mlxsw_sp_port->link.rx_pause; |
| 1360 | pause->tx_pause = mlxsw_sp_port->link.tx_pause; |
| 1361 | } |
| 1362 | |
| 1363 | static int mlxsw_sp_port_pause_set(struct mlxsw_sp_port *mlxsw_sp_port, |
| 1364 | struct ethtool_pauseparam *pause) |
| 1365 | { |
| 1366 | char pfcc_pl[MLXSW_REG_PFCC_LEN]; |
| 1367 | |
| 1368 | mlxsw_reg_pfcc_pack(pfcc_pl, mlxsw_sp_port->local_port); |
| 1369 | mlxsw_reg_pfcc_pprx_set(pfcc_pl, pause->rx_pause); |
| 1370 | mlxsw_reg_pfcc_pptx_set(pfcc_pl, pause->tx_pause); |
| 1371 | |
| 1372 | return mlxsw_reg_write(mlxsw_sp_port->mlxsw_sp->core, MLXSW_REG(pfcc), |
| 1373 | pfcc_pl); |
| 1374 | } |
| 1375 | |
| 1376 | static int mlxsw_sp_port_set_pauseparam(struct net_device *dev, |
| 1377 | struct ethtool_pauseparam *pause) |
| 1378 | { |
| 1379 | struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev); |
| 1380 | bool pause_en = pause->tx_pause || pause->rx_pause; |
| 1381 | int err; |
| 1382 | |
Ido Schimmel | d81a6bd | 2016-04-06 17:10:16 +0200 | [diff] [blame] | 1383 | if (mlxsw_sp_port->dcb.pfc && mlxsw_sp_port->dcb.pfc->pfc_en) { |
| 1384 | netdev_err(dev, "PFC already enabled on port\n"); |
| 1385 | return -EINVAL; |
| 1386 | } |
| 1387 | |
Ido Schimmel | 9f7ec05 | 2016-04-06 17:10:14 +0200 | [diff] [blame] | 1388 | if (pause->autoneg) { |
| 1389 | netdev_err(dev, "PAUSE frames autonegotiation isn't supported\n"); |
| 1390 | return -EINVAL; |
| 1391 | } |
| 1392 | |
| 1393 | err = mlxsw_sp_port_headroom_set(mlxsw_sp_port, dev->mtu, pause_en); |
| 1394 | if (err) { |
| 1395 | netdev_err(dev, "Failed to configure port's headroom\n"); |
| 1396 | return err; |
| 1397 | } |
| 1398 | |
| 1399 | err = mlxsw_sp_port_pause_set(mlxsw_sp_port, pause); |
| 1400 | if (err) { |
| 1401 | netdev_err(dev, "Failed to set PAUSE parameters\n"); |
| 1402 | goto err_port_pause_configure; |
| 1403 | } |
| 1404 | |
| 1405 | mlxsw_sp_port->link.rx_pause = pause->rx_pause; |
| 1406 | mlxsw_sp_port->link.tx_pause = pause->tx_pause; |
| 1407 | |
| 1408 | return 0; |
| 1409 | |
| 1410 | err_port_pause_configure: |
| 1411 | pause_en = mlxsw_sp_port_is_pause_en(mlxsw_sp_port); |
| 1412 | mlxsw_sp_port_headroom_set(mlxsw_sp_port, dev->mtu, pause_en); |
| 1413 | return err; |
| 1414 | } |
| 1415 | |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1416 | struct mlxsw_sp_port_hw_stats { |
| 1417 | char str[ETH_GSTRING_LEN]; |
Jiri Pirko | 412791d | 2016-10-21 16:07:19 +0200 | [diff] [blame] | 1418 | u64 (*getter)(const char *payload); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1419 | }; |
| 1420 | |
Ido Schimmel | 7ed674b | 2016-07-19 15:35:53 +0200 | [diff] [blame] | 1421 | static struct mlxsw_sp_port_hw_stats mlxsw_sp_port_hw_stats[] = { |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1422 | { |
| 1423 | .str = "a_frames_transmitted_ok", |
| 1424 | .getter = mlxsw_reg_ppcnt_a_frames_transmitted_ok_get, |
| 1425 | }, |
| 1426 | { |
| 1427 | .str = "a_frames_received_ok", |
| 1428 | .getter = mlxsw_reg_ppcnt_a_frames_received_ok_get, |
| 1429 | }, |
| 1430 | { |
| 1431 | .str = "a_frame_check_sequence_errors", |
| 1432 | .getter = mlxsw_reg_ppcnt_a_frame_check_sequence_errors_get, |
| 1433 | }, |
| 1434 | { |
| 1435 | .str = "a_alignment_errors", |
| 1436 | .getter = mlxsw_reg_ppcnt_a_alignment_errors_get, |
| 1437 | }, |
| 1438 | { |
| 1439 | .str = "a_octets_transmitted_ok", |
| 1440 | .getter = mlxsw_reg_ppcnt_a_octets_transmitted_ok_get, |
| 1441 | }, |
| 1442 | { |
| 1443 | .str = "a_octets_received_ok", |
| 1444 | .getter = mlxsw_reg_ppcnt_a_octets_received_ok_get, |
| 1445 | }, |
| 1446 | { |
| 1447 | .str = "a_multicast_frames_xmitted_ok", |
| 1448 | .getter = mlxsw_reg_ppcnt_a_multicast_frames_xmitted_ok_get, |
| 1449 | }, |
| 1450 | { |
| 1451 | .str = "a_broadcast_frames_xmitted_ok", |
| 1452 | .getter = mlxsw_reg_ppcnt_a_broadcast_frames_xmitted_ok_get, |
| 1453 | }, |
| 1454 | { |
| 1455 | .str = "a_multicast_frames_received_ok", |
| 1456 | .getter = mlxsw_reg_ppcnt_a_multicast_frames_received_ok_get, |
| 1457 | }, |
| 1458 | { |
| 1459 | .str = "a_broadcast_frames_received_ok", |
| 1460 | .getter = mlxsw_reg_ppcnt_a_broadcast_frames_received_ok_get, |
| 1461 | }, |
| 1462 | { |
| 1463 | .str = "a_in_range_length_errors", |
| 1464 | .getter = mlxsw_reg_ppcnt_a_in_range_length_errors_get, |
| 1465 | }, |
| 1466 | { |
| 1467 | .str = "a_out_of_range_length_field", |
| 1468 | .getter = mlxsw_reg_ppcnt_a_out_of_range_length_field_get, |
| 1469 | }, |
| 1470 | { |
| 1471 | .str = "a_frame_too_long_errors", |
| 1472 | .getter = mlxsw_reg_ppcnt_a_frame_too_long_errors_get, |
| 1473 | }, |
| 1474 | { |
| 1475 | .str = "a_symbol_error_during_carrier", |
| 1476 | .getter = mlxsw_reg_ppcnt_a_symbol_error_during_carrier_get, |
| 1477 | }, |
| 1478 | { |
| 1479 | .str = "a_mac_control_frames_transmitted", |
| 1480 | .getter = mlxsw_reg_ppcnt_a_mac_control_frames_transmitted_get, |
| 1481 | }, |
| 1482 | { |
| 1483 | .str = "a_mac_control_frames_received", |
| 1484 | .getter = mlxsw_reg_ppcnt_a_mac_control_frames_received_get, |
| 1485 | }, |
| 1486 | { |
| 1487 | .str = "a_unsupported_opcodes_received", |
| 1488 | .getter = mlxsw_reg_ppcnt_a_unsupported_opcodes_received_get, |
| 1489 | }, |
| 1490 | { |
| 1491 | .str = "a_pause_mac_ctrl_frames_received", |
| 1492 | .getter = mlxsw_reg_ppcnt_a_pause_mac_ctrl_frames_received_get, |
| 1493 | }, |
| 1494 | { |
| 1495 | .str = "a_pause_mac_ctrl_frames_xmitted", |
| 1496 | .getter = mlxsw_reg_ppcnt_a_pause_mac_ctrl_frames_transmitted_get, |
| 1497 | }, |
| 1498 | }; |
| 1499 | |
| 1500 | #define MLXSW_SP_PORT_HW_STATS_LEN ARRAY_SIZE(mlxsw_sp_port_hw_stats) |
| 1501 | |
Ido Schimmel | 7ed674b | 2016-07-19 15:35:53 +0200 | [diff] [blame] | 1502 | static struct mlxsw_sp_port_hw_stats mlxsw_sp_port_hw_prio_stats[] = { |
| 1503 | { |
| 1504 | .str = "rx_octets_prio", |
| 1505 | .getter = mlxsw_reg_ppcnt_rx_octets_get, |
| 1506 | }, |
| 1507 | { |
| 1508 | .str = "rx_frames_prio", |
| 1509 | .getter = mlxsw_reg_ppcnt_rx_frames_get, |
| 1510 | }, |
| 1511 | { |
| 1512 | .str = "tx_octets_prio", |
| 1513 | .getter = mlxsw_reg_ppcnt_tx_octets_get, |
| 1514 | }, |
| 1515 | { |
| 1516 | .str = "tx_frames_prio", |
| 1517 | .getter = mlxsw_reg_ppcnt_tx_frames_get, |
| 1518 | }, |
| 1519 | { |
| 1520 | .str = "rx_pause_prio", |
| 1521 | .getter = mlxsw_reg_ppcnt_rx_pause_get, |
| 1522 | }, |
| 1523 | { |
| 1524 | .str = "rx_pause_duration_prio", |
| 1525 | .getter = mlxsw_reg_ppcnt_rx_pause_duration_get, |
| 1526 | }, |
| 1527 | { |
| 1528 | .str = "tx_pause_prio", |
| 1529 | .getter = mlxsw_reg_ppcnt_tx_pause_get, |
| 1530 | }, |
| 1531 | { |
| 1532 | .str = "tx_pause_duration_prio", |
| 1533 | .getter = mlxsw_reg_ppcnt_tx_pause_duration_get, |
| 1534 | }, |
| 1535 | }; |
| 1536 | |
| 1537 | #define MLXSW_SP_PORT_HW_PRIO_STATS_LEN ARRAY_SIZE(mlxsw_sp_port_hw_prio_stats) |
| 1538 | |
Jiri Pirko | 412791d | 2016-10-21 16:07:19 +0200 | [diff] [blame] | 1539 | static u64 mlxsw_reg_ppcnt_tc_transmit_queue_bytes_get(const char *ppcnt_pl) |
Ido Schimmel | df4750e | 2016-07-19 15:35:54 +0200 | [diff] [blame] | 1540 | { |
| 1541 | u64 transmit_queue = mlxsw_reg_ppcnt_tc_transmit_queue_get(ppcnt_pl); |
| 1542 | |
| 1543 | return MLXSW_SP_CELLS_TO_BYTES(transmit_queue); |
| 1544 | } |
| 1545 | |
| 1546 | static struct mlxsw_sp_port_hw_stats mlxsw_sp_port_hw_tc_stats[] = { |
| 1547 | { |
| 1548 | .str = "tc_transmit_queue_tc", |
| 1549 | .getter = mlxsw_reg_ppcnt_tc_transmit_queue_bytes_get, |
| 1550 | }, |
| 1551 | { |
| 1552 | .str = "tc_no_buffer_discard_uc_tc", |
| 1553 | .getter = mlxsw_reg_ppcnt_tc_no_buffer_discard_uc_get, |
| 1554 | }, |
| 1555 | }; |
| 1556 | |
| 1557 | #define MLXSW_SP_PORT_HW_TC_STATS_LEN ARRAY_SIZE(mlxsw_sp_port_hw_tc_stats) |
| 1558 | |
Ido Schimmel | 7ed674b | 2016-07-19 15:35:53 +0200 | [diff] [blame] | 1559 | #define MLXSW_SP_PORT_ETHTOOL_STATS_LEN (MLXSW_SP_PORT_HW_STATS_LEN + \ |
Ido Schimmel | df4750e | 2016-07-19 15:35:54 +0200 | [diff] [blame] | 1560 | (MLXSW_SP_PORT_HW_PRIO_STATS_LEN + \ |
| 1561 | MLXSW_SP_PORT_HW_TC_STATS_LEN) * \ |
Ido Schimmel | 7ed674b | 2016-07-19 15:35:53 +0200 | [diff] [blame] | 1562 | IEEE_8021QAZ_MAX_TCS) |
| 1563 | |
| 1564 | static void mlxsw_sp_port_get_prio_strings(u8 **p, int prio) |
| 1565 | { |
| 1566 | int i; |
| 1567 | |
| 1568 | for (i = 0; i < MLXSW_SP_PORT_HW_PRIO_STATS_LEN; i++) { |
| 1569 | snprintf(*p, ETH_GSTRING_LEN, "%s_%d", |
| 1570 | mlxsw_sp_port_hw_prio_stats[i].str, prio); |
| 1571 | *p += ETH_GSTRING_LEN; |
| 1572 | } |
| 1573 | } |
| 1574 | |
Ido Schimmel | df4750e | 2016-07-19 15:35:54 +0200 | [diff] [blame] | 1575 | static void mlxsw_sp_port_get_tc_strings(u8 **p, int tc) |
| 1576 | { |
| 1577 | int i; |
| 1578 | |
| 1579 | for (i = 0; i < MLXSW_SP_PORT_HW_TC_STATS_LEN; i++) { |
| 1580 | snprintf(*p, ETH_GSTRING_LEN, "%s_%d", |
| 1581 | mlxsw_sp_port_hw_tc_stats[i].str, tc); |
| 1582 | *p += ETH_GSTRING_LEN; |
| 1583 | } |
| 1584 | } |
| 1585 | |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1586 | static void mlxsw_sp_port_get_strings(struct net_device *dev, |
| 1587 | u32 stringset, u8 *data) |
| 1588 | { |
| 1589 | u8 *p = data; |
| 1590 | int i; |
| 1591 | |
| 1592 | switch (stringset) { |
| 1593 | case ETH_SS_STATS: |
| 1594 | for (i = 0; i < MLXSW_SP_PORT_HW_STATS_LEN; i++) { |
| 1595 | memcpy(p, mlxsw_sp_port_hw_stats[i].str, |
| 1596 | ETH_GSTRING_LEN); |
| 1597 | p += ETH_GSTRING_LEN; |
| 1598 | } |
Ido Schimmel | 7ed674b | 2016-07-19 15:35:53 +0200 | [diff] [blame] | 1599 | |
| 1600 | for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) |
| 1601 | mlxsw_sp_port_get_prio_strings(&p, i); |
| 1602 | |
Ido Schimmel | df4750e | 2016-07-19 15:35:54 +0200 | [diff] [blame] | 1603 | for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) |
| 1604 | mlxsw_sp_port_get_tc_strings(&p, i); |
| 1605 | |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1606 | break; |
| 1607 | } |
| 1608 | } |
| 1609 | |
Ido Schimmel | 3a66ee3 | 2015-11-27 13:45:55 +0100 | [diff] [blame] | 1610 | static int mlxsw_sp_port_set_phys_id(struct net_device *dev, |
| 1611 | enum ethtool_phys_id_state state) |
| 1612 | { |
| 1613 | struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev); |
| 1614 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
| 1615 | char mlcr_pl[MLXSW_REG_MLCR_LEN]; |
| 1616 | bool active; |
| 1617 | |
| 1618 | switch (state) { |
| 1619 | case ETHTOOL_ID_ACTIVE: |
| 1620 | active = true; |
| 1621 | break; |
| 1622 | case ETHTOOL_ID_INACTIVE: |
| 1623 | active = false; |
| 1624 | break; |
| 1625 | default: |
| 1626 | return -EOPNOTSUPP; |
| 1627 | } |
| 1628 | |
| 1629 | mlxsw_reg_mlcr_pack(mlcr_pl, mlxsw_sp_port->local_port, active); |
| 1630 | return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(mlcr), mlcr_pl); |
| 1631 | } |
| 1632 | |
Ido Schimmel | 7ed674b | 2016-07-19 15:35:53 +0200 | [diff] [blame] | 1633 | static int |
| 1634 | mlxsw_sp_get_hw_stats_by_group(struct mlxsw_sp_port_hw_stats **p_hw_stats, |
| 1635 | int *p_len, enum mlxsw_reg_ppcnt_grp grp) |
| 1636 | { |
| 1637 | switch (grp) { |
| 1638 | case MLXSW_REG_PPCNT_IEEE_8023_CNT: |
| 1639 | *p_hw_stats = mlxsw_sp_port_hw_stats; |
| 1640 | *p_len = MLXSW_SP_PORT_HW_STATS_LEN; |
| 1641 | break; |
| 1642 | case MLXSW_REG_PPCNT_PRIO_CNT: |
| 1643 | *p_hw_stats = mlxsw_sp_port_hw_prio_stats; |
| 1644 | *p_len = MLXSW_SP_PORT_HW_PRIO_STATS_LEN; |
| 1645 | break; |
Ido Schimmel | df4750e | 2016-07-19 15:35:54 +0200 | [diff] [blame] | 1646 | case MLXSW_REG_PPCNT_TC_CNT: |
| 1647 | *p_hw_stats = mlxsw_sp_port_hw_tc_stats; |
| 1648 | *p_len = MLXSW_SP_PORT_HW_TC_STATS_LEN; |
| 1649 | break; |
Ido Schimmel | 7ed674b | 2016-07-19 15:35:53 +0200 | [diff] [blame] | 1650 | default: |
| 1651 | WARN_ON(1); |
| 1652 | return -ENOTSUPP; |
| 1653 | } |
| 1654 | return 0; |
| 1655 | } |
| 1656 | |
| 1657 | static void __mlxsw_sp_port_get_stats(struct net_device *dev, |
| 1658 | enum mlxsw_reg_ppcnt_grp grp, int prio, |
| 1659 | u64 *data, int data_index) |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1660 | { |
Ido Schimmel | 7ed674b | 2016-07-19 15:35:53 +0200 | [diff] [blame] | 1661 | struct mlxsw_sp_port_hw_stats *hw_stats; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1662 | char ppcnt_pl[MLXSW_REG_PPCNT_LEN]; |
Ido Schimmel | 7ed674b | 2016-07-19 15:35:53 +0200 | [diff] [blame] | 1663 | int i, len; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1664 | int err; |
| 1665 | |
Ido Schimmel | 7ed674b | 2016-07-19 15:35:53 +0200 | [diff] [blame] | 1666 | err = mlxsw_sp_get_hw_stats_by_group(&hw_stats, &len, grp); |
| 1667 | if (err) |
| 1668 | return; |
Nogah Frankel | fc1bbb0 | 2016-09-16 15:05:38 +0200 | [diff] [blame] | 1669 | mlxsw_sp_port_get_stats_raw(dev, grp, prio, ppcnt_pl); |
Ido Schimmel | 7ed674b | 2016-07-19 15:35:53 +0200 | [diff] [blame] | 1670 | for (i = 0; i < len; i++) |
Colin Ian King | faac0ff | 2016-09-23 12:02:45 +0100 | [diff] [blame] | 1671 | data[data_index + i] = hw_stats[i].getter(ppcnt_pl); |
Ido Schimmel | 7ed674b | 2016-07-19 15:35:53 +0200 | [diff] [blame] | 1672 | } |
| 1673 | |
| 1674 | static void mlxsw_sp_port_get_stats(struct net_device *dev, |
| 1675 | struct ethtool_stats *stats, u64 *data) |
| 1676 | { |
| 1677 | int i, data_index = 0; |
| 1678 | |
| 1679 | /* IEEE 802.3 Counters */ |
| 1680 | __mlxsw_sp_port_get_stats(dev, MLXSW_REG_PPCNT_IEEE_8023_CNT, 0, |
| 1681 | data, data_index); |
| 1682 | data_index = MLXSW_SP_PORT_HW_STATS_LEN; |
| 1683 | |
| 1684 | /* Per-Priority Counters */ |
| 1685 | for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) { |
| 1686 | __mlxsw_sp_port_get_stats(dev, MLXSW_REG_PPCNT_PRIO_CNT, i, |
| 1687 | data, data_index); |
| 1688 | data_index += MLXSW_SP_PORT_HW_PRIO_STATS_LEN; |
| 1689 | } |
Ido Schimmel | df4750e | 2016-07-19 15:35:54 +0200 | [diff] [blame] | 1690 | |
| 1691 | /* Per-TC Counters */ |
| 1692 | for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) { |
| 1693 | __mlxsw_sp_port_get_stats(dev, MLXSW_REG_PPCNT_TC_CNT, i, |
| 1694 | data, data_index); |
| 1695 | data_index += MLXSW_SP_PORT_HW_TC_STATS_LEN; |
| 1696 | } |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1697 | } |
| 1698 | |
| 1699 | static int mlxsw_sp_port_get_sset_count(struct net_device *dev, int sset) |
| 1700 | { |
| 1701 | switch (sset) { |
| 1702 | case ETH_SS_STATS: |
Ido Schimmel | 7ed674b | 2016-07-19 15:35:53 +0200 | [diff] [blame] | 1703 | return MLXSW_SP_PORT_ETHTOOL_STATS_LEN; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1704 | default: |
| 1705 | return -EOPNOTSUPP; |
| 1706 | } |
| 1707 | } |
| 1708 | |
| 1709 | struct mlxsw_sp_port_link_mode { |
Ido Schimmel | b9d66a3 | 2016-09-12 13:26:27 +0200 | [diff] [blame] | 1710 | enum ethtool_link_mode_bit_indices mask_ethtool; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1711 | u32 mask; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1712 | u32 speed; |
| 1713 | }; |
| 1714 | |
| 1715 | static const struct mlxsw_sp_port_link_mode mlxsw_sp_port_link_mode[] = { |
| 1716 | { |
| 1717 | .mask = MLXSW_REG_PTYS_ETH_SPEED_100BASE_T, |
Ido Schimmel | b9d66a3 | 2016-09-12 13:26:27 +0200 | [diff] [blame] | 1718 | .mask_ethtool = ETHTOOL_LINK_MODE_100baseT_Full_BIT, |
| 1719 | .speed = SPEED_100, |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1720 | }, |
| 1721 | { |
| 1722 | .mask = MLXSW_REG_PTYS_ETH_SPEED_SGMII | |
| 1723 | MLXSW_REG_PTYS_ETH_SPEED_1000BASE_KX, |
Ido Schimmel | b9d66a3 | 2016-09-12 13:26:27 +0200 | [diff] [blame] | 1724 | .mask_ethtool = ETHTOOL_LINK_MODE_1000baseKX_Full_BIT, |
| 1725 | .speed = SPEED_1000, |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1726 | }, |
| 1727 | { |
| 1728 | .mask = MLXSW_REG_PTYS_ETH_SPEED_10GBASE_T, |
Ido Schimmel | b9d66a3 | 2016-09-12 13:26:27 +0200 | [diff] [blame] | 1729 | .mask_ethtool = ETHTOOL_LINK_MODE_10000baseT_Full_BIT, |
| 1730 | .speed = SPEED_10000, |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1731 | }, |
| 1732 | { |
| 1733 | .mask = MLXSW_REG_PTYS_ETH_SPEED_10GBASE_CX4 | |
| 1734 | MLXSW_REG_PTYS_ETH_SPEED_10GBASE_KX4, |
Ido Schimmel | b9d66a3 | 2016-09-12 13:26:27 +0200 | [diff] [blame] | 1735 | .mask_ethtool = ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT, |
| 1736 | .speed = SPEED_10000, |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1737 | }, |
| 1738 | { |
| 1739 | .mask = MLXSW_REG_PTYS_ETH_SPEED_10GBASE_KR | |
| 1740 | MLXSW_REG_PTYS_ETH_SPEED_10GBASE_CR | |
| 1741 | MLXSW_REG_PTYS_ETH_SPEED_10GBASE_SR | |
| 1742 | MLXSW_REG_PTYS_ETH_SPEED_10GBASE_ER_LR, |
Ido Schimmel | b9d66a3 | 2016-09-12 13:26:27 +0200 | [diff] [blame] | 1743 | .mask_ethtool = ETHTOOL_LINK_MODE_10000baseKR_Full_BIT, |
| 1744 | .speed = SPEED_10000, |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1745 | }, |
| 1746 | { |
| 1747 | .mask = MLXSW_REG_PTYS_ETH_SPEED_20GBASE_KR2, |
Ido Schimmel | b9d66a3 | 2016-09-12 13:26:27 +0200 | [diff] [blame] | 1748 | .mask_ethtool = ETHTOOL_LINK_MODE_20000baseKR2_Full_BIT, |
| 1749 | .speed = SPEED_20000, |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1750 | }, |
| 1751 | { |
| 1752 | .mask = MLXSW_REG_PTYS_ETH_SPEED_40GBASE_CR4, |
Ido Schimmel | b9d66a3 | 2016-09-12 13:26:27 +0200 | [diff] [blame] | 1753 | .mask_ethtool = ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT, |
| 1754 | .speed = SPEED_40000, |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1755 | }, |
| 1756 | { |
| 1757 | .mask = MLXSW_REG_PTYS_ETH_SPEED_40GBASE_KR4, |
Ido Schimmel | b9d66a3 | 2016-09-12 13:26:27 +0200 | [diff] [blame] | 1758 | .mask_ethtool = ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT, |
| 1759 | .speed = SPEED_40000, |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1760 | }, |
| 1761 | { |
| 1762 | .mask = MLXSW_REG_PTYS_ETH_SPEED_40GBASE_SR4, |
Ido Schimmel | b9d66a3 | 2016-09-12 13:26:27 +0200 | [diff] [blame] | 1763 | .mask_ethtool = ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT, |
| 1764 | .speed = SPEED_40000, |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1765 | }, |
| 1766 | { |
| 1767 | .mask = MLXSW_REG_PTYS_ETH_SPEED_40GBASE_LR4_ER4, |
Ido Schimmel | b9d66a3 | 2016-09-12 13:26:27 +0200 | [diff] [blame] | 1768 | .mask_ethtool = ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT, |
| 1769 | .speed = SPEED_40000, |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1770 | }, |
| 1771 | { |
Ido Schimmel | b9d66a3 | 2016-09-12 13:26:27 +0200 | [diff] [blame] | 1772 | .mask = MLXSW_REG_PTYS_ETH_SPEED_25GBASE_CR, |
| 1773 | .mask_ethtool = ETHTOOL_LINK_MODE_25000baseCR_Full_BIT, |
| 1774 | .speed = SPEED_25000, |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1775 | }, |
| 1776 | { |
Ido Schimmel | b9d66a3 | 2016-09-12 13:26:27 +0200 | [diff] [blame] | 1777 | .mask = MLXSW_REG_PTYS_ETH_SPEED_25GBASE_KR, |
| 1778 | .mask_ethtool = ETHTOOL_LINK_MODE_25000baseKR_Full_BIT, |
| 1779 | .speed = SPEED_25000, |
| 1780 | }, |
| 1781 | { |
| 1782 | .mask = MLXSW_REG_PTYS_ETH_SPEED_25GBASE_SR, |
| 1783 | .mask_ethtool = ETHTOOL_LINK_MODE_25000baseSR_Full_BIT, |
| 1784 | .speed = SPEED_25000, |
| 1785 | }, |
| 1786 | { |
| 1787 | .mask = MLXSW_REG_PTYS_ETH_SPEED_25GBASE_SR, |
| 1788 | .mask_ethtool = ETHTOOL_LINK_MODE_25000baseSR_Full_BIT, |
| 1789 | .speed = SPEED_25000, |
| 1790 | }, |
| 1791 | { |
| 1792 | .mask = MLXSW_REG_PTYS_ETH_SPEED_50GBASE_CR2, |
| 1793 | .mask_ethtool = ETHTOOL_LINK_MODE_50000baseCR2_Full_BIT, |
| 1794 | .speed = SPEED_50000, |
| 1795 | }, |
| 1796 | { |
| 1797 | .mask = MLXSW_REG_PTYS_ETH_SPEED_50GBASE_KR2, |
| 1798 | .mask_ethtool = ETHTOOL_LINK_MODE_50000baseKR2_Full_BIT, |
| 1799 | .speed = SPEED_50000, |
| 1800 | }, |
| 1801 | { |
| 1802 | .mask = MLXSW_REG_PTYS_ETH_SPEED_50GBASE_SR2, |
| 1803 | .mask_ethtool = ETHTOOL_LINK_MODE_50000baseSR2_Full_BIT, |
| 1804 | .speed = SPEED_50000, |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1805 | }, |
| 1806 | { |
| 1807 | .mask = MLXSW_REG_PTYS_ETH_SPEED_56GBASE_R4, |
Ido Schimmel | b9d66a3 | 2016-09-12 13:26:27 +0200 | [diff] [blame] | 1808 | .mask_ethtool = ETHTOOL_LINK_MODE_56000baseKR4_Full_BIT, |
| 1809 | .speed = SPEED_56000, |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1810 | }, |
| 1811 | { |
Ido Schimmel | b9d66a3 | 2016-09-12 13:26:27 +0200 | [diff] [blame] | 1812 | .mask = MLXSW_REG_PTYS_ETH_SPEED_56GBASE_R4, |
| 1813 | .mask_ethtool = ETHTOOL_LINK_MODE_56000baseCR4_Full_BIT, |
| 1814 | .speed = SPEED_56000, |
| 1815 | }, |
| 1816 | { |
| 1817 | .mask = MLXSW_REG_PTYS_ETH_SPEED_56GBASE_R4, |
| 1818 | .mask_ethtool = ETHTOOL_LINK_MODE_56000baseSR4_Full_BIT, |
| 1819 | .speed = SPEED_56000, |
| 1820 | }, |
| 1821 | { |
| 1822 | .mask = MLXSW_REG_PTYS_ETH_SPEED_56GBASE_R4, |
| 1823 | .mask_ethtool = ETHTOOL_LINK_MODE_56000baseLR4_Full_BIT, |
| 1824 | .speed = SPEED_56000, |
| 1825 | }, |
| 1826 | { |
| 1827 | .mask = MLXSW_REG_PTYS_ETH_SPEED_100GBASE_CR4, |
| 1828 | .mask_ethtool = ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT, |
| 1829 | .speed = SPEED_100000, |
| 1830 | }, |
| 1831 | { |
| 1832 | .mask = MLXSW_REG_PTYS_ETH_SPEED_100GBASE_SR4, |
| 1833 | .mask_ethtool = ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT, |
| 1834 | .speed = SPEED_100000, |
| 1835 | }, |
| 1836 | { |
| 1837 | .mask = MLXSW_REG_PTYS_ETH_SPEED_100GBASE_KR4, |
| 1838 | .mask_ethtool = ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT, |
| 1839 | .speed = SPEED_100000, |
| 1840 | }, |
| 1841 | { |
| 1842 | .mask = MLXSW_REG_PTYS_ETH_SPEED_100GBASE_LR4_ER4, |
| 1843 | .mask_ethtool = ETHTOOL_LINK_MODE_100000baseLR4_ER4_Full_BIT, |
| 1844 | .speed = SPEED_100000, |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1845 | }, |
| 1846 | }; |
| 1847 | |
| 1848 | #define MLXSW_SP_PORT_LINK_MODE_LEN ARRAY_SIZE(mlxsw_sp_port_link_mode) |
| 1849 | |
Ido Schimmel | b9d66a3 | 2016-09-12 13:26:27 +0200 | [diff] [blame] | 1850 | static void |
| 1851 | mlxsw_sp_from_ptys_supported_port(u32 ptys_eth_proto, |
| 1852 | struct ethtool_link_ksettings *cmd) |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1853 | { |
| 1854 | if (ptys_eth_proto & (MLXSW_REG_PTYS_ETH_SPEED_10GBASE_CR | |
| 1855 | MLXSW_REG_PTYS_ETH_SPEED_10GBASE_SR | |
| 1856 | MLXSW_REG_PTYS_ETH_SPEED_40GBASE_CR4 | |
| 1857 | MLXSW_REG_PTYS_ETH_SPEED_40GBASE_SR4 | |
| 1858 | MLXSW_REG_PTYS_ETH_SPEED_100GBASE_SR4 | |
| 1859 | MLXSW_REG_PTYS_ETH_SPEED_SGMII)) |
Ido Schimmel | b9d66a3 | 2016-09-12 13:26:27 +0200 | [diff] [blame] | 1860 | ethtool_link_ksettings_add_link_mode(cmd, supported, FIBRE); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1861 | |
| 1862 | if (ptys_eth_proto & (MLXSW_REG_PTYS_ETH_SPEED_10GBASE_KR | |
| 1863 | MLXSW_REG_PTYS_ETH_SPEED_10GBASE_KX4 | |
| 1864 | MLXSW_REG_PTYS_ETH_SPEED_40GBASE_KR4 | |
| 1865 | MLXSW_REG_PTYS_ETH_SPEED_100GBASE_KR4 | |
| 1866 | MLXSW_REG_PTYS_ETH_SPEED_1000BASE_KX)) |
Ido Schimmel | b9d66a3 | 2016-09-12 13:26:27 +0200 | [diff] [blame] | 1867 | ethtool_link_ksettings_add_link_mode(cmd, supported, Backplane); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1868 | } |
| 1869 | |
Ido Schimmel | b9d66a3 | 2016-09-12 13:26:27 +0200 | [diff] [blame] | 1870 | static void mlxsw_sp_from_ptys_link(u32 ptys_eth_proto, unsigned long *mode) |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1871 | { |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1872 | int i; |
| 1873 | |
| 1874 | for (i = 0; i < MLXSW_SP_PORT_LINK_MODE_LEN; i++) { |
| 1875 | if (ptys_eth_proto & mlxsw_sp_port_link_mode[i].mask) |
Ido Schimmel | b9d66a3 | 2016-09-12 13:26:27 +0200 | [diff] [blame] | 1876 | __set_bit(mlxsw_sp_port_link_mode[i].mask_ethtool, |
| 1877 | mode); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1878 | } |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1879 | } |
| 1880 | |
| 1881 | static void mlxsw_sp_from_ptys_speed_duplex(bool carrier_ok, u32 ptys_eth_proto, |
Ido Schimmel | b9d66a3 | 2016-09-12 13:26:27 +0200 | [diff] [blame] | 1882 | struct ethtool_link_ksettings *cmd) |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1883 | { |
| 1884 | u32 speed = SPEED_UNKNOWN; |
| 1885 | u8 duplex = DUPLEX_UNKNOWN; |
| 1886 | int i; |
| 1887 | |
| 1888 | if (!carrier_ok) |
| 1889 | goto out; |
| 1890 | |
| 1891 | for (i = 0; i < MLXSW_SP_PORT_LINK_MODE_LEN; i++) { |
| 1892 | if (ptys_eth_proto & mlxsw_sp_port_link_mode[i].mask) { |
| 1893 | speed = mlxsw_sp_port_link_mode[i].speed; |
| 1894 | duplex = DUPLEX_FULL; |
| 1895 | break; |
| 1896 | } |
| 1897 | } |
| 1898 | out: |
Ido Schimmel | b9d66a3 | 2016-09-12 13:26:27 +0200 | [diff] [blame] | 1899 | cmd->base.speed = speed; |
| 1900 | cmd->base.duplex = duplex; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1901 | } |
| 1902 | |
| 1903 | static u8 mlxsw_sp_port_connector_port(u32 ptys_eth_proto) |
| 1904 | { |
| 1905 | if (ptys_eth_proto & (MLXSW_REG_PTYS_ETH_SPEED_10GBASE_SR | |
| 1906 | MLXSW_REG_PTYS_ETH_SPEED_40GBASE_SR4 | |
| 1907 | MLXSW_REG_PTYS_ETH_SPEED_100GBASE_SR4 | |
| 1908 | MLXSW_REG_PTYS_ETH_SPEED_SGMII)) |
| 1909 | return PORT_FIBRE; |
| 1910 | |
| 1911 | if (ptys_eth_proto & (MLXSW_REG_PTYS_ETH_SPEED_10GBASE_CR | |
| 1912 | MLXSW_REG_PTYS_ETH_SPEED_40GBASE_CR4 | |
| 1913 | MLXSW_REG_PTYS_ETH_SPEED_100GBASE_CR4)) |
| 1914 | return PORT_DA; |
| 1915 | |
| 1916 | if (ptys_eth_proto & (MLXSW_REG_PTYS_ETH_SPEED_10GBASE_KR | |
| 1917 | MLXSW_REG_PTYS_ETH_SPEED_10GBASE_KX4 | |
| 1918 | MLXSW_REG_PTYS_ETH_SPEED_40GBASE_KR4 | |
| 1919 | MLXSW_REG_PTYS_ETH_SPEED_100GBASE_KR4)) |
| 1920 | return PORT_NONE; |
| 1921 | |
| 1922 | return PORT_OTHER; |
| 1923 | } |
| 1924 | |
Ido Schimmel | b9d66a3 | 2016-09-12 13:26:27 +0200 | [diff] [blame] | 1925 | static u32 |
| 1926 | mlxsw_sp_to_ptys_advert_link(const struct ethtool_link_ksettings *cmd) |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1927 | { |
| 1928 | u32 ptys_proto = 0; |
| 1929 | int i; |
| 1930 | |
| 1931 | for (i = 0; i < MLXSW_SP_PORT_LINK_MODE_LEN; i++) { |
Ido Schimmel | b9d66a3 | 2016-09-12 13:26:27 +0200 | [diff] [blame] | 1932 | if (test_bit(mlxsw_sp_port_link_mode[i].mask_ethtool, |
| 1933 | cmd->link_modes.advertising)) |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 1934 | ptys_proto |= mlxsw_sp_port_link_mode[i].mask; |
| 1935 | } |
| 1936 | return ptys_proto; |
| 1937 | } |
| 1938 | |
| 1939 | static u32 mlxsw_sp_to_ptys_speed(u32 speed) |
| 1940 | { |
| 1941 | u32 ptys_proto = 0; |
| 1942 | int i; |
| 1943 | |
| 1944 | for (i = 0; i < MLXSW_SP_PORT_LINK_MODE_LEN; i++) { |
| 1945 | if (speed == mlxsw_sp_port_link_mode[i].speed) |
| 1946 | ptys_proto |= mlxsw_sp_port_link_mode[i].mask; |
| 1947 | } |
| 1948 | return ptys_proto; |
| 1949 | } |
| 1950 | |
Ido Schimmel | 18f1e70 | 2016-02-26 17:32:31 +0100 | [diff] [blame] | 1951 | static u32 mlxsw_sp_to_ptys_upper_speed(u32 upper_speed) |
| 1952 | { |
| 1953 | u32 ptys_proto = 0; |
| 1954 | int i; |
| 1955 | |
| 1956 | for (i = 0; i < MLXSW_SP_PORT_LINK_MODE_LEN; i++) { |
| 1957 | if (mlxsw_sp_port_link_mode[i].speed <= upper_speed) |
| 1958 | ptys_proto |= mlxsw_sp_port_link_mode[i].mask; |
| 1959 | } |
| 1960 | return ptys_proto; |
| 1961 | } |
| 1962 | |
Ido Schimmel | b9d66a3 | 2016-09-12 13:26:27 +0200 | [diff] [blame] | 1963 | static void mlxsw_sp_port_get_link_supported(u32 eth_proto_cap, |
| 1964 | struct ethtool_link_ksettings *cmd) |
| 1965 | { |
| 1966 | ethtool_link_ksettings_add_link_mode(cmd, supported, Asym_Pause); |
| 1967 | ethtool_link_ksettings_add_link_mode(cmd, supported, Autoneg); |
| 1968 | ethtool_link_ksettings_add_link_mode(cmd, supported, Pause); |
| 1969 | |
| 1970 | mlxsw_sp_from_ptys_supported_port(eth_proto_cap, cmd); |
| 1971 | mlxsw_sp_from_ptys_link(eth_proto_cap, cmd->link_modes.supported); |
| 1972 | } |
| 1973 | |
| 1974 | static void mlxsw_sp_port_get_link_advertise(u32 eth_proto_admin, bool autoneg, |
| 1975 | struct ethtool_link_ksettings *cmd) |
| 1976 | { |
| 1977 | if (!autoneg) |
| 1978 | return; |
| 1979 | |
| 1980 | ethtool_link_ksettings_add_link_mode(cmd, advertising, Autoneg); |
| 1981 | mlxsw_sp_from_ptys_link(eth_proto_admin, cmd->link_modes.advertising); |
| 1982 | } |
| 1983 | |
| 1984 | static void |
| 1985 | mlxsw_sp_port_get_link_lp_advertise(u32 eth_proto_lp, u8 autoneg_status, |
| 1986 | struct ethtool_link_ksettings *cmd) |
| 1987 | { |
| 1988 | if (autoneg_status != MLXSW_REG_PTYS_AN_STATUS_OK || !eth_proto_lp) |
| 1989 | return; |
| 1990 | |
| 1991 | ethtool_link_ksettings_add_link_mode(cmd, lp_advertising, Autoneg); |
| 1992 | mlxsw_sp_from_ptys_link(eth_proto_lp, cmd->link_modes.lp_advertising); |
| 1993 | } |
| 1994 | |
| 1995 | static int mlxsw_sp_port_get_link_ksettings(struct net_device *dev, |
| 1996 | struct ethtool_link_ksettings *cmd) |
| 1997 | { |
| 1998 | u32 eth_proto_cap, eth_proto_admin, eth_proto_oper, eth_proto_lp; |
| 1999 | struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev); |
| 2000 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
| 2001 | char ptys_pl[MLXSW_REG_PTYS_LEN]; |
| 2002 | u8 autoneg_status; |
| 2003 | bool autoneg; |
| 2004 | int err; |
| 2005 | |
| 2006 | autoneg = mlxsw_sp_port->link.autoneg; |
Elad Raz | 401c8b4 | 2016-10-28 21:35:52 +0200 | [diff] [blame] | 2007 | mlxsw_reg_ptys_eth_pack(ptys_pl, mlxsw_sp_port->local_port, 0); |
Ido Schimmel | b9d66a3 | 2016-09-12 13:26:27 +0200 | [diff] [blame] | 2008 | err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(ptys), ptys_pl); |
| 2009 | if (err) |
| 2010 | return err; |
Elad Raz | 401c8b4 | 2016-10-28 21:35:52 +0200 | [diff] [blame] | 2011 | mlxsw_reg_ptys_eth_unpack(ptys_pl, ð_proto_cap, ð_proto_admin, |
| 2012 | ð_proto_oper); |
Ido Schimmel | b9d66a3 | 2016-09-12 13:26:27 +0200 | [diff] [blame] | 2013 | |
| 2014 | mlxsw_sp_port_get_link_supported(eth_proto_cap, cmd); |
| 2015 | |
| 2016 | mlxsw_sp_port_get_link_advertise(eth_proto_admin, autoneg, cmd); |
| 2017 | |
| 2018 | eth_proto_lp = mlxsw_reg_ptys_eth_proto_lp_advertise_get(ptys_pl); |
| 2019 | autoneg_status = mlxsw_reg_ptys_an_status_get(ptys_pl); |
| 2020 | mlxsw_sp_port_get_link_lp_advertise(eth_proto_lp, autoneg_status, cmd); |
| 2021 | |
| 2022 | cmd->base.autoneg = autoneg ? AUTONEG_ENABLE : AUTONEG_DISABLE; |
| 2023 | cmd->base.port = mlxsw_sp_port_connector_port(eth_proto_oper); |
| 2024 | mlxsw_sp_from_ptys_speed_duplex(netif_carrier_ok(dev), eth_proto_oper, |
| 2025 | cmd); |
| 2026 | |
| 2027 | return 0; |
| 2028 | } |
| 2029 | |
| 2030 | static int |
| 2031 | mlxsw_sp_port_set_link_ksettings(struct net_device *dev, |
| 2032 | const struct ethtool_link_ksettings *cmd) |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 2033 | { |
| 2034 | struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev); |
| 2035 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
| 2036 | char ptys_pl[MLXSW_REG_PTYS_LEN]; |
Ido Schimmel | b9d66a3 | 2016-09-12 13:26:27 +0200 | [diff] [blame] | 2037 | u32 eth_proto_cap, eth_proto_new; |
Ido Schimmel | 0c83f88 | 2016-09-12 13:26:23 +0200 | [diff] [blame] | 2038 | bool autoneg; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 2039 | int err; |
| 2040 | |
Elad Raz | 401c8b4 | 2016-10-28 21:35:52 +0200 | [diff] [blame] | 2041 | mlxsw_reg_ptys_eth_pack(ptys_pl, mlxsw_sp_port->local_port, 0); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 2042 | err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(ptys), ptys_pl); |
Ido Schimmel | b9d66a3 | 2016-09-12 13:26:27 +0200 | [diff] [blame] | 2043 | if (err) |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 2044 | return err; |
Elad Raz | 401c8b4 | 2016-10-28 21:35:52 +0200 | [diff] [blame] | 2045 | mlxsw_reg_ptys_eth_unpack(ptys_pl, ð_proto_cap, NULL, NULL); |
Ido Schimmel | b9d66a3 | 2016-09-12 13:26:27 +0200 | [diff] [blame] | 2046 | |
| 2047 | autoneg = cmd->base.autoneg == AUTONEG_ENABLE; |
| 2048 | eth_proto_new = autoneg ? |
| 2049 | mlxsw_sp_to_ptys_advert_link(cmd) : |
| 2050 | mlxsw_sp_to_ptys_speed(cmd->base.speed); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 2051 | |
| 2052 | eth_proto_new = eth_proto_new & eth_proto_cap; |
| 2053 | if (!eth_proto_new) { |
Ido Schimmel | b9d66a3 | 2016-09-12 13:26:27 +0200 | [diff] [blame] | 2054 | netdev_err(dev, "No supported speed requested\n"); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 2055 | return -EINVAL; |
| 2056 | } |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 2057 | |
Elad Raz | 401c8b4 | 2016-10-28 21:35:52 +0200 | [diff] [blame] | 2058 | mlxsw_reg_ptys_eth_pack(ptys_pl, mlxsw_sp_port->local_port, |
| 2059 | eth_proto_new); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 2060 | err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ptys), ptys_pl); |
Ido Schimmel | b9d66a3 | 2016-09-12 13:26:27 +0200 | [diff] [blame] | 2061 | if (err) |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 2062 | return err; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 2063 | |
Ido Schimmel | 6277d46 | 2016-07-15 11:14:58 +0200 | [diff] [blame] | 2064 | if (!netif_running(dev)) |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 2065 | return 0; |
| 2066 | |
Ido Schimmel | 0c83f88 | 2016-09-12 13:26:23 +0200 | [diff] [blame] | 2067 | mlxsw_sp_port->link.autoneg = autoneg; |
| 2068 | |
Ido Schimmel | b9d66a3 | 2016-09-12 13:26:27 +0200 | [diff] [blame] | 2069 | mlxsw_sp_port_admin_status_set(mlxsw_sp_port, false); |
| 2070 | mlxsw_sp_port_admin_status_set(mlxsw_sp_port, true); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 2071 | |
| 2072 | return 0; |
| 2073 | } |
| 2074 | |
| 2075 | static const struct ethtool_ops mlxsw_sp_port_ethtool_ops = { |
| 2076 | .get_drvinfo = mlxsw_sp_port_get_drvinfo, |
| 2077 | .get_link = ethtool_op_get_link, |
Ido Schimmel | 9f7ec05 | 2016-04-06 17:10:14 +0200 | [diff] [blame] | 2078 | .get_pauseparam = mlxsw_sp_port_get_pauseparam, |
| 2079 | .set_pauseparam = mlxsw_sp_port_set_pauseparam, |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 2080 | .get_strings = mlxsw_sp_port_get_strings, |
Ido Schimmel | 3a66ee3 | 2015-11-27 13:45:55 +0100 | [diff] [blame] | 2081 | .set_phys_id = mlxsw_sp_port_set_phys_id, |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 2082 | .get_ethtool_stats = mlxsw_sp_port_get_stats, |
| 2083 | .get_sset_count = mlxsw_sp_port_get_sset_count, |
Ido Schimmel | b9d66a3 | 2016-09-12 13:26:27 +0200 | [diff] [blame] | 2084 | .get_link_ksettings = mlxsw_sp_port_get_link_ksettings, |
| 2085 | .set_link_ksettings = mlxsw_sp_port_set_link_ksettings, |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 2086 | }; |
| 2087 | |
Ido Schimmel | 18f1e70 | 2016-02-26 17:32:31 +0100 | [diff] [blame] | 2088 | static int |
| 2089 | mlxsw_sp_port_speed_by_width_set(struct mlxsw_sp_port *mlxsw_sp_port, u8 width) |
| 2090 | { |
| 2091 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
| 2092 | u32 upper_speed = MLXSW_SP_PORT_BASE_SPEED * width; |
| 2093 | char ptys_pl[MLXSW_REG_PTYS_LEN]; |
| 2094 | u32 eth_proto_admin; |
| 2095 | |
| 2096 | eth_proto_admin = mlxsw_sp_to_ptys_upper_speed(upper_speed); |
Elad Raz | 401c8b4 | 2016-10-28 21:35:52 +0200 | [diff] [blame] | 2097 | mlxsw_reg_ptys_eth_pack(ptys_pl, mlxsw_sp_port->local_port, |
| 2098 | eth_proto_admin); |
Ido Schimmel | 18f1e70 | 2016-02-26 17:32:31 +0100 | [diff] [blame] | 2099 | return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ptys), ptys_pl); |
| 2100 | } |
| 2101 | |
Ido Schimmel | 8e8dfe9 | 2016-04-06 17:10:10 +0200 | [diff] [blame] | 2102 | int mlxsw_sp_port_ets_set(struct mlxsw_sp_port *mlxsw_sp_port, |
| 2103 | enum mlxsw_reg_qeec_hr hr, u8 index, u8 next_index, |
| 2104 | bool dwrr, u8 dwrr_weight) |
Ido Schimmel | 90183b9 | 2016-04-06 17:10:08 +0200 | [diff] [blame] | 2105 | { |
| 2106 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
| 2107 | char qeec_pl[MLXSW_REG_QEEC_LEN]; |
| 2108 | |
| 2109 | mlxsw_reg_qeec_pack(qeec_pl, mlxsw_sp_port->local_port, hr, index, |
| 2110 | next_index); |
| 2111 | mlxsw_reg_qeec_de_set(qeec_pl, true); |
| 2112 | mlxsw_reg_qeec_dwrr_set(qeec_pl, dwrr); |
| 2113 | mlxsw_reg_qeec_dwrr_weight_set(qeec_pl, dwrr_weight); |
| 2114 | return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(qeec), qeec_pl); |
| 2115 | } |
| 2116 | |
Ido Schimmel | cc7cf51 | 2016-04-06 17:10:11 +0200 | [diff] [blame] | 2117 | int mlxsw_sp_port_ets_maxrate_set(struct mlxsw_sp_port *mlxsw_sp_port, |
| 2118 | enum mlxsw_reg_qeec_hr hr, u8 index, |
| 2119 | u8 next_index, u32 maxrate) |
Ido Schimmel | 90183b9 | 2016-04-06 17:10:08 +0200 | [diff] [blame] | 2120 | { |
| 2121 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
| 2122 | char qeec_pl[MLXSW_REG_QEEC_LEN]; |
| 2123 | |
| 2124 | mlxsw_reg_qeec_pack(qeec_pl, mlxsw_sp_port->local_port, hr, index, |
| 2125 | next_index); |
| 2126 | mlxsw_reg_qeec_mase_set(qeec_pl, true); |
| 2127 | mlxsw_reg_qeec_max_shaper_rate_set(qeec_pl, maxrate); |
| 2128 | return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(qeec), qeec_pl); |
| 2129 | } |
| 2130 | |
Ido Schimmel | 8e8dfe9 | 2016-04-06 17:10:10 +0200 | [diff] [blame] | 2131 | int mlxsw_sp_port_prio_tc_set(struct mlxsw_sp_port *mlxsw_sp_port, |
| 2132 | u8 switch_prio, u8 tclass) |
Ido Schimmel | 90183b9 | 2016-04-06 17:10:08 +0200 | [diff] [blame] | 2133 | { |
| 2134 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
| 2135 | char qtct_pl[MLXSW_REG_QTCT_LEN]; |
| 2136 | |
| 2137 | mlxsw_reg_qtct_pack(qtct_pl, mlxsw_sp_port->local_port, switch_prio, |
| 2138 | tclass); |
| 2139 | return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(qtct), qtct_pl); |
| 2140 | } |
| 2141 | |
| 2142 | static int mlxsw_sp_port_ets_init(struct mlxsw_sp_port *mlxsw_sp_port) |
| 2143 | { |
| 2144 | int err, i; |
| 2145 | |
| 2146 | /* Setup the elements hierarcy, so that each TC is linked to |
| 2147 | * one subgroup, which are all member in the same group. |
| 2148 | */ |
| 2149 | err = mlxsw_sp_port_ets_set(mlxsw_sp_port, |
| 2150 | MLXSW_REG_QEEC_HIERARCY_GROUP, 0, 0, false, |
| 2151 | 0); |
| 2152 | if (err) |
| 2153 | return err; |
| 2154 | for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) { |
| 2155 | err = mlxsw_sp_port_ets_set(mlxsw_sp_port, |
| 2156 | MLXSW_REG_QEEC_HIERARCY_SUBGROUP, i, |
| 2157 | 0, false, 0); |
| 2158 | if (err) |
| 2159 | return err; |
| 2160 | } |
| 2161 | for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) { |
| 2162 | err = mlxsw_sp_port_ets_set(mlxsw_sp_port, |
| 2163 | MLXSW_REG_QEEC_HIERARCY_TC, i, i, |
| 2164 | false, 0); |
| 2165 | if (err) |
| 2166 | return err; |
| 2167 | } |
| 2168 | |
| 2169 | /* Make sure the max shaper is disabled in all hierarcies that |
| 2170 | * support it. |
| 2171 | */ |
| 2172 | err = mlxsw_sp_port_ets_maxrate_set(mlxsw_sp_port, |
| 2173 | MLXSW_REG_QEEC_HIERARCY_PORT, 0, 0, |
| 2174 | MLXSW_REG_QEEC_MAS_DIS); |
| 2175 | if (err) |
| 2176 | return err; |
| 2177 | for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) { |
| 2178 | err = mlxsw_sp_port_ets_maxrate_set(mlxsw_sp_port, |
| 2179 | MLXSW_REG_QEEC_HIERARCY_SUBGROUP, |
| 2180 | i, 0, |
| 2181 | MLXSW_REG_QEEC_MAS_DIS); |
| 2182 | if (err) |
| 2183 | return err; |
| 2184 | } |
| 2185 | for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) { |
| 2186 | err = mlxsw_sp_port_ets_maxrate_set(mlxsw_sp_port, |
| 2187 | MLXSW_REG_QEEC_HIERARCY_TC, |
| 2188 | i, i, |
| 2189 | MLXSW_REG_QEEC_MAS_DIS); |
| 2190 | if (err) |
| 2191 | return err; |
| 2192 | } |
| 2193 | |
| 2194 | /* Map all priorities to traffic class 0. */ |
| 2195 | for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) { |
| 2196 | err = mlxsw_sp_port_prio_tc_set(mlxsw_sp_port, i, 0); |
| 2197 | if (err) |
| 2198 | return err; |
| 2199 | } |
| 2200 | |
| 2201 | return 0; |
| 2202 | } |
| 2203 | |
Ido Schimmel | 0597848 | 2016-08-17 16:39:30 +0200 | [diff] [blame] | 2204 | static int mlxsw_sp_port_pvid_vport_create(struct mlxsw_sp_port *mlxsw_sp_port) |
| 2205 | { |
| 2206 | mlxsw_sp_port->pvid = 1; |
| 2207 | |
| 2208 | return mlxsw_sp_port_add_vid(mlxsw_sp_port->dev, 0, 1); |
| 2209 | } |
| 2210 | |
| 2211 | static int mlxsw_sp_port_pvid_vport_destroy(struct mlxsw_sp_port *mlxsw_sp_port) |
| 2212 | { |
| 2213 | return mlxsw_sp_port_kill_vid(mlxsw_sp_port->dev, 0, 1); |
| 2214 | } |
| 2215 | |
Jiri Pirko | 67963a3 | 2016-10-28 21:35:55 +0200 | [diff] [blame] | 2216 | static int __mlxsw_sp_port_create(struct mlxsw_sp *mlxsw_sp, u8 local_port, |
| 2217 | bool split, u8 module, u8 width, u8 lane) |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 2218 | { |
| 2219 | struct mlxsw_sp_port *mlxsw_sp_port; |
| 2220 | struct net_device *dev; |
Ido Schimmel | bd40e9d | 2015-12-15 16:03:36 +0100 | [diff] [blame] | 2221 | size_t bytes; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 2222 | int err; |
| 2223 | |
| 2224 | dev = alloc_etherdev(sizeof(struct mlxsw_sp_port)); |
| 2225 | if (!dev) |
| 2226 | return -ENOMEM; |
Jiri Pirko | f20a91f | 2016-10-27 15:13:00 +0200 | [diff] [blame] | 2227 | SET_NETDEV_DEV(dev, mlxsw_sp->bus_info->dev); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 2228 | mlxsw_sp_port = netdev_priv(dev); |
| 2229 | mlxsw_sp_port->dev = dev; |
| 2230 | mlxsw_sp_port->mlxsw_sp = mlxsw_sp; |
| 2231 | mlxsw_sp_port->local_port = local_port; |
Ido Schimmel | 18f1e70 | 2016-02-26 17:32:31 +0100 | [diff] [blame] | 2232 | mlxsw_sp_port->split = split; |
Ido Schimmel | d664b41 | 2016-06-09 09:51:40 +0200 | [diff] [blame] | 2233 | mlxsw_sp_port->mapping.module = module; |
| 2234 | mlxsw_sp_port->mapping.width = width; |
| 2235 | mlxsw_sp_port->mapping.lane = lane; |
Ido Schimmel | 0c83f88 | 2016-09-12 13:26:23 +0200 | [diff] [blame] | 2236 | mlxsw_sp_port->link.autoneg = 1; |
Ido Schimmel | bd40e9d | 2015-12-15 16:03:36 +0100 | [diff] [blame] | 2237 | bytes = DIV_ROUND_UP(VLAN_N_VID, BITS_PER_BYTE); |
| 2238 | mlxsw_sp_port->active_vlans = kzalloc(bytes, GFP_KERNEL); |
| 2239 | if (!mlxsw_sp_port->active_vlans) { |
| 2240 | err = -ENOMEM; |
| 2241 | goto err_port_active_vlans_alloc; |
| 2242 | } |
Elad Raz | fc1273a | 2016-01-06 13:01:11 +0100 | [diff] [blame] | 2243 | mlxsw_sp_port->untagged_vlans = kzalloc(bytes, GFP_KERNEL); |
| 2244 | if (!mlxsw_sp_port->untagged_vlans) { |
| 2245 | err = -ENOMEM; |
| 2246 | goto err_port_untagged_vlans_alloc; |
| 2247 | } |
Ido Schimmel | 7f71eb4 | 2015-12-15 16:03:37 +0100 | [diff] [blame] | 2248 | INIT_LIST_HEAD(&mlxsw_sp_port->vports_list); |
Yotam Gigi | 763b4b7 | 2016-07-21 12:03:17 +0200 | [diff] [blame] | 2249 | INIT_LIST_HEAD(&mlxsw_sp_port->mall_tc_list); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 2250 | |
| 2251 | mlxsw_sp_port->pcpu_stats = |
| 2252 | netdev_alloc_pcpu_stats(struct mlxsw_sp_port_pcpu_stats); |
| 2253 | if (!mlxsw_sp_port->pcpu_stats) { |
| 2254 | err = -ENOMEM; |
| 2255 | goto err_alloc_stats; |
| 2256 | } |
| 2257 | |
Nogah Frankel | fc1bbb0 | 2016-09-16 15:05:38 +0200 | [diff] [blame] | 2258 | mlxsw_sp_port->hw_stats.cache = |
| 2259 | kzalloc(sizeof(*mlxsw_sp_port->hw_stats.cache), GFP_KERNEL); |
| 2260 | |
| 2261 | if (!mlxsw_sp_port->hw_stats.cache) { |
| 2262 | err = -ENOMEM; |
| 2263 | goto err_alloc_hw_stats; |
| 2264 | } |
| 2265 | INIT_DELAYED_WORK(&mlxsw_sp_port->hw_stats.update_dw, |
| 2266 | &update_stats_cache); |
| 2267 | |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 2268 | dev->netdev_ops = &mlxsw_sp_port_netdev_ops; |
| 2269 | dev->ethtool_ops = &mlxsw_sp_port_ethtool_ops; |
| 2270 | |
Ido Schimmel | 3247ff2 | 2016-09-08 08:16:02 +0200 | [diff] [blame] | 2271 | err = mlxsw_sp_port_swid_set(mlxsw_sp_port, 0); |
| 2272 | if (err) { |
| 2273 | dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to set SWID\n", |
| 2274 | mlxsw_sp_port->local_port); |
| 2275 | goto err_port_swid_set; |
| 2276 | } |
| 2277 | |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 2278 | err = mlxsw_sp_port_dev_addr_init(mlxsw_sp_port); |
| 2279 | if (err) { |
| 2280 | dev_err(mlxsw_sp->bus_info->dev, "Port %d: Unable to init port mac address\n", |
| 2281 | mlxsw_sp_port->local_port); |
| 2282 | goto err_dev_addr_init; |
| 2283 | } |
| 2284 | |
| 2285 | netif_carrier_off(dev); |
| 2286 | |
| 2287 | dev->features |= NETIF_F_NETNS_LOCAL | NETIF_F_LLTX | NETIF_F_SG | |
Yotam Gigi | 763b4b7 | 2016-07-21 12:03:17 +0200 | [diff] [blame] | 2288 | NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_TC; |
| 2289 | dev->hw_features |= NETIF_F_HW_TC; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 2290 | |
Jarod Wilson | d894be5 | 2016-10-20 13:55:16 -0400 | [diff] [blame] | 2291 | dev->min_mtu = 0; |
| 2292 | dev->max_mtu = ETH_MAX_MTU; |
| 2293 | |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 2294 | /* Each packet needs to have a Tx header (metadata) on top all other |
| 2295 | * headers. |
| 2296 | */ |
Yotam Gigi | feb7d38 | 2016-10-04 09:46:04 +0200 | [diff] [blame] | 2297 | dev->needed_headroom = MLXSW_TXHDR_LEN; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 2298 | |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 2299 | err = mlxsw_sp_port_system_port_mapping_set(mlxsw_sp_port); |
| 2300 | if (err) { |
| 2301 | dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to set system port mapping\n", |
| 2302 | mlxsw_sp_port->local_port); |
| 2303 | goto err_port_system_port_mapping_set; |
| 2304 | } |
| 2305 | |
Ido Schimmel | 18f1e70 | 2016-02-26 17:32:31 +0100 | [diff] [blame] | 2306 | err = mlxsw_sp_port_speed_by_width_set(mlxsw_sp_port, width); |
| 2307 | if (err) { |
| 2308 | dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to enable speeds\n", |
| 2309 | mlxsw_sp_port->local_port); |
| 2310 | goto err_port_speed_by_width_set; |
| 2311 | } |
| 2312 | |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 2313 | err = mlxsw_sp_port_mtu_set(mlxsw_sp_port, ETH_DATA_LEN); |
| 2314 | if (err) { |
| 2315 | dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to set MTU\n", |
| 2316 | mlxsw_sp_port->local_port); |
| 2317 | goto err_port_mtu_set; |
| 2318 | } |
| 2319 | |
| 2320 | err = mlxsw_sp_port_admin_status_set(mlxsw_sp_port, false); |
| 2321 | if (err) |
| 2322 | goto err_port_admin_status_set; |
| 2323 | |
| 2324 | err = mlxsw_sp_port_buffers_init(mlxsw_sp_port); |
| 2325 | if (err) { |
| 2326 | dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to initialize buffers\n", |
| 2327 | mlxsw_sp_port->local_port); |
| 2328 | goto err_port_buffers_init; |
| 2329 | } |
| 2330 | |
Ido Schimmel | 90183b9 | 2016-04-06 17:10:08 +0200 | [diff] [blame] | 2331 | err = mlxsw_sp_port_ets_init(mlxsw_sp_port); |
| 2332 | if (err) { |
| 2333 | dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to initialize ETS\n", |
| 2334 | mlxsw_sp_port->local_port); |
| 2335 | goto err_port_ets_init; |
| 2336 | } |
| 2337 | |
Ido Schimmel | f00817d | 2016-04-06 17:10:09 +0200 | [diff] [blame] | 2338 | /* ETS and buffers must be initialized before DCB. */ |
| 2339 | err = mlxsw_sp_port_dcb_init(mlxsw_sp_port); |
| 2340 | if (err) { |
| 2341 | dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to initialize DCB\n", |
| 2342 | mlxsw_sp_port->local_port); |
| 2343 | goto err_port_dcb_init; |
| 2344 | } |
| 2345 | |
Ido Schimmel | 0597848 | 2016-08-17 16:39:30 +0200 | [diff] [blame] | 2346 | err = mlxsw_sp_port_pvid_vport_create(mlxsw_sp_port); |
| 2347 | if (err) { |
| 2348 | dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to create PVID vPort\n", |
| 2349 | mlxsw_sp_port->local_port); |
| 2350 | goto err_port_pvid_vport_create; |
| 2351 | } |
| 2352 | |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 2353 | mlxsw_sp_port_switchdev_init(mlxsw_sp_port); |
Ido Schimmel | 2f25844 | 2016-08-17 16:39:31 +0200 | [diff] [blame] | 2354 | mlxsw_sp->ports[local_port] = mlxsw_sp_port; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 2355 | err = register_netdev(dev); |
| 2356 | if (err) { |
| 2357 | dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to register netdev\n", |
| 2358 | mlxsw_sp_port->local_port); |
| 2359 | goto err_register_netdev; |
| 2360 | } |
| 2361 | |
Elad Raz | d808c7e | 2016-10-28 21:35:57 +0200 | [diff] [blame] | 2362 | mlxsw_core_port_eth_set(mlxsw_sp->core, mlxsw_sp_port->local_port, |
| 2363 | mlxsw_sp_port, dev, mlxsw_sp_port->split, |
| 2364 | module); |
Nogah Frankel | fc1bbb0 | 2016-09-16 15:05:38 +0200 | [diff] [blame] | 2365 | mlxsw_core_schedule_dw(&mlxsw_sp_port->hw_stats.update_dw, 0); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 2366 | return 0; |
| 2367 | |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 2368 | err_register_netdev: |
Ido Schimmel | 2f25844 | 2016-08-17 16:39:31 +0200 | [diff] [blame] | 2369 | mlxsw_sp->ports[local_port] = NULL; |
Ido Schimmel | 0583272 | 2016-08-17 16:39:35 +0200 | [diff] [blame] | 2370 | mlxsw_sp_port_switchdev_fini(mlxsw_sp_port); |
Ido Schimmel | 0597848 | 2016-08-17 16:39:30 +0200 | [diff] [blame] | 2371 | mlxsw_sp_port_pvid_vport_destroy(mlxsw_sp_port); |
| 2372 | err_port_pvid_vport_create: |
Ido Schimmel | 4de34eb | 2016-08-04 17:36:22 +0300 | [diff] [blame] | 2373 | mlxsw_sp_port_dcb_fini(mlxsw_sp_port); |
Ido Schimmel | f00817d | 2016-04-06 17:10:09 +0200 | [diff] [blame] | 2374 | err_port_dcb_init: |
Ido Schimmel | 90183b9 | 2016-04-06 17:10:08 +0200 | [diff] [blame] | 2375 | err_port_ets_init: |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 2376 | err_port_buffers_init: |
| 2377 | err_port_admin_status_set: |
| 2378 | err_port_mtu_set: |
Ido Schimmel | 18f1e70 | 2016-02-26 17:32:31 +0100 | [diff] [blame] | 2379 | err_port_speed_by_width_set: |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 2380 | err_port_system_port_mapping_set: |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 2381 | err_dev_addr_init: |
Ido Schimmel | 3247ff2 | 2016-09-08 08:16:02 +0200 | [diff] [blame] | 2382 | mlxsw_sp_port_swid_set(mlxsw_sp_port, MLXSW_PORT_SWID_DISABLED_PORT); |
| 2383 | err_port_swid_set: |
Nogah Frankel | fc1bbb0 | 2016-09-16 15:05:38 +0200 | [diff] [blame] | 2384 | kfree(mlxsw_sp_port->hw_stats.cache); |
| 2385 | err_alloc_hw_stats: |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 2386 | free_percpu(mlxsw_sp_port->pcpu_stats); |
| 2387 | err_alloc_stats: |
Elad Raz | fc1273a | 2016-01-06 13:01:11 +0100 | [diff] [blame] | 2388 | kfree(mlxsw_sp_port->untagged_vlans); |
| 2389 | err_port_untagged_vlans_alloc: |
Ido Schimmel | bd40e9d | 2015-12-15 16:03:36 +0100 | [diff] [blame] | 2390 | kfree(mlxsw_sp_port->active_vlans); |
| 2391 | err_port_active_vlans_alloc: |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 2392 | free_netdev(dev); |
| 2393 | return err; |
| 2394 | } |
| 2395 | |
Jiri Pirko | 67963a3 | 2016-10-28 21:35:55 +0200 | [diff] [blame] | 2396 | static int mlxsw_sp_port_create(struct mlxsw_sp *mlxsw_sp, u8 local_port, |
| 2397 | bool split, u8 module, u8 width, u8 lane) |
| 2398 | { |
| 2399 | int err; |
| 2400 | |
| 2401 | err = mlxsw_core_port_init(mlxsw_sp->core, local_port); |
| 2402 | if (err) { |
| 2403 | dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to init core port\n", |
| 2404 | local_port); |
| 2405 | return err; |
| 2406 | } |
| 2407 | err = __mlxsw_sp_port_create(mlxsw_sp, local_port, false, |
| 2408 | module, width, lane); |
| 2409 | if (err) |
| 2410 | goto err_port_create; |
| 2411 | return 0; |
| 2412 | |
| 2413 | err_port_create: |
| 2414 | mlxsw_core_port_fini(mlxsw_sp->core, local_port); |
| 2415 | return err; |
| 2416 | } |
| 2417 | |
| 2418 | static void __mlxsw_sp_port_remove(struct mlxsw_sp *mlxsw_sp, u8 local_port) |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 2419 | { |
| 2420 | struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp->ports[local_port]; |
| 2421 | |
Nogah Frankel | fc1bbb0 | 2016-09-16 15:05:38 +0200 | [diff] [blame] | 2422 | cancel_delayed_work_sync(&mlxsw_sp_port->hw_stats.update_dw); |
Jiri Pirko | 67963a3 | 2016-10-28 21:35:55 +0200 | [diff] [blame] | 2423 | mlxsw_core_port_clear(mlxsw_sp->core, local_port, mlxsw_sp); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 2424 | unregister_netdev(mlxsw_sp_port->dev); /* This calls ndo_stop */ |
Ido Schimmel | 2f25844 | 2016-08-17 16:39:31 +0200 | [diff] [blame] | 2425 | mlxsw_sp->ports[local_port] = NULL; |
Ido Schimmel | 0583272 | 2016-08-17 16:39:35 +0200 | [diff] [blame] | 2426 | mlxsw_sp_port_switchdev_fini(mlxsw_sp_port); |
Ido Schimmel | 0597848 | 2016-08-17 16:39:30 +0200 | [diff] [blame] | 2427 | mlxsw_sp_port_pvid_vport_destroy(mlxsw_sp_port); |
Ido Schimmel | f00817d | 2016-04-06 17:10:09 +0200 | [diff] [blame] | 2428 | mlxsw_sp_port_dcb_fini(mlxsw_sp_port); |
Ido Schimmel | 3e9b27b | 2016-02-26 17:32:28 +0100 | [diff] [blame] | 2429 | mlxsw_sp_port_swid_set(mlxsw_sp_port, MLXSW_PORT_SWID_DISABLED_PORT); |
| 2430 | mlxsw_sp_port_module_unmap(mlxsw_sp, mlxsw_sp_port->local_port); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 2431 | free_percpu(mlxsw_sp_port->pcpu_stats); |
Nogah Frankel | fc1bbb0 | 2016-09-16 15:05:38 +0200 | [diff] [blame] | 2432 | kfree(mlxsw_sp_port->hw_stats.cache); |
Elad Raz | fc1273a | 2016-01-06 13:01:11 +0100 | [diff] [blame] | 2433 | kfree(mlxsw_sp_port->untagged_vlans); |
Ido Schimmel | bd40e9d | 2015-12-15 16:03:36 +0100 | [diff] [blame] | 2434 | kfree(mlxsw_sp_port->active_vlans); |
Ido Schimmel | 32d863f | 2016-07-02 11:00:10 +0200 | [diff] [blame] | 2435 | WARN_ON_ONCE(!list_empty(&mlxsw_sp_port->vports_list)); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 2436 | free_netdev(mlxsw_sp_port->dev); |
| 2437 | } |
| 2438 | |
Jiri Pirko | 67963a3 | 2016-10-28 21:35:55 +0200 | [diff] [blame] | 2439 | static void mlxsw_sp_port_remove(struct mlxsw_sp *mlxsw_sp, u8 local_port) |
| 2440 | { |
| 2441 | __mlxsw_sp_port_remove(mlxsw_sp, local_port); |
| 2442 | mlxsw_core_port_fini(mlxsw_sp->core, local_port); |
| 2443 | } |
| 2444 | |
Jiri Pirko | f83e210 | 2016-10-28 21:35:49 +0200 | [diff] [blame] | 2445 | static bool mlxsw_sp_port_created(struct mlxsw_sp *mlxsw_sp, u8 local_port) |
| 2446 | { |
| 2447 | return mlxsw_sp->ports[local_port] != NULL; |
| 2448 | } |
| 2449 | |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 2450 | static void mlxsw_sp_ports_remove(struct mlxsw_sp *mlxsw_sp) |
| 2451 | { |
| 2452 | int i; |
| 2453 | |
| 2454 | for (i = 1; i < MLXSW_PORT_MAX_PORTS; i++) |
Jiri Pirko | f83e210 | 2016-10-28 21:35:49 +0200 | [diff] [blame] | 2455 | if (mlxsw_sp_port_created(mlxsw_sp, i)) |
| 2456 | mlxsw_sp_port_remove(mlxsw_sp, i); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 2457 | kfree(mlxsw_sp->ports); |
| 2458 | } |
| 2459 | |
| 2460 | static int mlxsw_sp_ports_create(struct mlxsw_sp *mlxsw_sp) |
| 2461 | { |
Ido Schimmel | d664b41 | 2016-06-09 09:51:40 +0200 | [diff] [blame] | 2462 | u8 module, width, lane; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 2463 | size_t alloc_size; |
| 2464 | int i; |
| 2465 | int err; |
| 2466 | |
| 2467 | alloc_size = sizeof(struct mlxsw_sp_port *) * MLXSW_PORT_MAX_PORTS; |
| 2468 | mlxsw_sp->ports = kzalloc(alloc_size, GFP_KERNEL); |
| 2469 | if (!mlxsw_sp->ports) |
| 2470 | return -ENOMEM; |
| 2471 | |
| 2472 | for (i = 1; i < MLXSW_PORT_MAX_PORTS; i++) { |
Ido Schimmel | 558c2d5 | 2016-02-26 17:32:29 +0100 | [diff] [blame] | 2473 | err = mlxsw_sp_port_module_info_get(mlxsw_sp, i, &module, |
Ido Schimmel | d664b41 | 2016-06-09 09:51:40 +0200 | [diff] [blame] | 2474 | &width, &lane); |
Ido Schimmel | 558c2d5 | 2016-02-26 17:32:29 +0100 | [diff] [blame] | 2475 | if (err) |
| 2476 | goto err_port_module_info_get; |
| 2477 | if (!width) |
| 2478 | continue; |
| 2479 | mlxsw_sp->port_to_module[i] = module; |
Jiri Pirko | 67963a3 | 2016-10-28 21:35:55 +0200 | [diff] [blame] | 2480 | err = mlxsw_sp_port_create(mlxsw_sp, i, false, |
| 2481 | module, width, lane); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 2482 | if (err) |
| 2483 | goto err_port_create; |
| 2484 | } |
| 2485 | return 0; |
| 2486 | |
| 2487 | err_port_create: |
Ido Schimmel | 558c2d5 | 2016-02-26 17:32:29 +0100 | [diff] [blame] | 2488 | err_port_module_info_get: |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 2489 | for (i--; i >= 1; i--) |
Jiri Pirko | f83e210 | 2016-10-28 21:35:49 +0200 | [diff] [blame] | 2490 | if (mlxsw_sp_port_created(mlxsw_sp, i)) |
| 2491 | mlxsw_sp_port_remove(mlxsw_sp, i); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 2492 | kfree(mlxsw_sp->ports); |
| 2493 | return err; |
| 2494 | } |
| 2495 | |
Ido Schimmel | 18f1e70 | 2016-02-26 17:32:31 +0100 | [diff] [blame] | 2496 | static u8 mlxsw_sp_cluster_base_port_get(u8 local_port) |
| 2497 | { |
| 2498 | u8 offset = (local_port - 1) % MLXSW_SP_PORTS_PER_CLUSTER_MAX; |
| 2499 | |
| 2500 | return local_port - offset; |
| 2501 | } |
| 2502 | |
Ido Schimmel | be94535 | 2016-06-09 09:51:39 +0200 | [diff] [blame] | 2503 | static int mlxsw_sp_port_split_create(struct mlxsw_sp *mlxsw_sp, u8 base_port, |
| 2504 | u8 module, unsigned int count) |
| 2505 | { |
| 2506 | u8 width = MLXSW_PORT_MODULE_MAX_WIDTH / count; |
| 2507 | int err, i; |
| 2508 | |
| 2509 | for (i = 0; i < count; i++) { |
| 2510 | err = mlxsw_sp_port_module_map(mlxsw_sp, base_port + i, module, |
| 2511 | width, i * width); |
| 2512 | if (err) |
| 2513 | goto err_port_module_map; |
| 2514 | } |
| 2515 | |
| 2516 | for (i = 0; i < count; i++) { |
| 2517 | err = __mlxsw_sp_port_swid_set(mlxsw_sp, base_port + i, 0); |
| 2518 | if (err) |
| 2519 | goto err_port_swid_set; |
| 2520 | } |
| 2521 | |
| 2522 | for (i = 0; i < count; i++) { |
| 2523 | err = mlxsw_sp_port_create(mlxsw_sp, base_port + i, true, |
Ido Schimmel | d664b41 | 2016-06-09 09:51:40 +0200 | [diff] [blame] | 2524 | module, width, i * width); |
Ido Schimmel | be94535 | 2016-06-09 09:51:39 +0200 | [diff] [blame] | 2525 | if (err) |
| 2526 | goto err_port_create; |
| 2527 | } |
| 2528 | |
| 2529 | return 0; |
| 2530 | |
| 2531 | err_port_create: |
| 2532 | for (i--; i >= 0; i--) |
Jiri Pirko | f83e210 | 2016-10-28 21:35:49 +0200 | [diff] [blame] | 2533 | if (mlxsw_sp_port_created(mlxsw_sp, base_port + i)) |
| 2534 | mlxsw_sp_port_remove(mlxsw_sp, base_port + i); |
Ido Schimmel | be94535 | 2016-06-09 09:51:39 +0200 | [diff] [blame] | 2535 | i = count; |
| 2536 | err_port_swid_set: |
| 2537 | for (i--; i >= 0; i--) |
| 2538 | __mlxsw_sp_port_swid_set(mlxsw_sp, base_port + i, |
| 2539 | MLXSW_PORT_SWID_DISABLED_PORT); |
| 2540 | i = count; |
| 2541 | err_port_module_map: |
| 2542 | for (i--; i >= 0; i--) |
| 2543 | mlxsw_sp_port_module_unmap(mlxsw_sp, base_port + i); |
| 2544 | return err; |
| 2545 | } |
| 2546 | |
| 2547 | static void mlxsw_sp_port_unsplit_create(struct mlxsw_sp *mlxsw_sp, |
| 2548 | u8 base_port, unsigned int count) |
| 2549 | { |
| 2550 | u8 local_port, module, width = MLXSW_PORT_MODULE_MAX_WIDTH; |
| 2551 | int i; |
| 2552 | |
| 2553 | /* Split by four means we need to re-create two ports, otherwise |
| 2554 | * only one. |
| 2555 | */ |
| 2556 | count = count / 2; |
| 2557 | |
| 2558 | for (i = 0; i < count; i++) { |
| 2559 | local_port = base_port + i * 2; |
| 2560 | module = mlxsw_sp->port_to_module[local_port]; |
| 2561 | |
| 2562 | mlxsw_sp_port_module_map(mlxsw_sp, local_port, module, width, |
| 2563 | 0); |
| 2564 | } |
| 2565 | |
| 2566 | for (i = 0; i < count; i++) |
| 2567 | __mlxsw_sp_port_swid_set(mlxsw_sp, base_port + i * 2, 0); |
| 2568 | |
| 2569 | for (i = 0; i < count; i++) { |
| 2570 | local_port = base_port + i * 2; |
| 2571 | module = mlxsw_sp->port_to_module[local_port]; |
| 2572 | |
| 2573 | mlxsw_sp_port_create(mlxsw_sp, local_port, false, module, |
Ido Schimmel | d664b41 | 2016-06-09 09:51:40 +0200 | [diff] [blame] | 2574 | width, 0); |
Ido Schimmel | be94535 | 2016-06-09 09:51:39 +0200 | [diff] [blame] | 2575 | } |
| 2576 | } |
| 2577 | |
Jiri Pirko | b2f1057 | 2016-04-08 19:11:23 +0200 | [diff] [blame] | 2578 | static int mlxsw_sp_port_split(struct mlxsw_core *mlxsw_core, u8 local_port, |
| 2579 | unsigned int count) |
Ido Schimmel | 18f1e70 | 2016-02-26 17:32:31 +0100 | [diff] [blame] | 2580 | { |
Jiri Pirko | b2f1057 | 2016-04-08 19:11:23 +0200 | [diff] [blame] | 2581 | struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core); |
Ido Schimmel | 18f1e70 | 2016-02-26 17:32:31 +0100 | [diff] [blame] | 2582 | struct mlxsw_sp_port *mlxsw_sp_port; |
Ido Schimmel | 18f1e70 | 2016-02-26 17:32:31 +0100 | [diff] [blame] | 2583 | u8 module, cur_width, base_port; |
| 2584 | int i; |
| 2585 | int err; |
| 2586 | |
| 2587 | mlxsw_sp_port = mlxsw_sp->ports[local_port]; |
| 2588 | if (!mlxsw_sp_port) { |
| 2589 | dev_err(mlxsw_sp->bus_info->dev, "Port number \"%d\" does not exist\n", |
| 2590 | local_port); |
| 2591 | return -EINVAL; |
| 2592 | } |
| 2593 | |
Ido Schimmel | d664b41 | 2016-06-09 09:51:40 +0200 | [diff] [blame] | 2594 | module = mlxsw_sp_port->mapping.module; |
| 2595 | cur_width = mlxsw_sp_port->mapping.width; |
| 2596 | |
Ido Schimmel | 18f1e70 | 2016-02-26 17:32:31 +0100 | [diff] [blame] | 2597 | if (count != 2 && count != 4) { |
| 2598 | netdev_err(mlxsw_sp_port->dev, "Port can only be split into 2 or 4 ports\n"); |
| 2599 | return -EINVAL; |
| 2600 | } |
| 2601 | |
Ido Schimmel | 18f1e70 | 2016-02-26 17:32:31 +0100 | [diff] [blame] | 2602 | if (cur_width != MLXSW_PORT_MODULE_MAX_WIDTH) { |
| 2603 | netdev_err(mlxsw_sp_port->dev, "Port cannot be split further\n"); |
| 2604 | return -EINVAL; |
| 2605 | } |
| 2606 | |
| 2607 | /* Make sure we have enough slave (even) ports for the split. */ |
| 2608 | if (count == 2) { |
| 2609 | base_port = local_port; |
| 2610 | if (mlxsw_sp->ports[base_port + 1]) { |
| 2611 | netdev_err(mlxsw_sp_port->dev, "Invalid split configuration\n"); |
| 2612 | return -EINVAL; |
| 2613 | } |
| 2614 | } else { |
| 2615 | base_port = mlxsw_sp_cluster_base_port_get(local_port); |
| 2616 | if (mlxsw_sp->ports[base_port + 1] || |
| 2617 | mlxsw_sp->ports[base_port + 3]) { |
| 2618 | netdev_err(mlxsw_sp_port->dev, "Invalid split configuration\n"); |
| 2619 | return -EINVAL; |
| 2620 | } |
| 2621 | } |
| 2622 | |
| 2623 | for (i = 0; i < count; i++) |
Jiri Pirko | f83e210 | 2016-10-28 21:35:49 +0200 | [diff] [blame] | 2624 | if (mlxsw_sp_port_created(mlxsw_sp, base_port + i)) |
| 2625 | mlxsw_sp_port_remove(mlxsw_sp, base_port + i); |
Ido Schimmel | 18f1e70 | 2016-02-26 17:32:31 +0100 | [diff] [blame] | 2626 | |
Ido Schimmel | be94535 | 2016-06-09 09:51:39 +0200 | [diff] [blame] | 2627 | err = mlxsw_sp_port_split_create(mlxsw_sp, base_port, module, count); |
| 2628 | if (err) { |
| 2629 | dev_err(mlxsw_sp->bus_info->dev, "Failed to create split ports\n"); |
| 2630 | goto err_port_split_create; |
Ido Schimmel | 18f1e70 | 2016-02-26 17:32:31 +0100 | [diff] [blame] | 2631 | } |
| 2632 | |
| 2633 | return 0; |
| 2634 | |
Ido Schimmel | be94535 | 2016-06-09 09:51:39 +0200 | [diff] [blame] | 2635 | err_port_split_create: |
| 2636 | mlxsw_sp_port_unsplit_create(mlxsw_sp, base_port, count); |
Ido Schimmel | 18f1e70 | 2016-02-26 17:32:31 +0100 | [diff] [blame] | 2637 | return err; |
| 2638 | } |
| 2639 | |
Jiri Pirko | b2f1057 | 2016-04-08 19:11:23 +0200 | [diff] [blame] | 2640 | static int mlxsw_sp_port_unsplit(struct mlxsw_core *mlxsw_core, u8 local_port) |
Ido Schimmel | 18f1e70 | 2016-02-26 17:32:31 +0100 | [diff] [blame] | 2641 | { |
Jiri Pirko | b2f1057 | 2016-04-08 19:11:23 +0200 | [diff] [blame] | 2642 | struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core); |
Ido Schimmel | 18f1e70 | 2016-02-26 17:32:31 +0100 | [diff] [blame] | 2643 | struct mlxsw_sp_port *mlxsw_sp_port; |
Ido Schimmel | d664b41 | 2016-06-09 09:51:40 +0200 | [diff] [blame] | 2644 | u8 cur_width, base_port; |
Ido Schimmel | 18f1e70 | 2016-02-26 17:32:31 +0100 | [diff] [blame] | 2645 | unsigned int count; |
| 2646 | int i; |
Ido Schimmel | 18f1e70 | 2016-02-26 17:32:31 +0100 | [diff] [blame] | 2647 | |
| 2648 | mlxsw_sp_port = mlxsw_sp->ports[local_port]; |
| 2649 | if (!mlxsw_sp_port) { |
| 2650 | dev_err(mlxsw_sp->bus_info->dev, "Port number \"%d\" does not exist\n", |
| 2651 | local_port); |
| 2652 | return -EINVAL; |
| 2653 | } |
| 2654 | |
| 2655 | if (!mlxsw_sp_port->split) { |
| 2656 | netdev_err(mlxsw_sp_port->dev, "Port wasn't split\n"); |
| 2657 | return -EINVAL; |
| 2658 | } |
| 2659 | |
Ido Schimmel | d664b41 | 2016-06-09 09:51:40 +0200 | [diff] [blame] | 2660 | cur_width = mlxsw_sp_port->mapping.width; |
Ido Schimmel | 18f1e70 | 2016-02-26 17:32:31 +0100 | [diff] [blame] | 2661 | count = cur_width == 1 ? 4 : 2; |
| 2662 | |
| 2663 | base_port = mlxsw_sp_cluster_base_port_get(local_port); |
| 2664 | |
| 2665 | /* Determine which ports to remove. */ |
| 2666 | if (count == 2 && local_port >= base_port + 2) |
| 2667 | base_port = base_port + 2; |
| 2668 | |
| 2669 | for (i = 0; i < count; i++) |
Jiri Pirko | f83e210 | 2016-10-28 21:35:49 +0200 | [diff] [blame] | 2670 | if (mlxsw_sp_port_created(mlxsw_sp, base_port + i)) |
| 2671 | mlxsw_sp_port_remove(mlxsw_sp, base_port + i); |
Ido Schimmel | 18f1e70 | 2016-02-26 17:32:31 +0100 | [diff] [blame] | 2672 | |
Ido Schimmel | be94535 | 2016-06-09 09:51:39 +0200 | [diff] [blame] | 2673 | mlxsw_sp_port_unsplit_create(mlxsw_sp, base_port, count); |
Ido Schimmel | 18f1e70 | 2016-02-26 17:32:31 +0100 | [diff] [blame] | 2674 | |
| 2675 | return 0; |
| 2676 | } |
| 2677 | |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 2678 | static void mlxsw_sp_pude_event_func(const struct mlxsw_reg_info *reg, |
| 2679 | char *pude_pl, void *priv) |
| 2680 | { |
| 2681 | struct mlxsw_sp *mlxsw_sp = priv; |
| 2682 | struct mlxsw_sp_port *mlxsw_sp_port; |
| 2683 | enum mlxsw_reg_pude_oper_status status; |
| 2684 | u8 local_port; |
| 2685 | |
| 2686 | local_port = mlxsw_reg_pude_local_port_get(pude_pl); |
| 2687 | mlxsw_sp_port = mlxsw_sp->ports[local_port]; |
Ido Schimmel | bbf2a47 | 2016-07-02 11:00:14 +0200 | [diff] [blame] | 2688 | if (!mlxsw_sp_port) |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 2689 | return; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 2690 | |
| 2691 | status = mlxsw_reg_pude_oper_status_get(pude_pl); |
| 2692 | if (status == MLXSW_PORT_OPER_STATUS_UP) { |
| 2693 | netdev_info(mlxsw_sp_port->dev, "link up\n"); |
| 2694 | netif_carrier_on(mlxsw_sp_port->dev); |
| 2695 | } else { |
| 2696 | netdev_info(mlxsw_sp_port->dev, "link down\n"); |
| 2697 | netif_carrier_off(mlxsw_sp_port->dev); |
| 2698 | } |
| 2699 | } |
| 2700 | |
| 2701 | static struct mlxsw_event_listener mlxsw_sp_pude_event = { |
| 2702 | .func = mlxsw_sp_pude_event_func, |
| 2703 | .trap_id = MLXSW_TRAP_ID_PUDE, |
| 2704 | }; |
| 2705 | |
| 2706 | static int mlxsw_sp_event_register(struct mlxsw_sp *mlxsw_sp, |
| 2707 | enum mlxsw_event_trap_id trap_id) |
| 2708 | { |
| 2709 | struct mlxsw_event_listener *el; |
| 2710 | char hpkt_pl[MLXSW_REG_HPKT_LEN]; |
| 2711 | int err; |
| 2712 | |
| 2713 | switch (trap_id) { |
| 2714 | case MLXSW_TRAP_ID_PUDE: |
| 2715 | el = &mlxsw_sp_pude_event; |
| 2716 | break; |
| 2717 | } |
| 2718 | err = mlxsw_core_event_listener_register(mlxsw_sp->core, el, mlxsw_sp); |
| 2719 | if (err) |
| 2720 | return err; |
| 2721 | |
| 2722 | mlxsw_reg_hpkt_pack(hpkt_pl, MLXSW_REG_HPKT_ACTION_FORWARD, trap_id); |
| 2723 | err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(hpkt), hpkt_pl); |
| 2724 | if (err) |
| 2725 | goto err_event_trap_set; |
| 2726 | |
| 2727 | return 0; |
| 2728 | |
| 2729 | err_event_trap_set: |
| 2730 | mlxsw_core_event_listener_unregister(mlxsw_sp->core, el, mlxsw_sp); |
| 2731 | return err; |
| 2732 | } |
| 2733 | |
| 2734 | static void mlxsw_sp_event_unregister(struct mlxsw_sp *mlxsw_sp, |
| 2735 | enum mlxsw_event_trap_id trap_id) |
| 2736 | { |
| 2737 | struct mlxsw_event_listener *el; |
| 2738 | |
| 2739 | switch (trap_id) { |
| 2740 | case MLXSW_TRAP_ID_PUDE: |
| 2741 | el = &mlxsw_sp_pude_event; |
| 2742 | break; |
| 2743 | } |
| 2744 | mlxsw_core_event_listener_unregister(mlxsw_sp->core, el, mlxsw_sp); |
| 2745 | } |
| 2746 | |
| 2747 | static void mlxsw_sp_rx_listener_func(struct sk_buff *skb, u8 local_port, |
| 2748 | void *priv) |
| 2749 | { |
| 2750 | struct mlxsw_sp *mlxsw_sp = priv; |
| 2751 | struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp->ports[local_port]; |
| 2752 | struct mlxsw_sp_port_pcpu_stats *pcpu_stats; |
| 2753 | |
| 2754 | if (unlikely(!mlxsw_sp_port)) { |
| 2755 | dev_warn_ratelimited(mlxsw_sp->bus_info->dev, "Port %d: skb received for non-existent port\n", |
| 2756 | local_port); |
| 2757 | return; |
| 2758 | } |
| 2759 | |
| 2760 | skb->dev = mlxsw_sp_port->dev; |
| 2761 | |
| 2762 | pcpu_stats = this_cpu_ptr(mlxsw_sp_port->pcpu_stats); |
| 2763 | u64_stats_update_begin(&pcpu_stats->syncp); |
| 2764 | pcpu_stats->rx_packets++; |
| 2765 | pcpu_stats->rx_bytes += skb->len; |
| 2766 | u64_stats_update_end(&pcpu_stats->syncp); |
| 2767 | |
| 2768 | skb->protocol = eth_type_trans(skb, skb->dev); |
| 2769 | netif_receive_skb(skb); |
| 2770 | } |
| 2771 | |
Ido Schimmel | 1c6c6d2 | 2016-08-25 18:42:40 +0200 | [diff] [blame] | 2772 | static void mlxsw_sp_rx_listener_mark_func(struct sk_buff *skb, u8 local_port, |
| 2773 | void *priv) |
| 2774 | { |
| 2775 | skb->offload_fwd_mark = 1; |
| 2776 | return mlxsw_sp_rx_listener_func(skb, local_port, priv); |
| 2777 | } |
| 2778 | |
Ido Schimmel | 63a8114 | 2016-08-25 18:42:39 +0200 | [diff] [blame] | 2779 | #define MLXSW_SP_RXL(_func, _trap_id, _action) \ |
| 2780 | { \ |
| 2781 | .func = _func, \ |
| 2782 | .local_port = MLXSW_PORT_DONT_CARE, \ |
| 2783 | .trap_id = MLXSW_TRAP_ID_##_trap_id, \ |
| 2784 | .action = MLXSW_REG_HPKT_ACTION_##_action, \ |
Ido Schimmel | 93393b3 | 2016-08-25 18:42:38 +0200 | [diff] [blame] | 2785 | } |
| 2786 | |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 2787 | static const struct mlxsw_rx_listener mlxsw_sp_rx_listener[] = { |
Nogah Frankel | ee4a60d | 2016-11-25 10:33:29 +0100 | [diff] [blame^] | 2788 | /* L2 traps */ |
Ido Schimmel | 63a8114 | 2016-08-25 18:42:39 +0200 | [diff] [blame] | 2789 | MLXSW_SP_RXL(mlxsw_sp_rx_listener_func, STP, TRAP_TO_CPU), |
| 2790 | MLXSW_SP_RXL(mlxsw_sp_rx_listener_func, LACP, TRAP_TO_CPU), |
Ido Schimmel | 63a8114 | 2016-08-25 18:42:39 +0200 | [diff] [blame] | 2791 | MLXSW_SP_RXL(mlxsw_sp_rx_listener_func, LLDP, TRAP_TO_CPU), |
Ido Schimmel | 1c6c6d2 | 2016-08-25 18:42:40 +0200 | [diff] [blame] | 2792 | MLXSW_SP_RXL(mlxsw_sp_rx_listener_mark_func, DHCP, MIRROR_TO_CPU), |
| 2793 | MLXSW_SP_RXL(mlxsw_sp_rx_listener_mark_func, IGMP_QUERY, MIRROR_TO_CPU), |
Ido Schimmel | 63a8114 | 2016-08-25 18:42:39 +0200 | [diff] [blame] | 2794 | MLXSW_SP_RXL(mlxsw_sp_rx_listener_func, IGMP_V1_REPORT, TRAP_TO_CPU), |
| 2795 | MLXSW_SP_RXL(mlxsw_sp_rx_listener_func, IGMP_V2_REPORT, TRAP_TO_CPU), |
| 2796 | MLXSW_SP_RXL(mlxsw_sp_rx_listener_func, IGMP_V2_LEAVE, TRAP_TO_CPU), |
| 2797 | MLXSW_SP_RXL(mlxsw_sp_rx_listener_func, IGMP_V3_REPORT, TRAP_TO_CPU), |
Ido Schimmel | 1c6c6d2 | 2016-08-25 18:42:40 +0200 | [diff] [blame] | 2798 | MLXSW_SP_RXL(mlxsw_sp_rx_listener_mark_func, ARPBC, MIRROR_TO_CPU), |
| 2799 | MLXSW_SP_RXL(mlxsw_sp_rx_listener_mark_func, ARPUC, MIRROR_TO_CPU), |
Ido Schimmel | 93393b3 | 2016-08-25 18:42:38 +0200 | [diff] [blame] | 2800 | /* L3 traps */ |
Ido Schimmel | 63a8114 | 2016-08-25 18:42:39 +0200 | [diff] [blame] | 2801 | MLXSW_SP_RXL(mlxsw_sp_rx_listener_func, MTUERROR, TRAP_TO_CPU), |
| 2802 | MLXSW_SP_RXL(mlxsw_sp_rx_listener_func, TTLERROR, TRAP_TO_CPU), |
| 2803 | MLXSW_SP_RXL(mlxsw_sp_rx_listener_func, LBERROR, TRAP_TO_CPU), |
Ido Schimmel | 1c6c6d2 | 2016-08-25 18:42:40 +0200 | [diff] [blame] | 2804 | MLXSW_SP_RXL(mlxsw_sp_rx_listener_mark_func, OSPF, TRAP_TO_CPU), |
Ido Schimmel | 63a8114 | 2016-08-25 18:42:39 +0200 | [diff] [blame] | 2805 | MLXSW_SP_RXL(mlxsw_sp_rx_listener_func, IP2ME, TRAP_TO_CPU), |
| 2806 | MLXSW_SP_RXL(mlxsw_sp_rx_listener_func, RTR_INGRESS0, TRAP_TO_CPU), |
| 2807 | MLXSW_SP_RXL(mlxsw_sp_rx_listener_func, HOST_MISS_IPV4, TRAP_TO_CPU), |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 2808 | }; |
| 2809 | |
| 2810 | static int mlxsw_sp_traps_init(struct mlxsw_sp *mlxsw_sp) |
| 2811 | { |
| 2812 | char htgt_pl[MLXSW_REG_HTGT_LEN]; |
| 2813 | char hpkt_pl[MLXSW_REG_HPKT_LEN]; |
| 2814 | int i; |
| 2815 | int err; |
| 2816 | |
| 2817 | mlxsw_reg_htgt_pack(htgt_pl, MLXSW_REG_HTGT_TRAP_GROUP_RX); |
| 2818 | err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(htgt), htgt_pl); |
| 2819 | if (err) |
| 2820 | return err; |
| 2821 | |
| 2822 | mlxsw_reg_htgt_pack(htgt_pl, MLXSW_REG_HTGT_TRAP_GROUP_CTRL); |
| 2823 | err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(htgt), htgt_pl); |
| 2824 | if (err) |
| 2825 | return err; |
| 2826 | |
| 2827 | for (i = 0; i < ARRAY_SIZE(mlxsw_sp_rx_listener); i++) { |
| 2828 | err = mlxsw_core_rx_listener_register(mlxsw_sp->core, |
| 2829 | &mlxsw_sp_rx_listener[i], |
| 2830 | mlxsw_sp); |
| 2831 | if (err) |
| 2832 | goto err_rx_listener_register; |
| 2833 | |
Ido Schimmel | 63a8114 | 2016-08-25 18:42:39 +0200 | [diff] [blame] | 2834 | mlxsw_reg_hpkt_pack(hpkt_pl, mlxsw_sp_rx_listener[i].action, |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 2835 | mlxsw_sp_rx_listener[i].trap_id); |
| 2836 | err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(hpkt), hpkt_pl); |
| 2837 | if (err) |
| 2838 | goto err_rx_trap_set; |
| 2839 | } |
| 2840 | return 0; |
| 2841 | |
| 2842 | err_rx_trap_set: |
| 2843 | mlxsw_core_rx_listener_unregister(mlxsw_sp->core, |
| 2844 | &mlxsw_sp_rx_listener[i], |
| 2845 | mlxsw_sp); |
| 2846 | err_rx_listener_register: |
| 2847 | for (i--; i >= 0; i--) { |
Ido Schimmel | 10f00aa | 2016-07-02 11:00:19 +0200 | [diff] [blame] | 2848 | mlxsw_reg_hpkt_pack(hpkt_pl, MLXSW_REG_HPKT_ACTION_DISCARD, |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 2849 | mlxsw_sp_rx_listener[i].trap_id); |
| 2850 | mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(hpkt), hpkt_pl); |
| 2851 | |
| 2852 | mlxsw_core_rx_listener_unregister(mlxsw_sp->core, |
| 2853 | &mlxsw_sp_rx_listener[i], |
| 2854 | mlxsw_sp); |
| 2855 | } |
| 2856 | return err; |
| 2857 | } |
| 2858 | |
| 2859 | static void mlxsw_sp_traps_fini(struct mlxsw_sp *mlxsw_sp) |
| 2860 | { |
| 2861 | char hpkt_pl[MLXSW_REG_HPKT_LEN]; |
| 2862 | int i; |
| 2863 | |
| 2864 | for (i = 0; i < ARRAY_SIZE(mlxsw_sp_rx_listener); i++) { |
Ido Schimmel | 10f00aa | 2016-07-02 11:00:19 +0200 | [diff] [blame] | 2865 | mlxsw_reg_hpkt_pack(hpkt_pl, MLXSW_REG_HPKT_ACTION_DISCARD, |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 2866 | mlxsw_sp_rx_listener[i].trap_id); |
| 2867 | mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(hpkt), hpkt_pl); |
| 2868 | |
| 2869 | mlxsw_core_rx_listener_unregister(mlxsw_sp->core, |
| 2870 | &mlxsw_sp_rx_listener[i], |
| 2871 | mlxsw_sp); |
| 2872 | } |
| 2873 | } |
| 2874 | |
| 2875 | static int __mlxsw_sp_flood_init(struct mlxsw_core *mlxsw_core, |
| 2876 | enum mlxsw_reg_sfgc_type type, |
| 2877 | enum mlxsw_reg_sfgc_bridge_type bridge_type) |
| 2878 | { |
| 2879 | enum mlxsw_flood_table_type table_type; |
| 2880 | enum mlxsw_sp_flood_table flood_table; |
| 2881 | char sfgc_pl[MLXSW_REG_SFGC_LEN]; |
| 2882 | |
Ido Schimmel | 19ae612 | 2015-12-15 16:03:39 +0100 | [diff] [blame] | 2883 | if (bridge_type == MLXSW_REG_SFGC_BRIDGE_TYPE_VFID) |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 2884 | table_type = MLXSW_REG_SFGC_TABLE_TYPE_FID; |
Ido Schimmel | 19ae612 | 2015-12-15 16:03:39 +0100 | [diff] [blame] | 2885 | else |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 2886 | table_type = MLXSW_REG_SFGC_TABLE_TYPE_FID_OFFEST; |
Ido Schimmel | 19ae612 | 2015-12-15 16:03:39 +0100 | [diff] [blame] | 2887 | |
| 2888 | if (type == MLXSW_REG_SFGC_TYPE_UNKNOWN_UNICAST) |
| 2889 | flood_table = MLXSW_SP_FLOOD_TABLE_UC; |
| 2890 | else |
| 2891 | flood_table = MLXSW_SP_FLOOD_TABLE_BM; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 2892 | |
| 2893 | mlxsw_reg_sfgc_pack(sfgc_pl, type, bridge_type, table_type, |
| 2894 | flood_table); |
| 2895 | return mlxsw_reg_write(mlxsw_core, MLXSW_REG(sfgc), sfgc_pl); |
| 2896 | } |
| 2897 | |
| 2898 | static int mlxsw_sp_flood_init(struct mlxsw_sp *mlxsw_sp) |
| 2899 | { |
| 2900 | int type, err; |
| 2901 | |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 2902 | for (type = 0; type < MLXSW_REG_SFGC_TYPE_MAX; type++) { |
| 2903 | if (type == MLXSW_REG_SFGC_TYPE_RESERVED) |
| 2904 | continue; |
| 2905 | |
| 2906 | err = __mlxsw_sp_flood_init(mlxsw_sp->core, type, |
| 2907 | MLXSW_REG_SFGC_BRIDGE_TYPE_VFID); |
| 2908 | if (err) |
| 2909 | return err; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 2910 | |
| 2911 | err = __mlxsw_sp_flood_init(mlxsw_sp->core, type, |
| 2912 | MLXSW_REG_SFGC_BRIDGE_TYPE_1Q_FID); |
| 2913 | if (err) |
| 2914 | return err; |
| 2915 | } |
| 2916 | |
| 2917 | return 0; |
| 2918 | } |
| 2919 | |
Jiri Pirko | 0d65fc1 | 2015-12-03 12:12:28 +0100 | [diff] [blame] | 2920 | static int mlxsw_sp_lag_init(struct mlxsw_sp *mlxsw_sp) |
| 2921 | { |
| 2922 | char slcr_pl[MLXSW_REG_SLCR_LEN]; |
Nogah Frankel | ce0bd2b | 2016-09-20 11:16:50 +0200 | [diff] [blame] | 2923 | int err; |
Jiri Pirko | 0d65fc1 | 2015-12-03 12:12:28 +0100 | [diff] [blame] | 2924 | |
| 2925 | mlxsw_reg_slcr_pack(slcr_pl, MLXSW_REG_SLCR_LAG_HASH_SMAC | |
| 2926 | MLXSW_REG_SLCR_LAG_HASH_DMAC | |
| 2927 | MLXSW_REG_SLCR_LAG_HASH_ETHERTYPE | |
| 2928 | MLXSW_REG_SLCR_LAG_HASH_VLANID | |
| 2929 | MLXSW_REG_SLCR_LAG_HASH_SIP | |
| 2930 | MLXSW_REG_SLCR_LAG_HASH_DIP | |
| 2931 | MLXSW_REG_SLCR_LAG_HASH_SPORT | |
| 2932 | MLXSW_REG_SLCR_LAG_HASH_DPORT | |
| 2933 | MLXSW_REG_SLCR_LAG_HASH_IPPROTO); |
Nogah Frankel | ce0bd2b | 2016-09-20 11:16:50 +0200 | [diff] [blame] | 2934 | err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(slcr), slcr_pl); |
| 2935 | if (err) |
| 2936 | return err; |
| 2937 | |
Jiri Pirko | c1a3831 | 2016-10-21 16:07:23 +0200 | [diff] [blame] | 2938 | if (!MLXSW_CORE_RES_VALID(mlxsw_sp->core, MAX_LAG) || |
| 2939 | !MLXSW_CORE_RES_VALID(mlxsw_sp->core, MAX_LAG_MEMBERS)) |
Nogah Frankel | ce0bd2b | 2016-09-20 11:16:50 +0200 | [diff] [blame] | 2940 | return -EIO; |
| 2941 | |
Jiri Pirko | c1a3831 | 2016-10-21 16:07:23 +0200 | [diff] [blame] | 2942 | mlxsw_sp->lags = kcalloc(MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_LAG), |
Nogah Frankel | ce0bd2b | 2016-09-20 11:16:50 +0200 | [diff] [blame] | 2943 | sizeof(struct mlxsw_sp_upper), |
| 2944 | GFP_KERNEL); |
| 2945 | if (!mlxsw_sp->lags) |
| 2946 | return -ENOMEM; |
| 2947 | |
| 2948 | return 0; |
| 2949 | } |
| 2950 | |
| 2951 | static void mlxsw_sp_lag_fini(struct mlxsw_sp *mlxsw_sp) |
| 2952 | { |
| 2953 | kfree(mlxsw_sp->lags); |
Jiri Pirko | 0d65fc1 | 2015-12-03 12:12:28 +0100 | [diff] [blame] | 2954 | } |
| 2955 | |
Jiri Pirko | b2f1057 | 2016-04-08 19:11:23 +0200 | [diff] [blame] | 2956 | static int mlxsw_sp_init(struct mlxsw_core *mlxsw_core, |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 2957 | const struct mlxsw_bus_info *mlxsw_bus_info) |
| 2958 | { |
Jiri Pirko | b2f1057 | 2016-04-08 19:11:23 +0200 | [diff] [blame] | 2959 | struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 2960 | int err; |
| 2961 | |
| 2962 | mlxsw_sp->core = mlxsw_core; |
| 2963 | mlxsw_sp->bus_info = mlxsw_bus_info; |
Ido Schimmel | 14d3946 | 2016-06-20 23:04:15 +0200 | [diff] [blame] | 2964 | INIT_LIST_HEAD(&mlxsw_sp->fids); |
Ido Schimmel | 3ba2ebf | 2016-07-04 08:23:15 +0200 | [diff] [blame] | 2965 | INIT_LIST_HEAD(&mlxsw_sp->vfids.list); |
Elad Raz | 3a49b4f | 2016-01-10 21:06:28 +0100 | [diff] [blame] | 2966 | INIT_LIST_HEAD(&mlxsw_sp->br_mids.list); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 2967 | |
| 2968 | err = mlxsw_sp_base_mac_get(mlxsw_sp); |
| 2969 | if (err) { |
| 2970 | dev_err(mlxsw_sp->bus_info->dev, "Failed to get base mac\n"); |
| 2971 | return err; |
| 2972 | } |
| 2973 | |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 2974 | err = mlxsw_sp_event_register(mlxsw_sp, MLXSW_TRAP_ID_PUDE); |
| 2975 | if (err) { |
| 2976 | dev_err(mlxsw_sp->bus_info->dev, "Failed to register for PUDE events\n"); |
Ido Schimmel | bbf2a47 | 2016-07-02 11:00:14 +0200 | [diff] [blame] | 2977 | return err; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 2978 | } |
| 2979 | |
| 2980 | err = mlxsw_sp_traps_init(mlxsw_sp); |
| 2981 | if (err) { |
| 2982 | dev_err(mlxsw_sp->bus_info->dev, "Failed to set traps for RX\n"); |
| 2983 | goto err_rx_listener_register; |
| 2984 | } |
| 2985 | |
| 2986 | err = mlxsw_sp_flood_init(mlxsw_sp); |
| 2987 | if (err) { |
| 2988 | dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize flood tables\n"); |
| 2989 | goto err_flood_init; |
| 2990 | } |
| 2991 | |
| 2992 | err = mlxsw_sp_buffers_init(mlxsw_sp); |
| 2993 | if (err) { |
| 2994 | dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize buffers\n"); |
| 2995 | goto err_buffers_init; |
| 2996 | } |
| 2997 | |
Jiri Pirko | 0d65fc1 | 2015-12-03 12:12:28 +0100 | [diff] [blame] | 2998 | err = mlxsw_sp_lag_init(mlxsw_sp); |
| 2999 | if (err) { |
| 3000 | dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize LAG\n"); |
| 3001 | goto err_lag_init; |
| 3002 | } |
| 3003 | |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 3004 | err = mlxsw_sp_switchdev_init(mlxsw_sp); |
| 3005 | if (err) { |
| 3006 | dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize switchdev\n"); |
| 3007 | goto err_switchdev_init; |
| 3008 | } |
| 3009 | |
Ido Schimmel | 464dce1 | 2016-07-02 11:00:15 +0200 | [diff] [blame] | 3010 | err = mlxsw_sp_router_init(mlxsw_sp); |
| 3011 | if (err) { |
| 3012 | dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize router\n"); |
| 3013 | goto err_router_init; |
| 3014 | } |
| 3015 | |
Yotam Gigi | 763b4b7 | 2016-07-21 12:03:17 +0200 | [diff] [blame] | 3016 | err = mlxsw_sp_span_init(mlxsw_sp); |
| 3017 | if (err) { |
| 3018 | dev_err(mlxsw_sp->bus_info->dev, "Failed to init span system\n"); |
| 3019 | goto err_span_init; |
| 3020 | } |
| 3021 | |
Ido Schimmel | bbf2a47 | 2016-07-02 11:00:14 +0200 | [diff] [blame] | 3022 | err = mlxsw_sp_ports_create(mlxsw_sp); |
| 3023 | if (err) { |
| 3024 | dev_err(mlxsw_sp->bus_info->dev, "Failed to create ports\n"); |
| 3025 | goto err_ports_create; |
| 3026 | } |
| 3027 | |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 3028 | return 0; |
| 3029 | |
Ido Schimmel | bbf2a47 | 2016-07-02 11:00:14 +0200 | [diff] [blame] | 3030 | err_ports_create: |
Yotam Gigi | 763b4b7 | 2016-07-21 12:03:17 +0200 | [diff] [blame] | 3031 | mlxsw_sp_span_fini(mlxsw_sp); |
| 3032 | err_span_init: |
Ido Schimmel | 464dce1 | 2016-07-02 11:00:15 +0200 | [diff] [blame] | 3033 | mlxsw_sp_router_fini(mlxsw_sp); |
| 3034 | err_router_init: |
Ido Schimmel | bbf2a47 | 2016-07-02 11:00:14 +0200 | [diff] [blame] | 3035 | mlxsw_sp_switchdev_fini(mlxsw_sp); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 3036 | err_switchdev_init: |
Nogah Frankel | ce0bd2b | 2016-09-20 11:16:50 +0200 | [diff] [blame] | 3037 | mlxsw_sp_lag_fini(mlxsw_sp); |
Jiri Pirko | 0d65fc1 | 2015-12-03 12:12:28 +0100 | [diff] [blame] | 3038 | err_lag_init: |
Jiri Pirko | 0f433fa | 2016-04-14 18:19:24 +0200 | [diff] [blame] | 3039 | mlxsw_sp_buffers_fini(mlxsw_sp); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 3040 | err_buffers_init: |
| 3041 | err_flood_init: |
| 3042 | mlxsw_sp_traps_fini(mlxsw_sp); |
| 3043 | err_rx_listener_register: |
| 3044 | mlxsw_sp_event_unregister(mlxsw_sp, MLXSW_TRAP_ID_PUDE); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 3045 | return err; |
| 3046 | } |
| 3047 | |
Jiri Pirko | b2f1057 | 2016-04-08 19:11:23 +0200 | [diff] [blame] | 3048 | static void mlxsw_sp_fini(struct mlxsw_core *mlxsw_core) |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 3049 | { |
Jiri Pirko | b2f1057 | 2016-04-08 19:11:23 +0200 | [diff] [blame] | 3050 | struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 3051 | |
Ido Schimmel | bbf2a47 | 2016-07-02 11:00:14 +0200 | [diff] [blame] | 3052 | mlxsw_sp_ports_remove(mlxsw_sp); |
Yotam Gigi | 763b4b7 | 2016-07-21 12:03:17 +0200 | [diff] [blame] | 3053 | mlxsw_sp_span_fini(mlxsw_sp); |
Ido Schimmel | 464dce1 | 2016-07-02 11:00:15 +0200 | [diff] [blame] | 3054 | mlxsw_sp_router_fini(mlxsw_sp); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 3055 | mlxsw_sp_switchdev_fini(mlxsw_sp); |
Nogah Frankel | ce0bd2b | 2016-09-20 11:16:50 +0200 | [diff] [blame] | 3056 | mlxsw_sp_lag_fini(mlxsw_sp); |
Jiri Pirko | 5113bfd | 2016-05-06 22:20:59 +0200 | [diff] [blame] | 3057 | mlxsw_sp_buffers_fini(mlxsw_sp); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 3058 | mlxsw_sp_traps_fini(mlxsw_sp); |
| 3059 | mlxsw_sp_event_unregister(mlxsw_sp, MLXSW_TRAP_ID_PUDE); |
Ido Schimmel | 3ba2ebf | 2016-07-04 08:23:15 +0200 | [diff] [blame] | 3060 | WARN_ON(!list_empty(&mlxsw_sp->vfids.list)); |
Ido Schimmel | 14d3946 | 2016-06-20 23:04:15 +0200 | [diff] [blame] | 3061 | WARN_ON(!list_empty(&mlxsw_sp->fids)); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 3062 | } |
| 3063 | |
| 3064 | static struct mlxsw_config_profile mlxsw_sp_config_profile = { |
| 3065 | .used_max_vepa_channels = 1, |
| 3066 | .max_vepa_channels = 0, |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 3067 | .used_max_mid = 1, |
Elad Raz | 53ae628 | 2016-01-10 21:06:26 +0100 | [diff] [blame] | 3068 | .max_mid = MLXSW_SP_MID_MAX, |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 3069 | .used_max_pgt = 1, |
| 3070 | .max_pgt = 0, |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 3071 | .used_flood_tables = 1, |
| 3072 | .used_flood_mode = 1, |
| 3073 | .flood_mode = 3, |
| 3074 | .max_fid_offset_flood_tables = 2, |
| 3075 | .fid_offset_flood_table_size = VLAN_N_VID - 1, |
Ido Schimmel | 19ae612 | 2015-12-15 16:03:39 +0100 | [diff] [blame] | 3076 | .max_fid_flood_tables = 2, |
| 3077 | .fid_flood_table_size = MLXSW_SP_VFID_MAX, |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 3078 | .used_max_ib_mc = 1, |
| 3079 | .max_ib_mc = 0, |
| 3080 | .used_max_pkey = 1, |
| 3081 | .max_pkey = 0, |
Nogah Frankel | 403547d | 2016-09-20 11:16:52 +0200 | [diff] [blame] | 3082 | .used_kvd_split_data = 1, |
| 3083 | .kvd_hash_granularity = MLXSW_SP_KVD_GRANULARITY, |
| 3084 | .kvd_hash_single_parts = 2, |
| 3085 | .kvd_hash_double_parts = 1, |
Jiri Pirko | c602242 | 2016-07-05 11:27:46 +0200 | [diff] [blame] | 3086 | .kvd_linear_size = MLXSW_SP_KVD_LINEAR_SIZE, |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 3087 | .swid_config = { |
| 3088 | { |
| 3089 | .used_type = 1, |
| 3090 | .type = MLXSW_PORT_SWID_TYPE_ETH, |
| 3091 | } |
| 3092 | }, |
Nogah Frankel | 57d316b | 2016-07-21 12:03:09 +0200 | [diff] [blame] | 3093 | .resource_query_enable = 1, |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 3094 | }; |
| 3095 | |
| 3096 | static struct mlxsw_driver mlxsw_sp_driver = { |
Jiri Pirko | 1d20d23 | 2016-10-27 15:12:59 +0200 | [diff] [blame] | 3097 | .kind = mlxsw_sp_driver_name, |
Jiri Pirko | 2d0ed39 | 2016-04-14 18:19:30 +0200 | [diff] [blame] | 3098 | .priv_size = sizeof(struct mlxsw_sp), |
| 3099 | .init = mlxsw_sp_init, |
| 3100 | .fini = mlxsw_sp_fini, |
| 3101 | .port_split = mlxsw_sp_port_split, |
| 3102 | .port_unsplit = mlxsw_sp_port_unsplit, |
| 3103 | .sb_pool_get = mlxsw_sp_sb_pool_get, |
| 3104 | .sb_pool_set = mlxsw_sp_sb_pool_set, |
| 3105 | .sb_port_pool_get = mlxsw_sp_sb_port_pool_get, |
| 3106 | .sb_port_pool_set = mlxsw_sp_sb_port_pool_set, |
| 3107 | .sb_tc_pool_bind_get = mlxsw_sp_sb_tc_pool_bind_get, |
| 3108 | .sb_tc_pool_bind_set = mlxsw_sp_sb_tc_pool_bind_set, |
| 3109 | .sb_occ_snapshot = mlxsw_sp_sb_occ_snapshot, |
| 3110 | .sb_occ_max_clear = mlxsw_sp_sb_occ_max_clear, |
| 3111 | .sb_occ_port_pool_get = mlxsw_sp_sb_occ_port_pool_get, |
| 3112 | .sb_occ_tc_port_bind_get = mlxsw_sp_sb_occ_tc_port_bind_get, |
| 3113 | .txhdr_construct = mlxsw_sp_txhdr_construct, |
| 3114 | .txhdr_len = MLXSW_TXHDR_LEN, |
| 3115 | .profile = &mlxsw_sp_config_profile, |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 3116 | }; |
| 3117 | |
Jiri Pirko | 7ce856a | 2016-07-04 08:23:12 +0200 | [diff] [blame] | 3118 | static bool mlxsw_sp_port_dev_check(const struct net_device *dev) |
| 3119 | { |
| 3120 | return dev->netdev_ops == &mlxsw_sp_port_netdev_ops; |
| 3121 | } |
| 3122 | |
David Ahern | dd82364 | 2016-10-17 19:15:49 -0700 | [diff] [blame] | 3123 | static int mlxsw_lower_dev_walk(struct net_device *lower_dev, void *data) |
| 3124 | { |
| 3125 | struct mlxsw_sp_port **port = data; |
| 3126 | int ret = 0; |
| 3127 | |
| 3128 | if (mlxsw_sp_port_dev_check(lower_dev)) { |
| 3129 | *port = netdev_priv(lower_dev); |
| 3130 | ret = 1; |
| 3131 | } |
| 3132 | |
| 3133 | return ret; |
| 3134 | } |
| 3135 | |
Jiri Pirko | 7ce856a | 2016-07-04 08:23:12 +0200 | [diff] [blame] | 3136 | static struct mlxsw_sp_port *mlxsw_sp_port_dev_lower_find(struct net_device *dev) |
| 3137 | { |
David Ahern | dd82364 | 2016-10-17 19:15:49 -0700 | [diff] [blame] | 3138 | struct mlxsw_sp_port *port; |
Jiri Pirko | 7ce856a | 2016-07-04 08:23:12 +0200 | [diff] [blame] | 3139 | |
| 3140 | if (mlxsw_sp_port_dev_check(dev)) |
| 3141 | return netdev_priv(dev); |
| 3142 | |
David Ahern | dd82364 | 2016-10-17 19:15:49 -0700 | [diff] [blame] | 3143 | port = NULL; |
| 3144 | netdev_walk_all_lower_dev(dev, mlxsw_lower_dev_walk, &port); |
| 3145 | |
| 3146 | return port; |
Jiri Pirko | 7ce856a | 2016-07-04 08:23:12 +0200 | [diff] [blame] | 3147 | } |
| 3148 | |
| 3149 | static struct mlxsw_sp *mlxsw_sp_lower_get(struct net_device *dev) |
| 3150 | { |
| 3151 | struct mlxsw_sp_port *mlxsw_sp_port; |
| 3152 | |
| 3153 | mlxsw_sp_port = mlxsw_sp_port_dev_lower_find(dev); |
| 3154 | return mlxsw_sp_port ? mlxsw_sp_port->mlxsw_sp : NULL; |
| 3155 | } |
| 3156 | |
| 3157 | static struct mlxsw_sp_port *mlxsw_sp_port_dev_lower_find_rcu(struct net_device *dev) |
| 3158 | { |
David Ahern | dd82364 | 2016-10-17 19:15:49 -0700 | [diff] [blame] | 3159 | struct mlxsw_sp_port *port; |
Jiri Pirko | 7ce856a | 2016-07-04 08:23:12 +0200 | [diff] [blame] | 3160 | |
| 3161 | if (mlxsw_sp_port_dev_check(dev)) |
| 3162 | return netdev_priv(dev); |
| 3163 | |
David Ahern | dd82364 | 2016-10-17 19:15:49 -0700 | [diff] [blame] | 3164 | port = NULL; |
| 3165 | netdev_walk_all_lower_dev_rcu(dev, mlxsw_lower_dev_walk, &port); |
| 3166 | |
| 3167 | return port; |
Jiri Pirko | 7ce856a | 2016-07-04 08:23:12 +0200 | [diff] [blame] | 3168 | } |
| 3169 | |
| 3170 | struct mlxsw_sp_port *mlxsw_sp_port_lower_dev_hold(struct net_device *dev) |
| 3171 | { |
| 3172 | struct mlxsw_sp_port *mlxsw_sp_port; |
| 3173 | |
| 3174 | rcu_read_lock(); |
| 3175 | mlxsw_sp_port = mlxsw_sp_port_dev_lower_find_rcu(dev); |
| 3176 | if (mlxsw_sp_port) |
| 3177 | dev_hold(mlxsw_sp_port->dev); |
| 3178 | rcu_read_unlock(); |
| 3179 | return mlxsw_sp_port; |
| 3180 | } |
| 3181 | |
| 3182 | void mlxsw_sp_port_dev_put(struct mlxsw_sp_port *mlxsw_sp_port) |
| 3183 | { |
| 3184 | dev_put(mlxsw_sp_port->dev); |
| 3185 | } |
| 3186 | |
Ido Schimmel | 99724c1 | 2016-07-04 08:23:14 +0200 | [diff] [blame] | 3187 | static bool mlxsw_sp_rif_should_config(struct mlxsw_sp_rif *r, |
| 3188 | unsigned long event) |
| 3189 | { |
| 3190 | switch (event) { |
| 3191 | case NETDEV_UP: |
| 3192 | if (!r) |
| 3193 | return true; |
| 3194 | r->ref_count++; |
| 3195 | return false; |
| 3196 | case NETDEV_DOWN: |
| 3197 | if (r && --r->ref_count == 0) |
| 3198 | return true; |
| 3199 | /* It is possible we already removed the RIF ourselves |
| 3200 | * if it was assigned to a netdev that is now a bridge |
| 3201 | * or LAG slave. |
| 3202 | */ |
| 3203 | return false; |
| 3204 | } |
| 3205 | |
| 3206 | return false; |
| 3207 | } |
| 3208 | |
| 3209 | static int mlxsw_sp_avail_rif_get(struct mlxsw_sp *mlxsw_sp) |
| 3210 | { |
| 3211 | int i; |
| 3212 | |
Jiri Pirko | c1a3831 | 2016-10-21 16:07:23 +0200 | [diff] [blame] | 3213 | for (i = 0; i < MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_RIFS); i++) |
Ido Schimmel | 99724c1 | 2016-07-04 08:23:14 +0200 | [diff] [blame] | 3214 | if (!mlxsw_sp->rifs[i]) |
| 3215 | return i; |
| 3216 | |
Nogah Frankel | 8f8a62d | 2016-09-20 11:16:57 +0200 | [diff] [blame] | 3217 | return MLXSW_SP_INVALID_RIF; |
Ido Schimmel | 99724c1 | 2016-07-04 08:23:14 +0200 | [diff] [blame] | 3218 | } |
| 3219 | |
| 3220 | static void mlxsw_sp_vport_rif_sp_attr_get(struct mlxsw_sp_port *mlxsw_sp_vport, |
| 3221 | bool *p_lagged, u16 *p_system_port) |
| 3222 | { |
| 3223 | u8 local_port = mlxsw_sp_vport->local_port; |
| 3224 | |
| 3225 | *p_lagged = mlxsw_sp_vport->lagged; |
| 3226 | *p_system_port = *p_lagged ? mlxsw_sp_vport->lag_id : local_port; |
| 3227 | } |
| 3228 | |
| 3229 | static int mlxsw_sp_vport_rif_sp_op(struct mlxsw_sp_port *mlxsw_sp_vport, |
| 3230 | struct net_device *l3_dev, u16 rif, |
| 3231 | bool create) |
| 3232 | { |
| 3233 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_vport->mlxsw_sp; |
| 3234 | bool lagged = mlxsw_sp_vport->lagged; |
| 3235 | char ritr_pl[MLXSW_REG_RITR_LEN]; |
| 3236 | u16 system_port; |
| 3237 | |
| 3238 | mlxsw_reg_ritr_pack(ritr_pl, create, MLXSW_REG_RITR_SP_IF, rif, |
| 3239 | l3_dev->mtu, l3_dev->dev_addr); |
| 3240 | |
| 3241 | mlxsw_sp_vport_rif_sp_attr_get(mlxsw_sp_vport, &lagged, &system_port); |
| 3242 | mlxsw_reg_ritr_sp_if_pack(ritr_pl, lagged, system_port, |
| 3243 | mlxsw_sp_vport_vid_get(mlxsw_sp_vport)); |
| 3244 | |
| 3245 | return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl); |
| 3246 | } |
| 3247 | |
| 3248 | static void mlxsw_sp_vport_rif_sp_leave(struct mlxsw_sp_port *mlxsw_sp_vport); |
| 3249 | |
| 3250 | static struct mlxsw_sp_fid * |
| 3251 | mlxsw_sp_rfid_alloc(u16 fid, struct net_device *l3_dev) |
| 3252 | { |
| 3253 | struct mlxsw_sp_fid *f; |
| 3254 | |
| 3255 | f = kzalloc(sizeof(*f), GFP_KERNEL); |
| 3256 | if (!f) |
| 3257 | return NULL; |
| 3258 | |
| 3259 | f->leave = mlxsw_sp_vport_rif_sp_leave; |
| 3260 | f->ref_count = 0; |
| 3261 | f->dev = l3_dev; |
| 3262 | f->fid = fid; |
| 3263 | |
| 3264 | return f; |
| 3265 | } |
| 3266 | |
| 3267 | static struct mlxsw_sp_rif * |
| 3268 | mlxsw_sp_rif_alloc(u16 rif, struct net_device *l3_dev, struct mlxsw_sp_fid *f) |
| 3269 | { |
| 3270 | struct mlxsw_sp_rif *r; |
| 3271 | |
| 3272 | r = kzalloc(sizeof(*r), GFP_KERNEL); |
| 3273 | if (!r) |
| 3274 | return NULL; |
| 3275 | |
| 3276 | ether_addr_copy(r->addr, l3_dev->dev_addr); |
| 3277 | r->mtu = l3_dev->mtu; |
| 3278 | r->ref_count = 1; |
| 3279 | r->dev = l3_dev; |
| 3280 | r->rif = rif; |
| 3281 | r->f = f; |
| 3282 | |
| 3283 | return r; |
| 3284 | } |
| 3285 | |
| 3286 | static struct mlxsw_sp_rif * |
| 3287 | mlxsw_sp_vport_rif_sp_create(struct mlxsw_sp_port *mlxsw_sp_vport, |
| 3288 | struct net_device *l3_dev) |
| 3289 | { |
| 3290 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_vport->mlxsw_sp; |
| 3291 | struct mlxsw_sp_fid *f; |
| 3292 | struct mlxsw_sp_rif *r; |
| 3293 | u16 fid, rif; |
| 3294 | int err; |
| 3295 | |
| 3296 | rif = mlxsw_sp_avail_rif_get(mlxsw_sp); |
Nogah Frankel | 8f8a62d | 2016-09-20 11:16:57 +0200 | [diff] [blame] | 3297 | if (rif == MLXSW_SP_INVALID_RIF) |
Ido Schimmel | 99724c1 | 2016-07-04 08:23:14 +0200 | [diff] [blame] | 3298 | return ERR_PTR(-ERANGE); |
| 3299 | |
| 3300 | err = mlxsw_sp_vport_rif_sp_op(mlxsw_sp_vport, l3_dev, rif, true); |
| 3301 | if (err) |
| 3302 | return ERR_PTR(err); |
| 3303 | |
| 3304 | fid = mlxsw_sp_rif_sp_to_fid(rif); |
| 3305 | err = mlxsw_sp_rif_fdb_op(mlxsw_sp, l3_dev->dev_addr, fid, true); |
| 3306 | if (err) |
| 3307 | goto err_rif_fdb_op; |
| 3308 | |
| 3309 | f = mlxsw_sp_rfid_alloc(fid, l3_dev); |
| 3310 | if (!f) { |
| 3311 | err = -ENOMEM; |
| 3312 | goto err_rfid_alloc; |
| 3313 | } |
| 3314 | |
| 3315 | r = mlxsw_sp_rif_alloc(rif, l3_dev, f); |
| 3316 | if (!r) { |
| 3317 | err = -ENOMEM; |
| 3318 | goto err_rif_alloc; |
| 3319 | } |
| 3320 | |
| 3321 | f->r = r; |
| 3322 | mlxsw_sp->rifs[rif] = r; |
| 3323 | |
| 3324 | return r; |
| 3325 | |
| 3326 | err_rif_alloc: |
| 3327 | kfree(f); |
| 3328 | err_rfid_alloc: |
| 3329 | mlxsw_sp_rif_fdb_op(mlxsw_sp, l3_dev->dev_addr, fid, false); |
| 3330 | err_rif_fdb_op: |
| 3331 | mlxsw_sp_vport_rif_sp_op(mlxsw_sp_vport, l3_dev, rif, false); |
| 3332 | return ERR_PTR(err); |
| 3333 | } |
| 3334 | |
| 3335 | static void mlxsw_sp_vport_rif_sp_destroy(struct mlxsw_sp_port *mlxsw_sp_vport, |
| 3336 | struct mlxsw_sp_rif *r) |
| 3337 | { |
| 3338 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_vport->mlxsw_sp; |
| 3339 | struct net_device *l3_dev = r->dev; |
| 3340 | struct mlxsw_sp_fid *f = r->f; |
| 3341 | u16 fid = f->fid; |
| 3342 | u16 rif = r->rif; |
| 3343 | |
| 3344 | mlxsw_sp->rifs[rif] = NULL; |
| 3345 | f->r = NULL; |
| 3346 | |
| 3347 | kfree(r); |
| 3348 | |
| 3349 | kfree(f); |
| 3350 | |
| 3351 | mlxsw_sp_rif_fdb_op(mlxsw_sp, l3_dev->dev_addr, fid, false); |
| 3352 | |
| 3353 | mlxsw_sp_vport_rif_sp_op(mlxsw_sp_vport, l3_dev, rif, false); |
| 3354 | } |
| 3355 | |
| 3356 | static int mlxsw_sp_vport_rif_sp_join(struct mlxsw_sp_port *mlxsw_sp_vport, |
| 3357 | struct net_device *l3_dev) |
| 3358 | { |
| 3359 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_vport->mlxsw_sp; |
| 3360 | struct mlxsw_sp_rif *r; |
| 3361 | |
| 3362 | r = mlxsw_sp_rif_find_by_dev(mlxsw_sp, l3_dev); |
| 3363 | if (!r) { |
| 3364 | r = mlxsw_sp_vport_rif_sp_create(mlxsw_sp_vport, l3_dev); |
| 3365 | if (IS_ERR(r)) |
| 3366 | return PTR_ERR(r); |
| 3367 | } |
| 3368 | |
| 3369 | mlxsw_sp_vport_fid_set(mlxsw_sp_vport, r->f); |
| 3370 | r->f->ref_count++; |
| 3371 | |
| 3372 | netdev_dbg(mlxsw_sp_vport->dev, "Joined FID=%d\n", r->f->fid); |
| 3373 | |
| 3374 | return 0; |
| 3375 | } |
| 3376 | |
| 3377 | static void mlxsw_sp_vport_rif_sp_leave(struct mlxsw_sp_port *mlxsw_sp_vport) |
| 3378 | { |
| 3379 | struct mlxsw_sp_fid *f = mlxsw_sp_vport_fid_get(mlxsw_sp_vport); |
| 3380 | |
| 3381 | netdev_dbg(mlxsw_sp_vport->dev, "Left FID=%d\n", f->fid); |
| 3382 | |
| 3383 | mlxsw_sp_vport_fid_set(mlxsw_sp_vport, NULL); |
| 3384 | if (--f->ref_count == 0) |
| 3385 | mlxsw_sp_vport_rif_sp_destroy(mlxsw_sp_vport, f->r); |
| 3386 | } |
| 3387 | |
| 3388 | static int mlxsw_sp_inetaddr_vport_event(struct net_device *l3_dev, |
| 3389 | struct net_device *port_dev, |
| 3390 | unsigned long event, u16 vid) |
| 3391 | { |
| 3392 | struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(port_dev); |
| 3393 | struct mlxsw_sp_port *mlxsw_sp_vport; |
| 3394 | |
| 3395 | mlxsw_sp_vport = mlxsw_sp_port_vport_find(mlxsw_sp_port, vid); |
| 3396 | if (WARN_ON(!mlxsw_sp_vport)) |
| 3397 | return -EINVAL; |
| 3398 | |
| 3399 | switch (event) { |
| 3400 | case NETDEV_UP: |
| 3401 | return mlxsw_sp_vport_rif_sp_join(mlxsw_sp_vport, l3_dev); |
| 3402 | case NETDEV_DOWN: |
| 3403 | mlxsw_sp_vport_rif_sp_leave(mlxsw_sp_vport); |
| 3404 | break; |
| 3405 | } |
| 3406 | |
| 3407 | return 0; |
| 3408 | } |
| 3409 | |
| 3410 | static int mlxsw_sp_inetaddr_port_event(struct net_device *port_dev, |
| 3411 | unsigned long event) |
| 3412 | { |
| 3413 | if (netif_is_bridge_port(port_dev) || netif_is_lag_port(port_dev)) |
| 3414 | return 0; |
| 3415 | |
| 3416 | return mlxsw_sp_inetaddr_vport_event(port_dev, port_dev, event, 1); |
| 3417 | } |
| 3418 | |
| 3419 | static int __mlxsw_sp_inetaddr_lag_event(struct net_device *l3_dev, |
| 3420 | struct net_device *lag_dev, |
| 3421 | unsigned long event, u16 vid) |
| 3422 | { |
| 3423 | struct net_device *port_dev; |
| 3424 | struct list_head *iter; |
| 3425 | int err; |
| 3426 | |
| 3427 | netdev_for_each_lower_dev(lag_dev, port_dev, iter) { |
| 3428 | if (mlxsw_sp_port_dev_check(port_dev)) { |
| 3429 | err = mlxsw_sp_inetaddr_vport_event(l3_dev, port_dev, |
| 3430 | event, vid); |
| 3431 | if (err) |
| 3432 | return err; |
| 3433 | } |
| 3434 | } |
| 3435 | |
| 3436 | return 0; |
| 3437 | } |
| 3438 | |
| 3439 | static int mlxsw_sp_inetaddr_lag_event(struct net_device *lag_dev, |
| 3440 | unsigned long event) |
| 3441 | { |
| 3442 | if (netif_is_bridge_port(lag_dev)) |
| 3443 | return 0; |
| 3444 | |
| 3445 | return __mlxsw_sp_inetaddr_lag_event(lag_dev, lag_dev, event, 1); |
| 3446 | } |
| 3447 | |
Ido Schimmel | 99f44bb | 2016-07-04 08:23:17 +0200 | [diff] [blame] | 3448 | static struct mlxsw_sp_fid *mlxsw_sp_bridge_fid_get(struct mlxsw_sp *mlxsw_sp, |
| 3449 | struct net_device *l3_dev) |
| 3450 | { |
| 3451 | u16 fid; |
| 3452 | |
| 3453 | if (is_vlan_dev(l3_dev)) |
| 3454 | fid = vlan_dev_vlan_id(l3_dev); |
| 3455 | else if (mlxsw_sp->master_bridge.dev == l3_dev) |
| 3456 | fid = 1; |
| 3457 | else |
| 3458 | return mlxsw_sp_vfid_find(mlxsw_sp, l3_dev); |
| 3459 | |
| 3460 | return mlxsw_sp_fid_find(mlxsw_sp, fid); |
| 3461 | } |
| 3462 | |
Ido Schimmel | f888f58 | 2016-08-24 11:18:51 +0200 | [diff] [blame] | 3463 | static enum mlxsw_flood_table_type mlxsw_sp_flood_table_type_get(u16 fid) |
| 3464 | { |
| 3465 | return mlxsw_sp_fid_is_vfid(fid) ? MLXSW_REG_SFGC_TABLE_TYPE_FID : |
| 3466 | MLXSW_REG_SFGC_TABLE_TYPE_FID_OFFEST; |
| 3467 | } |
| 3468 | |
| 3469 | static u16 mlxsw_sp_flood_table_index_get(u16 fid) |
| 3470 | { |
| 3471 | return mlxsw_sp_fid_is_vfid(fid) ? mlxsw_sp_fid_to_vfid(fid) : fid; |
| 3472 | } |
| 3473 | |
| 3474 | static int mlxsw_sp_router_port_flood_set(struct mlxsw_sp *mlxsw_sp, u16 fid, |
| 3475 | bool set) |
| 3476 | { |
| 3477 | enum mlxsw_flood_table_type table_type; |
| 3478 | char *sftr_pl; |
| 3479 | u16 index; |
| 3480 | int err; |
| 3481 | |
| 3482 | sftr_pl = kmalloc(MLXSW_REG_SFTR_LEN, GFP_KERNEL); |
| 3483 | if (!sftr_pl) |
| 3484 | return -ENOMEM; |
| 3485 | |
| 3486 | table_type = mlxsw_sp_flood_table_type_get(fid); |
| 3487 | index = mlxsw_sp_flood_table_index_get(fid); |
| 3488 | mlxsw_reg_sftr_pack(sftr_pl, MLXSW_SP_FLOOD_TABLE_BM, index, table_type, |
| 3489 | 1, MLXSW_PORT_ROUTER_PORT, set); |
| 3490 | err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sftr), sftr_pl); |
| 3491 | |
| 3492 | kfree(sftr_pl); |
| 3493 | return err; |
| 3494 | } |
| 3495 | |
Ido Schimmel | 99f44bb | 2016-07-04 08:23:17 +0200 | [diff] [blame] | 3496 | static enum mlxsw_reg_ritr_if_type mlxsw_sp_rif_type_get(u16 fid) |
| 3497 | { |
| 3498 | if (mlxsw_sp_fid_is_vfid(fid)) |
| 3499 | return MLXSW_REG_RITR_FID_IF; |
| 3500 | else |
| 3501 | return MLXSW_REG_RITR_VLAN_IF; |
| 3502 | } |
| 3503 | |
| 3504 | static int mlxsw_sp_rif_bridge_op(struct mlxsw_sp *mlxsw_sp, |
| 3505 | struct net_device *l3_dev, |
| 3506 | u16 fid, u16 rif, |
| 3507 | bool create) |
| 3508 | { |
| 3509 | enum mlxsw_reg_ritr_if_type rif_type; |
| 3510 | char ritr_pl[MLXSW_REG_RITR_LEN]; |
| 3511 | |
| 3512 | rif_type = mlxsw_sp_rif_type_get(fid); |
| 3513 | mlxsw_reg_ritr_pack(ritr_pl, create, rif_type, rif, l3_dev->mtu, |
| 3514 | l3_dev->dev_addr); |
| 3515 | mlxsw_reg_ritr_fid_set(ritr_pl, rif_type, fid); |
| 3516 | |
| 3517 | return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl); |
| 3518 | } |
| 3519 | |
| 3520 | static int mlxsw_sp_rif_bridge_create(struct mlxsw_sp *mlxsw_sp, |
| 3521 | struct net_device *l3_dev, |
| 3522 | struct mlxsw_sp_fid *f) |
| 3523 | { |
| 3524 | struct mlxsw_sp_rif *r; |
| 3525 | u16 rif; |
| 3526 | int err; |
| 3527 | |
| 3528 | rif = mlxsw_sp_avail_rif_get(mlxsw_sp); |
Nogah Frankel | 8f8a62d | 2016-09-20 11:16:57 +0200 | [diff] [blame] | 3529 | if (rif == MLXSW_SP_INVALID_RIF) |
Ido Schimmel | 99f44bb | 2016-07-04 08:23:17 +0200 | [diff] [blame] | 3530 | return -ERANGE; |
| 3531 | |
Ido Schimmel | f888f58 | 2016-08-24 11:18:51 +0200 | [diff] [blame] | 3532 | err = mlxsw_sp_router_port_flood_set(mlxsw_sp, f->fid, true); |
Ido Schimmel | 99f44bb | 2016-07-04 08:23:17 +0200 | [diff] [blame] | 3533 | if (err) |
| 3534 | return err; |
| 3535 | |
Ido Schimmel | f888f58 | 2016-08-24 11:18:51 +0200 | [diff] [blame] | 3536 | err = mlxsw_sp_rif_bridge_op(mlxsw_sp, l3_dev, f->fid, rif, true); |
| 3537 | if (err) |
| 3538 | goto err_rif_bridge_op; |
| 3539 | |
Ido Schimmel | 99f44bb | 2016-07-04 08:23:17 +0200 | [diff] [blame] | 3540 | err = mlxsw_sp_rif_fdb_op(mlxsw_sp, l3_dev->dev_addr, f->fid, true); |
| 3541 | if (err) |
| 3542 | goto err_rif_fdb_op; |
| 3543 | |
| 3544 | r = mlxsw_sp_rif_alloc(rif, l3_dev, f); |
| 3545 | if (!r) { |
| 3546 | err = -ENOMEM; |
| 3547 | goto err_rif_alloc; |
| 3548 | } |
| 3549 | |
| 3550 | f->r = r; |
| 3551 | mlxsw_sp->rifs[rif] = r; |
| 3552 | |
| 3553 | netdev_dbg(l3_dev, "RIF=%d created\n", rif); |
| 3554 | |
| 3555 | return 0; |
| 3556 | |
| 3557 | err_rif_alloc: |
| 3558 | mlxsw_sp_rif_fdb_op(mlxsw_sp, l3_dev->dev_addr, f->fid, false); |
| 3559 | err_rif_fdb_op: |
| 3560 | mlxsw_sp_rif_bridge_op(mlxsw_sp, l3_dev, f->fid, rif, false); |
Ido Schimmel | f888f58 | 2016-08-24 11:18:51 +0200 | [diff] [blame] | 3561 | err_rif_bridge_op: |
| 3562 | mlxsw_sp_router_port_flood_set(mlxsw_sp, f->fid, false); |
Ido Schimmel | 99f44bb | 2016-07-04 08:23:17 +0200 | [diff] [blame] | 3563 | return err; |
| 3564 | } |
| 3565 | |
| 3566 | void mlxsw_sp_rif_bridge_destroy(struct mlxsw_sp *mlxsw_sp, |
| 3567 | struct mlxsw_sp_rif *r) |
| 3568 | { |
| 3569 | struct net_device *l3_dev = r->dev; |
| 3570 | struct mlxsw_sp_fid *f = r->f; |
| 3571 | u16 rif = r->rif; |
| 3572 | |
| 3573 | mlxsw_sp->rifs[rif] = NULL; |
| 3574 | f->r = NULL; |
| 3575 | |
| 3576 | kfree(r); |
| 3577 | |
| 3578 | mlxsw_sp_rif_fdb_op(mlxsw_sp, l3_dev->dev_addr, f->fid, false); |
| 3579 | |
| 3580 | mlxsw_sp_rif_bridge_op(mlxsw_sp, l3_dev, f->fid, rif, false); |
| 3581 | |
Ido Schimmel | f888f58 | 2016-08-24 11:18:51 +0200 | [diff] [blame] | 3582 | mlxsw_sp_router_port_flood_set(mlxsw_sp, f->fid, false); |
| 3583 | |
Ido Schimmel | 99f44bb | 2016-07-04 08:23:17 +0200 | [diff] [blame] | 3584 | netdev_dbg(l3_dev, "RIF=%d destroyed\n", rif); |
| 3585 | } |
| 3586 | |
| 3587 | static int mlxsw_sp_inetaddr_bridge_event(struct net_device *l3_dev, |
| 3588 | struct net_device *br_dev, |
| 3589 | unsigned long event) |
| 3590 | { |
| 3591 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(l3_dev); |
| 3592 | struct mlxsw_sp_fid *f; |
| 3593 | |
| 3594 | /* FID can either be an actual FID if the L3 device is the |
| 3595 | * VLAN-aware bridge or a VLAN device on top. Otherwise, the |
| 3596 | * L3 device is a VLAN-unaware bridge and we get a vFID. |
| 3597 | */ |
| 3598 | f = mlxsw_sp_bridge_fid_get(mlxsw_sp, l3_dev); |
| 3599 | if (WARN_ON(!f)) |
| 3600 | return -EINVAL; |
| 3601 | |
| 3602 | switch (event) { |
| 3603 | case NETDEV_UP: |
| 3604 | return mlxsw_sp_rif_bridge_create(mlxsw_sp, l3_dev, f); |
| 3605 | case NETDEV_DOWN: |
| 3606 | mlxsw_sp_rif_bridge_destroy(mlxsw_sp, f->r); |
| 3607 | break; |
| 3608 | } |
| 3609 | |
| 3610 | return 0; |
| 3611 | } |
| 3612 | |
Ido Schimmel | 99724c1 | 2016-07-04 08:23:14 +0200 | [diff] [blame] | 3613 | static int mlxsw_sp_inetaddr_vlan_event(struct net_device *vlan_dev, |
| 3614 | unsigned long event) |
| 3615 | { |
| 3616 | struct net_device *real_dev = vlan_dev_real_dev(vlan_dev); |
Ido Schimmel | 99f44bb | 2016-07-04 08:23:17 +0200 | [diff] [blame] | 3617 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(vlan_dev); |
Ido Schimmel | 99724c1 | 2016-07-04 08:23:14 +0200 | [diff] [blame] | 3618 | u16 vid = vlan_dev_vlan_id(vlan_dev); |
| 3619 | |
| 3620 | if (mlxsw_sp_port_dev_check(real_dev)) |
| 3621 | return mlxsw_sp_inetaddr_vport_event(vlan_dev, real_dev, event, |
| 3622 | vid); |
| 3623 | else if (netif_is_lag_master(real_dev)) |
| 3624 | return __mlxsw_sp_inetaddr_lag_event(vlan_dev, real_dev, event, |
| 3625 | vid); |
Ido Schimmel | 99f44bb | 2016-07-04 08:23:17 +0200 | [diff] [blame] | 3626 | else if (netif_is_bridge_master(real_dev) && |
| 3627 | mlxsw_sp->master_bridge.dev == real_dev) |
| 3628 | return mlxsw_sp_inetaddr_bridge_event(vlan_dev, real_dev, |
| 3629 | event); |
Ido Schimmel | 99724c1 | 2016-07-04 08:23:14 +0200 | [diff] [blame] | 3630 | |
| 3631 | return 0; |
| 3632 | } |
| 3633 | |
| 3634 | static int mlxsw_sp_inetaddr_event(struct notifier_block *unused, |
| 3635 | unsigned long event, void *ptr) |
| 3636 | { |
| 3637 | struct in_ifaddr *ifa = (struct in_ifaddr *) ptr; |
| 3638 | struct net_device *dev = ifa->ifa_dev->dev; |
| 3639 | struct mlxsw_sp *mlxsw_sp; |
| 3640 | struct mlxsw_sp_rif *r; |
| 3641 | int err = 0; |
| 3642 | |
| 3643 | mlxsw_sp = mlxsw_sp_lower_get(dev); |
| 3644 | if (!mlxsw_sp) |
| 3645 | goto out; |
| 3646 | |
| 3647 | r = mlxsw_sp_rif_find_by_dev(mlxsw_sp, dev); |
| 3648 | if (!mlxsw_sp_rif_should_config(r, event)) |
| 3649 | goto out; |
| 3650 | |
| 3651 | if (mlxsw_sp_port_dev_check(dev)) |
| 3652 | err = mlxsw_sp_inetaddr_port_event(dev, event); |
| 3653 | else if (netif_is_lag_master(dev)) |
| 3654 | err = mlxsw_sp_inetaddr_lag_event(dev, event); |
Ido Schimmel | 99f44bb | 2016-07-04 08:23:17 +0200 | [diff] [blame] | 3655 | else if (netif_is_bridge_master(dev)) |
| 3656 | err = mlxsw_sp_inetaddr_bridge_event(dev, dev, event); |
Ido Schimmel | 99724c1 | 2016-07-04 08:23:14 +0200 | [diff] [blame] | 3657 | else if (is_vlan_dev(dev)) |
| 3658 | err = mlxsw_sp_inetaddr_vlan_event(dev, event); |
| 3659 | |
| 3660 | out: |
| 3661 | return notifier_from_errno(err); |
| 3662 | } |
| 3663 | |
Ido Schimmel | 6e095fd | 2016-07-04 08:23:13 +0200 | [diff] [blame] | 3664 | static int mlxsw_sp_rif_edit(struct mlxsw_sp *mlxsw_sp, u16 rif, |
| 3665 | const char *mac, int mtu) |
| 3666 | { |
| 3667 | char ritr_pl[MLXSW_REG_RITR_LEN]; |
| 3668 | int err; |
| 3669 | |
| 3670 | mlxsw_reg_ritr_rif_pack(ritr_pl, rif); |
| 3671 | err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl); |
| 3672 | if (err) |
| 3673 | return err; |
| 3674 | |
| 3675 | mlxsw_reg_ritr_mtu_set(ritr_pl, mtu); |
| 3676 | mlxsw_reg_ritr_if_mac_memcpy_to(ritr_pl, mac); |
| 3677 | mlxsw_reg_ritr_op_set(ritr_pl, MLXSW_REG_RITR_RIF_CREATE); |
| 3678 | return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl); |
| 3679 | } |
| 3680 | |
| 3681 | static int mlxsw_sp_netdevice_router_port_event(struct net_device *dev) |
| 3682 | { |
| 3683 | struct mlxsw_sp *mlxsw_sp; |
| 3684 | struct mlxsw_sp_rif *r; |
| 3685 | int err; |
| 3686 | |
| 3687 | mlxsw_sp = mlxsw_sp_lower_get(dev); |
| 3688 | if (!mlxsw_sp) |
| 3689 | return 0; |
| 3690 | |
| 3691 | r = mlxsw_sp_rif_find_by_dev(mlxsw_sp, dev); |
| 3692 | if (!r) |
| 3693 | return 0; |
| 3694 | |
| 3695 | err = mlxsw_sp_rif_fdb_op(mlxsw_sp, r->addr, r->f->fid, false); |
| 3696 | if (err) |
| 3697 | return err; |
| 3698 | |
| 3699 | err = mlxsw_sp_rif_edit(mlxsw_sp, r->rif, dev->dev_addr, dev->mtu); |
| 3700 | if (err) |
| 3701 | goto err_rif_edit; |
| 3702 | |
| 3703 | err = mlxsw_sp_rif_fdb_op(mlxsw_sp, dev->dev_addr, r->f->fid, true); |
| 3704 | if (err) |
| 3705 | goto err_rif_fdb_op; |
| 3706 | |
| 3707 | ether_addr_copy(r->addr, dev->dev_addr); |
| 3708 | r->mtu = dev->mtu; |
| 3709 | |
| 3710 | netdev_dbg(dev, "Updated RIF=%d\n", r->rif); |
| 3711 | |
| 3712 | return 0; |
| 3713 | |
| 3714 | err_rif_fdb_op: |
| 3715 | mlxsw_sp_rif_edit(mlxsw_sp, r->rif, r->addr, r->mtu); |
| 3716 | err_rif_edit: |
| 3717 | mlxsw_sp_rif_fdb_op(mlxsw_sp, r->addr, r->f->fid, true); |
| 3718 | return err; |
| 3719 | } |
| 3720 | |
Ido Schimmel | fe3f6d1 | 2016-06-20 23:04:19 +0200 | [diff] [blame] | 3721 | static bool mlxsw_sp_lag_port_fid_member(struct mlxsw_sp_port *lag_port, |
| 3722 | u16 fid) |
| 3723 | { |
| 3724 | if (mlxsw_sp_fid_is_vfid(fid)) |
| 3725 | return mlxsw_sp_port_vport_find_by_fid(lag_port, fid); |
| 3726 | else |
| 3727 | return test_bit(fid, lag_port->active_vlans); |
| 3728 | } |
| 3729 | |
| 3730 | static bool mlxsw_sp_port_fdb_should_flush(struct mlxsw_sp_port *mlxsw_sp_port, |
| 3731 | u16 fid) |
Ido Schimmel | 039c49a | 2016-01-27 15:20:18 +0100 | [diff] [blame] | 3732 | { |
| 3733 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
Ido Schimmel | fe3f6d1 | 2016-06-20 23:04:19 +0200 | [diff] [blame] | 3734 | u8 local_port = mlxsw_sp_port->local_port; |
| 3735 | u16 lag_id = mlxsw_sp_port->lag_id; |
Jiri Pirko | c1a3831 | 2016-10-21 16:07:23 +0200 | [diff] [blame] | 3736 | u64 max_lag_members; |
Ido Schimmel | fe3f6d1 | 2016-06-20 23:04:19 +0200 | [diff] [blame] | 3737 | int i, count = 0; |
Ido Schimmel | 039c49a | 2016-01-27 15:20:18 +0100 | [diff] [blame] | 3738 | |
Ido Schimmel | fe3f6d1 | 2016-06-20 23:04:19 +0200 | [diff] [blame] | 3739 | if (!mlxsw_sp_port->lagged) |
| 3740 | return true; |
Ido Schimmel | 039c49a | 2016-01-27 15:20:18 +0100 | [diff] [blame] | 3741 | |
Jiri Pirko | c1a3831 | 2016-10-21 16:07:23 +0200 | [diff] [blame] | 3742 | max_lag_members = MLXSW_CORE_RES_GET(mlxsw_sp->core, |
| 3743 | MAX_LAG_MEMBERS); |
| 3744 | for (i = 0; i < max_lag_members; i++) { |
Ido Schimmel | fe3f6d1 | 2016-06-20 23:04:19 +0200 | [diff] [blame] | 3745 | struct mlxsw_sp_port *lag_port; |
| 3746 | |
| 3747 | lag_port = mlxsw_sp_port_lagged_get(mlxsw_sp, lag_id, i); |
| 3748 | if (!lag_port || lag_port->local_port == local_port) |
| 3749 | continue; |
| 3750 | if (mlxsw_sp_lag_port_fid_member(lag_port, fid)) |
| 3751 | count++; |
| 3752 | } |
| 3753 | |
| 3754 | return !count; |
Ido Schimmel | 039c49a | 2016-01-27 15:20:18 +0100 | [diff] [blame] | 3755 | } |
| 3756 | |
| 3757 | static int |
| 3758 | mlxsw_sp_port_fdb_flush_by_port_fid(const struct mlxsw_sp_port *mlxsw_sp_port, |
| 3759 | u16 fid) |
| 3760 | { |
| 3761 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
| 3762 | char sfdf_pl[MLXSW_REG_SFDF_LEN]; |
| 3763 | |
| 3764 | mlxsw_reg_sfdf_pack(sfdf_pl, MLXSW_REG_SFDF_FLUSH_PER_PORT_AND_FID); |
| 3765 | mlxsw_reg_sfdf_fid_set(sfdf_pl, fid); |
| 3766 | mlxsw_reg_sfdf_port_fid_system_port_set(sfdf_pl, |
| 3767 | mlxsw_sp_port->local_port); |
| 3768 | |
Ido Schimmel | 2230537 | 2016-06-20 23:04:21 +0200 | [diff] [blame] | 3769 | netdev_dbg(mlxsw_sp_port->dev, "FDB flushed using Port=%d, FID=%d\n", |
| 3770 | mlxsw_sp_port->local_port, fid); |
| 3771 | |
Ido Schimmel | 039c49a | 2016-01-27 15:20:18 +0100 | [diff] [blame] | 3772 | return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfdf), sfdf_pl); |
| 3773 | } |
| 3774 | |
| 3775 | static int |
Ido Schimmel | 039c49a | 2016-01-27 15:20:18 +0100 | [diff] [blame] | 3776 | mlxsw_sp_port_fdb_flush_by_lag_id_fid(const struct mlxsw_sp_port *mlxsw_sp_port, |
| 3777 | u16 fid) |
| 3778 | { |
| 3779 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
| 3780 | char sfdf_pl[MLXSW_REG_SFDF_LEN]; |
| 3781 | |
| 3782 | mlxsw_reg_sfdf_pack(sfdf_pl, MLXSW_REG_SFDF_FLUSH_PER_LAG_AND_FID); |
| 3783 | mlxsw_reg_sfdf_fid_set(sfdf_pl, fid); |
| 3784 | mlxsw_reg_sfdf_lag_fid_lag_id_set(sfdf_pl, mlxsw_sp_port->lag_id); |
| 3785 | |
Ido Schimmel | 2230537 | 2016-06-20 23:04:21 +0200 | [diff] [blame] | 3786 | netdev_dbg(mlxsw_sp_port->dev, "FDB flushed using LAG ID=%d, FID=%d\n", |
| 3787 | mlxsw_sp_port->lag_id, fid); |
| 3788 | |
Ido Schimmel | 039c49a | 2016-01-27 15:20:18 +0100 | [diff] [blame] | 3789 | return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfdf), sfdf_pl); |
| 3790 | } |
| 3791 | |
Ido Schimmel | fe3f6d1 | 2016-06-20 23:04:19 +0200 | [diff] [blame] | 3792 | int mlxsw_sp_port_fdb_flush(struct mlxsw_sp_port *mlxsw_sp_port, u16 fid) |
Ido Schimmel | 039c49a | 2016-01-27 15:20:18 +0100 | [diff] [blame] | 3793 | { |
Ido Schimmel | fe3f6d1 | 2016-06-20 23:04:19 +0200 | [diff] [blame] | 3794 | if (!mlxsw_sp_port_fdb_should_flush(mlxsw_sp_port, fid)) |
| 3795 | return 0; |
Ido Schimmel | 039c49a | 2016-01-27 15:20:18 +0100 | [diff] [blame] | 3796 | |
Ido Schimmel | fe3f6d1 | 2016-06-20 23:04:19 +0200 | [diff] [blame] | 3797 | if (mlxsw_sp_port->lagged) |
| 3798 | return mlxsw_sp_port_fdb_flush_by_lag_id_fid(mlxsw_sp_port, |
Ido Schimmel | 039c49a | 2016-01-27 15:20:18 +0100 | [diff] [blame] | 3799 | fid); |
| 3800 | else |
Ido Schimmel | fe3f6d1 | 2016-06-20 23:04:19 +0200 | [diff] [blame] | 3801 | return mlxsw_sp_port_fdb_flush_by_port_fid(mlxsw_sp_port, fid); |
Ido Schimmel | 039c49a | 2016-01-27 15:20:18 +0100 | [diff] [blame] | 3802 | } |
| 3803 | |
Ido Schimmel | 701b186 | 2016-07-04 08:23:16 +0200 | [diff] [blame] | 3804 | static void mlxsw_sp_master_bridge_gone_sync(struct mlxsw_sp *mlxsw_sp) |
| 3805 | { |
| 3806 | struct mlxsw_sp_fid *f, *tmp; |
| 3807 | |
| 3808 | list_for_each_entry_safe(f, tmp, &mlxsw_sp->fids, list) |
| 3809 | if (--f->ref_count == 0) |
| 3810 | mlxsw_sp_fid_destroy(mlxsw_sp, f); |
| 3811 | else |
| 3812 | WARN_ON_ONCE(1); |
| 3813 | } |
| 3814 | |
Ido Schimmel | 7117a57 | 2016-06-20 23:04:06 +0200 | [diff] [blame] | 3815 | static bool mlxsw_sp_master_bridge_check(struct mlxsw_sp *mlxsw_sp, |
| 3816 | struct net_device *br_dev) |
| 3817 | { |
| 3818 | return !mlxsw_sp->master_bridge.dev || |
| 3819 | mlxsw_sp->master_bridge.dev == br_dev; |
| 3820 | } |
| 3821 | |
| 3822 | static void mlxsw_sp_master_bridge_inc(struct mlxsw_sp *mlxsw_sp, |
| 3823 | struct net_device *br_dev) |
| 3824 | { |
| 3825 | mlxsw_sp->master_bridge.dev = br_dev; |
| 3826 | mlxsw_sp->master_bridge.ref_count++; |
| 3827 | } |
| 3828 | |
| 3829 | static void mlxsw_sp_master_bridge_dec(struct mlxsw_sp *mlxsw_sp) |
| 3830 | { |
Ido Schimmel | 701b186 | 2016-07-04 08:23:16 +0200 | [diff] [blame] | 3831 | if (--mlxsw_sp->master_bridge.ref_count == 0) { |
Ido Schimmel | 7117a57 | 2016-06-20 23:04:06 +0200 | [diff] [blame] | 3832 | mlxsw_sp->master_bridge.dev = NULL; |
Ido Schimmel | 701b186 | 2016-07-04 08:23:16 +0200 | [diff] [blame] | 3833 | /* It's possible upper VLAN devices are still holding |
| 3834 | * references to underlying FIDs. Drop the reference |
| 3835 | * and release the resources if it was the last one. |
| 3836 | * If it wasn't, then something bad happened. |
| 3837 | */ |
| 3838 | mlxsw_sp_master_bridge_gone_sync(mlxsw_sp); |
| 3839 | } |
Ido Schimmel | 7117a57 | 2016-06-20 23:04:06 +0200 | [diff] [blame] | 3840 | } |
| 3841 | |
| 3842 | static int mlxsw_sp_port_bridge_join(struct mlxsw_sp_port *mlxsw_sp_port, |
| 3843 | struct net_device *br_dev) |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 3844 | { |
| 3845 | struct net_device *dev = mlxsw_sp_port->dev; |
| 3846 | int err; |
| 3847 | |
| 3848 | /* When port is not bridged untagged packets are tagged with |
| 3849 | * PVID=VID=1, thereby creating an implicit VLAN interface in |
| 3850 | * the device. Remove it and let bridge code take care of its |
| 3851 | * own VLANs. |
| 3852 | */ |
| 3853 | err = mlxsw_sp_port_kill_vid(dev, 0, 1); |
Ido Schimmel | 6c72a3d | 2016-01-04 10:42:26 +0100 | [diff] [blame] | 3854 | if (err) |
| 3855 | return err; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 3856 | |
Ido Schimmel | 7117a57 | 2016-06-20 23:04:06 +0200 | [diff] [blame] | 3857 | mlxsw_sp_master_bridge_inc(mlxsw_sp_port->mlxsw_sp, br_dev); |
| 3858 | |
Ido Schimmel | 6c72a3d | 2016-01-04 10:42:26 +0100 | [diff] [blame] | 3859 | mlxsw_sp_port->learning = 1; |
| 3860 | mlxsw_sp_port->learning_sync = 1; |
| 3861 | mlxsw_sp_port->uc_flood = 1; |
| 3862 | mlxsw_sp_port->bridged = 1; |
| 3863 | |
| 3864 | return 0; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 3865 | } |
| 3866 | |
Ido Schimmel | fe3f6d1 | 2016-06-20 23:04:19 +0200 | [diff] [blame] | 3867 | static void mlxsw_sp_port_bridge_leave(struct mlxsw_sp_port *mlxsw_sp_port) |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 3868 | { |
| 3869 | struct net_device *dev = mlxsw_sp_port->dev; |
Ido Schimmel | 5a8f452 | 2016-01-04 10:42:25 +0100 | [diff] [blame] | 3870 | |
Ido Schimmel | 28a01d2 | 2016-02-18 11:30:02 +0100 | [diff] [blame] | 3871 | mlxsw_sp_port_pvid_set(mlxsw_sp_port, 1); |
| 3872 | |
Ido Schimmel | 7117a57 | 2016-06-20 23:04:06 +0200 | [diff] [blame] | 3873 | mlxsw_sp_master_bridge_dec(mlxsw_sp_port->mlxsw_sp); |
| 3874 | |
Ido Schimmel | 6c72a3d | 2016-01-04 10:42:26 +0100 | [diff] [blame] | 3875 | mlxsw_sp_port->learning = 0; |
| 3876 | mlxsw_sp_port->learning_sync = 0; |
| 3877 | mlxsw_sp_port->uc_flood = 0; |
Ido Schimmel | 5a8f452 | 2016-01-04 10:42:25 +0100 | [diff] [blame] | 3878 | mlxsw_sp_port->bridged = 0; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 3879 | |
| 3880 | /* Add implicit VLAN interface in the device, so that untagged |
| 3881 | * packets will be classified to the default vFID. |
| 3882 | */ |
Ido Schimmel | 82e6db0 | 2016-06-20 23:04:04 +0200 | [diff] [blame] | 3883 | mlxsw_sp_port_add_vid(dev, 0, 1); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 3884 | } |
| 3885 | |
Jiri Pirko | 0d65fc1 | 2015-12-03 12:12:28 +0100 | [diff] [blame] | 3886 | 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] | 3887 | { |
Jiri Pirko | 0d65fc1 | 2015-12-03 12:12:28 +0100 | [diff] [blame] | 3888 | char sldr_pl[MLXSW_REG_SLDR_LEN]; |
| 3889 | |
| 3890 | mlxsw_reg_sldr_lag_create_pack(sldr_pl, lag_id); |
| 3891 | return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sldr), sldr_pl); |
| 3892 | } |
| 3893 | |
| 3894 | static int mlxsw_sp_lag_destroy(struct mlxsw_sp *mlxsw_sp, u16 lag_id) |
| 3895 | { |
| 3896 | char sldr_pl[MLXSW_REG_SLDR_LEN]; |
| 3897 | |
| 3898 | mlxsw_reg_sldr_lag_destroy_pack(sldr_pl, lag_id); |
| 3899 | return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sldr), sldr_pl); |
| 3900 | } |
| 3901 | |
| 3902 | static int mlxsw_sp_lag_col_port_add(struct mlxsw_sp_port *mlxsw_sp_port, |
| 3903 | u16 lag_id, u8 port_index) |
| 3904 | { |
| 3905 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
| 3906 | char slcor_pl[MLXSW_REG_SLCOR_LEN]; |
| 3907 | |
| 3908 | mlxsw_reg_slcor_port_add_pack(slcor_pl, mlxsw_sp_port->local_port, |
| 3909 | lag_id, port_index); |
| 3910 | return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(slcor), slcor_pl); |
| 3911 | } |
| 3912 | |
| 3913 | static int mlxsw_sp_lag_col_port_remove(struct mlxsw_sp_port *mlxsw_sp_port, |
| 3914 | u16 lag_id) |
| 3915 | { |
| 3916 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
| 3917 | char slcor_pl[MLXSW_REG_SLCOR_LEN]; |
| 3918 | |
| 3919 | mlxsw_reg_slcor_port_remove_pack(slcor_pl, mlxsw_sp_port->local_port, |
| 3920 | lag_id); |
| 3921 | return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(slcor), slcor_pl); |
| 3922 | } |
| 3923 | |
| 3924 | static int mlxsw_sp_lag_col_port_enable(struct mlxsw_sp_port *mlxsw_sp_port, |
| 3925 | u16 lag_id) |
| 3926 | { |
| 3927 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
| 3928 | char slcor_pl[MLXSW_REG_SLCOR_LEN]; |
| 3929 | |
| 3930 | mlxsw_reg_slcor_col_enable_pack(slcor_pl, mlxsw_sp_port->local_port, |
| 3931 | lag_id); |
| 3932 | return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(slcor), slcor_pl); |
| 3933 | } |
| 3934 | |
| 3935 | static int mlxsw_sp_lag_col_port_disable(struct mlxsw_sp_port *mlxsw_sp_port, |
| 3936 | u16 lag_id) |
| 3937 | { |
| 3938 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
| 3939 | char slcor_pl[MLXSW_REG_SLCOR_LEN]; |
| 3940 | |
| 3941 | mlxsw_reg_slcor_col_disable_pack(slcor_pl, mlxsw_sp_port->local_port, |
| 3942 | lag_id); |
| 3943 | return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(slcor), slcor_pl); |
| 3944 | } |
| 3945 | |
| 3946 | static int mlxsw_sp_lag_index_get(struct mlxsw_sp *mlxsw_sp, |
| 3947 | struct net_device *lag_dev, |
| 3948 | u16 *p_lag_id) |
| 3949 | { |
| 3950 | struct mlxsw_sp_upper *lag; |
| 3951 | int free_lag_id = -1; |
Jiri Pirko | c1a3831 | 2016-10-21 16:07:23 +0200 | [diff] [blame] | 3952 | u64 max_lag; |
Jiri Pirko | 0d65fc1 | 2015-12-03 12:12:28 +0100 | [diff] [blame] | 3953 | int i; |
| 3954 | |
Jiri Pirko | c1a3831 | 2016-10-21 16:07:23 +0200 | [diff] [blame] | 3955 | max_lag = MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_LAG); |
| 3956 | for (i = 0; i < max_lag; i++) { |
Jiri Pirko | 0d65fc1 | 2015-12-03 12:12:28 +0100 | [diff] [blame] | 3957 | lag = mlxsw_sp_lag_get(mlxsw_sp, i); |
| 3958 | if (lag->ref_count) { |
| 3959 | if (lag->dev == lag_dev) { |
| 3960 | *p_lag_id = i; |
| 3961 | return 0; |
| 3962 | } |
| 3963 | } else if (free_lag_id < 0) { |
| 3964 | free_lag_id = i; |
| 3965 | } |
| 3966 | } |
| 3967 | if (free_lag_id < 0) |
| 3968 | return -EBUSY; |
| 3969 | *p_lag_id = free_lag_id; |
| 3970 | return 0; |
| 3971 | } |
| 3972 | |
| 3973 | static bool |
| 3974 | mlxsw_sp_master_lag_check(struct mlxsw_sp *mlxsw_sp, |
| 3975 | struct net_device *lag_dev, |
| 3976 | struct netdev_lag_upper_info *lag_upper_info) |
| 3977 | { |
| 3978 | u16 lag_id; |
| 3979 | |
| 3980 | if (mlxsw_sp_lag_index_get(mlxsw_sp, lag_dev, &lag_id) != 0) |
| 3981 | return false; |
| 3982 | if (lag_upper_info->tx_type != NETDEV_LAG_TX_TYPE_HASH) |
| 3983 | return false; |
| 3984 | return true; |
| 3985 | } |
| 3986 | |
| 3987 | static int mlxsw_sp_port_lag_index_get(struct mlxsw_sp *mlxsw_sp, |
| 3988 | u16 lag_id, u8 *p_port_index) |
| 3989 | { |
Jiri Pirko | c1a3831 | 2016-10-21 16:07:23 +0200 | [diff] [blame] | 3990 | u64 max_lag_members; |
Jiri Pirko | 0d65fc1 | 2015-12-03 12:12:28 +0100 | [diff] [blame] | 3991 | int i; |
| 3992 | |
Jiri Pirko | c1a3831 | 2016-10-21 16:07:23 +0200 | [diff] [blame] | 3993 | max_lag_members = MLXSW_CORE_RES_GET(mlxsw_sp->core, |
| 3994 | MAX_LAG_MEMBERS); |
| 3995 | for (i = 0; i < max_lag_members; i++) { |
Jiri Pirko | 0d65fc1 | 2015-12-03 12:12:28 +0100 | [diff] [blame] | 3996 | if (!mlxsw_sp_port_lagged_get(mlxsw_sp, lag_id, i)) { |
| 3997 | *p_port_index = i; |
| 3998 | return 0; |
| 3999 | } |
| 4000 | } |
| 4001 | return -EBUSY; |
| 4002 | } |
| 4003 | |
Ido Schimmel | 86bf95b | 2016-07-02 11:00:11 +0200 | [diff] [blame] | 4004 | static void |
| 4005 | mlxsw_sp_port_pvid_vport_lag_join(struct mlxsw_sp_port *mlxsw_sp_port, |
| 4006 | u16 lag_id) |
| 4007 | { |
| 4008 | struct mlxsw_sp_port *mlxsw_sp_vport; |
Ido Schimmel | 11943ff | 2016-07-02 11:00:12 +0200 | [diff] [blame] | 4009 | struct mlxsw_sp_fid *f; |
Ido Schimmel | 86bf95b | 2016-07-02 11:00:11 +0200 | [diff] [blame] | 4010 | |
| 4011 | mlxsw_sp_vport = mlxsw_sp_port_vport_find(mlxsw_sp_port, 1); |
| 4012 | if (WARN_ON(!mlxsw_sp_vport)) |
| 4013 | return; |
| 4014 | |
Ido Schimmel | 11943ff | 2016-07-02 11:00:12 +0200 | [diff] [blame] | 4015 | /* If vPort is assigned a RIF, then leave it since it's no |
| 4016 | * longer valid. |
| 4017 | */ |
| 4018 | f = mlxsw_sp_vport_fid_get(mlxsw_sp_vport); |
| 4019 | if (f) |
| 4020 | f->leave(mlxsw_sp_vport); |
| 4021 | |
Ido Schimmel | 86bf95b | 2016-07-02 11:00:11 +0200 | [diff] [blame] | 4022 | mlxsw_sp_vport->lag_id = lag_id; |
| 4023 | mlxsw_sp_vport->lagged = 1; |
| 4024 | } |
| 4025 | |
| 4026 | static void |
| 4027 | mlxsw_sp_port_pvid_vport_lag_leave(struct mlxsw_sp_port *mlxsw_sp_port) |
| 4028 | { |
| 4029 | struct mlxsw_sp_port *mlxsw_sp_vport; |
Ido Schimmel | 11943ff | 2016-07-02 11:00:12 +0200 | [diff] [blame] | 4030 | struct mlxsw_sp_fid *f; |
Ido Schimmel | 86bf95b | 2016-07-02 11:00:11 +0200 | [diff] [blame] | 4031 | |
| 4032 | mlxsw_sp_vport = mlxsw_sp_port_vport_find(mlxsw_sp_port, 1); |
| 4033 | if (WARN_ON(!mlxsw_sp_vport)) |
| 4034 | return; |
| 4035 | |
Ido Schimmel | 11943ff | 2016-07-02 11:00:12 +0200 | [diff] [blame] | 4036 | f = mlxsw_sp_vport_fid_get(mlxsw_sp_vport); |
| 4037 | if (f) |
| 4038 | f->leave(mlxsw_sp_vport); |
| 4039 | |
Ido Schimmel | 86bf95b | 2016-07-02 11:00:11 +0200 | [diff] [blame] | 4040 | mlxsw_sp_vport->lagged = 0; |
| 4041 | } |
| 4042 | |
Jiri Pirko | 0d65fc1 | 2015-12-03 12:12:28 +0100 | [diff] [blame] | 4043 | static int mlxsw_sp_port_lag_join(struct mlxsw_sp_port *mlxsw_sp_port, |
| 4044 | struct net_device *lag_dev) |
| 4045 | { |
| 4046 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
| 4047 | struct mlxsw_sp_upper *lag; |
| 4048 | u16 lag_id; |
| 4049 | u8 port_index; |
| 4050 | int err; |
| 4051 | |
| 4052 | err = mlxsw_sp_lag_index_get(mlxsw_sp, lag_dev, &lag_id); |
| 4053 | if (err) |
| 4054 | return err; |
| 4055 | lag = mlxsw_sp_lag_get(mlxsw_sp, lag_id); |
| 4056 | if (!lag->ref_count) { |
| 4057 | err = mlxsw_sp_lag_create(mlxsw_sp, lag_id); |
| 4058 | if (err) |
| 4059 | return err; |
| 4060 | lag->dev = lag_dev; |
| 4061 | } |
| 4062 | |
| 4063 | err = mlxsw_sp_port_lag_index_get(mlxsw_sp, lag_id, &port_index); |
| 4064 | if (err) |
| 4065 | return err; |
| 4066 | err = mlxsw_sp_lag_col_port_add(mlxsw_sp_port, lag_id, port_index); |
| 4067 | if (err) |
| 4068 | goto err_col_port_add; |
| 4069 | err = mlxsw_sp_lag_col_port_enable(mlxsw_sp_port, lag_id); |
| 4070 | if (err) |
| 4071 | goto err_col_port_enable; |
| 4072 | |
| 4073 | mlxsw_core_lag_mapping_set(mlxsw_sp->core, lag_id, port_index, |
| 4074 | mlxsw_sp_port->local_port); |
| 4075 | mlxsw_sp_port->lag_id = lag_id; |
| 4076 | mlxsw_sp_port->lagged = 1; |
| 4077 | lag->ref_count++; |
Ido Schimmel | 86bf95b | 2016-07-02 11:00:11 +0200 | [diff] [blame] | 4078 | |
| 4079 | mlxsw_sp_port_pvid_vport_lag_join(mlxsw_sp_port, lag_id); |
| 4080 | |
Jiri Pirko | 0d65fc1 | 2015-12-03 12:12:28 +0100 | [diff] [blame] | 4081 | return 0; |
| 4082 | |
Ido Schimmel | 51554db | 2016-05-06 22:18:39 +0200 | [diff] [blame] | 4083 | err_col_port_enable: |
| 4084 | mlxsw_sp_lag_col_port_remove(mlxsw_sp_port, lag_id); |
Jiri Pirko | 0d65fc1 | 2015-12-03 12:12:28 +0100 | [diff] [blame] | 4085 | err_col_port_add: |
| 4086 | if (!lag->ref_count) |
| 4087 | mlxsw_sp_lag_destroy(mlxsw_sp, lag_id); |
Jiri Pirko | 0d65fc1 | 2015-12-03 12:12:28 +0100 | [diff] [blame] | 4088 | return err; |
| 4089 | } |
| 4090 | |
Ido Schimmel | 82e6db0 | 2016-06-20 23:04:04 +0200 | [diff] [blame] | 4091 | static void mlxsw_sp_port_lag_leave(struct mlxsw_sp_port *mlxsw_sp_port, |
| 4092 | struct net_device *lag_dev) |
Jiri Pirko | 0d65fc1 | 2015-12-03 12:12:28 +0100 | [diff] [blame] | 4093 | { |
| 4094 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
Jiri Pirko | 0d65fc1 | 2015-12-03 12:12:28 +0100 | [diff] [blame] | 4095 | u16 lag_id = mlxsw_sp_port->lag_id; |
Ido Schimmel | 1c80075 | 2016-06-20 23:04:20 +0200 | [diff] [blame] | 4096 | struct mlxsw_sp_upper *lag; |
Jiri Pirko | 0d65fc1 | 2015-12-03 12:12:28 +0100 | [diff] [blame] | 4097 | |
| 4098 | if (!mlxsw_sp_port->lagged) |
Ido Schimmel | 82e6db0 | 2016-06-20 23:04:04 +0200 | [diff] [blame] | 4099 | return; |
Jiri Pirko | 0d65fc1 | 2015-12-03 12:12:28 +0100 | [diff] [blame] | 4100 | lag = mlxsw_sp_lag_get(mlxsw_sp, lag_id); |
| 4101 | WARN_ON(lag->ref_count == 0); |
| 4102 | |
Ido Schimmel | 82e6db0 | 2016-06-20 23:04:04 +0200 | [diff] [blame] | 4103 | mlxsw_sp_lag_col_port_disable(mlxsw_sp_port, lag_id); |
| 4104 | mlxsw_sp_lag_col_port_remove(mlxsw_sp_port, lag_id); |
Jiri Pirko | 0d65fc1 | 2015-12-03 12:12:28 +0100 | [diff] [blame] | 4105 | |
Ido Schimmel | 4dc236c | 2016-01-27 15:20:16 +0100 | [diff] [blame] | 4106 | if (mlxsw_sp_port->bridged) { |
| 4107 | mlxsw_sp_port_active_vlans_del(mlxsw_sp_port); |
Ido Schimmel | fe3f6d1 | 2016-06-20 23:04:19 +0200 | [diff] [blame] | 4108 | mlxsw_sp_port_bridge_leave(mlxsw_sp_port); |
Ido Schimmel | 4dc236c | 2016-01-27 15:20:16 +0100 | [diff] [blame] | 4109 | } |
| 4110 | |
Ido Schimmel | fe3f6d1 | 2016-06-20 23:04:19 +0200 | [diff] [blame] | 4111 | if (lag->ref_count == 1) |
Ido Schimmel | 82e6db0 | 2016-06-20 23:04:04 +0200 | [diff] [blame] | 4112 | mlxsw_sp_lag_destroy(mlxsw_sp, lag_id); |
Jiri Pirko | 0d65fc1 | 2015-12-03 12:12:28 +0100 | [diff] [blame] | 4113 | |
| 4114 | mlxsw_core_lag_mapping_clear(mlxsw_sp->core, lag_id, |
| 4115 | mlxsw_sp_port->local_port); |
| 4116 | mlxsw_sp_port->lagged = 0; |
| 4117 | lag->ref_count--; |
Ido Schimmel | 86bf95b | 2016-07-02 11:00:11 +0200 | [diff] [blame] | 4118 | |
| 4119 | mlxsw_sp_port_pvid_vport_lag_leave(mlxsw_sp_port); |
Jiri Pirko | 0d65fc1 | 2015-12-03 12:12:28 +0100 | [diff] [blame] | 4120 | } |
| 4121 | |
Jiri Pirko | 7458120 | 2015-12-03 12:12:30 +0100 | [diff] [blame] | 4122 | static int mlxsw_sp_lag_dist_port_add(struct mlxsw_sp_port *mlxsw_sp_port, |
| 4123 | u16 lag_id) |
| 4124 | { |
| 4125 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
| 4126 | char sldr_pl[MLXSW_REG_SLDR_LEN]; |
| 4127 | |
| 4128 | mlxsw_reg_sldr_lag_add_port_pack(sldr_pl, lag_id, |
| 4129 | mlxsw_sp_port->local_port); |
| 4130 | return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sldr), sldr_pl); |
| 4131 | } |
| 4132 | |
| 4133 | static int mlxsw_sp_lag_dist_port_remove(struct mlxsw_sp_port *mlxsw_sp_port, |
| 4134 | u16 lag_id) |
| 4135 | { |
| 4136 | struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
| 4137 | char sldr_pl[MLXSW_REG_SLDR_LEN]; |
| 4138 | |
| 4139 | mlxsw_reg_sldr_lag_remove_port_pack(sldr_pl, lag_id, |
| 4140 | mlxsw_sp_port->local_port); |
| 4141 | return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sldr), sldr_pl); |
| 4142 | } |
| 4143 | |
| 4144 | static int mlxsw_sp_port_lag_tx_en_set(struct mlxsw_sp_port *mlxsw_sp_port, |
| 4145 | bool lag_tx_enabled) |
| 4146 | { |
| 4147 | if (lag_tx_enabled) |
| 4148 | return mlxsw_sp_lag_dist_port_add(mlxsw_sp_port, |
| 4149 | mlxsw_sp_port->lag_id); |
| 4150 | else |
| 4151 | return mlxsw_sp_lag_dist_port_remove(mlxsw_sp_port, |
| 4152 | mlxsw_sp_port->lag_id); |
| 4153 | } |
| 4154 | |
| 4155 | static int mlxsw_sp_port_lag_changed(struct mlxsw_sp_port *mlxsw_sp_port, |
| 4156 | struct netdev_lag_lower_state_info *info) |
| 4157 | { |
| 4158 | return mlxsw_sp_port_lag_tx_en_set(mlxsw_sp_port, info->tx_enabled); |
| 4159 | } |
| 4160 | |
Ido Schimmel | 9589a7b5 | 2015-12-15 16:03:43 +0100 | [diff] [blame] | 4161 | static int mlxsw_sp_port_vlan_link(struct mlxsw_sp_port *mlxsw_sp_port, |
| 4162 | struct net_device *vlan_dev) |
| 4163 | { |
| 4164 | struct mlxsw_sp_port *mlxsw_sp_vport; |
| 4165 | u16 vid = vlan_dev_vlan_id(vlan_dev); |
| 4166 | |
| 4167 | mlxsw_sp_vport = mlxsw_sp_port_vport_find(mlxsw_sp_port, vid); |
Ido Schimmel | 423b937 | 2016-06-20 23:04:03 +0200 | [diff] [blame] | 4168 | if (WARN_ON(!mlxsw_sp_vport)) |
Ido Schimmel | 9589a7b5 | 2015-12-15 16:03:43 +0100 | [diff] [blame] | 4169 | return -EINVAL; |
Ido Schimmel | 9589a7b5 | 2015-12-15 16:03:43 +0100 | [diff] [blame] | 4170 | |
| 4171 | mlxsw_sp_vport->dev = vlan_dev; |
| 4172 | |
| 4173 | return 0; |
| 4174 | } |
| 4175 | |
Ido Schimmel | 82e6db0 | 2016-06-20 23:04:04 +0200 | [diff] [blame] | 4176 | static void mlxsw_sp_port_vlan_unlink(struct mlxsw_sp_port *mlxsw_sp_port, |
| 4177 | struct net_device *vlan_dev) |
Ido Schimmel | 9589a7b5 | 2015-12-15 16:03:43 +0100 | [diff] [blame] | 4178 | { |
| 4179 | struct mlxsw_sp_port *mlxsw_sp_vport; |
| 4180 | u16 vid = vlan_dev_vlan_id(vlan_dev); |
| 4181 | |
| 4182 | mlxsw_sp_vport = mlxsw_sp_port_vport_find(mlxsw_sp_port, vid); |
Ido Schimmel | 423b937 | 2016-06-20 23:04:03 +0200 | [diff] [blame] | 4183 | if (WARN_ON(!mlxsw_sp_vport)) |
Ido Schimmel | 82e6db0 | 2016-06-20 23:04:04 +0200 | [diff] [blame] | 4184 | return; |
Ido Schimmel | 9589a7b5 | 2015-12-15 16:03:43 +0100 | [diff] [blame] | 4185 | |
| 4186 | mlxsw_sp_vport->dev = mlxsw_sp_port->dev; |
Ido Schimmel | 9589a7b5 | 2015-12-15 16:03:43 +0100 | [diff] [blame] | 4187 | } |
| 4188 | |
Jiri Pirko | 7458120 | 2015-12-03 12:12:30 +0100 | [diff] [blame] | 4189 | static int mlxsw_sp_netdevice_port_upper_event(struct net_device *dev, |
| 4190 | unsigned long event, void *ptr) |
Jiri Pirko | 0d65fc1 | 2015-12-03 12:12:28 +0100 | [diff] [blame] | 4191 | { |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 4192 | struct netdev_notifier_changeupper_info *info; |
| 4193 | struct mlxsw_sp_port *mlxsw_sp_port; |
| 4194 | struct net_device *upper_dev; |
| 4195 | struct mlxsw_sp *mlxsw_sp; |
Ido Schimmel | 80bedf1 | 2016-06-20 23:03:59 +0200 | [diff] [blame] | 4196 | int err = 0; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 4197 | |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 4198 | mlxsw_sp_port = netdev_priv(dev); |
| 4199 | mlxsw_sp = mlxsw_sp_port->mlxsw_sp; |
| 4200 | info = ptr; |
| 4201 | |
| 4202 | switch (event) { |
| 4203 | case NETDEV_PRECHANGEUPPER: |
| 4204 | upper_dev = info->upper_dev; |
Ido Schimmel | 59fe9b3 | 2016-06-20 23:04:00 +0200 | [diff] [blame] | 4205 | if (!is_vlan_dev(upper_dev) && |
| 4206 | !netif_is_lag_master(upper_dev) && |
| 4207 | !netif_is_bridge_master(upper_dev)) |
| 4208 | return -EINVAL; |
Ido Schimmel | 6ec4390 | 2016-06-20 23:04:01 +0200 | [diff] [blame] | 4209 | if (!info->linking) |
Jiri Pirko | 0d65fc1 | 2015-12-03 12:12:28 +0100 | [diff] [blame] | 4210 | break; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 4211 | /* HW limitation forbids to put ports to multiple bridges. */ |
Jiri Pirko | 0d65fc1 | 2015-12-03 12:12:28 +0100 | [diff] [blame] | 4212 | if (netif_is_bridge_master(upper_dev) && |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 4213 | !mlxsw_sp_master_bridge_check(mlxsw_sp, upper_dev)) |
Ido Schimmel | 80bedf1 | 2016-06-20 23:03:59 +0200 | [diff] [blame] | 4214 | return -EINVAL; |
Jiri Pirko | 0d65fc1 | 2015-12-03 12:12:28 +0100 | [diff] [blame] | 4215 | if (netif_is_lag_master(upper_dev) && |
| 4216 | !mlxsw_sp_master_lag_check(mlxsw_sp, upper_dev, |
| 4217 | info->upper_info)) |
Ido Schimmel | 80bedf1 | 2016-06-20 23:03:59 +0200 | [diff] [blame] | 4218 | return -EINVAL; |
Ido Schimmel | 6ec4390 | 2016-06-20 23:04:01 +0200 | [diff] [blame] | 4219 | if (netif_is_lag_master(upper_dev) && vlan_uses_dev(dev)) |
| 4220 | return -EINVAL; |
| 4221 | if (netif_is_lag_port(dev) && is_vlan_dev(upper_dev) && |
| 4222 | !netif_is_lag_master(vlan_dev_real_dev(upper_dev))) |
| 4223 | return -EINVAL; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 4224 | break; |
| 4225 | case NETDEV_CHANGEUPPER: |
| 4226 | upper_dev = info->upper_dev; |
Ido Schimmel | 9589a7b5 | 2015-12-15 16:03:43 +0100 | [diff] [blame] | 4227 | if (is_vlan_dev(upper_dev)) { |
Ido Schimmel | 80bedf1 | 2016-06-20 23:03:59 +0200 | [diff] [blame] | 4228 | if (info->linking) |
Ido Schimmel | 9589a7b5 | 2015-12-15 16:03:43 +0100 | [diff] [blame] | 4229 | err = mlxsw_sp_port_vlan_link(mlxsw_sp_port, |
| 4230 | upper_dev); |
Ido Schimmel | 80bedf1 | 2016-06-20 23:03:59 +0200 | [diff] [blame] | 4231 | else |
Ido Schimmel | 82e6db0 | 2016-06-20 23:04:04 +0200 | [diff] [blame] | 4232 | mlxsw_sp_port_vlan_unlink(mlxsw_sp_port, |
| 4233 | upper_dev); |
Ido Schimmel | 9589a7b5 | 2015-12-15 16:03:43 +0100 | [diff] [blame] | 4234 | } else if (netif_is_bridge_master(upper_dev)) { |
Ido Schimmel | 7117a57 | 2016-06-20 23:04:06 +0200 | [diff] [blame] | 4235 | if (info->linking) |
| 4236 | err = mlxsw_sp_port_bridge_join(mlxsw_sp_port, |
| 4237 | upper_dev); |
| 4238 | else |
Ido Schimmel | fe3f6d1 | 2016-06-20 23:04:19 +0200 | [diff] [blame] | 4239 | mlxsw_sp_port_bridge_leave(mlxsw_sp_port); |
Jiri Pirko | 0d65fc1 | 2015-12-03 12:12:28 +0100 | [diff] [blame] | 4240 | } else if (netif_is_lag_master(upper_dev)) { |
Ido Schimmel | 80bedf1 | 2016-06-20 23:03:59 +0200 | [diff] [blame] | 4241 | if (info->linking) |
Jiri Pirko | 0d65fc1 | 2015-12-03 12:12:28 +0100 | [diff] [blame] | 4242 | err = mlxsw_sp_port_lag_join(mlxsw_sp_port, |
| 4243 | upper_dev); |
Ido Schimmel | 80bedf1 | 2016-06-20 23:03:59 +0200 | [diff] [blame] | 4244 | else |
Ido Schimmel | 82e6db0 | 2016-06-20 23:04:04 +0200 | [diff] [blame] | 4245 | mlxsw_sp_port_lag_leave(mlxsw_sp_port, |
| 4246 | upper_dev); |
Ido Schimmel | 59fe9b3 | 2016-06-20 23:04:00 +0200 | [diff] [blame] | 4247 | } else { |
| 4248 | err = -EINVAL; |
| 4249 | WARN_ON(1); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 4250 | } |
| 4251 | break; |
| 4252 | } |
| 4253 | |
Ido Schimmel | 80bedf1 | 2016-06-20 23:03:59 +0200 | [diff] [blame] | 4254 | return err; |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 4255 | } |
| 4256 | |
Jiri Pirko | 7458120 | 2015-12-03 12:12:30 +0100 | [diff] [blame] | 4257 | static int mlxsw_sp_netdevice_port_lower_event(struct net_device *dev, |
| 4258 | unsigned long event, void *ptr) |
| 4259 | { |
| 4260 | struct netdev_notifier_changelowerstate_info *info; |
| 4261 | struct mlxsw_sp_port *mlxsw_sp_port; |
| 4262 | int err; |
| 4263 | |
| 4264 | mlxsw_sp_port = netdev_priv(dev); |
| 4265 | info = ptr; |
| 4266 | |
| 4267 | switch (event) { |
| 4268 | case NETDEV_CHANGELOWERSTATE: |
| 4269 | if (netif_is_lag_port(dev) && mlxsw_sp_port->lagged) { |
| 4270 | err = mlxsw_sp_port_lag_changed(mlxsw_sp_port, |
| 4271 | info->lower_state_info); |
| 4272 | if (err) |
| 4273 | netdev_err(dev, "Failed to reflect link aggregation lower state change\n"); |
| 4274 | } |
| 4275 | break; |
| 4276 | } |
| 4277 | |
Ido Schimmel | 80bedf1 | 2016-06-20 23:03:59 +0200 | [diff] [blame] | 4278 | return 0; |
Jiri Pirko | 7458120 | 2015-12-03 12:12:30 +0100 | [diff] [blame] | 4279 | } |
| 4280 | |
| 4281 | static int mlxsw_sp_netdevice_port_event(struct net_device *dev, |
| 4282 | unsigned long event, void *ptr) |
| 4283 | { |
| 4284 | switch (event) { |
| 4285 | case NETDEV_PRECHANGEUPPER: |
| 4286 | case NETDEV_CHANGEUPPER: |
| 4287 | return mlxsw_sp_netdevice_port_upper_event(dev, event, ptr); |
| 4288 | case NETDEV_CHANGELOWERSTATE: |
| 4289 | return mlxsw_sp_netdevice_port_lower_event(dev, event, ptr); |
| 4290 | } |
| 4291 | |
Ido Schimmel | 80bedf1 | 2016-06-20 23:03:59 +0200 | [diff] [blame] | 4292 | return 0; |
Jiri Pirko | 7458120 | 2015-12-03 12:12:30 +0100 | [diff] [blame] | 4293 | } |
| 4294 | |
Jiri Pirko | 0d65fc1 | 2015-12-03 12:12:28 +0100 | [diff] [blame] | 4295 | static int mlxsw_sp_netdevice_lag_event(struct net_device *lag_dev, |
| 4296 | unsigned long event, void *ptr) |
| 4297 | { |
| 4298 | struct net_device *dev; |
| 4299 | struct list_head *iter; |
| 4300 | int ret; |
| 4301 | |
| 4302 | netdev_for_each_lower_dev(lag_dev, dev, iter) { |
| 4303 | if (mlxsw_sp_port_dev_check(dev)) { |
| 4304 | ret = mlxsw_sp_netdevice_port_event(dev, event, ptr); |
Ido Schimmel | 80bedf1 | 2016-06-20 23:03:59 +0200 | [diff] [blame] | 4305 | if (ret) |
Jiri Pirko | 0d65fc1 | 2015-12-03 12:12:28 +0100 | [diff] [blame] | 4306 | return ret; |
| 4307 | } |
| 4308 | } |
| 4309 | |
Ido Schimmel | 80bedf1 | 2016-06-20 23:03:59 +0200 | [diff] [blame] | 4310 | return 0; |
Jiri Pirko | 0d65fc1 | 2015-12-03 12:12:28 +0100 | [diff] [blame] | 4311 | } |
| 4312 | |
Ido Schimmel | 701b186 | 2016-07-04 08:23:16 +0200 | [diff] [blame] | 4313 | static int mlxsw_sp_master_bridge_vlan_link(struct mlxsw_sp *mlxsw_sp, |
| 4314 | struct net_device *vlan_dev) |
Ido Schimmel | 26f0e7f | 2015-12-15 16:03:44 +0100 | [diff] [blame] | 4315 | { |
Ido Schimmel | 701b186 | 2016-07-04 08:23:16 +0200 | [diff] [blame] | 4316 | u16 fid = vlan_dev_vlan_id(vlan_dev); |
Ido Schimmel | d0ec875 | 2016-06-20 23:04:12 +0200 | [diff] [blame] | 4317 | struct mlxsw_sp_fid *f; |
Ido Schimmel | 26f0e7f | 2015-12-15 16:03:44 +0100 | [diff] [blame] | 4318 | |
Ido Schimmel | 701b186 | 2016-07-04 08:23:16 +0200 | [diff] [blame] | 4319 | f = mlxsw_sp_fid_find(mlxsw_sp, fid); |
| 4320 | if (!f) { |
| 4321 | f = mlxsw_sp_fid_create(mlxsw_sp, fid); |
| 4322 | if (IS_ERR(f)) |
| 4323 | return PTR_ERR(f); |
Ido Schimmel | 26f0e7f | 2015-12-15 16:03:44 +0100 | [diff] [blame] | 4324 | } |
| 4325 | |
Ido Schimmel | 701b186 | 2016-07-04 08:23:16 +0200 | [diff] [blame] | 4326 | f->ref_count++; |
| 4327 | |
| 4328 | return 0; |
| 4329 | } |
| 4330 | |
| 4331 | static void mlxsw_sp_master_bridge_vlan_unlink(struct mlxsw_sp *mlxsw_sp, |
| 4332 | struct net_device *vlan_dev) |
| 4333 | { |
| 4334 | u16 fid = vlan_dev_vlan_id(vlan_dev); |
| 4335 | struct mlxsw_sp_fid *f; |
| 4336 | |
| 4337 | f = mlxsw_sp_fid_find(mlxsw_sp, fid); |
Ido Schimmel | 99f44bb | 2016-07-04 08:23:17 +0200 | [diff] [blame] | 4338 | if (f && f->r) |
| 4339 | mlxsw_sp_rif_bridge_destroy(mlxsw_sp, f->r); |
Ido Schimmel | 701b186 | 2016-07-04 08:23:16 +0200 | [diff] [blame] | 4340 | if (f && --f->ref_count == 0) |
| 4341 | mlxsw_sp_fid_destroy(mlxsw_sp, f); |
| 4342 | } |
| 4343 | |
| 4344 | static int mlxsw_sp_netdevice_bridge_event(struct net_device *br_dev, |
| 4345 | unsigned long event, void *ptr) |
| 4346 | { |
| 4347 | struct netdev_notifier_changeupper_info *info; |
| 4348 | struct net_device *upper_dev; |
| 4349 | struct mlxsw_sp *mlxsw_sp; |
| 4350 | int err; |
| 4351 | |
| 4352 | mlxsw_sp = mlxsw_sp_lower_get(br_dev); |
| 4353 | if (!mlxsw_sp) |
| 4354 | return 0; |
| 4355 | if (br_dev != mlxsw_sp->master_bridge.dev) |
| 4356 | return 0; |
| 4357 | |
| 4358 | info = ptr; |
| 4359 | |
| 4360 | switch (event) { |
| 4361 | case NETDEV_CHANGEUPPER: |
| 4362 | upper_dev = info->upper_dev; |
| 4363 | if (!is_vlan_dev(upper_dev)) |
| 4364 | break; |
| 4365 | if (info->linking) { |
| 4366 | err = mlxsw_sp_master_bridge_vlan_link(mlxsw_sp, |
| 4367 | upper_dev); |
| 4368 | if (err) |
| 4369 | return err; |
| 4370 | } else { |
| 4371 | mlxsw_sp_master_bridge_vlan_unlink(mlxsw_sp, upper_dev); |
| 4372 | } |
| 4373 | break; |
| 4374 | } |
| 4375 | |
| 4376 | return 0; |
Ido Schimmel | 26f0e7f | 2015-12-15 16:03:44 +0100 | [diff] [blame] | 4377 | } |
| 4378 | |
Ido Schimmel | 3ba2ebf | 2016-07-04 08:23:15 +0200 | [diff] [blame] | 4379 | static u16 mlxsw_sp_avail_vfid_get(const struct mlxsw_sp *mlxsw_sp) |
Ido Schimmel | 26f0e7f | 2015-12-15 16:03:44 +0100 | [diff] [blame] | 4380 | { |
Ido Schimmel | 3ba2ebf | 2016-07-04 08:23:15 +0200 | [diff] [blame] | 4381 | return find_first_zero_bit(mlxsw_sp->vfids.mapped, |
Ido Schimmel | 99724c1 | 2016-07-04 08:23:14 +0200 | [diff] [blame] | 4382 | MLXSW_SP_VFID_MAX); |
| 4383 | } |
| 4384 | |
| 4385 | static int mlxsw_sp_vfid_op(struct mlxsw_sp *mlxsw_sp, u16 fid, bool create) |
| 4386 | { |
| 4387 | char sfmr_pl[MLXSW_REG_SFMR_LEN]; |
| 4388 | |
| 4389 | mlxsw_reg_sfmr_pack(sfmr_pl, !create, fid, 0); |
| 4390 | return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfmr), sfmr_pl); |
Ido Schimmel | 26f0e7f | 2015-12-15 16:03:44 +0100 | [diff] [blame] | 4391 | } |
| 4392 | |
Ido Schimmel | 3ba2ebf | 2016-07-04 08:23:15 +0200 | [diff] [blame] | 4393 | static void mlxsw_sp_vport_vfid_leave(struct mlxsw_sp_port *mlxsw_sp_vport); |
Ido Schimmel | 1c80075 | 2016-06-20 23:04:20 +0200 | [diff] [blame] | 4394 | |
Ido Schimmel | 3ba2ebf | 2016-07-04 08:23:15 +0200 | [diff] [blame] | 4395 | static struct mlxsw_sp_fid *mlxsw_sp_vfid_create(struct mlxsw_sp *mlxsw_sp, |
| 4396 | struct net_device *br_dev) |
Ido Schimmel | 26f0e7f | 2015-12-15 16:03:44 +0100 | [diff] [blame] | 4397 | { |
| 4398 | struct device *dev = mlxsw_sp->bus_info->dev; |
Ido Schimmel | d0ec875 | 2016-06-20 23:04:12 +0200 | [diff] [blame] | 4399 | struct mlxsw_sp_fid *f; |
Ido Schimmel | c7e920b | 2016-06-20 23:04:09 +0200 | [diff] [blame] | 4400 | u16 vfid, fid; |
Ido Schimmel | 26f0e7f | 2015-12-15 16:03:44 +0100 | [diff] [blame] | 4401 | int err; |
| 4402 | |
Ido Schimmel | 3ba2ebf | 2016-07-04 08:23:15 +0200 | [diff] [blame] | 4403 | vfid = mlxsw_sp_avail_vfid_get(mlxsw_sp); |
Ido Schimmel | c7e920b | 2016-06-20 23:04:09 +0200 | [diff] [blame] | 4404 | if (vfid == MLXSW_SP_VFID_MAX) { |
Ido Schimmel | 26f0e7f | 2015-12-15 16:03:44 +0100 | [diff] [blame] | 4405 | dev_err(dev, "No available vFIDs\n"); |
| 4406 | return ERR_PTR(-ERANGE); |
| 4407 | } |
| 4408 | |
Ido Schimmel | c7e920b | 2016-06-20 23:04:09 +0200 | [diff] [blame] | 4409 | fid = mlxsw_sp_vfid_to_fid(vfid); |
| 4410 | err = mlxsw_sp_vfid_op(mlxsw_sp, fid, true); |
Ido Schimmel | 26f0e7f | 2015-12-15 16:03:44 +0100 | [diff] [blame] | 4411 | if (err) { |
Ido Schimmel | c7e920b | 2016-06-20 23:04:09 +0200 | [diff] [blame] | 4412 | dev_err(dev, "Failed to create FID=%d\n", fid); |
Ido Schimmel | 26f0e7f | 2015-12-15 16:03:44 +0100 | [diff] [blame] | 4413 | return ERR_PTR(err); |
| 4414 | } |
| 4415 | |
Ido Schimmel | c7e920b | 2016-06-20 23:04:09 +0200 | [diff] [blame] | 4416 | f = kzalloc(sizeof(*f), GFP_KERNEL); |
| 4417 | if (!f) |
Ido Schimmel | 26f0e7f | 2015-12-15 16:03:44 +0100 | [diff] [blame] | 4418 | goto err_allocate_vfid; |
| 4419 | |
Ido Schimmel | 3ba2ebf | 2016-07-04 08:23:15 +0200 | [diff] [blame] | 4420 | f->leave = mlxsw_sp_vport_vfid_leave; |
Ido Schimmel | d0ec875 | 2016-06-20 23:04:12 +0200 | [diff] [blame] | 4421 | f->fid = fid; |
| 4422 | f->dev = br_dev; |
Ido Schimmel | 26f0e7f | 2015-12-15 16:03:44 +0100 | [diff] [blame] | 4423 | |
Ido Schimmel | 3ba2ebf | 2016-07-04 08:23:15 +0200 | [diff] [blame] | 4424 | list_add(&f->list, &mlxsw_sp->vfids.list); |
| 4425 | set_bit(vfid, mlxsw_sp->vfids.mapped); |
Ido Schimmel | 26f0e7f | 2015-12-15 16:03:44 +0100 | [diff] [blame] | 4426 | |
Ido Schimmel | c7e920b | 2016-06-20 23:04:09 +0200 | [diff] [blame] | 4427 | return f; |
Ido Schimmel | 26f0e7f | 2015-12-15 16:03:44 +0100 | [diff] [blame] | 4428 | |
| 4429 | err_allocate_vfid: |
Ido Schimmel | c7e920b | 2016-06-20 23:04:09 +0200 | [diff] [blame] | 4430 | mlxsw_sp_vfid_op(mlxsw_sp, fid, false); |
Ido Schimmel | 26f0e7f | 2015-12-15 16:03:44 +0100 | [diff] [blame] | 4431 | return ERR_PTR(-ENOMEM); |
| 4432 | } |
| 4433 | |
Ido Schimmel | 3ba2ebf | 2016-07-04 08:23:15 +0200 | [diff] [blame] | 4434 | static void mlxsw_sp_vfid_destroy(struct mlxsw_sp *mlxsw_sp, |
| 4435 | struct mlxsw_sp_fid *f) |
Ido Schimmel | 26f0e7f | 2015-12-15 16:03:44 +0100 | [diff] [blame] | 4436 | { |
Ido Schimmel | d0ec875 | 2016-06-20 23:04:12 +0200 | [diff] [blame] | 4437 | u16 vfid = mlxsw_sp_fid_to_vfid(f->fid); |
Ido Schimmel | 99f44bb | 2016-07-04 08:23:17 +0200 | [diff] [blame] | 4438 | u16 fid = f->fid; |
Ido Schimmel | 26f0e7f | 2015-12-15 16:03:44 +0100 | [diff] [blame] | 4439 | |
Ido Schimmel | 3ba2ebf | 2016-07-04 08:23:15 +0200 | [diff] [blame] | 4440 | clear_bit(vfid, mlxsw_sp->vfids.mapped); |
Ido Schimmel | d0ec875 | 2016-06-20 23:04:12 +0200 | [diff] [blame] | 4441 | list_del(&f->list); |
Ido Schimmel | 26f0e7f | 2015-12-15 16:03:44 +0100 | [diff] [blame] | 4442 | |
Ido Schimmel | 99f44bb | 2016-07-04 08:23:17 +0200 | [diff] [blame] | 4443 | if (f->r) |
| 4444 | mlxsw_sp_rif_bridge_destroy(mlxsw_sp, f->r); |
Ido Schimmel | 26f0e7f | 2015-12-15 16:03:44 +0100 | [diff] [blame] | 4445 | |
Ido Schimmel | d0ec875 | 2016-06-20 23:04:12 +0200 | [diff] [blame] | 4446 | kfree(f); |
Ido Schimmel | 99f44bb | 2016-07-04 08:23:17 +0200 | [diff] [blame] | 4447 | |
| 4448 | mlxsw_sp_vfid_op(mlxsw_sp, fid, false); |
Ido Schimmel | 26f0e7f | 2015-12-15 16:03:44 +0100 | [diff] [blame] | 4449 | } |
| 4450 | |
Ido Schimmel | 99724c1 | 2016-07-04 08:23:14 +0200 | [diff] [blame] | 4451 | static int mlxsw_sp_vport_fid_map(struct mlxsw_sp_port *mlxsw_sp_vport, u16 fid, |
| 4452 | bool valid) |
| 4453 | { |
| 4454 | enum mlxsw_reg_svfa_mt mt = MLXSW_REG_SVFA_MT_PORT_VID_TO_FID; |
| 4455 | u16 vid = mlxsw_sp_vport_vid_get(mlxsw_sp_vport); |
| 4456 | |
| 4457 | return mlxsw_sp_port_vid_to_fid_set(mlxsw_sp_vport, mt, valid, fid, |
| 4458 | vid); |
| 4459 | } |
| 4460 | |
Ido Schimmel | 3ba2ebf | 2016-07-04 08:23:15 +0200 | [diff] [blame] | 4461 | static int mlxsw_sp_vport_vfid_join(struct mlxsw_sp_port *mlxsw_sp_vport, |
| 4462 | struct net_device *br_dev) |
Ido Schimmel | 26f0e7f | 2015-12-15 16:03:44 +0100 | [diff] [blame] | 4463 | { |
Ido Schimmel | 0355b59 | 2016-06-20 23:04:13 +0200 | [diff] [blame] | 4464 | struct mlxsw_sp_fid *f; |
Ido Schimmel | 26f0e7f | 2015-12-15 16:03:44 +0100 | [diff] [blame] | 4465 | int err; |
| 4466 | |
Ido Schimmel | 3ba2ebf | 2016-07-04 08:23:15 +0200 | [diff] [blame] | 4467 | f = mlxsw_sp_vfid_find(mlxsw_sp_vport->mlxsw_sp, br_dev); |
Ido Schimmel | 0355b59 | 2016-06-20 23:04:13 +0200 | [diff] [blame] | 4468 | if (!f) { |
Ido Schimmel | 3ba2ebf | 2016-07-04 08:23:15 +0200 | [diff] [blame] | 4469 | f = mlxsw_sp_vfid_create(mlxsw_sp_vport->mlxsw_sp, br_dev); |
Ido Schimmel | 0355b59 | 2016-06-20 23:04:13 +0200 | [diff] [blame] | 4470 | if (IS_ERR(f)) |
| 4471 | return PTR_ERR(f); |
Ido Schimmel | 26f0e7f | 2015-12-15 16:03:44 +0100 | [diff] [blame] | 4472 | } |
| 4473 | |
Ido Schimmel | 0355b59 | 2016-06-20 23:04:13 +0200 | [diff] [blame] | 4474 | err = mlxsw_sp_vport_flood_set(mlxsw_sp_vport, f->fid, true); |
| 4475 | if (err) |
Ido Schimmel | 26f0e7f | 2015-12-15 16:03:44 +0100 | [diff] [blame] | 4476 | goto err_vport_flood_set; |
Ido Schimmel | 26f0e7f | 2015-12-15 16:03:44 +0100 | [diff] [blame] | 4477 | |
Ido Schimmel | 0355b59 | 2016-06-20 23:04:13 +0200 | [diff] [blame] | 4478 | err = mlxsw_sp_vport_fid_map(mlxsw_sp_vport, f->fid, true); |
| 4479 | if (err) |
| 4480 | goto err_vport_fid_map; |
Ido Schimmel | 6a9863a | 2016-02-15 13:19:54 +0100 | [diff] [blame] | 4481 | |
Ido Schimmel | 41b996c | 2016-06-20 23:04:17 +0200 | [diff] [blame] | 4482 | mlxsw_sp_vport_fid_set(mlxsw_sp_vport, f); |
Ido Schimmel | 0355b59 | 2016-06-20 23:04:13 +0200 | [diff] [blame] | 4483 | f->ref_count++; |
Ido Schimmel | 039c49a | 2016-01-27 15:20:18 +0100 | [diff] [blame] | 4484 | |
Ido Schimmel | 2230537 | 2016-06-20 23:04:21 +0200 | [diff] [blame] | 4485 | netdev_dbg(mlxsw_sp_vport->dev, "Joined FID=%d\n", f->fid); |
| 4486 | |
Ido Schimmel | 0355b59 | 2016-06-20 23:04:13 +0200 | [diff] [blame] | 4487 | return 0; |
Ido Schimmel | 26f0e7f | 2015-12-15 16:03:44 +0100 | [diff] [blame] | 4488 | |
Ido Schimmel | 9c4d442 | 2016-06-20 23:04:10 +0200 | [diff] [blame] | 4489 | err_vport_fid_map: |
Ido Schimmel | 0355b59 | 2016-06-20 23:04:13 +0200 | [diff] [blame] | 4490 | mlxsw_sp_vport_flood_set(mlxsw_sp_vport, f->fid, false); |
| 4491 | err_vport_flood_set: |
| 4492 | if (!f->ref_count) |
Ido Schimmel | 3ba2ebf | 2016-07-04 08:23:15 +0200 | [diff] [blame] | 4493 | mlxsw_sp_vfid_destroy(mlxsw_sp_vport->mlxsw_sp, f); |
Ido Schimmel | 0355b59 | 2016-06-20 23:04:13 +0200 | [diff] [blame] | 4494 | return err; |
| 4495 | } |
| 4496 | |
Ido Schimmel | 3ba2ebf | 2016-07-04 08:23:15 +0200 | [diff] [blame] | 4497 | static void mlxsw_sp_vport_vfid_leave(struct mlxsw_sp_port *mlxsw_sp_vport) |
Ido Schimmel | 0355b59 | 2016-06-20 23:04:13 +0200 | [diff] [blame] | 4498 | { |
Ido Schimmel | 41b996c | 2016-06-20 23:04:17 +0200 | [diff] [blame] | 4499 | struct mlxsw_sp_fid *f = mlxsw_sp_vport_fid_get(mlxsw_sp_vport); |
Ido Schimmel | 0355b59 | 2016-06-20 23:04:13 +0200 | [diff] [blame] | 4500 | |
Ido Schimmel | 2230537 | 2016-06-20 23:04:21 +0200 | [diff] [blame] | 4501 | netdev_dbg(mlxsw_sp_vport->dev, "Left FID=%d\n", f->fid); |
| 4502 | |
Ido Schimmel | 0355b59 | 2016-06-20 23:04:13 +0200 | [diff] [blame] | 4503 | mlxsw_sp_vport_fid_map(mlxsw_sp_vport, f->fid, false); |
| 4504 | |
| 4505 | mlxsw_sp_vport_flood_set(mlxsw_sp_vport, f->fid, false); |
| 4506 | |
Ido Schimmel | fe3f6d1 | 2016-06-20 23:04:19 +0200 | [diff] [blame] | 4507 | mlxsw_sp_port_fdb_flush(mlxsw_sp_vport, f->fid); |
| 4508 | |
Ido Schimmel | 41b996c | 2016-06-20 23:04:17 +0200 | [diff] [blame] | 4509 | mlxsw_sp_vport_fid_set(mlxsw_sp_vport, NULL); |
Ido Schimmel | 0355b59 | 2016-06-20 23:04:13 +0200 | [diff] [blame] | 4510 | if (--f->ref_count == 0) |
Ido Schimmel | 3ba2ebf | 2016-07-04 08:23:15 +0200 | [diff] [blame] | 4511 | mlxsw_sp_vfid_destroy(mlxsw_sp_vport->mlxsw_sp, f); |
Ido Schimmel | 26f0e7f | 2015-12-15 16:03:44 +0100 | [diff] [blame] | 4512 | } |
| 4513 | |
| 4514 | static int mlxsw_sp_vport_bridge_join(struct mlxsw_sp_port *mlxsw_sp_vport, |
| 4515 | struct net_device *br_dev) |
| 4516 | { |
Ido Schimmel | 99724c1 | 2016-07-04 08:23:14 +0200 | [diff] [blame] | 4517 | struct mlxsw_sp_fid *f = mlxsw_sp_vport_fid_get(mlxsw_sp_vport); |
Ido Schimmel | 26f0e7f | 2015-12-15 16:03:44 +0100 | [diff] [blame] | 4518 | u16 vid = mlxsw_sp_vport_vid_get(mlxsw_sp_vport); |
| 4519 | struct net_device *dev = mlxsw_sp_vport->dev; |
Ido Schimmel | 26f0e7f | 2015-12-15 16:03:44 +0100 | [diff] [blame] | 4520 | int err; |
| 4521 | |
Ido Schimmel | 99724c1 | 2016-07-04 08:23:14 +0200 | [diff] [blame] | 4522 | if (f && !WARN_ON(!f->leave)) |
| 4523 | f->leave(mlxsw_sp_vport); |
Ido Schimmel | 26f0e7f | 2015-12-15 16:03:44 +0100 | [diff] [blame] | 4524 | |
Ido Schimmel | 3ba2ebf | 2016-07-04 08:23:15 +0200 | [diff] [blame] | 4525 | err = mlxsw_sp_vport_vfid_join(mlxsw_sp_vport, br_dev); |
Ido Schimmel | 26f0e7f | 2015-12-15 16:03:44 +0100 | [diff] [blame] | 4526 | if (err) { |
Ido Schimmel | 0355b59 | 2016-06-20 23:04:13 +0200 | [diff] [blame] | 4527 | netdev_err(dev, "Failed to join vFID\n"); |
Ido Schimmel | 99724c1 | 2016-07-04 08:23:14 +0200 | [diff] [blame] | 4528 | return err; |
Ido Schimmel | 26f0e7f | 2015-12-15 16:03:44 +0100 | [diff] [blame] | 4529 | } |
| 4530 | |
| 4531 | err = mlxsw_sp_port_vid_learning_set(mlxsw_sp_vport, vid, true); |
| 4532 | if (err) { |
| 4533 | netdev_err(dev, "Failed to enable learning\n"); |
| 4534 | goto err_port_vid_learning_set; |
| 4535 | } |
| 4536 | |
Ido Schimmel | 26f0e7f | 2015-12-15 16:03:44 +0100 | [diff] [blame] | 4537 | mlxsw_sp_vport->learning = 1; |
| 4538 | mlxsw_sp_vport->learning_sync = 1; |
| 4539 | mlxsw_sp_vport->uc_flood = 1; |
| 4540 | mlxsw_sp_vport->bridged = 1; |
| 4541 | |
| 4542 | return 0; |
| 4543 | |
Ido Schimmel | 26f0e7f | 2015-12-15 16:03:44 +0100 | [diff] [blame] | 4544 | err_port_vid_learning_set: |
Ido Schimmel | 3ba2ebf | 2016-07-04 08:23:15 +0200 | [diff] [blame] | 4545 | mlxsw_sp_vport_vfid_leave(mlxsw_sp_vport); |
Ido Schimmel | 26f0e7f | 2015-12-15 16:03:44 +0100 | [diff] [blame] | 4546 | return err; |
| 4547 | } |
| 4548 | |
Ido Schimmel | fe3f6d1 | 2016-06-20 23:04:19 +0200 | [diff] [blame] | 4549 | static void mlxsw_sp_vport_bridge_leave(struct mlxsw_sp_port *mlxsw_sp_vport) |
Ido Schimmel | 0355b59 | 2016-06-20 23:04:13 +0200 | [diff] [blame] | 4550 | { |
| 4551 | u16 vid = mlxsw_sp_vport_vid_get(mlxsw_sp_vport); |
Ido Schimmel | 0355b59 | 2016-06-20 23:04:13 +0200 | [diff] [blame] | 4552 | |
| 4553 | mlxsw_sp_port_vid_learning_set(mlxsw_sp_vport, vid, false); |
| 4554 | |
Ido Schimmel | 3ba2ebf | 2016-07-04 08:23:15 +0200 | [diff] [blame] | 4555 | mlxsw_sp_vport_vfid_leave(mlxsw_sp_vport); |
Ido Schimmel | 0355b59 | 2016-06-20 23:04:13 +0200 | [diff] [blame] | 4556 | |
Ido Schimmel | 0355b59 | 2016-06-20 23:04:13 +0200 | [diff] [blame] | 4557 | mlxsw_sp_vport->learning = 0; |
| 4558 | mlxsw_sp_vport->learning_sync = 0; |
| 4559 | mlxsw_sp_vport->uc_flood = 0; |
| 4560 | mlxsw_sp_vport->bridged = 0; |
| 4561 | } |
| 4562 | |
Ido Schimmel | 26f0e7f | 2015-12-15 16:03:44 +0100 | [diff] [blame] | 4563 | static bool |
| 4564 | mlxsw_sp_port_master_bridge_check(const struct mlxsw_sp_port *mlxsw_sp_port, |
| 4565 | const struct net_device *br_dev) |
| 4566 | { |
| 4567 | struct mlxsw_sp_port *mlxsw_sp_vport; |
| 4568 | |
| 4569 | list_for_each_entry(mlxsw_sp_vport, &mlxsw_sp_port->vports_list, |
| 4570 | vport.list) { |
Ido Schimmel | 3ba2ebf | 2016-07-04 08:23:15 +0200 | [diff] [blame] | 4571 | struct net_device *dev = mlxsw_sp_vport_dev_get(mlxsw_sp_vport); |
Ido Schimmel | 56918b6 | 2016-06-20 23:04:18 +0200 | [diff] [blame] | 4572 | |
| 4573 | if (dev && dev == br_dev) |
Ido Schimmel | 26f0e7f | 2015-12-15 16:03:44 +0100 | [diff] [blame] | 4574 | return false; |
| 4575 | } |
| 4576 | |
| 4577 | return true; |
| 4578 | } |
| 4579 | |
| 4580 | static int mlxsw_sp_netdevice_vport_event(struct net_device *dev, |
| 4581 | unsigned long event, void *ptr, |
| 4582 | u16 vid) |
| 4583 | { |
| 4584 | struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev); |
| 4585 | struct netdev_notifier_changeupper_info *info = ptr; |
| 4586 | struct mlxsw_sp_port *mlxsw_sp_vport; |
| 4587 | struct net_device *upper_dev; |
Ido Schimmel | 80bedf1 | 2016-06-20 23:03:59 +0200 | [diff] [blame] | 4588 | int err = 0; |
Ido Schimmel | 26f0e7f | 2015-12-15 16:03:44 +0100 | [diff] [blame] | 4589 | |
| 4590 | mlxsw_sp_vport = mlxsw_sp_port_vport_find(mlxsw_sp_port, vid); |
| 4591 | |
| 4592 | switch (event) { |
| 4593 | case NETDEV_PRECHANGEUPPER: |
| 4594 | upper_dev = info->upper_dev; |
Ido Schimmel | 26f0e7f | 2015-12-15 16:03:44 +0100 | [diff] [blame] | 4595 | if (!netif_is_bridge_master(upper_dev)) |
Ido Schimmel | 80bedf1 | 2016-06-20 23:03:59 +0200 | [diff] [blame] | 4596 | return -EINVAL; |
Ido Schimmel | ddbe993 | 2016-06-20 23:04:02 +0200 | [diff] [blame] | 4597 | if (!info->linking) |
| 4598 | break; |
Ido Schimmel | 26f0e7f | 2015-12-15 16:03:44 +0100 | [diff] [blame] | 4599 | /* We can't have multiple VLAN interfaces configured on |
| 4600 | * the same port and being members in the same bridge. |
| 4601 | */ |
| 4602 | if (!mlxsw_sp_port_master_bridge_check(mlxsw_sp_port, |
| 4603 | upper_dev)) |
Ido Schimmel | 80bedf1 | 2016-06-20 23:03:59 +0200 | [diff] [blame] | 4604 | return -EINVAL; |
Ido Schimmel | 26f0e7f | 2015-12-15 16:03:44 +0100 | [diff] [blame] | 4605 | break; |
| 4606 | case NETDEV_CHANGEUPPER: |
| 4607 | upper_dev = info->upper_dev; |
Ido Schimmel | 26f0e7f | 2015-12-15 16:03:44 +0100 | [diff] [blame] | 4608 | if (info->linking) { |
Ido Schimmel | 423b937 | 2016-06-20 23:04:03 +0200 | [diff] [blame] | 4609 | if (WARN_ON(!mlxsw_sp_vport)) |
Ido Schimmel | 80bedf1 | 2016-06-20 23:03:59 +0200 | [diff] [blame] | 4610 | return -EINVAL; |
Ido Schimmel | 26f0e7f | 2015-12-15 16:03:44 +0100 | [diff] [blame] | 4611 | err = mlxsw_sp_vport_bridge_join(mlxsw_sp_vport, |
| 4612 | upper_dev); |
Ido Schimmel | 26f0e7f | 2015-12-15 16:03:44 +0100 | [diff] [blame] | 4613 | } else { |
Ido Schimmel | 26f0e7f | 2015-12-15 16:03:44 +0100 | [diff] [blame] | 4614 | if (!mlxsw_sp_vport) |
Ido Schimmel | 80bedf1 | 2016-06-20 23:03:59 +0200 | [diff] [blame] | 4615 | return 0; |
Ido Schimmel | fe3f6d1 | 2016-06-20 23:04:19 +0200 | [diff] [blame] | 4616 | mlxsw_sp_vport_bridge_leave(mlxsw_sp_vport); |
Ido Schimmel | 26f0e7f | 2015-12-15 16:03:44 +0100 | [diff] [blame] | 4617 | } |
| 4618 | } |
| 4619 | |
Ido Schimmel | 80bedf1 | 2016-06-20 23:03:59 +0200 | [diff] [blame] | 4620 | return err; |
Ido Schimmel | 26f0e7f | 2015-12-15 16:03:44 +0100 | [diff] [blame] | 4621 | } |
| 4622 | |
Ido Schimmel | 272c447 | 2015-12-15 16:03:47 +0100 | [diff] [blame] | 4623 | static int mlxsw_sp_netdevice_lag_vport_event(struct net_device *lag_dev, |
| 4624 | unsigned long event, void *ptr, |
| 4625 | u16 vid) |
| 4626 | { |
| 4627 | struct net_device *dev; |
| 4628 | struct list_head *iter; |
| 4629 | int ret; |
| 4630 | |
| 4631 | netdev_for_each_lower_dev(lag_dev, dev, iter) { |
| 4632 | if (mlxsw_sp_port_dev_check(dev)) { |
| 4633 | ret = mlxsw_sp_netdevice_vport_event(dev, event, ptr, |
| 4634 | vid); |
Ido Schimmel | 80bedf1 | 2016-06-20 23:03:59 +0200 | [diff] [blame] | 4635 | if (ret) |
Ido Schimmel | 272c447 | 2015-12-15 16:03:47 +0100 | [diff] [blame] | 4636 | return ret; |
| 4637 | } |
| 4638 | } |
| 4639 | |
Ido Schimmel | 80bedf1 | 2016-06-20 23:03:59 +0200 | [diff] [blame] | 4640 | return 0; |
Ido Schimmel | 272c447 | 2015-12-15 16:03:47 +0100 | [diff] [blame] | 4641 | } |
| 4642 | |
Ido Schimmel | 26f0e7f | 2015-12-15 16:03:44 +0100 | [diff] [blame] | 4643 | static int mlxsw_sp_netdevice_vlan_event(struct net_device *vlan_dev, |
| 4644 | unsigned long event, void *ptr) |
| 4645 | { |
| 4646 | struct net_device *real_dev = vlan_dev_real_dev(vlan_dev); |
| 4647 | u16 vid = vlan_dev_vlan_id(vlan_dev); |
| 4648 | |
Ido Schimmel | 272c447 | 2015-12-15 16:03:47 +0100 | [diff] [blame] | 4649 | if (mlxsw_sp_port_dev_check(real_dev)) |
| 4650 | return mlxsw_sp_netdevice_vport_event(real_dev, event, ptr, |
| 4651 | vid); |
| 4652 | else if (netif_is_lag_master(real_dev)) |
| 4653 | return mlxsw_sp_netdevice_lag_vport_event(real_dev, event, ptr, |
| 4654 | vid); |
Ido Schimmel | 26f0e7f | 2015-12-15 16:03:44 +0100 | [diff] [blame] | 4655 | |
Ido Schimmel | 80bedf1 | 2016-06-20 23:03:59 +0200 | [diff] [blame] | 4656 | return 0; |
Ido Schimmel | 26f0e7f | 2015-12-15 16:03:44 +0100 | [diff] [blame] | 4657 | } |
| 4658 | |
Jiri Pirko | 0d65fc1 | 2015-12-03 12:12:28 +0100 | [diff] [blame] | 4659 | static int mlxsw_sp_netdevice_event(struct notifier_block *unused, |
| 4660 | unsigned long event, void *ptr) |
| 4661 | { |
| 4662 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); |
Ido Schimmel | 80bedf1 | 2016-06-20 23:03:59 +0200 | [diff] [blame] | 4663 | int err = 0; |
Jiri Pirko | 0d65fc1 | 2015-12-03 12:12:28 +0100 | [diff] [blame] | 4664 | |
Ido Schimmel | 6e095fd | 2016-07-04 08:23:13 +0200 | [diff] [blame] | 4665 | if (event == NETDEV_CHANGEADDR || event == NETDEV_CHANGEMTU) |
| 4666 | err = mlxsw_sp_netdevice_router_port_event(dev); |
| 4667 | else if (mlxsw_sp_port_dev_check(dev)) |
Ido Schimmel | 80bedf1 | 2016-06-20 23:03:59 +0200 | [diff] [blame] | 4668 | err = mlxsw_sp_netdevice_port_event(dev, event, ptr); |
| 4669 | else if (netif_is_lag_master(dev)) |
| 4670 | err = mlxsw_sp_netdevice_lag_event(dev, event, ptr); |
Ido Schimmel | 701b186 | 2016-07-04 08:23:16 +0200 | [diff] [blame] | 4671 | else if (netif_is_bridge_master(dev)) |
| 4672 | err = mlxsw_sp_netdevice_bridge_event(dev, event, ptr); |
Ido Schimmel | 80bedf1 | 2016-06-20 23:03:59 +0200 | [diff] [blame] | 4673 | else if (is_vlan_dev(dev)) |
| 4674 | err = mlxsw_sp_netdevice_vlan_event(dev, event, ptr); |
Jiri Pirko | 0d65fc1 | 2015-12-03 12:12:28 +0100 | [diff] [blame] | 4675 | |
Ido Schimmel | 80bedf1 | 2016-06-20 23:03:59 +0200 | [diff] [blame] | 4676 | return notifier_from_errno(err); |
Jiri Pirko | 0d65fc1 | 2015-12-03 12:12:28 +0100 | [diff] [blame] | 4677 | } |
| 4678 | |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 4679 | static struct notifier_block mlxsw_sp_netdevice_nb __read_mostly = { |
| 4680 | .notifier_call = mlxsw_sp_netdevice_event, |
| 4681 | }; |
| 4682 | |
Ido Schimmel | 99724c1 | 2016-07-04 08:23:14 +0200 | [diff] [blame] | 4683 | static struct notifier_block mlxsw_sp_inetaddr_nb __read_mostly = { |
| 4684 | .notifier_call = mlxsw_sp_inetaddr_event, |
| 4685 | .priority = 10, /* Must be called before FIB notifier block */ |
| 4686 | }; |
| 4687 | |
Jiri Pirko | e732263 | 2016-09-01 10:37:43 +0200 | [diff] [blame] | 4688 | static struct notifier_block mlxsw_sp_router_netevent_nb __read_mostly = { |
| 4689 | .notifier_call = mlxsw_sp_router_netevent_event, |
| 4690 | }; |
| 4691 | |
Jiri Pirko | 1d20d23 | 2016-10-27 15:12:59 +0200 | [diff] [blame] | 4692 | static const struct pci_device_id mlxsw_sp_pci_id_table[] = { |
| 4693 | {PCI_VDEVICE(MELLANOX, PCI_DEVICE_ID_MELLANOX_SPECTRUM), 0}, |
| 4694 | {0, }, |
| 4695 | }; |
| 4696 | |
| 4697 | static struct pci_driver mlxsw_sp_pci_driver = { |
| 4698 | .name = mlxsw_sp_driver_name, |
| 4699 | .id_table = mlxsw_sp_pci_id_table, |
| 4700 | }; |
| 4701 | |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 4702 | static int __init mlxsw_sp_module_init(void) |
| 4703 | { |
| 4704 | int err; |
| 4705 | |
| 4706 | register_netdevice_notifier(&mlxsw_sp_netdevice_nb); |
Ido Schimmel | 99724c1 | 2016-07-04 08:23:14 +0200 | [diff] [blame] | 4707 | register_inetaddr_notifier(&mlxsw_sp_inetaddr_nb); |
Jiri Pirko | e732263 | 2016-09-01 10:37:43 +0200 | [diff] [blame] | 4708 | register_netevent_notifier(&mlxsw_sp_router_netevent_nb); |
| 4709 | |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 4710 | err = mlxsw_core_driver_register(&mlxsw_sp_driver); |
| 4711 | if (err) |
| 4712 | goto err_core_driver_register; |
Jiri Pirko | 1d20d23 | 2016-10-27 15:12:59 +0200 | [diff] [blame] | 4713 | |
| 4714 | err = mlxsw_pci_driver_register(&mlxsw_sp_pci_driver); |
| 4715 | if (err) |
| 4716 | goto err_pci_driver_register; |
| 4717 | |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 4718 | return 0; |
| 4719 | |
Jiri Pirko | 1d20d23 | 2016-10-27 15:12:59 +0200 | [diff] [blame] | 4720 | err_pci_driver_register: |
| 4721 | mlxsw_core_driver_unregister(&mlxsw_sp_driver); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 4722 | err_core_driver_register: |
Jiri Pirko | e732263 | 2016-09-01 10:37:43 +0200 | [diff] [blame] | 4723 | unregister_netevent_notifier(&mlxsw_sp_router_netevent_nb); |
Jiri Pirko | de7d629 | 2016-09-01 10:37:42 +0200 | [diff] [blame] | 4724 | unregister_inetaddr_notifier(&mlxsw_sp_inetaddr_nb); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 4725 | unregister_netdevice_notifier(&mlxsw_sp_netdevice_nb); |
| 4726 | return err; |
| 4727 | } |
| 4728 | |
| 4729 | static void __exit mlxsw_sp_module_exit(void) |
| 4730 | { |
Jiri Pirko | 1d20d23 | 2016-10-27 15:12:59 +0200 | [diff] [blame] | 4731 | mlxsw_pci_driver_unregister(&mlxsw_sp_pci_driver); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 4732 | mlxsw_core_driver_unregister(&mlxsw_sp_driver); |
Jiri Pirko | e732263 | 2016-09-01 10:37:43 +0200 | [diff] [blame] | 4733 | unregister_netevent_notifier(&mlxsw_sp_router_netevent_nb); |
Ido Schimmel | 99724c1 | 2016-07-04 08:23:14 +0200 | [diff] [blame] | 4734 | unregister_inetaddr_notifier(&mlxsw_sp_inetaddr_nb); |
Jiri Pirko | 56ade8f | 2015-10-16 14:01:37 +0200 | [diff] [blame] | 4735 | unregister_netdevice_notifier(&mlxsw_sp_netdevice_nb); |
| 4736 | } |
| 4737 | |
| 4738 | module_init(mlxsw_sp_module_init); |
| 4739 | module_exit(mlxsw_sp_module_exit); |
| 4740 | |
| 4741 | MODULE_LICENSE("Dual BSD/GPL"); |
| 4742 | MODULE_AUTHOR("Jiri Pirko <jiri@mellanox.com>"); |
| 4743 | MODULE_DESCRIPTION("Mellanox Spectrum driver"); |
Jiri Pirko | 1d20d23 | 2016-10-27 15:12:59 +0200 | [diff] [blame] | 4744 | MODULE_DEVICE_TABLE(pci, mlxsw_sp_pci_id_table); |