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 #include "ice_fw_update.h"
8 
ice_info_get_dsn(struct ice_pf * pf,char * buf,size_t len)9 static void ice_info_get_dsn(struct ice_pf *pf, char *buf, size_t len)
10 {
11 	u8 dsn[8];
12 
13 	/* Copy the DSN into an array in Big Endian format */
14 	put_unaligned_be64(pci_get_dsn(pf->pdev), dsn);
15 
16 	snprintf(buf, len, "%8phD", dsn);
17 }
18 
ice_info_pba(struct ice_pf * pf,char * buf,size_t len)19 static int ice_info_pba(struct ice_pf *pf, char *buf, size_t len)
20 {
21 	struct ice_hw *hw = &pf->hw;
22 	enum ice_status status;
23 
24 	status = ice_read_pba_string(hw, (u8 *)buf, len);
25 	if (status)
26 		return -EIO;
27 
28 	return 0;
29 }
30 
ice_info_fw_mgmt(struct ice_pf * pf,char * buf,size_t len)31 static int ice_info_fw_mgmt(struct ice_pf *pf, char *buf, size_t len)
32 {
33 	struct ice_hw *hw = &pf->hw;
34 
35 	snprintf(buf, len, "%u.%u.%u", hw->fw_maj_ver, hw->fw_min_ver,
36 		 hw->fw_patch);
37 
38 	return 0;
39 }
40 
ice_info_fw_api(struct ice_pf * pf,char * buf,size_t len)41 static int ice_info_fw_api(struct ice_pf *pf, char *buf, size_t len)
42 {
43 	struct ice_hw *hw = &pf->hw;
44 
45 	snprintf(buf, len, "%u.%u", hw->api_maj_ver, hw->api_min_ver);
46 
47 	return 0;
48 }
49 
ice_info_fw_build(struct ice_pf * pf,char * buf,size_t len)50 static int ice_info_fw_build(struct ice_pf *pf, char *buf, size_t len)
51 {
52 	struct ice_hw *hw = &pf->hw;
53 
54 	snprintf(buf, len, "0x%08x", hw->fw_build);
55 
56 	return 0;
57 }
58 
ice_info_orom_ver(struct ice_pf * pf,char * buf,size_t len)59 static int ice_info_orom_ver(struct ice_pf *pf, char *buf, size_t len)
60 {
61 	struct ice_orom_info *orom = &pf->hw.nvm.orom;
62 
63 	snprintf(buf, len, "%u.%u.%u", orom->major, orom->build, orom->patch);
64 
65 	return 0;
66 }
67 
ice_info_nvm_ver(struct ice_pf * pf,char * buf,size_t len)68 static int ice_info_nvm_ver(struct ice_pf *pf, char *buf, size_t len)
69 {
70 	struct ice_nvm_info *nvm = &pf->hw.nvm;
71 
72 	snprintf(buf, len, "%x.%02x", nvm->major_ver, nvm->minor_ver);
73 
74 	return 0;
75 }
76 
ice_info_eetrack(struct ice_pf * pf,char * buf,size_t len)77 static int ice_info_eetrack(struct ice_pf *pf, char *buf, size_t len)
78 {
79 	struct ice_nvm_info *nvm = &pf->hw.nvm;
80 
81 	snprintf(buf, len, "0x%08x", nvm->eetrack);
82 
83 	return 0;
84 }
85 
ice_info_ddp_pkg_name(struct ice_pf * pf,char * buf,size_t len)86 static int ice_info_ddp_pkg_name(struct ice_pf *pf, char *buf, size_t len)
87 {
88 	struct ice_hw *hw = &pf->hw;
89 
90 	snprintf(buf, len, "%s", hw->active_pkg_name);
91 
92 	return 0;
93 }
94 
ice_info_ddp_pkg_version(struct ice_pf * pf,char * buf,size_t len)95 static int ice_info_ddp_pkg_version(struct ice_pf *pf, char *buf, size_t len)
96 {
97 	struct ice_pkg_ver *pkg = &pf->hw.active_pkg_ver;
98 
99 	snprintf(buf, len, "%u.%u.%u.%u", pkg->major, pkg->minor, pkg->update,
100 		 pkg->draft);
101 
102 	return 0;
103 }
104 
ice_info_ddp_pkg_bundle_id(struct ice_pf * pf,char * buf,size_t len)105 static int ice_info_ddp_pkg_bundle_id(struct ice_pf *pf, char *buf, size_t len)
106 {
107 	snprintf(buf, len, "0x%08x", pf->hw.active_track_id);
108 
109 	return 0;
110 }
111 
ice_info_netlist_ver(struct ice_pf * pf,char * buf,size_t len)112 static int ice_info_netlist_ver(struct ice_pf *pf, char *buf, size_t len)
113 {
114 	struct ice_netlist_ver_info *netlist = &pf->hw.netlist_ver;
115 
116 	/* The netlist version fields are BCD formatted */
117 	snprintf(buf, len, "%x.%x.%x-%x.%x.%x", netlist->major, netlist->minor,
118 		 netlist->type >> 16, netlist->type & 0xFFFF, netlist->rev,
119 		 netlist->cust_ver);
120 
121 	return 0;
122 }
123 
ice_info_netlist_build(struct ice_pf * pf,char * buf,size_t len)124 static int ice_info_netlist_build(struct ice_pf *pf, char *buf, size_t len)
125 {
126 	struct ice_netlist_ver_info *netlist = &pf->hw.netlist_ver;
127 
128 	snprintf(buf, len, "0x%08x", netlist->hash);
129 
130 	return 0;
131 }
132 
133 #define fixed(key, getter) { ICE_VERSION_FIXED, key, getter }
134 #define running(key, getter) { ICE_VERSION_RUNNING, key, getter }
135 
136 enum ice_version_type {
137 	ICE_VERSION_FIXED,
138 	ICE_VERSION_RUNNING,
139 	ICE_VERSION_STORED,
140 };
141 
142 static const struct ice_devlink_version {
143 	enum ice_version_type type;
144 	const char *key;
145 	int (*getter)(struct ice_pf *pf, char *buf, size_t len);
146 } ice_devlink_versions[] = {
147 	fixed(DEVLINK_INFO_VERSION_GENERIC_BOARD_ID, ice_info_pba),
148 	running(DEVLINK_INFO_VERSION_GENERIC_FW_MGMT, ice_info_fw_mgmt),
149 	running("fw.mgmt.api", ice_info_fw_api),
150 	running("fw.mgmt.build", ice_info_fw_build),
151 	running(DEVLINK_INFO_VERSION_GENERIC_FW_UNDI, ice_info_orom_ver),
152 	running("fw.psid.api", ice_info_nvm_ver),
153 	running(DEVLINK_INFO_VERSION_GENERIC_FW_BUNDLE_ID, ice_info_eetrack),
154 	running("fw.app.name", ice_info_ddp_pkg_name),
155 	running(DEVLINK_INFO_VERSION_GENERIC_FW_APP, ice_info_ddp_pkg_version),
156 	running("fw.app.bundle_id", ice_info_ddp_pkg_bundle_id),
157 	running("fw.netlist", ice_info_netlist_ver),
158 	running("fw.netlist.build", ice_info_netlist_build),
159 };
160 
161 /**
162  * ice_devlink_info_get - .info_get devlink handler
163  * @devlink: devlink instance structure
164  * @req: the devlink info request
165  * @extack: extended netdev ack structure
166  *
167  * Callback for the devlink .info_get operation. Reports information about the
168  * device.
169  *
170  * Return: zero on success or an error code on failure.
171  */
ice_devlink_info_get(struct devlink * devlink,struct devlink_info_req * req,struct netlink_ext_ack * extack)172 static int ice_devlink_info_get(struct devlink *devlink,
173 				struct devlink_info_req *req,
174 				struct netlink_ext_ack *extack)
175 {
176 	struct ice_pf *pf = devlink_priv(devlink);
177 	char buf[100];
178 	size_t i;
179 	int err;
180 
181 	err = devlink_info_driver_name_put(req, KBUILD_MODNAME);
182 	if (err) {
183 		NL_SET_ERR_MSG_MOD(extack, "Unable to set driver name");
184 		return err;
185 	}
186 
187 	ice_info_get_dsn(pf, buf, sizeof(buf));
188 
189 	err = devlink_info_serial_number_put(req, buf);
190 	if (err) {
191 		NL_SET_ERR_MSG_MOD(extack, "Unable to set serial number");
192 		return err;
193 	}
194 
195 	for (i = 0; i < ARRAY_SIZE(ice_devlink_versions); i++) {
196 		enum ice_version_type type = ice_devlink_versions[i].type;
197 		const char *key = ice_devlink_versions[i].key;
198 
199 		err = ice_devlink_versions[i].getter(pf, buf, sizeof(buf));
200 		if (err) {
201 			NL_SET_ERR_MSG_MOD(extack, "Unable to obtain version info");
202 			return err;
203 		}
204 
205 		switch (type) {
206 		case ICE_VERSION_FIXED:
207 			err = devlink_info_version_fixed_put(req, key, buf);
208 			if (err) {
209 				NL_SET_ERR_MSG_MOD(extack, "Unable to set fixed version");
210 				return err;
211 			}
212 			break;
213 		case ICE_VERSION_RUNNING:
214 			err = devlink_info_version_running_put(req, key, buf);
215 			if (err) {
216 				NL_SET_ERR_MSG_MOD(extack, "Unable to set running version");
217 				return err;
218 			}
219 			break;
220 		case ICE_VERSION_STORED:
221 			err = devlink_info_version_stored_put(req, key, buf);
222 			if (err) {
223 				NL_SET_ERR_MSG_MOD(extack, "Unable to set stored version");
224 				return err;
225 			}
226 			break;
227 		}
228 	}
229 
230 	return 0;
231 }
232 
233 /**
234  * ice_devlink_flash_update - Update firmware stored in flash on the device
235  * @devlink: pointer to devlink associated with device to update
236  * @params: flash update parameters
237  * @extack: netlink extended ACK structure
238  *
239  * Perform a device flash update. The bulk of the update logic is contained
240  * within the ice_flash_pldm_image function.
241  *
242  * Returns: zero on success, or an error code on failure.
243  */
244 static int
ice_devlink_flash_update(struct devlink * devlink,struct devlink_flash_update_params * params,struct netlink_ext_ack * extack)245 ice_devlink_flash_update(struct devlink *devlink,
246 			 struct devlink_flash_update_params *params,
247 			 struct netlink_ext_ack *extack)
248 {
249 	struct ice_pf *pf = devlink_priv(devlink);
250 	struct device *dev = &pf->pdev->dev;
251 	struct ice_hw *hw = &pf->hw;
252 	const struct firmware *fw;
253 	u8 preservation;
254 	int err;
255 
256 	if (!params->overwrite_mask) {
257 		/* preserve all settings and identifiers */
258 		preservation = ICE_AQC_NVM_PRESERVE_ALL;
259 	} else if (params->overwrite_mask == DEVLINK_FLASH_OVERWRITE_SETTINGS) {
260 		/* overwrite settings, but preserve the vital device identifiers */
261 		preservation = ICE_AQC_NVM_PRESERVE_SELECTED;
262 	} else if (params->overwrite_mask == (DEVLINK_FLASH_OVERWRITE_SETTINGS |
263 					      DEVLINK_FLASH_OVERWRITE_IDENTIFIERS)) {
264 		/* overwrite both settings and identifiers, preserve nothing */
265 		preservation = ICE_AQC_NVM_NO_PRESERVATION;
266 	} else {
267 		NL_SET_ERR_MSG_MOD(extack, "Requested overwrite mask is not supported");
268 		return -EOPNOTSUPP;
269 	}
270 
271 	if (!hw->dev_caps.common_cap.nvm_unified_update) {
272 		NL_SET_ERR_MSG_MOD(extack, "Current firmware does not support unified update");
273 		return -EOPNOTSUPP;
274 	}
275 
276 	err = ice_check_for_pending_update(pf, NULL, extack);
277 	if (err)
278 		return err;
279 
280 	err = request_firmware(&fw, params->file_name, dev);
281 	if (err) {
282 		NL_SET_ERR_MSG_MOD(extack, "Unable to read file from disk");
283 		return err;
284 	}
285 
286 	dev_dbg(dev, "Beginning flash update with file '%s'\n", params->file_name);
287 
288 	devlink_flash_update_begin_notify(devlink);
289 	devlink_flash_update_status_notify(devlink, "Preparing to flash", NULL, 0, 0);
290 	err = ice_flash_pldm_image(pf, fw, preservation, extack);
291 	devlink_flash_update_end_notify(devlink);
292 
293 	release_firmware(fw);
294 
295 	return err;
296 }
297 
298 static const struct devlink_ops ice_devlink_ops = {
299 	.supported_flash_update_params = DEVLINK_SUPPORT_FLASH_UPDATE_OVERWRITE_MASK,
300 	.info_get = ice_devlink_info_get,
301 	.flash_update = ice_devlink_flash_update,
302 };
303 
ice_devlink_free(void * devlink_ptr)304 static void ice_devlink_free(void *devlink_ptr)
305 {
306 	devlink_free((struct devlink *)devlink_ptr);
307 }
308 
309 /**
310  * ice_allocate_pf - Allocate devlink and return PF structure pointer
311  * @dev: the device to allocate for
312  *
313  * Allocate a devlink instance for this device and return the private area as
314  * the PF structure. The devlink memory is kept track of through devres by
315  * adding an action to remove it when unwinding.
316  */
ice_allocate_pf(struct device * dev)317 struct ice_pf *ice_allocate_pf(struct device *dev)
318 {
319 	struct devlink *devlink;
320 
321 	devlink = devlink_alloc(&ice_devlink_ops, sizeof(struct ice_pf));
322 	if (!devlink)
323 		return NULL;
324 
325 	/* Add an action to teardown the devlink when unwinding the driver */
326 	if (devm_add_action(dev, ice_devlink_free, devlink)) {
327 		devlink_free(devlink);
328 		return NULL;
329 	}
330 
331 	return devlink_priv(devlink);
332 }
333 
334 /**
335  * ice_devlink_register - Register devlink interface for this PF
336  * @pf: the PF to register the devlink for.
337  *
338  * Register the devlink instance associated with this physical function.
339  *
340  * Return: zero on success or an error code on failure.
341  */
ice_devlink_register(struct ice_pf * pf)342 int ice_devlink_register(struct ice_pf *pf)
343 {
344 	struct devlink *devlink = priv_to_devlink(pf);
345 	struct device *dev = ice_pf_to_dev(pf);
346 	int err;
347 
348 	err = devlink_register(devlink, dev);
349 	if (err) {
350 		dev_err(dev, "devlink registration failed: %d\n", err);
351 		return err;
352 	}
353 
354 	return 0;
355 }
356 
357 /**
358  * ice_devlink_unregister - Unregister devlink resources for this PF.
359  * @pf: the PF structure to cleanup
360  *
361  * Releases resources used by devlink and cleans up associated memory.
362  */
ice_devlink_unregister(struct ice_pf * pf)363 void ice_devlink_unregister(struct ice_pf *pf)
364 {
365 	devlink_unregister(priv_to_devlink(pf));
366 }
367 
368 /**
369  * ice_devlink_create_port - Create a devlink port for this VSI
370  * @vsi: the VSI to create a port for
371  *
372  * Create and register a devlink_port for this VSI.
373  *
374  * Return: zero on success or an error code on failure.
375  */
ice_devlink_create_port(struct ice_vsi * vsi)376 int ice_devlink_create_port(struct ice_vsi *vsi)
377 {
378 	struct devlink_port_attrs attrs = {};
379 	struct ice_port_info *pi;
380 	struct devlink *devlink;
381 	struct device *dev;
382 	struct ice_pf *pf;
383 	int err;
384 
385 	/* Currently we only create devlink_port instances for PF VSIs */
386 	if (vsi->type != ICE_VSI_PF)
387 		return -EINVAL;
388 
389 	pf = vsi->back;
390 	devlink = priv_to_devlink(pf);
391 	dev = ice_pf_to_dev(pf);
392 	pi = pf->hw.port_info;
393 
394 	attrs.flavour = DEVLINK_PORT_FLAVOUR_PHYSICAL;
395 	attrs.phys.port_number = pi->lport;
396 	devlink_port_attrs_set(&vsi->devlink_port, &attrs);
397 	err = devlink_port_register(devlink, &vsi->devlink_port, vsi->idx);
398 	if (err) {
399 		dev_err(dev, "devlink_port_register failed: %d\n", err);
400 		return err;
401 	}
402 
403 	vsi->devlink_port_registered = true;
404 
405 	return 0;
406 }
407 
408 /**
409  * ice_devlink_destroy_port - Destroy the devlink_port for this VSI
410  * @vsi: the VSI to cleanup
411  *
412  * Unregisters the devlink_port structure associated with this VSI.
413  */
ice_devlink_destroy_port(struct ice_vsi * vsi)414 void ice_devlink_destroy_port(struct ice_vsi *vsi)
415 {
416 	if (!vsi->devlink_port_registered)
417 		return;
418 
419 	devlink_port_type_clear(&vsi->devlink_port);
420 	devlink_port_unregister(&vsi->devlink_port);
421 
422 	vsi->devlink_port_registered = false;
423 }
424 
425 /**
426  * ice_devlink_nvm_snapshot - Capture a snapshot of the Shadow RAM contents
427  * @devlink: the devlink instance
428  * @ops: the devlink region being snapshotted
429  * @extack: extended ACK response structure
430  * @data: on exit points to snapshot data buffer
431  *
432  * This function is called in response to the DEVLINK_CMD_REGION_TRIGGER for
433  * the shadow-ram devlink region. It captures a snapshot of the shadow ram
434  * contents. This snapshot can later be viewed via the devlink-region
435  * interface.
436  *
437  * @returns zero on success, and updates the data pointer. Returns a non-zero
438  * error code on failure.
439  */
ice_devlink_nvm_snapshot(struct devlink * devlink,const struct devlink_region_ops * ops,struct netlink_ext_ack * extack,u8 ** data)440 static int ice_devlink_nvm_snapshot(struct devlink *devlink,
441 				    const struct devlink_region_ops *ops,
442 				    struct netlink_ext_ack *extack, u8 **data)
443 {
444 	struct ice_pf *pf = devlink_priv(devlink);
445 	struct device *dev = ice_pf_to_dev(pf);
446 	struct ice_hw *hw = &pf->hw;
447 	enum ice_status status;
448 	void *nvm_data;
449 	u32 nvm_size;
450 
451 	nvm_size = hw->nvm.flash_size;
452 	nvm_data = vzalloc(nvm_size);
453 	if (!nvm_data)
454 		return -ENOMEM;
455 
456 	status = ice_acquire_nvm(hw, ICE_RES_READ);
457 	if (status) {
458 		dev_dbg(dev, "ice_acquire_nvm failed, err %d aq_err %d\n",
459 			status, hw->adminq.sq_last_status);
460 		NL_SET_ERR_MSG_MOD(extack, "Failed to acquire NVM semaphore");
461 		vfree(nvm_data);
462 		return -EIO;
463 	}
464 
465 	status = ice_read_flat_nvm(hw, 0, &nvm_size, nvm_data, false);
466 	if (status) {
467 		dev_dbg(dev, "ice_read_flat_nvm failed after reading %u bytes, err %d aq_err %d\n",
468 			nvm_size, status, hw->adminq.sq_last_status);
469 		NL_SET_ERR_MSG_MOD(extack, "Failed to read NVM contents");
470 		ice_release_nvm(hw);
471 		vfree(nvm_data);
472 		return -EIO;
473 	}
474 
475 	ice_release_nvm(hw);
476 
477 	*data = nvm_data;
478 
479 	return 0;
480 }
481 
482 /**
483  * ice_devlink_devcaps_snapshot - Capture snapshot of device capabilities
484  * @devlink: the devlink instance
485  * @ops: the devlink region being snapshotted
486  * @extack: extended ACK response structure
487  * @data: on exit points to snapshot data buffer
488  *
489  * This function is called in response to the DEVLINK_CMD_REGION_TRIGGER for
490  * the device-caps devlink region. It captures a snapshot of the device
491  * capabilities reported by firmware.
492  *
493  * @returns zero on success, and updates the data pointer. Returns a non-zero
494  * error code on failure.
495  */
496 static int
ice_devlink_devcaps_snapshot(struct devlink * devlink,const struct devlink_region_ops * ops,struct netlink_ext_ack * extack,u8 ** data)497 ice_devlink_devcaps_snapshot(struct devlink *devlink,
498 			     const struct devlink_region_ops *ops,
499 			     struct netlink_ext_ack *extack, u8 **data)
500 {
501 	struct ice_pf *pf = devlink_priv(devlink);
502 	struct device *dev = ice_pf_to_dev(pf);
503 	struct ice_hw *hw = &pf->hw;
504 	enum ice_status status;
505 	void *devcaps;
506 
507 	devcaps = vzalloc(ICE_AQ_MAX_BUF_LEN);
508 	if (!devcaps)
509 		return -ENOMEM;
510 
511 	status = ice_aq_list_caps(hw, devcaps, ICE_AQ_MAX_BUF_LEN, NULL,
512 				  ice_aqc_opc_list_dev_caps, NULL);
513 	if (status) {
514 		dev_dbg(dev, "ice_aq_list_caps: failed to read device capabilities, err %d aq_err %d\n",
515 			status, hw->adminq.sq_last_status);
516 		NL_SET_ERR_MSG_MOD(extack, "Failed to read device capabilities");
517 		vfree(devcaps);
518 		return -EIO;
519 	}
520 
521 	*data = (u8 *)devcaps;
522 
523 	return 0;
524 }
525 
526 static const struct devlink_region_ops ice_nvm_region_ops = {
527 	.name = "nvm-flash",
528 	.destructor = vfree,
529 	.snapshot = ice_devlink_nvm_snapshot,
530 };
531 
532 static const struct devlink_region_ops ice_devcaps_region_ops = {
533 	.name = "device-caps",
534 	.destructor = vfree,
535 	.snapshot = ice_devlink_devcaps_snapshot,
536 };
537 
538 /**
539  * ice_devlink_init_regions - Initialize devlink regions
540  * @pf: the PF device structure
541  *
542  * Create devlink regions used to enable access to dump the contents of the
543  * flash memory on the device.
544  */
ice_devlink_init_regions(struct ice_pf * pf)545 void ice_devlink_init_regions(struct ice_pf *pf)
546 {
547 	struct devlink *devlink = priv_to_devlink(pf);
548 	struct device *dev = ice_pf_to_dev(pf);
549 	u64 nvm_size;
550 
551 	nvm_size = pf->hw.nvm.flash_size;
552 	pf->nvm_region = devlink_region_create(devlink, &ice_nvm_region_ops, 1,
553 					       nvm_size);
554 	if (IS_ERR(pf->nvm_region)) {
555 		dev_err(dev, "failed to create NVM devlink region, err %ld\n",
556 			PTR_ERR(pf->nvm_region));
557 		pf->nvm_region = NULL;
558 	}
559 
560 	pf->devcaps_region = devlink_region_create(devlink,
561 						   &ice_devcaps_region_ops, 10,
562 						   ICE_AQ_MAX_BUF_LEN);
563 	if (IS_ERR(pf->devcaps_region)) {
564 		dev_err(dev, "failed to create device-caps devlink region, err %ld\n",
565 			PTR_ERR(pf->devcaps_region));
566 		pf->devcaps_region = NULL;
567 	}
568 }
569 
570 /**
571  * ice_devlink_destroy_regions - Destroy devlink regions
572  * @pf: the PF device structure
573  *
574  * Remove previously created regions for this PF.
575  */
ice_devlink_destroy_regions(struct ice_pf * pf)576 void ice_devlink_destroy_regions(struct ice_pf *pf)
577 {
578 	if (pf->nvm_region)
579 		devlink_region_destroy(pf->nvm_region);
580 	if (pf->devcaps_region)
581 		devlink_region_destroy(pf->devcaps_region);
582 }
583