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>
35 #include <uapi/linux/fsi.h>
38 * The SBEFIFO is a pipe-like FSI device for communicating with
39 * the self boot engine on POWER processors.
42 #define DEVICE_NAME "sbefifo"
43 #define FSI_ENGID_SBE 0x22
50 #define SBEFIFO_UP 0x00 /* FSI -> Host */
51 #define SBEFIFO_DOWN 0x40 /* Host -> FSI */
53 /* Per-bank registers */
54 #define SBEFIFO_FIFO 0x00 /* The FIFO itself */
55 #define SBEFIFO_STS 0x04 /* Status register */
56 #define SBEFIFO_STS_PARITY_ERR 0x20000000
57 #define SBEFIFO_STS_RESET_REQ 0x02000000
58 #define SBEFIFO_STS_GOT_EOT 0x00800000
59 #define SBEFIFO_STS_MAX_XFER_LIMIT 0x00400000
60 #define SBEFIFO_STS_FULL 0x00200000
61 #define SBEFIFO_STS_EMPTY 0x00100000
62 #define SBEFIFO_STS_ECNT_MASK 0x000f0000
63 #define SBEFIFO_STS_ECNT_SHIFT 16
64 #define SBEFIFO_STS_VALID_MASK 0x0000ff00
65 #define SBEFIFO_STS_VALID_SHIFT 8
66 #define SBEFIFO_STS_EOT_MASK 0x000000ff
67 #define SBEFIFO_STS_EOT_SHIFT 0
68 #define SBEFIFO_EOT_RAISE 0x08 /* (Up only) Set End Of Transfer */
69 #define SBEFIFO_REQ_RESET 0x0C /* (Up only) Reset Request */
70 #define SBEFIFO_PERFORM_RESET 0x10 /* (Down only) Perform Reset */
71 #define SBEFIFO_EOT_ACK 0x14 /* (Down only) Acknowledge EOT */
72 #define SBEFIFO_DOWN_MAX 0x18 /* (Down only) Max transfer */
74 /* CFAM GP Mailbox SelfBoot Message register */
75 #define CFAM_GP_MBOX_SBM_ADDR 0x2824 /* Converted 0x2809 */
77 #define CFAM_SBM_SBE_BOOTED 0x80000000
78 #define CFAM_SBM_SBE_ASYNC_FFDC 0x40000000
79 #define CFAM_SBM_SBE_STATE_MASK 0x00f00000
80 #define CFAM_SBM_SBE_STATE_SHIFT 20
84 SBE_STATE_UNKNOWN = 0x0, // Unkown, initial state
85 SBE_STATE_IPLING = 0x1, // IPL'ing - autonomous mode (transient)
86 SBE_STATE_ISTEP = 0x2, // ISTEP - Running IPL by steps (transient)
87 SBE_STATE_MPIPL = 0x3, // MPIPL
88 SBE_STATE_RUNTIME = 0x4, // SBE Runtime
89 SBE_STATE_DMT = 0x5, // Dead Man Timer State (transient)
90 SBE_STATE_DUMP = 0x6, // Dumping
91 SBE_STATE_FAILURE = 0x7, // Internal SBE failure
92 SBE_STATE_QUIESCE = 0x8, // Final state - needs SBE reset to get out
96 #define SBEFIFO_FIFO_DEPTH 8
99 #define sbefifo_empty(sts) ((sts) & SBEFIFO_STS_EMPTY)
100 #define sbefifo_full(sts) ((sts) & SBEFIFO_STS_FULL)
101 #define sbefifo_parity_err(sts) ((sts) & SBEFIFO_STS_PARITY_ERR)
102 #define sbefifo_populated(sts) (((sts) & SBEFIFO_STS_ECNT_MASK) >> SBEFIFO_STS_ECNT_SHIFT)
103 #define sbefifo_vacant(sts) (SBEFIFO_FIFO_DEPTH - sbefifo_populated(sts))
104 #define sbefifo_eot_set(sts) (((sts) & SBEFIFO_STS_EOT_MASK) >> SBEFIFO_STS_EOT_SHIFT)
106 /* Reset request timeout in ms */
107 #define SBEFIFO_RESET_TIMEOUT 10000
109 /* Timeouts for commands in ms */
110 #define SBEFIFO_TIMEOUT_START_CMD 10000
111 #define SBEFIFO_TIMEOUT_IN_CMD 1000
112 #define SBEFIFO_TIMEOUT_START_RSP 10000
113 #define SBEFIFO_TIMEOUT_IN_RSP 1000
115 /* Other constants */
116 #define SBEFIFO_MAX_USER_CMD_LEN (0x100000 + PAGE_SIZE)
117 #define SBEFIFO_RESET_MAGIC 0x52534554 /* "RSET" */
121 #define SBEFIFO_MAGIC 0x53424546 /* "SBEF" */
122 struct fsi_device *fsi_dev;
130 u32 timeout_start_rsp_ms;
133 struct sbefifo_user {
134 struct sbefifo *sbefifo;
135 struct mutex file_lock;
142 static DEFINE_MUTEX(sbefifo_ffdc_mutex);
144 static ssize_t timeout_show(struct device *dev, struct device_attribute *attr,
147 struct sbefifo *sbefifo = container_of(dev, struct sbefifo, dev);
149 return sysfs_emit(buf, "%d\n", sbefifo->timed_out ? 1 : 0);
151 static DEVICE_ATTR_RO(timeout);
153 static void __sbefifo_dump_ffdc(struct device *dev, const __be32 *ffdc,
154 size_t ffdc_sz, bool internal)
157 #define FFDC_LSIZE 60
158 static char ffdc_line[FFDC_LSIZE];
164 dev_err(dev, "SBE invalid FFDC package size %zd\n", ffdc_sz);
167 w0 = be32_to_cpu(*(ffdc++));
168 w1 = be32_to_cpu(*(ffdc++));
169 w2 = be32_to_cpu(*(ffdc++));
171 if ((w0 >> 16) != 0xFFDC) {
172 dev_err(dev, "SBE invalid FFDC package signature %08x %08x %08x\n",
178 dev_err(dev, "SBE FFDC package len %d words but only %zd remaining\n",
184 dev_warn(dev, "+---- SBE FFDC package %d for async err -----+\n",
187 dev_warn(dev, "+---- SBE FFDC package %d for cmd %02x:%02x -----+\n",
188 pack++, (w1 >> 8) & 0xff, w1 & 0xff);
190 dev_warn(dev, "| Response code: %08x |\n", w2);
191 dev_warn(dev, "|-------------------------------------------|\n");
192 for (i = 0; i < w0; i++) {
195 p += sprintf(p, "| %04x:", i << 4);
197 p += sprintf(p, " %08x", be32_to_cpu(*(ffdc++)));
199 if ((i & 3) == 3 || i == (w0 - 1)) {
200 while ((i & 3) < 3) {
201 p += sprintf(p, " ");
204 dev_warn(dev, "%s |\n", ffdc_line);
207 dev_warn(dev, "+-------------------------------------------+\n");
211 static void sbefifo_dump_ffdc(struct device *dev, const __be32 *ffdc,
212 size_t ffdc_sz, bool internal)
214 mutex_lock(&sbefifo_ffdc_mutex);
215 __sbefifo_dump_ffdc(dev, ffdc, ffdc_sz, internal);
216 mutex_unlock(&sbefifo_ffdc_mutex);
219 int sbefifo_parse_status(struct device *dev, u16 cmd, __be32 *response,
220 size_t resp_len, size_t *data_len)
226 pr_debug("sbefifo: cmd %04x, response too small: %zd\n",
230 dh = be32_to_cpu(response[resp_len - 1]);
231 if (dh > resp_len || dh < 3) {
232 dev_err(dev, "SBE cmd %02x:%02x status offset out of range: %d/%zd\n",
233 cmd >> 8, cmd & 0xff, dh, resp_len);
236 s0 = be32_to_cpu(response[resp_len - dh]);
237 s1 = be32_to_cpu(response[resp_len - dh + 1]);
238 if (((s0 >> 16) != 0xC0DE) || ((s0 & 0xffff) != cmd)) {
239 dev_err(dev, "SBE cmd %02x:%02x, status signature invalid: 0x%08x 0x%08x\n",
240 cmd >> 8, cmd & 0xff, s0, s1);
245 dev_warn(dev, "SBE error cmd %02x:%02x status=%04x:%04x\n",
246 cmd >> 8, cmd & 0xff, s1 >> 16, s1 & 0xffff);
248 sbefifo_dump_ffdc(dev, &response[resp_len - dh + 2],
252 *data_len = resp_len - dh;
255 * Primary status don't have the top bit set, so can't be confused with
256 * Linux negative error codes, so return the status word whole.
260 EXPORT_SYMBOL_GPL(sbefifo_parse_status);
262 static int sbefifo_regr(struct sbefifo *sbefifo, int reg, u32 *word)
267 rc = fsi_device_read(sbefifo->fsi_dev, reg, &raw_word,
272 *word = be32_to_cpu(raw_word);
277 static int sbefifo_regw(struct sbefifo *sbefifo, int reg, u32 word)
279 __be32 raw_word = cpu_to_be32(word);
281 return fsi_device_write(sbefifo->fsi_dev, reg, &raw_word,
285 static int sbefifo_check_sbe_state(struct sbefifo *sbefifo)
291 rc = fsi_slave_read(sbefifo->fsi_dev->slave, CFAM_GP_MBOX_SBM_ADDR,
292 &raw_word, sizeof(raw_word));
295 sbm = be32_to_cpu(raw_word);
297 /* SBE booted at all ? */
298 if (!(sbm & CFAM_SBM_SBE_BOOTED))
301 /* Check its state */
302 switch ((sbm & CFAM_SBM_SBE_STATE_MASK) >> CFAM_SBM_SBE_STATE_SHIFT) {
303 case SBE_STATE_UNKNOWN:
307 case SBE_STATE_IPLING:
308 case SBE_STATE_ISTEP:
309 case SBE_STATE_MPIPL:
310 case SBE_STATE_RUNTIME:
311 case SBE_STATE_DUMP: /* Not sure about that one */
313 case SBE_STATE_FAILURE:
314 case SBE_STATE_QUIESCE:
318 /* Is there async FFDC available ? Remember it */
319 if (sbm & CFAM_SBM_SBE_ASYNC_FFDC)
320 sbefifo->async_ffdc = true;
325 /* Don't flip endianness of data to/from FIFO, just pass through. */
326 static int sbefifo_down_read(struct sbefifo *sbefifo, __be32 *word)
328 return fsi_device_read(sbefifo->fsi_dev, SBEFIFO_DOWN, word,
332 static int sbefifo_up_write(struct sbefifo *sbefifo, __be32 word)
334 return fsi_device_write(sbefifo->fsi_dev, SBEFIFO_UP, &word,
338 static int sbefifo_request_reset(struct sbefifo *sbefifo)
340 struct device *dev = &sbefifo->fsi_dev->dev;
341 unsigned long end_time;
345 dev_dbg(dev, "Requesting FIFO reset\n");
347 /* Mark broken first, will be cleared if reset succeeds */
348 sbefifo->broken = true;
350 /* Send reset request */
351 rc = sbefifo_regw(sbefifo, SBEFIFO_UP | SBEFIFO_REQ_RESET, 1);
353 dev_err(dev, "Sending reset request failed, rc=%d\n", rc);
357 /* Wait for it to complete */
358 end_time = jiffies + msecs_to_jiffies(SBEFIFO_RESET_TIMEOUT);
359 while (!time_after(jiffies, end_time)) {
360 rc = sbefifo_regr(sbefifo, SBEFIFO_UP | SBEFIFO_STS, &status);
362 dev_err(dev, "Failed to read UP fifo status during reset"
367 if (!(status & SBEFIFO_STS_RESET_REQ)) {
368 dev_dbg(dev, "FIFO reset done\n");
369 sbefifo->broken = false;
375 dev_err(dev, "FIFO reset timed out\n");
380 static int sbefifo_cleanup_hw(struct sbefifo *sbefifo)
382 struct device *dev = &sbefifo->fsi_dev->dev;
383 u32 up_status, down_status;
384 bool need_reset = false;
387 rc = sbefifo_check_sbe_state(sbefifo);
389 dev_dbg(dev, "SBE state=%d\n", rc);
393 /* If broken, we don't need to look at status, go straight to reset */
397 rc = sbefifo_regr(sbefifo, SBEFIFO_UP | SBEFIFO_STS, &up_status);
399 dev_err(dev, "Cleanup: Reading UP status failed, rc=%d\n", rc);
401 /* Will try reset again on next attempt at using it */
402 sbefifo->broken = true;
406 rc = sbefifo_regr(sbefifo, SBEFIFO_DOWN | SBEFIFO_STS, &down_status);
408 dev_err(dev, "Cleanup: Reading DOWN status failed, rc=%d\n", rc);
410 /* Will try reset again on next attempt at using it */
411 sbefifo->broken = true;
415 /* The FIFO already contains a reset request from the SBE ? */
416 if (down_status & SBEFIFO_STS_RESET_REQ) {
417 dev_info(dev, "Cleanup: FIFO reset request set, resetting\n");
418 rc = sbefifo_regw(sbefifo, SBEFIFO_DOWN, SBEFIFO_PERFORM_RESET);
420 sbefifo->broken = true;
421 dev_err(dev, "Cleanup: Reset reg write failed, rc=%d\n", rc);
424 sbefifo->broken = false;
428 /* Parity error on either FIFO ? */
429 if ((up_status | down_status) & SBEFIFO_STS_PARITY_ERR)
432 /* Either FIFO not empty ? */
433 if (!((up_status & down_status) & SBEFIFO_STS_EMPTY))
439 dev_info(dev, "Cleanup: FIFO not clean (up=0x%08x down=0x%08x)\n",
440 up_status, down_status);
444 /* Mark broken, will be cleared if/when reset succeeds */
445 return sbefifo_request_reset(sbefifo);
448 static int sbefifo_wait(struct sbefifo *sbefifo, bool up,
449 u32 *status, unsigned long timeout)
451 struct device *dev = &sbefifo->fsi_dev->dev;
452 unsigned long end_time;
457 dev_vdbg(dev, "Wait on %s fifo...\n", up ? "up" : "down");
459 addr = (up ? SBEFIFO_UP : SBEFIFO_DOWN) | SBEFIFO_STS;
461 end_time = jiffies + timeout;
462 while (!time_after(jiffies, end_time)) {
464 rc = sbefifo_regr(sbefifo, addr, &sts);
466 dev_err(dev, "FSI error %d reading status register\n", rc);
469 if (!up && sbefifo_parity_err(sts)) {
470 dev_err(dev, "Parity error in DOWN FIFO\n");
473 ready = !(up ? sbefifo_full(sts) : sbefifo_empty(sts));
478 sysfs_notify(&sbefifo->dev.kobj, NULL, dev_attr_timeout.attr.name);
479 sbefifo->timed_out = true;
480 dev_err(dev, "%s FIFO Timeout ! status=%08x\n", up ? "UP" : "DOWN", sts);
483 dev_vdbg(dev, "End of wait status: %08x\n", sts);
485 sbefifo->timed_out = false;
491 static int sbefifo_send_command(struct sbefifo *sbefifo,
492 const __be32 *command, size_t cmd_len)
494 struct device *dev = &sbefifo->fsi_dev->dev;
495 size_t len, chunk, vacant = 0, remaining = cmd_len;
496 unsigned long timeout;
500 dev_vdbg(dev, "sending command (%zd words, cmd=%04x)\n",
501 cmd_len, be32_to_cpu(command[1]));
503 /* As long as there's something to send */
504 timeout = msecs_to_jiffies(SBEFIFO_TIMEOUT_START_CMD);
506 /* Wait for room in the FIFO */
507 rc = sbefifo_wait(sbefifo, true, &status, timeout);
510 timeout = msecs_to_jiffies(SBEFIFO_TIMEOUT_IN_CMD);
512 vacant = sbefifo_vacant(status);
513 len = chunk = min(vacant, remaining);
515 dev_vdbg(dev, " status=%08x vacant=%zd chunk=%zd\n",
516 status, vacant, chunk);
518 /* Write as much as we can */
520 rc = sbefifo_up_write(sbefifo, *(command++));
522 dev_err(dev, "FSI error %d writing UP FIFO\n", rc);
530 /* If there's no room left, wait for some to write EOT */
532 rc = sbefifo_wait(sbefifo, true, &status, timeout);
538 rc = sbefifo_regw(sbefifo, SBEFIFO_UP | SBEFIFO_EOT_RAISE, 0);
540 dev_err(dev, "FSI error %d writing EOT\n", rc);
544 static int sbefifo_read_response(struct sbefifo *sbefifo, struct iov_iter *response)
546 struct device *dev = &sbefifo->fsi_dev->dev;
548 unsigned long timeout;
549 bool overflow = false;
554 dev_vdbg(dev, "reading response, buflen = %zd\n", iov_iter_count(response));
556 timeout = msecs_to_jiffies(sbefifo->timeout_start_rsp_ms);
558 /* Grab FIFO status (this will handle parity errors) */
559 rc = sbefifo_wait(sbefifo, false, &status, timeout);
562 timeout = msecs_to_jiffies(SBEFIFO_TIMEOUT_IN_RSP);
565 len = sbefifo_populated(status);
566 eot_set = sbefifo_eot_set(status);
568 dev_vdbg(dev, " chunk size %zd eot_set=0x%x\n", len, eot_set);
570 /* Go through the chunk */
573 rc = sbefifo_down_read(sbefifo, &data);
577 /* Was it an EOT ? */
578 if (eot_set & 0x80) {
580 * There should be nothing else in the FIFO,
581 * if there is, mark broken, this will force
582 * a reset on next use, but don't fail the
586 dev_warn(dev, "FIFO read hit"
587 " EOT with still %zd data\n",
589 sbefifo->broken = true;
593 rc = sbefifo_regw(sbefifo,
594 SBEFIFO_DOWN | SBEFIFO_EOT_ACK, 0);
597 * If that write fail, still complete the request but mark
598 * the fifo as broken for subsequent reset (not much else
602 dev_err(dev, "FSI error %d ack'ing EOT\n", rc);
603 sbefifo->broken = true;
606 /* Tell whether we overflowed */
607 return overflow ? -EOVERFLOW : 0;
610 /* Store it if there is room */
611 if (iov_iter_count(response) >= sizeof(__be32)) {
612 if (copy_to_iter(&data, sizeof(__be32), response) < sizeof(__be32))
615 dev_vdbg(dev, "Response overflowed !\n");
624 /* Shouldn't happen */
628 static int sbefifo_do_command(struct sbefifo *sbefifo,
629 const __be32 *command, size_t cmd_len,
630 struct iov_iter *response)
632 /* Try sending the command */
633 int rc = sbefifo_send_command(sbefifo, command, cmd_len);
637 /* Now, get the response */
638 return sbefifo_read_response(sbefifo, response);
641 static void sbefifo_collect_async_ffdc(struct sbefifo *sbefifo)
643 struct device *dev = &sbefifo->fsi_dev->dev;
644 struct iov_iter ffdc_iter;
645 struct kvec ffdc_iov;
651 sbefifo->async_ffdc = false;
652 ffdc = vmalloc(SBEFIFO_MAX_FFDC_SIZE);
654 dev_err(dev, "Failed to allocate SBE FFDC buffer\n");
657 ffdc_iov.iov_base = ffdc;
658 ffdc_iov.iov_len = SBEFIFO_MAX_FFDC_SIZE;
659 iov_iter_kvec(&ffdc_iter, WRITE, &ffdc_iov, 1, SBEFIFO_MAX_FFDC_SIZE);
660 cmd[0] = cpu_to_be32(2);
661 cmd[1] = cpu_to_be32(SBEFIFO_CMD_GET_SBE_FFDC);
662 rc = sbefifo_do_command(sbefifo, cmd, 2, &ffdc_iter);
664 dev_err(dev, "Error %d retrieving SBE FFDC\n", rc);
667 ffdc_sz = SBEFIFO_MAX_FFDC_SIZE - iov_iter_count(&ffdc_iter);
668 ffdc_sz /= sizeof(__be32);
669 rc = sbefifo_parse_status(dev, SBEFIFO_CMD_GET_SBE_FFDC, ffdc,
672 dev_err(dev, "Error %d decoding SBE FFDC\n", rc);
676 sbefifo_dump_ffdc(dev, ffdc, ffdc_sz, true);
682 static int __sbefifo_submit(struct sbefifo *sbefifo,
683 const __be32 *command, size_t cmd_len,
684 struct iov_iter *response)
686 struct device *dev = &sbefifo->fsi_dev->dev;
692 if (cmd_len < 2 || be32_to_cpu(command[0]) != cmd_len) {
693 dev_vdbg(dev, "Invalid command len %zd (header: %d)\n",
694 cmd_len, be32_to_cpu(command[0]));
698 /* First ensure the HW is in a clean state */
699 rc = sbefifo_cleanup_hw(sbefifo);
703 /* Look for async FFDC first if any */
704 if (sbefifo->async_ffdc)
705 sbefifo_collect_async_ffdc(sbefifo);
707 rc = sbefifo_do_command(sbefifo, command, cmd_len, response);
708 if (rc != 0 && rc != -EOVERFLOW)
713 * On failure, attempt a reset. Ignore the result, it will mark
714 * the fifo broken if the reset fails
716 sbefifo_request_reset(sbefifo);
718 /* Return original error */
723 * sbefifo_submit() - Submit and SBE fifo command and receive response
724 * @dev: The sbefifo device
725 * @command: The raw command data
726 * @cmd_len: The command size (in 32-bit words)
727 * @response: The output response buffer
728 * @resp_len: In: Response buffer size, Out: Response size
730 * This will perform the entire operation. If the reponse buffer
731 * overflows, returns -EOVERFLOW
733 int sbefifo_submit(struct device *dev, const __be32 *command, size_t cmd_len,
734 __be32 *response, size_t *resp_len)
736 struct sbefifo *sbefifo;
737 struct iov_iter resp_iter;
738 struct kvec resp_iov;
744 sbefifo = dev_get_drvdata(dev);
747 if (WARN_ON_ONCE(sbefifo->magic != SBEFIFO_MAGIC))
749 if (!resp_len || !command || !response)
752 /* Prepare iov iterator */
753 rbytes = (*resp_len) * sizeof(__be32);
754 resp_iov.iov_base = response;
755 resp_iov.iov_len = rbytes;
756 iov_iter_kvec(&resp_iter, WRITE, &resp_iov, 1, rbytes);
758 /* Perform the command */
759 rc = mutex_lock_interruptible(&sbefifo->lock);
762 rc = __sbefifo_submit(sbefifo, command, cmd_len, &resp_iter);
763 mutex_unlock(&sbefifo->lock);
765 /* Extract the response length */
766 rbytes -= iov_iter_count(&resp_iter);
767 *resp_len = rbytes / sizeof(__be32);
771 EXPORT_SYMBOL_GPL(sbefifo_submit);
774 * Char device interface
777 static void sbefifo_release_command(struct sbefifo_user *user)
779 if (is_vmalloc_addr(user->pending_cmd))
780 vfree(user->pending_cmd);
781 user->pending_cmd = NULL;
782 user->pending_len = 0;
785 static int sbefifo_user_open(struct inode *inode, struct file *file)
787 struct sbefifo *sbefifo = container_of(inode->i_cdev, struct sbefifo, cdev);
788 struct sbefifo_user *user;
790 user = kzalloc(sizeof(struct sbefifo_user), GFP_KERNEL);
794 file->private_data = user;
795 user->sbefifo = sbefifo;
796 user->cmd_page = (void *)__get_free_page(GFP_KERNEL);
797 if (!user->cmd_page) {
801 mutex_init(&user->file_lock);
802 user->read_timeout_ms = SBEFIFO_TIMEOUT_START_RSP;
807 static ssize_t sbefifo_user_read(struct file *file, char __user *buf,
808 size_t len, loff_t *offset)
810 struct sbefifo_user *user = file->private_data;
811 struct sbefifo *sbefifo;
812 struct iov_iter resp_iter;
813 struct iovec resp_iov;
819 sbefifo = user->sbefifo;
823 mutex_lock(&user->file_lock);
825 /* Cronus relies on -EAGAIN after a short read */
826 if (user->pending_len == 0) {
830 if (user->pending_len < 8) {
834 cmd_len = user->pending_len >> 2;
836 /* Prepare iov iterator */
837 resp_iov.iov_base = buf;
838 resp_iov.iov_len = len;
839 iov_iter_init(&resp_iter, WRITE, &resp_iov, 1, len);
841 /* Perform the command */
842 rc = mutex_lock_interruptible(&sbefifo->lock);
845 sbefifo->timeout_start_rsp_ms = user->read_timeout_ms;
846 rc = __sbefifo_submit(sbefifo, user->pending_cmd, cmd_len, &resp_iter);
847 sbefifo->timeout_start_rsp_ms = SBEFIFO_TIMEOUT_START_RSP;
848 mutex_unlock(&sbefifo->lock);
852 /* Extract the response length */
853 rc = len - iov_iter_count(&resp_iter);
855 sbefifo_release_command(user);
856 mutex_unlock(&user->file_lock);
860 static ssize_t sbefifo_user_write(struct file *file, const char __user *buf,
861 size_t len, loff_t *offset)
863 struct sbefifo_user *user = file->private_data;
864 struct sbefifo *sbefifo;
869 sbefifo = user->sbefifo;
870 if (len > SBEFIFO_MAX_USER_CMD_LEN)
875 mutex_lock(&user->file_lock);
877 /* Can we use the pre-allocate buffer ? If not, allocate */
878 if (len <= PAGE_SIZE)
879 user->pending_cmd = user->cmd_page;
881 user->pending_cmd = vmalloc(len);
882 if (!user->pending_cmd) {
887 /* Copy the command into the staging buffer */
888 if (copy_from_user(user->pending_cmd, buf, len)) {
893 /* Check for the magic reset command */
894 if (len == 4 && be32_to_cpu(*(__be32 *)user->pending_cmd) ==
895 SBEFIFO_RESET_MAGIC) {
897 /* Clear out any pending command */
898 user->pending_len = 0;
900 /* Trigger reset request */
901 rc = mutex_lock_interruptible(&sbefifo->lock);
904 rc = sbefifo_request_reset(user->sbefifo);
905 mutex_unlock(&sbefifo->lock);
911 /* Update the staging buffer size */
912 user->pending_len = len;
914 if (!user->pending_len)
915 sbefifo_release_command(user);
917 mutex_unlock(&user->file_lock);
919 /* And that's it, we'll issue the command on a read */
923 static int sbefifo_user_release(struct inode *inode, struct file *file)
925 struct sbefifo_user *user = file->private_data;
930 sbefifo_release_command(user);
931 free_page((unsigned long)user->cmd_page);
937 static int sbefifo_read_timeout(struct sbefifo_user *user, void __user *argp)
939 struct device *dev = &user->sbefifo->dev;
942 if (get_user(timeout, (__u32 __user *)argp))
946 user->read_timeout_ms = SBEFIFO_TIMEOUT_START_RSP;
947 dev_dbg(dev, "Timeout reset to %d\n", user->read_timeout_ms);
951 if (timeout < 10 || timeout > 120)
954 user->read_timeout_ms = timeout * 1000; /* user timeout is in sec */
956 dev_dbg(dev, "Timeout set to %d\n", user->read_timeout_ms);
961 static long sbefifo_user_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
963 struct sbefifo_user *user = file->private_data;
969 mutex_lock(&user->file_lock);
971 case FSI_SBEFIFO_READ_TIMEOUT_SECONDS:
972 rc = sbefifo_read_timeout(user, (void __user *)arg);
975 mutex_unlock(&user->file_lock);
979 static const struct file_operations sbefifo_fops = {
980 .owner = THIS_MODULE,
981 .open = sbefifo_user_open,
982 .read = sbefifo_user_read,
983 .write = sbefifo_user_write,
984 .release = sbefifo_user_release,
985 .unlocked_ioctl = sbefifo_user_ioctl,
988 static void sbefifo_free(struct device *dev)
990 struct sbefifo *sbefifo = container_of(dev, struct sbefifo, dev);
992 put_device(&sbefifo->fsi_dev->dev);
1000 static int sbefifo_probe(struct device *dev)
1002 struct fsi_device *fsi_dev = to_fsi_dev(dev);
1003 struct sbefifo *sbefifo;
1004 struct device_node *np;
1005 struct platform_device *child;
1006 char child_name[32];
1007 int rc, didx, child_idx = 0;
1009 dev_dbg(dev, "Found sbefifo device\n");
1011 sbefifo = kzalloc(sizeof(*sbefifo), GFP_KERNEL);
1015 /* Grab a reference to the device (parent of our cdev), we'll drop it later */
1016 if (!get_device(dev)) {
1021 sbefifo->magic = SBEFIFO_MAGIC;
1022 sbefifo->fsi_dev = fsi_dev;
1023 dev_set_drvdata(dev, sbefifo);
1024 mutex_init(&sbefifo->lock);
1025 sbefifo->timeout_start_rsp_ms = SBEFIFO_TIMEOUT_START_RSP;
1028 * Try cleaning up the FIFO. If this fails, we still register the
1029 * driver and will try cleaning things up again on the next access.
1031 rc = sbefifo_cleanup_hw(sbefifo);
1032 if (rc && rc != -ESHUTDOWN)
1033 dev_err(dev, "Initial HW cleanup failed, will retry later\n");
1035 /* Create chardev for userspace access */
1036 sbefifo->dev.type = &fsi_cdev_type;
1037 sbefifo->dev.parent = dev;
1038 sbefifo->dev.release = sbefifo_free;
1039 device_initialize(&sbefifo->dev);
1041 /* Allocate a minor in the FSI space */
1042 rc = fsi_get_new_minor(fsi_dev, fsi_dev_sbefifo, &sbefifo->dev.devt, &didx);
1046 dev_set_name(&sbefifo->dev, "sbefifo%d", didx);
1047 cdev_init(&sbefifo->cdev, &sbefifo_fops);
1048 rc = cdev_device_add(&sbefifo->cdev, &sbefifo->dev);
1050 dev_err(dev, "Error %d creating char device %s\n",
1051 rc, dev_name(&sbefifo->dev));
1052 goto err_free_minor;
1055 /* Create platform devs for dts child nodes (occ, etc) */
1056 for_each_available_child_of_node(dev->of_node, np) {
1057 snprintf(child_name, sizeof(child_name), "%s-dev%d",
1058 dev_name(&sbefifo->dev), child_idx++);
1059 child = of_platform_device_create(np, child_name, dev);
1061 dev_warn(dev, "failed to create child %s dev\n",
1065 device_create_file(&sbefifo->dev, &dev_attr_timeout);
1069 fsi_free_minor(sbefifo->dev.devt);
1071 put_device(&sbefifo->dev);
1075 static int sbefifo_unregister_child(struct device *dev, void *data)
1077 struct platform_device *child = to_platform_device(dev);
1079 of_device_unregister(child);
1081 of_node_clear_flag(dev->of_node, OF_POPULATED);
1086 static int sbefifo_remove(struct device *dev)
1088 struct sbefifo *sbefifo = dev_get_drvdata(dev);
1090 dev_dbg(dev, "Removing sbefifo device...\n");
1092 device_remove_file(&sbefifo->dev, &dev_attr_timeout);
1094 mutex_lock(&sbefifo->lock);
1095 sbefifo->dead = true;
1096 mutex_unlock(&sbefifo->lock);
1098 cdev_device_del(&sbefifo->cdev, &sbefifo->dev);
1099 fsi_free_minor(sbefifo->dev.devt);
1100 device_for_each_child(dev, NULL, sbefifo_unregister_child);
1101 put_device(&sbefifo->dev);
1106 static const struct fsi_device_id sbefifo_ids[] = {
1108 .engine_type = FSI_ENGID_SBE,
1109 .version = FSI_VERSION_ANY,
1114 static struct fsi_driver sbefifo_drv = {
1115 .id_table = sbefifo_ids,
1117 .name = DEVICE_NAME,
1118 .bus = &fsi_bus_type,
1119 .probe = sbefifo_probe,
1120 .remove = sbefifo_remove,
1124 static int sbefifo_init(void)
1126 return fsi_driver_register(&sbefifo_drv);
1129 static void sbefifo_exit(void)
1131 fsi_driver_unregister(&sbefifo_drv);
1134 module_init(sbefifo_init);
1135 module_exit(sbefifo_exit);
1136 MODULE_LICENSE("GPL");
1137 MODULE_AUTHOR("Brad Bishop <bradleyb@fuzziesquirrel.com>");
1138 MODULE_AUTHOR("Eddie James <eajames@linux.vnet.ibm.com>");
1139 MODULE_AUTHOR("Andrew Jeffery <andrew@aj.id.au>");
1140 MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
1141 MODULE_DESCRIPTION("Linux device interface to the POWER Self Boot Engine");