1 // SPDX-License-Identifier: GPL-2.0+ 2 // Copyright IBM Corp 2019 3 4 #include <linux/device.h> 5 #include <linux/errno.h> 6 #include <linux/slab.h> 7 #include <linux/fsi-occ.h> 8 #include <linux/mm.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> 14 15 #include "common.h" 16 17 #define OCC_CHECKSUM_RETRIES 3 18 19 struct p9_sbe_occ { 20 struct occ occ; 21 bool sbe_error; 22 void *ffdc; 23 size_t ffdc_len; 24 size_t ffdc_size; 25 struct mutex sbe_error_lock; /* lock access to ffdc data */ 26 struct device *sbe; 27 }; 28 29 #define to_p9_sbe_occ(x) container_of((x), struct p9_sbe_occ, occ) 30 31 static ssize_t ffdc_read(struct file *filp, struct kobject *kobj, 32 const struct bin_attribute *battr, char *buf, loff_t pos, 33 size_t count) 34 { 35 ssize_t rc = 0; 36 struct occ *occ = dev_get_drvdata(kobj_to_dev(kobj)); 37 struct p9_sbe_occ *ctx = to_p9_sbe_occ(occ); 38 39 mutex_lock(&ctx->sbe_error_lock); 40 if (ctx->sbe_error) { 41 rc = memory_read_from_buffer(buf, count, &pos, ctx->ffdc, 42 ctx->ffdc_len); 43 if (pos >= ctx->ffdc_len) 44 ctx->sbe_error = false; 45 } 46 mutex_unlock(&ctx->sbe_error_lock); 47 48 return rc; 49 } 50 static const BIN_ATTR_RO(ffdc, OCC_MAX_RESP_WORDS * 4); 51 52 static bool p9_sbe_occ_save_ffdc(struct p9_sbe_occ *ctx, const void *resp, 53 size_t resp_len) 54 { 55 bool notify = false; 56 57 mutex_lock(&ctx->sbe_error_lock); 58 if (!ctx->sbe_error) { 59 if (resp_len > ctx->ffdc_size) { 60 kvfree(ctx->ffdc); 61 ctx->ffdc = kvmalloc(resp_len, GFP_KERNEL); 62 if (!ctx->ffdc) { 63 ctx->ffdc_len = 0; 64 ctx->ffdc_size = 0; 65 goto done; 66 } 67 68 ctx->ffdc_size = resp_len; 69 } 70 71 notify = true; 72 ctx->sbe_error = true; 73 ctx->ffdc_len = resp_len; 74 memcpy(ctx->ffdc, resp, resp_len); 75 } 76 77 done: 78 mutex_unlock(&ctx->sbe_error_lock); 79 return notify; 80 } 81 82 static int p9_sbe_occ_send_cmd(struct occ *occ, u8 *cmd, size_t len, 83 void *resp, size_t resp_len) 84 { 85 size_t original_resp_len = resp_len; 86 struct p9_sbe_occ *ctx = to_p9_sbe_occ(occ); 87 int rc, i; 88 89 for (i = 0; i < OCC_CHECKSUM_RETRIES; ++i) { 90 rc = fsi_occ_submit(ctx->sbe, cmd, len, resp, &resp_len); 91 if (rc >= 0) 92 break; 93 if (resp_len) { 94 if (p9_sbe_occ_save_ffdc(ctx, resp, resp_len)) 95 sysfs_notify(&occ->bus_dev->kobj, NULL, 96 bin_attr_ffdc.attr.name); 97 return rc; 98 } 99 if (rc != -EBADE) 100 return rc; 101 resp_len = original_resp_len; 102 } 103 104 switch (((struct occ_response *)resp)->return_status) { 105 case OCC_RESP_CMD_IN_PRG: 106 rc = -ETIMEDOUT; 107 break; 108 case OCC_RESP_SUCCESS: 109 rc = 0; 110 break; 111 case OCC_RESP_CMD_INVAL: 112 case OCC_RESP_CMD_LEN_INVAL: 113 case OCC_RESP_DATA_INVAL: 114 case OCC_RESP_CHKSUM_ERR: 115 rc = -EINVAL; 116 break; 117 case OCC_RESP_INT_ERR: 118 case OCC_RESP_BAD_STATE: 119 case OCC_RESP_CRIT_EXCEPT: 120 case OCC_RESP_CRIT_INIT: 121 case OCC_RESP_CRIT_WATCHDOG: 122 case OCC_RESP_CRIT_OCB: 123 case OCC_RESP_CRIT_HW: 124 rc = -EREMOTEIO; 125 break; 126 default: 127 rc = -EPROTO; 128 } 129 130 return rc; 131 } 132 133 static int p9_sbe_occ_probe(struct platform_device *pdev) 134 { 135 int rc; 136 struct occ *occ; 137 struct p9_sbe_occ *ctx = devm_kzalloc_obj(&pdev->dev, *ctx); 138 if (!ctx) 139 return -ENOMEM; 140 141 mutex_init(&ctx->sbe_error_lock); 142 143 ctx->sbe = pdev->dev.parent; 144 occ = &ctx->occ; 145 occ->bus_dev = &pdev->dev; 146 platform_set_drvdata(pdev, occ); 147 148 occ->powr_sample_time_us = 500; 149 occ->poll_cmd_data = 0x20; /* P9 OCC poll data */ 150 occ->send_cmd = p9_sbe_occ_send_cmd; 151 152 rc = occ_setup(occ); 153 if (rc == -ESHUTDOWN) 154 rc = -ENODEV; /* Host is shutdown, don't spew errors */ 155 156 if (!rc) { 157 rc = device_create_bin_file(occ->bus_dev, &bin_attr_ffdc); 158 if (rc) { 159 dev_warn(occ->bus_dev, 160 "failed to create SBE error ffdc file\n"); 161 rc = 0; 162 } 163 } 164 165 return rc; 166 } 167 168 static void p9_sbe_occ_remove(struct platform_device *pdev) 169 { 170 struct occ *occ = platform_get_drvdata(pdev); 171 struct p9_sbe_occ *ctx = to_p9_sbe_occ(occ); 172 173 device_remove_bin_file(occ->bus_dev, &bin_attr_ffdc); 174 175 ctx->sbe = NULL; 176 occ_shutdown(occ); 177 178 kvfree(ctx->ffdc); 179 } 180 181 static const struct of_device_id p9_sbe_occ_of_match[] = { 182 { .compatible = "ibm,p9-occ-hwmon" }, 183 { .compatible = "ibm,p10-occ-hwmon" }, 184 {} 185 }; 186 MODULE_DEVICE_TABLE(of, p9_sbe_occ_of_match); 187 188 static struct platform_driver p9_sbe_occ_driver = { 189 .driver = { 190 .name = "occ-hwmon", 191 .of_match_table = p9_sbe_occ_of_match, 192 }, 193 .probe = p9_sbe_occ_probe, 194 .remove = p9_sbe_occ_remove, 195 }; 196 197 module_platform_driver(p9_sbe_occ_driver); 198 199 MODULE_AUTHOR("Eddie James <eajames@linux.ibm.com>"); 200 MODULE_DESCRIPTION("BMC P9 OCC hwmon driver"); 201 MODULE_LICENSE("GPL"); 202