1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2024, Intel Corporation. */
7 #include "ice_sf_eth.h"
8 #include "devlink/devlink_port.h"
9 #include "devlink/devlink.h"
11 static const struct net_device_ops ice_sf_netdev_ops = {
14 .ndo_start_xmit = ice_start_xmit,
15 .ndo_vlan_rx_add_vid = ice_vlan_rx_add_vid,
16 .ndo_vlan_rx_kill_vid = ice_vlan_rx_kill_vid,
17 .ndo_change_mtu = ice_change_mtu,
18 .ndo_get_stats64 = ice_get_stats64,
19 .ndo_tx_timeout = ice_tx_timeout,
21 .ndo_xdp_xmit = ice_xdp_xmit,
22 .ndo_xsk_wakeup = ice_xsk_wakeup,
26 * ice_sf_cfg_netdev - Allocate, configure and register a netdev
27 * @dyn_port: subfunction associated with configured netdev
28 * @devlink_port: subfunction devlink port to be linked with netdev
30 * Return: 0 on success, negative value on failure
32 static int ice_sf_cfg_netdev(struct ice_dynamic_port *dyn_port,
33 struct devlink_port *devlink_port)
35 struct ice_vsi *vsi = dyn_port->vsi;
36 struct ice_netdev_priv *np;
37 struct net_device *netdev;
40 netdev = alloc_etherdev_mqs(sizeof(*np), vsi->alloc_txq,
45 SET_NETDEV_DEV(netdev, &vsi->back->pdev->dev);
46 set_bit(ICE_VSI_NETDEV_ALLOCD, vsi->state);
48 np = netdev_priv(netdev);
51 ice_set_netdev_features(netdev);
53 netdev->xdp_features = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT |
54 NETDEV_XDP_ACT_XSK_ZEROCOPY |
56 netdev->xdp_zc_max_segs = ICE_MAX_BUF_TXD;
58 eth_hw_addr_set(netdev, dyn_port->hw_addr);
59 ether_addr_copy(netdev->perm_addr, dyn_port->hw_addr);
60 netdev->netdev_ops = &ice_sf_netdev_ops;
61 SET_NETDEV_DEVLINK_PORT(netdev, devlink_port);
63 err = register_netdev(netdev);
69 set_bit(ICE_VSI_NETDEV_REGISTERED, vsi->state);
70 netif_carrier_off(netdev);
71 netif_tx_stop_all_queues(netdev);
76 static void ice_sf_decfg_netdev(struct ice_vsi *vsi)
78 unregister_netdev(vsi->netdev);
79 clear_bit(ICE_VSI_NETDEV_REGISTERED, vsi->state);
80 free_netdev(vsi->netdev);
82 clear_bit(ICE_VSI_NETDEV_ALLOCD, vsi->state);
86 * ice_sf_dev_probe - subfunction driver probe function
87 * @adev: pointer to the auxiliary device
88 * @id: pointer to the auxiliary_device id
90 * Configure VSI and netdev resources for the subfunction device.
92 * Return: zero on success or an error code on failure.
94 static int ice_sf_dev_probe(struct auxiliary_device *adev,
95 const struct auxiliary_device_id *id)
97 struct ice_sf_dev *sf_dev = ice_adev_to_sf_dev(adev);
98 struct ice_dynamic_port *dyn_port = sf_dev->dyn_port;
99 struct ice_vsi *vsi = dyn_port->vsi;
100 struct ice_pf *pf = dyn_port->pf;
101 struct device *dev = &adev->dev;
102 struct ice_sf_priv *priv;
103 struct devlink *devlink;
106 vsi->type = ICE_VSI_SF;
107 vsi->port_info = pf->hw.port_info;
108 vsi->flags = ICE_VSI_FLAG_INIT;
110 priv = ice_allocate_sf(&adev->dev, pf);
112 dev_err(dev, "Subfunction devlink alloc failed");
113 return PTR_ERR(priv);
118 devlink = priv_to_devlink(priv);
122 err = ice_vsi_cfg(vsi);
124 dev_err(dev, "Subfunction vsi config failed");
125 goto err_free_devlink;
129 ice_eswitch_update_repr(&dyn_port->repr_id, vsi);
131 err = ice_devlink_create_sf_dev_port(sf_dev);
133 dev_err(dev, "Cannot add ice virtual devlink port for subfunction");
137 err = ice_sf_cfg_netdev(dyn_port, &sf_dev->priv->devlink_port);
139 dev_err(dev, "Subfunction netdev config failed");
140 goto err_devlink_destroy;
143 err = devl_port_fn_devlink_set(&dyn_port->devlink_port, devlink);
145 dev_err(dev, "Can't link devlink instance to SF devlink port");
146 goto err_netdev_decfg;
151 devl_register(devlink);
152 devl_unlock(devlink);
154 dyn_port->attached = true;
159 ice_sf_decfg_netdev(vsi);
161 ice_devlink_destroy_sf_dev_port(sf_dev);
165 devl_unlock(devlink);
166 devlink_free(devlink);
171 * ice_sf_dev_remove - subfunction driver remove function
172 * @adev: pointer to the auxiliary device
174 * Deinitalize VSI and netdev resources for the subfunction device.
176 static void ice_sf_dev_remove(struct auxiliary_device *adev)
178 struct ice_sf_dev *sf_dev = ice_adev_to_sf_dev(adev);
179 struct ice_dynamic_port *dyn_port = sf_dev->dyn_port;
180 struct ice_vsi *vsi = dyn_port->vsi;
181 struct devlink *devlink;
183 devlink = priv_to_devlink(sf_dev->priv);
188 ice_sf_decfg_netdev(vsi);
189 ice_devlink_destroy_sf_dev_port(sf_dev);
190 devl_unregister(devlink);
191 devl_unlock(devlink);
192 devlink_free(devlink);
195 dyn_port->attached = false;
198 static const struct auxiliary_device_id ice_sf_dev_id_table[] = {
199 { .name = "ice.sf", },
203 MODULE_DEVICE_TABLE(auxiliary, ice_sf_dev_id_table);
205 static struct auxiliary_driver ice_sf_driver = {
207 .probe = ice_sf_dev_probe,
208 .remove = ice_sf_dev_remove,
209 .id_table = ice_sf_dev_id_table
212 static DEFINE_XARRAY_ALLOC1(ice_sf_aux_id);
215 * ice_sf_driver_register - Register new auxiliary subfunction driver
217 * Return: zero on success or an error code on failure.
219 int ice_sf_driver_register(void)
221 return auxiliary_driver_register(&ice_sf_driver);
225 * ice_sf_driver_unregister - Unregister new auxiliary subfunction driver
228 void ice_sf_driver_unregister(void)
230 auxiliary_driver_unregister(&ice_sf_driver);
234 * ice_sf_dev_release - Release device associated with auxiliary device
235 * @device: pointer to the device
237 * Since most of the code for subfunction deactivation is handled in
238 * the remove handler, here just free tracking resources.
240 static void ice_sf_dev_release(struct device *device)
242 struct auxiliary_device *adev = to_auxiliary_dev(device);
243 struct ice_sf_dev *sf_dev = ice_adev_to_sf_dev(adev);
245 xa_erase(&ice_sf_aux_id, adev->id);
250 * ice_sf_eth_activate - Activate Ethernet subfunction port
251 * @dyn_port: the dynamic port instance for this subfunction
252 * @extack: extack for reporting error messages
254 * Activate the dynamic port as an Ethernet subfunction. Setup the netdev
255 * resources associated and initialize the auxiliary device.
257 * Return: zero on success or an error code on failure.
260 ice_sf_eth_activate(struct ice_dynamic_port *dyn_port,
261 struct netlink_ext_ack *extack)
263 struct ice_pf *pf = dyn_port->pf;
264 struct ice_sf_dev *sf_dev;
265 struct pci_dev *pdev;
269 err = xa_alloc(&ice_sf_aux_id, &id, NULL, xa_limit_32b,
272 NL_SET_ERR_MSG_MOD(extack, "Could not allocate SF ID");
276 sf_dev = kzalloc(sizeof(*sf_dev), GFP_KERNEL);
279 NL_SET_ERR_MSG_MOD(extack, "Could not allocate SF memory");
284 sf_dev->dyn_port = dyn_port;
285 sf_dev->adev.id = id;
286 sf_dev->adev.name = "sf";
287 sf_dev->adev.dev.release = ice_sf_dev_release;
288 sf_dev->adev.dev.parent = &pdev->dev;
290 err = auxiliary_device_init(&sf_dev->adev);
292 NL_SET_ERR_MSG_MOD(extack, "Failed to initialize SF device");
296 err = auxiliary_device_add(&sf_dev->adev);
298 NL_SET_ERR_MSG_MOD(extack, "Failed to add SF device");
302 dyn_port->sf_dev = sf_dev;
307 auxiliary_device_uninit(&sf_dev->adev);
311 xa_erase(&ice_sf_aux_id, id);
317 * ice_sf_eth_deactivate - Deactivate Ethernet subfunction port
318 * @dyn_port: the dynamic port instance for this subfunction
320 * Deactivate the Ethernet subfunction, removing its auxiliary device and the
321 * associated resources.
323 void ice_sf_eth_deactivate(struct ice_dynamic_port *dyn_port)
325 struct ice_sf_dev *sf_dev = dyn_port->sf_dev;
327 auxiliary_device_delete(&sf_dev->adev);
328 auxiliary_device_uninit(&sf_dev->adev);