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;
328 unsigned long end_time;
332 dev_dbg(dev, "Requesting FIFO reset\n");
334 /* Mark broken first, will be cleared if reset succeeds */
335 sbefifo->broken = true;
337 /* Send reset request */
338 rc = sbefifo_regw(sbefifo, SBEFIFO_UP | SBEFIFO_REQ_RESET, 1);
340 dev_err(dev, "Sending reset request failed, rc=%d\n", rc);
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);
349 dev_err(dev, "Failed to read UP fifo status during reset"
354 if (!(status & SBEFIFO_STS_RESET_REQ)) {
355 dev_dbg(dev, "FIFO reset done\n");
356 sbefifo->broken = false;
362 dev_err(dev, "FIFO reset timed out\n");
367 static int sbefifo_cleanup_hw(struct sbefifo *sbefifo)
369 struct device *dev = &sbefifo->fsi_dev->dev;
370 u32 up_status, down_status;
371 bool need_reset = false;
374 rc = sbefifo_check_sbe_state(sbefifo);
376 dev_dbg(dev, "SBE state=%d\n", rc);
380 /* If broken, we don't need to look at status, go straight to reset */
384 rc = sbefifo_regr(sbefifo, SBEFIFO_UP | SBEFIFO_STS, &up_status);
386 dev_err(dev, "Cleanup: Reading UP status failed, rc=%d\n", rc);
388 /* Will try reset again on next attempt at using it */
389 sbefifo->broken = true;
393 rc = sbefifo_regr(sbefifo, SBEFIFO_DOWN | SBEFIFO_STS, &down_status);
395 dev_err(dev, "Cleanup: Reading DOWN status failed, rc=%d\n", rc);
397 /* Will try reset again on next attempt at using it */
398 sbefifo->broken = true;
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);
407 sbefifo->broken = true;
408 dev_err(dev, "Cleanup: Reset reg write failed, rc=%d\n", rc);
411 sbefifo->broken = false;
415 /* Parity error on either FIFO ? */
416 if ((up_status | down_status) & SBEFIFO_STS_PARITY_ERR)
419 /* Either FIFO not empty ? */
420 if (!((up_status & down_status) & SBEFIFO_STS_EMPTY))
426 dev_info(dev, "Cleanup: FIFO not clean (up=0x%08x down=0x%08x)\n",
427 up_status, down_status);
431 /* Mark broken, will be cleared if/when reset succeeds */
432 return sbefifo_request_reset(sbefifo);
435 static int sbefifo_wait(struct sbefifo *sbefifo, bool up,
436 u32 *status, unsigned long timeout)
438 struct device *dev = &sbefifo->fsi_dev->dev;
439 unsigned long end_time;
444 dev_vdbg(dev, "Wait on %s fifo...\n", up ? "up" : "down");
446 addr = (up ? SBEFIFO_UP : SBEFIFO_DOWN) | SBEFIFO_STS;
448 end_time = jiffies + timeout;
449 while (!time_after(jiffies, end_time)) {
451 rc = sbefifo_regr(sbefifo, addr, &sts);
453 dev_err(dev, "FSI error %d reading status register\n", rc);
456 if (!up && sbefifo_parity_err(sts)) {
457 dev_err(dev, "Parity error in DOWN FIFO\n");
460 ready = !(up ? sbefifo_full(sts) : sbefifo_empty(sts));
465 dev_err(dev, "%s FIFO Timeout ! status=%08x\n", up ? "UP" : "DOWN", sts);
468 dev_vdbg(dev, "End of wait status: %08x\n", sts);
475 static int sbefifo_send_command(struct sbefifo *sbefifo,
476 const __be32 *command, size_t cmd_len)
478 struct device *dev = &sbefifo->fsi_dev->dev;
479 size_t len, chunk, vacant = 0, remaining = cmd_len;
480 unsigned long timeout;
484 dev_vdbg(dev, "sending command (%zd words, cmd=%04x)\n",
485 cmd_len, be32_to_cpu(command[1]));
487 /* As long as there's something to send */
488 timeout = msecs_to_jiffies(SBEFIFO_TIMEOUT_START_CMD);
490 /* Wait for room in the FIFO */
491 rc = sbefifo_wait(sbefifo, true, &status, timeout);
494 timeout = msecs_to_jiffies(SBEFIFO_TIMEOUT_IN_CMD);
496 vacant = sbefifo_vacant(status);
497 len = chunk = min(vacant, remaining);
499 dev_vdbg(dev, " status=%08x vacant=%zd chunk=%zd\n",
500 status, vacant, chunk);
502 /* Write as much as we can */
504 rc = sbefifo_up_write(sbefifo, *(command++));
506 dev_err(dev, "FSI error %d writing UP FIFO\n", rc);
514 /* If there's no room left, wait for some to write EOT */
516 rc = sbefifo_wait(sbefifo, true, &status, timeout);
522 rc = sbefifo_regw(sbefifo, SBEFIFO_UP | SBEFIFO_EOT_RAISE, 0);
524 dev_err(dev, "FSI error %d writing EOT\n", rc);
528 static int sbefifo_read_response(struct sbefifo *sbefifo, struct iov_iter *response)
530 struct device *dev = &sbefifo->fsi_dev->dev;
532 unsigned long timeout;
533 bool overflow = false;
538 dev_vdbg(dev, "reading response, buflen = %zd\n", iov_iter_count(response));
540 timeout = msecs_to_jiffies(SBEFIFO_TIMEOUT_START_RSP);
542 /* Grab FIFO status (this will handle parity errors) */
543 rc = sbefifo_wait(sbefifo, false, &status, timeout);
546 timeout = msecs_to_jiffies(SBEFIFO_TIMEOUT_IN_RSP);
549 len = sbefifo_populated(status);
550 eot_set = sbefifo_eot_set(status);
552 dev_vdbg(dev, " chunk size %zd eot_set=0x%x\n", len, eot_set);
554 /* Go through the chunk */
557 rc = sbefifo_down_read(sbefifo, &data);
561 /* Was it an EOT ? */
562 if (eot_set & 0x80) {
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
570 dev_warn(dev, "FIFO read hit"
571 " EOT with still %zd data\n",
573 sbefifo->broken = true;
577 rc = sbefifo_regw(sbefifo,
578 SBEFIFO_DOWN | SBEFIFO_EOT_ACK, 0);
581 * If that write fail, still complete the request but mark
582 * the fifo as broken for subsequent reset (not much else
586 dev_err(dev, "FSI error %d ack'ing EOT\n", rc);
587 sbefifo->broken = true;
590 /* Tell whether we overflowed */
591 return overflow ? -EOVERFLOW : 0;
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))
599 dev_vdbg(dev, "Response overflowed !\n");
608 /* Shouldn't happen */
612 static int sbefifo_do_command(struct sbefifo *sbefifo,
613 const __be32 *command, size_t cmd_len,
614 struct iov_iter *response)
616 /* Try sending the command */
617 int rc = sbefifo_send_command(sbefifo, command, cmd_len);
621 /* Now, get the response */
622 return sbefifo_read_response(sbefifo, response);
625 static void sbefifo_collect_async_ffdc(struct sbefifo *sbefifo)
627 struct device *dev = &sbefifo->fsi_dev->dev;
628 struct iov_iter ffdc_iter;
629 struct kvec ffdc_iov;
635 sbefifo->async_ffdc = false;
636 ffdc = vmalloc(SBEFIFO_MAX_FFDC_SIZE);
638 dev_err(dev, "Failed to allocate SBE FFDC buffer\n");
641 ffdc_iov.iov_base = ffdc;
642 ffdc_iov.iov_len = SBEFIFO_MAX_FFDC_SIZE;
643 iov_iter_kvec(&ffdc_iter, WRITE, &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);
648 dev_err(dev, "Error %d retrieving SBE FFDC\n", rc);
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,
656 dev_err(dev, "Error %d decoding SBE FFDC\n", rc);
660 sbefifo_dump_ffdc(dev, ffdc, ffdc_sz, true);
666 static int __sbefifo_submit(struct sbefifo *sbefifo,
667 const __be32 *command, size_t cmd_len,
668 struct iov_iter *response)
670 struct device *dev = &sbefifo->fsi_dev->dev;
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]));
682 /* First ensure the HW is in a clean state */
683 rc = sbefifo_cleanup_hw(sbefifo);
687 /* Look for async FFDC first if any */
688 if (sbefifo->async_ffdc)
689 sbefifo_collect_async_ffdc(sbefifo);
691 rc = sbefifo_do_command(sbefifo, command, cmd_len, response);
692 if (rc != 0 && rc != -EOVERFLOW)
697 * On failure, attempt a reset. Ignore the result, it will mark
698 * the fifo broken if the reset fails
700 sbefifo_request_reset(sbefifo);
702 /* Return original error */
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
714 * This will perform the entire operation. If the reponse buffer
715 * overflows, returns -EOVERFLOW
717 int sbefifo_submit(struct device *dev, const __be32 *command, size_t cmd_len,
718 __be32 *response, size_t *resp_len)
720 struct sbefifo *sbefifo;
721 struct iov_iter resp_iter;
722 struct kvec resp_iov;
728 sbefifo = dev_get_drvdata(dev);
731 if (WARN_ON_ONCE(sbefifo->magic != SBEFIFO_MAGIC))
733 if (!resp_len || !command || !response)
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, &resp_iov, 1, rbytes);
742 /* Perform the command */
743 mutex_lock(&sbefifo->lock);
744 rc = __sbefifo_submit(sbefifo, command, cmd_len, &resp_iter);
745 mutex_unlock(&sbefifo->lock);
747 /* Extract the response length */
748 rbytes -= iov_iter_count(&resp_iter);
749 *resp_len = rbytes / sizeof(__be32);
753 EXPORT_SYMBOL_GPL(sbefifo_submit);
756 * Char device interface
759 static void sbefifo_release_command(struct sbefifo_user *user)
761 if (is_vmalloc_addr(user->pending_cmd))
762 vfree(user->pending_cmd);
763 user->pending_cmd = NULL;
764 user->pending_len = 0;
767 static int sbefifo_user_open(struct inode *inode, struct file *file)
769 struct sbefifo *sbefifo = container_of(inode->i_cdev, struct sbefifo, cdev);
770 struct sbefifo_user *user;
772 user = kzalloc(sizeof(struct sbefifo_user), GFP_KERNEL);
776 file->private_data = user;
777 user->sbefifo = sbefifo;
778 user->cmd_page = (void *)__get_free_page(GFP_KERNEL);
779 if (!user->cmd_page) {
783 mutex_init(&user->file_lock);
788 static ssize_t sbefifo_user_read(struct file *file, char __user *buf,
789 size_t len, loff_t *offset)
791 struct sbefifo_user *user = file->private_data;
792 struct sbefifo *sbefifo;
793 struct iov_iter resp_iter;
794 struct iovec resp_iov;
800 sbefifo = user->sbefifo;
804 mutex_lock(&user->file_lock);
806 /* Cronus relies on -EAGAIN after a short read */
807 if (user->pending_len == 0) {
811 if (user->pending_len < 8) {
815 cmd_len = user->pending_len >> 2;
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);
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);
829 /* Extract the response length */
830 rc = len - iov_iter_count(&resp_iter);
832 sbefifo_release_command(user);
833 mutex_unlock(&user->file_lock);
837 static ssize_t sbefifo_user_write(struct file *file, const char __user *buf,
838 size_t len, loff_t *offset)
840 struct sbefifo_user *user = file->private_data;
841 struct sbefifo *sbefifo;
846 sbefifo = user->sbefifo;
847 if (len > SBEFIFO_MAX_USER_CMD_LEN)
852 mutex_lock(&user->file_lock);
854 /* Can we use the pre-allocate buffer ? If not, allocate */
855 if (len <= PAGE_SIZE)
856 user->pending_cmd = user->cmd_page;
858 user->pending_cmd = vmalloc(len);
859 if (!user->pending_cmd) {
864 /* Copy the command into the staging buffer */
865 if (copy_from_user(user->pending_cmd, buf, len)) {
870 /* Check for the magic reset command */
871 if (len == 4 && be32_to_cpu(*(__be32 *)user->pending_cmd) ==
872 SBEFIFO_RESET_MAGIC) {
874 /* Clear out any pending command */
875 user->pending_len = 0;
877 /* Trigger reset request */
878 mutex_lock(&sbefifo->lock);
879 rc = sbefifo_request_reset(user->sbefifo);
880 mutex_unlock(&sbefifo->lock);
886 /* Update the staging buffer size */
887 user->pending_len = len;
889 if (!user->pending_len)
890 sbefifo_release_command(user);
892 mutex_unlock(&user->file_lock);
894 /* And that's it, we'll issue the command on a read */
898 static int sbefifo_user_release(struct inode *inode, struct file *file)
900 struct sbefifo_user *user = file->private_data;
905 sbefifo_release_command(user);
906 free_page((unsigned long)user->cmd_page);
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,
920 static void sbefifo_free(struct device *dev)
922 struct sbefifo *sbefifo = container_of(dev, struct sbefifo, dev);
924 put_device(&sbefifo->fsi_dev->dev);
932 static int sbefifo_probe(struct device *dev)
934 struct fsi_device *fsi_dev = to_fsi_dev(dev);
935 struct sbefifo *sbefifo;
936 struct device_node *np;
937 struct platform_device *child;
939 int rc, didx, child_idx = 0;
941 dev_dbg(dev, "Found sbefifo device\n");
943 sbefifo = kzalloc(sizeof(*sbefifo), GFP_KERNEL);
947 /* Grab a reference to the device (parent of our cdev), we'll drop it later */
948 if (!get_device(dev)) {
953 sbefifo->magic = SBEFIFO_MAGIC;
954 sbefifo->fsi_dev = fsi_dev;
955 dev_set_drvdata(dev, sbefifo);
956 mutex_init(&sbefifo->lock);
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.
962 rc = sbefifo_cleanup_hw(sbefifo);
963 if (rc && rc != -ESHUTDOWN)
964 dev_err(dev, "Initial HW cleanup failed, will retry later\n");
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);
972 /* Allocate a minor in the FSI space */
973 rc = fsi_get_new_minor(fsi_dev, fsi_dev_sbefifo, &sbefifo->dev.devt, &didx);
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);
981 dev_err(dev, "Error %d creating char device %s\n",
982 rc, dev_name(&sbefifo->dev));
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);
992 dev_warn(dev, "failed to create child %s dev\n",
998 fsi_free_minor(sbefifo->dev.devt);
1000 put_device(&sbefifo->dev);
1004 static int sbefifo_unregister_child(struct device *dev, void *data)
1006 struct platform_device *child = to_platform_device(dev);
1008 of_device_unregister(child);
1010 of_node_clear_flag(dev->of_node, OF_POPULATED);
1015 static int sbefifo_remove(struct device *dev)
1017 struct sbefifo *sbefifo = dev_get_drvdata(dev);
1019 dev_dbg(dev, "Removing sbefifo device...\n");
1021 mutex_lock(&sbefifo->lock);
1022 sbefifo->dead = true;
1023 mutex_unlock(&sbefifo->lock);
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);
1033 static const struct fsi_device_id sbefifo_ids[] = {
1035 .engine_type = FSI_ENGID_SBE,
1036 .version = FSI_VERSION_ANY,
1041 static struct fsi_driver sbefifo_drv = {
1042 .id_table = sbefifo_ids,
1044 .name = DEVICE_NAME,
1045 .bus = &fsi_bus_type,
1046 .probe = sbefifo_probe,
1047 .remove = sbefifo_remove,
1051 static int sbefifo_init(void)
1053 return fsi_driver_register(&sbefifo_drv);
1056 static void sbefifo_exit(void)
1058 fsi_driver_unregister(&sbefifo_drv);
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");