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