1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Copyright (c) 2016 Mellanox Technologies. All rights reserved. 4 * Copyright (c) 2016 Jiri Pirko <jiri@mellanox.com> 5 */ 6 7 #include "devl_internal.h" 8 9 #define DEVLINK_PORT_FN_CAPS_VALID_MASK \ 10 (_BITUL(__DEVLINK_PORT_FN_ATTR_CAPS_MAX) - 1) 11 12 static const struct nla_policy devlink_function_nl_policy[DEVLINK_PORT_FUNCTION_ATTR_MAX + 1] = { 13 [DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR] = { .type = NLA_BINARY }, 14 [DEVLINK_PORT_FN_ATTR_STATE] = 15 NLA_POLICY_RANGE(NLA_U8, DEVLINK_PORT_FN_STATE_INACTIVE, 16 DEVLINK_PORT_FN_STATE_ACTIVE), 17 [DEVLINK_PORT_FN_ATTR_CAPS] = 18 NLA_POLICY_BITFIELD32(DEVLINK_PORT_FN_CAPS_VALID_MASK), 19 }; 20 21 #define ASSERT_DEVLINK_PORT_REGISTERED(devlink_port) \ 22 WARN_ON_ONCE(!(devlink_port)->registered) 23 #define ASSERT_DEVLINK_PORT_NOT_REGISTERED(devlink_port) \ 24 WARN_ON_ONCE((devlink_port)->registered) 25 26 struct devlink_port *devlink_port_get_by_index(struct devlink *devlink, 27 unsigned int port_index) 28 { 29 return xa_load(&devlink->ports, port_index); 30 } 31 32 struct devlink_port *devlink_port_get_from_attrs(struct devlink *devlink, 33 struct nlattr **attrs) 34 { 35 if (attrs[DEVLINK_ATTR_PORT_INDEX]) { 36 u32 port_index = nla_get_u32(attrs[DEVLINK_ATTR_PORT_INDEX]); 37 struct devlink_port *devlink_port; 38 39 devlink_port = devlink_port_get_by_index(devlink, port_index); 40 if (!devlink_port) 41 return ERR_PTR(-ENODEV); 42 return devlink_port; 43 } 44 return ERR_PTR(-EINVAL); 45 } 46 47 struct devlink_port *devlink_port_get_from_info(struct devlink *devlink, 48 struct genl_info *info) 49 { 50 return devlink_port_get_from_attrs(devlink, info->attrs); 51 } 52 53 static void devlink_port_fn_cap_fill(struct nla_bitfield32 *caps, 54 u32 cap, bool is_enable) 55 { 56 caps->selector |= cap; 57 if (is_enable) 58 caps->value |= cap; 59 } 60 61 static int devlink_port_fn_roce_fill(struct devlink_port *devlink_port, 62 struct nla_bitfield32 *caps, 63 struct netlink_ext_ack *extack) 64 { 65 bool is_enable; 66 int err; 67 68 if (!devlink_port->ops->port_fn_roce_get) 69 return 0; 70 71 err = devlink_port->ops->port_fn_roce_get(devlink_port, &is_enable, 72 extack); 73 if (err) { 74 if (err == -EOPNOTSUPP) 75 return 0; 76 return err; 77 } 78 79 devlink_port_fn_cap_fill(caps, DEVLINK_PORT_FN_CAP_ROCE, is_enable); 80 return 0; 81 } 82 83 static int devlink_port_fn_migratable_fill(struct devlink_port *devlink_port, 84 struct nla_bitfield32 *caps, 85 struct netlink_ext_ack *extack) 86 { 87 bool is_enable; 88 int err; 89 90 if (!devlink_port->ops->port_fn_migratable_get || 91 devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_PCI_VF) 92 return 0; 93 94 err = devlink_port->ops->port_fn_migratable_get(devlink_port, 95 &is_enable, extack); 96 if (err) { 97 if (err == -EOPNOTSUPP) 98 return 0; 99 return err; 100 } 101 102 devlink_port_fn_cap_fill(caps, DEVLINK_PORT_FN_CAP_MIGRATABLE, is_enable); 103 return 0; 104 } 105 106 static int devlink_port_fn_ipsec_crypto_fill(struct devlink_port *devlink_port, 107 struct nla_bitfield32 *caps, 108 struct netlink_ext_ack *extack) 109 { 110 bool is_enable; 111 int err; 112 113 if (!devlink_port->ops->port_fn_ipsec_crypto_get || 114 devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_PCI_VF) 115 return 0; 116 117 err = devlink_port->ops->port_fn_ipsec_crypto_get(devlink_port, &is_enable, extack); 118 if (err) { 119 if (err == -EOPNOTSUPP) 120 return 0; 121 return err; 122 } 123 124 devlink_port_fn_cap_fill(caps, DEVLINK_PORT_FN_CAP_IPSEC_CRYPTO, is_enable); 125 return 0; 126 } 127 128 static int devlink_port_fn_ipsec_packet_fill(struct devlink_port *devlink_port, 129 struct nla_bitfield32 *caps, 130 struct netlink_ext_ack *extack) 131 { 132 bool is_enable; 133 int err; 134 135 if (!devlink_port->ops->port_fn_ipsec_packet_get || 136 devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_PCI_VF) 137 return 0; 138 139 err = devlink_port->ops->port_fn_ipsec_packet_get(devlink_port, &is_enable, extack); 140 if (err) { 141 if (err == -EOPNOTSUPP) 142 return 0; 143 return err; 144 } 145 146 devlink_port_fn_cap_fill(caps, DEVLINK_PORT_FN_CAP_IPSEC_PACKET, is_enable); 147 return 0; 148 } 149 150 static int devlink_port_fn_caps_fill(struct devlink_port *devlink_port, 151 struct sk_buff *msg, 152 struct netlink_ext_ack *extack, 153 bool *msg_updated) 154 { 155 struct nla_bitfield32 caps = {}; 156 int err; 157 158 err = devlink_port_fn_roce_fill(devlink_port, &caps, extack); 159 if (err) 160 return err; 161 162 err = devlink_port_fn_migratable_fill(devlink_port, &caps, extack); 163 if (err) 164 return err; 165 166 err = devlink_port_fn_ipsec_crypto_fill(devlink_port, &caps, extack); 167 if (err) 168 return err; 169 170 err = devlink_port_fn_ipsec_packet_fill(devlink_port, &caps, extack); 171 if (err) 172 return err; 173 174 if (!caps.selector) 175 return 0; 176 err = nla_put_bitfield32(msg, DEVLINK_PORT_FN_ATTR_CAPS, caps.value, 177 caps.selector); 178 if (err) 179 return err; 180 181 *msg_updated = true; 182 return 0; 183 } 184 185 int devlink_nl_port_handle_fill(struct sk_buff *msg, struct devlink_port *devlink_port) 186 { 187 if (devlink_nl_put_handle(msg, devlink_port->devlink)) 188 return -EMSGSIZE; 189 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index)) 190 return -EMSGSIZE; 191 return 0; 192 } 193 194 size_t devlink_nl_port_handle_size(struct devlink_port *devlink_port) 195 { 196 struct devlink *devlink = devlink_port->devlink; 197 198 return nla_total_size(strlen(devlink->dev->bus->name) + 1) /* DEVLINK_ATTR_BUS_NAME */ 199 + nla_total_size(strlen(dev_name(devlink->dev)) + 1) /* DEVLINK_ATTR_DEV_NAME */ 200 + nla_total_size(4); /* DEVLINK_ATTR_PORT_INDEX */ 201 } 202 203 static int devlink_nl_port_attrs_put(struct sk_buff *msg, 204 struct devlink_port *devlink_port) 205 { 206 struct devlink_port_attrs *attrs = &devlink_port->attrs; 207 208 if (!devlink_port->attrs_set) 209 return 0; 210 if (attrs->lanes) { 211 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_LANES, attrs->lanes)) 212 return -EMSGSIZE; 213 } 214 if (nla_put_u8(msg, DEVLINK_ATTR_PORT_SPLITTABLE, attrs->splittable)) 215 return -EMSGSIZE; 216 if (nla_put_u16(msg, DEVLINK_ATTR_PORT_FLAVOUR, attrs->flavour)) 217 return -EMSGSIZE; 218 switch (devlink_port->attrs.flavour) { 219 case DEVLINK_PORT_FLAVOUR_PCI_PF: 220 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_CONTROLLER_NUMBER, 221 attrs->pci_pf.controller) || 222 nla_put_u16(msg, DEVLINK_ATTR_PORT_PCI_PF_NUMBER, attrs->pci_pf.pf)) 223 return -EMSGSIZE; 224 if (nla_put_u8(msg, DEVLINK_ATTR_PORT_EXTERNAL, attrs->pci_pf.external)) 225 return -EMSGSIZE; 226 break; 227 case DEVLINK_PORT_FLAVOUR_PCI_VF: 228 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_CONTROLLER_NUMBER, 229 attrs->pci_vf.controller) || 230 nla_put_u16(msg, DEVLINK_ATTR_PORT_PCI_PF_NUMBER, attrs->pci_vf.pf) || 231 nla_put_u16(msg, DEVLINK_ATTR_PORT_PCI_VF_NUMBER, attrs->pci_vf.vf)) 232 return -EMSGSIZE; 233 if (nla_put_u8(msg, DEVLINK_ATTR_PORT_EXTERNAL, attrs->pci_vf.external)) 234 return -EMSGSIZE; 235 break; 236 case DEVLINK_PORT_FLAVOUR_PCI_SF: 237 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_CONTROLLER_NUMBER, 238 attrs->pci_sf.controller) || 239 nla_put_u16(msg, DEVLINK_ATTR_PORT_PCI_PF_NUMBER, 240 attrs->pci_sf.pf) || 241 nla_put_u32(msg, DEVLINK_ATTR_PORT_PCI_SF_NUMBER, 242 attrs->pci_sf.sf)) 243 return -EMSGSIZE; 244 break; 245 case DEVLINK_PORT_FLAVOUR_PHYSICAL: 246 case DEVLINK_PORT_FLAVOUR_CPU: 247 case DEVLINK_PORT_FLAVOUR_DSA: 248 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_NUMBER, 249 attrs->phys.port_number)) 250 return -EMSGSIZE; 251 if (!attrs->split) 252 return 0; 253 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_SPLIT_GROUP, 254 attrs->phys.port_number)) 255 return -EMSGSIZE; 256 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_SPLIT_SUBPORT_NUMBER, 257 attrs->phys.split_subport_number)) 258 return -EMSGSIZE; 259 break; 260 default: 261 break; 262 } 263 return 0; 264 } 265 266 static int devlink_port_fn_hw_addr_fill(struct devlink_port *port, 267 struct sk_buff *msg, 268 struct netlink_ext_ack *extack, 269 bool *msg_updated) 270 { 271 u8 hw_addr[MAX_ADDR_LEN]; 272 int hw_addr_len; 273 int err; 274 275 if (!port->ops->port_fn_hw_addr_get) 276 return 0; 277 278 err = port->ops->port_fn_hw_addr_get(port, hw_addr, &hw_addr_len, 279 extack); 280 if (err) { 281 if (err == -EOPNOTSUPP) 282 return 0; 283 return err; 284 } 285 err = nla_put(msg, DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR, hw_addr_len, hw_addr); 286 if (err) 287 return err; 288 *msg_updated = true; 289 return 0; 290 } 291 292 static bool 293 devlink_port_fn_state_valid(enum devlink_port_fn_state state) 294 { 295 return state == DEVLINK_PORT_FN_STATE_INACTIVE || 296 state == DEVLINK_PORT_FN_STATE_ACTIVE; 297 } 298 299 static bool 300 devlink_port_fn_opstate_valid(enum devlink_port_fn_opstate opstate) 301 { 302 return opstate == DEVLINK_PORT_FN_OPSTATE_DETACHED || 303 opstate == DEVLINK_PORT_FN_OPSTATE_ATTACHED; 304 } 305 306 static int devlink_port_fn_state_fill(struct devlink_port *port, 307 struct sk_buff *msg, 308 struct netlink_ext_ack *extack, 309 bool *msg_updated) 310 { 311 enum devlink_port_fn_opstate opstate; 312 enum devlink_port_fn_state state; 313 int err; 314 315 if (!port->ops->port_fn_state_get) 316 return 0; 317 318 err = port->ops->port_fn_state_get(port, &state, &opstate, extack); 319 if (err) { 320 if (err == -EOPNOTSUPP) 321 return 0; 322 return err; 323 } 324 if (!devlink_port_fn_state_valid(state)) { 325 WARN_ON_ONCE(1); 326 NL_SET_ERR_MSG(extack, "Invalid state read from driver"); 327 return -EINVAL; 328 } 329 if (!devlink_port_fn_opstate_valid(opstate)) { 330 WARN_ON_ONCE(1); 331 NL_SET_ERR_MSG(extack, "Invalid operational state read from driver"); 332 return -EINVAL; 333 } 334 if (nla_put_u8(msg, DEVLINK_PORT_FN_ATTR_STATE, state) || 335 nla_put_u8(msg, DEVLINK_PORT_FN_ATTR_OPSTATE, opstate)) 336 return -EMSGSIZE; 337 *msg_updated = true; 338 return 0; 339 } 340 341 static int 342 devlink_port_fn_mig_set(struct devlink_port *devlink_port, bool enable, 343 struct netlink_ext_ack *extack) 344 { 345 return devlink_port->ops->port_fn_migratable_set(devlink_port, enable, 346 extack); 347 } 348 349 static int 350 devlink_port_fn_roce_set(struct devlink_port *devlink_port, bool enable, 351 struct netlink_ext_ack *extack) 352 { 353 return devlink_port->ops->port_fn_roce_set(devlink_port, enable, 354 extack); 355 } 356 357 static int 358 devlink_port_fn_ipsec_crypto_set(struct devlink_port *devlink_port, bool enable, 359 struct netlink_ext_ack *extack) 360 { 361 return devlink_port->ops->port_fn_ipsec_crypto_set(devlink_port, enable, extack); 362 } 363 364 static int 365 devlink_port_fn_ipsec_packet_set(struct devlink_port *devlink_port, bool enable, 366 struct netlink_ext_ack *extack) 367 { 368 return devlink_port->ops->port_fn_ipsec_packet_set(devlink_port, enable, extack); 369 } 370 371 static int devlink_port_fn_caps_set(struct devlink_port *devlink_port, 372 const struct nlattr *attr, 373 struct netlink_ext_ack *extack) 374 { 375 struct nla_bitfield32 caps; 376 u32 caps_value; 377 int err; 378 379 caps = nla_get_bitfield32(attr); 380 caps_value = caps.value & caps.selector; 381 if (caps.selector & DEVLINK_PORT_FN_CAP_ROCE) { 382 err = devlink_port_fn_roce_set(devlink_port, 383 caps_value & DEVLINK_PORT_FN_CAP_ROCE, 384 extack); 385 if (err) 386 return err; 387 } 388 if (caps.selector & DEVLINK_PORT_FN_CAP_MIGRATABLE) { 389 err = devlink_port_fn_mig_set(devlink_port, caps_value & 390 DEVLINK_PORT_FN_CAP_MIGRATABLE, 391 extack); 392 if (err) 393 return err; 394 } 395 if (caps.selector & DEVLINK_PORT_FN_CAP_IPSEC_CRYPTO) { 396 err = devlink_port_fn_ipsec_crypto_set(devlink_port, caps_value & 397 DEVLINK_PORT_FN_CAP_IPSEC_CRYPTO, 398 extack); 399 if (err) 400 return err; 401 } 402 if (caps.selector & DEVLINK_PORT_FN_CAP_IPSEC_PACKET) { 403 err = devlink_port_fn_ipsec_packet_set(devlink_port, caps_value & 404 DEVLINK_PORT_FN_CAP_IPSEC_PACKET, 405 extack); 406 if (err) 407 return err; 408 } 409 return 0; 410 } 411 412 static int 413 devlink_nl_port_function_attrs_put(struct sk_buff *msg, struct devlink_port *port, 414 struct netlink_ext_ack *extack) 415 { 416 struct nlattr *function_attr; 417 bool msg_updated = false; 418 int err; 419 420 function_attr = nla_nest_start_noflag(msg, DEVLINK_ATTR_PORT_FUNCTION); 421 if (!function_attr) 422 return -EMSGSIZE; 423 424 err = devlink_port_fn_hw_addr_fill(port, msg, extack, &msg_updated); 425 if (err) 426 goto out; 427 err = devlink_port_fn_caps_fill(port, msg, extack, &msg_updated); 428 if (err) 429 goto out; 430 err = devlink_port_fn_state_fill(port, msg, extack, &msg_updated); 431 out: 432 if (err || !msg_updated) 433 nla_nest_cancel(msg, function_attr); 434 else 435 nla_nest_end(msg, function_attr); 436 return err; 437 } 438 439 static int devlink_nl_port_fill(struct sk_buff *msg, 440 struct devlink_port *devlink_port, 441 enum devlink_command cmd, u32 portid, u32 seq, 442 int flags, struct netlink_ext_ack *extack) 443 { 444 struct devlink *devlink = devlink_port->devlink; 445 void *hdr; 446 447 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd); 448 if (!hdr) 449 return -EMSGSIZE; 450 451 if (devlink_nl_put_handle(msg, devlink)) 452 goto nla_put_failure; 453 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index)) 454 goto nla_put_failure; 455 456 spin_lock_bh(&devlink_port->type_lock); 457 if (nla_put_u16(msg, DEVLINK_ATTR_PORT_TYPE, devlink_port->type)) 458 goto nla_put_failure_type_locked; 459 if (devlink_port->desired_type != DEVLINK_PORT_TYPE_NOTSET && 460 nla_put_u16(msg, DEVLINK_ATTR_PORT_DESIRED_TYPE, 461 devlink_port->desired_type)) 462 goto nla_put_failure_type_locked; 463 if (devlink_port->type == DEVLINK_PORT_TYPE_ETH) { 464 if (devlink_port->type_eth.netdev && 465 (nla_put_u32(msg, DEVLINK_ATTR_PORT_NETDEV_IFINDEX, 466 devlink_port->type_eth.ifindex) || 467 nla_put_string(msg, DEVLINK_ATTR_PORT_NETDEV_NAME, 468 devlink_port->type_eth.ifname))) 469 goto nla_put_failure_type_locked; 470 } 471 if (devlink_port->type == DEVLINK_PORT_TYPE_IB) { 472 struct ib_device *ibdev = devlink_port->type_ib.ibdev; 473 474 if (ibdev && 475 nla_put_string(msg, DEVLINK_ATTR_PORT_IBDEV_NAME, 476 ibdev->name)) 477 goto nla_put_failure_type_locked; 478 } 479 spin_unlock_bh(&devlink_port->type_lock); 480 if (devlink_nl_port_attrs_put(msg, devlink_port)) 481 goto nla_put_failure; 482 if (devlink_nl_port_function_attrs_put(msg, devlink_port, extack)) 483 goto nla_put_failure; 484 if (devlink_port->linecard && 485 nla_put_u32(msg, DEVLINK_ATTR_LINECARD_INDEX, 486 devlink_port->linecard->index)) 487 goto nla_put_failure; 488 489 genlmsg_end(msg, hdr); 490 return 0; 491 492 nla_put_failure_type_locked: 493 spin_unlock_bh(&devlink_port->type_lock); 494 nla_put_failure: 495 genlmsg_cancel(msg, hdr); 496 return -EMSGSIZE; 497 } 498 499 static void devlink_port_notify(struct devlink_port *devlink_port, 500 enum devlink_command cmd) 501 { 502 struct devlink *devlink = devlink_port->devlink; 503 struct sk_buff *msg; 504 int err; 505 506 WARN_ON(cmd != DEVLINK_CMD_PORT_NEW && cmd != DEVLINK_CMD_PORT_DEL); 507 508 if (!xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED)) 509 return; 510 511 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); 512 if (!msg) 513 return; 514 515 err = devlink_nl_port_fill(msg, devlink_port, cmd, 0, 0, 0, NULL); 516 if (err) { 517 nlmsg_free(msg); 518 return; 519 } 520 521 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink), msg, 522 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL); 523 } 524 525 static void devlink_ports_notify(struct devlink *devlink, 526 enum devlink_command cmd) 527 { 528 struct devlink_port *devlink_port; 529 unsigned long port_index; 530 531 xa_for_each(&devlink->ports, port_index, devlink_port) 532 devlink_port_notify(devlink_port, cmd); 533 } 534 535 void devlink_ports_notify_register(struct devlink *devlink) 536 { 537 devlink_ports_notify(devlink, DEVLINK_CMD_PORT_NEW); 538 } 539 540 void devlink_ports_notify_unregister(struct devlink *devlink) 541 { 542 devlink_ports_notify(devlink, DEVLINK_CMD_PORT_DEL); 543 } 544 545 int devlink_nl_port_get_doit(struct sk_buff *skb, struct genl_info *info) 546 { 547 struct devlink_port *devlink_port = info->user_ptr[1]; 548 struct sk_buff *msg; 549 int err; 550 551 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); 552 if (!msg) 553 return -ENOMEM; 554 555 err = devlink_nl_port_fill(msg, devlink_port, DEVLINK_CMD_PORT_NEW, 556 info->snd_portid, info->snd_seq, 0, 557 info->extack); 558 if (err) { 559 nlmsg_free(msg); 560 return err; 561 } 562 563 return genlmsg_reply(msg, info); 564 } 565 566 static int 567 devlink_nl_port_get_dump_one(struct sk_buff *msg, struct devlink *devlink, 568 struct netlink_callback *cb, int flags) 569 { 570 struct devlink_nl_dump_state *state = devlink_dump_state(cb); 571 struct devlink_port *devlink_port; 572 unsigned long port_index; 573 int err = 0; 574 575 xa_for_each_start(&devlink->ports, port_index, devlink_port, state->idx) { 576 err = devlink_nl_port_fill(msg, devlink_port, 577 DEVLINK_CMD_NEW, 578 NETLINK_CB(cb->skb).portid, 579 cb->nlh->nlmsg_seq, flags, 580 cb->extack); 581 if (err) { 582 state->idx = port_index; 583 break; 584 } 585 } 586 587 return err; 588 } 589 590 int devlink_nl_port_get_dumpit(struct sk_buff *skb, struct netlink_callback *cb) 591 { 592 return devlink_nl_dumpit(skb, cb, devlink_nl_port_get_dump_one); 593 } 594 595 static int devlink_port_type_set(struct devlink_port *devlink_port, 596 enum devlink_port_type port_type) 597 598 { 599 int err; 600 601 if (!devlink_port->ops->port_type_set) 602 return -EOPNOTSUPP; 603 604 if (port_type == devlink_port->type) 605 return 0; 606 607 err = devlink_port->ops->port_type_set(devlink_port, port_type); 608 if (err) 609 return err; 610 611 devlink_port->desired_type = port_type; 612 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW); 613 return 0; 614 } 615 616 static int devlink_port_function_hw_addr_set(struct devlink_port *port, 617 const struct nlattr *attr, 618 struct netlink_ext_ack *extack) 619 { 620 const u8 *hw_addr; 621 int hw_addr_len; 622 623 hw_addr = nla_data(attr); 624 hw_addr_len = nla_len(attr); 625 if (hw_addr_len > MAX_ADDR_LEN) { 626 NL_SET_ERR_MSG(extack, "Port function hardware address too long"); 627 return -EINVAL; 628 } 629 if (port->type == DEVLINK_PORT_TYPE_ETH) { 630 if (hw_addr_len != ETH_ALEN) { 631 NL_SET_ERR_MSG(extack, "Address must be 6 bytes for Ethernet device"); 632 return -EINVAL; 633 } 634 if (!is_unicast_ether_addr(hw_addr)) { 635 NL_SET_ERR_MSG(extack, "Non-unicast hardware address unsupported"); 636 return -EINVAL; 637 } 638 } 639 640 return port->ops->port_fn_hw_addr_set(port, hw_addr, hw_addr_len, 641 extack); 642 } 643 644 static int devlink_port_fn_state_set(struct devlink_port *port, 645 const struct nlattr *attr, 646 struct netlink_ext_ack *extack) 647 { 648 enum devlink_port_fn_state state; 649 650 state = nla_get_u8(attr); 651 return port->ops->port_fn_state_set(port, state, extack); 652 } 653 654 static int devlink_port_function_validate(struct devlink_port *devlink_port, 655 struct nlattr **tb, 656 struct netlink_ext_ack *extack) 657 { 658 const struct devlink_port_ops *ops = devlink_port->ops; 659 struct nlattr *attr; 660 661 if (tb[DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR] && 662 !ops->port_fn_hw_addr_set) { 663 NL_SET_ERR_MSG_ATTR(extack, tb[DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR], 664 "Port doesn't support function attributes"); 665 return -EOPNOTSUPP; 666 } 667 if (tb[DEVLINK_PORT_FN_ATTR_STATE] && !ops->port_fn_state_set) { 668 NL_SET_ERR_MSG_ATTR(extack, tb[DEVLINK_PORT_FN_ATTR_STATE], 669 "Function does not support state setting"); 670 return -EOPNOTSUPP; 671 } 672 attr = tb[DEVLINK_PORT_FN_ATTR_CAPS]; 673 if (attr) { 674 struct nla_bitfield32 caps; 675 676 caps = nla_get_bitfield32(attr); 677 if (caps.selector & DEVLINK_PORT_FN_CAP_ROCE && 678 !ops->port_fn_roce_set) { 679 NL_SET_ERR_MSG_ATTR(extack, attr, 680 "Port doesn't support RoCE function attribute"); 681 return -EOPNOTSUPP; 682 } 683 if (caps.selector & DEVLINK_PORT_FN_CAP_MIGRATABLE) { 684 if (!ops->port_fn_migratable_set) { 685 NL_SET_ERR_MSG_ATTR(extack, attr, 686 "Port doesn't support migratable function attribute"); 687 return -EOPNOTSUPP; 688 } 689 if (devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_PCI_VF) { 690 NL_SET_ERR_MSG_ATTR(extack, attr, 691 "migratable function attribute supported for VFs only"); 692 return -EOPNOTSUPP; 693 } 694 } 695 if (caps.selector & DEVLINK_PORT_FN_CAP_IPSEC_CRYPTO) { 696 if (!ops->port_fn_ipsec_crypto_set) { 697 NL_SET_ERR_MSG_ATTR(extack, attr, 698 "Port doesn't support ipsec_crypto function attribute"); 699 return -EOPNOTSUPP; 700 } 701 if (devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_PCI_VF) { 702 NL_SET_ERR_MSG_ATTR(extack, attr, 703 "ipsec_crypto function attribute supported for VFs only"); 704 return -EOPNOTSUPP; 705 } 706 } 707 if (caps.selector & DEVLINK_PORT_FN_CAP_IPSEC_PACKET) { 708 if (!ops->port_fn_ipsec_packet_set) { 709 NL_SET_ERR_MSG_ATTR(extack, attr, 710 "Port doesn't support ipsec_packet function attribute"); 711 return -EOPNOTSUPP; 712 } 713 if (devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_PCI_VF) { 714 NL_SET_ERR_MSG_ATTR(extack, attr, 715 "ipsec_packet function attribute supported for VFs only"); 716 return -EOPNOTSUPP; 717 } 718 } 719 } 720 return 0; 721 } 722 723 static int devlink_port_function_set(struct devlink_port *port, 724 const struct nlattr *attr, 725 struct netlink_ext_ack *extack) 726 { 727 struct nlattr *tb[DEVLINK_PORT_FUNCTION_ATTR_MAX + 1]; 728 int err; 729 730 err = nla_parse_nested(tb, DEVLINK_PORT_FUNCTION_ATTR_MAX, attr, 731 devlink_function_nl_policy, extack); 732 if (err < 0) { 733 NL_SET_ERR_MSG(extack, "Fail to parse port function attributes"); 734 return err; 735 } 736 737 err = devlink_port_function_validate(port, tb, extack); 738 if (err) 739 return err; 740 741 attr = tb[DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR]; 742 if (attr) { 743 err = devlink_port_function_hw_addr_set(port, attr, extack); 744 if (err) 745 return err; 746 } 747 748 attr = tb[DEVLINK_PORT_FN_ATTR_CAPS]; 749 if (attr) { 750 err = devlink_port_fn_caps_set(port, attr, extack); 751 if (err) 752 return err; 753 } 754 755 /* Keep this as the last function attribute set, so that when 756 * multiple port function attributes are set along with state, 757 * Those can be applied first before activating the state. 758 */ 759 attr = tb[DEVLINK_PORT_FN_ATTR_STATE]; 760 if (attr) 761 err = devlink_port_fn_state_set(port, attr, extack); 762 763 if (!err) 764 devlink_port_notify(port, DEVLINK_CMD_PORT_NEW); 765 return err; 766 } 767 768 int devlink_nl_cmd_port_set_doit(struct sk_buff *skb, struct genl_info *info) 769 { 770 struct devlink_port *devlink_port = info->user_ptr[1]; 771 int err; 772 773 if (info->attrs[DEVLINK_ATTR_PORT_TYPE]) { 774 enum devlink_port_type port_type; 775 776 port_type = nla_get_u16(info->attrs[DEVLINK_ATTR_PORT_TYPE]); 777 err = devlink_port_type_set(devlink_port, port_type); 778 if (err) 779 return err; 780 } 781 782 if (info->attrs[DEVLINK_ATTR_PORT_FUNCTION]) { 783 struct nlattr *attr = info->attrs[DEVLINK_ATTR_PORT_FUNCTION]; 784 struct netlink_ext_ack *extack = info->extack; 785 786 err = devlink_port_function_set(devlink_port, attr, extack); 787 if (err) 788 return err; 789 } 790 791 return 0; 792 } 793 794 int devlink_nl_cmd_port_split_doit(struct sk_buff *skb, struct genl_info *info) 795 { 796 struct devlink_port *devlink_port = info->user_ptr[1]; 797 struct devlink *devlink = info->user_ptr[0]; 798 u32 count; 799 800 if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_PORT_SPLIT_COUNT)) 801 return -EINVAL; 802 if (!devlink_port->ops->port_split) 803 return -EOPNOTSUPP; 804 805 count = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_SPLIT_COUNT]); 806 807 if (!devlink_port->attrs.splittable) { 808 /* Split ports cannot be split. */ 809 if (devlink_port->attrs.split) 810 NL_SET_ERR_MSG(info->extack, "Port cannot be split further"); 811 else 812 NL_SET_ERR_MSG(info->extack, "Port cannot be split"); 813 return -EINVAL; 814 } 815 816 if (count < 2 || !is_power_of_2(count) || count > devlink_port->attrs.lanes) { 817 NL_SET_ERR_MSG(info->extack, "Invalid split count"); 818 return -EINVAL; 819 } 820 821 return devlink_port->ops->port_split(devlink, devlink_port, count, 822 info->extack); 823 } 824 825 int devlink_nl_cmd_port_unsplit_doit(struct sk_buff *skb, 826 struct genl_info *info) 827 { 828 struct devlink_port *devlink_port = info->user_ptr[1]; 829 struct devlink *devlink = info->user_ptr[0]; 830 831 if (!devlink_port->ops->port_unsplit) 832 return -EOPNOTSUPP; 833 return devlink_port->ops->port_unsplit(devlink, devlink_port, info->extack); 834 } 835 836 int devlink_nl_cmd_port_new_doit(struct sk_buff *skb, struct genl_info *info) 837 { 838 struct netlink_ext_ack *extack = info->extack; 839 struct devlink_port_new_attrs new_attrs = {}; 840 struct devlink *devlink = info->user_ptr[0]; 841 struct devlink_port *devlink_port; 842 struct sk_buff *msg; 843 int err; 844 845 if (!devlink->ops->port_new) 846 return -EOPNOTSUPP; 847 848 if (!info->attrs[DEVLINK_ATTR_PORT_FLAVOUR] || 849 !info->attrs[DEVLINK_ATTR_PORT_PCI_PF_NUMBER]) { 850 NL_SET_ERR_MSG(extack, "Port flavour or PCI PF are not specified"); 851 return -EINVAL; 852 } 853 new_attrs.flavour = nla_get_u16(info->attrs[DEVLINK_ATTR_PORT_FLAVOUR]); 854 new_attrs.pfnum = 855 nla_get_u16(info->attrs[DEVLINK_ATTR_PORT_PCI_PF_NUMBER]); 856 857 if (info->attrs[DEVLINK_ATTR_PORT_INDEX]) { 858 /* Port index of the new port being created by driver. */ 859 new_attrs.port_index = 860 nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]); 861 new_attrs.port_index_valid = true; 862 } 863 if (info->attrs[DEVLINK_ATTR_PORT_CONTROLLER_NUMBER]) { 864 new_attrs.controller = 865 nla_get_u16(info->attrs[DEVLINK_ATTR_PORT_CONTROLLER_NUMBER]); 866 new_attrs.controller_valid = true; 867 } 868 if (new_attrs.flavour == DEVLINK_PORT_FLAVOUR_PCI_SF && 869 info->attrs[DEVLINK_ATTR_PORT_PCI_SF_NUMBER]) { 870 new_attrs.sfnum = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_PCI_SF_NUMBER]); 871 new_attrs.sfnum_valid = true; 872 } 873 874 err = devlink->ops->port_new(devlink, &new_attrs, 875 extack, &devlink_port); 876 if (err) 877 return err; 878 879 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); 880 if (!msg) { 881 err = -ENOMEM; 882 goto err_out_port_del; 883 } 884 err = devlink_nl_port_fill(msg, devlink_port, DEVLINK_CMD_NEW, 885 info->snd_portid, info->snd_seq, 0, NULL); 886 if (WARN_ON_ONCE(err)) 887 goto err_out_msg_free; 888 err = genlmsg_reply(msg, info); 889 if (err) 890 goto err_out_port_del; 891 return 0; 892 893 err_out_msg_free: 894 nlmsg_free(msg); 895 err_out_port_del: 896 devlink_port->ops->port_del(devlink, devlink_port, NULL); 897 return err; 898 } 899 900 int devlink_nl_cmd_port_del_doit(struct sk_buff *skb, struct genl_info *info) 901 { 902 struct devlink_port *devlink_port = info->user_ptr[1]; 903 struct netlink_ext_ack *extack = info->extack; 904 struct devlink *devlink = info->user_ptr[0]; 905 906 if (!devlink_port->ops->port_del) 907 return -EOPNOTSUPP; 908 909 return devlink_port->ops->port_del(devlink, devlink_port, extack); 910 } 911 912 static void devlink_port_type_warn(struct work_struct *work) 913 { 914 struct devlink_port *port = container_of(to_delayed_work(work), 915 struct devlink_port, 916 type_warn_dw); 917 dev_warn(port->devlink->dev, "Type was not set for devlink port."); 918 } 919 920 static bool devlink_port_type_should_warn(struct devlink_port *devlink_port) 921 { 922 /* Ignore CPU and DSA flavours. */ 923 return devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_CPU && 924 devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_DSA && 925 devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_UNUSED; 926 } 927 928 #define DEVLINK_PORT_TYPE_WARN_TIMEOUT (HZ * 3600) 929 930 static void devlink_port_type_warn_schedule(struct devlink_port *devlink_port) 931 { 932 if (!devlink_port_type_should_warn(devlink_port)) 933 return; 934 /* Schedule a work to WARN in case driver does not set port 935 * type within timeout. 936 */ 937 schedule_delayed_work(&devlink_port->type_warn_dw, 938 DEVLINK_PORT_TYPE_WARN_TIMEOUT); 939 } 940 941 static void devlink_port_type_warn_cancel(struct devlink_port *devlink_port) 942 { 943 if (!devlink_port_type_should_warn(devlink_port)) 944 return; 945 cancel_delayed_work_sync(&devlink_port->type_warn_dw); 946 } 947 948 /** 949 * devlink_port_init() - Init devlink port 950 * 951 * @devlink: devlink 952 * @devlink_port: devlink port 953 * 954 * Initialize essential stuff that is needed for functions 955 * that may be called before devlink port registration. 956 * Call to this function is optional and not needed 957 * in case the driver does not use such functions. 958 */ 959 void devlink_port_init(struct devlink *devlink, 960 struct devlink_port *devlink_port) 961 { 962 if (devlink_port->initialized) 963 return; 964 devlink_port->devlink = devlink; 965 INIT_LIST_HEAD(&devlink_port->region_list); 966 devlink_port->initialized = true; 967 } 968 EXPORT_SYMBOL_GPL(devlink_port_init); 969 970 /** 971 * devlink_port_fini() - Deinitialize devlink port 972 * 973 * @devlink_port: devlink port 974 * 975 * Deinitialize essential stuff that is in use for functions 976 * that may be called after devlink port unregistration. 977 * Call to this function is optional and not needed 978 * in case the driver does not use such functions. 979 */ 980 void devlink_port_fini(struct devlink_port *devlink_port) 981 { 982 WARN_ON(!list_empty(&devlink_port->region_list)); 983 } 984 EXPORT_SYMBOL_GPL(devlink_port_fini); 985 986 static const struct devlink_port_ops devlink_port_dummy_ops = {}; 987 988 /** 989 * devl_port_register_with_ops() - Register devlink port 990 * 991 * @devlink: devlink 992 * @devlink_port: devlink port 993 * @port_index: driver-specific numerical identifier of the port 994 * @ops: port ops 995 * 996 * Register devlink port with provided port index. User can use 997 * any indexing, even hw-related one. devlink_port structure 998 * is convenient to be embedded inside user driver private structure. 999 * Note that the caller should take care of zeroing the devlink_port 1000 * structure. 1001 */ 1002 int devl_port_register_with_ops(struct devlink *devlink, 1003 struct devlink_port *devlink_port, 1004 unsigned int port_index, 1005 const struct devlink_port_ops *ops) 1006 { 1007 int err; 1008 1009 devl_assert_locked(devlink); 1010 1011 ASSERT_DEVLINK_PORT_NOT_REGISTERED(devlink_port); 1012 1013 devlink_port_init(devlink, devlink_port); 1014 devlink_port->registered = true; 1015 devlink_port->index = port_index; 1016 devlink_port->ops = ops ? ops : &devlink_port_dummy_ops; 1017 spin_lock_init(&devlink_port->type_lock); 1018 INIT_LIST_HEAD(&devlink_port->reporter_list); 1019 err = xa_insert(&devlink->ports, port_index, devlink_port, GFP_KERNEL); 1020 if (err) { 1021 devlink_port->registered = false; 1022 return err; 1023 } 1024 1025 INIT_DELAYED_WORK(&devlink_port->type_warn_dw, &devlink_port_type_warn); 1026 devlink_port_type_warn_schedule(devlink_port); 1027 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW); 1028 return 0; 1029 } 1030 EXPORT_SYMBOL_GPL(devl_port_register_with_ops); 1031 1032 /** 1033 * devlink_port_register_with_ops - Register devlink port 1034 * 1035 * @devlink: devlink 1036 * @devlink_port: devlink port 1037 * @port_index: driver-specific numerical identifier of the port 1038 * @ops: port ops 1039 * 1040 * Register devlink port with provided port index. User can use 1041 * any indexing, even hw-related one. devlink_port structure 1042 * is convenient to be embedded inside user driver private structure. 1043 * Note that the caller should take care of zeroing the devlink_port 1044 * structure. 1045 * 1046 * Context: Takes and release devlink->lock <mutex>. 1047 */ 1048 int devlink_port_register_with_ops(struct devlink *devlink, 1049 struct devlink_port *devlink_port, 1050 unsigned int port_index, 1051 const struct devlink_port_ops *ops) 1052 { 1053 int err; 1054 1055 devl_lock(devlink); 1056 err = devl_port_register_with_ops(devlink, devlink_port, 1057 port_index, ops); 1058 devl_unlock(devlink); 1059 return err; 1060 } 1061 EXPORT_SYMBOL_GPL(devlink_port_register_with_ops); 1062 1063 /** 1064 * devl_port_unregister() - Unregister devlink port 1065 * 1066 * @devlink_port: devlink port 1067 */ 1068 void devl_port_unregister(struct devlink_port *devlink_port) 1069 { 1070 lockdep_assert_held(&devlink_port->devlink->lock); 1071 WARN_ON(devlink_port->type != DEVLINK_PORT_TYPE_NOTSET); 1072 1073 devlink_port_type_warn_cancel(devlink_port); 1074 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_DEL); 1075 xa_erase(&devlink_port->devlink->ports, devlink_port->index); 1076 WARN_ON(!list_empty(&devlink_port->reporter_list)); 1077 devlink_port->registered = false; 1078 } 1079 EXPORT_SYMBOL_GPL(devl_port_unregister); 1080 1081 /** 1082 * devlink_port_unregister - Unregister devlink port 1083 * 1084 * @devlink_port: devlink port 1085 * 1086 * Context: Takes and release devlink->lock <mutex>. 1087 */ 1088 void devlink_port_unregister(struct devlink_port *devlink_port) 1089 { 1090 struct devlink *devlink = devlink_port->devlink; 1091 1092 devl_lock(devlink); 1093 devl_port_unregister(devlink_port); 1094 devl_unlock(devlink); 1095 } 1096 EXPORT_SYMBOL_GPL(devlink_port_unregister); 1097 1098 static void devlink_port_type_netdev_checks(struct devlink_port *devlink_port, 1099 struct net_device *netdev) 1100 { 1101 const struct net_device_ops *ops = netdev->netdev_ops; 1102 1103 /* If driver registers devlink port, it should set devlink port 1104 * attributes accordingly so the compat functions are called 1105 * and the original ops are not used. 1106 */ 1107 if (ops->ndo_get_phys_port_name) { 1108 /* Some drivers use the same set of ndos for netdevs 1109 * that have devlink_port registered and also for 1110 * those who don't. Make sure that ndo_get_phys_port_name 1111 * returns -EOPNOTSUPP here in case it is defined. 1112 * Warn if not. 1113 */ 1114 char name[IFNAMSIZ]; 1115 int err; 1116 1117 err = ops->ndo_get_phys_port_name(netdev, name, sizeof(name)); 1118 WARN_ON(err != -EOPNOTSUPP); 1119 } 1120 if (ops->ndo_get_port_parent_id) { 1121 /* Some drivers use the same set of ndos for netdevs 1122 * that have devlink_port registered and also for 1123 * those who don't. Make sure that ndo_get_port_parent_id 1124 * returns -EOPNOTSUPP here in case it is defined. 1125 * Warn if not. 1126 */ 1127 struct netdev_phys_item_id ppid; 1128 int err; 1129 1130 err = ops->ndo_get_port_parent_id(netdev, &ppid); 1131 WARN_ON(err != -EOPNOTSUPP); 1132 } 1133 } 1134 1135 static void __devlink_port_type_set(struct devlink_port *devlink_port, 1136 enum devlink_port_type type, 1137 void *type_dev) 1138 { 1139 struct net_device *netdev = type_dev; 1140 1141 ASSERT_DEVLINK_PORT_REGISTERED(devlink_port); 1142 1143 if (type == DEVLINK_PORT_TYPE_NOTSET) { 1144 devlink_port_type_warn_schedule(devlink_port); 1145 } else { 1146 devlink_port_type_warn_cancel(devlink_port); 1147 if (type == DEVLINK_PORT_TYPE_ETH && netdev) 1148 devlink_port_type_netdev_checks(devlink_port, netdev); 1149 } 1150 1151 spin_lock_bh(&devlink_port->type_lock); 1152 devlink_port->type = type; 1153 switch (type) { 1154 case DEVLINK_PORT_TYPE_ETH: 1155 devlink_port->type_eth.netdev = netdev; 1156 if (netdev) { 1157 ASSERT_RTNL(); 1158 devlink_port->type_eth.ifindex = netdev->ifindex; 1159 BUILD_BUG_ON(sizeof(devlink_port->type_eth.ifname) != 1160 sizeof(netdev->name)); 1161 strcpy(devlink_port->type_eth.ifname, netdev->name); 1162 } 1163 break; 1164 case DEVLINK_PORT_TYPE_IB: 1165 devlink_port->type_ib.ibdev = type_dev; 1166 break; 1167 default: 1168 break; 1169 } 1170 spin_unlock_bh(&devlink_port->type_lock); 1171 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW); 1172 } 1173 1174 /** 1175 * devlink_port_type_eth_set - Set port type to Ethernet 1176 * 1177 * @devlink_port: devlink port 1178 * 1179 * If driver is calling this, most likely it is doing something wrong. 1180 */ 1181 void devlink_port_type_eth_set(struct devlink_port *devlink_port) 1182 { 1183 dev_warn(devlink_port->devlink->dev, 1184 "devlink port type for port %d set to Ethernet without a software interface reference, device type not supported by the kernel?\n", 1185 devlink_port->index); 1186 __devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_ETH, NULL); 1187 } 1188 EXPORT_SYMBOL_GPL(devlink_port_type_eth_set); 1189 1190 /** 1191 * devlink_port_type_ib_set - Set port type to InfiniBand 1192 * 1193 * @devlink_port: devlink port 1194 * @ibdev: related IB device 1195 */ 1196 void devlink_port_type_ib_set(struct devlink_port *devlink_port, 1197 struct ib_device *ibdev) 1198 { 1199 __devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_IB, ibdev); 1200 } 1201 EXPORT_SYMBOL_GPL(devlink_port_type_ib_set); 1202 1203 /** 1204 * devlink_port_type_clear - Clear port type 1205 * 1206 * @devlink_port: devlink port 1207 * 1208 * If driver is calling this for clearing Ethernet type, most likely 1209 * it is doing something wrong. 1210 */ 1211 void devlink_port_type_clear(struct devlink_port *devlink_port) 1212 { 1213 if (devlink_port->type == DEVLINK_PORT_TYPE_ETH) 1214 dev_warn(devlink_port->devlink->dev, 1215 "devlink port type for port %d cleared without a software interface reference, device type not supported by the kernel?\n", 1216 devlink_port->index); 1217 __devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_NOTSET, NULL); 1218 } 1219 EXPORT_SYMBOL_GPL(devlink_port_type_clear); 1220 1221 int devlink_port_netdevice_event(struct notifier_block *nb, 1222 unsigned long event, void *ptr) 1223 { 1224 struct net_device *netdev = netdev_notifier_info_to_dev(ptr); 1225 struct devlink_port *devlink_port = netdev->devlink_port; 1226 struct devlink *devlink; 1227 1228 if (!devlink_port) 1229 return NOTIFY_OK; 1230 devlink = devlink_port->devlink; 1231 1232 switch (event) { 1233 case NETDEV_POST_INIT: 1234 /* Set the type but not netdev pointer. It is going to be set 1235 * later on by NETDEV_REGISTER event. Happens once during 1236 * netdevice register 1237 */ 1238 __devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_ETH, 1239 NULL); 1240 break; 1241 case NETDEV_REGISTER: 1242 case NETDEV_CHANGENAME: 1243 if (devlink_net(devlink) != dev_net(netdev)) 1244 return NOTIFY_OK; 1245 /* Set the netdev on top of previously set type. Note this 1246 * event happens also during net namespace change so here 1247 * we take into account netdev pointer appearing in this 1248 * namespace. 1249 */ 1250 __devlink_port_type_set(devlink_port, devlink_port->type, 1251 netdev); 1252 break; 1253 case NETDEV_UNREGISTER: 1254 if (devlink_net(devlink) != dev_net(netdev)) 1255 return NOTIFY_OK; 1256 /* Clear netdev pointer, but not the type. This event happens 1257 * also during net namespace change so we need to clear 1258 * pointer to netdev that is going to another net namespace. 1259 */ 1260 __devlink_port_type_set(devlink_port, devlink_port->type, 1261 NULL); 1262 break; 1263 case NETDEV_PRE_UNINIT: 1264 /* Clear the type and the netdev pointer. Happens one during 1265 * netdevice unregister. 1266 */ 1267 __devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_NOTSET, 1268 NULL); 1269 break; 1270 } 1271 1272 return NOTIFY_OK; 1273 } 1274 1275 static int __devlink_port_attrs_set(struct devlink_port *devlink_port, 1276 enum devlink_port_flavour flavour) 1277 { 1278 struct devlink_port_attrs *attrs = &devlink_port->attrs; 1279 1280 devlink_port->attrs_set = true; 1281 attrs->flavour = flavour; 1282 if (attrs->switch_id.id_len) { 1283 devlink_port->switch_port = true; 1284 if (WARN_ON(attrs->switch_id.id_len > MAX_PHYS_ITEM_ID_LEN)) 1285 attrs->switch_id.id_len = MAX_PHYS_ITEM_ID_LEN; 1286 } else { 1287 devlink_port->switch_port = false; 1288 } 1289 return 0; 1290 } 1291 1292 /** 1293 * devlink_port_attrs_set - Set port attributes 1294 * 1295 * @devlink_port: devlink port 1296 * @attrs: devlink port attrs 1297 */ 1298 void devlink_port_attrs_set(struct devlink_port *devlink_port, 1299 struct devlink_port_attrs *attrs) 1300 { 1301 int ret; 1302 1303 ASSERT_DEVLINK_PORT_NOT_REGISTERED(devlink_port); 1304 1305 devlink_port->attrs = *attrs; 1306 ret = __devlink_port_attrs_set(devlink_port, attrs->flavour); 1307 if (ret) 1308 return; 1309 WARN_ON(attrs->splittable && attrs->split); 1310 } 1311 EXPORT_SYMBOL_GPL(devlink_port_attrs_set); 1312 1313 /** 1314 * devlink_port_attrs_pci_pf_set - Set PCI PF port attributes 1315 * 1316 * @devlink_port: devlink port 1317 * @controller: associated controller number for the devlink port instance 1318 * @pf: associated PF for the devlink port instance 1319 * @external: indicates if the port is for an external controller 1320 */ 1321 void devlink_port_attrs_pci_pf_set(struct devlink_port *devlink_port, u32 controller, 1322 u16 pf, bool external) 1323 { 1324 struct devlink_port_attrs *attrs = &devlink_port->attrs; 1325 int ret; 1326 1327 ASSERT_DEVLINK_PORT_NOT_REGISTERED(devlink_port); 1328 1329 ret = __devlink_port_attrs_set(devlink_port, 1330 DEVLINK_PORT_FLAVOUR_PCI_PF); 1331 if (ret) 1332 return; 1333 attrs->pci_pf.controller = controller; 1334 attrs->pci_pf.pf = pf; 1335 attrs->pci_pf.external = external; 1336 } 1337 EXPORT_SYMBOL_GPL(devlink_port_attrs_pci_pf_set); 1338 1339 /** 1340 * devlink_port_attrs_pci_vf_set - Set PCI VF port attributes 1341 * 1342 * @devlink_port: devlink port 1343 * @controller: associated controller number for the devlink port instance 1344 * @pf: associated PF for the devlink port instance 1345 * @vf: associated VF of a PF for the devlink port instance 1346 * @external: indicates if the port is for an external controller 1347 */ 1348 void devlink_port_attrs_pci_vf_set(struct devlink_port *devlink_port, u32 controller, 1349 u16 pf, u16 vf, bool external) 1350 { 1351 struct devlink_port_attrs *attrs = &devlink_port->attrs; 1352 int ret; 1353 1354 ASSERT_DEVLINK_PORT_NOT_REGISTERED(devlink_port); 1355 1356 ret = __devlink_port_attrs_set(devlink_port, 1357 DEVLINK_PORT_FLAVOUR_PCI_VF); 1358 if (ret) 1359 return; 1360 attrs->pci_vf.controller = controller; 1361 attrs->pci_vf.pf = pf; 1362 attrs->pci_vf.vf = vf; 1363 attrs->pci_vf.external = external; 1364 } 1365 EXPORT_SYMBOL_GPL(devlink_port_attrs_pci_vf_set); 1366 1367 /** 1368 * devlink_port_attrs_pci_sf_set - Set PCI SF port attributes 1369 * 1370 * @devlink_port: devlink port 1371 * @controller: associated controller number for the devlink port instance 1372 * @pf: associated PF for the devlink port instance 1373 * @sf: associated SF of a PF for the devlink port instance 1374 * @external: indicates if the port is for an external controller 1375 */ 1376 void devlink_port_attrs_pci_sf_set(struct devlink_port *devlink_port, u32 controller, 1377 u16 pf, u32 sf, bool external) 1378 { 1379 struct devlink_port_attrs *attrs = &devlink_port->attrs; 1380 int ret; 1381 1382 ASSERT_DEVLINK_PORT_NOT_REGISTERED(devlink_port); 1383 1384 ret = __devlink_port_attrs_set(devlink_port, 1385 DEVLINK_PORT_FLAVOUR_PCI_SF); 1386 if (ret) 1387 return; 1388 attrs->pci_sf.controller = controller; 1389 attrs->pci_sf.pf = pf; 1390 attrs->pci_sf.sf = sf; 1391 attrs->pci_sf.external = external; 1392 } 1393 EXPORT_SYMBOL_GPL(devlink_port_attrs_pci_sf_set); 1394 1395 /** 1396 * devlink_port_linecard_set - Link port with a linecard 1397 * 1398 * @devlink_port: devlink port 1399 * @linecard: devlink linecard 1400 */ 1401 void devlink_port_linecard_set(struct devlink_port *devlink_port, 1402 struct devlink_linecard *linecard) 1403 { 1404 ASSERT_DEVLINK_PORT_NOT_REGISTERED(devlink_port); 1405 1406 devlink_port->linecard = linecard; 1407 } 1408 EXPORT_SYMBOL_GPL(devlink_port_linecard_set); 1409 1410 static int __devlink_port_phys_port_name_get(struct devlink_port *devlink_port, 1411 char *name, size_t len) 1412 { 1413 struct devlink_port_attrs *attrs = &devlink_port->attrs; 1414 int n = 0; 1415 1416 if (!devlink_port->attrs_set) 1417 return -EOPNOTSUPP; 1418 1419 switch (attrs->flavour) { 1420 case DEVLINK_PORT_FLAVOUR_PHYSICAL: 1421 if (devlink_port->linecard) 1422 n = snprintf(name, len, "l%u", 1423 devlink_port->linecard->index); 1424 if (n < len) 1425 n += snprintf(name + n, len - n, "p%u", 1426 attrs->phys.port_number); 1427 if (n < len && attrs->split) 1428 n += snprintf(name + n, len - n, "s%u", 1429 attrs->phys.split_subport_number); 1430 break; 1431 case DEVLINK_PORT_FLAVOUR_CPU: 1432 case DEVLINK_PORT_FLAVOUR_DSA: 1433 case DEVLINK_PORT_FLAVOUR_UNUSED: 1434 /* As CPU and DSA ports do not have a netdevice associated 1435 * case should not ever happen. 1436 */ 1437 WARN_ON(1); 1438 return -EINVAL; 1439 case DEVLINK_PORT_FLAVOUR_PCI_PF: 1440 if (attrs->pci_pf.external) { 1441 n = snprintf(name, len, "c%u", attrs->pci_pf.controller); 1442 if (n >= len) 1443 return -EINVAL; 1444 len -= n; 1445 name += n; 1446 } 1447 n = snprintf(name, len, "pf%u", attrs->pci_pf.pf); 1448 break; 1449 case DEVLINK_PORT_FLAVOUR_PCI_VF: 1450 if (attrs->pci_vf.external) { 1451 n = snprintf(name, len, "c%u", attrs->pci_vf.controller); 1452 if (n >= len) 1453 return -EINVAL; 1454 len -= n; 1455 name += n; 1456 } 1457 n = snprintf(name, len, "pf%uvf%u", 1458 attrs->pci_vf.pf, attrs->pci_vf.vf); 1459 break; 1460 case DEVLINK_PORT_FLAVOUR_PCI_SF: 1461 if (attrs->pci_sf.external) { 1462 n = snprintf(name, len, "c%u", attrs->pci_sf.controller); 1463 if (n >= len) 1464 return -EINVAL; 1465 len -= n; 1466 name += n; 1467 } 1468 n = snprintf(name, len, "pf%usf%u", attrs->pci_sf.pf, 1469 attrs->pci_sf.sf); 1470 break; 1471 case DEVLINK_PORT_FLAVOUR_VIRTUAL: 1472 return -EOPNOTSUPP; 1473 } 1474 1475 if (n >= len) 1476 return -EINVAL; 1477 1478 return 0; 1479 } 1480 1481 int devlink_compat_phys_port_name_get(struct net_device *dev, 1482 char *name, size_t len) 1483 { 1484 struct devlink_port *devlink_port; 1485 1486 /* RTNL mutex is held here which ensures that devlink_port 1487 * instance cannot disappear in the middle. No need to take 1488 * any devlink lock as only permanent values are accessed. 1489 */ 1490 ASSERT_RTNL(); 1491 1492 devlink_port = dev->devlink_port; 1493 if (!devlink_port) 1494 return -EOPNOTSUPP; 1495 1496 return __devlink_port_phys_port_name_get(devlink_port, name, len); 1497 } 1498 1499 int devlink_compat_switch_id_get(struct net_device *dev, 1500 struct netdev_phys_item_id *ppid) 1501 { 1502 struct devlink_port *devlink_port; 1503 1504 /* Caller must hold RTNL mutex or reference to dev, which ensures that 1505 * devlink_port instance cannot disappear in the middle. No need to take 1506 * any devlink lock as only permanent values are accessed. 1507 */ 1508 devlink_port = dev->devlink_port; 1509 if (!devlink_port || !devlink_port->switch_port) 1510 return -EOPNOTSUPP; 1511 1512 memcpy(ppid, &devlink_port->attrs.switch_id, sizeof(*ppid)); 1513 1514 return 0; 1515 } 1516