summaryrefslogtreecommitdiff
path: root/drivers/net/ethernet/amd/pds_core/devlink.c
blob: 63fe45e91f710a1d3e907eedd7e0b09cfc3644bc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
// SPDX-License-Identifier: GPL-2.0
/* Copyright(c) 2023 Advanced Micro Devices, Inc */

#include "core.h"
#include <linux/pds/pds_auxbus.h>

static struct
pdsc_viftype *pdsc_dl_find_viftype_by_id(struct pdsc *pdsc,
					 enum devlink_param_type dl_id)
{
	int vt;

	if (!pdsc->viftype_status)
		return NULL;

	for (vt = 0; vt < PDS_DEV_TYPE_MAX; vt++) {
		if (pdsc->viftype_status[vt].dl_id == dl_id)
			return &pdsc->viftype_status[vt];
	}

	return NULL;
}

int pdsc_dl_enable_get(struct devlink *dl, u32 id,
		       struct devlink_param_gset_ctx *ctx,
		       struct netlink_ext_ack *extack)
{
	struct pdsc *pdsc = devlink_priv(dl);
	struct pdsc_viftype *vt_entry;

	vt_entry = pdsc_dl_find_viftype_by_id(pdsc, id);
	if (!vt_entry)
		return -ENOENT;

	ctx->val.vbool = vt_entry->enabled;

	return 0;
}

int pdsc_dl_enable_set(struct devlink *dl, u32 id,
		       struct devlink_param_gset_ctx *ctx,
		       struct netlink_ext_ack *extack)
{
	struct pdsc *pdsc = devlink_priv(dl);
	struct pdsc_viftype *vt_entry;
	int err = 0;
	int vf_id;

	vt_entry = pdsc_dl_find_viftype_by_id(pdsc, id);
	if (!vt_entry || !vt_entry->supported)
		return -EOPNOTSUPP;

	if (vt_entry->enabled == ctx->val.vbool)
		return 0;

	vt_entry->enabled = ctx->val.vbool;
	for (vf_id = 0; vf_id < pdsc->num_vfs; vf_id++) {
		struct pdsc *vf = pdsc->vfs[vf_id].vf;

		if (ctx->val.vbool)
			err = pdsc_auxbus_dev_add(vf, pdsc, vt_entry->vif_id,
						  &pdsc->vfs[vf_id].padev);
		else
			pdsc_auxbus_dev_del(vf, pdsc, &pdsc->vfs[vf_id].padev);
	}

	return err;
}

int pdsc_dl_enable_validate(struct devlink *dl, u32 id,
			    union devlink_param_value *val,
			    struct netlink_ext_ack *extack)
{
	struct pdsc *pdsc = devlink_priv(dl);
	struct pdsc_viftype *vt_entry;

	vt_entry = pdsc_dl_find_viftype_by_id(pdsc, id);
	if (!vt_entry || !vt_entry->supported)
		return -EOPNOTSUPP;

	if (!pdsc->viftype_status[vt_entry->vif_id].supported)
		return -ENODEV;

	return 0;
}

int pdsc_dl_flash_update(struct devlink *dl,
			 struct devlink_flash_update_params *params,
			 struct netlink_ext_ack *extack)
{
	struct pdsc *pdsc = devlink_priv(dl);

	return pdsc_firmware_update(pdsc, params, extack);
}

static int pdsc_dl_report_component(struct devlink_info_req *req,
				    struct pds_core_fw_component_info *info)
{
	enum devlink_info_version_type ver_type;
	u16 flags = le16_to_cpu(info->flags);
	char *ver = info->version;
	const char *name;
	char buf[32];

	/* Main firmware is reported as generic "fw" */
	if (info->component_type == PDS_CORE_FW_TYPE_MAIN) {
		if (info->slot_id == PDS_CORE_FW_SLOT_GOLD)
			snprintf(buf, sizeof(buf), "fw.gold");
		else
			snprintf(buf, sizeof(buf), "fw");
	} else {
		name = pdsc_fw_type_to_name(info->component_type);
		if (!name)
			return 0;

		if (info->slot_id == PDS_CORE_FW_SLOT_GOLD)
			snprintf(buf, sizeof(buf), "fw.%s.gold", name);
		else
			snprintf(buf, sizeof(buf), "fw.%s", name);
	}

	ver_type = DEVLINK_INFO_VERSION_TYPE_NONE;
	if (flags & PDS_CORE_FW_COMPONENT_INFO_F_UPDATE_BY_NAME)
		ver_type = DEVLINK_INFO_VERSION_TYPE_COMPONENT;

	if (flags & PDS_CORE_FW_COMPONENT_INFO_F_FIXED) {
		int err;

		err = devlink_info_version_fixed_put(req, buf, ver);
		if (err)
			return err;
	}

	if (flags & PDS_CORE_FW_COMPONENT_INFO_F_RUNNING) {
		int err;

		err = devlink_info_version_running_put_ext(req, buf,
							   ver, ver_type);
		if (err)
			return err;
	}

	if (flags & PDS_CORE_FW_COMPONENT_INFO_F_STARTUP) {
		int err;

		err = devlink_info_version_stored_put_ext(req, buf,
							  ver, ver_type);
		if (err)
			return err;
	}

	return 0;
}

static int pdsc_dl_report_fw_ver(struct devlink_info_req *req, char *fw_ver)
{
	return devlink_info_version_running_put(req,
						DEVLINK_INFO_VERSION_GENERIC_FW,
						fw_ver);
}

static int pdsc_dl_component_info_get(struct devlink *dl,
				      struct devlink_info_req *req,
				      struct netlink_ext_ack *extack)
{
	struct pdsc *pdsc = devlink_priv(dl);
	u8 num_components;
	int err;
	int i;

