fsi-sbefifo.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) IBM Corporation 2017
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERGCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include <linux/device.h>
  15. #include <linux/errno.h>
  16. #include <linux/fs.h>
  17. #include <linux/fsi.h>
  18. #include <linux/fsi-sbefifo.h>
  19. #include <linux/kernel.h>
  20. #include <linux/cdev.h>
  21. #include <linux/module.h>
  22. #include <linux/mutex.h>
  23. #include <linux/of.h>
  24. #include <linux/of_device.h>
  25. #include <linux/of_platform.h>
  26. #include <linux/sched.h>
  27. #include <linux/slab.h>
  28. #include <linux/uaccess.h>
  29. #include <linux/delay.h>
  30. #include <linux/uio.h>
  31. #include <linux/vmalloc.h>
  32. #include <linux/mm.h>
  33. /*
  34. * The SBEFIFO is a pipe-like FSI device for communicating with
  35. * the self boot engine on POWER processors.
  36. */
  37. #define DEVICE_NAME "sbefifo"
  38. #define FSI_ENGID_SBE 0x22
  39. /*
  40. * Register layout
  41. */
  42. /* Register banks */
  43. #define SBEFIFO_UP 0x00 /* FSI -> Host */
  44. #define SBEFIFO_DOWN 0x40 /* Host -> FSI */
  45. /* Per-bank registers */
  46. #define SBEFIFO_FIFO 0x00 /* The FIFO itself */
  47. #define SBEFIFO_STS 0x04 /* Status register */
  48. #define SBEFIFO_STS_PARITY_ERR 0x20000000
  49. #define SBEFIFO_STS_RESET_REQ 0x02000000
  50. #define SBEFIFO_STS_GOT_EOT 0x00800000
  51. #define SBEFIFO_STS_MAX_XFER_LIMIT 0x00400000
  52. #define SBEFIFO_STS_FULL 0x00200000
  53. #define SBEFIFO_STS_EMPTY 0x00100000
  54. #define SBEFIFO_STS_ECNT_MASK 0x000f0000
  55. #define SBEFIFO_STS_ECNT_SHIFT 16
  56. #define SBEFIFO_STS_VALID_MASK 0x0000ff00
  57. #define SBEFIFO_STS_VALID_SHIFT 8
  58. #define SBEFIFO_STS_EOT_MASK 0x000000ff
  59. #define SBEFIFO_STS_EOT_SHIFT 0
  60. #define SBEFIFO_EOT_RAISE 0x08 /* (Up only) Set End Of Transfer */
  61. #define SBEFIFO_REQ_RESET 0x0C /* (Up only) Reset Request */
  62. #define SBEFIFO_PERFORM_RESET 0x10 /* (Down only) Perform Reset */
  63. #define SBEFIFO_EOT_ACK 0x14 /* (Down only) Acknowledge EOT */
  64. #define SBEFIFO_DOWN_MAX 0x18 /* (Down only) Max transfer */
  65. /* CFAM GP Mailbox SelfBoot Message register */
  66. #define CFAM_GP_MBOX_SBM_ADDR 0x2824 /* Converted 0x2809 */
  67. #define CFAM_SBM_SBE_BOOTED 0x80000000
  68. #define CFAM_SBM_SBE_ASYNC_FFDC 0x40000000
  69. #define CFAM_SBM_SBE_STATE_MASK 0x00f00000
  70. #define CFAM_SBM_SBE_STATE_SHIFT 20
  71. enum sbe_state
  72. {
  73. SBE_STATE_UNKNOWN = 0x0, // Unkown, initial state
  74. SBE_STATE_IPLING = 0x1, // IPL'ing - autonomous mode (transient)
  75. SBE_STATE_ISTEP = 0x2, // ISTEP - Running IPL by steps (transient)
  76. SBE_STATE_MPIPL = 0x3, // MPIPL
  77. SBE_STATE_RUNTIME = 0x4, // SBE Runtime
  78. SBE_STATE_DMT = 0x5, // Dead Man Timer State (transient)
  79. SBE_STATE_DUMP = 0x6, // Dumping
  80. SBE_STATE_FAILURE = 0x7, // Internal SBE failure
  81. SBE_STATE_QUIESCE = 0x8, // Final state - needs SBE reset to get out
  82. };
  83. /* FIFO depth */
  84. #define SBEFIFO_FIFO_DEPTH 8
  85. /* Helpers */
  86. #define sbefifo_empty(sts) ((sts) & SBEFIFO_STS_EMPTY)
  87. #define sbefifo_full(sts) ((sts) & SBEFIFO_STS_FULL)
  88. #define sbefifo_parity_err(sts) ((sts) & SBEFIFO_STS_PARITY_ERR)
  89. #define sbefifo_populated(sts) (((sts) & SBEFIFO_STS_ECNT_MASK) >> SBEFIFO_STS_ECNT_SHIFT)
  90. #define sbefifo_vacant(sts) (SBEFIFO_FIFO_DEPTH - sbefifo_populated(sts))
  91. #define sbefifo_eot_set(sts) (((sts) & SBEFIFO_STS_EOT_MASK) >> SBEFIFO_STS_EOT_SHIFT)
  92. /* Reset request timeout in ms */
  93. #define SBEFIFO_RESET_TIMEOUT 10000
  94. /* Timeouts for commands in ms */
  95. #define SBEFIFO_TIMEOUT_START_CMD 10000
  96. #define SBEFIFO_TIMEOUT_IN_CMD 1000
  97. #define SBEFIFO_TIMEOUT_START_RSP 10000
  98. #define SBEFIFO_TIMEOUT_IN_RSP 1000
  99. /* Other constants */
  100. #define SBEFIFO_MAX_USER_CMD_LEN (0x100000 + PAGE_SIZE)
  101. #define SBEFIFO_RESET_MAGIC 0x52534554 /* "RSET" */
  102. struct sbefifo {
  103. uint32_t magic;
  104. #define SBEFIFO_MAGIC 0x53424546 /* "SBEF" */
  105. struct fsi_device *fsi_dev;
  106. struct device dev;
  107. struct cdev cdev;
  108. struct mutex lock;
  109. bool broken;
  110. bool dead;
  111. bool async_ffdc;
  112. };
  113. struct sbefifo_user {
  114. struct sbefifo *sbefifo;
  115. struct mutex file_lock;
  116. void *cmd_page;
  117. void *pending_cmd;
  118. size_t pending_len;
  119. };
  120. static DEFINE_MUTEX(sbefifo_ffdc_mutex);
  121. static void __sbefifo_dump_ffdc(struct device *dev, const __be32 *ffdc,
  122. size_t ffdc_sz, bool internal)
  123. {
  124. int pack = 0;
  125. #define FFDC_LSIZE 60
  126. static char ffdc_line[FFDC_LSIZE];
  127. char *p = ffdc_line;
  128. while (ffdc_sz) {
  129. u32 w0, w1, w2, i;
  130. if (ffdc_sz < 3) {
  131. dev_err(dev, "SBE invalid FFDC package size %zd\n", ffdc_sz);
  132. return;
  133. }
  134. w0 = be32_to_cpu(*(ffdc++));
  135. w1 = be32_to_cpu(*(ffdc++));
  136. w2 = be32_to_cpu(*(ffdc++));
  137. ffdc_sz -= 3;
  138. if ((w0 >> 16) != 0xFFDC) {
  139. dev_err(dev, "SBE invalid FFDC package signature %08x %08x %08x\n",
  140. w0, w1, w2);
  141. break;
  142. }
  143. w0 &= 0xffff;
  144. if (w0 > ffdc_sz) {
  145. dev_err(dev, "SBE FFDC package len %d words but only %zd remaining\n",
  146. w0, ffdc_sz);
  147. w0 = ffdc_sz;
  148. break;
  149. }
  150. if (internal) {
  151. dev_warn(dev, "+---- SBE FFDC package %d for async err -----+\n",
  152. pack++);
  153. } else {
  154. dev_warn(dev, "+---- SBE FFDC package %d for cmd %02x:%02x -----+\n",
  155. pack++, (w1 >> 8) & 0xff, w1 & 0xff);
  156. }
  157. dev_warn(dev, "| Response code: %08x |\n", w2);
  158. dev_warn(dev, "|-------------------------------------------|\n");
  159. for (i = 0; i < w0; i++) {
  160. if ((i & 3) == 0) {
  161. p = ffdc_line;
  162. p += sprintf(p, "| %04x:", i << 4);
  163. }
  164. p += sprintf(p, " %08x", be32_to_cpu(*(ffdc++)));
  165. ffdc_sz--;
  166. if ((i & 3) == 3 || i == (w0 - 1)) {
  167. while ((i & 3) < 3) {
  168. p += sprintf(p, " ");
  169. i++;
  170. }
  171. dev_warn(dev, "%s |\n", ffdc_line);
  172. }
  173. }
  174. dev_warn(dev, "+-------------------------------------------+\n");
  175. }
  176. }
  177. static void sbefifo_dump_ffdc(struct device *dev, const __be32 *ffdc,
  178. size_t ffdc_sz, bool internal)
  179. {
  180. mutex_lock(&sbefifo_ffdc_mutex);
  181. __sbefifo_dump_ffdc(dev, ffdc, ffdc_sz, internal);
  182. mutex_unlock(&sbefifo_ffdc_mutex);
  183. }
  184. int sbefifo_parse_status(struct device *dev, u16 cmd, __be32 *response,
  185. size_t resp_len, size_t *data_len)
  186. {
  187. u32 dh, s0, s1;
  188. size_t ffdc_sz;
  189. if (resp_len < 3) {
  190. pr_debug("sbefifo: cmd %04x, response too small: %zd\n",
  191. cmd, resp_len);
  192. return -ENXIO;
  193. }
  194. dh = be32_to_cpu(response[resp_len - 1]);
  195. if (dh > resp_len || dh < 3) {
  196. dev_err(dev, "SBE cmd %02x:%02x status offset out of range: %d/%zd\n",
  197. cmd >> 8, cmd & 0xff, dh, resp_len);
  198. return -ENXIO;
  199. }
  200. s0 = be32_to_cpu(response[resp_len - dh]);
  201. s1 = be32_to_cpu(response[resp_len - dh + 1]);
  202. if (((s0 >> 16) != 0xC0DE) || ((s0 & 0xffff) != cmd)) {
  203. dev_err(dev, "SBE cmd %02x:%02x, status signature invalid: 0x%08x 0x%08x\n",
  204. cmd >> 8, cmd & 0xff, s0, s1);
  205. return -ENXIO;
  206. }
  207. if (s1 != 0) {
  208. ffdc_sz = dh - 3;
  209. dev_warn(dev, "SBE error cmd %02x:%02x status=%04x:%04x\n",
  210. cmd >> 8, cmd & 0xff, s1 >> 16, s1 & 0xffff);
  211. if (ffdc_sz)
  212. sbefifo_dump_ffdc(dev, &response[resp_len - dh + 2],
  213. ffdc_sz, false);
  214. }
  215. if (data_len)
  216. *data_len = resp_len - dh;
  217. /*
  218. * Primary status don't have the top bit set, so can't be confused with
  219. * Linux negative error codes, so return the status word whole.
  220. */
  221. return s1;
  222. }
  223. EXPORT_SYMBOL_GPL(sbefifo_parse_status);
  224. static int sbefifo_regr(struct sbefifo *sbefifo, int reg, u32 *word)
  225. {
  226. __be32 raw_word;
  227. int rc;
  228. rc = fsi_device_read(sbefifo->fsi_dev, reg, &raw_word,
  229. sizeof(raw_word));
  230. if (rc)
  231. return rc;
  232. *word = be32_to_cpu(raw_word);
  233. return 0;
  234. }
  235. static int sbefifo_regw(struct sbefifo *sbefifo, int reg, u32 word)
  236. {
  237. __be32 raw_word = cpu_to_be32(word);
  238. return fsi_device_write(sbefifo->fsi_dev, reg, &raw_word,
  239. sizeof(raw_word));
  240. }
  241. static int sbefifo_check_sbe_state(struct sbefifo *sbefifo)
  242. {
  243. __be32 raw_word;
  244. u32 sbm;
  245. int rc;
  246. rc = fsi_slave_read(sbefifo->fsi_dev->slave, CFAM_GP_MBOX_SBM_ADDR,
  247. &raw_word, sizeof(raw_word));
  248. if (rc)
  249. return rc;
  250. sbm = be32_to_cpu(raw_word);
  251. /* SBE booted at all ? */
  252. if (!(sbm & CFAM_SBM_SBE_BOOTED))
  253. return -ESHUTDOWN;
  254. /* Check its state */
  255. switch ((sbm & CFAM_SBM_SBE_STATE_MASK) >> CFAM_SBM_SBE_STATE_SHIFT) {
  256. case SBE_STATE_UNKNOWN:
  257. return -ESHUTDOWN;
  258. case SBE_STATE_DMT:
  259. return -EBUSY;
  260. case SBE_STATE_IPLING:
  261. case SBE_STATE_ISTEP:
  262. case SBE_STATE_MPIPL:
  263. case SBE_STATE_RUNTIME:
  264. case SBE_STATE_DUMP: /* Not sure about that one */
  265. break;
  266. case SBE_STATE_FAILURE:
  267. case SBE_STATE_QUIESCE:
  268. return -ESHUTDOWN;
  269. }
  270. /* Is there async FFDC available ? Remember it */
  271. if (sbm & CFAM_SBM_SBE_ASYNC_FFDC)
  272. sbefifo->async_ffdc = true;
  273. return 0;
  274. }
  275. /* Don't flip endianness of data to/from FIFO, just pass through. */
  276. static int sbefifo_down_read(struct sbefifo *sbefifo, __be32 *word)
  277. {
  278. return fsi_device_read(sbefifo->fsi_dev, SBEFIFO_DOWN, word,
  279. sizeof(*word));
  280. }
  281. static int sbefifo_up_write(struct sbefifo *sbefifo, __be32 word)
  282. {
  283. return fsi_device_write(sbefifo->fsi_dev, SBEFIFO_UP, &word,
  284. sizeof(word));
  285. }
  286. static int sbefifo_request_reset(struct sbefifo *sbefifo)
  287. {
  288. struct device *dev = &sbefifo->fsi_dev->dev;
  289. unsigned long end_time;
  290. u32 status;
  291. int rc;
  292. dev_dbg(dev, "Requesting FIFO reset\n");
  293. /* Mark broken first, will be cleared if reset succeeds */
  294. sbefifo->broken = true;
  295. /* Send reset request */
  296. rc = sbefifo_regw(sbefifo, SBEFIFO_UP | SBEFIFO_REQ_RESET, 1);
  297. if (rc) {
  298. dev_err(dev, "Sending reset request failed, rc=%d\n", rc);
  299. return rc;
  300. }
  301. /* Wait for it to complete */
  302. end_time = jiffies + msecs_to_jiffies(SBEFIFO_RESET_TIMEOUT);
  303. while (!time_after(jiffies, end_time)) {
  304. rc = sbefifo_regr(sbefifo, SBEFIFO_UP | SBEFIFO_STS, &status);
  305. if (rc) {
  306. dev_err(dev, "Failed to read UP fifo status during reset"
  307. " , rc=%d\n", rc);
  308. return rc;
  309. }
  310. if (!(status & SBEFIFO_STS_RESET_REQ)) {
  311. dev_dbg(dev, "FIFO reset done\n");
  312. sbefifo->broken = false;
  313. return 0;
  314. }
  315. cond_resched();
  316. }
  317. dev_err(dev, "FIFO reset timed out\n");
  318. return -ETIMEDOUT;
  319. }
  320. static int sbefifo_cleanup_hw(struct sbefifo *sbefifo)
  321. {
  322. struct device *dev = &sbefifo->fsi_dev->dev;
  323. u32 up_status, down_status;
  324. bool need_reset = false;
  325. int rc;
  326. rc = sbefifo_check_sbe_state(sbefifo);
  327. if (rc) {
  328. dev_dbg(dev, "SBE state=%d\n", rc);
  329. return rc;
  330. }
  331. /* If broken, we don't need to look at status, go straight to reset */
  332. if (sbefifo->broken)
  333. goto do_reset;
  334. rc = sbefifo_regr(sbefifo, SBEFIFO_UP | SBEFIFO_STS, &up_status);
  335. if (rc) {
  336. dev_err(dev, "Cleanup: Reading UP status failed, rc=%d\n", rc);
  337. /* Will try reset again on next attempt at using it */
  338. sbefifo->broken = true;
  339. return rc;
  340. }
  341. rc = sbefifo_regr(sbefifo, SBEFIFO_DOWN | SBEFIFO_STS, &down_status);
  342. if (rc) {
  343. dev_err(dev, "Cleanup: Reading DOWN status failed, rc=%d\n", rc);
  344. /* Will try reset again on next attempt at using it */
  345. sbefifo->broken = true;
  346. return rc;
  347. }
  348. /* The FIFO already contains a reset request from the SBE ? */
  349. if (down_status & SBEFIFO_STS_RESET_REQ) {
  350. dev_info(dev, "Cleanup: FIFO reset request set, resetting\n");
  351. rc = sbefifo_regw(sbefifo, SBEFIFO_DOWN, SBEFIFO_PERFORM_RESET);
  352. if (rc) {
  353. sbefifo->broken = true;
  354. dev_err(dev, "Cleanup: Reset reg write failed, rc=%d\n", rc);
  355. return rc;
  356. }
  357. sbefifo->broken = false;
  358. return 0;
  359. }
  360. /* Parity error on either FIFO ? */
  361. if ((up_status | down_status) & SBEFIFO_STS_PARITY_ERR)
  362. need_reset = true;
  363. /* Either FIFO not empty ? */
  364. if (!((up_status & down_status) & SBEFIFO_STS_EMPTY))
  365. need_reset = true;
  366. if (!need_reset)
  367. return 0;
  368. dev_info(dev, "Cleanup: FIFO not clean (up=0x%08x down=0x%08x)\n",
  369. up_status, down_status);
  370. do_reset:
  371. /* Mark broken, will be cleared if/when reset succeeds */
  372. return sbefifo_request_reset(sbefifo);
  373. }
  374. static int sbefifo_wait(struct sbefifo *sbefifo, bool up,
  375. u32 *status, unsigned long timeout)
  376. {
  377. struct device *dev = &sbefifo->fsi_dev->dev;
  378. unsigned long end_time;
  379. bool ready = false;
  380. u32 addr, sts = 0;
  381. int rc;
  382. dev_vdbg(dev, "Wait on %s fifo...\n", up ? "up" : "down");
  383. addr = (up ? SBEFIFO_UP : SBEFIFO_DOWN) | SBEFIFO_STS;
  384. end_time = jiffies + timeout;
  385. while (!time_after(jiffies, end_time)) {
  386. cond_resched();
  387. rc = sbefifo_regr(sbefifo, addr, &sts);
  388. if (rc < 0) {
  389. dev_err(dev, "FSI error %d reading status register\n", rc);
  390. return rc;
  391. }
  392. if (!up && sbefifo_parity_err(sts)) {
  393. dev_err(dev, "Parity error in DOWN FIFO\n");
  394. return -ENXIO;
  395. }
  396. ready = !(up ? sbefifo_full(sts) : sbefifo_empty(sts));
  397. if (ready)
  398. break;
  399. }
  400. if (!ready) {
  401. dev_err(dev, "%s FIFO Timeout ! status=%08x\n", up ? "UP" : "DOWN", sts);
  402. return -ETIMEDOUT;
  403. }
  404. dev_vdbg(dev, "End of wait status: %08x\n", sts);
  405. *status = sts;
  406. return 0;
  407. }
  408. static int sbefifo_send_command(struct sbefifo *sbefifo,
  409. const __be32 *command, size_t cmd_len)
  410. {
  411. struct device *dev = &sbefifo->fsi_dev->dev;
  412. size_t len, chunk, vacant = 0, remaining = cmd_len;
  413. unsigned long timeout;
  414. u32 status;
  415. int rc;
  416. dev_vdbg(dev, "sending command (%zd words, cmd=%04x)\n",
  417. cmd_len, be32_to_cpu(command[1]));
  418. /* As long as there's something to send */
  419. timeout = msecs_to_jiffies(SBEFIFO_TIMEOUT_START_CMD);
  420. while (remaining) {
  421. /* Wait for room in the FIFO */
  422. rc = sbefifo_wait(sbefifo, true, &status, timeout);
  423. if (rc < 0)
  424. return rc;
  425. timeout = msecs_to_jiffies(SBEFIFO_TIMEOUT_IN_CMD);
  426. vacant = sbefifo_vacant(status);
  427. len = chunk = min(vacant, remaining);
  428. dev_vdbg(dev, " status=%08x vacant=%zd chunk=%zd\n",
  429. status, vacant, chunk);
  430. /* Write as much as we can */
  431. while (len--) {
  432. rc = sbefifo_up_write(sbefifo, *(command++));
  433. if (rc) {
  434. dev_err(dev, "FSI error %d writing UP FIFO\n", rc);
  435. return rc;
  436. }
  437. }
  438. remaining -= chunk;
  439. vacant -= chunk;
  440. }
  441. /* If there's no room left, wait for some to write EOT */
  442. if (!vacant) {
  443. rc = sbefifo_wait(sbefifo, true, &status, timeout);
  444. if (rc)
  445. return rc;
  446. }
  447. /* Send an EOT */
  448. rc = sbefifo_regw(sbefifo, SBEFIFO_UP | SBEFIFO_EOT_RAISE, 0);
  449. if (rc)
  450. dev_err(dev, "FSI error %d writing EOT\n", rc);
  451. return rc;
  452. }
  453. static int sbefifo_read_response(struct sbefifo *sbefifo, struct iov_iter *response)
  454. {
  455. struct device *dev = &sbefifo->fsi_dev->dev;
  456. u32 status, eot_set;
  457. unsigned long timeout;
  458. bool overflow = false;
  459. __be32 data;
  460. size_t len;
  461. int rc;
  462. dev_vdbg(dev, "reading response, buflen = %zd\n", iov_iter_count(response));
  463. timeout = msecs_to_jiffies(SBEFIFO_TIMEOUT_START_RSP);
  464. for (;;) {
  465. /* Grab FIFO status (this will handle parity errors) */
  466. rc = sbefifo_wait(sbefifo, false, &status, timeout);
  467. if (rc < 0)
  468. return rc;
  469. timeout = msecs_to_jiffies(SBEFIFO_TIMEOUT_IN_RSP);
  470. /* Decode status */
  471. len = sbefifo_populated(status);
  472. eot_set = sbefifo_eot_set(status);
  473. dev_vdbg(dev, " chunk size %zd eot_set=0x%x\n", len, eot_set);
  474. /* Go through the chunk */
  475. while(len--) {
  476. /* Read the data */
  477. rc = sbefifo_down_read(sbefifo, &data);
  478. if (rc < 0)
  479. return rc;
  480. /* Was it an EOT ? */
  481. if (eot_set & 0x80) {
  482. /*
  483. * There should be nothing else in the FIFO,
  484. * if there is, mark broken, this will force
  485. * a reset on next use, but don't fail the
  486. * command.
  487. */
  488. if (len) {
  489. dev_warn(dev, "FIFO read hit"
  490. " EOT with still %zd data\n",
  491. len);
  492. sbefifo->broken = true;
  493. }
  494. /* We are done */
  495. rc = sbefifo_regw(sbefifo,
  496. SBEFIFO_DOWN | SBEFIFO_EOT_ACK, 0);
  497. /*
  498. * If that write fail, still complete the request but mark
  499. * the fifo as broken for subsequent reset (not much else
  500. * we can do here).
  501. */
  502. if (rc) {
  503. dev_err(dev, "FSI error %d ack'ing EOT\n", rc);
  504. sbefifo->broken = true;
  505. }
  506. /* Tell whether we overflowed */
  507. return overflow ? -EOVERFLOW : 0;
  508. }
  509. /* Store it if there is room */
  510. if (iov_iter_count(response) >= sizeof(__be32)) {
  511. if (copy_to_iter(&data, sizeof(__be32), response) < sizeof(__be32))
  512. return -EFAULT;
  513. } else {
  514. dev_vdbg(dev, "Response overflowed !\n");
  515. overflow = true;
  516. }
  517. /* Next EOT bit */
  518. eot_set <<= 1;
  519. }
  520. }
  521. /* Shouldn't happen */
  522. return -EIO;
  523. }
  524. static int sbefifo_do_command(struct sbefifo *sbefifo,
  525. const __be32 *command, size_t cmd_len,
  526. struct iov_iter *response)
  527. {
  528. /* Try sending the command */
  529. int rc = sbefifo_send_command(sbefifo, command, cmd_len);
  530. if (rc)
  531. return rc;
  532. /* Now, get the response */
  533. return sbefifo_read_response(sbefifo, response);
  534. }
  535. static void sbefifo_collect_async_ffdc(struct sbefifo *sbefifo)
  536. {
  537. struct device *dev = &sbefifo->fsi_dev->dev;
  538. struct iov_iter ffdc_iter;
  539. struct kvec ffdc_iov;
  540. __be32 *ffdc;
  541. size_t ffdc_sz;
  542. __be32 cmd[2];
  543. int rc;
  544. sbefifo->async_ffdc = false;
  545. ffdc = vmalloc(SBEFIFO_MAX_FFDC_SIZE);
  546. if (!ffdc) {
  547. dev_err(dev, "Failed to allocate SBE FFDC buffer\n");
  548. return;
  549. }
  550. ffdc_iov.iov_base = ffdc;
  551. ffdc_iov.iov_len = SBEFIFO_MAX_FFDC_SIZE;
  552. iov_iter_kvec(&ffdc_iter, WRITE, &ffdc_iov, 1, SBEFIFO_MAX_FFDC_SIZE);
  553. cmd[0] = cpu_to_be32(2);
  554. cmd[1] = cpu_to_be32(SBEFIFO_CMD_GET_SBE_FFDC);
  555. rc = sbefifo_do_command(sbefifo, cmd, 2, &ffdc_iter);
  556. if (rc != 0) {
  557. dev_err(dev, "Error %d retrieving SBE FFDC\n", rc);
  558. goto bail;
  559. }
  560. ffdc_sz = SBEFIFO_MAX_FFDC_SIZE - iov_iter_count(&ffdc_iter);
  561. ffdc_sz /= sizeof(__be32);
  562. rc = sbefifo_parse_status(dev, SBEFIFO_CMD_GET_SBE_FFDC, ffdc,
  563. ffdc_sz, &ffdc_sz);
  564. if (rc != 0) {
  565. dev_err(dev, "Error %d decoding SBE FFDC\n", rc);
  566. goto bail;
  567. }
  568. if (ffdc_sz > 0)
  569. sbefifo_dump_ffdc(dev, ffdc, ffdc_sz, true);
  570. bail:
  571. vfree(ffdc);
  572. }
  573. static int __sbefifo_submit(struct sbefifo *sbefifo,
  574. const __be32 *command, size_t cmd_len,
  575. struct iov_iter *response)
  576. {
  577. struct device *dev = &sbefifo->fsi_dev->dev;
  578. int rc;
  579. if (sbefifo->dead)
  580. return -ENODEV;
  581. if (cmd_len < 2 || be32_to_cpu(command[0]) != cmd_len) {
  582. dev_vdbg(dev, "Invalid command len %zd (header: %d)\n",
  583. cmd_len, be32_to_cpu(command[0]));
  584. return -EINVAL;
  585. }
  586. /* First ensure the HW is in a clean state */
  587. rc = sbefifo_cleanup_hw(sbefifo);
  588. if (rc)
  589. return rc;
  590. /* Look for async FFDC first if any */
  591. if (sbefifo->async_ffdc)
  592. sbefifo_collect_async_ffdc(sbefifo);
  593. rc = sbefifo_do_command(sbefifo, command, cmd_len, response);
  594. if (rc != 0 && rc != -EOVERFLOW)
  595. goto fail;
  596. return rc;
  597. fail:
  598. /*
  599. * On failure, attempt a reset. Ignore the result, it will mark
  600. * the fifo broken if the reset fails
  601. */
  602. sbefifo_request_reset(sbefifo);
  603. /* Return original error */
  604. return rc;
  605. }
  606. /**
  607. * sbefifo_submit() - Submit and SBE fifo command and receive response
  608. * @dev: The sbefifo device
  609. * @command: The raw command data
  610. * @cmd_len: The command size (in 32-bit words)
  611. * @response: The output response buffer
  612. * @resp_len: In: Response buffer size, Out: Response size
  613. *
  614. * This will perform the entire operation. If the reponse buffer
  615. * overflows, returns -EOVERFLOW
  616. */
  617. int sbefifo_submit(struct device *dev, const __be32 *command, size_t cmd_len,
  618. __be32 *response, size_t *resp_len)
  619. {
  620. struct sbefifo *sbefifo;
  621. struct iov_iter resp_iter;
  622. struct kvec resp_iov;
  623. size_t rbytes;
  624. int rc;
  625. if (!dev)
  626. return -ENODEV;
  627. sbefifo = dev_get_drvdata(dev);
  628. if (!sbefifo)
  629. return -ENODEV;
  630. if (WARN_ON_ONCE(sbefifo->magic != SBEFIFO_MAGIC))
  631. return -ENODEV;
  632. if (!resp_len || !command || !response)
  633. return -EINVAL;
  634. /* Prepare iov iterator */
  635. rbytes = (*resp_len) * sizeof(__be32);
  636. resp_iov.iov_base = response;
  637. resp_iov.iov_len = rbytes;
  638. iov_iter_kvec(&resp_iter, WRITE, &resp_iov, 1, rbytes);
  639. /* Perform the command */
  640. mutex_lock(&sbefifo->lock);
  641. rc = __sbefifo_submit(sbefifo, command, cmd_len, &resp_iter);
  642. mutex_unlock(&sbefifo->lock);
  643. /* Extract the response length */
  644. rbytes -= iov_iter_count(&resp_iter);
  645. *resp_len = rbytes / sizeof(__be32);
  646. return rc;
  647. }
  648. EXPORT_SYMBOL_GPL(sbefifo_submit);
  649. /*
  650. * Char device interface
  651. */
  652. static void sbefifo_release_command(struct sbefifo_user *user)
  653. {
  654. if (is_vmalloc_addr(user->pending_cmd))
  655. vfree(user->pending_cmd);
  656. user->pending_cmd = NULL;
  657. user->pending_len = 0;
  658. }
  659. static int sbefifo_user_open(struct inode *inode, struct file *file)
  660. {
  661. struct sbefifo *sbefifo = container_of(inode->i_cdev, struct sbefifo, cdev);
  662. struct sbefifo_user *user;
  663. user = kzalloc(sizeof(struct sbefifo_user), GFP_KERNEL);
  664. if (!user)
  665. return -ENOMEM;
  666. file->private_data = user;
  667. user->sbefifo = sbefifo;
  668. user->cmd_page = (void *)__get_free_page(GFP_KERNEL);
  669. if (!user->cmd_page) {
  670. kfree(user);
  671. return -ENOMEM;
  672. }
  673. mutex_init(&user->file_lock);
  674. return 0;
  675. }
  676. static ssize_t sbefifo_user_read(struct file *file, char __user *buf,
  677. size_t len, loff_t *offset)
  678. {
  679. struct sbefifo_user *user = file->private_data;
  680. struct sbefifo *sbefifo;
  681. struct iov_iter resp_iter;
  682. struct iovec resp_iov;
  683. size_t cmd_len;
  684. int rc;
  685. if (!user)
  686. return -EINVAL;
  687. sbefifo = user->sbefifo;
  688. if (len & 3)
  689. return -EINVAL;
  690. mutex_lock(&user->file_lock);
  691. /* Cronus relies on -EAGAIN after a short read */
  692. if (user->pending_len == 0) {
  693. rc = -EAGAIN;
  694. goto bail;
  695. }
  696. if (user->pending_len < 8) {
  697. rc = -EINVAL;
  698. goto bail;
  699. }
  700. cmd_len = user->pending_len >> 2;
  701. /* Prepare iov iterator */
  702. resp_iov.iov_base = buf;
  703. resp_iov.iov_len = len;
  704. iov_iter_init(&resp_iter, WRITE, &resp_iov, 1, len);
  705. /* Perform the command */
  706. mutex_lock(&sbefifo->lock);
  707. rc = __sbefifo_submit(sbefifo, user->pending_cmd, cmd_len, &resp_iter);
  708. mutex_unlock(&sbefifo->lock);
  709. if (rc < 0)
  710. goto bail;
  711. /* Extract the response length */
  712. rc = len - iov_iter_count(&resp_iter);
  713. bail:
  714. sbefifo_release_command(user);
  715. mutex_unlock(&user->file_lock);
  716. return rc;
  717. }
  718. static ssize_t sbefifo_user_write(struct file *file, const char __user *buf,
  719. size_t len, loff_t *offset)
  720. {
  721. struct sbefifo_user *user = file->private_data;
  722. struct sbefifo *sbefifo;
  723. int rc = len;
  724. if (!user)
  725. return -EINVAL;
  726. sbefifo = user->sbefifo;
  727. if (len > SBEFIFO_MAX_USER_CMD_LEN)
  728. return -EINVAL;
  729. if (len & 3)
  730. return -EINVAL;
  731. mutex_lock(&user->file_lock);
  732. /* Can we use the pre-allocate buffer ? If not, allocate */
  733. if (len <= PAGE_SIZE)
  734. user->pending_cmd = user->cmd_page;
  735. else
  736. user->pending_cmd = vmalloc(len);
  737. if (!user->pending_cmd) {
  738. rc = -ENOMEM;
  739. goto bail;
  740. }
  741. /* Copy the command into the staging buffer */
  742. if (copy_from_user(user->pending_cmd, buf, len)) {
  743. rc = -EFAULT;
  744. goto bail;
  745. }
  746. /* Check for the magic reset command */
  747. if (len == 4 && be32_to_cpu(*(__be32 *)user->pending_cmd) ==
  748. SBEFIFO_RESET_MAGIC) {
  749. /* Clear out any pending command */
  750. user->pending_len = 0;
  751. /* Trigger reset request */
  752. mutex_lock(&sbefifo->lock);
  753. rc = sbefifo_request_reset(user->sbefifo);
  754. mutex_unlock(&sbefifo->lock);
  755. if (rc == 0)
  756. rc = 4;
  757. goto bail;
  758. }
  759. /* Update the staging buffer size */
  760. user->pending_len = len;
  761. bail:
  762. if (!user->pending_len)
  763. sbefifo_release_command(user);
  764. mutex_unlock(&user->file_lock);
  765. /* And that's it, we'll issue the command on a read */
  766. return rc;
  767. }
  768. static int sbefifo_user_release(struct inode *inode, struct file *file)
  769. {
  770. struct sbefifo_user *user = file->private_data;
  771. if (!user)
  772. return -EINVAL;
  773. sbefifo_release_command(user);
  774. free_page((unsigned long)user->cmd_page);
  775. kfree(user);
  776. return 0;
  777. }
  778. static const struct file_operations sbefifo_fops = {
  779. .owner = THIS_MODULE,
  780. .open = sbefifo_user_open,
  781. .read = sbefifo_user_read,
  782. .write = sbefifo_user_write,
  783. .release = sbefifo_user_release,
  784. };
  785. static void sbefifo_free(struct device *dev)
  786. {
  787. struct sbefifo *sbefifo = container_of(dev, struct sbefifo, dev);
  788. put_device(&sbefifo->fsi_dev->dev);
  789. kfree(sbefifo);
  790. }
  791. /*
  792. * Probe/remove
  793. */
  794. static int sbefifo_probe(struct device *dev)
  795. {
  796. struct fsi_device *fsi_dev = to_fsi_dev(dev);
  797. struct sbefifo *sbefifo;
  798. struct device_node *np;
  799. struct platform_device *child;
  800. char child_name[32];
  801. int rc, didx, child_idx = 0;
  802. dev_dbg(dev, "Found sbefifo device\n");
  803. sbefifo = kzalloc(sizeof(*sbefifo), GFP_KERNEL);
  804. if (!sbefifo)
  805. return -ENOMEM;
  806. /* Grab a reference to the device (parent of our cdev), we'll drop it later */
  807. if (!get_device(dev)) {
  808. kfree(sbefifo);
  809. return -ENODEV;
  810. }
  811. sbefifo->magic = SBEFIFO_MAGIC;
  812. sbefifo->fsi_dev = fsi_dev;
  813. dev_set_drvdata(dev, sbefifo);
  814. mutex_init(&sbefifo->lock);
  815. /*
  816. * Try cleaning up the FIFO. If this fails, we still register the
  817. * driver and will try cleaning things up again on the next access.
  818. */
  819. rc = sbefifo_cleanup_hw(sbefifo);
  820. if (rc && rc != -ESHUTDOWN)
  821. dev_err(dev, "Initial HW cleanup failed, will retry later\n");
  822. /* Create chardev for userspace access */
  823. sbefifo->dev.type = &fsi_cdev_type;
  824. sbefifo->dev.parent = dev;
  825. sbefifo->dev.release = sbefifo_free;
  826. device_initialize(&sbefifo->dev);
  827. /* Allocate a minor in the FSI space */
  828. rc = fsi_get_new_minor(fsi_dev, fsi_dev_sbefifo, &sbefifo->dev.devt, &didx);
  829. if (rc)
  830. goto err;
  831. dev_set_name(&sbefifo->dev, "sbefifo%d", didx);
  832. cdev_init(&sbefifo->cdev, &sbefifo_fops);
  833. rc = cdev_device_add(&sbefifo->cdev, &sbefifo->dev);
  834. if (rc) {
  835. dev_err(dev, "Error %d creating char device %s\n",
  836. rc, dev_name(&sbefifo->dev));
  837. goto err_free_minor;
  838. }
  839. /* Create platform devs for dts child nodes (occ, etc) */
  840. for_each_available_child_of_node(dev->of_node, np) {
  841. snprintf(child_name, sizeof(child_name), "%s-dev%d",
  842. dev_name(&sbefifo->dev), child_idx++);
  843. child = of_platform_device_create(np, child_name, dev);
  844. if (!child)
  845. dev_warn(dev, "failed to create child %s dev\n",
  846. child_name);
  847. }
  848. return 0;
  849. err_free_minor:
  850. fsi_free_minor(sbefifo->dev.devt);
  851. err:
  852. put_device(&sbefifo->dev);
  853. return rc;
  854. }
  855. static int sbefifo_unregister_child(struct device *dev, void *data)
  856. {
  857. struct platform_device *child = to_platform_device(dev);
  858. of_device_unregister(child);
  859. if (dev->of_node)
  860. of_node_clear_flag(dev->of_node, OF_POPULATED);
  861. return 0;
  862. }
  863. static int sbefifo_remove(struct device *dev)
  864. {
  865. struct sbefifo *sbefifo = dev_get_drvdata(dev);
  866. dev_dbg(dev, "Removing sbefifo device...\n");
  867. mutex_lock(&sbefifo->lock);
  868. sbefifo->dead = true;
  869. mutex_unlock(&sbefifo->lock);
  870. cdev_device_del(&sbefifo->cdev, &sbefifo->dev);
  871. fsi_free_minor(sbefifo->dev.devt);
  872. device_for_each_child(dev, NULL, sbefifo_unregister_child);
  873. put_device(&sbefifo->dev);
  874. return 0;
  875. }
  876. static const struct fsi_device_id sbefifo_ids[] = {
  877. {
  878. .engine_type = FSI_ENGID_SBE,
  879. .version = FSI_VERSION_ANY,
  880. },
  881. { 0 }
  882. };
  883. static struct fsi_driver sbefifo_drv = {
  884. .id_table = sbefifo_ids,
  885. .drv = {
  886. .name = DEVICE_NAME,
  887. .bus = &fsi_bus_type,
  888. .probe = sbefifo_probe,
  889. .remove = sbefifo_remove,
  890. }
  891. };
  892. static int sbefifo_init(void)
  893. {
  894. return fsi_driver_register(&sbefifo_drv);
  895. }
  896. static void sbefifo_exit(void)
  897. {
  898. fsi_driver_unregister(&sbefifo_drv);
  899. }
  900. module_init(sbefifo_init);
  901. module_exit(sbefifo_exit);
  902. MODULE_LICENSE("GPL");
  903. MODULE_AUTHOR("Brad Bishop <bradleyb@fuzziesquirrel.com>");
  904. MODULE_AUTHOR("Eddie James <eajames@linux.vnet.ibm.com>");
  905. MODULE_AUTHOR("Andrew Jeffery <andrew@aj.id.au>");
  906. MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
  907. MODULE_DESCRIPTION("Linux device interface to the POWER Self Boot Engine");