1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2024, Intel Corporation. */
4 #include <linux/vmalloc.h>
12 static int ice_active_port_option = -1;
15 * ice_devlink_port_opt_speed_str - convert speed to a string
18 static const char *ice_devlink_port_opt_speed_str(u8 speed)
20 switch (speed & ICE_AQC_PORT_OPT_MAX_LANE_M) {
21 case ICE_AQC_PORT_OPT_MAX_LANE_100M:
23 case ICE_AQC_PORT_OPT_MAX_LANE_1G:
25 case ICE_AQC_PORT_OPT_MAX_LANE_2500M:
27 case ICE_AQC_PORT_OPT_MAX_LANE_5G:
29 case ICE_AQC_PORT_OPT_MAX_LANE_10G:
31 case ICE_AQC_PORT_OPT_MAX_LANE_25G:
33 case ICE_AQC_PORT_OPT_MAX_LANE_50G:
35 case ICE_AQC_PORT_OPT_MAX_LANE_100G:
42 #define ICE_PORT_OPT_DESC_LEN 50
44 * ice_devlink_port_options_print - Print available port split options
45 * @pf: the PF to print split port options
47 * Prints a table with available port split options and max port speeds
49 static void ice_devlink_port_options_print(struct ice_pf *pf)
51 u8 i, j, options_count, cnt, speed, pending_idx, active_idx;
52 struct ice_aqc_get_port_options_elem *options, *opt;
53 struct device *dev = ice_pf_to_dev(pf);
54 bool active_valid, pending_valid;
55 char desc[ICE_PORT_OPT_DESC_LEN];
59 options = kcalloc(ICE_AQC_PORT_OPT_MAX * ICE_MAX_PORT_PER_PCI_DEV,
60 sizeof(*options), GFP_KERNEL);
64 for (i = 0; i < ICE_MAX_PORT_PER_PCI_DEV; i++) {
65 opt = options + i * ICE_AQC_PORT_OPT_MAX;
66 options_count = ICE_AQC_PORT_OPT_MAX;
69 status = ice_aq_get_port_options(&pf->hw, opt, &options_count,
71 &active_valid, &pending_idx,
74 dev_dbg(dev, "Couldn't read port option for port %d, err %d\n",
80 dev_dbg(dev, "Available port split options and max port speeds (Gbps):\n");
81 dev_dbg(dev, "Status Split Quad 0 Quad 1\n");
82 dev_dbg(dev, " count L0 L1 L2 L3 L4 L5 L6 L7\n");
84 for (i = 0; i < options_count; i++) {
87 if (i == ice_active_port_option)
89 else if ((i == pending_idx) && pending_valid)
94 cnt += snprintf(&desc[cnt], ICE_PORT_OPT_DESC_LEN - cnt,
97 cnt += snprintf(&desc[cnt], ICE_PORT_OPT_DESC_LEN - cnt,
98 "%-6u", options[i].pmd);
100 for (j = 0; j < ICE_MAX_PORT_PER_PCI_DEV; ++j) {
101 speed = options[i + j * ICE_AQC_PORT_OPT_MAX].max_lane_speed;
102 str = ice_devlink_port_opt_speed_str(speed);
103 cnt += snprintf(&desc[cnt], ICE_PORT_OPT_DESC_LEN - cnt,
107 dev_dbg(dev, "%s\n", desc);
115 * ice_devlink_aq_set_port_option - Send set port option admin queue command
116 * @pf: the PF to print split port options
117 * @option_idx: selected port option
118 * @extack: extended netdev ack structure
120 * Sends set port option admin queue command with selected port option and
121 * calls NVM write activate.
124 ice_devlink_aq_set_port_option(struct ice_pf *pf, u8 option_idx,
125 struct netlink_ext_ack *extack)
127 struct device *dev = ice_pf_to_dev(pf);
130 status = ice_aq_set_port_option(&pf->hw, 0, true, option_idx);
132 dev_dbg(dev, "ice_aq_set_port_option, err %d aq_err %d\n",
133 status, pf->hw.adminq.sq_last_status);
134 NL_SET_ERR_MSG_MOD(extack, "Port split request failed");
138 status = ice_acquire_nvm(&pf->hw, ICE_RES_WRITE);
140 dev_dbg(dev, "ice_acquire_nvm failed, err %d aq_err %d\n",
141 status, pf->hw.adminq.sq_last_status);
142 NL_SET_ERR_MSG_MOD(extack, "Failed to acquire NVM semaphore");
146 status = ice_nvm_write_activate(&pf->hw, ICE_AQC_NVM_ACTIV_REQ_EMPR, NULL);
148 dev_dbg(dev, "ice_nvm_write_activate failed, err %d aq_err %d\n",
149 status, pf->hw.adminq.sq_last_status);
150 NL_SET_ERR_MSG_MOD(extack, "Port split request failed to save data");
151 ice_release_nvm(&pf->hw);
155 ice_release_nvm(&pf->hw);
157 NL_SET_ERR_MSG_MOD(extack, "Reboot required to finish port split");
162 * ice_devlink_port_split - .port_split devlink handler
163 * @devlink: devlink instance structure
164 * @port: devlink port structure
165 * @count: number of ports to split to
166 * @extack: extended netdev ack structure
168 * Callback for the devlink .port_split operation.
170 * Unfortunately, the devlink expression of available options is limited
171 * to just a number, so search for an FW port option which supports
172 * the specified number. As there could be multiple FW port options with
173 * the same port split count, allow switching between them. When the same
174 * port split count request is issued again, switch to the next FW port
175 * option with the same port split count.
177 * Return: zero on success or an error code on failure.
180 ice_devlink_port_split(struct devlink *devlink, struct devlink_port *port,
181 unsigned int count, struct netlink_ext_ack *extack)
183 struct ice_aqc_get_port_options_elem options[ICE_AQC_PORT_OPT_MAX];
184 u8 i, j, active_idx, pending_idx, new_option;
185 struct ice_pf *pf = devlink_priv(devlink);
186 u8 option_count = ICE_AQC_PORT_OPT_MAX;
187 struct device *dev = ice_pf_to_dev(pf);
188 bool active_valid, pending_valid;
191 status = ice_aq_get_port_options(&pf->hw, options, &option_count,
192 0, true, &active_idx, &active_valid,
193 &pending_idx, &pending_valid);
195 dev_dbg(dev, "Couldn't read port split options, err = %d\n",
197 NL_SET_ERR_MSG_MOD(extack, "Failed to get available port split options");
201 new_option = ICE_AQC_PORT_OPT_MAX;
202 active_idx = pending_valid ? pending_idx : active_idx;
203 for (i = 1; i <= option_count; i++) {
204 /* In order to allow switching between FW port options with
205 * the same port split count, search for a new option starting
206 * from the active/pending option (with array wrap around).
208 j = (active_idx + i) % option_count;
210 if (count == options[j].pmd) {
216 if (new_option == active_idx) {
217 dev_dbg(dev, "request to split: count: %u is already set and there are no other options\n",
219 NL_SET_ERR_MSG_MOD(extack, "Requested split count is already set");
220 ice_devlink_port_options_print(pf);
224 if (new_option == ICE_AQC_PORT_OPT_MAX) {
225 dev_dbg(dev, "request to split: count: %u not found\n", count);
226 NL_SET_ERR_MSG_MOD(extack, "Port split requested unsupported port config");
227 ice_devlink_port_options_print(pf);
231 status = ice_devlink_aq_set_port_option(pf, new_option, extack);
235 ice_devlink_port_options_print(pf);
241 * ice_devlink_port_unsplit - .port_unsplit devlink handler
242 * @devlink: devlink instance structure
243 * @port: devlink port structure
244 * @extack: extended netdev ack structure
246 * Callback for the devlink .port_unsplit operation.
247 * Calls ice_devlink_port_split with split count set to 1.
248 * There could be no FW option available with split count 1.
250 * Return: zero on success or an error code on failure.
253 ice_devlink_port_unsplit(struct devlink *devlink, struct devlink_port *port,
254 struct netlink_ext_ack *extack)
256 return ice_devlink_port_split(devlink, port, 1, extack);
260 * ice_devlink_set_port_split_options - Set port split options
261 * @pf: the PF to set port split options
262 * @attrs: devlink attributes
264 * Sets devlink port split options based on available FW port options
267 ice_devlink_set_port_split_options(struct ice_pf *pf,
268 struct devlink_port_attrs *attrs)
270 struct ice_aqc_get_port_options_elem options[ICE_AQC_PORT_OPT_MAX];
271 u8 i, active_idx, pending_idx, option_count = ICE_AQC_PORT_OPT_MAX;
272 bool active_valid, pending_valid;
275 status = ice_aq_get_port_options(&pf->hw, options, &option_count,
276 0, true, &active_idx, &active_valid,
277 &pending_idx, &pending_valid);
279 dev_dbg(ice_pf_to_dev(pf), "Couldn't read port split options, err = %d\n",
284 /* find the biggest available port split count */
285 for (i = 0; i < option_count; i++)
286 attrs->lanes = max_t(int, attrs->lanes, options[i].pmd);
288 attrs->splittable = attrs->lanes ? 1 : 0;
289 ice_active_port_option = active_idx;
292 static const struct devlink_port_ops ice_devlink_port_ops = {
293 .port_split = ice_devlink_port_split,
294 .port_unsplit = ice_devlink_port_unsplit,
298 * ice_devlink_set_switch_id - Set unique switch id based on pci dsn
299 * @pf: the PF to create a devlink port for
300 * @ppid: struct with switch id information
303 ice_devlink_set_switch_id(struct ice_pf *pf, struct netdev_phys_item_id *ppid)
305 struct pci_dev *pdev = pf->pdev;
308 id = pci_get_dsn(pdev);
310 ppid->id_len = sizeof(id);
311 put_unaligned_be64(id, &ppid->id);
315 * ice_devlink_create_pf_port - Create a devlink port for this PF
316 * @pf: the PF to create a devlink port for
318 * Create and register a devlink_port for this PF.
319 * This function has to be called under devl_lock.
321 * Return: zero on success or an error code on failure.
323 int ice_devlink_create_pf_port(struct ice_pf *pf)
325 struct devlink_port_attrs attrs = {};
326 struct devlink_port *devlink_port;
327 struct devlink *devlink;
332 devlink = priv_to_devlink(pf);
334 dev = ice_pf_to_dev(pf);
336 devlink_port = &pf->devlink_port;
338 vsi = ice_get_main_vsi(pf);
342 attrs.flavour = DEVLINK_PORT_FLAVOUR_PHYSICAL;
343 attrs.phys.port_number = pf->hw.pf_id;
345 /* As FW supports only port split options for whole device,
346 * set port split options only for first PF.
348 if (pf->hw.pf_id == 0)
349 ice_devlink_set_port_split_options(pf, &attrs);
351 ice_devlink_set_switch_id(pf, &attrs.switch_id);
353 devlink_port_attrs_set(devlink_port, &attrs);
355 err = devl_port_register_with_ops(devlink, devlink_port, vsi->idx,
356 &ice_devlink_port_ops);
358 dev_err(dev, "Failed to create devlink port for PF %d, error %d\n",
367 * ice_devlink_destroy_pf_port - Destroy the devlink_port for this PF
368 * @pf: the PF to cleanup
370 * Unregisters the devlink_port structure associated with this PF.
371 * This function has to be called under devl_lock.
373 void ice_devlink_destroy_pf_port(struct ice_pf *pf)
375 devl_port_unregister(&pf->devlink_port);
379 * ice_devlink_port_get_vf_fn_mac - .port_fn_hw_addr_get devlink handler
380 * @port: devlink port structure
381 * @hw_addr: MAC address of the port
382 * @hw_addr_len: length of MAC address
383 * @extack: extended netdev ack structure
385 * Callback for the devlink .port_fn_hw_addr_get operation
386 * Return: zero on success or an error code on failure.
388 static int ice_devlink_port_get_vf_fn_mac(struct devlink_port *port,
389 u8 *hw_addr, int *hw_addr_len,
390 struct netlink_ext_ack *extack)
392 struct ice_vf *vf = container_of(port, struct ice_vf, devlink_port);
394 ether_addr_copy(hw_addr, vf->dev_lan_addr);
395 *hw_addr_len = ETH_ALEN;
401 * ice_devlink_port_set_vf_fn_mac - .port_fn_hw_addr_set devlink handler
402 * @port: devlink port structure
403 * @hw_addr: MAC address of the port
404 * @hw_addr_len: length of MAC address
405 * @extack: extended netdev ack structure
407 * Callback for the devlink .port_fn_hw_addr_set operation
408 * Return: zero on success or an error code on failure.
410 static int ice_devlink_port_set_vf_fn_mac(struct devlink_port *port,
413 struct netlink_ext_ack *extack)
416 struct devlink_port_attrs *attrs = &port->attrs;
417 struct devlink_port_pci_vf_attrs *pci_vf;
418 struct devlink *devlink = port->devlink;
422 pf = devlink_priv(devlink);
423 pci_vf = &attrs->pci_vf;
426 return __ice_set_vf_mac(pf, vf_id, hw_addr);
429 static const struct devlink_port_ops ice_devlink_vf_port_ops = {
430 .port_fn_hw_addr_get = ice_devlink_port_get_vf_fn_mac,
431 .port_fn_hw_addr_set = ice_devlink_port_set_vf_fn_mac,
435 * ice_devlink_create_vf_port - Create a devlink port for this VF
436 * @vf: the VF to create a port for
438 * Create and register a devlink_port for this VF.
440 * Return: zero on success or an error code on failure.
442 int ice_devlink_create_vf_port(struct ice_vf *vf)
444 struct devlink_port_attrs attrs = {};
445 struct devlink_port *devlink_port;
446 struct devlink *devlink;
453 dev = ice_pf_to_dev(pf);
454 devlink_port = &vf->devlink_port;
456 vsi = ice_get_vf_vsi(vf);
460 attrs.flavour = DEVLINK_PORT_FLAVOUR_PCI_VF;
461 attrs.pci_vf.pf = pf->hw.pf_id;
462 attrs.pci_vf.vf = vf->vf_id;
464 ice_devlink_set_switch_id(pf, &attrs.switch_id);
466 devlink_port_attrs_set(devlink_port, &attrs);
467 devlink = priv_to_devlink(pf);
469 err = devl_port_register_with_ops(devlink, devlink_port, vsi->idx,
470 &ice_devlink_vf_port_ops);
472 dev_err(dev, "Failed to create devlink port for VF %d, error %d\n",
481 * ice_devlink_destroy_vf_port - Destroy the devlink_port for this VF
482 * @vf: the VF to cleanup
484 * Unregisters the devlink_port structure associated with this VF.
486 void ice_devlink_destroy_vf_port(struct ice_vf *vf)
488 devl_rate_leaf_destroy(&vf->devlink_port);
489 devl_port_unregister(&vf->devlink_port);
493 * ice_devlink_create_sf_dev_port - Register virtual port for a subfunction
494 * @sf_dev: the subfunction device to create a devlink port for
496 * Register virtual flavour devlink port for the subfunction auxiliary device
497 * created after activating a dynamically added devlink port.
499 * Return: zero on success or an error code on failure.
501 int ice_devlink_create_sf_dev_port(struct ice_sf_dev *sf_dev)
503 struct devlink_port_attrs attrs = {};
504 struct ice_dynamic_port *dyn_port;
505 struct devlink_port *devlink_port;
506 struct devlink *devlink;
509 dyn_port = sf_dev->dyn_port;
512 devlink_port = &sf_dev->priv->devlink_port;
514 attrs.flavour = DEVLINK_PORT_FLAVOUR_VIRTUAL;
516 devlink_port_attrs_set(devlink_port, &attrs);
517 devlink = priv_to_devlink(sf_dev->priv);
519 return devl_port_register(devlink, devlink_port, vsi->idx);
523 * ice_devlink_destroy_sf_dev_port - Destroy virtual port for a subfunction
524 * @sf_dev: the subfunction device to create a devlink port for
526 * Unregisters the virtual port associated with this subfunction.
528 void ice_devlink_destroy_sf_dev_port(struct ice_sf_dev *sf_dev)
530 devl_port_unregister(&sf_dev->priv->devlink_port);
534 * ice_activate_dynamic_port - Activate a dynamic port
535 * @dyn_port: dynamic port instance to activate
536 * @extack: extack for reporting error messages
538 * Activate the dynamic port based on its flavour.
540 * Return: zero on success or an error code on failure.
543 ice_activate_dynamic_port(struct ice_dynamic_port *dyn_port,
544 struct netlink_ext_ack *extack)
548 if (dyn_port->active)
551 err = ice_sf_eth_activate(dyn_port, extack);
555 dyn_port->active = true;
561 * ice_deactivate_dynamic_port - Deactivate a dynamic port
562 * @dyn_port: dynamic port instance to deactivate
564 * Undo activation of a dynamic port.
566 static void ice_deactivate_dynamic_port(struct ice_dynamic_port *dyn_port)
568 if (!dyn_port->active)
571 ice_sf_eth_deactivate(dyn_port);
572 dyn_port->active = false;
576 * ice_dealloc_dynamic_port - Deallocate and remove a dynamic port
577 * @dyn_port: dynamic port instance to deallocate
579 * Free resources associated with a dynamically added devlink port. Will
580 * deactivate the port if its currently active.
582 static void ice_dealloc_dynamic_port(struct ice_dynamic_port *dyn_port)
584 struct devlink_port *devlink_port = &dyn_port->devlink_port;
585 struct ice_pf *pf = dyn_port->pf;
587 ice_deactivate_dynamic_port(dyn_port);
589 xa_erase(&pf->sf_nums, devlink_port->attrs.pci_sf.sf);
590 ice_eswitch_detach_sf(pf, dyn_port);
591 ice_vsi_free(dyn_port->vsi);
592 xa_erase(&pf->dyn_ports, dyn_port->vsi->idx);
597 * ice_dealloc_all_dynamic_ports - Deallocate all dynamic devlink ports
598 * @pf: pointer to the pf structure
600 void ice_dealloc_all_dynamic_ports(struct ice_pf *pf)
602 struct ice_dynamic_port *dyn_port;
605 xa_for_each(&pf->dyn_ports, index, dyn_port)
606 ice_dealloc_dynamic_port(dyn_port);
610 * ice_devlink_port_new_check_attr - Check that new port attributes are valid
611 * @pf: pointer to the PF structure
612 * @new_attr: the attributes for the new port
613 * @extack: extack for reporting error messages
615 * Check that the attributes for the new port are valid before continuing to
616 * allocate the devlink port.
618 * Return: zero on success or an error code on failure.
621 ice_devlink_port_new_check_attr(struct ice_pf *pf,
622 const struct devlink_port_new_attrs *new_attr,
623 struct netlink_ext_ack *extack)
625 if (new_attr->flavour != DEVLINK_PORT_FLAVOUR_PCI_SF) {
626 NL_SET_ERR_MSG_MOD(extack, "Flavour other than pcisf is not supported");
630 if (new_attr->controller_valid) {
631 NL_SET_ERR_MSG_MOD(extack, "Setting controller is not supported");
635 if (new_attr->port_index_valid) {
636 NL_SET_ERR_MSG_MOD(extack, "Driver does not support user defined port index assignment");
640 if (new_attr->pfnum != pf->hw.pf_id) {
641 NL_SET_ERR_MSG_MOD(extack, "Incorrect pfnum supplied");
645 if (!pci_msix_can_alloc_dyn(pf->pdev)) {
646 NL_SET_ERR_MSG_MOD(extack, "Dynamic MSIX-X interrupt allocation is not supported");
654 * ice_devlink_port_del - devlink handler for port delete
655 * @devlink: pointer to devlink
656 * @port: devlink port to be deleted
657 * @extack: pointer to extack
659 * Deletes devlink port and deallocates all resources associated with
660 * created subfunction.
662 * Return: zero on success or an error code on failure.
665 ice_devlink_port_del(struct devlink *devlink, struct devlink_port *port,
666 struct netlink_ext_ack *extack)
668 struct ice_dynamic_port *dyn_port;
670 dyn_port = ice_devlink_port_to_dyn(port);
671 ice_dealloc_dynamic_port(dyn_port);
677 * ice_devlink_port_fn_hw_addr_set - devlink handler for mac address set
678 * @port: pointer to devlink port
679 * @hw_addr: hw address to set
680 * @hw_addr_len: hw address length
681 * @extack: extack for reporting error messages
683 * Sets mac address for the port, verifies arguments and copies address
684 * to the subfunction structure.
686 * Return: zero on success or an error code on failure.
689 ice_devlink_port_fn_hw_addr_set(struct devlink_port *port, const u8 *hw_addr,
691 struct netlink_ext_ack *extack)
693 struct ice_dynamic_port *dyn_port;
695 dyn_port = ice_devlink_port_to_dyn(port);
697 if (dyn_port->attached) {
698 NL_SET_ERR_MSG_MOD(extack,
699 "Ethernet address can be change only in detached state");
703 if (hw_addr_len != ETH_ALEN || !is_valid_ether_addr(hw_addr)) {
704 NL_SET_ERR_MSG_MOD(extack, "Invalid ethernet address");
705 return -EADDRNOTAVAIL;
708 ether_addr_copy(dyn_port->hw_addr, hw_addr);
714 * ice_devlink_port_fn_hw_addr_get - devlink handler for mac address get
715 * @port: pointer to devlink port
716 * @hw_addr: hw address to set
717 * @hw_addr_len: hw address length
718 * @extack: extack for reporting error messages
720 * Returns mac address for the port.
722 * Return: zero on success or an error code on failure.
725 ice_devlink_port_fn_hw_addr_get(struct devlink_port *port, u8 *hw_addr,
727 struct netlink_ext_ack *extack)
729 struct ice_dynamic_port *dyn_port;
731 dyn_port = ice_devlink_port_to_dyn(port);
733 ether_addr_copy(hw_addr, dyn_port->hw_addr);
734 *hw_addr_len = ETH_ALEN;
740 * ice_devlink_port_fn_state_set - devlink handler for port state set
741 * @port: pointer to devlink port
742 * @state: state to set
743 * @extack: extack for reporting error messages
745 * Activates or deactivates the port.
747 * Return: zero on success or an error code on failure.
750 ice_devlink_port_fn_state_set(struct devlink_port *port,
751 enum devlink_port_fn_state state,
752 struct netlink_ext_ack *extack)
754 struct ice_dynamic_port *dyn_port;
756 dyn_port = ice_devlink_port_to_dyn(port);
759 case DEVLINK_PORT_FN_STATE_ACTIVE:
760 return ice_activate_dynamic_port(dyn_port, extack);
762 case DEVLINK_PORT_FN_STATE_INACTIVE:
763 ice_deactivate_dynamic_port(dyn_port);
771 * ice_devlink_port_fn_state_get - devlink handler for port state get
772 * @port: pointer to devlink port
773 * @state: admin configured state of the port
774 * @opstate: current port operational state
775 * @extack: extack for reporting error messages
779 * Return: zero on success or an error code on failure.
782 ice_devlink_port_fn_state_get(struct devlink_port *port,
783 enum devlink_port_fn_state *state,
784 enum devlink_port_fn_opstate *opstate,
785 struct netlink_ext_ack *extack)
787 struct ice_dynamic_port *dyn_port;
789 dyn_port = ice_devlink_port_to_dyn(port);
791 if (dyn_port->active)
792 *state = DEVLINK_PORT_FN_STATE_ACTIVE;
794 *state = DEVLINK_PORT_FN_STATE_INACTIVE;
796 if (dyn_port->attached)
797 *opstate = DEVLINK_PORT_FN_OPSTATE_ATTACHED;
799 *opstate = DEVLINK_PORT_FN_OPSTATE_DETACHED;
804 static const struct devlink_port_ops ice_devlink_port_sf_ops = {
805 .port_del = ice_devlink_port_del,
806 .port_fn_hw_addr_get = ice_devlink_port_fn_hw_addr_get,
807 .port_fn_hw_addr_set = ice_devlink_port_fn_hw_addr_set,
808 .port_fn_state_get = ice_devlink_port_fn_state_get,
809 .port_fn_state_set = ice_devlink_port_fn_state_set,
813 * ice_reserve_sf_num - Reserve a subfunction number for this port
814 * @pf: pointer to the pf structure
815 * @new_attr: devlink port attributes requested
816 * @extack: extack for reporting error messages
817 * @sfnum: on success, the sf number reserved
819 * Reserve a subfunction number for this port. Only called for
820 * DEVLINK_PORT_FLAVOUR_PCI_SF ports.
822 * Return: zero on success or an error code on failure.
825 ice_reserve_sf_num(struct ice_pf *pf,
826 const struct devlink_port_new_attrs *new_attr,
827 struct netlink_ext_ack *extack, u32 *sfnum)
831 /* If user didn't request an explicit number, pick one */
832 if (!new_attr->sfnum_valid)
833 return xa_alloc(&pf->sf_nums, sfnum, NULL, xa_limit_32b,
836 /* Otherwise, check and use the number provided */
837 err = xa_insert(&pf->sf_nums, new_attr->sfnum, NULL, GFP_KERNEL);
840 NL_SET_ERR_MSG_MOD(extack, "Subfunction with given sfnum already exists");
844 *sfnum = new_attr->sfnum;
850 * ice_devlink_create_sf_port - Register PCI subfunction devlink port
851 * @dyn_port: the dynamic port instance structure for this subfunction
853 * Register PCI subfunction flavour devlink port for a dynamically added
856 * Return: zero on success or an error code on failure.
858 int ice_devlink_create_sf_port(struct ice_dynamic_port *dyn_port)
860 struct devlink_port_attrs attrs = {};
861 struct devlink_port *devlink_port;
862 struct devlink *devlink;
869 devlink_port = &dyn_port->devlink_port;
871 attrs.flavour = DEVLINK_PORT_FLAVOUR_PCI_SF;
872 attrs.pci_sf.pf = pf->hw.pf_id;
873 attrs.pci_sf.sf = dyn_port->sfnum;
875 devlink_port_attrs_set(devlink_port, &attrs);
876 devlink = priv_to_devlink(pf);
878 return devl_port_register_with_ops(devlink, devlink_port, vsi->idx,
879 &ice_devlink_port_sf_ops);
883 * ice_devlink_destroy_sf_port - Destroy the devlink_port for this SF
884 * @dyn_port: the dynamic port instance structure for this subfunction
886 * Unregisters the devlink_port structure associated with this SF.
888 void ice_devlink_destroy_sf_port(struct ice_dynamic_port *dyn_port)
890 devl_rate_leaf_destroy(&dyn_port->devlink_port);
891 devl_port_unregister(&dyn_port->devlink_port);
895 * ice_alloc_dynamic_port - Allocate new dynamic port
896 * @pf: pointer to the pf structure
897 * @new_attr: devlink port attributes requested
898 * @extack: extack for reporting error messages
899 * @devlink_port: index of newly created devlink port
901 * Allocate a new dynamic port instance and prepare it for configuration
904 * Return: zero on success or an error code on failure.
907 ice_alloc_dynamic_port(struct ice_pf *pf,
908 const struct devlink_port_new_attrs *new_attr,
909 struct netlink_ext_ack *extack,
910 struct devlink_port **devlink_port)
912 struct ice_dynamic_port *dyn_port;
917 err = ice_reserve_sf_num(pf, new_attr, extack, &sfnum);
921 dyn_port = kzalloc(sizeof(*dyn_port), GFP_KERNEL);
924 goto unroll_reserve_sf_num;
927 vsi = ice_vsi_alloc(pf);
929 NL_SET_ERR_MSG_MOD(extack, "Unable to allocate VSI");
931 goto unroll_dyn_port_alloc;
936 dyn_port->sfnum = sfnum;
937 eth_random_addr(dyn_port->hw_addr);
939 err = xa_insert(&pf->dyn_ports, vsi->idx, dyn_port, GFP_KERNEL);
941 NL_SET_ERR_MSG_MOD(extack, "Port index reservation failed");
942 goto unroll_vsi_alloc;
945 err = ice_eswitch_attach_sf(pf, dyn_port);
947 NL_SET_ERR_MSG_MOD(extack, "Failed to attach SF to eswitch");
948 goto unroll_xa_insert;
951 *devlink_port = &dyn_port->devlink_port;
956 xa_erase(&pf->dyn_ports, vsi->idx);
959 unroll_dyn_port_alloc:
961 unroll_reserve_sf_num:
962 xa_erase(&pf->sf_nums, sfnum);
968 * ice_devlink_port_new - devlink handler for the new port
969 * @devlink: pointer to devlink
970 * @new_attr: pointer to the port new attributes
971 * @extack: extack for reporting error messages
972 * @devlink_port: pointer to a new port
974 * Creates new devlink port, checks new port attributes and reject
975 * any unsupported parameters, allocates new subfunction for that port.
977 * Return: zero on success or an error code on failure.
980 ice_devlink_port_new(struct devlink *devlink,
981 const struct devlink_port_new_attrs *new_attr,
982 struct netlink_ext_ack *extack,
983 struct devlink_port **devlink_port)
985 struct ice_pf *pf = devlink_priv(devlink);
988 err = ice_devlink_port_new_check_attr(pf, new_attr, extack);
992 if (!ice_is_eswitch_mode_switchdev(pf)) {
993 NL_SET_ERR_MSG_MOD(extack,
994 "SF ports are only supported in eswitch switchdev mode");
998 return ice_alloc_dynamic_port(pf, new_attr, extack, devlink_port);