1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/device.h>
5 #include <linux/errno.h>
7 #include <linux/fsi-sbefifo.h>
10 #include <linux/kernel.h>
11 #include <linux/list.h>
12 #include <linux/miscdevice.h>
13 #include <linux/module.h>
14 #include <linux/mutex.h>
15 #include <linux/fsi-occ.h>
17 #include <linux/platform_device.h>
18 #include <linux/sched.h>
19 #include <linux/slab.h>
20 #include <linux/uaccess.h>
21 #include <asm/unaligned.h>
23 #define OCC_SRAM_BYTES 4096
24 #define OCC_CMD_DATA_BYTES 4090
25 #define OCC_RESP_DATA_BYTES 4089
27 #define OCC_SRAM_CMD_ADDR 0xFFFBE000
28 #define OCC_SRAM_RSP_ADDR 0xFFFBF000
31 * Assume we don't have much FFDC, if we do we'll overflow and
32 * fail the command. This needs to be big enough for simple
35 #define OCC_SBE_STATUS_WORDS 32
37 #define OCC_TIMEOUT_MS 1000
38 #define OCC_CMD_IN_PRG_WAIT_MS 50
42 struct device *sbefifo;
45 struct miscdevice mdev;
46 struct mutex occ_lock;
49 #define to_occ(x) container_of((x), struct occ, mdev)
56 u8 data[OCC_RESP_DATA_BYTES + 2]; /* two bytes checksum */
67 #define to_client(x) container_of((x), struct occ_client, xfr)
69 static DEFINE_IDA(occ_ida);
71 static int occ_open(struct inode *inode, struct file *file)
73 struct occ_client *client = kzalloc(sizeof(*client), GFP_KERNEL);
74 struct miscdevice *mdev = file->private_data;
75 struct occ *occ = to_occ(mdev);
80 client->buffer = (u8 *)__get_free_page(GFP_KERNEL);
81 if (!client->buffer) {
87 mutex_init(&client->lock);
88 file->private_data = client;
90 /* We allocate a 1-page buffer, make sure it all fits */
91 BUILD_BUG_ON((OCC_CMD_DATA_BYTES + 3) > PAGE_SIZE);
92 BUILD_BUG_ON((OCC_RESP_DATA_BYTES + 7) > PAGE_SIZE);
97 static ssize_t occ_read(struct file *file, char __user *buf, size_t len,
100 struct occ_client *client = file->private_data;
106 if (len > OCC_SRAM_BYTES)
109 mutex_lock(&client->lock);
111 /* This should not be possible ... */
112 if (WARN_ON_ONCE(client->read_offset > client->data_size)) {
117 /* Grab how much data we have to read */
118 rc = min(len, client->data_size - client->read_offset);
119 if (copy_to_user(buf, client->buffer + client->read_offset, rc))
122 client->read_offset += rc;
125 mutex_unlock(&client->lock);
130 static ssize_t occ_write(struct file *file, const char __user *buf,
131 size_t len, loff_t *offset)
133 struct occ_client *client = file->private_data;
134 size_t rlen, data_length;
142 if (len > (OCC_CMD_DATA_BYTES + 3) || len < 3)
145 mutex_lock(&client->lock);
147 /* Construct the command */
148 cmd = client->buffer;
150 /* Sequence number (we could increment and compare with response) */
154 * Copy the user command (assume user data follows the occ command
156 * byte 0: command type
157 * bytes 1-2: data length (msb first)
160 if (copy_from_user(&cmd[1], buf, len)) {
165 /* Extract data length */
166 data_length = (cmd[2] << 8) + cmd[3];
167 if (data_length > OCC_CMD_DATA_BYTES) {
172 /* Calculate checksum */
173 for (i = 0; i < data_length + 4; ++i)
176 cmd[data_length + 4] = checksum >> 8;
177 cmd[data_length + 5] = checksum & 0xFF;
181 rc = fsi_occ_submit(client->occ->dev, cmd, data_length + 6, cmd,
186 /* Set read tracking data */
187 client->data_size = rlen;
188 client->read_offset = 0;
194 mutex_unlock(&client->lock);
199 static int occ_release(struct inode *inode, struct file *file)
201 struct occ_client *client = file->private_data;
203 free_page((unsigned long)client->buffer);
209 static const struct file_operations occ_fops = {
210 .owner = THIS_MODULE,
214 .release = occ_release,
217 static int occ_verify_checksum(struct occ_response *resp, u16 data_length)
219 /* Fetch the two bytes after the data for the checksum. */
220 u16 checksum_resp = get_unaligned_be16(&resp->data[data_length]);
224 checksum = resp->seq_no;
225 checksum += resp->cmd_type;
226 checksum += resp->return_status;
227 checksum += (data_length >> 8) + (data_length & 0xFF);
229 for (i = 0; i < data_length; ++i)
230 checksum += resp->data[i];
232 if (checksum != checksum_resp)
238 static int occ_getsram(struct occ *occ, u32 address, void *data, ssize_t len)
240 u32 data_len = ((len + 7) / 8) * 8; /* must be multiples of 8 B */
241 size_t resp_len, resp_data_len;
242 __be32 *resp, cmd[5];
246 * Magic sequence to do SBE getsram command. SBE will fetch data from
247 * specified SRAM address.
249 cmd[0] = cpu_to_be32(0x5);
250 cmd[1] = cpu_to_be32(SBEFIFO_CMD_GET_OCC_SRAM);
251 cmd[2] = cpu_to_be32(1);
252 cmd[3] = cpu_to_be32(address);
253 cmd[4] = cpu_to_be32(data_len);
255 resp_len = (data_len >> 2) + OCC_SBE_STATUS_WORDS;
256 resp = kzalloc(resp_len << 2, GFP_KERNEL);
260 rc = sbefifo_submit(occ->sbefifo, cmd, 5, resp, &resp_len);
264 rc = sbefifo_parse_status(occ->sbefifo, SBEFIFO_CMD_GET_OCC_SRAM,
265 resp, resp_len, &resp_len);
269 resp_data_len = be32_to_cpu(resp[resp_len - 1]);
270 if (resp_data_len != data_len) {
271 dev_err(occ->dev, "SRAM read expected %d bytes got %zd\n",
272 data_len, resp_data_len);
275 memcpy(data, resp, len);
279 /* Convert positive SBEI status */
281 dev_err(occ->dev, "SRAM read returned failure status: %08x\n",
290 static int occ_putsram(struct occ *occ, u32 address, const void *data,
293 size_t cmd_len, buf_len, resp_len, resp_data_len;
294 u32 data_len = ((len + 7) / 8) * 8; /* must be multiples of 8 B */
299 * We use the same buffer for command and response, make
300 * sure it's big enough
302 resp_len = OCC_SBE_STATUS_WORDS;
303 cmd_len = (data_len >> 2) + 5;
304 buf_len = max(cmd_len, resp_len);
305 buf = kzalloc(buf_len << 2, GFP_KERNEL);
310 * Magic sequence to do SBE putsram command. SBE will transfer
311 * data to specified SRAM address.
313 buf[0] = cpu_to_be32(cmd_len);
314 buf[1] = cpu_to_be32(SBEFIFO_CMD_PUT_OCC_SRAM);
315 buf[2] = cpu_to_be32(1);
316 buf[3] = cpu_to_be32(address);
317 buf[4] = cpu_to_be32(data_len);
319 memcpy(&buf[5], data, len);
321 rc = sbefifo_submit(occ->sbefifo, buf, cmd_len, buf, &resp_len);
325 rc = sbefifo_parse_status(occ->sbefifo, SBEFIFO_CMD_PUT_OCC_SRAM,
326 buf, resp_len, &resp_len);
331 dev_err(occ->dev, "SRAM write response length invalid: %zd\n",
335 resp_data_len = be32_to_cpu(buf[0]);
336 if (resp_data_len != data_len) {
338 "SRAM write expected %d bytes got %zd\n",
339 data_len, resp_data_len);
345 /* Convert positive SBEI status */
347 dev_err(occ->dev, "SRAM write returned failure status: %08x\n",
356 static int occ_trigger_attn(struct occ *occ)
358 __be32 buf[OCC_SBE_STATUS_WORDS];
359 size_t resp_len, resp_data_len;
362 BUILD_BUG_ON(OCC_SBE_STATUS_WORDS < 7);
363 resp_len = OCC_SBE_STATUS_WORDS;
365 buf[0] = cpu_to_be32(0x5 + 0x2); /* Chip-op length in words */
366 buf[1] = cpu_to_be32(SBEFIFO_CMD_PUT_OCC_SRAM);
367 buf[2] = cpu_to_be32(0x3); /* Mode: Circular */
368 buf[3] = cpu_to_be32(0x0); /* Address: ignore in mode 3 */
369 buf[4] = cpu_to_be32(0x8); /* Data length in bytes */
370 buf[5] = cpu_to_be32(0x20010000); /* Trigger OCC attention */
373 rc = sbefifo_submit(occ->sbefifo, buf, 7, buf, &resp_len);
377 rc = sbefifo_parse_status(occ->sbefifo, SBEFIFO_CMD_PUT_OCC_SRAM,
378 buf, resp_len, &resp_len);
383 dev_err(occ->dev, "SRAM attn response length invalid: %zd\n",
387 resp_data_len = be32_to_cpu(buf[0]);
388 if (resp_data_len != 8) {
390 "SRAM attn expected 8 bytes got %zd\n",
397 /* Convert positive SBEI status */
399 dev_err(occ->dev, "SRAM attn returned failure status: %08x\n",
407 int fsi_occ_submit(struct device *dev, const void *request, size_t req_len,
408 void *response, size_t *resp_len)
410 const unsigned long timeout = msecs_to_jiffies(OCC_TIMEOUT_MS);
411 const unsigned long wait_time =
412 msecs_to_jiffies(OCC_CMD_IN_PRG_WAIT_MS);
413 struct occ *occ = dev_get_drvdata(dev);
414 struct occ_response *resp = response;
415 u16 resp_data_length;
423 dev_dbg(dev, "Bad resplen %zd\n", *resp_len);
427 mutex_lock(&occ->occ_lock);
429 rc = occ_putsram(occ, OCC_SRAM_CMD_ADDR, request, req_len);
433 rc = occ_trigger_attn(occ);
437 /* Read occ response header */
440 rc = occ_getsram(occ, OCC_SRAM_RSP_ADDR, resp, 8);
444 if (resp->return_status == OCC_RESP_CMD_IN_PRG) {
447 if (time_after(jiffies, start + timeout))
450 set_current_state(TASK_UNINTERRUPTIBLE);
451 schedule_timeout(wait_time);
455 /* Extract size of response data */
456 resp_data_length = get_unaligned_be16(&resp->data_length);
458 /* Message size is data length + 5 bytes header + 2 bytes checksum */
459 if ((resp_data_length + 7) > *resp_len) {
464 dev_dbg(dev, "resp_status=%02x resp_data_len=%d\n",
465 resp->return_status, resp_data_length);
468 if (resp_data_length > 1) {
469 /* already got 3 bytes resp, also need 2 bytes checksum */
470 rc = occ_getsram(occ, OCC_SRAM_RSP_ADDR + 8,
471 &resp->data[3], resp_data_length - 1);
476 *resp_len = resp_data_length + 7;
477 rc = occ_verify_checksum(resp, resp_data_length);
480 mutex_unlock(&occ->occ_lock);
484 EXPORT_SYMBOL_GPL(fsi_occ_submit);
486 static int occ_unregister_child(struct device *dev, void *data)
488 struct platform_device *hwmon_dev = to_platform_device(dev);
490 platform_device_unregister(hwmon_dev);
495 static int occ_probe(struct platform_device *pdev)
500 struct platform_device *hwmon_dev;
501 struct device *dev = &pdev->dev;
502 struct platform_device_info hwmon_dev_info = {
507 occ = devm_kzalloc(dev, sizeof(*occ), GFP_KERNEL);
512 occ->sbefifo = dev->parent;
513 mutex_init(&occ->occ_lock);
516 rc = of_property_read_u32(dev->of_node, "reg", ®);
518 /* make sure we don't have a duplicate from dts */
519 occ->idx = ida_simple_get(&occ_ida, reg, reg + 1,
522 occ->idx = ida_simple_get(&occ_ida, 1, INT_MAX,
525 occ->idx = ida_simple_get(&occ_ida, 1, INT_MAX,
529 occ->idx = ida_simple_get(&occ_ida, 1, INT_MAX, GFP_KERNEL);
532 platform_set_drvdata(pdev, occ);
534 snprintf(occ->name, sizeof(occ->name), "occ%d", occ->idx);
535 occ->mdev.fops = &occ_fops;
536 occ->mdev.minor = MISC_DYNAMIC_MINOR;
537 occ->mdev.name = occ->name;
538 occ->mdev.parent = dev;
540 rc = misc_register(&occ->mdev);
542 dev_err(dev, "failed to register miscdevice: %d\n", rc);
543 ida_simple_remove(&occ_ida, occ->idx);
547 hwmon_dev_info.id = occ->idx;
548 hwmon_dev = platform_device_register_full(&hwmon_dev_info);
550 dev_warn(dev, "failed to create hwmon device\n");
555 static int occ_remove(struct platform_device *pdev)
557 struct occ *occ = platform_get_drvdata(pdev);
559 misc_deregister(&occ->mdev);
561 device_for_each_child(&pdev->dev, NULL, occ_unregister_child);
563 ida_simple_remove(&occ_ida, occ->idx);
568 static const struct of_device_id occ_match[] = {
569 { .compatible = "ibm,p9-occ" },
573 static struct platform_driver occ_driver = {
576 .of_match_table = occ_match,
579 .remove = occ_remove,
582 static int occ_init(void)
584 return platform_driver_register(&occ_driver);
587 static void occ_exit(void)
589 platform_driver_unregister(&occ_driver);
591 ida_destroy(&occ_ida);
594 module_init(occ_init);
595 module_exit(occ_exit);
597 MODULE_AUTHOR("Eddie James <eajames@linux.ibm.com>");
598 MODULE_DESCRIPTION("BMC P9 OCC driver");
599 MODULE_LICENSE("GPL");