fsi-sbefifo.c 29 KB

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