summaryrefslogtreecommitdiff
path: root/drivers/dpll/zl3073x/devlink.c
blob: ccc22332b3463b8e1ccc9d58ad57f479a1328758 (plain)
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
// SPDX-License-Identifier: GPL-2.0-only

#include <linux/device/devres.h>
#include <linux/netlink.h>
#include <linux/sprintf.h>
#include <linux/types.h>
#include <net/devlink.h>

#include "core.h"
#include "devlink.h"
#include "dpll.h"
#include "flash.h"
#include "fw.h"
#include "regs.h"

/**
 * zl3073x_devlink_info_get - Devlink device info callback
 * @devlink: devlink structure pointer
 * @req: devlink request pointer to store information
 * @extack: netlink extack pointer to report errors
 *
 * Return: 0 on success, <0 on error
 */
static int
zl3073x_devlink_info_get(struct devlink *devlink, struct devlink_info_req *req,
			 struct netlink_ext_ack *extack)
{
	struct zl3073x_dev *zldev = devlink_priv(devlink);
	u16 id, revision, fw_ver;
	char buf[16];
	u32 cfg_ver;
	int rc;

	rc = zl3073x_read_u16(zldev, ZL_REG_ID, &id);
	if (rc)
		return rc;

	snprintf(buf, sizeof(buf), "%X", id);
	rc = devlink_info_version_fixed_put(req,
					    DEVLINK_INFO_VERSION_GENERIC_ASIC_ID,
					    buf);
	if (rc)
		return rc;

	rc = zl3073x_read_u16(zldev, ZL_REG_REVISION, &revision);
	if (rc)
		return rc;

	snprintf(buf, sizeof(buf), "%X", revision);
	rc = devlink_info_version_fixed_put(req,
					    DEVLINK_INFO_VERSION_GENERIC_ASIC_REV,
					    buf);
	if (rc)
		return rc;

	rc = zl3073x_read_u16(zldev, ZL_REG_FW_VER, &fw_ver);
	if (rc)
		return rc;

	snprintf(buf, sizeof(buf), "%u", fw_ver);
	rc = devlink_info_version_running_put(req,
					      DEVLINK_INFO_VERSION_GENERIC_FW,
					      buf);
	if (rc)
		return rc;

	rc = zl3073x_read_u32(zldev, ZL_REG_CUSTOM_CONFIG_VER, &cfg_ver);
	if (rc)
		return rc;

	/* No custom config version */
	if (cfg_ver == U32_MAX)
		return 0;

	snprintf(buf, sizeof(buf), "%lu.%lu.%lu.%lu",
		 FIELD_GET(GENMASK(31, 24), cfg_ver),
		 FIELD_GET(GENMASK(23, 16), cfg_ver),
		 FIELD_GET(GENMASK(15, 8), cfg_ver),
		 FIELD_GET(GENMASK(7, 0), cfg_ver));

	return devlink_info_version_running_put(req, "custom_cfg", buf);
}

static int
zl3073x_devlink_reload_down(struct devlink *devlink, bool netns_change,
			    enum devlink_reload_action action,
			    enum devlink_reload_limit limit,
			    struct netlink_ext_ack *extack)
{
	struct zl3073x_dev *zldev = devlink_priv(devlink);

	if (action != DEVLINK_RELOAD_ACTION_DRIVER_REINIT)
		return -EOPNOTSUPP;

	/* Stop normal operation */
	zl3073x_dev_stop(zldev);

	return 0;
}

static int
zl3073x_devlink_reload_up(struct devlink *devlink,
			  enum devlink_reload_action action,
			  enum devlink_reload_limit limit,
			  u32 *actions_performed,
			  struct netlink_ext_ack *extack)
{
	struct zl3073x_dev *zldev = devlink_priv(devlink);
	union devlink_param_value val;
	int rc;

	if (action != DEVLINK_RELOAD_ACTION_DRIVER_REINIT)
		return -EOPNOTSUPP;

	rc = devl_param_driverinit_value_get(devlink,
					     DEVLINK_PARAM_GENERIC_ID_CLOCK_ID,
					     &val);
	if (rc)
		return rc;

