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