stratix10-rsu.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2018-2019, Intel Corporation
  4. * Copyright (C) 2025, Altera Corporation
  5. */
  6. #include <linux/arm-smccc.h>
  7. #include <linux/bitfield.h>
  8. #include <linux/completion.h>
  9. #include <linux/kobject.h>
  10. #include <linux/module.h>
  11. #include <linux/mutex.h>
  12. #include <linux/of.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/firmware/intel/stratix10-svc-client.h>
  15. #include <linux/string.h>
  16. #include <linux/sysfs.h>
  17. #include <linux/delay.h>
  18. #define RSU_ERASE_SIZE_MASK GENMASK_ULL(63, 32)
  19. #define RSU_DCMF0_MASK GENMASK_ULL(31, 0)
  20. #define RSU_DCMF1_MASK GENMASK_ULL(63, 32)
  21. #define RSU_DCMF2_MASK GENMASK_ULL(31, 0)
  22. #define RSU_DCMF3_MASK GENMASK_ULL(63, 32)
  23. #define RSU_DCMF0_STATUS_MASK GENMASK_ULL(15, 0)
  24. #define RSU_DCMF1_STATUS_MASK GENMASK_ULL(31, 16)
  25. #define RSU_DCMF2_STATUS_MASK GENMASK_ULL(47, 32)
  26. #define RSU_DCMF3_STATUS_MASK GENMASK_ULL(63, 48)
  27. #define RSU_TIMEOUT (msecs_to_jiffies(SVC_RSU_REQUEST_TIMEOUT_MS))
  28. #define INVALID_RETRY_COUNTER 0xFF
  29. #define INVALID_DCMF_VERSION 0xFF
  30. #define INVALID_DCMF_STATUS 0xFFFFFFFF
  31. #define INVALID_SPT_ADDRESS 0x0
  32. #define RSU_RETRY_SLEEP_MS (1U)
  33. #define RSU_ASYNC_MSG_RETRY (3U)
  34. #define RSU_GET_SPT_RESP_LEN (4 * sizeof(unsigned int))
  35. typedef void (*rsu_callback)(struct stratix10_svc_client *client,
  36. struct stratix10_svc_cb_data *data);
  37. /**
  38. * struct stratix10_rsu_priv - rsu data structure
  39. * @chan: pointer to the allocated service channel
  40. * @client: active service client
  41. * @completion: state for callback completion
  42. * @lock: a mutex to protect callback completion state
  43. * @status.current_image: address of image currently running in flash
  44. * @status.fail_image: address of failed image in flash
  45. * @status.version: the interface version number of RSU firmware
  46. * @status.state: the state of RSU system
  47. * @status.error_details: error code
  48. * @status.error_location: the error offset inside the image that failed
  49. * @dcmf_version.dcmf0: Quartus dcmf0 version
  50. * @dcmf_version.dcmf1: Quartus dcmf1 version
  51. * @dcmf_version.dcmf2: Quartus dcmf2 version
  52. * @dcmf_version.dcmf3: Quartus dcmf3 version
  53. * @dcmf_status.dcmf0: dcmf0 status
  54. * @dcmf_status.dcmf1: dcmf1 status
  55. * @dcmf_status.dcmf2: dcmf2 status
  56. * @dcmf_status.dcmf3: dcmf3 status
  57. * @retry_counter: the current image's retry counter
  58. * @max_retry: the preset max retry value
  59. * @spt0_address: address of spt0
  60. * @spt1_address: address of spt1
  61. */
  62. struct stratix10_rsu_priv {
  63. struct stratix10_svc_chan *chan;
  64. struct stratix10_svc_client client;
  65. struct completion completion;
  66. struct mutex lock;
  67. struct {
  68. unsigned long current_image;
  69. unsigned long fail_image;
  70. unsigned int version;
  71. unsigned int state;
  72. unsigned int error_details;
  73. unsigned int error_location;
  74. } status;
  75. struct {
  76. unsigned int dcmf0;
  77. unsigned int dcmf1;
  78. unsigned int dcmf2;
  79. unsigned int dcmf3;
  80. } dcmf_version;
  81. struct {
  82. unsigned int dcmf0;
  83. unsigned int dcmf1;
  84. unsigned int dcmf2;
  85. unsigned int dcmf3;
  86. } dcmf_status;
  87. unsigned int retry_counter;
  88. unsigned int max_retry;
  89. unsigned long spt0_address;
  90. unsigned long spt1_address;
  91. };
  92. typedef void (*rsu_async_callback)(struct device *dev,
  93. struct stratix10_rsu_priv *priv, struct stratix10_svc_cb_data *data);
  94. /**
  95. * rsu_async_status_callback() - Status callback from rsu_async_send()
  96. * @dev: pointer to device object
  97. * @priv: pointer to priv object
  98. * @data: pointer to callback data structure
  99. *
  100. * Callback from rsu_async_send() to get the system rsu error status.
  101. */
  102. static void rsu_async_status_callback(struct device *dev,
  103. struct stratix10_rsu_priv *priv,
  104. struct stratix10_svc_cb_data *data)
  105. {
  106. struct arm_smccc_1_2_regs *res = (struct arm_smccc_1_2_regs *)data->kaddr1;
  107. priv->status.current_image = res->a2;
  108. priv->status.fail_image = res->a3;
  109. priv->status.state = res->a4;
  110. priv->status.version = res->a5;
  111. priv->status.error_location = res->a7;
  112. priv->status.error_details = res->a8;
  113. priv->retry_counter = res->a9;
  114. }
  115. /**
  116. * rsu_command_callback() - Update callback from Intel Service Layer
  117. * @client: pointer to client
  118. * @data: pointer to callback data structure
  119. *
  120. * Callback from Intel service layer for RSU commands.
  121. */
  122. static void rsu_command_callback(struct stratix10_svc_client *client,
  123. struct stratix10_svc_cb_data *data)
  124. {
  125. struct stratix10_rsu_priv *priv = client->priv;
  126. if (data->status == BIT(SVC_STATUS_NO_SUPPORT))
  127. dev_warn(client->dev, "Secure FW doesn't support notify\n");
  128. else if (data->status == BIT(SVC_STATUS_ERROR))
  129. dev_err(client->dev, "Failure, returned status is %lu\n",
  130. BIT(data->status));
  131. complete(&priv->completion);
  132. }
  133. /**
  134. * rsu_max_retry_callback() - Callback from Intel service layer for getting
  135. * the max retry value from the firmware
  136. * @client: pointer to client
  137. * @data: pointer to callback data structure
  138. *
  139. * Callback from Intel service layer for max retry.
  140. */
  141. static void rsu_max_retry_callback(struct stratix10_svc_client *client,
  142. struct stratix10_svc_cb_data *data)
  143. {
  144. struct stratix10_rsu_priv *priv = client->priv;
  145. unsigned int *max_retry = (unsigned int *)data->kaddr1;
  146. if (data->status == BIT(SVC_STATUS_OK))
  147. priv->max_retry = *max_retry;
  148. else if (data->status == BIT(SVC_STATUS_NO_SUPPORT))
  149. dev_warn(client->dev, "Secure FW doesn't support max retry\n");
  150. else
  151. dev_err(client->dev, "Failed to get max retry %lu\n",
  152. BIT(data->status));
  153. complete(&priv->completion);
  154. }
  155. /**
  156. * rsu_dcmf_version_callback() - Callback from Intel service layer for getting
  157. * the DCMF version
  158. * @client: pointer to client
  159. * @data: pointer to callback data structure
  160. *
  161. * Callback from Intel service layer for DCMF version number
  162. */
  163. static void rsu_dcmf_version_callback(struct stratix10_svc_client *client,
  164. struct stratix10_svc_cb_data *data)
  165. {
  166. struct stratix10_rsu_priv *priv = client->priv;
  167. unsigned long long *value1 = (unsigned long long *)data->kaddr1;
  168. unsigned long long *value2 = (unsigned long long *)data->kaddr2;
  169. if (data->status == BIT(SVC_STATUS_OK)) {
  170. priv->dcmf_version.dcmf0 = FIELD_GET(RSU_DCMF0_MASK, *value1);
  171. priv->dcmf_version.dcmf1 = FIELD_GET(RSU_DCMF1_MASK, *value1);
  172. priv->dcmf_version.dcmf2 = FIELD_GET(RSU_DCMF2_MASK, *value2);
  173. priv->dcmf_version.dcmf3 = FIELD_GET(RSU_DCMF3_MASK, *value2);
  174. } else
  175. dev_err(client->dev, "failed to get DCMF version\n");
  176. complete(&priv->completion);
  177. }
  178. /**
  179. * rsu_dcmf_status_callback() - Callback from Intel service layer for getting
  180. * the DCMF status
  181. * @client: pointer to client
  182. * @data: pointer to callback data structure
  183. *
  184. * Callback from Intel service layer for DCMF status
  185. */
  186. static void rsu_dcmf_status_callback(struct stratix10_svc_client *client,
  187. struct stratix10_svc_cb_data *data)
  188. {
  189. struct stratix10_rsu_priv *priv = client->priv;
  190. unsigned long long *value = (unsigned long long *)data->kaddr1;
  191. if (data->status == BIT(SVC_STATUS_OK)) {
  192. priv->dcmf_status.dcmf0 = FIELD_GET(RSU_DCMF0_STATUS_MASK,
  193. *value);
  194. priv->dcmf_status.dcmf1 = FIELD_GET(RSU_DCMF1_STATUS_MASK,
  195. *value);
  196. priv->dcmf_status.dcmf2 = FIELD_GET(RSU_DCMF2_STATUS_MASK,
  197. *value);
  198. priv->dcmf_status.dcmf3 = FIELD_GET(RSU_DCMF3_STATUS_MASK,
  199. *value);
  200. } else
  201. dev_err(client->dev, "failed to get DCMF status\n");
  202. complete(&priv->completion);
  203. }
  204. /**
  205. * rsu_async_get_spt_table_callback() - Callback to be used by the rsu_async_send()
  206. * to retrieve the SPT table information.
  207. * @dev: pointer to device object
  208. * @priv: pointer to priv object
  209. * @data: pointer to callback data structure
  210. */
  211. static void rsu_async_get_spt_table_callback(struct device *dev,
  212. struct stratix10_rsu_priv *priv,
  213. struct stratix10_svc_cb_data *data)
  214. {
  215. priv->spt0_address = *((unsigned long *)data->kaddr1);
  216. priv->spt1_address = *((unsigned long *)data->kaddr2);
  217. }
  218. /**
  219. * rsu_send_msg() - send a message to Intel service layer
  220. * @priv: pointer to rsu private data
  221. * @command: RSU status or update command
  222. * @arg: the request argument, the bitstream address or notify status
  223. * @callback: function pointer for the callback (status or update)
  224. *
  225. * Start an Intel service layer transaction to perform the SMC call that
  226. * is necessary to get RSU boot log or set the address of bitstream to
  227. * boot after reboot.
  228. *
  229. * Returns 0 on success or -ETIMEDOUT on error.
  230. */
  231. static int rsu_send_msg(struct stratix10_rsu_priv *priv,
  232. enum stratix10_svc_command_code command,
  233. unsigned long arg,
  234. rsu_callback callback)
  235. {
  236. struct stratix10_svc_client_msg msg;
  237. int ret;
  238. mutex_lock(&priv->lock);
  239. reinit_completion(&priv->completion);
  240. priv->client.receive_cb = callback;
  241. msg.command = command;
  242. if (arg)
  243. msg.arg[0] = arg;
  244. ret = stratix10_svc_send(priv->chan, &msg);
  245. if (ret < 0)
  246. goto status_done;
  247. ret = wait_for_completion_interruptible_timeout(&priv->completion,
  248. RSU_TIMEOUT);
  249. if (!ret) {
  250. dev_err(priv->client.dev,
  251. "timeout waiting for SMC call\n");
  252. ret = -ETIMEDOUT;
  253. goto status_done;
  254. } else if (ret < 0) {
  255. dev_err(priv->client.dev,
  256. "error %d waiting for SMC call\n", ret);
  257. goto status_done;
  258. } else {
  259. ret = 0;
  260. }
  261. status_done:
  262. stratix10_svc_done(priv->chan);
  263. mutex_unlock(&priv->lock);
  264. return ret;
  265. }
  266. /**
  267. * soc64_async_callback() - Callback from Intel service layer for async requests
  268. * @ptr: pointer to the completion object
  269. */
  270. static void soc64_async_callback(void *ptr)
  271. {
  272. if (ptr)
  273. complete(ptr);
  274. }
  275. /**
  276. * rsu_send_async_msg() - send an async message to Intel service layer
  277. * @dev: pointer to device object
  278. * @priv: pointer to rsu private data
  279. * @command: RSU status or update command
  280. * @arg: the request argument, notify status
  281. * @callback: function pointer for the callback (status or update)
  282. */
  283. static int rsu_send_async_msg(struct device *dev, struct stratix10_rsu_priv *priv,
  284. enum stratix10_svc_command_code command,
  285. unsigned long arg,
  286. rsu_async_callback callback)
  287. {
  288. struct stratix10_svc_client_msg msg = {0};
  289. struct stratix10_svc_cb_data data = {0};
  290. struct completion completion;
  291. int status, index, ret;
  292. void *handle = NULL;
  293. msg.command = command;
  294. msg.arg[0] = arg;
  295. init_completion(&completion);
  296. for (index = 0; index < RSU_ASYNC_MSG_RETRY; index++) {
  297. status = stratix10_svc_async_send(priv->chan, &msg,
  298. &handle, soc64_async_callback,
  299. &completion);
  300. if (status == 0)
  301. break;
  302. dev_warn(dev, "Failed to send async message\n");
  303. msleep(RSU_RETRY_SLEEP_MS);
  304. }
  305. if (status && !handle) {
  306. dev_err(dev, "Failed to send async message\n");
  307. return -ETIMEDOUT;
  308. }
  309. ret = wait_for_completion_io_timeout(&completion, RSU_TIMEOUT);
  310. if (ret > 0)
  311. dev_dbg(dev, "Received async interrupt\n");
  312. else if (ret == 0)
  313. dev_dbg(dev, "Timeout occurred. Trying to poll the response\n");
  314. for (index = 0; index < RSU_ASYNC_MSG_RETRY; index++) {
  315. status = stratix10_svc_async_poll(priv->chan, handle, &data);
  316. if (status == -EAGAIN) {
  317. dev_dbg(dev, "Async message is still in progress\n");
  318. } else if (status < 0) {
  319. dev_alert(dev, "Failed to poll async message\n");
  320. ret = -ETIMEDOUT;
  321. } else if (status == 0) {
  322. ret = 0;
  323. break;
  324. }
  325. msleep(RSU_RETRY_SLEEP_MS);
  326. }
  327. if (ret) {
  328. dev_err(dev, "Failed to get async response\n");
  329. goto status_done;
  330. }
  331. if (data.status == 0) {
  332. ret = 0;
  333. if (callback)
  334. callback(dev, priv, &data);
  335. } else {
  336. dev_err(dev, "%s returned 0x%x from SDM\n", __func__,
  337. data.status);
  338. ret = -EFAULT;
  339. }
  340. status_done:
  341. stratix10_svc_async_done(priv->chan, handle);
  342. return ret;
  343. }
  344. /*
  345. * This driver exposes some optional features of the Intel Stratix 10 SoC FPGA.
  346. * The sysfs interfaces exposed here are FPGA Remote System Update (RSU)
  347. * related. They allow user space software to query the configuration system
  348. * status and to request optional reboot behavior specific to Intel FPGAs.
  349. */
  350. static ssize_t current_image_show(struct device *dev,
  351. struct device_attribute *attr, char *buf)
  352. {
  353. struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
  354. if (!priv)
  355. return -ENODEV;
  356. return sprintf(buf, "0x%08lx\n", priv->status.current_image);
  357. }
  358. static ssize_t fail_image_show(struct device *dev,
  359. struct device_attribute *attr, char *buf)
  360. {
  361. struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
  362. if (!priv)
  363. return -ENODEV;
  364. return sprintf(buf, "0x%08lx\n", priv->status.fail_image);
  365. }
  366. static ssize_t version_show(struct device *dev, struct device_attribute *attr,
  367. char *buf)
  368. {
  369. struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
  370. if (!priv)
  371. return -ENODEV;
  372. return sprintf(buf, "0x%08x\n", priv->status.version);
  373. }
  374. static ssize_t state_show(struct device *dev, struct device_attribute *attr,
  375. char *buf)
  376. {
  377. struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
  378. if (!priv)
  379. return -ENODEV;
  380. return sprintf(buf, "0x%08x\n", priv->status.state);
  381. }
  382. static ssize_t error_location_show(struct device *dev,
  383. struct device_attribute *attr, char *buf)
  384. {
  385. struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
  386. if (!priv)
  387. return -ENODEV;
  388. return sprintf(buf, "0x%08x\n", priv->status.error_location);
  389. }
  390. static ssize_t error_details_show(struct device *dev,
  391. struct device_attribute *attr, char *buf)
  392. {
  393. struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
  394. if (!priv)
  395. return -ENODEV;
  396. return sprintf(buf, "0x%08x\n", priv->status.error_details);
  397. }
  398. static ssize_t retry_counter_show(struct device *dev,
  399. struct device_attribute *attr, char *buf)
  400. {
  401. struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
  402. if (!priv)
  403. return -ENODEV;
  404. return sprintf(buf, "0x%08x\n", priv->retry_counter);
  405. }
  406. static ssize_t max_retry_show(struct device *dev,
  407. struct device_attribute *attr, char *buf)
  408. {
  409. struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
  410. if (!priv)
  411. return -ENODEV;
  412. return sysfs_emit(buf, "0x%08x\n", priv->max_retry);
  413. }
  414. static ssize_t dcmf0_show(struct device *dev,
  415. struct device_attribute *attr, char *buf)
  416. {
  417. struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
  418. if (!priv)
  419. return -ENODEV;
  420. return sprintf(buf, "0x%08x\n", priv->dcmf_version.dcmf0);
  421. }
  422. static ssize_t dcmf1_show(struct device *dev,
  423. struct device_attribute *attr, char *buf)
  424. {
  425. struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
  426. if (!priv)
  427. return -ENODEV;
  428. return sprintf(buf, "0x%08x\n", priv->dcmf_version.dcmf1);
  429. }
  430. static ssize_t dcmf2_show(struct device *dev,
  431. struct device_attribute *attr, char *buf)
  432. {
  433. struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
  434. if (!priv)
  435. return -ENODEV;
  436. return sprintf(buf, "0x%08x\n", priv->dcmf_version.dcmf2);
  437. }
  438. static ssize_t dcmf3_show(struct device *dev,
  439. struct device_attribute *attr, char *buf)
  440. {
  441. struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
  442. if (!priv)
  443. return -ENODEV;
  444. return sprintf(buf, "0x%08x\n", priv->dcmf_version.dcmf3);
  445. }
  446. static ssize_t dcmf0_status_show(struct device *dev,
  447. struct device_attribute *attr, char *buf)
  448. {
  449. struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
  450. if (!priv)
  451. return -ENODEV;
  452. if (priv->dcmf_status.dcmf0 == INVALID_DCMF_STATUS)
  453. return -EIO;
  454. return sprintf(buf, "0x%08x\n", priv->dcmf_status.dcmf0);
  455. }
  456. static ssize_t dcmf1_status_show(struct device *dev,
  457. struct device_attribute *attr, char *buf)
  458. {
  459. struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
  460. if (!priv)
  461. return -ENODEV;
  462. if (priv->dcmf_status.dcmf1 == INVALID_DCMF_STATUS)
  463. return -EIO;
  464. return sprintf(buf, "0x%08x\n", priv->dcmf_status.dcmf1);
  465. }
  466. static ssize_t dcmf2_status_show(struct device *dev,
  467. struct device_attribute *attr, char *buf)
  468. {
  469. struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
  470. if (!priv)
  471. return -ENODEV;
  472. if (priv->dcmf_status.dcmf2 == INVALID_DCMF_STATUS)
  473. return -EIO;
  474. return sprintf(buf, "0x%08x\n", priv->dcmf_status.dcmf2);
  475. }
  476. static ssize_t dcmf3_status_show(struct device *dev,
  477. struct device_attribute *attr, char *buf)
  478. {
  479. struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
  480. if (!priv)
  481. return -ENODEV;
  482. if (priv->dcmf_status.dcmf3 == INVALID_DCMF_STATUS)
  483. return -EIO;
  484. return sprintf(buf, "0x%08x\n", priv->dcmf_status.dcmf3);
  485. }
  486. static ssize_t reboot_image_store(struct device *dev,
  487. struct device_attribute *attr,
  488. const char *buf, size_t count)
  489. {
  490. struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
  491. unsigned long address;
  492. int ret;
  493. if (!priv)
  494. return -ENODEV;
  495. ret = kstrtoul(buf, 0, &address);
  496. if (ret)
  497. return ret;
  498. ret = rsu_send_msg(priv, COMMAND_RSU_UPDATE,
  499. address, rsu_command_callback);
  500. if (ret) {
  501. dev_err(dev, "Error, RSU update returned %i\n", ret);
  502. return ret;
  503. }
  504. return count;
  505. }
  506. static ssize_t notify_store(struct device *dev,
  507. struct device_attribute *attr,
  508. const char *buf, size_t count)
  509. {
  510. struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
  511. unsigned long status;
  512. int ret;
  513. if (!priv)
  514. return -ENODEV;
  515. ret = kstrtoul(buf, 0, &status);
  516. if (ret)
  517. return ret;
  518. ret = rsu_send_async_msg(dev, priv, COMMAND_RSU_NOTIFY, status, NULL);
  519. if (ret) {
  520. dev_err(dev, "Error, RSU notify returned %i\n", ret);
  521. return ret;
  522. }
  523. /* to get the updated state */
  524. ret = rsu_send_async_msg(dev, priv, COMMAND_RSU_STATUS, 0,
  525. rsu_async_status_callback);
  526. if (ret) {
  527. dev_err(dev, "Error, getting RSU status %i\n", ret);
  528. return ret;
  529. }
  530. return count;
  531. }
  532. static ssize_t spt0_address_show(struct device *dev,
  533. struct device_attribute *attr, char *buf)
  534. {
  535. struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
  536. if (!priv)
  537. return -ENODEV;
  538. if (priv->spt0_address == INVALID_SPT_ADDRESS)
  539. return -EIO;
  540. return sysfs_emit(buf, "0x%08lx\n", priv->spt0_address);
  541. }
  542. static ssize_t spt1_address_show(struct device *dev,
  543. struct device_attribute *attr, char *buf)
  544. {
  545. struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
  546. if (!priv)
  547. return -ENODEV;
  548. if (priv->spt1_address == INVALID_SPT_ADDRESS)
  549. return -EIO;
  550. return sysfs_emit(buf, "0x%08lx\n", priv->spt1_address);
  551. }
  552. static DEVICE_ATTR_RO(current_image);
  553. static DEVICE_ATTR_RO(fail_image);
  554. static DEVICE_ATTR_RO(state);
  555. static DEVICE_ATTR_RO(version);
  556. static DEVICE_ATTR_RO(error_location);
  557. static DEVICE_ATTR_RO(error_details);
  558. static DEVICE_ATTR_RO(retry_counter);
  559. static DEVICE_ATTR_RO(max_retry);
  560. static DEVICE_ATTR_RO(dcmf0);
  561. static DEVICE_ATTR_RO(dcmf1);
  562. static DEVICE_ATTR_RO(dcmf2);
  563. static DEVICE_ATTR_RO(dcmf3);
  564. static DEVICE_ATTR_RO(dcmf0_status);
  565. static DEVICE_ATTR_RO(dcmf1_status);
  566. static DEVICE_ATTR_RO(dcmf2_status);
  567. static DEVICE_ATTR_RO(dcmf3_status);
  568. static DEVICE_ATTR_WO(reboot_image);
  569. static DEVICE_ATTR_WO(notify);
  570. static DEVICE_ATTR_RO(spt0_address);
  571. static DEVICE_ATTR_RO(spt1_address);
  572. static struct attribute *rsu_attrs[] = {
  573. &dev_attr_current_image.attr,
  574. &dev_attr_fail_image.attr,
  575. &dev_attr_state.attr,
  576. &dev_attr_version.attr,
  577. &dev_attr_error_location.attr,
  578. &dev_attr_error_details.attr,
  579. &dev_attr_retry_counter.attr,
  580. &dev_attr_max_retry.attr,
  581. &dev_attr_dcmf0.attr,
  582. &dev_attr_dcmf1.attr,
  583. &dev_attr_dcmf2.attr,
  584. &dev_attr_dcmf3.attr,
  585. &dev_attr_dcmf0_status.attr,
  586. &dev_attr_dcmf1_status.attr,
  587. &dev_attr_dcmf2_status.attr,
  588. &dev_attr_dcmf3_status.attr,
  589. &dev_attr_reboot_image.attr,
  590. &dev_attr_notify.attr,
  591. &dev_attr_spt0_address.attr,
  592. &dev_attr_spt1_address.attr,
  593. NULL
  594. };
  595. ATTRIBUTE_GROUPS(rsu);
  596. static int stratix10_rsu_probe(struct platform_device *pdev)
  597. {
  598. struct device *dev = &pdev->dev;
  599. struct stratix10_rsu_priv *priv;
  600. int ret;
  601. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  602. if (!priv)
  603. return -ENOMEM;
  604. priv->client.dev = dev;
  605. priv->client.receive_cb = NULL;
  606. priv->client.priv = priv;
  607. priv->status.current_image = 0;
  608. priv->status.fail_image = 0;
  609. priv->status.error_location = 0;
  610. priv->status.error_details = 0;
  611. priv->status.version = 0;
  612. priv->status.state = 0;
  613. priv->retry_counter = INVALID_RETRY_COUNTER;
  614. priv->dcmf_version.dcmf0 = INVALID_DCMF_VERSION;
  615. priv->dcmf_version.dcmf1 = INVALID_DCMF_VERSION;
  616. priv->dcmf_version.dcmf2 = INVALID_DCMF_VERSION;
  617. priv->dcmf_version.dcmf3 = INVALID_DCMF_VERSION;
  618. priv->dcmf_status.dcmf0 = INVALID_DCMF_STATUS;
  619. priv->dcmf_status.dcmf1 = INVALID_DCMF_STATUS;
  620. priv->dcmf_status.dcmf2 = INVALID_DCMF_STATUS;
  621. priv->dcmf_status.dcmf3 = INVALID_DCMF_STATUS;
  622. priv->max_retry = INVALID_RETRY_COUNTER;
  623. priv->spt0_address = INVALID_SPT_ADDRESS;
  624. priv->spt1_address = INVALID_SPT_ADDRESS;
  625. mutex_init(&priv->lock);
  626. priv->chan = stratix10_svc_request_channel_byname(&priv->client,
  627. SVC_CLIENT_RSU);
  628. if (IS_ERR(priv->chan)) {
  629. dev_err(dev, "couldn't get service channel %s\n",
  630. SVC_CLIENT_RSU);
  631. return PTR_ERR(priv->chan);
  632. }
  633. ret = stratix10_svc_add_async_client(priv->chan, false);
  634. if (ret) {
  635. dev_err(dev, "failed to add async client\n");
  636. stratix10_svc_free_channel(priv->chan);
  637. return ret;
  638. }
  639. init_completion(&priv->completion);
  640. platform_set_drvdata(pdev, priv);
  641. /* get the initial state from firmware */
  642. ret = rsu_send_async_msg(dev, priv, COMMAND_RSU_STATUS, 0,
  643. rsu_async_status_callback);
  644. if (ret) {
  645. dev_err(dev, "Error, getting RSU status %i\n", ret);
  646. stratix10_svc_remove_async_client(priv->chan);
  647. stratix10_svc_free_channel(priv->chan);
  648. return ret;
  649. }
  650. /* get DCMF version from firmware */
  651. ret = rsu_send_msg(priv, COMMAND_RSU_DCMF_VERSION,
  652. 0, rsu_dcmf_version_callback);
  653. if (ret) {
  654. dev_err(dev, "Error, getting DCMF version %i\n", ret);
  655. stratix10_svc_free_channel(priv->chan);
  656. }
  657. ret = rsu_send_msg(priv, COMMAND_RSU_DCMF_STATUS,
  658. 0, rsu_dcmf_status_callback);
  659. if (ret) {
  660. dev_err(dev, "Error, getting DCMF status %i\n", ret);
  661. stratix10_svc_free_channel(priv->chan);
  662. }
  663. ret = rsu_send_msg(priv, COMMAND_RSU_MAX_RETRY, 0,
  664. rsu_max_retry_callback);
  665. if (ret) {
  666. dev_err(dev, "Error, getting RSU max retry %i\n", ret);
  667. stratix10_svc_free_channel(priv->chan);
  668. }
  669. ret = rsu_send_async_msg(dev, priv, COMMAND_RSU_GET_SPT_TABLE, 0,
  670. rsu_async_get_spt_table_callback);
  671. if (ret) {
  672. dev_err(dev, "Error, getting SPT table %i\n", ret);
  673. stratix10_svc_free_channel(priv->chan);
  674. }
  675. return ret;
  676. }
  677. static void stratix10_rsu_remove(struct platform_device *pdev)
  678. {
  679. struct stratix10_rsu_priv *priv = platform_get_drvdata(pdev);
  680. stratix10_svc_free_channel(priv->chan);
  681. }
  682. static struct platform_driver stratix10_rsu_driver = {
  683. .probe = stratix10_rsu_probe,
  684. .remove = stratix10_rsu_remove,
  685. .driver = {
  686. .name = "stratix10-rsu",
  687. .dev_groups = rsu_groups,
  688. },
  689. };
  690. module_platform_driver(stratix10_rsu_driver);
  691. MODULE_LICENSE("GPL v2");
  692. MODULE_DESCRIPTION("Intel Remote System Update Driver");
  693. MODULE_AUTHOR("Richard Gong <richard.gong@intel.com>");