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