1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0 2 /* Copyright (c) 2018 Mellanox Technologies. All rights reserved */ 3 4 #include <linux/if_bridge.h> 5 #include <linux/list.h> 6 #include <linux/mutex.h> 7 #include <linux/refcount.h> 8 #include <linux/rtnetlink.h> 9 #include <linux/workqueue.h> 10 #include <net/arp.h> 11 #include <net/gre.h> 12 #include <net/lag.h> 13 #include <net/ndisc.h> 14 #include <net/ip6_tunnel.h> 15 16 #include "spectrum.h" 17 #include "spectrum_ipip.h" 18 #include "spectrum_span.h" 19 #include "spectrum_switchdev.h" 20 21 struct mlxsw_sp_span { 22 struct work_struct work; 23 struct mlxsw_sp *mlxsw_sp; 24 const struct mlxsw_sp_span_trigger_ops **span_trigger_ops_arr; 25 const struct mlxsw_sp_span_entry_ops **span_entry_ops_arr; 26 size_t span_entry_ops_arr_size; 27 struct list_head analyzed_ports_list; 28 struct mutex analyzed_ports_lock; /* Protects analyzed_ports_list */ 29 struct list_head trigger_entries_list; 30 u16 policer_id_base; 31 refcount_t policer_id_base_ref_count; 32 atomic_t active_entries_count; 33 int entries_count; 34 struct mlxsw_sp_span_entry entries[]; 35 }; 36 37 struct mlxsw_sp_span_analyzed_port { 38 struct list_head list; /* Member of analyzed_ports_list */ 39 refcount_t ref_count; 40 u8 local_port; 41 bool ingress; 42 }; 43 44 struct mlxsw_sp_span_trigger_entry { 45 struct list_head list; /* Member of trigger_entries_list */ 46 struct mlxsw_sp_span *span; 47 const struct mlxsw_sp_span_trigger_ops *ops; 48 refcount_t ref_count; 49 u8 local_port; 50 enum mlxsw_sp_span_trigger trigger; 51 struct mlxsw_sp_span_trigger_parms parms; 52 }; 53 54 enum mlxsw_sp_span_trigger_type { 55 MLXSW_SP_SPAN_TRIGGER_TYPE_PORT, 56 MLXSW_SP_SPAN_TRIGGER_TYPE_GLOBAL, 57 }; 58 59 struct mlxsw_sp_span_trigger_ops { 60 int (*bind)(struct mlxsw_sp_span_trigger_entry *trigger_entry); 61 void (*unbind)(struct mlxsw_sp_span_trigger_entry *trigger_entry); 62 bool (*matches)(struct mlxsw_sp_span_trigger_entry *trigger_entry, 63 enum mlxsw_sp_span_trigger trigger, 64 struct mlxsw_sp_port *mlxsw_sp_port); 65 int (*enable)(struct mlxsw_sp_span_trigger_entry *trigger_entry, 66 struct mlxsw_sp_port *mlxsw_sp_port, u8 tc); 67 void (*disable)(struct mlxsw_sp_span_trigger_entry *trigger_entry, 68 struct mlxsw_sp_port *mlxsw_sp_port, u8 tc); 69 }; 70 71 static void mlxsw_sp_span_respin_work(struct work_struct *work); 72 73 static u64 mlxsw_sp_span_occ_get(void *priv) 74 { 75 const struct mlxsw_sp *mlxsw_sp = priv; 76 77 return atomic_read(&mlxsw_sp->span->active_entries_count); 78 } 79 80 int mlxsw_sp_span_init(struct mlxsw_sp *mlxsw_sp) 81 { 82 struct devlink *devlink = priv_to_devlink(mlxsw_sp->core); 83 struct mlxsw_sp_span *span; 84 int i, entries_count, err; 85 86 if (!MLXSW_CORE_RES_VALID(mlxsw_sp->core, MAX_SPAN)) 87 return -EIO; 88 89 entries_count = MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_SPAN); 90 span = kzalloc(struct_size(span, entries, entries_count), GFP_KERNEL); 91 if (!span) 92 return -ENOMEM; 93 refcount_set(&span->policer_id_base_ref_count, 0); 94 span->entries_count = entries_count; 95 atomic_set(&span->active_entries_count, 0); 96 mutex_init(&span->analyzed_ports_lock); 97 INIT_LIST_HEAD(&span->analyzed_ports_list); 98 INIT_LIST_HEAD(&span->trigger_entries_list); 99 span->mlxsw_sp = mlxsw_sp; 100 mlxsw_sp->span = span; 101 102 for (i = 0; i < mlxsw_sp->span->entries_count; i++) 103 mlxsw_sp->span->entries[i].id = i; 104 105 err = mlxsw_sp->span_ops->init(mlxsw_sp); 106 if (err) 107 goto err_init; 108 109 devlink_resource_occ_get_register(devlink, MLXSW_SP_RESOURCE_SPAN, 110 mlxsw_sp_span_occ_get, mlxsw_sp); 111 INIT_WORK(&span->work, mlxsw_sp_span_respin_work); 112 113 return 0; 114 115 err_init: 116 mutex_destroy(&mlxsw_sp->span->analyzed_ports_lock); 117 kfree(mlxsw_sp->span); 118 return err; 119 } 120 121 void mlxsw_sp_span_fini(struct mlxsw_sp *mlxsw_sp) 122 { 123 struct devlink *devlink = priv_to_devlink(mlxsw_sp->core); 124 125 cancel_work_sync(&mlxsw_sp->span->work); 126 devlink_resource_occ_get_unregister(devlink, MLXSW_SP_RESOURCE_SPAN); 127 128 WARN_ON_ONCE(!list_empty(&mlxsw_sp->span->trigger_entries_list)); 129 WARN_ON_ONCE(!list_empty(&mlxsw_sp->span->analyzed_ports_list)); 130 mutex_destroy(&mlxsw_sp->span->analyzed_ports_lock); 131 kfree(mlxsw_sp->span); 132 } 133 134 static bool mlxsw_sp1_span_cpu_can_handle(const struct net_device *dev) 135 { 136 return !dev; 137 } 138 139 static int mlxsw_sp1_span_entry_cpu_parms(struct mlxsw_sp *mlxsw_sp, 140 const struct net_device *to_dev, 141 struct mlxsw_sp_span_parms *sparmsp) 142 { 143 return -EOPNOTSUPP; 144 } 145 146 static int 147 mlxsw_sp1_span_entry_cpu_configure(struct mlxsw_sp_span_entry *span_entry, 148 struct mlxsw_sp_span_parms sparms) 149 { 150 return -EOPNOTSUPP; 151 } 152 153 static void 154 mlxsw_sp1_span_entry_cpu_deconfigure(struct mlxsw_sp_span_entry *span_entry) 155 { 156 } 157 158 static const 159 struct mlxsw_sp_span_entry_ops mlxsw_sp1_span_entry_ops_cpu = { 160 .can_handle = mlxsw_sp1_span_cpu_can_handle, 161 .parms_set = mlxsw_sp1_span_entry_cpu_parms, 162 .configure = mlxsw_sp1_span_entry_cpu_configure, 163 .deconfigure = mlxsw_sp1_span_entry_cpu_deconfigure, 164 }; 165 166 static int 167 mlxsw_sp_span_entry_phys_parms(struct mlxsw_sp *mlxsw_sp, 168 const struct net_device *to_dev, 169 struct mlxsw_sp_span_parms *sparmsp) 170 { 171 sparmsp->dest_port = netdev_priv(to_dev); 172 return 0; 173 } 174 175 static int 176 mlxsw_sp_span_entry_phys_configure(struct mlxsw_sp_span_entry *span_entry, 177 struct mlxsw_sp_span_parms sparms) 178 { 179 struct mlxsw_sp_port *dest_port = sparms.dest_port; 180 struct mlxsw_sp *mlxsw_sp = dest_port->mlxsw_sp; 181 u8 local_port = dest_port->local_port; 182 char mpat_pl[MLXSW_REG_MPAT_LEN]; 183 int pa_id = span_entry->id; 184 185 /* Create a new port analayzer entry for local_port. */ 186 mlxsw_reg_mpat_pack(mpat_pl, pa_id, local_port, true, 187 MLXSW_REG_MPAT_SPAN_TYPE_LOCAL_ETH); 188 mlxsw_reg_mpat_pide_set(mpat_pl, sparms.policer_enable); 189 mlxsw_reg_mpat_pid_set(mpat_pl, sparms.policer_id); 190 191 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(mpat), mpat_pl); 192 } 193 194 static void 195 mlxsw_sp_span_entry_deconfigure_common(struct mlxsw_sp_span_entry *span_entry, 196 enum mlxsw_reg_mpat_span_type span_type) 197 { 198 struct mlxsw_sp_port *dest_port = span_entry->parms.dest_port; 199 struct mlxsw_sp *mlxsw_sp = dest_port->mlxsw_sp; 200 u8 local_port = dest_port->local_port; 201 char mpat_pl[MLXSW_REG_MPAT_LEN]; 202 int pa_id = span_entry->id; 203 204 mlxsw_reg_mpat_pack(mpat_pl, pa_id, local_port, false, span_type); 205 mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(mpat), mpat_pl); 206 } 207 208 static void 209 mlxsw_sp_span_entry_phys_deconfigure(struct mlxsw_sp_span_entry *span_entry) 210 { 211 mlxsw_sp_span_entry_deconfigure_common(span_entry, 212 MLXSW_REG_MPAT_SPAN_TYPE_LOCAL_ETH); 213 } 214 215 static const 216 struct mlxsw_sp_span_entry_ops mlxsw_sp_span_entry_ops_phys = { 217 .can_handle = mlxsw_sp_port_dev_check, 218 .parms_set = mlxsw_sp_span_entry_phys_parms, 219 .configure = mlxsw_sp_span_entry_phys_configure, 220 .deconfigure = mlxsw_sp_span_entry_phys_deconfigure, 221 }; 222 223 static int mlxsw_sp_span_dmac(struct neigh_table *tbl, 224 const void *pkey, 225 struct net_device *dev, 226 unsigned char dmac[ETH_ALEN]) 227 { 228 struct neighbour *neigh = neigh_lookup(tbl, pkey, dev); 229 int err = 0; 230 231 if (!neigh) { 232 neigh = neigh_create(tbl, pkey, dev); 233 if (IS_ERR(neigh)) 234 return PTR_ERR(neigh); 235 } 236 237 neigh_event_send(neigh, NULL); 238 239 read_lock_bh(&neigh->lock); 240 if ((neigh->nud_state & NUD_VALID) && !neigh->dead) 241 memcpy(dmac, neigh->ha, ETH_ALEN); 242 else 243 err = -ENOENT; 244 read_unlock_bh(&neigh->lock); 245 246 neigh_release(neigh); 247 return err; 248 } 249 250 static int 251 mlxsw_sp_span_entry_unoffloadable(struct mlxsw_sp_span_parms *sparmsp) 252 { 253 sparmsp->dest_port = NULL; 254 return 0; 255 } 256 257 static struct net_device * 258 mlxsw_sp_span_entry_bridge_8021q(const struct net_device *br_dev, 259 unsigned char *dmac, 260 u16 *p_vid) 261 { 262 struct bridge_vlan_info vinfo; 263 struct net_device *edev; 264 u16 vid = *p_vid; 265 266 if (!vid && WARN_ON(br_vlan_get_pvid(br_dev, &vid))) 267 return NULL; 268 if (!vid || 269 br_vlan_get_info(br_dev, vid, &vinfo) || 270 !(vinfo.flags & BRIDGE_VLAN_INFO_BRENTRY)) 271 return NULL; 272 273 edev = br_fdb_find_port(br_dev, dmac, vid); 274 if (!edev) 275 return NULL; 276 277 if (br_vlan_get_info(edev, vid, &vinfo)) 278 return NULL; 279 if (vinfo.flags & BRIDGE_VLAN_INFO_UNTAGGED) 280 *p_vid = 0; 281 else 282 *p_vid = vid; 283 return edev; 284 } 285 286 static struct net_device * 287 mlxsw_sp_span_entry_bridge_8021d(const struct net_device *br_dev, 288 unsigned char *dmac) 289 { 290 return br_fdb_find_port(br_dev, dmac, 0); 291 } 292 293 static struct net_device * 294 mlxsw_sp_span_entry_bridge(const struct net_device *br_dev, 295 unsigned char dmac[ETH_ALEN], 296 u16 *p_vid) 297 { 298 struct mlxsw_sp_bridge_port *bridge_port; 299 enum mlxsw_reg_spms_state spms_state; 300 struct net_device *dev = NULL; 301 struct mlxsw_sp_port *port; 302 u8 stp_state; 303 304 if (br_vlan_enabled(br_dev)) 305 dev = mlxsw_sp_span_entry_bridge_8021q(br_dev, dmac, p_vid); 306 else if (!*p_vid) 307 dev = mlxsw_sp_span_entry_bridge_8021d(br_dev, dmac); 308 if (!dev) 309 return NULL; 310 311 port = mlxsw_sp_port_dev_lower_find(dev); 312 if (!port) 313 return NULL; 314 315 bridge_port = mlxsw_sp_bridge_port_find(port->mlxsw_sp->bridge, dev); 316 if (!bridge_port) 317 return NULL; 318 319 stp_state = mlxsw_sp_bridge_port_stp_state(bridge_port); 320 spms_state = mlxsw_sp_stp_spms_state(stp_state); 321 if (spms_state != MLXSW_REG_SPMS_STATE_FORWARDING) 322 return NULL; 323 324 return dev; 325 } 326 327 static struct net_device * 328 mlxsw_sp_span_entry_vlan(const struct net_device *vlan_dev, 329 u16 *p_vid) 330 { 331 *p_vid = vlan_dev_vlan_id(vlan_dev); 332 return vlan_dev_real_dev(vlan_dev); 333 } 334 335 static struct net_device * 336 mlxsw_sp_span_entry_lag(struct net_device *lag_dev) 337 { 338 struct net_device *dev; 339 struct list_head *iter; 340 341 netdev_for_each_lower_dev(lag_dev, dev, iter) 342 if (netif_carrier_ok(dev) && 343 net_lag_port_dev_txable(dev) && 344 mlxsw_sp_port_dev_check(dev)) 345 return dev; 346 347 return NULL; 348 } 349 350 static __maybe_unused int 351 mlxsw_sp_span_entry_tunnel_parms_common(struct net_device *edev, 352 union mlxsw_sp_l3addr saddr, 353 union mlxsw_sp_l3addr daddr, 354 union mlxsw_sp_l3addr gw, 355 __u8 ttl, 356 struct neigh_table *tbl, 357 struct mlxsw_sp_span_parms *sparmsp) 358 { 359 unsigned char dmac[ETH_ALEN]; 360 u16 vid = 0; 361 362 if (mlxsw_sp_l3addr_is_zero(gw)) 363 gw = daddr; 364 365 if (!edev || mlxsw_sp_span_dmac(tbl, &gw, edev, dmac)) 366 goto unoffloadable; 367 368 if (is_vlan_dev(edev)) 369 edev = mlxsw_sp_span_entry_vlan(edev, &vid); 370 371 if (netif_is_bridge_master(edev)) { 372 edev = mlxsw_sp_span_entry_bridge(edev, dmac, &vid); 373 if (!edev) 374 goto unoffloadable; 375 } 376 377 if (is_vlan_dev(edev)) { 378 if (vid || !(edev->flags & IFF_UP)) 379 goto unoffloadable; 380 edev = mlxsw_sp_span_entry_vlan(edev, &vid); 381 } 382 383 if (netif_is_lag_master(edev)) { 384 if (!(edev->flags & IFF_UP)) 385 goto unoffloadable; 386 edev = mlxsw_sp_span_entry_lag(edev); 387 if (!edev) 388 goto unoffloadable; 389 } 390 391 if (!mlxsw_sp_port_dev_check(edev)) 392 goto unoffloadable; 393 394 sparmsp->dest_port = netdev_priv(edev); 395 sparmsp->ttl = ttl; 396 memcpy(sparmsp->dmac, dmac, ETH_ALEN); 397 memcpy(sparmsp->smac, edev->dev_addr, ETH_ALEN); 398 sparmsp->saddr = saddr; 399 sparmsp->daddr = daddr; 400 sparmsp->vid = vid; 401 return 0; 402 403 unoffloadable: 404 return mlxsw_sp_span_entry_unoffloadable(sparmsp); 405 } 406 407 #if IS_ENABLED(CONFIG_NET_IPGRE) 408 static struct net_device * 409 mlxsw_sp_span_gretap4_route(const struct net_device *to_dev, 410 __be32 *saddrp, __be32 *daddrp) 411 { 412 struct ip_tunnel *tun = netdev_priv(to_dev); 413 struct net_device *dev = NULL; 414 struct ip_tunnel_parm parms; 415 struct rtable *rt = NULL; 416 struct flowi4 fl4; 417 418 /* We assume "dev" stays valid after rt is put. */ 419 ASSERT_RTNL(); 420 421 parms = mlxsw_sp_ipip_netdev_parms4(to_dev); 422 ip_tunnel_init_flow(&fl4, parms.iph.protocol, *daddrp, *saddrp, 423 0, 0, parms.link, tun->fwmark, 0); 424 425 rt = ip_route_output_key(tun->net, &fl4); 426 if (IS_ERR(rt)) 427 return NULL; 428 429 if (rt->rt_type != RTN_UNICAST) 430 goto out; 431 432 dev = rt->dst.dev; 433 *saddrp = fl4.saddr; 434 if (rt->rt_gw_family == AF_INET) 435 *daddrp = rt->rt_gw4; 436 /* can not offload if route has an IPv6 gateway */ 437 else if (rt->rt_gw_family == AF_INET6) 438 dev = NULL; 439 440 out: 441 ip_rt_put(rt); 442 return dev; 443 } 444 445 static int 446 mlxsw_sp_span_entry_gretap4_parms(struct mlxsw_sp *mlxsw_sp, 447 const struct net_device *to_dev, 448 struct mlxsw_sp_span_parms *sparmsp) 449 { 450 struct ip_tunnel_parm tparm = mlxsw_sp_ipip_netdev_parms4(to_dev); 451 union mlxsw_sp_l3addr saddr = { .addr4 = tparm.iph.saddr }; 452 union mlxsw_sp_l3addr daddr = { .addr4 = tparm.iph.daddr }; 453 bool inherit_tos = tparm.iph.tos & 0x1; 454 bool inherit_ttl = !tparm.iph.ttl; 455 union mlxsw_sp_l3addr gw = daddr; 456 struct net_device *l3edev; 457 458 if (!(to_dev->flags & IFF_UP) || 459 /* Reject tunnels with GRE keys, checksums, etc. */ 460 tparm.i_flags || tparm.o_flags || 461 /* Require a fixed TTL and a TOS copied from the mirrored packet. */ 462 inherit_ttl || !inherit_tos || 463 /* A destination address may not be "any". */ 464 mlxsw_sp_l3addr_is_zero(daddr)) 465 return mlxsw_sp_span_entry_unoffloadable(sparmsp); 466 467 l3edev = mlxsw_sp_span_gretap4_route(to_dev, &saddr.addr4, &gw.addr4); 468 return mlxsw_sp_span_entry_tunnel_parms_common(l3edev, saddr, daddr, gw, 469 tparm.iph.ttl, 470 &arp_tbl, sparmsp); 471 } 472 473 static int 474 mlxsw_sp_span_entry_gretap4_configure(struct mlxsw_sp_span_entry *span_entry, 475 struct mlxsw_sp_span_parms sparms) 476 { 477 struct mlxsw_sp_port *dest_port = sparms.dest_port; 478 struct mlxsw_sp *mlxsw_sp = dest_port->mlxsw_sp; 479 u8 local_port = dest_port->local_port; 480 char mpat_pl[MLXSW_REG_MPAT_LEN]; 481 int pa_id = span_entry->id; 482 483 /* Create a new port analayzer entry for local_port. */ 484 mlxsw_reg_mpat_pack(mpat_pl, pa_id, local_port, true, 485 MLXSW_REG_MPAT_SPAN_TYPE_REMOTE_ETH_L3); 486 mlxsw_reg_mpat_pide_set(mpat_pl, sparms.policer_enable); 487 mlxsw_reg_mpat_pid_set(mpat_pl, sparms.policer_id); 488 mlxsw_reg_mpat_eth_rspan_pack(mpat_pl, sparms.vid); 489 mlxsw_reg_mpat_eth_rspan_l2_pack(mpat_pl, 490 MLXSW_REG_MPAT_ETH_RSPAN_VERSION_NO_HEADER, 491 sparms.dmac, !!sparms.vid); 492 mlxsw_reg_mpat_eth_rspan_l3_ipv4_pack(mpat_pl, 493 sparms.ttl, sparms.smac, 494 be32_to_cpu(sparms.saddr.addr4), 495 be32_to_cpu(sparms.daddr.addr4)); 496 497 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(mpat), mpat_pl); 498 } 499 500 static void 501 mlxsw_sp_span_entry_gretap4_deconfigure(struct mlxsw_sp_span_entry *span_entry) 502 { 503 mlxsw_sp_span_entry_deconfigure_common(span_entry, 504 MLXSW_REG_MPAT_SPAN_TYPE_REMOTE_ETH_L3); 505 } 506 507 static const struct mlxsw_sp_span_entry_ops mlxsw_sp_span_entry_ops_gretap4 = { 508 .can_handle = netif_is_gretap, 509 .parms_set = mlxsw_sp_span_entry_gretap4_parms, 510 .configure = mlxsw_sp_span_entry_gretap4_configure, 511 .deconfigure = mlxsw_sp_span_entry_gretap4_deconfigure, 512 }; 513 #endif 514 515 #if IS_ENABLED(CONFIG_IPV6_GRE) 516 static struct net_device * 517 mlxsw_sp_span_gretap6_route(const struct net_device *to_dev, 518 struct in6_addr *saddrp, 519 struct in6_addr *daddrp) 520 { 521 struct ip6_tnl *t = netdev_priv(to_dev); 522 struct flowi6 fl6 = t->fl.u.ip6; 523 struct net_device *dev = NULL; 524 struct dst_entry *dst; 525 struct rt6_info *rt6; 526 527 /* We assume "dev" stays valid after dst is released. */ 528 ASSERT_RTNL(); 529 530 fl6.flowi6_mark = t->parms.fwmark; 531 if (!ip6_tnl_xmit_ctl(t, &fl6.saddr, &fl6.daddr)) 532 return NULL; 533 534 dst = ip6_route_output(t->net, NULL, &fl6); 535 if (!dst || dst->error) 536 goto out; 537 538 rt6 = container_of(dst, struct rt6_info, dst); 539 540 dev = dst->dev; 541 *saddrp = fl6.saddr; 542 *daddrp = rt6->rt6i_gateway; 543 544 out: 545 dst_release(dst); 546 return dev; 547 } 548 549 static int 550 mlxsw_sp_span_entry_gretap6_parms(struct mlxsw_sp *mlxsw_sp, 551 const struct net_device *to_dev, 552 struct mlxsw_sp_span_parms *sparmsp) 553 { 554 struct __ip6_tnl_parm tparm = mlxsw_sp_ipip_netdev_parms6(to_dev); 555 bool inherit_tos = tparm.flags & IP6_TNL_F_USE_ORIG_TCLASS; 556 union mlxsw_sp_l3addr saddr = { .addr6 = tparm.laddr }; 557 union mlxsw_sp_l3addr daddr = { .addr6 = tparm.raddr }; 558 bool inherit_ttl = !tparm.hop_limit; 559 union mlxsw_sp_l3addr gw = daddr; 560 struct net_device *l3edev; 561 562 if (!(to_dev->flags & IFF_UP) || 563 /* Reject tunnels with GRE keys, checksums, etc. */ 564 tparm.i_flags || tparm.o_flags || 565 /* Require a fixed TTL and a TOS copied from the mirrored packet. */ 566 inherit_ttl || !inherit_tos || 567 /* A destination address may not be "any". */ 568 mlxsw_sp_l3addr_is_zero(daddr)) 569 return mlxsw_sp_span_entry_unoffloadable(sparmsp); 570 571 l3edev = mlxsw_sp_span_gretap6_route(to_dev, &saddr.addr6, &gw.addr6); 572 return mlxsw_sp_span_entry_tunnel_parms_common(l3edev, saddr, daddr, gw, 573 tparm.hop_limit, 574 &nd_tbl, sparmsp); 575 } 576 577 static int 578 mlxsw_sp_span_entry_gretap6_configure(struct mlxsw_sp_span_entry *span_entry, 579 struct mlxsw_sp_span_parms sparms) 580 { 581 struct mlxsw_sp_port *dest_port = sparms.dest_port; 582 struct mlxsw_sp *mlxsw_sp = dest_port->mlxsw_sp; 583 u8 local_port = dest_port->local_port; 584 char mpat_pl[MLXSW_REG_MPAT_LEN]; 585 int pa_id = span_entry->id; 586 587 /* Create a new port analayzer entry for local_port. */ 588 mlxsw_reg_mpat_pack(mpat_pl, pa_id, local_port, true, 589 MLXSW_REG_MPAT_SPAN_TYPE_REMOTE_ETH_L3); 590 mlxsw_reg_mpat_pide_set(mpat_pl, sparms.policer_enable); 591 mlxsw_reg_mpat_pid_set(mpat_pl, sparms.policer_id); 592 mlxsw_reg_mpat_eth_rspan_pack(mpat_pl, sparms.vid); 593 mlxsw_reg_mpat_eth_rspan_l2_pack(mpat_pl, 594 MLXSW_REG_MPAT_ETH_RSPAN_VERSION_NO_HEADER, 595 sparms.dmac, !!sparms.vid); 596 mlxsw_reg_mpat_eth_rspan_l3_ipv6_pack(mpat_pl, sparms.ttl, sparms.smac, 597 sparms.saddr.addr6, 598 sparms.daddr.addr6); 599 600 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(mpat), mpat_pl); 601 } 602 603 static void 604 mlxsw_sp_span_entry_gretap6_deconfigure(struct mlxsw_sp_span_entry *span_entry) 605 { 606 mlxsw_sp_span_entry_deconfigure_common(span_entry, 607 MLXSW_REG_MPAT_SPAN_TYPE_REMOTE_ETH_L3); 608 } 609 610 static const 611 struct mlxsw_sp_span_entry_ops mlxsw_sp_span_entry_ops_gretap6 = { 612 .can_handle = netif_is_ip6gretap, 613 .parms_set = mlxsw_sp_span_entry_gretap6_parms, 614 .configure = mlxsw_sp_span_entry_gretap6_configure, 615 .deconfigure = mlxsw_sp_span_entry_gretap6_deconfigure, 616 }; 617 #endif 618 619 static bool 620 mlxsw_sp_span_vlan_can_handle(const struct net_device *dev) 621 { 622 return is_vlan_dev(dev) && 623 mlxsw_sp_port_dev_check(vlan_dev_real_dev(dev)); 624 } 625 626 static int 627 mlxsw_sp_span_entry_vlan_parms(struct mlxsw_sp *mlxsw_sp, 628 const struct net_device *to_dev, 629 struct mlxsw_sp_span_parms *sparmsp) 630 { 631 struct net_device *real_dev; 632 u16 vid; 633 634 if (!(to_dev->flags & IFF_UP)) 635 return mlxsw_sp_span_entry_unoffloadable(sparmsp); 636 637 real_dev = mlxsw_sp_span_entry_vlan(to_dev, &vid); 638 sparmsp->dest_port = netdev_priv(real_dev); 639 sparmsp->vid = vid; 640 return 0; 641 } 642 643 static int 644 mlxsw_sp_span_entry_vlan_configure(struct mlxsw_sp_span_entry *span_entry, 645 struct mlxsw_sp_span_parms sparms) 646 { 647 struct mlxsw_sp_port *dest_port = sparms.dest_port; 648 struct mlxsw_sp *mlxsw_sp = dest_port->mlxsw_sp; 649 u8 local_port = dest_port->local_port; 650 char mpat_pl[MLXSW_REG_MPAT_LEN]; 651 int pa_id = span_entry->id; 652 653 mlxsw_reg_mpat_pack(mpat_pl, pa_id, local_port, true, 654 MLXSW_REG_MPAT_SPAN_TYPE_REMOTE_ETH); 655 mlxsw_reg_mpat_pide_set(mpat_pl, sparms.policer_enable); 656 mlxsw_reg_mpat_pid_set(mpat_pl, sparms.policer_id); 657 mlxsw_reg_mpat_eth_rspan_pack(mpat_pl, sparms.vid); 658 659 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(mpat), mpat_pl); 660 } 661 662 static void 663 mlxsw_sp_span_entry_vlan_deconfigure(struct mlxsw_sp_span_entry *span_entry) 664 { 665 mlxsw_sp_span_entry_deconfigure_common(span_entry, 666 MLXSW_REG_MPAT_SPAN_TYPE_REMOTE_ETH); 667 } 668 669 static const 670 struct mlxsw_sp_span_entry_ops mlxsw_sp_span_entry_ops_vlan = { 671 .can_handle = mlxsw_sp_span_vlan_can_handle, 672 .parms_set = mlxsw_sp_span_entry_vlan_parms, 673 .configure = mlxsw_sp_span_entry_vlan_configure, 674 .deconfigure = mlxsw_sp_span_entry_vlan_deconfigure, 675 }; 676 677 static const 678 struct mlxsw_sp_span_entry_ops *mlxsw_sp1_span_entry_ops_arr[] = { 679 &mlxsw_sp1_span_entry_ops_cpu, 680 &mlxsw_sp_span_entry_ops_phys, 681 #if IS_ENABLED(CONFIG_NET_IPGRE) 682 &mlxsw_sp_span_entry_ops_gretap4, 683 #endif 684 #if IS_ENABLED(CONFIG_IPV6_GRE) 685 &mlxsw_sp_span_entry_ops_gretap6, 686 #endif 687 &mlxsw_sp_span_entry_ops_vlan, 688 }; 689 690 static bool mlxsw_sp2_span_cpu_can_handle(const struct net_device *dev) 691 { 692 return !dev; 693 } 694 695 static int mlxsw_sp2_span_entry_cpu_parms(struct mlxsw_sp *mlxsw_sp, 696 const struct net_device *to_dev, 697 struct mlxsw_sp_span_parms *sparmsp) 698 { 699 sparmsp->dest_port = mlxsw_sp->ports[MLXSW_PORT_CPU_PORT]; 700 return 0; 701 } 702 703 static int 704 mlxsw_sp2_span_entry_cpu_configure(struct mlxsw_sp_span_entry *span_entry, 705 struct mlxsw_sp_span_parms sparms) 706 { 707 /* Mirroring to the CPU port is like mirroring to any other physical 708 * port. Its local port is used instead of that of the physical port. 709 */ 710 return mlxsw_sp_span_entry_phys_configure(span_entry, sparms); 711 } 712 713 static void 714 mlxsw_sp2_span_entry_cpu_deconfigure(struct mlxsw_sp_span_entry *span_entry) 715 { 716 enum mlxsw_reg_mpat_span_type span_type; 717 718 span_type = MLXSW_REG_MPAT_SPAN_TYPE_LOCAL_ETH; 719 mlxsw_sp_span_entry_deconfigure_common(span_entry, span_type); 720 } 721 722 static const 723 struct mlxsw_sp_span_entry_ops mlxsw_sp2_span_entry_ops_cpu = { 724 .can_handle = mlxsw_sp2_span_cpu_can_handle, 725 .parms_set = mlxsw_sp2_span_entry_cpu_parms, 726 .configure = mlxsw_sp2_span_entry_cpu_configure, 727 .deconfigure = mlxsw_sp2_span_entry_cpu_deconfigure, 728 }; 729 730 static const 731 struct mlxsw_sp_span_entry_ops *mlxsw_sp2_span_entry_ops_arr[] = { 732 &mlxsw_sp2_span_entry_ops_cpu, 733 &mlxsw_sp_span_entry_ops_phys, 734 #if IS_ENABLED(CONFIG_NET_IPGRE) 735 &mlxsw_sp_span_entry_ops_gretap4, 736 #endif 737 #if IS_ENABLED(CONFIG_IPV6_GRE) 738 &mlxsw_sp_span_entry_ops_gretap6, 739 #endif 740 &mlxsw_sp_span_entry_ops_vlan, 741 }; 742 743 static int 744 mlxsw_sp_span_entry_nop_parms(struct mlxsw_sp *mlxsw_sp, 745 const struct net_device *to_dev, 746 struct mlxsw_sp_span_parms *sparmsp) 747 { 748 return mlxsw_sp_span_entry_unoffloadable(sparmsp); 749 } 750 751 static int 752 mlxsw_sp_span_entry_nop_configure(struct mlxsw_sp_span_entry *span_entry, 753 struct mlxsw_sp_span_parms sparms) 754 { 755 return 0; 756 } 757 758 static void 759 mlxsw_sp_span_entry_nop_deconfigure(struct mlxsw_sp_span_entry *span_entry) 760 { 761 } 762 763 static const struct mlxsw_sp_span_entry_ops mlxsw_sp_span_entry_ops_nop = { 764 .parms_set = mlxsw_sp_span_entry_nop_parms, 765 .configure = mlxsw_sp_span_entry_nop_configure, 766 .deconfigure = mlxsw_sp_span_entry_nop_deconfigure, 767 }; 768 769 static void 770 mlxsw_sp_span_entry_configure(struct mlxsw_sp *mlxsw_sp, 771 struct mlxsw_sp_span_entry *span_entry, 772 struct mlxsw_sp_span_parms sparms) 773 { 774 int err; 775 776 if (!sparms.dest_port) 777 goto set_parms; 778 779 if (sparms.dest_port->mlxsw_sp != mlxsw_sp) { 780 dev_err(mlxsw_sp->bus_info->dev, 781 "Cannot mirror to a port which belongs to a different mlxsw instance\n"); 782 sparms.dest_port = NULL; 783 goto set_parms; 784 } 785 786 err = span_entry->ops->configure(span_entry, sparms); 787 if (err) { 788 dev_err(mlxsw_sp->bus_info->dev, "Failed to offload mirror\n"); 789 sparms.dest_port = NULL; 790 goto set_parms; 791 } 792 793 set_parms: 794 span_entry->parms = sparms; 795 } 796 797 static void 798 mlxsw_sp_span_entry_deconfigure(struct mlxsw_sp_span_entry *span_entry) 799 { 800 if (span_entry->parms.dest_port) 801 span_entry->ops->deconfigure(span_entry); 802 } 803 804 static int mlxsw_sp_span_policer_id_base_set(struct mlxsw_sp_span *span, 805 u16 policer_id) 806 { 807 struct mlxsw_sp *mlxsw_sp = span->mlxsw_sp; 808 u16 policer_id_base; 809 int err; 810 811 /* Policers set on SPAN agents must be in the range of 812 * `policer_id_base .. policer_id_base + max_span_agents - 1`. If the 813 * base is set and the new policer is not within the range, then we 814 * must error out. 815 */ 816 if (refcount_read(&span->policer_id_base_ref_count)) { 817 if (policer_id < span->policer_id_base || 818 policer_id >= span->policer_id_base + span->entries_count) 819 return -EINVAL; 820 821 refcount_inc(&span->policer_id_base_ref_count); 822 return 0; 823 } 824 825 /* Base must be even. */ 826 policer_id_base = policer_id % 2 == 0 ? policer_id : policer_id - 1; 827 err = mlxsw_sp->span_ops->policer_id_base_set(mlxsw_sp, 828 policer_id_base); 829 if (err) 830 return err; 831 832 span->policer_id_base = policer_id_base; 833 refcount_set(&span->policer_id_base_ref_count, 1); 834 835 return 0; 836 } 837 838 static void mlxsw_sp_span_policer_id_base_unset(struct mlxsw_sp_span *span) 839 { 840 refcount_dec(&span->policer_id_base_ref_count); 841 } 842 843 static struct mlxsw_sp_span_entry * 844 mlxsw_sp_span_entry_create(struct mlxsw_sp *mlxsw_sp, 845 const struct net_device *to_dev, 846 const struct mlxsw_sp_span_entry_ops *ops, 847 struct mlxsw_sp_span_parms sparms) 848 { 849 struct mlxsw_sp_span_entry *span_entry = NULL; 850 int i; 851 852 /* find a free entry to use */ 853 for (i = 0; i < mlxsw_sp->span->entries_count; i++) { 854 if (!refcount_read(&mlxsw_sp->span->entries[i].ref_count)) { 855 span_entry = &mlxsw_sp->span->entries[i]; 856 break; 857 } 858 } 859 if (!span_entry) 860 return NULL; 861 862 if (sparms.policer_enable) { 863 int err; 864 865 err = mlxsw_sp_span_policer_id_base_set(mlxsw_sp->span, 866 sparms.policer_id); 867 if (err) 868 return NULL; 869 } 870 871 atomic_inc(&mlxsw_sp->span->active_entries_count); 872 span_entry->ops = ops; 873 refcount_set(&span_entry->ref_count, 1); 874 span_entry->to_dev = to_dev; 875 mlxsw_sp_span_entry_configure(mlxsw_sp, span_entry, sparms); 876 877 return span_entry; 878 } 879 880 static void mlxsw_sp_span_entry_destroy(struct mlxsw_sp *mlxsw_sp, 881 struct mlxsw_sp_span_entry *span_entry) 882 { 883 mlxsw_sp_span_entry_deconfigure(span_entry); 884 atomic_dec(&mlxsw_sp->span->active_entries_count); 885 if (span_entry->parms.policer_enable) 886 mlxsw_sp_span_policer_id_base_unset(mlxsw_sp->span); 887 } 888 889 struct mlxsw_sp_span_entry * 890 mlxsw_sp_span_entry_find_by_port(struct mlxsw_sp *mlxsw_sp, 891 const struct net_device *to_dev) 892 { 893 int i; 894 895 for (i = 0; i < mlxsw_sp->span->entries_count; i++) { 896 struct mlxsw_sp_span_entry *curr = &mlxsw_sp->span->entries[i]; 897 898 if (refcount_read(&curr->ref_count) && curr->to_dev == to_dev) 899 return curr; 900 } 901 return NULL; 902 } 903 904 void mlxsw_sp_span_entry_invalidate(struct mlxsw_sp *mlxsw_sp, 905 struct mlxsw_sp_span_entry *span_entry) 906 { 907 mlxsw_sp_span_entry_deconfigure(span_entry); 908 span_entry->ops = &mlxsw_sp_span_entry_ops_nop; 909 } 910 911 static struct mlxsw_sp_span_entry * 912 mlxsw_sp_span_entry_find_by_id(struct mlxsw_sp *mlxsw_sp, int span_id) 913 { 914 int i; 915 916 for (i = 0; i < mlxsw_sp->span->entries_count; i++) { 917 struct mlxsw_sp_span_entry *curr = &mlxsw_sp->span->entries[i]; 918 919 if (refcount_read(&curr->ref_count) && curr->id == span_id) 920 return curr; 921 } 922 return NULL; 923 } 924 925 static struct mlxsw_sp_span_entry * 926 mlxsw_sp_span_entry_find_by_parms(struct mlxsw_sp *mlxsw_sp, 927 const struct net_device *to_dev, 928 const struct mlxsw_sp_span_parms *sparms) 929 { 930 int i; 931 932 for (i = 0; i < mlxsw_sp->span->entries_count; i++) { 933 struct mlxsw_sp_span_entry *curr = &mlxsw_sp->span->entries[i]; 934 935 if (refcount_read(&curr->ref_count) && curr->to_dev == to_dev && 936 curr->parms.policer_enable == sparms->policer_enable && 937 curr->parms.policer_id == sparms->policer_id) 938 return curr; 939 } 940 return NULL; 941 } 942 943 static struct mlxsw_sp_span_entry * 944 mlxsw_sp_span_entry_get(struct mlxsw_sp *mlxsw_sp, 945 const struct net_device *to_dev, 946 const struct mlxsw_sp_span_entry_ops *ops, 947 struct mlxsw_sp_span_parms sparms) 948 { 949 struct mlxsw_sp_span_entry *span_entry; 950 951 span_entry = mlxsw_sp_span_entry_find_by_parms(mlxsw_sp, to_dev, 952 &sparms); 953 if (span_entry) { 954 /* Already exists, just take a reference */ 955 refcount_inc(&span_entry->ref_count); 956 return span_entry; 957 } 958 959 return mlxsw_sp_span_entry_create(mlxsw_sp, to_dev, ops, sparms); 960 } 961 962 static int mlxsw_sp_span_entry_put(struct mlxsw_sp *mlxsw_sp, 963 struct mlxsw_sp_span_entry *span_entry) 964 { 965 if (refcount_dec_and_test(&span_entry->ref_count)) 966 mlxsw_sp_span_entry_destroy(mlxsw_sp, span_entry); 967 return 0; 968 } 969 970 static u32 mlxsw_sp_span_buffsize_get(struct mlxsw_sp *mlxsw_sp, int mtu, 971 u32 speed) 972 { 973 u32 buffsize = mlxsw_sp->span_ops->buffsize_get(speed, mtu); 974 975 return mlxsw_sp_bytes_cells(mlxsw_sp, buffsize) + 1; 976 } 977 978 static int 979 mlxsw_sp_span_port_buffer_update(struct mlxsw_sp_port *mlxsw_sp_port, u16 mtu) 980 { 981 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; 982 char sbib_pl[MLXSW_REG_SBIB_LEN]; 983 u32 buffsize; 984 u32 speed; 985 int err; 986 987 err = mlxsw_sp_port_speed_get(mlxsw_sp_port, &speed); 988 if (err) 989 return err; 990 if (speed == SPEED_UNKNOWN) 991 speed = 0; 992 993 buffsize = mlxsw_sp_span_buffsize_get(mlxsw_sp, speed, mtu); 994 buffsize = mlxsw_sp_port_headroom_8x_adjust(mlxsw_sp_port, buffsize); 995 mlxsw_reg_sbib_pack(sbib_pl, mlxsw_sp_port->local_port, buffsize); 996 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sbib), sbib_pl); 997 } 998 999 static void mlxsw_sp_span_port_buffer_disable(struct mlxsw_sp *mlxsw_sp, 1000 u8 local_port) 1001 { 1002 char sbib_pl[MLXSW_REG_SBIB_LEN]; 1003 1004 mlxsw_reg_sbib_pack(sbib_pl, local_port, 0); 1005 mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sbib), sbib_pl); 1006 } 1007 1008 static struct mlxsw_sp_span_analyzed_port * 1009 mlxsw_sp_span_analyzed_port_find(struct mlxsw_sp_span *span, u8 local_port, 1010 bool ingress) 1011 { 1012 struct mlxsw_sp_span_analyzed_port *analyzed_port; 1013 1014 list_for_each_entry(analyzed_port, &span->analyzed_ports_list, list) { 1015 if (analyzed_port->local_port == local_port && 1016 analyzed_port->ingress == ingress) 1017 return analyzed_port; 1018 } 1019 1020 return NULL; 1021 } 1022 1023 int mlxsw_sp_span_port_mtu_update(struct mlxsw_sp_port *port, u16 mtu) 1024 { 1025 struct mlxsw_sp *mlxsw_sp = port->mlxsw_sp; 1026 int err = 0; 1027 1028 /* If port is egress mirrored, the shared buffer size should be 1029 * updated according to the mtu value 1030 */ 1031 mutex_lock(&mlxsw_sp->span->analyzed_ports_lock); 1032 1033 if (mlxsw_sp_span_analyzed_port_find(mlxsw_sp->span, port->local_port, 1034 false)) 1035 err = mlxsw_sp_span_port_buffer_update(port, mtu); 1036 1037 mutex_unlock(&mlxsw_sp->span->analyzed_ports_lock); 1038 1039 return err; 1040 } 1041 1042 void mlxsw_sp_span_speed_update_work(struct work_struct *work) 1043 { 1044 struct delayed_work *dwork = to_delayed_work(work); 1045 struct mlxsw_sp_port *mlxsw_sp_port; 1046 struct mlxsw_sp *mlxsw_sp; 1047 1048 mlxsw_sp_port = container_of(dwork, struct mlxsw_sp_port, 1049 span.speed_update_dw); 1050 1051 /* If port is egress mirrored, the shared buffer size should be 1052 * updated according to the speed value. 1053 */ 1054 mlxsw_sp = mlxsw_sp_port->mlxsw_sp; 1055 mutex_lock(&mlxsw_sp->span->analyzed_ports_lock); 1056 1057 if (mlxsw_sp_span_analyzed_port_find(mlxsw_sp->span, 1058 mlxsw_sp_port->local_port, false)) 1059 mlxsw_sp_span_port_buffer_update(mlxsw_sp_port, 1060 mlxsw_sp_port->dev->mtu); 1061 1062 mutex_unlock(&mlxsw_sp->span->analyzed_ports_lock); 1063 } 1064 1065 static const struct mlxsw_sp_span_entry_ops * 1066 mlxsw_sp_span_entry_ops(struct mlxsw_sp *mlxsw_sp, 1067 const struct net_device *to_dev) 1068 { 1069 struct mlxsw_sp_span *span = mlxsw_sp->span; 1070 size_t i; 1071 1072 for (i = 0; i < span->span_entry_ops_arr_size; ++i) 1073 if (span->span_entry_ops_arr[i]->can_handle(to_dev)) 1074 return span->span_entry_ops_arr[i]; 1075 1076 return NULL; 1077 } 1078 1079 static void mlxsw_sp_span_respin_work(struct work_struct *work) 1080 { 1081 struct mlxsw_sp_span *span; 1082 struct mlxsw_sp *mlxsw_sp; 1083 int i, err; 1084 1085 span = container_of(work, struct mlxsw_sp_span, work); 1086 mlxsw_sp = span->mlxsw_sp; 1087 1088 rtnl_lock(); 1089 for (i = 0; i < mlxsw_sp->span->entries_count; i++) { 1090 struct mlxsw_sp_span_entry *curr = &mlxsw_sp->span->entries[i]; 1091 struct mlxsw_sp_span_parms sparms = {NULL}; 1092 1093 if (!refcount_read(&curr->ref_count)) 1094 continue; 1095 1096 err = curr->ops->parms_set(mlxsw_sp, curr->to_dev, &sparms); 1097 if (err) 1098 continue; 1099 1100 if (memcmp(&sparms, &curr->parms, sizeof(sparms))) { 1101 mlxsw_sp_span_entry_deconfigure(curr); 1102 mlxsw_sp_span_entry_configure(mlxsw_sp, curr, sparms); 1103 } 1104 } 1105 rtnl_unlock(); 1106 } 1107 1108 void mlxsw_sp_span_respin(struct mlxsw_sp *mlxsw_sp) 1109 { 1110 if (atomic_read(&mlxsw_sp->span->active_entries_count) == 0) 1111 return; 1112 mlxsw_core_schedule_work(&mlxsw_sp->span->work); 1113 } 1114 1115 int mlxsw_sp_span_agent_get(struct mlxsw_sp *mlxsw_sp, int *p_span_id, 1116 const struct mlxsw_sp_span_agent_parms *parms) 1117 { 1118 const struct net_device *to_dev = parms->to_dev; 1119 const struct mlxsw_sp_span_entry_ops *ops; 1120 struct mlxsw_sp_span_entry *span_entry; 1121 struct mlxsw_sp_span_parms sparms; 1122 int err; 1123 1124 ASSERT_RTNL(); 1125 1126 ops = mlxsw_sp_span_entry_ops(mlxsw_sp, to_dev); 1127 if (!ops) { 1128 dev_err(mlxsw_sp->bus_info->dev, "Cannot mirror to requested destination\n"); 1129 return -EOPNOTSUPP; 1130 } 1131 1132 memset(&sparms, 0, sizeof(sparms)); 1133 err = ops->parms_set(mlxsw_sp, to_dev, &sparms); 1134 if (err) 1135 return err; 1136 1137 sparms.policer_id = parms->policer_id; 1138 sparms.policer_enable = parms->policer_enable; 1139 span_entry = mlxsw_sp_span_entry_get(mlxsw_sp, to_dev, ops, sparms); 1140 if (!span_entry) 1141 return -ENOBUFS; 1142 1143 *p_span_id = span_entry->id; 1144 1145 return 0; 1146 } 1147 1148 void mlxsw_sp_span_agent_put(struct mlxsw_sp *mlxsw_sp, int span_id) 1149 { 1150 struct mlxsw_sp_span_entry *span_entry; 1151 1152 ASSERT_RTNL(); 1153 1154 span_entry = mlxsw_sp_span_entry_find_by_id(mlxsw_sp, span_id); 1155 if (WARN_ON_ONCE(!span_entry)) 1156 return; 1157 1158 mlxsw_sp_span_entry_put(mlxsw_sp, span_entry); 1159 } 1160 1161 static struct mlxsw_sp_span_analyzed_port * 1162 mlxsw_sp_span_analyzed_port_create(struct mlxsw_sp_span *span, 1163 struct mlxsw_sp_port *mlxsw_sp_port, 1164 bool ingress) 1165 { 1166 struct mlxsw_sp_span_analyzed_port *analyzed_port; 1167 int err; 1168 1169 analyzed_port = kzalloc(sizeof(*analyzed_port), GFP_KERNEL); 1170 if (!analyzed_port) 1171 return ERR_PTR(-ENOMEM); 1172 1173 refcount_set(&analyzed_port->ref_count, 1); 1174 analyzed_port->local_port = mlxsw_sp_port->local_port; 1175 analyzed_port->ingress = ingress; 1176 list_add_tail(&analyzed_port->list, &span->analyzed_ports_list); 1177 1178 /* An egress mirror buffer should be allocated on the egress port which 1179 * does the mirroring. 1180 */ 1181 if (!ingress) { 1182 u16 mtu = mlxsw_sp_port->dev->mtu; 1183 1184 err = mlxsw_sp_span_port_buffer_update(mlxsw_sp_port, mtu); 1185 if (err) 1186 goto err_buffer_update; 1187 } 1188 1189 return analyzed_port; 1190 1191 err_buffer_update: 1192 list_del(&analyzed_port->list); 1193 kfree(analyzed_port); 1194 return ERR_PTR(err); 1195 } 1196 1197 static void 1198 mlxsw_sp_span_analyzed_port_destroy(struct mlxsw_sp_span *span, 1199 struct mlxsw_sp_span_analyzed_port * 1200 analyzed_port) 1201 { 1202 struct mlxsw_sp *mlxsw_sp = span->mlxsw_sp; 1203 1204 /* Remove egress mirror buffer now that port is no longer analyzed 1205 * at egress. 1206 */ 1207 if (!analyzed_port->ingress) 1208 mlxsw_sp_span_port_buffer_disable(mlxsw_sp, 1209 analyzed_port->local_port); 1210 1211 list_del(&analyzed_port->list); 1212 kfree(analyzed_port); 1213 } 1214 1215 int mlxsw_sp_span_analyzed_port_get(struct mlxsw_sp_port *mlxsw_sp_port, 1216 bool ingress) 1217 { 1218 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; 1219 struct mlxsw_sp_span_analyzed_port *analyzed_port; 1220 u8 local_port = mlxsw_sp_port->local_port; 1221 int err = 0; 1222 1223 mutex_lock(&mlxsw_sp->span->analyzed_ports_lock); 1224 1225 analyzed_port = mlxsw_sp_span_analyzed_port_find(mlxsw_sp->span, 1226 local_port, ingress); 1227 if (analyzed_port) { 1228 refcount_inc(&analyzed_port->ref_count); 1229 goto out_unlock; 1230 } 1231 1232 analyzed_port = mlxsw_sp_span_analyzed_port_create(mlxsw_sp->span, 1233 mlxsw_sp_port, 1234 ingress); 1235 if (IS_ERR(analyzed_port)) 1236 err = PTR_ERR(analyzed_port); 1237 1238 out_unlock: 1239 mutex_unlock(&mlxsw_sp->span->analyzed_ports_lock); 1240 return err; 1241 } 1242 1243 void mlxsw_sp_span_analyzed_port_put(struct mlxsw_sp_port *mlxsw_sp_port, 1244 bool ingress) 1245 { 1246 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; 1247 struct mlxsw_sp_span_analyzed_port *analyzed_port; 1248 u8 local_port = mlxsw_sp_port->local_port; 1249 1250 mutex_lock(&mlxsw_sp->span->analyzed_ports_lock); 1251 1252 analyzed_port = mlxsw_sp_span_analyzed_port_find(mlxsw_sp->span, 1253 local_port, ingress); 1254 if (WARN_ON_ONCE(!analyzed_port)) 1255 goto out_unlock; 1256 1257 if (!refcount_dec_and_test(&analyzed_port->ref_count)) 1258 goto out_unlock; 1259 1260 mlxsw_sp_span_analyzed_port_destroy(mlxsw_sp->span, analyzed_port); 1261 1262 out_unlock: 1263 mutex_unlock(&mlxsw_sp->span->analyzed_ports_lock); 1264 } 1265 1266 static int 1267 __mlxsw_sp_span_trigger_port_bind(struct mlxsw_sp_span *span, 1268 struct mlxsw_sp_span_trigger_entry * 1269 trigger_entry, bool enable) 1270 { 1271 char mpar_pl[MLXSW_REG_MPAR_LEN]; 1272 enum mlxsw_reg_mpar_i_e i_e; 1273 1274 switch (trigger_entry->trigger) { 1275 case MLXSW_SP_SPAN_TRIGGER_INGRESS: 1276 i_e = MLXSW_REG_MPAR_TYPE_INGRESS; 1277 break; 1278 case MLXSW_SP_SPAN_TRIGGER_EGRESS: 1279 i_e = MLXSW_REG_MPAR_TYPE_EGRESS; 1280 break; 1281 default: 1282 WARN_ON_ONCE(1); 1283 return -EINVAL; 1284 } 1285 1286 mlxsw_reg_mpar_pack(mpar_pl, trigger_entry->local_port, i_e, enable, 1287 trigger_entry->parms.span_id); 1288 return mlxsw_reg_write(span->mlxsw_sp->core, MLXSW_REG(mpar), mpar_pl); 1289 } 1290 1291 static int 1292 mlxsw_sp_span_trigger_port_bind(struct mlxsw_sp_span_trigger_entry * 1293 trigger_entry) 1294 { 1295 return __mlxsw_sp_span_trigger_port_bind(trigger_entry->span, 1296 trigger_entry, true); 1297 } 1298 1299 static void 1300 mlxsw_sp_span_trigger_port_unbind(struct mlxsw_sp_span_trigger_entry * 1301 trigger_entry) 1302 { 1303 __mlxsw_sp_span_trigger_port_bind(trigger_entry->span, trigger_entry, 1304 false); 1305 } 1306 1307 static bool 1308 mlxsw_sp_span_trigger_port_matches(struct mlxsw_sp_span_trigger_entry * 1309 trigger_entry, 1310 enum mlxsw_sp_span_trigger trigger, 1311 struct mlxsw_sp_port *mlxsw_sp_port) 1312 { 1313 return trigger_entry->trigger == trigger && 1314 trigger_entry->local_port == mlxsw_sp_port->local_port; 1315 } 1316 1317 static int 1318 mlxsw_sp_span_trigger_port_enable(struct mlxsw_sp_span_trigger_entry * 1319 trigger_entry, 1320 struct mlxsw_sp_port *mlxsw_sp_port, u8 tc) 1321 { 1322 /* Port trigger are enabled during binding. */ 1323 return 0; 1324 } 1325 1326 static void 1327 mlxsw_sp_span_trigger_port_disable(struct mlxsw_sp_span_trigger_entry * 1328 trigger_entry, 1329 struct mlxsw_sp_port *mlxsw_sp_port, u8 tc) 1330 { 1331 } 1332 1333 static const struct mlxsw_sp_span_trigger_ops 1334 mlxsw_sp_span_trigger_port_ops = { 1335 .bind = mlxsw_sp_span_trigger_port_bind, 1336 .unbind = mlxsw_sp_span_trigger_port_unbind, 1337 .matches = mlxsw_sp_span_trigger_port_matches, 1338 .enable = mlxsw_sp_span_trigger_port_enable, 1339 .disable = mlxsw_sp_span_trigger_port_disable, 1340 }; 1341 1342 static int 1343 mlxsw_sp1_span_trigger_global_bind(struct mlxsw_sp_span_trigger_entry * 1344 trigger_entry) 1345 { 1346 return -EOPNOTSUPP; 1347 } 1348 1349 static void 1350 mlxsw_sp1_span_trigger_global_unbind(struct mlxsw_sp_span_trigger_entry * 1351 trigger_entry) 1352 { 1353 } 1354 1355 static bool 1356 mlxsw_sp1_span_trigger_global_matches(struct mlxsw_sp_span_trigger_entry * 1357 trigger_entry, 1358 enum mlxsw_sp_span_trigger trigger, 1359 struct mlxsw_sp_port *mlxsw_sp_port) 1360 { 1361 WARN_ON_ONCE(1); 1362 return false; 1363 } 1364 1365 static int 1366 mlxsw_sp1_span_trigger_global_enable(struct mlxsw_sp_span_trigger_entry * 1367 trigger_entry, 1368 struct mlxsw_sp_port *mlxsw_sp_port, 1369 u8 tc) 1370 { 1371 return -EOPNOTSUPP; 1372 } 1373 1374 static void 1375 mlxsw_sp1_span_trigger_global_disable(struct mlxsw_sp_span_trigger_entry * 1376 trigger_entry, 1377 struct mlxsw_sp_port *mlxsw_sp_port, 1378 u8 tc) 1379 { 1380 } 1381 1382 static const struct mlxsw_sp_span_trigger_ops 1383 mlxsw_sp1_span_trigger_global_ops = { 1384 .bind = mlxsw_sp1_span_trigger_global_bind, 1385 .unbind = mlxsw_sp1_span_trigger_global_unbind, 1386 .matches = mlxsw_sp1_span_trigger_global_matches, 1387 .enable = mlxsw_sp1_span_trigger_global_enable, 1388 .disable = mlxsw_sp1_span_trigger_global_disable, 1389 }; 1390 1391 static const struct mlxsw_sp_span_trigger_ops * 1392 mlxsw_sp1_span_trigger_ops_arr[] = { 1393 [MLXSW_SP_SPAN_TRIGGER_TYPE_PORT] = &mlxsw_sp_span_trigger_port_ops, 1394 [MLXSW_SP_SPAN_TRIGGER_TYPE_GLOBAL] = 1395 &mlxsw_sp1_span_trigger_global_ops, 1396 }; 1397 1398 static int 1399 mlxsw_sp2_span_trigger_global_bind(struct mlxsw_sp_span_trigger_entry * 1400 trigger_entry) 1401 { 1402 struct mlxsw_sp *mlxsw_sp = trigger_entry->span->mlxsw_sp; 1403 enum mlxsw_reg_mpagr_trigger trigger; 1404 char mpagr_pl[MLXSW_REG_MPAGR_LEN]; 1405 1406 switch (trigger_entry->trigger) { 1407 case MLXSW_SP_SPAN_TRIGGER_TAIL_DROP: 1408 trigger = MLXSW_REG_MPAGR_TRIGGER_INGRESS_SHARED_BUFFER; 1409 break; 1410 case MLXSW_SP_SPAN_TRIGGER_EARLY_DROP: 1411 trigger = MLXSW_REG_MPAGR_TRIGGER_INGRESS_WRED; 1412 break; 1413 case MLXSW_SP_SPAN_TRIGGER_ECN: 1414 trigger = MLXSW_REG_MPAGR_TRIGGER_EGRESS_ECN; 1415 break; 1416 default: 1417 WARN_ON_ONCE(1); 1418 return -EINVAL; 1419 } 1420 1421 mlxsw_reg_mpagr_pack(mpagr_pl, trigger, trigger_entry->parms.span_id, 1422 1); 1423 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(mpagr), mpagr_pl); 1424 } 1425 1426 static void 1427 mlxsw_sp2_span_trigger_global_unbind(struct mlxsw_sp_span_trigger_entry * 1428 trigger_entry) 1429 { 1430 /* There is no unbinding for global triggers. The trigger should be 1431 * disabled on all ports by now. 1432 */ 1433 } 1434 1435 static bool 1436 mlxsw_sp2_span_trigger_global_matches(struct mlxsw_sp_span_trigger_entry * 1437 trigger_entry, 1438 enum mlxsw_sp_span_trigger trigger, 1439 struct mlxsw_sp_port *mlxsw_sp_port) 1440 { 1441 return trigger_entry->trigger == trigger; 1442 } 1443 1444 static int 1445 __mlxsw_sp2_span_trigger_global_enable(struct mlxsw_sp_span_trigger_entry * 1446 trigger_entry, 1447 struct mlxsw_sp_port *mlxsw_sp_port, 1448 u8 tc, bool enable) 1449 { 1450 struct mlxsw_sp *mlxsw_sp = trigger_entry->span->mlxsw_sp; 1451 char momte_pl[MLXSW_REG_MOMTE_LEN]; 1452 enum mlxsw_reg_momte_type type; 1453 int err; 1454 1455 switch (trigger_entry->trigger) { 1456 case MLXSW_SP_SPAN_TRIGGER_TAIL_DROP: 1457 type = MLXSW_REG_MOMTE_TYPE_SHARED_BUFFER_TCLASS; 1458 break; 1459 case MLXSW_SP_SPAN_TRIGGER_EARLY_DROP: 1460 type = MLXSW_REG_MOMTE_TYPE_WRED; 1461 break; 1462 case MLXSW_SP_SPAN_TRIGGER_ECN: 1463 type = MLXSW_REG_MOMTE_TYPE_ECN; 1464 break; 1465 default: 1466 WARN_ON_ONCE(1); 1467 return -EINVAL; 1468 } 1469 1470 /* Query existing configuration in order to only change the state of 1471 * the specified traffic class. 1472 */ 1473 mlxsw_reg_momte_pack(momte_pl, mlxsw_sp_port->local_port, type); 1474 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(momte), momte_pl); 1475 if (err) 1476 return err; 1477 1478 mlxsw_reg_momte_tclass_en_set(momte_pl, tc, enable); 1479 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(momte), momte_pl); 1480 } 1481 1482 static int 1483 mlxsw_sp2_span_trigger_global_enable(struct mlxsw_sp_span_trigger_entry * 1484 trigger_entry, 1485 struct mlxsw_sp_port *mlxsw_sp_port, 1486 u8 tc) 1487 { 1488 return __mlxsw_sp2_span_trigger_global_enable(trigger_entry, 1489 mlxsw_sp_port, tc, true); 1490 } 1491 1492 static void 1493 mlxsw_sp2_span_trigger_global_disable(struct mlxsw_sp_span_trigger_entry * 1494 trigger_entry, 1495 struct mlxsw_sp_port *mlxsw_sp_port, 1496 u8 tc) 1497 { 1498 __mlxsw_sp2_span_trigger_global_enable(trigger_entry, mlxsw_sp_port, tc, 1499 false); 1500 } 1501 1502 static const struct mlxsw_sp_span_trigger_ops 1503 mlxsw_sp2_span_trigger_global_ops = { 1504 .bind = mlxsw_sp2_span_trigger_global_bind, 1505 .unbind = mlxsw_sp2_span_trigger_global_unbind, 1506 .matches = mlxsw_sp2_span_trigger_global_matches, 1507 .enable = mlxsw_sp2_span_trigger_global_enable, 1508 .disable = mlxsw_sp2_span_trigger_global_disable, 1509 }; 1510 1511 static const struct mlxsw_sp_span_trigger_ops * 1512 mlxsw_sp2_span_trigger_ops_arr[] = { 1513 [MLXSW_SP_SPAN_TRIGGER_TYPE_PORT] = &mlxsw_sp_span_trigger_port_ops, 1514 [MLXSW_SP_SPAN_TRIGGER_TYPE_GLOBAL] = 1515 &mlxsw_sp2_span_trigger_global_ops, 1516 }; 1517 1518 static void 1519 mlxsw_sp_span_trigger_ops_set(struct mlxsw_sp_span_trigger_entry *trigger_entry) 1520 { 1521 struct mlxsw_sp_span *span = trigger_entry->span; 1522 enum mlxsw_sp_span_trigger_type type; 1523 1524 switch (trigger_entry->trigger) { 1525 case MLXSW_SP_SPAN_TRIGGER_INGRESS: /* fall-through */ 1526 case MLXSW_SP_SPAN_TRIGGER_EGRESS: 1527 type = MLXSW_SP_SPAN_TRIGGER_TYPE_PORT; 1528 break; 1529 case MLXSW_SP_SPAN_TRIGGER_TAIL_DROP: /* fall-through */ 1530 case MLXSW_SP_SPAN_TRIGGER_EARLY_DROP: /* fall-through */ 1531 case MLXSW_SP_SPAN_TRIGGER_ECN: 1532 type = MLXSW_SP_SPAN_TRIGGER_TYPE_GLOBAL; 1533 break; 1534 default: 1535 WARN_ON_ONCE(1); 1536 return; 1537 } 1538 1539 trigger_entry->ops = span->span_trigger_ops_arr[type]; 1540 } 1541 1542 static struct mlxsw_sp_span_trigger_entry * 1543 mlxsw_sp_span_trigger_entry_create(struct mlxsw_sp_span *span, 1544 enum mlxsw_sp_span_trigger trigger, 1545 struct mlxsw_sp_port *mlxsw_sp_port, 1546 const struct mlxsw_sp_span_trigger_parms 1547 *parms) 1548 { 1549 struct mlxsw_sp_span_trigger_entry *trigger_entry; 1550 int err; 1551 1552 trigger_entry = kzalloc(sizeof(*trigger_entry), GFP_KERNEL); 1553 if (!trigger_entry) 1554 return ERR_PTR(-ENOMEM); 1555 1556 refcount_set(&trigger_entry->ref_count, 1); 1557 trigger_entry->local_port = mlxsw_sp_port ? mlxsw_sp_port->local_port : 1558 0; 1559 trigger_entry->trigger = trigger; 1560 memcpy(&trigger_entry->parms, parms, sizeof(trigger_entry->parms)); 1561 trigger_entry->span = span; 1562 mlxsw_sp_span_trigger_ops_set(trigger_entry); 1563 list_add_tail(&trigger_entry->list, &span->trigger_entries_list); 1564 1565 err = trigger_entry->ops->bind(trigger_entry); 1566 if (err) 1567 goto err_trigger_entry_bind; 1568 1569 return trigger_entry; 1570 1571 err_trigger_entry_bind: 1572 list_del(&trigger_entry->list); 1573 kfree(trigger_entry); 1574 return ERR_PTR(err); 1575 } 1576 1577 static void 1578 mlxsw_sp_span_trigger_entry_destroy(struct mlxsw_sp_span *span, 1579 struct mlxsw_sp_span_trigger_entry * 1580 trigger_entry) 1581 { 1582 trigger_entry->ops->unbind(trigger_entry); 1583 list_del(&trigger_entry->list); 1584 kfree(trigger_entry); 1585 } 1586 1587 static struct mlxsw_sp_span_trigger_entry * 1588 mlxsw_sp_span_trigger_entry_find(struct mlxsw_sp_span *span, 1589 enum mlxsw_sp_span_trigger trigger, 1590 struct mlxsw_sp_port *mlxsw_sp_port) 1591 { 1592 struct mlxsw_sp_span_trigger_entry *trigger_entry; 1593 1594 list_for_each_entry(trigger_entry, &span->trigger_entries_list, list) { 1595 if (trigger_entry->ops->matches(trigger_entry, trigger, 1596 mlxsw_sp_port)) 1597 return trigger_entry; 1598 } 1599 1600 return NULL; 1601 } 1602 1603 int mlxsw_sp_span_agent_bind(struct mlxsw_sp *mlxsw_sp, 1604 enum mlxsw_sp_span_trigger trigger, 1605 struct mlxsw_sp_port *mlxsw_sp_port, 1606 const struct mlxsw_sp_span_trigger_parms *parms) 1607 { 1608 struct mlxsw_sp_span_trigger_entry *trigger_entry; 1609 int err = 0; 1610 1611 ASSERT_RTNL(); 1612 1613 if (!mlxsw_sp_span_entry_find_by_id(mlxsw_sp, parms->span_id)) 1614 return -EINVAL; 1615 1616 trigger_entry = mlxsw_sp_span_trigger_entry_find(mlxsw_sp->span, 1617 trigger, 1618 mlxsw_sp_port); 1619 if (trigger_entry) { 1620 if (trigger_entry->parms.span_id != parms->span_id) 1621 return -EINVAL; 1622 refcount_inc(&trigger_entry->ref_count); 1623 goto out; 1624 } 1625 1626 trigger_entry = mlxsw_sp_span_trigger_entry_create(mlxsw_sp->span, 1627 trigger, 1628 mlxsw_sp_port, 1629 parms); 1630 if (IS_ERR(trigger_entry)) 1631 err = PTR_ERR(trigger_entry); 1632 1633 out: 1634 return err; 1635 } 1636 1637 void mlxsw_sp_span_agent_unbind(struct mlxsw_sp *mlxsw_sp, 1638 enum mlxsw_sp_span_trigger trigger, 1639 struct mlxsw_sp_port *mlxsw_sp_port, 1640 const struct mlxsw_sp_span_trigger_parms *parms) 1641 { 1642 struct mlxsw_sp_span_trigger_entry *trigger_entry; 1643 1644 ASSERT_RTNL(); 1645 1646 if (WARN_ON_ONCE(!mlxsw_sp_span_entry_find_by_id(mlxsw_sp, 1647 parms->span_id))) 1648 return; 1649 1650 trigger_entry = mlxsw_sp_span_trigger_entry_find(mlxsw_sp->span, 1651 trigger, 1652 mlxsw_sp_port); 1653 if (WARN_ON_ONCE(!trigger_entry)) 1654 return; 1655 1656 if (!refcount_dec_and_test(&trigger_entry->ref_count)) 1657 return; 1658 1659 mlxsw_sp_span_trigger_entry_destroy(mlxsw_sp->span, trigger_entry); 1660 } 1661 1662 int mlxsw_sp_span_trigger_enable(struct mlxsw_sp_port *mlxsw_sp_port, 1663 enum mlxsw_sp_span_trigger trigger, u8 tc) 1664 { 1665 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; 1666 struct mlxsw_sp_span_trigger_entry *trigger_entry; 1667 1668 ASSERT_RTNL(); 1669 1670 trigger_entry = mlxsw_sp_span_trigger_entry_find(mlxsw_sp->span, 1671 trigger, 1672 mlxsw_sp_port); 1673 if (WARN_ON_ONCE(!trigger_entry)) 1674 return -EINVAL; 1675 1676 return trigger_entry->ops->enable(trigger_entry, mlxsw_sp_port, tc); 1677 } 1678 1679 void mlxsw_sp_span_trigger_disable(struct mlxsw_sp_port *mlxsw_sp_port, 1680 enum mlxsw_sp_span_trigger trigger, u8 tc) 1681 { 1682 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp; 1683 struct mlxsw_sp_span_trigger_entry *trigger_entry; 1684 1685 ASSERT_RTNL(); 1686 1687 trigger_entry = mlxsw_sp_span_trigger_entry_find(mlxsw_sp->span, 1688 trigger, 1689 mlxsw_sp_port); 1690 if (WARN_ON_ONCE(!trigger_entry)) 1691 return; 1692 1693 return trigger_entry->ops->disable(trigger_entry, mlxsw_sp_port, tc); 1694 } 1695 1696 static int mlxsw_sp1_span_init(struct mlxsw_sp *mlxsw_sp) 1697 { 1698 size_t arr_size = ARRAY_SIZE(mlxsw_sp1_span_entry_ops_arr); 1699 1700 /* Must be first to avoid NULL pointer dereference by subsequent 1701 * can_handle() callbacks. 1702 */ 1703 if (WARN_ON(mlxsw_sp1_span_entry_ops_arr[0] != 1704 &mlxsw_sp1_span_entry_ops_cpu)) 1705 return -EINVAL; 1706 1707 mlxsw_sp->span->span_trigger_ops_arr = mlxsw_sp1_span_trigger_ops_arr; 1708 mlxsw_sp->span->span_entry_ops_arr = mlxsw_sp1_span_entry_ops_arr; 1709 mlxsw_sp->span->span_entry_ops_arr_size = arr_size; 1710 1711 return 0; 1712 } 1713 1714 static u32 mlxsw_sp1_span_buffsize_get(int mtu, u32 speed) 1715 { 1716 return mtu * 5 / 2; 1717 } 1718 1719 static int mlxsw_sp1_span_policer_id_base_set(struct mlxsw_sp *mlxsw_sp, 1720 u16 policer_id_base) 1721 { 1722 return -EOPNOTSUPP; 1723 } 1724 1725 const struct mlxsw_sp_span_ops mlxsw_sp1_span_ops = { 1726 .init = mlxsw_sp1_span_init, 1727 .buffsize_get = mlxsw_sp1_span_buffsize_get, 1728 .policer_id_base_set = mlxsw_sp1_span_policer_id_base_set, 1729 }; 1730 1731 static int mlxsw_sp2_span_init(struct mlxsw_sp *mlxsw_sp) 1732 { 1733 size_t arr_size = ARRAY_SIZE(mlxsw_sp2_span_entry_ops_arr); 1734 1735 /* Must be first to avoid NULL pointer dereference by subsequent 1736 * can_handle() callbacks. 1737 */ 1738 if (WARN_ON(mlxsw_sp2_span_entry_ops_arr[0] != 1739 &mlxsw_sp2_span_entry_ops_cpu)) 1740 return -EINVAL; 1741 1742 mlxsw_sp->span->span_trigger_ops_arr = mlxsw_sp2_span_trigger_ops_arr; 1743 mlxsw_sp->span->span_entry_ops_arr = mlxsw_sp2_span_entry_ops_arr; 1744 mlxsw_sp->span->span_entry_ops_arr_size = arr_size; 1745 1746 return 0; 1747 } 1748 1749 #define MLXSW_SP2_SPAN_EG_MIRROR_BUFFER_FACTOR 38 1750 #define MLXSW_SP3_SPAN_EG_MIRROR_BUFFER_FACTOR 50 1751 1752 static u32 __mlxsw_sp_span_buffsize_get(int mtu, u32 speed, u32 buffer_factor) 1753 { 1754 return 3 * mtu + buffer_factor * speed / 1000; 1755 } 1756 1757 static u32 mlxsw_sp2_span_buffsize_get(int mtu, u32 speed) 1758 { 1759 int factor = MLXSW_SP2_SPAN_EG_MIRROR_BUFFER_FACTOR; 1760 1761 return __mlxsw_sp_span_buffsize_get(mtu, speed, factor); 1762 } 1763 1764 static int mlxsw_sp2_span_policer_id_base_set(struct mlxsw_sp *mlxsw_sp, 1765 u16 policer_id_base) 1766 { 1767 char mogcr_pl[MLXSW_REG_MOGCR_LEN]; 1768 int err; 1769 1770 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(mogcr), mogcr_pl); 1771 if (err) 1772 return err; 1773 1774 mlxsw_reg_mogcr_mirroring_pid_base_set(mogcr_pl, policer_id_base); 1775 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(mogcr), mogcr_pl); 1776 } 1777 1778 const struct mlxsw_sp_span_ops mlxsw_sp2_span_ops = { 1779 .init = mlxsw_sp2_span_init, 1780 .buffsize_get = mlxsw_sp2_span_buffsize_get, 1781 .policer_id_base_set = mlxsw_sp2_span_policer_id_base_set, 1782 }; 1783 1784 static u32 mlxsw_sp3_span_buffsize_get(int mtu, u32 speed) 1785 { 1786 int factor = MLXSW_SP3_SPAN_EG_MIRROR_BUFFER_FACTOR; 1787 1788 return __mlxsw_sp_span_buffsize_get(mtu, speed, factor); 1789 } 1790 1791 const struct mlxsw_sp_span_ops mlxsw_sp3_span_ops = { 1792 .init = mlxsw_sp2_span_init, 1793 .buffsize_get = mlxsw_sp3_span_buffsize_get, 1794 .policer_id_base_set = mlxsw_sp2_span_policer_id_base_set, 1795 }; 1796