1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (c) 2016 Mellanox Technologies. All rights reserved.
4 * Copyright (c) 2016 Jiri Pirko <jiri@mellanox.com>
7 #include "devl_internal.h"
9 #define DEVLINK_PORT_FN_CAPS_VALID_MASK \
10 (_BITUL(__DEVLINK_PORT_FN_ATTR_CAPS_MAX) - 1)
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),
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)
26 struct devlink_port *devlink_port_get_by_index(struct devlink *devlink,
27 unsigned int port_index)
29 return xa_load(&devlink->ports, port_index);
32 struct devlink_port *devlink_port_get_from_attrs(struct devlink *devlink,
33 struct nlattr **attrs)
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;
39 devlink_port = devlink_port_get_by_index(devlink, port_index);
41 return ERR_PTR(-ENODEV);
44 return ERR_PTR(-EINVAL);
47 struct devlink_port *devlink_port_get_from_info(struct devlink *devlink,
48 struct genl_info *info)
50 return devlink_port_get_from_attrs(devlink, info->attrs);
53 static void devlink_port_fn_cap_fill(struct nla_bitfield32 *caps,
54 u32 cap, bool is_enable)
56 caps->selector |= cap;
61 static int devlink_port_fn_roce_fill(struct devlink_port *devlink_port,
62 struct nla_bitfield32 *caps,
63 struct netlink_ext_ack *extack)
68 if (!devlink_port->ops->port_fn_roce_get)
71 err = devlink_port->ops->port_fn_roce_get(devlink_port, &is_enable,
74 if (err == -EOPNOTSUPP)
79 devlink_port_fn_cap_fill(caps, DEVLINK_PORT_FN_CAP_ROCE, is_enable);
83 static int devlink_port_fn_migratable_fill(struct devlink_port *devlink_port,
84 struct nla_bitfield32 *caps,
85 struct netlink_ext_ack *extack)
90 if (!devlink_port->ops->port_fn_migratable_get ||
91 devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_PCI_VF)
94 err = devlink_port->ops->port_fn_migratable_get(devlink_port,
97 if (err == -EOPNOTSUPP)
102 devlink_port_fn_cap_fill(caps, DEVLINK_PORT_FN_CAP_MIGRATABLE, is_enable);
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)
113 if (!devlink_port->ops->port_fn_ipsec_crypto_get ||
114 devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_PCI_VF)
117 err = devlink_port->ops->port_fn_ipsec_crypto_get(devlink_port, &is_enable, extack);
119 if (err == -EOPNOTSUPP)
124 devlink_port_fn_cap_fill(caps, DEVLINK_PORT_FN_CAP_IPSEC_CRYPTO, is_enable);
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)
135 if (!devlink_port->ops->port_fn_ipsec_packet_get ||
136 devlink_port->attrs.flavour != DEVLINK_PORT_FLAVOUR_PCI_VF)
139 err = devlink_port->ops->port_fn_ipsec_packet_get(devlink_port, &is_enable, extack);
141 if (err == -EOPNOTSUPP)
146 devlink_port_fn_cap_fill(caps, DEVLINK_PORT_FN_CAP_IPSEC_PACKET, is_enable);
150 static int devlink_port_fn_caps_fill(struct devlink_port *devlink_port,
152 struct netlink_ext_ack *extack,
155 struct nla_bitfield32 caps = {};
158 err = devlink_port_fn_roce_fill(devlink_port, &caps, extack);
162 err = devlink_port_fn_migratable_fill(devlink_port, &caps, extack);
166 err = devlink_port_fn_ipsec_crypto_fill(devlink_port, &caps, extack);
170 err = devlink_port_fn_ipsec_packet_fill(devlink_port, &caps, extack);
176 err = nla_put_bitfield32(msg, DEVLINK_PORT_FN_ATTR_CAPS, caps.value,
185 int devlink_nl_port_handle_fill(struct sk_buff *msg, struct devlink_port *devlink_port)
187 if (devlink_nl_put_handle(msg, devlink_port->devlink))
189 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
194 size_t devlink_nl_port_handle_size(struct devlink_port *devlink_port)
196 struct devlink *devlink = devlink_port->devlink;
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 */
203 static int devlink_nl_port_attrs_put(struct sk_buff *msg,
204 struct devlink_port *devlink_port)
206 struct devlink_port_attrs *attrs = &devlink_port->attrs;
208 if (!devlink_port->attrs_set)
211 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_LANES, attrs->lanes))
214 if (nla_put_u8(msg, DEVLINK_ATTR_PORT_SPLITTABLE, attrs->splittable))
216 if (nla_put_u16(msg, DEVLINK_ATTR_PORT_FLAVOUR, attrs->flavour))
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))
224 if (nla_put_u8(msg, DEVLINK_ATTR_PORT_EXTERNAL, attrs->pci_pf.external))
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))
233 if (nla_put_u8(msg, DEVLINK_ATTR_PORT_EXTERNAL, attrs->pci_vf.external))
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,
241 nla_put_u32(msg, DEVLINK_ATTR_PORT_PCI_SF_NUMBER,
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))
253 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_SPLIT_GROUP,
254 attrs->phys.port_number))
256 if (nla_put_u32(msg, DEVLINK_ATTR_PORT_SPLIT_SUBPORT_NUMBER,
257 attrs->phys.split_subport_number))
266 static int devlink_port_fn_hw_addr_fill(struct devlink_port *port,
268 struct netlink_ext_ack *extack,
271 u8 hw_addr[MAX_ADDR_LEN];
275 if (!port->ops->port_fn_hw_addr_get)
278 err = port->ops->port_fn_hw_addr_get(port, hw_addr, &hw_addr_len,
281 if (err == -EOPNOTSUPP)
285 err = nla_put(msg, DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR, hw_addr_len, hw_addr);
293 devlink_port_fn_state_valid(enum devlink_port_fn_state state)
295 return state == DEVLINK_PORT_FN_STATE_INACTIVE ||
296 state == DEVLINK_PORT_FN_STATE_ACTIVE;
300 devlink_port_fn_opstate_valid(enum devlink_port_fn_opstate opstate)
302 return opstate == DEVLINK_PORT_FN_OPSTATE_DETACHED ||
303 opstate == DEVLINK_PORT_FN_OPSTATE_ATTACHED;
306 static int devlink_port_fn_state_fill(struct devlink_port *port,
308 struct netlink_ext_ack *extack,
311 enum devlink_port_fn_opstate opstate;
312 enum devlink_port_fn_state state;
315 if (!port->ops->port_fn_state_get)
318 err = port->ops->port_fn_state_get(port, &state, &opstate, extack);
320 if (err == -EOPNOTSUPP)
324 if (!devlink_port_fn_state_valid(state)) {
326 NL_SET_ERR_MSG(extack, "Invalid state read from driver");
329 if (!devlink_port_fn_opstate_valid(opstate)) {
331 NL_SET_ERR_MSG(extack, "Invalid operational state read from driver");
334 if (nla_put_u8(msg, DEVLINK_PORT_FN_ATTR_STATE, state) ||
335 nla_put_u8(msg, DEVLINK_PORT_FN_ATTR_OPSTATE, opstate))
342 devlink_port_fn_mig_set(struct devlink_port *devlink_port, bool enable,
343 struct netlink_ext_ack *extack)
345 return devlink_port->ops->port_fn_migratable_set(devlink_port, enable,
350 devlink_port_fn_roce_set(struct devlink_port *devlink_port, bool enable,
351 struct netlink_ext_ack *extack)
353 return devlink_port->ops->port_fn_roce_set(devlink_port, enable,
358 devlink_port_fn_ipsec_crypto_set(struct devlink_port *devlink_port, bool enable,
359 struct netlink_ext_ack *extack)
361 return devlink_port->ops->port_fn_ipsec_crypto_set(devlink_port, enable, extack);
365 devlink_port_fn_ipsec_packet_set(struct devlink_port *devlink_port, bool enable,
366 struct netlink_ext_ack *extack)
368 return devlink_port->ops->port_fn_ipsec_packet_set(devlink_port, enable, extack);
371 static int devlink_port_fn_caps_set(struct devlink_port *devlink_port,
372 const struct nlattr *attr,
373 struct netlink_ext_ack *extack)
375 struct nla_bitfield32 caps;
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,
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,
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,
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,
413 devlink_nl_port_function_attrs_put(struct sk_buff *msg, struct devlink_port *port,
414 struct netlink_ext_ack *extack)
416 struct nlattr *function_attr;
417 bool msg_updated = false;
420 function_attr = nla_nest_start_noflag(msg, DEVLINK_ATTR_PORT_FUNCTION);
424 err = devlink_port_fn_hw_addr_fill(port, msg, extack, &msg_updated);
427 err = devlink_port_fn_caps_fill(port, msg, extack, &msg_updated);
430 err = devlink_port_fn_state_fill(port, msg, extack, &msg_updated);
432 if (err || !msg_updated)
433 nla_nest_cancel(msg, function_attr);
435 nla_nest_end(msg, function_attr);
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)
444 struct devlink *devlink = devlink_port->devlink;
447 hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
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;
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;
471 if (devlink_port->type == DEVLINK_PORT_TYPE_IB) {
472 struct ib_device *ibdev = devlink_port->type_ib.ibdev;
475 nla_put_string(msg, DEVLINK_ATTR_PORT_IBDEV_NAME,
477 goto nla_put_failure_type_locked;
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;
489 genlmsg_end(msg, hdr);
492 nla_put_failure_type_locked:
493 spin_unlock_bh(&devlink_port->type_lock);
495 genlmsg_cancel(msg, hdr);
499 static void devlink_port_notify(struct devlink_port *devlink_port,
500 enum devlink_command cmd)
502 struct devlink *devlink = devlink_port->devlink;
506 WARN_ON(cmd != DEVLINK_CMD_PORT_NEW && cmd != DEVLINK_CMD_PORT_DEL);
508 if (!xa_get_mark(&devlinks, devlink->index, DEVLINK_REGISTERED))
511 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
515 err = devlink_nl_port_fill(msg, devlink_port, cmd, 0, 0, 0, NULL);
521 genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink), msg,
522 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
525 static void devlink_ports_notify(struct devlink *devlink,
526 enum devlink_command cmd)
528 struct devlink_port *devlink_port;
529 unsigned long port_index;
531 xa_for_each(&devlink->ports, port_index, devlink_port)
532 devlink_port_notify(devlink_port, cmd);
535 void devlink_ports_notify_register(struct devlink *devlink)
537 devlink_ports_notify(devlink, DEVLINK_CMD_PORT_NEW);
540 void devlink_ports_notify_unregister(struct devlink *devlink)
542 devlink_ports_notify(devlink, DEVLINK_CMD_PORT_DEL);
545 int devlink_nl_port_get_doit(struct sk_buff *skb, struct genl_info *info)
547 struct devlink_port *devlink_port = info->user_ptr[1];
551 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
555 err = devlink_nl_port_fill(msg, devlink_port, DEVLINK_CMD_PORT_NEW,
556 info->snd_portid, info->snd_seq, 0,
563 return genlmsg_reply(msg, info);
567 devlink_nl_port_get_dump_one(struct sk_buff *msg, struct devlink *devlink,
568 struct netlink_callback *cb, int flags)
570 struct devlink_nl_dump_state *state = devlink_dump_state(cb);
571 struct devlink_port *devlink_port;
572 unsigned long port_index;
575 xa_for_each_start(&devlink->ports, port_index, devlink_port, state->idx) {
576 err = devlink_nl_port_fill(msg, devlink_port,
578 NETLINK_CB(cb->skb).portid,
579 cb->nlh->nlmsg_seq, flags,
582 state->idx = port_index;
590 int devlink_nl_port_get_dumpit(struct sk_buff *skb, struct netlink_callback *cb)
592 return devlink_nl_dumpit(skb, cb, devlink_nl_port_get_dump_one);
595 static int devlink_port_type_set(struct devlink_port *devlink_port,
596 enum devlink_port_type port_type)
601 if (!devlink_port->ops->port_type_set)
604 if (port_type == devlink_port->type)
607 err = devlink_port->ops->port_type_set(devlink_port, port_type);
611 devlink_port->desired_type = port_type;
612 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
616 static int devlink_port_function_hw_addr_set(struct devlink_port *port,
617 const struct nlattr *attr,
618 struct netlink_ext_ack *extack)
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");
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");
634 if (!is_unicast_ether_addr(hw_addr)) {
635 NL_SET_ERR_MSG(extack, "Non-unicast hardware address unsupported");
640 return port->ops->port_fn_hw_addr_set(port, hw_addr, hw_addr_len,
644 static int devlink_port_fn_state_set(struct devlink_port *port,
645 const struct nlattr *attr,
646 struct netlink_ext_ack *extack)
648 enum devlink_port_fn_state state;
650 state = nla_get_u8(attr);
651 return port->ops->port_fn_state_set(port, state, extack);
654 static int devlink_port_function_validate(struct devlink_port *devlink_port,
656 struct netlink_ext_ack *extack)
658 const struct devlink_port_ops *ops = devlink_port->ops;
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");
667 if (tb[DEVLINK_PORT_FN_ATTR_STATE] && !ops->port_fn_state_set) {
668 NL_SET_ERR_MSG_ATTR(extack, tb[DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR],
669 "Function does not support state setting");
672 attr = tb[DEVLINK_PORT_FN_ATTR_CAPS];
674 struct nla_bitfield32 caps;
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");
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");
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");
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");
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");
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");
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");
723 static int devlink_port_function_set(struct devlink_port *port,
724 const struct nlattr *attr,
725 struct netlink_ext_ack *extack)
727 struct nlattr *tb[DEVLINK_PORT_FUNCTION_ATTR_MAX + 1];
730 err = nla_parse_nested(tb, DEVLINK_PORT_FUNCTION_ATTR_MAX, attr,
731 devlink_function_nl_policy, extack);
733 NL_SET_ERR_MSG(extack, "Fail to parse port function attributes");
737 err = devlink_port_function_validate(port, tb, extack);
741 attr = tb[DEVLINK_PORT_FUNCTION_ATTR_HW_ADDR];
743 err = devlink_port_function_hw_addr_set(port, attr, extack);
748 attr = tb[DEVLINK_PORT_FN_ATTR_CAPS];
750 err = devlink_port_fn_caps_set(port, attr, extack);
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.
759 attr = tb[DEVLINK_PORT_FN_ATTR_STATE];
761 err = devlink_port_fn_state_set(port, attr, extack);
764 devlink_port_notify(port, DEVLINK_CMD_PORT_NEW);
768 int devlink_nl_cmd_port_set_doit(struct sk_buff *skb, struct genl_info *info)
770 struct devlink_port *devlink_port = info->user_ptr[1];
773 if (info->attrs[DEVLINK_ATTR_PORT_TYPE]) {
774 enum devlink_port_type port_type;
776 port_type = nla_get_u16(info->attrs[DEVLINK_ATTR_PORT_TYPE]);
777 err = devlink_port_type_set(devlink_port, port_type);
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;
786 err = devlink_port_function_set(devlink_port, attr, extack);
794 int devlink_nl_cmd_port_split_doit(struct sk_buff *skb, struct genl_info *info)
796 struct devlink_port *devlink_port = info->user_ptr[1];
797 struct devlink *devlink = info->user_ptr[0];
800 if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_PORT_SPLIT_COUNT))
802 if (!devlink_port->ops->port_split)
805 count = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_SPLIT_COUNT]);
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");
812 NL_SET_ERR_MSG(info->extack, "Port cannot be split");
816 if (count < 2 || !is_power_of_2(count) || count > devlink_port->attrs.lanes) {
817 NL_SET_ERR_MSG(info->extack, "Invalid split count");
821 return devlink_port->ops->port_split(devlink, devlink_port, count,
825 int devlink_nl_cmd_port_unsplit_doit(struct sk_buff *skb,
826 struct genl_info *info)
828 struct devlink_port *devlink_port = info->user_ptr[1];
829 struct devlink *devlink = info->user_ptr[0];
831 if (!devlink_port->ops->port_unsplit)
833 return devlink_port->ops->port_unsplit(devlink, devlink_port, info->extack);
836 int devlink_nl_cmd_port_new_doit(struct sk_buff *skb, struct genl_info *info)
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;
845 if (!devlink->ops->port_new)
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");
853 new_attrs.flavour = nla_get_u16(info->attrs[DEVLINK_ATTR_PORT_FLAVOUR]);
855 nla_get_u16(info->attrs[DEVLINK_ATTR_PORT_PCI_PF_NUMBER]);
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;
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;
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;
874 err = devlink->ops->port_new(devlink, &new_attrs,
875 extack, &devlink_port);
879 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
882 goto err_out_port_del;
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);
890 goto err_out_port_del;
896 devlink_port->ops->port_del(devlink, devlink_port, NULL);
900 int devlink_nl_cmd_port_del_doit(struct sk_buff *skb, struct genl_info *info)
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];
906 if (!devlink_port->ops->port_del)
909 return devlink_port->ops->port_del(devlink, devlink_port, extack);
912 static void devlink_port_type_warn(struct work_struct *work)
914 struct devlink_port *port = container_of(to_delayed_work(work),
917 dev_warn(port->devlink->dev, "Type was not set for devlink port.");
920 static bool devlink_port_type_should_warn(struct devlink_port *devlink_port)
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;
928 #define DEVLINK_PORT_TYPE_WARN_TIMEOUT (HZ * 3600)
930 static void devlink_port_type_warn_schedule(struct devlink_port *devlink_port)
932 if (!devlink_port_type_should_warn(devlink_port))
934 /* Schedule a work to WARN in case driver does not set port
935 * type within timeout.
937 schedule_delayed_work(&devlink_port->type_warn_dw,
938 DEVLINK_PORT_TYPE_WARN_TIMEOUT);
941 static void devlink_port_type_warn_cancel(struct devlink_port *devlink_port)
943 if (!devlink_port_type_should_warn(devlink_port))
945 cancel_delayed_work_sync(&devlink_port->type_warn_dw);
949 * devlink_port_init() - Init devlink port
952 * @devlink_port: devlink port
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.
959 void devlink_port_init(struct devlink *devlink,
960 struct devlink_port *devlink_port)
962 if (devlink_port->initialized)
964 devlink_port->devlink = devlink;
965 INIT_LIST_HEAD(&devlink_port->region_list);
966 devlink_port->initialized = true;
968 EXPORT_SYMBOL_GPL(devlink_port_init);
971 * devlink_port_fini() - Deinitialize devlink port
973 * @devlink_port: devlink port
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.
980 void devlink_port_fini(struct devlink_port *devlink_port)
982 WARN_ON(!list_empty(&devlink_port->region_list));
984 EXPORT_SYMBOL_GPL(devlink_port_fini);
986 static const struct devlink_port_ops devlink_port_dummy_ops = {};
989 * devl_port_register_with_ops() - Register devlink port
992 * @devlink_port: devlink port
993 * @port_index: driver-specific numerical identifier of the port
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
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)
1009 devl_assert_locked(devlink);
1011 ASSERT_DEVLINK_PORT_NOT_REGISTERED(devlink_port);
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);
1021 devlink_port->registered = false;
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);
1030 EXPORT_SYMBOL_GPL(devl_port_register_with_ops);
1033 * devlink_port_register_with_ops - Register devlink port
1036 * @devlink_port: devlink port
1037 * @port_index: driver-specific numerical identifier of the port
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
1046 * Context: Takes and release devlink->lock <mutex>.
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)
1056 err = devl_port_register_with_ops(devlink, devlink_port,
1058 devl_unlock(devlink);
1061 EXPORT_SYMBOL_GPL(devlink_port_register_with_ops);
1064 * devl_port_unregister() - Unregister devlink port
1066 * @devlink_port: devlink port
1068 void devl_port_unregister(struct devlink_port *devlink_port)
1070 lockdep_assert_held(&devlink_port->devlink->lock);
1071 WARN_ON(devlink_port->type != DEVLINK_PORT_TYPE_NOTSET);
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;
1079 EXPORT_SYMBOL_GPL(devl_port_unregister);
1082 * devlink_port_unregister - Unregister devlink port
1084 * @devlink_port: devlink port
1086 * Context: Takes and release devlink->lock <mutex>.
1088 void devlink_port_unregister(struct devlink_port *devlink_port)
1090 struct devlink *devlink = devlink_port->devlink;
1093 devl_port_unregister(devlink_port);
1094 devl_unlock(devlink);
1096 EXPORT_SYMBOL_GPL(devlink_port_unregister);
1098 static void devlink_port_type_netdev_checks(struct devlink_port *devlink_port,
1099 struct net_device *netdev)
1101 const struct net_device_ops *ops = netdev->netdev_ops;
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.
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.
1114 char name[IFNAMSIZ];
1117 err = ops->ndo_get_phys_port_name(netdev, name, sizeof(name));
1118 WARN_ON(err != -EOPNOTSUPP);
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.
1127 struct netdev_phys_item_id ppid;
1130 err = ops->ndo_get_port_parent_id(netdev, &ppid);
1131 WARN_ON(err != -EOPNOTSUPP);
1135 static void __devlink_port_type_set(struct devlink_port *devlink_port,
1136 enum devlink_port_type type,
1139 struct net_device *netdev = type_dev;
1141 ASSERT_DEVLINK_PORT_REGISTERED(devlink_port);
1143 if (type == DEVLINK_PORT_TYPE_NOTSET) {
1144 devlink_port_type_warn_schedule(devlink_port);
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);
1151 spin_lock_bh(&devlink_port->type_lock);
1152 devlink_port->type = type;
1154 case DEVLINK_PORT_TYPE_ETH:
1155 devlink_port->type_eth.netdev = netdev;
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);
1164 case DEVLINK_PORT_TYPE_IB:
1165 devlink_port->type_ib.ibdev = type_dev;
1170 spin_unlock_bh(&devlink_port->type_lock);
1171 devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
1175 * devlink_port_type_eth_set - Set port type to Ethernet
1177 * @devlink_port: devlink port
1179 * If driver is calling this, most likely it is doing something wrong.
1181 void devlink_port_type_eth_set(struct devlink_port *devlink_port)
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);
1188 EXPORT_SYMBOL_GPL(devlink_port_type_eth_set);
1191 * devlink_port_type_ib_set - Set port type to InfiniBand
1193 * @devlink_port: devlink port
1194 * @ibdev: related IB device
1196 void devlink_port_type_ib_set(struct devlink_port *devlink_port,
1197 struct ib_device *ibdev)
1199 __devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_IB, ibdev);
1201 EXPORT_SYMBOL_GPL(devlink_port_type_ib_set);
1204 * devlink_port_type_clear - Clear port type
1206 * @devlink_port: devlink port
1208 * If driver is calling this for clearing Ethernet type, most likely
1209 * it is doing something wrong.
1211 void devlink_port_type_clear(struct devlink_port *devlink_port)
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);
1219 EXPORT_SYMBOL_GPL(devlink_port_type_clear);
1221 int devlink_port_netdevice_event(struct notifier_block *nb,
1222 unsigned long event, void *ptr)
1224 struct net_device *netdev = netdev_notifier_info_to_dev(ptr);
1225 struct devlink_port *devlink_port = netdev->devlink_port;
1226 struct devlink *devlink;
1230 devlink = devlink_port->devlink;
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
1238 __devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_ETH,
1241 case NETDEV_REGISTER:
1242 case NETDEV_CHANGENAME:
1243 if (devlink_net(devlink) != dev_net(netdev))
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
1250 __devlink_port_type_set(devlink_port, devlink_port->type,
1253 case NETDEV_UNREGISTER:
1254 if (devlink_net(devlink) != dev_net(netdev))
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.
1260 __devlink_port_type_set(devlink_port, devlink_port->type,
1263 case NETDEV_PRE_UNINIT:
1264 /* Clear the type and the netdev pointer. Happens one during
1265 * netdevice unregister.
1267 __devlink_port_type_set(devlink_port, DEVLINK_PORT_TYPE_NOTSET,
1275 static int __devlink_port_attrs_set(struct devlink_port *devlink_port,
1276 enum devlink_port_flavour flavour)
1278 struct devlink_port_attrs *attrs = &devlink_port->attrs;
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;
1287 devlink_port->switch_port = false;
1293 * devlink_port_attrs_set - Set port attributes
1295 * @devlink_port: devlink port
1296 * @attrs: devlink port attrs
1298 void devlink_port_attrs_set(struct devlink_port *devlink_port,
1299 struct devlink_port_attrs *attrs)
1303 ASSERT_DEVLINK_PORT_NOT_REGISTERED(devlink_port);
1305 devlink_port->attrs = *attrs;
1306 ret = __devlink_port_attrs_set(devlink_port, attrs->flavour);
1309 WARN_ON(attrs->splittable && attrs->split);
1311 EXPORT_SYMBOL_GPL(devlink_port_attrs_set);
1314 * devlink_port_attrs_pci_pf_set - Set PCI PF port attributes
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
1321 void devlink_port_attrs_pci_pf_set(struct devlink_port *devlink_port, u32 controller,
1322 u16 pf, bool external)
1324 struct devlink_port_attrs *attrs = &devlink_port->attrs;
1327 ASSERT_DEVLINK_PORT_NOT_REGISTERED(devlink_port);
1329 ret = __devlink_port_attrs_set(devlink_port,
1330 DEVLINK_PORT_FLAVOUR_PCI_PF);
1333 attrs->pci_pf.controller = controller;
1334 attrs->pci_pf.pf = pf;
1335 attrs->pci_pf.external = external;
1337 EXPORT_SYMBOL_GPL(devlink_port_attrs_pci_pf_set);
1340 * devlink_port_attrs_pci_vf_set - Set PCI VF port attributes
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
1348 void devlink_port_attrs_pci_vf_set(struct devlink_port *devlink_port, u32 controller,
1349 u16 pf, u16 vf, bool external)
1351 struct devlink_port_attrs *attrs = &devlink_port->attrs;
1354 ASSERT_DEVLINK_PORT_NOT_REGISTERED(devlink_port);
1356 ret = __devlink_port_attrs_set(devlink_port,
1357 DEVLINK_PORT_FLAVOUR_PCI_VF);
1360 attrs->pci_vf.controller = controller;
1361 attrs->pci_vf.pf = pf;
1362 attrs->pci_vf.vf = vf;
1363 attrs->pci_vf.external = external;
1365 EXPORT_SYMBOL_GPL(devlink_port_attrs_pci_vf_set);
1368 * devlink_port_attrs_pci_sf_set - Set PCI SF port attributes
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
1376 void devlink_port_attrs_pci_sf_set(struct devlink_port *devlink_port, u32 controller,
1377 u16 pf, u32 sf, bool external)
1379 struct devlink_port_attrs *attrs = &devlink_port->attrs;
1382 ASSERT_DEVLINK_PORT_NOT_REGISTERED(devlink_port);
1384 ret = __devlink_port_attrs_set(devlink_port,
1385 DEVLINK_PORT_FLAVOUR_PCI_SF);
1388 attrs->pci_sf.controller = controller;
1389 attrs->pci_sf.pf = pf;
1390 attrs->pci_sf.sf = sf;
1391 attrs->pci_sf.external = external;
1393 EXPORT_SYMBOL_GPL(devlink_port_attrs_pci_sf_set);
1396 * devlink_port_linecard_set - Link port with a linecard
1398 * @devlink_port: devlink port
1399 * @linecard: devlink linecard
1401 void devlink_port_linecard_set(struct devlink_port *devlink_port,
1402 struct devlink_linecard *linecard)
1404 ASSERT_DEVLINK_PORT_NOT_REGISTERED(devlink_port);
1406 devlink_port->linecard = linecard;
1408 EXPORT_SYMBOL_GPL(devlink_port_linecard_set);
1410 static int __devlink_port_phys_port_name_get(struct devlink_port *devlink_port,
1411 char *name, size_t len)
1413 struct devlink_port_attrs *attrs = &devlink_port->attrs;
1416 if (!devlink_port->attrs_set)
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);
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);
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.
1439 case DEVLINK_PORT_FLAVOUR_PCI_PF:
1440 if (attrs->pci_pf.external) {
1441 n = snprintf(name, len, "c%u", attrs->pci_pf.controller);
1447 n = snprintf(name, len, "pf%u", attrs->pci_pf.pf);
1449 case DEVLINK_PORT_FLAVOUR_PCI_VF:
1450 if (attrs->pci_vf.external) {
1451 n = snprintf(name, len, "c%u", attrs->pci_vf.controller);
1457 n = snprintf(name, len, "pf%uvf%u",
1458 attrs->pci_vf.pf, attrs->pci_vf.vf);
1460 case DEVLINK_PORT_FLAVOUR_PCI_SF:
1461 if (attrs->pci_sf.external) {
1462 n = snprintf(name, len, "c%u", attrs->pci_sf.controller);
1468 n = snprintf(name, len, "pf%usf%u", attrs->pci_sf.pf,
1471 case DEVLINK_PORT_FLAVOUR_VIRTUAL:
1481 int devlink_compat_phys_port_name_get(struct net_device *dev,
1482 char *name, size_t len)
1484 struct devlink_port *devlink_port;
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.
1492 devlink_port = dev->devlink_port;
1496 return __devlink_port_phys_port_name_get(devlink_port, name, len);
1499 int devlink_compat_switch_id_get(struct net_device *dev,
1500 struct netdev_phys_item_id *ppid)
1502 struct devlink_port *devlink_port;
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.
1508 devlink_port = dev->devlink_port;
1509 if (!devlink_port || !devlink_port->switch_port)
1512 memcpy(ppid, &devlink_port->attrs.switch_id, sizeof(*ppid));