fsi-sbefifo.c 29 KB

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