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
9static int ice_active_port_option = -1;
10
11/**
12 * ice_devlink_port_opt_speed_str - convert speed to a string
13 * @speed: speed value
14 */
15static const char *ice_devlink_port_opt_speed_str(u8 speed)
16{
17	switch (speed & ICE_AQC_PORT_OPT_MAX_LANE_M) {
18	case ICE_AQC_PORT_OPT_MAX_LANE_100M:
19		return "0.1";
20	case ICE_AQC_PORT_OPT_MAX_LANE_1G:
21		return "1";
22	case ICE_AQC_PORT_OPT_MAX_LANE_2500M:
23		return "2.5";
24	case ICE_AQC_PORT_OPT_MAX_LANE_5G:
25		return "5";
26	case ICE_AQC_PORT_OPT_MAX_LANE_10G:
27		return "10";
28	case ICE_AQC_PORT_OPT_MAX_LANE_25G:
29		return "25";
30	case ICE_AQC_PORT_OPT_MAX_LANE_50G:
31		return "50";
32	case ICE_AQC_PORT_OPT_MAX_LANE_100G:
33		return "100";
34	}
35
36	return "-";
37}
38
39#define ICE_PORT_OPT_DESC_LEN	50
40/**
41 * ice_devlink_port_options_print - Print available port split options
42 * @pf: the PF to print split port options
43 *
44 * Prints a table with available port split options and max port speeds
45 */
46static void ice_devlink_port_options_print(struct ice_pf *pf)
47{
48	u8 i, j, options_count, cnt, speed, pending_idx, active_idx;
49	struct ice_aqc_get_port_options_elem *options, *opt;
50	struct device *dev = ice_pf_to_dev(pf);
51	bool active_valid, pending_valid;
52	char desc[ICE_PORT_OPT_DESC_LEN];
53	const char *str;
54	int status;
55
56	options = kcalloc(ICE_AQC_PORT_OPT_MAX * ICE_MAX_PORT_PER_PCI_DEV,
57			  sizeof(*options), GFP_KERNEL);
58	if (!options)
59		return;
60
61	for (i = 0; i < ICE_MAX_PORT_PER_PCI_DEV; i++) {
62		opt = options + i * ICE_AQC_PORT_OPT_MAX;
63		options_count = ICE_AQC_PORT_OPT_MAX;
64		active_valid = 0;
65
66		status = ice_aq_get_port_options(&pf->hw, opt, &options_count,
67						 i, true, &active_idx,
68						 &active_valid, &pending_idx,
69						 &pending_valid);
70		if (status) {
71			dev_dbg(dev, "Couldn't read port option for port %d, err %d\n",
72				i, status);
73			goto err;
74		}
75	}
76
77	dev_dbg(dev, "Available port split options and max port speeds (Gbps):\n");
78	dev_dbg(dev, "Status  Split      Quad 0          Quad 1\n");
79	dev_dbg(dev, "        count  L0  L1  L2  L3  L4  L5  L6  L7\n");
80
81	for (i = 0; i < options_count; i++) {
82		cnt = 0;
83
84		if (i == ice_active_port_option)
85			str = "Active";
86		else if ((i == pending_idx) && pending_valid)
87			str = "Pending";
88		else
89			str = "";
90
91		cnt += snprintf(&desc[cnt], ICE_PORT_OPT_DESC_LEN - cnt,
92				"%-8s", str);
93
94		cnt += snprintf(&desc[cnt], ICE_PORT_OPT_DESC_LEN - cnt,
95				"%-6u", options[i].pmd);
96
97		for (j = 0; j < ICE_MAX_PORT_PER_PCI_DEV; ++j) {
98			speed = options[i + j * ICE_AQC_PORT_OPT_MAX].max_lane_speed;
99			str = ice_devlink_port_opt_speed_str(speed);
100			cnt += snprintf(&desc[cnt], ICE_PORT_OPT_DESC_LEN - cnt,
101					"%3s ", str);
102		}
103
104		dev_dbg(dev, "%s\n", desc);
105	}
106
107err:
108	kfree(options);
109}
110
111/**
112 * ice_devlink_aq_set_port_option - Send set port option admin queue command
113 * @pf: the PF to print split port options
114 * @option_idx: selected port option
115 * @extack: extended netdev ack structure
116 *
117 * Sends set port option admin queue command with selected port option and
118 * calls NVM write activate.
119 */
120static int
121ice_devlink_aq_set_port_option(struct ice_pf *pf, u8 option_idx,
122			       struct netlink_ext_ack *extack)
123{
124	struct device *dev = ice_pf_to_dev(pf);
125	int status;
126
127	status = ice_aq_set_port_option(&pf->hw, 0, true, option_idx);
128	if (status) {
129		dev_dbg(dev, "ice_aq_set_port_option, err %d aq_err %d\n",
130			status, pf->hw.adminq.sq_last_status);
131		NL_SET_ERR_MSG_MOD(extack, "Port split request failed");
132		return -EIO;
133	}
134
135	status = ice_acquire_nvm(&pf->hw, ICE_RES_WRITE);
136	if (status) {
137		dev_dbg(dev, "ice_acquire_nvm failed, err %d aq_err %d\n",
138			status, pf->hw.adminq.sq_last_status);
139		NL_SET_ERR_MSG_MOD(extack, "Failed to acquire NVM semaphore");
140		return -EIO;
141	}
142
143	status = ice_nvm_write_activate(&pf->hw, ICE_AQC_NVM_ACTIV_REQ_EMPR, NULL);
144	if (status) {
145		dev_dbg(dev, "ice_nvm_write_activate failed, err %d aq_err %d\n",
146			status, pf->hw.adminq.sq_last_status);
147		NL_SET_ERR_MSG_MOD(extack, "Port split request failed to save data");
148		ice_release_nvm(&pf->hw);
149		return -EIO;
150	}
151
152	ice_release_nvm(&pf->hw);
153
154	NL_SET_ERR_MSG_MOD(extack, "Reboot required to finish port split");
155	return 0;
156}
157
158/**
159 * ice_devlink_port_split - .port_split devlink handler
160 * @devlink: devlink instance structure
161 * @port: devlink port structure
162 * @count: number of ports to split to
163 * @extack: extended netdev ack structure
164 *
165 * Callback for the devlink .port_split operation.
166 *
167 * Unfortunately, the devlink expression of available options is limited
168 * to just a number, so search for an FW port option which supports
169 * the specified number. As there could be multiple FW port options with
170 * the same port split count, allow switching between them. When the same
171 * port split count request is issued again, switch to the next FW port
172 * option with the same port split count.
173 *
174 * Return: zero on success or an error code on failure.
175 */
176static int
177ice_devlink_port_split(struct devlink *devlink, struct devlink_port *port,
178		       unsigned int count, struct netlink_ext_ack *extack)
179{
180	struct ice_aqc_get_port_options_elem options[ICE_AQC_PORT_OPT_MAX];
181	u8 i, j, active_idx, pending_idx, new_option;
182	struct ice_pf *pf = devlink_priv(devlink);
183	u8 option_count = ICE_AQC_PORT_OPT_MAX;
184	struct device *dev = ice_pf_to_dev(pf);
185	bool active_valid, pending_valid;
186	int status;
187
188	status = ice_aq_get_port_options(&pf->hw, options, &option_count,
189					 0, true, &active_idx, &active_valid,
190					 &pending_idx, &pending_valid);
191	if (status) {
192		dev_dbg(dev, "Couldn't read port split options, err = %d\n",
193			status);
194		NL_SET_ERR_MSG_MOD(extack, "Failed to get available port split options");
195		return -EIO;
196	}
197
198	new_option = ICE_AQC_PORT_OPT_MAX;
199	active_idx = pending_valid ? pending_idx : active_idx;
200	for (i = 1; i <= option_count; i++) {
201		/* In order to allow switching between FW port options with
202		 * the same port split count, search for a new option starting
203		 * from the active/pending option (with array wrap around).
204		 */
205		j = (active_idx + i) % option_count;
206
207		if (count == options[j].pmd) {
208			new_option = j;
209			break;
210		}
211	}
212
213	if (new_option == active_idx) {
214		dev_dbg(dev, "request to split: count: %u is already set and there are no other options\n",
215			count);
216		NL_SET_ERR_MSG_MOD(extack, "Requested split count is already set");
217		ice_devlink_port_options_print(pf);
218		return -EINVAL;
219	}
220
221	if (new_option == ICE_AQC_PORT_OPT_MAX) {
222		dev_dbg(dev, "request to split: count: %u not found\n", count);
223		NL_SET_ERR_MSG_MOD(extack, "Port split requested unsupported port config");
224		ice_devlink_port_options_print(pf);
225		return -EINVAL;
226	}
227
228	status = ice_devlink_aq_set_port_option(pf, new_option, extack);
229	if (status)
230		return status;
231
232	ice_devlink_port_options_print(pf);
233
234	return 0;
235}
236
237/**
238 * ice_devlink_port_unsplit - .port_unsplit devlink handler
239 * @devlink: devlink instance structure
240 * @port: devlink port structure
241 * @extack: extended netdev ack structure
242 *
243 * Callback for the devlink .port_unsplit operation.
244 * Calls ice_devlink_port_split with split count set to 1.
245 * There could be no FW option available with split count 1.
246 *
247 * Return: zero on success or an error code on failure.
248 */
249static int
250ice_devlink_port_unsplit(struct devlink *devlink, struct devlink_port *port,
251			 struct netlink_ext_ack *extack)
252{
253	return ice_devlink_port_split(devlink, port, 1, extack);
254}
255
256/**
257 * ice_devlink_set_port_split_options - Set port split options
258 * @pf: the PF to set port split options
259 * @attrs: devlink attributes
260 *
261 * Sets devlink port split options based on available FW port options
262 */
263static void
264ice_devlink_set_port_split_options(struct ice_pf *pf,
265				   struct devlink_port_attrs *attrs)
266{
267	struct ice_aqc_get_port_options_elem options[ICE_AQC_PORT_OPT_MAX];
268	u8 i, active_idx, pending_idx, option_count = ICE_AQC_PORT_OPT_MAX;
269	bool active_valid, pending_valid;
270	int status;
271
272	status = ice_aq_get_port_options(&pf->hw, options, &option_count,
273					 0, true, &active_idx, &active_valid,
274					 &pending_idx, &pending_valid);
275	if (status) {
276		dev_dbg(ice_pf_to_dev(pf), "Couldn't read port split options, err = %d\n",
277			status);
278		return;
279	}
280
281	/* find the biggest available port split count */
282	for (i = 0; i < option_count; i++)
283		attrs->lanes = max_t(int, attrs->lanes, options[i].pmd);
284
285	attrs->splittable = attrs->lanes ? 1 : 0;
286	ice_active_port_option = active_idx;
287}
288
289static const struct devlink_port_ops ice_devlink_port_ops = {
290	.port_split = ice_devlink_port_split,
291	.port_unsplit = ice_devlink_port_unsplit,
292};
293
294/**
295 * ice_devlink_set_switch_id - Set unique switch id based on pci dsn
296 * @pf: the PF to create a devlink port for
297 * @ppid: struct with switch id information
298 */
299static void
300ice_devlink_set_switch_id(struct ice_pf *pf, struct netdev_phys_item_id *ppid)
301{
302	struct pci_dev *pdev = pf->pdev;
303	u64 id;
304
305	id = pci_get_dsn(pdev);
306
307	ppid->id_len = sizeof(id);
308	put_unaligned_be64(id, &ppid->id);
309}
310
311/**
312 * ice_devlink_create_pf_port - Create a devlink port for this PF
313 * @pf: the PF to create a devlink port for
314 *
315 * Create and register a devlink_port for this PF.
316 * This function has to be called under devl_lock.
317 *
318 * Return: zero on success or an error code on failure.
319 */
320int ice_devlink_create_pf_port(struct ice_pf *pf)
321{
322	struct devlink_port_attrs attrs = {};
323	struct devlink_port *devlink_port;
324	struct devlink *devlink;
325	struct ice_vsi *vsi;
326	struct device *dev;
327	int err;
328
329	devlink = priv_to_devlink(pf);
330
331	dev = ice_pf_to_dev(pf);
332
333	devlink_port = &pf->devlink_port;
334
335	vsi = ice_get_main_vsi(pf);
336	if (!vsi)
337		return -EIO;
338
339	attrs.flavour = DEVLINK_PORT_FLAVOUR_PHYSICAL;
340	attrs.phys.port_number = pf->hw.bus.func;
341
342	/* As FW supports only port split options for whole device,
343	 * set port split options only for first PF.
344	 */
345	if (pf->hw.pf_id == 0)
346		ice_devlink_set_port_split_options(pf, &attrs);
347
348	ice_devlink_set_switch_id(pf, &attrs.switch_id);
349
350	devlink_port_attrs_set(devlink_port, &attrs);
351
352	err = devl_port_register_with_ops(devlink, devlink_port, vsi->idx,
353					  &ice_devlink_port_ops);
354	if (err) {
355		dev_err(dev, "Failed to create devlink port for PF %d, error %d\n",
356			pf->hw.pf_id, err);
357		return err;
358	}
359
360	return 0;
361}
362
363/**
364 * ice_devlink_destroy_pf_port - Destroy the devlink_port for this PF
365 * @pf: the PF to cleanup
366 *
367 * Unregisters the devlink_port structure associated with this PF.
368 * This function has to be called under devl_lock.
369 */
370void ice_devlink_destroy_pf_port(struct ice_pf *pf)
371{
372	devl_port_unregister(&pf->devlink_port);
373}
374
375/**
376 * ice_devlink_create_vf_port - Create a devlink port for this VF
377 * @vf: the VF to create a port for
378 *
379 * Create and register a devlink_port for this VF.
380 *
381 * Return: zero on success or an error code on failure.
382 */
383int ice_devlink_create_vf_port(struct ice_vf *vf)
384{
385	struct devlink_port_attrs attrs = {};
386	struct devlink_port *devlink_port;
387	struct devlink *devlink;
388	struct ice_vsi *vsi;
389	struct device *dev;
390	struct ice_pf *pf;
391	int err;
392
393	pf = vf->pf;
394	dev = ice_pf_to_dev(pf);
395	devlink_port = &vf->devlink_port;
396
397	vsi = ice_get_vf_vsi(vf);
398	if (!vsi)
399		return -EINVAL;
400
401	attrs.flavour = DEVLINK_PORT_FLAVOUR_PCI_VF;
402	attrs.pci_vf.pf = pf->hw.bus.func;
403	attrs.pci_vf.vf = vf->vf_id;
404
405	ice_devlink_set_switch_id(pf, &attrs.switch_id);
406
407	devlink_port_attrs_set(devlink_port, &attrs);
408	devlink = priv_to_devlink(pf);
409
410	err = devlink_port_register(devlink, devlink_port, vsi->idx);
411	if (err) {
412		dev_err(dev, "Failed to create devlink port for VF %d, error %d\n",
413			vf->vf_id, err);
414		return err;
415	}
416
417	return 0;
418}
419
420/**
421 * ice_devlink_destroy_vf_port - Destroy the devlink_port for this VF
422 * @vf: the VF to cleanup
423 *
424 * Unregisters the devlink_port structure associated with this VF.
425 */
426void ice_devlink_destroy_vf_port(struct ice_vf *vf)
427{
428	devl_rate_leaf_destroy(&vf->devlink_port);
429	devlink_port_unregister(&vf->devlink_port);
430}
431