Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1// SPDX-License-Identifier: GPL-2.0
2/* Copyright (c) 2020, Intel Corporation. */
3
4#include "ice.h"
5#include "ice_lib.h"
6#include "ice_devlink.h"
7
8static int ice_info_get_dsn(struct ice_pf *pf, char *buf, size_t len)
9{
10 u8 dsn[8];
11
12 /* Copy the DSN into an array in Big Endian format */
13 put_unaligned_be64(pci_get_dsn(pf->pdev), dsn);
14
15 snprintf(buf, len, "%02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x",
16 dsn[0], dsn[1], dsn[2], dsn[3],
17 dsn[4], dsn[5], dsn[6], dsn[7]);
18
19 return 0;
20}
21
22static int ice_info_pba(struct ice_pf *pf, char *buf, size_t len)
23{
24 struct ice_hw *hw = &pf->hw;
25 enum ice_status status;
26
27 status = ice_read_pba_string(hw, (u8 *)buf, len);
28 if (status)
29 return -EIO;
30
31 return 0;
32}
33
34static int ice_info_fw_mgmt(struct ice_pf *pf, char *buf, size_t len)
35{
36 struct ice_hw *hw = &pf->hw;
37
38 snprintf(buf, len, "%u.%u.%u", hw->fw_maj_ver, hw->fw_min_ver,
39 hw->fw_patch);
40
41 return 0;
42}
43
44static int ice_info_fw_api(struct ice_pf *pf, char *buf, size_t len)
45{
46 struct ice_hw *hw = &pf->hw;
47
48 snprintf(buf, len, "%u.%u", hw->api_maj_ver, hw->api_min_ver);
49
50 return 0;
51}
52
53static int ice_info_fw_build(struct ice_pf *pf, char *buf, size_t len)
54{
55 struct ice_hw *hw = &pf->hw;
56
57 snprintf(buf, len, "0x%08x", hw->fw_build);
58
59 return 0;
60}
61
62static int ice_info_orom_ver(struct ice_pf *pf, char *buf, size_t len)
63{
64 struct ice_orom_info *orom = &pf->hw.nvm.orom;
65
66 snprintf(buf, len, "%u.%u.%u", orom->major, orom->build, orom->patch);
67
68 return 0;
69}
70
71static int ice_info_nvm_ver(struct ice_pf *pf, char *buf, size_t len)
72{
73 struct ice_nvm_info *nvm = &pf->hw.nvm;
74
75 snprintf(buf, len, "%x.%02x", nvm->major_ver, nvm->minor_ver);
76
77 return 0;
78}
79
80static int ice_info_eetrack(struct ice_pf *pf, char *buf, size_t len)
81{
82 struct ice_nvm_info *nvm = &pf->hw.nvm;
83
84 snprintf(buf, len, "0x%08x", nvm->eetrack);
85
86 return 0;
87}
88
89static int ice_info_ddp_pkg_name(struct ice_pf *pf, char *buf, size_t len)
90{
91 struct ice_hw *hw = &pf->hw;
92
93 snprintf(buf, len, "%s", hw->active_pkg_name);
94
95 return 0;
96}
97
98static int ice_info_ddp_pkg_version(struct ice_pf *pf, char *buf, size_t len)
99{
100 struct ice_pkg_ver *pkg = &pf->hw.active_pkg_ver;
101
102 snprintf(buf, len, "%u.%u.%u.%u", pkg->major, pkg->minor, pkg->update,
103 pkg->draft);
104
105 return 0;
106}
107
108#define fixed(key, getter) { ICE_VERSION_FIXED, key, getter }
109#define running(key, getter) { ICE_VERSION_RUNNING, key, getter }
110
111enum ice_version_type {
112 ICE_VERSION_FIXED,
113 ICE_VERSION_RUNNING,
114 ICE_VERSION_STORED,
115};
116
117static const struct ice_devlink_version {
118 enum ice_version_type type;
119 const char *key;
120 int (*getter)(struct ice_pf *pf, char *buf, size_t len);
121} ice_devlink_versions[] = {
122 fixed(DEVLINK_INFO_VERSION_GENERIC_BOARD_ID, ice_info_pba),
123 running(DEVLINK_INFO_VERSION_GENERIC_FW_MGMT, ice_info_fw_mgmt),
124 running("fw.mgmt.api", ice_info_fw_api),
125 running("fw.mgmt.build", ice_info_fw_build),
126 running(DEVLINK_INFO_VERSION_GENERIC_FW_UNDI, ice_info_orom_ver),
127 running("fw.psid.api", ice_info_nvm_ver),
128 running(DEVLINK_INFO_VERSION_GENERIC_FW_BUNDLE_ID, ice_info_eetrack),
129 running("fw.app.name", ice_info_ddp_pkg_name),
130 running(DEVLINK_INFO_VERSION_GENERIC_FW_APP, ice_info_ddp_pkg_version),
131};
132
133/**
134 * ice_devlink_info_get - .info_get devlink handler
135 * @devlink: devlink instance structure
136 * @req: the devlink info request
137 * @extack: extended netdev ack structure
138 *
139 * Callback for the devlink .info_get operation. Reports information about the
140 * device.
141 *
142 * Return: zero on success or an error code on failure.
143 */
144static int ice_devlink_info_get(struct devlink *devlink,
145 struct devlink_info_req *req,
146 struct netlink_ext_ack *extack)
147{
148 struct ice_pf *pf = devlink_priv(devlink);
149 char buf[100];
150 size_t i;
151 int err;
152
153 err = devlink_info_driver_name_put(req, KBUILD_MODNAME);
154 if (err) {
155 NL_SET_ERR_MSG_MOD(extack, "Unable to set driver name");
156 return err;
157 }
158
159 err = ice_info_get_dsn(pf, buf, sizeof(buf));
160 if (err) {
161 NL_SET_ERR_MSG_MOD(extack, "Unable to obtain serial number");
162 return err;
163 }
164
165 err = devlink_info_serial_number_put(req, buf);
166 if (err) {
167 NL_SET_ERR_MSG_MOD(extack, "Unable to set serial number");
168 return err;
169 }
170
171 for (i = 0; i < ARRAY_SIZE(ice_devlink_versions); i++) {
172 enum ice_version_type type = ice_devlink_versions[i].type;
173 const char *key = ice_devlink_versions[i].key;
174
175 err = ice_devlink_versions[i].getter(pf, buf, sizeof(buf));
176 if (err) {
177 NL_SET_ERR_MSG_MOD(extack, "Unable to obtain version info");
178 return err;
179 }
180
181 switch (type) {
182 case ICE_VERSION_FIXED:
183 err = devlink_info_version_fixed_put(req, key, buf);
184 if (err) {
185 NL_SET_ERR_MSG_MOD(extack, "Unable to set fixed version");
186 return err;
187 }
188 break;
189 case ICE_VERSION_RUNNING:
190 err = devlink_info_version_running_put(req, key, buf);
191 if (err) {
192 NL_SET_ERR_MSG_MOD(extack, "Unable to set running version");
193 return err;
194 }
195 break;
196 case ICE_VERSION_STORED:
197 err = devlink_info_version_stored_put(req, key, buf);
198 if (err) {
199 NL_SET_ERR_MSG_MOD(extack, "Unable to set stored version");
200 return err;
201 }
202 break;
203 }
204 }
205
206 return 0;
207}
208
209static const struct devlink_ops ice_devlink_ops = {
210 .info_get = ice_devlink_info_get,
211};
212
213static void ice_devlink_free(void *devlink_ptr)
214{
215 devlink_free((struct devlink *)devlink_ptr);
216}
217
218/**
219 * ice_allocate_pf - Allocate devlink and return PF structure pointer
220 * @dev: the device to allocate for
221 *
222 * Allocate a devlink instance for this device and return the private area as
223 * the PF structure. The devlink memory is kept track of through devres by
224 * adding an action to remove it when unwinding.
225 */
226struct ice_pf *ice_allocate_pf(struct device *dev)
227{
228 struct devlink *devlink;
229
230 devlink = devlink_alloc(&ice_devlink_ops, sizeof(struct ice_pf));
231 if (!devlink)
232 return NULL;
233
234 /* Add an action to teardown the devlink when unwinding the driver */
235 if (devm_add_action(dev, ice_devlink_free, devlink)) {
236 devlink_free(devlink);
237 return NULL;
238 }
239
240 return devlink_priv(devlink);
241}
242
243/**
244 * ice_devlink_register - Register devlink interface for this PF
245 * @pf: the PF to register the devlink for.
246 *
247 * Register the devlink instance associated with this physical function.
248 *
249 * Return: zero on success or an error code on failure.
250 */
251int ice_devlink_register(struct ice_pf *pf)
252{
253 struct devlink *devlink = priv_to_devlink(pf);
254 struct device *dev = ice_pf_to_dev(pf);
255 int err;
256
257 err = devlink_register(devlink, dev);
258 if (err) {
259 dev_err(dev, "devlink registration failed: %d\n", err);
260 return err;
261 }
262
263 return 0;
264}
265
266/**
267 * ice_devlink_unregister - Unregister devlink resources for this PF.
268 * @pf: the PF structure to cleanup
269 *
270 * Releases resources used by devlink and cleans up associated memory.
271 */
272void ice_devlink_unregister(struct ice_pf *pf)
273{
274 devlink_unregister(priv_to_devlink(pf));
275}
276
277/**
278 * ice_devlink_create_port - Create a devlink port for this PF
279 * @pf: the PF to create a port for
280 *
281 * Create and register a devlink_port for this PF. Note that although each
282 * physical function is connected to a separate devlink instance, the port
283 * will still be numbered according to the physical function id.
284 *
285 * Return: zero on success or an error code on failure.
286 */
287int ice_devlink_create_port(struct ice_pf *pf)
288{
289 struct devlink *devlink = priv_to_devlink(pf);
290 struct ice_vsi *vsi = ice_get_main_vsi(pf);
291 struct device *dev = ice_pf_to_dev(pf);
292 int err;
293
294 if (!vsi) {
295 dev_err(dev, "%s: unable to find main VSI\n", __func__);
296 return -EIO;
297 }
298
299 devlink_port_attrs_set(&pf->devlink_port, DEVLINK_PORT_FLAVOUR_PHYSICAL,
300 pf->hw.pf_id, false, 0, NULL, 0);
301 err = devlink_port_register(devlink, &pf->devlink_port, pf->hw.pf_id);
302 if (err) {
303 dev_err(dev, "devlink_port_register failed: %d\n", err);
304 return err;
305 }
306
307 return 0;
308}
309
310/**
311 * ice_devlink_destroy_port - Destroy the devlink_port for this PF
312 * @pf: the PF to cleanup
313 *
314 * Unregisters the devlink_port structure associated with this PF.
315 */
316void ice_devlink_destroy_port(struct ice_pf *pf)
317{
318 devlink_port_type_clear(&pf->devlink_port);
319 devlink_port_unregister(&pf->devlink_port);
320}
321
322/**
323 * ice_devlink_nvm_snapshot - Capture a snapshot of the Shadow RAM contents
324 * @devlink: the devlink instance
325 * @extack: extended ACK response structure
326 * @data: on exit points to snapshot data buffer
327 *
328 * This function is called in response to the DEVLINK_CMD_REGION_TRIGGER for
329 * the shadow-ram devlink region. It captures a snapshot of the shadow ram
330 * contents. This snapshot can later be viewed via the devlink-region
331 * interface.
332 *
333 * @returns zero on success, and updates the data pointer. Returns a non-zero
334 * error code on failure.
335 */
336static int ice_devlink_nvm_snapshot(struct devlink *devlink,
337 struct netlink_ext_ack *extack, u8 **data)
338{
339 struct ice_pf *pf = devlink_priv(devlink);
340 struct device *dev = ice_pf_to_dev(pf);
341 struct ice_hw *hw = &pf->hw;
342 enum ice_status status;
343 void *nvm_data;
344 u32 nvm_size;
345
346 nvm_size = hw->nvm.flash_size;
347 nvm_data = vzalloc(nvm_size);
348 if (!nvm_data)
349 return -ENOMEM;
350
351 status = ice_acquire_nvm(hw, ICE_RES_READ);
352 if (status) {
353 dev_dbg(dev, "ice_acquire_nvm failed, err %d aq_err %d\n",
354 status, hw->adminq.sq_last_status);
355 NL_SET_ERR_MSG_MOD(extack, "Failed to acquire NVM semaphore");
356 vfree(nvm_data);
357 return -EIO;
358 }
359
360 status = ice_read_flat_nvm(hw, 0, &nvm_size, nvm_data, false);
361 if (status) {
362 dev_dbg(dev, "ice_read_flat_nvm failed after reading %u bytes, err %d aq_err %d\n",
363 nvm_size, status, hw->adminq.sq_last_status);
364 NL_SET_ERR_MSG_MOD(extack, "Failed to read NVM contents");
365 ice_release_nvm(hw);
366 vfree(nvm_data);
367 return -EIO;
368 }
369
370 ice_release_nvm(hw);
371
372 *data = nvm_data;
373
374 return 0;
375}
376
377static const struct devlink_region_ops ice_nvm_region_ops = {
378 .name = "nvm-flash",
379 .destructor = vfree,
380 .snapshot = ice_devlink_nvm_snapshot,
381};
382
383/**
384 * ice_devlink_init_regions - Initialize devlink regions
385 * @pf: the PF device structure
386 *
387 * Create devlink regions used to enable access to dump the contents of the
388 * flash memory on the device.
389 */
390void ice_devlink_init_regions(struct ice_pf *pf)
391{
392 struct devlink *devlink = priv_to_devlink(pf);
393 struct device *dev = ice_pf_to_dev(pf);
394 u64 nvm_size;
395
396 nvm_size = pf->hw.nvm.flash_size;
397 pf->nvm_region = devlink_region_create(devlink, &ice_nvm_region_ops, 1,
398 nvm_size);
399 if (IS_ERR(pf->nvm_region)) {
400 dev_err(dev, "failed to create NVM devlink region, err %ld\n",
401 PTR_ERR(pf->nvm_region));
402 pf->nvm_region = NULL;
403 }
404}
405
406/**
407 * ice_devlink_destroy_regions - Destroy devlink regions
408 * @pf: the PF device structure
409 *
410 * Remove previously created regions for this PF.
411 */
412void ice_devlink_destroy_regions(struct ice_pf *pf)
413{
414 if (pf->nvm_region)
415 devlink_region_destroy(pf->nvm_region);
416}