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