	if (zldev->clock_id != val.vu64) {
		dev_dbg(zldev->dev,
			"'clock_id' changed to %016llx\n", val.vu64);
		zldev->clock_id = val.vu64;
	}

	/* Restart normal operation */
	rc = zl3073x_dev_start(zldev, false);
	if (rc)
		dev_warn(zldev->dev, "Failed to re-start normal operation\n");

	*actions_performed = BIT(DEVLINK_RELOAD_ACTION_DRIVER_REINIT);

	return 0;
}

void zl3073x_devlink_flash_notify(struct zl3073x_dev *zldev, const char *msg,
				  const char *component, u32 done, u32 total)
{
	struct devlink *devlink = priv_to_devlink(zldev);

	devlink_flash_update_status_notify(devlink, msg, component, done,
					   total);
}

/**
 * zl3073x_devlink_flash_prepare - Prepare and enter flash mode
 * @zldev: zl3073x device pointer
 * @zlfw: pointer to loaded firmware
 * @extack: netlink extack pointer to report errors
 *
 * The function stops normal operation and switches the device to flash mode.
 * If an error occurs the normal operation is resumed.
 *
 * Return: 0 on success, <0 on error
 */
static int
zl3073x_devlink_flash_prepare(struct zl3073x_dev *zldev,
			      struct zl3073x_fw *zlfw,
			      struct netlink_ext_ack *extack)
{
	struct zl3073x_fw_component *util;
	int rc;

	util = zlfw->component[ZL_FW_COMPONENT_UTIL];
	if (!util) {
		zl3073x_devlink_flash_notify(zldev,
					     "Utility is missing in firmware",
					     NULL, 0, 0);
		return -ENOEXEC;
	}

	/* Stop normal operation prior entering flash mode */
	zl3073x_dev_stop(zldev);

	rc = zl3073x_flash_mode_enter(zldev, util->data, util->size, extack);
	if (rc) {
		zl3073x_devlink_flash_notify(zldev,
					     "Failed to enter flash mode",
					     NULL, 0, 0);

		/* Resume normal operation */
		zl3073x_dev_start(zldev, true);

		return rc;
	}

	return 0;
}

/**
 * zl3073x_devlink_flash_finish - Leave flash mode and resume normal operation
 * @zldev: zl3073x device pointer
 * @extack: netlink extack pointer to report errors
 *
 * The function switches the device back to standard mode and resumes normal
 * operation.
 *
 * Return: 0 on success, <0 on error
 */
static int
zl3073x_devlink_flash_finish(struct zl3073x_dev *zldev,
			     struct netlink_ext_ack *extack)
{
	int rc;

	/* Reset device CPU to normal mode */
	zl3073x_flash_mode_leave(zldev, extack);

	/* Resume normal operation */
	rc = zl3073x_dev_start(zldev, true);
	if (rc)
		zl3073x_devlink_flash_notify(zldev,
					     "Failed to start normal operation",
					     NULL, 0, 0);

	return rc;
}

/**
 * zl3073x_devlink_flash_update - Devlink flash update callback
 * @devlink: devlink structure pointer
 * @params: flashing parameters pointer
 * @extack: netlink extack pointer to report errors
 *
 * Return: 0 on success, <0 on error
 */
static int
zl3073x_devlink_flash_update(struct devlink *devlink,
			     struct devlink_flash_update_params *params,
			     struct netlink_ext_ack *extack)
{
	struct zl3073x_dev *zldev = devlink_priv(devlink);
	struct zl3073x_fw *zlfw;
	int rc = 0;

	zlfw = zl3073x_fw_load(zldev, params->fw->data, params->fw->size,
			       extack);
	if (IS_ERR(zlfw)) {
		zl3073x_devlink_flash_notify(zldev, "Failed to load firmware",
					     NULL, 0, 0);
		rc = PTR_ERR(zlfw);
		goto finish;
	}

	/* Stop normal operation and enter flash mode */
	rc = zl3073x_devlink_flash_prepare(zldev, zlfw, extack);
	if (rc)
		goto finish;

	rc = zl3073x_fw_flash(zldev, zlfw, extack);
	if (rc) {
		zl3073x_devlink_flash_finish(zldev, extack);
		goto finish;
	}

