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