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