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