xref: /openbmc/linux/drivers/firmware/stratix10-rsu.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
14526ebbcSRichard Gong // SPDX-License-Identifier: GPL-2.0
24526ebbcSRichard Gong /*
34526ebbcSRichard Gong  * Copyright (C) 2018-2019, Intel Corporation
44526ebbcSRichard Gong  */
54526ebbcSRichard Gong 
64526ebbcSRichard Gong #include <linux/arm-smccc.h>
74526ebbcSRichard Gong #include <linux/bitfield.h>
84526ebbcSRichard Gong #include <linux/completion.h>
94526ebbcSRichard Gong #include <linux/kobject.h>
104526ebbcSRichard Gong #include <linux/module.h>
114526ebbcSRichard Gong #include <linux/mutex.h>
124526ebbcSRichard Gong #include <linux/of.h>
134526ebbcSRichard Gong #include <linux/platform_device.h>
144526ebbcSRichard Gong #include <linux/firmware/intel/stratix10-svc-client.h>
154526ebbcSRichard Gong #include <linux/string.h>
164526ebbcSRichard Gong #include <linux/sysfs.h>
174526ebbcSRichard Gong 
184526ebbcSRichard Gong #define RSU_STATE_MASK			GENMASK_ULL(31, 0)
194526ebbcSRichard Gong #define RSU_VERSION_MASK		GENMASK_ULL(63, 32)
204526ebbcSRichard Gong #define RSU_ERROR_LOCATION_MASK		GENMASK_ULL(31, 0)
214526ebbcSRichard Gong #define RSU_ERROR_DETAIL_MASK		GENMASK_ULL(63, 32)
2275bc73fcSRichard Gong #define RSU_DCMF0_MASK			GENMASK_ULL(31, 0)
2375bc73fcSRichard Gong #define RSU_DCMF1_MASK			GENMASK_ULL(63, 32)
2475bc73fcSRichard Gong #define RSU_DCMF2_MASK			GENMASK_ULL(31, 0)
2575bc73fcSRichard Gong #define RSU_DCMF3_MASK			GENMASK_ULL(63, 32)
264a6c8c56SKah Jing Lee #define RSU_DCMF0_STATUS_MASK		GENMASK_ULL(15, 0)
274a6c8c56SKah Jing Lee #define RSU_DCMF1_STATUS_MASK		GENMASK_ULL(31, 16)
284a6c8c56SKah Jing Lee #define RSU_DCMF2_STATUS_MASK		GENMASK_ULL(47, 32)
294a6c8c56SKah Jing Lee #define RSU_DCMF3_STATUS_MASK		GENMASK_ULL(63, 48)
304526ebbcSRichard Gong 
314526ebbcSRichard Gong #define RSU_TIMEOUT	(msecs_to_jiffies(SVC_RSU_REQUEST_TIMEOUT_MS))
324526ebbcSRichard Gong 
3375bc73fcSRichard Gong #define INVALID_RETRY_COUNTER		0xFF
3475bc73fcSRichard Gong #define INVALID_DCMF_VERSION		0xFF
354a6c8c56SKah Jing Lee #define INVALID_DCMF_STATUS		0xFFFFFFFF
36*abe8ff43SRadu Bacrau #define INVALID_SPT_ADDRESS		0x0
37*abe8ff43SRadu Bacrau 
38*abe8ff43SRadu Bacrau #define RSU_GET_SPT_CMD			0x5A
39*abe8ff43SRadu Bacrau #define RSU_GET_SPT_RESP_LEN		(4 * sizeof(unsigned int))
404526ebbcSRichard Gong 
414526ebbcSRichard Gong typedef void (*rsu_callback)(struct stratix10_svc_client *client,
424526ebbcSRichard Gong 			     struct stratix10_svc_cb_data *data);
434526ebbcSRichard Gong /**
444526ebbcSRichard Gong  * struct stratix10_rsu_priv - rsu data structure
454526ebbcSRichard Gong  * @chan: pointer to the allocated service channel
464526ebbcSRichard Gong  * @client: active service client
474526ebbcSRichard Gong  * @completion: state for callback completion
484526ebbcSRichard Gong  * @lock: a mutex to protect callback completion state
494526ebbcSRichard Gong  * @status.current_image: address of image currently running in flash
504526ebbcSRichard Gong  * @status.fail_image: address of failed image in flash
5175bc73fcSRichard Gong  * @status.version: the interface version number of RSU firmware
524526ebbcSRichard Gong  * @status.state: the state of RSU system
534526ebbcSRichard Gong  * @status.error_details: error code
544526ebbcSRichard Gong  * @status.error_location: the error offset inside the image that failed
5575bc73fcSRichard Gong  * @dcmf_version.dcmf0: Quartus dcmf0 version
5675bc73fcSRichard Gong  * @dcmf_version.dcmf1: Quartus dcmf1 version
5775bc73fcSRichard Gong  * @dcmf_version.dcmf2: Quartus dcmf2 version
5875bc73fcSRichard Gong  * @dcmf_version.dcmf3: Quartus dcmf3 version
594a6c8c56SKah Jing Lee  * @dcmf_status.dcmf0: dcmf0 status
604a6c8c56SKah Jing Lee  * @dcmf_status.dcmf1: dcmf1 status
614a6c8c56SKah Jing Lee  * @dcmf_status.dcmf2: dcmf2 status
624a6c8c56SKah Jing Lee  * @dcmf_status.dcmf3: dcmf3 status
634526ebbcSRichard Gong  * @retry_counter: the current image's retry counter
6475bc73fcSRichard Gong  * @max_retry: the preset max retry value
65*abe8ff43SRadu Bacrau  * @spt0_address: address of spt0
66*abe8ff43SRadu Bacrau  * @spt1_address: address of spt1
67*abe8ff43SRadu Bacrau  * @get_spt_response_buf: response from sdm for get_spt command
684526ebbcSRichard Gong  */
694526ebbcSRichard Gong struct stratix10_rsu_priv {
704526ebbcSRichard Gong 	struct stratix10_svc_chan *chan;
714526ebbcSRichard Gong 	struct stratix10_svc_client client;
724526ebbcSRichard Gong 	struct completion completion;
734526ebbcSRichard Gong 	struct mutex lock;
744526ebbcSRichard Gong 	struct {
754526ebbcSRichard Gong 		unsigned long current_image;
764526ebbcSRichard Gong 		unsigned long fail_image;
774526ebbcSRichard Gong 		unsigned int version;
784526ebbcSRichard Gong 		unsigned int state;
794526ebbcSRichard Gong 		unsigned int error_details;
804526ebbcSRichard Gong 		unsigned int error_location;
814526ebbcSRichard Gong 	} status;
8275bc73fcSRichard Gong 
8375bc73fcSRichard Gong 	struct {
8475bc73fcSRichard Gong 		unsigned int dcmf0;
8575bc73fcSRichard Gong 		unsigned int dcmf1;
8675bc73fcSRichard Gong 		unsigned int dcmf2;
8775bc73fcSRichard Gong 		unsigned int dcmf3;
8875bc73fcSRichard Gong 	} dcmf_version;
8975bc73fcSRichard Gong 
904a6c8c56SKah Jing Lee 	struct {
914a6c8c56SKah Jing Lee 		unsigned int dcmf0;
924a6c8c56SKah Jing Lee 		unsigned int dcmf1;
934a6c8c56SKah Jing Lee 		unsigned int dcmf2;
944a6c8c56SKah Jing Lee 		unsigned int dcmf3;
954a6c8c56SKah Jing Lee 	} dcmf_status;
964a6c8c56SKah Jing Lee 
974526ebbcSRichard Gong 	unsigned int retry_counter;
9875bc73fcSRichard Gong 	unsigned int max_retry;
99*abe8ff43SRadu Bacrau 
100*abe8ff43SRadu Bacrau 	unsigned long spt0_address;
101*abe8ff43SRadu Bacrau 	unsigned long spt1_address;
102*abe8ff43SRadu Bacrau 
103*abe8ff43SRadu Bacrau 	unsigned int *get_spt_response_buf;
1044526ebbcSRichard Gong };
1054526ebbcSRichard Gong 
1064526ebbcSRichard Gong /**
1074526ebbcSRichard Gong  * rsu_status_callback() - Status callback from Intel Service Layer
1084526ebbcSRichard Gong  * @client: pointer to service client
1094526ebbcSRichard Gong  * @data: pointer to callback data structure
1104526ebbcSRichard Gong  *
1114526ebbcSRichard Gong  * Callback from Intel service layer for RSU status request. Status is
1124526ebbcSRichard Gong  * only updated after a system reboot, so a get updated status call is
1134526ebbcSRichard Gong  * made during driver probe.
1144526ebbcSRichard Gong  */
rsu_status_callback(struct stratix10_svc_client * client,struct stratix10_svc_cb_data * data)1154526ebbcSRichard Gong static void rsu_status_callback(struct stratix10_svc_client *client,
1164526ebbcSRichard Gong 				struct stratix10_svc_cb_data *data)
1174526ebbcSRichard Gong {
1184526ebbcSRichard Gong 	struct stratix10_rsu_priv *priv = client->priv;
1194526ebbcSRichard Gong 	struct arm_smccc_res *res = (struct arm_smccc_res *)data->kaddr1;
1204526ebbcSRichard Gong 
1217536ad8dSRichard Gong 	if (data->status == BIT(SVC_STATUS_OK)) {
1224526ebbcSRichard Gong 		priv->status.version = FIELD_GET(RSU_VERSION_MASK,
1234526ebbcSRichard Gong 						 res->a2);
1244526ebbcSRichard Gong 		priv->status.state = FIELD_GET(RSU_STATE_MASK, res->a2);
1254526ebbcSRichard Gong 		priv->status.fail_image = res->a1;
1264526ebbcSRichard Gong 		priv->status.current_image = res->a0;
1274526ebbcSRichard Gong 		priv->status.error_location =
1284526ebbcSRichard Gong 			FIELD_GET(RSU_ERROR_LOCATION_MASK, res->a3);
1294526ebbcSRichard Gong 		priv->status.error_details =
1304526ebbcSRichard Gong 			FIELD_GET(RSU_ERROR_DETAIL_MASK, res->a3);
1314526ebbcSRichard Gong 	} else {
1324526ebbcSRichard Gong 		dev_err(client->dev, "COMMAND_RSU_STATUS returned 0x%lX\n",
1334526ebbcSRichard Gong 			res->a0);
1344526ebbcSRichard Gong 		priv->status.version = 0;
1354526ebbcSRichard Gong 		priv->status.state = 0;
1364526ebbcSRichard Gong 		priv->status.fail_image = 0;
1374526ebbcSRichard Gong 		priv->status.current_image = 0;
1384526ebbcSRichard Gong 		priv->status.error_location = 0;
1394526ebbcSRichard Gong 		priv->status.error_details = 0;
1404526ebbcSRichard Gong 	}
1414526ebbcSRichard Gong 
1424526ebbcSRichard Gong 	complete(&priv->completion);
1434526ebbcSRichard Gong }
1444526ebbcSRichard Gong 
1454526ebbcSRichard Gong /**
1464526ebbcSRichard Gong  * rsu_command_callback() - Update callback from Intel Service Layer
1474526ebbcSRichard Gong  * @client: pointer to client
1484526ebbcSRichard Gong  * @data: pointer to callback data structure
1494526ebbcSRichard Gong  *
1504526ebbcSRichard Gong  * Callback from Intel service layer for RSU commands.
1514526ebbcSRichard Gong  */
rsu_command_callback(struct stratix10_svc_client * client,struct stratix10_svc_cb_data * data)1524526ebbcSRichard Gong static void rsu_command_callback(struct stratix10_svc_client *client,
1534526ebbcSRichard Gong 				 struct stratix10_svc_cb_data *data)
1544526ebbcSRichard Gong {
1554526ebbcSRichard Gong 	struct stratix10_rsu_priv *priv = client->priv;
1564526ebbcSRichard Gong 
1577536ad8dSRichard Gong 	if (data->status == BIT(SVC_STATUS_NO_SUPPORT))
1584a6c8c56SKah Jing Lee 		dev_warn(client->dev, "Secure FW doesn't support notify\n");
1597536ad8dSRichard Gong 	else if (data->status == BIT(SVC_STATUS_ERROR))
160e9cb0497SRichard Gong 		dev_err(client->dev, "Failure, returned status is %lu\n",
161e9cb0497SRichard Gong 			BIT(data->status));
162e9cb0497SRichard Gong 
1634526ebbcSRichard Gong 	complete(&priv->completion);
1644526ebbcSRichard Gong }
1654526ebbcSRichard Gong 
1664526ebbcSRichard Gong /**
1674526ebbcSRichard Gong  * rsu_retry_callback() - Callback from Intel service layer for getting
16875bc73fcSRichard Gong  * the current image's retry counter from the firmware
1694526ebbcSRichard Gong  * @client: pointer to client
1704526ebbcSRichard Gong  * @data: pointer to callback data structure
1714526ebbcSRichard Gong  *
1724526ebbcSRichard Gong  * Callback from Intel service layer for retry counter, which is used by
1734526ebbcSRichard Gong  * user to know how many times the images is still allowed to reload
1744526ebbcSRichard Gong  * itself before giving up and starting RSU fail-over flow.
1754526ebbcSRichard Gong  */
rsu_retry_callback(struct stratix10_svc_client * client,struct stratix10_svc_cb_data * data)1764526ebbcSRichard Gong static void rsu_retry_callback(struct stratix10_svc_client *client,
1774526ebbcSRichard Gong 			       struct stratix10_svc_cb_data *data)
1784526ebbcSRichard Gong {
1794526ebbcSRichard Gong 	struct stratix10_rsu_priv *priv = client->priv;
1804526ebbcSRichard Gong 	unsigned int *counter = (unsigned int *)data->kaddr1;
1814526ebbcSRichard Gong 
1827536ad8dSRichard Gong 	if (data->status == BIT(SVC_STATUS_OK))
1834526ebbcSRichard Gong 		priv->retry_counter = *counter;
1847536ad8dSRichard Gong 	else if (data->status == BIT(SVC_STATUS_NO_SUPPORT))
1854a6c8c56SKah Jing Lee 		dev_warn(client->dev, "Secure FW doesn't support retry\n");
1864526ebbcSRichard Gong 	else
187e9cb0497SRichard Gong 		dev_err(client->dev, "Failed to get retry counter %lu\n",
188e9cb0497SRichard Gong 			BIT(data->status));
1894526ebbcSRichard Gong 
1904526ebbcSRichard Gong 	complete(&priv->completion);
1914526ebbcSRichard Gong }
1924526ebbcSRichard Gong 
1934526ebbcSRichard Gong /**
19475bc73fcSRichard Gong  * rsu_max_retry_callback() - Callback from Intel service layer for getting
19575bc73fcSRichard Gong  * the max retry value from the firmware
19675bc73fcSRichard Gong  * @client: pointer to client
19775bc73fcSRichard Gong  * @data: pointer to callback data structure
19875bc73fcSRichard Gong  *
19975bc73fcSRichard Gong  * Callback from Intel service layer for max retry.
20075bc73fcSRichard Gong  */
rsu_max_retry_callback(struct stratix10_svc_client * client,struct stratix10_svc_cb_data * data)20175bc73fcSRichard Gong static void rsu_max_retry_callback(struct stratix10_svc_client *client,
20275bc73fcSRichard Gong 				   struct stratix10_svc_cb_data *data)
20375bc73fcSRichard Gong {
20475bc73fcSRichard Gong 	struct stratix10_rsu_priv *priv = client->priv;
20575bc73fcSRichard Gong 	unsigned int *max_retry = (unsigned int *)data->kaddr1;
20675bc73fcSRichard Gong 
20775bc73fcSRichard Gong 	if (data->status == BIT(SVC_STATUS_OK))
20875bc73fcSRichard Gong 		priv->max_retry = *max_retry;
20975bc73fcSRichard Gong 	else if (data->status == BIT(SVC_STATUS_NO_SUPPORT))
2104a6c8c56SKah Jing Lee 		dev_warn(client->dev, "Secure FW doesn't support max retry\n");
21175bc73fcSRichard Gong 	else
21275bc73fcSRichard Gong 		dev_err(client->dev, "Failed to get max retry %lu\n",
21375bc73fcSRichard Gong 			BIT(data->status));
21475bc73fcSRichard Gong 
21575bc73fcSRichard Gong 	complete(&priv->completion);
21675bc73fcSRichard Gong }
21775bc73fcSRichard Gong 
21875bc73fcSRichard Gong /**
21975bc73fcSRichard Gong  * rsu_dcmf_version_callback() - Callback from Intel service layer for getting
22075bc73fcSRichard Gong  * the DCMF version
22175bc73fcSRichard Gong  * @client: pointer to client
22275bc73fcSRichard Gong  * @data: pointer to callback data structure
22375bc73fcSRichard Gong  *
22475bc73fcSRichard Gong  * Callback from Intel service layer for DCMF version number
22575bc73fcSRichard Gong  */
rsu_dcmf_version_callback(struct stratix10_svc_client * client,struct stratix10_svc_cb_data * data)22675bc73fcSRichard Gong static void rsu_dcmf_version_callback(struct stratix10_svc_client *client,
22775bc73fcSRichard Gong 				      struct stratix10_svc_cb_data *data)
22875bc73fcSRichard Gong {
22975bc73fcSRichard Gong 	struct stratix10_rsu_priv *priv = client->priv;
23075bc73fcSRichard Gong 	unsigned long long *value1 = (unsigned long long *)data->kaddr1;
23175bc73fcSRichard Gong 	unsigned long long *value2 = (unsigned long long *)data->kaddr2;
23275bc73fcSRichard Gong 
23375bc73fcSRichard Gong 	if (data->status == BIT(SVC_STATUS_OK)) {
23475bc73fcSRichard Gong 		priv->dcmf_version.dcmf0 = FIELD_GET(RSU_DCMF0_MASK, *value1);
23575bc73fcSRichard Gong 		priv->dcmf_version.dcmf1 = FIELD_GET(RSU_DCMF1_MASK, *value1);
23675bc73fcSRichard Gong 		priv->dcmf_version.dcmf2 = FIELD_GET(RSU_DCMF2_MASK, *value2);
23775bc73fcSRichard Gong 		priv->dcmf_version.dcmf3 = FIELD_GET(RSU_DCMF3_MASK, *value2);
23875bc73fcSRichard Gong 	} else
23975bc73fcSRichard Gong 		dev_err(client->dev, "failed to get DCMF version\n");
24075bc73fcSRichard Gong 
24175bc73fcSRichard Gong 	complete(&priv->completion);
24275bc73fcSRichard Gong }
24375bc73fcSRichard Gong 
24475bc73fcSRichard Gong /**
2454a6c8c56SKah Jing Lee  * rsu_dcmf_status_callback() - Callback from Intel service layer for getting
2464a6c8c56SKah Jing Lee  * the DCMF status
2474a6c8c56SKah Jing Lee  * @client: pointer to client
2484a6c8c56SKah Jing Lee  * @data: pointer to callback data structure
2494a6c8c56SKah Jing Lee  *
2504a6c8c56SKah Jing Lee  * Callback from Intel service layer for DCMF status
2514a6c8c56SKah Jing Lee  */
rsu_dcmf_status_callback(struct stratix10_svc_client * client,struct stratix10_svc_cb_data * data)2524a6c8c56SKah Jing Lee static void rsu_dcmf_status_callback(struct stratix10_svc_client *client,
2534a6c8c56SKah Jing Lee 				     struct stratix10_svc_cb_data *data)
2544a6c8c56SKah Jing Lee {
2554a6c8c56SKah Jing Lee 	struct stratix10_rsu_priv *priv = client->priv;
2564a6c8c56SKah Jing Lee 	unsigned long long *value = (unsigned long long *)data->kaddr1;
2574a6c8c56SKah Jing Lee 
2584a6c8c56SKah Jing Lee 	if (data->status == BIT(SVC_STATUS_OK)) {
2594a6c8c56SKah Jing Lee 		priv->dcmf_status.dcmf0 = FIELD_GET(RSU_DCMF0_STATUS_MASK,
2604a6c8c56SKah Jing Lee 						    *value);
2614a6c8c56SKah Jing Lee 		priv->dcmf_status.dcmf1 = FIELD_GET(RSU_DCMF1_STATUS_MASK,
2624a6c8c56SKah Jing Lee 						    *value);
2634a6c8c56SKah Jing Lee 		priv->dcmf_status.dcmf2 = FIELD_GET(RSU_DCMF2_STATUS_MASK,
2644a6c8c56SKah Jing Lee 						    *value);
2654a6c8c56SKah Jing Lee 		priv->dcmf_status.dcmf3 = FIELD_GET(RSU_DCMF3_STATUS_MASK,
2664a6c8c56SKah Jing Lee 						    *value);
2674a6c8c56SKah Jing Lee 	} else
2684a6c8c56SKah Jing Lee 		dev_err(client->dev, "failed to get DCMF status\n");
2694a6c8c56SKah Jing Lee 
2704a6c8c56SKah Jing Lee 	complete(&priv->completion);
2714a6c8c56SKah Jing Lee }
2724a6c8c56SKah Jing Lee 
rsu_get_spt_callback(struct stratix10_svc_client * client,struct stratix10_svc_cb_data * data)273*abe8ff43SRadu Bacrau static void rsu_get_spt_callback(struct stratix10_svc_client *client,
274*abe8ff43SRadu Bacrau 				 struct stratix10_svc_cb_data *data)
275*abe8ff43SRadu Bacrau {
276*abe8ff43SRadu Bacrau 	struct stratix10_rsu_priv *priv = client->priv;
277*abe8ff43SRadu Bacrau 	unsigned long *mbox_err = (unsigned long *)data->kaddr1;
278*abe8ff43SRadu Bacrau 	unsigned long *resp_len = (unsigned long *)data->kaddr2;
279*abe8ff43SRadu Bacrau 
280*abe8ff43SRadu Bacrau 	if (data->status != BIT(SVC_STATUS_OK) || (*mbox_err) ||
281*abe8ff43SRadu Bacrau 	    (*resp_len != RSU_GET_SPT_RESP_LEN))
282*abe8ff43SRadu Bacrau 		goto error;
283*abe8ff43SRadu Bacrau 
284*abe8ff43SRadu Bacrau 	priv->spt0_address = priv->get_spt_response_buf[0];
285*abe8ff43SRadu Bacrau 	priv->spt0_address <<= 32;
286*abe8ff43SRadu Bacrau 	priv->spt0_address |= priv->get_spt_response_buf[1];
287*abe8ff43SRadu Bacrau 
288*abe8ff43SRadu Bacrau 	priv->spt1_address = priv->get_spt_response_buf[2];
289*abe8ff43SRadu Bacrau 	priv->spt1_address <<= 32;
290*abe8ff43SRadu Bacrau 	priv->spt1_address |= priv->get_spt_response_buf[3];
291*abe8ff43SRadu Bacrau 
292*abe8ff43SRadu Bacrau 	goto complete;
293*abe8ff43SRadu Bacrau 
294*abe8ff43SRadu Bacrau error:
295*abe8ff43SRadu Bacrau 	dev_err(client->dev, "failed to get SPTs\n");
296*abe8ff43SRadu Bacrau 
297*abe8ff43SRadu Bacrau complete:
298*abe8ff43SRadu Bacrau 	stratix10_svc_free_memory(priv->chan, priv->get_spt_response_buf);
299*abe8ff43SRadu Bacrau 	priv->get_spt_response_buf = NULL;
300*abe8ff43SRadu Bacrau 	complete(&priv->completion);
301*abe8ff43SRadu Bacrau }
302*abe8ff43SRadu Bacrau 
3034a6c8c56SKah Jing Lee /**
3044526ebbcSRichard Gong  * rsu_send_msg() - send a message to Intel service layer
3054526ebbcSRichard Gong  * @priv: pointer to rsu private data
3064526ebbcSRichard Gong  * @command: RSU status or update command
3074526ebbcSRichard Gong  * @arg: the request argument, the bitstream address or notify status
3084526ebbcSRichard Gong  * @callback: function pointer for the callback (status or update)
3094526ebbcSRichard Gong  *
3104526ebbcSRichard Gong  * Start an Intel service layer transaction to perform the SMC call that
3114526ebbcSRichard Gong  * is necessary to get RSU boot log or set the address of bitstream to
3124526ebbcSRichard Gong  * boot after reboot.
3134526ebbcSRichard Gong  *
3144526ebbcSRichard Gong  * Returns 0 on success or -ETIMEDOUT on error.
3154526ebbcSRichard Gong  */
rsu_send_msg(struct stratix10_rsu_priv * priv,enum stratix10_svc_command_code command,unsigned long arg,rsu_callback callback)3164526ebbcSRichard Gong static int rsu_send_msg(struct stratix10_rsu_priv *priv,
3174526ebbcSRichard Gong 			enum stratix10_svc_command_code command,
3184526ebbcSRichard Gong 			unsigned long arg,
3194526ebbcSRichard Gong 			rsu_callback callback)
3204526ebbcSRichard Gong {
3214526ebbcSRichard Gong 	struct stratix10_svc_client_msg msg;
3224526ebbcSRichard Gong 	int ret;
3234526ebbcSRichard Gong 
3244526ebbcSRichard Gong 	mutex_lock(&priv->lock);
3254526ebbcSRichard Gong 	reinit_completion(&priv->completion);
3264526ebbcSRichard Gong 	priv->client.receive_cb = callback;
3274526ebbcSRichard Gong 
3284526ebbcSRichard Gong 	msg.command = command;
3294526ebbcSRichard Gong 	if (arg)
3304526ebbcSRichard Gong 		msg.arg[0] = arg;
3314526ebbcSRichard Gong 
332*abe8ff43SRadu Bacrau 	if (command == COMMAND_MBOX_SEND_CMD) {
333*abe8ff43SRadu Bacrau 		msg.arg[1] = 0;
334*abe8ff43SRadu Bacrau 		msg.payload = NULL;
335*abe8ff43SRadu Bacrau 		msg.payload_length = 0;
336*abe8ff43SRadu Bacrau 		msg.payload_output = priv->get_spt_response_buf;
337*abe8ff43SRadu Bacrau 		msg.payload_length_output = RSU_GET_SPT_RESP_LEN;
338*abe8ff43SRadu Bacrau 	}
339*abe8ff43SRadu Bacrau 
3404526ebbcSRichard Gong 	ret = stratix10_svc_send(priv->chan, &msg);
3414526ebbcSRichard Gong 	if (ret < 0)
3424526ebbcSRichard Gong 		goto status_done;
3434526ebbcSRichard Gong 
3444526ebbcSRichard Gong 	ret = wait_for_completion_interruptible_timeout(&priv->completion,
3454526ebbcSRichard Gong 							RSU_TIMEOUT);
3464526ebbcSRichard Gong 	if (!ret) {
3474526ebbcSRichard Gong 		dev_err(priv->client.dev,
3484526ebbcSRichard Gong 			"timeout waiting for SMC call\n");
3494526ebbcSRichard Gong 		ret = -ETIMEDOUT;
3504526ebbcSRichard Gong 		goto status_done;
3514526ebbcSRichard Gong 	} else if (ret < 0) {
3524526ebbcSRichard Gong 		dev_err(priv->client.dev,
3534526ebbcSRichard Gong 			"error %d waiting for SMC call\n", ret);
3544526ebbcSRichard Gong 		goto status_done;
3554526ebbcSRichard Gong 	} else {
3564526ebbcSRichard Gong 		ret = 0;
3574526ebbcSRichard Gong 	}
3584526ebbcSRichard Gong 
3594526ebbcSRichard Gong status_done:
3604526ebbcSRichard Gong 	stratix10_svc_done(priv->chan);
3614526ebbcSRichard Gong 	mutex_unlock(&priv->lock);
3624526ebbcSRichard Gong 	return ret;
3634526ebbcSRichard Gong }
3644526ebbcSRichard Gong 
3654526ebbcSRichard Gong /*
3664526ebbcSRichard Gong  * This driver exposes some optional features of the Intel Stratix 10 SoC FPGA.
3674526ebbcSRichard Gong  * The sysfs interfaces exposed here are FPGA Remote System Update (RSU)
3684526ebbcSRichard Gong  * related. They allow user space software to query the configuration system
3694526ebbcSRichard Gong  * status and to request optional reboot behavior specific to Intel FPGAs.
3704526ebbcSRichard Gong  */
3714526ebbcSRichard Gong 
current_image_show(struct device * dev,struct device_attribute * attr,char * buf)3724526ebbcSRichard Gong static ssize_t current_image_show(struct device *dev,
3734526ebbcSRichard Gong 				  struct device_attribute *attr, char *buf)
3744526ebbcSRichard Gong {
3754526ebbcSRichard Gong 	struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
3764526ebbcSRichard Gong 
3774526ebbcSRichard Gong 	if (!priv)
3784526ebbcSRichard Gong 		return -ENODEV;
3794526ebbcSRichard Gong 
3804526ebbcSRichard Gong 	return sprintf(buf, "0x%08lx\n", priv->status.current_image);
3814526ebbcSRichard Gong }
3824526ebbcSRichard Gong 
fail_image_show(struct device * dev,struct device_attribute * attr,char * buf)3834526ebbcSRichard Gong static ssize_t fail_image_show(struct device *dev,
3844526ebbcSRichard Gong 			       struct device_attribute *attr, char *buf)
3854526ebbcSRichard Gong {
3864526ebbcSRichard Gong 	struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
3874526ebbcSRichard Gong 
3884526ebbcSRichard Gong 	if (!priv)
3894526ebbcSRichard Gong 		return -ENODEV;
3904526ebbcSRichard Gong 
3914526ebbcSRichard Gong 	return sprintf(buf, "0x%08lx\n", priv->status.fail_image);
3924526ebbcSRichard Gong }
3934526ebbcSRichard Gong 
version_show(struct device * dev,struct device_attribute * attr,char * buf)3944526ebbcSRichard Gong static ssize_t version_show(struct device *dev, struct device_attribute *attr,
3954526ebbcSRichard Gong 			    char *buf)
3964526ebbcSRichard Gong {
3974526ebbcSRichard Gong 	struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
3984526ebbcSRichard Gong 
3994526ebbcSRichard Gong 	if (!priv)
4004526ebbcSRichard Gong 		return -ENODEV;
4014526ebbcSRichard Gong 
4024526ebbcSRichard Gong 	return sprintf(buf, "0x%08x\n", priv->status.version);
4034526ebbcSRichard Gong }
4044526ebbcSRichard Gong 
state_show(struct device * dev,struct device_attribute * attr,char * buf)4054526ebbcSRichard Gong static ssize_t state_show(struct device *dev, struct device_attribute *attr,
4064526ebbcSRichard Gong 			  char *buf)
4074526ebbcSRichard Gong {
4084526ebbcSRichard Gong 	struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
4094526ebbcSRichard Gong 
4104526ebbcSRichard Gong 	if (!priv)
4114526ebbcSRichard Gong 		return -ENODEV;
4124526ebbcSRichard Gong 
4134526ebbcSRichard Gong 	return sprintf(buf, "0x%08x\n", priv->status.state);
4144526ebbcSRichard Gong }
4154526ebbcSRichard Gong 
error_location_show(struct device * dev,struct device_attribute * attr,char * buf)4164526ebbcSRichard Gong static ssize_t error_location_show(struct device *dev,
4174526ebbcSRichard Gong 				   struct device_attribute *attr, char *buf)
4184526ebbcSRichard Gong {
4194526ebbcSRichard Gong 	struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
4204526ebbcSRichard Gong 
4214526ebbcSRichard Gong 	if (!priv)
4224526ebbcSRichard Gong 		return -ENODEV;
4234526ebbcSRichard Gong 
4244526ebbcSRichard Gong 	return sprintf(buf, "0x%08x\n", priv->status.error_location);
4254526ebbcSRichard Gong }
4264526ebbcSRichard Gong 
error_details_show(struct device * dev,struct device_attribute * attr,char * buf)4274526ebbcSRichard Gong static ssize_t error_details_show(struct device *dev,
4284526ebbcSRichard Gong 				  struct device_attribute *attr, char *buf)
4294526ebbcSRichard Gong {
4304526ebbcSRichard Gong 	struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
4314526ebbcSRichard Gong 
4324526ebbcSRichard Gong 	if (!priv)
4334526ebbcSRichard Gong 		return -ENODEV;
4344526ebbcSRichard Gong 
4354526ebbcSRichard Gong 	return sprintf(buf, "0x%08x\n", priv->status.error_details);
4364526ebbcSRichard Gong }
4374526ebbcSRichard Gong 
retry_counter_show(struct device * dev,struct device_attribute * attr,char * buf)4384526ebbcSRichard Gong static ssize_t retry_counter_show(struct device *dev,
4394526ebbcSRichard Gong 				  struct device_attribute *attr, char *buf)
4404526ebbcSRichard Gong {
4414526ebbcSRichard Gong 	struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
4424526ebbcSRichard Gong 
4434526ebbcSRichard Gong 	if (!priv)
4444526ebbcSRichard Gong 		return -ENODEV;
4454526ebbcSRichard Gong 
4464526ebbcSRichard Gong 	return sprintf(buf, "0x%08x\n", priv->retry_counter);
4474526ebbcSRichard Gong }
4484526ebbcSRichard Gong 
max_retry_show(struct device * dev,struct device_attribute * attr,char * buf)44975bc73fcSRichard Gong static ssize_t max_retry_show(struct device *dev,
45075bc73fcSRichard Gong 			      struct device_attribute *attr, char *buf)
45175bc73fcSRichard Gong {
45275bc73fcSRichard Gong 	struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
45375bc73fcSRichard Gong 
45475bc73fcSRichard Gong 	if (!priv)
45575bc73fcSRichard Gong 		return -ENODEV;
45675bc73fcSRichard Gong 
4574a6c8c56SKah Jing Lee 	return scnprintf(buf, sizeof(priv->max_retry),
4584a6c8c56SKah Jing Lee 			 "0x%08x\n", priv->max_retry);
45975bc73fcSRichard Gong }
46075bc73fcSRichard Gong 
dcmf0_show(struct device * dev,struct device_attribute * attr,char * buf)46175bc73fcSRichard Gong static ssize_t dcmf0_show(struct device *dev,
46275bc73fcSRichard Gong 			  struct device_attribute *attr, char *buf)
46375bc73fcSRichard Gong {
46475bc73fcSRichard Gong 	struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
46575bc73fcSRichard Gong 
46675bc73fcSRichard Gong 	if (!priv)
46775bc73fcSRichard Gong 		return -ENODEV;
46875bc73fcSRichard Gong 
46975bc73fcSRichard Gong 	return sprintf(buf, "0x%08x\n", priv->dcmf_version.dcmf0);
47075bc73fcSRichard Gong }
47175bc73fcSRichard Gong 
dcmf1_show(struct device * dev,struct device_attribute * attr,char * buf)47275bc73fcSRichard Gong static ssize_t dcmf1_show(struct device *dev,
47375bc73fcSRichard Gong 			  struct device_attribute *attr, char *buf)
47475bc73fcSRichard Gong {
47575bc73fcSRichard Gong 	struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
47675bc73fcSRichard Gong 
47775bc73fcSRichard Gong 	if (!priv)
47875bc73fcSRichard Gong 		return -ENODEV;
47975bc73fcSRichard Gong 
48075bc73fcSRichard Gong 	return sprintf(buf, "0x%08x\n", priv->dcmf_version.dcmf1);
48175bc73fcSRichard Gong }
48275bc73fcSRichard Gong 
dcmf2_show(struct device * dev,struct device_attribute * attr,char * buf)48375bc73fcSRichard Gong static ssize_t dcmf2_show(struct device *dev,
48475bc73fcSRichard Gong 			  struct device_attribute *attr, char *buf)
48575bc73fcSRichard Gong {
48675bc73fcSRichard Gong 	struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
48775bc73fcSRichard Gong 
48875bc73fcSRichard Gong 	if (!priv)
48975bc73fcSRichard Gong 		return -ENODEV;
49075bc73fcSRichard Gong 
49175bc73fcSRichard Gong 	return sprintf(buf, "0x%08x\n", priv->dcmf_version.dcmf2);
49275bc73fcSRichard Gong }
49375bc73fcSRichard Gong 
dcmf3_show(struct device * dev,struct device_attribute * attr,char * buf)49475bc73fcSRichard Gong static ssize_t dcmf3_show(struct device *dev,
49575bc73fcSRichard Gong 			  struct device_attribute *attr, char *buf)
49675bc73fcSRichard Gong {
49775bc73fcSRichard Gong 	struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
49875bc73fcSRichard Gong 
49975bc73fcSRichard Gong 	if (!priv)
50075bc73fcSRichard Gong 		return -ENODEV;
50175bc73fcSRichard Gong 
50275bc73fcSRichard Gong 	return sprintf(buf, "0x%08x\n", priv->dcmf_version.dcmf3);
50375bc73fcSRichard Gong }
50475bc73fcSRichard Gong 
dcmf0_status_show(struct device * dev,struct device_attribute * attr,char * buf)5054a6c8c56SKah Jing Lee static ssize_t dcmf0_status_show(struct device *dev,
5064a6c8c56SKah Jing Lee 				 struct device_attribute *attr, char *buf)
5074a6c8c56SKah Jing Lee {
5084a6c8c56SKah Jing Lee 	struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
5094a6c8c56SKah Jing Lee 
5104a6c8c56SKah Jing Lee 	if (!priv)
5114a6c8c56SKah Jing Lee 		return -ENODEV;
5124a6c8c56SKah Jing Lee 
5134a6c8c56SKah Jing Lee 	if (priv->dcmf_status.dcmf0 == INVALID_DCMF_STATUS)
5144a6c8c56SKah Jing Lee 		return -EIO;
5154a6c8c56SKah Jing Lee 
5164a6c8c56SKah Jing Lee 	return sprintf(buf, "0x%08x\n", priv->dcmf_status.dcmf0);
5174a6c8c56SKah Jing Lee }
5184a6c8c56SKah Jing Lee 
dcmf1_status_show(struct device * dev,struct device_attribute * attr,char * buf)5194a6c8c56SKah Jing Lee static ssize_t dcmf1_status_show(struct device *dev,
5204a6c8c56SKah Jing Lee 				 struct device_attribute *attr, char *buf)
5214a6c8c56SKah Jing Lee {
5224a6c8c56SKah Jing Lee 	struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
5234a6c8c56SKah Jing Lee 
5244a6c8c56SKah Jing Lee 	if (!priv)
5254a6c8c56SKah Jing Lee 		return -ENODEV;
5264a6c8c56SKah Jing Lee 
5274a6c8c56SKah Jing Lee 	if (priv->dcmf_status.dcmf1 == INVALID_DCMF_STATUS)
5284a6c8c56SKah Jing Lee 		return -EIO;
5294a6c8c56SKah Jing Lee 
5304a6c8c56SKah Jing Lee 	return sprintf(buf, "0x%08x\n", priv->dcmf_status.dcmf1);
5314a6c8c56SKah Jing Lee }
5324a6c8c56SKah Jing Lee 
dcmf2_status_show(struct device * dev,struct device_attribute * attr,char * buf)5334a6c8c56SKah Jing Lee static ssize_t dcmf2_status_show(struct device *dev,
5344a6c8c56SKah Jing Lee 				struct device_attribute *attr, char *buf)
5354a6c8c56SKah Jing Lee {
5364a6c8c56SKah Jing Lee 	struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
5374a6c8c56SKah Jing Lee 
5384a6c8c56SKah Jing Lee 	if (!priv)
5394a6c8c56SKah Jing Lee 		return -ENODEV;
5404a6c8c56SKah Jing Lee 
5414a6c8c56SKah Jing Lee 	if (priv->dcmf_status.dcmf2 == INVALID_DCMF_STATUS)
5424a6c8c56SKah Jing Lee 		return -EIO;
5434a6c8c56SKah Jing Lee 
5444a6c8c56SKah Jing Lee 	return sprintf(buf, "0x%08x\n", priv->dcmf_status.dcmf2);
5454a6c8c56SKah Jing Lee }
5464a6c8c56SKah Jing Lee 
dcmf3_status_show(struct device * dev,struct device_attribute * attr,char * buf)5474a6c8c56SKah Jing Lee static ssize_t dcmf3_status_show(struct device *dev,
5484a6c8c56SKah Jing Lee 				 struct device_attribute *attr, char *buf)
5494a6c8c56SKah Jing Lee {
5504a6c8c56SKah Jing Lee 	struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
5514a6c8c56SKah Jing Lee 
5524a6c8c56SKah Jing Lee 	if (!priv)
5534a6c8c56SKah Jing Lee 		return -ENODEV;
5544a6c8c56SKah Jing Lee 
5554a6c8c56SKah Jing Lee 	if (priv->dcmf_status.dcmf3 == INVALID_DCMF_STATUS)
5564a6c8c56SKah Jing Lee 		return -EIO;
5574a6c8c56SKah Jing Lee 
5584a6c8c56SKah Jing Lee 	return sprintf(buf, "0x%08x\n", priv->dcmf_status.dcmf3);
5594a6c8c56SKah Jing Lee }
reboot_image_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)5604526ebbcSRichard Gong static ssize_t reboot_image_store(struct device *dev,
5614526ebbcSRichard Gong 				  struct device_attribute *attr,
5624526ebbcSRichard Gong 				  const char *buf, size_t count)
5634526ebbcSRichard Gong {
5644526ebbcSRichard Gong 	struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
5654526ebbcSRichard Gong 	unsigned long address;
5664526ebbcSRichard Gong 	int ret;
5674526ebbcSRichard Gong 
56852f944eeSRichard Gong 	if (!priv)
5694526ebbcSRichard Gong 		return -ENODEV;
5704526ebbcSRichard Gong 
5714526ebbcSRichard Gong 	ret = kstrtoul(buf, 0, &address);
5724526ebbcSRichard Gong 	if (ret)
5734526ebbcSRichard Gong 		return ret;
5744526ebbcSRichard Gong 
5754526ebbcSRichard Gong 	ret = rsu_send_msg(priv, COMMAND_RSU_UPDATE,
5764526ebbcSRichard Gong 			   address, rsu_command_callback);
5774526ebbcSRichard Gong 	if (ret) {
5784526ebbcSRichard Gong 		dev_err(dev, "Error, RSU update returned %i\n", ret);
5794526ebbcSRichard Gong 		return ret;
5804526ebbcSRichard Gong 	}
5814526ebbcSRichard Gong 
5824526ebbcSRichard Gong 	return count;
5834526ebbcSRichard Gong }
5844526ebbcSRichard Gong 
notify_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)5854526ebbcSRichard Gong static ssize_t notify_store(struct device *dev,
5864526ebbcSRichard Gong 			    struct device_attribute *attr,
5874526ebbcSRichard Gong 			    const char *buf, size_t count)
5884526ebbcSRichard Gong {
5894526ebbcSRichard Gong 	struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
5904526ebbcSRichard Gong 	unsigned long status;
5914526ebbcSRichard Gong 	int ret;
5924526ebbcSRichard Gong 
59352f944eeSRichard Gong 	if (!priv)
5944526ebbcSRichard Gong 		return -ENODEV;
5954526ebbcSRichard Gong 
5964526ebbcSRichard Gong 	ret = kstrtoul(buf, 0, &status);
5974526ebbcSRichard Gong 	if (ret)
5984526ebbcSRichard Gong 		return ret;
5994526ebbcSRichard Gong 
6004526ebbcSRichard Gong 	ret = rsu_send_msg(priv, COMMAND_RSU_NOTIFY,
6014526ebbcSRichard Gong 			   status, rsu_command_callback);
6024526ebbcSRichard Gong 	if (ret) {
6034526ebbcSRichard Gong 		dev_err(dev, "Error, RSU notify returned %i\n", ret);
6044526ebbcSRichard Gong 		return ret;
6054526ebbcSRichard Gong 	}
6064526ebbcSRichard Gong 
6074526ebbcSRichard Gong 	/* to get the updated state */
6084526ebbcSRichard Gong 	ret = rsu_send_msg(priv, COMMAND_RSU_STATUS,
6094526ebbcSRichard Gong 			   0, rsu_status_callback);
6104526ebbcSRichard Gong 	if (ret) {
6114526ebbcSRichard Gong 		dev_err(dev, "Error, getting RSU status %i\n", ret);
6124526ebbcSRichard Gong 		return ret;
6134526ebbcSRichard Gong 	}
6144526ebbcSRichard Gong 
615e9cb0497SRichard Gong 	ret = rsu_send_msg(priv, COMMAND_RSU_RETRY, 0, rsu_retry_callback);
6164526ebbcSRichard Gong 	if (ret) {
617e9cb0497SRichard Gong 		dev_err(dev, "Error, getting RSU retry %i\n", ret);
6184526ebbcSRichard Gong 		return ret;
6194526ebbcSRichard Gong 	}
6204526ebbcSRichard Gong 
6214526ebbcSRichard Gong 	return count;
6224526ebbcSRichard Gong }
6234526ebbcSRichard Gong 
spt0_address_show(struct device * dev,struct device_attribute * attr,char * buf)624*abe8ff43SRadu Bacrau static ssize_t spt0_address_show(struct device *dev,
625*abe8ff43SRadu Bacrau 				 struct device_attribute *attr, char *buf)
626*abe8ff43SRadu Bacrau {
627*abe8ff43SRadu Bacrau 	struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
628*abe8ff43SRadu Bacrau 
629*abe8ff43SRadu Bacrau 	if (!priv)
630*abe8ff43SRadu Bacrau 		return -ENODEV;
631*abe8ff43SRadu Bacrau 
632*abe8ff43SRadu Bacrau 	if (priv->spt0_address == INVALID_SPT_ADDRESS)
633*abe8ff43SRadu Bacrau 		return -EIO;
634*abe8ff43SRadu Bacrau 
635*abe8ff43SRadu Bacrau 	return scnprintf(buf, PAGE_SIZE, "0x%08lx\n", priv->spt0_address);
636*abe8ff43SRadu Bacrau }
637*abe8ff43SRadu Bacrau 
spt1_address_show(struct device * dev,struct device_attribute * attr,char * buf)638*abe8ff43SRadu Bacrau static ssize_t spt1_address_show(struct device *dev,
639*abe8ff43SRadu Bacrau 				 struct device_attribute *attr, char *buf)
640*abe8ff43SRadu Bacrau {
641*abe8ff43SRadu Bacrau 	struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
642*abe8ff43SRadu Bacrau 
643*abe8ff43SRadu Bacrau 	if (!priv)
644*abe8ff43SRadu Bacrau 		return -ENODEV;
645*abe8ff43SRadu Bacrau 
646*abe8ff43SRadu Bacrau 	if (priv->spt1_address == INVALID_SPT_ADDRESS)
647*abe8ff43SRadu Bacrau 		return -EIO;
648*abe8ff43SRadu Bacrau 
649*abe8ff43SRadu Bacrau 	return scnprintf(buf, PAGE_SIZE, "0x%08lx\n", priv->spt1_address);
650*abe8ff43SRadu Bacrau }
651*abe8ff43SRadu Bacrau 
6524526ebbcSRichard Gong static DEVICE_ATTR_RO(current_image);
6534526ebbcSRichard Gong static DEVICE_ATTR_RO(fail_image);
6544526ebbcSRichard Gong static DEVICE_ATTR_RO(state);
6554526ebbcSRichard Gong static DEVICE_ATTR_RO(version);
6564526ebbcSRichard Gong static DEVICE_ATTR_RO(error_location);
6574526ebbcSRichard Gong static DEVICE_ATTR_RO(error_details);
6584526ebbcSRichard Gong static DEVICE_ATTR_RO(retry_counter);
65975bc73fcSRichard Gong static DEVICE_ATTR_RO(max_retry);
66075bc73fcSRichard Gong static DEVICE_ATTR_RO(dcmf0);
66175bc73fcSRichard Gong static DEVICE_ATTR_RO(dcmf1);
66275bc73fcSRichard Gong static DEVICE_ATTR_RO(dcmf2);
66375bc73fcSRichard Gong static DEVICE_ATTR_RO(dcmf3);
6644a6c8c56SKah Jing Lee static DEVICE_ATTR_RO(dcmf0_status);
6654a6c8c56SKah Jing Lee static DEVICE_ATTR_RO(dcmf1_status);
6664a6c8c56SKah Jing Lee static DEVICE_ATTR_RO(dcmf2_status);
6674a6c8c56SKah Jing Lee static DEVICE_ATTR_RO(dcmf3_status);
6684526ebbcSRichard Gong static DEVICE_ATTR_WO(reboot_image);
6694526ebbcSRichard Gong static DEVICE_ATTR_WO(notify);
670*abe8ff43SRadu Bacrau static DEVICE_ATTR_RO(spt0_address);
671*abe8ff43SRadu Bacrau static DEVICE_ATTR_RO(spt1_address);
6724526ebbcSRichard Gong 
6734526ebbcSRichard Gong static struct attribute *rsu_attrs[] = {
6744526ebbcSRichard Gong 	&dev_attr_current_image.attr,
6754526ebbcSRichard Gong 	&dev_attr_fail_image.attr,
6764526ebbcSRichard Gong 	&dev_attr_state.attr,
6774526ebbcSRichard Gong 	&dev_attr_version.attr,
6784526ebbcSRichard Gong 	&dev_attr_error_location.attr,
6794526ebbcSRichard Gong 	&dev_attr_error_details.attr,
6804526ebbcSRichard Gong 	&dev_attr_retry_counter.attr,
68175bc73fcSRichard Gong 	&dev_attr_max_retry.attr,
68275bc73fcSRichard Gong 	&dev_attr_dcmf0.attr,
68375bc73fcSRichard Gong 	&dev_attr_dcmf1.attr,
68475bc73fcSRichard Gong 	&dev_attr_dcmf2.attr,
68575bc73fcSRichard Gong 	&dev_attr_dcmf3.attr,
6864a6c8c56SKah Jing Lee 	&dev_attr_dcmf0_status.attr,
6874a6c8c56SKah Jing Lee 	&dev_attr_dcmf1_status.attr,
6884a6c8c56SKah Jing Lee 	&dev_attr_dcmf2_status.attr,
6894a6c8c56SKah Jing Lee 	&dev_attr_dcmf3_status.attr,
6904526ebbcSRichard Gong 	&dev_attr_reboot_image.attr,
6914526ebbcSRichard Gong 	&dev_attr_notify.attr,
692*abe8ff43SRadu Bacrau 	&dev_attr_spt0_address.attr,
693*abe8ff43SRadu Bacrau 	&dev_attr_spt1_address.attr,
6944526ebbcSRichard Gong 	NULL
6954526ebbcSRichard Gong };
6964526ebbcSRichard Gong 
6974526ebbcSRichard Gong ATTRIBUTE_GROUPS(rsu);
6984526ebbcSRichard Gong 
stratix10_rsu_probe(struct platform_device * pdev)6994526ebbcSRichard Gong static int stratix10_rsu_probe(struct platform_device *pdev)
7004526ebbcSRichard Gong {
7014526ebbcSRichard Gong 	struct device *dev = &pdev->dev;
7024526ebbcSRichard Gong 	struct stratix10_rsu_priv *priv;
7034526ebbcSRichard Gong 	int ret;
7044526ebbcSRichard Gong 
7054526ebbcSRichard Gong 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
7064526ebbcSRichard Gong 	if (!priv)
7074526ebbcSRichard Gong 		return -ENOMEM;
7084526ebbcSRichard Gong 
7094526ebbcSRichard Gong 	priv->client.dev = dev;
7104526ebbcSRichard Gong 	priv->client.receive_cb = NULL;
7114526ebbcSRichard Gong 	priv->client.priv = priv;
7124526ebbcSRichard Gong 	priv->status.current_image = 0;
7134526ebbcSRichard Gong 	priv->status.fail_image = 0;
7144526ebbcSRichard Gong 	priv->status.error_location = 0;
7154526ebbcSRichard Gong 	priv->status.error_details = 0;
7164526ebbcSRichard Gong 	priv->status.version = 0;
7174526ebbcSRichard Gong 	priv->status.state = 0;
7184526ebbcSRichard Gong 	priv->retry_counter = INVALID_RETRY_COUNTER;
71975bc73fcSRichard Gong 	priv->dcmf_version.dcmf0 = INVALID_DCMF_VERSION;
72075bc73fcSRichard Gong 	priv->dcmf_version.dcmf1 = INVALID_DCMF_VERSION;
72175bc73fcSRichard Gong 	priv->dcmf_version.dcmf2 = INVALID_DCMF_VERSION;
72275bc73fcSRichard Gong 	priv->dcmf_version.dcmf3 = INVALID_DCMF_VERSION;
7234a6c8c56SKah Jing Lee 	priv->dcmf_status.dcmf0 = INVALID_DCMF_STATUS;
7244a6c8c56SKah Jing Lee 	priv->dcmf_status.dcmf1 = INVALID_DCMF_STATUS;
7254a6c8c56SKah Jing Lee 	priv->dcmf_status.dcmf2 = INVALID_DCMF_STATUS;
7264a6c8c56SKah Jing Lee 	priv->dcmf_status.dcmf3 = INVALID_DCMF_STATUS;
727*abe8ff43SRadu Bacrau 	priv->max_retry = INVALID_RETRY_COUNTER;
728*abe8ff43SRadu Bacrau 	priv->spt0_address = INVALID_SPT_ADDRESS;
729*abe8ff43SRadu Bacrau 	priv->spt1_address = INVALID_SPT_ADDRESS;
7304526ebbcSRichard Gong 
7314526ebbcSRichard Gong 	mutex_init(&priv->lock);
7324526ebbcSRichard Gong 	priv->chan = stratix10_svc_request_channel_byname(&priv->client,
7334526ebbcSRichard Gong 							  SVC_CLIENT_RSU);
7344526ebbcSRichard Gong 	if (IS_ERR(priv->chan)) {
7354526ebbcSRichard Gong 		dev_err(dev, "couldn't get service channel %s\n",
7364526ebbcSRichard Gong 			SVC_CLIENT_RSU);
7374526ebbcSRichard Gong 		return PTR_ERR(priv->chan);
7384526ebbcSRichard Gong 	}
7394526ebbcSRichard Gong 
7404526ebbcSRichard Gong 	init_completion(&priv->completion);
7414526ebbcSRichard Gong 	platform_set_drvdata(pdev, priv);
7424526ebbcSRichard Gong 
7434526ebbcSRichard Gong 	/* get the initial state from firmware */
7444526ebbcSRichard Gong 	ret = rsu_send_msg(priv, COMMAND_RSU_STATUS,
7454526ebbcSRichard Gong 			   0, rsu_status_callback);
7464526ebbcSRichard Gong 	if (ret) {
7474526ebbcSRichard Gong 		dev_err(dev, "Error, getting RSU status %i\n", ret);
7484526ebbcSRichard Gong 		stratix10_svc_free_channel(priv->chan);
7494526ebbcSRichard Gong 	}
7504526ebbcSRichard Gong 
75175bc73fcSRichard Gong 	/* get DCMF version from firmware */
75275bc73fcSRichard Gong 	ret = rsu_send_msg(priv, COMMAND_RSU_DCMF_VERSION,
75375bc73fcSRichard Gong 			   0, rsu_dcmf_version_callback);
75475bc73fcSRichard Gong 	if (ret) {
75575bc73fcSRichard Gong 		dev_err(dev, "Error, getting DCMF version %i\n", ret);
75675bc73fcSRichard Gong 		stratix10_svc_free_channel(priv->chan);
75775bc73fcSRichard Gong 	}
75875bc73fcSRichard Gong 
7594a6c8c56SKah Jing Lee 	ret = rsu_send_msg(priv, COMMAND_RSU_DCMF_STATUS,
7604a6c8c56SKah Jing Lee 			   0, rsu_dcmf_status_callback);
7614a6c8c56SKah Jing Lee 	if (ret) {
7624a6c8c56SKah Jing Lee 		dev_err(dev, "Error, getting DCMF status %i\n", ret);
7634a6c8c56SKah Jing Lee 		stratix10_svc_free_channel(priv->chan);
7644a6c8c56SKah Jing Lee 	}
7654a6c8c56SKah Jing Lee 
766e9cb0497SRichard Gong 	ret = rsu_send_msg(priv, COMMAND_RSU_RETRY, 0, rsu_retry_callback);
7674526ebbcSRichard Gong 	if (ret) {
768e9cb0497SRichard Gong 		dev_err(dev, "Error, getting RSU retry %i\n", ret);
7694526ebbcSRichard Gong 		stratix10_svc_free_channel(priv->chan);
7704526ebbcSRichard Gong 	}
7714526ebbcSRichard Gong 
77275bc73fcSRichard Gong 	ret = rsu_send_msg(priv, COMMAND_RSU_MAX_RETRY, 0,
77375bc73fcSRichard Gong 			   rsu_max_retry_callback);
77475bc73fcSRichard Gong 	if (ret) {
77575bc73fcSRichard Gong 		dev_err(dev, "Error, getting RSU max retry %i\n", ret);
77675bc73fcSRichard Gong 		stratix10_svc_free_channel(priv->chan);
77775bc73fcSRichard Gong 	}
77875bc73fcSRichard Gong 
779*abe8ff43SRadu Bacrau 	priv->get_spt_response_buf =
780*abe8ff43SRadu Bacrau 		stratix10_svc_allocate_memory(priv->chan, RSU_GET_SPT_RESP_LEN);
781*abe8ff43SRadu Bacrau 
782*abe8ff43SRadu Bacrau 	if (IS_ERR(priv->get_spt_response_buf)) {
783*abe8ff43SRadu Bacrau 		dev_err(dev, "failed to allocate get spt buffer\n");
784*abe8ff43SRadu Bacrau 	} else {
785*abe8ff43SRadu Bacrau 		ret = rsu_send_msg(priv, COMMAND_MBOX_SEND_CMD,
786*abe8ff43SRadu Bacrau 				   RSU_GET_SPT_CMD, rsu_get_spt_callback);
787*abe8ff43SRadu Bacrau 		if (ret) {
788*abe8ff43SRadu Bacrau 			dev_err(dev, "Error, getting SPT table %i\n", ret);
789*abe8ff43SRadu Bacrau 			stratix10_svc_free_channel(priv->chan);
790*abe8ff43SRadu Bacrau 		}
791*abe8ff43SRadu Bacrau 	}
792*abe8ff43SRadu Bacrau 
7934526ebbcSRichard Gong 	return ret;
7944526ebbcSRichard Gong }
7954526ebbcSRichard Gong 
stratix10_rsu_remove(struct platform_device * pdev)7964526ebbcSRichard Gong static int stratix10_rsu_remove(struct platform_device *pdev)
7974526ebbcSRichard Gong {
7984526ebbcSRichard Gong 	struct stratix10_rsu_priv *priv = platform_get_drvdata(pdev);
7994526ebbcSRichard Gong 
8004526ebbcSRichard Gong 	stratix10_svc_free_channel(priv->chan);
8014526ebbcSRichard Gong 	return 0;
8024526ebbcSRichard Gong }
8034526ebbcSRichard Gong 
8044526ebbcSRichard Gong static struct platform_driver stratix10_rsu_driver = {
8054526ebbcSRichard Gong 	.probe = stratix10_rsu_probe,
8064526ebbcSRichard Gong 	.remove = stratix10_rsu_remove,
8074526ebbcSRichard Gong 	.driver = {
8084526ebbcSRichard Gong 		.name = "stratix10-rsu",
8094526ebbcSRichard Gong 		.dev_groups = rsu_groups,
8104526ebbcSRichard Gong 	},
8114526ebbcSRichard Gong };
8124526ebbcSRichard Gong 
8134526ebbcSRichard Gong module_platform_driver(stratix10_rsu_driver);
8144526ebbcSRichard Gong 
8154526ebbcSRichard Gong MODULE_LICENSE("GPL v2");
8164526ebbcSRichard Gong MODULE_DESCRIPTION("Intel Remote System Update Driver");
8174526ebbcSRichard Gong MODULE_AUTHOR("Richard Gong <richard.gong@intel.com>");
818