at commit c03c1a14
| 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* Copyright (c) 2024, Intel Corporation. */ |
| 3 | |
| 4 | #include <linux/vmalloc.h> |
| 5 | |
| 6 | #include "ice.h" |
| 7 | #include "devlink.h" |
| 8 | #include "port.h" |
| 9 | #include "ice_lib.h" |
| 10 | #include "ice_fltr.h" |
| 11 | |
| 12 | static int ice_active_port_option = -1; |
| 13 | |
| 14 | /** |
| 15 | * ice_devlink_port_opt_speed_str - convert speed to a string |
| 16 | * @speed: speed value |
| 17 | */ |
| 18 | static const char *ice_devlink_port_opt_speed_str(u8 speed) |
| 19 | { |
| 20 | switch (speed & ICE_AQC_PORT_OPT_MAX_LANE_M) { |
| 21 | case ICE_AQC_PORT_OPT_MAX_LANE_100M: |
| 22 | return "0.1"; |
| 23 | case ICE_AQC_PORT_OPT_MAX_LANE_1G: |
| 24 | return "1"; |
| 25 | case ICE_AQC_PORT_OPT_MAX_LANE_2500M: |
| 26 | return "2.5"; |
| 27 | case ICE_AQC_PORT_OPT_MAX_LANE_5G: |
| 28 | return "5"; |
| 29 | case ICE_AQC_PORT_OPT_MAX_LANE_10G: |
| 30 | return "10"; |
| 31 | case ICE_AQC_PORT_OPT_MAX_LANE_25G: |
| 32 | return "25"; |
| 33 | case ICE_AQC_PORT_OPT_MAX_LANE_40G: |
| 34 | return "40"; |
| 35 | case ICE_AQC_PORT_OPT_MAX_LANE_50G: |
| 36 | return "50"; |
| 37 | case ICE_AQC_PORT_OPT_MAX_LANE_100G: |
| 38 | return "100"; |
| 39 | } |
| 40 | |
| 41 | return "-"; |
| 42 | } |
| 43 | |
| 44 | #define ICE_PORT_OPT_DESC_LEN 50 |
| 45 | /** |
| 46 | * ice_devlink_port_options_print - Print available port split options |
| 47 | * @pf: the PF to print split port options |
| 48 | * |
| 49 | * Prints a table with available port split options and max port speeds |
| 50 | */ |
| 51 | static void ice_devlink_port_options_print(struct ice_pf *pf) |
| 52 | { |
| 53 | u8 i, j, options_count, cnt, speed, pending_idx, active_idx; |
| 54 | struct ice_aqc_get_port_options_elem *options, *opt; |
| 55 | struct device *dev = ice_pf_to_dev(pf); |
| 56 | bool active_valid, pending_valid; |
| 57 | char desc[ICE_PORT_OPT_DESC_LEN]; |
| 58 | const char *str; |
| 59 | int status; |
| 60 | |
| 61 | options = kzalloc_objs(*options, |
| 62 | ICE_AQC_PORT_OPT_MAX * ICE_MAX_PORT_PER_PCI_DEV); |
| 63 | if (!options) |
| 64 | return; |
| 65 | |
| 66 | for (i = 0; i < ICE_MAX_PORT_PER_PCI_DEV; i++) { |
| 67 | opt = options + i * ICE_AQC_PORT_OPT_MAX; |
| 68 | options_count = ICE_AQC_PORT_OPT_MAX; |
| 69 | active_valid = 0; |
| 70 | |
| 71 | status = ice_aq_get_port_options(&pf->hw, opt, &options_count, |
| 72 | i, true, &active_idx, |
| 73 | &active_valid, &pending_idx, |
| 74 | &pending_valid); |
| 75 | if (status) { |
| 76 | dev_dbg(dev, "Couldn't read port option for port %d, err %d\n", |
| 77 | i, status); |
| 78 | goto err; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | dev_dbg(dev, "Available port split options and max port speeds (Gbps):\n"); |
| 83 | dev_dbg(dev, "Status Split Quad 0 Quad 1\n"); |
| 84 | dev_dbg(dev, " count L0 L1 L2 L3 L4 L5 L6 L7\n"); |
| 85 | |
| 86 | for (i = 0; i < options_count; i++) { |
| 87 | cnt = 0; |
| 88 | |
| 89 | if (i == ice_active_port_option) |
| 90 | str = "Active"; |
| 91 | else if ((i == pending_idx) && pending_valid) |
| 92 | str = "Pending"; |
| 93 | else |
| 94 | str = ""; |
| 95 | |
| 96 | cnt += snprintf(&desc[cnt], ICE_PORT_OPT_DESC_LEN - cnt, |
| 97 | "%-8s", str); |
| 98 | |
| 99 | cnt += snprintf(&desc[cnt], ICE_PORT_OPT_DESC_LEN - cnt, |
| 100 | "%-6u", options[i].pmd); |
| 101 | |
| 102 | for (j = 0; j < ICE_MAX_PORT_PER_PCI_DEV; ++j) { |
| 103 | speed = options[i + j * ICE_AQC_PORT_OPT_MAX].max_lane_speed; |
| 104 | str = ice_devlink_port_opt_speed_str(speed); |
| 105 | cnt += snprintf(&desc[cnt], ICE_PORT_OPT_DESC_LEN - cnt, |
| 106 | "%3s ", str); |
| 107 | } |
| 108 | |
| 109 | dev_dbg(dev, "%s\n", desc); |
| 110 | } |
| 111 | |
| 112 | err: |
| 113 | kfree(options); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * ice_devlink_aq_set_port_option - Send set port option admin queue command |
| 118 | * @pf: the PF to print split port options |
| 119 | * @option_idx: selected port option |
| 120 | * @extack: extended netdev ack structure |
| 121 | * |
| 122 | * Sends set port option admin queue command with selected port option and |
| 123 | * calls NVM write activate. |
| 124 | */ |
| 125 | static int |
| 126 | ice_devlink_aq_set_port_option(struct ice_pf *pf, u8 option_idx, |
| 127 | struct netlink_ext_ack *extack) |
| 128 | { |
| 129 | struct device *dev = ice_pf_to_dev(pf); |
| 130 | int status; |
| 131 | |
| 132 | status = ice_aq_set_port_option(&pf->hw, 0, true, option_idx); |
| 133 | if (status) { |
| 134 | dev_dbg(dev, "ice_aq_set_port_option, err %d aq_err %d\n", |
| 135 | status, pf->hw.adminq.sq_last_status); |
| 136 | NL_SET_ERR_MSG_MOD(extack, "Port split request failed"); |
| 137 | return -EIO; |
| 138 | } |
| 139 | |
| 140 | status = ice_acquire_nvm(&pf->hw, ICE_RES_WRITE); |
| 141 | if (status) { |
| 142 | dev_dbg(dev, "ice_acquire_nvm failed, err %d aq_err %d\n", |
| 143 | status, pf->hw.adminq.sq_last_status); |
| 144 | NL_SET_ERR_MSG_MOD(extack, "Failed to acquire NVM semaphore"); |
| 145 | return -EIO; |
| 146 | } |
| 147 | |
| 148 | status = ice_nvm_write_activate(&pf->hw, ICE_AQC_NVM_ACTIV_REQ_EMPR, NULL); |
| 149 | if (status) { |
| 150 | dev_dbg(dev, "ice_nvm_write_activate failed, err %d aq_err %d\n", |
| 151 | status, pf->hw.adminq.sq_last_status); |
| 152 | NL_SET_ERR_MSG_MOD(extack, "Port split request failed to save data"); |
| 153 | ice_release_nvm(&pf->hw); |
| 154 | return -EIO; |
| 155 | } |
| 156 | |
| 157 | ice_release_nvm(&pf->hw); |
| 158 | |
| 159 | NL_SET_ERR_MSG_MOD(extack, "Reboot required to finish port split"); |
| 160 | return 0; |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * ice_devlink_port_split - .port_split devlink handler |
| 165 | * @devlink: devlink instance structure |
| 166 | * @port: devlink port structure |
| 167 | * @count: number of ports to split to |
| 168 | * @extack: extended netdev ack structure |
| 169 | * |
| 170 | * Callback for the devlink .port_split operation. |
| 171 | * |
| 172 | * Unfortunately, the devlink expression of available options is limited |
| 173 | * to just a number, so search for an FW port option which supports |
| 174 | * the specified number. As there could be multiple FW port options with |
| 175 | * the same port split count, allow switching between them. When the same |
| 176 | * port split count request is issued again, switch to the next FW port |
| 177 | * option with the same port split count. |
| 178 | * |
| 179 | * Return: zero on success or an error code on failure. |
| 180 | */ |
| 181 | static int |
| 182 | ice_devlink_port_split(struct devlink *devlink, struct devlink_port *port, |
| 183 | unsigned int count, struct netlink_ext_ack *extack) |
| 184 | { |
| 185 | struct ice_aqc_get_port_options_elem options[ICE_AQC_PORT_OPT_MAX]; |
| 186 | u8 i, j, active_idx, pending_idx, new_option; |
| 187 | struct ice_pf *pf = devlink_priv(devlink); |
| 188 | u8 option_count = ICE_AQC_PORT_OPT_MAX; |
| 189 | struct device *dev = ice_pf_to_dev(pf); |
| 190 | bool active_valid, pending_valid; |
| 191 | int status; |
| 192 | |
| 193 | status = ice_aq_get_port_options(&pf->hw, options, &option_count, |
| 194 | 0, true, &active_idx, &active_valid, |
| 195 | &pending_idx, &pending_valid); |
| 196 | if (status) { |
| 197 | dev_dbg(dev, "Couldn't read port split options, err = %d\n", |
| 198 | status); |
| 199 | NL_SET_ERR_MSG_MOD(extack, "Failed to get available port split options"); |
| 200 | return -EIO; |
| 201 | } |
| 202 | |
| 203 | new_option = ICE_AQC_PORT_OPT_MAX; |
| 204 | active_idx = pending_valid ? pending_idx : active_idx; |
| 205 | for (i = 1; i <= option_count; i++) { |
| 206 | /* In order to allow switching between FW port options with |
| 207 | * the same port split count, search for a new option starting |
| 208 | * from the active/pending option (with array wrap around). |
| 209 | */ |
| 210 | j = (active_idx + i) % option_count; |
| 211 | |
| 212 | if (count == options[j].pmd) { |
| 213 | new_option = j; |
| 214 | break; |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | if (new_option == active_idx) { |
| 219 | dev_dbg(dev, "request to split: count: %u is already set and there are no other options\n", |
| 220 | count); |
| 221 | NL_SET_ERR_MSG_MOD(extack, "Requested split count is already set"); |
| 222 | ice_devlink_port_options_print(pf); |
| 223 | return -EINVAL; |
| 224 | } |
| 225 | |
| 226 | if (new_option == ICE_AQC_PORT_OPT_MAX) { |
| 227 | dev_dbg(dev, "request to split: count: %u not found\n", count); |
| 228 | NL_SET_ERR_MSG_MOD(extack, "Port split requested unsupported port config"); |
| 229 | ice_devlink_port_options_print(pf); |
| 230 | return -EINVAL; |
| 231 | } |
| 232 | |
| 233 | status = ice_devlink_aq_set_port_option(pf, new_option, extack); |
| 234 | if (status) |
| 235 | return status; |
| 236 | |
| 237 | ice_devlink_port_options_print(pf); |
| 238 | |
| 239 | return 0; |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * ice_devlink_port_unsplit - .port_unsplit devlink handler |
| 244 | * @devlink: devlink instance structure |
| 245 | * @port: devlink port structure |
| 246 | * @extack: extended netdev ack structure |
| 247 | * |
| 248 | * Callback for the devlink .port_unsplit operation. |
| 249 | * Calls ice_devlink_port_split with split count set to 1. |
| 250 | * There could be no FW option available with split count 1. |
| 251 | * |
| 252 | * Return: zero on success or an error code on failure. |
| 253 | */ |
| 254 | static int |
| 255 | ice_devlink_port_unsplit(struct devlink *devlink, struct devlink_port *port, |
| 256 | struct netlink_ext_ack *extack) |
| 257 | { |
| 258 | return ice_devlink_port_split(devlink, port, 1, extack); |
| 259 | } |
| 260 | |
| 261 | /** |
| 262 | * ice_devlink_set_port_split_options - Set port split options |
| 263 | * @pf: the PF to set port split options |
| 264 | * @attrs: devlink attributes |
| 265 | * |
| 266 | * Sets devlink port split options based on available FW port options |
| 267 | */ |
| 268 | static void |
| 269 | ice_devlink_set_port_split_options(struct ice_pf *pf, |
| 270 | struct devlink_port_attrs *attrs) |
| 271 | { |
| 272 | struct ice_aqc_get_port_options_elem options[ICE_AQC_PORT_OPT_MAX]; |
| 273 | u8 i, active_idx, pending_idx, option_count = ICE_AQC_PORT_OPT_MAX; |
| 274 | bool active_valid, pending_valid; |
| 275 | int status; |
| 276 | |
| 277 | status = ice_aq_get_port_options(&pf->hw, options, &option_count, |
| 278 | 0, true, &active_idx, &active_valid, |
| 279 | &pending_idx, &pending_valid); |
| 280 | if (status) { |
| 281 | dev_dbg(ice_pf_to_dev(pf), "Couldn't read port split options, err = %d\n", |
| 282 | status); |
| 283 | return; |
| 284 | } |
| 285 | |
| 286 | /* find the biggest available port split count */ |
| 287 | for (i = 0; i < option_count; i++) |
| 288 | attrs->lanes = max_t(int, attrs->lanes, options[i].pmd); |
| 289 | |
| 290 | attrs->splittable = attrs->lanes ? 1 : 0; |
| 291 | ice_active_port_option = active_idx; |
| 292 | } |
| 293 | |
| 294 | static const struct devlink_port_ops ice_devlink_port_ops = { |
| 295 | .port_split = ice_devlink_port_split, |
| 296 | .port_unsplit = ice_devlink_port_unsplit, |
| 297 | }; |
| 298 | |
| 299 | /** |
| 300 | * ice_devlink_set_switch_id - Set unique switch id based on pci dsn |
| 301 | * @pf: the PF to create a devlink port for |
| 302 | * @ppid: struct with switch id information |
| 303 | */ |
| 304 | static void |
| 305 | ice_devlink_set_switch_id(struct ice_pf *pf, struct netdev_phys_item_id *ppid) |
| 306 | { |
| 307 | struct pci_dev *pdev = pf->pdev; |
| 308 | u64 id; |
| 309 | |
| 310 | id = pci_get_dsn(pdev); |
| 311 | |
| 312 | ppid->id_len = sizeof(id); |
| 313 | put_unaligned_be64(id, &ppid->id); |
| 314 | } |
| 315 | |
| 316 | /** |
| 317 | * ice_devlink_create_pf_port - Create a devlink port for this PF |
| 318 | * @pf: the PF to create a devlink port for |
| 319 | * |
| 320 | * Create and register a devlink_port for this PF. |
| 321 | * This function has to be called under devl_lock. |
| 322 | * |
| 323 | * Return: zero on success or an error code on failure. |
| 324 | */ |
| 325 | int ice_devlink_create_pf_port(struct ice_pf *pf) |
| 326 | { |
| 327 | struct devlink_port_attrs attrs = {}; |
| 328 | struct devlink_port *devlink_port; |
| 329 | struct devlink *devlink; |
| 330 | struct ice_vsi *vsi; |
| 331 | struct device *dev; |
| 332 | int err; |
| 333 | |
| 334 | devlink = priv_to_devlink(pf); |
| 335 | |
| 336 | dev = ice_pf_to_dev(pf); |
| 337 | |
| 338 | devlink_port = &pf->devlink_port; |
| 339 | |
| 340 | vsi = ice_get_main_vsi(pf); |
| 341 | if (!vsi) |
| 342 | return -EIO; |
| 343 | |
| 344 | attrs.flavour = DEVLINK_PORT_FLAVOUR_PHYSICAL; |
| 345 | attrs.phys.port_number = pf->hw.pf_id; |
| 346 | |
| 347 | /* As FW supports only port split options for whole device, |
| 348 | * set port split options only for first PF. |
| 349 | */ |
| 350 | if (pf->hw.pf_id == 0) |
| 351 | ice_devlink_set_port_split_options(pf, &attrs); |
| 352 | |
| 353 | ice_devlink_set_switch_id(pf, &attrs.switch_id); |
| 354 | |
| 355 | devlink_port_attrs_set(devlink_port, &attrs); |
| 356 | |
| 357 | err = devl_port_register_with_ops(devlink, devlink_port, vsi->idx, |
| 358 | &ice_devlink_port_ops); |
| 359 | if (err) { |
| 360 | dev_err(dev, "Failed to create devlink port for PF %d, error %d\n", |
| 361 | pf->hw.pf_id, err); |
| 362 | return err; |
| 363 | } |
| 364 | |
| 365 | return 0; |
| 366 | } |
| 367 | |
| 368 | /** |
| 369 | * ice_devlink_destroy_pf_port - Destroy the devlink_port for this PF |
| 370 | * @pf: the PF to cleanup |
| 371 | * |
| 372 | * Unregisters the devlink_port structure associated with this PF. |
| 373 | * This function has to be called under devl_lock. |
| 374 | */ |
| 375 | void ice_devlink_destroy_pf_port(struct ice_pf *pf) |
| 376 | { |
| 377 | devl_port_unregister(&pf->devlink_port); |
| 378 | } |
| 379 | |
| 380 | /** |
| 381 | * ice_devlink_port_get_vf_fn_mac - .port_fn_hw_addr_get devlink handler |
| 382 | * @port: devlink port structure |
| 383 | * @hw_addr: MAC address of the port |
| 384 | * @hw_addr_len: length of MAC address |
| 385 | * @extack: extended netdev ack structure |
| 386 | * |
| 387 | * Callback for the devlink .port_fn_hw_addr_get operation |
| 388 | * Return: zero on success or an error code on failure. |
| 389 | */ |
| 390 | static int ice_devlink_port_get_vf_fn_mac(struct devlink_port *port, |
| 391 | u8 *hw_addr, int *hw_addr_len, |
| 392 | struct netlink_ext_ack *extack) |
| 393 | { |
| 394 | struct ice_vf *vf = container_of(port, struct ice_vf, devlink_port); |
| 395 | |
| 396 | ether_addr_copy(hw_addr, vf->dev_lan_addr); |
| 397 | *hw_addr_len = ETH_ALEN; |
| 398 | |
| 399 | return 0; |
| 400 | } |
| 401 | |
| 402 | /** |
| 403 | * ice_devlink_port_set_vf_fn_mac - .port_fn_hw_addr_set devlink handler |
| 404 | * @port: devlink port structure |
| 405 | * @hw_addr: MAC address of the port |
| 406 | * @hw_addr_len: length of MAC address |
| 407 | * @extack: extended netdev ack structure |
| 408 | * |
| 409 | * Callback for the devlink .port_fn_hw_addr_set operation |
| 410 | * Return: zero on success or an error code on failure. |
| 411 | */ |
| 412 | static int ice_devlink_port_set_vf_fn_mac(struct devlink_port *port, |
| 413 | const u8 *hw_addr, |
| 414 | int hw_addr_len, |
| 415 | struct netlink_ext_ack *extack) |
| 416 | |
| 417 | { |
| 418 | struct devlink_port_attrs *attrs = &port->attrs; |
| 419 | struct devlink_port_pci_vf_attrs *pci_vf; |
| 420 | struct devlink *devlink = port->devlink; |
| 421 | struct ice_pf *pf; |
| 422 | u16 vf_id; |
| 423 | |
| 424 | pf = devlink_priv(devlink); |
| 425 | pci_vf = &attrs->pci_vf; |
| 426 | vf_id = pci_vf->vf; |
| 427 | |
| 428 | return __ice_set_vf_mac(pf, vf_id, hw_addr); |
| 429 | } |
| 430 | |
| 431 | static const struct devlink_port_ops ice_devlink_vf_port_ops = { |
| 432 | .port_fn_hw_addr_get = ice_devlink_port_get_vf_fn_mac, |
| 433 | .port_fn_hw_addr_set = ice_devlink_port_set_vf_fn_mac, |
| 434 | }; |
| 435 | |
| 436 | /** |
| 437 | * ice_devlink_create_vf_port - Create a devlink port for this VF |
| 438 | * @vf: the VF to create a port for |
| 439 | * |
| 440 | * Create and register a devlink_port for this VF. |
| 441 | * |
| 442 | * Return: zero on success or an error code on failure. |
| 443 | */ |
| 444 | int ice_devlink_create_vf_port(struct ice_vf *vf) |
| 445 | { |
| 446 | struct devlink_port_attrs attrs = {}; |
| 447 | struct devlink_port *devlink_port; |
| 448 | struct devlink *devlink; |
| 449 | struct ice_vsi *vsi; |
| 450 | struct device *dev; |
| 451 | struct ice_pf *pf; |
| 452 | int err; |
| 453 | |
| 454 | pf = vf->pf; |
| 455 | dev = ice_pf_to_dev(pf); |
| 456 | devlink_port = &vf->devlink_port; |
| 457 | |
| 458 | vsi = ice_get_vf_vsi(vf); |
| 459 | if (!vsi) |
| 460 | return -EINVAL; |
| 461 | |
| 462 | attrs.flavour = DEVLINK_PORT_FLAVOUR_PCI_VF; |
| 463 | attrs.pci_vf.pf = pf->hw.pf_id; |
| 464 | attrs.pci_vf.vf = vf->vf_id; |
| 465 | |
| 466 | ice_devlink_set_switch_id(pf, &attrs.switch_id); |
| 467 | |
| 468 | devlink_port_attrs_set(devlink_port, &attrs); |
| 469 | devlink = priv_to_devlink(pf); |
| 470 | |
| 471 | err = devl_port_register_with_ops(devlink, devlink_port, vsi->idx, |
| 472 | &ice_devlink_vf_port_ops); |
| 473 | if (err) { |
| 474 | dev_err(dev, "Failed to create devlink port for VF %d, error %d\n", |
| 475 | vf->vf_id, err); |
| 476 | return err; |
| 477 | } |
| 478 | |
| 479 | return 0; |
| 480 | } |
| 481 | |
| 482 | /** |
| 483 | * ice_devlink_destroy_vf_port - Destroy the devlink_port for this VF |
| 484 | * @vf: the VF to cleanup |
| 485 | * |
| 486 | * Unregisters the devlink_port structure associated with this VF. |
| 487 | */ |
| 488 | void ice_devlink_destroy_vf_port(struct ice_vf *vf) |
| 489 | { |
| 490 | devl_rate_leaf_destroy(&vf->devlink_port); |
| 491 | devl_port_unregister(&vf->devlink_port); |
| 492 | } |
| 493 | |
| 494 | /** |
| 495 | * ice_devlink_create_sf_dev_port - Register virtual port for a subfunction |
| 496 | * @sf_dev: the subfunction device to create a devlink port for |
| 497 | * |
| 498 | * Register virtual flavour devlink port for the subfunction auxiliary device |
| 499 | * created after activating a dynamically added devlink port. |
| 500 | * |
| 501 | * Return: zero on success or an error code on failure. |
| 502 | */ |
| 503 | int ice_devlink_create_sf_dev_port(struct ice_sf_dev *sf_dev) |
| 504 | { |
| 505 | struct devlink_port_attrs attrs = {}; |
| 506 | struct ice_dynamic_port *dyn_port; |
| 507 | struct devlink_port *devlink_port; |
| 508 | struct devlink *devlink; |
| 509 | struct ice_vsi *vsi; |
| 510 | |
| 511 | dyn_port = sf_dev->dyn_port; |
| 512 | vsi = dyn_port->vsi; |
| 513 | |
| 514 | devlink_port = &sf_dev->priv->devlink_port; |
| 515 | |
| 516 | attrs.flavour = DEVLINK_PORT_FLAVOUR_VIRTUAL; |
| 517 | |
| 518 | devlink_port_attrs_set(devlink_port, &attrs); |
| 519 | devlink = priv_to_devlink(sf_dev->priv); |
| 520 | |
| 521 | return devl_port_register(devlink, devlink_port, vsi->idx); |
| 522 | } |
| 523 | |
| 524 | /** |
| 525 | * ice_devlink_destroy_sf_dev_port - Destroy virtual port for a subfunction |
| 526 | * @sf_dev: the subfunction device to create a devlink port for |
| 527 | * |
| 528 | * Unregisters the virtual port associated with this subfunction. |
| 529 | */ |
| 530 | void ice_devlink_destroy_sf_dev_port(struct ice_sf_dev *sf_dev) |
| 531 | { |
| 532 | devl_port_unregister(&sf_dev->priv->devlink_port); |
| 533 | } |
| 534 | |
| 535 | /** |
| 536 | * ice_activate_dynamic_port - Activate a dynamic port |
| 537 | * @dyn_port: dynamic port instance to activate |
| 538 | * @extack: extack for reporting error messages |
| 539 | * |
| 540 | * Activate the dynamic port based on its flavour. |
| 541 | * |
| 542 | * Return: zero on success or an error code on failure. |
| 543 | */ |
| 544 | static int |
| 545 | ice_activate_dynamic_port(struct ice_dynamic_port *dyn_port, |
| 546 | struct netlink_ext_ack *extack) |
| 547 | { |
| 548 | int err; |
| 549 | |
| 550 | if (dyn_port->active) |
| 551 | return 0; |
| 552 | |
| 553 | err = ice_sf_eth_activate(dyn_port, extack); |
| 554 | if (err) |
| 555 | return err; |
| 556 | |
| 557 | dyn_port->active = true; |
| 558 | |
| 559 | return 0; |
| 560 | } |
| 561 | |
| 562 | /** |
| 563 | * ice_deactivate_dynamic_port - Deactivate a dynamic port |
| 564 | * @dyn_port: dynamic port instance to deactivate |
| 565 | * |
| 566 | * Undo activation of a dynamic port. |
| 567 | */ |
| 568 | static void ice_deactivate_dynamic_port(struct ice_dynamic_port *dyn_port) |
| 569 | { |
| 570 | if (!dyn_port->active) |
| 571 | return; |
| 572 | |
| 573 | ice_sf_eth_deactivate(dyn_port); |
| 574 | dyn_port->active = false; |
| 575 | } |
| 576 | |
| 577 | /** |
| 578 | * ice_dealloc_dynamic_port - Deallocate and remove a dynamic port |
| 579 | * @dyn_port: dynamic port instance to deallocate |
| 580 | * |
| 581 | * Free resources associated with a dynamically added devlink port. Will |
| 582 | * deactivate the port if its currently active. |
| 583 | */ |
| 584 | static void ice_dealloc_dynamic_port(struct ice_dynamic_port *dyn_port) |
| 585 | { |
| 586 | struct devlink_port *devlink_port = &dyn_port->devlink_port; |
| 587 | struct ice_pf *pf = dyn_port->pf; |
| 588 | |
| 589 | ice_deactivate_dynamic_port(dyn_port); |
| 590 | |
| 591 | xa_erase(&pf->sf_nums, devlink_port->attrs.pci_sf.sf); |
| 592 | ice_eswitch_detach_sf(pf, dyn_port); |
| 593 | xa_erase(&pf->dyn_ports, dyn_port->vsi->idx); |
| 594 | ice_vsi_free(dyn_port->vsi); |
| 595 | kfree(dyn_port); |
| 596 | } |
| 597 | |
| 598 | /** |
| 599 | * ice_dealloc_all_dynamic_ports - Deallocate all dynamic devlink ports |
| 600 | * @pf: pointer to the pf structure |
| 601 | */ |
| 602 | void ice_dealloc_all_dynamic_ports(struct ice_pf *pf) |
| 603 | { |
| 604 | struct ice_dynamic_port *dyn_port; |
| 605 | unsigned long index; |
| 606 | |
| 607 | xa_for_each(&pf->dyn_ports, index, dyn_port) |
| 608 | ice_dealloc_dynamic_port(dyn_port); |
| 609 | } |
| 610 | |
| 611 | /** |
| 612 | * ice_devlink_port_new_check_attr - Check that new port attributes are valid |
| 613 | * @pf: pointer to the PF structure |
| 614 | * @new_attr: the attributes for the new port |
| 615 | * @extack: extack for reporting error messages |
| 616 | * |
| 617 | * Check that the attributes for the new port are valid before continuing to |
| 618 | * allocate the devlink port. |
| 619 | * |
| 620 | * Return: zero on success or an error code on failure. |
| 621 | */ |
| 622 | static int |
| 623 | ice_devlink_port_new_check_attr(struct ice_pf *pf, |
| 624 | const struct devlink_port_new_attrs *new_attr, |
| 625 | struct netlink_ext_ack *extack) |
| 626 | { |
| 627 | if (new_attr->flavour != DEVLINK_PORT_FLAVOUR_PCI_SF) { |
| 628 | NL_SET_ERR_MSG_MOD(extack, "Flavour other than pcisf is not supported"); |
| 629 | return -EOPNOTSUPP; |
| 630 | } |
| 631 | |
| 632 | if (new_attr->controller_valid) { |
| 633 | NL_SET_ERR_MSG_MOD(extack, "Setting controller is not supported"); |
| 634 | return -EOPNOTSUPP; |
| 635 | } |
| 636 | |
| 637 | if (new_attr->port_index_valid) { |
| 638 | NL_SET_ERR_MSG_MOD(extack, "Driver does not support user defined port index assignment"); |
| 639 | return -EOPNOTSUPP; |
| 640 | } |
| 641 | |
| 642 | if (new_attr->pfnum != pf->hw.pf_id) { |
| 643 | NL_SET_ERR_MSG_MOD(extack, "Incorrect pfnum supplied"); |
| 644 | return -EINVAL; |
| 645 | } |
| 646 | |
| 647 | if (!pci_msix_can_alloc_dyn(pf->pdev)) { |
| 648 | NL_SET_ERR_MSG_MOD(extack, "Dynamic MSIX-X interrupt allocation is not supported"); |
| 649 | return -EOPNOTSUPP; |
| 650 | } |
| 651 | |
| 652 | return 0; |
| 653 | } |
| 654 | |
| 655 | /** |
| 656 | * ice_devlink_port_del - devlink handler for port delete |
| 657 | * @devlink: pointer to devlink |
| 658 | * @port: devlink port to be deleted |
| 659 | * @extack: pointer to extack |
| 660 | * |
| 661 | * Deletes devlink port and deallocates all resources associated with |
| 662 | * created subfunction. |
| 663 | * |
| 664 | * Return: zero on success or an error code on failure. |
| 665 | */ |
| 666 | static int |
| 667 | ice_devlink_port_del(struct devlink *devlink, struct devlink_port *port, |
| 668 | struct netlink_ext_ack *extack) |
| 669 | { |
| 670 | struct ice_dynamic_port *dyn_port; |
| 671 | |
| 672 | dyn_port = ice_devlink_port_to_dyn(port); |
| 673 | ice_dealloc_dynamic_port(dyn_port); |
| 674 | |
| 675 | return 0; |
| 676 | } |
| 677 | |
| 678 | /** |
| 679 | * ice_devlink_port_fn_hw_addr_set - devlink handler for mac address set |
| 680 | * @port: pointer to devlink port |
| 681 | * @hw_addr: hw address to set |
| 682 | * @hw_addr_len: hw address length |
| 683 | * @extack: extack for reporting error messages |
| 684 | * |
| 685 | * Sets mac address for the port, verifies arguments and copies address |
| 686 | * to the subfunction structure. |
| 687 | * |
| 688 | * Return: zero on success or an error code on failure. |
| 689 | */ |
| 690 | static int |
| 691 | ice_devlink_port_fn_hw_addr_set(struct devlink_port *port, const u8 *hw_addr, |
| 692 | int hw_addr_len, |
| 693 | struct netlink_ext_ack *extack) |
| 694 | { |
| 695 | struct ice_dynamic_port *dyn_port; |
| 696 | |
| 697 | dyn_port = ice_devlink_port_to_dyn(port); |
| 698 | |
| 699 | if (dyn_port->attached) { |
| 700 | NL_SET_ERR_MSG_MOD(extack, |
| 701 | "Ethernet address can be change only in detached state"); |
| 702 | return -EBUSY; |
| 703 | } |
| 704 | |
| 705 | if (hw_addr_len != ETH_ALEN || !is_valid_ether_addr(hw_addr)) { |
| 706 | NL_SET_ERR_MSG_MOD(extack, "Invalid ethernet address"); |
| 707 | return -EADDRNOTAVAIL; |
| 708 | } |
| 709 | |
| 710 | ether_addr_copy(dyn_port->hw_addr, hw_addr); |
| 711 | |
| 712 | return 0; |
| 713 | } |
| 714 | |
| 715 | /** |
| 716 | * ice_devlink_port_fn_hw_addr_get - devlink handler for mac address get |
| 717 | * @port: pointer to devlink port |
| 718 | * @hw_addr: hw address to set |
| 719 | * @hw_addr_len: hw address length |
| 720 | * @extack: extack for reporting error messages |
| 721 | * |
| 722 | * Returns mac address for the port. |
| 723 | * |
| 724 | * Return: zero on success or an error code on failure. |
| 725 | */ |
| 726 | static int |
| 727 | ice_devlink_port_fn_hw_addr_get(struct devlink_port *port, u8 *hw_addr, |
| 728 | int *hw_addr_len, |
| 729 | struct netlink_ext_ack *extack) |
| 730 | { |
| 731 | struct ice_dynamic_port *dyn_port; |
| 732 | |
| 733 | dyn_port = ice_devlink_port_to_dyn(port); |
| 734 | |
| 735 | ether_addr_copy(hw_addr, dyn_port->hw_addr); |
| 736 | *hw_addr_len = ETH_ALEN; |
| 737 | |
| 738 | return 0; |
| 739 | } |
| 740 | |
| 741 | /** |
| 742 | * ice_devlink_port_fn_state_set - devlink handler for port state set |
| 743 | * @port: pointer to devlink port |
| 744 | * @state: state to set |
| 745 | * @extack: extack for reporting error messages |
| 746 | * |
| 747 | * Activates or deactivates the port. |
| 748 | * |
| 749 | * Return: zero on success or an error code on failure. |
| 750 | */ |
| 751 | static int |
| 752 | ice_devlink_port_fn_state_set(struct devlink_port *port, |
| 753 | enum devlink_port_fn_state state, |
| 754 | struct netlink_ext_ack *extack) |
| 755 | { |
| 756 | struct ice_dynamic_port *dyn_port; |
| 757 | |
| 758 | dyn_port = ice_devlink_port_to_dyn(port); |
| 759 | |
| 760 | switch (state) { |
| 761 | case DEVLINK_PORT_FN_STATE_ACTIVE: |
| 762 | return ice_activate_dynamic_port(dyn_port, extack); |
| 763 | |
| 764 | case DEVLINK_PORT_FN_STATE_INACTIVE: |
| 765 | ice_deactivate_dynamic_port(dyn_port); |
| 766 | break; |
| 767 | } |
| 768 | |
| 769 | return 0; |
| 770 | } |
| 771 | |
| 772 | /** |
| 773 | * ice_devlink_port_fn_state_get - devlink handler for port state get |
| 774 | * @port: pointer to devlink port |
| 775 | * @state: admin configured state of the port |
| 776 | * @opstate: current port operational state |
| 777 | * @extack: extack for reporting error messages |
| 778 | * |
| 779 | * Gets port state. |
| 780 | * |
| 781 | * Return: zero on success or an error code on failure. |
| 782 | */ |
| 783 | static int |
| 784 | ice_devlink_port_fn_state_get(struct devlink_port *port, |
| 785 | enum devlink_port_fn_state *state, |
| 786 | enum devlink_port_fn_opstate *opstate, |
| 787 | struct netlink_ext_ack *extack) |
| 788 | { |
| 789 | struct ice_dynamic_port *dyn_port; |
| 790 | |
| 791 | dyn_port = ice_devlink_port_to_dyn(port); |
| 792 | |
| 793 | if (dyn_port->active) |
| 794 | *state = DEVLINK_PORT_FN_STATE_ACTIVE; |
| 795 | else |
| 796 | *state = DEVLINK_PORT_FN_STATE_INACTIVE; |
| 797 | |
| 798 | if (dyn_port->attached) |
| 799 | *opstate = DEVLINK_PORT_FN_OPSTATE_ATTACHED; |
| 800 | else |
| 801 | *opstate = DEVLINK_PORT_FN_OPSTATE_DETACHED; |
| 802 | |
| 803 | return 0; |
| 804 | } |
| 805 | |
| 806 | static const struct devlink_port_ops ice_devlink_port_sf_ops = { |
| 807 | .port_del = ice_devlink_port_del, |
| 808 | .port_fn_hw_addr_get = ice_devlink_port_fn_hw_addr_get, |
| 809 | .port_fn_hw_addr_set = ice_devlink_port_fn_hw_addr_set, |
| 810 | .port_fn_state_get = ice_devlink_port_fn_state_get, |
| 811 | .port_fn_state_set = ice_devlink_port_fn_state_set, |
| 812 | }; |
| 813 | |
| 814 | /** |
| 815 | * ice_reserve_sf_num - Reserve a subfunction number for this port |
| 816 | * @pf: pointer to the pf structure |
| 817 | * @new_attr: devlink port attributes requested |
| 818 | * @extack: extack for reporting error messages |
| 819 | * @sfnum: on success, the sf number reserved |
| 820 | * |
| 821 | * Reserve a subfunction number for this port. Only called for |
| 822 | * DEVLINK_PORT_FLAVOUR_PCI_SF ports. |
| 823 | * |
| 824 | * Return: zero on success or an error code on failure. |
| 825 | */ |
| 826 | static int |
| 827 | ice_reserve_sf_num(struct ice_pf *pf, |
| 828 | const struct devlink_port_new_attrs *new_attr, |
| 829 | struct netlink_ext_ack *extack, u32 *sfnum) |
| 830 | { |
| 831 | int err; |
| 832 | |
| 833 | /* If user didn't request an explicit number, pick one */ |
| 834 | if (!new_attr->sfnum_valid) |
| 835 | return xa_alloc(&pf->sf_nums, sfnum, NULL, xa_limit_32b, |
| 836 | GFP_KERNEL); |
| 837 | |
| 838 | /* Otherwise, check and use the number provided */ |
| 839 | err = xa_insert(&pf->sf_nums, new_attr->sfnum, NULL, GFP_KERNEL); |
| 840 | if (err) { |
| 841 | if (err == -EBUSY) |
| 842 | NL_SET_ERR_MSG_MOD(extack, "Subfunction with given sfnum already exists"); |
| 843 | return err; |
| 844 | } |
| 845 | |
| 846 | *sfnum = new_attr->sfnum; |
| 847 | |
| 848 | return 0; |
| 849 | } |
| 850 | |
| 851 | /** |
| 852 | * ice_devlink_create_sf_port - Register PCI subfunction devlink port |
| 853 | * @dyn_port: the dynamic port instance structure for this subfunction |
| 854 | * |
| 855 | * Register PCI subfunction flavour devlink port for a dynamically added |
| 856 | * subfunction port. |
| 857 | * |
| 858 | * Return: zero on success or an error code on failure. |
| 859 | */ |
| 860 | int ice_devlink_create_sf_port(struct ice_dynamic_port *dyn_port) |
| 861 | { |
| 862 | struct devlink_port_attrs attrs = {}; |
| 863 | struct devlink_port *devlink_port; |
| 864 | struct devlink *devlink; |
| 865 | struct ice_vsi *vsi; |
| 866 | struct ice_pf *pf; |
| 867 | |
| 868 | vsi = dyn_port->vsi; |
| 869 | pf = dyn_port->pf; |
| 870 | |
| 871 | devlink_port = &dyn_port->devlink_port; |
| 872 | |
| 873 | attrs.flavour = DEVLINK_PORT_FLAVOUR_PCI_SF; |
| 874 | attrs.pci_sf.pf = pf->hw.pf_id; |
| 875 | attrs.pci_sf.sf = dyn_port->sfnum; |
| 876 | |
| 877 | devlink_port_attrs_set(devlink_port, &attrs); |
| 878 | devlink = priv_to_devlink(pf); |
| 879 | |
| 880 | return devl_port_register_with_ops(devlink, devlink_port, vsi->idx, |
| 881 | &ice_devlink_port_sf_ops); |
| 882 | } |
| 883 | |
| 884 | /** |
| 885 | * ice_devlink_destroy_sf_port - Destroy the devlink_port for this SF |
| 886 | * @dyn_port: the dynamic port instance structure for this subfunction |
| 887 | * |
| 888 | * Unregisters the devlink_port structure associated with this SF. |
| 889 | */ |
| 890 | void ice_devlink_destroy_sf_port(struct ice_dynamic_port *dyn_port) |
| 891 | { |
| 892 | devl_rate_leaf_destroy(&dyn_port->devlink_port); |
| 893 | devl_port_unregister(&dyn_port->devlink_port); |
| 894 | } |
| 895 | |
| 896 | /** |
| 897 | * ice_alloc_dynamic_port - Allocate new dynamic port |
| 898 | * @pf: pointer to the pf structure |
| 899 | * @new_attr: devlink port attributes requested |
| 900 | * @extack: extack for reporting error messages |
| 901 | * @devlink_port: index of newly created devlink port |
| 902 | * |
| 903 | * Allocate a new dynamic port instance and prepare it for configuration |
| 904 | * with devlink. |
| 905 | * |
| 906 | * Return: zero on success or an error code on failure. |
| 907 | */ |
| 908 | static int |
| 909 | ice_alloc_dynamic_port(struct ice_pf *pf, |
| 910 | const struct devlink_port_new_attrs *new_attr, |
| 911 | struct netlink_ext_ack *extack, |
| 912 | struct devlink_port **devlink_port) |
| 913 | { |
| 914 | struct ice_dynamic_port *dyn_port; |
| 915 | struct ice_vsi *vsi; |
| 916 | u32 sfnum; |
| 917 | int err; |
| 918 | |
| 919 | err = ice_reserve_sf_num(pf, new_attr, extack, &sfnum); |
| 920 | if (err) |
| 921 | return err; |
| 922 | |
| 923 | dyn_port = kzalloc_obj(*dyn_port); |
| 924 | if (!dyn_port) { |
| 925 | err = -ENOMEM; |
| 926 | goto unroll_reserve_sf_num; |
| 927 | } |
| 928 | |
| 929 | vsi = ice_vsi_alloc(pf); |
| 930 | if (!vsi) { |
| 931 | NL_SET_ERR_MSG_MOD(extack, "Unable to allocate VSI"); |
| 932 | err = -ENOMEM; |
| 933 | goto unroll_dyn_port_alloc; |
| 934 | } |
| 935 | |
| 936 | dyn_port->vsi = vsi; |
| 937 | dyn_port->pf = pf; |
| 938 | dyn_port->sfnum = sfnum; |
| 939 | eth_random_addr(dyn_port->hw_addr); |
| 940 | |
| 941 | err = xa_insert(&pf->dyn_ports, vsi->idx, dyn_port, GFP_KERNEL); |
| 942 | if (err) { |
| 943 | NL_SET_ERR_MSG_MOD(extack, "Port index reservation failed"); |
| 944 | goto unroll_vsi_alloc; |
| 945 | } |
| 946 | |
| 947 | err = ice_eswitch_attach_sf(pf, dyn_port); |
| 948 | if (err) { |
| 949 | NL_SET_ERR_MSG_MOD(extack, "Failed to attach SF to eswitch"); |
| 950 | goto unroll_xa_insert; |
| 951 | } |
| 952 | |
| 953 | *devlink_port = &dyn_port->devlink_port; |
| 954 | |
| 955 | return 0; |
| 956 | |
| 957 | unroll_xa_insert: |
| 958 | xa_erase(&pf->dyn_ports, vsi->idx); |
| 959 | unroll_vsi_alloc: |
| 960 | ice_vsi_free(vsi); |
| 961 | unroll_dyn_port_alloc: |
| 962 | kfree(dyn_port); |
| 963 | unroll_reserve_sf_num: |
| 964 | xa_erase(&pf->sf_nums, sfnum); |
| 965 | |
| 966 | return err; |
| 967 | } |
| 968 | |
| 969 | /** |
| 970 | * ice_devlink_port_new - devlink handler for the new port |
| 971 | * @devlink: pointer to devlink |
| 972 | * @new_attr: pointer to the port new attributes |
| 973 | * @extack: extack for reporting error messages |
| 974 | * @devlink_port: pointer to a new port |
| 975 | * |
| 976 | * Creates new devlink port, checks new port attributes and reject |
| 977 | * any unsupported parameters, allocates new subfunction for that port. |
| 978 | * |
| 979 | * Return: zero on success or an error code on failure. |
| 980 | */ |
| 981 | int |
| 982 | ice_devlink_port_new(struct devlink *devlink, |
| 983 | const struct devlink_port_new_attrs *new_attr, |
| 984 | struct netlink_ext_ack *extack, |
| 985 | struct devlink_port **devlink_port) |
| 986 | { |
| 987 | struct ice_pf *pf = devlink_priv(devlink); |
| 988 | int err; |
| 989 | |
| 990 | err = ice_devlink_port_new_check_attr(pf, new_attr, extack); |
| 991 | if (err) |
| 992 | return err; |
| 993 | |
| 994 | if (!ice_is_eswitch_mode_switchdev(pf)) { |
| 995 | NL_SET_ERR_MSG_MOD(extack, |
| 996 | "SF ports are only supported in eswitch switchdev mode"); |
| 997 | return -EOPNOTSUPP; |
| 998 | } |
| 999 | |
| 1000 | return ice_alloc_dynamic_port(pf, new_attr, extack, devlink_port); |
| 1001 | } |