1 // SPDX-License-Identifier: GPL-2.0+
2 // Copyright IBM Corp 2019
4 #include <linux/device.h>
5 #include <linux/errno.h>
6 #include <linux/slab.h>
7 #include <linux/fsi-occ.h>
9 #include <linux/module.h>
10 #include <linux/mutex.h>
11 #include <linux/platform_device.h>
12 #include <linux/string.h>
13 #include <linux/sysfs.h>
23 struct mutex sbe_error_lock; /* lock access to ffdc data */
27 #define to_p9_sbe_occ(x) container_of((x), struct p9_sbe_occ, occ)
29 static ssize_t ffdc_read(struct file *filp, struct kobject *kobj,
30 struct bin_attribute *battr, char *buf, loff_t pos,
34 struct occ *occ = dev_get_drvdata(kobj_to_dev(kobj));
35 struct p9_sbe_occ *ctx = to_p9_sbe_occ(occ);
37 mutex_lock(&ctx->sbe_error_lock);
39 rc = memory_read_from_buffer(buf, count, &pos, ctx->ffdc,
41 if (pos >= ctx->ffdc_len)
42 ctx->sbe_error = false;
44 mutex_unlock(&ctx->sbe_error_lock);
48 static BIN_ATTR_RO(ffdc, OCC_MAX_RESP_WORDS * 4);
50 static bool p9_sbe_occ_save_ffdc(struct p9_sbe_occ *ctx, const void *resp,
55 mutex_lock(&ctx->sbe_error_lock);
56 if (!ctx->sbe_error) {
57 if (resp_len > ctx->ffdc_size) {
59 ctx->ffdc = kvmalloc(resp_len, GFP_KERNEL);
66 ctx->ffdc_size = resp_len;
70 ctx->sbe_error = true;
71 ctx->ffdc_len = resp_len;
72 memcpy(ctx->ffdc, resp, resp_len);
76 mutex_unlock(&ctx->sbe_error_lock);
80 static int p9_sbe_occ_send_cmd(struct occ *occ, u8 *cmd, size_t len,
81 void *resp, size_t resp_len)
83 struct p9_sbe_occ *ctx = to_p9_sbe_occ(occ);
86 rc = fsi_occ_submit(ctx->sbe, cmd, len, resp, &resp_len);
89 if (p9_sbe_occ_save_ffdc(ctx, resp, resp_len))
90 sysfs_notify(&occ->bus_dev->kobj, NULL,
91 bin_attr_ffdc.attr.name);
97 switch (((struct occ_response *)resp)->return_status) {
98 case OCC_RESP_CMD_IN_PRG:
101 case OCC_RESP_SUCCESS:
104 case OCC_RESP_CMD_INVAL:
105 case OCC_RESP_CMD_LEN_INVAL:
106 case OCC_RESP_DATA_INVAL:
107 case OCC_RESP_CHKSUM_ERR:
110 case OCC_RESP_INT_ERR:
111 case OCC_RESP_BAD_STATE:
112 case OCC_RESP_CRIT_EXCEPT:
113 case OCC_RESP_CRIT_INIT:
114 case OCC_RESP_CRIT_WATCHDOG:
115 case OCC_RESP_CRIT_OCB:
116 case OCC_RESP_CRIT_HW:
126 static int p9_sbe_occ_probe(struct platform_device *pdev)
130 struct p9_sbe_occ *ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx),
135 mutex_init(&ctx->sbe_error_lock);
137 ctx->sbe = pdev->dev.parent;
139 occ->bus_dev = &pdev->dev;
140 platform_set_drvdata(pdev, occ);
142 occ->powr_sample_time_us = 500;
143 occ->poll_cmd_data = 0x20; /* P9 OCC poll data */
144 occ->send_cmd = p9_sbe_occ_send_cmd;
147 if (rc == -ESHUTDOWN)
148 rc = -ENODEV; /* Host is shutdown, don't spew errors */
151 rc = device_create_bin_file(occ->bus_dev, &bin_attr_ffdc);
153 dev_warn(occ->bus_dev,
154 "failed to create SBE error ffdc file\n");
162 static int p9_sbe_occ_remove(struct platform_device *pdev)
164 struct occ *occ = platform_get_drvdata(pdev);
165 struct p9_sbe_occ *ctx = to_p9_sbe_occ(occ);
167 device_remove_bin_file(occ->bus_dev, &bin_attr_ffdc);
177 static struct platform_driver p9_sbe_occ_driver = {
181 .probe = p9_sbe_occ_probe,
182 .remove = p9_sbe_occ_remove,
185 module_platform_driver(p9_sbe_occ_driver);
187 MODULE_AUTHOR("Eddie James <eajames@linux.ibm.com>");
188 MODULE_DESCRIPTION("BMC P9 OCC hwmon driver");
189 MODULE_LICENSE("GPL");