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 
8 static 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 
22 static 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 
34 static 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 
44 static 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 
53 static 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 
62 static 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 
71 static 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 
80 static 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 
89 static 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 
98 static 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 static int ice_info_netlist_ver(struct ice_pf *pf, char *buf, size_t len)
109 {
110 	struct ice_netlist_ver_info *netlist = &pf->hw.netlist_ver;
111 
112 	/* The netlist version fields are BCD formatted */
113 	snprintf(buf, len, "%x.%x.%x-%x.%x.%x", netlist->major, netlist->minor,
114 		 netlist->type >> 16, netlist->type & 0xFFFF, netlist->rev,
115 		 netlist->cust_ver);
116 
117 	return 0;
118 }
119 
120 static int ice_info_netlist_build(struct ice_pf *pf, char *buf, size_t len)
121 {
122 	struct ice_netlist_ver_info *netlist = &pf->hw.netlist_ver;
123 
124 	snprintf(buf, len, "0x%08x", netlist->hash);
125 
126 	return 0;
127 }
128 
129 #define fixed(key, getter) { ICE_VERSION_FIXED, key, getter }
130 #define running(key, getter) { ICE_VERSION_RUNNING, key, getter }
131 
132 enum ice_version_type {
133 	ICE_VERSION_FIXED,
134 	ICE_VERSION_RUNNING,
135 	ICE_VERSION_STORED,
136 };
137 
138 static const struct ice_devlink_version {
139 	enum ice_version_type type;
140 	const char *key;
141 	int (*getter)(struct ice_pf *pf, char *buf, size_t len);
142 } ice_devlink_versions[] = {
143 	fixed(DEVLINK_INFO_VERSION_GENERIC_BOARD_ID, ice_info_pba),
144 	running(DEVLINK_INFO_VERSION_GENERIC_FW_MGMT, ice_info_fw_mgmt),
145 	running("fw.mgmt.api", ice_info_fw_api),
146 	running("fw.mgmt.build", ice_info_fw_build),
147 	running(DEVLINK_INFO_VERSION_GENERIC_FW_UNDI, ice_info_orom_ver),
148 	running("fw.psid.api", ice_info_nvm_ver),
149 	running(DEVLINK_INFO_VERSION_GENERIC_FW_BUNDLE_ID, ice_info_eetrack),
150 	running("fw.app.name", ice_info_ddp_pkg_name),
151 	running(DEVLINK_INFO_VERSION_GENERIC_FW_APP, ice_info_ddp_pkg_version),
152 	running("fw.netlist", ice_info_netlist_ver),
153 	running("fw.netlist.build", ice_info_netlist_build),
154 };
155 
156 /**
157  * ice_devlink_info_get - .info_get devlink handler
158  * @devlink: devlink instance structure
159  * @req: the devlink info request
160  * @extack: extended netdev ack structure
161  *
162  * Callback for the devlink .info_get operation. Reports information about the
163  * device.
164  *
165  * Return: zero on success or an error code on failure.
166  */
167 static int ice_devlink_info_get(struct devlink *devlink,
168 				struct devlink_info_req *req,
169 				struct netlink_ext_ack *extack)
170 {
171 	struct ice_pf *pf = devlink_priv(devlink);
172 	char buf[100];
173 	size_t i;
174 	int err;
175 
176 	err = devlink_info_driver_name_put(req, KBUILD_MODNAME);
177 	if (err) {
178 		NL_SET_ERR_MSG_MOD(extack, "Unable to set driver name");
179 		return err;
180 	}
181 
182 	err = ice_info_get_dsn(pf, buf, sizeof(buf));
183 	if (err) {
184 		NL_SET_ERR_MSG_MOD(extack, "Unable to obtain serial number");
185 		return err;
186 	}
187 
188 	err = devlink_info_serial_number_put(req, buf);
189 	if (err) {
190 		NL_SET_ERR_MSG_MOD(extack, "Unable to set serial number");
191 		return err;
192 	}
193 
194 	for (i = 0; i < ARRAY_SIZE(ice_devlink_versions); i++) {
195 		enum ice_version_type type = ice_devlink_versions[i].type;
196 		const char *key = ice_devlink_versions[i].key;
197 
198 		err = ice_devlink_versions[i].getter(pf, buf, sizeof(buf));
199 		if (err) {
200 			NL_SET_ERR_MSG_MOD(extack, "Unable to obtain version info");
201 			return err;
202 		}
203 
204 		switch (type) {
205 		case ICE_VERSION_FIXED:
206 			err = devlink_info_version_fixed_put(req, key, buf);
207 			if (err) {
208 				NL_SET_ERR_MSG_MOD(extack, "Unable to set fixed version");
209 				return err;
210 			}
211 			break;
212 		case ICE_VERSION_RUNNING:
213 			err = devlink_info_version_running_put(req, key, buf);
214 			if (err) {
215 				NL_SET_ERR_MSG_MOD(extack, "Unable to set running version");
216 				return err;
217 			}
218 			break;
219 		case ICE_VERSION_STORED:
220 			err = devlink_info_version_stored_put(req, key, buf);
221 			if (err) {
222 				NL_SET_ERR_MSG_MOD(extack, "Unable to set stored version");
223 				return err;
224 			}
225 			break;
226 		}
227 	}
228 
229 	return 0;
230 }
231 
232 static const struct devlink_ops ice_devlink_ops = {
233 	.info_get = ice_devlink_info_get,
234 };
235 
236 static void ice_devlink_free(void *devlink_ptr)
237 {
238 	devlink_free((struct devlink *)devlink_ptr);
239 }
240 
241 /**
242  * ice_allocate_pf - Allocate devlink and return PF structure pointer
243  * @dev: the device to allocate for
244  *
245  * Allocate a devlink instance for this device and return the private area as
246  * the PF structure. The devlink memory is kept track of through devres by
247  * adding an action to remove it when unwinding.
248  */
249 struct ice_pf *ice_allocate_pf(struct device *dev)
250 {
251 	struct devlink *devlink;
252 
253 	devlink = devlink_alloc(&ice_devlink_ops, sizeof(struct ice_pf));
254 	if (!devlink)
255 		return NULL;
256 
257 	/* Add an action to teardown the devlink when unwinding the driver */
258 	if (devm_add_action(dev, ice_devlink_free, devlink)) {
259 		devlink_free(devlink);
260 		return NULL;
261 	}
262 
263 	return devlink_priv(devlink);
264 }
265 
266 /**
267  * ice_devlink_register - Register devlink interface for this PF
268  * @pf: the PF to register the devlink for.
269  *
270  * Register the devlink instance associated with this physical function.
271  *
272  * Return: zero on success or an error code on failure.
273  */
274 int ice_devlink_register(struct ice_pf *pf)
275 {
276 	struct devlink *devlink = priv_to_devlink(pf);
277 	struct device *dev = ice_pf_to_dev(pf);
278 	int err;
279 
280 	err = devlink_register(devlink, dev);
281 	if (err) {
282 		dev_err(dev, "devlink registration failed: %d\n", err);
283 		return err;
284 	}
285 
286 	return 0;
287 }
288 
289 /**
290  * ice_devlink_unregister - Unregister devlink resources for this PF.
291  * @pf: the PF structure to cleanup
292  *
293  * Releases resources used by devlink and cleans up associated memory.
294  */
295 void ice_devlink_unregister(struct ice_pf *pf)
296 {
297 	devlink_unregister(priv_to_devlink(pf));
298 }
299 
300 /**
301  * ice_devlink_create_port - Create a devlink port for this PF
302  * @pf: the PF to create a port for
303  *
304  * Create and register a devlink_port for this PF. Note that although each
305  * physical function is connected to a separate devlink instance, the port
306  * will still be numbered according to the physical function id.
307  *
308  * Return: zero on success or an error code on failure.
309  */
310 int ice_devlink_create_port(struct ice_pf *pf)
311 {
312 	struct devlink *devlink = priv_to_devlink(pf);
313 	struct ice_vsi *vsi = ice_get_main_vsi(pf);
314 	struct device *dev = ice_pf_to_dev(pf);
315 	int err;
316 
317 	if (!vsi) {
318 		dev_err(dev, "%s: unable to find main VSI\n", __func__);
319 		return -EIO;
320 	}
321 
322 	devlink_port_attrs_set(&pf->devlink_port, DEVLINK_PORT_FLAVOUR_PHYSICAL,
323 			       pf->hw.pf_id, false, 0, NULL, 0);
324 	err = devlink_port_register(devlink, &pf->devlink_port, pf->hw.pf_id);
325 	if (err) {
326 		dev_err(dev, "devlink_port_register failed: %d\n", err);
327 		return err;
328 	}
329 
330 	return 0;
331 }
332 
333 /**
334  * ice_devlink_destroy_port - Destroy the devlink_port for this PF
335  * @pf: the PF to cleanup
336  *
337  * Unregisters the devlink_port structure associated with this PF.
338  */
339 void ice_devlink_destroy_port(struct ice_pf *pf)
340 {
341 	devlink_port_type_clear(&pf->devlink_port);
342 	devlink_port_unregister(&pf->devlink_port);
343 }
344 
345 /**
346  * ice_devlink_nvm_snapshot - Capture a snapshot of the Shadow RAM contents
347  * @devlink: the devlink instance
348  * @extack: extended ACK response structure
349  * @data: on exit points to snapshot data buffer
350  *
351  * This function is called in response to the DEVLINK_CMD_REGION_TRIGGER for
352  * the shadow-ram devlink region. It captures a snapshot of the shadow ram
353  * contents. This snapshot can later be viewed via the devlink-region
354  * interface.
355  *
356  * @returns zero on success, and updates the data pointer. Returns a non-zero
357  * error code on failure.
358  */
359 static int ice_devlink_nvm_snapshot(struct devlink *devlink,
360 				    struct netlink_ext_ack *extack, u8 **data)
361 {
362 	struct ice_pf *pf = devlink_priv(devlink);
363 	struct device *dev = ice_pf_to_dev(pf);
364 	struct ice_hw *hw = &pf->hw;
365 	enum ice_status status;
366 	void *nvm_data;
367 	u32 nvm_size;
368 
369 	nvm_size = hw->nvm.flash_size;
370 	nvm_data = vzalloc(nvm_size);
371 	if (!nvm_data)
372 		return -ENOMEM;
373 
374 	status = ice_acquire_nvm(hw, ICE_RES_READ);
375 	if (status) {
376 		dev_dbg(dev, "ice_acquire_nvm failed, err %d aq_err %d\n",
377 			status, hw->adminq.sq_last_status);
378 		NL_SET_ERR_MSG_MOD(extack, "Failed to acquire NVM semaphore");
379 		vfree(nvm_data);
380 		return -EIO;
381 	}
382 
383 	status = ice_read_flat_nvm(hw, 0, &nvm_size, nvm_data, false);
384 	if (status) {
385 		dev_dbg(dev, "ice_read_flat_nvm failed after reading %u bytes, err %d aq_err %d\n",
386 			nvm_size, status, hw->adminq.sq_last_status);
387 		NL_SET_ERR_MSG_MOD(extack, "Failed to read NVM contents");
388 		ice_release_nvm(hw);
389 		vfree(nvm_data);
390 		return -EIO;
391 	}
392 
393 	ice_release_nvm(hw);
394 
395 	*data = nvm_data;
396 
397 	return 0;
398 }
399 
400 static const struct devlink_region_ops ice_nvm_region_ops = {
401 	.name = "nvm-flash",
402 	.destructor = vfree,
403 	.snapshot = ice_devlink_nvm_snapshot,
404 };
405 
406 /**
407  * ice_devlink_init_regions - Initialize devlink regions
408  * @pf: the PF device structure
409  *
410  * Create devlink regions used to enable access to dump the contents of the
411  * flash memory on the device.
412  */
413 void ice_devlink_init_regions(struct ice_pf *pf)
414 {
415 	struct devlink *devlink = priv_to_devlink(pf);
416 	struct device *dev = ice_pf_to_dev(pf);
417 	u64 nvm_size;
418 
419 	nvm_size = pf->hw.nvm.flash_size;
420 	pf->nvm_region = devlink_region_create(devlink, &ice_nvm_region_ops, 1,
421 					       nvm_size);
422 	if (IS_ERR(pf->nvm_region)) {
423 		dev_err(dev, "failed to create NVM devlink region, err %ld\n",
424 			PTR_ERR(pf->nvm_region));
425 		pf->nvm_region = NULL;
426 	}
427 }
428 
429 /**
430  * ice_devlink_destroy_regions - Destroy devlink regions
431  * @pf: the PF device structure
432  *
433  * Remove previously created regions for this PF.
434  */
435 void ice_devlink_destroy_regions(struct ice_pf *pf)
436 {
437 	if (pf->nvm_region)
438 		devlink_region_destroy(pf->nvm_region);
439 }
440