fsi-sbefifo.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067
  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. u32 status, timeout;
  290. int rc;
  291. dev_dbg(dev, "Requesting FIFO reset\n");
  292. /* Mark broken first, will be cleared if reset succeeds */
  293. sbefifo->broken = true;
  294. /* Send reset request */
  295. rc = sbefifo_regw(sbefifo, SBEFIFO_UP | SBEFIFO_REQ_RESET, 1);
  296. if (rc) {
  297. dev_err(dev, "Sending reset request failed, rc=%d\n", rc);
  298. return rc;
  299. }
  300. /* Wait for it to complete */
  301. for (timeout = 0; timeout < SBEFIFO_RESET_TIMEOUT; timeout++) {
  302. rc = sbefifo_regr(sbefifo, SBEFIFO_UP | SBEFIFO_STS, &status);
  303. if (rc) {
  304. dev_err(dev, "Failed to read UP fifo status during reset"
  305. " , rc=%d\n", rc);
  306. return rc;
  307. }
  308. if (!(status & SBEFIFO_STS_RESET_REQ)) {
  309. dev_dbg(dev, "FIFO reset done\n");
  310. sbefifo->broken = false;
  311. return 0;
  312. }
  313. msleep(1);
  314. }
  315. dev_err(dev, "FIFO reset timed out\n");
  316. return -ETIMEDOUT;
  317. }
  318. static int sbefifo_cleanup_hw(struct sbefifo *sbefifo)
  319. {
  320. struct device *dev = &sbefifo->fsi_dev->dev;
  321. u32 up_status, down_status;
  322. bool need_reset = false;
  323. int rc;
  324. rc = sbefifo_check_sbe_state(sbefifo);
  325. if (rc) {
  326. dev_dbg(dev, "SBE state=%d\n", rc);
  327. return rc;
  328. }
  329. /* If broken, we don't need to look at status, go straight to reset */
  330. if (sbefifo->broken)
  331. goto do_reset;
  332. rc = sbefifo_regr(sbefifo, SBEFIFO_UP | SBEFIFO_STS, &up_status);
  333. if (rc) {
  334. dev_err(dev, "Cleanup: Reading UP status failed, rc=%d\n", rc);
  335. /* Will try reset again on next attempt at using it */
  336. sbefifo->broken = true;
  337. return rc;
  338. }
  339. rc = sbefifo_regr(sbefifo, SBEFIFO_DOWN | SBEFIFO_STS, &down_status);
  340. if (rc) {
  341. dev_err(dev, "Cleanup: Reading DOWN status failed, rc=%d\n", rc);
  342. /* Will try reset again on next attempt at using it */
  343. sbefifo->broken = true;
  344. return rc;
  345. }
  346. /* The FIFO already contains a reset request from the SBE ? */
  347. if (down_status & SBEFIFO_STS_RESET_REQ) {
  348. dev_info(dev, "Cleanup: FIFO reset request set, resetting\n");
  349. rc = sbefifo_regw(sbefifo, SBEFIFO_UP, SBEFIFO_PERFORM_RESET);
  350. if (rc) {
  351. sbefifo->broken = true;
  352. dev_err(dev, "Cleanup: Reset reg write failed, rc=%d\n", rc);
  353. return rc;
  354. }
  355. sbefifo->broken = false;
  356. return 0;
  357. }
  358. /* Parity error on either FIFO ? */
  359. if ((up_status | down_status) & SBEFIFO_STS_PARITY_ERR)
  360. need_reset = true;
  361. /* Either FIFO not empty ? */
  362. if (!((up_status & down_status) & SBEFIFO_STS_EMPTY))
  363. need_reset = true;
  364. if (!need_reset)
  365. return 0;
  366. dev_info(dev, "Cleanup: FIFO not clean (up=0x%08x down=0x%08x)\n",
  367. up_status, down_status);
  368. do_reset:
  369. /* Mark broken, will be cleared if/when reset succeeds */
  370. return sbefifo_request_reset(sbefifo);
  371. }
  372. static int sbefifo_wait(struct sbefifo *sbefifo, bool up,
  373. u32 *status, unsigned long timeout)
  374. {
  375. struct device *dev = &sbefifo->fsi_dev->dev;
  376. unsigned long end_time;
  377. bool ready = false;
  378. u32 addr, sts = 0;
  379. int rc;
  380. dev_vdbg(dev, "Wait on %s fifo...\n", up ? "up" : "down");
  381. addr = (up ? SBEFIFO_UP : SBEFIFO_DOWN) | SBEFIFO_STS;
  382. end_time = jiffies + timeout;
  383. while (!time_after(jiffies, end_time)) {
  384. cond_resched();
  385. rc = sbefifo_regr(sbefifo, addr, &sts);
  386. if (rc < 0) {
  387. dev_err(dev, "FSI error %d reading status register\n", rc);
  388. return rc;
  389. }
  390. if (!up && sbefifo_parity_err(sts)) {
  391. dev_err(dev, "Parity error in DOWN FIFO\n");
  392. return -ENXIO;
  393. }
  394. ready = !(up ? sbefifo_full(sts) : sbefifo_empty(sts));
  395. if (ready)
  396. break;
  397. }
  398. if (!ready) {
  399. dev_err(dev, "%s FIFO Timeout ! status=%08x\n", up ? "UP" : "DOWN", sts);
  400. return -ETIMEDOUT;
  401. }
  402. dev_vdbg(dev, "End of wait status: %08x\n", sts);
  403. *status = sts;
  404. return 0;
  405. }
  406. static int sbefifo_send_command(struct sbefifo *sbefifo,
  407. const __be32 *command, size_t cmd_len)
  408. {
  409. struct device *dev = &sbefifo->fsi_dev->dev;
  410. size_t len, chunk, vacant = 0, remaining = cmd_len;
  411. unsigned long timeout;
  412. u32 status;
  413. int rc;
  414. dev_vdbg(dev, "sending command (%zd words, cmd=%04x)\n",
  415. cmd_len, be32_to_cpu(command[1]));
  416. /* As long as there's something to send */
  417. timeout = msecs_to_jiffies(SBEFIFO_TIMEOUT_START_CMD);
  418. while (remaining) {
  419. /* Wait for room in the FIFO */
  420. rc = sbefifo_wait(sbefifo, true, &status, timeout);
  421. if (rc < 0)
  422. return rc;
  423. timeout = msecs_to_jiffies(SBEFIFO_TIMEOUT_IN_CMD);
  424. vacant = sbefifo_vacant(status);
  425. len = chunk = min(vacant, remaining);
  426. dev_vdbg(dev, " status=%08x vacant=%zd chunk=%zd\n",
  427. status, vacant, chunk);
  428. /* Write as much as we can */
  429. while (len--) {
  430. rc = sbefifo_up_write(sbefifo, *(command++));
  431. if (rc) {
  432. dev_err(dev, "FSI error %d writing UP FIFO\n", rc);
  433. return rc;
  434. }
  435. }
  436. remaining -= chunk;
  437. vacant -= chunk;
  438. }
  439. /* If there's no room left, wait for some to write EOT */
  440. if (!vacant) {
  441. rc = sbefifo_wait(sbefifo, true, &status, timeout);
  442. if (rc)
  443. return rc;
  444. }
  445. /* Send an EOT */
  446. rc = sbefifo_regw(sbefifo, SBEFIFO_UP | SBEFIFO_EOT_RAISE, 0);
  447. if (rc)
  448. dev_err(dev, "FSI error %d writing EOT\n", rc);
  449. return rc;
  450. }
  451. static int sbefifo_read_response(struct sbefifo *sbefifo, struct iov_iter *response)
  452. {
  453. struct device *dev = &sbefifo->fsi_dev->dev;
  454. u32 status, eot_set;
  455. unsigned long timeout;
  456. bool overflow = false;
  457. __be32 data;
  458. size_t len;
  459. int rc;
  460. dev_vdbg(dev, "reading response, buflen = %zd\n", iov_iter_count(response));
  461. timeout = msecs_to_jiffies(SBEFIFO_TIMEOUT_START_RSP);
  462. for (;;) {
  463. /* Grab FIFO status (this will handle parity errors) */
  464. rc = sbefifo_wait(sbefifo, false, &status, timeout);
  465. if (rc < 0)
  466. return rc;
  467. timeout = msecs_to_jiffies(SBEFIFO_TIMEOUT_IN_RSP);
  468. /* Decode status */
  469. len = sbefifo_populated(status);
  470. eot_set = sbefifo_eot_set(status);
  471. dev_vdbg(dev, " chunk size %zd eot_set=0x%x\n", len, eot_set);
  472. /* Go through the chunk */
  473. while(len--) {
  474. /* Read the data */
  475. rc = sbefifo_down_read(sbefifo, &data);
  476. if (rc < 0)
  477. return rc;
  478. /* Was it an EOT ? */
  479. if (eot_set & 0x80) {
  480. /*
  481. * There should be nothing else in the FIFO,
  482. * if there is, mark broken, this will force
  483. * a reset on next use, but don't fail the
  484. * command.
  485. */
  486. if (len) {
  487. dev_warn(dev, "FIFO read hit"
  488. " EOT with still %zd data\n",
  489. len);
  490. sbefifo->broken = true;
  491. }
  492. /* We are done */
  493. rc = sbefifo_regw(sbefifo,
  494. SBEFIFO_DOWN | SBEFIFO_EOT_ACK, 0);
  495. /*
  496. * If that write fail, still complete the request but mark
  497. * the fifo as broken for subsequent reset (not much else
  498. * we can do here).
  499. */
  500. if (rc) {
  501. dev_err(dev, "FSI error %d ack'ing EOT\n", rc);
  502. sbefifo->broken = true;
  503. }
  504. /* Tell whether we overflowed */
  505. return overflow ? -EOVERFLOW : 0;
  506. }
  507. /* Store it if there is room */
  508. if (iov_iter_count(response) >= sizeof(__be32)) {
  509. if (copy_to_iter(&data, sizeof(__be32), response) < sizeof(__be32))
  510. return -EFAULT;
  511. } else {
  512. dev_vdbg(dev, "Response overflowed !\n");
  513. overflow = true;
  514. }
  515. /* Next EOT bit */
  516. eot_set <<= 1;
  517. }
  518. }
  519. /* Shouldn't happen */
  520. return -EIO;
  521. }
  522. static int sbefifo_do_command(struct sbefifo *sbefifo,
  523. const __be32 *command, size_t cmd_len,
  524. struct iov_iter *response)
  525. {
  526. /* Try sending the command */
  527. int rc = sbefifo_send_command(sbefifo, command, cmd_len);
  528. if (rc)
  529. return rc;
  530. /* Now, get the response */
  531. return sbefifo_read_response(sbefifo, response);
  532. }
  533. static void sbefifo_collect_async_ffdc(struct sbefifo *sbefifo)
  534. {
  535. struct device *dev = &sbefifo->fsi_dev->dev;
  536. struct iov_iter ffdc_iter;
  537. struct kvec ffdc_iov;
  538. __be32 *ffdc;
  539. size_t ffdc_sz;
  540. __be32 cmd[2];
  541. int rc;
  542. sbefifo->async_ffdc = false;
  543. ffdc = vmalloc(SBEFIFO_MAX_FFDC_SIZE);
  544. if (!ffdc) {
  545. dev_err(dev, "Failed to allocate SBE FFDC buffer\n");
  546. return;
  547. }
  548. ffdc_iov.iov_base = ffdc;
  549. ffdc_iov.iov_len = SBEFIFO_MAX_FFDC_SIZE;
  550. iov_iter_kvec(&ffdc_iter, WRITE | ITER_KVEC, &ffdc_iov, 1, SBEFIFO_MAX_FFDC_SIZE);
  551. cmd[0] = cpu_to_be32(2);
  552. cmd[1] = cpu_to_be32(SBEFIFO_CMD_GET_SBE_FFDC);
  553. rc = sbefifo_do_command(sbefifo, cmd, 2, &ffdc_iter);
  554. if (rc != 0) {
  555. dev_err(dev, "Error %d retrieving SBE FFDC\n", rc);
  556. goto bail;
  557. }
  558. ffdc_sz = SBEFIFO_MAX_FFDC_SIZE - iov_iter_count(&ffdc_iter);
  559. ffdc_sz /= sizeof(__be32);
  560. rc = sbefifo_parse_status(dev, SBEFIFO_CMD_GET_SBE_FFDC, ffdc,
  561. ffdc_sz, &ffdc_sz);
  562. if (rc != 0) {
  563. dev_err(dev, "Error %d decoding SBE FFDC\n", rc);
  564. goto bail;
  565. }
  566. if (ffdc_sz > 0)
  567. sbefifo_dump_ffdc(dev, ffdc, ffdc_sz, true);
  568. bail:
  569. vfree(ffdc);
  570. }
  571. static int __sbefifo_submit(struct sbefifo *sbefifo,
  572. const __be32 *command, size_t cmd_len,
  573. struct iov_iter *response)
  574. {
  575. struct device *dev = &sbefifo->fsi_dev->dev;
  576. int rc;
  577. if (sbefifo->dead)
  578. return -ENODEV;
  579. if (cmd_len < 2 || be32_to_cpu(command[0]) != cmd_len) {
  580. dev_vdbg(dev, "Invalid command len %zd (header: %d)\n",
  581. cmd_len, be32_to_cpu(command[0]));
  582. return -EINVAL;
  583. }
  584. /* First ensure the HW is in a clean state */
  585. rc = sbefifo_cleanup_hw(sbefifo);
  586. if (rc)
  587. return rc;
  588. /* Look for async FFDC first if any */
  589. if (sbefifo->async_ffdc)
  590. sbefifo_collect_async_ffdc(sbefifo);
  591. rc = sbefifo_do_command(sbefifo, command, cmd_len, response);
  592. if (rc != 0 && rc != -EOVERFLOW)
  593. goto fail;
  594. return rc;
  595. fail:
  596. /*
  597. * On failure, attempt a reset. Ignore the result, it will mark
  598. * the fifo broken if the reset fails
  599. */
  600. sbefifo_request_reset(sbefifo);
  601. /* Return original error */
  602. return rc;
  603. }
  604. /**
  605. * sbefifo_submit() - Submit and SBE fifo command and receive response
  606. * @dev: The sbefifo device
  607. * @command: The raw command data
  608. * @cmd_len: The command size (in 32-bit words)
  609. * @response: The output response buffer
  610. * @resp_len: In: Response buffer size, Out: Response size
  611. *
  612. * This will perform the entire operation. If the reponse buffer
  613. * overflows, returns -EOVERFLOW
  614. */
  615. int sbefifo_submit(struct device *dev, const __be32 *command, size_t cmd_len,
  616. __be32 *response, size_t *resp_len)
  617. {
  618. struct sbefifo *sbefifo;
  619. struct iov_iter resp_iter;
  620. struct kvec resp_iov;
  621. size_t rbytes;
  622. int rc;
  623. if (!dev)
  624. return -ENODEV;
  625. sbefifo = dev_get_drvdata(dev);
  626. if (!sbefifo)
  627. return -ENODEV;
  628. if (WARN_ON_ONCE(sbefifo->magic != SBEFIFO_MAGIC))
  629. return -ENODEV;
  630. if (!resp_len || !command || !response)
  631. return -EINVAL;
  632. /* Prepare iov iterator */
  633. rbytes = (*resp_len) * sizeof(__be32);
  634. resp_iov.iov_base = response;
  635. resp_iov.iov_len = rbytes;
  636. iov_iter_kvec(&resp_iter, WRITE | ITER_KVEC, &resp_iov, 1, rbytes);
  637. /* Perform the command */
  638. mutex_lock(&sbefifo->lock);
  639. rc = __sbefifo_submit(sbefifo, command, cmd_len, &resp_iter);
  640. mutex_unlock(&sbefifo->lock);
  641. /* Extract the response length */
  642. rbytes -= iov_iter_count(&resp_iter);
  643. *resp_len = rbytes / sizeof(__be32);
  644. return rc;
  645. }
  646. EXPORT_SYMBOL_GPL(sbefifo_submit);
  647. /*
  648. * Char device interface
  649. */
  650. static void sbefifo_release_command(struct sbefifo_user *user)
  651. {
  652. if (is_vmalloc_addr(user->pending_cmd))
  653. vfree(user->pending_cmd);
  654. user->pending_cmd = NULL;
  655. user->pending_len = 0;
  656. }
  657. static int sbefifo_user_open(struct inode *inode, struct file *file)
  658. {
  659. struct sbefifo *sbefifo = container_of(inode->i_cdev, struct sbefifo, cdev);
  660. struct sbefifo_user *user;
  661. user = kzalloc(sizeof(struct sbefifo_user), GFP_KERNEL);
  662. if (!user)
  663. return -ENOMEM;
  664. file->private_data = user;
  665. user->sbefifo = sbefifo;
  666. user->cmd_page = (void *)__get_free_page(GFP_KERNEL);
  667. if (!user->cmd_page) {
  668. kfree(user);
  669. return -ENOMEM;
  670. }
  671. mutex_init(&user->file_lock);
  672. return 0;
  673. }
  674. static ssize_t sbefifo_user_read(struct file *file, char __user *buf,
  675. size_t len, loff_t *offset)
  676. {
  677. struct sbefifo_user *user = file->private_data;
  678. struct sbefifo *sbefifo;
  679. struct iov_iter resp_iter;
  680. struct iovec resp_iov;
  681. size_t cmd_len;
  682. int rc;
  683. if (!user)
  684. return -EINVAL;
  685. sbefifo = user->sbefifo;
  686. if (len & 3)
  687. return -EINVAL;
  688. mutex_lock(&user->file_lock);
  689. /* Cronus relies on -EAGAIN after a short read */
  690. if (user->pending_len == 0) {
  691. rc = -EAGAIN;
  692. goto bail;
  693. }
  694. if (user->pending_len < 8) {
  695. rc = -EINVAL;
  696. goto bail;
  697. }
  698. cmd_len = user->pending_len >> 2;
  699. /* Prepare iov iterator */
  700. resp_iov.iov_base = buf;
  701. resp_iov.iov_len = len;
  702. iov_iter_init(&resp_iter, WRITE, &resp_iov, 1, len);
  703. /* Perform the command */
  704. mutex_lock(&sbefifo->lock);
  705. rc = __sbefifo_submit(sbefifo, user->pending_cmd, cmd_len, &resp_iter);
  706. mutex_unlock(&sbefifo->lock);
  707. if (rc < 0)
  708. goto bail;
  709. /* Extract the response length */
  710. rc = len - iov_iter_count(&resp_iter);
  711. bail:
  712. sbefifo_release_command(user);
  713. mutex_unlock(&user->file_lock);
  714. return rc;
  715. }
  716. static ssize_t sbefifo_user_write(struct file *file, const char __user *buf,
  717. size_t len, loff_t *offset)
  718. {
  719. struct sbefifo_user *user = file->private_data;
  720. struct sbefifo *sbefifo;
  721. int rc = len;
  722. if (!user)
  723. return -EINVAL;
  724. sbefifo = user->sbefifo;
  725. if (len > SBEFIFO_MAX_USER_CMD_LEN)
  726. return -EINVAL;
  727. if (len & 3)
  728. return -EINVAL;
  729. mutex_lock(&user->file_lock);
  730. /* Can we use the pre-allocate buffer ? If not, allocate */
  731. if (len <= PAGE_SIZE)
  732. user->pending_cmd = user->cmd_page;
  733. else
  734. user->pending_cmd = vmalloc(len);
  735. if (!user->pending_cmd) {
  736. rc = -ENOMEM;
  737. goto bail;
  738. }
  739. /* Copy the command into the staging buffer */
  740. if (copy_from_user(user->pending_cmd, buf, len)) {
  741. rc = -EFAULT;
  742. goto bail;
  743. }
  744. /* Check for the magic reset command */
  745. if (len == 4 && be32_to_cpu(*(__be32 *)user->pending_cmd) ==
  746. SBEFIFO_RESET_MAGIC) {
  747. /* Clear out any pending command */
  748. user->pending_len = 0;
  749. /* Trigger reset request */
  750. mutex_lock(&sbefifo->lock);
  751. rc = sbefifo_request_reset(user->sbefifo);
  752. mutex_unlock(&sbefifo->lock);
  753. if (rc == 0)
  754. rc = 4;
  755. goto bail;
  756. }
  757. /* Update the staging buffer size */
  758. user->pending_len = len;
  759. bail:
  760. if (!user->pending_len)
  761. sbefifo_release_command(user);
  762. mutex_unlock(&user->file_lock);
  763. /* And that's it, we'll issue the command on a read */
  764. return rc;
  765. }
  766. static int sbefifo_user_release(struct inode *inode, struct file *file)
  767. {
  768. struct sbefifo_user *user = file->private_data;
  769. if (!user)
  770. return -EINVAL;
  771. sbefifo_release_command(user);
  772. free_page((unsigned long)user->cmd_page);
  773. kfree(user);
  774. return 0;
  775. }
  776. static const struct file_operations sbefifo_fops = {
  777. .owner = THIS_MODULE,
  778. .open = sbefifo_user_open,
  779. .read = sbefifo_user_read,
  780. .write = sbefifo_user_write,
  781. .release = sbefifo_user_release,
  782. };
  783. static void sbefifo_free(struct device *dev)
  784. {
  785. struct sbefifo *sbefifo = container_of(dev, struct sbefifo, dev);
  786. put_device(&sbefifo->fsi_dev->dev);
  787. kfree(sbefifo);
  788. }
  789. /*
  790. * Probe/remove
  791. */
  792. static int sbefifo_probe(struct device *dev)
  793. {
  794. struct fsi_device *fsi_dev = to_fsi_dev(dev);
  795. struct sbefifo *sbefifo;
  796. struct device_node *np;
  797. struct platform_device *child;
  798. char child_name[32];
  799. int rc, didx, child_idx = 0;
  800. dev_dbg(dev, "Found sbefifo device\n");
  801. sbefifo = kzalloc(sizeof(*sbefifo), GFP_KERNEL);
  802. if (!sbefifo)
  803. return -ENOMEM;
  804. /* Grab a reference to the device (parent of our cdev), we'll drop it later */
  805. if (!get_device(dev)) {
  806. kfree(sbefifo);
  807. return -ENODEV;
  808. }
  809. sbefifo->magic = SBEFIFO_MAGIC;
  810. sbefifo->fsi_dev = fsi_dev;
  811. dev_set_drvdata(dev, sbefifo);
  812. mutex_init(&sbefifo->lock);
  813. /*
  814. * Try cleaning up the FIFO. If this fails, we still register the
  815. * driver and will try cleaning things up again on the next access.
  816. */
  817. rc = sbefifo_cleanup_hw(sbefifo);
  818. if (rc && rc != -ESHUTDOWN)
  819. dev_err(dev, "Initial HW cleanup failed, will retry later\n");
  820. /* Create chardev for userspace access */
  821. sbefifo->dev.type = &fsi_cdev_type;
  822. sbefifo->dev.parent = dev;
  823. sbefifo->dev.release = sbefifo_free;
  824. device_initialize(&sbefifo->dev);
  825. /* Allocate a minor in the FSI space */
  826. rc = fsi_get_new_minor(fsi_dev, fsi_dev_sbefifo, &sbefifo->dev.devt, &didx);
  827. if (rc)
  828. goto err;
  829. dev_set_name(&sbefifo->dev, "sbefifo%d", didx);
  830. cdev_init(&sbefifo->cdev, &sbefifo_fops);
  831. rc = cdev_device_add(&sbefifo->cdev, &sbefifo->dev);
  832. if (rc) {
  833. dev_err(dev, "Error %d creating char device %s\n",
  834. rc, dev_name(&sbefifo->dev));
  835. goto err_free_minor;
  836. }
  837. /* Create platform devs for dts child nodes (occ, etc) */
  838. for_each_available_child_of_node(dev->of_node, np) {
  839. snprintf(child_name, sizeof(child_name), "%s-dev%d",
  840. dev_name(&sbefifo->dev), child_idx++);
  841. child = of_platform_device_create(np, child_name, dev);
  842. if (!child)
  843. dev_warn(dev, "failed to create child %s dev\n",
  844. child_name);
  845. }
  846. return 0;
  847. err_free_minor:
  848. fsi_free_minor(sbefifo->dev.devt);
  849. err:
  850. put_device(&sbefifo->dev);
  851. return rc;
  852. }
  853. static int sbefifo_unregister_child(struct device *dev, void *data)
  854. {
  855. struct platform_device *child = to_platform_device(dev);
  856. of_device_unregister(child);
  857. if (dev->of_node)
  858. of_node_clear_flag(dev->of_node, OF_POPULATED);
  859. return 0;
  860. }
  861. static int sbefifo_remove(struct device *dev)
  862. {
  863. struct sbefifo *sbefifo = dev_get_drvdata(dev);
  864. dev_dbg(dev, "Removing sbefifo device...\n");
  865. mutex_lock(&sbefifo->lock);
  866. sbefifo->dead = true;
  867. mutex_unlock(&sbefifo->lock);
  868. cdev_device_del(&sbefifo->cdev, &sbefifo->dev);
  869. fsi_free_minor(sbefifo->dev.devt);
  870. device_for_each_child(dev, NULL, sbefifo_unregister_child);
  871. put_device(&sbefifo->dev);
  872. return 0;
  873. }
  874. static struct fsi_device_id sbefifo_ids[] = {
  875. {
  876. .engine_type = FSI_ENGID_SBE,
  877. .version = FSI_VERSION_ANY,
  878. },
  879. { 0 }
  880. };
  881. static struct fsi_driver sbefifo_drv = {
  882. .id_table = sbefifo_ids,
  883. .drv = {
  884. .name = DEVICE_NAME,
  885. .bus = &fsi_bus_type,
  886. .probe = sbefifo_probe,
  887. .remove = sbefifo_remove,
  888. }
  889. };
  890. static int sbefifo_init(void)
  891. {
  892. return fsi_driver_register(&sbefifo_drv);
  893. }
  894. static void sbefifo_exit(void)
  895. {
  896. fsi_driver_unregister(&sbefifo_drv);
  897. }
  898. module_init(sbefifo_init);
  899. module_exit(sbefifo_exit);
  900. MODULE_LICENSE("GPL");
  901. MODULE_AUTHOR("Brad Bishop <bradleyb@fuzziesquirrel.com>");
  902. MODULE_AUTHOR("Eddie James <eajames@linux.vnet.ibm.com>");
  903. MODULE_AUTHOR("Andrew Jeffery <andrew@aj.id.au>");
  904. MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
  905. MODULE_DESCRIPTION("Linux device interface to the POWER Self Boot Engine");