	/* Pairs with WRITE_ONCE in pdsc_fw_components_invalidate().
	 * Use READ_ONCE to get a consistent snapshot of num_components.
	 * pdsc_fw_components_invalidate() can zero it concurrently during
	 * firmware recovery; using the local copy avoids iterating zero
	 * times when we already decided the cache was valid.
	 */
	num_components = READ_ONCE(pdsc->fw_components.num_components);
	if (!num_components) {
		err = pdsc_get_component_info(pdsc);
		if (err)
			return pdsc_dl_report_fw_ver(req,
						    pdsc->dev_info.fw_version);
		num_components = READ_ONCE(pdsc->fw_components.num_components);
		if (!num_components)
			return pdsc_dl_report_fw_ver(req,
						    pdsc->dev_info.fw_version);
	}

	num_components = min_t(u16, num_components,
			       le16_to_cpu(pdsc->dev_ident.max_fw_slots));
	for (i = 0; i < num_components; i++) {
		err = pdsc_dl_report_component(req,
					       &pdsc->fw_components.info[i]);
		if (err)
			return err;
	}

	return 0;
}

static char *fw_slotnames[] = {
	"fw.goldfw",
	"fw.mainfwa",
	"fw.mainfwb",
};

static int pdsc_dl_fw_list_info_get(struct devlink *dl,
				    struct devlink_info_req *req,
				    struct netlink_ext_ack *extack)
{
	union pds_core_dev_cmd cmd = {
		.fw_control.opcode = PDS_CORE_CMD_FW_CONTROL,
		.fw_control.oper = PDS_CORE_FW_GET_LIST,
	};
	struct pds_core_fw_list_info fw_list = {};
	struct pdsc *pdsc = devlink_priv(dl);
	union pds_core_dev_comp comp;
	char buf[32];
	int listlen;
	int err;
	int i;

	mutex_lock(&pdsc->devcmd_lock);
	err = pdsc_devcmd_locked(pdsc, &cmd, &comp, pdsc->devcmd_timeout * 2);
	if (!err)
		memcpy_fromio(&fw_list, pdsc->cmd_regs->data, sizeof(fw_list));
	mutex_unlock(&pdsc->devcmd_lock);

	listlen = min(fw_list.num_fw_slots, ARRAY_SIZE(fw_list.fw_names));
	for (i = 0; i < listlen; i++) {
		char *fw_ver = fw_list.fw_names[i].fw_version;

		if (i < ARRAY_SIZE(fw_slotnames))
			strscpy(buf, fw_slotnames[i], sizeof(buf));
		else
			snprintf(buf, sizeof(buf), "fw.slot_%d", i);
		fw_ver[sizeof(fw_list.fw_names[i].fw_version) - 1] = '\0';
		err = devlink_info_version_stored_put(req, buf, fw_ver);
		if (err)
			return err;
	}

	return 0;
}

static int pdsc_dl_info_get_v1(struct devlink *dl,
			       struct devlink_info_req *req,
			       struct netlink_ext_ack *extack)
{
	struct pdsc *pdsc = devlink_priv(dl);
	int err;

	err = pdsc_dl_fw_list_info_get(dl, req, extack);
	if (err)
		return err;

	/* Version 1: report fw from dev_info (running only) */
	return pdsc_dl_report_fw_ver(req, pdsc->dev_info.fw_version);
}

int pdsc_dl_info_get(struct devlink *dl, struct devlink_info_req *req,
		     struct netlink_ext_ack *extack)
{
	struct pdsc *pdsc = devlink_priv(dl);
	char buf[32];
	int err;

	if (pdsc->dev_ident.version >= PDS_CORE_IDENTITY_VERSION_2) {
		err = pdsc_dl_component_info_get(dl, req, extack);
		if (err)
			return err;
	} else {
		err = pdsc_dl_info_get_v1(dl, req, extack);
		if (err)
			return err;
	}

	snprintf(buf, sizeof(buf), "0x%x", pdsc->dev_info.asic_type);
	err = devlink_info_version_fixed_put(req,
					     DEVLINK_INFO_VERSION_GENERIC_ASIC_ID,
					     buf);
	if (err)
		return err;

	snprintf(buf, sizeof(buf), "0x%x", pdsc->dev_info.asic_rev);
	err = devlink_info_version_fixed_put(req,
					     DEVLINK_INFO_VERSION_GENERIC_ASIC_REV,
					     buf);
	if (err)
		return err;

	return devlink_info_serial_number_put(req, pdsc->dev_info.serial_num);
}

int pdsc_fw_reporter_diagnose(struct devlink_health_reporter *reporter,
			      struct devlink_fmsg *fmsg,
			      struct netlink_ext_ack *extack)
{
	struct pdsc *pdsc = devlink_health_reporter_priv(reporter);

	mutex_lock(&pdsc->config_lock);
	if (test_bit(PDSC_S_FW_DEAD, &pdsc->state))
		devlink_fmsg_string_pair_put(fmsg, "Status", "dead");
	else if (!pdsc_is_fw_good(pdsc))
		devlink_fmsg_string_pair_put(fmsg, "Status", "unhealthy");
	else
		devlink_fmsg_string_pair_put(fmsg, "Status", "healthy");
	mutex_unlock(&pdsc->config_lock);

	devlink_fmsg_u32_pair_put(fmsg, "State",
				  pdsc->fw_status & ~PDS_CORE_FW_STS_F_GENERATION);
	devlink_fmsg_u32_pair_put(fmsg, "Generation", pdsc->fw_generation >> 4);
	devlink_fmsg_u32_pair_put(fmsg, "Recoveries", pdsc->fw_recoveries);

	return 0;
}