1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) IBM Corporation 2017
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.
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.
15 #include <linux/device.h>
16 #include <linux/errno.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>
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>
36 * The SBEFIFO is a pipe-like FSI device for communicating with
37 * the self boot engine on POWER processors.
40 #define DEVICE_NAME "sbefifo"
41 #define FSI_ENGID_SBE 0x22
48 #define SBEFIFO_UP 0x00 /* FSI -> Host */
49 #define SBEFIFO_DOWN 0x40 /* Host -> FSI */
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 */
72 /* CFAM GP Mailbox SelfBoot Message register */
73 #define CFAM_GP_MBOX_SBM_ADDR 0x2824 /* Converted 0x2809 */
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
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
94 #define SBEFIFO_FIFO_DEPTH 8
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)
104 /* Reset request timeout in ms */
105 #define SBEFIFO_RESET_TIMEOUT 10000
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
113 /* Other constants */
114 #define SBEFIFO_MAX_USER_CMD_LEN (0x100000 + PAGE_SIZE)
115 #define SBEFIFO_RESET_MAGIC 0x52534554 /* "RSET" */
119 #define SBEFIFO_MAGIC 0x53424546 /* "SBEF" */
120 struct fsi_device
*fsi_dev
;
129 struct sbefifo_user
{
130 struct sbefifo
*sbefifo
;
131 struct mutex file_lock
;
137 static DEFINE_MUTEX(sbefifo_ffdc_mutex
);
140 static void __sbefifo_dump_ffdc(struct device
*dev
, const __be32
*ffdc
,
141 size_t ffdc_sz
, bool internal
)
144 #define FFDC_LSIZE 60
145 static char ffdc_line
[FFDC_LSIZE
];
151 dev_err(dev
, "SBE invalid FFDC package size %zd\n", ffdc_sz
);
154 w0
= be32_to_cpu(*(ffdc
++));
155 w1
= be32_to_cpu(*(ffdc
++));
156 w2
= be32_to_cpu(*(ffdc
++));
158 if ((w0
>> 16) != 0xFFDC) {
159 dev_err(dev
, "SBE invalid FFDC package signature %08x %08x %08x\n",
165 dev_err(dev
, "SBE FFDC package len %d words but only %zd remaining\n",
171 dev_warn(dev
, "+---- SBE FFDC package %d for async err -----+\n",
174 dev_warn(dev
, "+---- SBE FFDC package %d for cmd %02x:%02x -----+\n",
175 pack
++, (w1
>> 8) & 0xff, w1
& 0xff);
177 dev_warn(dev
, "| Response code: %08x |\n", w2
);
178 dev_warn(dev
, "|-------------------------------------------|\n");
179 for (i
= 0; i
< w0
; i
++) {
182 p
+= sprintf(p
, "| %04x:", i
<< 4);
184 p
+= sprintf(p
, " %08x", be32_to_cpu(*(ffdc
++)));
186 if ((i
& 3) == 3 || i
== (w0
- 1)) {
187 while ((i
& 3) < 3) {
188 p
+= sprintf(p
, " ");
191 dev_warn(dev
, "%s |\n", ffdc_line
);
194 dev_warn(dev
, "+-------------------------------------------+\n");
198 static void sbefifo_dump_ffdc(struct device
*dev
, const __be32
*ffdc
,
199 size_t ffdc_sz
, bool internal
)
201 mutex_lock(&sbefifo_ffdc_mutex
);
202 __sbefifo_dump_ffdc(dev
, ffdc
, ffdc_sz
, internal
);
203 mutex_unlock(&sbefifo_ffdc_mutex
);
206 int sbefifo_parse_status(struct device
*dev
, u16 cmd
, __be32
*response
,
207 size_t resp_len
, size_t *data_len
)
213 pr_debug("sbefifo: cmd %04x, response too small: %zd\n",
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
);
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
);
232 dev_warn(dev
, "SBE error cmd %02x:%02x status=%04x:%04x\n",
233 cmd
>> 8, cmd
& 0xff, s1
>> 16, s1
& 0xffff);
235 sbefifo_dump_ffdc(dev
, &response
[resp_len
- dh
+ 2],
239 *data_len
= resp_len
- dh
;
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.
247 EXPORT_SYMBOL_GPL(sbefifo_parse_status
);
249 static int sbefifo_regr(struct sbefifo
*sbefifo
, int reg
, u32
*word
)
254 rc
= fsi_device_read(sbefifo
->fsi_dev
, reg
, &raw_word
,
259 *word
= be32_to_cpu(raw_word
);
264 static int sbefifo_regw(struct sbefifo
*sbefifo
, int reg
, u32 word
)
266 __be32 raw_word
= cpu_to_be32(word
);
268 return fsi_device_write(sbefifo
->fsi_dev
, reg
, &raw_word
,
272 static int sbefifo_check_sbe_state(struct sbefifo
*sbefifo
)
278 rc
= fsi_slave_read(sbefifo
->fsi_dev
->slave
, CFAM_GP_MBOX_SBM_ADDR
,
279 &raw_word
, sizeof(raw_word
));
282 sbm
= be32_to_cpu(raw_word
);
284 /* SBE booted at all ? */
285 if (!(sbm
& CFAM_SBM_SBE_BOOTED
))
288 /* Check its state */
289 switch ((sbm
& CFAM_SBM_SBE_STATE_MASK
) >> CFAM_SBM_SBE_STATE_SHIFT
) {
290 case SBE_STATE_UNKNOWN
:
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 */
300 case SBE_STATE_FAILURE
:
301 case SBE_STATE_QUIESCE
:
305 /* Is there async FFDC available ? Remember it */
306 if (sbm
& CFAM_SBM_SBE_ASYNC_FFDC
)
307 sbefifo
->async_ffdc
= true;
312 /* Don't flip endianness of data to/from FIFO, just pass through. */
313 static int sbefifo_down_read(struct sbefifo
*sbefifo
, __be32
*word
)
315 return fsi_device_read(sbefifo
->fsi_dev
, SBEFIFO_DOWN
, word
,
319 static int sbefifo_up_write(struct sbefifo
*sbefifo
, __be32 word
)
321 return fsi_device_write(sbefifo
->fsi_dev
, SBEFIFO_UP
, &word
,
325 static int sbefifo_request_reset(struct sbefifo
*sbefifo
)
327 struct device
*dev
= &sbefifo
->fsi_dev
->dev
;
331 dev_dbg(dev
, "Requesting FIFO reset\n");
333 /* Mark broken first, will be cleared if reset succeeds */
334 sbefifo
->broken
= true;
336 /* Send reset request */
337 rc
= sbefifo_regw(sbefifo
, SBEFIFO_UP
| SBEFIFO_REQ_RESET
, 1);
339 dev_err(dev
, "Sending reset request failed, rc=%d\n", rc
);
343 /* Wait for it to complete */
344 for (timeout
= 0; timeout
< SBEFIFO_RESET_TIMEOUT
; timeout
++) {
345 rc
= sbefifo_regr(sbefifo
, SBEFIFO_UP
| SBEFIFO_STS
, &status
);
347 dev_err(dev
, "Failed to read UP fifo status during reset"
352 if (!(status
& SBEFIFO_STS_RESET_REQ
)) {
353 dev_dbg(dev
, "FIFO reset done\n");
354 sbefifo
->broken
= false;
360 dev_err(dev
, "FIFO reset timed out\n");
365 static int sbefifo_cleanup_hw(struct sbefifo
*sbefifo
)
367 struct device
*dev
= &sbefifo
->fsi_dev
->dev
;
368 u32 up_status
, down_status
;
369 bool need_reset
= false;
372 rc
= sbefifo_check_sbe_state(sbefifo
);
374 dev_dbg(dev
, "SBE state=%d\n", rc
);
378 /* If broken, we don't need to look at status, go straight to reset */
382 rc
= sbefifo_regr(sbefifo
, SBEFIFO_UP
| SBEFIFO_STS
, &up_status
);
384 dev_err(dev
, "Cleanup: Reading UP status failed, rc=%d\n", rc
);
386 /* Will try reset again on next attempt at using it */
387 sbefifo
->broken
= true;
391 rc
= sbefifo_regr(sbefifo
, SBEFIFO_DOWN
| SBEFIFO_STS
, &down_status
);
393 dev_err(dev
, "Cleanup: Reading DOWN status failed, rc=%d\n", rc
);
395 /* Will try reset again on next attempt at using it */
396 sbefifo
->broken
= true;
400 /* The FIFO already contains a reset request from the SBE ? */
401 if (down_status
& SBEFIFO_STS_RESET_REQ
) {
402 dev_info(dev
, "Cleanup: FIFO reset request set, resetting\n");
403 rc
= sbefifo_regw(sbefifo
, SBEFIFO_UP
, SBEFIFO_PERFORM_RESET
);
405 sbefifo
->broken
= true;
406 dev_err(dev
, "Cleanup: Reset reg write failed, rc=%d\n", rc
);
409 sbefifo
->broken
= false;
413 /* Parity error on either FIFO ? */
414 if ((up_status
| down_status
) & SBEFIFO_STS_PARITY_ERR
)
417 /* Either FIFO not empty ? */
418 if (!((up_status
& down_status
) & SBEFIFO_STS_EMPTY
))
424 dev_info(dev
, "Cleanup: FIFO not clean (up=0x%08x down=0x%08x)\n",
425 up_status
, down_status
);
429 /* Mark broken, will be cleared if/when reset succeeds */
430 return sbefifo_request_reset(sbefifo
);
433 static int sbefifo_wait(struct sbefifo
*sbefifo
, bool up
,
434 u32
*status
, unsigned long timeout
)
436 struct device
*dev
= &sbefifo
->fsi_dev
->dev
;
437 unsigned long end_time
;
442 dev_vdbg(dev
, "Wait on %s fifo...\n", up
? "up" : "down");
444 addr
= (up
? SBEFIFO_UP
: SBEFIFO_DOWN
) | SBEFIFO_STS
;
446 end_time
= jiffies
+ timeout
;
447 while (!time_after(jiffies
, end_time
)) {
449 rc
= sbefifo_regr(sbefifo
, addr
, &sts
);
451 dev_err(dev
, "FSI error %d reading status register\n", rc
);
454 if (!up
&& sbefifo_parity_err(sts
)) {
455 dev_err(dev
, "Parity error in DOWN FIFO\n");
458 ready
= !(up
? sbefifo_full(sts
) : sbefifo_empty(sts
));
463 dev_err(dev
, "%s FIFO Timeout ! status=%08x\n", up
? "UP" : "DOWN", sts
);
466 dev_vdbg(dev
, "End of wait status: %08x\n", sts
);
473 static int sbefifo_send_command(struct sbefifo
*sbefifo
,
474 const __be32
*command
, size_t cmd_len
)
476 struct device
*dev
= &sbefifo
->fsi_dev
->dev
;
477 size_t len
, chunk
, vacant
= 0, remaining
= cmd_len
;
478 unsigned long timeout
;
482 dev_vdbg(dev
, "sending command (%zd words, cmd=%04x)\n",
483 cmd_len
, be32_to_cpu(command
[1]));
485 /* As long as there's something to send */
486 timeout
= msecs_to_jiffies(SBEFIFO_TIMEOUT_START_CMD
);
488 /* Wait for room in the FIFO */
489 rc
= sbefifo_wait(sbefifo
, true, &status
, timeout
);
492 timeout
= msecs_to_jiffies(SBEFIFO_TIMEOUT_IN_CMD
);
494 vacant
= sbefifo_vacant(status
);
495 len
= chunk
= min(vacant
, remaining
);
497 dev_vdbg(dev
, " status=%08x vacant=%zd chunk=%zd\n",
498 status
, vacant
, chunk
);
500 /* Write as much as we can */
502 rc
= sbefifo_up_write(sbefifo
, *(command
++));
504 dev_err(dev
, "FSI error %d writing UP FIFO\n", rc
);
512 /* If there's no room left, wait for some to write EOT */
514 rc
= sbefifo_wait(sbefifo
, true, &status
, timeout
);
520 rc
= sbefifo_regw(sbefifo
, SBEFIFO_UP
| SBEFIFO_EOT_RAISE
, 0);
522 dev_err(dev
, "FSI error %d writing EOT\n", rc
);
526 static int sbefifo_read_response(struct sbefifo
*sbefifo
, struct iov_iter
*response
)
528 struct device
*dev
= &sbefifo
->fsi_dev
->dev
;
530 unsigned long timeout
;
531 bool overflow
= false;
536 dev_vdbg(dev
, "reading response, buflen = %zd\n", iov_iter_count(response
));
538 timeout
= msecs_to_jiffies(SBEFIFO_TIMEOUT_START_RSP
);
540 /* Grab FIFO status (this will handle parity errors) */
541 rc
= sbefifo_wait(sbefifo
, false, &status
, timeout
);
544 timeout
= msecs_to_jiffies(SBEFIFO_TIMEOUT_IN_RSP
);
547 len
= sbefifo_populated(status
);
548 eot_set
= sbefifo_eot_set(status
);
550 dev_vdbg(dev
, " chunk size %zd eot_set=0x%x\n", len
, eot_set
);
552 /* Go through the chunk */
555 rc
= sbefifo_down_read(sbefifo
, &data
);
559 /* Was it an EOT ? */
560 if (eot_set
& 0x80) {
562 * There should be nothing else in the FIFO,
563 * if there is, mark broken, this will force
564 * a reset on next use, but don't fail the
568 dev_warn(dev
, "FIFO read hit"
569 " EOT with still %zd data\n",
571 sbefifo
->broken
= true;
575 rc
= sbefifo_regw(sbefifo
,
576 SBEFIFO_DOWN
| SBEFIFO_EOT_ACK
, 0);
579 * If that write fail, still complete the request but mark
580 * the fifo as broken for subsequent reset (not much else
584 dev_err(dev
, "FSI error %d ack'ing EOT\n", rc
);
585 sbefifo
->broken
= true;
588 /* Tell whether we overflowed */
589 return overflow
? -EOVERFLOW
: 0;
592 /* Store it if there is room */
593 if (iov_iter_count(response
) >= sizeof(__be32
)) {
594 if (copy_to_iter(&data
, sizeof(__be32
), response
) < sizeof(__be32
))
597 dev_vdbg(dev
, "Response overflowed !\n");
606 /* Shouldn't happen */
610 static int sbefifo_do_command(struct sbefifo
*sbefifo
,
611 const __be32
*command
, size_t cmd_len
,
612 struct iov_iter
*response
)
614 /* Try sending the command */
615 int rc
= sbefifo_send_command(sbefifo
, command
, cmd_len
);
619 /* Now, get the response */
620 return sbefifo_read_response(sbefifo
, response
);
623 static void sbefifo_collect_async_ffdc(struct sbefifo
*sbefifo
)
625 struct device
*dev
= &sbefifo
->fsi_dev
->dev
;
626 struct iov_iter ffdc_iter
;
627 struct kvec ffdc_iov
;
633 sbefifo
->async_ffdc
= false;
634 ffdc
= vmalloc(SBEFIFO_MAX_FFDC_SIZE
);
636 dev_err(dev
, "Failed to allocate SBE FFDC buffer\n");
639 ffdc_iov
.iov_base
= ffdc
;
640 ffdc_iov
.iov_len
= SBEFIFO_MAX_FFDC_SIZE
;
641 iov_iter_kvec(&ffdc_iter
, WRITE
, &ffdc_iov
, 1, SBEFIFO_MAX_FFDC_SIZE
);
642 cmd
[0] = cpu_to_be32(2);
643 cmd
[1] = cpu_to_be32(SBEFIFO_CMD_GET_SBE_FFDC
);
644 rc
= sbefifo_do_command(sbefifo
, cmd
, 2, &ffdc_iter
);
646 dev_err(dev
, "Error %d retrieving SBE FFDC\n", rc
);
649 ffdc_sz
= SBEFIFO_MAX_FFDC_SIZE
- iov_iter_count(&ffdc_iter
);
650 ffdc_sz
/= sizeof(__be32
);
651 rc
= sbefifo_parse_status(dev
, SBEFIFO_CMD_GET_SBE_FFDC
, ffdc
,
654 dev_err(dev
, "Error %d decoding SBE FFDC\n", rc
);
658 sbefifo_dump_ffdc(dev
, ffdc
, ffdc_sz
, true);
664 static int __sbefifo_submit(struct sbefifo
*sbefifo
,
665 const __be32
*command
, size_t cmd_len
,
666 struct iov_iter
*response
)
668 struct device
*dev
= &sbefifo
->fsi_dev
->dev
;
674 if (cmd_len
< 2 || be32_to_cpu(command
[0]) != cmd_len
) {
675 dev_vdbg(dev
, "Invalid command len %zd (header: %d)\n",
676 cmd_len
, be32_to_cpu(command
[0]));
680 /* First ensure the HW is in a clean state */
681 rc
= sbefifo_cleanup_hw(sbefifo
);
685 /* Look for async FFDC first if any */
686 if (sbefifo
->async_ffdc
)
687 sbefifo_collect_async_ffdc(sbefifo
);
689 rc
= sbefifo_do_command(sbefifo
, command
, cmd_len
, response
);
690 if (rc
!= 0 && rc
!= -EOVERFLOW
)
695 * On failure, attempt a reset. Ignore the result, it will mark
696 * the fifo broken if the reset fails
698 sbefifo_request_reset(sbefifo
);
700 /* Return original error */
705 * sbefifo_submit() - Submit and SBE fifo command and receive response
706 * @dev: The sbefifo device
707 * @command: The raw command data
708 * @cmd_len: The command size (in 32-bit words)
709 * @response: The output response buffer
710 * @resp_len: In: Response buffer size, Out: Response size
712 * This will perform the entire operation. If the reponse buffer
713 * overflows, returns -EOVERFLOW
715 int sbefifo_submit(struct device
*dev
, const __be32
*command
, size_t cmd_len
,
716 __be32
*response
, size_t *resp_len
)
718 struct sbefifo
*sbefifo
;
719 struct iov_iter resp_iter
;
720 struct kvec resp_iov
;
726 sbefifo
= dev_get_drvdata(dev
);
729 if (WARN_ON_ONCE(sbefifo
->magic
!= SBEFIFO_MAGIC
))
731 if (!resp_len
|| !command
|| !response
)
734 /* Prepare iov iterator */
735 rbytes
= (*resp_len
) * sizeof(__be32
);
736 resp_iov
.iov_base
= response
;
737 resp_iov
.iov_len
= rbytes
;
738 iov_iter_kvec(&resp_iter
, WRITE
, &resp_iov
, 1, rbytes
);
740 /* Perform the command */
741 mutex_lock(&sbefifo
->lock
);
742 rc
= __sbefifo_submit(sbefifo
, command
, cmd_len
, &resp_iter
);
743 mutex_unlock(&sbefifo
->lock
);
745 /* Extract the response length */
746 rbytes
-= iov_iter_count(&resp_iter
);
747 *resp_len
= rbytes
/ sizeof(__be32
);
751 EXPORT_SYMBOL_GPL(sbefifo_submit
);
754 * Char device interface
757 static void sbefifo_release_command(struct sbefifo_user
*user
)
759 if (is_vmalloc_addr(user
->pending_cmd
))
760 vfree(user
->pending_cmd
);
761 user
->pending_cmd
= NULL
;
762 user
->pending_len
= 0;
765 static int sbefifo_user_open(struct inode
*inode
, struct file
*file
)
767 struct sbefifo
*sbefifo
= container_of(inode
->i_cdev
, struct sbefifo
, cdev
);
768 struct sbefifo_user
*user
;
770 user
= kzalloc(sizeof(struct sbefifo_user
), GFP_KERNEL
);
774 file
->private_data
= user
;
775 user
->sbefifo
= sbefifo
;
776 user
->cmd_page
= (void *)__get_free_page(GFP_KERNEL
);
777 if (!user
->cmd_page
) {
781 mutex_init(&user
->file_lock
);
786 static ssize_t
sbefifo_user_read(struct file
*file
, char __user
*buf
,
787 size_t len
, loff_t
*offset
)
789 struct sbefifo_user
*user
= file
->private_data
;
790 struct sbefifo
*sbefifo
;
791 struct iov_iter resp_iter
;
792 struct iovec resp_iov
;
798 sbefifo
= user
->sbefifo
;
802 mutex_lock(&user
->file_lock
);
804 /* Cronus relies on -EAGAIN after a short read */
805 if (user
->pending_len
== 0) {
809 if (user
->pending_len
< 8) {
813 cmd_len
= user
->pending_len
>> 2;
815 /* Prepare iov iterator */
816 resp_iov
.iov_base
= buf
;
817 resp_iov
.iov_len
= len
;
818 iov_iter_init(&resp_iter
, WRITE
, &resp_iov
, 1, len
);
820 /* Perform the command */
821 mutex_lock(&sbefifo
->lock
);
822 rc
= __sbefifo_submit(sbefifo
, user
->pending_cmd
, cmd_len
, &resp_iter
);
823 mutex_unlock(&sbefifo
->lock
);
827 /* Extract the response length */
828 rc
= len
- iov_iter_count(&resp_iter
);
830 sbefifo_release_command(user
);
831 mutex_unlock(&user
->file_lock
);
835 static ssize_t
sbefifo_user_write(struct file
*file
, const char __user
*buf
,
836 size_t len
, loff_t
*offset
)
838 struct sbefifo_user
*user
= file
->private_data
;
839 struct sbefifo
*sbefifo
;
844 sbefifo
= user
->sbefifo
;
845 if (len
> SBEFIFO_MAX_USER_CMD_LEN
)
850 mutex_lock(&user
->file_lock
);
852 /* Can we use the pre-allocate buffer ? If not, allocate */
853 if (len
<= PAGE_SIZE
)
854 user
->pending_cmd
= user
->cmd_page
;
856 user
->pending_cmd
= vmalloc(len
);
857 if (!user
->pending_cmd
) {
862 /* Copy the command into the staging buffer */
863 if (copy_from_user(user
->pending_cmd
, buf
, len
)) {
868 /* Check for the magic reset command */
869 if (len
== 4 && be32_to_cpu(*(__be32
*)user
->pending_cmd
) ==
870 SBEFIFO_RESET_MAGIC
) {
872 /* Clear out any pending command */
873 user
->pending_len
= 0;
875 /* Trigger reset request */
876 mutex_lock(&sbefifo
->lock
);
877 rc
= sbefifo_request_reset(user
->sbefifo
);
878 mutex_unlock(&sbefifo
->lock
);
884 /* Update the staging buffer size */
885 user
->pending_len
= len
;
887 if (!user
->pending_len
)
888 sbefifo_release_command(user
);
890 mutex_unlock(&user
->file_lock
);
892 /* And that's it, we'll issue the command on a read */
896 static int sbefifo_user_release(struct inode
*inode
, struct file
*file
)
898 struct sbefifo_user
*user
= file
->private_data
;
903 sbefifo_release_command(user
);
904 free_page((unsigned long)user
->cmd_page
);
910 static const struct file_operations sbefifo_fops
= {
911 .owner
= THIS_MODULE
,
912 .open
= sbefifo_user_open
,
913 .read
= sbefifo_user_read
,
914 .write
= sbefifo_user_write
,
915 .release
= sbefifo_user_release
,
918 static void sbefifo_free(struct device
*dev
)
920 struct sbefifo
*sbefifo
= container_of(dev
, struct sbefifo
, dev
);
922 put_device(&sbefifo
->fsi_dev
->dev
);
930 static int sbefifo_probe(struct device
*dev
)
932 struct fsi_device
*fsi_dev
= to_fsi_dev(dev
);
933 struct sbefifo
*sbefifo
;
934 struct device_node
*np
;
935 struct platform_device
*child
;
937 int rc
, didx
, child_idx
= 0;
939 dev_dbg(dev
, "Found sbefifo device\n");
941 sbefifo
= kzalloc(sizeof(*sbefifo
), GFP_KERNEL
);
945 /* Grab a reference to the device (parent of our cdev), we'll drop it later */
946 if (!get_device(dev
)) {
951 sbefifo
->magic
= SBEFIFO_MAGIC
;
952 sbefifo
->fsi_dev
= fsi_dev
;
953 dev_set_drvdata(dev
, sbefifo
);
954 mutex_init(&sbefifo
->lock
);
957 * Try cleaning up the FIFO. If this fails, we still register the
958 * driver and will try cleaning things up again on the next access.
960 rc
= sbefifo_cleanup_hw(sbefifo
);
961 if (rc
&& rc
!= -ESHUTDOWN
)
962 dev_err(dev
, "Initial HW cleanup failed, will retry later\n");
964 /* Create chardev for userspace access */
965 sbefifo
->dev
.type
= &fsi_cdev_type
;
966 sbefifo
->dev
.parent
= dev
;
967 sbefifo
->dev
.release
= sbefifo_free
;
968 device_initialize(&sbefifo
->dev
);
970 /* Allocate a minor in the FSI space */
971 rc
= fsi_get_new_minor(fsi_dev
, fsi_dev_sbefifo
, &sbefifo
->dev
.devt
, &didx
);
975 dev_set_name(&sbefifo
->dev
, "sbefifo%d", didx
);
976 cdev_init(&sbefifo
->cdev
, &sbefifo_fops
);
977 rc
= cdev_device_add(&sbefifo
->cdev
, &sbefifo
->dev
);
979 dev_err(dev
, "Error %d creating char device %s\n",
980 rc
, dev_name(&sbefifo
->dev
));
984 /* Create platform devs for dts child nodes (occ, etc) */
985 for_each_available_child_of_node(dev
->of_node
, np
) {
986 snprintf(child_name
, sizeof(child_name
), "%s-dev%d",
987 dev_name(&sbefifo
->dev
), child_idx
++);
988 child
= of_platform_device_create(np
, child_name
, dev
);
990 dev_warn(dev
, "failed to create child %s dev\n",
996 fsi_free_minor(sbefifo
->dev
.devt
);
998 put_device(&sbefifo
->dev
);
1002 static int sbefifo_unregister_child(struct device
*dev
, void *data
)
1004 struct platform_device
*child
= to_platform_device(dev
);
1006 of_device_unregister(child
);
1008 of_node_clear_flag(dev
->of_node
, OF_POPULATED
);
1013 static int sbefifo_remove(struct device
*dev
)
1015 struct sbefifo
*sbefifo
= dev_get_drvdata(dev
);
1017 dev_dbg(dev
, "Removing sbefifo device...\n");
1019 mutex_lock(&sbefifo
->lock
);
1020 sbefifo
->dead
= true;
1021 mutex_unlock(&sbefifo
->lock
);
1023 cdev_device_del(&sbefifo
->cdev
, &sbefifo
->dev
);
1024 fsi_free_minor(sbefifo
->dev
.devt
);
1025 device_for_each_child(dev
, NULL
, sbefifo_unregister_child
);
1026 put_device(&sbefifo
->dev
);
1031 static const struct fsi_device_id sbefifo_ids
[] = {
1033 .engine_type
= FSI_ENGID_SBE
,
1034 .version
= FSI_VERSION_ANY
,
1039 static struct fsi_driver sbefifo_drv
= {
1040 .id_table
= sbefifo_ids
,
1042 .name
= DEVICE_NAME
,
1043 .bus
= &fsi_bus_type
,
1044 .probe
= sbefifo_probe
,
1045 .remove
= sbefifo_remove
,
1049 static int sbefifo_init(void)
1051 return fsi_driver_register(&sbefifo_drv
);
1054 static void sbefifo_exit(void)
1056 fsi_driver_unregister(&sbefifo_drv
);
1059 module_init(sbefifo_init
);
1060 module_exit(sbefifo_exit
);
1061 MODULE_LICENSE("GPL");
1062 MODULE_AUTHOR("Brad Bishop <bradleyb@fuzziesquirrel.com>");
1063 MODULE_AUTHOR("Eddie James <eajames@linux.vnet.ibm.com>");
1064 MODULE_AUTHOR("Andrew Jeffery <andrew@aj.id.au>");
1065 MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
1066 MODULE_DESCRIPTION("Linux device interface to the POWER Self Boot Engine");