	/* Resume normal mode */
	rc = zl3073x_devlink_flash_finish(zldev, extack);

finish:
	if (!IS_ERR(zlfw))
		zl3073x_fw_free(zlfw);

	zl3073x_devlink_flash_notify(zldev,
				     rc ? "Flashing failed" : "Flashing done",
				     NULL, 0, 0);

	return rc;
}

static const struct devlink_ops zl3073x_devlink_ops = {
	.info_get = zl3073x_devlink_info_get,
	.reload_actions = BIT(DEVLINK_RELOAD_ACTION_DRIVER_REINIT),
	.reload_down = zl3073x_devlink_reload_down,
	.reload_up = zl3073x_devlink_reload_up,
	.flash_update = zl3073x_devlink_flash_update,
};

static void
zl3073x_devlink_free(void *ptr)
{
	devlink_free(ptr);
}

/**
 * zl3073x_devm_alloc - allocates zl3073x device structure
 * @dev: pointer to device structure
 *
 * Allocates zl3073x device structure as device resource.
 *
 * Return: pointer to zl3073x device on success, error pointer on error
 */
struct zl3073x_dev *zl3073x_devm_alloc(struct device *dev)
{
	struct zl3073x_dev *zldev;
	struct devlink *devlink;
	int rc;

	devlink = devlink_alloc(&zl3073x_devlink_ops, sizeof(*zldev), dev);
	if (!devlink)
		return ERR_PTR(-ENOMEM);

	/* Add devres action to free devlink device */
	rc = devm_add_action_or_reset(dev, zl3073x_devlink_free, devlink);
	if (rc)
		return ERR_PTR(rc);

	zldev = devlink_priv(devlink);
	zldev->dev = dev;
	dev_set_drvdata(zldev->dev, zldev);

	return zldev;
}
EXPORT_SYMBOL_NS_GPL(zl3073x_devm_alloc, "ZL3073X");

static int
zl3073x_devlink_param_clock_id_validate(struct devlink *devlink, u32 id,
					union devlink_param_value val,
					struct netlink_ext_ack *extack)
{
	if (!val.vu64) {
		NL_SET_ERR_MSG_MOD(extack, "'clock_id' must be non-zero");
		return -EINVAL;
	}

	return 0;
}

static const struct devlink_param zl3073x_devlink_params[] = {
	DEVLINK_PARAM_GENERIC(CLOCK_ID, BIT(DEVLINK_PARAM_CMODE_DRIVERINIT),
			      NULL, NULL,
			      zl3073x_devlink_param_clock_id_validate),
};

static void
zl3073x_devlink_unregister(void *ptr)
{
	struct devlink *devlink = priv_to_devlink(ptr);

	devl_lock(devlink);

	/* Unregister devlink params */
	devl_params_unregister(devlink, zl3073x_devlink_params,
			       ARRAY_SIZE(zl3073x_devlink_params));

	/* Unregister devlink instance */
	devl_unregister(devlink);

	devl_unlock(devlink);
}

/**
 * zl3073x_devlink_register - register devlink instance and params
 * @zldev: zl3073x device to register the devlink for
 *
 * Register the devlink instance and parameters associated with the device.
 *
 * Return: 0 on success, <0 on error
 */
int zl3073x_devlink_register(struct zl3073x_dev *zldev)
{
	struct devlink *devlink = priv_to_devlink(zldev);
	union devlink_param_value value;
	int rc;

	devl_lock(devlink);

	/* Register devlink params */
	rc = devl_params_register(devlink, zl3073x_devlink_params,
				  ARRAY_SIZE(zl3073x_devlink_params));
	if (rc) {
		devl_unlock(devlink);

		return rc;
	}

	value.vu64 = zldev->clock_id;
	devl_param_driverinit_value_set(devlink,
					DEVLINK_PARAM_GENERIC_ID_CLOCK_ID,
					value);

	/* Register devlink instance */
	devl_register(devlink);

	devl_unlock(devlink);

	/* Add devres action to unregister devlink device */
	return devm_add_action_or_reset(zldev->dev, zl3073x_devlink_unregister,
					zldev);
}